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