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