From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2790 invoked by alias); 13 Mar 2013 11:01:11 -0000 Received: (qmail 2460 invoked by uid 48); 13 Mar 2013 11:00:18 -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: Wed, 13 Mar 2013 11:01: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/msg01024.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53265 --- Comment #24 from Richard Biener 2013-03-13 11:00:11 UTC --- (In reply to comment #23) > Created attachment 29661 [details] > gcc48-pr53265.patch > > Updated patch as per IRC discussions. Still need to look at longbranch2.C and > do_1.f90, then test it. Looks good. Few comments: + number_of_latch_executions (loop); add a comment what side-effect you are interested in. + + /* 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. */ + if (loop->nb_iterations + && TREE_CODE (loop->nb_iterations) == INTEGER_CST + && loop->any_upper_bound + && loop->nb_iterations_upper_bound.ucmp + (tree_to_double_int (loop->nb_iterations)) < 0) + loop->nb_iterations_upper_bound = tree_to_double_int (loop->nb_iterations); We don't avoid any work, so adjust the comment. I'd also simply do: /* If we know the exact number of iterations record that as the upper bound as well. This avoids breaking code with undefined behavior by eventually recording a smaller maximum. */ if (loop->nb_iterations && TREE_CODE (loop->nb_iterations) == INTEGER_CST) { loop->any_upper_bound = true; loop->nb_iterations_upper_bound = tree_to_double_int (loop->nb_iterations); } that's always correct.