From patchwork Wed Jun 17 08:16:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre-Marie de Rodat X-Patchwork-Id: 1311022 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=adacore.com Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49myc02Q3Wz9sSS for ; Wed, 17 Jun 2020 18:17:36 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A7D6A38930DF; Wed, 17 Jun 2020 08:16:23 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from rock.gnat.com (rock.gnat.com [IPv6:2620:20:4000:0:a9e:1ff:fe9b:1d1]) by sourceware.org (Postfix) with ESMTP id ABE333890409 for ; Wed, 17 Jun 2020 08:16:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org ABE333890409 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=derodat@adacore.com Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 405F856005; Wed, 17 Jun 2020 04:16:11 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id jmzy3onPg3B2; Wed, 17 Jun 2020 04:16:11 -0400 (EDT) Received: from tron.gnat.com (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) by rock.gnat.com (Postfix) with ESMTP id 291A7117EC7; Wed, 17 Jun 2020 04:16:11 -0400 (EDT) Received: by tron.gnat.com (Postfix, from userid 4862) id 27031A9; Wed, 17 Jun 2020 04:16:11 -0400 (EDT) Date: Wed, 17 Jun 2020 04:16:11 -0400 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Subject: [Ada] Do not generate useless length check for array initialization Message-ID: <20200617081611.GA55414@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00, JMQ_SPF_NEUTRAL, KAM_ASCII_DIVIDERS, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no 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: , Cc: Eric Botcazou Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" This prevents the compiler from generating a length check that can never fail for the initialization of an array whose nominal subtype is unconstrained and which is subject to an address clause. Tested on x86_64-pc-linux-gnu, committed on trunk 2020-06-17 Eric Botcazou gcc/ada/ * checks.ads (Apply_Length_Check_On_Assignment): Declare. * checks.adb (Apply_Length_Check_On_Assignment): New procedure to apply a length check to an expression in an assignment. * exp_ch5.adb (Expand_Assign_Array): Call it instead of calling Apply_Length_Check to generate a length check. * sem_ch5.adb (Analyze_Assignment): Likewise. --- gcc/ada/checks.adb +++ gcc/ada/checks.adb @@ -2220,6 +2220,34 @@ package body Checks is (Expr, Target_Typ, Source_Typ, Do_Static => False); end Apply_Length_Check; + -------------------------------------- + -- Apply_Length_Check_On_Assignment -- + -------------------------------------- + + procedure Apply_Length_Check_On_Assignment + (Expr : Node_Id; + Target_Typ : Entity_Id; + Target : Node_Id; + Source_Typ : Entity_Id := Empty) + is + Assign : constant Node_Id := Parent (Target); + + begin + -- No check is needed for the initialization of an object whose + -- nominal subtype is unconstrained. + + if Is_Constr_Subt_For_U_Nominal (Target_Typ) + and then Nkind (Parent (Assign)) = N_Freeze_Entity + and then Is_Entity_Name (Target) + and then Entity (Target) = Entity (Parent (Assign)) + then + return; + end if; + + Apply_Selected_Length_Checks + (Expr, Target_Typ, Source_Typ, Do_Static => False); + end Apply_Length_Check_On_Assignment; + ------------------------------------- -- Apply_Parameter_Aliasing_Checks -- ------------------------------------- --- gcc/ada/checks.ads +++ gcc/ada/checks.ads @@ -569,6 +569,15 @@ package Checks is -- processes it as described above for consistency with the other routines -- in this section. + procedure Apply_Length_Check_On_Assignment + (Expr : Node_Id; + Target_Typ : Entity_Id; + Target : Node_Id; + Source_Typ : Entity_Id := Empty); + -- Similar to Apply_Length_Check, but takes the target of an assignment for + -- which the check is to be done. Used to filter out specific cases where + -- the check is superfluous. + procedure Apply_Range_Check (Expr : Node_Id; Target_Typ : Entity_Id; --- gcc/ada/exp_ch5.adb +++ gcc/ada/exp_ch5.adb @@ -441,7 +441,7 @@ package body Exp_Ch5 is -- respect to the right-hand side as given, not a possible underlying -- renamed object, since this would generate incorrect extra checks. - Apply_Length_Check (Rhs, L_Type); + Apply_Length_Check_On_Assignment (Rhs, L_Type, Lhs); -- We start by assuming that the move can be done in either direction, -- i.e. that the two sides are completely disjoint. --- gcc/ada/sem_ch5.adb +++ gcc/ada/sem_ch5.adb @@ -995,7 +995,7 @@ package body Sem_Ch5 is and then (Nkind (Rhs) /= N_Function_Call or else Nkind (N) /= N_Block_Statement) then - -- Assignment verifies that the length of the Lsh and Rhs are equal, + -- Assignment verifies that the length of the Lhs and Rhs are equal, -- but of course the indexes do not have to match. If the right-hand -- side is a type conversion to an unconstrained type, a length check -- is performed on the expression itself during expansion. In rare @@ -1003,7 +1003,7 @@ package body Sem_Ch5 is -- with a different representation, triggering incorrect code in the -- back end. - Apply_Length_Check (Rhs, Etype (Lhs)); + Apply_Length_Check_On_Assignment (Rhs, Etype (Lhs), Lhs); else -- Discriminant checks are applied in the course of expansion