Ranger cache dominator queries should ignore backedges.
authorAndrew MacLeod <amacleod@redhat.com>
Thu, 23 Mar 2023 14:28:34 +0000 (10:28 -0400)
committerAndrew MacLeod <amacleod@redhat.com>
Fri, 24 Mar 2023 13:11:09 +0000 (09:11 -0400)
When querying dominators for cache values, ignore back edges in
read-only mode.

PR tree-optimization/109238
gcc/
* gimple-range-cache.cc (ranger_cache::resolve_dom): Ignore
predecessors which this block dominates.

gcc/testsuite/
* gcc.dg/pr109238.c: New.

gcc/gimple-range-cache.cc
gcc/testsuite/gcc.dg/pr109238.c [new file with mode: 0644]

index cc56fd4..6a098d8 100644 (file)
@@ -1404,6 +1404,11 @@ ranger_cache::resolve_dom (vrange &r, tree name, basic_block bb)
   Value_Range er (TREE_TYPE (name));
   FOR_EACH_EDGE (e, ei, bb->preds)
     {
+      // If the predecessor is dominated by this block, then there is a back
+      // edge, and won't provide anything useful.  We'll actually end up with
+      // VARYING as we will not resolve this node.
+      if (dominated_by_p (CDI_DOMINATORS, e->src, bb))
+       continue;
       edge_range (er, e, name, RFD_READ_ONLY);
       r.union_ (er);
     }
diff --git a/gcc/testsuite/gcc.dg/pr109238.c b/gcc/testsuite/gcc.dg/pr109238.c
new file mode 100644 (file)
index 0000000..0cad823
--- /dev/null
@@ -0,0 +1,16 @@
+/* PR tree-optimization/109238 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -Wall" } */
+
+void foo (void *) __attribute__((noreturn));
+void bar (void *);
+
+void
+baz (void *p)
+{
+  void *c = __builtin_realloc (p, 16);
+  if (c)
+    foo (c);
+  for (;;)
+    bar (__builtin_realloc (p, 8));    /* { dg-bogus "pointer 'p' may be used after '__builtin_realloc'" } */
+}