Article View: comp.unix.programmer
Article #3662Re: gcc -O2 bug
From: Bryan Ischo
Date: Thu, 08 Aug 1996 00:00
Date: Thu, 08 Aug 1996 00:00
83 lines
3838 bytes
3838 bytes
In article <danpop.839504721@news.cern.ch> Dan.Pop@cern.ch (Dan Pop) writes:
In <4ubnat$75u@freeside.fc.net> mrbig@freeside.fc.net (Mike Big) writes:
[stuff skipped]
>#include <stdio.h>
>#include <sys/time.h>
>#include <sys/types.h>
>#include <signal.h>
>
>void fca(); /* SIGALRM handler function */
>
>long counter;
>int *flag_pt;
>
>int main()
>{
> struct itimerval timer_length;
> int flag;
>
> flag_pt=&flag;
> /* specify signal handler */
> signal(SIGALRM, fca);
>
> /* set the signal to repeat every 3 seconds */
> timer_length.it_value.tv_sec=3; /* initial time */
> timer_length.it_value.tv_usec=0;
> timer_length.it_interval.tv_sec=3; /* repeat */
> timer_length.it_interval.tv_usec=0;
> setitimer(ITIMER_REAL, &timer_length, NULL);
>
> flag=time(NULL); /* this is so that the compiler won't substitute a
> constant for flag as it might if I did something
> like flag=1; */
>
> /* count until interrupted (after 3 seconds) */
> counter=0;
> while(flag){ /* does NOT exit when using gcc -O2 */
[more skippage]
> counter++;
> /* uncomenting the printf below will fix things. weird heh */
There is nothing weird here. flag is accessible via a global pointer
so printf might change its value, as a side effect. The compiler can't
tell whether this will happen or not, so it cannot assume that flag is
a loop invariant in the presence of a function call inside the loop.
The above-mentioned optimization is illegal now. A smarter compiler
might have more information about printf and decide from its invocation
that it cannot modify flag and still do the optimization :-)
> /* printf("counter=%ld flag=%d\n",counter,flag); */
> }
>
> exit(0);
[skipped]
Just out of curiosity ... why can't the compiler be sure that 'flag' won't be
modified by printf? After all, its type is declared as int, not int *, and
the loop conditional is 'while(flag)' not 'while (*flag)'. What I'm getting
at is, the only way flag could be modified is if the programmer is assuming
that the size of an int is the same as the size of an int * and is passing in
an int in place of an int *, presumably one which has been casted from an
int * somewhere earlier. And even then, the loop conditional is
'while(flag)', not 'while(*flag)', so even in the case that 'flag' was really
an int * in disguise, it never gets dereferenced anyhere.
Shouldn't the compiler be smart enough to know that flag can't be modified
even IF the printf is in the loop, given how smart it's been about everything
else?
Thanks,
Bryan
--
------------------------------------------------------------------------
Bryan Ischo mailto:bji@connectinc.com
Mountain View, CA, USA http://www.connectinc.com
Linux Aficionado 1990 Honda CB-1
Technical Details
Message-ID:
<vkohklaizj.fsf@atari.connectinc.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<4ubnat$75u@freeside.fc.net> <danpop.839504721@news.cern.ch>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11318c,7d30cae02ba9b089 X-Google-Attributes: gid11318c,public X-Google-Thread: 10c696,7d30cae02ba9b089 X-Google-Attributes: gid10c696,public X-Google-Thread: fc89c,7d30cae02ba9b089 X-Google-Attributes: gidfc89c,public X-Google-Thread: 1014db,7d30cae02ba9b089 X-Google-Attributes: gid1014db,public From: bji@connectinc.com (Bryan Ischo) Subject: Re: gcc -O2 bug Date: 1996/08/08 Message-ID: <vkohklaizj.fsf@atari.connectinc.com> X-Deja-AN: 172947559 sender: bji@atari.connectinc.com references: <4ubnat$75u@freeside.fc.net> <danpop.839504721@news.cern.ch> organization: Connect, Inc. newsgroups: comp.unix.programmer,gnu.gcc.help,comp.os.linux.development.apps,comp.lang.c