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]

Gcc-3.0 unroll bug


I found a bug with loop unrolling. I am using gcc-3.0 on linux powerpc
but I believe the bug is independent of the target architecture.

Consider the following for statement:
         for (i = UINT_MAX - 1; i > 5; i++)
This statement should execute 2 times because, after that,
the i.v. will turn 0 and the condition (i > 5) will be false.

If loop unrolling is enabled the loop will be unrolled 3 times,
giving the wrong answer.

Regards
Edmar W. Jr.





Here is the result of gcc -v:
=============================
Reading specs from
/home/edmar/compiler/gnu/installed/gcc_3_0/lib/gcc-lib/powerpc-unknown-linux-gnu/

3.0/specs
Configured with: ../../gcc-3.0/configure
--prefix=/home/edmar/compiler/gnu/installed/gcc_3_0 --with-
local-prefix=/home/edmar/compiler/gnu/installed/include
--enable-version-specific-runtime-libs --wit
h-gnu-as --with-gnu-ld --enable-cpp --enable-shared
--enable-languages=c++,f77 --enable-haifa
Thread model: single
gcc version 3.0


Here is the code that fails:
============================
#include <stdio.h>
#include <limits.h>

int a[32] = {1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1,
             1, 1, 1, 1, 1, 1, 1, 1};

int main ()
{
  unsigned int i;
  int j;
  int sum;

  sum = 0;

  j = 5;
  for (i = UINT_MAX - 1; i > 5; i++)
    {
      sum += a[j];
      j++;
    }

  printf ("Sum is %d, final j is: %d\n", sum, j);
}


Here is how I compiled and executed it:
=======================================
#!/usr/local/bin/bash

CC=/home/edmar/compiler/gnu/installed/gcc_3_0/bin/gcc
OPTS="-mcpu=750 -O2"

$CC $OPTS -S unroll.c -o unroll_1.s
$CC $OPTS unroll.c -o unroll_1
unroll_1
$CC $OPTS -funroll-loops -S unroll.c -o unroll_2.s
$CC $OPTS -funroll-loops unroll.c -o unroll_2
unroll_2


Here is the output of the above script:
=======================================
Sum is 2, final j is: 7
Sum is 3, final j is: 8


Here is the significant part in the assembler outputed:
=======================================================
main:                          main:
        stwu 1,-16(1)                  stwu 1,-16(1)
        mflr 0                         mflr 0
        lis 9,a@ha                     lis 9,a@ha
        stw 0,20(1)                    stw 0,20(1)
        li 4,0               |         la 9,a@l(9)
        la 10,a@l(9)         |         lwz 4,20(9)
        li 5,5               |         lwz 0,24(9)
        li 11,-2             |         lwz 11,28(9)
.L20:                        |         add 4,4,0
        addi 11,11,1         |         add 4,4,11
        slwi 0,5,2           <
        cmplwi 0,11,5        <
        lwzx 9,10,0          <
        addi 5,5,1           <
        add 4,4,9            <
        bgt- 0,.L20          <
        lis 3,.LC0@ha                  lis 3,.LC0@ha
                             >         li 5,8
        la 3,.LC0@l(3)                 la 3,.LC0@l(3)
        crxor 6,6,6                    crxor 6,6,6
        bl printf                      bl printf
        lwz 0,20(1)                    lwz 0,20(1)
        addi 1,1,16                    addi 1,1,16
        mtlr 0                         mtlr 0
        blr                            blr



--
/*----------------------------------------------------------------------*/
/* Edmar Wienskoski Jr.                   Motorola Inc    TX32  MD:PL30 */
/* (512)996-4935 fax:(512)996-7439        7700 W Parmer Lane, Bldg C    */
/* edmar@motorola.com                     Austin, Texas 78729           */
/*----------------------------------------------------------------------*/
     ____
    | [] |                                                 ______()_||_
 ---+----+---  ------------  ------------  ------------     | []       |
 | |      | |  |          |  |          |  |          |  ___|          |
 |_|______|_|  |__________|  |__________|  |__________|  |______________\
"o-o      o-o""o-o      o-o""o-o      o-o""o-o      o-o""o-o  O-O-O  o-o "




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