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