From 6584458441d7d93ab09f8cde0f210c0b32519784 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Mon, 16 Dec 2013 17:21:33 -0500
Subject: [PATCH 38/89] Introduce gimple_omp_for

gcc/
	* coretypes.h (gimple_omp_for): New.
	(const_gimple_omp_for): New.

	* gimple.h (gimple_statement_base::as_a_gimple_omp_for): New.
	(gimple_statement_base::dyn_cast_gimple_omp_for): New.
	(gimple_build_omp_for): Return a gimple_omp_for rather than a
	plain gimple.
	(gimple_omp_for_set_kind): Require a gimple_omp_for rather than a
	plain gimple.
	(gimple_omp_for_set_combined_p): Likewise.
	(gimple_omp_for_set_combined_into_p): Likewise.

	* gimple-pretty-print.c (dump_gimple_omp_for): Require a
	gimple_omp_for rather than a plain gimple.
	(pp_gimple_stmt_1): Add a checked cast to gimple_omp_for in
	GIMPLE_OMP_FOR case of switch statement.

	* gimple.c (gimple_build_omp_for): Return a gimple_omp_for rather
	than a plain gimple.
	(gimple_copy): Add a checked cast to gimple_omp_for and a new local.

	* gimplify.c (gimplify_omp_for): Strengthen local "gfor" from
	gimple to gimple_omp_for.

	* omp-low.c (omp_for_data::for_stmt): Strengthen field from gimple
	to gimple_omp_for.
	(extract_omp_for_data): Require a gimple_omp_for rather than a
	plain gimple.
	(workshare_safe_to_combine_p): Add a checked cast to
	gimple_omp_for.
	(get_ws_args_for): Convert check of code against GIMPLE_OMP_FOR
	with a dyn_cast_gimple_omp_for and a new local.
	(scan_omp_parallel): Add a checked cast to gimple_omp_for and a
	new local.
	(scan_omp_for): Require a gimple_omp_for rather than a plain
	gimple.
	(scan_omp_1_stmt): Add a checked cast to gimple_omp_for in
	GIMPLE_OMP_FOR case of switch statement.
	(expand_omp_for): Add a checked cast to gimple_omp_for.
	(lower_omp_for): Strengthen local "stmt" from gimple to
	gimple_omp_for.

	* tree-nested.c (walk_gimple_omp_for): Require a gimple_omp_for
	rather than a plain gimple.
	(convert_nonlocal_reference_stmt): Add a checked cast to
	gimple_omp_for in GIMPLE_OMP_FOR case of switch statement.
	(convert_local_reference_stmt): Likewise.

	* tree-parloops.c (create_parallel_loop): Strengthen local
	"for_stmt" from gimple to gimple_omp_for.
---
 gcc/coretypes.h           |  4 ++++
 gcc/gimple-pretty-print.c |  4 ++--
 gcc/gimple.c              |  8 +++-----
 gcc/gimple.h              | 22 +++++++++++++++-------
 gcc/gimplify.c            |  2 +-
 gcc/omp-low.c             | 26 +++++++++++++-------------
 gcc/tree-nested.c         |  8 +++++---
 gcc/tree-parloops.c       |  3 ++-
 8 files changed, 45 insertions(+), 32 deletions(-)

diff --git a/gcc/coretypes.h b/gcc/coretypes.h
index f527da6..0a797a3 100644
--- a/gcc/coretypes.h
+++ b/gcc/coretypes.h
@@ -163,6 +163,10 @@ struct gimple_statement_omp_critical;
 typedef struct gimple_statement_omp_critical *gimple_omp_critical;
 typedef const struct gimple_statement_omp_critical *const_gimple_omp_critical;
 
+struct gimple_statement_omp_for;
+typedef struct gimple_statement_omp_for *gimple_omp_for;
+typedef const struct gimple_statement_omp_for *const_gimple_omp_for;
+
 union section;
 typedef union section section;
 struct gcc_options;
diff --git a/gcc/gimple-pretty-print.c b/gcc/gimple-pretty-print.c
index 8248c67..1b70885 100644
--- a/gcc/gimple-pretty-print.c
+++ b/gcc/gimple-pretty-print.c
@@ -1107,7 +1107,7 @@ dump_gimple_debug (pretty_printer *buffer, gimple_debug gs, int spc, int flags)
 
 /* Dump a GIMPLE_OMP_FOR tuple on the pretty_printer BUFFER.  */
 static void
-dump_gimple_omp_for (pretty_printer *buffer, gimple gs, int spc, int flags)
+dump_gimple_omp_for (pretty_printer *buffer, gimple_omp_for gs, int spc, int flags)
 {
   size_t i;
 
@@ -2162,7 +2162,7 @@ pp_gimple_stmt_1 (pretty_printer *buffer, gimple gs, int spc, int flags)
       break;
 
     case GIMPLE_OMP_FOR:
-      dump_gimple_omp_for (buffer, gs, spc, flags);
+      dump_gimple_omp_for (buffer, gs->as_a_gimple_omp_for (), spc, flags);
       break;
 
     case GIMPLE_OMP_CONTINUE:
diff --git a/gcc/gimple.c b/gcc/gimple.c
index 3b40884..7f25207 100644
--- a/gcc/gimple.c
+++ b/gcc/gimple.c
@@ -836,12 +836,11 @@ gimple_build_omp_critical (gimple_seq body, tree name)
    COLLAPSE is the collapse count.
    PRE_BODY is the sequence of statements that are loop invariant.  */
 
-gimple
+gimple_omp_for
 gimple_build_omp_for (gimple_seq body, int kind, tree clauses, size_t collapse,
 		      gimple_seq pre_body)
 {
-  gimple_statement_omp_for *p =
-    as_a <gimple_statement_omp_for> (gimple_alloc (GIMPLE_OMP_FOR, 0));
+  gimple_omp_for p = gimple_alloc (GIMPLE_OMP_FOR, 0)->as_a_gimple_omp_for ();
   if (body)
     gimple_omp_set_body (p, body);
   gimple_omp_for_set_clauses (p, clauses);
@@ -1689,8 +1688,7 @@ gimple_copy (gimple stmt)
 	  t = unshare_expr (gimple_omp_for_clauses (stmt));
 	  gimple_omp_for_set_clauses (copy, t);
 	  {
-	    gimple_statement_omp_for *omp_for_copy =
-	      as_a <gimple_statement_omp_for> (copy);
+	    gimple_omp_for omp_for_copy = copy->as_a_gimple_omp_for ();
 	    omp_for_copy->iter =
 	      static_cast <struct gimple_omp_for_iter *> (
 		  ggc_internal_vec_alloc_stat (sizeof (struct gimple_omp_for_iter),
diff --git a/gcc/gimple.h b/gcc/gimple.h
index 525fb7b..d5e9af8 100644
--- a/gcc/gimple.h
+++ b/gcc/gimple.h
@@ -360,6 +360,12 @@ public:
     return as_a <gimple_statement_omp_critical> (this);
   }
 
+  inline gimple_omp_for
+  as_a_gimple_omp_for ()
+  {
+    return as_a <gimple_statement_omp_for> (this);
+  }
+
   /* Dynamic casting methods, where the cast returns NULL if the
      stmt is not of the required kind.
 
@@ -452,6 +458,11 @@ public:
     return dyn_cast <gimple_statement_omp_critical> (this);
   }
 
+  inline gimple_omp_for
+  dyn_cast_gimple_omp_for ()
+  {
+    return dyn_cast <gimple_statement_omp_for> (this);
+  }
 };
 
 
@@ -1595,7 +1606,7 @@ gimple_debug gimple_build_debug_source_bind_stat (tree, tree, gimple MEM_STAT_DE
 #define gimple_build_debug_source_bind(var,val,stmt)			\
   gimple_build_debug_source_bind_stat ((var), (val), (stmt) MEM_STAT_INFO)
 gimple_omp_critical gimple_build_omp_critical (gimple_seq, tree);
-gimple gimple_build_omp_for (gimple_seq, int, tree, size_t, gimple_seq);
+gimple_omp_for gimple_build_omp_for (gimple_seq, int, tree, size_t, gimple_seq);
 gimple gimple_build_omp_parallel (gimple_seq, tree, tree, tree);
 gimple gimple_build_omp_task (gimple_seq, tree, tree, tree, tree, tree, tree);
 gimple gimple_build_omp_section (gimple_seq);
@@ -4613,9 +4624,8 @@ gimple_omp_for_kind (const_gimple g)
 /* Set the OMP for kind.  */
 
 static inline void
-gimple_omp_for_set_kind (gimple g, int kind)
+gimple_omp_for_set_kind (gimple_omp_for g, int kind)
 {
-  GIMPLE_CHECK (g, GIMPLE_OMP_FOR);
   g->subcode = (g->subcode & ~GF_OMP_FOR_KIND_MASK)
 		      | (kind & GF_OMP_FOR_KIND_MASK);
 }
@@ -4636,9 +4646,8 @@ gimple_omp_for_combined_p (const_gimple g)
    value of COMBINED_P.  */
 
 static inline void
-gimple_omp_for_set_combined_p (gimple g, bool combined_p)
+gimple_omp_for_set_combined_p (gimple_omp_for g, bool combined_p)
 {
-  GIMPLE_CHECK (g, GIMPLE_OMP_FOR);
   if (combined_p)
     g->subcode |= GF_OMP_FOR_COMBINED;
   else
@@ -4661,9 +4670,8 @@ gimple_omp_for_combined_into_p (const_gimple g)
    value of COMBINED_P.  */
 
 static inline void
-gimple_omp_for_set_combined_into_p (gimple g, bool combined_p)
+gimple_omp_for_set_combined_into_p (gimple_omp_for g, bool combined_p)
 {
-  GIMPLE_CHECK (g, GIMPLE_OMP_FOR);
   if (combined_p)
     g->subcode |= GF_OMP_FOR_COMBINED_INTO;
   else
diff --git a/gcc/gimplify.c b/gcc/gimplify.c
index 27058a8..763b3c0 100644
--- a/gcc/gimplify.c
+++ b/gcc/gimplify.c
@@ -6572,7 +6572,7 @@ gimplify_omp_for (tree *expr_p, gimple_seq *pre_p)
   tree for_stmt, orig_for_stmt, decl, var, t;
   enum gimplify_status ret = GS_ALL_DONE;
   enum gimplify_status tret;
-  gimple gfor;
+  gimple_omp_for gfor;
   gimple_seq for_body, for_pre_body;
   int i;
   bool simd;
diff --git a/gcc/omp-low.c b/gcc/omp-low.c
index c2ab88a..8a3b2a1 100644
--- a/gcc/omp-low.c
+++ b/gcc/omp-low.c
@@ -190,7 +190,7 @@ struct omp_for_data
 {
   struct omp_for_data_loop loop;
   tree chunk_size;
-  gimple for_stmt;
+  gimple_omp_for for_stmt;
   tree pre, iter_type;
   int collapse;
   bool have_nowait, have_ordered;
@@ -289,7 +289,7 @@ is_combined_parallel (struct omp_region *region)
    them into *FD.  */
 
 static void
-extract_omp_for_data (gimple for_stmt, struct omp_for_data *fd,
+extract_omp_for_data (gimple_omp_for for_stmt, struct omp_for_data *fd,
 		      struct omp_for_data_loop *loops)
 {
   tree t, var, *collapse_iter, *collapse_count;
@@ -632,7 +632,7 @@ workshare_safe_to_combine_p (basic_block ws_entry_bb)
 
   gcc_assert (gimple_code (ws_stmt) == GIMPLE_OMP_FOR);
 
-  extract_omp_for_data (ws_stmt, &fd, NULL);
+  extract_omp_for_data (ws_stmt->as_a_gimple_omp_for (), &fd, NULL);
 
   if (fd.collapse > 1 && TREE_CODE (fd.loop.n2) != INTEGER_CST)
     return false;
@@ -665,16 +665,16 @@ get_ws_args_for (gimple par_stmt, gimple ws_stmt)
   location_t loc = gimple_location (ws_stmt);
   vec<tree, va_gc> *ws_args;
 
-  if (gimple_code (ws_stmt) == GIMPLE_OMP_FOR)
+  if (gimple_omp_for for_stmt = ws_stmt->dyn_cast_gimple_omp_for ())
     {
       struct omp_for_data fd;
       tree n1, n2;
 
-      extract_omp_for_data (ws_stmt, &fd, NULL);
+      extract_omp_for_data (for_stmt, &fd, NULL);
       n1 = fd.loop.n1;
       n2 = fd.loop.n2;
 
-      if (gimple_omp_for_combined_into_p (ws_stmt))
+      if (gimple_omp_for_combined_into_p (for_stmt))
 	{
 	  tree innerc
 	    = find_omp_clause (gimple_omp_parallel_clauses (par_stmt),
@@ -1972,16 +1972,15 @@ scan_omp_parallel (gimple_stmt_iterator *gsi, omp_context *outer_ctx)
 
   if (gimple_omp_parallel_combined_p (stmt))
     {
-      gimple for_stmt;
       struct walk_stmt_info wi;
 
       memset (&wi, 0, sizeof (wi));
       wi.val_only = true;
       walk_gimple_seq (gimple_omp_body (stmt),
 		       find_combined_for, NULL, &wi);
-      for_stmt = (gimple) wi.info;
-      if (for_stmt)
+      if (wi.info)
 	{
+	  gimple_omp_for for_stmt = ((gimple) wi.info)->as_a_gimple_omp_for ();
 	  struct omp_for_data fd;
 	  extract_omp_for_data (for_stmt, &fd, NULL);
 	  /* We need two temporaries with fd.loop.v type (istart/iend)
@@ -2121,7 +2120,7 @@ scan_omp_task (gimple_stmt_iterator *gsi, omp_context *outer_ctx)
 /* Scan an OpenMP loop directive.  */
 
 static void
-scan_omp_for (gimple stmt, omp_context *outer_ctx)
+scan_omp_for (gimple_omp_for stmt, omp_context *outer_ctx)
 {
   omp_context *ctx;
   size_t i;
@@ -2644,7 +2643,7 @@ scan_omp_1_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
       break;
 
     case GIMPLE_OMP_FOR:
-      scan_omp_for (stmt, ctx);
+      scan_omp_for (stmt->as_a_gimple_omp_for (), ctx);
       break;
 
     case GIMPLE_OMP_SECTIONS:
@@ -6871,7 +6870,8 @@ expand_omp_for (struct omp_region *region, gimple inner_stmt)
     = (struct omp_for_data_loop *)
       alloca (gimple_omp_for_collapse (last_stmt (region->entry))
 	      * sizeof (struct omp_for_data_loop));
-  extract_omp_for_data (last_stmt (region->entry), &fd, loops);
+  extract_omp_for_data (last_stmt (region->entry)->as_a_gimple_omp_for (),
+			&fd, loops);
   region->sched_kind = fd.sched_kind;
 
   gcc_assert (EDGE_COUNT (region->entry->succs) == 2);
@@ -8946,7 +8946,7 @@ lower_omp_for (gimple_stmt_iterator *gsi_p, omp_context *ctx)
 {
   tree *rhs_p, block;
   struct omp_for_data fd, *fdp = NULL;
-  gimple stmt = gsi_stmt (*gsi_p);
+  gimple_omp_for stmt = gsi_stmt (*gsi_p)->as_a_gimple_omp_for ();
   gimple_bind new_stmt;
   gimple_seq omp_for_body, body, dlist;
   size_t i;
diff --git a/gcc/tree-nested.c b/gcc/tree-nested.c
index 4009311..ca3e0ba 100644
--- a/gcc/tree-nested.c
+++ b/gcc/tree-nested.c
@@ -618,7 +618,7 @@ walk_function (walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
 /* Invoke CALLBACK on a GIMPLE_OMP_FOR's init, cond, incr and pre-body.  */
 
 static void
-walk_gimple_omp_for (gimple for_stmt,
+walk_gimple_omp_for (gimple_omp_for for_stmt,
     		     walk_stmt_fn callback_stmt, walk_tree_fn callback_op,
     		     struct nesting_info *info)
 {
@@ -1280,7 +1280,8 @@ convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
     case GIMPLE_OMP_FOR:
       save_suppress = info->suppress_expansion;
       convert_nonlocal_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
-      walk_gimple_omp_for (stmt, convert_nonlocal_reference_stmt,
+      walk_gimple_omp_for (stmt->as_a_gimple_omp_for (),
+			   convert_nonlocal_reference_stmt,
 	  		   convert_nonlocal_reference_op, info);
       walk_body (convert_nonlocal_reference_stmt,
 	  	 convert_nonlocal_reference_op, info, gimple_omp_body_ptr (stmt));
@@ -1741,7 +1742,8 @@ convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
     case GIMPLE_OMP_FOR:
       save_suppress = info->suppress_expansion;
       convert_local_omp_clauses (gimple_omp_for_clauses_ptr (stmt), wi);
-      walk_gimple_omp_for (stmt, convert_local_reference_stmt,
+      walk_gimple_omp_for (stmt->as_a_gimple_omp_for (),
+			   convert_local_reference_stmt,
 			   convert_local_reference_op, info);
       walk_body (convert_local_reference_stmt, convert_local_reference_op,
 		 info, gimple_omp_body_ptr (stmt));
diff --git a/gcc/tree-parloops.c b/gcc/tree-parloops.c
index dea9606..84fd451 100644
--- a/gcc/tree-parloops.c
+++ b/gcc/tree-parloops.c
@@ -1619,7 +1619,8 @@ create_parallel_loop (struct loop *loop, tree loop_fn, tree data,
   gimple_stmt_iterator gsi;
   basic_block bb, paral_bb, for_bb, ex_bb;
   tree t, param;
-  gimple stmt, for_stmt, phi, cond_stmt;
+  gimple stmt, phi, cond_stmt;
+  gimple_omp_for for_stmt;
   tree cvar, cvar_init, initvar, cvar_next, cvar_base, type;
   edge exit, nexit, guard, end, e;
 
-- 
1.8.5.3

