tree-optimization/97953 - fix bougs range recorded by EVRP
authorRichard Biener <rguenther@suse.de>
Thu, 26 Nov 2020 15:13:08 +0000 (16:13 +0100)
committerRichard Biener <rguenther@suse.de>
Thu, 26 Nov 2020 15:18:07 +0000 (16:18 +0100)
EVRP records some ranges from asserts into SSA_NAME_RANGE_INFO
but fails to assert that the condition the range is derived from
is always true after the SSA names definition.  The patch implements
the simplest post-dominance check, basic-block equality.

2020-11-26  Richard Biener  <rguenther@suse.de>

PR tree-optimization/97953
* gimple-ssa-evrp-analyze.c
(evrp_range_analyzer::record_ranges_from_incoming_edge): Make
sure the condition post-dominates the SSA definition before
recording into SSA_NAME_RANGE_INFO.

* gcc.dg/pr97953.c: New testcase.

gcc/gimple-ssa-evrp-analyze.c
gcc/testsuite/gcc.dg/pr97953.c [new file with mode: 0644]

index 485774d3f22ebc2988086f4b969120e4a1e20c78..b998e0b6594863ee8d8dc15aa9b6b90350407354 100644 (file)
@@ -218,7 +218,11 @@ evrp_range_analyzer::record_ranges_from_incoming_edge (basic_block bb)
              push_value_range (vrs[i].first, vrs[i].second);
              if (is_fallthru
                  && m_update_global_ranges
-                 && all_uses_feed_or_dominated_by_stmt (vrs[i].first, stmt))
+                 && all_uses_feed_or_dominated_by_stmt (vrs[i].first, stmt)
+                 /* The condition must post-dominate the definition point.  */
+                 && (SSA_NAME_IS_DEFAULT_DEF (vrs[i].first)
+                     || (gimple_bb (SSA_NAME_DEF_STMT (vrs[i].first))
+                         == pred_e->src)))
                {
                  set_ssa_range_info (vrs[i].first, vrs[i].second);
                  maybe_set_nonzero_bits (pred_e, vrs[i].first);
diff --git a/gcc/testsuite/gcc.dg/pr97953.c b/gcc/testsuite/gcc.dg/pr97953.c
new file mode 100644 (file)
index 0000000..6219619
--- /dev/null
@@ -0,0 +1,24 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -fno-tree-fre" } */
+
+int __attribute__((noipa))
+foo (int flag, int *p)
+{
+  int val = *p;
+  if (flag)
+    {
+      if (val != 1)
+        __builtin_unreachable ();
+      return 0;
+    }
+  int val2 = *p;
+  return val2 == 2;
+}
+
+int main()
+{
+  int i = 2;
+  if (foo (0, &i) != 1)
+    __builtin_abort ();
+  return 0;
+}