i386-expand.c (ix86_expand_floorceildf_32, [...]): Reorder functions.
[gcc.git] / gcc / ipa-devirt.c
index 09c3b5b045586ef42ded142369ddb8fb3030f1c2..6c651a3870575165a4e5acce5e936337408b97ef 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-2019 Free Software Foundation, Inc.
    Contributed by Jan Hubicka
 
 This file is part of GCC.
@@ -108,76 +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 "streamer-hooks.h"
-#include "lto-streamer.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)
   {
@@ -206,7 +173,10 @@ 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 void warn_odr (tree t1, tree t2, tree st1, tree st2,
+                     bool warn, bool *warned, const char *reason);
 
 static bool odr_violation_reported = false;
 
@@ -243,65 +213,10 @@ struct GTY(()) odr_type_d
   bool odr_violated;
   /* Set when virtual table without RTTI previaled table with.  */
   bool rtti_broken;
+  /* Set when the canonical type is determined using the type name.  */
+  bool tbaa_enabled;
 };
 
-/* Return true if T is a type with linkage defined.  */
-
-bool
-type_with_linkage_p (const_tree t)
-{
-  /* Builtin types do not define linkage, their TYPE_CONTEXT is NULL.  */
-  if (!TYPE_CONTEXT (t)
-      || !TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL)
-    return false;
-
-  return (RECORD_OR_UNION_TYPE_P (t)
-         || TREE_CODE (t) == ENUMERAL_TYPE);
-}
-
-/* Return true if T is in anonymous namespace.
-   This works only on those C++ types with linkage defined.  */
-
-bool
-type_in_anonymous_namespace_p (const_tree t)
-{
-  gcc_assert (type_with_linkage_p (t));
-
-  if (TYPE_STUB_DECL (t) && !TREE_PUBLIC (TYPE_STUB_DECL (t)))
-    {
-      if (DECL_ARTIFICIAL (TYPE_NAME (t)))
-       return true;
-      tree ctx = DECL_CONTEXT (TYPE_NAME (t));
-      while (ctx)
-       {
-         if (TREE_CODE (ctx) == NAMESPACE_DECL)
-           return !TREE_PUBLIC (ctx);
-         if (TREE_CODE (ctx) == BLOCK)
-           ctx = BLOCK_SUPERCONTEXT (ctx);
-         else
-           ctx = get_containing_scope (ctx);
-       }
-    }
-  return false;
-}
-
-/* Return true of T is type with One Definition Rule info attached. 
-   It means that either it is anonymous type or it has assembler name
-   set.  */
-
-bool
-odr_type_p (const_tree t)
-{
-  if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
-    return true;
-  /* We do not have this information when not in LTO, but we do not need
-     to care, since it is used only for type merging.  */
-  gcc_checking_assert (in_lto_p || flag_lto);
-
-  return (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
-          && (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t))));
-}
-
 /* Return TRUE if all derived types of T are known and thus
    we may consider the walk of derived type complete.
 
@@ -331,7 +246,7 @@ type_all_ctors_visible_p (tree t)
 {
   return !flag_ltrans
         && symtab->state >= CONSTRUCTION
-        /* We can not always use type_all_derivations_known_p.
+        /* We cannot always use type_all_derivations_known_p.
            For function local types we must assume case where
            the function is COMDAT and shared in between units.
 
@@ -363,38 +278,14 @@ 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 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.
-   This hash is needed to keep -fno-lto-odr-type-merging to work and contains
-   only polymorphic types.  Types with mangled names are inserted to both.  */
-
-struct odr_vtable_hasher:odr_name_hasher
-{
-  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
-   qualified variant of it.  */
-
-static inline tree
-main_odr_variant (const_tree t)
-{
-  if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
-    return TREE_TYPE (TYPE_NAME (t));
-  /* Unnamed types and non-C++ produced types can be compared by variants.  */
-  else
-    return TYPE_MAIN_VARIANT (t);
-}
-
 static bool
 can_be_name_hashed_p (tree t)
 {
@@ -406,7 +297,7 @@ can_be_name_hashed_p (tree t)
 static hashval_t
 hash_odr_name (const_tree t)
 {
-  gcc_checking_assert (main_odr_variant (t) == t);
+  gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
 
   /* If not in LTO, all main variants are unique, so we can do
      pointer hash.  */
@@ -430,51 +321,6 @@ odr_name_hasher::hash (const odr_type_d *odr_type)
   return hash_odr_name (odr_type->type);
 }
 
-static bool
-can_be_vtable_hashed_p (tree t)
-{
-  /* vtable hashing can distinguish only main variants.  */
-  if (TYPE_MAIN_VARIANT (t) != t)
-    return false;
-  /* Anonymous namespace types are always handled by name hash.  */
-  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)));
-}
-
-/* Hash type by assembler name of its vtable.  */
-
-static hashval_t
-hash_odr_vtable (const_tree t)
-{
-  tree v = BINFO_VTABLE (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
-  inchash::hash hstate;
-
-  gcc_checking_assert (in_lto_p);
-  gcc_checking_assert (!type_in_anonymous_namespace_p (t));
-  gcc_checking_assert (TREE_CODE (t) == RECORD_TYPE
-                      && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
-  gcc_checking_assert (main_odr_variant (t) == t);
-
-  if (TREE_CODE (v) == POINTER_PLUS_EXPR)
-    {
-      add_expr (TREE_OPERAND (v, 1), hstate);
-      v = TREE_OPERAND (TREE_OPERAND (v, 0), 0);
-    }
-
-  hstate.add_wide_int (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 odr_type_d *odr_type)
-{
-  return hash_odr_vtable (odr_type->type);
-}
-
 /* For languages with One Definition Rule, work out if
    types are the same based on their name.
 
@@ -484,24 +330,15 @@ odr_vtable_hasher::hash (const odr_type_d *odr_type)
 
    Until we start streaming mangled type names, this function works
    only for polymorphic types.
-
-   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.
 */
 
 bool
-types_same_for_odr (const_tree type1, const_tree type2, bool strict)
+types_same_for_odr (const_tree type1, const_tree type2)
 {
   gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
 
-  type1 = main_odr_variant (type1);
-  type2 = main_odr_variant (type2);
-  if (!strict)
-    {
-      type1 = TYPE_MAIN_VARIANT (type1);
-      type2 = TYPE_MAIN_VARIANT (type2);
-    }
+  type1 = TYPE_MAIN_VARIANT (type1);
+  type2 = TYPE_MAIN_VARIANT (type2);
 
   if (type1 == type2)
     return true;
@@ -509,66 +346,11 @@ types_same_for_odr (const_tree type1, const_tree type2, bool strict)
   if (!in_lto_p)
     return false;
 
-  /* Check for anonymous namespaces. Those have !TREE_PUBLIC
-     on the corresponding TYPE_STUB_DECL.  */
+  /* Anonymous namespace types are never duplicated.  */
   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;
 
-
-  /* ODR name of the type is set in DECL_ASSEMBLER_NAME of its TYPE_NAME.
-
-     Ideally we should never need types without ODR names here.  It can however
-     happen in two cases:
-
-       1) for builtin types that are not streamed but rebuilt in lto/lto-lang.c
-          Here testing for equivalence is safe, since their MAIN_VARIANTs are
-          unique.
-       2) for units streamed with -fno-lto-odr-type-merging.  Here we can't
-         establish precise ODR equivalency, but for correctness we care only
-         about equivalency on complete polymorphic types.  For these we can
-         compare assembler names of their virtual tables.  */
-  if ((!TYPE_NAME (type1) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type1)))
-      || (!TYPE_NAME (type2) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type2))))
-    {
-      /* See if types are obviously different (i.e. different codes
-        or polymorphic wrt non-polymorphic).  This is not strictly correct
-        for ODR violating programs, but we can't do better without streaming
-        ODR names.  */
-      if (TREE_CODE (type1) != TREE_CODE (type2))
-       return false;
-      if (TREE_CODE (type1) == RECORD_TYPE
-         && (TYPE_BINFO (type1) == NULL_TREE)
-             != (TYPE_BINFO (type1) == NULL_TREE))
-       return false;
-      if (TREE_CODE (type1) == RECORD_TYPE && TYPE_BINFO (type1)
-         && (BINFO_VTABLE (TYPE_BINFO (type1)) == NULL_TREE)
-            != (BINFO_VTABLE (TYPE_BINFO (type2)) == NULL_TREE))
-       return false;
-
-      /* At the moment we have no way to establish ODR equivalence at LTO
-        other than comparing virtual table pointers of polymorphic types.
-        Eventually we should start saving mangled names in TYPE_NAME.
-        Then this condition will become non-trivial.  */
-
-      if (TREE_CODE (type1) == RECORD_TYPE
-         && TYPE_BINFO (type1) && TYPE_BINFO (type2)
-         && BINFO_VTABLE (TYPE_BINFO (type1))
-         && BINFO_VTABLE (TYPE_BINFO (type2)))
-       {
-         tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
-         tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
-         gcc_assert (TREE_CODE (v1) == POINTER_PLUS_EXPR
-                     && TREE_CODE (v2) == POINTER_PLUS_EXPR);
-         return (operand_equal_p (TREE_OPERAND (v1, 1),
-                                  TREE_OPERAND (v2, 1), 0)
-                 && DECL_ASSEMBLER_NAME
-                        (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
-                    == DECL_ASSEMBLER_NAME
-                        (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
-       }
-      gcc_unreachable ();
-    }
   return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1))
          == DECL_ASSEMBLER_NAME (TYPE_NAME (type2)));
 }
@@ -576,21 +358,15 @@ types_same_for_odr (const_tree type1, const_tree type2, bool strict)
 /* Return true if we can decide on ODR equivalency.
 
    In non-LTO it is always decide, in LTO however it depends in the type has
-   ODR info attached.
-
-   When STRICT is false, compare main variants.  */
+   ODR info attached. */
 
 bool
-types_odr_comparable (tree t1, tree t2, bool strict)
+types_odr_comparable (tree t1, tree t2)
 {
   return (!in_lto_p
-         || (strict ? main_odr_variant (t1) == main_odr_variant (t2)
-             : 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
-             && TYPE_BINFO (t1) && TYPE_BINFO (t2)
-             && polymorphic_type_binfo_p (TYPE_BINFO (t1))
-             && polymorphic_type_binfo_p (TYPE_BINFO (t2))));
+         || TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)
+         || (odr_type_p (TYPE_MAIN_VARIANT (t1))
+             && odr_type_p (TYPE_MAIN_VARIANT (t2))));
 }
 
 /* Return true if T1 and T2 are ODR equivalent.  If ODR equivalency is not
@@ -631,7 +407,7 @@ odr_or_derived_type_p (const_tree t)
 {
   do
     {
-      if (odr_type_p (t))
+      if (odr_type_p (TYPE_MAIN_VARIANT (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.
@@ -646,7 +422,7 @@ odr_or_derived_type_p (const_tree t)
             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)))
+              if (odr_or_derived_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (t))))
                 return true;
             return false;
           }
@@ -666,14 +442,13 @@ odr_name_hasher::equal (const odr_type_d *o1, const tree_node *t2)
 {
   tree t1 = o1->type;
 
-  gcc_checking_assert (main_odr_variant (t2) == t2);
-  gcc_checking_assert (main_odr_variant (t1) == t1);
+  gcc_checking_assert (TYPE_MAIN_VARIANT (t2) == t2);
+  gcc_checking_assert (TYPE_MAIN_VARIANT (t1) == t1);
   if (t1 == t2)
     return true;
   if (!in_lto_p)
     return false;
-  /* Check for anonymous namespaces. Those have !TREE_PUBLIC
-     on the corresponding TYPE_STUB_DECL.  */
+  /* Check for anonymous namespaces.  */
   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;
@@ -683,31 +458,6 @@ odr_name_hasher::equal (const odr_type_d *o1, const tree_node *t2)
          == DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
 }
 
-/* Compare types T1 and T2 and return true if they are
-   equivalent.  */
-
-inline bool
-odr_vtable_hasher::equal (const odr_type_d *o1, const tree_node *t2)
-{
-  tree t1 = o1->type;
-
-  gcc_checking_assert (main_odr_variant (t2) == t2);
-  gcc_checking_assert (main_odr_variant (t1) == t1);
-  gcc_checking_assert (in_lto_p);
-  t1 = TYPE_MAIN_VARIANT (t1);
-  t2 = TYPE_MAIN_VARIANT (t2);
-  if (t1 == t2)
-    return true;
-  tree v1 = BINFO_VTABLE (TYPE_BINFO (t1));
-  tree v2 = BINFO_VTABLE (TYPE_BINFO (t2));
-  return (operand_equal_p (TREE_OPERAND (v1, 1),
-                          TREE_OPERAND (v2, 1), 0)
-         && DECL_ASSEMBLER_NAME
-                (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
-            == DECL_ASSEMBLER_NAME
-                (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
-}
-
 /* Free ODR type V.  */
 
 inline void
@@ -724,8 +474,6 @@ odr_name_hasher::remove (odr_type_d *v)
 
 typedef hash_table<odr_name_hasher> odr_hash_type;
 static odr_hash_type *odr_hash;
-typedef hash_table<odr_vtable_hasher> odr_vtable_hash_type;
-static odr_vtable_hash_type *odr_vtable_hash;
 
 /* ODR types are also stored into ODR_TYPE vector to allow consistent
    walking.  Bases appear before derived types.  Vector is garbage collected
@@ -745,36 +493,63 @@ set_type_binfo (tree type, tree binfo)
       gcc_assert (!TYPE_BINFO (type));
 }
 
-/* Compare T2 and T2 based on name or structure.  */
+/* Return true if type variants match.
+   This assumes that we already verified that T1 and T2 are variants of the
+   same type.  */
+
+static bool
+type_variants_equivalent_p (tree t1, tree t2)
+{
+  if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
+    return false;
+
+  if (comp_type_attributes (t1, t2) != 1)
+    return false;
+
+  if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
+      && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
+    return false;
+
+  return true;
+}
+
+/* Compare T1 and T2 based on name or structure.  */
 
 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)
 {
 
   /* This can happen in incomplete types that should be handled earlier.  */
   gcc_assert (t1 && t2);
 
-  t1 = main_odr_variant (t1);
-  t2 = main_odr_variant (t2);
   if (t1 == t2)
     return true;
 
   /* Anonymous namespace types must match exactly.  */
-  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 (TYPE_MAIN_VARIANT (t1))
+       && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
+      || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2))
+         && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2))))
     return false;
 
   /* For ODR types be sure to compare their names.
-     To support -wno-odr-type-merging we allow one type to be non-ODR
+     To support -Wno-odr-type-merging we allow one type to be non-ODR
      and other ODR even though it is a violation.  */
-  if (types_odr_comparable (t1, t2, true))
+  if (types_odr_comparable (t1, t2))
     {
-      if (!types_same_for_odr (t1, t2, true))
+      if (t1 != t2
+         && odr_type_p (TYPE_MAIN_VARIANT (t1))
+         && get_odr_type (TYPE_MAIN_VARIANT (t1), true)->odr_violated)
+       return false;
+      if (!types_same_for_odr (t1, t2))
         return false;
+      if (!type_variants_equivalent_p (t1, t2))
+       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)
+        that they are same, be happy.  */
+      if (odr_type_p (TYPE_MAIN_VARIANT (t1)))
         return true;
     }
 
@@ -786,15 +561,43 @@ odr_subtypes_equivalent_p (tree t1, tree t2,
       && (TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
     return false;
 
-  type_pair pair={t1,t2};
-  if (TYPE_UID (t1) > TYPE_UID (t2))
+  type_pair pair={TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2)};
+  if (TYPE_UID (TYPE_MAIN_VARIANT (t1)) > TYPE_UID (TYPE_MAIN_VARIANT (t2)))
     {
-      pair.first = t2;
-      pair.second = t1;
+      pair.first = TYPE_MAIN_VARIANT (t2);
+      pair.second = TYPE_MAIN_VARIANT (t1);
     }
   if (visited->add (pair))
     return true;
-  return odr_types_equivalent_p (t1, t2, false, NULL, visited);
+  if (!odr_types_equivalent_p (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2),
+                             false, NULL, visited, loc1, loc2))
+    return false;
+  if (!type_variants_equivalent_p (t1, t2))
+    return false;
+  return true;
+}
+
+/* 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
@@ -814,6 +617,7 @@ compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
          prevailing = vtable;
          vtable = tmp;
        }
+      auto_diagnostic_group d;
       if (warning_at (DECL_SOURCE_LOCATION
                        (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
                      OPT_Wodr,
@@ -850,35 +654,37 @@ 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)
+                || (methods_equal_p (ref1->referred->decl,
+                                     ref2->referred->decl)
                     && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL))
             && TREE_CODE (ref2->referred->decl) != FUNCTION_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)))
+         if (!class_type->rtti_broken)
            {
-             inform (DECL_SOURCE_LOCATION
-                       (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
-                     "but is prevailed by one without from other translation "
-                     "unit");
-             inform (DECL_SOURCE_LOCATION
-                       (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
-                     "RTTI will not work on this type");
-             class_type->rtti_broken = true;
+             auto_diagnostic_group d;
+             if (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))),
+                           "but is prevailed by one without from other"
+                           " translation unit");
+                 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)
+                || (methods_equal_p (ref2->referred->decl, ref1->referred->decl)
                     && TREE_CODE (ref2->referred->decl) == FUNCTION_DECL))
             && TREE_CODE (ref1->referred->decl) != FUNCTION_DECL)
        {
@@ -897,17 +703,17 @@ compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
          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))),
-                             OPT_Wodr,
+             auto_diagnostic_group d;
+             tree ctx = TYPE_NAME (DECL_CONTEXT (vtable->decl));
+             if (warning_at (DECL_SOURCE_LOCATION (ctx), OPT_Wodr,
                              "virtual table of type %qD violates "
-                             "one definition rule  ",
+                             "one definition rule",
                              DECL_CONTEXT (vtable->decl)))
                {
-                 inform (DECL_SOURCE_LOCATION
-                           (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
-                         "the conflicting type defined in another translation "
-                         "unit has virtual table of different size");
+                 ctx = TYPE_NAME (DECL_CONTEXT (prevailing->decl));
+                 inform (DECL_SOURCE_LOCATION (ctx),
+                         "the conflicting type defined in another translation"
+                         " unit has virtual table of different size");
                }
            }
          return;
@@ -915,8 +721,7 @@ 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;
@@ -926,11 +731,12 @@ compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
          if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL
              && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
            {
+             auto_diagnostic_group d;
              if (warning_at (DECL_SOURCE_LOCATION
                                (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
                              OPT_Wodr,
                              "virtual table of type %qD violates "
-                             "one definition rule  ",
+                             "one definition rule",
                              DECL_CONTEXT (vtable->decl)))
                {
                  inform (DECL_SOURCE_LOCATION
@@ -947,9 +753,9 @@ compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
          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;
            }
        }
@@ -967,6 +773,7 @@ compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
              vtable = tmp;
              ref1 = ref2;
            }
+         auto_diagnostic_group d;
          if (warning_at (DECL_SOURCE_LOCATION
                            (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
                          OPT_Wodr,
@@ -990,7 +797,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;
@@ -998,10 +805,11 @@ 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.  */
+      auto_diagnostic_group d;
       if (warning_at (DECL_SOURCE_LOCATION
                        (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr,
                      "virtual table of type %qD violates "
-                     "one definition rule  ",
+                     "one definition rule",
                      DECL_CONTEXT (vtable->decl)))
        {
          if (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL)
@@ -1012,17 +820,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;
        }
     }
@@ -1034,15 +845,15 @@ compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
    If WARN is false, do nothing. Set WARNED if warning was indeed
    output.  */
 
-void
+static void
 warn_odr (tree t1, tree t2, tree st1, tree st2,
          bool warn, bool *warned, const char *reason)
 {
-  tree decl2 = TYPE_NAME (t2);
+  tree decl2 = TYPE_NAME (TYPE_MAIN_VARIANT (t2));
   if (warned)
     *warned = false;
 
-  if (!warn || !TYPE_NAME(t1))
+  if (!warn || !TYPE_NAME(TYPE_MAIN_VARIANT (t1)))
     return;
 
   /* ODR warnings are output druing LTO streaming; we must apply location
@@ -1050,10 +861,23 @@ warn_odr (tree t1, tree t2, tree st1, tree st2,
   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",
-                  t1))
-    return;
+  auto_diagnostic_group d;
+  if (t1 != TYPE_MAIN_VARIANT (t1)
+      && TYPE_NAME (t1) != TYPE_NAME (TYPE_MAIN_VARIANT (t1)))
+    {
+      if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1))),
+                      OPT_Wodr, "type %qT (typedef of %qT) violates the "
+                      "C++ One Definition Rule",
+                      t1, TYPE_MAIN_VARIANT (t1)))
+       return;
+    }
+  else
+    {
+      if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1))),
+                      OPT_Wodr, "type %qT violates the C++ One Definition Rule",
+                      t1))
+       return;
+    }
   if (!st1 && !st2)
     ;
   /* For FIELD_DECL support also case where one of fields is
@@ -1091,69 +915,142 @@ 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 types have names and they are different, it is most informative to
-     output those.  */
-  if (TYPE_NAME (t1) && TYPE_NAME (t2)
-      && 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)))
+  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 (TYPE_MAIN_VARIANT (t1))
+       && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
+      || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2))
+         && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2))))
+    {
+      if (!type_with_linkage_p (TYPE_MAIN_VARIANT (t1))
+         || !type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
+       {
+         std::swap (t1, t2);
+         std::swap (loc_t1, loc_t2);
+       }
+      gcc_assert (TYPE_NAME (t1)
+                 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL);
+      tree n1 = TYPE_NAME (t1);
+      tree n2 = TYPE_NAME (t2) ? TYPE_NAME (t2) : NULL;
+
+      if (TREE_CODE (n1) == TYPE_DECL)
+       n1 = DECL_NAME (n1);
+      if (n2 && TREE_CODE (n2) == TYPE_DECL)
+       n2 = DECL_NAME (n2);
+      /* Most of the time, the type names will match, do not be unnecesarily
+         verbose.  */
+      if (n1 != n2)
+        inform (loc_t1,
+               "type %qT defined in anonymous namespace cannot match "
+               "type %qT across the translation unit boundary",
+               t1, t2);
+      else
+        inform (loc_t1,
+               "type %qT defined in anonymous namespace cannot match "
+               "across the translation unit boundary",
+               t1);
+      if (loc_t2_useful)
+        inform (loc_t2,
+               "the incompatible type defined in another translation unit");
+      return;
+    }
+  tree mt1 = TYPE_MAIN_VARIANT (t1);
+  tree mt2 = TYPE_MAIN_VARIANT (t2);
+  /* 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 (mt1) && TYPE_NAME (mt2)
+      && TREE_CODE (TYPE_NAME (mt1)) == TYPE_DECL
+      && TREE_CODE (TYPE_NAME (mt2)) == TYPE_DECL
+      && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (mt1))
+      && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (mt2))
+      && DECL_ASSEMBLER_NAME (TYPE_NAME (mt1))
+        != DECL_ASSEMBLER_NAME (TYPE_NAME (mt2)))
     {
       char *name1 = xstrdup (cplus_demangle
-        (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))),
+        (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (mt1))),
          DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES));
       char *name2 = cplus_demangle
-        (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t2))),
+        (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (mt2))),
          DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES);
       if (name1 && name2 && strcmp (name1, name2))
        {
-         inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
-                 "type name %<%s%> should match type name %<%s%>",
+         inform (loc_t1,
+                 "type name %qs should match type name %qs",
                  name1, name2);
-         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");
          free (name1);
          return;
        }
       free (name1);
     }
-  /* 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))
-       {
-         tree tmp = t1;;
-         t1 = t2;
-         t2 = tmp;
-       }
-      if (TYPE_NAME (t1) && TYPE_NAME (t2)
-         && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
-         && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL)
-       {
-         inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
-                 "type %qT defined in anonymous namespace can not match "
-                 "type %qT",
-                 t1, t2);
-         inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
-                 "the incompatible type defined in anonymous namespace in "
-                 "another translation unit");
-       }
-      else
-       inform (UNKNOWN_LOCATION,
-               "types in anonymous namespace does not match across "
-               "translation unit boundary");
-      return;
-    }
-  /* A tricky case are component types.  Often they appear the same in source
+  /* 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
@@ -1162,7 +1059,6 @@ warn_types_mismatch (tree t1, tree t2)
     {
       if (TREE_CODE (t1) == TREE_CODE (t2))
        {
-         hash_set<type_pair,pair_traits> visited;
          if (TREE_CODE (t1) == ARRAY_TYPE
              && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
            {
@@ -1175,27 +1071,25 @@ warn_types_mismatch (tree t1, tree t2)
                  && !operand_equal_p (TYPE_MAX_VALUE (i1),
                                       TYPE_MAX_VALUE (i2), 0))
                {
-                 inform (UNKNOWN_LOCATION,
+                 inform (loc,
                          "array types have different bounds");
                  return;
                }
            }
          if ((POINTER_TYPE_P (t1) || TREE_CODE (t1) == ARRAY_TYPE)
-             && !odr_subtypes_equivalent_p (TREE_TYPE (t1),
-                                            TREE_TYPE (t2),
-                                            &visited))
-           warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
+             && 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 (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
-                                             &visited))
+             if (type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
                {
-                 inform (UNKNOWN_LOCATION, "return value type mismatch");
-                 warn_types_mismatch (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))
@@ -1204,24 +1098,24 @@ warn_types_mismatch (tree t1, tree t2)
                     parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2),
                     count++)
                  {
-                   if (!odr_subtypes_equivalent_p
-                       (TREE_VALUE (parms1), TREE_VALUE (parms2), &visited))
+                   if (type_mismatch_p (TREE_VALUE (parms1), TREE_VALUE (parms2)))
                      {
                        if (count == 1 && TREE_CODE (t1) == METHOD_TYPE)
-                         inform (UNKNOWN_LOCATION,
+                         inform (loc,
                                  "implicit this pointer type mismatch");
                        else
-                         inform (UNKNOWN_LOCATION,
+                         inform (loc,
                                  "type mismatch in parameter %i",
                                  count - (TREE_CODE (t1) == METHOD_TYPE));
                        warn_types_mismatch (TREE_VALUE (parms1),
-                                            TREE_VALUE (parms2));
+                                            TREE_VALUE (parms2),
+                                            loc_t1, loc_t2);
                        return;
                      }
                  }
              if (parms1 || parms2)
                {
-                 inform (UNKNOWN_LOCATION,
+                 inform (loc,
                          "types have different parameter counts");
                  return;
                }
@@ -1229,52 +1123,60 @@ warn_types_mismatch (tree t1, tree t2)
        }
       return;
     }
-  /* This should not happen but if it does, the warning would not be helpful.
-     TODO: turn it into assert next stage1.  */
-  if (TYPE_NAME (t1) == TYPE_NAME (t2))
+
+  if (types_odr_comparable (t1, t2)
+      /* We make assign integers mangled names to be able to handle
+        signed/unsigned chars.  Accepting them here would however lead to
+        confussing message like
+        "type â€˜const int’ itself violates the C++ One Definition Rule"  */
+      && TREE_CODE (t1) != INTEGER_TYPE
+      && types_same_for_odr (t1, t2))
+    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 if (types_odr_comparable (t1, t2, true)
-          && types_same_for_odr (t1, t2, true))
-    inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
-           "type %qT should match type %qT that itself violate "
-           "one definition rule",
-           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);
-  if (DECL_SOURCE_LOCATION (TYPE_NAME (t2)) > BUILTINS_LOCATION)
-    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");
+}
+
+/* Return true if T should be ignored in TYPE_FIELDS for ODR comparsion.  */
+
+static bool
+skip_in_fields_list_p (tree t)
+{
+  if (TREE_CODE (t) != FIELD_DECL)
+    return true;
+  /* C++ FE introduces zero sized fields depending on -std setting, see
+     PR89358.  */
+  if (DECL_SIZE (t)
+      && integer_zerop (DECL_SIZE (t))
+      && DECL_ARTIFICIAL (t)
+      && DECL_IGNORED_P (t)
+      && !DECL_NAME (t))
+    return true;
+  return false;
 }
 
 /* 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_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))
@@ -1284,32 +1186,18 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
       return false;
     }
 
-  if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
-    {
-      warn_odr (t1, t2, NULL, NULL, warn, warned,
-               G_("a type with different qualifiers is defined in another "
-                  "translation unit"));
-      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)))
+  if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1))
+       && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
+      || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2))
+         && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2))))
     {
-      /* We can not trip this when comparing ODR types, only when trying to
+      /* We cannot 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 different attributes "
-                  "is defined in another translation unit"));
-      return false;
-    }
-
   if (TREE_CODE (t1) == ENUMERAL_TYPE
       && TYPE_VALUES (t1) && TYPE_VALUES (t2))
     {
@@ -1324,9 +1212,7 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
                           " is defined in another translation unit"));
              return false;
            }
-         if (TREE_VALUE (v1) != TREE_VALUE (v2)
-             && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1)),
-                                  DECL_INITIAL (TREE_VALUE (v2)), 0))
+         if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2), 0))
            {
              warn_odr (t1, t2, NULL, NULL, warn, warned,
                        G_("an enum with different values is defined"
@@ -1392,26 +1278,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;
        }
     }
@@ -1422,13 +1311,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)
@@ -1440,7 +1330,7 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
        /* For an incomplete external array, the type domain can be
           NULL_TREE.  Check this condition also.  */
        if (i1 == NULL_TREE || i2 == NULL_TREE)
-         return true;
+          return type_variants_equivalent_p (t1, t2);
 
        tree min1 = TYPE_MIN_VALUE (i1);
        tree min2 = TYPE_MIN_VALUE (i2);
@@ -1463,19 +1353,20 @@ 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)
          || !prototype_p (t1) || !prototype_p (t2))
-       return true;
+        return type_variants_equivalent_p (t1, t2);
       else
        {
          tree parms1, parms2;
@@ -1485,14 +1376,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;
                }
            }
@@ -1505,7 +1397,7 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
              return false;
            }
 
-         return true;
+          return type_variants_equivalent_p (t1, t2);
        }
 
     case RECORD_TYPE:
@@ -1536,9 +1428,9 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
                 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
              {
                /* Skip non-fields.  */
-               while (f1 && TREE_CODE (f1) != FIELD_DECL)
+               while (f1 && skip_in_fields_list_p (f1))
                  f1 = TREE_CHAIN (f1);
-               while (f2 && TREE_CODE (f2) != FIELD_DECL)
+               while (f2 && skip_in_fields_list_p (f2))
                  f2 = TREE_CHAIN (f2);
                if (!f1 || !f2)
                  break;
@@ -1565,7 +1457,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.  */
@@ -1576,7 +1469,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))
@@ -1586,12 +1479,20 @@ 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;
                  }
-               gcc_assert (DECL_NONADDRESSABLE_P (f1)
-                           == DECL_NONADDRESSABLE_P (f2));
+               if (DECL_BIT_FIELD (f1) != DECL_BIT_FIELD (f2))
+                 {
+                   warn_odr (t1, t2, f1, f2, warn, warned,
+                             G_("one field is a bitfield while the other "
+                                "is not"));
+                   return false;
+                 }
+               else
+                 gcc_assert (DECL_NONADDRESSABLE_P (f1)
+                             == DECL_NONADDRESSABLE_P (f2));
              }
 
            /* If one aggregate has more fields than the other, they
@@ -1614,61 +1515,6 @@ 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)
-               && COMPLETE_TYPE_P (TYPE_MAIN_VARIANT (t1))
-               && COMPLETE_TYPE_P (TYPE_MAIN_VARIANT (t2))
-               && odr_type_p (TYPE_MAIN_VARIANT (t1))
-               && odr_type_p (TYPE_MAIN_VARIANT (t2))
-               && (TYPE_METHODS (TYPE_MAIN_VARIANT (t1))
-                   != TYPE_METHODS (TYPE_MAIN_VARIANT (t2))))
-             {
-               /* Currently free_lang_data sets TYPE_METHODS to error_mark_node
-                  if it is non-NULL so this loop will never realy execute.  */
-               if (TYPE_METHODS (TYPE_MAIN_VARIANT (t1)) != error_mark_node
-                   && TYPE_METHODS (TYPE_MAIN_VARIANT (t2)) != error_mark_node)
-                 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 == NULL) != (f2 == NULL))
-                 {
-                   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;
       }
@@ -1690,18 +1536,11 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
                   "is defined in another translation unit"));
       return false;
     }
-  if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
-      && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
-    {
-      warn_odr (t1, t2, NULL, NULL, warn, warned,
-               G_("a type with different alignment "
-                  "is defined in another translation unit"));
-      return false;
-    }
+
   gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2)
              || operand_equal_p (TYPE_SIZE_UNIT (t1),
                                  TYPE_SIZE_UNIT (t2), 0));
-  return true;
+  return type_variants_equivalent_p (t1, t2);
 }
 
 /* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule.  */
@@ -1709,13 +1548,12 @@ odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
 bool
 odr_types_equivalent_p (tree type1, tree type2)
 {
-  hash_set<type_pair,pair_traits> visited;
+  gcc_checking_assert (odr_or_derived_type_p (type1)
+                      && odr_or_derived_type_p (type2));
 
-#ifdef ENABLE_CHECKING
-  gcc_assert (odr_or_derived_type_p (type1) && odr_or_derived_type_p (type2));
-#endif
+  hash_set<type_pair> visited;
   return odr_types_equivalent_p (type1, type2, false, NULL,
-                                &visited);
+                                &visited, UNKNOWN_LOCATION, UNKNOWN_LOCATION);
 }
 
 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
@@ -1751,7 +1589,8 @@ add_type_duplicate (odr_type val, tree type)
   else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type))
     {
       prevail = true;
-      build_bases = TYPE_BINFO (type);
+      if (TREE_CODE (type) == RECORD_TYPE)
+        build_bases = TYPE_BINFO (type);
     }
   else if (COMPLETE_TYPE_P (val->type) && !COMPLETE_TYPE_P (type))
     ;
@@ -1769,27 +1608,21 @@ 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);
 
-  /* If we now have a mangled name, be sure to record it to val->type
-     so ODR hash can work.  */
+  if (!odr_hash)
+    return false;
 
-  if (can_be_name_hashed_p (type) && !can_be_name_hashed_p (val->type))
-    SET_DECL_ASSEMBLER_NAME (TYPE_NAME (val->type),
-                            DECL_ASSEMBLER_NAME (TYPE_NAME (type)));
+  gcc_checking_assert (can_be_name_hashed_p (type)
+                      && can_be_name_hashed_p (val->type));
 
   bool merge = true;
   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);
@@ -1853,7 +1686,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;
              }
@@ -1914,28 +1748,31 @@ add_type_duplicate (odr_type val, tree type)
        }
     }
 
-  /* Next compare memory layout.  */
-  if (!odr_types_equivalent_p (val->type, 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 ();
+  /* As a special case we stream mangles names of integer types so we can see
+     if they are believed to be same even though they have different
+     representation.  Avoid bogus warning on mismatches in these.  */
+  if (TREE_CODE (type) != INTEGER_TYPE
+      && TREE_CODE (val->type) != INTEGER_TYPE
+      && !odr_types_equivalent_p (val->type, type,
                               !flag_ltrans && !val->odr_violated && !warned,
-                              &warned, &visited))
+                              &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;
-      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);
-       }
     }
   gcc_assert (val->odr_violated || !odr_must_violate);
   /* Sanity check that all bases will be build same way again.  */
-#ifdef ENABLE_CHECKING
-  if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
+  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)
@@ -1964,7 +1801,6 @@ add_type_duplicate (odr_type val, tree type)
            j++;
          }
     }
-#endif
 
 
   /* Regularize things a little.  During LTO same types may come with
@@ -2028,6 +1864,30 @@ add_type_duplicate (odr_type val, tree type)
   return build_bases;
 }
 
+/* REF is OBJ_TYPE_REF, return the class the ref corresponds to.  */
+
+tree
+obj_type_ref_class (const_tree ref)
+{
+  gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
+  ref = TREE_TYPE (ref);
+  gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
+  ref = TREE_TYPE (ref);
+  /* We look for type THIS points to.  ObjC also builds
+     OBJ_TYPE_REF with non-method calls, Their first parameter
+     ID however also corresponds to class type. */
+  gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
+                      || TREE_CODE (ref) == FUNCTION_TYPE);
+  ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
+  gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
+  tree ret = TREE_TYPE (ref);
+  if (!in_lto_p && !TYPE_STRUCTURAL_EQUALITY_P (ret))
+    ret = TYPE_CANONICAL (ret);
+  else
+    ret = get_odr_type (ret)->type;
+  return ret;
+}
+
 /* Get ODR type hash entry for TYPE.  If INSERT is true, create
    possibly new entry.  */
 
@@ -2035,77 +1895,33 @@ odr_type
 get_odr_type (tree type, bool insert)
 {
   odr_type_d **slot = NULL;
-  odr_type_d **vtable_slot = NULL;
   odr_type val = NULL;
   hashval_t hash;
   bool build_bases = false;
   bool insert_to_odr_array = false;
   int base_id = -1;
 
-  type = main_odr_variant (type);
+  type = TYPE_MAIN_VARIANT (type);
+  if (!in_lto_p && !TYPE_STRUCTURAL_EQUALITY_P (type))
+    type = TYPE_CANONICAL (type);
 
-  gcc_checking_assert (can_be_name_hashed_p (type)
-                      || can_be_vtable_hashed_p (type));
+  gcc_checking_assert (can_be_name_hashed_p (type));
 
-  /* Lookup entry, first try name hash, fallback to vtable hash.  */
-  if (can_be_name_hashed_p (type))
-    {
-      hash = hash_odr_name (type);
-      slot = odr_hash->find_slot_with_hash (type, hash,
-                                           insert ? INSERT : NO_INSERT);
-    }
-  if ((!slot || !*slot) && 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,
-                                                  insert ? INSERT : NO_INSERT);
-    }
+  hash = hash_odr_name (type);
+  slot = odr_hash->find_slot_with_hash (type, hash,
+                                       insert ? INSERT : NO_INSERT);
 
-  if (!slot && !vtable_slot)
+  if (!slot)
     return NULL;
 
   /* See if we already have entry for type.  */
-  if ((slot && *slot) || (vtable_slot && *vtable_slot))
+  if (*slot)
     {
-      if (slot && *slot)
-       {
-         val = *slot;
-#ifdef ENABLE_CHECKING
-         if (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,
-                                                                 NO_INSERT);
-             gcc_assert (!vtable_slot || *vtable_slot == *slot);
-             vtable_slot = NULL;
-           }
-#endif
-       }
-      else if (*vtable_slot)
-       val = *vtable_slot;
+      val = *slot;
 
-      if (val->type != type
+      if (val->type != type && insert
          && (!val->types_set || !val->types_set->add (type)))
-       {
-         gcc_assert (insert);
-         /* We have type duplicate, but it may introduce vtable name or
-            mangled name; be sure to keep hashes in sync.  */
-         if (in_lto_p && can_be_vtable_hashed_p (type)
-             && (!vtable_slot || !*vtable_slot))
-           {
-             if (!vtable_slot)
-               {
-                 hash = hash_odr_vtable (type);
-                 vtable_slot = odr_vtable_hash->find_slot_with_hash
-                            (type, hash, INSERT);
-                 gcc_checking_assert (!*vtable_slot || *vtable_slot == val);
-               }
-             *vtable_slot = val;
-           }
-         if (slot && !*slot)
-           *slot = val;
-         build_bases = add_type_duplicate (val, type);
-       }
+       build_bases = add_type_duplicate (val, type);
     }
   else
     {
@@ -2119,10 +1935,7 @@ get_odr_type (tree type, bool insert)
        val->anonymous_namespace = 0;
       build_bases = COMPLETE_TYPE_P (val->type);
       insert_to_odr_array = true;
-      if (slot)
-        *slot = val;
-      if (vtable_slot)
-       *vtable_slot = val;
+      *slot = val;
     }
 
   if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
@@ -2137,7 +1950,7 @@ get_odr_type (tree type, bool insert)
       val->all_derivations_known = type_all_derivations_known_p (type);
       for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
        /* For now record only polymorphic types. other are
-          pointless for devirtualization and we can not precisely
+          pointless for devirtualization and we cannot precisely
           determine ODR equivalency of these during LTO.  */
        if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
          {
@@ -2163,34 +1976,110 @@ 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;
 }
 
+/* Return type that in ODR type hash prevailed TYPE.  Be careful and punt
+   on ODR violations.  */
+
+tree
+prevailing_odr_type (tree type)
+{
+  odr_type t = get_odr_type (type, false);
+  if (!t || t->odr_violated)
+    return type;
+  return t->type;
+}
+
+/* Set tbaa_enabled flag for TYPE.  */
+
+void
+enable_odr_based_tbaa (tree type)
+{
+  odr_type t = get_odr_type (type, true);
+  t->tbaa_enabled = true;
+}
+
+/* True if canonical type of TYPE is determined using ODR name.  */
+
+bool
+odr_based_tbaa_p (const_tree type)
+{
+  if (!RECORD_OR_UNION_TYPE_P (type))
+    return false;
+  odr_type t = get_odr_type (const_cast <tree> (type), false);
+  if (!t || !t->tbaa_enabled)
+    return false;
+  return true;
+}
+
+/* Set TYPE_CANONICAL of type and all its variants and duplicates
+   to CANONICAL.  */
+
+void
+set_type_canonical_for_odr_type (tree type, tree canonical)
+{
+  odr_type t = get_odr_type (type, false);
+  unsigned int i;
+  tree tt;
+
+  for (tree t2 = t->type; t2; t2 = TYPE_NEXT_VARIANT (t2))
+    TYPE_CANONICAL (t2) = canonical;
+  if (t->types)
+    FOR_EACH_VEC_ELT (*t->types, i, tt)
+      for (tree t2 = tt; t2; t2 = TYPE_NEXT_VARIANT (t2))
+        TYPE_CANONICAL (t2) = canonical;
+}
+
+/* Return true if we reported some ODR violation on TYPE.  */
+
+bool
+odr_type_violation_reported_p (tree type)
+{
+  return get_odr_type (type, false)->odr_violated;
+}
+
 /* Add TYPE od ODR type hash.  */
 
 void
 register_odr_type (tree type)
 {
   if (!odr_hash)
+    odr_hash = new odr_hash_type (23);
+  if (type == TYPE_MAIN_VARIANT (type))
     {
-      odr_hash = new odr_hash_type (23);
-      if (in_lto_p)
-        odr_vtable_hash = new odr_vtable_hash_type (23);
+      /* To get ODR warings right, first register all sub-types.  */
+      if (RECORD_OR_UNION_TYPE_P (type)
+         && COMPLETE_TYPE_P (type))
+       {
+         /* Limit recursion on types which are already registered.  */
+         odr_type ot = get_odr_type (type, false);
+         if (ot
+             && (ot->type == type
+                 || (ot->types_set
+                     && ot->types_set->contains (type))))
+           return;
+         for (tree f = TYPE_FIELDS (type); f; f = TREE_CHAIN (f))
+           if (TREE_CODE (f) == FIELD_DECL)
+             {
+               tree subtype = TREE_TYPE (f);
+
+               while (TREE_CODE (subtype) == ARRAY_TYPE)
+                 subtype = TREE_TYPE (subtype);
+               if (type_with_linkage_p (TYPE_MAIN_VARIANT (subtype)))
+                 register_odr_type (TYPE_MAIN_VARIANT (subtype));
+             }
+          if (TYPE_BINFO (type))
+            for (unsigned int i = 0;
+                 i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
+              register_odr_type (BINFO_TYPE (BINFO_BASE_BINFO
+                                                (TYPE_BINFO (type), i)));
+       }
+      get_odr_type (type, true);
     }
-  /* 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 && odr_type_p (main_odr_variant (type)))
-    get_odr_type (type, true);
 }
 
 /* Return true if type is known to have no derivations.  */
@@ -2217,9 +2106,6 @@ 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, "",
-              DECL_SOURCE_FILE (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
@@ -2247,6 +2133,7 @@ static void
 dump_type_inheritance_graph (FILE *f)
 {
   unsigned int i;
+  unsigned int num_all_types = 0, num_types = 0, num_duplicates = 0;
   if (!odr_types_ptr)
     return;
   fprintf (f, "\n\nType inheritance graph:\n");
@@ -2257,38 +2144,88 @@ dump_type_inheritance_graph (FILE *f)
     }
   for (i = 0; i < odr_types.length (); i++)
     {
-      if (odr_types[i] && odr_types[i]->types && odr_types[i]->types->length ())
+      if (!odr_types[i])
+       continue;
+
+      num_all_types++;
+      if (!odr_types[i]->types || !odr_types[i]->types->length ())
+       continue;
+
+      /* To aid ODR warnings we also mangle integer constants but do
+        not consinder duplicates there.  */
+      if (TREE_CODE (odr_types[i]->type) == INTEGER_TYPE)
+       continue;
+
+      /* It is normal to have one duplicate and one normal variant.  */
+      if (odr_types[i]->types->length () == 1
+         && COMPLETE_TYPE_P (odr_types[i]->type)
+         && !COMPLETE_TYPE_P ((*odr_types[i]->types)[0]))
+       continue;
+
+      num_types ++;
+
+      unsigned int j;
+      fprintf (f, "Duplicate tree types for odr type %i\n", i);
+      print_node (f, "", odr_types[i]->type, 0);
+      print_node (f, "", TYPE_NAME (odr_types[i]->type), 0);
+      putc ('\n',f);
+      for (j = 0; j < odr_types[i]->types->length (); j++)
        {
-         unsigned int j;
-         fprintf (f, "Duplicate tree types for odr type %i\n", i);
-         print_node (f, "", odr_types[i]->type, 0);
-         for (j = 0; j < odr_types[i]->types->length (); j++)
+         tree t;
+         num_duplicates ++;
+         fprintf (f, "duplicate #%i\n", j);
+         print_node (f, "", (*odr_types[i]->types)[j], 0);
+         t = (*odr_types[i]->types)[j];
+         while (TYPE_P (t) && TYPE_CONTEXT (t))
            {
-             tree t;
-             fprintf (f, "duplicate #%i\n", j);
-             print_node (f, "", (*odr_types[i]->types)[j], 0);
-             t = (*odr_types[i]->types)[j];
-             while (TYPE_P (t) && TYPE_CONTEXT (t))
-               {
-                 t = TYPE_CONTEXT (t);
-                 print_node (f, "", t, 0);
-               }
-             putc ('\n',f);
+             t = TYPE_CONTEXT (t);
+             print_node (f, "", t, 0);
            }
+         print_node (f, "", TYPE_NAME ((*odr_types[i]->types)[j]), 0);
+         putc ('\n',f);
        }
     }
+  fprintf (f, "Out of %i types there are %i types with duplicates; "
+          "%i duplicates overall\n", num_all_types, num_types, num_duplicates);
 }
 
-/* Given method type T, return type of class it belongs to.
-   Look up this pointer and get its type.    */
+/* Save some WPA->ltrans streaming by freeing stuff needed only for good
+   ODR warnings.
+   We free TYPE_VALUES of enums and also make TYPE_DECLs to not point back
+   to the type (which is needed to keep them in the same SCC and preserve
+   location information to output warnings) and subsequently we make all
+   TYPE_DECLS of same assembler name equivalent.  */
 
-tree
-method_class_type (const_tree t)
+static void
+free_odr_warning_data ()
 {
-  tree first_parm_type = TREE_VALUE (TYPE_ARG_TYPES (t));
-  gcc_assert (TREE_CODE (t) == METHOD_TYPE);
+  static bool odr_data_freed = false;
+
+  if (odr_data_freed || !flag_wpa || !odr_types_ptr)
+    return;
+
+  odr_data_freed = true;
+
+  for (unsigned int i = 0; i < odr_types.length (); i++)
+    if (odr_types[i])
+      {
+       tree t = odr_types[i]->type;
 
-  return TREE_TYPE (first_parm_type);
+       if (TREE_CODE (t) == ENUMERAL_TYPE)
+         TYPE_VALUES (t) = NULL;
+       TREE_TYPE (TYPE_NAME (t)) = void_type_node;
+
+       if (odr_types[i]->types)
+          for (unsigned int j = 0; j < odr_types[i]->types->length (); j++)
+           {
+             tree td = (*odr_types[i]->types)[j];
+
+             if (TREE_CODE (td) == ENUMERAL_TYPE)
+               TYPE_VALUES (td) = NULL;
+             TYPE_NAME (td) = TYPE_NAME (t);
+           }
+      }
+  odr_data_freed = true;
 }
 
 /* Initialize IPA devirt and build inheritance tree graph.  */
@@ -2298,24 +2235,24 @@ build_type_inheritance_graph (void)
 {
   struct symtab_node *n;
   FILE *inheritance_dump_file;
-  int flags;
+  dump_flags_t flags;
 
   if (odr_hash)
-    return;
+    {
+      free_odr_warning_data ();
+      return;
+    }
   timevar_push (TV_IPA_INHERITANCE);
   inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
   odr_hash = new odr_hash_type (23);
-  if (in_lto_p)
-    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.
  
@@ -2347,6 +2284,7 @@ build_type_inheritance_graph (void)
       dump_type_inheritance_graph (inheritance_dump_file);
       dump_end (TDI_inheritance, inheritance_dump_file);
     }
+  free_odr_warning_data ();
   timevar_pop (TV_IPA_INHERITANCE);
 }
 
@@ -2382,7 +2320,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;
@@ -2391,9 +2329,20 @@ 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
+   if TARGET is unknown (NULL) or it cannot be inserted (for example because
    its body was already removed and there is no way to refer to it), clear
    COMPLETEP.  */
 
@@ -2405,11 +2354,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)
@@ -2452,6 +2402,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)))
@@ -2465,16 +2416,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 cannot 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;
 }
 
@@ -2648,24 +2626,25 @@ possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
    polymorphic calls in the program, so we memoize all the previous
    queries and avoid duplicated work.  */
 
-struct polymorphic_call_target_d
+class polymorphic_call_target_d
 {
+public:
   HOST_WIDE_INT otr_token;
   ipa_polymorphic_call_context context;
   odr_type type;
   vec <cgraph_node *> targets;
   tree decl_warning;
   int type_warning;
+  unsigned int n_odr_types;
   bool complete;
   bool speculative;
 };
 
 /* 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 polymorphic_call_target_d *);
   static inline bool equal (const polymorphic_call_target_d *,
                            const polymorphic_call_target_d *);
@@ -2679,14 +2658,15 @@ 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);
+  hstate.add_hwi (odr_query->n_odr_types);
 
   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);
@@ -2712,7 +2692,9 @@ polymorphic_call_target_hasher::equal (const polymorphic_call_target_d *t1,
              == t2->context.maybe_in_construction
          && t1->context.maybe_derived_type == t2->context.maybe_derived_type
          && (t1->context.speculative_maybe_derived_type
-             == t2->context.speculative_maybe_derived_type));
+             == t2->context.speculative_maybe_derived_type)
+         /* Adding new type may affect outcome of target search.  */
+         && t1->n_odr_types == t2->n_odr_types);
 }
 
 /* Remove entry in polymorphic call target cache hash.  */
@@ -2744,6 +2726,21 @@ free_polymorphic_call_targets_hash ()
     }
 }
 
+/* Force rebuilding type inheritance graph from scratch.
+   This is use to make sure that we do not keep references to types
+   which was not visible to free_lang_data.  */
+
+void
+rebuild_type_inheritance_graph ()
+{
+  if (!odr_hash)
+    return;
+  delete odr_hash;
+  odr_hash = NULL;
+  odr_types_ptr = NULL;
+  free_polymorphic_call_targets_hash ();
+}
+
 /* When virtual function is removed, we may need to flush the cache.  */
 
 static void
@@ -2939,7 +2936,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.  */
@@ -2948,18 +2945,36 @@ 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
+class final_warning_record
 {
-  gcov_type dyn_count;
-  vec<odr_type_warn_count> type_warnings;
+public:
+  /* 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;
 };
-struct final_warning_record *final_warning_records;
+
+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 ();
+    }
+}
+
+class final_warning_record *final_warning_records;
 
 /* Return vector containing possible targets of polymorphic call of type
    OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
@@ -3083,6 +3098,7 @@ possible_polymorphic_call_targets (tree otr_type,
   key.otr_token = otr_token;
   key.speculative = speculative;
   key.context = context;
+  key.n_odr_types = odr_types.length ();
   slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
   if (cache_token)
    *cache_token = (void *)*slot;
@@ -3093,15 +3109,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;
     }
@@ -3215,17 +3238,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
@@ -3234,7 +3261,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))
                    {
@@ -3289,6 +3315,7 @@ possible_polymorphic_call_targets (tree otr_type,
 
   (*slot)->targets = nodes;
   (*slot)->complete = complete;
+  (*slot)->n_odr_types = odr_types.length ();
   if (completep)
     *completep = complete;
 
@@ -3307,7 +3334,7 @@ add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value
 /* Dump target list TARGETS into FILE.  */
 
 static void
-dump_targets (FILE *f, vec <cgraph_node *> targets)
+dump_targets (FILE *f, vec <cgraph_node *> targets, bool verbose)
 {
   unsigned int i;
 
@@ -3316,13 +3343,21 @@ 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)
        fprintf (f, " (no definition%s)",
                 DECL_DECLARED_INLINE_P (targets[i]->decl)
                 ? " inline" : "");
+      /* With many targets for every call polymorphic dumps are going to
+        be quadratic in size.  */
+      if (i > 10 && !verbose)
+       {
+         fprintf (f, " ... and %i more targets\n", targets.length () - i);
+         return;
+       }
     }
   fprintf (f, "\n");
 }
@@ -3333,7 +3368,8 @@ void
 dump_possible_polymorphic_call_targets (FILE *f,
                                        tree otr_type,
                                        HOST_WIDE_INT otr_token,
-                                       const ipa_polymorphic_call_context &ctx)
+                                       const ipa_polymorphic_call_context &ctx,
+                                       bool verbose)
 {
   vec <cgraph_node *> targets;
   bool final;
@@ -3358,7 +3394,7 @@ dump_possible_polymorphic_call_targets (FILE *f,
           ctx.maybe_derived_type ? " (derived types included)" : "",
           ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
   len = targets.length ();
-  dump_targets (f, targets);
+  dump_targets (f, targets, verbose);
 
   targets = possible_polymorphic_call_targets (otr_type, otr_token,
                                               ctx,
@@ -3366,9 +3402,15 @@ dump_possible_polymorphic_call_targets (FILE *f,
   if (targets.length () != len)
     {
       fprintf (f, "  Speculative targets:");
-      dump_targets (f, targets);
-    }
-  gcc_assert (targets.length () <= len);
+      dump_targets (f, targets, verbose);
+    }
+  /* 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");
 }
 
@@ -3384,13 +3426,13 @@ possible_polymorphic_call_target_p (tree otr_type,
 {
   vec <cgraph_node *> targets;
   unsigned int i;
-  enum built_in_function fcode;
   bool final;
 
-  if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
-      && ((fcode = DECL_FUNCTION_CODE (n->decl))
-         == BUILT_IN_UNREACHABLE
-          || fcode == BUILT_IN_TRAP))
+  if (fndecl_built_in_p (n->decl, BUILT_IN_UNREACHABLE)
+      || fndecl_built_in_p (n->decl, BUILT_IN_TRAP))
+    return true;
+
+  if (is_cxa_pure_virtual_p (n->decl))
     return true;
 
   if (!odr_hash)
@@ -3414,7 +3456,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);
@@ -3441,13 +3483,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);
 }
 
@@ -3483,7 +3524,7 @@ likely_target_p (struct cgraph_node *n)
 /* Compare type warning records P1 and P2 and choose one with larger count;
    helper for qsort.  */
 
-int
+static int
 type_warning_cmp (const void *p1, const void *p2)
 {
   const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
@@ -3499,7 +3540,7 @@ type_warning_cmp (const void *p1, const void *p2)
 /* Compare decl warning records P1 and P2 and choose one with larger count;
    helper for qsort.  */
 
-int
+static int
 decl_warning_cmp (const void *p1, const void *p2)
 {
   const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
@@ -3579,8 +3620,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 ();
     }
 
@@ -3590,8 +3631,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)
          {
@@ -3600,7 +3641,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
@@ -3613,7 +3654,7 @@ ipa_devirt (void)
 
            if (dump_file)
              dump_possible_polymorphic_call_targets 
-               (dump_file, e);
+               (dump_file, e, (dump_flags & TDF_DETAILS));
 
            npolymorphic++;
 
@@ -3636,7 +3677,7 @@ ipa_devirt (void)
                ndropped++;
                if (dump_file)
                  fprintf (dump_file, "Dropping polymorphic call info;"
-                          " it can not be used by ipa-prop\n");
+                          " it cannot be used by ipa-prop\n");
              }
 
            if (!opt_for_fn (n->decl, flag_devirtualize_speculatively))
@@ -3744,12 +3785,11 @@ ipa_devirt (void)
              {
                if (dump_enabled_p ())
                   {
-                    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);
+                    dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, e->call_stmt,
+                                    "speculatively devirtualizing call "
+                                    "in %s to %s\n",
+                                    n->dump_name (),
+                                    likely_target->dump_name ());
                   }
                if (!likely_target->can_be_discarded_p ())
                  {
@@ -3761,11 +3801,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)
     {
@@ -3778,10 +3818,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 "
@@ -3801,13 +3841,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);
@@ -3816,9 +3856,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,
@@ -3844,7 +3885,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,
@@ -3854,10 +3896,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;
     }