OSDN Git Service

2010-02-10 Joost VandeVondele <jv244@cam.ac.uk>
[pf3gnuchains/gcc-fork.git] / gcc / tree-cfg.c
index 5b747e8..e5ed9ec 100644 (file)
@@ -1,6 +1,6 @@
 /* Control flow functions for trees.
-   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
-   Free Software Foundation, Inc.
+   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
+   2010  Free Software Foundation, Inc.
    Contributed by Diego Novillo <dnovillo@redhat.com>
 
 This file is part of GCC.
@@ -59,7 +59,7 @@ static const int initial_cfg_capacity = 20;
 /* 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.
+   hash table to prevent problems with duplication of GIMPLE_SWITCHes.
 
    Access to this list of CASE_LABEL_EXPRs allows us to efficiently
    update the case vector in response to edge redirections.
@@ -82,36 +82,46 @@ static struct cfg_stats_d cfg_stats;
 /* Nonzero if we found a computed goto while building basic blocks.  */
 static bool found_computed_goto;
 
+/* Hash table to store last discriminator assigned for each locus.  */
+struct locus_discrim_map
+{
+  location_t locus;
+  int discriminator;
+};
+static htab_t discriminator_per_locus;
+
 /* Basic blocks and flowgraphs.  */
-static basic_block create_bb (void *, void *, basic_block);
-static void make_blocks (tree);
+static void make_blocks (gimple_seq);
 static void factor_computed_gotos (void);
 
 /* Edges.  */
 static void make_edges (void);
 static void make_cond_expr_edges (basic_block);
-static void make_switch_expr_edges (basic_block);
+static void make_gimple_switch_edges (basic_block);
 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 make_gimple_asm_edges (basic_block);
+static unsigned int locus_map_hash (const void *);
+static int locus_map_eq (const void *, const void *);
+static void assign_discriminator (location_t, basic_block);
+static edge gimple_redirect_edge_and_branch (edge, basic_block);
+static edge gimple_try_redirect_by_replacing_jump (edge, basic_block);
 static unsigned int split_critical_edges (void);
 
 /* Various helpers.  */
-static inline bool stmt_starts_bb_p (const_tree, const_tree);
-static int tree_verify_flow_info (void);
-static void tree_make_forwarder_block (edge);
-static void tree_cfg2vcg (FILE *);
-static inline void change_bb_for_stmt (tree t, basic_block bb);
-static bool computed_goto_p (const_tree);
+static inline bool stmt_starts_bb_p (gimple, gimple);
+static int gimple_verify_flow_info (void);
+static void gimple_make_forwarder_block (edge);
+static void gimple_cfg2vcg (FILE *);
+static gimple first_non_label_stmt (basic_block);
 
 /* Flowgraph optimization and cleanup.  */
-static void tree_merge_blocks (basic_block, basic_block);
-static bool tree_can_merge_blocks_p (basic_block, basic_block);
+static void gimple_merge_blocks (basic_block, basic_block);
+static bool gimple_can_merge_blocks_p (basic_block, basic_block);
 static void remove_bb (basic_block);
 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 tree find_case_label_for_value (gimple, tree);
 
 void
 init_empty_tree_cfg_for_function (struct function *fn)
@@ -134,9 +144,9 @@ init_empty_tree_cfg_for_function (struct function *fn)
                         label_to_block_map_for_function (fn),
                         initial_cfg_capacity);
 
-  SET_BASIC_BLOCK_FOR_FUNCTION (fn, ENTRY_BLOCK, 
+  SET_BASIC_BLOCK_FOR_FUNCTION (fn, ENTRY_BLOCK,
                                ENTRY_BLOCK_PTR_FOR_FUNCTION (fn));
-  SET_BASIC_BLOCK_FOR_FUNCTION (fn, EXIT_BLOCK, 
+  SET_BASIC_BLOCK_FOR_FUNCTION (fn, EXIT_BLOCK,
                   EXIT_BLOCK_PTR_FOR_FUNCTION (fn));
 
   ENTRY_BLOCK_PTR_FOR_FUNCTION (fn)->next_bb
@@ -155,21 +165,21 @@ init_empty_tree_cfg (void)
                              Create basic blocks
 ---------------------------------------------------------------------------*/
 
-/* Entry point to the CFG builder for trees.  TP points to the list of
+/* Entry point to the CFG builder for trees.  SEQ is the sequence of
    statements to be added to the flowgraph.  */
 
 static void
-build_tree_cfg (tree *tp)
+build_gimple_cfg (gimple_seq seq)
 {
-  /* Register specific tree functions.  */
-  tree_register_cfg_hooks ();
+  /* Register specific gimple functions.  */
+  gimple_register_cfg_hooks ();
 
   memset ((void *) &cfg_stats, 0, sizeof (cfg_stats));
 
   init_empty_tree_cfg ();
 
   found_computed_goto = 0;
-  make_blocks (*tp);
+  make_blocks (seq);
 
   /* Computed gotos are hell to deal with, especially if there are
      lots of them with a large number of destinations.  So we factor
@@ -196,8 +206,11 @@ build_tree_cfg (tree *tp)
   group_case_labels ();
 
   /* Create the edges of the flowgraph.  */
+  discriminator_per_locus = htab_create (13, locus_map_hash, locus_map_eq,
+                                         free);
   make_edges ();
   cleanup_dead_labels ();
+  htab_delete (discriminator_per_locus);
 
   /* Debugging dumps.  */
 
@@ -207,7 +220,7 @@ build_tree_cfg (tree *tp)
     FILE *vcg_file = dump_begin (TDI_vcg, &local_dump_flags);
     if (vcg_file)
       {
-       tree_cfg2vcg (vcg_file);
+       gimple_cfg2vcg (vcg_file);
        dump_end (TDI_vcg, vcg_file);
       }
   }
@@ -215,16 +228,20 @@ build_tree_cfg (tree *tp)
 #ifdef ENABLE_CHECKING
   verify_stmts ();
 #endif
-
-  /* Dump a textual representation of the flowgraph.  */
-  if (dump_file)
-    dump_tree_cfg (dump_file, dump_flags);
 }
 
 static unsigned int
 execute_build_cfg (void)
 {
-  build_tree_cfg (&DECL_SAVED_TREE (current_function_decl));
+  gimple_seq body = gimple_body (current_function_decl);
+
+  build_gimple_cfg (body);
+  gimple_set_body (current_function_decl, NULL);
+  if (dump_file && (dump_flags & TDF_DETAILS))
+    {
+      fprintf (dump_file, "Scope blocks:\n");
+      dump_scope_blocks (dump_file, dump_flags);
+    }
   return 0;
 }
 
@@ -239,14 +256,26 @@ struct gimple_opt_pass pass_build_cfg =
   NULL,                                        /* next */
   0,                                   /* static_pass_number */
   TV_TREE_CFG,                         /* tv_id */
-  PROP_gimple_leh,                     /* properties_required */
+  PROP_gimple_leh,                     /* properties_required */
   PROP_cfg,                            /* properties_provided */
   0,                                   /* properties_destroyed */
   0,                                   /* todo_flags_start */
-  TODO_verify_stmts | TODO_cleanup_cfg /* todo_flags_finish */
+  TODO_verify_stmts | TODO_cleanup_cfg
+  | TODO_dump_func                     /* todo_flags_finish */
  }
 };
 
+
+/* Return true if T is a computed goto.  */
+
+static bool
+computed_goto_p (gimple t)
+{
+  return (gimple_code (t) == GIMPLE_GOTO
+         && TREE_CODE (gimple_goto_dest (t)) != LABEL_DECL);
+}
+
+
 /* Search the CFG for any computed gotos.  If found, factor them to a
    common computed goto site.  Also record the location of that site so
    that we can un-factor the gotos after we have converted back to
@@ -258,8 +287,8 @@ factor_computed_gotos (void)
   basic_block bb;
   tree factored_label_decl = NULL;
   tree var = NULL;
-  tree factored_computed_goto_label = NULL;
-  tree factored_computed_goto = NULL;
+  gimple factored_computed_goto_label = NULL;
+  gimple factored_computed_goto = NULL;
 
   /* We know there are one or more computed gotos in this function.
      Examine the last statement in each basic block to see if the block
@@ -267,12 +296,13 @@ factor_computed_gotos (void)
 
   FOR_EACH_BB (bb)
     {
-      block_stmt_iterator bsi = bsi_last (bb);
-      tree last;
+      gimple_stmt_iterator gsi = gsi_last_bb (bb);
+      gimple last;
 
-      if (bsi_end_p (bsi))
+      if (gsi_end_p (gsi))
        continue;
-      last = bsi_stmt (bsi);
+
+      last = gsi_stmt (gsi);
 
       /* Ignore the computed goto we create when we factor the original
         computed gotos.  */
@@ -282,15 +312,15 @@ factor_computed_gotos (void)
       /* If the last statement is a computed goto, factor it.  */
       if (computed_goto_p (last))
        {
-         tree assignment;
+         gimple assignment;
 
          /* The first time we find a computed goto we need to create
             the factored goto block and the variable each original
             computed goto will use for their goto destination.  */
-         if (! factored_computed_goto)
+         if (!factored_computed_goto)
            {
              basic_block new_bb = create_empty_bb (bb);
-             block_stmt_iterator new_bsi = bsi_start (new_bb);
+             gimple_stmt_iterator new_gsi = gsi_start_bb (new_bb);
 
              /* Create the destination of the factored goto.  Each original
                 computed goto will put its desired destination into this
@@ -300,62 +330,60 @@ factor_computed_gotos (void)
 
              /* Build a label for the new block which will contain the
                 factored computed goto.  */
-             factored_label_decl = create_artificial_label ();
+             factored_label_decl = create_artificial_label (UNKNOWN_LOCATION);
              factored_computed_goto_label
-               = build1 (LABEL_EXPR, void_type_node, factored_label_decl);
-             bsi_insert_after (&new_bsi, factored_computed_goto_label,
-                               BSI_NEW_STMT);
+               = gimple_build_label (factored_label_decl);
+             gsi_insert_after (&new_gsi, factored_computed_goto_label,
+                               GSI_NEW_STMT);
 
              /* Build our new computed goto.  */
-             factored_computed_goto = build1 (GOTO_EXPR, void_type_node, var);
-             bsi_insert_after (&new_bsi, factored_computed_goto,
-                               BSI_NEW_STMT);
+             factored_computed_goto = gimple_build_goto (var);
+             gsi_insert_after (&new_gsi, factored_computed_goto, GSI_NEW_STMT);
            }
 
          /* Copy the original computed goto's destination into VAR.  */
-         assignment = build_gimple_modify_stmt (var,
-                                                GOTO_DESTINATION (last));
-         bsi_insert_before (&bsi, assignment, BSI_SAME_STMT);
+         assignment = gimple_build_assign (var, gimple_goto_dest (last));
+         gsi_insert_before (&gsi, assignment, GSI_SAME_STMT);
 
          /* And re-vector the computed goto to the new destination.  */
-         GOTO_DESTINATION (last) = factored_label_decl;
+         gimple_goto_set_dest (last, factored_label_decl);
        }
     }
 }
 
 
-/* Build a flowgraph for the statement_list STMT_LIST.  */
+/* Build a flowgraph for the sequence of stmts SEQ.  */
 
 static void
-make_blocks (tree stmt_list)
+make_blocks (gimple_seq seq)
 {
-  tree_stmt_iterator i = tsi_start (stmt_list);
-  tree stmt = NULL;
+  gimple_stmt_iterator i = gsi_start (seq);
+  gimple stmt = NULL;
   bool start_new_block = true;
-  bool first_stmt_of_list = true;
+  bool first_stmt_of_seq = true;
   basic_block bb = ENTRY_BLOCK_PTR;
 
-  while (!tsi_end_p (i))
+  while (!gsi_end_p (i))
     {
-      tree prev_stmt;
+      gimple prev_stmt;
 
       prev_stmt = stmt;
-      stmt = tsi_stmt (i);
+      stmt = gsi_stmt (i);
 
       /* If the statement starts a new basic block or if we have determined
         in a previous pass that we need to create a new block for STMT, do
         so now.  */
       if (start_new_block || stmt_starts_bb_p (stmt, prev_stmt))
        {
-         if (!first_stmt_of_list)
-           stmt_list = tsi_split_statement_list_before (&i);
-         bb = create_basic_block (stmt_list, NULL, bb);
+         if (!first_stmt_of_seq)
+           seq = gsi_split_seq_before (&i);
+         bb = create_basic_block (seq, NULL, bb);
          start_new_block = false;
        }
 
       /* Now add STMT to BB and create the subgraphs for special statement
         codes.  */
-      set_bb_for_stmt (stmt, bb);
+      gimple_set_bb (stmt, bb);
 
       if (computed_goto_p (stmt))
        found_computed_goto = true;
@@ -363,10 +391,32 @@ make_blocks (tree stmt_list)
       /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
         next iteration.  */
       if (stmt_ends_bb_p (stmt))
-       start_new_block = true;
+       {
+         /* If the stmt can make abnormal goto use a new temporary
+            for the assignment to the LHS.  This makes sure the old value
+            of the LHS is available on the abnormal edge.  Otherwise
+            we will end up with overlapping life-ranges for abnormal
+            SSA names.  */
+         if (gimple_has_lhs (stmt)
+             && stmt_can_make_abnormal_goto (stmt)
+             && is_gimple_reg_type (TREE_TYPE (gimple_get_lhs (stmt))))
+           {
+             tree lhs = gimple_get_lhs (stmt);
+             tree tmp = create_tmp_var (TREE_TYPE (lhs), NULL);
+             gimple s = gimple_build_assign (lhs, tmp);
+             gimple_set_location (s, gimple_location (stmt));
+             gimple_set_block (s, gimple_block (stmt));
+             gimple_set_lhs (stmt, tmp);
+             if (TREE_CODE (TREE_TYPE (tmp)) == COMPLEX_TYPE
+                 || TREE_CODE (TREE_TYPE (tmp)) == VECTOR_TYPE)
+               DECL_GIMPLE_REG_P (tmp) = 1;
+             gsi_insert_after (&i, s, GSI_SAME_STMT);
+           }
+         start_new_block = true;
+       }
 
-      tsi_next (&i);
-      first_stmt_of_list = false;
+      gsi_next (&i);
+      first_stmt_of_seq = false;
     }
 }
 
@@ -387,8 +437,8 @@ create_bb (void *h, void *e, basic_block after)
 
   bb->index = last_basic_block;
   bb->flags = BB_NEW;
-  bb->il.tree = GGC_CNEW (struct tree_bb_info);
-  set_bb_stmt_list (bb, h ? (tree) h : alloc_stmt_list ());
+  bb->il.gimple = GGC_CNEW (struct gimple_bb_info);
+  set_bb_seq (bb, h ? (gimple_seq) h : gimple_seq_alloc ());
 
   /* Add the new block to the linked list of blocks.  */
   link_block (bb, after);
@@ -423,25 +473,32 @@ fold_cond_expr_cond (void)
 
   FOR_EACH_BB (bb)
     {
-      tree stmt = last_stmt (bb);
+      gimple stmt = last_stmt (bb);
 
-      if (stmt
-         && TREE_CODE (stmt) == COND_EXPR)
+      if (stmt && gimple_code (stmt) == GIMPLE_COND)
        {
+         location_t loc = gimple_location (stmt);
          tree cond;
          bool zerop, onep;
 
          fold_defer_overflow_warnings ();
-         cond = fold (COND_EXPR_COND (stmt));
-         zerop = integer_zerop (cond);
-         onep = integer_onep (cond);
+         cond = fold_binary_loc (loc, gimple_cond_code (stmt), boolean_type_node,
+                             gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
+         if (cond)
+           {
+             zerop = integer_zerop (cond);
+             onep = integer_onep (cond);
+           }
+         else
+           zerop = onep = false;
+
          fold_undefer_overflow_warnings (zerop || onep,
                                          stmt,
                                          WARN_STRICT_OVERFLOW_CONDITIONAL);
          if (zerop)
-           COND_EXPR_COND (stmt) = boolean_false_node;
+           gimple_cond_make_false (stmt);
          else if (onep)
-           COND_EXPR_COND (stmt) = boolean_true_node;
+           gimple_cond_make_true (stmt);
        }
     }
 }
@@ -461,40 +518,43 @@ make_edges (void)
   /* Traverse the basic block array placing edges.  */
   FOR_EACH_BB (bb)
     {
-      tree last = last_stmt (bb);
+      gimple last = last_stmt (bb);
       bool fallthru;
 
       if (last)
        {
-         enum tree_code code = TREE_CODE (last);
+         enum gimple_code code = gimple_code (last);
          switch (code)
            {
-           case GOTO_EXPR:
+           case GIMPLE_GOTO:
              make_goto_expr_edges (bb);
              fallthru = false;
              break;
-           case RETURN_EXPR:
+           case GIMPLE_RETURN:
              make_edge (bb, EXIT_BLOCK_PTR, 0);
              fallthru = false;
              break;
-           case COND_EXPR:
+           case GIMPLE_COND:
              make_cond_expr_edges (bb);
              fallthru = false;
              break;
-           case SWITCH_EXPR:
-             make_switch_expr_edges (bb);
+           case GIMPLE_SWITCH:
+             make_gimple_switch_edges (bb);
              fallthru = false;
              break;
-           case RESX_EXPR:
+           case GIMPLE_RESX:
              make_eh_edges (last);
              fallthru = false;
              break;
+           case GIMPLE_EH_DISPATCH:
+             fallthru = make_eh_dispatch_edges (last);
+             break;
 
-           case CALL_EXPR:
+           case GIMPLE_CALL:
              /* If this function receives a nonlocal goto, then we need to
                 make edges from this call site to all the nonlocal goto
                 handlers.  */
-             if (tree_can_make_abnormal_goto (last))
+             if (stmt_can_make_abnormal_goto (last))
                make_abnormal_goto_edges (bb, true);
 
              /* If this statement has reachable exception handlers, then
@@ -502,83 +562,79 @@ make_edges (void)
              make_eh_edges (last);
 
              /* Some calls are known not to return.  */
-             fallthru = !(call_expr_flags (last) & ECF_NORETURN);
+             fallthru = !(gimple_call_flags (last) & ECF_NORETURN);
              break;
 
-           case MODIFY_EXPR:
-             gcc_unreachable ();
-
-           case GIMPLE_MODIFY_STMT:
+           case GIMPLE_ASSIGN:
+              /* A GIMPLE_ASSIGN may throw internally and thus be considered
+                 control-altering. */
              if (is_ctrl_altering_stmt (last))
-               {
-                 /* A GIMPLE_MODIFY_STMT may have a CALL_EXPR on its RHS and
-                    the CALL_EXPR may have an abnormal edge.  Search the RHS
-                    for this case and create any required edges.  */
-                 if (tree_can_make_abnormal_goto (last))
-                   make_abnormal_goto_edges (bb, true);  
+               make_eh_edges (last);
+             fallthru = true;
+             break;
 
-                 make_eh_edges (last);
-               }
+           case GIMPLE_ASM:
+             make_gimple_asm_edges (bb);
              fallthru = true;
              break;
 
-           case OMP_PARALLEL:
-           case OMP_TASK:
-           case OMP_FOR:
-           case OMP_SINGLE:
-           case OMP_MASTER:
-           case OMP_ORDERED:
-           case OMP_CRITICAL:
-           case OMP_SECTION:
+           case GIMPLE_OMP_PARALLEL:
+           case GIMPLE_OMP_TASK:
+           case GIMPLE_OMP_FOR:
+           case GIMPLE_OMP_SINGLE:
+           case GIMPLE_OMP_MASTER:
+           case GIMPLE_OMP_ORDERED:
+           case GIMPLE_OMP_CRITICAL:
+           case GIMPLE_OMP_SECTION:
              cur_region = new_omp_region (bb, code, cur_region);
              fallthru = true;
              break;
 
-           case OMP_SECTIONS:
+           case GIMPLE_OMP_SECTIONS:
              cur_region = new_omp_region (bb, code, cur_region);
              fallthru = true;
              break;
 
-           case OMP_SECTIONS_SWITCH:
+           case GIMPLE_OMP_SECTIONS_SWITCH:
              fallthru = false;
              break;
 
-
-            case OMP_ATOMIC_LOAD:
-            case OMP_ATOMIC_STORE:
+            case GIMPLE_OMP_ATOMIC_LOAD:
+            case GIMPLE_OMP_ATOMIC_STORE:
                fallthru = true;
                break;
 
-
-           case OMP_RETURN:
-             /* In the case of an OMP_SECTION, the edge will go somewhere
-                other than the next block.  This will be created later.  */
+           case GIMPLE_OMP_RETURN:
+             /* In the case of a GIMPLE_OMP_SECTION, the edge will go
+                somewhere other than the next block.  This will be
+                created later.  */
              cur_region->exit = bb;
-             fallthru = cur_region->type != OMP_SECTION;
+             fallthru = cur_region->type != GIMPLE_OMP_SECTION;
              cur_region = cur_region->outer;
              break;
 
-           case OMP_CONTINUE:
+           case GIMPLE_OMP_CONTINUE:
              cur_region->cont = bb;
              switch (cur_region->type)
                {
-               case OMP_FOR:
-                 /* Mark all OMP_FOR and OMP_CONTINUE succs edges as abnormal
-                    to prevent splitting them.  */
+               case GIMPLE_OMP_FOR:
+                 /* Mark all GIMPLE_OMP_FOR and GIMPLE_OMP_CONTINUE
+                    succs edges as abnormal to prevent splitting
+                    them.  */
                  single_succ_edge (cur_region->entry)->flags |= EDGE_ABNORMAL;
                  /* Make the loopback edge.  */
                  make_edge (bb, single_succ (cur_region->entry),
                             EDGE_ABNORMAL);
 
-                 /* Create an edge from OMP_FOR to exit, which corresponds to
-                    the case that the body of the loop is not executed at
-                    all.  */
+                 /* Create an edge from GIMPLE_OMP_FOR to exit, which
+                    corresponds to the case that the body of the loop
+                    is not executed at all.  */
                  make_edge (cur_region->entry, bb->next_bb, EDGE_ABNORMAL);
                  make_edge (bb, bb->next_bb, EDGE_FALLTHRU | EDGE_ABNORMAL);
                  fallthru = false;
                  break;
 
-               case OMP_SECTIONS:
+               case GIMPLE_OMP_SECTIONS:
                  /* Wire up the edges into and out of the nested sections.  */
                  {
                    basic_block switch_bb = single_succ (cur_region->entry);
@@ -586,13 +642,13 @@ make_edges (void)
                    struct omp_region *i;
                    for (i = cur_region->inner; i ; i = i->next)
                      {
-                       gcc_assert (i->type == OMP_SECTION);
+                       gcc_assert (i->type == GIMPLE_OMP_SECTION);
                        make_edge (switch_bb, i->entry, 0);
                        make_edge (i->exit, bb, EDGE_FALLTHRU);
                      }
 
                    /* Make the loopback edge to the block with
-                      OMP_SECTIONS_SWITCH.  */
+                      GIMPLE_OMP_SECTIONS_SWITCH.  */
                    make_edge (bb, switch_bb, 0);
 
                    /* Make the edge from the switch to exit.  */
@@ -615,7 +671,11 @@ make_edges (void)
        fallthru = true;
 
       if (fallthru)
-       make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
+        {
+         make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
+         if (last)
+            assign_discriminator (gimple_location (last), bb->next_bb);
+       }
     }
 
   if (root_omp_region)
@@ -625,36 +685,136 @@ make_edges (void)
   fold_cond_expr_cond ();
 }
 
+/* Trivial hash function for a location_t.  ITEM is a pointer to
+   a hash table entry that maps a location_t to a discriminator.  */
+
+static unsigned int
+locus_map_hash (const void *item)
+{
+  return ((const struct locus_discrim_map *) item)->locus;
+}
+
+/* Equality function for the locus-to-discriminator map.  VA and VB
+   point to the two hash table entries to compare.  */
+
+static int
+locus_map_eq (const void *va, const void *vb)
+{
+  const struct locus_discrim_map *a = (const struct locus_discrim_map *) va;
+  const struct locus_discrim_map *b = (const struct locus_discrim_map *) vb;
+  return a->locus == b->locus;
+}
+
+/* Find the next available discriminator value for LOCUS.  The
+   discriminator distinguishes among several basic blocks that
+   share a common locus, allowing for more accurate sample-based
+   profiling.  */
+
+static int
+next_discriminator_for_locus (location_t locus)
+{
+  struct locus_discrim_map item;
+  struct locus_discrim_map **slot;
+
+  item.locus = locus;
+  item.discriminator = 0;
+  slot = (struct locus_discrim_map **)
+      htab_find_slot_with_hash (discriminator_per_locus, (void *) &item,
+                                (hashval_t) locus, INSERT);
+  gcc_assert (slot);
+  if (*slot == HTAB_EMPTY_ENTRY)
+    {
+      *slot = XNEW (struct locus_discrim_map);
+      gcc_assert (*slot);
+      (*slot)->locus = locus;
+      (*slot)->discriminator = 0;
+    }
+  (*slot)->discriminator++;
+  return (*slot)->discriminator;
+}
+
+/* Return TRUE if LOCUS1 and LOCUS2 refer to the same source line.  */
+
+static bool
+same_line_p (location_t locus1, location_t locus2)
+{
+  expanded_location from, to;
+
+  if (locus1 == locus2)
+    return true;
+
+  from = expand_location (locus1);
+  to = expand_location (locus2);
+
+  if (from.line != to.line)
+    return false;
+  if (from.file == to.file)
+    return true;
+  return (from.file != NULL
+          && to.file != NULL
+          && strcmp (from.file, to.file) == 0);
+}
+
+/* Assign a unique discriminator value to block BB if it begins at the same
+   LOCUS as its predecessor block.  */
 
-/* Create the edges for a COND_EXPR starting at block BB.
-   At this point, both clauses must contain only simple gotos.  */
+static void
+assign_discriminator (location_t locus, basic_block bb)
+{
+  gimple first_in_to_bb, last_in_to_bb;
+
+  if (locus == 0 || bb->discriminator != 0)
+    return;
+
+  first_in_to_bb = first_non_label_stmt (bb);
+  last_in_to_bb = last_stmt (bb);
+  if ((first_in_to_bb && same_line_p (locus, gimple_location (first_in_to_bb)))
+      || (last_in_to_bb && same_line_p (locus, gimple_location (last_in_to_bb))))
+    bb->discriminator = next_discriminator_for_locus (locus);
+}
+
+/* Create the edges for a GIMPLE_COND starting at block BB.  */
 
 static void
 make_cond_expr_edges (basic_block bb)
 {
-  tree entry = last_stmt (bb);
+  gimple entry = last_stmt (bb);
+  gimple then_stmt, else_stmt;
   basic_block then_bb, else_bb;
   tree then_label, else_label;
   edge e;
+  location_t entry_locus;
 
   gcc_assert (entry);
-  gcc_assert (TREE_CODE (entry) == COND_EXPR);
+  gcc_assert (gimple_code (entry) == GIMPLE_COND);
+
+  entry_locus = gimple_location (entry);
 
   /* Entry basic blocks for each component.  */
-  then_label = GOTO_DESTINATION (COND_EXPR_THEN (entry));
-  else_label = GOTO_DESTINATION (COND_EXPR_ELSE (entry));
+  then_label = gimple_cond_true_label (entry);
+  else_label = gimple_cond_false_label (entry);
   then_bb = label_to_block (then_label);
   else_bb = label_to_block (else_label);
+  then_stmt = first_stmt (then_bb);
+  else_stmt = first_stmt (else_bb);
 
   e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
-  e->goto_locus = EXPR_LOCATION (COND_EXPR_THEN (entry));
+  assign_discriminator (entry_locus, then_bb);
+  e->goto_locus = gimple_location (then_stmt);
+  if (e->goto_locus)
+    e->goto_block = gimple_block (then_stmt);
   e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
   if (e)
-    e->goto_locus = EXPR_LOCATION (COND_EXPR_ELSE (entry));
+    {
+      assign_discriminator (entry_locus, else_bb);
+      e->goto_locus = gimple_location (else_stmt);
+      if (e->goto_locus)
+       e->goto_block = gimple_block (else_stmt);
+    }
 
-  /* We do not need the gotos anymore.  */
-  COND_EXPR_THEN (entry) = NULL_TREE;
-  COND_EXPR_ELSE (entry) = NULL_TREE;
+  /* We do not need the labels anymore.  */
+  gimple_cond_set_true_label (entry, NULL_TREE);
+  gimple_cond_set_false_label (entry, NULL_TREE);
 }
 
 
@@ -714,11 +874,10 @@ end_recording_case_labels (void)
    Otherwise return NULL.  */
 
 static tree
-get_cases_for_edge (edge e, tree t)
+get_cases_for_edge (edge e, gimple t)
 {
   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.  */
@@ -733,11 +892,10 @@ get_cases_for_edge (edge e, tree t)
      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);
+  n = gimple_switch_num_labels (t);
   for (i = 0; i < n; i++)
     {
-      tree elt = TREE_VEC_ELT (vec, i);
+      tree elt = gimple_switch_label (t, i);
       tree lab = CASE_LABEL (elt);
       basic_block label_bb = label_to_block (lab);
       edge this_edge = find_edge (e->src, label_bb);
@@ -752,25 +910,25 @@ get_cases_for_edge (edge e, tree t)
   return (tree) *pointer_map_contains (edge_to_cases, e);
 }
 
-/* Create the edges for a SWITCH_EXPR starting at block BB.
-   At this point, the switch body has been lowered and the
-   SWITCH_LABELS filled in, so this is in effect a multi-way branch.  */
+/* Create the edges for a GIMPLE_SWITCH starting at block BB.  */
 
 static void
-make_switch_expr_edges (basic_block bb)
+make_gimple_switch_edges (basic_block bb)
 {
-  tree entry = last_stmt (bb);
+  gimple entry = last_stmt (bb);
+  location_t entry_locus;
   size_t i, n;
-  tree vec;
 
-  vec = SWITCH_LABELS (entry);
-  n = TREE_VEC_LENGTH (vec);
+  entry_locus = gimple_location (entry);
+
+  n = gimple_switch_num_labels (entry);
 
   for (i = 0; i < n; ++i)
     {
-      tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
+      tree lab = CASE_LABEL (gimple_switch_label (entry, i));
       basic_block label_bb = label_to_block (lab);
       make_edge (bb, label_bb, 0);
+      assign_discriminator (entry_locus, label_bb);
     }
 }
 
@@ -787,12 +945,11 @@ label_to_block_fn (struct function *ifun, tree dest)
      and undefined variable warnings quite right.  */
   if ((errorcount || sorrycount) && uid < 0)
     {
-      block_stmt_iterator bsi =
-       bsi_start (BASIC_BLOCK (NUM_FIXED_BLOCKS));
-      tree stmt;
+      gimple_stmt_iterator gsi = gsi_start_bb (BASIC_BLOCK (NUM_FIXED_BLOCKS));
+      gimple stmt;
 
-      stmt = build1 (LABEL_EXPR, void_type_node, dest);
-      bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
+      stmt = gimple_build_label (dest);
+      gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
       uid = LABEL_DECL_UID (dest);
     }
   if (VEC_length (basic_block, ifun->cfg->x_label_to_block_map)
@@ -808,17 +965,18 @@ void
 make_abnormal_goto_edges (basic_block bb, bool for_call)
 {
   basic_block target_bb;
-  block_stmt_iterator bsi;
+  gimple_stmt_iterator gsi;
 
   FOR_EACH_BB (target_bb)
-    for (bsi = bsi_start (target_bb); !bsi_end_p (bsi); bsi_next (&bsi))
+    for (gsi = gsi_start_bb (target_bb); !gsi_end_p (gsi); gsi_next (&gsi))
       {
-       tree target = bsi_stmt (bsi);
+       gimple label_stmt = gsi_stmt (gsi);
+       tree target;
 
-       if (TREE_CODE (target) != LABEL_EXPR)
+       if (gimple_code (label_stmt) != GIMPLE_LABEL)
          break;
 
-       target = LABEL_EXPR_LABEL (target);
+       target = gimple_label_label (label_stmt);
 
        /* Make an edge to every label block that has been marked as a
           potential target for a computed goto or a non-local goto.  */
@@ -836,16 +994,20 @@ make_abnormal_goto_edges (basic_block bb, bool for_call)
 static void
 make_goto_expr_edges (basic_block bb)
 {
-  block_stmt_iterator last = bsi_last (bb);
-  tree goto_t = bsi_stmt (last);
+  gimple_stmt_iterator last = gsi_last_bb (bb);
+  gimple goto_t = gsi_stmt (last);
 
   /* A simple GOTO creates normal edges.  */
   if (simple_goto_p (goto_t))
     {
-      tree dest = GOTO_DESTINATION (goto_t);
-      edge e = make_edge (bb, label_to_block (dest), EDGE_FALLTHRU);
-      e->goto_locus = EXPR_LOCATION (goto_t);
-      bsi_remove (&last, true);
+      tree dest = gimple_goto_dest (goto_t);
+      basic_block label_bb = label_to_block (dest);
+      edge e = make_edge (bb, label_bb, EDGE_FALLTHRU);
+      e->goto_locus = gimple_location (goto_t);
+      assign_discriminator (e->goto_locus, label_bb);
+      if (e->goto_locus)
+       e->goto_block = gimple_block (goto_t);
+      gsi_remove (&last, true);
       return;
     }
 
@@ -853,6 +1015,23 @@ make_goto_expr_edges (basic_block bb)
   make_abnormal_goto_edges (bb, false);
 }
 
+/* Create edges for an asm statement with labels at block BB.  */
+
+static void
+make_gimple_asm_edges (basic_block bb)
+{
+  gimple stmt = last_stmt (bb);
+  location_t stmt_loc = gimple_location (stmt);
+  int i, n = gimple_asm_nlabels (stmt);
+
+  for (i = 0; i < n; ++i)
+    {
+      tree label = TREE_VALUE (gimple_asm_label_op (stmt, i));
+      basic_block label_bb = label_to_block (label);
+      make_edge (bb, label_bb, 0);
+      assign_discriminator (stmt_loc, label_bb);
+    }
+}
 
 /*---------------------------------------------------------------------------
                               Flowgraph analysis
@@ -876,29 +1055,8 @@ static struct label_record
   bool used;
 } *label_for_bb;
 
-/* Callback for for_each_eh_region.  Helper for cleanup_dead_labels.  */
-static void
-update_eh_label (struct eh_region *region)
-{
-  tree old_label = get_eh_region_tree_label (region);
-  if (old_label)
-    {
-      tree new_label;
-      basic_block bb = label_to_block (old_label);
-
-      /* ??? After optimizing, there may be EH regions with labels
-        that have already been removed from the function body, so
-        there is no basic block for them.  */
-      if (! bb)
-       return;
-
-      new_label = label_for_bb[bb->index].label;
-      label_for_bb[bb->index].used = true;
-      set_eh_region_tree_label (region, new_label);
-    }
-}
-
 /* Given LABEL return the first label in the same basic block.  */
+
 static tree
 main_block_label (tree label)
 {
@@ -916,6 +1074,58 @@ main_block_label (tree label)
   return main_label;
 }
 
+/* Clean up redundant labels within the exception tree.  */
+
+static void
+cleanup_dead_labels_eh (void)
+{
+  eh_landing_pad lp;
+  eh_region r;
+  tree lab;
+  int i;
+
+  if (cfun->eh == NULL)
+    return;
+
+  for (i = 1; VEC_iterate (eh_landing_pad, cfun->eh->lp_array, i, lp); ++i)
+    if (lp && lp->post_landing_pad)
+      {
+       lab = main_block_label (lp->post_landing_pad);
+       if (lab != lp->post_landing_pad)
+         {
+           EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
+           EH_LANDING_PAD_NR (lab) = lp->index;
+         }
+      }
+
+  FOR_ALL_EH_REGION (r)
+    switch (r->type)
+      {
+      case ERT_CLEANUP:
+      case ERT_MUST_NOT_THROW:
+       break;
+
+      case ERT_TRY:
+       {
+         eh_catch c;
+         for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
+           {
+             lab = c->label;
+             if (lab)
+               c->label = main_block_label (lab);
+           }
+       }
+       break;
+
+      case ERT_ALLOWED_EXCEPTIONS:
+       lab = r->u.allowed.label;
+       if (lab)
+         r->u.allowed.label = main_block_label (lab);
+       break;
+      }
+}
+
+
 /* Cleanup redundant labels.  This is a three-step process:
      1) Find the leading label for each block.
      2) Redirect all references to labels to the leading labels.
@@ -931,16 +1141,17 @@ cleanup_dead_labels (void)
      label if there is one, or otherwise just the first label we see.  */
   FOR_EACH_BB (bb)
     {
-      block_stmt_iterator i;
+      gimple_stmt_iterator i;
 
-      for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
+      for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
        {
-         tree label, stmt = bsi_stmt (i);
+         tree label;
+         gimple stmt = gsi_stmt (i);
 
-         if (TREE_CODE (stmt) != LABEL_EXPR)
+         if (gimple_code (stmt) != GIMPLE_LABEL)
            break;
 
-         label = LABEL_EXPR_LABEL (stmt);
+         label = gimple_label_label (stmt);
 
          /* If we have not yet seen a label for the current block,
             remember this one and see if there are more labels.  */
@@ -966,68 +1177,75 @@ cleanup_dead_labels (void)
      First do so for each block ending in a control statement.  */
   FOR_EACH_BB (bb)
     {
-      tree stmt = last_stmt (bb);
+      gimple stmt = last_stmt (bb);
       if (!stmt)
        continue;
 
-      switch (TREE_CODE (stmt))
+      switch (gimple_code (stmt))
        {
-       case COND_EXPR:
+       case GIMPLE_COND:
          {
-           tree true_branch, false_branch;
+           tree true_label = gimple_cond_true_label (stmt);
+           tree false_label = gimple_cond_false_label (stmt);
 
-           true_branch = COND_EXPR_THEN (stmt);
-           false_branch = COND_EXPR_ELSE (stmt);
+           if (true_label)
+             gimple_cond_set_true_label (stmt, main_block_label (true_label));
+           if (false_label)
+             gimple_cond_set_false_label (stmt, main_block_label (false_label));
+           break;
+         }
 
-           if (true_branch)
-             GOTO_DESTINATION (true_branch)
-                     = main_block_label (GOTO_DESTINATION (true_branch));
-           if (false_branch)
-             GOTO_DESTINATION (false_branch)
-                     = main_block_label (GOTO_DESTINATION (false_branch));
+       case GIMPLE_SWITCH:
+         {
+           size_t i, n = gimple_switch_num_labels (stmt);
 
+           /* Replace all destination labels.  */
+           for (i = 0; i < n; ++i)
+             {
+               tree case_label = gimple_switch_label (stmt, i);
+               tree label = main_block_label (CASE_LABEL (case_label));
+               CASE_LABEL (case_label) = label;
+             }
            break;
          }
 
-       case SWITCH_EXPR:
+       case GIMPLE_ASM:
          {
-           size_t i;
-           tree vec = SWITCH_LABELS (stmt);
-           size_t n = TREE_VEC_LENGTH (vec);
+           int i, n = gimple_asm_nlabels (stmt);
 
-           /* Replace all destination labels.  */
            for (i = 0; i < n; ++i)
              {
-               tree elt = TREE_VEC_ELT (vec, i);
-               tree label = main_block_label (CASE_LABEL (elt));
-               CASE_LABEL (elt) = label;
+               tree cons = gimple_asm_label_op (stmt, i);
+               tree label = main_block_label (TREE_VALUE (cons));
+               TREE_VALUE (cons) = label;
              }
            break;
          }
 
-       /* We have to handle GOTO_EXPRs until they're removed, and we don't
+       /* We have to handle gotos until they're removed, and we don't
           remove them until after we've created the CFG edges.  */
-       case GOTO_EXPR:
-          if (! computed_goto_p (stmt))
+       case GIMPLE_GOTO:
+          if (!computed_goto_p (stmt))
            {
-             GOTO_DESTINATION (stmt)
-               = main_block_label (GOTO_DESTINATION (stmt));
-             break;
+             tree new_dest = main_block_label (gimple_goto_dest (stmt));
+             gimple_goto_set_dest (stmt, new_dest);
            }
+         break;
 
        default:
          break;
       }
     }
 
-  for_each_eh_region (update_eh_label);
+  /* Do the same for the exception region tree labels.  */
+  cleanup_dead_labels_eh ();
 
   /* Finally, purge dead labels.  All user-defined labels and labels that
      can be the target of non-local gotos and labels which have their
      address taken are preserved.  */
   FOR_EACH_BB (bb)
     {
-      block_stmt_iterator i;
+      gimple_stmt_iterator i;
       tree label_for_this_bb = label_for_bb[bb->index].label;
 
       if (!label_for_this_bb)
@@ -1037,22 +1255,23 @@ cleanup_dead_labels (void)
       if (!label_for_bb[bb->index].used)
        label_for_this_bb = NULL;
 
-      for (i = bsi_start (bb); !bsi_end_p (i); )
+      for (i = gsi_start_bb (bb); !gsi_end_p (i); )
        {
-         tree label, stmt = bsi_stmt (i);
+         tree label;
+         gimple stmt = gsi_stmt (i);
 
-         if (TREE_CODE (stmt) != LABEL_EXPR)
+         if (gimple_code (stmt) != GIMPLE_LABEL)
            break;
 
-         label = LABEL_EXPR_LABEL (stmt);
+         label = gimple_label_label (stmt);
 
          if (label == label_for_this_bb
-             || ! DECL_ARTIFICIAL (label)
+             || !DECL_ARTIFICIAL (label)
              || DECL_NONLOCAL (label)
              || FORCED_LABEL (label))
-           bsi_next (&i);
+           gsi_next (&i);
          else
-           bsi_remove (&i, true);
+           gsi_remove (&i, true);
        }
     }
 
@@ -1071,32 +1290,37 @@ group_case_labels (void)
 
   FOR_EACH_BB (bb)
     {
-      tree stmt = last_stmt (bb);
-      if (stmt && TREE_CODE (stmt) == SWITCH_EXPR)
+      gimple stmt = last_stmt (bb);
+      if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
        {
-         tree labels = SWITCH_LABELS (stmt);
-         int old_size = TREE_VEC_LENGTH (labels);
+         int old_size = gimple_switch_num_labels (stmt);
          int i, j, new_size = old_size;
          tree default_case = NULL_TREE;
          tree default_label = NULL_TREE;
+         bool has_default;
 
-         /* The default label is always the last case in a switch
+         /* The default label is always the first case in a switch
             statement after gimplification if it was not optimized
-            away */
-         if (!CASE_LOW (TREE_VEC_ELT (labels, old_size - 1))
-             && !CASE_HIGH (TREE_VEC_ELT (labels, old_size - 1)))
+            away */
+         if (!CASE_LOW (gimple_switch_default_label (stmt))
+             && !CASE_HIGH (gimple_switch_default_label (stmt)))
            {
-             default_case = TREE_VEC_ELT (labels, old_size - 1);
+             default_case = gimple_switch_default_label (stmt);
              default_label = CASE_LABEL (default_case);
-             old_size--;
+             has_default = true;
            }
+         else
+           has_default = false;
 
          /* Look for possible opportunities to merge cases.  */
-          i = 0;
+         if (has_default)
+           i = 1;
+         else
+           i = 0;
          while (i < old_size)
            {
              tree base_case, base_label, base_high;
-             base_case = TREE_VEC_ELT (labels, i);
+             base_case = gimple_switch_label (stmt, i);
 
              gcc_assert (base_case);
              base_label = CASE_LABEL (base_case);
@@ -1105,21 +1329,23 @@ group_case_labels (void)
                 default case.  */
              if (base_label == default_label)
                {
-                 TREE_VEC_ELT (labels, i) = NULL_TREE;
+                 gimple_switch_set_label (stmt, i, NULL_TREE);
                  i++;
                  new_size--;
                  continue;
                }
 
-             base_high = CASE_HIGH (base_case) ?
-               CASE_HIGH (base_case) : 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)
                {
-                 tree merge_case = TREE_VEC_ELT (labels, i);
+                 tree merge_case = gimple_switch_label (stmt, i);
                  tree merge_label = CASE_LABEL (merge_case);
                  tree t = int_const_binop (PLUS_EXPR, base_high,
                                            integer_one_node, 1);
@@ -1132,7 +1358,7 @@ group_case_labels (void)
                      base_high = CASE_HIGH (merge_case) ?
                        CASE_HIGH (merge_case) : CASE_LOW (merge_case);
                      CASE_HIGH (base_case) = base_high;
-                     TREE_VEC_ELT (labels, i) = NULL_TREE;
+                     gimple_switch_set_label (stmt, i, NULL_TREE);
                      new_size--;
                      i++;
                    }
@@ -1145,11 +1371,14 @@ group_case_labels (void)
             length of the vector.  */
          for (i = 0, j = 0; i < new_size; i++)
            {
-             while (! TREE_VEC_ELT (labels, j))
+             while (! gimple_switch_label (stmt, j))
                j++;
-             TREE_VEC_ELT (labels, i) = TREE_VEC_ELT (labels, j++);
+             gimple_switch_set_label (stmt, i,
+                                      gimple_switch_label (stmt, j++));
            }
-         TREE_VEC_LENGTH (labels) = new_size;
+
+         gcc_assert (new_size <= old_size);
+         gimple_switch_set_num_labels (stmt, new_size);
        }
     }
 }
@@ -1157,16 +1386,16 @@ group_case_labels (void)
 /* Checks whether we can merge block B into block A.  */
 
 static bool
-tree_can_merge_blocks_p (basic_block a, basic_block b)
+gimple_can_merge_blocks_p (basic_block a, basic_block b)
 {
-  const_tree stmt;
-  block_stmt_iterator bsi;
-  tree phi;
+  gimple stmt;
+  gimple_stmt_iterator gsi;
+  gimple_seq phis;
 
   if (!single_succ_p (a))
     return false;
 
-  if (single_succ_edge (a)->flags & EDGE_ABNORMAL)
+  if (single_succ_edge (a)->flags & (EDGE_ABNORMAL | EDGE_EH))
     return false;
 
   if (single_succ (a) != b)
@@ -1180,51 +1409,103 @@ tree_can_merge_blocks_p (basic_block a, basic_block b)
 
   /* If A ends by a statement causing exceptions or something similar, we
      cannot merge the blocks.  */
-  /* This CONST_CAST is okay because last_stmt doesn't modify its
-     argument and the return value is assign to a const_tree.  */
-  stmt = last_stmt (CONST_CAST_BB (a));
+  stmt = last_stmt (a);
   if (stmt && stmt_ends_bb_p (stmt))
     return false;
 
   /* Do not allow a block with only a non-local label to be merged.  */
-  if (stmt && TREE_CODE (stmt) == LABEL_EXPR
-      && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
+  if (stmt
+      && gimple_code (stmt) == GIMPLE_LABEL
+      && DECL_NONLOCAL (gimple_label_label (stmt)))
+    return false;
+
+  /* Examine the labels at the beginning of B.  */
+  for (gsi = gsi_start_bb (b); !gsi_end_p (gsi); gsi_next (&gsi))
+    {
+      tree lab;
+      stmt = gsi_stmt (gsi);
+      if (gimple_code (stmt) != GIMPLE_LABEL)
+       break;
+      lab = gimple_label_label (stmt);
+
+      /* Do not remove user labels.  */
+      if (!DECL_ARTIFICIAL (lab))
+       return false;
+    }
+
+  /* Protect the loop latches.  */
+  if (current_loops && b->loop_father->latch == b)
     return false;
 
   /* It must be possible to eliminate all phi nodes in B.  If ssa form
      is not up-to-date, we cannot eliminate any phis; however, if only
      some symbols as whole are marked for renaming, this is not a problem,
      as phi nodes for those symbols are irrelevant in updating anyway.  */
-  phi = phi_nodes (b);
-  if (phi)
+  phis = phi_nodes (b);
+  if (!gimple_seq_empty_p (phis))
     {
+      gimple_stmt_iterator i;
+
       if (name_mappings_registered_p ())
        return false;
 
-      for (; phi; phi = PHI_CHAIN (phi))
-       if (!is_gimple_reg (PHI_RESULT (phi))
-           && !may_propagate_copy (PHI_RESULT (phi), PHI_ARG_DEF (phi, 0)))
-         return false;
-    }
+      for (i = gsi_start (phis); !gsi_end_p (i); gsi_next (&i))
+       {
+         gimple phi = gsi_stmt (i);
 
-  /* Do not remove user labels.  */
-  for (bsi = bsi_start (b); !bsi_end_p (bsi); bsi_next (&bsi))
-    {
-      stmt = bsi_stmt (bsi);
-      if (TREE_CODE (stmt) != LABEL_EXPR)
-       break;
-      if (!DECL_ARTIFICIAL (LABEL_EXPR_LABEL (stmt)))
-       return false;
+         if (!is_gimple_reg (gimple_phi_result (phi))
+             && !may_propagate_copy (gimple_phi_result (phi),
+                                     gimple_phi_arg_def (phi, 0)))
+           return false;
+       }
     }
 
-  /* Protect the loop latches.  */
-  if (current_loops
-      && b->loop_father->latch == b)
-    return false;
+  return true;
+}
+
+/* Return true if the var whose chain of uses starts at PTR has no
+   nondebug uses.  */
+bool
+has_zero_uses_1 (const ssa_use_operand_t *head)
+{
+  const ssa_use_operand_t *ptr;
+
+  for (ptr = head->next; ptr != head; ptr = ptr->next)
+    if (!is_gimple_debug (USE_STMT (ptr)))
+      return false;
 
   return true;
 }
 
+/* Return true if the var whose chain of uses starts at PTR has a
+   single nondebug use.  Set USE_P and STMT to that single nondebug
+   use, if so, or to NULL otherwise.  */
+bool
+single_imm_use_1 (const ssa_use_operand_t *head,
+                 use_operand_p *use_p, gimple *stmt)
+{
+  ssa_use_operand_t *ptr, *single_use = 0;
+
+  for (ptr = head->next; ptr != head; ptr = ptr->next)
+    if (!is_gimple_debug (USE_STMT (ptr)))
+      {
+       if (single_use)
+         {
+           single_use = NULL;
+           break;
+         }
+       single_use = ptr;
+      }
+
+  if (use_p)
+    *use_p = single_use;
+
+  if (stmt)
+    *stmt = single_use ? single_use->loc.stmt : NULL;
+
+  return !!single_use;
+}
+
 /* Replaces all uses of NAME by VAL.  */
 
 void
@@ -1232,21 +1513,18 @@ replace_uses_by (tree name, tree val)
 {
   imm_use_iterator imm_iter;
   use_operand_p use;
-  tree stmt;
+  gimple stmt;
   edge e;
 
   FOR_EACH_IMM_USE_STMT (stmt, imm_iter, name)
     {
-      if (TREE_CODE (stmt) != PHI_NODE)
-       push_stmt_changes (&stmt);
-
       FOR_EACH_IMM_USE_ON_STMT (use, imm_iter)
         {
          replace_exp (use, val);
 
-         if (TREE_CODE (stmt) == PHI_NODE)
+         if (gimple_code (stmt) == GIMPLE_PHI)
            {
-             e = PHI_ARG_EDGE (stmt, PHI_ARG_INDEX_FROM_USE (use));
+             e = gimple_phi_arg_edge (stmt, PHI_ARG_INDEX_FROM_USE (use));
              if (e->flags & EDGE_ABNORMAL)
                {
                  /* This can only occur for virtual operands, since
@@ -1258,22 +1536,27 @@ replace_uses_by (tree name, tree val)
            }
        }
 
-      if (TREE_CODE (stmt) != PHI_NODE)
+      if (gimple_code (stmt) != GIMPLE_PHI)
        {
-         tree rhs;
+         size_t i;
 
          fold_stmt_inplace (stmt);
          if (cfgcleanup_altered_bbs)
-           bitmap_set_bit (cfgcleanup_altered_bbs, bb_for_stmt (stmt)->index);
+           bitmap_set_bit (cfgcleanup_altered_bbs, gimple_bb (stmt)->index);
 
-         /* FIXME.  This should go in pop_stmt_changes.  */
-         rhs = get_rhs (stmt);
-         if (TREE_CODE (rhs) == ADDR_EXPR)
-           recompute_tree_invariant_for_addr_expr (rhs);
+         /* FIXME.  This should go in update_stmt.  */
+         for (i = 0; i < gimple_num_ops (stmt); i++)
+           {
+             tree op = gimple_op (stmt, i);
+              /* Operands may be empty here.  For example, the labels
+                 of a GIMPLE_COND are nulled out following the creation
+                 of the corresponding CFG edges.  */
+             if (op && TREE_CODE (op) == ADDR_EXPR)
+               recompute_tree_invariant_for_addr_expr (op);
+           }
 
          maybe_clean_or_replace_eh_stmt (stmt, stmt);
-
-         pop_stmt_changes (&stmt);
+         update_stmt (stmt);
        }
     }
 
@@ -1295,23 +1578,24 @@ replace_uses_by (tree name, tree val)
 /* Merge block B into block A.  */
 
 static void
-tree_merge_blocks (basic_block a, basic_block b)
+gimple_merge_blocks (basic_block a, basic_block b)
 {
-  block_stmt_iterator bsi;
-  tree_stmt_iterator last;
-  tree phi;
+  gimple_stmt_iterator last, gsi, psi;
+  gimple_seq phis = phi_nodes (b);
 
   if (dump_file)
     fprintf (dump_file, "Merging blocks %d and %d\n", a->index, b->index);
 
   /* Remove all single-valued PHI nodes from block B of the form
      V_i = PHI <V_j> by propagating V_j to all the uses of V_i.  */
-  bsi = bsi_last (a);
-  for (phi = phi_nodes (b); phi; phi = phi_nodes (b))
+  gsi = gsi_last_bb (a);
+  for (psi = gsi_start (phis); !gsi_end_p (psi); )
     {
-      tree def = PHI_RESULT (phi), use = PHI_ARG_DEF (phi, 0);
-      tree copy;
-      bool may_replace_uses = may_propagate_copy (def, use);
+      gimple phi = gsi_stmt (psi);
+      tree def = gimple_phi_result (phi), use = gimple_phi_arg_def (phi, 0);
+      gimple copy;
+      bool may_replace_uses = !is_gimple_reg (def)
+                             || may_propagate_copy (def, use);
 
       /* In case we maintain loop closed ssa form, do not propagate arguments
         of loop exit phi nodes.  */
@@ -1330,10 +1614,9 @@ tree_merge_blocks (basic_block a, basic_block b)
             with ordering of phi nodes.  This is because A is the single
             predecessor of B, therefore results of the phi nodes cannot
             appear as arguments of the phi nodes.  */
-         copy = build_gimple_modify_stmt (def, use);
-         bsi_insert_after (&bsi, copy, BSI_NEW_STMT);
-         SSA_NAME_DEF_STMT (def) = copy;
-          remove_phi_node (phi, NULL, false);
+         copy = gimple_build_assign (def, use);
+         gsi_insert_after (&gsi, copy, GSI_NEW_STMT);
+          remove_phi_node (&psi, false);
        }
       else
         {
@@ -1344,7 +1627,7 @@ tree_merge_blocks (basic_block a, basic_block b)
            {
              imm_use_iterator iter;
              use_operand_p use_p;
-             tree stmt;
+             gimple stmt;
 
              FOR_EACH_IMM_USE_STMT (stmt, iter, def)
                FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
@@ -1352,7 +1635,8 @@ tree_merge_blocks (basic_block a, basic_block b)
            }
          else
             replace_uses_by (def, use);
-          remove_phi_node (phi, NULL, true);
+
+          remove_phi_node (&psi, true);
         }
     }
 
@@ -1362,37 +1646,47 @@ tree_merge_blocks (basic_block a, basic_block b)
   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);)
+  /* Remove labels from B and set gimple_bb to A for other statements.  */
+  for (gsi = gsi_start_bb (b); !gsi_end_p (gsi);)
     {
-      if (TREE_CODE (bsi_stmt (bsi)) == LABEL_EXPR)
+      gimple stmt = gsi_stmt (gsi);
+      if (gimple_code (stmt) == GIMPLE_LABEL)
        {
-         tree label = bsi_stmt (bsi);
+         tree label = gimple_label_label (stmt);
+         int lp_nr;
+
+         gsi_remove (&gsi, false);
 
-         bsi_remove (&bsi, false);
          /* 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)))
+         if (FORCED_LABEL (label))
+           {
+             gimple_stmt_iterator dest_gsi = gsi_start_bb (a);
+             gsi_insert_before (&dest_gsi, stmt, GSI_NEW_STMT);
+           }
+
+         lp_nr = EH_LANDING_PAD_NR (label);
+         if (lp_nr)
            {
-             block_stmt_iterator dest_bsi = bsi_start (a);
-             bsi_insert_before (&dest_bsi, label, BSI_NEW_STMT);
+             eh_landing_pad lp = get_eh_landing_pad_from_number (lp_nr);
+             lp->post_landing_pad = NULL;
            }
        }
       else
        {
-         change_bb_for_stmt (bsi_stmt (bsi), a);
-         bsi_next (&bsi);
+         gimple_set_bb (stmt, a);
+         gsi_next (&gsi);
        }
     }
 
-  /* Merge the chains.  */
-  last = tsi_last (bb_stmt_list (a));
-  tsi_link_after (&last, bb_stmt_list (b), TSI_NEW_STMT);
-  set_bb_stmt_list (b, NULL_TREE);
+  /* Merge the sequences.  */
+  last = gsi_last_bb (a);
+  gsi_insert_seq_after (&last, bb_seq (b), GSI_NEW_STMT);
+  set_bb_seq (b, NULL);
 
   if (cfgcleanup_altered_bbs)
     bitmap_set_bit (cfgcleanup_altered_bbs, a->index);
@@ -1400,7 +1694,7 @@ tree_merge_blocks (basic_block a, basic_block b)
 
 
 /* Return the one of two successors of BB that is not reachable by a
-   reached by a complex edge, if there is one.  Else, return BB.  We use
+   complex edge, if there is one.  Else, return BB.  We use
    this in optimizations that use post-dominators for their heuristics,
    to catch the cases in C++ where function calls are involved.  */
 
@@ -1421,635 +1715,65 @@ single_noncomplex_succ (basic_block bb)
   return bb;
 }
 
+/* T is CALL_EXPR.  Set current_function_calls_* flags.  */
 
-/* Walk the function tree removing unnecessary statements.
-
-     * Empty statement nodes are removed
-
-     * Unnecessary TRY_FINALLY and TRY_CATCH blocks are removed
-
-     * Unnecessary COND_EXPRs are removed
+void
+notice_special_calls (gimple call)
+{
+  int flags = gimple_call_flags (call);
 
-     * Some unnecessary BIND_EXPRs are removed
+  if (flags & ECF_MAY_BE_ALLOCA)
+    cfun->calls_alloca = true;
+  if (flags & ECF_RETURNS_TWICE)
+    cfun->calls_setjmp = true;
+}
 
-   Clearly more work could be done.  The trick is doing the analysis
-   and removal fast enough to be a net improvement in compile times.
 
-   Note that when we remove a control structure such as a COND_EXPR
-   BIND_EXPR, or TRY block, we will need to repeat this optimization pass
-   to ensure we eliminate all the useless code.  */
+/* Clear flags set by notice_special_calls.  Used by dead code removal
+   to update the flags.  */
 
-struct rus_data
+void
+clear_special_calls (void)
 {
-  tree *last_goto;
-  bool repeat;
-  bool may_throw;
-  bool may_branch;
-  bool has_label;
-};
+  cfun->calls_alloca = false;
+  cfun->calls_setjmp = false;
+}
 
-static void remove_useless_stmts_1 (tree *, struct rus_data *);
+/* Remove PHI nodes associated with basic block BB and all edges out of BB.  */
 
-static bool
-remove_useless_stmts_warn_notreached (tree stmt)
+static void
+remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
 {
-  if (EXPR_HAS_LOCATION (stmt))
-    {
-      location_t loc = EXPR_LOCATION (stmt);
-      if (LOCATION_LINE (loc) > 0)
-       {
-         warning (OPT_Wunreachable_code, "%Hwill never be executed", &loc);
-         return true;
-       }
-    }
-
-  switch (TREE_CODE (stmt))
-    {
-    case STATEMENT_LIST:
-      {
-       tree_stmt_iterator i;
-       for (i = tsi_start (stmt); !tsi_end_p (i); tsi_next (&i))
-         if (remove_useless_stmts_warn_notreached (tsi_stmt (i)))
-           return true;
-      }
-      break;
-
-    case COND_EXPR:
-      if (remove_useless_stmts_warn_notreached (COND_EXPR_COND (stmt)))
-       return true;
-      if (remove_useless_stmts_warn_notreached (COND_EXPR_THEN (stmt)))
-       return true;
-      if (remove_useless_stmts_warn_notreached (COND_EXPR_ELSE (stmt)))
-       return true;
-      break;
-
-    case TRY_FINALLY_EXPR:
-    case TRY_CATCH_EXPR:
-      if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 0)))
-       return true;
-      if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 1)))
-       return true;
-      break;
+  /* Since this block is no longer reachable, we can just delete all
+     of its PHI nodes.  */
+  remove_phi_nodes (bb);
 
-    case CATCH_EXPR:
-      return remove_useless_stmts_warn_notreached (CATCH_BODY (stmt));
-    case EH_FILTER_EXPR:
-      return remove_useless_stmts_warn_notreached (EH_FILTER_FAILURE (stmt));
-    case BIND_EXPR:
-      return remove_useless_stmts_warn_notreached (BIND_EXPR_BLOCK (stmt));
+  /* Remove edges to BB's successors.  */
+  while (EDGE_COUNT (bb->succs) > 0)
+    remove_edge (EDGE_SUCC (bb, 0));
+}
 
-    default:
-      /* Not a live container.  */
-      break;
-    }
 
-  return false;
-}
+/* Remove statements of basic block BB.  */
 
 static void
-remove_useless_stmts_cond (tree *stmt_p, struct rus_data *data)
+remove_bb (basic_block bb)
 {
-  tree then_clause, else_clause, cond;
-  bool save_has_label, then_has_label, else_has_label;
-
-  save_has_label = data->has_label;
-  data->has_label = false;
-  data->last_goto = NULL;
-
-  remove_useless_stmts_1 (&COND_EXPR_THEN (*stmt_p), data);
+  gimple_stmt_iterator i;
 
-  then_has_label = data->has_label;
-  data->has_label = false;
-  data->last_goto = NULL;
-
-  remove_useless_stmts_1 (&COND_EXPR_ELSE (*stmt_p), data);
-
-  else_has_label = data->has_label;
-  data->has_label = save_has_label | then_has_label | else_has_label;
-
-  then_clause = COND_EXPR_THEN (*stmt_p);
-  else_clause = COND_EXPR_ELSE (*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))
+  if (dump_file)
     {
-      *stmt_p = build_empty_stmt ();
-      data->repeat = true;
+      fprintf (dump_file, "Removing basic block %d\n", bb->index);
+      if (dump_flags & TDF_DETAILS)
+       {
+         dump_bb (bb, dump_file, 0);
+         fprintf (dump_file, "\n");
+       }
     }
 
-  /* If there are no reachable statements in an arm, then we can
-     zap the entire conditional.  */
-  else if (integer_nonzerop (cond) && !else_has_label)
-    {
-      if (warn_notreached)
-       remove_useless_stmts_warn_notreached (else_clause);
-      *stmt_p = then_clause;
-      data->repeat = true;
-    }
-  else if (integer_zerop (cond) && !then_has_label)
+  if (current_loops)
     {
-      if (warn_notreached)
-       remove_useless_stmts_warn_notreached (then_clause);
-      *stmt_p = else_clause;
-      data->repeat = true;
-    }
-
-  /* Check a couple of simple things on then/else with single stmts.  */
-  else
-    {
-      tree then_stmt = expr_only (then_clause);
-      tree else_stmt = expr_only (else_clause);
-
-      /* Notice branches to a common destination.  */
-      if (then_stmt && else_stmt
-         && TREE_CODE (then_stmt) == GOTO_EXPR
-         && TREE_CODE (else_stmt) == GOTO_EXPR
-         && (GOTO_DESTINATION (then_stmt) == GOTO_DESTINATION (else_stmt)))
-       {
-         *stmt_p = then_stmt;
-         data->repeat = true;
-       }
-
-      /* If the THEN/ELSE clause merely assigns a value to a variable or
-        parameter which is already known to contain that value, then
-        remove the useless THEN/ELSE clause.  */
-      else if (TREE_CODE (cond) == VAR_DECL || TREE_CODE (cond) == PARM_DECL)
-       {
-         if (else_stmt
-             && TREE_CODE (else_stmt) == GIMPLE_MODIFY_STMT
-             && GIMPLE_STMT_OPERAND (else_stmt, 0) == cond
-             && integer_zerop (GIMPLE_STMT_OPERAND (else_stmt, 1)))
-           COND_EXPR_ELSE (*stmt_p) = alloc_stmt_list ();
-       }
-      else if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
-              && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
-                  || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL)
-              && TREE_CONSTANT (TREE_OPERAND (cond, 1)))
-       {
-         tree stmt = (TREE_CODE (cond) == EQ_EXPR
-                      ? then_stmt : else_stmt);
-         tree *location = (TREE_CODE (cond) == EQ_EXPR
-                           ? &COND_EXPR_THEN (*stmt_p)
-                           : &COND_EXPR_ELSE (*stmt_p));
-
-         if (stmt
-             && TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
-             && GIMPLE_STMT_OPERAND (stmt, 0) == TREE_OPERAND (cond, 0)
-             && GIMPLE_STMT_OPERAND (stmt, 1) == TREE_OPERAND (cond, 1))
-           *location = alloc_stmt_list ();
-       }
-    }
-
-  /* Protect GOTOs in the arm of COND_EXPRs from being removed.  They
-     would be re-introduced during lowering.  */
-  data->last_goto = NULL;
-}
-
-
-static void
-remove_useless_stmts_tf (tree *stmt_p, struct rus_data *data)
-{
-  bool save_may_branch, save_may_throw;
-  bool this_may_branch, this_may_throw;
-
-  /* Collect may_branch and may_throw information for the body only.  */
-  save_may_branch = data->may_branch;
-  save_may_throw = data->may_throw;
-  data->may_branch = false;
-  data->may_throw = false;
-  data->last_goto = NULL;
-
-  remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data);
-
-  this_may_branch = data->may_branch;
-  this_may_throw = data->may_throw;
-  data->may_branch |= save_may_branch;
-  data->may_throw |= save_may_throw;
-  data->last_goto = NULL;
-
-  remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data);
-
-  /* If the body is empty, then we can emit the FINALLY block without
-     the enclosing TRY_FINALLY_EXPR.  */
-  if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 0)))
-    {
-      *stmt_p = TREE_OPERAND (*stmt_p, 1);
-      data->repeat = true;
-    }
-
-  /* If the handler is empty, then we can emit the TRY block without
-     the enclosing TRY_FINALLY_EXPR.  */
-  else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1)))
-    {
-      *stmt_p = TREE_OPERAND (*stmt_p, 0);
-      data->repeat = true;
-    }
-
-  /* If the body neither throws, nor branches, then we can safely
-     string the TRY and FINALLY blocks together.  */
-  else if (!this_may_branch && !this_may_throw)
-    {
-      tree stmt = *stmt_p;
-      *stmt_p = TREE_OPERAND (stmt, 0);
-      append_to_statement_list (TREE_OPERAND (stmt, 1), stmt_p);
-      data->repeat = true;
-    }
-}
-
-
-static void
-remove_useless_stmts_tc (tree *stmt_p, struct rus_data *data)
-{
-  bool save_may_throw, this_may_throw;
-  tree_stmt_iterator i;
-  tree stmt;
-
-  /* Collect may_throw information for the body only.  */
-  save_may_throw = data->may_throw;
-  data->may_throw = false;
-  data->last_goto = NULL;
-
-  remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data);
-
-  this_may_throw = data->may_throw;
-  data->may_throw = save_may_throw;
-
-  /* If the body cannot throw, then we can drop the entire TRY_CATCH_EXPR.  */
-  if (!this_may_throw)
-    {
-      if (warn_notreached)
-       remove_useless_stmts_warn_notreached (TREE_OPERAND (*stmt_p, 1));
-      *stmt_p = TREE_OPERAND (*stmt_p, 0);
-      data->repeat = true;
-      return;
-    }
-
-  /* Process the catch clause specially.  We may be able to tell that
-     no exceptions propagate past this point.  */
-
-  this_may_throw = true;
-  i = tsi_start (TREE_OPERAND (*stmt_p, 1));
-  stmt = tsi_stmt (i);
-  data->last_goto = NULL;
-
-  switch (TREE_CODE (stmt))
-    {
-    case CATCH_EXPR:
-      for (; !tsi_end_p (i); tsi_next (&i))
-       {
-         stmt = tsi_stmt (i);
-         /* If we catch all exceptions, then the body does not
-            propagate exceptions past this point.  */
-         if (CATCH_TYPES (stmt) == NULL)
-           this_may_throw = false;
-         data->last_goto = NULL;
-         remove_useless_stmts_1 (&CATCH_BODY (stmt), data);
-       }
-      break;
-
-    case EH_FILTER_EXPR:
-      if (EH_FILTER_MUST_NOT_THROW (stmt))
-       this_may_throw = false;
-      else if (EH_FILTER_TYPES (stmt) == NULL)
-       this_may_throw = false;
-      remove_useless_stmts_1 (&EH_FILTER_FAILURE (stmt), data);
-      break;
-
-    default:
-      /* Otherwise this is a cleanup.  */
-      remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data);
-
-      /* If the cleanup is empty, then we can emit the TRY block without
-        the enclosing TRY_CATCH_EXPR.  */
-      if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1)))
-       {
-         *stmt_p = TREE_OPERAND (*stmt_p, 0);
-         data->repeat = true;
-       }
-      break;
-    }
-  data->may_throw |= this_may_throw;
-}
-
-
-static void
-remove_useless_stmts_bind (tree *stmt_p, struct rus_data *data)
-{
-  tree block;
-
-  /* First remove anything underneath the BIND_EXPR.  */
-  remove_useless_stmts_1 (&BIND_EXPR_BODY (*stmt_p), data);
-
-  /* If the BIND_EXPR has no variables, then we can pull everything
-     up one level and remove the BIND_EXPR, unless this is the toplevel
-     BIND_EXPR for the current function or an inlined function.
-
-     When this situation occurs we will want to apply this
-     optimization again.  */
-  block = BIND_EXPR_BLOCK (*stmt_p);
-  if (BIND_EXPR_VARS (*stmt_p) == NULL_TREE
-      && *stmt_p != DECL_SAVED_TREE (current_function_decl)
-      && (! block
-         || ! BLOCK_ABSTRACT_ORIGIN (block)
-         || (TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block))
-             != FUNCTION_DECL)))
-    {
-      *stmt_p = BIND_EXPR_BODY (*stmt_p);
-      data->repeat = true;
-    }
-}
-
-
-static void
-remove_useless_stmts_goto (tree *stmt_p, struct rus_data *data)
-{
-  tree dest = GOTO_DESTINATION (*stmt_p);
-
-  data->may_branch = true;
-  data->last_goto = NULL;
-
-  /* Record the last goto expr, so that we can delete it if unnecessary.  */
-  if (TREE_CODE (dest) == LABEL_DECL)
-    data->last_goto = stmt_p;
-}
-
-
-static void
-remove_useless_stmts_label (tree *stmt_p, struct rus_data *data)
-{
-  tree label = LABEL_EXPR_LABEL (*stmt_p);
-
-  data->has_label = true;
-
-  /* We do want to jump across non-local label receiver code.  */
-  if (DECL_NONLOCAL (label))
-    data->last_goto = NULL;
-
-  else if (data->last_goto && GOTO_DESTINATION (*data->last_goto) == label)
-    {
-      *data->last_goto = build_empty_stmt ();
-      data->repeat = true;
-    }
-
-  /* ??? Add something here to delete unused labels.  */
-}
-
-
-/* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
-   decl.  This allows us to eliminate redundant or useless
-   calls to "const" functions.
-
-   Gimplifier already does the same operation, but we may notice functions
-   being const and pure once their calls has been gimplified, so we need
-   to update the flag.  */
-
-static void
-update_call_expr_flags (tree call)
-{
-  tree decl = get_callee_fndecl (call);
-  int flags;
-  if (!decl)
-    return;
-  flags = call_expr_flags (call);
-  if (flags & (ECF_CONST | ECF_PURE) && !(flags & ECF_LOOPING_CONST_OR_PURE))
-    TREE_SIDE_EFFECTS (call) = 0;
-  if (TREE_NOTHROW (decl))
-    TREE_NOTHROW (call) = 1;
-}
-
-
-/* T is CALL_EXPR.  Set current_function_calls_* flags.  */
-
-void
-notice_special_calls (tree t)
-{
-  int flags = call_expr_flags (t);
-
-  if (flags & ECF_MAY_BE_ALLOCA)
-    cfun->calls_alloca = true;
-  if (flags & ECF_RETURNS_TWICE)
-    cfun->calls_setjmp = true;
-}
-
-
-/* Clear flags set by notice_special_calls.  Used by dead code removal
-   to update the flags.  */
-
-void
-clear_special_calls (void)
-{
-  cfun->calls_alloca = false;
-  cfun->calls_setjmp = false;
-}
-
-
-static void
-remove_useless_stmts_1 (tree *tp, struct rus_data *data)
-{
-  tree t = *tp, op;
-
-  switch (TREE_CODE (t))
-    {
-    case COND_EXPR:
-      remove_useless_stmts_cond (tp, data);
-      break;
-
-    case TRY_FINALLY_EXPR:
-      remove_useless_stmts_tf (tp, data);
-      break;
-
-    case TRY_CATCH_EXPR:
-      remove_useless_stmts_tc (tp, data);
-      break;
-
-    case BIND_EXPR:
-      remove_useless_stmts_bind (tp, data);
-      break;
-
-    case GOTO_EXPR:
-      remove_useless_stmts_goto (tp, data);
-      break;
-
-    case LABEL_EXPR:
-      remove_useless_stmts_label (tp, data);
-      break;
-
-    case RETURN_EXPR:
-      fold_stmt (tp);
-      data->last_goto = NULL;
-      data->may_branch = true;
-      break;
-
-    case CALL_EXPR:
-      fold_stmt (tp);
-      data->last_goto = NULL;
-      notice_special_calls (t);
-      update_call_expr_flags (t);
-      if (tree_could_throw_p (t))
-       data->may_throw = true;
-      break;
-
-    case MODIFY_EXPR:
-      gcc_unreachable ();
-
-    case GIMPLE_MODIFY_STMT:
-      data->last_goto = NULL;
-      fold_stmt (tp);
-      op = get_call_expr_in (t);
-      if (op)
-       {
-         update_call_expr_flags (op);
-         notice_special_calls (op);
-       }
-      if (tree_could_throw_p (t))
-       data->may_throw = true;
-      break;
-
-    case STATEMENT_LIST:
-      {
-       tree_stmt_iterator i = tsi_start (t);
-       while (!tsi_end_p (i))
-         {
-           t = tsi_stmt (i);
-           if (IS_EMPTY_STMT (t))
-             {
-               tsi_delink (&i);
-               continue;
-             }
-
-           remove_useless_stmts_1 (tsi_stmt_ptr (i), data);
-
-           t = tsi_stmt (i);
-           if (TREE_CODE (t) == STATEMENT_LIST)
-             {
-               tsi_link_before (&i, t, TSI_SAME_STMT);
-               tsi_delink (&i);
-             }
-           else
-             tsi_next (&i);
-         }
-      }
-      break;
-    case ASM_EXPR:
-      fold_stmt (tp);
-      data->last_goto = NULL;
-      break;
-
-    case OMP_PARALLEL:
-    case OMP_TASK:
-      /* Make sure the outermost BIND_EXPR in OMP_BODY isn't removed
-        as useless.  */
-      remove_useless_stmts_1 (&BIND_EXPR_BODY (OMP_TASKREG_BODY (*tp)), data);
-      data->last_goto = NULL;
-      break;
-
-    case OMP_SECTIONS:
-    case OMP_SINGLE:
-    case OMP_SECTION:
-    case OMP_MASTER:
-    case OMP_ORDERED:
-    case OMP_CRITICAL:
-      remove_useless_stmts_1 (&OMP_BODY (*tp), data);
-      data->last_goto = NULL;
-      break;
-
-    case OMP_FOR:
-      remove_useless_stmts_1 (&OMP_FOR_BODY (*tp), data);
-      data->last_goto = NULL;
-      if (OMP_FOR_PRE_BODY (*tp))
-       {
-         remove_useless_stmts_1 (&OMP_FOR_PRE_BODY (*tp), data);
-         data->last_goto = NULL;
-       }
-      break;
-
-    default:
-      data->last_goto = NULL;
-      break;
-    }
-}
-
-static unsigned int
-remove_useless_stmts (void)
-{
-  struct rus_data data;
-
-  clear_special_calls ();
-
-  do
-    {
-      memset (&data, 0, sizeof (data));
-      remove_useless_stmts_1 (&DECL_SAVED_TREE (current_function_decl), &data);
-    }
-  while (data.repeat);
-  return 0;
-}
-
-
-struct gimple_opt_pass pass_remove_useless_stmts =
-{
- {
-  GIMPLE_PASS,
-  "useless",                           /* name */
-  NULL,                                        /* gate */
-  remove_useless_stmts,                        /* execute */
-  NULL,                                        /* sub */
-  NULL,                                        /* next */
-  0,                                   /* static_pass_number */
-  0,                                   /* tv_id */
-  PROP_gimple_any,                     /* properties_required */
-  0,                                   /* properties_provided */
-  0,                                   /* properties_destroyed */
-  0,                                   /* todo_flags_start */
-  TODO_dump_func                       /* todo_flags_finish */
- }
-};
-
-/* Remove PHI nodes associated with basic block BB and all edges out of BB.  */
-
-static void
-remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
-{
-  tree phi;
-
-  /* Since this block is no longer reachable, we can just delete all
-     of its PHI nodes.  */
-  phi = phi_nodes (bb);
-  while (phi)
-    {
-      tree next = PHI_CHAIN (phi);
-      remove_phi_node (phi, NULL_TREE, true);
-      phi = next;
-    }
-
-  /* Remove edges to BB's successors.  */
-  while (EDGE_COUNT (bb->succs) > 0)
-    remove_edge (EDGE_SUCC (bb, 0));
-}
-
-
-/* Remove statements of basic block BB.  */
-
-static void
-remove_bb (basic_block bb)
-{
-  block_stmt_iterator i;
-  source_location loc = UNKNOWN_LOCATION;
-
-  if (dump_file)
-    {
-      fprintf (dump_file, "Removing basic block %d\n", bb->index);
-      if (dump_flags & TDF_DETAILS)
-       {
-         dump_bb (bb, dump_file, 0);
-         fprintf (dump_file, "\n");
-       }
-    }
-
-  if (current_loops)
-    {
-      struct loop *loop = bb->loop_father;
+      struct loop *loop = bb->loop_father;
 
       /* If a loop gets removed, clean up the information associated
         with it.  */
@@ -2059,31 +1783,35 @@ remove_bb (basic_block bb)
     }
 
   /* Remove all the instructions in the block.  */
-  if (bb_stmt_list (bb) != NULL_TREE)
+  if (bb_seq (bb) != NULL)
     {
-      for (i = bsi_start (bb); !bsi_end_p (i);)
+      /* Walk backwards so as to get a chance to substitute all
+        released DEFs into debug stmts.  See
+        eliminate_unnecessary_stmts() in tree-ssa-dce.c for more
+        details.  */
+      for (i = gsi_last_bb (bb); !gsi_end_p (i);)
        {
-         tree stmt = bsi_stmt (i);
-         if (TREE_CODE (stmt) == LABEL_EXPR
-             && (FORCED_LABEL (LABEL_EXPR_LABEL (stmt))
-                 || DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt))))
+         gimple stmt = gsi_stmt (i);
+         if (gimple_code (stmt) == GIMPLE_LABEL
+             && (FORCED_LABEL (gimple_label_label (stmt))
+                 || DECL_NONLOCAL (gimple_label_label (stmt))))
            {
              basic_block new_bb;
-             block_stmt_iterator new_bsi;
+             gimple_stmt_iterator new_gsi;
 
              /* A non-reachable non-local label may still be referenced.
                 But it no longer needs to carry the extra semantics of
                 non-locality.  */
-             if (DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
+             if (DECL_NONLOCAL (gimple_label_label (stmt)))
                {
-                 DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)) = 0;
-                 FORCED_LABEL (LABEL_EXPR_LABEL (stmt)) = 1;
+                 DECL_NONLOCAL (gimple_label_label (stmt)) = 0;
+                 FORCED_LABEL (gimple_label_label (stmt)) = 1;
                }
 
              new_bb = bb->prev_bb;
-             new_bsi = bsi_start (new_bb);
-             bsi_remove (&i, false);
-             bsi_insert_before (&new_bsi, stmt, BSI_NEW_STMT);
+             new_gsi = gsi_start_bb (new_bb);
+             gsi_remove (&i, false);
+             gsi_insert_before (&new_gsi, stmt, GSI_NEW_STMT);
            }
          else
            {
@@ -2094,30 +1822,18 @@ remove_bb (basic_block bb)
              if (gimple_in_ssa_p (cfun))
                release_defs (stmt);
 
-             bsi_remove (&i, true);
+             gsi_remove (&i, true);
            }
 
-         /* Don't warn for removed gotos.  Gotos are often removed due to
-            jump threading, thus resulting in bogus warnings.  Not great,
-            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)
-           {
-             if (EXPR_HAS_LOCATION (stmt))
-               loc = EXPR_LOCATION (stmt);
-           }
+         if (gsi_end_p (i))
+           i = gsi_last_bb (bb);
+         else
+           gsi_prev (&i);
        }
     }
 
-  /* 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 (loc > BUILTINS_LOCATION && LOCATION_LINE (loc) > 0)
-    warning (OPT_Wunreachable_code, "%Hwill never be executed", &loc);
-
   remove_phi_nodes_and_edges_for_unreachable_block (bb);
-  bb->il.tree = NULL;
+  bb->il.gimple = NULL;
 }
 
 
@@ -2128,21 +1844,23 @@ remove_bb (basic_block bb)
 edge
 find_taken_edge (basic_block bb, tree val)
 {
-  tree stmt;
+  gimple stmt;
 
   stmt = last_stmt (bb);
 
   gcc_assert (stmt);
   gcc_assert (is_ctrl_stmt (stmt));
-  gcc_assert (val);
 
-  if (! is_gimple_min_invariant (val))
+  if (val == NULL)
     return NULL;
 
-  if (TREE_CODE (stmt) == COND_EXPR)
+  if (!is_gimple_min_invariant (val))
+    return NULL;
+
+  if (gimple_code (stmt) == GIMPLE_COND)
     return find_taken_edge_cond_expr (bb, val);
 
-  if (TREE_CODE (stmt) == SWITCH_EXPR)
+  if (gimple_code (stmt) == GIMPLE_SWITCH)
     return find_taken_edge_switch_expr (bb, val);
 
   if (computed_goto_p (stmt))
@@ -2204,12 +1922,13 @@ find_taken_edge_cond_expr (basic_block bb, tree val)
 static edge
 find_taken_edge_switch_expr (basic_block bb, tree val)
 {
-  tree switch_expr, taken_case;
   basic_block dest_bb;
   edge e;
+  gimple switch_stmt;
+  tree taken_case;
 
-  switch_expr = last_stmt (bb);
-  taken_case = find_case_label_for_value (switch_expr, val);
+  switch_stmt = last_stmt (bb);
+  taken_case = find_case_label_for_value (switch_stmt, val);
   dest_bb = label_to_block (CASE_LABEL (taken_case));
 
   e = find_edge (bb, dest_bb);
@@ -2218,21 +1937,20 @@ find_taken_edge_switch_expr (basic_block bb, tree val)
 }
 
 
-/* Return the CASE_LABEL_EXPR that SWITCH_EXPR will take for VAL.
+/* Return the CASE_LABEL_EXPR that SWITCH_STMT will take for VAL.
    We can make optimal use here of the fact that the case labels are
    sorted: We can do a binary search for a case matching VAL.  */
 
 static tree
-find_case_label_for_value (tree switch_expr, tree val)
+find_case_label_for_value (gimple switch_stmt, tree val)
 {
-  tree vec = SWITCH_LABELS (switch_expr);
-  size_t low, high, n = TREE_VEC_LENGTH (vec);
-  tree default_case = TREE_VEC_ELT (vec, n - 1);
+  size_t low, high, n = gimple_switch_num_labels (switch_stmt);
+  tree default_case = gimple_switch_default_label (switch_stmt);
 
-  for (low = -1, high = n - 1; high - low > 1; )
+  for (low = 0, high = n; high - low > 1; )
     {
       size_t i = (high + low) / 2;
-      tree t = TREE_VEC_ELT (vec, i);
+      tree t = gimple_switch_label (switch_stmt, i);
       int cmp;
 
       /* Cache the result of comparing CASE_LOW and val.  */
@@ -2261,36 +1979,21 @@ find_case_label_for_value (tree switch_expr, tree val)
 }
 
 
-
-
-/*---------------------------------------------------------------------------
-                             Debugging functions
----------------------------------------------------------------------------*/
-
-/* Dump tree-specific information of block BB to file OUTF.  */
-
-void
-tree_dump_bb (basic_block bb, FILE *outf, int indent)
-{
-  dump_generic_bb (outf, bb, indent, TDF_VOPS|TDF_MEMSYMS);
-}
-
-
 /* Dump a basic block on stderr.  */
 
 void
-debug_tree_bb (basic_block bb)
+gimple_debug_bb (basic_block bb)
 {
-  dump_bb (bb, stderr, 0);
+  gimple_dump_bb (bb, stderr, 0, TDF_VOPS|TDF_MEMSYMS);
 }
 
 
 /* Dump basic block with index N on stderr.  */
 
 basic_block
-debug_tree_bb_n (int n)
+gimple_debug_bb_n (int n)
 {
-  debug_tree_bb (BASIC_BLOCK (n));
+  gimple_debug_bb (BASIC_BLOCK (n));
   return BASIC_BLOCK (n);
 }
 
@@ -2301,9 +2004,9 @@ debug_tree_bb_n (int n)
    (see TDF_* in tree-pass.h).  */
 
 void
-debug_tree_cfg (int flags)
+gimple_debug_cfg (int flags)
 {
-  dump_tree_cfg (stderr, flags);
+  gimple_dump_cfg (stderr, flags);
 }
 
 
@@ -2313,7 +2016,7 @@ debug_tree_cfg (int flags)
    tree.h).  */
 
 void
-dump_tree_cfg (FILE *file, int flags)
+gimple_dump_cfg (FILE *file, int flags)
 {
   if (flags & TDF_DETAILS)
     {
@@ -2401,7 +2104,7 @@ debug_cfg_stats (void)
 /* Dump the flowgraph to a .vcg FILE.  */
 
 static void
-tree_cfg2vcg (FILE *file)
+gimple_cfg2vcg (FILE *file)
 {
   edge e;
   edge_iterator ei;
@@ -2431,17 +2134,17 @@ tree_cfg2vcg (FILE *file)
 
   FOR_EACH_BB (bb)
     {
-      enum tree_code head_code, end_code;
+      enum gimple_code head_code, end_code;
       const char *head_name, *end_name;
       int head_line = 0;
       int end_line = 0;
-      tree first = first_stmt (bb);
-      tree last = last_stmt (bb);
+      gimple first = first_stmt (bb);
+      gimple last = last_stmt (bb);
 
       if (first)
        {
-         head_code = TREE_CODE (first);
-         head_name = tree_code_name[head_code];
+         head_code = gimple_code (first);
+         head_name = gimple_code_name[head_code];
          head_line = get_lineno (first);
        }
       else
@@ -2449,8 +2152,8 @@ tree_cfg2vcg (FILE *file)
 
       if (last)
        {
-         end_code = TREE_CODE (last);
-         end_name = tree_code_name[end_code];
+         end_code = gimple_code (last);
+         end_name = gimple_code_name[end_code];
          end_line = get_lineno (last);
        }
       else
@@ -2491,13 +2194,19 @@ tree_cfg2vcg (FILE *file)
 /* Return true if T represents a stmt that always transfers control.  */
 
 bool
-is_ctrl_stmt (const_tree t)
+is_ctrl_stmt (gimple t)
 {
-  return (TREE_CODE (t) == COND_EXPR
-         || TREE_CODE (t) == SWITCH_EXPR
-         || TREE_CODE (t) == GOTO_EXPR
-         || TREE_CODE (t) == RETURN_EXPR
-         || TREE_CODE (t) == RESX_EXPR);
+  switch (gimple_code (t))
+    {
+    case GIMPLE_COND:
+    case GIMPLE_SWITCH:
+    case GIMPLE_GOTO:
+    case GIMPLE_RETURN:
+    case GIMPLE_RESX:
+      return true;
+    default:
+      return false;
+    }
 }
 
 
@@ -2505,50 +2214,58 @@ is_ctrl_stmt (const_tree t)
    (e.g., a call to a non-returning function).  */
 
 bool
-is_ctrl_altering_stmt (const_tree t)
+is_ctrl_altering_stmt (gimple t)
 {
-  const_tree call;
-
   gcc_assert (t);
-  call = get_call_expr_in (CONST_CAST_TREE (t));
-  if (call)
+
+  switch (gimple_code (t))
     {
-      /* A non-pure/const CALL_EXPR alters flow control if the current
-        function has nonlocal labels.  */
-      if (TREE_SIDE_EFFECTS (call) && cfun->has_nonlocal_label)
-       return true;
+    case GIMPLE_CALL:
+      {
+       int flags = gimple_call_flags (t);
 
-      /* A CALL_EXPR also alters control flow if it does not return.  */
-      if (call_expr_flags (call) & ECF_NORETURN)
-       return true;
-    }
+       /* A non-pure/const call alters flow control if the current
+          function has nonlocal labels.  */
+       if (!(flags & (ECF_CONST | ECF_PURE)) && cfun->has_nonlocal_label)
+         return true;
 
-  /* OpenMP directives alter control flow.  */
-  if (OMP_DIRECTIVE_P (t))
-    return true;
+       /* A call also alters control flow if it does not return.  */
+       if (flags & ECF_NORETURN)
+         return true;
+      }
+      break;
 
-  /* If a statement can throw, it alters control flow.  */
-  return tree_can_throw_internal (t);
-}
+    case GIMPLE_EH_DISPATCH:
+      /* EH_DISPATCH branches to the individual catch handlers at
+        this level of a try or allowed-exceptions region.  It can
+        fallthru to the next statement as well.  */
+      return true;
 
+    case GIMPLE_ASM:
+      if (gimple_asm_nlabels (t) > 0)
+       return true;
+      break;
 
-/* Return true if T is a computed goto.  */
+    CASE_GIMPLE_OMP:
+      /* OpenMP directives alter control flow.  */
+      return true;
 
-static bool
-computed_goto_p (const_tree t)
-{
-  return (TREE_CODE (t) == GOTO_EXPR
-         && TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL);
+    default:
+      break;
+    }
+
+  /* If a statement can throw, it alters control flow.  */
+  return stmt_can_throw_internal (t);
 }
 
 
 /* Return true if T is a simple local goto.  */
 
 bool
-simple_goto_p (const_tree t)
+simple_goto_p (gimple t)
 {
-  return (TREE_CODE (t) == GOTO_EXPR
-         && TREE_CODE (GOTO_DESTINATION (t)) == LABEL_DECL);
+  return (gimple_code (t) == GIMPLE_GOTO
+         && TREE_CODE (gimple_goto_dest (t)) == LABEL_DECL);
 }
 
 
@@ -2556,46 +2273,42 @@ simple_goto_p (const_tree t)
    Transfers of control flow associated with EH are excluded.  */
 
 bool
-tree_can_make_abnormal_goto (const_tree t)
+stmt_can_make_abnormal_goto (gimple t)
 {
   if (computed_goto_p (t))
     return true;
-  if (TREE_CODE (t) == GIMPLE_MODIFY_STMT)
-    t = GIMPLE_STMT_OPERAND (t, 1);
-  if (TREE_CODE (t) == WITH_SIZE_EXPR)
-    t = TREE_OPERAND (t, 0);
-  if (TREE_CODE (t) == CALL_EXPR)
-    return TREE_SIDE_EFFECTS (t) && cfun->has_nonlocal_label;
+  if (is_gimple_call (t))
+    return gimple_has_side_effects (t) && cfun->has_nonlocal_label;
   return false;
 }
 
 
-/* Return true if T should start a new basic block.  PREV_T is the
-   statement preceding T.  It is used when T is a label or a case label.
-   Labels should only start a new basic block if their previous statement
-   wasn't a label.  Otherwise, sequence of labels would generate
-   unnecessary basic blocks that only contain a single label.  */
+/* Return true if STMT should start a new basic block.  PREV_STMT is
+   the statement preceding STMT.  It is used when STMT is a label or a
+   case label.  Labels should only start a new basic block if their
+   previous statement wasn't a label.  Otherwise, sequence of labels
+   would generate unnecessary basic blocks that only contain a single
+   label.  */
 
 static inline bool
-stmt_starts_bb_p (const_tree t, const_tree prev_t)
+stmt_starts_bb_p (gimple stmt, gimple prev_stmt)
 {
-  if (t == NULL_TREE)
+  if (stmt == NULL)
     return false;
 
-  /* LABEL_EXPRs start a new basic block only if the preceding
-     statement wasn't a label of the same type.  This prevents the
-     creation of consecutive blocks that have nothing but a single
-     label.  */
-  if (TREE_CODE (t) == LABEL_EXPR)
+  /* Labels start a new basic block only if the preceding statement
+     wasn't a label of the same type.  This prevents the creation of
+     consecutive blocks that have nothing but a single label.  */
+  if (gimple_code (stmt) == GIMPLE_LABEL)
     {
       /* Nonlocal and computed GOTO targets always start a new block.  */
-      if (DECL_NONLOCAL (LABEL_EXPR_LABEL (t))
-         || FORCED_LABEL (LABEL_EXPR_LABEL (t)))
+      if (DECL_NONLOCAL (gimple_label_label (stmt))
+         || FORCED_LABEL (gimple_label_label (stmt)))
        return true;
 
-      if (prev_t && TREE_CODE (prev_t) == LABEL_EXPR)
+      if (prev_stmt && gimple_code (prev_stmt) == GIMPLE_LABEL)
        {
-         if (DECL_NONLOCAL (LABEL_EXPR_LABEL (prev_t)))
+         if (DECL_NONLOCAL (gimple_label_label (prev_stmt)))
            return true;
 
          cfg_stats.num_merged_labels++;
@@ -2612,500 +2325,120 @@ stmt_starts_bb_p (const_tree t, const_tree prev_t)
 /* Return true if T should end a basic block.  */
 
 bool
-stmt_ends_bb_p (const_tree t)
+stmt_ends_bb_p (gimple t)
 {
   return is_ctrl_stmt (t) || is_ctrl_altering_stmt (t);
 }
 
-/* Remove block annotations and other datastructures.  */
+/* Remove block annotations and other data structures.  */
 
 void
 delete_tree_cfg_annotations (void)
 {
-  basic_block bb;
-  block_stmt_iterator bsi;
-
-  /* Remove annotations from every tree in the function.  */
-  FOR_EACH_BB (bb)
-    for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
-      {
-       tree stmt = bsi_stmt (bsi);
-       ggc_free (stmt->base.ann);
-       stmt->base.ann = NULL;
-      }
   label_to_block_map = NULL;
 }
 
 
 /* Return the first statement in basic block BB.  */
 
-tree
+gimple
 first_stmt (basic_block bb)
 {
-  block_stmt_iterator i = bsi_start (bb);
-  return !bsi_end_p (i) ? bsi_stmt (i) : NULL_TREE;
+  gimple_stmt_iterator i = gsi_start_bb (bb);
+  gimple stmt = NULL;
+
+  while (!gsi_end_p (i) && is_gimple_debug ((stmt = gsi_stmt (i))))
+    {
+      gsi_next (&i);
+      stmt = NULL;
+    }
+  return stmt;
+}
+
+/* Return the first non-label statement in basic block BB.  */
+
+static gimple
+first_non_label_stmt (basic_block bb)
+{
+  gimple_stmt_iterator i = gsi_start_bb (bb);
+  while (!gsi_end_p (i) && gimple_code (gsi_stmt (i)) == GIMPLE_LABEL)
+    gsi_next (&i);
+  return !gsi_end_p (i) ? gsi_stmt (i) : NULL;
 }
 
 /* Return the last statement in basic block BB.  */
 
-tree
+gimple
 last_stmt (basic_block bb)
 {
-  block_stmt_iterator b = bsi_last (bb);
-  return !bsi_end_p (b) ? bsi_stmt (b) : NULL_TREE;
+  gimple_stmt_iterator i = gsi_last_bb (bb);
+  gimple stmt = NULL;
+
+  while (!gsi_end_p (i) && is_gimple_debug ((stmt = gsi_stmt (i))))
+    {
+      gsi_prev (&i);
+      stmt = NULL;
+    }
+  return stmt;
 }
 
 /* Return the last statement of an otherwise empty block.  Return NULL
    if the block is totally empty, or if it contains more than one
    statement.  */
 
-tree
+gimple
 last_and_only_stmt (basic_block bb)
 {
-  block_stmt_iterator i = bsi_last (bb);
-  tree last, prev;
+  gimple_stmt_iterator i = gsi_last_nondebug_bb (bb);
+  gimple last, prev;
 
-  if (bsi_end_p (i))
-    return NULL_TREE;
+  if (gsi_end_p (i))
+    return NULL;
 
-  last = bsi_stmt (i);
-  bsi_prev (&i);
-  if (bsi_end_p (i))
+  last = gsi_stmt (i);
+  gsi_prev_nondebug (&i);
+  if (gsi_end_p (i))
     return last;
 
   /* Empty statements should no longer appear in the instruction stream.
      Everything that might have appeared before should be deleted by
-     remove_useless_stmts, and the optimizers should just bsi_remove
+     remove_useless_stmts, and the optimizers should just gsi_remove
      instead of smashing with build_empty_stmt.
 
      Thus the only thing that should appear here in a block containing
      one executable statement is a label.  */
-  prev = bsi_stmt (i);
-  if (TREE_CODE (prev) == LABEL_EXPR)
+  prev = gsi_stmt (i);
+  if (gimple_code (prev) == GIMPLE_LABEL)
     return last;
   else
-    return NULL_TREE;
-}
-
-
-/* Mark BB as the basic block holding statement T.  */
-
-void
-set_bb_for_stmt (tree t, basic_block bb)
-{
-  if (TREE_CODE (t) == PHI_NODE)
-    PHI_BB (t) = bb;
-  else if (TREE_CODE (t) == STATEMENT_LIST)
-    {
-      tree_stmt_iterator i;
-      for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
-       set_bb_for_stmt (tsi_stmt (i), bb);
-    }
-  else
-    {
-      stmt_ann_t ann = get_stmt_ann (t);
-      ann->bb = bb;
-
-      /* If the statement is a label, add the label to block-to-labels map
-        so that we can speed up edge creation for GOTO_EXPRs.  */
-      if (TREE_CODE (t) == LABEL_EXPR)
-       {
-         int uid;
-
-         t = LABEL_EXPR_LABEL (t);
-         uid = LABEL_DECL_UID (t);
-         if (uid == -1)
-           {
-             unsigned old_len = VEC_length (basic_block, label_to_block_map);
-             LABEL_DECL_UID (t) = uid = cfun->cfg->last_label_uid++;
-             if (old_len <= (unsigned) uid)
-               {
-                 unsigned new_len = 3 * uid / 2;
-
-                 VEC_safe_grow_cleared (basic_block, gc, label_to_block_map,
-                                        new_len);
-               }
-           }
-         else
-           /* We're moving an existing label.  Make sure that we've
-               removed it from the old block.  */
-           gcc_assert (!bb
-                       || !VEC_index (basic_block, label_to_block_map, uid));
-         VEC_replace (basic_block, label_to_block_map, uid, bb);
-       }
-    }
-}
-
-/* Faster version of set_bb_for_stmt that assume that statement is being moved
-   from one basic block to another.  
-   For BB splitting we can run into quadratic case, so performance is quite
-   important and knowing that the tables are big enough, change_bb_for_stmt
-   can inline as leaf function.  */
-static inline void
-change_bb_for_stmt (tree t, basic_block bb)
-{
-  get_stmt_ann (t)->bb = bb;
-  if (TREE_CODE (t) == LABEL_EXPR)
-    VEC_replace (basic_block, label_to_block_map,
-                LABEL_DECL_UID (LABEL_EXPR_LABEL (t)), bb);
-}
-
-/* Finds iterator for STMT.  */
-
-extern block_stmt_iterator
-bsi_for_stmt (tree stmt)
-{
-  block_stmt_iterator bsi;
-
-  for (bsi = bsi_start (bb_for_stmt (stmt)); !bsi_end_p (bsi); bsi_next (&bsi))
-    if (bsi_stmt (bsi) == stmt)
-      return bsi;
-
-  gcc_unreachable ();
-}
-
-/* Mark statement T as modified, and update it.  */
-static inline void
-update_modified_stmts (tree t)
-{
-  if (!ssa_operands_active ())
-    return;
-  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).  */
-
-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);
-}
-
-
-/* Insert statement (or statement list) T after the statement
-   pointed-to by iterator I.  M specifies how to update iterator I
-   after insertion (see enum bsi_iterator_update).  */
-
-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);
-}
-
-
-/* Remove the statement pointed to by iterator I.  The iterator is updated
-   to the next statement.
-
-   When REMOVE_EH_INFO is true we remove the statement pointed to by
-   iterator I from the EH tables.  Otherwise we do not modify the EH
-   tables.
-
-   Generally, REMOVE_EH_INFO should be true when the statement is going to
-   be removed from the IL and not reinserted elsewhere.  */
-
-void
-bsi_remove (block_stmt_iterator *i, bool remove_eh_info)
-{
-  tree t = bsi_stmt (*i);
-  set_bb_for_stmt (t, NULL);
-  delink_stmt_imm_use (t);
-  tsi_delink (&i->tsi);
-  mark_stmt_modified (t);
-  if (remove_eh_info)
-    {
-      remove_stmt_from_eh_region (t);
-      gimple_remove_stmt_histograms (cfun, t);
-    }
-}
-
-
-/* Move the statement at FROM so it comes right after the statement at TO.  */
-
-void
-bsi_move_after (block_stmt_iterator *from, block_stmt_iterator *to)
-{
-  tree stmt = bsi_stmt (*from);
-  bsi_remove (from, false);
-  /* We must have BSI_NEW_STMT here, as bsi_move_after is sometimes used to
-     move statements to an empty block.  */
-  bsi_insert_after (to, stmt, BSI_NEW_STMT);
-}
-
-
-/* Move the statement at FROM so it comes right before the statement at TO.  */
-
-void
-bsi_move_before (block_stmt_iterator *from, block_stmt_iterator *to)
-{
-  tree stmt = bsi_stmt (*from);
-  bsi_remove (from, false);
-  /* For consistency with bsi_move_after, it might be better to have
-     BSI_NEW_STMT here; however, that breaks several places that expect
-     that TO does not change.  */
-  bsi_insert_before (to, stmt, BSI_SAME_STMT);
-}
-
-
-/* Move the statement at FROM to the end of basic block BB.  */
-
-void
-bsi_move_to_bb_end (block_stmt_iterator *from, basic_block bb)
-{
-  block_stmt_iterator last = bsi_last (bb);
-
-  /* Have to check bsi_end_p because it could be an empty block.  */
-  if (!bsi_end_p (last) && is_ctrl_stmt (bsi_stmt (last)))
-    bsi_move_before (from, &last);
-  else
-    bsi_move_after (from, &last);
-}
-
-
-/* Replace the contents of the statement pointed to by iterator BSI
-   with STMT.  If UPDATE_EH_INFO is true, the exception handling
-   information of the original statement is moved to the new statement.  */
-
-void
-bsi_replace (const block_stmt_iterator *bsi, tree stmt, bool update_eh_info)
-{
-  int eh_region;
-  tree orig_stmt = bsi_stmt (*bsi);
-
-  if (stmt == orig_stmt)
-    return;
-  SET_EXPR_LOCUS (stmt, EXPR_LOCUS (orig_stmt));
-  set_bb_for_stmt (stmt, bsi->bb);
-
-  /* Preserve EH region information from the original statement, if
-     requested by the caller.  */
-  if (update_eh_info)
-    {
-      eh_region = lookup_stmt_eh_region (orig_stmt);
-      if (eh_region >= 0)
-       {
-         remove_stmt_from_eh_region (orig_stmt);
-         add_stmt_to_eh_region (stmt, eh_region);
-       }
-    }
-
-  gimple_duplicate_stmt_histograms (cfun, stmt, cfun, orig_stmt);
-  gimple_remove_stmt_histograms (cfun, orig_stmt);
-  delink_stmt_imm_use (orig_stmt);
-  *bsi_stmt_ptr (*bsi) = stmt;
-  mark_stmt_modified (stmt);
-  update_modified_stmts (stmt);
-}
-
-
-/* Insert the statement pointed-to by BSI into edge E.  Every attempt
-   is made to place the statement in an existing basic block, but
-   sometimes that isn't possible.  When it isn't possible, the edge is
-   split and the statement is added to the new block.
-
-   In all cases, the returned *BSI points to the correct location.  The
-   return value is true if insertion should be done after the location,
-   or false if it should be done before the location.  If new basic block
-   has to be created, it is stored in *NEW_BB.  */
-
-static bool
-tree_find_edge_insert_loc (edge e, block_stmt_iterator *bsi,
-                          basic_block *new_bb)
-{
-  basic_block dest, src;
-  tree tmp;
-
-  dest = e->dest;
- restart:
-
-  /* If the destination has one predecessor which has no PHI nodes,
-     insert there.  Except for the exit block.
-
-     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
-     hardly seems worth the effort.  */
-  if (single_pred_p (dest)
-      && ! phi_nodes (dest)
-      && dest != EXIT_BLOCK_PTR)
-    {
-      *bsi = bsi_start (dest);
-      if (bsi_end_p (*bsi))
-       return true;
-
-      /* Make sure we insert after any leading labels.  */
-      tmp = bsi_stmt (*bsi);
-      while (TREE_CODE (tmp) == LABEL_EXPR)
-       {
-         bsi_next (bsi);
-         if (bsi_end_p (*bsi))
-           break;
-         tmp = bsi_stmt (*bsi);
-       }
-
-      if (bsi_end_p (*bsi))
-       {
-         *bsi = bsi_last (dest);
-         return true;
-       }
-      else
-       return false;
-    }
-
-  /* If the source has one successor, the edge is not abnormal and
-     the last statement does not end a basic block, insert there.
-     Except for the entry block.  */
-  src = e->src;
-  if ((e->flags & EDGE_ABNORMAL) == 0
-      && single_succ_p (src)
-      && src != ENTRY_BLOCK_PTR)
-    {
-      *bsi = bsi_last (src);
-      if (bsi_end_p (*bsi))
-       return true;
-
-      tmp = bsi_stmt (*bsi);
-      if (!stmt_ends_bb_p (tmp))
-       return true;
-
-      /* Insert code just before returning the value.  We may need to decompose
-         the return in the case it contains non-trivial operand.  */
-      if (TREE_CODE (tmp) == RETURN_EXPR)
-        {
-         tree op = TREE_OPERAND (tmp, 0);
-         if (op && !is_gimple_val (op))
-           {
-             gcc_assert (TREE_CODE (op) == GIMPLE_MODIFY_STMT);
-             bsi_insert_before (bsi, op, BSI_NEW_STMT);
-             TREE_OPERAND (tmp, 0) = GIMPLE_STMT_OPERAND (op, 0);
-           }
-         bsi_prev (bsi);
-         return true;
-        }
-    }
-
-  /* Otherwise, create a new basic block, and split this edge.  */
-  dest = split_edge (e);
-  if (new_bb)
-    *new_bb = dest;
-  e = single_pred_edge (dest);
-  goto restart;
-}
-
-
-/* This routine will commit all pending edge insertions, creating any new
-   basic blocks which are necessary.  */
-
-void
-bsi_commit_edge_inserts (void)
-{
-  basic_block bb;
-  edge e;
-  edge_iterator ei;
-
-  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_one_edge_insert (e, NULL);
-}
-
-
-/* Commit insertions pending at edge E. If a new block is created, set NEW_BB
-   to this block, otherwise set it to NULL.  */
-
-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;
-      tree stmt = PENDING_STMT (e);
-
-      PENDING_STMT (e) = NULL_TREE;
-
-      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);
-    }
-}
-
-
-/* Add STMT to the pending list of edge E.  No actual insertion is
-   made until a call to bsi_commit_edge_inserts () is made.  */
-
-void
-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 a new
-   block has to be created, it is returned.  */
-
-basic_block
-bsi_insert_on_edge_immediate (edge e, tree stmt)
-{
-  block_stmt_iterator bsi;
-  basic_block new_bb = NULL;
-
-  gcc_assert (!PENDING_STMT (e));
-
-  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);
-
-  return new_bb;
+    return NULL;
 }
 
-/*---------------------------------------------------------------------------
-            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 phi;
   edge_var_map_vector v;
   edge_var_map *vm;
   int i;
+  gimple_stmt_iterator phis;
 
   v = redirect_edge_var_map_vector (old_edge);
   if (!v)
     return;
 
-  for (i = 0, phi = phi_nodes (new_edge->dest);
-       VEC_iterate (edge_var_map, v, i, vm) && phi;
-       i++, phi = PHI_CHAIN (phi))
+  for (i = 0, phis = gsi_start_phis (new_edge->dest);
+       VEC_iterate (edge_var_map, v, i, vm) && !gsi_end_p (phis);
+       i++, gsi_next (&phis))
     {
+      gimple phi = gsi_stmt (phis);
       tree result = redirect_edge_var_map_result (vm);
       tree arg = redirect_edge_var_map_def (vm);
 
-      gcc_assert (result == PHI_RESULT (phi));
+      gcc_assert (result == gimple_phi_result (phi));
 
-      add_phi_arg (phi, arg, new_edge);
+      add_phi_arg (phi, arg, new_edge, redirect_edge_var_map_location (vm));
     }
 
   redirect_edge_var_map_clear (old_edge);
@@ -3120,18 +2453,22 @@ static basic_block
 split_edge_bb_loc (edge edge_in)
 {
   basic_block dest = edge_in->dest;
+  basic_block dest_prev = dest->prev_bb;
 
-  if (dest->prev_bb && find_edge (dest->prev_bb, dest))
-    return edge_in->src;
-  else
-    return dest->prev_bb;
+  if (dest_prev)
+    {
+      edge e = find_edge (dest_prev, dest);
+      if (e && !(e->flags & EDGE_COMPLEX))
+       return edge_in->src;
+    }
+  return dest_prev;
 }
 
 /* Split a (typically critical) edge EDGE_IN.  Return the new block.
    Abort on abnormal edges.  */
 
 static basic_block
-tree_split_edge (edge edge_in)
+gimple_split_edge (edge edge_in)
 {
   basic_block new_bb, after_bb, dest;
   edge new_edge, e;
@@ -3184,6 +2521,15 @@ verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
        }
       break;
 
+    case INDIRECT_REF:
+      x = TREE_OPERAND (t, 0);
+      if (!is_gimple_reg (x) && !is_gimple_min_invariant (x))
+       {
+         error ("Indirect reference's operand is not a register or a constant.");
+         return x;
+       }
+      break;
+
     case ASSERT_EXPR:
       x = fold (ASSERT_EXPR_COND (t));
       if (x == boolean_false_node)
@@ -3193,19 +2539,10 @@ verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
        }
       break;
 
-    case MODIFY_EXPR:
-      gcc_unreachable ();
-
-    case GIMPLE_MODIFY_STMT:
-      x = GIMPLE_STMT_OPERAND (t, 0);
-      if (TREE_CODE (x) == BIT_FIELD_REF
-         && is_gimple_reg (TREE_OPERAND (x, 0)))
-       {
-         error ("GIMPLE register modified with BIT_FIELD_REF");
-         return t;
-       }
-      break;
-
+    case MODIFY_EXPR:
+      error ("MODIFY_EXPR not expected while having tuples.");
+      return *tp;
+
     case ADDR_EXPR:
       {
        bool old_constant;
@@ -3241,13 +2578,20 @@ verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
             x = TREE_OPERAND (x, 0))
          ;
 
-       if (TREE_CODE (x) != VAR_DECL && TREE_CODE (x) != PARM_DECL)
+       if (!(TREE_CODE (x) == VAR_DECL
+             || TREE_CODE (x) == PARM_DECL
+             || TREE_CODE (x) == RESULT_DECL))
          return NULL;
        if (!TREE_ADDRESSABLE (x))
          {
            error ("address taken, but ADDRESSABLE bit not set");
            return x;
          }
+       if (DECL_GIMPLE_REG_P (x))
+         {
+           error ("DECL_GIMPLE_REG_P set on a variable with address taken");
+           return x;
+         }
 
        break;
       }
@@ -3420,88 +2764,29 @@ verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
 #undef CHECK_OP
 }
 
-/* Verifies if EXPR is a valid GIMPLE unary expression.  Returns true
-   if there is an error, otherwise false.  */
-
-static bool
-verify_gimple_unary_expr (const_tree expr)
-{
-  tree op = TREE_OPERAND (expr, 0);
-  tree type = TREE_TYPE (expr);
-
-  if (!is_gimple_val (op))
-    {
-      error ("invalid operand in unary expression");
-      return true;
-    }
-
-  /* For general unary expressions we have the operations type
-     as the effective type the operation is carried out on.  So all
-     we need to require is that the operand is trivially convertible
-     to that type.  */
-  if (!useless_type_conversion_p (type, TREE_TYPE (op)))
-    {
-      error ("type mismatch in unary expression");
-      debug_generic_expr (type);
-      debug_generic_expr (TREE_TYPE (op));
-      return true;
-    }
-
-  return false;
-}
-
-/* Verifies if EXPR is a valid GIMPLE binary expression.  Returns true
-   if there is an error, otherwise false.  */
-
-static bool
-verify_gimple_binary_expr (const_tree expr)
-{
-  tree op0 = TREE_OPERAND (expr, 0);
-  tree op1 = TREE_OPERAND (expr, 1);
-  tree type = TREE_TYPE (expr);
-
-  if (!is_gimple_val (op0) || !is_gimple_val (op1))
-    {
-      error ("invalid operands in binary expression");
-      return true;
-    }
-
-  /* For general binary expressions we have the operations type
-     as the effective type the operation is carried out on.  So all
-     we need to require is that both operands are trivially convertible
-     to that type.  */
-  if (!useless_type_conversion_p (type, TREE_TYPE (op0))
-      || !useless_type_conversion_p (type, TREE_TYPE (op1)))
-    {
-      error ("type mismatch in binary expression");
-      debug_generic_stmt (type);
-      debug_generic_stmt (TREE_TYPE (op0));
-      debug_generic_stmt (TREE_TYPE (op1));
-      return true;
-    }
-
-  return false;
-}
 
 /* Verify if EXPR is either a GIMPLE ID or a GIMPLE indirect reference.
    Returns true if there is an error, otherwise false.  */
 
 static bool
-verify_gimple_min_lval (tree expr)
+verify_types_in_gimple_min_lval (tree expr)
 {
   tree op;
 
   if (is_gimple_id (expr))
     return false;
 
-  if (TREE_CODE (expr) != INDIRECT_REF
-      && TREE_CODE (expr) != ALIGN_INDIRECT_REF
-      && TREE_CODE (expr) != MISALIGNED_INDIRECT_REF)
+  if (!INDIRECT_REF_P (expr)
+      && TREE_CODE (expr) != TARGET_MEM_REF)
     {
       error ("invalid expression for min lvalue");
       return true;
     }
 
+  /* TARGET_MEM_REFs are strange beasts.  */
+  if (TREE_CODE (expr) == TARGET_MEM_REF)
+    return false;
+
   op = TREE_OPERAND (expr, 0);
   if (!is_gimple_val (op))
     {
@@ -3521,11 +2806,12 @@ verify_gimple_min_lval (tree expr)
   return false;
 }
 
-/* Verify if EXPR is a valid GIMPLE reference expression.  Returns true
+/* Verify if EXPR is a valid GIMPLE reference expression.  If
+   REQUIRE_LVALUE is true verifies it is an lvalue.  Returns true
    if there is an error, otherwise false.  */
 
 static bool
-verify_gimple_reference (tree expr)
+verify_types_in_gimple_reference (tree expr, bool require_lvalue)
 {
   while (handled_component_p (expr))
     {
@@ -3587,14 +2873,30 @@ verify_gimple_reference (tree expr)
          return true;
        }
 
-      /* For VIEW_CONVERT_EXPRs which are allowed here, too, there
-        is nothing to verify.  Gross mismatches at most invoke
-        undefined behavior.  */
+      if (TREE_CODE (expr) == VIEW_CONVERT_EXPR)
+       {
+         /* For VIEW_CONVERT_EXPRs which are allowed here too, we only check
+            that their operand is not an SSA name or an invariant when
+            requiring an lvalue (this usually means there is a SRA or IPA-SRA
+            bug).  Otherwise there is nothing to verify, gross mismatches at
+            most invoke undefined behavior.  */
+         if (require_lvalue
+             && (TREE_CODE (op) == SSA_NAME
+                 || is_gimple_min_invariant (op)))
+           {
+             error ("Conversion of an SSA_NAME on the left hand side.");
+             debug_generic_stmt (expr);
+             return true;
+           }
+         else if (!handled_component_p (op))
+           return false;
+       }
 
       expr = op;
     }
 
-  return verify_gimple_min_lval (expr);
+  return ((require_lvalue || !is_gimple_min_invariant (expr))
+         && verify_types_in_gimple_min_lval (expr));
 }
 
 /* Returns true if there is one pointer type in TYPE_POINTER_TO (SRC_OBJ)
@@ -3627,80 +2929,237 @@ valid_fixed_convert_types_p (tree type1, tree type2)
              || FIXED_POINT_TYPE_P (type2)));
 }
 
-/* Verify the GIMPLE expression EXPR.  Returns true if there is an
-   error, otherwise false.  */
+/* Verify the contents of a GIMPLE_CALL STMT.  Returns true when there
+   is a problem, otherwise false.  */
 
 static bool
-verify_gimple_expr (tree expr)
+verify_gimple_call (gimple stmt)
 {
-  tree type = TREE_TYPE (expr);
+  tree fn = gimple_call_fn (stmt);
+  tree fntype;
+  unsigned i;
 
-  if (is_gimple_val (expr))
-    return false;
+  if (TREE_CODE (fn) != OBJ_TYPE_REF
+      && !is_gimple_val (fn))
+    {
+      error ("invalid function in gimple call");
+      debug_generic_stmt (fn);
+      return true;
+    }
 
-  /* Special codes we cannot handle via their class.  */
-  switch (TREE_CODE (expr))
+  if (!POINTER_TYPE_P (TREE_TYPE  (fn))
+      || (TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) != FUNCTION_TYPE
+         && TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) != METHOD_TYPE))
     {
-    CASE_CONVERT:
-      {
-       tree op = TREE_OPERAND (expr, 0);
-       if (!is_gimple_val (op))
-         {
-           error ("invalid operand in conversion");
-           return true;
-         }
+      error ("non-function in gimple call");
+      return true;
+    }
 
-       /* Allow conversions between integral types and between
-          pointer types.  */
-        if ((INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (op)))
-           || (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (op))))
-         return false;
+  if (gimple_call_lhs (stmt)
+      && (!is_gimple_lvalue (gimple_call_lhs (stmt))
+         || verify_types_in_gimple_reference (gimple_call_lhs (stmt), true)))
+    {
+      error ("invalid LHS in gimple call");
+      return true;
+    }
+
+  if (gimple_call_lhs (stmt) && gimple_call_noreturn_p (stmt))
+    {
+      error ("LHS in noreturn call");
+      return true;
+    }
+
+  fntype = TREE_TYPE (TREE_TYPE (fn));
+  if (gimple_call_lhs (stmt)
+      && !useless_type_conversion_p (TREE_TYPE (gimple_call_lhs (stmt)),
+                                    TREE_TYPE (fntype))
+      /* ???  At least C++ misses conversions at assignments from
+        void * call results.
+        ???  Java is completely off.  Especially with functions
+        returning java.lang.Object.
+        For now simply allow arbitrary pointer type conversions.  */
+      && !(POINTER_TYPE_P (TREE_TYPE (gimple_call_lhs (stmt)))
+          && POINTER_TYPE_P (TREE_TYPE (fntype))))
+    {
+      error ("invalid conversion in gimple call");
+      debug_generic_stmt (TREE_TYPE (gimple_call_lhs (stmt)));
+      debug_generic_stmt (TREE_TYPE (fntype));
+      return true;
+    }
+
+  if (gimple_call_chain (stmt)
+      && !is_gimple_val (gimple_call_chain (stmt)))
+    {
+      error ("invalid static chain in gimple call");
+      debug_generic_stmt (gimple_call_chain (stmt));
+      return true;
+    }
+
+  /* If there is a static chain argument, this should not be an indirect
+     call, and the decl should have DECL_STATIC_CHAIN set.  */
+  if (gimple_call_chain (stmt))
+    {
+      if (TREE_CODE (fn) != ADDR_EXPR
+         || TREE_CODE (TREE_OPERAND (fn, 0)) != FUNCTION_DECL)
+       {
+         error ("static chain in indirect gimple call");
+         return true;
+       }
+      fn = TREE_OPERAND (fn, 0);
+
+      if (!DECL_STATIC_CHAIN (fn))
+       {
+         error ("static chain with function that doesn't use one");
+         return true;
+       }
+    }
+
+  /* ???  The C frontend passes unpromoted arguments in case it
+     didn't see a function declaration before the call.  So for now
+     leave the call arguments mostly unverified.  Once we gimplify
+     unit-at-a-time we have a chance to fix this.  */
+
+  for (i = 0; i < gimple_call_num_args (stmt); ++i)
+    {
+      tree arg = gimple_call_arg (stmt, i);
+      if (!is_gimple_operand (arg))
+       {
+         error ("invalid argument to gimple call");
+         debug_generic_expr (arg);
+       }
+    }
+
+  return false;
+}
 
+/* Verifies the gimple comparison with the result type TYPE and
+   the operands OP0 and OP1.  */
+
+static bool
+verify_gimple_comparison (tree type, tree op0, tree op1)
+{
+  tree op0_type = TREE_TYPE (op0);
+  tree op1_type = TREE_TYPE (op1);
+
+  if (!is_gimple_val (op0) || !is_gimple_val (op1))
+    {
+      error ("invalid operands in gimple comparison");
+      return true;
+    }
+
+  /* For comparisons we do not have the operations type as the
+     effective type the comparison is carried out in.  Instead
+     we require that either the first operand is trivially
+     convertible into the second, or the other way around.
+     The resulting type of a comparison may be any integral type.
+     Because we special-case pointers to void we allow
+     comparisons of pointers with the same mode as well.  */
+  if ((!useless_type_conversion_p (op0_type, op1_type)
+       && !useless_type_conversion_p (op1_type, op0_type)
+       && (!POINTER_TYPE_P (op0_type)
+          || !POINTER_TYPE_P (op1_type)
+          || TYPE_MODE (op0_type) != TYPE_MODE (op1_type)))
+      || !INTEGRAL_TYPE_P (type))
+    {
+      error ("type mismatch in comparison expression");
+      debug_generic_expr (type);
+      debug_generic_expr (op0_type);
+      debug_generic_expr (op1_type);
+      return true;
+    }
+
+  return false;
+}
+
+/* Verify a gimple assignment statement STMT with an unary rhs.
+   Returns true if anything is wrong.  */
+
+static bool
+verify_gimple_assign_unary (gimple stmt)
+{
+  enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
+  tree lhs = gimple_assign_lhs (stmt);
+  tree lhs_type = TREE_TYPE (lhs);
+  tree rhs1 = gimple_assign_rhs1 (stmt);
+  tree rhs1_type = TREE_TYPE (rhs1);
+
+  if (!is_gimple_reg (lhs)
+      && !(optimize == 0
+          && TREE_CODE (lhs_type) == COMPLEX_TYPE))
+    {
+      error ("non-register as LHS of unary operation");
+      return true;
+    }
+
+  if (!is_gimple_val (rhs1))
+    {
+      error ("invalid operand in unary operation");
+      return true;
+    }
+
+  /* First handle conversions.  */
+  switch (rhs_code)
+    {
+    CASE_CONVERT:
+      {
        /* Allow conversions between integral types and pointers only if
-          there is no sign or zero extension involved.  */
-       if (((POINTER_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (op)))
-            || (POINTER_TYPE_P (TREE_TYPE (op)) && INTEGRAL_TYPE_P (type)))
-           && (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op))
-               /* For targets were the precision of sizetype doesn't
-                  match that of pointers we need the following.  */
-               || type == sizetype || TREE_TYPE (op) == sizetype))
+          there is no sign or zero extension involved.
+          For targets were the precision of sizetype doesn't match that
+          of pointers we need to allow arbitrary conversions from and
+          to sizetype.  */
+       if ((POINTER_TYPE_P (lhs_type)
+            && INTEGRAL_TYPE_P (rhs1_type)
+            && (TYPE_PRECISION (lhs_type) >= TYPE_PRECISION (rhs1_type)
+                || rhs1_type == sizetype))
+           || (POINTER_TYPE_P (rhs1_type)
+               && INTEGRAL_TYPE_P (lhs_type)
+               && (TYPE_PRECISION (rhs1_type) >= TYPE_PRECISION (lhs_type)
+                   || lhs_type == sizetype)))
          return false;
 
        /* Allow conversion from integer to offset type and vice versa.  */
-       if ((TREE_CODE (type) == OFFSET_TYPE
-            && TREE_CODE (TREE_TYPE (op)) == INTEGER_TYPE)
-           || (TREE_CODE (type) == INTEGER_TYPE
-               && TREE_CODE (TREE_TYPE (op)) == OFFSET_TYPE))
+       if ((TREE_CODE (lhs_type) == OFFSET_TYPE
+            && TREE_CODE (rhs1_type) == INTEGER_TYPE)
+           || (TREE_CODE (lhs_type) == INTEGER_TYPE
+               && TREE_CODE (rhs1_type) == OFFSET_TYPE))
          return false;
 
        /* Otherwise assert we are converting between types of the
           same kind.  */
-       if (TREE_CODE (type) != TREE_CODE (TREE_TYPE (op)))
+       if (INTEGRAL_TYPE_P (lhs_type) != INTEGRAL_TYPE_P (rhs1_type))
          {
            error ("invalid types in nop conversion");
-           debug_generic_expr (type);
-           debug_generic_expr (TREE_TYPE (op));
+           debug_generic_expr (lhs_type);
+           debug_generic_expr (rhs1_type);
            return true;
          }
 
        return false;
       }
 
-    case FIXED_CONVERT_EXPR:
+    case ADDR_SPACE_CONVERT_EXPR:
       {
-       tree op = TREE_OPERAND (expr, 0);
-       if (!is_gimple_val (op))
+       if (!POINTER_TYPE_P (rhs1_type) || !POINTER_TYPE_P (lhs_type)
+           || (TYPE_ADDR_SPACE (TREE_TYPE (rhs1_type))
+               == TYPE_ADDR_SPACE (TREE_TYPE (lhs_type))))
          {
-           error ("invalid operand in conversion");
+           error ("invalid types in address space conversion");
+           debug_generic_expr (lhs_type);
+           debug_generic_expr (rhs1_type);
            return true;
          }
 
-       if (!valid_fixed_convert_types_p (type, TREE_TYPE (op))
-           && !valid_fixed_convert_types_p (TREE_TYPE (op), type))
+       return false;
+      }
+
+    case FIXED_CONVERT_EXPR:
+      {
+       if (!valid_fixed_convert_types_p (lhs_type, rhs1_type)
+           && !valid_fixed_convert_types_p (rhs1_type, lhs_type))
          {
            error ("invalid types in fixed-point conversion");
-           debug_generic_expr (type);
-           debug_generic_expr (TREE_TYPE (op));
+           debug_generic_expr (lhs_type);
+           debug_generic_expr (rhs1_type);
            return true;
          }
 
@@ -3709,485 +3168,709 @@ verify_gimple_expr (tree expr)
 
     case FLOAT_EXPR:
       {
-       tree op = TREE_OPERAND (expr, 0);
-       if (!is_gimple_val (op))
-         {
-           error ("invalid operand in int to float conversion");
-           return true;
-         }
-       if (!INTEGRAL_TYPE_P (TREE_TYPE (op))
-           || !SCALAR_FLOAT_TYPE_P (type))
+       if (!INTEGRAL_TYPE_P (rhs1_type) || !SCALAR_FLOAT_TYPE_P (lhs_type))
          {
            error ("invalid types in conversion to floating point");
-           debug_generic_expr (type);
-           debug_generic_expr (TREE_TYPE (op));
+           debug_generic_expr (lhs_type);
+           debug_generic_expr (rhs1_type);
            return true;
          }
+
         return false;
       }
 
     case FIX_TRUNC_EXPR:
       {
-       tree op = TREE_OPERAND (expr, 0);
-       if (!is_gimple_val (op))
-         {
-           error ("invalid operand in float to int conversion");
-           return true;
-         }
-       if (!INTEGRAL_TYPE_P (type)
-           || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (op)))
+       if (!INTEGRAL_TYPE_P (lhs_type) || !SCALAR_FLOAT_TYPE_P (rhs1_type))
          {
            error ("invalid types in conversion to integer");
-           debug_generic_expr (type);
-           debug_generic_expr (TREE_TYPE (op));
+           debug_generic_expr (lhs_type);
+           debug_generic_expr (rhs1_type);
            return true;
          }
+
         return false;
       }
 
+    case VEC_UNPACK_HI_EXPR:
+    case VEC_UNPACK_LO_EXPR:
+    case REDUC_MAX_EXPR:
+    case REDUC_MIN_EXPR:
+    case REDUC_PLUS_EXPR:
+    case VEC_UNPACK_FLOAT_HI_EXPR:
+    case VEC_UNPACK_FLOAT_LO_EXPR:
+      /* FIXME.  */
+      return false;
+
+    case TRUTH_NOT_EXPR:
+    case NEGATE_EXPR:
+    case ABS_EXPR:
+    case BIT_NOT_EXPR:
+    case PAREN_EXPR:
+    case NON_LVALUE_EXPR:
+    case CONJ_EXPR:
+      break;
+
+    default:
+      gcc_unreachable ();
+    }
+
+  /* For the remaining codes assert there is no conversion involved.  */
+  if (!useless_type_conversion_p (lhs_type, rhs1_type))
+    {
+      error ("non-trivial conversion in unary operation");
+      debug_generic_expr (lhs_type);
+      debug_generic_expr (rhs1_type);
+      return true;
+    }
+
+  return false;
+}
+
+/* Verify a gimple assignment statement STMT with a binary rhs.
+   Returns true if anything is wrong.  */
+
+static bool
+verify_gimple_assign_binary (gimple stmt)
+{
+  enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
+  tree lhs = gimple_assign_lhs (stmt);
+  tree lhs_type = TREE_TYPE (lhs);
+  tree rhs1 = gimple_assign_rhs1 (stmt);
+  tree rhs1_type = TREE_TYPE (rhs1);
+  tree rhs2 = gimple_assign_rhs2 (stmt);
+  tree rhs2_type = TREE_TYPE (rhs2);
+
+  if (!is_gimple_reg (lhs)
+      && !(optimize == 0
+          && TREE_CODE (lhs_type) == COMPLEX_TYPE))
+    {
+      error ("non-register as LHS of binary operation");
+      return true;
+    }
+
+  if (!is_gimple_val (rhs1)
+      || !is_gimple_val (rhs2))
+    {
+      error ("invalid operands in binary operation");
+      return true;
+    }
+
+  /* First handle operations that involve different types.  */
+  switch (rhs_code)
+    {
     case COMPLEX_EXPR:
       {
-       tree op0 = TREE_OPERAND (expr, 0);
-       tree op1 = TREE_OPERAND (expr, 1);
-       if (!is_gimple_val (op0) || !is_gimple_val (op1))
-         {
-           error ("invalid operands in complex expression");
-           return true;
-         }
-       if (!TREE_CODE (type) == COMPLEX_TYPE
-           || !(TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
-                || SCALAR_FLOAT_TYPE_P (TREE_TYPE (op0)))
-           || !(TREE_CODE (TREE_TYPE (op1)) == INTEGER_TYPE
-                || SCALAR_FLOAT_TYPE_P (TREE_TYPE (op1)))
-           || !useless_type_conversion_p (TREE_TYPE (type),
-                                          TREE_TYPE (op0))
-           || !useless_type_conversion_p (TREE_TYPE (type),
-                                          TREE_TYPE (op1)))
+       if (TREE_CODE (lhs_type) != COMPLEX_TYPE
+           || !(INTEGRAL_TYPE_P (rhs1_type)
+                || SCALAR_FLOAT_TYPE_P (rhs1_type))
+           || !(INTEGRAL_TYPE_P (rhs2_type)
+                || SCALAR_FLOAT_TYPE_P (rhs2_type)))
          {
            error ("type mismatch in complex expression");
-           debug_generic_stmt (TREE_TYPE (expr));
-           debug_generic_stmt (TREE_TYPE (op0));
-           debug_generic_stmt (TREE_TYPE (op1));
+           debug_generic_expr (lhs_type);
+           debug_generic_expr (rhs1_type);
+           debug_generic_expr (rhs2_type);
            return true;
          }
+
        return false;
       }
 
-    case CONSTRUCTOR:
+    case LSHIFT_EXPR:
+    case RSHIFT_EXPR:
+    case LROTATE_EXPR:
+    case RROTATE_EXPR:
       {
-       /* This is used like COMPLEX_EXPR but for vectors.  */
-       if (TREE_CODE (type) != VECTOR_TYPE)
+       /* Shifts and rotates are ok on integral types, fixed point
+          types and integer vector types.  */
+       if ((!INTEGRAL_TYPE_P (rhs1_type)
+            && !FIXED_POINT_TYPE_P (rhs1_type)
+            && !(TREE_CODE (rhs1_type) == VECTOR_TYPE
+                 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))))
+           || (!INTEGRAL_TYPE_P (rhs2_type)
+               /* Vector shifts of vectors are also ok.  */
+               && !(TREE_CODE (rhs1_type) == VECTOR_TYPE
+                    && INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
+                    && TREE_CODE (rhs2_type) == VECTOR_TYPE
+                    && INTEGRAL_TYPE_P (TREE_TYPE (rhs2_type))))
+           || !useless_type_conversion_p (lhs_type, rhs1_type))
          {
-           error ("constructor not allowed for non-vector types");
-           debug_generic_stmt (type);
+           error ("type mismatch in shift expression");
+           debug_generic_expr (lhs_type);
+           debug_generic_expr (rhs1_type);
+           debug_generic_expr (rhs2_type);
            return true;
          }
-       /* FIXME: verify constructor arguments.  */
+
        return false;
       }
 
-    case LSHIFT_EXPR:
-    case RSHIFT_EXPR:
-    case LROTATE_EXPR:
-    case RROTATE_EXPR:
+    case VEC_LSHIFT_EXPR:
+    case VEC_RSHIFT_EXPR:
       {
-       tree op0 = TREE_OPERAND (expr, 0);
-       tree op1 = TREE_OPERAND (expr, 1);
-       if (!is_gimple_val (op0) || !is_gimple_val (op1))
+       if (TREE_CODE (rhs1_type) != VECTOR_TYPE
+           || !(INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
+                || FIXED_POINT_TYPE_P (TREE_TYPE (rhs1_type))
+                || SCALAR_FLOAT_TYPE_P (TREE_TYPE (rhs1_type)))
+           || (!INTEGRAL_TYPE_P (rhs2_type)
+               && (TREE_CODE (rhs2_type) != VECTOR_TYPE
+                   || !INTEGRAL_TYPE_P (TREE_TYPE (rhs2_type))))
+           || !useless_type_conversion_p (lhs_type, rhs1_type))
          {
-           error ("invalid operands in shift expression");
+           error ("type mismatch in vector shift expression");
+           debug_generic_expr (lhs_type);
+           debug_generic_expr (rhs1_type);
+           debug_generic_expr (rhs2_type);
            return true;
          }
-       if (!TREE_CODE (TREE_TYPE (op1)) == INTEGER_TYPE
-           || !useless_type_conversion_p (type, TREE_TYPE (op0)))
+       /* For shifting a vector of floating point components we
+          only allow shifting by a constant multiple of the element size.  */
+       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (rhs1_type))
+           && (TREE_CODE (rhs2) != INTEGER_CST
+               || !div_if_zero_remainder (EXACT_DIV_EXPR, rhs2,
+                                          TYPE_SIZE (TREE_TYPE (rhs1_type)))))
          {
-           error ("type mismatch in shift expression");
-           debug_generic_stmt (TREE_TYPE (expr));
-           debug_generic_stmt (TREE_TYPE (op0));
-           debug_generic_stmt (TREE_TYPE (op1));
+           error ("non-element sized vector shift of floating point vector");
            return true;
          }
+
        return false;
       }
 
     case PLUS_EXPR:
+      {
+       /* We use regular PLUS_EXPR for vectors.
+          ???  This just makes the checker happy and may not be what is
+          intended.  */
+       if (TREE_CODE (lhs_type) == VECTOR_TYPE
+           && POINTER_TYPE_P (TREE_TYPE (lhs_type)))
+         {
+           if (TREE_CODE (rhs1_type) != VECTOR_TYPE
+               || TREE_CODE (rhs2_type) != VECTOR_TYPE)
+             {
+               error ("invalid non-vector operands to vector valued plus");
+               return true;
+             }
+           lhs_type = TREE_TYPE (lhs_type);
+           rhs1_type = TREE_TYPE (rhs1_type);
+           rhs2_type = TREE_TYPE (rhs2_type);
+           /* PLUS_EXPR is commutative, so we might end up canonicalizing
+              the pointer to 2nd place.  */
+           if (POINTER_TYPE_P (rhs2_type))
+             {
+               tree tem = rhs1_type;
+               rhs1_type = rhs2_type;
+               rhs2_type = tem;
+             }
+           goto do_pointer_plus_expr_check;
+         }
+      }
+    /* Fallthru.  */
     case MINUS_EXPR:
       {
-       tree op0 = TREE_OPERAND (expr, 0);
-       tree op1 = TREE_OPERAND (expr, 1);
-       if (POINTER_TYPE_P (type)
-           || POINTER_TYPE_P (TREE_TYPE (op0))
-           || POINTER_TYPE_P (TREE_TYPE (op1)))
+       if (POINTER_TYPE_P (lhs_type)
+           || POINTER_TYPE_P (rhs1_type)
+           || POINTER_TYPE_P (rhs2_type))
          {
            error ("invalid (pointer) operands to plus/minus");
            return true;
          }
+
        /* Continue with generic binary expression handling.  */
        break;
       }
 
     case POINTER_PLUS_EXPR:
       {
-       tree op0 = TREE_OPERAND (expr, 0);
-       tree op1 = TREE_OPERAND (expr, 1);
-       if (!is_gimple_val (op0) || !is_gimple_val (op1))
-         {
-           error ("invalid operands in pointer plus expression");
-           return true;
-         }
-       if (!POINTER_TYPE_P (TREE_TYPE (op0))
-           || !useless_type_conversion_p (type, TREE_TYPE (op0))
-           || !useless_type_conversion_p (sizetype, TREE_TYPE (op1)))
+do_pointer_plus_expr_check:
+       if (!POINTER_TYPE_P (rhs1_type)
+           || !useless_type_conversion_p (lhs_type, rhs1_type)
+           || !useless_type_conversion_p (sizetype, rhs2_type))
          {
            error ("type mismatch in pointer plus expression");
-           debug_generic_stmt (type);
-           debug_generic_stmt (TREE_TYPE (op0));
-           debug_generic_stmt (TREE_TYPE (op1));
+           debug_generic_stmt (lhs_type);
+           debug_generic_stmt (rhs1_type);
+           debug_generic_stmt (rhs2_type);
            return true;
          }
+
        return false;
       }
 
-    case COND_EXPR:
-      {
-       tree op0 = TREE_OPERAND (expr, 0);
-       tree op1 = TREE_OPERAND (expr, 1);
-       tree op2 = TREE_OPERAND (expr, 2);
-       if ((!is_gimple_val (op1)
-            && TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
-           || (!is_gimple_val (op2)
-               && TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE))
-         {
-           error ("invalid operands in conditional expression");
-           return true;
-         }
-       if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
-           || (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE
-               && !useless_type_conversion_p (type, TREE_TYPE (op1)))
-           || (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE
-               && !useless_type_conversion_p (type, TREE_TYPE (op2))))
-         {
-           error ("type mismatch in conditional expression");
-           debug_generic_stmt (type);
-           debug_generic_stmt (TREE_TYPE (op0));
-           debug_generic_stmt (TREE_TYPE (op1));
-           debug_generic_stmt (TREE_TYPE (op2));
-           return true;
-         }
-       return verify_gimple_expr (op0);
-      }
+    case TRUTH_ANDIF_EXPR:
+    case TRUTH_ORIF_EXPR:
+      gcc_unreachable ();
 
-    case ADDR_EXPR:
+    case TRUTH_AND_EXPR:
+    case TRUTH_OR_EXPR:
+    case TRUTH_XOR_EXPR:
       {
-       tree op = TREE_OPERAND (expr, 0);
-       if (!is_gimple_addressable (op))
-         {
-           error ("invalid operand in unary expression");
-           return true;
-         }
-       if (!one_pointer_to_useless_type_conversion_p (type, TREE_TYPE (op))
-           /* FIXME: a longstanding wart, &a == &a[0].  */
-           && (TREE_CODE (TREE_TYPE (op)) != ARRAY_TYPE
-               || !one_pointer_to_useless_type_conversion_p (type,
-                     TREE_TYPE (TREE_TYPE (op)))))
+       /* We allow any kind of integral typed argument and result.  */
+       if (!INTEGRAL_TYPE_P (rhs1_type)
+           || !INTEGRAL_TYPE_P (rhs2_type)
+           || !INTEGRAL_TYPE_P (lhs_type))
          {
-           error ("type mismatch in address expression");
-           debug_generic_stmt (TREE_TYPE (expr));
-           debug_generic_stmt (TYPE_POINTER_TO (TREE_TYPE (op)));
+           error ("type mismatch in binary truth expression");
+           debug_generic_expr (lhs_type);
+           debug_generic_expr (rhs1_type);
+           debug_generic_expr (rhs2_type);
            return true;
          }
 
-       return verify_gimple_reference (op);
+       return false;
       }
 
-    case TRUTH_ANDIF_EXPR:
-    case TRUTH_ORIF_EXPR:
+    case LT_EXPR:
+    case LE_EXPR:
+    case GT_EXPR:
+    case GE_EXPR:
+    case EQ_EXPR:
+    case NE_EXPR:
+    case UNORDERED_EXPR:
+    case ORDERED_EXPR:
+    case UNLT_EXPR:
+    case UNLE_EXPR:
+    case UNGT_EXPR:
+    case UNGE_EXPR:
+    case UNEQ_EXPR:
+    case LTGT_EXPR:
+      /* Comparisons are also binary, but the result type is not
+        connected to the operand types.  */
+      return verify_gimple_comparison (lhs_type, rhs1, rhs2);
+
+    case WIDEN_SUM_EXPR:
+    case WIDEN_MULT_EXPR:
+    case VEC_WIDEN_MULT_HI_EXPR:
+    case VEC_WIDEN_MULT_LO_EXPR:
+    case VEC_PACK_TRUNC_EXPR:
+    case VEC_PACK_SAT_EXPR:
+    case VEC_PACK_FIX_TRUNC_EXPR:
+    case VEC_EXTRACT_EVEN_EXPR:
+    case VEC_EXTRACT_ODD_EXPR:
+    case VEC_INTERLEAVE_HIGH_EXPR:
+    case VEC_INTERLEAVE_LOW_EXPR:
+      /* FIXME.  */
+      return false;
+
+    case MULT_EXPR:
+    case TRUNC_DIV_EXPR:
+    case CEIL_DIV_EXPR:
+    case FLOOR_DIV_EXPR:
+    case ROUND_DIV_EXPR:
+    case TRUNC_MOD_EXPR:
+    case CEIL_MOD_EXPR:
+    case FLOOR_MOD_EXPR:
+    case ROUND_MOD_EXPR:
+    case RDIV_EXPR:
+    case EXACT_DIV_EXPR:
+    case MIN_EXPR:
+    case MAX_EXPR:
+    case BIT_IOR_EXPR:
+    case BIT_XOR_EXPR:
+    case BIT_AND_EXPR:
+      /* Continue with generic binary expression handling.  */
+      break;
+
+    default:
       gcc_unreachable ();
+    }
 
-    case TRUTH_AND_EXPR:
-    case TRUTH_OR_EXPR:
-    case TRUTH_XOR_EXPR:
-      {
-       tree op0 = TREE_OPERAND (expr, 0);
-       tree op1 = TREE_OPERAND (expr, 1);
+  if (!useless_type_conversion_p (lhs_type, rhs1_type)
+      || !useless_type_conversion_p (lhs_type, rhs2_type))
+    {
+      error ("type mismatch in binary expression");
+      debug_generic_stmt (lhs_type);
+      debug_generic_stmt (rhs1_type);
+      debug_generic_stmt (rhs2_type);
+      return true;
+    }
 
-       if (!is_gimple_val (op0) || !is_gimple_val (op1))
-         {
-           error ("invalid operands in truth expression");
-           return true;
-         }
+  return false;
+}
 
-       /* We allow any kind of integral typed argument and result.  */
-       if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
-           || !INTEGRAL_TYPE_P (TREE_TYPE (op1))
-           || !INTEGRAL_TYPE_P (type))
-         {
-           error ("type mismatch in binary truth expression");
-           debug_generic_stmt (type);
-           debug_generic_stmt (TREE_TYPE (op0));
-           debug_generic_stmt (TREE_TYPE (op1));
-           return true;
-         }
+/* Verify a gimple assignment statement STMT with a single rhs.
+   Returns true if anything is wrong.  */
 
-       return false;
-      }
+static bool
+verify_gimple_assign_single (gimple stmt)
+{
+  enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
+  tree lhs = gimple_assign_lhs (stmt);
+  tree lhs_type = TREE_TYPE (lhs);
+  tree rhs1 = gimple_assign_rhs1 (stmt);
+  tree rhs1_type = TREE_TYPE (rhs1);
+  bool res = false;
 
-    case TRUTH_NOT_EXPR:
-      {
-       tree op = TREE_OPERAND (expr, 0);
+  if (!useless_type_conversion_p (lhs_type, rhs1_type))
+    {
+      error ("non-trivial conversion at assignment");
+      debug_generic_expr (lhs_type);
+      debug_generic_expr (rhs1_type);
+      return true;
+    }
+
+  if (handled_component_p (lhs))
+    res |= verify_types_in_gimple_reference (lhs, true);
 
-       if (!is_gimple_val (op))
+  /* Special codes we cannot handle via their class.  */
+  switch (rhs_code)
+    {
+    case ADDR_EXPR:
+      {
+       tree op = TREE_OPERAND (rhs1, 0);
+       if (!is_gimple_addressable (op))
          {
-           error ("invalid operand in unary not");
+           error ("invalid operand in unary expression");
            return true;
          }
 
-       /* For TRUTH_NOT_EXPR we can have any kind of integral
-          typed arguments and results.  */
-       if (!INTEGRAL_TYPE_P (TREE_TYPE (op))
-           || !INTEGRAL_TYPE_P (type))
+       if (!types_compatible_p (TREE_TYPE (op), TREE_TYPE (TREE_TYPE (rhs1)))
+           && !one_pointer_to_useless_type_conversion_p (TREE_TYPE (rhs1),
+                                                         TREE_TYPE (op)))
          {
-           error ("type mismatch in not expression");
-           debug_generic_expr (TREE_TYPE (expr));
-           debug_generic_expr (TREE_TYPE (op));
+           error ("type mismatch in address expression");
+           debug_generic_stmt (TREE_TYPE (rhs1));
+           debug_generic_stmt (TREE_TYPE (op));
            return true;
          }
 
-       return false;
+       return verify_types_in_gimple_reference (op, true);
       }
 
-    case CALL_EXPR:
-      /* FIXME.  The C frontend passes unpromoted arguments in case it
-        didn't see a function declaration before the call.  */
-      {
-       tree decl = CALL_EXPR_FN (expr);
+    /* tcc_reference  */
+    case COMPONENT_REF:
+    case BIT_FIELD_REF:
+    case INDIRECT_REF:
+    case ALIGN_INDIRECT_REF:
+    case MISALIGNED_INDIRECT_REF:
+    case ARRAY_REF:
+    case ARRAY_RANGE_REF:
+    case VIEW_CONVERT_EXPR:
+    case REALPART_EXPR:
+    case IMAGPART_EXPR:
+    case TARGET_MEM_REF:
+      if (!is_gimple_reg (lhs)
+         && is_gimple_reg_type (TREE_TYPE (lhs)))
+       {
+         error ("invalid rhs for gimple memory store");
+         debug_generic_stmt (lhs);
+         debug_generic_stmt (rhs1);
+         return true;
+       }
+      return res || verify_types_in_gimple_reference (rhs1, false);
 
-       if (TREE_CODE (decl) == FUNCTION_DECL 
-           && DECL_LOOPING_CONST_OR_PURE_P (decl)
-           && (!DECL_PURE_P (decl))
-           && (!TREE_READONLY (decl)))
-         {
-           error ("invalid pure const state for function");
-           return true;
-         }
-       return false;
-      }
+    /* tcc_constant  */
+    case SSA_NAME:
+    case INTEGER_CST:
+    case REAL_CST:
+    case FIXED_CST:
+    case COMPLEX_CST:
+    case VECTOR_CST:
+    case STRING_CST:
+      return res;
+
+    /* tcc_declaration  */
+    case CONST_DECL:
+      return res;
+    case VAR_DECL:
+    case PARM_DECL:
+      if (!is_gimple_reg (lhs)
+         && !is_gimple_reg (rhs1)
+         && is_gimple_reg_type (TREE_TYPE (lhs)))
+       {
+         error ("invalid rhs for gimple memory store");
+         debug_generic_stmt (lhs);
+         debug_generic_stmt (rhs1);
+         return true;
+       }
+      return res;
 
+    case COND_EXPR:
+    case CONSTRUCTOR:
     case OBJ_TYPE_REF:
+    case ASSERT_EXPR:
+    case WITH_SIZE_EXPR:
+    case POLYNOMIAL_CHREC:
+    case DOT_PROD_EXPR:
+    case VEC_COND_EXPR:
+    case REALIGN_LOAD_EXPR:
       /* FIXME.  */
-      return false;
+      return res;
 
     default:;
     }
 
-  /* Generic handling via classes.  */
-  switch (TREE_CODE_CLASS (TREE_CODE (expr)))
-    {
-    case tcc_unary:
-      return verify_gimple_unary_expr (expr);
+  return res;
+}
 
-    case tcc_binary:
-      return verify_gimple_binary_expr (expr);
+/* Verify the contents of a GIMPLE_ASSIGN STMT.  Returns true when there
+   is a problem, otherwise false.  */
 
-    case tcc_reference:
-      return verify_gimple_reference (expr);
+static bool
+verify_gimple_assign (gimple stmt)
+{
+  switch (gimple_assign_rhs_class (stmt))
+    {
+    case GIMPLE_SINGLE_RHS:
+      return verify_gimple_assign_single (stmt);
 
-    case tcc_comparison:
-      {
-       tree op0 = TREE_OPERAND (expr, 0);
-       tree op1 = TREE_OPERAND (expr, 1);
-       if (!is_gimple_val (op0) || !is_gimple_val (op1))
-         {
-           error ("invalid operands in comparison expression");
-           return true;
-         }
-       /* For comparisons we do not have the operations type as the
-          effective type the comparison is carried out in.  Instead
-          we require that either the first operand is trivially
-          convertible into the second, or the other way around.
-          The resulting type of a comparison may be any integral type.
-          Because we special-case pointers to void we allow
-          comparisons of pointers with the same mode as well.  */
-       if ((!useless_type_conversion_p (TREE_TYPE (op0), TREE_TYPE (op1))
-            && !useless_type_conversion_p (TREE_TYPE (op1), TREE_TYPE (op0))
-            && (!POINTER_TYPE_P (TREE_TYPE (op0))
-                || !POINTER_TYPE_P (TREE_TYPE (op1))
-                || TYPE_MODE (TREE_TYPE (op0)) != TYPE_MODE (TREE_TYPE (op1))))
-           || !INTEGRAL_TYPE_P (type))
-         {
-           error ("type mismatch in comparison expression");
-           debug_generic_stmt (TREE_TYPE (expr));
-           debug_generic_stmt (TREE_TYPE (op0));
-           debug_generic_stmt (TREE_TYPE (op1));
-           return true;
-         }
-        break;
-      }
+    case GIMPLE_UNARY_RHS:
+      return verify_gimple_assign_unary (stmt);
+
+    case GIMPLE_BINARY_RHS:
+      return verify_gimple_assign_binary (stmt);
 
     default:
       gcc_unreachable ();
     }
-
-  return false;
 }
 
-/* Verify the GIMPLE assignment statement STMT.  Returns true if there
-   is an error, otherwise false.  */
+/* Verify the contents of a GIMPLE_RETURN STMT.  Returns true when there
+   is a problem, otherwise false.  */
 
 static bool
-verify_gimple_modify_stmt (const_tree stmt)
+verify_gimple_return (gimple stmt)
 {
-  tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
-  tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
+  tree op = gimple_return_retval (stmt);
+  tree restype = TREE_TYPE (TREE_TYPE (cfun->decl));
 
-  gcc_assert (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT);
+  /* We cannot test for present return values as we do not fix up missing
+     return values from the original source.  */
+  if (op == NULL)
+    return false;
 
-  if (!useless_type_conversion_p (TREE_TYPE (lhs),
-                                 TREE_TYPE (rhs)))
+  if (!is_gimple_val (op)
+      && TREE_CODE (op) != RESULT_DECL)
     {
-      error ("non-trivial conversion at assignment");
-      debug_generic_expr (TREE_TYPE (lhs));
-      debug_generic_expr (TREE_TYPE (rhs));
+      error ("invalid operand in return statement");
+      debug_generic_stmt (op);
       return true;
     }
 
-  /* Loads/stores from/to a variable are ok.  */
-  if ((is_gimple_val (lhs)
-       && is_gimple_variable (rhs))
-      || (is_gimple_val (rhs)
-         && is_gimple_variable (lhs)))
-    return false;
+  if (!useless_type_conversion_p (restype, TREE_TYPE (op))
+      /* ???  With C++ we can have the situation that the result
+        decl is a reference type while the return type is an aggregate.  */
+      && !(TREE_CODE (op) == RESULT_DECL
+          && TREE_CODE (TREE_TYPE (op)) == REFERENCE_TYPE
+          && useless_type_conversion_p (restype, TREE_TYPE (TREE_TYPE (op)))))
+    {
+      error ("invalid conversion in return statement");
+      debug_generic_stmt (restype);
+      debug_generic_stmt (TREE_TYPE (op));
+      return true;
+    }
 
-  /* Aggregate copies are ok.  */
-  if (!is_gimple_reg_type (TREE_TYPE (lhs))
-      && !is_gimple_reg_type (TREE_TYPE (rhs)))
-    return false;
+  return false;
+}
 
-  /* We might get 'loads' from a parameter which is not a gimple value.  */
-  if (TREE_CODE (rhs) == PARM_DECL)
-    return verify_gimple_expr (lhs);
 
-  if (!is_gimple_variable (lhs)
-      && verify_gimple_expr (lhs))
-    return true;
+/* Verify the contents of a GIMPLE_GOTO STMT.  Returns true when there
+   is a problem, otherwise false.  */
 
-  if (!is_gimple_variable (rhs)
-      && verify_gimple_expr (rhs))
-    return true;
+static bool
+verify_gimple_goto (gimple stmt)
+{
+  tree dest = gimple_goto_dest (stmt);
+
+  /* ???  We have two canonical forms of direct goto destinations, a
+     bare LABEL_DECL and an ADDR_EXPR of a LABEL_DECL.  */
+  if (TREE_CODE (dest) != LABEL_DECL
+      && (!is_gimple_val (dest)
+         || !POINTER_TYPE_P (TREE_TYPE (dest))))
+    {
+      error ("goto destination is neither a label nor a pointer");
+      return true;
+    }
+
+  return false;
+}
+
+/* Verify the contents of a GIMPLE_SWITCH STMT.  Returns true when there
+   is a problem, otherwise false.  */
+
+static bool
+verify_gimple_switch (gimple stmt)
+{
+  if (!is_gimple_val (gimple_switch_index (stmt)))
+    {
+      error ("invalid operand to switch statement");
+      debug_generic_stmt (gimple_switch_index (stmt));
+      return true;
+    }
 
   return false;
 }
 
-/* Verify the GIMPLE statement STMT.  Returns true if there is an
-   error, otherwise false.  */
+
+/* Verify the contents of a GIMPLE_PHI.  Returns true if there is a problem,
+   and false otherwise.  */
 
 static bool
-verify_gimple_stmt (tree stmt)
+verify_gimple_phi (gimple stmt)
 {
-  if (!is_gimple_stmt (stmt))
+  tree type = TREE_TYPE (gimple_phi_result (stmt));
+  unsigned i;
+
+  if (TREE_CODE (gimple_phi_result (stmt)) != SSA_NAME)
     {
-      error ("is not a valid GIMPLE statement");
+      error ("Invalid PHI result");
       return true;
     }
 
-  if (OMP_DIRECTIVE_P (stmt))
+  for (i = 0; i < gimple_phi_num_args (stmt); i++)
     {
-      /* OpenMP directives are validated by the FE and never operated
-        on by the optimizers.  Furthermore, OMP_FOR may contain
-        non-gimple expressions when the main index variable has had
-        its address taken.  This does not affect the loop itself
-        because the header of an OMP_FOR is merely used to determine
-        how to setup the parallel iteration.  */
-      return false;
+      tree arg = gimple_phi_arg_def (stmt, i);
+      if ((is_gimple_reg (gimple_phi_result (stmt))
+          && !is_gimple_val (arg))
+         || (!is_gimple_reg (gimple_phi_result (stmt))
+             && !is_gimple_addressable (arg)))
+       {
+         error ("Invalid PHI argument");
+         debug_generic_stmt (arg);
+         return true;
+       }
+      if (!useless_type_conversion_p (type, TREE_TYPE (arg)))
+       {
+         error ("Incompatible types in PHI argument %u", i);
+         debug_generic_stmt (type);
+         debug_generic_stmt (TREE_TYPE (arg));
+         return true;
+       }
     }
 
-  switch (TREE_CODE (stmt))
+  return false;
+}
+
+
+/* Verify a gimple debug statement STMT.
+   Returns true if anything is wrong.  */
+
+static bool
+verify_gimple_debug (gimple stmt ATTRIBUTE_UNUSED)
+{
+  /* There isn't much that could be wrong in a gimple debug stmt.  A
+     gimple debug bind stmt, for example, maps a tree, that's usually
+     a VAR_DECL or a PARM_DECL, but that could also be some scalarized
+     component or member of an aggregate type, to another tree, that
+     can be an arbitrary expression.  These stmts expand into debug
+     insns, and are converted to debug notes by var-tracking.c.  */
+  return false;
+}
+
+
+/* Verify the GIMPLE statement STMT.  Returns true if there is an
+   error, otherwise false.  */
+
+static bool
+verify_types_in_gimple_stmt (gimple stmt)
+{
+  switch (gimple_code (stmt))
     {
-    case GIMPLE_MODIFY_STMT:
-      return verify_gimple_modify_stmt (stmt);
+    case GIMPLE_ASSIGN:
+      return verify_gimple_assign (stmt);
 
-    case GOTO_EXPR:
-    case LABEL_EXPR:
-      return false;
+    case GIMPLE_LABEL:
+      return TREE_CODE (gimple_label_label (stmt)) != LABEL_DECL;
 
-    case SWITCH_EXPR:
-      if (!is_gimple_val (TREE_OPERAND (stmt, 0)))
+    case GIMPLE_CALL:
+      return verify_gimple_call (stmt);
+
+    case GIMPLE_COND:
+      if (TREE_CODE_CLASS (gimple_cond_code (stmt)) != tcc_comparison)
+       {
+         error ("invalid comparison code in gimple cond");
+         return true;
+       }
+      if (!(!gimple_cond_true_label (stmt)
+           || TREE_CODE (gimple_cond_true_label (stmt)) == LABEL_DECL)
+         || !(!gimple_cond_false_label (stmt)
+              || TREE_CODE (gimple_cond_false_label (stmt)) == LABEL_DECL))
        {
-         error ("invalid operand to switch statement");
-         debug_generic_expr (TREE_OPERAND (stmt, 0));
+         error ("invalid labels in gimple cond");
+         return true;
        }
-      return false;
+         
+      return verify_gimple_comparison (boolean_type_node,
+                                      gimple_cond_lhs (stmt),
+                                      gimple_cond_rhs (stmt));
 
-    case RETURN_EXPR:
-      {
-       tree op = TREE_OPERAND (stmt, 0);
+    case GIMPLE_GOTO:
+      return verify_gimple_goto (stmt);
 
-       if (TREE_CODE (TREE_TYPE (stmt)) != VOID_TYPE)
-         {
-           error ("type error in return expression");
-           return true;
-         }
+    case GIMPLE_SWITCH:
+      return verify_gimple_switch (stmt);
 
-       if (op == NULL_TREE
-           || TREE_CODE (op) == RESULT_DECL)
-         return false;
+    case GIMPLE_RETURN:
+      return verify_gimple_return (stmt);
 
-       return verify_gimple_modify_stmt (op);
-      }
+    case GIMPLE_ASM:
+      return false;
 
-    case CALL_EXPR:
-    case COND_EXPR:
-      return verify_gimple_expr (stmt);
+    case GIMPLE_PHI:
+      return verify_gimple_phi (stmt);
+
+    /* Tuples that do not have tree operands.  */
+    case GIMPLE_NOP:
+    case GIMPLE_PREDICT:
+    case GIMPLE_RESX:
+    case GIMPLE_EH_DISPATCH:
+    case GIMPLE_EH_MUST_NOT_THROW:
+      return false;
 
-    case NOP_EXPR:
-    case CHANGE_DYNAMIC_TYPE_EXPR:
-    case ASM_EXPR:
-    case PREDICT_EXPR:
+    CASE_GIMPLE_OMP:
+      /* OpenMP directives are validated by the FE and never operated
+        on by the optimizers.  Furthermore, GIMPLE_OMP_FOR may contain
+        non-gimple expressions when the main index variable has had
+        its address taken.  This does not affect the loop itself
+        because the header of an GIMPLE_OMP_FOR is merely used to determine
+        how to setup the parallel iteration.  */
       return false;
 
+    case GIMPLE_DEBUG:
+      return verify_gimple_debug (stmt);
+
     default:
       gcc_unreachable ();
     }
 }
 
-/* Verify the GIMPLE statements inside the statement list STMTS.
-   Returns true if there were any errors.  */
+/* Verify the GIMPLE statements inside the sequence STMTS.  */
 
 static bool
-verify_gimple_2 (tree stmts)
+verify_types_in_gimple_seq_2 (gimple_seq stmts)
 {
-  tree_stmt_iterator tsi;
+  gimple_stmt_iterator ittr;
   bool err = false;
 
-  for (tsi = tsi_start (stmts); !tsi_end_p (tsi); tsi_next (&tsi))
+  for (ittr = gsi_start (stmts); !gsi_end_p (ittr); gsi_next (&ittr))
     {
-      tree stmt = tsi_stmt (tsi);
+      gimple stmt = gsi_stmt (ittr);
 
-      switch (TREE_CODE (stmt))
-       {
-       case BIND_EXPR:
-         err |= verify_gimple_2 (BIND_EXPR_BODY (stmt));
+      switch (gimple_code (stmt))
+        {
+       case GIMPLE_BIND:
+         err |= verify_types_in_gimple_seq_2 (gimple_bind_body (stmt));
          break;
 
-       case TRY_CATCH_EXPR:
-       case TRY_FINALLY_EXPR:
-         err |= verify_gimple_2 (TREE_OPERAND (stmt, 0));
-         err |= verify_gimple_2 (TREE_OPERAND (stmt, 1));
+       case GIMPLE_TRY:
+         err |= verify_types_in_gimple_seq_2 (gimple_try_eval (stmt));
+         err |= verify_types_in_gimple_seq_2 (gimple_try_cleanup (stmt));
          break;
 
-       case CATCH_EXPR:
-         err |= verify_gimple_2 (CATCH_BODY (stmt));
+       case GIMPLE_EH_FILTER:
+         err |= verify_types_in_gimple_seq_2 (gimple_eh_filter_failure (stmt));
          break;
 
-       case EH_FILTER_EXPR:
-         err |= verify_gimple_2 (EH_FILTER_FAILURE (stmt));
+       case GIMPLE_CATCH:
+         err |= verify_types_in_gimple_seq_2 (gimple_catch_handler (stmt));
          break;
 
        default:
          {
-           bool err2 = verify_gimple_stmt (stmt);
+           bool err2 = verify_types_in_gimple_stmt (stmt);
            if (err2)
-             debug_generic_expr (stmt);
+             debug_gimple_stmt (stmt);
            err |= err2;
          }
        }
@@ -4200,54 +3883,70 @@ verify_gimple_2 (tree stmts)
 /* Verify the GIMPLE statements inside the statement list STMTS.  */
 
 void
-verify_gimple_1 (tree stmts)
+verify_types_in_gimple_seq (gimple_seq stmts)
 {
-  if (verify_gimple_2 (stmts))
+  if (verify_types_in_gimple_seq_2 (stmts))
     internal_error ("verify_gimple failed");
 }
 
-/* Verify the GIMPLE statements inside the current function.  */
-
-void
-verify_gimple (void)
-{
-  verify_gimple_1 (BIND_EXPR_BODY (DECL_SAVED_TREE (cfun->decl)));
-}
 
 /* Verify STMT, return true if STMT is not in GIMPLE form.
    TODO: Implement type checking.  */
 
 static bool
-verify_stmt (tree stmt, bool last_in_block)
+verify_stmt (gimple_stmt_iterator *gsi)
 {
   tree addr;
+  struct walk_stmt_info wi;
+  bool last_in_block = gsi_one_before_end_p (*gsi);
+  gimple stmt = gsi_stmt (*gsi);
+  int lp_nr;
 
-  if (OMP_DIRECTIVE_P (stmt))
+  if (is_gimple_omp (stmt))
     {
       /* OpenMP directives are validated by the FE and never operated
-        on by the optimizers.  Furthermore, OMP_FOR may contain
+        on by the optimizers.  Furthermore, GIMPLE_OMP_FOR may contain
         non-gimple expressions when the main index variable has had
         its address taken.  This does not affect the loop itself
-        because the header of an OMP_FOR is merely used to determine
+        because the header of an GIMPLE_OMP_FOR is merely used to determine
         how to setup the parallel iteration.  */
       return false;
     }
 
-  if (!is_gimple_stmt (stmt))
+  /* FIXME.  The C frontend passes unpromoted arguments in case it
+     didn't see a function declaration before the call.  */
+  if (is_gimple_call (stmt))
     {
-      error ("is not a valid GIMPLE statement");
-      goto fail;
+      tree decl;
+
+      if (!is_gimple_call_addr (gimple_call_fn (stmt)))
+       {
+         error ("invalid function in call statement");
+         return true;
+       }
+
+      decl = gimple_call_fndecl (stmt);
+      if (decl
+         && TREE_CODE (decl) == FUNCTION_DECL
+         && DECL_LOOPING_CONST_OR_PURE_P (decl)
+         && (!DECL_PURE_P (decl))
+         && (!TREE_READONLY (decl)))
+       {
+         error ("invalid pure const state for function");
+         return true;
+       }
     }
 
-  addr = walk_tree (&stmt, verify_expr, NULL, NULL);
+  if (is_gimple_debug (stmt))
+    return false;
+
+  memset (&wi, 0, sizeof (wi));
+  addr = walk_gimple_op (gsi_stmt (*gsi), verify_expr, &wi);
   if (addr)
     {
-      debug_generic_stmt (addr);
-      if (addr != stmt)
-       {
-         inform ("in statement");
-         debug_generic_stmt (stmt);
-       }
+      debug_generic_expr (addr);
+      inform (gimple_location (gsi_stmt (*gsi)), "in statement");
+      debug_gimple_stmt (stmt);
       return true;
     }
 
@@ -4256,14 +3955,21 @@ verify_stmt (tree stmt, bool last_in_block)
      have optimizations that simplify statements such that we prove
      that they cannot throw, that we update other data structures
      to match.  */
-  if (lookup_stmt_eh_region (stmt) >= 0)
+  lp_nr = lookup_stmt_eh_lp (stmt);
+  if (lp_nr != 0)
     {
-      if (!tree_could_throw_p (stmt))
+      if (!stmt_could_throw_p (stmt))
        {
-         error ("statement marked for throw, but doesn%'t");
-         goto fail;
+         /* During IPA passes, ipa-pure-const sets nothrow flags on calls
+            and they are updated on statements only after fixup_cfg
+            is executed at beggining of expansion stage.  */
+         if (cgraph_state != CGRAPH_STATE_IPA_SSA)
+           {
+             error ("statement marked for throw, but doesn%'t");
+             goto fail;
+           }
        }
-      if (!last_in_block && tree_can_throw_internal (stmt))
+      else if (lp_nr > 0 && !last_in_block && stmt_can_throw_internal (stmt))
        {
          error ("statement marked for throw in middle of block");
          goto fail;
@@ -4273,14 +3979,14 @@ verify_stmt (tree stmt, bool last_in_block)
   return false;
 
  fail:
-  debug_generic_stmt (stmt);
+  debug_gimple_stmt (stmt);
   return true;
 }
 
 
 /* Return true when the T can be shared.  */
 
-static bool
+bool
 tree_node_can_be_shared (tree t)
 {
   if (IS_TYPE_OR_DECL_P (t)
@@ -4307,12 +4013,13 @@ tree_node_can_be_shared (tree t)
 }
 
 
-/* Called via walk_trees.  Verify tree sharing.  */
+/* Called via walk_gimple_stmt.  Verify tree sharing.  */
 
 static tree
-verify_node_sharing (tree * tp, int *walk_subtrees, void *data)
+verify_node_sharing (tree *tp, int *walk_subtrees, void *data)
 {
-  struct pointer_set_t *visited = (struct pointer_set_t *) data;
+  struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
+  struct pointer_set_t *visited = (struct pointer_set_t *) wi->info;
 
   if (tree_node_can_be_shared (*tp))
     {
@@ -4327,35 +4034,6 @@ verify_node_sharing (tree * tp, int *walk_subtrees, void *data)
 }
 
 
-/* Helper function for verify_gimple_tuples.  */
-
-static tree
-verify_gimple_tuples_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
-                        void *data ATTRIBUTE_UNUSED)
-{
-  switch (TREE_CODE (*tp))
-    {
-    case MODIFY_EXPR:
-      error ("unexpected non-tuple");
-      debug_tree (*tp);
-      gcc_unreachable ();
-      return NULL_TREE;
-
-    default:
-      return NULL_TREE;
-    }
-}
-
-/* Verify that there are no trees that should have been converted to
-   gimple tuples.  Return true if T contains a node that should have
-   been converted to a gimple tuple, but hasn't.  */
-
-static bool
-verify_gimple_tuples (tree t)
-{
-  return walk_tree (&t, verify_gimple_tuples_1, NULL, NULL) != NULL;
-}
-
 static bool eh_error_found;
 static int
 verify_eh_throw_stmt_node (void **slot, void *data)
@@ -4366,52 +4044,56 @@ verify_eh_throw_stmt_node (void **slot, void *data)
   if (!pointer_set_contains (visited, node->stmt))
     {
       error ("Dead STMT in EH table");
-      debug_generic_stmt (node->stmt);
+      debug_gimple_stmt (node->stmt);
       eh_error_found = true;
     }
-  return 0;
+  return 1;
 }
 
-/* Verify the GIMPLE statement chain.  */
+
+/* Verify the GIMPLE statements in every basic block.  */
 
 void
 verify_stmts (void)
 {
   basic_block bb;
-  block_stmt_iterator bsi;
+  gimple_stmt_iterator gsi;
   bool err = false;
   struct pointer_set_t *visited, *visited_stmts;
   tree addr;
+  struct walk_stmt_info wi;
 
   timevar_push (TV_TREE_STMT_VERIFY);
   visited = pointer_set_create ();
   visited_stmts = pointer_set_create ();
 
+  memset (&wi, 0, sizeof (wi));
+  wi.info = (void *) visited;
+
   FOR_EACH_BB (bb)
     {
-      tree phi;
-      int i;
+      gimple phi;
+      size_t i;
 
-      for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
+      for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
        {
-         int phi_num_args = PHI_NUM_ARGS (phi);
-
+         phi = gsi_stmt (gsi);
          pointer_set_insert (visited_stmts, phi);
-         if (bb_for_stmt (phi) != bb)
+         if (gimple_bb (phi) != bb)
            {
-             error ("bb_for_stmt (phi) is set to a wrong basic block");
+             error ("gimple_bb (phi) is set to a wrong basic block");
              err |= true;
            }
 
-         for (i = 0; i < phi_num_args; i++)
+         for (i = 0; i < gimple_phi_num_args (phi); i++)
            {
-             tree t = PHI_ARG_DEF (phi, i);
+             tree t = gimple_phi_arg_def (phi, i);
              tree addr;
 
              if (!t)
                {
                  error ("missing PHI def");
-                 debug_generic_stmt (phi);
+                 debug_gimple_stmt (phi);
                  err |= true;
                  continue;
                }
@@ -4421,9 +4103,9 @@ verify_stmts (void)
                       && TREE_CODE (t) != FUNCTION_DECL
                       && !is_gimple_min_invariant (t))
                {
-                 error ("PHI def is not a GIMPLE value");
-                 debug_generic_stmt (phi);
-                 debug_generic_stmt (t);
+                 error ("PHI argument is not a GIMPLE value");
+                 debug_gimple_stmt (phi);
+                 debug_generic_expr (t);
                  err |= true;
                }
 
@@ -4431,38 +4113,87 @@ verify_stmts (void)
              if (addr)
                {
                  error ("incorrect sharing of tree nodes");
-                 debug_generic_stmt (phi);
-                 debug_generic_stmt (addr);
+                 debug_gimple_stmt (phi);
+                 debug_generic_expr (addr);
                  err |= true;
                }
            }
+
+#ifdef ENABLE_TYPES_CHECKING
+         if (verify_gimple_phi (phi))
+           {
+             debug_gimple_stmt (phi);
+             err |= true;
+           }
+#endif
        }
 
-      for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
+      for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
        {
-         tree stmt = bsi_stmt (bsi);
+         gimple stmt = gsi_stmt (gsi);
+
+         if (gimple_code (stmt) == GIMPLE_WITH_CLEANUP_EXPR
+             || gimple_code (stmt) == GIMPLE_BIND)
+           {
+             error ("invalid GIMPLE statement");
+             debug_gimple_stmt (stmt);
+             err |= true;
+           }
 
          pointer_set_insert (visited_stmts, stmt);
-         err |= verify_gimple_tuples (stmt);
 
-         if (bb_for_stmt (stmt) != bb)
+         if (gimple_bb (stmt) != bb)
            {
-             error ("bb_for_stmt (stmt) is set to a wrong basic block");
+             error ("gimple_bb (stmt) is set to a wrong basic block");
+             debug_gimple_stmt (stmt);
              err |= true;
            }
 
-         bsi_next (&bsi);
-         err |= verify_stmt (stmt, bsi_end_p (bsi));
-         addr = walk_tree (&stmt, verify_node_sharing, visited, NULL);
+         if (gimple_code (stmt) == GIMPLE_LABEL)
+           {
+             tree decl = gimple_label_label (stmt);
+             int uid = LABEL_DECL_UID (decl);
+
+             if (uid == -1
+                 || VEC_index (basic_block, label_to_block_map, uid) != bb)
+               {
+                 error ("incorrect entry in label_to_block_map");
+                 err |= true;
+               }
+
+             uid = EH_LANDING_PAD_NR (decl);
+             if (uid)
+               {
+                 eh_landing_pad lp = get_eh_landing_pad_from_number (uid);
+                 if (decl != lp->post_landing_pad)
+                   {
+                     error ("incorrect setting of landing pad number");
+                     err |= true;
+                   }
+               }
+           }
+
+         err |= verify_stmt (&gsi);
+
+#ifdef ENABLE_TYPES_CHECKING
+         if (verify_types_in_gimple_stmt (gsi_stmt (gsi)))
+           {
+             debug_gimple_stmt (stmt);
+             err |= true;
+           }
+#endif
+         addr = walk_gimple_op (gsi_stmt (gsi), verify_node_sharing, &wi);
          if (addr)
            {
              error ("incorrect sharing of tree nodes");
-             debug_generic_stmt (stmt);
-             debug_generic_stmt (addr);
+             debug_gimple_stmt (stmt);
+             debug_generic_expr (addr);
              err |= true;
            }
+         gsi_next (&gsi);
        }
     }
+
   eh_error_found = false;
   if (get_eh_throw_stmt_table (cfun))
     htab_traverse (get_eh_throw_stmt_table (cfun),
@@ -4482,22 +4213,22 @@ verify_stmts (void)
 /* Verifies that the flow information is OK.  */
 
 static int
-tree_verify_flow_info (void)
+gimple_verify_flow_info (void)
 {
   int err = 0;
   basic_block bb;
-  block_stmt_iterator bsi;
-  tree stmt;
+  gimple_stmt_iterator gsi;
+  gimple stmt;
   edge e;
   edge_iterator ei;
 
-  if (ENTRY_BLOCK_PTR->il.tree)
+  if (ENTRY_BLOCK_PTR->il.gimple)
     {
       error ("ENTRY_BLOCK has IL associated with it");
       err = 1;
     }
 
-  if (EXIT_BLOCK_PTR->il.tree)
+  if (EXIT_BLOCK_PTR->il.gimple)
     {
       error ("EXIT_BLOCK has IL associated with it");
       err = 1;
@@ -4514,41 +4245,51 @@ tree_verify_flow_info (void)
     {
       bool found_ctrl_stmt = false;
 
-      stmt = NULL_TREE;
+      stmt = NULL;
 
       /* Skip labels on the start of basic block.  */
-      for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
+      for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
        {
-         tree prev_stmt = stmt;
+         tree label;
+         gimple prev_stmt = stmt;
 
-         stmt = bsi_stmt (bsi);
+         stmt = gsi_stmt (gsi);
 
-         if (TREE_CODE (stmt) != LABEL_EXPR)
+         if (gimple_code (stmt) != GIMPLE_LABEL)
            break;
 
-         if (prev_stmt && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
+         label = gimple_label_label (stmt);
+         if (prev_stmt && DECL_NONLOCAL (label))
            {
              error ("nonlocal label ");
-             print_generic_expr (stderr, LABEL_EXPR_LABEL (stmt), 0);
+             print_generic_expr (stderr, label, 0);
+             fprintf (stderr, " is not first in a sequence of labels in bb %d",
+                      bb->index);
+             err = 1;
+           }
+
+         if (prev_stmt && EH_LANDING_PAD_NR (label) != 0)
+           {
+             error ("EH landing pad label ");
+             print_generic_expr (stderr, label, 0);
              fprintf (stderr, " is not first in a sequence of labels in bb %d",
                       bb->index);
              err = 1;
            }
 
-         if (label_to_block (LABEL_EXPR_LABEL (stmt)) != bb)
+         if (label_to_block (label) != bb)
            {
              error ("label ");
-             print_generic_expr (stderr, LABEL_EXPR_LABEL (stmt), 0);
+             print_generic_expr (stderr, label, 0);
              fprintf (stderr, " to block does not match in bb %d",
                       bb->index);
              err = 1;
            }
 
-         if (decl_function_context (LABEL_EXPR_LABEL (stmt))
-             != current_function_decl)
+         if (decl_function_context (label) != current_function_decl)
            {
              error ("label ");
-             print_generic_expr (stderr, LABEL_EXPR_LABEL (stmt), 0);
+             print_generic_expr (stderr, label, 0);
              fprintf (stderr, " has incorrect context in bb %d",
                       bb->index);
              err = 1;
@@ -4556,9 +4297,9 @@ tree_verify_flow_info (void)
        }
 
       /* Verify that body of basic block BB is free of control flow.  */
-      for (; !bsi_end_p (bsi); bsi_next (&bsi))
+      for (; !gsi_end_p (gsi); gsi_next (&gsi))
        {
-         tree stmt = bsi_stmt (bsi);
+         gimple stmt = gsi_stmt (gsi);
 
          if (found_ctrl_stmt)
            {
@@ -4570,20 +4311,23 @@ tree_verify_flow_info (void)
          if (stmt_ends_bb_p (stmt))
            found_ctrl_stmt = true;
 
-         if (TREE_CODE (stmt) == LABEL_EXPR)
+         if (gimple_code (stmt) == GIMPLE_LABEL)
            {
              error ("label ");
-             print_generic_expr (stderr, LABEL_EXPR_LABEL (stmt), 0);
+             print_generic_expr (stderr, gimple_label_label (stmt), 0);
              fprintf (stderr, " in the middle of basic block %d", bb->index);
              err = 1;
            }
        }
 
-      bsi = bsi_last (bb);
-      if (bsi_end_p (bsi))
+      gsi = gsi_last_bb (bb);
+      if (gsi_end_p (gsi))
        continue;
 
-      stmt = bsi_stmt (bsi);
+      stmt = gsi_stmt (gsi);
+
+      if (gimple_code (stmt) == GIMPLE_LABEL)
+       continue;
 
       err |= verify_eh_edges (stmt);
 
@@ -4598,37 +4342,30 @@ tree_verify_flow_info (void)
              }
        }
 
-      if (TREE_CODE (stmt) != COND_EXPR)
+      if (gimple_code (stmt) != GIMPLE_COND)
        {
          /* Verify that there are no edges with EDGE_TRUE/FALSE_FLAG set
             after anything else but if statement.  */
          FOR_EACH_EDGE (e, ei, bb->succs)
            if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))
              {
-               error ("true/false edge after a non-COND_EXPR in bb %d",
+               error ("true/false edge after a non-GIMPLE_COND in bb %d",
                       bb->index);
                err = 1;
              }
        }
 
-      switch (TREE_CODE (stmt))
+      switch (gimple_code (stmt))
        {
-       case COND_EXPR:
+       case GIMPLE_COND:
          {
            edge true_edge;
            edge false_edge;
-  
-           if (COND_EXPR_THEN (stmt) != NULL_TREE
-               || COND_EXPR_ELSE (stmt) != NULL_TREE)
-             {
-               error ("COND_EXPR with code in branches at the end of bb %d",
-                      bb->index);
-               err = 1;
-             }
 
            extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
 
-           if (!true_edge || !false_edge
+           if (!true_edge
+               || !false_edge
                || !(true_edge->flags & EDGE_TRUE_VALUE)
                || !(false_edge->flags & EDGE_FALSE_VALUE)
                || (true_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
@@ -4642,7 +4379,7 @@ tree_verify_flow_info (void)
          }
          break;
 
-       case GOTO_EXPR:
+       case GIMPLE_GOTO:
          if (simple_goto_p (stmt))
            {
              error ("explicit goto at end of bb %d", bb->index);
@@ -4664,7 +4401,7 @@ tree_verify_flow_info (void)
            }
          break;
 
-       case RETURN_EXPR:
+       case GIMPLE_RETURN:
          if (!single_succ_p (bb)
              || (single_succ_edge (bb)->flags
                  & (EDGE_FALLTHRU | EDGE_ABNORMAL
@@ -4681,41 +4418,37 @@ tree_verify_flow_info (void)
            }
          break;
 
-       case SWITCH_EXPR:
+       case GIMPLE_SWITCH:
          {
            tree prev;
            edge e;
            size_t i, n;
-           tree vec;
 
-           vec = SWITCH_LABELS (stmt);
-           n = TREE_VEC_LENGTH (vec);
+           n = gimple_switch_num_labels (stmt);
 
            /* Mark all the destination basic blocks.  */
            for (i = 0; i < n; ++i)
              {
-               tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
+               tree lab = CASE_LABEL (gimple_switch_label (stmt, i));
                basic_block label_bb = label_to_block (lab);
-
                gcc_assert (!label_bb->aux || label_bb->aux == (void *)1);
                label_bb->aux = (void *)1;
              }
 
            /* Verify that the case labels are sorted.  */
-           prev = TREE_VEC_ELT (vec, 0);
+           prev = gimple_switch_label (stmt, 0);
            for (i = 1; i < n; ++i)
              {
-               tree c = TREE_VEC_ELT (vec, i);
-               if (! CASE_LOW (c))
+               tree c = gimple_switch_label (stmt, i);
+               if (!CASE_LOW (c))
                  {
-                   if (i != n - 1)
-                     {
-                       error ("found default case not at end of case vector");
-                       err = 1;
-                     }
+                   error ("found default case not at the start of "
+                          "case vector");
+                   err = 1;
                    continue;
                  }
-               if (! tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c)))
+               if (CASE_LOW (prev)
+                   && !tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c)))
                  {
                    error ("case labels not sorted: ");
                    print_generic_expr (stderr, prev, 0);
@@ -4738,6 +4471,7 @@ tree_verify_flow_info (void)
                           bb->index, e->dest->index);
                    err = 1;
                  }
+
                e->dest->aux = (void *)2;
                if ((e->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
                                 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
@@ -4751,13 +4485,12 @@ tree_verify_flow_info (void)
            /* Check that we have all of them.  */
            for (i = 0; i < n; ++i)
              {
-               tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
+               tree lab = CASE_LABEL (gimple_switch_label (stmt, i));
                basic_block label_bb = label_to_block (lab);
 
                if (label_bb->aux != (void *)2)
                  {
-                   error ("missing edge %i->%i",
-                          bb->index, label_bb->index);
+                   error ("missing edge %i->%i", bb->index, label_bb->index);
                    err = 1;
                  }
              }
@@ -4765,8 +4498,14 @@ tree_verify_flow_info (void)
            FOR_EACH_EDGE (e, ei, bb->succs)
              e->dest->aux = (void *)0;
          }
+         break;
+
+       case GIMPLE_EH_DISPATCH:
+         err |= verify_eh_dispatch_edge (stmt);
+         break;
 
-       default: ;
+       default:
+         break;
        }
     }
 
@@ -4781,12 +4520,13 @@ tree_verify_flow_info (void)
    by edge FALLTHRU.  */
 
 static void
-tree_make_forwarder_block (edge fallthru)
+gimple_make_forwarder_block (edge fallthru)
 {
   edge e;
   edge_iterator ei;
   basic_block dummy, bb;
-  tree phi, new_phi, var;
+  tree var;
+  gimple_stmt_iterator gsi;
 
   dummy = fallthru->src;
   bb = fallthru->dest;
@@ -4796,18 +4536,19 @@ tree_make_forwarder_block (edge fallthru)
 
   /* If we redirected a branch we must create new PHI nodes at the
      start of BB.  */
-  for (phi = phi_nodes (dummy); phi; phi = PHI_CHAIN (phi))
+  for (gsi = gsi_start_phis (dummy); !gsi_end_p (gsi); gsi_next (&gsi))
     {
-      var = PHI_RESULT (phi);
+      gimple phi, new_phi;
+
+      phi = gsi_stmt (gsi);
+      var = gimple_phi_result (phi);
       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);
+      gimple_phi_set_result (phi, make_ssa_name (SSA_NAME_VAR (var), phi));
+      add_phi_arg (new_phi, gimple_phi_result (phi), fallthru,
+                  UNKNOWN_LOCATION);
     }
 
-  /* Ensure that the PHI node chain is in the same order.  */
-  set_phi_nodes (bb, phi_reverse (phi_nodes (bb)));
-
   /* Add the arguments we have stored on edges.  */
   FOR_EACH_EDGE (e, ei, bb->preds)
     {
@@ -4823,29 +4564,30 @@ tree_make_forwarder_block (edge fallthru)
    Create one if it doesn't exist.  */
 
 tree
-tree_block_label (basic_block bb)
+gimple_block_label (basic_block bb)
 {
-  block_stmt_iterator i, s = bsi_start (bb);
+  gimple_stmt_iterator i, s = gsi_start_bb (bb);
   bool first = true;
-  tree label, stmt;
+  tree label;
+  gimple stmt;
 
-  for (i = s; !bsi_end_p (i); first = false, bsi_next (&i))
+  for (i = s; !gsi_end_p (i); first = false, gsi_next (&i))
     {
-      stmt = bsi_stmt (i);
-      if (TREE_CODE (stmt) != LABEL_EXPR)
+      stmt = gsi_stmt (i);
+      if (gimple_code (stmt) != GIMPLE_LABEL)
        break;
-      label = LABEL_EXPR_LABEL (stmt);
+      label = gimple_label_label (stmt);
       if (!DECL_NONLOCAL (label))
        {
          if (!first)
-           bsi_move_before (&i, &s);
+           gsi_move_before (&i, &s);
          return label;
        }
     }
 
-  label = create_artificial_label ();
-  stmt = build1 (LABEL_EXPR, void_type_node, label);
-  bsi_insert_before (&s, stmt, BSI_NEW_STMT);
+  label = create_artificial_label (UNKNOWN_LOCATION);
+  stmt = gimple_build_label (label);
+  gsi_insert_before (&s, stmt, GSI_NEW_STMT);
   return label;
 }
 
@@ -4857,11 +4599,11 @@ tree_block_label (basic_block bb)
    redirect_edge_and_branch.  */
 
 static edge
-tree_try_redirect_by_replacing_jump (edge e, basic_block target)
+gimple_try_redirect_by_replacing_jump (edge e, basic_block target)
 {
   basic_block src = e->src;
-  block_stmt_iterator b;
-  tree stmt;
+  gimple_stmt_iterator i;
+  gimple stmt;
 
   /* We can replace or remove a complex jump only when we have exactly
      two edges.  */
@@ -4871,15 +4613,15 @@ tree_try_redirect_by_replacing_jump (edge e, basic_block target)
       || EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target)
     return NULL;
 
-  b = bsi_last (src);
-  if (bsi_end_p (b))
+  i = gsi_last_bb (src);
+  if (gsi_end_p (i))
     return NULL;
-  stmt = bsi_stmt (b);
 
-  if (TREE_CODE (stmt) == COND_EXPR
-      || TREE_CODE (stmt) == SWITCH_EXPR)
+  stmt = gsi_stmt (i);
+
+  if (gimple_code (stmt) == GIMPLE_COND || gimple_code (stmt) == GIMPLE_SWITCH)
     {
-      bsi_remove (&b, true);
+      gsi_remove (&i, true);
       e = ssa_redirect_edge (e, target);
       e->flags = EDGE_FALLTHRU;
       return e;
@@ -4893,41 +4635,47 @@ tree_try_redirect_by_replacing_jump (edge e, basic_block target)
    edge representing the redirected branch.  */
 
 static edge
-tree_redirect_edge_and_branch (edge e, basic_block dest)
+gimple_redirect_edge_and_branch (edge e, basic_block dest)
 {
   basic_block bb = e->src;
-  block_stmt_iterator bsi;
+  gimple_stmt_iterator gsi;
   edge ret;
-  tree stmt;
+  gimple stmt;
 
   if (e->flags & EDGE_ABNORMAL)
     return NULL;
 
-  if (e->src != ENTRY_BLOCK_PTR
-      && (ret = tree_try_redirect_by_replacing_jump (e, dest)))
-    return ret;
-
   if (e->dest == dest)
     return NULL;
 
-  bsi = bsi_last (bb);
-  stmt = bsi_end_p (bsi) ? NULL : bsi_stmt (bsi);
+  if (e->flags & EDGE_EH)
+    return redirect_eh_edge (e, dest);
 
-  switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
+  if (e->src != ENTRY_BLOCK_PTR)
     {
-    case COND_EXPR:
+      ret = gimple_try_redirect_by_replacing_jump (e, dest);
+      if (ret)
+       return ret;
+    }
+
+  gsi = gsi_last_bb (bb);
+  stmt = gsi_end_p (gsi) ? NULL : gsi_stmt (gsi);
+
+  switch (stmt ? gimple_code (stmt) : GIMPLE_ERROR_MARK)
+    {
+    case GIMPLE_COND:
       /* For COND_EXPR, we only need to redirect the edge.  */
       break;
 
-    case GOTO_EXPR:
+    case GIMPLE_GOTO:
       /* No non-abnormal edges should lead from a non-simple goto, and
         simple ones should be represented implicitly.  */
       gcc_unreachable ();
 
-    case SWITCH_EXPR:
+    case GIMPLE_SWITCH:
       {
+       tree label = gimple_block_label (dest);
         tree cases = get_cases_for_edge (e, stmt);
-       tree label = tree_block_label (dest);
 
        /* 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.  */
@@ -4956,33 +4704,58 @@ tree_redirect_edge_and_branch (edge e, basic_block dest)
          }
        else
          {
-           tree vec = SWITCH_LABELS (stmt);
-           size_t i, n = TREE_VEC_LENGTH (vec);
+           size_t i, n = gimple_switch_num_labels (stmt);
 
            for (i = 0; i < n; i++)
              {
-               tree elt = TREE_VEC_ELT (vec, i);
-
+               tree elt = gimple_switch_label (stmt, i);
                if (label_to_block (CASE_LABEL (elt)) == e->dest)
                  CASE_LABEL (elt) = label;
              }
          }
+      }
+      break;
 
-       break;
+    case GIMPLE_ASM:
+      {
+       int i, n = gimple_asm_nlabels (stmt);
+       tree label = NULL;
+
+       for (i = 0; i < n; ++i)
+         {
+           tree cons = gimple_asm_label_op (stmt, i);
+           if (label_to_block (TREE_VALUE (cons)) == e->dest)
+             {
+               if (!label)
+                 label = gimple_block_label (dest);
+               TREE_VALUE (cons) = label;
+             }
+         }
+
+       /* If we didn't find any label matching the former edge in the
+          asm labels, we must be redirecting the fallthrough
+          edge.  */
+       gcc_assert (label || (e->flags & EDGE_FALLTHRU));
       }
+      break;
 
-    case RETURN_EXPR:
-      bsi_remove (&bsi, true);
+    case GIMPLE_RETURN:
+      gsi_remove (&gsi, true);
       e->flags |= EDGE_FALLTHRU;
       break;
 
-    case OMP_RETURN:
-    case OMP_CONTINUE:
-    case OMP_SECTIONS_SWITCH:
-    case OMP_FOR:
+    case GIMPLE_OMP_RETURN:
+    case GIMPLE_OMP_CONTINUE:
+    case GIMPLE_OMP_SECTIONS_SWITCH:
+    case GIMPLE_OMP_FOR:
       /* The edges from OMP constructs can be simply redirected.  */
       break;
 
+    case GIMPLE_EH_DISPATCH:
+      if (!(e->flags & EDGE_FALLTHRU))
+       redirect_eh_dispatch_edge (stmt, e, dest);
+      break;
+
     default:
       /* Otherwise it must be a fallthru edge, and we don't need to
         do anything besides redirecting it.  */
@@ -5002,9 +4775,9 @@ tree_redirect_edge_and_branch (edge e, basic_block dest)
    it to the destination of the other edge from E->src.  */
 
 static bool
-tree_can_remove_branch_p (const_edge e)
+gimple_can_remove_branch_p (const_edge e)
 {
-  if (e->flags & EDGE_ABNORMAL)
+  if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
     return false;
 
   return true;
@@ -5013,9 +4786,9 @@ tree_can_remove_branch_p (const_edge e)
 /* Simple wrapper, as we can always redirect fallthru edges.  */
 
 static basic_block
-tree_redirect_edge_and_branch_force (edge e, basic_block dest)
+gimple_redirect_edge_and_branch_force (edge e, basic_block dest)
 {
-  e = tree_redirect_edge_and_branch (e, dest);
+  e = gimple_redirect_edge_and_branch (e, dest);
   gcc_assert (e);
 
   return NULL;
@@ -5026,11 +4799,12 @@ tree_redirect_edge_and_branch_force (edge e, basic_block dest)
    labels).  If STMT is NULL, BB is split just after the labels.  */
 
 static basic_block
-tree_split_block (basic_block bb, void *stmt)
+gimple_split_block (basic_block bb, void *stmt)
 {
-  block_stmt_iterator bsi;
-  tree_stmt_iterator tsi_tgt;
-  tree act, list;
+  gimple_stmt_iterator gsi;
+  gimple_stmt_iterator gsi_tgt;
+  gimple act;
+  gimple_seq list;
   basic_block new_bb;
   edge e;
   edge_iterator ei;
@@ -5043,14 +4817,14 @@ tree_split_block (basic_block bb, void *stmt)
   FOR_EACH_EDGE (e, ei, new_bb->succs)
     e->src = new_bb;
 
-  if (stmt && TREE_CODE ((tree) stmt) == LABEL_EXPR)
+  if (stmt && gimple_code ((gimple) stmt) == GIMPLE_LABEL)
     stmt = NULL;
 
-  /* Move everything from BSI to the new basic block.  */
-  for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
+  /* Move everything from GSI to the new basic block.  */
+  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
     {
-      act = bsi_stmt (bsi);
-      if (TREE_CODE (act) == LABEL_EXPR)
+      act = gsi_stmt (gsi);
+      if (gimple_code (act) == GIMPLE_LABEL)
        continue;
 
       if (!stmt)
@@ -5058,23 +4832,23 @@ tree_split_block (basic_block bb, void *stmt)
 
       if (stmt == act)
        {
-         bsi_next (&bsi);
+         gsi_next (&gsi);
          break;
        }
     }
 
-  if (bsi_end_p (bsi))
+  if (gsi_end_p (gsi))
     return new_bb;
 
   /* Split the statement list - avoid re-creating new containers as this
-     brings ugly quadratic memory consumption in the inliner.  
+     brings ugly quadratic memory consumption in the inliner.
      (We are still quadratic since we need to update stmt BB pointers,
      sadly.)  */
-  list = tsi_split_statement_list_before (&bsi.tsi);
-  set_bb_stmt_list (new_bb, list);
-  for (tsi_tgt = tsi_start (list);
-       !tsi_end_p (tsi_tgt); tsi_next (&tsi_tgt))
-    change_bb_for_stmt (tsi_stmt (tsi_tgt), new_bb);
+  list = gsi_split_seq_before (&gsi);
+  set_bb_seq (new_bb, list);
+  for (gsi_tgt = gsi_start (list);
+       !gsi_end_p (gsi_tgt); gsi_next (&gsi_tgt))
+    gimple_set_bb (gsi_stmt (gsi_tgt), new_bb);
 
   return new_bb;
 }
@@ -5083,7 +4857,7 @@ tree_split_block (basic_block bb, void *stmt)
 /* Moves basic block BB after block AFTER.  */
 
 static bool
-tree_move_block_after (basic_block bb, basic_block after)
+gimple_move_block_after (basic_block bb, basic_block after)
 {
   if (bb->prev_bb == after)
     return true;
@@ -5098,56 +4872,50 @@ tree_move_block_after (basic_block bb, basic_block after)
 /* Return true if basic_block can be duplicated.  */
 
 static bool
-tree_can_duplicate_bb_p (const_basic_block bb ATTRIBUTE_UNUSED)
+gimple_can_duplicate_bb_p (const_basic_block bb ATTRIBUTE_UNUSED)
 {
   return true;
 }
 
-
 /* Create a duplicate of the basic block BB.  NOTE: This does not
    preserve SSA form.  */
 
 static basic_block
-tree_duplicate_bb (basic_block bb)
+gimple_duplicate_bb (basic_block bb)
 {
   basic_block new_bb;
-  block_stmt_iterator bsi, bsi_tgt;
-  tree phi;
+  gimple_stmt_iterator gsi, gsi_tgt;
+  gimple_seq phis = phi_nodes (bb);
+  gimple phi, stmt, copy;
 
   new_bb = create_empty_bb (EXIT_BLOCK_PTR->prev_bb);
 
   /* Copy the PHI nodes.  We ignore PHI node arguments here because
      the incoming edges have not been setup yet.  */
-  for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
+  for (gsi = gsi_start (phis); !gsi_end_p (gsi); gsi_next (&gsi))
     {
-      tree copy = create_phi_node (PHI_RESULT (phi), new_bb);
-      create_new_def_for (PHI_RESULT (copy), copy, PHI_RESULT_PTR (copy));
+      phi = gsi_stmt (gsi);
+      copy = create_phi_node (gimple_phi_result (phi), new_bb);
+      create_new_def_for (gimple_phi_result (copy), copy,
+                         gimple_phi_result_ptr (copy));
     }
 
-  /* Keep the chain of PHI nodes in the same order so that they can be
-     updated by ssa_redirect_edge.  */
-  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))
+  gsi_tgt = gsi_start_bb (new_bb);
+  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
     {
       def_operand_p def_p;
       ssa_op_iter op_iter;
-      tree stmt, copy;
-      int region;
 
-      stmt = bsi_stmt (bsi);
-      if (TREE_CODE (stmt) == LABEL_EXPR)
+      stmt = gsi_stmt (gsi);
+      if (gimple_code (stmt) == GIMPLE_LABEL)
        continue;
 
       /* Create a new copy of STMT and duplicate STMT's virtual
         operands.  */
-      copy = unshare_expr (stmt);
-      bsi_insert_after (&bsi_tgt, copy, BSI_NEW_STMT);
-      copy_virtual_operands (copy, stmt);
-      region = lookup_stmt_eh_region (stmt);
-      if (region >= 0)
-       add_stmt_to_eh_region (copy, region);
+      copy = gimple_copy (stmt);
+      gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
+
+      maybe_duplicate_eh_stmt (copy, stmt);
       gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
 
       /* Create new names for all the definitions created by COPY and
@@ -5167,9 +4935,11 @@ add_phi_args_after_copy_edge (edge e_copy)
   basic_block bb, bb_copy = e_copy->src, dest;
   edge e;
   edge_iterator ei;
-  tree phi, phi_copy, phi_next, def;
+  gimple phi, phi_copy;
+  tree def;
+  gimple_stmt_iterator psi, psi_copy;
 
-  if (!phi_nodes (e_copy->dest))
+  if (gimple_seq_empty_p (phi_nodes (e_copy->dest)))
     return;
 
   bb = bb_copy->flags & BB_DUPLICATED ? get_bb_original (bb_copy) : bb_copy;
@@ -5195,13 +4965,16 @@ add_phi_args_after_copy_edge (edge e_copy)
       gcc_assert (e != NULL);
     }
 
-  for (phi = phi_nodes (e->dest), phi_copy = phi_nodes (e_copy->dest);
-       phi;
-       phi = phi_next, phi_copy = PHI_CHAIN (phi_copy))
+  for (psi = gsi_start_phis (e->dest),
+       psi_copy = gsi_start_phis (e_copy->dest);
+       !gsi_end_p (psi);
+       gsi_next (&psi), gsi_next (&psi_copy))
     {
-      phi_next = PHI_CHAIN (phi);
+      phi = gsi_stmt (psi);
+      phi_copy = gsi_stmt (psi_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,
+                  gimple_phi_arg_location_from_edge (phi, e));
     }
 }
 
@@ -5213,8 +4986,8 @@ add_phi_args_after_copy_edge (edge e_copy)
 void
 add_phi_args_after_copy_bb (basic_block bb_copy)
 {
-  edge_iterator ei;
   edge e_copy;
+  edge_iterator ei;
 
   FOR_EACH_EDGE (e_copy, ei, bb_copy->succs)
     {
@@ -5256,7 +5029,7 @@ add_phi_args_after_copy (basic_block *region_copy, unsigned n_region,
    true otherwise.  */
 
 bool
-tree_duplicate_sese_region (edge entry, edge exit,
+gimple_duplicate_sese_region (edge entry, edge exit,
                            basic_block *region, unsigned n_region,
                            basic_block *region_copy)
 {
@@ -5312,7 +5085,7 @@ tree_duplicate_sese_region (edge entry, edge exit,
       free_region_copy = true;
     }
 
-  gcc_assert (!need_ssa_update_p ());
+  gcc_assert (!need_ssa_update_p (cfun));
 
   /* Record blocks outside the region that are dominated by something
      inside.  */
@@ -5399,8 +5172,8 @@ tree_duplicate_sese_region (edge entry, edge exit,
    is moved to ENTRY.  Returns true if duplication succeeds, false
    otherwise.
 
-   For example, 
+   For example,
+
    some_code;
    if (cond)
      A;
@@ -5422,9 +5195,9 @@ tree_duplicate_sese_region (edge entry, edge exit,
 */
 
 bool
-tree_duplicate_sese_tail (edge entry, edge exit,
-                         basic_block *region, unsigned n_region,
-                         basic_block *region_copy)
+gimple_duplicate_sese_tail (edge entry ATTRIBUTE_UNUSED, edge exit ATTRIBUTE_UNUSED,
+                         basic_block *region ATTRIBUTE_UNUSED, unsigned n_region ATTRIBUTE_UNUSED,
+                         basic_block *region_copy ATTRIBUTE_UNUSED)
 {
   unsigned i;
   bool free_region_copy = false;
@@ -5435,9 +5208,15 @@ tree_duplicate_sese_tail (edge entry, edge exit,
   int total_freq = 0, exit_freq = 0;
   gcov_type total_count = 0, exit_count = 0;
   edge exits[2], nexits[2], e;
-  block_stmt_iterator bsi;
-  tree cond;
+  gimple_stmt_iterator gsi,gsi1;
+  gimple cond_stmt;
   edge sorig, snew;
+  basic_block exit_bb;
+  basic_block iters_bb;
+  tree new_rhs;
+  gimple_stmt_iterator psi;
+  gimple phi;
+  tree def;
 
   gcc_assert (EDGE_COUNT (exit->src->succs) == 2);
   exits[0] = exit;
@@ -5446,24 +5225,9 @@ tree_duplicate_sese_tail (edge entry, edge exit,
   if (!can_copy_bbs_p (region, n_region))
     return false;
 
-  /* Some sanity checking.  Note that we do not check for all possible
-     missuses of the functions.  I.e. if you ask to copy something weird
-     (e.g., in the example, if there is a jump from inside to the middle
-     of some_code, or come_code defines some of the values used in cond)
-     it will work, but the resulting code will not be correct.  */
-  for (i = 0; i < n_region; i++)
-    {
-      /* We do not handle subloops, i.e. all the blocks must belong to the
-        same loop.  */
-      if (region[i]->loop_father != orig_loop)
-       return false;
-
-      if (region[i] == orig_loop->latch)
-       return false;
-    }
-
   initialize_original_copy_tables ();
   set_loop_copy (orig_loop, loop);
+  duplicate_subloops (orig_loop, loop);
 
   if (!region_copy)
     {
@@ -5471,7 +5235,7 @@ tree_duplicate_sese_tail (edge entry, edge exit,
       free_region_copy = true;
     }
 
-  gcc_assert (!need_ssa_update_p ());
+  gcc_assert (!need_ssa_update_p (cfun));
 
   /* Record blocks outside the region that are dominated by something
      inside.  */
@@ -5525,10 +5289,41 @@ tree_duplicate_sese_tail (edge entry, edge exit,
     switch_bb = split_edge (entry);
   set_immediate_dominator (CDI_DOMINATORS, nentry_bb, switch_bb);
 
-  bsi = bsi_last (switch_bb);
-  cond = last_stmt (exit->src);
-  gcc_assert (TREE_CODE (cond) == COND_EXPR);
-  bsi_insert_after (&bsi, unshare_expr (cond), BSI_NEW_STMT);
+  gsi = gsi_last_bb (switch_bb);
+  cond_stmt = last_stmt (exit->src);
+  gcc_assert (gimple_code (cond_stmt) == GIMPLE_COND);
+  cond_stmt = gimple_copy (cond_stmt);
+
+ /* If the block consisting of the exit condition has the latch as
+    successor, then the body of the loop is executed before
+    the exit condition is tested.  In such case, moving the
+    condition to the entry, causes that the loop will iterate
+    one less iteration (which is the wanted outcome, since we
+    peel out the last iteration).  If the body is executed after
+    the condition, moving the condition to the entry requires
+    decrementing one iteration.  */
+  if (exits[1]->dest == orig_loop->latch)
+    new_rhs = gimple_cond_rhs (cond_stmt);
+  else
+  {
+    new_rhs = fold_build2 (MINUS_EXPR, TREE_TYPE (gimple_cond_rhs (cond_stmt)),
+                          gimple_cond_rhs (cond_stmt),
+                          build_int_cst (TREE_TYPE (gimple_cond_rhs (cond_stmt)), 1));
+
+    if (TREE_CODE (gimple_cond_rhs (cond_stmt)) == SSA_NAME)
+      {
+       iters_bb = gimple_bb (SSA_NAME_DEF_STMT (gimple_cond_rhs (cond_stmt)));
+       for (gsi1 = gsi_start_bb (iters_bb); !gsi_end_p (gsi1); gsi_next (&gsi1))
+         if (gsi_stmt (gsi1) == SSA_NAME_DEF_STMT (gimple_cond_rhs (cond_stmt)))
+           break;
+
+       new_rhs = force_gimple_operand_gsi (&gsi1, new_rhs, true,
+                                           NULL_TREE,false,GSI_CONTINUE_LINKING);
+      }
+  }
+  gimple_cond_set_rhs (cond_stmt, unshare_expr (new_rhs));
+  gimple_cond_set_lhs (cond_stmt, unshare_expr (gimple_cond_lhs (cond_stmt)));
+  gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
 
   sorig = single_succ_edge (switch_bb);
   sorig->flags = exits[1]->flags;
@@ -5542,16 +5337,35 @@ tree_duplicate_sese_tail (edge entry, edge exit,
 
   /* Get rid of now superfluous conditions and associated edges (and phi node
      arguments).  */
+  exit_bb = exit->dest;
+
   e = redirect_edge_and_branch (exits[0], exits[1]->dest);
-  PENDING_STMT (e) = NULL_TREE;
-  e = redirect_edge_and_branch (nexits[1], nexits[0]->dest);
-  PENDING_STMT (e) = NULL_TREE;
+  PENDING_STMT (e) = NULL;
 
+  /* The latch of ORIG_LOOP was copied, and so was the backedge 
+     to the original header.  We redirect this backedge to EXIT_BB.  */
+  for (i = 0; i < n_region; i++)
+    if (get_bb_original (region_copy[i]) == orig_loop->latch)
+      {
+       gcc_assert (single_succ_edge (region_copy[i]));
+       e = redirect_edge_and_branch (single_succ_edge (region_copy[i]), exit_bb);
+       PENDING_STMT (e) = NULL;
+       for (psi = gsi_start_phis (exit_bb);
+            !gsi_end_p (psi);
+            gsi_next (&psi))
+         {
+           phi = gsi_stmt (psi);
+           def = PHI_ARG_DEF (phi, nexits[0]->dest_idx);
+           add_phi_arg (phi, def, e, gimple_phi_arg_location_from_edge (phi, e));
+         }
+      }
+  e = redirect_edge_and_branch (nexits[0], nexits[1]->dest);
+  PENDING_STMT (e) = NULL;
+  
   /* Anything that is outside of the region, but was dominated by something
      inside needs to update dominance info.  */
   iterate_fix_dominators (CDI_DOMINATORS, doms, false);
   VEC_free (basic_block, heap, doms);
-
   /* Update the SSA web.  */
   update_ssa (TODO_update_ssa);
 
@@ -5562,11 +5376,6 @@ tree_duplicate_sese_tail (edge entry, edge exit,
   return true;
 }
 
-/*
-DEF_VEC_P(basic_block);
-DEF_VEC_ALLOC_P(basic_block,heap);
-*/
-
 /* Add all the blocks dominated by ENTRY to the array BBS_P.  Stop
    adding blocks when the dominator traversal reaches EXIT.  This
    function silently assumes that ENTRY strictly dominates EXIT.  */
@@ -5627,6 +5436,7 @@ replace_by_duplicate_decl (tree *tp, struct pointer_map_t *vars_map,
   *tp = new_t;
 }
 
+
 /* Creates an ssa name in TO_CONTEXT equivalent to NAME.
    VARS_MAP maps old ssa names and var_decls to the new ones.  */
 
@@ -5671,6 +5481,7 @@ struct move_stmt_d
   tree to_context;
   struct pointer_map_t *vars_map;
   htab_t new_label_map;
+  struct pointer_map_t *eh_map;
   bool remap_decls_p;
 };
 
@@ -5679,44 +5490,16 @@ struct move_stmt_d
    DECL_CONTEXT of every local variable referenced in *TP.  */
 
 static tree
-move_stmt_r (tree *tp, int *walk_subtrees, void *data)
+move_stmt_op (tree *tp, int *walk_subtrees, void *data)
 {
-  struct move_stmt_d *p = (struct move_stmt_d *) data;
+  struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
+  struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
   tree t = *tp;
 
-  if (EXPR_P (t) || GIMPLE_STMT_P (t))
-    {
-      tree block = TREE_BLOCK (t);
-      if (p->orig_block == NULL_TREE
-         || block == p->orig_block
-         || block == NULL_TREE)
-       TREE_BLOCK (t) = p->new_block;
-#ifdef ENABLE_CHECKING
-      else if (block != p->new_block)
-       {
-         while (block && block != p->orig_block)
-           block = BLOCK_SUPERCONTEXT (block);
-         gcc_assert (block);
-       }
-#endif
-    }
-
-  if (OMP_DIRECTIVE_P (t)
-      && TREE_CODE (t) != OMP_RETURN
-      && TREE_CODE (t) != OMP_CONTINUE)
-    {
-      /* Do not remap variables inside OMP directives.  Variables
-        referenced in clauses and directive header belong to the
-        parent function and should not be moved into the child
-        function.  */
-      bool save_remap_decls_p = p->remap_decls_p;
-      p->remap_decls_p = false;
-      *walk_subtrees = 0;
-
-      walk_tree (&OMP_BODY (t), move_stmt_r, p, NULL);
+  if (EXPR_P (t))
+    /* We should never have TREE_BLOCK set on non-statements.  */
+    gcc_assert (!TREE_BLOCK (t));
 
-      p->remap_decls_p = save_remap_decls_p;
-    }
   else if (DECL_P (t) || TREE_CODE (t) == SSA_NAME)
     {
       if (TREE_CODE (t) == SSA_NAME)
@@ -5747,7 +5530,7 @@ move_stmt_r (tree *tp, int *walk_subtrees, void *data)
               && !is_global_var (t))
              || TREE_CODE (t) == CONST_DECL)
            replace_by_duplicate_decl (tp, p->vars_map, p->to_context);
-         
+
          if (SSA_VAR_P (t)
              && gimple_in_ssa_p (cfun))
            {
@@ -5764,33 +5547,145 @@ move_stmt_r (tree *tp, int *walk_subtrees, void *data)
   return NULL_TREE;
 }
 
-/* Marks virtual operands of all statements in basic blocks BBS for
-   renaming.  */
+/* Helper for move_stmt_r.  Given an EH region number for the source
+   function, map that to the duplicate EH regio number in the dest.  */
 
-void
-mark_virtual_ops_in_bb (basic_block bb)
+static int
+move_stmt_eh_region_nr (int old_nr, struct move_stmt_d *p)
+{
+  eh_region old_r, new_r;
+  void **slot;
+
+  old_r = get_eh_region_from_number (old_nr);
+  slot = pointer_map_contains (p->eh_map, old_r);
+  new_r = (eh_region) *slot;
+
+  return new_r->index;
+}
+
+/* Similar, but operate on INTEGER_CSTs.  */
+
+static tree
+move_stmt_eh_region_tree_nr (tree old_t_nr, struct move_stmt_d *p)
 {
-  tree phi;
-  block_stmt_iterator bsi;
+  int old_nr, new_nr;
+
+  old_nr = tree_low_cst (old_t_nr, 0);
+  new_nr = move_stmt_eh_region_nr (old_nr, p);
+
+  return build_int_cst (NULL, new_nr);
+}
+
+/* Like move_stmt_op, but for gimple statements.
+
+   Helper for move_block_to_fn.  Set GIMPLE_BLOCK in every expression
+   contained in the current statement in *GSI_P and change the
+   DECL_CONTEXT of every local variable referenced in the current
+   statement.  */
+
+static tree
+move_stmt_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
+            struct walk_stmt_info *wi)
+{
+  struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
+  gimple stmt = gsi_stmt (*gsi_p);
+  tree block = gimple_block (stmt);
+
+  if (p->orig_block == NULL_TREE
+      || block == p->orig_block
+      || block == NULL_TREE)
+    gimple_set_block (stmt, p->new_block);
+#ifdef ENABLE_CHECKING
+  else if (block != p->new_block)
+    {
+      while (block && block != p->orig_block)
+       block = BLOCK_SUPERCONTEXT (block);
+      gcc_assert (block);
+    }
+#endif
+
+  switch (gimple_code (stmt))
+    {
+    case GIMPLE_CALL:
+      /* Remap the region numbers for __builtin_eh_{pointer,filter}.  */
+      {
+       tree r, fndecl = gimple_call_fndecl (stmt);
+       if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
+         switch (DECL_FUNCTION_CODE (fndecl))
+           {
+           case BUILT_IN_EH_COPY_VALUES:
+             r = gimple_call_arg (stmt, 1);
+             r = move_stmt_eh_region_tree_nr (r, p);
+             gimple_call_set_arg (stmt, 1, r);
+             /* FALLTHRU */
+
+           case BUILT_IN_EH_POINTER:
+           case BUILT_IN_EH_FILTER:
+             r = gimple_call_arg (stmt, 0);
+             r = move_stmt_eh_region_tree_nr (r, p);
+             gimple_call_set_arg (stmt, 0, r);
+             break;
+
+           default:
+             break;
+           }
+      }
+      break;
+
+    case GIMPLE_RESX:
+      {
+       int r = gimple_resx_region (stmt);
+       r = move_stmt_eh_region_nr (r, p);
+       gimple_resx_set_region (stmt, r);
+      }
+      break;
+
+    case GIMPLE_EH_DISPATCH:
+      {
+       int r = gimple_eh_dispatch_region (stmt);
+       r = move_stmt_eh_region_nr (r, p);
+       gimple_eh_dispatch_set_region (stmt, r);
+      }
+      break;
 
-  for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
-    mark_virtual_ops_for_renaming (phi);
+    case GIMPLE_OMP_RETURN:
+    case GIMPLE_OMP_CONTINUE:
+      break;
+    default:
+      if (is_gimple_omp (stmt))
+       {
+         /* Do not remap variables inside OMP directives.  Variables
+            referenced in clauses and directive header belong to the
+            parent function and should not be moved into the child
+            function.  */
+         bool save_remap_decls_p = p->remap_decls_p;
+         p->remap_decls_p = false;
+         *handled_ops_p = true;
+
+         walk_gimple_seq (gimple_omp_body (stmt), move_stmt_r,
+                          move_stmt_op, wi);
+
+         p->remap_decls_p = save_remap_decls_p;
+       }
+      break;
+    }
 
-  for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
-    mark_virtual_ops_for_renaming (bsi_stmt (bsi));
+  return NULL_TREE;
 }
 
 /* Marks virtual operands of all statements in basic blocks BBS for
    renaming.  */
 
-static void
-mark_virtual_ops_in_region (VEC (basic_block,heap) *bbs)
+void
+mark_virtual_ops_in_bb (basic_block bb)
 {
-  basic_block bb;
-  unsigned i;
+  gimple_stmt_iterator gsi;
 
-  for (i = 0; VEC_iterate (basic_block, bbs, i, bb); i++)
-    mark_virtual_ops_in_bb (bb);
+  for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+    mark_virtual_ops_for_renaming (gsi_stmt (gsi));
+
+  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+    mark_virtual_ops_for_renaming (gsi_stmt (gsi));
 }
 
 /* Move basic block BB from function CFUN to function DEST_FN.  The
@@ -5806,14 +5701,13 @@ mark_virtual_ops_in_region (VEC (basic_block,heap) *bbs)
 static void
 move_block_to_fn (struct function *dest_cfun, basic_block bb,
                  basic_block after, bool update_edge_count_p,
-                 struct move_stmt_d *d, int eh_offset)
+                 struct move_stmt_d *d)
 {
   struct control_flow_graph *cfg;
   edge_iterator ei;
   edge e;
-  block_stmt_iterator si;
+  gimple_stmt_iterator si;
   unsigned old_len, new_len;
-  tree phi, next_phi;
 
   /* Remove BB from dominance structures.  */
   delete_from_dominance_info (CDI_DOMINATORS, bb);
@@ -5853,18 +5747,18 @@ move_block_to_fn (struct function *dest_cfun, basic_block bb,
                bb->index, bb);
 
   /* Remap the variables in phi nodes.  */
-  for (phi = phi_nodes (bb); phi; phi = next_phi)
+  for (si = gsi_start_phis (bb); !gsi_end_p (si); )
     {
+      gimple phi = gsi_stmt (si);
       use_operand_p use;
       tree op = PHI_RESULT (phi);
       ssa_op_iter oi;
 
-      next_phi = PHI_CHAIN (phi);
       if (!is_gimple_reg (op))
        {
          /* Remove the phi nodes for virtual operands (alias analysis will be
             run for the new function, anyway).  */
-          remove_phi_node (phi, NULL, true);
+          remove_phi_node (&si, true);
          continue;
        }
 
@@ -5876,18 +5770,22 @@ move_block_to_fn (struct function *dest_cfun, basic_block bb,
          if (TREE_CODE (op) == SSA_NAME)
            SET_USE (use, replace_ssa_name (op, d->vars_map, dest_cfun->decl));
        }
+
+      gsi_next (&si);
     }
 
-  for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
+  for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
     {
-      tree stmt = bsi_stmt (si);
-      int region;
+      gimple stmt = gsi_stmt (si);
+      struct walk_stmt_info wi;
 
-      walk_tree (&stmt, move_stmt_r, d, NULL);
+      memset (&wi, 0, sizeof (wi));
+      wi.info = d;
+      walk_gimple_stmt (&si, move_stmt_r, move_stmt_op, &wi);
 
-      if (TREE_CODE (stmt) == LABEL_EXPR)
+      if (gimple_code (stmt) == GIMPLE_LABEL)
        {
-         tree label = LABEL_EXPR_LABEL (stmt);
+         tree label = gimple_label_label (stmt);
          int uid = LABEL_DECL_UID (label);
 
          gcc_assert (uid > -1);
@@ -5895,7 +5793,7 @@ move_block_to_fn (struct function *dest_cfun, basic_block bb,
          old_len = VEC_length (basic_block, cfg->x_label_to_block_map);
          if (old_len <= (unsigned) uid)
            {
-             new_len = 3 * uid / 2;
+             new_len = 3 * uid / 2 + 1;
              VEC_safe_grow_cleared (basic_block, gc,
                                     cfg->x_label_to_block_map, new_len);
            }
@@ -5908,20 +5806,12 @@ move_block_to_fn (struct function *dest_cfun, basic_block bb,
          if (uid >= dest_cfun->cfg->last_label_uid)
            dest_cfun->cfg->last_label_uid = uid + 1;
        }
-      else if (TREE_CODE (stmt) == RESX_EXPR && eh_offset != 0)
-       TREE_OPERAND (stmt, 0) =
-         build_int_cst (NULL_TREE,
-                        TREE_INT_CST_LOW (TREE_OPERAND (stmt, 0))
-                        + eh_offset);
-
-      region = lookup_stmt_eh_region (stmt);
-      if (region >= 0)
-       {
-         add_stmt_to_eh_region_fn (dest_cfun, stmt, region + eh_offset);
-         remove_stmt_from_eh_region (stmt);
-         gimple_duplicate_stmt_histograms (dest_cfun, stmt, cfun, stmt);
-          gimple_remove_stmt_histograms (cfun, stmt);
-       }
+
+      maybe_duplicate_eh_stmt_fn (dest_cfun, stmt, cfun, stmt, d->eh_map, 0);
+      remove_stmt_from_eh_lp_fn (cfun, stmt);
+
+      gimple_duplicate_stmt_histograms (dest_cfun, stmt, cfun, stmt);
+      gimple_remove_stmt_histograms (cfun, stmt);
 
       /* We cannot leave any operands allocated from the operand caches of
         the current function.  */
@@ -5930,34 +5820,50 @@ move_block_to_fn (struct function *dest_cfun, basic_block bb,
       update_stmt (stmt);
       pop_cfun ();
     }
+
+  FOR_EACH_EDGE (e, ei, bb->succs)
+    if (e->goto_locus)
+      {
+       tree block = e->goto_block;
+       if (d->orig_block == NULL_TREE
+           || block == d->orig_block)
+         e->goto_block = d->new_block;
+#ifdef ENABLE_CHECKING
+       else if (block != d->new_block)
+         {
+           while (block && block != d->orig_block)
+             block = BLOCK_SUPERCONTEXT (block);
+           gcc_assert (block);
+         }
+#endif
+      }
 }
 
 /* Examine the statements in BB (which is in SRC_CFUN); find and return
    the outermost EH region.  Use REGION as the incoming base EH region.  */
 
-static int
+static eh_region
 find_outermost_region_in_block (struct function *src_cfun,
-                               basic_block bb, int region)
+                               basic_block bb, eh_region region)
 {
-  block_stmt_iterator si;
+  gimple_stmt_iterator si;
 
-  for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
+  for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
     {
-      tree stmt = bsi_stmt (si);
-      int stmt_region;
+      gimple stmt = gsi_stmt (si);
+      eh_region stmt_region;
+      int lp_nr;
 
-      if (TREE_CODE (stmt) == RESX_EXPR)
-       stmt_region = TREE_INT_CST_LOW (TREE_OPERAND (stmt, 0));
-      else
-       stmt_region = lookup_stmt_eh_region_fn (src_cfun, stmt);
-      if (stmt_region > 0)
+      lp_nr = lookup_stmt_eh_lp_fn (src_cfun, stmt);
+      stmt_region = get_eh_region_from_lp_number_fn (src_cfun, lp_nr);
+      if (stmt_region)
        {
-         if (region < 0)
+         if (region == NULL)
            region = stmt_region;
          else if (stmt_region != region)
            {
              region = eh_region_outermost (src_cfun, stmt_region, region);
-             gcc_assert (region != -1);
+             gcc_assert (region != NULL);
            }
        }
     }
@@ -5977,7 +5883,7 @@ new_label_mapper (tree decl, void *data)
   m = XNEW (struct tree_map);
   m->hash = DECL_UID (decl);
   m->base.from = decl;
-  m->to = create_artificial_label ();
+  m->to = create_artificial_label (UNKNOWN_LOCATION);
   LABEL_DECL_UID (m->to) = LABEL_DECL_UID (decl);
   if (LABEL_DECL_UID (m->to) >= cfun->cfg->last_label_uid)
     cfun->cfg->last_label_uid = LABEL_DECL_UID (m->to) + 1;
@@ -6002,6 +5908,8 @@ replace_block_vars_by_duplicates (tree block, struct pointer_map_t *vars_map,
   for (tp = &BLOCK_VARS (block); *tp; tp = &TREE_CHAIN (*tp))
     {
       t = *tp;
+      if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != CONST_DECL)
+       continue;
       replace_by_duplicate_decl (&t, vars_map, to_context);
       if (t != *tp)
        {
@@ -6045,13 +5953,13 @@ move_sese_region_to_fn (struct function *dest_cfun, basic_block entry_bb,
   basic_block dom_entry = get_immediate_dominator (CDI_DOMINATORS, entry_bb);
   basic_block after, bb, *entry_pred, *exit_succ, abb;
   struct function *saved_cfun = cfun;
-  int *entry_flag, *exit_flag, eh_offset;
+  int *entry_flag, *exit_flag;
   unsigned *entry_prob, *exit_prob;
   unsigned i, num_entry_edges, num_exit_edges;
   edge e;
   edge_iterator ei;
   htab_t new_label_map;
-  struct pointer_map_t *vars_map;
+  struct pointer_map_t *vars_map, *eh_map;
   struct loop *loop = entry_bb->loop_father;
   struct move_stmt_d d;
 
@@ -6121,51 +6029,47 @@ move_sese_region_to_fn (struct function *dest_cfun, basic_block entry_bb,
   init_empty_tree_cfg ();
 
   /* Initialize EH information for the new function.  */
-  eh_offset = 0;
+  eh_map = NULL;
   new_label_map = NULL;
   if (saved_cfun->eh)
     {
-      int region = -1;
+      eh_region region = NULL;
 
       for (i = 0; VEC_iterate (basic_block, bbs, i, bb); i++)
        region = find_outermost_region_in_block (saved_cfun, bb, region);
 
       init_eh_for_function ();
-      if (region != -1)
+      if (region != NULL)
        {
          new_label_map = htab_create (17, tree_map_hash, tree_map_eq, free);
-         eh_offset = duplicate_eh_regions (saved_cfun, new_label_mapper,
-                                           new_label_map, region, 0);
+         eh_map = duplicate_eh_regions (saved_cfun, region, 0,
+                                        new_label_mapper, new_label_map);
        }
     }
 
   pop_cfun ();
 
-  /* The ssa form for virtual operands in the source function will have to
-     be repaired.  We do not care for the real operands -- the sese region
-     must be closed with respect to those.  */
-  mark_virtual_ops_in_region (bbs);
-
   /* Move blocks from BBS into DEST_CFUN.  */
   gcc_assert (VEC_length (basic_block, bbs) >= 2);
   after = dest_cfun->cfg->x_entry_block_ptr;
   vars_map = pointer_map_create ();
 
   memset (&d, 0, sizeof (d));
-  d.vars_map = vars_map;
+  d.orig_block = orig_block;
+  d.new_block = DECL_INITIAL (dest_cfun->decl);
   d.from_context = cfun->decl;
   d.to_context = dest_cfun->decl;
+  d.vars_map = vars_map;
   d.new_label_map = new_label_map;
+  d.eh_map = eh_map;
   d.remap_decls_p = true;
-  d.orig_block = orig_block;
-  d.new_block = DECL_INITIAL (dest_cfun->decl);
 
   for (i = 0; VEC_iterate (basic_block, bbs, i, bb); i++)
     {
       /* No need to update edge counts on the last block.  It has
         already been updated earlier when we detached the region from
         the original CFG.  */
-      move_block_to_fn (dest_cfun, bb, after, bb != exit_bb, &d, eh_offset);
+      move_block_to_fn (dest_cfun, bb, after, bb != exit_bb, &d);
       after = bb;
     }
 
@@ -6188,6 +6092,8 @@ move_sese_region_to_fn (struct function *dest_cfun, basic_block entry_bb,
 
   if (new_label_map)
     htab_delete (new_label_map);
+  if (eh_map)
+    pointer_map_destroy (eh_map);
   pointer_map_destroy (vars_map);
 
   /* Rewire the entry and exit blocks.  The successor to the entry
@@ -6242,7 +6148,8 @@ move_sese_region_to_fn (struct function *dest_cfun, basic_block entry_bb,
 }
 
 
-/* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in tree.h)  */
+/* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in tree-pass.h)
+   */
 
 void
 dump_function_to_file (tree fn, FILE *file, int flags)
@@ -6273,10 +6180,10 @@ dump_function_to_file (tree fn, FILE *file, int flags)
     print_node (file, "", fn, 2);
 
   dsf = DECL_STRUCT_FUNCTION (fn);
-  if (dsf && (flags & TDF_DETAILS))
+  if (dsf && (flags & TDF_EH))
     dump_eh_tree (file, dsf);
 
-  if (flags & TDF_RAW)
+  if (flags & TDF_RAW && !gimple_has_body_p (fn))
     {
       dump_node (fn, TDF_SLIM | flags, file);
       return;
@@ -6307,7 +6214,7 @@ dump_function_to_file (tree fn, FILE *file, int flags)
 
   if (cfun && cfun->decl == fn && cfun->cfg && basic_block_info)
     {
-      /* Make a CFG based dump.  */
+      /* If the CFG has been built, emit a CFG-based dump.  */
       check_bb_profile (ENTRY_BLOCK_PTR, file);
       if (!ignore_topmost_bind)
        fprintf (file, "{\n");
@@ -6316,11 +6223,34 @@ dump_function_to_file (tree fn, FILE *file, int flags)
        fprintf (file, "\n");
 
       FOR_EACH_BB (bb)
-       dump_generic_bb (file, bb, 2, flags);
+       gimple_dump_bb (bb, file, 2, flags);
 
       fprintf (file, "}\n");
       check_bb_profile (EXIT_BLOCK_PTR, file);
     }
+  else if (DECL_SAVED_TREE (fn) == NULL)
+    {
+      /* The function is now in GIMPLE form but the CFG has not been
+        built yet.  Emit the single sequence of GIMPLE statements
+        that make up its body.  */
+      gimple_seq body = gimple_body (fn);
+
+      if (gimple_seq_first_stmt (body)
+         && gimple_seq_first_stmt (body) == gimple_seq_last_stmt (body)
+         && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND)
+       print_gimple_seq (file, body, 0, flags);
+      else
+       {
+         if (!ignore_topmost_bind)
+           fprintf (file, "{\n");
+
+         if (any_var)
+           fprintf (file, "\n");
+
+         print_gimple_seq (file, body, 2, flags);
+         fprintf (file, "}\n");
+       }
+    }
   else
     {
       int indent;
@@ -6396,7 +6326,7 @@ print_succ_bbs (FILE *file, basic_block bb)
 
 /* Print to FILE the basic block BB following the VERBOSITY level.  */
 
-void 
+void
 print_loops_bb (FILE *file, basic_block bb, int indent, int verbosity)
 {
   char *s_indent = (char *) alloca ((size_t) indent + 1);
@@ -6417,7 +6347,7 @@ print_loops_bb (FILE *file, basic_block bb, int indent, int verbosity)
   if (verbosity >= 3)
     {
       fprintf (file, "%s  {\n", s_indent);
-      tree_dump_bb (bb, file, indent + 4);
+      gimple_dump_bb (bb, file, indent + 4, TDF_VOPS|TDF_MEMSYMS);
       fprintf (file, "%s  }\n", s_indent);
     }
 }
@@ -6442,7 +6372,7 @@ print_loop (FILE *file, struct loop *loop, int indent, int verbosity)
   s_indent[indent] = '\0';
 
   /* Print loop's header.  */
-  fprintf (file, "%sloop_%d (header = %d, latch = %d", s_indent, 
+  fprintf (file, "%sloop_%d (header = %d, latch = %d", s_indent,
           loop->num, loop->header->index, loop->latch->index);
   fprintf (file, ", niter = ");
   print_generic_expr (file, loop->nb_iterations, 0);
@@ -6495,7 +6425,7 @@ print_loops (FILE *file, int verbosity)
 {
   basic_block bb;
 
-  bb = BASIC_BLOCK (NUM_FIXED_BLOCKS);
+  bb = ENTRY_BLOCK_PTR;
   if (bb && bb->loop_father)
     print_loop_and_siblings (file, bb->loop_father, 0, verbosity);
 }
@@ -6531,10 +6461,10 @@ debug_loop_num (unsigned num, int verbosity)
    otherwise.  */
 
 static bool
-tree_block_ends_with_call_p (basic_block bb)
+gimple_block_ends_with_call_p (basic_block bb)
 {
-  block_stmt_iterator bsi = bsi_last (bb);
-  return get_call_expr_in (bsi_stmt (bsi)) != NULL;
+  gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
+  return is_gimple_call (gsi_stmt (gsi));
 }
 
 
@@ -6542,23 +6472,21 @@ tree_block_ends_with_call_p (basic_block bb)
    otherwise.  */
 
 static bool
-tree_block_ends_with_condjump_p (const_basic_block bb)
+gimple_block_ends_with_condjump_p (const_basic_block bb)
 {
-  /* This CONST_CAST is okay because last_stmt doesn't modify its
-     argument and the return value is not modified.  */
-  const_tree stmt = last_stmt (CONST_CAST_BB(bb));
-  return (stmt && TREE_CODE (stmt) == COND_EXPR);
+  gimple stmt = last_stmt (CONST_CAST_BB (bb));
+  return (stmt && gimple_code (stmt) == GIMPLE_COND);
 }
 
 
 /* Return true if we need to add fake edge to exit at statement T.
-   Helper function for tree_flow_call_edges_add.  */
+   Helper function for gimple_flow_call_edges_add.  */
 
 static bool
-need_fake_edge_p (tree t)
+need_fake_edge_p (gimple t)
 {
-  tree call, fndecl = NULL_TREE;
-  int call_flags;
+  tree fndecl = NULL_TREE;
+  int call_flags = 0;
 
   /* NORETURN and LONGJMP calls already have an edge to exit.
      CONST and PURE calls do not need one.
@@ -6567,24 +6495,31 @@ need_fake_edge_p (tree t)
      figured out from the RTL in mark_constant_function, and
      the counter incrementation code from -fprofile-arcs
      leads to different results from -fbranch-probabilities.  */
-  call = get_call_expr_in (t);
-  if (call)
+  if (is_gimple_call (t))
     {
-      fndecl = get_callee_fndecl (call);
-      call_flags = call_expr_flags (call);
+      fndecl = gimple_call_fndecl (t);
+      call_flags = gimple_call_flags (t);
     }
 
-  if (call && fndecl && DECL_BUILT_IN (fndecl)
+  if (is_gimple_call (t)
+      && fndecl
+      && DECL_BUILT_IN (fndecl)
       && (call_flags & ECF_NOTHROW)
-      && !(call_flags & ECF_NORETURN)
-      && !(call_flags & ECF_RETURNS_TWICE))
-   return false;
+      && !(call_flags & ECF_RETURNS_TWICE)
+      /* fork() doesn't really return twice, but the effect of
+         wrapping it in __gcov_fork() which calls __gcov_flush()
+        and clears the counters before forking has the same
+        effect as returning twice.  Force a fake edge.  */
+      && !(DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
+          && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FORK))
+    return false;
 
-  if (call && !(call_flags & ECF_NORETURN))
+  if (is_gimple_call (t)
+      && !(call_flags & ECF_NORETURN))
     return true;
 
-  if (TREE_CODE (t) == ASM_EXPR
-       && (ASM_VOLATILE_P (t) || ASM_INPUT_P (t)))
+  if (gimple_code (t) == GIMPLE_ASM
+       && (gimple_asm_volatile_p (t) || gimple_asm_input_p (t)))
     return true;
 
   return false;
@@ -6600,7 +6535,7 @@ need_fake_edge_p (tree t)
    not imply that all subsequent instructions must be executed.  */
 
 static int
-tree_flow_call_edges_add (sbitmap blocks)
+gimple_flow_call_edges_add (sbitmap blocks)
 {
   int i;
   int blocks_split = 0;
@@ -6630,10 +6565,11 @@ tree_flow_call_edges_add (sbitmap blocks)
   if (check_last_block)
     {
       basic_block bb = EXIT_BLOCK_PTR->prev_bb;
-      block_stmt_iterator bsi = bsi_last (bb);
-      tree t = NULL_TREE;
-      if (!bsi_end_p (bsi))
-       t = bsi_stmt (bsi);
+      gimple_stmt_iterator gsi = gsi_last_bb (bb);
+      gimple t = NULL;
+
+      if (!gsi_end_p (gsi))
+       t = gsi_stmt (gsi);
 
       if (t && need_fake_edge_p (t))
        {
@@ -6642,8 +6578,8 @@ tree_flow_call_edges_add (sbitmap blocks)
          e = find_edge (bb, EXIT_BLOCK_PTR);
          if (e)
            {
-             bsi_insert_on_edge (e, build_empty_stmt ());
-             bsi_commit_edge_inserts ();
+             gsi_insert_on_edge (e, gimple_build_nop ());
+             gsi_commit_edge_inserts ();
            }
        }
     }
@@ -6654,8 +6590,8 @@ tree_flow_call_edges_add (sbitmap blocks)
   for (i = 0; i < last_bb; i++)
     {
       basic_block bb = BASIC_BLOCK (i);
-      block_stmt_iterator bsi;
-      tree stmt, last_stmt;
+      gimple_stmt_iterator gsi;
+      gimple stmt, last_stmt;
 
       if (!bb)
        continue;
@@ -6663,16 +6599,17 @@ tree_flow_call_edges_add (sbitmap blocks)
       if (blocks && !TEST_BIT (blocks, i))
        continue;
 
-      bsi = bsi_last (bb);
-      if (!bsi_end_p (bsi))
+      gsi = gsi_last_bb (bb);
+      if (!gsi_end_p (gsi))
        {
-         last_stmt = bsi_stmt (bsi);
+         last_stmt = gsi_stmt (gsi);
          do
            {
-             stmt = bsi_stmt (bsi);
+             stmt = gsi_stmt (gsi);
              if (need_fake_edge_p (stmt))
                {
                  edge e;
+
                  /* The handling above of the final block before the
                     epilogue should be enough to verify that there is
                     no edge to the exit block in CFG already.
@@ -6696,9 +6633,9 @@ tree_flow_call_edges_add (sbitmap blocks)
                    }
                  make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
                }
-             bsi_prev (&bsi);
+             gsi_prev (&gsi);
            }
-         while (!bsi_end_p (bsi));
+         while (!gsi_end_p (gsi));
        }
     }
 
@@ -6711,17 +6648,17 @@ tree_flow_call_edges_add (sbitmap blocks)
 /* Purge dead abnormal call edges from basic block BB.  */
 
 bool
-tree_purge_dead_abnormal_call_edges (basic_block bb)
+gimple_purge_dead_abnormal_call_edges (basic_block bb)
 {
-  bool changed = tree_purge_dead_eh_edges (bb);
+  bool changed = gimple_purge_dead_eh_edges (bb);
 
   if (cfun->has_nonlocal_label)
     {
-      tree stmt = last_stmt (bb);
+      gimple stmt = last_stmt (bb);
       edge_iterator ei;
       edge e;
 
-      if (!(stmt && tree_can_make_abnormal_goto (stmt)))
+      if (!(stmt && stmt_can_make_abnormal_goto (stmt)))
        for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
          {
            if (e->flags & EDGE_ABNORMAL)
@@ -6733,7 +6670,7 @@ tree_purge_dead_abnormal_call_edges (basic_block bb)
              ei_next (&ei);
          }
 
-      /* See tree_purge_dead_eh_edges below.  */
+      /* See gimple_purge_dead_eh_edges below.  */
       if (changed)
        free_dominance_info (CDI_DOMINATORS);
     }
@@ -6741,20 +6678,6 @@ tree_purge_dead_abnormal_call_edges (basic_block bb)
   return changed;
 }
 
-/* Stores all basic blocks dominated by BB to DOM_BBS.  */
-
-static void
-get_all_dominated_blocks (basic_block bb, VEC (basic_block, heap) **dom_bbs)
-{
-  basic_block son;
-
-  VEC_safe_push (basic_block, heap, *dom_bbs, bb);
-  for (son = first_dom_son (CDI_DOMINATORS, bb);
-       son;
-       son = next_dom_son (CDI_DOMINATORS, son))
-    get_all_dominated_blocks (son, dom_bbs);
-}
-
 /* Removes edge E and all the blocks dominated by it, and updates dominance
    information.  The IL in E->src needs to be updated separately.
    If dominance info is not available, only the edge E is removed.*/
@@ -6814,7 +6737,7 @@ remove_edge_and_dominated_blocks (edge e)
                    get_immediate_dominator (CDI_DOMINATORS, e->dest)->index);
   else
     {
-      get_all_dominated_blocks (e->dest, &bbs_to_remove);
+      bbs_to_remove = get_all_dominated_blocks (CDI_DOMINATORS, e->dest);
       for (i = 0; VEC_iterate (basic_block, bbs_to_remove, i, bb); i++)
        {
          FOR_EACH_EDGE (f, ei, bb->succs)
@@ -6846,20 +6769,24 @@ remove_edge_and_dominated_blocks (edge e)
     remove_edge (e);
   else
     {
-      for (i = 0; VEC_iterate (basic_block, bbs_to_remove, i, bb); i++)
-       delete_basic_block (bb);
+      /* Walk backwards so as to get a chance to substitute all
+        released DEFs into debug stmts.  See
+        eliminate_unnecessary_stmts() in tree-ssa-dce.c for more
+        details.  */
+      for (i = VEC_length (basic_block, bbs_to_remove); i-- > 0; )
+       delete_basic_block (VEC_index (basic_block, bbs_to_remove, i));
     }
 
   /* Update the dominance information.  The immediate dominator may change only
      for blocks whose immediate dominator belongs to DF_IDOM:
-   
+
      Suppose that idom(X) = Y before removal of E and idom(X) != Y after the
      removal.  Let Z the arbitrary block such that idom(Z) = Y and
      Z dominates X after the removal.  Before removal, there exists a path P
      from Y to X that avoids Z.  Let F be the last edge on P that is
      removed, and let W = F->dest.  Before removal, idom(W) = Y (since Y
      dominates W, and because of P, Z does not dominate W), and W belongs to
-     the dominance frontier of E.  Therefore, Y belongs to DF_IDOM.  */ 
+     the dominance frontier of E.  Therefore, Y belongs to DF_IDOM.  */
   EXECUTE_IF_SET_IN_BITMAP (df_idom, 0, i, bi)
     {
       bb = BASIC_BLOCK (i);
@@ -6880,14 +6807,14 @@ remove_edge_and_dominated_blocks (edge e)
 /* Purge dead EH edges from basic block BB.  */
 
 bool
-tree_purge_dead_eh_edges (basic_block bb)
+gimple_purge_dead_eh_edges (basic_block bb)
 {
   bool changed = false;
   edge e;
   edge_iterator ei;
-  tree stmt = last_stmt (bb);
+  gimple stmt = last_stmt (bb);
 
-  if (stmt && tree_can_throw_internal (stmt))
+  if (stmt && stmt_can_throw_internal (stmt))
     return false;
 
   for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
@@ -6905,7 +6832,7 @@ tree_purge_dead_eh_edges (basic_block bb)
 }
 
 bool
-tree_purge_all_dead_eh_edges (const_bitmap blocks)
+gimple_purge_all_dead_eh_edges (const_bitmap blocks)
 {
   bool changed = false;
   unsigned i;
@@ -6913,7 +6840,13 @@ tree_purge_all_dead_eh_edges (const_bitmap blocks)
 
   EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
     {
-      changed |= tree_purge_dead_eh_edges (BASIC_BLOCK (i));
+      basic_block bb = BASIC_BLOCK (i);
+
+      /* Earlier gimple_purge_dead_eh_edges could have removed
+        this basic block already.  */
+      gcc_assert (bb || changed);
+      if (bb != NULL)
+       changed |= gimple_purge_dead_eh_edges (bb);
     }
 
   return changed;
@@ -6923,11 +6856,11 @@ tree_purge_all_dead_eh_edges (const_bitmap blocks)
    redirected.  */
 
 static void
-tree_execute_on_growing_pred (edge e)
+gimple_execute_on_growing_pred (edge e)
 {
   basic_block bb = e->dest;
 
-  if (phi_nodes (bb))
+  if (!gimple_seq_empty_p (phi_nodes (bb)))
     reserve_phi_args_for_new_edge (bb);
 }
 
@@ -6935,9 +6868,9 @@ tree_execute_on_growing_pred (edge e)
    the edge vector E->dest->preds.  */
 
 static void
-tree_execute_on_shrinking_pred (edge e)
+gimple_execute_on_shrinking_pred (edge e)
 {
-  if (phi_nodes (e->dest))
+  if (!gimple_seq_empty_p (phi_nodes (e->dest)))
     remove_phi_args (e);
 }
 
@@ -6951,14 +6884,15 @@ tree_execute_on_shrinking_pred (edge e)
    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.
-*/
+   splitting.  */
 
 static void
-tree_lv_adjust_loop_header_phi (basic_block first, basic_block second,
-                               basic_block new_head, edge e)
+gimple_lv_adjust_loop_header_phi (basic_block first, basic_block second,
+                                 basic_block new_head, edge e)
 {
-  tree phi1, phi2;
+  gimple phi1, phi2;
+  gimple_stmt_iterator psi1, psi2;
+  tree def;
   edge e2 = find_edge (new_head, second);
 
   /* Because NEW_HEAD has been created by splitting SECOND's incoming
@@ -6968,35 +6902,41 @@ tree_lv_adjust_loop_header_phi (basic_block first, basic_block second,
   /* 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))
+  for (psi2 = gsi_start_phis (second),
+       psi1 = gsi_start_phis (first);
+       !gsi_end_p (psi2) && !gsi_end_p (psi1);
+       gsi_next (&psi2),  gsi_next (&psi1))
     {
-      tree def = PHI_ARG_DEF (phi2, e2->dest_idx);
-      add_phi_arg (phi1, def, e);
+      phi1 = gsi_stmt (psi1);
+      phi2 = gsi_stmt (psi2);
+      def = PHI_ARG_DEF (phi2, e2->dest_idx);
+      add_phi_arg (phi1, def, e, gimple_phi_arg_location_from_edge (phi2, e2));
     }
 }
 
+
 /* 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 ATTRIBUTE_UNUSED,
-                            basic_block second_head ATTRIBUTE_UNUSED,
-                            basic_block cond_bb, void *cond_e)
+gimple_lv_add_condition_to_bb (basic_block first_head ATTRIBUTE_UNUSED,
+                              basic_block second_head ATTRIBUTE_UNUSED,
+                              basic_block cond_bb, void *cond_e)
 {
-  block_stmt_iterator bsi;
-  tree new_cond_expr = NULL_TREE;
+  gimple_stmt_iterator gsi;
+  gimple new_cond_expr;
   tree cond_expr = (tree) cond_e;
   edge e0;
 
   /* Build new conditional expr */
-  new_cond_expr = build3 (COND_EXPR, void_type_node, cond_expr,
-                         NULL_TREE, NULL_TREE);
+  new_cond_expr = gimple_build_cond_from_tree (cond_expr,
+                                              NULL_TREE, NULL_TREE);
 
   /* Add new cond in cond_bb.  */
-  bsi = bsi_start (cond_bb);
-  bsi_insert_after (&bsi, new_cond_expr, BSI_NEW_STMT);
+  gsi = gsi_last_bb (cond_bb);
+  gsi_insert_after (&gsi, new_cond_expr, GSI_NEW_STMT);
+
   /* Adjust edges appropriately to connect new head with first head
      as well as second head.  */
   e0 = single_succ_edge (cond_bb);
@@ -7004,34 +6944,34 @@ tree_lv_add_condition_to_bb (basic_block first_head ATTRIBUTE_UNUSED,
   e0->flags |= EDGE_FALSE_VALUE;
 }
 
-struct cfg_hooks tree_cfg_hooks = {
-  "tree",
-  tree_verify_flow_info,
-  tree_dump_bb,                        /* dump_bb  */
+struct cfg_hooks gimple_cfg_hooks = {
+  "gimple",
+  gimple_verify_flow_info,
+  gimple_dump_bb,              /* dump_bb  */
   create_bb,                   /* create_basic_block  */
-  tree_redirect_edge_and_branch,/* redirect_edge_and_branch  */
-  tree_redirect_edge_and_branch_force,/* redirect_edge_and_branch_force  */
-  tree_can_remove_branch_p,    /* can_remove_branch_p  */
+  gimple_redirect_edge_and_branch, /* redirect_edge_and_branch  */
+  gimple_redirect_edge_and_branch_force, /* redirect_edge_and_branch_force  */
+  gimple_can_remove_branch_p,  /* can_remove_branch_p  */
   remove_bb,                   /* delete_basic_block  */
-  tree_split_block,            /* split_block  */
-  tree_move_block_after,       /* move_block_after  */
-  tree_can_merge_blocks_p,     /* can_merge_blocks_p  */
-  tree_merge_blocks,           /* merge_blocks  */
-  tree_predict_edge,           /* predict_edge  */
-  tree_predicted_by_p,         /* predicted_by_p  */
-  tree_can_duplicate_bb_p,     /* can_duplicate_block_p  */
-  tree_duplicate_bb,           /* duplicate_block  */
-  tree_split_edge,             /* split_edge  */
-  tree_make_forwarder_block,   /* make_forward_block  */
+  gimple_split_block,          /* split_block  */
+  gimple_move_block_after,     /* move_block_after  */
+  gimple_can_merge_blocks_p,   /* can_merge_blocks_p  */
+  gimple_merge_blocks,         /* merge_blocks  */
+  gimple_predict_edge,         /* predict_edge  */
+  gimple_predicted_by_p,               /* predicted_by_p  */
+  gimple_can_duplicate_bb_p,   /* can_duplicate_block_p  */
+  gimple_duplicate_bb,         /* duplicate_block  */
+  gimple_split_edge,           /* split_edge  */
+  gimple_make_forwarder_block, /* make_forward_block  */
   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_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*/
+  gimple_block_ends_with_call_p,/* block_ends_with_call_p */
+  gimple_block_ends_with_condjump_p, /* block_ends_with_condjump_p */
+  gimple_flow_call_edges_add,     /* flow_call_edges_add */
+  gimple_execute_on_growing_pred,      /* execute_on_growing_pred */
+  gimple_execute_on_shrinking_pred, /* execute_on_shrinking_pred */
+  gimple_duplicate_loop_to_header_edge, /* duplicate loop for trees */
+  gimple_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
+  gimple_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 */
 };
@@ -7053,10 +6993,31 @@ split_critical_edges (void)
   FOR_ALL_BB (bb)
     {
       FOR_EACH_EDGE (e, ei, bb->succs)
-       if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL))
-         {
+        {
+         if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL))
            split_edge (e);
-         }
+         /* PRE inserts statements to edges and expects that
+            since split_critical_edges was done beforehand, committing edge
+            insertions will not split more edges.  In addition to critical
+            edges we must split edges that have multiple successors and
+            end by control flow statements, such as RESX.
+            Go ahead and split them too.  This matches the logic in
+            gimple_find_edge_insert_loc.  */
+         else if ((!single_pred_p (e->dest)
+                   || !gimple_seq_empty_p (phi_nodes (e->dest))
+                   || e->dest == EXIT_BLOCK_PTR)
+                  && e->src != ENTRY_BLOCK_PTR
+                  && !(e->flags & EDGE_ABNORMAL))
+           {
+             gimple_stmt_iterator gsi;
+
+             gsi = gsi_last_bb (e->src);
+             if (!gsi_end_p (gsi)
+                 && stmt_ends_bb_p (gsi_stmt (gsi))
+                 && gimple_code (gsi_stmt (gsi)) != GIMPLE_RETURN)
+               split_edge (e);
+           }
+       }
     }
   end_recording_case_labels ();
   return 0;
@@ -7077,81 +7038,58 @@ struct gimple_opt_pass pass_split_crit_edges =
   PROP_no_crit_edges,            /* properties_provided */
   0,                             /* properties_destroyed */
   0,                             /* todo_flags_start */
-  TODO_dump_func                 /* todo_flags_finish */
+  TODO_dump_func | TODO_verify_flow  /* todo_flags_finish */
  }
 };
 
-\f
-/* Return EXP if it is a valid GIMPLE rvalue, else gimplify it into
-   a temporary, make sure and register it to be renamed if necessary,
-   and finally return the temporary.  Put the statements to compute
-   EXP before the current statement in BSI.  */
-
-tree
-gimplify_val (block_stmt_iterator *bsi, tree type, tree exp)
-{
-  tree t, new_stmt, orig_stmt;
-
-  if (is_gimple_val (exp))
-    return exp;
-
-  t = make_rename_temp (type, NULL);
-  new_stmt = build_gimple_modify_stmt (t, exp);
-
-  orig_stmt = bsi_stmt (*bsi);
-  SET_EXPR_LOCUS (new_stmt, EXPR_LOCUS (orig_stmt));
-  TREE_BLOCK (new_stmt) = TREE_BLOCK (orig_stmt);
 
-  bsi_insert_before (bsi, new_stmt, BSI_SAME_STMT);
-  if (gimple_in_ssa_p (cfun))
-    mark_symbols_for_renaming (new_stmt);
-
-  return t;
-}
-
-/* Build a ternary operation and gimplify it.  Emit code before BSI.
+/* Build a ternary operation and gimplify it.  Emit code before GSI.
    Return the gimple_val holding the result.  */
 
 tree
-gimplify_build3 (block_stmt_iterator *bsi, enum tree_code code,
+gimplify_build3 (gimple_stmt_iterator *gsi, enum tree_code code,
                 tree type, tree a, tree b, tree c)
 {
   tree ret;
+  location_t loc = gimple_location (gsi_stmt (*gsi));
 
-  ret = fold_build3 (code, type, a, b, c);
+  ret = fold_build3_loc (loc, code, type, a, b, c);
   STRIP_NOPS (ret);
 
-  return gimplify_val (bsi, type, ret);
+  return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
+                                   GSI_SAME_STMT);
 }
 
-/* Build a binary operation and gimplify it.  Emit code before BSI.
+/* Build a binary operation and gimplify it.  Emit code before GSI.
    Return the gimple_val holding the result.  */
 
 tree
-gimplify_build2 (block_stmt_iterator *bsi, enum tree_code code,
+gimplify_build2 (gimple_stmt_iterator *gsi, enum tree_code code,
                 tree type, tree a, tree b)
 {
   tree ret;
 
-  ret = fold_build2 (code, type, a, b);
+  ret = fold_build2_loc (gimple_location (gsi_stmt (*gsi)), code, type, a, b);
   STRIP_NOPS (ret);
 
-  return gimplify_val (bsi, type, ret);
+  return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
+                                   GSI_SAME_STMT);
 }
 
-/* Build a unary operation and gimplify it.  Emit code before BSI.
+/* Build a unary operation and gimplify it.  Emit code before GSI.
    Return the gimple_val holding the result.  */
 
 tree
-gimplify_build1 (block_stmt_iterator *bsi, enum tree_code code, tree type,
+gimplify_build1 (gimple_stmt_iterator *gsi, enum tree_code code, tree type,
                 tree a)
 {
   tree ret;
 
-  ret = fold_build1 (code, type, a);
+  ret = fold_build1_loc (gimple_location (gsi_stmt (*gsi)), code, type, a);
   STRIP_NOPS (ret);
 
-  return gimplify_val (bsi, type, ret);
+  return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
+                                   GSI_SAME_STMT);
 }
 
 
@@ -7162,7 +7100,7 @@ static unsigned int
 execute_warn_function_return (void)
 {
   source_location location;
-  tree last;
+  gimple last;
   edge e;
   edge_iterator ei;
 
@@ -7174,13 +7112,13 @@ execute_warn_function_return (void)
       FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
        {
          last = last_stmt (e->src);
-         if (TREE_CODE (last) == RETURN_EXPR
-             && (location = EXPR_LOCATION (last)) != UNKNOWN_LOCATION)
+         if (gimple_code (last) == GIMPLE_RETURN
+             && (location = gimple_location (last)) != UNKNOWN_LOCATION)
            break;
        }
       if (location == UNKNOWN_LOCATION)
        location = cfun->function_end_locus;
-      warning (0, "%H%<noreturn%> function does return", &location);
+      warning_at (location, 0, "%<noreturn%> function does return");
     }
 
   /* If we see "return;" in some basic block, then we do reach the end
@@ -7192,12 +7130,12 @@ execute_warn_function_return (void)
     {
       FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
        {
-         tree last = last_stmt (e->src);
-         if (TREE_CODE (last) == RETURN_EXPR
-             && TREE_OPERAND (last, 0) == NULL
-             && !TREE_NO_WARNING (last))
+         gimple last = last_stmt (e->src);
+         if (gimple_code (last) == GIMPLE_RETURN
+             && gimple_return_retval (last) == NULL
+             && !gimple_no_warning_p (last))
            {
-             location = EXPR_LOCATION (last);
+             location = gimple_location (last);
              if (location == UNKNOWN_LOCATION)
                  location = cfun->function_end_locus;
              warning_at (location, OPT_Wreturn_type, "control reaches end of non-void function");
@@ -7238,13 +7176,13 @@ struct gimple_opt_pass pass_warn_function_return =
 {
  {
   GIMPLE_PASS,
-  NULL,                                        /* name */
+  "*warn_function_return",             /* name */
   NULL,                                        /* gate */
   execute_warn_function_return,                /* execute */
   NULL,                                        /* sub */
   NULL,                                        /* next */
   0,                                   /* static_pass_number */
-  0,                                   /* tv_id */
+  TV_NONE,                             /* tv_id */
   PROP_cfg,                            /* properties_required */
   0,                                   /* properties_provided */
   0,                                   /* properties_destroyed */
@@ -7262,9 +7200,9 @@ execute_warn_function_noreturn (void)
       && !TREE_THIS_VOLATILE (cfun->decl)
       && EDGE_COUNT (EXIT_BLOCK_PTR->preds) == 0
       && !lang_hooks.missing_noreturn_ok_p (cfun->decl))
-    warning (OPT_Wmissing_noreturn, "%Jfunction might be possible candidate "
-            "for attribute %<noreturn%>",
-            cfun->decl);
+    warning_at (DECL_SOURCE_LOCATION (cfun->decl), OPT_Wmissing_noreturn,
+               "function might be possible candidate "
+               "for attribute %<noreturn%>");
   return 0;
 }
 
@@ -7272,13 +7210,13 @@ struct gimple_opt_pass pass_warn_function_noreturn =
 {
  {
   GIMPLE_PASS,
-  NULL,                                        /* name */
+  "*warn_function_noreturn",           /* name */
   NULL,                                        /* gate */
   execute_warn_function_noreturn,      /* execute */
   NULL,                                        /* sub */
   NULL,                                        /* next */
   0,                                   /* static_pass_number */
-  0,                                   /* tv_id */
+  TV_NONE,                             /* tv_id */
   PROP_cfg,                            /* properties_required */
   0,                                   /* properties_provided */
   0,                                   /* properties_destroyed */
@@ -7286,3 +7224,100 @@ struct gimple_opt_pass pass_warn_function_noreturn =
   0                                    /* todo_flags_finish */
  }
 };
+
+
+/* Walk a gimplified function and warn for functions whose return value is
+   ignored and attribute((warn_unused_result)) is set.  This is done before
+   inlining, so we don't have to worry about that.  */
+
+static void
+do_warn_unused_result (gimple_seq seq)
+{
+  tree fdecl, ftype;
+  gimple_stmt_iterator i;
+
+  for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
+    {
+      gimple g = gsi_stmt (i);
+
+      switch (gimple_code (g))
+       {
+       case GIMPLE_BIND:
+         do_warn_unused_result (gimple_bind_body (g));
+         break;
+       case GIMPLE_TRY:
+         do_warn_unused_result (gimple_try_eval (g));
+         do_warn_unused_result (gimple_try_cleanup (g));
+         break;
+       case GIMPLE_CATCH:
+         do_warn_unused_result (gimple_catch_handler (g));
+         break;
+       case GIMPLE_EH_FILTER:
+         do_warn_unused_result (gimple_eh_filter_failure (g));
+         break;
+
+       case GIMPLE_CALL:
+         if (gimple_call_lhs (g))
+           break;
+
+         /* This is a naked call, as opposed to a GIMPLE_CALL with an
+            LHS.  All calls whose value is ignored should be
+            represented like this.  Look for the attribute.  */
+         fdecl = gimple_call_fndecl (g);
+         ftype = TREE_TYPE (TREE_TYPE (gimple_call_fn (g)));
+
+         if (lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (ftype)))
+           {
+             location_t loc = gimple_location (g);
+
+             if (fdecl)
+               warning_at (loc, OPT_Wunused_result,
+                           "ignoring return value of %qD, "
+                           "declared with attribute warn_unused_result",
+                           fdecl);
+             else
+               warning_at (loc, OPT_Wunused_result,
+                           "ignoring return value of function "
+                           "declared with attribute warn_unused_result");
+           }
+         break;
+
+       default:
+         /* Not a container, not a call, or a call whose value is used.  */
+         break;
+       }
+    }
+}
+
+static unsigned int
+run_warn_unused_result (void)
+{
+  do_warn_unused_result (gimple_body (current_function_decl));
+  return 0;
+}
+
+static bool
+gate_warn_unused_result (void)
+{
+  return flag_warn_unused_result;
+}
+
+struct gimple_opt_pass pass_warn_unused_result =
+{
+  {
+    GIMPLE_PASS,
+    "*warn_unused_result",             /* name */
+    gate_warn_unused_result,           /* gate */
+    run_warn_unused_result,            /* execute */
+    NULL,                              /* sub */
+    NULL,                              /* next */
+    0,                                 /* static_pass_number */
+    TV_NONE,                           /* tv_id */
+    PROP_gimple_any,                   /* properties_required */
+    0,                                 /* properties_provided */
+    0,                                 /* properties_destroyed */
+    0,                                 /* todo_flags_start */
+    0,                                 /* todo_flags_finish */
+  }
+};
+