]>
Commit | Line | Data |
---|---|---|
eefe9a99 JH |
1 | /* Basic IPA utilities for type inheritance graph construction and |
2 | devirtualization. | |
3 | Copyright (C) 2013 Free Software Foundation, Inc. | |
4 | Contributed by Jan Hubicka | |
5 | ||
6 | This file is part of GCC. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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/>. */ | |
21 | ||
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. | |
38 | ||
39 | OTR = OBJ_TYPE_REF | |
0e1474e5 | 40 | This is the Gimple representation of type information of a polymorphic call. |
eefe9a99 JH |
41 | It contains two parameters: |
42 | otr_type is a type of class whose method is called. | |
0e1474e5 | 43 | otr_token is the index into virtual table where address is taken. |
eefe9a99 JH |
44 | |
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. | |
49 | ||
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. | |
53 | ||
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 | |
0e1474e5 | 58 | virtual table of the base type. Also BINFO_OFFSET specifies |
eefe9a99 JH |
59 | offset of the base within the type. |
60 | ||
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). | |
66 | ||
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. | |
71 | ||
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 | |
0e1474e5 | 75 | or from DECL_VINDEX of a given virtual table. |
eefe9a99 JH |
76 | |
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. | |
81 | ||
82 | What we do here: | |
83 | ||
84 | build_type_inheritance_graph triggers a construction of the type inheritance | |
85 | graph. | |
86 | ||
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 | |
0e1474e5 | 89 | inserted into the graph. Also types without virtual methods are not |
eefe9a99 JH |
90 | represented at all, though it may be easy to add this. |
91 | ||
92 | The inheritance graph is represented as follows: | |
93 | ||
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) | |
97 | ||
0e1474e5 | 98 | Edges are represented by odr_type->base and odr_type->derived_types. |
eefe9a99 JH |
99 | At the moment we do not track offsets of types for multiple inheritance. |
100 | Adding this is easy. | |
101 | ||
102 | possible_polymorphic_call_targets returns, given an parameters found in | |
103 | indirect polymorphic edge all possible polymorphic call targets of the call. | |
bbc9396b JH |
104 | |
105 | pass_ipa_devirt performs simple speculative devirtualization. | |
eefe9a99 JH |
106 | */ |
107 | ||
108 | #include "config.h" | |
109 | #include "system.h" | |
110 | #include "coretypes.h" | |
111 | #include "tm.h" | |
4d648807 | 112 | #include "tree.h" |
eefe9a99 JH |
113 | #include "cgraph.h" |
114 | #include "tree-pass.h" | |
115 | #include "ggc.h" | |
116 | #include "pointer-set.h" | |
117 | #include "target.h" | |
118 | #include "hash-table.h" | |
119 | #include "tree-pretty-print.h" | |
120 | #include "ipa-utils.h" | |
121 | #include "gimple.h" | |
bbc9396b | 122 | #include "ipa-inline.h" |
61a74079 | 123 | #include "diagnostic.h" |
eefe9a99 | 124 | |
0e1474e5 JH |
125 | /* Pointer set of all call targets appearing in the cache. */ |
126 | static pointer_set_t *cached_polymorphic_call_targets; | |
127 | ||
eefe9a99 JH |
128 | /* The node of type inheritance graph. For each type unique in |
129 | One Defintion Rule (ODR) sense, we produce one node linking all | |
130 | main variants of types equivalent to it, bases and derived types. */ | |
131 | ||
132 | struct GTY(()) odr_type_d | |
133 | { | |
eefe9a99 JH |
134 | /* leader type. */ |
135 | tree type; | |
136 | /* All bases. */ | |
137 | vec<odr_type> GTY((skip)) bases; | |
138 | /* All derrived types with virtual methods seen in unit. */ | |
139 | vec<odr_type> GTY((skip)) derived_types; | |
0e1474e5 | 140 | |
61a74079 JH |
141 | /* All equivalent types, if more than one. */ |
142 | vec<tree, va_gc> *types; | |
143 | /* Set of all equivalent types, if NON-NULL. */ | |
144 | pointer_set_t * GTY((skip)) types_set; | |
145 | ||
0e1474e5 JH |
146 | /* Unique ID indexing the type in odr_types array. */ |
147 | int id; | |
eefe9a99 JH |
148 | /* Is it in anonymous namespace? */ |
149 | bool anonymous_namespace; | |
150 | }; | |
151 | ||
152 | ||
0e1474e5 JH |
153 | /* Return true if BINFO corresponds to a type with virtual methods. |
154 | ||
155 | Every type has several BINFOs. One is the BINFO associated by the type | |
156 | while other represents bases of derived types. The BINFOs representing | |
157 | bases do not have BINFO_VTABLE pointer set when this is the single | |
158 | inheritance (because vtables are shared). Look up the BINFO of type | |
159 | and check presence of its vtable. */ | |
eefe9a99 JH |
160 | |
161 | static inline bool | |
162 | polymorphic_type_binfo_p (tree binfo) | |
163 | { | |
164 | /* See if BINFO's type has an virtual table associtated with it. */ | |
165 | return BINFO_VTABLE (TYPE_BINFO (BINFO_TYPE (binfo))); | |
166 | } | |
167 | ||
168 | /* One Definition Rule hashtable helpers. */ | |
169 | ||
170 | struct odr_hasher | |
171 | { | |
172 | typedef odr_type_d value_type; | |
173 | typedef union tree_node compare_type; | |
174 | static inline hashval_t hash (const value_type *); | |
175 | static inline bool equal (const value_type *, const compare_type *); | |
176 | static inline void remove (value_type *); | |
177 | }; | |
178 | ||
179 | /* Produce hash based on type name. */ | |
180 | ||
181 | hashval_t | |
182 | hash_type_name (tree t) | |
183 | { | |
184 | gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t); | |
185 | ||
186 | /* If not in LTO, all main variants are unique, so we can do | |
187 | pointer hash. */ | |
188 | if (!in_lto_p) | |
189 | return htab_hash_pointer (t); | |
190 | ||
191 | /* Anonymous types are unique. */ | |
192 | if (type_in_anonymous_namespace_p (t)) | |
193 | return htab_hash_pointer (t); | |
194 | ||
61a74079 JH |
195 | /* For polymorphic types, we can simply hash the virtual table. */ |
196 | if (TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t))) | |
197 | { | |
198 | tree v = BINFO_VTABLE (TYPE_BINFO (t)); | |
199 | hashval_t hash = 0; | |
200 | ||
201 | if (TREE_CODE (v) == POINTER_PLUS_EXPR) | |
202 | { | |
203 | hash = TREE_INT_CST_LOW (TREE_OPERAND (v, 1)); | |
204 | v = TREE_OPERAND (TREE_OPERAND (v, 0), 0); | |
205 | } | |
206 | ||
207 | v = DECL_ASSEMBLER_NAME (v); | |
61a74079 JH |
208 | hash = iterative_hash_hashval_t (hash, htab_hash_pointer (v)); |
209 | return hash; | |
210 | } | |
211 | ||
eefe9a99 JH |
212 | /* Rest is not implemented yet. */ |
213 | gcc_unreachable (); | |
214 | } | |
215 | ||
216 | /* Return the computed hashcode for ODR_TYPE. */ | |
217 | ||
218 | inline hashval_t | |
219 | odr_hasher::hash (const value_type *odr_type) | |
220 | { | |
221 | return hash_type_name (odr_type->type); | |
222 | } | |
223 | ||
0e1474e5 | 224 | /* Compare types T1 and T2 and return true if they are |
eefe9a99 JH |
225 | equivalent. */ |
226 | ||
227 | inline bool | |
228 | odr_hasher::equal (const value_type *t1, const compare_type *ct2) | |
229 | { | |
230 | tree t2 = const_cast <tree> (ct2); | |
231 | ||
232 | gcc_checking_assert (TYPE_MAIN_VARIANT (ct2) == ct2); | |
233 | if (t1->type == t2) | |
234 | return true; | |
235 | if (!in_lto_p) | |
236 | return false; | |
237 | return types_same_for_odr (t1->type, t2); | |
238 | } | |
239 | ||
0e1474e5 | 240 | /* Free ODR type V. */ |
eefe9a99 JH |
241 | |
242 | inline void | |
243 | odr_hasher::remove (value_type *v) | |
244 | { | |
245 | v->bases.release (); | |
246 | v->derived_types.release (); | |
61a74079 JH |
247 | if (v->types_set) |
248 | pointer_set_destroy (v->types_set); | |
eefe9a99 JH |
249 | ggc_free (v); |
250 | } | |
251 | ||
252 | /* ODR type hash used to lookup ODR type based on tree type node. */ | |
253 | ||
254 | typedef hash_table <odr_hasher> odr_hash_type; | |
255 | static odr_hash_type odr_hash; | |
256 | ||
257 | /* ODR types are also stored into ODR_TYPE vector to allow consistent | |
258 | walking. Bases appear before derived types. Vector is garbage collected | |
259 | so we won't end up visiting empty types. */ | |
260 | ||
261 | static GTY(()) vec <odr_type, va_gc> *odr_types_ptr; | |
262 | #define odr_types (*odr_types_ptr) | |
263 | ||
61a74079 JH |
264 | /* TYPE is equivalent to VAL by ODR, but its tree representation differs |
265 | from VAL->type. This may happen in LTO where tree merging did not merge | |
266 | all variants of the same type. It may or may not mean the ODR violation. | |
267 | Add it to the list of duplicates and warn on some violations. */ | |
268 | ||
269 | static void | |
270 | add_type_duplicate (odr_type val, tree type) | |
271 | { | |
272 | if (!val->types_set) | |
273 | val->types_set = pointer_set_create (); | |
274 | ||
275 | /* See if this duplicate is new. */ | |
276 | if (!pointer_set_insert (val->types_set, type)) | |
277 | { | |
278 | bool merge = true; | |
279 | bool base_mismatch = false; | |
280 | gcc_assert (in_lto_p); | |
281 | vec_safe_push (val->types, type); | |
282 | unsigned int i,j; | |
283 | ||
284 | /* First we compare memory layout. */ | |
285 | if (!types_compatible_p (val->type, type)) | |
286 | { | |
287 | merge = false; | |
288 | if (BINFO_VTABLE (TYPE_BINFO (val->type)) | |
289 | && warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (type)), 0, | |
290 | "type %qD violates one definition rule ", | |
291 | type)) | |
292 | inform (DECL_SOURCE_LOCATION (TYPE_NAME (val->type)), | |
293 | "a type with the same name but different layout is " | |
294 | "defined in another translation unit"); | |
295 | debug_tree (BINFO_VTABLE (TYPE_BINFO (type))); | |
296 | debug_tree (BINFO_VTABLE (TYPE_BINFO (val->type))); | |
297 | if (cgraph_dump_file) | |
298 | { | |
299 | fprintf (cgraph_dump_file, "ODR violation or merging or ODR type bug?\n"); | |
300 | ||
301 | print_node (cgraph_dump_file, "", val->type, 0); | |
302 | putc ('\n',cgraph_dump_file); | |
303 | print_node (cgraph_dump_file, "", type, 0); | |
304 | putc ('\n',cgraph_dump_file); | |
305 | } | |
306 | } | |
307 | ||
308 | /* Next sanity check that bases are the same. If not, we will end | |
309 | up producing wrong answers. */ | |
310 | for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++) | |
311 | if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (TYPE_BINFO (type), i))) | |
312 | { | |
313 | odr_type base = get_odr_type | |
314 | (BINFO_TYPE | |
315 | (BINFO_BASE_BINFO (TYPE_BINFO (type), | |
316 | i)), | |
317 | true); | |
318 | if (val->bases.length () <= j || val->bases[j] != base) | |
319 | base_mismatch = true; | |
320 | j++; | |
321 | } | |
322 | if (base_mismatch) | |
323 | { | |
324 | merge = false; | |
325 | ||
326 | if (warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (type)), 0, | |
327 | "type %qD violates one definition rule ", | |
328 | type)) | |
329 | inform (DECL_SOURCE_LOCATION (TYPE_NAME (val->type)), | |
330 | "a type with the same name but different bases is " | |
331 | "defined in another translation unit"); | |
332 | if (cgraph_dump_file) | |
333 | { | |
334 | fprintf (cgraph_dump_file, "ODR bse violation or merging bug?\n"); | |
335 | ||
336 | print_node (cgraph_dump_file, "", val->type, 0); | |
337 | putc ('\n',cgraph_dump_file); | |
338 | print_node (cgraph_dump_file, "", type, 0); | |
339 | putc ('\n',cgraph_dump_file); | |
340 | } | |
341 | } | |
342 | ||
343 | /* Regularize things a little. During LTO same types may come with | |
344 | different BINFOs. Either because their virtual table was | |
345 | not merged by tree merging and only later at decl merging or | |
346 | because one type comes with external vtable, while other | |
347 | with internal. We want to merge equivalent binfos to conserve | |
348 | memory and streaming overhead. | |
349 | ||
350 | The external vtables are more harmful: they contain references | |
351 | to external declarations of methods that may be defined in the | |
352 | merged LTO unit. For this reason we absolutely need to remove | |
353 | them and replace by internal variants. Not doing so will lead | |
354 | to incomplete answers from possible_polymorphic_call_targets. */ | |
355 | if (!flag_ltrans && merge) | |
356 | { | |
357 | tree master_binfo = TYPE_BINFO (val->type); | |
358 | tree v1 = BINFO_VTABLE (master_binfo); | |
359 | tree v2 = BINFO_VTABLE (TYPE_BINFO (type)); | |
360 | ||
361 | if (TREE_CODE (v1) == POINTER_PLUS_EXPR) | |
362 | { | |
363 | gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR | |
364 | && operand_equal_p (TREE_OPERAND (v1, 1), | |
365 | TREE_OPERAND (v2, 1), 0)); | |
366 | v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0); | |
367 | v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0); | |
368 | } | |
369 | gcc_assert (DECL_ASSEMBLER_NAME (v1) | |
370 | == DECL_ASSEMBLER_NAME (v2)); | |
371 | ||
372 | if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2)) | |
373 | { | |
374 | unsigned int i; | |
375 | ||
376 | TYPE_BINFO (val->type) = TYPE_BINFO (type); | |
c3284718 | 377 | for (i = 0; i < val->types->length (); i++) |
61a74079 JH |
378 | { |
379 | if (TYPE_BINFO ((*val->types)[i]) | |
380 | == master_binfo) | |
381 | TYPE_BINFO ((*val->types)[i]) = TYPE_BINFO (type); | |
382 | } | |
383 | } | |
384 | else | |
385 | TYPE_BINFO (type) = master_binfo; | |
386 | } | |
387 | } | |
388 | } | |
389 | ||
eefe9a99 JH |
390 | /* Get ODR type hash entry for TYPE. If INSERT is true, create |
391 | possibly new entry. */ | |
392 | ||
393 | odr_type | |
394 | get_odr_type (tree type, bool insert) | |
395 | { | |
396 | odr_type_d **slot; | |
397 | odr_type val; | |
398 | hashval_t hash; | |
399 | ||
400 | type = TYPE_MAIN_VARIANT (type); | |
401 | gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type); | |
402 | hash = hash_type_name (type); | |
403 | slot = odr_hash.find_slot_with_hash (type, hash, insert ? INSERT : NO_INSERT); | |
404 | if (!slot) | |
405 | return NULL; | |
406 | ||
407 | /* See if we already have entry for type. */ | |
408 | if (*slot) | |
409 | { | |
410 | val = *slot; | |
411 | ||
61a74079 JH |
412 | /* With LTO we need to support multiple tree representation of |
413 | the same ODR type. */ | |
414 | if (val->type != type) | |
415 | add_type_duplicate (val, type); | |
eefe9a99 JH |
416 | } |
417 | else | |
418 | { | |
419 | tree binfo = TYPE_BINFO (type); | |
420 | unsigned int i; | |
421 | ||
422 | val = ggc_alloc_cleared_odr_type_d (); | |
423 | val->type = type; | |
424 | val->bases = vNULL; | |
425 | val->derived_types = vNULL; | |
0e1474e5 | 426 | val->anonymous_namespace = type_in_anonymous_namespace_p (type); |
eefe9a99 JH |
427 | *slot = val; |
428 | for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++) | |
429 | /* For now record only polymorphic types. other are | |
430 | pointless for devirtualization and we can not precisely | |
431 | determine ODR equivalency of these during LTO. */ | |
432 | if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i))) | |
433 | { | |
434 | odr_type base = get_odr_type (BINFO_TYPE (BINFO_BASE_BINFO (binfo, | |
435 | i)), | |
436 | true); | |
437 | base->derived_types.safe_push (val); | |
438 | val->bases.safe_push (base); | |
439 | } | |
440 | /* First record bases, then add into array so ids are increasing. */ | |
441 | if (odr_types_ptr) | |
c3284718 | 442 | val->id = odr_types.length (); |
eefe9a99 JH |
443 | vec_safe_push (odr_types_ptr, val); |
444 | } | |
445 | return val; | |
446 | } | |
447 | ||
448 | /* Dump ODR type T and all its derrived type. INDENT specify indentation for | |
449 | recusive printing. */ | |
450 | ||
451 | static void | |
452 | dump_odr_type (FILE *f, odr_type t, int indent=0) | |
453 | { | |
454 | unsigned int i; | |
455 | fprintf (f, "%*s type %i: ", indent * 2, "", t->id); | |
456 | print_generic_expr (f, t->type, TDF_SLIM); | |
0e1474e5 | 457 | fprintf (f, "%s\n", t->anonymous_namespace ? " (anonymous namespace)":""); |
eefe9a99 JH |
458 | if (TYPE_NAME (t->type)) |
459 | { | |
460 | fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "", | |
461 | DECL_SOURCE_FILE (TYPE_NAME (t->type)), | |
462 | DECL_SOURCE_LINE (TYPE_NAME (t->type))); | |
463 | } | |
c3284718 | 464 | if (t->bases.length ()) |
eefe9a99 JH |
465 | { |
466 | fprintf (f, "%*s base odr type ids: ", indent * 2, ""); | |
c3284718 | 467 | for (i = 0; i < t->bases.length (); i++) |
eefe9a99 JH |
468 | fprintf (f, " %i", t->bases[i]->id); |
469 | fprintf (f, "\n"); | |
470 | } | |
c3284718 | 471 | if (t->derived_types.length ()) |
eefe9a99 JH |
472 | { |
473 | fprintf (f, "%*s derived types:\n", indent * 2, ""); | |
c3284718 | 474 | for (i = 0; i < t->derived_types.length (); i++) |
eefe9a99 JH |
475 | dump_odr_type (f, t->derived_types[i], indent + 1); |
476 | } | |
477 | fprintf (f, "\n"); | |
478 | } | |
479 | ||
480 | /* Dump the type inheritance graph. */ | |
481 | ||
482 | static void | |
483 | dump_type_inheritance_graph (FILE *f) | |
484 | { | |
485 | unsigned int i; | |
0e1474e5 JH |
486 | if (!odr_types_ptr) |
487 | return; | |
eefe9a99 | 488 | fprintf (f, "\n\nType inheritance graph:\n"); |
c3284718 | 489 | for (i = 0; i < odr_types.length (); i++) |
eefe9a99 | 490 | { |
c3284718 | 491 | if (odr_types[i]->bases.length () == 0) |
eefe9a99 JH |
492 | dump_odr_type (f, odr_types[i]); |
493 | } | |
c3284718 | 494 | for (i = 0; i < odr_types.length (); i++) |
61a74079 | 495 | { |
c3284718 | 496 | if (odr_types[i]->types && odr_types[i]->types->length ()) |
61a74079 JH |
497 | { |
498 | unsigned int j; | |
499 | fprintf (f, "Duplicate tree types for odr type %i\n", i); | |
500 | print_node (f, "", odr_types[i]->type, 0); | |
c3284718 | 501 | for (j = 0; j < odr_types[i]->types->length (); j++) |
61a74079 JH |
502 | { |
503 | tree t; | |
504 | fprintf (f, "duplicate #%i\n", j); | |
505 | print_node (f, "", (*odr_types[i]->types)[j], 0); | |
506 | t = (*odr_types[i]->types)[j]; | |
507 | while (TYPE_P (t) && TYPE_CONTEXT (t)) | |
508 | { | |
509 | t = TYPE_CONTEXT (t); | |
510 | print_node (f, "", t, 0); | |
511 | } | |
512 | putc ('\n',f); | |
513 | } | |
514 | } | |
515 | } | |
eefe9a99 JH |
516 | } |
517 | ||
518 | /* Given method type T, return type of class it belongs to. | |
519 | Lookup this pointer and get its type. */ | |
520 | ||
64cbf23d | 521 | tree |
eefe9a99 JH |
522 | method_class_type (tree t) |
523 | { | |
524 | tree first_parm_type = TREE_VALUE (TYPE_ARG_TYPES (t)); | |
525 | ||
526 | return TREE_TYPE (first_parm_type); | |
527 | } | |
528 | ||
529 | /* Initialize IPA devirt and build inheritance tree graph. */ | |
530 | ||
531 | void | |
532 | build_type_inheritance_graph (void) | |
533 | { | |
534 | struct cgraph_node *n; | |
535 | FILE *inheritance_dump_file; | |
536 | int flags; | |
537 | ||
538 | if (odr_hash.is_created ()) | |
539 | return; | |
540 | timevar_push (TV_IPA_INHERITANCE); | |
541 | inheritance_dump_file = dump_begin (TDI_inheritance, &flags); | |
542 | odr_hash.create (23); | |
543 | ||
544 | /* We reconstruct the graph starting of types of all methods seen in the | |
545 | the unit. */ | |
546 | FOR_EACH_FUNCTION (n) | |
67348ccc DM |
547 | if (DECL_VIRTUAL_P (n->decl) |
548 | && symtab_real_symbol_p (n)) | |
549 | get_odr_type (method_class_type (TREE_TYPE (n->decl)), true); | |
eefe9a99 JH |
550 | if (inheritance_dump_file) |
551 | { | |
552 | dump_type_inheritance_graph (inheritance_dump_file); | |
553 | dump_end (TDI_inheritance, inheritance_dump_file); | |
554 | } | |
555 | timevar_pop (TV_IPA_INHERITANCE); | |
556 | } | |
557 | ||
558 | /* If TARGET has associated node, record it in the NODES array. */ | |
559 | ||
560 | static void | |
561 | maybe_record_node (vec <cgraph_node *> &nodes, | |
562 | tree target, pointer_set_t *inserted) | |
563 | { | |
564 | struct cgraph_node *target_node; | |
565 | enum built_in_function fcode; | |
566 | ||
567 | if (target | |
568 | /* Those are used to mark impossible scenarios. */ | |
569 | && (fcode = DECL_FUNCTION_CODE (target)) | |
570 | != BUILT_IN_UNREACHABLE | |
571 | && fcode != BUILT_IN_TRAP | |
572 | && !pointer_set_insert (inserted, target) | |
573 | && (target_node = cgraph_get_node (target)) != NULL | |
3462aa02 | 574 | && (TREE_PUBLIC (target) |
67348ccc DM |
575 | || target_node->definition) |
576 | && symtab_real_symbol_p (target_node)) | |
0e1474e5 JH |
577 | { |
578 | pointer_set_insert (cached_polymorphic_call_targets, | |
579 | target_node); | |
580 | nodes.safe_push (target_node); | |
581 | } | |
eefe9a99 JH |
582 | } |
583 | ||
584 | /* See if BINFO's type match OTR_TYPE. If so, lookup method | |
585 | in vtable of TYPE_BINFO and insert method to NODES array. | |
586 | Otherwise recurse to base BINFOs. | |
587 | This match what get_binfo_at_offset does, but with offset | |
588 | being unknown. | |
589 | ||
590 | TYPE_BINFO is binfo holding an virtual table matching | |
591 | BINFO's type. In the case of single inheritance, this | |
592 | is binfo of BINFO's type ancestor (vtable is shared), | |
593 | otherwise it is binfo of BINFO's type. | |
594 | ||
595 | MATCHED_VTABLES tracks virtual tables we already did lookup | |
596 | for virtual function in. | |
3462aa02 JH |
597 | |
598 | ANONYMOUS is true if BINFO is part of anonymous namespace. | |
eefe9a99 JH |
599 | */ |
600 | ||
601 | static void | |
602 | record_binfo (vec <cgraph_node *> &nodes, | |
603 | tree binfo, | |
604 | tree otr_type, | |
605 | tree type_binfo, | |
606 | HOST_WIDE_INT otr_token, | |
607 | pointer_set_t *inserted, | |
3462aa02 JH |
608 | pointer_set_t *matched_vtables, |
609 | bool anonymous) | |
eefe9a99 JH |
610 | { |
611 | tree type = BINFO_TYPE (binfo); | |
612 | int i; | |
613 | tree base_binfo; | |
614 | ||
615 | gcc_checking_assert (BINFO_VTABLE (type_binfo)); | |
616 | ||
617 | if (types_same_for_odr (type, otr_type) | |
618 | && !pointer_set_insert (matched_vtables, BINFO_VTABLE (type_binfo))) | |
619 | { | |
3462aa02 JH |
620 | /* For types in anonymous namespace first check if the respective vtable |
621 | is alive. If not, we know the type can't be called. */ | |
622 | if (!flag_ltrans && anonymous) | |
623 | { | |
624 | tree vtable = BINFO_VTABLE (type_binfo); | |
625 | struct varpool_node *vnode; | |
626 | ||
627 | if (TREE_CODE (vtable) == POINTER_PLUS_EXPR) | |
628 | vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0); | |
629 | vnode = varpool_get_node (vtable); | |
67348ccc | 630 | if (!vnode || !vnode->definition) |
3462aa02 JH |
631 | return; |
632 | } | |
eefe9a99 JH |
633 | tree target = gimple_get_virt_method_for_binfo (otr_token, type_binfo); |
634 | if (target) | |
635 | maybe_record_node (nodes, target, inserted); | |
636 | return; | |
637 | } | |
638 | ||
639 | /* Walk bases. */ | |
640 | for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) | |
641 | /* Walking bases that have no virtual method is pointless excercise. */ | |
642 | if (polymorphic_type_binfo_p (base_binfo)) | |
643 | record_binfo (nodes, base_binfo, otr_type, | |
0e1474e5 JH |
644 | /* In the case of single inheritance, the virtual table |
645 | is shared with the outer type. */ | |
eefe9a99 JH |
646 | BINFO_VTABLE (base_binfo) ? base_binfo : type_binfo, |
647 | otr_token, inserted, | |
3462aa02 | 648 | matched_vtables, anonymous); |
eefe9a99 JH |
649 | } |
650 | ||
651 | /* Lookup virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN) | |
652 | of TYPE, insert them to NODES, recurse into derived nodes. | |
653 | INSERTED is used to avoid duplicate insertions of methods into NODES. | |
654 | MATCHED_VTABLES are used to avoid duplicate walking vtables. */ | |
655 | ||
656 | static void | |
657 | possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes, | |
658 | pointer_set_t *inserted, | |
659 | pointer_set_t *matched_vtables, | |
660 | tree otr_type, | |
661 | odr_type type, | |
662 | HOST_WIDE_INT otr_token) | |
663 | { | |
664 | tree binfo = TYPE_BINFO (type->type); | |
665 | unsigned int i; | |
666 | ||
667 | record_binfo (nodes, binfo, otr_type, binfo, otr_token, inserted, | |
3462aa02 | 668 | matched_vtables, type->anonymous_namespace); |
c3284718 | 669 | for (i = 0; i < type->derived_types.length (); i++) |
eefe9a99 JH |
670 | possible_polymorphic_call_targets_1 (nodes, inserted, |
671 | matched_vtables, | |
672 | otr_type, | |
673 | type->derived_types[i], | |
674 | otr_token); | |
675 | } | |
676 | ||
677 | /* Cache of queries for polymorphic call targets. | |
678 | ||
679 | Enumerating all call targets may get expensive when there are many | |
680 | polymorphic calls in the program, so we memoize all the previous | |
681 | queries and avoid duplicated work. */ | |
682 | ||
683 | struct polymorphic_call_target_d | |
684 | { | |
685 | odr_type type; | |
686 | HOST_WIDE_INT otr_token; | |
687 | vec <cgraph_node *> targets; | |
688 | }; | |
689 | ||
690 | /* Polymorphic call target cache helpers. */ | |
691 | ||
692 | struct polymorphic_call_target_hasher | |
693 | { | |
694 | typedef polymorphic_call_target_d value_type; | |
695 | typedef polymorphic_call_target_d compare_type; | |
696 | static inline hashval_t hash (const value_type *); | |
697 | static inline bool equal (const value_type *, const compare_type *); | |
698 | static inline void remove (value_type *); | |
699 | }; | |
700 | ||
701 | /* Return the computed hashcode for ODR_QUERY. */ | |
702 | ||
703 | inline hashval_t | |
704 | polymorphic_call_target_hasher::hash (const value_type *odr_query) | |
705 | { | |
706 | return iterative_hash_hashval_t (odr_query->type->id, | |
707 | odr_query->otr_token); | |
708 | } | |
709 | ||
710 | /* Compare cache entries T1 and T2. */ | |
711 | ||
712 | inline bool | |
713 | polymorphic_call_target_hasher::equal (const value_type *t1, | |
714 | const compare_type *t2) | |
715 | { | |
716 | return t1->type == t2->type && t1->otr_token == t2->otr_token; | |
717 | } | |
718 | ||
719 | /* Remove entry in polymorphic call target cache hash. */ | |
720 | ||
721 | inline void | |
722 | polymorphic_call_target_hasher::remove (value_type *v) | |
723 | { | |
724 | v->targets.release (); | |
725 | free (v); | |
726 | } | |
727 | ||
728 | /* Polymorphic call target query cache. */ | |
729 | ||
730 | typedef hash_table <polymorphic_call_target_hasher> | |
731 | polymorphic_call_target_hash_type; | |
732 | static polymorphic_call_target_hash_type polymorphic_call_target_hash; | |
eefe9a99 JH |
733 | |
734 | /* Destroy polymorphic call target query cache. */ | |
735 | ||
736 | static void | |
737 | free_polymorphic_call_targets_hash () | |
738 | { | |
0e1474e5 JH |
739 | if (cached_polymorphic_call_targets) |
740 | { | |
741 | polymorphic_call_target_hash.dispose (); | |
742 | pointer_set_destroy (cached_polymorphic_call_targets); | |
743 | cached_polymorphic_call_targets = NULL; | |
744 | } | |
eefe9a99 JH |
745 | } |
746 | ||
747 | /* When virtual function is removed, we may need to flush the cache. */ | |
748 | ||
749 | static void | |
750 | devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED) | |
751 | { | |
0e1474e5 JH |
752 | if (cached_polymorphic_call_targets |
753 | && pointer_set_contains (cached_polymorphic_call_targets, n)) | |
eefe9a99 JH |
754 | free_polymorphic_call_targets_hash (); |
755 | } | |
756 | ||
3462aa02 JH |
757 | /* When virtual table is removed, we may need to flush the cache. */ |
758 | ||
759 | static void | |
760 | devirt_variable_node_removal_hook (struct varpool_node *n, | |
761 | void *d ATTRIBUTE_UNUSED) | |
762 | { | |
763 | if (cached_polymorphic_call_targets | |
67348ccc DM |
764 | && DECL_VIRTUAL_P (n->decl) |
765 | && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl))) | |
3462aa02 JH |
766 | free_polymorphic_call_targets_hash (); |
767 | } | |
768 | ||
eefe9a99 JH |
769 | /* Return vector containing possible targets of polymorphic call of type |
770 | OTR_TYPE caling method OTR_TOKEN with OFFSET. If FINALp is non-NULL, | |
771 | store true if the list is complette. | |
772 | CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry | |
773 | in the target cache. If user needs to visit every target list | |
774 | just once, it can memoize them. | |
775 | ||
776 | Returned vector is placed into cache. It is NOT caller's responsibility | |
777 | to free it. The vector can be freed on cgraph_remove_node call if | |
778 | the particular node is a virtual function present in the cache. */ | |
779 | ||
780 | vec <cgraph_node *> | |
781 | possible_polymorphic_call_targets (tree otr_type, | |
782 | HOST_WIDE_INT otr_token, | |
783 | bool *finalp, | |
784 | void **cache_token) | |
785 | { | |
786 | static struct cgraph_node_hook_list *node_removal_hook_holder; | |
787 | pointer_set_t *inserted; | |
788 | pointer_set_t *matched_vtables; | |
789 | vec <cgraph_node *> nodes=vNULL; | |
790 | odr_type type; | |
791 | polymorphic_call_target_d key; | |
792 | polymorphic_call_target_d **slot; | |
793 | unsigned int i; | |
794 | tree binfo, target; | |
795 | ||
796 | if (finalp) | |
797 | *finalp = false; | |
798 | ||
799 | type = get_odr_type (otr_type, false); | |
800 | /* If we do not have type in our hash it means we never seen any method | |
801 | in it. */ | |
802 | if (!type) | |
803 | return nodes; | |
804 | ||
805 | /* For anonymous namespace types we can attempt to build full type. | |
806 | All derivations must be in this unit. */ | |
807 | if (type->anonymous_namespace && finalp && !flag_ltrans) | |
808 | *finalp = true; | |
809 | ||
810 | /* Initialize query cache. */ | |
811 | if (!cached_polymorphic_call_targets) | |
812 | { | |
813 | cached_polymorphic_call_targets = pointer_set_create (); | |
814 | polymorphic_call_target_hash.create (23); | |
815 | if (!node_removal_hook_holder) | |
3462aa02 JH |
816 | { |
817 | node_removal_hook_holder = | |
818 | cgraph_add_node_removal_hook (&devirt_node_removal_hook, NULL); | |
819 | varpool_add_node_removal_hook (&devirt_variable_node_removal_hook, | |
820 | NULL); | |
821 | } | |
eefe9a99 JH |
822 | } |
823 | ||
824 | /* Lookup cached answer. */ | |
825 | key.type = type; | |
826 | key.otr_token = otr_token; | |
827 | slot = polymorphic_call_target_hash.find_slot (&key, INSERT); | |
828 | if (cache_token) | |
829 | *cache_token = (void *)*slot; | |
830 | if (*slot) | |
831 | return (*slot)->targets; | |
832 | ||
833 | /* Do actual search. */ | |
834 | timevar_push (TV_IPA_VIRTUAL_CALL); | |
835 | *slot = XCNEW (polymorphic_call_target_d); | |
836 | if (cache_token) | |
837 | *cache_token = (void *)*slot; | |
838 | (*slot)->type = type; | |
839 | (*slot)->otr_token = otr_token; | |
840 | ||
841 | inserted = pointer_set_create (); | |
842 | matched_vtables = pointer_set_create (); | |
843 | ||
844 | /* First see virtual method of type itself. */ | |
845 | ||
846 | binfo = TYPE_BINFO (type->type); | |
847 | target = gimple_get_virt_method_for_binfo (otr_token, binfo); | |
848 | if (target) | |
849 | maybe_record_node (nodes, target, inserted); | |
850 | pointer_set_insert (matched_vtables, BINFO_VTABLE (binfo)); | |
851 | ||
852 | /* TODO: If method is final, we can stop here and signaize that | |
853 | list is final. We need C++ FE to pass our info about final | |
854 | methods and classes. */ | |
855 | ||
856 | /* Walk recursively all derived types. Here we need to lookup proper basetype | |
857 | via their BINFO walk that is done by record_binfo */ | |
c3284718 | 858 | for (i = 0; i < type->derived_types.length (); i++) |
eefe9a99 JH |
859 | possible_polymorphic_call_targets_1 (nodes, inserted, |
860 | matched_vtables, | |
861 | otr_type, type->derived_types[i], | |
862 | otr_token); | |
863 | (*slot)->targets = nodes; | |
864 | ||
865 | pointer_set_destroy (inserted); | |
866 | pointer_set_destroy (matched_vtables); | |
867 | timevar_pop (TV_IPA_VIRTUAL_CALL); | |
868 | return nodes; | |
869 | } | |
870 | ||
871 | /* Dump all possible targets of a polymorphic call. */ | |
872 | ||
873 | void | |
874 | dump_possible_polymorphic_call_targets (FILE *f, | |
875 | tree otr_type, | |
876 | HOST_WIDE_INT otr_token) | |
877 | { | |
878 | vec <cgraph_node *> targets; | |
879 | bool final; | |
880 | odr_type type = get_odr_type (otr_type, false); | |
881 | unsigned int i; | |
882 | ||
883 | if (!type) | |
884 | return; | |
885 | targets = possible_polymorphic_call_targets (otr_type, otr_token, | |
886 | &final); | |
887 | fprintf (f, "Targets of polymorphic call of type %i ", type->id); | |
888 | print_generic_expr (f, type->type, TDF_SLIM); | |
889 | fprintf (f, " token %i%s:", | |
890 | (int)otr_token, | |
891 | final ? " (full list)" : " (partial list, may call to other unit)"); | |
892 | for (i = 0; i < targets.length (); i++) | |
893 | fprintf (f, " %s/%i", cgraph_node_name (targets[i]), | |
67348ccc | 894 | targets[i]->order); |
eefe9a99 JH |
895 | fprintf (f, "\n"); |
896 | } | |
897 | ||
0e1474e5 JH |
898 | |
899 | /* Return true if N can be possibly target of a polymorphic call of | |
900 | OTR_TYPE/OTR_TOKEN. */ | |
901 | ||
902 | bool | |
903 | possible_polymorphic_call_target_p (tree otr_type, | |
904 | HOST_WIDE_INT otr_token, | |
905 | struct cgraph_node *n) | |
906 | { | |
907 | vec <cgraph_node *> targets; | |
908 | unsigned int i; | |
450ad0cd | 909 | bool final; |
0e1474e5 JH |
910 | |
911 | if (!odr_hash.is_created ()) | |
912 | return true; | |
450ad0cd | 913 | targets = possible_polymorphic_call_targets (otr_type, otr_token, &final); |
0e1474e5 JH |
914 | for (i = 0; i < targets.length (); i++) |
915 | if (n == targets[i]) | |
916 | return true; | |
450ad0cd JH |
917 | |
918 | /* At a moment we allow middle end to dig out new external declarations | |
919 | as a targets of polymorphic calls. */ | |
67348ccc | 920 | if (!final && !n->definition) |
450ad0cd | 921 | return true; |
0e1474e5 JH |
922 | return false; |
923 | } | |
924 | ||
925 | ||
926 | /* After callgraph construction new external nodes may appear. | |
927 | Add them into the graph. */ | |
928 | ||
929 | void | |
930 | update_type_inheritance_graph (void) | |
931 | { | |
932 | struct cgraph_node *n; | |
933 | ||
934 | if (!odr_hash.is_created ()) | |
935 | return; | |
936 | free_polymorphic_call_targets_hash (); | |
937 | timevar_push (TV_IPA_INHERITANCE); | |
938 | /* We reconstruct the graph starting of types of all methods seen in the | |
939 | the unit. */ | |
940 | FOR_EACH_FUNCTION (n) | |
67348ccc DM |
941 | if (DECL_VIRTUAL_P (n->decl) |
942 | && !n->definition | |
943 | && symtab_real_symbol_p (n)) | |
944 | get_odr_type (method_class_type (TREE_TYPE (n->decl)), true); | |
0e1474e5 JH |
945 | timevar_pop (TV_IPA_INHERITANCE); |
946 | } | |
bbc9396b JH |
947 | |
948 | ||
949 | /* Return true if N looks like likely target of a polymorphic call. | |
950 | Rule out cxa_pure_virtual, noreturns, function declared cold and | |
951 | other obvious cases. */ | |
952 | ||
953 | bool | |
954 | likely_target_p (struct cgraph_node *n) | |
955 | { | |
956 | int flags; | |
957 | /* cxa_pure_virtual and similar things are not likely. */ | |
67348ccc | 958 | if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE) |
bbc9396b | 959 | return false; |
67348ccc | 960 | flags = flags_from_decl_or_type (n->decl); |
bbc9396b JH |
961 | if (flags & ECF_NORETURN) |
962 | return false; | |
963 | if (lookup_attribute ("cold", | |
67348ccc | 964 | DECL_ATTRIBUTES (n->decl))) |
bbc9396b JH |
965 | return false; |
966 | if (n->frequency < NODE_FREQUENCY_NORMAL) | |
967 | return false; | |
968 | return true; | |
969 | } | |
970 | ||
971 | /* The ipa-devirt pass. | |
3462aa02 JH |
972 | When polymorphic call has only one likely target in the unit, |
973 | turn it into speculative call. */ | |
bbc9396b JH |
974 | |
975 | static unsigned int | |
976 | ipa_devirt (void) | |
977 | { | |
978 | struct cgraph_node *n; | |
979 | struct pointer_set_t *bad_call_targets = pointer_set_create (); | |
980 | struct cgraph_edge *e; | |
981 | ||
982 | int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0; | |
983 | int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0; | |
984 | int nwrong = 0, nok = 0, nexternal = 0;; | |
985 | ||
986 | FOR_EACH_DEFINED_FUNCTION (n) | |
987 | { | |
988 | bool update = false; | |
989 | if (dump_file && n->indirect_calls) | |
990 | fprintf (dump_file, "\n\nProcesing function %s/%i\n", | |
67348ccc | 991 | cgraph_node_name (n), n->order); |
bbc9396b JH |
992 | for (e = n->indirect_calls; e; e = e->next_callee) |
993 | if (e->indirect_info->polymorphic) | |
994 | { | |
995 | struct cgraph_node *likely_target = NULL; | |
996 | void *cache_token; | |
997 | bool final; | |
998 | vec <cgraph_node *>targets | |
999 | = possible_polymorphic_call_targets | |
1000 | (e, &final, &cache_token); | |
1001 | unsigned int i; | |
1002 | ||
1003 | if (dump_file) | |
1004 | dump_possible_polymorphic_call_targets | |
1005 | (dump_file, e); | |
3462aa02 | 1006 | |
bbc9396b JH |
1007 | npolymorphic++; |
1008 | ||
bbc9396b JH |
1009 | if (!cgraph_maybe_hot_edge_p (e)) |
1010 | { | |
1011 | if (dump_file) | |
1012 | fprintf (dump_file, "Call is cold\n"); | |
1013 | ncold++; | |
1014 | continue; | |
1015 | } | |
1016 | if (e->speculative) | |
1017 | { | |
1018 | if (dump_file) | |
1019 | fprintf (dump_file, "Call is aready speculated\n"); | |
1020 | nspeculated++; | |
1021 | ||
1022 | /* When dumping see if we agree with speculation. */ | |
1023 | if (!dump_file) | |
1024 | continue; | |
1025 | } | |
1026 | if (pointer_set_contains (bad_call_targets, | |
1027 | cache_token)) | |
1028 | { | |
1029 | if (dump_file) | |
1030 | fprintf (dump_file, "Target list is known to be useless\n"); | |
1031 | nmultiple++; | |
1032 | continue; | |
1033 | } | |
c3284718 | 1034 | for (i = 0; i < targets.length (); i++) |
bbc9396b JH |
1035 | if (likely_target_p (targets[i])) |
1036 | { | |
1037 | if (likely_target) | |
1038 | { | |
1039 | likely_target = NULL; | |
1040 | if (dump_file) | |
1041 | fprintf (dump_file, "More than one likely target\n"); | |
1042 | nmultiple++; | |
1043 | break; | |
1044 | } | |
1045 | likely_target = targets[i]; | |
1046 | } | |
1047 | if (!likely_target) | |
1048 | { | |
1049 | pointer_set_insert (bad_call_targets, cache_token); | |
1050 | continue; | |
1051 | } | |
1052 | /* This is reached only when dumping; check if we agree or disagree | |
1053 | with the speculation. */ | |
1054 | if (e->speculative) | |
1055 | { | |
1056 | struct cgraph_edge *e2; | |
1057 | struct ipa_ref *ref; | |
1058 | cgraph_speculative_call_info (e, e2, e, ref); | |
1059 | if (cgraph_function_or_thunk_node (e2->callee, NULL) | |
1060 | == cgraph_function_or_thunk_node (likely_target, NULL)) | |
1061 | { | |
1062 | fprintf (dump_file, "We agree with speculation\n"); | |
1063 | nok++; | |
1064 | } | |
1065 | else | |
1066 | { | |
1067 | fprintf (dump_file, "We disagree with speculation\n"); | |
1068 | nwrong++; | |
1069 | } | |
1070 | continue; | |
1071 | } | |
67348ccc | 1072 | if (!likely_target->definition) |
bbc9396b JH |
1073 | { |
1074 | if (dump_file) | |
1075 | fprintf (dump_file, "Target is not an definition\n"); | |
1076 | nnotdefined++; | |
1077 | continue; | |
1078 | } | |
1079 | /* Do not introduce new references to external symbols. While we | |
1080 | can handle these just well, it is common for programs to | |
1081 | incorrectly with headers defining methods they are linked | |
1082 | with. */ | |
67348ccc | 1083 | if (DECL_EXTERNAL (likely_target->decl)) |
bbc9396b JH |
1084 | { |
1085 | if (dump_file) | |
1086 | fprintf (dump_file, "Target is external\n"); | |
1087 | nexternal++; | |
1088 | continue; | |
1089 | } | |
1090 | if (cgraph_function_body_availability (likely_target) | |
1091 | <= AVAIL_OVERWRITABLE | |
67348ccc | 1092 | && symtab_can_be_discarded (likely_target)) |
bbc9396b JH |
1093 | { |
1094 | if (dump_file) | |
1095 | fprintf (dump_file, "Target is overwritable\n"); | |
1096 | noverwritable++; | |
1097 | continue; | |
1098 | } | |
1099 | else | |
1100 | { | |
1101 | if (dump_file) | |
1102 | fprintf (dump_file, | |
1103 | "Speculatively devirtualizing call in %s/%i to %s/%i\n", | |
67348ccc | 1104 | cgraph_node_name (n), n->order, |
bbc9396b | 1105 | cgraph_node_name (likely_target), |
67348ccc DM |
1106 | likely_target->order); |
1107 | if (!symtab_can_be_discarded (likely_target)) | |
5b79657a JH |
1108 | { |
1109 | cgraph_node *alias; | |
1110 | alias = cgraph (symtab_nonoverwritable_alias | |
67348ccc | 1111 | (likely_target)); |
5b79657a JH |
1112 | if (alias) |
1113 | likely_target = alias; | |
1114 | } | |
bbc9396b JH |
1115 | nconverted++; |
1116 | update = true; | |
1117 | cgraph_turn_edge_to_speculative | |
1118 | (e, likely_target, e->count * 8 / 10, e->frequency * 8 / 10); | |
1119 | } | |
1120 | } | |
1121 | if (update) | |
1122 | inline_update_overall_summary (n); | |
1123 | } | |
1124 | pointer_set_destroy (bad_call_targets); | |
1125 | ||
1126 | if (dump_file) | |
1127 | fprintf (dump_file, | |
1128 | "%i polymorphic calls, %i devirtualized," | |
1129 | " %i speculatively devirtualized, %i cold\n" | |
1130 | "%i have multiple targets, %i overwritable," | |
1131 | " %i already speculated (%i agree, %i disagree)," | |
1132 | " %i external, %i not defined\n", | |
1133 | npolymorphic, ndevirtualized, nconverted, ncold, | |
1134 | nmultiple, noverwritable, nspeculated, nok, nwrong, | |
1135 | nexternal, nnotdefined); | |
1136 | return ndevirtualized ? TODO_remove_functions : 0; | |
1137 | } | |
1138 | ||
1139 | /* Gate for IPCP optimization. */ | |
1140 | ||
1141 | static bool | |
1142 | gate_ipa_devirt (void) | |
1143 | { | |
5bbcb888 | 1144 | return flag_devirtualize_speculatively && optimize; |
bbc9396b JH |
1145 | } |
1146 | ||
1147 | namespace { | |
1148 | ||
1149 | const pass_data pass_data_ipa_devirt = | |
1150 | { | |
1151 | IPA_PASS, /* type */ | |
1152 | "devirt", /* name */ | |
1153 | OPTGROUP_NONE, /* optinfo_flags */ | |
1154 | true, /* has_gate */ | |
1155 | true, /* has_execute */ | |
1156 | TV_IPA_DEVIRT, /* tv_id */ | |
1157 | 0, /* properties_required */ | |
1158 | 0, /* properties_provided */ | |
1159 | 0, /* properties_destroyed */ | |
1160 | 0, /* todo_flags_start */ | |
1161 | ( TODO_dump_symtab ), /* todo_flags_finish */ | |
1162 | }; | |
1163 | ||
1164 | class pass_ipa_devirt : public ipa_opt_pass_d | |
1165 | { | |
1166 | public: | |
c3284718 RS |
1167 | pass_ipa_devirt (gcc::context *ctxt) |
1168 | : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt, | |
1169 | NULL, /* generate_summary */ | |
1170 | NULL, /* write_summary */ | |
1171 | NULL, /* read_summary */ | |
1172 | NULL, /* write_optimization_summary */ | |
1173 | NULL, /* read_optimization_summary */ | |
1174 | NULL, /* stmt_fixup */ | |
1175 | 0, /* function_transform_todo_flags_start */ | |
1176 | NULL, /* function_transform */ | |
1177 | NULL) /* variable_transform */ | |
bbc9396b JH |
1178 | {} |
1179 | ||
1180 | /* opt_pass methods: */ | |
1181 | bool gate () { return gate_ipa_devirt (); } | |
1182 | unsigned int execute () { return ipa_devirt (); } | |
1183 | ||
1184 | }; // class pass_ipa_devirt | |
1185 | ||
1186 | } // anon namespace | |
1187 | ||
1188 | ipa_opt_pass_d * | |
1189 | make_pass_ipa_devirt (gcc::context *ctxt) | |
1190 | { | |
1191 | return new pass_ipa_devirt (ctxt); | |
1192 | } | |
1193 | ||
eefe9a99 | 1194 | #include "gt-ipa-devirt.h" |