From patchwork Thu May 5 09:44:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 53500 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 196EA3858434 for ; Thu, 5 May 2022 09:45:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 196EA3858434 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1651743931; bh=wr6rvXfrz/xXe08WQ0JXm/YJ7IcKzP4oVeKwXiFB86Q=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=gwzVADn4NedGSeDNHZ+66MKEIJsjR+aca32sdwnTWRnom7Geh8hZU2gm+8X7MWkPy t2VVaOGLb1RdIZN+dM80wZX1XLrPNH6jRLqQZ00O3bEncBZVHEYhvKTSBiqAKPegds 1YUqJTOZBTFV0W6CJtMuUQ4SjYT6+kbddr2b6LV4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id DEECB3858434 for ; Thu, 5 May 2022 09:45:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DEECB3858434 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id A9182218EB; Thu, 5 May 2022 09:44:59 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 912A313A65; Thu, 5 May 2022 09:44:59 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id M9pLIpucc2KPMgAAMHmgww (envelope-from ); Thu, 05 May 2022 09:44:59 +0000 Date: Thu, 5 May 2022 11:44:59 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] rewrite undefined overflow to defined in ifcombine MIME-Version: 1.0 Message-Id: <20220505094459.912A313A65@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Cc: Jakub Jelinek Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" When we make stmts to execute unconditionally in ifcombine we have to make sure to rewrite stmts that can invoke undefined behavior on overflow into a form with defined overflow. That's possible for all but signed division for which we have to avoid the transform. Bootstrapped and tested on x86_64-unknown-linux-gnu. This was developed as not working solution to fix PR105142 and I do not have a testcase that experiences wrong-code (but I only tried for about 10 minutes to construct one). Still the problem is obviously latent and we should fix it. Thus - OK for trunk? Thanks, Richard. 2022-04-04 Richard Biener * tree-ssa-ifcombine.cc (bb_no_side_effects_p): Avoid executing divisions with undefined overflow unconditionally. (pass_tree_ifcombine::execute): Rewrite stmts with undefined overflow to defined. --- gcc/tree-ssa-ifcombine.cc | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/gcc/tree-ssa-ifcombine.cc b/gcc/tree-ssa-ifcombine.cc index 3a4ab694b71..cb86cc1ea5f 100644 --- a/gcc/tree-ssa-ifcombine.cc +++ b/gcc/tree-ssa-ifcombine.cc @@ -125,10 +125,26 @@ bb_no_side_effects_p (basic_block bb) if (is_gimple_debug (stmt)) continue; + gassign *ass; + enum tree_code rhs_code; if (gimple_has_side_effects (stmt) || gimple_uses_undefined_value_p (stmt) || gimple_could_trap_p (stmt) || gimple_vuse (stmt) + /* We need to rewrite stmts with undefined overflow to use + unsigned arithmetic but cannot do so for signed division. */ + || ((ass = dyn_cast (stmt)) + && INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (ass))) + && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (gimple_assign_lhs (ass))) + && ((rhs_code = gimple_assign_rhs_code (ass)), true) + && (rhs_code == TRUNC_DIV_EXPR + || rhs_code == CEIL_DIV_EXPR + || rhs_code == FLOOR_DIV_EXPR + || rhs_code == ROUND_DIV_EXPR) + /* We cannot use expr_not_equal_to since we'd have to restrict + flow-sensitive info to whats known at the outer if. */ + && (TREE_CODE (gimple_assign_rhs2 (ass)) != INTEGER_CST + || !integer_minus_onep (gimple_assign_rhs2 (ass)))) /* const calls don't match any of the above, yet they could still have some side-effects - they could contain gimple_could_trap_p statements, like floating point @@ -847,6 +863,19 @@ pass_tree_ifcombine::execute (function *fun) /* Clear range info from all stmts in BB which is now executed conditional on a always true/false condition. */ reset_flow_sensitive_info_in_bb (bb); + for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); + gsi_next (&gsi)) + { + gassign *ass = dyn_cast (gsi_stmt (gsi)); + if (!ass) + continue; + tree lhs = gimple_assign_lhs (ass); + if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs)) + || POINTER_TYPE_P (TREE_TYPE (lhs))) + && arith_code_with_undefined_signed_overflow + (gimple_assign_rhs_code (ass))) + rewrite_to_defined_overflow (ass, true); + } cfg_changed |= true; } }