Bug in optimizer (-O2 and -O3)

Peter Leven p-leven@bird.ai.uiuc.edu
Wed Jan 28 10:06:00 GMT 1998


Hello,

I have found an optimizer bug in egcs-1.0.1 when running the test suite
after compilation.  The system on which I compiled egcs is
sparc-sun-solaris2.5.1.  The file for which this bug appears is
loop-2f.c in the gcc/testsuite/gcc.c-torture/execute directory.
The bug appears to be fairly old, since I get the same result
using gcc-2.6.3, gcc-2.7.2.1, and gcc-2.8.0 on sparc and gcc-2.7.2 on
linux.

egcs version: egcs-2.90.23 980102 (egcs-1.0.1 release)

Relevant code segment:

f (int s, char *p)
{
  int i;
  for (i = s; i >= 0 && &p[i] < &p[40]; i++)
    {
      p[i] = -2;
    }
}

With optimization level O2 and O3, the variable i is optimized out
and the first comparison in the for loop (i >= 0) is converted
into i + p >= p.  At lower levels of optimization, or when the
value of i is used elsewhere in the function, this conversion does
not take place.  This probably would not normally be a problem,
except that the test program uses an address of p at the boundary
between + and - for signed integers (0x7ffffff8).  Because the
comparison function used is signed, the comparison fails when 
i reaches 0x80000000.

The problem in this case is that the optimizer is not expecting the
overflow in the integer representation.

Of course, for this particular case, the i >= 0 test could be optimized
out of the for loop altogether and just happen once at the beginning
of the function.

The assembly output:

Option: -O				Option: -O2
==============================          ==============================

	.align 4                             	.align 4
.stabs "f:F(0,1)",36,0,29,f            .stabs "f:F(0,1)",36,0,29,f
.stabs "s:P(0,1)",64,0,28,8            .stabs "s:P(0,1)",64,0,28,8
.stabs "p:P(5,6)",64,0,28,9            .stabs "p:P(5,6)",64,0,28,9
	.global f                               .global f
	.type    f,#function                    .type    f,#function
	.proc   04                              .proc   04
f:                                     f:
.stabn 68,0,29,.LM1-f                  .stabn 68,0,29,.LM1-f
.LM1:                                  .LM1:
	!#PROLOGUE# 0                           !#PROLOGUE# 0
	!#PROLOGUE# 1                           !#PROLOGUE# 1
.stabn 68,0,30,.LM2-f                  .stabn 68,0,30,.LM2-f
.LM2:                                  .LM2:
.LLBB2:                                .LLBB2:
.stabn 68,0,31,.LM3-f                  .stabn 68,0,31,.LM3-f
.LM3:                                  .LM3:
	cmp %o0,0                               cmp %o0,0
	bl .LL3                                 bl .LL3
	add %o1,40,%g3                          add %o1,40,%g2
	mov -2,%o2                              mov -2,%g3
	add %o1,%o0,%g2                         add %o0,%o1,%o0
.LL8:                                           cmp %o0,%g2
	cmp %g2,%g3                    .LL8:
	bgeu .LL3                               bgeu .LL3
	nop                                     nop
.stabn 68,0,33,.LM4-f                  .stabn 68,0,33,.LM4-f
.LM4:                                  .LM4:
	stb %o2,[%o1+%o0]                       stb %g3,[%o0]
.stabn 68,0,31,.LM5-f                  .stabn 68,0,31,.LM5-f
.LM5:                                  .LM5:
	addcc %o0,1,%o0                         add %o0,1,%o0
	bpos .LL8                               cmp %o0,%o1
	add %o1,%o0,%g2                         bge .LL8
.LL3:                                           cmp %o0,%g2
.stabn 68,0,35,.LM6-f                  .LL3:
.LM6:                                  .stabn 68,0,35,.LM6-f
.LLBE2:                                .LM6:
	retl                           .LLBE2:
	nop                                     retl
.LLfe1:                                         nop
	.size    f,.LLfe1-f            .LLfe1:
                                        	.size	 f,.LLfe1-f
                              
Attached are the complete versions of the relevant files.

-Peter


P.S.  A relatively unrelated topic is that the standard template library
does not pass the -Weffc++ tests.  This makes it rather annoying to use the
option with one's one code, since one must find the messages relating to
one's own code in between the messages for the standard library.


-- 
**********************************************************************
*   Peter Leven                          *  p-leven@uiuc.edu         * 
**  Electrical and Computer Engineering  *  University of Illinois  **
**  Beckman Institute, MC 251            *  Urbana, IL  61801       **
*   http://www.uiuc.edu/ph/www/p-leven   *  (217) 244-1372           *
**********************************************************************


More information about the Gcc-bugs mailing list