On x32 branch, I got [hjl@gnu-33 tmp]$ cat x32.c unsigned long long t(const void* p, unsigned long long q) { unsigned long long a = (((unsigned long long) ((unsigned long) p)) + q) >> 32; return a; } [hjl@gnu-33 tmp]$ /usr/gcc-4.7.0-x32/bin/gcc -O2 -mx32 -S x32.c [hjl@gnu-33 tmp]$ cat x32.s .file "x32.c" .text .p2align 4,,15 .globl t .type t, @function t: .LFB0: .cfi_startproc xorl %eax, %eax ret .cfi_endproc .LFE0: .size t, .-t .ident "GCC: (GNU) 4.7.0 20110616 (experimental)" .section .note.GNU-stack,"",@progbits [hjl@gnu-33 tmp]$ From http://code.google.com/p/nativeclient/issues/detail?id=1601 What it does is that when pointers from memory (ptr_mode) are zero extended when getting into registers (Pmode)(POINTERS_EXTEND_UNSIGNED > 0), and there is a Pmode PLUS or MINUS of a register that holds a pointer value with some other register, all bits above ptr_mode are considered zero. Here is the test that demonstrates the bug clearly (compile with -O2): unsigned long long t(const void* p, unsigned long long q) { unsigned long long a = (((unsigned long long)p) + q) >> 32; return a; } In our z86_64 compiler, p is 32-bit. It gets zero-extended to 64-bit long long for addition with q, but the fact that it was originally a pointer is preserved. Thus, nonzero_bits1 thinks the result of addition always has high 32-bits equal to zero, so the result of the right shift is always zero. The test gets optimized to "return 0;".
Another combine bug.
The patch is posted at http://gcc.gnu.org/ml/gcc-patches/2011-06/msg01704.html
Author: hjl Date: Wed Jun 22 19:59:52 2011 New Revision: 175306 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=175306 Log: Properly handle pointer addition/subtraction. gcc/ 2011-06-22 H.J. Lu <hongjiu.lu@intel.com> PR rtl-optimization/49504 * rtlanal.c (nonzero_bits1): Properly handle addition or subtraction a pointer in Pmode if pointers extend unsigned. gcc/testsuite/ 2011-06-22 H.J. Lu <hongjiu.lu@intel.com> PR rtl-optimization/49504 * gcc.target/i386/pr49504.c: New. Added: branches/x32/gcc/testsuite/gcc.target/i386/pr49504.c Modified: branches/x32/gcc/ChangeLog.x32 branches/x32/gcc/rtlanal.c branches/x32/gcc/testsuite/ChangeLog.x32
*** Bug 49088 has been marked as a duplicate of this bug. ***
Author: hjl Date: Fri Jun 24 13:41:40 2011 New Revision: 175377 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=175377 Log: Properly handle pointer addition/subtraction. 2011-06-24 H.J. Lu <hongjiu.lu@intel.com> PR rtl-optimization/49504 * rtlanal.c (nonzero_bits1): Properly handle addition or subtraction a pointer in Pmode if pointers extend unsigned. Modified: trunk/gcc/ChangeLog trunk/gcc/rtlanal.c
Fixed.
Author: hjl Date: Sat Aug 6 14:05:39 2011 New Revision: 177509 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=177509 Log: Add testcases for PRs 48084/49504/49860. 2011-08-06 H.J. Lu <hongjiu.lu@intel.com> PR target/48084 * gcc.target/i386/pr48084-1.c: New. * gcc.target/i386/pr48084-2.c: Likewise. * gcc.target/i386/pr48084-3.c: Likewise. * gcc.target/i386/pr48084-4.c: Likewise. * gcc.target/i386/pr48084-5.c: Likewise. PR rtl-optimization/49504 * gcc.target/i386/pr49504.c: New. PR target/49860 * gcc.dg/pr49860.c: New. Added: trunk/gcc/testsuite/gcc.dg/pr49860.c trunk/gcc/testsuite/gcc.target/i386/pr48084-1.c trunk/gcc/testsuite/gcc.target/i386/pr48084-2.c trunk/gcc/testsuite/gcc.target/i386/pr48084-3.c trunk/gcc/testsuite/gcc.target/i386/pr48084-4.c trunk/gcc/testsuite/gcc.target/i386/pr48084-5.c trunk/gcc/testsuite/gcc.target/i386/pr49504.c Modified: trunk/gcc/testsuite/ChangeLog