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