This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug tree-optimization/53265] Warn when undefined behavior implies smaller iteration count


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53265

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> 2013-03-11 14:14:06 UTC ---
The following avoids the "miscompile" in these obvious cases:

Index: gcc/tree-ssa-loop-niter.c
===================================================================
--- gcc/tree-ssa-loop-niter.c   (revision 196594)
+++ gcc/tree-ssa-loop-niter.c   (working copy)
@@ -3345,6 +3345,18 @@ estimate_numbers_of_iterations_loop (str
       bound = gcov_type_to_double_int (nit);
       record_niter_bound (loop, bound, true, false);
     }
+
+  /* If we know the exact number of iterations of this loop avoid all the
+     work below and most importantly do not break code with undefined
+     behavior by recording smaller maximum number of iterations.  */
+  niter = number_of_latch_executions (loop);
+  if (TREE_CODE (niter) == INTEGER_CST)
+    {
+      if (loop->any_upper_bound
+         && loop->nb_iterations_upper_bound.ucmp
+              (tree_to_double_int (niter)) < 0)
+       loop->nb_iterations_upper_bound = tree_to_double_int (niter);
+    }
 }

 /* Sets NIT to the estimated number of executions of the latch of the


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]