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