Merge aosp-toolchain/gcc/gcc-4_9 changes.
[official-gcc.git] / gcc-4_9-mobile / gcc / ipa-devirt.c
blob017bdb7cd2bfbcd49280875b2fe54bacb9bb9bc3
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 "pointer-set.h"
119 #include "target.h"
120 #include "hash-table.h"
121 #include "tree-pretty-print.h"
122 #include "ipa-utils.h"
123 #include "tree-ssa-alias.h"
124 #include "internal-fn.h"
125 #include "gimple-fold.h"
126 #include "gimple-expr.h"
127 #include "gimple.h"
128 #include "ipa-inline.h"
129 #include "diagnostic.h"
130 #include "tree-dfa.h"
131 #include "demangle.h"
133 static bool odr_violation_reported = false;
135 /* Dummy polymorphic call context. */
137 const ipa_polymorphic_call_context ipa_dummy_polymorphic_call_context
138 = {0, NULL, false, true};
140 /* Pointer set of all call targets appearing in the cache. */
141 static pointer_set_t *cached_polymorphic_call_targets;
143 /* The node of type inheritance graph. For each type unique in
144 One Defintion Rule (ODR) sense, we produce one node linking all
145 main variants of types equivalent to it, bases and derived types. */
147 struct GTY(()) odr_type_d
149 /* leader type. */
150 tree type;
151 /* All bases. */
152 vec<odr_type> GTY((skip)) bases;
153 /* All derrived types with virtual methods seen in unit. */
154 vec<odr_type> GTY((skip)) derived_types;
156 /* All equivalent types, if more than one. */
157 vec<tree, va_gc> *types;
158 /* Set of all equivalent types, if NON-NULL. */
159 pointer_set_t * GTY((skip)) types_set;
161 /* Unique ID indexing the type in odr_types array. */
162 int id;
163 /* Is it in anonymous namespace? */
164 bool anonymous_namespace;
168 /* Return true if BINFO corresponds to a type with virtual methods.
170 Every type has several BINFOs. One is the BINFO associated by the type
171 while other represents bases of derived types. The BINFOs representing
172 bases do not have BINFO_VTABLE pointer set when this is the single
173 inheritance (because vtables are shared). Look up the BINFO of type
174 and check presence of its vtable. */
176 static inline bool
177 polymorphic_type_binfo_p (tree binfo)
179 /* See if BINFO's type has an virtual table associtated with it. */
180 tree type_binfo = TYPE_BINFO (BINFO_TYPE (binfo));
181 if (L_IPO_COMP_MODE && !type_binfo)
182 return false;
183 return BINFO_VTABLE (type_binfo);
186 /* One Definition Rule hashtable helpers. */
188 struct odr_hasher
190 typedef odr_type_d value_type;
191 typedef union tree_node compare_type;
192 static inline hashval_t hash (const value_type *);
193 static inline bool equal (const value_type *, const compare_type *);
194 static inline void remove (value_type *);
197 /* Produce hash based on type name. */
199 hashval_t
200 hash_type_name (tree t)
202 gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
204 /* If not in LTO, all main variants are unique, so we can do
205 pointer hash. */
206 if (!in_lto_p)
207 return htab_hash_pointer (t);
209 /* Anonymous types are unique. */
210 if (type_in_anonymous_namespace_p (t))
211 return htab_hash_pointer (t);
213 /* For polymorphic types, we can simply hash the virtual table. */
214 if (TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)))
216 tree v = BINFO_VTABLE (TYPE_BINFO (t));
217 hashval_t hash = 0;
219 if (TREE_CODE (v) == POINTER_PLUS_EXPR)
221 hash = TREE_INT_CST_LOW (TREE_OPERAND (v, 1));
222 v = TREE_OPERAND (TREE_OPERAND (v, 0), 0);
225 v = DECL_ASSEMBLER_NAME (v);
226 hash = iterative_hash_hashval_t (hash, htab_hash_pointer (v));
227 return hash;
230 /* Rest is not implemented yet. */
231 gcc_unreachable ();
234 /* Return the computed hashcode for ODR_TYPE. */
236 inline hashval_t
237 odr_hasher::hash (const value_type *odr_type)
239 return hash_type_name (odr_type->type);
242 /* Compare types T1 and T2 and return true if they are
243 equivalent. */
245 inline bool
246 odr_hasher::equal (const value_type *t1, const compare_type *ct2)
248 tree t2 = const_cast <tree> (ct2);
250 gcc_checking_assert (TYPE_MAIN_VARIANT (ct2) == ct2);
251 if (t1->type == t2)
252 return true;
253 if (!in_lto_p)
254 return false;
255 return types_same_for_odr (t1->type, t2);
258 /* Free ODR type V. */
260 inline void
261 odr_hasher::remove (value_type *v)
263 v->bases.release ();
264 v->derived_types.release ();
265 if (v->types_set)
266 pointer_set_destroy (v->types_set);
267 ggc_free (v);
270 /* ODR type hash used to lookup ODR type based on tree type node. */
272 typedef hash_table <odr_hasher> odr_hash_type;
273 static odr_hash_type odr_hash;
275 /* ODR types are also stored into ODR_TYPE vector to allow consistent
276 walking. Bases appear before derived types. Vector is garbage collected
277 so we won't end up visiting empty types. */
279 static GTY(()) vec <odr_type, va_gc> *odr_types_ptr;
280 #define odr_types (*odr_types_ptr)
282 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
283 from VAL->type. This may happen in LTO where tree merging did not merge
284 all variants of the same type. It may or may not mean the ODR violation.
285 Add it to the list of duplicates and warn on some violations. */
287 static void
288 add_type_duplicate (odr_type val, tree type)
290 if (!val->types_set)
291 val->types_set = pointer_set_create ();
293 /* See if this duplicate is new. */
294 if (!pointer_set_insert (val->types_set, type))
296 bool merge = true;
297 bool base_mismatch = false;
298 gcc_assert (in_lto_p);
299 vec_safe_push (val->types, type);
300 unsigned int i,j;
302 /* First we compare memory layout. */
303 if (!types_compatible_p (val->type, type))
305 merge = false;
306 odr_violation_reported = true;
307 if (BINFO_VTABLE (TYPE_BINFO (val->type))
308 && warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (type)), 0,
309 "type %qD violates one definition rule ",
310 type))
311 inform (DECL_SOURCE_LOCATION (TYPE_NAME (val->type)),
312 "a type with the same name but different layout is "
313 "defined in another translation unit");
314 if (cgraph_dump_file)
316 fprintf (cgraph_dump_file, "ODR violation or merging or ODR type bug?\n");
318 print_node (cgraph_dump_file, "", val->type, 0);
319 putc ('\n',cgraph_dump_file);
320 print_node (cgraph_dump_file, "", type, 0);
321 putc ('\n',cgraph_dump_file);
325 /* Next sanity check that bases are the same. If not, we will end
326 up producing wrong answers. */
327 for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
328 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (TYPE_BINFO (type), i)))
330 odr_type base = get_odr_type
331 (BINFO_TYPE
332 (BINFO_BASE_BINFO (TYPE_BINFO (type),
333 i)),
334 true);
335 if (val->bases.length () <= j || val->bases[j] != base)
336 base_mismatch = true;
337 j++;
339 if (base_mismatch)
341 merge = false;
342 odr_violation_reported = true;
344 if (warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (type)), 0,
345 "type %qD violates one definition rule ",
346 type))
347 inform (DECL_SOURCE_LOCATION (TYPE_NAME (val->type)),
348 "a type with the same name but different bases is "
349 "defined in another translation unit");
350 if (cgraph_dump_file)
352 fprintf (cgraph_dump_file, "ODR bse violation or merging bug?\n");
354 print_node (cgraph_dump_file, "", val->type, 0);
355 putc ('\n',cgraph_dump_file);
356 print_node (cgraph_dump_file, "", type, 0);
357 putc ('\n',cgraph_dump_file);
361 /* Regularize things a little. During LTO same types may come with
362 different BINFOs. Either because their virtual table was
363 not merged by tree merging and only later at decl merging or
364 because one type comes with external vtable, while other
365 with internal. We want to merge equivalent binfos to conserve
366 memory and streaming overhead.
368 The external vtables are more harmful: they contain references
369 to external declarations of methods that may be defined in the
370 merged LTO unit. For this reason we absolutely need to remove
371 them and replace by internal variants. Not doing so will lead
372 to incomplete answers from possible_polymorphic_call_targets. */
373 if (!flag_ltrans && merge)
375 tree master_binfo = TYPE_BINFO (val->type);
376 tree v1 = BINFO_VTABLE (master_binfo);
377 tree v2 = BINFO_VTABLE (TYPE_BINFO (type));
379 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
381 gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR
382 && operand_equal_p (TREE_OPERAND (v1, 1),
383 TREE_OPERAND (v2, 1), 0));
384 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
385 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
387 gcc_assert (DECL_ASSEMBLER_NAME (v1)
388 == DECL_ASSEMBLER_NAME (v2));
390 if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2))
392 unsigned int i;
394 TYPE_BINFO (val->type) = TYPE_BINFO (type);
395 for (i = 0; i < val->types->length (); i++)
397 if (TYPE_BINFO ((*val->types)[i])
398 == master_binfo)
399 TYPE_BINFO ((*val->types)[i]) = TYPE_BINFO (type);
402 else
403 TYPE_BINFO (type) = master_binfo;
408 /* Get ODR type hash entry for TYPE. If INSERT is true, create
409 possibly new entry. */
411 odr_type
412 get_odr_type (tree type, bool insert)
414 odr_type_d **slot;
415 odr_type val;
416 hashval_t hash;
418 type = TYPE_MAIN_VARIANT (type);
419 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
420 hash = hash_type_name (type);
421 slot = odr_hash.find_slot_with_hash (type, hash, insert ? INSERT : NO_INSERT);
422 if (!slot)
423 return NULL;
425 /* See if we already have entry for type. */
426 if (*slot)
428 val = *slot;
430 /* With LTO we need to support multiple tree representation of
431 the same ODR type. */
432 if (val->type != type)
433 add_type_duplicate (val, type);
435 else
437 tree binfo = TYPE_BINFO (type);
438 unsigned int i;
440 val = ggc_alloc_cleared_odr_type_d ();
441 val->type = type;
442 val->bases = vNULL;
443 val->derived_types = vNULL;
444 val->anonymous_namespace = type_in_anonymous_namespace_p (type);
445 *slot = val;
446 for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
447 /* For now record only polymorphic types. other are
448 pointless for devirtualization and we can not precisely
449 determine ODR equivalency of these during LTO. */
450 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
452 odr_type base = get_odr_type (BINFO_TYPE (BINFO_BASE_BINFO (binfo,
453 i)),
454 true);
455 base->derived_types.safe_push (val);
456 val->bases.safe_push (base);
458 /* First record bases, then add into array so ids are increasing. */
459 if (odr_types_ptr)
460 val->id = odr_types.length ();
461 vec_safe_push (odr_types_ptr, val);
463 return val;
466 /* Dump ODR type T and all its derrived type. INDENT specify indentation for
467 recusive printing. */
469 static void
470 dump_odr_type (FILE *f, odr_type t, int indent=0)
472 unsigned int i;
473 fprintf (f, "%*s type %i: ", indent * 2, "", t->id);
474 print_generic_expr (f, t->type, TDF_SLIM);
475 fprintf (f, "%s\n", t->anonymous_namespace ? " (anonymous namespace)":"");
476 if (TYPE_NAME (t->type))
478 fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
479 DECL_SOURCE_FILE (TYPE_NAME (t->type)),
480 DECL_SOURCE_LINE (TYPE_NAME (t->type)));
482 if (t->bases.length ())
484 fprintf (f, "%*s base odr type ids: ", indent * 2, "");
485 for (i = 0; i < t->bases.length (); i++)
486 fprintf (f, " %i", t->bases[i]->id);
487 fprintf (f, "\n");
489 if (t->derived_types.length ())
491 fprintf (f, "%*s derived types:\n", indent * 2, "");
492 for (i = 0; i < t->derived_types.length (); i++)
493 dump_odr_type (f, t->derived_types[i], indent + 1);
495 fprintf (f, "\n");
498 /* Dump the type inheritance graph. */
500 static void
501 dump_type_inheritance_graph (FILE *f)
503 unsigned int i;
504 if (!odr_types_ptr)
505 return;
506 fprintf (f, "\n\nType inheritance graph:\n");
507 for (i = 0; i < odr_types.length (); i++)
509 if (odr_types[i]->bases.length () == 0)
510 dump_odr_type (f, odr_types[i]);
512 for (i = 0; i < odr_types.length (); i++)
514 if (odr_types[i]->types && odr_types[i]->types->length ())
516 unsigned int j;
517 fprintf (f, "Duplicate tree types for odr type %i\n", i);
518 print_node (f, "", odr_types[i]->type, 0);
519 for (j = 0; j < odr_types[i]->types->length (); j++)
521 tree t;
522 fprintf (f, "duplicate #%i\n", j);
523 print_node (f, "", (*odr_types[i]->types)[j], 0);
524 t = (*odr_types[i]->types)[j];
525 while (TYPE_P (t) && TYPE_CONTEXT (t))
527 t = TYPE_CONTEXT (t);
528 print_node (f, "", t, 0);
530 putc ('\n',f);
536 /* Given method type T, return type of class it belongs to.
537 Lookup this pointer and get its type. */
539 tree
540 method_class_type (tree t)
542 tree first_parm_type = TREE_VALUE (TYPE_ARG_TYPES (t));
543 gcc_assert (TREE_CODE (t) == METHOD_TYPE);
545 return TREE_TYPE (first_parm_type);
548 /* Initialize IPA devirt and build inheritance tree graph. */
550 void
551 build_type_inheritance_graph (void)
553 struct symtab_node *n;
554 FILE *inheritance_dump_file;
555 int flags;
557 if (odr_hash.is_created ())
558 return;
559 timevar_push (TV_IPA_INHERITANCE);
560 inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
561 odr_hash.create (23);
563 /* We reconstruct the graph starting of types of all methods seen in the
564 the unit. */
565 FOR_EACH_SYMBOL (n)
566 if (is_a <cgraph_node> (n)
567 && DECL_VIRTUAL_P (n->decl)
568 && symtab_real_symbol_p (n))
569 get_odr_type (method_class_type (TREE_TYPE (n->decl)), true);
571 /* Look also for virtual tables of types that do not define any methods.
573 We need it in a case where class B has virtual base of class A
574 re-defining its virtual method and there is class C with no virtual
575 methods with B as virtual base.
577 Here we output B's virtual method in two variant - for non-virtual
578 and virtual inheritance. B's virtual table has non-virtual version,
579 while C's has virtual.
581 For this reason we need to know about C in order to include both
582 variants of B. More correctly, record_target_from_binfo should
583 add both variants of the method when walking B, but we have no
584 link in between them.
586 We rely on fact that either the method is exported and thus we
587 assume it is called externally or C is in anonymous namespace and
588 thus we will see the vtable. */
590 else if (is_a <varpool_node> (n)
591 && DECL_VIRTUAL_P (n->decl)
592 && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE
593 && TYPE_BINFO (DECL_CONTEXT (n->decl))
594 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl))))
595 get_odr_type (DECL_CONTEXT (n->decl), true);
596 if (inheritance_dump_file)
598 dump_type_inheritance_graph (inheritance_dump_file);
599 dump_end (TDI_inheritance, inheritance_dump_file);
601 timevar_pop (TV_IPA_INHERITANCE);
604 /* If TARGET has associated node, record it in the NODES array.
605 CAN_REFER specify if program can refer to the target directly.
606 if TARGET is unknown (NULL) or it can not be inserted (for example because
607 its body was already removed and there is no way to refer to it), clear
608 COMPLETEP. */
610 static void
611 maybe_record_node (vec <cgraph_node *> &nodes,
612 tree target, pointer_set_t *inserted,
613 bool can_refer,
614 bool *completep)
616 struct cgraph_node *target_node;
617 enum built_in_function fcode;
619 if (!can_refer)
621 /* The only case when method of anonymous namespace becomes unreferable
622 is when we completely optimized it out. */
623 if (flag_ltrans
624 || !target
625 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
626 *completep = false;
627 return;
630 if (!target
631 /* Those are used to mark impossible scenarios. */
632 || (fcode = DECL_FUNCTION_CODE (target))
633 == BUILT_IN_UNREACHABLE
634 || fcode == BUILT_IN_TRAP)
635 return;
637 target_node = cgraph_get_node (target);
639 if (target_node != NULL
640 && (TREE_PUBLIC (target)
641 || target_node->definition)
642 && symtab_real_symbol_p (target_node))
644 gcc_assert (!target_node->global.inlined_to);
645 gcc_assert (symtab_real_symbol_p (target_node));
646 if (!pointer_set_insert (inserted, target))
648 pointer_set_insert (cached_polymorphic_call_targets,
649 target_node);
650 nodes.safe_push (target_node);
653 else if (completep
654 && !type_in_anonymous_namespace_p
655 (method_class_type (TREE_TYPE (target))))
656 *completep = false;
659 /* See if BINFO's type match OUTER_TYPE. If so, lookup
660 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
661 method in vtable and insert method to NODES array.
662 Otherwise recurse to base BINFOs.
663 This match what get_binfo_at_offset does, but with offset
664 being unknown.
666 TYPE_BINFOS is a stack of BINFOS of types with defined
667 virtual table seen on way from class type to BINFO.
669 MATCHED_VTABLES tracks virtual tables we already did lookup
670 for virtual function in. INSERTED tracks nodes we already
671 inserted.
673 ANONYMOUS is true if BINFO is part of anonymous namespace.
675 Clear COMPLETEP when we hit unreferable target.
678 static void
679 record_target_from_binfo (vec <cgraph_node *> &nodes,
680 tree binfo,
681 tree otr_type,
682 vec <tree> &type_binfos,
683 HOST_WIDE_INT otr_token,
684 tree outer_type,
685 HOST_WIDE_INT offset,
686 pointer_set_t *inserted,
687 pointer_set_t *matched_vtables,
688 bool anonymous,
689 bool *completep)
691 tree type = BINFO_TYPE (binfo);
692 int i;
693 tree base_binfo;
696 if (BINFO_VTABLE (binfo))
697 type_binfos.safe_push (binfo);
698 if (types_same_for_odr (type, outer_type))
700 int i;
701 tree type_binfo = NULL;
703 /* Lookup BINFO with virtual table. For normal types it is always last
704 binfo on stack. */
705 for (i = type_binfos.length () - 1; i >= 0; i--)
706 if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo))
708 type_binfo = type_binfos[i];
709 break;
711 if (BINFO_VTABLE (binfo))
712 type_binfos.pop ();
713 /* If this is duplicated BINFO for base shared by virtual inheritance,
714 we may not have its associated vtable. This is not a problem, since
715 we will walk it on the other path. */
716 if (!type_binfo)
717 return;
718 tree inner_binfo = get_binfo_at_offset (type_binfo,
719 offset, otr_type);
720 if (!inner_binfo)
722 gcc_assert (odr_violation_reported);
723 return;
725 /* For types in anonymous namespace first check if the respective vtable
726 is alive. If not, we know the type can't be called. */
727 if (!flag_ltrans && anonymous)
729 tree vtable = BINFO_VTABLE (inner_binfo);
730 varpool_node *vnode;
732 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
733 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
734 vnode = varpool_get_node (vtable);
735 if (!vnode || !vnode->definition)
736 return;
738 gcc_assert (inner_binfo);
739 if (!pointer_set_insert (matched_vtables, BINFO_VTABLE (inner_binfo)))
741 bool can_refer;
742 tree target = gimple_get_virt_method_for_binfo (otr_token,
743 inner_binfo,
744 &can_refer);
745 maybe_record_node (nodes, target, inserted, can_refer, completep);
747 return;
750 /* Walk bases. */
751 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
752 /* Walking bases that have no virtual method is pointless excercise. */
753 if (polymorphic_type_binfo_p (base_binfo))
754 record_target_from_binfo (nodes, base_binfo, otr_type,
755 type_binfos,
756 otr_token, outer_type, offset, inserted,
757 matched_vtables, anonymous, completep);
758 if (BINFO_VTABLE (binfo))
759 type_binfos.pop ();
762 /* Lookup virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
763 of TYPE, insert them to NODES, recurse into derived nodes.
764 INSERTED is used to avoid duplicate insertions of methods into NODES.
765 MATCHED_VTABLES are used to avoid duplicate walking vtables.
766 Clear COMPLETEP if unreferable target is found. */
768 static void
769 possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
770 pointer_set_t *inserted,
771 pointer_set_t *matched_vtables,
772 tree otr_type,
773 odr_type type,
774 HOST_WIDE_INT otr_token,
775 tree outer_type,
776 HOST_WIDE_INT offset,
777 bool *completep)
779 tree binfo = TYPE_BINFO (type->type);
780 unsigned int i;
781 vec <tree> type_binfos = vNULL;
783 record_target_from_binfo (nodes, binfo, otr_type, type_binfos, otr_token,
784 outer_type, offset,
785 inserted, matched_vtables,
786 type->anonymous_namespace, completep);
787 type_binfos.release ();
788 for (i = 0; i < type->derived_types.length (); i++)
789 possible_polymorphic_call_targets_1 (nodes, inserted,
790 matched_vtables,
791 otr_type,
792 type->derived_types[i],
793 otr_token, outer_type, offset, completep);
796 /* Cache of queries for polymorphic call targets.
798 Enumerating all call targets may get expensive when there are many
799 polymorphic calls in the program, so we memoize all the previous
800 queries and avoid duplicated work. */
802 struct polymorphic_call_target_d
804 HOST_WIDE_INT otr_token;
805 ipa_polymorphic_call_context context;
806 odr_type type;
807 vec <cgraph_node *> targets;
808 int nonconstruction_targets;
809 bool complete;
812 /* Polymorphic call target cache helpers. */
814 struct polymorphic_call_target_hasher
816 typedef polymorphic_call_target_d value_type;
817 typedef polymorphic_call_target_d compare_type;
818 static inline hashval_t hash (const value_type *);
819 static inline bool equal (const value_type *, const compare_type *);
820 static inline void remove (value_type *);
823 /* Return the computed hashcode for ODR_QUERY. */
825 inline hashval_t
826 polymorphic_call_target_hasher::hash (const value_type *odr_query)
828 hashval_t hash;
830 hash = iterative_hash_host_wide_int
831 (odr_query->otr_token,
832 odr_query->type->id);
833 hash = iterative_hash_hashval_t (TYPE_UID (odr_query->context.outer_type),
834 hash);
835 hash = iterative_hash_host_wide_int (odr_query->context.offset, hash);
836 return iterative_hash_hashval_t
837 (((int)odr_query->context.maybe_in_construction << 1)
838 | (int)odr_query->context.maybe_derived_type, hash);
841 /* Compare cache entries T1 and T2. */
843 inline bool
844 polymorphic_call_target_hasher::equal (const value_type *t1,
845 const compare_type *t2)
847 return (t1->type == t2->type && t1->otr_token == t2->otr_token
848 && t1->context.offset == t2->context.offset
849 && t1->context.outer_type == t2->context.outer_type
850 && t1->context.maybe_in_construction
851 == t2->context.maybe_in_construction
852 && t1->context.maybe_derived_type == t2->context.maybe_derived_type);
855 /* Remove entry in polymorphic call target cache hash. */
857 inline void
858 polymorphic_call_target_hasher::remove (value_type *v)
860 v->targets.release ();
861 free (v);
864 /* Polymorphic call target query cache. */
866 typedef hash_table <polymorphic_call_target_hasher>
867 polymorphic_call_target_hash_type;
868 static polymorphic_call_target_hash_type polymorphic_call_target_hash;
870 /* Destroy polymorphic call target query cache. */
872 static void
873 free_polymorphic_call_targets_hash ()
875 if (cached_polymorphic_call_targets)
877 polymorphic_call_target_hash.dispose ();
878 pointer_set_destroy (cached_polymorphic_call_targets);
879 cached_polymorphic_call_targets = NULL;
883 /* When virtual function is removed, we may need to flush the cache. */
885 static void
886 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
888 if (cached_polymorphic_call_targets
889 && pointer_set_contains (cached_polymorphic_call_targets, n))
890 free_polymorphic_call_targets_hash ();
893 /* CONTEXT->OUTER_TYPE is a type of memory object where object of EXPECTED_TYPE
894 is contained at CONTEXT->OFFSET. Walk the memory representation of
895 CONTEXT->OUTER_TYPE and find the outermost class type that match
896 EXPECTED_TYPE or contain EXPECTED_TYPE as a base. Update CONTEXT
897 to represent it.
899 For example when CONTEXT represents type
900 class A
902 int a;
903 class B b;
905 and we look for type at offset sizeof(int), we end up with B and offset 0.
906 If the same is produced by multiple inheritance, we end up with A and offset
907 sizeof(int).
909 If we can not find corresponding class, give up by setting
910 CONTEXT->OUTER_TYPE to EXPECTED_TYPE and CONTEXT->OFFSET to NULL.
911 Return true when lookup was sucesful. */
913 static bool
914 get_class_context (ipa_polymorphic_call_context *context,
915 tree expected_type)
917 tree type = context->outer_type;
918 HOST_WIDE_INT offset = context->offset;
920 /* Find the sub-object the constant actually refers to and mark whether it is
921 an artificial one (as opposed to a user-defined one). */
922 while (true)
924 HOST_WIDE_INT pos, size;
925 tree fld;
927 /* On a match, just return what we found. */
928 if (TREE_CODE (type) == TREE_CODE (expected_type)
929 && types_same_for_odr (type, expected_type))
931 /* Type can not contain itself on an non-zero offset. In that case
932 just give up. */
933 if (offset != 0)
934 goto give_up;
935 gcc_assert (offset == 0);
936 return true;
939 /* Walk fields and find corresponding on at OFFSET. */
940 if (TREE_CODE (type) == RECORD_TYPE)
942 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
944 if (TREE_CODE (fld) != FIELD_DECL)
945 continue;
947 pos = int_bit_position (fld);
948 size = tree_to_uhwi (DECL_SIZE (fld));
949 if (pos <= offset && (pos + size) > offset)
950 break;
953 if (!fld)
954 goto give_up;
956 type = TREE_TYPE (fld);
957 offset -= pos;
958 /* DECL_ARTIFICIAL represents a basetype. */
959 if (!DECL_ARTIFICIAL (fld))
961 context->outer_type = type;
962 context->offset = offset;
963 /* As soon as we se an field containing the type,
964 we know we are not looking for derivations. */
965 context->maybe_derived_type = false;
968 else if (TREE_CODE (type) == ARRAY_TYPE)
970 tree subtype = TREE_TYPE (type);
972 /* Give up if we don't know array size. */
973 if (!tree_fits_shwi_p (TYPE_SIZE (subtype))
974 || !tree_to_shwi (TYPE_SIZE (subtype)) <= 0)
975 goto give_up;
976 offset = offset % tree_to_shwi (TYPE_SIZE (subtype));
977 type = subtype;
978 context->outer_type = type;
979 context->offset = offset;
980 context->maybe_derived_type = false;
982 /* Give up on anything else. */
983 else
984 goto give_up;
987 /* If we failed to find subtype we look for, give up and fall back to the
988 most generic query. */
989 give_up:
990 context->outer_type = expected_type;
991 context->offset = 0;
992 context->maybe_derived_type = true;
993 context->maybe_in_construction = true;
994 /* POD can be changed to an instance of a polymorphic type by
995 placement new. Here we play safe and assume that any
996 non-polymorphic type is POD. */
997 if ((TREE_CODE (type) != RECORD_TYPE
998 || !TYPE_BINFO (type)
999 || !polymorphic_type_binfo_p (TYPE_BINFO (type)))
1000 && (!TYPE_SIZE (type)
1001 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
1002 || (offset + tree_to_uhwi (TYPE_SIZE (expected_type)) <=
1003 tree_to_uhwi (TYPE_SIZE (type)))))
1004 return true;
1005 return false;
1008 /* Return true if OUTER_TYPE contains OTR_TYPE at OFFSET. */
1010 static bool
1011 contains_type_p (tree outer_type, HOST_WIDE_INT offset,
1012 tree otr_type)
1014 ipa_polymorphic_call_context context = {offset, outer_type,
1015 false, true};
1016 return get_class_context (&context, otr_type);
1019 /* Lookup base of BINFO that has virtual table VTABLE with OFFSET. */
1021 static tree
1022 subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
1023 tree vtable)
1025 tree v = BINFO_VTABLE (binfo);
1026 int i;
1027 tree base_binfo;
1028 unsigned HOST_WIDE_INT this_offset;
1030 if (v)
1032 if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
1033 gcc_unreachable ();
1035 if (offset == this_offset
1036 && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
1037 return binfo;
1040 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1041 if (polymorphic_type_binfo_p (base_binfo))
1043 base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable);
1044 if (base_binfo)
1045 return base_binfo;
1047 return NULL;
1050 /* T is known constant value of virtual table pointer.
1051 Store virtual table to V and its offset to OFFSET.
1052 Return false if T does not look like virtual table reference. */
1054 bool
1055 vtable_pointer_value_to_vtable (tree t, tree *v, unsigned HOST_WIDE_INT *offset)
1057 /* We expect &MEM[(void *)&virtual_table + 16B].
1058 We obtain object's BINFO from the context of the virtual table.
1059 This one contains pointer to virtual table represented via
1060 POINTER_PLUS_EXPR. Verify that this pointer match to what
1061 we propagated through.
1063 In the case of virtual inheritance, the virtual tables may
1064 be nested, i.e. the offset may be different from 16 and we may
1065 need to dive into the type representation. */
1066 if (TREE_CODE (t) == ADDR_EXPR
1067 && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
1068 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
1069 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
1070 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
1071 == VAR_DECL)
1072 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
1073 (TREE_OPERAND (t, 0), 0), 0)))
1075 *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
1076 *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
1077 return true;
1080 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
1081 We need to handle it when T comes from static variable initializer or
1082 BINFO. */
1083 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
1085 *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
1086 t = TREE_OPERAND (t, 0);
1088 else
1089 *offset = 0;
1091 if (TREE_CODE (t) != ADDR_EXPR)
1092 return false;
1093 *v = TREE_OPERAND (t, 0);
1094 return true;
1097 /* T is known constant value of virtual table pointer. Return BINFO of the
1098 instance type. */
1100 tree
1101 vtable_pointer_value_to_binfo (tree t)
1103 tree vtable;
1104 unsigned HOST_WIDE_INT offset;
1106 if (!vtable_pointer_value_to_vtable (t, &vtable, &offset))
1107 return NULL_TREE;
1109 /* FIXME: for stores of construction vtables we return NULL,
1110 because we do not have BINFO for those. Eventually we should fix
1111 our representation to allow this case to be handled, too.
1112 In the case we see store of BINFO we however may assume
1113 that standard folding will be ale to cope with it. */
1114 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
1115 offset, vtable);
1118 /* Proudce polymorphic call context for call method of instance
1119 that is located within BASE (that is assumed to be a decl) at OFFSET. */
1121 static void
1122 get_polymorphic_call_info_for_decl (ipa_polymorphic_call_context *context,
1123 tree base, HOST_WIDE_INT offset)
1125 gcc_assert (DECL_P (base));
1127 context->outer_type = TREE_TYPE (base);
1128 context->offset = offset;
1129 /* Make very conservative assumption that all objects
1130 may be in construction.
1131 TODO: ipa-prop already contains code to tell better.
1132 merge it later. */
1133 context->maybe_in_construction = true;
1134 context->maybe_derived_type = false;
1137 /* CST is an invariant (address of decl), try to get meaningful
1138 polymorphic call context for polymorphic call of method
1139 if instance of OTR_TYPE that is located at OFFSET of this invariant.
1140 Return FALSE if nothing meaningful can be found. */
1142 bool
1143 get_polymorphic_call_info_from_invariant (ipa_polymorphic_call_context *context,
1144 tree cst,
1145 tree otr_type,
1146 HOST_WIDE_INT offset)
1148 HOST_WIDE_INT offset2, size, max_size;
1149 tree base;
1151 if (TREE_CODE (cst) != ADDR_EXPR)
1152 return false;
1154 cst = TREE_OPERAND (cst, 0);
1155 base = get_ref_base_and_extent (cst, &offset2, &size, &max_size);
1156 if (!DECL_P (base) || max_size == -1 || max_size != size)
1157 return false;
1159 /* Only type inconsistent programs can have otr_type that is
1160 not part of outer type. */
1161 if (!contains_type_p (TREE_TYPE (base), offset, otr_type))
1162 return false;
1164 get_polymorphic_call_info_for_decl (context, base, offset);
1165 return true;
1168 /* Given REF call in FNDECL, determine class of the polymorphic
1169 call (OTR_TYPE), its token (OTR_TOKEN) and CONTEXT.
1170 Return pointer to object described by the context */
1172 tree
1173 get_polymorphic_call_info (tree fndecl,
1174 tree ref,
1175 tree *otr_type,
1176 HOST_WIDE_INT *otr_token,
1177 ipa_polymorphic_call_context *context)
1179 tree base_pointer;
1180 *otr_type = obj_type_ref_class (ref);
1181 *otr_token = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref));
1183 /* Set up basic info in case we find nothing interesting in the analysis. */
1184 context->outer_type = *otr_type;
1185 context->offset = 0;
1186 base_pointer = OBJ_TYPE_REF_OBJECT (ref);
1187 context->maybe_derived_type = true;
1188 context->maybe_in_construction = false;
1190 /* Walk SSA for outer object. */
1193 if (TREE_CODE (base_pointer) == SSA_NAME
1194 && !SSA_NAME_IS_DEFAULT_DEF (base_pointer)
1195 && SSA_NAME_DEF_STMT (base_pointer)
1196 && gimple_assign_single_p (SSA_NAME_DEF_STMT (base_pointer)))
1198 base_pointer = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (base_pointer));
1199 STRIP_NOPS (base_pointer);
1201 else if (TREE_CODE (base_pointer) == ADDR_EXPR)
1203 HOST_WIDE_INT size, max_size;
1204 HOST_WIDE_INT offset2;
1205 tree base = get_ref_base_and_extent (TREE_OPERAND (base_pointer, 0),
1206 &offset2, &size, &max_size);
1208 /* If this is a varying address, punt. */
1209 if ((TREE_CODE (base) == MEM_REF || DECL_P (base))
1210 && max_size != -1
1211 && max_size == size)
1213 /* We found dereference of a pointer. Type of the pointer
1214 and MEM_REF is meaningless, but we can look futher. */
1215 if (TREE_CODE (base) == MEM_REF)
1217 base_pointer = TREE_OPERAND (base, 0);
1218 context->offset
1219 += offset2 + mem_ref_offset (base).low * BITS_PER_UNIT;
1220 context->outer_type = NULL;
1222 /* We found base object. In this case the outer_type
1223 is known. */
1224 else if (DECL_P (base))
1226 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (base)));
1228 /* Only type inconsistent programs can have otr_type that is
1229 not part of outer type. */
1230 if (!contains_type_p (TREE_TYPE (base),
1231 context->offset + offset2, *otr_type))
1233 /* Use OTR_TOKEN = INT_MAX as a marker of probably type inconsistent
1234 code sequences; we arrange the calls to be builtin_unreachable
1235 later. */
1236 *otr_token = INT_MAX;
1237 return base_pointer;
1239 get_polymorphic_call_info_for_decl (context, base,
1240 context->offset + offset2);
1241 return NULL;
1243 else
1244 break;
1246 else
1247 break;
1249 else if (TREE_CODE (base_pointer) == POINTER_PLUS_EXPR
1250 && tree_fits_uhwi_p (TREE_OPERAND (base_pointer, 1)))
1252 context->offset += tree_to_shwi (TREE_OPERAND (base_pointer, 1))
1253 * BITS_PER_UNIT;
1254 base_pointer = TREE_OPERAND (base_pointer, 0);
1256 else
1257 break;
1259 while (true);
1261 /* Try to determine type of the outer object. */
1262 if (TREE_CODE (base_pointer) == SSA_NAME
1263 && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
1264 && TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL)
1266 /* See if parameter is THIS pointer of a method. */
1267 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE
1268 && SSA_NAME_VAR (base_pointer) == DECL_ARGUMENTS (fndecl))
1270 context->outer_type = TREE_TYPE (TREE_TYPE (base_pointer));
1271 gcc_assert (TREE_CODE (context->outer_type) == RECORD_TYPE);
1273 /* Dynamic casting has possibly upcasted the type
1274 in the hiearchy. In this case outer type is less
1275 informative than inner type and we should forget
1276 about it. */
1277 if (!contains_type_p (context->outer_type, context->offset,
1278 *otr_type))
1280 context->outer_type = NULL;
1281 return base_pointer;
1284 /* If the function is constructor or destructor, then
1285 the type is possibly in construction, but we know
1286 it is not derived type. */
1287 if (DECL_CXX_CONSTRUCTOR_P (fndecl)
1288 || DECL_CXX_DESTRUCTOR_P (fndecl))
1290 context->maybe_in_construction = true;
1291 context->maybe_derived_type = false;
1293 else
1295 context->maybe_derived_type = true;
1296 context->maybe_in_construction = false;
1298 return base_pointer;
1300 /* Non-PODs passed by value are really passed by invisible
1301 reference. In this case we also know the type of the
1302 object. */
1303 if (DECL_BY_REFERENCE (SSA_NAME_VAR (base_pointer)))
1305 context->outer_type = TREE_TYPE (TREE_TYPE (base_pointer));
1306 gcc_assert (!POINTER_TYPE_P (context->outer_type));
1307 /* Only type inconsistent programs can have otr_type that is
1308 not part of outer type. */
1309 if (!contains_type_p (context->outer_type, context->offset,
1310 *otr_type))
1312 /* Use OTR_TOKEN = INT_MAX as a marker of probably type inconsistent
1313 code sequences; we arrange the calls to be builtin_unreachable
1314 later. */
1315 *otr_token = INT_MAX;
1316 return base_pointer;
1318 context->maybe_derived_type = false;
1319 context->maybe_in_construction = false;
1320 return base_pointer;
1323 /* TODO: There are multiple ways to derive a type. For instance
1324 if BASE_POINTER is passed to an constructor call prior our refernece.
1325 We do not make this type of flow sensitive analysis yet. */
1326 return base_pointer;
1329 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
1330 Lookup their respecitve virtual methods for OTR_TOKEN and OTR_TYPE
1331 and insert them to NODES.
1333 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
1335 static void
1336 record_targets_from_bases (tree otr_type,
1337 HOST_WIDE_INT otr_token,
1338 tree outer_type,
1339 HOST_WIDE_INT offset,
1340 vec <cgraph_node *> &nodes,
1341 pointer_set_t *inserted,
1342 pointer_set_t *matched_vtables,
1343 bool *completep)
1345 while (true)
1347 HOST_WIDE_INT pos, size;
1348 tree base_binfo;
1349 tree fld;
1351 if (types_same_for_odr (outer_type, otr_type))
1352 return;
1354 for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
1356 if (TREE_CODE (fld) != FIELD_DECL)
1357 continue;
1359 pos = int_bit_position (fld);
1360 size = tree_to_shwi (DECL_SIZE (fld));
1361 if (pos <= offset && (pos + size) > offset
1362 /* Do not get confused by zero sized bases. */
1363 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
1364 break;
1366 /* Within a class type we should always find correcponding fields. */
1367 gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
1369 /* Nonbasetypes should have been stripped by outer_class_type. */
1370 gcc_assert (DECL_ARTIFICIAL (fld));
1372 outer_type = TREE_TYPE (fld);
1373 offset -= pos;
1375 base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
1376 offset, otr_type);
1377 if (!base_binfo)
1379 gcc_assert (odr_violation_reported);
1380 return;
1382 gcc_assert (base_binfo);
1383 if (!pointer_set_insert (matched_vtables, BINFO_VTABLE (base_binfo)))
1385 bool can_refer;
1386 tree target = gimple_get_virt_method_for_binfo (otr_token,
1387 base_binfo,
1388 &can_refer);
1389 maybe_record_node (nodes, target, inserted, can_refer, completep);
1390 pointer_set_insert (matched_vtables, BINFO_VTABLE (base_binfo));
1395 /* When virtual table is removed, we may need to flush the cache. */
1397 static void
1398 devirt_variable_node_removal_hook (varpool_node *n,
1399 void *d ATTRIBUTE_UNUSED)
1401 if (cached_polymorphic_call_targets
1402 && DECL_VIRTUAL_P (n->decl)
1403 && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
1404 free_polymorphic_call_targets_hash ();
1407 /* Return vector containing possible targets of polymorphic call of type
1408 OTR_TYPE caling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
1409 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containig
1410 OTR_TYPE and include their virtual method. This is useful for types
1411 possibly in construction or destruction where the virtual table may
1412 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
1413 us to walk the inheritance graph for all derivations.
1415 OTR_TOKEN == INT_MAX is used to mark calls that are provably
1416 undefined and should be redirected to unreachable.
1418 If COMPLETEP is non-NULL, store true if the list is complete.
1419 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
1420 in the target cache. If user needs to visit every target list
1421 just once, it can memoize them.
1423 NONCONSTRUCTION_TARGETS specify number of targets with asumption that
1424 the type is not in the construction. Those targets appear first in the
1425 vector returned.
1427 Returned vector is placed into cache. It is NOT caller's responsibility
1428 to free it. The vector can be freed on cgraph_remove_node call if
1429 the particular node is a virtual function present in the cache. */
1431 vec <cgraph_node *>
1432 possible_polymorphic_call_targets (tree otr_type,
1433 HOST_WIDE_INT otr_token,
1434 ipa_polymorphic_call_context context,
1435 bool *completep,
1436 void **cache_token,
1437 int *nonconstruction_targetsp)
1439 static struct cgraph_node_hook_list *node_removal_hook_holder;
1440 pointer_set_t *inserted;
1441 pointer_set_t *matched_vtables;
1442 vec <cgraph_node *> nodes = vNULL;
1443 odr_type type, outer_type;
1444 polymorphic_call_target_d key;
1445 polymorphic_call_target_d **slot;
1446 unsigned int i;
1447 tree binfo, target;
1448 bool complete;
1449 bool can_refer;
1451 /* If ODR is not initialized, return empty incomplete list. */
1452 if (!odr_hash.is_created ())
1454 if (completep)
1455 *completep = false;
1456 if (cache_token)
1457 *cache_token = NULL;
1458 if (nonconstruction_targetsp)
1459 *nonconstruction_targetsp = 0;
1460 return nodes;
1463 /* If we hit type inconsistency, just return empty list of targets. */
1464 if (otr_token == INT_MAX)
1466 if (completep)
1467 *completep = true;
1468 if (cache_token)
1469 *cache_token = NULL;
1470 if (nonconstruction_targetsp)
1471 *nonconstruction_targetsp = 0;
1472 return nodes;
1475 type = get_odr_type (otr_type, true);
1477 /* Lookup the outer class type we want to walk. */
1478 if (context.outer_type
1479 && !get_class_context (&context, otr_type))
1481 if (completep)
1482 *completep = false;
1483 if (cache_token)
1484 *cache_token = NULL;
1485 if (nonconstruction_targetsp)
1486 *nonconstruction_targetsp = 0;
1487 return nodes;
1490 /* We canonicalize our query, so we do not need extra hashtable entries. */
1492 /* Without outer type, we have no use for offset. Just do the
1493 basic search from innter type */
1494 if (!context.outer_type)
1496 context.outer_type = otr_type;
1497 context.offset = 0;
1499 /* We need to update our hiearchy if the type does not exist. */
1500 outer_type = get_odr_type (context.outer_type, true);
1501 /* If outer and inner type match, there are no bases to see. */
1502 if (type == outer_type)
1503 context.maybe_in_construction = false;
1504 /* If the type is complete, there are no derivations. */
1505 if (TYPE_FINAL_P (outer_type->type))
1506 context.maybe_derived_type = false;
1508 /* Initialize query cache. */
1509 if (!cached_polymorphic_call_targets)
1511 cached_polymorphic_call_targets = pointer_set_create ();
1512 polymorphic_call_target_hash.create (23);
1513 if (!node_removal_hook_holder)
1515 node_removal_hook_holder =
1516 cgraph_add_node_removal_hook (&devirt_node_removal_hook, NULL);
1517 varpool_add_node_removal_hook (&devirt_variable_node_removal_hook,
1518 NULL);
1522 /* Lookup cached answer. */
1523 key.type = type;
1524 key.otr_token = otr_token;
1525 key.context = context;
1526 slot = polymorphic_call_target_hash.find_slot (&key, INSERT);
1527 if (cache_token)
1528 *cache_token = (void *)*slot;
1529 if (*slot)
1531 if (completep)
1532 *completep = (*slot)->complete;
1533 if (nonconstruction_targetsp)
1534 *nonconstruction_targetsp = (*slot)->nonconstruction_targets;
1535 return (*slot)->targets;
1538 complete = true;
1540 /* Do actual search. */
1541 timevar_push (TV_IPA_VIRTUAL_CALL);
1542 *slot = XCNEW (polymorphic_call_target_d);
1543 if (cache_token)
1544 *cache_token = (void *)*slot;
1545 (*slot)->type = type;
1546 (*slot)->otr_token = otr_token;
1547 (*slot)->context = context;
1549 inserted = pointer_set_create ();
1550 matched_vtables = pointer_set_create ();
1552 /* First see virtual method of type itself. */
1553 binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
1554 context.offset, otr_type);
1555 if (binfo)
1556 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
1557 &can_refer);
1558 else
1560 gcc_assert (odr_violation_reported);
1561 target = NULL;
1564 maybe_record_node (nodes, target, inserted, can_refer, &complete);
1566 if (target)
1568 /* In the case we get complete method, we don't need
1569 to walk derivations. */
1570 if (DECL_FINAL_P (target))
1571 context.maybe_derived_type = false;
1573 else
1574 gcc_assert (!complete);
1576 pointer_set_insert (matched_vtables, BINFO_VTABLE (binfo));
1578 /* Next walk recursively all derived types. */
1579 if (context.maybe_derived_type)
1581 /* For anonymous namespace types we can attempt to build full type.
1582 All derivations must be in this unit (unless we see partial unit). */
1583 if (!type->anonymous_namespace || flag_ltrans)
1584 complete = false;
1585 for (i = 0; i < outer_type->derived_types.length(); i++)
1586 possible_polymorphic_call_targets_1 (nodes, inserted,
1587 matched_vtables,
1588 otr_type,
1589 outer_type->derived_types[i],
1590 otr_token, outer_type->type,
1591 context.offset, &complete);
1594 /* Finally walk bases, if asked to. */
1595 (*slot)->nonconstruction_targets = nodes.length();
1596 if (context.maybe_in_construction)
1597 record_targets_from_bases (otr_type, otr_token, outer_type->type,
1598 context.offset, nodes, inserted,
1599 matched_vtables, &complete);
1601 (*slot)->targets = nodes;
1602 (*slot)->complete = complete;
1603 if (completep)
1604 *completep = complete;
1605 if (nonconstruction_targetsp)
1606 *nonconstruction_targetsp = (*slot)->nonconstruction_targets;
1608 pointer_set_destroy (inserted);
1609 pointer_set_destroy (matched_vtables);
1610 timevar_pop (TV_IPA_VIRTUAL_CALL);
1611 return nodes;
1614 /* Dump all possible targets of a polymorphic call. */
1616 void
1617 dump_possible_polymorphic_call_targets (FILE *f,
1618 tree otr_type,
1619 HOST_WIDE_INT otr_token,
1620 const ipa_polymorphic_call_context &ctx)
1622 vec <cgraph_node *> targets;
1623 bool final;
1624 odr_type type = get_odr_type (otr_type, false);
1625 unsigned int i;
1626 int nonconstruction;
1628 if (!type)
1629 return;
1630 targets = possible_polymorphic_call_targets (otr_type, otr_token,
1631 ctx,
1632 &final, NULL, &nonconstruction);
1633 fprintf (f, " Targets of polymorphic call of type %i:", type->id);
1634 print_generic_expr (f, type->type, TDF_SLIM);
1635 fprintf (f, " token %i\n", (int)otr_token);
1636 if (ctx.outer_type || ctx.offset)
1638 fprintf (f, " Contained in type:");
1639 print_generic_expr (f, ctx.outer_type, TDF_SLIM);
1640 fprintf (f, " at offset "HOST_WIDE_INT_PRINT_DEC"\n",
1641 ctx.offset);
1644 fprintf (f, " %s%s%s\n ",
1645 final ? "This is a complete list." :
1646 "This is partial list; extra targets may be defined in other units.",
1647 ctx.maybe_in_construction ? " (base types included)" : "",
1648 ctx.maybe_derived_type ? " (derived types included)" : "");
1649 for (i = 0; i < targets.length (); i++)
1651 char *name = NULL;
1652 if (i == (unsigned)nonconstruction)
1653 fprintf (f, "\n If the type is in construction,"
1654 " then additional tarets are:\n"
1655 " ");
1656 if (in_lto_p)
1657 name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
1658 fprintf (f, " %s/%i", name ? name : targets[i]->name (), targets[i]->order);
1659 if (in_lto_p)
1660 free (name);
1661 if (!targets[i]->definition)
1662 fprintf (f, " (no definition%s)",
1663 DECL_DECLARED_INLINE_P (targets[i]->decl)
1664 ? " inline" : "");
1666 fprintf (f, "\n\n");
1670 /* Return true if N can be possibly target of a polymorphic call of
1671 OTR_TYPE/OTR_TOKEN. */
1673 bool
1674 possible_polymorphic_call_target_p (tree otr_type,
1675 HOST_WIDE_INT otr_token,
1676 const ipa_polymorphic_call_context &ctx,
1677 struct cgraph_node *n)
1679 vec <cgraph_node *> targets;
1680 unsigned int i;
1681 enum built_in_function fcode;
1682 bool final;
1684 if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
1685 && ((fcode = DECL_FUNCTION_CODE (n->decl))
1686 == BUILT_IN_UNREACHABLE
1687 || fcode == BUILT_IN_TRAP))
1688 return true;
1690 if (!odr_hash.is_created ())
1691 return true;
1692 targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
1693 for (i = 0; i < targets.length (); i++)
1694 if (symtab_semantically_equivalent_p (n, targets[i]))
1695 return true;
1697 /* At a moment we allow middle end to dig out new external declarations
1698 as a targets of polymorphic calls. */
1699 if (!final && !n->definition)
1700 return true;
1701 return false;
1705 /* After callgraph construction new external nodes may appear.
1706 Add them into the graph. */
1708 void
1709 update_type_inheritance_graph (void)
1711 struct cgraph_node *n;
1713 if (!odr_hash.is_created ())
1714 return;
1715 free_polymorphic_call_targets_hash ();
1716 timevar_push (TV_IPA_INHERITANCE);
1717 /* We reconstruct the graph starting from types of all methods seen in the
1718 the unit. */
1719 FOR_EACH_FUNCTION (n)
1720 if (DECL_VIRTUAL_P (n->decl)
1721 && !n->definition
1722 && symtab_real_symbol_p (n))
1723 get_odr_type (method_class_type (TREE_TYPE (n->decl)), true);
1724 timevar_pop (TV_IPA_INHERITANCE);
1728 /* Return true if N looks like likely target of a polymorphic call.
1729 Rule out cxa_pure_virtual, noreturns, function declared cold and
1730 other obvious cases. */
1732 bool
1733 likely_target_p (struct cgraph_node *n)
1735 int flags;
1736 /* cxa_pure_virtual and similar things are not likely. */
1737 if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
1738 return false;
1739 flags = flags_from_decl_or_type (n->decl);
1740 if (flags & ECF_NORETURN)
1741 return false;
1742 if (lookup_attribute ("cold",
1743 DECL_ATTRIBUTES (n->decl)))
1744 return false;
1745 if (n->frequency < NODE_FREQUENCY_NORMAL)
1746 return false;
1747 return true;
1750 /* The ipa-devirt pass.
1751 When polymorphic call has only one likely target in the unit,
1752 turn it into speculative call. */
1754 static unsigned int
1755 ipa_devirt (void)
1757 struct cgraph_node *n;
1758 struct pointer_set_t *bad_call_targets = pointer_set_create ();
1759 struct cgraph_edge *e;
1761 int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
1762 int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
1763 int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
1765 FOR_EACH_DEFINED_FUNCTION (n)
1767 bool update = false;
1768 if (dump_file && n->indirect_calls)
1769 fprintf (dump_file, "\n\nProcesing function %s/%i\n",
1770 n->name (), n->order);
1771 for (e = n->indirect_calls; e; e = e->next_callee)
1772 if (e->indirect_info->polymorphic)
1774 struct cgraph_node *likely_target = NULL;
1775 void *cache_token;
1776 bool final;
1777 int nonconstruction_targets;
1778 vec <cgraph_node *>targets
1779 = possible_polymorphic_call_targets
1780 (e, &final, &cache_token, &nonconstruction_targets);
1781 unsigned int i;
1783 if (dump_file)
1784 dump_possible_polymorphic_call_targets
1785 (dump_file, e);
1787 npolymorphic++;
1789 if (!cgraph_maybe_hot_edge_p (e))
1791 if (dump_file)
1792 fprintf (dump_file, "Call is cold\n\n");
1793 ncold++;
1794 continue;
1796 if (e->speculative)
1798 if (dump_file)
1799 fprintf (dump_file, "Call is aready speculated\n\n");
1800 nspeculated++;
1802 /* When dumping see if we agree with speculation. */
1803 if (!dump_file)
1804 continue;
1806 if (pointer_set_contains (bad_call_targets,
1807 cache_token))
1809 if (dump_file)
1810 fprintf (dump_file, "Target list is known to be useless\n\n");
1811 nmultiple++;
1812 continue;
1814 for (i = 0; i < targets.length (); i++)
1815 if (likely_target_p (targets[i]))
1817 if (likely_target)
1819 if (i < (unsigned) nonconstruction_targets)
1821 likely_target = NULL;
1822 if (dump_file)
1823 fprintf (dump_file, "More than one likely target\n\n");
1824 nmultiple++;
1826 break;
1828 likely_target = targets[i];
1830 if (!likely_target)
1832 pointer_set_insert (bad_call_targets, cache_token);
1833 continue;
1835 /* This is reached only when dumping; check if we agree or disagree
1836 with the speculation. */
1837 if (e->speculative)
1839 struct cgraph_edge *e2;
1840 struct ipa_ref *ref;
1841 cgraph_speculative_call_info (e, e2, e, ref);
1842 if (cgraph_function_or_thunk_node (e2->callee, NULL)
1843 == cgraph_function_or_thunk_node (likely_target, NULL))
1845 fprintf (dump_file, "We agree with speculation\n\n");
1846 nok++;
1848 else
1850 fprintf (dump_file, "We disagree with speculation\n\n");
1851 nwrong++;
1853 continue;
1855 if (!likely_target->definition)
1857 if (dump_file)
1858 fprintf (dump_file, "Target is not an definition\n\n");
1859 nnotdefined++;
1860 continue;
1862 /* Do not introduce new references to external symbols. While we
1863 can handle these just well, it is common for programs to
1864 incorrectly with headers defining methods they are linked
1865 with. */
1866 if (DECL_EXTERNAL (likely_target->decl))
1868 if (dump_file)
1869 fprintf (dump_file, "Target is external\n\n");
1870 nexternal++;
1871 continue;
1873 /* Don't use an implicitly-declared destructor (c++/58678). */
1874 struct cgraph_node *non_thunk_target
1875 = cgraph_function_node (likely_target);
1876 if (DECL_ARTIFICIAL (non_thunk_target->decl))
1878 if (dump_file)
1879 fprintf (dump_file, "Target is artificial\n\n");
1880 nartificial++;
1881 continue;
1883 if (cgraph_function_body_availability (likely_target)
1884 <= AVAIL_OVERWRITABLE
1885 && symtab_can_be_discarded (likely_target))
1887 if (dump_file)
1888 fprintf (dump_file, "Target is overwritable\n\n");
1889 noverwritable++;
1890 continue;
1892 else
1894 if (dump_file)
1895 fprintf (dump_file,
1896 "Speculatively devirtualizing call in %s/%i to %s/%i\n\n",
1897 n->name (), n->order,
1898 likely_target->name (),
1899 likely_target->order);
1900 if (!symtab_can_be_discarded (likely_target))
1902 cgraph_node *alias;
1903 alias = cgraph (symtab_nonoverwritable_alias
1904 (likely_target));
1905 if (alias)
1906 likely_target = alias;
1908 nconverted++;
1909 update = true;
1910 cgraph_turn_edge_to_speculative
1911 (e, likely_target, e->count * 8 / 10, e->frequency * 8 / 10);
1914 if (update)
1915 inline_update_overall_summary (n);
1917 pointer_set_destroy (bad_call_targets);
1919 if (dump_file)
1920 fprintf (dump_file,
1921 "%i polymorphic calls, %i devirtualized,"
1922 " %i speculatively devirtualized, %i cold\n"
1923 "%i have multiple targets, %i overwritable,"
1924 " %i already speculated (%i agree, %i disagree),"
1925 " %i external, %i not defined, %i artificial\n",
1926 npolymorphic, ndevirtualized, nconverted, ncold,
1927 nmultiple, noverwritable, nspeculated, nok, nwrong,
1928 nexternal, nnotdefined, nartificial);
1929 return ndevirtualized ? TODO_remove_functions : 0;
1932 /* Gate for speculative IPA devirtualization optimization. */
1934 static bool
1935 gate_ipa_devirt (void)
1937 return (flag_devirtualize
1938 && flag_devirtualize_speculatively
1939 && optimize);
1942 namespace {
1944 const pass_data pass_data_ipa_devirt =
1946 IPA_PASS, /* type */
1947 "devirt", /* name */
1948 OPTGROUP_NONE, /* optinfo_flags */
1949 true, /* has_gate */
1950 true, /* has_execute */
1951 TV_IPA_DEVIRT, /* tv_id */
1952 0, /* properties_required */
1953 0, /* properties_provided */
1954 0, /* properties_destroyed */
1955 0, /* todo_flags_start */
1956 ( TODO_dump_symtab ), /* todo_flags_finish */
1959 class pass_ipa_devirt : public ipa_opt_pass_d
1961 public:
1962 pass_ipa_devirt (gcc::context *ctxt)
1963 : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
1964 NULL, /* generate_summary */
1965 NULL, /* write_summary */
1966 NULL, /* read_summary */
1967 NULL, /* write_optimization_summary */
1968 NULL, /* read_optimization_summary */
1969 NULL, /* stmt_fixup */
1970 0, /* function_transform_todo_flags_start */
1971 NULL, /* function_transform */
1972 NULL) /* variable_transform */
1975 /* opt_pass methods: */
1976 bool gate () { return gate_ipa_devirt (); }
1977 unsigned int execute () { return ipa_devirt (); }
1979 }; // class pass_ipa_devirt
1981 } // anon namespace
1983 ipa_opt_pass_d *
1984 make_pass_ipa_devirt (gcc::context *ctxt)
1986 return new pass_ipa_devirt (ctxt);
1989 #include "gt-ipa-devirt.h"