From patchwork Mon May 15 09:43:01 2023 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: 94032 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp6791575vqo; Mon, 15 May 2023 02:46:44 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6acopT898988wFPLOt0CjapAXh2TwfLlYuKtWZCFeInWAIuM8RZZXE6qMSVRCbbd+gyYCu X-Received: by 2002:a17:907:318b:b0:962:9ffa:be19 with SMTP id xe11-20020a170907318b00b009629ffabe19mr30601313ejb.5.1684144004007; Mon, 15 May 2023 02:46:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684144003; cv=none; d=google.com; s=arc-20160816; b=CVr5kQZxGD6YXfOAxZedWedHTVaEEjW3BKvIc0q1hcZ4hvgJ+kFra3QntEdaBIPLor AGSC2uXZ97Xue6UYYpvRUQr6HIfaZf6/+KBn/FZ3G+QctvSrysmecfRHeRdT1zrjBXBU 1BU8knCiS0TBy0gXHAmCC+/XfthL2tMNWdfdxzxftLhytHvH7dEtN8Dozsr4FtAlQBUs /J2Eu4v9EVkKb38l4h1n7AENrBPX/cxXOOtG/qEEsaMIQk8NmQ4mVCn2adzERN0Cu49B Z26wGyVctk9NQyws9NI4zQIo9HUSyPjgdcVGkeMT01W9eldG4KrN3JgBl2Ih8K0xDnNX Y9RQ== 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 :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=g+6kX3XtpnZCqVNzJeqDWgb3GOSkuI66cWs3DgNel/Q=; b=mdNyPCfoc3kSVTtvFtxj7qJuW2g3wSA/4qKQp9lETiA/A0sQcwow1cP3Ppr4xHDbJ3 /Znka0AvQeYFiD6O1ww3UsdBLdeJGR0/Afw2WgTbVR9Fy1H3leQtXYIwUBxtcN8yOYYy kWL/DhfQ+qFngxadKvkIfmEHwH7866Fcxd3uZsgEiqlfDdcgGMcPr5H8CfT6ISfyxjp+ a6DExWX1d8xn17PM6nX1AENEuY7+NMxrrNd9v7n5FQSjkYATcrHzZ8OyelWIbMfH1Rwy tJQoyR1F1QqM4ZDL4cUxeMJAkrQKVNaNDmEUgJcyB/ITmGt1bc6/FjecXYwf8SLclGYl Af8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=HUfStVaX; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id r21-20020a056402019500b00509d228f9casi12672030edv.146.2023.05.15.02.46.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 May 2023 02:46:43 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=HUfStVaX; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 6E3A23881D07 for ; Mon, 15 May 2023 09:44:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6E3A23881D07 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684143878; bh=g+6kX3XtpnZCqVNzJeqDWgb3GOSkuI66cWs3DgNel/Q=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=HUfStVaXhaimiFxxAx3wyw9H89g0cWMOhDSX6Pfv12MviDrSTxtEahG2XlxxAdwSK l8imATdlIM0YK+R3gkORWfpzMy6XDsZ6NZpv44CopSD4yg2UI6QEdKZtI0YkWwjg/Y TBFZoOv2FXh+v8hK+x9T+p25/hrCzFTKxSDDtlD4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id 485093851143 for ; Mon, 15 May 2023 09:43:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 485093851143 Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-3063208beedso11908241f8f.1 for ; Mon, 15 May 2023 02:43:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684143784; x=1686735784; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=g+6kX3XtpnZCqVNzJeqDWgb3GOSkuI66cWs3DgNel/Q=; b=I19PPDgWoWC4WoSYwYx6ebeMNGb45OEothPg/Ih/ef7W7NWcdOCJOTdpIIl6Gb5SfX aw1obDXS7+xap6is1ARC6QdEvkhtGDQT8FrINmfl9svTIiV+runYix2BiPNmFKquxeZr ynShoYp2zLj6+h5MxevU/FzzG6h1PavfIQYd5y3QnjP/g/0xDiMQK35L618hHrVOF/8y CMsR7t7nKZrFj7btWFnqsGipb0CdeIenyfofE4GCrYlK2u2cF0l3/Q8/sFXvYFLxI88q ZrY/EbLMpXFJSmJkqmiUkGbWhYykVCIqkcdNE3uYo9OkuWNQzt3m6a9Jpf9FJiqLlMQS jA2g== X-Gm-Message-State: AC+VfDx4vL+UW5Sbrudf103h67g0shALb3QC1G3Rd4tTP2VtIaBeuI8L 5MNcWsT0LYiPvZb12j/2DbJdb/nHIyLH4npvf7Firw== X-Received: by 2002:adf:f302:0:b0:306:3b78:fe33 with SMTP id i2-20020adff302000000b003063b78fe33mr22534275wro.32.1684143784006; Mon, 15 May 2023 02:43:04 -0700 (PDT) Received: from poulhies-Precision-5550.telnowedge.local (lmontsouris-659-1-24-67.w81-250.abo.wanadoo.fr. [81.250.175.67]) by smtp.gmail.com with ESMTPSA id k9-20020adfe8c9000000b0030642f5da27sm31788103wrn.37.2023.05.15.02.43.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 May 2023 02:43:03 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: Bob Duff Subject: [COMMITTED] ada: Optimize 2**N to avoid explicit 'if' in modular case Date: Mon, 15 May 2023 11:43:01 +0200 Message-Id: <20230515094301.1407966-1-poulhies@adacore.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.4 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, T_SCC_BODY_TEXT_LINE 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: =?utf-8?q?Marc_Poulhi=C3=A8s_via_Gcc-patches?= From: =?utf-8?q?Marc_Poulhi=C3=A8s?= Reply-To: =?utf-8?q?Marc_Poulhi=C3=A8s?= 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?1765952983051598175?= X-GMAIL-MSGID: =?utf-8?q?1765952983051598175?= From: Bob Duff The compiler usually turns 2**N into Shift_Left(1,N). This patch removes the check for "shift amount too big" in the modular case, because Shift_Left works properly in that case (i.e. if N is very large, it returns 0). This removes a redundant check on most hardware; Shift_Left takes care of large shirt amounts as necessary, even though most hardware does not. gcc/ada/ * exp_ch4.adb (Expand_N_Op_Expon): Remove the too-big check. Simplify. Signed and modular cases are combined, etc. Remove code with comment "We only handle cases where the right type is a[sic] integer", because the right operand must always be an integer at this point. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/exp_ch4.adb | 131 +++++++++----------------------------------- 1 file changed, 26 insertions(+), 105 deletions(-) diff --git a/gcc/ada/exp_ch4.adb b/gcc/ada/exp_ch4.adb index 31823eaeca7..c1fe02d60c1 100644 --- a/gcc/ada/exp_ch4.adb +++ b/gcc/ada/exp_ch4.adb @@ -9048,7 +9048,7 @@ package body Exp_Ch4 is end if; end if; - -- Deal with optimizing 2 ** expression to shift where possible + -- Optimize 2 ** expression to shift where possible -- Note: we used to check that Exptyp was an unsigned type. But that is -- an unnecessary check, since if Exp is negative, we have a run-time @@ -9063,14 +9063,8 @@ package body Exp_Ch4 is and then CRT_Safe_Compile_Time_Known_Value (Base) and then Expr_Value (Base) = Uint_2 - -- We only handle cases where the right type is a integer - - and then Is_Integer_Type (Root_Type (Exptyp)) - and then Esize (Root_Type (Exptyp)) <= Standard_Integer_Size - -- This transformation is not applicable for a modular type with a - -- nonbinary modulus because we do not handle modular reduction in - -- a correct manner if we attempt this transformation in this case. + -- nonbinary modulus because shifting makes no sense in that case. and then not Non_Binary_Modulus (Typ) then @@ -9107,61 +9101,26 @@ package body Exp_Ch4 is end if; end; - -- Here we just have 2 ** N on its own, so we can convert this to a - -- shift node. We are prepared to deal with overflow here, and we - -- also have to handle proper modular reduction for binary modular. + -- Here we have 2 ** N on its own, so we can convert this into a + -- shift. else - declare - OK : Boolean; - Lo : Uint; - Hi : Uint; - - MaxS : Uint; - -- Maximum shift count with no overflow - - TestS : Boolean; - -- Set True if we must test the shift count - - Test_Gt : Node_Id; - -- Node for test against TestS - - begin - -- Compute maximum shift based on the underlying size. For a - -- modular type this is one less than the size. - - if Is_Modular_Integer_Type (Typ) then - - -- For modular integer types, this is the size of the value - -- being shifted minus one. Any larger values will cause - -- modular reduction to a result of zero. Note that we do - -- want the RM_Size here (e.g. mod 2 ** 7, we want a result - -- of 6, since 2**7 should be reduced to zero). - - MaxS := RM_Size (Rtyp) - 1; - - -- For signed integer types, we use the size of the value - -- being shifted minus 2. Larger values cause overflow. + -- Op_Shift_Left (generated below) has modular-shift semantics; + -- therefore we might need to generate an overflow check here + -- if the type is signed. - else - MaxS := Esize (Rtyp) - 2; - end if; - - -- Determine range to see if it can be larger than MaxS - - Determine_Range (Exp, OK, Lo, Hi, Assume_Valid => True); - TestS := (not OK) or else Hi > MaxS; - - -- Signed integer case - - if Is_Signed_Integer_Type (Typ) then + if Is_Signed_Integer_Type (Typ) and then Ovflo then + declare + OK : Boolean; + Lo : Uint; + Hi : Uint; - -- Generate overflow check if overflow is active. Note that - -- we can simply ignore the possibility of overflow if the - -- flag is not set (means that overflow cannot happen or - -- that overflow checks are suppressed). + MaxS : constant Uint := Esize (Rtyp) - 2; + -- Maximum shift count with no overflow + begin + Determine_Range (Exp, OK, Lo, Hi, Assume_Valid => True); - if Ovflo and TestS then + if (not OK) or else Hi > MaxS then Insert_Action (N, Make_Raise_Constraint_Error (Loc, Condition => @@ -9170,56 +9129,18 @@ package body Exp_Ch4 is Right_Opnd => Make_Integer_Literal (Loc, MaxS)), Reason => CE_Overflow_Check_Failed)); end if; + end; + end if; - -- Now rewrite node as Shift_Left (1, right-operand) - - Rewrite (N, - Make_Op_Shift_Left (Loc, - Left_Opnd => Make_Integer_Literal (Loc, Uint_1), - Right_Opnd => Exp)); - - -- Modular integer case - - else pragma Assert (Is_Modular_Integer_Type (Typ)); - - -- If shift count can be greater than MaxS, we need to wrap - -- the shift in a test that will reduce the result value to - -- zero if this shift count is exceeded. - - if TestS then - - -- Note: build node for the comparison first, before we - -- reuse the Right_Opnd, so that we have proper parents - -- in place for the Duplicate_Subexpr call. - - Test_Gt := - Make_Op_Gt (Loc, - Left_Opnd => Duplicate_Subexpr (Exp), - Right_Opnd => Make_Integer_Literal (Loc, MaxS)); - - Rewrite (N, - Make_If_Expression (Loc, - Expressions => New_List ( - Test_Gt, - Make_Integer_Literal (Loc, Uint_0), - Make_Op_Shift_Left (Loc, - Left_Opnd => Make_Integer_Literal (Loc, Uint_1), - Right_Opnd => Exp)))); - - -- If we know shift count cannot be greater than MaxS, then - -- it is safe to just rewrite as a shift with no test. + -- Generate Shift_Left (1, Exp) - else - Rewrite (N, - Make_Op_Shift_Left (Loc, - Left_Opnd => Make_Integer_Literal (Loc, Uint_1), - Right_Opnd => Exp)); - end if; - end if; + Rewrite (N, + Make_Op_Shift_Left (Loc, + Left_Opnd => Make_Integer_Literal (Loc, Uint_1), + Right_Opnd => Exp)); - Analyze_And_Resolve (N, Typ); - return; - end; + Analyze_And_Resolve (N, Typ); + return; end if; end if;