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