re PR target/65697 (__atomic memory barriers not strong enough for __sync builtins)
[gcc.git] / gcc / tree-inline.c
index f07c1a85d743918af3aef2a11032e6069cb16a6d..6f138edc7f7f1caa44e0fb81412aae1d4f023176 100644 (file)
@@ -1,5 +1,5 @@
 /* Tree inlining.
-   Copyright (C) 2001-2014 Free Software Foundation, Inc.
+   Copyright (C) 2001-2015 Free Software Foundation, Inc.
    Contributed by Alexandre Oliva <aoliva@redhat.com>
 
 This file is part of GCC.
@@ -23,26 +23,31 @@ along with GCC; see the file COPYING3.  If not see
 #include "coretypes.h"
 #include "tm.h"
 #include "diagnostic-core.h"
+#include "alias.h"
+#include "symtab.h"
 #include "tree.h"
+#include "fold-const.h"
 #include "stor-layout.h"
 #include "calls.h"
 #include "tree-inline.h"
 #include "flags.h"
 #include "params.h"
-#include "input.h"
 #include "insn-config.h"
-#include "hashtab.h"
 #include "langhooks.h"
+#include "predict.h"
+#include "hard-reg-set.h"
+#include "function.h"
+#include "dominance.h"
+#include "cfg.h"
+#include "cfganal.h"
 #include "basic-block.h"
 #include "tree-iterator.h"
 #include "intl.h"
-#include "pointer-set.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimple-expr.h"
-#include "is-a.h"
 #include "gimple.h"
 #include "gimplify.h"
 #include "gimple-iterator.h"
@@ -55,19 +60,29 @@ along with GCC; see the file COPYING3.  If not see
 #include "stringpool.h"
 #include "tree-ssanames.h"
 #include "tree-into-ssa.h"
+#include "rtl.h"
+#include "expmed.h"
+#include "dojump.h"
+#include "explow.h"
+#include "emit-rtl.h"
+#include "varasm.h"
+#include "stmt.h"
 #include "expr.h"
 #include "tree-dfa.h"
 #include "tree-ssa.h"
-#include "function.h"
 #include "tree-pretty-print.h"
 #include "except.h"
 #include "debug.h"
+#include "cgraph.h"
+#include "alloc-pool.h"
+#include "symbol-summary.h"
 #include "ipa-prop.h"
 #include "value-prof.h"
 #include "tree-pass.h"
 #include "target.h"
 #include "cfgloop.h"
 #include "builtins.h"
+#include "tree-chkp.h"
 
 #include "rtl.h"       /* FIXME: For asm_str_count.  */
 
@@ -131,17 +146,19 @@ eni_weights eni_time_weights;
 
 /* Prototypes.  */
 
-static tree declare_return_variable (copy_body_data *, tree, tree, basic_block);
+static tree declare_return_variable (copy_body_data *, tree, tree, tree,
+                                    basic_block);
 static void remap_block (tree *, copy_body_data *);
 static void copy_bind_expr (tree *, int *, copy_body_data *);
 static void declare_inline_vars (tree, tree);
-static void remap_save_expr (tree *, void *, int *);
+static void remap_save_expr (tree *, hash_map<tree, tree> *, int *);
 static void prepend_lexical_block (tree current_block, tree new_block);
 static tree copy_decl_to_var (tree, copy_body_data *);
 static tree copy_result_decl_to_var (tree, copy_body_data *);
 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
-static gimple remap_gimple_stmt (gimple, copy_body_data *);
+static gimple_seq remap_gimple_stmt (gimple, copy_body_data *);
 static bool delete_unreachable_blocks_update_callgraph (copy_body_data *id);
+static void insert_init_stmt (copy_body_data *, basic_block, gimple);
 
 /* Insert a tree->tree mapping for ID.  Despite the name suggests
    that the trees should be variables, it is used for more than that.  */
@@ -149,12 +166,12 @@ static bool delete_unreachable_blocks_update_callgraph (copy_body_data *id);
 void
 insert_decl_map (copy_body_data *id, tree key, tree value)
 {
-  *pointer_map_insert (id->decl_map, key) = value;
+  id->decl_map->put (key, value);
 
   /* Always insert an identity map as well.  If we see this same new
      node again, we won't want to duplicate it a second time.  */
   if (key != value)
-    *pointer_map_insert (id->decl_map, value) = value;
+    id->decl_map->put (value, value);
 }
 
 /* Insert a tree->tree mapping for ID.  This is only used for
@@ -166,7 +183,7 @@ insert_debug_decl_map (copy_body_data *id, tree key, tree value)
   if (!gimple_in_ssa_p (id->src_cfun))
     return;
 
-  if (!MAY_HAVE_DEBUG_STMTS)
+  if (!opt_for_fn (id->dst_fn, flag_var_tracking_assignments))
     return;
 
   if (!target_for_debug_bind (key))
@@ -176,9 +193,9 @@ insert_debug_decl_map (copy_body_data *id, tree key, tree value)
   gcc_assert (TREE_CODE (value) == VAR_DECL);
 
   if (!id->debug_map)
-    id->debug_map = pointer_map_create ();
+    id->debug_map = new hash_map<tree, tree>;
 
-  *pointer_map_insert (id->debug_map, key) = value;
+  id->debug_map->put (key, value);
 }
 
 /* If nonzero, we're remapping the contents of inlined debug
@@ -197,7 +214,7 @@ remap_ssa_name (tree name, copy_body_data *id)
 
   gcc_assert (TREE_CODE (name) == SSA_NAME);
 
-  n = (tree *) pointer_map_contains (id->decl_map, name);
+  n = id->decl_map->get (name);
   if (n)
     return unshare_expr (*n);
 
@@ -213,7 +230,7 @@ remap_ssa_name (tree name, copy_body_data *id)
          gimple_stmt_iterator gsi;
          tree val = SSA_NAME_VAR (name);
 
-         n = (tree *) pointer_map_contains (id->decl_map, val);
+         n = id->decl_map->get (val);
          if (n != NULL)
            val = *n;
          if (TREE_CODE (val) != PARM_DECL)
@@ -245,7 +262,7 @@ remap_ssa_name (tree name, copy_body_data *id)
          && !DECL_NAME (var)))
     {
       struct ptr_info_def *pi;
-      new_tree = make_ssa_name (remap_type (TREE_TYPE (name), id), NULL);
+      new_tree = make_ssa_name (remap_type (TREE_TYPE (name), id));
       if (!var && SSA_NAME_IDENTIFIER (name))
        SET_SSA_NAME_VAR_OR_IDENTIFIER (new_tree, SSA_NAME_IDENTIFIER (name));
       insert_decl_map (id, name, new_tree);
@@ -279,7 +296,7 @@ remap_ssa_name (tree name, copy_body_data *id)
          || !id->transform_return_to_modify))
     {
       struct ptr_info_def *pi;
-      new_tree = make_ssa_name (new_tree, NULL);
+      new_tree = make_ssa_name (new_tree);
       insert_decl_map (id, name, new_tree);
       SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
        = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
@@ -342,7 +359,7 @@ remap_decl (tree decl, copy_body_data *id)
 
   /* See if we have remapped this declaration.  */
 
-  n = (tree *) pointer_map_contains (id->decl_map, decl);
+  n = id->decl_map->get (decl);
 
   if (!n && processing_debug_stmt)
     {
@@ -451,6 +468,8 @@ remap_type_1 (tree type, copy_body_data *id)
   TYPE_POINTER_TO (new_tree) = NULL;
   TYPE_REFERENCE_TO (new_tree) = NULL;
 
+  /* Copy all types that may contain references to local variables; be sure to
+     preserve sharing in between type and its main variant when possible.  */
   switch (TREE_CODE (new_tree))
     {
     case INTEGER_TYPE:
@@ -458,40 +477,74 @@ remap_type_1 (tree type, copy_body_data *id)
     case FIXED_POINT_TYPE:
     case ENUMERAL_TYPE:
     case BOOLEAN_TYPE:
-      t = TYPE_MIN_VALUE (new_tree);
-      if (t && TREE_CODE (t) != INTEGER_CST)
-        walk_tree (&TYPE_MIN_VALUE (new_tree), copy_tree_body_r, id, NULL);
+      if (TYPE_MAIN_VARIANT (new_tree) != new_tree)
+       {
+         gcc_checking_assert (TYPE_MIN_VALUE (type) == TYPE_MIN_VALUE (TYPE_MAIN_VARIANT (type)));
+         gcc_checking_assert (TYPE_MAX_VALUE (type) == TYPE_MAX_VALUE (TYPE_MAIN_VARIANT (type)));
+
+         TYPE_MIN_VALUE (new_tree) = TYPE_MIN_VALUE (TYPE_MAIN_VARIANT (new_tree));
+         TYPE_MAX_VALUE (new_tree) = TYPE_MAX_VALUE (TYPE_MAIN_VARIANT (new_tree));
+       }
+      else
+       {
+         t = TYPE_MIN_VALUE (new_tree);
+         if (t && TREE_CODE (t) != INTEGER_CST)
+           walk_tree (&TYPE_MIN_VALUE (new_tree), copy_tree_body_r, id, NULL);
 
-      t = TYPE_MAX_VALUE (new_tree);
-      if (t && TREE_CODE (t) != INTEGER_CST)
-        walk_tree (&TYPE_MAX_VALUE (new_tree), copy_tree_body_r, id, NULL);
+         t = TYPE_MAX_VALUE (new_tree);
+         if (t && TREE_CODE (t) != INTEGER_CST)
+           walk_tree (&TYPE_MAX_VALUE (new_tree), copy_tree_body_r, id, NULL);
+       }
       return new_tree;
 
     case FUNCTION_TYPE:
-      TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
-      walk_tree (&TYPE_ARG_TYPES (new_tree), copy_tree_body_r, id, NULL);
+      if (TYPE_MAIN_VARIANT (new_tree) != new_tree
+         && TREE_TYPE (type) == TREE_TYPE (TYPE_MAIN_VARIANT (type)))
+       TREE_TYPE (new_tree) = TREE_TYPE (TYPE_MAIN_VARIANT (new_tree));
+      else
+        TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
+      if (TYPE_MAIN_VARIANT (new_tree) != new_tree
+         && TYPE_ARG_TYPES (type) == TYPE_ARG_TYPES (TYPE_MAIN_VARIANT (type)))
+       TYPE_ARG_TYPES (new_tree) = TYPE_ARG_TYPES (TYPE_MAIN_VARIANT (new_tree));
+      else
+        walk_tree (&TYPE_ARG_TYPES (new_tree), copy_tree_body_r, id, NULL);
       return new_tree;
 
     case ARRAY_TYPE:
-      TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
-      TYPE_DOMAIN (new_tree) = remap_type (TYPE_DOMAIN (new_tree), id);
+      if (TYPE_MAIN_VARIANT (new_tree) != new_tree
+         && TREE_TYPE (type) == TREE_TYPE (TYPE_MAIN_VARIANT (type)))
+       TREE_TYPE (new_tree) = TREE_TYPE (TYPE_MAIN_VARIANT (new_tree));
+      else
+       TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
+
+      if (TYPE_MAIN_VARIANT (new_tree) != new_tree)
+       {
+         gcc_checking_assert (TYPE_DOMAIN (type) == TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)));
+         TYPE_DOMAIN (new_tree) = TYPE_DOMAIN (TYPE_MAIN_VARIANT (new_tree));
+       }
+      else
+       TYPE_DOMAIN (new_tree) = remap_type (TYPE_DOMAIN (new_tree), id);
       break;
 
     case RECORD_TYPE:
     case UNION_TYPE:
     case QUAL_UNION_TYPE:
-      {
-       tree f, nf = NULL;
+      if (TYPE_MAIN_VARIANT (type) != type
+         && TYPE_FIELDS (type) == TYPE_FIELDS (TYPE_MAIN_VARIANT (type)))
+       TYPE_FIELDS (new_tree) = TYPE_FIELDS (TYPE_MAIN_VARIANT (new_tree));
+      else
+       {
+         tree f, nf = NULL;
 
-       for (f = TYPE_FIELDS (new_tree); f ; f = DECL_CHAIN (f))
-         {
-           t = remap_decl (f, id);
-           DECL_CONTEXT (t) = new_tree;
-           DECL_CHAIN (t) = nf;
-           nf = t;
-         }
-       TYPE_FIELDS (new_tree) = nreverse (nf);
-      }
+         for (f = TYPE_FIELDS (new_tree); f ; f = DECL_CHAIN (f))
+           {
+             t = remap_decl (f, id);
+             DECL_CONTEXT (t) = new_tree;
+             DECL_CHAIN (t) = nf;
+             nf = t;
+           }
+         TYPE_FIELDS (new_tree) = nreverse (nf);
+       }
       break;
 
     case OFFSET_TYPE:
@@ -500,8 +553,20 @@ remap_type_1 (tree type, copy_body_data *id)
       gcc_unreachable ();
     }
 
-  walk_tree (&TYPE_SIZE (new_tree), copy_tree_body_r, id, NULL);
-  walk_tree (&TYPE_SIZE_UNIT (new_tree), copy_tree_body_r, id, NULL);
+  /* All variants of type share the same size, so use the already remaped data.  */
+  if (TYPE_MAIN_VARIANT (new_tree) != new_tree)
+    {
+      gcc_checking_assert (TYPE_SIZE (type) == TYPE_SIZE (TYPE_MAIN_VARIANT (type)));
+      gcc_checking_assert (TYPE_SIZE_UNIT (type) == TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (type)));
+
+      TYPE_SIZE (new_tree) = TYPE_SIZE (TYPE_MAIN_VARIANT (new_tree));
+      TYPE_SIZE_UNIT (new_tree) = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (new_tree));
+    }
+  else
+    {
+      walk_tree (&TYPE_SIZE (new_tree), copy_tree_body_r, id, NULL);
+      walk_tree (&TYPE_SIZE_UNIT (new_tree), copy_tree_body_r, id, NULL);
+    }
 
   return new_tree;
 }
@@ -516,7 +581,7 @@ remap_type (tree type, copy_body_data *id)
     return type;
 
   /* See if we have remapped this type.  */
-  node = (tree *) pointer_map_contains (id->decl_map, type);
+  node = id->decl_map->get (type);
   if (node)
     return *node;
 
@@ -733,8 +798,8 @@ remap_gimple_seq (gimple_seq body, copy_body_data *id)
 
   for (si = gsi_start (body); !gsi_end_p (si); gsi_next (&si))
     {
-      gimple new_stmt = remap_gimple_stmt (gsi_stmt (si), id);
-      gimple_seq_add_stmt (&new_body, new_stmt);
+      gimple_seq new_stmts = remap_gimple_stmt (gsi_stmt (si), id);
+      gimple_seq_add_seq (&new_body, new_stmts);
     }
 
   return new_body;
@@ -745,7 +810,7 @@ remap_gimple_seq (gimple_seq body, copy_body_data *id)
    block using the mapping information in ID.  */
 
 static gimple
-copy_gimple_bind (gimple stmt, copy_body_data *id)
+copy_gimple_bind (gbind *stmt, copy_body_data *id)
 {
   gimple new_bind;
   tree new_block, new_vars;
@@ -786,6 +851,23 @@ is_parm (tree decl)
   return (TREE_CODE (decl) == PARM_DECL);
 }
 
+/* Remap the dependence CLIQUE from the source to the destination function
+   as specified in ID.  */
+
+static unsigned short
+remap_dependence_clique (copy_body_data *id, unsigned short clique)
+{
+  if (clique == 0)
+    return 0;
+  if (!id->dependence_map)
+    id->dependence_map = new hash_map<dependence_hash, unsigned short>;
+  bool existed;
+  unsigned short &newc = id->dependence_map->get_or_insert (clique, &existed);
+  if (!existed)
+    newc = ++cfun->last_clique;
+  return newc;
+}
+
 /* Remap the GIMPLE operand pointed to by *TP.  DATA is really a
    'struct walk_stmt_info *'.  DATA->INFO is a 'copy_body_data *'.
    WALK_SUBTREES is used to indicate walk_gimple_op whether to keep
@@ -841,7 +923,7 @@ remap_gimple_op_r (tree *tp, int *walk_subtrees, void *data)
     {
       /* If the enclosing record type is variably_modified_type_p, the field
         has already been remapped.  Otherwise, it need not be.  */
-      tree *n = (tree *) pointer_map_contains (id->decl_map, *tp);
+      tree *n = id->decl_map->get (*tp);
       if (n)
        *tp = *n;
       *walk_subtrees = 0;
@@ -884,6 +966,12 @@ remap_gimple_op_r (tree *tp, int *walk_subtrees, void *data)
          TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
          TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
          TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
+         if (MR_DEPENDENCE_CLIQUE (old) != 0)
+           {
+             MR_DEPENDENCE_CLIQUE (*tp)
+               = remap_dependence_clique (id, MR_DEPENDENCE_CLIQUE (old));
+             MR_DEPENDENCE_BASE (*tp) = MR_DEPENDENCE_BASE (old);
+           }
          /* We cannot propagate the TREE_THIS_NOTRAP flag if we have
             remapped a parameter as the property might be valid only
             for the parameter itself.  */
@@ -935,8 +1023,7 @@ remap_gimple_op_r (tree *tp, int *walk_subtrees, void *data)
       if (old_block)
        {
          tree *n;
-         n = (tree *) pointer_map_contains (id->decl_map,
-                                            TREE_BLOCK (*tp));
+         n = id->decl_map->get (TREE_BLOCK (*tp));
          if (n)
            new_block = *n;
        }
@@ -1062,7 +1149,7 @@ copy_tree_body_r (tree *tp, int *walk_subtrees, void *data)
          tree decl = TREE_OPERAND (*tp, 0), value;
          tree *n;
 
-         n = (tree *) pointer_map_contains (id->decl_map, decl);
+         n = id->decl_map->get (decl);
          if (n)
            {
              value = *n;
@@ -1079,7 +1166,7 @@ copy_tree_body_r (tree *tp, int *walk_subtrees, void *data)
          /* Get rid of *& from inline substitutions that can happen when a
             pointer argument is an ADDR_EXPR.  */
          tree decl = TREE_OPERAND (*tp, 0);
-         tree *n = (tree *) pointer_map_contains (id->decl_map, decl);
+         tree *n = id->decl_map->get (decl);
          if (n)
            {
              /* If we happen to get an ADDR_EXPR in n->value, strip
@@ -1136,6 +1223,12 @@ copy_tree_body_r (tree *tp, int *walk_subtrees, void *data)
          TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
          TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
          TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
+         if (MR_DEPENDENCE_CLIQUE (old) != 0)
+           {
+             MR_DEPENDENCE_CLIQUE (*tp)
+               = remap_dependence_clique (id, MR_DEPENDENCE_CLIQUE (old));
+             MR_DEPENDENCE_BASE (*tp) = MR_DEPENDENCE_BASE (old);
+           }
          /* We cannot propagate the TREE_THIS_NOTRAP flag if we have
             remapped a parameter as the property might be valid only
             for the parameter itself.  */
@@ -1160,8 +1253,7 @@ copy_tree_body_r (tree *tp, int *walk_subtrees, void *data)
          if (TREE_BLOCK (*tp))
            {
              tree *n;
-             n = (tree *) pointer_map_contains (id->decl_map,
-                                                TREE_BLOCK (*tp));
+             n = id->decl_map->get (TREE_BLOCK (*tp));
              if (n)
                new_block = *n;
            }
@@ -1215,11 +1307,9 @@ static int
 remap_eh_region_nr (int old_nr, copy_body_data *id)
 {
   eh_region old_r, new_r;
-  void **slot;
 
   old_r = get_eh_region_from_number_fn (id->src_cfun, old_nr);
-  slot = pointer_map_contains (id->eh_map, old_r);
-  new_r = (eh_region) *slot;
+  new_r = static_cast<eh_region> (*id->eh_map->get (old_r));
 
   return new_r->index;
 }
@@ -1240,12 +1330,17 @@ remap_eh_region_tree_nr (tree old_t_nr, copy_body_data *id)
 /* Helper for copy_bb.  Remap statement STMT using the inlining
    information in ID.  Return the new statement copy.  */
 
-static gimple
+static gimple_seq
 remap_gimple_stmt (gimple stmt, copy_body_data *id)
 {
   gimple copy = NULL;
   struct walk_stmt_info wi;
   bool skip_first = false;
+  gimple_seq stmts = NULL;
+
+  if (is_gimple_debug (stmt)
+      && !opt_for_fn (id->dst_fn, flag_var_tracking_assignments))
+    return stmts;
 
   /* Begin by recognizing trees that we'll completely rewrite for the
      inlining context.  Our output for these trees is completely
@@ -1259,7 +1354,18 @@ remap_gimple_stmt (gimple stmt, copy_body_data *id)
      statement.  */
   if (gimple_code (stmt) == GIMPLE_RETURN && id->transform_return_to_modify)
     {
-      tree retval = gimple_return_retval (stmt);
+      tree retval = gimple_return_retval (as_a <greturn *> (stmt));
+      tree retbnd = gimple_return_retbnd (stmt);
+      tree bndslot = id->retbnd;
+
+      if (retbnd && bndslot)
+       {
+         gimple bndcopy = gimple_build_assign (bndslot, retbnd);
+         memset (&wi, 0, sizeof (wi));
+         wi.info = id;
+         walk_gimple_op (bndcopy, remap_gimple_op_r, &wi);
+         gimple_seq_add_stmt (&stmts, bndcopy);
+       }
 
       /* If we're returning something, just turn that into an
         assignment into the equivalent of the original RESULT_DECL.
@@ -1277,9 +1383,18 @@ remap_gimple_stmt (gimple stmt, copy_body_data *id)
                                      retval);
          /* id->retvar is already substituted.  Skip it on later remapping.  */
          skip_first = true;
+
+         /* We need to copy bounds if return structure with pointers into
+            instrumented function.  */
+         if (chkp_function_instrumented_p (id->dst_fn)
+             && !bndslot
+             && !BOUNDED_P (id->retvar)
+             && chkp_type_has_pointer (TREE_TYPE (id->retvar)))
+           id->assign_stmts.safe_push (copy);
+
        }
       else
-       return gimple_build_nop ();
+       return stmts;
     }
   else if (gimple_has_substatements (stmt))
     {
@@ -1291,12 +1406,15 @@ remap_gimple_stmt (gimple stmt, copy_body_data *id)
       switch (gimple_code (stmt))
        {
        case GIMPLE_BIND:
-         copy = copy_gimple_bind (stmt, id);
+         copy = copy_gimple_bind (as_a <gbind *> (stmt), id);
          break;
 
        case GIMPLE_CATCH:
-         s1 = remap_gimple_seq (gimple_catch_handler (stmt), id);
-         copy = gimple_build_catch (gimple_catch_types (stmt), s1);
+         {
+           gcatch *catch_stmt = as_a <gcatch *> (stmt);
+           s1 = remap_gimple_seq (gimple_catch_handler (catch_stmt), id);
+           copy = gimple_build_catch (gimple_catch_types (catch_stmt), s1);
+         }
          break;
 
        case GIMPLE_EH_FILTER:
@@ -1316,12 +1434,15 @@ remap_gimple_stmt (gimple stmt, copy_body_data *id)
          break;
 
        case GIMPLE_OMP_PARALLEL:
-         s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
-         copy = gimple_build_omp_parallel
-                  (s1,
-                   gimple_omp_parallel_clauses (stmt),
-                   gimple_omp_parallel_child_fn (stmt),
-                   gimple_omp_parallel_data_arg (stmt));
+         {
+           gomp_parallel *omp_par_stmt = as_a <gomp_parallel *> (stmt);
+           s1 = remap_gimple_seq (gimple_omp_body (omp_par_stmt), id);
+           copy = gimple_build_omp_parallel
+                    (s1,
+                     gimple_omp_parallel_clauses (omp_par_stmt),
+                     gimple_omp_parallel_child_fn (omp_par_stmt),
+                     gimple_omp_parallel_data_arg (omp_par_stmt));
+         }
          break;
 
        case GIMPLE_OMP_TASK:
@@ -1407,14 +1528,25 @@ remap_gimple_stmt (gimple stmt, copy_body_data *id)
 
        case GIMPLE_OMP_CRITICAL:
          s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
-         copy
-           = gimple_build_omp_critical (s1, gimple_omp_critical_name (stmt));
+         copy = gimple_build_omp_critical (s1,
+                                           gimple_omp_critical_name (
+                                             as_a <gomp_critical *> (stmt)));
          break;
 
        case GIMPLE_TRANSACTION:
-         s1 = remap_gimple_seq (gimple_transaction_body (stmt), id);
-         copy = gimple_build_transaction (s1, gimple_transaction_label (stmt));
-         gimple_transaction_set_subcode (copy, gimple_transaction_subcode (stmt));
+         {
+           gtransaction *old_trans_stmt = as_a <gtransaction *> (stmt);
+           gtransaction *new_trans_stmt;
+           s1 = remap_gimple_seq (gimple_transaction_body (old_trans_stmt),
+                                  id);
+           copy = new_trans_stmt
+             = gimple_build_transaction (
+                 s1,
+                 gimple_transaction_label (old_trans_stmt));
+           gimple_transaction_set_subcode (
+              new_trans_stmt,
+             gimple_transaction_subcode (old_trans_stmt));
+         }
          break;
 
        default:
@@ -1437,13 +1569,13 @@ remap_gimple_stmt (gimple stmt, copy_body_data *id)
          tree decl = gimple_assign_lhs (stmt), value;
          tree *n;
 
-         n = (tree *) pointer_map_contains (id->decl_map, decl);
+         n = id->decl_map->get (decl);
          if (n)
            {
              value = *n;
              STRIP_TYPE_NOPS (value);
              if (TREE_CONSTANT (value) || TREE_READONLY (value))
-               return gimple_build_nop ();
+               return NULL;
            }
        }
 
@@ -1460,34 +1592,42 @@ remap_gimple_stmt (gimple stmt, copy_body_data *id)
              if (gimple_bb (def_stmt)
                  && !bitmap_bit_p (id->blocks_to_copy,
                                    gimple_bb (def_stmt)->index))
-               return gimple_build_nop ();
+               return NULL;
            }
        }
 
       if (gimple_debug_bind_p (stmt))
        {
-         copy = gimple_build_debug_bind (gimple_debug_bind_get_var (stmt),
-                                         gimple_debug_bind_get_value (stmt),
-                                         stmt);
+         gdebug *copy
+           = gimple_build_debug_bind (gimple_debug_bind_get_var (stmt),
+                                      gimple_debug_bind_get_value (stmt),
+                                      stmt);
          id->debug_stmts.safe_push (copy);
-         return copy;
+         gimple_seq_add_stmt (&stmts, copy);
+         return stmts;
        }
       if (gimple_debug_source_bind_p (stmt))
        {
-         copy = gimple_build_debug_source_bind
-                  (gimple_debug_source_bind_get_var (stmt),
-                   gimple_debug_source_bind_get_value (stmt), stmt);
+         gdebug *copy = gimple_build_debug_source_bind
+                          (gimple_debug_source_bind_get_var (stmt),
+                           gimple_debug_source_bind_get_value (stmt),
+                           stmt);
          id->debug_stmts.safe_push (copy);
-         return copy;
+         gimple_seq_add_stmt (&stmts, copy);
+         return stmts;
        }
 
       /* Create a new deep copy of the statement.  */
       copy = gimple_copy (stmt);
 
       /* Clear flags that need revisiting.  */
-      if (is_gimple_call (copy)
-         && gimple_call_tail_p (copy))
-       gimple_call_set_tail (copy, false);
+      if (gcall *call_stmt = dyn_cast <gcall *> (copy))
+        {
+         if (gimple_call_tail_p (call_stmt))
+           gimple_call_set_tail (call_stmt, false);
+         if (gimple_call_from_thunk_p (call_stmt))
+           gimple_call_set_from_thunk (call_stmt, false);
+       }
 
       /* Remap the region numbers for __builtin_eh_{pointer,filter},
         RESX and EH_DISPATCH.  */
@@ -1521,23 +1661,25 @@ remap_gimple_stmt (gimple stmt, copy_body_data *id)
                 keep it valid over inlining by setting DECL_PT_UID.  */
              if (!id->src_cfun->gimple_df
                  || !id->src_cfun->gimple_df->ipa_pta)
-               gimple_call_reset_alias_info (copy);
+               gimple_call_reset_alias_info (as_a <gcall *> (copy));
            }
            break;
 
          case GIMPLE_RESX:
            {
-             int r = gimple_resx_region (copy);
+             gresx *resx_stmt = as_a <gresx *> (copy);
+             int r = gimple_resx_region (resx_stmt);
              r = remap_eh_region_nr (r, id);
-             gimple_resx_set_region (copy, r);
+             gimple_resx_set_region (resx_stmt, r);
            }
            break;
 
          case GIMPLE_EH_DISPATCH:
            {
-             int r = gimple_eh_dispatch_region (copy);
+             geh_dispatch *eh_dispatch = as_a <geh_dispatch *> (copy);
+             int r = gimple_eh_dispatch_region (eh_dispatch);
              r = remap_eh_region_nr (r, id);
-             gimple_eh_dispatch_set_region (copy, r);
+             gimple_eh_dispatch_set_region (eh_dispatch, r);
            }
            break;
 
@@ -1551,13 +1693,16 @@ remap_gimple_stmt (gimple stmt, copy_body_data *id)
   if (gimple_block (copy))
     {
       tree *n;
-      n = (tree *) pointer_map_contains (id->decl_map, gimple_block (copy));
+      n = id->decl_map->get (gimple_block (copy));
       gcc_assert (n);
       gimple_set_block (copy, *n);
     }
 
   if (gimple_debug_bind_p (copy) || gimple_debug_source_bind_p (copy))
-    return copy;
+    {
+      gimple_seq_add_stmt (&stmts, copy);
+      return stmts;
+    }
 
   /* Remap all the operands in COPY.  */
   memset (&wi, 0, sizeof (wi));
@@ -1575,7 +1720,8 @@ remap_gimple_stmt (gimple stmt, copy_body_data *id)
       gimple_set_vuse (copy, NULL_TREE);
     }
 
-  return copy;
+  gimple_seq_add_stmt (&stmts, copy);
+  return stmts;
 }
 
 
@@ -1599,8 +1745,7 @@ copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
 
   /* create_basic_block() will append every new block to
      basic_block_info automatically.  */
-  copy_basic_block = create_basic_block (NULL, (void *) 0,
-                                         (basic_block) prev->aux);
+  copy_basic_block = create_basic_block (NULL, (basic_block) prev->aux);
   copy_basic_block->count = apply_scale (bb->count, count_scale);
 
   /* We are going to rebuild frequencies from scratch.  These values
@@ -1616,36 +1761,59 @@ copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
 
   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
     {
+      gimple_seq stmts;
       gimple stmt = gsi_stmt (gsi);
       gimple orig_stmt = stmt;
+      gimple_stmt_iterator stmts_gsi;
+      bool stmt_added = false;
 
       id->regimplify = false;
-      stmt = remap_gimple_stmt (stmt, id);
-      if (gimple_nop_p (stmt))
+      stmts = remap_gimple_stmt (stmt, id);
+
+      if (gimple_seq_empty_p (stmts))
        continue;
 
-      gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun, orig_stmt);
       seq_gsi = copy_gsi;
 
-      /* With return slot optimization we can end up with
-        non-gimple (foo *)&this->m, fix that here.  */
-      if (is_gimple_assign (stmt)
-         && gimple_assign_rhs_code (stmt) == NOP_EXPR
-         && !is_gimple_val (gimple_assign_rhs1 (stmt)))
+      for (stmts_gsi = gsi_start (stmts);
+          !gsi_end_p (stmts_gsi); )
        {
-         tree new_rhs;
-         new_rhs = force_gimple_operand_gsi (&seq_gsi,
-                                             gimple_assign_rhs1 (stmt),
-                                             true, NULL, false,
-                                             GSI_CONTINUE_LINKING);
-         gimple_assign_set_rhs1 (stmt, new_rhs);
-         id->regimplify = false;
-       }
+         stmt = gsi_stmt (stmts_gsi);
+
+         /* Advance iterator now before stmt is moved to seq_gsi.  */
+         gsi_next (&stmts_gsi);
+
+         if (gimple_nop_p (stmt))
+             continue;
+
+         gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun,
+                                           orig_stmt);
+
+         /* With return slot optimization we can end up with
+            non-gimple (foo *)&this->m, fix that here.  */
+         if (is_gimple_assign (stmt)
+             && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
+             && !is_gimple_val (gimple_assign_rhs1 (stmt)))
+           {
+             tree new_rhs;
+             new_rhs = force_gimple_operand_gsi (&seq_gsi,
+                                                 gimple_assign_rhs1 (stmt),
+                                                 true, NULL, false,
+                                                 GSI_CONTINUE_LINKING);
+             gimple_assign_set_rhs1 (stmt, new_rhs);
+             id->regimplify = false;
+           }
+
+         gsi_insert_after (&seq_gsi, stmt, GSI_NEW_STMT);
 
-      gsi_insert_after (&seq_gsi, stmt, GSI_NEW_STMT);
+         if (id->regimplify)
+           gimple_regimplify_operands (stmt, &seq_gsi);
 
-      if (id->regimplify)
-       gimple_regimplify_operands (stmt, &seq_gsi);
+         stmt_added = true;
+       }
+
+      if (!stmt_added)
+       continue;
 
       /* If copy_basic_block has been empty at the start of this iteration,
         call gsi_start_bb again to get at the newly added statements.  */
@@ -1660,75 +1828,124 @@ copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
       do
        {
          tree fn;
+         gcall *call_stmt;
 
          stmt = gsi_stmt (copy_gsi);
-         if (is_gimple_call (stmt)
-             && gimple_call_va_arg_pack_p (stmt)
-             && id->gimple_call)
+         call_stmt = dyn_cast <gcall *> (stmt);
+         if (call_stmt
+             && gimple_call_va_arg_pack_p (call_stmt)
+             && id->call_stmt)
            {
              /* __builtin_va_arg_pack () should be replaced by
                 all arguments corresponding to ... in the caller.  */
              tree p;
-             gimple new_call;
+             gcall *new_call;
              vec<tree> argarray;
-             size_t nargs = gimple_call_num_args (id->gimple_call);
-             size_t n;
+             size_t nargs = gimple_call_num_args (id->call_stmt);
+             size_t n, i, nargs_to_copy;
+             bool remove_bounds = false;
 
              for (p = DECL_ARGUMENTS (id->src_fn); p; p = DECL_CHAIN (p))
                nargs--;
 
+             /* Bounds should be removed from arg pack in case
+                we handle not instrumented call in instrumented
+                function.  */
+             nargs_to_copy = nargs;
+             if (gimple_call_with_bounds_p (id->call_stmt)
+                 && !gimple_call_with_bounds_p (stmt))
+               {
+                 for (i = gimple_call_num_args (id->call_stmt) - nargs;
+                      i < gimple_call_num_args (id->call_stmt);
+                      i++)
+                   if (POINTER_BOUNDS_P (gimple_call_arg (id->call_stmt, i)))
+                     nargs_to_copy--;
+                 remove_bounds = true;
+               }
+
              /* Create the new array of arguments.  */
-             n = nargs + gimple_call_num_args (stmt);
+             n = nargs_to_copy + gimple_call_num_args (call_stmt);
              argarray.create (n);
              argarray.safe_grow_cleared (n);
 
              /* Copy all the arguments before '...'  */
              memcpy (argarray.address (),
-                     gimple_call_arg_ptr (stmt, 0),
-                     gimple_call_num_args (stmt) * sizeof (tree));
+                     gimple_call_arg_ptr (call_stmt, 0),
+                     gimple_call_num_args (call_stmt) * sizeof (tree));
 
-             /* Append the arguments passed in '...'  */
-             memcpy (argarray.address () + gimple_call_num_args (stmt),
-                     gimple_call_arg_ptr (id->gimple_call, 0)
-                       + (gimple_call_num_args (id->gimple_call) - nargs),
-                     nargs * sizeof (tree));
+             if (remove_bounds)
+               {
+                 /* Append the rest of arguments removing bounds.  */
+                 unsigned cur = gimple_call_num_args (call_stmt);
+                 i = gimple_call_num_args (id->call_stmt) - nargs;
+                 for (i = gimple_call_num_args (id->call_stmt) - nargs;
+                      i < gimple_call_num_args (id->call_stmt);
+                      i++)
+                   if (!POINTER_BOUNDS_P (gimple_call_arg (id->call_stmt, i)))
+                     argarray[cur++] = gimple_call_arg (id->call_stmt, i);
+                 gcc_assert (cur == n);
+               }
+             else
+               {
+                 /* Append the arguments passed in '...'  */
+                 memcpy (argarray.address () + gimple_call_num_args (call_stmt),
+                         gimple_call_arg_ptr (id->call_stmt, 0)
+                         + (gimple_call_num_args (id->call_stmt) - nargs),
+                         nargs * sizeof (tree));
+               }
 
-             new_call = gimple_build_call_vec (gimple_call_fn (stmt),
+             new_call = gimple_build_call_vec (gimple_call_fn (call_stmt),
                                                argarray);
 
              argarray.release ();
 
              /* Copy all GIMPLE_CALL flags, location and block, except
                 GF_CALL_VA_ARG_PACK.  */
-             gimple_call_copy_flags (new_call, stmt);
+             gimple_call_copy_flags (new_call, call_stmt);
              gimple_call_set_va_arg_pack (new_call, false);
              gimple_set_location (new_call, gimple_location (stmt));
              gimple_set_block (new_call, gimple_block (stmt));
-             gimple_call_set_lhs (new_call, gimple_call_lhs (stmt));
+             gimple_call_set_lhs (new_call, gimple_call_lhs (call_stmt));
 
              gsi_replace (&copy_gsi, new_call, false);
              stmt = new_call;
            }
-         else if (is_gimple_call (stmt)
-                  && id->gimple_call
+         else if (call_stmt
+                  && id->call_stmt
                   && (decl = gimple_call_fndecl (stmt))
                   && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
                   && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_ARG_PACK_LEN)
            {
              /* __builtin_va_arg_pack_len () should be replaced by
                 the number of anonymous arguments.  */
-             size_t nargs = gimple_call_num_args (id->gimple_call);
+             size_t nargs = gimple_call_num_args (id->call_stmt), i;
              tree count, p;
              gimple new_stmt;
 
              for (p = DECL_ARGUMENTS (id->src_fn); p; p = DECL_CHAIN (p))
                nargs--;
 
+             /* For instrumented calls we should ignore bounds.  */
+             for (i = gimple_call_num_args (id->call_stmt) - nargs;
+                  i < gimple_call_num_args (id->call_stmt);
+                  i++)
+               if (POINTER_BOUNDS_P (gimple_call_arg (id->call_stmt, i)))
+                 nargs--;
+
              count = build_int_cst (integer_type_node, nargs);
              new_stmt = gimple_build_assign (gimple_call_lhs (stmt), count);
              gsi_replace (&copy_gsi, new_stmt, false);
              stmt = new_stmt;
            }
+         else if (call_stmt
+                  && id->call_stmt
+                  && gimple_call_internal_p (stmt)
+                  && gimple_call_internal_fn (stmt) == IFN_TSAN_FUNC_EXIT)
+           {
+             /* Drop TSAN_FUNC_EXIT () internal calls during inlining.  */
+             gsi_remove (&copy_gsi, false);
+             continue;
+           }
 
          /* Statements produced by inlining can be unfolded, especially
             when we constant propagated some operands.  We can't fold
@@ -1742,27 +1959,27 @@ copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
             expensive, copy_body can be told to watch for nontrivial
             changes.  */
          if (id->statements_to_fold)
-           pointer_set_insert (id->statements_to_fold, stmt);
+           id->statements_to_fold->add (stmt);
 
          /* We're duplicating a CALL_EXPR.  Find any corresponding
             callgraph edges and update or duplicate them.  */
-         if (is_gimple_call (stmt))
+         if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
            {
              struct cgraph_edge *edge;
 
              switch (id->transform_call_graph_edges)
                {
                case CB_CGE_DUPLICATE:
-                 edge = cgraph_edge (id->src_node, orig_stmt);
+                 edge = id->src_node->get_edge (orig_stmt);
                  if (edge)
                    {
                      int edge_freq = edge->frequency;
                      int new_freq;
                      struct cgraph_edge *old_edge = edge;
-                     edge = cgraph_clone_edge (edge, id->dst_node, stmt,
-                                               gimple_uid (stmt),
-                                               REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
-                                               true);
+                     edge = edge->clone (id->dst_node, call_stmt,
+                                         gimple_uid (stmt),
+                                         REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
+                                         true);
                      /* We could also just rescale the frequency, but
                         doing so would introduce roundoff errors and make
                         verifier unhappy.  */
@@ -1777,11 +1994,11 @@ copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
                          struct ipa_ref *ref;
 
                          gcc_assert (!edge->indirect_unknown_callee);
-                         cgraph_speculative_call_info (old_edge, direct, indirect, ref);
-                         indirect = cgraph_clone_edge (indirect, id->dst_node, stmt,
-                                                       gimple_uid (stmt),
-                                                       REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
-                                                       true);
+                         old_edge->speculative_call_info (direct, indirect, ref);
+                         indirect = indirect->clone (id->dst_node, call_stmt,
+                                                     gimple_uid (stmt),
+                                                     REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
+                                                     true);
                          if (old_edge->frequency + indirect->frequency)
                            {
                              edge->frequency = MIN (RDIV ((gcov_type)new_freq * old_edge->frequency,
@@ -1816,15 +2033,15 @@ copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
                  break;
 
                case CB_CGE_MOVE_CLONES:
-                 cgraph_set_call_stmt_including_clones (id->dst_node,
-                                                        orig_stmt, stmt);
-                 edge = cgraph_edge (id->dst_node, stmt);
+                 id->dst_node->set_call_stmt_including_clones (orig_stmt,
+                                                               call_stmt);
+                 edge = id->dst_node->get_edge (stmt);
                  break;
 
                case CB_CGE_MOVE:
-                 edge = cgraph_edge (id->dst_node, orig_stmt);
+                 edge = id->dst_node->get_edge (orig_stmt);
                  if (edge)
-                   cgraph_set_call_stmt (edge, stmt);
+                   edge->set_call_stmt (call_stmt);
                  break;
 
                default:
@@ -1839,7 +2056,7 @@ copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
                  && id->dst_node->definition
                  && (fn = gimple_call_fndecl (stmt)) != NULL)
                {
-                 struct cgraph_node *dest = cgraph_get_node (fn);
+                 struct cgraph_node *dest = cgraph_node::get (fn);
 
                  /* We have missing edge in the callgraph.  This can happen
                     when previous inlining turned an indirect call into a
@@ -1852,13 +2069,13 @@ copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
                              || !id->src_node->definition
                              || !id->dst_node->definition);
                  if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES)
-                   cgraph_create_edge_including_clones
-                     (id->dst_node, dest, orig_stmt, stmt, bb->count,
+                   id->dst_node->create_edge_including_clones
+                     (dest, orig_stmt, call_stmt, bb->count,
                       compute_call_stmt_bb_frequency (id->dst_node->decl,
                                                       copy_basic_block),
                       CIF_ORIGINALLY_INDIRECT_CALL);
                  else
-                   cgraph_create_edge (id->dst_node, dest, stmt,
+                   id->dst_node->create_edge (dest, call_stmt,
                                        bb->count,
                                        compute_call_stmt_bb_frequency
                                          (id->dst_node->decl,
@@ -1871,7 +2088,7 @@ copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
                    }
                }
 
-             notice_special_calls (stmt);
+             notice_special_calls (as_a <gcall *> (stmt));
            }
 
          maybe_duplicate_eh_stmt_fn (cfun, stmt, id->src_cfun, orig_stmt,
@@ -1926,8 +2143,8 @@ update_ssa_across_abnormal_edges (basic_block bb, basic_block ret_bb,
     if (!e->dest->aux
        || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
       {
-       gimple phi;
-       gimple_stmt_iterator si;
+       gphi *phi;
+       gphi_iterator si;
 
        if (!nonlocal_goto)
          gcc_assert (e->flags & EDGE_EH);
@@ -1939,7 +2156,7 @@ update_ssa_across_abnormal_edges (basic_block bb, basic_block ret_bb,
          {
            edge re;
 
-           phi = gsi_stmt (si);
+           phi = si.phi ();
 
            /* For abnormal goto/call edges the receiver can be the
               ENTRY_BLOCK.  Do not assert this cannot happen.  */
@@ -2047,7 +2264,7 @@ copy_edges_for_bb (basic_block bb, gcov_type count_scale, basic_block ret_bb,
        }
 
       if (gimple_code (copy_stmt) == GIMPLE_EH_DISPATCH)
-       make_eh_dispatch_edges (copy_stmt);
+       make_eh_dispatch_edges (as_a <geh_dispatch *> (copy_stmt));
       else if (can_throw)
        make_eh_edges (copy_stmt);
 
@@ -2093,17 +2310,17 @@ copy_phis_for_bb (basic_block bb, copy_body_data *id)
 {
   basic_block const new_bb = (basic_block) bb->aux;
   edge_iterator ei;
-  gimple phi;
-  gimple_stmt_iterator si;
+  gphi *phi;
+  gphi_iterator si;
   edge new_edge;
   bool inserted = false;
 
   for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
     {
       tree res, new_res;
-      gimple new_phi;
+      gphi *new_phi;
 
-      phi = gsi_stmt (si);
+      phi = si.phi ();
       res = PHI_RESULT (phi);
       new_res = res;
       if (!virtual_operand_p (res))
@@ -2145,8 +2362,7 @@ copy_phis_for_bb (basic_block bb, copy_body_data *id)
              if (LOCATION_BLOCK (locus))
                {
                  tree *n;
-                 n = (tree *) pointer_map_contains (id->decl_map,
-                       LOCATION_BLOCK (locus));
+                 n = id->decl_map->get (LOCATION_BLOCK (locus));
                  gcc_assert (n);
                  if (*n)
                    locus = COMBINE_LOCATION_DATA (line_table, locus, *n);
@@ -2275,7 +2491,8 @@ maybe_move_debug_stmts_to_successors (copy_body_data *id, basic_block new_bb)
       gimple_stmt_iterator dsi = gsi_after_labels (e->dest);
       while (is_gimple_debug (gsi_stmt (ssi)))
        {
-         gimple stmt = gsi_stmt (ssi), new_stmt;
+         gimple stmt = gsi_stmt (ssi);
+         gdebug *new_stmt;
          tree var;
          tree value;
 
@@ -2336,11 +2553,8 @@ copy_loops (copy_body_data *id,
 
          /* Assign the new loop its header and latch and associate
             those with the new loop.  */
-         if (src_loop->header != NULL)
-           {
-             dest_loop->header = (basic_block)src_loop->header->aux;
-             dest_loop->header->loop_father = dest_loop;
-           }
+         dest_loop->header = (basic_block)src_loop->header->aux;
+         dest_loop->header->loop_father = dest_loop;
          if (src_loop->latch != NULL)
            {
              dest_loop->latch = (basic_block)src_loop->latch->aux;
@@ -2380,13 +2594,19 @@ void
 redirect_all_calls (copy_body_data * id, basic_block bb)
 {
   gimple_stmt_iterator si;
+  gimple last = last_stmt (bb);
   for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
     {
-      if (is_gimple_call (gsi_stmt (si)))
+      gimple stmt = gsi_stmt (si);
+      if (is_gimple_call (stmt))
        {
-         struct cgraph_edge *edge = cgraph_edge (id->dst_node, gsi_stmt (si));
+         struct cgraph_edge *edge = id->dst_node->get_edge (stmt);
          if (edge)
-           cgraph_redirect_edge_call_stmt_to_callee (edge);
+           {
+             edge->redirect_call_stmt_to_callee ();
+             if (stmt == last && id->call_stmt && maybe_clean_eh_stmt (stmt))
+               gimple_purge_dead_eh_edges (bb);
+           }
        }
     }
 }
@@ -2515,12 +2735,12 @@ copy_cfg_body (copy_body_data * id, gcov_type count, int frequency_scale,
 
   /* Now that we've duplicated the blocks, duplicate their edges.  */
   basic_block abnormal_goto_dest = NULL;
-  if (id->gimple_call
-      && stmt_can_make_abnormal_goto (id->gimple_call))
+  if (id->call_stmt
+      && stmt_can_make_abnormal_goto (id->call_stmt))
     {
-      gimple_stmt_iterator gsi = gsi_for_stmt (id->gimple_call);
+      gimple_stmt_iterator gsi = gsi_for_stmt (id->call_stmt);
 
-      bb = gimple_bb (id->gimple_call);
+      bb = gimple_bb (id->call_stmt);
       gsi_next (&gsi);
       if (gsi_end_p (gsi))
        abnormal_goto_dest = get_abnormal_succ_dispatcher (bb);
@@ -2568,7 +2788,9 @@ copy_cfg_body (copy_body_data * id, gcov_type count, int frequency_scale,
          maybe_move_debug_stmts_to_successors (id, (basic_block) bb->aux);
        /* Update call edge destinations.  This can not be done before loop
           info is updated, because we may split basic blocks.  */
-       if (id->transform_call_graph_edges == CB_CGE_DUPLICATE)
+       if (id->transform_call_graph_edges == CB_CGE_DUPLICATE
+           && bb->index != ENTRY_BLOCK
+           && bb->index != EXIT_BLOCK)
          redirect_all_calls (id, (basic_block)bb->aux);
        ((basic_block)bb->aux)->aux = NULL;
        bb->aux = NULL;
@@ -2592,9 +2814,14 @@ copy_cfg_body (copy_body_data * id, gcov_type count, int frequency_scale,
 
   if (id->eh_map)
     {
-      pointer_map_destroy (id->eh_map);
+      delete id->eh_map;
       id->eh_map = NULL;
     }
+  if (id->dependence_map)
+    {
+      delete id->dependence_map;
+      id->dependence_map = NULL;
+    }
 
   return new_fndecl;
 }
@@ -2606,14 +2833,14 @@ copy_cfg_body (copy_body_data * id, gcov_type count, int frequency_scale,
    this arises, we drop the VALUE expression altogether.  */
 
 static void
-copy_debug_stmt (gimple stmt, copy_body_data *id)
+copy_debug_stmt (gdebug *stmt, copy_body_data *id)
 {
   tree t, *n;
   struct walk_stmt_info wi;
 
   if (gimple_block (stmt))
     {
-      n = (tree *) pointer_map_contains (id->decl_map, gimple_block (stmt));
+      n = id->decl_map->get (gimple_block (stmt));
       gimple_set_block (stmt, n ? *n : id->block);
     }
 
@@ -2629,14 +2856,14 @@ copy_debug_stmt (gimple stmt, copy_body_data *id)
     t = gimple_debug_bind_get_var (stmt);
 
   if (TREE_CODE (t) == PARM_DECL && id->debug_map
-      && (n = (tree *) pointer_map_contains (id->debug_map, t)))
+      && (n = id->debug_map->get (t)))
     {
       gcc_assert (TREE_CODE (*n) == VAR_DECL);
       t = *n;
     }
   else if (TREE_CODE (t) == VAR_DECL
           && !is_global_var (t)
-          && !pointer_map_contains (id->decl_map, t))
+          && !id->decl_map->get (t))
     /* T is a non-localized variable.  */;
   else
     walk_tree (&t, remap_gimple_op_r, &wi, NULL);
@@ -2665,7 +2892,7 @@ copy_debug_stmt (gimple stmt, copy_body_data *id)
       t = gimple_debug_source_bind_get_value (stmt);
       if (t != NULL_TREE
          && TREE_CODE (t) == PARM_DECL
-         && id->gimple_call)
+         && id->call_stmt)
        {
          vec<tree, va_gc> **debug_args = decl_debug_args_lookup (id->src_fn);
          unsigned int i;
@@ -2698,7 +2925,7 @@ static void
 copy_debug_stmts (copy_body_data *id)
 {
   size_t i;
-  gimple stmt;
+  gdebug *stmt;
 
   if (!id->debug_stmts.exists ())
     return;
@@ -2775,7 +3002,7 @@ insert_init_debug_bind (copy_body_data *id,
   if (!gimple_in_ssa_p (id->src_cfun))
     return NULL;
 
-  if (!MAY_HAVE_DEBUG_STMTS)
+  if (!opt_for_fn (id->dst_fn, flag_var_tracking_assignments))
     return NULL;
 
   tracked_var = target_for_debug_bind (var);
@@ -2789,7 +3016,7 @@ insert_init_debug_bind (copy_body_data *id,
        base_stmt = gsi_stmt (gsi);
     }
 
-  note = gimple_build_debug_bind (tracked_var, value, base_stmt);
+  note = gimple_build_debug_bind (tracked_var, unshare_expr (value), base_stmt);
 
   if (bb)
     {
@@ -2831,7 +3058,7 @@ insert_init_stmt (copy_body_data *id, basic_block bb, gimple init_stmt)
       gsi_insert_after (&si, init_stmt, GSI_NEW_STMT);
       gimple_regimplify_operands (init_stmt, &si);
 
-      if (!is_gimple_debug (init_stmt) && MAY_HAVE_DEBUG_STMTS)
+      if (!is_gimple_debug (init_stmt))
        {
          tree def = gimple_assign_lhs (init_stmt);
          insert_init_debug_bind (id, bb, def, def, init_stmt);
@@ -2987,7 +3214,7 @@ setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
            }
          else if (!optimize)
            {
-             def = make_ssa_name (var, NULL);
+             def = make_ssa_name (var);
              init_stmt = gimple_build_assign (def, rhs);
            }
        }
@@ -3030,7 +3257,7 @@ initialize_inlined_parameters (copy_body_data *id, gimple stmt,
      parameter following the array.  */
   for (p = parms, i = 0; p; p = DECL_CHAIN (p), i++)
     {
-      tree *varp = (tree *) pointer_map_contains (id->decl_map, p);
+      tree *varp = id->decl_map->get (p);
       if (varp
          && TREE_CODE (*varp) == VAR_DECL)
        {
@@ -3043,7 +3270,7 @@ initialize_inlined_parameters (copy_body_data *id, gimple stmt,
             by the parameter setup.  */
          if (def)
            {
-             tree *defp = (tree *) pointer_map_contains (id->decl_map, def);
+             tree *defp = id->decl_map->get (def);
              if (defp
                  && TREE_CODE (*defp) == SSA_NAME
                  && SSA_NAME_VAR (*defp) == var)
@@ -3076,12 +3303,14 @@ initialize_inlined_parameters (copy_body_data *id, gimple stmt,
    is set only for CALL_EXPR_RETURN_SLOT_OPT.  MODIFY_DEST, if non-null,
    was the LHS of the MODIFY_EXPR to which this call is the RHS.
 
+   RETURN_BOUNDS holds a destination for returned bounds.
+
    The return value is a (possibly null) value that holds the result
    as seen by the caller.  */
 
 static tree
 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest,
-                        basic_block entry_bb)
+                        tree return_bounds, basic_block entry_bb)
 {
   tree callee = id->src_fn;
   tree result = DECL_RESULT (callee);
@@ -3250,7 +3479,7 @@ declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest,
       if (gimple_in_ssa_p (id->src_cfun)
          && is_gimple_reg (result))
        {
-         temp = make_ssa_name (temp, NULL);
+         temp = make_ssa_name (temp);
          insert_decl_map (id, ssa_default_def (id->src_cfun, result), temp);
        }
       insert_init_stmt (id, entry_bb, gimple_build_assign (temp, var));
@@ -3261,6 +3490,19 @@ declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest,
   /* Remember this so we can ignore it in remap_decls.  */
   id->retvar = var;
 
+  /* If returned bounds are used, then make var for them.  */
+  if (return_bounds)
+  {
+    tree bndtemp = create_tmp_var (pointer_bounds_type_node, "retbnd");
+    DECL_SEEN_IN_BIND_EXPR_P (bndtemp) = 1;
+    TREE_NO_WARNING (bndtemp) = 1;
+    declare_inline_vars (id->block, bndtemp);
+
+    id->retbnd = bndtemp;
+    insert_init_stmt (id, entry_bb,
+                     gimple_build_assign (bndtemp, chkp_get_zero_bounds_var ()));
+  }
+
   return use;
 }
 
@@ -3285,7 +3527,7 @@ has_label_address_in_static_1 (tree *nodep, int *walk_subtrees, void *fnp)
 /* Determine if the function can be copied.  If so return NULL.  If
    not return a string describng the reason for failure.  */
 
-static const char *
+const char *
 copy_forbidden (struct function *fun, tree fndecl)
 {
   const char *reason = fun->cannot_be_copied_reason;
@@ -3354,7 +3596,7 @@ inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
         VLA objects as those can't cause unbounded growth (they're always
         wrapped inside stack_save/stack_restore regions.  */
       if (gimple_alloca_call_p (stmt)
-         && !gimple_call_alloca_for_var_p (stmt)
+         && !gimple_call_alloca_for_var_p (as_a <gcall *> (stmt))
          && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
        {
          inline_forbidden_reason
@@ -3461,7 +3703,6 @@ inline_forbidden_p (tree fndecl)
 {
   struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
   struct walk_stmt_info wi;
-  struct pointer_set_t *visited_nodes;
   basic_block bb;
   bool forbidden_p = false;
 
@@ -3472,10 +3713,10 @@ inline_forbidden_p (tree fndecl)
 
   /* Next, walk the statements of the function looking for
      constraucts we can't handle, or are non-optimal for inlining.  */
-  visited_nodes = pointer_set_create ();
+  hash_set<tree> visited_nodes;
   memset (&wi, 0, sizeof (wi));
   wi.info = (void *) fndecl;
-  wi.pset = visited_nodes;
+  wi.pset = &visited_nodes;
 
   FOR_EACH_BB_FN (bb, fun)
     {
@@ -3487,7 +3728,6 @@ inline_forbidden_p (tree fndecl)
        break;
     }
 
-  pointer_set_destroy (visited_nodes);
   return forbidden_p;
 }
 \f
@@ -3577,11 +3817,12 @@ tree_inlinable_function_p (tree fn)
   return inlinable;
 }
 
-/* Estimate the cost of a memory move.  Use machine dependent
-   word size and take possible memcpy call into account.  */
+/* Estimate the cost of a memory move of type TYPE.  Use machine dependent
+   word size and take possible memcpy call into account and return
+   cost based on whether optimizing for size or speed according to SPEED_P.  */
 
 int
-estimate_move_cost (tree type)
+estimate_move_cost (tree type, bool ARG_UNUSED (speed_p))
 {
   HOST_WIDE_INT size;
 
@@ -3589,8 +3830,8 @@ estimate_move_cost (tree type)
 
   if (TREE_CODE (type) == VECTOR_TYPE)
     {
-      enum machine_mode inner = TYPE_MODE (TREE_TYPE (type));
-      enum machine_mode simd
+      machine_mode inner = TYPE_MODE (TREE_TYPE (type));
+      machine_mode simd
        = targetm.vectorize.preferred_simd_mode (inner);
       int simd_mode_size = GET_MODE_SIZE (simd);
       return ((GET_MODE_SIZE (TYPE_MODE (type)) + simd_mode_size - 1)
@@ -3599,7 +3840,7 @@ estimate_move_cost (tree type)
 
   size = int_size_in_bytes (type);
 
-  if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO (!optimize_size))
+  if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO (speed_p))
     /* Cost of a memcpy call, 3 arguments and the call.  */
     return 4;
   else
@@ -3650,8 +3891,6 @@ estimate_operator_cost (enum tree_code code, eni_weights *weights,
     case RSHIFT_EXPR:
     case LROTATE_EXPR:
     case RROTATE_EXPR:
-    case VEC_LSHIFT_EXPR:
-    case VEC_RSHIFT_EXPR:
 
     case BIT_IOR_EXPR:
     case BIT_XOR_EXPR:
@@ -3801,9 +4040,9 @@ estimate_num_insns (gimple stmt, eni_weights *weights)
 
       /* Account for the cost of moving to / from memory.  */
       if (gimple_store_p (stmt))
-       cost += estimate_move_cost (TREE_TYPE (lhs));
+       cost += estimate_move_cost (TREE_TYPE (lhs), weights->time_based);
       if (gimple_assign_load_p (stmt))
-       cost += estimate_move_cost (TREE_TYPE (rhs));
+       cost += estimate_move_cost (TREE_TYPE (rhs), weights->time_based);
 
       cost += estimate_operator_cost (gimple_assign_rhs_code (stmt), weights,
                                      gimple_assign_rhs1 (stmt),
@@ -3819,15 +4058,18 @@ estimate_num_insns (gimple stmt, eni_weights *weights)
       break;
 
     case GIMPLE_SWITCH:
-      /* Take into account cost of the switch + guess 2 conditional jumps for
-         each case label.
-
-        TODO: once the switch expansion logic is sufficiently separated, we can
-        do better job on estimating cost of the switch.  */
-      if (weights->time_based)
-        cost = floor_log2 (gimple_switch_num_labels (stmt)) * 2;
-      else
-        cost = gimple_switch_num_labels (stmt) * 2;
+      {
+       gswitch *switch_stmt = as_a <gswitch *> (stmt);
+       /* Take into account cost of the switch + guess 2 conditional jumps for
+          each case label.
+
+          TODO: once the switch expansion logic is sufficiently separated, we can
+          do better job on estimating cost of the switch.  */
+       if (weights->time_based)
+         cost = floor_log2 (gimple_switch_num_labels (switch_stmt)) * 2;
+       else
+         cost = gimple_switch_num_labels (switch_stmt) * 2;
+      }
       break;
 
     case GIMPLE_CALL:
@@ -3842,7 +4084,7 @@ estimate_num_insns (gimple stmt, eni_weights *weights)
            /* Do not special case builtins where we see the body.
               This just confuse inliner.  */
            struct cgraph_node *node;
-           if (!(node = cgraph_get_node (decl))
+           if (!(node = cgraph_node::get (decl))
                || node->definition)
              ;
            /* For buitins that are likely expanded to nothing or
@@ -3877,11 +4119,13 @@ estimate_num_insns (gimple stmt, eni_weights *weights)
 
        cost = decl ? weights->call_cost : weights->indirect_call_cost;
        if (gimple_call_lhs (stmt))
-         cost += estimate_move_cost (TREE_TYPE (gimple_call_lhs (stmt)));
+         cost += estimate_move_cost (TREE_TYPE (gimple_call_lhs (stmt)),
+                                     weights->time_based);
        for (i = 0; i < gimple_call_num_args (stmt); i++)
          {
            tree arg = gimple_call_arg (stmt, i);
-           cost += estimate_move_cost (TREE_TYPE (arg));
+           cost += estimate_move_cost (TREE_TYPE (arg),
+                                       weights->time_based);
          }
        break;
       }
@@ -3899,7 +4143,7 @@ estimate_num_insns (gimple stmt, eni_weights *weights)
 
     case GIMPLE_ASM:
       {
-       int count = asm_str_count (gimple_asm_string (stmt));
+       int count = asm_str_count (gimple_asm_string (as_a <gasm *> (stmt)));
        /* 1000 means infinity. This avoids overflows later
           with very long asm statements.  */
        if (count > 1000)
@@ -3919,19 +4163,23 @@ estimate_num_insns (gimple stmt, eni_weights *weights)
       return 10;
 
     case GIMPLE_BIND:
-      return estimate_num_insns_seq (gimple_bind_body (stmt), weights);
+      return estimate_num_insns_seq (
+              gimple_bind_body (as_a <gbind *> (stmt)),
+              weights);
 
     case GIMPLE_EH_FILTER:
       return estimate_num_insns_seq (gimple_eh_filter_failure (stmt), weights);
 
     case GIMPLE_CATCH:
-      return estimate_num_insns_seq (gimple_catch_handler (stmt), weights);
+      return estimate_num_insns_seq (gimple_catch_handler (
+                                      as_a <gcatch *> (stmt)),
+                                    weights);
 
     case GIMPLE_TRY:
       return (estimate_num_insns_seq (gimple_try_eval (stmt), weights)
               + estimate_num_insns_seq (gimple_try_cleanup (stmt), weights));
 
-    /* OpenMP directives are generally very expensive.  */
+    /* OMP directives are generally very expensive.  */
 
     case GIMPLE_OMP_RETURN:
     case GIMPLE_OMP_SECTIONS_SWITCH:
@@ -3964,7 +4212,8 @@ estimate_num_insns (gimple stmt, eni_weights *weights)
 
     case GIMPLE_TRANSACTION:
       return (weights->tm_cost
-             + estimate_num_insns_seq (gimple_transaction_body (stmt),
+             + estimate_num_insns_seq (gimple_transaction_body (
+                                         as_a <gtransaction *> (stmt)),
                                        weights));
 
     default:
@@ -4081,6 +4330,60 @@ add_local_variables (struct function *callee, struct function *caller,
       }
 }
 
+/* Add to BINDINGS a debug stmt resetting SRCVAR if inlining might
+   have brought in or introduced any debug stmts for SRCVAR.  */
+
+static inline void
+reset_debug_binding (copy_body_data *id, tree srcvar, gimple_seq *bindings)
+{
+  tree *remappedvarp = id->decl_map->get (srcvar);
+
+  if (!remappedvarp)
+    return;
+
+  if (TREE_CODE (*remappedvarp) != VAR_DECL)
+    return;
+
+  if (*remappedvarp == id->retvar || *remappedvarp == id->retbnd)
+    return;
+
+  tree tvar = target_for_debug_bind (*remappedvarp);
+  if (!tvar)
+    return;
+
+  gdebug *stmt = gimple_build_debug_bind (tvar, NULL_TREE,
+                                         id->call_stmt);
+  gimple_seq_add_stmt (bindings, stmt);
+}
+
+/* For each inlined variable for which we may have debug bind stmts,
+   add before GSI a final debug stmt resetting it, marking the end of
+   its life, so that var-tracking knows it doesn't have to compute
+   further locations for it.  */
+
+static inline void
+reset_debug_bindings (copy_body_data *id, gimple_stmt_iterator gsi)
+{
+  tree var;
+  unsigned ix;
+  gimple_seq bindings = NULL;
+
+  if (!gimple_in_ssa_p (id->src_cfun))
+    return;
+
+  if (!opt_for_fn (id->dst_fn, flag_var_tracking_assignments))
+    return;
+
+  for (var = DECL_ARGUMENTS (id->src_fn);
+       var; var = DECL_CHAIN (var))
+    reset_debug_binding (id, var, &bindings);
+
+  FOR_EACH_LOCAL_DECL (id->src_cfun, ix, var)
+    reset_debug_binding (id, var, &bindings);
+
+  gsi_insert_seq_before_without_update (&gsi, bindings, GSI_SAME_STMT);
+}
+
 /* If STMT is a GIMPLE_CALL, replace it with its inline expansion.  */
 
 static bool
@@ -4088,9 +4391,11 @@ expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
 {
   tree use_retvar;
   tree fn;
-  struct pointer_map_t *st, *dst;
+  hash_map<tree, tree> *dst;
+  hash_map<tree, tree> *st = NULL;
   tree return_slot;
   tree modify_dest;
+  tree return_bounds = NULL;
   location_t saved_location;
   struct cgraph_edge *cg_edge;
   cgraph_inline_failed_t reason;
@@ -4099,6 +4404,8 @@ expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
   gimple_stmt_iterator gsi, stmt_gsi;
   bool successfully_inlined = FALSE;
   bool purge_dead_abnormal_edges;
+  gcall *call_stmt;
+  unsigned int i;
 
   /* Set input_location here so we get the right instantiation context
      if we call instantiate_decl from inlinable_function_p.  */
@@ -4107,10 +4414,11 @@ expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
   input_location = gimple_location (stmt);
 
   /* From here on, we're only interested in CALL_EXPRs.  */
-  if (gimple_code (stmt) != GIMPLE_CALL)
+  call_stmt = dyn_cast <gcall *> (stmt);
+  if (!call_stmt)
     goto egress;
 
-  cg_edge = cgraph_edge (id->dst_node, stmt);
+  cg_edge = id->dst_node->get_edge (stmt);
   gcc_checking_assert (cg_edge);
   /* First, see if we can figure out what function is being called.
      If we cannot, then there is no hope of inlining the function.  */
@@ -4150,7 +4458,7 @@ expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
          && !cg_edge->callee->local.redefined_extern_inline
          /* During early inline pass, report only when optimization is
             not turned on.  */
-         && (cgraph_global_info_ready
+         && (symtab->global_info_ready
              || !optimize
              || cgraph_inline_failed_type (reason) == CIF_FINAL_ERROR)
          /* PR 20090218-1_0.c. Body can be provided by another module. */
@@ -4167,9 +4475,9 @@ expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
               && reason != CIF_UNSPECIFIED
               && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
               /* Do not warn about not inlined recursive calls.  */
-              && !cgraph_edge_recursive_p (cg_edge)
+              && !cg_edge->recursive_p ()
               /* Avoid warnings during early inline pass. */
-              && cgraph_global_info_ready)
+              && symtab->global_info_ready)
        {
          warning (OPT_Winline, "inlining failed in call to %q+F: %s",
                   fn, _(cgraph_inline_failed_string (reason)));
@@ -4178,15 +4486,16 @@ expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
       goto egress;
     }
   fn = cg_edge->callee->decl;
-  cgraph_get_body (cg_edge->callee);
+  cg_edge->callee->get_untransformed_body ();
 
 #ifdef ENABLE_CHECKING
   if (cg_edge->callee->decl != id->dst_node->decl)
-    verify_cgraph_node (cg_edge->callee);
+    cg_edge->callee->verify ();
 #endif
 
   /* We will be inlining this callee.  */
   id->eh_lp_nr = lookup_stmt_eh_lp (stmt);
+  id->assign_stmts.create (0);
 
   /* Update the callers EH personality.  */
   if (DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl))
@@ -4244,7 +4553,7 @@ expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
   /* Local declarations will be replaced by their equivalents in this
      map.  */
   st = id->decl_map;
-  id->decl_map = pointer_map_create ();
+  id->decl_map = new hash_map<tree, tree>;
   dst = id->debug_map;
   id->debug_map = NULL;
 
@@ -4252,7 +4561,15 @@ expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
   id->src_fn = fn;
   id->src_node = cg_edge->callee;
   id->src_cfun = DECL_STRUCT_FUNCTION (fn);
-  id->gimple_call = stmt;
+  id->call_stmt = stmt;
+
+  /* If the the src function contains an IFN_VA_ARG, then so will the dst
+     function after inlining.  */
+  if ((id->src_cfun->curr_properties & PROP_gimple_lva) == 0)
+    {
+      struct function *dst_cfun = DECL_STRUCT_FUNCTION (id->dst_fn);
+      dst_cfun->curr_properties &= ~PROP_gimple_lva;
+    }
 
   gcc_assert (!id->src_cfun->after_inlining);
 
@@ -4308,6 +4625,24 @@ expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
     {
       modify_dest = gimple_call_lhs (stmt);
 
+      /* Remember where to copy returned bounds.  */
+      if (gimple_call_with_bounds_p (stmt)
+         && TREE_CODE (modify_dest) == SSA_NAME)
+       {
+         gcall *retbnd = chkp_retbnd_call_by_val (modify_dest);
+         if (retbnd)
+           {
+             return_bounds = gimple_call_lhs (retbnd);
+             /* If returned bounds are not used then just
+                remove unused call.  */
+             if (!return_bounds)
+               {
+                 gimple_stmt_iterator iter = gsi_for_stmt (retbnd);
+                 gsi_remove (&iter, true);
+               }
+           }
+       }
+
       /* The function which we are inlining might not return a value,
         in which case we should issue a warning that the function
         does not return a value.  In that case the optimizers will
@@ -4317,7 +4652,7 @@ expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
       if (DECL_P (modify_dest))
        TREE_NO_WARNING (modify_dest) = 1;
 
-      if (gimple_call_return_slot_opt_p (stmt))
+      if (gimple_call_return_slot_opt_p (call_stmt))
        {
          return_slot = modify_dest;
          modify_dest = NULL;
@@ -4338,7 +4673,8 @@ expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
     }
 
   /* Declare the return variable for the function.  */
-  use_retvar = declare_return_variable (id, return_slot, modify_dest, bb);
+  use_retvar = declare_return_variable (id, return_slot, modify_dest,
+                                       return_bounds, bb);
 
   /* Add local vars in this inlined callee to caller.  */
   add_local_variables (id->src_cfun, cfun, id);
@@ -4361,6 +4697,8 @@ expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
             GCOV_COMPUTE_SCALE (cg_edge->frequency, CGRAPH_FREQ_BASE),
             bb, return_block, NULL);
 
+  reset_debug_bindings (id, stmt_gsi);
+
   /* Reset the escaped solution.  */
   if (cfun->gimple_df)
     pt_solution_reset (&cfun->gimple_df->escaped);
@@ -4368,10 +4706,10 @@ expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
   /* Clean up.  */
   if (id->debug_map)
     {
-      pointer_map_destroy (id->debug_map);
+      delete id->debug_map;
       id->debug_map = dst;
     }
-  pointer_map_destroy (id->decl_map);
+  delete id->decl_map;
   id->decl_map = st;
 
   /* Unlink the calls virtual operands before replacing it.  */
@@ -4390,6 +4728,12 @@ expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
       stmt = gimple_build_assign (gimple_call_lhs (stmt), use_retvar);
       gsi_replace (&stmt_gsi, stmt, false);
       maybe_clean_or_replace_eh_stmt (old_stmt, stmt);
+
+      /* Copy bounds if we copy structure with bounds.  */
+      if (chkp_function_instrumented_p (id->dst_fn)
+         && !BOUNDED_P (use_retvar)
+         && chkp_type_has_pointer (TREE_TYPE (use_retvar)))
+       id->assign_stmts.safe_push (stmt);
     }
   else
     {
@@ -4421,6 +4765,20 @@ expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
         gsi_remove (&stmt_gsi, true);
     }
 
+  /* Put returned bounds into the correct place if required.  */
+  if (return_bounds)
+    {
+      gimple old_stmt = SSA_NAME_DEF_STMT (return_bounds);
+      gimple new_stmt = gimple_build_assign (return_bounds, id->retbnd);
+      gimple_stmt_iterator bnd_gsi = gsi_for_stmt (old_stmt);
+      unlink_stmt_vdef (old_stmt);
+      gsi_replace (&bnd_gsi, new_stmt, false);
+      maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt);
+      cgraph_update_edges_for_call_stmt (old_stmt,
+                                        gimple_call_fndecl (old_stmt),
+                                        new_stmt);
+    }
+
   if (purge_dead_abnormal_edges)
     {
       gimple_purge_dead_eh_edges (return_block);
@@ -4437,6 +4795,11 @@ expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
       TREE_USED (gimple_assign_rhs1 (stmt)) = 1;
     }
 
+  /* Copy bounds for all generated assigns that need it.  */
+  for (i = 0; i < id->assign_stmts.length (); i++)
+    chkp_copy_bounds_for_assign (id->assign_stmts[i], cg_edge);
+  id->assign_stmts.release ();
+
   /* Output the inlining info for this abstract function, since it has been
      inlined.  If we don't do this now, we can lose the information about the
      variables in the function when the blocks get blown away as soon as we
@@ -4445,7 +4808,7 @@ expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
     (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
 
   /* Update callgraph if needed.  */
-  cgraph_remove_node (cg_edge->callee);
+  cg_edge->callee->remove ();
 
   id->block = NULL_TREE;
   successfully_inlined = TRUE;
@@ -4463,18 +4826,19 @@ static bool
 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
 {
   gimple_stmt_iterator gsi;
+  bool inlined = false;
 
-  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+  for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
     {
       gimple stmt = gsi_stmt (gsi);
+      gsi_prev (&gsi);
 
       if (is_gimple_call (stmt)
-         && !gimple_call_internal_p (stmt)
-         && expand_call_inline (bb, stmt, id))
-       return true;
+         && !gimple_call_internal_p (stmt))
+       inlined |= expand_call_inline (bb, stmt, id);
     }
 
-  return false;
+  return inlined;
 }
 
 
@@ -4482,7 +4846,7 @@ gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
    in the STATEMENTS pointer set.  */
 
 static void
-fold_marked_statements (int first, struct pointer_set_t *statements)
+fold_marked_statements (int first, hash_set<gimple> *statements)
 {
   for (; first < n_basic_blocks_for_fn (cfun); first++)
     if (BASIC_BLOCK_FOR_FN (cfun, first))
@@ -4492,7 +4856,7 @@ fold_marked_statements (int first, struct pointer_set_t *statements)
        for (gsi = gsi_start_bb (BASIC_BLOCK_FOR_FN (cfun, first));
             !gsi_end_p (gsi);
             gsi_next (&gsi))
-         if (pointer_set_contains (statements, gsi_stmt (gsi)))
+         if (statements->contains (gsi_stmt (gsi)))
            {
              gimple old_stmt = gsi_stmt (gsi);
              tree old_decl = is_gimple_call (old_stmt) ? gimple_call_fndecl (old_stmt) : 0;
@@ -4580,7 +4944,7 @@ optimize_inline_calls (tree fn)
   /* Clear out ID.  */
   memset (&id, 0, sizeof (id));
 
-  id.src_node = id.dst_node = cgraph_get_node (fn);
+  id.src_node = id.dst_node = cgraph_node::get (fn);
   gcc_assert (id.dst_node->definition);
   id.dst_fn = fn;
   /* Or any functions that aren't finished yet.  */
@@ -4593,7 +4957,7 @@ optimize_inline_calls (tree fn)
   id.transform_return_to_modify = true;
   id.transform_parameter = true;
   id.transform_lang_insert_block = NULL;
-  id.statements_to_fold = pointer_set_create ();
+  id.statements_to_fold = new hash_set<gimple>;
 
   push_gimplify_context ();
 
@@ -4619,7 +4983,7 @@ optimize_inline_calls (tree fn)
     {
       struct cgraph_edge *e;
 
-      verify_cgraph_node (id.dst_node);
+      id.dst_node->verify ();
 
       /* Double check that we inlined everything we are supposed to inline.  */
       for (e = id.dst_node->callees; e; e = e->next_callee)
@@ -4629,7 +4993,7 @@ optimize_inline_calls (tree fn)
 
   /* Fold queued statements.  */
   fold_marked_statements (last, id.statements_to_fold);
-  pointer_set_destroy (id.statements_to_fold);
+  delete id.statements_to_fold;
 
   gcc_assert (!id.debug_stmts.exists ());
 
@@ -4642,7 +5006,7 @@ optimize_inline_calls (tree fn)
 
   delete_unreachable_blocks_update_callgraph (&id);
 #ifdef ENABLE_CHECKING
-  verify_cgraph_node (id.dst_node);
+  id.dst_node->verify ();
 #endif
 
   /* It would be nice to check SSA/CFG/statement consistency here, but it is
@@ -4725,14 +5089,13 @@ copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
    the function into which the copy will be placed.  */
 
 static void
-remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
+remap_save_expr (tree *tp, hash_map<tree, tree> *st, int *walk_subtrees)
 {
-  struct pointer_map_t *st = (struct pointer_map_t *) st_;
   tree *n;
   tree t;
 
   /* See if we already encountered this SAVE_EXPR.  */
-  n = (tree *) pointer_map_contains (st, *tp);
+  n = st->get (*tp);
 
   /* If we didn't already remap this SAVE_EXPR, do so now.  */
   if (!n)
@@ -4740,9 +5103,9 @@ remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
       t = copy_node (*tp);
 
       /* Remember this SAVE_EXPR.  */
-      *pointer_map_insert (st, *tp) = t;
+      st->put (*tp, t);
       /* Make sure we don't remap an already-remapped SAVE_EXPR.  */
-      *pointer_map_insert (st, t) = t;
+      st->put (t, t);
     }
   else
     {
@@ -4765,9 +5128,9 @@ mark_local_labels_stmt (gimple_stmt_iterator *gsip,
                        struct walk_stmt_info *wi)
 {
   copy_body_data *id = (copy_body_data *) wi->info;
-  gimple stmt = gsi_stmt (*gsip);
+  glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsip));
 
-  if (gimple_code (stmt) == GIMPLE_LABEL)
+  if (stmt)
     {
       tree decl = gimple_label_label (stmt);
 
@@ -4789,7 +5152,7 @@ replace_locals_op (tree *tp, int *walk_subtrees, void *data)
 {
   struct walk_stmt_info *wi = (struct walk_stmt_info*) data;
   copy_body_data *id = (copy_body_data *) wi->info;
-  struct pointer_map_t *st = id->decl_map;
+  hash_map<tree, tree> *st = id->decl_map;
   tree *n;
   tree expr = *tp;
 
@@ -4799,7 +5162,7 @@ replace_locals_op (tree *tp, int *walk_subtrees, void *data)
       || TREE_CODE (expr) == LABEL_DECL)
     {
       /* Lookup the declaration.  */
-      n = (tree *) pointer_map_contains (st, expr);
+      n = st->get (expr);
 
       /* If it's there, remap it.  */
       if (n)
@@ -4839,9 +5202,9 @@ replace_locals_stmt (gimple_stmt_iterator *gsip,
                     struct walk_stmt_info *wi)
 {
   copy_body_data *id = (copy_body_data *) wi->info;
-  gimple stmt = gsi_stmt (*gsip);
+  gimple gs = gsi_stmt (*gsip);
 
-  if (gimple_code (stmt) == GIMPLE_BIND)
+  if (gbind *stmt = dyn_cast <gbind *> (gs))
     {
       tree block = gimple_bind_block (stmt);
 
@@ -4871,7 +5234,6 @@ copy_gimple_seq_and_replace_locals (gimple_seq seq)
 {
   copy_body_data id;
   struct walk_stmt_info wi;
-  struct pointer_set_t *visited;
   gimple_seq copy;
 
   /* There's nothing to do for NULL_TREE.  */
@@ -4882,7 +5244,7 @@ copy_gimple_seq_and_replace_locals (gimple_seq seq)
   memset (&id, 0, sizeof (id));
   id.src_fn = current_function_decl;
   id.dst_fn = current_function_decl;
-  id.decl_map = pointer_map_create ();
+  id.decl_map = new hash_map<tree, tree>;
   id.debug_map = NULL;
 
   id.copy_decl = copy_decl_no_change;
@@ -4894,11 +5256,10 @@ copy_gimple_seq_and_replace_locals (gimple_seq seq)
 
   /* Walk the tree once to find local labels.  */
   memset (&wi, 0, sizeof (wi));
-  visited = pointer_set_create ();
+  hash_set<tree> visited;
   wi.info = &id;
-  wi.pset = visited;
+  wi.pset = &visited;
   walk_gimple_seq (seq, mark_local_labels_stmt, NULL, &wi);
-  pointer_set_destroy (visited);
 
   copy = gimple_seq_copy (seq);
 
@@ -4908,9 +5269,14 @@ copy_gimple_seq_and_replace_locals (gimple_seq seq)
   walk_gimple_seq (copy, replace_locals_stmt, replace_locals_op, &wi);
 
   /* Clean up.  */
-  pointer_map_destroy (id.decl_map);
+  delete id.decl_map;
   if (id.debug_map)
-    pointer_map_destroy (id.debug_map);
+    delete id.debug_map;
+  if (id.dependence_map)
+    {
+      delete id.dependence_map;
+      id.dependence_map = NULL;
+    }
 
   return copy;
 }
@@ -5056,7 +5422,7 @@ copy_decl_no_change (tree decl, copy_body_data *id)
   copy = copy_node (decl);
 
   /* The COPY is not abstract; it will be generated in DST_FN.  */
-  DECL_ABSTRACT (copy) = 0;
+  DECL_ABSTRACT_P (copy) = false;
   lang_hooks.dup_lang_specific_decl (copy);
 
   /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
@@ -5100,7 +5466,7 @@ copy_arguments_for_versioning (tree orig_parm, copy_body_data * id,
         *parg = new_tree;
        parg = &DECL_CHAIN (new_tree);
       }
-    else if (!pointer_map_contains (id->decl_map, arg))
+    else if (!id->decl_map->get (arg))
       {
        /* Make an equivalent VAR_DECL.  If the argument was used
           as temporary variable later in function, the uses will be
@@ -5172,12 +5538,12 @@ delete_unreachable_blocks_update_callgraph (copy_body_data *id)
              id->dst_node->remove_stmt_references (gsi_stmt (bsi));
 
              if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL
-                 &&(e = cgraph_edge (id->dst_node, gsi_stmt (bsi))) != NULL)
+                 &&(e = id->dst_node->get_edge (gsi_stmt (bsi))) != NULL)
                {
                  if (!e->inline_failed)
-                   cgraph_remove_node_and_inline_clones (e->callee, id->dst_node);
+                   e->callee->remove_symbol_and_inline_clones (id->dst_node);
                  else
-                   cgraph_remove_edge (e);
+                   e->remove ();
                }
              if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
                  && id->dst_node->clones)
@@ -5185,12 +5551,12 @@ delete_unreachable_blocks_update_callgraph (copy_body_data *id)
                  {
                    node->remove_stmt_references (gsi_stmt (bsi));
                    if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL
-                       && (e = cgraph_edge (node, gsi_stmt (bsi))) != NULL)
+                       && (e = node->get_edge (gsi_stmt (bsi))) != NULL)
                      {
                        if (!e->inline_failed)
-                         cgraph_remove_node_and_inline_clones (e->callee, id->dst_node);
+                         e->callee->remove_symbol_and_inline_clones (id->dst_node);
                        else
-                         cgraph_remove_edge (e);
+                         e->remove ();
                      }
 
                    if (node->clones)
@@ -5267,7 +5633,7 @@ update_clone_info (copy_body_data * id)
 */
 void
 tree_function_versioning (tree old_decl, tree new_decl,
-                         vec<ipa_replace_map_p, va_gc> *tree_map,
+                         vec<ipa_replace_map *, va_gc> *tree_map,
                          bool update_clones, bitmap args_to_skip,
                          bool skip_return, bitmap blocks_to_copy,
                          basic_block new_entry)
@@ -5286,9 +5652,9 @@ tree_function_versioning (tree old_decl, tree new_decl,
              && TREE_CODE (new_decl) == FUNCTION_DECL);
   DECL_POSSIBLY_INLINED (old_decl) = 1;
 
-  old_version_node = cgraph_get_node (old_decl);
+  old_version_node = cgraph_node::get (old_decl);
   gcc_checking_assert (old_version_node);
-  new_version_node = cgraph_get_node (new_decl);
+  new_version_node = cgraph_node::get (new_decl);
   gcc_checking_assert (new_version_node);
 
   /* Copy over debug args.  */
@@ -5321,9 +5687,9 @@ tree_function_versioning (tree old_decl, tree new_decl,
   memset (&id, 0, sizeof (id));
 
   /* Generate a new name for the new version. */
-  id.statements_to_fold = pointer_set_create ();
+  id.statements_to_fold = new hash_set<gimple>;
 
-  id.decl_map = pointer_map_create ();
+  id.decl_map = new hash_map<tree, tree>;
   id.debug_map = NULL;
   id.src_fn = old_decl;
   id.dst_fn = new_decl;
@@ -5443,7 +5809,7 @@ tree_function_versioning (tree old_decl, tree new_decl,
          && DECL_BY_REFERENCE (DECL_RESULT (old_decl))
          && (old_name = ssa_default_def (id.src_cfun, DECL_RESULT (old_decl))))
        {
-         tree new_name = make_ssa_name (DECL_RESULT (new_decl), NULL);
+         tree new_name = make_ssa_name (DECL_RESULT (new_decl));
          insert_decl_map (&id, old_name, new_name);
          SSA_NAME_DEF_STMT (new_name) = gimple_build_nop ();
          set_ssa_default_def (cfun, DECL_RESULT (new_decl), new_name);
@@ -5485,18 +5851,23 @@ tree_function_versioning (tree old_decl, tree new_decl,
     }
 
   /* Clean up.  */
-  pointer_map_destroy (id.decl_map);
+  delete id.decl_map;
   if (id.debug_map)
-    pointer_map_destroy (id.debug_map);
+    delete id.debug_map;
   free_dominance_info (CDI_DOMINATORS);
   free_dominance_info (CDI_POST_DOMINATORS);
 
   fold_marked_statements (0, id.statements_to_fold);
-  pointer_set_destroy (id.statements_to_fold);
+  delete id.statements_to_fold;
   fold_cond_expr_cond ();
   delete_unreachable_blocks_update_callgraph (&id);
   if (id.dst_node->definition)
-    cgraph_rebuild_references ();
+    cgraph_edge::rebuild_references ();
+  if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
+    {
+      calculate_dominance_info (CDI_DOMINATORS);
+      fix_loop_structure (NULL);
+    }
   update_ssa (TODO_update_ssa);
 
   /* After partial cloning we need to rescale frequencies, so they are
@@ -5542,22 +5913,22 @@ maybe_inline_call_in_expr (tree exp)
   /* We can only try to inline "const" functions.  */
   if (fn && TREE_READONLY (fn) && DECL_SAVED_TREE (fn))
     {
-      struct pointer_map_t *decl_map = pointer_map_create ();
       call_expr_arg_iterator iter;
       copy_body_data id;
       tree param, arg, t;
+      hash_map<tree, tree> decl_map;
 
       /* Remap the parameters.  */
       for (param = DECL_ARGUMENTS (fn), arg = first_call_expr_arg (exp, &iter);
           param;
           param = DECL_CHAIN (param), arg = next_call_expr_arg (&iter))
-       *pointer_map_insert (decl_map, param) = arg;
+       decl_map.put (param, arg);
 
       memset (&id, 0, sizeof (id));
       id.src_fn = fn;
       id.dst_fn = current_function_decl;
       id.src_cfun = DECL_STRUCT_FUNCTION (fn);
-      id.decl_map = decl_map;
+      id.decl_map = &decl_map;
 
       id.copy_decl = copy_decl_no_change;
       id.transform_call_graph_edges = CB_CGE_DUPLICATE;
@@ -5575,7 +5946,6 @@ maybe_inline_call_in_expr (tree exp)
       id.eh_lp_nr = 0;
 
       t = copy_tree_body (&id);
-      pointer_map_destroy (decl_map);
 
       /* We can only return something suitable for use in a GENERIC
         expression tree.  */
@@ -5597,17 +5967,69 @@ build_duplicate_type (tree type)
   id.src_fn = current_function_decl;
   id.dst_fn = current_function_decl;
   id.src_cfun = cfun;
-  id.decl_map = pointer_map_create ();
+  id.decl_map = new hash_map<tree, tree>;
   id.debug_map = NULL;
   id.copy_decl = copy_decl_no_change;
 
   type = remap_type_1 (type, &id);
 
-  pointer_map_destroy (id.decl_map);
+  delete id.decl_map;
   if (id.debug_map)
-    pointer_map_destroy (id.debug_map);
+    delete id.debug_map;
 
   TYPE_CANONICAL (type) = type;
 
   return type;
 }
+
+/* Unshare the entire DECL_SAVED_TREE of FN and return the remapped
+   parameters and RESULT_DECL in PARMS and RESULT.  Used by C++ constexpr
+   evaluation.  */
+
+tree
+copy_fn (tree fn, tree& parms, tree& result)
+{
+  copy_body_data id;
+  tree param;
+  hash_map<tree, tree> decl_map;
+
+  tree *p = &parms;
+  *p = NULL_TREE;
+
+  memset (&id, 0, sizeof (id));
+  id.src_fn = fn;
+  id.dst_fn = current_function_decl;
+  id.src_cfun = DECL_STRUCT_FUNCTION (fn);
+  id.decl_map = &decl_map;
+
+  id.copy_decl = copy_decl_no_change;
+  id.transform_call_graph_edges = CB_CGE_DUPLICATE;
+  id.transform_new_cfg = false;
+  id.transform_return_to_modify = false;
+  id.transform_parameter = true;
+  id.transform_lang_insert_block = NULL;
+
+  /* Make sure not to unshare trees behind the front-end's back
+     since front-end specific mechanisms may rely on sharing.  */
+  id.regimplify = false;
+  id.do_not_unshare = true;
+
+  /* We're not inside any EH region.  */
+  id.eh_lp_nr = 0;
+
+  /* Remap the parameters and result and return them to the caller.  */
+  for (param = DECL_ARGUMENTS (fn);
+       param;
+       param = DECL_CHAIN (param))
+    {
+      *p = remap_decl (param, &id);
+      p = &DECL_CHAIN (*p);
+    }
+
+  if (DECL_RESULT (fn))
+    result = remap_decl (DECL_RESULT (fn), &id);
+  else
+    result = NULL_TREE;
+
+  return copy_tree_body (&id);
+}