OSDN Git Service

* tree-cfg.c (dump_function_to_file): Use cfun info only if it
[pf3gnuchains/gcc-fork.git] / gcc / tree-cfg.c
index 680262c..28af511 100644 (file)
@@ -54,10 +54,6 @@ Boston, MA 02111-1307, USA.  */
 /* Initial capacity for the basic block array.  */
 static const int initial_cfg_capacity = 20;
 
-/* Mapping of labels to their associated blocks.  This can greatly speed up
-   building of the CFG in code with lots of gotos.  */
-static GTY(()) varray_type label_to_block_map;
-
 /* This hash table allows us to efficiently lookup all CASE_LABEL_EXPRs
    which use a particular edge.  The CASE_LABEL_EXPRs are chained together
    via their TREE_CHAIN field, which we clear after we're done with the
@@ -129,6 +125,7 @@ static bool tree_can_merge_blocks_p (basic_block, basic_block);
 static void remove_bb (basic_block);
 static bool cleanup_control_flow (void);
 static bool cleanup_control_expr_graph (basic_block, block_stmt_iterator);
+static edge find_taken_edge_computed_goto (basic_block, tree);
 static edge find_taken_edge_cond_expr (basic_block, tree);
 static edge find_taken_edge_switch_expr (basic_block, tree);
 static tree find_case_label_for_value (tree, tree);
@@ -149,9 +146,6 @@ build_tree_cfg (tree *tp)
   /* Register specific tree functions.  */
   tree_register_cfg_hooks ();
 
-  /* Initialize rbi_pool.  */
-  alloc_rbi_pool ();
-
   /* Initialize the basic block array.  */
   init_flow ();
   profile_status = PROFILE_ABSENT;
@@ -440,6 +434,29 @@ create_bb (void *h, void *e, basic_block after)
                                 Edge creation
 ---------------------------------------------------------------------------*/
 
+/* Fold COND_EXPR_COND of each COND_EXPR.  */
+
+static void
+fold_cond_expr_cond (void)
+{
+  basic_block bb;
+
+  FOR_EACH_BB (bb)
+    {
+      tree stmt = last_stmt (bb);
+
+      if (stmt
+         && TREE_CODE (stmt) == COND_EXPR)
+       {
+         tree cond = fold (COND_EXPR_COND (stmt));
+         if (integer_zerop (cond))
+           COND_EXPR_COND (stmt) = boolean_false_node;
+         else if (integer_onep (cond))
+           COND_EXPR_COND (stmt) = boolean_true_node;
+       }
+    }
+}
+
 /* Join all the blocks in the flowgraph.  */
 
 static void
@@ -478,6 +495,9 @@ make_edges (void)
      builder inserted for completeness.  */
   remove_fake_exit_edges ();
 
+  /* Fold COND_EXPR_COND of each COND_EXPR.  */
+  fold_cond_expr_cond ();
+
   /* Clean up the graph and warn for unreachable code.  */
   cleanup_tree_cfg ();
 }
@@ -785,7 +805,7 @@ make_switch_expr_edges (basic_block bb)
 /* Return the basic block holding label DEST.  */
 
 basic_block
-label_to_block (tree dest)
+label_to_block_fn (struct function *ifun, tree dest)
 {
   int uid = LABEL_DECL_UID (dest);
 
@@ -801,16 +821,15 @@ label_to_block (tree dest)
       bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
       uid = LABEL_DECL_UID (dest);
     }
-  return VARRAY_BB (label_to_block_map, uid);
+  return VARRAY_BB (ifun->cfg->x_label_to_block_map, uid);
 }
 
-
 /* Create edges for a goto statement at block BB.  */
 
 static void
 make_goto_expr_edges (basic_block bb)
 {
-  tree goto_t, dest;
+  tree goto_t;
   basic_block target_bb;
   int for_call;
   block_stmt_iterator last = bsi_last (bb);
@@ -821,13 +840,10 @@ make_goto_expr_edges (basic_block bb)
      CALL_EXPR or MODIFY_EXPR), then the edge is an abnormal edge resulting
      from a nonlocal goto.  */
   if (TREE_CODE (goto_t) != GOTO_EXPR)
-    {
-      dest = error_mark_node;
-      for_call = 1;
-    }
+    for_call = 1;
   else
     {
-      dest = GOTO_DESTINATION (goto_t);
+      tree dest = GOTO_DESTINATION (goto_t);
       for_call = 0;
 
       /* A GOTO to a local label creates normal edges.  */
@@ -932,6 +948,30 @@ cleanup_tree_cfg (void)
 }
 
 
+/* Cleanup cfg and repair loop structures.  */
+
+void
+cleanup_tree_cfg_loop (void)
+{
+  bitmap changed_bbs = BITMAP_ALLOC (NULL);
+
+  cleanup_tree_cfg ();
+
+  fix_loop_structure (current_loops, changed_bbs);
+  calculate_dominance_info (CDI_DOMINATORS);
+
+  /* This usually does nothing.  But sometimes parts of cfg that originally
+     were inside a loop get out of it due to edge removal (since they
+     become unreachable by back edges from latch).  */
+  rewrite_into_loop_closed_ssa (changed_bbs);
+
+  BITMAP_FREE (changed_bbs);
+
+#ifdef ENABLE_CHECKING
+  verify_loop_structure (current_loops);
+#endif
+}
+
 /* Cleanup useless labels in basic blocks.  This is something we wish
    to do early because it allows us to group case labels before creating
    the edges for the CFG, and it speeds up block statement iterators in
@@ -1141,7 +1181,7 @@ group_case_labels (void)
           i = 0;
          while (i < old_size - 1)
            {
-             tree base_case, base_label, base_high, type;
+             tree base_case, base_label, base_high;
              base_case = TREE_VEC_ELT (labels, i);
 
              gcc_assert (base_case);
@@ -1157,7 +1197,6 @@ group_case_labels (void)
                  continue;
                }
 
-             type = TREE_TYPE (CASE_LOW (base_case));
              base_high = CASE_HIGH (base_case) ?
                CASE_HIGH (base_case) : CASE_LOW (base_case);
              i++;
@@ -1209,16 +1248,16 @@ tree_can_merge_blocks_p (basic_block a, basic_block b)
   tree stmt;
   block_stmt_iterator bsi;
 
-  if (EDGE_COUNT (a->succs) != 1)
+  if (!single_succ_p (a))
     return false;
 
-  if (EDGE_SUCC (a, 0)->flags & EDGE_ABNORMAL)
+  if (single_succ_edge (a)->flags & EDGE_ABNORMAL)
     return false;
 
-  if (EDGE_SUCC (a, 0)->dest != b)
+  if (single_succ (a) != b)
     return false;
 
-  if (EDGE_COUNT (b->preds) > 1)
+  if (!single_pred_p (b))
     return false;
 
   if (b == EXIT_BLOCK_PTR)
@@ -1235,8 +1274,7 @@ tree_can_merge_blocks_p (basic_block a, basic_block b)
       && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
     return false;
 
-  /* There may be no phi nodes at the start of b.  Most of these degenerate
-     phi nodes should be cleaned up by kill_redundant_phi_nodes.  */
+  /* There may be no PHI nodes at the start of B.  */
   if (phi_nodes (b))
     return false;
 
@@ -1250,6 +1288,11 @@ tree_can_merge_blocks_p (basic_block a, basic_block b)
        return false;
     }
 
+  /* Protect the loop latches.  */
+  if (current_loops
+      && b->loop_father->latch == b)
+    return false;
+
   return true;
 }
 
@@ -1268,14 +1311,29 @@ tree_merge_blocks (basic_block a, basic_block b)
   /* Ensure that B follows A.  */
   move_block_after (b, a);
 
-  gcc_assert (EDGE_SUCC (a, 0)->flags & EDGE_FALLTHRU);
+  gcc_assert (single_succ_edge (a)->flags & EDGE_FALLTHRU);
   gcc_assert (!last_stmt (a) || !stmt_ends_bb_p (last_stmt (a)));
 
   /* Remove labels from B and set bb_for_stmt to A for other statements.  */
   for (bsi = bsi_start (b); !bsi_end_p (bsi);)
     {
       if (TREE_CODE (bsi_stmt (bsi)) == LABEL_EXPR)
-       bsi_remove (&bsi);
+       {
+         tree label = bsi_stmt (bsi);
+
+         bsi_remove (&bsi);
+         /* Now that we can thread computed gotos, we might have
+            a situation where we have a forced label in block B
+            However, the label at the start of block B might still be
+            used in other ways (think about the runtime checking for
+            Fortran assigned gotos).  So we can not just delete the
+            label.  Instead we move the label to the start of block A.  */
+         if (FORCED_LABEL (LABEL_EXPR_LABEL (label)))
+           {
+             block_stmt_iterator dest_bsi = bsi_start (a);
+             bsi_insert_before (&dest_bsi, label, BSI_NEW_STMT);
+           }
+       }
       else
        {
          set_bb_for_stmt (bsi_stmt (bsi), a);
@@ -1838,126 +1896,6 @@ struct tree_opt_pass pass_remove_useless_stmts =
   0                                    /* letter */
 };
 
-
-/* Remove obviously useless statements in basic block BB.  */
-
-static void
-cfg_remove_useless_stmts_bb (basic_block bb)
-{
-  block_stmt_iterator bsi;
-  tree stmt = NULL_TREE;
-  tree cond, var = NULL_TREE, val = NULL_TREE;
-  struct var_ann_d *ann;
-
-  /* Check whether we come here from a condition, and if so, get the
-     condition.  */
-  if (EDGE_COUNT (bb->preds) != 1
-      || !(EDGE_PRED (bb, 0)->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
-    return;
-
-  cond = COND_EXPR_COND (last_stmt (EDGE_PRED (bb, 0)->src));
-
-  if (TREE_CODE (cond) == VAR_DECL || TREE_CODE (cond) == PARM_DECL)
-    {
-      var = cond;
-      val = (EDGE_PRED (bb, 0)->flags & EDGE_FALSE_VALUE
-            ? boolean_false_node : boolean_true_node);
-    }
-  else if (TREE_CODE (cond) == TRUTH_NOT_EXPR
-          && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
-              || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL))
-    {
-      var = TREE_OPERAND (cond, 0);
-      val = (EDGE_PRED (bb, 0)->flags & EDGE_FALSE_VALUE
-            ? boolean_true_node : boolean_false_node);
-    }
-  else
-    {
-      if (EDGE_PRED (bb, 0)->flags & EDGE_FALSE_VALUE)
-       cond = invert_truthvalue (cond);
-      if (TREE_CODE (cond) == EQ_EXPR
-         && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
-             || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL)
-         && (TREE_CODE (TREE_OPERAND (cond, 1)) == VAR_DECL
-             || TREE_CODE (TREE_OPERAND (cond, 1)) == PARM_DECL
-             || TREE_CONSTANT (TREE_OPERAND (cond, 1))))
-       {
-         var = TREE_OPERAND (cond, 0);
-         val = TREE_OPERAND (cond, 1);
-       }
-      else
-       return;
-    }
-
-  /* Only work for normal local variables.  */
-  ann = var_ann (var);
-  if (!ann
-      || ann->may_aliases
-      || TREE_ADDRESSABLE (var))
-    return;
-
-  if (! TREE_CONSTANT (val))
-    {
-      ann = var_ann (val);
-      if (!ann
-         || ann->may_aliases
-         || TREE_ADDRESSABLE (val))
-       return;
-    }
-
-  /* Ignore floating point variables, since comparison behaves weird for
-     them.  */
-  if (FLOAT_TYPE_P (TREE_TYPE (var)))
-    return;
-
-  for (bsi = bsi_start (bb); !bsi_end_p (bsi);)
-    {
-      stmt = bsi_stmt (bsi);
-
-      /* If the THEN/ELSE clause merely assigns a value to a variable/parameter
-        which is already known to contain that value, then remove the useless
-        THEN/ELSE clause.  */
-      if (TREE_CODE (stmt) == MODIFY_EXPR
-         && TREE_OPERAND (stmt, 0) == var
-         && operand_equal_p (val, TREE_OPERAND (stmt, 1), 0))
-       {
-         bsi_remove (&bsi);
-         continue;
-       }
-
-      /* Invalidate the var if we encounter something that could modify it.
-        Likewise for the value it was previously set to.  Note that we only
-        consider values that are either a VAR_DECL or PARM_DECL so we
-        can test for conflict very simply.  */
-      if (TREE_CODE (stmt) == ASM_EXPR
-         || (TREE_CODE (stmt) == MODIFY_EXPR
-             && (TREE_OPERAND (stmt, 0) == var
-                 || TREE_OPERAND (stmt, 0) == val)))
-       return;
-  
-      bsi_next (&bsi);
-    }
-}
-
-
-/* A CFG-aware version of remove_useless_stmts.  */
-
-void
-cfg_remove_useless_stmts (void)
-{
-  basic_block bb;
-
-#ifdef ENABLE_CHECKING
-  verify_flow_info ();
-#endif
-
-  FOR_EACH_BB (bb)
-    {
-      cfg_remove_useless_stmts_bb (bb);
-    }
-}
-
-
 /* Remove PHI nodes associated with basic block BB and all edges out of BB.  */
 
 static void
@@ -1971,7 +1909,7 @@ remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
   while (phi)
     {
       tree next = PHI_CHAIN (phi);
-      remove_phi_node (phi, NULL_TREE, bb);
+      remove_phi_node (phi, NULL_TREE);
       phi = next;
     }
 
@@ -1987,7 +1925,11 @@ static void
 remove_bb (basic_block bb)
 {
   block_stmt_iterator i;
+#ifdef USE_MAPPED_LOCATION
+  source_location loc = UNKNOWN_LOCATION;
+#else
   source_locus loc = 0;
+#endif
 
   if (dump_file)
     {
@@ -1999,6 +1941,20 @@ remove_bb (basic_block bb)
        }
     }
 
+  /* If we remove the header or the latch of a loop, mark the loop for
+     removal by setting its header and latch to NULL.  */
+  if (current_loops)
+    {
+      struct loop *loop = bb->loop_father;
+
+      if (loop->latch == bb
+         || loop->header == bb)
+       {
+         loop->latch = NULL;
+         loop->header = NULL;
+       }
+    }
+
   /* Remove all the instructions in the block.  */
   for (i = bsi_start (bb); !bsi_end_p (i);)
     {
@@ -2026,15 +1982,15 @@ remove_bb (basic_block bb)
         program that are indeed unreachable.  */
       if (TREE_CODE (stmt) != GOTO_EXPR && EXPR_HAS_LOCATION (stmt) && !loc)
        {
-         source_locus t;
-
 #ifdef USE_MAPPED_LOCATION
-         t = EXPR_LOCATION (stmt);
+         if (EXPR_HAS_LOCATION (stmt))
+           loc = EXPR_LOCATION (stmt);
 #else
+         source_locus t;
          t = EXPR_LOCUS (stmt);
-#endif
          if (t && LOCATION_LINE (*t) > 0)
            loc = t;
+#endif
        }
     }
 
@@ -2042,10 +1998,11 @@ remove_bb (basic_block bb)
      block is unreachable.  We walk statements backwards in the
      loop above, so the last statement we process is the first statement
      in the block.  */
-  if (warn_notreached && loc)
 #ifdef USE_MAPPED_LOCATION
+  if (warn_notreached && loc > BUILTINS_LOCATION)
     warning ("%Hwill never be executed", &loc);
 #else
+  if (warn_notreached && loc)
     warning ("%Hwill never be executed", loc);
 #endif
 
@@ -2091,6 +2048,52 @@ cleanup_control_flow (void)
          || TREE_CODE (stmt) == SWITCH_EXPR)
        retval |= cleanup_control_expr_graph (bb, bsi);
 
+      /* If we had a computed goto which has a compile-time determinable
+        destination, then we can eliminate the goto.  */
+      if (TREE_CODE (stmt) == GOTO_EXPR
+         && TREE_CODE (GOTO_DESTINATION (stmt)) == ADDR_EXPR
+         && TREE_CODE (TREE_OPERAND (GOTO_DESTINATION (stmt), 0)) == LABEL_DECL)
+       {
+         edge e;
+         tree label;
+         edge_iterator ei;
+         basic_block target_block;
+         bool removed_edge = false;
+
+         /* First look at all the outgoing edges.  Delete any outgoing
+            edges which do not go to the right block.  For the one
+            edge which goes to the right block, fix up its flags.  */
+         label = TREE_OPERAND (GOTO_DESTINATION (stmt), 0);
+         target_block = label_to_block (label);
+         for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
+           {
+             if (e->dest != target_block)
+               {
+                 removed_edge = true;
+                 remove_edge (e);
+               }
+             else
+               {
+                 /* Turn off the EDGE_ABNORMAL flag.  */
+                 e->flags &= ~EDGE_ABNORMAL;
+
+                 /* And set EDGE_FALLTHRU.  */
+                 e->flags |= EDGE_FALLTHRU;
+                 ei_next (&ei);
+               }
+           }
+
+         /* If we removed one or more edges, then we will need to fix the
+            dominators.  It may be possible to incrementally update them.  */
+         if (removed_edge)
+           free_dominance_info (CDI_DOMINATORS);
+
+         /* Remove the GOTO_EXPR as it is not needed.  The CFG has all the
+            relevant information we need.  */
+         bsi_remove (&bsi);
+         retval = true;
+       }
+
       /* Check for indirect calls that have been turned into
         noreturn calls.  */
       if (noreturn_call_p (stmt) && remove_fallthru_edge (bb->succs))
@@ -2113,7 +2116,7 @@ cleanup_control_expr_graph (basic_block bb, block_stmt_iterator bsi)
   bool retval = false;
   tree expr = bsi_stmt (bsi), val;
 
-  if (EDGE_COUNT (bb->succs) > 1)
+  if (!single_succ_p (bb))
     {
       edge e;
       edge_iterator ei;
@@ -2155,7 +2158,7 @@ cleanup_control_expr_graph (basic_block bb, block_stmt_iterator bsi)
        taken_edge->probability = REG_BR_PROB_BASE;
     }
   else
-    taken_edge = EDGE_SUCC (bb, 0);
+    taken_edge = single_succ_edge (bb);
 
   bsi_remove (&bsi);
   taken_edge->flags = EDGE_FALLTHRU;
@@ -2198,14 +2201,7 @@ find_taken_edge (basic_block bb, tree val)
   gcc_assert (is_ctrl_stmt (stmt));
   gcc_assert (val);
 
-  /* If VAL is a predicate of the form N RELOP N, where N is an
-     SSA_NAME, we can usually determine its truth value.  */
-  if (COMPARISON_CLASS_P (val))
-    val = fold (val);
-
-  /* If VAL is not a constant, we can't determine which edge might
-     be taken.  */
-  if (!really_constant_p (val))
+  if (! is_gimple_min_invariant (val))
     return NULL;
 
   if (TREE_CODE (stmt) == COND_EXPR)
@@ -2214,9 +2210,31 @@ find_taken_edge (basic_block bb, tree val)
   if (TREE_CODE (stmt) == SWITCH_EXPR)
     return find_taken_edge_switch_expr (bb, val);
 
+  if (computed_goto_p (stmt))
+    return find_taken_edge_computed_goto (bb, TREE_OPERAND( val, 0));
+
   gcc_unreachable ();
 }
 
+/* Given a constant value VAL and the entry block BB to a GOTO_EXPR
+   statement, determine which of the outgoing edges will be taken out of the
+   block.  Return NULL if either edge may be taken.  */
+
+static edge
+find_taken_edge_computed_goto (basic_block bb, tree val)
+{
+  basic_block dest;
+  edge e = NULL;
+
+  dest = label_to_block (val);
+  if (dest)
+    {
+      e = find_edge (bb, dest);
+      gcc_assert (e != NULL);
+    }
+
+  return e;
+}
 
 /* Given a constant value VAL and the entry block BB to a COND_EXPR
    statement, determine which of the two edges will be taken out of the
@@ -2228,21 +2246,12 @@ find_taken_edge_cond_expr (basic_block bb, tree val)
   edge true_edge, false_edge;
 
   extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
-
-  /* Otherwise, try to determine which branch of the if() will be taken.
-     If VAL is a constant but it can't be reduced to a 0 or a 1, then
-     we don't really know which edge will be taken at runtime.  This
-     may happen when comparing addresses (e.g., if (&var1 == 4)).  */
-  if (integer_nonzerop (val))
-    return true_edge;
-  else if (integer_zerop (val))
-    return false_edge;
-  else
-    return NULL;
+  
+  gcc_assert (TREE_CODE (val) == INTEGER_CST);
+  return (zero_p (val) ? false_edge : true_edge);
 }
 
-
-/* Given a constant value VAL and the entry block BB to a SWITCH_EXPR
+/* Given an INTEGER_CST VAL and the entry block BB to a SWITCH_EXPR
    statement, determine which edge will be taken out of the block.  Return
    NULL if any edge may be taken.  */
 
@@ -2253,9 +2262,6 @@ find_taken_edge_switch_expr (basic_block bb, tree val)
   basic_block dest_bb;
   edge e;
 
-  if (TREE_CODE (val) != INTEGER_CST)
-    return NULL;
-
   switch_expr = last_stmt (bb);
   taken_case = find_case_label_for_value (switch_expr, val);
   dest_bb = label_to_block (CASE_LABEL (taken_case));
@@ -2416,7 +2422,7 @@ dump_cfg_stats (FILE *file)
 {
   static long max_num_merged_labels = 0;
   unsigned long size, total = 0;
-  int n_edges;
+  long num_edges;
   basic_block bb;
   const char * const fmt_str   = "%-30s%-13s%12s\n";
   const char * const fmt_str_1 = "%-30s%13d%11lu%c\n";
@@ -2437,12 +2443,12 @@ dump_cfg_stats (FILE *file)
   fprintf (file, fmt_str_1, "Basic blocks", n_basic_blocks,
           SCALE (size), LABEL (size));
 
-  n_edges = 0;
+  num_edges = 0;
   FOR_EACH_BB (bb)
-    n_edges += EDGE_COUNT (bb->succs);
-  size = n_edges * sizeof (struct edge_def);
+    num_edges += EDGE_COUNT (bb->succs);
+  size = num_edges * sizeof (struct edge_def);
   total += size;
-  fprintf (file, fmt_str_1, "Edges", n_edges, SCALE (size), LABEL (size));
+  fprintf (file, fmt_str_1, "Edges", num_edges, SCALE (size), LABEL (size));
 
   size = n_basic_blocks * sizeof (struct bb_ann_d);
   total += size;
@@ -2634,8 +2640,6 @@ simple_goto_p (tree expr)
 static inline bool
 stmt_starts_bb_p (tree t, tree prev_t)
 {
-  enum tree_code code;
-
   if (t == NULL_TREE)
     return false;
 
@@ -2643,16 +2647,14 @@ stmt_starts_bb_p (tree t, tree prev_t)
      statement wasn't a label of the same type.  This prevents the
      creation of consecutive blocks that have nothing but a single
      label.  */
-  code = TREE_CODE (t);
-  if (code == LABEL_EXPR)
+  if (TREE_CODE (t) == LABEL_EXPR)
     {
       /* Nonlocal and computed GOTO targets always start a new block.  */
-      if (code == LABEL_EXPR
-         && (DECL_NONLOCAL (LABEL_EXPR_LABEL (t))
-             || FORCED_LABEL (LABEL_EXPR_LABEL (t))))
+      if (DECL_NONLOCAL (LABEL_EXPR_LABEL (t))
+         || FORCED_LABEL (LABEL_EXPR_LABEL (t)))
        return true;
 
-      if (prev_t && TREE_CODE (prev_t) == code)
+      if (prev_t && TREE_CODE (prev_t) == LABEL_EXPR)
        {
          if (DECL_NONLOCAL (LABEL_EXPR_LABEL (prev_t)))
            return true;
@@ -2718,14 +2720,14 @@ disband_implicit_edges (void)
        {
          /* Remove the RETURN_EXPR if we may fall though to the exit
             instead.  */
-         gcc_assert (EDGE_COUNT (bb->succs) == 1);
-         gcc_assert (EDGE_SUCC (bb, 0)->dest == EXIT_BLOCK_PTR);
+         gcc_assert (single_succ_p (bb));
+         gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR);
 
          if (bb->next_bb == EXIT_BLOCK_PTR
              && !TREE_OPERAND (stmt, 0))
            {
              bsi_remove (&last);
-             EDGE_SUCC (bb, 0)->flags |= EDGE_FALLTHRU;
+             single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
            }
          continue;
        }
@@ -2767,7 +2769,6 @@ delete_tree_cfg_annotations (void)
     free_blocks_annotations ();
 
   label_to_block_map = NULL;
-  free_rbi_pool ();
   FOR_EACH_BB (bb)
     bb->rbi = NULL;
 }
@@ -2891,6 +2892,24 @@ bsi_for_stmt (tree stmt)
   gcc_unreachable ();
 }
 
+/* Mark statement T as modified, and update it.  */
+static inline void
+update_modified_stmts (tree t)
+{
+  if (TREE_CODE (t) == STATEMENT_LIST)
+    {
+      tree_stmt_iterator i;
+      tree stmt;
+      for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
+        {
+         stmt = tsi_stmt (i);
+         update_stmt_if_modified (stmt);
+       }
+    }
+  else
+    update_stmt_if_modified (t);
+}
+
 /* Insert statement (or statement list) T before the statement
    pointed-to by iterator I.  M specifies how to update iterator I
    after insertion (see enum bsi_iterator_update).  */
@@ -2899,8 +2918,8 @@ void
 bsi_insert_before (block_stmt_iterator *i, tree t, enum bsi_iterator_update m)
 {
   set_bb_for_stmt (t, i->bb);
+  update_modified_stmts (t);
   tsi_link_before (&i->tsi, t, m);
-  modify_stmt (t);
 }
 
 
@@ -2912,8 +2931,8 @@ void
 bsi_insert_after (block_stmt_iterator *i, tree t, enum bsi_iterator_update m)
 {
   set_bb_for_stmt (t, i->bb);
+  update_modified_stmts (t);
   tsi_link_after (&i->tsi, t, m);
-  modify_stmt (t);
 }
 
 
@@ -2925,7 +2944,9 @@ bsi_remove (block_stmt_iterator *i)
 {
   tree t = bsi_stmt (*i);
   set_bb_for_stmt (t, NULL);
+  delink_stmt_imm_use (t);
   tsi_delink (&i->tsi);
+  mark_stmt_modified (t);
 }
 
 
@@ -2989,7 +3010,8 @@ bsi_replace (const block_stmt_iterator *bsi, tree stmt, bool preserve_eh_info)
     }
 
   *bsi_stmt_ptr (*bsi) = stmt;
-  modify_stmt (stmt);
+  mark_stmt_modified (stmt);
+  update_modified_stmts (stmt);
 }
 
 
@@ -3020,7 +3042,7 @@ tree_find_edge_insert_loc (edge e, block_stmt_iterator *bsi,
      would have to examine the PHIs to prove that none of them used
      the value set by the statement we want to insert on E.  That
      hardly seems worth the effort.  */
-  if (EDGE_COUNT (dest->preds) == 1
+  if (single_pred_p (dest)
       && ! phi_nodes (dest)
       && dest != EXIT_BLOCK_PTR)
     {
@@ -3052,7 +3074,7 @@ tree_find_edge_insert_loc (edge e, block_stmt_iterator *bsi,
      Except for the entry block.  */
   src = e->src;
   if ((e->flags & EDGE_ABNORMAL) == 0
-      && EDGE_COUNT (src->succs) == 1
+      && single_succ_p (src)
       && src != ENTRY_BLOCK_PTR)
     {
       *bsi = bsi_last (src);
@@ -3083,7 +3105,7 @@ tree_find_edge_insert_loc (edge e, block_stmt_iterator *bsi,
   dest = split_edge (e);
   if (new_bb)
     *new_bb = dest;
-  e = EDGE_PRED (dest, 0);
+  e = single_pred_edge (dest);
   goto restart;
 }
 
@@ -3098,7 +3120,7 @@ bsi_commit_edge_inserts (void)
   edge e;
   edge_iterator ei;
 
-  bsi_commit_one_edge_insert (EDGE_SUCC (ENTRY_BLOCK_PTR, 0), NULL);
+  bsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR), NULL);
 
   FOR_EACH_BB (bb)
     FOR_EACH_EDGE (e, ei, bb->succs)
@@ -3275,6 +3297,15 @@ verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
        }
       break;
 
+    case ASSERT_EXPR:
+      x = fold (ASSERT_EXPR_COND (t));
+      if (x == boolean_false_node)
+       {
+         error ("ASSERT_EXPR with an always-false condition");
+         return *tp;
+       }
+      break;
+
     case MODIFY_EXPR:
       x = TREE_OPERAND (t, 0);
       if (TREE_CODE (x) == BIT_FIELD_REF
@@ -3555,6 +3586,12 @@ verify_stmts (void)
        {
          int phi_num_args = PHI_NUM_ARGS (phi);
 
+         if (bb_for_stmt (phi) != bb)
+           {
+             error ("bb_for_stmt (phi) is set to a wrong basic block\n");
+             err |= true;
+           }
+
          for (i = 0; i < phi_num_args; i++)
            {
              tree t = PHI_ARG_DEF (phi, i);
@@ -3593,6 +3630,13 @@ verify_stmts (void)
       for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
        {
          tree stmt = bsi_stmt (bsi);
+
+         if (bb_for_stmt (stmt) != bb)
+           {
+             error ("bb_for_stmt (stmt) is set to a wrong basic block\n");
+             err |= true;
+           }
+
          bsi_next (&bsi);
          err |= verify_stmt (stmt, bsi_end_p (bsi));
          addr = walk_tree (&stmt, verify_node_sharing, htab, NULL);
@@ -3706,7 +3750,7 @@ tree_verify_flow_info (void)
          if (TREE_CODE (stmt) == LABEL_EXPR)
            {
              error ("Label %s in the middle of basic block %d\n",
-                    IDENTIFIER_POINTER (DECL_NAME (stmt)),
+                    IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
                     bb->index);
              err = 1;
            }
@@ -3796,14 +3840,15 @@ tree_verify_flow_info (void)
          break;
 
        case RETURN_EXPR:
-         if (EDGE_COUNT (bb->succs) != 1
-             || (EDGE_SUCC (bb, 0)->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
-                                    | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
+         if (!single_succ_p (bb)
+             || (single_succ_edge (bb)->flags
+                 & (EDGE_FALLTHRU | EDGE_ABNORMAL
+                    | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
            {
              error ("Wrong outgoing edge flags at end of bb %d\n", bb->index);
              err = 1;
            }
-         if (EDGE_SUCC (bb, 0)->dest != EXIT_BLOCK_PTR)
+         if (single_succ (bb) != EXIT_BLOCK_PTR)
            {
              error ("Return edge does not point to exit in bb %d\n",
                     bb->index);
@@ -3920,7 +3965,7 @@ tree_make_forwarder_block (edge fallthru)
   dummy = fallthru->src;
   bb = fallthru->dest;
 
-  if (EDGE_COUNT (bb->preds) == 1)
+  if (single_pred_p (bb))
     return;
 
   /* If we redirected a branch we must create new phi nodes at the
@@ -3961,16 +4006,16 @@ tree_forwarder_block_p (basic_block bb, bool phi_wanted)
   block_stmt_iterator bsi;
 
   /* BB must have a single outgoing edge.  */
-  if (EDGE_COUNT (bb->succs) != 1
+  if (single_succ_p (bb) != 1
       /* If PHI_WANTED is false, BB must not have any PHI nodes.
         Otherwise, BB must have PHI nodes.  */
       || (phi_nodes (bb) != NULL_TREE) != phi_wanted
       /* BB may not be a predecessor of EXIT_BLOCK_PTR.  */
-      || EDGE_SUCC (bb, 0)->dest == EXIT_BLOCK_PTR
+      || single_succ (bb) == EXIT_BLOCK_PTR
       /* Nor should this be an infinite loop.  */
-      || EDGE_SUCC (bb, 0)->dest == bb
+      || single_succ (bb) == bb
       /* BB may not have an abnormal outgoing edge.  */
-      || (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL))
+      || (single_succ_edge (bb)->flags & EDGE_ABNORMAL))
     return false; 
 
 #if ENABLE_CHECKING
@@ -3998,6 +4043,18 @@ tree_forwarder_block_p (basic_block bb, bool phi_wanted)
   if (find_edge (ENTRY_BLOCK_PTR, bb))
     return false;
 
+  if (current_loops)
+    { 
+      basic_block dest;
+      /* Protect loop latches, headers and preheaders.  */
+      if (bb->loop_father->header == bb)
+       return false;
+      dest = EDGE_SUCC (bb, 0)->dest;
+      if (dest->loop_father->header == dest)
+       return false;
+    }
+
   return true;
 }
 
@@ -4023,7 +4080,7 @@ has_abnormal_incoming_edge_p (basic_block bb)
 static bool
 remove_forwarder_block (basic_block bb, basic_block **worklist)
 {
-  edge succ = EDGE_SUCC (bb, 0), e, s;
+  edge succ = single_succ_edge (bb), e, s;
   basic_block dest = succ->dest;
   tree label;
   tree phi;
@@ -4182,7 +4239,7 @@ cleanup_forwarder_blocks (void)
 static void
 remove_forwarder_block_with_phi (basic_block bb)
 {
-  edge succ = EDGE_SUCC (bb, 0);
+  edge succ = single_succ_edge (bb);
   basic_block dest = succ->dest;
   tree label;
   basic_block dombb, domdest, dom;
@@ -4223,7 +4280,7 @@ remove_forwarder_block_with_phi (basic_block bb)
          /* PHI arguments are different.  Create a forwarder block by
             splitting E so that we can merge PHI arguments on E to
             DEST.  */
-         e = EDGE_SUCC (split_edge (e), 0);
+         e = single_succ_edge (split_edge (e));
        }
 
       s = redirect_edge_and_branch (e, dest);
@@ -4325,7 +4382,7 @@ merge_phi_nodes (void)
       if (!tree_forwarder_block_p (bb, true))
        continue;
 
-      dest = EDGE_SUCC (bb, 0)->dest;
+      dest = single_succ (bb);
 
       /* We have to feed into another basic block with PHI
         nodes.  */
@@ -4673,9 +4730,6 @@ tree_duplicate_bb (basic_block bb)
       if (TREE_CODE (stmt) == LABEL_EXPR)
        continue;
 
-      /* Record the definitions.  */
-      get_stmt_operands (stmt);
-
       FOR_EACH_SSA_TREE_OPERAND (val, stmt, op_iter, SSA_OP_ALL_DEFS)
        mark_for_rewrite (val);
 
@@ -4889,7 +4943,6 @@ rewrite_to_new_ssa_names_bb (basic_block bb, htab_t map)
   for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
     {
       stmt = bsi_stmt (bsi);
-      get_stmt_operands (stmt);
       ann = stmt_ann (stmt);
 
       uses = USE_OPS (ann);
@@ -5114,7 +5167,7 @@ dump_function_to_file (tree fn, FILE *file, int flags)
 
   /* When GIMPLE is lowered, the variables are no longer available in
      BIND_EXPRs, so display them separately.  */
-  if (cfun && cfun->unexpanded_var_list)
+  if (cfun && cfun->decl == fn && cfun->unexpanded_var_list)
     {
       ignore_topmost_bind = true;
 
@@ -5130,7 +5183,7 @@ dump_function_to_file (tree fn, FILE *file, int flags)
        }
     }
 
-  if (basic_block_info)
+  if (cfun && cfun->decl == fn && cfun->cfg && basic_block_info)
     {
       /* Make a CFG based dump.  */
       check_bb_profile (ENTRY_BLOCK_PTR, file);
@@ -5311,7 +5364,7 @@ need_fake_edge_p (tree t)
   tree call;
 
   /* NORETURN and LONGJMP calls already have an edge to exit.
-     CONST, PURE and ALWAYS_RETURN calls do not need one.
+     CONST and PURE calls do not need one.
      We don't currently check for CONST and PURE here, although
      it would be a good idea, because those attributes are
      figured out from the RTL in mark_constant_function, and
@@ -5319,7 +5372,7 @@ need_fake_edge_p (tree t)
      leads to different results from -fbranch-probabilities.  */
   call = get_call_expr_in (t);
   if (call
-      && !(call_expr_flags (call) & (ECF_NORETURN | ECF_ALWAYS_RETURN)))
+      && !(call_expr_flags (call) & ECF_NORETURN))
     return true;
 
   if (TREE_CODE (t) == ASM_EXPR
@@ -5529,6 +5582,71 @@ tree_execute_on_shrinking_pred (edge e)
     remove_phi_args (e);
 }
 
+/*---------------------------------------------------------------------------
+  Helper functions for Loop versioning
+  ---------------------------------------------------------------------------*/
+
+/* Adjust phi nodes for 'first' basic block.  'second' basic block is a copy
+   of 'first'. Both of them are dominated by 'new_head' basic block. When
+   'new_head' was created by 'second's incoming edge it received phi arguments
+   on the edge by split_edge(). Later, additional edge 'e' was created to
+   connect 'new_head' and 'first'. Now this routine adds phi args on this 
+   additional edge 'e' that new_head to second edge received as part of edge 
+   splitting.
+*/
+
+static void
+tree_lv_adjust_loop_header_phi (basic_block first, basic_block second,
+                               basic_block new_head, edge e)
+{
+  tree phi1, phi2;
+
+  /* Browse all 'second' basic block phi nodes and add phi args to
+     edge 'e' for 'first' head. PHI args are always in correct order.  */
+
+  for (phi2 = phi_nodes (second), phi1 = phi_nodes (first); 
+       phi2 && phi1; 
+       phi2 = PHI_CHAIN (phi2),  phi1 = PHI_CHAIN (phi1))
+    {
+      edge e2 = find_edge (new_head, second);
+
+      if (e2)
+       {
+         tree def = PHI_ARG_DEF (phi2, e2->dest_idx);
+         add_phi_arg (phi1, def, e);
+       }
+    }
+}
+
+/* Adds a if else statement to COND_BB with condition COND_EXPR.  
+   SECOND_HEAD is the destination of the THEN and FIRST_HEAD is 
+   the destination of the ELSE part.  */
+static void
+tree_lv_add_condition_to_bb (basic_block first_head, basic_block second_head,
+                            basic_block cond_bb, void *cond_e)
+{
+  block_stmt_iterator bsi;
+  tree goto1 = NULL_TREE;
+  tree goto2 = NULL_TREE;
+  tree new_cond_expr = NULL_TREE;
+  tree cond_expr = (tree) cond_e;
+  edge e0;
+
+  /* Build new conditional expr */
+  goto1 = build1 (GOTO_EXPR, void_type_node, tree_block_label (first_head));
+  goto2 = build1 (GOTO_EXPR, void_type_node, tree_block_label (second_head));
+  new_cond_expr = build3 (COND_EXPR, void_type_node, cond_expr, goto1, goto2);
+
+  /* Add new cond in cond_bb.  */ 
+  bsi = bsi_start (cond_bb); 
+  bsi_insert_after (&bsi, new_cond_expr, BSI_NEW_STMT);
+  /* Adjust edges appropriately to connect new head with first head
+     as well as second head.  */
+  e0 = single_succ_edge (cond_bb);
+  e0->flags &= ~EDGE_FALLTHRU;
+  e0->flags |= EDGE_FALSE_VALUE;
+}
+
 struct cfg_hooks tree_cfg_hooks = {
   "tree",
   tree_verify_flow_info,
@@ -5553,6 +5671,11 @@ struct cfg_hooks tree_cfg_hooks = {
   tree_flow_call_edges_add,     /* flow_call_edges_add */
   tree_execute_on_growing_pred,        /* execute_on_growing_pred */
   tree_execute_on_shrinking_pred, /* execute_on_shrinking_pred */
+  tree_duplicate_loop_to_header_edge, /* duplicate loop for trees */
+  tree_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
+  tree_lv_adjust_loop_header_phi, /* lv_adjust_loop_header_phi*/
+  extract_true_false_edges_from_block, /* extract_cond_bb_edges */
+  flush_pending_stmts          /* flush_pending_stmts */  
 };
 
 
@@ -5795,5 +5918,3 @@ struct tree_opt_pass pass_warn_function_return =
   0,                                   /* todo_flags_finish */
   0                                    /* letter */
 };
-
-#include "gt-tree-cfg.h"