From patchwork Tue Jul 2 13:21:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 1955289 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; secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=h6uvMnUe; 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 4WD3Z371psz1xpN for ; Tue, 2 Jul 2024 23:25:31 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2BC493882671 for ; Tue, 2 Jul 2024 13:25:30 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by sourceware.org (Postfix) with ESMTPS id F08A13882064 for ; Tue, 2 Jul 2024 13:21:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F08A13882064 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F08A13882064 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::12f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719926519; cv=none; b=e85RPtX+/j0IJjgyIP3DmP6YcK38x6ERV30cQN4Q/Y66gLu02ZXW034/5SYaCMNKzRlHVd3DABRo0hc0exxOrCA8qjgcztFrpYX3ozn7HStGAq0oqp84t4n3Xp+vIBcSxMiuAmSgwXrOAz7FEbgUGTCXPOGqPcy9mirrGBOvuOc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719926519; c=relaxed/simple; bh=st09M3Y9Cp11c7Hva3EFO4YgrE2i0z58yKnx/r275Tc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=h7TZNTp8o5cmd0KAWqAV253c6EJVRHsW4LHVrn1Qk0mbscOPfxFx1r908Y1R+l1IiTfTd/yrUp0MpIt3eW6uvpLH1xIuiw1NcOheEt2KJbM4TTlVeorxAU0k7gKCqyVlfdCc0GQnMuKuTHWgblwC84jJbVmkyhRjt2W5rmyTBKg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lf1-x12f.google.com with SMTP id 2adb3069b0e04-52cecba8d11so5064746e87.1 for ; Tue, 02 Jul 2024 06:21:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1719926515; x=1720531315; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=193wqb0ZQTFIvWDRKe79zVZu81pjPx8LwvEf62SLLww=; b=h6uvMnUe2pn2O0LqWjpx4z7MHH6ULa/uuxtUoRMpw2LC7sOTGKaPkOgSuLbBOc+zY7 ANC3Q3B4HVDPszb/b3EMhioLl3zDjB7gK2PAIhzGZozr2OckYDpM81Cy4sCgsVADgPxg ijHAnJSh4dPiiVeJjYBoHC36V3gbqrDTw/AOEPeiUaSmM+pRVNgJMRwh66g1Fk0sqKYs wEVkWslAv1DlncTHqiXSm+NRGQFDN6XyVE9AZz+GahwwzqFU24xFR3Qw1Tvf1X/+Bwu1 MQD5LETE2ifam+ZQUUL0Ou3xNPhmWtwXi8bWdxIaOT5YwMqr0ldfKVo8GiEaUtB0r2l7 DR3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719926515; x=1720531315; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=193wqb0ZQTFIvWDRKe79zVZu81pjPx8LwvEf62SLLww=; b=bDy/B9BLRicVcs4x90ejziODmvLrWAkGNYEI3hLa2/oFRNSakKQcW4tO3vnSAaFc2A oknPZjUHEQdoT+oO8qZoOKzMhLC8KazlnsWP3uiDoAJ2fvxVG/ti81uukh02F6JJT7Sp GRWrW37yk7heoFCoT5aT9+USqBmtv4QpAnDtNUqtbW6RED59LNfrI+lcQKrypYfmJKrb JomkM8Mxa34uksWqcUdvpKxcWsMFMfz9u/P7kUFnoAxEhp3XSg5ak54OnU0b0PyQf/++ R7I+66SImTcqZNtUgamIz9ivd1e300U+D/IOt66agY1QOowM9Bm+z5DPzbpGF1vBiPG9 jxAA== X-Gm-Message-State: AOJu0YzjcIjW/PkoZ73q8L/pNsPe+DKiN5tzyGWOAejBccR7uo4ZJ9iC +esTW7L18XR8HCn/hoCnCHQAjVz1fgLytzmYiEE2UPFH048A54vLe52KC9uwdgXED6I5gfkGnIE = X-Google-Smtp-Source: AGHT+IFQgN4HYSJPorvGjnCnLSxRkcftJJ66DHTceMu/4CQ+L2+W4+Y9AZckjsudwCWdgz7ikg8Tfw== X-Received: by 2002:a05:6512:3dab:b0:52c:d085:9978 with SMTP id 2adb3069b0e04-52e82733e8amr6204158e87.62.1719926515467; Tue, 02 Jul 2024 06:21:55 -0700 (PDT) Received: from poulhies-Precision-5550.lan ([2001:861:3382:1a90:53cf:a5ff:fb60:5a70]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4256b09abbfsm197319895e9.35.2024.07.02.06.21.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jul 2024 06:21:54 -0700 (PDT) From: =?utf-8?q?Marc_Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED 13/13] ada: Use static allocation for small dynamic string concatenations in more cases Date: Tue, 2 Jul 2024 15:21:29 +0200 Message-ID: <20240702132130.523603-13-poulhies@adacore.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240702132130.523603-1-poulhies@adacore.com> References: <20240702132130.523603-1-poulhies@adacore.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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.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 From: Eric Botcazou This lifts the limitation of the original implementation whereby the first operand of the concatenation needs to have a length known at compiled time in order for the static allocation to be used. gcc/ada/ * exp_ch4.adb (Expand_Concatenate): In the case where an operand does not have both bounds known at compile time, use nevertheless the low bound directly if it is known at compile time. Fold the conditional expression giving the low bound of the result in the general case if the low bound of all the operands are equal. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/exp_ch4.adb | 91 +++++++++++++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 24 deletions(-) diff --git a/gcc/ada/exp_ch4.adb b/gcc/ada/exp_ch4.adb index e4c9de474ad..abe76c8767e 100644 --- a/gcc/ada/exp_ch4.adb +++ b/gcc/ada/exp_ch4.adb @@ -2837,13 +2837,32 @@ package body Exp_Ch4 is if not Set then NN := NN + 1; - -- Capture operand bounds + -- Set low bound of operand and check first the constrained + -- case with known bound - Opnd_Low_Bound (NN) := - Make_Attribute_Reference (Loc, - Prefix => - Duplicate_Subexpr (Opnd, Name_Req => True), - Attribute_Name => Name_First); + if Is_Constrained (Opnd_Typ) then + declare + Low_Bound : constant Node_Id + := Type_Low_Bound + (Underlying_Type (Etype (First_Index (Opnd_Typ)))); + + begin + if Compile_Time_Known_Value (Low_Bound) then + Opnd_Low_Bound (NN) := New_Copy_Tree (Low_Bound); + Set := True; + end if; + end; + end if; + + -- Otherwise fall back to the general expression + + if not Set then + Opnd_Low_Bound (NN) := + Make_Attribute_Reference (Loc, + Prefix => + Duplicate_Subexpr (Opnd, Name_Req => True), + Attribute_Name => Name_First); + end if; -- Capture last operand bounds if result could be null @@ -3018,6 +3037,8 @@ package body Exp_Ch4 is -- take unconditionally whether or not it is null. It's easiest to do -- this with a recursive procedure: + -- We fold the common case where all the low bounds are the same + else declare function Get_Known_Bound (J : Nat) return Node_Id; @@ -3033,32 +3054,54 @@ package body Exp_Ch4 is return New_Copy_Tree (Opnd_Low_Bound (J)); else - return - Make_If_Expression (Loc, - Expressions => New_List ( + declare + Known_Bound : constant Node_Id := Get_Known_Bound (J + 1); + Comparison : constant Compare_Result + := Compile_Time_Compare + (Opnd_Low_Bound (J), + Known_Bound, + Assume_Valid => True); - Make_Op_Ne (Loc, - Left_Opnd => - New_Occurrence_Of (Var_Length (J), Loc), - Right_Opnd => - Make_Integer_Literal (Loc, 0)), + begin + if Comparison = EQ then + return Known_Bound; - New_Copy_Tree (Opnd_Low_Bound (J)), - Get_Known_Bound (J + 1))); + else + return + Make_If_Expression (Loc, + Expressions => New_List ( + + Make_Op_Ne (Loc, + Left_Opnd => + New_Occurrence_Of (Var_Length (J), Loc), + Right_Opnd => + Make_Integer_Literal (Loc, 0)), + + New_Copy_Tree (Opnd_Low_Bound (J)), + Known_Bound)); + end if; + end; end if; end Get_Known_Bound; + Known_Bound : constant Node_Id := Get_Known_Bound (1); + begin - Ent := Make_Temporary (Loc, 'L'); + if Nkind (Known_Bound) /= N_If_Expression then + Low_Bound := Known_Bound; - Append_To (Actions, - Make_Object_Declaration (Loc, - Defining_Identifier => Ent, - Constant_Present => True, - Object_Definition => New_Occurrence_Of (Ityp, Loc), - Expression => Get_Known_Bound (1))); + else + Ent := Make_Temporary (Loc, 'L'); - Low_Bound := New_Occurrence_Of (Ent, Loc); + Append_To (Actions, + Make_Object_Declaration (Loc, + Defining_Identifier => Ent, + Constant_Present => True, + Object_Definition => New_Occurrence_Of (Ityp, Loc), + Expression => Known_Bound)); + + Low_Bound := New_Occurrence_Of (Ent, Loc); + end if; end; end if;