This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Bug in loop optimize (invalid postinc to preinc transformation)
- To: David Korn <dkorn at pixelpower dot com>
- Subject: Re: Bug in loop optimize (invalid postinc to preinc transformation)
- From: Peter Osterlund <peter dot osterlund at mailbox dot swipnet dot se>
- Date: 28 Dec 2000 00:41:04 +0100
- Cc: "'Denis Chertykov'" <denisc at overta dot ru>, gcc at gcc dot gnu dot org
- References: <718D38CAB6E0D011B2C90060970C28A5642545@EXCHANGESERVER>
David Korn <dkorn@pixelpower.com> writes:
> >-----Original Message-----
> >From: Denis Chertykov [mailto:denisc@overta.ru]
> >Sent: 26 December 2000 20:06
>
> >"Alexander Popov" <sasho@vip.orbitel.bg> have founded a bug in generic
> >GCC code.
> >I have tried avr and x86 ports.
> >
> >The XXX.rtl file already have a bug.
> >
> >
> >volatile unsigned char *p;
> >volatile unsigned char i;
> >
> >int main(void) {
> >
> > do {
> > i++;
> > } while(p++<(unsigned char *)0xffffffff);
> > /* if 0xffffffff change to 0xfffffffe then no bug */
> > return 0;
> >}
>
> This is undefined behaviour. Since you haven't initialised variable p,
> the compiler is free to implement whatever it likes here. In particular,
No it is not. The p and i variables have static storage, so they are
implicitly initialized to 0. (As expected, even if you explicitly
initialize the variables, the bug doesn't go away.)
Compiling without optimization indicates that the compiler is
transforming (p++ < x) into (++p < (x+1)), even when not optimizing.
This transformation is incorrect because x+1 wraps around. At -O1 the
compiler realizes that testing for <0 will always fail and therefore
the test is removed, resulting in the assembly code below.
(This bug also exists in gcc 2.95 and egcs 1.1.2)
> >.globl main
> > .type main,@function
> >main:
> > movb i, %al
> > pushl %ebp
> > incl %eax
> > movl %esp, %ebp
> > incl p
> > movb %al, i
> > xorl %eax, %eax
> > popl %ebp
> > ret
--
Peter Österlund Email: peter.osterlund@mailbox.swipnet.se
Sköndalsvägen 35 f90-pos@nada.kth.se
S-128 66 Sköndal Home page: http://home1.swipnet.se/~w-15919
Sweden Phone: +46 8 942647