From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15924 invoked by alias); 11 Mar 2013 14:14:44 -0000 Received: (qmail 15793 invoked by uid 48); 11 Mar 2013 14:14:09 -0000 From: "rguenth at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/53265] Warn when undefined behavior implies smaller iteration count Date: Mon, 11 Mar 2013 14:14:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: enhancement X-Bugzilla-Who: rguenth at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2013-03/txt/msg00857.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53265 --- Comment #5 from Richard Biener 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