Bug 154987

Summary: -O2 optimizer produces bad code from casts, autoincrement and globals
Product: [Fedora] Fedora Reporter: Michael Selway <mas-rhn>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact:
Severity: medium Docs Contact:
Priority: medium    
Version: 3   
Target Milestone: ---   
Target Release: ---   
Hardware: i386   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2005-04-15 22:51:33 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Attachments:
Description Flags
the C program which doesn't compile correctly under -O2
none
assembler output from the compiler with "cc -S -g -dA -O2 tst.c" none

Description Michael Selway 2005-04-15 12:33:46 UTC
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):
gcc-3.4.3-22.fc3

How reproducible:
Always

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 12:35:43 UTC
Created attachment 113221 [details]
the C program which doesn't compile correctly under -O2

Comment 2 Michael Selway 2005-04-15 12:38:46 UTC
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 22:51:33 UTC
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.