]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa-devirt.c
re PR tree-optimization/91114 (ICE in vect_analyze_loop, at tree-vect-loop.c:2415)
[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
2629struct polymorphic_call_target_d
2630{
eefe9a99 2631 HOST_WIDE_INT otr_token;
68377e53
JH
2632 ipa_polymorphic_call_context context;
2633 odr_type type;
eefe9a99 2634 vec <cgraph_node *> targets;
91bc34a9 2635 tree decl_warning;
2f28755f 2636 int type_warning;
f7293b9d 2637 unsigned int n_odr_types;
2f28755f
JH
2638 bool complete;
2639 bool speculative;
eefe9a99
JH
2640};
2641
2642/* Polymorphic call target cache helpers. */
2643
7edd9b15
RS
2644struct polymorphic_call_target_hasher
2645 : pointer_hash <polymorphic_call_target_d>
eefe9a99 2646{
67f58944
TS
2647 static inline hashval_t hash (const polymorphic_call_target_d *);
2648 static inline bool equal (const polymorphic_call_target_d *,
2649 const polymorphic_call_target_d *);
2650 static inline void remove (polymorphic_call_target_d *);
eefe9a99
JH
2651};
2652
2653/* Return the computed hashcode for ODR_QUERY. */
2654
2655inline hashval_t
67f58944 2656polymorphic_call_target_hasher::hash (const polymorphic_call_target_d *odr_query)
eefe9a99 2657{
d313d45f
AK
2658 inchash::hash hstate (odr_query->otr_token);
2659
449e9a33 2660 hstate.add_hwi (odr_query->type->id);
d313d45f 2661 hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
449e9a33 2662 hstate.add_hwi (odr_query->context.offset);
f7293b9d 2663 hstate.add_hwi (odr_query->n_odr_types);
68377e53 2664
3339f0bc
JH
2665 if (odr_query->context.speculative_outer_type)
2666 {
d313d45f 2667 hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
449e9a33 2668 hstate.add_hwi (odr_query->context.speculative_offset);
3339f0bc 2669 }
2f28755f 2670 hstate.add_flag (odr_query->speculative);
d313d45f
AK
2671 hstate.add_flag (odr_query->context.maybe_in_construction);
2672 hstate.add_flag (odr_query->context.maybe_derived_type);
2673 hstate.add_flag (odr_query->context.speculative_maybe_derived_type);
2674 hstate.commit_flag ();
2675 return hstate.end ();
eefe9a99
JH
2676}
2677
2678/* Compare cache entries T1 and T2. */
2679
2680inline bool
67f58944
TS
2681polymorphic_call_target_hasher::equal (const polymorphic_call_target_d *t1,
2682 const polymorphic_call_target_d *t2)
eefe9a99 2683{
68377e53 2684 return (t1->type == t2->type && t1->otr_token == t2->otr_token
2f28755f 2685 && t1->speculative == t2->speculative
68377e53 2686 && t1->context.offset == t2->context.offset
3339f0bc 2687 && t1->context.speculative_offset == t2->context.speculative_offset
68377e53 2688 && t1->context.outer_type == t2->context.outer_type
3339f0bc 2689 && t1->context.speculative_outer_type == t2->context.speculative_outer_type
68377e53
JH
2690 && t1->context.maybe_in_construction
2691 == t2->context.maybe_in_construction
3339f0bc
JH
2692 && t1->context.maybe_derived_type == t2->context.maybe_derived_type
2693 && (t1->context.speculative_maybe_derived_type
f7293b9d
JH
2694 == t2->context.speculative_maybe_derived_type)
2695 /* Adding new type may affect outcome of target search. */
2696 && t1->n_odr_types == t2->n_odr_types);
eefe9a99
JH
2697}
2698
2699/* Remove entry in polymorphic call target cache hash. */
2700
2701inline void
67f58944 2702polymorphic_call_target_hasher::remove (polymorphic_call_target_d *v)
eefe9a99
JH
2703{
2704 v->targets.release ();
2705 free (v);
2706}
2707
2708/* Polymorphic call target query cache. */
2709
c203e8a7 2710typedef hash_table<polymorphic_call_target_hasher>
eefe9a99 2711 polymorphic_call_target_hash_type;
c203e8a7 2712static polymorphic_call_target_hash_type *polymorphic_call_target_hash;
eefe9a99
JH
2713
2714/* Destroy polymorphic call target query cache. */
2715
2716static void
2717free_polymorphic_call_targets_hash ()
2718{
0e1474e5
JH
2719 if (cached_polymorphic_call_targets)
2720 {
c203e8a7
TS
2721 delete polymorphic_call_target_hash;
2722 polymorphic_call_target_hash = NULL;
6e2830c3 2723 delete cached_polymorphic_call_targets;
0e1474e5
JH
2724 cached_polymorphic_call_targets = NULL;
2725 }
eefe9a99
JH
2726}
2727
c1b8f25d
JH
2728/* Force rebuilding type inheritance graph from scratch.
2729 This is use to make sure that we do not keep references to types
2730 which was not visible to free_lang_data. */
2731
2732void
2733rebuild_type_inheritance_graph ()
2734{
2735 if (!odr_hash)
2736 return;
2737 delete odr_hash;
c1b8f25d 2738 odr_hash = NULL;
c1b8f25d
JH
2739 odr_types_ptr = NULL;
2740 free_polymorphic_call_targets_hash ();
2741}
2742
eefe9a99
JH
2743/* When virtual function is removed, we may need to flush the cache. */
2744
2745static void
2746devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
2747{
0e1474e5 2748 if (cached_polymorphic_call_targets
6e2830c3 2749 && cached_polymorphic_call_targets->contains (n))
eefe9a99
JH
2750 free_polymorphic_call_targets_hash ();
2751}
2752
d6ae9a6d 2753/* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
390675c8 2754
aa803cc7 2755tree
85942f45
JH
2756subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
2757 tree vtable)
390675c8
JH
2758{
2759 tree v = BINFO_VTABLE (binfo);
2760 int i;
2761 tree base_binfo;
85942f45 2762 unsigned HOST_WIDE_INT this_offset;
390675c8 2763
85942f45
JH
2764 if (v)
2765 {
2766 if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
2767 gcc_unreachable ();
2768
2769 if (offset == this_offset
2770 && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
2771 return binfo;
2772 }
390675c8 2773
390675c8
JH
2774 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2775 if (polymorphic_type_binfo_p (base_binfo))
2776 {
2777 base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable);
2778 if (base_binfo)
2779 return base_binfo;
2780 }
2781 return NULL;
2782}
2783
85942f45
JH
2784/* T is known constant value of virtual table pointer.
2785 Store virtual table to V and its offset to OFFSET.
2786 Return false if T does not look like virtual table reference. */
390675c8 2787
85942f45 2788bool
d570d364
JH
2789vtable_pointer_value_to_vtable (const_tree t, tree *v,
2790 unsigned HOST_WIDE_INT *offset)
390675c8
JH
2791{
2792 /* We expect &MEM[(void *)&virtual_table + 16B].
2793 We obtain object's BINFO from the context of the virtual table.
2794 This one contains pointer to virtual table represented via
d6ae9a6d 2795 POINTER_PLUS_EXPR. Verify that this pointer matches what
390675c8
JH
2796 we propagated through.
2797
2798 In the case of virtual inheritance, the virtual tables may
2799 be nested, i.e. the offset may be different from 16 and we may
2800 need to dive into the type representation. */
85942f45 2801 if (TREE_CODE (t) == ADDR_EXPR
390675c8
JH
2802 && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
2803 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
2804 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
2805 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
2806 == VAR_DECL)
2807 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2808 (TREE_OPERAND (t, 0), 0), 0)))
2809 {
85942f45
JH
2810 *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
2811 *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
2812 return true;
390675c8 2813 }
85942f45
JH
2814
2815 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2816 We need to handle it when T comes from static variable initializer or
2817 BINFO. */
2818 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
2819 {
2820 *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
2821 t = TREE_OPERAND (t, 0);
2822 }
2823 else
2824 *offset = 0;
2825
2826 if (TREE_CODE (t) != ADDR_EXPR)
2827 return false;
2828 *v = TREE_OPERAND (t, 0);
2829 return true;
2830}
2831
2832/* T is known constant value of virtual table pointer. Return BINFO of the
2833 instance type. */
2834
2835tree
d570d364 2836vtable_pointer_value_to_binfo (const_tree t)
85942f45
JH
2837{
2838 tree vtable;
2839 unsigned HOST_WIDE_INT offset;
2840
2841 if (!vtable_pointer_value_to_vtable (t, &vtable, &offset))
2842 return NULL_TREE;
2843
2844 /* FIXME: for stores of construction vtables we return NULL,
2845 because we do not have BINFO for those. Eventually we should fix
2846 our representation to allow this case to be handled, too.
2847 In the case we see store of BINFO we however may assume
d6ae9a6d 2848 that standard folding will be able to cope with it. */
85942f45
JH
2849 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
2850 offset, vtable);
390675c8
JH
2851}
2852
68377e53 2853/* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
d6ae9a6d
SL
2854 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2855 and insert them in NODES.
68377e53
JH
2856
2857 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2858
2859static void
2860record_targets_from_bases (tree otr_type,
2861 HOST_WIDE_INT otr_token,
2862 tree outer_type,
2863 HOST_WIDE_INT offset,
ec77d61f 2864 vec <cgraph_node *> &nodes,
6e2830c3
TS
2865 hash_set<tree> *inserted,
2866 hash_set<tree> *matched_vtables,
68377e53
JH
2867 bool *completep)
2868{
2869 while (true)
2870 {
2871 HOST_WIDE_INT pos, size;
2872 tree base_binfo;
2873 tree fld;
2874
2875 if (types_same_for_odr (outer_type, otr_type))
2876 return;
2877
2878 for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
2879 {
2880 if (TREE_CODE (fld) != FIELD_DECL)
2881 continue;
2882
2883 pos = int_bit_position (fld);
2884 size = tree_to_shwi (DECL_SIZE (fld));
ec77d61f
JH
2885 if (pos <= offset && (pos + size) > offset
2886 /* Do not get confused by zero sized bases. */
2887 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
68377e53
JH
2888 break;
2889 }
d6ae9a6d 2890 /* Within a class type we should always find corresponding fields. */
68377e53
JH
2891 gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
2892
d6ae9a6d 2893 /* Nonbase types should have been stripped by outer_class_type. */
68377e53
JH
2894 gcc_assert (DECL_ARTIFICIAL (fld));
2895
2896 outer_type = TREE_TYPE (fld);
2897 offset -= pos;
2898
2899 base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
2900 offset, otr_type);
ec77d61f
JH
2901 if (!base_binfo)
2902 {
2903 gcc_assert (odr_violation_reported);
2904 return;
2905 }
68377e53 2906 gcc_assert (base_binfo);
6e2830c3 2907 if (!matched_vtables->add (BINFO_VTABLE (base_binfo)))
68377e53 2908 {
ec77d61f
JH
2909 bool can_refer;
2910 tree target = gimple_get_virt_method_for_binfo (otr_token,
2911 base_binfo,
2912 &can_refer);
2d1644bf
JH
2913 if (!target || ! DECL_CXX_DESTRUCTOR_P (target))
2914 maybe_record_node (nodes, target, inserted, can_refer, completep);
6e2830c3 2915 matched_vtables->add (BINFO_VTABLE (base_binfo));
68377e53
JH
2916 }
2917 }
2918}
2919
3462aa02
JH
2920/* When virtual table is removed, we may need to flush the cache. */
2921
2922static void
2c8326a5 2923devirt_variable_node_removal_hook (varpool_node *n,
3462aa02
JH
2924 void *d ATTRIBUTE_UNUSED)
2925{
2926 if (cached_polymorphic_call_targets
67348ccc
DM
2927 && DECL_VIRTUAL_P (n->decl)
2928 && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
3462aa02
JH
2929 free_polymorphic_call_targets_hash ();
2930}
2931
91bc34a9 2932/* Record about how many calls would benefit from given type to be final. */
7d0aa05b 2933
91bc34a9
JH
2934struct odr_type_warn_count
2935{
9716cc3e 2936 tree type;
91bc34a9 2937 int count;
3995f3a2 2938 profile_count dyn_count;
91bc34a9
JH
2939};
2940
2941/* Record about how many calls would benefit from given method to be final. */
7d0aa05b 2942
91bc34a9
JH
2943struct decl_warn_count
2944{
2945 tree decl;
2946 int count;
3995f3a2 2947 profile_count dyn_count;
91bc34a9
JH
2948};
2949
2950/* Information about type and decl warnings. */
7d0aa05b 2951
91bc34a9
JH
2952struct final_warning_record
2953{
53b73588
ML
2954 /* If needed grow type_warnings vector and initialize new decl_warn_count
2955 to have dyn_count set to profile_count::zero (). */
2956 void grow_type_warnings (unsigned newlen);
2957
3995f3a2 2958 profile_count dyn_count;
ed37a6cf 2959 auto_vec<odr_type_warn_count> type_warnings;
91bc34a9
JH
2960 hash_map<tree, decl_warn_count> decl_warnings;
2961};
53b73588
ML
2962
2963void
2964final_warning_record::grow_type_warnings (unsigned newlen)
2965{
2966 unsigned len = type_warnings.length ();
2967 if (newlen > len)
2968 {
2969 type_warnings.safe_grow_cleared (newlen);
2970 for (unsigned i = len; i < newlen; i++)
2971 type_warnings[i].dyn_count = profile_count::zero ();
2972 }
2973}
2974
91bc34a9
JH
2975struct final_warning_record *final_warning_records;
2976
eefe9a99 2977/* Return vector containing possible targets of polymorphic call of type
d6ae9a6d
SL
2978 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
2979 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
68377e53
JH
2980 OTR_TYPE and include their virtual method. This is useful for types
2981 possibly in construction or destruction where the virtual table may
2982 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
2983 us to walk the inheritance graph for all derivations.
2984
add5c763 2985 If COMPLETEP is non-NULL, store true if the list is complete.
eefe9a99
JH
2986 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
2987 in the target cache. If user needs to visit every target list
2988 just once, it can memoize them.
2989
2f28755f
JH
2990 If SPECULATIVE is set, the list will not contain targets that
2991 are not speculatively taken.
ec77d61f 2992
eefe9a99
JH
2993 Returned vector is placed into cache. It is NOT caller's responsibility
2994 to free it. The vector can be freed on cgraph_remove_node call if
2995 the particular node is a virtual function present in the cache. */
2996
2997vec <cgraph_node *>
2998possible_polymorphic_call_targets (tree otr_type,
2999 HOST_WIDE_INT otr_token,
68377e53
JH
3000 ipa_polymorphic_call_context context,
3001 bool *completep,
ec77d61f 3002 void **cache_token,
2f28755f 3003 bool speculative)
eefe9a99
JH
3004{
3005 static struct cgraph_node_hook_list *node_removal_hook_holder;
add5c763 3006 vec <cgraph_node *> nodes = vNULL;
1be0e58d 3007 auto_vec <tree, 8> bases_to_consider;
68377e53 3008 odr_type type, outer_type;
eefe9a99
JH
3009 polymorphic_call_target_d key;
3010 polymorphic_call_target_d **slot;
3011 unsigned int i;
3012 tree binfo, target;
ec77d61f 3013 bool complete;
b2d82f2d 3014 bool can_refer = false;
2d1644bf 3015 bool skipped = false;
eefe9a99 3016
c7e1befa
JH
3017 otr_type = TYPE_MAIN_VARIANT (otr_type);
3018
d6ae9a6d 3019 /* If ODR is not initialized or the context is invalid, return empty
6f8091fc 3020 incomplete list. */
b41e0d29 3021 if (!odr_hash || context.invalid || !TYPE_BINFO (otr_type))
3e86c6a8
JH
3022 {
3023 if (completep)
6f8091fc 3024 *completep = context.invalid;
beb683ab
MJ
3025 if (cache_token)
3026 *cache_token = NULL;
3e86c6a8
JH
3027 return nodes;
3028 }
3029
91bc34a9 3030 /* Do not bother to compute speculative info when user do not asks for it. */
2f28755f 3031 if (!speculative || !context.speculative_outer_type)
4d7cf10d 3032 context.clear_speculation ();
91bc34a9 3033
68377e53 3034 type = get_odr_type (otr_type, true);
eefe9a99 3035
d6ae9a6d 3036 /* Recording type variants would waste results cache. */
c7e1befa
JH
3037 gcc_assert (!context.outer_type
3038 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3039
d6ae9a6d 3040 /* Look up the outer class type we want to walk.
a1981458 3041 If we fail to do so, the context is invalid. */
a0fd3373 3042 if ((context.outer_type || context.speculative_outer_type)
4d7cf10d 3043 && !context.restrict_to_inner_class (otr_type))
3e86c6a8
JH
3044 {
3045 if (completep)
a1981458 3046 *completep = true;
beb683ab
MJ
3047 if (cache_token)
3048 *cache_token = NULL;
3e86c6a8
JH
3049 return nodes;
3050 }
a1981458 3051 gcc_assert (!context.invalid);
eefe9a99 3052
4d7cf10d 3053 /* Check that restrict_to_inner_class kept the main variant. */
c7e1befa
JH
3054 gcc_assert (!context.outer_type
3055 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3056
79c7de84 3057 /* We canonicalize our query, so we do not need extra hashtable entries. */
68377e53
JH
3058
3059 /* Without outer type, we have no use for offset. Just do the
d6ae9a6d 3060 basic search from inner type. */
68377e53 3061 if (!context.outer_type)
6ff65dd7 3062 context.clear_outer_type (otr_type);
d6ae9a6d 3063 /* We need to update our hierarchy if the type does not exist. */
68377e53 3064 outer_type = get_odr_type (context.outer_type, true);
ec77d61f 3065 /* If the type is complete, there are no derivations. */
68377e53
JH
3066 if (TYPE_FINAL_P (outer_type->type))
3067 context.maybe_derived_type = false;
eefe9a99
JH
3068
3069 /* Initialize query cache. */
3070 if (!cached_polymorphic_call_targets)
3071 {
6e2830c3 3072 cached_polymorphic_call_targets = new hash_set<cgraph_node *>;
c203e8a7
TS
3073 polymorphic_call_target_hash
3074 = new polymorphic_call_target_hash_type (23);
eefe9a99 3075 if (!node_removal_hook_holder)
3462aa02
JH
3076 {
3077 node_removal_hook_holder =
3dafb85c
ML
3078 symtab->add_cgraph_removal_hook (&devirt_node_removal_hook, NULL);
3079 symtab->add_varpool_removal_hook (&devirt_variable_node_removal_hook,
3462aa02
JH
3080 NULL);
3081 }
eefe9a99
JH
3082 }
3083
21a9ce6e
JH
3084 if (in_lto_p)
3085 {
3086 if (context.outer_type != otr_type)
3087 context.outer_type
3088 = get_odr_type (context.outer_type, true)->type;
3089 if (context.speculative_outer_type)
3090 context.speculative_outer_type
3091 = get_odr_type (context.speculative_outer_type, true)->type;
3092 }
3093
d6ae9a6d 3094 /* Look up cached answer. */
eefe9a99
JH
3095 key.type = type;
3096 key.otr_token = otr_token;
2f28755f 3097 key.speculative = speculative;
68377e53 3098 key.context = context;
f7293b9d 3099 key.n_odr_types = odr_types.length ();
c203e8a7 3100 slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
eefe9a99
JH
3101 if (cache_token)
3102 *cache_token = (void *)*slot;
3103 if (*slot)
68377e53
JH
3104 {
3105 if (completep)
ec77d61f 3106 *completep = (*slot)->complete;
91bc34a9
JH
3107 if ((*slot)->type_warning && final_warning_records)
3108 {
3109 final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
3995f3a2
JH
3110 if (!final_warning_records->type_warnings
3111 [(*slot)->type_warning - 1].dyn_count.initialized_p ())
3112 final_warning_records->type_warnings
3113 [(*slot)->type_warning - 1].dyn_count = profile_count::zero ();
3114 if (final_warning_records->dyn_count > 0)
3115 final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
3116 = final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
3117 + final_warning_records->dyn_count;
91bc34a9 3118 }
2f28755f 3119 if (!speculative && (*slot)->decl_warning && final_warning_records)
91bc34a9
JH
3120 {
3121 struct decl_warn_count *c =
3122 final_warning_records->decl_warnings.get ((*slot)->decl_warning);
3123 c->count++;
3995f3a2
JH
3124 if (final_warning_records->dyn_count > 0)
3125 c->dyn_count += final_warning_records->dyn_count;
91bc34a9 3126 }
68377e53
JH
3127 return (*slot)->targets;
3128 }
3129
ec77d61f 3130 complete = true;
eefe9a99
JH
3131
3132 /* Do actual search. */
3133 timevar_push (TV_IPA_VIRTUAL_CALL);
3134 *slot = XCNEW (polymorphic_call_target_d);
3135 if (cache_token)
68377e53 3136 *cache_token = (void *)*slot;
eefe9a99
JH
3137 (*slot)->type = type;
3138 (*slot)->otr_token = otr_token;
68377e53 3139 (*slot)->context = context;
2f28755f 3140 (*slot)->speculative = speculative;
eefe9a99 3141
6e2830c3
TS
3142 hash_set<tree> inserted;
3143 hash_set<tree> matched_vtables;
eefe9a99 3144
91bc34a9 3145 /* First insert targets we speculatively identified as likely. */
a0fd3373
JH
3146 if (context.speculative_outer_type)
3147 {
3148 odr_type speculative_outer_type;
91bc34a9
JH
3149 bool speculation_complete = true;
3150
d6ae9a6d
SL
3151 /* First insert target from type itself and check if it may have
3152 derived types. */
a0fd3373
JH
3153 speculative_outer_type = get_odr_type (context.speculative_outer_type, true);
3154 if (TYPE_FINAL_P (speculative_outer_type->type))
3155 context.speculative_maybe_derived_type = false;
3156 binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
3157 context.speculative_offset, otr_type);
3158 if (binfo)
3159 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3160 &can_refer);
3161 else
3162 target = NULL;
3163
91bc34a9
JH
3164 /* In the case we get complete method, we don't need
3165 to walk derivations. */
3166 if (target && DECL_FINAL_P (target))
3167 context.speculative_maybe_derived_type = false;
a0fd3373 3168 if (type_possibly_instantiated_p (speculative_outer_type->type))
91bc34a9 3169 maybe_record_node (nodes, target, &inserted, can_refer, &speculation_complete);
a0fd3373 3170 if (binfo)
6e2830c3 3171 matched_vtables.add (BINFO_VTABLE (binfo));
91bc34a9 3172
9716cc3e 3173
a0fd3373
JH
3174 /* Next walk recursively all derived types. */
3175 if (context.speculative_maybe_derived_type)
91bc34a9
JH
3176 for (i = 0; i < speculative_outer_type->derived_types.length(); i++)
3177 possible_polymorphic_call_targets_1 (nodes, &inserted,
3178 &matched_vtables,
3179 otr_type,
3180 speculative_outer_type->derived_types[i],
3181 otr_token, speculative_outer_type->type,
3182 context.speculative_offset,
3183 &speculation_complete,
3184 bases_to_consider,
3185 false);
a0fd3373
JH
3186 }
3187
2f28755f 3188 if (!speculative || !nodes.length ())
68377e53 3189 {
2f28755f
JH
3190 /* First see virtual method of type itself. */
3191 binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
3192 context.offset, otr_type);
3193 if (binfo)
3194 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3195 &can_refer);
3196 else
3197 {
3198 gcc_assert (odr_violation_reported);
3199 target = NULL;
3200 }
68377e53 3201
2f28755f
JH
3202 /* Destructors are never called through construction virtual tables,
3203 because the type is always known. */
3204 if (target && DECL_CXX_DESTRUCTOR_P (target))
3205 context.maybe_in_construction = false;
ec77d61f 3206
2f28755f
JH
3207 if (target)
3208 {
3209 /* In the case we get complete method, we don't need
3210 to walk derivations. */
3211 if (DECL_FINAL_P (target))
3212 context.maybe_derived_type = false;
3213 }
2d1644bf 3214
2f28755f
JH
3215 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3216 if (type_possibly_instantiated_p (outer_type->type))
3217 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3218 else
3219 skipped = true;
79c7de84 3220
2f28755f
JH
3221 if (binfo)
3222 matched_vtables.add (BINFO_VTABLE (binfo));
eefe9a99 3223
2f28755f
JH
3224 /* Next walk recursively all derived types. */
3225 if (context.maybe_derived_type)
91bc34a9 3226 {
2f28755f
JH
3227 for (i = 0; i < outer_type->derived_types.length(); i++)
3228 possible_polymorphic_call_targets_1 (nodes, &inserted,
3229 &matched_vtables,
3230 otr_type,
3231 outer_type->derived_types[i],
3232 otr_token, outer_type->type,
3233 context.offset, &complete,
3234 bases_to_consider,
3235 context.maybe_in_construction);
3236
3237 if (!outer_type->all_derivations_known)
91bc34a9 3238 {
079cd854 3239 if (!speculative && final_warning_records
448476ff 3240 && nodes.length () == 1
079cd854 3241 && TREE_CODE (TREE_TYPE (nodes[0]->decl)) == METHOD_TYPE)
91bc34a9 3242 {
2f28755f 3243 if (complete
2f28755f
JH
3244 && warn_suggest_final_types
3245 && !outer_type->derived_types.length ())
91bc34a9 3246 {
53b73588
ML
3247 final_warning_records->grow_type_warnings
3248 (outer_type->id);
2f28755f 3249 final_warning_records->type_warnings[outer_type->id].count++;
3995f3a2
JH
3250 if (!final_warning_records->type_warnings
3251 [outer_type->id].dyn_count.initialized_p ())
3252 final_warning_records->type_warnings
3253 [outer_type->id].dyn_count = profile_count::zero ();
2f28755f
JH
3254 final_warning_records->type_warnings[outer_type->id].dyn_count
3255 += final_warning_records->dyn_count;
3256 final_warning_records->type_warnings[outer_type->id].type
3257 = outer_type->type;
3258 (*slot)->type_warning = outer_type->id + 1;
91bc34a9 3259 }
2f28755f
JH
3260 if (complete
3261 && warn_suggest_final_methods
2f28755f
JH
3262 && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl),
3263 outer_type->type))
91bc34a9 3264 {
2f28755f
JH
3265 bool existed;
3266 struct decl_warn_count &c =
3267 final_warning_records->decl_warnings.get_or_insert
3268 (nodes[0]->decl, &existed);
3269
3270 if (existed)
3271 {
3272 c.count++;
3273 c.dyn_count += final_warning_records->dyn_count;
3274 }
3275 else
3276 {
3277 c.count = 1;
3278 c.dyn_count = final_warning_records->dyn_count;
3279 c.decl = nodes[0]->decl;
3280 }
3281 (*slot)->decl_warning = nodes[0]->decl;
91bc34a9 3282 }
91bc34a9 3283 }
2f28755f 3284 complete = false;
91bc34a9 3285 }
91bc34a9 3286 }
2d1644bf 3287
2f28755f
JH
3288 if (!speculative)
3289 {
3290 /* Destructors are never called through construction virtual tables,
d6ae9a6d
SL
3291 because the type is always known. One of entries may be
3292 cxa_pure_virtual so look to at least two of them. */
2f28755f
JH
3293 if (context.maybe_in_construction)
3294 for (i =0 ; i < MIN (nodes.length (), 2); i++)
3295 if (DECL_CXX_DESTRUCTOR_P (nodes[i]->decl))
3296 context.maybe_in_construction = false;
3297 if (context.maybe_in_construction)
3298 {
3299 if (type != outer_type
3300 && (!skipped
3301 || (context.maybe_derived_type
3302 && !type_all_derivations_known_p (outer_type->type))))
3303 record_targets_from_bases (otr_type, otr_token, outer_type->type,
3304 context.offset, nodes, &inserted,
3305 &matched_vtables, &complete);
3306 if (skipped)
3307 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3308 for (i = 0; i < bases_to_consider.length(); i++)
3309 maybe_record_node (nodes, bases_to_consider[i], &inserted, can_refer, &complete);
3310 }
3311 }
2d1644bf 3312 }
ec77d61f 3313
eefe9a99 3314 (*slot)->targets = nodes;
ec77d61f 3315 (*slot)->complete = complete;
f7293b9d 3316 (*slot)->n_odr_types = odr_types.length ();
68377e53 3317 if (completep)
ec77d61f 3318 *completep = complete;
eefe9a99 3319
eefe9a99
JH
3320 timevar_pop (TV_IPA_VIRTUAL_CALL);
3321 return nodes;
3322}
3323
91bc34a9
JH
3324bool
3325add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value,
3326 vec<const decl_warn_count*> *vec)
3327{
3328 vec->safe_push (&value);
3329 return true;
3330}
3331
2f28755f
JH
3332/* Dump target list TARGETS into FILE. */
3333
3334static void
c1dd347c 3335dump_targets (FILE *f, vec <cgraph_node *> targets, bool verbose)
2f28755f
JH
3336{
3337 unsigned int i;
3338
3339 for (i = 0; i < targets.length (); i++)
3340 {
3341 char *name = NULL;
3342 if (in_lto_p)
3343 name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
464d0118
ML
3344 fprintf (f, " %s/%i", name ? name : targets[i]->name (),
3345 targets[i]->order);
2f28755f
JH
3346 if (in_lto_p)
3347 free (name);
3348 if (!targets[i]->definition)
3349 fprintf (f, " (no definition%s)",
3350 DECL_DECLARED_INLINE_P (targets[i]->decl)
3351 ? " inline" : "");
c1dd347c
JH
3352 /* With many targets for every call polymorphic dumps are going to
3353 be quadratic in size. */
3354 if (i > 10 && !verbose)
3355 {
3356 fprintf (f, " ... and %i more targets\n", targets.length () - i);
3357 return;
3358 }
2f28755f
JH
3359 }
3360 fprintf (f, "\n");
3361}
3362
eefe9a99
JH
3363/* Dump all possible targets of a polymorphic call. */
3364
3365void
3366dump_possible_polymorphic_call_targets (FILE *f,
68377e53
JH
3367 tree otr_type,
3368 HOST_WIDE_INT otr_token,
c1dd347c
JH
3369 const ipa_polymorphic_call_context &ctx,
3370 bool verbose)
eefe9a99
JH
3371{
3372 vec <cgraph_node *> targets;
3373 bool final;
549bcbd1 3374 odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false);
2f28755f 3375 unsigned int len;
eefe9a99
JH
3376
3377 if (!type)
3378 return;
3379 targets = possible_polymorphic_call_targets (otr_type, otr_token,
68377e53 3380 ctx,
2f28755f 3381 &final, NULL, false);
68377e53 3382 fprintf (f, " Targets of polymorphic call of type %i:", type->id);
eefe9a99 3383 print_generic_expr (f, type->type, TDF_SLIM);
ec77d61f 3384 fprintf (f, " token %i\n", (int)otr_token);
a1981458
JH
3385
3386 ctx.dump (f);
ec77d61f 3387
a0fd3373 3388 fprintf (f, " %s%s%s%s\n ",
ec77d61f 3389 final ? "This is a complete list." :
68377e53
JH
3390 "This is partial list; extra targets may be defined in other units.",
3391 ctx.maybe_in_construction ? " (base types included)" : "",
a0fd3373
JH
3392 ctx.maybe_derived_type ? " (derived types included)" : "",
3393 ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
2f28755f 3394 len = targets.length ();
c1dd347c 3395 dump_targets (f, targets, verbose);
2f28755f
JH
3396
3397 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3398 ctx,
3399 &final, NULL, true);
2f28755f 3400 if (targets.length () != len)
ec77d61f 3401 {
2f28755f 3402 fprintf (f, " Speculative targets:");
c1dd347c 3403 dump_targets (f, targets, verbose);
ec77d61f 3404 }
3ebd8e62
ML
3405 /* Ugly: during callgraph construction the target cache may get populated
3406 before all targets are found. While this is harmless (because all local
3407 types are discovered and only in those case we devirtualize fully and we
3408 don't do speculative devirtualization before IPA stage) it triggers
3409 assert here when dumping at that stage also populates the case with
3410 speculative targets. Quietly ignore this. */
3411 gcc_assert (symtab->state < IPA_SSA || targets.length () <= len);
2f28755f 3412 fprintf (f, "\n");
eefe9a99
JH
3413}
3414
0e1474e5
JH
3415
3416/* Return true if N can be possibly target of a polymorphic call of
3417 OTR_TYPE/OTR_TOKEN. */
3418
3419bool
3420possible_polymorphic_call_target_p (tree otr_type,
3421 HOST_WIDE_INT otr_token,
68377e53 3422 const ipa_polymorphic_call_context &ctx,
0e1474e5
JH
3423 struct cgraph_node *n)
3424{
3425 vec <cgraph_node *> targets;
3426 unsigned int i;
68377e53 3427 enum built_in_function fcode;
450ad0cd 3428 bool final;
0e1474e5 3429
68377e53 3430 if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
d27ecc49 3431 && ((fcode = DECL_FUNCTION_CODE (n->decl)) == BUILT_IN_UNREACHABLE
68377e53
JH
3432 || fcode == BUILT_IN_TRAP))
3433 return true;
3434
ceda2c69
JH
3435 if (is_cxa_pure_virtual_p (n->decl))
3436 return true;
3437
c203e8a7 3438 if (!odr_hash)
0e1474e5 3439 return true;
68377e53 3440 targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
0e1474e5 3441 for (i = 0; i < targets.length (); i++)
d52f5295 3442 if (n->semantically_equivalent_p (targets[i]))
0e1474e5 3443 return true;
450ad0cd
JH
3444
3445 /* At a moment we allow middle end to dig out new external declarations
3446 as a targets of polymorphic calls. */
67348ccc 3447 if (!final && !n->definition)
450ad0cd 3448 return true;
0e1474e5
JH
3449 return false;
3450}
3451
3452
6f8091fc
JH
3453
3454/* Return true if N can be possibly target of a polymorphic call of
3455 OBJ_TYPE_REF expression REF in STMT. */
3456
3457bool
3458possible_polymorphic_call_target_p (tree ref,
355fe088 3459 gimple *stmt,
6f8091fc
JH
3460 struct cgraph_node *n)
3461{
3462 ipa_polymorphic_call_context context (current_function_decl, ref, stmt);
3463 tree call_fn = gimple_call_fn (stmt);
3464
3465 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn),
3466 tree_to_uhwi
3467 (OBJ_TYPE_REF_TOKEN (call_fn)),
3468 context,
3469 n);
3470}
3471
3472
0e1474e5
JH
3473/* After callgraph construction new external nodes may appear.
3474 Add them into the graph. */
3475
3476void
3477update_type_inheritance_graph (void)
3478{
3479 struct cgraph_node *n;
3480
c203e8a7 3481 if (!odr_hash)
0e1474e5
JH
3482 return;
3483 free_polymorphic_call_targets_hash ();
3484 timevar_push (TV_IPA_INHERITANCE);
68377e53 3485 /* We reconstruct the graph starting from types of all methods seen in the
6af801f5 3486 unit. */
0e1474e5 3487 FOR_EACH_FUNCTION (n)
67348ccc
DM
3488 if (DECL_VIRTUAL_P (n->decl)
3489 && !n->definition
d52f5295 3490 && n->real_symbol_p ())
70e7f2a2 3491 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
0e1474e5
JH
3492 timevar_pop (TV_IPA_INHERITANCE);
3493}
bbc9396b
JH
3494
3495
3496/* Return true if N looks like likely target of a polymorphic call.
3497 Rule out cxa_pure_virtual, noreturns, function declared cold and
3498 other obvious cases. */
3499
3500bool
3501likely_target_p (struct cgraph_node *n)
3502{
3503 int flags;
3504 /* cxa_pure_virtual and similar things are not likely. */
67348ccc 3505 if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
bbc9396b 3506 return false;
67348ccc 3507 flags = flags_from_decl_or_type (n->decl);
bbc9396b
JH
3508 if (flags & ECF_NORETURN)
3509 return false;
3510 if (lookup_attribute ("cold",
67348ccc 3511 DECL_ATTRIBUTES (n->decl)))
bbc9396b
JH
3512 return false;
3513 if (n->frequency < NODE_FREQUENCY_NORMAL)
3514 return false;
d6ae9a6d
SL
3515 /* If there are no live virtual tables referring the target,
3516 the only way the target can be called is an instance coming from other
3517 compilation unit; speculative devirtualization is built around an
ccb05ef2
JH
3518 assumption that won't happen. */
3519 if (!referenced_from_vtable_p (n))
3520 return false;
bbc9396b
JH
3521 return true;
3522}
3523
d6ae9a6d 3524/* Compare type warning records P1 and P2 and choose one with larger count;
91bc34a9
JH
3525 helper for qsort. */
3526
3527int
3528type_warning_cmp (const void *p1, const void *p2)
3529{
3530 const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
3531 const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
3532
3533 if (t1->dyn_count < t2->dyn_count)
3534 return 1;
3535 if (t1->dyn_count > t2->dyn_count)
3536 return -1;
3537 return t2->count - t1->count;
3538}
3539
d6ae9a6d 3540/* Compare decl warning records P1 and P2 and choose one with larger count;
91bc34a9
JH
3541 helper for qsort. */
3542
3543int
3544decl_warning_cmp (const void *p1, const void *p2)
3545{
3546 const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
3547 const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
3548
3549 if (t1->dyn_count < t2->dyn_count)
3550 return 1;
3551 if (t1->dyn_count > t2->dyn_count)
3552 return -1;
3553 return t2->count - t1->count;
3554}
3555
5ce97055 3556
d6ae9a6d 3557/* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
5ce97055
JH
3558 context CTX. */
3559
3560struct cgraph_node *
3561try_speculative_devirtualization (tree otr_type, HOST_WIDE_INT otr_token,
3562 ipa_polymorphic_call_context ctx)
3563{
3564 vec <cgraph_node *>targets
3565 = possible_polymorphic_call_targets
3566 (otr_type, otr_token, ctx, NULL, NULL, true);
3567 unsigned int i;
3568 struct cgraph_node *likely_target = NULL;
3569
3570 for (i = 0; i < targets.length (); i++)
3571 if (likely_target_p (targets[i]))
3572 {
3573 if (likely_target)
3574 return NULL;
3575 likely_target = targets[i];
3576 }
3577 if (!likely_target
3578 ||!likely_target->definition
3579 || DECL_EXTERNAL (likely_target->decl))
3580 return NULL;
3581
3582 /* Don't use an implicitly-declared destructor (c++/58678). */
3583 struct cgraph_node *non_thunk_target
3584 = likely_target->function_symbol ();
3585 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3586 return NULL;
3587 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3588 && likely_target->can_be_discarded_p ())
3589 return NULL;
3590 return likely_target;
3591}
3592
bbc9396b 3593/* The ipa-devirt pass.
3462aa02 3594 When polymorphic call has only one likely target in the unit,
d6ae9a6d 3595 turn it into a speculative call. */
bbc9396b
JH
3596
3597static unsigned int
3598ipa_devirt (void)
3599{
3600 struct cgraph_node *n;
6e2830c3 3601 hash_set<void *> bad_call_targets;
bbc9396b
JH
3602 struct cgraph_edge *e;
3603
3604 int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
3605 int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
570215f9 3606 int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
68c9467f 3607 int ndropped = 0;
bbc9396b 3608
4cd5658b
MT
3609 if (!odr_types_ptr)
3610 return 0;
3611
609570b4
JH
3612 if (dump_file)
3613 dump_type_inheritance_graph (dump_file);
3614
91bc34a9
JH
3615 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3616 This is implemented by setting up final_warning_records that are updated
3617 by get_polymorphic_call_targets.
3618 We need to clear cache in this case to trigger recomputation of all
3619 entries. */
3620 if (warn_suggest_final_methods || warn_suggest_final_types)
3621 {
3622 final_warning_records = new (final_warning_record);
3995f3a2 3623 final_warning_records->dyn_count = profile_count::zero ();
53b73588 3624 final_warning_records->grow_type_warnings (odr_types.length ());
91bc34a9
JH
3625 free_polymorphic_call_targets_hash ();
3626 }
3627
bbc9396b
JH
3628 FOR_EACH_DEFINED_FUNCTION (n)
3629 {
3630 bool update = false;
2bf86c84
JH
3631 if (!opt_for_fn (n->decl, flag_devirtualize))
3632 continue;
bbc9396b 3633 if (dump_file && n->indirect_calls)
464d0118
ML
3634 fprintf (dump_file, "\n\nProcesing function %s\n",
3635 n->dump_name ());
bbc9396b
JH
3636 for (e = n->indirect_calls; e; e = e->next_callee)
3637 if (e->indirect_info->polymorphic)
3638 {
3639 struct cgraph_node *likely_target = NULL;
3640 void *cache_token;
3641 bool final;
91bc34a9
JH
3642
3643 if (final_warning_records)
1bad9c18 3644 final_warning_records->dyn_count = e->count.ipa ();
91bc34a9 3645
bbc9396b
JH
3646 vec <cgraph_node *>targets
3647 = possible_polymorphic_call_targets
2f28755f 3648 (e, &final, &cache_token, true);
bbc9396b
JH
3649 unsigned int i;
3650
2f28755f
JH
3651 /* Trigger warnings by calculating non-speculative targets. */
3652 if (warn_suggest_final_methods || warn_suggest_final_types)
3653 possible_polymorphic_call_targets (e);
3654
bbc9396b
JH
3655 if (dump_file)
3656 dump_possible_polymorphic_call_targets
c1dd347c 3657 (dump_file, e, (dump_flags & TDF_DETAILS));
3462aa02 3658
bbc9396b
JH
3659 npolymorphic++;
3660
68c9467f
JH
3661 /* See if the call can be devirtualized by means of ipa-prop's
3662 polymorphic call context propagation. If not, we can just
3663 forget about this call being polymorphic and avoid some heavy
3664 lifting in remove_unreachable_nodes that will otherwise try to
3665 keep all possible targets alive until inlining and in the inliner
3666 itself.
3667
3668 This may need to be revisited once we add further ways to use
3669 the may edges, but it is a resonable thing to do right now. */
3670
3671 if ((e->indirect_info->param_index == -1
3672 || (!opt_for_fn (n->decl, flag_devirtualize_speculatively)
3673 && e->indirect_info->vptr_changed))
3674 && !flag_ltrans_devirtualize)
3675 {
3676 e->indirect_info->polymorphic = false;
3677 ndropped++;
3678 if (dump_file)
3679 fprintf (dump_file, "Dropping polymorphic call info;"
67914693 3680 " it cannot be used by ipa-prop\n");
68c9467f
JH
3681 }
3682
2bf86c84 3683 if (!opt_for_fn (n->decl, flag_devirtualize_speculatively))
91bc34a9
JH
3684 continue;
3685
3dafb85c 3686 if (!e->maybe_hot_p ())
bbc9396b
JH
3687 {
3688 if (dump_file)
ec77d61f 3689 fprintf (dump_file, "Call is cold\n\n");
bbc9396b
JH
3690 ncold++;
3691 continue;
3692 }
3693 if (e->speculative)
3694 {
3695 if (dump_file)
d6ae9a6d 3696 fprintf (dump_file, "Call is already speculated\n\n");
bbc9396b
JH
3697 nspeculated++;
3698
3699 /* When dumping see if we agree with speculation. */
3700 if (!dump_file)
3701 continue;
3702 }
6e2830c3 3703 if (bad_call_targets.contains (cache_token))
bbc9396b
JH
3704 {
3705 if (dump_file)
ec77d61f 3706 fprintf (dump_file, "Target list is known to be useless\n\n");
bbc9396b
JH
3707 nmultiple++;
3708 continue;
3709 }
c3284718 3710 for (i = 0; i < targets.length (); i++)
bbc9396b
JH
3711 if (likely_target_p (targets[i]))
3712 {
3713 if (likely_target)
3714 {
2f28755f
JH
3715 likely_target = NULL;
3716 if (dump_file)
3717 fprintf (dump_file, "More than one likely target\n\n");
3718 nmultiple++;
bbc9396b
JH
3719 break;
3720 }
3721 likely_target = targets[i];
3722 }
3723 if (!likely_target)
3724 {
6e2830c3 3725 bad_call_targets.add (cache_token);
bbc9396b
JH
3726 continue;
3727 }
3728 /* This is reached only when dumping; check if we agree or disagree
3729 with the speculation. */
3730 if (e->speculative)
3731 {
3732 struct cgraph_edge *e2;
3733 struct ipa_ref *ref;
3dafb85c 3734 e->speculative_call_info (e2, e, ref);
d52f5295
ML
3735 if (e2->callee->ultimate_alias_target ()
3736 == likely_target->ultimate_alias_target ())
bbc9396b 3737 {
ec77d61f 3738 fprintf (dump_file, "We agree with speculation\n\n");
bbc9396b
JH
3739 nok++;
3740 }
3741 else
3742 {
ec77d61f 3743 fprintf (dump_file, "We disagree with speculation\n\n");
bbc9396b
JH
3744 nwrong++;
3745 }
3746 continue;
3747 }
67348ccc 3748 if (!likely_target->definition)
bbc9396b
JH
3749 {
3750 if (dump_file)
d6ae9a6d 3751 fprintf (dump_file, "Target is not a definition\n\n");
bbc9396b
JH
3752 nnotdefined++;
3753 continue;
3754 }
3755 /* Do not introduce new references to external symbols. While we
3756 can handle these just well, it is common for programs to
3757 incorrectly with headers defining methods they are linked
3758 with. */
67348ccc 3759 if (DECL_EXTERNAL (likely_target->decl))
bbc9396b
JH
3760 {
3761 if (dump_file)
ec77d61f 3762 fprintf (dump_file, "Target is external\n\n");
bbc9396b
JH
3763 nexternal++;
3764 continue;
3765 }
570215f9
JM
3766 /* Don't use an implicitly-declared destructor (c++/58678). */
3767 struct cgraph_node *non_thunk_target
d52f5295 3768 = likely_target->function_symbol ();
89632536 3769 if (DECL_ARTIFICIAL (non_thunk_target->decl))
570215f9
JM
3770 {
3771 if (dump_file)
3772 fprintf (dump_file, "Target is artificial\n\n");
3773 nartificial++;
3774 continue;
3775 }
d52f5295
ML
3776 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3777 && likely_target->can_be_discarded_p ())
bbc9396b
JH
3778 {
3779 if (dump_file)
ec77d61f 3780 fprintf (dump_file, "Target is overwritable\n\n");
bbc9396b
JH
3781 noverwritable++;
3782 continue;
3783 }
2b5f0895 3784 else if (dbg_cnt (devirt))
bbc9396b 3785 {
2b5f0895
XDL
3786 if (dump_enabled_p ())
3787 {
4f5b9c80 3788 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, e->call_stmt,
464d0118
ML
3789 "speculatively devirtualizing call "
3790 "in %s to %s\n",
3791 n->dump_name (),
3792 likely_target->dump_name ());
2b5f0895 3793 }
d52f5295 3794 if (!likely_target->can_be_discarded_p ())
5b79657a
JH
3795 {
3796 cgraph_node *alias;
d52f5295 3797 alias = dyn_cast<cgraph_node *> (likely_target->noninterposable_alias ());
5b79657a
JH
3798 if (alias)
3799 likely_target = alias;
3800 }
bbc9396b
JH
3801 nconverted++;
3802 update = true;
3dafb85c 3803 e->make_speculative
1bad9c18 3804 (likely_target, e->count.apply_scale (8, 10));
bbc9396b
JH
3805 }
3806 }
3807 if (update)
0bceb671 3808 ipa_update_overall_fn_summary (n);
bbc9396b 3809 }
91bc34a9
JH
3810 if (warn_suggest_final_methods || warn_suggest_final_types)
3811 {
3812 if (warn_suggest_final_types)
3813 {
3814 final_warning_records->type_warnings.qsort (type_warning_cmp);
3815 for (unsigned int i = 0;
3816 i < final_warning_records->type_warnings.length (); i++)
3817 if (final_warning_records->type_warnings[i].count)
3818 {
9716cc3e 3819 tree type = final_warning_records->type_warnings[i].type;
26e82579 3820 int count = final_warning_records->type_warnings[i].count;
3995f3a2 3821 profile_count dyn_count
26e82579
JH
3822 = final_warning_records->type_warnings[i].dyn_count;
3823
3995f3a2 3824 if (!(dyn_count > 0))
26e82579
JH
3825 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3826 OPT_Wsuggest_final_types, count,
3827 "Declaring type %qD final "
3828 "would enable devirtualization of %i call",
3829 "Declaring type %qD final "
3830 "would enable devirtualization of %i calls",
3831 type,
3832 count);
3833 else
3834 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3835 OPT_Wsuggest_final_types, count,
3836 "Declaring type %qD final "
3837 "would enable devirtualization of %i call "
3838 "executed %lli times",
3839 "Declaring type %qD final "
3840 "would enable devirtualization of %i calls "
3841 "executed %lli times",
3842 type,
3843 count,
3995f3a2 3844 (long long) dyn_count.to_gcov_type ());
91bc34a9
JH
3845 }
3846 }
3847
3848 if (warn_suggest_final_methods)
3849 {
ed37a6cf 3850 auto_vec<const decl_warn_count*> decl_warnings_vec;
91bc34a9
JH
3851
3852 final_warning_records->decl_warnings.traverse
3853 <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec);
3854 decl_warnings_vec.qsort (decl_warning_cmp);
3855 for (unsigned int i = 0; i < decl_warnings_vec.length (); i++)
3856 {
3857 tree decl = decl_warnings_vec[i]->decl;
3858 int count = decl_warnings_vec[i]->count;
3995f3a2
JH
3859 profile_count dyn_count
3860 = decl_warnings_vec[i]->dyn_count;
26e82579 3861
3995f3a2 3862 if (!(dyn_count > 0))
26e82579
JH
3863 if (DECL_CXX_DESTRUCTOR_P (decl))
3864 warning_n (DECL_SOURCE_LOCATION (decl),
3865 OPT_Wsuggest_final_methods, count,
3866 "Declaring virtual destructor of %qD final "
3867 "would enable devirtualization of %i call",
3868 "Declaring virtual destructor of %qD final "
3869 "would enable devirtualization of %i calls",
3870 DECL_CONTEXT (decl), count);
3871 else
3872 warning_n (DECL_SOURCE_LOCATION (decl),
3873 OPT_Wsuggest_final_methods, count,
3874 "Declaring method %qD final "
3875 "would enable devirtualization of %i call",
3876 "Declaring method %qD final "
3877 "would enable devirtualization of %i calls",
3878 decl, count);
3879 else if (DECL_CXX_DESTRUCTOR_P (decl))
3880 warning_n (DECL_SOURCE_LOCATION (decl),
3881 OPT_Wsuggest_final_methods, count,
3882 "Declaring virtual destructor of %qD final "
3883 "would enable devirtualization of %i call "
3884 "executed %lli times",
3885 "Declaring virtual destructor of %qD final "
3886 "would enable devirtualization of %i calls "
3887 "executed %lli times",
3995f3a2
JH
3888 DECL_CONTEXT (decl), count,
3889 (long long)dyn_count.to_gcov_type ());
26e82579
JH
3890 else
3891 warning_n (DECL_SOURCE_LOCATION (decl),
3892 OPT_Wsuggest_final_methods, count,
3893 "Declaring method %qD final "
3894 "would enable devirtualization of %i call "
3895 "executed %lli times",
3896 "Declaring method %qD final "
3897 "would enable devirtualization of %i calls "
3898 "executed %lli times",
3995f3a2
JH
3899 decl, count,
3900 (long long)dyn_count.to_gcov_type ());
91bc34a9
JH
3901 }
3902 }
ed37a6cf 3903
91bc34a9
JH
3904 delete (final_warning_records);
3905 final_warning_records = 0;
3906 }
bbc9396b
JH
3907
3908 if (dump_file)
3909 fprintf (dump_file,
3910 "%i polymorphic calls, %i devirtualized,"
3911 " %i speculatively devirtualized, %i cold\n"
3912 "%i have multiple targets, %i overwritable,"
3913 " %i already speculated (%i agree, %i disagree),"
68c9467f 3914 " %i external, %i not defined, %i artificial, %i infos dropped\n",
bbc9396b
JH
3915 npolymorphic, ndevirtualized, nconverted, ncold,
3916 nmultiple, noverwritable, nspeculated, nok, nwrong,
68c9467f
JH
3917 nexternal, nnotdefined, nartificial, ndropped);
3918 return ndevirtualized || ndropped ? TODO_remove_functions : 0;
bbc9396b
JH
3919}
3920
bbc9396b
JH
3921namespace {
3922
3923const pass_data pass_data_ipa_devirt =
3924{
3925 IPA_PASS, /* type */
3926 "devirt", /* name */
3927 OPTGROUP_NONE, /* optinfo_flags */
bbc9396b
JH
3928 TV_IPA_DEVIRT, /* tv_id */
3929 0, /* properties_required */
3930 0, /* properties_provided */
3931 0, /* properties_destroyed */
3932 0, /* todo_flags_start */
3933 ( TODO_dump_symtab ), /* todo_flags_finish */
3934};
3935
3936class pass_ipa_devirt : public ipa_opt_pass_d
3937{
3938public:
c3284718
RS
3939 pass_ipa_devirt (gcc::context *ctxt)
3940 : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
3941 NULL, /* generate_summary */
3942 NULL, /* write_summary */
3943 NULL, /* read_summary */
3944 NULL, /* write_optimization_summary */
3945 NULL, /* read_optimization_summary */
3946 NULL, /* stmt_fixup */
3947 0, /* function_transform_todo_flags_start */
3948 NULL, /* function_transform */
3949 NULL) /* variable_transform */
bbc9396b
JH
3950 {}
3951
3952 /* opt_pass methods: */
1a3d085c
TS
3953 virtual bool gate (function *)
3954 {
2bf86c84
JH
3955 /* In LTO, always run the IPA passes and decide on function basis if the
3956 pass is enabled. */
3957 if (in_lto_p)
3958 return true;
1a3d085c 3959 return (flag_devirtualize
91bc34a9
JH
3960 && (flag_devirtualize_speculatively
3961 || (warn_suggest_final_methods
3962 || warn_suggest_final_types))
1a3d085c
TS
3963 && optimize);
3964 }
3965
be55bfe6 3966 virtual unsigned int execute (function *) { return ipa_devirt (); }
bbc9396b
JH
3967
3968}; // class pass_ipa_devirt
3969
3970} // anon namespace
3971
3972ipa_opt_pass_d *
3973make_pass_ipa_devirt (gcc::context *ctxt)
3974{
3975 return new pass_ipa_devirt (ctxt);
3976}
3977
eefe9a99 3978#include "gt-ipa-devirt.h"