From patchwork Tue Jun 22 06:53:26 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [Ada] Improve handling of folding of conditional expressions Date: Mon, 21 Jun 2010 20:53:26 -0000 From: Arnaud Charlet X-Patchwork-Id: 56400 Message-Id: <20100622065326.GA3412@adacore.com> To: gcc-patches@gcc.gnu.org Cc: Robert Dewar 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.