Update changelog entry.
[official-gcc.git] / gcc / ipa-devirt.c
blob8c375ff07bd5cd4275239ec2d818828260cb61d1
1 /* Basic IPA utilities for type inheritance graph construction and
2 devirtualization.
3 Copyright (C) 2013-2018 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Brief vocabulary:
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.
39 OTR = OBJ_TYPE_REF
40 This is the Gimple representation of type information of a polymorphic call.
41 It contains two parameters:
42 otr_type is a type of class whose method is called.
43 otr_token is the index into virtual table where address is taken.
45 BINFO
46 This is the type inheritance information attached to each tree
47 RECORD_TYPE by the C++ frontend. It provides information about base
48 types and virtual tables.
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.
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
58 virtual table of the base type. Also BINFO_OFFSET specifies
59 offset of the base within the type.
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
64 BINFO_VTABLE of base binfos (that differs of BINFO_VTABLE of
65 binfo associated to the base type).
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.
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
75 or from DECL_VINDEX of a given virtual table.
77 polymorphic (indirect) call
78 This is callgraph representation of virtual method call. Every
79 polymorphic call contains otr_type and otr_token taken from
80 original OBJ_TYPE_REF at callgraph construction time.
82 What we do here:
84 build_type_inheritance_graph triggers a construction of the type inheritance
85 graph.
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
89 inserted into the graph. Also types without virtual methods are not
90 represented at all, though it may be easy to add this.
92 The inheritance graph is represented as follows:
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)
98 Edges are represented by odr_type->base and odr_type->derived_types.
99 At the moment we do not track offsets of types for multiple inheritance.
100 Adding this is easy.
102 possible_polymorphic_call_targets returns, given an parameters found in
103 indirect polymorphic edge all possible polymorphic call targets of the call.
105 pass_ipa_devirt performs simple speculative devirtualization.
108 #include "config.h"
109 #include "system.h"
110 #include "coretypes.h"
111 #include "backend.h"
112 #include "rtl.h"
113 #include "tree.h"
114 #include "gimple.h"
115 #include "alloc-pool.h"
116 #include "tree-pass.h"
117 #include "cgraph.h"
118 #include "lto-streamer.h"
119 #include "fold-const.h"
120 #include "print-tree.h"
121 #include "calls.h"
122 #include "ipa-utils.h"
123 #include "gimple-fold.h"
124 #include "symbol-summary.h"
125 #include "tree-vrp.h"
126 #include "ipa-prop.h"
127 #include "ipa-fnsummary.h"
128 #include "demangle.h"
129 #include "dbgcnt.h"
130 #include "gimple-pretty-print.h"
131 #include "intl.h"
132 #include "stringpool.h"
133 #include "attribs.h"
135 /* Hash based set of pairs of types. */
136 struct type_pair
138 tree first;
139 tree second;
142 template <>
143 struct default_hash_traits <type_pair>
144 : typed_noop_remove <type_pair>
146 GTY((skip)) typedef type_pair value_type;
147 GTY((skip)) typedef type_pair compare_type;
148 static hashval_t
149 hash (type_pair p)
151 return TYPE_UID (p.first) ^ TYPE_UID (p.second);
153 static bool
154 is_empty (type_pair p)
156 return p.first == NULL;
158 static bool
159 is_deleted (type_pair p ATTRIBUTE_UNUSED)
161 return false;
163 static bool
164 equal (const type_pair &a, const type_pair &b)
166 return a.first==b.first && a.second == b.second;
168 static void
169 mark_empty (type_pair &e)
171 e.first = NULL;
175 static bool odr_types_equivalent_p (tree, tree, bool, bool *,
176 hash_set<type_pair> *,
177 location_t, location_t);
178 static void warn_odr (tree t1, tree t2, tree st1, tree st2,
179 bool warn, bool *warned, const char *reason);
181 static bool odr_violation_reported = false;
184 /* Pointer set of all call targets appearing in the cache. */
185 static hash_set<cgraph_node *> *cached_polymorphic_call_targets;
187 /* The node of type inheritance graph. For each type unique in
188 One Definition Rule (ODR) sense, we produce one node linking all
189 main variants of types equivalent to it, bases and derived types. */
191 struct GTY(()) odr_type_d
193 /* leader type. */
194 tree type;
195 /* All bases; built only for main variants of types. */
196 vec<odr_type> GTY((skip)) bases;
197 /* All derived types with virtual methods seen in unit;
198 built only for main variants of types. */
199 vec<odr_type> GTY((skip)) derived_types;
201 /* All equivalent types, if more than one. */
202 vec<tree, va_gc> *types;
203 /* Set of all equivalent types, if NON-NULL. */
204 hash_set<tree> * GTY((skip)) types_set;
206 /* Unique ID indexing the type in odr_types array. */
207 int id;
208 /* Is it in anonymous namespace? */
209 bool anonymous_namespace;
210 /* Do we know about all derivations of given type? */
211 bool all_derivations_known;
212 /* Did we report ODR violation here? */
213 bool odr_violated;
214 /* Set when virtual table without RTTI previaled table with. */
215 bool rtti_broken;
218 /* Return TRUE if all derived types of T are known and thus
219 we may consider the walk of derived type complete.
221 This is typically true only for final anonymous namespace types and types
222 defined within functions (that may be COMDAT and thus shared across units,
223 but with the same set of derived types). */
225 bool
226 type_all_derivations_known_p (const_tree t)
228 if (TYPE_FINAL_P (t))
229 return true;
230 if (flag_ltrans)
231 return false;
232 /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
233 if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL)
234 return true;
235 if (type_in_anonymous_namespace_p (t))
236 return true;
237 return (decl_function_context (TYPE_NAME (t)) != NULL);
240 /* Return TRUE if type's constructors are all visible. */
242 static bool
243 type_all_ctors_visible_p (tree t)
245 return !flag_ltrans
246 && symtab->state >= CONSTRUCTION
247 /* We can not always use type_all_derivations_known_p.
248 For function local types we must assume case where
249 the function is COMDAT and shared in between units.
251 TODO: These cases are quite easy to get, but we need
252 to keep track of C++ privatizing via -Wno-weak
253 as well as the IPA privatizing. */
254 && type_in_anonymous_namespace_p (t);
257 /* Return TRUE if type may have instance. */
259 static bool
260 type_possibly_instantiated_p (tree t)
262 tree vtable;
263 varpool_node *vnode;
265 /* TODO: Add abstract types here. */
266 if (!type_all_ctors_visible_p (t))
267 return true;
269 vtable = BINFO_VTABLE (TYPE_BINFO (t));
270 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
271 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
272 vnode = varpool_node::get (vtable);
273 return vnode && vnode->definition;
276 /* Hash used to unify ODR types based on their mangled name and for anonymous
277 namespace types. */
279 struct odr_name_hasher : pointer_hash <odr_type_d>
281 typedef union tree_node *compare_type;
282 static inline hashval_t hash (const odr_type_d *);
283 static inline bool equal (const odr_type_d *, const tree_node *);
284 static inline void remove (odr_type_d *);
287 /* Has used to unify ODR types based on their associated virtual table.
288 This hash is needed to keep -fno-lto-odr-type-merging to work and contains
289 only polymorphic types. Types with mangled names are inserted to both. */
291 struct odr_vtable_hasher:odr_name_hasher
293 static inline hashval_t hash (const odr_type_d *);
294 static inline bool equal (const odr_type_d *, const tree_node *);
297 static bool
298 can_be_name_hashed_p (tree t)
300 return (!in_lto_p || odr_type_p (t));
303 /* Hash type by its ODR name. */
305 static hashval_t
306 hash_odr_name (const_tree t)
308 gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
310 /* If not in LTO, all main variants are unique, so we can do
311 pointer hash. */
312 if (!in_lto_p)
313 return htab_hash_pointer (t);
315 /* Anonymous types are unique. */
316 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
317 return htab_hash_pointer (t);
319 gcc_checking_assert (TYPE_NAME (t)
320 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)));
321 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t)));
324 /* Return the computed hashcode for ODR_TYPE. */
326 inline hashval_t
327 odr_name_hasher::hash (const odr_type_d *odr_type)
329 return hash_odr_name (odr_type->type);
332 static bool
333 can_be_vtable_hashed_p (tree t)
335 /* vtable hashing can distinguish only main variants. */
336 if (TYPE_MAIN_VARIANT (t) != t)
337 return false;
338 /* Anonymous namespace types are always handled by name hash. */
339 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
340 return false;
341 return (TREE_CODE (t) == RECORD_TYPE
342 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
345 /* Hash type by assembler name of its vtable. */
347 static hashval_t
348 hash_odr_vtable (const_tree t)
350 tree v = BINFO_VTABLE (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
351 inchash::hash hstate;
353 gcc_checking_assert (in_lto_p);
354 gcc_checking_assert (!type_in_anonymous_namespace_p (t));
355 gcc_checking_assert (TREE_CODE (t) == RECORD_TYPE
356 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
357 gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
359 if (TREE_CODE (v) == POINTER_PLUS_EXPR)
361 add_expr (TREE_OPERAND (v, 1), hstate);
362 v = TREE_OPERAND (TREE_OPERAND (v, 0), 0);
365 hstate.add_hwi (IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (v)));
366 return hstate.end ();
369 /* Return the computed hashcode for ODR_TYPE. */
371 inline hashval_t
372 odr_vtable_hasher::hash (const odr_type_d *odr_type)
374 return hash_odr_vtable (odr_type->type);
377 /* For languages with One Definition Rule, work out if
378 types are the same based on their name.
380 This is non-trivial for LTO where minor differences in
381 the type representation may have prevented type merging
382 to merge two copies of otherwise equivalent type.
384 Until we start streaming mangled type names, this function works
385 only for polymorphic types.
388 bool
389 types_same_for_odr (const_tree type1, const_tree type2)
391 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
393 type1 = TYPE_MAIN_VARIANT (type1);
394 type2 = TYPE_MAIN_VARIANT (type2);
396 if (type1 == type2)
397 return true;
399 if (!in_lto_p)
400 return false;
402 /* Anonymous namespace types are never duplicated. */
403 if ((type_with_linkage_p (type1) && type_in_anonymous_namespace_p (type1))
404 || (type_with_linkage_p (type2) && type_in_anonymous_namespace_p (type2)))
405 return false;
408 /* ODR name of the type is set in DECL_ASSEMBLER_NAME of its TYPE_NAME.
410 Ideally we should never need types without ODR names here. It can however
411 happen in two cases:
413 1) for builtin types that are not streamed but rebuilt in lto/lto-lang.c
414 Here testing for equivalence is safe, since their MAIN_VARIANTs are
415 unique.
416 2) for units streamed with -fno-lto-odr-type-merging. Here we can't
417 establish precise ODR equivalency, but for correctness we care only
418 about equivalency on complete polymorphic types. For these we can
419 compare assembler names of their virtual tables. */
420 if ((!TYPE_NAME (type1) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type1)))
421 || (!TYPE_NAME (type2) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type2))))
423 /* See if types are obviously different (i.e. different codes
424 or polymorphic wrt non-polymorphic). This is not strictly correct
425 for ODR violating programs, but we can't do better without streaming
426 ODR names. */
427 if (TREE_CODE (type1) != TREE_CODE (type2))
428 return false;
429 if (TREE_CODE (type1) == RECORD_TYPE
430 && (TYPE_BINFO (type1) == NULL_TREE)
431 != (TYPE_BINFO (type2) == NULL_TREE))
432 return false;
433 if (TREE_CODE (type1) == RECORD_TYPE && TYPE_BINFO (type1)
434 && (BINFO_VTABLE (TYPE_BINFO (type1)) == NULL_TREE)
435 != (BINFO_VTABLE (TYPE_BINFO (type2)) == NULL_TREE))
436 return false;
438 /* At the moment we have no way to establish ODR equivalence at LTO
439 other than comparing virtual table pointers of polymorphic types.
440 Eventually we should start saving mangled names in TYPE_NAME.
441 Then this condition will become non-trivial. */
443 if (TREE_CODE (type1) == RECORD_TYPE
444 && TYPE_BINFO (type1) && TYPE_BINFO (type2)
445 && BINFO_VTABLE (TYPE_BINFO (type1))
446 && BINFO_VTABLE (TYPE_BINFO (type2)))
448 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
449 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
450 gcc_assert (TREE_CODE (v1) == POINTER_PLUS_EXPR
451 && TREE_CODE (v2) == POINTER_PLUS_EXPR);
452 return (operand_equal_p (TREE_OPERAND (v1, 1),
453 TREE_OPERAND (v2, 1), 0)
454 && DECL_ASSEMBLER_NAME
455 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
456 == DECL_ASSEMBLER_NAME
457 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
459 gcc_unreachable ();
461 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1))
462 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2)));
465 /* Return true if we can decide on ODR equivalency.
467 In non-LTO it is always decide, in LTO however it depends in the type has
468 ODR info attached. */
470 bool
471 types_odr_comparable (tree t1, tree t2)
473 return (!in_lto_p
474 || TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)
475 || (odr_type_p (TYPE_MAIN_VARIANT (t1))
476 && odr_type_p (TYPE_MAIN_VARIANT (t2)))
477 || (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE
478 && TYPE_BINFO (t1) && TYPE_BINFO (t2)
479 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
480 && polymorphic_type_binfo_p (TYPE_BINFO (t2))));
483 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
484 known, be conservative and return false. */
486 bool
487 types_must_be_same_for_odr (tree t1, tree t2)
489 if (types_odr_comparable (t1, t2))
490 return types_same_for_odr (t1, t2);
491 else
492 return TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2);
495 /* If T is compound type, return type it is based on. */
497 static tree
498 compound_type_base (const_tree t)
500 if (TREE_CODE (t) == ARRAY_TYPE
501 || POINTER_TYPE_P (t)
502 || TREE_CODE (t) == COMPLEX_TYPE
503 || VECTOR_TYPE_P (t))
504 return TREE_TYPE (t);
505 if (TREE_CODE (t) == METHOD_TYPE)
506 return TYPE_METHOD_BASETYPE (t);
507 if (TREE_CODE (t) == OFFSET_TYPE)
508 return TYPE_OFFSET_BASETYPE (t);
509 return NULL_TREE;
512 /* Return true if T is either ODR type or compound type based from it.
513 If the function return true, we know that T is a type originating from C++
514 source even at link-time. */
516 bool
517 odr_or_derived_type_p (const_tree t)
521 if (odr_type_p (TYPE_MAIN_VARIANT (t)))
522 return true;
523 /* Function type is a tricky one. Basically we can consider it
524 ODR derived if return type or any of the parameters is.
525 We need to check all parameters because LTO streaming merges
526 common types (such as void) and they are not considered ODR then. */
527 if (TREE_CODE (t) == FUNCTION_TYPE)
529 if (TYPE_METHOD_BASETYPE (t))
530 t = TYPE_METHOD_BASETYPE (t);
531 else
533 if (TREE_TYPE (t) && odr_or_derived_type_p (TREE_TYPE (t)))
534 return true;
535 for (t = TYPE_ARG_TYPES (t); t; t = TREE_CHAIN (t))
536 if (odr_or_derived_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (t))))
537 return true;
538 return false;
541 else
542 t = compound_type_base (t);
544 while (t);
545 return t;
548 /* Compare types T1 and T2 and return true if they are
549 equivalent. */
551 inline bool
552 odr_name_hasher::equal (const odr_type_d *o1, const tree_node *t2)
554 tree t1 = o1->type;
556 gcc_checking_assert (TYPE_MAIN_VARIANT (t2) == t2);
557 gcc_checking_assert (TYPE_MAIN_VARIANT (t1) == t1);
558 if (t1 == t2)
559 return true;
560 if (!in_lto_p)
561 return false;
562 /* Check for anonymous namespaces. */
563 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
564 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
565 return false;
566 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1)));
567 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
568 return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
569 == DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
572 /* Compare types T1 and T2 and return true if they are
573 equivalent. */
575 inline bool
576 odr_vtable_hasher::equal (const odr_type_d *o1, const tree_node *t2)
578 tree t1 = o1->type;
580 gcc_checking_assert (TYPE_MAIN_VARIANT (t2) == t2);
581 gcc_checking_assert (TYPE_MAIN_VARIANT (t1) == t1);
582 gcc_checking_assert (in_lto_p);
583 t1 = TYPE_MAIN_VARIANT (t1);
584 t2 = TYPE_MAIN_VARIANT (t2);
585 if (t1 == t2)
586 return true;
587 tree v1 = BINFO_VTABLE (TYPE_BINFO (t1));
588 tree v2 = BINFO_VTABLE (TYPE_BINFO (t2));
589 return (operand_equal_p (TREE_OPERAND (v1, 1),
590 TREE_OPERAND (v2, 1), 0)
591 && DECL_ASSEMBLER_NAME
592 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
593 == DECL_ASSEMBLER_NAME
594 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
597 /* Free ODR type V. */
599 inline void
600 odr_name_hasher::remove (odr_type_d *v)
602 v->bases.release ();
603 v->derived_types.release ();
604 if (v->types_set)
605 delete v->types_set;
606 ggc_free (v);
609 /* ODR type hash used to look up ODR type based on tree type node. */
611 typedef hash_table<odr_name_hasher> odr_hash_type;
612 static odr_hash_type *odr_hash;
613 typedef hash_table<odr_vtable_hasher> odr_vtable_hash_type;
614 static odr_vtable_hash_type *odr_vtable_hash;
616 /* ODR types are also stored into ODR_TYPE vector to allow consistent
617 walking. Bases appear before derived types. Vector is garbage collected
618 so we won't end up visiting empty types. */
620 static GTY(()) vec <odr_type, va_gc> *odr_types_ptr;
621 #define odr_types (*odr_types_ptr)
623 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
624 void
625 set_type_binfo (tree type, tree binfo)
627 for (; type; type = TYPE_NEXT_VARIANT (type))
628 if (COMPLETE_TYPE_P (type))
629 TYPE_BINFO (type) = binfo;
630 else
631 gcc_assert (!TYPE_BINFO (type));
634 /* Return true if type variants match.
635 This assumes that we already verified that T1 and T2 are variants of the
636 same type. */
638 static bool
639 type_variants_equivalent_p (tree t1, tree t2, bool warn, bool *warned)
641 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
643 warn_odr (t1, t2, NULL, NULL, warn, warned,
644 G_("a type with different qualifiers is defined in another "
645 "translation unit"));
646 return false;
649 if (comp_type_attributes (t1, t2) != 1)
651 warn_odr (t1, t2, NULL, NULL, warn, warned,
652 G_("a type with different attributes "
653 "is defined in another translation unit"));
654 return false;
657 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
658 && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
660 warn_odr (t1, t2, NULL, NULL, warn, warned,
661 G_("a type with different alignment "
662 "is defined in another translation unit"));
663 return false;
666 return true;
669 /* Compare T1 and T2 based on name or structure. */
671 static bool
672 odr_subtypes_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
673 hash_set<type_pair> *visited,
674 location_t loc1, location_t loc2)
677 /* This can happen in incomplete types that should be handled earlier. */
678 gcc_assert (t1 && t2);
680 if (t1 == t2)
681 return true;
683 /* Anonymous namespace types must match exactly. */
684 if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1))
685 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
686 || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2))
687 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2))))
688 return false;
690 /* For ODR types be sure to compare their names.
691 To support -Wno-odr-type-merging we allow one type to be non-ODR
692 and other ODR even though it is a violation. */
693 if (types_odr_comparable (t1, t2))
695 if (t1 != t2
696 && odr_type_p (TYPE_MAIN_VARIANT (t1))
697 && get_odr_type (TYPE_MAIN_VARIANT (t1), true)->odr_violated)
698 return false;
699 if (!types_same_for_odr (t1, t2))
700 return false;
701 if (!type_variants_equivalent_p (t1, t2, warn, warned))
702 return false;
703 /* Limit recursion: If subtypes are ODR types and we know
704 that they are same, be happy. */
705 if (odr_type_p (TYPE_MAIN_VARIANT (t1)))
706 return true;
709 /* Component types, builtins and possibly violating ODR types
710 have to be compared structurally. */
711 if (TREE_CODE (t1) != TREE_CODE (t2))
712 return false;
713 if (AGGREGATE_TYPE_P (t1)
714 && (TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
715 return false;
717 type_pair pair={TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2)};
718 if (TYPE_UID (TYPE_MAIN_VARIANT (t1)) > TYPE_UID (TYPE_MAIN_VARIANT (t2)))
720 pair.first = TYPE_MAIN_VARIANT (t2);
721 pair.second = TYPE_MAIN_VARIANT (t1);
723 if (visited->add (pair))
724 return true;
725 if (!odr_types_equivalent_p (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2),
726 false, NULL, visited, loc1, loc2))
727 return false;
728 if (!type_variants_equivalent_p (t1, t2, warn, warned))
729 return false;
730 return true;
733 /* Return true if DECL1 and DECL2 are identical methods. Consider
734 name equivalent to name.localalias.xyz. */
736 static bool
737 methods_equal_p (tree decl1, tree decl2)
739 if (DECL_ASSEMBLER_NAME (decl1) == DECL_ASSEMBLER_NAME (decl2))
740 return true;
741 const char sep = symbol_table::symbol_suffix_separator ();
743 const char *name1 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl1));
744 const char *ptr1 = strchr (name1, sep);
745 int len1 = ptr1 ? ptr1 - name1 : strlen (name1);
747 const char *name2 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl2));
748 const char *ptr2 = strchr (name2, sep);
749 int len2 = ptr2 ? ptr2 - name2 : strlen (name2);
751 if (len1 != len2)
752 return false;
753 return !strncmp (name1, name2, len1);
756 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
757 violation warnings. */
759 void
760 compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
762 int n1, n2;
764 if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl))
766 odr_violation_reported = true;
767 if (DECL_VIRTUAL_P (prevailing->decl))
769 varpool_node *tmp = prevailing;
770 prevailing = vtable;
771 vtable = tmp;
773 auto_diagnostic_group d;
774 if (warning_at (DECL_SOURCE_LOCATION
775 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
776 OPT_Wodr,
777 "virtual table of type %qD violates one definition rule",
778 DECL_CONTEXT (vtable->decl)))
779 inform (DECL_SOURCE_LOCATION (prevailing->decl),
780 "variable of same assembler name as the virtual table is "
781 "defined in another translation unit");
782 return;
784 if (!prevailing->definition || !vtable->definition)
785 return;
787 /* If we do not stream ODR type info, do not bother to do useful compare. */
788 if (!TYPE_BINFO (DECL_CONTEXT (vtable->decl))
789 || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable->decl))))
790 return;
792 odr_type class_type = get_odr_type (DECL_CONTEXT (vtable->decl), true);
794 if (class_type->odr_violated)
795 return;
797 for (n1 = 0, n2 = 0; true; n1++, n2++)
799 struct ipa_ref *ref1, *ref2;
800 bool end1, end2;
802 end1 = !prevailing->iterate_reference (n1, ref1);
803 end2 = !vtable->iterate_reference (n2, ref2);
805 /* !DECL_VIRTUAL_P means RTTI entry;
806 We warn when RTTI is lost because non-RTTI previals; we silently
807 accept the other case. */
808 while (!end2
809 && (end1
810 || (methods_equal_p (ref1->referred->decl,
811 ref2->referred->decl)
812 && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL))
813 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
815 if (!class_type->rtti_broken)
817 auto_diagnostic_group d;
818 if (warning_at (DECL_SOURCE_LOCATION
819 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
820 OPT_Wodr,
821 "virtual table of type %qD contains RTTI "
822 "information",
823 DECL_CONTEXT (vtable->decl)))
825 inform (DECL_SOURCE_LOCATION
826 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
827 "but is prevailed by one without from other"
828 " translation unit");
829 inform (DECL_SOURCE_LOCATION
830 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
831 "RTTI will not work on this type");
832 class_type->rtti_broken = true;
835 n2++;
836 end2 = !vtable->iterate_reference (n2, ref2);
838 while (!end1
839 && (end2
840 || (methods_equal_p (ref2->referred->decl, ref1->referred->decl)
841 && TREE_CODE (ref2->referred->decl) == FUNCTION_DECL))
842 && TREE_CODE (ref1->referred->decl) != FUNCTION_DECL)
844 n1++;
845 end1 = !prevailing->iterate_reference (n1, ref1);
848 /* Finished? */
849 if (end1 && end2)
851 /* Extra paranoia; compare the sizes. We do not have information
852 about virtual inheritance offsets, so just be sure that these
853 match.
854 Do this as very last check so the not very informative error
855 is not output too often. */
856 if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl))
858 class_type->odr_violated = true;
859 auto_diagnostic_group d;
860 if (warning_at (DECL_SOURCE_LOCATION
861 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
862 OPT_Wodr,
863 "virtual table of type %qD violates "
864 "one definition rule ",
865 DECL_CONTEXT (vtable->decl)))
867 inform (DECL_SOURCE_LOCATION
868 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
869 "the conflicting type defined in another translation "
870 "unit has virtual table of different size");
873 return;
876 if (!end1 && !end2)
878 if (methods_equal_p (ref1->referred->decl, ref2->referred->decl))
879 continue;
881 class_type->odr_violated = true;
883 /* If the loops above stopped on non-virtual pointer, we have
884 mismatch in RTTI information mangling. */
885 if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL
886 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
888 auto_diagnostic_group d;
889 if (warning_at (DECL_SOURCE_LOCATION
890 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
891 OPT_Wodr,
892 "virtual table of type %qD violates "
893 "one definition rule ",
894 DECL_CONTEXT (vtable->decl)))
896 inform (DECL_SOURCE_LOCATION
897 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
898 "the conflicting type defined in another translation "
899 "unit with different RTTI information");
901 return;
903 /* At this point both REF1 and REF2 points either to virtual table
904 or virtual method. If one points to virtual table and other to
905 method we can complain the same way as if one table was shorter
906 than other pointing out the extra method. */
907 if (TREE_CODE (ref1->referred->decl)
908 != TREE_CODE (ref2->referred->decl))
910 if (VAR_P (ref1->referred->decl))
911 end1 = true;
912 else if (VAR_P (ref2->referred->decl))
913 end2 = true;
917 class_type->odr_violated = true;
919 /* Complain about size mismatch. Either we have too many virutal
920 functions or too many virtual table pointers. */
921 if (end1 || end2)
923 if (end1)
925 varpool_node *tmp = prevailing;
926 prevailing = vtable;
927 vtable = tmp;
928 ref1 = ref2;
930 auto_diagnostic_group d;
931 if (warning_at (DECL_SOURCE_LOCATION
932 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
933 OPT_Wodr,
934 "virtual table of type %qD violates "
935 "one definition rule",
936 DECL_CONTEXT (vtable->decl)))
938 if (TREE_CODE (ref1->referring->decl) == FUNCTION_DECL)
940 inform (DECL_SOURCE_LOCATION
941 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
942 "the conflicting type defined in another translation "
943 "unit");
944 inform (DECL_SOURCE_LOCATION
945 (TYPE_NAME (DECL_CONTEXT (ref1->referring->decl))),
946 "contains additional virtual method %qD",
947 ref1->referred->decl);
949 else
951 inform (DECL_SOURCE_LOCATION
952 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
953 "the conflicting type defined in another translation "
954 "unit has virtual table with more entries");
957 return;
960 /* And in the last case we have either mistmatch in between two virtual
961 methods or two virtual table pointers. */
962 auto_diagnostic_group d;
963 if (warning_at (DECL_SOURCE_LOCATION
964 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr,
965 "virtual table of type %qD violates "
966 "one definition rule ",
967 DECL_CONTEXT (vtable->decl)))
969 if (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL)
971 inform (DECL_SOURCE_LOCATION
972 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
973 "the conflicting type defined in another translation "
974 "unit");
975 gcc_assert (TREE_CODE (ref2->referred->decl)
976 == FUNCTION_DECL);
977 inform (DECL_SOURCE_LOCATION
978 (ref1->referred->ultimate_alias_target ()->decl),
979 "virtual method %qD",
980 ref1->referred->ultimate_alias_target ()->decl);
981 inform (DECL_SOURCE_LOCATION
982 (ref2->referred->ultimate_alias_target ()->decl),
983 "ought to match virtual method %qD but does not",
984 ref2->referred->ultimate_alias_target ()->decl);
986 else
987 inform (DECL_SOURCE_LOCATION
988 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
989 "the conflicting type defined in another translation "
990 "unit has virtual table with different contents");
991 return;
996 /* Output ODR violation warning about T1 and T2 with REASON.
997 Display location of ST1 and ST2 if REASON speaks about field or
998 method of the type.
999 If WARN is false, do nothing. Set WARNED if warning was indeed
1000 output. */
1002 static void
1003 warn_odr (tree t1, tree t2, tree st1, tree st2,
1004 bool warn, bool *warned, const char *reason)
1006 tree decl2 = TYPE_NAME (TYPE_MAIN_VARIANT (t2));
1007 if (warned)
1008 *warned = false;
1010 if (!warn || !TYPE_NAME(TYPE_MAIN_VARIANT (t1)))
1011 return;
1013 /* ODR warnings are output druing LTO streaming; we must apply location
1014 cache for potential warnings to be output correctly. */
1015 if (lto_location_cache::current_cache)
1016 lto_location_cache::current_cache->apply_location_cache ();
1018 auto_diagnostic_group d;
1019 if (t1 != TYPE_MAIN_VARIANT (t1)
1020 && TYPE_NAME (t1) != DECL_NAME (TYPE_MAIN_VARIANT (t1)))
1022 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1))),
1023 OPT_Wodr, "type %qT (typedef of %qT) violates the "
1024 "C++ One Definition Rule",
1025 t1, TYPE_MAIN_VARIANT (t1)))
1026 return;
1028 else
1030 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1))),
1031 OPT_Wodr, "type %qT violates the C++ One Definition Rule",
1032 t1))
1033 return;
1035 if (!st1 && !st2)
1037 /* For FIELD_DECL support also case where one of fields is
1038 NULL - this is used when the structures have mismatching number of
1039 elements. */
1040 else if (!st1 || TREE_CODE (st1) == FIELD_DECL)
1042 inform (DECL_SOURCE_LOCATION (decl2),
1043 "a different type is defined in another translation unit");
1044 if (!st1)
1046 st1 = st2;
1047 st2 = NULL;
1049 inform (DECL_SOURCE_LOCATION (st1),
1050 "the first difference of corresponding definitions is field %qD",
1051 st1);
1052 if (st2)
1053 decl2 = st2;
1055 else if (TREE_CODE (st1) == FUNCTION_DECL)
1057 inform (DECL_SOURCE_LOCATION (decl2),
1058 "a different type is defined in another translation unit");
1059 inform (DECL_SOURCE_LOCATION (st1),
1060 "the first difference of corresponding definitions is method %qD",
1061 st1);
1062 decl2 = st2;
1064 else
1065 return;
1066 inform (DECL_SOURCE_LOCATION (decl2), reason);
1068 if (warned)
1069 *warned = true;
1072 /* Return ture if T1 and T2 are incompatible and we want to recusively
1073 dive into them from warn_type_mismatch to give sensible answer. */
1075 static bool
1076 type_mismatch_p (tree t1, tree t2)
1078 if (odr_or_derived_type_p (t1) && odr_or_derived_type_p (t2)
1079 && !odr_types_equivalent_p (t1, t2))
1080 return true;
1081 return !types_compatible_p (t1, t2);
1085 /* Types T1 and T2 was found to be incompatible in a context they can't
1086 (either used to declare a symbol of same assembler name or unified by
1087 ODR rule). We already output warning about this, but if possible, output
1088 extra information on how the types mismatch.
1090 This is hard to do in general. We basically handle the common cases.
1092 If LOC1 and LOC2 are meaningful locations, use it in the case the types
1093 themselves do no thave one.*/
1095 void
1096 warn_types_mismatch (tree t1, tree t2, location_t loc1, location_t loc2)
1098 /* Location of type is known only if it has TYPE_NAME and the name is
1099 TYPE_DECL. */
1100 location_t loc_t1 = TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1101 ? DECL_SOURCE_LOCATION (TYPE_NAME (t1))
1102 : UNKNOWN_LOCATION;
1103 location_t loc_t2 = TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1104 ? DECL_SOURCE_LOCATION (TYPE_NAME (t2))
1105 : UNKNOWN_LOCATION;
1106 bool loc_t2_useful = false;
1108 /* With LTO it is a common case that the location of both types match.
1109 See if T2 has a location that is different from T1. If so, we will
1110 inform user about the location.
1111 Do not consider the location passed to us in LOC1/LOC2 as those are
1112 already output. */
1113 if (loc_t2 > BUILTINS_LOCATION && loc_t2 != loc_t1)
1115 if (loc_t1 <= BUILTINS_LOCATION)
1116 loc_t2_useful = true;
1117 else
1119 expanded_location xloc1 = expand_location (loc_t1);
1120 expanded_location xloc2 = expand_location (loc_t2);
1122 if (strcmp (xloc1.file, xloc2.file)
1123 || xloc1.line != xloc2.line
1124 || xloc1.column != xloc2.column)
1125 loc_t2_useful = true;
1129 if (loc_t1 <= BUILTINS_LOCATION)
1130 loc_t1 = loc1;
1131 if (loc_t2 <= BUILTINS_LOCATION)
1132 loc_t2 = loc2;
1134 location_t loc = loc_t1 <= BUILTINS_LOCATION ? loc_t2 : loc_t1;
1136 /* It is a quite common bug to reference anonymous namespace type in
1137 non-anonymous namespace class. */
1138 if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1))
1139 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
1140 || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2))
1141 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2))))
1143 if (type_with_linkage_p (TYPE_MAIN_VARIANT (t1))
1144 && !type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
1146 std::swap (t1, t2);
1147 std::swap (loc_t1, loc_t2);
1149 gcc_assert (TYPE_NAME (t1) && TYPE_NAME (t2)
1150 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1151 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL);
1152 tree n1 = TYPE_NAME (t1);
1153 tree n2 = TYPE_NAME (t2);
1154 if (TREE_CODE (n1) == TYPE_DECL)
1155 n1 = DECL_NAME (n1);
1156 if (TREE_CODE (n2) == TYPE_DECL)
1157 n2 = DECL_NAME (n2);
1158 /* Most of the time, the type names will match, do not be unnecesarily
1159 verbose. */
1160 if (IDENTIFIER_POINTER (n1) != IDENTIFIER_POINTER (n2))
1161 inform (loc_t1,
1162 "type %qT defined in anonymous namespace can not match "
1163 "type %qT across the translation unit boundary",
1164 t1, t2);
1165 else
1166 inform (loc_t1,
1167 "type %qT defined in anonymous namespace can not match "
1168 "across the translation unit boundary",
1169 t1);
1170 if (loc_t2_useful)
1171 inform (loc_t2,
1172 "the incompatible type defined in another translation unit");
1173 return;
1175 tree mt1 = TYPE_MAIN_VARIANT (t1);
1176 tree mt2 = TYPE_MAIN_VARIANT (t2);
1177 /* If types have mangled ODR names and they are different, it is most
1178 informative to output those.
1179 This also covers types defined in different namespaces. */
1180 if (TYPE_NAME (mt1) && TYPE_NAME (mt2)
1181 && TREE_CODE (TYPE_NAME (mt1)) == TYPE_DECL
1182 && TREE_CODE (TYPE_NAME (mt2)) == TYPE_DECL
1183 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (mt1))
1184 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (mt2))
1185 && DECL_ASSEMBLER_NAME (TYPE_NAME (mt1))
1186 != DECL_ASSEMBLER_NAME (TYPE_NAME (mt2)))
1188 char *name1 = xstrdup (cplus_demangle
1189 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (mt1))),
1190 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES));
1191 char *name2 = cplus_demangle
1192 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (mt2))),
1193 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES);
1194 if (name1 && name2 && strcmp (name1, name2))
1196 inform (loc_t1,
1197 "type name %qs should match type name %qs",
1198 name1, name2);
1199 if (loc_t2_useful)
1200 inform (loc_t2,
1201 "the incompatible type is defined here");
1202 free (name1);
1203 return;
1205 free (name1);
1207 /* A tricky case are compound types. Often they appear the same in source
1208 code and the mismatch is dragged in by type they are build from.
1209 Look for those differences in subtypes and try to be informative. In other
1210 cases just output nothing because the source code is probably different
1211 and in this case we already output a all necessary info. */
1212 if (!TYPE_NAME (t1) || !TYPE_NAME (t2))
1214 if (TREE_CODE (t1) == TREE_CODE (t2))
1216 if (TREE_CODE (t1) == ARRAY_TYPE
1217 && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1219 tree i1 = TYPE_DOMAIN (t1);
1220 tree i2 = TYPE_DOMAIN (t2);
1222 if (i1 && i2
1223 && TYPE_MAX_VALUE (i1)
1224 && TYPE_MAX_VALUE (i2)
1225 && !operand_equal_p (TYPE_MAX_VALUE (i1),
1226 TYPE_MAX_VALUE (i2), 0))
1228 inform (loc,
1229 "array types have different bounds");
1230 return;
1233 if ((POINTER_TYPE_P (t1) || TREE_CODE (t1) == ARRAY_TYPE)
1234 && type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1235 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1, loc_t2);
1236 else if (TREE_CODE (t1) == METHOD_TYPE
1237 || TREE_CODE (t1) == FUNCTION_TYPE)
1239 tree parms1 = NULL, parms2 = NULL;
1240 int count = 1;
1242 if (type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1244 inform (loc, "return value type mismatch");
1245 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1,
1246 loc_t2);
1247 return;
1249 if (prototype_p (t1) && prototype_p (t2))
1250 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1251 parms1 && parms2;
1252 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2),
1253 count++)
1255 if (type_mismatch_p (TREE_VALUE (parms1), TREE_VALUE (parms2)))
1257 if (count == 1 && TREE_CODE (t1) == METHOD_TYPE)
1258 inform (loc,
1259 "implicit this pointer type mismatch");
1260 else
1261 inform (loc,
1262 "type mismatch in parameter %i",
1263 count - (TREE_CODE (t1) == METHOD_TYPE));
1264 warn_types_mismatch (TREE_VALUE (parms1),
1265 TREE_VALUE (parms2),
1266 loc_t1, loc_t2);
1267 return;
1270 if (parms1 || parms2)
1272 inform (loc,
1273 "types have different parameter counts");
1274 return;
1278 return;
1281 if (types_odr_comparable (t1, t2)
1282 && types_same_for_odr (t1, t2))
1283 inform (loc_t1,
1284 "type %qT itself violates the C++ One Definition Rule", t1);
1285 /* Prevent pointless warnings like "struct aa" should match "struct aa". */
1286 else if (TYPE_NAME (t1) == TYPE_NAME (t2)
1287 && TREE_CODE (t1) == TREE_CODE (t2) && !loc_t2_useful)
1288 return;
1289 else
1290 inform (loc_t1, "type %qT should match type %qT",
1291 t1, t2);
1292 if (loc_t2_useful)
1293 inform (loc_t2, "the incompatible type is defined here");
1296 /* Compare T1 and T2, report ODR violations if WARN is true and set
1297 WARNED to true if anything is reported. Return true if types match.
1298 If true is returned, the types are also compatible in the sense of
1299 gimple_canonical_types_compatible_p.
1300 If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning
1301 about the type if the type itself do not have location. */
1303 static bool
1304 odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
1305 hash_set<type_pair> *visited,
1306 location_t loc1, location_t loc2)
1308 /* Check first for the obvious case of pointer identity. */
1309 if (t1 == t2)
1310 return true;
1312 /* Can't be the same type if the types don't have the same code. */
1313 if (TREE_CODE (t1) != TREE_CODE (t2))
1315 warn_odr (t1, t2, NULL, NULL, warn, warned,
1316 G_("a different type is defined in another translation unit"));
1317 return false;
1320 if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1))
1321 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
1322 || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2))
1323 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2))))
1325 /* We can not trip this when comparing ODR types, only when trying to
1326 match different ODR derivations from different declarations.
1327 So WARN should be always false. */
1328 gcc_assert (!warn);
1329 return false;
1332 if (TREE_CODE (t1) == ENUMERAL_TYPE
1333 && TYPE_VALUES (t1) && TYPE_VALUES (t2))
1335 tree v1, v2;
1336 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
1337 v1 && v2 ; v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
1339 if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
1341 warn_odr (t1, t2, NULL, NULL, warn, warned,
1342 G_("an enum with different value name"
1343 " is defined in another translation unit"));
1344 return false;
1346 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2), 0))
1348 warn_odr (t1, t2, NULL, NULL, warn, warned,
1349 G_("an enum with different values is defined"
1350 " in another translation unit"));
1351 return false;
1354 if (v1 || v2)
1356 warn_odr (t1, t2, NULL, NULL, warn, warned,
1357 G_("an enum with mismatching number of values "
1358 "is defined in another translation unit"));
1359 return false;
1363 /* Non-aggregate types can be handled cheaply. */
1364 if (INTEGRAL_TYPE_P (t1)
1365 || SCALAR_FLOAT_TYPE_P (t1)
1366 || FIXED_POINT_TYPE_P (t1)
1367 || TREE_CODE (t1) == VECTOR_TYPE
1368 || TREE_CODE (t1) == COMPLEX_TYPE
1369 || TREE_CODE (t1) == OFFSET_TYPE
1370 || POINTER_TYPE_P (t1))
1372 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
1374 warn_odr (t1, t2, NULL, NULL, warn, warned,
1375 G_("a type with different precision is defined "
1376 "in another translation unit"));
1377 return false;
1379 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
1381 warn_odr (t1, t2, NULL, NULL, warn, warned,
1382 G_("a type with different signedness is defined "
1383 "in another translation unit"));
1384 return false;
1387 if (TREE_CODE (t1) == INTEGER_TYPE
1388 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
1390 /* char WRT uint_8? */
1391 warn_odr (t1, t2, NULL, NULL, warn, warned,
1392 G_("a different type is defined in another "
1393 "translation unit"));
1394 return false;
1397 /* For canonical type comparisons we do not want to build SCCs
1398 so we cannot compare pointed-to types. But we can, for now,
1399 require the same pointed-to type kind and match what
1400 useless_type_conversion_p would do. */
1401 if (POINTER_TYPE_P (t1))
1403 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
1404 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
1406 warn_odr (t1, t2, NULL, NULL, warn, warned,
1407 G_("it is defined as a pointer in different address "
1408 "space in another translation unit"));
1409 return false;
1412 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1413 warn, warned, visited, loc1, loc2))
1415 warn_odr (t1, t2, NULL, NULL, warn, warned,
1416 G_("it is defined as a pointer to different type "
1417 "in another translation unit"));
1418 if (warn && warned)
1419 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2),
1420 loc1, loc2);
1421 return false;
1425 if ((TREE_CODE (t1) == VECTOR_TYPE || TREE_CODE (t1) == COMPLEX_TYPE)
1426 && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1427 warn, warned,
1428 visited, loc1, loc2))
1430 /* Probably specific enough. */
1431 warn_odr (t1, t2, NULL, NULL, warn, warned,
1432 G_("a different type is defined "
1433 "in another translation unit"));
1434 if (warn && warned)
1435 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1436 return false;
1439 /* Do type-specific comparisons. */
1440 else switch (TREE_CODE (t1))
1442 case ARRAY_TYPE:
1444 /* Array types are the same if the element types are the same and
1445 the number of elements are the same. */
1446 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1447 warn, warned, visited, loc1, loc2))
1449 warn_odr (t1, t2, NULL, NULL, warn, warned,
1450 G_("a different type is defined in another "
1451 "translation unit"));
1452 if (warn && warned)
1453 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1455 gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
1456 gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
1457 == TYPE_NONALIASED_COMPONENT (t2));
1459 tree i1 = TYPE_DOMAIN (t1);
1460 tree i2 = TYPE_DOMAIN (t2);
1462 /* For an incomplete external array, the type domain can be
1463 NULL_TREE. Check this condition also. */
1464 if (i1 == NULL_TREE || i2 == NULL_TREE)
1465 return type_variants_equivalent_p (t1, t2, warn, warned);
1467 tree min1 = TYPE_MIN_VALUE (i1);
1468 tree min2 = TYPE_MIN_VALUE (i2);
1469 tree max1 = TYPE_MAX_VALUE (i1);
1470 tree max2 = TYPE_MAX_VALUE (i2);
1472 /* In C++, minimums should be always 0. */
1473 gcc_assert (min1 == min2);
1474 if (!operand_equal_p (max1, max2, 0))
1476 warn_odr (t1, t2, NULL, NULL, warn, warned,
1477 G_("an array of different size is defined "
1478 "in another translation unit"));
1479 return false;
1482 break;
1484 case METHOD_TYPE:
1485 case FUNCTION_TYPE:
1486 /* Function types are the same if the return type and arguments types
1487 are the same. */
1488 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1489 warn, warned, visited, loc1, loc2))
1491 warn_odr (t1, t2, NULL, NULL, warn, warned,
1492 G_("has different return value "
1493 "in another translation unit"));
1494 if (warn && warned)
1495 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1496 return false;
1499 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2)
1500 || !prototype_p (t1) || !prototype_p (t2))
1501 return type_variants_equivalent_p (t1, t2, warn, warned);
1502 else
1504 tree parms1, parms2;
1506 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1507 parms1 && parms2;
1508 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
1510 if (!odr_subtypes_equivalent_p
1511 (TREE_VALUE (parms1), TREE_VALUE (parms2), warn, warned,
1512 visited, loc1, loc2))
1514 warn_odr (t1, t2, NULL, NULL, warn, warned,
1515 G_("has different parameters in another "
1516 "translation unit"));
1517 if (warn && warned)
1518 warn_types_mismatch (TREE_VALUE (parms1),
1519 TREE_VALUE (parms2), loc1, loc2);
1520 return false;
1524 if (parms1 || parms2)
1526 warn_odr (t1, t2, NULL, NULL, warn, warned,
1527 G_("has different parameters "
1528 "in another translation unit"));
1529 return false;
1532 return type_variants_equivalent_p (t1, t2, warn, warned);
1535 case RECORD_TYPE:
1536 case UNION_TYPE:
1537 case QUAL_UNION_TYPE:
1539 tree f1, f2;
1541 /* For aggregate types, all the fields must be the same. */
1542 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1544 if (TYPE_BINFO (t1) && TYPE_BINFO (t2)
1545 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
1546 != polymorphic_type_binfo_p (TYPE_BINFO (t2)))
1548 if (polymorphic_type_binfo_p (TYPE_BINFO (t1)))
1549 warn_odr (t1, t2, NULL, NULL, warn, warned,
1550 G_("a type defined in another translation unit "
1551 "is not polymorphic"));
1552 else
1553 warn_odr (t1, t2, NULL, NULL, warn, warned,
1554 G_("a type defined in another translation unit "
1555 "is polymorphic"));
1556 return false;
1558 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1559 f1 || f2;
1560 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1562 /* Skip non-fields. */
1563 while (f1 && TREE_CODE (f1) != FIELD_DECL)
1564 f1 = TREE_CHAIN (f1);
1565 while (f2 && TREE_CODE (f2) != FIELD_DECL)
1566 f2 = TREE_CHAIN (f2);
1567 if (!f1 || !f2)
1568 break;
1569 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1571 warn_odr (t1, t2, NULL, NULL, warn, warned,
1572 G_("a type with different virtual table pointers"
1573 " is defined in another translation unit"));
1574 return false;
1576 if (DECL_ARTIFICIAL (f1) != DECL_ARTIFICIAL (f2))
1578 warn_odr (t1, t2, NULL, NULL, warn, warned,
1579 G_("a type with different bases is defined "
1580 "in another translation unit"));
1581 return false;
1583 if (DECL_NAME (f1) != DECL_NAME (f2)
1584 && !DECL_ARTIFICIAL (f1))
1586 warn_odr (t1, t2, f1, f2, warn, warned,
1587 G_("a field with different name is defined "
1588 "in another translation unit"));
1589 return false;
1591 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1),
1592 TREE_TYPE (f2), warn, warned,
1593 visited, loc1, loc2))
1595 /* Do not warn about artificial fields and just go into
1596 generic field mismatch warning. */
1597 if (DECL_ARTIFICIAL (f1))
1598 break;
1600 warn_odr (t1, t2, f1, f2, warn, warned,
1601 G_("a field of same name but different type "
1602 "is defined in another translation unit"));
1603 if (warn && warned)
1604 warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2), loc1, loc2);
1605 return false;
1607 if (!gimple_compare_field_offset (f1, f2))
1609 /* Do not warn about artificial fields and just go into
1610 generic field mismatch warning. */
1611 if (DECL_ARTIFICIAL (f1))
1612 break;
1613 warn_odr (t1, t2, f1, f2, warn, warned,
1614 G_("fields have different layout "
1615 "in another translation unit"));
1616 return false;
1618 if (DECL_BIT_FIELD (f1) != DECL_BIT_FIELD (f2))
1620 warn_odr (t1, t2, f1, f2, warn, warned,
1621 G_("one field is bitfield while other is not"));
1622 return false;
1624 else
1625 gcc_assert (DECL_NONADDRESSABLE_P (f1)
1626 == DECL_NONADDRESSABLE_P (f2));
1629 /* If one aggregate has more fields than the other, they
1630 are not the same. */
1631 if (f1 || f2)
1633 if ((f1 && DECL_VIRTUAL_P (f1)) || (f2 && DECL_VIRTUAL_P (f2)))
1634 warn_odr (t1, t2, NULL, NULL, warn, warned,
1635 G_("a type with different virtual table pointers"
1636 " is defined in another translation unit"));
1637 else if ((f1 && DECL_ARTIFICIAL (f1))
1638 || (f2 && DECL_ARTIFICIAL (f2)))
1639 warn_odr (t1, t2, NULL, NULL, warn, warned,
1640 G_("a type with different bases is defined "
1641 "in another translation unit"));
1642 else
1643 warn_odr (t1, t2, f1, f2, warn, warned,
1644 G_("a type with different number of fields "
1645 "is defined in another translation unit"));
1647 return false;
1650 break;
1652 case VOID_TYPE:
1653 case NULLPTR_TYPE:
1654 break;
1656 default:
1657 debug_tree (t1);
1658 gcc_unreachable ();
1661 /* Those are better to come last as they are utterly uninformative. */
1662 if (TYPE_SIZE (t1) && TYPE_SIZE (t2)
1663 && !operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0))
1665 warn_odr (t1, t2, NULL, NULL, warn, warned,
1666 G_("a type with different size "
1667 "is defined in another translation unit"));
1668 return false;
1671 gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2)
1672 || operand_equal_p (TYPE_SIZE_UNIT (t1),
1673 TYPE_SIZE_UNIT (t2), 0));
1674 return type_variants_equivalent_p (t1, t2, warn, warned);
1677 /* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule. */
1679 bool
1680 odr_types_equivalent_p (tree type1, tree type2)
1682 gcc_checking_assert (odr_or_derived_type_p (type1)
1683 && odr_or_derived_type_p (type2));
1685 hash_set<type_pair> visited;
1686 return odr_types_equivalent_p (type1, type2, false, NULL,
1687 &visited, UNKNOWN_LOCATION, UNKNOWN_LOCATION);
1690 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1691 from VAL->type. This may happen in LTO where tree merging did not merge
1692 all variants of the same type or due to ODR violation.
1694 Analyze and report ODR violations and add type to duplicate list.
1695 If TYPE is more specified than VAL->type, prevail VAL->type. Also if
1696 this is first time we see definition of a class return true so the
1697 base types are analyzed. */
1699 static bool
1700 add_type_duplicate (odr_type val, tree type)
1702 bool build_bases = false;
1703 bool prevail = false;
1704 bool odr_must_violate = false;
1706 if (!val->types_set)
1707 val->types_set = new hash_set<tree>;
1709 /* Chose polymorphic type as leader (this happens only in case of ODR
1710 violations. */
1711 if ((TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1712 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
1713 && (TREE_CODE (val->type) != RECORD_TYPE || !TYPE_BINFO (val->type)
1714 || !polymorphic_type_binfo_p (TYPE_BINFO (val->type))))
1716 prevail = true;
1717 build_bases = true;
1719 /* Always prefer complete type to be the leader. */
1720 else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type))
1722 prevail = true;
1723 if (TREE_CODE (type) == RECORD_TYPE)
1724 build_bases = TYPE_BINFO (type);
1726 else if (COMPLETE_TYPE_P (val->type) && !COMPLETE_TYPE_P (type))
1728 else if (TREE_CODE (val->type) == ENUMERAL_TYPE
1729 && TREE_CODE (type) == ENUMERAL_TYPE
1730 && !TYPE_VALUES (val->type) && TYPE_VALUES (type))
1731 prevail = true;
1732 else if (TREE_CODE (val->type) == RECORD_TYPE
1733 && TREE_CODE (type) == RECORD_TYPE
1734 && TYPE_BINFO (type) && !TYPE_BINFO (val->type))
1736 gcc_assert (!val->bases.length ());
1737 build_bases = true;
1738 prevail = true;
1741 if (prevail)
1742 std::swap (val->type, type);
1744 val->types_set->add (type);
1746 /* If we now have a mangled name, be sure to record it to val->type
1747 so ODR hash can work. */
1749 if (can_be_name_hashed_p (type) && !can_be_name_hashed_p (val->type))
1750 SET_DECL_ASSEMBLER_NAME (TYPE_NAME (val->type),
1751 DECL_ASSEMBLER_NAME (TYPE_NAME (type)));
1753 bool merge = true;
1754 bool base_mismatch = false;
1755 unsigned int i;
1756 bool warned = false;
1757 hash_set<type_pair> visited;
1759 gcc_assert (in_lto_p);
1760 vec_safe_push (val->types, type);
1762 /* If both are class types, compare the bases. */
1763 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1764 && TREE_CODE (val->type) == RECORD_TYPE
1765 && TREE_CODE (type) == RECORD_TYPE
1766 && TYPE_BINFO (val->type) && TYPE_BINFO (type))
1768 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1769 != BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1771 if (!flag_ltrans && !warned && !val->odr_violated)
1773 tree extra_base;
1774 warn_odr (type, val->type, NULL, NULL, !warned, &warned,
1775 "a type with the same name but different "
1776 "number of polymorphic bases is "
1777 "defined in another translation unit");
1778 if (warned)
1780 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1781 > BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1782 extra_base = BINFO_BASE_BINFO
1783 (TYPE_BINFO (type),
1784 BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)));
1785 else
1786 extra_base = BINFO_BASE_BINFO
1787 (TYPE_BINFO (val->type),
1788 BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1789 tree extra_base_type = BINFO_TYPE (extra_base);
1790 inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type)),
1791 "the extra base is defined here");
1794 base_mismatch = true;
1796 else
1797 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1799 tree base1 = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1800 tree base2 = BINFO_BASE_BINFO (TYPE_BINFO (val->type), i);
1801 tree type1 = BINFO_TYPE (base1);
1802 tree type2 = BINFO_TYPE (base2);
1804 if (types_odr_comparable (type1, type2))
1806 if (!types_same_for_odr (type1, type2))
1807 base_mismatch = true;
1809 else
1810 if (!odr_types_equivalent_p (type1, type2))
1811 base_mismatch = true;
1812 if (base_mismatch)
1814 if (!warned && !val->odr_violated)
1816 warn_odr (type, val->type, NULL, NULL,
1817 !warned, &warned,
1818 "a type with the same name but different base "
1819 "type is defined in another translation unit");
1820 if (warned)
1821 warn_types_mismatch (type1, type2,
1822 UNKNOWN_LOCATION, UNKNOWN_LOCATION);
1824 break;
1826 if (BINFO_OFFSET (base1) != BINFO_OFFSET (base2))
1828 base_mismatch = true;
1829 if (!warned && !val->odr_violated)
1830 warn_odr (type, val->type, NULL, NULL,
1831 !warned, &warned,
1832 "a type with the same name but different base "
1833 "layout is defined in another translation unit");
1834 break;
1836 /* One of bases is not of complete type. */
1837 if (!TYPE_BINFO (type1) != !TYPE_BINFO (type2))
1839 /* If we have a polymorphic type info specified for TYPE1
1840 but not for TYPE2 we possibly missed a base when recording
1841 VAL->type earlier.
1842 Be sure this does not happen. */
1843 if (TYPE_BINFO (type1)
1844 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1845 && !build_bases)
1846 odr_must_violate = true;
1847 break;
1849 /* One base is polymorphic and the other not.
1850 This ought to be diagnosed earlier, but do not ICE in the
1851 checking bellow. */
1852 else if (TYPE_BINFO (type1)
1853 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1854 != polymorphic_type_binfo_p (TYPE_BINFO (type2)))
1856 if (!warned && !val->odr_violated)
1857 warn_odr (type, val->type, NULL, NULL,
1858 !warned, &warned,
1859 "a base of the type is polymorphic only in one "
1860 "translation unit");
1861 base_mismatch = true;
1862 break;
1865 if (base_mismatch)
1867 merge = false;
1868 odr_violation_reported = true;
1869 val->odr_violated = true;
1871 if (symtab->dump_file)
1873 fprintf (symtab->dump_file, "ODR base violation\n");
1875 print_node (symtab->dump_file, "", val->type, 0);
1876 putc ('\n',symtab->dump_file);
1877 print_node (symtab->dump_file, "", type, 0);
1878 putc ('\n',symtab->dump_file);
1883 /* Next compare memory layout.
1884 The DECL_SOURCE_LOCATIONs in this invocation came from LTO streaming.
1885 We must apply the location cache to ensure that they are valid
1886 before we can pass them to odr_types_equivalent_p (PR lto/83121). */
1887 if (lto_location_cache::current_cache)
1888 lto_location_cache::current_cache->apply_location_cache ();
1889 /* As a special case we stream mangles names of integer types so we can see
1890 if they are believed to be same even though they have different
1891 representation. Avoid bogus warning on mismatches in these. */
1892 if (TREE_CODE (type) != INTEGER_TYPE
1893 && TREE_CODE (val->type) != INTEGER_TYPE
1894 && !odr_types_equivalent_p (val->type, type,
1895 !flag_ltrans && !val->odr_violated && !warned,
1896 &warned, &visited,
1897 DECL_SOURCE_LOCATION (TYPE_NAME (val->type)),
1898 DECL_SOURCE_LOCATION (TYPE_NAME (type))))
1900 merge = false;
1901 odr_violation_reported = true;
1902 val->odr_violated = true;
1904 gcc_assert (val->odr_violated || !odr_must_violate);
1905 /* Sanity check that all bases will be build same way again. */
1906 if (flag_checking
1907 && COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1908 && TREE_CODE (val->type) == RECORD_TYPE
1909 && TREE_CODE (type) == RECORD_TYPE
1910 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1911 && !val->odr_violated
1912 && !base_mismatch && val->bases.length ())
1914 unsigned int num_poly_bases = 0;
1915 unsigned int j;
1917 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1918 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1919 (TYPE_BINFO (type), i)))
1920 num_poly_bases++;
1921 gcc_assert (num_poly_bases == val->bases.length ());
1922 for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type));
1923 i++)
1924 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1925 (TYPE_BINFO (type), i)))
1927 odr_type base = get_odr_type
1928 (BINFO_TYPE
1929 (BINFO_BASE_BINFO (TYPE_BINFO (type),
1930 i)),
1931 true);
1932 gcc_assert (val->bases[j] == base);
1933 j++;
1938 /* Regularize things a little. During LTO same types may come with
1939 different BINFOs. Either because their virtual table was
1940 not merged by tree merging and only later at decl merging or
1941 because one type comes with external vtable, while other
1942 with internal. We want to merge equivalent binfos to conserve
1943 memory and streaming overhead.
1945 The external vtables are more harmful: they contain references
1946 to external declarations of methods that may be defined in the
1947 merged LTO unit. For this reason we absolutely need to remove
1948 them and replace by internal variants. Not doing so will lead
1949 to incomplete answers from possible_polymorphic_call_targets.
1951 FIXME: disable for now; because ODR types are now build during
1952 streaming in, the variants do not need to be linked to the type,
1953 yet. We need to do the merging in cleanup pass to be implemented
1954 soon. */
1955 if (!flag_ltrans && merge
1956 && 0
1957 && TREE_CODE (val->type) == RECORD_TYPE
1958 && TREE_CODE (type) == RECORD_TYPE
1959 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1960 && TYPE_MAIN_VARIANT (type) == type
1961 && TYPE_MAIN_VARIANT (val->type) == val->type
1962 && BINFO_VTABLE (TYPE_BINFO (val->type))
1963 && BINFO_VTABLE (TYPE_BINFO (type)))
1965 tree master_binfo = TYPE_BINFO (val->type);
1966 tree v1 = BINFO_VTABLE (master_binfo);
1967 tree v2 = BINFO_VTABLE (TYPE_BINFO (type));
1969 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
1971 gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR
1972 && operand_equal_p (TREE_OPERAND (v1, 1),
1973 TREE_OPERAND (v2, 1), 0));
1974 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
1975 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
1977 gcc_assert (DECL_ASSEMBLER_NAME (v1)
1978 == DECL_ASSEMBLER_NAME (v2));
1980 if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2))
1982 unsigned int i;
1984 set_type_binfo (val->type, TYPE_BINFO (type));
1985 for (i = 0; i < val->types->length (); i++)
1987 if (TYPE_BINFO ((*val->types)[i])
1988 == master_binfo)
1989 set_type_binfo ((*val->types)[i], TYPE_BINFO (type));
1991 BINFO_TYPE (TYPE_BINFO (type)) = val->type;
1993 else
1994 set_type_binfo (type, master_binfo);
1996 return build_bases;
1999 /* Get ODR type hash entry for TYPE. If INSERT is true, create
2000 possibly new entry. */
2002 odr_type
2003 get_odr_type (tree type, bool insert)
2005 odr_type_d **slot = NULL;
2006 odr_type_d **vtable_slot = NULL;
2007 odr_type val = NULL;
2008 hashval_t hash;
2009 bool build_bases = false;
2010 bool insert_to_odr_array = false;
2011 int base_id = -1;
2013 type = TYPE_MAIN_VARIANT (type);
2015 gcc_checking_assert (can_be_name_hashed_p (type)
2016 || can_be_vtable_hashed_p (type));
2018 /* Lookup entry, first try name hash, fallback to vtable hash. */
2019 if (can_be_name_hashed_p (type))
2021 hash = hash_odr_name (type);
2022 slot = odr_hash->find_slot_with_hash (type, hash,
2023 insert ? INSERT : NO_INSERT);
2025 if ((!slot || !*slot) && in_lto_p && can_be_vtable_hashed_p (type))
2027 hash = hash_odr_vtable (type);
2028 if (!odr_vtable_hash)
2029 odr_vtable_hash = new odr_vtable_hash_type (23);
2030 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
2031 insert ? INSERT : NO_INSERT);
2034 if (!slot && !vtable_slot)
2035 return NULL;
2037 /* See if we already have entry for type. */
2038 if ((slot && *slot) || (vtable_slot && *vtable_slot))
2040 if (slot && *slot)
2042 val = *slot;
2043 if (flag_checking
2044 && in_lto_p && can_be_vtable_hashed_p (type))
2046 hash = hash_odr_vtable (type);
2047 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
2048 NO_INSERT);
2049 gcc_assert (!vtable_slot || *vtable_slot == *slot);
2050 vtable_slot = NULL;
2053 else if (*vtable_slot)
2054 val = *vtable_slot;
2056 if (val->type != type && insert
2057 && (!val->types_set || !val->types_set->add (type)))
2059 /* We have type duplicate, but it may introduce vtable name or
2060 mangled name; be sure to keep hashes in sync. */
2061 if (in_lto_p && can_be_vtable_hashed_p (type)
2062 && (!vtable_slot || !*vtable_slot))
2064 if (!vtable_slot)
2066 hash = hash_odr_vtable (type);
2067 vtable_slot = odr_vtable_hash->find_slot_with_hash
2068 (type, hash, INSERT);
2069 gcc_checking_assert (!*vtable_slot || *vtable_slot == val);
2071 *vtable_slot = val;
2073 if (slot && !*slot)
2074 *slot = val;
2075 build_bases = add_type_duplicate (val, type);
2078 else
2080 val = ggc_cleared_alloc<odr_type_d> ();
2081 val->type = type;
2082 val->bases = vNULL;
2083 val->derived_types = vNULL;
2084 if (type_with_linkage_p (type))
2085 val->anonymous_namespace = type_in_anonymous_namespace_p (type);
2086 else
2087 val->anonymous_namespace = 0;
2088 build_bases = COMPLETE_TYPE_P (val->type);
2089 insert_to_odr_array = true;
2090 if (slot)
2091 *slot = val;
2092 if (vtable_slot)
2093 *vtable_slot = val;
2096 if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
2097 && type_with_linkage_p (type)
2098 && type == TYPE_MAIN_VARIANT (type))
2100 tree binfo = TYPE_BINFO (type);
2101 unsigned int i;
2103 gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) == type);
2105 val->all_derivations_known = type_all_derivations_known_p (type);
2106 for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
2107 /* For now record only polymorphic types. other are
2108 pointless for devirtualization and we can not precisely
2109 determine ODR equivalency of these during LTO. */
2110 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
2112 tree base_type= BINFO_TYPE (BINFO_BASE_BINFO (binfo, i));
2113 odr_type base = get_odr_type (base_type, true);
2114 gcc_assert (TYPE_MAIN_VARIANT (base_type) == base_type);
2115 base->derived_types.safe_push (val);
2116 val->bases.safe_push (base);
2117 if (base->id > base_id)
2118 base_id = base->id;
2121 /* Ensure that type always appears after bases. */
2122 if (insert_to_odr_array)
2124 if (odr_types_ptr)
2125 val->id = odr_types.length ();
2126 vec_safe_push (odr_types_ptr, val);
2128 else if (base_id > val->id)
2130 odr_types[val->id] = 0;
2131 /* Be sure we did not recorded any derived types; these may need
2132 renumbering too. */
2133 gcc_assert (val->derived_types.length() == 0);
2134 val->id = odr_types.length ();
2135 vec_safe_push (odr_types_ptr, val);
2137 return val;
2140 /* Add TYPE od ODR type hash. */
2142 void
2143 register_odr_type (tree type)
2145 if (!odr_hash)
2147 odr_hash = new odr_hash_type (23);
2148 if (in_lto_p)
2149 odr_vtable_hash = new odr_vtable_hash_type (23);
2151 if (type == TYPE_MAIN_VARIANT (type))
2153 /* To get ODR warings right, first register all sub-types. */
2154 if (RECORD_OR_UNION_TYPE_P (type)
2155 && COMPLETE_TYPE_P (type))
2157 /* Limit recursion on types which are already registered. */
2158 odr_type ot = get_odr_type (type, false);
2159 if (ot
2160 && (ot->type == type
2161 || (ot->types_set
2162 && ot->types_set->contains (type))))
2163 return;
2164 for (tree f = TYPE_FIELDS (type); f; f = TREE_CHAIN (f))
2165 if (TREE_CODE (f) == FIELD_DECL)
2167 tree subtype = TREE_TYPE (f);
2169 while (TREE_CODE (subtype) == ARRAY_TYPE)
2170 subtype = TREE_TYPE (subtype);
2171 if (type_with_linkage_p (TYPE_MAIN_VARIANT (subtype)))
2172 register_odr_type (TYPE_MAIN_VARIANT (subtype));
2174 if (TYPE_BINFO (type))
2175 for (unsigned int i = 0;
2176 i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
2177 register_odr_type (BINFO_TYPE (BINFO_BASE_BINFO
2178 (TYPE_BINFO (type), i)));
2180 get_odr_type (type, true);
2184 /* Return true if type is known to have no derivations. */
2186 bool
2187 type_known_to_have_no_derivations_p (tree t)
2189 return (type_all_derivations_known_p (t)
2190 && (TYPE_FINAL_P (t)
2191 || (odr_hash
2192 && !get_odr_type (t, true)->derived_types.length())));
2195 /* Dump ODR type T and all its derived types. INDENT specifies indentation for
2196 recursive printing. */
2198 static void
2199 dump_odr_type (FILE *f, odr_type t, int indent=0)
2201 unsigned int i;
2202 fprintf (f, "%*s type %i: ", indent * 2, "", t->id);
2203 print_generic_expr (f, t->type, TDF_SLIM);
2204 fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":"");
2205 fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":"");
2206 if (TYPE_NAME (t->type))
2208 /*fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
2209 DECL_SOURCE_FILE (TYPE_NAME (t->type)),
2210 DECL_SOURCE_LINE (TYPE_NAME (t->type)));*/
2211 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t->type)))
2212 fprintf (f, "%*s mangled name: %s\n", indent * 2, "",
2213 IDENTIFIER_POINTER
2214 (DECL_ASSEMBLER_NAME (TYPE_NAME (t->type))));
2216 if (t->bases.length ())
2218 fprintf (f, "%*s base odr type ids: ", indent * 2, "");
2219 for (i = 0; i < t->bases.length (); i++)
2220 fprintf (f, " %i", t->bases[i]->id);
2221 fprintf (f, "\n");
2223 if (t->derived_types.length ())
2225 fprintf (f, "%*s derived types:\n", indent * 2, "");
2226 for (i = 0; i < t->derived_types.length (); i++)
2227 dump_odr_type (f, t->derived_types[i], indent + 1);
2229 fprintf (f, "\n");
2232 /* Dump the type inheritance graph. */
2234 static void
2235 dump_type_inheritance_graph (FILE *f)
2237 unsigned int i;
2238 unsigned int num_all_types = 0, num_types = 0, num_duplicates = 0;
2239 if (!odr_types_ptr)
2240 return;
2241 fprintf (f, "\n\nType inheritance graph:\n");
2242 for (i = 0; i < odr_types.length (); i++)
2244 if (odr_types[i] && odr_types[i]->bases.length () == 0)
2245 dump_odr_type (f, odr_types[i]);
2247 for (i = 0; i < odr_types.length (); i++)
2249 if (!odr_types[i])
2250 continue;
2252 num_all_types++;
2253 if (!odr_types[i]->types || !odr_types[i]->types->length ())
2254 continue;
2256 /* To aid ODR warnings we also mangle integer constants but do
2257 not consinder duplicates there. */
2258 if (TREE_CODE (odr_types[i]->type) == INTEGER_TYPE)
2259 continue;
2261 /* It is normal to have one duplicate and one normal variant. */
2262 if (odr_types[i]->types->length () == 1
2263 && COMPLETE_TYPE_P (odr_types[i]->type)
2264 && !COMPLETE_TYPE_P ((*odr_types[i]->types)[0]))
2265 continue;
2267 num_types ++;
2269 unsigned int j;
2270 fprintf (f, "Duplicate tree types for odr type %i\n", i);
2271 print_node (f, "", odr_types[i]->type, 0);
2272 print_node (f, "", TYPE_NAME (odr_types[i]->type), 0);
2273 putc ('\n',f);
2274 for (j = 0; j < odr_types[i]->types->length (); j++)
2276 tree t;
2277 num_duplicates ++;
2278 fprintf (f, "duplicate #%i\n", j);
2279 print_node (f, "", (*odr_types[i]->types)[j], 0);
2280 t = (*odr_types[i]->types)[j];
2281 while (TYPE_P (t) && TYPE_CONTEXT (t))
2283 t = TYPE_CONTEXT (t);
2284 print_node (f, "", t, 0);
2286 print_node (f, "", TYPE_NAME ((*odr_types[i]->types)[j]), 0);
2287 putc ('\n',f);
2290 fprintf (f, "Out of %i types there are %i types with duplicates; "
2291 "%i duplicates overall\n", num_all_types, num_types, num_duplicates);
2294 /* Save some WPA->ltrans streaming by freeing stuff needed only for good
2295 ODR warnings.
2296 We free TYPE_VALUES of enums and also make TYPE_DECLs to not point back
2297 to the type (which is needed to keep them in the same SCC and preserve
2298 location information to output warnings) and subsequently we make all
2299 TYPE_DECLS of same assembler name equivalent. */
2301 static void
2302 free_odr_warning_data ()
2304 static bool odr_data_freed = false;
2306 if (odr_data_freed || !flag_wpa || !odr_types_ptr)
2307 return;
2309 odr_data_freed = true;
2311 for (unsigned int i = 0; i < odr_types.length (); i++)
2312 if (odr_types[i])
2314 tree t = odr_types[i]->type;
2316 if (TREE_CODE (t) == ENUMERAL_TYPE)
2317 TYPE_VALUES (t) = NULL;
2318 TREE_TYPE (TYPE_NAME (t)) = void_type_node;
2320 if (odr_types[i]->types)
2321 for (unsigned int j = 0; j < odr_types[i]->types->length (); j++)
2323 tree td = (*odr_types[i]->types)[j];
2325 if (TREE_CODE (td) == ENUMERAL_TYPE)
2326 TYPE_VALUES (td) = NULL;
2327 TYPE_NAME (td) = TYPE_NAME (t);
2330 odr_data_freed = true;
2333 /* Initialize IPA devirt and build inheritance tree graph. */
2335 void
2336 build_type_inheritance_graph (void)
2338 struct symtab_node *n;
2339 FILE *inheritance_dump_file;
2340 dump_flags_t flags;
2342 if (odr_hash)
2344 free_odr_warning_data ();
2345 return;
2347 timevar_push (TV_IPA_INHERITANCE);
2348 inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
2349 odr_hash = new odr_hash_type (23);
2350 if (in_lto_p)
2351 odr_vtable_hash = new odr_vtable_hash_type (23);
2353 /* We reconstruct the graph starting of types of all methods seen in the
2354 unit. */
2355 FOR_EACH_SYMBOL (n)
2356 if (is_a <cgraph_node *> (n)
2357 && DECL_VIRTUAL_P (n->decl)
2358 && n->real_symbol_p ())
2359 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
2361 /* Look also for virtual tables of types that do not define any methods.
2363 We need it in a case where class B has virtual base of class A
2364 re-defining its virtual method and there is class C with no virtual
2365 methods with B as virtual base.
2367 Here we output B's virtual method in two variant - for non-virtual
2368 and virtual inheritance. B's virtual table has non-virtual version,
2369 while C's has virtual.
2371 For this reason we need to know about C in order to include both
2372 variants of B. More correctly, record_target_from_binfo should
2373 add both variants of the method when walking B, but we have no
2374 link in between them.
2376 We rely on fact that either the method is exported and thus we
2377 assume it is called externally or C is in anonymous namespace and
2378 thus we will see the vtable. */
2380 else if (is_a <varpool_node *> (n)
2381 && DECL_VIRTUAL_P (n->decl)
2382 && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE
2383 && TYPE_BINFO (DECL_CONTEXT (n->decl))
2384 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl))))
2385 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n->decl)), true);
2386 if (inheritance_dump_file)
2388 dump_type_inheritance_graph (inheritance_dump_file);
2389 dump_end (TDI_inheritance, inheritance_dump_file);
2391 free_odr_warning_data ();
2392 timevar_pop (TV_IPA_INHERITANCE);
2395 /* Return true if N has reference from live virtual table
2396 (and thus can be a destination of polymorphic call).
2397 Be conservatively correct when callgraph is not built or
2398 if the method may be referred externally. */
2400 static bool
2401 referenced_from_vtable_p (struct cgraph_node *node)
2403 int i;
2404 struct ipa_ref *ref;
2405 bool found = false;
2407 if (node->externally_visible
2408 || DECL_EXTERNAL (node->decl)
2409 || node->used_from_other_partition)
2410 return true;
2412 /* Keep this test constant time.
2413 It is unlikely this can happen except for the case where speculative
2414 devirtualization introduced many speculative edges to this node.
2415 In this case the target is very likely alive anyway. */
2416 if (node->ref_list.referring.length () > 100)
2417 return true;
2419 /* We need references built. */
2420 if (symtab->state <= CONSTRUCTION)
2421 return true;
2423 for (i = 0; node->iterate_referring (i, ref); i++)
2424 if ((ref->use == IPA_REF_ALIAS
2425 && referenced_from_vtable_p (dyn_cast<cgraph_node *> (ref->referring)))
2426 || (ref->use == IPA_REF_ADDR
2427 && VAR_P (ref->referring->decl)
2428 && DECL_VIRTUAL_P (ref->referring->decl)))
2430 found = true;
2431 break;
2433 return found;
2436 /* Return if TARGET is cxa_pure_virtual. */
2438 static bool
2439 is_cxa_pure_virtual_p (tree target)
2441 return target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE
2442 && DECL_NAME (target)
2443 && id_equal (DECL_NAME (target),
2444 "__cxa_pure_virtual");
2447 /* If TARGET has associated node, record it in the NODES array.
2448 CAN_REFER specify if program can refer to the target directly.
2449 if TARGET is unknown (NULL) or it can not be inserted (for example because
2450 its body was already removed and there is no way to refer to it), clear
2451 COMPLETEP. */
2453 static void
2454 maybe_record_node (vec <cgraph_node *> &nodes,
2455 tree target, hash_set<tree> *inserted,
2456 bool can_refer,
2457 bool *completep)
2459 struct cgraph_node *target_node, *alias_target;
2460 enum availability avail;
2461 bool pure_virtual = is_cxa_pure_virtual_p (target);
2463 /* __builtin_unreachable do not need to be added into
2464 list of targets; the runtime effect of calling them is undefined.
2465 Only "real" virtual methods should be accounted. */
2466 if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE && !pure_virtual)
2467 return;
2469 if (!can_refer)
2471 /* The only case when method of anonymous namespace becomes unreferable
2472 is when we completely optimized it out. */
2473 if (flag_ltrans
2474 || !target
2475 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
2476 *completep = false;
2477 return;
2480 if (!target)
2481 return;
2483 target_node = cgraph_node::get (target);
2485 /* Prefer alias target over aliases, so we do not get confused by
2486 fake duplicates. */
2487 if (target_node)
2489 alias_target = target_node->ultimate_alias_target (&avail);
2490 if (target_node != alias_target
2491 && avail >= AVAIL_AVAILABLE
2492 && target_node->get_availability ())
2493 target_node = alias_target;
2496 /* Method can only be called by polymorphic call if any
2497 of vtables referring to it are alive.
2499 While this holds for non-anonymous functions, too, there are
2500 cases where we want to keep them in the list; for example
2501 inline functions with -fno-weak are static, but we still
2502 may devirtualize them when instance comes from other unit.
2503 The same holds for LTO.
2505 Currently we ignore these functions in speculative devirtualization.
2506 ??? Maybe it would make sense to be more aggressive for LTO even
2507 elsewhere. */
2508 if (!flag_ltrans
2509 && !pure_virtual
2510 && type_in_anonymous_namespace_p (DECL_CONTEXT (target))
2511 && (!target_node
2512 || !referenced_from_vtable_p (target_node)))
2514 /* See if TARGET is useful function we can deal with. */
2515 else if (target_node != NULL
2516 && (TREE_PUBLIC (target)
2517 || DECL_EXTERNAL (target)
2518 || target_node->definition)
2519 && target_node->real_symbol_p ())
2521 gcc_assert (!target_node->global.inlined_to);
2522 gcc_assert (target_node->real_symbol_p ());
2523 /* When sanitizing, do not assume that __cxa_pure_virtual is not called
2524 by valid program. */
2525 if (flag_sanitize & SANITIZE_UNREACHABLE)
2527 /* Only add pure virtual if it is the only possible target. This way
2528 we will preserve the diagnostics about pure virtual called in many
2529 cases without disabling optimization in other. */
2530 else if (pure_virtual)
2532 if (nodes.length ())
2533 return;
2535 /* If we found a real target, take away cxa_pure_virtual. */
2536 else if (!pure_virtual && nodes.length () == 1
2537 && is_cxa_pure_virtual_p (nodes[0]->decl))
2538 nodes.pop ();
2539 if (pure_virtual && nodes.length ())
2540 return;
2541 if (!inserted->add (target))
2543 cached_polymorphic_call_targets->add (target_node);
2544 nodes.safe_push (target_node);
2547 else if (!completep)
2549 /* We have definition of __cxa_pure_virtual that is not accessible (it is
2550 optimized out or partitioned to other unit) so we can not add it. When
2551 not sanitizing, there is nothing to do.
2552 Otherwise declare the list incomplete. */
2553 else if (pure_virtual)
2555 if (flag_sanitize & SANITIZE_UNREACHABLE)
2556 *completep = false;
2558 else if (flag_ltrans
2559 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
2560 *completep = false;
2563 /* See if BINFO's type matches OUTER_TYPE. If so, look up
2564 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
2565 method in vtable and insert method to NODES array
2566 or BASES_TO_CONSIDER if this array is non-NULL.
2567 Otherwise recurse to base BINFOs.
2568 This matches what get_binfo_at_offset does, but with offset
2569 being unknown.
2571 TYPE_BINFOS is a stack of BINFOS of types with defined
2572 virtual table seen on way from class type to BINFO.
2574 MATCHED_VTABLES tracks virtual tables we already did lookup
2575 for virtual function in. INSERTED tracks nodes we already
2576 inserted.
2578 ANONYMOUS is true if BINFO is part of anonymous namespace.
2580 Clear COMPLETEP when we hit unreferable target.
2583 static void
2584 record_target_from_binfo (vec <cgraph_node *> &nodes,
2585 vec <tree> *bases_to_consider,
2586 tree binfo,
2587 tree otr_type,
2588 vec <tree> &type_binfos,
2589 HOST_WIDE_INT otr_token,
2590 tree outer_type,
2591 HOST_WIDE_INT offset,
2592 hash_set<tree> *inserted,
2593 hash_set<tree> *matched_vtables,
2594 bool anonymous,
2595 bool *completep)
2597 tree type = BINFO_TYPE (binfo);
2598 int i;
2599 tree base_binfo;
2602 if (BINFO_VTABLE (binfo))
2603 type_binfos.safe_push (binfo);
2604 if (types_same_for_odr (type, outer_type))
2606 int i;
2607 tree type_binfo = NULL;
2609 /* Look up BINFO with virtual table. For normal types it is always last
2610 binfo on stack. */
2611 for (i = type_binfos.length () - 1; i >= 0; i--)
2612 if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo))
2614 type_binfo = type_binfos[i];
2615 break;
2617 if (BINFO_VTABLE (binfo))
2618 type_binfos.pop ();
2619 /* If this is duplicated BINFO for base shared by virtual inheritance,
2620 we may not have its associated vtable. This is not a problem, since
2621 we will walk it on the other path. */
2622 if (!type_binfo)
2623 return;
2624 tree inner_binfo = get_binfo_at_offset (type_binfo,
2625 offset, otr_type);
2626 if (!inner_binfo)
2628 gcc_assert (odr_violation_reported);
2629 return;
2631 /* For types in anonymous namespace first check if the respective vtable
2632 is alive. If not, we know the type can't be called. */
2633 if (!flag_ltrans && anonymous)
2635 tree vtable = BINFO_VTABLE (inner_binfo);
2636 varpool_node *vnode;
2638 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
2639 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
2640 vnode = varpool_node::get (vtable);
2641 if (!vnode || !vnode->definition)
2642 return;
2644 gcc_assert (inner_binfo);
2645 if (bases_to_consider
2646 ? !matched_vtables->contains (BINFO_VTABLE (inner_binfo))
2647 : !matched_vtables->add (BINFO_VTABLE (inner_binfo)))
2649 bool can_refer;
2650 tree target = gimple_get_virt_method_for_binfo (otr_token,
2651 inner_binfo,
2652 &can_refer);
2653 if (!bases_to_consider)
2654 maybe_record_node (nodes, target, inserted, can_refer, completep);
2655 /* Destructors are never called via construction vtables. */
2656 else if (!target || !DECL_CXX_DESTRUCTOR_P (target))
2657 bases_to_consider->safe_push (target);
2659 return;
2662 /* Walk bases. */
2663 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2664 /* Walking bases that have no virtual method is pointless exercise. */
2665 if (polymorphic_type_binfo_p (base_binfo))
2666 record_target_from_binfo (nodes, bases_to_consider, base_binfo, otr_type,
2667 type_binfos,
2668 otr_token, outer_type, offset, inserted,
2669 matched_vtables, anonymous, completep);
2670 if (BINFO_VTABLE (binfo))
2671 type_binfos.pop ();
2674 /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
2675 of TYPE, insert them to NODES, recurse into derived nodes.
2676 INSERTED is used to avoid duplicate insertions of methods into NODES.
2677 MATCHED_VTABLES are used to avoid duplicate walking vtables.
2678 Clear COMPLETEP if unreferable target is found.
2680 If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
2681 all cases where BASE_SKIPPED is true (because the base is abstract
2682 class). */
2684 static void
2685 possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
2686 hash_set<tree> *inserted,
2687 hash_set<tree> *matched_vtables,
2688 tree otr_type,
2689 odr_type type,
2690 HOST_WIDE_INT otr_token,
2691 tree outer_type,
2692 HOST_WIDE_INT offset,
2693 bool *completep,
2694 vec <tree> &bases_to_consider,
2695 bool consider_construction)
2697 tree binfo = TYPE_BINFO (type->type);
2698 unsigned int i;
2699 auto_vec <tree, 8> type_binfos;
2700 bool possibly_instantiated = type_possibly_instantiated_p (type->type);
2702 /* We may need to consider types w/o instances because of possible derived
2703 types using their methods either directly or via construction vtables.
2704 We are safe to skip them when all derivations are known, since we will
2705 handle them later.
2706 This is done by recording them to BASES_TO_CONSIDER array. */
2707 if (possibly_instantiated || consider_construction)
2709 record_target_from_binfo (nodes,
2710 (!possibly_instantiated
2711 && type_all_derivations_known_p (type->type))
2712 ? &bases_to_consider : NULL,
2713 binfo, otr_type, type_binfos, otr_token,
2714 outer_type, offset,
2715 inserted, matched_vtables,
2716 type->anonymous_namespace, completep);
2718 for (i = 0; i < type->derived_types.length (); i++)
2719 possible_polymorphic_call_targets_1 (nodes, inserted,
2720 matched_vtables,
2721 otr_type,
2722 type->derived_types[i],
2723 otr_token, outer_type, offset, completep,
2724 bases_to_consider, consider_construction);
2727 /* Cache of queries for polymorphic call targets.
2729 Enumerating all call targets may get expensive when there are many
2730 polymorphic calls in the program, so we memoize all the previous
2731 queries and avoid duplicated work. */
2733 struct polymorphic_call_target_d
2735 HOST_WIDE_INT otr_token;
2736 ipa_polymorphic_call_context context;
2737 odr_type type;
2738 vec <cgraph_node *> targets;
2739 tree decl_warning;
2740 int type_warning;
2741 bool complete;
2742 bool speculative;
2745 /* Polymorphic call target cache helpers. */
2747 struct polymorphic_call_target_hasher
2748 : pointer_hash <polymorphic_call_target_d>
2750 static inline hashval_t hash (const polymorphic_call_target_d *);
2751 static inline bool equal (const polymorphic_call_target_d *,
2752 const polymorphic_call_target_d *);
2753 static inline void remove (polymorphic_call_target_d *);
2756 /* Return the computed hashcode for ODR_QUERY. */
2758 inline hashval_t
2759 polymorphic_call_target_hasher::hash (const polymorphic_call_target_d *odr_query)
2761 inchash::hash hstate (odr_query->otr_token);
2763 hstate.add_hwi (odr_query->type->id);
2764 hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
2765 hstate.add_hwi (odr_query->context.offset);
2767 if (odr_query->context.speculative_outer_type)
2769 hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
2770 hstate.add_hwi (odr_query->context.speculative_offset);
2772 hstate.add_flag (odr_query->speculative);
2773 hstate.add_flag (odr_query->context.maybe_in_construction);
2774 hstate.add_flag (odr_query->context.maybe_derived_type);
2775 hstate.add_flag (odr_query->context.speculative_maybe_derived_type);
2776 hstate.commit_flag ();
2777 return hstate.end ();
2780 /* Compare cache entries T1 and T2. */
2782 inline bool
2783 polymorphic_call_target_hasher::equal (const polymorphic_call_target_d *t1,
2784 const polymorphic_call_target_d *t2)
2786 return (t1->type == t2->type && t1->otr_token == t2->otr_token
2787 && t1->speculative == t2->speculative
2788 && t1->context.offset == t2->context.offset
2789 && t1->context.speculative_offset == t2->context.speculative_offset
2790 && t1->context.outer_type == t2->context.outer_type
2791 && t1->context.speculative_outer_type == t2->context.speculative_outer_type
2792 && t1->context.maybe_in_construction
2793 == t2->context.maybe_in_construction
2794 && t1->context.maybe_derived_type == t2->context.maybe_derived_type
2795 && (t1->context.speculative_maybe_derived_type
2796 == t2->context.speculative_maybe_derived_type));
2799 /* Remove entry in polymorphic call target cache hash. */
2801 inline void
2802 polymorphic_call_target_hasher::remove (polymorphic_call_target_d *v)
2804 v->targets.release ();
2805 free (v);
2808 /* Polymorphic call target query cache. */
2810 typedef hash_table<polymorphic_call_target_hasher>
2811 polymorphic_call_target_hash_type;
2812 static polymorphic_call_target_hash_type *polymorphic_call_target_hash;
2814 /* Destroy polymorphic call target query cache. */
2816 static void
2817 free_polymorphic_call_targets_hash ()
2819 if (cached_polymorphic_call_targets)
2821 delete polymorphic_call_target_hash;
2822 polymorphic_call_target_hash = NULL;
2823 delete cached_polymorphic_call_targets;
2824 cached_polymorphic_call_targets = NULL;
2828 /* Force rebuilding type inheritance graph from scratch.
2829 This is use to make sure that we do not keep references to types
2830 which was not visible to free_lang_data. */
2832 void
2833 rebuild_type_inheritance_graph ()
2835 if (!odr_hash)
2836 return;
2837 delete odr_hash;
2838 if (in_lto_p)
2839 delete odr_vtable_hash;
2840 odr_hash = NULL;
2841 odr_vtable_hash = NULL;
2842 odr_types_ptr = NULL;
2843 free_polymorphic_call_targets_hash ();
2846 /* When virtual function is removed, we may need to flush the cache. */
2848 static void
2849 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
2851 if (cached_polymorphic_call_targets
2852 && cached_polymorphic_call_targets->contains (n))
2853 free_polymorphic_call_targets_hash ();
2856 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2858 tree
2859 subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
2860 tree vtable)
2862 tree v = BINFO_VTABLE (binfo);
2863 int i;
2864 tree base_binfo;
2865 unsigned HOST_WIDE_INT this_offset;
2867 if (v)
2869 if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
2870 gcc_unreachable ();
2872 if (offset == this_offset
2873 && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
2874 return binfo;
2877 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2878 if (polymorphic_type_binfo_p (base_binfo))
2880 base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable);
2881 if (base_binfo)
2882 return base_binfo;
2884 return NULL;
2887 /* T is known constant value of virtual table pointer.
2888 Store virtual table to V and its offset to OFFSET.
2889 Return false if T does not look like virtual table reference. */
2891 bool
2892 vtable_pointer_value_to_vtable (const_tree t, tree *v,
2893 unsigned HOST_WIDE_INT *offset)
2895 /* We expect &MEM[(void *)&virtual_table + 16B].
2896 We obtain object's BINFO from the context of the virtual table.
2897 This one contains pointer to virtual table represented via
2898 POINTER_PLUS_EXPR. Verify that this pointer matches what
2899 we propagated through.
2901 In the case of virtual inheritance, the virtual tables may
2902 be nested, i.e. the offset may be different from 16 and we may
2903 need to dive into the type representation. */
2904 if (TREE_CODE (t) == ADDR_EXPR
2905 && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
2906 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
2907 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
2908 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
2909 == VAR_DECL)
2910 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2911 (TREE_OPERAND (t, 0), 0), 0)))
2913 *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
2914 *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
2915 return true;
2918 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2919 We need to handle it when T comes from static variable initializer or
2920 BINFO. */
2921 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
2923 *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
2924 t = TREE_OPERAND (t, 0);
2926 else
2927 *offset = 0;
2929 if (TREE_CODE (t) != ADDR_EXPR)
2930 return false;
2931 *v = TREE_OPERAND (t, 0);
2932 return true;
2935 /* T is known constant value of virtual table pointer. Return BINFO of the
2936 instance type. */
2938 tree
2939 vtable_pointer_value_to_binfo (const_tree t)
2941 tree vtable;
2942 unsigned HOST_WIDE_INT offset;
2944 if (!vtable_pointer_value_to_vtable (t, &vtable, &offset))
2945 return NULL_TREE;
2947 /* FIXME: for stores of construction vtables we return NULL,
2948 because we do not have BINFO for those. Eventually we should fix
2949 our representation to allow this case to be handled, too.
2950 In the case we see store of BINFO we however may assume
2951 that standard folding will be able to cope with it. */
2952 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
2953 offset, vtable);
2956 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2957 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2958 and insert them in NODES.
2960 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2962 static void
2963 record_targets_from_bases (tree otr_type,
2964 HOST_WIDE_INT otr_token,
2965 tree outer_type,
2966 HOST_WIDE_INT offset,
2967 vec <cgraph_node *> &nodes,
2968 hash_set<tree> *inserted,
2969 hash_set<tree> *matched_vtables,
2970 bool *completep)
2972 while (true)
2974 HOST_WIDE_INT pos, size;
2975 tree base_binfo;
2976 tree fld;
2978 if (types_same_for_odr (outer_type, otr_type))
2979 return;
2981 for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
2983 if (TREE_CODE (fld) != FIELD_DECL)
2984 continue;
2986 pos = int_bit_position (fld);
2987 size = tree_to_shwi (DECL_SIZE (fld));
2988 if (pos <= offset && (pos + size) > offset
2989 /* Do not get confused by zero sized bases. */
2990 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
2991 break;
2993 /* Within a class type we should always find corresponding fields. */
2994 gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
2996 /* Nonbase types should have been stripped by outer_class_type. */
2997 gcc_assert (DECL_ARTIFICIAL (fld));
2999 outer_type = TREE_TYPE (fld);
3000 offset -= pos;
3002 base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
3003 offset, otr_type);
3004 if (!base_binfo)
3006 gcc_assert (odr_violation_reported);
3007 return;
3009 gcc_assert (base_binfo);
3010 if (!matched_vtables->add (BINFO_VTABLE (base_binfo)))
3012 bool can_refer;
3013 tree target = gimple_get_virt_method_for_binfo (otr_token,
3014 base_binfo,
3015 &can_refer);
3016 if (!target || ! DECL_CXX_DESTRUCTOR_P (target))
3017 maybe_record_node (nodes, target, inserted, can_refer, completep);
3018 matched_vtables->add (BINFO_VTABLE (base_binfo));
3023 /* When virtual table is removed, we may need to flush the cache. */
3025 static void
3026 devirt_variable_node_removal_hook (varpool_node *n,
3027 void *d ATTRIBUTE_UNUSED)
3029 if (cached_polymorphic_call_targets
3030 && DECL_VIRTUAL_P (n->decl)
3031 && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
3032 free_polymorphic_call_targets_hash ();
3035 /* Record about how many calls would benefit from given type to be final. */
3037 struct odr_type_warn_count
3039 tree type;
3040 int count;
3041 profile_count dyn_count;
3044 /* Record about how many calls would benefit from given method to be final. */
3046 struct decl_warn_count
3048 tree decl;
3049 int count;
3050 profile_count dyn_count;
3053 /* Information about type and decl warnings. */
3055 struct final_warning_record
3057 /* If needed grow type_warnings vector and initialize new decl_warn_count
3058 to have dyn_count set to profile_count::zero (). */
3059 void grow_type_warnings (unsigned newlen);
3061 profile_count dyn_count;
3062 auto_vec<odr_type_warn_count> type_warnings;
3063 hash_map<tree, decl_warn_count> decl_warnings;
3066 void
3067 final_warning_record::grow_type_warnings (unsigned newlen)
3069 unsigned len = type_warnings.length ();
3070 if (newlen > len)
3072 type_warnings.safe_grow_cleared (newlen);
3073 for (unsigned i = len; i < newlen; i++)
3074 type_warnings[i].dyn_count = profile_count::zero ();
3078 struct final_warning_record *final_warning_records;
3080 /* Return vector containing possible targets of polymorphic call of type
3081 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
3082 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
3083 OTR_TYPE and include their virtual method. This is useful for types
3084 possibly in construction or destruction where the virtual table may
3085 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
3086 us to walk the inheritance graph for all derivations.
3088 If COMPLETEP is non-NULL, store true if the list is complete.
3089 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
3090 in the target cache. If user needs to visit every target list
3091 just once, it can memoize them.
3093 If SPECULATIVE is set, the list will not contain targets that
3094 are not speculatively taken.
3096 Returned vector is placed into cache. It is NOT caller's responsibility
3097 to free it. The vector can be freed on cgraph_remove_node call if
3098 the particular node is a virtual function present in the cache. */
3100 vec <cgraph_node *>
3101 possible_polymorphic_call_targets (tree otr_type,
3102 HOST_WIDE_INT otr_token,
3103 ipa_polymorphic_call_context context,
3104 bool *completep,
3105 void **cache_token,
3106 bool speculative)
3108 static struct cgraph_node_hook_list *node_removal_hook_holder;
3109 vec <cgraph_node *> nodes = vNULL;
3110 auto_vec <tree, 8> bases_to_consider;
3111 odr_type type, outer_type;
3112 polymorphic_call_target_d key;
3113 polymorphic_call_target_d **slot;
3114 unsigned int i;
3115 tree binfo, target;
3116 bool complete;
3117 bool can_refer = false;
3118 bool skipped = false;
3120 otr_type = TYPE_MAIN_VARIANT (otr_type);
3122 /* If ODR is not initialized or the context is invalid, return empty
3123 incomplete list. */
3124 if (!odr_hash || context.invalid || !TYPE_BINFO (otr_type))
3126 if (completep)
3127 *completep = context.invalid;
3128 if (cache_token)
3129 *cache_token = NULL;
3130 return nodes;
3133 /* Do not bother to compute speculative info when user do not asks for it. */
3134 if (!speculative || !context.speculative_outer_type)
3135 context.clear_speculation ();
3137 type = get_odr_type (otr_type, true);
3139 /* Recording type variants would waste results cache. */
3140 gcc_assert (!context.outer_type
3141 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3143 /* Look up the outer class type we want to walk.
3144 If we fail to do so, the context is invalid. */
3145 if ((context.outer_type || context.speculative_outer_type)
3146 && !context.restrict_to_inner_class (otr_type))
3148 if (completep)
3149 *completep = true;
3150 if (cache_token)
3151 *cache_token = NULL;
3152 return nodes;
3154 gcc_assert (!context.invalid);
3156 /* Check that restrict_to_inner_class kept the main variant. */
3157 gcc_assert (!context.outer_type
3158 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3160 /* We canonicalize our query, so we do not need extra hashtable entries. */
3162 /* Without outer type, we have no use for offset. Just do the
3163 basic search from inner type. */
3164 if (!context.outer_type)
3165 context.clear_outer_type (otr_type);
3166 /* We need to update our hierarchy if the type does not exist. */
3167 outer_type = get_odr_type (context.outer_type, true);
3168 /* If the type is complete, there are no derivations. */
3169 if (TYPE_FINAL_P (outer_type->type))
3170 context.maybe_derived_type = false;
3172 /* Initialize query cache. */
3173 if (!cached_polymorphic_call_targets)
3175 cached_polymorphic_call_targets = new hash_set<cgraph_node *>;
3176 polymorphic_call_target_hash
3177 = new polymorphic_call_target_hash_type (23);
3178 if (!node_removal_hook_holder)
3180 node_removal_hook_holder =
3181 symtab->add_cgraph_removal_hook (&devirt_node_removal_hook, NULL);
3182 symtab->add_varpool_removal_hook (&devirt_variable_node_removal_hook,
3183 NULL);
3187 if (in_lto_p)
3189 if (context.outer_type != otr_type)
3190 context.outer_type
3191 = get_odr_type (context.outer_type, true)->type;
3192 if (context.speculative_outer_type)
3193 context.speculative_outer_type
3194 = get_odr_type (context.speculative_outer_type, true)->type;
3197 /* Look up cached answer. */
3198 key.type = type;
3199 key.otr_token = otr_token;
3200 key.speculative = speculative;
3201 key.context = context;
3202 slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
3203 if (cache_token)
3204 *cache_token = (void *)*slot;
3205 if (*slot)
3207 if (completep)
3208 *completep = (*slot)->complete;
3209 if ((*slot)->type_warning && final_warning_records)
3211 final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
3212 if (!final_warning_records->type_warnings
3213 [(*slot)->type_warning - 1].dyn_count.initialized_p ())
3214 final_warning_records->type_warnings
3215 [(*slot)->type_warning - 1].dyn_count = profile_count::zero ();
3216 if (final_warning_records->dyn_count > 0)
3217 final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
3218 = final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
3219 + final_warning_records->dyn_count;
3221 if (!speculative && (*slot)->decl_warning && final_warning_records)
3223 struct decl_warn_count *c =
3224 final_warning_records->decl_warnings.get ((*slot)->decl_warning);
3225 c->count++;
3226 if (final_warning_records->dyn_count > 0)
3227 c->dyn_count += final_warning_records->dyn_count;
3229 return (*slot)->targets;
3232 complete = true;
3234 /* Do actual search. */
3235 timevar_push (TV_IPA_VIRTUAL_CALL);
3236 *slot = XCNEW (polymorphic_call_target_d);
3237 if (cache_token)
3238 *cache_token = (void *)*slot;
3239 (*slot)->type = type;
3240 (*slot)->otr_token = otr_token;
3241 (*slot)->context = context;
3242 (*slot)->speculative = speculative;
3244 hash_set<tree> inserted;
3245 hash_set<tree> matched_vtables;
3247 /* First insert targets we speculatively identified as likely. */
3248 if (context.speculative_outer_type)
3250 odr_type speculative_outer_type;
3251 bool speculation_complete = true;
3253 /* First insert target from type itself and check if it may have
3254 derived types. */
3255 speculative_outer_type = get_odr_type (context.speculative_outer_type, true);
3256 if (TYPE_FINAL_P (speculative_outer_type->type))
3257 context.speculative_maybe_derived_type = false;
3258 binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
3259 context.speculative_offset, otr_type);
3260 if (binfo)
3261 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3262 &can_refer);
3263 else
3264 target = NULL;
3266 /* In the case we get complete method, we don't need
3267 to walk derivations. */
3268 if (target && DECL_FINAL_P (target))
3269 context.speculative_maybe_derived_type = false;
3270 if (type_possibly_instantiated_p (speculative_outer_type->type))
3271 maybe_record_node (nodes, target, &inserted, can_refer, &speculation_complete);
3272 if (binfo)
3273 matched_vtables.add (BINFO_VTABLE (binfo));
3276 /* Next walk recursively all derived types. */
3277 if (context.speculative_maybe_derived_type)
3278 for (i = 0; i < speculative_outer_type->derived_types.length(); i++)
3279 possible_polymorphic_call_targets_1 (nodes, &inserted,
3280 &matched_vtables,
3281 otr_type,
3282 speculative_outer_type->derived_types[i],
3283 otr_token, speculative_outer_type->type,
3284 context.speculative_offset,
3285 &speculation_complete,
3286 bases_to_consider,
3287 false);
3290 if (!speculative || !nodes.length ())
3292 /* First see virtual method of type itself. */
3293 binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
3294 context.offset, otr_type);
3295 if (binfo)
3296 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3297 &can_refer);
3298 else
3300 gcc_assert (odr_violation_reported);
3301 target = NULL;
3304 /* Destructors are never called through construction virtual tables,
3305 because the type is always known. */
3306 if (target && DECL_CXX_DESTRUCTOR_P (target))
3307 context.maybe_in_construction = false;
3309 if (target)
3311 /* In the case we get complete method, we don't need
3312 to walk derivations. */
3313 if (DECL_FINAL_P (target))
3314 context.maybe_derived_type = false;
3317 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3318 if (type_possibly_instantiated_p (outer_type->type))
3319 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3320 else
3321 skipped = true;
3323 if (binfo)
3324 matched_vtables.add (BINFO_VTABLE (binfo));
3326 /* Next walk recursively all derived types. */
3327 if (context.maybe_derived_type)
3329 for (i = 0; i < outer_type->derived_types.length(); i++)
3330 possible_polymorphic_call_targets_1 (nodes, &inserted,
3331 &matched_vtables,
3332 otr_type,
3333 outer_type->derived_types[i],
3334 otr_token, outer_type->type,
3335 context.offset, &complete,
3336 bases_to_consider,
3337 context.maybe_in_construction);
3339 if (!outer_type->all_derivations_known)
3341 if (!speculative && final_warning_records
3342 && nodes.length () == 1
3343 && TREE_CODE (TREE_TYPE (nodes[0]->decl)) == METHOD_TYPE)
3345 if (complete
3346 && warn_suggest_final_types
3347 && !outer_type->derived_types.length ())
3349 final_warning_records->grow_type_warnings
3350 (outer_type->id);
3351 final_warning_records->type_warnings[outer_type->id].count++;
3352 if (!final_warning_records->type_warnings
3353 [outer_type->id].dyn_count.initialized_p ())
3354 final_warning_records->type_warnings
3355 [outer_type->id].dyn_count = profile_count::zero ();
3356 final_warning_records->type_warnings[outer_type->id].dyn_count
3357 += final_warning_records->dyn_count;
3358 final_warning_records->type_warnings[outer_type->id].type
3359 = outer_type->type;
3360 (*slot)->type_warning = outer_type->id + 1;
3362 if (complete
3363 && warn_suggest_final_methods
3364 && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl),
3365 outer_type->type))
3367 bool existed;
3368 struct decl_warn_count &c =
3369 final_warning_records->decl_warnings.get_or_insert
3370 (nodes[0]->decl, &existed);
3372 if (existed)
3374 c.count++;
3375 c.dyn_count += final_warning_records->dyn_count;
3377 else
3379 c.count = 1;
3380 c.dyn_count = final_warning_records->dyn_count;
3381 c.decl = nodes[0]->decl;
3383 (*slot)->decl_warning = nodes[0]->decl;
3386 complete = false;
3390 if (!speculative)
3392 /* Destructors are never called through construction virtual tables,
3393 because the type is always known. One of entries may be
3394 cxa_pure_virtual so look to at least two of them. */
3395 if (context.maybe_in_construction)
3396 for (i =0 ; i < MIN (nodes.length (), 2); i++)
3397 if (DECL_CXX_DESTRUCTOR_P (nodes[i]->decl))
3398 context.maybe_in_construction = false;
3399 if (context.maybe_in_construction)
3401 if (type != outer_type
3402 && (!skipped
3403 || (context.maybe_derived_type
3404 && !type_all_derivations_known_p (outer_type->type))))
3405 record_targets_from_bases (otr_type, otr_token, outer_type->type,
3406 context.offset, nodes, &inserted,
3407 &matched_vtables, &complete);
3408 if (skipped)
3409 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3410 for (i = 0; i < bases_to_consider.length(); i++)
3411 maybe_record_node (nodes, bases_to_consider[i], &inserted, can_refer, &complete);
3416 (*slot)->targets = nodes;
3417 (*slot)->complete = complete;
3418 if (completep)
3419 *completep = complete;
3421 timevar_pop (TV_IPA_VIRTUAL_CALL);
3422 return nodes;
3425 bool
3426 add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value,
3427 vec<const decl_warn_count*> *vec)
3429 vec->safe_push (&value);
3430 return true;
3433 /* Dump target list TARGETS into FILE. */
3435 static void
3436 dump_targets (FILE *f, vec <cgraph_node *> targets)
3438 unsigned int i;
3440 for (i = 0; i < targets.length (); i++)
3442 char *name = NULL;
3443 if (in_lto_p)
3444 name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
3445 fprintf (f, " %s/%i", name ? name : targets[i]->name (),
3446 targets[i]->order);
3447 if (in_lto_p)
3448 free (name);
3449 if (!targets[i]->definition)
3450 fprintf (f, " (no definition%s)",
3451 DECL_DECLARED_INLINE_P (targets[i]->decl)
3452 ? " inline" : "");
3454 fprintf (f, "\n");
3457 /* Dump all possible targets of a polymorphic call. */
3459 void
3460 dump_possible_polymorphic_call_targets (FILE *f,
3461 tree otr_type,
3462 HOST_WIDE_INT otr_token,
3463 const ipa_polymorphic_call_context &ctx)
3465 vec <cgraph_node *> targets;
3466 bool final;
3467 odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false);
3468 unsigned int len;
3470 if (!type)
3471 return;
3472 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3473 ctx,
3474 &final, NULL, false);
3475 fprintf (f, " Targets of polymorphic call of type %i:", type->id);
3476 print_generic_expr (f, type->type, TDF_SLIM);
3477 fprintf (f, " token %i\n", (int)otr_token);
3479 ctx.dump (f);
3481 fprintf (f, " %s%s%s%s\n ",
3482 final ? "This is a complete list." :
3483 "This is partial list; extra targets may be defined in other units.",
3484 ctx.maybe_in_construction ? " (base types included)" : "",
3485 ctx.maybe_derived_type ? " (derived types included)" : "",
3486 ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
3487 len = targets.length ();
3488 dump_targets (f, targets);
3490 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3491 ctx,
3492 &final, NULL, true);
3493 if (targets.length () != len)
3495 fprintf (f, " Speculative targets:");
3496 dump_targets (f, targets);
3498 /* Ugly: during callgraph construction the target cache may get populated
3499 before all targets are found. While this is harmless (because all local
3500 types are discovered and only in those case we devirtualize fully and we
3501 don't do speculative devirtualization before IPA stage) it triggers
3502 assert here when dumping at that stage also populates the case with
3503 speculative targets. Quietly ignore this. */
3504 gcc_assert (symtab->state < IPA_SSA || targets.length () <= len);
3505 fprintf (f, "\n");
3509 /* Return true if N can be possibly target of a polymorphic call of
3510 OTR_TYPE/OTR_TOKEN. */
3512 bool
3513 possible_polymorphic_call_target_p (tree otr_type,
3514 HOST_WIDE_INT otr_token,
3515 const ipa_polymorphic_call_context &ctx,
3516 struct cgraph_node *n)
3518 vec <cgraph_node *> targets;
3519 unsigned int i;
3520 enum built_in_function fcode;
3521 bool final;
3523 if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
3524 && ((fcode = DECL_FUNCTION_CODE (n->decl)) == BUILT_IN_UNREACHABLE
3525 || fcode == BUILT_IN_TRAP))
3526 return true;
3528 if (is_cxa_pure_virtual_p (n->decl))
3529 return true;
3531 if (!odr_hash)
3532 return true;
3533 targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
3534 for (i = 0; i < targets.length (); i++)
3535 if (n->semantically_equivalent_p (targets[i]))
3536 return true;
3538 /* At a moment we allow middle end to dig out new external declarations
3539 as a targets of polymorphic calls. */
3540 if (!final && !n->definition)
3541 return true;
3542 return false;
3547 /* Return true if N can be possibly target of a polymorphic call of
3548 OBJ_TYPE_REF expression REF in STMT. */
3550 bool
3551 possible_polymorphic_call_target_p (tree ref,
3552 gimple *stmt,
3553 struct cgraph_node *n)
3555 ipa_polymorphic_call_context context (current_function_decl, ref, stmt);
3556 tree call_fn = gimple_call_fn (stmt);
3558 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn),
3559 tree_to_uhwi
3560 (OBJ_TYPE_REF_TOKEN (call_fn)),
3561 context,
3566 /* After callgraph construction new external nodes may appear.
3567 Add them into the graph. */
3569 void
3570 update_type_inheritance_graph (void)
3572 struct cgraph_node *n;
3574 if (!odr_hash)
3575 return;
3576 free_polymorphic_call_targets_hash ();
3577 timevar_push (TV_IPA_INHERITANCE);
3578 /* We reconstruct the graph starting from types of all methods seen in the
3579 unit. */
3580 FOR_EACH_FUNCTION (n)
3581 if (DECL_VIRTUAL_P (n->decl)
3582 && !n->definition
3583 && n->real_symbol_p ())
3584 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
3585 timevar_pop (TV_IPA_INHERITANCE);
3589 /* Return true if N looks like likely target of a polymorphic call.
3590 Rule out cxa_pure_virtual, noreturns, function declared cold and
3591 other obvious cases. */
3593 bool
3594 likely_target_p (struct cgraph_node *n)
3596 int flags;
3597 /* cxa_pure_virtual and similar things are not likely. */
3598 if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
3599 return false;
3600 flags = flags_from_decl_or_type (n->decl);
3601 if (flags & ECF_NORETURN)
3602 return false;
3603 if (lookup_attribute ("cold",
3604 DECL_ATTRIBUTES (n->decl)))
3605 return false;
3606 if (n->frequency < NODE_FREQUENCY_NORMAL)
3607 return false;
3608 /* If there are no live virtual tables referring the target,
3609 the only way the target can be called is an instance coming from other
3610 compilation unit; speculative devirtualization is built around an
3611 assumption that won't happen. */
3612 if (!referenced_from_vtable_p (n))
3613 return false;
3614 return true;
3617 /* Compare type warning records P1 and P2 and choose one with larger count;
3618 helper for qsort. */
3621 type_warning_cmp (const void *p1, const void *p2)
3623 const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
3624 const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
3626 if (t1->dyn_count < t2->dyn_count)
3627 return 1;
3628 if (t1->dyn_count > t2->dyn_count)
3629 return -1;
3630 return t2->count - t1->count;
3633 /* Compare decl warning records P1 and P2 and choose one with larger count;
3634 helper for qsort. */
3637 decl_warning_cmp (const void *p1, const void *p2)
3639 const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
3640 const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
3642 if (t1->dyn_count < t2->dyn_count)
3643 return 1;
3644 if (t1->dyn_count > t2->dyn_count)
3645 return -1;
3646 return t2->count - t1->count;
3650 /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
3651 context CTX. */
3653 struct cgraph_node *
3654 try_speculative_devirtualization (tree otr_type, HOST_WIDE_INT otr_token,
3655 ipa_polymorphic_call_context ctx)
3657 vec <cgraph_node *>targets
3658 = possible_polymorphic_call_targets
3659 (otr_type, otr_token, ctx, NULL, NULL, true);
3660 unsigned int i;
3661 struct cgraph_node *likely_target = NULL;
3663 for (i = 0; i < targets.length (); i++)
3664 if (likely_target_p (targets[i]))
3666 if (likely_target)
3667 return NULL;
3668 likely_target = targets[i];
3670 if (!likely_target
3671 ||!likely_target->definition
3672 || DECL_EXTERNAL (likely_target->decl))
3673 return NULL;
3675 /* Don't use an implicitly-declared destructor (c++/58678). */
3676 struct cgraph_node *non_thunk_target
3677 = likely_target->function_symbol ();
3678 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3679 return NULL;
3680 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3681 && likely_target->can_be_discarded_p ())
3682 return NULL;
3683 return likely_target;
3686 /* The ipa-devirt pass.
3687 When polymorphic call has only one likely target in the unit,
3688 turn it into a speculative call. */
3690 static unsigned int
3691 ipa_devirt (void)
3693 struct cgraph_node *n;
3694 hash_set<void *> bad_call_targets;
3695 struct cgraph_edge *e;
3697 int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
3698 int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
3699 int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
3700 int ndropped = 0;
3702 if (!odr_types_ptr)
3703 return 0;
3705 if (dump_file)
3706 dump_type_inheritance_graph (dump_file);
3708 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3709 This is implemented by setting up final_warning_records that are updated
3710 by get_polymorphic_call_targets.
3711 We need to clear cache in this case to trigger recomputation of all
3712 entries. */
3713 if (warn_suggest_final_methods || warn_suggest_final_types)
3715 final_warning_records = new (final_warning_record);
3716 final_warning_records->dyn_count = profile_count::zero ();
3717 final_warning_records->grow_type_warnings (odr_types.length ());
3718 free_polymorphic_call_targets_hash ();
3721 FOR_EACH_DEFINED_FUNCTION (n)
3723 bool update = false;
3724 if (!opt_for_fn (n->decl, flag_devirtualize))
3725 continue;
3726 if (dump_file && n->indirect_calls)
3727 fprintf (dump_file, "\n\nProcesing function %s\n",
3728 n->dump_name ());
3729 for (e = n->indirect_calls; e; e = e->next_callee)
3730 if (e->indirect_info->polymorphic)
3732 struct cgraph_node *likely_target = NULL;
3733 void *cache_token;
3734 bool final;
3736 if (final_warning_records)
3737 final_warning_records->dyn_count = e->count.ipa ();
3739 vec <cgraph_node *>targets
3740 = possible_polymorphic_call_targets
3741 (e, &final, &cache_token, true);
3742 unsigned int i;
3744 /* Trigger warnings by calculating non-speculative targets. */
3745 if (warn_suggest_final_methods || warn_suggest_final_types)
3746 possible_polymorphic_call_targets (e);
3748 if (dump_file)
3749 dump_possible_polymorphic_call_targets
3750 (dump_file, e);
3752 npolymorphic++;
3754 /* See if the call can be devirtualized by means of ipa-prop's
3755 polymorphic call context propagation. If not, we can just
3756 forget about this call being polymorphic and avoid some heavy
3757 lifting in remove_unreachable_nodes that will otherwise try to
3758 keep all possible targets alive until inlining and in the inliner
3759 itself.
3761 This may need to be revisited once we add further ways to use
3762 the may edges, but it is a resonable thing to do right now. */
3764 if ((e->indirect_info->param_index == -1
3765 || (!opt_for_fn (n->decl, flag_devirtualize_speculatively)
3766 && e->indirect_info->vptr_changed))
3767 && !flag_ltrans_devirtualize)
3769 e->indirect_info->polymorphic = false;
3770 ndropped++;
3771 if (dump_file)
3772 fprintf (dump_file, "Dropping polymorphic call info;"
3773 " it can not be used by ipa-prop\n");
3776 if (!opt_for_fn (n->decl, flag_devirtualize_speculatively))
3777 continue;
3779 if (!e->maybe_hot_p ())
3781 if (dump_file)
3782 fprintf (dump_file, "Call is cold\n\n");
3783 ncold++;
3784 continue;
3786 if (e->speculative)
3788 if (dump_file)
3789 fprintf (dump_file, "Call is already speculated\n\n");
3790 nspeculated++;
3792 /* When dumping see if we agree with speculation. */
3793 if (!dump_file)
3794 continue;
3796 if (bad_call_targets.contains (cache_token))
3798 if (dump_file)
3799 fprintf (dump_file, "Target list is known to be useless\n\n");
3800 nmultiple++;
3801 continue;
3803 for (i = 0; i < targets.length (); i++)
3804 if (likely_target_p (targets[i]))
3806 if (likely_target)
3808 likely_target = NULL;
3809 if (dump_file)
3810 fprintf (dump_file, "More than one likely target\n\n");
3811 nmultiple++;
3812 break;
3814 likely_target = targets[i];
3816 if (!likely_target)
3818 bad_call_targets.add (cache_token);
3819 continue;
3821 /* This is reached only when dumping; check if we agree or disagree
3822 with the speculation. */
3823 if (e->speculative)
3825 struct cgraph_edge *e2;
3826 struct ipa_ref *ref;
3827 e->speculative_call_info (e2, e, ref);
3828 if (e2->callee->ultimate_alias_target ()
3829 == likely_target->ultimate_alias_target ())
3831 fprintf (dump_file, "We agree with speculation\n\n");
3832 nok++;
3834 else
3836 fprintf (dump_file, "We disagree with speculation\n\n");
3837 nwrong++;
3839 continue;
3841 if (!likely_target->definition)
3843 if (dump_file)
3844 fprintf (dump_file, "Target is not a definition\n\n");
3845 nnotdefined++;
3846 continue;
3848 /* Do not introduce new references to external symbols. While we
3849 can handle these just well, it is common for programs to
3850 incorrectly with headers defining methods they are linked
3851 with. */
3852 if (DECL_EXTERNAL (likely_target->decl))
3854 if (dump_file)
3855 fprintf (dump_file, "Target is external\n\n");
3856 nexternal++;
3857 continue;
3859 /* Don't use an implicitly-declared destructor (c++/58678). */
3860 struct cgraph_node *non_thunk_target
3861 = likely_target->function_symbol ();
3862 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3864 if (dump_file)
3865 fprintf (dump_file, "Target is artificial\n\n");
3866 nartificial++;
3867 continue;
3869 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3870 && likely_target->can_be_discarded_p ())
3872 if (dump_file)
3873 fprintf (dump_file, "Target is overwritable\n\n");
3874 noverwritable++;
3875 continue;
3877 else if (dbg_cnt (devirt))
3879 if (dump_enabled_p ())
3881 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, e->call_stmt,
3882 "speculatively devirtualizing call "
3883 "in %s to %s\n",
3884 n->dump_name (),
3885 likely_target->dump_name ());
3887 if (!likely_target->can_be_discarded_p ())
3889 cgraph_node *alias;
3890 alias = dyn_cast<cgraph_node *> (likely_target->noninterposable_alias ());
3891 if (alias)
3892 likely_target = alias;
3894 nconverted++;
3895 update = true;
3896 e->make_speculative
3897 (likely_target, e->count.apply_scale (8, 10));
3900 if (update)
3901 ipa_update_overall_fn_summary (n);
3903 if (warn_suggest_final_methods || warn_suggest_final_types)
3905 if (warn_suggest_final_types)
3907 final_warning_records->type_warnings.qsort (type_warning_cmp);
3908 for (unsigned int i = 0;
3909 i < final_warning_records->type_warnings.length (); i++)
3910 if (final_warning_records->type_warnings[i].count)
3912 tree type = final_warning_records->type_warnings[i].type;
3913 int count = final_warning_records->type_warnings[i].count;
3914 profile_count dyn_count
3915 = final_warning_records->type_warnings[i].dyn_count;
3917 if (!(dyn_count > 0))
3918 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3919 OPT_Wsuggest_final_types, count,
3920 "Declaring type %qD final "
3921 "would enable devirtualization of %i call",
3922 "Declaring type %qD final "
3923 "would enable devirtualization of %i calls",
3924 type,
3925 count);
3926 else
3927 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3928 OPT_Wsuggest_final_types, count,
3929 "Declaring type %qD final "
3930 "would enable devirtualization of %i call "
3931 "executed %lli times",
3932 "Declaring type %qD final "
3933 "would enable devirtualization of %i calls "
3934 "executed %lli times",
3935 type,
3936 count,
3937 (long long) dyn_count.to_gcov_type ());
3941 if (warn_suggest_final_methods)
3943 auto_vec<const decl_warn_count*> decl_warnings_vec;
3945 final_warning_records->decl_warnings.traverse
3946 <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec);
3947 decl_warnings_vec.qsort (decl_warning_cmp);
3948 for (unsigned int i = 0; i < decl_warnings_vec.length (); i++)
3950 tree decl = decl_warnings_vec[i]->decl;
3951 int count = decl_warnings_vec[i]->count;
3952 profile_count dyn_count
3953 = decl_warnings_vec[i]->dyn_count;
3955 if (!(dyn_count > 0))
3956 if (DECL_CXX_DESTRUCTOR_P (decl))
3957 warning_n (DECL_SOURCE_LOCATION (decl),
3958 OPT_Wsuggest_final_methods, count,
3959 "Declaring virtual destructor of %qD final "
3960 "would enable devirtualization of %i call",
3961 "Declaring virtual destructor of %qD final "
3962 "would enable devirtualization of %i calls",
3963 DECL_CONTEXT (decl), count);
3964 else
3965 warning_n (DECL_SOURCE_LOCATION (decl),
3966 OPT_Wsuggest_final_methods, count,
3967 "Declaring method %qD final "
3968 "would enable devirtualization of %i call",
3969 "Declaring method %qD final "
3970 "would enable devirtualization of %i calls",
3971 decl, count);
3972 else if (DECL_CXX_DESTRUCTOR_P (decl))
3973 warning_n (DECL_SOURCE_LOCATION (decl),
3974 OPT_Wsuggest_final_methods, count,
3975 "Declaring virtual destructor of %qD final "
3976 "would enable devirtualization of %i call "
3977 "executed %lli times",
3978 "Declaring virtual destructor of %qD final "
3979 "would enable devirtualization of %i calls "
3980 "executed %lli times",
3981 DECL_CONTEXT (decl), count,
3982 (long long)dyn_count.to_gcov_type ());
3983 else
3984 warning_n (DECL_SOURCE_LOCATION (decl),
3985 OPT_Wsuggest_final_methods, count,
3986 "Declaring method %qD final "
3987 "would enable devirtualization of %i call "
3988 "executed %lli times",
3989 "Declaring method %qD final "
3990 "would enable devirtualization of %i calls "
3991 "executed %lli times",
3992 decl, count,
3993 (long long)dyn_count.to_gcov_type ());
3997 delete (final_warning_records);
3998 final_warning_records = 0;
4001 if (dump_file)
4002 fprintf (dump_file,
4003 "%i polymorphic calls, %i devirtualized,"
4004 " %i speculatively devirtualized, %i cold\n"
4005 "%i have multiple targets, %i overwritable,"
4006 " %i already speculated (%i agree, %i disagree),"
4007 " %i external, %i not defined, %i artificial, %i infos dropped\n",
4008 npolymorphic, ndevirtualized, nconverted, ncold,
4009 nmultiple, noverwritable, nspeculated, nok, nwrong,
4010 nexternal, nnotdefined, nartificial, ndropped);
4011 return ndevirtualized || ndropped ? TODO_remove_functions : 0;
4014 namespace {
4016 const pass_data pass_data_ipa_devirt =
4018 IPA_PASS, /* type */
4019 "devirt", /* name */
4020 OPTGROUP_NONE, /* optinfo_flags */
4021 TV_IPA_DEVIRT, /* tv_id */
4022 0, /* properties_required */
4023 0, /* properties_provided */
4024 0, /* properties_destroyed */
4025 0, /* todo_flags_start */
4026 ( TODO_dump_symtab ), /* todo_flags_finish */
4029 class pass_ipa_devirt : public ipa_opt_pass_d
4031 public:
4032 pass_ipa_devirt (gcc::context *ctxt)
4033 : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
4034 NULL, /* generate_summary */
4035 NULL, /* write_summary */
4036 NULL, /* read_summary */
4037 NULL, /* write_optimization_summary */
4038 NULL, /* read_optimization_summary */
4039 NULL, /* stmt_fixup */
4040 0, /* function_transform_todo_flags_start */
4041 NULL, /* function_transform */
4042 NULL) /* variable_transform */
4045 /* opt_pass methods: */
4046 virtual bool gate (function *)
4048 /* In LTO, always run the IPA passes and decide on function basis if the
4049 pass is enabled. */
4050 if (in_lto_p)
4051 return true;
4052 return (flag_devirtualize
4053 && (flag_devirtualize_speculatively
4054 || (warn_suggest_final_methods
4055 || warn_suggest_final_types))
4056 && optimize);
4059 virtual unsigned int execute (function *) { return ipa_devirt (); }
4061 }; // class pass_ipa_devirt
4063 } // anon namespace
4065 ipa_opt_pass_d *
4066 make_pass_ipa_devirt (gcc::context *ctxt)
4068 return new pass_ipa_devirt (ctxt);
4071 #include "gt-ipa-devirt.h"