public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug optimization/13816] New: access to local array in while loop is optimized away
@ 2004-01-22 17:37 a dot simon at kent dot ac dot uk
  2004-01-22 18:14 ` [Bug optimization/13816] " pinskia at gcc dot gnu dot org
  0 siblings, 1 reply; 2+ messages in thread
From: a dot simon at kent dot ac dot uk @ 2004-01-22 17:37 UTC (permalink / raw)
  To: gcc-bugs

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.

-- 
           Summary: access to local array in while loop is optimized away
           Product: gcc
           Version: 3.3.3
            Status: UNCONFIRMED
          Severity: critical
          Priority: P2
         Component: optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: a dot simon at kent dot ac dot uk
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i386-unknown-freebsd5.1
  GCC host triplet: i386-unknown-freebsd5.1
GCC target triplet: i386-unknown-freebsd5.1


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13816


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug optimization/13816] access to local array in while loop is optimized away
  2004-01-22 17:37 [Bug optimization/13816] New: access to local array in while loop is optimized away a dot simon at kent dot ac dot uk
@ 2004-01-22 18:14 ` pinskia at gcc dot gnu dot org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-01-22 18:14 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-01-22 18:14 -------
This is invalid as this line:
    localArray[current]=localArray[++current];
you modified current twice which violates squence points.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID
            Summary|access to local array in    |access to local array in
                   |while loop is optimized away|while loop is optimized away


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13816


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-01-22 18:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-22 17:37 [Bug optimization/13816] New: access to local array in while loop is optimized away a dot simon at kent dot ac dot uk
2004-01-22 18:14 ` [Bug optimization/13816] " pinskia at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).