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