LCOV - code coverage report
Current view: top level - gcc - ipa-devirt.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 82.2 % 1835 1508
Test Date: 2024-04-20 14:03:02 Functions: 93.8 % 80 75
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Basic IPA utilities for type inheritance graph construction and
       2                 :             :    devirtualization.
       3                 :             :    Copyright (C) 2013-2024 Free Software Foundation, Inc.
       4                 :             :    Contributed by Jan Hubicka
       5                 :             : 
       6                 :             : This file is part of GCC.
       7                 :             : 
       8                 :             : GCC is free software; you can redistribute it and/or modify it under
       9                 :             : the terms of the GNU General Public License as published by the Free
      10                 :             : Software Foundation; either version 3, or (at your option) any later
      11                 :             : version.
      12                 :             : 
      13                 :             : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      14                 :             : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      15                 :             : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      16                 :             : for more details.
      17                 :             : 
      18                 :             : You should have received a copy of the GNU General Public License
      19                 :             : along with GCC; see the file COPYING3.  If not see
      20                 :             : <http://www.gnu.org/licenses/>.  */
      21                 :             : 
      22                 :             : /* Brief vocabulary:
      23                 :             :      ODR = One Definition Rule
      24                 :             :         In short, the ODR states that:
      25                 :             :         1 In any translation unit, a template, type, function, or object can
      26                 :             :           have no more than one definition. Some of these can have any number
      27                 :             :           of declarations. A definition provides an instance.
      28                 :             :         2 In the entire program, an object or non-inline function cannot have
      29                 :             :           more than one definition; if an object or function is used, it must
      30                 :             :           have exactly one definition. You can declare an object or function
      31                 :             :           that is never used, in which case you don't have to provide
      32                 :             :           a definition. In no event can there be more than one definition.
      33                 :             :         3 Some things, like types, templates, and extern inline functions, can
      34                 :             :           be defined in more than one translation unit. For a given entity,
      35                 :             :           each definition must be the same. Non-extern objects and functions
      36                 :             :           in different translation units are different entities, even if their
      37                 :             :           names and types are the same.
      38                 :             : 
      39                 :             :      OTR = OBJ_TYPE_REF
      40                 :             :        This is the Gimple representation of type information of a polymorphic call.
      41                 :             :        It contains two parameters:
      42                 :             :          otr_type is a type of class whose method is called.
      43                 :             :          otr_token is the index into virtual table where address is taken.
      44                 :             : 
      45                 :             :      BINFO
      46                 :             :        This is the type inheritance information attached to each tree
      47                 :             :        RECORD_TYPE by the C++ frontend.  It provides information about base
      48                 :             :        types and virtual tables.
      49                 :             : 
      50                 :             :        BINFO is linked to the RECORD_TYPE by TYPE_BINFO.
      51                 :             :        BINFO also links to its type by BINFO_TYPE and to the virtual table by
      52                 :             :        BINFO_VTABLE.
      53                 :             : 
      54                 :             :        Base types of a given type are enumerated by BINFO_BASE_BINFO
      55                 :             :        vector.  Members of this vectors are not BINFOs associated
      56                 :             :        with a base type.  Rather they are new copies of BINFOs
      57                 :             :        (base BINFOs). Their virtual tables may differ from
      58                 :             :        virtual table of the base type.  Also BINFO_OFFSET specifies
      59                 :             :        offset of the base within the type.
      60                 :             : 
      61                 :             :        In the case of single inheritance, the virtual table is shared
      62                 :             :        and BINFO_VTABLE of base BINFO is NULL.  In the case of multiple
      63                 :             :        inheritance the individual virtual tables are pointer to by
      64                 :             :        BINFO_VTABLE of base binfos (that differs of BINFO_VTABLE of
      65                 :             :        binfo associated to the base type).
      66                 :             : 
      67                 :             :        BINFO lookup for a given base type and offset can be done by
      68                 :             :        get_binfo_at_offset.  It returns proper BINFO whose virtual table
      69                 :             :        can be used for lookup of virtual methods associated with the
      70                 :             :        base type.
      71                 :             : 
      72                 :             :      token
      73                 :             :        This is an index of virtual method in virtual table associated
      74                 :             :        to the type defining it. Token can be looked up from OBJ_TYPE_REF
      75                 :             :        or from DECL_VINDEX of a given virtual table.
      76                 :             : 
      77                 :             :      polymorphic (indirect) call
      78                 :             :        This is callgraph representation of virtual method call.  Every
      79                 :             :        polymorphic call contains otr_type and otr_token taken from
      80                 :             :        original OBJ_TYPE_REF at callgraph construction time.
      81                 :             : 
      82                 :             :    What we do here:
      83                 :             : 
      84                 :             :    build_type_inheritance_graph triggers a construction of the type inheritance
      85                 :             :    graph.
      86                 :             : 
      87                 :             :      We reconstruct it based on types of methods we see in the unit.
      88                 :             :      This means that the graph is not complete. Types with no methods are not
      89                 :             :      inserted into the graph.  Also types without virtual methods are not
      90                 :             :      represented at all, though it may be easy to add this.
      91                 :             : 
      92                 :             :      The inheritance graph is represented as follows:
      93                 :             : 
      94                 :             :        Vertices are structures odr_type.  Every odr_type may correspond
      95                 :             :        to one or more tree type nodes that are equivalent by ODR rule.
      96                 :             :        (the multiple type nodes appear only with linktime optimization)
      97                 :             : 
      98                 :             :        Edges are represented by odr_type->base and odr_type->derived_types.
      99                 :             :        At the moment we do not track offsets of types for multiple inheritance.
     100                 :             :        Adding this is easy.
     101                 :             : 
     102                 :             :   possible_polymorphic_call_targets returns, given an parameters found in
     103                 :             :   indirect polymorphic edge all possible polymorphic call targets of the call.
     104                 :             : 
     105                 :             :   pass_ipa_devirt performs simple speculative devirtualization.
     106                 :             : */
     107                 :             : 
     108                 :             : #include "config.h"
     109                 :             : #include "system.h"
     110                 :             : #include "coretypes.h"
     111                 :             : #include "backend.h"
     112                 :             : #include "rtl.h"
     113                 :             : #include "tree.h"
     114                 :             : #include "gimple.h"
     115                 :             : #include "alloc-pool.h"
     116                 :             : #include "tree-pass.h"
     117                 :             : #include "cgraph.h"
     118                 :             : #include "lto-streamer.h"
     119                 :             : #include "fold-const.h"
     120                 :             : #include "print-tree.h"
     121                 :             : #include "calls.h"
     122                 :             : #include "ipa-utils.h"
     123                 :             : #include "gimple-iterator.h"
     124                 :             : #include "gimple-fold.h"
     125                 :             : #include "symbol-summary.h"
     126                 :             : #include "tree-vrp.h"
     127                 :             : #include "sreal.h"
     128                 :             : #include "ipa-cp.h"
     129                 :             : #include "ipa-prop.h"
     130                 :             : #include "ipa-fnsummary.h"
     131                 :             : #include "demangle.h"
     132                 :             : #include "dbgcnt.h"
     133                 :             : #include "gimple-pretty-print.h"
     134                 :             : #include "intl.h"
     135                 :             : #include "stringpool.h"
     136                 :             : #include "attribs.h"
     137                 :             : #include "data-streamer.h"
     138                 :             : #include "lto-streamer.h"
     139                 :             : #include "streamer-hooks.h"
     140                 :             : 
     141                 :             : /* Hash based set of pairs of types.  */
     142                 :             : struct type_pair
     143                 :             : {
     144                 :             :   tree first;
     145                 :             :   tree second;
     146                 :             : };
     147                 :             : 
     148                 :             : template <>
     149                 :             : struct default_hash_traits <type_pair>
     150                 :             :   : typed_noop_remove <type_pair>
     151                 :             : {
     152                 :             :   GTY((skip)) typedef type_pair value_type;
     153                 :             :   GTY((skip)) typedef type_pair compare_type;
     154                 :             :   static hashval_t
     155                 :          13 :   hash (type_pair p)
     156                 :             :   {
     157                 :          13 :     return TYPE_UID (p.first) ^ TYPE_UID (p.second);
     158                 :             :   }
     159                 :             :   static const bool empty_zero_p = true;
     160                 :             :   static bool
     161                 :       92924 :   is_empty (type_pair p)
     162                 :             :   {
     163                 :       92911 :     return p.first == NULL;
     164                 :             :   }
     165                 :             :   static bool
     166                 :             :   is_deleted (type_pair p ATTRIBUTE_UNUSED)
     167                 :             :     {
     168                 :             :       return false;
     169                 :             :     }
     170                 :             :   static bool
     171                 :           0 :   equal (const type_pair &a, const type_pair &b)
     172                 :             :     {
     173                 :           0 :       return a.first==b.first && a.second == b.second;
     174                 :             :     }
     175                 :             :   static void
     176                 :           0 :   mark_empty (type_pair &e)
     177                 :             :     {
     178                 :           0 :       e.first = NULL;
     179                 :             :     }
     180                 :             : };
     181                 :             : 
     182                 :             : /* HACK alert: this is used to communicate with ipa-inline-transform that
     183                 :             :    thunk is being expanded and there is no need to clear the polymorphic
     184                 :             :    call target cache.  */
     185                 :             : bool thunk_expansion;
     186                 :             : 
     187                 :             : static bool odr_types_equivalent_p (tree, tree, bool, bool *,
     188                 :             :                                     hash_set<type_pair> *,
     189                 :             :                                     location_t, location_t);
     190                 :             : static void warn_odr (tree t1, tree t2, tree st1, tree st2,
     191                 :             :                       bool warn, bool *warned, const char *reason);
     192                 :             : 
     193                 :             : static bool odr_violation_reported = false;
     194                 :             : 
     195                 :             : 
     196                 :             : /* Pointer set of all call targets appearing in the cache.  */
     197                 :             : static hash_set<cgraph_node *> *cached_polymorphic_call_targets;
     198                 :             : 
     199                 :             : /* The node of type inheritance graph.  For each type unique in
     200                 :             :    One Definition Rule (ODR) sense, we produce one node linking all
     201                 :             :    main variants of types equivalent to it, bases and derived types.  */
     202                 :             : 
     203                 :             : struct GTY(()) odr_type_d
     204                 :             : {
     205                 :             :   /* leader type.  */
     206                 :             :   tree type;
     207                 :             :   /* All bases; built only for main variants of types.  */
     208                 :             :   vec<odr_type> GTY((skip)) bases;
     209                 :             :   /* All derived types with virtual methods seen in unit;
     210                 :             :      built only for main variants of types.  */
     211                 :             :   vec<odr_type> GTY((skip)) derived_types;
     212                 :             : 
     213                 :             :   /* All equivalent types, if more than one.  */
     214                 :             :   vec<tree, va_gc> *types;
     215                 :             :   /* Set of all equivalent types, if NON-NULL.  */
     216                 :             :   hash_set<tree> * GTY((skip)) types_set;
     217                 :             : 
     218                 :             :   /* Unique ID indexing the type in odr_types array.  */
     219                 :             :   int id;
     220                 :             :   /* Is it in anonymous namespace? */
     221                 :             :   bool anonymous_namespace;
     222                 :             :   /* Do we know about all derivations of given type?  */
     223                 :             :   bool all_derivations_known;
     224                 :             :   /* Did we report ODR violation here?  */
     225                 :             :   bool odr_violated;
     226                 :             :   /* Set when virtual table without RTTI prevailed table with.  */
     227                 :             :   bool rtti_broken;
     228                 :             :   /* Set when the canonical type is determined using the type name.  */
     229                 :             :   bool tbaa_enabled;
     230                 :             : };
     231                 :             : 
     232                 :             : /* Return TRUE if all derived types of T are known and thus
     233                 :             :    we may consider the walk of derived type complete.
     234                 :             : 
     235                 :             :    This is typically true only for final anonymous namespace types and types
     236                 :             :    defined within functions (that may be COMDAT and thus shared across units,
     237                 :             :    but with the same set of derived types).  */
     238                 :             : 
     239                 :             : bool
     240                 :     2779744 : type_all_derivations_known_p (const_tree t)
     241                 :             : {
     242                 :     2779744 :   if (TYPE_FINAL_P (t))
     243                 :             :     return true;
     244                 :     2713699 :   if (flag_ltrans)
     245                 :             :     return false;
     246                 :             :   /* Non-C++ types may have IDENTIFIER_NODE here, do not crash.  */
     247                 :     2712588 :   if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL)
     248                 :             :     return true;
     249                 :     2712588 :   if (type_in_anonymous_namespace_p (t))
     250                 :             :     return true;
     251                 :     2709134 :   return (decl_function_context (TYPE_NAME (t)) != NULL);
     252                 :             : }
     253                 :             : 
     254                 :             : /* Return TRUE if type's constructors are all visible.  */
     255                 :             : 
     256                 :             : static bool
     257                 :      284232 : type_all_ctors_visible_p (tree t)
     258                 :             : {
     259                 :      284232 :   return !flag_ltrans
     260                 :      284148 :          && symtab->state >= CONSTRUCTION
     261                 :             :          /* We cannot always use type_all_derivations_known_p.
     262                 :             :             For function local types we must assume case where
     263                 :             :             the function is COMDAT and shared in between units.
     264                 :             : 
     265                 :             :             TODO: These cases are quite easy to get, but we need
     266                 :             :             to keep track of C++ privatizing via -Wno-weak
     267                 :             :             as well as the  IPA privatizing.  */
     268                 :      568380 :          && type_in_anonymous_namespace_p (t);
     269                 :             : }
     270                 :             : 
     271                 :             : /* Return TRUE if type may have instance.  */
     272                 :             : 
     273                 :             : static bool
     274                 :      284232 : type_possibly_instantiated_p (tree t)
     275                 :             : {
     276                 :      284232 :   tree vtable;
     277                 :      284232 :   varpool_node *vnode;
     278                 :             : 
     279                 :             :   /* TODO: Add abstract types here.  */
     280                 :      284232 :   if (!type_all_ctors_visible_p (t))
     281                 :             :     return true;
     282                 :             : 
     283                 :        8836 :   vtable = BINFO_VTABLE (TYPE_BINFO (t));
     284                 :        8836 :   if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
     285                 :        8836 :     vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
     286                 :        8836 :   vnode = varpool_node::get (vtable);
     287                 :        8836 :   return vnode && vnode->definition;
     288                 :             : }
     289                 :             : 
     290                 :             : /* Return true if T or type derived from T may have instance.  */
     291                 :             : 
     292                 :             : static bool
     293                 :           6 : type_or_derived_type_possibly_instantiated_p (odr_type t)
     294                 :             : {
     295                 :           6 :   if (type_possibly_instantiated_p (t->type))
     296                 :             :     return true;
     297                 :           9 :   for (auto derived : t->derived_types)
     298                 :           3 :     if (type_or_derived_type_possibly_instantiated_p (derived))
     299                 :             :       return true;
     300                 :             :   return false;
     301                 :             : }
     302                 :             : 
     303                 :             : /* Hash used to unify ODR types based on their mangled name and for anonymous
     304                 :             :    namespace types.  */
     305                 :             : 
     306                 :             : struct odr_name_hasher : pointer_hash <odr_type_d>
     307                 :             : {
     308                 :             :   typedef union tree_node *compare_type;
     309                 :             :   static inline hashval_t hash (const odr_type_d *);
     310                 :             :   static inline bool equal (const odr_type_d *, const tree_node *);
     311                 :             :   static inline void remove (odr_type_d *);
     312                 :             : };
     313                 :             : 
     314                 :             : static bool
     315                 :     5787459 : can_be_name_hashed_p (tree t)
     316                 :             : {
     317                 :      105446 :   return (!in_lto_p || odr_type_p (t));
     318                 :             : }
     319                 :             : 
     320                 :             : /* Hash type by its ODR name.  */
     321                 :             : 
     322                 :             : static hashval_t
     323                 :    32955670 : hash_odr_name (const_tree t)
     324                 :             : {
     325                 :    32955670 :   gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
     326                 :             : 
     327                 :             :   /* If not in LTO, all main variants are unique, so we can do
     328                 :             :      pointer hash.  */
     329                 :    32955670 :   if (!in_lto_p)
     330                 :    32550355 :     return htab_hash_pointer (t);
     331                 :             : 
     332                 :             :   /* Anonymous types are unique.  */
     333                 :      405315 :   if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
     334                 :       14758 :     return htab_hash_pointer (t);
     335                 :             : 
     336                 :      390557 :   gcc_checking_assert (TYPE_NAME (t)
     337                 :             :                        && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)));
     338                 :      390557 :   return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t)));
     339                 :             : }
     340                 :             : 
     341                 :             : /* Return the computed hashcode for ODR_TYPE.  */
     342                 :             : 
     343                 :             : inline hashval_t
     344                 :    27182123 : odr_name_hasher::hash (const odr_type_d *odr_type)
     345                 :             : {
     346                 :    27182123 :   return hash_odr_name (odr_type->type);
     347                 :             : }
     348                 :             : 
     349                 :             : /* For languages with One Definition Rule, work out if
     350                 :             :    types are the same based on their name.
     351                 :             : 
     352                 :             :    This is non-trivial for LTO where minor differences in
     353                 :             :    the type representation may have prevented type merging
     354                 :             :    to merge two copies of otherwise equivalent type.
     355                 :             : 
     356                 :             :    Until we start streaming mangled type names, this function works
     357                 :             :    only for polymorphic types.
     358                 :             : */
     359                 :             : 
     360                 :             : bool
     361                 :     4020188 : types_same_for_odr (const_tree type1, const_tree type2)
     362                 :             : {
     363                 :     4020188 :   gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
     364                 :             : 
     365                 :     4020188 :   type1 = TYPE_MAIN_VARIANT (type1);
     366                 :     4020188 :   type2 = TYPE_MAIN_VARIANT (type2);
     367                 :             : 
     368                 :     4020188 :   if (type1 == type2)
     369                 :             :     return true;
     370                 :             : 
     371                 :     1338926 :   if (!in_lto_p)
     372                 :             :     return false;
     373                 :             : 
     374                 :             :   /* Anonymous namespace types are never duplicated.  */
     375                 :        2240 :   if ((type_with_linkage_p (type1) && type_in_anonymous_namespace_p (type1))
     376                 :        2240 :       || (type_with_linkage_p (type2) && type_in_anonymous_namespace_p (type2)))
     377                 :           0 :     return false;
     378                 :             : 
     379                 :             :   /* If both type has mangled defined check if they are same.
     380                 :             :      Watch for anonymous types which are all mangled as "<anon">.  */
     381                 :        1120 :   if (!type_with_linkage_p (type1) || !type_with_linkage_p (type2))
     382                 :             :     return false;
     383                 :        1120 :   if (type_in_anonymous_namespace_p (type1)
     384                 :        1120 :       || type_in_anonymous_namespace_p (type2))
     385                 :           0 :     return false;
     386                 :        1120 :   return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1))
     387                 :        1120 :           == DECL_ASSEMBLER_NAME (TYPE_NAME (type2)));
     388                 :             : }
     389                 :             : 
     390                 :             : /* Return true if we can decide on ODR equivalency.
     391                 :             : 
     392                 :             :    In non-LTO it is always decide, in LTO however it depends in the type has
     393                 :             :    ODR info attached. */
     394                 :             : 
     395                 :             : bool
     396                 :     3097627 : types_odr_comparable (tree t1, tree t2)
     397                 :             : {
     398                 :     3097627 :   return (!in_lto_p
     399                 :        3289 :           || TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)
     400                 :     3098525 :           || (odr_type_p (TYPE_MAIN_VARIANT (t1))
     401                 :         884 :               && odr_type_p (TYPE_MAIN_VARIANT (t2))));
     402                 :             : }
     403                 :             : 
     404                 :             : /* Return true if T1 and T2 are ODR equivalent.  If ODR equivalency is not
     405                 :             :    known, be conservative and return false.  */
     406                 :             : 
     407                 :             : bool
     408                 :      896651 : types_must_be_same_for_odr (tree t1, tree t2)
     409                 :             : {
     410                 :      896651 :   if (types_odr_comparable (t1, t2))
     411                 :      896651 :     return types_same_for_odr (t1, t2);
     412                 :             :   else
     413                 :           0 :     return TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2);
     414                 :             : }
     415                 :             : 
     416                 :             : /* If T is compound type, return type it is based on.  */
     417                 :             : 
     418                 :             : static tree
     419                 :        6614 : compound_type_base (const_tree t)
     420                 :             : {
     421                 :        6614 :   if (TREE_CODE (t) == ARRAY_TYPE
     422                 :        6481 :       || POINTER_TYPE_P (t)
     423                 :        4934 :       || TREE_CODE (t) == COMPLEX_TYPE
     424                 :        4928 :       || VECTOR_TYPE_P (t))
     425                 :        1688 :     return TREE_TYPE (t);
     426                 :        4926 :   if (TREE_CODE (t) == METHOD_TYPE)
     427                 :         394 :     return TYPE_METHOD_BASETYPE (t);
     428                 :        4532 :   if (TREE_CODE (t) == OFFSET_TYPE)
     429                 :           0 :     return TYPE_OFFSET_BASETYPE (t);
     430                 :             :   return NULL_TREE;
     431                 :             : }
     432                 :             : 
     433                 :             : /* Return true if T is either ODR type or compound type based from it.
     434                 :             :    If the function return true, we know that T is a type originating from C++
     435                 :             :    source even at link-time.  */
     436                 :             : 
     437                 :             : bool
     438                 :        6519 : odr_or_derived_type_p (const_tree t)
     439                 :             : {
     440                 :        8601 :   do
     441                 :             :     {
     442                 :        8601 :       if (odr_type_p (TYPE_MAIN_VARIANT (t)))
     443                 :             :         return true;
     444                 :             :       /* Function type is a tricky one. Basically we can consider it
     445                 :             :          ODR derived if return type or any of the parameters is.
     446                 :             :          We need to check all parameters because LTO streaming merges
     447                 :             :          common types (such as void) and they are not considered ODR then.  */
     448                 :        7699 :       if (TREE_CODE (t) == FUNCTION_TYPE)
     449                 :             :         {
     450                 :        1085 :           if (TYPE_METHOD_BASETYPE (t))
     451                 :           0 :             t = TYPE_METHOD_BASETYPE (t);
     452                 :             :           else
     453                 :             :            {
     454                 :        1085 :              if (TREE_TYPE (t) && odr_or_derived_type_p (TREE_TYPE (t)))
     455                 :             :                return true;
     456                 :        3295 :              for (t = TYPE_ARG_TYPES (t); t; t = TREE_CHAIN (t))
     457                 :        2258 :                if (odr_or_derived_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (t))))
     458                 :             :                  return true;
     459                 :             :              return false;
     460                 :             :            }
     461                 :             :         }
     462                 :             :       else
     463                 :        6614 :         t = compound_type_base (t);
     464                 :             :     }
     465                 :        6614 :   while (t);
     466                 :             :   return t;
     467                 :             : }
     468                 :             : 
     469                 :             : /* Compare types T1 and T2 and return true if they are
     470                 :             :    equivalent.  */
     471                 :             : 
     472                 :             : inline bool
     473                 :    32330530 : odr_name_hasher::equal (const odr_type_d *o1, const tree_node *t2)
     474                 :             : {
     475                 :    32330530 :   tree t1 = o1->type;
     476                 :             : 
     477                 :    32330530 :   gcc_checking_assert (TYPE_MAIN_VARIANT (t2) == t2);
     478                 :    32330530 :   gcc_checking_assert (TYPE_MAIN_VARIANT (t1) == t1);
     479                 :    32330530 :   if (t1 == t2)
     480                 :             :     return true;
     481                 :    27855614 :   if (!in_lto_p)
     482                 :             :     return false;
     483                 :             :   /* Check for anonymous namespaces.  */
     484                 :      670878 :   if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
     485                 :      659746 :       || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
     486                 :       23513 :     return false;
     487                 :      311926 :   gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1)));
     488                 :      311926 :   gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
     489                 :      311926 :   return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
     490                 :      311926 :           == DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
     491                 :             : }
     492                 :             : 
     493                 :             : /* Free ODR type V.  */
     494                 :             : 
     495                 :             : inline void
     496                 :     1167312 : odr_name_hasher::remove (odr_type_d *v)
     497                 :             : {
     498                 :     1167312 :   v->bases.release ();
     499                 :     1167312 :   v->derived_types.release ();
     500                 :     1167312 :   if (v->types_set)
     501                 :           0 :     delete v->types_set;
     502                 :     1167312 :   ggc_free (v);
     503                 :     1167312 : }
     504                 :             : 
     505                 :             : /* ODR type hash used to look up ODR type based on tree type node.  */
     506                 :             : 
     507                 :             : typedef hash_table<odr_name_hasher> odr_hash_type;
     508                 :             : static odr_hash_type *odr_hash;
     509                 :             : 
     510                 :             : /* ODR types are also stored into ODR_TYPE vector to allow consistent
     511                 :             :    walking.  Bases appear before derived types.  Vector is garbage collected
     512                 :             :    so we won't end up visiting empty types.  */
     513                 :             : 
     514                 :             : static GTY(()) vec <odr_type, va_gc> *odr_types_ptr;
     515                 :             : #define odr_types (*odr_types_ptr)
     516                 :             : 
     517                 :             : /* All enums defined and accessible for the unit.  */
     518                 :             : static GTY(()) vec <tree, va_gc> *odr_enums;
     519                 :             : 
     520                 :             : /* Information we hold about value defined by an enum type.  */
     521                 :        3102 : struct odr_enum_val
     522                 :             : {
     523                 :             :   const char *name;
     524                 :             :   wide_int val;
     525                 :             :   location_t locus;
     526                 :             : };
     527                 :             : 
     528                 :             : /* Information about enum values.  */
     529                 :         442 : struct odr_enum
     530                 :             : {
     531                 :             :   location_t locus;
     532                 :             :   auto_vec<odr_enum_val, 0> vals;
     533                 :             :   bool warned;
     534                 :             : };
     535                 :             : 
     536                 :             : /* A table of all ODR enum definitions.  */
     537                 :             : static hash_map <nofree_string_hash, odr_enum> *odr_enum_map = NULL;
     538                 :             : static struct obstack odr_enum_obstack;
     539                 :             : 
     540                 :             : /* Set TYPE_BINFO of TYPE and its variants to BINFO.  */
     541                 :             : void
     542                 :           0 : set_type_binfo (tree type, tree binfo)
     543                 :             : {
     544                 :           0 :   for (; type; type = TYPE_NEXT_VARIANT (type))
     545                 :           0 :     if (COMPLETE_TYPE_P (type))
     546                 :           0 :       TYPE_BINFO (type) = binfo;
     547                 :             :     else
     548                 :           0 :       gcc_assert (!TYPE_BINFO (type));
     549                 :           0 : }
     550                 :             : 
     551                 :             : /* Return true if type variants match.
     552                 :             :    This assumes that we already verified that T1 and T2 are variants of the
     553                 :             :    same type.  */
     554                 :             : 
     555                 :             : static bool
     556                 :        7041 : type_variants_equivalent_p (tree t1, tree t2)
     557                 :             : {
     558                 :        7041 :   if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
     559                 :             :     return false;
     560                 :             : 
     561                 :        7034 :   if (comp_type_attributes (t1, t2) != 1)
     562                 :             :     return false;
     563                 :             : 
     564                 :       14066 :   if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
     565                 :        7324 :       && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
     566                 :             :     return false;
     567                 :             : 
     568                 :             :   return true;
     569                 :             : }
     570                 :             : 
     571                 :             : /* Compare T1 and T2 based on name or structure.  */
     572                 :             : 
     573                 :             : static bool
     574                 :         425 : odr_subtypes_equivalent_p (tree t1, tree t2,
     575                 :             :                            hash_set<type_pair> *visited,
     576                 :             :                            location_t loc1, location_t loc2)
     577                 :             : {
     578                 :             : 
     579                 :             :   /* This can happen in incomplete types that should be handled earlier.  */
     580                 :         425 :   gcc_assert (t1 && t2);
     581                 :             : 
     582                 :         425 :   if (t1 == t2)
     583                 :             :     return true;
     584                 :             : 
     585                 :             :   /* Anonymous namespace types must match exactly.  */
     586                 :          96 :   if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1))
     587                 :          75 :        && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
     588                 :         168 :       || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2))
     589                 :          73 :           && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2))))
     590                 :          11 :     return false;
     591                 :             : 
     592                 :             :   /* For ODR types be sure to compare their names.
     593                 :             :      To support -Wno-odr-type-merging we allow one type to be non-ODR
     594                 :             :      and other ODR even though it is a violation.  */
     595                 :          85 :   if (types_odr_comparable (t1, t2))
     596                 :             :     {
     597                 :          72 :       if (t1 != t2
     598                 :          72 :           && odr_type_p (TYPE_MAIN_VARIANT (t1))
     599                 :          65 :           && get_odr_type (TYPE_MAIN_VARIANT (t1), true)->odr_violated)
     600                 :             :         return false;
     601                 :          48 :       if (!types_same_for_odr (t1, t2))
     602                 :             :         return false;
     603                 :          41 :       if (!type_variants_equivalent_p (t1, t2))
     604                 :             :         return false;
     605                 :             :       /* Limit recursion: If subtypes are ODR types and we know
     606                 :             :          that they are same, be happy.  */
     607                 :          34 :       if (odr_type_p (TYPE_MAIN_VARIANT (t1)))
     608                 :             :         return true;
     609                 :             :     }
     610                 :             : 
     611                 :             :   /* Component types, builtins and possibly violating ODR types
     612                 :             :      have to be compared structurally.  */
     613                 :          13 :   if (TREE_CODE (t1) != TREE_CODE (t2))
     614                 :             :     return false;
     615                 :          13 :   if (AGGREGATE_TYPE_P (t1)
     616                 :          13 :       && (TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
     617                 :             :     return false;
     618                 :             : 
     619                 :          13 :   type_pair pair={TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2)};
     620                 :          13 :   if (TYPE_UID (TYPE_MAIN_VARIANT (t1)) > TYPE_UID (TYPE_MAIN_VARIANT (t2)))
     621                 :             :     {
     622                 :           0 :       pair.first = TYPE_MAIN_VARIANT (t2);
     623                 :           0 :       pair.second = TYPE_MAIN_VARIANT (t1);
     624                 :             :     }
     625                 :          13 :   if (visited->add (pair))
     626                 :             :     return true;
     627                 :          13 :   if (!odr_types_equivalent_p (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2),
     628                 :             :                               false, NULL, visited, loc1, loc2))
     629                 :             :     return false;
     630                 :           2 :   if (!type_variants_equivalent_p (t1, t2))
     631                 :             :     return false;
     632                 :             :   return true;
     633                 :             : }
     634                 :             : 
     635                 :             : /* Return true if DECL1 and DECL2 are identical methods.  Consider
     636                 :             :    name equivalent to name.localalias.xyz.  */
     637                 :             : 
     638                 :             : static bool
     639                 :         120 : methods_equal_p (tree decl1, tree decl2)
     640                 :             : {
     641                 :         120 :   if (DECL_ASSEMBLER_NAME (decl1) == DECL_ASSEMBLER_NAME (decl2))
     642                 :             :     return true;
     643                 :           0 :   const char sep = symbol_table::symbol_suffix_separator ();
     644                 :             : 
     645                 :           0 :   const char *name1 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl1));
     646                 :           0 :   const char *ptr1 = strchr (name1, sep);
     647                 :           0 :   int len1 = ptr1 ? ptr1 - name1 : strlen (name1);
     648                 :             : 
     649                 :           0 :   const char *name2 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl2));
     650                 :           0 :   const char *ptr2 = strchr (name2, sep);
     651                 :           0 :   int len2 = ptr2 ? ptr2 - name2 : strlen (name2);
     652                 :             : 
     653                 :           0 :   if (len1 != len2)
     654                 :             :     return false;
     655                 :           0 :   return !strncmp (name1, name2, len1);
     656                 :             : }
     657                 :             : 
     658                 :             : /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
     659                 :             :    violation warnings.  */
     660                 :             : 
     661                 :             : void
     662                 :          63 : compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
     663                 :             : {
     664                 :          63 :   int n1, n2;
     665                 :             : 
     666                 :          63 :   if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl))
     667                 :             :     {
     668                 :           0 :       odr_violation_reported = true;
     669                 :           0 :       if (DECL_VIRTUAL_P (prevailing->decl))
     670                 :             :         {
     671                 :           0 :           varpool_node *tmp = prevailing;
     672                 :           0 :           prevailing = vtable;
     673                 :           0 :           vtable = tmp;
     674                 :             :         }
     675                 :           0 :       auto_diagnostic_group d;
     676                 :           0 :       if (warning_at (DECL_SOURCE_LOCATION
     677                 :             :                         (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
     678                 :             :                       OPT_Wodr,
     679                 :             :                       "virtual table of type %qD violates one definition rule",
     680                 :           0 :                       DECL_CONTEXT (vtable->decl)))
     681                 :           0 :         inform (DECL_SOURCE_LOCATION (prevailing->decl),
     682                 :             :                 "variable of same assembler name as the virtual table is "
     683                 :             :                 "defined in another translation unit");
     684                 :           0 :       return;
     685                 :           0 :     }
     686                 :          63 :   if (!prevailing->definition || !vtable->definition)
     687                 :             :     return;
     688                 :             : 
     689                 :             :   /* If we do not stream ODR type info, do not bother to do useful compare.  */
     690                 :          16 :   if (!TYPE_BINFO (DECL_CONTEXT (vtable->decl))
     691                 :          16 :       || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable->decl))))
     692                 :             :     return;
     693                 :             : 
     694                 :          16 :   odr_type class_type = get_odr_type (DECL_CONTEXT (vtable->decl), true);
     695                 :             : 
     696                 :          16 :   if (class_type->odr_violated)
     697                 :             :     return;
     698                 :             : 
     699                 :          40 :   for (n1 = 0, n2 = 0; true; n1++, n2++)
     700                 :             :     {
     701                 :          56 :       struct ipa_ref *ref1, *ref2;
     702                 :          56 :       bool end1, end2;
     703                 :             : 
     704                 :          56 :       end1 = !prevailing->iterate_reference (n1, ref1);
     705                 :          56 :       end2 = !vtable->iterate_reference (n2, ref2);
     706                 :             : 
     707                 :             :       /* !DECL_VIRTUAL_P means RTTI entry;
     708                 :             :          We warn when RTTI is lost because non-RTTI prevails; we silently
     709                 :             :          accept the other case.  */
     710                 :          56 :       while (!end2
     711                 :          40 :              && (end1
     712                 :          40 :                  || (methods_equal_p (ref1->referred->decl,
     713                 :          40 :                                       ref2->referred->decl)
     714                 :          40 :                      && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL))
     715                 :          80 :              && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
     716                 :             :         {
     717                 :           0 :           if (!class_type->rtti_broken)
     718                 :             :             {
     719                 :           0 :               auto_diagnostic_group d;
     720                 :           0 :               if (warning_at (DECL_SOURCE_LOCATION
     721                 :             :                                   (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
     722                 :             :                                 OPT_Wodr,
     723                 :             :                                 "virtual table of type %qD contains RTTI "
     724                 :             :                                 "information",
     725                 :           0 :                                 DECL_CONTEXT (vtable->decl)))
     726                 :             :                 {
     727                 :           0 :                   inform (DECL_SOURCE_LOCATION
     728                 :             :                               (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
     729                 :             :                             "but is prevailed by one without from other"
     730                 :             :                             " translation unit");
     731                 :           0 :                   inform (DECL_SOURCE_LOCATION
     732                 :             :                               (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
     733                 :             :                             "RTTI will not work on this type");
     734                 :           0 :                   class_type->rtti_broken = true;
     735                 :             :                 }
     736                 :           0 :             }
     737                 :           0 :           n2++;
     738                 :           0 :           end2 = !vtable->iterate_reference (n2, ref2);
     739                 :             :         }
     740                 :             :       while (!end1
     741                 :          40 :              && (end2
     742                 :          40 :                  || (methods_equal_p (ref2->referred->decl, ref1->referred->decl)
     743                 :          40 :                      && TREE_CODE (ref2->referred->decl) == FUNCTION_DECL))
     744                 :          80 :              && TREE_CODE (ref1->referred->decl) != FUNCTION_DECL)
     745                 :             :         {
     746                 :           0 :           n1++;
     747                 :           0 :           end1 = !prevailing->iterate_reference (n1, ref1);
     748                 :             :         }
     749                 :             : 
     750                 :             :       /* Finished?  */
     751                 :          56 :       if (end1 && end2)
     752                 :             :         {
     753                 :             :           /* Extra paranoia; compare the sizes.  We do not have information
     754                 :             :              about virtual inheritance offsets, so just be sure that these
     755                 :             :              match. 
     756                 :             :              Do this as very last check so the not very informative error
     757                 :             :              is not output too often.  */
     758                 :          16 :           if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl))
     759                 :             :             {
     760                 :           0 :               class_type->odr_violated = true;
     761                 :           0 :               auto_diagnostic_group d;
     762                 :           0 :               tree ctx = TYPE_NAME (DECL_CONTEXT (vtable->decl));
     763                 :           0 :               if (warning_at (DECL_SOURCE_LOCATION (ctx), OPT_Wodr,
     764                 :             :                               "virtual table of type %qD violates "
     765                 :             :                               "one definition rule",
     766                 :           0 :                               DECL_CONTEXT (vtable->decl)))
     767                 :             :                 {
     768                 :           0 :                   ctx = TYPE_NAME (DECL_CONTEXT (prevailing->decl));
     769                 :           0 :                   inform (DECL_SOURCE_LOCATION (ctx),
     770                 :             :                           "the conflicting type defined in another translation"
     771                 :             :                           " unit has virtual table of different size");
     772                 :             :                 }
     773                 :           0 :             }
     774                 :          16 :           return;
     775                 :             :         }
     776                 :             : 
     777                 :          40 :       if (!end1 && !end2)
     778                 :             :         {
     779                 :          40 :           if (methods_equal_p (ref1->referred->decl, ref2->referred->decl))
     780                 :          40 :             continue;
     781                 :             : 
     782                 :           0 :           class_type->odr_violated = true;
     783                 :             : 
     784                 :             :           /* If the loops above stopped on non-virtual pointer, we have
     785                 :             :              mismatch in RTTI information mangling.  */
     786                 :           0 :           if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL
     787                 :           0 :               && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
     788                 :             :             {
     789                 :           0 :               auto_diagnostic_group d;
     790                 :           0 :               if (warning_at (DECL_SOURCE_LOCATION
     791                 :             :                                 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
     792                 :             :                               OPT_Wodr,
     793                 :             :                               "virtual table of type %qD violates "
     794                 :             :                               "one definition rule",
     795                 :           0 :                               DECL_CONTEXT (vtable->decl)))
     796                 :             :                 {
     797                 :           0 :                   inform (DECL_SOURCE_LOCATION
     798                 :             :                             (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
     799                 :             :                           "the conflicting type defined in another translation "
     800                 :             :                           "unit with different RTTI information");
     801                 :             :                 }
     802                 :           0 :               return;
     803                 :           0 :             }
     804                 :             :           /* At this point both REF1 and REF2 points either to virtual table
     805                 :             :              or virtual method.  If one points to virtual table and other to
     806                 :             :              method we can complain the same way as if one table was shorter
     807                 :             :              than other pointing out the extra method.  */
     808                 :           0 :           if (TREE_CODE (ref1->referred->decl)
     809                 :           0 :               != TREE_CODE (ref2->referred->decl))
     810                 :             :             {
     811                 :           0 :               if (VAR_P (ref1->referred->decl))
     812                 :             :                 end1 = true;
     813                 :           0 :               else if (VAR_P (ref2->referred->decl))
     814                 :           0 :                 end2 = true;
     815                 :             :             }
     816                 :             :         }
     817                 :             : 
     818                 :           0 :       class_type->odr_violated = true;
     819                 :             : 
     820                 :             :       /* Complain about size mismatch.  Either we have too many virtual
     821                 :             :          functions or too many virtual table pointers.  */
     822                 :           0 :       if (end1 || end2)
     823                 :             :         {
     824                 :           0 :           if (end1)
     825                 :             :             {
     826                 :           0 :               varpool_node *tmp = prevailing;
     827                 :           0 :               prevailing = vtable;
     828                 :           0 :               vtable = tmp;
     829                 :           0 :               ref1 = ref2;
     830                 :             :             }
     831                 :           0 :           auto_diagnostic_group d;
     832                 :           0 :           if (warning_at (DECL_SOURCE_LOCATION
     833                 :             :                             (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
     834                 :             :                           OPT_Wodr,
     835                 :             :                           "virtual table of type %qD violates "
     836                 :             :                           "one definition rule",
     837                 :           0 :                           DECL_CONTEXT (vtable->decl)))
     838                 :             :             {
     839                 :           0 :               if (TREE_CODE (ref1->referring->decl) == FUNCTION_DECL)
     840                 :             :                 {
     841                 :           0 :                   inform (DECL_SOURCE_LOCATION
     842                 :             :                            (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
     843                 :             :                           "the conflicting type defined in another translation "
     844                 :             :                           "unit");
     845                 :           0 :                   inform (DECL_SOURCE_LOCATION
     846                 :             :                             (TYPE_NAME (DECL_CONTEXT (ref1->referring->decl))),
     847                 :             :                           "contains additional virtual method %qD",
     848                 :           0 :                           ref1->referred->decl);
     849                 :             :                 }
     850                 :             :               else
     851                 :             :                 {
     852                 :           0 :                   inform (DECL_SOURCE_LOCATION
     853                 :             :                            (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
     854                 :             :                           "the conflicting type defined in another translation "
     855                 :             :                           "unit has virtual table with more entries");
     856                 :             :                 }
     857                 :             :             }
     858                 :           0 :           return;
     859                 :           0 :         }
     860                 :             : 
     861                 :             :       /* And in the last case we have either mismatch in between two virtual
     862                 :             :          methods or two virtual table pointers.  */
     863                 :           0 :       auto_diagnostic_group d;
     864                 :           0 :       if (warning_at (DECL_SOURCE_LOCATION
     865                 :             :                         (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr,
     866                 :             :                       "virtual table of type %qD violates "
     867                 :             :                       "one definition rule",
     868                 :           0 :                       DECL_CONTEXT (vtable->decl)))
     869                 :             :         {
     870                 :           0 :           if (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL)
     871                 :             :             {
     872                 :           0 :               inform (DECL_SOURCE_LOCATION
     873                 :             :                         (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
     874                 :             :                       "the conflicting type defined in another translation "
     875                 :             :                       "unit");
     876                 :           0 :               gcc_assert (TREE_CODE (ref2->referred->decl)
     877                 :             :                           == FUNCTION_DECL);
     878                 :           0 :               inform (DECL_SOURCE_LOCATION
     879                 :             :                          (ref1->referred->ultimate_alias_target ()->decl),
     880                 :             :                       "virtual method %qD",
     881                 :           0 :                       ref1->referred->ultimate_alias_target ()->decl);
     882                 :           0 :               inform (DECL_SOURCE_LOCATION
     883                 :             :                          (ref2->referred->ultimate_alias_target ()->decl),
     884                 :             :                       "ought to match virtual method %qD but does not",
     885                 :           0 :                       ref2->referred->ultimate_alias_target ()->decl);
     886                 :             :             }
     887                 :             :           else
     888                 :           0 :             inform (DECL_SOURCE_LOCATION
     889                 :             :                       (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
     890                 :             :                     "the conflicting type defined in another translation "
     891                 :             :                     "unit has virtual table with different contents");
     892                 :           0 :           return;
     893                 :             :         }
     894                 :          40 :     }
     895                 :             : }
     896                 :             : 
     897                 :             : /* Output ODR violation warning about T1 and T2 with REASON.
     898                 :             :    Display location of ST1 and ST2 if REASON speaks about field or
     899                 :             :    method of the type.
     900                 :             :    If WARN is false, do nothing. Set WARNED if warning was indeed
     901                 :             :    output.  */
     902                 :             : 
     903                 :             : static void
     904                 :         102 : warn_odr (tree t1, tree t2, tree st1, tree st2,
     905                 :             :           bool warn, bool *warned, const char *reason)
     906                 :             : {
     907                 :         102 :   tree decl2 = TYPE_NAME (TYPE_MAIN_VARIANT (t2));
     908                 :         102 :   if (warned)
     909                 :          79 :     *warned = false;
     910                 :             : 
     911                 :         102 :   if (!warn || !TYPE_NAME(TYPE_MAIN_VARIANT (t1)))
     912                 :          56 :     return;
     913                 :             : 
     914                 :             :   /* ODR warnings are output during LTO streaming; we must apply location
     915                 :             :      cache for potential warnings to be output correctly.  */
     916                 :          49 :   if (lto_location_cache::current_cache)
     917                 :          49 :     lto_location_cache::current_cache->apply_location_cache ();
     918                 :             : 
     919                 :          49 :   auto_diagnostic_group d;
     920                 :          49 :   if (t1 != TYPE_MAIN_VARIANT (t1)
     921                 :          49 :       && TYPE_NAME (t1) != TYPE_NAME (TYPE_MAIN_VARIANT (t1)))
     922                 :             :     {
     923                 :           0 :       if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1))),
     924                 :             :                        OPT_Wodr, "type %qT (typedef of %qT) violates the "
     925                 :             :                        "C++ One Definition Rule",
     926                 :           0 :                        t1, TYPE_MAIN_VARIANT (t1)))
     927                 :             :         return;
     928                 :             :     }
     929                 :             :   else
     930                 :             :     {
     931                 :          49 :       if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1))),
     932                 :             :                        OPT_Wodr, "type %qT violates the C++ One Definition Rule",
     933                 :             :                        t1))
     934                 :             :         return;
     935                 :             :     }
     936                 :          46 :   if (!st1 && !st2)
     937                 :             :     ;
     938                 :             :   /* For FIELD_DECL support also case where one of fields is
     939                 :             :      NULL - this is used when the structures have mismatching number of
     940                 :             :      elements.  */
     941                 :          34 :   else if (!st1 || TREE_CODE (st1) == FIELD_DECL)
     942                 :             :     {
     943                 :          34 :       inform (DECL_SOURCE_LOCATION (decl2),
     944                 :             :               "a different type is defined in another translation unit");
     945                 :          34 :       if (!st1)
     946                 :             :         {
     947                 :           1 :           st1 = st2;
     948                 :           1 :           st2 = NULL;
     949                 :             :         }
     950                 :          34 :       inform (DECL_SOURCE_LOCATION (st1),
     951                 :             :               "the first difference of corresponding definitions is field %qD",
     952                 :             :               st1);
     953                 :          34 :       if (st2)
     954                 :          46 :         decl2 = st2;
     955                 :             :     }
     956                 :           0 :   else if (TREE_CODE (st1) == FUNCTION_DECL)
     957                 :             :     {
     958                 :           0 :       inform (DECL_SOURCE_LOCATION (decl2),
     959                 :             :               "a different type is defined in another translation unit");
     960                 :           0 :       inform (DECL_SOURCE_LOCATION (st1),
     961                 :             :               "the first difference of corresponding definitions is method %qD",
     962                 :             :               st1);
     963                 :           0 :       decl2 = st2;
     964                 :             :     }
     965                 :             :   else
     966                 :             :     return;
     967                 :          46 :   inform (DECL_SOURCE_LOCATION (decl2), reason);
     968                 :             : 
     969                 :          46 :   if (warned)
     970                 :          46 :     *warned = true;
     971                 :          49 : }
     972                 :             : 
     973                 :             : /* Return true if T1 and T2 are incompatible and we want to recursively
     974                 :             :    dive into them from warn_type_mismatch to give sensible answer.  */
     975                 :             : 
     976                 :             : static bool
     977                 :           6 : type_mismatch_p (tree t1, tree t2)
     978                 :             : {
     979                 :          12 :   if (odr_or_derived_type_p (t1) && odr_or_derived_type_p (t2)
     980                 :          12 :       && !odr_types_equivalent_p (t1, t2))
     981                 :             :     return true;
     982                 :           0 :   return !types_compatible_p (t1, t2);
     983                 :             : }
     984                 :             : 
     985                 :             : 
     986                 :             : /* Types T1 and T2 was found to be incompatible in a context they can't
     987                 :             :    (either used to declare a symbol of same assembler name or unified by
     988                 :             :    ODR rule).  We already output warning about this, but if possible, output
     989                 :             :    extra information on how the types mismatch.
     990                 :             : 
     991                 :             :    This is hard to do in general.  We basically handle the common cases.
     992                 :             : 
     993                 :             :    If LOC1 and LOC2 are meaningful locations, use it in the case the types
     994                 :             :    themselves do not have one.  */
     995                 :             : 
     996                 :             : void
     997                 :          40 : warn_types_mismatch (tree t1, tree t2, location_t loc1, location_t loc2)
     998                 :             : {
     999                 :             :   /* Location of type is known only if it has TYPE_NAME and the name is
    1000                 :             :      TYPE_DECL.  */
    1001                 :          85 :   location_t loc_t1 = TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
    1002                 :          78 :                       ? DECL_SOURCE_LOCATION (TYPE_NAME (t1))
    1003                 :          46 :                       : UNKNOWN_LOCATION;
    1004                 :          79 :   location_t loc_t2 = TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
    1005                 :          71 :                       ? DECL_SOURCE_LOCATION (TYPE_NAME (t2))
    1006                 :          71 :                       : UNKNOWN_LOCATION;
    1007                 :          25 :   bool loc_t2_useful = false;
    1008                 :             : 
    1009                 :             :   /* With LTO it is a common case that the location of both types match.
    1010                 :             :      See if T2 has a location that is different from T1. If so, we will
    1011                 :             :      inform user about the location.
    1012                 :             :      Do not consider the location passed to us in LOC1/LOC2 as those are
    1013                 :             :      already output.  */
    1014                 :          25 :   if (loc_t2 > BUILTINS_LOCATION && loc_t2 != loc_t1)
    1015                 :             :     {
    1016                 :          19 :       if (loc_t1 <= BUILTINS_LOCATION)
    1017                 :             :         loc_t2_useful = true;
    1018                 :             :       else
    1019                 :             :         {
    1020                 :          19 :           expanded_location xloc1 = expand_location (loc_t1);
    1021                 :          19 :           expanded_location xloc2 = expand_location (loc_t2);
    1022                 :             : 
    1023                 :          19 :           if (strcmp (xloc1.file, xloc2.file)
    1024                 :             :               || xloc1.line != xloc2.line
    1025                 :           0 :               || xloc1.column != xloc2.column)
    1026                 :          19 :             loc_t2_useful = true;
    1027                 :             :         }
    1028                 :             :     }
    1029                 :             : 
    1030                 :          46 :   if (loc_t1 <= BUILTINS_LOCATION)
    1031                 :             :     loc_t1 = loc1;
    1032                 :          46 :   if (loc_t2 <= BUILTINS_LOCATION)
    1033                 :          27 :     loc_t2 = loc2;
    1034                 :             : 
    1035                 :          46 :   location_t loc = loc_t1 <= BUILTINS_LOCATION ? loc_t2 : loc_t1;
    1036                 :             : 
    1037                 :             :   /* It is a quite common bug to reference anonymous namespace type in
    1038                 :             :      non-anonymous namespace class.  */
    1039                 :          46 :   tree mt1 = TYPE_MAIN_VARIANT (t1);
    1040                 :          46 :   tree mt2 = TYPE_MAIN_VARIANT (t2);
    1041                 :          46 :   if ((type_with_linkage_p (mt1)
    1042                 :          31 :        && type_in_anonymous_namespace_p (mt1))
    1043                 :          71 :       || (type_with_linkage_p (mt2)
    1044                 :          26 :           && type_in_anonymous_namespace_p (mt2)))
    1045                 :             :     {
    1046                 :          13 :       if (!type_with_linkage_p (mt1)
    1047                 :          13 :           || !type_in_anonymous_namespace_p (mt1))
    1048                 :             :         {
    1049                 :             :           std::swap (t1, t2);
    1050                 :             :           std::swap (mt1, mt2);
    1051                 :             :           std::swap (loc_t1, loc_t2);
    1052                 :             :         }
    1053                 :          13 :       gcc_assert (TYPE_NAME (mt1)
    1054                 :             :                   && TREE_CODE (TYPE_NAME (mt1)) == TYPE_DECL);
    1055                 :          13 :       tree n1 = TYPE_NAME (mt1);
    1056                 :          13 :       tree n2 = TYPE_NAME (mt2) ? TYPE_NAME (mt2) : NULL;
    1057                 :             : 
    1058                 :          13 :       if (TREE_CODE (n1) == TYPE_DECL)
    1059                 :          13 :         n1 = DECL_NAME (n1);
    1060                 :          13 :       if (n2 && TREE_CODE (n2) == TYPE_DECL)
    1061                 :           7 :         n2 = DECL_NAME (n2);
    1062                 :             :       /* Most of the time, the type names will match, do not be unnecessarily
    1063                 :             :          verbose.  */
    1064                 :          13 :       if (n1 != n2)
    1065                 :           1 :         inform (loc_t1,
    1066                 :             :                 "type %qT defined in anonymous namespace cannot match "
    1067                 :             :                 "type %qT across the translation unit boundary",
    1068                 :             :                 t1, t2);
    1069                 :             :       else
    1070                 :          12 :         inform (loc_t1,
    1071                 :             :                 "type %qT defined in anonymous namespace cannot match "
    1072                 :             :                 "across the translation unit boundary",
    1073                 :             :                 t1);
    1074                 :          13 :       if (loc_t2_useful)
    1075                 :           6 :         inform (loc_t2,
    1076                 :             :                 "the incompatible type defined in another translation unit");
    1077                 :          13 :       return;
    1078                 :             :     }
    1079                 :             :   /* If types have mangled ODR names and they are different, it is most
    1080                 :             :      informative to output those.
    1081                 :             :      This also covers types defined in different namespaces.  */
    1082                 :          33 :   const char *odr1 = get_odr_name_for_type (mt1);
    1083                 :          33 :   const char *odr2 = get_odr_name_for_type (mt2);
    1084                 :          33 :   if (odr1 != NULL && odr2 != NULL && odr1 != odr2)
    1085                 :             :     {
    1086                 :           6 :       const int opts = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
    1087                 :           6 :       char *name1 = xstrdup (cplus_demangle (odr1, opts));
    1088                 :           6 :       char *name2 = cplus_demangle (odr2, opts);
    1089                 :           6 :       if (name1 && name2 && strcmp (name1, name2))
    1090                 :             :         {
    1091                 :           6 :           inform (loc_t1,
    1092                 :             :                   "type name %qs should match type name %qs",
    1093                 :             :                   name1, name2);
    1094                 :           6 :           if (loc_t2_useful)
    1095                 :           6 :             inform (loc_t2,
    1096                 :             :                     "the incompatible type is defined here");
    1097                 :           6 :           free (name1);
    1098                 :           6 :           return;
    1099                 :             :         }
    1100                 :           0 :       free (name1);
    1101                 :             :     }
    1102                 :             :   /* A tricky case are compound types.  Often they appear the same in source
    1103                 :             :      code and the mismatch is dragged in by type they are build from.
    1104                 :             :      Look for those differences in subtypes and try to be informative.  In other
    1105                 :             :      cases just output nothing because the source code is probably different
    1106                 :             :      and in this case we already output a all necessary info.  */
    1107                 :          27 :   if (!TYPE_NAME (t1) || !TYPE_NAME (t2))
    1108                 :             :     {
    1109                 :           7 :       if (TREE_CODE (t1) == TREE_CODE (t2))
    1110                 :             :         {
    1111                 :           7 :           if (TREE_CODE (t1) == ARRAY_TYPE
    1112                 :           7 :               && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
    1113                 :             :             {
    1114                 :           1 :               tree i1 = TYPE_DOMAIN (t1);
    1115                 :           1 :               tree i2 = TYPE_DOMAIN (t2);
    1116                 :             :         
    1117                 :           1 :               if (i1 && i2
    1118                 :           1 :                   && TYPE_MAX_VALUE (i1)
    1119                 :           1 :                   && TYPE_MAX_VALUE (i2)
    1120                 :           2 :                   && !operand_equal_p (TYPE_MAX_VALUE (i1),
    1121                 :           1 :                                        TYPE_MAX_VALUE (i2), 0))
    1122                 :             :                 {
    1123                 :           1 :                   inform (loc,
    1124                 :             :                           "array types have different bounds");
    1125                 :           1 :                   return;
    1126                 :             :                 }
    1127                 :             :             }
    1128                 :           0 :           if ((POINTER_TYPE_P (t1) || TREE_CODE (t1) == ARRAY_TYPE)
    1129                 :           6 :               && type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
    1130                 :           6 :             warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1, loc_t2);
    1131                 :           0 :           else if (TREE_CODE (t1) == METHOD_TYPE
    1132                 :           0 :                    || TREE_CODE (t1) == FUNCTION_TYPE)
    1133                 :             :             {
    1134                 :           0 :               tree parms1 = NULL, parms2 = NULL;
    1135                 :           0 :               int count = 1;
    1136                 :             : 
    1137                 :           0 :               if (type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
    1138                 :             :                 {
    1139                 :           0 :                   inform (loc, "return value type mismatch");
    1140                 :           0 :                   warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1,
    1141                 :             :                                        loc_t2);
    1142                 :           0 :                   return;
    1143                 :             :                 }
    1144                 :           0 :               if (prototype_p (t1) && prototype_p (t2))
    1145                 :           0 :                 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
    1146                 :           0 :                      parms1 && parms2;
    1147                 :           0 :                      parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2),
    1148                 :             :                      count++)
    1149                 :             :                   {
    1150                 :           0 :                     if (type_mismatch_p (TREE_VALUE (parms1), TREE_VALUE (parms2)))
    1151                 :             :                       {
    1152                 :           0 :                         if (count == 1 && TREE_CODE (t1) == METHOD_TYPE)
    1153                 :           0 :                           inform (loc,
    1154                 :             :                                   "implicit this pointer type mismatch");
    1155                 :             :                         else
    1156                 :           0 :                           inform (loc,
    1157                 :             :                                   "type mismatch in parameter %i",
    1158                 :           0 :                                   count - (TREE_CODE (t1) == METHOD_TYPE));
    1159                 :           0 :                         warn_types_mismatch (TREE_VALUE (parms1),
    1160                 :           0 :                                              TREE_VALUE (parms2),
    1161                 :             :                                              loc_t1, loc_t2);
    1162                 :           0 :                         return;
    1163                 :             :                       }
    1164                 :             :                   }
    1165                 :           0 :               if (parms1 || parms2)
    1166                 :             :                 {
    1167                 :           0 :                   inform (loc,
    1168                 :             :                           "types have different parameter counts");
    1169                 :           0 :                   return;
    1170                 :             :                 }
    1171                 :             :             }
    1172                 :             :         }
    1173                 :           0 :       return;
    1174                 :             :     }
    1175                 :             : 
    1176                 :          20 :   if (types_odr_comparable (t1, t2)
    1177                 :             :       /* We make assign integers mangled names to be able to handle
    1178                 :             :          signed/unsigned chars.  Accepting them here would however lead to
    1179                 :             :          confusing message like
    1180                 :             :          "type ‘const int’ itself violates the C++ One Definition Rule"  */
    1181                 :          19 :       && TREE_CODE (t1) != INTEGER_TYPE
    1182                 :          33 :       && types_same_for_odr (t1, t2))
    1183                 :          13 :     inform (loc_t1,
    1184                 :             :             "type %qT itself violates the C++ One Definition Rule", t1);
    1185                 :             :   /* Prevent pointless warnings like "struct aa" should match "struct aa".  */
    1186                 :           7 :   else if (TYPE_NAME (t1) == TYPE_NAME (t2)
    1187                 :           7 :            && TREE_CODE (t1) == TREE_CODE (t2) && !loc_t2_useful)
    1188                 :             :     return;
    1189                 :             :   else
    1190                 :           6 :     inform (loc_t1, "type %qT should match type %qT",
    1191                 :             :             t1, t2);
    1192                 :          19 :   if (loc_t2_useful)
    1193                 :           7 :     inform (loc_t2, "the incompatible type is defined here");
    1194                 :             : }
    1195                 :             : 
    1196                 :             : /* Return true if T should be ignored in TYPE_FIELDS for ODR comparison.  */
    1197                 :             : 
    1198                 :             : static bool
    1199                 :         682 : skip_in_fields_list_p (tree t)
    1200                 :             : {
    1201                 :         682 :   if (TREE_CODE (t) != FIELD_DECL)
    1202                 :             :     return true;
    1203                 :             :   /* C++ FE introduces zero sized fields depending on -std setting, see
    1204                 :             :      PR89358.  */
    1205                 :         682 :   if (DECL_SIZE (t)
    1206                 :         682 :       && integer_zerop (DECL_SIZE (t))
    1207                 :           2 :       && DECL_ARTIFICIAL (t)
    1208                 :           2 :       && DECL_IGNORED_P (t)
    1209                 :         684 :       && !DECL_NAME (t))
    1210                 :             :     return true;
    1211                 :             :   return false;
    1212                 :             : }
    1213                 :             : 
    1214                 :             : /* Compare T1 and T2, report ODR violations if WARN is true and set
    1215                 :             :    WARNED to true if anything is reported.  Return true if types match.
    1216                 :             :    If true is returned, the types are also compatible in the sense of
    1217                 :             :    gimple_canonical_types_compatible_p.
    1218                 :             :    If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning
    1219                 :             :    about the type if the type itself do not have location.  */
    1220                 :             : 
    1221                 :             : static bool
    1222                 :        7104 : odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
    1223                 :             :                         hash_set<type_pair> *visited,
    1224                 :             :                         location_t loc1, location_t loc2)
    1225                 :             : {
    1226                 :             :   /* If we are asked to warn, we need warned to keep track if warning was
    1227                 :             :      output.  */
    1228                 :        7104 :   gcc_assert (!warn || warned);
    1229                 :             :   /* Check first for the obvious case of pointer identity.  */
    1230                 :        7104 :   if (t1 == t2)
    1231                 :             :     return true;
    1232                 :             : 
    1233                 :             :   /* Can't be the same type if the types don't have the same code.  */
    1234                 :        7104 :   if (TREE_CODE (t1) != TREE_CODE (t2))
    1235                 :             :     {
    1236                 :          13 :       warn_odr (t1, t2, NULL, NULL, warn, warned,
    1237                 :             :                 G_("a different type is defined in another translation unit"));
    1238                 :          13 :       return false;
    1239                 :             :     }
    1240                 :             : 
    1241                 :        7091 :   if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1))
    1242                 :        6998 :        && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
    1243                 :       14089 :       || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2))
    1244                 :        6998 :           && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2))))
    1245                 :             :     {
    1246                 :             :       /* We cannot trip this when comparing ODR types, only when trying to
    1247                 :             :          match different ODR derivations from different declarations.
    1248                 :             :          So WARN should be always false.  */
    1249                 :           6 :       gcc_assert (!warn);
    1250                 :             :       return false;
    1251                 :             :     }
    1252                 :             : 
    1253                 :             :   /* Non-aggregate types can be handled cheaply.  */
    1254                 :        7085 :   if (INTEGRAL_TYPE_P (t1)
    1255                 :        7085 :       || SCALAR_FLOAT_TYPE_P (t1)
    1256                 :        7069 :       || FIXED_POINT_TYPE_P (t1)
    1257                 :        7069 :       || VECTOR_TYPE_P (t1)
    1258                 :        7069 :       || TREE_CODE (t1) == COMPLEX_TYPE
    1259                 :        7069 :       || TREE_CODE (t1) == OFFSET_TYPE
    1260                 :        7069 :       || POINTER_TYPE_P (t1))
    1261                 :             :     {
    1262                 :          29 :       if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
    1263                 :             :         {
    1264                 :           0 :           warn_odr (t1, t2, NULL, NULL, warn, warned,
    1265                 :             :                     G_("a type with different precision is defined "
    1266                 :             :                        "in another translation unit"));
    1267                 :           0 :           return false;
    1268                 :             :         }
    1269                 :          29 :       if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
    1270                 :             :         {
    1271                 :           0 :           warn_odr (t1, t2, NULL, NULL, warn, warned,
    1272                 :             :                     G_("a type with different signedness is defined "
    1273                 :             :                        "in another translation unit"));
    1274                 :           0 :           return false;
    1275                 :             :         }
    1276                 :             : 
    1277                 :          29 :       if (TREE_CODE (t1) == INTEGER_TYPE
    1278                 :          29 :           && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
    1279                 :             :         {
    1280                 :             :           /* char WRT uint_8?  */
    1281                 :           0 :           warn_odr (t1, t2, NULL, NULL, warn, warned,
    1282                 :             :                     G_("a different type is defined in another "
    1283                 :             :                        "translation unit"));
    1284                 :           0 :           return false;
    1285                 :             :         }
    1286                 :             : 
    1287                 :             :       /* For canonical type comparisons we do not want to build SCCs
    1288                 :             :          so we cannot compare pointed-to types.  But we can, for now,
    1289                 :             :          require the same pointed-to type kind and match what
    1290                 :             :          useless_type_conversion_p would do.  */
    1291                 :          29 :       if (POINTER_TYPE_P (t1))
    1292                 :             :         {
    1293                 :          13 :           if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
    1294                 :          13 :               != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
    1295                 :             :             {
    1296                 :           0 :               warn_odr (t1, t2, NULL, NULL, warn, warned,
    1297                 :             :                         G_("it is defined as a pointer in different address "
    1298                 :             :                            "space in another translation unit"));
    1299                 :           0 :               return false;
    1300                 :             :             }
    1301                 :             : 
    1302                 :          13 :           if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
    1303                 :             :                                           visited, loc1, loc2))
    1304                 :             :             {
    1305                 :           9 :               warn_odr (t1, t2, NULL, NULL, warn, warned,
    1306                 :             :                         G_("it is defined as a pointer to different type "
    1307                 :             :                            "in another translation unit"));
    1308                 :           9 :               if (warn && *warned)
    1309                 :           0 :                 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2),
    1310                 :             :                                      loc1, loc2);
    1311                 :           9 :               return false;
    1312                 :             :             }
    1313                 :             :         }
    1314                 :             : 
    1315                 :          20 :       if ((VECTOR_TYPE_P (t1) || TREE_CODE (t1) == COMPLEX_TYPE)
    1316                 :          20 :           && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
    1317                 :             :                                          visited, loc1, loc2))
    1318                 :             :         {
    1319                 :             :           /* Probably specific enough.  */
    1320                 :           0 :           warn_odr (t1, t2, NULL, NULL, warn, warned,
    1321                 :             :                     G_("a different type is defined "
    1322                 :             :                        "in another translation unit"));
    1323                 :           0 :           if (warn && *warned)
    1324                 :           0 :             warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
    1325                 :           0 :           return false;
    1326                 :             :         }
    1327                 :             :     }
    1328                 :             :   /* Do type-specific comparisons.  */
    1329                 :        7056 :   else switch (TREE_CODE (t1))
    1330                 :             :     {
    1331                 :           2 :     case ARRAY_TYPE:
    1332                 :           2 :       {
    1333                 :             :         /* Array types are the same if the element types are the same and
    1334                 :             :            the number of elements are the same.  */
    1335                 :           2 :         if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
    1336                 :             :                                         visited, loc1, loc2))
    1337                 :             :           {
    1338                 :           0 :             warn_odr (t1, t2, NULL, NULL, warn, warned,
    1339                 :             :                       G_("a different type is defined in another "
    1340                 :             :                          "translation unit"));
    1341                 :           0 :             if (warn && *warned)
    1342                 :           0 :               warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
    1343                 :             :           }
    1344                 :           2 :         gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
    1345                 :           2 :         gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
    1346                 :             :                     == TYPE_NONALIASED_COMPONENT (t2));
    1347                 :             : 
    1348                 :           2 :         tree i1 = TYPE_DOMAIN (t1);
    1349                 :           2 :         tree i2 = TYPE_DOMAIN (t2);
    1350                 :             : 
    1351                 :             :         /* For an incomplete external array, the type domain can be
    1352                 :             :            NULL_TREE.  Check this condition also.  */
    1353                 :           2 :         if (i1 == NULL_TREE || i2 == NULL_TREE)
    1354                 :           0 :           return type_variants_equivalent_p (t1, t2);
    1355                 :             : 
    1356                 :           2 :         tree min1 = TYPE_MIN_VALUE (i1);
    1357                 :           2 :         tree min2 = TYPE_MIN_VALUE (i2);
    1358                 :           2 :         tree max1 = TYPE_MAX_VALUE (i1);
    1359                 :           2 :         tree max2 = TYPE_MAX_VALUE (i2);
    1360                 :             : 
    1361                 :             :         /* In C++, minimums should be always 0.  */
    1362                 :           2 :         gcc_assert (min1 == min2);
    1363                 :           2 :         if (!operand_equal_p (max1, max2, 0))
    1364                 :             :           {
    1365                 :           2 :             warn_odr (t1, t2, NULL, NULL, warn, warned,
    1366                 :             :                       G_("an array of different size is defined "
    1367                 :             :                          "in another translation unit"));
    1368                 :           2 :             return false;
    1369                 :             :           }
    1370                 :             :       }
    1371                 :             :     break;
    1372                 :             : 
    1373                 :          78 :     case METHOD_TYPE:
    1374                 :          78 :     case FUNCTION_TYPE:
    1375                 :             :       /* Function types are the same if the return type and arguments types
    1376                 :             :          are the same.  */
    1377                 :          78 :       if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
    1378                 :             :                                       visited, loc1, loc2))
    1379                 :             :         {
    1380                 :           0 :           warn_odr (t1, t2, NULL, NULL, warn, warned,
    1381                 :             :                     G_("has different return value "
    1382                 :             :                        "in another translation unit"));
    1383                 :           0 :           if (warn && *warned)
    1384                 :           0 :             warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
    1385                 :           0 :           return false;
    1386                 :             :         }
    1387                 :             : 
    1388                 :          78 :       if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2)
    1389                 :          78 :           || !prototype_p (t1) || !prototype_p (t2))
    1390                 :          78 :         return type_variants_equivalent_p (t1, t2);
    1391                 :             :       else
    1392                 :             :         {
    1393                 :           0 :           tree parms1, parms2;
    1394                 :             : 
    1395                 :           0 :           for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
    1396                 :           0 :                parms1 && parms2;
    1397                 :           0 :                parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
    1398                 :             :             {
    1399                 :           0 :               if (!odr_subtypes_equivalent_p
    1400                 :           0 :                      (TREE_VALUE (parms1), TREE_VALUE (parms2),
    1401                 :             :                       visited, loc1, loc2))
    1402                 :             :                 {
    1403                 :           0 :                   warn_odr (t1, t2, NULL, NULL, warn, warned,
    1404                 :             :                             G_("has different parameters in another "
    1405                 :             :                                "translation unit"));
    1406                 :           0 :                   if (warn && *warned)
    1407                 :           0 :                     warn_types_mismatch (TREE_VALUE (parms1),
    1408                 :           0 :                                          TREE_VALUE (parms2), loc1, loc2);
    1409                 :           0 :                   return false;
    1410                 :             :                 }
    1411                 :             :             }
    1412                 :             : 
    1413                 :           0 :           if (parms1 || parms2)
    1414                 :             :             {
    1415                 :           0 :               warn_odr (t1, t2, NULL, NULL, warn, warned,
    1416                 :             :                         G_("has different parameters "
    1417                 :             :                            "in another translation unit"));
    1418                 :           0 :               return false;
    1419                 :             :             }
    1420                 :             : 
    1421                 :           0 :           return type_variants_equivalent_p (t1, t2);
    1422                 :             :         }
    1423                 :             : 
    1424                 :        6976 :     case RECORD_TYPE:
    1425                 :        6976 :     case UNION_TYPE:
    1426                 :        6976 :     case QUAL_UNION_TYPE:
    1427                 :        6976 :       {
    1428                 :        6976 :         tree f1, f2;
    1429                 :             : 
    1430                 :             :         /* For aggregate types, all the fields must be the same.  */
    1431                 :        6976 :         if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
    1432                 :             :           {
    1433                 :         406 :             if (TYPE_BINFO (t1) && TYPE_BINFO (t2)
    1434                 :         573 :                 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
    1435                 :         167 :                    != polymorphic_type_binfo_p (TYPE_BINFO (t2)))
    1436                 :             :               {
    1437                 :           0 :                 if (polymorphic_type_binfo_p (TYPE_BINFO (t1)))
    1438                 :           0 :                   warn_odr (t1, t2, NULL, NULL, warn, warned,
    1439                 :             :                             G_("a type defined in another translation unit "
    1440                 :             :                                "is not polymorphic"));
    1441                 :             :                 else
    1442                 :           0 :                   warn_odr (t1, t2, NULL, NULL, warn, warned,
    1443                 :             :                             G_("a type defined in another translation unit "
    1444                 :             :                                "is polymorphic"));
    1445                 :           0 :                 return false;
    1446                 :             :               }
    1447                 :         239 :             for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
    1448                 :         520 :                  f1 || f2;
    1449                 :         281 :                  f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
    1450                 :             :               {
    1451                 :             :                 /* Skip non-fields.  */
    1452                 :         350 :                 while (f1 && skip_in_fields_list_p (f1))
    1453                 :           2 :                   f1 = TREE_CHAIN (f1);
    1454                 :         348 :                 while (f2 && skip_in_fields_list_p (f2))
    1455                 :           0 :                   f2 = TREE_CHAIN (f2);
    1456                 :         348 :                 if (!f1 || !f2)
    1457                 :             :                   break;
    1458                 :         332 :                 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
    1459                 :             :                   {
    1460                 :           0 :                     warn_odr (t1, t2, NULL, NULL, warn, warned,
    1461                 :             :                               G_("a type with different virtual table pointers"
    1462                 :             :                                  " is defined in another translation unit"));
    1463                 :           0 :                     return false;
    1464                 :             :                   }
    1465                 :         332 :                 if (DECL_ARTIFICIAL (f1) != DECL_ARTIFICIAL (f2))
    1466                 :             :                   {
    1467                 :           0 :                     warn_odr (t1, t2, NULL, NULL, warn, warned,
    1468                 :             :                               G_("a type with different bases is defined "
    1469                 :             :                                  "in another translation unit"));
    1470                 :           0 :                     return false;
    1471                 :             :                   }
    1472                 :         332 :                 if (DECL_NAME (f1) != DECL_NAME (f2)
    1473                 :         332 :                     && !DECL_ARTIFICIAL (f1))
    1474                 :             :                   {
    1475                 :           0 :                     warn_odr (t1, t2, f1, f2, warn, warned,
    1476                 :             :                               G_("a field with different name is defined "
    1477                 :             :                                  "in another translation unit"));
    1478                 :           0 :                     return false;
    1479                 :             :                   }
    1480                 :         332 :                 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1),
    1481                 :         332 :                                                 TREE_TYPE (f2),
    1482                 :             :                                                 visited, loc1, loc2))
    1483                 :             :                   {
    1484                 :             :                     /* Do not warn about artificial fields and just go into
    1485                 :             :                        generic field mismatch warning.  */
    1486                 :          51 :                     if (DECL_ARTIFICIAL (f1))
    1487                 :             :                       break;
    1488                 :             : 
    1489                 :          51 :                     warn_odr (t1, t2, f1, f2, warn, warned,
    1490                 :             :                               G_("a field of same name but different type "
    1491                 :             :                                  "is defined in another translation unit"));
    1492                 :          51 :                     if (warn && *warned)
    1493                 :          27 :                       warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2), loc1, loc2);
    1494                 :          51 :                     return false;
    1495                 :             :                   }
    1496                 :         281 :                 if (!gimple_compare_field_offset (f1, f2))
    1497                 :             :                   {
    1498                 :             :                     /* Do not warn about artificial fields and just go into
    1499                 :             :                        generic field mismatch warning.  */
    1500                 :           0 :                     if (DECL_ARTIFICIAL (f1))
    1501                 :             :                       break;
    1502                 :           0 :                     warn_odr (t1, t2, f1, f2, warn, warned,
    1503                 :             :                               G_("fields have different layout "
    1504                 :             :                                  "in another translation unit"));
    1505                 :           0 :                     return false;
    1506                 :             :                   }
    1507                 :         281 :                 if (DECL_BIT_FIELD (f1) != DECL_BIT_FIELD (f2))
    1508                 :             :                   {
    1509                 :           0 :                     warn_odr (t1, t2, f1, f2, warn, warned,
    1510                 :             :                               G_("one field is a bitfield while the other "
    1511                 :             :                                  "is not"));
    1512                 :           0 :                     return false;
    1513                 :             :                   }
    1514                 :             :                 else
    1515                 :         281 :                   gcc_assert (DECL_NONADDRESSABLE_P (f1)
    1516                 :             :                               == DECL_NONADDRESSABLE_P (f2));
    1517                 :             :               }
    1518                 :             : 
    1519                 :             :             /* If one aggregate has more fields than the other, they
    1520                 :             :                are not the same.  */
    1521                 :         188 :             if (f1 || f2)
    1522                 :             :               {
    1523                 :          16 :                 if ((f1 && DECL_VIRTUAL_P (f1)) || (f2 && DECL_VIRTUAL_P (f2)))
    1524                 :           0 :                   warn_odr (t1, t2, NULL, NULL, warn, warned,
    1525                 :             :                             G_("a type with different virtual table pointers"
    1526                 :             :                                " is defined in another translation unit"));
    1527                 :           8 :                 else if ((f1 && DECL_ARTIFICIAL (f1))
    1528                 :          24 :                          || (f2 && DECL_ARTIFICIAL (f2)))
    1529                 :           2 :                   warn_odr (t1, t2, NULL, NULL, warn, warned,
    1530                 :             :                             G_("a type with different bases is defined "
    1531                 :             :                                "in another translation unit"));
    1532                 :             :                 else
    1533                 :          14 :                   warn_odr (t1, t2, f1, f2, warn, warned,
    1534                 :             :                             G_("a type with different number of fields "
    1535                 :             :                                "is defined in another translation unit"));
    1536                 :             :                 
    1537                 :          16 :                 return false;
    1538                 :             :               }
    1539                 :             :           }
    1540                 :             :         break;
    1541                 :             :       }
    1542                 :             :     case VOID_TYPE:
    1543                 :             :     case OPAQUE_TYPE:
    1544                 :             :     case NULLPTR_TYPE:
    1545                 :             :       break;
    1546                 :             : 
    1547                 :           0 :     default:
    1548                 :           0 :       debug_tree (t1);
    1549                 :           0 :       gcc_unreachable ();
    1550                 :             :     }
    1551                 :             : 
    1552                 :             :   /* Those are better to come last as they are utterly uninformative.  */
    1553                 :       13858 :   if (TYPE_SIZE (t1) && TYPE_SIZE (t2)
    1554                 :        7116 :       && !operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0))
    1555                 :             :     {
    1556                 :           0 :       warn_odr (t1, t2, NULL, NULL, warn, warned,
    1557                 :             :                 G_("a type with different size "
    1558                 :             :                    "is defined in another translation unit"));
    1559                 :           0 :       return false;
    1560                 :             :     }
    1561                 :             : 
    1562                 :        6929 :   if (TREE_ADDRESSABLE (t1) != TREE_ADDRESSABLE (t2)
    1563                 :        6929 :       && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
    1564                 :             :     {
    1565                 :           9 :       warn_odr (t1, t2, NULL, NULL, warn, warned,
    1566                 :             :                 G_("one type needs to be constructed while the other does not"));
    1567                 :           9 :       gcc_checking_assert (RECORD_OR_UNION_TYPE_P (t1));
    1568                 :             :       return false;
    1569                 :             :     }
    1570                 :             :   /* There is no really good user facing warning for this.
    1571                 :             :      Either the original reason for modes being different is lost during
    1572                 :             :      streaming or we should catch earlier warnings.  We however must detect
    1573                 :             :      the mismatch to avoid type verifier from cmplaining on mismatched
    1574                 :             :      types between type and canonical type. See PR91576.  */
    1575                 :        6920 :   if (TYPE_MODE (t1) != TYPE_MODE (t2)
    1576                 :        6920 :       && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
    1577                 :             :     {
    1578                 :           0 :       warn_odr (t1, t2, NULL, NULL, warn, warned,
    1579                 :             :                 G_("memory layout mismatch"));
    1580                 :           0 :       return false;
    1581                 :             :     }
    1582                 :             : 
    1583                 :        6920 :   gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2)
    1584                 :             :               || operand_equal_p (TYPE_SIZE_UNIT (t1),
    1585                 :             :                                   TYPE_SIZE_UNIT (t2), 0));
    1586                 :        6920 :   return type_variants_equivalent_p (t1, t2);
    1587                 :             : }
    1588                 :             : 
    1589                 :             : /* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule.  */
    1590                 :             : 
    1591                 :             : bool
    1592                 :         176 : odr_types_equivalent_p (tree type1, tree type2)
    1593                 :             : {
    1594                 :         176 :   gcc_checking_assert (odr_or_derived_type_p (type1)
    1595                 :             :                        && odr_or_derived_type_p (type2));
    1596                 :             : 
    1597                 :         176 :   hash_set<type_pair> visited;
    1598                 :         176 :   return odr_types_equivalent_p (type1, type2, false, NULL,
    1599                 :         176 :                                  &visited, UNKNOWN_LOCATION, UNKNOWN_LOCATION);
    1600                 :         176 : }
    1601                 :             : 
    1602                 :             : /* TYPE is equivalent to VAL by ODR, but its tree representation differs
    1603                 :             :    from VAL->type.  This may happen in LTO where tree merging did not merge
    1604                 :             :    all variants of the same type or due to ODR violation.
    1605                 :             : 
    1606                 :             :    Analyze and report ODR violations and add type to duplicate list.
    1607                 :             :    If TYPE is more specified than VAL->type, prevail VAL->type.  Also if
    1608                 :             :    this is first time we see definition of a class return true so the
    1609                 :             :    base types are analyzed.  */
    1610                 :             : 
    1611                 :             : static bool
    1612                 :        6956 : add_type_duplicate (odr_type val, tree type)
    1613                 :             : {
    1614                 :        6956 :   bool build_bases = false;
    1615                 :        6956 :   bool prevail = false;
    1616                 :        6956 :   bool odr_must_violate = false;
    1617                 :             : 
    1618                 :        6956 :   if (!val->types_set)
    1619                 :        6823 :     val->types_set = new hash_set<tree>;
    1620                 :             : 
    1621                 :             :   /* Chose polymorphic type as leader (this happens only in case of ODR
    1622                 :             :      violations.  */
    1623                 :        6823 :   if ((TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
    1624                 :         243 :        && polymorphic_type_binfo_p (TYPE_BINFO (type)))
    1625                 :        7199 :       && (TREE_CODE (val->type) != RECORD_TYPE || !TYPE_BINFO (val->type)
    1626                 :          89 :           || !polymorphic_type_binfo_p (TYPE_BINFO (val->type))))
    1627                 :             :     {
    1628                 :             :       prevail = true;
    1629                 :             :       build_bases = true;
    1630                 :             :     }
    1631                 :             :   /* Always prefer complete type to be the leader.  */
    1632                 :        6802 :   else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type))
    1633                 :             :     {
    1634                 :        1156 :       prevail = true;
    1635                 :        1156 :       if (TREE_CODE (type) == RECORD_TYPE)
    1636                 :        1138 :         build_bases = TYPE_BINFO (type);
    1637                 :             :     }
    1638                 :        5646 :   else if (COMPLETE_TYPE_P (val->type) && !COMPLETE_TYPE_P (type))
    1639                 :             :     ;
    1640                 :         208 :   else if (TREE_CODE (val->type) == RECORD_TYPE
    1641                 :         140 :            && TREE_CODE (type) == RECORD_TYPE
    1642                 :         347 :            && TYPE_BINFO (type) && !TYPE_BINFO (val->type))
    1643                 :             :     {
    1644                 :           0 :       gcc_assert (!val->bases.length ());
    1645                 :             :       build_bases = true;
    1646                 :             :       prevail = true;
    1647                 :             :     }
    1648                 :             : 
    1649                 :        1138 :   if (prevail)
    1650                 :        1310 :     std::swap (val->type, type);
    1651                 :             : 
    1652                 :        6956 :   val->types_set->add (type);
    1653                 :             : 
    1654                 :        6956 :   if (!odr_hash)
    1655                 :             :     return false;
    1656                 :             : 
    1657                 :       20868 :   gcc_checking_assert (can_be_name_hashed_p (type)
    1658                 :             :                        && can_be_name_hashed_p (val->type));
    1659                 :             : 
    1660                 :        6956 :   bool merge = true;
    1661                 :        6956 :   bool base_mismatch = false;
    1662                 :        6956 :   unsigned int i;
    1663                 :        6956 :   bool warned = false;
    1664                 :        6956 :   hash_set<type_pair> visited;
    1665                 :             : 
    1666                 :        6956 :   gcc_assert (in_lto_p);
    1667                 :        6956 :   vec_safe_push (val->types, type);
    1668                 :             : 
    1669                 :             :   /* If both are class types, compare the bases.  */
    1670                 :        7164 :   if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
    1671                 :         208 :       && TREE_CODE (val->type) == RECORD_TYPE
    1672                 :         140 :       && TREE_CODE (type) == RECORD_TYPE
    1673                 :        7095 :       && TYPE_BINFO (val->type) && TYPE_BINFO (type))
    1674                 :             :     {
    1675                 :         178 :       if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
    1676                 :          89 :           != BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
    1677                 :             :         {
    1678                 :           0 :           if (!flag_ltrans && !warned && !val->odr_violated)
    1679                 :             :             {
    1680                 :           0 :               tree extra_base;
    1681                 :           0 :               warn_odr (type, val->type, NULL, NULL, !warned, &warned,
    1682                 :             :                         "a type with the same name but different "
    1683                 :             :                         "number of polymorphic bases is "
    1684                 :             :                         "defined in another translation unit");
    1685                 :           0 :               if (warned)
    1686                 :             :                 {
    1687                 :           0 :                   if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
    1688                 :           0 :                       > BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
    1689                 :           0 :                     extra_base = BINFO_BASE_BINFO
    1690                 :             :                                  (TYPE_BINFO (type),
    1691                 :             :                                   BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)));
    1692                 :             :                   else
    1693                 :           0 :                     extra_base = BINFO_BASE_BINFO
    1694                 :             :                                  (TYPE_BINFO (val->type),
    1695                 :             :                                   BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
    1696                 :           0 :                   tree extra_base_type = BINFO_TYPE (extra_base);
    1697                 :           0 :                   inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type)),
    1698                 :             :                           "the extra base is defined here");
    1699                 :             :                 }
    1700                 :             :             }
    1701                 :             :           base_mismatch = true;
    1702                 :             :         }
    1703                 :             :       else
    1704                 :         139 :         for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
    1705                 :             :           {
    1706                 :          52 :             tree base1 = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
    1707                 :          52 :             tree base2 = BINFO_BASE_BINFO (TYPE_BINFO (val->type), i);
    1708                 :          52 :             tree type1 = BINFO_TYPE (base1);
    1709                 :          52 :             tree type2 = BINFO_TYPE (base2);
    1710                 :             : 
    1711                 :          52 :             if (types_odr_comparable (type1, type2))
    1712                 :             :               {
    1713                 :          52 :                 if (!types_same_for_odr (type1, type2))
    1714                 :             :                   base_mismatch = true;
    1715                 :             :               }
    1716                 :             :             else
    1717                 :           0 :               if (!odr_types_equivalent_p (type1, type2))
    1718                 :             :                 base_mismatch = true;
    1719                 :          52 :             if (base_mismatch)
    1720                 :             :               {
    1721                 :           0 :                 if (!warned && !val->odr_violated)
    1722                 :             :                   {
    1723                 :           0 :                     warn_odr (type, val->type, NULL, NULL,
    1724                 :             :                               !warned, &warned,
    1725                 :             :                               "a type with the same name but different base "
    1726                 :             :                               "type is defined in another translation unit");
    1727                 :           0 :                     if (warned)
    1728                 :           0 :                       warn_types_mismatch (type1, type2,
    1729                 :             :                                             UNKNOWN_LOCATION, UNKNOWN_LOCATION);
    1730                 :             :                   }
    1731                 :             :                 break;
    1732                 :             :               }
    1733                 :          52 :             if (BINFO_OFFSET (base1) != BINFO_OFFSET (base2))
    1734                 :             :               {
    1735                 :           2 :                 base_mismatch = true;
    1736                 :           2 :                 if (!warned && !val->odr_violated)
    1737                 :           2 :                   warn_odr (type, val->type, NULL, NULL,
    1738                 :             :                             !warned, &warned,
    1739                 :             :                             "a type with the same name but different base "
    1740                 :             :                             "layout is defined in another translation unit");
    1741                 :             :                 break;
    1742                 :             :               }
    1743                 :             :             /* One of bases is not of complete type.  */
    1744                 :          50 :             if (!TYPE_BINFO (type1) != !TYPE_BINFO (type2))
    1745                 :             :               {
    1746                 :             :                 /* If we have a polymorphic type info specified for TYPE1
    1747                 :             :                    but not for TYPE2 we possibly missed a base when recording
    1748                 :             :                    VAL->type earlier.
    1749                 :             :                    Be sure this does not happen.  */
    1750                 :           0 :                 if (TYPE_BINFO (type1)
    1751                 :           0 :                     && polymorphic_type_binfo_p (TYPE_BINFO (type1))
    1752                 :           0 :                     && !build_bases)
    1753                 :             :                   odr_must_violate = true;
    1754                 :             :                 break;
    1755                 :             :               }
    1756                 :             :             /* One base is polymorphic and the other not.
    1757                 :             :                This ought to be diagnosed earlier, but do not ICE in the
    1758                 :             :                checking bellow.  */
    1759                 :          50 :             else if (TYPE_BINFO (type1)
    1760                 :         100 :                      && polymorphic_type_binfo_p (TYPE_BINFO (type1))
    1761                 :          50 :                         != polymorphic_type_binfo_p (TYPE_BINFO (type2)))
    1762                 :             :               {
    1763                 :           0 :                 if (!warned && !val->odr_violated)
    1764                 :           0 :                   warn_odr (type, val->type, NULL, NULL,
    1765                 :             :                             !warned, &warned,
    1766                 :             :                             "a base of the type is polymorphic only in one "
    1767                 :             :                             "translation unit");
    1768                 :             :                 base_mismatch = true;
    1769                 :             :                 break;
    1770                 :             :               }
    1771                 :             :           }
    1772                 :          89 :       if (base_mismatch)
    1773                 :             :         {
    1774                 :           2 :           merge = false;
    1775                 :           2 :           odr_violation_reported = true;
    1776                 :           2 :           val->odr_violated = true;
    1777                 :             : 
    1778                 :           2 :           if (symtab->dump_file)
    1779                 :             :             {
    1780                 :           0 :               fprintf (symtab->dump_file, "ODR base violation\n");
    1781                 :             :             
    1782                 :           0 :               print_node (symtab->dump_file, "", val->type, 0);
    1783                 :           0 :               putc ('\n',symtab->dump_file);
    1784                 :           0 :               print_node (symtab->dump_file, "", type, 0);
    1785                 :           0 :               putc ('\n',symtab->dump_file);
    1786                 :             :             }
    1787                 :             :         }
    1788                 :             :     }
    1789                 :             : 
    1790                 :             :   /* Next compare memory layout.
    1791                 :             :      The DECL_SOURCE_LOCATIONs in this invocation came from LTO streaming.
    1792                 :             :      We must apply the location cache to ensure that they are valid
    1793                 :             :      before we can pass them to odr_types_equivalent_p (PR lto/83121).  */
    1794                 :        6956 :   if (lto_location_cache::current_cache)
    1795                 :        6956 :     lto_location_cache::current_cache->apply_location_cache ();
    1796                 :             :   /* As a special case we stream mangles names of integer types so we can see
    1797                 :             :      if they are believed to be same even though they have different
    1798                 :             :      representation.  Avoid bogus warning on mismatches in these.  */
    1799                 :        6956 :   if (TREE_CODE (type) != INTEGER_TYPE
    1800                 :        6915 :       && TREE_CODE (val->type) != INTEGER_TYPE
    1801                 :       13871 :       && !odr_types_equivalent_p (val->type, type,
    1802                 :        6915 :                                !flag_ltrans && !val->odr_violated && !warned,
    1803                 :             :                                &warned, &visited,
    1804                 :        6915 :                                DECL_SOURCE_LOCATION (TYPE_NAME (val->type)),
    1805                 :        6915 :                                DECL_SOURCE_LOCATION (TYPE_NAME (type))))
    1806                 :             :     {
    1807                 :          77 :       merge = false;
    1808                 :          77 :       odr_violation_reported = true;
    1809                 :          77 :       val->odr_violated = true;
    1810                 :             :     }
    1811                 :        6956 :   gcc_assert (val->odr_violated || !odr_must_violate);
    1812                 :             :   /* Sanity check that all bases will be build same way again.  */
    1813                 :        6956 :   if (flag_checking
    1814                 :        6956 :       && COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
    1815                 :         208 :       && TREE_CODE (val->type) == RECORD_TYPE
    1816                 :         140 :       && TREE_CODE (type) == RECORD_TYPE
    1817                 :         139 :       && TYPE_BINFO (val->type) && TYPE_BINFO (type)
    1818                 :          89 :       && !val->odr_violated
    1819                 :        7041 :       && !base_mismatch && val->bases.length ())
    1820                 :             :     {
    1821                 :             :       unsigned int num_poly_bases = 0;
    1822                 :             :       unsigned int j;
    1823                 :             : 
    1824                 :          91 :       for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
    1825                 :          50 :         if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
    1826                 :             :                                          (TYPE_BINFO (type), i)))
    1827                 :          50 :           num_poly_bases++;
    1828                 :          41 :       gcc_assert (num_poly_bases == val->bases.length ());
    1829                 :          91 :       for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type));
    1830                 :             :            i++)
    1831                 :          50 :         if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
    1832                 :             :                                        (TYPE_BINFO (type), i)))
    1833                 :             :           {
    1834                 :          50 :             odr_type base = get_odr_type
    1835                 :          50 :                                (BINFO_TYPE
    1836                 :             :                                   (BINFO_BASE_BINFO (TYPE_BINFO (type),
    1837                 :             :                                                      i)),
    1838                 :             :                                 true);
    1839                 :          50 :             gcc_assert (val->bases[j] == base);
    1840                 :          50 :             j++;
    1841                 :             :           }
    1842                 :             :     }
    1843                 :             : 
    1844                 :             : 
    1845                 :             :   /* Regularize things a little.  During LTO same types may come with
    1846                 :             :      different BINFOs.  Either because their virtual table was
    1847                 :             :      not merged by tree merging and only later at decl merging or
    1848                 :             :      because one type comes with external vtable, while other
    1849                 :             :      with internal.  We want to merge equivalent binfos to conserve
    1850                 :             :      memory and streaming overhead.
    1851                 :             : 
    1852                 :             :      The external vtables are more harmful: they contain references
    1853                 :             :      to external declarations of methods that may be defined in the
    1854                 :             :      merged LTO unit.  For this reason we absolutely need to remove
    1855                 :             :      them and replace by internal variants. Not doing so will lead
    1856                 :             :      to incomplete answers from possible_polymorphic_call_targets.
    1857                 :             : 
    1858                 :             :      FIXME: disable for now; because ODR types are now build during
    1859                 :             :      streaming in, the variants do not need to be linked to the type,
    1860                 :             :      yet.  We need to do the merging in cleanup pass to be implemented
    1861                 :             :      soon.  */
    1862                 :        6956 :   if (!flag_ltrans && merge
    1863                 :             :       && 0
    1864                 :             :       && TREE_CODE (val->type) == RECORD_TYPE
    1865                 :             :       && TREE_CODE (type) == RECORD_TYPE
    1866                 :             :       && TYPE_BINFO (val->type) && TYPE_BINFO (type)
    1867                 :             :       && TYPE_MAIN_VARIANT (type) == type
    1868                 :             :       && TYPE_MAIN_VARIANT (val->type) == val->type
    1869                 :             :       && BINFO_VTABLE (TYPE_BINFO (val->type))
    1870                 :             :       && BINFO_VTABLE (TYPE_BINFO (type)))
    1871                 :             :     {
    1872                 :             :       tree master_binfo = TYPE_BINFO (val->type);
    1873                 :             :       tree v1 = BINFO_VTABLE (master_binfo);
    1874                 :             :       tree v2 = BINFO_VTABLE (TYPE_BINFO (type));
    1875                 :             : 
    1876                 :             :       if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
    1877                 :             :         {
    1878                 :             :           gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR
    1879                 :             :                       && operand_equal_p (TREE_OPERAND (v1, 1),
    1880                 :             :                                           TREE_OPERAND (v2, 1), 0));
    1881                 :             :           v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
    1882                 :             :           v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
    1883                 :             :         }
    1884                 :             :       gcc_assert (DECL_ASSEMBLER_NAME (v1)
    1885                 :             :                   == DECL_ASSEMBLER_NAME (v2));
    1886                 :             : 
    1887                 :             :       if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2))
    1888                 :             :         {
    1889                 :             :           unsigned int i;
    1890                 :             : 
    1891                 :             :           set_type_binfo (val->type, TYPE_BINFO (type));
    1892                 :             :           for (i = 0; i < val->types->length (); i++)
    1893                 :             :             {
    1894                 :             :               if (TYPE_BINFO ((*val->types)[i])
    1895                 :             :                   == master_binfo)
    1896                 :             :                 set_type_binfo ((*val->types)[i], TYPE_BINFO (type));
    1897                 :             :             }
    1898                 :             :           BINFO_TYPE (TYPE_BINFO (type)) = val->type;
    1899                 :             :         }
    1900                 :             :       else
    1901                 :             :         set_type_binfo (type, master_binfo);
    1902                 :             :     }
    1903                 :        6956 :   return build_bases;
    1904                 :        6956 : }
    1905                 :             : 
    1906                 :             : /* REF is OBJ_TYPE_REF, return the class the ref corresponds to.
    1907                 :             :    FOR_DUMP_P is true when being called from the dump routines.  */
    1908                 :             : 
    1909                 :             : tree
    1910                 :     3136150 : obj_type_ref_class (const_tree ref, bool for_dump_p)
    1911                 :             : {
    1912                 :     3136150 :   gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
    1913                 :     3136150 :   ref = TREE_TYPE (ref);
    1914                 :     3136150 :   gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
    1915                 :     3136150 :   ref = TREE_TYPE (ref);
    1916                 :             :   /* We look for type THIS points to.  ObjC also builds
    1917                 :             :      OBJ_TYPE_REF with non-method calls, Their first parameter
    1918                 :             :      ID however also corresponds to class type. */
    1919                 :     3136150 :   gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
    1920                 :             :                        || TREE_CODE (ref) == FUNCTION_TYPE);
    1921                 :     3136150 :   ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
    1922                 :     3136150 :   gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
    1923                 :     3136150 :   tree ret = TREE_TYPE (ref);
    1924                 :     3136150 :   if (!in_lto_p && !TYPE_STRUCTURAL_EQUALITY_P (ret))
    1925                 :     3133904 :     ret = TYPE_CANONICAL (ret);
    1926                 :        2246 :   else if (odr_type ot = get_odr_type (ret, !for_dump_p))
    1927                 :        2246 :     ret = ot->type;
    1928                 :             :   else
    1929                 :           0 :     gcc_assert (for_dump_p);
    1930                 :     3136150 :   return ret;
    1931                 :             : }
    1932                 :             : 
    1933                 :             : /* Get ODR type hash entry for TYPE.  If INSERT is true, create
    1934                 :             :    possibly new entry.  */
    1935                 :             : 
    1936                 :             : odr_type
    1937                 :     5773547 : get_odr_type (tree type, bool insert)
    1938                 :             : {
    1939                 :     5773547 :   odr_type_d **slot = NULL;
    1940                 :     5773547 :   odr_type val = NULL;
    1941                 :     5773547 :   hashval_t hash;
    1942                 :     5773547 :   bool build_bases = false;
    1943                 :     5773547 :   bool insert_to_odr_array = false;
    1944                 :     5773547 :   int base_id = -1;
    1945                 :             : 
    1946                 :     5773547 :   type = TYPE_MAIN_VARIANT (type);
    1947                 :     5773547 :   if (!in_lto_p && !TYPE_STRUCTURAL_EQUALITY_P (type))
    1948                 :     5682013 :     type = TYPE_CANONICAL (type);
    1949                 :             : 
    1950                 :     5865081 :   gcc_checking_assert (can_be_name_hashed_p (type));
    1951                 :             : 
    1952                 :     5773547 :   hash = hash_odr_name (type);
    1953                 :     5829501 :   slot = odr_hash->find_slot_with_hash (type, hash,
    1954                 :             :                                         insert ? INSERT : NO_INSERT);
    1955                 :             : 
    1956                 :     5773547 :   if (!slot)
    1957                 :             :     return NULL;
    1958                 :             : 
    1959                 :             :   /* See if we already have entry for type.  */
    1960                 :     5764960 :   if (*slot)
    1961                 :             :     {
    1962                 :     4485870 :       val = *slot;
    1963                 :             : 
    1964                 :       10954 :       if (val->type != type && insert
    1965                 :     4495133 :           && (!val->types_set || !val->types_set->add (type)))
    1966                 :        6956 :         build_bases = add_type_duplicate (val, type);
    1967                 :             :     }
    1968                 :             :   else
    1969                 :             :     {
    1970                 :     1279090 :       val = ggc_cleared_alloc<odr_type_d> ();
    1971                 :     1279090 :       val->type = type;
    1972                 :     1279090 :       val->bases = vNULL;
    1973                 :     1279090 :       val->derived_types = vNULL;
    1974                 :     1279090 :       if (type_with_linkage_p (type))
    1975                 :     1279090 :         val->anonymous_namespace = type_in_anonymous_namespace_p (type);
    1976                 :             :       else
    1977                 :           0 :         val->anonymous_namespace = 0;
    1978                 :     1279090 :       build_bases = COMPLETE_TYPE_P (val->type);
    1979                 :     1279090 :       insert_to_odr_array = true;
    1980                 :     1279090 :       *slot = val;
    1981                 :             :     }
    1982                 :             : 
    1983                 :     1276632 :   if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
    1984                 :     1267737 :       && type_with_linkage_p (type)
    1985                 :     2553783 :       && type == TYPE_MAIN_VARIANT (type))
    1986                 :             :     {
    1987                 :     1267737 :       tree binfo = TYPE_BINFO (type);
    1988                 :     1267737 :       unsigned int i;
    1989                 :             : 
    1990                 :     1267737 :       gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) == type);
    1991                 :             : 
    1992                 :     1267737 :       val->all_derivations_known = type_all_derivations_known_p (type);
    1993                 :     2600648 :       for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
    1994                 :             :         /* For now record only polymorphic types. other are
    1995                 :             :            pointless for devirtualization and we cannot precisely
    1996                 :             :            determine ODR equivalency of these during LTO.  */
    1997                 :     1332911 :         if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
    1998                 :             :           {
    1999                 :     1146076 :             tree base_type= BINFO_TYPE (BINFO_BASE_BINFO (binfo, i));
    2000                 :     1146076 :             odr_type base = get_odr_type (base_type, true);
    2001                 :     1146076 :             gcc_assert (TYPE_MAIN_VARIANT (base_type) == base_type);
    2002                 :     1146076 :             base->derived_types.safe_push (val);
    2003                 :     1146076 :             val->bases.safe_push (base);
    2004                 :     1146076 :             if (base->id > base_id)
    2005                 :             :               base_id = base->id;
    2006                 :             :           }
    2007                 :             :       }
    2008                 :             :   /* Ensure that type always appears after bases.  */
    2009                 :     5764960 :   if (insert_to_odr_array)
    2010                 :             :     {
    2011                 :     1279090 :       if (odr_types_ptr)
    2012                 :     1250110 :         val->id = odr_types.length ();
    2013                 :     1279090 :       vec_safe_push (odr_types_ptr, val);
    2014                 :             :     }
    2015                 :     4485870 :   else if (base_id > val->id)
    2016                 :             :     {
    2017                 :          79 :       odr_types[val->id] = 0;
    2018                 :             :       /* Be sure we did not recorded any derived types; these may need
    2019                 :             :          renumbering too.  */
    2020                 :          79 :       gcc_assert (val->derived_types.length() == 0);
    2021                 :          79 :       val->id = odr_types.length ();
    2022                 :          79 :       vec_safe_push (odr_types_ptr, val);
    2023                 :             :     }
    2024                 :     5764960 :   return val;
    2025                 :             : }
    2026                 :             : 
    2027                 :             : /* Return type that in ODR type hash prevailed TYPE.  Be careful and punt
    2028                 :             :    on ODR violations.  */
    2029                 :             : 
    2030                 :             : tree
    2031                 :       10070 : prevailing_odr_type (tree type)
    2032                 :             : {
    2033                 :       10070 :   odr_type t = get_odr_type (type, false);
    2034                 :       10070 :   if (!t || t->odr_violated)
    2035                 :             :     return type;
    2036                 :       10065 :   return t->type;
    2037                 :             : }
    2038                 :             : 
    2039                 :             : /* Set tbaa_enabled flag for TYPE.  */
    2040                 :             : 
    2041                 :             : void
    2042                 :        9802 : enable_odr_based_tbaa (tree type)
    2043                 :             : {
    2044                 :        9802 :   odr_type t = get_odr_type (type, true);
    2045                 :        9802 :   t->tbaa_enabled = true;
    2046                 :        9802 : }
    2047                 :             : 
    2048                 :             : /* True if canonical type of TYPE is determined using ODR name.  */
    2049                 :             : 
    2050                 :             : bool
    2051                 :        7021 : odr_based_tbaa_p (const_tree type)
    2052                 :             : {
    2053                 :        7021 :   if (!RECORD_OR_UNION_TYPE_P (type))
    2054                 :             :     return false;
    2055                 :        5682 :   if (!odr_hash)
    2056                 :             :     return false;
    2057                 :        5679 :   odr_type t = get_odr_type (const_cast <tree> (type), false);
    2058                 :        5679 :   if (!t || !t->tbaa_enabled)
    2059                 :             :     return false;
    2060                 :             :   return true;
    2061                 :             : }
    2062                 :             : 
    2063                 :             : /* Set TYPE_CANONICAL of type and all its variants and duplicates
    2064                 :             :    to CANONICAL.  */
    2065                 :             : 
    2066                 :             : void
    2067                 :        9819 : set_type_canonical_for_odr_type (tree type, tree canonical)
    2068                 :             : {
    2069                 :        9819 :   odr_type t = get_odr_type (type, false);
    2070                 :        9819 :   unsigned int i;
    2071                 :        9819 :   tree tt;
    2072                 :             : 
    2073                 :       24856 :   for (tree t2 = t->type; t2; t2 = TYPE_NEXT_VARIANT (t2))
    2074                 :       15037 :     TYPE_CANONICAL (t2) = canonical;
    2075                 :        9819 :   if (t->types)
    2076                 :       13495 :     FOR_EACH_VEC_ELT (*t->types, i, tt)
    2077                 :       17069 :       for (tree t2 = tt; t2; t2 = TYPE_NEXT_VARIANT (t2))
    2078                 :       10279 :         TYPE_CANONICAL (t2) = canonical;
    2079                 :        9819 : }
    2080                 :             : 
    2081                 :             : /* Return true if we reported some ODR violation on TYPE.  */
    2082                 :             : 
    2083                 :             : bool
    2084                 :        9954 : odr_type_violation_reported_p (tree type)
    2085                 :             : {
    2086                 :        9954 :   return get_odr_type (type, false)->odr_violated;
    2087                 :             : }
    2088                 :             : 
    2089                 :             : /* Add TYPE of ODR type hash.  */
    2090                 :             : 
    2091                 :             : void
    2092                 :       30431 : register_odr_type (tree type)
    2093                 :             : {
    2094                 :       30431 :   if (!odr_hash)
    2095                 :        1737 :     odr_hash = new odr_hash_type (23);
    2096                 :       30431 :   if (type == TYPE_MAIN_VARIANT (type))
    2097                 :             :     {
    2098                 :             :       /* To get ODR warnings right, first register all sub-types.  */
    2099                 :       30431 :       if (RECORD_OR_UNION_TYPE_P (type)
    2100                 :       30431 :           && COMPLETE_TYPE_P (type))
    2101                 :             :         {
    2102                 :             :           /* Limit recursion on types which are already registered.  */
    2103                 :       20325 :           odr_type ot = get_odr_type (type, false);
    2104                 :       20325 :           if (ot
    2105                 :       20325 :               && (ot->type == type
    2106                 :        1557 :                   || (ot->types_set
    2107                 :         181 :                       && ot->types_set->contains (type))))
    2108                 :       10273 :             return;
    2109                 :       29325 :           for (tree f = TYPE_FIELDS (type); f; f = TREE_CHAIN (f))
    2110                 :       19273 :             if (TREE_CODE (f) == FIELD_DECL)
    2111                 :             :               {
    2112                 :       19273 :                 tree subtype = TREE_TYPE (f);
    2113                 :             : 
    2114                 :       19765 :                 while (TREE_CODE (subtype) == ARRAY_TYPE)
    2115                 :         492 :                   subtype = TREE_TYPE (subtype);
    2116                 :       19273 :                 if (type_with_linkage_p (TYPE_MAIN_VARIANT (subtype)))
    2117                 :        9844 :                   register_odr_type (TYPE_MAIN_VARIANT (subtype));
    2118                 :             :               }
    2119                 :       10052 :            if (TYPE_BINFO (type))
    2120                 :        1015 :              for (unsigned int i = 0;
    2121                 :        2367 :                   i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
    2122                 :        1015 :                register_odr_type (BINFO_TYPE (BINFO_BASE_BINFO
    2123                 :             :                                                  (TYPE_BINFO (type), i)));
    2124                 :             :         }
    2125                 :       20158 :       get_odr_type (type, true);
    2126                 :             :     }
    2127                 :             : }
    2128                 :             : 
    2129                 :             : /* Return true if type is known to have no derivations.  */
    2130                 :             : 
    2131                 :             : bool
    2132                 :     1511993 : type_known_to_have_no_derivations_p (tree t)
    2133                 :             : {
    2134                 :     1511993 :   return (type_all_derivations_known_p (t)
    2135                 :     1511993 :           && (TYPE_FINAL_P (t)
    2136                 :        2261 :               || (odr_hash
    2137                 :        2261 :                   && !get_odr_type (t, true)->derived_types.length())));
    2138                 :             : }
    2139                 :             : 
    2140                 :             : /* Dump ODR type T and all its derived types.  INDENT specifies indentation for
    2141                 :             :    recursive printing.  */
    2142                 :             : 
    2143                 :             : static void
    2144                 :          83 : dump_odr_type (FILE *f, odr_type t, int indent=0)
    2145                 :             : {
    2146                 :          83 :   unsigned int i;
    2147                 :          83 :   fprintf (f, "%*s type %i: ", indent * 2, "", t->id);
    2148                 :          83 :   print_generic_expr (f, t->type, TDF_SLIM);
    2149                 :         166 :   fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":"");
    2150                 :         166 :   fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":"");
    2151                 :          83 :   if (TYPE_NAME (t->type))
    2152                 :             :     {
    2153                 :          83 :       if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t->type)))
    2154                 :          14 :         fprintf (f, "%*s mangled name: %s\n", indent * 2, "",
    2155                 :           7 :                  IDENTIFIER_POINTER
    2156                 :             :                    (DECL_ASSEMBLER_NAME (TYPE_NAME (t->type))));
    2157                 :             :     }
    2158                 :          83 :   if (t->bases.length ())
    2159                 :             :     {
    2160                 :          42 :       fprintf (f, "%*s base odr type ids: ", indent * 2, "");
    2161                 :         210 :       for (i = 0; i < t->bases.length (); i++)
    2162                 :          42 :         fprintf (f, " %i", t->bases[i]->id);
    2163                 :          42 :       fprintf (f, "\n");
    2164                 :             :     }
    2165                 :          83 :   if (t->derived_types.length ())
    2166                 :             :     {
    2167                 :          38 :       fprintf (f, "%*s derived types:\n", indent * 2, "");
    2168                 :         198 :       for (i = 0; i < t->derived_types.length (); i++)
    2169                 :          42 :         dump_odr_type (f, t->derived_types[i], indent + 1);
    2170                 :             :     }
    2171                 :          83 :   fprintf (f, "\n");
    2172                 :          83 : }
    2173                 :             : 
    2174                 :             : /* Dump the type inheritance graph.  */
    2175                 :             : 
    2176                 :             : static void
    2177                 :         136 : dump_type_inheritance_graph (FILE *f)
    2178                 :             : {
    2179                 :         136 :   unsigned int i;
    2180                 :         136 :   unsigned int num_all_types = 0, num_types = 0, num_duplicates = 0;
    2181                 :         136 :   if (!odr_types_ptr)
    2182                 :             :     return;
    2183                 :          38 :   fprintf (f, "\n\nType inheritance graph:\n");
    2184                 :         159 :   for (i = 0; i < odr_types.length (); i++)
    2185                 :             :     {
    2186                 :          83 :       if (odr_types[i] && odr_types[i]->bases.length () == 0)
    2187                 :          41 :         dump_odr_type (f, odr_types[i]);
    2188                 :             :     }
    2189                 :         121 :   for (i = 0; i < odr_types.length (); i++)
    2190                 :             :     {
    2191                 :          83 :       if (!odr_types[i])
    2192                 :           0 :         continue;
    2193                 :             : 
    2194                 :          83 :       num_all_types++;
    2195                 :          83 :       if (!odr_types[i]->types || !odr_types[i]->types->length ())
    2196                 :          80 :         continue;
    2197                 :             : 
    2198                 :             :       /* To aid ODR warnings we also mangle integer constants but do
    2199                 :             :          not consider duplicates there.  */
    2200                 :           3 :       if (TREE_CODE (odr_types[i]->type) == INTEGER_TYPE)
    2201                 :           0 :         continue;
    2202                 :             : 
    2203                 :             :       /* It is normal to have one duplicate and one normal variant.  */
    2204                 :           6 :       if (odr_types[i]->types->length () == 1
    2205                 :           3 :           && COMPLETE_TYPE_P (odr_types[i]->type)
    2206                 :           6 :           && !COMPLETE_TYPE_P ((*odr_types[i]->types)[0]))
    2207                 :           3 :         continue;
    2208                 :             : 
    2209                 :           0 :       num_types ++;
    2210                 :             : 
    2211                 :           0 :       unsigned int j;
    2212                 :           0 :       fprintf (f, "Duplicate tree types for odr type %i\n", i);
    2213                 :           0 :       print_node (f, "", odr_types[i]->type, 0);
    2214                 :           0 :       print_node (f, "", TYPE_NAME (odr_types[i]->type), 0);
    2215                 :           0 :       putc ('\n',f);
    2216                 :           0 :       for (j = 0; j < odr_types[i]->types->length (); j++)
    2217                 :             :         {
    2218                 :           0 :           tree t;
    2219                 :           0 :           num_duplicates ++;
    2220                 :           0 :           fprintf (f, "duplicate #%i\n", j);
    2221                 :           0 :           print_node (f, "", (*odr_types[i]->types)[j], 0);
    2222                 :           0 :           t = (*odr_types[i]->types)[j];
    2223                 :           0 :           while (TYPE_P (t) && TYPE_CONTEXT (t))
    2224                 :             :             {
    2225                 :           0 :               t = TYPE_CONTEXT (t);
    2226                 :           0 :               print_node (f, "", t, 0);
    2227                 :             :             }
    2228                 :           0 :           print_node (f, "", TYPE_NAME ((*odr_types[i]->types)[j]), 0);
    2229                 :           0 :           putc ('\n',f);
    2230                 :             :         }
    2231                 :             :     }
    2232                 :          38 :   fprintf (f, "Out of %i types there are %i types with duplicates; "
    2233                 :             :            "%i duplicates overall\n", num_all_types, num_types, num_duplicates);
    2234                 :             : }
    2235                 :             : 
    2236                 :             : /* Save some WPA->ltrans streaming by freeing stuff needed only for good
    2237                 :             :    ODR warnings.
    2238                 :             :    We make TYPE_DECLs to not point back
    2239                 :             :    to the type (which is needed to keep them in the same SCC and preserve
    2240                 :             :    location information to output warnings) and subsequently we make all
    2241                 :             :    TYPE_DECLS of same assembler name equivalent.  */
    2242                 :             : 
    2243                 :             : static void
    2244                 :     1942745 : free_odr_warning_data ()
    2245                 :             : {
    2246                 :     1942745 :   static bool odr_data_freed = false;
    2247                 :             : 
    2248                 :     1942745 :   if (odr_data_freed || !flag_wpa || !odr_types_ptr)
    2249                 :             :     return;
    2250                 :             : 
    2251                 :         636 :   odr_data_freed = true;
    2252                 :             : 
    2253                 :        5928 :   for (unsigned int i = 0; i < odr_types.length (); i++)
    2254                 :        5292 :     if (odr_types[i])
    2255                 :             :       {
    2256                 :        5261 :         tree t = odr_types[i]->type;
    2257                 :             : 
    2258                 :        5261 :         TREE_TYPE (TYPE_NAME (t)) = void_type_node;
    2259                 :             : 
    2260                 :        5261 :         if (odr_types[i]->types)
    2261                 :        5765 :           for (unsigned int j = 0; j < odr_types[i]->types->length (); j++)
    2262                 :             :             {
    2263                 :        2910 :               tree td = (*odr_types[i]->types)[j];
    2264                 :             : 
    2265                 :        2910 :               TYPE_NAME (td) = TYPE_NAME (t);
    2266                 :             :             }
    2267                 :             :       }
    2268                 :         636 :   odr_data_freed = true;
    2269                 :             : }
    2270                 :             : 
    2271                 :             : /* Initialize IPA devirt and build inheritance tree graph.  */
    2272                 :             : 
    2273                 :             : void
    2274                 :     1942745 : build_type_inheritance_graph (void)
    2275                 :             : {
    2276                 :     1942745 :   struct symtab_node *n;
    2277                 :     1942745 :   FILE *inheritance_dump_file;
    2278                 :     1942745 :   dump_flags_t flags;
    2279                 :             : 
    2280                 :     1942745 :   if (odr_hash)
    2281                 :             :     {
    2282                 :     1443386 :       free_odr_warning_data ();
    2283                 :     1443386 :       return;
    2284                 :             :     }
    2285                 :      499359 :   timevar_push (TV_IPA_INHERITANCE);
    2286                 :      499359 :   inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
    2287                 :      499359 :   odr_hash = new odr_hash_type (23);
    2288                 :             : 
    2289                 :             :   /* We reconstruct the graph starting of types of all methods seen in the
    2290                 :             :      unit.  */
    2291                 :   111891211 :   FOR_EACH_SYMBOL (n)
    2292                 :   111391852 :     if (is_a <cgraph_node *> (n)
    2293                 :    89146879 :         && DECL_VIRTUAL_P (n->decl)
    2294                 :     1283829 :         && n->real_symbol_p ())
    2295                 :     1033916 :       get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
    2296                 :             : 
    2297                 :             :     /* Look also for virtual tables of types that do not define any methods.
    2298                 :             : 
    2299                 :             :        We need it in a case where class B has virtual base of class A
    2300                 :             :        re-defining its virtual method and there is class C with no virtual
    2301                 :             :        methods with B as virtual base.
    2302                 :             : 
    2303                 :             :        Here we output B's virtual method in two variant - for non-virtual
    2304                 :             :        and virtual inheritance.  B's virtual table has non-virtual version,
    2305                 :             :        while C's has virtual.
    2306                 :             : 
    2307                 :             :        For this reason we need to know about C in order to include both
    2308                 :             :        variants of B.  More correctly, record_target_from_binfo should
    2309                 :             :        add both variants of the method when walking B, but we have no
    2310                 :             :        link in between them.
    2311                 :             : 
    2312                 :             :        We rely on fact that either the method is exported and thus we
    2313                 :             :        assume it is called externally or C is in anonymous namespace and
    2314                 :             :        thus we will see the vtable.  */
    2315                 :             : 
    2316                 :   133636825 :     else if (is_a <varpool_node *> (n)
    2317                 :    22244973 :              && DECL_VIRTUAL_P (n->decl)
    2318                 :     1139164 :              && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE
    2319                 :     1139164 :              && TYPE_BINFO (DECL_CONTEXT (n->decl))
    2320                 :     1126663 :              && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl))))
    2321                 :     1126663 :       get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n->decl)), true);
    2322                 :      499359 :   if (inheritance_dump_file)
    2323                 :             :     {
    2324                 :         102 :       dump_type_inheritance_graph (inheritance_dump_file);
    2325                 :         102 :       dump_end (TDI_inheritance, inheritance_dump_file);
    2326                 :             :     }
    2327                 :      499359 :   free_odr_warning_data ();
    2328                 :      499359 :   timevar_pop (TV_IPA_INHERITANCE);
    2329                 :             : }
    2330                 :             : 
    2331                 :             : /* Return true if N has reference from live virtual table
    2332                 :             :    (and thus can be a destination of polymorphic call). 
    2333                 :             :    Be conservatively correct when callgraph is not built or
    2334                 :             :    if the method may be referred externally.  */
    2335                 :             : 
    2336                 :             : static bool
    2337                 :      142432 : referenced_from_vtable_p (struct cgraph_node *node)
    2338                 :             : {
    2339                 :      142432 :   int i;
    2340                 :      142432 :   struct ipa_ref *ref;
    2341                 :      142432 :   bool found = false;
    2342                 :             : 
    2343                 :      142432 :   if (node->externally_visible
    2344                 :       21148 :       || DECL_EXTERNAL (node->decl)
    2345                 :      152341 :       || node->used_from_other_partition)
    2346                 :             :     return true;
    2347                 :             : 
    2348                 :             :   /* Keep this test constant time.
    2349                 :             :      It is unlikely this can happen except for the case where speculative
    2350                 :             :      devirtualization introduced many speculative edges to this node. 
    2351                 :             :      In this case the target is very likely alive anyway.  */
    2352                 :        9909 :   if (node->ref_list.referring.length () > 100)
    2353                 :             :     return true;
    2354                 :             : 
    2355                 :             :   /* We need references built.  */
    2356                 :        9909 :   if (symtab->state <= CONSTRUCTION)
    2357                 :             :     return true;
    2358                 :             : 
    2359                 :        9088 :   for (i = 0; node->iterate_referring (i, ref); i++)
    2360                 :        9072 :     if ((ref->use == IPA_REF_ALIAS
    2361                 :        2032 :          && referenced_from_vtable_p (dyn_cast<cgraph_node *> (ref->referring)))
    2362                 :        9072 :         || (ref->use == IPA_REF_ADDR
    2363                 :        8056 :             && VAR_P (ref->referring->decl)
    2364                 :        8056 :             && DECL_VIRTUAL_P (ref->referring->decl)))
    2365                 :             :       {
    2366                 :             :         found = true;
    2367                 :             :         break;
    2368                 :             :       }
    2369                 :             :   return found;
    2370                 :             : }
    2371                 :             : 
    2372                 :             : /* Return if TARGET is cxa_pure_virtual.  */
    2373                 :             : 
    2374                 :             : static bool
    2375                 :      377735 : is_cxa_pure_virtual_p (tree target)
    2376                 :             : {
    2377                 :      377587 :   return target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE
    2378                 :       59926 :          && DECL_NAME (target)
    2379                 :      437661 :          && id_equal (DECL_NAME (target),
    2380                 :      377735 :                      "__cxa_pure_virtual");
    2381                 :             : }
    2382                 :             : 
    2383                 :             : /* If TARGET has associated node, record it in the NODES array.
    2384                 :             :    CAN_REFER specify if program can refer to the target directly.
    2385                 :             :    if TARGET is unknown (NULL) or it cannot be inserted (for example because
    2386                 :             :    its body was already removed and there is no way to refer to it), clear
    2387                 :             :    COMPLETEP.  */
    2388                 :             : 
    2389                 :             : static void
    2390                 :      283957 : maybe_record_node (vec <cgraph_node *> &nodes,
    2391                 :             :                    tree target, hash_set<tree> *inserted,
    2392                 :             :                    bool can_refer,
    2393                 :             :                    bool *completep)
    2394                 :             : {
    2395                 :      283957 :   struct cgraph_node *target_node, *alias_target;
    2396                 :      283957 :   enum availability avail;
    2397                 :      283957 :   bool pure_virtual = is_cxa_pure_virtual_p (target);
    2398                 :             : 
    2399                 :             :   /* __builtin_unreachable do not need to be added into
    2400                 :             :      list of targets; the runtime effect of calling them is undefined.
    2401                 :             :      Only "real" virtual methods should be accounted.  */
    2402                 :      283957 :   if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE && !pure_virtual)
    2403                 :       54825 :     return;
    2404                 :             : 
    2405                 :      277711 :   if (!can_refer)
    2406                 :             :     {
    2407                 :             :       /* The only case when method of anonymous namespace becomes unreferable
    2408                 :             :          is when we completely optimized it out.  */
    2409                 :       45591 :       if (flag_ltrans
    2410                 :       45545 :           || !target 
    2411                 :       91034 :           || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
    2412                 :       45571 :         *completep = false;
    2413                 :       45591 :       return;
    2414                 :             :     }
    2415                 :             : 
    2416                 :      232120 :   if (!target)
    2417                 :             :     return;
    2418                 :             : 
    2419                 :      232120 :   target_node = cgraph_node::get (target);
    2420                 :             : 
    2421                 :             :   /* Prefer alias target over aliases, so we do not get confused by
    2422                 :             :      fake duplicates.  */
    2423                 :      232120 :   if (target_node)
    2424                 :             :     {
    2425                 :      232120 :       alias_target = target_node->ultimate_alias_target (&avail);
    2426                 :      232120 :       if (target_node != alias_target
    2427                 :        5596 :           && avail >= AVAIL_AVAILABLE
    2428                 :      237275 :           && target_node->get_availability ())
    2429                 :        5155 :         target_node = alias_target;
    2430                 :             :     }
    2431                 :             : 
    2432                 :             :   /* Method can only be called by polymorphic call if any
    2433                 :             :      of vtables referring to it are alive. 
    2434                 :             : 
    2435                 :             :      While this holds for non-anonymous functions, too, there are
    2436                 :             :      cases where we want to keep them in the list; for example
    2437                 :             :      inline functions with -fno-weak are static, but we still
    2438                 :             :      may devirtualize them when instance comes from other unit.
    2439                 :             :      The same holds for LTO.
    2440                 :             : 
    2441                 :             :      Currently we ignore these functions in speculative devirtualization.
    2442                 :             :      ??? Maybe it would make sense to be more aggressive for LTO even
    2443                 :             :      elsewhere.  */
    2444                 :      232120 :   if (!flag_ltrans
    2445                 :      232081 :       && !pure_virtual
    2446                 :      200393 :       && type_in_anonymous_namespace_p (DECL_CONTEXT (target))
    2447                 :      238946 :       && (!target_node
    2448                 :        6826 :           || !referenced_from_vtable_p (target_node)))
    2449                 :             :     ;
    2450                 :             :   /* See if TARGET is useful function we can deal with.  */
    2451                 :      232112 :   else if (target_node != NULL
    2452                 :      232112 :            && (TREE_PUBLIC (target)
    2453                 :        6925 :                || DECL_EXTERNAL (target)
    2454                 :        6925 :                || target_node->definition)
    2455                 :      464215 :            && target_node->real_symbol_p ())
    2456                 :             :     {
    2457                 :      232103 :       gcc_assert (!target_node->inlined_to);
    2458                 :      232103 :       gcc_assert (target_node->real_symbol_p ());
    2459                 :             :       /* When sanitizing, do not assume that __cxa_pure_virtual is not called
    2460                 :             :          by valid program.  */
    2461                 :      232103 :       if (flag_sanitize & SANITIZE_UNREACHABLE)
    2462                 :             :         ;
    2463                 :             :       /* Only add pure virtual if it is the only possible target.  This way
    2464                 :             :          we will preserve the diagnostics about pure virtual called in many
    2465                 :             :          cases without disabling optimization in other.  */
    2466                 :      232033 :       else if (pure_virtual)
    2467                 :             :         {
    2468                 :       31672 :           if (nodes.length ())
    2469                 :             :             return;
    2470                 :             :         }
    2471                 :             :       /* If we found a real target, take away cxa_pure_virtual.  */
    2472                 :      200361 :       else if (!pure_virtual && nodes.length () == 1
    2473                 :       70786 :                && is_cxa_pure_virtual_p (nodes[0]->decl))
    2474                 :       21986 :         nodes.pop ();
    2475                 :      229115 :       if (pure_virtual && nodes.length ())
    2476                 :             :         return;
    2477                 :      229115 :       if (!inserted->add (target))
    2478                 :             :         {
    2479                 :      199094 :           cached_polymorphic_call_targets->add (target_node);
    2480                 :      199094 :           nodes.safe_push (target_node);
    2481                 :             :         }
    2482                 :             :     }
    2483                 :           9 :   else if (!completep)
    2484                 :             :     ;
    2485                 :             :   /* We have definition of __cxa_pure_virtual that is not accessible (it is
    2486                 :             :      optimized out or partitioned to other unit) so we cannot add it.  When
    2487                 :             :      not sanitizing, there is nothing to do.
    2488                 :             :      Otherwise declare the list incomplete.  */
    2489                 :           9 :   else if (pure_virtual)
    2490                 :             :     {
    2491                 :           0 :       if (flag_sanitize & SANITIZE_UNREACHABLE)
    2492                 :           0 :         *completep = false;
    2493                 :             :     }
    2494                 :           9 :   else if (flag_ltrans
    2495                 :           9 :            || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
    2496                 :           9 :     *completep = false;
    2497                 :             : }
    2498                 :             : 
    2499                 :             : /* See if BINFO's type matches OUTER_TYPE.  If so, look up 
    2500                 :             :    BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
    2501                 :             :    method in vtable and insert method to NODES array
    2502                 :             :    or BASES_TO_CONSIDER if this array is non-NULL.
    2503                 :             :    Otherwise recurse to base BINFOs.
    2504                 :             :    This matches what get_binfo_at_offset does, but with offset
    2505                 :             :    being unknown.
    2506                 :             : 
    2507                 :             :    TYPE_BINFOS is a stack of BINFOS of types with defined
    2508                 :             :    virtual table seen on way from class type to BINFO.
    2509                 :             : 
    2510                 :             :    MATCHED_VTABLES tracks virtual tables we already did lookup
    2511                 :             :    for virtual function in. INSERTED tracks nodes we already
    2512                 :             :    inserted.
    2513                 :             : 
    2514                 :             :    ANONYMOUS is true if BINFO is part of anonymous namespace.
    2515                 :             : 
    2516                 :             :    Clear COMPLETEP when we hit unreferable target.
    2517                 :             :   */
    2518                 :             : 
    2519                 :             : static void
    2520                 :      392412 : record_target_from_binfo (vec <cgraph_node *> &nodes,
    2521                 :             :                           vec <tree> *bases_to_consider,
    2522                 :             :                           tree binfo,
    2523                 :             :                           tree otr_type,
    2524                 :             :                           vec <tree> &type_binfos,
    2525                 :             :                           HOST_WIDE_INT otr_token,
    2526                 :             :                           tree outer_type,
    2527                 :             :                           HOST_WIDE_INT offset,
    2528                 :             :                           hash_set<tree> *inserted,
    2529                 :             :                           hash_set<tree> *matched_vtables,
    2530                 :             :                           bool anonymous,
    2531                 :             :                           bool *completep)
    2532                 :             : {
    2533                 :      392412 :   tree type = BINFO_TYPE (binfo);
    2534                 :      392412 :   int i;
    2535                 :      392412 :   tree base_binfo;
    2536                 :             : 
    2537                 :             : 
    2538                 :      392412 :   if (BINFO_VTABLE (binfo))
    2539                 :      177207 :     type_binfos.safe_push (binfo);
    2540                 :      392412 :   if (types_same_for_odr (type, outer_type))
    2541                 :             :     {
    2542                 :      175966 :       int i;
    2543                 :      175966 :       tree type_binfo = NULL;
    2544                 :             : 
    2545                 :             :       /* Look up BINFO with virtual table.  For normal types it is always last
    2546                 :             :          binfo on stack.  */
    2547                 :      363478 :       for (i = type_binfos.length () - 1; i >= 0; i--)
    2548                 :      187460 :         if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo))
    2549                 :             :           {
    2550                 :             :             type_binfo = type_binfos[i];
    2551                 :             :             break;
    2552                 :             :           }
    2553                 :      175966 :       if (BINFO_VTABLE (binfo))
    2554                 :        1735 :         type_binfos.pop ();
    2555                 :             :       /* If this is duplicated BINFO for base shared by virtual inheritance,
    2556                 :             :          we may not have its associated vtable.  This is not a problem, since
    2557                 :             :          we will walk it on the other path.  */
    2558                 :      175966 :       if (!type_binfo)
    2559                 :      392412 :         return;
    2560                 :      175914 :       tree inner_binfo = get_binfo_at_offset (type_binfo,
    2561                 :             :                                               offset, otr_type);
    2562                 :      175914 :       if (!inner_binfo)
    2563                 :             :         {
    2564                 :           0 :           gcc_assert (odr_violation_reported);
    2565                 :             :           return;
    2566                 :             :         }
    2567                 :             :       /* For types in anonymous namespace first check if the respective vtable
    2568                 :             :          is alive. If not, we know the type can't be called.  */
    2569                 :      175914 :       if (!flag_ltrans && anonymous)
    2570                 :             :         {
    2571                 :        8614 :           tree vtable = BINFO_VTABLE (inner_binfo);
    2572                 :        8614 :           varpool_node *vnode;
    2573                 :             : 
    2574                 :        8614 :           if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
    2575                 :        8614 :             vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
    2576                 :        8614 :           vnode = varpool_node::get (vtable);
    2577                 :        8614 :           if (!vnode || !vnode->definition)
    2578                 :             :             return;
    2579                 :             :         }
    2580                 :      175900 :       gcc_assert (inner_binfo);
    2581                 :      175900 :       if (bases_to_consider
    2582                 :      351800 :           ? !matched_vtables->contains (BINFO_VTABLE (inner_binfo))
    2583                 :      175900 :           : !matched_vtables->add (BINFO_VTABLE (inner_binfo)))
    2584                 :             :         {
    2585                 :      168301 :           bool can_refer;
    2586                 :      168301 :           tree target = gimple_get_virt_method_for_binfo (otr_token,
    2587                 :             :                                                           inner_binfo,
    2588                 :      168301 :                                                           &can_refer);
    2589                 :      168301 :           if (!bases_to_consider)
    2590                 :      168301 :             maybe_record_node (nodes, target, inserted, can_refer, completep);
    2591                 :             :           /* Destructors are never called via construction vtables.  */
    2592                 :           0 :           else if (!target || !DECL_CXX_DESTRUCTOR_P (target))
    2593                 :           0 :             bases_to_consider->safe_push (target);
    2594                 :             :         }
    2595                 :      175900 :       return;
    2596                 :             :     }
    2597                 :             : 
    2598                 :             :   /* Walk bases.  */
    2599                 :      446290 :   for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
    2600                 :             :     /* Walking bases that have no virtual method is pointless exercise.  */
    2601                 :      229844 :     if (polymorphic_type_binfo_p (base_binfo))
    2602                 :      223120 :       record_target_from_binfo (nodes, bases_to_consider, base_binfo, otr_type,
    2603                 :             :                                 type_binfos, 
    2604                 :             :                                 otr_token, outer_type, offset, inserted,
    2605                 :             :                                 matched_vtables, anonymous, completep);
    2606                 :      216446 :   if (BINFO_VTABLE (binfo))
    2607                 :      175472 :     type_binfos.pop ();
    2608                 :             : }
    2609                 :             :      
    2610                 :             : /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
    2611                 :             :    of TYPE, insert them to NODES, recurse into derived nodes. 
    2612                 :             :    INSERTED is used to avoid duplicate insertions of methods into NODES.
    2613                 :             :    MATCHED_VTABLES are used to avoid duplicate walking vtables.
    2614                 :             :    Clear COMPLETEP if unreferable target is found.
    2615                 :             : 
    2616                 :             :    If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
    2617                 :             :    all cases where BASE_SKIPPED is true (because the base is abstract
    2618                 :             :    class).  */
    2619                 :             : 
    2620                 :             : static void
    2621                 :      169302 : possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
    2622                 :             :                                      hash_set<tree> *inserted,
    2623                 :             :                                      hash_set<tree> *matched_vtables,
    2624                 :             :                                      tree otr_type,
    2625                 :             :                                      odr_type type,
    2626                 :             :                                      HOST_WIDE_INT otr_token,
    2627                 :             :                                      tree outer_type,
    2628                 :             :                                      HOST_WIDE_INT offset,
    2629                 :             :                                      bool *completep,
    2630                 :             :                                      vec <tree> &bases_to_consider,
    2631                 :             :                                      bool consider_construction)
    2632                 :             : {
    2633                 :      169302 :   tree binfo = TYPE_BINFO (type->type);
    2634                 :      169302 :   unsigned int i;
    2635                 :      169302 :   auto_vec <tree, 8> type_binfos;
    2636                 :      169302 :   bool possibly_instantiated = type_possibly_instantiated_p (type->type);
    2637                 :             : 
    2638                 :             :   /* We may need to consider types w/o instances because of possible derived
    2639                 :             :      types using their methods either directly or via construction vtables.
    2640                 :             :      We are safe to skip them when all derivations are known, since we will
    2641                 :             :      handle them later.
    2642                 :             :      This is done by recording them to BASES_TO_CONSIDER array.  */
    2643                 :      169302 :   if (possibly_instantiated || consider_construction)
    2644                 :             :     {
    2645                 :      169292 :       record_target_from_binfo (nodes,
    2646                 :             :                                 (!possibly_instantiated
    2647                 :          14 :                                  && type_all_derivations_known_p (type->type))
    2648                 :             :                                 ? &bases_to_consider : NULL,
    2649                 :             :                                 binfo, otr_type, type_binfos, otr_token,
    2650                 :             :                                 outer_type, offset,
    2651                 :             :                                 inserted, matched_vtables,
    2652                 :      169292 :                                 type->anonymous_namespace, completep);
    2653                 :             :     }
    2654                 :      234512 :   for (i = 0; i < type->derived_types.length (); i++)
    2655                 :       48874 :     possible_polymorphic_call_targets_1 (nodes, inserted, 
    2656                 :             :                                          matched_vtables,
    2657                 :             :                                          otr_type,
    2658                 :       24437 :                                          type->derived_types[i],
    2659                 :             :                                          otr_token, outer_type, offset, completep,
    2660                 :             :                                          bases_to_consider, consider_construction);
    2661                 :      169302 : }
    2662                 :             : 
    2663                 :             : /* Cache of queries for polymorphic call targets.
    2664                 :             : 
    2665                 :             :    Enumerating all call targets may get expensive when there are many
    2666                 :             :    polymorphic calls in the program, so we memoize all the previous
    2667                 :             :    queries and avoid duplicated work.  */
    2668                 :             : 
    2669                 :     1132236 : class polymorphic_call_target_d
    2670                 :             : {
    2671                 :             : public:
    2672                 :             :   HOST_WIDE_INT otr_token;
    2673                 :             :   ipa_polymorphic_call_context context;
    2674                 :             :   odr_type type;
    2675                 :             :   vec <cgraph_node *> targets;
    2676                 :             :   tree decl_warning;
    2677                 :             :   int type_warning;
    2678                 :             :   unsigned int n_odr_types;
    2679                 :             :   bool complete;
    2680                 :             :   bool speculative;
    2681                 :             : };
    2682                 :             : 
    2683                 :             : /* Polymorphic call target cache helpers.  */
    2684                 :             : 
    2685                 :             : struct polymorphic_call_target_hasher
    2686                 :             :   : pointer_hash <polymorphic_call_target_d>
    2687                 :             : {
    2688                 :             :   static inline hashval_t hash (const polymorphic_call_target_d *);
    2689                 :             :   static inline bool equal (const polymorphic_call_target_d *,
    2690                 :             :                             const polymorphic_call_target_d *);
    2691                 :             :   static inline void remove (polymorphic_call_target_d *);
    2692                 :             : };
    2693                 :             : 
    2694                 :             : /* Return the computed hashcode for ODR_QUERY.  */
    2695                 :             : 
    2696                 :             : inline hashval_t
    2697                 :     5037933 : polymorphic_call_target_hasher::hash (const polymorphic_call_target_d *odr_query)
    2698                 :             : {
    2699                 :     5037933 :   inchash::hash hstate (odr_query->otr_token);
    2700                 :             : 
    2701                 :     5037933 :   hstate.add_hwi (odr_query->type->id);
    2702                 :     5037933 :   hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
    2703                 :     5037933 :   hstate.add_hwi (odr_query->context.offset);
    2704                 :     5037933 :   hstate.add_hwi (odr_query->n_odr_types);
    2705                 :             : 
    2706                 :     5037933 :   if (odr_query->context.speculative_outer_type)
    2707                 :             :     {
    2708                 :       24420 :       hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
    2709                 :       24420 :       hstate.add_hwi (odr_query->context.speculative_offset);
    2710                 :             :     }
    2711                 :     5037933 :   hstate.add_flag (odr_query->speculative);
    2712                 :     5037933 :   hstate.add_flag (odr_query->context.maybe_in_construction);
    2713                 :     5037933 :   hstate.add_flag (odr_query->context.maybe_derived_type);
    2714                 :     5037933 :   hstate.add_flag (odr_query->context.speculative_maybe_derived_type);
    2715                 :     5037933 :   hstate.commit_flag ();
    2716                 :     5037933 :   return hstate.end ();
    2717                 :             : }
    2718                 :             : 
    2719                 :             : /* Compare cache entries T1 and T2.  */
    2720                 :             : 
    2721                 :             : inline bool
    2722                 :     4891650 : polymorphic_call_target_hasher::equal (const polymorphic_call_target_d *t1,
    2723                 :             :                                        const polymorphic_call_target_d *t2)
    2724                 :             : {
    2725                 :     2547185 :   return (t1->type == t2->type && t1->otr_token == t2->otr_token
    2726                 :     1532358 :           && t1->speculative == t2->speculative
    2727                 :     1277128 :           && t1->context.offset == t2->context.offset
    2728                 :     1276736 :           && t1->context.speculative_offset == t2->context.speculative_offset
    2729                 :     1276726 :           && t1->context.outer_type == t2->context.outer_type
    2730                 :     1256581 :           && t1->context.speculative_outer_type == t2->context.speculative_outer_type
    2731                 :             :           && t1->context.maybe_in_construction
    2732                 :     1256081 :               == t2->context.maybe_in_construction
    2733                 :             :           && t1->context.maybe_derived_type == t2->context.maybe_derived_type
    2734                 :     1256081 :           && (t1->context.speculative_maybe_derived_type
    2735                 :             :               == t2->context.speculative_maybe_derived_type)
    2736                 :             :           /* Adding new type may affect outcome of target search.  */
    2737                 :     5914654 :           && t1->n_odr_types == t2->n_odr_types);
    2738                 :             : }
    2739                 :             : 
    2740                 :             : /* Remove entry in polymorphic call target cache hash.  */
    2741                 :             : 
    2742                 :             : inline void
    2743                 :       83559 : polymorphic_call_target_hasher::remove (polymorphic_call_target_d *v)
    2744                 :             : {
    2745                 :       83559 :   v->targets.release ();
    2746                 :       83559 :   free (v);
    2747                 :       83559 : }
    2748                 :             : 
    2749                 :             : /* Polymorphic call target query cache.  */
    2750                 :             : 
    2751                 :             : typedef hash_table<polymorphic_call_target_hasher>
    2752                 :             :    polymorphic_call_target_hash_type;
    2753                 :             : static polymorphic_call_target_hash_type *polymorphic_call_target_hash;
    2754                 :             : 
    2755                 :             : /* Destroy polymorphic call target query cache.  */
    2756                 :             : 
    2757                 :             : static void
    2758                 :      741706 : free_polymorphic_call_targets_hash ()
    2759                 :             : {
    2760                 :      741706 :   if (cached_polymorphic_call_targets)
    2761                 :             :     {
    2762                 :       10395 :       delete polymorphic_call_target_hash;
    2763                 :       10395 :       polymorphic_call_target_hash = NULL;
    2764                 :       20790 :       delete cached_polymorphic_call_targets;
    2765                 :       10395 :       cached_polymorphic_call_targets = NULL;
    2766                 :             :     }
    2767                 :      741706 : }
    2768                 :             : 
    2769                 :             : /* Force rebuilding type inheritance graph from scratch.
    2770                 :             :    This is use to make sure that we do not keep references to types
    2771                 :             :    which was not visible to free_lang_data.  */
    2772                 :             : 
    2773                 :             : void
    2774                 :      228019 : rebuild_type_inheritance_graph ()
    2775                 :             : {
    2776                 :      228019 :   if (!odr_hash)
    2777                 :             :     return;
    2778                 :      228019 :   delete odr_hash;
    2779                 :      228019 :   odr_hash = NULL;
    2780                 :      228019 :   odr_types_ptr = NULL;
    2781                 :      228019 :   free_polymorphic_call_targets_hash ();
    2782                 :             : }
    2783                 :             : 
    2784                 :             : /* When virtual function is removed, we may need to flush the cache.  */
    2785                 :             : 
    2786                 :             : static void
    2787                 :    12892131 : devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
    2788                 :             : {
    2789                 :    12892131 :   if (cached_polymorphic_call_targets
    2790                 :     2196098 :       && !thunk_expansion
    2791                 :    15087688 :       && cached_polymorphic_call_targets->contains (n))
    2792                 :        6159 :     free_polymorphic_call_targets_hash ();
    2793                 :    12892131 : }
    2794                 :             : 
    2795                 :             : /* Look up base of BINFO that has virtual table VTABLE with OFFSET.  */
    2796                 :             : 
    2797                 :             : tree
    2798                 :       20855 : subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
    2799                 :             :                                 tree vtable)
    2800                 :             : {
    2801                 :       20855 :   tree v = BINFO_VTABLE (binfo);
    2802                 :       20855 :   int i;
    2803                 :       20855 :   tree base_binfo;
    2804                 :       20855 :   unsigned HOST_WIDE_INT this_offset;
    2805                 :             : 
    2806                 :       20855 :   if (v)
    2807                 :             :     {
    2808                 :       17002 :       if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
    2809                 :           0 :         gcc_unreachable ();
    2810                 :             : 
    2811                 :       17002 :       if (offset == this_offset
    2812                 :       17002 :           && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
    2813                 :             :         return binfo;
    2814                 :             :     }
    2815                 :             : 
    2816                 :       16278 :   for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
    2817                 :       11314 :     if (polymorphic_type_binfo_p (base_binfo))
    2818                 :             :       {
    2819                 :        7625 :         base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable);
    2820                 :        7625 :         if (base_binfo)
    2821                 :        2850 :           return base_binfo;
    2822                 :             :       }
    2823                 :             :   return NULL;
    2824                 :             : }
    2825                 :             : 
    2826                 :             : /* T is known constant value of virtual table pointer.
    2827                 :             :    Store virtual table to V and its offset to OFFSET. 
    2828                 :             :    Return false if T does not look like virtual table reference.  */
    2829                 :             : 
    2830                 :             : bool
    2831                 :      326544 : vtable_pointer_value_to_vtable (const_tree t, tree *v,
    2832                 :             :                                 unsigned HOST_WIDE_INT *offset)
    2833                 :             : {
    2834                 :             :   /* We expect &MEM[(void *)&virtual_table + 16B].
    2835                 :             :      We obtain object's BINFO from the context of the virtual table. 
    2836                 :             :      This one contains pointer to virtual table represented via
    2837                 :             :      POINTER_PLUS_EXPR.  Verify that this pointer matches what
    2838                 :             :      we propagated through.
    2839                 :             : 
    2840                 :             :      In the case of virtual inheritance, the virtual tables may
    2841                 :             :      be nested, i.e. the offset may be different from 16 and we may
    2842                 :             :      need to dive into the type representation.  */
    2843                 :      326544 :   if (TREE_CODE (t) == ADDR_EXPR
    2844                 :       25505 :       && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
    2845                 :       25505 :       && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
    2846                 :       25505 :       && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
    2847                 :       25505 :       && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
    2848                 :             :           == VAR_DECL)
    2849                 :      352049 :       && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
    2850                 :             :                                          (TREE_OPERAND (t, 0), 0), 0)))
    2851                 :             :     {
    2852                 :       25505 :       *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
    2853                 :       25505 :       *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
    2854                 :       25505 :       return true;
    2855                 :             :     }
    2856                 :             : 
    2857                 :             :   /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
    2858                 :             :      We need to handle it when T comes from static variable initializer or
    2859                 :             :      BINFO. */
    2860                 :      301039 :   if (TREE_CODE (t) == POINTER_PLUS_EXPR)
    2861                 :             :     {
    2862                 :      300968 :       *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
    2863                 :      300968 :       t = TREE_OPERAND (t, 0);
    2864                 :             :     }
    2865                 :             :   else
    2866                 :          71 :     *offset = 0;
    2867                 :             : 
    2868                 :      301039 :   if (TREE_CODE (t) != ADDR_EXPR)
    2869                 :             :     return false;
    2870                 :      300968 :   *v = TREE_OPERAND (t, 0);
    2871                 :      300968 :   return true;
    2872                 :             : }
    2873                 :             : 
    2874                 :             : /* T is known constant value of virtual table pointer.  Return BINFO of the
    2875                 :             :    instance type.  */
    2876                 :             : 
    2877                 :             : tree
    2878                 :           0 : vtable_pointer_value_to_binfo (const_tree t)
    2879                 :             : {
    2880                 :           0 :   tree vtable;
    2881                 :           0 :   unsigned HOST_WIDE_INT offset;
    2882                 :             : 
    2883                 :           0 :   if (!vtable_pointer_value_to_vtable (t, &vtable, &offset))
    2884                 :             :     return NULL_TREE;
    2885                 :             : 
    2886                 :             :   /* FIXME: for stores of construction vtables we return NULL,
    2887                 :             :      because we do not have BINFO for those. Eventually we should fix
    2888                 :             :      our representation to allow this case to be handled, too.
    2889                 :             :      In the case we see store of BINFO we however may assume
    2890                 :             :      that standard folding will be able to cope with it.  */
    2891                 :           0 :   return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
    2892                 :           0 :                                          offset, vtable);
    2893                 :             : }
    2894                 :             : 
    2895                 :             : /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
    2896                 :             :    Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
    2897                 :             :    and insert them in NODES.
    2898                 :             : 
    2899                 :             :    MATCHED_VTABLES and INSERTED is used to avoid duplicated work.  */
    2900                 :             : 
    2901                 :             : static void
    2902                 :         603 : record_targets_from_bases (tree otr_type,
    2903                 :             :                            HOST_WIDE_INT otr_token,
    2904                 :             :                            tree outer_type,
    2905                 :             :                            HOST_WIDE_INT offset,
    2906                 :             :                            vec <cgraph_node *> &nodes,
    2907                 :             :                            hash_set<tree> *inserted,
    2908                 :             :                            hash_set<tree> *matched_vtables,
    2909                 :             :                            bool *completep)
    2910                 :             : {
    2911                 :        1341 :   while (true)
    2912                 :             :     {
    2913                 :        1341 :       HOST_WIDE_INT pos, size;
    2914                 :        1341 :       tree base_binfo;
    2915                 :        1341 :       tree fld;
    2916                 :             : 
    2917                 :        1341 :       if (types_same_for_odr (outer_type, otr_type))
    2918                 :             :         return;
    2919                 :             : 
    2920                 :        2204 :       for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
    2921                 :             :         {
    2922                 :        2204 :           if (TREE_CODE (fld) != FIELD_DECL)
    2923                 :        1458 :             continue;
    2924                 :             : 
    2925                 :         746 :           pos = int_bit_position (fld);
    2926                 :         746 :           size = tree_to_shwi (DECL_SIZE (fld));
    2927                 :         746 :           if (pos <= offset && (pos + size) > offset
    2928                 :             :               /* Do not get confused by zero sized bases.  */
    2929                 :        1484 :               && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
    2930                 :             :             break;
    2931                 :             :         }
    2932                 :             :       /* Within a class type we should always find corresponding fields.  */
    2933                 :         738 :       gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
    2934                 :             : 
    2935                 :             :       /* Nonbase types should have been stripped by outer_class_type.  */
    2936                 :         738 :       gcc_assert (DECL_ARTIFICIAL (fld));
    2937                 :             : 
    2938                 :         738 :       outer_type = TREE_TYPE (fld);
    2939                 :         738 :       offset -= pos;
    2940                 :             : 
    2941                 :         738 :       base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
    2942                 :             :                                         offset, otr_type);
    2943                 :         738 :       if (!base_binfo)
    2944                 :             :         {
    2945                 :           0 :           gcc_assert (odr_violation_reported);
    2946                 :             :           return;
    2947                 :             :         }
    2948                 :         738 :       gcc_assert (base_binfo);
    2949                 :         738 :       if (!matched_vtables->add (BINFO_VTABLE (base_binfo)))
    2950                 :             :         {
    2951                 :         738 :           bool can_refer;
    2952                 :         738 :           tree target = gimple_get_virt_method_for_binfo (otr_token,
    2953                 :             :                                                           base_binfo,
    2954                 :             :                                                           &can_refer);
    2955                 :        1474 :           if (!target || ! DECL_CXX_DESTRUCTOR_P (target))
    2956                 :         738 :             maybe_record_node (nodes, target, inserted, can_refer, completep);
    2957                 :         738 :           matched_vtables->add (BINFO_VTABLE (base_binfo));
    2958                 :             :         }
    2959                 :             :     }
    2960                 :             : }
    2961                 :             : 
    2962                 :             : /* When virtual table is removed, we may need to flush the cache.  */
    2963                 :             : 
    2964                 :             : static void
    2965                 :     3342052 : devirt_variable_node_removal_hook (varpool_node *n,
    2966                 :             :                                    void *d ATTRIBUTE_UNUSED)
    2967                 :             : {
    2968                 :     3342052 :   if (cached_polymorphic_call_targets
    2969                 :       12334 :       && DECL_VIRTUAL_P (n->decl)
    2970                 :     3345538 :       && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
    2971                 :          15 :     free_polymorphic_call_targets_hash ();
    2972                 :     3342052 : }
    2973                 :             : 
    2974                 :             : /* Record about how many calls would benefit from given type to be final.  */
    2975                 :             : 
    2976                 :             : struct odr_type_warn_count
    2977                 :             : {
    2978                 :             :   tree type;
    2979                 :             :   int count;
    2980                 :             :   profile_count dyn_count;
    2981                 :             : };
    2982                 :             : 
    2983                 :             : /* Record about how many calls would benefit from given method to be final.  */
    2984                 :             : 
    2985                 :             : struct decl_warn_count
    2986                 :             : {
    2987                 :             :   tree decl;
    2988                 :             :   int count;
    2989                 :             :   profile_count dyn_count;
    2990                 :             : };
    2991                 :             : 
    2992                 :             : /* Information about type and decl warnings.  */
    2993                 :             : 
    2994                 :          13 : class final_warning_record
    2995                 :             : {
    2996                 :             : public:
    2997                 :             :   /* If needed grow type_warnings vector and initialize new decl_warn_count
    2998                 :             :      to have dyn_count set to profile_count::zero ().  */
    2999                 :             :   void grow_type_warnings (unsigned newlen);
    3000                 :             : 
    3001                 :             :   profile_count dyn_count;
    3002                 :             :   auto_vec<odr_type_warn_count> type_warnings;
    3003                 :             :   hash_map<tree, decl_warn_count> decl_warnings;
    3004                 :             : };
    3005                 :             : 
    3006                 :             : void
    3007                 :          25 : final_warning_record::grow_type_warnings (unsigned newlen)
    3008                 :             : {
    3009                 :          25 :   unsigned len = type_warnings.length ();
    3010                 :          25 :   if (newlen > len)
    3011                 :             :     {
    3012                 :          13 :       type_warnings.safe_grow_cleared (newlen, true);
    3013                 :          29 :       for (unsigned i = len; i < newlen; i++)
    3014                 :          16 :         type_warnings[i].dyn_count = profile_count::zero ();
    3015                 :             :     }
    3016                 :          25 : }
    3017                 :             : 
    3018                 :             : class final_warning_record *final_warning_records;
    3019                 :             : 
    3020                 :             : /* Return vector containing possible targets of polymorphic call of type
    3021                 :             :    OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
    3022                 :             :    If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
    3023                 :             :    OTR_TYPE and include their virtual method.  This is useful for types
    3024                 :             :    possibly in construction or destruction where the virtual table may
    3025                 :             :    temporarily change to one of base types.  INCLUDE_DERIVED_TYPES make
    3026                 :             :    us to walk the inheritance graph for all derivations.
    3027                 :             : 
    3028                 :             :    If COMPLETEP is non-NULL, store true if the list is complete. 
    3029                 :             :    CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
    3030                 :             :    in the target cache.  If user needs to visit every target list
    3031                 :             :    just once, it can memoize them.
    3032                 :             : 
    3033                 :             :    If SPECULATIVE is set, the list will not contain targets that
    3034                 :             :    are not speculatively taken.
    3035                 :             : 
    3036                 :             :    Returned vector is placed into cache.  It is NOT caller's responsibility
    3037                 :             :    to free it.  The vector can be freed on cgraph_remove_node call if
    3038                 :             :    the particular node is a virtual function present in the cache.  */
    3039                 :             : 
    3040                 :             : vec <cgraph_node *>
    3041                 :     1132236 : possible_polymorphic_call_targets (tree otr_type,
    3042                 :             :                                    HOST_WIDE_INT otr_token,
    3043                 :             :                                    ipa_polymorphic_call_context context,
    3044                 :             :                                    bool *completep,
    3045                 :             :                                    void **cache_token,
    3046                 :             :                                    bool speculative)
    3047                 :             : {
    3048                 :     1132236 :   static struct cgraph_node_hook_list *node_removal_hook_holder;
    3049                 :     1132236 :   vec <cgraph_node *> nodes = vNULL;
    3050                 :     1132236 :   auto_vec <tree, 8> bases_to_consider;
    3051                 :     1132236 :   odr_type type, outer_type;
    3052                 :     1132236 :   polymorphic_call_target_d key;
    3053                 :     1132236 :   polymorphic_call_target_d **slot;
    3054                 :     1132236 :   unsigned int i;
    3055                 :     1132236 :   tree binfo, target;
    3056                 :     1132236 :   bool complete;
    3057                 :     1132236 :   bool can_refer = false;
    3058                 :     1132236 :   bool skipped = false;
    3059                 :             : 
    3060                 :     1132236 :   otr_type = TYPE_MAIN_VARIANT (otr_type);
    3061                 :             : 
    3062                 :             :   /* If ODR is not initialized or the context is invalid, return empty
    3063                 :             :      incomplete list.  */
    3064                 :     1132236 :   if (!odr_hash || context.invalid || !TYPE_BINFO (otr_type))
    3065                 :             :     {
    3066                 :         126 :       if (completep)
    3067                 :         126 :         *completep = context.invalid;
    3068                 :         126 :       if (cache_token)
    3069                 :           4 :         *cache_token = NULL;
    3070                 :         126 :       return nodes;
    3071                 :             :     }
    3072                 :             : 
    3073                 :             :   /* Do not bother to compute speculative info when user do not asks for it.  */
    3074                 :     1132110 :   if (!speculative || !context.speculative_outer_type)
    3075                 :     1082367 :     context.clear_speculation ();
    3076                 :             : 
    3077                 :     1132110 :   type = get_odr_type (otr_type, true);
    3078                 :             : 
    3079                 :             :   /* Recording type variants would waste results cache.  */
    3080                 :     1132110 :   gcc_assert (!context.outer_type
    3081                 :             :               || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
    3082                 :             : 
    3083                 :             :   /* Look up the outer class type we want to walk.
    3084                 :             :      If we fail to do so, the context is invalid.  */
    3085                 :      192935 :   if ((context.outer_type || context.speculative_outer_type)
    3086                 :     1181142 :       && !context.restrict_to_inner_class (otr_type))
    3087                 :             :     {
    3088                 :          20 :       if (completep)
    3089                 :          20 :         *completep = true;
    3090                 :          20 :       if (cache_token)
    3091                 :           0 :         *cache_token = NULL;
    3092                 :          20 :       return nodes;
    3093                 :             :     }
    3094                 :     1132090 :   gcc_assert (!context.invalid);
    3095                 :             : 
    3096                 :             :   /* Check that restrict_to_inner_class kept the main variant.  */
    3097                 :     1132090 :   gcc_assert (!context.outer_type
    3098                 :             :               || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
    3099                 :             : 
    3100                 :             :   /* We canonicalize our query, so we do not need extra hashtable entries.  */
    3101                 :             : 
    3102                 :             :   /* Without outer type, we have no use for offset.  Just do the
    3103                 :             :      basic search from inner type.  */
    3104                 :     1132090 :   if (!context.outer_type)
    3105                 :      143903 :     context.clear_outer_type (otr_type);
    3106                 :             :   /* We need to update our hierarchy if the type does not exist.  */
    3107                 :     1132090 :   outer_type = get_odr_type (context.outer_type, true);
    3108                 :             :   /* If the type is complete, there are no derivations.  */
    3109                 :     1132090 :   if (TYPE_FINAL_P (outer_type->type))
    3110                 :           5 :     context.maybe_derived_type = false;
    3111                 :             : 
    3112                 :             :   /* Initialize query cache.  */
    3113                 :     1132090 :   if (!cached_polymorphic_call_targets)
    3114                 :             :     {
    3115                 :       14224 :       cached_polymorphic_call_targets = new hash_set<cgraph_node *>;
    3116                 :       14224 :       polymorphic_call_target_hash
    3117                 :       14224 :         = new polymorphic_call_target_hash_type (23);
    3118                 :       14224 :       if (!node_removal_hook_holder)
    3119                 :             :         {
    3120                 :        8660 :           node_removal_hook_holder =
    3121                 :        4330 :             symtab->add_cgraph_removal_hook (&devirt_node_removal_hook, NULL);
    3122                 :        4330 :           symtab->add_varpool_removal_hook (&devirt_variable_node_removal_hook,
    3123                 :             :                                          NULL);
    3124                 :             :         }
    3125                 :             :     }
    3126                 :             : 
    3127                 :     1132090 :   if (in_lto_p)
    3128                 :             :     {
    3129                 :        1201 :       if (context.outer_type != otr_type)
    3130                 :         121 :         context.outer_type
    3131                 :         121 :           = get_odr_type (context.outer_type, true)->type;
    3132                 :        1201 :       if (context.speculative_outer_type)
    3133                 :          11 :         context.speculative_outer_type
    3134                 :          11 :           = get_odr_type (context.speculative_outer_type, true)->type;
    3135                 :             :     }
    3136                 :             : 
    3137                 :             :   /* Look up cached answer.  */
    3138                 :     1132090 :   key.type = type;
    3139                 :     1132090 :   key.otr_token = otr_token;
    3140                 :     1132090 :   key.speculative = speculative;
    3141                 :     1132090 :   key.context = context;
    3142                 :     1132090 :   key.n_odr_types = odr_types.length ();
    3143                 :     1132090 :   slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
    3144                 :     1132090 :   if (cache_token)
    3145                 :      217824 :    *cache_token = (void *)*slot;
    3146                 :     1132090 :   if (*slot)
    3147                 :             :     {
    3148                 :     1017370 :       if (completep)
    3149                 :      950050 :         *completep = (*slot)->complete;
    3150                 :     1017370 :       if ((*slot)->type_warning && final_warning_records)
    3151                 :             :         {
    3152                 :           8 :           final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
    3153                 :           8 :           if (!final_warning_records->type_warnings
    3154                 :           8 :                 [(*slot)->type_warning - 1].dyn_count.initialized_p ())
    3155                 :           8 :             final_warning_records->type_warnings
    3156                 :           8 :                 [(*slot)->type_warning - 1].dyn_count = profile_count::zero ();
    3157                 :           8 :           if (final_warning_records->dyn_count > 0)
    3158                 :           0 :             final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
    3159                 :           0 :               = final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
    3160                 :           0 :                 + final_warning_records->dyn_count;
    3161                 :             :         }
    3162                 :     1017370 :       if (!speculative && (*slot)->decl_warning && final_warning_records)
    3163                 :             :         {
    3164                 :           8 :           struct decl_warn_count *c =
    3165                 :           8 :              final_warning_records->decl_warnings.get ((*slot)->decl_warning);
    3166                 :           8 :           c->count++;
    3167                 :           8 :           if (final_warning_records->dyn_count > 0)
    3168                 :           0 :             c->dyn_count += final_warning_records->dyn_count;
    3169                 :             :         }
    3170                 :     1017370 :       return (*slot)->targets;
    3171                 :             :     }
    3172                 :             : 
    3173                 :      114720 :   complete = true;
    3174                 :             : 
    3175                 :             :   /* Do actual search.  */
    3176                 :      114720 :   timevar_push (TV_IPA_VIRTUAL_CALL);
    3177                 :      114720 :   *slot = XCNEW (polymorphic_call_target_d);
    3178                 :      114720 :   if (cache_token)
    3179                 :       51119 :     *cache_token = (void *)*slot;
    3180                 :      114720 :   (*slot)->type = type;
    3181                 :      114720 :   (*slot)->otr_token = otr_token;
    3182                 :      114720 :   (*slot)->context = context;
    3183                 :      114720 :   (*slot)->speculative = speculative;
    3184                 :             : 
    3185                 :      114720 :   hash_set<tree> inserted;
    3186                 :      114720 :   hash_set<tree> matched_vtables;
    3187                 :             : 
    3188                 :             :   /* First insert targets we speculatively identified as likely.  */
    3189                 :      114720 :   if (context.speculative_outer_type)
    3190                 :             :     {
    3191                 :        1745 :       odr_type speculative_outer_type;
    3192                 :        1745 :       bool speculation_complete = true;
    3193                 :        1745 :       bool check_derived_types = false;
    3194                 :             : 
    3195                 :             :       /* First insert target from type itself and check if it may have
    3196                 :             :          derived types.  */
    3197                 :        1745 :       speculative_outer_type = get_odr_type (context.speculative_outer_type, true);
    3198                 :        1745 :       if (TYPE_FINAL_P (speculative_outer_type->type))
    3199                 :          11 :         context.speculative_maybe_derived_type = false;
    3200                 :        1745 :       binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
    3201                 :             :                                    context.speculative_offset, otr_type);
    3202                 :        1745 :       if (binfo)
    3203                 :        1745 :         target = gimple_get_virt_method_for_binfo (otr_token, binfo,
    3204                 :             :                                                    &can_refer);
    3205                 :             :       else
    3206                 :             :         target = NULL;
    3207                 :             : 
    3208                 :             :       /* In the case we get complete method, we don't need 
    3209                 :             :          to walk derivations.  */
    3210                 :        3488 :       if (target && DECL_FINAL_P (target))
    3211                 :           0 :         context.speculative_maybe_derived_type = false;
    3212                 :        1745 :       if (check_derived_types
    3213                 :             :           ? type_or_derived_type_possibly_instantiated_p
    3214                 :             :                  (speculative_outer_type)
    3215                 :        1745 :           : type_possibly_instantiated_p (speculative_outer_type->type))
    3216                 :        1745 :         maybe_record_node (nodes, target, &inserted, can_refer,
    3217                 :             :                            &speculation_complete);
    3218                 :        1745 :       if (binfo)
    3219                 :        1745 :         matched_vtables.add (BINFO_VTABLE (binfo));
    3220                 :             : 
    3221                 :             : 
    3222                 :             :       /* Next walk recursively all derived types.  */
    3223                 :        1745 :       if (context.speculative_maybe_derived_type)
    3224                 :        2685 :         for (i = 0; i < speculative_outer_type->derived_types.length(); i++)
    3225                 :         712 :           possible_polymorphic_call_targets_1 (nodes, &inserted,
    3226                 :             :                                                &matched_vtables,
    3227                 :             :                                                otr_type,
    3228                 :         356 :                                                speculative_outer_type->derived_types[i],
    3229                 :             :                                                otr_token, speculative_outer_type->type,
    3230                 :             :                                                context.speculative_offset,
    3231                 :             :                                                &speculation_complete,
    3232                 :             :                                                bases_to_consider,
    3233                 :             :                                                false);
    3234                 :             :     }
    3235                 :             : 
    3236                 :      116258 :   if (!speculative || !nodes.length ())
    3237                 :             :     {
    3238                 :      113182 :       bool check_derived_types = false;
    3239                 :             :       /* First see virtual method of type itself.  */
    3240                 :      113182 :       binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
    3241                 :             :                                    context.offset, otr_type);
    3242                 :      113182 :       if (binfo)
    3243                 :      113182 :         target = gimple_get_virt_method_for_binfo (otr_token, binfo,
    3244                 :             :                                                    &can_refer);
    3245                 :             :       else
    3246                 :             :         {
    3247                 :           0 :           gcc_assert (odr_violation_reported);
    3248                 :             :           target = NULL;
    3249                 :             :         }
    3250                 :             : 
    3251                 :             :       /* Destructors are never called through construction virtual tables,
    3252                 :             :          because the type is always known.  */
    3253                 :      226237 :       if (target && DECL_CXX_DESTRUCTOR_P (target))
    3254                 :       13411 :         context.maybe_in_construction = false;
    3255                 :             : 
    3256                 :             :       /* In the case we get complete method, we don't need 
    3257                 :             :          to walk derivations.  */
    3258                 :      226237 :       if (target && DECL_FINAL_P (target))
    3259                 :             :         {
    3260                 :           3 :           check_derived_types = true;
    3261                 :           3 :           context.maybe_derived_type = false;
    3262                 :             :         }
    3263                 :             : 
    3264                 :             :       /* If OUTER_TYPE is abstract, we know we are not seeing its instance.  */
    3265                 :      113182 :       if (check_derived_types
    3266                 :           3 :           ? type_or_derived_type_possibly_instantiated_p (outer_type)
    3267                 :      113179 :           : type_possibly_instantiated_p (outer_type->type))
    3268                 :      113154 :         maybe_record_node (nodes, target, &inserted, can_refer, &complete);
    3269                 :             :       else
    3270                 :             :         skipped = true;
    3271                 :             : 
    3272                 :      113182 :       if (binfo)
    3273                 :      113182 :         matched_vtables.add (BINFO_VTABLE (binfo));
    3274                 :             : 
    3275                 :             :       /* Next walk recursively all derived types.  */
    3276                 :      113182 :       if (context.maybe_derived_type)
    3277                 :             :         {
    3278                 :      458725 :           for (i = 0; i < outer_type->derived_types.length(); i++)
    3279                 :      289018 :             possible_polymorphic_call_targets_1 (nodes, &inserted,
    3280                 :             :                                                  &matched_vtables,
    3281                 :             :                                                  otr_type,
    3282                 :      144509 :                                                  outer_type->derived_types[i],
    3283                 :             :                                                  otr_token, outer_type->type,
    3284                 :             :                                                  context.offset, &complete,
    3285                 :             :                                                  bases_to_consider,
    3286                 :      144509 :                                                  context.maybe_in_construction);
    3287                 :             : 
    3288                 :      109833 :           if (!outer_type->all_derivations_known)
    3289                 :             :             {
    3290                 :       89748 :               if (!speculative && final_warning_records
    3291                 :          12 :                   && nodes.length () == 1
    3292                 :      109681 :                   && TREE_CODE (TREE_TYPE (nodes[0]->decl)) == METHOD_TYPE)
    3293                 :             :                 {
    3294                 :          12 :                   if (complete
    3295                 :          12 :                       && warn_suggest_final_types
    3296                 :          24 :                       && !outer_type->derived_types.length ())
    3297                 :             :                     {
    3298                 :          12 :                       final_warning_records->grow_type_warnings
    3299                 :          12 :                         (outer_type->id);
    3300                 :          12 :                       final_warning_records->type_warnings[outer_type->id].count++;
    3301                 :          12 :                       if (!final_warning_records->type_warnings
    3302                 :          12 :                                 [outer_type->id].dyn_count.initialized_p ())
    3303                 :           0 :                         final_warning_records->type_warnings
    3304                 :           0 :                            [outer_type->id].dyn_count = profile_count::zero ();
    3305                 :          24 :                       final_warning_records->type_warnings[outer_type->id].dyn_count
    3306                 :          12 :                         += final_warning_records->dyn_count;
    3307                 :          24 :                       final_warning_records->type_warnings[outer_type->id].type
    3308                 :          12 :                         = outer_type->type;
    3309                 :          12 :                       (*slot)->type_warning = outer_type->id + 1;
    3310                 :             :                     }
    3311                 :          12 :                   if (complete
    3312                 :          12 :                       && warn_suggest_final_methods
    3313                 :          20 :                       && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl),
    3314                 :           8 :                                              outer_type->type))
    3315                 :             :                     {
    3316                 :           8 :                       bool existed;
    3317                 :           8 :                       struct decl_warn_count &c =
    3318                 :           8 :                          final_warning_records->decl_warnings.get_or_insert
    3319                 :           8 :                             (nodes[0]->decl, &existed);
    3320                 :             : 
    3321                 :           8 :                       if (existed)
    3322                 :             :                         {
    3323                 :           0 :                           c.count++;
    3324                 :           0 :                           c.dyn_count += final_warning_records->dyn_count;
    3325                 :             :                         }
    3326                 :             :                       else
    3327                 :             :                         {
    3328                 :           8 :                           c.count = 1;
    3329                 :           8 :                           c.dyn_count = final_warning_records->dyn_count;
    3330                 :           8 :                           c.decl = nodes[0]->decl;
    3331                 :             :                         }
    3332                 :           8 :                       (*slot)->decl_warning = nodes[0]->decl;
    3333                 :             :                     }
    3334                 :             :                 }
    3335                 :      109669 :               complete = false;
    3336                 :             :             }
    3337                 :             :         }
    3338                 :             : 
    3339                 :      113182 :       if (!speculative)
    3340                 :             :         {
    3341                 :             :           /* Destructors are never called through construction virtual tables,
    3342                 :             :              because the type is always known.  One of entries may be
    3343                 :             :              cxa_pure_virtual so look to at least two of them.  */
    3344                 :       92677 :           if (context.maybe_in_construction)
    3345                 :      141488 :             for (i =0 ; i < MIN (nodes.length (), 2); i++)
    3346                 :       48695 :               if (DECL_CXX_DESTRUCTOR_P (nodes[i]->decl))
    3347                 :        3151 :                 context.maybe_in_construction = false;
    3348                 :       92677 :           if (context.maybe_in_construction)
    3349                 :             :             {
    3350                 :       41012 :               if (type != outer_type
    3351                 :       41012 :                   && (!skipped
    3352                 :           0 :                       || (context.maybe_derived_type
    3353                 :           0 :                           && !type_all_derivations_known_p (outer_type->type))))
    3354                 :         603 :                 record_targets_from_bases (otr_type, otr_token, outer_type->type,
    3355                 :             :                                            context.offset, nodes, &inserted,
    3356                 :             :                                            &matched_vtables, &complete);
    3357                 :       41012 :               if (skipped)
    3358                 :          19 :                 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
    3359                 :       82024 :               for (i = 0; i < bases_to_consider.length(); i++)
    3360                 :           0 :                 maybe_record_node (nodes, bases_to_consider[i], &inserted, can_refer, &complete);
    3361                 :             :             }
    3362                 :             :         }
    3363                 :             :     }
    3364                 :             : 
    3365                 :      114720 :   (*slot)->targets = nodes;
    3366                 :      114720 :   (*slot)->complete = complete;
    3367                 :      114720 :   (*slot)->n_odr_types = odr_types.length ();
    3368                 :      114720 :   if (completep)
    3369                 :      106817 :     *completep = complete;
    3370                 :             : 
    3371                 :      114720 :   timevar_pop (TV_IPA_VIRTUAL_CALL);
    3372                 :      114720 :   return nodes;
    3373                 :      114720 : }
    3374                 :             : 
    3375                 :             : bool
    3376                 :           8 : add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value,
    3377                 :             :                   vec<const decl_warn_count*> *vec)
    3378                 :             : {
    3379                 :           8 :   vec->safe_push (&value);
    3380                 :           8 :   return true;
    3381                 :             : }
    3382                 :             : 
    3383                 :             : /* Dump target list TARGETS into FILE.  */
    3384                 :             : 
    3385                 :             : static void
    3386                 :         118 : dump_targets (FILE *f, vec <cgraph_node *> targets, bool verbose)
    3387                 :             : {
    3388                 :         118 :   unsigned int i;
    3389                 :             : 
    3390                 :         543 :   for (i = 0; i < targets.length (); i++)
    3391                 :             :     {
    3392                 :         156 :       char *name = NULL;
    3393                 :         156 :       if (in_lto_p)
    3394                 :           3 :         name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
    3395                 :         156 :       fprintf (f, " %s", name ? name : targets[i]->dump_name ());
    3396                 :         156 :       if (in_lto_p)
    3397                 :           3 :         free (name);
    3398                 :         156 :       if (!targets[i]->definition)
    3399                 :          53 :         fprintf (f, " (no definition%s)",
    3400                 :          53 :                  DECL_DECLARED_INLINE_P (targets[i]->decl)
    3401                 :             :                  ? " inline" : "");
    3402                 :             :       /* With many targets for every call polymorphic dumps are going to
    3403                 :             :          be quadratic in size.  */
    3404                 :         156 :       if (i > 10 && !verbose)
    3405                 :             :         {
    3406                 :           0 :           fprintf (f, " ... and %i more targets\n", targets.length () - i);
    3407                 :           0 :           return;
    3408                 :             :         }
    3409                 :             :     }
    3410                 :         118 :   fprintf (f, "\n");
    3411                 :             : }
    3412                 :             : 
    3413                 :             : /* Dump all possible targets of a polymorphic call.  */
    3414                 :             : 
    3415                 :             : void
    3416                 :          97 : dump_possible_polymorphic_call_targets (FILE *f,
    3417                 :             :                                         tree otr_type,
    3418                 :             :                                         HOST_WIDE_INT otr_token,
    3419                 :             :                                         const ipa_polymorphic_call_context &ctx,
    3420                 :             :                                         bool verbose)
    3421                 :             : {
    3422                 :          97 :   vec <cgraph_node *> targets;
    3423                 :          97 :   bool final;
    3424                 :          97 :   odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false);
    3425                 :          97 :   unsigned int len;
    3426                 :             : 
    3427                 :          97 :   if (!type)
    3428                 :           0 :     return;
    3429                 :          97 :   targets = possible_polymorphic_call_targets (otr_type, otr_token,
    3430                 :             :                                                ctx,
    3431                 :             :                                                &final, NULL, false);
    3432                 :          97 :   fprintf (f, "  Targets of polymorphic call of type %i:", type->id);
    3433                 :          97 :   print_generic_expr (f, type->type, TDF_SLIM);
    3434                 :          97 :   fprintf (f, " token %i\n", (int)otr_token);
    3435                 :             : 
    3436                 :          97 :   ctx.dump (f);
    3437                 :             : 
    3438                 :         182 :   fprintf (f, "    %s%s%s%s\n      ",
    3439                 :             :            final ? "This is a complete list." :
    3440                 :             :            "This is partial list; extra targets may be defined in other units.",
    3441                 :          97 :            ctx.maybe_in_construction ? " (base types included)" : "",
    3442                 :          97 :            ctx.maybe_derived_type ? " (derived types included)" : "",
    3443                 :          97 :            ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
    3444                 :          97 :   len = targets.length ();
    3445                 :          97 :   dump_targets (f, targets, verbose);
    3446                 :             : 
    3447                 :          97 :   targets = possible_polymorphic_call_targets (otr_type, otr_token,
    3448                 :             :                                                ctx,
    3449                 :             :                                                &final, NULL, true);
    3450                 :         189 :   if (targets.length () != len)
    3451                 :             :     {
    3452                 :          21 :       fprintf (f, "  Speculative targets:");
    3453                 :          21 :       dump_targets (f, targets, verbose);
    3454                 :             :     }
    3455                 :             :   /* Ugly: during callgraph construction the target cache may get populated
    3456                 :             :      before all targets are found.  While this is harmless (because all local
    3457                 :             :      types are discovered and only in those case we devirtualize fully and we
    3458                 :             :      don't do speculative devirtualization before IPA stage) it triggers
    3459                 :             :      assert here when dumping at that stage also populates the case with
    3460                 :             :      speculative targets.  Quietly ignore this.  */
    3461                 :         173 :   gcc_assert (symtab->state < IPA_SSA || targets.length () <= len);
    3462                 :          97 :   fprintf (f, "\n");
    3463                 :             : }
    3464                 :             : 
    3465                 :             : 
    3466                 :             : /* Return true if N can be possibly target of a polymorphic call of
    3467                 :             :    OTR_TYPE/OTR_TOKEN.  */
    3468                 :             : 
    3469                 :             : bool
    3470                 :       23080 : possible_polymorphic_call_target_p (tree otr_type,
    3471                 :             :                                     HOST_WIDE_INT otr_token,
    3472                 :             :                                     const ipa_polymorphic_call_context &ctx,
    3473                 :             :                                     struct cgraph_node *n)
    3474                 :             : {
    3475                 :       23080 :   vec <cgraph_node *> targets;
    3476                 :       23080 :   unsigned int i;
    3477                 :       23080 :   bool final;
    3478                 :             : 
    3479                 :       23080 :   if (fndecl_built_in_p (n->decl, BUILT_IN_NORMAL)
    3480                 :       23080 :       && (DECL_FUNCTION_CODE (n->decl) == BUILT_IN_UNREACHABLE
    3481                 :           0 :           || DECL_FUNCTION_CODE (n->decl) == BUILT_IN_TRAP
    3482                 :           0 :           || DECL_FUNCTION_CODE (n->decl) == BUILT_IN_UNREACHABLE_TRAP))
    3483                 :             :     return true;
    3484                 :             : 
    3485                 :       22992 :   if (is_cxa_pure_virtual_p (n->decl))
    3486                 :             :     return true;
    3487                 :             : 
    3488                 :       22986 :   if (!odr_hash)
    3489                 :             :     return true;
    3490                 :       22986 :   targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
    3491                 :       63530 :   for (i = 0; i < targets.length (); i++)
    3492                 :       31441 :     if (n->semantically_equivalent_p (targets[i]))
    3493                 :             :       return true;
    3494                 :             : 
    3495                 :             :   /* At a moment we allow middle end to dig out new external declarations
    3496                 :             :      as a targets of polymorphic calls.  */
    3497                 :         333 :   if (!final && !n->definition)
    3498                 :             :     return true;
    3499                 :             :   return false;
    3500                 :             : }
    3501                 :             : 
    3502                 :             : 
    3503                 :             : 
    3504                 :             : /* Return true if N can be possibly target of a polymorphic call of
    3505                 :             :    OBJ_TYPE_REF expression REF in STMT.  */
    3506                 :             : 
    3507                 :             : bool
    3508                 :           8 : possible_polymorphic_call_target_p (tree ref,
    3509                 :             :                                     gimple *stmt,
    3510                 :             :                                     struct cgraph_node *n)
    3511                 :             : {
    3512                 :           8 :   ipa_polymorphic_call_context context (current_function_decl, ref, stmt);
    3513                 :           8 :   tree call_fn = gimple_call_fn (stmt);
    3514                 :             : 
    3515                 :          16 :   return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn),
    3516                 :             :                                              tree_to_uhwi
    3517                 :           8 :                                                (OBJ_TYPE_REF_TOKEN (call_fn)),
    3518                 :             :                                              context,
    3519                 :           8 :                                              n);
    3520                 :             : }
    3521                 :             : 
    3522                 :             : 
    3523                 :             : /* After callgraph construction new external nodes may appear.
    3524                 :             :    Add them into the graph.  */
    3525                 :             : 
    3526                 :             : void
    3527                 :      507500 : update_type_inheritance_graph (void)
    3528                 :             : {
    3529                 :      507500 :   struct cgraph_node *n;
    3530                 :             : 
    3531                 :      507500 :   if (!odr_hash)
    3532                 :             :     return;
    3533                 :      507500 :   free_polymorphic_call_targets_hash ();
    3534                 :      507500 :   timevar_push (TV_IPA_INHERITANCE);
    3535                 :             :   /* We reconstruct the graph starting from types of all methods seen in the
    3536                 :             :      unit.  */
    3537                 :   183074222 :   FOR_EACH_FUNCTION (n)
    3538                 :    91029611 :     if (DECL_VIRTUAL_P (n->decl)
    3539                 :     1343431 :         && !n->definition
    3540                 :    91389295 :         && n->real_symbol_p ())
    3541                 :      110273 :       get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
    3542                 :      507500 :   timevar_pop (TV_IPA_INHERITANCE);
    3543                 :             : }
    3544                 :             : 
    3545                 :             : 
    3546                 :             : /* Return true if N looks like likely target of a polymorphic call.
    3547                 :             :    Rule out cxa_pure_virtual, noreturns, function declared cold and
    3548                 :             :    other obvious cases.  */
    3549                 :             : 
    3550                 :             : bool
    3551                 :      136060 : likely_target_p (struct cgraph_node *n)
    3552                 :             : {
    3553                 :      136060 :   int flags;
    3554                 :             :   /* cxa_pure_virtual and similar things are not likely.  */
    3555                 :      136060 :   if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
    3556                 :             :     return false;
    3557                 :      135081 :   flags = flags_from_decl_or_type (n->decl);
    3558                 :      135081 :   if (flags & ECF_NORETURN)
    3559                 :             :     return false;
    3560                 :      134590 :   if (lookup_attribute ("cold",
    3561                 :      134590 :                         DECL_ATTRIBUTES (n->decl)))
    3562                 :             :     return false;
    3563                 :      134590 :   if (n->frequency < NODE_FREQUENCY_NORMAL)
    3564                 :             :     return false;
    3565                 :             :   /* If there are no live virtual tables referring the target,
    3566                 :             :      the only way the target can be called is an instance coming from other
    3567                 :             :      compilation unit; speculative devirtualization is built around an
    3568                 :             :      assumption that won't happen.  */
    3569                 :      134590 :   if (!referenced_from_vtable_p (n))
    3570                 :             :     return false;
    3571                 :             :   return true;
    3572                 :             : }
    3573                 :             : 
    3574                 :             : /* Compare type warning records P1 and P2 and choose one with larger count;
    3575                 :             :    helper for qsort.  */
    3576                 :             : 
    3577                 :             : static int
    3578                 :           0 : type_warning_cmp (const void *p1, const void *p2)
    3579                 :             : {
    3580                 :           0 :   const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
    3581                 :           0 :   const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
    3582                 :             : 
    3583                 :           0 :   if (t1->dyn_count < t2->dyn_count)
    3584                 :             :    return 1;
    3585                 :           0 :   if (t1->dyn_count > t2->dyn_count)
    3586                 :             :    return -1;
    3587                 :           0 :   return t2->count - t1->count;
    3588                 :             : }
    3589                 :             : 
    3590                 :             : /* Compare decl warning records P1 and P2 and choose one with larger count;
    3591                 :             :    helper for qsort.  */
    3592                 :             : 
    3593                 :             : static int
    3594                 :          12 : decl_warning_cmp (const void *p1, const void *p2)
    3595                 :             : {
    3596                 :          12 :   const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
    3597                 :          12 :   const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
    3598                 :             : 
    3599                 :          12 :   if (t1->dyn_count < t2->dyn_count)
    3600                 :             :    return 1;
    3601                 :          12 :   if (t1->dyn_count > t2->dyn_count)
    3602                 :             :    return -1;
    3603                 :          12 :   return t2->count - t1->count;
    3604                 :             : }
    3605                 :             : 
    3606                 :             : 
    3607                 :             : /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
    3608                 :             :    context CTX.  */
    3609                 :             : 
    3610                 :             : struct cgraph_node *
    3611                 :       75203 : try_speculative_devirtualization (tree otr_type, HOST_WIDE_INT otr_token,
    3612                 :             :                                   ipa_polymorphic_call_context ctx)
    3613                 :             : {
    3614                 :       75203 :   vec <cgraph_node *>targets
    3615                 :             :      = possible_polymorphic_call_targets
    3616                 :       75203 :           (otr_type, otr_token, ctx, NULL, NULL, true);
    3617                 :       75203 :   unsigned int i;
    3618                 :       75203 :   struct cgraph_node *likely_target = NULL;
    3619                 :             : 
    3620                 :      261451 :   for (i = 0; i < targets.length (); i++)
    3621                 :      117752 :     if (likely_target_p (targets[i]))
    3622                 :             :       {
    3623                 :      117205 :         if (likely_target)
    3624                 :             :           return NULL;
    3625                 :       61570 :         likely_target = targets[i];
    3626                 :             :       }
    3627                 :       19568 :   if (!likely_target
    3628                 :        5935 :       ||!likely_target->definition
    3629                 :       23894 :       || DECL_EXTERNAL (likely_target->decl))
    3630                 :             :     return NULL;
    3631                 :             : 
    3632                 :             :   /* Don't use an implicitly-declared destructor (c++/58678).  */
    3633                 :        3575 :   struct cgraph_node *non_thunk_target
    3634                 :        3575 :     = likely_target->function_symbol ();
    3635                 :        3575 :   if (DECL_ARTIFICIAL (non_thunk_target->decl))
    3636                 :             :     return NULL;
    3637                 :         775 :   if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
    3638                 :         775 :       && likely_target->can_be_discarded_p ())
    3639                 :             :     return NULL;
    3640                 :             :   return likely_target;
    3641                 :             : }
    3642                 :             : 
    3643                 :             : /* The ipa-devirt pass.
    3644                 :             :    When polymorphic call has only one likely target in the unit,
    3645                 :             :    turn it into a speculative call.  */
    3646                 :             : 
    3647                 :             : static unsigned int
    3648                 :      122599 : ipa_devirt (void)
    3649                 :             : {
    3650                 :      122599 :   struct cgraph_node *n;
    3651                 :      122599 :   hash_set<void *> bad_call_targets;
    3652                 :      122599 :   struct cgraph_edge *e;
    3653                 :             : 
    3654                 :      122599 :   int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
    3655                 :      122599 :   int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
    3656                 :      122599 :   int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
    3657                 :      122599 :   int ndropped = 0;
    3658                 :             : 
    3659                 :      122599 :   if (!odr_types_ptr)
    3660                 :             :     return 0;
    3661                 :             : 
    3662                 :        6813 :   if (dump_file)
    3663                 :          34 :     dump_type_inheritance_graph (dump_file);
    3664                 :             : 
    3665                 :             :   /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
    3666                 :             :      This is implemented by setting up final_warning_records that are updated
    3667                 :             :      by get_polymorphic_call_targets.
    3668                 :             :      We need to clear cache in this case to trigger recomputation of all
    3669                 :             :      entries.  */
    3670                 :        6813 :   if (warn_suggest_final_methods || warn_suggest_final_types)
    3671                 :             :     {
    3672                 :          13 :       final_warning_records = new (final_warning_record);
    3673                 :          13 :       final_warning_records->dyn_count = profile_count::zero ();
    3674                 :          13 :       final_warning_records->grow_type_warnings (odr_types.length ());
    3675                 :          13 :       free_polymorphic_call_targets_hash ();
    3676                 :             :     }
    3677                 :             : 
    3678                 :      336940 :   FOR_EACH_DEFINED_FUNCTION (n)
    3679                 :             :     {   
    3680                 :      330127 :       bool update = false;
    3681                 :      330127 :       if (!opt_for_fn (n->decl, flag_devirtualize))
    3682                 :        6261 :         continue;
    3683                 :      323866 :       if (dump_file && n->indirect_calls)
    3684                 :          38 :         fprintf (dump_file, "\n\nProcesing function %s\n",
    3685                 :             :                  n->dump_name ());
    3686                 :      351529 :       for (e = n->indirect_calls; e; e = e->next_callee)
    3687                 :       27663 :         if (e->indirect_info->polymorphic)
    3688                 :             :           {
    3689                 :       24656 :             struct cgraph_node *likely_target = NULL;
    3690                 :       24656 :             void *cache_token;
    3691                 :       24656 :             bool final;
    3692                 :             : 
    3693                 :       24656 :             if (final_warning_records)
    3694                 :          20 :               final_warning_records->dyn_count = e->count.ipa ();
    3695                 :             : 
    3696                 :       24656 :             vec <cgraph_node *>targets
    3697                 :             :                = possible_polymorphic_call_targets
    3698                 :       24656 :                     (e, &final, &cache_token, true);
    3699                 :       24656 :             unsigned int i;
    3700                 :             : 
    3701                 :             :             /* Trigger warnings by calculating non-speculative targets.  */
    3702                 :       24656 :             if (warn_suggest_final_methods || warn_suggest_final_types)
    3703                 :          20 :               possible_polymorphic_call_targets (e);
    3704                 :             : 
    3705                 :       24656 :             if (dump_file)
    3706                 :          54 :               dump_possible_polymorphic_call_targets 
    3707                 :          54 :                 (dump_file, e, (dump_flags & TDF_DETAILS));
    3708                 :             : 
    3709                 :       24656 :             npolymorphic++;
    3710                 :             : 
    3711                 :             :             /* See if the call can be devirtualized by means of ipa-prop's
    3712                 :             :                polymorphic call context propagation.  If not, we can just
    3713                 :             :                forget about this call being polymorphic and avoid some heavy
    3714                 :             :                lifting in remove_unreachable_nodes that will otherwise try to
    3715                 :             :                keep all possible targets alive until inlining and in the inliner
    3716                 :             :                itself.
    3717                 :             : 
    3718                 :             :                This may need to be revisited once we add further ways to use
    3719                 :             :                the may edges, but it is a reasonable thing to do right now.  */
    3720                 :             : 
    3721                 :       24656 :             if ((e->indirect_info->param_index == -1
    3722                 :        9793 :                 || (!opt_for_fn (n->decl, flag_devirtualize_speculatively)
    3723                 :           9 :                     && e->indirect_info->vptr_changed))
    3724                 :       24656 :                 && !flag_ltrans_devirtualize)
    3725                 :             :               {
    3726                 :       14863 :                 e->indirect_info->polymorphic = false;
    3727                 :       14863 :                 ndropped++;
    3728                 :       14863 :                 if (dump_file)
    3729                 :          24 :                   fprintf (dump_file, "Dropping polymorphic call info;"
    3730                 :             :                            " it cannot be used by ipa-prop\n");
    3731                 :             :               }
    3732                 :             : 
    3733                 :       24656 :             if (!opt_for_fn (n->decl, flag_devirtualize_speculatively))
    3734                 :       19417 :               continue;
    3735                 :             : 
    3736                 :       24647 :             if (!e->maybe_hot_p ())
    3737                 :             :               {
    3738                 :         822 :                 if (dump_file)
    3739                 :           4 :                   fprintf (dump_file, "Call is cold\n\n");
    3740                 :         822 :                 ncold++;
    3741                 :         822 :                 continue;
    3742                 :             :               }
    3743                 :       23825 :             if (e->speculative)
    3744                 :             :               {
    3745                 :           0 :                 if (dump_file)
    3746                 :           0 :                   fprintf (dump_file, "Call is already speculated\n\n");
    3747                 :           0 :                 nspeculated++;
    3748                 :             : 
    3749                 :             :                 /* When dumping see if we agree with speculation.  */
    3750                 :           0 :                 if (!dump_file)
    3751                 :           0 :                   continue;
    3752                 :             :               }
    3753                 :       23825 :             if (bad_call_targets.contains (cache_token))
    3754                 :             :               {
    3755                 :        5763 :                 if (dump_file)
    3756                 :           0 :                   fprintf (dump_file, "Target list is known to be useless\n\n");
    3757                 :        5763 :                 nmultiple++;
    3758                 :        5763 :                 continue;
    3759                 :             :               }
    3760                 :       64319 :             for (i = 0; i < targets.length (); i++)
    3761                 :       18308 :               if (likely_target_p (targets[i]))
    3762                 :             :                 {
    3763                 :       17377 :                   if (likely_target)
    3764                 :             :                     {
    3765                 :        2863 :                       likely_target = NULL;
    3766                 :        2863 :                       if (dump_file)
    3767                 :           4 :                         fprintf (dump_file, "More than one likely target\n\n");
    3768                 :        2863 :                       nmultiple++;
    3769                 :        2863 :                       break;
    3770                 :             :                     }
    3771                 :       14514 :                   likely_target = targets[i];
    3772                 :             :                 }
    3773                 :       18062 :             if (!likely_target)
    3774                 :             :               {
    3775                 :        6411 :                 bad_call_targets.add (cache_token);
    3776                 :        6411 :                 continue;
    3777                 :             :               }
    3778                 :             :             /* This is reached only when dumping; check if we agree or disagree
    3779                 :             :                with the speculation.  */
    3780                 :       11651 :             if (e->speculative)
    3781                 :             :               {
    3782                 :           0 :                 bool found = e->speculative_call_for_target (likely_target);
    3783                 :           0 :                 if (found)
    3784                 :             :                   {
    3785                 :           0 :                     fprintf (dump_file, "We agree with speculation\n\n");
    3786                 :           0 :                     nok++;
    3787                 :             :                   }
    3788                 :             :                 else
    3789                 :             :                   {
    3790                 :           0 :                     fprintf (dump_file, "We disagree with speculation\n\n");
    3791                 :           0 :                     nwrong++;
    3792                 :             :                   }
    3793                 :           0 :                 continue;
    3794                 :           0 :               }
    3795                 :       11651 :             if (!likely_target->definition)
    3796                 :             :               {
    3797                 :        5217 :                 if (dump_file)
    3798                 :          12 :                   fprintf (dump_file, "Target is not a definition\n\n");
    3799                 :        5217 :                 nnotdefined++;
    3800                 :        5217 :                 continue;
    3801                 :             :               }
    3802                 :             :             /* Do not introduce new references to external symbols.  While we
    3803                 :             :                can handle these just well, it is common for programs to
    3804                 :             :                incorrectly with headers defining methods they are linked
    3805                 :             :                with.  */
    3806                 :        6434 :             if (DECL_EXTERNAL (likely_target->decl))
    3807                 :             :               {
    3808                 :         423 :                 if (dump_file)
    3809                 :           0 :                   fprintf (dump_file, "Target is external\n\n");
    3810                 :         423 :                 nexternal++;
    3811                 :         423 :                 continue;
    3812                 :             :               }
    3813                 :             :             /* Don't use an implicitly-declared destructor (c++/58678).  */
    3814                 :        6011 :             struct cgraph_node *non_thunk_target
    3815                 :        6011 :               = likely_target->function_symbol ();
    3816                 :        6011 :             if (DECL_ARTIFICIAL (non_thunk_target->decl))
    3817                 :             :               {
    3818                 :         560 :                 if (dump_file)
    3819                 :           4 :                   fprintf (dump_file, "Target is artificial\n\n");
    3820                 :         560 :                 nartificial++;
    3821                 :         560 :                 continue;
    3822                 :             :               }
    3823                 :        5451 :             if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
    3824                 :        5451 :                 && likely_target->can_be_discarded_p ())
    3825                 :             :               {
    3826                 :         212 :                 if (dump_file)
    3827                 :           0 :                   fprintf (dump_file, "Target is overwritable\n\n");
    3828                 :         212 :                 noverwritable++;
    3829                 :         212 :                 continue;
    3830                 :             :               }
    3831                 :        5239 :             else if (dbg_cnt (devirt))
    3832                 :             :               {
    3833                 :        5239 :                 if (dump_enabled_p ())
    3834                 :             :                   {
    3835                 :          25 :                     dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, e->call_stmt,
    3836                 :             :                                      "speculatively devirtualizing call "
    3837                 :             :                                      "in %s to %s\n",
    3838                 :             :                                      n->dump_name (),
    3839                 :             :                                      likely_target->dump_name ());
    3840                 :             :                   }
    3841                 :        5239 :                 if (!likely_target->can_be_discarded_p ())
    3842                 :             :                   {
    3843                 :         459 :                     cgraph_node *alias;
    3844                 :         459 :                     alias = dyn_cast<cgraph_node *> (likely_target->noninterposable_alias ());
    3845                 :             :                     if (alias)
    3846                 :        5239 :                       likely_target = alias;
    3847                 :             :                   }
    3848                 :        5239 :                 nconverted++;
    3849                 :        5239 :                 update = true;
    3850                 :        5239 :                 e->make_speculative
    3851                 :        5239 :                   (likely_target, e->count.apply_scale (8, 10));
    3852                 :             :               }
    3853                 :             :           }
    3854                 :      323866 :       if (update)
    3855                 :        4315 :         ipa_update_overall_fn_summary (n);
    3856                 :             :     }
    3857                 :        6813 :   if (warn_suggest_final_methods || warn_suggest_final_types)
    3858                 :             :     {
    3859                 :          13 :       if (warn_suggest_final_types)
    3860                 :             :         {
    3861                 :          12 :           final_warning_records->type_warnings.qsort (type_warning_cmp);
    3862                 :          12 :           for (unsigned int i = 0;
    3863                 :          48 :                i < final_warning_records->type_warnings.length (); i++)
    3864                 :          12 :             if (final_warning_records->type_warnings[i].count)
    3865                 :             :               {
    3866                 :           8 :                 tree type = final_warning_records->type_warnings[i].type;
    3867                 :           8 :                 int count = final_warning_records->type_warnings[i].count;
    3868                 :           8 :                 profile_count dyn_count
    3869                 :           8 :                   = final_warning_records->type_warnings[i].dyn_count;
    3870                 :             : 
    3871                 :           8 :                 if (!(dyn_count > 0))
    3872                 :           8 :                   warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
    3873                 :             :                              OPT_Wsuggest_final_types, count,
    3874                 :             :                              "Declaring type %qD final "
    3875                 :             :                              "would enable devirtualization of %i call",
    3876                 :             :                              "Declaring type %qD final "
    3877                 :             :                              "would enable devirtualization of %i calls",
    3878                 :             :                              type,
    3879                 :             :                              count);
    3880                 :             :                 else
    3881                 :           0 :                   warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
    3882                 :             :                              OPT_Wsuggest_final_types, count,
    3883                 :             :                              "Declaring type %qD final "
    3884                 :             :                              "would enable devirtualization of %i call "
    3885                 :             :                              "executed %lli times",
    3886                 :             :                              "Declaring type %qD final "
    3887                 :             :                              "would enable devirtualization of %i calls "
    3888                 :             :                              "executed %lli times",
    3889                 :             :                              type,
    3890                 :             :                              count,
    3891                 :           0 :                              (long long) dyn_count.to_gcov_type ());
    3892                 :             :               }
    3893                 :             :         }
    3894                 :             : 
    3895                 :          13 :       if (warn_suggest_final_methods)
    3896                 :             :         {
    3897                 :           5 :           auto_vec<const decl_warn_count*> decl_warnings_vec;
    3898                 :             : 
    3899                 :           5 :           final_warning_records->decl_warnings.traverse
    3900                 :           5 :             <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec);
    3901                 :           5 :           decl_warnings_vec.qsort (decl_warning_cmp);
    3902                 :          25 :           for (unsigned int i = 0; i < decl_warnings_vec.length (); i++)
    3903                 :             :             {
    3904                 :           8 :               tree decl = decl_warnings_vec[i]->decl;
    3905                 :           8 :               int count = decl_warnings_vec[i]->count;
    3906                 :           8 :               profile_count dyn_count
    3907                 :           8 :                   = decl_warnings_vec[i]->dyn_count;
    3908                 :             : 
    3909                 :           8 :               if (!(dyn_count > 0))
    3910                 :           8 :                 if (DECL_CXX_DESTRUCTOR_P (decl))
    3911                 :           0 :                   warning_n (DECL_SOURCE_LOCATION (decl),
    3912                 :             :                               OPT_Wsuggest_final_methods, count,
    3913                 :             :                               "Declaring virtual destructor of %qD final "
    3914                 :             :                               "would enable devirtualization of %i call",
    3915                 :             :                               "Declaring virtual destructor of %qD final "
    3916                 :             :                               "would enable devirtualization of %i calls",
    3917                 :           0 :                               DECL_CONTEXT (decl), count);
    3918                 :             :                 else
    3919                 :           8 :                   warning_n (DECL_SOURCE_LOCATION (decl),
    3920                 :             :                               OPT_Wsuggest_final_methods, count,
    3921                 :             :                               "Declaring method %qD final "
    3922                 :             :                               "would enable devirtualization of %i call",
    3923                 :             :                               "Declaring method %qD final "
    3924                 :             :                               "would enable devirtualization of %i calls",
    3925                 :             :                               decl, count);
    3926                 :           0 :                else if (DECL_CXX_DESTRUCTOR_P (decl))
    3927                 :           0 :                   warning_n (DECL_SOURCE_LOCATION (decl),
    3928                 :             :                               OPT_Wsuggest_final_methods, count,
    3929                 :             :                               "Declaring virtual destructor of %qD final "
    3930                 :             :                               "would enable devirtualization of %i call "
    3931                 :             :                               "executed %lli times",
    3932                 :             :                               "Declaring virtual destructor of %qD final "
    3933                 :             :                               "would enable devirtualization of %i calls "
    3934                 :             :                               "executed %lli times",
    3935                 :           0 :                               DECL_CONTEXT (decl), count,
    3936                 :           0 :                               (long long)dyn_count.to_gcov_type ());
    3937                 :             :                 else
    3938                 :           0 :                   warning_n (DECL_SOURCE_LOCATION (decl),
    3939                 :             :                               OPT_Wsuggest_final_methods, count,
    3940                 :             :                               "Declaring method %qD final "
    3941                 :             :                               "would enable devirtualization of %i call "
    3942                 :             :                               "executed %lli times",
    3943                 :             :                               "Declaring method %qD final "
    3944                 :             :                               "would enable devirtualization of %i calls "
    3945                 :             :                               "executed %lli times",
    3946                 :             :                               decl, count,
    3947                 :           0 :                               (long long)dyn_count.to_gcov_type ());
    3948                 :             :             }
    3949                 :           5 :         }
    3950                 :             : 
    3951                 :          26 :       delete (final_warning_records);
    3952                 :          13 :       final_warning_records = 0;
    3953                 :             :     }
    3954                 :             : 
    3955                 :        6813 :   if (dump_file)
    3956                 :          34 :     fprintf (dump_file,
    3957                 :             :              "%i polymorphic calls, %i devirtualized,"
    3958                 :             :              " %i speculatively devirtualized, %i cold\n"
    3959                 :             :              "%i have multiple targets, %i overwritable,"
    3960                 :             :              " %i already speculated (%i agree, %i disagree),"
    3961                 :             :              " %i external, %i not defined, %i artificial, %i infos dropped\n",
    3962                 :             :              npolymorphic, ndevirtualized, nconverted, ncold,
    3963                 :             :              nmultiple, noverwritable, nspeculated, nok, nwrong,
    3964                 :             :              nexternal, nnotdefined, nartificial, ndropped);
    3965                 :        6813 :   return ndevirtualized || ndropped ? TODO_remove_functions : 0;
    3966                 :      122599 : }
    3967                 :             : 
    3968                 :             : namespace {
    3969                 :             : 
    3970                 :             : const pass_data pass_data_ipa_devirt =
    3971                 :             : {
    3972                 :             :   IPA_PASS, /* type */
    3973                 :             :   "devirt", /* name */
    3974                 :             :   OPTGROUP_NONE, /* optinfo_flags */
    3975                 :             :   TV_IPA_DEVIRT, /* tv_id */
    3976                 :             :   0, /* properties_required */
    3977                 :             :   0, /* properties_provided */
    3978                 :             :   0, /* properties_destroyed */
    3979                 :             :   0, /* todo_flags_start */
    3980                 :             :   ( TODO_dump_symtab ), /* todo_flags_finish */
    3981                 :             : };
    3982                 :             : 
    3983                 :             : class pass_ipa_devirt : public ipa_opt_pass_d
    3984                 :             : {
    3985                 :             : public:
    3986                 :      281914 :   pass_ipa_devirt (gcc::context *ctxt)
    3987                 :             :     : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
    3988                 :             :                       NULL, /* generate_summary */
    3989                 :             :                       NULL, /* write_summary */
    3990                 :             :                       NULL, /* read_summary */
    3991                 :             :                       NULL, /* write_optimization_summary */
    3992                 :             :                       NULL, /* read_optimization_summary */
    3993                 :             :                       NULL, /* stmt_fixup */
    3994                 :             :                       0, /* function_transform_todo_flags_start */
    3995                 :             :                       NULL, /* function_transform */
    3996                 :      281914 :                       NULL) /* variable_transform */
    3997                 :      281914 :   {}
    3998                 :             : 
    3999                 :             :   /* opt_pass methods: */
    4000                 :      557041 :   bool gate (function *) final override
    4001                 :             :     {
    4002                 :             :       /* In LTO, always run the IPA passes and decide on function basis if the
    4003                 :             :          pass is enabled.  */
    4004                 :      557041 :       if (in_lto_p)
    4005                 :             :         return true;
    4006                 :      443535 :       return (flag_devirtualize
    4007                 :      229851 :               && (flag_devirtualize_speculatively
    4008                 :         238 :                   || (warn_suggest_final_methods
    4009                 :         238 :                       || warn_suggest_final_types))
    4010                 :      673148 :               && optimize);
    4011                 :             :     }
    4012                 :             : 
    4013                 :      122599 :   unsigned int execute (function *) final override { return ipa_devirt (); }
    4014                 :             : 
    4015                 :             : }; // class pass_ipa_devirt
    4016                 :             : 
    4017                 :             : } // anon namespace
    4018                 :             : 
    4019                 :             : ipa_opt_pass_d *
    4020                 :      281914 : make_pass_ipa_devirt (gcc::context *ctxt)
    4021                 :             : {
    4022                 :      281914 :   return new pass_ipa_devirt (ctxt);
    4023                 :             : }
    4024                 :             : 
    4025                 :             : /* Print ODR name of a TYPE if available.
    4026                 :             :    Use demangler when option DEMANGLE is used.  */
    4027                 :             : 
    4028                 :             : DEBUG_FUNCTION void
    4029                 :           0 : debug_tree_odr_name (tree type, bool demangle)
    4030                 :             : {
    4031                 :           0 :   const char *odr = get_odr_name_for_type (type);
    4032                 :           0 :   if (demangle)
    4033                 :             :     {
    4034                 :           0 :       const int opts = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
    4035                 :           0 :       odr = cplus_demangle (odr, opts);
    4036                 :             :     }
    4037                 :             : 
    4038                 :           0 :   fprintf (stderr, "%s\n", odr);
    4039                 :           0 : }
    4040                 :             : 
    4041                 :             : /* Register ODR enum so we later stream record about its values.  */
    4042                 :             : 
    4043                 :             : void
    4044                 :         332 : register_odr_enum (tree t)
    4045                 :             : {
    4046                 :         332 :   if (flag_lto)
    4047                 :         332 :     vec_safe_push (odr_enums, t);
    4048                 :         332 : }
    4049                 :             : 
    4050                 :             : /* Write ODR enums to LTO stream file.  */
    4051                 :             : 
    4052                 :             : static void
    4053                 :       22422 : ipa_odr_summary_write (void)
    4054                 :             : {
    4055                 :       22422 :   if (!odr_enums && !odr_enum_map)
    4056                 :       22422 :     return;
    4057                 :         168 :   struct output_block *ob = create_output_block (LTO_section_odr_types);
    4058                 :         168 :   unsigned int i;
    4059                 :         168 :   tree t;
    4060                 :             : 
    4061                 :         168 :   if (odr_enums)
    4062                 :             :     {
    4063                 :         165 :       streamer_write_uhwi (ob, odr_enums->length ());
    4064                 :             : 
    4065                 :             :       /* For every ODR enum stream out
    4066                 :             :            - its ODR name
    4067                 :             :            - number of values,
    4068                 :             :            - value names and constant their represent
    4069                 :             :            - bitpack of locations so we can do good diagnostics.  */
    4070                 :         497 :       FOR_EACH_VEC_ELT (*odr_enums, i, t)
    4071                 :             :         {
    4072                 :         664 :           streamer_write_string (ob, ob->main_stream,
    4073                 :         332 :                                  IDENTIFIER_POINTER
    4074                 :             :                                      (DECL_ASSEMBLER_NAME (TYPE_NAME (t))),
    4075                 :             :                                  true);
    4076                 :             : 
    4077                 :         332 :           int n = 0;
    4078                 :        2496 :           for (tree e = TYPE_VALUES (t); e; e = TREE_CHAIN (e))
    4079                 :        2164 :             n++;
    4080                 :         332 :           streamer_write_uhwi (ob, n);
    4081                 :        2496 :           for (tree e = TYPE_VALUES (t); e; e = TREE_CHAIN (e))
    4082                 :             :             {
    4083                 :        2164 :               streamer_write_string (ob, ob->main_stream,
    4084                 :        2164 :                                      IDENTIFIER_POINTER (TREE_PURPOSE (e)),
    4085                 :             :                                      true);
    4086                 :        4328 :               streamer_write_wide_int (ob,
    4087                 :        4328 :                                        wi::to_wide (DECL_INITIAL
    4088                 :             :                                                       (TREE_VALUE (e))));
    4089                 :             :             }
    4090                 :             : 
    4091                 :         332 :           bitpack_d bp = bitpack_create (ob->main_stream);
    4092                 :         332 :           lto_output_location (ob, &bp, DECL_SOURCE_LOCATION (TYPE_NAME (t)));
    4093                 :        2496 :           for (tree e = TYPE_VALUES (t); e; e = TREE_CHAIN (e))
    4094                 :        4328 :             lto_output_location (ob, &bp,
    4095                 :        2164 :                                  DECL_SOURCE_LOCATION (TREE_VALUE (e)));
    4096                 :         332 :           streamer_write_bitpack (&bp);
    4097                 :             :         }
    4098                 :         165 :       vec_free (odr_enums);
    4099                 :         165 :       odr_enums = NULL;
    4100                 :             :     }
    4101                 :             :   /* During LTO incremental linking we already have streamed in types.  */
    4102                 :           3 :   else if (odr_enum_map)
    4103                 :             :     {
    4104                 :           3 :       gcc_checking_assert (!odr_enums);
    4105                 :           3 :       streamer_write_uhwi (ob, odr_enum_map->elements ());
    4106                 :             : 
    4107                 :           3 :       hash_map<nofree_string_hash, odr_enum>::iterator iter
    4108                 :           3 :                 = odr_enum_map->begin ();
    4109                 :           7 :       for (; iter != odr_enum_map->end (); ++iter)
    4110                 :             :         {
    4111                 :           4 :           odr_enum &this_enum = (*iter).second;
    4112                 :           4 :           streamer_write_string (ob, ob->main_stream, (*iter).first, true);
    4113                 :             : 
    4114                 :           4 :           streamer_write_uhwi (ob, this_enum.vals.length ());
    4115                 :          26 :           for (unsigned j = 0; j < this_enum.vals.length (); j++)
    4116                 :             :             {
    4117                 :          18 :               streamer_write_string (ob, ob->main_stream,
    4118                 :           9 :                                      this_enum.vals[j].name, true);
    4119                 :           9 :               streamer_write_wide_int (ob, this_enum.vals[j].val);
    4120                 :             :             }
    4121                 :             : 
    4122                 :           4 :           bitpack_d bp = bitpack_create (ob->main_stream);
    4123                 :           4 :           lto_output_location (ob, &bp, this_enum.locus);
    4124                 :          26 :           for (unsigned j = 0; j < this_enum.vals.length (); j++)
    4125                 :           9 :             lto_output_location (ob, &bp, this_enum.vals[j].locus);
    4126                 :           4 :           streamer_write_bitpack (&bp);
    4127                 :             :         }
    4128                 :             : 
    4129                 :           6 :       delete odr_enum_map;
    4130                 :           3 :       obstack_free (&odr_enum_obstack, NULL);
    4131                 :           3 :       odr_enum_map = NULL;
    4132                 :             :     }
    4133                 :             : 
    4134                 :         168 :   produce_asm (ob, NULL);
    4135                 :         168 :   destroy_output_block (ob);
    4136                 :             : }
    4137                 :             : 
    4138                 :             : /* Write ODR enums from LTO stream file and warn on mismatches.  */
    4139                 :             : 
    4140                 :             : static void
    4141                 :          95 : ipa_odr_read_section (struct lto_file_decl_data *file_data, const char *data,
    4142                 :             :                       size_t len)
    4143                 :             : {
    4144                 :          95 :   const struct lto_function_header *header
    4145                 :             :     = (const struct lto_function_header *) data;
    4146                 :          95 :   const int cfg_offset = sizeof (struct lto_function_header);
    4147                 :          95 :   const int main_offset = cfg_offset + header->cfg_size;
    4148                 :          95 :   const int string_offset = main_offset + header->main_size;
    4149                 :          95 :   class data_in *data_in;
    4150                 :             : 
    4151                 :          95 :   lto_input_block ib ((const char *) data + main_offset, header->main_size,
    4152                 :          95 :                       file_data);
    4153                 :             : 
    4154                 :          95 :   data_in
    4155                 :         190 :     = lto_data_in_create (file_data, (const char *) data + string_offset,
    4156                 :          95 :                           header->string_size, vNULL);
    4157                 :          95 :   unsigned int n = streamer_read_uhwi (&ib);
    4158                 :             : 
    4159                 :          95 :   if (!odr_enum_map)
    4160                 :             :     {
    4161                 :          67 :       gcc_obstack_init (&odr_enum_obstack);
    4162                 :          67 :       odr_enum_map = new (hash_map <nofree_string_hash, odr_enum>);
    4163                 :             :     }
    4164                 :             : 
    4165                 :         339 :   for (unsigned i = 0; i < n; i++)
    4166                 :             :     {
    4167                 :         244 :       const char *rname = streamer_read_string (data_in, &ib);
    4168                 :         244 :       unsigned int nvals = streamer_read_uhwi (&ib);
    4169                 :         244 :       char *name;
    4170                 :             :   
    4171                 :         244 :       obstack_grow (&odr_enum_obstack, rname, strlen (rname) + 1);
    4172                 :         244 :       name = XOBFINISH (&odr_enum_obstack, char *);
    4173                 :             : 
    4174                 :         244 :       bool existed_p;
    4175                 :         244 :       class odr_enum &this_enum
    4176                 :         244 :                  = odr_enum_map->get_or_insert (xstrdup (name), &existed_p);
    4177                 :             : 
    4178                 :             :       /* If this is first time we see the enum, remember its definition.  */
    4179                 :         244 :       if (!existed_p)
    4180                 :             :         {
    4181                 :         216 :           this_enum.vals.safe_grow_cleared (nvals, true);
    4182                 :         216 :           this_enum.warned = false;
    4183                 :         216 :           if (dump_file)
    4184                 :           0 :             fprintf (dump_file, "enum %s\n{\n", name);
    4185                 :        1767 :           for (unsigned j = 0; j < nvals; j++)
    4186                 :             :             {
    4187                 :        1551 :               const char *val_name = streamer_read_string (data_in, &ib);
    4188                 :        1551 :               obstack_grow (&odr_enum_obstack, val_name, strlen (val_name) + 1);
    4189                 :        1551 :               this_enum.vals[j].name = XOBFINISH (&odr_enum_obstack, char *);
    4190                 :        1551 :               this_enum.vals[j].val = streamer_read_wide_int (&ib);
    4191                 :        1551 :               if (dump_file)
    4192                 :           0 :                 fprintf (dump_file, "  %s = " HOST_WIDE_INT_PRINT_DEC ",\n",
    4193                 :           0 :                          val_name, wi::fits_shwi_p (this_enum.vals[j].val)
    4194                 :           0 :                          ? this_enum.vals[j].val.to_shwi () : -1);
    4195                 :             :             }
    4196                 :         216 :           bitpack_d bp = streamer_read_bitpack (&ib);
    4197                 :         216 :           stream_input_location (&this_enum.locus, &bp, data_in);
    4198                 :        1767 :           for (unsigned j = 0; j < nvals; j++)
    4199                 :        1551 :             stream_input_location (&this_enum.vals[j].locus, &bp, data_in);
    4200                 :         216 :           data_in->location_cache.apply_location_cache ();
    4201                 :         216 :           if (dump_file)
    4202                 :           0 :             fprintf (dump_file, "}\n");
    4203                 :             :         }
    4204                 :             :       /* If we already have definition, compare it with new one and output
    4205                 :             :          warnings if they differs.  */
    4206                 :             :       else
    4207                 :             :         {
    4208                 :          28 :           int do_warning = -1;
    4209                 :          28 :           char *warn_name = NULL;
    4210                 :          28 :           wide_int warn_value = wi::zero (1);
    4211                 :             : 
    4212                 :          28 :           if (dump_file)
    4213                 :           0 :             fprintf (dump_file, "Comparing enum %s\n", name);
    4214                 :             : 
    4215                 :             :           /* Look for differences which we will warn about later once locations
    4216                 :             :              are streamed.  */
    4217                 :         109 :           for (unsigned j = 0; j < nvals; j++)
    4218                 :             :             {
    4219                 :          81 :               const char *id = streamer_read_string (data_in, &ib);
    4220                 :          81 :               wide_int val = streamer_read_wide_int (&ib);
    4221                 :             : 
    4222                 :          81 :               if (do_warning != -1 || j >= this_enum.vals.length ())
    4223                 :          20 :                 continue;
    4224                 :          61 :               if (strcmp (id, this_enum.vals[j].name)
    4225                 :          54 :                   || (val.get_precision() !=
    4226                 :          54 :                       this_enum.vals[j].val.get_precision())
    4227                 :         109 :                   || val != this_enum.vals[j].val)
    4228                 :             :                 {
    4229                 :          13 :                   warn_name = xstrdup (id);
    4230                 :          13 :                   warn_value = val;
    4231                 :          13 :                   do_warning = j;
    4232                 :          13 :                   if (dump_file)
    4233                 :           0 :                     fprintf (dump_file, "  Different on entry %i\n", j);
    4234                 :             :                 }
    4235                 :          81 :             }
    4236                 :             : 
    4237                 :             :           /* Stream in locations, but do not apply them unless we are going
    4238                 :             :              to warn.  */
    4239                 :          28 :           bitpack_d bp = streamer_read_bitpack (&ib);
    4240                 :          28 :           location_t locus;
    4241                 :             : 
    4242                 :          28 :           stream_input_location (&locus, &bp, data_in);
    4243                 :             : 
    4244                 :             :           /* Did we find a difference?  */
    4245                 :          43 :           if (do_warning != -1 || nvals != this_enum.vals.length ())
    4246                 :             :             {
    4247                 :          13 :               data_in->location_cache.apply_location_cache ();
    4248                 :             : 
    4249                 :          13 :               const int opts = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
    4250                 :          13 :               char *dmgname = cplus_demangle (name, opts);
    4251                 :          13 :               if (this_enum.warned
    4252                 :          13 :                   || !warning_at (this_enum.locus,
    4253                 :             :                                   OPT_Wodr, "type %qs violates the "
    4254                 :             :                                   "C++ One Definition Rule",
    4255                 :             :                                   dmgname))
    4256                 :             :                 do_warning = -1;
    4257                 :             :               else
    4258                 :             :                {
    4259                 :          13 :                  this_enum.warned = true;
    4260                 :          13 :                  if (do_warning == -1)
    4261                 :           0 :                    inform (locus,
    4262                 :             :                            "an enum with different number of values is defined"
    4263                 :             :                            " in another translation unit");
    4264                 :          13 :                  else if (warn_name)
    4265                 :          13 :                    inform (locus,
    4266                 :             :                            "an enum with different value name"
    4267                 :             :                            " is defined in another translation unit");
    4268                 :             :                  else
    4269                 :           0 :                    inform (locus,
    4270                 :             :                            "an enum with different values"
    4271                 :             :                            " is defined in another translation unit");
    4272                 :             :                }
    4273                 :             :             }
    4274                 :             :           else
    4275                 :          15 :             data_in->location_cache.revert_location_cache ();
    4276                 :             : 
    4277                 :             :           /* Finally look up for location of the actual value that diverged.  */
    4278                 :         109 :           for (unsigned j = 0; j < nvals; j++)
    4279                 :             :             {
    4280                 :          81 :               location_t id_locus;
    4281                 :             : 
    4282                 :          81 :               data_in->location_cache.revert_location_cache ();
    4283                 :          81 :               stream_input_location (&id_locus, &bp, data_in);
    4284                 :             : 
    4285                 :          81 :               if ((int) j == do_warning)
    4286                 :             :                 {
    4287                 :          13 :                   data_in->location_cache.apply_location_cache ();
    4288                 :             : 
    4289                 :          13 :                   if (strcmp (warn_name, this_enum.vals[j].name))
    4290                 :           7 :                     inform (this_enum.vals[j].locus,
    4291                 :             :                             "name %qs differs from name %qs defined"
    4292                 :             :                             " in another translation unit",
    4293                 :           7 :                             this_enum.vals[j].name, warn_name);
    4294                 :           6 :                   else if (this_enum.vals[j].val.get_precision() !=
    4295                 :           6 :                            warn_value.get_precision())
    4296                 :           6 :                     inform (this_enum.vals[j].locus,
    4297                 :             :                             "name %qs is defined as %u-bit while another "
    4298                 :             :                             "translation unit defines it as %u-bit",
    4299                 :           6 :                             warn_name, this_enum.vals[j].val.get_precision(),
    4300                 :             :                             warn_value.get_precision());
    4301                 :             :                   /* FIXME: In case there is easy way to print wide_ints,
    4302                 :             :                      perhaps we could do it here instead of overflow check.  */
    4303                 :           0 :                   else if (wi::fits_shwi_p (this_enum.vals[j].val)
    4304                 :           0 :                            && wi::fits_shwi_p (warn_value))
    4305                 :           0 :                     inform (this_enum.vals[j].locus,
    4306                 :             :                             "name %qs is defined to %wd while another "
    4307                 :             :                             "translation unit defines it as %wd",
    4308                 :           0 :                             warn_name, this_enum.vals[j].val.to_shwi (),
    4309                 :             :                             warn_value.to_shwi ());
    4310                 :             :                   else
    4311                 :           0 :                     inform (this_enum.vals[j].locus,
    4312                 :             :                             "name %qs is defined to different value "
    4313                 :             :                             "in another translation unit",
    4314                 :             :                             warn_name);
    4315                 :             : 
    4316                 :          13 :                   inform (id_locus,
    4317                 :             :                           "mismatching definition");
    4318                 :             :                 }
    4319                 :             :               else
    4320                 :          68 :                 data_in->location_cache.revert_location_cache ();
    4321                 :             :             }
    4322                 :          28 :           if (warn_name)
    4323                 :          13 :             free (warn_name);
    4324                 :          28 :           obstack_free (&odr_enum_obstack, name);
    4325                 :          28 :         }
    4326                 :             :     }
    4327                 :          95 :   lto_free_section_data (file_data, LTO_section_ipa_fn_summary, NULL, data,
    4328                 :             :                          len);
    4329                 :          95 :   lto_data_in_delete (data_in);
    4330                 :          95 : }
    4331                 :             : 
    4332                 :             : /* Read all ODR type sections.  */
    4333                 :             : 
    4334                 :             : static void
    4335                 :       12050 : ipa_odr_summary_read (void)
    4336                 :             : {
    4337                 :       12050 :   struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
    4338                 :       12050 :   struct lto_file_decl_data *file_data;
    4339                 :       12050 :   unsigned int j = 0;
    4340                 :             : 
    4341                 :       37160 :   while ((file_data = file_data_vec[j++]))
    4342                 :             :     {
    4343                 :       13060 :       size_t len;
    4344                 :       13060 :       const char *data
    4345                 :       13060 :         = lto_get_summary_section_data (file_data, LTO_section_odr_types,
    4346                 :             :                                         &len);
    4347                 :       13060 :       if (data)
    4348                 :          95 :         ipa_odr_read_section (file_data, data, len);
    4349                 :             :     }
    4350                 :             :   /* Enum info is used only to produce warnings.  Only case we will need it
    4351                 :             :      again is streaming for incremental LTO.  */
    4352                 :       12050 :   if (flag_incremental_link != INCREMENTAL_LINK_LTO)
    4353                 :             :     {
    4354                 :       12082 :       delete odr_enum_map;
    4355                 :       12018 :       obstack_free (&odr_enum_obstack, NULL);
    4356                 :       12018 :       odr_enum_map = NULL;
    4357                 :             :     }
    4358                 :       12050 : }
    4359                 :             : 
    4360                 :             : namespace {
    4361                 :             : 
    4362                 :             : const pass_data pass_data_ipa_odr =
    4363                 :             : {
    4364                 :             :   IPA_PASS, /* type */
    4365                 :             :   "odr", /* name */
    4366                 :             :   OPTGROUP_NONE, /* optinfo_flags */
    4367                 :             :   TV_IPA_ODR, /* tv_id */
    4368                 :             :   0, /* properties_required */
    4369                 :             :   0, /* properties_provided */
    4370                 :             :   0, /* properties_destroyed */
    4371                 :             :   0, /* todo_flags_start */
    4372                 :             :   0, /* todo_flags_finish */
    4373                 :             : };
    4374                 :             : 
    4375                 :             : class pass_ipa_odr : public ipa_opt_pass_d
    4376                 :             : {
    4377                 :             : public:
    4378                 :      281914 :   pass_ipa_odr (gcc::context *ctxt)
    4379                 :             :     : ipa_opt_pass_d (pass_data_ipa_odr, ctxt,
    4380                 :             :                       NULL, /* generate_summary */
    4381                 :             :                       ipa_odr_summary_write, /* write_summary */
    4382                 :             :                       ipa_odr_summary_read, /* read_summary */
    4383                 :             :                       NULL, /* write_optimization_summary */
    4384                 :             :                       NULL, /* read_optimization_summary */
    4385                 :             :                       NULL, /* stmt_fixup */
    4386                 :             :                       0, /* function_transform_todo_flags_start */
    4387                 :             :                       NULL, /* function_transform */
    4388                 :      281914 :                       NULL) /* variable_transform */
    4389                 :      281914 :   {}
    4390                 :             : 
    4391                 :             :   /* opt_pass methods: */
    4392                 :      579463 :   bool gate (function *) final override
    4393                 :             :     {
    4394                 :      579463 :       return (in_lto_p || flag_lto);
    4395                 :             :     }
    4396                 :             : 
    4397                 :       22187 :   unsigned int execute (function *) final override
    4398                 :             :     {
    4399                 :       22187 :       return 0;
    4400                 :             :     }
    4401                 :             : 
    4402                 :             : }; // class pass_ipa_odr
    4403                 :             : 
    4404                 :             : } // anon namespace
    4405                 :             : 
    4406                 :             : ipa_opt_pass_d *
    4407                 :      281914 : make_pass_ipa_odr (gcc::context *ctxt)
    4408                 :             : {
    4409                 :      281914 :   return new pass_ipa_odr (ctxt);
    4410                 :             : }
    4411                 :             : 
    4412                 :             : 
    4413                 :             : #include "gt-ipa-devirt.h"
        

Generated by: LCOV version 2.1-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.