diff mbox

[Ada] Improve handling of folding of conditional expressions

Message ID 20100622065326.GA3412@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet June 22, 2010, 6:53 a.m. UTC
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  <dewar@adacore.com>

	* exp_ch4.adb (Expand_N_Conditional_Expression): Use
	Expression_With_Actions to clean up the code generated when folding
	constant expressions.
diff mbox

Patch

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;
 
+      <<Skip_Optimization>>
+
       --  If the type is limited or unconstrained, we expand as follows to
       --  avoid any possibility of improper copies.