From 937e8bf1a7131812beec9e174eea1a8d61273ac2 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Mon, 16 Dec 2013 14:48:41 -0500
Subject: [PATCH 32/89] Introduce gimple_try

gcc/
	* coretypes.h (gimple_try): New typedef.
	(const_gimple_try): New typedef.

	* gimple.h (gimple_statement_base::as_a_gimple_try): New.

	* gimple-low.c (gimple_try_catch_may_fallthru): Require a
	gimple_try rather than a plain gimple.
	(gimple_stmt_may_fallthru): Add checked cast to gimple_try.

	* gimple-pretty-print.c (dump_gimple_try): Require a gimple_try
	rather than a plain gimple.
	(pp_gimple_stmt_1): Add checked cast to gimple_try within
	GIMPLE_TRY case of switch statement.

	* tree-eh.c (finally_tree_node::parent): Strengthen field from
	gimple to gimple_try.
	(record_in_finally_tree): Require a gimple_try rather than a plain
	gimple.
	(collect_finally_tree): Likewise.
	(collect_finally_tree_1): Likewise.
	(struct leh_tf_state::try_finally_expr): Strengthen field from
	gimple to gimple_try.
	(struct leh_tf_state::top_p): Likewise.
	(lower_eh_must_not_throw): Require a gimple_try rather than a
	plain gimple.
	(frob_into_branch_around): Likewise.
	(lower_try_finally_dup_block): Strengthen local from gimple to
	gimple_try.
	(honor_protect_cleanup_actions): Split out uses of "x" into new
	locals "eh_mnt" and "try_stmt" with stronger types.
	(lower_try_finally): Require a gimple_try rather than a plain
	gimple.
	(lower_catch): Likewise.
	(lower_eh_filter): Likewise.
	(lower_eh_must_not_throw): Likewise.
	(lower_cleanup): Likewise.
	(lower_eh_constructs_2): Add checked cast to gimple_try within
	GIMPLE_TRY case of switch statement, introducing new local
	"try_stmt", using it for type-safety.
---
 gcc/coretypes.h           |  4 ++
 gcc/gimple-low.c          |  4 +-
 gcc/gimple-pretty-print.c |  4 +-
 gcc/gimple.h              |  6 +++
 gcc/tree-eh.c             | 94 +++++++++++++++++++++++++----------------------
 5 files changed, 64 insertions(+), 48 deletions(-)

diff --git a/gcc/coretypes.h b/gcc/coretypes.h
index b997a8d..6d5c363 100644
--- a/gcc/coretypes.h
+++ b/gcc/coretypes.h
@@ -141,6 +141,10 @@ struct gimple_statement_phi;
 typedef struct gimple_statement_phi *gimple_phi;
 typedef const struct gimple_statement_phi *const_gimple_phi;
 
+struct gimple_statement_try;
+typedef struct gimple_statement_try *gimple_try;
+typedef const struct gimple_statement_try *const_gimple_try;
+
 union section;
 typedef union section section;
 struct gcc_options;
diff --git a/gcc/gimple-low.c b/gcc/gimple-low.c
index 4056b3c..0efb844 100644
--- a/gcc/gimple-low.c
+++ b/gcc/gimple-low.c
@@ -501,7 +501,7 @@ lower_try_catch (gimple_stmt_iterator *gsi, struct lower_data *data)
    This is a subroutine of gimple_stmt_may_fallthru.  */
 
 static bool
-gimple_try_catch_may_fallthru (gimple stmt)
+gimple_try_catch_may_fallthru (gimple_try stmt)
 {
   gimple_stmt_iterator i;
 
@@ -587,7 +587,7 @@ gimple_stmt_may_fallthru (gimple stmt)
 
     case GIMPLE_TRY:
       if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
-        return gimple_try_catch_may_fallthru (stmt);
+        return gimple_try_catch_may_fallthru (stmt->as_a_gimple_try ());
 
       /* It must be a GIMPLE_TRY_FINALLY.  */
 
diff --git a/gcc/gimple-pretty-print.c b/gcc/gimple-pretty-print.c
index 28234cb..f2525f4 100644
--- a/gcc/gimple-pretty-print.c
+++ b/gcc/gimple-pretty-print.c
@@ -922,7 +922,7 @@ dump_gimple_bind (pretty_printer *buffer, gimple_bind gs, int spc, int flags)
    dumpfile.h).  */
 
 static void
-dump_gimple_try (pretty_printer *buffer, gimple gs, int spc, int flags)
+dump_gimple_try (pretty_printer *buffer, gimple_try gs, int spc, int flags)
 {
   if (flags & TDF_RAW)
     {
@@ -2134,7 +2134,7 @@ pp_gimple_stmt_1 (pretty_printer *buffer, gimple gs, int spc, int flags)
       break;
 
     case GIMPLE_TRY:
-      dump_gimple_try (buffer, gs, spc, flags);
+      dump_gimple_try (buffer, gs->as_a_gimple_try (), spc, flags);
       break;
 
     case GIMPLE_PHI:
diff --git a/gcc/gimple.h b/gcc/gimple.h
index 1ee15e9..931503c 100644
--- a/gcc/gimple.h
+++ b/gcc/gimple.h
@@ -330,6 +330,12 @@ public:
     return as_a <gimple_statement_phi> (this);
   }
 
+  inline gimple_try
+  as_a_gimple_try ()
+  {
+    return as_a <gimple_statement_try> (this);
+  }
+
   /* Dynamic casting methods, where the cast returns NULL if the
      stmt is not of the required kind.
 
diff --git a/gcc/tree-eh.c b/gcc/tree-eh.c
index ee810b0..ca8d4aa 100644
--- a/gcc/tree-eh.c
+++ b/gcc/tree-eh.c
@@ -199,7 +199,7 @@ struct finally_tree_node
      tree) leaves the TRY block, its necessary to record a tree in
      this field.  Thus a treemple is used. */
   treemple child;
-  gimple parent;
+  gimple_try parent;
 };
 
 /* Hashtable helpers.  */
@@ -228,7 +228,7 @@ finally_tree_hasher::equal (const value_type *v, const compare_type *c)
 static hash_table <finally_tree_hasher> finally_tree;
 
 static void
-record_in_finally_tree (treemple child, gimple parent)
+record_in_finally_tree (treemple child, gimple_try parent)
 {
   struct finally_tree_node *n;
   finally_tree_node **slot;
@@ -243,13 +243,13 @@ record_in_finally_tree (treemple child, gimple parent)
 }
 
 static void
-collect_finally_tree (gimple stmt, gimple region);
+collect_finally_tree (gimple stmt, gimple_try region);
 
 /* Go through the gimple sequence.  Works with collect_finally_tree to
    record all GIMPLE_LABEL and GIMPLE_TRY statements. */
 
 static void
-collect_finally_tree_1 (gimple_seq seq, gimple region)
+collect_finally_tree_1 (gimple_seq seq, gimple_try region)
 {
   gimple_stmt_iterator gsi;
 
@@ -258,7 +258,7 @@ collect_finally_tree_1 (gimple_seq seq, gimple region)
 }
 
 static void
-collect_finally_tree (gimple stmt, gimple region)
+collect_finally_tree (gimple stmt, gimple_try region)
 {
   treemple temp;
 
@@ -274,7 +274,8 @@ collect_finally_tree (gimple stmt, gimple region)
         {
           temp.g = stmt;
           record_in_finally_tree (temp, region);
-          collect_finally_tree_1 (gimple_try_eval (stmt), stmt);
+          collect_finally_tree_1 (gimple_try_eval (stmt),
+				  stmt->as_a_gimple_try ());
 	  collect_finally_tree_1 (gimple_try_cleanup (stmt), region);
         }
       else if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
@@ -391,8 +392,8 @@ struct leh_tf_state
      try_finally_expr is the original GIMPLE_TRY_FINALLY.  We need to retain
      this so that outside_finally_tree can reliably reference the tree used
      in the collect_finally_tree data structures.  */
-  gimple try_finally_expr;
-  gimple top_p;
+  gimple_try try_finally_expr;
+  gimple_try top_p;
 
   /* While lowering a top_p usually it is expanded into multiple statements,
      thus we need the following field to store them. */
@@ -432,7 +433,7 @@ struct leh_tf_state
   bool may_throw;
 };
 
-static gimple_seq lower_eh_must_not_throw (struct leh_state *, gimple);
+static gimple_seq lower_eh_must_not_throw (struct leh_state *, gimple_try);
 
 /* Search for STMT in the goto queue.  Return the replacement,
    or null if the statement isn't in the queue.  */
@@ -885,7 +886,7 @@ eh_region_may_contain_throw (eh_region r)
    an existing label that should be put at the exit, or NULL.  */
 
 static gimple_seq
-frob_into_branch_around (gimple tp, eh_region region, tree over)
+frob_into_branch_around (gimple_try tp, eh_region region, tree over)
 {
   gimple x;
   gimple_seq cleanup, result;
@@ -922,7 +923,7 @@ static gimple_seq
 lower_try_finally_dup_block (gimple_seq seq, struct leh_state *outer_state,
 			     location_t loc)
 {
-  gimple region = NULL;
+  gimple_try region = NULL;
   gimple_seq new_seq;
   gimple_stmt_iterator gsi;
 
@@ -1014,6 +1015,8 @@ honor_protect_cleanup_actions (struct leh_state *outer_state,
   bool finally_may_fallthru;
   gimple_seq finally;
   gimple x;
+  gimple_eh_must_not_throw eh_mnt;
+  gimple_try try_stmt;
   gimple_eh_else eh_else;
 
   /* First check for nothing to do.  */
@@ -1056,10 +1059,10 @@ honor_protect_cleanup_actions (struct leh_state *outer_state,
     }
 
   /* Wrap the block with protect_cleanup_actions as the action.  */
-  x = gimple_build_eh_must_not_throw (protect_cleanup_actions);
-  x = gimple_build_try (finally, gimple_seq_alloc_with_stmt (x),
-			GIMPLE_TRY_CATCH);
-  finally = lower_eh_must_not_throw (outer_state, x);
+  eh_mnt = gimple_build_eh_must_not_throw (protect_cleanup_actions);
+  try_stmt = gimple_build_try (finally, gimple_seq_alloc_with_stmt (eh_mnt),
+			       GIMPLE_TRY_CATCH);
+  finally = lower_eh_must_not_throw (outer_state, try_stmt);
 
   /* Drop all of this into the exception sequence.  */
   emit_post_landing_pad (&eh_seq, tf->region);
@@ -1667,7 +1670,7 @@ cleanup_is_dead_in (eh_region reg)
    arrange for the FINALLY block to be executed on all exits.  */
 
 static gimple_seq
-lower_try_finally (struct leh_state *state, gimple tp)
+lower_try_finally (struct leh_state *state, gimple_try tp)
 {
   struct leh_tf_state this_tf;
   struct leh_state this_state;
@@ -1774,7 +1777,7 @@ lower_try_finally (struct leh_state *state, gimple tp)
    exception region trees that records all the magic.  */
 
 static gimple_seq
-lower_catch (struct leh_state *state, gimple tp)
+lower_catch (struct leh_state *state, gimple_try tp)
 {
   eh_region try_region = NULL;
   struct leh_state this_state = *state;
@@ -1846,7 +1849,7 @@ lower_catch (struct leh_state *state, gimple tp)
    region trees that record all the magic.  */
 
 static gimple_seq
-lower_eh_filter (struct leh_state *state, gimple tp)
+lower_eh_filter (struct leh_state *state, gimple_try tp)
 {
   struct leh_state this_state = *state;
   eh_region this_region = NULL;
@@ -1891,7 +1894,7 @@ lower_eh_filter (struct leh_state *state, gimple tp)
    plus the exception region trees that record all the magic.  */
 
 static gimple_seq
-lower_eh_must_not_throw (struct leh_state *state, gimple tp)
+lower_eh_must_not_throw (struct leh_state *state, gimple_try tp)
 {
   struct leh_state this_state = *state;
 
@@ -1924,7 +1927,7 @@ lower_eh_must_not_throw (struct leh_state *state, gimple tp)
    except that we only execute the cleanup block for exception edges.  */
 
 static gimple_seq
-lower_cleanup (struct leh_state *state, gimple tp)
+lower_cleanup (struct leh_state *state, gimple_try tp)
 {
   struct leh_state this_state = *state;
   eh_region this_region = NULL;
@@ -2079,36 +2082,39 @@ lower_eh_constructs_2 (struct leh_state *state, gimple_stmt_iterator *gsi)
       break;
 
     case GIMPLE_TRY:
-      if (gimple_try_kind (stmt) == GIMPLE_TRY_FINALLY)
-	replace = lower_try_finally (state, stmt);
-      else
-	{
-	  x = gimple_seq_first_stmt (gimple_try_cleanup (stmt));
-	  if (!x)
-	    {
-	      replace = gimple_try_eval (stmt);
-	      lower_eh_constructs_1 (state, &replace);
-	    }
-	  else
-	    switch (gimple_code (x))
+      {
+	gimple_try try_stmt = stmt->as_a_gimple_try ();
+	if (gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
+	  replace = lower_try_finally (state, try_stmt);
+	else
+	  {
+	    x = gimple_seq_first_stmt (gimple_try_cleanup (try_stmt));
+	    if (!x)
 	      {
+		replace = gimple_try_eval (try_stmt);
+		lower_eh_constructs_1 (state, &replace);
+	      }
+	    else
+	      switch (gimple_code (x))
+		{
 		case GIMPLE_CATCH:
-		    replace = lower_catch (state, stmt);
-		    break;
+		  replace = lower_catch (state, try_stmt);
+		  break;
 		case GIMPLE_EH_FILTER:
-		    replace = lower_eh_filter (state, stmt);
-		    break;
+		  replace = lower_eh_filter (state, try_stmt);
+		  break;
 		case GIMPLE_EH_MUST_NOT_THROW:
-		    replace = lower_eh_must_not_throw (state, stmt);
-		    break;
+		  replace = lower_eh_must_not_throw (state, try_stmt);
+		  break;
 		case GIMPLE_EH_ELSE:
-		    /* This code is only valid with GIMPLE_TRY_FINALLY.  */
-		    gcc_unreachable ();
+		  /* This code is only valid with GIMPLE_TRY_FINALLY.  */
+		  gcc_unreachable ();
 		default:
-		    replace = lower_cleanup (state, stmt);
-		    break;
-	      }
-	}
+		  replace = lower_cleanup (state, try_stmt);
+		  break;
+		}
+	  }
+      }
 
       /* Remove the old stmt and insert the transformed sequence
 	 instead. */
-- 
1.8.5.3

