]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Avoid recursion with SCEV
authorAndrew MacLeod <amacleod@redhat.com>
Wed, 19 Nov 2025 16:31:16 +0000 (11:31 -0500)
committerAndrew MacLeod <amacleod@redhat.com>
Wed, 19 Nov 2025 19:00:17 +0000 (14:00 -0500)
Ranger should not invoke SCEV if its already in the middle of a SCEV call.

PR tree-optimization/122756
gcc/
* gimple-range-fold.cc (range_of_ssa_name_with_loop_info): Do
not invoke SCEV if already in a SCEV call.

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

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

index 63e114e4d044880be781bcd183e43ba3fee73f82..bd5e53516b79101659cb0c35eba5eb89210fa6ff 100644 (file)
@@ -1252,11 +1252,15 @@ fold_using_range::range_of_ssa_name_with_loop_info (vrange &r, tree name,
                                                    class loop *l, gphi *phi,
                                                    fur_source &src)
 {
+  static bool in_scev_call = false;
   gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
+  // Avoid SCEV callbacks causing infinite recursion.
+  if (in_scev_call)
+    r.set_varying (TREE_TYPE (name));
   // SCEV currently invokes get_range_query () for values.  If the query
   // being passed in is not the same SCEV will use, do not invoke SCEV.
   // This can be remove if/when SCEV uses a passed in range-query.
-  if (src.query () != get_range_query (cfun))
+  else if (src.query () != get_range_query (cfun))
     {
       r.set_varying (TREE_TYPE (name));
       // Report the msmatch if SRC is not the global query.  The cache
@@ -1266,8 +1270,13 @@ fold_using_range::range_of_ssa_name_with_loop_info (vrange &r, tree name,
        fprintf (dump_file,
          "fold_using-range:: SCEV not invoked due to mismatched queries\n");
     }
-  else if (!range_of_var_in_loop (r, name, l, phi, src.query ()))
-      r.set_varying (TREE_TYPE (name));
+  else
+    {
+      in_scev_call = true;
+      if (!range_of_var_in_loop (r, name, l, phi, src.query ()))
+       r.set_varying (TREE_TYPE (name));
+      in_scev_call = false;
+    }
 }
 
 // -----------------------------------------------------------------------
diff --git a/gcc/testsuite/gcc.dg/pr122756.c b/gcc/testsuite/gcc.dg/pr122756.c
new file mode 100644 (file)
index 0000000..6299469
--- /dev/null
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-O3" } */
+/* { dg-additional-options "-march=rv64gcv -mabi=lp64d" { target { rv64 } } } */
+
+long a;
+void b() {
+  unsigned long c, d;
+  for (;; c = d + 2000) {
+    d = c;
+    for (; d < a; d += 2)
+      if (d % 2)
+        for (;;)
+          ;
+  }
+}