Bug 49504 - Invalid optimization for Pmode != ptr_mode
Summary: Invalid optimization for Pmode != ptr_mode
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: 4.7.0
Assignee: Not yet assigned to anyone
URL: http://gcc.gnu.org/ml/gcc-patches/201...
Keywords:
: 49088 (view as bug list)
Depends on:
Blocks:
 
Reported: 2011-06-22 17:00 UTC by H.J. Lu
Modified: 2011-08-06 14:05 UTC (History)
1 user (show)

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


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2011-06-22 17:00:45 UTC
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;".
Comment 1 H.J. Lu 2011-06-22 18:11:33 UTC
Another combine bug.
Comment 2 H.J. Lu 2011-06-22 19:44:09 UTC
The patch is posted at

http://gcc.gnu.org/ml/gcc-patches/2011-06/msg01704.html
Comment 3 hjl@gcc.gnu.org 2011-06-22 19:59:59 UTC
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
Comment 4 H.J. Lu 2011-06-23 14:52:40 UTC
*** Bug 49088 has been marked as a duplicate of this bug. ***
Comment 5 hjl@gcc.gnu.org 2011-06-24 13:41:44 UTC
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
Comment 6 H.J. Lu 2011-06-24 15:40:08 UTC
Fixed.
Comment 7 hjl@gcc.gnu.org 2011-08-06 14:05:43 UTC
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