This is the mail archive of the gcc-bugs@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]
Other format: [Raw text]

[Bug c/21332] New: -O2 makes a loop doesn't execute a body


4.1.0 20050502 (experimental) seems have a problem with optimization.
The for loop in the else clause in the main doesn't execute the loop body. 

% cat t.c
extern int printf (__const char *__restrict __format, ...);

int f()
{
  return -2;
}

int main(int argc, char **argv)
{
  int c = f();
  long i;
  if (c > 0) {
    for (i=0;i<c;i++) {
      f();
    }
  }
  else {
    c = -c;
    printf("beg %d\n", c);
    for (i=0;i<c;i++) {
      printf("%d\n", i);
    }
    printf("end %d\n", i);
  }
  return 0;
}

% gcc -v -O2 t.c
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../gcc/configure --prefix=/home/src/gcc/trunk --enable-languages=c
Thread model: posix
gcc version 4.1.0 20050502 (experimental)
 /home/src/gcc/trunk/libexec/gcc/i686-pc-linux-gnu/4.1.0/cc1 -quiet -v t.c
-quiet -dumpbase t.c -mtune=pentiumpro -auxbase t -O2 -version -o /tmp/cc6LaTqE.s
ignoring nonexistent directory
"/home/src/gcc/trunk/lib/gcc/i686-pc-linux-gnu/4.1.0/../../../../i686-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /home/src/gcc/trunk/include
 /home/src/gcc/trunk/lib/gcc/i686-pc-linux-gnu/4.1.0/include
 /usr/include
End of search list.
GNU C version
 4.1.0 20050502 (experimental) (i686-pc-linux-gnu)
        compiled by GNU C version 4.1.0 20050502 (experimental).
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
 as -V -Qy -o /tmp/cc6jWNoa.o /tmp/cc6LaTqE.s
GNU assembler version 2.15 (i386-linux) using BFD version 2.15
 /home/src/gcc/trunk/libexec/gcc/i686-pc-linux-gnu/4.1.0/collect2 --eh-frame-hdr
-m elf_i386 -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o
/home/src/gcc/trunk/lib/gcc/i686-pc-linux-gnu/4.1.0/crtbegin.o
-L/home/src/gcc/trunk/lib/gcc/i686-pc-linux-gnu/4.1.0
-L/home/src/gcc/trunk/lib/gcc/i686-pc-linux-gnu/4.1.0/../../.. /tmp/cc6jWNoa.o
-lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s
--no-as-needed /home/src/gcc/trunk/lib/gcc/i686-pc-linux-gnu/4.1.0/crtend.o
/usr/lib/crtn.o
% ./a.out       
beg 2
end 0

It works well without optimization as follows.

% gcc t.c       
% ./a.out 
beg 2
0
1
end 2

-- 
           Summary: -O2 makes a loop doesn't execute a body
           Product: gcc
           Version: 4.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: akr at m17n dot org
                CC: gcc-bugs at gcc dot gnu dot org


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


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