Article View: comp.unix.programmer
Article #3666Re: gcc -O2 bug
From: Dan Pop
Date: Thu, 08 Aug 1996 00:00
Date: Thu, 08 Aug 1996 00:00
71 lines
3365 bytes
3365 bytes
In <vkohklaizj.fsf@atari.connectinc.com> bji@connectinc.com (Bryan Ischo) writes:
>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:
>
> >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?
Because flag is accessible via the flag_pt pointer, which is a global
variable, hence available to any function called by the program.
>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?
On the contrary, it is smart enough to know that flag CAN be modified by
a function called within the loop.
Dan
--
Dan Pop
CERN, CN Division
Email: Dan.Pop@cern.ch
Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
Technical Details
Message-ID:
<danpop.839541415@news.cern.ch>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<4ubnat$75u@freeside.fc.net> <danpop.839504721@news.cern.ch> <vkohklaizj.fsf@atari.connectinc.com>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: Dan.Pop@cern.ch (Dan Pop) Subject: Re: gcc -O2 bug Date: 1996/08/08 Message-ID: <danpop.839541415@news.cern.ch> X-Deja-AN: 173025999 sender: news@news.cern.ch (USENET News System) x-nntp-posting-host: ues5.cern.ch references: <4ubnat$75u@freeside.fc.net> <danpop.839504721@news.cern.ch> <vkohklaizj.fsf@atari.connectinc.com> organization: CERN European Lab for Particle Physics newsgroups: comp.unix.programmer,gnu.gcc.help,comp.os.linux.development.apps,comp.lang.c