From 37cd16f7520ce52be9f310f80351ea96b40fe91d Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 26 Mar 2014 17:31:09 -0400
Subject: [PATCH 49/88] Update GRAPHITE to use more concrete gimple statement
 classes

gcc/
	* graphite-scop-detection.c (canonicalize_loop_closed_ssa):
	Strengthen local "psi" to be a gimple_phi_iterator and "phi" to
	a gimple_phi.

	* graphite-sese-to-poly.c (phi_arg_in_outermost_loop): Require
	a gimple_phi rathen than a plain gimple.
	(remove_simple_copy_phi): Require a gimple_phi_iterator;
	strengthen local "phi" to be a gimple_phi and "stmt" to be a
	gimple_assign.
	(remove_invariant_phi): Likewise.
	(simple_copy_phi_p): Require a gimple_phi.
	(reduction_phi_p): Require a gimple_phi_iterator; strengthen
	local "phi" to be a gimple_phi.
	(add_condition_to_pbb): Require a gimple_cond rather than a
	plain gimple.
	(add_conditions_to_domain): Add checked cast to gimple_cond
	within GIMPLE_COND case of switch statement.
	(single_pred_cond_non_loop_exit): Return a gimple_cond rather
	than a plain gimple, via a checked cast.
	(sese_dom_walker::before_dom_children): Strengthen local "stmt"
	from gimple to gimple_cond.
	(gsi_for_phi_node): Require a gimple_phi, and return a
	gimple_phi_iterator.
	(insert_out_of_ssa_copy): Strengthen local "stmt" from gimple to
	gimple_assign.
	(rewrite_reductions_out_of_ssa): Strengthen "psi" to be a
	gimple_phi_iterator, and "phi" to be a gimple_phi.
	(phi_contains_arg): Require a gimple_phi.
	(follow_ssa_with_commutative_ops): Strengthen return type from
	gimple to gimple_phi, by converting a check for code GIMPLE_PHI to
	a dyn_cast<gimple_phi>, and strengthening local "res" from gimple
	to gimple_phi.
	(detect_commutative_reduction_arg): Strengthen return type from
	gimple to gimple_phi, and strengthen local "phi" to be a
	gimple_phi.
	(detect_commutative_reduction_assign): Strengthen return type from
	gimple to gimple_phi, and strengthen local "res" to be a
	gimple_phi.
	(follow_inital_value_to_phi): Strengthen return type from
	gimple to gimple_phi.  Replace check for code GIMPLE_PHI with
	a dyn_cast<gimple_phi>.
	(detect_commutative_reduction): Strengthen return type and locals
	"loop_phi", "phi", "close_phi" from gimple to gimple_phi,
	introducing a checked cast of "stmt" in region guarded by
	scalar_close_phi_node_p (stmt).
	(translate_scalar_reduction_to_array_for_stmt): Require param
	"loop_phi" to be a gimple_phi.  Strengthen local "assign" from
	gimple to gimple_assign.
	(remove_phi): Require a gimple_phi.
	(close_phi_written_to_memory): Likewise.
	(translate_scalar_reduction_to_array): We expect the first element
	in each vector to be an arbitrary statement, but all of the
	subsequent elements to be phi nodes.  Hence the decls of gimple
	locals "loop_phi" and "close_phi" are replaced with decls of gimple
	"loop_stmt" and "close_stmt", with decls of the more-strongly typed
	gimple_phi "loop_phi" and "close_phi" occurring lower down, within
	the region where we're dealing with i > 0 and hence where we can
	safely assign them using the checked cast as_a <gimple_phi>.
	This allows many of the strengthenings from gimple to gimple_phi
	above.  We eliminate the local "stmt", since we can simply use
	"loop_stmt".
	(rewrite_commutative_reductions_out_of_ssa_close_phi): Strengthen
	param "close_phi" from gimple to gimple_phi, and local "gsi" from
	gimple_stmt_iterator to gimple_phi_iterator, converting uses of
	gsi_stmt to gsi.phi for type-safety.
	(scop_ivs_can_be_represented): Strengthen local "gsi" from
	gimple_stmt_iterator to gimple_phi_iterator, and "phi" from gimple
	to gimple_phi.
---
 gcc/graphite-scop-detection.c |   4 +-
 gcc/graphite-sese-to-poly.c   | 125 ++++++++++++++++++++++--------------------
 2 files changed, 68 insertions(+), 61 deletions(-)

diff --git a/gcc/graphite-scop-detection.c b/gcc/graphite-scop-detection.c
index 40ccad0..5e0c507 100644
--- a/gcc/graphite-scop-detection.c
+++ b/gcc/graphite-scop-detection.c
@@ -1338,14 +1338,14 @@ canonicalize_loop_closed_ssa (loop_p loop)
     }
   else
     {
-      gimple_stmt_iterator psi;
+      gimple_phi_iterator psi;
       basic_block close = split_edge (e);
 
       e = single_succ_edge (close);
 
       for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
 	{
-	  gimple phi = gsi_stmt (psi);
+	  gimple_phi phi = psi.phi ();
 	  unsigned i;
 
 	  for (i = 0; i < gimple_phi_num_args (phi); i++)
diff --git a/gcc/graphite-sese-to-poly.c b/gcc/graphite-sese-to-poly.c
index c3adfe1..f58f750 100644
--- a/gcc/graphite-sese-to-poly.c
+++ b/gcc/graphite-sese-to-poly.c
@@ -94,7 +94,7 @@ tree_int_to_gmp (tree t, mpz_t res)
    loop.  */
 
 static size_t
-phi_arg_in_outermost_loop (gimple phi)
+phi_arg_in_outermost_loop (gimple_phi phi)
 {
   loop_p loop = gimple_bb (phi)->loop_father;
   size_t i, res = 0;
@@ -113,13 +113,13 @@ phi_arg_in_outermost_loop (gimple phi)
    PSI by inserting on the loop ENTRY edge assignment "RES = INIT".  */
 
 static void
-remove_simple_copy_phi (gimple_stmt_iterator *psi)
+remove_simple_copy_phi (gimple_phi_iterator *psi)
 {
-  gimple phi = gsi_stmt (*psi);
+  gimple_phi phi = psi->phi ();
   tree res = gimple_phi_result (phi);
   size_t entry = phi_arg_in_outermost_loop (phi);
   tree init = gimple_phi_arg_def (phi, entry);
-  gimple stmt = gimple_build_assign (res, init);
+  gimple_assign stmt = gimple_build_assign (res, init);
   edge e = gimple_phi_arg_edge (phi, entry);
 
   remove_phi_node (psi, false);
@@ -130,16 +130,16 @@ remove_simple_copy_phi (gimple_stmt_iterator *psi)
    loop ENTRY edge the assignment RES = INIT.  */
 
 static void
-remove_invariant_phi (sese region, gimple_stmt_iterator *psi)
+remove_invariant_phi (sese region, gimple_phi_iterator *psi)
 {
-  gimple phi = gsi_stmt (*psi);
+  gimple_phi phi = psi->phi ();
   loop_p loop = loop_containing_stmt (phi);
   tree res = gimple_phi_result (phi);
   tree scev = scalar_evolution_in_region (region, loop, res);
   size_t entry = phi_arg_in_outermost_loop (phi);
   edge e = gimple_phi_arg_edge (phi, entry);
   tree var;
-  gimple stmt;
+  gimple_assign stmt;
   gimple_seq stmts = NULL;
 
   if (tree_contains_chrecs (scev, NULL))
@@ -158,7 +158,7 @@ remove_invariant_phi (sese region, gimple_stmt_iterator *psi)
 /* Returns true when the phi node at PSI is of the form "a = phi (a, x)".  */
 
 static inline bool
-simple_copy_phi_p (gimple phi)
+simple_copy_phi_p (gimple_phi phi)
 {
   tree res;
 
@@ -175,10 +175,10 @@ simple_copy_phi_p (gimple phi)
    be considered.  */
 
 static bool
-reduction_phi_p (sese region, gimple_stmt_iterator *psi)
+reduction_phi_p (sese region, gimple_phi_iterator *psi)
 {
   loop_p loop;
-  gimple phi = gsi_stmt (*psi);
+  gimple_phi phi = psi->phi ();
   tree res = gimple_phi_result (phi);
 
   loop = loop_containing_stmt (phi);
@@ -1122,7 +1122,7 @@ create_pw_aff_from_tree (poly_bb_p pbb, tree t)
    inequalities.  */
 
 static void
-add_condition_to_pbb (poly_bb_p pbb, gimple stmt, enum tree_code code)
+add_condition_to_pbb (poly_bb_p pbb, gimple_cond stmt, enum tree_code code)
 {
   isl_pw_aff *lhs = create_pw_aff_from_tree (pbb, gimple_cond_lhs (stmt));
   isl_pw_aff *rhs = create_pw_aff_from_tree (pbb, gimple_cond_rhs (stmt));
@@ -1182,13 +1182,14 @@ add_conditions_to_domain (poly_bb_p pbb)
       {
       case GIMPLE_COND:
 	  {
-	    enum tree_code code = gimple_cond_code (stmt);
+	    gimple_cond cond_stmt = as_a <gimple_cond> (stmt);
+	    enum tree_code code = gimple_cond_code (cond_stmt);
 
 	    /* The conditions for ELSE-branches are inverted.  */
 	    if (!GBB_CONDITION_CASES (gbb)[i])
 	      code = invert_tree_comparison (code, false);
 
-	    add_condition_to_pbb (pbb, stmt, code);
+	    add_condition_to_pbb (pbb, cond_stmt, code);
 	    break;
 	  }
 
@@ -1218,7 +1219,7 @@ add_conditions_to_constraints (scop_p scop)
    edge between BB and its predecessor is not a loop exit edge, and
    the last statement of the single predecessor is a COND_EXPR.  */
 
-static gimple
+static gimple_cond
 single_pred_cond_non_loop_exit (basic_block bb)
 {
   if (single_pred_p (bb))
@@ -1233,7 +1234,7 @@ single_pred_cond_non_loop_exit (basic_block bb)
       stmt = last_stmt (pred);
 
       if (stmt && gimple_code (stmt) == GIMPLE_COND)
-	return stmt;
+	return as_a <gimple_cond> (stmt);
     }
 
   return NULL;
@@ -1264,7 +1265,7 @@ void
 sese_dom_walker::before_dom_children (basic_block bb)
 {
   gimple_bb_p gbb;
-  gimple stmt;
+  gimple_cond stmt;
 
   if (!bb_in_sese_p (bb, m_region))
     return;
@@ -1930,10 +1931,10 @@ build_scop_drs (scop_p scop)
 
 /* Return a gsi at the position of the phi node STMT.  */
 
-static gimple_stmt_iterator
-gsi_for_phi_node (gimple stmt)
+static gimple_phi_iterator
+gsi_for_phi_node (gimple_phi stmt)
 {
-  gimple_stmt_iterator psi;
+  gimple_phi_iterator psi;
   basic_block bb = gimple_bb (stmt);
 
   for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
@@ -2005,7 +2006,7 @@ insert_out_of_ssa_copy (scop_p scop, tree res, tree expr, gimple after_stmt)
   gimple_seq stmts;
   gimple_stmt_iterator gsi;
   tree var = force_gimple_operand (expr, &stmts, true, NULL_TREE);
-  gimple stmt = gimple_build_assign (unshare_expr (res), var);
+  gimple_assign stmt = gimple_build_assign (unshare_expr (res), var);
   auto_vec<gimple, 3> x;
 
   gimple_seq_add_stmt (&stmts, stmt);
@@ -2294,14 +2295,14 @@ static void
 rewrite_reductions_out_of_ssa (scop_p scop)
 {
   basic_block bb;
-  gimple_stmt_iterator psi;
+  gimple_phi_iterator psi;
   sese region = SCOP_REGION (scop);
 
   FOR_EACH_BB_FN (bb, cfun)
     if (bb_in_sese_p (bb, region))
       for (psi = gsi_start_phis (bb); !gsi_end_p (psi);)
 	{
-	  gimple phi = gsi_stmt (psi);
+	  gimple_phi phi = psi.phi ();
 
 	  if (virtual_operand_p (gimple_phi_result (phi)))
 	    {
@@ -2617,7 +2618,7 @@ is_reduction_operation_p (gimple stmt)
 /* Returns true when PHI contains an argument ARG.  */
 
 static bool
-phi_contains_arg (gimple phi, tree arg)
+phi_contains_arg (gimple_phi phi, tree arg)
 {
   size_t i;
 
@@ -2630,7 +2631,7 @@ phi_contains_arg (gimple phi, tree arg)
 
 /* Return a loop phi node that corresponds to a reduction containing LHS.  */
 
-static gimple
+static gimple_phi
 follow_ssa_with_commutative_ops (tree arg, tree lhs)
 {
   gimple stmt;
@@ -2644,10 +2645,10 @@ follow_ssa_with_commutative_ops (tree arg, tree lhs)
       || gimple_code (stmt) == GIMPLE_CALL)
     return NULL;
 
-  if (gimple_code (stmt) == GIMPLE_PHI)
+  if (gimple_phi phi = dyn_cast <gimple_phi> (stmt))
     {
-      if (phi_contains_arg (stmt, lhs))
-	return stmt;
+      if (phi_contains_arg (phi, lhs))
+	return phi;
       return NULL;
     }
 
@@ -2659,7 +2660,8 @@ follow_ssa_with_commutative_ops (tree arg, tree lhs)
 
   if (is_reduction_operation_p (stmt))
     {
-      gimple res = follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt), lhs);
+      gimple_phi res =
+	follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt), lhs);
 
       return res ? res :
 	follow_ssa_with_commutative_ops (gimple_assign_rhs2 (stmt), lhs);
@@ -2671,12 +2673,12 @@ follow_ssa_with_commutative_ops (tree arg, tree lhs)
 /* Detect commutative and associative scalar reductions starting at
    the STMT.  Return the phi node of the reduction cycle, or NULL.  */
 
-static gimple
+static gimple_phi
 detect_commutative_reduction_arg (tree lhs, gimple stmt, tree arg,
 				  vec<gimple> *in,
 				  vec<gimple> *out)
 {
-  gimple phi = follow_ssa_with_commutative_ops (arg, lhs);
+  gimple_phi phi = follow_ssa_with_commutative_ops (arg, lhs);
 
   if (!phi)
     return NULL;
@@ -2689,7 +2691,7 @@ detect_commutative_reduction_arg (tree lhs, gimple stmt, tree arg,
 /* Detect commutative and associative scalar reductions starting at
    STMT.  Return the phi node of the reduction cycle, or NULL.  */
 
-static gimple
+static gimple_phi
 detect_commutative_reduction_assign (gimple stmt, vec<gimple> *in,
 				     vec<gimple> *out)
 {
@@ -2702,9 +2704,10 @@ detect_commutative_reduction_assign (gimple stmt, vec<gimple> *in,
 
   if (is_reduction_operation_p (stmt))
     {
-      gimple res = detect_commutative_reduction_arg (lhs, stmt,
-						     gimple_assign_rhs1 (stmt),
-						     in, out);
+      gimple_phi res =
+	detect_commutative_reduction_arg (lhs, stmt,
+					  gimple_assign_rhs1 (stmt),
+					  in, out);
       return res ? res
 	: detect_commutative_reduction_arg (lhs, stmt,
 					    gimple_assign_rhs2 (stmt),
@@ -2716,7 +2719,7 @@ detect_commutative_reduction_assign (gimple stmt, vec<gimple> *in,
 
 /* Return a loop phi node that corresponds to a reduction containing LHS.  */
 
-static gimple
+static gimple_phi
 follow_inital_value_to_phi (tree arg, tree lhs)
 {
   gimple stmt;
@@ -2726,9 +2729,9 @@ follow_inital_value_to_phi (tree arg, tree lhs)
 
   stmt = SSA_NAME_DEF_STMT (arg);
 
-  if (gimple_code (stmt) == GIMPLE_PHI
-      && phi_contains_arg (stmt, lhs))
-    return stmt;
+  if (gimple_phi phi = dyn_cast <gimple_phi> (stmt))
+    if (phi_contains_arg (phi, lhs))
+      return phi;
 
   return NULL;
 }
@@ -2802,13 +2805,14 @@ used_outside_reduction (tree def, gimple loop_phi)
    the SCOP starting at the loop closed phi node STMT.  Return the phi
    node of the reduction cycle, or NULL.  */
 
-static gimple
+static gimple_phi
 detect_commutative_reduction (scop_p scop, gimple stmt, vec<gimple> *in,
 			      vec<gimple> *out)
 {
   if (scalar_close_phi_node_p (stmt))
     {
-      gimple def, loop_phi, phi, close_phi = stmt;
+      gimple def;
+      gimple_phi loop_phi, phi, close_phi = as_a <gimple_phi> (stmt);
       tree init, lhs, arg = gimple_phi_arg_def (close_phi, 0);
 
       if (TREE_CODE (arg) != SSA_NAME)
@@ -2848,10 +2852,10 @@ detect_commutative_reduction (scop_p scop, gimple stmt, vec<gimple> *in,
 
 static void
 translate_scalar_reduction_to_array_for_stmt (scop_p scop, tree red,
-					      gimple stmt, gimple loop_phi)
+					      gimple stmt, gimple_phi loop_phi)
 {
   tree res = gimple_phi_result (loop_phi);
-  gimple assign = gimple_build_assign (res, unshare_expr (red));
+  gimple_assign assign = gimple_build_assign (res, unshare_expr (red));
   gimple_stmt_iterator gsi;
 
   insert_stmts (scop, assign, NULL, gsi_after_labels (gimple_bb (loop_phi)));
@@ -2866,7 +2870,7 @@ translate_scalar_reduction_to_array_for_stmt (scop_p scop, tree red,
    the PHI_RESULT.  */
 
 static void
-remove_phi (gimple phi)
+remove_phi (gimple_phi phi)
 {
   imm_use_iterator imm_iter;
   tree def;
@@ -2929,7 +2933,7 @@ dr_indices_valid_in_loop (tree ref ATTRIBUTE_UNUSED, tree *index, void *data)
    NULL_TREE.  */
 
 static tree
-close_phi_written_to_memory (gimple close_phi)
+close_phi_written_to_memory (gimple_phi close_phi)
 {
   imm_use_iterator imm_iter;
   use_operand_p use_p;
@@ -2984,30 +2988,33 @@ translate_scalar_reduction_to_array (scop_p scop,
 				     vec<gimple> in,
 				     vec<gimple> out)
 {
-  gimple loop_phi;
+  gimple loop_stmt;
   unsigned int i = out.length () - 1;
-  tree red = close_phi_written_to_memory (out[i]);
+  tree red = close_phi_written_to_memory (as_a <gimple_phi> (out[i]));
 
-  FOR_EACH_VEC_ELT (in, i, loop_phi)
+  FOR_EACH_VEC_ELT (in, i, loop_stmt)
     {
-      gimple close_phi = out[i];
+      gimple close_stmt = out[i];
 
       if (i == 0)
 	{
-	  gimple stmt = loop_phi;
-	  basic_block bb = split_reduction_stmt (scop, stmt);
+	  basic_block bb = split_reduction_stmt (scop, loop_stmt);
 	  poly_bb_p pbb = pbb_from_bb (bb);
 	  PBB_IS_REDUCTION (pbb) = true;
-	  gcc_assert (close_phi == loop_phi);
+	  gcc_assert (close_stmt == loop_stmt);
 
 	  if (!red)
 	    red = create_zero_dim_array
-	      (gimple_assign_lhs (stmt), "Commutative_Associative_Reduction");
+	      (gimple_assign_lhs (loop_stmt), "Commutative_Associative_Reduction");
 
-	  translate_scalar_reduction_to_array_for_stmt (scop, red, stmt, in[1]);
+	  translate_scalar_reduction_to_array_for_stmt (scop, red, loop_stmt,
+							as_a <gimple_phi> (in[1]));
 	  continue;
 	}
 
+      gimple_phi loop_phi = as_a <gimple_phi> (loop_stmt);
+      gimple_phi close_phi = as_a <gimple_phi> (close_stmt);
+
       if (i == in.length () - 1)
 	{
 	  insert_out_of_ssa_copy (scop, gimple_phi_result (close_phi),
@@ -3027,7 +3034,7 @@ translate_scalar_reduction_to_array (scop_p scop,
 
 static bool
 rewrite_commutative_reductions_out_of_ssa_close_phi (scop_p scop,
-						     gimple close_phi)
+						     gimple_phi close_phi)
 {
   bool res;
   auto_vec<gimple, 10> in;
@@ -3048,7 +3055,7 @@ static bool
 rewrite_commutative_reductions_out_of_ssa_loop (scop_p scop,
 						loop_p loop)
 {
-  gimple_stmt_iterator gsi;
+  gimple_phi_iterator gsi;
   edge exit = single_exit (loop);
   tree res;
   bool changed = false;
@@ -3057,11 +3064,11 @@ rewrite_commutative_reductions_out_of_ssa_loop (scop_p scop,
     return false;
 
   for (gsi = gsi_start_phis (exit->dest); !gsi_end_p (gsi); gsi_next (&gsi))
-    if ((res = gimple_phi_result (gsi_stmt (gsi)))
+    if ((res = gimple_phi_result (gsi.phi ()))
 	&& !virtual_operand_p (res)
 	&& !scev_analyzable_p (res, SCOP_REGION (scop)))
       changed |= rewrite_commutative_reductions_out_of_ssa_close_phi
-	(scop, gsi_stmt (gsi));
+	(scop, gsi.phi ());
 
   return changed;
 }
@@ -3098,7 +3105,7 @@ static bool
 scop_ivs_can_be_represented (scop_p scop)
 {
   loop_p loop;
-  gimple_stmt_iterator psi;
+  gimple_phi_iterator psi;
   bool result = true;
 
   FOR_EACH_LOOP (loop, 0)
@@ -3109,7 +3116,7 @@ scop_ivs_can_be_represented (scop_p scop)
       for (psi = gsi_start_phis (loop->header);
 	   !gsi_end_p (psi); gsi_next (&psi))
 	{
-	  gimple phi = gsi_stmt (psi);
+	  gimple_phi phi = psi.phi ();
 	  tree res = PHI_RESULT (phi);
 	  tree type = TREE_TYPE (res);
 
-- 
1.8.5.3

