Concretize gimple_cond_set_code
[official-gcc.git] / gcc / ipa-devirt.c
blob00fc6bb8285a293ebc9505afa46d4574b7761e92
1 /* Basic IPA utilities for type inheritance graph construction and
2 devirtualization.
3 Copyright (C) 2013-2014 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 vocalburary:
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++ frotend. 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 represention 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 "tm.h"
112 #include "tree.h"
113 #include "print-tree.h"
114 #include "calls.h"
115 #include "cgraph.h"
116 #include "expr.h"
117 #include "tree-pass.h"
118 #include "hash-set.h"
119 #include "target.h"
120 #include "hash-table.h"
121 #include "inchash.h"
122 #include "tree-pretty-print.h"
123 #include "ipa-utils.h"
124 #include "tree-ssa-alias.h"
125 #include "internal-fn.h"
126 #include "gimple-fold.h"
127 #include "gimple-expr.h"
128 #include "gimple.h"
129 #include "ipa-inline.h"
130 #include "diagnostic.h"
131 #include "tree-dfa.h"
132 #include "demangle.h"
133 #include "dbgcnt.h"
134 #include "gimple-pretty-print.h"
135 #include "stor-layout.h"
136 #include "intl.h"
137 #include "hash-map.h"
139 /* Hash based set of pairs of types. */
140 typedef struct
142 tree first;
143 tree second;
144 } type_pair;
146 struct pair_traits : default_hashset_traits
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,pair_traits> *);
178 static bool odr_violation_reported = false;
181 /* Pointer set of all call targets appearing in the cache. */
182 static hash_set<cgraph_node *> *cached_polymorphic_call_targets;
184 /* The node of type inheritance graph. For each type unique in
185 One Defintion Rule (ODR) sense, we produce one node linking all
186 main variants of types equivalent to it, bases and derived types. */
188 struct GTY(()) odr_type_d
190 /* leader type. */
191 tree type;
192 /* All bases; built only for main variants of types */
193 vec<odr_type> GTY((skip)) bases;
194 /* All derrived types with virtual methods seen in unit;
195 built only for main variants oftypes */
196 vec<odr_type> GTY((skip)) derived_types;
198 /* All equivalent types, if more than one. */
199 vec<tree, va_gc> *types;
200 /* Set of all equivalent types, if NON-NULL. */
201 hash_set<tree> * GTY((skip)) types_set;
203 /* Unique ID indexing the type in odr_types array. */
204 int id;
205 /* Is it in anonymous namespace? */
206 bool anonymous_namespace;
207 /* Do we know about all derivations of given type? */
208 bool all_derivations_known;
209 /* Did we report ODR violation here? */
210 bool odr_violated;
213 /* Return TRUE if all derived types of T are known and thus
214 we may consider the walk of derived type complete.
216 This is typically true only for final anonymous namespace types and types
217 defined within functions (that may be COMDAT and thus shared across units,
218 but with the same set of derived types). */
220 bool
221 type_all_derivations_known_p (const_tree t)
223 if (TYPE_FINAL_P (t))
224 return true;
225 if (flag_ltrans)
226 return false;
227 /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
228 if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL)
229 return true;
230 if (type_in_anonymous_namespace_p (t))
231 return true;
232 return (decl_function_context (TYPE_NAME (t)) != NULL);
235 /* Return TURE if type's constructors are all visible. */
237 static bool
238 type_all_ctors_visible_p (tree t)
240 return !flag_ltrans
241 && symtab->state >= CONSTRUCTION
242 /* We can not always use type_all_derivations_known_p.
243 For function local types we must assume case where
244 the function is COMDAT and shared in between units.
246 TODO: These cases are quite easy to get, but we need
247 to keep track of C++ privatizing via -Wno-weak
248 as well as the IPA privatizing. */
249 && type_in_anonymous_namespace_p (t);
252 /* Return TRUE if type may have instance. */
254 static bool
255 type_possibly_instantiated_p (tree t)
257 tree vtable;
258 varpool_node *vnode;
260 /* TODO: Add abstract types here. */
261 if (!type_all_ctors_visible_p (t))
262 return true;
264 vtable = BINFO_VTABLE (TYPE_BINFO (t));
265 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
266 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
267 vnode = varpool_node::get (vtable);
268 return vnode && vnode->definition;
271 /* One Definition Rule hashtable helpers. */
273 struct odr_hasher
275 typedef odr_type_d value_type;
276 typedef union tree_node compare_type;
277 static inline hashval_t hash (const value_type *);
278 static inline bool equal (const value_type *, const compare_type *);
279 static inline void remove (value_type *);
282 /* Return type that was declared with T's name so that T is an
283 qualified variant of it. */
285 static inline tree
286 main_odr_variant (const_tree t)
288 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
289 return TREE_TYPE (TYPE_NAME (t));
290 /* Unnamed types and non-C++ produced types can be compared by variants. */
291 else
292 return TYPE_MAIN_VARIANT (t);
295 /* Produce hash based on type name. */
297 static hashval_t
298 hash_type_name (tree t)
300 gcc_checking_assert (main_odr_variant (t) == t);
302 /* If not in LTO, all main variants are unique, so we can do
303 pointer hash. */
304 if (!in_lto_p)
305 return htab_hash_pointer (t);
307 /* Anonymous types are unique. */
308 if (type_in_anonymous_namespace_p (t))
309 return htab_hash_pointer (t);
311 /* ODR types have name specified. */
312 if (TYPE_NAME (t)
313 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)))
314 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t)));
316 /* For polymorphic types that was compiled with -fno-lto-odr-type-merging
317 we can simply hash the virtual table. */
318 if (TREE_CODE (t) == RECORD_TYPE
319 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)))
321 tree v = BINFO_VTABLE (TYPE_BINFO (t));
322 hashval_t hash = 0;
324 if (TREE_CODE (v) == POINTER_PLUS_EXPR)
326 hash = TREE_INT_CST_LOW (TREE_OPERAND (v, 1));
327 v = TREE_OPERAND (TREE_OPERAND (v, 0), 0);
330 v = DECL_ASSEMBLER_NAME (v);
331 hash = iterative_hash_hashval_t (hash, htab_hash_pointer (v));
332 return hash;
335 /* Builtin types may appear as main variants of ODR types and are unique.
336 Sanity check we do not get anything that looks non-builtin. */
337 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
338 || TREE_CODE (t) == VOID_TYPE
339 || TREE_CODE (t) == COMPLEX_TYPE
340 || TREE_CODE (t) == REAL_TYPE
341 || TREE_CODE (t) == POINTER_TYPE);
342 return htab_hash_pointer (t);
345 /* Return the computed hashcode for ODR_TYPE. */
347 inline hashval_t
348 odr_hasher::hash (const value_type *odr_type)
350 return hash_type_name (odr_type->type);
353 /* For languages with One Definition Rule, work out if
354 types are the same based on their name.
356 This is non-trivial for LTO where minnor differences in
357 the type representation may have prevented type merging
358 to merge two copies of otherwise equivalent type.
360 Until we start streaming mangled type names, this function works
361 only for polymorphic types. */
363 bool
364 types_same_for_odr (const_tree type1, const_tree type2)
366 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
368 type1 = main_odr_variant (type1);
369 type2 = main_odr_variant (type2);
371 if (type1 == type2)
372 return true;
374 if (!in_lto_p)
375 return false;
377 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
378 on the corresponding TYPE_STUB_DECL. */
379 if (type_in_anonymous_namespace_p (type1)
380 || type_in_anonymous_namespace_p (type2))
381 return false;
384 /* ODR name of the type is set in DECL_ASSEMBLER_NAME of its TYPE_NAME.
386 Ideally we should never meed types without ODR names here. It can however
387 happen in two cases:
389 1) for builtin types that are not streamed but rebuilt in lto/lto-lang.c
390 Here testing for equivalence is safe, since their MAIN_VARIANTs are
391 unique.
392 2) for units streamed with -fno-lto-odr-type-merging. Here we can't
393 establish precise ODR equivalency, but for correctness we care only
394 about equivalency on complete polymorphic types. For these we can
395 compare assembler names of their virtual tables. */
396 if ((!TYPE_NAME (type1) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type1)))
397 || (!TYPE_NAME (type2) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type2))))
399 /* See if types are obvoiusly different (i.e. different codes
400 or polymorphis wrt non-polymorphic). This is not strictly correct
401 for ODR violating programs, but we can't do better without streaming
402 ODR names. */
403 if (TREE_CODE (type1) != TREE_CODE (type2))
404 return false;
405 if (TREE_CODE (type1) == RECORD_TYPE
406 && (TYPE_BINFO (type1) == NULL_TREE) != (TYPE_BINFO (type1) == NULL_TREE))
407 return false;
408 if (TREE_CODE (type1) == RECORD_TYPE && TYPE_BINFO (type1)
409 && (BINFO_VTABLE (TYPE_BINFO (type1)) == NULL_TREE)
410 != (BINFO_VTABLE (TYPE_BINFO (type2)) == NULL_TREE))
411 return false;
413 /* At the moment we have no way to establish ODR equivlaence at LTO
414 other than comparing virtual table pointrs of polymorphic types.
415 Eventually we should start saving mangled names in TYPE_NAME.
416 Then this condition will become non-trivial. */
418 if (TREE_CODE (type1) == RECORD_TYPE
419 && TYPE_BINFO (type1) && TYPE_BINFO (type2)
420 && BINFO_VTABLE (TYPE_BINFO (type1))
421 && BINFO_VTABLE (TYPE_BINFO (type2)))
423 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
424 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
425 gcc_assert (TREE_CODE (v1) == POINTER_PLUS_EXPR
426 && TREE_CODE (v2) == POINTER_PLUS_EXPR);
427 return (operand_equal_p (TREE_OPERAND (v1, 1),
428 TREE_OPERAND (v2, 1), 0)
429 && DECL_ASSEMBLER_NAME
430 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
431 == DECL_ASSEMBLER_NAME
432 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
434 gcc_unreachable ();
436 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1))
437 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2)));
440 /* Return true if we can decide on ODR equivalency.
442 In non-LTO it is always decide, in LTO however it depends in the type has
443 ODR info attached. */
445 bool
446 types_odr_comparable (tree t1, tree t2)
448 return (!in_lto_p
449 || main_odr_variant (t1) == main_odr_variant (t2)
450 || (odr_type_p (t1) && odr_type_p (t2))
451 || (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE
452 && TYPE_BINFO (t1) && TYPE_BINFO (t2)
453 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
454 && polymorphic_type_binfo_p (TYPE_BINFO (t2))));
457 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
458 known, be conservative and return false. */
460 bool
461 types_must_be_same_for_odr (tree t1, tree t2)
463 if (types_odr_comparable (t1, t2))
464 return types_same_for_odr (t1, t2);
465 else
466 return main_odr_variant (t1) == main_odr_variant (t2);
469 /* Compare types T1 and T2 and return true if they are
470 equivalent. */
472 inline bool
473 odr_hasher::equal (const value_type *t1, const compare_type *ct2)
475 tree t2 = const_cast <tree> (ct2);
477 gcc_checking_assert (main_odr_variant (t2) == t2);
478 if (t1->type == t2)
479 return true;
480 if (!in_lto_p)
481 return false;
482 return types_same_for_odr (t1->type, t2);
485 /* Free ODR type V. */
487 inline void
488 odr_hasher::remove (value_type *v)
490 v->bases.release ();
491 v->derived_types.release ();
492 if (v->types_set)
493 delete v->types_set;
494 ggc_free (v);
497 /* ODR type hash used to lookup ODR type based on tree type node. */
499 typedef hash_table<odr_hasher> odr_hash_type;
500 static odr_hash_type *odr_hash;
502 /* ODR types are also stored into ODR_TYPE vector to allow consistent
503 walking. Bases appear before derived types. Vector is garbage collected
504 so we won't end up visiting empty types. */
506 static GTY(()) vec <odr_type, va_gc> *odr_types_ptr;
507 #define odr_types (*odr_types_ptr)
509 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
510 void
511 set_type_binfo (tree type, tree binfo)
513 for (; type; type = TYPE_NEXT_VARIANT (type))
514 if (COMPLETE_TYPE_P (type))
515 TYPE_BINFO (type) = binfo;
516 else
517 gcc_assert (!TYPE_BINFO (type));
520 /* Compare T2 and T2 based on name or structure. */
522 static bool
523 odr_subtypes_equivalent_p (tree t1, tree t2, hash_set<type_pair,pair_traits> *visited)
525 bool an1, an2;
527 /* This can happen in incomplete types that should be handled earlier. */
528 gcc_assert (t1 && t2);
530 t1 = main_odr_variant (t1);
531 t2 = main_odr_variant (t2);
532 if (t1 == t2)
533 return true;
535 /* Anonymous namespace types must match exactly. */
536 an1 = type_in_anonymous_namespace_p (t1);
537 an2 = type_in_anonymous_namespace_p (t2);
538 if (an1 != an2 || an1)
539 return false;
541 /* For ODR types be sure to compare their names.
542 To support -wno-odr-type-merging we allow one type to be non-ODR
543 and other ODR even though it is a violation. */
544 if (types_odr_comparable (t1, t2))
546 if (!types_same_for_odr (t1, t2))
547 return false;
548 /* Limit recursion: If subtypes are ODR types and we know
549 that they are same, be happy. */
550 if (!get_odr_type (t1, true)->odr_violated)
551 return true;
554 /* Component types, builtins and possibly vioalting ODR types
555 have to be compared structurally. */
556 if (TREE_CODE (t1) != TREE_CODE (t2))
557 return false;
558 if ((TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
559 return false;
560 if (TYPE_NAME (t1) && DECL_NAME (TYPE_NAME (t1)) != DECL_NAME (TYPE_NAME (t2)))
561 return false;
563 type_pair pair={t1,t2};
564 if (TYPE_UID (t1) > TYPE_UID (t2))
566 pair.first = t2;
567 pair.second = t1;
569 if (visited->add (pair))
570 return true;
571 return odr_types_equivalent_p (t1, t2, false, NULL, visited);
574 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
575 violation warings. */
577 void
578 compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
580 int n1, n2;
581 if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl))
583 odr_violation_reported = true;
584 if (DECL_VIRTUAL_P (prevailing->decl))
586 varpool_node *tmp = prevailing;
587 prevailing = vtable;
588 vtable = tmp;
590 if (warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
591 OPT_Wodr,
592 "virtual table of type %qD violates one definition rule",
593 DECL_CONTEXT (vtable->decl)))
594 inform (DECL_SOURCE_LOCATION (prevailing->decl),
595 "variable of same assembler name as the virtual table is "
596 "defined in another translation unit");
597 return;
599 if (!prevailing->definition || !vtable->definition)
600 return;
601 for (n1 = 0, n2 = 0; true; n1++, n2++)
603 struct ipa_ref *ref1, *ref2;
604 bool end1, end2;
605 end1 = !prevailing->iterate_reference (n1, ref1);
606 end2 = !vtable->iterate_reference (n2, ref2);
607 if (end1 && end2)
608 return;
609 if (!end1 && !end2
610 && DECL_ASSEMBLER_NAME (ref1->referred->decl)
611 != DECL_ASSEMBLER_NAME (ref2->referred->decl)
612 && !n2
613 && !DECL_VIRTUAL_P (ref2->referred->decl)
614 && DECL_VIRTUAL_P (ref1->referred->decl))
616 if (warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 0,
617 "virtual table of type %qD contains RTTI information",
618 DECL_CONTEXT (vtable->decl)))
620 inform (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
621 "but is prevailed by one without from other translation unit");
622 inform (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
623 "RTTI will not work on this type");
625 n2++;
626 end2 = !vtable->iterate_reference (n2, ref2);
628 if (!end1 && !end2
629 && DECL_ASSEMBLER_NAME (ref1->referred->decl)
630 != DECL_ASSEMBLER_NAME (ref2->referred->decl)
631 && !n1
632 && !DECL_VIRTUAL_P (ref1->referred->decl)
633 && DECL_VIRTUAL_P (ref2->referred->decl))
635 n1++;
636 end1 = !vtable->iterate_reference (n1, ref1);
638 if (end1 || end2)
640 if (end1)
642 varpool_node *tmp = prevailing;
643 prevailing = vtable;
644 vtable = tmp;
645 ref1 = ref2;
647 if (warning_at (DECL_SOURCE_LOCATION
648 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 0,
649 "virtual table of type %qD violates "
650 "one definition rule",
651 DECL_CONTEXT (vtable->decl)))
653 inform (DECL_SOURCE_LOCATION
654 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
655 "the conflicting type defined in another translation "
656 "unit");
657 inform (DECL_SOURCE_LOCATION
658 (TYPE_NAME (DECL_CONTEXT (ref1->referring->decl))),
659 "contains additional virtual method %qD",
660 ref1->referred->decl);
662 return;
664 if (DECL_ASSEMBLER_NAME (ref1->referred->decl)
665 != DECL_ASSEMBLER_NAME (ref2->referred->decl))
667 if (warning_at (DECL_SOURCE_LOCATION
668 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 0,
669 "virtual table of type %qD violates "
670 "one definition rule ",
671 DECL_CONTEXT (vtable->decl)))
673 inform (DECL_SOURCE_LOCATION
674 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
675 "the conflicting type defined in another translation "
676 "unit");
677 inform (DECL_SOURCE_LOCATION (ref1->referred->decl),
678 "virtual method %qD", ref1->referred->decl);
679 inform (DECL_SOURCE_LOCATION (ref2->referred->decl),
680 "ought to match virtual method %qD but does not",
681 ref2->referred->decl);
682 return;
688 /* Output ODR violation warning about T1 and T2 with REASON.
689 Display location of ST1 and ST2 if REASON speaks about field or
690 method of the type.
691 If WARN is false, do nothing. Set WARNED if warning was indeed
692 output. */
694 void
695 warn_odr (tree t1, tree t2, tree st1, tree st2,
696 bool warn, bool *warned, const char *reason)
698 tree decl2 = TYPE_NAME (t2);
700 if (!warn)
701 return;
702 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr,
703 "type %qT violates one definition rule",
704 t1))
705 return;
706 if (!st1 && !st2)
708 /* For FIELD_DECL support also case where one of fields is
709 NULL - this is used when the structures have mismatching number of
710 elements. */
711 else if (!st1 || TREE_CODE (st1) == FIELD_DECL)
713 inform (DECL_SOURCE_LOCATION (decl2),
714 "a different type is defined in another translation unit");
715 if (!st1)
717 st1 = st2;
718 st2 = NULL;
720 inform (DECL_SOURCE_LOCATION (st1),
721 "the first difference of corresponding definitions is field %qD",
722 st1);
723 if (st2)
724 decl2 = st2;
726 else if (TREE_CODE (st1) == FUNCTION_DECL)
728 inform (DECL_SOURCE_LOCATION (decl2),
729 "a different type is defined in another translation unit");
730 inform (DECL_SOURCE_LOCATION (st1),
731 "the first difference of corresponding definitions is method %qD",
732 st1);
733 decl2 = st2;
735 else
736 return;
737 inform (DECL_SOURCE_LOCATION (decl2), reason);
739 if (warned)
740 *warned = true;
743 /* We already warned about ODR mismatch. T1 and T2 ought to be equivalent
744 because they are used on same place in ODR matching types.
745 They are not; inform the user. */
747 void
748 warn_types_mismatch (tree t1, tree t2)
750 if (!TYPE_NAME (t1) || !TYPE_NAME (t2))
751 return;
752 /* In Firefox it is a common bug to have same types but in
753 different namespaces. Be a bit more informative on
754 this. */
755 if (TYPE_CONTEXT (t1) && TYPE_CONTEXT (t2)
756 && (((TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL)
757 != (TREE_CODE (TYPE_CONTEXT (t2)) == NAMESPACE_DECL))
758 || (TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL
759 && (DECL_NAME (TYPE_CONTEXT (t1)) !=
760 DECL_NAME (TYPE_CONTEXT (t2))))))
761 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
762 "type %qT should match type %qT but is defined "
763 "in different namespace ",
764 t1, t2);
765 else
766 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
767 "type %qT should match type %qT",
768 t1, t2);
769 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
770 "the incompatible type is defined here");
773 /* Compare T1 and T2, report ODR violations if WARN is true and set
774 WARNED to true if anything is reported. Return true if types match.
775 If true is returned, the types are also compatible in the sense of
776 gimple_canonical_types_compatible_p. */
778 static bool
779 odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned, hash_set<type_pair,pair_traits> *visited)
781 /* Check first for the obvious case of pointer identity. */
782 if (t1 == t2)
783 return true;
784 gcc_assert (!type_in_anonymous_namespace_p (t1));
785 gcc_assert (!type_in_anonymous_namespace_p (t2));
787 /* Can't be the same type if the types don't have the same code. */
788 if (TREE_CODE (t1) != TREE_CODE (t2))
790 warn_odr (t1, t2, NULL, NULL, warn, warned,
791 G_("a different type is defined in another translation unit"));
792 return false;
795 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
797 warn_odr (t1, t2, NULL, NULL, warn, warned,
798 G_("a type with different qualifiers is defined in another "
799 "translation unit"));
800 return false;
803 if (comp_type_attributes (t1, t2) != 1)
805 warn_odr (t1, t2, NULL, NULL, warn, warned,
806 G_("a type with attributes "
807 "is defined in another translation unit"));
808 return false;
811 if (TREE_CODE (t1) == ENUMERAL_TYPE)
813 tree v1, v2;
814 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
815 v1 && v2 ; v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
817 if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
819 warn_odr (t1, t2, NULL, NULL, warn, warned,
820 G_("an enum with different value name"
821 " is defined in another translation unit"));
822 return false;
824 if (TREE_VALUE (v1) != TREE_VALUE (v2)
825 && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1)),
826 DECL_INITIAL (TREE_VALUE (v2)), 0))
828 warn_odr (t1, t2, NULL, NULL, warn, warned,
829 G_("an enum with different values is defined"
830 " in another translation unit"));
831 return false;
834 if (v1 || v2)
836 warn_odr (t1, t2, NULL, NULL, warn, warned,
837 G_("an enum with mismatching number of values "
838 "is defined in another translation unit"));
839 return false;
843 /* Non-aggregate types can be handled cheaply. */
844 if (INTEGRAL_TYPE_P (t1)
845 || SCALAR_FLOAT_TYPE_P (t1)
846 || FIXED_POINT_TYPE_P (t1)
847 || TREE_CODE (t1) == VECTOR_TYPE
848 || TREE_CODE (t1) == COMPLEX_TYPE
849 || TREE_CODE (t1) == OFFSET_TYPE
850 || POINTER_TYPE_P (t1))
852 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
854 warn_odr (t1, t2, NULL, NULL, warn, warned,
855 G_("a type with different precision is defined "
856 "in another translation unit"));
857 return false;
859 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
861 warn_odr (t1, t2, NULL, NULL, warn, warned,
862 G_("a type with different signedness is defined "
863 "in another translation unit"));
864 return false;
867 if (TREE_CODE (t1) == INTEGER_TYPE
868 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
870 /* char WRT uint_8? */
871 warn_odr (t1, t2, NULL, NULL, warn, warned,
872 G_("a different type is defined in another "
873 "translation unit"));
874 return false;
877 /* For canonical type comparisons we do not want to build SCCs
878 so we cannot compare pointed-to types. But we can, for now,
879 require the same pointed-to type kind and match what
880 useless_type_conversion_p would do. */
881 if (POINTER_TYPE_P (t1))
883 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
884 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
886 warn_odr (t1, t2, NULL, NULL, warn, warned,
887 G_("it is defined as a pointer in different address "
888 "space in another translation unit"));
889 return false;
892 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
894 warn_odr (t1, t2, NULL, NULL, warn, warned,
895 G_("it is defined as a pointer to different type "
896 "in another translation unit"));
897 if (warn && warned)
898 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
899 return false;
903 if ((TREE_CODE (t1) == VECTOR_TYPE || TREE_CODE (t1) == COMPLEX_TYPE)
904 && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
906 /* Probably specific enough. */
907 warn_odr (t1, t2, NULL, NULL, warn, warned,
908 G_("a different type is defined "
909 "in another translation unit"));
910 if (warn && warned)
911 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
912 return false;
915 /* Do type-specific comparisons. */
916 else switch (TREE_CODE (t1))
918 case ARRAY_TYPE:
920 /* Array types are the same if the element types are the same and
921 the number of elements are the same. */
922 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
924 warn_odr (t1, t2, NULL, NULL, warn, warned,
925 G_("a different type is defined in another "
926 "translation unit"));
927 if (warn && warned)
928 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
930 gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
931 gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
932 == TYPE_NONALIASED_COMPONENT (t2));
934 tree i1 = TYPE_DOMAIN (t1);
935 tree i2 = TYPE_DOMAIN (t2);
937 /* For an incomplete external array, the type domain can be
938 NULL_TREE. Check this condition also. */
939 if (i1 == NULL_TREE || i2 == NULL_TREE)
940 return true;
942 tree min1 = TYPE_MIN_VALUE (i1);
943 tree min2 = TYPE_MIN_VALUE (i2);
944 tree max1 = TYPE_MAX_VALUE (i1);
945 tree max2 = TYPE_MAX_VALUE (i2);
947 /* In C++, minimums should be always 0. */
948 gcc_assert (min1 == min2);
949 if (!operand_equal_p (max1, max2, 0))
951 warn_odr (t1, t2, NULL, NULL, warn, warned,
952 G_("an array of different size is defined "
953 "in another translation unit"));
954 return false;
957 break;
959 case METHOD_TYPE:
960 case FUNCTION_TYPE:
961 /* Function types are the same if the return type and arguments types
962 are the same. */
963 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
965 warn_odr (t1, t2, NULL, NULL, warn, warned,
966 G_("has different return value "
967 "in another translation unit"));
968 if (warn && warned)
969 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
970 return false;
973 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
974 return true;
975 else
977 tree parms1, parms2;
979 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
980 parms1 && parms2;
981 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
983 if (!odr_subtypes_equivalent_p
984 (TREE_VALUE (parms1), TREE_VALUE (parms2), visited))
986 warn_odr (t1, t2, NULL, NULL, warn, warned,
987 G_("has different parameters in another "
988 "translation unit"));
989 if (warn && warned)
990 warn_types_mismatch (TREE_VALUE (parms1),
991 TREE_VALUE (parms2));
992 return false;
996 if (parms1 || parms2)
998 warn_odr (t1, t2, NULL, NULL, warn, warned,
999 G_("has different parameters "
1000 "in another translation unit"));
1001 return false;
1004 return true;
1007 case RECORD_TYPE:
1008 case UNION_TYPE:
1009 case QUAL_UNION_TYPE:
1011 tree f1, f2;
1013 /* For aggregate types, all the fields must be the same. */
1014 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1016 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1017 f1 || f2;
1018 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1020 /* Skip non-fields. */
1021 while (f1 && TREE_CODE (f1) != FIELD_DECL)
1022 f1 = TREE_CHAIN (f1);
1023 while (f2 && TREE_CODE (f2) != FIELD_DECL)
1024 f2 = TREE_CHAIN (f2);
1025 if (!f1 || !f2)
1026 break;
1027 if (DECL_ARTIFICIAL (f1) != DECL_ARTIFICIAL (f2))
1028 break;
1029 if (DECL_NAME (f1) != DECL_NAME (f2)
1030 && !DECL_ARTIFICIAL (f1))
1032 warn_odr (t1, t2, f1, f2, warn, warned,
1033 G_("a field with different name is defined "
1034 "in another translation unit"));
1035 return false;
1037 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1), TREE_TYPE (f2), visited))
1039 /* Do not warn about artificial fields and just go into generic
1040 field mismatch warning. */
1041 if (DECL_ARTIFICIAL (f1))
1042 break;
1044 warn_odr (t1, t2, f1, f2, warn, warned,
1045 G_("a field of same name but different type "
1046 "is defined in another translation unit"));
1047 if (warn && warned)
1048 warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2));
1049 return false;
1051 if (!gimple_compare_field_offset (f1, f2))
1053 /* Do not warn about artificial fields and just go into generic
1054 field mismatch warning. */
1055 if (DECL_ARTIFICIAL (f1))
1056 break;
1057 warn_odr (t1, t2, t1, t2, warn, warned,
1058 G_("fields has different layout "
1059 "in another translation unit"));
1060 return false;
1062 gcc_assert (DECL_NONADDRESSABLE_P (f1)
1063 == DECL_NONADDRESSABLE_P (f2));
1066 /* If one aggregate has more fields than the other, they
1067 are not the same. */
1068 if (f1 || f2)
1070 if (f1 && DECL_ARTIFICIAL (f1))
1071 f1 = NULL;
1072 if (f2 && DECL_ARTIFICIAL (f2))
1073 f2 = NULL;
1074 if (f1 || f2)
1075 warn_odr (t1, t2, f1, f2, warn, warned,
1076 G_("a type with different number of fields "
1077 "is defined in another translation unit"));
1078 /* Ideally we should never get this generic message. */
1079 else
1080 warn_odr (t1, t2, f1, f2, warn, warned,
1081 G_("a type with different memory representation "
1082 "is defined in another translation unit"));
1084 return false;
1086 if ((TYPE_MAIN_VARIANT (t1) == t1 || TYPE_MAIN_VARIANT (t2) == t2)
1087 && (TYPE_METHODS (TYPE_MAIN_VARIANT (t1))
1088 != TYPE_METHODS (TYPE_MAIN_VARIANT (t2))))
1090 for (f1 = TYPE_METHODS (TYPE_MAIN_VARIANT (t1)),
1091 f2 = TYPE_METHODS (TYPE_MAIN_VARIANT (t2));
1092 f1 && f2 ; f1 = DECL_CHAIN (f1), f2 = DECL_CHAIN (f2))
1094 if (DECL_ASSEMBLER_NAME (f1) != DECL_ASSEMBLER_NAME (f2))
1096 warn_odr (t1, t2, f1, f2, warn, warned,
1097 G_("a different method of same type "
1098 "is defined in another translation unit"));
1099 return false;
1101 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1103 warn_odr (t1, t2, f1, f2, warn, warned,
1104 G_("s definition that differs by virtual "
1105 "keyword in another translation unit"));
1106 return false;
1108 if (DECL_VINDEX (f1) != DECL_VINDEX (f2))
1110 warn_odr (t1, t2, f1, f2, warn, warned,
1111 G_("virtual table layout differs in another "
1112 "translation unit"));
1113 return false;
1115 if (odr_subtypes_equivalent_p (TREE_TYPE (f1), TREE_TYPE (f2), visited))
1117 warn_odr (t1, t2, f1, f2, warn, warned,
1118 G_("method with incompatible type is defined "
1119 "in another translation unit"));
1120 return false;
1123 if (f1 || f2)
1125 warn_odr (t1, t2, NULL, NULL, warn, warned,
1126 G_("a type with different number of methods "
1127 "is defined in another translation unit"));
1128 return false;
1132 break;
1134 case VOID_TYPE:
1135 break;
1137 default:
1138 debug_tree (t1);
1139 gcc_unreachable ();
1142 /* Those are better to come last as they are utterly uninformative. */
1143 if (TYPE_SIZE (t1) && TYPE_SIZE (t2)
1144 && !operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0))
1146 warn_odr (t1, t2, NULL, NULL, warn, warned,
1147 G_("a type with different size "
1148 "is defined in another translation unit"));
1149 return false;
1151 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
1152 && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
1154 warn_odr (t1, t2, NULL, NULL, warn, warned,
1155 G_("a type with different alignment "
1156 "is defined in another translation unit"));
1157 return false;
1159 gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2)
1160 || operand_equal_p (TYPE_SIZE_UNIT (t1),
1161 TYPE_SIZE_UNIT (t2), 0));
1162 return true;
1165 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1166 from VAL->type. This may happen in LTO where tree merging did not merge
1167 all variants of the same type. It may or may not mean the ODR violation.
1168 Add it to the list of duplicates and warn on some violations. */
1170 static bool
1171 add_type_duplicate (odr_type val, tree type)
1173 bool build_bases = false;
1174 if (!val->types_set)
1175 val->types_set = new hash_set<tree>;
1177 /* Always prefer complete type to be the leader. */
1178 if (!COMPLETE_TYPE_P (val->type)
1179 && COMPLETE_TYPE_P (type))
1181 tree tmp = type;
1183 build_bases = true;
1184 type = val->type;
1185 val->type = tmp;
1188 /* See if this duplicate is new. */
1189 if (!val->types_set->add (type))
1191 bool merge = true;
1192 bool base_mismatch = false;
1193 unsigned int i,j;
1194 bool warned = false;
1195 hash_set<type_pair,pair_traits> visited;
1197 gcc_assert (in_lto_p);
1198 vec_safe_push (val->types, type);
1200 /* First we compare memory layout. */
1201 if (!odr_types_equivalent_p (val->type, type, !flag_ltrans && !val->odr_violated,
1202 &warned, &visited))
1204 merge = false;
1205 odr_violation_reported = true;
1206 val->odr_violated = true;
1207 if (symtab->dump_file)
1209 fprintf (symtab->dump_file, "ODR violation\n");
1211 print_node (symtab->dump_file, "", val->type, 0);
1212 putc ('\n',symtab->dump_file);
1213 print_node (symtab->dump_file, "", type, 0);
1214 putc ('\n',symtab->dump_file);
1218 /* Next sanity check that bases are the same. If not, we will end
1219 up producing wrong answers. */
1220 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1221 && TREE_CODE (val->type) == RECORD_TYPE
1222 && TREE_CODE (type) == RECORD_TYPE
1223 && TYPE_BINFO (val->type) && TYPE_BINFO (type))
1225 for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1226 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (TYPE_BINFO (type), i)))
1228 odr_type base = get_odr_type
1229 (BINFO_TYPE
1230 (BINFO_BASE_BINFO (TYPE_BINFO (type),
1231 i)),
1232 true);
1233 if (val->bases.length () <= j || val->bases[j] != base)
1234 base_mismatch = true;
1235 j++;
1237 if (base_mismatch)
1239 merge = false;
1240 odr_violation_reported = true;
1242 if (!warned && !val->odr_violated)
1243 warn_odr (type, val->type, NULL, NULL, !warned, &warned,
1244 "a type with the same name but different bases is "
1245 "defined in another translation unit");
1246 val->odr_violated = true;
1247 if (symtab->dump_file)
1249 fprintf (symtab->dump_file, "ODR bse violation or merging bug?\n");
1251 print_node (symtab->dump_file, "", val->type, 0);
1252 putc ('\n',symtab->dump_file);
1253 print_node (symtab->dump_file, "", type, 0);
1254 putc ('\n',symtab->dump_file);
1259 /* Regularize things a little. During LTO same types may come with
1260 different BINFOs. Either because their virtual table was
1261 not merged by tree merging and only later at decl merging or
1262 because one type comes with external vtable, while other
1263 with internal. We want to merge equivalent binfos to conserve
1264 memory and streaming overhead.
1266 The external vtables are more harmful: they contain references
1267 to external declarations of methods that may be defined in the
1268 merged LTO unit. For this reason we absolutely need to remove
1269 them and replace by internal variants. Not doing so will lead
1270 to incomplete answers from possible_polymorphic_call_targets.
1272 FIXME: disable for now; because ODR types are now build during
1273 streaming in, the variants do not need to be linked to the type,
1274 yet. We need to do the merging in cleanup pass to be implemented
1275 soon. */
1276 if (!flag_ltrans && merge
1277 && 0
1278 && TREE_CODE (val->type) == RECORD_TYPE
1279 && TREE_CODE (type) == RECORD_TYPE
1280 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1281 && TYPE_MAIN_VARIANT (type) == type
1282 && TYPE_MAIN_VARIANT (val->type) == val->type
1283 && BINFO_VTABLE (TYPE_BINFO (val->type))
1284 && BINFO_VTABLE (TYPE_BINFO (type)))
1286 tree master_binfo = TYPE_BINFO (val->type);
1287 tree v1 = BINFO_VTABLE (master_binfo);
1288 tree v2 = BINFO_VTABLE (TYPE_BINFO (type));
1290 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
1292 gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR
1293 && operand_equal_p (TREE_OPERAND (v1, 1),
1294 TREE_OPERAND (v2, 1), 0));
1295 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
1296 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
1298 gcc_assert (DECL_ASSEMBLER_NAME (v1)
1299 == DECL_ASSEMBLER_NAME (v2));
1301 if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2))
1303 unsigned int i;
1305 set_type_binfo (val->type, TYPE_BINFO (type));
1306 for (i = 0; i < val->types->length (); i++)
1308 if (TYPE_BINFO ((*val->types)[i])
1309 == master_binfo)
1310 set_type_binfo ((*val->types)[i], TYPE_BINFO (type));
1312 BINFO_TYPE (TYPE_BINFO (type)) = val->type;
1314 else
1315 set_type_binfo (type, master_binfo);
1318 return build_bases;
1321 /* Get ODR type hash entry for TYPE. If INSERT is true, create
1322 possibly new entry. */
1324 odr_type
1325 get_odr_type (tree type, bool insert)
1327 odr_type_d **slot;
1328 odr_type val;
1329 hashval_t hash;
1330 bool build_bases = false;
1331 bool insert_to_odr_array = false;
1332 int base_id = -1;
1334 type = main_odr_variant (type);
1336 hash = hash_type_name (type);
1337 slot = odr_hash->find_slot_with_hash (type, hash,
1338 insert ? INSERT : NO_INSERT);
1339 if (!slot)
1340 return NULL;
1342 /* See if we already have entry for type. */
1343 if (*slot)
1345 val = *slot;
1347 /* With LTO we need to support multiple tree representation of
1348 the same ODR type. */
1349 if (val->type != type)
1350 build_bases = add_type_duplicate (val, type);
1352 else
1354 val = ggc_cleared_alloc<odr_type_d> ();
1355 val->type = type;
1356 val->bases = vNULL;
1357 val->derived_types = vNULL;
1358 val->anonymous_namespace = type_in_anonymous_namespace_p (type);
1359 build_bases = COMPLETE_TYPE_P (val->type);
1360 insert_to_odr_array = true;
1363 if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1364 && type == TYPE_MAIN_VARIANT (type))
1366 tree binfo = TYPE_BINFO (type);
1367 unsigned int i;
1369 gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) = type);
1371 val->all_derivations_known = type_all_derivations_known_p (type);
1372 *slot = val;
1373 for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
1374 /* For now record only polymorphic types. other are
1375 pointless for devirtualization and we can not precisely
1376 determine ODR equivalency of these during LTO. */
1377 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
1379 odr_type base = get_odr_type (BINFO_TYPE (BINFO_BASE_BINFO (binfo,
1380 i)),
1381 true);
1382 gcc_assert (TYPE_MAIN_VARIANT (base->type) == base->type);
1383 base->derived_types.safe_push (val);
1384 val->bases.safe_push (base);
1385 if (base->id > base_id)
1386 base_id = base->id;
1389 /* Ensure that type always appears after bases. */
1390 if (insert_to_odr_array)
1392 if (odr_types_ptr)
1393 val->id = odr_types.length ();
1394 vec_safe_push (odr_types_ptr, val);
1396 else if (base_id > val->id)
1398 odr_types[val->id] = 0;
1399 /* Be sure we did not recorded any derived types; these may need
1400 renumbering too. */
1401 gcc_assert (val->derived_types.length() == 0);
1402 if (odr_types_ptr)
1403 val->id = odr_types.length ();
1404 vec_safe_push (odr_types_ptr, val);
1406 return val;
1409 /* Add TYPE od ODR type hash. */
1411 void
1412 register_odr_type (tree type)
1414 if (!odr_hash)
1415 odr_hash = new odr_hash_type (23);
1416 /* Arrange things to be nicer and insert main variants first. */
1417 if (odr_type_p (TYPE_MAIN_VARIANT (type)))
1418 get_odr_type (TYPE_MAIN_VARIANT (type), true);
1419 if (TYPE_MAIN_VARIANT (type) != type)
1420 get_odr_type (type, true);
1423 /* Return true if type is known to have no derivations. */
1425 bool
1426 type_known_to_have_no_deriavations_p (tree t)
1428 return (type_all_derivations_known_p (t)
1429 && (TYPE_FINAL_P (t)
1430 || (odr_hash
1431 && !get_odr_type (t, true)->derived_types.length())));
1434 /* Dump ODR type T and all its derrived type. INDENT specify indentation for
1435 recusive printing. */
1437 static void
1438 dump_odr_type (FILE *f, odr_type t, int indent=0)
1440 unsigned int i;
1441 fprintf (f, "%*s type %i: ", indent * 2, "", t->id);
1442 print_generic_expr (f, t->type, TDF_SLIM);
1443 fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":"");
1444 fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":"");
1445 if (TYPE_NAME (t->type))
1447 fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
1448 DECL_SOURCE_FILE (TYPE_NAME (t->type)),
1449 DECL_SOURCE_LINE (TYPE_NAME (t->type)));
1451 if (t->bases.length ())
1453 fprintf (f, "%*s base odr type ids: ", indent * 2, "");
1454 for (i = 0; i < t->bases.length (); i++)
1455 fprintf (f, " %i", t->bases[i]->id);
1456 fprintf (f, "\n");
1458 if (t->derived_types.length ())
1460 fprintf (f, "%*s derived types:\n", indent * 2, "");
1461 for (i = 0; i < t->derived_types.length (); i++)
1462 dump_odr_type (f, t->derived_types[i], indent + 1);
1464 fprintf (f, "\n");
1467 /* Dump the type inheritance graph. */
1469 static void
1470 dump_type_inheritance_graph (FILE *f)
1472 unsigned int i;
1473 if (!odr_types_ptr)
1474 return;
1475 fprintf (f, "\n\nType inheritance graph:\n");
1476 for (i = 0; i < odr_types.length (); i++)
1478 if (odr_types[i] && odr_types[i]->bases.length () == 0)
1479 dump_odr_type (f, odr_types[i]);
1481 for (i = 0; i < odr_types.length (); i++)
1483 if (odr_types[i] && odr_types[i]->types && odr_types[i]->types->length ())
1485 unsigned int j;
1486 fprintf (f, "Duplicate tree types for odr type %i\n", i);
1487 print_node (f, "", odr_types[i]->type, 0);
1488 for (j = 0; j < odr_types[i]->types->length (); j++)
1490 tree t;
1491 fprintf (f, "duplicate #%i\n", j);
1492 print_node (f, "", (*odr_types[i]->types)[j], 0);
1493 t = (*odr_types[i]->types)[j];
1494 while (TYPE_P (t) && TYPE_CONTEXT (t))
1496 t = TYPE_CONTEXT (t);
1497 print_node (f, "", t, 0);
1499 putc ('\n',f);
1505 /* Given method type T, return type of class it belongs to.
1506 Lookup this pointer and get its type. */
1508 tree
1509 method_class_type (const_tree t)
1511 tree first_parm_type = TREE_VALUE (TYPE_ARG_TYPES (t));
1512 gcc_assert (TREE_CODE (t) == METHOD_TYPE);
1514 return TREE_TYPE (first_parm_type);
1517 /* Initialize IPA devirt and build inheritance tree graph. */
1519 void
1520 build_type_inheritance_graph (void)
1522 struct symtab_node *n;
1523 FILE *inheritance_dump_file;
1524 int flags;
1526 if (odr_hash)
1527 return;
1528 timevar_push (TV_IPA_INHERITANCE);
1529 inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
1530 odr_hash = new odr_hash_type (23);
1532 /* We reconstruct the graph starting of types of all methods seen in the
1533 the unit. */
1534 FOR_EACH_SYMBOL (n)
1535 if (is_a <cgraph_node *> (n)
1536 && DECL_VIRTUAL_P (n->decl)
1537 && n->real_symbol_p ())
1538 get_odr_type (TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (n->decl))),
1539 true);
1541 /* Look also for virtual tables of types that do not define any methods.
1543 We need it in a case where class B has virtual base of class A
1544 re-defining its virtual method and there is class C with no virtual
1545 methods with B as virtual base.
1547 Here we output B's virtual method in two variant - for non-virtual
1548 and virtual inheritance. B's virtual table has non-virtual version,
1549 while C's has virtual.
1551 For this reason we need to know about C in order to include both
1552 variants of B. More correctly, record_target_from_binfo should
1553 add both variants of the method when walking B, but we have no
1554 link in between them.
1556 We rely on fact that either the method is exported and thus we
1557 assume it is called externally or C is in anonymous namespace and
1558 thus we will see the vtable. */
1560 else if (is_a <varpool_node *> (n)
1561 && DECL_VIRTUAL_P (n->decl)
1562 && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE
1563 && TYPE_BINFO (DECL_CONTEXT (n->decl))
1564 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl))))
1565 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n->decl)), true);
1566 if (inheritance_dump_file)
1568 dump_type_inheritance_graph (inheritance_dump_file);
1569 dump_end (TDI_inheritance, inheritance_dump_file);
1571 timevar_pop (TV_IPA_INHERITANCE);
1574 /* Return true if N has reference from live virtual table
1575 (and thus can be a destination of polymorphic call).
1576 Be conservatively correct when callgraph is not built or
1577 if the method may be referred externally. */
1579 static bool
1580 referenced_from_vtable_p (struct cgraph_node *node)
1582 int i;
1583 struct ipa_ref *ref;
1584 bool found = false;
1586 if (node->externally_visible
1587 || DECL_EXTERNAL (node->decl)
1588 || node->used_from_other_partition)
1589 return true;
1591 /* Keep this test constant time.
1592 It is unlikely this can happen except for the case where speculative
1593 devirtualization introduced many speculative edges to this node.
1594 In this case the target is very likely alive anyway. */
1595 if (node->ref_list.referring.length () > 100)
1596 return true;
1598 /* We need references built. */
1599 if (symtab->state <= CONSTRUCTION)
1600 return true;
1602 for (i = 0; node->iterate_referring (i, ref); i++)
1604 if ((ref->use == IPA_REF_ALIAS
1605 && referenced_from_vtable_p (dyn_cast<cgraph_node *> (ref->referring)))
1606 || (ref->use == IPA_REF_ADDR
1607 && TREE_CODE (ref->referring->decl) == VAR_DECL
1608 && DECL_VIRTUAL_P (ref->referring->decl)))
1610 found = true;
1611 break;
1613 return found;
1616 /* If TARGET has associated node, record it in the NODES array.
1617 CAN_REFER specify if program can refer to the target directly.
1618 if TARGET is unknown (NULL) or it can not be inserted (for example because
1619 its body was already removed and there is no way to refer to it), clear
1620 COMPLETEP. */
1622 static void
1623 maybe_record_node (vec <cgraph_node *> &nodes,
1624 tree target, hash_set<tree> *inserted,
1625 bool can_refer,
1626 bool *completep)
1628 struct cgraph_node *target_node, *alias_target;
1629 enum availability avail;
1631 /* cxa_pure_virtual and __builtin_unreachable do not need to be added into
1632 list of targets; the runtime effect of calling them is undefined.
1633 Only "real" virtual methods should be accounted. */
1634 if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE)
1635 return;
1637 if (!can_refer)
1639 /* The only case when method of anonymous namespace becomes unreferable
1640 is when we completely optimized it out. */
1641 if (flag_ltrans
1642 || !target
1643 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
1644 *completep = false;
1645 return;
1648 if (!target)
1649 return;
1651 target_node = cgraph_node::get (target);
1653 /* Preffer alias target over aliases, so we do not get confused by
1654 fake duplicates. */
1655 if (target_node)
1657 alias_target = target_node->ultimate_alias_target (&avail);
1658 if (target_node != alias_target
1659 && avail >= AVAIL_AVAILABLE
1660 && target_node->get_availability ())
1661 target_node = alias_target;
1664 /* Method can only be called by polymorphic call if any
1665 of vtables refering to it are alive.
1667 While this holds for non-anonymous functions, too, there are
1668 cases where we want to keep them in the list; for example
1669 inline functions with -fno-weak are static, but we still
1670 may devirtualize them when instance comes from other unit.
1671 The same holds for LTO.
1673 Currently we ignore these functions in speculative devirtualization.
1674 ??? Maybe it would make sense to be more aggressive for LTO even
1675 eslewhere. */
1676 if (!flag_ltrans
1677 && type_in_anonymous_namespace_p (DECL_CONTEXT (target))
1678 && (!target_node
1679 || !referenced_from_vtable_p (target_node)))
1681 /* See if TARGET is useful function we can deal with. */
1682 else if (target_node != NULL
1683 && (TREE_PUBLIC (target)
1684 || DECL_EXTERNAL (target)
1685 || target_node->definition)
1686 && target_node->real_symbol_p ())
1688 gcc_assert (!target_node->global.inlined_to);
1689 gcc_assert (target_node->real_symbol_p ());
1690 if (!inserted->add (target))
1692 cached_polymorphic_call_targets->add (target_node);
1693 nodes.safe_push (target_node);
1696 else if (completep
1697 && (!type_in_anonymous_namespace_p
1698 (DECL_CONTEXT (target))
1699 || flag_ltrans))
1700 *completep = false;
1703 /* See if BINFO's type match OUTER_TYPE. If so, lookup
1704 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
1705 method in vtable and insert method to NODES array
1706 or BASES_TO_CONSIDER if this array is non-NULL.
1707 Otherwise recurse to base BINFOs.
1708 This match what get_binfo_at_offset does, but with offset
1709 being unknown.
1711 TYPE_BINFOS is a stack of BINFOS of types with defined
1712 virtual table seen on way from class type to BINFO.
1714 MATCHED_VTABLES tracks virtual tables we already did lookup
1715 for virtual function in. INSERTED tracks nodes we already
1716 inserted.
1718 ANONYMOUS is true if BINFO is part of anonymous namespace.
1720 Clear COMPLETEP when we hit unreferable target.
1723 static void
1724 record_target_from_binfo (vec <cgraph_node *> &nodes,
1725 vec <tree> *bases_to_consider,
1726 tree binfo,
1727 tree otr_type,
1728 vec <tree> &type_binfos,
1729 HOST_WIDE_INT otr_token,
1730 tree outer_type,
1731 HOST_WIDE_INT offset,
1732 hash_set<tree> *inserted,
1733 hash_set<tree> *matched_vtables,
1734 bool anonymous,
1735 bool *completep)
1737 tree type = BINFO_TYPE (binfo);
1738 int i;
1739 tree base_binfo;
1742 if (BINFO_VTABLE (binfo))
1743 type_binfos.safe_push (binfo);
1744 if (types_same_for_odr (type, outer_type))
1746 int i;
1747 tree type_binfo = NULL;
1749 /* Lookup BINFO with virtual table. For normal types it is always last
1750 binfo on stack. */
1751 for (i = type_binfos.length () - 1; i >= 0; i--)
1752 if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo))
1754 type_binfo = type_binfos[i];
1755 break;
1757 if (BINFO_VTABLE (binfo))
1758 type_binfos.pop ();
1759 /* If this is duplicated BINFO for base shared by virtual inheritance,
1760 we may not have its associated vtable. This is not a problem, since
1761 we will walk it on the other path. */
1762 if (!type_binfo)
1763 return;
1764 tree inner_binfo = get_binfo_at_offset (type_binfo,
1765 offset, otr_type);
1766 if (!inner_binfo)
1768 gcc_assert (odr_violation_reported);
1769 return;
1771 /* For types in anonymous namespace first check if the respective vtable
1772 is alive. If not, we know the type can't be called. */
1773 if (!flag_ltrans && anonymous)
1775 tree vtable = BINFO_VTABLE (inner_binfo);
1776 varpool_node *vnode;
1778 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
1779 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
1780 vnode = varpool_node::get (vtable);
1781 if (!vnode || !vnode->definition)
1782 return;
1784 gcc_assert (inner_binfo);
1785 if (bases_to_consider
1786 ? !matched_vtables->contains (BINFO_VTABLE (inner_binfo))
1787 : !matched_vtables->add (BINFO_VTABLE (inner_binfo)))
1789 bool can_refer;
1790 tree target = gimple_get_virt_method_for_binfo (otr_token,
1791 inner_binfo,
1792 &can_refer);
1793 if (!bases_to_consider)
1794 maybe_record_node (nodes, target, inserted, can_refer, completep);
1795 /* Destructors are never called via construction vtables. */
1796 else if (!target || !DECL_CXX_DESTRUCTOR_P (target))
1797 bases_to_consider->safe_push (target);
1799 return;
1802 /* Walk bases. */
1803 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1804 /* Walking bases that have no virtual method is pointless excercise. */
1805 if (polymorphic_type_binfo_p (base_binfo))
1806 record_target_from_binfo (nodes, bases_to_consider, base_binfo, otr_type,
1807 type_binfos,
1808 otr_token, outer_type, offset, inserted,
1809 matched_vtables, anonymous, completep);
1810 if (BINFO_VTABLE (binfo))
1811 type_binfos.pop ();
1814 /* Lookup virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
1815 of TYPE, insert them to NODES, recurse into derived nodes.
1816 INSERTED is used to avoid duplicate insertions of methods into NODES.
1817 MATCHED_VTABLES are used to avoid duplicate walking vtables.
1818 Clear COMPLETEP if unreferable target is found.
1820 If CONSIDER_CONSTURCTION is true, record to BASES_TO_CONSDIER
1821 all cases where BASE_SKIPPED is true (because the base is abstract
1822 class). */
1824 static void
1825 possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
1826 hash_set<tree> *inserted,
1827 hash_set<tree> *matched_vtables,
1828 tree otr_type,
1829 odr_type type,
1830 HOST_WIDE_INT otr_token,
1831 tree outer_type,
1832 HOST_WIDE_INT offset,
1833 bool *completep,
1834 vec <tree> &bases_to_consider,
1835 bool consider_construction)
1837 tree binfo = TYPE_BINFO (type->type);
1838 unsigned int i;
1839 vec <tree> type_binfos = vNULL;
1840 bool possibly_instantiated = type_possibly_instantiated_p (type->type);
1842 /* We may need to consider types w/o instances because of possible derived
1843 types using their methods either directly or via construction vtables.
1844 We are safe to skip them when all derivations are known, since we will
1845 handle them later.
1846 This is done by recording them to BASES_TO_CONSIDER array. */
1847 if (possibly_instantiated || consider_construction)
1849 record_target_from_binfo (nodes,
1850 (!possibly_instantiated
1851 && type_all_derivations_known_p (type->type))
1852 ? &bases_to_consider : NULL,
1853 binfo, otr_type, type_binfos, otr_token,
1854 outer_type, offset,
1855 inserted, matched_vtables,
1856 type->anonymous_namespace, completep);
1858 type_binfos.release ();
1859 for (i = 0; i < type->derived_types.length (); i++)
1860 possible_polymorphic_call_targets_1 (nodes, inserted,
1861 matched_vtables,
1862 otr_type,
1863 type->derived_types[i],
1864 otr_token, outer_type, offset, completep,
1865 bases_to_consider, consider_construction);
1868 /* Cache of queries for polymorphic call targets.
1870 Enumerating all call targets may get expensive when there are many
1871 polymorphic calls in the program, so we memoize all the previous
1872 queries and avoid duplicated work. */
1874 struct polymorphic_call_target_d
1876 HOST_WIDE_INT otr_token;
1877 ipa_polymorphic_call_context context;
1878 odr_type type;
1879 vec <cgraph_node *> targets;
1880 tree decl_warning;
1881 int type_warning;
1882 bool complete;
1883 bool speculative;
1886 /* Polymorphic call target cache helpers. */
1888 struct polymorphic_call_target_hasher
1890 typedef polymorphic_call_target_d value_type;
1891 typedef polymorphic_call_target_d compare_type;
1892 static inline hashval_t hash (const value_type *);
1893 static inline bool equal (const value_type *, const compare_type *);
1894 static inline void remove (value_type *);
1897 /* Return the computed hashcode for ODR_QUERY. */
1899 inline hashval_t
1900 polymorphic_call_target_hasher::hash (const value_type *odr_query)
1902 inchash::hash hstate (odr_query->otr_token);
1904 hstate.add_wide_int (odr_query->type->id);
1905 hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
1906 hstate.add_wide_int (odr_query->context.offset);
1908 if (odr_query->context.speculative_outer_type)
1910 hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
1911 hstate.add_wide_int (odr_query->context.speculative_offset);
1913 hstate.add_flag (odr_query->speculative);
1914 hstate.add_flag (odr_query->context.maybe_in_construction);
1915 hstate.add_flag (odr_query->context.maybe_derived_type);
1916 hstate.add_flag (odr_query->context.speculative_maybe_derived_type);
1917 hstate.commit_flag ();
1918 return hstate.end ();
1921 /* Compare cache entries T1 and T2. */
1923 inline bool
1924 polymorphic_call_target_hasher::equal (const value_type *t1,
1925 const compare_type *t2)
1927 return (t1->type == t2->type && t1->otr_token == t2->otr_token
1928 && t1->speculative == t2->speculative
1929 && t1->context.offset == t2->context.offset
1930 && t1->context.speculative_offset == t2->context.speculative_offset
1931 && t1->context.outer_type == t2->context.outer_type
1932 && t1->context.speculative_outer_type == t2->context.speculative_outer_type
1933 && t1->context.maybe_in_construction
1934 == t2->context.maybe_in_construction
1935 && t1->context.maybe_derived_type == t2->context.maybe_derived_type
1936 && (t1->context.speculative_maybe_derived_type
1937 == t2->context.speculative_maybe_derived_type));
1940 /* Remove entry in polymorphic call target cache hash. */
1942 inline void
1943 polymorphic_call_target_hasher::remove (value_type *v)
1945 v->targets.release ();
1946 free (v);
1949 /* Polymorphic call target query cache. */
1951 typedef hash_table<polymorphic_call_target_hasher>
1952 polymorphic_call_target_hash_type;
1953 static polymorphic_call_target_hash_type *polymorphic_call_target_hash;
1955 /* Destroy polymorphic call target query cache. */
1957 static void
1958 free_polymorphic_call_targets_hash ()
1960 if (cached_polymorphic_call_targets)
1962 delete polymorphic_call_target_hash;
1963 polymorphic_call_target_hash = NULL;
1964 delete cached_polymorphic_call_targets;
1965 cached_polymorphic_call_targets = NULL;
1969 /* When virtual function is removed, we may need to flush the cache. */
1971 static void
1972 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
1974 if (cached_polymorphic_call_targets
1975 && cached_polymorphic_call_targets->contains (n))
1976 free_polymorphic_call_targets_hash ();
1979 /* Lookup base of BINFO that has virtual table VTABLE with OFFSET. */
1981 tree
1982 subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
1983 tree vtable)
1985 tree v = BINFO_VTABLE (binfo);
1986 int i;
1987 tree base_binfo;
1988 unsigned HOST_WIDE_INT this_offset;
1990 if (v)
1992 if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
1993 gcc_unreachable ();
1995 if (offset == this_offset
1996 && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
1997 return binfo;
2000 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2001 if (polymorphic_type_binfo_p (base_binfo))
2003 base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable);
2004 if (base_binfo)
2005 return base_binfo;
2007 return NULL;
2010 /* T is known constant value of virtual table pointer.
2011 Store virtual table to V and its offset to OFFSET.
2012 Return false if T does not look like virtual table reference. */
2014 bool
2015 vtable_pointer_value_to_vtable (const_tree t, tree *v,
2016 unsigned HOST_WIDE_INT *offset)
2018 /* We expect &MEM[(void *)&virtual_table + 16B].
2019 We obtain object's BINFO from the context of the virtual table.
2020 This one contains pointer to virtual table represented via
2021 POINTER_PLUS_EXPR. Verify that this pointer match to what
2022 we propagated through.
2024 In the case of virtual inheritance, the virtual tables may
2025 be nested, i.e. the offset may be different from 16 and we may
2026 need to dive into the type representation. */
2027 if (TREE_CODE (t) == ADDR_EXPR
2028 && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
2029 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
2030 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
2031 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
2032 == VAR_DECL)
2033 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2034 (TREE_OPERAND (t, 0), 0), 0)))
2036 *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
2037 *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
2038 return true;
2041 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2042 We need to handle it when T comes from static variable initializer or
2043 BINFO. */
2044 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
2046 *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
2047 t = TREE_OPERAND (t, 0);
2049 else
2050 *offset = 0;
2052 if (TREE_CODE (t) != ADDR_EXPR)
2053 return false;
2054 *v = TREE_OPERAND (t, 0);
2055 return true;
2058 /* T is known constant value of virtual table pointer. Return BINFO of the
2059 instance type. */
2061 tree
2062 vtable_pointer_value_to_binfo (const_tree t)
2064 tree vtable;
2065 unsigned HOST_WIDE_INT offset;
2067 if (!vtable_pointer_value_to_vtable (t, &vtable, &offset))
2068 return NULL_TREE;
2070 /* FIXME: for stores of construction vtables we return NULL,
2071 because we do not have BINFO for those. Eventually we should fix
2072 our representation to allow this case to be handled, too.
2073 In the case we see store of BINFO we however may assume
2074 that standard folding will be ale to cope with it. */
2075 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
2076 offset, vtable);
2079 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2080 Lookup their respecitve virtual methods for OTR_TOKEN and OTR_TYPE
2081 and insert them to NODES.
2083 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2085 static void
2086 record_targets_from_bases (tree otr_type,
2087 HOST_WIDE_INT otr_token,
2088 tree outer_type,
2089 HOST_WIDE_INT offset,
2090 vec <cgraph_node *> &nodes,
2091 hash_set<tree> *inserted,
2092 hash_set<tree> *matched_vtables,
2093 bool *completep)
2095 while (true)
2097 HOST_WIDE_INT pos, size;
2098 tree base_binfo;
2099 tree fld;
2101 if (types_same_for_odr (outer_type, otr_type))
2102 return;
2104 for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
2106 if (TREE_CODE (fld) != FIELD_DECL)
2107 continue;
2109 pos = int_bit_position (fld);
2110 size = tree_to_shwi (DECL_SIZE (fld));
2111 if (pos <= offset && (pos + size) > offset
2112 /* Do not get confused by zero sized bases. */
2113 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
2114 break;
2116 /* Within a class type we should always find correcponding fields. */
2117 gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
2119 /* Nonbasetypes should have been stripped by outer_class_type. */
2120 gcc_assert (DECL_ARTIFICIAL (fld));
2122 outer_type = TREE_TYPE (fld);
2123 offset -= pos;
2125 base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
2126 offset, otr_type);
2127 if (!base_binfo)
2129 gcc_assert (odr_violation_reported);
2130 return;
2132 gcc_assert (base_binfo);
2133 if (!matched_vtables->add (BINFO_VTABLE (base_binfo)))
2135 bool can_refer;
2136 tree target = gimple_get_virt_method_for_binfo (otr_token,
2137 base_binfo,
2138 &can_refer);
2139 if (!target || ! DECL_CXX_DESTRUCTOR_P (target))
2140 maybe_record_node (nodes, target, inserted, can_refer, completep);
2141 matched_vtables->add (BINFO_VTABLE (base_binfo));
2146 /* When virtual table is removed, we may need to flush the cache. */
2148 static void
2149 devirt_variable_node_removal_hook (varpool_node *n,
2150 void *d ATTRIBUTE_UNUSED)
2152 if (cached_polymorphic_call_targets
2153 && DECL_VIRTUAL_P (n->decl)
2154 && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
2155 free_polymorphic_call_targets_hash ();
2158 /* Record about how many calls would benefit from given type to be final. */
2160 struct odr_type_warn_count
2162 tree type;
2163 int count;
2164 gcov_type dyn_count;
2167 /* Record about how many calls would benefit from given method to be final. */
2169 struct decl_warn_count
2171 tree decl;
2172 int count;
2173 gcov_type dyn_count;
2176 /* Information about type and decl warnings. */
2178 struct final_warning_record
2180 gcov_type dyn_count;
2181 vec<odr_type_warn_count> type_warnings;
2182 hash_map<tree, decl_warn_count> decl_warnings;
2184 struct final_warning_record *final_warning_records;
2186 /* Return vector containing possible targets of polymorphic call of type
2187 OTR_TYPE caling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
2188 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containig
2189 OTR_TYPE and include their virtual method. This is useful for types
2190 possibly in construction or destruction where the virtual table may
2191 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
2192 us to walk the inheritance graph for all derivations.
2194 If COMPLETEP is non-NULL, store true if the list is complete.
2195 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
2196 in the target cache. If user needs to visit every target list
2197 just once, it can memoize them.
2199 If SPECULATIVE is set, the list will not contain targets that
2200 are not speculatively taken.
2202 Returned vector is placed into cache. It is NOT caller's responsibility
2203 to free it. The vector can be freed on cgraph_remove_node call if
2204 the particular node is a virtual function present in the cache. */
2206 vec <cgraph_node *>
2207 possible_polymorphic_call_targets (tree otr_type,
2208 HOST_WIDE_INT otr_token,
2209 ipa_polymorphic_call_context context,
2210 bool *completep,
2211 void **cache_token,
2212 bool speculative)
2214 static struct cgraph_node_hook_list *node_removal_hook_holder;
2215 vec <cgraph_node *> nodes = vNULL;
2216 vec <tree> bases_to_consider = vNULL;
2217 odr_type type, outer_type;
2218 polymorphic_call_target_d key;
2219 polymorphic_call_target_d **slot;
2220 unsigned int i;
2221 tree binfo, target;
2222 bool complete;
2223 bool can_refer = false;
2224 bool skipped = false;
2226 otr_type = TYPE_MAIN_VARIANT (otr_type);
2228 /* If ODR is not initialized or the constext is invalid, return empty
2229 incomplete list. */
2230 if (!odr_hash || context.invalid)
2232 if (completep)
2233 *completep = context.invalid;
2234 if (cache_token)
2235 *cache_token = NULL;
2236 return nodes;
2239 /* Do not bother to compute speculative info when user do not asks for it. */
2240 if (!speculative || !context.speculative_outer_type)
2241 context.clear_speculation ();
2243 type = get_odr_type (otr_type, true);
2245 /* Recording type variants would wast results cache. */
2246 gcc_assert (!context.outer_type
2247 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
2249 /* Lookup the outer class type we want to walk.
2250 If we fail to do so, the context is invalid. */
2251 if ((context.outer_type || context.speculative_outer_type)
2252 && !context.restrict_to_inner_class (otr_type))
2254 if (completep)
2255 *completep = true;
2256 if (cache_token)
2257 *cache_token = NULL;
2258 return nodes;
2260 gcc_assert (!context.invalid);
2262 /* Check that restrict_to_inner_class kept the main variant. */
2263 gcc_assert (!context.outer_type
2264 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
2266 /* We canonicalize our query, so we do not need extra hashtable entries. */
2268 /* Without outer type, we have no use for offset. Just do the
2269 basic search from innter type */
2270 if (!context.outer_type)
2272 context.outer_type = otr_type;
2273 context.offset = 0;
2275 /* We need to update our hiearchy if the type does not exist. */
2276 outer_type = get_odr_type (context.outer_type, true);
2277 /* If the type is complete, there are no derivations. */
2278 if (TYPE_FINAL_P (outer_type->type))
2279 context.maybe_derived_type = false;
2281 /* Initialize query cache. */
2282 if (!cached_polymorphic_call_targets)
2284 cached_polymorphic_call_targets = new hash_set<cgraph_node *>;
2285 polymorphic_call_target_hash
2286 = new polymorphic_call_target_hash_type (23);
2287 if (!node_removal_hook_holder)
2289 node_removal_hook_holder =
2290 symtab->add_cgraph_removal_hook (&devirt_node_removal_hook, NULL);
2291 symtab->add_varpool_removal_hook (&devirt_variable_node_removal_hook,
2292 NULL);
2296 if (in_lto_p)
2298 if (context.outer_type != otr_type)
2299 context.outer_type
2300 = get_odr_type (context.outer_type, true)->type;
2301 if (context.speculative_outer_type)
2302 context.speculative_outer_type
2303 = get_odr_type (context.speculative_outer_type, true)->type;
2306 /* Lookup cached answer. */
2307 key.type = type;
2308 key.otr_token = otr_token;
2309 key.speculative = speculative;
2310 key.context = context;
2311 slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
2312 if (cache_token)
2313 *cache_token = (void *)*slot;
2314 if (*slot)
2316 if (completep)
2317 *completep = (*slot)->complete;
2318 if ((*slot)->type_warning && final_warning_records)
2320 final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
2321 final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
2322 += final_warning_records->dyn_count;
2324 if (!speculative && (*slot)->decl_warning && final_warning_records)
2326 struct decl_warn_count *c =
2327 final_warning_records->decl_warnings.get ((*slot)->decl_warning);
2328 c->count++;
2329 c->dyn_count += final_warning_records->dyn_count;
2331 return (*slot)->targets;
2334 complete = true;
2336 /* Do actual search. */
2337 timevar_push (TV_IPA_VIRTUAL_CALL);
2338 *slot = XCNEW (polymorphic_call_target_d);
2339 if (cache_token)
2340 *cache_token = (void *)*slot;
2341 (*slot)->type = type;
2342 (*slot)->otr_token = otr_token;
2343 (*slot)->context = context;
2344 (*slot)->speculative = speculative;
2346 hash_set<tree> inserted;
2347 hash_set<tree> matched_vtables;
2349 /* First insert targets we speculatively identified as likely. */
2350 if (context.speculative_outer_type)
2352 odr_type speculative_outer_type;
2353 bool speculation_complete = true;
2355 /* First insert target from type itself and check if it may have derived types. */
2356 speculative_outer_type = get_odr_type (context.speculative_outer_type, true);
2357 if (TYPE_FINAL_P (speculative_outer_type->type))
2358 context.speculative_maybe_derived_type = false;
2359 binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
2360 context.speculative_offset, otr_type);
2361 if (binfo)
2362 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
2363 &can_refer);
2364 else
2365 target = NULL;
2367 /* In the case we get complete method, we don't need
2368 to walk derivations. */
2369 if (target && DECL_FINAL_P (target))
2370 context.speculative_maybe_derived_type = false;
2371 if (type_possibly_instantiated_p (speculative_outer_type->type))
2372 maybe_record_node (nodes, target, &inserted, can_refer, &speculation_complete);
2373 if (binfo)
2374 matched_vtables.add (BINFO_VTABLE (binfo));
2377 /* Next walk recursively all derived types. */
2378 if (context.speculative_maybe_derived_type)
2379 for (i = 0; i < speculative_outer_type->derived_types.length(); i++)
2380 possible_polymorphic_call_targets_1 (nodes, &inserted,
2381 &matched_vtables,
2382 otr_type,
2383 speculative_outer_type->derived_types[i],
2384 otr_token, speculative_outer_type->type,
2385 context.speculative_offset,
2386 &speculation_complete,
2387 bases_to_consider,
2388 false);
2391 if (!speculative || !nodes.length ())
2393 /* First see virtual method of type itself. */
2394 binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
2395 context.offset, otr_type);
2396 if (binfo)
2397 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
2398 &can_refer);
2399 else
2401 gcc_assert (odr_violation_reported);
2402 target = NULL;
2405 /* Destructors are never called through construction virtual tables,
2406 because the type is always known. */
2407 if (target && DECL_CXX_DESTRUCTOR_P (target))
2408 context.maybe_in_construction = false;
2410 if (target)
2412 /* In the case we get complete method, we don't need
2413 to walk derivations. */
2414 if (DECL_FINAL_P (target))
2415 context.maybe_derived_type = false;
2418 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
2419 if (type_possibly_instantiated_p (outer_type->type))
2420 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
2421 else
2422 skipped = true;
2424 if (binfo)
2425 matched_vtables.add (BINFO_VTABLE (binfo));
2427 /* Next walk recursively all derived types. */
2428 if (context.maybe_derived_type)
2430 for (i = 0; i < outer_type->derived_types.length(); i++)
2431 possible_polymorphic_call_targets_1 (nodes, &inserted,
2432 &matched_vtables,
2433 otr_type,
2434 outer_type->derived_types[i],
2435 otr_token, outer_type->type,
2436 context.offset, &complete,
2437 bases_to_consider,
2438 context.maybe_in_construction);
2440 if (!outer_type->all_derivations_known)
2442 if (!speculative && final_warning_records)
2444 if (complete
2445 && nodes.length () == 1
2446 && warn_suggest_final_types
2447 && !outer_type->derived_types.length ())
2449 if (outer_type->id >= (int)final_warning_records->type_warnings.length ())
2450 final_warning_records->type_warnings.safe_grow_cleared
2451 (odr_types.length ());
2452 final_warning_records->type_warnings[outer_type->id].count++;
2453 final_warning_records->type_warnings[outer_type->id].dyn_count
2454 += final_warning_records->dyn_count;
2455 final_warning_records->type_warnings[outer_type->id].type
2456 = outer_type->type;
2457 (*slot)->type_warning = outer_type->id + 1;
2459 if (complete
2460 && warn_suggest_final_methods
2461 && nodes.length () == 1
2462 && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl),
2463 outer_type->type))
2465 bool existed;
2466 struct decl_warn_count &c =
2467 final_warning_records->decl_warnings.get_or_insert
2468 (nodes[0]->decl, &existed);
2470 if (existed)
2472 c.count++;
2473 c.dyn_count += final_warning_records->dyn_count;
2475 else
2477 c.count = 1;
2478 c.dyn_count = final_warning_records->dyn_count;
2479 c.decl = nodes[0]->decl;
2481 (*slot)->decl_warning = nodes[0]->decl;
2484 complete = false;
2488 if (!speculative)
2490 /* Destructors are never called through construction virtual tables,
2491 because the type is always known. One of entries may be cxa_pure_virtual
2492 so look to at least two of them. */
2493 if (context.maybe_in_construction)
2494 for (i =0 ; i < MIN (nodes.length (), 2); i++)
2495 if (DECL_CXX_DESTRUCTOR_P (nodes[i]->decl))
2496 context.maybe_in_construction = false;
2497 if (context.maybe_in_construction)
2499 if (type != outer_type
2500 && (!skipped
2501 || (context.maybe_derived_type
2502 && !type_all_derivations_known_p (outer_type->type))))
2503 record_targets_from_bases (otr_type, otr_token, outer_type->type,
2504 context.offset, nodes, &inserted,
2505 &matched_vtables, &complete);
2506 if (skipped)
2507 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
2508 for (i = 0; i < bases_to_consider.length(); i++)
2509 maybe_record_node (nodes, bases_to_consider[i], &inserted, can_refer, &complete);
2514 bases_to_consider.release();
2515 (*slot)->targets = nodes;
2516 (*slot)->complete = complete;
2517 if (completep)
2518 *completep = complete;
2520 timevar_pop (TV_IPA_VIRTUAL_CALL);
2521 return nodes;
2524 bool
2525 add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value,
2526 vec<const decl_warn_count*> *vec)
2528 vec->safe_push (&value);
2529 return true;
2532 /* Dump target list TARGETS into FILE. */
2534 static void
2535 dump_targets (FILE *f, vec <cgraph_node *> targets)
2537 unsigned int i;
2539 for (i = 0; i < targets.length (); i++)
2541 char *name = NULL;
2542 if (in_lto_p)
2543 name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
2544 fprintf (f, " %s/%i", name ? name : targets[i]->name (), targets[i]->order);
2545 if (in_lto_p)
2546 free (name);
2547 if (!targets[i]->definition)
2548 fprintf (f, " (no definition%s)",
2549 DECL_DECLARED_INLINE_P (targets[i]->decl)
2550 ? " inline" : "");
2552 fprintf (f, "\n");
2555 /* Dump all possible targets of a polymorphic call. */
2557 void
2558 dump_possible_polymorphic_call_targets (FILE *f,
2559 tree otr_type,
2560 HOST_WIDE_INT otr_token,
2561 const ipa_polymorphic_call_context &ctx)
2563 vec <cgraph_node *> targets;
2564 bool final;
2565 odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false);
2566 unsigned int len;
2568 if (!type)
2569 return;
2570 targets = possible_polymorphic_call_targets (otr_type, otr_token,
2571 ctx,
2572 &final, NULL, false);
2573 fprintf (f, " Targets of polymorphic call of type %i:", type->id);
2574 print_generic_expr (f, type->type, TDF_SLIM);
2575 fprintf (f, " token %i\n", (int)otr_token);
2577 ctx.dump (f);
2579 fprintf (f, " %s%s%s%s\n ",
2580 final ? "This is a complete list." :
2581 "This is partial list; extra targets may be defined in other units.",
2582 ctx.maybe_in_construction ? " (base types included)" : "",
2583 ctx.maybe_derived_type ? " (derived types included)" : "",
2584 ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
2585 len = targets.length ();
2586 dump_targets (f, targets);
2588 targets = possible_polymorphic_call_targets (otr_type, otr_token,
2589 ctx,
2590 &final, NULL, true);
2591 gcc_assert (targets.length () <= len);
2592 if (targets.length () != len)
2594 fprintf (f, " Speculative targets:");
2595 dump_targets (f, targets);
2597 fprintf (f, "\n");
2601 /* Return true if N can be possibly target of a polymorphic call of
2602 OTR_TYPE/OTR_TOKEN. */
2604 bool
2605 possible_polymorphic_call_target_p (tree otr_type,
2606 HOST_WIDE_INT otr_token,
2607 const ipa_polymorphic_call_context &ctx,
2608 struct cgraph_node *n)
2610 vec <cgraph_node *> targets;
2611 unsigned int i;
2612 enum built_in_function fcode;
2613 bool final;
2615 if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
2616 && ((fcode = DECL_FUNCTION_CODE (n->decl))
2617 == BUILT_IN_UNREACHABLE
2618 || fcode == BUILT_IN_TRAP))
2619 return true;
2621 if (!odr_hash)
2622 return true;
2623 targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
2624 for (i = 0; i < targets.length (); i++)
2625 if (n->semantically_equivalent_p (targets[i]))
2626 return true;
2628 /* At a moment we allow middle end to dig out new external declarations
2629 as a targets of polymorphic calls. */
2630 if (!final && !n->definition)
2631 return true;
2632 return false;
2637 /* Return true if N can be possibly target of a polymorphic call of
2638 OBJ_TYPE_REF expression REF in STMT. */
2640 bool
2641 possible_polymorphic_call_target_p (tree ref,
2642 gimple stmt,
2643 struct cgraph_node *n)
2645 ipa_polymorphic_call_context context (current_function_decl, ref, stmt);
2646 tree call_fn = gimple_call_fn (stmt);
2648 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn),
2649 tree_to_uhwi
2650 (OBJ_TYPE_REF_TOKEN (call_fn)),
2651 context,
2656 /* After callgraph construction new external nodes may appear.
2657 Add them into the graph. */
2659 void
2660 update_type_inheritance_graph (void)
2662 struct cgraph_node *n;
2664 if (!odr_hash)
2665 return;
2666 free_polymorphic_call_targets_hash ();
2667 timevar_push (TV_IPA_INHERITANCE);
2668 /* We reconstruct the graph starting from types of all methods seen in the
2669 the unit. */
2670 FOR_EACH_FUNCTION (n)
2671 if (DECL_VIRTUAL_P (n->decl)
2672 && !n->definition
2673 && n->real_symbol_p ())
2674 get_odr_type (method_class_type (TYPE_MAIN_VARIANT (TREE_TYPE (n->decl))),
2675 true);
2676 timevar_pop (TV_IPA_INHERITANCE);
2680 /* Return true if N looks like likely target of a polymorphic call.
2681 Rule out cxa_pure_virtual, noreturns, function declared cold and
2682 other obvious cases. */
2684 bool
2685 likely_target_p (struct cgraph_node *n)
2687 int flags;
2688 /* cxa_pure_virtual and similar things are not likely. */
2689 if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
2690 return false;
2691 flags = flags_from_decl_or_type (n->decl);
2692 if (flags & ECF_NORETURN)
2693 return false;
2694 if (lookup_attribute ("cold",
2695 DECL_ATTRIBUTES (n->decl)))
2696 return false;
2697 if (n->frequency < NODE_FREQUENCY_NORMAL)
2698 return false;
2699 /* If there are no virtual tables refering the target alive,
2700 the only way the target can be called is an instance comming from other
2701 compilation unit; speculative devirtualization is build around an
2702 assumption that won't happen. */
2703 if (!referenced_from_vtable_p (n))
2704 return false;
2705 return true;
2708 /* Compare type warning records P1 and P2 and chose one with larger count;
2709 helper for qsort. */
2712 type_warning_cmp (const void *p1, const void *p2)
2714 const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
2715 const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
2717 if (t1->dyn_count < t2->dyn_count)
2718 return 1;
2719 if (t1->dyn_count > t2->dyn_count)
2720 return -1;
2721 return t2->count - t1->count;
2724 /* Compare decl warning records P1 and P2 and chose one with larger count;
2725 helper for qsort. */
2728 decl_warning_cmp (const void *p1, const void *p2)
2730 const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
2731 const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
2733 if (t1->dyn_count < t2->dyn_count)
2734 return 1;
2735 if (t1->dyn_count > t2->dyn_count)
2736 return -1;
2737 return t2->count - t1->count;
2741 /* Try speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
2742 context CTX. */
2744 struct cgraph_node *
2745 try_speculative_devirtualization (tree otr_type, HOST_WIDE_INT otr_token,
2746 ipa_polymorphic_call_context ctx)
2748 vec <cgraph_node *>targets
2749 = possible_polymorphic_call_targets
2750 (otr_type, otr_token, ctx, NULL, NULL, true);
2751 unsigned int i;
2752 struct cgraph_node *likely_target = NULL;
2754 for (i = 0; i < targets.length (); i++)
2755 if (likely_target_p (targets[i]))
2757 if (likely_target)
2758 return NULL;
2759 likely_target = targets[i];
2761 if (!likely_target
2762 ||!likely_target->definition
2763 || DECL_EXTERNAL (likely_target->decl))
2764 return NULL;
2766 /* Don't use an implicitly-declared destructor (c++/58678). */
2767 struct cgraph_node *non_thunk_target
2768 = likely_target->function_symbol ();
2769 if (DECL_ARTIFICIAL (non_thunk_target->decl))
2770 return NULL;
2771 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
2772 && likely_target->can_be_discarded_p ())
2773 return NULL;
2774 return likely_target;
2777 /* The ipa-devirt pass.
2778 When polymorphic call has only one likely target in the unit,
2779 turn it into speculative call. */
2781 static unsigned int
2782 ipa_devirt (void)
2784 struct cgraph_node *n;
2785 hash_set<void *> bad_call_targets;
2786 struct cgraph_edge *e;
2788 int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
2789 int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
2790 int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
2792 if (!odr_types_ptr)
2793 return 0;
2795 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
2796 This is implemented by setting up final_warning_records that are updated
2797 by get_polymorphic_call_targets.
2798 We need to clear cache in this case to trigger recomputation of all
2799 entries. */
2800 if (warn_suggest_final_methods || warn_suggest_final_types)
2802 final_warning_records = new (final_warning_record);
2803 final_warning_records->type_warnings = vNULL;
2804 final_warning_records->type_warnings.safe_grow_cleared (odr_types.length ());
2805 free_polymorphic_call_targets_hash ();
2808 FOR_EACH_DEFINED_FUNCTION (n)
2810 bool update = false;
2811 if (dump_file && n->indirect_calls)
2812 fprintf (dump_file, "\n\nProcesing function %s/%i\n",
2813 n->name (), n->order);
2814 for (e = n->indirect_calls; e; e = e->next_callee)
2815 if (e->indirect_info->polymorphic)
2817 struct cgraph_node *likely_target = NULL;
2818 void *cache_token;
2819 bool final;
2821 if (final_warning_records)
2822 final_warning_records->dyn_count = e->count;
2824 vec <cgraph_node *>targets
2825 = possible_polymorphic_call_targets
2826 (e, &final, &cache_token, true);
2827 unsigned int i;
2829 /* Trigger warnings by calculating non-speculative targets. */
2830 if (warn_suggest_final_methods || warn_suggest_final_types)
2831 possible_polymorphic_call_targets (e);
2833 if (dump_file)
2834 dump_possible_polymorphic_call_targets
2835 (dump_file, e);
2837 npolymorphic++;
2839 if (!flag_devirtualize_speculatively)
2840 continue;
2842 if (!e->maybe_hot_p ())
2844 if (dump_file)
2845 fprintf (dump_file, "Call is cold\n\n");
2846 ncold++;
2847 continue;
2849 if (e->speculative)
2851 if (dump_file)
2852 fprintf (dump_file, "Call is aready speculated\n\n");
2853 nspeculated++;
2855 /* When dumping see if we agree with speculation. */
2856 if (!dump_file)
2857 continue;
2859 if (bad_call_targets.contains (cache_token))
2861 if (dump_file)
2862 fprintf (dump_file, "Target list is known to be useless\n\n");
2863 nmultiple++;
2864 continue;
2866 for (i = 0; i < targets.length (); i++)
2867 if (likely_target_p (targets[i]))
2869 if (likely_target)
2871 likely_target = NULL;
2872 if (dump_file)
2873 fprintf (dump_file, "More than one likely target\n\n");
2874 nmultiple++;
2875 break;
2877 likely_target = targets[i];
2879 if (!likely_target)
2881 bad_call_targets.add (cache_token);
2882 continue;
2884 /* This is reached only when dumping; check if we agree or disagree
2885 with the speculation. */
2886 if (e->speculative)
2888 struct cgraph_edge *e2;
2889 struct ipa_ref *ref;
2890 e->speculative_call_info (e2, e, ref);
2891 if (e2->callee->ultimate_alias_target ()
2892 == likely_target->ultimate_alias_target ())
2894 fprintf (dump_file, "We agree with speculation\n\n");
2895 nok++;
2897 else
2899 fprintf (dump_file, "We disagree with speculation\n\n");
2900 nwrong++;
2902 continue;
2904 if (!likely_target->definition)
2906 if (dump_file)
2907 fprintf (dump_file, "Target is not an definition\n\n");
2908 nnotdefined++;
2909 continue;
2911 /* Do not introduce new references to external symbols. While we
2912 can handle these just well, it is common for programs to
2913 incorrectly with headers defining methods they are linked
2914 with. */
2915 if (DECL_EXTERNAL (likely_target->decl))
2917 if (dump_file)
2918 fprintf (dump_file, "Target is external\n\n");
2919 nexternal++;
2920 continue;
2922 /* Don't use an implicitly-declared destructor (c++/58678). */
2923 struct cgraph_node *non_thunk_target
2924 = likely_target->function_symbol ();
2925 if (DECL_ARTIFICIAL (non_thunk_target->decl))
2927 if (dump_file)
2928 fprintf (dump_file, "Target is artificial\n\n");
2929 nartificial++;
2930 continue;
2932 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
2933 && likely_target->can_be_discarded_p ())
2935 if (dump_file)
2936 fprintf (dump_file, "Target is overwritable\n\n");
2937 noverwritable++;
2938 continue;
2940 else if (dbg_cnt (devirt))
2942 if (dump_enabled_p ())
2944 location_t locus = gimple_location_safe (e->call_stmt);
2945 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
2946 "speculatively devirtualizing call in %s/%i to %s/%i\n",
2947 n->name (), n->order,
2948 likely_target->name (),
2949 likely_target->order);
2951 if (!likely_target->can_be_discarded_p ())
2953 cgraph_node *alias;
2954 alias = dyn_cast<cgraph_node *> (likely_target->noninterposable_alias ());
2955 if (alias)
2956 likely_target = alias;
2958 nconverted++;
2959 update = true;
2960 e->make_speculative
2961 (likely_target, e->count * 8 / 10, e->frequency * 8 / 10);
2964 if (update)
2965 inline_update_overall_summary (n);
2967 if (warn_suggest_final_methods || warn_suggest_final_types)
2969 if (warn_suggest_final_types)
2971 final_warning_records->type_warnings.qsort (type_warning_cmp);
2972 for (unsigned int i = 0;
2973 i < final_warning_records->type_warnings.length (); i++)
2974 if (final_warning_records->type_warnings[i].count)
2976 tree type = final_warning_records->type_warnings[i].type;
2977 int count = final_warning_records->type_warnings[i].count;
2978 long long dyn_count
2979 = final_warning_records->type_warnings[i].dyn_count;
2981 if (!dyn_count)
2982 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
2983 OPT_Wsuggest_final_types, count,
2984 "Declaring type %qD final "
2985 "would enable devirtualization of %i call",
2986 "Declaring type %qD final "
2987 "would enable devirtualization of %i calls",
2988 type,
2989 count);
2990 else
2991 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
2992 OPT_Wsuggest_final_types, count,
2993 "Declaring type %qD final "
2994 "would enable devirtualization of %i call "
2995 "executed %lli times",
2996 "Declaring type %qD final "
2997 "would enable devirtualization of %i calls "
2998 "executed %lli times",
2999 type,
3000 count,
3001 dyn_count);
3005 if (warn_suggest_final_methods)
3007 vec<const decl_warn_count*> decl_warnings_vec = vNULL;
3009 final_warning_records->decl_warnings.traverse
3010 <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec);
3011 decl_warnings_vec.qsort (decl_warning_cmp);
3012 for (unsigned int i = 0; i < decl_warnings_vec.length (); i++)
3014 tree decl = decl_warnings_vec[i]->decl;
3015 int count = decl_warnings_vec[i]->count;
3016 long long dyn_count = decl_warnings_vec[i]->dyn_count;
3018 if (!dyn_count)
3019 if (DECL_CXX_DESTRUCTOR_P (decl))
3020 warning_n (DECL_SOURCE_LOCATION (decl),
3021 OPT_Wsuggest_final_methods, count,
3022 "Declaring virtual destructor of %qD final "
3023 "would enable devirtualization of %i call",
3024 "Declaring virtual destructor of %qD final "
3025 "would enable devirtualization of %i calls",
3026 DECL_CONTEXT (decl), count);
3027 else
3028 warning_n (DECL_SOURCE_LOCATION (decl),
3029 OPT_Wsuggest_final_methods, count,
3030 "Declaring method %qD final "
3031 "would enable devirtualization of %i call",
3032 "Declaring method %qD final "
3033 "would enable devirtualization of %i calls",
3034 decl, count);
3035 else if (DECL_CXX_DESTRUCTOR_P (decl))
3036 warning_n (DECL_SOURCE_LOCATION (decl),
3037 OPT_Wsuggest_final_methods, count,
3038 "Declaring virtual destructor of %qD final "
3039 "would enable devirtualization of %i call "
3040 "executed %lli times",
3041 "Declaring virtual destructor of %qD final "
3042 "would enable devirtualization of %i calls "
3043 "executed %lli times",
3044 DECL_CONTEXT (decl), count, dyn_count);
3045 else
3046 warning_n (DECL_SOURCE_LOCATION (decl),
3047 OPT_Wsuggest_final_methods, count,
3048 "Declaring method %qD final "
3049 "would enable devirtualization of %i call "
3050 "executed %lli times",
3051 "Declaring method %qD final "
3052 "would enable devirtualization of %i calls "
3053 "executed %lli times",
3054 decl, count, dyn_count);
3058 delete (final_warning_records);
3059 final_warning_records = 0;
3062 if (dump_file)
3063 fprintf (dump_file,
3064 "%i polymorphic calls, %i devirtualized,"
3065 " %i speculatively devirtualized, %i cold\n"
3066 "%i have multiple targets, %i overwritable,"
3067 " %i already speculated (%i agree, %i disagree),"
3068 " %i external, %i not defined, %i artificial\n",
3069 npolymorphic, ndevirtualized, nconverted, ncold,
3070 nmultiple, noverwritable, nspeculated, nok, nwrong,
3071 nexternal, nnotdefined, nartificial);
3072 return ndevirtualized ? TODO_remove_functions : 0;
3075 namespace {
3077 const pass_data pass_data_ipa_devirt =
3079 IPA_PASS, /* type */
3080 "devirt", /* name */
3081 OPTGROUP_NONE, /* optinfo_flags */
3082 TV_IPA_DEVIRT, /* tv_id */
3083 0, /* properties_required */
3084 0, /* properties_provided */
3085 0, /* properties_destroyed */
3086 0, /* todo_flags_start */
3087 ( TODO_dump_symtab ), /* todo_flags_finish */
3090 class pass_ipa_devirt : public ipa_opt_pass_d
3092 public:
3093 pass_ipa_devirt (gcc::context *ctxt)
3094 : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
3095 NULL, /* generate_summary */
3096 NULL, /* write_summary */
3097 NULL, /* read_summary */
3098 NULL, /* write_optimization_summary */
3099 NULL, /* read_optimization_summary */
3100 NULL, /* stmt_fixup */
3101 0, /* function_transform_todo_flags_start */
3102 NULL, /* function_transform */
3103 NULL) /* variable_transform */
3106 /* opt_pass methods: */
3107 virtual bool gate (function *)
3109 return (flag_devirtualize
3110 && (flag_devirtualize_speculatively
3111 || (warn_suggest_final_methods
3112 || warn_suggest_final_types))
3113 && optimize);
3116 virtual unsigned int execute (function *) { return ipa_devirt (); }
3118 }; // class pass_ipa_devirt
3120 } // anon namespace
3122 ipa_opt_pass_d *
3123 make_pass_ipa_devirt (gcc::context *ctxt)
3125 return new pass_ipa_devirt (ctxt);
3128 #include "gt-ipa-devirt.h"