OSDN Git Service

PR rtl-optimization/45101
[pf3gnuchains/gcc-fork.git] / gcc / tree-if-conv.c
index ba3b7c6..0f1caaa 100644 (file)
@@ -89,7 +89,6 @@ along with GCC; see the file COPYING3.  If not see
 #include "flags.h"
 #include "timevar.h"
 #include "basic-block.h"
-#include "diagnostic.h"
 #include "tree-pretty-print.h"
 #include "gimple-pretty-print.h"
 #include "tree-flow.h"
@@ -99,10 +98,121 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-data-ref.h"
 #include "tree-scalar-evolution.h"
 #include "tree-pass.h"
+#include "dbgcnt.h"
 
 /* List of basic blocks in if-conversion-suitable order.  */
 static basic_block *ifc_bbs;
 
+/* Structure used to predicate basic blocks.  This is attached to the
+   ->aux field of the BBs in the loop to be if-converted.  */
+typedef struct bb_predicate_s {
+
+  /* The condition under which this basic block is executed.  */
+  tree predicate;
+
+  /* PREDICATE is gimplified, and the sequence of statements is
+     recorded here, in order to avoid the duplication of computations
+     that occur in previous conditions.  See PR44483.  */
+  gimple_seq predicate_gimplified_stmts;
+} *bb_predicate_p;
+
+/* Returns true when the basic block BB has a predicate.  */
+
+static inline bool
+bb_has_predicate (basic_block bb)
+{
+  return bb->aux != NULL;
+}
+
+/* Returns the gimplified predicate for basic block BB.  */
+
+static inline tree
+bb_predicate (basic_block bb)
+{
+  return ((bb_predicate_p) bb->aux)->predicate;
+}
+
+/* Sets the gimplified predicate COND for basic block BB.  */
+
+static inline void
+set_bb_predicate (basic_block bb, tree cond)
+{
+  ((bb_predicate_p) bb->aux)->predicate = cond;
+}
+
+/* Returns the sequence of statements of the gimplification of the
+   predicate for basic block BB.  */
+
+static inline gimple_seq
+bb_predicate_gimplified_stmts (basic_block bb)
+{
+  return ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts;
+}
+
+/* Sets the sequence of statements STMTS of the gimplification of the
+   predicate for basic block BB.  */
+
+static inline void
+set_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
+{
+  ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts = stmts;
+}
+
+/* Adds the sequence of statements STMTS to the sequence of statements
+   of the predicate for basic block BB.  */
+
+static inline void
+add_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
+{
+  gimple_seq_add_seq
+    (&(((bb_predicate_p) bb->aux)->predicate_gimplified_stmts), stmts);
+}
+
+/* Initializes to TRUE the predicate of basic block BB.  */
+
+static inline void
+init_bb_predicate (basic_block bb)
+{
+  bb->aux = XNEW (struct bb_predicate_s);
+  set_bb_predicate_gimplified_stmts (bb, NULL);
+  set_bb_predicate (bb, boolean_true_node);
+}
+
+/* Free the predicate of basic block BB.  */
+
+static inline void
+free_bb_predicate (basic_block bb)
+{
+  gimple_seq stmts;
+
+  if (!bb_has_predicate (bb))
+    return;
+
+  /* Release the SSA_NAMEs created for the gimplification of the
+     predicate.  */
+  stmts = bb_predicate_gimplified_stmts (bb);
+  if (stmts)
+    {
+      gimple_stmt_iterator i;
+
+      for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
+       free_stmt_operands (gsi_stmt (i));
+    }
+
+  free (bb->aux);
+  bb->aux = NULL;
+}
+
+/* Free the predicate of BB and reinitialize it with the true
+   predicate.  */
+
+static inline void
+reset_bb_predicate (basic_block bb)
+{
+  free_bb_predicate (bb);
+  init_bb_predicate (bb);
+}
+
 /* Create a new temp variable of type TYPE.  Add GIMPLE_ASSIGN to assign EXP
    to the new variable.  */
 
@@ -130,173 +240,148 @@ ifc_temp_var (tree type, tree exp)
   return stmt;
 }
 
-/* Add condition NEW_COND to the predicate list of basic block BB.  */
+/* Return true when COND is a true predicate.  */
 
-static void
-add_to_predicate_list (basic_block bb, tree new_cond)
+static inline bool
+is_true_predicate (tree cond)
 {
-  tree cond = (tree) bb->aux;
+  return (cond == NULL_TREE
+         || cond == boolean_true_node
+         || integer_onep (cond));
+}
 
-  if (cond)
-    cond = fold_build2_loc (EXPR_LOCATION (cond),
-                           TRUTH_OR_EXPR, boolean_type_node,
-                           unshare_expr (cond), new_cond);
-  else
-    cond = new_cond;
+/* Returns true when BB has a predicate that is not trivial: true or
+   NULL_TREE.  */
 
-  bb->aux = cond;
+static inline bool
+is_predicated (basic_block bb)
+{
+  return !is_true_predicate (bb_predicate (bb));
 }
 
-/* Add the condition COND to the previous condition PREV_COND, and add this
-   to the predicate list of the destination of edge E.  GSI is the
-   place where the gimplification of the resulting condition should
-   output code.  LOOP is the loop to be if-converted.  */
+/* Parses the predicate COND and returns its comparison code and
+   operands OP0 and OP1.  */
 
-static tree
-add_to_dst_predicate_list (struct loop *loop, edge e,
-                          tree prev_cond, tree cond,
-                          gimple_stmt_iterator *gsi)
+static enum tree_code
+parse_predicate (tree cond, tree *op0, tree *op1)
 {
-  tree new_cond = NULL_TREE;
-
-  if (!flow_bb_inside_loop_p (loop, e->dest))
-    return NULL_TREE;
+  gimple s;
 
-  if (prev_cond == boolean_true_node || !prev_cond)
-    new_cond = unshare_expr (cond);
-  else
+  if (TREE_CODE (cond) == SSA_NAME
+      && is_gimple_assign (s = SSA_NAME_DEF_STMT (cond)))
     {
-      tree tmp;
-      gimple tmp_stmt = NULL;
+      if (TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
+       {
+         *op0 = gimple_assign_rhs1 (s);
+         *op1 = gimple_assign_rhs2 (s);
+         return gimple_assign_rhs_code (s);
+       }
 
-      prev_cond = force_gimple_operand_gsi (gsi, unshare_expr (prev_cond),
-                                           true, NULL, true, GSI_SAME_STMT);
+      else if (gimple_assign_rhs_code (s) == TRUTH_NOT_EXPR)
+       {
+         tree op = gimple_assign_rhs1 (s);
+         tree type = TREE_TYPE (op);
+         enum tree_code code = parse_predicate (op, op0, op1);
 
-      cond = force_gimple_operand_gsi (gsi, unshare_expr (cond),
-                                      true, NULL, true, GSI_SAME_STMT);
+         return code == ERROR_MARK ? ERROR_MARK
+           : invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
+       }
 
-      /* Add the condition COND to the e->aux field.  In case the edge
-        destination is a PHI node, this condition will be added to
-        the block predicate to construct a complete condition.  */
-      e->aux = cond;
+      return ERROR_MARK;
+    }
 
-      tmp = build2 (TRUTH_AND_EXPR, boolean_type_node,
-                   unshare_expr (prev_cond), cond);
-      tmp_stmt = ifc_temp_var (boolean_type_node, tmp);
-      gsi_insert_before (gsi, tmp_stmt, GSI_SAME_STMT);
-      new_cond = gimple_assign_lhs (tmp_stmt);
+  if (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison)
+    {
+      *op0 = TREE_OPERAND (cond, 0);
+      *op1 = TREE_OPERAND (cond, 1);
+      return TREE_CODE (cond);
     }
 
-  add_to_predicate_list (e->dest, new_cond);
-  return new_cond;
+  return ERROR_MARK;
 }
 
-/* Return true if one of the successor edges of BB exits LOOP.  */
+/* Returns the fold of predicate C1 OR C2 at location LOC.  */
 
-static bool
-bb_with_exit_edge_p (struct loop *loop, basic_block bb)
+static tree
+fold_or_predicates (location_t loc, tree c1, tree c2)
 {
-  edge e;
-  edge_iterator ei;
+  tree op1a, op1b, op2a, op2b;
+  enum tree_code code1 = parse_predicate (c1, &op1a, &op1b);
+  enum tree_code code2 = parse_predicate (c2, &op2a, &op2b);
 
-  FOR_EACH_EDGE (e, ei, bb->succs)
-    if (loop_exit_edge_p (loop, e))
-      return true;
+  if (code1 != ERROR_MARK && code2 != ERROR_MARK)
+    {
+      tree t = maybe_fold_or_comparisons (code1, op1a, op1b,
+                                         code2, op2a, op2b);
+      if (t)
+       return t;
+    }
 
-  return false;
+  return fold_build2_loc (loc, TRUTH_OR_EXPR, boolean_type_node, c1, c2);
 }
 
-/* STMT is a GIMPLE_COND.  Update two destination's predicate list.
-   Remove COND_EXPR, if it is not the exit condition of LOOP.
-   Otherwise update the exit condition of LOOP appropriately.  GSI
-   points to the statement STMT.  */
+/* Add condition NC to the predicate list of basic block BB.  */
 
-static void
-tree_if_convert_cond_stmt (struct loop *loop, gimple stmt, tree cond,
-                          gimple_stmt_iterator *gsi)
+static inline void
+add_to_predicate_list (basic_block bb, tree nc)
 {
-  tree c2;
-  edge true_edge, false_edge;
-  location_t loc = gimple_location (stmt);
-  tree c = fold_build2_loc (loc, gimple_cond_code (stmt), boolean_type_node,
-                           gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
-
-  extract_true_false_edges_from_block (gimple_bb (stmt),
-                                      &true_edge, &false_edge);
-
-  /* Add new condition into destination's predicate list.  */
+  tree bc;
 
-  /* If C is true, then TRUE_EDGE is taken.  */
-  add_to_dst_predicate_list (loop, true_edge, cond, c, gsi);
+  if (is_true_predicate (nc))
+    return;
 
-  /* If C is false, then FALSE_EDGE is taken.  */
-  c2 = invert_truthvalue_loc (loc, unshare_expr (c));
-  add_to_dst_predicate_list (loop, false_edge, cond, c2, gsi);
+  if (!is_predicated (bb))
+    bc = nc;
+  else
+    {
+      bc = bb_predicate (bb);
+      bc = fold_or_predicates (EXPR_LOCATION (bc), nc, bc);
+    }
 
-  /* Now this conditional statement is redundant.  Remove it.  But, do
-     not remove the exit condition!  Update the exit condition using
-     the new condition.  */
-  if (!bb_with_exit_edge_p (loop, gimple_bb (stmt)))
+  if (!is_gimple_condexpr (bc))
     {
-      gsi_remove (gsi, true);
-      cond = NULL_TREE;
+      gimple_seq stmts;
+      bc = force_gimple_operand (bc, &stmts, true, NULL_TREE);
+      add_bb_predicate_gimplified_stmts (bb, stmts);
     }
-}
 
-/* If-convert stmt T which is part of LOOP.
+  if (is_true_predicate (bc))
+    reset_bb_predicate (bb);
+  else
+    set_bb_predicate (bb, bc);
+}
 
-   If T is a GIMPLE_ASSIGN then it is converted into a conditional
-   modify expression using COND.  For conditional expressions, add
-   a condition in the destination basic block's predicate list and
-   remove the conditional expression itself.  GSI points to the
-   statement T.  */
+/* Add the condition COND to the previous condition PREV_COND, and add
+   this to the predicate list of the destination of edge E.  LOOP is
+   the loop to be if-converted.  */
 
-static tree
-tree_if_convert_stmt (struct loop *loop, gimple t, tree cond,
-                     gimple_stmt_iterator *gsi)
+static void
+add_to_dst_predicate_list (struct loop *loop, edge e,
+                          tree prev_cond, tree cond)
 {
-  if (dump_file && (dump_flags & TDF_DETAILS))
-    {
-      fprintf (dump_file, "------if-convert stmt\n");
-      print_gimple_stmt (dump_file, t, 0, TDF_SLIM);
-      print_generic_stmt (dump_file, cond, TDF_SLIM);
-    }
+  if (!flow_bb_inside_loop_p (loop, e->dest))
+    return;
 
-  switch (gimple_code (t))
-    {
-      /* Labels are harmless here.  */
-    case GIMPLE_LABEL:
-      break;
+  if (!is_true_predicate (prev_cond))
+    cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
+                       prev_cond, cond);
 
-    case GIMPLE_DEBUG:
-      /* ??? Should there be conditional GIMPLE_DEBUG_BINDs?  */
-      if (gimple_debug_bind_p (gsi_stmt (*gsi)))
-       {
-         gimple_debug_bind_reset_value (gsi_stmt (*gsi));
-         update_stmt (gsi_stmt (*gsi));
-       }
-      break;
+  add_to_predicate_list (e->dest, cond);
+}
 
-    case GIMPLE_ASSIGN:
-      /* This GIMPLE_ASSIGN is killing previous value of LHS.  Appropriate
-        value will be selected by PHI node based on condition.  It is possible
-        that before this transformation, PHI nodes was selecting default
-        value and now it will use this new value.  This is OK because it does
-        not change the validity of the program.  */
-      break;
+/* Return true if one of the successor edges of BB exits LOOP.  */
 
-    case GIMPLE_COND:
-      /* Update destination blocks' predicate list and remove this
-        condition expression.  */
-      tree_if_convert_cond_stmt (loop, t, cond, gsi);
-      cond = NULL_TREE;
-      break;
+static bool
+bb_with_exit_edge_p (struct loop *loop, basic_block bb)
+{
+  edge e;
+  edge_iterator ei;
 
-    default:
-      gcc_unreachable ();
-    }
+  FOR_EACH_EDGE (e, ei, bb->succs)
+    if (loop_exit_edge_p (loop, e))
+      return true;
 
-  return cond;
+  return false;
 }
 
 /* Return true when PHI is if-convertible.  PHI is part of loop LOOP
@@ -381,9 +466,7 @@ if_convertible_gimple_assign_stmt_p (struct loop *loop, basic_block bb,
       return false;
     }
 
-  /* See if it needs speculative loading or not.  */
-  if (bb != loop->header
-      && gimple_assign_rhs_could_trap_p (stmt))
+  if (gimple_assign_rhs_could_trap_p (stmt))
     {
       if (dump_file && (dump_flags & TDF_DETAILS))
        fprintf (dump_file, "tree could trap...\n");
@@ -460,6 +543,10 @@ if_convertible_bb_p (struct loop *loop, basic_block bb, basic_block exit_bb)
   if (dump_file && (dump_flags & TDF_DETAILS))
     fprintf (dump_file, "----------[%d]-------------\n", bb->index);
 
+  if (EDGE_COUNT (bb->preds) > 2
+      || EDGE_COUNT (bb->succs) > 2)
+    return false;
+
   if (exit_bb)
     {
       if (bb != loop->latch)
@@ -571,6 +658,123 @@ get_loop_body_in_if_conv_order (const struct loop *loop)
   return blocks;
 }
 
+/* Returns true when the analysis of the predicates for all the basic
+   blocks in LOOP succeeded.
+
+   predicate_bbs first allocates the predicates of the basic blocks.
+   These fields are then initialized with the tree expressions
+   representing the predicates under which a basic block is executed
+   in the LOOP.  As the loop->header is executed at each iteration, it
+   has the "true" predicate.  Other statements executed under a
+   condition are predicated with that condition, for example
+
+   | if (x)
+   |   S1;
+   | else
+   |   S2;
+
+   S1 will be predicated with "x", and
+   S2 will be predicated with "!x".  */
+
+static bool
+predicate_bbs (loop_p loop)
+{
+  unsigned int i;
+
+  for (i = 0; i < loop->num_nodes; i++)
+    init_bb_predicate (ifc_bbs[i]);
+
+  for (i = 0; i < loop->num_nodes; i++)
+    {
+      basic_block bb = ifc_bbs[i];
+      tree cond;
+      gimple_stmt_iterator itr;
+
+      /* The loop latch is always executed and has no extra conditions
+        to be processed: skip it.  */
+      if (bb == loop->latch)
+       {
+         reset_bb_predicate (loop->latch);
+         continue;
+       }
+
+      cond = bb_predicate (bb);
+      if (cond
+         && bb != loop->header)
+       {
+         gimple_seq stmts;
+
+         cond = force_gimple_operand (cond, &stmts, true, NULL_TREE);
+         add_bb_predicate_gimplified_stmts (bb, stmts);
+       }
+
+      for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
+       {
+         gimple stmt = gsi_stmt (itr);
+
+         switch (gimple_code (stmt))
+           {
+           case GIMPLE_LABEL:
+           case GIMPLE_ASSIGN:
+           case GIMPLE_CALL:
+           case GIMPLE_DEBUG:
+             break;
+
+           case GIMPLE_COND:
+             {
+               tree c2;
+               edge true_edge, false_edge;
+               location_t loc = gimple_location (stmt);
+               tree c = fold_build2_loc (loc, gimple_cond_code (stmt),
+                                         boolean_type_node,
+                                         gimple_cond_lhs (stmt),
+                                         gimple_cond_rhs (stmt));
+
+               /* Add new condition into destination's predicate list.  */
+               extract_true_false_edges_from_block (gimple_bb (stmt),
+                                                    &true_edge, &false_edge);
+
+               /* If C is true, then TRUE_EDGE is taken.  */
+               add_to_dst_predicate_list (loop, true_edge, cond, c);
+
+               /* If C is false, then FALSE_EDGE is taken.  */
+               c2 = invert_truthvalue_loc (loc, unshare_expr (c));
+               add_to_dst_predicate_list (loop, false_edge, cond, c2);
+
+               cond = NULL_TREE;
+               break;
+             }
+
+           default:
+             /* Not handled yet in if-conversion.  */
+             return false;
+           }
+       }
+
+      /* If current bb has only one successor, then consider it as an
+        unconditional goto.  */
+      if (single_succ_p (bb))
+       {
+         basic_block bb_n = single_succ (bb);
+
+         /* The successor bb inherits the predicate of its
+            predecessor.  If there is no predicate in the predecessor
+            bb, then consider the successor bb as always executed.  */
+         if (cond == NULL_TREE)
+           cond = boolean_true_node;
+
+         add_to_predicate_list (bb_n, cond);
+       }
+    }
+
+  /* The loop header is always executed.  */
+  reset_bb_predicate (loop->header);
+  gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
+             && bb_predicate_gimplified_stmts (loop->latch) == NULL);
+
+  return true;
+}
+
 /* Return true when LOOP is if-convertible.
    LOOP is if-convertible if:
    - it is innermost,
@@ -582,8 +786,6 @@ get_loop_body_in_if_conv_order (const struct loop *loop)
 static bool
 if_convertible_loop_p (struct loop *loop)
 {
-  basic_block bb;
-  gimple_stmt_iterator itr;
   unsigned int i;
   edge e;
   edge_iterator ei;
@@ -623,8 +825,21 @@ if_convertible_loop_p (struct loop *loop)
        return false;
     }
 
+  /* Don't if-convert the loop when the data dependences cannot be
+     computed: the loop won't be vectorized in that case.  */
+  {
+    VEC (data_reference_p, heap) *refs = VEC_alloc (data_reference_p, heap, 5);
+    VEC (ddr_p, heap) *ddrs = VEC_alloc (ddr_p, heap, 25);
+    bool res = compute_data_dependences_for_loop (loop, true, &refs, &ddrs);
+
+    free_data_refs (refs);
+    free_dependence_relations (ddrs);
+
+    if (!res)
+      return false;
+  }
+
   calculate_dominance_info (CDI_DOMINATORS);
-  calculate_dominance_info (CDI_POST_DOMINATORS);
 
   /* Allow statements that can be handled during if-conversion.  */
   ifc_bbs = get_loop_body_in_if_conv_order (loop);
@@ -632,71 +847,53 @@ if_convertible_loop_p (struct loop *loop)
     {
       if (dump_file && (dump_flags & TDF_DETAILS))
        fprintf (dump_file, "Irreducible loop\n");
-      free_dominance_info (CDI_POST_DOMINATORS);
       return false;
     }
 
   for (i = 0; i < loop->num_nodes; i++)
     {
-      bb = ifc_bbs[i];
+      basic_block bb = ifc_bbs[i];
 
       if (!if_convertible_bb_p (loop, bb, exit_bb))
        return false;
 
-      for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
-       if (!if_convertible_stmt_p (loop, bb, gsi_stmt (itr)))
-         return false;
+      if (bb_with_exit_edge_p (loop, bb))
+       exit_bb = bb;
+    }
 
-      itr = gsi_start_phis (bb);
+  if (!predicate_bbs (loop))
+    return false;
 
-      if (!gsi_end_p (itr))
-       FOR_EACH_EDGE (e, ei, bb->preds)
-         e->aux = NULL;
+  for (i = 0; i < loop->num_nodes; i++)
+    {
+      basic_block bb = ifc_bbs[i];
+      gimple_stmt_iterator itr;
 
-      for (; !gsi_end_p (itr); gsi_next (&itr))
+      for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
        if (!if_convertible_phi_p (loop, bb, gsi_stmt (itr)))
          return false;
 
-      if (bb_with_exit_edge_p (loop, bb))
-       exit_bb = bb;
+      /* For non predicated BBs, don't check their statements.  */
+      if (!is_predicated (bb))
+       continue;
+
+      for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
+       if (!if_convertible_stmt_p (loop, bb, gsi_stmt (itr)))
+         return false;
     }
 
   if (dump_file)
     fprintf (dump_file, "Applying if-conversion\n");
 
-  free_dominance_info (CDI_POST_DOMINATORS);
   return true;
 }
 
-/* During if-conversion, the bb->aux field is used to hold a predicate
-   list.  This function cleans for all the basic blocks in the given
-   LOOP their predicate list.  It also cleans up the e->aux field of
-   all the successor edges: e->aux is used to hold the true and false
-   conditions for conditional expressions.  */
-
-static void
-clean_predicate_lists (struct loop *loop)
-{
-  basic_block *bb;
-  unsigned int i;
-  edge e;
-  edge_iterator ei;
-
-  bb = get_loop_body (loop);
-  for (i = 0; i < loop->num_nodes; i++)
-    {
-      bb[i]->aux = NULL;
-      FOR_EACH_EDGE (e, ei, bb[i]->succs)
-       e->aux = NULL;
-    }
-  free (bb);
-}
-
-/* Basic block BB has two predecessors.  Using predecessor's bb->aux
-   field, set appropriate condition COND for the PHI node replacement.
-   Return true block whose phi arguments are selected when cond is
-   true.  LOOP is the loop containing the if-converted region, GSI is
-   the place to insert the code for the if-conversion.  */
+/* Basic block BB has two predecessors.  Using predecessor's bb
+   predicate, set an appropriate condition COND for the PHI node
+   replacement.  Return the true block whose phi arguments are
+   selected when cond is true.  LOOP is the loop containing the
+   if-converted region, GSI is the place to insert the code for the
+   if-conversion.  */
 
 static basic_block
 find_phi_replacement_condition (struct loop *loop,
@@ -735,7 +932,7 @@ find_phi_replacement_condition (struct loop *loop,
         See PR23115.  */
 
   /* Select condition that is not TRUTH_NOT_EXPR.  */
-  tmp_cond = (tree) (first_edge->src)->aux;
+  tmp_cond = bb_predicate (first_edge->src);
   gcc_assert (tmp_cond);
 
   if (TREE_CODE (tmp_cond) == TRUTH_NOT_EXPR)
@@ -753,13 +950,7 @@ find_phi_replacement_condition (struct loop *loop,
       || dominated_by_p (CDI_DOMINATORS,
                         second_edge->src, first_edge->src))
     {
-      *cond = (tree) (second_edge->src)->aux;
-
-      /* If there is a condition on an incoming edge, add it to the
-        incoming bb predicate.  */
-      if (second_edge->aux)
-       *cond = build2 (TRUTH_AND_EXPR, boolean_type_node,
-                       *cond, (tree) second_edge->aux);
+      *cond = bb_predicate (second_edge->src);
 
       if (TREE_CODE (*cond) == TRUTH_NOT_EXPR)
        *cond = invert_truthvalue (*cond);
@@ -768,15 +959,7 @@ find_phi_replacement_condition (struct loop *loop,
        first_edge = second_edge;
     }
   else
-    {
-      *cond = (tree) (first_edge->src)->aux;
-
-      /* If there is a condition on an incoming edge, add it to the
-        incoming bb predicate.  */
-      if (first_edge->aux)
-       *cond = build2 (TRUTH_AND_EXPR, boolean_type_node,
-                       *cond, (tree) first_edge->aux);
-    }
+    *cond = bb_predicate (first_edge->src);
 
   /* Gimplify the condition: the vectorizer prefers to have gimple
      values as conditions.  Various targets use different means to
@@ -820,31 +1003,37 @@ replace_phi_with_cond_gimple_assign_stmt (gimple phi, tree cond,
   gimple new_stmt;
   basic_block bb;
   tree rhs;
-  tree arg_0, arg_1;
+  tree arg;
 
   gcc_assert (gimple_code (phi) == GIMPLE_PHI
              && gimple_phi_num_args (phi) == 2);
 
   bb = gimple_bb (phi);
 
-  /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr.  */
-  if (EDGE_PRED (bb, 1)->src == true_bb)
-    {
-      arg_0 = gimple_phi_arg_def (phi, 1);
-      arg_1 = gimple_phi_arg_def (phi, 0);
-    }
+  arg = degenerate_phi_result (phi);
+  if (arg)
+    rhs = arg;
   else
     {
-      arg_0 = gimple_phi_arg_def (phi, 0);
-      arg_1 = gimple_phi_arg_def (phi, 1);
-    }
+      tree arg_0, arg_1;
+      /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr.  */
+      if (EDGE_PRED (bb, 1)->src == true_bb)
+       {
+         arg_0 = gimple_phi_arg_def (phi, 1);
+         arg_1 = gimple_phi_arg_def (phi, 0);
+       }
+      else
+       {
+         arg_0 = gimple_phi_arg_def (phi, 0);
+         arg_1 = gimple_phi_arg_def (phi, 1);
+       }
 
-  /* Build new RHS using selected condition and arguments.  */
-  rhs = build3 (COND_EXPR, TREE_TYPE (PHI_RESULT (phi)),
-               unshare_expr (cond), unshare_expr (arg_0),
-               unshare_expr (arg_1));
+      /* Build new RHS using selected condition and arguments.  */
+      rhs = build3 (COND_EXPR, TREE_TYPE (PHI_RESULT (phi)),
+                   unshare_expr (cond), arg_0, arg_1);
+    }
 
-  new_stmt = gimple_build_assign (unshare_expr (PHI_RESULT (phi)), rhs);
+  new_stmt = gimple_build_assign (PHI_RESULT (phi), rhs);
   SSA_NAME_DEF_STMT (gimple_phi_result (phi)) = new_stmt;
   gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
   update_stmt (new_stmt);
@@ -856,11 +1045,11 @@ replace_phi_with_cond_gimple_assign_stmt (gimple phi, tree cond,
     }
 }
 
-/* Process phi nodes for the given LOOP.  Replace phi nodes with
-   conditional modify expressions.  */
+/* Replaces in LOOP all the phi nodes other than those in the
+   LOOP->header block with conditional modify expressions.  */
 
 static void
-process_phi_nodes (struct loop *loop)
+ifconvert_phi_nodes (struct loop *loop)
 {
   basic_block bb;
   unsigned int orig_loop_num_nodes = loop->num_nodes;
@@ -878,12 +1067,13 @@ process_phi_nodes (struct loop *loop)
        continue;
 
       phi_gsi = gsi_start_phis (bb);
-      gsi = gsi_after_labels (bb);
+      if (gsi_end_p (phi_gsi))
+       continue;
 
       /* BB has two predecessors.  Using predecessor's aux field, set
         appropriate condition for the PHI node replacement.  */
-      if (!gsi_end_p (phi_gsi))
-       true_bb = find_phi_replacement_condition (loop, bb, &cond, &gsi);
+      gsi = gsi_after_labels (bb);
+      true_bb = find_phi_replacement_condition (loop, bb, &cond, &gsi);
 
       while (!gsi_end_p (phi_gsi))
        {
@@ -892,10 +1082,91 @@ process_phi_nodes (struct loop *loop)
          release_phi_node (phi);
          gsi_next (&phi_gsi);
        }
+
       set_phi_nodes (bb, NULL);
     }
 }
 
+/* Insert in each basic block of LOOP the statements produced by the
+   gimplification of the predicates.  */
+
+static void
+insert_gimplified_predicates (loop_p loop)
+{
+  unsigned int i;
+
+  for (i = 0; i < loop->num_nodes; i++)
+    {
+      basic_block bb = ifc_bbs[i];
+      gimple_seq stmts = bb_predicate_gimplified_stmts (bb);
+
+      if (!is_predicated (bb))
+       {
+         /* Do not insert statements for a basic block that is not
+            predicated.  Also make sure that the predicate of the
+            basic block is set to true.  */
+         reset_bb_predicate (bb);
+         continue;
+       }
+
+      if (stmts)
+       {
+         gimple_stmt_iterator gsi = gsi_last_bb (bb);
+
+         if (gsi_end_p (gsi)
+             || gimple_code (gsi_stmt (gsi)) == GIMPLE_COND)
+           gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
+         else
+           gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
+
+         /* Once the sequence is code generated, set it to NULL.  */
+         set_bb_predicate_gimplified_stmts (bb, NULL);
+       }
+    }
+}
+
+/* Remove all GIMPLE_CONDs and GIMPLE_LABELs of all the basic blocks
+   other than the exit and latch of the LOOP.  Also resets the
+   GIMPLE_DEBUG information.  */
+
+static void
+remove_conditions_and_labels (loop_p loop)
+{
+  gimple_stmt_iterator gsi;
+  unsigned int i;
+
+  for (i = 0; i < loop->num_nodes; i++)
+    {
+      basic_block bb = ifc_bbs[i];
+
+      if (bb_with_exit_edge_p (loop, bb)
+        || bb == loop->latch)
+      continue;
+
+      for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
+       switch (gimple_code (gsi_stmt (gsi)))
+         {
+         case GIMPLE_COND:
+         case GIMPLE_LABEL:
+           gsi_remove (&gsi, true);
+           break;
+
+         case GIMPLE_DEBUG:
+           /* ??? Should there be conditional GIMPLE_DEBUG_BINDs?  */
+           if (gimple_debug_bind_p (gsi_stmt (gsi)))
+             {
+               gimple_debug_bind_reset_value (gsi_stmt (gsi));
+               update_stmt (gsi_stmt (gsi));
+             }
+           gsi_next (&gsi);
+           break;
+
+         default:
+           gsi_next (&gsi);
+         }
+    }
+}
+
 /* Combine all the basic blocks from LOOP into one or two super basic
    blocks.  Replace PHI nodes with conditional modify expressions.  */
 
@@ -908,8 +1179,9 @@ combine_blocks (struct loop *loop)
   edge e;
   edge_iterator ei;
 
-  /* Process phi nodes to prepare blocks for merge.  */
-  process_phi_nodes (loop);
+  remove_conditions_and_labels (loop);
+  insert_gimplified_predicates (loop);
+  ifconvert_phi_nodes (loop);
 
   /* Merge basic blocks: first remove all the edges in the loop,
      except for those from the exit block.  */
@@ -973,17 +1245,9 @@ combine_blocks (struct loop *loop)
       if (bb == exit_bb || bb == loop->latch)
        continue;
 
-      /* Remove labels and make stmts member of loop->header.  */
-      for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
-       {
-         if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
-           gsi_remove (&gsi, true);
-         else
-           {
-             gimple_set_bb (gsi_stmt (gsi), merge_target_bb);
-             gsi_next (&gsi);
-           }
-       }
+      /* Make stmts member of loop->header.  */
+      for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+       gimple_set_bb (gsi_stmt (gsi), merge_target_bb);
 
       /* Update stmt list.  */
       last = gsi_last_bb (merge_target_bb);
@@ -995,9 +1259,7 @@ combine_blocks (struct loop *loop)
 
   /* If possible, merge loop header to the block with the exit edge.
      This reduces the number of basic blocks to two, to please the
-     vectorizer that handles only loops with two nodes.
-
-     FIXME: Call cleanup_tree_cfg.  */
+     vectorizer that handles only loops with two nodes.  */
   if (exit_bb
       && exit_bb != loop->header
       && can_merge_blocks_p (loop->header, exit_bb))
@@ -1005,72 +1267,37 @@ combine_blocks (struct loop *loop)
 }
 
 /* If-convert LOOP when it is legal.  For the moment this pass has no
-   profitability analysis.  */
+   profitability analysis.  Returns true when something changed.  */
 
-static void
+static bool
 tree_if_conversion (struct loop *loop)
 {
-  gimple_stmt_iterator itr;
-  unsigned int i;
-
+  bool changed = false;
   ifc_bbs = NULL;
 
-  /* If-conversion is not appropriate for all loops.  First, check if
-     the loop is if-convertible.  */
-  if (!if_convertible_loop_p (loop))
-    {
-      if (dump_file && (dump_flags & TDF_DETAILS))
-       fprintf (dump_file, "-------------------------\n");
-      if (ifc_bbs)
-       {
-         free (ifc_bbs);
-         ifc_bbs = NULL;
-       }
-      free_dominance_info (CDI_POST_DOMINATORS);
-      return;
-    }
-
-  for (i = 0; i < loop->num_nodes; i++)
-    {
-      basic_block bb = ifc_bbs [i];
-      tree cond = (tree) bb->aux;
+  if (!if_convertible_loop_p (loop)
+      || !dbg_cnt (if_conversion_tree))
+    goto cleanup;
 
-      /* Process all the statements in this basic block.
-        Remove conditional expression, if any, and annotate
-        destination basic block(s) appropriately.  */
-      for (itr = gsi_start_bb (bb); !gsi_end_p (itr); /* empty */)
-       {
-         gimple t = gsi_stmt (itr);
-         cond = tree_if_convert_stmt (loop, t, cond, &itr);
-         if (!gsi_end_p (itr))
-           gsi_next (&itr);
-       }
+  /* Now all statements are if-convertible.  Combine all the basic
+     blocks into one huge basic block doing the if-conversion
+     on-the-fly.  */
+  combine_blocks (loop);
+  changed = true;
 
-      /* If current bb has only one successor, then consider it as an
-        unconditional goto.  */
-      if (single_succ_p (bb))
-       {
-         basic_block bb_n = single_succ (bb);
+ cleanup:
+  if (ifc_bbs)
+    {
+      unsigned int i;
 
-         /* The successor bb inherits the predicate of its
-            predecessor.  If there is no predicate in the predecessor
-            bb, then consider the successor bb as always executed.  */
-         if (cond == NULL_TREE)
-           cond = boolean_true_node;
+      for (i = 0; i < loop->num_nodes; i++)
+       free_bb_predicate (ifc_bbs[i]);
 
-         add_to_predicate_list (bb_n, cond);
-       }
+      free (ifc_bbs);
+      ifc_bbs = NULL;
     }
 
-  /* Now, all statements are if-converted and basic blocks are
-     annotated appropriately.  Combine all the basic blocks into one
-     huge basic block.  */
-  combine_blocks (loop);
-
-  /* clean up */
-  clean_predicate_lists (loop);
-  free (ifc_bbs);
-  ifc_bbs = NULL;
+  return changed;
 }
 
 /* Tree if-conversion pass management.  */
@@ -1080,20 +1307,24 @@ main_tree_if_conversion (void)
 {
   loop_iterator li;
   struct loop *loop;
+  bool changed = false;
 
   if (number_of_loops () <= 1)
     return 0;
 
   FOR_EACH_LOOP (li, loop, 0)
-    tree_if_conversion (loop);
+    changed |= tree_if_conversion (loop);
 
-  return 0;
+  return changed ? TODO_cleanup_cfg : 0;
 }
 
+/* Returns true when the if-conversion pass is enabled.  */
+
 static bool
 gate_tree_if_conversion (void)
 {
-  return flag_tree_vectorize != 0;
+  return ((flag_tree_vectorize && flag_tree_loop_if_convert != 0)
+         || flag_tree_loop_if_convert == 1);
 }
 
 struct gimple_opt_pass pass_if_conversion =