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