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