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