]>
Commit | Line | Data |
---|---|---|
b84d4347 | 1 | /* Interprocedural Identical Code Folding pass |
5624e564 | 2 | Copyright (C) 2014-2015 Free Software Foundation, Inc. |
b84d4347 ML |
3 | |
4 | Contributed by Jan Hubicka <hubicka@ucw.cz> and Martin Liska <mliska@suse.cz> | |
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 | /* Interprocedural Identical Code Folding for functions and | |
23 | read-only variables. | |
24 | ||
25 | The goal of this transformation is to discover functions and read-only | |
26 | variables which do have exactly the same semantics. | |
27 | ||
28 | In case of functions, | |
29 | we could either create a virtual clone or do a simple function wrapper | |
30 | that will call equivalent function. If the function is just locally visible, | |
31 | all function calls can be redirected. For read-only variables, we create | |
32 | aliases if possible. | |
33 | ||
34 | Optimization pass arranges as follows: | |
35 | 1) All functions and read-only variables are visited and internal | |
36 | data structure, either sem_function or sem_variables is created. | |
37 | 2) For every symbol from the previous step, VAR_DECL and FUNCTION_DECL are | |
38 | saved and matched to corresponding sem_items. | |
39 | 3) These declaration are ignored for equality check and are solved | |
40 | by Value Numbering algorithm published by Alpert, Zadeck in 1992. | |
41 | 4) We compute hash value for each symbol. | |
42 | 5) Congruence classes are created based on hash value. If hash value are | |
43 | equal, equals function is called and symbols are deeply compared. | |
44 | We must prove that all SSA names, declarations and other items | |
45 | correspond. | |
46 | 6) Value Numbering is executed for these classes. At the end of the process | |
47 | all symbol members in remaining classes can be merged. | |
48 | 7) Merge operation creates alias in case of read-only variables. For | |
49 | callgraph node, we must decide if we can redirect local calls, | |
50 | create an alias or a thunk. | |
51 | ||
52 | */ | |
53 | ||
54 | #include "config.h" | |
55 | #include "system.h" | |
46305737 | 56 | #include <list> |
b84d4347 | 57 | #include "coretypes.h" |
60393bbc AM |
58 | #include "hash-set.h" |
59 | #include "machmode.h" | |
40e23961 MC |
60 | #include "vec.h" |
61 | #include "double-int.h" | |
62 | #include "input.h" | |
63 | #include "alias.h" | |
64 | #include "symtab.h" | |
65 | #include "options.h" | |
66 | #include "wide-int.h" | |
67 | #include "inchash.h" | |
68 | #include "tree.h" | |
69 | #include "fold-const.h" | |
70 | #include "predict.h" | |
60393bbc AM |
71 | #include "tm.h" |
72 | #include "hard-reg-set.h" | |
60393bbc AM |
73 | #include "function.h" |
74 | #include "dominance.h" | |
75 | #include "cfg.h" | |
b84d4347 ML |
76 | #include "basic-block.h" |
77 | #include "tree-ssa-alias.h" | |
78 | #include "internal-fn.h" | |
79 | #include "gimple-expr.h" | |
80 | #include "is-a.h" | |
81 | #include "gimple.h" | |
36566b39 PK |
82 | #include "hashtab.h" |
83 | #include "rtl.h" | |
84 | #include "flags.h" | |
85 | #include "statistics.h" | |
86 | #include "real.h" | |
87 | #include "fixed-value.h" | |
88 | #include "insn-config.h" | |
89 | #include "expmed.h" | |
90 | #include "dojump.h" | |
91 | #include "explow.h" | |
92 | #include "calls.h" | |
93 | #include "emit-rtl.h" | |
94 | #include "varasm.h" | |
95 | #include "stmt.h" | |
b84d4347 ML |
96 | #include "expr.h" |
97 | #include "gimple-iterator.h" | |
98 | #include "gimple-ssa.h" | |
99 | #include "tree-cfg.h" | |
100 | #include "tree-phinodes.h" | |
101 | #include "stringpool.h" | |
102 | #include "tree-ssanames.h" | |
103 | #include "tree-dfa.h" | |
104 | #include "tree-pass.h" | |
105 | #include "gimple-pretty-print.h" | |
c582198b AM |
106 | #include "hash-map.h" |
107 | #include "plugin-api.h" | |
108 | #include "ipa-ref.h" | |
109 | #include "cgraph.h" | |
110 | #include "alloc-pool.h" | |
dd912cb8 | 111 | #include "symbol-summary.h" |
c582198b | 112 | #include "ipa-prop.h" |
b84d4347 ML |
113 | #include "ipa-inline.h" |
114 | #include "cfgloop.h" | |
115 | #include "except.h" | |
116 | #include "hash-table.h" | |
117 | #include "coverage.h" | |
118 | #include "attribs.h" | |
119 | #include "print-tree.h" | |
120 | #include "lto-streamer.h" | |
121 | #include "data-streamer.h" | |
122 | #include "ipa-utils.h" | |
b84d4347 ML |
123 | #include "ipa-icf-gimple.h" |
124 | #include "ipa-icf.h" | |
46305737 | 125 | #include "stor-layout.h" |
b84d4347 ML |
126 | |
127 | using namespace ipa_icf_gimple; | |
128 | ||
129 | namespace ipa_icf { | |
5ebd0e61 ML |
130 | |
131 | /* Constructor. */ | |
132 | ||
133 | symbol_compare_collection::symbol_compare_collection (symtab_node *node) | |
134 | { | |
135 | m_references.create (0); | |
136 | m_interposables.create (0); | |
137 | ||
138 | ipa_ref *ref; | |
139 | ||
140 | if (is_a <varpool_node *> (node) && DECL_VIRTUAL_P (node->decl)) | |
141 | return; | |
142 | ||
143 | for (unsigned i = 0; i < node->num_references (); i++) | |
144 | { | |
145 | ref = node->iterate_reference (i, ref); | |
146 | if (ref->address_matters_p ()) | |
147 | m_references.safe_push (ref->referred); | |
148 | ||
149 | if (ref->referred->get_availability () <= AVAIL_INTERPOSABLE) | |
150 | { | |
0a7246ee | 151 | if (ref->address_matters_p ()) |
5ebd0e61 ML |
152 | m_references.safe_push (ref->referred); |
153 | else | |
154 | m_interposables.safe_push (ref->referred); | |
155 | } | |
156 | } | |
157 | ||
158 | if (is_a <cgraph_node *> (node)) | |
159 | { | |
160 | cgraph_node *cnode = dyn_cast <cgraph_node *> (node); | |
161 | ||
162 | for (cgraph_edge *e = cnode->callees; e; e = e->next_callee) | |
163 | if (e->callee->get_availability () <= AVAIL_INTERPOSABLE) | |
164 | m_interposables.safe_push (e->callee); | |
165 | } | |
166 | } | |
167 | ||
b84d4347 ML |
168 | /* Constructor for key value pair, where _ITEM is key and _INDEX is a target. */ |
169 | ||
170 | sem_usage_pair::sem_usage_pair (sem_item *_item, unsigned int _index): | |
171 | item (_item), index (_index) | |
172 | { | |
173 | } | |
174 | ||
175 | /* Semantic item constructor for a node of _TYPE, where STACK is used | |
176 | for bitmap memory allocation. */ | |
177 | ||
178 | sem_item::sem_item (sem_item_type _type, | |
179 | bitmap_obstack *stack): type(_type), hash(0) | |
180 | { | |
181 | setup (stack); | |
182 | } | |
183 | ||
184 | /* Semantic item constructor for a node of _TYPE, where STACK is used | |
185 | for bitmap memory allocation. The item is based on symtab node _NODE | |
186 | with computed _HASH. */ | |
187 | ||
188 | sem_item::sem_item (sem_item_type _type, symtab_node *_node, | |
189 | hashval_t _hash, bitmap_obstack *stack): type(_type), | |
190 | node (_node), hash (_hash) | |
191 | { | |
192 | decl = node->decl; | |
193 | setup (stack); | |
194 | } | |
195 | ||
196 | /* Add reference to a semantic TARGET. */ | |
197 | ||
198 | void | |
199 | sem_item::add_reference (sem_item *target) | |
200 | { | |
201 | refs.safe_push (target); | |
202 | unsigned index = refs.length (); | |
203 | target->usages.safe_push (new sem_usage_pair(this, index)); | |
204 | bitmap_set_bit (target->usage_index_bitmap, index); | |
205 | refs_set.add (target->node); | |
206 | } | |
207 | ||
208 | /* Initialize internal data structures. Bitmap STACK is used for | |
209 | bitmap memory allocation process. */ | |
210 | ||
211 | void | |
212 | sem_item::setup (bitmap_obstack *stack) | |
213 | { | |
214 | gcc_checking_assert (node); | |
215 | ||
216 | refs.create (0); | |
217 | tree_refs.create (0); | |
218 | usages.create (0); | |
219 | usage_index_bitmap = BITMAP_ALLOC (stack); | |
220 | } | |
221 | ||
222 | sem_item::~sem_item () | |
223 | { | |
224 | for (unsigned i = 0; i < usages.length (); i++) | |
225 | delete usages[i]; | |
226 | ||
227 | refs.release (); | |
228 | tree_refs.release (); | |
229 | usages.release (); | |
230 | ||
231 | BITMAP_FREE (usage_index_bitmap); | |
232 | } | |
233 | ||
234 | /* Dump function for debugging purpose. */ | |
235 | ||
236 | DEBUG_FUNCTION void | |
237 | sem_item::dump (void) | |
238 | { | |
239 | if (dump_file) | |
240 | { | |
241 | fprintf (dump_file, "[%s] %s (%u) (tree:%p)\n", type == FUNC ? "func" : "var", | |
242 | name(), node->order, (void *) node->decl); | |
243 | fprintf (dump_file, " hash: %u\n", get_hash ()); | |
244 | fprintf (dump_file, " references: "); | |
245 | ||
246 | for (unsigned i = 0; i < refs.length (); i++) | |
247 | fprintf (dump_file, "%s%s ", refs[i]->name (), | |
248 | i < refs.length() - 1 ? "," : ""); | |
249 | ||
250 | fprintf (dump_file, "\n"); | |
251 | } | |
252 | } | |
253 | ||
f657d665 ML |
254 | /* Return true if target supports alias symbols. */ |
255 | ||
256 | bool | |
257 | sem_item::target_supports_symbol_aliases_p (void) | |
258 | { | |
259 | #if !defined (ASM_OUTPUT_DEF) || (!defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)) | |
260 | return false; | |
261 | #else | |
262 | return true; | |
263 | #endif | |
264 | } | |
265 | ||
b84d4347 ML |
266 | /* Semantic function constructor that uses STACK as bitmap memory stack. */ |
267 | ||
268 | sem_function::sem_function (bitmap_obstack *stack): sem_item (FUNC, stack), | |
269 | m_checker (NULL), m_compared_func (NULL) | |
270 | { | |
271 | arg_types.create (0); | |
272 | bb_sizes.create (0); | |
273 | bb_sorted.create (0); | |
274 | } | |
275 | ||
276 | /* Constructor based on callgraph node _NODE with computed hash _HASH. | |
277 | Bitmap STACK is used for memory allocation. */ | |
278 | sem_function::sem_function (cgraph_node *node, hashval_t hash, | |
279 | bitmap_obstack *stack): | |
280 | sem_item (FUNC, node, hash, stack), | |
281 | m_checker (NULL), m_compared_func (NULL) | |
282 | { | |
283 | arg_types.create (0); | |
284 | bb_sizes.create (0); | |
285 | bb_sorted.create (0); | |
286 | } | |
287 | ||
288 | sem_function::~sem_function () | |
289 | { | |
290 | for (unsigned i = 0; i < bb_sorted.length (); i++) | |
e27d328a | 291 | delete (bb_sorted[i]); |
b84d4347 ML |
292 | |
293 | arg_types.release (); | |
294 | bb_sizes.release (); | |
295 | bb_sorted.release (); | |
296 | } | |
297 | ||
298 | /* Calculates hash value based on a BASIC_BLOCK. */ | |
299 | ||
300 | hashval_t | |
301 | sem_function::get_bb_hash (const sem_bb *basic_block) | |
302 | { | |
303 | inchash::hash hstate; | |
304 | ||
305 | hstate.add_int (basic_block->nondbg_stmt_count); | |
306 | hstate.add_int (basic_block->edge_count); | |
307 | ||
308 | return hstate.end (); | |
309 | } | |
310 | ||
311 | /* References independent hash function. */ | |
312 | ||
313 | hashval_t | |
314 | sem_function::get_hash (void) | |
315 | { | |
316 | if(!hash) | |
317 | { | |
318 | inchash::hash hstate; | |
319 | hstate.add_int (177454); /* Random number for function type. */ | |
320 | ||
321 | hstate.add_int (arg_count); | |
322 | hstate.add_int (cfg_checksum); | |
323 | hstate.add_int (gcode_hash); | |
324 | ||
325 | for (unsigned i = 0; i < bb_sorted.length (); i++) | |
326 | hstate.merge_hash (get_bb_hash (bb_sorted[i])); | |
327 | ||
328 | for (unsigned i = 0; i < bb_sizes.length (); i++) | |
329 | hstate.add_int (bb_sizes[i]); | |
330 | ||
331 | hash = hstate.end (); | |
332 | } | |
333 | ||
334 | return hash; | |
335 | } | |
336 | ||
337 | /* For a given symbol table nodes N1 and N2, we check that FUNCTION_DECLs | |
338 | point to a same function. Comparison can be skipped if IGNORED_NODES | |
b6cddc7f | 339 | contains these nodes. ADDRESS indicate if address is taken. */ |
b84d4347 ML |
340 | |
341 | bool | |
46305737 | 342 | sem_item::compare_cgraph_references ( |
b6cddc7f ML |
343 | hash_map <symtab_node *, sem_item *> &ignored_nodes, |
344 | symtab_node *n1, symtab_node *n2, bool address) | |
b84d4347 | 345 | { |
b6cddc7f ML |
346 | enum availability avail1, avail2; |
347 | ||
fe75bd20 JH |
348 | if (n1 == n2) |
349 | return true; | |
350 | ||
351 | /* Merging two definitions with a reference to equivalent vtables, but | |
352 | belonging to a different type may result in ipa-polymorphic-call analysis | |
353 | giving a wrong answer about the dynamic type of instance. */ | |
354 | if (is_a <varpool_node *> (n1) | |
355 | && (DECL_VIRTUAL_P (n1->decl) || DECL_VIRTUAL_P (n2->decl)) | |
356 | && (DECL_VIRTUAL_P (n1->decl) != DECL_VIRTUAL_P (n2->decl) | |
357 | || !types_must_be_same_for_odr (DECL_CONTEXT (n1->decl), | |
358 | DECL_CONTEXT (n2->decl)))) | |
359 | return return_false_with_msg | |
360 | ("references to virtual tables can not be merged"); | |
361 | ||
b6cddc7f ML |
362 | if (address && n1->equal_address_to (n2) == 1) |
363 | return true; | |
364 | if (!address && n1->semantically_equivalent_p (n2)) | |
b84d4347 ML |
365 | return true; |
366 | ||
b6cddc7f ML |
367 | n1 = n1->ultimate_alias_target (&avail1); |
368 | n2 = n2->ultimate_alias_target (&avail2); | |
369 | ||
370 | if (avail1 >= AVAIL_INTERPOSABLE && ignored_nodes.get (n1) | |
371 | && avail2 >= AVAIL_INTERPOSABLE && ignored_nodes.get (n2)) | |
372 | return true; | |
b84d4347 ML |
373 | |
374 | return return_false_with_msg ("different references"); | |
375 | } | |
376 | ||
377 | /* If cgraph edges E1 and E2 are indirect calls, verify that | |
378 | ECF flags are the same. */ | |
379 | ||
380 | bool sem_function::compare_edge_flags (cgraph_edge *e1, cgraph_edge *e2) | |
381 | { | |
382 | if (e1->indirect_info && e2->indirect_info) | |
383 | { | |
384 | int e1_flags = e1->indirect_info->ecf_flags; | |
385 | int e2_flags = e2->indirect_info->ecf_flags; | |
386 | ||
387 | if (e1_flags != e2_flags) | |
388 | return return_false_with_msg ("ICF flags are different"); | |
389 | } | |
390 | else if (e1->indirect_info || e2->indirect_info) | |
391 | return false; | |
392 | ||
393 | return true; | |
394 | } | |
395 | ||
396 | /* Fast equality function based on knowledge known in WPA. */ | |
397 | ||
398 | bool | |
399 | sem_function::equals_wpa (sem_item *item, | |
400 | hash_map <symtab_node *, sem_item *> &ignored_nodes) | |
401 | { | |
402 | gcc_assert (item->type == FUNC); | |
403 | ||
404 | m_compared_func = static_cast<sem_function *> (item); | |
405 | ||
406 | if (arg_types.length () != m_compared_func->arg_types.length ()) | |
407 | return return_false_with_msg ("different number of arguments"); | |
408 | ||
c4234544 JH |
409 | /* Compare special function DECL attributes. */ |
410 | if (DECL_FUNCTION_PERSONALITY (decl) | |
411 | != DECL_FUNCTION_PERSONALITY (item->decl)) | |
412 | return return_false_with_msg ("function personalities are different"); | |
413 | ||
414 | if (DECL_DISREGARD_INLINE_LIMITS (decl) | |
415 | != DECL_DISREGARD_INLINE_LIMITS (item->decl)) | |
416 | return return_false_with_msg ("DECL_DISREGARD_INLINE_LIMITS are different"); | |
417 | ||
418 | if (DECL_DECLARED_INLINE_P (decl) != DECL_DECLARED_INLINE_P (item->decl)) | |
419 | return return_false_with_msg ("inline attributes are different"); | |
420 | ||
421 | if (DECL_IS_OPERATOR_NEW (decl) != DECL_IS_OPERATOR_NEW (item->decl)) | |
422 | return return_false_with_msg ("operator new flags are different"); | |
423 | ||
424 | if (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) | |
425 | != DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (item->decl)) | |
426 | return return_false_with_msg ("intrument function entry exit " | |
427 | "attributes are different"); | |
428 | ||
429 | if (DECL_NO_LIMIT_STACK (decl) != DECL_NO_LIMIT_STACK (item->decl)) | |
430 | return return_false_with_msg ("no stack limit attributes are different"); | |
431 | ||
060cfff4 JH |
432 | if (DECL_CXX_CONSTRUCTOR_P (decl) != DECL_CXX_CONSTRUCTOR_P (item->decl)) |
433 | return return_false_with_msg ("DELC_CXX_CONSTRUCTOR mismatch"); | |
434 | ||
435 | if (DECL_CXX_DESTRUCTOR_P (decl) != DECL_CXX_DESTRUCTOR_P (item->decl)) | |
436 | return return_false_with_msg ("DELC_CXX_DESTRUCTOR mismatch"); | |
437 | ||
c4234544 JH |
438 | if (flags_from_decl_or_type (decl) != flags_from_decl_or_type (item->decl)) |
439 | return return_false_with_msg ("decl_or_type flags are different"); | |
440 | ||
060cfff4 JH |
441 | /* Do not match polymorphic constructors of different types. They calls |
442 | type memory location for ipa-polymorphic-call and we do not want | |
443 | it to get confused by wrong type. */ | |
444 | if (DECL_CXX_CONSTRUCTOR_P (decl) | |
445 | && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) | |
446 | { | |
447 | if (TREE_CODE (TREE_TYPE (item->decl)) != METHOD_TYPE) | |
448 | return return_false_with_msg ("DECL_CXX_CONSTURCTOR type mismatch"); | |
449 | else if (!func_checker::compatible_polymorphic_types_p | |
450 | (method_class_type (TREE_TYPE (decl)), | |
451 | method_class_type (TREE_TYPE (item->decl)), false)) | |
452 | return return_false_with_msg ("ctor polymorphic type mismatch"); | |
453 | } | |
454 | ||
c4234544 JH |
455 | /* Checking function TARGET and OPTIMIZATION flags. */ |
456 | cl_target_option *tar1 = target_opts_for_fn (decl); | |
457 | cl_target_option *tar2 = target_opts_for_fn (item->decl); | |
458 | ||
459 | if (tar1 != tar2 && !cl_target_option_eq (tar1, tar2)) | |
460 | { | |
461 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
462 | { | |
463 | fprintf (dump_file, "target flags difference"); | |
464 | cl_target_option_print_diff (dump_file, 2, tar1, tar2); | |
465 | } | |
466 | ||
467 | return return_false_with_msg ("Target flags are different"); | |
468 | } | |
469 | ||
470 | cl_optimization *opt1 = opts_for_fn (decl); | |
471 | cl_optimization *opt2 = opts_for_fn (item->decl); | |
472 | ||
473 | if (opt1 != opt2 && memcmp (opt1, opt2, sizeof(cl_optimization))) | |
474 | { | |
475 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
476 | { | |
477 | fprintf (dump_file, "optimization flags difference"); | |
478 | cl_optimization_print_diff (dump_file, 2, opt1, opt2); | |
479 | } | |
480 | ||
481 | return return_false_with_msg ("optimization flags are different"); | |
482 | } | |
483 | ||
484 | /* Result type checking. */ | |
485 | if (!func_checker::compatible_types_p (result_type, | |
486 | m_compared_func->result_type)) | |
487 | return return_false_with_msg ("result types are different"); | |
488 | ||
b84d4347 ML |
489 | /* Checking types of arguments. */ |
490 | for (unsigned i = 0; i < arg_types.length (); i++) | |
491 | { | |
492 | /* This guard is here for function pointer with attributes (pr59927.c). */ | |
493 | if (!arg_types[i] || !m_compared_func->arg_types[i]) | |
494 | return return_false_with_msg ("NULL argument type"); | |
495 | ||
b84d4347 | 496 | if (!func_checker::compatible_types_p (arg_types[i], |
060cfff4 | 497 | m_compared_func->arg_types[i])) |
b84d4347 | 498 | return return_false_with_msg ("argument type is different"); |
fe75bd20 JH |
499 | if (POINTER_TYPE_P (arg_types[i]) |
500 | && (TYPE_RESTRICT (arg_types[i]) | |
501 | != TYPE_RESTRICT (m_compared_func->arg_types[i]))) | |
502 | return return_false_with_msg ("argument restrict flag mismatch"); | |
b84d4347 ML |
503 | } |
504 | ||
b84d4347 ML |
505 | if (node->num_references () != item->node->num_references ()) |
506 | return return_false_with_msg ("different number of references"); | |
507 | ||
fe75bd20 JH |
508 | if (comp_type_attributes (TREE_TYPE (decl), |
509 | TREE_TYPE (item->decl)) != 1) | |
510 | return return_false_with_msg ("different type attributes"); | |
511 | ||
060cfff4 JH |
512 | /* The type of THIS pointer type memory location for |
513 | ipa-polymorphic-call-analysis. */ | |
514 | if (opt_for_fn (decl, flag_devirtualize) | |
515 | && (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE | |
516 | || TREE_CODE (TREE_TYPE (item->decl)) == METHOD_TYPE) | |
517 | && (!flag_ipa_cp | |
518 | || ipa_is_param_used (IPA_NODE_REF (dyn_cast <cgraph_node *>(node)), | |
519 | 0)) | |
520 | && compare_polymorphic_p ()) | |
521 | { | |
522 | if (TREE_CODE (TREE_TYPE (decl)) != TREE_CODE (TREE_TYPE (item->decl))) | |
523 | return return_false_with_msg ("METHOD_TYPE and FUNCTION_TYPE mismatch"); | |
524 | if (!func_checker::compatible_polymorphic_types_p | |
525 | (method_class_type (TREE_TYPE (decl)), | |
526 | method_class_type (TREE_TYPE (item->decl)), false)) | |
527 | return return_false_with_msg ("THIS pointer ODR type mismatch"); | |
528 | } | |
529 | ||
b84d4347 ML |
530 | ipa_ref *ref = NULL, *ref2 = NULL; |
531 | for (unsigned i = 0; node->iterate_reference (i, ref); i++) | |
532 | { | |
533 | item->node->iterate_reference (i, ref2); | |
534 | ||
b6cddc7f ML |
535 | if (!compare_cgraph_references (ignored_nodes, ref->referred, |
536 | ref2->referred, | |
537 | ref->address_matters_p ())) | |
b84d4347 ML |
538 | return false; |
539 | } | |
540 | ||
541 | cgraph_edge *e1 = dyn_cast <cgraph_node *> (node)->callees; | |
542 | cgraph_edge *e2 = dyn_cast <cgraph_node *> (item->node)->callees; | |
543 | ||
544 | while (e1 && e2) | |
545 | { | |
b6cddc7f ML |
546 | if (!compare_cgraph_references (ignored_nodes, e1->callee, |
547 | e2->callee, false)) | |
b84d4347 ML |
548 | return false; |
549 | ||
550 | e1 = e1->next_callee; | |
551 | e2 = e2->next_callee; | |
552 | } | |
553 | ||
554 | if (e1 || e2) | |
555 | return return_false_with_msg ("different number of edges"); | |
556 | ||
557 | return true; | |
558 | } | |
559 | ||
560 | /* Returns true if the item equals to ITEM given as argument. */ | |
561 | ||
562 | bool | |
563 | sem_function::equals (sem_item *item, | |
564 | hash_map <symtab_node *, sem_item *> &ignored_nodes) | |
565 | { | |
566 | gcc_assert (item->type == FUNC); | |
567 | bool eq = equals_private (item, ignored_nodes); | |
568 | ||
569 | if (m_checker != NULL) | |
570 | { | |
571 | delete m_checker; | |
572 | m_checker = NULL; | |
573 | } | |
574 | ||
575 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
576 | fprintf (dump_file, | |
577 | "Equals called for:%s:%s (%u:%u) (%s:%s) with result: %s\n\n", | |
578 | name(), item->name (), node->order, item->node->order, asm_name (), | |
579 | item->asm_name (), eq ? "true" : "false"); | |
580 | ||
581 | return eq; | |
582 | } | |
583 | ||
584 | /* Processes function equality comparison. */ | |
585 | ||
586 | bool | |
587 | sem_function::equals_private (sem_item *item, | |
588 | hash_map <symtab_node *, sem_item *> &ignored_nodes) | |
589 | { | |
590 | if (item->type != FUNC) | |
591 | return false; | |
592 | ||
593 | basic_block bb1, bb2; | |
594 | edge e1, e2; | |
595 | edge_iterator ei1, ei2; | |
b84d4347 ML |
596 | bool result = true; |
597 | tree arg1, arg2; | |
598 | ||
599 | m_compared_func = static_cast<sem_function *> (item); | |
600 | ||
601 | gcc_assert (decl != item->decl); | |
602 | ||
603 | if (bb_sorted.length () != m_compared_func->bb_sorted.length () | |
604 | || edge_count != m_compared_func->edge_count | |
605 | || cfg_checksum != m_compared_func->cfg_checksum) | |
606 | return return_false (); | |
607 | ||
608 | if (!equals_wpa (item, ignored_nodes)) | |
609 | return false; | |
610 | ||
611 | /* Checking function arguments. */ | |
612 | tree decl1 = DECL_ATTRIBUTES (decl); | |
613 | tree decl2 = DECL_ATTRIBUTES (m_compared_func->decl); | |
614 | ||
615 | m_checker = new func_checker (decl, m_compared_func->decl, | |
616 | compare_polymorphic_p (), | |
617 | false, | |
618 | &refs_set, | |
619 | &m_compared_func->refs_set); | |
620 | while (decl1) | |
621 | { | |
622 | if (decl2 == NULL) | |
623 | return return_false (); | |
624 | ||
625 | if (get_attribute_name (decl1) != get_attribute_name (decl2)) | |
626 | return return_false (); | |
627 | ||
628 | tree attr_value1 = TREE_VALUE (decl1); | |
629 | tree attr_value2 = TREE_VALUE (decl2); | |
630 | ||
631 | if (attr_value1 && attr_value2) | |
632 | { | |
633 | bool ret = m_checker->compare_operand (TREE_VALUE (attr_value1), | |
634 | TREE_VALUE (attr_value2)); | |
635 | if (!ret) | |
636 | return return_false_with_msg ("attribute values are different"); | |
637 | } | |
638 | else if (!attr_value1 && !attr_value2) | |
639 | {} | |
640 | else | |
641 | return return_false (); | |
642 | ||
643 | decl1 = TREE_CHAIN (decl1); | |
644 | decl2 = TREE_CHAIN (decl2); | |
645 | } | |
646 | ||
647 | if (decl1 != decl2) | |
648 | return return_false(); | |
649 | ||
b84d4347 ML |
650 | for (arg1 = DECL_ARGUMENTS (decl), |
651 | arg2 = DECL_ARGUMENTS (m_compared_func->decl); | |
652 | arg1; arg1 = DECL_CHAIN (arg1), arg2 = DECL_CHAIN (arg2)) | |
653 | if (!m_checker->compare_decl (arg1, arg2)) | |
654 | return return_false (); | |
655 | ||
656 | /* Fill-up label dictionary. */ | |
657 | for (unsigned i = 0; i < bb_sorted.length (); ++i) | |
658 | { | |
659 | m_checker->parse_labels (bb_sorted[i]); | |
660 | m_checker->parse_labels (m_compared_func->bb_sorted[i]); | |
661 | } | |
662 | ||
663 | /* Checking all basic blocks. */ | |
664 | for (unsigned i = 0; i < bb_sorted.length (); ++i) | |
665 | if(!m_checker->compare_bb (bb_sorted[i], m_compared_func->bb_sorted[i])) | |
666 | return return_false(); | |
667 | ||
668 | dump_message ("All BBs are equal\n"); | |
669 | ||
c190efcc ML |
670 | auto_vec <int> bb_dict; |
671 | ||
b84d4347 ML |
672 | /* Basic block edges check. */ |
673 | for (unsigned i = 0; i < bb_sorted.length (); ++i) | |
674 | { | |
b84d4347 ML |
675 | bb1 = bb_sorted[i]->bb; |
676 | bb2 = m_compared_func->bb_sorted[i]->bb; | |
677 | ||
678 | ei2 = ei_start (bb2->preds); | |
679 | ||
680 | for (ei1 = ei_start (bb1->preds); ei_cond (ei1, &e1); ei_next (&ei1)) | |
681 | { | |
682 | ei_cond (ei2, &e2); | |
683 | ||
684 | if (e1->flags != e2->flags) | |
685 | return return_false_with_msg ("flags comparison returns false"); | |
686 | ||
1216ea72 | 687 | if (!bb_dict_test (&bb_dict, e1->src->index, e2->src->index)) |
b84d4347 ML |
688 | return return_false_with_msg ("edge comparison returns false"); |
689 | ||
1216ea72 | 690 | if (!bb_dict_test (&bb_dict, e1->dest->index, e2->dest->index)) |
b84d4347 ML |
691 | return return_false_with_msg ("BB comparison returns false"); |
692 | ||
693 | if (!m_checker->compare_edge (e1, e2)) | |
694 | return return_false_with_msg ("edge comparison returns false"); | |
695 | ||
696 | ei_next (&ei2); | |
697 | } | |
698 | } | |
699 | ||
700 | /* Basic block PHI nodes comparison. */ | |
701 | for (unsigned i = 0; i < bb_sorted.length (); i++) | |
702 | if (!compare_phi_node (bb_sorted[i]->bb, m_compared_func->bb_sorted[i]->bb)) | |
703 | return return_false_with_msg ("PHI node comparison returns false"); | |
704 | ||
705 | return result; | |
706 | } | |
707 | ||
106b5466 JH |
708 | /* Set LOCAL_P of NODE to true if DATA is non-NULL. |
709 | Helper for call_for_symbol_thunks_and_aliases. */ | |
710 | ||
711 | static bool | |
712 | set_local (cgraph_node *node, void *data) | |
713 | { | |
714 | node->local.local = data != NULL; | |
715 | return false; | |
716 | } | |
717 | ||
412049de | 718 | /* TREE_ADDRESSABLE of NODE to true. |
0a7246ee JH |
719 | Helper for call_for_symbol_thunks_and_aliases. */ |
720 | ||
721 | static bool | |
722 | set_addressable (varpool_node *node, void *) | |
723 | { | |
724 | TREE_ADDRESSABLE (node->decl) = 1; | |
725 | return false; | |
726 | } | |
727 | ||
412049de JH |
728 | /* Clear DECL_RTL of NODE. |
729 | Helper for call_for_symbol_thunks_and_aliases. */ | |
730 | ||
731 | static bool | |
732 | clear_decl_rtl (symtab_node *node, void *) | |
733 | { | |
734 | SET_DECL_RTL (node->decl, NULL); | |
735 | return false; | |
736 | } | |
737 | ||
0a7246ee JH |
738 | /* Redirect all callers of N and its aliases to TO. Remove aliases if |
739 | possible. Return number of redirections made. */ | |
740 | ||
741 | static int | |
742 | redirect_all_callers (cgraph_node *n, cgraph_node *to) | |
743 | { | |
744 | int nredirected = 0; | |
745 | ipa_ref *ref; | |
17d1bf76 | 746 | cgraph_edge *e = n->callers; |
0a7246ee | 747 | |
17d1bf76 | 748 | while (e) |
0a7246ee | 749 | { |
17d1bf76 ML |
750 | /* Redirecting thunks to interposable symbols or symbols in other sections |
751 | may not be supported by target output code. Play safe for now and | |
752 | punt on redirection. */ | |
753 | if (!e->caller->thunk.thunk_p) | |
754 | { | |
755 | struct cgraph_edge *nexte = e->next_caller; | |
756 | e->redirect_callee (to); | |
757 | e = nexte; | |
758 | nredirected++; | |
759 | } | |
760 | else | |
761 | e = e->next_callee; | |
0a7246ee JH |
762 | } |
763 | for (unsigned i = 0; n->iterate_direct_aliases (i, ref);) | |
764 | { | |
765 | bool removed = false; | |
766 | cgraph_node *n_alias = dyn_cast <cgraph_node *> (ref->referring); | |
767 | ||
768 | if ((DECL_COMDAT_GROUP (n->decl) | |
769 | && (DECL_COMDAT_GROUP (n->decl) | |
770 | == DECL_COMDAT_GROUP (n_alias->decl))) | |
771 | || (n_alias->get_availability () > AVAIL_INTERPOSABLE | |
772 | && n->get_availability () > AVAIL_INTERPOSABLE)) | |
773 | { | |
774 | nredirected += redirect_all_callers (n_alias, to); | |
775 | if (n_alias->can_remove_if_no_direct_calls_p () | |
17d1bf76 ML |
776 | && !n_alias->call_for_symbol_and_aliases (cgraph_node::has_thunk_p, |
777 | NULL, true) | |
0a7246ee JH |
778 | && !n_alias->has_aliases_p ()) |
779 | n_alias->remove (); | |
780 | } | |
781 | if (!removed) | |
782 | i++; | |
783 | } | |
784 | return nredirected; | |
785 | } | |
786 | ||
b84d4347 ML |
787 | /* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can |
788 | be applied. */ | |
0a7246ee | 789 | |
b84d4347 ML |
790 | bool |
791 | sem_function::merge (sem_item *alias_item) | |
792 | { | |
793 | gcc_assert (alias_item->type == FUNC); | |
794 | ||
795 | sem_function *alias_func = static_cast<sem_function *> (alias_item); | |
796 | ||
797 | cgraph_node *original = get_node (); | |
0a7246ee | 798 | cgraph_node *local_original = NULL; |
b84d4347 | 799 | cgraph_node *alias = alias_func->get_node (); |
b84d4347 | 800 | |
0a7246ee | 801 | bool create_wrapper = false; |
b84d4347 ML |
802 | bool create_alias = false; |
803 | bool redirect_callers = false; | |
0a7246ee JH |
804 | bool remove = false; |
805 | ||
b84d4347 | 806 | bool original_discardable = false; |
c7a06bc1 | 807 | bool original_discarded = false; |
b84d4347 | 808 | |
0a7246ee JH |
809 | bool original_address_matters = original->address_matters_p (); |
810 | bool alias_address_matters = alias->address_matters_p (); | |
811 | ||
812 | if (DECL_NO_INLINE_WARNING_P (original->decl) | |
813 | != DECL_NO_INLINE_WARNING_P (alias->decl)) | |
814 | { | |
815 | if (dump_file) | |
816 | fprintf (dump_file, | |
817 | "Not unifying; " | |
818 | "DECL_NO_INLINE_WARNING mismatch.\n\n"); | |
819 | return false; | |
820 | } | |
821 | ||
b84d4347 ML |
822 | /* Do not attempt to mix functions from different user sections; |
823 | we do not know what user intends with those. */ | |
824 | if (((DECL_SECTION_NAME (original->decl) && !original->implicit_section) | |
825 | || (DECL_SECTION_NAME (alias->decl) && !alias->implicit_section)) | |
826 | && DECL_SECTION_NAME (original->decl) != DECL_SECTION_NAME (alias->decl)) | |
827 | { | |
828 | if (dump_file) | |
829 | fprintf (dump_file, | |
0a7246ee JH |
830 | "Not unifying; " |
831 | "original and alias are in different sections.\n\n"); | |
b84d4347 ML |
832 | return false; |
833 | } | |
834 | ||
835 | /* See if original is in a section that can be discarded if the main | |
c7a06bc1 | 836 | symbol is not used. */ |
0a7246ee | 837 | |
c7a06bc1 JH |
838 | if (original->can_be_discarded_p ()) |
839 | original_discardable = true; | |
840 | /* Also consider case where we have resolution info and we know that | |
0a7246ee JH |
841 | original's definition is not going to be used. In this case we can not |
842 | create alias to original. */ | |
c7a06bc1 JH |
843 | if (node->resolution != LDPR_UNKNOWN |
844 | && !decl_binds_to_current_def_p (node->decl)) | |
845 | original_discardable = original_discarded = true; | |
b84d4347 | 846 | |
0a7246ee JH |
847 | /* Creating a symtab alias is the optimal way to merge. |
848 | It however can not be used in the following cases: | |
849 | ||
850 | 1) if ORIGINAL and ALIAS may be possibly compared for address equality. | |
851 | 2) if ORIGINAL is in a section that may be discarded by linker or if | |
852 | it is an external functions where we can not create an alias | |
853 | (ORIGINAL_DISCARDABLE) | |
854 | 3) if target do not support symbol aliases. | |
bbcdfb93 | 855 | 4) original and alias lie in different comdat groups. |
0a7246ee JH |
856 | |
857 | If we can not produce alias, we will turn ALIAS into WRAPPER of ORIGINAL | |
858 | and/or redirect all callers from ALIAS to ORIGINAL. */ | |
859 | if ((original_address_matters && alias_address_matters) | |
bbcdfb93 JH |
860 | || (original_discardable |
861 | && (!DECL_COMDAT_GROUP (alias->decl) | |
862 | || (DECL_COMDAT_GROUP (alias->decl) | |
863 | != DECL_COMDAT_GROUP (original->decl)))) | |
c7a06bc1 | 864 | || original_discarded |
bbcdfb93 JH |
865 | || !sem_item::target_supports_symbol_aliases_p () |
866 | || DECL_COMDAT_GROUP (alias->decl) != DECL_COMDAT_GROUP (original->decl)) | |
b84d4347 | 867 | { |
0a7246ee JH |
868 | /* First see if we can produce wrapper. */ |
869 | ||
870 | /* Do not turn function in one comdat group into wrapper to another | |
871 | comdat group. Other compiler producing the body of the | |
872 | another comdat group may make opossite decision and with unfortunate | |
873 | linker choices this may close a loop. */ | |
c7a06bc1 | 874 | if (DECL_COMDAT_GROUP (original->decl) && DECL_COMDAT_GROUP (alias->decl) |
0a7246ee JH |
875 | && (DECL_COMDAT_GROUP (alias->decl) |
876 | != DECL_COMDAT_GROUP (original->decl))) | |
877 | { | |
878 | if (dump_file) | |
879 | fprintf (dump_file, | |
880 | "Wrapper cannot be created because of COMDAT\n"); | |
881 | } | |
882 | else if (DECL_STATIC_CHAIN (alias->decl)) | |
883 | { | |
884 | if (dump_file) | |
885 | fprintf (dump_file, | |
886 | "Can not create wrapper of nested functions.\n"); | |
887 | } | |
888 | /* TODO: We can also deal with variadic functions never calling | |
889 | VA_START. */ | |
890 | else if (stdarg_p (TREE_TYPE (alias->decl))) | |
891 | { | |
892 | if (dump_file) | |
893 | fprintf (dump_file, | |
894 | "can not create wrapper of stdarg function.\n"); | |
895 | } | |
896 | else if (inline_summaries | |
897 | && inline_summaries->get (alias)->self_size <= 2) | |
898 | { | |
899 | if (dump_file) | |
900 | fprintf (dump_file, "Wrapper creation is not " | |
901 | "profitable (function is too small).\n"); | |
902 | } | |
903 | /* If user paid attention to mark function noinline, assume it is | |
904 | somewhat special and do not try to turn it into a wrapper that can | |
905 | not be undone by inliner. */ | |
906 | else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (alias->decl))) | |
907 | { | |
908 | if (dump_file) | |
909 | fprintf (dump_file, "Wrappers are not created for noinline.\n"); | |
910 | } | |
911 | else | |
912 | create_wrapper = true; | |
913 | ||
914 | /* We can redirect local calls in the case both alias and orignal | |
915 | are not interposable. */ | |
b84d4347 | 916 | redirect_callers |
0a7246ee JH |
917 | = alias->get_availability () > AVAIL_INTERPOSABLE |
918 | && original->get_availability () > AVAIL_INTERPOSABLE | |
919 | && !alias->instrumented_version; | |
b84d4347 | 920 | |
0a7246ee JH |
921 | if (!redirect_callers && !create_wrapper) |
922 | { | |
923 | if (dump_file) | |
924 | fprintf (dump_file, "Not unifying; can not redirect callers nor " | |
925 | "produce wrapper\n\n"); | |
926 | return false; | |
927 | } | |
928 | ||
929 | /* Work out the symbol the wrapper should call. | |
930 | If ORIGINAL is interposable, we need to call a local alias. | |
c7a06bc1 JH |
931 | Also produce local alias (if possible) as an optimization. |
932 | ||
933 | Local aliases can not be created inside comdat groups because that | |
934 | prevents inlining. */ | |
935 | if (!original_discardable && !original->get_comdat_group ()) | |
0a7246ee JH |
936 | { |
937 | local_original | |
938 | = dyn_cast <cgraph_node *> (original->noninterposable_alias ()); | |
939 | if (!local_original | |
940 | && original->get_availability () > AVAIL_INTERPOSABLE) | |
941 | local_original = original; | |
0a7246ee JH |
942 | } |
943 | /* If we can not use local alias, fallback to the original | |
944 | when possible. */ | |
945 | else if (original->get_availability () > AVAIL_INTERPOSABLE) | |
946 | local_original = original; | |
c7a06bc1 JH |
947 | |
948 | /* If original is COMDAT local, we can not really redirect calls outside | |
949 | of its comdat group to it. */ | |
950 | if (original->comdat_local_p ()) | |
951 | redirect_callers = false; | |
0a7246ee JH |
952 | if (!local_original) |
953 | { | |
954 | if (dump_file) | |
955 | fprintf (dump_file, "Not unifying; " | |
956 | "can not produce local alias.\n\n"); | |
957 | return false; | |
958 | } | |
f657d665 | 959 | |
0a7246ee JH |
960 | if (!redirect_callers && !create_wrapper) |
961 | { | |
962 | if (dump_file) | |
963 | fprintf (dump_file, "Not unifying; " | |
964 | "can not redirect callers nor produce a wrapper\n\n"); | |
965 | return false; | |
966 | } | |
967 | if (!create_wrapper | |
17d1bf76 ML |
968 | && !alias->call_for_symbol_and_aliases (cgraph_node::has_thunk_p, |
969 | NULL, true) | |
0a7246ee JH |
970 | && !alias->can_remove_if_no_direct_calls_p ()) |
971 | { | |
972 | if (dump_file) | |
973 | fprintf (dump_file, "Not unifying; can not make wrapper and " | |
974 | "function has other uses than direct calls\n\n"); | |
975 | return false; | |
976 | } | |
544dafa6 | 977 | } |
0a7246ee JH |
978 | else |
979 | create_alias = true; | |
544dafa6 | 980 | |
b84d4347 ML |
981 | if (redirect_callers) |
982 | { | |
0a7246ee | 983 | int nredirected = redirect_all_callers (alias, local_original); |
b84d4347 | 984 | |
0a7246ee JH |
985 | if (nredirected) |
986 | { | |
987 | alias->icf_merged = true; | |
988 | local_original->icf_merged = true; | |
b84d4347 | 989 | |
0a7246ee JH |
990 | if (dump_file && nredirected) |
991 | fprintf (dump_file, "%i local calls have been " | |
992 | "redirected.\n", nredirected); | |
b84d4347 ML |
993 | } |
994 | ||
0a7246ee JH |
995 | /* If all callers was redirected, do not produce wrapper. */ |
996 | if (alias->can_remove_if_no_direct_calls_p () | |
997 | && !alias->has_aliases_p ()) | |
998 | { | |
999 | create_wrapper = false; | |
1000 | remove = true; | |
1001 | } | |
1002 | gcc_assert (!create_alias); | |
b84d4347 | 1003 | } |
b84d4347 ML |
1004 | else if (create_alias) |
1005 | { | |
1006 | alias->icf_merged = true; | |
1007 | ||
1008 | /* Remove the function's body. */ | |
1009 | ipa_merge_profiles (original, alias); | |
1010 | alias->release_body (true); | |
1011 | alias->reset (); | |
412049de JH |
1012 | /* Notice global symbol possibly produced RTL. */ |
1013 | ((symtab_node *)alias)->call_for_symbol_and_aliases (clear_decl_rtl, | |
1014 | NULL, true); | |
b84d4347 ML |
1015 | |
1016 | /* Create the alias. */ | |
1017 | cgraph_node::create_alias (alias_func->decl, decl); | |
1018 | alias->resolve_alias (original); | |
1019 | ||
106b5466 JH |
1020 | original->call_for_symbol_thunks_and_aliases |
1021 | (set_local, (void *)(size_t) original->local_p (), true); | |
9d4ded75 | 1022 | |
b84d4347 | 1023 | if (dump_file) |
0a7246ee | 1024 | fprintf (dump_file, "Unified; Function alias has been created.\n\n"); |
b84d4347 | 1025 | } |
0a7246ee | 1026 | if (create_wrapper) |
b84d4347 | 1027 | { |
0a7246ee JH |
1028 | gcc_assert (!create_alias); |
1029 | alias->icf_merged = true; | |
1030 | local_original->icf_merged = true; | |
b84d4347 | 1031 | |
0a7246ee JH |
1032 | ipa_merge_profiles (local_original, alias, true); |
1033 | alias->create_wrapper (local_original); | |
b84d4347 | 1034 | |
0a7246ee JH |
1035 | if (dump_file) |
1036 | fprintf (dump_file, "Unified; Wrapper has been created.\n\n"); | |
1037 | } | |
17d1bf76 ML |
1038 | |
1039 | /* It's possible that redirection can hit thunks that block | |
1040 | redirection opportunities. */ | |
1041 | gcc_assert (alias->icf_merged || remove || redirect_callers); | |
0a7246ee | 1042 | original->icf_merged = true; |
34b42fb0 | 1043 | |
0a7246ee JH |
1044 | /* Inform the inliner about cross-module merging. */ |
1045 | if ((original->lto_file_data || alias->lto_file_data) | |
1046 | && original->lto_file_data != alias->lto_file_data) | |
1047 | local_original->merged = original->merged = true; | |
34b42fb0 | 1048 | |
0a7246ee JH |
1049 | if (remove) |
1050 | { | |
1051 | ipa_merge_profiles (original, alias); | |
1052 | alias->release_body (); | |
1053 | alias->reset (); | |
1054 | alias->body_removed = true; | |
b84d4347 | 1055 | alias->icf_merged = true; |
b84d4347 | 1056 | if (dump_file) |
0a7246ee | 1057 | fprintf (dump_file, "Unified; Function body was removed.\n"); |
b84d4347 | 1058 | } |
b84d4347 ML |
1059 | |
1060 | return true; | |
1061 | } | |
1062 | ||
1063 | /* Semantic item initialization function. */ | |
1064 | ||
1065 | void | |
1066 | sem_function::init (void) | |
1067 | { | |
1068 | if (in_lto_p) | |
70486010 | 1069 | get_node ()->get_untransformed_body (); |
b84d4347 ML |
1070 | |
1071 | tree fndecl = node->decl; | |
1072 | function *func = DECL_STRUCT_FUNCTION (fndecl); | |
1073 | ||
1074 | gcc_assert (func); | |
1075 | gcc_assert (SSANAMES (func)); | |
1076 | ||
1077 | ssa_names_size = SSANAMES (func)->length (); | |
1078 | node = node; | |
1079 | ||
1080 | decl = fndecl; | |
1081 | region_tree = func->eh->region_tree; | |
1082 | ||
1083 | /* iterating all function arguments. */ | |
1084 | arg_count = count_formal_params (fndecl); | |
1085 | ||
1086 | edge_count = n_edges_for_fn (func); | |
1087 | cfg_checksum = coverage_compute_cfg_checksum (func); | |
1088 | ||
1089 | inchash::hash hstate; | |
1090 | ||
1091 | basic_block bb; | |
1092 | FOR_EACH_BB_FN (bb, func) | |
1093 | { | |
1094 | unsigned nondbg_stmt_count = 0; | |
1095 | ||
1096 | edge e; | |
a8d93817 JH |
1097 | for (edge_iterator ei = ei_start (bb->preds); ei_cond (ei, &e); |
1098 | ei_next (&ei)) | |
b84d4347 ML |
1099 | cfg_checksum = iterative_hash_host_wide_int (e->flags, |
1100 | cfg_checksum); | |
1101 | ||
1102 | for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); | |
1103 | gsi_next (&gsi)) | |
1104 | { | |
1105 | gimple stmt = gsi_stmt (gsi); | |
1106 | ||
a8d93817 JH |
1107 | if (gimple_code (stmt) != GIMPLE_DEBUG |
1108 | && gimple_code (stmt) != GIMPLE_PREDICT) | |
b84d4347 | 1109 | { |
a8d93817 | 1110 | hash_stmt (stmt, hstate); |
b84d4347 ML |
1111 | nondbg_stmt_count++; |
1112 | } | |
1113 | } | |
1114 | ||
1115 | gcode_hash = hstate.end (); | |
1116 | bb_sizes.safe_push (nondbg_stmt_count); | |
1117 | ||
1118 | /* Inserting basic block to hash table. */ | |
1119 | sem_bb *semantic_bb = new sem_bb (bb, nondbg_stmt_count, | |
a8d93817 JH |
1120 | EDGE_COUNT (bb->preds) |
1121 | + EDGE_COUNT (bb->succs)); | |
b84d4347 ML |
1122 | |
1123 | bb_sorted.safe_push (semantic_bb); | |
1124 | } | |
1125 | ||
1126 | parse_tree_args (); | |
1127 | } | |
1128 | ||
a8d93817 JH |
1129 | /* Accumulate to HSTATE a hash of expression EXP. |
1130 | Identical to inchash::add_expr, but guaranteed to be stable across LTO | |
1131 | and DECL equality classes. */ | |
1132 | ||
1133 | void | |
1134 | sem_item::add_expr (const_tree exp, inchash::hash &hstate) | |
1135 | { | |
1136 | if (exp == NULL_TREE) | |
1137 | { | |
1138 | hstate.merge_hash (0); | |
1139 | return; | |
1140 | } | |
1141 | ||
1142 | /* Handled component can be matched in a cureful way proving equivalence | |
1143 | even if they syntactically differ. Just skip them. */ | |
1144 | STRIP_NOPS (exp); | |
1145 | while (handled_component_p (exp)) | |
1146 | exp = TREE_OPERAND (exp, 0); | |
1147 | ||
1148 | enum tree_code code = TREE_CODE (exp); | |
1149 | hstate.add_int (code); | |
1150 | ||
1151 | switch (code) | |
1152 | { | |
1153 | /* Use inchash::add_expr for everything that is LTO stable. */ | |
1154 | case VOID_CST: | |
1155 | case INTEGER_CST: | |
1156 | case REAL_CST: | |
1157 | case FIXED_CST: | |
1158 | case STRING_CST: | |
1159 | case COMPLEX_CST: | |
1160 | case VECTOR_CST: | |
1161 | inchash::add_expr (exp, hstate); | |
1162 | break; | |
1163 | case CONSTRUCTOR: | |
1164 | { | |
1165 | unsigned HOST_WIDE_INT idx; | |
1166 | tree value; | |
1167 | ||
1168 | hstate.add_wide_int (int_size_in_bytes (TREE_TYPE (exp))); | |
1169 | ||
1170 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value) | |
1171 | if (value) | |
1172 | add_expr (value, hstate); | |
1173 | break; | |
1174 | } | |
1175 | case ADDR_EXPR: | |
1176 | case FDESC_EXPR: | |
1177 | add_expr (get_base_address (TREE_OPERAND (exp, 0)), hstate); | |
1178 | break; | |
1179 | case SSA_NAME: | |
1180 | case VAR_DECL: | |
1181 | case CONST_DECL: | |
1182 | case PARM_DECL: | |
1183 | hstate.add_wide_int (int_size_in_bytes (TREE_TYPE (exp))); | |
1184 | break; | |
1185 | case MEM_REF: | |
1186 | case POINTER_PLUS_EXPR: | |
1187 | case MINUS_EXPR: | |
1188 | case RANGE_EXPR: | |
1189 | add_expr (TREE_OPERAND (exp, 0), hstate); | |
1190 | add_expr (TREE_OPERAND (exp, 1), hstate); | |
1191 | break; | |
1192 | case PLUS_EXPR: | |
1193 | { | |
1194 | inchash::hash one, two; | |
1195 | add_expr (TREE_OPERAND (exp, 0), one); | |
1196 | add_expr (TREE_OPERAND (exp, 1), two); | |
1197 | hstate.add_commutative (one, two); | |
1198 | } | |
1199 | break; | |
1200 | CASE_CONVERT: | |
1201 | hstate.add_wide_int (int_size_in_bytes (TREE_TYPE (exp))); | |
1202 | return add_expr (TREE_OPERAND (exp, 0), hstate); | |
1203 | default: | |
1204 | break; | |
1205 | } | |
1206 | } | |
1207 | ||
b84d4347 ML |
1208 | /* Improve accumulated hash for HSTATE based on a gimple statement STMT. */ |
1209 | ||
1210 | void | |
a8d93817 | 1211 | sem_function::hash_stmt (gimple stmt, inchash::hash &hstate) |
b84d4347 ML |
1212 | { |
1213 | enum gimple_code code = gimple_code (stmt); | |
1214 | ||
a8d93817 | 1215 | hstate.add_int (code); |
b84d4347 | 1216 | |
a8d93817 | 1217 | switch (code) |
b84d4347 | 1218 | { |
a8d93817 JH |
1219 | case GIMPLE_ASSIGN: |
1220 | if (commutative_tree_code (gimple_assign_rhs_code (stmt)) | |
1221 | || commutative_ternary_tree_code (gimple_assign_rhs_code (stmt))) | |
b84d4347 | 1222 | { |
a8d93817 | 1223 | inchash::hash one, two; |
b84d4347 | 1224 | |
a8d93817 JH |
1225 | add_expr (gimple_assign_rhs1 (stmt), one); |
1226 | add_expr (gimple_assign_rhs2 (stmt), two); | |
1227 | hstate.add_commutative (one, two); | |
1228 | add_expr (gimple_assign_lhs (stmt), hstate); | |
1229 | break; | |
b84d4347 | 1230 | } |
a8d93817 JH |
1231 | /* ... fall through ... */ |
1232 | case GIMPLE_CALL: | |
1233 | case GIMPLE_ASM: | |
1234 | case GIMPLE_COND: | |
1235 | case GIMPLE_GOTO: | |
1236 | case GIMPLE_RETURN: | |
1237 | /* All these statements are equivalent if their operands are. */ | |
1238 | for (unsigned i = 0; i < gimple_num_ops (stmt); ++i) | |
1239 | add_expr (gimple_op (stmt, i), hstate); | |
1240 | default: | |
1241 | break; | |
b84d4347 ML |
1242 | } |
1243 | } | |
1244 | ||
1245 | ||
1246 | /* Return true if polymorphic comparison must be processed. */ | |
1247 | ||
1248 | bool | |
1249 | sem_function::compare_polymorphic_p (void) | |
1250 | { | |
060cfff4 JH |
1251 | struct cgraph_edge *e; |
1252 | ||
1253 | if (!opt_for_fn (decl, flag_devirtualize)) | |
1254 | return false; | |
1255 | if (get_node ()->indirect_calls != NULL | |
1256 | || m_compared_func->get_node ()->indirect_calls != NULL) | |
1257 | return true; | |
1258 | /* TODO: We can do simple propagation determining what calls may lead to | |
1259 | a polymorphic call. */ | |
1260 | for (e = m_compared_func->get_node ()->callees; e; e = e->next_callee) | |
1261 | if (e->callee->definition | |
1262 | && opt_for_fn (e->callee->decl, flag_devirtualize)) | |
1263 | return true; | |
1264 | return false; | |
b84d4347 ML |
1265 | } |
1266 | ||
1267 | /* For a given call graph NODE, the function constructs new | |
1268 | semantic function item. */ | |
1269 | ||
1270 | sem_function * | |
1271 | sem_function::parse (cgraph_node *node, bitmap_obstack *stack) | |
1272 | { | |
1273 | tree fndecl = node->decl; | |
1274 | function *func = DECL_STRUCT_FUNCTION (fndecl); | |
1275 | ||
b6cddc7f | 1276 | /* TODO: add support for thunks. */ |
b84d4347 ML |
1277 | |
1278 | if (!func || !node->has_gimple_body_p ()) | |
1279 | return NULL; | |
1280 | ||
1281 | if (lookup_attribute_by_prefix ("omp ", DECL_ATTRIBUTES (node->decl)) != NULL) | |
1282 | return NULL; | |
1283 | ||
1284 | sem_function *f = new sem_function (node, 0, stack); | |
1285 | ||
1286 | f->init (); | |
1287 | ||
1288 | return f; | |
1289 | } | |
1290 | ||
1291 | /* Parses function arguments and result type. */ | |
1292 | ||
1293 | void | |
1294 | sem_function::parse_tree_args (void) | |
1295 | { | |
1296 | tree result; | |
1297 | ||
1298 | if (arg_types.exists ()) | |
1299 | arg_types.release (); | |
1300 | ||
1301 | arg_types.create (4); | |
1302 | tree fnargs = DECL_ARGUMENTS (decl); | |
1303 | ||
1304 | for (tree parm = fnargs; parm; parm = DECL_CHAIN (parm)) | |
1305 | arg_types.safe_push (DECL_ARG_TYPE (parm)); | |
1306 | ||
1307 | /* Function result type. */ | |
1308 | result = DECL_RESULT (decl); | |
1309 | result_type = result ? TREE_TYPE (result) : NULL; | |
1310 | ||
1311 | /* During WPA, we can get arguments by following method. */ | |
1312 | if (!fnargs) | |
1313 | { | |
1314 | tree type = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1315 | for (tree parm = type; parm; parm = TREE_CHAIN (parm)) | |
1316 | arg_types.safe_push (TYPE_CANONICAL (TREE_VALUE (parm))); | |
1317 | ||
1318 | result_type = TREE_TYPE (TREE_TYPE (decl)); | |
1319 | } | |
1320 | } | |
1321 | ||
1322 | /* For given basic blocks BB1 and BB2 (from functions FUNC1 and FUNC), | |
1323 | return true if phi nodes are semantically equivalent in these blocks . */ | |
1324 | ||
1325 | bool | |
1326 | sem_function::compare_phi_node (basic_block bb1, basic_block bb2) | |
1327 | { | |
538dd0b7 DM |
1328 | gphi_iterator si1, si2; |
1329 | gphi *phi1, *phi2; | |
b84d4347 ML |
1330 | unsigned size1, size2, i; |
1331 | tree t1, t2; | |
1332 | edge e1, e2; | |
1333 | ||
1334 | gcc_assert (bb1 != NULL); | |
1335 | gcc_assert (bb2 != NULL); | |
1336 | ||
1337 | si2 = gsi_start_phis (bb2); | |
1338 | for (si1 = gsi_start_phis (bb1); !gsi_end_p (si1); | |
1339 | gsi_next (&si1)) | |
1340 | { | |
1341 | gsi_next_nonvirtual_phi (&si1); | |
1342 | gsi_next_nonvirtual_phi (&si2); | |
1343 | ||
1344 | if (gsi_end_p (si1) && gsi_end_p (si2)) | |
1345 | break; | |
1346 | ||
1347 | if (gsi_end_p (si1) || gsi_end_p (si2)) | |
1348 | return return_false(); | |
1349 | ||
538dd0b7 DM |
1350 | phi1 = si1.phi (); |
1351 | phi2 = si2.phi (); | |
b84d4347 | 1352 | |
59f084e0 ML |
1353 | tree phi_result1 = gimple_phi_result (phi1); |
1354 | tree phi_result2 = gimple_phi_result (phi2); | |
1355 | ||
1356 | if (!m_checker->compare_operand (phi_result1, phi_result2)) | |
1357 | return return_false_with_msg ("PHI results are different"); | |
1358 | ||
b84d4347 ML |
1359 | size1 = gimple_phi_num_args (phi1); |
1360 | size2 = gimple_phi_num_args (phi2); | |
1361 | ||
1362 | if (size1 != size2) | |
1363 | return return_false (); | |
1364 | ||
1365 | for (i = 0; i < size1; ++i) | |
1366 | { | |
1367 | t1 = gimple_phi_arg (phi1, i)->def; | |
1368 | t2 = gimple_phi_arg (phi2, i)->def; | |
1369 | ||
1370 | if (!m_checker->compare_operand (t1, t2)) | |
1371 | return return_false (); | |
1372 | ||
1373 | e1 = gimple_phi_arg_edge (phi1, i); | |
1374 | e2 = gimple_phi_arg_edge (phi2, i); | |
1375 | ||
1376 | if (!m_checker->compare_edge (e1, e2)) | |
1377 | return return_false (); | |
1378 | } | |
1379 | ||
1380 | gsi_next (&si2); | |
1381 | } | |
1382 | ||
1383 | return true; | |
1384 | } | |
1385 | ||
1386 | /* Returns true if tree T can be compared as a handled component. */ | |
1387 | ||
1388 | bool | |
1389 | sem_function::icf_handled_component_p (tree t) | |
1390 | { | |
1391 | tree_code tc = TREE_CODE (t); | |
1392 | ||
1393 | return ((handled_component_p (t)) | |
1394 | || tc == ADDR_EXPR || tc == MEM_REF || tc == REALPART_EXPR | |
1395 | || tc == IMAGPART_EXPR || tc == OBJ_TYPE_REF); | |
1396 | } | |
1397 | ||
1398 | /* Basic blocks dictionary BB_DICT returns true if SOURCE index BB | |
1399 | corresponds to TARGET. */ | |
1400 | ||
1401 | bool | |
1216ea72 | 1402 | sem_function::bb_dict_test (vec<int> *bb_dict, int source, int target) |
b84d4347 | 1403 | { |
c190efcc ML |
1404 | source++; |
1405 | target++; | |
1406 | ||
1216ea72 TS |
1407 | if (bb_dict->length () <= (unsigned)source) |
1408 | bb_dict->safe_grow_cleared (source + 1); | |
c190efcc | 1409 | |
1216ea72 | 1410 | if ((*bb_dict)[source] == 0) |
b84d4347 | 1411 | { |
1216ea72 | 1412 | (*bb_dict)[source] = target; |
b84d4347 ML |
1413 | return true; |
1414 | } | |
1415 | else | |
1216ea72 | 1416 | return (*bb_dict)[source] == target; |
b84d4347 ML |
1417 | } |
1418 | ||
b84d4347 ML |
1419 | |
1420 | /* Semantic variable constructor that uses STACK as bitmap memory stack. */ | |
1421 | ||
1422 | sem_variable::sem_variable (bitmap_obstack *stack): sem_item (VAR, stack) | |
1423 | { | |
1424 | } | |
1425 | ||
1426 | /* Constructor based on varpool node _NODE with computed hash _HASH. | |
1427 | Bitmap STACK is used for memory allocation. */ | |
1428 | ||
1429 | sem_variable::sem_variable (varpool_node *node, hashval_t _hash, | |
1430 | bitmap_obstack *stack): sem_item(VAR, | |
1431 | node, _hash, stack) | |
1432 | { | |
1433 | gcc_checking_assert (node); | |
1434 | gcc_checking_assert (get_node ()); | |
1435 | } | |
1436 | ||
46305737 JH |
1437 | /* Fast equality function based on knowledge known in WPA. */ |
1438 | ||
1439 | bool | |
1440 | sem_variable::equals_wpa (sem_item *item, | |
1441 | hash_map <symtab_node *, sem_item *> &ignored_nodes) | |
1442 | { | |
1443 | gcc_assert (item->type == VAR); | |
1444 | ||
1445 | if (node->num_references () != item->node->num_references ()) | |
1446 | return return_false_with_msg ("different number of references"); | |
1447 | ||
1448 | if (DECL_TLS_MODEL (decl) || DECL_TLS_MODEL (item->decl)) | |
1449 | return return_false_with_msg ("TLS model"); | |
1450 | ||
1451 | if (DECL_ALIGN (decl) != DECL_ALIGN (item->decl)) | |
1452 | return return_false_with_msg ("alignment mismatch"); | |
1453 | ||
1454 | if (DECL_VIRTUAL_P (decl) != DECL_VIRTUAL_P (item->decl)) | |
1455 | return return_false_with_msg ("Virtual flag mismatch"); | |
1456 | ||
1457 | if (DECL_SIZE (decl) != DECL_SIZE (item->decl) | |
1458 | && ((!DECL_SIZE (decl) || !DECL_SIZE (item->decl)) | |
1459 | || !operand_equal_p (DECL_SIZE (decl), | |
1460 | DECL_SIZE (item->decl), OEP_ONLY_CONST))) | |
1461 | return return_false_with_msg ("size mismatch"); | |
1462 | ||
1463 | /* Do not attempt to mix data from different user sections; | |
1464 | we do not know what user intends with those. */ | |
1465 | if (((DECL_SECTION_NAME (decl) && !node->implicit_section) | |
1466 | || (DECL_SECTION_NAME (item->decl) && !item->node->implicit_section)) | |
1467 | && DECL_SECTION_NAME (decl) != DECL_SECTION_NAME (item->decl)) | |
1468 | return return_false_with_msg ("user section mismatch"); | |
1469 | ||
1470 | if (DECL_IN_TEXT_SECTION (decl) != DECL_IN_TEXT_SECTION (item->decl)) | |
1471 | return return_false_with_msg ("text section"); | |
1472 | ||
1473 | ipa_ref *ref = NULL, *ref2 = NULL; | |
1474 | for (unsigned i = 0; node->iterate_reference (i, ref); i++) | |
1475 | { | |
1476 | item->node->iterate_reference (i, ref2); | |
1477 | ||
1478 | if (!compare_cgraph_references (ignored_nodes, | |
1479 | ref->referred, ref2->referred, | |
1480 | ref->address_matters_p ())) | |
1481 | return false; | |
ab038a80 JH |
1482 | |
1483 | /* DECL_FINAL_P flag on methods referred by virtual tables is used | |
1484 | to decide on completeness possible_polymorphic_call_targets lists | |
1485 | and therefore it must match. */ | |
1486 | if ((DECL_VIRTUAL_P (decl) || DECL_VIRTUAL_P (item->decl)) | |
1487 | && (DECL_VIRTUAL_P (ref->referred->decl) | |
1488 | || DECL_VIRTUAL_P (ref2->referred->decl)) | |
1489 | && ((DECL_VIRTUAL_P (ref->referred->decl) | |
1490 | != DECL_VIRTUAL_P (ref2->referred->decl)) | |
1491 | || (DECL_FINAL_P (ref->referred->decl) | |
1492 | != DECL_FINAL_P (ref2->referred->decl)))) | |
1493 | return return_false_with_msg ("virtual or final flag mismatch"); | |
46305737 JH |
1494 | } |
1495 | ||
1496 | return true; | |
1497 | } | |
1498 | ||
b84d4347 ML |
1499 | /* Returns true if the item equals to ITEM given as argument. */ |
1500 | ||
c4c0f336 ML |
1501 | /* Returns true if the item equals to ITEM given as argument. */ |
1502 | ||
b84d4347 ML |
1503 | bool |
1504 | sem_variable::equals (sem_item *item, | |
c4c0f336 | 1505 | hash_map <symtab_node *, sem_item *> &) |
b84d4347 ML |
1506 | { |
1507 | gcc_assert (item->type == VAR); | |
c4c0f336 | 1508 | bool ret; |
b84d4347 | 1509 | |
c4c0f336 ML |
1510 | if (DECL_INITIAL (decl) == error_mark_node && in_lto_p) |
1511 | dyn_cast <varpool_node *>(node)->get_constructor (); | |
1512 | if (DECL_INITIAL (item->decl) == error_mark_node && in_lto_p) | |
1513 | dyn_cast <varpool_node *>(item->node)->get_constructor (); | |
b84d4347 | 1514 | |
9374ef82 ML |
1515 | /* As seen in PR ipa/65303 we have to compare variables types. */ |
1516 | if (!func_checker::compatible_types_p (TREE_TYPE (decl), | |
1517 | TREE_TYPE (item->decl))) | |
1518 | return return_false_with_msg ("variables types are different"); | |
1519 | ||
c4c0f336 ML |
1520 | ret = sem_variable::equals (DECL_INITIAL (decl), |
1521 | DECL_INITIAL (item->node->decl)); | |
1522 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1523 | fprintf (dump_file, | |
1524 | "Equals called for vars:%s:%s (%u:%u) (%s:%s) with result: %s\n\n", | |
1525 | name(), item->name (), node->order, item->node->order, asm_name (), | |
1526 | item->asm_name (), ret ? "true" : "false"); | |
b84d4347 | 1527 | |
c4c0f336 | 1528 | return ret; |
b84d4347 ML |
1529 | } |
1530 | ||
1531 | /* Compares trees T1 and T2 for semantic equality. */ | |
1532 | ||
1533 | bool | |
1534 | sem_variable::equals (tree t1, tree t2) | |
1535 | { | |
46305737 JH |
1536 | if (!t1 || !t2) |
1537 | return return_with_debug (t1 == t2); | |
1538 | if (t1 == t2) | |
1539 | return true; | |
b84d4347 ML |
1540 | tree_code tc1 = TREE_CODE (t1); |
1541 | tree_code tc2 = TREE_CODE (t2); | |
1542 | ||
1543 | if (tc1 != tc2) | |
46305737 | 1544 | return return_false_with_msg ("TREE_CODE mismatch"); |
b84d4347 ML |
1545 | |
1546 | switch (tc1) | |
1547 | { | |
1548 | case CONSTRUCTOR: | |
1549 | { | |
46305737 JH |
1550 | vec<constructor_elt, va_gc> *v1, *v2; |
1551 | unsigned HOST_WIDE_INT idx; | |
b84d4347 | 1552 | |
46305737 JH |
1553 | enum tree_code typecode = TREE_CODE (TREE_TYPE (t1)); |
1554 | if (typecode != TREE_CODE (TREE_TYPE (t2))) | |
1555 | return return_false_with_msg ("constructor type mismatch"); | |
b84d4347 | 1556 | |
46305737 JH |
1557 | if (typecode == ARRAY_TYPE) |
1558 | { | |
1559 | HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1)); | |
1560 | /* For arrays, check that the sizes all match. */ | |
1561 | if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)) | |
1562 | || size_1 == -1 | |
1563 | || size_1 != int_size_in_bytes (TREE_TYPE (t2))) | |
1564 | return return_false_with_msg ("constructor array size mismatch"); | |
1565 | } | |
1566 | else if (!func_checker::compatible_types_p (TREE_TYPE (t1), | |
1567 | TREE_TYPE (t2))) | |
1568 | return return_false_with_msg ("constructor type incompatible"); | |
b84d4347 | 1569 | |
46305737 JH |
1570 | v1 = CONSTRUCTOR_ELTS (t1); |
1571 | v2 = CONSTRUCTOR_ELTS (t2); | |
1572 | if (vec_safe_length (v1) != vec_safe_length (v2)) | |
1573 | return return_false_with_msg ("constructor number of elts mismatch"); | |
1574 | ||
1575 | for (idx = 0; idx < vec_safe_length (v1); ++idx) | |
1576 | { | |
1577 | constructor_elt *c1 = &(*v1)[idx]; | |
1578 | constructor_elt *c2 = &(*v2)[idx]; | |
1579 | ||
1580 | /* Check that each value is the same... */ | |
1581 | if (!sem_variable::equals (c1->value, c2->value)) | |
1582 | return false; | |
1583 | /* ... and that they apply to the same fields! */ | |
1584 | if (!sem_variable::equals (c1->index, c2->index)) | |
1585 | return false; | |
1586 | } | |
b84d4347 ML |
1587 | return true; |
1588 | } | |
1589 | case MEM_REF: | |
1590 | { | |
1591 | tree x1 = TREE_OPERAND (t1, 0); | |
1592 | tree x2 = TREE_OPERAND (t2, 0); | |
1593 | tree y1 = TREE_OPERAND (t1, 1); | |
1594 | tree y2 = TREE_OPERAND (t2, 1); | |
1595 | ||
060cfff4 | 1596 | if (!func_checker::compatible_types_p (TREE_TYPE (x1), TREE_TYPE (x2))) |
b84d4347 ML |
1597 | return return_false (); |
1598 | ||
1599 | /* Type of the offset on MEM_REF does not matter. */ | |
46305737 JH |
1600 | return return_with_debug (sem_variable::equals (x1, x2) |
1601 | && wi::to_offset (y1) | |
1602 | == wi::to_offset (y2)); | |
b84d4347 | 1603 | } |
b84d4347 | 1604 | case ADDR_EXPR: |
46305737 | 1605 | case FDESC_EXPR: |
b84d4347 ML |
1606 | { |
1607 | tree op1 = TREE_OPERAND (t1, 0); | |
1608 | tree op2 = TREE_OPERAND (t2, 0); | |
1609 | return sem_variable::equals (op1, op2); | |
1610 | } | |
46305737 | 1611 | /* References to other vars/decls are compared using ipa-ref. */ |
b84d4347 ML |
1612 | case FUNCTION_DECL: |
1613 | case VAR_DECL: | |
46305737 JH |
1614 | if (decl_in_symtab_p (t1) && decl_in_symtab_p (t2)) |
1615 | return true; | |
1616 | return return_false_with_msg ("Declaration mismatch"); | |
1617 | case CONST_DECL: | |
1618 | /* TODO: We can check CONST_DECL by its DECL_INITIAL, but for that we | |
1619 | need to process its VAR/FUNCTION references without relying on ipa-ref | |
1620 | compare. */ | |
b84d4347 ML |
1621 | case FIELD_DECL: |
1622 | case LABEL_DECL: | |
46305737 | 1623 | return return_false_with_msg ("Declaration mismatch"); |
b84d4347 | 1624 | case INTEGER_CST: |
46305737 JH |
1625 | /* Integer constants are the same only if the same width of type. */ |
1626 | if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2))) | |
1627 | return return_false_with_msg ("INTEGER_CST precision mismatch"); | |
1628 | if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))) | |
1629 | return return_false_with_msg ("INTEGER_CST mode mismatch"); | |
1630 | return return_with_debug (tree_int_cst_equal (t1, t2)); | |
b84d4347 | 1631 | case STRING_CST: |
46305737 JH |
1632 | if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))) |
1633 | return return_false_with_msg ("STRING_CST mode mismatch"); | |
1634 | if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2)) | |
1635 | return return_false_with_msg ("STRING_CST length mismatch"); | |
1636 | if (memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2), | |
1637 | TREE_STRING_LENGTH (t1))) | |
1638 | return return_false_with_msg ("STRING_CST mismatch"); | |
1639 | return true; | |
1640 | case FIXED_CST: | |
1641 | /* Fixed constants are the same only if the same width of type. */ | |
1642 | if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2))) | |
1643 | return return_false_with_msg ("FIXED_CST precision mismatch"); | |
1644 | ||
1645 | return return_with_debug (FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), | |
1646 | TREE_FIXED_CST (t2))); | |
b84d4347 | 1647 | case COMPLEX_CST: |
46305737 JH |
1648 | return (sem_variable::equals (TREE_REALPART (t1), TREE_REALPART (t2)) |
1649 | && sem_variable::equals (TREE_IMAGPART (t1), TREE_IMAGPART (t2))); | |
1650 | case REAL_CST: | |
1651 | /* Real constants are the same only if the same width of type. */ | |
1652 | if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2))) | |
1653 | return return_false_with_msg ("REAL_CST precision mismatch"); | |
1654 | return return_with_debug (REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), | |
1655 | TREE_REAL_CST (t2))); | |
1656 | case VECTOR_CST: | |
1657 | { | |
1658 | unsigned i; | |
1659 | ||
1660 | if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2)) | |
1661 | return return_false_with_msg ("VECTOR_CST nelts mismatch"); | |
1662 | ||
1663 | for (i = 0; i < VECTOR_CST_NELTS (t1); ++i) | |
1664 | if (!sem_variable::equals (VECTOR_CST_ELT (t1, i), | |
1665 | VECTOR_CST_ELT (t2, i))) | |
1666 | return 0; | |
1667 | ||
1668 | return 1; | |
1669 | } | |
b84d4347 | 1670 | case ARRAY_REF: |
46305737 JH |
1671 | case ARRAY_RANGE_REF: |
1672 | { | |
1673 | tree x1 = TREE_OPERAND (t1, 0); | |
1674 | tree x2 = TREE_OPERAND (t2, 0); | |
1675 | tree y1 = TREE_OPERAND (t1, 1); | |
1676 | tree y2 = TREE_OPERAND (t2, 1); | |
1677 | ||
3c031cbe | 1678 | if (!sem_variable::equals (x1, x2) || !sem_variable::equals (y1, y2)) |
46305737 JH |
1679 | return false; |
1680 | if (!sem_variable::equals (array_ref_low_bound (t1), | |
1681 | array_ref_low_bound (t2))) | |
1682 | return false; | |
1683 | if (!sem_variable::equals (array_ref_element_size (t1), | |
1684 | array_ref_element_size (t2))) | |
1685 | return false; | |
1686 | return true; | |
1687 | } | |
1688 | ||
1689 | case COMPONENT_REF: | |
b84d4347 | 1690 | case POINTER_PLUS_EXPR: |
46305737 JH |
1691 | case PLUS_EXPR: |
1692 | case MINUS_EXPR: | |
1693 | case RANGE_EXPR: | |
b84d4347 ML |
1694 | { |
1695 | tree x1 = TREE_OPERAND (t1, 0); | |
1696 | tree x2 = TREE_OPERAND (t2, 0); | |
1697 | tree y1 = TREE_OPERAND (t1, 1); | |
1698 | tree y2 = TREE_OPERAND (t2, 1); | |
1699 | ||
1700 | return sem_variable::equals (x1, x2) && sem_variable::equals (y1, y2); | |
1701 | } | |
46305737 JH |
1702 | |
1703 | CASE_CONVERT: | |
1704 | case VIEW_CONVERT_EXPR: | |
060cfff4 | 1705 | if (!func_checker::compatible_types_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
46305737 JH |
1706 | return return_false (); |
1707 | return sem_variable::equals (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)); | |
b84d4347 ML |
1708 | case ERROR_MARK: |
1709 | return return_false_with_msg ("ERROR_MARK"); | |
1710 | default: | |
1711 | return return_false_with_msg ("Unknown TREE code reached"); | |
1712 | } | |
1713 | } | |
1714 | ||
1715 | /* Parser function that visits a varpool NODE. */ | |
1716 | ||
1717 | sem_variable * | |
1718 | sem_variable::parse (varpool_node *node, bitmap_obstack *stack) | |
1719 | { | |
6fb12821 ML |
1720 | if (TREE_THIS_VOLATILE (node->decl) || DECL_HARD_REGISTER (node->decl) |
1721 | || node->alias) | |
b84d4347 ML |
1722 | return NULL; |
1723 | ||
1724 | sem_variable *v = new sem_variable (node, 0, stack); | |
1725 | ||
1726 | v->init (); | |
1727 | ||
1728 | return v; | |
1729 | } | |
1730 | ||
1731 | /* References independent hash function. */ | |
1732 | ||
1733 | hashval_t | |
1734 | sem_variable::get_hash (void) | |
1735 | { | |
1736 | if (hash) | |
b84d4347 | 1737 | |
c4c0f336 | 1738 | return hash; |
a8d93817 JH |
1739 | /* All WPA streamed in symbols should have their hashes computed at compile |
1740 | time. At this point, the constructor may not be in memory at all. | |
1741 | DECL_INITIAL (decl) would be error_mark_node in that case. */ | |
1742 | gcc_assert (!node->lto_file_data); | |
1743 | tree ctor = DECL_INITIAL (decl); | |
b84d4347 ML |
1744 | inchash::hash hstate; |
1745 | ||
1746 | hstate.add_int (456346417); | |
a8d93817 JH |
1747 | if (DECL_SIZE (decl) && tree_fits_shwi_p (DECL_SIZE (decl))) |
1748 | hstate.add_wide_int (tree_to_shwi (DECL_SIZE (decl))); | |
1749 | add_expr (ctor, hstate); | |
b84d4347 ML |
1750 | hash = hstate.end (); |
1751 | ||
1752 | return hash; | |
1753 | } | |
1754 | ||
1755 | /* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can | |
1756 | be applied. */ | |
1757 | ||
1758 | bool | |
1759 | sem_variable::merge (sem_item *alias_item) | |
1760 | { | |
1761 | gcc_assert (alias_item->type == VAR); | |
1762 | ||
f657d665 ML |
1763 | if (!sem_item::target_supports_symbol_aliases_p ()) |
1764 | { | |
1765 | if (dump_file) | |
0a7246ee JH |
1766 | fprintf (dump_file, "Not unifying; " |
1767 | "Symbol aliases are not supported by target\n\n"); | |
f657d665 ML |
1768 | return false; |
1769 | } | |
1770 | ||
b84d4347 ML |
1771 | sem_variable *alias_var = static_cast<sem_variable *> (alias_item); |
1772 | ||
1773 | varpool_node *original = get_node (); | |
1774 | varpool_node *alias = alias_var->get_node (); | |
1775 | bool original_discardable = false; | |
1776 | ||
0a7246ee JH |
1777 | bool original_address_matters = original->address_matters_p (); |
1778 | bool alias_address_matters = alias->address_matters_p (); | |
1779 | ||
b84d4347 | 1780 | /* See if original is in a section that can be discarded if the main |
0a7246ee JH |
1781 | symbol is not used. |
1782 | Also consider case where we have resolution info and we know that | |
1783 | original's definition is not going to be used. In this case we can not | |
1784 | create alias to original. */ | |
1785 | if (original->can_be_discarded_p () | |
1786 | || (node->resolution != LDPR_UNKNOWN | |
1787 | && !decl_binds_to_current_def_p (node->decl))) | |
b84d4347 ML |
1788 | original_discardable = true; |
1789 | ||
1790 | gcc_assert (!TREE_ASM_WRITTEN (alias->decl)); | |
1791 | ||
0a7246ee JH |
1792 | /* Constant pool machinery is not quite ready for aliases. |
1793 | TODO: varasm code contains logic for merging DECL_IN_CONSTANT_POOL. | |
1794 | For LTO merging does not happen that is an important missing feature. | |
1795 | We can enable merging with LTO if the DECL_IN_CONSTANT_POOL | |
1796 | flag is dropped and non-local symbol name is assigned. */ | |
1797 | if (DECL_IN_CONSTANT_POOL (alias->decl) | |
1798 | || DECL_IN_CONSTANT_POOL (original->decl)) | |
b84d4347 ML |
1799 | { |
1800 | if (dump_file) | |
0a7246ee JH |
1801 | fprintf (dump_file, |
1802 | "Not unifying; constant pool variables.\n\n"); | |
1803 | return false; | |
1804 | } | |
b84d4347 | 1805 | |
0a7246ee JH |
1806 | /* Do not attempt to mix functions from different user sections; |
1807 | we do not know what user intends with those. */ | |
1808 | if (((DECL_SECTION_NAME (original->decl) && !original->implicit_section) | |
1809 | || (DECL_SECTION_NAME (alias->decl) && !alias->implicit_section)) | |
1810 | && DECL_SECTION_NAME (original->decl) != DECL_SECTION_NAME (alias->decl)) | |
1811 | { | |
1812 | if (dump_file) | |
1813 | fprintf (dump_file, | |
1814 | "Not unifying; " | |
1815 | "original and alias are in different sections.\n\n"); | |
b84d4347 ML |
1816 | return false; |
1817 | } | |
0a7246ee JH |
1818 | |
1819 | /* We can not merge if address comparsion metters. */ | |
1820 | if (original_address_matters && alias_address_matters | |
1821 | && flag_merge_constants < 2) | |
b84d4347 | 1822 | { |
0a7246ee JH |
1823 | if (dump_file) |
1824 | fprintf (dump_file, | |
1825 | "Not unifying; " | |
1826 | "adress of original and alias may be compared.\n\n"); | |
1827 | return false; | |
1828 | } | |
c7a06bc1 JH |
1829 | if (DECL_COMDAT_GROUP (original->decl) != DECL_COMDAT_GROUP (alias->decl)) |
1830 | { | |
1831 | if (dump_file) | |
1832 | fprintf (dump_file, "Not unifying; alias cannot be created; " | |
1833 | "across comdat group boundary\n\n"); | |
1834 | ||
1835 | return false; | |
1836 | } | |
b84d4347 | 1837 | |
c7a06bc1 | 1838 | if (original_discardable) |
0a7246ee JH |
1839 | { |
1840 | if (dump_file) | |
1841 | fprintf (dump_file, "Not unifying; alias cannot be created; " | |
1842 | "target is discardable\n\n"); | |
b84d4347 | 1843 | |
0a7246ee JH |
1844 | return false; |
1845 | } | |
1846 | else | |
1847 | { | |
1848 | gcc_assert (!original->alias); | |
1849 | gcc_assert (!alias->alias); | |
b84d4347 ML |
1850 | |
1851 | alias->analyzed = false; | |
1852 | ||
1853 | DECL_INITIAL (alias->decl) = NULL; | |
412049de JH |
1854 | ((symtab_node *)alias)->call_for_symbol_and_aliases (clear_decl_rtl, |
1855 | NULL, true); | |
d5e254e1 | 1856 | alias->need_bounds_init = false; |
b84d4347 | 1857 | alias->remove_all_references (); |
0a7246ee JH |
1858 | if (TREE_ADDRESSABLE (alias->decl)) |
1859 | original->call_for_symbol_and_aliases (set_addressable, NULL, true); | |
b84d4347 ML |
1860 | |
1861 | varpool_node::create_alias (alias_var->decl, decl); | |
1862 | alias->resolve_alias (original); | |
1863 | ||
1864 | if (dump_file) | |
0a7246ee | 1865 | fprintf (dump_file, "Unified; Variable alias has been created.\n\n"); |
b84d4347 ML |
1866 | |
1867 | return true; | |
1868 | } | |
1869 | } | |
1870 | ||
b84d4347 ML |
1871 | /* Dump symbol to FILE. */ |
1872 | ||
1873 | void | |
1874 | sem_variable::dump_to_file (FILE *file) | |
1875 | { | |
1876 | gcc_assert (file); | |
1877 | ||
1878 | print_node (file, "", decl, 0); | |
1879 | fprintf (file, "\n\n"); | |
1880 | } | |
1881 | ||
b84d4347 ML |
1882 | unsigned int sem_item_optimizer::class_id = 0; |
1883 | ||
1884 | sem_item_optimizer::sem_item_optimizer (): worklist (0), m_classes (0), | |
1885 | m_classes_count (0), m_cgraph_node_hooks (NULL), m_varpool_node_hooks (NULL) | |
1886 | { | |
1887 | m_items.create (0); | |
1888 | bitmap_obstack_initialize (&m_bmstack); | |
1889 | } | |
1890 | ||
1891 | sem_item_optimizer::~sem_item_optimizer () | |
1892 | { | |
1893 | for (unsigned int i = 0; i < m_items.length (); i++) | |
1894 | delete m_items[i]; | |
1895 | ||
1896 | for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin (); | |
1897 | it != m_classes.end (); ++it) | |
1898 | { | |
1899 | for (unsigned int i = 0; i < (*it)->classes.length (); i++) | |
1900 | delete (*it)->classes[i]; | |
1901 | ||
1902 | (*it)->classes.release (); | |
ba2dbfce | 1903 | free (*it); |
b84d4347 ML |
1904 | } |
1905 | ||
1906 | m_items.release (); | |
1907 | ||
1908 | bitmap_obstack_release (&m_bmstack); | |
1909 | } | |
1910 | ||
1911 | /* Write IPA ICF summary for symbols. */ | |
1912 | ||
1913 | void | |
1914 | sem_item_optimizer::write_summary (void) | |
1915 | { | |
1916 | unsigned int count = 0; | |
1917 | ||
1918 | output_block *ob = create_output_block (LTO_section_ipa_icf); | |
1919 | lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder; | |
1920 | ob->symbol = NULL; | |
1921 | ||
1922 | /* Calculate number of symbols to be serialized. */ | |
1923 | for (lto_symtab_encoder_iterator lsei = lsei_start_in_partition (encoder); | |
1924 | !lsei_end_p (lsei); | |
1925 | lsei_next_in_partition (&lsei)) | |
1926 | { | |
1927 | symtab_node *node = lsei_node (lsei); | |
1928 | ||
1929 | if (m_symtab_node_map.get (node)) | |
1930 | count++; | |
1931 | } | |
1932 | ||
1933 | streamer_write_uhwi (ob, count); | |
1934 | ||
1935 | /* Process all of the symbols. */ | |
1936 | for (lto_symtab_encoder_iterator lsei = lsei_start_in_partition (encoder); | |
1937 | !lsei_end_p (lsei); | |
1938 | lsei_next_in_partition (&lsei)) | |
1939 | { | |
1940 | symtab_node *node = lsei_node (lsei); | |
1941 | ||
1942 | sem_item **item = m_symtab_node_map.get (node); | |
1943 | ||
1944 | if (item && *item) | |
1945 | { | |
1946 | int node_ref = lto_symtab_encoder_encode (encoder, node); | |
1947 | streamer_write_uhwi_stream (ob->main_stream, node_ref); | |
1948 | ||
1949 | streamer_write_uhwi (ob, (*item)->get_hash ()); | |
1950 | } | |
1951 | } | |
1952 | ||
1953 | streamer_write_char_stream (ob->main_stream, 0); | |
1954 | produce_asm (ob, NULL); | |
1955 | destroy_output_block (ob); | |
1956 | } | |
1957 | ||
1958 | /* Reads a section from LTO stream file FILE_DATA. Input block for DATA | |
1959 | contains LEN bytes. */ | |
1960 | ||
1961 | void | |
1962 | sem_item_optimizer::read_section (lto_file_decl_data *file_data, | |
1963 | const char *data, size_t len) | |
1964 | { | |
1965 | const lto_function_header *header = | |
1966 | (const lto_function_header *) data; | |
1967 | const int cfg_offset = sizeof (lto_function_header); | |
1968 | const int main_offset = cfg_offset + header->cfg_size; | |
1969 | const int string_offset = main_offset + header->main_size; | |
1970 | data_in *data_in; | |
1971 | unsigned int i; | |
1972 | unsigned int count; | |
1973 | ||
1974 | lto_input_block ib_main ((const char *) data + main_offset, 0, | |
db847fa8 | 1975 | header->main_size, file_data->mode_table); |
b84d4347 ML |
1976 | |
1977 | data_in = | |
1978 | lto_data_in_create (file_data, (const char *) data + string_offset, | |
1979 | header->string_size, vNULL); | |
1980 | ||
1981 | count = streamer_read_uhwi (&ib_main); | |
1982 | ||
1983 | for (i = 0; i < count; i++) | |
1984 | { | |
1985 | unsigned int index; | |
1986 | symtab_node *node; | |
1987 | lto_symtab_encoder_t encoder; | |
1988 | ||
1989 | index = streamer_read_uhwi (&ib_main); | |
1990 | encoder = file_data->symtab_node_encoder; | |
1991 | node = lto_symtab_encoder_deref (encoder, index); | |
1992 | ||
1993 | hashval_t hash = streamer_read_uhwi (&ib_main); | |
1994 | ||
1995 | gcc_assert (node->definition); | |
1996 | ||
1997 | if (dump_file) | |
1998 | fprintf (dump_file, "Symbol added:%s (tree: %p, uid:%u)\n", node->asm_name (), | |
1999 | (void *) node->decl, node->order); | |
2000 | ||
2001 | if (is_a<cgraph_node *> (node)) | |
2002 | { | |
2003 | cgraph_node *cnode = dyn_cast <cgraph_node *> (node); | |
2004 | ||
2005 | m_items.safe_push (new sem_function (cnode, hash, &m_bmstack)); | |
2006 | } | |
2007 | else | |
2008 | { | |
2009 | varpool_node *vnode = dyn_cast <varpool_node *> (node); | |
2010 | ||
2011 | m_items.safe_push (new sem_variable (vnode, hash, &m_bmstack)); | |
2012 | } | |
2013 | } | |
2014 | ||
2015 | lto_free_section_data (file_data, LTO_section_ipa_icf, NULL, data, | |
2016 | len); | |
2017 | lto_data_in_delete (data_in); | |
2018 | } | |
2019 | ||
2020 | /* Read IPA IPA ICF summary for symbols. */ | |
2021 | ||
2022 | void | |
2023 | sem_item_optimizer::read_summary (void) | |
2024 | { | |
2025 | lto_file_decl_data **file_data_vec = lto_get_file_decl_data (); | |
2026 | lto_file_decl_data *file_data; | |
2027 | unsigned int j = 0; | |
2028 | ||
2029 | while ((file_data = file_data_vec[j++])) | |
2030 | { | |
2031 | size_t len; | |
2032 | const char *data = lto_get_section_data (file_data, | |
2033 | LTO_section_ipa_icf, NULL, &len); | |
2034 | ||
2035 | if (data) | |
2036 | read_section (file_data, data, len); | |
2037 | } | |
2038 | } | |
2039 | ||
2040 | /* Register callgraph and varpool hooks. */ | |
2041 | ||
2042 | void | |
2043 | sem_item_optimizer::register_hooks (void) | |
2044 | { | |
e3e968e9 ML |
2045 | if (!m_cgraph_node_hooks) |
2046 | m_cgraph_node_hooks = symtab->add_cgraph_removal_hook | |
2047 | (&sem_item_optimizer::cgraph_removal_hook, this); | |
b84d4347 | 2048 | |
e3e968e9 ML |
2049 | if (!m_varpool_node_hooks) |
2050 | m_varpool_node_hooks = symtab->add_varpool_removal_hook | |
2051 | (&sem_item_optimizer::varpool_removal_hook, this); | |
b84d4347 ML |
2052 | } |
2053 | ||
2054 | /* Unregister callgraph and varpool hooks. */ | |
2055 | ||
2056 | void | |
2057 | sem_item_optimizer::unregister_hooks (void) | |
2058 | { | |
2059 | if (m_cgraph_node_hooks) | |
2060 | symtab->remove_cgraph_removal_hook (m_cgraph_node_hooks); | |
2061 | ||
2062 | if (m_varpool_node_hooks) | |
2063 | symtab->remove_varpool_removal_hook (m_varpool_node_hooks); | |
2064 | } | |
2065 | ||
2066 | /* Adds a CLS to hashtable associated by hash value. */ | |
2067 | ||
2068 | void | |
2069 | sem_item_optimizer::add_class (congruence_class *cls) | |
2070 | { | |
2071 | gcc_assert (cls->members.length ()); | |
2072 | ||
2073 | congruence_class_group *group = get_group_by_hash ( | |
2074 | cls->members[0]->get_hash (), | |
2075 | cls->members[0]->type); | |
2076 | group->classes.safe_push (cls); | |
2077 | } | |
2078 | ||
2079 | /* Gets a congruence class group based on given HASH value and TYPE. */ | |
2080 | ||
2081 | congruence_class_group * | |
2082 | sem_item_optimizer::get_group_by_hash (hashval_t hash, sem_item_type type) | |
2083 | { | |
2084 | congruence_class_group *item = XNEW (congruence_class_group); | |
2085 | item->hash = hash; | |
2086 | item->type = type; | |
2087 | ||
2088 | congruence_class_group **slot = m_classes.find_slot (item, INSERT); | |
2089 | ||
2090 | if (*slot) | |
2091 | free (item); | |
2092 | else | |
2093 | { | |
2094 | item->classes.create (1); | |
2095 | *slot = item; | |
2096 | } | |
2097 | ||
2098 | return *slot; | |
2099 | } | |
2100 | ||
2101 | /* Callgraph removal hook called for a NODE with a custom DATA. */ | |
2102 | ||
2103 | void | |
2104 | sem_item_optimizer::cgraph_removal_hook (cgraph_node *node, void *data) | |
2105 | { | |
2106 | sem_item_optimizer *optimizer = (sem_item_optimizer *) data; | |
2107 | optimizer->remove_symtab_node (node); | |
2108 | } | |
2109 | ||
2110 | /* Varpool removal hook called for a NODE with a custom DATA. */ | |
2111 | ||
2112 | void | |
2113 | sem_item_optimizer::varpool_removal_hook (varpool_node *node, void *data) | |
2114 | { | |
2115 | sem_item_optimizer *optimizer = (sem_item_optimizer *) data; | |
2116 | optimizer->remove_symtab_node (node); | |
2117 | } | |
2118 | ||
2119 | /* Remove symtab NODE triggered by symtab removal hooks. */ | |
2120 | ||
2121 | void | |
2122 | sem_item_optimizer::remove_symtab_node (symtab_node *node) | |
2123 | { | |
2124 | gcc_assert (!m_classes.elements()); | |
2125 | ||
2126 | m_removed_items_set.add (node); | |
2127 | } | |
2128 | ||
2129 | void | |
2130 | sem_item_optimizer::remove_item (sem_item *item) | |
2131 | { | |
2132 | if (m_symtab_node_map.get (item->node)) | |
2133 | m_symtab_node_map.remove (item->node); | |
2134 | delete item; | |
2135 | } | |
2136 | ||
2137 | /* Removes all callgraph and varpool nodes that are marked by symtab | |
2138 | as deleted. */ | |
2139 | ||
2140 | void | |
2141 | sem_item_optimizer::filter_removed_items (void) | |
2142 | { | |
2143 | auto_vec <sem_item *> filtered; | |
2144 | ||
2145 | for (unsigned int i = 0; i < m_items.length(); i++) | |
2146 | { | |
2147 | sem_item *item = m_items[i]; | |
2148 | ||
90190bb3 ML |
2149 | if (m_removed_items_set.contains (item->node)) |
2150 | { | |
b84d4347 ML |
2151 | remove_item (item); |
2152 | continue; | |
90190bb3 | 2153 | } |
b84d4347 ML |
2154 | |
2155 | if (item->type == FUNC) | |
90190bb3 | 2156 | { |
b84d4347 ML |
2157 | cgraph_node *cnode = static_cast <sem_function *>(item)->get_node (); |
2158 | ||
060cfff4 | 2159 | if (in_lto_p && (cnode->alias || cnode->body_removed)) |
90190bb3 ML |
2160 | remove_item (item); |
2161 | else | |
2162 | filtered.safe_push (item); | |
2163 | } | |
2164 | else /* VAR. */ | |
2165 | { | |
2166 | if (!flag_ipa_icf_variables) | |
2167 | remove_item (item); | |
2168 | else | |
c4c0f336 ML |
2169 | { |
2170 | /* Filter out non-readonly variables. */ | |
2171 | tree decl = item->decl; | |
2172 | if (TREE_READONLY (decl)) | |
2173 | filtered.safe_push (item); | |
2174 | else | |
2175 | remove_item (item); | |
2176 | } | |
90190bb3 | 2177 | } |
b84d4347 ML |
2178 | } |
2179 | ||
2180 | /* Clean-up of released semantic items. */ | |
2181 | ||
2182 | m_items.release (); | |
2183 | for (unsigned int i = 0; i < filtered.length(); i++) | |
2184 | m_items.safe_push (filtered[i]); | |
2185 | } | |
2186 | ||
bd31fe14 ML |
2187 | /* Optimizer entry point which returns true in case it processes |
2188 | a merge operation. True is returned if there's a merge operation | |
2189 | processed. */ | |
b84d4347 | 2190 | |
bd31fe14 | 2191 | bool |
b84d4347 ML |
2192 | sem_item_optimizer::execute (void) |
2193 | { | |
2194 | filter_removed_items (); | |
8b048701 JJ |
2195 | unregister_hooks (); |
2196 | ||
b84d4347 ML |
2197 | build_hash_based_classes (); |
2198 | ||
2199 | if (dump_file) | |
2200 | fprintf (dump_file, "Dump after hash based groups\n"); | |
2201 | dump_cong_classes (); | |
2202 | ||
2203 | for (unsigned int i = 0; i < m_items.length(); i++) | |
2204 | m_items[i]->init_wpa (); | |
2205 | ||
2206 | build_graph (); | |
2207 | ||
2208 | subdivide_classes_by_equality (true); | |
2209 | ||
2210 | if (dump_file) | |
2211 | fprintf (dump_file, "Dump after WPA based types groups\n"); | |
2212 | ||
2213 | dump_cong_classes (); | |
2214 | ||
2215 | process_cong_reduction (); | |
2216 | verify_classes (); | |
2217 | ||
2218 | if (dump_file) | |
2219 | fprintf (dump_file, "Dump after callgraph-based congruence reduction\n"); | |
2220 | ||
2221 | dump_cong_classes (); | |
2222 | ||
2223 | parse_nonsingleton_classes (); | |
2224 | subdivide_classes_by_equality (); | |
2225 | ||
2226 | if (dump_file) | |
2227 | fprintf (dump_file, "Dump after full equality comparison of groups\n"); | |
2228 | ||
2229 | dump_cong_classes (); | |
2230 | ||
2231 | unsigned int prev_class_count = m_classes_count; | |
2232 | ||
2233 | process_cong_reduction (); | |
2234 | dump_cong_classes (); | |
2235 | verify_classes (); | |
bd31fe14 | 2236 | bool merged_p = merge_classes (prev_class_count); |
b84d4347 ML |
2237 | |
2238 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2239 | symtab_node::dump_table (dump_file); | |
bd31fe14 ML |
2240 | |
2241 | return merged_p; | |
b84d4347 ML |
2242 | } |
2243 | ||
2244 | /* Function responsible for visiting all potential functions and | |
2245 | read-only variables that can be merged. */ | |
2246 | ||
2247 | void | |
2248 | sem_item_optimizer::parse_funcs_and_vars (void) | |
2249 | { | |
2250 | cgraph_node *cnode; | |
2251 | ||
2252 | if (flag_ipa_icf_functions) | |
2253 | FOR_EACH_DEFINED_FUNCTION (cnode) | |
2254 | { | |
2255 | sem_function *f = sem_function::parse (cnode, &m_bmstack); | |
2256 | if (f) | |
2257 | { | |
2258 | m_items.safe_push (f); | |
2259 | m_symtab_node_map.put (cnode, f); | |
2260 | ||
2261 | if (dump_file) | |
2262 | fprintf (dump_file, "Parsed function:%s\n", f->asm_name ()); | |
2263 | ||
2264 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2265 | f->dump_to_file (dump_file); | |
2266 | } | |
2267 | else if (dump_file) | |
2268 | fprintf (dump_file, "Not parsed function:%s\n", cnode->asm_name ()); | |
2269 | } | |
2270 | ||
2271 | varpool_node *vnode; | |
2272 | ||
2273 | if (flag_ipa_icf_variables) | |
2274 | FOR_EACH_DEFINED_VARIABLE (vnode) | |
2275 | { | |
2276 | sem_variable *v = sem_variable::parse (vnode, &m_bmstack); | |
2277 | ||
2278 | if (v) | |
2279 | { | |
2280 | m_items.safe_push (v); | |
2281 | m_symtab_node_map.put (vnode, v); | |
2282 | } | |
2283 | } | |
2284 | } | |
2285 | ||
2286 | /* Makes pairing between a congruence class CLS and semantic ITEM. */ | |
2287 | ||
2288 | void | |
2289 | sem_item_optimizer::add_item_to_class (congruence_class *cls, sem_item *item) | |
2290 | { | |
2291 | item->index_in_class = cls->members.length (); | |
2292 | cls->members.safe_push (item); | |
2293 | item->cls = cls; | |
2294 | } | |
2295 | ||
2296 | /* Congruence classes are built by hash value. */ | |
2297 | ||
2298 | void | |
2299 | sem_item_optimizer::build_hash_based_classes (void) | |
2300 | { | |
2301 | for (unsigned i = 0; i < m_items.length (); i++) | |
2302 | { | |
2303 | sem_item *item = m_items[i]; | |
2304 | ||
2305 | congruence_class_group *group = get_group_by_hash (item->get_hash (), | |
2306 | item->type); | |
2307 | ||
2308 | if (!group->classes.length ()) | |
2309 | { | |
2310 | m_classes_count++; | |
2311 | group->classes.safe_push (new congruence_class (class_id++)); | |
2312 | } | |
2313 | ||
2314 | add_item_to_class (group->classes[0], item); | |
2315 | } | |
2316 | } | |
2317 | ||
2318 | /* Build references according to call graph. */ | |
2319 | ||
2320 | void | |
2321 | sem_item_optimizer::build_graph (void) | |
2322 | { | |
2323 | for (unsigned i = 0; i < m_items.length (); i++) | |
2324 | { | |
2325 | sem_item *item = m_items[i]; | |
2326 | m_symtab_node_map.put (item->node, item); | |
2327 | } | |
2328 | ||
2329 | for (unsigned i = 0; i < m_items.length (); i++) | |
2330 | { | |
2331 | sem_item *item = m_items[i]; | |
2332 | ||
2333 | if (item->type == FUNC) | |
2334 | { | |
2335 | cgraph_node *cnode = dyn_cast <cgraph_node *> (item->node); | |
2336 | ||
2337 | cgraph_edge *e = cnode->callees; | |
2338 | while (e) | |
2339 | { | |
b6cddc7f ML |
2340 | sem_item **slot = m_symtab_node_map.get |
2341 | (e->callee->ultimate_alias_target ()); | |
b84d4347 ML |
2342 | if (slot) |
2343 | item->add_reference (*slot); | |
2344 | ||
2345 | e = e->next_callee; | |
2346 | } | |
2347 | } | |
2348 | ||
2349 | ipa_ref *ref = NULL; | |
2350 | for (unsigned i = 0; item->node->iterate_reference (i, ref); i++) | |
2351 | { | |
b6cddc7f ML |
2352 | sem_item **slot = m_symtab_node_map.get |
2353 | (ref->referred->ultimate_alias_target ()); | |
b84d4347 ML |
2354 | if (slot) |
2355 | item->add_reference (*slot); | |
2356 | } | |
2357 | } | |
2358 | } | |
2359 | ||
2360 | /* Semantic items in classes having more than one element and initialized. | |
2361 | In case of WPA, we load function body. */ | |
2362 | ||
2363 | void | |
2364 | sem_item_optimizer::parse_nonsingleton_classes (void) | |
2365 | { | |
2366 | unsigned int init_called_count = 0; | |
2367 | ||
2368 | for (unsigned i = 0; i < m_items.length (); i++) | |
2369 | if (m_items[i]->cls->members.length () > 1) | |
2370 | { | |
2371 | m_items[i]->init (); | |
2372 | init_called_count++; | |
2373 | } | |
2374 | ||
2375 | if (dump_file) | |
2376 | fprintf (dump_file, "Init called for %u items (%.2f%%).\n", init_called_count, | |
f1c859ee | 2377 | m_items.length () ? 100.0f * init_called_count / m_items.length (): 0.0f); |
b84d4347 ML |
2378 | } |
2379 | ||
2380 | /* Equality function for semantic items is used to subdivide existing | |
2381 | classes. If IN_WPA, fast equality function is invoked. */ | |
2382 | ||
2383 | void | |
2384 | sem_item_optimizer::subdivide_classes_by_equality (bool in_wpa) | |
2385 | { | |
2386 | for (hash_table <congruence_class_group_hash>::iterator it = m_classes.begin (); | |
2387 | it != m_classes.end (); ++it) | |
2388 | { | |
2389 | unsigned int class_count = (*it)->classes.length (); | |
2390 | ||
2391 | for (unsigned i = 0; i < class_count; i++) | |
2392 | { | |
2393 | congruence_class *c = (*it)->classes [i]; | |
2394 | ||
2395 | if (c->members.length() > 1) | |
2396 | { | |
2397 | auto_vec <sem_item *> new_vector; | |
2398 | ||
2399 | sem_item *first = c->members[0]; | |
2400 | new_vector.safe_push (first); | |
2401 | ||
2402 | unsigned class_split_first = (*it)->classes.length (); | |
2403 | ||
2404 | for (unsigned j = 1; j < c->members.length (); j++) | |
2405 | { | |
2406 | sem_item *item = c->members[j]; | |
2407 | ||
2408 | bool equals = in_wpa ? first->equals_wpa (item, | |
2409 | m_symtab_node_map) : first->equals (item, m_symtab_node_map); | |
2410 | ||
2411 | if (equals) | |
2412 | new_vector.safe_push (item); | |
2413 | else | |
2414 | { | |
2415 | bool integrated = false; | |
2416 | ||
2417 | for (unsigned k = class_split_first; k < (*it)->classes.length (); k++) | |
2418 | { | |
2419 | sem_item *x = (*it)->classes[k]->members[0]; | |
2420 | bool equals = in_wpa ? x->equals_wpa (item, | |
2421 | m_symtab_node_map) : x->equals (item, m_symtab_node_map); | |
2422 | ||
2423 | if (equals) | |
2424 | { | |
2425 | integrated = true; | |
2426 | add_item_to_class ((*it)->classes[k], item); | |
2427 | ||
2428 | break; | |
2429 | } | |
2430 | } | |
2431 | ||
2432 | if (!integrated) | |
2433 | { | |
2434 | congruence_class *c = new congruence_class (class_id++); | |
2435 | m_classes_count++; | |
2436 | add_item_to_class (c, item); | |
2437 | ||
2438 | (*it)->classes.safe_push (c); | |
2439 | } | |
2440 | } | |
2441 | } | |
2442 | ||
2443 | // we replace newly created new_vector for the class we've just splitted | |
2444 | c->members.release (); | |
2445 | c->members.create (new_vector.length ()); | |
2446 | ||
2447 | for (unsigned int j = 0; j < new_vector.length (); j++) | |
2448 | add_item_to_class (c, new_vector[j]); | |
2449 | } | |
2450 | } | |
2451 | } | |
2452 | ||
2453 | verify_classes (); | |
2454 | } | |
2455 | ||
5ebd0e61 ML |
2456 | /* Subdivide classes by address references that members of the class |
2457 | reference. Example can be a pair of functions that have an address | |
2458 | taken from a function. If these addresses are different the class | |
2459 | is split. */ | |
2460 | ||
2461 | unsigned | |
2462 | sem_item_optimizer::subdivide_classes_by_sensitive_refs () | |
2463 | { | |
2464 | unsigned newly_created_classes = 0; | |
2465 | ||
2466 | for (hash_table <congruence_class_group_hash>::iterator it = m_classes.begin (); | |
2467 | it != m_classes.end (); ++it) | |
2468 | { | |
2469 | unsigned int class_count = (*it)->classes.length (); | |
2470 | auto_vec<congruence_class *> new_classes; | |
2471 | ||
2472 | for (unsigned i = 0; i < class_count; i++) | |
2473 | { | |
2474 | congruence_class *c = (*it)->classes [i]; | |
2475 | ||
2476 | if (c->members.length() > 1) | |
2477 | { | |
2478 | hash_map <symbol_compare_collection *, vec <sem_item *>, | |
2479 | symbol_compare_hashmap_traits> split_map; | |
2480 | ||
2481 | for (unsigned j = 0; j < c->members.length (); j++) | |
2482 | { | |
2483 | sem_item *source_node = c->members[j]; | |
2484 | ||
2485 | symbol_compare_collection *collection = new symbol_compare_collection (source_node->node); | |
2486 | ||
2487 | vec <sem_item *> *slot = &split_map.get_or_insert (collection); | |
2488 | gcc_checking_assert (slot); | |
2489 | ||
2490 | slot->safe_push (source_node); | |
2491 | } | |
2492 | ||
2493 | /* If the map contains more than one key, we have to split the map | |
2494 | appropriately. */ | |
2495 | if (split_map.elements () != 1) | |
2496 | { | |
2497 | bool first_class = true; | |
2498 | ||
2499 | hash_map <symbol_compare_collection *, vec <sem_item *>, | |
2500 | symbol_compare_hashmap_traits>::iterator it2 = split_map.begin (); | |
2501 | for (; it2 != split_map.end (); ++it2) | |
2502 | { | |
2503 | congruence_class *new_cls; | |
2504 | new_cls = new congruence_class (class_id++); | |
2505 | ||
2506 | for (unsigned k = 0; k < (*it2).second.length (); k++) | |
2507 | add_item_to_class (new_cls, (*it2).second[k]); | |
2508 | ||
2509 | worklist_push (new_cls); | |
2510 | newly_created_classes++; | |
2511 | ||
2512 | if (first_class) | |
2513 | { | |
2514 | (*it)->classes[i] = new_cls; | |
2515 | first_class = false; | |
2516 | } | |
2517 | else | |
2518 | { | |
2519 | new_classes.safe_push (new_cls); | |
2520 | m_classes_count++; | |
2521 | } | |
2522 | } | |
2523 | } | |
2524 | } | |
2525 | } | |
2526 | ||
2527 | for (unsigned i = 0; i < new_classes.length (); i++) | |
2528 | (*it)->classes.safe_push (new_classes[i]); | |
2529 | } | |
2530 | ||
2531 | return newly_created_classes; | |
2532 | } | |
2533 | ||
b84d4347 ML |
2534 | /* Verify congruence classes if checking is enabled. */ |
2535 | ||
2536 | void | |
2537 | sem_item_optimizer::verify_classes (void) | |
2538 | { | |
2539 | #if ENABLE_CHECKING | |
2540 | for (hash_table <congruence_class_group_hash>::iterator it = m_classes.begin (); | |
2541 | it != m_classes.end (); ++it) | |
2542 | { | |
2543 | for (unsigned int i = 0; i < (*it)->classes.length (); i++) | |
2544 | { | |
2545 | congruence_class *cls = (*it)->classes[i]; | |
2546 | ||
2547 | gcc_checking_assert (cls); | |
2548 | gcc_checking_assert (cls->members.length () > 0); | |
2549 | ||
2550 | for (unsigned int j = 0; j < cls->members.length (); j++) | |
2551 | { | |
2552 | sem_item *item = cls->members[j]; | |
2553 | ||
2554 | gcc_checking_assert (item); | |
2555 | gcc_checking_assert (item->cls == cls); | |
2556 | ||
2557 | for (unsigned k = 0; k < item->usages.length (); k++) | |
2558 | { | |
2559 | sem_usage_pair *usage = item->usages[k]; | |
2560 | gcc_checking_assert (usage->item->index_in_class < | |
2561 | usage->item->cls->members.length ()); | |
2562 | } | |
2563 | } | |
2564 | } | |
2565 | } | |
2566 | #endif | |
2567 | } | |
2568 | ||
2569 | /* Disposes split map traverse function. CLS_PTR is pointer to congruence | |
2570 | class, BSLOT is bitmap slot we want to release. DATA is mandatory, | |
2571 | but unused argument. */ | |
2572 | ||
2573 | bool | |
2574 | sem_item_optimizer::release_split_map (congruence_class * const &, | |
2575 | bitmap const &b, traverse_split_pair *) | |
2576 | { | |
2577 | bitmap bmp = b; | |
2578 | ||
2579 | BITMAP_FREE (bmp); | |
2580 | ||
2581 | return true; | |
2582 | } | |
2583 | ||
2584 | /* Process split operation for a class given as pointer CLS_PTR, | |
2585 | where bitmap B splits congruence class members. DATA is used | |
2586 | as argument of split pair. */ | |
2587 | ||
2588 | bool | |
2589 | sem_item_optimizer::traverse_congruence_split (congruence_class * const &cls, | |
2590 | bitmap const &b, traverse_split_pair *pair) | |
2591 | { | |
2592 | sem_item_optimizer *optimizer = pair->optimizer; | |
2593 | const congruence_class *splitter_cls = pair->cls; | |
2594 | ||
2595 | /* If counted bits are greater than zero and less than the number of members | |
2596 | a group will be splitted. */ | |
2597 | unsigned popcount = bitmap_count_bits (b); | |
2598 | ||
2599 | if (popcount > 0 && popcount < cls->members.length ()) | |
2600 | { | |
2601 | congruence_class* newclasses[2] = { new congruence_class (class_id++), new congruence_class (class_id++) }; | |
2602 | ||
2603 | for (unsigned int i = 0; i < cls->members.length (); i++) | |
2604 | { | |
2605 | int target = bitmap_bit_p (b, i); | |
2606 | congruence_class *tc = newclasses[target]; | |
2607 | ||
2608 | add_item_to_class (tc, cls->members[i]); | |
2609 | } | |
2610 | ||
2611 | #ifdef ENABLE_CHECKING | |
2612 | for (unsigned int i = 0; i < 2; i++) | |
2613 | gcc_checking_assert (newclasses[i]->members.length ()); | |
2614 | #endif | |
2615 | ||
2616 | if (splitter_cls == cls) | |
2617 | optimizer->splitter_class_removed = true; | |
2618 | ||
2619 | /* Remove old class from worklist if presented. */ | |
2620 | bool in_worklist = cls->in_worklist; | |
2621 | ||
2622 | if (in_worklist) | |
2623 | cls->in_worklist = false; | |
2624 | ||
2625 | congruence_class_group g; | |
2626 | g.hash = cls->members[0]->get_hash (); | |
2627 | g.type = cls->members[0]->type; | |
2628 | ||
2629 | congruence_class_group *slot = optimizer->m_classes.find(&g); | |
2630 | ||
2631 | for (unsigned int i = 0; i < slot->classes.length (); i++) | |
2632 | if (slot->classes[i] == cls) | |
2633 | { | |
2634 | slot->classes.ordered_remove (i); | |
2635 | break; | |
2636 | } | |
2637 | ||
2638 | /* New class will be inserted and integrated to work list. */ | |
2639 | for (unsigned int i = 0; i < 2; i++) | |
2640 | optimizer->add_class (newclasses[i]); | |
2641 | ||
2642 | /* Two classes replace one, so that increment just by one. */ | |
2643 | optimizer->m_classes_count++; | |
2644 | ||
2645 | /* If OLD class was presented in the worklist, we remove the class | |
2646 | and replace it will both newly created classes. */ | |
2647 | if (in_worklist) | |
2648 | for (unsigned int i = 0; i < 2; i++) | |
2649 | optimizer->worklist_push (newclasses[i]); | |
2650 | else /* Just smaller class is inserted. */ | |
2651 | { | |
2652 | unsigned int smaller_index = newclasses[0]->members.length () < | |
2653 | newclasses[1]->members.length () ? | |
2654 | 0 : 1; | |
2655 | optimizer->worklist_push (newclasses[smaller_index]); | |
2656 | } | |
2657 | ||
2658 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2659 | { | |
2660 | fprintf (dump_file, " congruence class splitted:\n"); | |
2661 | cls->dump (dump_file, 4); | |
2662 | ||
2663 | fprintf (dump_file, " newly created groups:\n"); | |
2664 | for (unsigned int i = 0; i < 2; i++) | |
2665 | newclasses[i]->dump (dump_file, 4); | |
2666 | } | |
2667 | ||
2668 | /* Release class if not presented in work list. */ | |
2669 | if (!in_worklist) | |
2670 | delete cls; | |
2671 | } | |
2672 | ||
2673 | ||
2674 | return true; | |
2675 | } | |
2676 | ||
2677 | /* Tests if a class CLS used as INDEXth splits any congruence classes. | |
2678 | Bitmap stack BMSTACK is used for bitmap allocation. */ | |
2679 | ||
2680 | void | |
2681 | sem_item_optimizer::do_congruence_step_for_index (congruence_class *cls, | |
2682 | unsigned int index) | |
2683 | { | |
2684 | hash_map <congruence_class *, bitmap> split_map; | |
2685 | ||
2686 | for (unsigned int i = 0; i < cls->members.length (); i++) | |
2687 | { | |
2688 | sem_item *item = cls->members[i]; | |
2689 | ||
2690 | /* Iterate all usages that have INDEX as usage of the item. */ | |
2691 | for (unsigned int j = 0; j < item->usages.length (); j++) | |
2692 | { | |
2693 | sem_usage_pair *usage = item->usages[j]; | |
2694 | ||
2695 | if (usage->index != index) | |
2696 | continue; | |
2697 | ||
2698 | bitmap *slot = split_map.get (usage->item->cls); | |
2699 | bitmap b; | |
2700 | ||
2701 | if(!slot) | |
2702 | { | |
2703 | b = BITMAP_ALLOC (&m_bmstack); | |
2704 | split_map.put (usage->item->cls, b); | |
2705 | } | |
2706 | else | |
2707 | b = *slot; | |
2708 | ||
2709 | #if ENABLE_CHECKING | |
2710 | gcc_checking_assert (usage->item->cls); | |
2711 | gcc_checking_assert (usage->item->index_in_class < | |
2712 | usage->item->cls->members.length ()); | |
2713 | #endif | |
2714 | ||
2715 | bitmap_set_bit (b, usage->item->index_in_class); | |
2716 | } | |
2717 | } | |
2718 | ||
2719 | traverse_split_pair pair; | |
2720 | pair.optimizer = this; | |
2721 | pair.cls = cls; | |
2722 | ||
2723 | splitter_class_removed = false; | |
2724 | split_map.traverse | |
2725 | <traverse_split_pair *, sem_item_optimizer::traverse_congruence_split> (&pair); | |
2726 | ||
2727 | /* Bitmap clean-up. */ | |
2728 | split_map.traverse | |
2729 | <traverse_split_pair *, sem_item_optimizer::release_split_map> (NULL); | |
2730 | } | |
2731 | ||
2732 | /* Every usage of a congruence class CLS is a candidate that can split the | |
2733 | collection of classes. Bitmap stack BMSTACK is used for bitmap | |
2734 | allocation. */ | |
2735 | ||
2736 | void | |
2737 | sem_item_optimizer::do_congruence_step (congruence_class *cls) | |
2738 | { | |
2739 | bitmap_iterator bi; | |
2740 | unsigned int i; | |
2741 | ||
2742 | bitmap usage = BITMAP_ALLOC (&m_bmstack); | |
2743 | ||
2744 | for (unsigned int i = 0; i < cls->members.length (); i++) | |
2745 | bitmap_ior_into (usage, cls->members[i]->usage_index_bitmap); | |
2746 | ||
2747 | EXECUTE_IF_SET_IN_BITMAP (usage, 0, i, bi) | |
2748 | { | |
2749 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2750 | fprintf (dump_file, " processing congruece step for class: %u, index: %u\n", | |
2751 | cls->id, i); | |
2752 | ||
2753 | do_congruence_step_for_index (cls, i); | |
2754 | ||
2755 | if (splitter_class_removed) | |
2756 | break; | |
2757 | } | |
2758 | ||
2759 | BITMAP_FREE (usage); | |
2760 | } | |
2761 | ||
2762 | /* Adds a newly created congruence class CLS to worklist. */ | |
2763 | ||
2764 | void | |
2765 | sem_item_optimizer::worklist_push (congruence_class *cls) | |
2766 | { | |
2767 | /* Return if the class CLS is already presented in work list. */ | |
2768 | if (cls->in_worklist) | |
2769 | return; | |
2770 | ||
2771 | cls->in_worklist = true; | |
2772 | worklist.push_back (cls); | |
2773 | } | |
2774 | ||
2775 | /* Pops a class from worklist. */ | |
2776 | ||
2777 | congruence_class * | |
2778 | sem_item_optimizer::worklist_pop (void) | |
2779 | { | |
2780 | congruence_class *cls; | |
2781 | ||
2782 | while (!worklist.empty ()) | |
2783 | { | |
2784 | cls = worklist.front (); | |
2785 | worklist.pop_front (); | |
2786 | if (cls->in_worklist) | |
2787 | { | |
2788 | cls->in_worklist = false; | |
2789 | ||
2790 | return cls; | |
2791 | } | |
2792 | else | |
2793 | { | |
2794 | /* Work list item was already intended to be removed. | |
2795 | The only reason for doing it is to split a class. | |
2796 | Thus, the class CLS is deleted. */ | |
2797 | delete cls; | |
2798 | } | |
2799 | } | |
2800 | ||
2801 | return NULL; | |
2802 | } | |
2803 | ||
2804 | /* Iterative congruence reduction function. */ | |
2805 | ||
2806 | void | |
2807 | sem_item_optimizer::process_cong_reduction (void) | |
2808 | { | |
2809 | for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin (); | |
2810 | it != m_classes.end (); ++it) | |
2811 | for (unsigned i = 0; i < (*it)->classes.length (); i++) | |
2812 | if ((*it)->classes[i]->is_class_used ()) | |
2813 | worklist_push ((*it)->classes[i]); | |
2814 | ||
2815 | if (dump_file) | |
2816 | fprintf (dump_file, "Worklist has been filled with: %lu\n", | |
10568163 | 2817 | (unsigned long) worklist.size ()); |
b84d4347 ML |
2818 | |
2819 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2820 | fprintf (dump_file, "Congruence class reduction\n"); | |
2821 | ||
2822 | congruence_class *cls; | |
5ebd0e61 ML |
2823 | |
2824 | /* Process complete congruence reduction. */ | |
b84d4347 ML |
2825 | while ((cls = worklist_pop ()) != NULL) |
2826 | do_congruence_step (cls); | |
5ebd0e61 ML |
2827 | |
2828 | /* Subdivide newly created classes according to references. */ | |
2829 | unsigned new_classes = subdivide_classes_by_sensitive_refs (); | |
2830 | ||
2831 | if (dump_file) | |
2832 | fprintf (dump_file, "Address reference subdivision created: %u " | |
2833 | "new classes.\n", new_classes); | |
b84d4347 ML |
2834 | } |
2835 | ||
2836 | /* Debug function prints all informations about congruence classes. */ | |
2837 | ||
2838 | void | |
2839 | sem_item_optimizer::dump_cong_classes (void) | |
2840 | { | |
2841 | if (!dump_file) | |
2842 | return; | |
2843 | ||
2844 | fprintf (dump_file, | |
2845 | "Congruence classes: %u (unique hash values: %lu), with total: %u items\n", | |
10568163 | 2846 | m_classes_count, (unsigned long) m_classes.elements(), m_items.length ()); |
b84d4347 ML |
2847 | |
2848 | /* Histogram calculation. */ | |
2849 | unsigned int max_index = 0; | |
2850 | unsigned int* histogram = XCNEWVEC (unsigned int, m_items.length () + 1); | |
2851 | ||
2852 | for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin (); | |
2853 | it != m_classes.end (); ++it) | |
2854 | ||
2855 | for (unsigned i = 0; i < (*it)->classes.length (); i++) | |
2856 | { | |
2857 | unsigned int c = (*it)->classes[i]->members.length (); | |
2858 | histogram[c]++; | |
2859 | ||
2860 | if (c > max_index) | |
2861 | max_index = c; | |
2862 | } | |
2863 | ||
2864 | fprintf (dump_file, | |
2865 | "Class size histogram [num of members]: number of classe number of classess\n"); | |
2866 | ||
2867 | for (unsigned int i = 0; i <= max_index; i++) | |
2868 | if (histogram[i]) | |
2869 | fprintf (dump_file, "[%u]: %u classes\n", i, histogram[i]); | |
2870 | ||
2871 | fprintf (dump_file, "\n\n"); | |
2872 | ||
2873 | ||
2874 | if (dump_flags & TDF_DETAILS) | |
2875 | for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin (); | |
2876 | it != m_classes.end (); ++it) | |
2877 | { | |
2878 | fprintf (dump_file, " group: with %u classes:\n", (*it)->classes.length ()); | |
2879 | ||
2880 | for (unsigned i = 0; i < (*it)->classes.length (); i++) | |
2881 | { | |
2882 | (*it)->classes[i]->dump (dump_file, 4); | |
2883 | ||
2884 | if(i < (*it)->classes.length () - 1) | |
2885 | fprintf (dump_file, " "); | |
2886 | } | |
2887 | } | |
2888 | ||
2889 | free (histogram); | |
2890 | } | |
2891 | ||
2892 | /* After reduction is done, we can declare all items in a group | |
2893 | to be equal. PREV_CLASS_COUNT is start number of classes | |
bd31fe14 ML |
2894 | before reduction. True is returned if there's a merge operation |
2895 | processed. */ | |
b84d4347 | 2896 | |
bd31fe14 | 2897 | bool |
b84d4347 ML |
2898 | sem_item_optimizer::merge_classes (unsigned int prev_class_count) |
2899 | { | |
2900 | unsigned int item_count = m_items.length (); | |
2901 | unsigned int class_count = m_classes_count; | |
2902 | unsigned int equal_items = item_count - class_count; | |
2903 | ||
2904 | unsigned int non_singular_classes_count = 0; | |
2905 | unsigned int non_singular_classes_sum = 0; | |
2906 | ||
bd31fe14 ML |
2907 | bool merged_p = false; |
2908 | ||
b84d4347 ML |
2909 | for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin (); |
2910 | it != m_classes.end (); ++it) | |
2911 | for (unsigned int i = 0; i < (*it)->classes.length (); i++) | |
2912 | { | |
2913 | congruence_class *c = (*it)->classes[i]; | |
2914 | if (c->members.length () > 1) | |
2915 | { | |
2916 | non_singular_classes_count++; | |
2917 | non_singular_classes_sum += c->members.length (); | |
2918 | } | |
2919 | } | |
2920 | ||
2921 | if (dump_file) | |
2922 | { | |
2923 | fprintf (dump_file, "\nItem count: %u\n", item_count); | |
2924 | fprintf (dump_file, "Congruent classes before: %u, after: %u\n", | |
2925 | prev_class_count, class_count); | |
2926 | fprintf (dump_file, "Average class size before: %.2f, after: %.2f\n", | |
f1c859ee ML |
2927 | prev_class_count ? 1.0f * item_count / prev_class_count : 0.0f, |
2928 | class_count ? 1.0f * item_count / class_count : 0.0f); | |
b84d4347 | 2929 | fprintf (dump_file, "Average non-singular class size: %.2f, count: %u\n", |
f1c859ee ML |
2930 | non_singular_classes_count ? 1.0f * non_singular_classes_sum / |
2931 | non_singular_classes_count : 0.0f, | |
b84d4347 ML |
2932 | non_singular_classes_count); |
2933 | fprintf (dump_file, "Equal symbols: %u\n", equal_items); | |
2934 | fprintf (dump_file, "Fraction of visited symbols: %.2f%%\n\n", | |
f1c859ee | 2935 | item_count ? 100.0f * equal_items / item_count : 0.0f); |
b84d4347 ML |
2936 | } |
2937 | ||
2938 | for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin (); | |
2939 | it != m_classes.end (); ++it) | |
2940 | for (unsigned int i = 0; i < (*it)->classes.length (); i++) | |
2941 | { | |
2942 | congruence_class *c = (*it)->classes[i]; | |
2943 | ||
2944 | if (c->members.length () == 1) | |
2945 | continue; | |
2946 | ||
2947 | gcc_assert (c->members.length ()); | |
2948 | ||
2949 | sem_item *source = c->members[0]; | |
2950 | ||
2951 | for (unsigned int j = 1; j < c->members.length (); j++) | |
2952 | { | |
2953 | sem_item *alias = c->members[j]; | |
b84d4347 ML |
2954 | |
2955 | if (dump_file) | |
2956 | { | |
2957 | fprintf (dump_file, "Semantic equality hit:%s->%s\n", | |
2958 | source->name (), alias->name ()); | |
2959 | fprintf (dump_file, "Assembler symbol names:%s->%s\n", | |
2960 | source->asm_name (), alias->asm_name ()); | |
2961 | } | |
2962 | ||
185c9e56 ML |
2963 | if (lookup_attribute ("no_icf", DECL_ATTRIBUTES (alias->decl))) |
2964 | { | |
2965 | if (dump_file) | |
2966 | fprintf (dump_file, | |
2967 | "Merge operation is skipped due to no_icf " | |
2968 | "attribute.\n\n"); | |
2969 | ||
2970 | continue; | |
2971 | } | |
2972 | ||
b84d4347 ML |
2973 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2974 | { | |
2975 | source->dump_to_file (dump_file); | |
2976 | alias->dump_to_file (dump_file); | |
2977 | } | |
2978 | ||
21ce14d3 | 2979 | merged_p |= source->merge (alias); |
b84d4347 ML |
2980 | } |
2981 | } | |
bd31fe14 ML |
2982 | |
2983 | return merged_p; | |
b84d4347 ML |
2984 | } |
2985 | ||
2986 | /* Dump function prints all class members to a FILE with an INDENT. */ | |
2987 | ||
2988 | void | |
2989 | congruence_class::dump (FILE *file, unsigned int indent) const | |
2990 | { | |
2991 | FPRINTF_SPACES (file, indent, "class with id: %u, hash: %u, items: %u\n", | |
2992 | id, members[0]->get_hash (), members.length ()); | |
2993 | ||
2994 | FPUTS_SPACES (file, indent + 2, ""); | |
2995 | for (unsigned i = 0; i < members.length (); i++) | |
2996 | fprintf (file, "%s(%p/%u) ", members[i]->asm_name (), (void *) members[i]->decl, | |
2997 | members[i]->node->order); | |
2998 | ||
2999 | fprintf (file, "\n"); | |
3000 | } | |
3001 | ||
3002 | /* Returns true if there's a member that is used from another group. */ | |
3003 | ||
3004 | bool | |
3005 | congruence_class::is_class_used (void) | |
3006 | { | |
3007 | for (unsigned int i = 0; i < members.length (); i++) | |
3008 | if (members[i]->usages.length ()) | |
3009 | return true; | |
3010 | ||
3011 | return false; | |
3012 | } | |
3013 | ||
3014 | /* Initialization and computation of symtab node hash, there data | |
3015 | are propagated later on. */ | |
3016 | ||
3017 | static sem_item_optimizer *optimizer = NULL; | |
3018 | ||
3019 | /* Generate pass summary for IPA ICF pass. */ | |
3020 | ||
3021 | static void | |
3022 | ipa_icf_generate_summary (void) | |
3023 | { | |
3024 | if (!optimizer) | |
3025 | optimizer = new sem_item_optimizer (); | |
3026 | ||
e3e968e9 | 3027 | optimizer->register_hooks (); |
b84d4347 ML |
3028 | optimizer->parse_funcs_and_vars (); |
3029 | } | |
3030 | ||
3031 | /* Write pass summary for IPA ICF pass. */ | |
3032 | ||
3033 | static void | |
3034 | ipa_icf_write_summary (void) | |
3035 | { | |
3036 | gcc_assert (optimizer); | |
3037 | ||
3038 | optimizer->write_summary (); | |
3039 | } | |
3040 | ||
3041 | /* Read pass summary for IPA ICF pass. */ | |
3042 | ||
3043 | static void | |
3044 | ipa_icf_read_summary (void) | |
3045 | { | |
3046 | if (!optimizer) | |
3047 | optimizer = new sem_item_optimizer (); | |
3048 | ||
3049 | optimizer->read_summary (); | |
3050 | optimizer->register_hooks (); | |
3051 | } | |
3052 | ||
3053 | /* Semantic equality exection function. */ | |
3054 | ||
3055 | static unsigned int | |
3056 | ipa_icf_driver (void) | |
3057 | { | |
3058 | gcc_assert (optimizer); | |
3059 | ||
bd31fe14 | 3060 | bool merged_p = optimizer->execute (); |
b84d4347 ML |
3061 | |
3062 | delete optimizer; | |
9612a39a | 3063 | optimizer = NULL; |
b84d4347 | 3064 | |
bd31fe14 | 3065 | return merged_p ? TODO_remove_functions : 0; |
b84d4347 ML |
3066 | } |
3067 | ||
3068 | const pass_data pass_data_ipa_icf = | |
3069 | { | |
3070 | IPA_PASS, /* type */ | |
3071 | "icf", /* name */ | |
3072 | OPTGROUP_IPA, /* optinfo_flags */ | |
3073 | TV_IPA_ICF, /* tv_id */ | |
3074 | 0, /* properties_required */ | |
3075 | 0, /* properties_provided */ | |
3076 | 0, /* properties_destroyed */ | |
3077 | 0, /* todo_flags_start */ | |
3078 | 0, /* todo_flags_finish */ | |
3079 | }; | |
3080 | ||
3081 | class pass_ipa_icf : public ipa_opt_pass_d | |
3082 | { | |
3083 | public: | |
3084 | pass_ipa_icf (gcc::context *ctxt) | |
3085 | : ipa_opt_pass_d (pass_data_ipa_icf, ctxt, | |
3086 | ipa_icf_generate_summary, /* generate_summary */ | |
3087 | ipa_icf_write_summary, /* write_summary */ | |
3088 | ipa_icf_read_summary, /* read_summary */ | |
3089 | NULL, /* | |
3090 | write_optimization_summary */ | |
3091 | NULL, /* | |
3092 | read_optimization_summary */ | |
3093 | NULL, /* stmt_fixup */ | |
3094 | 0, /* function_transform_todo_flags_start */ | |
3095 | NULL, /* function_transform */ | |
3096 | NULL) /* variable_transform */ | |
3097 | {} | |
3098 | ||
3099 | /* opt_pass methods: */ | |
3100 | virtual bool gate (function *) | |
3101 | { | |
b16650ac | 3102 | return in_lto_p || flag_ipa_icf_variables || flag_ipa_icf_functions; |
b84d4347 ML |
3103 | } |
3104 | ||
3105 | virtual unsigned int execute (function *) | |
3106 | { | |
3107 | return ipa_icf_driver(); | |
3108 | } | |
3109 | }; // class pass_ipa_icf | |
3110 | ||
3111 | } // ipa_icf namespace | |
3112 | ||
3113 | ipa_opt_pass_d * | |
3114 | make_pass_ipa_icf (gcc::context *ctxt) | |
3115 | { | |
3116 | return new ipa_icf::pass_ipa_icf (ctxt); | |
3117 | } |