From patchwork Tue Jun 22 06:53:26 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 56400 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 7AB00B6EE8 for ; Tue, 22 Jun 2010 16:53:35 +1000 (EST) Received: (qmail 31763 invoked by alias); 22 Jun 2010 06:53:32 -0000 Received: (qmail 31751 invoked by uid 22791); 22 Jun 2010 06:53:29 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL, BAYES_00, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mel.act-europe.fr (HELO mel.act-europe.fr) (212.99.106.210) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 22 Jun 2010 06:53:25 +0000 Received: from localhost (localhost [127.0.0.1]) by filtered-smtp.eu.adacore.com (Postfix) with ESMTP id 49CC1CB0231; Tue, 22 Jun 2010 08:53:27 +0200 (CEST) Received: from mel.act-europe.fr ([127.0.0.1]) by localhost (smtp.eu.adacore.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id yupD3yTtMDUD; Tue, 22 Jun 2010 08:53:27 +0200 (CEST) Received: from saumur.act-europe.fr (saumur.act-europe.fr [10.10.0.183]) by mel.act-europe.fr (Postfix) with ESMTP id 3365ECB022F; Tue, 22 Jun 2010 08:53:27 +0200 (CEST) Received: by saumur.act-europe.fr (Postfix, from userid 525) id DC8A3D9AB0; Tue, 22 Jun 2010 08:53:26 +0200 (CEST) Date: Tue, 22 Jun 2010 08:53:26 +0200 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Robert Dewar Subject: [Ada] Improve handling of folding of conditional expressions Message-ID: <20100622065326.GA3412@adacore.com> Mime-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.9i X-IsSubscribed: yes 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 This patch uses Expression_With_Actions to generate a much cleaner sequence when actions are present. The following test program: function foldifexpr2 (x : integer; s : string) return boolean is begin return (if x > x then False else s (x .. x+1) = s (x+1 .. x+2)); end; Generates the following output compiled with -gnatX -gnatp -gnatG Source recreated from tree for foldifexpr2 (body) function foldifexpr2 (x : integer; s : string) return boolean is subtype foldifexpr2__S1b is string (s'first(1) .. s'last(1)); begin return do [subtype foldifexpr2__T3b is integer range x .. x + 1] [subtype foldifexpr2__T4b is string (foldifexpr2__T3b)] reference foldifexpr2__T4b [subtype foldifexpr2__T5b is integer range x + 1 .. x + 2] [subtype foldifexpr2__T6b is string (foldifexpr2__T5b)] reference foldifexpr2__T6b in s (x .. x + 1) = s (x + 1 .. x + 2) end ; end foldifexpr2; Tested on x86_64-pc-linux-gnu, committed on trunk 2010-06-22 Robert Dewar * exp_ch4.adb (Expand_N_Conditional_Expression): Use Expression_With_Actions to clean up the code generated when folding constant expressions. Index: exp_ch4.adb =================================================================== --- exp_ch4.adb (revision 161088) +++ exp_ch4.adb (working copy) @@ -4053,8 +4053,25 @@ package body Exp_Ch4 is end if; Remove (Expr); - Insert_Actions (N, Actions); - Rewrite (N, Relocate_Node (Expr)); + + if Present (Actions) then + + -- If we are not allowed to use Expression_With_Actions, just + -- skip the optimization, it is not critical for correctness. + + if not Use_Expression_With_Actions then + goto Skip_Optimization; + end if; + + Rewrite (N, + Make_Expression_With_Actions (Loc, + Expression => Relocate_Node (Expr), + Actions => Actions)); + Analyze_And_Resolve (N, Typ); + + else + Rewrite (N, Relocate_Node (Expr)); + end if; -- Note that the result is never static (legitimate cases of static -- conditional expressions were folded in Sem_Eval). @@ -4063,6 +4080,8 @@ package body Exp_Ch4 is return; end if; + <> + -- If the type is limited or unconstrained, we expand as follows to -- avoid any possibility of improper copies.