Bug 154987 - -O2 optimizer produces bad code from casts, autoincrement and globals
-O2 optimizer produces bad code from casts, autoincrement and globals
Product: Fedora
Classification: Fedora
Component: gcc (Show other bugs)
i386 Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
Depends On:
  Show dependency treegraph
Reported: 2005-04-15 08:33 EDT by Michael Selway
Modified: 2007-11-30 17:11 EST (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2005-04-15 18:51:33 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)
the C program which doesn't compile correctly under -O2 (332 bytes, text/plain)
2005-04-15 08:35 EDT, Michael Selway
no flags Details
assembler output from the compiler with "cc -S -g -dA -O2 tst.c" (13.60 KB, text/plain)
2005-04-15 08:38 EDT, Michael Selway
no flags Details

  None (edit)
Description Michael Selway 2005-04-15 08:33:46 EDT
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.6) Gecko/20050323 Firefox/1.0.2 Fedora/1.0.2-1.3.1

Description of problem:
Attached program doesn't increment the global "cp" variable when compiled with gcc -O2.  When compiled with -O or non-optimized, it works correctly.  In the assembler code (also attached) which the compiler produces, the compiler is updating the global "cp" variable twice, 1st time correctly, 2nd time wrongly.

(This is a very highly cut down version of a virtual machine run-time: the global is the program counter (bytecode pointer))

Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:
1. compile attached program: "cc -o tst tst.c"
2. run with "./tst"
3. result is "cp change is 6" (correct behaviour)

4. compile again with "cc -O2 -o tst tst.c"
5. run with "./tst"
6. result is "cp change is 2" (incorrect behaviour)  

Actual Results:  prints "cp change is 2"

Expected Results:  should print "cp change is 6"

Additional info:

also seen in gcc-3.4.2-6.fc3
Comment 1 Michael Selway 2005-04-15 08:35:43 EDT
Created attachment 113221 [details]
the C program which doesn't compile correctly under -O2
Comment 2 Michael Selway 2005-04-15 08:38:46 EDT
Created attachment 113222 [details]
assembler output from the compiler with "cc -S -g -dA -O2 tst.c"

search for "tst.c:10" (the code for source line 10).  Notice that there are 2
movl instructions updating the global "cp".  There should clearly only be one
under high optimization, and the 2nd update is wrong anyway.
Comment 3 Jakub Jelinek 2005-04-15 18:51:33 EDT
Please consider using -Wall before reporting something as a bug.
Compiler tells you where the problem is.  The testcase is buggy, you
violate ISO C99 aliasing rules, as you access the same object using
different types and the object is not a union of those different types.
If you turn off strict aliasing, with -O2 -fno-strict-aliasing, it will
output 6 as you expected.

Note You need to log in before you can comment on or make changes to this bug.