When we PHI translate dependent expressions we keep SSA defs in
place of the translated expression in case the expression itself
did not change even though it's context did and thus the validity
of ranges associated with it.  That eventually leads to simplification
errors given we violate the precondition that used SSA defs fed to
vn_valueize are valid to use (including their associated ranges).
The following makes sure to replace those with new representatives
always, not only when the dependent expression translation changed it.

The fix was originally discovered by Michael Morin.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed to trunk.

        PR tree-optimization/115494
        * tre-ssa-pre.cc (phi_translate_1): Always generate a
        representative for translated dependent expressions.

        * gcc.dg/torture/pr115494.c: New testcase.

Co-Authored-By: Mikael Morin <mik...@gcc.gnu.org>
---
 gcc/testsuite/gcc.dg/torture/pr115494.c | 24 ++++++++++++++++++++++++
 gcc/tree-ssa-pre.cc                     |  4 ++--
 2 files changed, 26 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr115494.c

diff --git a/gcc/testsuite/gcc.dg/torture/pr115494.c 
b/gcc/testsuite/gcc.dg/torture/pr115494.c
new file mode 100644
index 00000000000..a8c614433a6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr115494.c
@@ -0,0 +1,24 @@
+/* { dg-do run } */
+
+unsigned char a;
+int b = 1, c, d;
+int __attribute__((noipa))
+f()
+{
+  char e;
+  c = b - c;
+  a = ~(c || a);
+  e = -(b ^ a);
+  d = e && b;
+  a = ~(b & a);
+  if (a < 2)
+    return 1;
+  return 0;
+}
+
+int main()
+{
+  if (f())
+    __builtin_abort();
+}
+
diff --git a/gcc/tree-ssa-pre.cc b/gcc/tree-ssa-pre.cc
index 32bcd9b1b7a..735893bb191 100644
--- a/gcc/tree-ssa-pre.cc
+++ b/gcc/tree-ssa-pre.cc
@@ -1430,7 +1430,7 @@ phi_translate_1 (bitmap_set_t dest,
                unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
                leader = find_leader_in_sets (op_val_id, set1, set2);
                result = phi_translate (dest, leader, set1, set2, e);
-               if (result && result != leader)
+               if (result)
                  /* If op has a leader in the sets we translate make
                     sure to use the value of the translated expression.
                     We might need a new representative for that.  */
@@ -1553,7 +1553,7 @@ phi_translate_1 (bitmap_set_t dest,
                op_val_id = VN_INFO (op[n])->value_id;
                leader = find_leader_in_sets (op_val_id, set1, set2);
                opresult = phi_translate (dest, leader, set1, set2, e);
-               if (opresult && opresult != leader)
+               if (opresult)
                  {
                    tree name = get_representative_for (opresult);
                    changed |= name != op[n];
-- 
2.43.0

Reply via email to