From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x22b.google.com (mail-lj1-x22b.google.com [IPv6:2a00:1450:4864:20::22b]) by sourceware.org (Postfix) with ESMTPS id 096FD3858433 for ; Mon, 10 Jun 2024 09:08:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 096FD3858433 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 096FD3858433 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::22b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718010510; cv=none; b=aa2h+It+R1AogQcpb1JtZ8JsLlzqh8PSn+b1s2Y2nMa6o2lK1PgwEPuYKpzgouh0ASigzjkUTBpsvZvmg2TCfnB3pZgow6pkXIOcPqesELdG4BGxwfgbMCJ7jIfIAMl1Ri5Du6+YtM2cwZhRXhuOA4Jc15ylFs421p0HjqI4D0U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718010510; c=relaxed/simple; bh=WnmDIXRBHckYbQCErU0wLvkbU8YlxOxgiDlo6tyNJf4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=WbmkVL36VAZ1U6FiJTn8KxOwKTVyDYe3lLVba2DDEDashBMfUAaTjEl/W5wT97DOAjjONyw3+1tpK/GVk0yET7PjoroQq6hwJDrmU/genbvxoxrPMPEpJ7+bEcKjlc9lhoqgcGUEKv+3dBx2fAD6ULntJSIyF52sOV2VzOyptMY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lj1-x22b.google.com with SMTP id 38308e7fff4ca-2ebd421a931so19379571fa.1 for ; Mon, 10 Jun 2024 02:08:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1718010505; x=1718615305; 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=hYbZDKPqztbb4dRIUlszLFBl5j8GnKc2ezeoQRTS28g=; b=fbsF2zL4SHi80C2klEmpL/R+ZaRwlnrLwDjDDnhoMm6Sao7WuMS3Kb/+KFDY+Eosvl xOgLblCZmWyq9Sl6WQnLigA0klQMlkrrk3C/MidLmvumAg5hmEdNA7Fr0Cj4IPJChUmH ZnbDJrw3MALQ3aCJuJEo/R3DxVAT93JwpStFRJWSVO2PhBZCXfDtY519JTJ2SVtM4RlH 7dUWSUE4cxKWPzrh28kAkZAH3yUNkAUcmDy5NPrVainr16hjxlHm+zXG8I0otxOPiU9t jC2j42GSrCSSpNjwCp+RrfM+zxEHetAw5sMC/FdFdmbga8uYMZjOlHi7+2hW5RrHy64n 07pQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718010505; x=1718615305; 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=hYbZDKPqztbb4dRIUlszLFBl5j8GnKc2ezeoQRTS28g=; b=UM9znkAmqoEv27khn99gieMk+zlB3oS+yVjCW2MIWIvQVP8B1C4QvgS1ktcPqTIW+k +QHFW+yiNc5/SuTSg423m/sjZyzkNIw1+w2tObkgvBFKgq2QTUYc4vP4pHq/KEKRF3Zj PLz+wtcp+WHL2KO0R/PMZXq5i89L0zJASqY2h1r6VzEARRpyg7EiHOvalhCRnDZyfuji fPUTmqH4XXTeOdq+iD3NTE+lN2Q8QxhSRHC+SV1BTogJrBSVX9gIAqI/zu4MFrWk7ECh UlyD1nQ9NLVrQYCPmjFadQJhWNl5abMpFw0ZQWODSETCCp4oMqFgun0Fop6qh+O430N5 yhrA== X-Gm-Message-State: AOJu0YxN/hwSEN1m1RmS3t7nmEl4j716XslmJcCOoDTTnSxcZQuu3cJ+ +q/gN0HFBusWOYEk6mJOnK3FHJ79EX1QwVZwJD3RPdTT2IwEW7tBFFf/DUWwlwNDABT5P/0HPlg = X-Google-Smtp-Source: AGHT+IHyojfx2Kpq+6hk9CPLvAh8svHTBcw8DbwNwNivRg63JSGRt24317wwEMZJb8jwNfpSmOC97g== X-Received: by 2002:a2e:a7c4:0:b0:2eb:eb23:6cda with SMTP id 38308e7fff4ca-2ebeb2371ebmr6894811fa.49.1718010505379; Mon, 10 Jun 2024 02:08:25 -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 ffacd0b85a97d-35f0f551c20sm6454970f8f.69.2024.06.10.02.08.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 02:08:24 -0700 (PDT) From: =?UTF-8?q?Marc=20Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED 30/30] ada: Add support for No_Implicit_Conditionals to nonbinary modular types Date: Mon, 10 Jun 2024 11:07:46 +0200 Message-ID: <20240610090747.1557638-30-poulhies@adacore.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240610090747.1557638-1-poulhies@adacore.com> References: <20240610090747.1557638-1-poulhies@adacore.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.7 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 List-Id: From: Eric Botcazou The expansion of additive operations for nonbinary modular types implemented in the front-end and its counterpart in code generators may create branches, which is not allowed when restriction No_Implicit_Conditionals is in effect. This changes it to use an explicit Mod operation when the restriction is in effect, which is assumed not to create such branches. gcc/ada/ * exp_ch4.adb (Expand_Nonbinary_Modular_Op): Create an explicit Mod for additive operations if No_Implicit_Conditionals is in effect. (Expand_Modular_Addition): Likewise. (Expand_Modular_Subtraction): Likewise. (Expand_Modular_Op): Always use an unsigned type obtained by calling Small_Integer_Type_For on the required size. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/exp_ch4.adb | 132 ++++++++++++++++++++++++++------------------ 1 file changed, 77 insertions(+), 55 deletions(-) diff --git a/gcc/ada/exp_ch4.adb b/gcc/ada/exp_ch4.adb index 95b7765b173..bf90b46249a 100644 --- a/gcc/ada/exp_ch4.adb +++ b/gcc/ada/exp_ch4.adb @@ -139,9 +139,10 @@ package body Exp_Ch4 is -- case of array type arguments. procedure Expand_Nonbinary_Modular_Op (N : Node_Id); - -- When generating C code, convert nonbinary modular arithmetic operations - -- into code that relies on the front-end expansion of operator Mod. No - -- expansion is performed if N is not a nonbinary modular operand. + -- When generating C code or if restriction No_Implicit_Conditionals is in + -- effect, convert most nonbinary modular arithmetic operations into code + -- that relies on the expansion of an explicit Mod operator. No expansion + -- is performed if N is not a nonbinary modular operation. procedure Expand_Short_Circuit_Operator (N : Node_Id); -- Common expansion processing for short-circuit boolean operators @@ -3899,10 +3900,13 @@ package body Exp_Ch4 is procedure Expand_Modular_Addition is begin - -- If this is not the addition of a constant then compute it using - -- the general rule: (lhs + rhs) mod Modulus + -- If this is not the addition of a constant or else restriction + -- No_Implicit_Conditionals is in effect, then compute it using + -- the general rule: (lhs + rhs) mod Modulus. - if Nkind (Right_Opnd (N)) /= N_Integer_Literal then + if Nkind (Right_Opnd (N)) /= N_Integer_Literal + or else Restriction_Active (No_Implicit_Conditionals) + then Expand_Modular_Op; -- If this is an addition of a constant, convert it to a subtraction @@ -3921,6 +3925,7 @@ package body Exp_Ch4 is Cond_Expr : Node_Id; Then_Expr : Node_Id; Else_Expr : Node_Id; + begin -- To prevent spurious visibility issues, convert all -- operands to Standard.Unsigned. @@ -3966,12 +3971,12 @@ package body Exp_Ch4 is -- We will convert to another type (not a nonbinary-modulus modular -- type), evaluate the op in that representation, reduce the result, -- and convert back to the original type. This means that the - -- backend does not have to deal with nonbinary-modulus ops. - - Op_Expr : constant Node_Id := New_Op_Node (Nkind (N), Loc); - Mod_Expr : Node_Id; + -- back end does not have to deal with nonbinary-modulus ops. + Mod_Expr : Node_Id; + Op_Expr : Node_Id; Target_Type : Entity_Id; + begin -- Select a target type that is large enough to avoid spurious -- intermediate overflow on pre-reduction computation (for @@ -3979,22 +3984,15 @@ package body Exp_Ch4 is declare Required_Size : Uint := RM_Size (Etype (N)); - Use_Unsigned : Boolean := True; + begin case Nkind (N) is - when N_Op_Add => + when N_Op_Add | N_Op_Subtract => -- For example, if modulus is 255 then RM_Size will be 8 -- and the range of possible values (before reduction) will -- be 0 .. 508; that range requires 9 bits. Required_Size := Required_Size + 1; - when N_Op_Subtract => - -- For example, if modulus is 255 then RM_Size will be 8 - -- and the range of possible values (before reduction) will - -- be -254 .. 254; that range requires 9 bits, signed. - Use_Unsigned := False; - Required_Size := Required_Size + 1; - when N_Op_Multiply => -- For example, if modulus is 255 then RM_Size will be 8 -- and the range of possible values (before reduction) will @@ -4005,37 +4003,15 @@ package body Exp_Ch4 is null; end case; - if Use_Unsigned then - if Required_Size <= Standard_Short_Short_Integer_Size then - Target_Type := Standard_Short_Short_Unsigned; - elsif Required_Size <= Standard_Short_Integer_Size then - Target_Type := Standard_Short_Unsigned; - elsif Required_Size <= Standard_Integer_Size then - Target_Type := Standard_Unsigned; - else - pragma Assert (Required_Size <= 64); - Target_Type := Standard_Unsigned_64; - end if; - elsif Required_Size <= 8 then - Target_Type := Standard_Integer_8; - elsif Required_Size <= 16 then - Target_Type := Standard_Integer_16; - elsif Required_Size <= 32 then - Target_Type := Standard_Integer_32; - else - pragma Assert (Required_Size <= 64); - Target_Type := Standard_Integer_64; - end if; - + Target_Type := Small_Integer_Type_For (Required_Size, Uns => True); pragma Assert (Present (Target_Type)); end; + Op_Expr := New_Op_Node (Nkind (N), Loc); Set_Left_Opnd (Op_Expr, - Unchecked_Convert_To (Target_Type, - New_Copy_Tree (Left_Opnd (N)))); + Unchecked_Convert_To (Target_Type, New_Copy_Tree (Left_Opnd (N)))); Set_Right_Opnd (Op_Expr, - Unchecked_Convert_To (Target_Type, - New_Copy_Tree (Right_Opnd (N)))); + Unchecked_Convert_To (Target_Type, New_Copy_Tree (Right_Opnd (N)))); -- ??? Why do this stuff for some ops and not others? if Nkind (N) not in N_Op_And | N_Op_Or | N_Op_Xor then @@ -4064,13 +4040,24 @@ package body Exp_Ch4 is Force_Evaluation (Op_Expr, Mode => Strict); end if; + -- Unconditionally add the modulus to the result for a subtraction, + -- this gets rid of all its peculiarities by cancelling out the + -- addition of the binary modulus in the case where the subtraction + -- wraps around in Target_Type. + + if Nkind (N) = N_Op_Subtract then + Op_Expr := + Make_Op_Add (Loc, + Left_Opnd => Op_Expr, + Right_Opnd => Make_Integer_Literal (Loc, Modulus (Typ))); + end if; + Mod_Expr := Make_Op_Mod (Loc, Left_Opnd => Op_Expr, Right_Opnd => Make_Integer_Literal (Loc, Modulus (Typ))); - Rewrite (N, - Unchecked_Convert_To (Typ, Mod_Expr)); + Rewrite (N, Unchecked_Convert_To (Typ, Mod_Expr)); end Expand_Modular_Op; -------------------------------- @@ -4079,10 +4066,13 @@ package body Exp_Ch4 is procedure Expand_Modular_Subtraction is begin - -- If this is not the addition of a constant then compute it using - -- the general rule: (lhs + rhs) mod Modulus + -- If this is not the addition of a constant or else restriction + -- No_Implicit_Conditionals is in effect, then compute it using + -- the general rule: (lhs - rhs) mod Modulus. - if Nkind (Right_Opnd (N)) /= N_Integer_Literal then + if Nkind (Right_Opnd (N)) /= N_Integer_Literal + or else Restriction_Active (No_Implicit_Conditionals) + then Expand_Modular_Op; -- If this is an addition of a constant, convert it to a subtraction @@ -4101,6 +4091,7 @@ package body Exp_Ch4 is Cond_Expr : Node_Id; Then_Expr : Node_Id; Else_Expr : Node_Id; + begin Cond_Expr := Make_Op_Lt (Loc, @@ -4139,23 +4130,46 @@ package body Exp_Ch4 is -- Start of processing for Expand_Nonbinary_Modular_Op begin - -- No action needed if front-end expansion is not required or if we - -- have a binary modular operand. + -- No action needed if we have a binary modular operand - if not Expand_Nonbinary_Modular_Ops - or else not Non_Binary_Modulus (Typ) - then + if not Non_Binary_Modulus (Typ) then return; end if; case Nkind (N) is when N_Op_Add => + -- No action needed if front-end expansion is not required and + -- restriction No_Implicit_Conditionals is not in effect. + + if not Expand_Nonbinary_Modular_Ops + and then not Restriction_Active (No_Implicit_Conditionals) + then + return; + end if; + Expand_Modular_Addition; when N_Op_Subtract => + -- No action needed if front-end expansion is not required and + -- restriction No_Implicit_Conditionals is not in effect. + + if not Expand_Nonbinary_Modular_Ops + and then not Restriction_Active (No_Implicit_Conditionals) + then + return; + end if; + Expand_Modular_Subtraction; when N_Op_Minus => + -- No action needed if front-end expansion is not required and + -- restriction No_Implicit_Conditionals is not in effect. + + if not Expand_Nonbinary_Modular_Ops + and then not Restriction_Active (No_Implicit_Conditionals) + then + return; + end if; -- Expand -expr into (0 - expr) @@ -4166,6 +4180,14 @@ package body Exp_Ch4 is Analyze_And_Resolve (N, Typ); when others => + -- No action needed only if front-end expansion is not required + -- because we assume that logical and multiplicative operations + -- do not involve implicit conditionals. + + if not Expand_Nonbinary_Modular_Ops then + return; + end if; + Expand_Modular_Op; end case; -- 2.45.1