From patchwork Sat Jun 22 18:54:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andi Kleen X-Patchwork-Id: 1951123 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=U78BqOYr; 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 4W63Sq3Dlnz20X6 for ; Sun, 23 Jun 2024 05:00:11 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BBFBC385DDF9 for ; Sat, 22 Jun 2024 19:00:09 +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.17]) by sourceware.org (Postfix) with ESMTPS id 83A0E385DDF3 for ; Sat, 22 Jun 2024 18:56:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 83A0E385DDF3 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 83A0E385DDF3 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.17 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719082591; cv=none; b=HjEK+Ms4JelHmcUD9caF4IcRUbFVQq5V7LtZkYXvZDBqnJ25MaRJp1mZDZ4An+JdBsEF4tXPO3jWdWwtHcnTuXuoXw/8gOOg9DxvV0y5yQUmw6Avy0CTAtkfKyXmkknDlQRRDeJRLx3hYvuJrhumtXZk3nL/rLMCIUg4FDErOcQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719082591; c=relaxed/simple; bh=7utk2TQu5gY9FUr2z+DKnMwEgWYL/jM3bu5tlpRdodk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ivP4vAdQshT7q8VFdd+4Oyj07np1CK81NwCqKDsEIhLRoYkKDISePRGVq9bUNe6iRU0LahkmUxtrICKcq4+h11MiGt60Zj871gdrWinqf5asZarHrUl7JB3yuM3zihr1JkheaVthIgO4sQ28Wz84qI3+4JGUFz9s7vraiyJOFns= 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=1719082585; x=1750618585; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7utk2TQu5gY9FUr2z+DKnMwEgWYL/jM3bu5tlpRdodk=; b=U78BqOYrvqHBgrXG7kOpa10sgkPmigbXldNvu/LddulOxkWkeh1BEBH7 RgxB1ks47j8/MBVwNFoXH3fo4vzzK5GAl1ZbmbTBqXAnEzv7lwN3pydBr fy4c5GRROf6RDrZTrH9ea3OUc7/SI6uIKZg5mCJcmVZkpLvpTvk0dRZk4 ZadNBMQyr7b7J4sLcMmQQKejfhDVm5qdMFnS5YYLxSCU/XE77nlOKyWYX 0ihu4/D2vW5CX6TK+/J4jSKstklVUkDUh7BlMqm5QjBz1iiRFs+txp6sq eZMuhjDRTvFcbOV16gghKu4vtjzkAcUY/XxlBirLbkkJYPSJvOHeMRCoV w==; X-CSE-ConnectionGUID: bLj2eN/QQ7azlAEoM8iV0w== X-CSE-MsgGUID: WZfPYlFxSEKJFydg4bHNPQ== X-IronPort-AV: E=McAfee;i="6700,10204,11111"; a="16216445" X-IronPort-AV: E=Sophos;i="6.08,258,1712646000"; d="scan'208";a="16216445" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by orvoesa109.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jun 2024 11:56:06 -0700 X-CSE-ConnectionGUID: NiPe/n1MRYeOzgvUKZMiHw== X-CSE-MsgGUID: TXLKx5suSnWC4Y3U4mBLpA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,258,1712646000"; d="scan'208";a="42968340" Received: from tassilo.jf.intel.com ([10.54.38.190]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jun 2024 11:56:05 -0700 From: Andi Kleen To: gcc-patches@gcc.gnu.org Cc: Andi Kleen Subject: [PATCH v8 09/12] Delay caller error reporting for musttail Date: Sat, 22 Jun 2024 11:54:41 -0700 Message-ID: <20240622185557.1589179-10-ak@linux.intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240622185557.1589179-1-ak@linux.intel.com> References: <20240622185557.1589179-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 Move the error reporting for caller attributes to be after the tail call discovery, so that we can give proper error messages tagged to the calls. gcc/ChangeLog: * tree-tailcall.cc (maybe_error_musttail): Declare. (suitable_for_tail_opt_p): Take call and report errors. (suitable_for_tail_call_opt_p): Take call and report errors. (find_tail_calls): Report caller errors after discovery. (tree_optimize_tail_calls_1): Remove caller suitableness check. --- gcc/tree-tailcall.cc | 62 ++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 19 deletions(-) diff --git a/gcc/tree-tailcall.cc b/gcc/tree-tailcall.cc index 4687e20e61d0..a77fa1511415 100644 --- a/gcc/tree-tailcall.cc +++ b/gcc/tree-tailcall.cc @@ -133,14 +133,20 @@ static tree m_acc, a_acc; static bitmap tailr_arg_needs_copy; +static void maybe_error_musttail (gcall *call, const char *err); + /* Returns false when the function is not suitable for tail call optimization - from some reason (e.g. if it takes variable number of arguments). */ + from some reason (e.g. if it takes variable number of arguments). CALL + is call to report for. */ static bool -suitable_for_tail_opt_p (void) +suitable_for_tail_opt_p (gcall *call) { if (cfun->stdarg) - return false; + { + maybe_error_musttail (call, _("caller uses stdargs")); + return false; + } return true; } @@ -148,35 +154,47 @@ suitable_for_tail_opt_p (void) /* Returns false when the function is not suitable for tail call optimization for some reason (e.g. if it takes variable number of arguments). This test must pass in addition to suitable_for_tail_opt_p in order to make - tail call discovery happen. */ + tail call discovery happen. CALL is call to report error for. */ static bool -suitable_for_tail_call_opt_p (void) +suitable_for_tail_call_opt_p (gcall *call) { tree param; /* alloca (until we have stack slot life analysis) inhibits sibling call optimizations, but not tail recursion. */ if (cfun->calls_alloca) - return false; + { + maybe_error_musttail (call, _("caller uses alloca")); + return false; + } /* If we are using sjlj exceptions, we may need to add a call to _Unwind_SjLj_Unregister at exit of the function. Which means that we cannot do any sibcall transformations. */ if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ && current_function_has_exception_handlers ()) - return false; + { + maybe_error_musttail (call, _("caller uses sjlj exceptions")); + return false; + } /* Any function that calls setjmp might have longjmp called from any called function. ??? We really should represent this properly in the CFG so that this needn't be special cased. */ if (cfun->calls_setjmp) - return false; + { + maybe_error_musttail (call, _("caller uses setjmp")); + return false; + } /* Various targets don't handle tail calls correctly in functions that call __builtin_eh_return. */ if (cfun->calls_eh_return) - return false; + { + maybe_error_musttail (call, _("caller uses __builtin_eh_return")); + return false; + } /* ??? It is OK if the argument of a function is taken in some cases, but not in all cases. See PR15387 and PR19616. Revisit for 4.1. */ @@ -184,7 +202,10 @@ suitable_for_tail_call_opt_p (void) param; param = DECL_CHAIN (param)) if (TREE_ADDRESSABLE (param)) - return false; + { + maybe_error_musttail (call, _("address of caller arguments taken")); + return false; + } return true; } @@ -445,10 +466,12 @@ 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. When ONLY_MUSTTAIL is set only handle musttail. */ + added to the start of RET. When ONLY_MUSTTAIL is set only handle musttail. + Update OPT_TAILCALLS as output parameter. */ static void -find_tail_calls (basic_block bb, struct tailcall **ret, bool only_musttail) +find_tail_calls (basic_block bb, struct tailcall **ret, bool only_musttail, + bool &opt_tailcalls) { tree ass_var = NULL_TREE, ret_var, func, param; gimple *stmt; @@ -526,11 +549,17 @@ find_tail_calls (basic_block bb, struct tailcall **ret, bool only_musttail) edge_iterator ei; /* Recurse to the predecessors. */ FOR_EACH_EDGE (e, ei, bb->preds) - find_tail_calls (e->src, ret, only_musttail); + find_tail_calls (e->src, ret, only_musttail, opt_tailcalls); return; } + if (!suitable_for_tail_opt_p (call)) + return; + + if (!suitable_for_tail_call_opt_p (call)) + opt_tailcalls = false; + /* If the LHS of our call is not just a simple register or local variable, we can't transform this into a tail or sibling call. This situation happens, in (e.g.) "*p = foo()" where foo returns a @@ -1200,17 +1229,12 @@ tree_optimize_tail_calls_1 (bool opt_tailcalls, bool only_mustcall) tree param; edge_iterator ei; - if (!suitable_for_tail_opt_p ()) - return 0; - if (opt_tailcalls) - opt_tailcalls = suitable_for_tail_call_opt_p (); - FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) { /* 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, only_mustcall); + find_tail_calls (e->src, &tailcalls, only_mustcall, opt_tailcalls); } if (live_vars)