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