From patchwork Mon Nov 7 09:02:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 16332 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1926864wru; Mon, 7 Nov 2022 01:02:59 -0800 (PST) X-Google-Smtp-Source: AMsMyM4Hd2k8K4BgoHPGR6jAVSkZjS9fD2mLV0IPfCD0VLMaN3E9NRe6NhPWhucJGEGwChExRaXU X-Received: by 2002:a17:907:761b:b0:7a3:86dd:d330 with SMTP id jx27-20020a170907761b00b007a386ddd330mr46435810ejc.34.1667811779351; Mon, 07 Nov 2022 01:02:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667811779; cv=none; d=google.com; s=arc-20160816; b=nKd0QHDSIJUPvTXuTK5X3lb1l5TtEzmjWnvVJz6k1Qst7LVIc50UOGuFlZyrJTnk4Z fnepshDdIBSKPjbTdsT8aq2L8YQFzTGSQa7GBdmwU1Tx/WBaNHNdC2jewTf6el7NxKCA oKJKqrR4MgJ04MVS3VQ69PQk28o6ZzUxKKSnYQa94Jmd3RmI0ErhZ/hiNScDyELBSePi PyaJOg+YW3e+QaLyd/75BjH2kdEu7nV7mYmxnziz9354z9TFmkPxbKvpplXfD2ORenC5 L7+KyyjvOTb9JkN6W4bZachH+vAecNA3mOomzojTZmylaN07v2fwmj5+cjA86KLniMb0 zS/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:message-id :mime-version:subject:cc:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=pmsKiO2Z7XzR+1yA2ICKVpre7KmPDK/mTQO3nK8dqxw=; b=Jw2xtuEEhXNx1FphblCtYAs9OJs87ch0GWoWckTpJ8K3l/6hgGGH4wlFE0FtbqJil1 gXnT21zJEM0vh6N8Kxc/UFeQ9oL8CA1AFM43H1NFEsrxdWlQ5y03O37YWuR+Etf3Yvk+ k72uh0qnnnpOjZTHZBFVf9r6yLKLrmLxh1eGX10W8VhiX6pwCnNuKKPg9UXwUNVTm/nG flHkvU1B8RzB1I4CSnmIlCLQ0XYlAOpe9DdiLuyRTD/Ieu31mL75h33kB7wjDjsxb3gM 0XFCfOV8AF32waViLmjDh2ywLs2e/qxFGdS0Bi9qD7AEStQ670QNWrHkIk2Gu8f+5cze n1hw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=pv6WUmN1; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id s4-20020a056402520400b004523826ed6dsi9956420edd.554.2022.11.07.01.02.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Nov 2022 01:02:59 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=pv6WUmN1; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5D948385840B for ; Mon, 7 Nov 2022 09:02:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5D948385840B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667811778; bh=pmsKiO2Z7XzR+1yA2ICKVpre7KmPDK/mTQO3nK8dqxw=; h=Date:To:cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=pv6WUmN1OyZApJlArVNvjNR8hHnw/E7MwSPMpeBlZOWR5sdJ2cBM3c0oZXiLZj13k PBVhzV0qxXVupstsg24XtW/TKvaZGkODftNZ6A/+gA8uAuyDsrACUh+BlU9ryH3yTU l4+EMfSm/JZ8A5Ne593JfpfeCvxIIrjXizrRF02g= 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 [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id 1496D3858D20 for ; Mon, 7 Nov 2022 09:02:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1496D3858D20 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 0D0A5222C4; Mon, 7 Nov 2022 09:02:12 +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 E59EA13494; Mon, 7 Nov 2022 09:02:11 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id nXvJNpPJaGNyPgAAMHmgww (envelope-from ); Mon, 07 Nov 2022 09:02:11 +0000 Date: Mon, 7 Nov 2022 10:02:11 +0100 (CET) To: gcc-patches@gcc.gnu.org cc: Jakub Jelinek Subject: [PATCH][RFC] tree-optimization/107389 - use __builtin_assume_alignment at -O0 MIME-Version: 1.0 Message-Id: <20221107090211.E59EA13494@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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 Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748827404556423845?= X-GMAIL-MSGID: =?utf-8?q?1748827404556423845?= The following adds a fold_builtins pass at -O0, keying off some unwanted optimization and setting pointer alignment of the result of __builtin_assume_alignment before removing the call. This allows libatomic calls to be elided at -O0 on s390 for __uint128_t foo(__uint128_t *p) { return __atomic_load_n((__uint128_t *)__builtin_assume_aligned (p, 16), 0); } not sure how to reliably test this though. Thoughts? Thanks, Richard. PR tree-optimization/107389 * tree-pass.h (pass_fold_builtins_O0): Declare. * passes.def (pass_fold_builtins_O0): Schedule. * tree-ssa-ccp.cc (pass_fold_builtins_O0): Define. (pass_fold_builtins): Template, add gate. (pass_fold_builtins::execute): Apply alignment to the target of __builtin_assume_aligned at -O0. Disable most optimizations for the -O0 pass instance. --- gcc/passes.def | 1 + gcc/tree-pass.h | 1 + gcc/tree-ssa-ccp.cc | 52 +++++++++++++++++++++++++++++++++++++-------- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/gcc/passes.def b/gcc/passes.def index 193b5794749..4cc7c8acfcb 100644 --- a/gcc/passes.def +++ b/gcc/passes.def @@ -419,6 +419,7 @@ along with GCC; see the file COPYING3. If not see NEXT_PASS (pass_lower_vaarg); NEXT_PASS (pass_lower_vector); NEXT_PASS (pass_lower_complex_O0); + NEXT_PASS (pass_fold_builtins_O0); NEXT_PASS (pass_sancov_O0); NEXT_PASS (pass_lower_switch_O0); NEXT_PASS (pass_asan_O0); diff --git a/gcc/tree-pass.h b/gcc/tree-pass.h index 8480d41384b..43a4811128b 100644 --- a/gcc/tree-pass.h +++ b/gcc/tree-pass.h @@ -441,6 +441,7 @@ extern gimple_opt_pass *make_pass_warn_printf (gcc::context *ctxt); extern gimple_opt_pass *make_pass_warn_recursion (gcc::context *ctxt); extern gimple_opt_pass *make_pass_strlen (gcc::context *ctxt); extern gimple_opt_pass *make_pass_fold_builtins (gcc::context *ctxt); +extern gimple_opt_pass *make_pass_fold_builtins_O0 (gcc::context *ctxt); extern gimple_opt_pass *make_pass_post_ipa_warn (gcc::context *ctxt); extern gimple_opt_pass *make_pass_stdarg (gcc::context *ctxt); extern gimple_opt_pass *make_pass_early_warn_uninitialized (gcc::context *ctxt); diff --git a/gcc/tree-ssa-ccp.cc b/gcc/tree-ssa-ccp.cc index 9778e776cf2..adb1e351e15 100644 --- a/gcc/tree-ssa-ccp.cc +++ b/gcc/tree-ssa-ccp.cc @@ -4197,6 +4197,7 @@ const pass_data pass_data_fold_builtins = TODO_update_ssa, /* todo_flags_finish */ }; +template class pass_fold_builtins : public gimple_opt_pass { public: @@ -4204,14 +4205,17 @@ public: : gimple_opt_pass (pass_data_fold_builtins, ctxt) {} + bool gate (function *) final override { return O0 == !optimize; } + /* opt_pass methods: */ opt_pass * clone () final override { return new pass_fold_builtins (m_ctxt); } unsigned int execute (function *) final override; }; // class pass_fold_builtins +template unsigned int -pass_fold_builtins::execute (function *fun) +pass_fold_builtins::execute (function *fun) { bool cfg_changed = false; basic_block bb; @@ -4245,7 +4249,7 @@ pass_fold_builtins::execute (function *fun) continue; } } - else if (gimple_assign_load_p (stmt) && gimple_store_p (stmt)) + else if (!O0 && gimple_assign_load_p (stmt) && gimple_store_p (stmt)) optimize_memcpy (&i, gimple_assign_lhs (stmt), gimple_assign_rhs1 (stmt), NULL_TREE); gsi_next (&i); @@ -4266,7 +4270,7 @@ pass_fold_builtins::execute (function *fun) } fcode = DECL_FUNCTION_CODE (callee); - if (fold_stmt (&i)) + if (!O0 && fold_stmt (&i)) ; else { @@ -4281,7 +4285,29 @@ pass_fold_builtins::execute (function *fun) break; case BUILT_IN_ASSUME_ALIGNED: - /* Remove __builtin_assume_aligned. */ + /* Remove __builtin_assume_aligned. At -O0 preserve + the alignment info in the result. */ + if (O0) + { + tree align = gimple_call_arg (stmt, 1); + tree misalign = (gimple_call_num_args (stmt) > 2 + ? gimple_call_arg (stmt, 2) : NULL_TREE); + tree lhs = gimple_call_lhs (stmt); + if (lhs + && POINTER_TYPE_P (TREE_TYPE (lhs)) + && tree_fits_uhwi_p (align) + && (!misalign || tree_fits_uhwi_p (misalign))) + { + ptr_info_def *pi = get_ptr_info (lhs); + unsigned aligni = TREE_INT_CST_LOW (align); + unsigned misaligni + = misalign ? TREE_INT_CST_LOW (misalign) : 0; + if (aligni > 1 + && (aligni & (aligni - 1)) == 0 + && (misaligni & ~(aligni - 1)) == 0) + set_ptr_info_alignment (pi, aligni, misaligni); + } + } result = gimple_call_arg (stmt, 0); break; @@ -4293,7 +4319,7 @@ pass_fold_builtins::execute (function *fun) continue; case BUILT_IN_UNREACHABLE: - if (optimize_unreachable (i)) + if (!O0 && optimize_unreachable (i)) cfg_changed = true; break; @@ -4454,7 +4480,8 @@ pass_fold_builtins::execute (function *fun) break; case BUILT_IN_MEMCPY: - if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL) + if (!O0 + && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL) && TREE_CODE (gimple_call_arg (stmt, 0)) == ADDR_EXPR && TREE_CODE (gimple_call_arg (stmt, 1)) == ADDR_EXPR && TREE_CODE (gimple_call_arg (stmt, 2)) == INTEGER_CST) @@ -4470,7 +4497,8 @@ pass_fold_builtins::execute (function *fun) case BUILT_IN_VA_END: case BUILT_IN_VA_COPY: /* These shouldn't be folded before pass_stdarg. */ - result = optimize_stdarg_builtin (stmt); + if (!O0) + result = optimize_stdarg_builtin (stmt); break; default:; @@ -4534,7 +4562,13 @@ pass_fold_builtins::execute (function *fun) gimple_opt_pass * make_pass_fold_builtins (gcc::context *ctxt) { - return new pass_fold_builtins (ctxt); + return new pass_fold_builtins (ctxt); +} + +gimple_opt_pass * +make_pass_fold_builtins_O0 (gcc::context *ctxt) +{ + return new pass_fold_builtins (ctxt); } /* A simple pass that emits some warnings post IPA. */ @@ -4566,7 +4600,7 @@ public: bool gate (function *) final override { return warn_nonnull != 0; } unsigned int execute (function *) final override; -}; // class pass_fold_builtins +}; // class pass_post_ipa_warn unsigned int pass_post_ipa_warn::execute (function *fun)