ipa-prop uses symbol_summary class.
[gcc.git] / gcc / ipa-icf.c
1 /* Interprocedural Identical Code Folding pass
2 Copyright (C) 2014 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 "tree.h"
58 #include "predict.h"
59 #include "vec.h"
60 #include "hashtab.h"
61 #include "hash-set.h"
62 #include "machmode.h"
63 #include "tm.h"
64 #include "hard-reg-set.h"
65 #include "input.h"
66 #include "function.h"
67 #include "dominance.h"
68 #include "cfg.h"
69 #include "basic-block.h"
70 #include "tree-ssa-alias.h"
71 #include "internal-fn.h"
72 #include "gimple-expr.h"
73 #include "is-a.h"
74 #include "gimple.h"
75 #include "expr.h"
76 #include "gimple-iterator.h"
77 #include "gimple-ssa.h"
78 #include "tree-cfg.h"
79 #include "tree-phinodes.h"
80 #include "stringpool.h"
81 #include "tree-ssanames.h"
82 #include "tree-dfa.h"
83 #include "tree-pass.h"
84 #include "gimple-pretty-print.h"
85 #include "hash-map.h"
86 #include "plugin-api.h"
87 #include "ipa-ref.h"
88 #include "cgraph.h"
89 #include "alloc-pool.h"
90 #include "symbol-summary.h"
91 #include "ipa-prop.h"
92 #include "ipa-inline.h"
93 #include "cfgloop.h"
94 #include "except.h"
95 #include "hash-table.h"
96 #include "coverage.h"
97 #include "attribs.h"
98 #include "print-tree.h"
99 #include "lto-streamer.h"
100 #include "data-streamer.h"
101 #include "ipa-utils.h"
102 #include <list>
103 #include "ipa-icf-gimple.h"
104 #include "ipa-icf.h"
105 #include "varasm.h"
106
107 using namespace ipa_icf_gimple;
108
109 namespace ipa_icf {
110 /* Constructor for key value pair, where _ITEM is key and _INDEX is a target. */
111
112 sem_usage_pair::sem_usage_pair (sem_item *_item, unsigned int _index):
113 item (_item), index (_index)
114 {
115 }
116
117 /* Semantic item constructor for a node of _TYPE, where STACK is used
118 for bitmap memory allocation. */
119
120 sem_item::sem_item (sem_item_type _type,
121 bitmap_obstack *stack): type(_type), hash(0)
122 {
123 setup (stack);
124 }
125
126 /* Semantic item constructor for a node of _TYPE, where STACK is used
127 for bitmap memory allocation. The item is based on symtab node _NODE
128 with computed _HASH. */
129
130 sem_item::sem_item (sem_item_type _type, symtab_node *_node,
131 hashval_t _hash, bitmap_obstack *stack): type(_type),
132 node (_node), hash (_hash)
133 {
134 decl = node->decl;
135 setup (stack);
136 }
137
138 /* Add reference to a semantic TARGET. */
139
140 void
141 sem_item::add_reference (sem_item *target)
142 {
143 refs.safe_push (target);
144 unsigned index = refs.length ();
145 target->usages.safe_push (new sem_usage_pair(this, index));
146 bitmap_set_bit (target->usage_index_bitmap, index);
147 refs_set.add (target->node);
148 }
149
150 /* Initialize internal data structures. Bitmap STACK is used for
151 bitmap memory allocation process. */
152
153 void
154 sem_item::setup (bitmap_obstack *stack)
155 {
156 gcc_checking_assert (node);
157
158 refs.create (0);
159 tree_refs.create (0);
160 usages.create (0);
161 usage_index_bitmap = BITMAP_ALLOC (stack);
162 }
163
164 sem_item::~sem_item ()
165 {
166 for (unsigned i = 0; i < usages.length (); i++)
167 delete usages[i];
168
169 refs.release ();
170 tree_refs.release ();
171 usages.release ();
172
173 BITMAP_FREE (usage_index_bitmap);
174 }
175
176 /* Dump function for debugging purpose. */
177
178 DEBUG_FUNCTION void
179 sem_item::dump (void)
180 {
181 if (dump_file)
182 {
183 fprintf (dump_file, "[%s] %s (%u) (tree:%p)\n", type == FUNC ? "func" : "var",
184 name(), node->order, (void *) node->decl);
185 fprintf (dump_file, " hash: %u\n", get_hash ());
186 fprintf (dump_file, " references: ");
187
188 for (unsigned i = 0; i < refs.length (); i++)
189 fprintf (dump_file, "%s%s ", refs[i]->name (),
190 i < refs.length() - 1 ? "," : "");
191
192 fprintf (dump_file, "\n");
193 }
194 }
195
196 /* Return true if target supports alias symbols. */
197
198 bool
199 sem_item::target_supports_symbol_aliases_p (void)
200 {
201 #if !defined (ASM_OUTPUT_DEF) || (!defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL))
202 return false;
203 #else
204 return true;
205 #endif
206 }
207
208 /* Semantic function constructor that uses STACK as bitmap memory stack. */
209
210 sem_function::sem_function (bitmap_obstack *stack): sem_item (FUNC, stack),
211 m_checker (NULL), m_compared_func (NULL)
212 {
213 arg_types.create (0);
214 bb_sizes.create (0);
215 bb_sorted.create (0);
216 }
217
218 /* Constructor based on callgraph node _NODE with computed hash _HASH.
219 Bitmap STACK is used for memory allocation. */
220 sem_function::sem_function (cgraph_node *node, hashval_t hash,
221 bitmap_obstack *stack):
222 sem_item (FUNC, node, hash, stack),
223 m_checker (NULL), m_compared_func (NULL)
224 {
225 arg_types.create (0);
226 bb_sizes.create (0);
227 bb_sorted.create (0);
228 }
229
230 sem_function::~sem_function ()
231 {
232 for (unsigned i = 0; i < bb_sorted.length (); i++)
233 delete (bb_sorted[i]);
234
235 arg_types.release ();
236 bb_sizes.release ();
237 bb_sorted.release ();
238 }
239
240 /* Calculates hash value based on a BASIC_BLOCK. */
241
242 hashval_t
243 sem_function::get_bb_hash (const sem_bb *basic_block)
244 {
245 inchash::hash hstate;
246
247 hstate.add_int (basic_block->nondbg_stmt_count);
248 hstate.add_int (basic_block->edge_count);
249
250 return hstate.end ();
251 }
252
253 /* References independent hash function. */
254
255 hashval_t
256 sem_function::get_hash (void)
257 {
258 if(!hash)
259 {
260 inchash::hash hstate;
261 hstate.add_int (177454); /* Random number for function type. */
262
263 hstate.add_int (arg_count);
264 hstate.add_int (cfg_checksum);
265 hstate.add_int (gcode_hash);
266
267 for (unsigned i = 0; i < bb_sorted.length (); i++)
268 hstate.merge_hash (get_bb_hash (bb_sorted[i]));
269
270 for (unsigned i = 0; i < bb_sizes.length (); i++)
271 hstate.add_int (bb_sizes[i]);
272
273 hash = hstate.end ();
274 }
275
276 return hash;
277 }
278
279 /* For a given symbol table nodes N1 and N2, we check that FUNCTION_DECLs
280 point to a same function. Comparison can be skipped if IGNORED_NODES
281 contains these nodes. */
282
283 bool
284 sem_function::compare_cgraph_references (hash_map <symtab_node *, sem_item *>
285 &ignored_nodes,
286 symtab_node *n1, symtab_node *n2)
287 {
288 if (n1 == n2 || (ignored_nodes.get (n1) && ignored_nodes.get (n2)))
289 return true;
290
291 /* TODO: add more precise comparison for weakrefs, etc. */
292
293 return return_false_with_msg ("different references");
294 }
295
296 /* If cgraph edges E1 and E2 are indirect calls, verify that
297 ECF flags are the same. */
298
299 bool sem_function::compare_edge_flags (cgraph_edge *e1, cgraph_edge *e2)
300 {
301 if (e1->indirect_info && e2->indirect_info)
302 {
303 int e1_flags = e1->indirect_info->ecf_flags;
304 int e2_flags = e2->indirect_info->ecf_flags;
305
306 if (e1_flags != e2_flags)
307 return return_false_with_msg ("ICF flags are different");
308 }
309 else if (e1->indirect_info || e2->indirect_info)
310 return false;
311
312 return true;
313 }
314
315 /* Fast equality function based on knowledge known in WPA. */
316
317 bool
318 sem_function::equals_wpa (sem_item *item,
319 hash_map <symtab_node *, sem_item *> &ignored_nodes)
320 {
321 gcc_assert (item->type == FUNC);
322
323 m_compared_func = static_cast<sem_function *> (item);
324
325 if (arg_types.length () != m_compared_func->arg_types.length ())
326 return return_false_with_msg ("different number of arguments");
327
328 /* Checking types of arguments. */
329 for (unsigned i = 0; i < arg_types.length (); i++)
330 {
331 /* This guard is here for function pointer with attributes (pr59927.c). */
332 if (!arg_types[i] || !m_compared_func->arg_types[i])
333 return return_false_with_msg ("NULL argument type");
334
335 /* Polymorphic comparison is executed just for non-leaf functions. */
336 bool is_not_leaf = get_node ()->callees != NULL;
337
338 if (!func_checker::compatible_types_p (arg_types[i],
339 m_compared_func->arg_types[i],
340 is_not_leaf, i == 0))
341 return return_false_with_msg ("argument type is different");
342 }
343
344 /* Result type checking. */
345 if (!func_checker::compatible_types_p (result_type,
346 m_compared_func->result_type))
347 return return_false_with_msg ("result types are different");
348
349 if (node->num_references () != item->node->num_references ())
350 return return_false_with_msg ("different number of references");
351
352 ipa_ref *ref = NULL, *ref2 = NULL;
353 for (unsigned i = 0; node->iterate_reference (i, ref); i++)
354 {
355 item->node->iterate_reference (i, ref2);
356
357 if (!compare_cgraph_references (ignored_nodes, ref->referred, ref2->referred))
358 return false;
359 }
360
361 cgraph_edge *e1 = dyn_cast <cgraph_node *> (node)->callees;
362 cgraph_edge *e2 = dyn_cast <cgraph_node *> (item->node)->callees;
363
364 while (e1 && e2)
365 {
366 if (!compare_cgraph_references (ignored_nodes, e1->callee, e2->callee))
367 return false;
368
369 e1 = e1->next_callee;
370 e2 = e2->next_callee;
371 }
372
373 if (e1 || e2)
374 return return_false_with_msg ("different number of edges");
375
376 return true;
377 }
378
379 /* Returns true if the item equals to ITEM given as argument. */
380
381 bool
382 sem_function::equals (sem_item *item,
383 hash_map <symtab_node *, sem_item *> &ignored_nodes)
384 {
385 gcc_assert (item->type == FUNC);
386 bool eq = equals_private (item, ignored_nodes);
387
388 if (m_checker != NULL)
389 {
390 delete m_checker;
391 m_checker = NULL;
392 }
393
394 if (dump_file && (dump_flags & TDF_DETAILS))
395 fprintf (dump_file,
396 "Equals called for:%s:%s (%u:%u) (%s:%s) with result: %s\n\n",
397 name(), item->name (), node->order, item->node->order, asm_name (),
398 item->asm_name (), eq ? "true" : "false");
399
400 return eq;
401 }
402
403 /* Processes function equality comparison. */
404
405 bool
406 sem_function::equals_private (sem_item *item,
407 hash_map <symtab_node *, sem_item *> &ignored_nodes)
408 {
409 if (item->type != FUNC)
410 return false;
411
412 basic_block bb1, bb2;
413 edge e1, e2;
414 edge_iterator ei1, ei2;
415 bool result = true;
416 tree arg1, arg2;
417
418 m_compared_func = static_cast<sem_function *> (item);
419
420 gcc_assert (decl != item->decl);
421
422 if (bb_sorted.length () != m_compared_func->bb_sorted.length ()
423 || edge_count != m_compared_func->edge_count
424 || cfg_checksum != m_compared_func->cfg_checksum)
425 return return_false ();
426
427 if (!equals_wpa (item, ignored_nodes))
428 return false;
429
430 /* Checking function arguments. */
431 tree decl1 = DECL_ATTRIBUTES (decl);
432 tree decl2 = DECL_ATTRIBUTES (m_compared_func->decl);
433
434 m_checker = new func_checker (decl, m_compared_func->decl,
435 compare_polymorphic_p (),
436 false,
437 &refs_set,
438 &m_compared_func->refs_set);
439 while (decl1)
440 {
441 if (decl2 == NULL)
442 return return_false ();
443
444 if (get_attribute_name (decl1) != get_attribute_name (decl2))
445 return return_false ();
446
447 tree attr_value1 = TREE_VALUE (decl1);
448 tree attr_value2 = TREE_VALUE (decl2);
449
450 if (attr_value1 && attr_value2)
451 {
452 bool ret = m_checker->compare_operand (TREE_VALUE (attr_value1),
453 TREE_VALUE (attr_value2));
454 if (!ret)
455 return return_false_with_msg ("attribute values are different");
456 }
457 else if (!attr_value1 && !attr_value2)
458 {}
459 else
460 return return_false ();
461
462 decl1 = TREE_CHAIN (decl1);
463 decl2 = TREE_CHAIN (decl2);
464 }
465
466 if (decl1 != decl2)
467 return return_false();
468
469
470 for (arg1 = DECL_ARGUMENTS (decl),
471 arg2 = DECL_ARGUMENTS (m_compared_func->decl);
472 arg1; arg1 = DECL_CHAIN (arg1), arg2 = DECL_CHAIN (arg2))
473 if (!m_checker->compare_decl (arg1, arg2))
474 return return_false ();
475
476 /* Fill-up label dictionary. */
477 for (unsigned i = 0; i < bb_sorted.length (); ++i)
478 {
479 m_checker->parse_labels (bb_sorted[i]);
480 m_checker->parse_labels (m_compared_func->bb_sorted[i]);
481 }
482
483 /* Checking all basic blocks. */
484 for (unsigned i = 0; i < bb_sorted.length (); ++i)
485 if(!m_checker->compare_bb (bb_sorted[i], m_compared_func->bb_sorted[i]))
486 return return_false();
487
488 dump_message ("All BBs are equal\n");
489
490 auto_vec <int> bb_dict;
491
492 /* Basic block edges check. */
493 for (unsigned i = 0; i < bb_sorted.length (); ++i)
494 {
495 bb1 = bb_sorted[i]->bb;
496 bb2 = m_compared_func->bb_sorted[i]->bb;
497
498 ei2 = ei_start (bb2->preds);
499
500 for (ei1 = ei_start (bb1->preds); ei_cond (ei1, &e1); ei_next (&ei1))
501 {
502 ei_cond (ei2, &e2);
503
504 if (e1->flags != e2->flags)
505 return return_false_with_msg ("flags comparison returns false");
506
507 if (!bb_dict_test (bb_dict, e1->src->index, e2->src->index))
508 return return_false_with_msg ("edge comparison returns false");
509
510 if (!bb_dict_test (bb_dict, e1->dest->index, e2->dest->index))
511 return return_false_with_msg ("BB comparison returns false");
512
513 if (!m_checker->compare_edge (e1, e2))
514 return return_false_with_msg ("edge comparison returns false");
515
516 ei_next (&ei2);
517 }
518 }
519
520 /* Basic block PHI nodes comparison. */
521 for (unsigned i = 0; i < bb_sorted.length (); i++)
522 if (!compare_phi_node (bb_sorted[i]->bb, m_compared_func->bb_sorted[i]->bb))
523 return return_false_with_msg ("PHI node comparison returns false");
524
525 return result;
526 }
527
528 /* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can
529 be applied. */
530 bool
531 sem_function::merge (sem_item *alias_item)
532 {
533 gcc_assert (alias_item->type == FUNC);
534
535 sem_function *alias_func = static_cast<sem_function *> (alias_item);
536
537 cgraph_node *original = get_node ();
538 cgraph_node *local_original = original;
539 cgraph_node *alias = alias_func->get_node ();
540 bool original_address_matters;
541 bool alias_address_matters;
542
543 bool create_thunk = false;
544 bool create_alias = false;
545 bool redirect_callers = false;
546 bool original_discardable = false;
547
548 /* Do not attempt to mix functions from different user sections;
549 we do not know what user intends with those. */
550 if (((DECL_SECTION_NAME (original->decl) && !original->implicit_section)
551 || (DECL_SECTION_NAME (alias->decl) && !alias->implicit_section))
552 && DECL_SECTION_NAME (original->decl) != DECL_SECTION_NAME (alias->decl))
553 {
554 if (dump_file)
555 fprintf (dump_file,
556 "Not unifying; original and alias are in different sections.\n\n");
557 return false;
558 }
559
560 /* See if original is in a section that can be discarded if the main
561 symbol is not used. */
562 if (DECL_EXTERNAL (original->decl))
563 original_discardable = true;
564 if (original->resolution == LDPR_PREEMPTED_REG
565 || original->resolution == LDPR_PREEMPTED_IR)
566 original_discardable = true;
567 if (original->can_be_discarded_p ())
568 original_discardable = true;
569
570 /* See if original and/or alias address can be compared for equality. */
571 original_address_matters
572 = (!DECL_VIRTUAL_P (original->decl)
573 && (original->externally_visible
574 || original->address_taken_from_non_vtable_p ()));
575 alias_address_matters
576 = (!DECL_VIRTUAL_P (alias->decl)
577 && (alias->externally_visible
578 || alias->address_taken_from_non_vtable_p ()));
579
580 /* If alias and original can be compared for address equality, we need
581 to create a thunk. Also we can not create extra aliases into discardable
582 section (or we risk link failures when section is discarded). */
583 if ((original_address_matters
584 && alias_address_matters)
585 || original_discardable)
586 {
587 create_thunk = !stdarg_p (TREE_TYPE (alias->decl));
588 create_alias = false;
589 /* When both alias and original are not overwritable, we can save
590 the extra thunk wrapper for direct calls. */
591 redirect_callers
592 = (!original_discardable
593 && alias->get_availability () > AVAIL_INTERPOSABLE
594 && original->get_availability () > AVAIL_INTERPOSABLE
595 && !alias->instrumented_version);
596 }
597 else
598 {
599 create_alias = true;
600 create_thunk = false;
601 redirect_callers = false;
602 }
603
604 if (create_alias && (DECL_COMDAT_GROUP (alias->decl)
605 || !sem_item::target_supports_symbol_aliases_p ()))
606 {
607 create_alias = false;
608 create_thunk = true;
609 }
610
611 /* We want thunk to always jump to the local function body
612 unless the body is comdat and may be optimized out. */
613 if ((create_thunk || redirect_callers)
614 && (!original_discardable
615 || (DECL_COMDAT_GROUP (original->decl)
616 && (DECL_COMDAT_GROUP (original->decl)
617 == DECL_COMDAT_GROUP (alias->decl)))))
618 local_original
619 = dyn_cast <cgraph_node *> (original->noninterposable_alias ());
620
621 if (!local_original)
622 {
623 if (dump_file)
624 fprintf (dump_file, "Noninterposable alias cannot be created.\n\n");
625
626 return false;
627 }
628
629 if (!decl_binds_to_current_def_p (alias->decl))
630 {
631 if (dump_file)
632 fprintf (dump_file, "Declaration does not bind to currect definition.\n\n");
633 return false;
634 }
635
636 if (redirect_callers)
637 {
638 /* If alias is non-overwritable then
639 all direct calls are safe to be redirected to the original. */
640 bool redirected = false;
641 while (alias->callers)
642 {
643 cgraph_edge *e = alias->callers;
644 e->redirect_callee (local_original);
645 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
646
647 if (e->call_stmt)
648 e->redirect_call_stmt_to_callee ();
649
650 pop_cfun ();
651 redirected = true;
652 }
653
654 alias->icf_merged = true;
655
656 /* The alias function is removed if symbol address
657 does not matter. */
658 if (!alias_address_matters)
659 alias->remove ();
660
661 if (dump_file && redirected)
662 fprintf (dump_file, "Callgraph local calls have been redirected.\n\n");
663 }
664 /* If the condtion above is not met, we are lucky and can turn the
665 function into real alias. */
666 else if (create_alias)
667 {
668 alias->icf_merged = true;
669
670 /* Remove the function's body. */
671 ipa_merge_profiles (original, alias);
672 alias->release_body (true);
673 alias->reset ();
674
675 /* Create the alias. */
676 cgraph_node::create_alias (alias_func->decl, decl);
677 alias->resolve_alias (original);
678
679 /* Workaround for PR63566 that forces equal calling convention
680 to be used. */
681 alias->local.local = false;
682 original->local.local = false;
683
684 if (dump_file)
685 fprintf (dump_file, "Callgraph alias has been created.\n\n");
686 }
687 else if (create_thunk)
688 {
689 if (DECL_COMDAT_GROUP (alias->decl))
690 {
691 if (dump_file)
692 fprintf (dump_file, "Callgraph thunk cannot be created because of COMDAT\n");
693
694 return 0;
695 }
696
697 alias->icf_merged = true;
698 ipa_merge_profiles (local_original, alias);
699 alias->create_wrapper (local_original);
700
701 if (dump_file)
702 fprintf (dump_file, "Callgraph thunk has been created.\n\n");
703 }
704 else if (dump_file)
705 fprintf (dump_file, "Callgraph merge operation cannot be performed.\n\n");
706
707 return true;
708 }
709
710 /* Semantic item initialization function. */
711
712 void
713 sem_function::init (void)
714 {
715 if (in_lto_p)
716 get_node ()->get_untransformed_body ();
717
718 tree fndecl = node->decl;
719 function *func = DECL_STRUCT_FUNCTION (fndecl);
720
721 gcc_assert (func);
722 gcc_assert (SSANAMES (func));
723
724 ssa_names_size = SSANAMES (func)->length ();
725 node = node;
726
727 decl = fndecl;
728 region_tree = func->eh->region_tree;
729
730 /* iterating all function arguments. */
731 arg_count = count_formal_params (fndecl);
732
733 edge_count = n_edges_for_fn (func);
734 cfg_checksum = coverage_compute_cfg_checksum (func);
735
736 inchash::hash hstate;
737
738 basic_block bb;
739 FOR_EACH_BB_FN (bb, func)
740 {
741 unsigned nondbg_stmt_count = 0;
742
743 edge e;
744 for (edge_iterator ei = ei_start (bb->preds); ei_cond (ei, &e); ei_next (&ei))
745 cfg_checksum = iterative_hash_host_wide_int (e->flags,
746 cfg_checksum);
747
748 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
749 gsi_next (&gsi))
750 {
751 gimple stmt = gsi_stmt (gsi);
752
753 if (gimple_code (stmt) != GIMPLE_DEBUG)
754 {
755 hash_stmt (&hstate, stmt);
756 nondbg_stmt_count++;
757 }
758 }
759
760 gcode_hash = hstate.end ();
761 bb_sizes.safe_push (nondbg_stmt_count);
762
763 /* Inserting basic block to hash table. */
764 sem_bb *semantic_bb = new sem_bb (bb, nondbg_stmt_count,
765 EDGE_COUNT (bb->preds) + EDGE_COUNT (bb->succs));
766
767 bb_sorted.safe_push (semantic_bb);
768 }
769
770 parse_tree_args ();
771 }
772
773 /* Improve accumulated hash for HSTATE based on a gimple statement STMT. */
774
775 void
776 sem_function::hash_stmt (inchash::hash *hstate, gimple stmt)
777 {
778 enum gimple_code code = gimple_code (stmt);
779
780 hstate->add_int (code);
781
782 if (code == GIMPLE_CALL)
783 {
784 /* Checking of argument. */
785 for (unsigned i = 0; i < gimple_call_num_args (stmt); ++i)
786 {
787 tree argument = gimple_call_arg (stmt, i);
788
789 switch (TREE_CODE (argument))
790 {
791 case INTEGER_CST:
792 if (tree_fits_shwi_p (argument))
793 hstate->add_wide_int (tree_to_shwi (argument));
794 else if (tree_fits_uhwi_p (argument))
795 hstate->add_wide_int (tree_to_uhwi (argument));
796 break;
797 case REAL_CST:
798 REAL_VALUE_TYPE c;
799 HOST_WIDE_INT n;
800
801 c = TREE_REAL_CST (argument);
802 n = real_to_integer (&c);
803
804 hstate->add_wide_int (n);
805 break;
806 case ADDR_EXPR:
807 {
808 tree addr_operand = TREE_OPERAND (argument, 0);
809
810 if (TREE_CODE (addr_operand) == STRING_CST)
811 hstate->add (TREE_STRING_POINTER (addr_operand),
812 TREE_STRING_LENGTH (addr_operand));
813 break;
814 }
815 default:
816 break;
817 }
818 }
819 }
820 }
821
822
823 /* Return true if polymorphic comparison must be processed. */
824
825 bool
826 sem_function::compare_polymorphic_p (void)
827 {
828 return get_node ()->callees != NULL
829 || m_compared_func->get_node ()->callees != NULL;
830 }
831
832 /* For a given call graph NODE, the function constructs new
833 semantic function item. */
834
835 sem_function *
836 sem_function::parse (cgraph_node *node, bitmap_obstack *stack)
837 {
838 tree fndecl = node->decl;
839 function *func = DECL_STRUCT_FUNCTION (fndecl);
840
841 /* TODO: add support for thunks and aliases. */
842
843 if (!func || !node->has_gimple_body_p ())
844 return NULL;
845
846 if (lookup_attribute_by_prefix ("omp ", DECL_ATTRIBUTES (node->decl)) != NULL)
847 return NULL;
848
849 sem_function *f = new sem_function (node, 0, stack);
850
851 f->init ();
852
853 return f;
854 }
855
856 /* Parses function arguments and result type. */
857
858 void
859 sem_function::parse_tree_args (void)
860 {
861 tree result;
862
863 if (arg_types.exists ())
864 arg_types.release ();
865
866 arg_types.create (4);
867 tree fnargs = DECL_ARGUMENTS (decl);
868
869 for (tree parm = fnargs; parm; parm = DECL_CHAIN (parm))
870 arg_types.safe_push (DECL_ARG_TYPE (parm));
871
872 /* Function result type. */
873 result = DECL_RESULT (decl);
874 result_type = result ? TREE_TYPE (result) : NULL;
875
876 /* During WPA, we can get arguments by following method. */
877 if (!fnargs)
878 {
879 tree type = TYPE_ARG_TYPES (TREE_TYPE (decl));
880 for (tree parm = type; parm; parm = TREE_CHAIN (parm))
881 arg_types.safe_push (TYPE_CANONICAL (TREE_VALUE (parm)));
882
883 result_type = TREE_TYPE (TREE_TYPE (decl));
884 }
885 }
886
887 /* For given basic blocks BB1 and BB2 (from functions FUNC1 and FUNC),
888 return true if phi nodes are semantically equivalent in these blocks . */
889
890 bool
891 sem_function::compare_phi_node (basic_block bb1, basic_block bb2)
892 {
893 gphi_iterator si1, si2;
894 gphi *phi1, *phi2;
895 unsigned size1, size2, i;
896 tree t1, t2;
897 edge e1, e2;
898
899 gcc_assert (bb1 != NULL);
900 gcc_assert (bb2 != NULL);
901
902 si2 = gsi_start_phis (bb2);
903 for (si1 = gsi_start_phis (bb1); !gsi_end_p (si1);
904 gsi_next (&si1))
905 {
906 gsi_next_nonvirtual_phi (&si1);
907 gsi_next_nonvirtual_phi (&si2);
908
909 if (gsi_end_p (si1) && gsi_end_p (si2))
910 break;
911
912 if (gsi_end_p (si1) || gsi_end_p (si2))
913 return return_false();
914
915 phi1 = si1.phi ();
916 phi2 = si2.phi ();
917
918 tree phi_result1 = gimple_phi_result (phi1);
919 tree phi_result2 = gimple_phi_result (phi2);
920
921 if (!m_checker->compare_operand (phi_result1, phi_result2))
922 return return_false_with_msg ("PHI results are different");
923
924 size1 = gimple_phi_num_args (phi1);
925 size2 = gimple_phi_num_args (phi2);
926
927 if (size1 != size2)
928 return return_false ();
929
930 for (i = 0; i < size1; ++i)
931 {
932 t1 = gimple_phi_arg (phi1, i)->def;
933 t2 = gimple_phi_arg (phi2, i)->def;
934
935 if (!m_checker->compare_operand (t1, t2))
936 return return_false ();
937
938 e1 = gimple_phi_arg_edge (phi1, i);
939 e2 = gimple_phi_arg_edge (phi2, i);
940
941 if (!m_checker->compare_edge (e1, e2))
942 return return_false ();
943 }
944
945 gsi_next (&si2);
946 }
947
948 return true;
949 }
950
951 /* Returns true if tree T can be compared as a handled component. */
952
953 bool
954 sem_function::icf_handled_component_p (tree t)
955 {
956 tree_code tc = TREE_CODE (t);
957
958 return ((handled_component_p (t))
959 || tc == ADDR_EXPR || tc == MEM_REF || tc == REALPART_EXPR
960 || tc == IMAGPART_EXPR || tc == OBJ_TYPE_REF);
961 }
962
963 /* Basic blocks dictionary BB_DICT returns true if SOURCE index BB
964 corresponds to TARGET. */
965
966 bool
967 sem_function::bb_dict_test (auto_vec<int> bb_dict, int source, int target)
968 {
969 source++;
970 target++;
971
972 if (bb_dict.length () <= (unsigned)source)
973 bb_dict.safe_grow_cleared (source + 1);
974
975 if (bb_dict[source] == 0)
976 {
977 bb_dict[source] = target;
978 return true;
979 }
980 else
981 return bb_dict[source] == target;
982 }
983
984 /* Iterates all tree types in T1 and T2 and returns true if all types
985 are compatible. If COMPARE_POLYMORPHIC is set to true,
986 more strict comparison is executed. */
987
988 bool
989 sem_function::compare_type_list (tree t1, tree t2, bool compare_polymorphic)
990 {
991 tree tv1, tv2;
992 tree_code tc1, tc2;
993
994 if (!t1 && !t2)
995 return true;
996
997 while (t1 != NULL && t2 != NULL)
998 {
999 tv1 = TREE_VALUE (t1);
1000 tv2 = TREE_VALUE (t2);
1001
1002 tc1 = TREE_CODE (tv1);
1003 tc2 = TREE_CODE (tv2);
1004
1005 if (tc1 == NOP_EXPR && tc2 == NOP_EXPR)
1006 {}
1007 else if (tc1 == NOP_EXPR || tc2 == NOP_EXPR)
1008 return false;
1009 else if (!func_checker::compatible_types_p (tv1, tv2, compare_polymorphic))
1010 return false;
1011
1012 t1 = TREE_CHAIN (t1);
1013 t2 = TREE_CHAIN (t2);
1014 }
1015
1016 return !(t1 || t2);
1017 }
1018
1019
1020 /* Semantic variable constructor that uses STACK as bitmap memory stack. */
1021
1022 sem_variable::sem_variable (bitmap_obstack *stack): sem_item (VAR, stack)
1023 {
1024 }
1025
1026 /* Constructor based on varpool node _NODE with computed hash _HASH.
1027 Bitmap STACK is used for memory allocation. */
1028
1029 sem_variable::sem_variable (varpool_node *node, hashval_t _hash,
1030 bitmap_obstack *stack): sem_item(VAR,
1031 node, _hash, stack)
1032 {
1033 gcc_checking_assert (node);
1034 gcc_checking_assert (get_node ());
1035 }
1036
1037 /* Returns true if the item equals to ITEM given as argument. */
1038
1039 bool
1040 sem_variable::equals (sem_item *item,
1041 hash_map <symtab_node *, sem_item *> & ARG_UNUSED (ignored_nodes))
1042 {
1043 gcc_assert (item->type == VAR);
1044
1045 sem_variable *v = static_cast<sem_variable *>(item);
1046
1047 if (!ctor || !v->ctor)
1048 return return_false_with_msg ("ctor is missing for semantic variable");
1049
1050 return sem_variable::equals (ctor, v->ctor);
1051 }
1052
1053 /* Compares trees T1 and T2 for semantic equality. */
1054
1055 bool
1056 sem_variable::equals (tree t1, tree t2)
1057 {
1058 tree_code tc1 = TREE_CODE (t1);
1059 tree_code tc2 = TREE_CODE (t2);
1060
1061 if (tc1 != tc2)
1062 return false;
1063
1064 switch (tc1)
1065 {
1066 case CONSTRUCTOR:
1067 {
1068 unsigned len1 = vec_safe_length (CONSTRUCTOR_ELTS (t1));
1069 unsigned len2 = vec_safe_length (CONSTRUCTOR_ELTS (t2));
1070
1071 if (len1 != len2)
1072 return false;
1073
1074 for (unsigned i = 0; i < len1; i++)
1075 if (!sem_variable::equals (CONSTRUCTOR_ELT (t1, i)->value,
1076 CONSTRUCTOR_ELT (t2, i)->value)
1077 || CONSTRUCTOR_ELT (t1, i)->index != CONSTRUCTOR_ELT (t2, i)->index)
1078 return false;
1079
1080 return true;
1081 }
1082 case MEM_REF:
1083 {
1084 tree x1 = TREE_OPERAND (t1, 0);
1085 tree x2 = TREE_OPERAND (t2, 0);
1086 tree y1 = TREE_OPERAND (t1, 1);
1087 tree y2 = TREE_OPERAND (t2, 1);
1088
1089 if (!func_checker::compatible_types_p (TREE_TYPE (x1), TREE_TYPE (x2),
1090 true))
1091 return return_false ();
1092
1093 /* Type of the offset on MEM_REF does not matter. */
1094 return sem_variable::equals (x1, x2)
1095 && wi::to_offset (y1) == wi::to_offset (y2);
1096 }
1097 case NOP_EXPR:
1098 case ADDR_EXPR:
1099 {
1100 tree op1 = TREE_OPERAND (t1, 0);
1101 tree op2 = TREE_OPERAND (t2, 0);
1102 return sem_variable::equals (op1, op2);
1103 }
1104 case FUNCTION_DECL:
1105 case VAR_DECL:
1106 case FIELD_DECL:
1107 case LABEL_DECL:
1108 return t1 == t2;
1109 case INTEGER_CST:
1110 return func_checker::compatible_types_p (TREE_TYPE (t1), TREE_TYPE (t2),
1111 true)
1112 && wi::to_offset (t1) == wi::to_offset (t2);
1113 case STRING_CST:
1114 case REAL_CST:
1115 case COMPLEX_CST:
1116 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
1117 case COMPONENT_REF:
1118 case ARRAY_REF:
1119 case POINTER_PLUS_EXPR:
1120 {
1121 tree x1 = TREE_OPERAND (t1, 0);
1122 tree x2 = TREE_OPERAND (t2, 0);
1123 tree y1 = TREE_OPERAND (t1, 1);
1124 tree y2 = TREE_OPERAND (t2, 1);
1125
1126 return sem_variable::equals (x1, x2) && sem_variable::equals (y1, y2);
1127 }
1128 case ERROR_MARK:
1129 return return_false_with_msg ("ERROR_MARK");
1130 default:
1131 return return_false_with_msg ("Unknown TREE code reached");
1132 }
1133 }
1134
1135 /* Parser function that visits a varpool NODE. */
1136
1137 sem_variable *
1138 sem_variable::parse (varpool_node *node, bitmap_obstack *stack)
1139 {
1140 tree decl = node->decl;
1141
1142 bool readonly = TYPE_P (decl) ? TYPE_READONLY (decl) : TREE_READONLY (decl);
1143 if (!readonly)
1144 return NULL;
1145
1146 bool can_handle = DECL_VIRTUAL_P (decl)
1147 || flag_merge_constants >= 2
1148 || (!TREE_ADDRESSABLE (decl) && !node->externally_visible);
1149
1150 if (!can_handle || DECL_EXTERNAL (decl))
1151 return NULL;
1152
1153 tree ctor = ctor_for_folding (decl);
1154 if (!ctor)
1155 return NULL;
1156
1157 sem_variable *v = new sem_variable (node, 0, stack);
1158
1159 v->init ();
1160
1161 return v;
1162 }
1163
1164 /* References independent hash function. */
1165
1166 hashval_t
1167 sem_variable::get_hash (void)
1168 {
1169 if (hash)
1170 return hash;
1171
1172 inchash::hash hstate;
1173
1174 hstate.add_int (456346417);
1175 hstate.add_int (TREE_CODE (ctor));
1176
1177 if (TREE_CODE (ctor) == CONSTRUCTOR)
1178 {
1179 unsigned length = vec_safe_length (CONSTRUCTOR_ELTS (ctor));
1180 hstate.add_int (length);
1181 }
1182
1183 hash = hstate.end ();
1184
1185 return hash;
1186 }
1187
1188 /* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can
1189 be applied. */
1190
1191 bool
1192 sem_variable::merge (sem_item *alias_item)
1193 {
1194 gcc_assert (alias_item->type == VAR);
1195
1196 if (!sem_item::target_supports_symbol_aliases_p ())
1197 {
1198 if (dump_file)
1199 fprintf (dump_file, "Symbol aliases are not supported by target\n\n");
1200 return false;
1201 }
1202
1203 sem_variable *alias_var = static_cast<sem_variable *> (alias_item);
1204
1205 varpool_node *original = get_node ();
1206 varpool_node *alias = alias_var->get_node ();
1207 bool original_discardable = false;
1208
1209 /* See if original is in a section that can be discarded if the main
1210 symbol is not used. */
1211 if (DECL_EXTERNAL (original->decl))
1212 original_discardable = true;
1213 if (original->resolution == LDPR_PREEMPTED_REG
1214 || original->resolution == LDPR_PREEMPTED_IR)
1215 original_discardable = true;
1216 if (original->can_be_discarded_p ())
1217 original_discardable = true;
1218
1219 gcc_assert (!TREE_ASM_WRITTEN (alias->decl));
1220
1221 if (original_discardable || DECL_EXTERNAL (alias_var->decl) ||
1222 !compare_sections (alias_var))
1223 {
1224 if (dump_file)
1225 fprintf (dump_file, "Varpool alias cannot be created\n\n");
1226
1227 return false;
1228 }
1229 else
1230 {
1231 // alias cycle creation check
1232 varpool_node *n = original;
1233
1234 while (n->alias)
1235 {
1236 n = n->get_alias_target ();
1237 if (n == alias)
1238 {
1239 if (dump_file)
1240 fprintf (dump_file, "Varpool alias cannot be created (alias cycle).\n\n");
1241
1242 return false;
1243 }
1244 }
1245
1246 alias->analyzed = false;
1247
1248 DECL_INITIAL (alias->decl) = NULL;
1249 alias->need_bounds_init = false;
1250 alias->remove_all_references ();
1251
1252 varpool_node::create_alias (alias_var->decl, decl);
1253 alias->resolve_alias (original);
1254
1255 if (dump_file)
1256 fprintf (dump_file, "Varpool alias has been created.\n\n");
1257
1258 return true;
1259 }
1260 }
1261
1262 bool
1263 sem_variable::compare_sections (sem_variable *alias)
1264 {
1265 const char *source = node->get_section ();
1266 const char *target = alias->node->get_section();
1267
1268 if (source == NULL && target == NULL)
1269 return true;
1270 else if(!source || !target)
1271 return false;
1272 else
1273 return strcmp (source, target) == 0;
1274 }
1275
1276 /* Dump symbol to FILE. */
1277
1278 void
1279 sem_variable::dump_to_file (FILE *file)
1280 {
1281 gcc_assert (file);
1282
1283 print_node (file, "", decl, 0);
1284 fprintf (file, "\n\n");
1285 }
1286
1287 /* Iterates though a constructor and identifies tree references
1288 we are interested in semantic function equality. */
1289
1290 void
1291 sem_variable::parse_tree_refs (tree t)
1292 {
1293 switch (TREE_CODE (t))
1294 {
1295 case CONSTRUCTOR:
1296 {
1297 unsigned length = vec_safe_length (CONSTRUCTOR_ELTS (t));
1298
1299 for (unsigned i = 0; i < length; i++)
1300 parse_tree_refs(CONSTRUCTOR_ELT (t, i)->value);
1301
1302 break;
1303 }
1304 case NOP_EXPR:
1305 case ADDR_EXPR:
1306 {
1307 tree op = TREE_OPERAND (t, 0);
1308 parse_tree_refs (op);
1309 break;
1310 }
1311 case FUNCTION_DECL:
1312 {
1313 tree_refs.safe_push (t);
1314 break;
1315 }
1316 default:
1317 break;
1318 }
1319 }
1320
1321 unsigned int sem_item_optimizer::class_id = 0;
1322
1323 sem_item_optimizer::sem_item_optimizer (): worklist (0), m_classes (0),
1324 m_classes_count (0), m_cgraph_node_hooks (NULL), m_varpool_node_hooks (NULL)
1325 {
1326 m_items.create (0);
1327 bitmap_obstack_initialize (&m_bmstack);
1328 }
1329
1330 sem_item_optimizer::~sem_item_optimizer ()
1331 {
1332 for (unsigned int i = 0; i < m_items.length (); i++)
1333 delete m_items[i];
1334
1335 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
1336 it != m_classes.end (); ++it)
1337 {
1338 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
1339 delete (*it)->classes[i];
1340
1341 (*it)->classes.release ();
1342 free (*it);
1343 }
1344
1345 m_items.release ();
1346
1347 bitmap_obstack_release (&m_bmstack);
1348 }
1349
1350 /* Write IPA ICF summary for symbols. */
1351
1352 void
1353 sem_item_optimizer::write_summary (void)
1354 {
1355 unsigned int count = 0;
1356
1357 output_block *ob = create_output_block (LTO_section_ipa_icf);
1358 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
1359 ob->symbol = NULL;
1360
1361 /* Calculate number of symbols to be serialized. */
1362 for (lto_symtab_encoder_iterator lsei = lsei_start_in_partition (encoder);
1363 !lsei_end_p (lsei);
1364 lsei_next_in_partition (&lsei))
1365 {
1366 symtab_node *node = lsei_node (lsei);
1367
1368 if (m_symtab_node_map.get (node))
1369 count++;
1370 }
1371
1372 streamer_write_uhwi (ob, count);
1373
1374 /* Process all of the symbols. */
1375 for (lto_symtab_encoder_iterator lsei = lsei_start_in_partition (encoder);
1376 !lsei_end_p (lsei);
1377 lsei_next_in_partition (&lsei))
1378 {
1379 symtab_node *node = lsei_node (lsei);
1380
1381 sem_item **item = m_symtab_node_map.get (node);
1382
1383 if (item && *item)
1384 {
1385 int node_ref = lto_symtab_encoder_encode (encoder, node);
1386 streamer_write_uhwi_stream (ob->main_stream, node_ref);
1387
1388 streamer_write_uhwi (ob, (*item)->get_hash ());
1389 }
1390 }
1391
1392 streamer_write_char_stream (ob->main_stream, 0);
1393 produce_asm (ob, NULL);
1394 destroy_output_block (ob);
1395 }
1396
1397 /* Reads a section from LTO stream file FILE_DATA. Input block for DATA
1398 contains LEN bytes. */
1399
1400 void
1401 sem_item_optimizer::read_section (lto_file_decl_data *file_data,
1402 const char *data, size_t len)
1403 {
1404 const lto_function_header *header =
1405 (const lto_function_header *) data;
1406 const int cfg_offset = sizeof (lto_function_header);
1407 const int main_offset = cfg_offset + header->cfg_size;
1408 const int string_offset = main_offset + header->main_size;
1409 data_in *data_in;
1410 unsigned int i;
1411 unsigned int count;
1412
1413 lto_input_block ib_main ((const char *) data + main_offset, 0,
1414 header->main_size);
1415
1416 data_in =
1417 lto_data_in_create (file_data, (const char *) data + string_offset,
1418 header->string_size, vNULL);
1419
1420 count = streamer_read_uhwi (&ib_main);
1421
1422 for (i = 0; i < count; i++)
1423 {
1424 unsigned int index;
1425 symtab_node *node;
1426 lto_symtab_encoder_t encoder;
1427
1428 index = streamer_read_uhwi (&ib_main);
1429 encoder = file_data->symtab_node_encoder;
1430 node = lto_symtab_encoder_deref (encoder, index);
1431
1432 hashval_t hash = streamer_read_uhwi (&ib_main);
1433
1434 gcc_assert (node->definition);
1435
1436 if (dump_file)
1437 fprintf (dump_file, "Symbol added:%s (tree: %p, uid:%u)\n", node->asm_name (),
1438 (void *) node->decl, node->order);
1439
1440 if (is_a<cgraph_node *> (node))
1441 {
1442 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1443
1444 m_items.safe_push (new sem_function (cnode, hash, &m_bmstack));
1445 }
1446 else
1447 {
1448 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1449
1450 m_items.safe_push (new sem_variable (vnode, hash, &m_bmstack));
1451 }
1452 }
1453
1454 lto_free_section_data (file_data, LTO_section_ipa_icf, NULL, data,
1455 len);
1456 lto_data_in_delete (data_in);
1457 }
1458
1459 /* Read IPA IPA ICF summary for symbols. */
1460
1461 void
1462 sem_item_optimizer::read_summary (void)
1463 {
1464 lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1465 lto_file_decl_data *file_data;
1466 unsigned int j = 0;
1467
1468 while ((file_data = file_data_vec[j++]))
1469 {
1470 size_t len;
1471 const char *data = lto_get_section_data (file_data,
1472 LTO_section_ipa_icf, NULL, &len);
1473
1474 if (data)
1475 read_section (file_data, data, len);
1476 }
1477 }
1478
1479 /* Register callgraph and varpool hooks. */
1480
1481 void
1482 sem_item_optimizer::register_hooks (void)
1483 {
1484 m_cgraph_node_hooks = symtab->add_cgraph_removal_hook
1485 (&sem_item_optimizer::cgraph_removal_hook, this);
1486
1487 m_varpool_node_hooks = symtab->add_varpool_removal_hook
1488 (&sem_item_optimizer::varpool_removal_hook, this);
1489 }
1490
1491 /* Unregister callgraph and varpool hooks. */
1492
1493 void
1494 sem_item_optimizer::unregister_hooks (void)
1495 {
1496 if (m_cgraph_node_hooks)
1497 symtab->remove_cgraph_removal_hook (m_cgraph_node_hooks);
1498
1499 if (m_varpool_node_hooks)
1500 symtab->remove_varpool_removal_hook (m_varpool_node_hooks);
1501 }
1502
1503 /* Adds a CLS to hashtable associated by hash value. */
1504
1505 void
1506 sem_item_optimizer::add_class (congruence_class *cls)
1507 {
1508 gcc_assert (cls->members.length ());
1509
1510 congruence_class_group *group = get_group_by_hash (
1511 cls->members[0]->get_hash (),
1512 cls->members[0]->type);
1513 group->classes.safe_push (cls);
1514 }
1515
1516 /* Gets a congruence class group based on given HASH value and TYPE. */
1517
1518 congruence_class_group *
1519 sem_item_optimizer::get_group_by_hash (hashval_t hash, sem_item_type type)
1520 {
1521 congruence_class_group *item = XNEW (congruence_class_group);
1522 item->hash = hash;
1523 item->type = type;
1524
1525 congruence_class_group **slot = m_classes.find_slot (item, INSERT);
1526
1527 if (*slot)
1528 free (item);
1529 else
1530 {
1531 item->classes.create (1);
1532 *slot = item;
1533 }
1534
1535 return *slot;
1536 }
1537
1538 /* Callgraph removal hook called for a NODE with a custom DATA. */
1539
1540 void
1541 sem_item_optimizer::cgraph_removal_hook (cgraph_node *node, void *data)
1542 {
1543 sem_item_optimizer *optimizer = (sem_item_optimizer *) data;
1544 optimizer->remove_symtab_node (node);
1545 }
1546
1547 /* Varpool removal hook called for a NODE with a custom DATA. */
1548
1549 void
1550 sem_item_optimizer::varpool_removal_hook (varpool_node *node, void *data)
1551 {
1552 sem_item_optimizer *optimizer = (sem_item_optimizer *) data;
1553 optimizer->remove_symtab_node (node);
1554 }
1555
1556 /* Remove symtab NODE triggered by symtab removal hooks. */
1557
1558 void
1559 sem_item_optimizer::remove_symtab_node (symtab_node *node)
1560 {
1561 gcc_assert (!m_classes.elements());
1562
1563 m_removed_items_set.add (node);
1564 }
1565
1566 void
1567 sem_item_optimizer::remove_item (sem_item *item)
1568 {
1569 if (m_symtab_node_map.get (item->node))
1570 m_symtab_node_map.remove (item->node);
1571 delete item;
1572 }
1573
1574 /* Removes all callgraph and varpool nodes that are marked by symtab
1575 as deleted. */
1576
1577 void
1578 sem_item_optimizer::filter_removed_items (void)
1579 {
1580 auto_vec <sem_item *> filtered;
1581
1582 for (unsigned int i = 0; i < m_items.length(); i++)
1583 {
1584 sem_item *item = m_items[i];
1585
1586 if (!flag_ipa_icf_functions && item->type == FUNC)
1587 {
1588 remove_item (item);
1589 continue;
1590 }
1591
1592 if (!flag_ipa_icf_variables && item->type == VAR)
1593 {
1594 remove_item (item);
1595 continue;
1596 }
1597
1598 bool no_body_function = false;
1599
1600 if (item->type == FUNC)
1601 {
1602 cgraph_node *cnode = static_cast <sem_function *>(item)->get_node ();
1603
1604 no_body_function = in_lto_p && (cnode->alias || cnode->body_removed);
1605 }
1606
1607 if(!m_removed_items_set.contains (m_items[i]->node)
1608 && !no_body_function)
1609 {
1610 if (item->type == VAR || (!DECL_CXX_CONSTRUCTOR_P (item->decl)
1611 && !DECL_CXX_DESTRUCTOR_P (item->decl)))
1612 {
1613 filtered.safe_push (m_items[i]);
1614 continue;
1615 }
1616 }
1617
1618 remove_item (item);
1619 }
1620
1621 /* Clean-up of released semantic items. */
1622
1623 m_items.release ();
1624 for (unsigned int i = 0; i < filtered.length(); i++)
1625 m_items.safe_push (filtered[i]);
1626 }
1627
1628 /* Optimizer entry point. */
1629
1630 void
1631 sem_item_optimizer::execute (void)
1632 {
1633 filter_removed_items ();
1634 build_hash_based_classes ();
1635
1636 if (dump_file)
1637 fprintf (dump_file, "Dump after hash based groups\n");
1638 dump_cong_classes ();
1639
1640 for (unsigned int i = 0; i < m_items.length(); i++)
1641 m_items[i]->init_wpa ();
1642
1643 build_graph ();
1644
1645 subdivide_classes_by_equality (true);
1646
1647 if (dump_file)
1648 fprintf (dump_file, "Dump after WPA based types groups\n");
1649
1650 dump_cong_classes ();
1651
1652 process_cong_reduction ();
1653 verify_classes ();
1654
1655 if (dump_file)
1656 fprintf (dump_file, "Dump after callgraph-based congruence reduction\n");
1657
1658 dump_cong_classes ();
1659
1660 parse_nonsingleton_classes ();
1661 subdivide_classes_by_equality ();
1662
1663 if (dump_file)
1664 fprintf (dump_file, "Dump after full equality comparison of groups\n");
1665
1666 dump_cong_classes ();
1667
1668 unsigned int prev_class_count = m_classes_count;
1669
1670 process_cong_reduction ();
1671 dump_cong_classes ();
1672 verify_classes ();
1673 merge_classes (prev_class_count);
1674
1675 if (dump_file && (dump_flags & TDF_DETAILS))
1676 symtab_node::dump_table (dump_file);
1677 }
1678
1679 /* Function responsible for visiting all potential functions and
1680 read-only variables that can be merged. */
1681
1682 void
1683 sem_item_optimizer::parse_funcs_and_vars (void)
1684 {
1685 cgraph_node *cnode;
1686
1687 if (flag_ipa_icf_functions)
1688 FOR_EACH_DEFINED_FUNCTION (cnode)
1689 {
1690 sem_function *f = sem_function::parse (cnode, &m_bmstack);
1691 if (f)
1692 {
1693 m_items.safe_push (f);
1694 m_symtab_node_map.put (cnode, f);
1695
1696 if (dump_file)
1697 fprintf (dump_file, "Parsed function:%s\n", f->asm_name ());
1698
1699 if (dump_file && (dump_flags & TDF_DETAILS))
1700 f->dump_to_file (dump_file);
1701 }
1702 else if (dump_file)
1703 fprintf (dump_file, "Not parsed function:%s\n", cnode->asm_name ());
1704 }
1705
1706 varpool_node *vnode;
1707
1708 if (flag_ipa_icf_variables)
1709 FOR_EACH_DEFINED_VARIABLE (vnode)
1710 {
1711 sem_variable *v = sem_variable::parse (vnode, &m_bmstack);
1712
1713 if (v)
1714 {
1715 m_items.safe_push (v);
1716 m_symtab_node_map.put (vnode, v);
1717 }
1718 }
1719 }
1720
1721 /* Makes pairing between a congruence class CLS and semantic ITEM. */
1722
1723 void
1724 sem_item_optimizer::add_item_to_class (congruence_class *cls, sem_item *item)
1725 {
1726 item->index_in_class = cls->members.length ();
1727 cls->members.safe_push (item);
1728 item->cls = cls;
1729 }
1730
1731 /* Congruence classes are built by hash value. */
1732
1733 void
1734 sem_item_optimizer::build_hash_based_classes (void)
1735 {
1736 for (unsigned i = 0; i < m_items.length (); i++)
1737 {
1738 sem_item *item = m_items[i];
1739
1740 congruence_class_group *group = get_group_by_hash (item->get_hash (),
1741 item->type);
1742
1743 if (!group->classes.length ())
1744 {
1745 m_classes_count++;
1746 group->classes.safe_push (new congruence_class (class_id++));
1747 }
1748
1749 add_item_to_class (group->classes[0], item);
1750 }
1751 }
1752
1753 /* Build references according to call graph. */
1754
1755 void
1756 sem_item_optimizer::build_graph (void)
1757 {
1758 for (unsigned i = 0; i < m_items.length (); i++)
1759 {
1760 sem_item *item = m_items[i];
1761 m_symtab_node_map.put (item->node, item);
1762 }
1763
1764 for (unsigned i = 0; i < m_items.length (); i++)
1765 {
1766 sem_item *item = m_items[i];
1767
1768 if (item->type == FUNC)
1769 {
1770 cgraph_node *cnode = dyn_cast <cgraph_node *> (item->node);
1771
1772 cgraph_edge *e = cnode->callees;
1773 while (e)
1774 {
1775 sem_item **slot = m_symtab_node_map.get (e->callee);
1776 if (slot)
1777 item->add_reference (*slot);
1778
1779 e = e->next_callee;
1780 }
1781 }
1782
1783 ipa_ref *ref = NULL;
1784 for (unsigned i = 0; item->node->iterate_reference (i, ref); i++)
1785 {
1786 sem_item **slot = m_symtab_node_map.get (ref->referred);
1787 if (slot)
1788 item->add_reference (*slot);
1789 }
1790 }
1791 }
1792
1793 /* Semantic items in classes having more than one element and initialized.
1794 In case of WPA, we load function body. */
1795
1796 void
1797 sem_item_optimizer::parse_nonsingleton_classes (void)
1798 {
1799 unsigned int init_called_count = 0;
1800
1801 for (unsigned i = 0; i < m_items.length (); i++)
1802 if (m_items[i]->cls->members.length () > 1)
1803 {
1804 m_items[i]->init ();
1805 init_called_count++;
1806 }
1807
1808 if (dump_file)
1809 fprintf (dump_file, "Init called for %u items (%.2f%%).\n", init_called_count,
1810 m_items.length () ? 100.0f * init_called_count / m_items.length (): 0.0f);
1811 }
1812
1813 /* Equality function for semantic items is used to subdivide existing
1814 classes. If IN_WPA, fast equality function is invoked. */
1815
1816 void
1817 sem_item_optimizer::subdivide_classes_by_equality (bool in_wpa)
1818 {
1819 for (hash_table <congruence_class_group_hash>::iterator it = m_classes.begin ();
1820 it != m_classes.end (); ++it)
1821 {
1822 unsigned int class_count = (*it)->classes.length ();
1823
1824 for (unsigned i = 0; i < class_count; i++)
1825 {
1826 congruence_class *c = (*it)->classes [i];
1827
1828 if (c->members.length() > 1)
1829 {
1830 auto_vec <sem_item *> new_vector;
1831
1832 sem_item *first = c->members[0];
1833 new_vector.safe_push (first);
1834
1835 unsigned class_split_first = (*it)->classes.length ();
1836
1837 for (unsigned j = 1; j < c->members.length (); j++)
1838 {
1839 sem_item *item = c->members[j];
1840
1841 bool equals = in_wpa ? first->equals_wpa (item,
1842 m_symtab_node_map) : first->equals (item, m_symtab_node_map);
1843
1844 if (equals)
1845 new_vector.safe_push (item);
1846 else
1847 {
1848 bool integrated = false;
1849
1850 for (unsigned k = class_split_first; k < (*it)->classes.length (); k++)
1851 {
1852 sem_item *x = (*it)->classes[k]->members[0];
1853 bool equals = in_wpa ? x->equals_wpa (item,
1854 m_symtab_node_map) : x->equals (item, m_symtab_node_map);
1855
1856 if (equals)
1857 {
1858 integrated = true;
1859 add_item_to_class ((*it)->classes[k], item);
1860
1861 break;
1862 }
1863 }
1864
1865 if (!integrated)
1866 {
1867 congruence_class *c = new congruence_class (class_id++);
1868 m_classes_count++;
1869 add_item_to_class (c, item);
1870
1871 (*it)->classes.safe_push (c);
1872 }
1873 }
1874 }
1875
1876 // we replace newly created new_vector for the class we've just splitted
1877 c->members.release ();
1878 c->members.create (new_vector.length ());
1879
1880 for (unsigned int j = 0; j < new_vector.length (); j++)
1881 add_item_to_class (c, new_vector[j]);
1882 }
1883 }
1884 }
1885
1886 verify_classes ();
1887 }
1888
1889 /* Verify congruence classes if checking is enabled. */
1890
1891 void
1892 sem_item_optimizer::verify_classes (void)
1893 {
1894 #if ENABLE_CHECKING
1895 for (hash_table <congruence_class_group_hash>::iterator it = m_classes.begin ();
1896 it != m_classes.end (); ++it)
1897 {
1898 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
1899 {
1900 congruence_class *cls = (*it)->classes[i];
1901
1902 gcc_checking_assert (cls);
1903 gcc_checking_assert (cls->members.length () > 0);
1904
1905 for (unsigned int j = 0; j < cls->members.length (); j++)
1906 {
1907 sem_item *item = cls->members[j];
1908
1909 gcc_checking_assert (item);
1910 gcc_checking_assert (item->cls == cls);
1911
1912 for (unsigned k = 0; k < item->usages.length (); k++)
1913 {
1914 sem_usage_pair *usage = item->usages[k];
1915 gcc_checking_assert (usage->item->index_in_class <
1916 usage->item->cls->members.length ());
1917 }
1918 }
1919 }
1920 }
1921 #endif
1922 }
1923
1924 /* Disposes split map traverse function. CLS_PTR is pointer to congruence
1925 class, BSLOT is bitmap slot we want to release. DATA is mandatory,
1926 but unused argument. */
1927
1928 bool
1929 sem_item_optimizer::release_split_map (congruence_class * const &,
1930 bitmap const &b, traverse_split_pair *)
1931 {
1932 bitmap bmp = b;
1933
1934 BITMAP_FREE (bmp);
1935
1936 return true;
1937 }
1938
1939 /* Process split operation for a class given as pointer CLS_PTR,
1940 where bitmap B splits congruence class members. DATA is used
1941 as argument of split pair. */
1942
1943 bool
1944 sem_item_optimizer::traverse_congruence_split (congruence_class * const &cls,
1945 bitmap const &b, traverse_split_pair *pair)
1946 {
1947 sem_item_optimizer *optimizer = pair->optimizer;
1948 const congruence_class *splitter_cls = pair->cls;
1949
1950 /* If counted bits are greater than zero and less than the number of members
1951 a group will be splitted. */
1952 unsigned popcount = bitmap_count_bits (b);
1953
1954 if (popcount > 0 && popcount < cls->members.length ())
1955 {
1956 congruence_class* newclasses[2] = { new congruence_class (class_id++), new congruence_class (class_id++) };
1957
1958 for (unsigned int i = 0; i < cls->members.length (); i++)
1959 {
1960 int target = bitmap_bit_p (b, i);
1961 congruence_class *tc = newclasses[target];
1962
1963 add_item_to_class (tc, cls->members[i]);
1964 }
1965
1966 #ifdef ENABLE_CHECKING
1967 for (unsigned int i = 0; i < 2; i++)
1968 gcc_checking_assert (newclasses[i]->members.length ());
1969 #endif
1970
1971 if (splitter_cls == cls)
1972 optimizer->splitter_class_removed = true;
1973
1974 /* Remove old class from worklist if presented. */
1975 bool in_worklist = cls->in_worklist;
1976
1977 if (in_worklist)
1978 cls->in_worklist = false;
1979
1980 congruence_class_group g;
1981 g.hash = cls->members[0]->get_hash ();
1982 g.type = cls->members[0]->type;
1983
1984 congruence_class_group *slot = optimizer->m_classes.find(&g);
1985
1986 for (unsigned int i = 0; i < slot->classes.length (); i++)
1987 if (slot->classes[i] == cls)
1988 {
1989 slot->classes.ordered_remove (i);
1990 break;
1991 }
1992
1993 /* New class will be inserted and integrated to work list. */
1994 for (unsigned int i = 0; i < 2; i++)
1995 optimizer->add_class (newclasses[i]);
1996
1997 /* Two classes replace one, so that increment just by one. */
1998 optimizer->m_classes_count++;
1999
2000 /* If OLD class was presented in the worklist, we remove the class
2001 and replace it will both newly created classes. */
2002 if (in_worklist)
2003 for (unsigned int i = 0; i < 2; i++)
2004 optimizer->worklist_push (newclasses[i]);
2005 else /* Just smaller class is inserted. */
2006 {
2007 unsigned int smaller_index = newclasses[0]->members.length () <
2008 newclasses[1]->members.length () ?
2009 0 : 1;
2010 optimizer->worklist_push (newclasses[smaller_index]);
2011 }
2012
2013 if (dump_file && (dump_flags & TDF_DETAILS))
2014 {
2015 fprintf (dump_file, " congruence class splitted:\n");
2016 cls->dump (dump_file, 4);
2017
2018 fprintf (dump_file, " newly created groups:\n");
2019 for (unsigned int i = 0; i < 2; i++)
2020 newclasses[i]->dump (dump_file, 4);
2021 }
2022
2023 /* Release class if not presented in work list. */
2024 if (!in_worklist)
2025 delete cls;
2026 }
2027
2028
2029 return true;
2030 }
2031
2032 /* Tests if a class CLS used as INDEXth splits any congruence classes.
2033 Bitmap stack BMSTACK is used for bitmap allocation. */
2034
2035 void
2036 sem_item_optimizer::do_congruence_step_for_index (congruence_class *cls,
2037 unsigned int index)
2038 {
2039 hash_map <congruence_class *, bitmap> split_map;
2040
2041 for (unsigned int i = 0; i < cls->members.length (); i++)
2042 {
2043 sem_item *item = cls->members[i];
2044
2045 /* Iterate all usages that have INDEX as usage of the item. */
2046 for (unsigned int j = 0; j < item->usages.length (); j++)
2047 {
2048 sem_usage_pair *usage = item->usages[j];
2049
2050 if (usage->index != index)
2051 continue;
2052
2053 bitmap *slot = split_map.get (usage->item->cls);
2054 bitmap b;
2055
2056 if(!slot)
2057 {
2058 b = BITMAP_ALLOC (&m_bmstack);
2059 split_map.put (usage->item->cls, b);
2060 }
2061 else
2062 b = *slot;
2063
2064 #if ENABLE_CHECKING
2065 gcc_checking_assert (usage->item->cls);
2066 gcc_checking_assert (usage->item->index_in_class <
2067 usage->item->cls->members.length ());
2068 #endif
2069
2070 bitmap_set_bit (b, usage->item->index_in_class);
2071 }
2072 }
2073
2074 traverse_split_pair pair;
2075 pair.optimizer = this;
2076 pair.cls = cls;
2077
2078 splitter_class_removed = false;
2079 split_map.traverse
2080 <traverse_split_pair *, sem_item_optimizer::traverse_congruence_split> (&pair);
2081
2082 /* Bitmap clean-up. */
2083 split_map.traverse
2084 <traverse_split_pair *, sem_item_optimizer::release_split_map> (NULL);
2085 }
2086
2087 /* Every usage of a congruence class CLS is a candidate that can split the
2088 collection of classes. Bitmap stack BMSTACK is used for bitmap
2089 allocation. */
2090
2091 void
2092 sem_item_optimizer::do_congruence_step (congruence_class *cls)
2093 {
2094 bitmap_iterator bi;
2095 unsigned int i;
2096
2097 bitmap usage = BITMAP_ALLOC (&m_bmstack);
2098
2099 for (unsigned int i = 0; i < cls->members.length (); i++)
2100 bitmap_ior_into (usage, cls->members[i]->usage_index_bitmap);
2101
2102 EXECUTE_IF_SET_IN_BITMAP (usage, 0, i, bi)
2103 {
2104 if (dump_file && (dump_flags & TDF_DETAILS))
2105 fprintf (dump_file, " processing congruece step for class: %u, index: %u\n",
2106 cls->id, i);
2107
2108 do_congruence_step_for_index (cls, i);
2109
2110 if (splitter_class_removed)
2111 break;
2112 }
2113
2114 BITMAP_FREE (usage);
2115 }
2116
2117 /* Adds a newly created congruence class CLS to worklist. */
2118
2119 void
2120 sem_item_optimizer::worklist_push (congruence_class *cls)
2121 {
2122 /* Return if the class CLS is already presented in work list. */
2123 if (cls->in_worklist)
2124 return;
2125
2126 cls->in_worklist = true;
2127 worklist.push_back (cls);
2128 }
2129
2130 /* Pops a class from worklist. */
2131
2132 congruence_class *
2133 sem_item_optimizer::worklist_pop (void)
2134 {
2135 congruence_class *cls;
2136
2137 while (!worklist.empty ())
2138 {
2139 cls = worklist.front ();
2140 worklist.pop_front ();
2141 if (cls->in_worklist)
2142 {
2143 cls->in_worklist = false;
2144
2145 return cls;
2146 }
2147 else
2148 {
2149 /* Work list item was already intended to be removed.
2150 The only reason for doing it is to split a class.
2151 Thus, the class CLS is deleted. */
2152 delete cls;
2153 }
2154 }
2155
2156 return NULL;
2157 }
2158
2159 /* Iterative congruence reduction function. */
2160
2161 void
2162 sem_item_optimizer::process_cong_reduction (void)
2163 {
2164 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
2165 it != m_classes.end (); ++it)
2166 for (unsigned i = 0; i < (*it)->classes.length (); i++)
2167 if ((*it)->classes[i]->is_class_used ())
2168 worklist_push ((*it)->classes[i]);
2169
2170 if (dump_file)
2171 fprintf (dump_file, "Worklist has been filled with: %lu\n",
2172 (unsigned long) worklist.size ());
2173
2174 if (dump_file && (dump_flags & TDF_DETAILS))
2175 fprintf (dump_file, "Congruence class reduction\n");
2176
2177 congruence_class *cls;
2178 while ((cls = worklist_pop ()) != NULL)
2179 do_congruence_step (cls);
2180 }
2181
2182 /* Debug function prints all informations about congruence classes. */
2183
2184 void
2185 sem_item_optimizer::dump_cong_classes (void)
2186 {
2187 if (!dump_file)
2188 return;
2189
2190 fprintf (dump_file,
2191 "Congruence classes: %u (unique hash values: %lu), with total: %u items\n",
2192 m_classes_count, (unsigned long) m_classes.elements(), m_items.length ());
2193
2194 /* Histogram calculation. */
2195 unsigned int max_index = 0;
2196 unsigned int* histogram = XCNEWVEC (unsigned int, m_items.length () + 1);
2197
2198 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
2199 it != m_classes.end (); ++it)
2200
2201 for (unsigned i = 0; i < (*it)->classes.length (); i++)
2202 {
2203 unsigned int c = (*it)->classes[i]->members.length ();
2204 histogram[c]++;
2205
2206 if (c > max_index)
2207 max_index = c;
2208 }
2209
2210 fprintf (dump_file,
2211 "Class size histogram [num of members]: number of classe number of classess\n");
2212
2213 for (unsigned int i = 0; i <= max_index; i++)
2214 if (histogram[i])
2215 fprintf (dump_file, "[%u]: %u classes\n", i, histogram[i]);
2216
2217 fprintf (dump_file, "\n\n");
2218
2219
2220 if (dump_flags & TDF_DETAILS)
2221 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
2222 it != m_classes.end (); ++it)
2223 {
2224 fprintf (dump_file, " group: with %u classes:\n", (*it)->classes.length ());
2225
2226 for (unsigned i = 0; i < (*it)->classes.length (); i++)
2227 {
2228 (*it)->classes[i]->dump (dump_file, 4);
2229
2230 if(i < (*it)->classes.length () - 1)
2231 fprintf (dump_file, " ");
2232 }
2233 }
2234
2235 free (histogram);
2236 }
2237
2238 /* After reduction is done, we can declare all items in a group
2239 to be equal. PREV_CLASS_COUNT is start number of classes
2240 before reduction. */
2241
2242 void
2243 sem_item_optimizer::merge_classes (unsigned int prev_class_count)
2244 {
2245 unsigned int item_count = m_items.length ();
2246 unsigned int class_count = m_classes_count;
2247 unsigned int equal_items = item_count - class_count;
2248
2249 unsigned int non_singular_classes_count = 0;
2250 unsigned int non_singular_classes_sum = 0;
2251
2252 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
2253 it != m_classes.end (); ++it)
2254 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
2255 {
2256 congruence_class *c = (*it)->classes[i];
2257 if (c->members.length () > 1)
2258 {
2259 non_singular_classes_count++;
2260 non_singular_classes_sum += c->members.length ();
2261 }
2262 }
2263
2264 if (dump_file)
2265 {
2266 fprintf (dump_file, "\nItem count: %u\n", item_count);
2267 fprintf (dump_file, "Congruent classes before: %u, after: %u\n",
2268 prev_class_count, class_count);
2269 fprintf (dump_file, "Average class size before: %.2f, after: %.2f\n",
2270 prev_class_count ? 1.0f * item_count / prev_class_count : 0.0f,
2271 class_count ? 1.0f * item_count / class_count : 0.0f);
2272 fprintf (dump_file, "Average non-singular class size: %.2f, count: %u\n",
2273 non_singular_classes_count ? 1.0f * non_singular_classes_sum /
2274 non_singular_classes_count : 0.0f,
2275 non_singular_classes_count);
2276 fprintf (dump_file, "Equal symbols: %u\n", equal_items);
2277 fprintf (dump_file, "Fraction of visited symbols: %.2f%%\n\n",
2278 item_count ? 100.0f * equal_items / item_count : 0.0f);
2279 }
2280
2281 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
2282 it != m_classes.end (); ++it)
2283 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
2284 {
2285 congruence_class *c = (*it)->classes[i];
2286
2287 if (c->members.length () == 1)
2288 continue;
2289
2290 gcc_assert (c->members.length ());
2291
2292 sem_item *source = c->members[0];
2293
2294 for (unsigned int j = 1; j < c->members.length (); j++)
2295 {
2296 sem_item *alias = c->members[j];
2297 source->equals (alias, m_symtab_node_map);
2298
2299 if (dump_file)
2300 {
2301 fprintf (dump_file, "Semantic equality hit:%s->%s\n",
2302 source->name (), alias->name ());
2303 fprintf (dump_file, "Assembler symbol names:%s->%s\n",
2304 source->asm_name (), alias->asm_name ());
2305 }
2306
2307 if (dump_file && (dump_flags & TDF_DETAILS))
2308 {
2309 source->dump_to_file (dump_file);
2310 alias->dump_to_file (dump_file);
2311 }
2312
2313 source->merge (alias);
2314 }
2315 }
2316 }
2317
2318 /* Dump function prints all class members to a FILE with an INDENT. */
2319
2320 void
2321 congruence_class::dump (FILE *file, unsigned int indent) const
2322 {
2323 FPRINTF_SPACES (file, indent, "class with id: %u, hash: %u, items: %u\n",
2324 id, members[0]->get_hash (), members.length ());
2325
2326 FPUTS_SPACES (file, indent + 2, "");
2327 for (unsigned i = 0; i < members.length (); i++)
2328 fprintf (file, "%s(%p/%u) ", members[i]->asm_name (), (void *) members[i]->decl,
2329 members[i]->node->order);
2330
2331 fprintf (file, "\n");
2332 }
2333
2334 /* Returns true if there's a member that is used from another group. */
2335
2336 bool
2337 congruence_class::is_class_used (void)
2338 {
2339 for (unsigned int i = 0; i < members.length (); i++)
2340 if (members[i]->usages.length ())
2341 return true;
2342
2343 return false;
2344 }
2345
2346 /* Initialization and computation of symtab node hash, there data
2347 are propagated later on. */
2348
2349 static sem_item_optimizer *optimizer = NULL;
2350
2351 /* Generate pass summary for IPA ICF pass. */
2352
2353 static void
2354 ipa_icf_generate_summary (void)
2355 {
2356 if (!optimizer)
2357 optimizer = new sem_item_optimizer ();
2358
2359 optimizer->parse_funcs_and_vars ();
2360 }
2361
2362 /* Write pass summary for IPA ICF pass. */
2363
2364 static void
2365 ipa_icf_write_summary (void)
2366 {
2367 gcc_assert (optimizer);
2368
2369 optimizer->write_summary ();
2370 }
2371
2372 /* Read pass summary for IPA ICF pass. */
2373
2374 static void
2375 ipa_icf_read_summary (void)
2376 {
2377 if (!optimizer)
2378 optimizer = new sem_item_optimizer ();
2379
2380 optimizer->read_summary ();
2381 optimizer->register_hooks ();
2382 }
2383
2384 /* Semantic equality exection function. */
2385
2386 static unsigned int
2387 ipa_icf_driver (void)
2388 {
2389 gcc_assert (optimizer);
2390
2391 optimizer->execute ();
2392 optimizer->unregister_hooks ();
2393
2394 delete optimizer;
2395 optimizer = NULL;
2396
2397 return 0;
2398 }
2399
2400 const pass_data pass_data_ipa_icf =
2401 {
2402 IPA_PASS, /* type */
2403 "icf", /* name */
2404 OPTGROUP_IPA, /* optinfo_flags */
2405 TV_IPA_ICF, /* tv_id */
2406 0, /* properties_required */
2407 0, /* properties_provided */
2408 0, /* properties_destroyed */
2409 0, /* todo_flags_start */
2410 0, /* todo_flags_finish */
2411 };
2412
2413 class pass_ipa_icf : public ipa_opt_pass_d
2414 {
2415 public:
2416 pass_ipa_icf (gcc::context *ctxt)
2417 : ipa_opt_pass_d (pass_data_ipa_icf, ctxt,
2418 ipa_icf_generate_summary, /* generate_summary */
2419 ipa_icf_write_summary, /* write_summary */
2420 ipa_icf_read_summary, /* read_summary */
2421 NULL, /*
2422 write_optimization_summary */
2423 NULL, /*
2424 read_optimization_summary */
2425 NULL, /* stmt_fixup */
2426 0, /* function_transform_todo_flags_start */
2427 NULL, /* function_transform */
2428 NULL) /* variable_transform */
2429 {}
2430
2431 /* opt_pass methods: */
2432 virtual bool gate (function *)
2433 {
2434 return flag_ipa_icf_variables || flag_ipa_icf_functions;
2435 }
2436
2437 virtual unsigned int execute (function *)
2438 {
2439 return ipa_icf_driver();
2440 }
2441 }; // class pass_ipa_icf
2442
2443 } // ipa_icf namespace
2444
2445 ipa_opt_pass_d *
2446 make_pass_ipa_icf (gcc::context *ctxt)
2447 {
2448 return new ipa_icf::pass_ipa_icf (ctxt);
2449 }