re PR ipa/83983 (FAIL: g++.dg/lto/pr83121 (test for LTO warnings, pr83121_0.C line 8))
[gcc.git] / gcc / ipa-devirt.c
index a3feb8823ca26c6a7abe3ecd10d9e24703e6c0be..fae82072a7e90eb754bc96cca3684bbfdde20dac 100644 (file)
@@ -1,6 +1,6 @@
 /* Basic IPA utilities for type inheritance graph construction and
    devirtualization.
-   Copyright (C) 2013-2015 Free Software Foundation, Inc.
+   Copyright (C) 2013-2018 Free Software Foundation, Inc.
    Contributed by Jan Hubicka
 
 This file is part of GCC.
@@ -108,74 +108,43 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
-#include "tm.h"
-#include "hash-set.h"
-#include "machmode.h"
-#include "hash-map.h"
-#include "vec.h"
-#include "double-int.h"
-#include "input.h"
-#include "alias.h"
-#include "symtab.h"
-#include "wide-int.h"
-#include "inchash.h"
+#include "backend.h"
+#include "rtl.h"
 #include "tree.h"
+#include "gimple.h"
+#include "alloc-pool.h"
+#include "tree-pass.h"
+#include "cgraph.h"
+#include "lto-streamer.h"
 #include "fold-const.h"
 #include "print-tree.h"
 #include "calls.h"
-#include "predict.h"
-#include "basic-block.h"
-#include "is-a.h"
-#include "plugin-api.h"
-#include "hard-reg-set.h"
-#include "function.h"
-#include "ipa-ref.h"
-#include "cgraph.h"
-#include "hashtab.h"
-#include "rtl.h"
-#include "flags.h"
-#include "statistics.h"
-#include "real.h"
-#include "fixed-value.h"
-#include "insn-config.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-pass.h"
-#include "target.h"
-#include "hash-table.h"
-#include "tree-pretty-print.h"
 #include "ipa-utils.h"
-#include "tree-ssa-alias.h"
-#include "internal-fn.h"
 #include "gimple-fold.h"
-#include "gimple-expr.h"
-#include "gimple.h"
-#include "alloc-pool.h"
 #include "symbol-summary.h"
+#include "tree-vrp.h"
 #include "ipa-prop.h"
-#include "ipa-inline.h"
-#include "diagnostic.h"
-#include "tree-dfa.h"
+#include "ipa-fnsummary.h"
 #include "demangle.h"
 #include "dbgcnt.h"
 #include "gimple-pretty-print.h"
-#include "stor-layout.h"
 #include "intl.h"
+#include "stringpool.h"
+#include "attribs.h"
 
 /* Hash based set of pairs of types.  */
-typedef struct
+struct type_pair
 {
   tree first;
   tree second;
-} type_pair;
+};
 
-struct pair_traits : default_hashset_traits
+template <>
+struct default_hash_traits <type_pair>
+  : typed_noop_remove <type_pair>
 {
+  GTY((skip)) typedef type_pair value_type;
+  GTY((skip)) typedef type_pair compare_type;
   static hashval_t
   hash (type_pair p)
   {
@@ -204,7 +173,8 @@ struct pair_traits : default_hashset_traits
 };
 
 static bool odr_types_equivalent_p (tree, tree, bool, bool *,
-                                   hash_set<type_pair,pair_traits> *);
+                                   hash_set<type_pair> *,
+                                   location_t, location_t);
 
 static bool odr_violation_reported = false;
 
@@ -239,6 +209,8 @@ struct GTY(()) odr_type_d
   bool all_derivations_known;
   /* Did we report ODR violation here?  */
   bool odr_violated;
+  /* Set when virtual table without RTTI previaled table with.  */
+  bool rtti_broken;
 };
 
 /* Return TRUE if all derived types of T are known and thus
@@ -302,13 +274,12 @@ type_possibly_instantiated_p (tree t)
 /* Hash used to unify ODR types based on their mangled name and for anonymous
    namespace types.  */
 
-struct odr_name_hasher
+struct odr_name_hasher : pointer_hash <odr_type_d>
 {
-  typedef odr_type_d value_type;
-  typedef union tree_node compare_type;
-  static inline hashval_t hash (const value_type *);
-  static inline bool equal (const value_type *, const compare_type *);
-  static inline void remove (value_type *);
+  typedef union tree_node *compare_type;
+  static inline hashval_t hash (const odr_type_d *);
+  static inline bool equal (const odr_type_d *, const tree_node *);
+  static inline void remove (odr_type_d *);
 };
 
 /* Has used to unify ODR types based on their associated virtual table.
@@ -317,8 +288,8 @@ struct odr_name_hasher
 
 struct odr_vtable_hasher:odr_name_hasher
 {
-  static inline hashval_t hash (const value_type *);
-  static inline bool equal (const value_type *, const compare_type *);
+  static inline hashval_t hash (const odr_type_d *);
+  static inline bool equal (const odr_type_d *, const tree_node *);
 };
 
 /* Return type that was declared with T's name so that T is an
@@ -337,8 +308,7 @@ main_odr_variant (const_tree t)
 static bool
 can_be_name_hashed_p (tree t)
 {
-  return (!in_lto_p || type_in_anonymous_namespace_p (t)
-         || (TYPE_NAME (t) && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t))));
+  return (!in_lto_p || odr_type_p (t));
 }
 
 /* Hash type by its ODR name.  */
@@ -354,7 +324,7 @@ hash_odr_name (const_tree t)
     return htab_hash_pointer (t);
 
   /* Anonymous types are unique.  */
-  if (type_in_anonymous_namespace_p (t))
+  if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
     return htab_hash_pointer (t);
 
   gcc_checking_assert (TYPE_NAME (t)
@@ -365,7 +335,7 @@ hash_odr_name (const_tree t)
 /* Return the computed hashcode for ODR_TYPE.  */
 
 inline hashval_t
-odr_name_hasher::hash (const value_type *odr_type)
+odr_name_hasher::hash (const odr_type_d *odr_type)
 {
   return hash_odr_name (odr_type->type);
 }
@@ -377,7 +347,7 @@ can_be_vtable_hashed_p (tree t)
   if (TYPE_MAIN_VARIANT (t) != t)
     return false;
   /* Anonymous namespace types are always handled by name hash.  */
-  if (type_in_anonymous_namespace_p (t))
+  if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
     return false;
   return (TREE_CODE (t) == RECORD_TYPE
          && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
@@ -403,14 +373,14 @@ hash_odr_vtable (const_tree t)
       v = TREE_OPERAND (TREE_OPERAND (v, 0), 0);
     }
 
-  hstate.add_wide_int (IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (v)));
+  hstate.add_hwi (IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (v)));
   return hstate.end ();
 }
 
 /* Return the computed hashcode for ODR_TYPE.  */
 
 inline hashval_t
-odr_vtable_hasher::hash (const value_type *odr_type)
+odr_vtable_hasher::hash (const odr_type_d *odr_type)
 {
   return hash_odr_vtable (odr_type->type);
 }
@@ -427,7 +397,7 @@ odr_vtable_hasher::hash (const value_type *odr_type)
 
    When STRICT is true, we compare types by their names for purposes of
    ODR violation warnings.  When strict is false, we consider variants
-   equivalent, becuase it is all that matters for devirtualization machinery.
+   equivalent, because it is all that matters for devirtualization machinery.
 */
 
 bool
@@ -451,8 +421,8 @@ types_same_for_odr (const_tree type1, const_tree type2, bool strict)
 
   /* Check for anonymous namespaces. Those have !TREE_PUBLIC
      on the corresponding TYPE_STUB_DECL.  */
-  if (type_in_anonymous_namespace_p (type1)
-      || type_in_anonymous_namespace_p (type2))
+  if ((type_with_linkage_p (type1) && type_in_anonymous_namespace_p (type1))
+      || (type_with_linkage_p (type2) && type_in_anonymous_namespace_p (type2)))
     return false;
 
 
@@ -479,7 +449,7 @@ types_same_for_odr (const_tree type1, const_tree type2, bool strict)
        return false;
       if (TREE_CODE (type1) == RECORD_TYPE
          && (TYPE_BINFO (type1) == NULL_TREE)
-             != (TYPE_BINFO (type1) == NULL_TREE))
+             != (TYPE_BINFO (type2) == NULL_TREE))
        return false;
       if (TREE_CODE (type1) == RECORD_TYPE && TYPE_BINFO (type1)
          && (BINFO_VTABLE (TYPE_BINFO (type1)) == NULL_TREE)
@@ -524,7 +494,8 @@ bool
 types_odr_comparable (tree t1, tree t2, bool strict)
 {
   return (!in_lto_p
-         || (strict ? main_odr_variant (t1) == main_odr_variant (t2)
+         || (strict ? (main_odr_variant (t1) == main_odr_variant (t2)
+                       && main_odr_variant (t1))
              : TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
          || (odr_type_p (t1) && odr_type_p (t2))
          || (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE
@@ -545,11 +516,64 @@ types_must_be_same_for_odr (tree t1, tree t2)
     return TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2);
 }
 
+/* If T is compound type, return type it is based on.  */
+
+static tree
+compound_type_base (const_tree t)
+{
+  if (TREE_CODE (t) == ARRAY_TYPE
+      || POINTER_TYPE_P (t)
+      || TREE_CODE (t) == COMPLEX_TYPE
+      || VECTOR_TYPE_P (t))
+    return TREE_TYPE (t);
+  if (TREE_CODE (t) == METHOD_TYPE)
+    return TYPE_METHOD_BASETYPE (t);
+  if (TREE_CODE (t) == OFFSET_TYPE)
+    return TYPE_OFFSET_BASETYPE (t);
+  return NULL_TREE;
+}
+
+/* Return true if T is either ODR type or compound type based from it.
+   If the function return true, we know that T is a type originating from C++
+   source even at link-time.  */
+
+bool
+odr_or_derived_type_p (const_tree t)
+{
+  do
+    {
+      if (odr_type_p (t))
+       return true;
+      /* Function type is a tricky one. Basically we can consider it
+        ODR derived if return type or any of the parameters is.
+        We need to check all parameters because LTO streaming merges
+        common types (such as void) and they are not considered ODR then.  */
+      if (TREE_CODE (t) == FUNCTION_TYPE)
+       {
+         if (TYPE_METHOD_BASETYPE (t))
+           t = TYPE_METHOD_BASETYPE (t);
+         else
+          {
+            if (TREE_TYPE (t) && odr_or_derived_type_p (TREE_TYPE (t)))
+              return true;
+            for (t = TYPE_ARG_TYPES (t); t; t = TREE_CHAIN (t))
+              if (odr_or_derived_type_p (TREE_VALUE (t)))
+                return true;
+            return false;
+          }
+       }
+      else
+       t = compound_type_base (t);
+    }
+  while (t);
+  return t;
+}
+
 /* Compare types T1 and T2 and return true if they are
    equivalent.  */
 
 inline bool
-odr_name_hasher::equal (const value_type *o1, const compare_type *t2)
+odr_name_hasher::equal (const odr_type_d *o1, const tree_node *t2)
 {
   tree t1 = o1->type;
 
@@ -561,8 +585,8 @@ odr_name_hasher::equal (const value_type *o1, const compare_type *t2)
     return false;
   /* Check for anonymous namespaces. Those have !TREE_PUBLIC
      on the corresponding TYPE_STUB_DECL.  */
-  if (type_in_anonymous_namespace_p (t1)
-      || type_in_anonymous_namespace_p (t2))
+  if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
+      || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
     return false;
   gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1)));
   gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
@@ -574,7 +598,7 @@ odr_name_hasher::equal (const value_type *o1, const compare_type *t2)
    equivalent.  */
 
 inline bool
-odr_vtable_hasher::equal (const value_type *o1, const compare_type *t2)
+odr_vtable_hasher::equal (const odr_type_d *o1, const tree_node *t2)
 {
   tree t1 = o1->type;
 
@@ -598,7 +622,7 @@ odr_vtable_hasher::equal (const value_type *o1, const compare_type *t2)
 /* Free ODR type V.  */
 
 inline void
-odr_name_hasher::remove (value_type *v)
+odr_name_hasher::remove (odr_type_d *v)
 {
   v->bases.release ();
   v->derived_types.release ();
@@ -636,9 +660,9 @@ set_type_binfo (tree type, tree binfo)
 
 static bool
 odr_subtypes_equivalent_p (tree t1, tree t2,
-                          hash_set<type_pair,pair_traits> *visited)
+                          hash_set<type_pair> *visited,
+                          location_t loc1, location_t loc2)
 {
-  bool an1, an2;
 
   /* This can happen in incomplete types that should be handled earlier.  */
   gcc_assert (t1 && t2);
@@ -649,9 +673,8 @@ odr_subtypes_equivalent_p (tree t1, tree t2,
     return true;
 
   /* Anonymous namespace types must match exactly.  */
-  an1 = type_in_anonymous_namespace_p (t1);
-  an2 = type_in_anonymous_namespace_p (t2);
-  if (an1 != an2 || an1)
+  if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
+      || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
     return false;
 
   /* For ODR types be sure to compare their names.
@@ -661,9 +684,14 @@ odr_subtypes_equivalent_p (tree t1, tree t2,
     {
       if (!types_same_for_odr (t1, t2, true))
         return false;
-      /* Limit recursion: If subtypes are ODR types and we know
-         that they are same, be happy.  */
-      if (!get_odr_type (t1, true)->odr_violated)
+      /* Limit recursion: if subtypes are ODR types and we know that they are
+        same, be happy.  We need to call get_odr_type on both subtypes since
+        we don't know which among t1 and t2 defines the common ODR type and
+        therefore which call will report the ODR violation, if any.  */
+        if (!odr_type_p (t1)
+            || !odr_type_p (t2)
+            || (!get_odr_type (t1, true)->odr_violated
+                && !get_odr_type (t2, true)->odr_violated))
         return true;
     }
 
@@ -671,9 +699,8 @@ odr_subtypes_equivalent_p (tree t1, tree t2,
      have to be compared structurally.  */
   if (TREE_CODE (t1) != TREE_CODE (t2))
     return false;
-  if ((TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
-    return false;
-  if (TYPE_NAME (t1) && DECL_NAME (TYPE_NAME (t1)) != DECL_NAME (TYPE_NAME (t2)))
+  if (AGGREGATE_TYPE_P (t1)
+      && (TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
     return false;
 
   type_pair pair={t1,t2};
@@ -684,7 +711,30 @@ odr_subtypes_equivalent_p (tree t1, tree t2,
     }
   if (visited->add (pair))
     return true;
-  return odr_types_equivalent_p (t1, t2, false, NULL, visited);
+  return odr_types_equivalent_p (t1, t2, false, NULL, visited, loc1, loc2);
+}
+
+/* Return true if DECL1 and DECL2 are identical methods.  Consider
+   name equivalent to name.localalias.xyz.  */
+
+static bool
+methods_equal_p (tree decl1, tree decl2)
+{
+  if (DECL_ASSEMBLER_NAME (decl1) == DECL_ASSEMBLER_NAME (decl2))
+    return true;
+  const char sep = symbol_table::symbol_suffix_separator ();
+
+  const char *name1 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl1));
+  const char *ptr1 = strchr (name1, sep);
+  int len1 = ptr1 ? ptr1 - name1 : strlen (name1);
+
+  const char *name2 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl2));
+  const char *ptr2 = strchr (name2, sep);
+  int len2 = ptr2 ? ptr2 - name2 : strlen (name2);
+
+  if (len1 != len2)
+    return false;
+  return !strncmp (name1, name2, len1);
 }
 
 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
@@ -694,6 +744,7 @@ void
 compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
 {
   int n1, n2;
+
   if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl))
     {
       odr_violation_reported = true;
@@ -715,6 +766,17 @@ compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
     }
   if (!prevailing->definition || !vtable->definition)
     return;
+
+  /* If we do not stream ODR type info, do not bother to do useful compare.  */
+  if (!TYPE_BINFO (DECL_CONTEXT (vtable->decl))
+      || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable->decl))))
+    return;
+
+  odr_type class_type = get_odr_type (DECL_CONTEXT (vtable->decl), true);
+
+  if (class_type->odr_violated)
+    return;
+
   for (n1 = 0, n2 = 0; true; n1++, n2++)
     {
       struct ipa_ref *ref1, *ref2;
@@ -728,15 +790,18 @@ compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
         accept the other case.  */
       while (!end2
             && (end1
-                || (DECL_ASSEMBLER_NAME (ref1->referred->decl)
-                    != DECL_ASSEMBLER_NAME (ref2->referred->decl)
-                    && DECL_VIRTUAL_P (ref1->referred->decl)))
-            && !DECL_VIRTUAL_P (ref2->referred->decl))
+                || (methods_equal_p (ref1->referred->decl,
+                                     ref2->referred->decl)
+                    && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL))
+            && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
        {
-         if (warning_at (DECL_SOURCE_LOCATION
-                           (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 0,
-                         "virtual table of type %qD contains RTTI information",
-                         DECL_CONTEXT (vtable->decl)))
+         if (!class_type->rtti_broken
+             && warning_at (DECL_SOURCE_LOCATION
+                             (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
+                            OPT_Wodr,
+                            "virtual table of type %qD contains RTTI "
+                            "information",
+                            DECL_CONTEXT (vtable->decl)))
            {
              inform (DECL_SOURCE_LOCATION
                        (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
@@ -745,19 +810,19 @@ compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
              inform (DECL_SOURCE_LOCATION
                        (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
                      "RTTI will not work on this type");
+             class_type->rtti_broken = true;
            }
          n2++;
           end2 = !vtable->iterate_reference (n2, ref2);
        }
       while (!end1
             && (end2
-                || (DECL_ASSEMBLER_NAME (ref2->referred->decl)
-                    != DECL_ASSEMBLER_NAME (ref1->referred->decl)
-                    && DECL_VIRTUAL_P (ref2->referred->decl)))
-            && !DECL_VIRTUAL_P (ref1->referred->decl))
+                || (methods_equal_p (ref2->referred->decl, ref1->referred->decl)
+                    && TREE_CODE (ref2->referred->decl) == FUNCTION_DECL))
+            && TREE_CODE (ref1->referred->decl) != FUNCTION_DECL)
        {
          n1++;
-          end1 = !vtable->iterate_reference (n1, ref1);
+          end1 = !prevailing->iterate_reference (n1, ref1);
        }
 
       /* Finished?  */
@@ -770,8 +835,10 @@ compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
             is not output too often.  */
          if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl))
            {
+             class_type->odr_violated = true;
              if (warning_at (DECL_SOURCE_LOCATION
-                               (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 0,
+                               (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
+                             OPT_Wodr,
                              "virtual table of type %qD violates "
                              "one definition rule  ",
                              DECL_CONTEXT (vtable->decl)))
@@ -787,17 +854,19 @@ compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
 
       if (!end1 && !end2)
        {
-         if (DECL_ASSEMBLER_NAME (ref1->referred->decl)
-             == DECL_ASSEMBLER_NAME (ref2->referred->decl))
+         if (methods_equal_p (ref1->referred->decl, ref2->referred->decl))
            continue;
 
+         class_type->odr_violated = true;
+
          /* If the loops above stopped on non-virtual pointer, we have
             mismatch in RTTI information mangling.  */
-         if (!DECL_VIRTUAL_P (ref1->referred->decl)
-             && !DECL_VIRTUAL_P (ref2->referred->decl))
+         if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL
+             && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
            {
              if (warning_at (DECL_SOURCE_LOCATION
-                               (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 0,
+                               (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
+                             OPT_Wodr,
                              "virtual table of type %qD violates "
                              "one definition rule  ",
                              DECL_CONTEXT (vtable->decl)))
@@ -813,22 +882,18 @@ compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
             or virtual method.  If one points to virtual table and other to
             method we can complain the same way as if one table was shorter
             than other pointing out the extra method.  */
-         gcc_assert (DECL_VIRTUAL_P (ref1->referred->decl)
-                     && (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL
-                         || TREE_CODE (ref1->referred->decl) == VAR_DECL));
-         gcc_assert (DECL_VIRTUAL_P (ref2->referred->decl)
-                     && (TREE_CODE (ref2->referred->decl) == FUNCTION_DECL
-                         || TREE_CODE (ref2->referred->decl) == VAR_DECL));
          if (TREE_CODE (ref1->referred->decl)
              != TREE_CODE (ref2->referred->decl))
            {
-             if (TREE_CODE (ref1->referred->decl) == VAR_DECL)
+             if (VAR_P (ref1->referred->decl))
                end1 = true;
-             else if (TREE_CODE (ref2->referred->decl) == VAR_DECL)
+             else if (VAR_P (ref2->referred->decl))
                end2 = true;
            }
        }
 
+      class_type->odr_violated = true;
+
       /* Complain about size mismatch.  Either we have too many virutal
         functions or too many virtual table pointers.  */
       if (end1 || end2)
@@ -841,7 +906,8 @@ compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
              ref1 = ref2;
            }
          if (warning_at (DECL_SOURCE_LOCATION
-                           (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 0,
+                           (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
+                         OPT_Wodr,
                          "virtual table of type %qD violates "
                          "one definition rule",
                          DECL_CONTEXT (vtable->decl)))
@@ -862,7 +928,7 @@ compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
                  inform (DECL_SOURCE_LOCATION
                           (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
                          "the conflicting type defined in another translation "
-                         "unit has virtual table table with more entries");
+                         "unit has virtual table with more entries");
                }
            }
          return;
@@ -871,7 +937,7 @@ compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
       /* And in the last case we have either mistmatch in between two virtual
         methods or two virtual table pointers.  */
       if (warning_at (DECL_SOURCE_LOCATION
-                       (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 0,
+                       (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr,
                      "virtual table of type %qD violates "
                      "one definition rule  ",
                      DECL_CONTEXT (vtable->decl)))
@@ -884,17 +950,20 @@ compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
                      "unit");
              gcc_assert (TREE_CODE (ref2->referred->decl)
                          == FUNCTION_DECL);
-             inform (DECL_SOURCE_LOCATION (ref1->referred->decl),
-                     "virtual method %qD", ref1->referred->decl);
-             inform (DECL_SOURCE_LOCATION (ref2->referred->decl),
+             inform (DECL_SOURCE_LOCATION
+                        (ref1->referred->ultimate_alias_target ()->decl),
+                     "virtual method %qD",
+                     ref1->referred->ultimate_alias_target ()->decl);
+             inform (DECL_SOURCE_LOCATION
+                        (ref2->referred->ultimate_alias_target ()->decl),
                      "ought to match virtual method %qD but does not",
-                     ref2->referred->decl);
+                     ref2->referred->ultimate_alias_target ()->decl);
            }
          else
            inform (DECL_SOURCE_LOCATION
                      (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
                    "the conflicting type defined in another translation "
-                   "unit has virtual table table with different contents");
+                   "unit has virtual table with different contents");
          return;
        }
     }
@@ -914,10 +983,16 @@ warn_odr (tree t1, tree t2, tree st1, tree st2,
   if (warned)
     *warned = false;
 
-  if (!warn)
+  if (!warn || !TYPE_NAME(t1))
     return;
+
+  /* ODR warnings are output druing LTO streaming; we must apply location
+     cache for potential warnings to be output correctly.  */
+  if (lto_location_cache::current_cache)
+    lto_location_cache::current_cache->apply_location_cache ();
+
   if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr,
-                  "type %qT violates one definition rule",
+                  "type %qT violates the C++ One Definition Rule",
                   t1))
     return;
   if (!st1 && !st2)
@@ -957,50 +1032,237 @@ warn_odr (tree t1, tree t2, tree st1, tree st2,
     *warned = true;
 }
 
-/* We already warned about ODR mismatch.  T1 and T2 ought to be equivalent
-   because they are used on same place in ODR matching types.
-   They are not; inform the user.  */
+/* Return ture if T1 and T2 are incompatible and we want to recusively
+   dive into them from warn_type_mismatch to give sensible answer.  */
 
-void
-warn_types_mismatch (tree t1, tree t2)
+static bool
+type_mismatch_p (tree t1, tree t2)
 {
+  if (odr_or_derived_type_p (t1) && odr_or_derived_type_p (t2)
+      && !odr_types_equivalent_p (t1, t2))
+    return true;
+  return !types_compatible_p (t1, t2);
+}
+
+
+/* Types T1 and T2 was found to be incompatible in a context they can't
+   (either used to declare a symbol of same assembler name or unified by
+   ODR rule).  We already output warning about this, but if possible, output
+   extra information on how the types mismatch.
+
+   This is hard to do in general.  We basically handle the common cases.
+
+   If LOC1 and LOC2 are meaningful locations, use it in the case the types
+   themselves do no thave one.*/
+
+void
+warn_types_mismatch (tree t1, tree t2, location_t loc1, location_t loc2)
+{
+  /* Location of type is known only if it has TYPE_NAME and the name is
+     TYPE_DECL.  */
+  location_t loc_t1 = TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
+                     ? DECL_SOURCE_LOCATION (TYPE_NAME (t1))
+                     : UNKNOWN_LOCATION;
+  location_t loc_t2 = TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
+                     ? DECL_SOURCE_LOCATION (TYPE_NAME (t2))
+                     : UNKNOWN_LOCATION;
+  bool loc_t2_useful = false;
+
+  /* With LTO it is a common case that the location of both types match.
+     See if T2 has a location that is different from T1. If so, we will
+     inform user about the location.
+     Do not consider the location passed to us in LOC1/LOC2 as those are
+     already output.  */
+  if (loc_t2 > BUILTINS_LOCATION && loc_t2 != loc_t1)
+    {
+      if (loc_t1 <= BUILTINS_LOCATION)
+       loc_t2_useful = true;
+      else
+       {
+         expanded_location xloc1 = expand_location (loc_t1);
+         expanded_location xloc2 = expand_location (loc_t2);
+
+         if (strcmp (xloc1.file, xloc2.file)
+             || xloc1.line != xloc2.line
+             || xloc1.column != xloc2.column)
+           loc_t2_useful = true;
+       }
+    }
+
+  if (loc_t1 <= BUILTINS_LOCATION)
+    loc_t1 = loc1;
+  if (loc_t2 <= BUILTINS_LOCATION)
+    loc_t2 = loc2;
+
+  location_t loc = loc_t1 <= BUILTINS_LOCATION ? loc_t2 : loc_t1;
+
+  /* It is a quite common bug to reference anonymous namespace type in
+     non-anonymous namespace class.  */
+  if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
+      || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
+    {
+      if (type_with_linkage_p (t1) && !type_in_anonymous_namespace_p (t1))
+       {
+         std::swap (t1, t2);
+         std::swap (loc_t1, loc_t2);
+       }
+      gcc_assert (TYPE_NAME (t1) && TYPE_NAME (t2)
+                 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
+                 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL);
+      /* Most of the time, the type names will match, do not be unnecesarily
+         verbose.  */
+      if (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t1)))
+         != IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t2))))
+        inform (loc_t1,
+               "type %qT defined in anonymous namespace can not match "
+               "type %qT across the translation unit boundary",
+               t1, t2);
+      else
+        inform (loc_t1,
+               "type %qT defined in anonymous namespace can not match "
+               "across the translation unit boundary",
+               t1);
+      if (loc_t2_useful)
+        inform (loc_t2,
+               "the incompatible type defined in another translation unit");
+      return;
+    }
+  /* If types have mangled ODR names and they are different, it is most
+     informative to output those.
+     This also covers types defined in different namespaces.  */
+  if (TYPE_NAME (t1) && TYPE_NAME (t2)
+      && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
+      && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
+      && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t1))
+      && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t2))
+      && DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
+        != DECL_ASSEMBLER_NAME (TYPE_NAME (t2)))
+    {
+      char *name1 = xstrdup (cplus_demangle
+        (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))),
+         DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES));
+      char *name2 = cplus_demangle
+        (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t2))),
+         DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES);
+      if (name1 && name2 && strcmp (name1, name2))
+       {
+         inform (loc_t1,
+                 "type name %qs should match type name %qs",
+                 name1, name2);
+         if (loc_t2_useful)
+           inform (loc_t2,
+                   "the incompatible type is defined here");
+         free (name1);
+         return;
+       }
+      free (name1);
+    }
+  /* A tricky case are compound types.  Often they appear the same in source
+     code and the mismatch is dragged in by type they are build from.
+     Look for those differences in subtypes and try to be informative.  In other
+     cases just output nothing because the source code is probably different
+     and in this case we already output a all necessary info.  */
   if (!TYPE_NAME (t1) || !TYPE_NAME (t2))
+    {
+      if (TREE_CODE (t1) == TREE_CODE (t2))
+       {
+         if (TREE_CODE (t1) == ARRAY_TYPE
+             && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
+           {
+             tree i1 = TYPE_DOMAIN (t1);
+             tree i2 = TYPE_DOMAIN (t2);
+       
+             if (i1 && i2
+                 && TYPE_MAX_VALUE (i1)
+                 && TYPE_MAX_VALUE (i2)
+                 && !operand_equal_p (TYPE_MAX_VALUE (i1),
+                                      TYPE_MAX_VALUE (i2), 0))
+               {
+                 inform (loc,
+                         "array types have different bounds");
+                 return;
+               }
+           }
+         if ((POINTER_TYPE_P (t1) || TREE_CODE (t1) == ARRAY_TYPE)
+             && type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
+           warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1, loc_t2);
+         else if (TREE_CODE (t1) == METHOD_TYPE
+                  || TREE_CODE (t1) == FUNCTION_TYPE)
+           {
+             tree parms1 = NULL, parms2 = NULL;
+             int count = 1;
+
+             if (type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
+               {
+                 inform (loc, "return value type mismatch");
+                 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1,
+                                      loc_t2);
+                 return;
+               }
+             if (prototype_p (t1) && prototype_p (t2))
+               for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
+                    parms1 && parms2;
+                    parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2),
+                    count++)
+                 {
+                   if (type_mismatch_p (TREE_VALUE (parms1), TREE_VALUE (parms2)))
+                     {
+                       if (count == 1 && TREE_CODE (t1) == METHOD_TYPE)
+                         inform (loc,
+                                 "implicit this pointer type mismatch");
+                       else
+                         inform (loc,
+                                 "type mismatch in parameter %i",
+                                 count - (TREE_CODE (t1) == METHOD_TYPE));
+                       warn_types_mismatch (TREE_VALUE (parms1),
+                                            TREE_VALUE (parms2),
+                                            loc_t1, loc_t2);
+                       return;
+                     }
+                 }
+             if (parms1 || parms2)
+               {
+                 inform (loc,
+                         "types have different parameter counts");
+                 return;
+               }
+           }
+       }
+      return;
+    }
+
+  if (types_odr_comparable (t1, t2, true)
+      && types_same_for_odr (t1, t2, true))
+    inform (loc_t1,
+           "type %qT itself violates the C++ One Definition Rule", t1);
+  /* Prevent pointless warnings like "struct aa" should match "struct aa".  */
+  else if (TYPE_NAME (t1) == TYPE_NAME (t2)
+          && TREE_CODE (t1) == TREE_CODE (t2) && !loc_t2_useful)
     return;
-  /* In Firefox it is a common bug to have same types but in
-     different namespaces.  Be a bit more informative on
-     this.  */
-  if (TYPE_CONTEXT (t1) && TYPE_CONTEXT (t2)
-      && (((TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL)
-           != (TREE_CODE (TYPE_CONTEXT (t2)) == NAMESPACE_DECL))
-          || (TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL
-              && (DECL_NAME (TYPE_CONTEXT (t1)) !=
-                  DECL_NAME (TYPE_CONTEXT (t2))))))
-    inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
-           "type %qT should match type %qT but is defined "
-           "in different namespace  ",
-           t1, t2);
   else
-    inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
-           "type %qT should match type %qT",
+    inform (loc_t1, "type %qT should match type %qT",
            t1, t2);
-  inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
-         "the incompatible type is defined here");
+  if (loc_t2_useful)
+    inform (loc_t2, "the incompatible type is defined here");
 }
 
 /* Compare T1 and T2, report ODR violations if WARN is true and set
    WARNED to true if anything is reported.  Return true if types match.
    If true is returned, the types are also compatible in the sense of
-   gimple_canonical_types_compatible_p.  */
+   gimple_canonical_types_compatible_p.
+   If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning
+   about the type if the type itself do not have location.  */
 
 static bool
 odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
-                       hash_set<type_pair,pair_traits> *visited)
+                       hash_set<type_pair> *visited,
+                       location_t loc1, location_t loc2)
 {
   /* Check first for the obvious case of pointer identity.  */
   if (t1 == t2)
     return true;
-  gcc_assert (!type_in_anonymous_namespace_p (t1));
-  gcc_assert (!type_in_anonymous_namespace_p (t2));
+  gcc_assert (!type_with_linkage_p (t1) || !type_in_anonymous_namespace_p (t1));
+  gcc_assert (!type_with_linkage_p (t2) || !type_in_anonymous_namespace_p (t2));
 
   /* Can't be the same type if the types don't have the same code.  */
   if (TREE_CODE (t1) != TREE_CODE (t2))
@@ -1018,10 +1280,20 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
       return false;
     }
 
+  if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
+      || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
+    {
+      /* We can not trip this when comparing ODR types, only when trying to
+        match different ODR derivations from different declarations.
+        So WARN should be always false.  */
+      gcc_assert (!warn);
+      return false;
+    }
+
   if (comp_type_attributes (t1, t2) != 1)
     {
       warn_odr (t1, t2, NULL, NULL, warn, warned,
-               G_("a type with attributes "
+               G_("a type with different attributes "
                   "is defined in another translation unit"));
       return false;
     }
@@ -1108,26 +1380,29 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
              return false;
            }
 
-         if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
+         if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
+                                         visited, loc1, loc2))
            {
              warn_odr (t1, t2, NULL, NULL, warn, warned,
                        G_("it is defined as a pointer to different type "
                           "in another translation unit"));
              if (warn && warned)
-               warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
+               warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2),
+                                    loc1, loc2);
              return false;
            }
        }
 
       if ((TREE_CODE (t1) == VECTOR_TYPE || TREE_CODE (t1) == COMPLEX_TYPE)
-         && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
+         && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
+                                        visited, loc1, loc2))
        {
          /* Probably specific enough.  */
          warn_odr (t1, t2, NULL, NULL, warn, warned,
                    G_("a different type is defined "
                       "in another translation unit"));
          if (warn && warned)
-           warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
+           warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
          return false;
        }
     }
@@ -1138,13 +1413,14 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
       {
        /* Array types are the same if the element types are the same and
           the number of elements are the same.  */
-       if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
+       if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
+                                       visited, loc1, loc2))
          {
            warn_odr (t1, t2, NULL, NULL, warn, warned,
                      G_("a different type is defined in another "
                         "translation unit"));
            if (warn && warned)
-             warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
+             warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
          }
        gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
        gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
@@ -1179,17 +1455,19 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
     case FUNCTION_TYPE:
       /* Function types are the same if the return type and arguments types
         are the same.  */
-      if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
+      if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
+                                     visited, loc1, loc2))
        {
          warn_odr (t1, t2, NULL, NULL, warn, warned,
                    G_("has different return value "
                       "in another translation unit"));
          if (warn && warned)
-           warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
+           warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
          return false;
        }
 
-      if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
+      if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2)
+         || !prototype_p (t1) || !prototype_p (t2))
        return true;
       else
        {
@@ -1200,14 +1478,15 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
               parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
            {
              if (!odr_subtypes_equivalent_p
-                    (TREE_VALUE (parms1), TREE_VALUE (parms2), visited))
+                    (TREE_VALUE (parms1), TREE_VALUE (parms2), visited,
+                     loc1, loc2))
                {
                  warn_odr (t1, t2, NULL, NULL, warn, warned,
                            G_("has different parameters in another "
                               "translation unit"));
                  if (warn && warned)
                    warn_types_mismatch (TREE_VALUE (parms1),
-                                        TREE_VALUE (parms2));
+                                        TREE_VALUE (parms2), loc1, loc2);
                  return false;
                }
            }
@@ -1232,6 +1511,20 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
        /* For aggregate types, all the fields must be the same.  */
        if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
          {
+           if (TYPE_BINFO (t1) && TYPE_BINFO (t2)
+               && polymorphic_type_binfo_p (TYPE_BINFO (t1))
+                  != polymorphic_type_binfo_p (TYPE_BINFO (t2)))
+             {
+               if (polymorphic_type_binfo_p (TYPE_BINFO (t1)))
+                 warn_odr (t1, t2, NULL, NULL, warn, warned,
+                           G_("a type defined in another translation unit "
+                              "is not polymorphic"));
+               else
+                 warn_odr (t1, t2, NULL, NULL, warn, warned,
+                           G_("a type defined in another translation unit "
+                              "is polymorphic"));
+               return false;
+             }
            for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
                 f1 || f2;
                 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
@@ -1266,7 +1559,8 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
                    return false;
                  }
                if (!odr_subtypes_equivalent_p (TREE_TYPE (f1),
-                                               TREE_TYPE (f2), visited))
+                                               TREE_TYPE (f2), visited,
+                                               loc1, loc2))
                  {
                    /* Do not warn about artificial fields and just go into
                       generic field mismatch warning.  */
@@ -1277,7 +1571,7 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
                              G_("a field of same name but different type "
                                 "is defined in another translation unit"));
                    if (warn && warned)
-                     warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2));
+                     warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2), loc1, loc2);
                    return false;
                  }
                if (!gimple_compare_field_offset (f1, f2))
@@ -1287,7 +1581,7 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
                    if (DECL_ARTIFICIAL (f1))
                      break;
                    warn_odr (t1, t2, f1, f2, warn, warned,
-                             G_("fields has different layout "
+                             G_("fields have different layout "
                                 "in another translation unit"));
                    return false;
                  }
@@ -1303,8 +1597,8 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
                  warn_odr (t1, t2, NULL, NULL, warn, warned,
                            G_("a type with different virtual table pointers"
                               " is defined in another translation unit"));
-               if ((f1 && DECL_ARTIFICIAL (f1))
-                   || (f2 && DECL_ARTIFICIAL (f2)))
+               else if ((f1 && DECL_ARTIFICIAL (f1))
+                        || (f2 && DECL_ARTIFICIAL (f2)))
                  warn_odr (t1, t2, NULL, NULL, warn, warned,
                            G_("a type with different bases is defined "
                               "in another translation unit"));
@@ -1315,55 +1609,11 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
                
                return false;
              }
-           if ((TYPE_MAIN_VARIANT (t1) == t1 || TYPE_MAIN_VARIANT (t2) == t2)
-               && (TYPE_METHODS (TYPE_MAIN_VARIANT (t1))
-                   != TYPE_METHODS (TYPE_MAIN_VARIANT (t2))))
-             {
-               for (f1 = TYPE_METHODS (TYPE_MAIN_VARIANT (t1)),
-                    f2 = TYPE_METHODS (TYPE_MAIN_VARIANT (t2));
-                    f1 && f2 ; f1 = DECL_CHAIN (f1), f2 = DECL_CHAIN (f2))
-                 {
-                   if (DECL_ASSEMBLER_NAME (f1) != DECL_ASSEMBLER_NAME (f2))
-                     {
-                       warn_odr (t1, t2, f1, f2, warn, warned,
-                                 G_("a different method of same type "
-                                    "is defined in another translation unit"));
-                       return false;
-                     }
-                   if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
-                     {
-                       warn_odr (t1, t2, f1, f2, warn, warned,
-                                 G_("s definition that differs by virtual "
-                                    "keyword in another translation unit"));
-                       return false;
-                     }
-                   if (DECL_VINDEX (f1) != DECL_VINDEX (f2))
-                     {
-                       warn_odr (t1, t2, f1, f2, warn, warned,
-                                 G_("virtual table layout differs in another "
-                                    "translation unit"));
-                       return false;
-                     }
-                   if (odr_subtypes_equivalent_p (TREE_TYPE (f1), TREE_TYPE (f2), visited))
-                     {
-                       warn_odr (t1, t2, f1, f2, warn, warned,
-                                 G_("method with incompatible type is defined "
-                                    "in another translation unit"));
-                       return false;
-                     }
-                 }
-               if (f1 || f2)
-                 {
-                   warn_odr (t1, t2, NULL, NULL, warn, warned,
-                             G_("a type with different number of methods "
-                                "is defined in another translation unit"));
-                   return false;
-                 }
-             }
          }
        break;
       }
     case VOID_TYPE:
+    case NULLPTR_TYPE:
       break;
 
     default:
@@ -1394,6 +1644,19 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
   return true;
 }
 
+/* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule.  */
+
+bool
+odr_types_equivalent_p (tree type1, tree type2)
+{
+  gcc_checking_assert (odr_or_derived_type_p (type1)
+                      && odr_or_derived_type_p (type2));
+
+  hash_set<type_pair> visited;
+  return odr_types_equivalent_p (type1, type2, false, NULL,
+                                &visited, UNKNOWN_LOCATION, UNKNOWN_LOCATION);
+}
+
 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
    from VAL->type.  This may happen in LTO where tree merging did not merge
    all variants of the same type or due to ODR violation.
@@ -1408,13 +1671,23 @@ add_type_duplicate (odr_type val, tree type)
 {
   bool build_bases = false;
   bool prevail = false;
+  bool odr_must_violate = false;
 
   if (!val->types_set)
     val->types_set = new hash_set<tree>;
 
+  /* Chose polymorphic type as leader (this happens only in case of ODR
+     violations.  */
+  if ((TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
+       && polymorphic_type_binfo_p (TYPE_BINFO (type)))
+      && (TREE_CODE (val->type) != RECORD_TYPE || !TYPE_BINFO (val->type)
+          || !polymorphic_type_binfo_p (TYPE_BINFO (val->type))))
+    {
+      prevail = true;
+      build_bases = true;
+    }
   /* Always prefer complete type to be the leader.  */
-
-  if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type))
+  else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type))
     {
       prevail = true;
       build_bases = TYPE_BINFO (type);
@@ -1435,12 +1708,7 @@ add_type_duplicate (odr_type val, tree type)
     }
 
   if (prevail)
-    {
-      tree tmp = type;
-
-      type = val->type;
-      val->type = tmp;
-    }
+    std::swap (val->type, type);
 
   val->types_set->add (type);
 
@@ -1455,32 +1723,12 @@ add_type_duplicate (odr_type val, tree type)
   bool base_mismatch = false;
   unsigned int i;
   bool warned = false;
-  hash_set<type_pair,pair_traits> visited;
+  hash_set<type_pair> visited;
 
   gcc_assert (in_lto_p);
   vec_safe_push (val->types, type);
 
-  /* First we compare memory layout.  */
-  if (!odr_types_equivalent_p (val->type, type,
-                              !flag_ltrans && !val->odr_violated,
-                              &warned, &visited))
-    {
-      merge = false;
-      odr_violation_reported = true;
-      val->odr_violated = true;
-      if (symtab->dump_file)
-       {
-         fprintf (symtab->dump_file, "ODR violation\n");
-
-         print_node (symtab->dump_file, "", val->type, 0);
-         putc ('\n',symtab->dump_file);
-         print_node (symtab->dump_file, "", type, 0);
-         putc ('\n',symtab->dump_file);
-       }
-    }
-
-  /* Next sanity check that bases are the same.  If not, we will end
-     up producing wrong answers.  */
+  /* If both are class types, compare the bases.  */
   if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
       && TREE_CODE (val->type) == RECORD_TYPE
       && TREE_CODE (type) == RECORD_TYPE
@@ -1489,25 +1737,28 @@ add_type_duplicate (odr_type val, tree type)
       if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
          != BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
        {
-         if (!warned && !val->odr_violated)
+         if (!flag_ltrans && !warned && !val->odr_violated)
            {
              tree extra_base;
              warn_odr (type, val->type, NULL, NULL, !warned, &warned,
                        "a type with the same name but different "
                        "number of polymorphic bases is "
                        "defined in another translation unit");
-             if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
-                 > BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
-               extra_base = BINFO_BASE_BINFO
-                            (TYPE_BINFO (type),
-                             BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)));
-             else
-               extra_base = BINFO_BASE_BINFO
-                            (TYPE_BINFO (val->type),
-                             BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
-             inform (DECL_SOURCE_LOCATION
-                       (TYPE_NAME (DECL_CONTEXT (extra_base))),
-                     "the extra base is defined here ");
+             if (warned)
+               {
+                 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
+                     > BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
+                   extra_base = BINFO_BASE_BINFO
+                                (TYPE_BINFO (type),
+                                 BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)));
+                 else
+                   extra_base = BINFO_BASE_BINFO
+                                (TYPE_BINFO (val->type),
+                                 BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
+                 tree extra_base_type = BINFO_TYPE (extra_base);
+                 inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type)),
+                         "the extra base is defined here");
+               }
            }
          base_mismatch = true;
        }
@@ -1525,12 +1776,8 @@ add_type_duplicate (odr_type val, tree type)
                  base_mismatch = true;
              }
            else
-             {
-               hash_set<type_pair,pair_traits> visited;
-               if (!odr_types_equivalent_p (type1, type2, false, NULL,
-                                            &visited))
-                 base_mismatch = true;
-             }
+             if (!odr_types_equivalent_p (type1, type2))
+               base_mismatch = true;
            if (base_mismatch)
              {
                if (!warned && !val->odr_violated)
@@ -1540,7 +1787,8 @@ add_type_duplicate (odr_type val, tree type)
                              "a type with the same name but different base "
                              "type is defined in another translation unit");
                    if (warned)
-                     warn_types_mismatch (type1, type2);
+                     warn_types_mismatch (type1, type2,
+                                           UNKNOWN_LOCATION, UNKNOWN_LOCATION);
                  }
                break;
              }
@@ -1561,9 +1809,10 @@ add_type_duplicate (odr_type val, tree type)
                   but not for TYPE2 we possibly missed a base when recording
                   VAL->type earlier.
                   Be sure this does not happen.  */
-               gcc_assert (TYPE_BINFO (type2)
-                           || !polymorphic_type_binfo_p (TYPE_BINFO (type1))
-                           || build_bases);
+               if (TYPE_BINFO (type1)
+                   && polymorphic_type_binfo_p (TYPE_BINFO (type1))
+                   && !build_bases)
+                 odr_must_violate = true;
                break;
              }
            /* One base is polymorphic and the other not.
@@ -1573,38 +1822,15 @@ add_type_duplicate (odr_type val, tree type)
                     && polymorphic_type_binfo_p (TYPE_BINFO (type1))
                        != polymorphic_type_binfo_p (TYPE_BINFO (type2)))
              {
-               gcc_assert (val->odr_violated);
+               if (!warned && !val->odr_violated)
+                 warn_odr (type, val->type, NULL, NULL,
+                           !warned, &warned,
+                           "a base of the type is polymorphic only in one "
+                           "translation unit");
                base_mismatch = true;
                break;
              }
          }
-#ifdef ENABLE_CHECKING
-      /* Sanity check that all bases will be build same way again.  */
-      if (!base_mismatch && val->bases.length ())
-       {
-         unsigned int num_poly_bases = 0;
-         unsigned int j;
-
-         for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
-           if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
-                                            (TYPE_BINFO (type), i)))
-             num_poly_bases++;
-         gcc_assert (num_poly_bases == val->bases.length ());
-         for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type));
-              i++)
-           if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
-                                          (TYPE_BINFO (type), i)))
-             {
-               odr_type base = get_odr_type
-                                  (BINFO_TYPE
-                                     (BINFO_BASE_BINFO (TYPE_BINFO (type),
-                                                        i)),
-                                   true);
-               gcc_assert (val->bases[j] == base);
-               j++;
-             }
-       }
-#endif
       if (base_mismatch)
        {
          merge = false;
@@ -1623,6 +1849,56 @@ add_type_duplicate (odr_type val, tree type)
        }
     }
 
+  /* Next compare memory layout.
+     The DECL_SOURCE_LOCATIONs in this invocation came from LTO streaming.
+     We must apply the location cache to ensure that they are valid
+     before we can pass them to odr_types_equivalent_p (PR lto/83121).  */
+  if (lto_location_cache::current_cache)
+    lto_location_cache::current_cache->apply_location_cache ();
+  if (!odr_types_equivalent_p (val->type, type,
+                              !flag_ltrans && !val->odr_violated && !warned,
+                              &warned, &visited,
+                              DECL_SOURCE_LOCATION (TYPE_NAME (val->type)),
+                              DECL_SOURCE_LOCATION (TYPE_NAME (type))))
+    {
+      merge = false;
+      odr_violation_reported = true;
+      val->odr_violated = true;
+    }
+  gcc_assert (val->odr_violated || !odr_must_violate);
+  /* Sanity check that all bases will be build same way again.  */
+  if (flag_checking
+      && COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
+      && TREE_CODE (val->type) == RECORD_TYPE
+      && TREE_CODE (type) == RECORD_TYPE
+      && TYPE_BINFO (val->type) && TYPE_BINFO (type)
+      && !val->odr_violated
+      && !base_mismatch && val->bases.length ())
+    {
+      unsigned int num_poly_bases = 0;
+      unsigned int j;
+
+      for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
+       if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
+                                        (TYPE_BINFO (type), i)))
+         num_poly_bases++;
+      gcc_assert (num_poly_bases == val->bases.length ());
+      for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type));
+          i++)
+       if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
+                                      (TYPE_BINFO (type), i)))
+         {
+           odr_type base = get_odr_type
+                              (BINFO_TYPE
+                                 (BINFO_BASE_BINFO (TYPE_BINFO (type),
+                                                    i)),
+                               true);
+           gcc_assert (val->bases[j] == base);
+           j++;
+         }
+    }
+
+
   /* Regularize things a little.  During LTO same types may come with
      different BINFOs.  Either because their virtual table was
      not merged by tree merging and only later at decl merging or
@@ -1726,8 +2002,8 @@ get_odr_type (tree type, bool insert)
       if (slot && *slot)
        {
          val = *slot;
-#ifdef ENABLE_CHECKING
-         if (in_lto_p && can_be_vtable_hashed_p (type))
+         if (flag_checking
+             && in_lto_p && can_be_vtable_hashed_p (type))
            {
              hash = hash_odr_vtable (type);
              vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
@@ -1735,7 +2011,6 @@ get_odr_type (tree type, bool insert)
              gcc_assert (!vtable_slot || *vtable_slot == *slot);
              vtable_slot = NULL;
            }
-#endif
        }
       else if (*vtable_slot)
        val = *vtable_slot;
@@ -1769,7 +2044,10 @@ get_odr_type (tree type, bool insert)
       val->type = type;
       val->bases = vNULL;
       val->derived_types = vNULL;
-      val->anonymous_namespace = type_in_anonymous_namespace_p (type);
+      if (type_with_linkage_p (type))
+        val->anonymous_namespace = type_in_anonymous_namespace_p (type);
+      else
+       val->anonymous_namespace = 0;
       build_bases = COMPLETE_TYPE_P (val->type);
       insert_to_odr_array = true;
       if (slot)
@@ -1779,12 +2057,13 @@ get_odr_type (tree type, bool insert)
     }
 
   if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
+      && type_with_linkage_p (type)
       && type == TYPE_MAIN_VARIANT (type))
     {
       tree binfo = TYPE_BINFO (type);
       unsigned int i;
 
-      gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) = type);
+      gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) == type);
   
       val->all_derivations_known = type_all_derivations_known_p (type);
       for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
@@ -1793,10 +2072,9 @@ get_odr_type (tree type, bool insert)
           determine ODR equivalency of these during LTO.  */
        if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
          {
-           odr_type base = get_odr_type (BINFO_TYPE (BINFO_BASE_BINFO (binfo,
-                                                                       i)),
-                                         true);
-           gcc_assert (TYPE_MAIN_VARIANT (base->type) == base->type);
+           tree base_type= BINFO_TYPE (BINFO_BASE_BINFO (binfo, i));
+           odr_type base = get_odr_type (base_type, true);
+           gcc_assert (TYPE_MAIN_VARIANT (base_type) == base_type);
            base->derived_types.safe_push (val);
            val->bases.safe_push (base);
            if (base->id > base_id)
@@ -1816,8 +2094,7 @@ get_odr_type (tree type, bool insert)
       /* Be sure we did not recorded any derived types; these may need
         renumbering too.  */
       gcc_assert (val->derived_types.length() == 0);
-      if (odr_types_ptr)
-       val->id = odr_types.length ();
+      val->id = odr_types.length ();
       vec_safe_push (odr_types_ptr, val);
     }
   return val;
@@ -1834,17 +2111,22 @@ register_odr_type (tree type)
       if (in_lto_p)
         odr_vtable_hash = new odr_vtable_hash_type (23);
     }
-  /* Arrange things to be nicer and insert main variants first.  */
-  if (odr_type_p (TYPE_MAIN_VARIANT (type)))
+  /* Arrange things to be nicer and insert main variants first.
+     ??? fundamental prerecorded types do not have mangled names; this
+     makes it possible that non-ODR type is main_odr_variant of ODR type.
+     Things may get smoother if LTO FE set mangled name of those types same
+     way as C++ FE does.  */
+  if (odr_type_p (main_odr_variant (TYPE_MAIN_VARIANT (type)))
+      && odr_type_p (TYPE_MAIN_VARIANT (type)))
     get_odr_type (TYPE_MAIN_VARIANT (type), true);
-  if (TYPE_MAIN_VARIANT (type) != type)
+  if (TYPE_MAIN_VARIANT (type) != type && odr_type_p (main_odr_variant (type)))
     get_odr_type (type, true);
 }
 
 /* Return true if type is known to have no derivations.  */
 
 bool
-type_known_to_have_no_deriavations_p (tree t)
+type_known_to_have_no_derivations_p (tree t)
 {
   return (type_all_derivations_known_p (t)
          && (TYPE_FINAL_P (t)
@@ -1865,9 +2147,9 @@ dump_odr_type (FILE *f, odr_type t, int indent=0)
   fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":"");
   if (TYPE_NAME (t->type))
     {
-      fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
+      /*fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
               DECL_SOURCE_FILE (TYPE_NAME (t->type)),
-              DECL_SOURCE_LINE (TYPE_NAME (t->type)));
+              DECL_SOURCE_LINE (TYPE_NAME (t->type)));*/
       if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t->type)))
         fprintf (f, "%*s mangled name: %s\n", indent * 2, "",
                 IDENTIFIER_POINTER
@@ -1927,18 +2209,6 @@ dump_type_inheritance_graph (FILE *f)
     }
 }
 
-/* Given method type T, return type of class it belongs to.
-   Look up this pointer and get its type.    */
-
-tree
-method_class_type (const_tree t)
-{
-  tree first_parm_type = TREE_VALUE (TYPE_ARG_TYPES (t));
-  gcc_assert (TREE_CODE (t) == METHOD_TYPE);
-
-  return TREE_TYPE (first_parm_type);
-}
-
 /* Initialize IPA devirt and build inheritance tree graph.  */
 
 void
@@ -1946,7 +2216,7 @@ build_type_inheritance_graph (void)
 {
   struct symtab_node *n;
   FILE *inheritance_dump_file;
-  int flags;
+  dump_flags_t flags;
 
   if (odr_hash)
     return;
@@ -1957,13 +2227,12 @@ build_type_inheritance_graph (void)
     odr_vtable_hash = new odr_vtable_hash_type (23);
 
   /* We reconstruct the graph starting of types of all methods seen in the
-     the unit.  */
+     unit.  */
   FOR_EACH_SYMBOL (n)
     if (is_a <cgraph_node *> (n)
        && DECL_VIRTUAL_P (n->decl)
        && n->real_symbol_p ())
-      get_odr_type (TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (n->decl))),
-                   true);
+      get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
 
     /* Look also for virtual tables of types that do not define any methods.
  
@@ -2030,7 +2299,7 @@ referenced_from_vtable_p (struct cgraph_node *node)
     if ((ref->use == IPA_REF_ALIAS
         && referenced_from_vtable_p (dyn_cast<cgraph_node *> (ref->referring)))
        || (ref->use == IPA_REF_ADDR
-           && TREE_CODE (ref->referring->decl) == VAR_DECL
+           && VAR_P (ref->referring->decl)
            && DECL_VIRTUAL_P (ref->referring->decl)))
       {
        found = true;
@@ -2039,6 +2308,17 @@ referenced_from_vtable_p (struct cgraph_node *node)
   return found;
 }
 
+/* Return if TARGET is cxa_pure_virtual.  */
+
+static bool
+is_cxa_pure_virtual_p (tree target)
+{
+  return target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE
+        && DECL_NAME (target)
+        && id_equal (DECL_NAME (target),
+                    "__cxa_pure_virtual");
+}
+
 /* If TARGET has associated node, record it in the NODES array.
    CAN_REFER specify if program can refer to the target directly.
    if TARGET is unknown (NULL) or it can not be inserted (for example because
@@ -2053,11 +2333,12 @@ maybe_record_node (vec <cgraph_node *> &nodes,
 {
   struct cgraph_node *target_node, *alias_target;
   enum availability avail;
+  bool pure_virtual = is_cxa_pure_virtual_p (target);
 
-  /* cxa_pure_virtual and __builtin_unreachable do not need to be added into
+  /* __builtin_unreachable do not need to be added into
      list of targets; the runtime effect of calling them is undefined.
      Only "real" virtual methods should be accounted.  */
-  if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE)
+  if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE && !pure_virtual)
     return;
 
   if (!can_refer)
@@ -2100,6 +2381,7 @@ maybe_record_node (vec <cgraph_node *> &nodes,
      ??? Maybe it would make sense to be more aggressive for LTO even
      elsewhere.  */
   if (!flag_ltrans
+      && !pure_virtual
       && type_in_anonymous_namespace_p (DECL_CONTEXT (target))
       && (!target_node
           || !referenced_from_vtable_p (target_node)))
@@ -2113,16 +2395,43 @@ maybe_record_node (vec <cgraph_node *> &nodes,
     {
       gcc_assert (!target_node->global.inlined_to);
       gcc_assert (target_node->real_symbol_p ());
+      /* When sanitizing, do not assume that __cxa_pure_virtual is not called
+        by valid program.  */
+      if (flag_sanitize & SANITIZE_UNREACHABLE)
+       ;
+      /* Only add pure virtual if it is the only possible target.  This way
+        we will preserve the diagnostics about pure virtual called in many
+        cases without disabling optimization in other.  */
+      else if (pure_virtual)
+       {
+         if (nodes.length ())
+           return;
+       }
+      /* If we found a real target, take away cxa_pure_virtual.  */
+      else if (!pure_virtual && nodes.length () == 1
+              && is_cxa_pure_virtual_p (nodes[0]->decl))
+       nodes.pop ();
+      if (pure_virtual && nodes.length ())
+       return;
       if (!inserted->add (target))
        {
          cached_polymorphic_call_targets->add (target_node);
          nodes.safe_push (target_node);
        }
     }
-  else if (completep
-          && (!type_in_anonymous_namespace_p
-                (DECL_CONTEXT (target))
-              || flag_ltrans))
+  else if (!completep)
+    ;
+  /* We have definition of __cxa_pure_virtual that is not accessible (it is
+     optimized out or partitioned to other unit) so we can not add it.  When
+     not sanitizing, there is nothing to do.
+     Otherwise declare the list incomplete.  */
+  else if (pure_virtual)
+    {
+      if (flag_sanitize & SANITIZE_UNREACHABLE)
+       *completep = false;
+    }
+  else if (flag_ltrans
+          || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
     *completep = false;
 }
 
@@ -2310,30 +2619,30 @@ struct polymorphic_call_target_d
 
 /* Polymorphic call target cache helpers.  */
 
-struct polymorphic_call_target_hasher 
+struct polymorphic_call_target_hasher
+  : pointer_hash <polymorphic_call_target_d>
 {
-  typedef polymorphic_call_target_d value_type;
-  typedef polymorphic_call_target_d compare_type;
-  static inline hashval_t hash (const value_type *);
-  static inline bool equal (const value_type *, const compare_type *);
-  static inline void remove (value_type *);
+  static inline hashval_t hash (const polymorphic_call_target_d *);
+  static inline bool equal (const polymorphic_call_target_d *,
+                           const polymorphic_call_target_d *);
+  static inline void remove (polymorphic_call_target_d *);
 };
 
 /* Return the computed hashcode for ODR_QUERY.  */
 
 inline hashval_t
-polymorphic_call_target_hasher::hash (const value_type *odr_query)
+polymorphic_call_target_hasher::hash (const polymorphic_call_target_d *odr_query)
 {
   inchash::hash hstate (odr_query->otr_token);
 
-  hstate.add_wide_int (odr_query->type->id);
+  hstate.add_hwi (odr_query->type->id);
   hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
-  hstate.add_wide_int (odr_query->context.offset);
+  hstate.add_hwi (odr_query->context.offset);
 
   if (odr_query->context.speculative_outer_type)
     {
       hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
-      hstate.add_wide_int (odr_query->context.speculative_offset);
+      hstate.add_hwi (odr_query->context.speculative_offset);
     }
   hstate.add_flag (odr_query->speculative);
   hstate.add_flag (odr_query->context.maybe_in_construction);
@@ -2346,8 +2655,8 @@ polymorphic_call_target_hasher::hash (const value_type *odr_query)
 /* Compare cache entries T1 and T2.  */
 
 inline bool
-polymorphic_call_target_hasher::equal (const value_type *t1,
-                                      const compare_type *t2)
+polymorphic_call_target_hasher::equal (const polymorphic_call_target_d *t1,
+                                      const polymorphic_call_target_d *t2)
 {
   return (t1->type == t2->type && t1->otr_token == t2->otr_token
          && t1->speculative == t2->speculative
@@ -2365,7 +2674,7 @@ polymorphic_call_target_hasher::equal (const value_type *t1,
 /* Remove entry in polymorphic call target cache hash.  */
 
 inline void
-polymorphic_call_target_hasher::remove (value_type *v)
+polymorphic_call_target_hasher::remove (polymorphic_call_target_d *v)
 {
   v->targets.release ();
   free (v);
@@ -2586,7 +2895,7 @@ struct odr_type_warn_count
 {
   tree type;
   int count;
-  gcov_type dyn_count;
+  profile_count dyn_count;
 };
 
 /* Record about how many calls would benefit from given method to be final.  */
@@ -2595,17 +2904,34 @@ struct decl_warn_count
 {
   tree decl;
   int count;
-  gcov_type dyn_count;
+  profile_count dyn_count;
 };
 
 /* Information about type and decl warnings.  */
 
 struct final_warning_record
 {
-  gcov_type dyn_count;
-  vec<odr_type_warn_count> type_warnings;
+  /* If needed grow type_warnings vector and initialize new decl_warn_count
+     to have dyn_count set to profile_count::zero ().  */
+  void grow_type_warnings (unsigned newlen);
+
+  profile_count dyn_count;
+  auto_vec<odr_type_warn_count> type_warnings;
   hash_map<tree, decl_warn_count> decl_warnings;
 };
+
+void
+final_warning_record::grow_type_warnings (unsigned newlen)
+{
+  unsigned len = type_warnings.length ();
+  if (newlen > len)
+    {
+      type_warnings.safe_grow_cleared (newlen);
+      for (unsigned i = len; i < newlen; i++)
+       type_warnings[i].dyn_count = profile_count::zero ();
+    }
+}
+
 struct final_warning_record *final_warning_records;
 
 /* Return vector containing possible targets of polymorphic call of type
@@ -2740,15 +3066,22 @@ possible_polymorphic_call_targets (tree otr_type,
       if ((*slot)->type_warning && final_warning_records)
        {
          final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
-         final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
-           += final_warning_records->dyn_count;
+         if (!final_warning_records->type_warnings
+               [(*slot)->type_warning - 1].dyn_count.initialized_p ())
+           final_warning_records->type_warnings
+               [(*slot)->type_warning - 1].dyn_count = profile_count::zero ();
+         if (final_warning_records->dyn_count > 0)
+           final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
+             = final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
+               + final_warning_records->dyn_count;
        }
       if (!speculative && (*slot)->decl_warning && final_warning_records)
        {
          struct decl_warn_count *c =
             final_warning_records->decl_warnings.get ((*slot)->decl_warning);
          c->count++;
-         c->dyn_count += final_warning_records->dyn_count;
+         if (final_warning_records->dyn_count > 0)
+           c->dyn_count += final_warning_records->dyn_count;
        }
       return (*slot)->targets;
     }
@@ -2862,17 +3195,21 @@ possible_polymorphic_call_targets (tree otr_type,
 
          if (!outer_type->all_derivations_known)
            {
-             if (!speculative && final_warning_records)
+             if (!speculative && final_warning_records
+                 && nodes.length () == 1
+                 && TREE_CODE (TREE_TYPE (nodes[0]->decl)) == METHOD_TYPE)
                {
                  if (complete
-                     && nodes.length () == 1
                      && warn_suggest_final_types
                      && !outer_type->derived_types.length ())
                    {
-                     if (outer_type->id >= (int)final_warning_records->type_warnings.length ())
-                       final_warning_records->type_warnings.safe_grow_cleared
-                         (odr_types.length ());
+                     final_warning_records->grow_type_warnings
+                       (outer_type->id);
                      final_warning_records->type_warnings[outer_type->id].count++;
+                     if (!final_warning_records->type_warnings
+                               [outer_type->id].dyn_count.initialized_p ())
+                       final_warning_records->type_warnings
+                          [outer_type->id].dyn_count = profile_count::zero ();
                      final_warning_records->type_warnings[outer_type->id].dyn_count
                        += final_warning_records->dyn_count;
                      final_warning_records->type_warnings[outer_type->id].type
@@ -2881,7 +3218,6 @@ possible_polymorphic_call_targets (tree otr_type,
                    }
                  if (complete
                      && warn_suggest_final_methods
-                     && nodes.length () == 1
                      && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl),
                                             outer_type->type))
                    {
@@ -2963,7 +3299,8 @@ dump_targets (FILE *f, vec <cgraph_node *> targets)
       char *name = NULL;
       if (in_lto_p)
        name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
-      fprintf (f, " %s/%i", name ? name : targets[i]->name (), targets[i]->order);
+      fprintf (f, " %s/%i", name ? name : targets[i]->name (),
+              targets[i]->order);
       if (in_lto_p)
        free (name);
       if (!targets[i]->definition)
@@ -3015,7 +3352,13 @@ dump_possible_polymorphic_call_targets (FILE *f,
       fprintf (f, "  Speculative targets:");
       dump_targets (f, targets);
     }
-  gcc_assert (targets.length () <= len);
+  /* Ugly: during callgraph construction the target cache may get populated
+     before all targets are found.  While this is harmless (because all local
+     types are discovered and only in those case we devirtualize fully and we
+     don't do speculative devirtualization before IPA stage) it triggers
+     assert here when dumping at that stage also populates the case with
+     speculative targets.  Quietly ignore this.  */
+  gcc_assert (symtab->state < IPA_SSA || targets.length () <= len);
   fprintf (f, "\n");
 }
 
@@ -3035,11 +3378,13 @@ possible_polymorphic_call_target_p (tree otr_type,
   bool final;
 
   if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
-      && ((fcode = DECL_FUNCTION_CODE (n->decl))
-         == BUILT_IN_UNREACHABLE
+      && ((fcode = DECL_FUNCTION_CODE (n->decl)) == BUILT_IN_UNREACHABLE
           || fcode == BUILT_IN_TRAP))
     return true;
 
+  if (is_cxa_pure_virtual_p (n->decl))
+    return true;
+
   if (!odr_hash)
     return true;
   targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
@@ -3061,7 +3406,7 @@ possible_polymorphic_call_target_p (tree otr_type,
 
 bool
 possible_polymorphic_call_target_p (tree ref,
-                                   gimple stmt,
+                                   gimple *stmt,
                                    struct cgraph_node *n)
 {
   ipa_polymorphic_call_context context (current_function_decl, ref, stmt);
@@ -3088,13 +3433,12 @@ update_type_inheritance_graph (void)
   free_polymorphic_call_targets_hash ();
   timevar_push (TV_IPA_INHERITANCE);
   /* We reconstruct the graph starting from types of all methods seen in the
-     the unit.  */
+     unit.  */
   FOR_EACH_FUNCTION (n)
     if (DECL_VIRTUAL_P (n->decl)
        && !n->definition
        && n->real_symbol_p ())
-      get_odr_type (method_class_type (TYPE_MAIN_VARIANT (TREE_TYPE (n->decl))),
-                                      true);
+      get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
   timevar_pop (TV_IPA_INHERITANCE);
 }
 
@@ -3226,8 +3570,8 @@ ipa_devirt (void)
   if (warn_suggest_final_methods || warn_suggest_final_types)
     {
       final_warning_records = new (final_warning_record);
-      final_warning_records->type_warnings = vNULL;
-      final_warning_records->type_warnings.safe_grow_cleared (odr_types.length ());
+      final_warning_records->dyn_count = profile_count::zero ();
+      final_warning_records->grow_type_warnings (odr_types.length ());
       free_polymorphic_call_targets_hash ();
     }
 
@@ -3237,8 +3581,8 @@ ipa_devirt (void)
       if (!opt_for_fn (n->decl, flag_devirtualize))
        continue;
       if (dump_file && n->indirect_calls)
-       fprintf (dump_file, "\n\nProcesing function %s/%i\n",
-                n->name (), n->order);
+       fprintf (dump_file, "\n\nProcesing function %s\n",
+                n->dump_name ());
       for (e = n->indirect_calls; e; e = e->next_callee)
        if (e->indirect_info->polymorphic)
          {
@@ -3247,7 +3591,7 @@ ipa_devirt (void)
            bool final;
 
            if (final_warning_records)
-             final_warning_records->dyn_count = e->count;
+             final_warning_records->dyn_count = e->count.ipa ();
 
            vec <cgraph_node *>targets
               = possible_polymorphic_call_targets
@@ -3393,10 +3737,10 @@ ipa_devirt (void)
                   {
                     location_t locus = gimple_location_safe (e->call_stmt);
                     dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
-                                     "speculatively devirtualizing call in %s/%i to %s/%i\n",
-                                     n->name (), n->order,
-                                     likely_target->name (),
-                                     likely_target->order);
+                                    "speculatively devirtualizing call "
+                                    "in %s to %s\n",
+                                    n->dump_name (),
+                                    likely_target->dump_name ());
                   }
                if (!likely_target->can_be_discarded_p ())
                  {
@@ -3408,11 +3752,11 @@ ipa_devirt (void)
                nconverted++;
                update = true;
                e->make_speculative
-                 (likely_target, e->count * 8 / 10, e->frequency * 8 / 10);
+                 (likely_target, e->count.apply_scale (8, 10));
              }
          }
       if (update)
-       inline_update_overall_summary (n);
+       ipa_update_overall_fn_summary (n);
     }
   if (warn_suggest_final_methods || warn_suggest_final_types)
     {
@@ -3425,10 +3769,10 @@ ipa_devirt (void)
              {
                tree type = final_warning_records->type_warnings[i].type;
                int count = final_warning_records->type_warnings[i].count;
-               long long dyn_count
+               profile_count dyn_count
                  = final_warning_records->type_warnings[i].dyn_count;
 
-               if (!dyn_count)
+               if (!(dyn_count > 0))
                  warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
                             OPT_Wsuggest_final_types, count,
                             "Declaring type %qD final "
@@ -3448,13 +3792,13 @@ ipa_devirt (void)
                             "executed %lli times",
                             type,
                             count,
-                            dyn_count);
+                            (long long) dyn_count.to_gcov_type ());
              }
        }
 
       if (warn_suggest_final_methods)
        {
-         vec<const decl_warn_count*> decl_warnings_vec = vNULL;
+         auto_vec<const decl_warn_count*> decl_warnings_vec;
 
          final_warning_records->decl_warnings.traverse
            <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec);
@@ -3463,9 +3807,10 @@ ipa_devirt (void)
            {
              tree decl = decl_warnings_vec[i]->decl;
              int count = decl_warnings_vec[i]->count;
-             long long dyn_count = decl_warnings_vec[i]->dyn_count;
+             profile_count dyn_count
+                 = decl_warnings_vec[i]->dyn_count;
 
-             if (!dyn_count)
+             if (!(dyn_count > 0))
                if (DECL_CXX_DESTRUCTOR_P (decl))
                  warning_n (DECL_SOURCE_LOCATION (decl),
                              OPT_Wsuggest_final_methods, count,
@@ -3491,7 +3836,8 @@ ipa_devirt (void)
                              "Declaring virtual destructor of %qD final "
                              "would enable devirtualization of %i calls "
                              "executed %lli times",
-                             DECL_CONTEXT (decl), count, dyn_count);
+                             DECL_CONTEXT (decl), count,
+                             (long long)dyn_count.to_gcov_type ());
                else
                  warning_n (DECL_SOURCE_LOCATION (decl),
                              OPT_Wsuggest_final_methods, count,
@@ -3501,10 +3847,11 @@ ipa_devirt (void)
                              "Declaring method %qD final "
                              "would enable devirtualization of %i calls "
                              "executed %lli times",
-                             decl, count, dyn_count);
+                             decl, count,
+                             (long long)dyn_count.to_gcov_type ());
            }
        }
-       
+
       delete (final_warning_records);
       final_warning_records = 0;
     }