From patchwork Fri Dec 15 11:16:57 2017 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: 849094 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-469326-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="E2PIj0tT"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yynvl6lVWz9sNw for ; Fri, 15 Dec 2017 22:17:23 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=DLP1axQk58UgD9eDVqbr23O1oGQcfAtLG5bP6fEAB6NoWNLmuB 15wtRScEZOtlqllFk7Z1vEFtdyKNKDMiO48awWFy6tFPjhwzpFeyrqIMx7WwtERa vvDWbUGq4QvniLoNNhrvxp+x1HCHIbdfN9JzA7M0nMcIbWOh2gy/1L0Ko= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=wVw9IoZESHuNgS9a70Q40U101MA=; b=E2PIj0tTMYfeMqHrQaNM 3tTV4Sy+KH1GElyxAlb2125M91dnSC06PuWuxmV6kynnI2xtyO5O6Xr/zI0wssG8 SjQhiBbIWH5FgpTeMlcDt/gprPcOXvdEEuCkA3ynTSpTFIWbydL2vKMB60XFl3/s oqKekdCn+fKkevgI4Jha2zY= Received: (qmail 47957 invoked by alias); 15 Dec 2017 11:17:01 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 47864 invoked by uid 89); 15 Dec 2017 11:17:00 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 15 Dec 2017 11:16:58 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 83B5A116ED1; Fri, 15 Dec 2017 06:16:57 -0500 (EST) 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 XJUUhFGslTsR; Fri, 15 Dec 2017 06:16:57 -0500 (EST) Received: from tron.gnat.com (tron.gnat.com [205.232.38.10]) by rock.gnat.com (Postfix) with ESMTP id 7328D116ECD; Fri, 15 Dec 2017 06:16:57 -0500 (EST) Received: by tron.gnat.com (Postfix, from userid 4862) id 71F2C508; Fri, 15 Dec 2017 06:16:57 -0500 (EST) Date: Fri, 15 Dec 2017 06:16:57 -0500 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Cc: Ed Schonberg Subject: [Ada] Optimizing allocators for arrays with non-static upper bound Message-ID: <20171215111657.GA47534@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-IsSubscribed: yes This patch extends the optimization of allocators for arrays of non-controlled components, when the qualified expression for the aggregate has an unconstrained type and the upper bound of the aggregte is non-static. In this case it is safe to build the array in the allocated object, instead of first creating a temporary for the aggregate, then allocating the object, and then assigning the temporary to the object, as mandated by the dynamic semantics of initialized allocators. This optimization is particularly useful when the size of the aggregate may be too large to be built on the stack, Executing the following: gnatmake -q foo ./foo must yield: 10000000 --- with Text_IO; use Text_IO; procedure Foo is type Record_Type is record I : Integer; end record; type Array_Type is array (Positive range <>) of Record_Type; type Array_Access is access all Array_Type; function Get_Last return Integer is begin return 10_000_000; end Get_Last; A : Array_Access := new Array_Type'(1 .. Get_Last => (I => 0)); begin Put_Line (Integer'Image (A'Length)); end Foo; Tested on x86_64-pc-linux-gnu, committed on trunk 2017-12-15 Ed Schonberg * exp_aggr.adb (In_Place_Assign_OK): Extend the predicate to recognize an array aggregate in an allocator, when the designated type is unconstrained and the upper bound of the aggregate belongs to the base type of the index. Index: exp_aggr.adb =================================================================== --- exp_aggr.adb (revision 255678) +++ exp_aggr.adb (working copy) @@ -5537,13 +5537,29 @@ Get_Index_Bounds (Obj_In, Obj_Lo, Obj_Hi); if not Compile_Time_Known_Value (Aggr_Lo) - or else not Compile_Time_Known_Value (Aggr_Hi) or else not Compile_Time_Known_Value (Obj_Lo) or else not Compile_Time_Known_Value (Obj_Hi) or else Expr_Value (Aggr_Lo) /= Expr_Value (Obj_Lo) - or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi) then return False; + + -- For an assignment statement we require static matching + -- of bounds. Ditto for an allocator whose qualified + -- expression is a constrained type. If the expression in + -- the allocator is an unconstrained array, we accept an + -- upper bound that is not static, to allow for non-static + -- expressions of the base type. Clearly there are further + -- possibilities (with diminishing returns) for safely + -- building arrays in place here. + + elsif Nkind (Parent (N)) = N_Assignment_Statement + or else Is_Constrained (Etype (Parent (N))) + then + if not Compile_Time_Known_Value (Aggr_Hi) + or else Expr_Value (Aggr_Hi) /= Expr_Value (Obj_Hi) + then + return False; + end if; end if; Next_Index (Aggr_In);