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