g++ generates bad code for the program below if -O3 is used.
If optimization is off, it works correctly.



$ g++ -o x x.cc
$ ./x
$ g++ -o x x.cc -O3
$ ./x
fail
$

Here is the generated code for main() (with exception-handling labels elided):

main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        subl    $16, %esp
        movl    $.LC0, (%esp)
        call    puts
        xorl    %eax, %eax
        leave
        ret

The test has been erroneously optimized out.

Here's main() from x.cc.t66.blocks:

int main() ()
{
<bb 0>:
  if ((short int) (short unsigned int) (int) (short unsigned int) rawdata < 0) 
goto <L1>; else goto <L2>;

<L1>:;
  printf (&"fail\n"[0]);

<L2>:;
  return 0;

}


This looks ok.
And here it is from x.cc.t67.final_cleanup:

;; Function int main() (main)

Merging blocks 0 and 1
Merging blocks 0 and 2
int main() ()
{
<bb 0>:
  printf (&"fail\n"[0]);
  return 0;

}

Where the test is gone.
So the problem is in this step.

This looks like it may be related to the previous bug
tree-optimization/18576.  However, that one is classified
as missed-optimization, while this is wrong code.

Environment:
System: Linux karma 2.6.8.1 #20 Mon Sep 13 23:48:47 EDT 2004 i686 i686 i386 
GNU/Linux
Architecture: i686

        <machine, os, target, libraries (multiple lines)>
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: /home/sss/gcc/gcc/configure --prefix=/usr/local/gcc 
--enable-threads=posix --enable-long-long --enable-languages=c,c++,f95

How-To-Repeat:

Compile the following with -O3:

-------------------------------------------------------------------
extern "C" int printf (const char*, ...);


unsigned short int foo(unsigned int* ptr) {
  return *ptr;
};

unsigned rawdata = 0;

int main ()
{
  if ((foo(&rawdata) & 0x8000) != 0)
    printf ("fail\n");
  return 0;
}

-------------------------------------------------------------------
------- Additional Comments From snyder at fnal dot gov  2005-01-05 23:49 
-------
Fix:
        <how to correct or work around the problem, if known (multiple lines)>

-- 
           Summary: Bad code generated in final_cleanup
           Product: gcc
           Version: 4.0.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: snyder at fnal dot gov
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


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

Reply via email to