* config/i386/i386.md (fmodxf3): Enable for flag_finite_math_only only.
[official-gcc.git] / gcc / ipa-devirt.c
blobdc47e99f6b511da51549c60c6bdcec532881c451
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 if (type_in_anonymous_namespace_p (t))
228 return true;
229 return (decl_function_context (TYPE_NAME (t)) != NULL);
232 /* Return TURE if type's constructors are all visible. */
234 static bool
235 type_all_ctors_visible_p (tree t)
237 return !flag_ltrans
238 && symtab->state >= CONSTRUCTION
239 /* We can not always use type_all_derivations_known_p.
240 For function local types we must assume case where
241 the function is COMDAT and shared in between units.
243 TODO: These cases are quite easy to get, but we need
244 to keep track of C++ privatizing via -Wno-weak
245 as well as the IPA privatizing. */
246 && type_in_anonymous_namespace_p (t);
249 /* Return TRUE if type may have instance. */
251 static bool
252 type_possibly_instantiated_p (tree t)
254 tree vtable;
255 varpool_node *vnode;
257 /* TODO: Add abstract types here. */
258 if (!type_all_ctors_visible_p (t))
259 return true;
261 vtable = BINFO_VTABLE (TYPE_BINFO (t));
262 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
263 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
264 vnode = varpool_node::get (vtable);
265 return vnode && vnode->definition;
268 /* One Definition Rule hashtable helpers. */
270 struct odr_hasher
272 typedef odr_type_d value_type;
273 typedef union tree_node compare_type;
274 static inline hashval_t hash (const value_type *);
275 static inline bool equal (const value_type *, const compare_type *);
276 static inline void remove (value_type *);
279 /* Return type that was declared with T's name so that T is an
280 qualified variant of it. */
282 static inline tree
283 main_odr_variant (const_tree t)
285 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
286 return TREE_TYPE (TYPE_NAME (t));
287 /* Unnamed types and non-C++ produced types can be compared by variants. */
288 else
289 return TYPE_MAIN_VARIANT (t);
292 /* Produce hash based on type name. */
294 static hashval_t
295 hash_type_name (tree t)
297 gcc_checking_assert (main_odr_variant (t) == t);
299 /* If not in LTO, all main variants are unique, so we can do
300 pointer hash. */
301 if (!in_lto_p)
302 return htab_hash_pointer (t);
304 /* Anonymous types are unique. */
305 if (type_in_anonymous_namespace_p (t))
306 return htab_hash_pointer (t);
308 /* ODR types have name specified. */
309 if (TYPE_NAME (t)
310 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)))
311 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t)));
313 /* For polymorphic types that was compiled with -fno-lto-odr-type-merging
314 we can simply hash the virtual table. */
315 if (TREE_CODE (t) == RECORD_TYPE
316 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)))
318 tree v = BINFO_VTABLE (TYPE_BINFO (t));
319 hashval_t hash = 0;
321 if (TREE_CODE (v) == POINTER_PLUS_EXPR)
323 hash = TREE_INT_CST_LOW (TREE_OPERAND (v, 1));
324 v = TREE_OPERAND (TREE_OPERAND (v, 0), 0);
327 v = DECL_ASSEMBLER_NAME (v);
328 hash = iterative_hash_hashval_t (hash, htab_hash_pointer (v));
329 return hash;
332 /* Builtin types may appear as main variants of ODR types and are unique.
333 Sanity check we do not get anything that looks non-builtin. */
334 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
335 || TREE_CODE (t) == VOID_TYPE
336 || TREE_CODE (t) == COMPLEX_TYPE
337 || TREE_CODE (t) == REAL_TYPE
338 || TREE_CODE (t) == POINTER_TYPE);
339 return htab_hash_pointer (t);
342 /* Return the computed hashcode for ODR_TYPE. */
344 inline hashval_t
345 odr_hasher::hash (const value_type *odr_type)
347 return hash_type_name (odr_type->type);
350 /* For languages with One Definition Rule, work out if
351 types are the same based on their name.
353 This is non-trivial for LTO where minnor differences in
354 the type representation may have prevented type merging
355 to merge two copies of otherwise equivalent type.
357 Until we start streaming mangled type names, this function works
358 only for polymorphic types. */
360 bool
361 types_same_for_odr (const_tree type1, const_tree type2)
363 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
365 type1 = main_odr_variant (type1);
366 type2 = main_odr_variant (type2);
368 if (type1 == type2)
369 return true;
371 if (!in_lto_p)
372 return false;
374 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
375 on the corresponding TYPE_STUB_DECL. */
376 if (type_in_anonymous_namespace_p (type1)
377 || type_in_anonymous_namespace_p (type2))
378 return false;
381 /* ODR name of the type is set in DECL_ASSEMBLER_NAME of its TYPE_NAME.
383 Ideally we should never meed types without ODR names here. It can however
384 happen in two cases:
386 1) for builtin types that are not streamed but rebuilt in lto/lto-lang.c
387 Here testing for equivalence is safe, since their MAIN_VARIANTs are
388 unique.
389 2) for units streamed with -fno-lto-odr-type-merging. Here we can't
390 establish precise ODR equivalency, but for correctness we care only
391 about equivalency on complete polymorphic types. For these we can
392 compare assembler names of their virtual tables. */
393 if ((!TYPE_NAME (type1) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type1)))
394 || (!TYPE_NAME (type2) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type2))))
396 /* See if types are obvoiusly different (i.e. different codes
397 or polymorphis wrt non-polymorphic). This is not strictly correct
398 for ODR violating programs, but we can't do better without streaming
399 ODR names. */
400 if (TREE_CODE (type1) != TREE_CODE (type2))
401 return false;
402 if (TREE_CODE (type1) == RECORD_TYPE
403 && (TYPE_BINFO (type1) == NULL_TREE) != (TYPE_BINFO (type1) == NULL_TREE))
404 return false;
405 if (TREE_CODE (type1) == RECORD_TYPE && TYPE_BINFO (type1)
406 && (BINFO_VTABLE (TYPE_BINFO (type1)) == NULL_TREE)
407 != (BINFO_VTABLE (TYPE_BINFO (type2)) == NULL_TREE))
408 return false;
410 /* At the moment we have no way to establish ODR equivlaence at LTO
411 other than comparing virtual table pointrs of polymorphic types.
412 Eventually we should start saving mangled names in TYPE_NAME.
413 Then this condition will become non-trivial. */
415 if (TREE_CODE (type1) == RECORD_TYPE
416 && TYPE_BINFO (type1) && TYPE_BINFO (type2)
417 && BINFO_VTABLE (TYPE_BINFO (type1))
418 && BINFO_VTABLE (TYPE_BINFO (type2)))
420 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
421 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
422 gcc_assert (TREE_CODE (v1) == POINTER_PLUS_EXPR
423 && TREE_CODE (v2) == POINTER_PLUS_EXPR);
424 return (operand_equal_p (TREE_OPERAND (v1, 1),
425 TREE_OPERAND (v2, 1), 0)
426 && DECL_ASSEMBLER_NAME
427 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
428 == DECL_ASSEMBLER_NAME
429 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
431 gcc_unreachable ();
433 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1))
434 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2)));
437 /* Return true if we can decide on ODR equivalency.
439 In non-LTO it is always decide, in LTO however it depends in the type has
440 ODR info attached. */
442 bool
443 types_odr_comparable (tree t1, tree t2)
445 return (!in_lto_p
446 || main_odr_variant (t1) == main_odr_variant (t2)
447 || (odr_type_p (t1) && odr_type_p (t2))
448 || (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE
449 && TYPE_BINFO (t1) && TYPE_BINFO (t2)
450 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
451 && polymorphic_type_binfo_p (TYPE_BINFO (t2))));
454 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
455 known, be conservative and return false. */
457 bool
458 types_must_be_same_for_odr (tree t1, tree t2)
460 if (types_odr_comparable (t1, t2))
461 return types_same_for_odr (t1, t2);
462 else
463 return main_odr_variant (t1) == main_odr_variant (t2);
466 /* Compare types T1 and T2 and return true if they are
467 equivalent. */
469 inline bool
470 odr_hasher::equal (const value_type *t1, const compare_type *ct2)
472 tree t2 = const_cast <tree> (ct2);
474 gcc_checking_assert (main_odr_variant (t2) == t2);
475 if (t1->type == t2)
476 return true;
477 if (!in_lto_p)
478 return false;
479 return types_same_for_odr (t1->type, t2);
482 /* Free ODR type V. */
484 inline void
485 odr_hasher::remove (value_type *v)
487 v->bases.release ();
488 v->derived_types.release ();
489 if (v->types_set)
490 delete v->types_set;
491 ggc_free (v);
494 /* ODR type hash used to lookup ODR type based on tree type node. */
496 typedef hash_table<odr_hasher> odr_hash_type;
497 static odr_hash_type *odr_hash;
499 /* ODR types are also stored into ODR_TYPE vector to allow consistent
500 walking. Bases appear before derived types. Vector is garbage collected
501 so we won't end up visiting empty types. */
503 static GTY(()) vec <odr_type, va_gc> *odr_types_ptr;
504 #define odr_types (*odr_types_ptr)
506 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
507 void
508 set_type_binfo (tree type, tree binfo)
510 for (; type; type = TYPE_NEXT_VARIANT (type))
511 if (COMPLETE_TYPE_P (type))
512 TYPE_BINFO (type) = binfo;
513 else
514 gcc_assert (!TYPE_BINFO (type));
517 /* Compare T2 and T2 based on name or structure. */
519 static bool
520 odr_subtypes_equivalent_p (tree t1, tree t2, hash_set<type_pair,pair_traits> *visited)
522 bool an1, an2;
524 /* This can happen in incomplete types that should be handled earlier. */
525 gcc_assert (t1 && t2);
527 t1 = main_odr_variant (t1);
528 t2 = main_odr_variant (t2);
529 if (t1 == t2)
530 return true;
532 /* Anonymous namespace types must match exactly. */
533 an1 = type_in_anonymous_namespace_p (t1);
534 an2 = type_in_anonymous_namespace_p (t2);
535 if (an1 != an2 || an1)
536 return false;
538 /* For ODR types be sure to compare their names.
539 To support -wno-odr-type-merging we allow one type to be non-ODR
540 and other ODR even though it is a violation. */
541 if (types_odr_comparable (t1, t2))
543 if (!types_same_for_odr (t1, t2))
544 return false;
545 /* Limit recursion: If subtypes are ODR types and we know
546 that they are same, be happy. */
547 if (!get_odr_type (t1, true)->odr_violated)
548 return true;
551 /* Component types, builtins and possibly vioalting ODR types
552 have to be compared structurally. */
553 if (TREE_CODE (t1) != TREE_CODE (t2))
554 return false;
555 if ((TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
556 return false;
557 if (TYPE_NAME (t1) && DECL_NAME (TYPE_NAME (t1)) != DECL_NAME (TYPE_NAME (t2)))
558 return false;
560 type_pair pair={t1,t2};
561 if (TYPE_UID (t1) > TYPE_UID (t2))
563 pair.first = t2;
564 pair.second = t1;
566 if (visited->add (pair))
567 return true;
568 return odr_types_equivalent_p (t1, t2, false, NULL, visited);
571 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
572 violation warings. */
574 void
575 compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
577 int n1, n2;
578 if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl))
580 odr_violation_reported = true;
581 if (DECL_VIRTUAL_P (prevailing->decl))
583 varpool_node *tmp = prevailing;
584 prevailing = vtable;
585 vtable = tmp;
587 if (warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
588 OPT_Wodr,
589 "virtual table of type %qD violates one definition rule",
590 DECL_CONTEXT (vtable->decl)))
591 inform (DECL_SOURCE_LOCATION (prevailing->decl),
592 "variable of same assembler name as the virtual table is "
593 "defined in another translation unit");
594 return;
596 if (!prevailing->definition || !vtable->definition)
597 return;
598 for (n1 = 0, n2 = 0; true; n1++, n2++)
600 struct ipa_ref *ref1, *ref2;
601 bool end1, end2;
602 end1 = !prevailing->iterate_reference (n1, ref1);
603 end2 = !vtable->iterate_reference (n2, ref2);
604 if (end1 && end2)
605 return;
606 if (!end1 && !end2
607 && DECL_ASSEMBLER_NAME (ref1->referred->decl)
608 != DECL_ASSEMBLER_NAME (ref2->referred->decl)
609 && !n2
610 && !DECL_VIRTUAL_P (ref2->referred->decl)
611 && DECL_VIRTUAL_P (ref1->referred->decl))
613 if (warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 0,
614 "virtual table of type %qD contains RTTI information",
615 DECL_CONTEXT (vtable->decl)))
617 inform (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
618 "but is prevailed by one without from other translation unit");
619 inform (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
620 "RTTI will not work on this type");
622 n2++;
623 end2 = !vtable->iterate_reference (n2, ref2);
625 if (!end1 && !end2
626 && DECL_ASSEMBLER_NAME (ref1->referred->decl)
627 != DECL_ASSEMBLER_NAME (ref2->referred->decl)
628 && !n1
629 && !DECL_VIRTUAL_P (ref1->referred->decl)
630 && DECL_VIRTUAL_P (ref2->referred->decl))
632 n1++;
633 end1 = !vtable->iterate_reference (n1, ref1);
635 if (end1 || end2)
637 if (end1)
639 varpool_node *tmp = prevailing;
640 prevailing = vtable;
641 vtable = tmp;
642 ref1 = ref2;
644 if (warning_at (DECL_SOURCE_LOCATION
645 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 0,
646 "virtual table of type %qD violates "
647 "one definition rule",
648 DECL_CONTEXT (vtable->decl)))
650 inform (DECL_SOURCE_LOCATION
651 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
652 "the conflicting type defined in another translation "
653 "unit");
654 inform (DECL_SOURCE_LOCATION
655 (TYPE_NAME (DECL_CONTEXT (ref1->referring->decl))),
656 "contains additional virtual method %qD",
657 ref1->referred->decl);
659 return;
661 if (DECL_ASSEMBLER_NAME (ref1->referred->decl)
662 != DECL_ASSEMBLER_NAME (ref2->referred->decl))
664 if (warning_at (DECL_SOURCE_LOCATION
665 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 0,
666 "virtual table of type %qD violates "
667 "one definition rule ",
668 DECL_CONTEXT (vtable->decl)))
670 inform (DECL_SOURCE_LOCATION
671 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
672 "the conflicting type defined in another translation "
673 "unit");
674 inform (DECL_SOURCE_LOCATION (ref1->referred->decl),
675 "virtual method %qD", ref1->referred->decl);
676 inform (DECL_SOURCE_LOCATION (ref2->referred->decl),
677 "ought to match virtual method %qD but does not",
678 ref2->referred->decl);
679 return;
685 /* Output ODR violation warning about T1 and T2 with REASON.
686 Display location of ST1 and ST2 if REASON speaks about field or
687 method of the type.
688 If WARN is false, do nothing. Set WARNED if warning was indeed
689 output. */
691 void
692 warn_odr (tree t1, tree t2, tree st1, tree st2,
693 bool warn, bool *warned, const char *reason)
695 tree decl2 = TYPE_NAME (t2);
697 if (!warn)
698 return;
699 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr,
700 "type %qT violates one definition rule",
701 t1))
702 return;
703 if (!st1 && !st2)
705 /* For FIELD_DECL support also case where one of fields is
706 NULL - this is used when the structures have mismatching number of
707 elements. */
708 else if (!st1 || TREE_CODE (st1) == FIELD_DECL)
710 inform (DECL_SOURCE_LOCATION (decl2),
711 "a different type is defined in another translation unit");
712 if (!st1)
714 st1 = st2;
715 st2 = NULL;
717 inform (DECL_SOURCE_LOCATION (st1),
718 "the first difference of corresponding definitions is field %qD",
719 st1);
720 if (st2)
721 decl2 = st2;
723 else if (TREE_CODE (st1) == FUNCTION_DECL)
725 inform (DECL_SOURCE_LOCATION (decl2),
726 "a different type is defined in another translation unit");
727 inform (DECL_SOURCE_LOCATION (st1),
728 "the first difference of corresponding definitions is method %qD",
729 st1);
730 decl2 = st2;
732 else
733 return;
734 inform (DECL_SOURCE_LOCATION (decl2), reason);
736 if (warned)
737 *warned = true;
740 /* We already warned about ODR mismatch. T1 and T2 ought to be equivalent
741 because they are used on same place in ODR matching types.
742 They are not; inform the user. */
744 void
745 warn_types_mismatch (tree t1, tree t2)
747 if (!TYPE_NAME (t1) || !TYPE_NAME (t2))
748 return;
749 /* In Firefox it is a common bug to have same types but in
750 different namespaces. Be a bit more informative on
751 this. */
752 if (TYPE_CONTEXT (t1) && TYPE_CONTEXT (t2)
753 && (((TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL)
754 != (TREE_CODE (TYPE_CONTEXT (t2)) == NAMESPACE_DECL))
755 || (TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL
756 && (DECL_NAME (TYPE_CONTEXT (t1)) !=
757 DECL_NAME (TYPE_CONTEXT (t2))))))
758 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
759 "type %qT should match type %qT but is defined "
760 "in different namespace ",
761 t1, t2);
762 else
763 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
764 "type %qT should match type %qT",
765 t1, t2);
766 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
767 "the incompatible type is defined here");
770 /* Compare T1 and T2, report ODR violations if WARN is true and set
771 WARNED to true if anything is reported. Return true if types match.
772 If true is returned, the types are also compatible in the sense of
773 gimple_canonical_types_compatible_p. */
775 static bool
776 odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned, hash_set<type_pair,pair_traits> *visited)
778 /* Check first for the obvious case of pointer identity. */
779 if (t1 == t2)
780 return true;
781 gcc_assert (!type_in_anonymous_namespace_p (t1));
782 gcc_assert (!type_in_anonymous_namespace_p (t2));
784 /* Can't be the same type if the types don't have the same code. */
785 if (TREE_CODE (t1) != TREE_CODE (t2))
787 warn_odr (t1, t2, NULL, NULL, warn, warned,
788 G_("a different type is defined in another translation unit"));
789 return false;
792 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
794 warn_odr (t1, t2, NULL, NULL, warn, warned,
795 G_("a type with different qualifiers is defined in another "
796 "translation unit"));
797 return false;
800 if (comp_type_attributes (t1, t2) != 1)
802 warn_odr (t1, t2, NULL, NULL, warn, warned,
803 G_("a type with attributes "
804 "is defined in another translation unit"));
805 return false;
808 if (TREE_CODE (t1) == ENUMERAL_TYPE)
810 tree v1, v2;
811 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
812 v1 && v2 ; v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
814 if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
816 warn_odr (t1, t2, NULL, NULL, warn, warned,
817 G_("an enum with different value name"
818 " is defined in another translation unit"));
819 return false;
821 if (TREE_VALUE (v1) != TREE_VALUE (v2)
822 && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1)),
823 DECL_INITIAL (TREE_VALUE (v2)), 0))
825 warn_odr (t1, t2, NULL, NULL, warn, warned,
826 G_("an enum with different values is defined"
827 " in another translation unit"));
828 return false;
831 if (v1 || v2)
833 warn_odr (t1, t2, NULL, NULL, warn, warned,
834 G_("an enum with mismatching number of values "
835 "is defined in another translation unit"));
836 return false;
840 /* Non-aggregate types can be handled cheaply. */
841 if (INTEGRAL_TYPE_P (t1)
842 || SCALAR_FLOAT_TYPE_P (t1)
843 || FIXED_POINT_TYPE_P (t1)
844 || TREE_CODE (t1) == VECTOR_TYPE
845 || TREE_CODE (t1) == COMPLEX_TYPE
846 || TREE_CODE (t1) == OFFSET_TYPE
847 || POINTER_TYPE_P (t1))
849 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
851 warn_odr (t1, t2, NULL, NULL, warn, warned,
852 G_("a type with different precision is defined "
853 "in another translation unit"));
854 return false;
856 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
858 warn_odr (t1, t2, NULL, NULL, warn, warned,
859 G_("a type with different signedness is defined "
860 "in another translation unit"));
861 return false;
864 if (TREE_CODE (t1) == INTEGER_TYPE
865 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
867 /* char WRT uint_8? */
868 warn_odr (t1, t2, NULL, NULL, warn, warned,
869 G_("a different type is defined in another "
870 "translation unit"));
871 return false;
874 /* For canonical type comparisons we do not want to build SCCs
875 so we cannot compare pointed-to types. But we can, for now,
876 require the same pointed-to type kind and match what
877 useless_type_conversion_p would do. */
878 if (POINTER_TYPE_P (t1))
880 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
881 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
883 warn_odr (t1, t2, NULL, NULL, warn, warned,
884 G_("it is defined as a pointer in different address "
885 "space in another translation unit"));
886 return false;
889 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
891 warn_odr (t1, t2, NULL, NULL, warn, warned,
892 G_("it is defined as a pointer to different type "
893 "in another translation unit"));
894 if (warn && warned)
895 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
896 return false;
900 if ((TREE_CODE (t1) == VECTOR_TYPE || TREE_CODE (t1) == COMPLEX_TYPE)
901 && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
903 /* Probably specific enough. */
904 warn_odr (t1, t2, NULL, NULL, warn, warned,
905 G_("a different type is defined "
906 "in another translation unit"));
907 if (warn && warned)
908 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
909 return false;
912 /* Do type-specific comparisons. */
913 else switch (TREE_CODE (t1))
915 case ARRAY_TYPE:
917 /* Array types are the same if the element types are the same and
918 the number of elements are the same. */
919 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
921 warn_odr (t1, t2, NULL, NULL, warn, warned,
922 G_("a different type is defined in another "
923 "translation unit"));
924 if (warn && warned)
925 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
927 gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
928 gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
929 == TYPE_NONALIASED_COMPONENT (t2));
931 tree i1 = TYPE_DOMAIN (t1);
932 tree i2 = TYPE_DOMAIN (t2);
934 /* For an incomplete external array, the type domain can be
935 NULL_TREE. Check this condition also. */
936 if (i1 == NULL_TREE || i2 == NULL_TREE)
937 return true;
939 tree min1 = TYPE_MIN_VALUE (i1);
940 tree min2 = TYPE_MIN_VALUE (i2);
941 tree max1 = TYPE_MAX_VALUE (i1);
942 tree max2 = TYPE_MAX_VALUE (i2);
944 /* In C++, minimums should be always 0. */
945 gcc_assert (min1 == min2);
946 if (!operand_equal_p (max1, max2, 0))
948 warn_odr (t1, t2, NULL, NULL, warn, warned,
949 G_("an array of different size is defined "
950 "in another translation unit"));
951 return false;
954 break;
956 case METHOD_TYPE:
957 case FUNCTION_TYPE:
958 /* Function types are the same if the return type and arguments types
959 are the same. */
960 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
962 warn_odr (t1, t2, NULL, NULL, warn, warned,
963 G_("has different return value "
964 "in another translation unit"));
965 if (warn && warned)
966 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
967 return false;
970 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
971 return true;
972 else
974 tree parms1, parms2;
976 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
977 parms1 && parms2;
978 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
980 if (!odr_subtypes_equivalent_p
981 (TREE_VALUE (parms1), TREE_VALUE (parms2), visited))
983 warn_odr (t1, t2, NULL, NULL, warn, warned,
984 G_("has different parameters in another "
985 "translation unit"));
986 if (warn && warned)
987 warn_types_mismatch (TREE_VALUE (parms1),
988 TREE_VALUE (parms2));
989 return false;
993 if (parms1 || parms2)
995 warn_odr (t1, t2, NULL, NULL, warn, warned,
996 G_("has different parameters "
997 "in another translation unit"));
998 return false;
1001 return true;
1004 case RECORD_TYPE:
1005 case UNION_TYPE:
1006 case QUAL_UNION_TYPE:
1008 tree f1, f2;
1010 /* For aggregate types, all the fields must be the same. */
1011 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1013 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1014 f1 || f2;
1015 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1017 /* Skip non-fields. */
1018 while (f1 && TREE_CODE (f1) != FIELD_DECL)
1019 f1 = TREE_CHAIN (f1);
1020 while (f2 && TREE_CODE (f2) != FIELD_DECL)
1021 f2 = TREE_CHAIN (f2);
1022 if (!f1 || !f2)
1023 break;
1024 if (DECL_ARTIFICIAL (f1) != DECL_ARTIFICIAL (f2))
1025 break;
1026 if (DECL_NAME (f1) != DECL_NAME (f2)
1027 && !DECL_ARTIFICIAL (f1))
1029 warn_odr (t1, t2, f1, f2, warn, warned,
1030 G_("a field with different name is defined "
1031 "in another translation unit"));
1032 return false;
1034 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1), TREE_TYPE (f2), visited))
1036 /* Do not warn about artificial fields and just go into generic
1037 field mismatch warning. */
1038 if (DECL_ARTIFICIAL (f1))
1039 break;
1041 warn_odr (t1, t2, f1, f2, warn, warned,
1042 G_("a field of same name but different type "
1043 "is defined in another translation unit"));
1044 if (warn && warned)
1045 warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2));
1046 return false;
1048 if (!gimple_compare_field_offset (f1, f2))
1050 /* Do not warn about artificial fields and just go into generic
1051 field mismatch warning. */
1052 if (DECL_ARTIFICIAL (f1))
1053 break;
1054 warn_odr (t1, t2, t1, t2, warn, warned,
1055 G_("fields has different layout "
1056 "in another translation unit"));
1057 return false;
1059 gcc_assert (DECL_NONADDRESSABLE_P (f1)
1060 == DECL_NONADDRESSABLE_P (f2));
1063 /* If one aggregate has more fields than the other, they
1064 are not the same. */
1065 if (f1 || f2)
1067 if (f1 && DECL_ARTIFICIAL (f1))
1068 f1 = NULL;
1069 if (f2 && DECL_ARTIFICIAL (f2))
1070 f2 = NULL;
1071 if (f1 || f2)
1072 warn_odr (t1, t2, f1, f2, warn, warned,
1073 G_("a type with different number of fields "
1074 "is defined in another translation unit"));
1075 /* Ideally we should never get this generic message. */
1076 else
1077 warn_odr (t1, t2, f1, f2, warn, warned,
1078 G_("a type with different memory representation "
1079 "is defined in another translation unit"));
1081 return false;
1083 if ((TYPE_MAIN_VARIANT (t1) == t1 || TYPE_MAIN_VARIANT (t2) == t2)
1084 && (TYPE_METHODS (TYPE_MAIN_VARIANT (t1))
1085 != TYPE_METHODS (TYPE_MAIN_VARIANT (t2))))
1087 for (f1 = TYPE_METHODS (TYPE_MAIN_VARIANT (t1)),
1088 f2 = TYPE_METHODS (TYPE_MAIN_VARIANT (t2));
1089 f1 && f2 ; f1 = DECL_CHAIN (f1), f2 = DECL_CHAIN (f2))
1091 if (DECL_ASSEMBLER_NAME (f1) != DECL_ASSEMBLER_NAME (f2))
1093 warn_odr (t1, t2, f1, f2, warn, warned,
1094 G_("a different method of same type "
1095 "is defined in another translation unit"));
1096 return false;
1098 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1100 warn_odr (t1, t2, f1, f2, warn, warned,
1101 G_("s definition that differs by virtual "
1102 "keyword in another translation unit"));
1103 return false;
1105 if (DECL_VINDEX (f1) != DECL_VINDEX (f2))
1107 warn_odr (t1, t2, f1, f2, warn, warned,
1108 G_("virtual table layout differs in another "
1109 "translation unit"));
1110 return false;
1112 if (odr_subtypes_equivalent_p (TREE_TYPE (f1), TREE_TYPE (f2), visited))
1114 warn_odr (t1, t2, f1, f2, warn, warned,
1115 G_("method with incompatible type is defined "
1116 "in another translation unit"));
1117 return false;
1120 if (f1 || f2)
1122 warn_odr (t1, t2, NULL, NULL, warn, warned,
1123 G_("a type with different number of methods "
1124 "is defined in another translation unit"));
1125 return false;
1129 break;
1131 case VOID_TYPE:
1132 break;
1134 default:
1135 debug_tree (t1);
1136 gcc_unreachable ();
1139 /* Those are better to come last as they are utterly uninformative. */
1140 if (TYPE_SIZE (t1) && TYPE_SIZE (t2)
1141 && !operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0))
1143 warn_odr (t1, t2, NULL, NULL, warn, warned,
1144 G_("a type with different size "
1145 "is defined in another translation unit"));
1146 return false;
1148 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
1149 && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
1151 warn_odr (t1, t2, NULL, NULL, warn, warned,
1152 G_("a type with different alignment "
1153 "is defined in another translation unit"));
1154 return false;
1156 gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2)
1157 || operand_equal_p (TYPE_SIZE_UNIT (t1),
1158 TYPE_SIZE_UNIT (t2), 0));
1159 return true;
1162 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1163 from VAL->type. This may happen in LTO where tree merging did not merge
1164 all variants of the same type. It may or may not mean the ODR violation.
1165 Add it to the list of duplicates and warn on some violations. */
1167 static bool
1168 add_type_duplicate (odr_type val, tree type)
1170 bool build_bases = false;
1171 if (!val->types_set)
1172 val->types_set = new hash_set<tree>;
1174 /* Always prefer complete type to be the leader. */
1175 if (!COMPLETE_TYPE_P (val->type)
1176 && COMPLETE_TYPE_P (type))
1178 tree tmp = type;
1180 build_bases = true;
1181 type = val->type;
1182 val->type = tmp;
1185 /* See if this duplicate is new. */
1186 if (!val->types_set->add (type))
1188 bool merge = true;
1189 bool base_mismatch = false;
1190 unsigned int i,j;
1191 bool warned = false;
1192 hash_set<type_pair,pair_traits> visited;
1194 gcc_assert (in_lto_p);
1195 vec_safe_push (val->types, type);
1197 /* First we compare memory layout. */
1198 if (!odr_types_equivalent_p (val->type, type, !flag_ltrans && !val->odr_violated,
1199 &warned, &visited))
1201 merge = false;
1202 odr_violation_reported = true;
1203 val->odr_violated = true;
1204 if (symtab->dump_file)
1206 fprintf (symtab->dump_file, "ODR violation\n");
1208 print_node (symtab->dump_file, "", val->type, 0);
1209 putc ('\n',symtab->dump_file);
1210 print_node (symtab->dump_file, "", type, 0);
1211 putc ('\n',symtab->dump_file);
1215 /* Next sanity check that bases are the same. If not, we will end
1216 up producing wrong answers. */
1217 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1218 && TREE_CODE (val->type) == RECORD_TYPE
1219 && TREE_CODE (type) == RECORD_TYPE
1220 && TYPE_BINFO (val->type) && TYPE_BINFO (type))
1222 for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1223 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (TYPE_BINFO (type), i)))
1225 odr_type base = get_odr_type
1226 (BINFO_TYPE
1227 (BINFO_BASE_BINFO (TYPE_BINFO (type),
1228 i)),
1229 true);
1230 if (val->bases.length () <= j || val->bases[j] != base)
1231 base_mismatch = true;
1232 j++;
1234 if (base_mismatch)
1236 merge = false;
1237 odr_violation_reported = true;
1239 if (!warned && !val->odr_violated)
1240 warn_odr (type, val->type, NULL, NULL, !warned, &warned,
1241 "a type with the same name but different bases is "
1242 "defined in another translation unit");
1243 val->odr_violated = true;
1244 if (symtab->dump_file)
1246 fprintf (symtab->dump_file, "ODR bse violation or merging bug?\n");
1248 print_node (symtab->dump_file, "", val->type, 0);
1249 putc ('\n',symtab->dump_file);
1250 print_node (symtab->dump_file, "", type, 0);
1251 putc ('\n',symtab->dump_file);
1256 /* Regularize things a little. During LTO same types may come with
1257 different BINFOs. Either because their virtual table was
1258 not merged by tree merging and only later at decl merging or
1259 because one type comes with external vtable, while other
1260 with internal. We want to merge equivalent binfos to conserve
1261 memory and streaming overhead.
1263 The external vtables are more harmful: they contain references
1264 to external declarations of methods that may be defined in the
1265 merged LTO unit. For this reason we absolutely need to remove
1266 them and replace by internal variants. Not doing so will lead
1267 to incomplete answers from possible_polymorphic_call_targets.
1269 FIXME: disable for now; because ODR types are now build during
1270 streaming in, the variants do not need to be linked to the type,
1271 yet. We need to do the merging in cleanup pass to be implemented
1272 soon. */
1273 if (!flag_ltrans && merge
1274 && 0
1275 && TREE_CODE (val->type) == RECORD_TYPE
1276 && TREE_CODE (type) == RECORD_TYPE
1277 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1278 && TYPE_MAIN_VARIANT (type) == type
1279 && TYPE_MAIN_VARIANT (val->type) == val->type
1280 && BINFO_VTABLE (TYPE_BINFO (val->type))
1281 && BINFO_VTABLE (TYPE_BINFO (type)))
1283 tree master_binfo = TYPE_BINFO (val->type);
1284 tree v1 = BINFO_VTABLE (master_binfo);
1285 tree v2 = BINFO_VTABLE (TYPE_BINFO (type));
1287 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
1289 gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR
1290 && operand_equal_p (TREE_OPERAND (v1, 1),
1291 TREE_OPERAND (v2, 1), 0));
1292 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
1293 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
1295 gcc_assert (DECL_ASSEMBLER_NAME (v1)
1296 == DECL_ASSEMBLER_NAME (v2));
1298 if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2))
1300 unsigned int i;
1302 set_type_binfo (val->type, TYPE_BINFO (type));
1303 for (i = 0; i < val->types->length (); i++)
1305 if (TYPE_BINFO ((*val->types)[i])
1306 == master_binfo)
1307 set_type_binfo ((*val->types)[i], TYPE_BINFO (type));
1309 BINFO_TYPE (TYPE_BINFO (type)) = val->type;
1311 else
1312 set_type_binfo (type, master_binfo);
1315 return build_bases;
1318 /* Get ODR type hash entry for TYPE. If INSERT is true, create
1319 possibly new entry. */
1321 odr_type
1322 get_odr_type (tree type, bool insert)
1324 odr_type_d **slot;
1325 odr_type val;
1326 hashval_t hash;
1327 bool build_bases = false;
1328 bool insert_to_odr_array = false;
1329 int base_id = -1;
1331 type = main_odr_variant (type);
1333 hash = hash_type_name (type);
1334 slot = odr_hash->find_slot_with_hash (type, hash,
1335 insert ? INSERT : NO_INSERT);
1336 if (!slot)
1337 return NULL;
1339 /* See if we already have entry for type. */
1340 if (*slot)
1342 val = *slot;
1344 /* With LTO we need to support multiple tree representation of
1345 the same ODR type. */
1346 if (val->type != type)
1347 build_bases = add_type_duplicate (val, type);
1349 else
1351 val = ggc_cleared_alloc<odr_type_d> ();
1352 val->type = type;
1353 val->bases = vNULL;
1354 val->derived_types = vNULL;
1355 val->anonymous_namespace = type_in_anonymous_namespace_p (type);
1356 build_bases = COMPLETE_TYPE_P (val->type);
1357 insert_to_odr_array = true;
1360 if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1361 && type == TYPE_MAIN_VARIANT (type))
1363 tree binfo = TYPE_BINFO (type);
1364 unsigned int i;
1366 gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) = type);
1368 val->all_derivations_known = type_all_derivations_known_p (type);
1369 *slot = val;
1370 for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
1371 /* For now record only polymorphic types. other are
1372 pointless for devirtualization and we can not precisely
1373 determine ODR equivalency of these during LTO. */
1374 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
1376 odr_type base = get_odr_type (BINFO_TYPE (BINFO_BASE_BINFO (binfo,
1377 i)),
1378 true);
1379 gcc_assert (TYPE_MAIN_VARIANT (base->type) == base->type);
1380 base->derived_types.safe_push (val);
1381 val->bases.safe_push (base);
1382 if (base->id > base_id)
1383 base_id = base->id;
1386 /* Ensure that type always appears after bases. */
1387 if (insert_to_odr_array)
1389 if (odr_types_ptr)
1390 val->id = odr_types.length ();
1391 vec_safe_push (odr_types_ptr, val);
1393 else if (base_id > val->id)
1395 odr_types[val->id] = 0;
1396 /* Be sure we did not recorded any derived types; these may need
1397 renumbering too. */
1398 gcc_assert (val->derived_types.length() == 0);
1399 if (odr_types_ptr)
1400 val->id = odr_types.length ();
1401 vec_safe_push (odr_types_ptr, val);
1403 return val;
1406 /* Add TYPE od ODR type hash. */
1408 void
1409 register_odr_type (tree type)
1411 if (!odr_hash)
1412 odr_hash = new odr_hash_type (23);
1413 /* Arrange things to be nicer and insert main variants first. */
1414 if (odr_type_p (TYPE_MAIN_VARIANT (type)))
1415 get_odr_type (TYPE_MAIN_VARIANT (type), true);
1416 if (TYPE_MAIN_VARIANT (type) != type)
1417 get_odr_type (type, true);
1420 /* Return true if type is known to have no derivations. */
1422 bool
1423 type_known_to_have_no_deriavations_p (tree t)
1425 return (type_all_derivations_known_p (t)
1426 && (TYPE_FINAL_P (t)
1427 || (odr_hash
1428 && !get_odr_type (t, true)->derived_types.length())));
1431 /* Dump ODR type T and all its derrived type. INDENT specify indentation for
1432 recusive printing. */
1434 static void
1435 dump_odr_type (FILE *f, odr_type t, int indent=0)
1437 unsigned int i;
1438 fprintf (f, "%*s type %i: ", indent * 2, "", t->id);
1439 print_generic_expr (f, t->type, TDF_SLIM);
1440 fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":"");
1441 fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":"");
1442 if (TYPE_NAME (t->type))
1444 fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
1445 DECL_SOURCE_FILE (TYPE_NAME (t->type)),
1446 DECL_SOURCE_LINE (TYPE_NAME (t->type)));
1448 if (t->bases.length ())
1450 fprintf (f, "%*s base odr type ids: ", indent * 2, "");
1451 for (i = 0; i < t->bases.length (); i++)
1452 fprintf (f, " %i", t->bases[i]->id);
1453 fprintf (f, "\n");
1455 if (t->derived_types.length ())
1457 fprintf (f, "%*s derived types:\n", indent * 2, "");
1458 for (i = 0; i < t->derived_types.length (); i++)
1459 dump_odr_type (f, t->derived_types[i], indent + 1);
1461 fprintf (f, "\n");
1464 /* Dump the type inheritance graph. */
1466 static void
1467 dump_type_inheritance_graph (FILE *f)
1469 unsigned int i;
1470 if (!odr_types_ptr)
1471 return;
1472 fprintf (f, "\n\nType inheritance graph:\n");
1473 for (i = 0; i < odr_types.length (); i++)
1475 if (odr_types[i] && odr_types[i]->bases.length () == 0)
1476 dump_odr_type (f, odr_types[i]);
1478 for (i = 0; i < odr_types.length (); i++)
1480 if (odr_types[i] && odr_types[i]->types && odr_types[i]->types->length ())
1482 unsigned int j;
1483 fprintf (f, "Duplicate tree types for odr type %i\n", i);
1484 print_node (f, "", odr_types[i]->type, 0);
1485 for (j = 0; j < odr_types[i]->types->length (); j++)
1487 tree t;
1488 fprintf (f, "duplicate #%i\n", j);
1489 print_node (f, "", (*odr_types[i]->types)[j], 0);
1490 t = (*odr_types[i]->types)[j];
1491 while (TYPE_P (t) && TYPE_CONTEXT (t))
1493 t = TYPE_CONTEXT (t);
1494 print_node (f, "", t, 0);
1496 putc ('\n',f);
1502 /* Given method type T, return type of class it belongs to.
1503 Lookup this pointer and get its type. */
1505 tree
1506 method_class_type (const_tree t)
1508 tree first_parm_type = TREE_VALUE (TYPE_ARG_TYPES (t));
1509 gcc_assert (TREE_CODE (t) == METHOD_TYPE);
1511 return TREE_TYPE (first_parm_type);
1514 /* Initialize IPA devirt and build inheritance tree graph. */
1516 void
1517 build_type_inheritance_graph (void)
1519 struct symtab_node *n;
1520 FILE *inheritance_dump_file;
1521 int flags;
1523 if (odr_hash)
1524 return;
1525 timevar_push (TV_IPA_INHERITANCE);
1526 inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
1527 odr_hash = new odr_hash_type (23);
1529 /* We reconstruct the graph starting of types of all methods seen in the
1530 the unit. */
1531 FOR_EACH_SYMBOL (n)
1532 if (is_a <cgraph_node *> (n)
1533 && DECL_VIRTUAL_P (n->decl)
1534 && n->real_symbol_p ())
1535 get_odr_type (TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (n->decl))),
1536 true);
1538 /* Look also for virtual tables of types that do not define any methods.
1540 We need it in a case where class B has virtual base of class A
1541 re-defining its virtual method and there is class C with no virtual
1542 methods with B as virtual base.
1544 Here we output B's virtual method in two variant - for non-virtual
1545 and virtual inheritance. B's virtual table has non-virtual version,
1546 while C's has virtual.
1548 For this reason we need to know about C in order to include both
1549 variants of B. More correctly, record_target_from_binfo should
1550 add both variants of the method when walking B, but we have no
1551 link in between them.
1553 We rely on fact that either the method is exported and thus we
1554 assume it is called externally or C is in anonymous namespace and
1555 thus we will see the vtable. */
1557 else if (is_a <varpool_node *> (n)
1558 && DECL_VIRTUAL_P (n->decl)
1559 && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE
1560 && TYPE_BINFO (DECL_CONTEXT (n->decl))
1561 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl))))
1562 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n->decl)), true);
1563 if (inheritance_dump_file)
1565 dump_type_inheritance_graph (inheritance_dump_file);
1566 dump_end (TDI_inheritance, inheritance_dump_file);
1568 timevar_pop (TV_IPA_INHERITANCE);
1571 /* Return true if N has reference from live virtual table
1572 (and thus can be a destination of polymorphic call).
1573 Be conservatively correct when callgraph is not built or
1574 if the method may be referred externally. */
1576 static bool
1577 referenced_from_vtable_p (struct cgraph_node *node)
1579 int i;
1580 struct ipa_ref *ref;
1581 bool found = false;
1583 if (node->externally_visible
1584 || DECL_EXTERNAL (node->decl)
1585 || node->used_from_other_partition)
1586 return true;
1588 /* Keep this test constant time.
1589 It is unlikely this can happen except for the case where speculative
1590 devirtualization introduced many speculative edges to this node.
1591 In this case the target is very likely alive anyway. */
1592 if (node->ref_list.referring.length () > 100)
1593 return true;
1595 /* We need references built. */
1596 if (symtab->state <= CONSTRUCTION)
1597 return true;
1599 for (i = 0; node->iterate_referring (i, ref); i++)
1601 if ((ref->use == IPA_REF_ALIAS
1602 && referenced_from_vtable_p (dyn_cast<cgraph_node *> (ref->referring)))
1603 || (ref->use == IPA_REF_ADDR
1604 && TREE_CODE (ref->referring->decl) == VAR_DECL
1605 && DECL_VIRTUAL_P (ref->referring->decl)))
1607 found = true;
1608 break;
1610 return found;
1613 /* If TARGET has associated node, record it in the NODES array.
1614 CAN_REFER specify if program can refer to the target directly.
1615 if TARGET is unknown (NULL) or it can not be inserted (for example because
1616 its body was already removed and there is no way to refer to it), clear
1617 COMPLETEP. */
1619 static void
1620 maybe_record_node (vec <cgraph_node *> &nodes,
1621 tree target, hash_set<tree> *inserted,
1622 bool can_refer,
1623 bool *completep)
1625 struct cgraph_node *target_node, *alias_target;
1626 enum availability avail;
1628 /* cxa_pure_virtual and __builtin_unreachable do not need to be added into
1629 list of targets; the runtime effect of calling them is undefined.
1630 Only "real" virtual methods should be accounted. */
1631 if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE)
1632 return;
1634 if (!can_refer)
1636 /* The only case when method of anonymous namespace becomes unreferable
1637 is when we completely optimized it out. */
1638 if (flag_ltrans
1639 || !target
1640 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
1641 *completep = false;
1642 return;
1645 if (!target)
1646 return;
1648 target_node = cgraph_node::get (target);
1650 /* Preffer alias target over aliases, so we do not get confused by
1651 fake duplicates. */
1652 if (target_node)
1654 alias_target = target_node->ultimate_alias_target (&avail);
1655 if (target_node != alias_target
1656 && avail >= AVAIL_AVAILABLE
1657 && target_node->get_availability ())
1658 target_node = alias_target;
1661 /* Method can only be called by polymorphic call if any
1662 of vtables refering to it are alive.
1664 While this holds for non-anonymous functions, too, there are
1665 cases where we want to keep them in the list; for example
1666 inline functions with -fno-weak are static, but we still
1667 may devirtualize them when instance comes from other unit.
1668 The same holds for LTO.
1670 Currently we ignore these functions in speculative devirtualization.
1671 ??? Maybe it would make sense to be more aggressive for LTO even
1672 eslewhere. */
1673 if (!flag_ltrans
1674 && type_in_anonymous_namespace_p (DECL_CONTEXT (target))
1675 && (!target_node
1676 || !referenced_from_vtable_p (target_node)))
1678 /* See if TARGET is useful function we can deal with. */
1679 else if (target_node != NULL
1680 && (TREE_PUBLIC (target)
1681 || DECL_EXTERNAL (target)
1682 || target_node->definition)
1683 && target_node->real_symbol_p ())
1685 gcc_assert (!target_node->global.inlined_to);
1686 gcc_assert (target_node->real_symbol_p ());
1687 if (!inserted->add (target))
1689 cached_polymorphic_call_targets->add (target_node);
1690 nodes.safe_push (target_node);
1693 else if (completep
1694 && (!type_in_anonymous_namespace_p
1695 (DECL_CONTEXT (target))
1696 || flag_ltrans))
1697 *completep = false;
1700 /* See if BINFO's type match OUTER_TYPE. If so, lookup
1701 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
1702 method in vtable and insert method to NODES array
1703 or BASES_TO_CONSIDER if this array is non-NULL.
1704 Otherwise recurse to base BINFOs.
1705 This match what get_binfo_at_offset does, but with offset
1706 being unknown.
1708 TYPE_BINFOS is a stack of BINFOS of types with defined
1709 virtual table seen on way from class type to BINFO.
1711 MATCHED_VTABLES tracks virtual tables we already did lookup
1712 for virtual function in. INSERTED tracks nodes we already
1713 inserted.
1715 ANONYMOUS is true if BINFO is part of anonymous namespace.
1717 Clear COMPLETEP when we hit unreferable target.
1720 static void
1721 record_target_from_binfo (vec <cgraph_node *> &nodes,
1722 vec <tree> *bases_to_consider,
1723 tree binfo,
1724 tree otr_type,
1725 vec <tree> &type_binfos,
1726 HOST_WIDE_INT otr_token,
1727 tree outer_type,
1728 HOST_WIDE_INT offset,
1729 hash_set<tree> *inserted,
1730 hash_set<tree> *matched_vtables,
1731 bool anonymous,
1732 bool *completep)
1734 tree type = BINFO_TYPE (binfo);
1735 int i;
1736 tree base_binfo;
1739 if (BINFO_VTABLE (binfo))
1740 type_binfos.safe_push (binfo);
1741 if (types_same_for_odr (type, outer_type))
1743 int i;
1744 tree type_binfo = NULL;
1746 /* Lookup BINFO with virtual table. For normal types it is always last
1747 binfo on stack. */
1748 for (i = type_binfos.length () - 1; i >= 0; i--)
1749 if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo))
1751 type_binfo = type_binfos[i];
1752 break;
1754 if (BINFO_VTABLE (binfo))
1755 type_binfos.pop ();
1756 /* If this is duplicated BINFO for base shared by virtual inheritance,
1757 we may not have its associated vtable. This is not a problem, since
1758 we will walk it on the other path. */
1759 if (!type_binfo)
1760 return;
1761 tree inner_binfo = get_binfo_at_offset (type_binfo,
1762 offset, otr_type);
1763 if (!inner_binfo)
1765 gcc_assert (odr_violation_reported);
1766 return;
1768 /* For types in anonymous namespace first check if the respective vtable
1769 is alive. If not, we know the type can't be called. */
1770 if (!flag_ltrans && anonymous)
1772 tree vtable = BINFO_VTABLE (inner_binfo);
1773 varpool_node *vnode;
1775 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
1776 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
1777 vnode = varpool_node::get (vtable);
1778 if (!vnode || !vnode->definition)
1779 return;
1781 gcc_assert (inner_binfo);
1782 if (bases_to_consider
1783 ? !matched_vtables->contains (BINFO_VTABLE (inner_binfo))
1784 : !matched_vtables->add (BINFO_VTABLE (inner_binfo)))
1786 bool can_refer;
1787 tree target = gimple_get_virt_method_for_binfo (otr_token,
1788 inner_binfo,
1789 &can_refer);
1790 if (!bases_to_consider)
1791 maybe_record_node (nodes, target, inserted, can_refer, completep);
1792 /* Destructors are never called via construction vtables. */
1793 else if (!target || !DECL_CXX_DESTRUCTOR_P (target))
1794 bases_to_consider->safe_push (target);
1796 return;
1799 /* Walk bases. */
1800 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1801 /* Walking bases that have no virtual method is pointless excercise. */
1802 if (polymorphic_type_binfo_p (base_binfo))
1803 record_target_from_binfo (nodes, bases_to_consider, base_binfo, otr_type,
1804 type_binfos,
1805 otr_token, outer_type, offset, inserted,
1806 matched_vtables, anonymous, completep);
1807 if (BINFO_VTABLE (binfo))
1808 type_binfos.pop ();
1811 /* Lookup virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
1812 of TYPE, insert them to NODES, recurse into derived nodes.
1813 INSERTED is used to avoid duplicate insertions of methods into NODES.
1814 MATCHED_VTABLES are used to avoid duplicate walking vtables.
1815 Clear COMPLETEP if unreferable target is found.
1817 If CONSIDER_CONSTURCTION is true, record to BASES_TO_CONSDIER
1818 all cases where BASE_SKIPPED is true (because the base is abstract
1819 class). */
1821 static void
1822 possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
1823 hash_set<tree> *inserted,
1824 hash_set<tree> *matched_vtables,
1825 tree otr_type,
1826 odr_type type,
1827 HOST_WIDE_INT otr_token,
1828 tree outer_type,
1829 HOST_WIDE_INT offset,
1830 bool *completep,
1831 vec <tree> &bases_to_consider,
1832 bool consider_construction)
1834 tree binfo = TYPE_BINFO (type->type);
1835 unsigned int i;
1836 vec <tree> type_binfos = vNULL;
1837 bool possibly_instantiated = type_possibly_instantiated_p (type->type);
1839 /* We may need to consider types w/o instances because of possible derived
1840 types using their methods either directly or via construction vtables.
1841 We are safe to skip them when all derivations are known, since we will
1842 handle them later.
1843 This is done by recording them to BASES_TO_CONSIDER array. */
1844 if (possibly_instantiated || consider_construction)
1846 record_target_from_binfo (nodes,
1847 (!possibly_instantiated
1848 && type_all_derivations_known_p (type->type))
1849 ? &bases_to_consider : NULL,
1850 binfo, otr_type, type_binfos, otr_token,
1851 outer_type, offset,
1852 inserted, matched_vtables,
1853 type->anonymous_namespace, completep);
1855 type_binfos.release ();
1856 for (i = 0; i < type->derived_types.length (); i++)
1857 possible_polymorphic_call_targets_1 (nodes, inserted,
1858 matched_vtables,
1859 otr_type,
1860 type->derived_types[i],
1861 otr_token, outer_type, offset, completep,
1862 bases_to_consider, consider_construction);
1865 /* Cache of queries for polymorphic call targets.
1867 Enumerating all call targets may get expensive when there are many
1868 polymorphic calls in the program, so we memoize all the previous
1869 queries and avoid duplicated work. */
1871 struct polymorphic_call_target_d
1873 HOST_WIDE_INT otr_token;
1874 ipa_polymorphic_call_context context;
1875 odr_type type;
1876 vec <cgraph_node *> targets;
1877 tree decl_warning;
1878 int type_warning;
1879 bool complete;
1880 bool speculative;
1883 /* Polymorphic call target cache helpers. */
1885 struct polymorphic_call_target_hasher
1887 typedef polymorphic_call_target_d value_type;
1888 typedef polymorphic_call_target_d compare_type;
1889 static inline hashval_t hash (const value_type *);
1890 static inline bool equal (const value_type *, const compare_type *);
1891 static inline void remove (value_type *);
1894 /* Return the computed hashcode for ODR_QUERY. */
1896 inline hashval_t
1897 polymorphic_call_target_hasher::hash (const value_type *odr_query)
1899 inchash::hash hstate (odr_query->otr_token);
1901 hstate.add_wide_int (odr_query->type->id);
1902 hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
1903 hstate.add_wide_int (odr_query->context.offset);
1905 if (odr_query->context.speculative_outer_type)
1907 hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
1908 hstate.add_wide_int (odr_query->context.speculative_offset);
1910 hstate.add_flag (odr_query->speculative);
1911 hstate.add_flag (odr_query->context.maybe_in_construction);
1912 hstate.add_flag (odr_query->context.maybe_derived_type);
1913 hstate.add_flag (odr_query->context.speculative_maybe_derived_type);
1914 hstate.commit_flag ();
1915 return hstate.end ();
1918 /* Compare cache entries T1 and T2. */
1920 inline bool
1921 polymorphic_call_target_hasher::equal (const value_type *t1,
1922 const compare_type *t2)
1924 return (t1->type == t2->type && t1->otr_token == t2->otr_token
1925 && t1->speculative == t2->speculative
1926 && t1->context.offset == t2->context.offset
1927 && t1->context.speculative_offset == t2->context.speculative_offset
1928 && t1->context.outer_type == t2->context.outer_type
1929 && t1->context.speculative_outer_type == t2->context.speculative_outer_type
1930 && t1->context.maybe_in_construction
1931 == t2->context.maybe_in_construction
1932 && t1->context.maybe_derived_type == t2->context.maybe_derived_type
1933 && (t1->context.speculative_maybe_derived_type
1934 == t2->context.speculative_maybe_derived_type));
1937 /* Remove entry in polymorphic call target cache hash. */
1939 inline void
1940 polymorphic_call_target_hasher::remove (value_type *v)
1942 v->targets.release ();
1943 free (v);
1946 /* Polymorphic call target query cache. */
1948 typedef hash_table<polymorphic_call_target_hasher>
1949 polymorphic_call_target_hash_type;
1950 static polymorphic_call_target_hash_type *polymorphic_call_target_hash;
1952 /* Destroy polymorphic call target query cache. */
1954 static void
1955 free_polymorphic_call_targets_hash ()
1957 if (cached_polymorphic_call_targets)
1959 delete polymorphic_call_target_hash;
1960 polymorphic_call_target_hash = NULL;
1961 delete cached_polymorphic_call_targets;
1962 cached_polymorphic_call_targets = NULL;
1966 /* When virtual function is removed, we may need to flush the cache. */
1968 static void
1969 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
1971 if (cached_polymorphic_call_targets
1972 && cached_polymorphic_call_targets->contains (n))
1973 free_polymorphic_call_targets_hash ();
1976 /* Lookup base of BINFO that has virtual table VTABLE with OFFSET. */
1978 tree
1979 subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
1980 tree vtable)
1982 tree v = BINFO_VTABLE (binfo);
1983 int i;
1984 tree base_binfo;
1985 unsigned HOST_WIDE_INT this_offset;
1987 if (v)
1989 if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
1990 gcc_unreachable ();
1992 if (offset == this_offset
1993 && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
1994 return binfo;
1997 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1998 if (polymorphic_type_binfo_p (base_binfo))
2000 base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable);
2001 if (base_binfo)
2002 return base_binfo;
2004 return NULL;
2007 /* T is known constant value of virtual table pointer.
2008 Store virtual table to V and its offset to OFFSET.
2009 Return false if T does not look like virtual table reference. */
2011 bool
2012 vtable_pointer_value_to_vtable (const_tree t, tree *v,
2013 unsigned HOST_WIDE_INT *offset)
2015 /* We expect &MEM[(void *)&virtual_table + 16B].
2016 We obtain object's BINFO from the context of the virtual table.
2017 This one contains pointer to virtual table represented via
2018 POINTER_PLUS_EXPR. Verify that this pointer match to what
2019 we propagated through.
2021 In the case of virtual inheritance, the virtual tables may
2022 be nested, i.e. the offset may be different from 16 and we may
2023 need to dive into the type representation. */
2024 if (TREE_CODE (t) == ADDR_EXPR
2025 && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
2026 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
2027 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
2028 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
2029 == VAR_DECL)
2030 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2031 (TREE_OPERAND (t, 0), 0), 0)))
2033 *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
2034 *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
2035 return true;
2038 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2039 We need to handle it when T comes from static variable initializer or
2040 BINFO. */
2041 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
2043 *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
2044 t = TREE_OPERAND (t, 0);
2046 else
2047 *offset = 0;
2049 if (TREE_CODE (t) != ADDR_EXPR)
2050 return false;
2051 *v = TREE_OPERAND (t, 0);
2052 return true;
2055 /* T is known constant value of virtual table pointer. Return BINFO of the
2056 instance type. */
2058 tree
2059 vtable_pointer_value_to_binfo (const_tree t)
2061 tree vtable;
2062 unsigned HOST_WIDE_INT offset;
2064 if (!vtable_pointer_value_to_vtable (t, &vtable, &offset))
2065 return NULL_TREE;
2067 /* FIXME: for stores of construction vtables we return NULL,
2068 because we do not have BINFO for those. Eventually we should fix
2069 our representation to allow this case to be handled, too.
2070 In the case we see store of BINFO we however may assume
2071 that standard folding will be ale to cope with it. */
2072 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
2073 offset, vtable);
2076 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2077 Lookup their respecitve virtual methods for OTR_TOKEN and OTR_TYPE
2078 and insert them to NODES.
2080 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2082 static void
2083 record_targets_from_bases (tree otr_type,
2084 HOST_WIDE_INT otr_token,
2085 tree outer_type,
2086 HOST_WIDE_INT offset,
2087 vec <cgraph_node *> &nodes,
2088 hash_set<tree> *inserted,
2089 hash_set<tree> *matched_vtables,
2090 bool *completep)
2092 while (true)
2094 HOST_WIDE_INT pos, size;
2095 tree base_binfo;
2096 tree fld;
2098 if (types_same_for_odr (outer_type, otr_type))
2099 return;
2101 for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
2103 if (TREE_CODE (fld) != FIELD_DECL)
2104 continue;
2106 pos = int_bit_position (fld);
2107 size = tree_to_shwi (DECL_SIZE (fld));
2108 if (pos <= offset && (pos + size) > offset
2109 /* Do not get confused by zero sized bases. */
2110 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
2111 break;
2113 /* Within a class type we should always find correcponding fields. */
2114 gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
2116 /* Nonbasetypes should have been stripped by outer_class_type. */
2117 gcc_assert (DECL_ARTIFICIAL (fld));
2119 outer_type = TREE_TYPE (fld);
2120 offset -= pos;
2122 base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
2123 offset, otr_type);
2124 if (!base_binfo)
2126 gcc_assert (odr_violation_reported);
2127 return;
2129 gcc_assert (base_binfo);
2130 if (!matched_vtables->add (BINFO_VTABLE (base_binfo)))
2132 bool can_refer;
2133 tree target = gimple_get_virt_method_for_binfo (otr_token,
2134 base_binfo,
2135 &can_refer);
2136 if (!target || ! DECL_CXX_DESTRUCTOR_P (target))
2137 maybe_record_node (nodes, target, inserted, can_refer, completep);
2138 matched_vtables->add (BINFO_VTABLE (base_binfo));
2143 /* When virtual table is removed, we may need to flush the cache. */
2145 static void
2146 devirt_variable_node_removal_hook (varpool_node *n,
2147 void *d ATTRIBUTE_UNUSED)
2149 if (cached_polymorphic_call_targets
2150 && DECL_VIRTUAL_P (n->decl)
2151 && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
2152 free_polymorphic_call_targets_hash ();
2155 /* Record about how many calls would benefit from given type to be final. */
2157 struct odr_type_warn_count
2159 tree type;
2160 int count;
2161 gcov_type dyn_count;
2164 /* Record about how many calls would benefit from given method to be final. */
2166 struct decl_warn_count
2168 tree decl;
2169 int count;
2170 gcov_type dyn_count;
2173 /* Information about type and decl warnings. */
2175 struct final_warning_record
2177 gcov_type dyn_count;
2178 vec<odr_type_warn_count> type_warnings;
2179 hash_map<tree, decl_warn_count> decl_warnings;
2181 struct final_warning_record *final_warning_records;
2183 /* Return vector containing possible targets of polymorphic call of type
2184 OTR_TYPE caling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
2185 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containig
2186 OTR_TYPE and include their virtual method. This is useful for types
2187 possibly in construction or destruction where the virtual table may
2188 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
2189 us to walk the inheritance graph for all derivations.
2191 If COMPLETEP is non-NULL, store true if the list is complete.
2192 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
2193 in the target cache. If user needs to visit every target list
2194 just once, it can memoize them.
2196 If SPECULATIVE is set, the list will not contain targets that
2197 are not speculatively taken.
2199 Returned vector is placed into cache. It is NOT caller's responsibility
2200 to free it. The vector can be freed on cgraph_remove_node call if
2201 the particular node is a virtual function present in the cache. */
2203 vec <cgraph_node *>
2204 possible_polymorphic_call_targets (tree otr_type,
2205 HOST_WIDE_INT otr_token,
2206 ipa_polymorphic_call_context context,
2207 bool *completep,
2208 void **cache_token,
2209 bool speculative)
2211 static struct cgraph_node_hook_list *node_removal_hook_holder;
2212 vec <cgraph_node *> nodes = vNULL;
2213 vec <tree> bases_to_consider = vNULL;
2214 odr_type type, outer_type;
2215 polymorphic_call_target_d key;
2216 polymorphic_call_target_d **slot;
2217 unsigned int i;
2218 tree binfo, target;
2219 bool complete;
2220 bool can_refer = false;
2221 bool skipped = false;
2223 otr_type = TYPE_MAIN_VARIANT (otr_type);
2225 /* If ODR is not initialized or the constext is invalid, return empty
2226 incomplete list. */
2227 if (!odr_hash || context.invalid)
2229 if (completep)
2230 *completep = context.invalid;
2231 if (cache_token)
2232 *cache_token = NULL;
2233 return nodes;
2236 /* Do not bother to compute speculative info when user do not asks for it. */
2237 if (!speculative || !context.speculative_outer_type)
2238 context.clear_speculation ();
2240 type = get_odr_type (otr_type, true);
2242 /* Recording type variants would wast results cache. */
2243 gcc_assert (!context.outer_type
2244 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
2246 /* Lookup the outer class type we want to walk.
2247 If we fail to do so, the context is invalid. */
2248 if ((context.outer_type || context.speculative_outer_type)
2249 && !context.restrict_to_inner_class (otr_type))
2251 if (completep)
2252 *completep = true;
2253 if (cache_token)
2254 *cache_token = NULL;
2255 return nodes;
2257 gcc_assert (!context.invalid);
2259 /* Check that restrict_to_inner_class kept the main variant. */
2260 gcc_assert (!context.outer_type
2261 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
2263 /* We canonicalize our query, so we do not need extra hashtable entries. */
2265 /* Without outer type, we have no use for offset. Just do the
2266 basic search from innter type */
2267 if (!context.outer_type)
2269 context.outer_type = otr_type;
2270 context.offset = 0;
2272 /* We need to update our hiearchy if the type does not exist. */
2273 outer_type = get_odr_type (context.outer_type, true);
2274 /* If the type is complete, there are no derivations. */
2275 if (TYPE_FINAL_P (outer_type->type))
2276 context.maybe_derived_type = false;
2278 /* Initialize query cache. */
2279 if (!cached_polymorphic_call_targets)
2281 cached_polymorphic_call_targets = new hash_set<cgraph_node *>;
2282 polymorphic_call_target_hash
2283 = new polymorphic_call_target_hash_type (23);
2284 if (!node_removal_hook_holder)
2286 node_removal_hook_holder =
2287 symtab->add_cgraph_removal_hook (&devirt_node_removal_hook, NULL);
2288 symtab->add_varpool_removal_hook (&devirt_variable_node_removal_hook,
2289 NULL);
2293 if (in_lto_p)
2295 if (context.outer_type != otr_type)
2296 context.outer_type
2297 = get_odr_type (context.outer_type, true)->type;
2298 if (context.speculative_outer_type)
2299 context.speculative_outer_type
2300 = get_odr_type (context.speculative_outer_type, true)->type;
2303 /* Lookup cached answer. */
2304 key.type = type;
2305 key.otr_token = otr_token;
2306 key.speculative = speculative;
2307 key.context = context;
2308 slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
2309 if (cache_token)
2310 *cache_token = (void *)*slot;
2311 if (*slot)
2313 if (completep)
2314 *completep = (*slot)->complete;
2315 if ((*slot)->type_warning && final_warning_records)
2317 final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
2318 final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
2319 += final_warning_records->dyn_count;
2321 if (!speculative && (*slot)->decl_warning && final_warning_records)
2323 struct decl_warn_count *c =
2324 final_warning_records->decl_warnings.get ((*slot)->decl_warning);
2325 c->count++;
2326 c->dyn_count += final_warning_records->dyn_count;
2328 return (*slot)->targets;
2331 complete = true;
2333 /* Do actual search. */
2334 timevar_push (TV_IPA_VIRTUAL_CALL);
2335 *slot = XCNEW (polymorphic_call_target_d);
2336 if (cache_token)
2337 *cache_token = (void *)*slot;
2338 (*slot)->type = type;
2339 (*slot)->otr_token = otr_token;
2340 (*slot)->context = context;
2341 (*slot)->speculative = speculative;
2343 hash_set<tree> inserted;
2344 hash_set<tree> matched_vtables;
2346 /* First insert targets we speculatively identified as likely. */
2347 if (context.speculative_outer_type)
2349 odr_type speculative_outer_type;
2350 bool speculation_complete = true;
2352 /* First insert target from type itself and check if it may have derived types. */
2353 speculative_outer_type = get_odr_type (context.speculative_outer_type, true);
2354 if (TYPE_FINAL_P (speculative_outer_type->type))
2355 context.speculative_maybe_derived_type = false;
2356 binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
2357 context.speculative_offset, otr_type);
2358 if (binfo)
2359 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
2360 &can_refer);
2361 else
2362 target = NULL;
2364 /* In the case we get complete method, we don't need
2365 to walk derivations. */
2366 if (target && DECL_FINAL_P (target))
2367 context.speculative_maybe_derived_type = false;
2368 if (type_possibly_instantiated_p (speculative_outer_type->type))
2369 maybe_record_node (nodes, target, &inserted, can_refer, &speculation_complete);
2370 if (binfo)
2371 matched_vtables.add (BINFO_VTABLE (binfo));
2374 /* Next walk recursively all derived types. */
2375 if (context.speculative_maybe_derived_type)
2376 for (i = 0; i < speculative_outer_type->derived_types.length(); i++)
2377 possible_polymorphic_call_targets_1 (nodes, &inserted,
2378 &matched_vtables,
2379 otr_type,
2380 speculative_outer_type->derived_types[i],
2381 otr_token, speculative_outer_type->type,
2382 context.speculative_offset,
2383 &speculation_complete,
2384 bases_to_consider,
2385 false);
2388 if (!speculative || !nodes.length ())
2390 /* First see virtual method of type itself. */
2391 binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
2392 context.offset, otr_type);
2393 if (binfo)
2394 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
2395 &can_refer);
2396 else
2398 gcc_assert (odr_violation_reported);
2399 target = NULL;
2402 /* Destructors are never called through construction virtual tables,
2403 because the type is always known. */
2404 if (target && DECL_CXX_DESTRUCTOR_P (target))
2405 context.maybe_in_construction = false;
2407 if (target)
2409 /* In the case we get complete method, we don't need
2410 to walk derivations. */
2411 if (DECL_FINAL_P (target))
2412 context.maybe_derived_type = false;
2415 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
2416 if (type_possibly_instantiated_p (outer_type->type))
2417 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
2418 else
2419 skipped = true;
2421 if (binfo)
2422 matched_vtables.add (BINFO_VTABLE (binfo));
2424 /* Next walk recursively all derived types. */
2425 if (context.maybe_derived_type)
2427 for (i = 0; i < outer_type->derived_types.length(); i++)
2428 possible_polymorphic_call_targets_1 (nodes, &inserted,
2429 &matched_vtables,
2430 otr_type,
2431 outer_type->derived_types[i],
2432 otr_token, outer_type->type,
2433 context.offset, &complete,
2434 bases_to_consider,
2435 context.maybe_in_construction);
2437 if (!outer_type->all_derivations_known)
2439 if (!speculative && final_warning_records)
2441 if (complete
2442 && nodes.length () == 1
2443 && warn_suggest_final_types
2444 && !outer_type->derived_types.length ())
2446 if (outer_type->id >= (int)final_warning_records->type_warnings.length ())
2447 final_warning_records->type_warnings.safe_grow_cleared
2448 (odr_types.length ());
2449 final_warning_records->type_warnings[outer_type->id].count++;
2450 final_warning_records->type_warnings[outer_type->id].dyn_count
2451 += final_warning_records->dyn_count;
2452 final_warning_records->type_warnings[outer_type->id].type
2453 = outer_type->type;
2454 (*slot)->type_warning = outer_type->id + 1;
2456 if (complete
2457 && warn_suggest_final_methods
2458 && nodes.length () == 1
2459 && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl),
2460 outer_type->type))
2462 bool existed;
2463 struct decl_warn_count &c =
2464 final_warning_records->decl_warnings.get_or_insert
2465 (nodes[0]->decl, &existed);
2467 if (existed)
2469 c.count++;
2470 c.dyn_count += final_warning_records->dyn_count;
2472 else
2474 c.count = 1;
2475 c.dyn_count = final_warning_records->dyn_count;
2476 c.decl = nodes[0]->decl;
2478 (*slot)->decl_warning = nodes[0]->decl;
2481 complete = false;
2485 if (!speculative)
2487 /* Destructors are never called through construction virtual tables,
2488 because the type is always known. One of entries may be cxa_pure_virtual
2489 so look to at least two of them. */
2490 if (context.maybe_in_construction)
2491 for (i =0 ; i < MIN (nodes.length (), 2); i++)
2492 if (DECL_CXX_DESTRUCTOR_P (nodes[i]->decl))
2493 context.maybe_in_construction = false;
2494 if (context.maybe_in_construction)
2496 if (type != outer_type
2497 && (!skipped
2498 || (context.maybe_derived_type
2499 && !type_all_derivations_known_p (outer_type->type))))
2500 record_targets_from_bases (otr_type, otr_token, outer_type->type,
2501 context.offset, nodes, &inserted,
2502 &matched_vtables, &complete);
2503 if (skipped)
2504 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
2505 for (i = 0; i < bases_to_consider.length(); i++)
2506 maybe_record_node (nodes, bases_to_consider[i], &inserted, can_refer, &complete);
2511 bases_to_consider.release();
2512 (*slot)->targets = nodes;
2513 (*slot)->complete = complete;
2514 if (completep)
2515 *completep = complete;
2517 timevar_pop (TV_IPA_VIRTUAL_CALL);
2518 return nodes;
2521 bool
2522 add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value,
2523 vec<const decl_warn_count*> *vec)
2525 vec->safe_push (&value);
2526 return true;
2529 /* Dump target list TARGETS into FILE. */
2531 static void
2532 dump_targets (FILE *f, vec <cgraph_node *> targets)
2534 unsigned int i;
2536 for (i = 0; i < targets.length (); i++)
2538 char *name = NULL;
2539 if (in_lto_p)
2540 name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
2541 fprintf (f, " %s/%i", name ? name : targets[i]->name (), targets[i]->order);
2542 if (in_lto_p)
2543 free (name);
2544 if (!targets[i]->definition)
2545 fprintf (f, " (no definition%s)",
2546 DECL_DECLARED_INLINE_P (targets[i]->decl)
2547 ? " inline" : "");
2549 fprintf (f, "\n");
2552 /* Dump all possible targets of a polymorphic call. */
2554 void
2555 dump_possible_polymorphic_call_targets (FILE *f,
2556 tree otr_type,
2557 HOST_WIDE_INT otr_token,
2558 const ipa_polymorphic_call_context &ctx)
2560 vec <cgraph_node *> targets;
2561 bool final;
2562 odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false);
2563 unsigned int len;
2565 if (!type)
2566 return;
2567 targets = possible_polymorphic_call_targets (otr_type, otr_token,
2568 ctx,
2569 &final, NULL, false);
2570 fprintf (f, " Targets of polymorphic call of type %i:", type->id);
2571 print_generic_expr (f, type->type, TDF_SLIM);
2572 fprintf (f, " token %i\n", (int)otr_token);
2574 ctx.dump (f);
2576 fprintf (f, " %s%s%s%s\n ",
2577 final ? "This is a complete list." :
2578 "This is partial list; extra targets may be defined in other units.",
2579 ctx.maybe_in_construction ? " (base types included)" : "",
2580 ctx.maybe_derived_type ? " (derived types included)" : "",
2581 ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
2582 len = targets.length ();
2583 dump_targets (f, targets);
2585 targets = possible_polymorphic_call_targets (otr_type, otr_token,
2586 ctx,
2587 &final, NULL, true);
2588 gcc_assert (targets.length () <= len);
2589 if (targets.length () != len)
2591 fprintf (f, " Speculative targets:");
2592 dump_targets (f, targets);
2594 fprintf (f, "\n");
2598 /* Return true if N can be possibly target of a polymorphic call of
2599 OTR_TYPE/OTR_TOKEN. */
2601 bool
2602 possible_polymorphic_call_target_p (tree otr_type,
2603 HOST_WIDE_INT otr_token,
2604 const ipa_polymorphic_call_context &ctx,
2605 struct cgraph_node *n)
2607 vec <cgraph_node *> targets;
2608 unsigned int i;
2609 enum built_in_function fcode;
2610 bool final;
2612 if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
2613 && ((fcode = DECL_FUNCTION_CODE (n->decl))
2614 == BUILT_IN_UNREACHABLE
2615 || fcode == BUILT_IN_TRAP))
2616 return true;
2618 if (!odr_hash)
2619 return true;
2620 targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
2621 for (i = 0; i < targets.length (); i++)
2622 if (n->semantically_equivalent_p (targets[i]))
2623 return true;
2625 /* At a moment we allow middle end to dig out new external declarations
2626 as a targets of polymorphic calls. */
2627 if (!final && !n->definition)
2628 return true;
2629 return false;
2634 /* Return true if N can be possibly target of a polymorphic call of
2635 OBJ_TYPE_REF expression REF in STMT. */
2637 bool
2638 possible_polymorphic_call_target_p (tree ref,
2639 gimple stmt,
2640 struct cgraph_node *n)
2642 ipa_polymorphic_call_context context (current_function_decl, ref, stmt);
2643 tree call_fn = gimple_call_fn (stmt);
2645 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn),
2646 tree_to_uhwi
2647 (OBJ_TYPE_REF_TOKEN (call_fn)),
2648 context,
2653 /* After callgraph construction new external nodes may appear.
2654 Add them into the graph. */
2656 void
2657 update_type_inheritance_graph (void)
2659 struct cgraph_node *n;
2661 if (!odr_hash)
2662 return;
2663 free_polymorphic_call_targets_hash ();
2664 timevar_push (TV_IPA_INHERITANCE);
2665 /* We reconstruct the graph starting from types of all methods seen in the
2666 the unit. */
2667 FOR_EACH_FUNCTION (n)
2668 if (DECL_VIRTUAL_P (n->decl)
2669 && !n->definition
2670 && n->real_symbol_p ())
2671 get_odr_type (method_class_type (TYPE_MAIN_VARIANT (TREE_TYPE (n->decl))),
2672 true);
2673 timevar_pop (TV_IPA_INHERITANCE);
2677 /* Return true if N looks like likely target of a polymorphic call.
2678 Rule out cxa_pure_virtual, noreturns, function declared cold and
2679 other obvious cases. */
2681 bool
2682 likely_target_p (struct cgraph_node *n)
2684 int flags;
2685 /* cxa_pure_virtual and similar things are not likely. */
2686 if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
2687 return false;
2688 flags = flags_from_decl_or_type (n->decl);
2689 if (flags & ECF_NORETURN)
2690 return false;
2691 if (lookup_attribute ("cold",
2692 DECL_ATTRIBUTES (n->decl)))
2693 return false;
2694 if (n->frequency < NODE_FREQUENCY_NORMAL)
2695 return false;
2696 /* If there are no virtual tables refering the target alive,
2697 the only way the target can be called is an instance comming from other
2698 compilation unit; speculative devirtualization is build around an
2699 assumption that won't happen. */
2700 if (!referenced_from_vtable_p (n))
2701 return false;
2702 return true;
2705 /* Compare type warning records P1 and P2 and chose one with larger count;
2706 helper for qsort. */
2709 type_warning_cmp (const void *p1, const void *p2)
2711 const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
2712 const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
2714 if (t1->dyn_count < t2->dyn_count)
2715 return 1;
2716 if (t1->dyn_count > t2->dyn_count)
2717 return -1;
2718 return t2->count - t1->count;
2721 /* Compare decl warning records P1 and P2 and chose one with larger count;
2722 helper for qsort. */
2725 decl_warning_cmp (const void *p1, const void *p2)
2727 const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
2728 const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
2730 if (t1->dyn_count < t2->dyn_count)
2731 return 1;
2732 if (t1->dyn_count > t2->dyn_count)
2733 return -1;
2734 return t2->count - t1->count;
2737 /* The ipa-devirt pass.
2738 When polymorphic call has only one likely target in the unit,
2739 turn it into speculative call. */
2741 static unsigned int
2742 ipa_devirt (void)
2744 struct cgraph_node *n;
2745 hash_set<void *> bad_call_targets;
2746 struct cgraph_edge *e;
2748 int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
2749 int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
2750 int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
2752 if (!odr_types_ptr)
2753 return 0;
2755 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
2756 This is implemented by setting up final_warning_records that are updated
2757 by get_polymorphic_call_targets.
2758 We need to clear cache in this case to trigger recomputation of all
2759 entries. */
2760 if (warn_suggest_final_methods || warn_suggest_final_types)
2762 final_warning_records = new (final_warning_record);
2763 final_warning_records->type_warnings = vNULL;
2764 final_warning_records->type_warnings.safe_grow_cleared (odr_types.length ());
2765 free_polymorphic_call_targets_hash ();
2768 FOR_EACH_DEFINED_FUNCTION (n)
2770 bool update = false;
2771 if (dump_file && n->indirect_calls)
2772 fprintf (dump_file, "\n\nProcesing function %s/%i\n",
2773 n->name (), n->order);
2774 for (e = n->indirect_calls; e; e = e->next_callee)
2775 if (e->indirect_info->polymorphic)
2777 struct cgraph_node *likely_target = NULL;
2778 void *cache_token;
2779 bool final;
2781 if (final_warning_records)
2782 final_warning_records->dyn_count = e->count;
2784 vec <cgraph_node *>targets
2785 = possible_polymorphic_call_targets
2786 (e, &final, &cache_token, true);
2787 unsigned int i;
2789 /* Trigger warnings by calculating non-speculative targets. */
2790 if (warn_suggest_final_methods || warn_suggest_final_types)
2791 possible_polymorphic_call_targets (e);
2793 if (dump_file)
2794 dump_possible_polymorphic_call_targets
2795 (dump_file, e);
2797 npolymorphic++;
2799 if (!flag_devirtualize_speculatively)
2800 continue;
2802 if (!e->maybe_hot_p ())
2804 if (dump_file)
2805 fprintf (dump_file, "Call is cold\n\n");
2806 ncold++;
2807 continue;
2809 if (e->speculative)
2811 if (dump_file)
2812 fprintf (dump_file, "Call is aready speculated\n\n");
2813 nspeculated++;
2815 /* When dumping see if we agree with speculation. */
2816 if (!dump_file)
2817 continue;
2819 if (bad_call_targets.contains (cache_token))
2821 if (dump_file)
2822 fprintf (dump_file, "Target list is known to be useless\n\n");
2823 nmultiple++;
2824 continue;
2826 for (i = 0; i < targets.length (); i++)
2827 if (likely_target_p (targets[i]))
2829 if (likely_target)
2831 likely_target = NULL;
2832 if (dump_file)
2833 fprintf (dump_file, "More than one likely target\n\n");
2834 nmultiple++;
2835 break;
2837 likely_target = targets[i];
2839 if (!likely_target)
2841 bad_call_targets.add (cache_token);
2842 continue;
2844 /* This is reached only when dumping; check if we agree or disagree
2845 with the speculation. */
2846 if (e->speculative)
2848 struct cgraph_edge *e2;
2849 struct ipa_ref *ref;
2850 e->speculative_call_info (e2, e, ref);
2851 if (e2->callee->ultimate_alias_target ()
2852 == likely_target->ultimate_alias_target ())
2854 fprintf (dump_file, "We agree with speculation\n\n");
2855 nok++;
2857 else
2859 fprintf (dump_file, "We disagree with speculation\n\n");
2860 nwrong++;
2862 continue;
2864 if (!likely_target->definition)
2866 if (dump_file)
2867 fprintf (dump_file, "Target is not an definition\n\n");
2868 nnotdefined++;
2869 continue;
2871 /* Do not introduce new references to external symbols. While we
2872 can handle these just well, it is common for programs to
2873 incorrectly with headers defining methods they are linked
2874 with. */
2875 if (DECL_EXTERNAL (likely_target->decl))
2877 if (dump_file)
2878 fprintf (dump_file, "Target is external\n\n");
2879 nexternal++;
2880 continue;
2882 /* Don't use an implicitly-declared destructor (c++/58678). */
2883 struct cgraph_node *non_thunk_target
2884 = likely_target->function_symbol ();
2885 if (DECL_ARTIFICIAL (non_thunk_target->decl))
2887 if (dump_file)
2888 fprintf (dump_file, "Target is artificial\n\n");
2889 nartificial++;
2890 continue;
2892 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
2893 && likely_target->can_be_discarded_p ())
2895 if (dump_file)
2896 fprintf (dump_file, "Target is overwritable\n\n");
2897 noverwritable++;
2898 continue;
2900 else if (dbg_cnt (devirt))
2902 if (dump_enabled_p ())
2904 location_t locus = gimple_location_safe (e->call_stmt);
2905 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
2906 "speculatively devirtualizing call in %s/%i to %s/%i\n",
2907 n->name (), n->order,
2908 likely_target->name (),
2909 likely_target->order);
2911 if (!likely_target->can_be_discarded_p ())
2913 cgraph_node *alias;
2914 alias = dyn_cast<cgraph_node *> (likely_target->noninterposable_alias ());
2915 if (alias)
2916 likely_target = alias;
2918 nconverted++;
2919 update = true;
2920 e->make_speculative
2921 (likely_target, e->count * 8 / 10, e->frequency * 8 / 10);
2924 if (update)
2925 inline_update_overall_summary (n);
2927 if (warn_suggest_final_methods || warn_suggest_final_types)
2929 if (warn_suggest_final_types)
2931 final_warning_records->type_warnings.qsort (type_warning_cmp);
2932 for (unsigned int i = 0;
2933 i < final_warning_records->type_warnings.length (); i++)
2934 if (final_warning_records->type_warnings[i].count)
2936 tree type = final_warning_records->type_warnings[i].type;
2937 int count = final_warning_records->type_warnings[i].count;
2938 long long dyn_count
2939 = final_warning_records->type_warnings[i].dyn_count;
2941 if (!dyn_count)
2942 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
2943 OPT_Wsuggest_final_types, count,
2944 "Declaring type %qD final "
2945 "would enable devirtualization of %i call",
2946 "Declaring type %qD final "
2947 "would enable devirtualization of %i calls",
2948 type,
2949 count);
2950 else
2951 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
2952 OPT_Wsuggest_final_types, count,
2953 "Declaring type %qD final "
2954 "would enable devirtualization of %i call "
2955 "executed %lli times",
2956 "Declaring type %qD final "
2957 "would enable devirtualization of %i calls "
2958 "executed %lli times",
2959 type,
2960 count,
2961 dyn_count);
2965 if (warn_suggest_final_methods)
2967 vec<const decl_warn_count*> decl_warnings_vec = vNULL;
2969 final_warning_records->decl_warnings.traverse
2970 <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec);
2971 decl_warnings_vec.qsort (decl_warning_cmp);
2972 for (unsigned int i = 0; i < decl_warnings_vec.length (); i++)
2974 tree decl = decl_warnings_vec[i]->decl;
2975 int count = decl_warnings_vec[i]->count;
2976 long long dyn_count = decl_warnings_vec[i]->dyn_count;
2978 if (!dyn_count)
2979 if (DECL_CXX_DESTRUCTOR_P (decl))
2980 warning_n (DECL_SOURCE_LOCATION (decl),
2981 OPT_Wsuggest_final_methods, count,
2982 "Declaring virtual destructor of %qD final "
2983 "would enable devirtualization of %i call",
2984 "Declaring virtual destructor of %qD final "
2985 "would enable devirtualization of %i calls",
2986 DECL_CONTEXT (decl), count);
2987 else
2988 warning_n (DECL_SOURCE_LOCATION (decl),
2989 OPT_Wsuggest_final_methods, count,
2990 "Declaring method %qD final "
2991 "would enable devirtualization of %i call",
2992 "Declaring method %qD final "
2993 "would enable devirtualization of %i calls",
2994 decl, count);
2995 else if (DECL_CXX_DESTRUCTOR_P (decl))
2996 warning_n (DECL_SOURCE_LOCATION (decl),
2997 OPT_Wsuggest_final_methods, count,
2998 "Declaring virtual destructor of %qD final "
2999 "would enable devirtualization of %i call "
3000 "executed %lli times",
3001 "Declaring virtual destructor of %qD final "
3002 "would enable devirtualization of %i calls "
3003 "executed %lli times",
3004 DECL_CONTEXT (decl), count, dyn_count);
3005 else
3006 warning_n (DECL_SOURCE_LOCATION (decl),
3007 OPT_Wsuggest_final_methods, count,
3008 "Declaring method %qD final "
3009 "would enable devirtualization of %i call "
3010 "executed %lli times",
3011 "Declaring method %qD final "
3012 "would enable devirtualization of %i calls "
3013 "executed %lli times",
3014 decl, count, dyn_count);
3018 delete (final_warning_records);
3019 final_warning_records = 0;
3022 if (dump_file)
3023 fprintf (dump_file,
3024 "%i polymorphic calls, %i devirtualized,"
3025 " %i speculatively devirtualized, %i cold\n"
3026 "%i have multiple targets, %i overwritable,"
3027 " %i already speculated (%i agree, %i disagree),"
3028 " %i external, %i not defined, %i artificial\n",
3029 npolymorphic, ndevirtualized, nconverted, ncold,
3030 nmultiple, noverwritable, nspeculated, nok, nwrong,
3031 nexternal, nnotdefined, nartificial);
3032 return ndevirtualized ? TODO_remove_functions : 0;
3035 namespace {
3037 const pass_data pass_data_ipa_devirt =
3039 IPA_PASS, /* type */
3040 "devirt", /* name */
3041 OPTGROUP_NONE, /* optinfo_flags */
3042 TV_IPA_DEVIRT, /* tv_id */
3043 0, /* properties_required */
3044 0, /* properties_provided */
3045 0, /* properties_destroyed */
3046 0, /* todo_flags_start */
3047 ( TODO_dump_symtab ), /* todo_flags_finish */
3050 class pass_ipa_devirt : public ipa_opt_pass_d
3052 public:
3053 pass_ipa_devirt (gcc::context *ctxt)
3054 : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
3055 NULL, /* generate_summary */
3056 NULL, /* write_summary */
3057 NULL, /* read_summary */
3058 NULL, /* write_optimization_summary */
3059 NULL, /* read_optimization_summary */
3060 NULL, /* stmt_fixup */
3061 0, /* function_transform_todo_flags_start */
3062 NULL, /* function_transform */
3063 NULL) /* variable_transform */
3066 /* opt_pass methods: */
3067 virtual bool gate (function *)
3069 return (flag_devirtualize
3070 && (flag_devirtualize_speculatively
3071 || (warn_suggest_final_methods
3072 || warn_suggest_final_types))
3073 && optimize);
3076 virtual unsigned int execute (function *) { return ipa_devirt (); }
3078 }; // class pass_ipa_devirt
3080 } // anon namespace
3082 ipa_opt_pass_d *
3083 make_pass_ipa_devirt (gcc::context *ctxt)
3085 return new pass_ipa_devirt (ctxt);
3088 #include "gt-ipa-devirt.h"