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