From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 467A4384B0C0; Tue, 15 Dec 2020 13:35:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 467A4384B0C0 From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/94779] Bad optimization of simple switch Date: Tue, 15 Dec 2020 13:35:53 +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-Version: 10.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: jakub at gcc dot gnu.org X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: marxin at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 15 Dec 2020 13:35:53 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D94779 Jakub Jelinek changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |amacleod at redhat dot com --- Comment #12 from Jakub Jelinek --- So, for start I think we should do: 1) query->range_of_expr (m_index_range, m_index_expr, swtch) where query is address of gimple_ranger passed down from the caller to figure out range of the index expression; case labels without CASE_HI= GH for which m_index_range.contains_p (CASE_LOW (case)) is false can be ignored for the optimization (well, handled the way it is better for the optimization) For CASE_HIGH labels, similarly query if the range overlaps the index ra= nge. Note, I don't remember exactly in which way the types of the index expression and of the case labels can differ, I believe all case labels have to have the same type, so probably for the computation of the range if it has differ= ent type from the case label ones, it needs to call something that would emu= late the effect of conversion to that type (or vice versa?) CCing Andrew for the range stuff 2) similarly, cases that refer to blocks which have EDGE_COUNT (bb->succs) = =3D=3D 0 && gimple_seq_unreachable_p (bb_seq (bb)) should be treated again like c= ases one shouldn't need to care about 3) to handle the #c0 testcase in C (C++ already adds __builtin_unreachable), handle also the case where case refers to block which has EDGE_COUNT (bb->succs) and ends in GIMPLE_RETURN without expression in a function that returns integral or pointer value (for simplicity) and has no statements other than debug stmts or clobber stmts before it. Note, this case can'= t be treated completely like a UB case, there is no UB in C unless the caller checks the return value, but it could be handled conditionally, as long = as the code we emit for that doesn't invoke UB in the function, we really d= on't=20 care about the value we return to the caller. So e.g. if we are conside= ring a linear function and other case labels return that linear value and some return unspecified value, just use the linear function to cover those to= o. 4) to be able to optimize the int f1(unsigned x) { if (x >=3D 3) __builtin_unreachable(); switch (x) { case 0: return 1; case 1: return 2; case 2: return 3; } } testcase, we should for consideration virtually undo the evrp optimizati= on which removed the case 0: and replaced it with default: - here the handl= ed cases (that should include the really handled ones and ones that and the UB ones (if the case is outside of range or __builtin_unreachable) cover everything but one case, assume the default label is that for that single case rather than handling anything else; similarly, if the whole range is covered, ignore the default label=