From patchwork Tue May 21 14:28:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 1937465 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=ez0uCZ8/; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VkH6R4Qp7z1ynR for ; Wed, 22 May 2024 00:35:43 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DB1703858D1E for ; Tue, 21 May 2024 14:35:41 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) by sourceware.org (Postfix) with ESMTPS id B6E1E384AB55 for ; Tue, 21 May 2024 14:32:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B6E1E384AB55 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.intel.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=linux.intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B6E1E384AB55 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.14 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301952; cv=none; b=YI4xiP7cmOXJs+VRWrI8qhIvTfAX9t3nqNmRa610p8Wgj6CozXMcaAuRB3EcPYGqvwQhjglloLMoAOWNOeNtAgsYdvZw2PuQ0Ic7mU2l6cTC5s33UZ+Pl5uZ9Y1QSAiT1sCgRKjAL+WZYR2s26TyhtQpxY4AeQCQFFmP+m8WXTw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716301952; c=relaxed/simple; bh=l/qU6rQnzEnIledCINoxQXm7qusUO3h9o6VSPCAtbL8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=LVQAmvgXb+bA9SGnQ/021Y/IKjyp+Md4vQjKAWBAxs6OyVAlKQMNia76fcoF7IvRXd15rtUf9Au/lSPhGVz2eTFeqj/JMe67yequiDmZUn2FR8AiyQS/f9a644QCx7Lv6NXC+GHKpYC/YKumFLqa+lnj8zgnAhs2rYDyCErINY8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1716301950; x=1747837950; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=l/qU6rQnzEnIledCINoxQXm7qusUO3h9o6VSPCAtbL8=; b=ez0uCZ8/nto33NKXdRiU8mDbtWXDvupeJeQpzOW6c+1chbzchvtaF+DN h62ziWs1GpW59QJu8r9famyXhBln+5XNLYcHeRylduKXn9Fi2v3dV6LA0 IA1h2KF5T+5WrX6OUJPb4YWDtg6cgAP9sEF2jLmC3U6vM9WXqJyK3Ogbn zlEyGZO+3rfKGjLv28DHqbMDlQh5TypDhKHB1ZMReZ+fjIb15C3MpUU5Q AgfiM1CMJ1xpVpgUrBlDDM0Qs6En+VW0VCRn4/gtvPAJV5NTzb7adVb0l XM5VGb5/FICkWtFNYXvqw73WvIA14IigE8wA496omeMkC1o5oP8MfZ9et w==; X-CSE-ConnectionGUID: VVPk69fYRziNg1pqa0dkQA== X-CSE-MsgGUID: vuOLh1IhQQ6Z4GQ4tPwyhg== X-IronPort-AV: E=McAfee;i="6600,9927,11078"; a="16328283" X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="16328283" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:19 -0700 X-CSE-ConnectionGUID: hbFNE5tvS3WSpN0nDbN6gA== X-CSE-MsgGUID: n9KUXhKdT3KiW4Asc6ePGg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,178,1712646000"; d="scan'208";a="64166854" Received: from tassilo.jf.intel.com ([10.54.38.190]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2024 07:32:20 -0700 From: Andi Kleen To: gcc-patches@gcc.gnu.org Cc: richard.guenther@gmail.com, nathan@acm.org, josmyers@redhat.com, richard.sandiford@arm.com, jason@redhat.com, Andi Kleen Subject: [PATCH v6 6/8] Enable musttail tail conversion even when not optimizing Date: Tue, 21 May 2024 07:28:39 -0700 Message-ID: <20240521143203.2893096-7-ak@linux.intel.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240521143203.2893096-1-ak@linux.intel.com> References: <20240521143203.2893096-1-ak@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Enable the tailcall optimization for non optimizing builds, but in this case only checks calls that have the musttail attribute set. This makes musttail work without optimization. This is done with a new late musttail pass that is only active when not optimizing. The pass must be after ehcleanup. gcc/ChangeLog: * passes.def (pass_musttail): Add. * tree-pass.h (make_pass_musttail): Add. * tree-tailcall.cc (find_tail_calls): Handle only_musttail argument. (tree_optimize_tail_calls_1): Pass on only_musttail. (execute_tail_calls): Pass only_musttail as false. (class pass_musttail): Add. (make_pass_musttail): Add. --- gcc/passes.def | 1 + gcc/tree-pass.h | 1 + gcc/tree-tailcall.cc | 64 +++++++++++++++++++++++++++++++++++++------- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/gcc/passes.def b/gcc/passes.def index 1cbbd4130970..3e83cc327fd2 100644 --- a/gcc/passes.def +++ b/gcc/passes.def @@ -443,6 +443,7 @@ along with GCC; see the file COPYING3. If not see NEXT_PASS (pass_tsan_O0); NEXT_PASS (pass_sanopt); NEXT_PASS (pass_cleanup_eh); + NEXT_PASS (pass_musttail); NEXT_PASS (pass_lower_resx); NEXT_PASS (pass_nrv); NEXT_PASS (pass_gimple_isel); diff --git a/gcc/tree-pass.h b/gcc/tree-pass.h index 29267589eeb3..0668cea0a48e 100644 --- a/gcc/tree-pass.h +++ b/gcc/tree-pass.h @@ -368,6 +368,7 @@ extern gimple_opt_pass *make_pass_sra (gcc::context *ctxt); extern gimple_opt_pass *make_pass_sra_early (gcc::context *ctxt); extern gimple_opt_pass *make_pass_tail_recursion (gcc::context *ctxt); extern gimple_opt_pass *make_pass_tail_calls (gcc::context *ctxt); +extern gimple_opt_pass *make_pass_musttail (gcc::context *ctxt); extern gimple_opt_pass *make_pass_fix_loops (gcc::context *ctxt); extern gimple_opt_pass *make_pass_tree_loop (gcc::context *ctxt); extern gimple_opt_pass *make_pass_tree_no_loop (gcc::context *ctxt); diff --git a/gcc/tree-tailcall.cc b/gcc/tree-tailcall.cc index e9f7f8a12b3a..094856de22ef 100644 --- a/gcc/tree-tailcall.cc +++ b/gcc/tree-tailcall.cc @@ -408,10 +408,10 @@ static live_vars_map *live_vars; static vec live_vars_vec; /* Finds tailcalls falling into basic block BB. The list of found tailcalls is - added to the start of RET. */ + added to the start of RET. When ONLY_MUSTTAIL is set only handle musttail. */ static void -find_tail_calls (basic_block bb, struct tailcall **ret) +find_tail_calls (basic_block bb, struct tailcall **ret, bool only_musttail) { tree ass_var = NULL_TREE, ret_var, func, param; gimple *stmt; @@ -445,6 +445,9 @@ find_tail_calls (basic_block bb, struct tailcall **ret) if (is_gimple_call (stmt)) { call = as_a (stmt); + /* Handle only musttail calls when not optimizing. */ + if (only_musttail && !gimple_call_must_tail_p (call)) + return; ass_var = gimple_call_lhs (call); break; } @@ -467,7 +470,7 @@ find_tail_calls (basic_block bb, struct tailcall **ret) edge_iterator ei; /* Recurse to the predecessors. */ FOR_EACH_EDGE (e, ei, bb->preds) - find_tail_calls (e->src, ret); + find_tail_calls (e->src, ret, only_musttail); return; } @@ -528,7 +531,8 @@ find_tail_calls (basic_block bb, struct tailcall **ret) func = gimple_call_fndecl (call); if (func && !fndecl_built_in_p (func) - && recursive_call_p (current_function_decl, func)) + && recursive_call_p (current_function_decl, func) + && !only_musttail) { tree arg; @@ -1094,10 +1098,11 @@ create_tailcall_accumulator (const char *label, basic_block bb, tree init) } /* Optimizes tail calls in the function, turning the tail recursion - into iteration. */ + into iteration. When ONLY_MUSTCALL is true only optimize mustcall + marked calls. */ static unsigned int -tree_optimize_tail_calls_1 (bool opt_tailcalls) +tree_optimize_tail_calls_1 (bool opt_tailcalls, bool only_mustcall) { edge e; bool phis_constructed = false; @@ -1117,7 +1122,7 @@ tree_optimize_tail_calls_1 (bool opt_tailcalls) /* Only traverse the normal exits, i.e. those that end with return statement. */ if (safe_is_a (*gsi_last_bb (e->src))) - find_tail_calls (e->src, &tailcalls); + find_tail_calls (e->src, &tailcalls, only_mustcall); } if (live_vars) @@ -1228,7 +1233,7 @@ gate_tail_calls (void) static unsigned int execute_tail_calls (void) { - return tree_optimize_tail_calls_1 (true); + return tree_optimize_tail_calls_1 (true, false); } namespace { @@ -1261,7 +1266,7 @@ public: bool gate (function *) final override { return gate_tail_calls (); } unsigned int execute (function *) final override { - return tree_optimize_tail_calls_1 (false); + return tree_optimize_tail_calls_1 (false, false); } }; // class pass_tail_recursion @@ -1312,3 +1317,44 @@ make_pass_tail_calls (gcc::context *ctxt) { return new pass_tail_calls (ctxt); } + +namespace { + +const pass_data pass_data_musttail = +{ + GIMPLE_PASS, /* type */ + "musttail", /* name */ + OPTGROUP_NONE, /* optinfo_flags */ + TV_NONE, /* tv_id */ + ( PROP_cfg | PROP_ssa ), /* properties_required */ + 0, /* properties_provided */ + 0, /* properties_destroyed */ + 0, /* todo_flags_start */ + 0, /* todo_flags_finish */ +}; + +class pass_musttail : public gimple_opt_pass +{ +public: + pass_musttail (gcc::context *ctxt) + : gimple_opt_pass (pass_data_musttail, ctxt) + {} + + /* opt_pass methods: */ + /* This pass is only used when not optimizing to make [[musttail]] still + work. */ + bool gate (function *) final override { return !flag_optimize_sibling_calls; } + unsigned int execute (function *) final override + { + return tree_optimize_tail_calls_1 (true, true); + } + +}; // class pass_musttail + +} // anon namespace + +gimple_opt_pass * +make_pass_musttail (gcc::context *ctxt) +{ + return new pass_musttail (ctxt); +}