Bug 49321 - Incorrect Optimization O2 and O3 on Shift Operation for 64 bit Integers
Summary: Incorrect Optimization O2 and O3 on Shift Operation for 64 bit Integers
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-06-07 21:00 UTC by bbli08
Modified: 2011-06-08 13:26 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Added attachment for the last commitment (301 bytes, application/octet-stream)
2011-06-07 21:01 UTC, bbli08
Details

Note You need to log in before you can comment on or make changes to this bug.
Description bbli08 2011-06-07 21:00:29 UTC
// Shift operation yielded different results on different
// level of optimization for the same source. O1 is working but
// not O2 and O3

// Tested environments: Fedora 10. GCC 4.3.2.20081105 
// and Windows XP, MingGW GCC 4.5.2

// Compiler output and warning: nothing
// source file: one attached (no header file)

#include "stdio.h"
/*
 * Incorrect -O2 and -O3 optimization for 64 bit integer shift operator.
 * -O1 is working correclty
g++ -O1 main.cpp && ./a.out
i[0]=0, i[1]=8

g++ -O2 main.cpp && ./a.out 
i[0]=1, i[1]=0

g++ -O3 main.cpp && ./a.out 
i[0]=1, i[1]=0
 */
int main(int argc, char* argv[]) {
       int i[2];
        i[0] = 1;
        i[1] = 0;
        *((unsigned long long *) (&i)) <<= 35;
	/* correct result: linux test i[0]=0, i[1]=8 */
        printf ("i[0]=%d, i[1]=%d\n", i[0],i[1]);
	return 0;
}
Comment 1 bbli08 2011-06-07 21:01:52 UTC
Created attachment 24463 [details]
Added attachment for the last commitment
Comment 2 Jakub Jelinek 2011-06-07 21:25:08 UTC
This is aliasing violation, the code has undefined behavior.
Comment 3 Yu Simin 2011-06-08 13:26:40 UTC
I've tried GCC 4.6 (r174793, i786-pc-mingw32) and it gives the 'corrent' result (-01 -02 or -03):
i[0]=0, i[1]=8