public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Fix postreload_combine miscompilation (PR 69941)
       [not found] <56DA1DFB.6040606@t-online.de>
@ 2016-03-04 23:57 ` Bernd Schmidt
  2016-03-05  5:27   ` Jeff Law
  0 siblings, 1 reply; 4+ messages in thread
From: Bernd Schmidt @ 2016-03-04 23:57 UTC (permalink / raw)
  To: GCC Patches

[-- Attachment #1: Type: text/plain, Size: 416 bytes --]

This is a transformation which looks for

(set reg1 const)
(set reg2 (plus reg2 reg1))

and tries to replace all further uses of reg2 with (plus reg2 reg1). The 
problem here is that one of the uses is in a narrower mode, inside a 
zero_extend, so we produce wrong results.

The fix seems rather straightforward, verifying all uses have a mode 
matching the set. Bootstrapped and tested on x86_64-linux, ok?


Bernd

[-- Attachment #2: rcom-modes.diff --]
[-- Type: text/x-patch, Size: 2373 bytes --]

	PR rtl-optimization/69941
	* postreload.c (reload_combine_recognize_pattern): Ensure all uses of
	the reg share its mode.

testsuite/
	PR rtl-optimization/69941
	* gcc.dg/torture/pr69941.c: New test.

Index: gcc/postreload.c
===================================================================
--- gcc/postreload.c	(revision 233451)
+++ gcc/postreload.c	(working copy)
@@ -1057,7 +1057,6 @@ static bool
 reload_combine_recognize_pattern (rtx_insn *insn)
 {
   rtx set, reg, src;
-  unsigned int regno;
 
   set = single_set (insn);
   if (set == NULL_RTX)
@@ -1068,7 +1067,20 @@ reload_combine_recognize_pattern (rtx_in
   if (!REG_P (reg) || REG_NREGS (reg) != 1)
     return false;
 
-  regno = REGNO (reg);
+  unsigned int regno = REGNO (reg);
+  machine_mode mode = GET_MODE (reg);
+
+  if (reg_state[regno].use_index < 0
+      || reg_state[regno].use_index >= RELOAD_COMBINE_MAX_USES)
+    return false;
+
+  for (int i = reg_state[regno].use_index;
+       i < RELOAD_COMBINE_MAX_USES; i++)
+    {
+      struct reg_use *use = reg_state[regno].reg_use + i;
+      if (GET_MODE (*use->usep) != mode)
+	return false;
+    }
 
   /* Look for (set (REGX) (CONST_INT))
      (set (REGX) (PLUS (REGX) (REGY)))
@@ -1090,8 +1102,6 @@ reload_combine_recognize_pattern (rtx_in
       && REG_P (XEXP (src, 1))
       && rtx_equal_p (XEXP (src, 0), reg)
       && !rtx_equal_p (XEXP (src, 1), reg)
-      && reg_state[regno].use_index >= 0
-      && reg_state[regno].use_index < RELOAD_COMBINE_MAX_USES
       && last_label_ruid < reg_state[regno].use_ruid)
     {
       rtx base = XEXP (src, 1);
Index: gcc/testsuite/gcc.dg/torture/pr69941.c
===================================================================
--- gcc/testsuite/gcc.dg/torture/pr69941.c	(revision 0)
+++ gcc/testsuite/gcc.dg/torture/pr69941.c	(working copy)
@@ -0,0 +1,30 @@
+/* { dg-do run } */
+ 
+int a = 0;
+int b = 0;
+int c = 0;
+int e = 0;
+int f = 0;
+int *g = &e;
+ 
+int fn1() { return b ? a : b; }
+ 
+int main() {
+  int h = fn1() <= 0x8000000000000000ULL; // h = 1;
+ 
+  int k = f; // k = 0;
+ 
+  long i = h ? k : k / h; // i = 0;
+ 
+  long l = (unsigned short)(i - 0x1800); // l = 0xe800
+ 
+  i = l ? l : c; // i = 0xe800;
+ 
+  *g = i; // *g = 0xe800; e = 0xe800;
+ 
+  unsigned char result = e >> 9; // result = 0x74;
+
+  if ((int)result != 0x74)
+    __builtin_abort ();
+  return 0;
+}


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Fix postreload_combine miscompilation (PR 69941)
  2016-03-04 23:57 ` Fix postreload_combine miscompilation (PR 69941) Bernd Schmidt
@ 2016-03-05  5:27   ` Jeff Law
  2016-03-07 12:37     ` Bernd Schmidt
  0 siblings, 1 reply; 4+ messages in thread
From: Jeff Law @ 2016-03-05  5:27 UTC (permalink / raw)
  To: Bernd Schmidt, GCC Patches

On 03/04/2016 04:57 PM, Bernd Schmidt wrote:
> This is a transformation which looks for
>
> (set reg1 const)
> (set reg2 (plus reg2 reg1))
>
> and tries to replace all further uses of reg2 with (plus reg2 reg1). The
> problem here is that one of the uses is in a narrower mode, inside a
> zero_extend, so we produce wrong results.
>
> The fix seems rather straightforward, verifying all uses have a mode
> matching the set. Bootstrapped and tested on x86_64-linux, ok?
>
>
> Bernd
>
> rcom-modes.diff
>
>
> 	PR rtl-optimization/69941
> 	* postreload.c (reload_combine_recognize_pattern): Ensure all uses of
> 	the reg share its mode.
>
> testsuite/
> 	PR rtl-optimization/69941
> 	* gcc.dg/torture/pr69941.c: New test.
OK.

FWIW, based on my reading of the BZ, I think this counts as a regression 
-- it just happens to come and go.  I strongly suspect that's due to 
changing register assignments or some such.

I'm going to go ahead and push this to the trunk.

Jeff

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Fix postreload_combine miscompilation (PR 69941)
  2016-03-05  5:27   ` Jeff Law
@ 2016-03-07 12:37     ` Bernd Schmidt
  2016-03-07 16:36       ` Jeff Law
  0 siblings, 1 reply; 4+ messages in thread
From: Bernd Schmidt @ 2016-03-07 12:37 UTC (permalink / raw)
  To: Jeff Law, GCC Patches

On 03/05/2016 06:27 AM, Jeff Law wrote:
>>     PR rtl-optimization/69941
>>     * postreload.c (reload_combine_recognize_pattern): Ensure all uses of
>>     the reg share its mode.
>>
>> testsuite/
>>     PR rtl-optimization/69941
>>     * gcc.dg/torture/pr69941.c: New test.
> OK.

For branches as well?


Bernd

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Fix postreload_combine miscompilation (PR 69941)
  2016-03-07 12:37     ` Bernd Schmidt
@ 2016-03-07 16:36       ` Jeff Law
  0 siblings, 0 replies; 4+ messages in thread
From: Jeff Law @ 2016-03-07 16:36 UTC (permalink / raw)
  To: Bernd Schmidt, GCC Patches

On 03/07/2016 05:37 AM, Bernd Schmidt wrote:
> On 03/05/2016 06:27 AM, Jeff Law wrote:
>>>     PR rtl-optimization/69941
>>>     * postreload.c (reload_combine_recognize_pattern): Ensure all
>>> uses of
>>>     the reg share its mode.
>>>
>>> testsuite/
>>>     PR rtl-optimization/69941
>>>     * gcc.dg/torture/pr69941.c: New test.
>> OK.
>
> For branches as well?
Yes.

jeff

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-03-07 16:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <56DA1DFB.6040606@t-online.de>
2016-03-04 23:57 ` Fix postreload_combine miscompilation (PR 69941) Bernd Schmidt
2016-03-05  5:27   ` Jeff Law
2016-03-07 12:37     ` Bernd Schmidt
2016-03-07 16:36       ` Jeff Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).