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