Bug 13816 - access to local array in while loop is optimized away
Summary: access to local array in while loop is optimized away
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 3.3.3
: P2 critical
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-01-22 17:37 UTC by a.simon
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host: i386-unknown-freebsd5.1
Target: i386-unknown-freebsd5.1
Build: i386-unknown-freebsd5.1
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description a.simon 2004-01-22 17:37:40 UTC
The body of a while loop that operates on a stack-allocated array is optimized
away. Turning optimizations off gives correct bahviour. I observed this bug with
gcc 3.2.2 on FreeBSD, other people have confirmed this for gcc 3.3.3 20031229 on
Debian.

This program:
                                                                                  
                                                                                  
#include<stdio.h>
                                                                                  
void runBug(int lastElem) {
  int localArray[lastElem];
  int j;
  for(j=0; j<lastElem; j++) localArray[j]=j;
                                                                                  
  int temp=localArray[0];
  size_t current=0;
  while (current<lastElem)
    localArray[current]=localArray[++current];
  localArray[lastElem-1]=temp;
                                                                                  
  for(j=0; j<lastElem; j++) printf("%i, ",localArray[j]);
};
                                                                                  
int main() {
  runBug(4);
  return 0;
};
                                                                                  
compiled with:
                                                                                  
gcc -O0 bug.c
                                                                                  
gives
                                                                                  
./a.out
1, 2, 3, 0,
                                                                                  
(the while loop shuffles the list around, which is what I want). However
with optimizations the loop reduces to incrementing the loop counter and
nothing else:
                                                                                  
gcc -O2 bug.c
                                                                                  
gives:
                                                                                  
./a.out
0, 1, 2, 0,
                                                                                  
Thanks,
Axel.
Comment 1 Andrew Pinski 2004-01-22 18:14:35 UTC
This is invalid as this line:
    localArray[current]=localArray[++current];
you modified current twice which violates squence points.