diff mbox

[PR71408] - Fix wrong code at -Os and above

Message ID 575404F3.70806@linaro.org
State New
Headers show

Commit Message

Kugan Vivekanandarajah June 5, 2016, 10:54 a.m. UTC
Hi All,

For the testcase in PR71408 zero_one_operation seems still broken. In 
handling NEGATE_EXPR, as part of undistribute_ops_list, in 
zero_one_operation, we are doing propagate_op_to_single_use (op, stmt, def);

This results in:
-  _14 = _5 * _12;
    _15 = (int) _11;
    _16 = ~_15;
    _17 = (unsigned int) _16;
-  _18 = -_5;
-  _19 = _17 * _18;
-  _20 = _14 + _19;
-  _24 = _5 & _20;
+  _19 = _5 * _17;
+  _35 = _19 + _12;
+  _34 = _35 * _5;
+  _20 = _34;
+  _24 = _20 & _5;

We should instead propagate (-1) as "op" is the one which gets factored 
out. With the attached patch we now have:
-  _14 = _5 * _12;
    _15 = (int) _11;
    _16 = ~_15;
    _17 = (unsigned int) _16;
-  _18 = -_5;
-  _19 = _17 * _18;
-  _20 = _14 + _19;
-  _24 = _5 & _20;
+  _32 = _17;
+  _19 = -_32;
+  _34 = _19 + _12;
+  _33 = _34 * _5;
+  _20 = _33;
+  _24 = _20 & _5;

Regression tested and bootstrapped on x86-64-linux-gnu with no new 
regression. Is this OK for trunk?

Thanks,
Kugan

gcc/ChangeLog:

2016-06-05  Kugan Vivekanandarajah  <kuganv@linaro.org>

	PR middle-end/71408
	* tree-ssa-reassoc.c (zero_one_operation): Fix NEGATE_EXPR operand for
	propagate_op_to_single_use.


gcc/testsuite/ChangeLog:

2016-06-05  Kugan Vivekanandarajah  <kuganv@linaro.org>

	PR middle-end/71408
	* gcc.dg/tree-ssa/pr71408.c: New test.
diff mbox

Patch

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71408.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71408.c
index e69de29..896a428 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/pr71408.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr71408.c
@@ -0,0 +1,30 @@ 
+/* PR middle-end/71408 */
+/* { dg-do run } */
+/* { dg-options "-Os" } */
+unsigned a, b;
+
+struct S0
+{
+  int f1:18;
+  unsigned f3:4;
+};
+
+void fn1 ()
+{
+  struct S0 c = { 7, 0 };
+  if (c.f1)
+    c.f3 = 3;
+  a = -~c.f3;
+  c.f3 = ~(c.f1 && c.f1);
+  c.f1 = c.f3 * (c.f1 - (c.f1 - a % c.f1)) + ~c.f3 * -a;
+  b = ~(c.f1 & a);
+  if (b >= 4294967295)
+    __builtin_abort ();
+}
+
+int
+main ()
+{
+  fn1 ();
+  return 0;
+}
diff --git a/gcc/tree-ssa-reassoc.c b/gcc/tree-ssa-reassoc.c
index 1973077..7865df0 100644
--- a/gcc/tree-ssa-reassoc.c
+++ b/gcc/tree-ssa-reassoc.c
@@ -1203,7 +1203,8 @@  zero_one_operation (tree *def, enum tree_code opcode, tree op)
 	    {
 	      if (gimple_assign_rhs1 (stmt) == op)
 		{
-		  propagate_op_to_single_use (op, stmt, def);
+		  tree cst = build_minus_one_cst (TREE_TYPE (op));
+		  propagate_op_to_single_use (cst, stmt, def);
 		  return;
 		}
 	      else if (integer_minus_onep (op)
@@ -1251,7 +1252,8 @@  zero_one_operation (tree *def, enum tree_code opcode, tree op)
 	    {
 	      if (gimple_assign_rhs1 (stmt2) == op)
 		{
-		  propagate_op_to_single_use (op, stmt2, def);
+		  tree cst = build_minus_one_cst (TREE_TYPE (op));
+		  propagate_op_to_single_use (cst, stmt2, def);
 		  return;
 		}
 	      else if (integer_minus_onep (op)