This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Bug in loop optimize (invalid postinc to preinc transformation)


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]