Hi,

this patchlet makes it possible to get rid of useless EH cleanups generated at 
-O0 in Ada for simple constructs involving VLAs:

  declare
    S : String (1 .. N);
  begin
    ...
  end;

by duplicating finally blocks that contain only a stack restore.  Then the EH 
optimization machinery (which is also run at -O0) can remove the cleanups.

Tested on x86_64-suse-linux, OK for the mainline?


2015-09-14  Eric Botcazou  <ebotca...@adacore.com>

        * tree-eh.c (lower_try_finally_dup_block): Clear location information
        on stack restore statements.
        (decide_copy_try_finally): Do not consider a stack restore statement as
        coming from sources.


2015-09-14  Eric Botcazou  <ebotca...@adacore.com>

        * gnat.dg/array24.adb: New test.

-- 
Eric Botcazou
Index: tree-eh.c
===================================================================
--- tree-eh.c	(revision 227729)
+++ tree-eh.c	(working copy)
@@ -915,7 +915,12 @@ lower_try_finally_dup_block (gimple_seq
   for (gsi = gsi_start (new_seq); !gsi_end_p (gsi); gsi_next (&gsi))
     {
       gimple stmt = gsi_stmt (gsi);
-      if (LOCATION_LOCUS (gimple_location (stmt)) == UNKNOWN_LOCATION)
+      /* We duplicate __builtin_stack_restore at -O0 in the hope of eliminating
+	 it on the EH paths.  When it is not eliminated, make it transparent in
+	 the debug info.  */
+      if (gimple_call_builtin_p (stmt, BUILT_IN_STACK_RESTORE))
+	gimple_set_location (stmt, UNKNOWN_LOCATION);
+      else if (LOCATION_LOCUS (gimple_location (stmt)) == UNKNOWN_LOCATION)
 	{
 	  tree block = gimple_block (stmt);
 	  gimple_set_location (stmt, loc);
@@ -1604,8 +1609,12 @@ decide_copy_try_finally (int ndests, boo
 
       for (gsi = gsi_start (finally); !gsi_end_p (gsi); gsi_next (&gsi))
 	{
+	  /* Duplicate __builtin_stack_restore in the hope of eliminating it
+	     on the EH paths and, consequently, useless cleanups.  */
 	  gimple stmt = gsi_stmt (gsi);
-	  if (!is_gimple_debug (stmt) && !gimple_clobber_p (stmt))
+	  if (!is_gimple_debug (stmt)
+	      && !gimple_clobber_p (stmt)
+	      && !gimple_call_builtin_p (stmt, BUILT_IN_STACK_RESTORE))
 	    return false;
 	}
       return true;
-- { dg-do compile }
-- { dg-options "-fdump-tree-optimized" }

procedure Array24 (N : Natural) is
  S : String (1 .. N);
  pragma Volatile (S);
begin
  S := (others => '0');
end;

-- { dg-final { scan-tree-dump-not "builtin_unwind_resume" "optimized"  } }

Reply via email to