Summary: | Bad program bahavior with -O2 optimization in plain C | ||
---|---|---|---|
Product: | gcc | Reporter: | Sabi <adeymo> |
Component: | c | Assignee: | Not yet assigned to anyone <unassigned> |
Status: | RESOLVED INVALID | ||
Severity: | major | CC: | ebotcazou, gcc-bugs |
Priority: | P3 | ||
Version: | 4.2.4 | ||
Target Milestone: | --- | ||
Host: | i486-linux-gnu | Target: | i486-linux-gnu |
Build: | i486-linux-gnu | Known to work: | |
Known to fail: | Last reconfirmed: | ||
Attachments: |
Testcase with debug information and explanation
Testcase smaller and simpler |
Description
Sabi
2009-03-01 05:01:31 UTC
Created attachment 17379 [details]
Testcase with debug information and explanation
Compile with:
gcc -save-temps -O2 -Wall -Werror xqsort.c -o xqsort
Created attachment 17380 [details]
Testcase smaller and simpler
Comment on attachment 17380 [details]
Testcase smaller and simpler
Compile with:
gcc -save-temps -O2 -Wall -Werror xqsort-small.c -o xqsort-small
Run with:
./xqsort-small && echo ok
If compiled with -O1 , the echo command must be executed.
> There is a bug in the gcc compiler for the C code that changes the behavior of
> a simple program with -O2 optimizations, but not with -O1 or -O0.
-O2 enables -fstrict-aliasing so the code must be written in ISO C as far as aliasing is concerned, otherwise the behavior is undefined.
Your code is not written in ISO C:
#define VL(X) (*((uint32*)(X)))
is a direct violation of ISO C. See the -fstrict-aliasing entry in the GCC manual or the ISO standard.
|