PR c++/68795: fix uninitialized close_paren_loc in cp_parser_postfix_expression
[gcc.git] / gcc / cgraph.c
index 8f04284863f5c509788c9478870502d69cce5291..0d70904b5e8ceac49eeb966d23010e09827a8285 100644 (file)
@@ -1,5 +1,5 @@
 /* Callgraph handling code.
-   Copyright (C) 2003-2014 Free Software Foundation, Inc.
+   Copyright (C) 2003-2016 Free Software Foundation, Inc.
    Contributed by Jan Hubicka
 
 This file is part of GCC.
@@ -26,44 +26,39 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
-#include "tm.h"
+#include "backend.h"
+#include "target.h"
+#include "rtl.h"
 #include "tree.h"
+#include "gimple.h"
+#include "predict.h"
+#include "alloc-pool.h"
+#include "gimple-ssa.h"
+#include "cgraph.h"
+#include "lto-streamer.h"
+#include "fold-const.h"
 #include "varasm.h"
 #include "calls.h"
 #include "print-tree.h"
-#include "tree-inline.h"
 #include "langhooks.h"
-#include "hashtab.h"
-#include "hash-set.h"
-#include "toplev.h"
-#include "flags.h"
-#include "debug.h"
-#include "target.h"
-#include "cgraph.h"
 #include "intl.h"
-#include "tree-ssa-alias.h"
-#include "internal-fn.h"
 #include "tree-eh.h"
-#include "gimple-expr.h"
-#include "gimple.h"
 #include "gimple-iterator.h"
-#include "timevar.h"
-#include "dumpfile.h"
-#include "gimple-ssa.h"
-#include "cgraph.h"
 #include "tree-cfg.h"
 #include "tree-ssa.h"
 #include "value-prof.h"
-#include "except.h"
-#include "diagnostic-core.h"
-#include "rtl.h"
 #include "ipa-utils.h"
-#include "lto-streamer.h"
+#include "symbol-summary.h"
+#include "ipa-prop.h"
 #include "ipa-inline.h"
 #include "cfgloop.h"
 #include "gimple-pretty-print.h"
-#include "expr.h"
 #include "tree-dfa.h"
+#include "profile.h"
+#include "params.h"
+#include "tree-chkp.h"
+#include "context.h"
+#include "gimplify.h"
 
 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this.  */
 #include "tree-pass.h"
@@ -103,30 +98,34 @@ struct cgraph_2node_hook_list {
   struct cgraph_2node_hook_list *next;
 };
 
+/* Hash descriptor for cgraph_function_version_info.  */
+
+struct function_version_hasher : ggc_ptr_hash<cgraph_function_version_info>
+{
+  static hashval_t hash (cgraph_function_version_info *);
+  static bool equal (cgraph_function_version_info *,
+                    cgraph_function_version_info *);
+};
+
 /* Map a cgraph_node to cgraph_function_version_info using this htab.
    The cgraph_function_version_info has a THIS_NODE field that is the
    corresponding cgraph_node..  */
 
-static GTY((param_is (cgraph_function_version_info))) htab_t
-  cgraph_fnver_htab = NULL;
+static GTY(()) hash_table<function_version_hasher> *cgraph_fnver_htab = NULL;
 
 /* Hash function for cgraph_fnver_htab.  */
-static hashval_t
-cgraph_fnver_htab_hash (const void *ptr)
+hashval_t
+function_version_hasher::hash (cgraph_function_version_info *ptr)
 {
-  int uid = ((const cgraph_function_version_info *)ptr)->this_node->uid;
+  int uid = ptr->this_node->uid;
   return (hashval_t)(uid);
 }
 
 /* eq function for cgraph_fnver_htab.  */
-static int
-cgraph_fnver_htab_eq (const void *p1, const void *p2)
+bool
+function_version_hasher::equal (cgraph_function_version_info *n1,
+                               cgraph_function_version_info *n2)
 {
-  const cgraph_function_version_info *n1
-    = (const cgraph_function_version_info *)p1;
-  const cgraph_function_version_info *n2
-    = (const cgraph_function_version_info *)p2;
-
   return n1->this_node->uid == n2->this_node->uid;
 }
 
@@ -134,21 +133,40 @@ cgraph_fnver_htab_eq (const void *p1, const void *p2)
 static GTY(()) struct cgraph_function_version_info *
   version_info_node = NULL;
 
+/* Return true if NODE's address can be compared.  */
+
+bool
+symtab_node::address_can_be_compared_p ()
+{
+  /* Address of virtual tables and functions is never compared.  */
+  if (DECL_VIRTUAL_P (decl))
+    return false;
+  /* Address of C++ cdtors is never compared.  */
+  if (is_a <cgraph_node *> (this)
+      && (DECL_CXX_CONSTRUCTOR_P (decl)
+         || DECL_CXX_DESTRUCTOR_P (decl)))
+    return false;
+  /* Constant pool symbols addresses are never compared.
+     flag_merge_constants permits us to assume the same on readonly vars.  */
+  if (is_a <varpool_node *> (this)
+      && (DECL_IN_CONSTANT_POOL (decl)
+         || (flag_merge_constants >= 2
+             && TREE_READONLY (decl) && !TREE_THIS_VOLATILE (decl))))
+    return false;
+  return true;
+}
+
 /* Get the cgraph_function_version_info node corresponding to node.  */
 cgraph_function_version_info *
 cgraph_node::function_version (void)
 {
-  cgraph_function_version_info *ret;
   cgraph_function_version_info key;
   key.this_node = this;
 
   if (cgraph_fnver_htab == NULL)
     return NULL;
 
-  ret = (cgraph_function_version_info *)
-    htab_find (cgraph_fnver_htab, &key);
-
-  return ret;
+  return cgraph_fnver_htab->find (&key);
 }
 
 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
@@ -156,19 +174,15 @@ cgraph_node::function_version (void)
 cgraph_function_version_info *
 cgraph_node::insert_new_function_version (void)
 {
-  void **slot;
-  
   version_info_node = NULL;
   version_info_node = ggc_cleared_alloc<cgraph_function_version_info> ();
   version_info_node->this_node = this;
 
   if (cgraph_fnver_htab == NULL)
-    cgraph_fnver_htab = htab_create_ggc (2, cgraph_fnver_htab_hash,
-                                        cgraph_fnver_htab_eq, NULL);
+    cgraph_fnver_htab = hash_table<function_version_hasher>::create_ggc (2);
 
-  slot = htab_find_slot (cgraph_fnver_htab, version_info_node, INSERT);
-  gcc_assert (slot != NULL);
-  *slot = version_info_node;
+  *cgraph_fnver_htab->find_slot (version_info_node, INSERT)
+    = version_info_node;
   return version_info_node;
 }
 
@@ -195,7 +209,7 @@ cgraph_node::delete_function_version (tree decl)
     decl_v->next->prev = decl_v->prev;
 
   if (cgraph_fnver_htab != NULL)
-    htab_remove_elt (cgraph_fnver_htab, decl_v);
+    cgraph_fnver_htab->remove_elt (decl_v);
 
   decl_node->remove ();
 }
@@ -241,6 +255,30 @@ cgraph_node::record_function_versions (tree decl1, tree decl2)
   after->prev = before;
 }
 
+/* Initialize callgraph dump file.  */
+
+void
+symbol_table::initialize (void)
+{
+  if (!dump_file)
+    dump_file = dump_begin (TDI_cgraph, NULL);
+}
+
+/* Allocate new callgraph node and insert it into basic data structures.  */
+
+cgraph_node *
+symbol_table::create_empty (void)
+{
+  cgraph_node *node = allocate_cgraph_symbol ();
+
+  node->type = SYMTAB_FUNCTION;
+  node->frequency = NODE_FREQUENCY_NORMAL;
+  node->count_materialization_scale = REG_BR_PROB_BASE;
+  cgraph_count++;
+
+  return node;
+}
+
 /* Register HOOK to be called with DATA on each removed edge.  */
 cgraph_edge_hook_list *
 symbol_table::add_edge_removal_hook (cgraph_edge_hook hook, void *data)
@@ -457,6 +495,15 @@ cgraph_node::create (tree decl)
   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
 
   node->decl = decl;
+
+  if ((flag_openacc || flag_openmp)
+      && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
+    {
+      node->offloadable = 1;
+      if (ENABLE_OFFLOADING)
+       g->have_offload = true;
+    }
+
   node->register_symbol ();
 
   if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
@@ -489,12 +536,11 @@ cgraph_node::get_create (tree decl)
       if (dump_file)
        fprintf (dump_file, "Introduced new external node "
                 "(%s/%i) and turned into root of the clone tree.\n",
-                xstrdup (node->name ()), node->order);
+                node->name (), node->order);
     }
   else if (dump_file)
     fprintf (dump_file, "Introduced new external node "
-            "(%s/%i).\n", xstrdup (node->name ()),
-            node->order);
+            "(%s/%i).\n", node->name (), node->order);
   return node;
 }
 
@@ -515,7 +561,7 @@ cgraph_node::create_alias (tree alias, tree target)
   alias_node->definition = true;
   alias_node->alias = true;
   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
-    alias_node->weakref = true;
+    alias_node->transparent_alias = alias_node->weakref = true;
   return alias_node;
 }
 
@@ -596,18 +642,30 @@ cgraph_node::get_for_asmname (tree asmname)
 
 /* Returns a hash value for X (which really is a cgraph_edge).  */
 
-static hashval_t
-edge_hash (const void *x)
+hashval_t
+cgraph_edge_hasher::hash (cgraph_edge *e)
 {
-  return htab_hash_pointer (((const cgraph_edge *) x)->call_stmt);
+  /* This is a really poor hash function, but it is what htab_hash_pointer
+     uses.  */
+  return (hashval_t) ((intptr_t)e->call_stmt >> 3);
 }
 
-/* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y.  */
+/* Returns a hash value for X (which really is a cgraph_edge).  */
 
-static int
-edge_eq (const void *x, const void *y)
+hashval_t
+cgraph_edge_hasher::hash (gimple *call_stmt)
 {
-  return ((const cgraph_edge *) x)->call_stmt == y;
+  /* This is a really poor hash function, but it is what htab_hash_pointer
+     uses.  */
+  return (hashval_t) ((intptr_t)call_stmt >> 3);
+}
+
+/* Return nonzero if the call_stmt of cgraph_edge X is stmt *Y.  */
+
+inline bool
+cgraph_edge_hasher::equal (cgraph_edge *x, gimple *y)
+{
+  return x->call_stmt == y;
 }
 
 /* Add call graph edge E to call site hash of its caller.  */
@@ -615,12 +673,9 @@ edge_eq (const void *x, const void *y)
 static inline void
 cgraph_update_edge_in_call_site_hash (cgraph_edge *e)
 {
-  void **slot;
-  slot = htab_find_slot_with_hash (e->caller->call_site_hash,
-                                  e->call_stmt,
-                                  htab_hash_pointer (e->call_stmt),
-                                  INSERT);
-  *slot = e;
+  gimple *call = e->call_stmt;
+  *e->caller->call_site_hash->find_slot_with_hash
+      (call, cgraph_edge_hasher::hash (call), INSERT) = e;
 }
 
 /* Add call graph edge E to call site hash of its caller.  */
@@ -628,15 +683,12 @@ cgraph_update_edge_in_call_site_hash (cgraph_edge *e)
 static inline void
 cgraph_add_edge_to_call_site_hash (cgraph_edge *e)
 {
-  void **slot;
   /* There are two speculative edges for every statement (one direct,
      one indirect); always hash the direct one.  */
   if (e->speculative && e->indirect_unknown_callee)
     return;
-  slot = htab_find_slot_with_hash (e->caller->call_site_hash,
-                                  e->call_stmt,
-                                  htab_hash_pointer (e->call_stmt),
-                                  INSERT);
+  cgraph_edge **slot = e->caller->call_site_hash->find_slot_with_hash
+      (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt), INSERT);
   if (*slot)
     {
       gcc_assert (((cgraph_edge *)*slot)->speculative);
@@ -652,15 +704,14 @@ cgraph_add_edge_to_call_site_hash (cgraph_edge *e)
    CALL_STMT.  */
 
 cgraph_edge *
-cgraph_node::get_edge (gimple call_stmt)
+cgraph_node::get_edge (gimple *call_stmt)
 {
   cgraph_edge *e, *e2;
   int n = 0;
 
   if (call_site_hash)
-    return (cgraph_edge *)
-      htab_find_with_hash (call_site_hash, call_stmt,
-                          htab_hash_pointer (call_stmt));
+    return call_site_hash->find_with_hash
+       (call_stmt, cgraph_edge_hasher::hash (call_stmt));
 
   /* This loop may turn out to be performance problem.  In such case adding
      hashtables into call nodes with very many edges is probably best
@@ -684,7 +735,7 @@ cgraph_node::get_edge (gimple call_stmt)
 
   if (n > 100)
     {
-      call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
+      call_site_hash = hash_table<cgraph_edge_hasher>::create_ggc (120);
       for (e2 = callees; e2; e2 = e2->next_callee)
        cgraph_add_edge_to_call_site_hash (e2);
       for (e2 = indirect_calls; e2; e2 = e2->next_callee)
@@ -700,7 +751,7 @@ cgraph_node::get_edge (gimple call_stmt)
    edge, then update all components.  */
 
 void
-cgraph_edge::set_call_stmt (gimple new_stmt, bool update_speculative)
+cgraph_edge::set_call_stmt (gcall *new_stmt, bool update_speculative)
 {
   tree decl;
 
@@ -722,9 +773,8 @@ cgraph_edge::set_call_stmt (gimple new_stmt, bool update_speculative)
   if (caller->call_site_hash
       && (!speculative || !indirect_unknown_callee))
     {
-      htab_remove_elt_with_hash (caller->call_site_hash,
-                                call_stmt,
-                                htab_hash_pointer (call_stmt));
+      caller->call_site_hash->remove_elt_with_hash
+       (call_stmt, cgraph_edge_hasher::hash (call_stmt));
     }
 
   cgraph_edge *e = this;
@@ -754,8 +804,8 @@ cgraph_edge::set_call_stmt (gimple new_stmt, bool update_speculative)
 
 cgraph_edge *
 symbol_table::create_edge (cgraph_node *caller, cgraph_node *callee,
-                    gimple call_stmt, gcov_type count, int freq,
-                    bool indir_unknown_callee)
+                          gcall *call_stmt, gcov_type count, int freq,
+                          bool indir_unknown_callee)
 {
   cgraph_edge *edge;
 
@@ -765,11 +815,9 @@ symbol_table::create_edge (cgraph_node *caller, cgraph_node *callee,
     {
       /* This is a rather expensive check possibly triggering
         construction of call stmt hashtable.  */
-#ifdef ENABLE_CHECKING
       cgraph_edge *e;
-      gcc_checking_assert (
-       !(e = caller->get_edge (call_stmt)) || e->speculative);
-#endif
+      gcc_checking_assert (!(e = caller->get_edge (call_stmt))
+                          || e->speculative);
 
       gcc_assert (is_gimple_call (call_stmt));
     }
@@ -819,6 +867,13 @@ symbol_table::create_edge (cgraph_node *caller, cgraph_node *callee,
   edge->indirect_inlining_edge = 0;
   edge->speculative = false;
   edge->indirect_unknown_callee = indir_unknown_callee;
+  if (opt_for_fn (edge->caller->decl, flag_devirtualize)
+      && call_stmt && DECL_STRUCT_FUNCTION (caller->decl))
+    edge->in_polymorphic_cdtor
+      = decl_maybe_in_construction_p (NULL, NULL, call_stmt,
+                                     caller->decl);
+  else
+    edge->in_polymorphic_cdtor = caller->thunk.thunk_p;
   if (call_stmt && caller->call_site_hash)
     cgraph_add_edge_to_call_site_hash (edge);
 
@@ -829,7 +884,7 @@ symbol_table::create_edge (cgraph_node *caller, cgraph_node *callee,
 
 cgraph_edge *
 cgraph_node::create_edge (cgraph_node *callee,
-                         gimple call_stmt, gcov_type count, int freq)
+                         gcall *call_stmt, gcov_type count, int freq)
 {
   cgraph_edge *edge = symtab->create_edge (this, callee, call_stmt, count,
                                           freq, false);
@@ -865,7 +920,7 @@ cgraph_allocate_init_indirect_info (void)
    PARAM_INDEX. */
 
 cgraph_edge *
-cgraph_node::create_indirect_edge (gimple call_stmt, int ecf_flags,
+cgraph_node::create_indirect_edge (gcall *call_stmt, int ecf_flags,
                                   gcov_type count, int freq,
                                   bool compute_indirect_info)
 {
@@ -877,6 +932,7 @@ cgraph_node::create_indirect_edge (gimple call_stmt, int ecf_flags,
 
   edge->indirect_info = cgraph_allocate_init_indirect_info ();
   edge->indirect_info->ecf_flags = ecf_flags;
+  edge->indirect_info->vptr_changed = true;
 
   /* Record polymorphic call info.  */
   if (compute_indirect_info
@@ -893,16 +949,7 @@ cgraph_node::create_indirect_edge (gimple call_stmt, int ecf_flags,
         = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
       edge->indirect_info->otr_type = obj_type_ref_class (target);
       gcc_assert (TREE_CODE (edge->indirect_info->otr_type) == RECORD_TYPE);
-      edge->indirect_info->outer_type = context.outer_type;
-      edge->indirect_info->speculative_outer_type
-        = context.speculative_outer_type;
-      edge->indirect_info->offset = context.offset;
-      edge->indirect_info->speculative_offset = context.speculative_offset;
-      edge->indirect_info->maybe_in_construction
-        = context.maybe_in_construction;
-      edge->indirect_info->maybe_derived_type = context.maybe_derived_type;
-      edge->indirect_info->speculative_maybe_derived_type
-        = context.speculative_maybe_derived_type;
+      edge->indirect_info->context = context;
     }
 
   edge->next_callee = indirect_calls;
@@ -913,20 +960,6 @@ cgraph_node::create_indirect_edge (gimple call_stmt, int ecf_flags,
   return edge;
 }
 
-/* Remove the edge from the list of the callers of the callee.  */
-
-void
-cgraph_edge::remove_callee (void)
-{
-  gcc_assert (!indirect_unknown_callee);
-  if (prev_caller)
-    prev_caller->next_caller = next_caller;
-  if (next_caller)
-    next_caller->prev_caller = prev_caller;
-  if (!prev_caller)
-    callee->callers = next_caller;
-}
-
 /* Remove the edge from the list of the callees of the caller.  */
 
 void
@@ -944,9 +977,8 @@ cgraph_edge::remove_caller (void)
        caller->callees = next_callee;
     }
   if (caller->call_site_hash)
-    htab_remove_elt_with_hash (caller->call_site_hash,
-                              call_stmt,
-                              htab_hash_pointer (call_stmt));
+    caller->call_site_hash->remove_elt_with_hash
+       (call_stmt, cgraph_edge_hasher::hash (call_stmt));
 }
 
 /* Put the edge onto the free list.  */
@@ -986,20 +1018,6 @@ cgraph_edge::remove (void)
   symtab->free_edge (this);
 }
 
-/* Set callee of call graph edge E and add it to the corresponding set of
-   callers. */
-
-static void
-cgraph_set_edge_callee (cgraph_edge *e, cgraph_node *n)
-{
-  e->prev_caller = NULL;
-  if (n->callers)
-    n->callers->prev_caller = e;
-  e->next_caller = n->callers;
-  n->callers = e;
-  e->callee = n;
-}
-
 /* Turn edge into speculative call calling N2. Update
    the profile so the direct call is taken COUNT times
    with FREQUENCY.  
@@ -1030,8 +1048,8 @@ cgraph_edge::make_speculative (cgraph_node *n2, gcov_type direct_count,
     {
       fprintf (dump_file, "Indirect call -> speculative call"
               " %s/%i => %s/%i\n",
-              xstrdup (n->name ()), n->order,
-              xstrdup (n2->name ()), n2->order);
+              xstrdup_for_dump (n->name ()), n->order,
+              xstrdup_for_dump (n2->name ()), n2->order);
     }
   speculative = true;
   e2 = n->create_edge (n2, call_stmt, direct_count, direct_frequency);
@@ -1042,6 +1060,7 @@ cgraph_edge::make_speculative (cgraph_node *n2, gcov_type direct_count,
   else
     e2->can_throw_external = can_throw_external;
   e2->lto_stmt_uid = lto_stmt_uid;
+  e2->in_polymorphic_cdtor = in_polymorphic_cdtor;
   count -= e2->count;
   frequency -= e2->frequency;
   symtab->call_edge_duplication_hooks (this, e2);
@@ -1113,19 +1132,6 @@ cgraph_edge::speculative_call_info (cgraph_edge *&direct,
   gcc_assert (e && e2 && ref);
 }
 
-/* Redirect callee of the edge to N.  The function does not update underlying
-   call expression.  */
-
-void
-cgraph_edge::redirect_callee (cgraph_node *n)
-{
-  /* Remove from callers list of the current callee.  */
-  remove_callee ();
-
-  /* Insert to callers list of the new callee.  */
-  cgraph_set_edge_callee (this, n);
-}
-
 /* Speculative call edge turned out to be direct call to CALLE_DECL.
    Remove the speculative call sequence and return edge representing the call.
    It is up to caller to redirect the call as appropriate. */
@@ -1149,16 +1155,20 @@ cgraph_edge::resolve_speculation (tree callee_decl)
            {
              fprintf (dump_file, "Speculative indirect call %s/%i => %s/%i has "
                       "turned out to have contradicting known target ",
-                      xstrdup (edge->caller->name ()), edge->caller->order,
-                      xstrdup (e2->callee->name ()), e2->callee->order);
+                      xstrdup_for_dump (edge->caller->name ()),
+                      edge->caller->order,
+                      xstrdup_for_dump (e2->callee->name ()),
+                      e2->callee->order);
              print_generic_expr (dump_file, callee_decl, 0);
              fprintf (dump_file, "\n");
            }
          else
            {
              fprintf (dump_file, "Removing speculative call %s/%i => %s/%i\n",
-                      xstrdup (edge->caller->name ()), edge->caller->order,
-                      xstrdup (e2->callee->name ()), e2->callee->order);
+                      xstrdup_for_dump (edge->caller->name ()),
+                      edge->caller->order,
+                      xstrdup_for_dump (e2->callee->name ()),
+                      e2->callee->order);
            }
        }
     }
@@ -1228,7 +1238,7 @@ cgraph_edge::make_direct (cgraph_node *callee)
   caller->callees = edge;
 
   /* Insert to callers list of the new callee.  */
-  cgraph_set_edge_callee (edge, callee);
+  edge->set_callee (callee);
 
   if (call_stmt)
     call_stmt_cannot_inline_p
@@ -1243,23 +1253,21 @@ cgraph_edge::make_direct (cgraph_node *callee)
 /* If necessary, change the function declaration in the call statement
    associated with E so that it corresponds to the edge callee.  */
 
-gimple
+gimple *
 cgraph_edge::redirect_call_stmt_to_callee (void)
 {
   cgraph_edge *e = this;
 
   tree decl = gimple_call_fndecl (e->call_stmt);
   tree lhs = gimple_call_lhs (e->call_stmt);
-  gimple new_stmt;
+  gcall *new_stmt;
   gimple_stmt_iterator gsi;
-#ifdef ENABLE_CHECKING
-  cgraph_node *node;
-#endif
+  bool skip_bounds = false;
 
   if (e->speculative)
     {
       cgraph_edge *e2;
-      gimple new_stmt;
+      gcall *new_stmt;
       ipa_ref *ref;
 
       e->speculative_call_info (e, e2, ref);
@@ -1268,7 +1276,7 @@ cgraph_edge::redirect_call_stmt_to_callee (void)
       if (decl)
        e = e->resolve_speculation (decl);
       /* If types do not match, speculation was likely wrong. 
-         The direct edge was posisbly redirected to the clone with a different
+         The direct edge was possibly redirected to the clone with a different
         signature.  We did not update the call statement yet, so compare it 
         with the reference that still points to the proper type.  */
       else if (!gimple_check_call_matching_types (e->call_stmt,
@@ -1278,9 +1286,9 @@ cgraph_edge::redirect_call_stmt_to_callee (void)
          if (dump_file)
            fprintf (dump_file, "Not expanding speculative call of %s/%i -> %s/%i\n"
                     "Type mismatch.\n",
-                    xstrdup (e->caller->name ()),
+                    xstrdup_for_dump (e->caller->name ()),
                     e->caller->order,
-                    xstrdup (e->callee->name ()),
+                    xstrdup_for_dump (e->callee->name ()),
                     e->callee->order);
          e = e->resolve_speculation ();
          /* We are producing the final function body and will throw away the
@@ -1296,15 +1304,16 @@ cgraph_edge::redirect_call_stmt_to_callee (void)
          if (dump_file)
            fprintf (dump_file,
                     "Expanding speculative call of %s/%i -> %s/%i count:"
-                    "%"PRId64"\n",
-                    xstrdup (e->caller->name ()),
+                    "%" PRId64"\n",
+                    xstrdup_for_dump (e->caller->name ()),
                     e->caller->order,
-                    xstrdup (e->callee->name ()),
+                    xstrdup_for_dump (e->callee->name ()),
                     e->callee->order,
                     (int64_t)e->count);
          gcc_assert (e2->speculative);
          push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
-         new_stmt = gimple_ic (e->call_stmt, dyn_cast<cgraph_node *> (ref->referred),
+         new_stmt = gimple_ic (e->call_stmt,
+                               dyn_cast<cgraph_node *> (ref->referred),
                                e->count || e2->count
                                ?  RDIV (e->count * REG_BR_PROB_BASE,
                                         e->count + e2->count)
@@ -1316,6 +1325,33 @@ cgraph_edge::redirect_call_stmt_to_callee (void)
          e->speculative = false;
          e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt,
                                                     false);
+
+         /* Fix edges for BUILT_IN_CHKP_BNDRET calls attached to the
+            processed call stmt.  */
+         if (gimple_call_with_bounds_p (new_stmt)
+             && gimple_call_lhs (new_stmt)
+             && chkp_retbnd_call_by_val (gimple_call_lhs (e2->call_stmt)))
+           {
+             tree dresult = gimple_call_lhs (new_stmt);
+             tree iresult = gimple_call_lhs (e2->call_stmt);
+             gcall *dbndret = chkp_retbnd_call_by_val (dresult);
+             gcall *ibndret = chkp_retbnd_call_by_val (iresult);
+             struct cgraph_edge *iedge
+               = e2->caller->cgraph_node::get_edge (ibndret);
+             struct cgraph_edge *dedge;
+
+             if (dbndret)
+               {
+                 dedge = iedge->caller->create_edge (iedge->callee,
+                                                     dbndret, e->count,
+                                                     e->frequency);
+                 dedge->frequency = compute_call_stmt_bb_frequency
+                   (dedge->caller->decl, gimple_bb (dedge->call_stmt));
+               }
+             iedge->frequency = compute_call_stmt_bb_frequency
+               (iedge->caller->decl, gimple_bb (iedge->call_stmt));
+           }
+
          e->frequency = compute_call_stmt_bb_frequency
                           (e->caller->decl, gimple_bb (e->call_stmt));
          e2->frequency = compute_call_stmt_bb_frequency
@@ -1332,23 +1368,29 @@ cgraph_edge::redirect_call_stmt_to_callee (void)
        }
     }
 
+  /* We might propagate instrumented function pointer into
+     not instrumented function and vice versa.  In such a
+     case we need to either fix function declaration or
+     remove bounds from call statement.  */
+  if (flag_check_pointer_bounds && e->callee)
+    skip_bounds = chkp_redirect_edge (e);
+
   if (e->indirect_unknown_callee
-      || decl == e->callee->decl)
+      || (decl == e->callee->decl
+         && !skip_bounds))
     return e->call_stmt;
 
-#ifdef ENABLE_CHECKING
-  if (decl)
+  if (flag_checking && decl)
     {
-      node = cgraph_node::get (decl);
+      cgraph_node *node = cgraph_node::get (decl);
       gcc_assert (!node || !node->clone.combined_args_to_skip);
     }
-#endif
 
   if (symtab->dump_file)
     {
       fprintf (symtab->dump_file, "updating call of %s/%i -> %s/%i: ",
-              xstrdup (e->caller->name ()), e->caller->order,
-              xstrdup (e->callee->name ()), e->callee->order);
+              xstrdup_for_dump (e->caller->name ()), e->caller->order,
+              xstrdup_for_dump (e->callee->name ()), e->callee->order);
       print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
       if (e->callee->clone.combined_args_to_skip)
        {
@@ -1358,13 +1400,19 @@ cgraph_edge::redirect_call_stmt_to_callee (void)
        }
     }
 
-  if (e->callee->clone.combined_args_to_skip)
+  if (e->callee->clone.combined_args_to_skip
+      || skip_bounds)
     {
       int lp_nr;
 
-      new_stmt
-       = gimple_call_copy_skip_args (e->call_stmt,
-                                     e->callee->clone.combined_args_to_skip);
+      new_stmt = e->call_stmt;
+      if (e->callee->clone.combined_args_to_skip)
+       new_stmt
+         = gimple_call_copy_skip_args (new_stmt,
+                                       e->callee->clone.combined_args_to_skip);
+      if (skip_bounds)
+       new_stmt = chkp_copy_call_skip_bounds (new_stmt);
+
       gimple_call_set_fndecl (new_stmt, e->callee->decl);
       gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
 
@@ -1373,6 +1421,70 @@ cgraph_edge::redirect_call_stmt_to_callee (void)
        SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
 
       gsi = gsi_for_stmt (e->call_stmt);
+
+      /* For optimized away parameters, add on the caller side
+        before the call
+        DEBUG D#X => parm_Y(D)
+        stmts and associate D#X with parm in decl_debug_args_lookup
+        vector to say for debug info that if parameter parm had been passed,
+        it would have value parm_Y(D).  */
+      if (e->callee->clone.combined_args_to_skip && MAY_HAVE_DEBUG_STMTS)
+       {
+         vec<tree, va_gc> **debug_args
+           = decl_debug_args_lookup (e->callee->decl);
+         tree old_decl = gimple_call_fndecl (e->call_stmt);
+         if (debug_args && old_decl)
+           {
+             tree parm;
+             unsigned i = 0, num;
+             unsigned len = vec_safe_length (*debug_args);
+             unsigned nargs = gimple_call_num_args (e->call_stmt);
+             for (parm = DECL_ARGUMENTS (old_decl), num = 0;
+                  parm && num < nargs;
+                  parm = DECL_CHAIN (parm), num++)
+               if (bitmap_bit_p (e->callee->clone.combined_args_to_skip, num)
+                   && is_gimple_reg (parm))
+                 {
+                   unsigned last = i;
+
+                   while (i < len && (**debug_args)[i] != DECL_ORIGIN (parm))
+                     i += 2;
+                   if (i >= len)
+                     {
+                       i = 0;
+                       while (i < last
+                              && (**debug_args)[i] != DECL_ORIGIN (parm))
+                         i += 2;
+                       if (i >= last)
+                         continue;
+                     }
+                   tree ddecl = (**debug_args)[i + 1];
+                   tree arg = gimple_call_arg (e->call_stmt, num);
+                   if (!useless_type_conversion_p (TREE_TYPE (ddecl),
+                                                   TREE_TYPE (arg)))
+                     {
+                       tree rhs1;
+                       if (!fold_convertible_p (TREE_TYPE (ddecl), arg))
+                         continue;
+                       if (TREE_CODE (arg) == SSA_NAME
+                           && gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg))
+                           && (rhs1
+                               = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (arg)))
+                           && useless_type_conversion_p (TREE_TYPE (ddecl),
+                                                         TREE_TYPE (rhs1)))
+                         arg = rhs1;
+                       else
+                         arg = fold_convert (TREE_TYPE (ddecl), arg);
+                     }
+
+                   gimple *def_temp
+                     = gimple_build_debug_bind (ddecl, unshare_expr (arg),
+                                                e->call_stmt);
+                   gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
+                 }
+           }
+       }
+
       gsi_replace (&gsi, new_stmt, false);
       /* We need to defer cleaning EH info on the new statement to
          fixup-cfg.  We may not have dominator information at this point
@@ -1392,8 +1504,10 @@ cgraph_edge::redirect_call_stmt_to_callee (void)
       update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
     }
 
-  /* If the call becomes noreturn, remove the lhs.  */
-  if (lhs && (gimple_call_flags (new_stmt) & ECF_NORETURN))
+  /* If the call becomes noreturn, remove the LHS if possible.  */
+  if (lhs
+      && (gimple_call_flags (new_stmt) & ECF_NORETURN)
+      && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
     {
       if (TREE_CODE (lhs) == SSA_NAME)
        {
@@ -1401,7 +1515,7 @@ cgraph_edge::redirect_call_stmt_to_callee (void)
                                        TREE_TYPE (lhs), NULL);
          var = get_or_create_ssa_default_def
                  (DECL_STRUCT_FUNCTION (e->caller->decl), var);
-         gimple set_stmt = gimple_build_assign (lhs, var);
+         gimple *set_stmt = gimple_build_assign (lhs, var);
           gsi = gsi_for_stmt (new_stmt);
          gsi_insert_before_without_update (&gsi, set_stmt, GSI_SAME_STMT);
          update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), set_stmt);
@@ -1417,6 +1531,9 @@ cgraph_edge::redirect_call_stmt_to_callee (void)
       update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
     }
 
+  maybe_remove_unused_call_args (DECL_STRUCT_FUNCTION (e->caller->decl),
+                                new_stmt);
+
   e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, false);
 
   if (symtab->dump_file)
@@ -1435,8 +1552,8 @@ cgraph_edge::redirect_call_stmt_to_callee (void)
 
 static void
 cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
-                                       gimple old_stmt, tree old_call,
-                                       gimple new_stmt)
+                                       gimple *old_stmt, tree old_call,
+                                       gimple *new_stmt)
 {
   tree new_call = (new_stmt && is_gimple_call (new_stmt))
                  ? gimple_call_fndecl (new_stmt) : 0;
@@ -1455,6 +1572,15 @@ cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
 
       if (e)
        {
+         /* Keep calls marked as dead dead.  */
+         if (new_stmt && is_gimple_call (new_stmt) && e->callee
+             && DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
+             && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_UNREACHABLE)
+           {
+              node->get_edge (old_stmt)->set_call_stmt
+                (as_a <gcall *> (new_stmt));
+             return;
+           }
          /* See if the edge is already there and has the correct callee.  It
             might be so because of indirect inlining has already updated
             it.  We also might've cloned and redirected the edge.  */
@@ -1466,7 +1592,7 @@ cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
                  if (callee->decl == new_call
                      || callee->former_clone_of == new_call)
                    {
-                     e->set_call_stmt (new_stmt);
+                     e->set_call_stmt (as_a <gcall *> (new_stmt));
                      return;
                    }
                  callee = callee->clone_of;
@@ -1495,13 +1621,14 @@ cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
       if (new_call)
        {
          ne = node->create_edge (cgraph_node::get_create (new_call),
-                                 new_stmt, count, frequency);
+                                 as_a <gcall *> (new_stmt), count,
+                                 frequency);
          gcc_assert (ne->inline_failed);
        }
     }
   /* We only updated the call stmt; update pointer in cgraph edge..  */
   else if (old_stmt != new_stmt)
-    node->get_edge (old_stmt)->set_call_stmt (new_stmt);
+    node->get_edge (old_stmt)->set_call_stmt (as_a <gcall *> (new_stmt));
 }
 
 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
@@ -1509,7 +1636,8 @@ cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
    of OLD_STMT before it was updated (updating can happen inplace).  */
 
 void
-cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
+cgraph_update_edges_for_call_stmt (gimple *old_stmt, tree old_decl,
+                                  gimple *new_stmt)
 {
   cgraph_node *orig = cgraph_node::get (cfun->decl);
   cgraph_node *node;
@@ -1565,7 +1693,7 @@ cgraph_node::remove_callees (void)
   callees = NULL;
   if (call_site_hash)
     {
-      htab_delete (call_site_hash);
+      call_site_hash->empty ();
       call_site_hash = NULL;
     }
 }
@@ -1597,35 +1725,38 @@ cgraph_node::remove_callers (void)
 void
 release_function_body (tree decl)
 {
-  if (DECL_STRUCT_FUNCTION (decl))
+  function *fn = DECL_STRUCT_FUNCTION (decl);
+  if (fn)
     {
-      push_cfun (DECL_STRUCT_FUNCTION (decl));
-      if (cfun->cfg
-         && current_loops)
-       {
-         cfun->curr_properties &= ~PROP_loops;
-         loop_optimizer_finalize ();
-       }
-      if (cfun->gimple_df)
-       {
-         delete_tree_ssa ();
-         delete_tree_cfg_annotations ();
-         cfun->eh = NULL;
-       }
-      if (cfun->cfg)
+      if (fn->cfg
+         || fn->gimple_df)
        {
-         gcc_assert (!dom_info_available_p (CDI_DOMINATORS));
-         gcc_assert (!dom_info_available_p (CDI_POST_DOMINATORS));
-         clear_edges ();
-         cfun->cfg = NULL;
+         if (fn->cfg
+             && loops_for_fn (fn))
+           {
+             fn->curr_properties &= ~PROP_loops;
+             loop_optimizer_finalize (fn);
+           }
+         if (fn->gimple_df)
+           {
+             delete_tree_ssa (fn);
+             delete_tree_cfg_annotations (fn);
+             fn->eh = NULL;
+           }
+         if (fn->cfg)
+           {
+             gcc_assert (!dom_info_available_p (fn, CDI_DOMINATORS));
+             gcc_assert (!dom_info_available_p (fn, CDI_POST_DOMINATORS));
+             clear_edges (fn);
+             fn->cfg = NULL;
+           }
+         if (fn->value_histograms)
+           free_histograms (fn);
        }
-      if (cfun->value_histograms)
-       free_histograms ();
-      pop_cfun ();
       gimple_set_body (decl, NULL);
       /* Struct function hangs a lot of data that would leak if we didn't
          removed all pointers to it.   */
-      ggc_free (DECL_STRUCT_FUNCTION (decl));
+      ggc_free (fn);
       DECL_STRUCT_FUNCTION (decl) = NULL;
     }
   DECL_SAVED_TREE (decl) = NULL;
@@ -1634,25 +1765,31 @@ release_function_body (tree decl)
 /* Release memory used to represent body of function.
    Use this only for functions that are released before being translated to
    target code (i.e. RTL).  Functions that are compiled to RTL and beyond
-   are free'd in final.c via free_after_compilation().  */
+   are free'd in final.c via free_after_compilation().
+   KEEP_ARGUMENTS are useful only if you want to rebuild body as thunk.  */
 
 void
-cgraph_node::release_body (void)
+cgraph_node::release_body (bool keep_arguments)
 {
   ipa_transforms_to_apply.release ();
   if (!used_as_abstract_origin && symtab->state != PARSING)
     {
       DECL_RESULT (decl) = NULL;
-      DECL_ARGUMENTS (decl) = NULL;
+
+      if (!keep_arguments)
+       DECL_ARGUMENTS (decl) = NULL;
     }
-  /* If the node is abstract and needed, then do not clear DECL_INITIAL
-     of its associated function function declaration because it's
+  /* If the node is abstract and needed, then do not clear
+     DECL_INITIAL of its associated function declaration because it's
      needed to emit debug info later.  */
   if (!used_as_abstract_origin && DECL_INITIAL (decl))
     DECL_INITIAL (decl) = error_mark_node;
   release_function_body (decl);
   if (lto_file_data)
-    lto_free_function_in_decl_state_for_node (this);
+    {
+      lto_free_function_in_decl_state_for_node (this);
+      lto_file_data = NULL;
+    }
 }
 
 /* Remove function from symbol table.  */
@@ -1730,21 +1867,32 @@ cgraph_node::remove (void)
       n = cgraph_node::get (decl);
       if (!n
          || (!n->clones && !n->clone_of && !n->global.inlined_to
-             && (symtab->global_info_ready
+             && ((symtab->global_info_ready || in_lto_p)
                  && (TREE_ASM_WRITTEN (n->decl)
                      || DECL_EXTERNAL (n->decl)
                      || !n->analyzed
                      || (!flag_wpa && n->in_other_partition)))))
        release_body ();
     }
+  else
+    {
+      lto_free_function_in_decl_state_for_node (this);
+      lto_file_data = NULL;
+    }
 
   decl = NULL;
   if (call_site_hash)
     {
-      htab_delete (call_site_hash);
+      call_site_hash->empty ();
       call_site_hash = NULL;
     }
 
+  if (instrumented_version)
+    {
+      instrumented_version->instrumented_version = NULL;
+      instrumented_version = NULL;
+    }
+
   symtab->release_symbol (this, uid);
 }
 
@@ -1781,20 +1929,7 @@ cgraph_node::local_info (tree decl)
   cgraph_node *node = get (decl);
   if (!node)
     return NULL;
-  return &node->local;
-}
-
-/* Return global info for the compiled function.  */
-
-cgraph_global_info *
-cgraph_node::global_info (tree decl)
-{
-  gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
-    && symtab->global_info_ready);
-  cgraph_node *node = get (decl);
-  if (!node)
-    return NULL;
-  return &node->global;
+  return &node->ultimate_alias_target ()->local;
 }
 
 /* Return local info for the compiled function.  */
@@ -1804,11 +1939,16 @@ cgraph_node::rtl_info (tree decl)
 {
   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
   cgraph_node *node = get (decl);
-  if (!node
-      || (decl != current_function_decl
-         && !TREE_ASM_WRITTEN (node->decl)))
+  if (!node)
+    return NULL;
+  node = node->ultimate_alias_target ();
+  if (node->decl != current_function_decl
+      && !TREE_ASM_WRITTEN (node->decl))
     return NULL;
-  return &node->rtl;
+  /* Allocate if it doesnt exist.  */
+  if (node->ultimate_alias_target ()->rtl == NULL)
+    node->ultimate_alias_target ()->rtl = ggc_cleared_alloc<cgraph_rtl_info> ();
+  return node->ultimate_alias_target ()->rtl;
 }
 
 /* Return a string describing the failure REASON.  */
@@ -1851,6 +1991,24 @@ cgraph_inline_failed_type (cgraph_inline_failed_t reason)
 const char * const cgraph_availability_names[] =
   {"unset", "not_available", "overwritable", "available", "local"};
 
+/* Output flags of edge to a file F.  */
+
+void
+cgraph_edge::dump_edge_flags (FILE *f)
+{
+  if (speculative)
+    fprintf (f, "(speculative) ");
+  if (!inline_failed)
+    fprintf (f, "(inlined) ");
+  if (indirect_inlining_edge)
+    fprintf (f, "(indirect_inlining) ");
+  if (count)
+    fprintf (f, "(%" PRId64"x) ", (int64_t)count);
+  if (frequency)
+    fprintf (f, "(%.2f per call) ", frequency / (double)CGRAPH_FREQ_BASE);
+  if (can_throw_external)
+    fprintf (f, "(can throw external) ");
+}
 
 /* Dump call graph node to file F.  */
 
@@ -1858,15 +2016,14 @@ void
 cgraph_node::dump (FILE *f)
 {
   cgraph_edge *edge;
-  int indirect_calls_count = 0;
 
   dump_base (f);
 
   if (global.inlined_to)
     fprintf (f, "  Function %s/%i is inline copy in %s/%i\n",
-            xstrdup (name ()),
+            xstrdup_for_dump (name ()),
             order,
-            xstrdup (global.inlined_to->name ()),
+            xstrdup_for_dump (global.inlined_to->name ()),
             global.inlined_to->order);
   if (clone_of)
     fprintf (f, "  Clone of %s/%i\n",
@@ -1882,7 +2039,7 @@ cgraph_node::dump (FILE *f)
   fprintf (f, "  First run: %i\n", tp_first_run);
   fprintf (f, "  Function flags:");
   if (count)
-    fprintf (f, " executed %"PRId64"x",
+    fprintf (f, " executed %" PRId64"x",
             (int64_t)count);
   if (origin)
     fprintf (f, " nested in: %s", origin->asm_name ());
@@ -1900,10 +2057,30 @@ cgraph_node::dump (FILE *f)
     fprintf (f, " only_called_at_exit");
   if (tm_clone)
     fprintf (f, " tm_clone");
+  if (icf_merged)
+    fprintf (f, " icf_merged");
+  if (merged_comdat)
+    fprintf (f, " merged_comdat");
+  if (nonfreeing_fn)
+    fprintf (f, " nonfreeing_fn");
   if (DECL_STATIC_CONSTRUCTOR (decl))
     fprintf (f," static_constructor (priority:%i)", get_init_priority ());
   if (DECL_STATIC_DESTRUCTOR (decl))
     fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
+  if (frequency == NODE_FREQUENCY_HOT)
+    fprintf (f, " hot");
+  if (frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
+    fprintf (f, " unlikely_executed");
+  if (frequency == NODE_FREQUENCY_EXECUTED_ONCE)
+    fprintf (f, " executed_once");
+  if (only_called_at_startup)
+    fprintf (f, " only_called_at_startup");
+  if (only_called_at_exit)
+    fprintf (f, " only_called_at_exit");
+  if (opt_for_fn (decl, optimize_size))
+    fprintf (f, " optimize_size");
+  if (parallelized_function)
+    fprintf (f, " parallelized_function");
 
   fprintf (f, "\n");
 
@@ -1937,20 +2114,7 @@ cgraph_node::dump (FILE *f)
     {
       fprintf (f, "%s/%i ", edge->caller->asm_name (),
               edge->caller->order);
-      if (count)
-       fprintf (f, "(%"PRId64"x) ",
-                (int64_t)count);
-      if (frequency)
-       fprintf (f, "(%.2f per call) ",
-                frequency / (double)CGRAPH_FREQ_BASE);
-      if (edge->speculative)
-       fprintf (f, "(speculative) ");
-      if (!edge->inline_failed)
-       fprintf (f, "(inlined) ");
-      if (edge->indirect_inlining_edge)
-       fprintf (f, "(indirect_inlining) ");
-      if (edge->can_throw_external)
-       fprintf (f, "(can throw external) ");
+      edge->dump_edge_flags (f);
     }
 
   fprintf (f, "\n  Calls: ");
@@ -1958,28 +2122,41 @@ cgraph_node::dump (FILE *f)
     {
       fprintf (f, "%s/%i ", edge->callee->asm_name (),
               edge->callee->order);
-      if (edge->speculative)
-       fprintf (f, "(speculative) ");
-      if (!edge->inline_failed)
-       fprintf (f, "(inlined) ");
-      if (edge->indirect_inlining_edge)
-       fprintf (f, "(indirect_inlining) ");
-      if (edge->count)
-       fprintf (f, "(%"PRId64"x) ",
-                (int64_t)count);
-      if (edge->frequency)
-       fprintf (f, "(%.2f per call) ",
-                frequency / (double)CGRAPH_FREQ_BASE);
-      if (edge->can_throw_external)
-       fprintf (f, "(can throw external) ");
+      edge->dump_edge_flags (f);
     }
   fprintf (f, "\n");
 
   for (edge = indirect_calls; edge; edge = edge->next_callee)
-    indirect_calls_count++;
-  if (indirect_calls_count)
-    fprintf (f, "  Has %i outgoing edges for indirect calls.\n",
-            indirect_calls_count);
+    {
+      if (edge->indirect_info->polymorphic)
+       {
+          fprintf (f, "   Polymorphic indirect call of type ");
+         print_generic_expr (f, edge->indirect_info->otr_type, TDF_SLIM);
+         fprintf (f, " token:%i", (int) edge->indirect_info->otr_token);
+       }
+      else
+        fprintf (f, "   Indirect call");
+      edge->dump_edge_flags (f);
+      if (edge->indirect_info->param_index != -1)
+       {
+         fprintf (f, " of param:%i", edge->indirect_info->param_index);
+         if (edge->indirect_info->agg_contents)
+          fprintf (f, " loaded from %s %s at offset %i",
+                   edge->indirect_info->member_ptr ? "member ptr" : "aggregate",
+                   edge->indirect_info->by_ref ? "passed by reference":"",
+                   (int)edge->indirect_info->offset);
+         if (edge->indirect_info->vptr_changed)
+           fprintf (f, " (vptr maybe changed)");
+       }
+      fprintf (f, "\n");
+      if (edge->indirect_info->polymorphic)
+       edge->indirect_info->context.dump (f);
+    }
+
+  if (instrumentation_clone)
+    fprintf (f, "  Is instrumented version.\n");
+  else if (instrumented_version)
+    fprintf (f, "  Has instrumented version.\n");
 }
 
 /* Dump call graph node NODE to stderr.  */
@@ -2035,7 +2212,7 @@ cgraph_node::get_availability (void)
     avail = AVAIL_NOT_AVAILABLE;
   else if (local.local)
     avail = AVAIL_LOCAL;
-  else if (alias && weakref)
+  else if (transparent_alias)
     ultimate_alias_target (&avail);
   else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
     avail = AVAIL_INTERPOSABLE;
@@ -2088,65 +2265,43 @@ cgraph_node::can_be_local_p (void)
                                                NULL, true));
 }
 
-/* Call calback on cgraph_node, thunks and aliases associated to cgraph_node.
+/* Call callback on cgraph_node, thunks and aliases associated to cgraph_node.
    When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
-   skipped. */
-
+   skipped.  When EXCLUDE_VIRTUAL_THUNKS is true, virtual thunks are
+   skipped.  */
 bool
 cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
                                                   (cgraph_node *, void *),
                                                 void *data,
-                                                bool include_overwritable)
+                                                bool include_overwritable,
+                                                bool exclude_virtual_thunks)
 {
   cgraph_edge *e;
   ipa_ref *ref;
 
   if (callback (this, data))
     return true;
-  for (e = callers; e; e = e->next_caller)
-    if (e->caller->thunk.thunk_p
-       && (include_overwritable
-           || e->caller->get_availability () > AVAIL_INTERPOSABLE))
-      if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
-                                                      include_overwritable))
-       return true;
-
   FOR_EACH_ALIAS (this, ref)
     {
       cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
       if (include_overwritable
          || alias->get_availability () > AVAIL_INTERPOSABLE)
        if (alias->call_for_symbol_thunks_and_aliases (callback, data,
-                                                    include_overwritable))
+                                                    include_overwritable,
+                                                    exclude_virtual_thunks))
          return true;
     }
-  return false;
-}
-
-/* Call calback on function and aliases associated to the function.
-   When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
-   skipped. */
-
-bool
-cgraph_node::call_for_symbol_and_aliases (bool (*callback) (cgraph_node *,
-                                                           void *),
-                                         void *data,
-                                         bool include_overwritable)
-{
-  ipa_ref *ref;
-
-  if (callback (this, data))
-    return true;
+  for (e = callers; e; e = e->next_caller)
+    if (e->caller->thunk.thunk_p
+       && (include_overwritable
+           || e->caller->get_availability () > AVAIL_INTERPOSABLE)
+       && !(exclude_virtual_thunks
+            && e->caller->thunk.virtual_offset_p))
+      if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
+                                                      include_overwritable,
+                                                      exclude_virtual_thunks))
+       return true;
 
-  FOR_EACH_ALIAS (this, ref)
-    {
-      cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
-      if (include_overwritable
-         || alias->get_availability () > AVAIL_INTERPOSABLE)
-       if (alias->call_for_symbol_and_aliases (callback, data,
-                                               include_overwritable))
-         return true;
-    }
   return false;
 }
 
@@ -2165,8 +2320,9 @@ cgraph_node::make_local (cgraph_node *node, void *)
       node->forced_by_abi = false;
       node->local.local = true;
       node->set_section (NULL);
-      node->unique_name = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
-                                 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
+      node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
+                          || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
+                          && !flag_incremental_link);
       node->resolution = LDPR_PREVAILING_DEF_IRONLY;
       gcc_assert (node->get_availability () == AVAIL_LOCAL);
     }
@@ -2233,7 +2389,7 @@ cgraph_node::set_const_flag (bool readonly, bool looping)
 {
   call_for_symbol_thunks_and_aliases (cgraph_set_const_flag_1,
                                    (void *)(size_t)(readonly + (int)looping * 2),
-                                     false);
+                                   false, true);
 }
 
 /* Worker to set pure flag.  */
@@ -2263,7 +2419,7 @@ cgraph_node::set_pure_flag (bool pure, bool looping)
 {
   call_for_symbol_thunks_and_aliases (cgraph_set_pure_flag_1,
                                    (void *)(size_t)(pure + (int)looping * 2),
-                                   false);
+                                   false, true);
 }
 
 /* Return true when cgraph_node can not return or throw and thus
@@ -2273,7 +2429,7 @@ bool
 cgraph_node::cannot_return_p (void)
 {
   int flags = flags_from_decl_or_type (decl);
-  if (!flag_exceptions)
+  if (!opt_for_fn (decl, flag_exceptions))
     return (flags & ECF_NORETURN) != 0;
   else
     return ((flags & (ECF_NORETURN | ECF_NOTHROW))
@@ -2293,7 +2449,7 @@ cgraph_edge::cannot_lead_to_return_p (void)
   if (indirect_unknown_callee)
     {
       int flags = indirect_info->ecf_flags;
-      if (!flag_exceptions)
+      if (!opt_for_fn (caller->decl, flag_exceptions))
        return (flags & ECF_NORETURN) != 0;
       else
        return ((flags & (ECF_NORETURN | ECF_NOTHROW))
@@ -2303,28 +2459,38 @@ cgraph_edge::cannot_lead_to_return_p (void)
     return callee->cannot_return_p ();
 }
 
-/* Return true when function can be removed from callgraph
-   if all direct calls are eliminated.  */
+/* Return true if the call can be hot.  */
 
 bool
-cgraph_node::can_remove_if_no_direct_calls_and_refs_p (void)
+cgraph_edge::maybe_hot_p (void)
 {
-  gcc_assert (!global.inlined_to);
-  /* Extern inlines can always go, we will use the external definition.  */
-  if (DECL_EXTERNAL (decl))
-    return true;
-  /* When function is needed, we can not remove it.  */
-  if (force_output || used_from_other_partition)
+  /* TODO: Export profile_status from cfun->cfg to cgraph_node.  */
+  if (profile_info
+      && opt_for_fn (caller->decl, flag_branch_probabilities)
+      && !maybe_hot_count_p (NULL, count))
+    return false;
+  if (caller->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED
+      || (callee
+         && callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED))
     return false;
-  if (DECL_STATIC_CONSTRUCTOR (decl)
-      || DECL_STATIC_DESTRUCTOR (decl))
+  if (caller->frequency > NODE_FREQUENCY_UNLIKELY_EXECUTED
+      && (callee
+         && callee->frequency <= NODE_FREQUENCY_EXECUTED_ONCE))
     return false;
-  /* Only COMDAT functions can be removed if externally visible.  */
-  if (externally_visible
-      && (!DECL_COMDAT (decl)
-         || forced_by_abi
-         || used_from_object_file_p ()))
+  if (opt_for_fn (caller->decl, optimize_size))
     return false;
+  if (caller->frequency == NODE_FREQUENCY_HOT)
+    return true;
+  if (caller->frequency == NODE_FREQUENCY_EXECUTED_ONCE
+      && frequency < CGRAPH_FREQ_BASE * 3 / 2)
+    return false;
+  if (opt_for_fn (caller->decl, flag_guess_branch_prob))
+    {
+      if (PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION) == 0
+         || frequency <= (CGRAPH_FREQ_BASE
+                          / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION)))
+        return false;
+    }
   return true;
 }
 
@@ -2336,18 +2502,64 @@ nonremovable_p (cgraph_node *node, void *)
   return !node->can_remove_if_no_direct_calls_and_refs_p ();
 }
 
-/* Return true when function cgraph_node and its aliases can be removed from
-   callgraph if all direct calls are eliminated.  */
+/* Return true if whole comdat group can be removed if there are no direct
+   calls to THIS.  */
 
 bool
-cgraph_node::can_remove_if_no_direct_calls_p (void)
+cgraph_node::can_remove_if_no_direct_calls_p (bool will_inline)
 {
-  /* Extern inlines can always go, we will use the external definition.  */
-  if (DECL_EXTERNAL (decl))
-    return true;
-  if (address_taken)
+  struct ipa_ref *ref;
+
+  /* For local symbols or non-comdat group it is the same as 
+     can_remove_if_no_direct_calls_p.  */
+  if (!externally_visible || !same_comdat_group)
+    {
+      if (DECL_EXTERNAL (decl))
+       return true;
+      if (address_taken)
+       return false;
+      return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
+    }
+
+  if (will_inline && address_taken)
+    return false;
+
+  /* Otheriwse check if we can remove the symbol itself and then verify
+     that only uses of the comdat groups are direct call to THIS
+     or its aliases.   */
+  if (!can_remove_if_no_direct_calls_and_refs_p ())
     return false;
-  return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
+
+  /* Check that all refs come from within the comdat group.  */
+  for (int i = 0; iterate_referring (i, ref); i++)
+    if (ref->referring->get_comdat_group () != get_comdat_group ())
+      return false;
+
+  struct cgraph_node *target = ultimate_alias_target ();
+  for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
+       next != this; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
+    {
+      if (!externally_visible)
+       continue;
+      if (!next->alias
+         && !next->can_remove_if_no_direct_calls_and_refs_p ())
+       return false;
+
+      /* If we see different symbol than THIS, be sure to check calls.  */
+      if (next->ultimate_alias_target () != target)
+       for (cgraph_edge *e = next->callers; e; e = e->next_caller)
+         if (e->caller->get_comdat_group () != get_comdat_group ()
+             || will_inline)
+           return false;
+
+      /* If function is not being inlined, we care only about
+        references outside of the comdat group.  */
+      if (!will_inline)
+        for (int i = 0; next->iterate_referring (i, ref); i++)
+         if (ref->referring->get_comdat_group () != get_comdat_group ())
+           return false;
+    }
+  return true;
 }
 
 /* Return true when function cgraph_node can be expected to be removed
@@ -2365,21 +2577,49 @@ cgraph_node::can_remove_if_no_direct_calls_p (void)
    linkonce section.  */
 
 bool
-cgraph_node::will_be_removed_from_program_if_no_direct_calls_p (void)
+cgraph_node::will_be_removed_from_program_if_no_direct_calls_p
+        (bool will_inline)
 {
   gcc_assert (!global.inlined_to);
+  if (DECL_EXTERNAL (decl))
+    return true;
 
-  if (call_for_symbol_and_aliases (used_from_object_file_p_worker,
-                                  NULL, true))
-    return false;
   if (!in_lto_p && !flag_whole_program)
-    return only_called_directly_p ();
-  else
     {
-       if (DECL_EXTERNAL (decl))
-         return true;
-      return can_remove_if_no_direct_calls_p ();
+      /* If the symbol is in comdat group, we need to verify that whole comdat
+        group becomes unreachable.  Technically we could skip references from
+        within the group, too.  */
+      if (!only_called_directly_p ())
+       return false;
+      if (same_comdat_group && externally_visible)
+       {
+         struct cgraph_node *target = ultimate_alias_target ();
+
+         if (will_inline && address_taken)
+           return true;
+         for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
+              next != this;
+              next = dyn_cast<cgraph_node *> (next->same_comdat_group))
+           {
+             if (!externally_visible)
+               continue;
+             if (!next->alias
+                 && !next->only_called_directly_p ())
+               return false;
+
+             /* If we see different symbol than THIS,
+                be sure to check calls.  */
+             if (next->ultimate_alias_target () != target)
+               for (cgraph_edge *e = next->callers; e; e = e->next_caller)
+                 if (e->caller->get_comdat_group () != get_comdat_group ()
+                     || will_inline)
+                   return false;
+           }
+       }
+      return true;
     }
+  else
+    return can_remove_if_no_direct_calls_p (will_inline);
 }
 
 
@@ -2417,7 +2657,8 @@ collect_callers_of_node_1 (cgraph_node *node, void *data)
 
   if (avail > AVAIL_INTERPOSABLE)
     for (cs = node->callers; cs != NULL; cs = cs->next_caller)
-      if (!cs->indirect_inlining_edge)
+      if (!cs->indirect_inlining_edge
+         && !cs->caller->thunk.thunk_p)
         redirect_callers->safe_push (cs);
   return false;
 }
@@ -2472,52 +2713,33 @@ clone_of_p (cgraph_node *node, cgraph_node *node2)
   return node2 != NULL;
 }
 
-/* Verify edge count and frequency.  */
+/* Verify edge count and frequency.  */
 
-static bool
-verify_edge_count_and_frequency (cgraph_edge *e)
+bool
+cgraph_edge::verify_count_and_frequency ()
 {
   bool error_found = false;
-  if (e->count < 0)
+  if (count < 0)
     {
       error ("caller edge count is negative");
       error_found = true;
     }
-  if (e->frequency < 0)
+  if (frequency < 0)
     {
       error ("caller edge frequency is negative");
       error_found = true;
     }
-  if (e->frequency > CGRAPH_FREQ_MAX)
+  if (frequency > CGRAPH_FREQ_MAX)
     {
       error ("caller edge frequency is too large");
       error_found = true;
     }
-  if (gimple_has_body_p (e->caller->decl)
-      && !e->caller->global.inlined_to
-      && !e->speculative
-      /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
-        Remove this once edges are actually removed from the function at that time.  */
-      && (e->frequency
-         || (inline_edge_summary_vec.exists ()
-             && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
-                 || !inline_edge_summary (e)->predicate)))
-      && (e->frequency
-         != compute_call_stmt_bb_frequency (e->caller->decl,
-                                            gimple_bb (e->call_stmt))))
-    {
-      error ("caller edge frequency %i does not match BB frequency %i",
-            e->frequency,
-            compute_call_stmt_bb_frequency (e->caller->decl,
-                                            gimple_bb (e->call_stmt)));
-      error_found = true;
-    }
   return error_found;
 }
 
 /* Switch to THIS_CFUN if needed and print STMT to stderr.  */
 static void
-cgraph_debug_gimple_stmt (function *this_cfun, gimple stmt)
+cgraph_debug_gimple_stmt (function *this_cfun, gimple *stmt)
 {
   bool fndecl_was_null = false;
   /* debug_gimple_stmt needs correct cfun */
@@ -2534,15 +2756,15 @@ cgraph_debug_gimple_stmt (function *this_cfun, gimple stmt)
     current_function_decl = NULL;
 }
 
-/* Verify that call graph edge corresponds to DECL from the associated
+/* Verify that call graph edge corresponds to DECL from the associated
    statement.  Return true if the verification should fail.  */
 
-static bool
-verify_edge_corresponds_to_fndecl (cgraph_edge *e, tree decl)
+bool
+cgraph_edge::verify_corresponds_to_fndecl (tree decl)
 {
   cgraph_node *node;
 
-  if (!decl || e->callee->global.inlined_to)
+  if (!decl || callee->global.inlined_to)
     return false;
   if (symtab->state == LTO_STREAMING)
     return false;
@@ -2555,20 +2777,21 @@ verify_edge_corresponds_to_fndecl (cgraph_edge *e, tree decl)
   if (!node
       || node->body_removed
       || node->in_other_partition
-      || e->callee->in_other_partition)
+      || callee->icf_merged
+      || callee->in_other_partition)
     return false;
 
   node = node->ultimate_alias_target ();
 
   /* Optimizers can redirect unreachable calls or calls triggering undefined
      behaviour to builtin_unreachable.  */
-  if (DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
-      && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_UNREACHABLE)
+  if (DECL_BUILT_IN_CLASS (callee->decl) == BUILT_IN_NORMAL
+      && DECL_FUNCTION_CODE (callee->decl) == BUILT_IN_UNREACHABLE)
     return false;
 
-  if (e->callee->former_clone_of != node->decl
-      && (node != e->callee->ultimate_alias_target ())
-      && !clone_of_p (node, e->callee))
+  if (callee->former_clone_of != node->decl
+      && (node != callee->ultimate_alias_target ())
+      && !clone_of_p (node, callee))
     return true;
   else
     return false;
@@ -2648,7 +2871,7 @@ cgraph_node::verify_node (void)
   bool check_comdat = comdat_local_p ();
   for (e = callers; e; e = e->next_caller)
     {
-      if (verify_edge_count_and_frequency (e))
+      if (e->verify_count_and_frequency ())
        error_found = true;
       if (check_comdat
          && !in_same_comdat_group_p (e->caller))
@@ -2679,9 +2902,46 @@ cgraph_node::verify_node (void)
            error_found = true;
          }
     }
+  for (e = callees; e; e = e->next_callee)
+    {
+      if (e->verify_count_and_frequency ())
+       error_found = true;
+      if (gimple_has_body_p (e->caller->decl)
+         && !e->caller->global.inlined_to
+         && !e->speculative
+         /* Optimized out calls are redirected to __builtin_unreachable.  */
+         && (e->frequency
+             || e->callee->decl
+                != builtin_decl_implicit (BUILT_IN_UNREACHABLE))
+         && (e->frequency
+             != compute_call_stmt_bb_frequency (e->caller->decl,
+                                                gimple_bb (e->call_stmt))))
+       {
+         error ("caller edge frequency %i does not match BB frequency %i",
+                e->frequency,
+                compute_call_stmt_bb_frequency (e->caller->decl,
+                                                gimple_bb (e->call_stmt)));
+         error_found = true;
+       }
+    }
   for (e = indirect_calls; e; e = e->next_callee)
-    if (verify_edge_count_and_frequency (e))
-      error_found = true;
+    {
+      if (e->verify_count_and_frequency ())
+       error_found = true;
+      if (gimple_has_body_p (e->caller->decl)
+         && !e->caller->global.inlined_to
+         && !e->speculative
+         && (e->frequency
+             != compute_call_stmt_bb_frequency (e->caller->decl,
+                                                gimple_bb (e->call_stmt))))
+       {
+         error ("indirect call frequency %i does not match BB frequency %i",
+                e->frequency,
+                compute_call_stmt_bb_frequency (e->caller->decl,
+                                                gimple_bb (e->call_stmt)));
+         error_found = true;
+       }
+    }
   if (!callers && global.inlined_to)
     {
       error ("inlined_to pointer is set but no predecessors found");
@@ -2745,7 +3005,9 @@ cgraph_node::verify_node (void)
           error_found = true;
        }
       for (i = 0; iterate_reference (i, ref); i++)
-       if (ref->use != IPA_REF_ALIAS)
+       if (ref->use == IPA_REF_CHKP)
+         ;
+       else if (ref->use != IPA_REF_ALIAS)
          {
            error ("Alias has non-alias reference");
            error_found = true;
@@ -2757,12 +3019,84 @@ cgraph_node::verify_node (void)
          }
        else
          ref_found = true;
-       if (!ref_found)
+      if (!ref_found)
+       {
+         error ("Analyzed alias has no reference");
+         error_found = true;
+       }
+    }
+
+  /* Check instrumented version reference.  */
+  if (instrumented_version
+      && instrumented_version->instrumented_version != this)
+    {
+      error ("Instrumentation clone does not reference original node");
+      error_found = true;
+    }
+
+  /* Cannot have orig_decl for not instrumented nodes.  */
+  if (!instrumentation_clone && orig_decl)
+    {
+      error ("Not instrumented node has non-NULL original declaration");
+      error_found = true;
+    }
+
+  /* If original not instrumented node still exists then we may check
+     original declaration is set properly.  */
+  if (instrumented_version
+      && orig_decl
+      && orig_decl != instrumented_version->decl)
+    {
+      error ("Instrumented node has wrong original declaration");
+      error_found = true;
+    }
+
+  /* Check all nodes have chkp reference to their instrumented versions.  */
+  if (analyzed
+      && instrumented_version
+      && !instrumentation_clone)
+    {
+      bool ref_found = false;
+      int i;
+      struct ipa_ref *ref;
+
+      for (i = 0; iterate_reference (i, ref); i++)
+       if (ref->use == IPA_REF_CHKP)
          {
-           error ("Analyzed alias has no reference");
-           error_found = true;
+           if (ref_found)
+             {
+               error ("Node has more than one chkp reference");
+               error_found = true;
+             }
+           if (ref->referred != instrumented_version)
+             {
+               error ("Wrong node is referenced with chkp reference");
+               error_found = true;
+             }
+           ref_found = true;
          }
+
+      if (!ref_found)
+       {
+         error ("Analyzed node has no reference to instrumented version");
+         error_found = true;
+       }
     }
+
+  if (instrumentation_clone
+      && DECL_BUILT_IN_CLASS (decl) == NOT_BUILT_IN)
+    {
+      tree name = DECL_ASSEMBLER_NAME (decl);
+      tree orig_name = DECL_ASSEMBLER_NAME (orig_decl);
+
+      if (!IDENTIFIER_TRANSPARENT_ALIAS (name)
+         || TREE_CHAIN (name) != orig_name)
+       {
+         error ("Alias chain for instrumented node is broken");
+         error_found = true;
+       }
+    }
+
   if (analyzed && thunk.thunk_p)
     {
       if (!callees)
@@ -2780,6 +3114,12 @@ cgraph_node::verify_node (void)
          error ("Thunk is not supposed to have body");
           error_found = true;
         }
+      if (thunk.add_pointer_bounds_args
+         && !instrumented_version->semantically_equivalent_p (callees->callee))
+       {
+         error ("Instrumentation thunk has wrong edge callee");
+          error_found = true;
+       }
     }
   else if (analyzed && gimple_has_body_p (decl)
           && !TREE_ASM_WRITTEN (decl)
@@ -2788,7 +3128,7 @@ cgraph_node::verify_node (void)
     {
       if (this_cfun->cfg)
        {
-         hash_set<gimple> stmts;
+         hash_set<gimple *> stmts;
          int i;
          ipa_ref *ref = NULL;
 
@@ -2803,7 +3143,7 @@ cgraph_node::verify_node (void)
                   !gsi_end_p (gsi);
                   gsi_next (&gsi))
                {
-                 gimple stmt = gsi_stmt (gsi);
+                 gimple *stmt = gsi_stmt (gsi);
                  stmts.add (stmt);
                  if (is_gimple_call (stmt))
                    {
@@ -2819,7 +3159,7 @@ cgraph_node::verify_node (void)
                            }
                          if (!e->indirect_unknown_callee)
                            {
-                             if (verify_edge_corresponds_to_fndecl (e, decl))
+                             if (e->verify_corresponds_to_fndecl (decl))
                                {
                                  error ("edge points to wrong declaration:");
                                  debug_tree (e->callee->decl);
@@ -2905,30 +3245,52 @@ cgraph_node::verify_cgraph_nodes (void)
 }
 
 /* Walk the alias chain to return the function cgraph_node is alias of.
-   Walk through thunk, too.
+   Walk through thunks, too.
    When AVAILABILITY is non-NULL, get minimal availability in the chain.  */
 
 cgraph_node *
 cgraph_node::function_symbol (enum availability *availability)
 {
-  cgraph_node *node = this;
+  cgraph_node *node = ultimate_alias_target (availability);
 
-  do
+  while (node->thunk.thunk_p)
     {
+      node = node->callees->callee;
+      if (availability)
+       {
+         enum availability a;
+         a = node->get_availability ();
+         if (a < *availability)
+           *availability = a;
+       }
       node = node->ultimate_alias_target (availability);
-      if (node->thunk.thunk_p)
+    }
+  return node;
+}
+
+/* Walk the alias chain to return the function cgraph_node is alias of.
+   Walk through non virtual thunks, too.  Thus we return either a function
+   or a virtual thunk node.
+   When AVAILABILITY is non-NULL, get minimal availability in the chain.  */
+
+cgraph_node *
+cgraph_node::function_or_virtual_thunk_symbol
+                               (enum availability *availability)
+{
+  cgraph_node *node = ultimate_alias_target (availability);
+
+  while (node->thunk.thunk_p && !node->thunk.virtual_offset_p)
+    {
+      node = node->callees->callee;
+      if (availability)
        {
-         node = node->callees->callee;
-         if (availability)
-           {
-             enum availability a;
-             a = node->get_availability ();
-             if (a < *availability)
-               *availability = a;
-           }
-         node = node->ultimate_alias_target (availability);
+         enum availability a;
+         a = node->get_availability ();
+         if (a < *availability)
+           *availability = a;
        }
-    } while (node && node->thunk.thunk_p);
+      node = node->ultimate_alias_target (availability);
+    }
   return node;
 }
 
@@ -2936,7 +3298,7 @@ cgraph_node::function_symbol (enum availability *availability)
    present.  */
 
 bool
-cgraph_node::get_body (void)
+cgraph_node::get_untransformed_body (void)
 {
   lto_file_decl_data *file_data;
   const char *data, *name;
@@ -2955,11 +3317,13 @@ cgraph_node::get_body (void)
 
   /* We may have renamed the declaration, e.g., a static function.  */
   name = lto_get_decl_name_mapping (file_data, name);
+  struct lto_in_decl_state *decl_state
+        = lto_get_function_in_decl_state (file_data, decl);
 
   data = lto_get_section_data (file_data, LTO_section_function_body,
-                              name, &len);
+                              name, &len, decl_state->compressed);
   if (!data)
-    fatal_error ("%s: section %s is missing",
+    fatal_error (input_location, "%s: section %s is missing",
                 file_data->file_name,
                 name);
 
@@ -2968,20 +3332,77 @@ cgraph_node::get_body (void)
   lto_input_function_body (file_data, this, data);
   lto_stats.num_function_bodies++;
   lto_free_section_data (file_data, LTO_section_function_body, name,
-                        data, len);
+                        data, len, decl_state->compressed);
   lto_free_function_in_decl_state_for_node (this);
+  /* Keep lto file data so ipa-inline-analysis knows about cross module
+     inlining.  */
 
   timevar_pop (TV_IPA_LTO_GIMPLE_IN);
 
   return true;
 }
 
+/* Prepare function body.  When doing LTO, read cgraph_node's body from disk 
+   if it is not already present.  When some IPA transformations are scheduled,
+   apply them.  */
+
+bool
+cgraph_node::get_body (void)
+{
+  bool updated;
+
+  updated = get_untransformed_body ();
+
+  /* Getting transformed body makes no sense for inline clones;
+     we should never use this on real clones becuase they are materialized
+     early.
+     TODO: Materializing clones here will likely lead to smaller LTRANS
+     footprint. */
+  gcc_assert (!global.inlined_to && !clone_of);
+  if (ipa_transforms_to_apply.exists ())
+    {
+      opt_pass *saved_current_pass = current_pass;
+      FILE *saved_dump_file = dump_file;
+      int saved_dump_flags = dump_flags;
+
+      push_cfun (DECL_STRUCT_FUNCTION (decl));
+      execute_all_ipa_transforms ();
+      cgraph_edge::rebuild_edges ();
+      free_dominance_info (CDI_DOMINATORS);
+      free_dominance_info (CDI_POST_DOMINATORS);
+      pop_cfun ();
+      updated = true;
+
+      current_pass = saved_current_pass;
+      dump_file = saved_dump_file;
+      dump_flags = saved_dump_flags;
+    }
+  return updated;
+}
+
+/* Return the DECL_STRUCT_FUNCTION of the function.  */
+
+struct function *
+cgraph_node::get_fun (void)
+{
+  cgraph_node *node = this;
+  struct function *fun = DECL_STRUCT_FUNCTION (node->decl);
+
+  while (!fun && node->clone_of)
+    {
+      node = node->clone_of;
+      fun = DECL_STRUCT_FUNCTION (node->decl);
+    }
+
+  return fun;
+}
+
 /* Verify if the type of the argument matches that of the function
    declaration.  If we cannot verify this or there is a mismatch,
    return false.  */
 
 static bool
-gimple_check_call_args (gimple stmt, tree fndecl, bool args_count_match)
+gimple_check_call_args (gimple *stmt, tree fndecl, bool args_count_match)
 {
   tree parms, p;
   unsigned int i, nargs;
@@ -3056,7 +3477,7 @@ gimple_check_call_args (gimple stmt, tree fndecl, bool args_count_match)
    If we cannot verify this or there is a mismatch, return false.  */
 
 bool
-gimple_check_call_matching_types (gimple call_stmt, tree callee,
+gimple_check_call_matching_types (gimple *call_stmt, tree callee,
                                  bool args_count_match)
 {
   tree lhs;
@@ -3072,4 +3493,50 @@ gimple_check_call_matching_types (gimple call_stmt, tree callee,
   return true;
 }
 
+/* Reset all state within cgraph.c so that we can rerun the compiler
+   within the same process.  For use by toplev::finalize.  */
+
+void
+cgraph_c_finalize (void)
+{
+  symtab = NULL;
+
+  x_cgraph_nodes_queue = NULL;
+
+  cgraph_fnver_htab = NULL;
+  version_info_node = NULL;
+}
+
+/* A wroker for call_for_symbol_and_aliases.  */
+
+bool
+cgraph_node::call_for_symbol_and_aliases_1 (bool (*callback) (cgraph_node *,
+                                                             void *),
+                                           void *data,
+                                           bool include_overwritable)
+{
+  ipa_ref *ref;
+  FOR_EACH_ALIAS (this, ref)
+    {
+      cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
+      if (include_overwritable
+         || alias->get_availability () > AVAIL_INTERPOSABLE)
+       if (alias->call_for_symbol_and_aliases (callback, data,
+                                               include_overwritable))
+         return true;
+    }
+  return false;
+}
+
+/* Return true if NODE has thunk.  */
+
+bool
+cgraph_node::has_thunk_p (cgraph_node *node, void *)
+{
+  for (cgraph_edge *e = node->callers; e; e = e->next_caller)
+    if (e->caller->thunk.thunk_p)
+      return true;
+  return false;
+}
+
 #include "gt-cgraph.h"