]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
finite_loop_p tweak
authorJan Hubicka <jh@suse.cz>
Fri, 21 Jul 2023 11:57:34 +0000 (13:57 +0200)
committerJan Hubicka <jh@suse.cz>
Fri, 21 Jul 2023 11:57:34 +0000 (13:57 +0200)
We have finite_p flag in loop structure.  finite_loop_p already know to
use it, but we also may set the flag when we prove loop to be finite by
SCEV analysis to avoid duplicated work.

Bootstrapped/regtested x86_64-linux, OK?

gcc/ChangeLog:

* tree-ssa-loop-niter.cc (finite_loop_p): Reorder to do cheap
tests first; update finite_p flag.

gcc/tree-ssa-loop-niter.cc

index 3c4e66291fb733ff419dbaa94e80fff91ab143cb..a8068014c5fc8edda253d7bdb01eb021b1af7708 100644 (file)
@@ -3338,24 +3338,6 @@ finite_loop_p (class loop *loop)
   widest_int nit;
   int flags;
 
-  flags = flags_from_decl_or_type (current_function_decl);
-  if ((flags & (ECF_CONST|ECF_PURE)) && !(flags & ECF_LOOPING_CONST_OR_PURE))
-    {
-      if (dump_file && (dump_flags & TDF_DETAILS))
-       fprintf (dump_file, "Found loop %i to be finite: it is within pure or const function.\n",
-                loop->num);
-      return true;
-    }
-
-  if (loop->any_upper_bound
-      || max_loop_iterations (loop, &nit))
-    {
-      if (dump_file && (dump_flags & TDF_DETAILS))
-       fprintf (dump_file, "Found loop %i to be finite: upper bound found.\n",
-                loop->num);
-      return true;
-    }
-
   if (loop->finite_p)
     {
       unsigned i;
@@ -3368,11 +3350,36 @@ finite_loop_p (class loop *loop)
          {
            if (dump_file)
              fprintf (dump_file, "Assume loop %i to be finite: it has an exit "
-                      "and -ffinite-loops is on.\n", loop->num);
+                      "and -ffinite-loops is on or loop was "
+                      "previously finite.\n",
+                      loop->num);
            return true;
          }
     }
 
+  flags = flags_from_decl_or_type (current_function_decl);
+  if ((flags & (ECF_CONST|ECF_PURE)) && !(flags & ECF_LOOPING_CONST_OR_PURE))
+    {
+      if (dump_file && (dump_flags & TDF_DETAILS))
+       fprintf (dump_file,
+                "Found loop %i to be finite: it is within "
+                "pure or const function.\n",
+                loop->num);
+      loop->finite_p = true;
+      return true;
+    }
+
+  if (loop->any_upper_bound
+      /* Loop with no normal exit will not pass max_loop_iterations.  */
+      || (!loop->finite_p && max_loop_iterations (loop, &nit)))
+    {
+      if (dump_file && (dump_flags & TDF_DETAILS))
+       fprintf (dump_file, "Found loop %i to be finite: upper bound found.\n",
+                loop->num);
+      loop->finite_p = true;
+      return true;
+    }
+
   return false;
 }