OSDN Git Service

Fix grammar/spelling.
[pf3gnuchains/gcc-fork.git] / gcc / graphite-sese-to-poly.c
index badd25f..9167a72 100644 (file)
@@ -1,5 +1,5 @@
 /* Conversion of SESE regions to Polyhedra.
-   Copyright (C) 2009, 2010 Free Software Foundation, Inc.
+   Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
    Contributed by Sebastian Pop <sebastian.pop@amd.com>.
 
 This file is part of GCC.
@@ -21,32 +21,19 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
-#include "tm.h"
-#include "ggc.h"
-#include "tree.h"
-#include "rtl.h"
-#include "basic-block.h"
-#include "diagnostic.h"
 #include "tree-flow.h"
 #include "tree-dump.h"
-#include "timevar.h"
 #include "cfgloop.h"
 #include "tree-chrec.h"
 #include "tree-data-ref.h"
 #include "tree-scalar-evolution.h"
-#include "tree-pass.h"
 #include "domwalk.h"
-#include "value-prof.h"
-#include "pointer-set.h"
-#include "gimple.h"
 #include "sese.h"
 
 #ifdef HAVE_cloog
 #include "ppl_c.h"
 #include "graphite-ppl.h"
-#include "graphite.h"
 #include "graphite-poly.h"
-#include "graphite-scop-detection.h"
 #include "graphite-sese-to-poly.h"
 
 /* Returns the index of the PHI argument defined in the outermost
@@ -203,8 +190,7 @@ free_data_refs_aux (VEC (data_reference_p, heap) *datarefs)
       {
        base_alias_pair *bap = (base_alias_pair *)(dr->aux);
 
-       if (bap->alias_set)
-         free (bap->alias_set);
+       free (bap->alias_set);
 
        free (bap);
        dr->aux = NULL;
@@ -254,6 +240,32 @@ free_scops (VEC (scop_p, heap) *scops)
   VEC_free (scop_p, heap, scops);
 }
 
+/* Same as outermost_loop_in_sese, returns the outermost loop
+   containing BB in REGION, but makes sure that the returned loop
+   belongs to the REGION, and so this returns the first loop in the
+   REGION when the loop containing BB does not belong to REGION.  */
+
+static loop_p
+outermost_loop_in_sese_1 (sese region, basic_block bb)
+{
+  loop_p nest = outermost_loop_in_sese (region, bb);
+
+  if (loop_in_sese_p (nest, region))
+    return nest;
+
+  /* When the basic block BB does not belong to a loop in the region,
+     return the first loop in the region.  */
+  nest = nest->inner;
+  while (nest)
+    if (loop_in_sese_p (nest, region))
+      break;
+    else
+      nest = nest->next;
+
+  gcc_assert (nest);
+  return nest;
+}
+
 /* Generates a polyhedral black box only if the bb contains interesting
    information.  */
 
@@ -261,14 +273,23 @@ static gimple_bb_p
 try_generate_gimple_bb (scop_p scop, basic_block bb)
 {
   VEC (data_reference_p, heap) *drs = VEC_alloc (data_reference_p, heap, 5);
-  loop_p nest = outermost_loop_in_sese (SCOP_REGION (scop), bb);
+  sese region = SCOP_REGION (scop);
+  loop_p nest = outermost_loop_in_sese_1 (region, bb);
   gimple_stmt_iterator gsi;
 
   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
     {
       gimple stmt = gsi_stmt (gsi);
-      if (!is_gimple_debug (stmt))
-       graphite_find_data_references_in_stmt (nest, stmt, &drs);
+      loop_p loop;
+
+      if (is_gimple_debug (stmt))
+       continue;
+
+      loop = loop_containing_stmt (stmt);
+      if (!loop_in_sese_p (loop, region))
+       loop = nest;
+
+      graphite_find_data_references_in_stmt (nest, loop, stmt, &drs);
     }
 
   return new_gimple_bb (bb, drs);
@@ -612,7 +633,7 @@ scan_tree_for_params_right_scev (sese s, tree e, int var,
       gcc_assert (TREE_CODE (e) == INTEGER_CST);
 
       mpz_init (val);
-      mpz_set_si (val, int_cst_value (e));
+      tree_int_to_gmp (e, val);
       add_value_to_dim (l, expr, val);
       mpz_clear (val);
     }
@@ -626,16 +647,13 @@ scan_tree_for_params_int (tree cst, ppl_Linear_Expression_t expr, mpz_t k)
 {
   mpz_t val;
   ppl_Coefficient_t coef;
-  int v = int_cst_value (cst);
+  tree type = TREE_TYPE (cst);
 
   mpz_init (val);
-  mpz_set_si (val, 0);
 
   /* Necessary to not get "-1 = 2^n - 1". */
-  if (v < 0)
-    mpz_sub_ui (val, val, -v);
-  else
-    mpz_add_ui (val, val, v);
+  mpz_set_double_int (val, double_int_sext (tree_to_double_int (cst),
+                                           TYPE_PRECISION (type)), false);
 
   mpz_mul (val, val, k);
   ppl_new_Coefficient (&coef);
@@ -713,7 +731,7 @@ scan_tree_for_params (sese s, tree e, ppl_Linear_Expression_t c,
              mpz_t val;
              gcc_assert (host_integerp (TREE_OPERAND (e, 1), 0));
              mpz_init (val);
-             mpz_set_si (val, int_cst_value (TREE_OPERAND (e, 1)));
+             tree_int_to_gmp (TREE_OPERAND (e, 1), val);
              mpz_mul (val, val, k);
              scan_tree_for_params (s, TREE_OPERAND (e, 0), c, val);
              mpz_clear (val);
@@ -728,7 +746,7 @@ scan_tree_for_params (sese s, tree e, ppl_Linear_Expression_t c,
              mpz_t val;
              gcc_assert (host_integerp (TREE_OPERAND (e, 0), 0));
              mpz_init (val);
-             mpz_set_si (val, int_cst_value (TREE_OPERAND (e, 0)));
+             tree_int_to_gmp (TREE_OPERAND (e, 0), val);
              mpz_mul (val, val, k);
              scan_tree_for_params (s, TREE_OPERAND (e, 1), c, val);
              mpz_clear (val);
@@ -847,6 +865,9 @@ scan_tree_for_params (sese s, tree e, ppl_Linear_Expression_t c,
       scan_tree_for_params (s, TREE_OPERAND (e, 0), c, k);
       break;
 
+    case ADDR_EXPR:
+      break;
+
    default:
       gcc_unreachable ();
       break;
@@ -1071,7 +1092,7 @@ build_loop_iteration_domains (scop_p scop, struct loop *loop,
       scan_tree_for_params (SCOP_REGION (scop), nb_iters, ub_expr, one);
       mpz_clear (one);
 
-      if (estimated_loop_iterations (loop, true, &nit))
+      if (max_stmt_executions (loop, true, &nit))
        add_upper_bounds_from_estimated_nit (scop, nit, dim, ub_expr);
 
       /* loop_i <= expr_nb_iters */
@@ -1614,10 +1635,13 @@ pdr_add_data_dimensions (ppl_Polyhedron_t accesses, data_reference_p dr,
       /* subscript - low >= 0 */
       if (host_integerp (low, 0))
        {
+         tree minus_low;
+
          ppl_new_Linear_Expression_with_dimension (&expr, accessp_nb_dims);
          ppl_set_coef (expr, subscript, 1);
 
-         ppl_set_inhomogeneous (expr, -int_cst_value (low));
+         minus_low = fold_build1 (NEGATE_EXPR, TREE_TYPE (low), low);
+         ppl_set_inhomogeneous_tree (expr, minus_low);
 
          ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
          ppl_Polyhedron_add_constraint (accesses, cstr);
@@ -1637,7 +1661,7 @@ pdr_add_data_dimensions (ppl_Polyhedron_t accesses, data_reference_p dr,
          ppl_new_Linear_Expression_with_dimension (&expr, accessp_nb_dims);
          ppl_set_coef (expr, subscript, -1);
 
-         ppl_set_inhomogeneous (expr, int_cst_value (high));
+         ppl_set_inhomogeneous_tree (expr, high);
 
          ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
          ppl_Polyhedron_add_constraint (accesses, cstr);
@@ -1696,7 +1720,7 @@ write_alias_graph_to_ascii_dimacs (FILE *file, char *comment,
 
   FOR_EACH_VEC_ELT (data_reference_p, drs, i, dr1)
     for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
-      if (dr_may_alias_p (dr1, dr2))
+      if (dr_may_alias_p (dr1, dr2, true))
        edge_num++;
 
   fprintf (file, "$\n");
@@ -1708,7 +1732,7 @@ write_alias_graph_to_ascii_dimacs (FILE *file, char *comment,
 
   FOR_EACH_VEC_ELT (data_reference_p, drs, i, dr1)
     for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
-      if (dr_may_alias_p (dr1, dr2))
+      if (dr_may_alias_p (dr1, dr2, true))
        fprintf (file, "e %d %d\n", i + 1, j + 1);
 
   return true;
@@ -1738,7 +1762,7 @@ write_alias_graph_to_ascii_dot (FILE *file, char *comment,
 
   FOR_EACH_VEC_ELT (data_reference_p, drs, i, dr1)
     for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
-      if (dr_may_alias_p (dr1, dr2))
+      if (dr_may_alias_p (dr1, dr2, true))
        fprintf (file, "n%d n%d\n", i, j);
 
   return true;
@@ -1764,7 +1788,7 @@ write_alias_graph_to_ascii_ecc (FILE *file, char *comment,
 
   FOR_EACH_VEC_ELT (data_reference_p, drs, i, dr1)
     for (j = i + 1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
-      if (dr_may_alias_p (dr1, dr2))
+      if (dr_may_alias_p (dr1, dr2, true))
        fprintf (file, "%d %d\n", i, j);
 
   return true;
@@ -1800,7 +1824,7 @@ build_alias_set_optimal_p (VEC (data_reference_p, heap) *drs)
 
   FOR_EACH_VEC_ELT (data_reference_p, drs, i, dr1)
     for (j = i+1; VEC_iterate (data_reference_p, drs, j, dr2); j++)
-      if (dr_may_alias_p (dr1, dr2))
+      if (dr_may_alias_p (dr1, dr2, true))
        {
          add_edge (g, i, j);
          add_edge (g, j, i);
@@ -1956,36 +1980,6 @@ dump_alias_graphs (VEC (data_reference_p, heap) *drs)
     }
 }
 
-/* Recompute all the data references of BB and add them to the
-   GBB_DATA_REFS vector.  */
-
-static void
-analyze_drs (scop_p scop, basic_block bb)
-{
-  loop_p nest;
-  poly_bb_p pbb;
-  gimple_stmt_iterator gsi;
-  gimple_bb_p gbb;
-
-  if (!bb_in_sese_p (bb, SCOP_REGION (scop)))
-    return;
-
-  nest = outermost_loop_in_sese (SCOP_REGION (scop), bb);
-  pbb = pbb_from_bb (bb);
-  gbb = PBB_BLACK_BOX (pbb);
-
-  VEC_free (data_reference_p, heap, GBB_DATA_REFS (gbb));
-  GBB_DATA_REFS (gbb) = VEC_alloc (data_reference_p, heap, 3);
-
-  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
-    {
-      gimple stmt = gsi_stmt (gsi);
-      if (!is_gimple_debug (stmt))
-       graphite_find_data_references_in_stmt (nest, stmt,
-                                              &GBB_DATA_REFS (gbb));
-    }
-}
-
 /* Build data references in SCOP.  */
 
 static void
@@ -1999,14 +1993,12 @@ build_scop_drs (scop_p scop)
   /* Remove all the PBBs that do not have data references: these basic
      blocks are not handled in the polyhedral representation.  */
   for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
-    {
-      analyze_drs (scop, GBB_BB (PBB_BLACK_BOX (pbb)));
-      if (VEC_empty (data_reference_p, GBB_DATA_REFS (PBB_BLACK_BOX (pbb))))
-       {
-         VEC_ordered_remove (poly_bb_p, SCOP_BBS (scop), i);
-         i--;
-       }
-    }
+    if (VEC_empty (data_reference_p, GBB_DATA_REFS (PBB_BLACK_BOX (pbb))))
+      {
+       free_gimple_bb (PBB_BLACK_BOX (pbb));
+       VEC_ordered_remove (poly_bb_p, SCOP_BBS (scop), i);
+       i--;
+      }
 
   FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
     for (j = 0; VEC_iterate (data_reference_p,
@@ -2051,21 +2043,82 @@ gsi_for_phi_node (gimple stmt)
   return psi;
 }
 
+/* Analyze all the data references of STMTS and add them to the
+   GBB_DATA_REFS vector of BB.  */
+
+static void
+analyze_drs_in_stmts (scop_p scop, basic_block bb, VEC (gimple, heap) *stmts)
+{
+  loop_p nest;
+  gimple_bb_p gbb;
+  gimple stmt;
+  int i;
+  sese region = SCOP_REGION (scop);
+
+  if (!bb_in_sese_p (bb, region))
+    return;
+
+  nest = outermost_loop_in_sese_1 (region, bb);
+  gbb = gbb_from_bb (bb);
+
+  FOR_EACH_VEC_ELT (gimple, stmts, i, stmt)
+    {
+      loop_p loop;
+
+      if (is_gimple_debug (stmt))
+       continue;
+
+      loop = loop_containing_stmt (stmt);
+      if (!loop_in_sese_p (loop, region))
+       loop = nest;
+
+      graphite_find_data_references_in_stmt (nest, loop, stmt,
+                                            &GBB_DATA_REFS (gbb));
+    }
+}
+
+/* Insert STMT at the end of the STMTS sequence and then insert the
+   statements from STMTS at INSERT_GSI and call analyze_drs_in_stmts
+   on STMTS.  */
+
+static void
+insert_stmts (scop_p scop, gimple stmt, gimple_seq stmts,
+             gimple_stmt_iterator insert_gsi)
+{
+  gimple_stmt_iterator gsi;
+  VEC (gimple, heap) *x = VEC_alloc (gimple, heap, 3);
+
+  if (!stmts)
+    stmts = gimple_seq_alloc ();
+
+  gsi = gsi_last (stmts);
+  gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
+  for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
+    VEC_safe_push (gimple, heap, x, gsi_stmt (gsi));
+
+  gsi_insert_seq_before (&insert_gsi, stmts, GSI_SAME_STMT);
+  analyze_drs_in_stmts (scop, gsi_bb (insert_gsi), x);
+  VEC_free (gimple, heap, x);
+}
+
 /* Insert the assignment "RES := EXPR" just after AFTER_STMT.  */
 
 static void
-insert_out_of_ssa_copy (tree res, tree expr, gimple after_stmt)
+insert_out_of_ssa_copy (scop_p scop, tree res, tree expr, gimple after_stmt)
 {
   gimple_seq stmts;
   gimple_stmt_iterator si;
   gimple_stmt_iterator gsi;
   tree var = force_gimple_operand (expr, &stmts, true, NULL_TREE);
   gimple stmt = gimple_build_assign (res, var);
+  VEC (gimple, heap) *x = VEC_alloc (gimple, heap, 3);
 
   if (!stmts)
     stmts = gimple_seq_alloc ();
   si = gsi_last (stmts);
   gsi_insert_after (&si, stmt, GSI_NEW_STMT);
+  for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
+    VEC_safe_push (gimple, heap, x, gsi_stmt (gsi));
 
   if (gimple_code (after_stmt) == GIMPLE_PHI)
     {
@@ -2077,6 +2130,9 @@ insert_out_of_ssa_copy (tree res, tree expr, gimple after_stmt)
       gsi = gsi_for_stmt (after_stmt);
       gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
     }
+
+  analyze_drs_in_stmts (scop, gimple_bb (after_stmt), x);
+  VEC_free (gimple, heap, x);
 }
 
 /* Creates a poly_bb_p for basic_block BB from the existing PBB.  */
@@ -2095,9 +2151,11 @@ new_pbb_from_pbb (scop_p scop, poly_bb_p pbb, basic_block bb)
     if (VEC_index (poly_bb_p, SCOP_BBS (scop), index) == pbb)
       break;
 
+  if (PBB_DOMAIN (pbb))
+    ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
+      (&PBB_DOMAIN (pbb1), PBB_DOMAIN (pbb));
+
   GBB_PBB (gbb1) = pbb1;
-  ppl_new_Pointset_Powerset_C_Polyhedron_from_Pointset_Powerset_C_Polyhedron
-    (&PBB_DOMAIN (pbb1), PBB_DOMAIN (pbb));
   GBB_CONDITIONS (gbb1) = VEC_copy (gimple, heap, GBB_CONDITIONS (gbb));
   GBB_CONDITION_CASES (gbb1) = VEC_copy (gimple, heap, GBB_CONDITION_CASES (gbb));
   VEC_safe_insert (poly_bb_p, heap, SCOP_BBS (scop), index + 1, pbb1);
@@ -2113,12 +2171,16 @@ insert_out_of_ssa_copy_on_edge (scop_p scop, edge e, tree res, tree expr)
   tree var = force_gimple_operand (expr, &stmts, true, NULL_TREE);
   gimple stmt = gimple_build_assign (res, var);
   basic_block bb;
+  VEC (gimple, heap) *x = VEC_alloc (gimple, heap, 3);
 
   if (!stmts)
     stmts = gimple_seq_alloc ();
 
   gsi = gsi_last (stmts);
   gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
+  for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
+    VEC_safe_push (gimple, heap, x, gsi_stmt (gsi));
+
   gsi_insert_seq_on_edge (e, stmts);
   gsi_commit_edge_inserts ();
   bb = gimple_bb (stmt);
@@ -2128,6 +2190,9 @@ insert_out_of_ssa_copy_on_edge (scop_p scop, edge e, tree res, tree expr)
 
   if (!gbb_from_bb (bb))
     new_pbb_from_pbb (scop, pbb_from_bb (e->src), bb);
+
+  analyze_drs_in_stmts (scop, bb, x);
+  VEC_free (gimple, heap, x);
 }
 
 /* Creates a zero dimension array of the same type as VAR.  */
@@ -2269,7 +2334,7 @@ rewrite_close_phi_out_of_ssa (scop_p scop, gimple_stmt_iterator *psi)
       stmt = gimple_build_assign (res, zero_dim_array);
 
       if (TREE_CODE (arg) == SSA_NAME)
-       insert_out_of_ssa_copy (zero_dim_array, arg,
+       insert_out_of_ssa_copy (scop, zero_dim_array, arg,
                                SSA_NAME_DEF_STMT (arg));
       else
        insert_out_of_ssa_copy_on_edge (scop, single_pred_edge (bb),
@@ -2277,8 +2342,9 @@ rewrite_close_phi_out_of_ssa (scop_p scop, gimple_stmt_iterator *psi)
     }
 
   remove_phi_node (psi, false);
-  gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
   SSA_NAME_DEF_STMT (res) = stmt;
+
+  insert_stmts (scop, stmt, NULL, gsi_after_labels (bb));
 }
 
 /* Rewrite out of SSA the reduction phi node at PSI by creating a zero
@@ -2293,7 +2359,6 @@ rewrite_phi_out_of_ssa (scop_p scop, gimple_stmt_iterator *psi)
   tree res = gimple_phi_result (phi);
   tree var = SSA_NAME_VAR (res);
   tree zero_dim_array = create_zero_dim_array (var, "phi_out_of_ssa");
-  gimple_stmt_iterator gsi;
   gimple stmt;
   gimple_seq stmts;
 
@@ -2306,7 +2371,7 @@ rewrite_phi_out_of_ssa (scop_p scop, gimple_stmt_iterator *psi)
         pattern matching of the vectorizer.  */
       if (TREE_CODE (arg) == SSA_NAME
          && e->src == bb->loop_father->latch)
-       insert_out_of_ssa_copy (zero_dim_array, arg,
+       insert_out_of_ssa_copy (scop, zero_dim_array, arg,
                                SSA_NAME_DEF_STMT (arg));
       else
        insert_out_of_ssa_copy_on_edge (scop, e, zero_dim_array, arg);
@@ -2314,18 +2379,11 @@ rewrite_phi_out_of_ssa (scop_p scop, gimple_stmt_iterator *psi)
 
   var = force_gimple_operand (zero_dim_array, &stmts, true, NULL_TREE);
 
-  if (!stmts)
-    stmts = gimple_seq_alloc ();
-
   stmt = gimple_build_assign (res, var);
   remove_phi_node (psi, false);
   SSA_NAME_DEF_STMT (res) = stmt;
 
-  gsi = gsi_last (stmts);
-  gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
-
-  gsi = gsi_after_labels (bb);
-  gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
+  insert_stmts (scop, stmt, stmts, gsi_after_labels (bb));
 }
 
 /* Rewrite the degenerate phi node at position PSI from the degenerate
@@ -2395,7 +2453,7 @@ rewrite_reductions_out_of_ssa (scop_p scop)
    read from ZERO_DIM_ARRAY.  */
 
 static void
-rewrite_cross_bb_scalar_dependence (tree zero_dim_array,
+rewrite_cross_bb_scalar_dependence (scop_p scop, tree zero_dim_array,
                                    tree def, gimple use_stmt)
 {
   tree var = SSA_NAME_VAR (def);
@@ -2403,14 +2461,11 @@ rewrite_cross_bb_scalar_dependence (tree zero_dim_array,
   tree name = make_ssa_name (var, name_stmt);
   ssa_op_iter iter;
   use_operand_p use_p;
-  gimple_stmt_iterator gsi;
 
   gcc_assert (gimple_code (use_stmt) != GIMPLE_PHI);
 
   gimple_assign_set_lhs (name_stmt, name);
-
-  gsi = gsi_for_stmt (use_stmt);
-  gsi_insert_before (&gsi, name_stmt, GSI_NEW_STMT);
+  insert_stmts (scop, name_stmt, NULL, gsi_for_stmt (use_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))
@@ -2534,12 +2589,12 @@ rewrite_cross_bb_scalar_deps (scop_p scop, gimple_stmt_iterator *gsi)
          {
            zero_dim_array = create_zero_dim_array
              (SSA_NAME_VAR (def), "Cross_BB_scalar_dependence");
-           insert_out_of_ssa_copy (zero_dim_array, def,
+           insert_out_of_ssa_copy (scop, zero_dim_array, def,
                                    SSA_NAME_DEF_STMT (def));
            gsi_next (gsi);
          }
 
-       rewrite_cross_bb_scalar_dependence (zero_dim_array,
+       rewrite_cross_bb_scalar_dependence (scop, zero_dim_array,
                                            def, use_stmt);
       }
 
@@ -2552,13 +2607,12 @@ static void
 rewrite_cross_bb_scalar_deps_out_of_ssa (scop_p scop)
 {
   basic_block bb;
-  basic_block exit;
   gimple_stmt_iterator psi;
   sese region = SCOP_REGION (scop);
   bool changed = false;
 
   /* Create an extra empty BB after the scop.  */
-  exit = split_edge (SESE_EXIT (region));
+  split_edge (SESE_EXIT (region));
 
   FOR_EACH_BB (bb)
     if (bb_in_sese_p (bb, region))
@@ -2626,11 +2680,16 @@ split_reduction_stmt (scop_p scop, gimple stmt)
 {
   basic_block bb = gimple_bb (stmt);
   poly_bb_p pbb = pbb_from_bb (bb);
+  gimple_bb_p gbb = gbb_from_bb (bb);
   edge e1;
+  int i;
+  data_reference_p dr;
 
   /* 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)
+  if (nb_data_writes_in_bb (bb) == 0
+      /* Or if we have already marked BB as a reduction.  */
+      || PBB_IS_REDUCTION (pbb_from_bb (bb)))
     return bb;
 
   e1 = split_pbb (scop, pbb, bb, stmt);
@@ -2644,6 +2703,22 @@ split_reduction_stmt (scop_p scop, gimple stmt)
       e1 = split_pbb (scop, pbb, bb, gsi_stmt (gsi));
     }
 
+  /* A part of the data references will end in a different basic block
+     after the split: move the DRs from the original GBB to the newly
+     created GBB1.  */
+  FOR_EACH_VEC_ELT (data_reference_p, GBB_DATA_REFS (gbb), i, dr)
+    {
+      basic_block bb1 = gimple_bb (DR_STMT (dr));
+
+      if (bb1 != bb)
+       {
+         gimple_bb_p gbb1 = gbb_from_bb (bb1);
+         VEC_safe_push (data_reference_p, heap, GBB_DATA_REFS (gbb1), dr);
+         VEC_ordered_remove (data_reference_p, GBB_DATA_REFS (gbb), i);
+         i--;
+       }
+    }
+
   return e1->dest;
 }
 
@@ -2822,18 +2897,42 @@ initial_value_for_loop_phi (gimple phi)
   return NULL_TREE;
 }
 
-/* Detect commutative and associative scalar reductions starting at
-   the loop closed phi node STMT.  Return the phi node of the
-   reduction cycle, or NULL.  */
+/* Returns true when DEF is used outside the reduction cycle of
+   LOOP_PHI.  */
+
+static bool
+used_outside_reduction (tree def, gimple loop_phi)
+{
+  use_operand_p use_p;
+  imm_use_iterator imm_iter;
+  loop_p loop = loop_containing_stmt (loop_phi);
+
+  /* In LOOP, DEF should be used only in LOOP_PHI.  */
+  FOR_EACH_IMM_USE_FAST (use_p, imm_iter, def)
+    {
+      gimple stmt = USE_STMT (use_p);
+
+      if (stmt != loop_phi
+         && !is_gimple_debug (stmt)
+         && flow_bb_inside_loop_p (loop, gimple_bb (stmt)))
+       return true;
+    }
+
+  return false;
+}
+
+/* Detect commutative and associative scalar reductions belonging to
+   the SCOP starting at the loop closed phi node STMT.  Return the phi
+   node of the reduction cycle, or NULL.  */
 
 static gimple
-detect_commutative_reduction (gimple stmt, VEC (gimple, heap) **in,
+detect_commutative_reduction (scop_p scop, 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, loop_phi;
+      gimple def, loop_phi, phi, close_phi = stmt;
+      tree init, lhs, arg = gimple_phi_arg_def (close_phi, 0);
 
       if (TREE_CODE (arg) != SSA_NAME)
        return NULL;
@@ -2841,23 +2940,24 @@ detect_commutative_reduction (gimple stmt, VEC (gimple, heap) **in,
       /* Note that loop close phi nodes should have a single argument
         because we translated the representation into a canonical form
         before Graphite: see canonicalize_loop_closed_ssa_form.  */
-      gcc_assert (gimple_phi_num_args (stmt) == 1);
+      gcc_assert (gimple_phi_num_args (close_phi) == 1);
 
       def = SSA_NAME_DEF_STMT (arg);
-      loop_phi = detect_commutative_reduction (def, in, out);
+      if (!stmt_in_sese_p (def, SCOP_REGION (scop))
+         || !(loop_phi = detect_commutative_reduction (scop, def, in, out)))
+       return NULL;
 
-      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);
+      lhs = gimple_phi_result (close_phi);
+      init = initial_value_for_loop_phi (loop_phi);
+      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
+      if (phi && (used_outside_reduction (lhs, phi)
+                 || !has_single_use (gimple_phi_result (phi))))
        return NULL;
+
+      VEC_safe_push (gimple, heap, *in, loop_phi);
+      VEC_safe_push (gimple, heap, *out, close_phi);
+      return phi;
     }
 
   if (gimple_code (stmt) == GIMPLE_ASSIGN)
@@ -2870,18 +2970,19 @@ detect_commutative_reduction (gimple stmt, VEC (gimple, heap) **in,
    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)
+translate_scalar_reduction_to_array_for_stmt (scop_p scop, tree red,
+                                             gimple stmt, gimple loop_phi)
 {
   tree res = gimple_phi_result (loop_phi);
-  gimple assign = gimple_build_assign (res, red);
-  gimple_stmt_iterator insert_gsi = gsi_after_labels (gimple_bb (loop_phi));
+  gimple assign = gimple_build_assign (res, unshare_expr (red));
+  gimple_stmt_iterator gsi;
 
-  gsi_insert_before (&insert_gsi, assign, GSI_SAME_STMT);
+  insert_stmts (scop, assign, NULL, gsi_after_labels (gimple_bb (loop_phi)));
 
-  assign = gimple_build_assign (red, gimple_assign_lhs (stmt));
-  insert_gsi = gsi_for_stmt (stmt);
-  gsi_insert_after (&insert_gsi, assign, GSI_SAME_STMT);
+  assign = gimple_build_assign (unshare_expr (red), gimple_assign_lhs (stmt));
+  gsi = gsi_for_stmt (stmt);
+  gsi_next (&gsi);
+  insert_stmts (scop, assign, NULL, gsi);
 }
 
 /* Removes the PHI node and resets all the debug stmts that are using
@@ -2919,6 +3020,80 @@ remove_phi (gimple phi)
   remove_phi_node (&gsi, false);
 }
 
+/* Helper function for for_each_index.  For each INDEX of the data
+   reference REF, returns true when its indices are valid in the loop
+   nest LOOP passed in as DATA.  */
+
+static bool
+dr_indices_valid_in_loop (tree ref ATTRIBUTE_UNUSED, tree *index, void *data)
+{
+  loop_p loop;
+  basic_block header, def_bb;
+  gimple stmt;
+
+  if (TREE_CODE (*index) != SSA_NAME)
+    return true;
+
+  loop = *((loop_p *) data);
+  header = loop->header;
+  stmt = SSA_NAME_DEF_STMT (*index);
+
+  if (!stmt)
+    return true;
+
+  def_bb = gimple_bb (stmt);
+
+  if (!def_bb)
+    return true;
+
+  return dominated_by_p (CDI_DOMINATORS, header, def_bb);
+}
+
+/* When the result of a CLOSE_PHI is written to a memory location,
+   return a pointer to that memory reference, otherwise return
+   NULL_TREE.  */
+
+static tree
+close_phi_written_to_memory (gimple close_phi)
+{
+  imm_use_iterator imm_iter;
+  use_operand_p use_p;
+  gimple stmt;
+  tree res, def = gimple_phi_result (close_phi);
+
+  FOR_EACH_IMM_USE_FAST (use_p, imm_iter, def)
+    if ((stmt = USE_STMT (use_p))
+       && gimple_code (stmt) == GIMPLE_ASSIGN
+       && (res = gimple_assign_lhs (stmt)))
+      {
+       switch (TREE_CODE (res))
+         {
+         case VAR_DECL:
+         case PARM_DECL:
+         case RESULT_DECL:
+           return res;
+
+         case ARRAY_REF:
+         case MEM_REF:
+           {
+             tree arg = gimple_phi_arg_def (close_phi, 0);
+             loop_p nest = loop_containing_stmt (SSA_NAME_DEF_STMT (arg));
+
+             /* FIXME: this restriction is for id-{24,25}.f and
+                could be handled by duplicating the computation of
+                array indices before the loop of the close_phi.  */
+             if (for_each_index (&res, dr_indices_valid_in_loop, &nest))
+               return res;
+           }
+           /* Fallthru.  */
+
+         default:
+           continue;
+         }
+      }
+  return NULL_TREE;
+}
+
 /* 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:
@@ -2934,9 +3109,9 @@ translate_scalar_reduction_to_array (scop_p scop,
                                     VEC (gimple, heap) *in,
                                     VEC (gimple, heap) *out)
 {
-  unsigned int i;
   gimple loop_phi;
-  tree red = NULL_TREE;
+  unsigned int i = VEC_length (gimple, out) - 1;
+  tree red = close_phi_written_to_memory (VEC_index (gimple, out, i));
 
   FOR_EACH_VEC_ELT (gimple, in, i, loop_phi)
     {
@@ -2950,20 +3125,22 @@ translate_scalar_reduction_to_array (scop_p scop,
          PBB_IS_REDUCTION (pbb) = true;
          gcc_assert (close_phi == loop_phi);
 
-         red = create_zero_dim_array
-           (gimple_assign_lhs (stmt), "Commutative_Associative_Reduction");
+         if (!red)
+           red = create_zero_dim_array
+             (gimple_assign_lhs (stmt), "Commutative_Associative_Reduction");
+
          translate_scalar_reduction_to_array_for_stmt
-           (red, stmt, VEC_index (gimple, in, 1));
+           (scop, red, stmt, VEC_index (gimple, in, 1));
          continue;
        }
 
       if (i == VEC_length (gimple, in) - 1)
        {
-         insert_out_of_ssa_copy (gimple_phi_result (close_phi), red,
-                                 close_phi);
+         insert_out_of_ssa_copy (scop, gimple_phi_result (close_phi),
+                                 unshare_expr (red), close_phi);
          insert_out_of_ssa_copy_on_edge
            (scop, edge_initial_value_for_loop_phi (loop_phi),
-            red, initial_value_for_loop_phi (loop_phi));
+            unshare_expr (red), initial_value_for_loop_phi (loop_phi));
        }
 
       remove_phi (loop_phi);
@@ -2982,8 +3159,8 @@ rewrite_commutative_reductions_out_of_ssa_close_phi (scop_p scop,
   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);
-  res = VEC_length (gimple, in) > 0;
+  detect_commutative_reduction (scop, close_phi, &in, &out);
+  res = VEC_length (gimple, in) > 1;
   if (res)
     translate_scalar_reduction_to_array (scop, in, out);
 
@@ -3042,9 +3219,6 @@ rewrite_commutative_reductions_out_of_ssa (scop_p scop)
     }
 }
 
-/* Java does not initialize long_long_integer_type_node.  */
-#define my_long_long (long_long_integer_type_node ? long_long_integer_type_node : ssizetype)
-
 /* Can all ivs be represented by a signed integer?
    As CLooG might generate negative values in its expressions, signed loop ivs
    are required in the backend. */
@@ -3055,6 +3229,7 @@ scop_ivs_can_be_represented (scop_p scop)
   loop_iterator li;
   loop_p loop;
   gimple_stmt_iterator psi;
+  bool result = true;
 
   FOR_EACH_LOOP (li, loop, 0)
     {
@@ -3069,16 +3244,19 @@ scop_ivs_can_be_represented (scop_p scop)
          tree type = TREE_TYPE (res);
 
          if (TYPE_UNSIGNED (type)
-             && TYPE_PRECISION (type) >= TYPE_PRECISION (my_long_long))
-           return false;
+             && TYPE_PRECISION (type) >= TYPE_PRECISION (long_long_integer_type_node))
+           {
+             result = false;
+             break;
+           }
        }
+      if (!result)
+       FOR_EACH_LOOP_BREAK (li);
     }
 
-  return true;
+  return result;
 }
 
-#undef my_long_long
-
 /* Builds the polyhedral representation for a SESE region.  */
 
 void
@@ -3099,6 +3277,9 @@ build_poly_scop (scop_p scop)
   if (!scop_ivs_can_be_represented (scop))
     return;
 
+  if (flag_associative_math)
+    rewrite_commutative_reductions_out_of_ssa (scop);
+
   build_sese_loop_nests (region);
   build_sese_conditions (region);
   find_scop_parameters (scop);
@@ -3115,8 +3296,6 @@ build_poly_scop (scop_p scop)
      representation to the polyhedral representation to avoid scev
      analysis failures.  That means that these functions will insert
      new data references that they create in the right place.  */
-  if (flag_associative_math)
-    rewrite_commutative_reductions_out_of_ssa (scop);
   rewrite_reductions_out_of_ssa (scop);
   rewrite_cross_bb_scalar_deps_out_of_ssa (scop);