Bug 000133

When Created: 02/02/1997 16:21:16
Against DJGPP version: 2.01
By whom:
Abstract: -funroll-loops can cause incorrect compilation
When compiling the small program below with "gcc -O -o test test.c" or similar, the program created will run fine. When using "gcc -O -funroll-loops -o test test.c" however, it crashes. When examining the assembly output, it seems the algorithm used by the compiler to unroll the loop is something like:
 A=0; V=0;
 do { SRAM[V]=A; SRAM[V+1]=A+1; SRAM[V+2]=A+2; SRAM[V+3]=A+3; 
 A+=4; V+=4; } while (V);
Which will cause V never to reach 0. You get similar results when using integers instead of bytes

Program listing follows:

#include <stdio.h>

typedef unsigned char byte;
typedef unsigned short word;

byte SRAM[256];

int main (void)
 byte V;
 word A;
 do SRAM[V++]=A++; while (V);
 printf ("Done\n");

Workaround added: 02/02/1997 16:23:25
By whom:
Rewriting the loop as
 for (I=0;I<256;++I) SRAM[I]=A++;
where I is an integer, will do the job

Note added: 02/15/1997 07:43:51
By whom:
This seems to be a general gcc bug, which is not specific to
DJGPP. Looks like it should be posted to gnu.gcc.bug.

Closed on 04/12/1999 12:00:53: A genuine GCC bug in optimizations.
By whom:

  webmaster     delorie software   privacy  
  Copyright 2010   by DJ Delorie     Updated Jul 2010