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