From patchwork Mon Jun 21 15:24:25 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 56325 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 5FB08B6F16 for ; Tue, 22 Jun 2010 01:24:39 +1000 (EST) Received: (qmail 29796 invoked by alias); 21 Jun 2010 15:24:35 -0000 Received: (qmail 29777 invoked by uid 22791); 21 Jun 2010 15:24:33 -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; Mon, 21 Jun 2010 15:24:26 +0000 Received: from localhost (localhost [127.0.0.1]) by filtered-smtp.eu.adacore.com (Postfix) with ESMTP id D86FECB029A; Mon, 21 Jun 2010 17:24:25 +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 XbUQc5sPVwFO; Mon, 21 Jun 2010 17:24:25 +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 C606ACB0299; Mon, 21 Jun 2010 17:24:25 +0200 (CEST) Received: by saumur.act-europe.fr (Postfix, from userid 525) id B030AD9A01; Mon, 21 Jun 2010 17:24:25 +0200 (CEST) Date: Mon, 21 Jun 2010 17:24:25 +0200 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Robert Dewar Subject: [Ada] Fold conditional expressions if condition known at compile time Message-ID: <20100621152425.GA19413@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 In Sem_Elab we fold static conditional expressions, but there are other cases that can be folded even if they are not static, namely any case in which the value of the condition is known at compile time. This patch adds that capability. The following program: function foldifexpr (x : integer) return integer is begin return (if x > x then 0 else x); end; Generates the following compiled with -gnatG -gnatX Source recreated from tree for foldifexpr (body) function foldifexpr (x : integer) return integer is begin return x; end foldifexpr; Tested on x86_64-pc-linux-gnu, committed on trunk 2010-06-21 Robert Dewar * exp_ch4.adb (Expand_N_Conditional_Expression): Fold if condition known at compile time. Index: exp_ch4.adb =================================================================== --- exp_ch4.adb (revision 161074) +++ exp_ch4.adb (working copy) @@ -2826,9 +2826,9 @@ package body Exp_Ch4 is Insert_Actions (Cnode, Actions, Suppress => All_Checks); - -- Now we construct an array object with appropriate bounds - -- The target is marked as internal, to prevent useless initialization - -- when Initialize_Scalars is enabled. + -- Now we construct an array object with appropriate bounds. We mark + -- the target as internal to prevent useless initialization when + -- Initialize_Scalars is enabled. Ent := Make_Temporary (Loc, 'S'); Set_Is_Internal (Ent); @@ -4025,13 +4025,44 @@ package body Exp_Ch4 is Elsex : constant Node_Id := Next (Thenx); Typ : constant Entity_Id := Etype (N); - Cnn : Entity_Id; - Decl : Node_Id; - New_If : Node_Id; - New_N : Node_Id; - P_Decl : Node_Id; + Cnn : Entity_Id; + Decl : Node_Id; + New_If : Node_Id; + New_N : Node_Id; + P_Decl : Node_Id; + Expr : Node_Id; + Actions : List_Id; begin + -- Fold at compile time if condition known. We have already folded + -- static conditional expressions, but it is possible to fold any + -- case in which the condition is known at compile time, even though + -- the result is non-static. + + -- Note that we don't do the fold of such cases in Sem_Elab because + -- it can cause infinite loops with the expander adding a conditional + -- expression, and Sem_Elab circuitry removing it repeatedly. + + if Compile_Time_Known_Value (Cond) then + if Is_True (Expr_Value (Cond)) then + Expr := Thenx; + Actions := Then_Actions (N); + else + Expr := Elsex; + Actions := Else_Actions (N); + end if; + + Remove (Expr); + Insert_Actions (N, Actions); + Rewrite (N, Relocate_Node (Expr)); + + -- Note that the result is never static (legitimate cases of static + -- conditional expressions were folded in Sem_Eval). + + Set_Is_Static_Expression (N, False); + return; + end if; + -- If the type is limited or unconstrained, we expand as follows to -- avoid any possibility of improper copies.