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 3fd8232..28af511 100644 (file)
@@ -1,5 +1,5 @@
 /* Control flow functions for trees.
-   Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
+   Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
    Contributed by Diego Novillo <dnovillo@redhat.com>
 
 This file is part of GCC.
@@ -44,6 +44,7 @@ Boston, MA 02111-1307, USA.  */
 #include "except.h"
 #include "cfgloop.h"
 #include "cfglayout.h"
+#include "hashtab.h"
 
 /* This file contains functions for building the Control Flow Graph (CFG)
    for a function tree.  */
@@ -53,9 +54,32 @@ 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
+   hash table to prevent problems with duplication of SWITCH_EXPRs.
+
+   Access to this list of CASE_LABEL_EXPRs allows us to efficiently
+   update the case vector in response to edge redirections.
+
+   Right now this table is set up and torn down at key points in the
+   compilation process.  It would be nice if we could make the table
+   more persistent.  The key is getting notification of changes to
+   the CFG (particularly edge removal, creation and redirection).  */
+
+struct edge_to_cases_elt
+{
+  /* The edge itself.  Necessary for hashing and equality tests.  */
+  edge e;
+
+  /* The case labels associated with this edge.  We link these up via
+     their TREE_CHAIN field, then we wipe out the TREE_CHAIN fields
+     when we destroy the hash table.  This prevents problems when copying
+     SWITCH_EXPRs.  */
+  tree case_labels;
+};
+
+static htab_t edge_to_cases;
 
 /* CFG statistics.  */
 struct cfg_stats_d
@@ -86,14 +110,13 @@ static void make_goto_expr_edges (basic_block);
 static edge tree_redirect_edge_and_branch (edge, basic_block);
 static edge tree_try_redirect_by_replacing_jump (edge, basic_block);
 static void split_critical_edges (void);
+static bool remove_fallthru_edge (VEC(edge) *);
 
 /* Various helpers.  */
 static inline bool stmt_starts_bb_p (tree, tree);
 static int tree_verify_flow_info (void);
 static void tree_make_forwarder_block (edge);
-static bool thread_jumps (void);
-static bool tree_forwarder_block_p (basic_block);
-static void bsi_commit_edge_inserts_1 (edge e);
+static bool tree_forwarder_block_p (basic_block, bool);
 static void tree_cfg2vcg (FILE *);
 
 /* Flowgraph optimization and cleanup.  */
@@ -102,10 +125,12 @@ 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);
 static bool phi_alternatives_equal (basic_block, edge, edge);
+static bool cleanup_forwarder_blocks (void);
 
 
 /*---------------------------------------------------------------------------
@@ -121,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;
@@ -376,9 +398,10 @@ create_bb (void *h, void *e, basic_block after)
 
   gcc_assert (!e);
 
-  /* Create and initialize a new basic block.  */
+  /* Create and initialize a new basic block.  Since alloc_block uses
+     ggc_alloc_cleared to allocate a basic block, we do not have to
+     clear the newly allocated basic block here.  */
   bb = alloc_block ();
-  memset (bb, 0, sizeof (*bb));
 
   bb->index = last_basic_block;
   bb->flags = BB_NEW;
@@ -411,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
@@ -422,7 +468,7 @@ make_edges (void)
      statements in it.  */
   make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (0), EDGE_FALLTHRU);
 
-  /* Traverse basic block array placing edges.  */
+  /* Traverse the basic block array placing edges.  */
   FOR_EACH_BB (bb)
     {
       tree first = first_stmt (bb);
@@ -449,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 ();
 }
@@ -524,7 +573,7 @@ make_exit_edges (basic_block bb)
         such a bloody pain to avoid creating edges for this case since
         all we do is remove these edges when we're done building the
         CFG.  */
-      if (call_expr_flags (last) & (ECF_NORETURN | ECF_LONGJMP))
+      if (call_expr_flags (last) & ECF_NORETURN)
        {
          make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
          return;
@@ -576,6 +625,159 @@ make_cond_expr_edges (basic_block bb)
   make_edge (bb, else_bb, EDGE_FALSE_VALUE);
 }
 
+/* Hashing routine for EDGE_TO_CASES.  */
+
+static hashval_t
+edge_to_cases_hash (const void *p)
+{
+  edge e = ((struct edge_to_cases_elt *)p)->e;
+
+  /* Hash on the edge itself (which is a pointer).  */
+  return htab_hash_pointer (e);
+}
+
+/* Equality routine for EDGE_TO_CASES, edges are unique, so testing
+   for equality is just a pointer comparison.  */
+
+static int
+edge_to_cases_eq (const void *p1, const void *p2)
+{
+  edge e1 = ((struct edge_to_cases_elt *)p1)->e;
+  edge e2 = ((struct edge_to_cases_elt *)p2)->e;
+
+  return e1 == e2;
+}
+
+/* Called for each element in the hash table (P) as we delete the
+   edge to cases hash table.
+
+   Clear all the TREE_CHAINs to prevent problems with copying of 
+   SWITCH_EXPRs and structure sharing rules, then free the hash table
+   element.  */
+
+static void
+edge_to_cases_cleanup (void *p)
+{
+  struct edge_to_cases_elt *elt = p;
+  tree t, next;
+
+  for (t = elt->case_labels; t; t = next)
+    {
+      next = TREE_CHAIN (t);
+      TREE_CHAIN (t) = NULL;
+    }
+  free (p);
+}
+
+/* Start recording information mapping edges to case labels.  */
+
+static void
+start_recording_case_labels (void)
+{
+  gcc_assert (edge_to_cases == NULL);
+
+  edge_to_cases = htab_create (37,
+                              edge_to_cases_hash,
+                              edge_to_cases_eq,
+                              edge_to_cases_cleanup);
+}
+
+/* Return nonzero if we are recording information for case labels.  */
+
+static bool
+recording_case_labels_p (void)
+{
+  return (edge_to_cases != NULL);
+}
+
+/* Stop recording information mapping edges to case labels and
+   remove any information we have recorded.  */
+static void
+end_recording_case_labels (void)
+{
+  htab_delete (edge_to_cases);
+  edge_to_cases = NULL;
+}
+
+/* Record that CASE_LABEL (a CASE_LABEL_EXPR) references edge E.  */
+
+static void
+record_switch_edge (edge e, tree case_label)
+{
+  struct edge_to_cases_elt *elt;
+  void **slot;
+
+  /* Build a hash table element so we can see if E is already
+     in the table.  */
+  elt = xmalloc (sizeof (struct edge_to_cases_elt));
+  elt->e = e;
+  elt->case_labels = case_label;
+
+  slot = htab_find_slot (edge_to_cases, elt, INSERT);
+
+  if (*slot == NULL)
+    {
+      /* E was not in the hash table.  Install E into the hash table.  */
+      *slot = (void *)elt;
+    }
+  else
+    {
+      /* E was already in the hash table.  Free ELT as we do not need it
+        anymore.  */
+      free (elt);
+
+      /* Get the entry stored in the hash table.  */
+      elt = (struct edge_to_cases_elt *) *slot;
+
+      /* Add it to the chain of CASE_LABEL_EXPRs referencing E.  */
+      TREE_CHAIN (case_label) = elt->case_labels;
+      elt->case_labels = case_label;
+    }
+}
+
+/* If we are inside a {start,end}_recording_cases block, then return
+   a chain of CASE_LABEL_EXPRs from T which reference E.
+
+   Otherwise return NULL.  */
+
+static tree
+get_cases_for_edge (edge e, tree t)
+{
+  struct edge_to_cases_elt elt, *elt_p;
+  void **slot;
+  size_t i, n;
+  tree vec;
+
+  /* If we are not recording cases, then we do not have CASE_LABEL_EXPR
+     chains available.  Return NULL so the caller can detect this case.  */
+  if (!recording_case_labels_p ())
+    return NULL;
+  
+restart:
+  elt.e = e;
+  elt.case_labels = NULL;
+  slot = htab_find_slot (edge_to_cases, &elt, NO_INSERT);
+
+  if (slot)
+    {
+      elt_p = (struct edge_to_cases_elt *)*slot;
+      return elt_p->case_labels;
+    }
+
+  /* If we did not find E in the hash table, then this must be the first
+     time we have been queried for information about E & T.  Add all the
+     elements from T to the hash table then perform the query again.  */
+
+  vec = SWITCH_LABELS (t);
+  n = TREE_VEC_LENGTH (vec);
+  for (i = 0; i < n; i++)
+    {
+      tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
+      basic_block label_bb = label_to_block (lab);
+      record_switch_edge (find_edge (e->src, label_bb), TREE_VEC_ELT (vec, i));
+    }
+  goto restart;
+}
 
 /* Create the edges for a SWITCH_EXPR starting at block BB.
    At this point, the switch body has been lowered and the
@@ -603,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);
 
@@ -619,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);
@@ -639,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.  */
@@ -719,23 +917,26 @@ cleanup_tree_cfg (void)
   retval = cleanup_control_flow ();
   retval |= delete_unreachable_blocks ();
 
-  /* thread_jumps sometimes leaves further transformation
-     opportunities for itself, so iterate on it until nothing
-     changes.  */
-  while (thread_jumps ())
-    retval = true;
+  /* cleanup_forwarder_blocks can redirect edges out of SWITCH_EXPRs,
+     which can get expensive.  So we want to enable recording of edge
+     to CASE_LABEL_EXPR mappings around the call to
+     cleanup_forwarder_blocks.  */
+  start_recording_case_labels ();
+  retval |= cleanup_forwarder_blocks ();
+  end_recording_case_labels ();
 
 #ifdef ENABLE_CHECKING
   if (retval)
     {
       gcc_assert (!cleanup_control_flow ());
       gcc_assert (!delete_unreachable_blocks ());
+      gcc_assert (!cleanup_forwarder_blocks ());
     }
 #endif
 
   /* Merging the blocks creates no new opportunities for the other
      optimizations, so do it here.  */
-  merge_seq_blocks ();
+  retval |= merge_seq_blocks ();
 
   compact_blocks ();
 
@@ -747,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
@@ -869,9 +1094,11 @@ cleanup_dead_labels (void)
   
            /* Replace all destination labels.  */
            for (i = 0; i < n; ++i)
-             CASE_LABEL (TREE_VEC_ELT (vec, i))
-               = main_block_label (CASE_LABEL (TREE_VEC_ELT (vec, i)));
-  
+             {
+               tree elt = TREE_VEC_ELT (vec, i);
+               tree label = main_block_label (CASE_LABEL (elt));
+               CASE_LABEL (elt) = label;
+             }
            break;
          }
 
@@ -952,9 +1179,9 @@ group_case_labels (void)
             Ignore the last element of the label vector because it
             must be the default case.  */
           i = 0;
-         while (i < old_size - 2)
+         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);
@@ -970,16 +1197,15 @@ 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++;
              /* Try to merge case labels.  Break out when we reach the end
                 of the label vector or when we cannot merge the next case
                 label with the current one.  */
-             while (i < old_size - 2)
+             while (i < old_size - 1)
                {
-                 tree merge_case = TREE_VEC_ELT (labels, ++i);
+                 tree merge_case = TREE_VEC_ELT (labels, i);
                  tree merge_label = CASE_LABEL (merge_case);
                  tree t = int_const_binop (PLUS_EXPR, base_high,
                                            integer_one_node, 1);
@@ -994,6 +1220,7 @@ group_case_labels (void)
                      CASE_HIGH (base_case) = base_high;
                      TREE_VEC_ELT (labels, i) = NULL_TREE;
                      new_size--;
+                     i++;
                    }
                  else
                    break;
@@ -1021,21 +1248,21 @@ 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 (!single_pred_p (b))
     return false;
 
   if (b == EXIT_BLOCK_PTR)
     return false;
   
-  if (EDGE_COUNT (b->preds) > 1)
-    return false;
-
   /* If A ends by a statement causing exceptions or something similar, we
      cannot merge the blocks.  */
   stmt = last_stmt (a);
@@ -1047,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;
 
@@ -1062,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;
 }
 
@@ -1080,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);
@@ -1136,8 +1382,11 @@ remove_useless_stmts_warn_notreached (tree stmt)
   if (EXPR_HAS_LOCATION (stmt))
     {
       location_t loc = EXPR_LOCATION (stmt);
-      warning ("%Hwill never be executed", &loc);
-      return true;
+      if (LOCATION_LINE (loc) > 0)
+       {
+         warning ("%Hwill never be executed", &loc);
+         return true;
+       }
     }
 
   switch (TREE_CODE (stmt))
@@ -1204,10 +1453,9 @@ remove_useless_stmts_cond (tree *stmt_p, struct rus_data *data)
   else_has_label = data->has_label;
   data->has_label = save_has_label | then_has_label | else_has_label;
 
-  fold_stmt (stmt_p);
   then_clause = COND_EXPR_THEN (*stmt_p);
   else_clause = COND_EXPR_ELSE (*stmt_p);
-  cond = COND_EXPR_COND (*stmt_p);
+  cond = fold (COND_EXPR_COND (*stmt_p));
 
   /* If neither arm does anything at all, we can remove the whole IF.  */
   if (!TREE_SIDE_EFFECTS (then_clause) && !TREE_SIDE_EFFECTS (else_clause))
@@ -1604,7 +1852,7 @@ remove_useless_stmts_1 (tree *tp, struct rus_data *data)
          }
       }
       break;
-    case SWITCH_EXPR:
+    case ASM_EXPR:
       fold_stmt (tp);
       data->last_goto = NULL;
       break;
@@ -1648,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
@@ -1781,13 +1909,13 @@ 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;
     }
 
   /* Remove edges to BB's successors.  */
   while (EDGE_COUNT (bb->succs) > 0)
-    ssa_remove_edge (EDGE_SUCC (bb, 0));
+    remove_edge (EDGE_SUCC (bb, 0));
 }
 
 
@@ -1797,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)
     {
@@ -1809,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);)
     {
@@ -1817,10 +1963,10 @@ remove_bb (basic_block bb)
           && FORCED_LABEL (LABEL_EXPR_LABEL (stmt)))
        {
          basic_block new_bb = bb->prev_bb;
-         block_stmt_iterator new_bsi = bsi_after_labels (new_bb);
+         block_stmt_iterator new_bsi = bsi_start (new_bb);
                  
          bsi_remove (&i);
-         bsi_insert_after (&new_bsi, stmt, BSI_NEW_STMT);
+         bsi_insert_before (&new_bsi, stmt, BSI_NEW_STMT);
        }
       else
         {
@@ -1835,27 +1981,42 @@ remove_bb (basic_block bb)
         since this way we lose warnings for gotos in the original
         program that are indeed unreachable.  */
       if (TREE_CODE (stmt) != GOTO_EXPR && EXPR_HAS_LOCATION (stmt) && !loc)
+       {
 #ifdef USE_MAPPED_LOCATION
-       loc = EXPR_LOCATION (stmt);
+         if (EXPR_HAS_LOCATION (stmt))
+           loc = EXPR_LOCATION (stmt);
 #else
-       loc = EXPR_LOCUS (stmt);
+         source_locus t;
+         t = EXPR_LOCUS (stmt);
+         if (t && LOCATION_LINE (*t) > 0)
+           loc = t;
 #endif
+       }
     }
 
   /* If requested, give a warning that the first statement in the
      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
 
   remove_phi_nodes_and_edges_for_unreachable_block (bb);
 }
 
+/* A list of all the noreturn calls passed to modify_stmt.
+   cleanup_control_flow uses it to detect cases where a mid-block
+   indirect call has been turned into a noreturn call.  When this
+   happens, all the instructions after the call are no longer
+   reachable and must be deleted as dead.  */
+
+VEC(tree) *modified_noreturn_calls;
+
 /* Try to remove superfluous control structures.  */
 
 static bool
@@ -1866,6 +2027,15 @@ cleanup_control_flow (void)
   bool retval = false;
   tree stmt;
 
+  /* Detect cases where a mid-block call is now known not to return.  */
+  while (VEC_length (tree, modified_noreturn_calls))
+    {
+      stmt = VEC_pop (tree, modified_noreturn_calls);
+      bb = bb_for_stmt (stmt);
+      if (bb != NULL && last_stmt (bb) != stmt && noreturn_call_p (stmt))
+       split_block (bb, stmt);
+    }
+
   FOR_EACH_BB (bb)
     {
       bsi = bsi_last (bb);
@@ -1877,6 +2047,60 @@ cleanup_control_flow (void)
       if (TREE_CODE (stmt) == COND_EXPR
          || 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))
+       {
+         free_dominance_info (CDI_DOMINATORS);
+         retval = true;
+       }
     }
   return retval;
 }
@@ -1892,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;
@@ -1924,7 +2148,7 @@ cleanup_control_expr_graph (basic_block bb, block_stmt_iterator bsi)
            {
              taken_edge->probability += e->probability;
              taken_edge->count += e->count;
-             ssa_remove_edge (e);
+             remove_edge (e);
              retval = true;
            }
          else
@@ -1934,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;
@@ -1945,10 +2169,26 @@ cleanup_control_expr_graph (basic_block bb, block_stmt_iterator bsi)
   return retval;
 }
 
+/* Remove any fallthru edge from EV.  Return true if an edge was removed.  */
+
+static bool
+remove_fallthru_edge (VEC(edge) *ev)
+{
+  edge_iterator ei;
+  edge e;
+
+  FOR_EACH_EDGE (e, ei, ev)
+    if ((e->flags & EDGE_FALLTHRU) != 0)
+      {
+       remove_edge (e);
+       return true;
+      }
+  return false;
+}
 
-/* Given a control block BB and a predicate VAL, return the edge that
-   will be taken out of the block.  If VAL does not match a unique
-   edge, NULL is returned.  */
+/* Given a basic block BB ending with COND_EXPR or SWITCH_EXPR, and a
+   predicate VAL, return the edge that will be taken out of the block.
+   If VAL does not match a unique edge, NULL is returned.  */
 
 edge
 find_taken_edge (basic_block bb, tree val)
@@ -1959,15 +2199,9 @@ find_taken_edge (basic_block bb, tree val)
 
   gcc_assert (stmt);
   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 (val && COMPARISON_CLASS_P (val))
-    val = fold (val);
-
-  /* If VAL is not a constant, we can't determine which edge might
-     be taken.  */
-  if (val == NULL || !really_constant_p (val))
+  if (! is_gimple_min_invariant (val))
     return NULL;
 
   if (TREE_CODE (stmt) == COND_EXPR)
@@ -1976,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);
 
-  return EDGE_SUCC (bb, 0);
+  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
@@ -1990,26 +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);
-
-  /* If both edges of the branch lead to the same basic block, it doesn't
-     matter which edge is taken.  */
-  if (true_edge->dest == false_edge->dest)
-    return true_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.  */
 
@@ -2020,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));
@@ -2083,21 +2322,19 @@ find_case_label_for_value (tree switch_expr, tree val)
 static bool
 phi_alternatives_equal (basic_block dest, edge e1, edge e2)
 {
-  tree phi, val1, val2;
-  int n1, n2;
+  int n1 = e1->dest_idx;
+  int n2 = e2->dest_idx;
+  tree phi;
 
   for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
     {
-      n1 = phi_arg_from_edge (phi, e1);
-      n2 = phi_arg_from_edge (phi, e2);
+      tree val1 = PHI_ARG_DEF (phi, n1);
+      tree val2 = PHI_ARG_DEF (phi, n2);
 
-      gcc_assert (n1 >= 0);
-      gcc_assert (n2 >= 0);
+      gcc_assert (val1 != NULL_TREE);
+      gcc_assert (val2 != NULL_TREE);
 
-      val1 = PHI_ARG_DEF (phi, n1);
-      val2 = PHI_ARG_DEF (phi, n2);
-
-      if (!operand_equal_p (val1, val2, 0))
+      if (!operand_equal_for_phi_arg_p (val1, val2))
        return false;
     }
 
@@ -2185,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";
@@ -2206,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;
@@ -2365,7 +2602,7 @@ is_ctrl_altering_stmt (tree t)
        return true;
 
       /* A CALL_EXPR also alters control flow if it does not return.  */
-      if (call_expr_flags (call) & (ECF_NORETURN | ECF_LONGJMP))
+      if (call_expr_flags (call) & ECF_NORETURN)
        return true;
     }
 
@@ -2403,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;
 
@@ -2412,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;
@@ -2468,11 +2701,9 @@ disband_implicit_edges (void)
             from cfg_remove_useless_stmts here since it violates the
             invariants for tree--cfg correspondence and thus fits better
             here where we do it anyway.  */
-         FOR_EACH_EDGE (e, ei, bb->succs)
+         e = find_edge (bb, bb->next_bb);
+         if (e)
            {
-             if (e->dest != bb->next_bb)
-               continue;
-
              if (e->flags & EDGE_TRUE_VALUE)
                COND_EXPR_THEN (stmt) = build_empty_stmt ();
              else if (e->flags & EDGE_FALSE_VALUE)
@@ -2489,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;
        }
@@ -2538,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;
 }
@@ -2662,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).  */
@@ -2670,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);
 }
 
 
@@ -2683,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);
 }
 
 
@@ -2696,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);
 }
 
 
@@ -2760,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);
 }
 
 
@@ -2789,9 +3040,9 @@ tree_find_edge_insert_loc (edge e, block_stmt_iterator *bsi,
 
      The requirement for no PHI nodes could be relaxed.  Basically we
      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
+     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)
     {
@@ -2823,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);
@@ -2854,43 +3105,37 @@ 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;
 }
 
 
 /* This routine will commit all pending edge insertions, creating any new
-   basic blocks which are necessary.
-
-   If specified, NEW_BLOCKS returns a count of the number of new basic
-   blocks which were created.  */
+   basic blocks which are necessary.  */
 
 void
-bsi_commit_edge_inserts (int *new_blocks)
+bsi_commit_edge_inserts (void)
 {
   basic_block bb;
   edge e;
-  int blocks;
   edge_iterator ei;
 
-  blocks = n_basic_blocks;
-
-  bsi_commit_edge_inserts_1 (EDGE_SUCC (ENTRY_BLOCK_PTR, 0));
+  bsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR), NULL);
 
   FOR_EACH_BB (bb)
     FOR_EACH_EDGE (e, ei, bb->succs)
-      bsi_commit_edge_inserts_1 (e);
-
-  if (new_blocks)
-    *new_blocks = n_basic_blocks - blocks;
+      bsi_commit_one_edge_insert (e, NULL);
 }
 
 
-/* Commit insertions pending at edge E.  */
+/* Commit insertions pending at edge E. If a new block is created, set NEW_BB
+   to this block, otherwise set it to NULL.  */
 
-static void
-bsi_commit_edge_inserts_1 (edge e)
+void
+bsi_commit_one_edge_insert (edge e, basic_block *new_bb)
 {
+  if (new_bb)
+    *new_bb = NULL;
   if (PENDING_STMT (e))
     {
       block_stmt_iterator bsi;
@@ -2898,7 +3143,7 @@ bsi_commit_edge_inserts_1 (edge e)
 
       PENDING_STMT (e) = NULL_TREE;
 
-      if (tree_find_edge_insert_loc (e, &bsi, NULL))
+      if (tree_find_edge_insert_loc (e, &bsi, new_bb))
        bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
       else
        bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
@@ -2915,8 +3160,8 @@ bsi_insert_on_edge (edge e, tree stmt)
   append_to_statement_list (stmt, &PENDING_STMT (e));
 }
 
-/* Similar to bsi_insert_on_edge+bsi_commit_edge_inserts.  If new block has to
-   be created, it is returned.  */
+/* Similar to bsi_insert_on_edge+bsi_commit_edge_inserts.  If a new
+   block has to be created, it is returned.  */
 
 basic_block
 bsi_insert_on_edge_immediate (edge e, tree stmt)
@@ -2938,6 +3183,31 @@ bsi_insert_on_edge_immediate (edge e, tree stmt)
             Tree specific functions for CFG manipulation
 ---------------------------------------------------------------------------*/
 
+/* Reinstall those PHI arguments queued in OLD_EDGE to NEW_EDGE.  */
+
+static void
+reinstall_phi_args (edge new_edge, edge old_edge)
+{
+  tree var, phi;
+
+  if (!PENDING_STMT (old_edge))
+    return;
+  
+  for (var = PENDING_STMT (old_edge), phi = phi_nodes (new_edge->dest);
+       var && phi;
+       var = TREE_CHAIN (var), phi = PHI_CHAIN (phi))
+    {
+      tree result = TREE_PURPOSE (var);
+      tree arg = TREE_VALUE (var);
+
+      gcc_assert (result == PHI_RESULT (phi));
+
+      add_phi_arg (phi, arg, new_edge);
+    }
+
+  PENDING_STMT (old_edge) = NULL;
+}
+
 /* Split a (typically critical) edge EDGE_IN.  Return the new block.
    Abort on abnormal edges.  */
 
@@ -2946,9 +3216,6 @@ tree_split_edge (edge edge_in)
 {
   basic_block new_bb, after_bb, dest, src;
   edge new_edge, e;
-  tree phi;
-  int i, num_elem;
-  edge_iterator ei;
 
   /* Abnormal edges cannot be split.  */
   gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
@@ -2959,13 +3226,10 @@ tree_split_edge (edge edge_in)
   /* Place the new block in the block list.  Try to keep the new block
      near its "logical" location.  This is of most help to humans looking
      at debugging dumps.  */
-  FOR_EACH_EDGE (e, ei, dest->preds)
-    if (e->src->next_bb == dest)
-      break;
-  if (!e)
-    after_bb = dest->prev_bb;
-  else
+  if (dest->prev_bb && find_edge (dest->prev_bb, dest))
     after_bb = edge_in->src;
+  else
+    after_bb = dest->prev_bb;
 
   new_bb = create_empty_bb (after_bb);
   new_bb->frequency = EDGE_FREQUENCY (edge_in);
@@ -2974,23 +3238,9 @@ tree_split_edge (edge edge_in)
   new_edge->probability = REG_BR_PROB_BASE;
   new_edge->count = edge_in->count;
 
-  /* Find all the PHI arguments on the original edge, and change them to
-     the new edge.  Do it before redirection, so that the argument does not
-     get removed.  */
-  for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
-    {
-      num_elem = PHI_NUM_ARGS (phi);
-      for (i = 0; i < num_elem; i++)
-       if (PHI_ARG_EDGE (phi, i) == edge_in)
-         {
-           PHI_ARG_EDGE (phi, i) = new_edge;
-           break;
-         }
-    }
-
   e = redirect_edge_and_branch (edge_in, new_bb);
   gcc_assert (e);
-  gcc_assert (!PENDING_STMT (edge_in));
+  reinstall_phi_args (new_edge, e);
 
   return new_bb;
 }
@@ -3017,12 +3267,14 @@ has_label_p (basic_block bb, tree label)
 
 
 /* Callback for walk_tree, check that all elements with address taken are
-   properly noticed as such.  */
+   properly noticed as such.  The DATA is an int* that is 1 if TP was seen
+   inside a PHI node.  */
 
 static tree
 verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
 {
   tree t = *tp, x;
+  bool in_phi = (data != NULL);
 
   if (TYPE_P (t))
     *walk_subtrees = 0;
@@ -3040,7 +3292,16 @@ verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
     case SSA_NAME:
       if (SSA_NAME_IN_FREE_LIST (t))
        {
-         error ("SSA name in freelist but still referenced");
+         error ("SSA name in freelist but still referenced");
+         return *tp;
+       }
+      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;
@@ -3056,13 +3317,21 @@ verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
       break;
 
     case ADDR_EXPR:
+      /* ??? tree-ssa-alias.c may have overlooked dead PHI nodes, missing
+        dead PHIs that take the address of something.  But if the PHI
+        result is dead, the fact that it takes the address of anything
+        is irrelevant.  Because we can not tell from here if a PHI result
+        is dead, we just skip this check for PHIs altogether.  This means
+        we may be missing "valid" checks, but what can you do?
+        This was PR19217.  */
+      if (in_phi)
+       break;
+
       /* Skip any references (they will be checked when we recurse down the
         tree) and ensure that any variable used as a prefix is marked
         addressable.  */
       for (x = TREE_OPERAND (t, 0);
-          (handled_component_p (x)
-           || TREE_CODE (x) == REALPART_EXPR
-           || TREE_CODE (x) == IMAGPART_EXPR);
+          handled_component_p (x);
           x = TREE_OPERAND (x, 0))
        ;
 
@@ -3076,7 +3345,7 @@ verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
       break;
 
     case COND_EXPR:
-      x = TREE_OPERAND (t, 0);
+      x = COND_EXPR_COND (t);
       if (TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE)
        {
          error ("non-boolean used in condition");
@@ -3110,8 +3379,7 @@ verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
         that determine where to reference is either a constant or a variable,
         verify that the base is valid, and then show we've already checked
         the subtrees.  */
-      while (TREE_CODE (t) == REALPART_EXPR || TREE_CODE (t) == IMAGPART_EXPR
-            || handled_component_p (t))
+      while (handled_component_p (t))
        {
          if (TREE_CODE (t) == COMPONENT_REF && TREE_OPERAND (t, 2))
            CHECK_OP (2, "Invalid COMPONENT_REF offset operator");
@@ -3248,7 +3516,11 @@ tree_node_can_be_shared (tree t)
         gimple invariants if they overflowed.  */
       || CONSTANT_CLASS_P (t)
       || is_gimple_min_invariant (t)
-      || TREE_CODE (t) == SSA_NAME)
+      || TREE_CODE (t) == SSA_NAME
+      || t == error_mark_node)
+    return true;
+
+  if (TREE_CODE (t) == CASE_LABEL_EXPR)
     return true;
 
   while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
@@ -3314,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);
@@ -3331,7 +3609,7 @@ verify_stmts (void)
                  err |= true;
                }
 
-             addr = walk_tree (&t, verify_expr, NULL, NULL);
+             addr = walk_tree (&t, verify_expr, (void *) 1, NULL);
              if (addr)
                {
                  debug_generic_stmt (addr);
@@ -3352,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);
@@ -3408,25 +3693,38 @@ tree_verify_flow_info (void)
     {
       bool found_ctrl_stmt = false;
 
+      stmt = NULL_TREE;
+
       /* Skip labels on the start of basic block.  */
       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
        {
-         if (TREE_CODE (bsi_stmt (bsi)) != LABEL_EXPR)
+         tree prev_stmt = stmt;
+
+         stmt = bsi_stmt (bsi);
+
+         if (TREE_CODE (stmt) != LABEL_EXPR)
            break;
 
-         if (label_to_block (LABEL_EXPR_LABEL (bsi_stmt (bsi))) != bb)
+         if (prev_stmt && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
+           {
+             error ("Nonlocal label %s is not first "
+                    "in a sequence of labels in bb %d",
+                    IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
+                    bb->index);
+             err = 1;
+           }
+
+         if (label_to_block (LABEL_EXPR_LABEL (stmt)) != bb)
            {
-             tree stmt = bsi_stmt (bsi);
              error ("Label %s to block does not match in bb %d\n",
                     IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
                     bb->index);
              err = 1;
            }
 
-         if (decl_function_context (LABEL_EXPR_LABEL (bsi_stmt (bsi)))
+         if (decl_function_context (LABEL_EXPR_LABEL (stmt))
              != current_function_decl)
            {
-             tree stmt = bsi_stmt (bsi);
              error ("Label %s has incorrect context in bb %d\n",
                     IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
                     bb->index);
@@ -3452,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;
            }
@@ -3542,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);
@@ -3631,7 +3930,7 @@ tree_verify_flow_info (void)
 
                if (label_bb->aux != (void *)2)
                  {
-                   error ("Missing edge %i->%i\n",
+                   error ("Missing edge %i->%i",
                           bb->index, label_bb->index);
                    err = 1;
                  }
@@ -3661,12 +3960,12 @@ tree_make_forwarder_block (edge fallthru)
   edge e;
   edge_iterator ei;
   basic_block dummy, bb;
-  tree phi, new_phi, var, prev, next;
+  tree phi, new_phi, var;
 
   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
@@ -3677,18 +3976,11 @@ tree_make_forwarder_block (edge fallthru)
       new_phi = create_phi_node (var, bb);
       SSA_NAME_DEF_STMT (var) = new_phi;
       SET_PHI_RESULT (phi, make_ssa_name (SSA_NAME_VAR (var), phi));
-      add_phi_arg (&new_phi, PHI_RESULT (phi), fallthru);
+      add_phi_arg (new_phi, PHI_RESULT (phi), fallthru);
     }
 
   /* Ensure that the PHI node chain is in the same order.  */
-  prev = NULL;
-  for (phi = phi_nodes (bb); phi; phi = next)
-    {
-      next = PHI_CHAIN (phi);
-      PHI_CHAIN (phi) = prev;
-      prev = phi;
-    }
-  set_phi_nodes (bb, prev);
+  set_phi_nodes (bb, phi_reverse (phi_nodes (bb)));
 
   /* Add the arguments we have stored on edges.  */
   FOR_EACH_EDGE (e, ei, bb->preds)
@@ -3696,12 +3988,7 @@ tree_make_forwarder_block (edge fallthru)
       if (e == fallthru)
        continue;
 
-      for (phi = phi_nodes (bb), var = PENDING_STMT (e);
-          phi;
-          phi = PHI_CHAIN (phi), var = TREE_CHAIN (var))
-       add_phi_arg (&phi, TREE_VALUE (var), e);
-
-      PENDING_STMT (e) = NULL;
+      flush_pending_stmts (e);
     }
 }
 
@@ -3714,36 +4001,30 @@ tree_make_forwarder_block (edge fallthru)
    ENTRY_BLOCK_PTR.  */
 
 static bool
-tree_forwarder_block_p (basic_block bb)
+tree_forwarder_block_p (basic_block bb, bool phi_wanted)
 {
   block_stmt_iterator bsi;
-  edge e;
-  edge_iterator ei;
 
   /* BB must have a single outgoing edge.  */
-  if (EDGE_COUNT (bb->succs) != 1
-      /* BB can not have any PHI nodes.  This could potentially be
-        relaxed early in compilation if we re-rewrote the variables
-        appearing in any PHI nodes in forwarder blocks.  */
-      || phi_nodes (bb)
+  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.  */
+      || 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
   gcc_assert (bb != ENTRY_BLOCK_PTR);
 #endif
 
-  /* Successors of the entry block are not forwarders.  */
-  FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
-    if (e->dest == bb)
-      return false;
-
-  /* Now walk through the statements.  We can ignore labels, anything else
-     means this is not a forwarder block.  */
-  for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
+  /* Now walk through the statements backward.  We can ignore labels,
+     anything else means this is not a forwarder block.  */
+  for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_next (&bsi))
     {
       tree stmt = bsi_stmt (bsi);
  
@@ -3759,198 +4040,399 @@ tree_forwarder_block_p (basic_block bb)
        }
     }
 
+  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;
 }
 
+/* Return true if BB has at least one abnormal incoming edge.  */
+
+static inline bool
+has_abnormal_incoming_edge_p (basic_block bb)
+{
+  edge e;
+  edge_iterator ei;
+
+  FOR_EACH_EDGE (e, ei, bb->preds)
+    if (e->flags & EDGE_ABNORMAL)
+      return true;
 
-/* Thread jumps over empty statements.
+  return false;
+}
 
-   This code should _not_ thread over obviously equivalent conditions
-   as that requires nontrivial updates to the SSA graph.
+/* Removes forwarder block BB.  Returns false if this failed.  If a new
+   forwarder block is created due to redirection of edges, it is
+   stored to worklist.  */
 
-   As a precondition, we require that all basic blocks be reachable.
-   That is, there should be no opportunities left for
-   delete_unreachable_blocks.  */
-   
 static bool
-thread_jumps (void)
+remove_forwarder_block (basic_block bb, basic_block **worklist)
 {
-  edge e, last, old;
-  basic_block bb, dest, tmp, old_dest, curr, dom;
-    tree phi;
-  int arg;
-  bool retval = false;
+  edge succ = single_succ_edge (bb), e, s;
+  basic_block dest = succ->dest;
+  tree label;
+  tree phi;
+  edge_iterator ei;
+  block_stmt_iterator bsi, bsi_to;
+  bool seen_abnormal_edge = false;
 
-  FOR_EACH_BB (bb)
-    bb_ann (bb)->forwardable = tree_forwarder_block_p (bb);
+  /* We check for infinite loops already in tree_forwarder_block_p.
+     However it may happen that the infinite loop is created
+     afterwards due to removal of forwarders.  */
+  if (dest == bb)
+    return false;
 
-  FOR_EACH_BB (bb)
+  /* If the destination block consists of a nonlocal label, do not merge
+     it.  */
+  label = first_stmt (dest);
+  if (label
+      && TREE_CODE (label) == LABEL_EXPR
+      && DECL_NONLOCAL (LABEL_EXPR_LABEL (label)))
+    return false;
+
+  /* If there is an abnormal edge to basic block BB, but not into
+     dest, problems might occur during removal of the phi node at out
+     of ssa due to overlapping live ranges of registers.
+
+     If there is an abnormal edge in DEST, the problems would occur
+     anyway since cleanup_dead_labels would then merge the labels for
+     two different eh regions, and rest of exception handling code
+     does not like it.
+     
+     So if there is an abnormal edge to BB, proceed only if there is
+     no abnormal edge to DEST and there are no phi nodes in DEST.  */
+  if (has_abnormal_incoming_edge_p (bb))
     {
-      edge_iterator ei;
-      bool this_jump_threaded = false;
+      seen_abnormal_edge = true;
 
-      /* Don't waste time on forwarders.  */
-      if (bb_ann (bb)->forwardable)
-       continue;
+      if (has_abnormal_incoming_edge_p (dest)
+         || phi_nodes (dest) != NULL_TREE)
+       return false;
+    }
 
-      /* Examine each of our block's successors to see if it is
-        forwardable.  */
-      for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
+  /* If there are phi nodes in DEST, and some of the blocks that are
+     predecessors of BB are also predecessors of DEST, check that the
+     phi node arguments match.  */
+  if (phi_nodes (dest))
+    {
+      FOR_EACH_EDGE (e, ei, bb->preds)
        {
-         int freq;
-         gcov_type count;
+         s = find_edge (e->src, dest);
+         if (!s)
+           continue;
 
-         /* If the edge is abnormal or its destination is not
-            forwardable, then there's nothing to do.  */
-         if ((e->flags & EDGE_ABNORMAL)
-             || !bb_ann (e->dest)->forwardable)
-           {
-             ei_next (&ei);
-             continue;
-           }
+         if (!phi_alternatives_equal (dest, succ, s))
+           return false;
+       }
+    }
+
+  /* Redirect the edges.  */
+  for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
+    {
+      if (e->flags & EDGE_ABNORMAL)
+       {
+         /* If there is an abnormal edge, redirect it anyway, and
+            move the labels to the new block to make it legal.  */
+         s = redirect_edge_succ_nodup (e, dest);
+       }
+      else
+       s = redirect_edge_and_branch (e, dest);
+
+      if (s == e)
+       {
+         /* Create arguments for the phi nodes, since the edge was not
+            here before.  */
+         for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
+           add_phi_arg (phi, PHI_ARG_DEF (phi, succ->dest_idx), s);
+       }
+      else
+       {
+         /* The source basic block might become a forwarder.  We know
+            that it was not a forwarder before, since it used to have
+            at least two outgoing edges, so we may just add it to
+            worklist.  */
+         if (tree_forwarder_block_p (s->src, false))
+           *(*worklist)++ = s->src;
+       }
+    }
+
+  if (seen_abnormal_edge)
+    {
+      /* Move the labels to the new block, so that the redirection of
+        the abnormal edges works.  */
+
+      bsi_to = bsi_start (dest);
+      for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
+       {
+         label = bsi_stmt (bsi);
+         gcc_assert (TREE_CODE (label) == LABEL_EXPR);
+         bsi_remove (&bsi);
+         bsi_insert_before (&bsi_to, label, BSI_CONTINUE_LINKING);
+       }
+    }
+
+  /* Update the dominators.  */
+  if (dom_info_available_p (CDI_DOMINATORS))
+    {
+      basic_block dom, dombb, domdest;
+
+      dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
+      domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
+      if (domdest == bb)
+       {
+         /* Shortcut to avoid calling (relatively expensive)
+            nearest_common_dominator unless necessary.  */
+         dom = dombb;
+       }
+      else
+       dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
+
+      set_immediate_dominator (CDI_DOMINATORS, dest, dom);
+    }
+
+  /* And kill the forwarder block.  */
+  delete_basic_block (bb);
+
+  return true;
+}
+
+/* Removes forwarder blocks.  */
+
+static bool
+cleanup_forwarder_blocks (void)
+{
+  basic_block bb;
+  bool changed = false;
+  basic_block *worklist = xmalloc (sizeof (basic_block) * n_basic_blocks);
+  basic_block *current = worklist;
+
+  FOR_EACH_BB (bb)
+    {
+      if (tree_forwarder_block_p (bb, false))
+       *current++ = bb;
+    }
+
+  while (current != worklist)
+    {
+      bb = *--current;
+      changed |= remove_forwarder_block (bb, &current);
+    }
+
+  free (worklist);
+  return changed;
+}
+
+/* Merge the PHI nodes at BB into those at BB's sole successor.  */
+
+static void
+remove_forwarder_block_with_phi (basic_block bb)
+{
+  edge succ = single_succ_edge (bb);
+  basic_block dest = succ->dest;
+  tree label;
+  basic_block dombb, domdest, dom;
+
+  /* We check for infinite loops already in tree_forwarder_block_p.
+     However it may happen that the infinite loop is created
+     afterwards due to removal of forwarders.  */
+  if (dest == bb)
+    return;
+
+  /* If the destination block consists of a nonlocal label, do not
+     merge it.  */
+  label = first_stmt (dest);
+  if (label
+      && TREE_CODE (label) == LABEL_EXPR
+      && DECL_NONLOCAL (LABEL_EXPR_LABEL (label)))
+    return;
+
+  /* Redirect each incoming edge to BB to DEST.  */
+  while (EDGE_COUNT (bb->preds) > 0)
+    {
+      edge e = EDGE_PRED (bb, 0), s;
+      tree phi;
 
-         count = e->count;
-         freq = EDGE_FREQUENCY (e);
-
-         /* Now walk through as many forwarder blocks as possible to
-            find the ultimate destination we want to thread our jump
-            to.  */
-         last = EDGE_SUCC (e->dest, 0);
-         bb_ann (e->dest)->forwardable = 0;
-         for (dest = EDGE_SUCC (e->dest, 0)->dest;
-              bb_ann (dest)->forwardable;
-              last = EDGE_SUCC (dest, 0),
-              dest = EDGE_SUCC (dest, 0)->dest)
-           bb_ann (dest)->forwardable = 0;
-
-         /* Reset the forwardable marks to 1.  */
-         for (tmp = e->dest;
-              tmp != dest;
-              tmp = EDGE_SUCC (tmp, 0)->dest)
-           bb_ann (tmp)->forwardable = 1;
-
-         if (dest == e->dest)
+      s = find_edge (e->src, dest);
+      if (s)
+       {
+         /* We already have an edge S from E->src to DEST.  If S and
+            E->dest's sole successor edge have the same PHI arguments
+            at DEST, redirect S to DEST.  */
+         if (phi_alternatives_equal (dest, s, succ))
            {
-             ei_next (&ei);
+             e = redirect_edge_and_branch (e, dest);
+             PENDING_STMT (e) = NULL_TREE;
              continue;
            }
-             
-         old = find_edge (bb, dest);
-         if (old)
+
+         /* PHI arguments are different.  Create a forwarder block by
+            splitting E so that we can merge PHI arguments on E to
+            DEST.  */
+         e = single_succ_edge (split_edge (e));
+       }
+
+      s = redirect_edge_and_branch (e, dest);
+
+      /* redirect_edge_and_branch must not create a new edge.  */
+      gcc_assert (s == e);
+
+      /* Add to the PHI nodes at DEST each PHI argument removed at the
+        destination of E.  */
+      for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
+       {
+         tree def = PHI_ARG_DEF (phi, succ->dest_idx);
+
+         if (TREE_CODE (def) == SSA_NAME)
            {
-             /* If there already is an edge, check whether the values
-                in phi nodes differ.  */
-             if (!phi_alternatives_equal (dest, last, old))
+             tree var;
+
+             /* If DEF is one of the results of PHI nodes removed during
+                redirection, replace it with the PHI argument that used
+                to be on E.  */
+             for (var = PENDING_STMT (e); var; var = TREE_CHAIN (var))
                {
-                 /* The previous block is forwarder.  Redirect our jump
-                    to that target instead since we know it has no PHI
-                    nodes that will need updating.  */
-                 dest = last->src;
-         
-                 /* That might mean that no forwarding at all is possible.  */
-                 if (dest == e->dest)
+                 tree old_arg = TREE_PURPOSE (var);
+                 tree new_arg = TREE_VALUE (var);
+
+                 if (def == old_arg)
                    {
-                     ei_next (&ei);
-                     continue;
+                     def = new_arg;
+                     break;
                    }
-
-                 old = find_edge (bb, dest);
                }
            }
 
-         /* Perform the redirection.  */
-         retval = this_jump_threaded = true;
-         old_dest = e->dest;
-         e = redirect_edge_and_branch (e, dest);
+         add_phi_arg (phi, def, s);
+       }
 
-         /* Update the profile.  */
-         if (profile_status != PROFILE_ABSENT)
-           for (curr = old_dest; curr != dest; curr = EDGE_SUCC (curr, 0)->dest)
-             {
-               curr->frequency -= freq;
-               if (curr->frequency < 0)
-                 curr->frequency = 0;
-               curr->count -= count;
-               if (curr->count < 0)
-                 curr->count = 0;
-               EDGE_SUCC (curr, 0)->count -= count;
-               if (EDGE_SUCC (curr, 0)->count < 0)
-                 EDGE_SUCC (curr, 0)->count = 0;
-             }
+      PENDING_STMT (e) = NULL;
+    }
 
-         if (!old)
-           {
-             /* Update PHI nodes.   We know that the new argument should
-                have the same value as the argument associated with LAST.
-                Otherwise we would have changed our target block above.  */
-             for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
-               {
-                 arg = phi_arg_from_edge (phi, last);
-                 gcc_assert (arg >= 0);
-                 add_phi_arg (&phi, PHI_ARG_DEF (phi, arg), e);
-               }
-           }
+  /* Update the dominators.  */
+  dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
+  domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
+  if (domdest == bb)
+    {
+      /* Shortcut to avoid calling (relatively expensive)
+        nearest_common_dominator unless necessary.  */
+      dom = dombb;
+    }
+  else
+    dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
 
-         /* Remove the unreachable blocks (observe that if all blocks
-            were reachable before, only those in the path we threaded
-            over and did not have any predecessor outside of the path
-            become unreachable).  */
-         for (; old_dest != dest; old_dest = tmp)
-           {
-             tmp = EDGE_SUCC (old_dest, 0)->dest;
+  set_immediate_dominator (CDI_DOMINATORS, dest, dom);
+  
+  /* Remove BB since all of BB's incoming edges have been redirected
+     to DEST.  */
+  delete_basic_block (bb);
+}
 
-             if (EDGE_COUNT (old_dest->preds) > 0)
-               break;
+/* This pass merges PHI nodes if one feeds into another.  For example,
+   suppose we have the following:
 
-             delete_basic_block (old_dest);
-           }
+  goto <bb 9> (<L9>);
 
-         /* Update the dominators.  */
-         if (dom_info_available_p (CDI_DOMINATORS))
-           {
-             /* If the dominator of the destination was in the path, set its
-                dominator to the start of the redirected edge.  */
-             if (get_immediate_dominator (CDI_DOMINATORS, old_dest) == NULL)
-               set_immediate_dominator (CDI_DOMINATORS, old_dest, bb);
+<L8>:;
+  tem_17 = foo ();
 
-             /* Now proceed like if we forwarded just over one edge at a time.
-                Algorithm for forwarding edge S --> A over edge A --> B then
-                is
+  # tem_6 = PHI <tem_17(8), tem_23(7)>;
+<L9>:;
 
-                if (idom (B) == A
-                    && !dominated_by (S, B))
-                  idom (B) = idom (A);
-                recount_idom (A);  */
+  # tem_3 = PHI <tem_6(9), tem_2(5)>;
+<L10>:;
 
-             for (; old_dest != dest; old_dest = tmp)
-               {
-                 tmp = EDGE_SUCC (old_dest, 0)->dest;
+  Then we merge the first PHI node into the second one like so:
 
-                 if (get_immediate_dominator (CDI_DOMINATORS, tmp) == old_dest
-                     && !dominated_by_p (CDI_DOMINATORS, bb, tmp))
-                   {
-                     dom = get_immediate_dominator (CDI_DOMINATORS, old_dest);
-                     set_immediate_dominator (CDI_DOMINATORS, tmp, dom);
-                   }
+  goto <bb 9> (<L10>);
 
-                 dom = recount_dominator (CDI_DOMINATORS, old_dest);
-                 set_immediate_dominator (CDI_DOMINATORS, old_dest, dom);
-               }
-           }
+<L8>:;
+  tem_17 = foo ();
+
+  # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>;
+<L10>:;
+*/
+
+static void
+merge_phi_nodes (void)
+{
+  basic_block *worklist = xmalloc (sizeof (basic_block) * n_basic_blocks);
+  basic_block *current = worklist;
+  basic_block bb;
+
+  calculate_dominance_info (CDI_DOMINATORS);
+
+  /* Find all PHI nodes that we may be able to merge.  */
+  FOR_EACH_BB (bb)
+    {
+      basic_block dest;
+
+      /* Look for a forwarder block with PHI nodes.  */
+      if (!tree_forwarder_block_p (bb, true))
+       continue;
+
+      dest = single_succ (bb);
+
+      /* We have to feed into another basic block with PHI
+        nodes.  */
+      if (!phi_nodes (dest)
+         /* We don't want to deal with a basic block with
+            abnormal edges.  */
+         || has_abnormal_incoming_edge_p (bb))
+       continue;
+
+      if (!dominated_by_p (CDI_DOMINATORS, dest, bb))
+       {
+         /* If BB does not dominate DEST, then the PHI nodes at
+            DEST must be the only users of the results of the PHI
+            nodes at BB.  */
+         *current++ = bb;
        }
+    }
 
-      /* If we succeeded in threading a jump at BB, update the
-        forwardable mark as BB may have become a new forwarder block.
-        This could happen if we have a useless "if" statement whose
-        two arms eventually merge without any intervening
-        statements.  */
-      if (this_jump_threaded && tree_forwarder_block_p (bb))
-       bb_ann (bb)->forwardable = true;
+  /* Now let's drain WORKLIST.  */
+  while (current != worklist)
+    {
+      bb = *--current;
+      remove_forwarder_block_with_phi (bb);
     }
 
-  return retval;
+  free (worklist);
+}
+
+static bool
+gate_merge_phi (void)
+{
+  return 1;
 }
 
+struct tree_opt_pass pass_merge_phi = {
+  "mergephi",                  /* name */
+  gate_merge_phi,              /* gate */
+  merge_phi_nodes,             /* execute */
+  NULL,                                /* sub */
+  NULL,                                /* next */
+  0,                           /* static_pass_number */
+  TV_TREE_MERGE_PHI,           /* tv_id */
+  PROP_cfg | PROP_ssa,         /* properties_required */
+  0,                           /* properties_provided */
+  0,                           /* properties_destroyed */
+  0,                           /* todo_flags_start */
+  TODO_dump_func | TODO_ggc_collect    /* todo_flags_finish */
+  | TODO_verify_ssa,
+  0                            /* letter */
+};
 
 /* Return a non-special label in the head of basic block BLOCK.
    Create one if it doesn't exist.  */
@@ -3993,17 +4475,15 @@ static edge
 tree_try_redirect_by_replacing_jump (edge e, basic_block target)
 {
   basic_block src = e->src;
-  edge tmp;
   block_stmt_iterator b;
   tree stmt;
-  edge_iterator ei;
-
-  /* Verify that all targets will be TARGET.  */
-  FOR_EACH_EDGE (tmp, ei, src->succs)
-    if (tmp->dest != target && tmp != e)
-      break;
 
-  if (tmp)
+  /* We can replace or remove a complex jump only when we have exactly
+     two edges.  */
+  if (EDGE_COUNT (src->succs) != 2
+      /* Verify that all targets will be TARGET.  Specifically, the
+        edge that is not E must also go to TARGET.  */
+      || EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target)
     return NULL;
 
   b = bsi_last (src);
@@ -4066,17 +4546,49 @@ tree_redirect_edge_and_branch (edge e, basic_block dest)
 
     case SWITCH_EXPR:
       {
-       tree vec = SWITCH_LABELS (stmt);
-       size_t i, n = TREE_VEC_LENGTH (vec);
+        tree cases = get_cases_for_edge (e, stmt);
+
+       /* If we have a list of cases associated with E, then use it
+          as it's a lot faster than walking the entire case vector.  */
+       if (cases)
+         {
+           edge e2 = find_edge (e->src, dest);
+           tree last, first;
+
+           first = cases;
+           while (cases)
+             {
+               last = cases;
+               CASE_LABEL (cases) = label;
+               cases = TREE_CHAIN (cases);
+             }
+
+           /* If there was already an edge in the CFG, then we need
+              to move all the cases associated with E to E2.  */
+           if (e2)
+             {
+               tree cases2 = get_cases_for_edge (e2, stmt);
 
-       for (i = 0; i < n; ++i)
+               TREE_CHAIN (last) = TREE_CHAIN (cases2);
+               TREE_CHAIN (cases2) = first;
+             }
+         }
+       else
          {
-           tree elt = TREE_VEC_ELT (vec, i);
-           if (label_to_block (CASE_LABEL (elt)) == e->dest)
-             CASE_LABEL (elt) = label;
+           tree vec = SWITCH_LABELS (stmt);
+           size_t i, n = TREE_VEC_LENGTH (vec);
+
+           for (i = 0; i < n; i++)
+             {
+               tree elt = TREE_VEC_ELT (vec, i);
+
+               if (label_to_block (CASE_LABEL (elt)) == e->dest)
+                 CASE_LABEL (elt) = label;
+             }
          }
+
+       break;
       }
-      break;
 
     case RETURN_EXPR:
       bsi_remove (&bsi);
@@ -4202,12 +4714,12 @@ tree_duplicate_bb (basic_block bb)
   /* First copy the phi nodes.  We do not copy phi node arguments here,
      since the edges are not ready yet.  Keep the chain of phi nodes in
      the same order, so that we can add them later.  */
-  for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
+  for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
     {
       mark_for_rewrite (PHI_RESULT (phi));
       create_phi_node (PHI_RESULT (phi), new_bb);
     }
-  set_phi_nodes (new_bb, nreverse (phi_nodes (new_bb)));
+  set_phi_nodes (new_bb, phi_reverse (phi_nodes (new_bb)));
 
   bsi_tgt = bsi_start (new_bb);
   for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
@@ -4218,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);
 
@@ -4276,13 +4785,13 @@ add_phi_args_after_copy_bb (basic_block bb_copy)
 
       for (phi = phi_nodes (e->dest), phi_copy = phi_nodes (e_copy->dest);
           phi;
-          phi = phi_next, phi_copy = TREE_CHAIN (phi_copy))
+          phi = phi_next, phi_copy = PHI_CHAIN (phi_copy))
        {
-         phi_next = TREE_CHAIN (phi);
+         phi_next = PHI_CHAIN (phi);
 
          gcc_assert (PHI_RESULT (phi) == PHI_RESULT (phi_copy));
          def = PHI_ARG_DEF_FROM_EDGE (phi, e);
-         add_phi_arg (&phi_copy, def, e_copy);
+         add_phi_arg (phi_copy, def, e_copy);
        }
     }
 }
@@ -4424,7 +4933,7 @@ rewrite_to_new_ssa_names_bb (basic_block bb, htab_t map)
     if (e->flags & EDGE_ABNORMAL)
       break;
 
-  for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
+  for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
     {
       rewrite_to_new_ssa_names_def (PHI_RESULT_PTR (phi), phi, map);
       if (e)
@@ -4434,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);
@@ -4460,12 +4968,16 @@ rewrite_to_new_ssa_names_bb (basic_block bb, htab_t map)
 
       v_must_defs = V_MUST_DEF_OPS (ann);
       for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++)
-       rewrite_to_new_ssa_names_def
-               (V_MUST_DEF_OP_PTR (v_must_defs, i), stmt, map);
+       {
+         rewrite_to_new_ssa_names_def
+           (V_MUST_DEF_RESULT_PTR (v_must_defs, i), stmt, map);
+         rewrite_to_new_ssa_names_use
+           (V_MUST_DEF_KILL_PTR (v_must_defs, i),  map);
+       }
     }
 
   FOR_EACH_EDGE (e, ei, bb->succs)
-    for (phi = phi_nodes (e->dest); phi; phi = TREE_CHAIN (phi))
+    for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
       {
        rewrite_to_new_ssa_names_use
                (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e), map);
@@ -4510,7 +5022,7 @@ tree_duplicate_sese_region (edge entry, edge exit,
   struct loop *loop = entry->dest->loop_father;
   edge exit_copy;
   bitmap definitions;
-  tree phi, var;
+  tree phi;
   basic_block *doms;
   htab_t ssa_name_map = NULL;
   edge redirected;
@@ -4579,11 +5091,7 @@ tree_duplicate_sese_region (edge entry, edge exit,
   /* Redirect the entry and add the phi node arguments.  */
   redirected = redirect_edge_and_branch (entry, entry->dest->rbi->copy);
   gcc_assert (redirected != NULL);
-  for (phi = phi_nodes (entry->dest), var = PENDING_STMT (entry);
-       phi;
-       phi = TREE_CHAIN (phi), var = TREE_CHAIN (var))
-    add_phi_arg (&phi, TREE_VALUE (var), entry);
-  PENDING_STMT (entry) = NULL;
+  flush_pending_stmts (entry);
 
   /* Concerning updating of dominators:  We must recount dominators
      for entry block and its copy.  Anything that is outside of the region, but
@@ -4604,8 +5112,8 @@ tree_duplicate_sese_region (edge entry, edge exit,
       tree name = ssa_name (ver);
 
       phi = create_phi_node (name, exit->dest);
-      add_phi_arg (&phi, name, exit);
-      add_phi_arg (&phi, name, exit_copy);
+      add_phi_arg (phi, name, exit);
+      add_phi_arg (phi, name, exit_copy);
 
       SSA_NAME_DEF_STMT (name) = phi;
     }
@@ -4624,7 +5132,7 @@ tree_duplicate_sese_region (edge entry, edge exit,
     free (region_copy);
 
   unmark_all_for_rewrite ();
-  BITMAP_XFREE (definitions);
+  BITMAP_FREE (definitions);
 
   return true;
 }
@@ -4659,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;
 
@@ -4675,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);
@@ -4856,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
@@ -4864,8 +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_LONGJMP | ECF_ALWAYS_RETURN)))
+      && !(call_expr_flags (call) & ECF_NORETURN))
     return true;
 
   if (TREE_CODE (t) == ASM_EXPR
@@ -4914,7 +5421,6 @@ tree_flow_call_edges_add (sbitmap blocks)
      Handle this by adding a dummy instruction in a new last basic block.  */
   if (check_last_block)
     {
-      edge_iterator ei;
       basic_block bb = EXIT_BLOCK_PTR->prev_bb;
       block_stmt_iterator bsi = bsi_last (bb);
       tree t = NULL_TREE;
@@ -4925,13 +5431,12 @@ tree_flow_call_edges_add (sbitmap blocks)
        {
          edge e;
 
-         FOR_EACH_EDGE (e, ei, bb->succs)
-           if (e->dest == EXIT_BLOCK_PTR)
-             {
-               bsi_insert_on_edge (e, build_empty_stmt ());
-               bsi_commit_edge_inserts ((int *)NULL);
-               break;
-             }
+         e = find_edge (bb, EXIT_BLOCK_PTR);
+         if (e)
+           {
+             bsi_insert_on_edge (e, build_empty_stmt ());
+             bsi_commit_edge_inserts ();
+           }
        }
     }
 
@@ -4968,9 +5473,8 @@ tree_flow_call_edges_add (sbitmap blocks)
 #ifdef ENABLE_CHECKING
                  if (stmt == last_stmt)
                    {
-                     edge_iterator ei;
-                     FOR_EACH_EDGE (e, ei, bb->succs)
-                       gcc_assert (e->dest != EXIT_BLOCK_PTR);
+                     e = find_edge (bb, EXIT_BLOCK_PTR);
+                     gcc_assert (e == NULL);
                    }
 #endif
 
@@ -5011,7 +5515,7 @@ tree_purge_dead_eh_edges (basic_block bb)
     {
       if (e->flags & EDGE_EH)
        {
-         ssa_remove_edge (e);
+         remove_edge (e);
          changed = true;
        }
       else
@@ -5045,7 +5549,7 @@ bool
 tree_purge_all_dead_eh_edges (bitmap blocks)
 {
   bool changed = false;
-  size_t i;
+  unsigned i;
   bitmap_iterator bi;
 
   EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
@@ -5056,6 +5560,93 @@ tree_purge_all_dead_eh_edges (bitmap blocks)
   return changed;
 }
 
+/* This function is called whenever a new edge is created or
+   redirected.  */
+
+static void
+tree_execute_on_growing_pred (edge e)
+{
+  basic_block bb = e->dest;
+
+  if (phi_nodes (bb))
+    reserve_phi_args_for_new_edge (bb);
+}
+
+/* This function is called immediately before edge E is removed from
+   the edge vector E->dest->preds.  */
+
+static void
+tree_execute_on_shrinking_pred (edge e)
+{
+  if (phi_nodes (e->dest))
+    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,
@@ -5077,7 +5668,14 @@ struct cfg_hooks tree_cfg_hooks = {
   NULL,                                /* tidy_fallthru_edge  */
   tree_block_ends_with_call_p, /* block_ends_with_call_p */
   tree_block_ends_with_condjump_p, /* block_ends_with_condjump_p */
-  tree_flow_call_edges_add      /* flow_call_edges_add */
+  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 */  
 };
 
 
@@ -5090,6 +5688,10 @@ split_critical_edges (void)
   edge e;
   edge_iterator ei;
 
+  /* split_edge can redirect edges out of SWITCH_EXPRs, which can get
+     expensive.  So we want to enable recording of edge to CASE_LABEL_EXPR
+     mappings around the calls to split_edge.  */
+  start_recording_case_labels ();
   FOR_ALL_BB (bb)
     {
       FOR_EACH_EDGE (e, ei, bb->succs)
@@ -5098,6 +5700,7 @@ split_critical_edges (void)
            split_edge (e);
          }
     }
+  end_recording_case_labels ();
 }
 
 struct tree_opt_pass pass_split_crit_edges = 
@@ -5246,6 +5849,7 @@ execute_warn_function_return (void)
   /* If we see "return;" in some basic block, then we do reach the end
      without returning a value.  */
   else if (warn_return_type
+          && !TREE_NO_WARNING (cfun->decl)
           && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0
           && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (cfun->decl))))
     {
@@ -5266,6 +5870,7 @@ execute_warn_function_return (void)
                locus = &cfun->function_end_locus;
              warning ("%Hcontrol reaches end of non-void function", locus);
 #endif
+             TREE_NO_WARNING (cfun->decl) = 1;
              break;
            }
        }
@@ -5313,5 +5918,3 @@ struct tree_opt_pass pass_warn_function_return =
   0,                                   /* todo_flags_finish */
   0                                    /* letter */
 };
-
-#include "gt-tree-cfg.h"