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