OSDN Git Service

* pt.c (print_template_statistics): New.
[pf3gnuchains/gcc-fork.git] / gcc / tree-loop-distribution.c
index 2d4a5d6..746e442 100644 (file)
@@ -1,20 +1,21 @@
 /* Loop distribution.
-   Copyright (C) 2006, 2007 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2007, 2008, 2009, 2010
+   Free Software Foundation, Inc.
    Contributed by Georges-Andre Silber <Georges-Andre.Silber@ensmp.fr>
    and Sebastian Pop <sebastian.pop@amd.com>.
 
 This file is part of GCC.
-   
+
 GCC is free software; you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the
 Free Software Foundation; either version 3, or (at your option) any
 later version.
-   
+
 GCC is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
-   
+
 You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
@@ -26,7 +27,7 @@ along with GCC; see the file COPYING3.  If not see
    |    D(I) = A(I-1)*E
    |ENDDO
 
-   is transformed to 
+   is transformed to
 
    |DOALL I = 2, N
    |   A(I) = B(I) + C
@@ -45,11 +46,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "coretypes.h"
 #include "tm.h"
-#include "ggc.h"
 #include "tree.h"
-#include "target.h"
-
-#include "rtl.h"
 #include "basic-block.h"
 #include "diagnostic.h"
 #include "tree-flow.h"
@@ -84,27 +81,33 @@ static void
 update_phis_for_loop_copy (struct loop *orig_loop, struct loop *new_loop)
 {
   tree new_ssa_name;
-  tree phi_new, phi_orig;
+  gimple_stmt_iterator si_new, si_orig;
   edge orig_loop_latch = loop_latch_edge (orig_loop);
   edge orig_entry_e = loop_preheader_edge (orig_loop);
   edge new_loop_entry_e = loop_preheader_edge (new_loop);
 
   /* Scan the phis in the headers of the old and new loops
      (they are organized in exactly the same order).  */
-
-  for (phi_new = phi_nodes (new_loop->header),
-       phi_orig = phi_nodes (orig_loop->header);
-       phi_new && phi_orig;
-       phi_new = PHI_CHAIN (phi_new), phi_orig = PHI_CHAIN (phi_orig))
+  for (si_new = gsi_start_phis (new_loop->header),
+       si_orig = gsi_start_phis (orig_loop->header);
+       !gsi_end_p (si_new) && !gsi_end_p (si_orig);
+       gsi_next (&si_new), gsi_next (&si_orig))
     {
+      tree def;
+      source_location locus;
+      gimple phi_new = gsi_stmt (si_new);
+      gimple phi_orig = gsi_stmt (si_orig);
+
       /* Add the first phi argument for the phi in NEW_LOOP (the one
         associated with the entry of NEW_LOOP)  */
-      tree def = PHI_ARG_DEF_FROM_EDGE (phi_orig, orig_entry_e);
-      add_phi_arg (phi_new, def, new_loop_entry_e);
+      def = PHI_ARG_DEF_FROM_EDGE (phi_orig, orig_entry_e);
+      locus = gimple_phi_arg_location_from_edge (phi_orig, orig_entry_e);
+      add_phi_arg (phi_new, def, new_loop_entry_e, locus);
 
       /* Add the second phi argument for the phi in NEW_LOOP (the one
         associated with the latch of NEW_LOOP)  */
       def = PHI_ARG_DEF_FROM_EDGE (phi_orig, orig_loop_latch);
+      locus = gimple_phi_arg_location_from_edge (phi_orig, orig_loop_latch);
 
       if (TREE_CODE (def) == SSA_NAME)
        {
@@ -112,14 +115,14 @@ update_phis_for_loop_copy (struct loop *orig_loop, struct loop *new_loop)
 
          if (!new_ssa_name)
            /* This only happens if there are no definitions inside the
-              loop.  Use the phi_result in this case.  */
-           new_ssa_name = PHI_RESULT (phi_new);
+              loop.  Use the the invariant in the new loop as is.  */
+           new_ssa_name = def;
        }
       else
        /* Could be an integer.  */
        new_ssa_name = def;
 
-      add_phi_arg (phi_new, new_ssa_name, loop_latch_edge (new_loop));
+      add_phi_arg (phi_new, new_ssa_name, loop_latch_edge (new_loop), locus);
     }
 }
 
@@ -171,7 +174,7 @@ static bool
 generate_loops_for_partition (struct loop *loop, bitmap partition, bool copy_p)
 {
   unsigned i, x;
-  block_stmt_iterator bsi;
+  gimple_stmt_iterator bsi;
   basic_block *bbs;
 
   if (copy_p)
@@ -192,27 +195,19 @@ generate_loops_for_partition (struct loop *loop, bitmap partition, bool copy_p)
   for (x = 0, i = 0; i < loop->num_nodes; i++)
     {
       basic_block bb = bbs[i];
-      tree phi, prev = NULL_TREE, next;
 
-      for (phi = phi_nodes (bb); phi;)
+      for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi);)
        if (!bitmap_bit_p (partition, x++))
-         {
-           next = PHI_CHAIN (phi);
-           remove_phi_node (phi, prev, true);
-           phi = next;
-         }
+         remove_phi_node (&bsi, true);
        else
-         {
-           prev = phi;
-           phi = PHI_CHAIN (phi);
-         }
+         gsi_next (&bsi);
 
-      for (bsi = bsi_start (bb); !bsi_end_p (bsi);)
-       if (TREE_CODE (bsi_stmt (bsi)) != LABEL_EXPR
+      for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi);)
+       if (gimple_code (gsi_stmt (bsi)) != GIMPLE_LABEL
            && !bitmap_bit_p (partition, x++))
-         bsi_remove (&bsi, false);
+         gsi_remove (&bsi, false);
        else
-         bsi_next (&bsi);
+         gsi_next (&bsi);
 
        mark_virtual_ops_in_bb (bb);
     }
@@ -221,99 +216,92 @@ generate_loops_for_partition (struct loop *loop, bitmap partition, bool copy_p)
   return true;
 }
 
+/* Build the size argument for a memset call.  */
+
+static inline tree
+build_size_arg_loc (location_t loc, tree nb_iter, tree op,
+                   gimple_seq *stmt_list)
+{
+  gimple_seq stmts;
+  tree x = size_binop_loc (loc, MULT_EXPR,
+                          fold_convert_loc (loc, sizetype, nb_iter),
+                          TYPE_SIZE_UNIT (TREE_TYPE (op)));
+  x = force_gimple_operand (x, &stmts, true, NULL);
+  gimple_seq_add_seq (stmt_list, stmts);
+
+  return x;
+}
+
 /* Generate a call to memset.  Return true when the operation succeeded.  */
 
 static bool
-generate_memset_zero (tree stmt, tree op0, tree nb_iter,
-                     block_stmt_iterator bsi)
+generate_memset_zero (gimple stmt, tree op0, tree nb_iter,
+                     gimple_stmt_iterator bsi)
 {
-  tree s, t, stmts, nb_bytes, addr_base;
+  tree addr_base, nb_bytes;
   bool res = false;
-  tree stmt_list = NULL_TREE;
-  tree args [3];
-  tree fn_call, mem, fndecl, fntype, fn;
-  tree_stmt_iterator i;
-  ssa_op_iter iter;
+  gimple_seq stmt_list = NULL, stmts;
+  gimple fn_call;
+  tree mem, fn;
+  gimple_stmt_iterator i;
   struct data_reference *dr = XCNEW (struct data_reference);
-
-  nb_bytes = fold_build2 (MULT_EXPR, TREE_TYPE (nb_iter),
-                         nb_iter, TYPE_SIZE_UNIT (TREE_TYPE (op0)));
-  nb_bytes = force_gimple_operand (nb_bytes, &stmts, true, NULL);
-  append_to_statement_list_force (stmts, &stmt_list);
+  location_t loc = gimple_location (stmt);
 
   DR_STMT (dr) = stmt;
   DR_REF (dr) = op0;
-  dr_analyze_innermost (dr);
+  if (!dr_analyze_innermost (dr))
+    goto end;
 
   /* Test for a positive stride, iterating over every element.  */
-  if (integer_zerop (fold_build2 (MINUS_EXPR, integer_type_node, DR_STEP (dr),
-                                 TYPE_SIZE_UNIT (TREE_TYPE (op0)))))
-    addr_base = fold_build2 (PLUS_EXPR, TREE_TYPE (DR_BASE_ADDRESS (dr)),
-                            DR_BASE_ADDRESS (dr), 
-                            size_binop (PLUS_EXPR,
-                                        DR_OFFSET (dr), DR_INIT (dr)));
+  if (integer_zerop (size_binop (MINUS_EXPR,
+                                fold_convert (sizetype, DR_STEP (dr)),
+                                TYPE_SIZE_UNIT (TREE_TYPE (op0)))))
+    {
+      addr_base = fold_convert_loc (loc, sizetype,
+                                   size_binop_loc (loc, PLUS_EXPR,
+                                                   DR_OFFSET (dr),
+                                                   DR_INIT (dr)));
+      addr_base = fold_build2_loc (loc, POINTER_PLUS_EXPR,
+                                  TREE_TYPE (DR_BASE_ADDRESS (dr)),
+                                  DR_BASE_ADDRESS (dr), addr_base);
+
+      nb_bytes = build_size_arg_loc (loc, nb_iter, op0, &stmt_list);
+    }
 
   /* Test for a negative stride, iterating over every element.  */
-  else if (integer_zerop (fold_build2 (PLUS_EXPR, integer_type_node,
-                                      TYPE_SIZE_UNIT (TREE_TYPE (op0)),
-                                      DR_STEP (dr))))
+  else if (integer_zerop (size_binop (PLUS_EXPR,
+                                     TYPE_SIZE_UNIT (TREE_TYPE (op0)),
+                                     fold_convert (sizetype, DR_STEP (dr)))))
     {
-      addr_base = size_binop (PLUS_EXPR, DR_OFFSET (dr), DR_INIT (dr));
-      addr_base = fold_build2 (MINUS_EXPR, sizetype, addr_base, nb_bytes);
-      addr_base = force_gimple_operand (addr_base, &stmts, true, NULL);
-      append_to_statement_list_force (stmts, &stmt_list);
-
-      addr_base = fold_build2 (POINTER_PLUS_EXPR,
-                              TREE_TYPE (DR_BASE_ADDRESS (dr)),
-                              DR_BASE_ADDRESS (dr), addr_base);
+      nb_bytes = build_size_arg_loc (loc, nb_iter, op0, &stmt_list);
+
+      addr_base = size_binop_loc (loc, PLUS_EXPR, DR_OFFSET (dr), DR_INIT (dr));
+      addr_base = fold_convert_loc (loc, sizetype, addr_base);
+      addr_base = size_binop_loc (loc, MINUS_EXPR, addr_base,
+                                 fold_convert_loc (loc, sizetype, nb_bytes));
+      addr_base = size_binop_loc (loc, PLUS_EXPR, addr_base,
+                                 TYPE_SIZE_UNIT (TREE_TYPE (op0)));
+      addr_base = fold_build2_loc (loc, POINTER_PLUS_EXPR,
+                                  TREE_TYPE (DR_BASE_ADDRESS (dr)),
+                                  DR_BASE_ADDRESS (dr), addr_base);
     }
   else
     goto end;
 
   mem = force_gimple_operand (addr_base, &stmts, true, NULL);
-  append_to_statement_list_force (stmts, &stmt_list);
-
+  gimple_seq_add_seq (&stmt_list, stmts);
 
-  fndecl = implicit_built_in_decls [BUILT_IN_MEMSET];
-  fntype = TREE_TYPE (fndecl);
-  fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
+  fn = build_fold_addr_expr (implicit_built_in_decls [BUILT_IN_MEMSET]);
+  fn_call = gimple_build_call (fn, 3, mem, integer_zero_node, nb_bytes);
+  gimple_seq_add_stmt (&stmt_list, fn_call);
 
-  args[0] = mem;
-  args[1] = integer_zero_node;
-  args[2] = nb_bytes;
-
-  fn_call = build_call_array (fntype, fn, 3, args);
-  append_to_statement_list_force (fn_call, &stmt_list);
-
-  for (i = tsi_start (stmt_list); !tsi_end_p (i); tsi_next (&i))
+  for (i = gsi_start (stmt_list); !gsi_end_p (i); gsi_next (&i))
     {
-      s = tsi_stmt (i);
+      gimple s = gsi_stmt (i);
       update_stmt_if_modified (s);
-
-      FOR_EACH_SSA_TREE_OPERAND (t, s, iter, SSA_OP_VIRTUAL_DEFS)
-       {
-         if (TREE_CODE (t) == SSA_NAME)
-           t = SSA_NAME_VAR (t);
-         mark_sym_for_renaming (t);
-       }
     }
 
-  /* Mark also the uses of the VDEFS of STMT to be renamed.  */
-  FOR_EACH_SSA_TREE_OPERAND (t, stmt, iter, SSA_OP_VIRTUAL_DEFS)
-    {
-      if (TREE_CODE (t) == SSA_NAME)
-       {
-         imm_use_iterator imm_iter;
-
-         FOR_EACH_IMM_USE_STMT (s, imm_iter, t)
-           update_stmt (s);
-
-         t = SSA_NAME_VAR (t);
-       }
-      mark_sym_for_renaming (t);
-    }
-
-  bsi_insert_after (&bsi, stmt_list, BSI_CONTINUE_LINKING);
+  gsi_insert_seq_after (&bsi, stmt_list, GSI_CONTINUE_LINKING);
   res = true;
 
   if (dump_file && (dump_flags & TDF_DETAILS))
@@ -324,6 +312,38 @@ generate_memset_zero (tree stmt, tree op0, tree nb_iter,
   return res;
 }
 
+/* Propagate phis in BB b to their uses and remove them.  */
+
+static void
+prop_phis (basic_block b)
+{
+  gimple_stmt_iterator psi;
+  gimple_seq phis = phi_nodes (b);
+
+  for (psi = gsi_start (phis); !gsi_end_p (psi); )
+    {
+      gimple phi = gsi_stmt (psi);
+      tree def = gimple_phi_result (phi), use = gimple_phi_arg_def (phi, 0);
+
+      gcc_assert (gimple_phi_num_args (phi) == 1);
+
+      if (!is_gimple_reg (def))
+       {
+         imm_use_iterator iter;
+         use_operand_p use_p;
+         gimple stmt;
+
+         FOR_EACH_IMM_USE_STMT (stmt, iter, def)
+           FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
+             SET_USE (use_p, use);
+       }
+      else
+       replace_uses_by (def, use);
+
+      remove_phi_node (&psi, true);
+    }
+}
+
 /* Tries to generate a builtin function for the instructions of LOOP
    pointed to by the bits set in PARTITION.  Returns true when the
    operation succeeded.  */
@@ -334,9 +354,9 @@ generate_builtin (struct loop *loop, bitmap partition, bool copy_p)
   bool res = false;
   unsigned i, x = 0;
   basic_block *bbs;
-  tree write = NULL_TREE;
+  gimple write = NULL;
   tree op0, op1;
-  block_stmt_iterator bsi;
+  gimple_stmt_iterator bsi;
   tree nb_iter = number_of_exit_cond_executions (loop);
 
   if (!nb_iter || nb_iter == chrec_dont_know)
@@ -347,18 +367,17 @@ generate_builtin (struct loop *loop, bitmap partition, bool copy_p)
   for (i = 0; i < loop->num_nodes; i++)
     {
       basic_block bb = bbs[i];
-      tree phi;
 
-      for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
+      for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
        x++;
 
-      for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
+      for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
        {
-         tree stmt = bsi_stmt (bsi);
+         gimple stmt = gsi_stmt (bsi);
 
          if (bitmap_bit_p (partition, x++)
-             && TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
-             && !is_gimple_reg (GIMPLE_STMT_OPERAND (stmt, 0)))
+             && is_gimple_assign (stmt)
+             && !is_gimple_reg (gimple_assign_lhs (stmt)))
            {
              /* Don't generate the builtins when there are more than
                 one memory write.  */
@@ -366,6 +385,8 @@ generate_builtin (struct loop *loop, bitmap partition, bool copy_p)
                goto end;
 
              write = stmt;
+             if (bb == loop->latch)
+               nb_iter = number_of_latch_executions (loop);
            }
        }
     }
@@ -373,17 +394,18 @@ generate_builtin (struct loop *loop, bitmap partition, bool copy_p)
   if (!write)
     goto end;
 
-  op0 = GIMPLE_STMT_OPERAND (write, 0);
-  op1 = GIMPLE_STMT_OPERAND (write, 1);
+  op0 = gimple_assign_lhs (write);
+  op1 = gimple_assign_rhs1 (write);
 
   if (!(TREE_CODE (op0) == ARRAY_REF
        || TREE_CODE (op0) == INDIRECT_REF))
     goto end;
 
   /* The new statements will be placed before LOOP.  */
-  bsi = bsi_last (loop_preheader_edge (loop)->src);
+  bsi = gsi_last_bb (loop_preheader_edge (loop)->src);
 
-  if (integer_zerop (op1) || real_zerop (op1))
+  if (gimple_assign_rhs_code (write) == INTEGER_CST
+      && (integer_zerop (op1) || real_zerop (op1)))
     res = generate_memset_zero (write, op0, nb_iter, bsi);
 
   /* If this is the last partition for which we generate code, we have
@@ -393,12 +415,15 @@ generate_builtin (struct loop *loop, bitmap partition, bool copy_p)
       unsigned nbbs = loop->num_nodes;
       basic_block src = loop_preheader_edge (loop)->src;
       basic_block dest = single_exit (loop)->dest;
+      prop_phis (dest);
       make_edge (src, dest, EDGE_FALLTHRU);
-      set_immediate_dominator (CDI_DOMINATORS, dest, src);
       cancel_loop_tree (loop);
 
       for (i = 0; i < nbbs; i++)
        delete_basic_block (bbs[i]);
+
+      set_immediate_dominator (CDI_DOMINATORS, dest,
+                              recompute_dominator (CDI_DOMINATORS, dest));
     }
 
  end:
@@ -490,7 +515,6 @@ mark_nodes_having_upstream_mem_writes (struct graph *rdg)
       {
        unsigned i;
        VEC (int, heap) *nodes = VEC_alloc (int, heap, 3);
-       bool has_upstream_mem_write_p = false;
 
        graphds_dfs (rdg, &v, 1, &nodes, false, NULL);
 
@@ -508,7 +532,6 @@ mark_nodes_having_upstream_mem_writes (struct graph *rdg)
                   should be placed in the same partition.  */
                || has_anti_dependence (&(rdg->vertices[x])))
              {
-               has_upstream_mem_write_p = true;
                bitmap_set_bit (upstream_mem_writes, x);
              }
          }
@@ -554,10 +577,9 @@ static void
 rdg_flag_uses (struct graph *rdg, int u, bitmap partition, bitmap loops,
               bitmap processed, bool *part_has_writes)
 {
-  ssa_op_iter iter;
   use_operand_p use_p;
   struct vertex *x = &(rdg->vertices[u]);
-  tree stmt = RDGV_STMT (x);
+  gimple stmt = RDGV_STMT (x);
   struct graph_edge *anti_dep = has_anti_dependence (x);
 
   /* Keep in the same partition the destination of an antidependence,
@@ -572,15 +594,15 @@ rdg_flag_uses (struct graph *rdg, int u, bitmap partition, bitmap loops,
                                       processed, part_has_writes);
     }
 
-  if (TREE_CODE (stmt) != PHI_NODE)
+  if (gimple_code (stmt) != GIMPLE_PHI)
     {
-      FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_VIRTUAL_USES)
+      if ((use_p = gimple_vuse_op (stmt)) != NULL_USE_OPERAND_P)
        {
          tree use = USE_FROM_PTR (use_p);
 
          if (TREE_CODE (use) == SSA_NAME)
            {
-             tree def_stmt = SSA_NAME_DEF_STMT (use);
+             gimple def_stmt = SSA_NAME_DEF_STMT (use);
              int v = rdg_vertex_for_stmt (rdg, def_stmt);
 
              if (v >= 0
@@ -591,10 +613,9 @@ rdg_flag_uses (struct graph *rdg, int u, bitmap partition, bitmap loops,
        }
     }
 
-  if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
-      && has_upstream_mem_writes (u))
+  if (is_gimple_assign (stmt) && has_upstream_mem_writes (u))
     {
-      tree op0 = GIMPLE_STMT_OPERAND (stmt, 0);
+      tree op0 = gimple_assign_lhs (stmt);
 
       /* Scalar channels don't have enough space for transmitting data
         between tasks, unless we add more storage by privatizing.  */
@@ -639,7 +660,7 @@ rdg_flag_vertex (struct graph *rdg, int v, bitmap partition, bitmap loops,
 }
 
 /* Flag in the bitmap PARTITION the vertex V and all its predecessors.
-   Alse flag their loop number in LOOPS.  */
+   Also flag their loop number in LOOPS.  */
 
 static void
 rdg_flag_vertex_and_dependent (struct graph *rdg, int v, bitmap partition,
@@ -667,7 +688,7 @@ rdg_flag_vertex_and_dependent (struct graph *rdg, int v, bitmap partition,
    blocks of LOOP.  */
 
 static void
-collect_condition_stmts (struct loop *loop, VEC (tree, heap) **conds)
+collect_condition_stmts (struct loop *loop, VEC (gimple, heap) **conds)
 {
   unsigned i;
   edge e;
@@ -675,10 +696,10 @@ collect_condition_stmts (struct loop *loop, VEC (tree, heap) **conds)
 
   for (i = 0; VEC_iterate (edge, exits, i, e); i++)
     {
-      tree cond = last_stmt (e->src);
+      gimple cond = last_stmt (e->src);
 
       if (cond)
-       VEC_safe_push (tree, heap, *conds, cond);
+       VEC_safe_push (gimple, heap, *conds, cond);
     }
 
   VEC_free (edge, heap, exits);
@@ -694,14 +715,14 @@ rdg_flag_loop_exits (struct graph *rdg, bitmap loops, bitmap partition,
 {
   unsigned i;
   bitmap_iterator bi;
-  VEC (tree, heap) *conds = VEC_alloc (tree, heap, 3);
+  VEC (gimple, heap) *conds = VEC_alloc (gimple, heap, 3);
 
   EXECUTE_IF_SET_IN_BITMAP (loops, 0, i, bi)
     collect_condition_stmts (get_loop (i), &conds);
 
-  while (!VEC_empty (tree, conds))
+  while (!VEC_empty (gimple, conds))
     {
-      tree cond = VEC_pop (tree, conds);
+      gimple cond = VEC_pop (gimple, conds);
       int v = rdg_vertex_for_stmt (rdg, cond);
       bitmap new_loops = BITMAP_ALLOC (NULL);
 
@@ -720,17 +741,6 @@ rdg_flag_loop_exits (struct graph *rdg, bitmap loops, bitmap partition,
     }
 }
 
-/* Strongly connected components of the reduced data dependence graph.  */
-
-typedef struct rdg_component
-{
-  int num;
-  VEC (int, heap) *vertices;
-} *rdgc;
-
-DEF_VEC_P (rdgc);
-DEF_VEC_ALLOC_P (rdgc, heap);
-
 /* Flag all the nodes of RDG containing memory accesses that could
    potentially belong to arrays already accessed in the current
    PARTITION.  */
@@ -837,7 +847,7 @@ free_rdg_components (VEC (rdgc, heap) *components)
    of RDG in which the STARTING_VERTICES occur.  */
 
 static void
-rdg_build_components (struct graph *rdg, VEC (int, heap) *starting_vertices, 
+rdg_build_components (struct graph *rdg, VEC (int, heap) *starting_vertices,
                      VEC (rdgc, heap) **components)
 {
   int i, v;
@@ -874,9 +884,6 @@ rdg_build_components (struct graph *rdg, VEC (int, heap) *starting_vertices,
   BITMAP_FREE (saved_components);
 }
 
-DEF_VEC_P (bitmap);
-DEF_VEC_ALLOC_P (bitmap, heap);
-
 /* Aggregate several components into a useful partition that is
    registered in the PARTITIONS vector.  Partitions will be
    distributed in different loops.  */
@@ -895,10 +902,10 @@ rdg_build_partitions (struct graph *rdg, VEC (rdgc, heap) *components,
       bitmap np;
       bool part_has_writes = false;
       int v = VEC_index (int, x->vertices, 0);
-       
+
       if (bitmap_bit_p (processed, v))
        continue;
-  
+
       np = build_rdg_partition_for_component (rdg, x, &part_has_writes,
                                              other_stores);
       bitmap_ior_into (partition, np);
@@ -969,6 +976,64 @@ debug_rdg_partitions (VEC (bitmap, heap) *partitions)
   dump_rdg_partitions (stderr, partitions);
 }
 
+/* Returns the number of read and write operations in the RDG.  */
+
+static int
+number_of_rw_in_rdg (struct graph *rdg)
+{
+  int i, res = 0;
+
+  for (i = 0; i < rdg->n_vertices; i++)
+    {
+      if (RDG_MEM_WRITE_STMT (rdg, i))
+       ++res;
+
+      if (RDG_MEM_READS_STMT (rdg, i))
+       ++res;
+    }
+
+  return res;
+}
+
+/* Returns the number of read and write operations in a PARTITION of
+   the RDG.  */
+
+static int
+number_of_rw_in_partition (struct graph *rdg, bitmap partition)
+{
+  int res = 0;
+  unsigned i;
+  bitmap_iterator ii;
+
+  EXECUTE_IF_SET_IN_BITMAP (partition, 0, i, ii)
+    {
+      if (RDG_MEM_WRITE_STMT (rdg, i))
+       ++res;
+
+      if (RDG_MEM_READS_STMT (rdg, i))
+       ++res;
+    }
+
+  return res;
+}
+
+/* Returns true when one of the PARTITIONS contains all the read or
+   write operations of RDG.  */
+
+static bool
+partition_contains_all_rw (struct graph *rdg, VEC (bitmap, heap) *partitions)
+{
+  int i;
+  bitmap partition;
+  int nrw = number_of_rw_in_rdg (rdg);
+
+  for (i = 0; VEC_iterate (bitmap, partitions, i, partition); i++)
+    if (nrw == number_of_rw_in_partition (rdg, partition))
+      return true;
+
+  return false;
+}
+
 /* Generate code from STARTING_VERTICES in RDG.  Returns the number of
    distributed loops.  */
 
@@ -1016,7 +1081,8 @@ ldist_gen (struct loop *loop, struct graph *rdg,
   BITMAP_FREE (processed);
   nbp = VEC_length (bitmap, partitions);
 
-  if (nbp <= 1)
+  if (nbp <= 1
+      || partition_contains_all_rw (rdg, partitions))
     goto ldist_done;
 
   if (dump_file && (dump_flags & TDF_DETAILS))
@@ -1050,11 +1116,11 @@ ldist_gen (struct loop *loop, struct graph *rdg,
    Returns the number of distributed loops.  */
 
 static int
-distribute_loop (struct loop *loop, VEC (tree, heap) *stmts)
+distribute_loop (struct loop *loop, VEC (gimple, heap) *stmts)
 {
-  bool res = false;
+  int res = 0;
   struct graph *rdg;
-  tree s;
+  gimple s;
   unsigned i;
   VEC (int, heap) *vertices;
 
@@ -1085,7 +1151,7 @@ distribute_loop (struct loop *loop, VEC (tree, heap) *stmts)
   if (dump_file && (dump_flags & TDF_DETAILS))
     dump_rdg (dump_file, rdg);
 
-  for (i = 0; VEC_iterate (tree, stmts, i, s); i++)
+  for (i = 0; VEC_iterate (gimple, stmts, i, s); i++)
     {
       int v = rdg_vertex_for_stmt (rdg, s);
 
@@ -1117,7 +1183,7 @@ tree_loop_distribution (void)
 
   FOR_EACH_LOOP (li, loop, 0)
     {
-      VEC (tree, heap) *work_list = VEC_alloc (tree, heap, 3);
+      VEC (gimple, heap) *work_list = VEC_alloc (gimple, heap, 3);
 
       /* With the following working list, we're asking distribute_loop
         to separate the stores of the loop: when dependences allow,
@@ -1143,7 +1209,7 @@ tree_loop_distribution (void)
 
       verify_loop_structure ();
 
-      VEC_free (tree, heap, work_list);
+      VEC_free (gimple, heap, work_list);
     }
 
   return 0;
@@ -1155,8 +1221,10 @@ gate_tree_loop_distribution (void)
   return flag_tree_loop_distribution != 0;
 }
 
-struct tree_opt_pass pass_loop_distribution =
+struct gimple_opt_pass pass_loop_distribution =
 {
+ {
+  GIMPLE_PASS,
   "ldist",                     /* name */
   gate_tree_loop_distribution,  /* gate */
   tree_loop_distribution,       /* execute */
@@ -1168,6 +1236,6 @@ struct tree_opt_pass pass_loop_distribution =
   0,                           /* properties_provided */
   0,                           /* properties_destroyed */
   0,                           /* todo_flags_start */
-  TODO_dump_func | TODO_verify_loops,            /* todo_flags_finish */
-  0                             /* letter */  
+  TODO_dump_func                /* todo_flags_finish */
+ }
 };