This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug tree-optimization/53265] Warn when undefined behavior implies smaller iteration count
- From: "rguenth at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Mon, 11 Mar 2013 14:14:06 +0000
- Subject: [Bug tree-optimization/53265] Warn when undefined behavior implies smaller iteration count
- Auto-submitted: auto-generated
- References: <bug-53265-4@http.gcc.gnu.org/bugzilla/>
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