From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by sourceware.org (Postfix) with ESMTPS id F087C393839A for ; Mon, 3 May 2021 08:07:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org F087C393839A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=botcazou@adacore.com Received: by mail-wm1-x32a.google.com with SMTP id u5-20020a7bc0450000b02901480e40338bso3044307wmc.1 for ; Mon, 03 May 2021 01:07:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore-com.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=+A8Z7LtX0xPe6Jq38PskhPp6NSM9NDVzLvPXMy4XE6k=; b=vx4jBWI/a38c4PbiemZPOyAII1Q/dOhd3j9jMRx2H0HydbWZgHw9+ojZwxywyDXIWR 7qj7DTHlJhkMrm/o7Jyl5ma7zCvb0+16H0JLa35gGQllsb084ke96WUL6cXtztMJN02q ydhg88s/woxWgkCqNNgPYdRiNCFaXUfk1Inov0YUOMi3s0tMVizzAo1fkifE/gAA6gxD f4CkU8SiBS4k8QggEvWNyJxthnOO5XaASqzqEVAw2jeHkr2+TnidG4rqZ6Mi4caTq6mL gqbqore/eOTWNte+td/UsKFlL5f4+TgCAWIINhrtVWS31zxvL3pXEoN/kRtbFxkx61kh F0Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=+A8Z7LtX0xPe6Jq38PskhPp6NSM9NDVzLvPXMy4XE6k=; b=Vm0BEtkDDFpBGXkcj15+SVv4YSH5CvvpEMDcCfmuYagWic6zjIyhM3MnZCSm68cqFN NH3r4uw3brtimp0iDohv3mSNfBnY4WrzAq4ESKcDcZOja0AmOBvMGi0cGWsoI0fqfB5B Eg8ncFTsXsm5E0ZFaIWKnovOs0pVKRkjzM1Sf6BmMdx4Oo7whCLLgbMXWwgYJaXlCQ6J vs53yRPx+w1jNmd0f81YJwEaBdY0nOJypToSiM44EkC/iSyKUxunQFjoUvjlKJb12Pa0 KXoTq3wZreG1LZuWpsTdI7rjVMumfjV6zpPEcAcqj6ixymQLP92J4z+zgs/wW9WdXXiH mn0A== X-Gm-Message-State: AOAM530/987PgCY6uGQMvztvYuaO5irasT7quPLRgZVNMNJNkWk8Km8O 60gxTjrRC+b4Xp+nME7QFtsaTr2K42Im7pDm X-Google-Smtp-Source: ABdhPJxDu5TAcH2pza3MVqtbuCzNVND2GnMb3jarr2klesgisQZiA6SoO5gVUSod7Qi2IRsZsEuhNQ== X-Received: by 2002:a1c:b7c4:: with SMTP id h187mr20141250wmf.178.1620029231968; Mon, 03 May 2021 01:07:11 -0700 (PDT) Received: from fomalhaut.localnet ([2a01:e0a:41b:84f0:cf71:f5e0:b050:bede]) by smtp.gmail.com with ESMTPSA id p10sm11038171wrr.58.2021.05.03.01.07.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 May 2021 01:07:11 -0700 (PDT) From: Eric Botcazou X-Google-Original-From: Eric Botcazou To: gcc-patches@gcc.gnu.org Subject: [patch] Reuse non-gimple_reg variable for inlining Date: Mon, 03 May 2021 10:04:20 +0200 Message-ID: <8762858.CDJkKcVGEf@fomalhaut> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="nextPart3045399.5fSG56mABF" Content-Transfer-Encoding: 7Bit X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-List-Received-Date: Mon, 03 May 2021 08:07:15 -0000 This is a multi-part message in MIME format. --nextPart3045399.5fSG56mABF Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Hi, when a call to a function is inlined and takes a parameter whose type is not gimple_reg, a variable is created in the caller to hold a copy of the argument passed in the call with the following comment: /* We may produce non-gimple trees by adding NOPs or introduce invalid sharing when operand is not really constant. It is not big deal to prohibit constant propagation here as we will constant propagate in DOM1 pass anyway. * Of course the second sentence of the comment does not apply to non-gimple_reg values, unless they get SRAed later, because we do not do constant propagation for them. This for example prevents two identical calls to a pure function from being merged in the attached Ada testcase. Therefore the attached patch attempts to reuse a read-only or non-addressable local DECL of the caller, the hitch being that expand_call_inline needs to be prevented from creating a CLOBBER for the cases where it ends uo being reused. Tested on x86-64/Linux, OK for the mainline? 2021-05-03 Eric Botcazou * tree-inline.c (setup_one_parameter): Do not create a variable if the value is either a read-only DECL or a non-addressable local variable. Register the variable thus reused instead of creating a new one. (expand_call_inline): Do not generate a CLOBBER for these variables. 2021-05-03 Eric Botcazou * gnat.dg/opt94.adb: New test. * gnat.dg/opt94_pkg.ads, opt94.adb: New helper. -- Eric Botcazou --nextPart3045399.5fSG56mABF Content-Disposition: attachment; filename="p.diff" Content-Transfer-Encoding: 7Bit Content-Type: text/x-patch; charset="UTF-8"; name="p.diff" diff --git a/gcc/tree-inline.c b/gcc/tree-inline.c index 1dcb31c0267..a05093ab829 100644 --- a/gcc/tree-inline.c +++ b/gcc/tree-inline.c @@ -3460,16 +3460,18 @@ setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn, value. */ if (TREE_READONLY (p) && !TREE_ADDRESSABLE (p) - && value && !TREE_SIDE_EFFECTS (value) + && value + && !TREE_SIDE_EFFECTS (value) && !def) { - /* We may produce non-gimple trees by adding NOPs or introduce - invalid sharing when operand is not really constant. - It is not big deal to prohibit constant propagation here as - we will constant propagate in DOM1 pass anyway. */ - if (is_gimple_min_invariant (value) - && useless_type_conversion_p (TREE_TYPE (p), - TREE_TYPE (value)) + /* We may produce non-gimple trees by adding NOPs or introduce invalid + sharing when the value is not constant or DECL. And we need to make + sure that it cannot be modified from another path in the callee. */ + if ((is_gimple_min_invariant (value) + || (DECL_P (value) && TREE_READONLY (value)) + || (auto_var_in_fn_p (value, id->src_fn) + && !TREE_ADDRESSABLE (value))) + && useless_type_conversion_p (TREE_TYPE (p), TREE_TYPE (value)) /* We have to be very careful about ADDR_EXPR. Make sure the base variable isn't a local variable of the inlined function, e.g., when doing recursive inlining, direct or @@ -3478,6 +3480,13 @@ setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn, && ! self_inlining_addr_expr (value, fn)) { insert_decl_map (id, p, value); + /* Prevent expand_call_inline from creating a CLOBBER for it. */ + if (VAR_P (value) & !is_gimple_reg (value)) + { + if (!id->debug_map) + id->debug_map = new hash_map; + id->debug_map->put (value, value); + } insert_debug_decl_map (id, p, var); return insert_init_debug_bind (id, bb, var, value, NULL); } @@ -5129,7 +5138,10 @@ expand_call_inline (basic_block bb, gimple *stmt, copy_body_data *id, if (!TREE_THIS_VOLATILE (p)) { tree *varp = id->decl_map->get (p); - if (varp && VAR_P (*varp) && !is_gimple_reg (*varp)) + if (varp + && VAR_P (*varp) + && !is_gimple_reg (*varp) + && !(id->debug_map && id->debug_map->get (*varp))) { tree clobber = build_clobber (TREE_TYPE (*varp)); gimple *clobber_stmt; --nextPart3045399.5fSG56mABF Content-Disposition: attachment; filename="opt94.adb" Content-Transfer-Encoding: 7Bit Content-Type: text/x-adasrc; charset="UTF-8"; name="opt94.adb" -- { dg-do compile } -- { dg-options "-O -gnatn -fdump-tree-optimized" } with Opt94_Pkg; use Opt94_Pkg; function Opt94 (S : String) return Integer is A : constant String := Get; begin if Valid_Result (A) then return Result (A); else return -1; end if; end; -- { dg-final { scan-tree-dump-times "worker" 1 "optimized" } } --nextPart3045399.5fSG56mABF Content-Disposition: attachment; filename="opt94_pkg.adb" Content-Transfer-Encoding: 7Bit Content-Type: text/x-adasrc; charset="UTF-8"; name="opt94_pkg.adb" package body Opt94_Pkg is function Worker (S : String) return Integer; pragma Pure_Function (Worker); function Valid_Result (S : String) return Boolean is begin return Worker (S) > 0; end; function Result (S : String) return Integer is R : constant Integer := Worker (S); begin if R > 0 then return R; else raise Program_Error; end if; end; function Worker (S : String) return Integer is begin return Character'Pos (S (1)); end; function Get return String is begin return ""; end; end Opt94_Pkg; --nextPart3045399.5fSG56mABF Content-Disposition: attachment; filename="opt94_pkg.ads" Content-Transfer-Encoding: 7Bit Content-Type: text/x-adasrc; charset="UTF-8"; name="opt94_pkg.ads" package Opt94_Pkg is function Valid_Result (S : String) return Boolean; pragma Inline (Valid_Result); function Result (S : String) return Integer; pragma Inline (Result); function Get return String; end Opt94_Pkg; --nextPart3045399.5fSG56mABF--