OSDN Git Service

2009-10-07 Sebastian Pop <sebastian.pop@amd.com>
[pf3gnuchains/gcc-fork.git] / gcc / graphite-sese-to-poly.c
index e2c9cf8..1dea7fd 100644 (file)
@@ -57,7 +57,6 @@ along with GCC; see the file COPYING3.  If not see
 static bool
 var_used_in_not_loop_header_phi_node (tree var)
 {
-
   imm_use_iterator imm_iter;
   gimple stmt;
   bool result = false;
@@ -194,7 +193,11 @@ reduction_phi_p (sese region, gimple_stmt_iterator *psi)
      reductions.  */
   if (simple_iv (loop, loop, res, &iv, true))
     {
-      gsi_next (psi);
+      if (integer_zerop (iv.step))
+       remove_invariant_phi (region, psi);
+      else
+       gsi_next (psi);
+
       return false;
     }
 
@@ -347,7 +350,7 @@ free_scops (VEC (scop_p, heap) *scops)
    information.  */
 
 static void
-try_generate_gimple_bb (scop_p scop, basic_block bb)
+try_generate_gimple_bb (scop_p scop, basic_block bb, sbitmap reductions)
 {
   VEC (data_reference_p, heap) *drs = VEC_alloc (data_reference_p, heap, 5);
   loop_p nest = outermost_loop_in_sese (SCOP_REGION (scop), bb);
@@ -363,7 +366,8 @@ try_generate_gimple_bb (scop_p scop, basic_block bb)
   if (!graphite_stmt_p (SCOP_REGION (scop), bb, drs))
     free_data_refs (drs);
   else
-    new_poly_bb (scop, new_gimple_bb (bb, drs));
+    new_poly_bb (scop, new_gimple_bb (bb, drs), TEST_BIT (reductions,
+                                                         bb->index));
 }
 
 /* Returns true if all predecessors of BB, that are not dominated by BB, are
@@ -418,7 +422,7 @@ graphite_sort_dominated_info (VEC (basic_block, heap) *dom)
 /* Recursive helper function for build_scops_bbs.  */
 
 static void
-build_scop_bbs_1 (scop_p scop, sbitmap visited, basic_block bb)
+build_scop_bbs_1 (scop_p scop, sbitmap visited, basic_block bb, sbitmap reductions)
 {
   sese region = SCOP_REGION (scop);
   VEC (basic_block, heap) *dom;
@@ -427,7 +431,7 @@ build_scop_bbs_1 (scop_p scop, sbitmap visited, basic_block bb)
       || !bb_in_sese_p (bb, region))
     return;
 
-  try_generate_gimple_bb (scop, bb);
+  try_generate_gimple_bb (scop, bb, reductions);
   SET_BIT (visited, bb->index);
 
   dom = get_dominated_by (CDI_DOMINATORS, bb);
@@ -445,7 +449,7 @@ build_scop_bbs_1 (scop_p scop, sbitmap visited, basic_block bb)
       for (i = 0; VEC_iterate (basic_block, dom, i, dom_bb); i++)
        if (all_non_dominated_preds_marked_p (dom_bb, visited))
          {
-           build_scop_bbs_1 (scop, visited, dom_bb);
+           build_scop_bbs_1 (scop, visited, dom_bb, reductions);
            VEC_unordered_remove (basic_block, dom, i);
            break;
          }
@@ -456,15 +460,14 @@ build_scop_bbs_1 (scop_p scop, sbitmap visited, basic_block bb)
 
 /* Gather the basic blocks belonging to the SCOP.  */
 
-void
-build_scop_bbs (scop_p scop)
+static void
+build_scop_bbs (scop_p scop, sbitmap reductions)
 {
   sbitmap visited = sbitmap_alloc (last_basic_block);
   sese region = SCOP_REGION (scop);
 
   sbitmap_zero (visited);
-  build_scop_bbs_1 (scop, visited, SESE_ENTRY_BB (region));
-
+  build_scop_bbs_1 (scop, visited, SESE_ENTRY_BB (region), reductions);
   sbitmap_free (visited);
 }
 
@@ -1158,7 +1161,7 @@ create_linear_expr_from_tree (poly_bb_p pbb, tree t)
   ppl_Linear_Expression_t res;
   ppl_dimension_type dim;
   sese region = SCOP_REGION (PBB_SCOP (pbb));
-  loop_p loop = GBB_BB (PBB_BLACK_BOX (pbb))->loop_father;
+  loop_p loop = pbb_loop (pbb);
 
   dim = pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb);
   ppl_new_Linear_Expression_with_dimension (&res, dim);
@@ -1708,20 +1711,54 @@ build_poly_dr (data_reference_p dr, poly_bb_p pbb)
               dr, DR_NUM_DIMENSIONS (dr));
 }
 
+/* Write to FILE the alias graph of data references with DIMACS format.  */
+
+static inline bool
+write_alias_graph_to_ascii_dimacs (FILE *file, char *comment,
+                                  VEC (data_reference_p, heap) *drs)
+{
+  int num_vertex = VEC_length (data_reference_p, drs);
+  int edge_num = 0;
+  data_reference_p dr1, dr2;
+  int i, j;
+
+  if (num_vertex == 0)
+    return true;
+
+  for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
+    for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
+      if (dr_may_alias_p (dr1, dr2))
+       edge_num++;
+
+  fprintf (file, "$\n");
+
+  if (comment)
+    fprintf (file, "c %s\n", comment);
+
+  fprintf (file, "p edge %d %d\n", num_vertex, edge_num);
+
+  for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
+    for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
+      if (dr_may_alias_p (dr1, dr2))
+       fprintf (file, "e %d %d\n", i + 1, j + 1);
+
+  return true;
+}
+
 static void
-partition_drs_to_sets (VEC (data_reference_p, heap) **drs, int choice,
-                        bool (* edge_exist_p) (const struct data_reference *,
-                                               const struct data_reference *))
+partition_drs_to_sets (VEC (data_reference_p, heap) *drs, int choice,
+                      bool (* edge_exist_p) (const struct data_reference *,
+                                             const struct data_reference *))
 {
-  int num_vertex = VEC_length (data_reference_p, *drs);
+  int num_vertex = VEC_length (data_reference_p, drs);
   struct graph *g = new_graph (num_vertex);
   data_reference_p dr1, dr2;
   int i, j;
   int num_component;
   int *queue;
 
-  for (i = 0; VEC_iterate (data_reference_p, *drs, i, dr1); i++)
-    for (j = i+1; VEC_iterate (data_reference_p, *drs, j, dr2); j++)
+  for (i = 0; VEC_iterate (data_reference_p, drs, i, dr1); i++)
+    for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
       if ((*edge_exist_p) (dr1, dr2))
        {
          add_edge (g, i, j);
@@ -1736,7 +1773,7 @@ partition_drs_to_sets (VEC (data_reference_p, heap) **drs, int choice,
 
   for (i = 0; i < g->n_vertices; i++)
     {
-      data_reference_p dr = VEC_index (data_reference_p, *drs, i);
+      data_reference_p dr = VEC_index (data_reference_p, drs, i);
       if (!dr->aux)
        dr->aux = XNEWVEC (int, 2);
       ((int *)(dr->aux))[choice] = g->vertices[i].component + 1;
@@ -1756,7 +1793,7 @@ dr_same_base_object_p (const struct data_reference *dr1,
 /* Group each data reference in DRS with it's alias set num.  */
 
 static void
-build_alias_set_for_drs (VEC (data_reference_p, heap) **drs)
+build_alias_set_for_drs (VEC (data_reference_p, heap) *drs)
 {
   partition_drs_to_sets (drs, ALIAS_SET_INDEX, dr_may_alias_p);
 }
@@ -1764,7 +1801,7 @@ build_alias_set_for_drs (VEC (data_reference_p, heap) **drs)
 /* Group each data reference in DRS with it's base object set num.  */
 
 static void
-build_base_obj_set_for_drs (VEC (data_reference_p, heap) **drs)
+build_base_obj_set_for_drs (VEC (data_reference_p, heap) *drs)
 {
   partition_drs_to_sets (drs, BASE_OBJECT_SET_INDEX, dr_same_base_object_p);
 }
@@ -1793,14 +1830,30 @@ build_scop_drs (scop_p scop)
   VEC (data_reference_p, heap) *drs = VEC_alloc (data_reference_p, heap, 3);
 
   for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
-    {
-      VEC (data_reference_p, heap) *gbb_drs = GBB_DATA_REFS (PBB_BLACK_BOX (pbb));
-      for (j = 0; VEC_iterate (data_reference_p, gbb_drs, j, dr); j++)
-       VEC_safe_push (data_reference_p, heap, drs, dr);
-    }
+    for (j = 0; VEC_iterate (data_reference_p,
+                            GBB_DATA_REFS (PBB_BLACK_BOX (pbb)), j, dr); j++)
+      VEC_safe_push (data_reference_p, heap, drs, dr);
+
+  build_alias_set_for_drs (drs);
+  build_base_obj_set_for_drs (drs);
 
-  build_alias_set_for_drs (&drs);
-  build_base_obj_set_for_drs (&drs);
+  /* When debugging, enable the following code.  This cannot be used
+     in production compilers.  */
+#if 0
+  {
+    char comment[100];
+    FILE *file;
+
+    file = fopen ("/tmp/dr_alias_graph", "ab");
+    if (file)
+      {
+       snprintf (comment, sizeof (comment), "%s %s", main_input_filename,
+                 current_function_name ());
+       write_alias_graph_to_ascii_dimacs (file, comment, drs);
+       fclose (file);
+      }
+  }
+#endif
 
   VEC_free (data_reference_p, heap, drs);
 
@@ -1808,34 +1861,20 @@ build_scop_drs (scop_p scop)
     build_pbb_drs (pbb);
 }
 
-/* Return a gsi at the position of the VAR definition.  */
+/* Return a gsi at the position of the phi node STMT.  */
 
 static gimple_stmt_iterator
-gsi_for_ssa_name_def (tree var)
+gsi_for_phi_node (gimple stmt)
 {
-  gimple stmt;
-  basic_block bb;
-  gimple_stmt_iterator gsi;
   gimple_stmt_iterator psi;
-
-  gcc_assert (TREE_CODE (var) == SSA_NAME);
-
-  stmt = SSA_NAME_DEF_STMT (var);
-  bb = gimple_bb (stmt);
+  basic_block bb = gimple_bb (stmt);
 
   for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
     if (stmt == gsi_stmt (psi))
-      return gsi_after_labels (bb);
-
-  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
-    if (stmt == gsi_stmt (gsi))
-      {
-       gsi_next (&gsi);
-       return gsi;
-      }
+      return psi;
 
   gcc_unreachable ();
-  return gsi;
+  return psi;
 }
 
 /* Insert the assignment "RES := VAR" just after the definition of VAR.  */
@@ -1843,10 +1882,10 @@ gsi_for_ssa_name_def (tree var)
 static void
 insert_out_of_ssa_copy (tree res, tree var)
 {
-  gimple_stmt_iterator gsi = gsi_for_ssa_name_def (var);
   gimple stmt;
   gimple_seq stmts;
   gimple_stmt_iterator si;
+  gimple_stmt_iterator gsi;
 
   var = force_gimple_operand (var, &stmts, true, NULL_TREE);
   stmt = gimple_build_assign (res, var);
@@ -1854,7 +1893,18 @@ insert_out_of_ssa_copy (tree res, tree var)
     stmts = gimple_seq_alloc ();
   si = gsi_last (stmts);
   gsi_insert_after (&si, stmt, GSI_NEW_STMT);
-  gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
+
+  stmt = SSA_NAME_DEF_STMT (var);
+  if (gimple_code (stmt) == GIMPLE_PHI)
+    {
+      gsi = gsi_after_labels (gimple_bb (stmt));
+      gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
+    }
+  else
+    {
+      gsi = gsi_for_stmt (stmt);
+      gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
+    }
 }
 
 /* Insert on edge E the assignment "RES := EXPR".  */
@@ -1897,9 +1947,8 @@ create_zero_dim_array (tree var)
 static bool
 scalar_close_phi_node_p (gimple phi)
 {
-  gcc_assert (gimple_code (phi) == GIMPLE_PHI);
-
-  if (!is_gimple_reg (gimple_phi_result (phi)))
+  if (gimple_code (phi) != GIMPLE_PHI
+      || !is_gimple_reg (gimple_phi_result (phi)))
     return false;
 
   return (gimple_phi_num_args (phi) == 1);
@@ -2015,6 +2064,106 @@ rewrite_phi_out_of_ssa (gimple_stmt_iterator *psi)
   gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
 }
 
+/* Return true when DEF can be analyzed in REGION by the scalar
+   evolution analyzer.  */
+
+static bool
+scev_analyzable_p (tree def, sese region)
+{
+  gimple stmt = SSA_NAME_DEF_STMT (def);
+  loop_p loop = loop_containing_stmt (stmt);
+  tree scev = scalar_evolution_in_region (region, loop, def);
+
+  return !chrec_contains_undetermined (scev);
+}
+
+/* Rewrite the scalar dependence of DEF used in USE_STMT with a memory
+   read from ZERO_DIM_ARRAY.  */
+
+static void
+rewrite_cross_bb_scalar_dependence (tree zero_dim_array, tree def, gimple use_stmt)
+{
+  tree var = SSA_NAME_VAR (def);
+  gimple name_stmt = gimple_build_assign (var, zero_dim_array);
+  tree name = make_ssa_name (var, name_stmt);
+  ssa_op_iter iter;
+  use_operand_p use_p;
+  gimple_stmt_iterator gsi;
+
+  gimple_assign_set_lhs (name_stmt, name);
+
+  if (gimple_code (use_stmt) == GIMPLE_PHI)
+    {
+      gimple phi = use_stmt;
+      edge entry;
+      unsigned i;
+
+      for (i = 0; i < gimple_phi_num_args (phi); i++)
+       if (operand_equal_p (def, gimple_phi_arg_def (phi, i), 0))
+         {
+           entry = gimple_phi_arg_edge (phi, i);
+           break;
+         }
+
+      FOR_EACH_PHI_ARG (use_p, phi, iter, SSA_OP_USE)
+       if (operand_equal_p (def, USE_FROM_PTR (use_p), 0))
+         {
+           gsi = gsi_last_bb (entry->src);
+           gsi_insert_after (&gsi, name_stmt, GSI_NEW_STMT);
+           SET_USE (use_p, name);
+           break;
+         }
+    }
+  else
+    {
+      gsi = gsi_for_stmt (use_stmt);
+      gsi_insert_before (&gsi, name_stmt, GSI_NEW_STMT);
+
+      FOR_EACH_SSA_USE_OPERAND (use_p, use_stmt, iter, SSA_OP_ALL_USES)
+       if (operand_equal_p (def, USE_FROM_PTR (use_p), 0))
+         replace_exp (use_p, name);
+    }
+
+  update_stmt (use_stmt);
+}
+
+/* Rewrite the scalar dependences crossing the boundary of the BB
+   containing STMT with an array.  */
+
+static void
+rewrite_cross_bb_scalar_deps (sese region, gimple_stmt_iterator *gsi)
+{
+  gimple stmt = gsi_stmt (*gsi);
+  imm_use_iterator imm_iter;
+  tree def;
+  basic_block def_bb;
+  tree zero_dim_array = NULL_TREE;
+  gimple use_stmt;
+
+  if (gimple_code (stmt) != GIMPLE_ASSIGN)
+    return;
+
+  def = gimple_assign_lhs (stmt);
+  if (!is_gimple_reg (def)
+      || scev_analyzable_p (def, region))
+    return;
+
+  def_bb = gimple_bb (stmt);
+
+  FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
+    if (def_bb != gimple_bb (use_stmt))
+      {
+       if (!zero_dim_array)
+         {
+           zero_dim_array = create_zero_dim_array (SSA_NAME_VAR (def));
+           insert_out_of_ssa_copy (zero_dim_array, def);
+           gsi_next (gsi);
+         }
+
+       rewrite_cross_bb_scalar_dependence (zero_dim_array, def, use_stmt);
+      }
+}
+
 /* Rewrite out of SSA all the reduction phi nodes of SCOP.  */
 
 static void
@@ -2025,7 +2174,7 @@ rewrite_reductions_out_of_ssa (scop_p scop)
   sese region = SCOP_REGION (scop);
 
   FOR_EACH_BB (bb)
-    if (bb_in_region (bb, SESE_ENTRY_BB (region), SESE_EXIT_BB (region)))
+    if (bb_in_sese_p (bb, region))
       for (psi = gsi_start_phis (bb); !gsi_end_p (psi);)
        {
          if (scalar_close_phi_node_p (gsi_stmt (psi)))
@@ -2039,6 +2188,17 @@ rewrite_reductions_out_of_ssa (scop_p scop)
   verify_ssa (false);
   verify_loop_closed_ssa ();
 #endif
+
+  FOR_EACH_BB (bb)
+    if (bb_in_sese_p (bb, region))
+      for (psi = gsi_start_bb (bb); !gsi_end_p (psi); gsi_next (&psi))
+       rewrite_cross_bb_scalar_deps (region, &psi);
+
+  update_ssa (TODO_update_ssa);
+#ifdef ENABLE_CHECKING
+  verify_ssa (false);
+  verify_loop_closed_ssa ();
+#endif
 }
 
 /* Returns the number of pbbs that are in loops contained in SCOP.  */
@@ -2057,14 +2217,403 @@ nb_pbbs_in_loops (scop_p scop)
   return res;
 }
 
+/* Return the number of data references in BB that write in
+   memory.  */
+
+static int
+nb_data_writes_in_bb (basic_block bb)
+{
+  int res = 0;
+  gimple_stmt_iterator gsi;
+
+  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+    if (gimple_vdef (gsi_stmt (gsi)))
+      res++;
+
+  return res;
+}
+
+/* Splits STMT out of its current BB.  */
+
+static basic_block
+split_reduction_stmt (gimple stmt)
+{
+  gimple_stmt_iterator gsi;
+  basic_block bb = gimple_bb (stmt);
+  edge e;
+
+  /* Do not split basic blocks with no writes to memory: the reduction
+     will be the only write to memory.  */
+  if (nb_data_writes_in_bb (bb) == 0)
+    return bb;
+
+  split_block (bb, stmt);
+
+  gsi = gsi_last_bb (bb);
+  gsi_prev (&gsi);
+  e = split_block (bb, gsi_stmt (gsi));
+
+  return e->dest;
+}
+
+/* Return true when stmt is a reduction operation.  */
+
+static inline bool
+is_reduction_operation_p (gimple stmt)
+{
+  return flag_associative_math
+    && commutative_tree_code (gimple_assign_rhs_code (stmt))
+    && associative_tree_code (gimple_assign_rhs_code (stmt));
+}
+
+/* Returns true when PHI contains an argument ARG.  */
+
+static bool
+phi_contains_arg (gimple phi, tree arg)
+{
+  size_t i;
+
+  for (i = 0; i < gimple_phi_num_args (phi); i++)
+    if (operand_equal_p (arg, gimple_phi_arg_def (phi, i), 0))
+      return true;
+
+  return false;
+}
+
+/* Return a loop phi node that corresponds to a reduction containing LHS.  */
+
+static gimple
+follow_ssa_with_commutative_ops (tree arg, tree lhs)
+{
+  gimple stmt;
+
+  if (TREE_CODE (arg) != SSA_NAME)
+    return NULL;
+
+  stmt = SSA_NAME_DEF_STMT (arg);
+
+  if (gimple_code (stmt) == GIMPLE_PHI)
+    {
+      if (phi_contains_arg (stmt, lhs))
+       return stmt;
+      return NULL;
+    }
+
+  if (gimple_num_ops (stmt) == 2)
+    return follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt), lhs);
+
+  if (is_reduction_operation_p (stmt))
+    {
+      gimple res = follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt), lhs);
+
+      return res ? res :
+       follow_ssa_with_commutative_ops (gimple_assign_rhs2 (stmt), lhs);
+    }
+
+  return NULL;
+}
+
+/* Detect commutative and associative scalar reductions starting at
+   the STMT.  */
+
+static gimple
+detect_commutative_reduction_arg (tree lhs, gimple stmt, tree arg,
+                                 VEC (gimple, heap) **in,
+                                 VEC (gimple, heap) **out)
+{
+  gimple phi = follow_ssa_with_commutative_ops (arg, lhs);
+
+  if (phi)
+    {
+      VEC_safe_push (gimple, heap, *in, stmt);
+      VEC_safe_push (gimple, heap, *out, stmt);
+      return phi;
+    }
+
+  return NULL;
+}
+
+/* Detect commutative and associative scalar reductions starting at
+   the STMT.  */
+
+static gimple
+detect_commutative_reduction_assign (gimple stmt, VEC (gimple, heap) **in,
+                                    VEC (gimple, heap) **out)
+{
+  tree lhs = gimple_assign_lhs (stmt);
+
+  if (gimple_num_ops (stmt) == 2)
+    return detect_commutative_reduction_arg (lhs, stmt,
+                                            gimple_assign_rhs1 (stmt),
+                                            in, out);
+
+  if (is_reduction_operation_p (stmt))
+    {
+      gimple 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),
+                                           in, out);
+    }
+
+  return NULL;
+}
+
+/* Return a loop phi node that corresponds to a reduction containing LHS.  */
+
+static gimple
+follow_inital_value_to_phi (tree arg, tree lhs)
+{
+  gimple stmt;
+
+  if (!arg || TREE_CODE (arg) != SSA_NAME)
+    return NULL;
+
+  stmt = SSA_NAME_DEF_STMT (arg);
+
+  if (gimple_code (stmt) == GIMPLE_PHI
+      && phi_contains_arg (stmt, lhs))
+    return stmt;
+
+  return NULL;
+}
+
+
+/* Return the argument of the loop PHI that is the inital value coming
+   from outside the loop.  */
+
+static edge
+edge_initial_value_for_loop_phi (gimple phi)
+{
+  size_t i;
+
+  for (i = 0; i < gimple_phi_num_args (phi); i++)
+    {
+      edge e = gimple_phi_arg_edge (phi, i);
+
+      if (loop_depth (e->src->loop_father)
+         < loop_depth (e->dest->loop_father))
+       return e;
+    }
+
+  return NULL;
+}
+
+/* Return the argument of the loop PHI that is the inital value coming
+   from outside the loop.  */
+
+static tree
+initial_value_for_loop_phi (gimple phi)
+{
+  size_t i;
+
+  for (i = 0; i < gimple_phi_num_args (phi); i++)
+    {
+      edge e = gimple_phi_arg_edge (phi, i);
+
+      if (loop_depth (e->src->loop_father)
+         < loop_depth (e->dest->loop_father))
+       return gimple_phi_arg_def (phi, i);
+    }
+
+  return NULL_TREE;
+}
+
+/* Detect commutative and associative scalar reductions starting at
+   the loop closed phi node CLOSE_PHI.  */
+
+static gimple
+detect_commutative_reduction (gimple stmt, VEC (gimple, heap) **in,
+                             VEC (gimple, heap) **out)
+{
+  if (scalar_close_phi_node_p (stmt))
+    {
+      tree arg = gimple_phi_arg_def (stmt, 0);
+      gimple def = SSA_NAME_DEF_STMT (arg);
+      gimple loop_phi = detect_commutative_reduction (def, in, out);
+
+      if (loop_phi)
+       {
+         tree lhs = gimple_phi_result (stmt);
+         tree init = initial_value_for_loop_phi (loop_phi);
+         gimple phi = follow_inital_value_to_phi (init, lhs);
+
+         VEC_safe_push (gimple, heap, *in, loop_phi);
+         VEC_safe_push (gimple, heap, *out, stmt);
+         return phi;
+       }
+      else
+       return NULL;
+    }
+
+  if (gimple_code (stmt) == GIMPLE_ASSIGN)
+    return detect_commutative_reduction_assign (stmt, in, out);
+
+  return NULL;
+}
+
+/* Translate the scalar reduction statement STMT to an array RED
+   knowing that its recursive phi node is LOOP_PHI.  */
+
+static void
+translate_scalar_reduction_to_array_for_stmt (tree red, gimple stmt,
+                                             gimple loop_phi)
+{
+  basic_block bb = gimple_bb (stmt);
+  gimple_stmt_iterator insert_gsi = gsi_after_labels (bb);
+  tree res = gimple_phi_result (loop_phi);
+  gimple assign = gimple_build_assign (res, red);
+
+  gsi_insert_before (&insert_gsi, assign, GSI_SAME_STMT);
+
+  assign = gimple_build_assign (red, gimple_assign_lhs (stmt));
+  insert_gsi = gsi_for_stmt (stmt);
+  gsi_insert_after (&insert_gsi, assign, GSI_SAME_STMT);
+}
+
+/* Insert the assignment "result (CLOSE_PHI) = RED".  */
+
+static void
+insert_copyout (tree red, gimple close_phi)
+{
+  tree res = gimple_phi_result (close_phi);
+  basic_block bb = gimple_bb (close_phi);
+  gimple_stmt_iterator insert_gsi = gsi_after_labels (bb);
+  gimple assign = gimple_build_assign (res, red);
+
+  gsi_insert_before (&insert_gsi, assign, GSI_SAME_STMT);
+}
+
+/* Insert the assignment "RED = initial_value (LOOP_PHI)".  */
+
+static void
+insert_copyin (tree red, gimple loop_phi)
+{
+  gimple_seq stmts;
+  tree init = initial_value_for_loop_phi (loop_phi);
+  edge e = edge_initial_value_for_loop_phi (loop_phi);
+  basic_block bb = e->src;
+  gimple_stmt_iterator insert_gsi = gsi_last_bb (bb);
+  tree expr = build2 (MODIFY_EXPR, TREE_TYPE (init), red, init);
+
+  force_gimple_operand (expr, &stmts, true, NULL);
+  gsi_insert_seq_before (&insert_gsi, stmts, GSI_SAME_STMT);
+}
+
+/* Rewrite out of SSA the reduction described by the loop phi nodes
+   IN, and the close phi nodes OUT.  IN and OUT are structured by loop
+   levels like this:
+
+   IN: stmt, loop_n, ..., loop_0
+   OUT: stmt, close_n, ..., close_0
+
+   the first element is the reduction statement, and the next elements
+   are the loop and close phi nodes of each of the outer loops.  */
+
+static void
+translate_scalar_reduction_to_array (VEC (gimple, heap) *in,
+                                    VEC (gimple, heap) *out,
+                                    sbitmap reductions)
+{
+  unsigned int i;
+  gimple loop_phi;
+  tree red;
+  gimple_stmt_iterator gsi;
+
+  for (i = 0; VEC_iterate (gimple, in, i, loop_phi); i++)
+    {
+      gimple close_phi = VEC_index (gimple, out, i);
+
+      if (i == 0)
+       {
+         gimple stmt = loop_phi;
+         basic_block bb = split_reduction_stmt (stmt);
+
+         SET_BIT (reductions, bb->index);
+         gcc_assert (close_phi == loop_phi);
+
+         red = create_zero_dim_array (gimple_assign_lhs (stmt));
+         translate_scalar_reduction_to_array_for_stmt
+           (red, stmt, VEC_index (gimple, in, 1));
+         continue;
+       }
+
+      if (i == VEC_length (gimple, in) - 1)
+       {
+         insert_copyout (red, close_phi);
+         insert_copyin (red, loop_phi);
+       }
+
+      gsi = gsi_for_phi_node (loop_phi);
+      remove_phi_node (&gsi, false);
+
+      gsi = gsi_for_phi_node (close_phi);
+      remove_phi_node (&gsi, false);
+    }
+}
+
+/* Rewrites out of SSA a commutative reduction at CLOSE_PHI.  */
+
+static void
+rewrite_commutative_reductions_out_of_ssa_close_phi (gimple close_phi,
+                                                    sbitmap reductions)
+{
+  VEC (gimple, heap) *in = VEC_alloc (gimple, heap, 10);
+  VEC (gimple, heap) *out = VEC_alloc (gimple, heap, 10);
+
+  detect_commutative_reduction (close_phi, &in, &out);
+  if (VEC_length (gimple, in) > 0)
+    translate_scalar_reduction_to_array (in, out, reductions);
+
+  VEC_free (gimple, heap, in);
+  VEC_free (gimple, heap, out);
+}
+
+/* Rewrites all the commutative reductions from LOOP out of SSA.  */
+
+static void
+rewrite_commutative_reductions_out_of_ssa_loop (loop_p loop,
+                                               sbitmap reductions)
+{
+  gimple_stmt_iterator gsi;
+  edge exit = single_exit (loop);
+
+  if (!exit)
+    return;
+
+  for (gsi = gsi_start_phis (exit->dest); !gsi_end_p (gsi); gsi_next (&gsi))
+    rewrite_commutative_reductions_out_of_ssa_close_phi (gsi_stmt (gsi),
+                                                        reductions);
+}
+
+/* Rewrites all the commutative reductions from SCOP out of SSA.  */
+
+static void
+rewrite_commutative_reductions_out_of_ssa (sese region, sbitmap reductions)
+{
+  loop_iterator li;
+  loop_p loop;
+
+  FOR_EACH_LOOP (li, loop, 0)
+    if (loop_in_sese_p (loop, region))
+      rewrite_commutative_reductions_out_of_ssa_loop (loop, reductions);
+}
+
 /* Builds the polyhedral representation for a SESE region.  */
 
 bool
 build_poly_scop (scop_p scop)
 {
   sese region = SCOP_REGION (scop);
+  sbitmap reductions = sbitmap_alloc (last_basic_block * 2);
+
+  sbitmap_zero (reductions);
+  rewrite_commutative_reductions_out_of_ssa (region, reductions);
   rewrite_reductions_out_of_ssa (scop);
-  build_scop_bbs (scop);
+  build_scop_bbs (scop, reductions);
+  sbitmap_free (reductions);
 
   /* FIXME: This restriction is needed to avoid a problem in CLooG.
      Once CLooG is fixed, remove this guard.  Anyways, it makes no
@@ -2081,6 +2630,7 @@ build_poly_scop (scop_p scop)
   build_scop_context (scop);
 
   add_conditions_to_constraints (scop);
+  scop_to_lst (scop);
   build_scop_scattering (scop);
   build_scop_drs (scop);