gcc/
[official-gcc.git] / gcc / ipa-devirt.c
blobf98a18e7b8f14161a91407bf1ea2278639e36223
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 static bool odr_types_equivalent_p (tree, tree, bool, bool *,
140 hash_set<tree> *);
142 static bool odr_violation_reported = false;
145 /* Pointer set of all call targets appearing in the cache. */
146 static hash_set<cgraph_node *> *cached_polymorphic_call_targets;
148 /* The node of type inheritance graph. For each type unique in
149 One Defintion Rule (ODR) sense, we produce one node linking all
150 main variants of types equivalent to it, bases and derived types. */
152 struct GTY(()) odr_type_d
154 /* leader type. */
155 tree type;
156 /* All bases; built only for main variants of types */
157 vec<odr_type> GTY((skip)) bases;
158 /* All derrived types with virtual methods seen in unit;
159 built only for main variants oftypes */
160 vec<odr_type> GTY((skip)) derived_types;
162 /* All equivalent types, if more than one. */
163 vec<tree, va_gc> *types;
164 /* Set of all equivalent types, if NON-NULL. */
165 hash_set<tree> * GTY((skip)) types_set;
167 /* Unique ID indexing the type in odr_types array. */
168 int id;
169 /* Is it in anonymous namespace? */
170 bool anonymous_namespace;
171 /* Do we know about all derivations of given type? */
172 bool all_derivations_known;
173 /* Did we report ODR violation here? */
174 bool odr_violated;
177 static bool contains_type_p (tree, HOST_WIDE_INT, tree);
180 /* Return true if BINFO corresponds to a type with virtual methods.
182 Every type has several BINFOs. One is the BINFO associated by the type
183 while other represents bases of derived types. The BINFOs representing
184 bases do not have BINFO_VTABLE pointer set when this is the single
185 inheritance (because vtables are shared). Look up the BINFO of type
186 and check presence of its vtable. */
188 static inline bool
189 polymorphic_type_binfo_p (tree binfo)
191 /* See if BINFO's type has an virtual table associtated with it. */
192 return BINFO_VTABLE (TYPE_BINFO (BINFO_TYPE (binfo)));
195 /* Return TRUE if all derived types of T are known and thus
196 we may consider the walk of derived type complete.
198 This is typically true only for final anonymous namespace types and types
199 defined within functions (that may be COMDAT and thus shared across units,
200 but with the same set of derived types). */
202 static bool
203 type_all_derivations_known_p (tree t)
205 if (TYPE_FINAL_P (t))
206 return true;
207 if (flag_ltrans)
208 return false;
209 if (type_in_anonymous_namespace_p (t))
210 return true;
211 return (decl_function_context (TYPE_NAME (t)) != NULL);
214 /* Return TURE if type's constructors are all visible. */
216 static bool
217 type_all_ctors_visible_p (tree t)
219 return !flag_ltrans
220 && symtab->state >= CONSTRUCTION
221 /* We can not always use type_all_derivations_known_p.
222 For function local types we must assume case where
223 the function is COMDAT and shared in between units.
225 TODO: These cases are quite easy to get, but we need
226 to keep track of C++ privatizing via -Wno-weak
227 as well as the IPA privatizing. */
228 && type_in_anonymous_namespace_p (t);
231 /* Return TRUE if type may have instance. */
233 static bool
234 type_possibly_instantiated_p (tree t)
236 tree vtable;
237 varpool_node *vnode;
239 /* TODO: Add abstract types here. */
240 if (!type_all_ctors_visible_p (t))
241 return true;
243 vtable = BINFO_VTABLE (TYPE_BINFO (t));
244 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
245 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
246 vnode = varpool_node::get (vtable);
247 return vnode && vnode->definition;
250 /* One Definition Rule hashtable helpers. */
252 struct odr_hasher
254 typedef odr_type_d value_type;
255 typedef union tree_node compare_type;
256 static inline hashval_t hash (const value_type *);
257 static inline bool equal (const value_type *, const compare_type *);
258 static inline void remove (value_type *);
261 /* Return type that was declared with T's name so that T is an
262 qualified variant of it. */
264 static inline tree
265 main_odr_variant (const_tree t)
267 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
268 return TREE_TYPE (TYPE_NAME (t));
269 /* Unnamed types and non-C++ produced types can be compared by variants. */
270 else
271 return TYPE_MAIN_VARIANT (t);
274 /* Produce hash based on type name. */
276 static hashval_t
277 hash_type_name (tree t)
279 gcc_checking_assert (main_odr_variant (t) == t);
281 /* If not in LTO, all main variants are unique, so we can do
282 pointer hash. */
283 if (!in_lto_p)
284 return htab_hash_pointer (t);
286 /* Anonymous types are unique. */
287 if (type_in_anonymous_namespace_p (t))
288 return htab_hash_pointer (t);
290 /* For polymorphic types, we can simply hash the virtual table. */
291 if (TREE_CODE (t) == RECORD_TYPE
292 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)))
294 tree v = BINFO_VTABLE (TYPE_BINFO (t));
295 hashval_t hash = 0;
297 if (TREE_CODE (v) == POINTER_PLUS_EXPR)
299 hash = TREE_INT_CST_LOW (TREE_OPERAND (v, 1));
300 v = TREE_OPERAND (TREE_OPERAND (v, 0), 0);
303 v = DECL_ASSEMBLER_NAME (v);
304 hash = iterative_hash_hashval_t (hash, htab_hash_pointer (v));
305 return hash;
308 /* Rest is not implemented yet. */
309 gcc_unreachable ();
312 /* Return the computed hashcode for ODR_TYPE. */
314 inline hashval_t
315 odr_hasher::hash (const value_type *odr_type)
317 return hash_type_name (odr_type->type);
320 /* For languages with One Definition Rule, work out if
321 types are the same based on their name.
323 This is non-trivial for LTO where minnor differences in
324 the type representation may have prevented type merging
325 to merge two copies of otherwise equivalent type.
327 Until we start streaming mangled type names, this function works
328 only for polymorphic types. */
330 bool
331 types_same_for_odr (const_tree type1, const_tree type2)
333 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
335 type1 = main_odr_variant (type1);
336 type2 = main_odr_variant (type2);
338 if (type1 == type2)
339 return true;
341 if (!in_lto_p)
342 return false;
344 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
345 on the corresponding TYPE_STUB_DECL. */
346 if (type_in_anonymous_namespace_p (type1)
347 || type_in_anonymous_namespace_p (type2))
348 return false;
350 /* See if types are obvoiusly different (i.e. different codes
351 or polymorphis wrt non-polymorphic). This is not strictly correct
352 for ODR violating programs, but we can't do better without streaming
353 ODR names. */
354 if (TREE_CODE (type1) != TREE_CODE (type2))
355 return false;
356 if (TREE_CODE (type1) == RECORD_TYPE
357 && (TYPE_BINFO (type1) == NULL_TREE) != (TYPE_BINFO (type1) == NULL_TREE))
358 return false;
359 if (TREE_CODE (type1) == RECORD_TYPE && TYPE_BINFO (type1)
360 && (BINFO_VTABLE (TYPE_BINFO (type1)) == NULL_TREE)
361 != (BINFO_VTABLE (TYPE_BINFO (type2)) == NULL_TREE))
362 return false;
364 /* At the moment we have no way to establish ODR equivlaence at LTO
365 other than comparing virtual table pointrs of polymorphic types.
366 Eventually we should start saving mangled names in TYPE_NAME.
367 Then this condition will become non-trivial. */
369 if (TREE_CODE (type1) == RECORD_TYPE
370 && TYPE_BINFO (type1) && TYPE_BINFO (type2)
371 && BINFO_VTABLE (TYPE_BINFO (type1))
372 && BINFO_VTABLE (TYPE_BINFO (type2)))
374 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
375 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
376 gcc_assert (TREE_CODE (v1) == POINTER_PLUS_EXPR
377 && TREE_CODE (v2) == POINTER_PLUS_EXPR);
378 return (operand_equal_p (TREE_OPERAND (v1, 1),
379 TREE_OPERAND (v2, 1), 0)
380 && DECL_ASSEMBLER_NAME
381 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
382 == DECL_ASSEMBLER_NAME
383 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
385 gcc_unreachable ();
389 /* Compare types T1 and T2 and return true if they are
390 equivalent. */
392 inline bool
393 odr_hasher::equal (const value_type *t1, const compare_type *ct2)
395 tree t2 = const_cast <tree> (ct2);
397 gcc_checking_assert (main_odr_variant (t2) == t2);
398 if (t1->type == t2)
399 return true;
400 if (!in_lto_p)
401 return false;
402 return types_same_for_odr (t1->type, t2);
405 /* Free ODR type V. */
407 inline void
408 odr_hasher::remove (value_type *v)
410 v->bases.release ();
411 v->derived_types.release ();
412 if (v->types_set)
413 delete v->types_set;
414 ggc_free (v);
417 /* ODR type hash used to lookup ODR type based on tree type node. */
419 typedef hash_table<odr_hasher> odr_hash_type;
420 static odr_hash_type *odr_hash;
422 /* ODR types are also stored into ODR_TYPE vector to allow consistent
423 walking. Bases appear before derived types. Vector is garbage collected
424 so we won't end up visiting empty types. */
426 static GTY(()) vec <odr_type, va_gc> *odr_types_ptr;
427 #define odr_types (*odr_types_ptr)
429 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
430 void
431 set_type_binfo (tree type, tree binfo)
433 for (; type; type = TYPE_NEXT_VARIANT (type))
434 if (COMPLETE_TYPE_P (type))
435 TYPE_BINFO (type) = binfo;
436 else
437 gcc_assert (!TYPE_BINFO (type));
440 /* Compare T2 and T2 based on name or structure. */
442 static bool
443 odr_subtypes_equivalent_p (tree t1, tree t2, hash_set<tree> *visited)
445 bool an1, an2;
447 /* This can happen in incomplete types that should be handled earlier. */
448 gcc_assert (t1 && t2);
450 t1 = main_odr_variant (t1);
451 t2 = main_odr_variant (t2);
452 if (t1 == t2)
453 return true;
454 if (TREE_CODE (t1) != TREE_CODE (t2))
455 return false;
456 if ((TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
457 return false;
458 if (TYPE_NAME (t1) && DECL_NAME (TYPE_NAME (t1)) != DECL_NAME (TYPE_NAME (t2)))
459 return false;
461 /* Anonymous namespace types must match exactly. */
462 an1 = type_in_anonymous_namespace_p (t1);
463 an2 = type_in_anonymous_namespace_p (t2);
464 if (an1 != an2 || an1)
465 return false;
467 /* For types where we can not establish ODR equivalency, recurse and deeply
468 compare. */
469 if (TREE_CODE (t1) != RECORD_TYPE
470 || !TYPE_BINFO (t1) || !TYPE_BINFO (t2)
471 || !polymorphic_type_binfo_p (TYPE_BINFO (t1))
472 || !polymorphic_type_binfo_p (TYPE_BINFO (t2)))
474 /* This should really be a pair hash, but for the moment we do not need
475 100% reliability and it would be better to compare all ODR types so
476 recursion here is needed only for component types. */
477 if (visited->add (t1))
478 return true;
479 return odr_types_equivalent_p (t1, t2, false, NULL, visited);
481 return types_same_for_odr (t1, t2);
484 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
485 violation warings. */
487 void
488 compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
490 int n1, n2;
491 if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl))
493 odr_violation_reported = true;
494 if (DECL_VIRTUAL_P (prevailing->decl))
496 varpool_node *tmp = prevailing;
497 prevailing = vtable;
498 vtable = tmp;
500 if (warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
501 OPT_Wodr,
502 "virtual table of type %qD violates one definition rule",
503 DECL_CONTEXT (vtable->decl)))
504 inform (DECL_SOURCE_LOCATION (prevailing->decl),
505 "variable of same assembler name as the virtual table is "
506 "defined in another translation unit");
507 return;
509 if (!prevailing->definition || !vtable->definition)
510 return;
511 for (n1 = 0, n2 = 0; true; n1++, n2++)
513 struct ipa_ref *ref1, *ref2;
514 bool end1, end2;
515 end1 = !prevailing->iterate_reference (n1, ref1);
516 end2 = !vtable->iterate_reference (n2, ref2);
517 if (end1 && end2)
518 return;
519 if (!end1 && !end2
520 && DECL_ASSEMBLER_NAME (ref1->referred->decl)
521 != DECL_ASSEMBLER_NAME (ref2->referred->decl)
522 && !n2
523 && !DECL_VIRTUAL_P (ref2->referred->decl)
524 && DECL_VIRTUAL_P (ref1->referred->decl))
526 if (warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 0,
527 "virtual table of type %qD contains RTTI information",
528 DECL_CONTEXT (vtable->decl)))
530 inform (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
531 "but is prevailed by one without from other translation unit");
532 inform (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
533 "RTTI will not work on this type");
535 n2++;
536 end2 = !vtable->iterate_reference (n2, ref2);
538 if (!end1 && !end2
539 && DECL_ASSEMBLER_NAME (ref1->referred->decl)
540 != DECL_ASSEMBLER_NAME (ref2->referred->decl)
541 && !n1
542 && !DECL_VIRTUAL_P (ref1->referred->decl)
543 && DECL_VIRTUAL_P (ref2->referred->decl))
545 n1++;
546 end1 = !vtable->iterate_reference (n1, ref1);
548 if (end1 || end2)
550 if (end1)
552 varpool_node *tmp = prevailing;
553 prevailing = vtable;
554 vtable = tmp;
555 ref1 = ref2;
557 if (warning_at (DECL_SOURCE_LOCATION
558 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 0,
559 "virtual table of type %qD violates "
560 "one definition rule",
561 DECL_CONTEXT (vtable->decl)))
563 inform (DECL_SOURCE_LOCATION
564 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
565 "the conflicting type defined in another translation "
566 "unit");
567 inform (DECL_SOURCE_LOCATION
568 (TYPE_NAME (DECL_CONTEXT (ref1->referring->decl))),
569 "contains additional virtual method %qD",
570 ref1->referred->decl);
572 return;
574 if (DECL_ASSEMBLER_NAME (ref1->referred->decl)
575 != DECL_ASSEMBLER_NAME (ref2->referred->decl))
577 if (warning_at (DECL_SOURCE_LOCATION
578 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 0,
579 "virtual table of type %qD violates "
580 "one definition rule ",
581 DECL_CONTEXT (vtable->decl)))
583 inform (DECL_SOURCE_LOCATION
584 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
585 "the conflicting type defined in another translation "
586 "unit");
587 inform (DECL_SOURCE_LOCATION (ref1->referred->decl),
588 "virtual method %qD", ref1->referred->decl);
589 inform (DECL_SOURCE_LOCATION (ref2->referred->decl),
590 "ought to match virtual method %qD but does not",
591 ref2->referred->decl);
592 return;
598 /* Output ODR violation warning about T1 and T2 with REASON.
599 Display location of ST1 and ST2 if REASON speaks about field or
600 method of the type.
601 If WARN is false, do nothing. Set WARNED if warning was indeed
602 output. */
604 void
605 warn_odr (tree t1, tree t2, tree st1, tree st2,
606 bool warn, bool *warned, const char *reason)
608 tree decl2 = TYPE_NAME (t2);
610 if (!warn)
611 return;
612 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr,
613 "type %qT violates one definition rule",
614 t1))
615 return;
616 if (!st1)
618 else if (TREE_CODE (st1) == FIELD_DECL)
620 inform (DECL_SOURCE_LOCATION (decl2),
621 "a different type is defined in another translation unit");
622 inform (DECL_SOURCE_LOCATION (st1),
623 "the first difference of corresponding definitions is field %qD",
624 st1);
625 decl2 = st2;
627 else if (TREE_CODE (st1) == FUNCTION_DECL)
629 inform (DECL_SOURCE_LOCATION (decl2),
630 "a different type is defined in another translation unit");
631 inform (DECL_SOURCE_LOCATION (st1),
632 "the first difference of corresponding definitions is method %qD",
633 st1);
634 decl2 = st2;
636 else
637 return;
638 inform (DECL_SOURCE_LOCATION (decl2), reason);
640 if (warned)
641 *warned = true;
644 /* We already warned about ODR mismatch. T1 and T2 ought to be equivalent
645 because they are used on same place in ODR matching types.
646 They are not; inform the user. */
648 void
649 warn_types_mismatch (tree t1, tree t2)
651 if (!TYPE_NAME (t1) || !TYPE_NAME (t2))
652 return;
653 /* In Firefox it is a common bug to have same types but in
654 different namespaces. Be a bit more informative on
655 this. */
656 if (TYPE_CONTEXT (t1) && TYPE_CONTEXT (t2)
657 && (((TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL)
658 != (TREE_CODE (TYPE_CONTEXT (t2)) == NAMESPACE_DECL))
659 || (TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL
660 && (DECL_NAME (TYPE_CONTEXT (t1)) !=
661 DECL_NAME (TYPE_CONTEXT (t2))))))
662 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
663 "type %qT should match type %qT but is defined "
664 "in different namespace ",
665 t1, t2);
666 else
667 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
668 "type %qT should match type %qT",
669 t1, t2);
670 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
671 "the incompatible type is defined here");
674 /* Compare T1 and T2, report ODR violations if WARN is true and set
675 WARNED to true if anything is reported. Return true if types match.
676 If true is returned, the types are also compatible in the sense of
677 gimple_canonical_types_compatible_p. */
679 static bool
680 odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned, hash_set<tree> *visited)
682 /* Check first for the obvious case of pointer identity. */
683 if (t1 == t2)
684 return true;
685 gcc_assert (!type_in_anonymous_namespace_p (t1));
686 gcc_assert (!type_in_anonymous_namespace_p (t2));
688 /* Can't be the same type if the types don't have the same code. */
689 if (TREE_CODE (t1) != TREE_CODE (t2))
691 warn_odr (t1, t2, NULL, NULL, warn, warned,
692 G_("a different type is defined in another translation unit"));
693 return false;
696 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
698 warn_odr (t1, t2, NULL, NULL, warn, warned,
699 G_("a type with different qualifiers is defined in another "
700 "translation unit"));
701 return false;
704 if (comp_type_attributes (t1, t2) != 1)
706 warn_odr (t1, t2, NULL, NULL, warn, warned,
707 G_("a type with attributes "
708 "is defined in another translation unit"));
709 return false;
712 if (TREE_CODE (t1) == ENUMERAL_TYPE)
714 tree v1, v2;
715 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
716 v1 && v2 ; v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
718 if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
720 warn_odr (t1, t2, NULL, NULL, warn, warned,
721 G_("an enum with different value name"
722 " is defined in another translation unit"));
723 return false;
725 if (TREE_VALUE (v1) != TREE_VALUE (v2)
726 && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1)),
727 DECL_INITIAL (TREE_VALUE (v2)), 0))
729 warn_odr (t1, t2, NULL, NULL, warn, warned,
730 G_("an enum with different values is defined"
731 " in another translation unit"));
732 return false;
735 if (v1 || v2)
737 warn_odr (t1, t2, NULL, NULL, warn, warned,
738 G_("an enum with mismatching number of values "
739 "is defined in another translation unit"));
740 return false;
744 /* Non-aggregate types can be handled cheaply. */
745 if (INTEGRAL_TYPE_P (t1)
746 || SCALAR_FLOAT_TYPE_P (t1)
747 || FIXED_POINT_TYPE_P (t1)
748 || TREE_CODE (t1) == VECTOR_TYPE
749 || TREE_CODE (t1) == COMPLEX_TYPE
750 || TREE_CODE (t1) == OFFSET_TYPE
751 || POINTER_TYPE_P (t1))
753 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
755 warn_odr (t1, t2, NULL, NULL, warn, warned,
756 G_("a type with different precision is defined "
757 "in another translation unit"));
758 return false;
760 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
762 warn_odr (t1, t2, NULL, NULL, warn, warned,
763 G_("a type with different signedness is defined "
764 "in another translation unit"));
765 return false;
768 if (TREE_CODE (t1) == INTEGER_TYPE
769 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
771 /* char WRT uint_8? */
772 warn_odr (t1, t2, NULL, NULL, warn, warned,
773 G_("a different type is defined in another "
774 "translation unit"));
775 return false;
778 /* For canonical type comparisons we do not want to build SCCs
779 so we cannot compare pointed-to types. But we can, for now,
780 require the same pointed-to type kind and match what
781 useless_type_conversion_p would do. */
782 if (POINTER_TYPE_P (t1))
784 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
785 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
787 warn_odr (t1, t2, NULL, NULL, warn, warned,
788 G_("it is defined as a pointer in different address "
789 "space in another translation unit"));
790 return false;
793 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
795 warn_odr (t1, t2, NULL, NULL, warn, warned,
796 G_("it is defined as a pointer to different type "
797 "in another translation unit"));
798 if (warn && warned)
799 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
800 return false;
804 /* Tail-recurse to components. */
805 if ((TREE_CODE (t1) == VECTOR_TYPE || TREE_CODE (t1) == COMPLEX_TYPE)
806 && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
808 /* Probably specific enough. */
809 warn_odr (t1, t2, NULL, NULL, warn, warned,
810 G_("a different type is defined "
811 "in another translation unit"));
812 if (warn && warned)
813 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
814 return false;
817 gcc_assert (operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0));
818 gcc_assert (operand_equal_p (TYPE_SIZE_UNIT (t1),
819 TYPE_SIZE_UNIT (t2), 0));
820 gcc_assert (TYPE_MODE (t1) == TYPE_MODE (t2));
822 return true;
825 /* Do type-specific comparisons. */
826 switch (TREE_CODE (t1))
828 case ARRAY_TYPE:
830 /* Array types are the same if the element types are the same and
831 the number of elements are the same. */
832 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
834 warn_odr (t1, t2, NULL, NULL, warn, warned,
835 G_("a different type is defined in another "
836 "translation unit"));
837 if (warn && warned)
838 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
840 gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
841 gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
842 == TYPE_NONALIASED_COMPONENT (t2));
844 tree i1 = TYPE_DOMAIN (t1);
845 tree i2 = TYPE_DOMAIN (t2);
847 /* For an incomplete external array, the type domain can be
848 NULL_TREE. Check this condition also. */
849 if (i1 == NULL_TREE || i2 == NULL_TREE)
850 return true;
852 tree min1 = TYPE_MIN_VALUE (i1);
853 tree min2 = TYPE_MIN_VALUE (i2);
854 tree max1 = TYPE_MAX_VALUE (i1);
855 tree max2 = TYPE_MAX_VALUE (i2);
857 /* In C++, minimums should be always 0. */
858 gcc_assert (min1 == min2);
859 if (!operand_equal_p (max1, max2, 0))
861 warn_odr (t1, t2, NULL, NULL, warn, warned,
862 G_("an array of different size is defined "
863 "in another translation unit"));
864 return false;
866 gcc_assert (operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0));
867 gcc_assert (operand_equal_p (TYPE_SIZE_UNIT (t1),
868 TYPE_SIZE_UNIT (t2), 0));
870 return true;
872 case METHOD_TYPE:
873 case FUNCTION_TYPE:
874 /* Function types are the same if the return type and arguments types
875 are the same. */
876 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
878 warn_odr (t1, t2, NULL, NULL, warn, warned,
879 G_("has different return value "
880 "in another translation unit"));
881 if (warn && warned)
882 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
883 return false;
886 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
887 return true;
888 else
890 tree parms1, parms2;
892 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
893 parms1 && parms2;
894 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
896 if (!odr_subtypes_equivalent_p
897 (TREE_VALUE (parms1), TREE_VALUE (parms2), visited))
899 warn_odr (t1, t2, NULL, NULL, warn, warned,
900 G_("has different parameters in another "
901 "translation unit"));
902 if (warn && warned)
903 warn_types_mismatch (TREE_VALUE (parms1),
904 TREE_VALUE (parms2));
905 return false;
909 if (parms1 || parms2)
911 warn_odr (t1, t2, NULL, NULL, warn, warned,
912 G_("has different parameters "
913 "in another translation unit"));
914 return false;
917 return true;
920 case RECORD_TYPE:
921 case UNION_TYPE:
922 case QUAL_UNION_TYPE:
924 tree f1, f2;
926 /* For aggregate types, all the fields must be the same. */
927 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
929 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
930 f1 || f2;
931 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
933 /* Skip non-fields. */
934 while (f1 && TREE_CODE (f1) != FIELD_DECL)
935 f1 = TREE_CHAIN (f1);
936 while (f2 && TREE_CODE (f2) != FIELD_DECL)
937 f2 = TREE_CHAIN (f2);
938 if (!f1 || !f2)
939 break;
940 if (DECL_ARTIFICIAL (f1) != DECL_ARTIFICIAL (f2))
941 break;
942 if (DECL_NAME (f1) != DECL_NAME (f2)
943 && !DECL_ARTIFICIAL (f1))
945 warn_odr (t1, t2, f1, f2, warn, warned,
946 G_("a field with different name is defined "
947 "in another translation unit"));
948 return false;
950 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1), TREE_TYPE (f2), visited))
952 /* Do not warn about artificial fields and just go into generic
953 field mismatch warning. */
954 if (DECL_ARTIFICIAL (f1))
955 break;
957 warn_odr (t1, t2, f1, f2, warn, warned,
958 G_("a field of same name but different type "
959 "is defined in another translation unit"));
960 if (warn && warned)
961 warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2));
962 return false;
964 if (!gimple_compare_field_offset (f1, f2))
966 /* Do not warn about artificial fields and just go into generic
967 field mismatch warning. */
968 if (DECL_ARTIFICIAL (f1))
969 break;
970 warn_odr (t1, t2, t1, t2, warn, warned,
971 G_("fields has different layout "
972 "in another translation unit"));
973 return false;
975 gcc_assert (DECL_NONADDRESSABLE_P (f1)
976 == DECL_NONADDRESSABLE_P (f2));
979 /* If one aggregate has more fields than the other, they
980 are not the same. */
981 if (f1 || f2)
983 warn_odr (t1, t2, NULL, NULL, warn, warned,
984 G_("a type with different number of fields "
985 "is defined in another translation unit"));
986 return false;
988 if ((TYPE_MAIN_VARIANT (t1) == t1 || TYPE_MAIN_VARIANT (t2) == t2)
989 && (TYPE_METHODS (TYPE_MAIN_VARIANT (t1))
990 != TYPE_METHODS (TYPE_MAIN_VARIANT (t2))))
992 for (f1 = TYPE_METHODS (TYPE_MAIN_VARIANT (t1)),
993 f2 = TYPE_METHODS (TYPE_MAIN_VARIANT (t2));
994 f1 && f2 ; f1 = DECL_CHAIN (f1), f2 = DECL_CHAIN (f2))
996 if (DECL_ASSEMBLER_NAME (f1) != DECL_ASSEMBLER_NAME (f2))
998 warn_odr (t1, t2, f1, f2, warn, warned,
999 G_("a different method of same type "
1000 "is defined in another translation unit"));
1001 return false;
1003 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1005 warn_odr (t1, t2, f1, f2, warn, warned,
1006 G_("s definition that differs by virtual "
1007 "keyword in another translation unit"));
1008 return false;
1010 if (DECL_VINDEX (f1) != DECL_VINDEX (f2))
1012 warn_odr (t1, t2, f1, f2, warn, warned,
1013 G_("virtual table layout differs in another "
1014 "translation unit"));
1015 return false;
1017 if (odr_subtypes_equivalent_p (TREE_TYPE (f1), TREE_TYPE (f2), visited))
1019 warn_odr (t1, t2, f1, f2, warn, warned,
1020 G_("method with incompatible type is defined "
1021 "in another translation unit"));
1022 return false;
1025 if (f1 || f2)
1027 warn_odr (t1, t2, NULL, NULL, warn, warned,
1028 G_("a type with different number of methods "
1029 "is defined in another translation unit"));
1030 return false;
1033 gcc_assert (operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0));
1034 gcc_assert (operand_equal_p (TYPE_SIZE_UNIT (t1),
1035 TYPE_SIZE_UNIT (t2), 0));
1038 return true;
1041 default:
1042 gcc_unreachable ();
1046 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1047 from VAL->type. This may happen in LTO where tree merging did not merge
1048 all variants of the same type. It may or may not mean the ODR violation.
1049 Add it to the list of duplicates and warn on some violations. */
1051 static bool
1052 add_type_duplicate (odr_type val, tree type)
1054 bool build_bases = false;
1055 if (!val->types_set)
1056 val->types_set = new hash_set<tree>;
1058 /* Always prefer complete type to be the leader. */
1059 if (!COMPLETE_TYPE_P (val->type)
1060 && COMPLETE_TYPE_P (type))
1062 tree tmp = type;
1064 build_bases = true;
1065 type = val->type;
1066 val->type = tmp;
1069 /* See if this duplicate is new. */
1070 if (!val->types_set->add (type))
1072 bool merge = true;
1073 bool base_mismatch = false;
1074 unsigned int i,j;
1075 bool warned = false;
1076 hash_set<tree> visited;
1078 gcc_assert (in_lto_p);
1079 vec_safe_push (val->types, type);
1081 /* First we compare memory layout. */
1082 if (!odr_types_equivalent_p (val->type, type, !flag_ltrans && !val->odr_violated,
1083 &warned, &visited))
1085 merge = false;
1086 odr_violation_reported = true;
1087 val->odr_violated = true;
1088 if (symtab->dump_file)
1090 fprintf (symtab->dump_file, "ODR violation\n");
1092 print_node (symtab->dump_file, "", val->type, 0);
1093 putc ('\n',symtab->dump_file);
1094 print_node (symtab->dump_file, "", type, 0);
1095 putc ('\n',symtab->dump_file);
1099 /* Next sanity check that bases are the same. If not, we will end
1100 up producing wrong answers. */
1101 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1102 && TREE_CODE (val->type) == RECORD_TYPE
1103 && TREE_CODE (type) == RECORD_TYPE
1104 && TYPE_BINFO (val->type) && TYPE_BINFO (type))
1106 for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1107 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (TYPE_BINFO (type), i)))
1109 odr_type base = get_odr_type
1110 (BINFO_TYPE
1111 (BINFO_BASE_BINFO (TYPE_BINFO (type),
1112 i)),
1113 true);
1114 if (val->bases.length () <= j || val->bases[j] != base)
1115 base_mismatch = true;
1116 j++;
1118 if (base_mismatch)
1120 merge = false;
1121 odr_violation_reported = true;
1123 if (!warned && !val->odr_violated)
1124 warn_odr (type, val->type, NULL, NULL, !warned, &warned,
1125 "a type with the same name but different bases is "
1126 "defined in another translation unit");
1127 val->odr_violated = true;
1128 if (symtab->dump_file)
1130 fprintf (symtab->dump_file, "ODR bse violation or merging bug?\n");
1132 print_node (symtab->dump_file, "", val->type, 0);
1133 putc ('\n',symtab->dump_file);
1134 print_node (symtab->dump_file, "", type, 0);
1135 putc ('\n',symtab->dump_file);
1140 /* Regularize things a little. During LTO same types may come with
1141 different BINFOs. Either because their virtual table was
1142 not merged by tree merging and only later at decl merging or
1143 because one type comes with external vtable, while other
1144 with internal. We want to merge equivalent binfos to conserve
1145 memory and streaming overhead.
1147 The external vtables are more harmful: they contain references
1148 to external declarations of methods that may be defined in the
1149 merged LTO unit. For this reason we absolutely need to remove
1150 them and replace by internal variants. Not doing so will lead
1151 to incomplete answers from possible_polymorphic_call_targets. */
1152 if (!flag_ltrans && merge
1153 && TREE_CODE (val->type) == RECORD_TYPE
1154 && TREE_CODE (type) == RECORD_TYPE
1155 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1156 && TYPE_MAIN_VARIANT (type) == type
1157 && TYPE_MAIN_VARIANT (val->type) == val->type
1158 && BINFO_VTABLE (TYPE_BINFO (val->type))
1159 && BINFO_VTABLE (TYPE_BINFO (type)))
1161 tree master_binfo = TYPE_BINFO (val->type);
1162 tree v1 = BINFO_VTABLE (master_binfo);
1163 tree v2 = BINFO_VTABLE (TYPE_BINFO (type));
1165 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
1167 gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR
1168 && operand_equal_p (TREE_OPERAND (v1, 1),
1169 TREE_OPERAND (v2, 1), 0));
1170 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
1171 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
1173 gcc_assert (DECL_ASSEMBLER_NAME (v1)
1174 == DECL_ASSEMBLER_NAME (v2));
1176 if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2))
1178 unsigned int i;
1180 set_type_binfo (val->type, TYPE_BINFO (type));
1181 for (i = 0; i < val->types->length (); i++)
1183 if (TYPE_BINFO ((*val->types)[i])
1184 == master_binfo)
1185 set_type_binfo ((*val->types)[i], TYPE_BINFO (type));
1187 BINFO_TYPE (TYPE_BINFO (type)) = val->type;
1189 else
1190 set_type_binfo (type, master_binfo);
1193 return build_bases;
1196 /* Get ODR type hash entry for TYPE. If INSERT is true, create
1197 possibly new entry. */
1199 odr_type
1200 get_odr_type (tree type, bool insert)
1202 odr_type_d **slot;
1203 odr_type val;
1204 hashval_t hash;
1205 bool build_bases = false;
1206 bool insert_to_odr_array = false;
1207 int base_id = -1;
1209 type = main_odr_variant (type);
1211 hash = hash_type_name (type);
1212 slot
1213 = odr_hash->find_slot_with_hash (type, hash, insert ? INSERT : NO_INSERT);
1214 if (!slot)
1215 return NULL;
1217 /* See if we already have entry for type. */
1218 if (*slot)
1220 val = *slot;
1222 /* With LTO we need to support multiple tree representation of
1223 the same ODR type. */
1224 if (val->type != type)
1225 build_bases = add_type_duplicate (val, type);
1227 else
1229 val = ggc_cleared_alloc<odr_type_d> ();
1230 val->type = type;
1231 val->bases = vNULL;
1232 val->derived_types = vNULL;
1233 val->anonymous_namespace = type_in_anonymous_namespace_p (type);
1234 build_bases = COMPLETE_TYPE_P (val->type);
1235 insert_to_odr_array = true;
1238 if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1239 && type == TYPE_MAIN_VARIANT (type))
1241 tree binfo = TYPE_BINFO (type);
1242 unsigned int i;
1244 gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) = type);
1246 val->all_derivations_known = type_all_derivations_known_p (type);
1247 *slot = val;
1248 for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
1249 /* For now record only polymorphic types. other are
1250 pointless for devirtualization and we can not precisely
1251 determine ODR equivalency of these during LTO. */
1252 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
1254 odr_type base = get_odr_type (BINFO_TYPE (BINFO_BASE_BINFO (binfo,
1255 i)),
1256 true);
1257 gcc_assert (TYPE_MAIN_VARIANT (base->type) == base->type);
1258 base->derived_types.safe_push (val);
1259 val->bases.safe_push (base);
1260 if (base->id > base_id)
1261 base_id = base->id;
1264 /* Ensure that type always appears after bases. */
1265 if (insert_to_odr_array)
1267 if (odr_types_ptr)
1268 val->id = odr_types.length ();
1269 vec_safe_push (odr_types_ptr, val);
1271 else if (base_id > val->id)
1273 odr_types[val->id] = 0;
1274 /* Be sure we did not recorded any derived types; these may need
1275 renumbering too. */
1276 gcc_assert (val->derived_types.length() == 0);
1277 if (odr_types_ptr)
1278 val->id = odr_types.length ();
1279 vec_safe_push (odr_types_ptr, val);
1281 return val;
1284 /* Dump ODR type T and all its derrived type. INDENT specify indentation for
1285 recusive printing. */
1287 static void
1288 dump_odr_type (FILE *f, odr_type t, int indent=0)
1290 unsigned int i;
1291 fprintf (f, "%*s type %i: ", indent * 2, "", t->id);
1292 print_generic_expr (f, t->type, TDF_SLIM);
1293 fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":"");
1294 fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":"");
1295 if (TYPE_NAME (t->type))
1297 fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
1298 DECL_SOURCE_FILE (TYPE_NAME (t->type)),
1299 DECL_SOURCE_LINE (TYPE_NAME (t->type)));
1301 if (t->bases.length ())
1303 fprintf (f, "%*s base odr type ids: ", indent * 2, "");
1304 for (i = 0; i < t->bases.length (); i++)
1305 fprintf (f, " %i", t->bases[i]->id);
1306 fprintf (f, "\n");
1308 if (t->derived_types.length ())
1310 fprintf (f, "%*s derived types:\n", indent * 2, "");
1311 for (i = 0; i < t->derived_types.length (); i++)
1312 dump_odr_type (f, t->derived_types[i], indent + 1);
1314 fprintf (f, "\n");
1317 /* Dump the type inheritance graph. */
1319 static void
1320 dump_type_inheritance_graph (FILE *f)
1322 unsigned int i;
1323 if (!odr_types_ptr)
1324 return;
1325 fprintf (f, "\n\nType inheritance graph:\n");
1326 for (i = 0; i < odr_types.length (); i++)
1328 if (odr_types[i] && odr_types[i]->bases.length () == 0)
1329 dump_odr_type (f, odr_types[i]);
1331 for (i = 0; i < odr_types.length (); i++)
1333 if (odr_types[i] && odr_types[i]->types && odr_types[i]->types->length ())
1335 unsigned int j;
1336 fprintf (f, "Duplicate tree types for odr type %i\n", i);
1337 print_node (f, "", odr_types[i]->type, 0);
1338 for (j = 0; j < odr_types[i]->types->length (); j++)
1340 tree t;
1341 fprintf (f, "duplicate #%i\n", j);
1342 print_node (f, "", (*odr_types[i]->types)[j], 0);
1343 t = (*odr_types[i]->types)[j];
1344 while (TYPE_P (t) && TYPE_CONTEXT (t))
1346 t = TYPE_CONTEXT (t);
1347 print_node (f, "", t, 0);
1349 putc ('\n',f);
1355 /* Given method type T, return type of class it belongs to.
1356 Lookup this pointer and get its type. */
1358 tree
1359 method_class_type (const_tree t)
1361 tree first_parm_type = TREE_VALUE (TYPE_ARG_TYPES (t));
1362 gcc_assert (TREE_CODE (t) == METHOD_TYPE);
1364 return TREE_TYPE (first_parm_type);
1367 /* Initialize IPA devirt and build inheritance tree graph. */
1369 void
1370 build_type_inheritance_graph (void)
1372 struct symtab_node *n;
1373 FILE *inheritance_dump_file;
1374 int flags;
1376 if (odr_hash)
1377 return;
1378 timevar_push (TV_IPA_INHERITANCE);
1379 inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
1380 odr_hash = new odr_hash_type (23);
1382 /* We reconstruct the graph starting of types of all methods seen in the
1383 the unit. */
1384 FOR_EACH_SYMBOL (n)
1385 if (is_a <cgraph_node *> (n)
1386 && DECL_VIRTUAL_P (n->decl)
1387 && n->real_symbol_p ())
1388 get_odr_type (TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (n->decl))),
1389 true);
1391 /* Look also for virtual tables of types that do not define any methods.
1393 We need it in a case where class B has virtual base of class A
1394 re-defining its virtual method and there is class C with no virtual
1395 methods with B as virtual base.
1397 Here we output B's virtual method in two variant - for non-virtual
1398 and virtual inheritance. B's virtual table has non-virtual version,
1399 while C's has virtual.
1401 For this reason we need to know about C in order to include both
1402 variants of B. More correctly, record_target_from_binfo should
1403 add both variants of the method when walking B, but we have no
1404 link in between them.
1406 We rely on fact that either the method is exported and thus we
1407 assume it is called externally or C is in anonymous namespace and
1408 thus we will see the vtable. */
1410 else if (is_a <varpool_node *> (n)
1411 && DECL_VIRTUAL_P (n->decl)
1412 && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE
1413 && TYPE_BINFO (DECL_CONTEXT (n->decl))
1414 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl))))
1415 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n->decl)), true);
1416 if (inheritance_dump_file)
1418 dump_type_inheritance_graph (inheritance_dump_file);
1419 dump_end (TDI_inheritance, inheritance_dump_file);
1421 timevar_pop (TV_IPA_INHERITANCE);
1424 /* Return true if N has reference from live virtual table
1425 (and thus can be a destination of polymorphic call).
1426 Be conservatively correct when callgraph is not built or
1427 if the method may be referred externally. */
1429 static bool
1430 referenced_from_vtable_p (struct cgraph_node *node)
1432 int i;
1433 struct ipa_ref *ref;
1434 bool found = false;
1436 if (node->externally_visible
1437 || DECL_EXTERNAL (node->decl)
1438 || node->used_from_other_partition)
1439 return true;
1441 /* Keep this test constant time.
1442 It is unlikely this can happen except for the case where speculative
1443 devirtualization introduced many speculative edges to this node.
1444 In this case the target is very likely alive anyway. */
1445 if (node->ref_list.referring.length () > 100)
1446 return true;
1448 /* We need references built. */
1449 if (symtab->state <= CONSTRUCTION)
1450 return true;
1452 for (i = 0; node->iterate_referring (i, ref); i++)
1454 if ((ref->use == IPA_REF_ALIAS
1455 && referenced_from_vtable_p (dyn_cast<cgraph_node *> (ref->referring)))
1456 || (ref->use == IPA_REF_ADDR
1457 && TREE_CODE (ref->referring->decl) == VAR_DECL
1458 && DECL_VIRTUAL_P (ref->referring->decl)))
1460 found = true;
1461 break;
1463 return found;
1466 /* If TARGET has associated node, record it in the NODES array.
1467 CAN_REFER specify if program can refer to the target directly.
1468 if TARGET is unknown (NULL) or it can not be inserted (for example because
1469 its body was already removed and there is no way to refer to it), clear
1470 COMPLETEP. */
1472 static void
1473 maybe_record_node (vec <cgraph_node *> &nodes,
1474 tree target, hash_set<tree> *inserted,
1475 bool can_refer,
1476 bool *completep)
1478 struct cgraph_node *target_node, *alias_target;
1479 enum availability avail;
1481 /* cxa_pure_virtual and __builtin_unreachable do not need to be added into
1482 list of targets; the runtime effect of calling them is undefined.
1483 Only "real" virtual methods should be accounted. */
1484 if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE)
1485 return;
1487 if (!can_refer)
1489 /* The only case when method of anonymous namespace becomes unreferable
1490 is when we completely optimized it out. */
1491 if (flag_ltrans
1492 || !target
1493 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
1494 *completep = false;
1495 return;
1498 if (!target)
1499 return;
1501 target_node = cgraph_node::get (target);
1503 /* Preffer alias target over aliases, so we do not get confused by
1504 fake duplicates. */
1505 if (target_node)
1507 alias_target = target_node->ultimate_alias_target (&avail);
1508 if (target_node != alias_target
1509 && avail >= AVAIL_AVAILABLE
1510 && target_node->get_availability ())
1511 target_node = alias_target;
1514 /* Method can only be called by polymorphic call if any
1515 of vtables refering to it are alive.
1517 While this holds for non-anonymous functions, too, there are
1518 cases where we want to keep them in the list; for example
1519 inline functions with -fno-weak are static, but we still
1520 may devirtualize them when instance comes from other unit.
1521 The same holds for LTO.
1523 Currently we ignore these functions in speculative devirtualization.
1524 ??? Maybe it would make sense to be more aggressive for LTO even
1525 eslewhere. */
1526 if (!flag_ltrans
1527 && type_in_anonymous_namespace_p (DECL_CONTEXT (target))
1528 && (!target_node
1529 || !referenced_from_vtable_p (target_node)))
1531 /* See if TARGET is useful function we can deal with. */
1532 else if (target_node != NULL
1533 && (TREE_PUBLIC (target)
1534 || DECL_EXTERNAL (target)
1535 || target_node->definition)
1536 && target_node->real_symbol_p ())
1538 gcc_assert (!target_node->global.inlined_to);
1539 gcc_assert (target_node->real_symbol_p ());
1540 if (!inserted->add (target))
1542 cached_polymorphic_call_targets->add (target_node);
1543 nodes.safe_push (target_node);
1546 else if (completep
1547 && (!type_in_anonymous_namespace_p
1548 (DECL_CONTEXT (target))
1549 || flag_ltrans))
1550 *completep = false;
1553 /* See if BINFO's type match OUTER_TYPE. If so, lookup
1554 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
1555 method in vtable and insert method to NODES array
1556 or BASES_TO_CONSIDER if this array is non-NULL.
1557 Otherwise recurse to base BINFOs.
1558 This match what get_binfo_at_offset does, but with offset
1559 being unknown.
1561 TYPE_BINFOS is a stack of BINFOS of types with defined
1562 virtual table seen on way from class type to BINFO.
1564 MATCHED_VTABLES tracks virtual tables we already did lookup
1565 for virtual function in. INSERTED tracks nodes we already
1566 inserted.
1568 ANONYMOUS is true if BINFO is part of anonymous namespace.
1570 Clear COMPLETEP when we hit unreferable target.
1573 static void
1574 record_target_from_binfo (vec <cgraph_node *> &nodes,
1575 vec <tree> *bases_to_consider,
1576 tree binfo,
1577 tree otr_type,
1578 vec <tree> &type_binfos,
1579 HOST_WIDE_INT otr_token,
1580 tree outer_type,
1581 HOST_WIDE_INT offset,
1582 hash_set<tree> *inserted,
1583 hash_set<tree> *matched_vtables,
1584 bool anonymous,
1585 bool *completep)
1587 tree type = BINFO_TYPE (binfo);
1588 int i;
1589 tree base_binfo;
1592 if (BINFO_VTABLE (binfo))
1593 type_binfos.safe_push (binfo);
1594 if (types_same_for_odr (type, outer_type))
1596 int i;
1597 tree type_binfo = NULL;
1599 /* Lookup BINFO with virtual table. For normal types it is always last
1600 binfo on stack. */
1601 for (i = type_binfos.length () - 1; i >= 0; i--)
1602 if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo))
1604 type_binfo = type_binfos[i];
1605 break;
1607 if (BINFO_VTABLE (binfo))
1608 type_binfos.pop ();
1609 /* If this is duplicated BINFO for base shared by virtual inheritance,
1610 we may not have its associated vtable. This is not a problem, since
1611 we will walk it on the other path. */
1612 if (!type_binfo)
1613 return;
1614 tree inner_binfo = get_binfo_at_offset (type_binfo,
1615 offset, otr_type);
1616 if (!inner_binfo)
1618 gcc_assert (odr_violation_reported);
1619 return;
1621 /* For types in anonymous namespace first check if the respective vtable
1622 is alive. If not, we know the type can't be called. */
1623 if (!flag_ltrans && anonymous)
1625 tree vtable = BINFO_VTABLE (inner_binfo);
1626 varpool_node *vnode;
1628 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
1629 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
1630 vnode = varpool_node::get (vtable);
1631 if (!vnode || !vnode->definition)
1632 return;
1634 gcc_assert (inner_binfo);
1635 if (bases_to_consider
1636 ? !matched_vtables->contains (BINFO_VTABLE (inner_binfo))
1637 : !matched_vtables->add (BINFO_VTABLE (inner_binfo)))
1639 bool can_refer;
1640 tree target = gimple_get_virt_method_for_binfo (otr_token,
1641 inner_binfo,
1642 &can_refer);
1643 if (!bases_to_consider)
1644 maybe_record_node (nodes, target, inserted, can_refer, completep);
1645 /* Destructors are never called via construction vtables. */
1646 else if (!target || !DECL_CXX_DESTRUCTOR_P (target))
1647 bases_to_consider->safe_push (target);
1649 return;
1652 /* Walk bases. */
1653 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1654 /* Walking bases that have no virtual method is pointless excercise. */
1655 if (polymorphic_type_binfo_p (base_binfo))
1656 record_target_from_binfo (nodes, bases_to_consider, base_binfo, otr_type,
1657 type_binfos,
1658 otr_token, outer_type, offset, inserted,
1659 matched_vtables, anonymous, completep);
1660 if (BINFO_VTABLE (binfo))
1661 type_binfos.pop ();
1664 /* Lookup virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
1665 of TYPE, insert them to NODES, recurse into derived nodes.
1666 INSERTED is used to avoid duplicate insertions of methods into NODES.
1667 MATCHED_VTABLES are used to avoid duplicate walking vtables.
1668 Clear COMPLETEP if unreferable target is found.
1670 If CONSIDER_CONSTURCTION is true, record to BASES_TO_CONSDIER
1671 all cases where BASE_SKIPPED is true (because the base is abstract
1672 class). */
1674 static void
1675 possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
1676 hash_set<tree> *inserted,
1677 hash_set<tree> *matched_vtables,
1678 tree otr_type,
1679 odr_type type,
1680 HOST_WIDE_INT otr_token,
1681 tree outer_type,
1682 HOST_WIDE_INT offset,
1683 bool *completep,
1684 vec <tree> &bases_to_consider,
1685 bool consider_construction)
1687 tree binfo = TYPE_BINFO (type->type);
1688 unsigned int i;
1689 vec <tree> type_binfos = vNULL;
1690 bool possibly_instantiated = type_possibly_instantiated_p (type->type);
1692 /* We may need to consider types w/o instances because of possible derived
1693 types using their methods either directly or via construction vtables.
1694 We are safe to skip them when all derivations are known, since we will
1695 handle them later.
1696 This is done by recording them to BASES_TO_CONSIDER array. */
1697 if (possibly_instantiated || consider_construction)
1699 record_target_from_binfo (nodes,
1700 (!possibly_instantiated
1701 && type_all_derivations_known_p (type->type))
1702 ? &bases_to_consider : NULL,
1703 binfo, otr_type, type_binfos, otr_token,
1704 outer_type, offset,
1705 inserted, matched_vtables,
1706 type->anonymous_namespace, completep);
1708 type_binfos.release ();
1709 for (i = 0; i < type->derived_types.length (); i++)
1710 possible_polymorphic_call_targets_1 (nodes, inserted,
1711 matched_vtables,
1712 otr_type,
1713 type->derived_types[i],
1714 otr_token, outer_type, offset, completep,
1715 bases_to_consider, consider_construction);
1718 /* Cache of queries for polymorphic call targets.
1720 Enumerating all call targets may get expensive when there are many
1721 polymorphic calls in the program, so we memoize all the previous
1722 queries and avoid duplicated work. */
1724 struct polymorphic_call_target_d
1726 HOST_WIDE_INT otr_token;
1727 ipa_polymorphic_call_context context;
1728 odr_type type;
1729 vec <cgraph_node *> targets;
1730 int speculative_targets;
1731 bool complete;
1732 int type_warning;
1733 tree decl_warning;
1736 /* Polymorphic call target cache helpers. */
1738 struct polymorphic_call_target_hasher
1740 typedef polymorphic_call_target_d value_type;
1741 typedef polymorphic_call_target_d compare_type;
1742 static inline hashval_t hash (const value_type *);
1743 static inline bool equal (const value_type *, const compare_type *);
1744 static inline void remove (value_type *);
1747 /* Return the computed hashcode for ODR_QUERY. */
1749 inline hashval_t
1750 polymorphic_call_target_hasher::hash (const value_type *odr_query)
1752 inchash::hash hstate (odr_query->otr_token);
1754 hstate.add_wide_int (odr_query->type->id);
1755 hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
1756 hstate.add_wide_int (odr_query->context.offset);
1758 if (odr_query->context.speculative_outer_type)
1760 hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
1761 hstate.add_wide_int (odr_query->context.speculative_offset);
1763 hstate.add_flag (odr_query->context.maybe_in_construction);
1764 hstate.add_flag (odr_query->context.maybe_derived_type);
1765 hstate.add_flag (odr_query->context.speculative_maybe_derived_type);
1766 hstate.commit_flag ();
1767 return hstate.end ();
1770 /* Compare cache entries T1 and T2. */
1772 inline bool
1773 polymorphic_call_target_hasher::equal (const value_type *t1,
1774 const compare_type *t2)
1776 return (t1->type == t2->type && t1->otr_token == t2->otr_token
1777 && t1->context.offset == t2->context.offset
1778 && t1->context.speculative_offset == t2->context.speculative_offset
1779 && t1->context.outer_type == t2->context.outer_type
1780 && t1->context.speculative_outer_type == t2->context.speculative_outer_type
1781 && t1->context.maybe_in_construction
1782 == t2->context.maybe_in_construction
1783 && t1->context.maybe_derived_type == t2->context.maybe_derived_type
1784 && (t1->context.speculative_maybe_derived_type
1785 == t2->context.speculative_maybe_derived_type));
1788 /* Remove entry in polymorphic call target cache hash. */
1790 inline void
1791 polymorphic_call_target_hasher::remove (value_type *v)
1793 v->targets.release ();
1794 free (v);
1797 /* Polymorphic call target query cache. */
1799 typedef hash_table<polymorphic_call_target_hasher>
1800 polymorphic_call_target_hash_type;
1801 static polymorphic_call_target_hash_type *polymorphic_call_target_hash;
1803 /* Destroy polymorphic call target query cache. */
1805 static void
1806 free_polymorphic_call_targets_hash ()
1808 if (cached_polymorphic_call_targets)
1810 delete polymorphic_call_target_hash;
1811 polymorphic_call_target_hash = NULL;
1812 delete cached_polymorphic_call_targets;
1813 cached_polymorphic_call_targets = NULL;
1817 /* When virtual function is removed, we may need to flush the cache. */
1819 static void
1820 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
1822 if (cached_polymorphic_call_targets
1823 && cached_polymorphic_call_targets->contains (n))
1824 free_polymorphic_call_targets_hash ();
1827 /* Return true when TYPE contains an polymorphic type and thus is interesting
1828 for devirtualization machinery. */
1830 bool
1831 contains_polymorphic_type_p (const_tree type)
1833 type = TYPE_MAIN_VARIANT (type);
1835 if (RECORD_OR_UNION_TYPE_P (type))
1837 if (TYPE_BINFO (type)
1838 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
1839 return true;
1840 for (tree fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
1841 if (TREE_CODE (fld) == FIELD_DECL
1842 && !DECL_ARTIFICIAL (fld)
1843 && contains_polymorphic_type_p (TREE_TYPE (fld)))
1844 return true;
1845 return false;
1847 if (TREE_CODE (type) == ARRAY_TYPE)
1848 return contains_polymorphic_type_p (TREE_TYPE (type));
1849 return false;
1852 /* THIS->OUTER_TYPE is a type of memory object where object of EXPECTED_TYPE
1853 is contained at THIS->OFFSET. Walk the memory representation of
1854 THIS->OUTER_TYPE and find the outermost class type that match
1855 EXPECTED_TYPE or contain EXPECTED_TYPE as a base. Update THIS
1856 to represent it.
1858 For example when THIS represents type
1859 class A
1861 int a;
1862 class B b;
1864 and we look for type at offset sizeof(int), we end up with B and offset 0.
1865 If the same is produced by multiple inheritance, we end up with A and offset
1866 sizeof(int).
1868 If we can not find corresponding class, give up by setting
1869 THIS->OUTER_TYPE to EXPECTED_TYPE and THIS->OFFSET to NULL.
1870 Return true when lookup was sucesful. */
1872 bool
1873 ipa_polymorphic_call_context::restrict_to_inner_class (tree expected_type)
1875 tree type = outer_type;
1876 HOST_WIDE_INT cur_offset = offset;
1877 bool speculative = false;
1878 bool speculation_valid = false;
1879 bool valid = false;
1881 if (!outer_type)
1883 type = outer_type = expected_type;
1884 offset = cur_offset = 0;
1887 if (speculative_outer_type == outer_type
1888 && (!maybe_derived_type
1889 || speculative_maybe_derived_type))
1891 speculative_outer_type = NULL;
1892 speculative_offset = 0;
1893 speculative_maybe_derived_type = false;
1896 /* See if speculative type seem to be derrived from outer_type.
1897 Then speculation is valid only if it really is a derivate and derived types
1898 are allowed.
1900 The test does not really look for derivate, but also accepts the case where
1901 outer_type is a field of speculative_outer_type. In this case eiter
1902 MAYBE_DERIVED_TYPE is false and we have full non-speculative information or
1903 the loop bellow will correctly update SPECULATIVE_OUTER_TYPE
1904 and SPECULATIVE_MAYBE_DERIVED_TYPE. */
1905 if (speculative_outer_type
1906 && speculative_offset >= offset
1907 && contains_type_p (speculative_outer_type,
1908 offset - speculative_offset,
1909 outer_type))
1910 speculation_valid = maybe_derived_type;
1911 else
1912 clear_speculation ();
1914 /* Find the sub-object the constant actually refers to and mark whether it is
1915 an artificial one (as opposed to a user-defined one).
1917 This loop is performed twice; first time for outer_type and second time
1918 for speculative_outer_type. The second iteration has SPECULATIVE set. */
1919 while (true)
1921 HOST_WIDE_INT pos, size;
1922 tree fld;
1924 /* On a match, just return what we found. */
1925 if (TREE_CODE (type) == TREE_CODE (expected_type)
1926 && (!in_lto_p
1927 || (TREE_CODE (type) == RECORD_TYPE
1928 && TYPE_BINFO (type)
1929 && polymorphic_type_binfo_p (TYPE_BINFO (type))))
1930 && types_same_for_odr (type, expected_type))
1932 if (speculative)
1934 gcc_assert (speculation_valid);
1935 gcc_assert (valid);
1937 /* If we did not match the offset, just give up on speculation. */
1938 if (cur_offset != 0
1939 || (types_same_for_odr (speculative_outer_type,
1940 outer_type)
1941 && (maybe_derived_type
1942 == speculative_maybe_derived_type)))
1943 clear_speculation ();
1944 return true;
1946 else
1948 /* Type can not contain itself on an non-zero offset. In that case
1949 just give up. */
1950 if (cur_offset != 0)
1952 valid = false;
1953 goto give_up;
1955 valid = true;
1956 /* If speculation is not valid or we determined type precisely,
1957 we are done. */
1958 if (!speculation_valid
1959 || !maybe_derived_type)
1961 clear_speculation ();
1962 return true;
1964 /* Otherwise look into speculation now. */
1965 else
1967 speculative = true;
1968 type = speculative_outer_type;
1969 cur_offset = speculative_offset;
1970 continue;
1975 /* Walk fields and find corresponding on at OFFSET. */
1976 if (TREE_CODE (type) == RECORD_TYPE)
1978 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
1980 if (TREE_CODE (fld) != FIELD_DECL)
1981 continue;
1983 pos = int_bit_position (fld);
1984 size = tree_to_uhwi (DECL_SIZE (fld));
1985 if (pos <= cur_offset && (pos + size) > cur_offset)
1986 break;
1989 if (!fld)
1990 goto give_up;
1992 type = TYPE_MAIN_VARIANT (TREE_TYPE (fld));
1993 cur_offset -= pos;
1994 /* DECL_ARTIFICIAL represents a basetype. */
1995 if (!DECL_ARTIFICIAL (fld))
1997 if (!speculative)
1999 outer_type = type;
2000 offset = cur_offset;
2001 /* As soon as we se an field containing the type,
2002 we know we are not looking for derivations. */
2003 maybe_derived_type = false;
2005 else
2007 speculative_outer_type = type;
2008 speculative_offset = cur_offset;
2009 speculative_maybe_derived_type = false;
2013 else if (TREE_CODE (type) == ARRAY_TYPE)
2015 tree subtype = TYPE_MAIN_VARIANT (TREE_TYPE (type));
2017 /* Give up if we don't know array size. */
2018 if (!TYPE_SIZE (subtype)
2019 || !tree_fits_shwi_p (TYPE_SIZE (subtype))
2020 || tree_to_shwi (TYPE_SIZE (subtype)) <= 0
2021 || !contains_polymorphic_type_p (subtype))
2022 goto give_up;
2023 cur_offset = cur_offset % tree_to_shwi (TYPE_SIZE (subtype));
2024 type = subtype;
2025 if (!speculative)
2027 outer_type = type;
2028 offset = cur_offset;
2029 maybe_derived_type = false;
2031 else
2033 speculative_outer_type = type;
2034 speculative_offset = cur_offset;
2035 speculative_maybe_derived_type = false;
2038 /* Give up on anything else. */
2039 else
2040 goto give_up;
2043 /* If we failed to find subtype we look for, give up and fall back to the
2044 most generic query. */
2045 give_up:
2046 clear_speculation ();
2047 if (valid)
2048 return true;
2049 outer_type = expected_type;
2050 offset = 0;
2051 maybe_derived_type = true;
2052 maybe_in_construction = true;
2053 /* POD can be changed to an instance of a polymorphic type by
2054 placement new. Here we play safe and assume that any
2055 non-polymorphic type is POD. */
2056 if ((TREE_CODE (type) != RECORD_TYPE
2057 || !TYPE_BINFO (type)
2058 || !polymorphic_type_binfo_p (TYPE_BINFO (type)))
2059 && (!TYPE_SIZE (type)
2060 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
2061 || (cur_offset + tree_to_uhwi (TYPE_SIZE (expected_type)) <=
2062 tree_to_uhwi (TYPE_SIZE (type)))))
2063 return true;
2064 return false;
2067 /* Return true if OUTER_TYPE contains OTR_TYPE at OFFSET. */
2069 static bool
2070 contains_type_p (tree outer_type, HOST_WIDE_INT offset,
2071 tree otr_type)
2073 ipa_polymorphic_call_context context;
2074 context.offset = offset;
2075 context.outer_type = TYPE_MAIN_VARIANT (outer_type);
2076 return context.restrict_to_inner_class (otr_type);
2079 /* Lookup base of BINFO that has virtual table VTABLE with OFFSET. */
2081 static tree
2082 subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
2083 tree vtable)
2085 tree v = BINFO_VTABLE (binfo);
2086 int i;
2087 tree base_binfo;
2088 unsigned HOST_WIDE_INT this_offset;
2090 if (v)
2092 if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
2093 gcc_unreachable ();
2095 if (offset == this_offset
2096 && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
2097 return binfo;
2100 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2101 if (polymorphic_type_binfo_p (base_binfo))
2103 base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable);
2104 if (base_binfo)
2105 return base_binfo;
2107 return NULL;
2110 /* T is known constant value of virtual table pointer.
2111 Store virtual table to V and its offset to OFFSET.
2112 Return false if T does not look like virtual table reference. */
2114 bool
2115 vtable_pointer_value_to_vtable (const_tree t, tree *v,
2116 unsigned HOST_WIDE_INT *offset)
2118 /* We expect &MEM[(void *)&virtual_table + 16B].
2119 We obtain object's BINFO from the context of the virtual table.
2120 This one contains pointer to virtual table represented via
2121 POINTER_PLUS_EXPR. Verify that this pointer match to what
2122 we propagated through.
2124 In the case of virtual inheritance, the virtual tables may
2125 be nested, i.e. the offset may be different from 16 and we may
2126 need to dive into the type representation. */
2127 if (TREE_CODE (t) == ADDR_EXPR
2128 && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
2129 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
2130 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
2131 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
2132 == VAR_DECL)
2133 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2134 (TREE_OPERAND (t, 0), 0), 0)))
2136 *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
2137 *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
2138 return true;
2141 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2142 We need to handle it when T comes from static variable initializer or
2143 BINFO. */
2144 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
2146 *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
2147 t = TREE_OPERAND (t, 0);
2149 else
2150 *offset = 0;
2152 if (TREE_CODE (t) != ADDR_EXPR)
2153 return false;
2154 *v = TREE_OPERAND (t, 0);
2155 return true;
2158 /* T is known constant value of virtual table pointer. Return BINFO of the
2159 instance type. */
2161 tree
2162 vtable_pointer_value_to_binfo (const_tree t)
2164 tree vtable;
2165 unsigned HOST_WIDE_INT offset;
2167 if (!vtable_pointer_value_to_vtable (t, &vtable, &offset))
2168 return NULL_TREE;
2170 /* FIXME: for stores of construction vtables we return NULL,
2171 because we do not have BINFO for those. Eventually we should fix
2172 our representation to allow this case to be handled, too.
2173 In the case we see store of BINFO we however may assume
2174 that standard folding will be ale to cope with it. */
2175 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
2176 offset, vtable);
2179 /* We know that the instance is stored in variable or parameter
2180 (not dynamically allocated) and we want to disprove the fact
2181 that it may be in construction at invocation of CALL.
2183 For the variable to be in construction we actually need to
2184 be in constructor of corresponding global variable or
2185 the inline stack of CALL must contain the constructor.
2186 Check this condition. This check works safely only before
2187 IPA passes, because inline stacks may become out of date
2188 later. */
2190 bool
2191 decl_maybe_in_construction_p (tree base, tree outer_type,
2192 gimple call, tree function)
2194 outer_type = TYPE_MAIN_VARIANT (outer_type);
2195 gcc_assert (DECL_P (base));
2197 /* After inlining the code unification optimizations may invalidate
2198 inline stacks. Also we need to give up on global variables after
2199 IPA, because addresses of these may have been propagated to their
2200 constructors. */
2201 if (DECL_STRUCT_FUNCTION (function)->after_inlining)
2202 return true;
2204 /* Pure functions can not do any changes on the dynamic type;
2205 that require writting to memory. */
2206 if (!auto_var_in_fn_p (base, function)
2207 && flags_from_decl_or_type (function) & (ECF_PURE | ECF_CONST))
2208 return false;
2210 for (tree block = gimple_block (call); block && TREE_CODE (block) == BLOCK;
2211 block = BLOCK_SUPERCONTEXT (block))
2212 if (BLOCK_ABSTRACT_ORIGIN (block)
2213 && TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block)) == FUNCTION_DECL)
2215 tree fn = BLOCK_ABSTRACT_ORIGIN (block);
2217 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
2218 || (!DECL_CXX_CONSTRUCTOR_P (fn)
2219 && !DECL_CXX_DESTRUCTOR_P (fn)))
2221 /* Watch for clones where we constant propagated the first
2222 argument (pointer to the instance). */
2223 fn = DECL_ABSTRACT_ORIGIN (fn);
2224 if (!fn
2225 || !is_global_var (base)
2226 || TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
2227 || (!DECL_CXX_CONSTRUCTOR_P (fn)
2228 && !DECL_CXX_DESTRUCTOR_P (fn)))
2229 continue;
2231 if (flags_from_decl_or_type (fn) & (ECF_PURE | ECF_CONST))
2232 continue;
2234 /* FIXME: this can go away once we have ODR types equivalency on
2235 LTO level. */
2236 if (in_lto_p && !polymorphic_type_binfo_p (TYPE_BINFO (outer_type)))
2237 return true;
2238 tree type = TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (fn)));
2239 if (types_same_for_odr (type, outer_type))
2240 return true;
2243 if (TREE_CODE (base) == VAR_DECL
2244 && is_global_var (base))
2246 if (TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
2247 || (!DECL_CXX_CONSTRUCTOR_P (function)
2248 && !DECL_CXX_DESTRUCTOR_P (function)))
2250 if (!DECL_ABSTRACT_ORIGIN (function))
2251 return false;
2252 /* Watch for clones where we constant propagated the first
2253 argument (pointer to the instance). */
2254 function = DECL_ABSTRACT_ORIGIN (function);
2255 if (!function
2256 || TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
2257 || (!DECL_CXX_CONSTRUCTOR_P (function)
2258 && !DECL_CXX_DESTRUCTOR_P (function)))
2259 return false;
2261 /* FIXME: this can go away once we have ODR types equivalency on
2262 LTO level. */
2263 if (in_lto_p && !polymorphic_type_binfo_p (TYPE_BINFO (outer_type)))
2264 return true;
2265 tree type = TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (function)));
2266 if (types_same_for_odr (type, outer_type))
2267 return true;
2269 return false;
2272 /* Proudce polymorphic call context for call method of instance
2273 that is located within BASE (that is assumed to be a decl) at OFFSET. */
2275 static void
2276 get_polymorphic_call_info_for_decl (ipa_polymorphic_call_context *context,
2277 tree base, HOST_WIDE_INT offset)
2279 gcc_assert (DECL_P (base));
2281 context->outer_type = TYPE_MAIN_VARIANT (TREE_TYPE (base));
2282 context->offset = offset;
2283 context->speculative_outer_type = NULL;
2284 context->speculative_offset = 0;
2285 context->speculative_maybe_derived_type = true;
2286 /* Make very conservative assumption that all objects
2287 may be in construction.
2288 TODO: ipa-prop already contains code to tell better.
2289 merge it later. */
2290 context->maybe_in_construction = true;
2291 context->maybe_derived_type = false;
2294 /* CST is an invariant (address of decl), try to get meaningful
2295 polymorphic call context for polymorphic call of method
2296 if instance of OTR_TYPE that is located at OFFSET of this invariant.
2297 Return FALSE if nothing meaningful can be found. */
2299 bool
2300 get_polymorphic_call_info_from_invariant (ipa_polymorphic_call_context *context,
2301 tree cst,
2302 tree otr_type,
2303 HOST_WIDE_INT offset)
2305 HOST_WIDE_INT offset2, size, max_size;
2306 tree base;
2308 if (TREE_CODE (cst) != ADDR_EXPR)
2309 return false;
2311 cst = TREE_OPERAND (cst, 0);
2312 base = get_ref_base_and_extent (cst, &offset2, &size, &max_size);
2313 if (!DECL_P (base) || max_size == -1 || max_size != size)
2314 return false;
2316 /* Only type inconsistent programs can have otr_type that is
2317 not part of outer type. */
2318 if (!contains_type_p (TREE_TYPE (base), offset, otr_type))
2319 return false;
2321 get_polymorphic_call_info_for_decl (context, base, offset);
2322 return true;
2325 /* See if OP is SSA name initialized as a copy or by single assignment.
2326 If so, walk the SSA graph up. */
2328 static tree
2329 walk_ssa_copies (tree op)
2331 STRIP_NOPS (op);
2332 while (TREE_CODE (op) == SSA_NAME
2333 && !SSA_NAME_IS_DEFAULT_DEF (op)
2334 && SSA_NAME_DEF_STMT (op)
2335 && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
2337 if (gimple_assign_load_p (SSA_NAME_DEF_STMT (op)))
2338 return op;
2339 op = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op));
2340 STRIP_NOPS (op);
2342 return op;
2345 /* Given REF call in FNDECL, determine class of the polymorphic
2346 call (OTR_TYPE), its token (OTR_TOKEN) and CONTEXT.
2347 CALL is optional argument giving the actual statement (usually call) where
2348 the context is used.
2349 Return pointer to object described by the context or an declaration if
2350 we found the instance to be stored in the static storage. */
2352 tree
2353 get_polymorphic_call_info (tree fndecl,
2354 tree ref,
2355 tree *otr_type,
2356 HOST_WIDE_INT *otr_token,
2357 ipa_polymorphic_call_context *context,
2358 gimple call)
2360 tree base_pointer;
2361 *otr_type = obj_type_ref_class (ref);
2362 *otr_token = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref));
2364 /* Set up basic info in case we find nothing interesting in the analysis. */
2365 context->speculative_outer_type = NULL;
2366 context->speculative_offset = 0;
2367 context->speculative_maybe_derived_type = true;
2368 context->outer_type = TYPE_MAIN_VARIANT (*otr_type);
2369 context->offset = 0;
2370 base_pointer = OBJ_TYPE_REF_OBJECT (ref);
2371 context->maybe_derived_type = true;
2372 context->maybe_in_construction = true;
2374 /* Walk SSA for outer object. */
2377 base_pointer = walk_ssa_copies (base_pointer);
2378 if (TREE_CODE (base_pointer) == ADDR_EXPR)
2380 HOST_WIDE_INT size, max_size;
2381 HOST_WIDE_INT offset2;
2382 tree base = get_ref_base_and_extent (TREE_OPERAND (base_pointer, 0),
2383 &offset2, &size, &max_size);
2385 /* If this is a varying address, punt. */
2386 if ((TREE_CODE (base) == MEM_REF || DECL_P (base))
2387 && max_size != -1
2388 && max_size == size)
2390 /* We found dereference of a pointer. Type of the pointer
2391 and MEM_REF is meaningless, but we can look futher. */
2392 if (TREE_CODE (base) == MEM_REF)
2394 base_pointer = TREE_OPERAND (base, 0);
2395 context->offset
2396 += offset2 + mem_ref_offset (base).to_short_addr () * BITS_PER_UNIT;
2397 context->outer_type = NULL;
2399 /* We found base object. In this case the outer_type
2400 is known. */
2401 else if (DECL_P (base))
2403 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (base)));
2405 /* Only type inconsistent programs can have otr_type that is
2406 not part of outer type. */
2407 if (!contains_type_p (TREE_TYPE (base),
2408 context->offset + offset2, *otr_type))
2410 /* Use OTR_TOKEN = INT_MAX as a marker of probably type inconsistent
2411 code sequences; we arrange the calls to be builtin_unreachable
2412 later. */
2413 *otr_token = INT_MAX;
2414 return base_pointer;
2416 get_polymorphic_call_info_for_decl (context, base,
2417 context->offset + offset2);
2418 if (context->maybe_in_construction && call)
2419 context->maybe_in_construction
2420 = decl_maybe_in_construction_p (base,
2421 context->outer_type,
2422 call,
2423 fndecl);
2424 return base;
2426 else
2427 break;
2429 else
2430 break;
2432 else if (TREE_CODE (base_pointer) == POINTER_PLUS_EXPR
2433 && tree_fits_uhwi_p (TREE_OPERAND (base_pointer, 1)))
2435 context->offset += tree_to_shwi (TREE_OPERAND (base_pointer, 1))
2436 * BITS_PER_UNIT;
2437 base_pointer = TREE_OPERAND (base_pointer, 0);
2439 else
2440 break;
2442 while (true);
2444 /* Try to determine type of the outer object. */
2445 if (TREE_CODE (base_pointer) == SSA_NAME
2446 && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
2447 && TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL)
2449 /* See if parameter is THIS pointer of a method. */
2450 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE
2451 && SSA_NAME_VAR (base_pointer) == DECL_ARGUMENTS (fndecl))
2453 context->outer_type
2454 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
2455 gcc_assert (TREE_CODE (context->outer_type) == RECORD_TYPE);
2457 /* Dynamic casting has possibly upcasted the type
2458 in the hiearchy. In this case outer type is less
2459 informative than inner type and we should forget
2460 about it. */
2461 if (!contains_type_p (context->outer_type, context->offset,
2462 *otr_type))
2464 context->outer_type = NULL;
2465 return base_pointer;
2468 /* If the function is constructor or destructor, then
2469 the type is possibly in construction, but we know
2470 it is not derived type. */
2471 if (DECL_CXX_CONSTRUCTOR_P (fndecl)
2472 || DECL_CXX_DESTRUCTOR_P (fndecl))
2474 context->maybe_in_construction = true;
2475 context->maybe_derived_type = false;
2477 else
2479 context->maybe_derived_type = true;
2480 context->maybe_in_construction = false;
2482 return base_pointer;
2484 /* Non-PODs passed by value are really passed by invisible
2485 reference. In this case we also know the type of the
2486 object. */
2487 if (DECL_BY_REFERENCE (SSA_NAME_VAR (base_pointer)))
2489 context->outer_type
2490 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
2491 gcc_assert (!POINTER_TYPE_P (context->outer_type));
2492 /* Only type inconsistent programs can have otr_type that is
2493 not part of outer type. */
2494 if (!contains_type_p (context->outer_type, context->offset,
2495 *otr_type))
2497 /* Use OTR_TOKEN = INT_MAX as a marker of probably type inconsistent
2498 code sequences; we arrange the calls to be builtin_unreachable
2499 later. */
2500 *otr_token = INT_MAX;
2501 return base_pointer;
2503 context->maybe_derived_type = false;
2504 context->maybe_in_construction = false;
2505 return base_pointer;
2509 tree base_type = TREE_TYPE (base_pointer);
2511 if (TREE_CODE (base_pointer) == SSA_NAME
2512 && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
2513 && TREE_CODE (SSA_NAME_VAR (base_pointer)) != PARM_DECL)
2515 /* Use OTR_TOKEN = INT_MAX as a marker of probably type inconsistent
2516 code sequences; we arrange the calls to be builtin_unreachable
2517 later. */
2518 *otr_token = INT_MAX;
2519 return base_pointer;
2521 if (TREE_CODE (base_pointer) == SSA_NAME
2522 && SSA_NAME_DEF_STMT (base_pointer)
2523 && gimple_assign_single_p (SSA_NAME_DEF_STMT (base_pointer)))
2524 base_type = TREE_TYPE (gimple_assign_rhs1
2525 (SSA_NAME_DEF_STMT (base_pointer)));
2527 if (POINTER_TYPE_P (base_type)
2528 && contains_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (base_type)),
2529 context->offset,
2530 *otr_type))
2532 context->speculative_outer_type = TYPE_MAIN_VARIANT
2533 (TREE_TYPE (base_type));
2534 context->speculative_offset = context->offset;
2535 context->speculative_maybe_derived_type = true;
2537 /* TODO: There are multiple ways to derive a type. For instance
2538 if BASE_POINTER is passed to an constructor call prior our refernece.
2539 We do not make this type of flow sensitive analysis yet. */
2540 return base_pointer;
2543 /* Structure to be passed in between detect_type_change and
2544 check_stmt_for_type_change. */
2546 struct type_change_info
2548 /* Offset into the object where there is the virtual method pointer we are
2549 looking for. */
2550 HOST_WIDE_INT offset;
2551 /* The declaration or SSA_NAME pointer of the base that we are checking for
2552 type change. */
2553 tree instance;
2554 /* The reference to virtual table pointer used. */
2555 tree vtbl_ptr_ref;
2556 tree otr_type;
2557 /* If we actually can tell the type that the object has changed to, it is
2558 stored in this field. Otherwise it remains NULL_TREE. */
2559 tree known_current_type;
2560 HOST_WIDE_INT known_current_offset;
2562 /* Set to true if dynamic type change has been detected. */
2563 bool type_maybe_changed;
2564 /* Set to true if multiple types have been encountered. known_current_type
2565 must be disregarded in that case. */
2566 bool multiple_types_encountered;
2567 /* Set to true if we possibly missed some dynamic type changes and we should
2568 consider the set to be speculative. */
2569 bool speculative;
2570 bool seen_unanalyzed_store;
2573 /* Return true if STMT is not call and can modify a virtual method table pointer.
2574 We take advantage of fact that vtable stores must appear within constructor
2575 and destructor functions. */
2577 bool
2578 noncall_stmt_may_be_vtbl_ptr_store (gimple stmt)
2580 if (is_gimple_assign (stmt))
2582 tree lhs = gimple_assign_lhs (stmt);
2584 if (gimple_clobber_p (stmt))
2585 return false;
2586 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
2588 if (flag_strict_aliasing
2589 && !POINTER_TYPE_P (TREE_TYPE (lhs)))
2590 return false;
2592 if (TREE_CODE (lhs) == COMPONENT_REF
2593 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
2594 return false;
2595 /* In the future we might want to use get_base_ref_and_offset to find
2596 if there is a field corresponding to the offset and if so, proceed
2597 almost like if it was a component ref. */
2601 /* Code unification may mess with inline stacks. */
2602 if (cfun->after_inlining)
2603 return true;
2605 /* Walk the inline stack and watch out for ctors/dtors.
2606 TODO: Maybe we can require the store to appear in toplevel
2607 block of CTOR/DTOR. */
2608 for (tree block = gimple_block (stmt); block && TREE_CODE (block) == BLOCK;
2609 block = BLOCK_SUPERCONTEXT (block))
2610 if (BLOCK_ABSTRACT_ORIGIN (block)
2611 && TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block)) == FUNCTION_DECL)
2613 tree fn = BLOCK_ABSTRACT_ORIGIN (block);
2615 if (flags_from_decl_or_type (fn) & (ECF_PURE | ECF_CONST))
2616 return false;
2617 return (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
2618 && (DECL_CXX_CONSTRUCTOR_P (fn)
2619 || DECL_CXX_DESTRUCTOR_P (fn)));
2621 return (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE
2622 && (DECL_CXX_CONSTRUCTOR_P (current_function_decl)
2623 || DECL_CXX_DESTRUCTOR_P (current_function_decl)));
2626 /* If STMT can be proved to be an assignment to the virtual method table
2627 pointer of ANALYZED_OBJ and the type associated with the new table
2628 identified, return the type. Otherwise return NULL_TREE. */
2630 static tree
2631 extr_type_from_vtbl_ptr_store (gimple stmt, struct type_change_info *tci,
2632 HOST_WIDE_INT *type_offset)
2634 HOST_WIDE_INT offset, size, max_size;
2635 tree lhs, rhs, base;
2637 if (!gimple_assign_single_p (stmt))
2638 return NULL_TREE;
2640 lhs = gimple_assign_lhs (stmt);
2641 rhs = gimple_assign_rhs1 (stmt);
2642 if (TREE_CODE (lhs) != COMPONENT_REF
2643 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
2645 if (dump_file)
2646 fprintf (dump_file, " LHS is not virtual table.\n");
2647 return NULL_TREE;
2650 if (tci->vtbl_ptr_ref && operand_equal_p (lhs, tci->vtbl_ptr_ref, 0))
2652 else
2654 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
2655 if (offset != tci->offset
2656 || size != POINTER_SIZE
2657 || max_size != POINTER_SIZE)
2659 if (dump_file)
2660 fprintf (dump_file, " wrong offset %i!=%i or size %i\n",
2661 (int)offset, (int)tci->offset, (int)size);
2662 return NULL_TREE;
2664 if (DECL_P (tci->instance))
2666 if (base != tci->instance)
2668 if (dump_file)
2670 fprintf (dump_file, " base:");
2671 print_generic_expr (dump_file, base, TDF_SLIM);
2672 fprintf (dump_file, " does not match instance:");
2673 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
2674 fprintf (dump_file, "\n");
2676 return NULL_TREE;
2679 else if (TREE_CODE (base) == MEM_REF)
2681 if (!operand_equal_p (tci->instance, TREE_OPERAND (base, 0), 0)
2682 || !integer_zerop (TREE_OPERAND (base, 1)))
2684 if (dump_file)
2686 fprintf (dump_file, " base mem ref:");
2687 print_generic_expr (dump_file, base, TDF_SLIM);
2688 fprintf (dump_file, " has nonzero offset or does not match instance:");
2689 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
2690 fprintf (dump_file, "\n");
2692 return NULL_TREE;
2695 else if (!operand_equal_p (tci->instance, base, 0)
2696 || tci->offset)
2698 if (dump_file)
2700 fprintf (dump_file, " base:");
2701 print_generic_expr (dump_file, base, TDF_SLIM);
2702 fprintf (dump_file, " does not match instance:");
2703 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
2704 fprintf (dump_file, " with offset %i\n", (int)tci->offset);
2706 return NULL_TREE;
2710 tree vtable;
2711 unsigned HOST_WIDE_INT offset2;
2713 if (!vtable_pointer_value_to_vtable (rhs, &vtable, &offset2))
2715 if (dump_file)
2716 fprintf (dump_file, " Failed to lookup binfo\n");
2717 return NULL;
2720 tree binfo = subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
2721 offset2, vtable);
2722 if (!binfo)
2724 if (dump_file)
2725 fprintf (dump_file, " Construction vtable used\n");
2726 /* FIXME: We should suport construction contextes. */
2727 return NULL;
2730 *type_offset = tree_to_shwi (BINFO_OFFSET (binfo)) * BITS_PER_UNIT;
2731 return DECL_CONTEXT (vtable);
2734 /* Record dynamic type change of TCI to TYPE. */
2736 void
2737 record_known_type (struct type_change_info *tci, tree type, HOST_WIDE_INT offset)
2739 if (dump_file)
2741 if (type)
2743 fprintf (dump_file, " Recording type: ");
2744 print_generic_expr (dump_file, type, TDF_SLIM);
2745 fprintf (dump_file, " at offset %i\n", (int)offset);
2747 else
2748 fprintf (dump_file, " Recording unknown type\n");
2751 /* If we found a constructor of type that is not polymorphic or
2752 that may contain the type in question as a field (not as base),
2753 restrict to the inner class first to make type matching bellow
2754 happier. */
2755 if (type
2756 && (offset
2757 || (TREE_CODE (type) != RECORD_TYPE
2758 || !polymorphic_type_binfo_p (TYPE_BINFO (type)))))
2760 ipa_polymorphic_call_context context;
2762 context.offset = offset;
2763 context.outer_type = type;
2764 context.maybe_in_construction = false;
2765 context.maybe_derived_type = false;
2766 /* If we failed to find the inner type, we know that the call
2767 would be undefined for type produced here. */
2768 if (!context.restrict_to_inner_class (tci->otr_type))
2770 if (dump_file)
2771 fprintf (dump_file, " Ignoring; does not contain otr_type\n");
2772 return;
2774 /* Watch for case we reached an POD type and anticipate placement
2775 new. */
2776 if (!context.maybe_derived_type)
2778 type = context.outer_type;
2779 offset = context.offset;
2782 if (tci->type_maybe_changed
2783 && (!types_same_for_odr (type, tci->known_current_type)
2784 || offset != tci->known_current_offset))
2785 tci->multiple_types_encountered = true;
2786 tci->known_current_type = TYPE_MAIN_VARIANT (type);
2787 tci->known_current_offset = offset;
2788 tci->type_maybe_changed = true;
2791 /* Callback of walk_aliased_vdefs and a helper function for
2792 detect_type_change to check whether a particular statement may modify
2793 the virtual table pointer, and if possible also determine the new type of
2794 the (sub-)object. It stores its result into DATA, which points to a
2795 type_change_info structure. */
2797 static bool
2798 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
2800 gimple stmt = SSA_NAME_DEF_STMT (vdef);
2801 struct type_change_info *tci = (struct type_change_info *) data;
2802 tree fn;
2804 /* If we already gave up, just terminate the rest of walk. */
2805 if (tci->multiple_types_encountered)
2806 return true;
2808 if (is_gimple_call (stmt))
2810 if (gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE))
2811 return false;
2813 /* Check for a constructor call. */
2814 if ((fn = gimple_call_fndecl (stmt)) != NULL_TREE
2815 && DECL_CXX_CONSTRUCTOR_P (fn)
2816 && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
2817 && gimple_call_num_args (stmt))
2819 tree op = walk_ssa_copies (gimple_call_arg (stmt, 0));
2820 tree type = method_class_type (TREE_TYPE (fn));
2821 HOST_WIDE_INT offset = 0, size, max_size;
2823 if (dump_file)
2825 fprintf (dump_file, " Checking constructor call: ");
2826 print_gimple_stmt (dump_file, stmt, 0, 0);
2829 /* See if THIS parameter seems like instance pointer. */
2830 if (TREE_CODE (op) == ADDR_EXPR)
2832 op = get_ref_base_and_extent (TREE_OPERAND (op, 0),
2833 &offset, &size, &max_size);
2834 if (size != max_size || max_size == -1)
2836 tci->speculative = true;
2837 return false;
2839 if (op && TREE_CODE (op) == MEM_REF)
2841 if (!tree_fits_shwi_p (TREE_OPERAND (op, 1)))
2843 tci->speculative = true;
2844 return false;
2846 offset += tree_to_shwi (TREE_OPERAND (op, 1))
2847 * BITS_PER_UNIT;
2848 op = TREE_OPERAND (op, 0);
2850 else if (DECL_P (op))
2852 else
2854 tci->speculative = true;
2855 return false;
2857 op = walk_ssa_copies (op);
2859 if (operand_equal_p (op, tci->instance, 0)
2860 && TYPE_SIZE (type)
2861 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
2862 && tree_fits_shwi_p (TYPE_SIZE (type))
2863 && tree_to_shwi (TYPE_SIZE (type)) + offset > tci->offset)
2865 record_known_type (tci, type, tci->offset - offset);
2866 return true;
2869 /* Calls may possibly change dynamic type by placement new. Assume
2870 it will not happen, but make result speculative only. */
2871 if (dump_file)
2873 fprintf (dump_file, " Function call may change dynamic type:");
2874 print_gimple_stmt (dump_file, stmt, 0, 0);
2876 tci->speculative = true;
2877 return false;
2879 /* Check for inlined virtual table store. */
2880 else if (noncall_stmt_may_be_vtbl_ptr_store (stmt))
2882 tree type;
2883 HOST_WIDE_INT offset = 0;
2884 if (dump_file)
2886 fprintf (dump_file, " Checking vtbl store: ");
2887 print_gimple_stmt (dump_file, stmt, 0, 0);
2890 type = extr_type_from_vtbl_ptr_store (stmt, tci, &offset);
2891 gcc_assert (!type || TYPE_MAIN_VARIANT (type) == type);
2892 if (!type)
2894 if (dump_file)
2895 fprintf (dump_file, " Unanalyzed store may change type.\n");
2896 tci->seen_unanalyzed_store = true;
2897 tci->speculative = true;
2899 else
2900 record_known_type (tci, type, offset);
2901 return true;
2903 else
2904 return false;
2907 /* THIS is polymorphic call context obtained from get_polymorphic_context.
2908 OTR_OBJECT is pointer to the instance returned by OBJ_TYPE_REF_OBJECT.
2909 INSTANCE is pointer to the outer instance as returned by
2910 get_polymorphic_context. To avoid creation of temporary expressions,
2911 INSTANCE may also be an declaration of get_polymorphic_context found the
2912 value to be in static storage.
2914 If the type of instance is not fully determined
2915 (either OUTER_TYPE is unknown or MAYBE_IN_CONSTRUCTION/INCLUDE_DERIVED_TYPES
2916 is set), try to walk memory writes and find the actual construction of the
2917 instance.
2919 We do not include this analysis in the context analysis itself, because
2920 it needs memory SSA to be fully built and the walk may be expensive.
2921 So it is not suitable for use withing fold_stmt and similar uses. */
2923 bool
2924 ipa_polymorphic_call_context::get_dynamic_type (tree instance,
2925 tree otr_object,
2926 tree otr_type,
2927 gimple call)
2929 struct type_change_info tci;
2930 ao_ref ao;
2931 bool function_entry_reached = false;
2932 tree instance_ref = NULL;
2933 gimple stmt = call;
2934 /* Remember OFFSET before it is modified by restrict_to_inner_class.
2935 This is because we do not update INSTANCE when walking inwards. */
2936 HOST_WIDE_INT instance_offset = offset;
2938 otr_type = TYPE_MAIN_VARIANT (otr_type);
2940 /* Walk into inner type. This may clear maybe_derived_type and save us
2941 from useless work. It also makes later comparsions with static type
2942 easier. */
2943 if (outer_type)
2945 if (!restrict_to_inner_class (otr_type))
2946 return false;
2949 if (!maybe_in_construction && !maybe_derived_type)
2950 return false;
2952 /* We need to obtain refernce to virtual table pointer. It is better
2953 to look it up in the code rather than build our own. This require bit
2954 of pattern matching, but we end up verifying that what we found is
2955 correct.
2957 What we pattern match is:
2959 tmp = instance->_vptr.A; // vtbl ptr load
2960 tmp2 = tmp[otr_token]; // vtable lookup
2961 OBJ_TYPE_REF(tmp2;instance->0) (instance);
2963 We want to start alias oracle walk from vtbl pointer load,
2964 but we may not be able to identify it, for example, when PRE moved the
2965 load around. */
2967 if (gimple_code (call) == GIMPLE_CALL)
2969 tree ref = gimple_call_fn (call);
2970 HOST_WIDE_INT offset2, size, max_size;
2972 if (TREE_CODE (ref) == OBJ_TYPE_REF)
2974 ref = OBJ_TYPE_REF_EXPR (ref);
2975 ref = walk_ssa_copies (ref);
2977 /* Check if definition looks like vtable lookup. */
2978 if (TREE_CODE (ref) == SSA_NAME
2979 && !SSA_NAME_IS_DEFAULT_DEF (ref)
2980 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref))
2981 && TREE_CODE (gimple_assign_rhs1
2982 (SSA_NAME_DEF_STMT (ref))) == MEM_REF)
2984 ref = get_base_address
2985 (TREE_OPERAND (gimple_assign_rhs1
2986 (SSA_NAME_DEF_STMT (ref)), 0));
2987 ref = walk_ssa_copies (ref);
2988 /* Find base address of the lookup and see if it looks like
2989 vptr load. */
2990 if (TREE_CODE (ref) == SSA_NAME
2991 && !SSA_NAME_IS_DEFAULT_DEF (ref)
2992 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref)))
2994 tree ref_exp = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (ref));
2995 tree base_ref = get_ref_base_and_extent
2996 (ref_exp, &offset2, &size, &max_size);
2998 /* Finally verify that what we found looks like read from OTR_OBJECT
2999 or from INSTANCE with offset OFFSET. */
3000 if (base_ref
3001 && ((TREE_CODE (base_ref) == MEM_REF
3002 && ((offset2 == instance_offset
3003 && TREE_OPERAND (base_ref, 0) == instance)
3004 || (!offset2 && TREE_OPERAND (base_ref, 0) == otr_object)))
3005 || (DECL_P (instance) && base_ref == instance
3006 && offset2 == instance_offset)))
3008 stmt = SSA_NAME_DEF_STMT (ref);
3009 instance_ref = ref_exp;
3016 /* If we failed to look up the refernece in code, build our own. */
3017 if (!instance_ref)
3019 /* If the statement in question does not use memory, we can't tell
3020 anything. */
3021 if (!gimple_vuse (stmt))
3022 return false;
3023 ao_ref_init_from_ptr_and_size (&ao, otr_object, NULL);
3025 else
3026 /* Otherwise use the real reference. */
3027 ao_ref_init (&ao, instance_ref);
3029 /* We look for vtbl pointer read. */
3030 ao.size = POINTER_SIZE;
3031 ao.max_size = ao.size;
3032 ao.ref_alias_set
3033 = get_deref_alias_set (TREE_TYPE (BINFO_VTABLE (TYPE_BINFO (otr_type))));
3035 if (dump_file)
3037 fprintf (dump_file, "Determining dynamic type for call: ");
3038 print_gimple_stmt (dump_file, call, 0, 0);
3039 fprintf (dump_file, " Starting walk at: ");
3040 print_gimple_stmt (dump_file, stmt, 0, 0);
3041 fprintf (dump_file, " instance pointer: ");
3042 print_generic_expr (dump_file, otr_object, TDF_SLIM);
3043 fprintf (dump_file, " Outer instance pointer: ");
3044 print_generic_expr (dump_file, instance, TDF_SLIM);
3045 fprintf (dump_file, " offset: %i (bits)", (int)offset);
3046 fprintf (dump_file, " vtbl reference: ");
3047 print_generic_expr (dump_file, instance_ref, TDF_SLIM);
3048 fprintf (dump_file, "\n");
3051 tci.offset = offset;
3052 tci.instance = instance;
3053 tci.vtbl_ptr_ref = instance_ref;
3054 gcc_assert (TREE_CODE (instance) != MEM_REF);
3055 tci.known_current_type = NULL_TREE;
3056 tci.known_current_offset = 0;
3057 tci.otr_type = otr_type;
3058 tci.type_maybe_changed = false;
3059 tci.multiple_types_encountered = false;
3060 tci.speculative = false;
3061 tci.seen_unanalyzed_store = false;
3063 walk_aliased_vdefs (&ao, gimple_vuse (stmt), check_stmt_for_type_change,
3064 &tci, NULL, &function_entry_reached);
3066 /* If we did not find any type changing statements, we may still drop
3067 maybe_in_construction flag if the context already have outer type.
3069 Here we make special assumptions about both constructors and
3070 destructors which are all the functions that are allowed to alter the
3071 VMT pointers. It assumes that destructors begin with assignment into
3072 all VMT pointers and that constructors essentially look in the
3073 following way:
3075 1) The very first thing they do is that they call constructors of
3076 ancestor sub-objects that have them.
3078 2) Then VMT pointers of this and all its ancestors is set to new
3079 values corresponding to the type corresponding to the constructor.
3081 3) Only afterwards, other stuff such as constructor of member
3082 sub-objects and the code written by the user is run. Only this may
3083 include calling virtual functions, directly or indirectly.
3085 4) placement new can not be used to change type of non-POD statically
3086 allocated variables.
3088 There is no way to call a constructor of an ancestor sub-object in any
3089 other way.
3091 This means that we do not have to care whether constructors get the
3092 correct type information because they will always change it (in fact,
3093 if we define the type to be given by the VMT pointer, it is undefined).
3095 The most important fact to derive from the above is that if, for some
3096 statement in the section 3, we try to detect whether the dynamic type
3097 has changed, we can safely ignore all calls as we examine the function
3098 body backwards until we reach statements in section 2 because these
3099 calls cannot be ancestor constructors or destructors (if the input is
3100 not bogus) and so do not change the dynamic type (this holds true only
3101 for automatically allocated objects but at the moment we devirtualize
3102 only these). We then must detect that statements in section 2 change
3103 the dynamic type and can try to derive the new type. That is enough
3104 and we can stop, we will never see the calls into constructors of
3105 sub-objects in this code.
3107 Therefore if the static outer type was found (outer_type)
3108 we can safely ignore tci.speculative that is set on calls and give up
3109 only if there was dyanmic type store that may affect given variable
3110 (seen_unanalyzed_store) */
3112 if (!tci.type_maybe_changed
3113 || (outer_type
3114 && !tci.seen_unanalyzed_store
3115 && !tci.multiple_types_encountered
3116 && offset == tci.offset
3117 && types_same_for_odr (tci.known_current_type,
3118 outer_type)))
3120 if (!outer_type || tci.seen_unanalyzed_store)
3121 return false;
3122 if (maybe_in_construction)
3123 maybe_in_construction = false;
3124 if (dump_file)
3125 fprintf (dump_file, " No dynamic type change found.\n");
3126 return true;
3129 if (tci.known_current_type
3130 && !function_entry_reached
3131 && !tci.multiple_types_encountered)
3133 if (!tci.speculative)
3135 outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
3136 offset = tci.known_current_offset;
3137 maybe_in_construction = false;
3138 maybe_derived_type = false;
3139 if (dump_file)
3140 fprintf (dump_file, " Determined dynamic type.\n");
3142 else if (!speculative_outer_type
3143 || speculative_maybe_derived_type)
3145 speculative_outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
3146 speculative_offset = tci.known_current_offset;
3147 speculative_maybe_derived_type = false;
3148 if (dump_file)
3149 fprintf (dump_file, " Determined speculative dynamic type.\n");
3152 else if (dump_file)
3154 fprintf (dump_file, " Found multiple types%s%s\n",
3155 function_entry_reached ? " (function entry reached)" : "",
3156 function_entry_reached ? " (multiple types encountered)" : "");
3159 return true;
3162 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
3163 Lookup their respecitve virtual methods for OTR_TOKEN and OTR_TYPE
3164 and insert them to NODES.
3166 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
3168 static void
3169 record_targets_from_bases (tree otr_type,
3170 HOST_WIDE_INT otr_token,
3171 tree outer_type,
3172 HOST_WIDE_INT offset,
3173 vec <cgraph_node *> &nodes,
3174 hash_set<tree> *inserted,
3175 hash_set<tree> *matched_vtables,
3176 bool *completep)
3178 while (true)
3180 HOST_WIDE_INT pos, size;
3181 tree base_binfo;
3182 tree fld;
3184 if (types_same_for_odr (outer_type, otr_type))
3185 return;
3187 for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
3189 if (TREE_CODE (fld) != FIELD_DECL)
3190 continue;
3192 pos = int_bit_position (fld);
3193 size = tree_to_shwi (DECL_SIZE (fld));
3194 if (pos <= offset && (pos + size) > offset
3195 /* Do not get confused by zero sized bases. */
3196 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
3197 break;
3199 /* Within a class type we should always find correcponding fields. */
3200 gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
3202 /* Nonbasetypes should have been stripped by outer_class_type. */
3203 gcc_assert (DECL_ARTIFICIAL (fld));
3205 outer_type = TREE_TYPE (fld);
3206 offset -= pos;
3208 base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
3209 offset, otr_type);
3210 if (!base_binfo)
3212 gcc_assert (odr_violation_reported);
3213 return;
3215 gcc_assert (base_binfo);
3216 if (!matched_vtables->add (BINFO_VTABLE (base_binfo)))
3218 bool can_refer;
3219 tree target = gimple_get_virt_method_for_binfo (otr_token,
3220 base_binfo,
3221 &can_refer);
3222 if (!target || ! DECL_CXX_DESTRUCTOR_P (target))
3223 maybe_record_node (nodes, target, inserted, can_refer, completep);
3224 matched_vtables->add (BINFO_VTABLE (base_binfo));
3229 /* When virtual table is removed, we may need to flush the cache. */
3231 static void
3232 devirt_variable_node_removal_hook (varpool_node *n,
3233 void *d ATTRIBUTE_UNUSED)
3235 if (cached_polymorphic_call_targets
3236 && DECL_VIRTUAL_P (n->decl)
3237 && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
3238 free_polymorphic_call_targets_hash ();
3241 /* Record about how many calls would benefit from given type to be final. */
3243 struct odr_type_warn_count
3245 tree type;
3246 int count;
3247 gcov_type dyn_count;
3250 /* Record about how many calls would benefit from given method to be final. */
3252 struct decl_warn_count
3254 tree decl;
3255 int count;
3256 gcov_type dyn_count;
3259 /* Information about type and decl warnings. */
3261 struct final_warning_record
3263 gcov_type dyn_count;
3264 vec<odr_type_warn_count> type_warnings;
3265 hash_map<tree, decl_warn_count> decl_warnings;
3267 struct final_warning_record *final_warning_records;
3269 /* Return vector containing possible targets of polymorphic call of type
3270 OTR_TYPE caling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
3271 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containig
3272 OTR_TYPE and include their virtual method. This is useful for types
3273 possibly in construction or destruction where the virtual table may
3274 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
3275 us to walk the inheritance graph for all derivations.
3277 OTR_TOKEN == INT_MAX is used to mark calls that are provably
3278 undefined and should be redirected to unreachable.
3280 If COMPLETEP is non-NULL, store true if the list is complete.
3281 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
3282 in the target cache. If user needs to visit every target list
3283 just once, it can memoize them.
3285 SPECULATION_TARGETS specify number of targets that are speculatively
3286 likely. These include targets specified by the speculative part
3287 of polymoprhic call context and also exclude all targets for classes
3288 in construction.
3290 Returned vector is placed into cache. It is NOT caller's responsibility
3291 to free it. The vector can be freed on cgraph_remove_node call if
3292 the particular node is a virtual function present in the cache. */
3294 vec <cgraph_node *>
3295 possible_polymorphic_call_targets (tree otr_type,
3296 HOST_WIDE_INT otr_token,
3297 ipa_polymorphic_call_context context,
3298 bool *completep,
3299 void **cache_token,
3300 int *speculative_targetsp)
3302 static struct cgraph_node_hook_list *node_removal_hook_holder;
3303 vec <cgraph_node *> nodes = vNULL;
3304 vec <tree> bases_to_consider = vNULL;
3305 odr_type type, outer_type;
3306 polymorphic_call_target_d key;
3307 polymorphic_call_target_d **slot;
3308 unsigned int i;
3309 tree binfo, target;
3310 bool complete;
3311 bool can_refer;
3312 bool skipped = false;
3314 otr_type = TYPE_MAIN_VARIANT (otr_type);
3316 /* If ODR is not initialized, return empty incomplete list. */
3317 if (!odr_hash)
3319 if (completep)
3320 *completep = false;
3321 if (cache_token)
3322 *cache_token = NULL;
3323 if (speculative_targetsp)
3324 *speculative_targetsp = 0;
3325 return nodes;
3328 /* If we hit type inconsistency, just return empty list of targets. */
3329 if (otr_token == INT_MAX)
3331 if (completep)
3332 *completep = true;
3333 if (cache_token)
3334 *cache_token = NULL;
3335 if (speculative_targetsp)
3336 *speculative_targetsp = 0;
3337 return nodes;
3340 /* Do not bother to compute speculative info when user do not asks for it. */
3341 if (!speculative_targetsp || !context.speculative_outer_type)
3342 context.clear_speculation ();
3344 type = get_odr_type (otr_type, true);
3346 /* Recording type variants would wast results cache. */
3347 gcc_assert (!context.outer_type
3348 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3350 /* Lookup the outer class type we want to walk. */
3351 if ((context.outer_type || context.speculative_outer_type)
3352 && !context.restrict_to_inner_class (otr_type))
3354 if (completep)
3355 *completep = false;
3356 if (cache_token)
3357 *cache_token = NULL;
3358 if (speculative_targetsp)
3359 *speculative_targetsp = 0;
3360 return nodes;
3363 /* Check that restrict_to_inner_class kept the main variant. */
3364 gcc_assert (!context.outer_type
3365 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3367 /* We canonicalize our query, so we do not need extra hashtable entries. */
3369 /* Without outer type, we have no use for offset. Just do the
3370 basic search from innter type */
3371 if (!context.outer_type)
3373 context.outer_type = otr_type;
3374 context.offset = 0;
3376 /* We need to update our hiearchy if the type does not exist. */
3377 outer_type = get_odr_type (context.outer_type, true);
3378 /* If the type is complete, there are no derivations. */
3379 if (TYPE_FINAL_P (outer_type->type))
3380 context.maybe_derived_type = false;
3382 /* Initialize query cache. */
3383 if (!cached_polymorphic_call_targets)
3385 cached_polymorphic_call_targets = new hash_set<cgraph_node *>;
3386 polymorphic_call_target_hash
3387 = new polymorphic_call_target_hash_type (23);
3388 if (!node_removal_hook_holder)
3390 node_removal_hook_holder =
3391 symtab->add_cgraph_removal_hook (&devirt_node_removal_hook, NULL);
3392 symtab->add_varpool_removal_hook (&devirt_variable_node_removal_hook,
3393 NULL);
3397 /* Lookup cached answer. */
3398 key.type = type;
3399 key.otr_token = otr_token;
3400 key.context = context;
3401 slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
3402 if (cache_token)
3403 *cache_token = (void *)*slot;
3404 if (*slot)
3406 if (completep)
3407 *completep = (*slot)->complete;
3408 if (speculative_targetsp)
3409 *speculative_targetsp = (*slot)->speculative_targets;
3410 if ((*slot)->type_warning && final_warning_records)
3412 final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
3413 final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
3414 += final_warning_records->dyn_count;
3416 if ((*slot)->decl_warning && final_warning_records)
3418 struct decl_warn_count *c =
3419 final_warning_records->decl_warnings.get ((*slot)->decl_warning);
3420 c->count++;
3421 c->dyn_count += final_warning_records->dyn_count;
3423 return (*slot)->targets;
3426 complete = true;
3428 /* Do actual search. */
3429 timevar_push (TV_IPA_VIRTUAL_CALL);
3430 *slot = XCNEW (polymorphic_call_target_d);
3431 if (cache_token)
3432 *cache_token = (void *)*slot;
3433 (*slot)->type = type;
3434 (*slot)->otr_token = otr_token;
3435 (*slot)->context = context;
3436 (*slot)->speculative_targets = 0;
3438 hash_set<tree> inserted;
3439 hash_set<tree> matched_vtables;
3441 /* First insert targets we speculatively identified as likely. */
3442 if (context.speculative_outer_type)
3444 odr_type speculative_outer_type;
3445 bool speculation_complete = true;
3447 /* First insert target from type itself and check if it may have derived types. */
3448 speculative_outer_type = get_odr_type (context.speculative_outer_type, true);
3449 if (TYPE_FINAL_P (speculative_outer_type->type))
3450 context.speculative_maybe_derived_type = false;
3451 binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
3452 context.speculative_offset, otr_type);
3453 if (binfo)
3454 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3455 &can_refer);
3456 else
3457 target = NULL;
3459 /* In the case we get complete method, we don't need
3460 to walk derivations. */
3461 if (target && DECL_FINAL_P (target))
3462 context.speculative_maybe_derived_type = false;
3463 if (type_possibly_instantiated_p (speculative_outer_type->type))
3464 maybe_record_node (nodes, target, &inserted, can_refer, &speculation_complete);
3465 if (binfo)
3466 matched_vtables.add (BINFO_VTABLE (binfo));
3469 /* Next walk recursively all derived types. */
3470 if (context.speculative_maybe_derived_type)
3471 for (i = 0; i < speculative_outer_type->derived_types.length(); i++)
3472 possible_polymorphic_call_targets_1 (nodes, &inserted,
3473 &matched_vtables,
3474 otr_type,
3475 speculative_outer_type->derived_types[i],
3476 otr_token, speculative_outer_type->type,
3477 context.speculative_offset,
3478 &speculation_complete,
3479 bases_to_consider,
3480 false);
3481 (*slot)->speculative_targets = nodes.length();
3484 /* First see virtual method of type itself. */
3485 binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
3486 context.offset, otr_type);
3487 if (binfo)
3488 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3489 &can_refer);
3490 else
3492 gcc_assert (odr_violation_reported);
3493 target = NULL;
3496 /* Destructors are never called through construction virtual tables,
3497 because the type is always known. */
3498 if (target && DECL_CXX_DESTRUCTOR_P (target))
3499 context.maybe_in_construction = false;
3501 if (target)
3503 /* In the case we get complete method, we don't need
3504 to walk derivations. */
3505 if (DECL_FINAL_P (target))
3506 context.maybe_derived_type = false;
3509 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3510 if (type_possibly_instantiated_p (outer_type->type))
3511 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3512 else
3514 skipped = true;
3515 gcc_assert (in_lto_p || context.maybe_derived_type);
3518 if (binfo)
3519 matched_vtables.add (BINFO_VTABLE (binfo));
3521 /* Next walk recursively all derived types. */
3522 if (context.maybe_derived_type)
3524 for (i = 0; i < outer_type->derived_types.length(); i++)
3525 possible_polymorphic_call_targets_1 (nodes, &inserted,
3526 &matched_vtables,
3527 otr_type,
3528 outer_type->derived_types[i],
3529 otr_token, outer_type->type,
3530 context.offset, &complete,
3531 bases_to_consider,
3532 context.maybe_in_construction);
3534 if (!outer_type->all_derivations_known)
3536 if (final_warning_records)
3538 if (complete
3539 && nodes.length () == 1
3540 && warn_suggest_final_types
3541 && !outer_type->derived_types.length ())
3543 if (outer_type->id >= (int)final_warning_records->type_warnings.length ())
3544 final_warning_records->type_warnings.safe_grow_cleared
3545 (odr_types.length ());
3546 final_warning_records->type_warnings[outer_type->id].count++;
3547 final_warning_records->type_warnings[outer_type->id].dyn_count
3548 += final_warning_records->dyn_count;
3549 final_warning_records->type_warnings[outer_type->id].type
3550 = outer_type->type;
3551 (*slot)->type_warning = outer_type->id + 1;
3553 if (complete
3554 && warn_suggest_final_methods
3555 && nodes.length () == 1
3556 && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl),
3557 outer_type->type))
3559 bool existed;
3560 struct decl_warn_count &c =
3561 final_warning_records->decl_warnings.get_or_insert
3562 (nodes[0]->decl, &existed);
3564 if (existed)
3566 c.count++;
3567 c.dyn_count += final_warning_records->dyn_count;
3569 else
3571 c.count = 1;
3572 c.dyn_count = final_warning_records->dyn_count;
3573 c.decl = nodes[0]->decl;
3575 (*slot)->decl_warning = nodes[0]->decl;
3578 complete = false;
3582 /* Finally walk bases, if asked to. */
3583 if (!(*slot)->speculative_targets)
3584 (*slot)->speculative_targets = nodes.length();
3586 /* Destructors are never called through construction virtual tables,
3587 because the type is always known. One of entries may be cxa_pure_virtual
3588 so look to at least two of them. */
3589 if (context.maybe_in_construction)
3590 for (i =0 ; i < MIN (nodes.length (), 2); i++)
3591 if (DECL_CXX_DESTRUCTOR_P (nodes[i]->decl))
3592 context.maybe_in_construction = false;
3593 if (context.maybe_in_construction)
3595 if (type != outer_type
3596 && (!skipped
3597 || (context.maybe_derived_type
3598 && !type_all_derivations_known_p (outer_type->type))))
3599 record_targets_from_bases (otr_type, otr_token, outer_type->type,
3600 context.offset, nodes, &inserted,
3601 &matched_vtables, &complete);
3602 if (skipped)
3603 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3604 for (i = 0; i < bases_to_consider.length(); i++)
3605 maybe_record_node (nodes, bases_to_consider[i], &inserted, can_refer, &complete);
3607 bases_to_consider.release();
3609 (*slot)->targets = nodes;
3610 (*slot)->complete = complete;
3611 if (completep)
3612 *completep = complete;
3613 if (speculative_targetsp)
3614 *speculative_targetsp = (*slot)->speculative_targets;
3616 timevar_pop (TV_IPA_VIRTUAL_CALL);
3617 return nodes;
3620 bool
3621 add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value,
3622 vec<const decl_warn_count*> *vec)
3624 vec->safe_push (&value);
3625 return true;
3628 /* Dump all possible targets of a polymorphic call. */
3630 void
3631 dump_possible_polymorphic_call_targets (FILE *f,
3632 tree otr_type,
3633 HOST_WIDE_INT otr_token,
3634 const ipa_polymorphic_call_context &ctx)
3636 vec <cgraph_node *> targets;
3637 bool final;
3638 odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false);
3639 unsigned int i;
3640 int speculative;
3642 if (!type)
3643 return;
3644 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3645 ctx,
3646 &final, NULL, &speculative);
3647 fprintf (f, " Targets of polymorphic call of type %i:", type->id);
3648 print_generic_expr (f, type->type, TDF_SLIM);
3649 fprintf (f, " token %i\n", (int)otr_token);
3650 if (ctx.outer_type || ctx.offset)
3652 fprintf (f, " Contained in type:");
3653 print_generic_expr (f, ctx.outer_type, TDF_SLIM);
3654 fprintf (f, " at offset "HOST_WIDE_INT_PRINT_DEC"\n",
3655 ctx.offset);
3657 if (ctx.speculative_outer_type)
3659 fprintf (f, " Speculatively contained in type:");
3660 print_generic_expr (f, ctx.speculative_outer_type, TDF_SLIM);
3661 fprintf (f, " at offset "HOST_WIDE_INT_PRINT_DEC"\n",
3662 ctx.speculative_offset);
3665 fprintf (f, " %s%s%s%s\n ",
3666 final ? "This is a complete list." :
3667 "This is partial list; extra targets may be defined in other units.",
3668 ctx.maybe_in_construction ? " (base types included)" : "",
3669 ctx.maybe_derived_type ? " (derived types included)" : "",
3670 ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
3671 for (i = 0; i < targets.length (); i++)
3673 char *name = NULL;
3674 if (i == (unsigned)speculative)
3675 fprintf (f, "\n Targets that are not likely:\n"
3676 " ");
3677 if (in_lto_p)
3678 name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
3679 fprintf (f, " %s/%i", name ? name : targets[i]->name (), targets[i]->order);
3680 if (in_lto_p)
3681 free (name);
3682 if (!targets[i]->definition)
3683 fprintf (f, " (no definition%s)",
3684 DECL_DECLARED_INLINE_P (targets[i]->decl)
3685 ? " inline" : "");
3687 fprintf (f, "\n\n");
3691 /* Return true if N can be possibly target of a polymorphic call of
3692 OTR_TYPE/OTR_TOKEN. */
3694 bool
3695 possible_polymorphic_call_target_p (tree otr_type,
3696 HOST_WIDE_INT otr_token,
3697 const ipa_polymorphic_call_context &ctx,
3698 struct cgraph_node *n)
3700 vec <cgraph_node *> targets;
3701 unsigned int i;
3702 enum built_in_function fcode;
3703 bool final;
3705 if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
3706 && ((fcode = DECL_FUNCTION_CODE (n->decl))
3707 == BUILT_IN_UNREACHABLE
3708 || fcode == BUILT_IN_TRAP))
3709 return true;
3711 if (!odr_hash)
3712 return true;
3713 targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
3714 for (i = 0; i < targets.length (); i++)
3715 if (n->semantically_equivalent_p (targets[i]))
3716 return true;
3718 /* At a moment we allow middle end to dig out new external declarations
3719 as a targets of polymorphic calls. */
3720 if (!final && !n->definition)
3721 return true;
3722 return false;
3726 /* After callgraph construction new external nodes may appear.
3727 Add them into the graph. */
3729 void
3730 update_type_inheritance_graph (void)
3732 struct cgraph_node *n;
3734 if (!odr_hash)
3735 return;
3736 free_polymorphic_call_targets_hash ();
3737 timevar_push (TV_IPA_INHERITANCE);
3738 /* We reconstruct the graph starting from types of all methods seen in the
3739 the unit. */
3740 FOR_EACH_FUNCTION (n)
3741 if (DECL_VIRTUAL_P (n->decl)
3742 && !n->definition
3743 && n->real_symbol_p ())
3744 get_odr_type (method_class_type (TYPE_MAIN_VARIANT (TREE_TYPE (n->decl))),
3745 true);
3746 timevar_pop (TV_IPA_INHERITANCE);
3750 /* Return true if N looks like likely target of a polymorphic call.
3751 Rule out cxa_pure_virtual, noreturns, function declared cold and
3752 other obvious cases. */
3754 bool
3755 likely_target_p (struct cgraph_node *n)
3757 int flags;
3758 /* cxa_pure_virtual and similar things are not likely. */
3759 if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
3760 return false;
3761 flags = flags_from_decl_or_type (n->decl);
3762 if (flags & ECF_NORETURN)
3763 return false;
3764 if (lookup_attribute ("cold",
3765 DECL_ATTRIBUTES (n->decl)))
3766 return false;
3767 if (n->frequency < NODE_FREQUENCY_NORMAL)
3768 return false;
3769 /* If there are no virtual tables refering the target alive,
3770 the only way the target can be called is an instance comming from other
3771 compilation unit; speculative devirtualization is build around an
3772 assumption that won't happen. */
3773 if (!referenced_from_vtable_p (n))
3774 return false;
3775 return true;
3778 /* Compare type warning records P1 and P2 and chose one with larger count;
3779 helper for qsort. */
3782 type_warning_cmp (const void *p1, const void *p2)
3784 const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
3785 const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
3787 if (t1->dyn_count < t2->dyn_count)
3788 return 1;
3789 if (t1->dyn_count > t2->dyn_count)
3790 return -1;
3791 return t2->count - t1->count;
3794 /* Compare decl warning records P1 and P2 and chose one with larger count;
3795 helper for qsort. */
3798 decl_warning_cmp (const void *p1, const void *p2)
3800 const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
3801 const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
3803 if (t1->dyn_count < t2->dyn_count)
3804 return 1;
3805 if (t1->dyn_count > t2->dyn_count)
3806 return -1;
3807 return t2->count - t1->count;
3810 /* The ipa-devirt pass.
3811 When polymorphic call has only one likely target in the unit,
3812 turn it into speculative call. */
3814 static unsigned int
3815 ipa_devirt (void)
3817 struct cgraph_node *n;
3818 hash_set<void *> bad_call_targets;
3819 struct cgraph_edge *e;
3821 int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
3822 int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
3823 int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
3825 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3826 This is implemented by setting up final_warning_records that are updated
3827 by get_polymorphic_call_targets.
3828 We need to clear cache in this case to trigger recomputation of all
3829 entries. */
3830 if (warn_suggest_final_methods || warn_suggest_final_types)
3832 final_warning_records = new (final_warning_record);
3833 final_warning_records->type_warnings = vNULL;
3834 final_warning_records->type_warnings.safe_grow_cleared (odr_types.length ());
3835 free_polymorphic_call_targets_hash ();
3838 FOR_EACH_DEFINED_FUNCTION (n)
3840 bool update = false;
3841 if (dump_file && n->indirect_calls)
3842 fprintf (dump_file, "\n\nProcesing function %s/%i\n",
3843 n->name (), n->order);
3844 for (e = n->indirect_calls; e; e = e->next_callee)
3845 if (e->indirect_info->polymorphic)
3847 struct cgraph_node *likely_target = NULL;
3848 void *cache_token;
3849 bool final;
3850 int speculative_targets;
3852 if (final_warning_records)
3853 final_warning_records->dyn_count = e->count;
3855 vec <cgraph_node *>targets
3856 = possible_polymorphic_call_targets
3857 (e, &final, &cache_token, &speculative_targets);
3858 unsigned int i;
3860 if (dump_file)
3861 dump_possible_polymorphic_call_targets
3862 (dump_file, e);
3864 npolymorphic++;
3866 if (!flag_devirtualize_speculatively)
3867 continue;
3869 if (!e->maybe_hot_p ())
3871 if (dump_file)
3872 fprintf (dump_file, "Call is cold\n\n");
3873 ncold++;
3874 continue;
3876 if (e->speculative)
3878 if (dump_file)
3879 fprintf (dump_file, "Call is aready speculated\n\n");
3880 nspeculated++;
3882 /* When dumping see if we agree with speculation. */
3883 if (!dump_file)
3884 continue;
3886 if (bad_call_targets.contains (cache_token))
3888 if (dump_file)
3889 fprintf (dump_file, "Target list is known to be useless\n\n");
3890 nmultiple++;
3891 continue;
3893 for (i = 0; i < targets.length (); i++)
3894 if (likely_target_p (targets[i]))
3896 if (likely_target)
3898 if (i < (unsigned) speculative_targets)
3900 likely_target = NULL;
3901 if (dump_file)
3902 fprintf (dump_file, "More than one likely target\n\n");
3903 nmultiple++;
3905 break;
3907 likely_target = targets[i];
3909 if (!likely_target)
3911 bad_call_targets.add (cache_token);
3912 continue;
3914 /* This is reached only when dumping; check if we agree or disagree
3915 with the speculation. */
3916 if (e->speculative)
3918 struct cgraph_edge *e2;
3919 struct ipa_ref *ref;
3920 e->speculative_call_info (e2, e, ref);
3921 if (e2->callee->ultimate_alias_target ()
3922 == likely_target->ultimate_alias_target ())
3924 fprintf (dump_file, "We agree with speculation\n\n");
3925 nok++;
3927 else
3929 fprintf (dump_file, "We disagree with speculation\n\n");
3930 nwrong++;
3932 continue;
3934 if (!likely_target->definition)
3936 if (dump_file)
3937 fprintf (dump_file, "Target is not an definition\n\n");
3938 nnotdefined++;
3939 continue;
3941 /* Do not introduce new references to external symbols. While we
3942 can handle these just well, it is common for programs to
3943 incorrectly with headers defining methods they are linked
3944 with. */
3945 if (DECL_EXTERNAL (likely_target->decl))
3947 if (dump_file)
3948 fprintf (dump_file, "Target is external\n\n");
3949 nexternal++;
3950 continue;
3952 /* Don't use an implicitly-declared destructor (c++/58678). */
3953 struct cgraph_node *non_thunk_target
3954 = likely_target->function_symbol ();
3955 if (DECL_ARTIFICIAL (non_thunk_target->decl)
3956 && DECL_COMDAT (non_thunk_target->decl))
3958 if (dump_file)
3959 fprintf (dump_file, "Target is artificial\n\n");
3960 nartificial++;
3961 continue;
3963 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3964 && likely_target->can_be_discarded_p ())
3966 if (dump_file)
3967 fprintf (dump_file, "Target is overwritable\n\n");
3968 noverwritable++;
3969 continue;
3971 else if (dbg_cnt (devirt))
3973 if (dump_enabled_p ())
3975 location_t locus = gimple_location_safe (e->call_stmt);
3976 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
3977 "speculatively devirtualizing call in %s/%i to %s/%i\n",
3978 n->name (), n->order,
3979 likely_target->name (),
3980 likely_target->order);
3982 if (!likely_target->can_be_discarded_p ())
3984 cgraph_node *alias;
3985 alias = dyn_cast<cgraph_node *> (likely_target->noninterposable_alias ());
3986 if (alias)
3987 likely_target = alias;
3989 nconverted++;
3990 update = true;
3991 e->make_speculative
3992 (likely_target, e->count * 8 / 10, e->frequency * 8 / 10);
3995 if (update)
3996 inline_update_overall_summary (n);
3998 if (warn_suggest_final_methods || warn_suggest_final_types)
4000 if (warn_suggest_final_types)
4002 final_warning_records->type_warnings.qsort (type_warning_cmp);
4003 for (unsigned int i = 0;
4004 i < final_warning_records->type_warnings.length (); i++)
4005 if (final_warning_records->type_warnings[i].count)
4007 tree type = final_warning_records->type_warnings[i].type;
4008 warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
4009 OPT_Wsuggest_final_types,
4010 "Declaring type %qD final "
4011 "would enable devirtualization of %i calls",
4012 type,
4013 final_warning_records->type_warnings[i].count);
4017 if (warn_suggest_final_methods)
4019 vec<const decl_warn_count*> decl_warnings_vec = vNULL;
4021 final_warning_records->decl_warnings.traverse
4022 <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec);
4023 decl_warnings_vec.qsort (decl_warning_cmp);
4024 for (unsigned int i = 0; i < decl_warnings_vec.length (); i++)
4026 tree decl = decl_warnings_vec[i]->decl;
4027 int count = decl_warnings_vec[i]->count;
4029 if (DECL_CXX_DESTRUCTOR_P (decl))
4030 warning_at (DECL_SOURCE_LOCATION (decl),
4031 OPT_Wsuggest_final_methods,
4032 "Declaring virtual destructor of %qD final "
4033 "would enable devirtualization of %i calls",
4034 DECL_CONTEXT (decl), count);
4035 else
4036 warning_at (DECL_SOURCE_LOCATION (decl),
4037 OPT_Wsuggest_final_methods,
4038 "Declaring method %qD final "
4039 "would enable devirtualization of %i calls",
4040 decl, count);
4044 delete (final_warning_records);
4045 final_warning_records = 0;
4048 if (dump_file)
4049 fprintf (dump_file,
4050 "%i polymorphic calls, %i devirtualized,"
4051 " %i speculatively devirtualized, %i cold\n"
4052 "%i have multiple targets, %i overwritable,"
4053 " %i already speculated (%i agree, %i disagree),"
4054 " %i external, %i not defined, %i artificial\n",
4055 npolymorphic, ndevirtualized, nconverted, ncold,
4056 nmultiple, noverwritable, nspeculated, nok, nwrong,
4057 nexternal, nnotdefined, nartificial);
4058 return ndevirtualized ? TODO_remove_functions : 0;
4061 namespace {
4063 const pass_data pass_data_ipa_devirt =
4065 IPA_PASS, /* type */
4066 "devirt", /* name */
4067 OPTGROUP_NONE, /* optinfo_flags */
4068 TV_IPA_DEVIRT, /* tv_id */
4069 0, /* properties_required */
4070 0, /* properties_provided */
4071 0, /* properties_destroyed */
4072 0, /* todo_flags_start */
4073 ( TODO_dump_symtab ), /* todo_flags_finish */
4076 class pass_ipa_devirt : public ipa_opt_pass_d
4078 public:
4079 pass_ipa_devirt (gcc::context *ctxt)
4080 : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
4081 NULL, /* generate_summary */
4082 NULL, /* write_summary */
4083 NULL, /* read_summary */
4084 NULL, /* write_optimization_summary */
4085 NULL, /* read_optimization_summary */
4086 NULL, /* stmt_fixup */
4087 0, /* function_transform_todo_flags_start */
4088 NULL, /* function_transform */
4089 NULL) /* variable_transform */
4092 /* opt_pass methods: */
4093 virtual bool gate (function *)
4095 return (flag_devirtualize
4096 && (flag_devirtualize_speculatively
4097 || (warn_suggest_final_methods
4098 || warn_suggest_final_types))
4099 && optimize);
4102 virtual unsigned int execute (function *) { return ipa_devirt (); }
4104 }; // class pass_ipa_devirt
4106 } // anon namespace
4108 ipa_opt_pass_d *
4109 make_pass_ipa_devirt (gcc::context *ctxt)
4111 return new pass_ipa_devirt (ctxt);
4114 #include "gt-ipa-devirt.h"