Fix for PR ipa/63851 and ipa/63852.
[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 if (DECL_STATIC_CHAIN (alias->decl))
698 {
699 if (dump_file)
700 fprintf (dump_file, "Thunk creation is risky for static-chain functions.\n\n");
701
702 return 0;
703 }
704
705 alias->icf_merged = true;
706 ipa_merge_profiles (local_original, alias);
707 alias->create_wrapper (local_original);
708
709 if (dump_file)
710 fprintf (dump_file, "Callgraph thunk has been created.\n\n");
711 }
712 else if (dump_file)
713 fprintf (dump_file, "Callgraph merge operation cannot be performed.\n\n");
714
715 return true;
716 }
717
718 /* Semantic item initialization function. */
719
720 void
721 sem_function::init (void)
722 {
723 if (in_lto_p)
724 get_node ()->get_untransformed_body ();
725
726 tree fndecl = node->decl;
727 function *func = DECL_STRUCT_FUNCTION (fndecl);
728
729 gcc_assert (func);
730 gcc_assert (SSANAMES (func));
731
732 ssa_names_size = SSANAMES (func)->length ();
733 node = node;
734
735 decl = fndecl;
736 region_tree = func->eh->region_tree;
737
738 /* iterating all function arguments. */
739 arg_count = count_formal_params (fndecl);
740
741 edge_count = n_edges_for_fn (func);
742 cfg_checksum = coverage_compute_cfg_checksum (func);
743
744 inchash::hash hstate;
745
746 basic_block bb;
747 FOR_EACH_BB_FN (bb, func)
748 {
749 unsigned nondbg_stmt_count = 0;
750
751 edge e;
752 for (edge_iterator ei = ei_start (bb->preds); ei_cond (ei, &e); ei_next (&ei))
753 cfg_checksum = iterative_hash_host_wide_int (e->flags,
754 cfg_checksum);
755
756 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
757 gsi_next (&gsi))
758 {
759 gimple stmt = gsi_stmt (gsi);
760
761 if (gimple_code (stmt) != GIMPLE_DEBUG)
762 {
763 hash_stmt (&hstate, stmt);
764 nondbg_stmt_count++;
765 }
766 }
767
768 gcode_hash = hstate.end ();
769 bb_sizes.safe_push (nondbg_stmt_count);
770
771 /* Inserting basic block to hash table. */
772 sem_bb *semantic_bb = new sem_bb (bb, nondbg_stmt_count,
773 EDGE_COUNT (bb->preds) + EDGE_COUNT (bb->succs));
774
775 bb_sorted.safe_push (semantic_bb);
776 }
777
778 parse_tree_args ();
779 }
780
781 /* Improve accumulated hash for HSTATE based on a gimple statement STMT. */
782
783 void
784 sem_function::hash_stmt (inchash::hash *hstate, gimple stmt)
785 {
786 enum gimple_code code = gimple_code (stmt);
787
788 hstate->add_int (code);
789
790 if (code == GIMPLE_CALL)
791 {
792 /* Checking of argument. */
793 for (unsigned i = 0; i < gimple_call_num_args (stmt); ++i)
794 {
795 tree argument = gimple_call_arg (stmt, i);
796
797 switch (TREE_CODE (argument))
798 {
799 case INTEGER_CST:
800 if (tree_fits_shwi_p (argument))
801 hstate->add_wide_int (tree_to_shwi (argument));
802 else if (tree_fits_uhwi_p (argument))
803 hstate->add_wide_int (tree_to_uhwi (argument));
804 break;
805 case REAL_CST:
806 REAL_VALUE_TYPE c;
807 HOST_WIDE_INT n;
808
809 c = TREE_REAL_CST (argument);
810 n = real_to_integer (&c);
811
812 hstate->add_wide_int (n);
813 break;
814 case ADDR_EXPR:
815 {
816 tree addr_operand = TREE_OPERAND (argument, 0);
817
818 if (TREE_CODE (addr_operand) == STRING_CST)
819 hstate->add (TREE_STRING_POINTER (addr_operand),
820 TREE_STRING_LENGTH (addr_operand));
821 break;
822 }
823 default:
824 break;
825 }
826 }
827 }
828 }
829
830
831 /* Return true if polymorphic comparison must be processed. */
832
833 bool
834 sem_function::compare_polymorphic_p (void)
835 {
836 return get_node ()->callees != NULL
837 || m_compared_func->get_node ()->callees != NULL;
838 }
839
840 /* For a given call graph NODE, the function constructs new
841 semantic function item. */
842
843 sem_function *
844 sem_function::parse (cgraph_node *node, bitmap_obstack *stack)
845 {
846 tree fndecl = node->decl;
847 function *func = DECL_STRUCT_FUNCTION (fndecl);
848
849 /* TODO: add support for thunks and aliases. */
850
851 if (!func || !node->has_gimple_body_p ())
852 return NULL;
853
854 if (lookup_attribute_by_prefix ("omp ", DECL_ATTRIBUTES (node->decl)) != NULL)
855 return NULL;
856
857 sem_function *f = new sem_function (node, 0, stack);
858
859 f->init ();
860
861 return f;
862 }
863
864 /* Parses function arguments and result type. */
865
866 void
867 sem_function::parse_tree_args (void)
868 {
869 tree result;
870
871 if (arg_types.exists ())
872 arg_types.release ();
873
874 arg_types.create (4);
875 tree fnargs = DECL_ARGUMENTS (decl);
876
877 for (tree parm = fnargs; parm; parm = DECL_CHAIN (parm))
878 arg_types.safe_push (DECL_ARG_TYPE (parm));
879
880 /* Function result type. */
881 result = DECL_RESULT (decl);
882 result_type = result ? TREE_TYPE (result) : NULL;
883
884 /* During WPA, we can get arguments by following method. */
885 if (!fnargs)
886 {
887 tree type = TYPE_ARG_TYPES (TREE_TYPE (decl));
888 for (tree parm = type; parm; parm = TREE_CHAIN (parm))
889 arg_types.safe_push (TYPE_CANONICAL (TREE_VALUE (parm)));
890
891 result_type = TREE_TYPE (TREE_TYPE (decl));
892 }
893 }
894
895 /* For given basic blocks BB1 and BB2 (from functions FUNC1 and FUNC),
896 return true if phi nodes are semantically equivalent in these blocks . */
897
898 bool
899 sem_function::compare_phi_node (basic_block bb1, basic_block bb2)
900 {
901 gphi_iterator si1, si2;
902 gphi *phi1, *phi2;
903 unsigned size1, size2, i;
904 tree t1, t2;
905 edge e1, e2;
906
907 gcc_assert (bb1 != NULL);
908 gcc_assert (bb2 != NULL);
909
910 si2 = gsi_start_phis (bb2);
911 for (si1 = gsi_start_phis (bb1); !gsi_end_p (si1);
912 gsi_next (&si1))
913 {
914 gsi_next_nonvirtual_phi (&si1);
915 gsi_next_nonvirtual_phi (&si2);
916
917 if (gsi_end_p (si1) && gsi_end_p (si2))
918 break;
919
920 if (gsi_end_p (si1) || gsi_end_p (si2))
921 return return_false();
922
923 phi1 = si1.phi ();
924 phi2 = si2.phi ();
925
926 tree phi_result1 = gimple_phi_result (phi1);
927 tree phi_result2 = gimple_phi_result (phi2);
928
929 if (!m_checker->compare_operand (phi_result1, phi_result2))
930 return return_false_with_msg ("PHI results are different");
931
932 size1 = gimple_phi_num_args (phi1);
933 size2 = gimple_phi_num_args (phi2);
934
935 if (size1 != size2)
936 return return_false ();
937
938 for (i = 0; i < size1; ++i)
939 {
940 t1 = gimple_phi_arg (phi1, i)->def;
941 t2 = gimple_phi_arg (phi2, i)->def;
942
943 if (!m_checker->compare_operand (t1, t2))
944 return return_false ();
945
946 e1 = gimple_phi_arg_edge (phi1, i);
947 e2 = gimple_phi_arg_edge (phi2, i);
948
949 if (!m_checker->compare_edge (e1, e2))
950 return return_false ();
951 }
952
953 gsi_next (&si2);
954 }
955
956 return true;
957 }
958
959 /* Returns true if tree T can be compared as a handled component. */
960
961 bool
962 sem_function::icf_handled_component_p (tree t)
963 {
964 tree_code tc = TREE_CODE (t);
965
966 return ((handled_component_p (t))
967 || tc == ADDR_EXPR || tc == MEM_REF || tc == REALPART_EXPR
968 || tc == IMAGPART_EXPR || tc == OBJ_TYPE_REF);
969 }
970
971 /* Basic blocks dictionary BB_DICT returns true if SOURCE index BB
972 corresponds to TARGET. */
973
974 bool
975 sem_function::bb_dict_test (auto_vec<int> bb_dict, int source, int target)
976 {
977 source++;
978 target++;
979
980 if (bb_dict.length () <= (unsigned)source)
981 bb_dict.safe_grow_cleared (source + 1);
982
983 if (bb_dict[source] == 0)
984 {
985 bb_dict[source] = target;
986 return true;
987 }
988 else
989 return bb_dict[source] == target;
990 }
991
992 /* Iterates all tree types in T1 and T2 and returns true if all types
993 are compatible. If COMPARE_POLYMORPHIC is set to true,
994 more strict comparison is executed. */
995
996 bool
997 sem_function::compare_type_list (tree t1, tree t2, bool compare_polymorphic)
998 {
999 tree tv1, tv2;
1000 tree_code tc1, tc2;
1001
1002 if (!t1 && !t2)
1003 return true;
1004
1005 while (t1 != NULL && t2 != NULL)
1006 {
1007 tv1 = TREE_VALUE (t1);
1008 tv2 = TREE_VALUE (t2);
1009
1010 tc1 = TREE_CODE (tv1);
1011 tc2 = TREE_CODE (tv2);
1012
1013 if (tc1 == NOP_EXPR && tc2 == NOP_EXPR)
1014 {}
1015 else if (tc1 == NOP_EXPR || tc2 == NOP_EXPR)
1016 return false;
1017 else if (!func_checker::compatible_types_p (tv1, tv2, compare_polymorphic))
1018 return false;
1019
1020 t1 = TREE_CHAIN (t1);
1021 t2 = TREE_CHAIN (t2);
1022 }
1023
1024 return !(t1 || t2);
1025 }
1026
1027
1028 /* Semantic variable constructor that uses STACK as bitmap memory stack. */
1029
1030 sem_variable::sem_variable (bitmap_obstack *stack): sem_item (VAR, stack)
1031 {
1032 }
1033
1034 /* Constructor based on varpool node _NODE with computed hash _HASH.
1035 Bitmap STACK is used for memory allocation. */
1036
1037 sem_variable::sem_variable (varpool_node *node, hashval_t _hash,
1038 bitmap_obstack *stack): sem_item(VAR,
1039 node, _hash, stack)
1040 {
1041 gcc_checking_assert (node);
1042 gcc_checking_assert (get_node ());
1043 }
1044
1045 /* Returns true if the item equals to ITEM given as argument. */
1046
1047 bool
1048 sem_variable::equals (sem_item *item,
1049 hash_map <symtab_node *, sem_item *> & ARG_UNUSED (ignored_nodes))
1050 {
1051 gcc_assert (item->type == VAR);
1052
1053 sem_variable *v = static_cast<sem_variable *>(item);
1054
1055 if (!ctor || !v->ctor)
1056 return return_false_with_msg ("ctor is missing for semantic variable");
1057
1058 return sem_variable::equals (ctor, v->ctor);
1059 }
1060
1061 /* Compares trees T1 and T2 for semantic equality. */
1062
1063 bool
1064 sem_variable::equals (tree t1, tree t2)
1065 {
1066 tree_code tc1 = TREE_CODE (t1);
1067 tree_code tc2 = TREE_CODE (t2);
1068
1069 if (tc1 != tc2)
1070 return false;
1071
1072 switch (tc1)
1073 {
1074 case CONSTRUCTOR:
1075 {
1076 unsigned len1 = vec_safe_length (CONSTRUCTOR_ELTS (t1));
1077 unsigned len2 = vec_safe_length (CONSTRUCTOR_ELTS (t2));
1078
1079 if (len1 != len2)
1080 return false;
1081
1082 for (unsigned i = 0; i < len1; i++)
1083 if (!sem_variable::equals (CONSTRUCTOR_ELT (t1, i)->value,
1084 CONSTRUCTOR_ELT (t2, i)->value)
1085 || CONSTRUCTOR_ELT (t1, i)->index != CONSTRUCTOR_ELT (t2, i)->index)
1086 return false;
1087
1088 return true;
1089 }
1090 case MEM_REF:
1091 {
1092 tree x1 = TREE_OPERAND (t1, 0);
1093 tree x2 = TREE_OPERAND (t2, 0);
1094 tree y1 = TREE_OPERAND (t1, 1);
1095 tree y2 = TREE_OPERAND (t2, 1);
1096
1097 if (!func_checker::compatible_types_p (TREE_TYPE (x1), TREE_TYPE (x2),
1098 true))
1099 return return_false ();
1100
1101 /* Type of the offset on MEM_REF does not matter. */
1102 return sem_variable::equals (x1, x2)
1103 && wi::to_offset (y1) == wi::to_offset (y2);
1104 }
1105 case NOP_EXPR:
1106 case ADDR_EXPR:
1107 {
1108 tree op1 = TREE_OPERAND (t1, 0);
1109 tree op2 = TREE_OPERAND (t2, 0);
1110 return sem_variable::equals (op1, op2);
1111 }
1112 case FUNCTION_DECL:
1113 case VAR_DECL:
1114 case FIELD_DECL:
1115 case LABEL_DECL:
1116 return t1 == t2;
1117 case INTEGER_CST:
1118 return func_checker::compatible_types_p (TREE_TYPE (t1), TREE_TYPE (t2),
1119 true)
1120 && wi::to_offset (t1) == wi::to_offset (t2);
1121 case STRING_CST:
1122 case REAL_CST:
1123 case COMPLEX_CST:
1124 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
1125 case COMPONENT_REF:
1126 case ARRAY_REF:
1127 case POINTER_PLUS_EXPR:
1128 {
1129 tree x1 = TREE_OPERAND (t1, 0);
1130 tree x2 = TREE_OPERAND (t2, 0);
1131 tree y1 = TREE_OPERAND (t1, 1);
1132 tree y2 = TREE_OPERAND (t2, 1);
1133
1134 return sem_variable::equals (x1, x2) && sem_variable::equals (y1, y2);
1135 }
1136 case ERROR_MARK:
1137 return return_false_with_msg ("ERROR_MARK");
1138 default:
1139 return return_false_with_msg ("Unknown TREE code reached");
1140 }
1141 }
1142
1143 /* Parser function that visits a varpool NODE. */
1144
1145 sem_variable *
1146 sem_variable::parse (varpool_node *node, bitmap_obstack *stack)
1147 {
1148 tree decl = node->decl;
1149
1150 bool readonly = TYPE_P (decl) ? TYPE_READONLY (decl) : TREE_READONLY (decl);
1151 if (!readonly)
1152 return NULL;
1153
1154 bool can_handle = DECL_VIRTUAL_P (decl)
1155 || flag_merge_constants >= 2
1156 || (!TREE_ADDRESSABLE (decl) && !node->externally_visible);
1157
1158 if (!can_handle || DECL_EXTERNAL (decl))
1159 return NULL;
1160
1161 tree ctor = ctor_for_folding (decl);
1162 if (!ctor)
1163 return NULL;
1164
1165 sem_variable *v = new sem_variable (node, 0, stack);
1166
1167 v->init ();
1168
1169 return v;
1170 }
1171
1172 /* References independent hash function. */
1173
1174 hashval_t
1175 sem_variable::get_hash (void)
1176 {
1177 if (hash)
1178 return hash;
1179
1180 inchash::hash hstate;
1181
1182 hstate.add_int (456346417);
1183 hstate.add_int (TREE_CODE (ctor));
1184
1185 if (TREE_CODE (ctor) == CONSTRUCTOR)
1186 {
1187 unsigned length = vec_safe_length (CONSTRUCTOR_ELTS (ctor));
1188 hstate.add_int (length);
1189 }
1190
1191 hash = hstate.end ();
1192
1193 return hash;
1194 }
1195
1196 /* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can
1197 be applied. */
1198
1199 bool
1200 sem_variable::merge (sem_item *alias_item)
1201 {
1202 gcc_assert (alias_item->type == VAR);
1203
1204 if (!sem_item::target_supports_symbol_aliases_p ())
1205 {
1206 if (dump_file)
1207 fprintf (dump_file, "Symbol aliases are not supported by target\n\n");
1208 return false;
1209 }
1210
1211 sem_variable *alias_var = static_cast<sem_variable *> (alias_item);
1212
1213 varpool_node *original = get_node ();
1214 varpool_node *alias = alias_var->get_node ();
1215 bool original_discardable = false;
1216
1217 /* See if original is in a section that can be discarded if the main
1218 symbol is not used. */
1219 if (DECL_EXTERNAL (original->decl))
1220 original_discardable = true;
1221 if (original->resolution == LDPR_PREEMPTED_REG
1222 || original->resolution == LDPR_PREEMPTED_IR)
1223 original_discardable = true;
1224 if (original->can_be_discarded_p ())
1225 original_discardable = true;
1226
1227 gcc_assert (!TREE_ASM_WRITTEN (alias->decl));
1228
1229 if (original_discardable || DECL_EXTERNAL (alias_var->decl) ||
1230 !compare_sections (alias_var))
1231 {
1232 if (dump_file)
1233 fprintf (dump_file, "Varpool alias cannot be created\n\n");
1234
1235 return false;
1236 }
1237 else
1238 {
1239 // alias cycle creation check
1240 varpool_node *n = original;
1241
1242 while (n->alias)
1243 {
1244 n = n->get_alias_target ();
1245 if (n == alias)
1246 {
1247 if (dump_file)
1248 fprintf (dump_file, "Varpool alias cannot be created (alias cycle).\n\n");
1249
1250 return false;
1251 }
1252 }
1253
1254 alias->analyzed = false;
1255
1256 DECL_INITIAL (alias->decl) = NULL;
1257 alias->need_bounds_init = false;
1258 alias->remove_all_references ();
1259
1260 varpool_node::create_alias (alias_var->decl, decl);
1261 alias->resolve_alias (original);
1262
1263 if (dump_file)
1264 fprintf (dump_file, "Varpool alias has been created.\n\n");
1265
1266 return true;
1267 }
1268 }
1269
1270 bool
1271 sem_variable::compare_sections (sem_variable *alias)
1272 {
1273 const char *source = node->get_section ();
1274 const char *target = alias->node->get_section();
1275
1276 if (source == NULL && target == NULL)
1277 return true;
1278 else if(!source || !target)
1279 return false;
1280 else
1281 return strcmp (source, target) == 0;
1282 }
1283
1284 /* Dump symbol to FILE. */
1285
1286 void
1287 sem_variable::dump_to_file (FILE *file)
1288 {
1289 gcc_assert (file);
1290
1291 print_node (file, "", decl, 0);
1292 fprintf (file, "\n\n");
1293 }
1294
1295 /* Iterates though a constructor and identifies tree references
1296 we are interested in semantic function equality. */
1297
1298 void
1299 sem_variable::parse_tree_refs (tree t)
1300 {
1301 switch (TREE_CODE (t))
1302 {
1303 case CONSTRUCTOR:
1304 {
1305 unsigned length = vec_safe_length (CONSTRUCTOR_ELTS (t));
1306
1307 for (unsigned i = 0; i < length; i++)
1308 parse_tree_refs(CONSTRUCTOR_ELT (t, i)->value);
1309
1310 break;
1311 }
1312 case NOP_EXPR:
1313 case ADDR_EXPR:
1314 {
1315 tree op = TREE_OPERAND (t, 0);
1316 parse_tree_refs (op);
1317 break;
1318 }
1319 case FUNCTION_DECL:
1320 {
1321 tree_refs.safe_push (t);
1322 break;
1323 }
1324 default:
1325 break;
1326 }
1327 }
1328
1329 unsigned int sem_item_optimizer::class_id = 0;
1330
1331 sem_item_optimizer::sem_item_optimizer (): worklist (0), m_classes (0),
1332 m_classes_count (0), m_cgraph_node_hooks (NULL), m_varpool_node_hooks (NULL)
1333 {
1334 m_items.create (0);
1335 bitmap_obstack_initialize (&m_bmstack);
1336 }
1337
1338 sem_item_optimizer::~sem_item_optimizer ()
1339 {
1340 for (unsigned int i = 0; i < m_items.length (); i++)
1341 delete m_items[i];
1342
1343 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
1344 it != m_classes.end (); ++it)
1345 {
1346 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
1347 delete (*it)->classes[i];
1348
1349 (*it)->classes.release ();
1350 free (*it);
1351 }
1352
1353 m_items.release ();
1354
1355 bitmap_obstack_release (&m_bmstack);
1356 }
1357
1358 /* Write IPA ICF summary for symbols. */
1359
1360 void
1361 sem_item_optimizer::write_summary (void)
1362 {
1363 unsigned int count = 0;
1364
1365 output_block *ob = create_output_block (LTO_section_ipa_icf);
1366 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
1367 ob->symbol = NULL;
1368
1369 /* Calculate number of symbols to be serialized. */
1370 for (lto_symtab_encoder_iterator lsei = lsei_start_in_partition (encoder);
1371 !lsei_end_p (lsei);
1372 lsei_next_in_partition (&lsei))
1373 {
1374 symtab_node *node = lsei_node (lsei);
1375
1376 if (m_symtab_node_map.get (node))
1377 count++;
1378 }
1379
1380 streamer_write_uhwi (ob, count);
1381
1382 /* Process all of the symbols. */
1383 for (lto_symtab_encoder_iterator lsei = lsei_start_in_partition (encoder);
1384 !lsei_end_p (lsei);
1385 lsei_next_in_partition (&lsei))
1386 {
1387 symtab_node *node = lsei_node (lsei);
1388
1389 sem_item **item = m_symtab_node_map.get (node);
1390
1391 if (item && *item)
1392 {
1393 int node_ref = lto_symtab_encoder_encode (encoder, node);
1394 streamer_write_uhwi_stream (ob->main_stream, node_ref);
1395
1396 streamer_write_uhwi (ob, (*item)->get_hash ());
1397 }
1398 }
1399
1400 streamer_write_char_stream (ob->main_stream, 0);
1401 produce_asm (ob, NULL);
1402 destroy_output_block (ob);
1403 }
1404
1405 /* Reads a section from LTO stream file FILE_DATA. Input block for DATA
1406 contains LEN bytes. */
1407
1408 void
1409 sem_item_optimizer::read_section (lto_file_decl_data *file_data,
1410 const char *data, size_t len)
1411 {
1412 const lto_function_header *header =
1413 (const lto_function_header *) data;
1414 const int cfg_offset = sizeof (lto_function_header);
1415 const int main_offset = cfg_offset + header->cfg_size;
1416 const int string_offset = main_offset + header->main_size;
1417 data_in *data_in;
1418 unsigned int i;
1419 unsigned int count;
1420
1421 lto_input_block ib_main ((const char *) data + main_offset, 0,
1422 header->main_size);
1423
1424 data_in =
1425 lto_data_in_create (file_data, (const char *) data + string_offset,
1426 header->string_size, vNULL);
1427
1428 count = streamer_read_uhwi (&ib_main);
1429
1430 for (i = 0; i < count; i++)
1431 {
1432 unsigned int index;
1433 symtab_node *node;
1434 lto_symtab_encoder_t encoder;
1435
1436 index = streamer_read_uhwi (&ib_main);
1437 encoder = file_data->symtab_node_encoder;
1438 node = lto_symtab_encoder_deref (encoder, index);
1439
1440 hashval_t hash = streamer_read_uhwi (&ib_main);
1441
1442 gcc_assert (node->definition);
1443
1444 if (dump_file)
1445 fprintf (dump_file, "Symbol added:%s (tree: %p, uid:%u)\n", node->asm_name (),
1446 (void *) node->decl, node->order);
1447
1448 if (is_a<cgraph_node *> (node))
1449 {
1450 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1451
1452 m_items.safe_push (new sem_function (cnode, hash, &m_bmstack));
1453 }
1454 else
1455 {
1456 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1457
1458 m_items.safe_push (new sem_variable (vnode, hash, &m_bmstack));
1459 }
1460 }
1461
1462 lto_free_section_data (file_data, LTO_section_ipa_icf, NULL, data,
1463 len);
1464 lto_data_in_delete (data_in);
1465 }
1466
1467 /* Read IPA IPA ICF summary for symbols. */
1468
1469 void
1470 sem_item_optimizer::read_summary (void)
1471 {
1472 lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1473 lto_file_decl_data *file_data;
1474 unsigned int j = 0;
1475
1476 while ((file_data = file_data_vec[j++]))
1477 {
1478 size_t len;
1479 const char *data = lto_get_section_data (file_data,
1480 LTO_section_ipa_icf, NULL, &len);
1481
1482 if (data)
1483 read_section (file_data, data, len);
1484 }
1485 }
1486
1487 /* Register callgraph and varpool hooks. */
1488
1489 void
1490 sem_item_optimizer::register_hooks (void)
1491 {
1492 m_cgraph_node_hooks = symtab->add_cgraph_removal_hook
1493 (&sem_item_optimizer::cgraph_removal_hook, this);
1494
1495 m_varpool_node_hooks = symtab->add_varpool_removal_hook
1496 (&sem_item_optimizer::varpool_removal_hook, this);
1497 }
1498
1499 /* Unregister callgraph and varpool hooks. */
1500
1501 void
1502 sem_item_optimizer::unregister_hooks (void)
1503 {
1504 if (m_cgraph_node_hooks)
1505 symtab->remove_cgraph_removal_hook (m_cgraph_node_hooks);
1506
1507 if (m_varpool_node_hooks)
1508 symtab->remove_varpool_removal_hook (m_varpool_node_hooks);
1509 }
1510
1511 /* Adds a CLS to hashtable associated by hash value. */
1512
1513 void
1514 sem_item_optimizer::add_class (congruence_class *cls)
1515 {
1516 gcc_assert (cls->members.length ());
1517
1518 congruence_class_group *group = get_group_by_hash (
1519 cls->members[0]->get_hash (),
1520 cls->members[0]->type);
1521 group->classes.safe_push (cls);
1522 }
1523
1524 /* Gets a congruence class group based on given HASH value and TYPE. */
1525
1526 congruence_class_group *
1527 sem_item_optimizer::get_group_by_hash (hashval_t hash, sem_item_type type)
1528 {
1529 congruence_class_group *item = XNEW (congruence_class_group);
1530 item->hash = hash;
1531 item->type = type;
1532
1533 congruence_class_group **slot = m_classes.find_slot (item, INSERT);
1534
1535 if (*slot)
1536 free (item);
1537 else
1538 {
1539 item->classes.create (1);
1540 *slot = item;
1541 }
1542
1543 return *slot;
1544 }
1545
1546 /* Callgraph removal hook called for a NODE with a custom DATA. */
1547
1548 void
1549 sem_item_optimizer::cgraph_removal_hook (cgraph_node *node, void *data)
1550 {
1551 sem_item_optimizer *optimizer = (sem_item_optimizer *) data;
1552 optimizer->remove_symtab_node (node);
1553 }
1554
1555 /* Varpool removal hook called for a NODE with a custom DATA. */
1556
1557 void
1558 sem_item_optimizer::varpool_removal_hook (varpool_node *node, void *data)
1559 {
1560 sem_item_optimizer *optimizer = (sem_item_optimizer *) data;
1561 optimizer->remove_symtab_node (node);
1562 }
1563
1564 /* Remove symtab NODE triggered by symtab removal hooks. */
1565
1566 void
1567 sem_item_optimizer::remove_symtab_node (symtab_node *node)
1568 {
1569 gcc_assert (!m_classes.elements());
1570
1571 m_removed_items_set.add (node);
1572 }
1573
1574 void
1575 sem_item_optimizer::remove_item (sem_item *item)
1576 {
1577 if (m_symtab_node_map.get (item->node))
1578 m_symtab_node_map.remove (item->node);
1579 delete item;
1580 }
1581
1582 /* Removes all callgraph and varpool nodes that are marked by symtab
1583 as deleted. */
1584
1585 void
1586 sem_item_optimizer::filter_removed_items (void)
1587 {
1588 auto_vec <sem_item *> filtered;
1589
1590 for (unsigned int i = 0; i < m_items.length(); i++)
1591 {
1592 sem_item *item = m_items[i];
1593
1594 if (!flag_ipa_icf_functions && item->type == FUNC)
1595 {
1596 remove_item (item);
1597 continue;
1598 }
1599
1600 if (!flag_ipa_icf_variables && item->type == VAR)
1601 {
1602 remove_item (item);
1603 continue;
1604 }
1605
1606 bool no_body_function = false;
1607
1608 if (item->type == FUNC)
1609 {
1610 cgraph_node *cnode = static_cast <sem_function *>(item)->get_node ();
1611
1612 no_body_function = in_lto_p && (cnode->alias || cnode->body_removed);
1613 }
1614
1615 if(!m_removed_items_set.contains (m_items[i]->node)
1616 && !no_body_function)
1617 {
1618 if (item->type == VAR || (!DECL_CXX_CONSTRUCTOR_P (item->decl)
1619 && !DECL_CXX_DESTRUCTOR_P (item->decl)))
1620 {
1621 filtered.safe_push (m_items[i]);
1622 continue;
1623 }
1624 }
1625
1626 remove_item (item);
1627 }
1628
1629 /* Clean-up of released semantic items. */
1630
1631 m_items.release ();
1632 for (unsigned int i = 0; i < filtered.length(); i++)
1633 m_items.safe_push (filtered[i]);
1634 }
1635
1636 /* Optimizer entry point. */
1637
1638 void
1639 sem_item_optimizer::execute (void)
1640 {
1641 filter_removed_items ();
1642 build_hash_based_classes ();
1643
1644 if (dump_file)
1645 fprintf (dump_file, "Dump after hash based groups\n");
1646 dump_cong_classes ();
1647
1648 for (unsigned int i = 0; i < m_items.length(); i++)
1649 m_items[i]->init_wpa ();
1650
1651 build_graph ();
1652
1653 subdivide_classes_by_equality (true);
1654
1655 if (dump_file)
1656 fprintf (dump_file, "Dump after WPA based types groups\n");
1657
1658 dump_cong_classes ();
1659
1660 process_cong_reduction ();
1661 verify_classes ();
1662
1663 if (dump_file)
1664 fprintf (dump_file, "Dump after callgraph-based congruence reduction\n");
1665
1666 dump_cong_classes ();
1667
1668 parse_nonsingleton_classes ();
1669 subdivide_classes_by_equality ();
1670
1671 if (dump_file)
1672 fprintf (dump_file, "Dump after full equality comparison of groups\n");
1673
1674 dump_cong_classes ();
1675
1676 unsigned int prev_class_count = m_classes_count;
1677
1678 process_cong_reduction ();
1679 dump_cong_classes ();
1680 verify_classes ();
1681 merge_classes (prev_class_count);
1682
1683 if (dump_file && (dump_flags & TDF_DETAILS))
1684 symtab_node::dump_table (dump_file);
1685 }
1686
1687 /* Function responsible for visiting all potential functions and
1688 read-only variables that can be merged. */
1689
1690 void
1691 sem_item_optimizer::parse_funcs_and_vars (void)
1692 {
1693 cgraph_node *cnode;
1694
1695 if (flag_ipa_icf_functions)
1696 FOR_EACH_DEFINED_FUNCTION (cnode)
1697 {
1698 sem_function *f = sem_function::parse (cnode, &m_bmstack);
1699 if (f)
1700 {
1701 m_items.safe_push (f);
1702 m_symtab_node_map.put (cnode, f);
1703
1704 if (dump_file)
1705 fprintf (dump_file, "Parsed function:%s\n", f->asm_name ());
1706
1707 if (dump_file && (dump_flags & TDF_DETAILS))
1708 f->dump_to_file (dump_file);
1709 }
1710 else if (dump_file)
1711 fprintf (dump_file, "Not parsed function:%s\n", cnode->asm_name ());
1712 }
1713
1714 varpool_node *vnode;
1715
1716 if (flag_ipa_icf_variables)
1717 FOR_EACH_DEFINED_VARIABLE (vnode)
1718 {
1719 sem_variable *v = sem_variable::parse (vnode, &m_bmstack);
1720
1721 if (v)
1722 {
1723 m_items.safe_push (v);
1724 m_symtab_node_map.put (vnode, v);
1725 }
1726 }
1727 }
1728
1729 /* Makes pairing between a congruence class CLS and semantic ITEM. */
1730
1731 void
1732 sem_item_optimizer::add_item_to_class (congruence_class *cls, sem_item *item)
1733 {
1734 item->index_in_class = cls->members.length ();
1735 cls->members.safe_push (item);
1736 item->cls = cls;
1737 }
1738
1739 /* Congruence classes are built by hash value. */
1740
1741 void
1742 sem_item_optimizer::build_hash_based_classes (void)
1743 {
1744 for (unsigned i = 0; i < m_items.length (); i++)
1745 {
1746 sem_item *item = m_items[i];
1747
1748 congruence_class_group *group = get_group_by_hash (item->get_hash (),
1749 item->type);
1750
1751 if (!group->classes.length ())
1752 {
1753 m_classes_count++;
1754 group->classes.safe_push (new congruence_class (class_id++));
1755 }
1756
1757 add_item_to_class (group->classes[0], item);
1758 }
1759 }
1760
1761 /* Build references according to call graph. */
1762
1763 void
1764 sem_item_optimizer::build_graph (void)
1765 {
1766 for (unsigned i = 0; i < m_items.length (); i++)
1767 {
1768 sem_item *item = m_items[i];
1769 m_symtab_node_map.put (item->node, item);
1770 }
1771
1772 for (unsigned i = 0; i < m_items.length (); i++)
1773 {
1774 sem_item *item = m_items[i];
1775
1776 if (item->type == FUNC)
1777 {
1778 cgraph_node *cnode = dyn_cast <cgraph_node *> (item->node);
1779
1780 cgraph_edge *e = cnode->callees;
1781 while (e)
1782 {
1783 sem_item **slot = m_symtab_node_map.get (e->callee);
1784 if (slot)
1785 item->add_reference (*slot);
1786
1787 e = e->next_callee;
1788 }
1789 }
1790
1791 ipa_ref *ref = NULL;
1792 for (unsigned i = 0; item->node->iterate_reference (i, ref); i++)
1793 {
1794 sem_item **slot = m_symtab_node_map.get (ref->referred);
1795 if (slot)
1796 item->add_reference (*slot);
1797 }
1798 }
1799 }
1800
1801 /* Semantic items in classes having more than one element and initialized.
1802 In case of WPA, we load function body. */
1803
1804 void
1805 sem_item_optimizer::parse_nonsingleton_classes (void)
1806 {
1807 unsigned int init_called_count = 0;
1808
1809 for (unsigned i = 0; i < m_items.length (); i++)
1810 if (m_items[i]->cls->members.length () > 1)
1811 {
1812 m_items[i]->init ();
1813 init_called_count++;
1814 }
1815
1816 if (dump_file)
1817 fprintf (dump_file, "Init called for %u items (%.2f%%).\n", init_called_count,
1818 m_items.length () ? 100.0f * init_called_count / m_items.length (): 0.0f);
1819 }
1820
1821 /* Equality function for semantic items is used to subdivide existing
1822 classes. If IN_WPA, fast equality function is invoked. */
1823
1824 void
1825 sem_item_optimizer::subdivide_classes_by_equality (bool in_wpa)
1826 {
1827 for (hash_table <congruence_class_group_hash>::iterator it = m_classes.begin ();
1828 it != m_classes.end (); ++it)
1829 {
1830 unsigned int class_count = (*it)->classes.length ();
1831
1832 for (unsigned i = 0; i < class_count; i++)
1833 {
1834 congruence_class *c = (*it)->classes [i];
1835
1836 if (c->members.length() > 1)
1837 {
1838 auto_vec <sem_item *> new_vector;
1839
1840 sem_item *first = c->members[0];
1841 new_vector.safe_push (first);
1842
1843 unsigned class_split_first = (*it)->classes.length ();
1844
1845 for (unsigned j = 1; j < c->members.length (); j++)
1846 {
1847 sem_item *item = c->members[j];
1848
1849 bool equals = in_wpa ? first->equals_wpa (item,
1850 m_symtab_node_map) : first->equals (item, m_symtab_node_map);
1851
1852 if (equals)
1853 new_vector.safe_push (item);
1854 else
1855 {
1856 bool integrated = false;
1857
1858 for (unsigned k = class_split_first; k < (*it)->classes.length (); k++)
1859 {
1860 sem_item *x = (*it)->classes[k]->members[0];
1861 bool equals = in_wpa ? x->equals_wpa (item,
1862 m_symtab_node_map) : x->equals (item, m_symtab_node_map);
1863
1864 if (equals)
1865 {
1866 integrated = true;
1867 add_item_to_class ((*it)->classes[k], item);
1868
1869 break;
1870 }
1871 }
1872
1873 if (!integrated)
1874 {
1875 congruence_class *c = new congruence_class (class_id++);
1876 m_classes_count++;
1877 add_item_to_class (c, item);
1878
1879 (*it)->classes.safe_push (c);
1880 }
1881 }
1882 }
1883
1884 // we replace newly created new_vector for the class we've just splitted
1885 c->members.release ();
1886 c->members.create (new_vector.length ());
1887
1888 for (unsigned int j = 0; j < new_vector.length (); j++)
1889 add_item_to_class (c, new_vector[j]);
1890 }
1891 }
1892 }
1893
1894 verify_classes ();
1895 }
1896
1897 /* Verify congruence classes if checking is enabled. */
1898
1899 void
1900 sem_item_optimizer::verify_classes (void)
1901 {
1902 #if ENABLE_CHECKING
1903 for (hash_table <congruence_class_group_hash>::iterator it = m_classes.begin ();
1904 it != m_classes.end (); ++it)
1905 {
1906 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
1907 {
1908 congruence_class *cls = (*it)->classes[i];
1909
1910 gcc_checking_assert (cls);
1911 gcc_checking_assert (cls->members.length () > 0);
1912
1913 for (unsigned int j = 0; j < cls->members.length (); j++)
1914 {
1915 sem_item *item = cls->members[j];
1916
1917 gcc_checking_assert (item);
1918 gcc_checking_assert (item->cls == cls);
1919
1920 for (unsigned k = 0; k < item->usages.length (); k++)
1921 {
1922 sem_usage_pair *usage = item->usages[k];
1923 gcc_checking_assert (usage->item->index_in_class <
1924 usage->item->cls->members.length ());
1925 }
1926 }
1927 }
1928 }
1929 #endif
1930 }
1931
1932 /* Disposes split map traverse function. CLS_PTR is pointer to congruence
1933 class, BSLOT is bitmap slot we want to release. DATA is mandatory,
1934 but unused argument. */
1935
1936 bool
1937 sem_item_optimizer::release_split_map (congruence_class * const &,
1938 bitmap const &b, traverse_split_pair *)
1939 {
1940 bitmap bmp = b;
1941
1942 BITMAP_FREE (bmp);
1943
1944 return true;
1945 }
1946
1947 /* Process split operation for a class given as pointer CLS_PTR,
1948 where bitmap B splits congruence class members. DATA is used
1949 as argument of split pair. */
1950
1951 bool
1952 sem_item_optimizer::traverse_congruence_split (congruence_class * const &cls,
1953 bitmap const &b, traverse_split_pair *pair)
1954 {
1955 sem_item_optimizer *optimizer = pair->optimizer;
1956 const congruence_class *splitter_cls = pair->cls;
1957
1958 /* If counted bits are greater than zero and less than the number of members
1959 a group will be splitted. */
1960 unsigned popcount = bitmap_count_bits (b);
1961
1962 if (popcount > 0 && popcount < cls->members.length ())
1963 {
1964 congruence_class* newclasses[2] = { new congruence_class (class_id++), new congruence_class (class_id++) };
1965
1966 for (unsigned int i = 0; i < cls->members.length (); i++)
1967 {
1968 int target = bitmap_bit_p (b, i);
1969 congruence_class *tc = newclasses[target];
1970
1971 add_item_to_class (tc, cls->members[i]);
1972 }
1973
1974 #ifdef ENABLE_CHECKING
1975 for (unsigned int i = 0; i < 2; i++)
1976 gcc_checking_assert (newclasses[i]->members.length ());
1977 #endif
1978
1979 if (splitter_cls == cls)
1980 optimizer->splitter_class_removed = true;
1981
1982 /* Remove old class from worklist if presented. */
1983 bool in_worklist = cls->in_worklist;
1984
1985 if (in_worklist)
1986 cls->in_worklist = false;
1987
1988 congruence_class_group g;
1989 g.hash = cls->members[0]->get_hash ();
1990 g.type = cls->members[0]->type;
1991
1992 congruence_class_group *slot = optimizer->m_classes.find(&g);
1993
1994 for (unsigned int i = 0; i < slot->classes.length (); i++)
1995 if (slot->classes[i] == cls)
1996 {
1997 slot->classes.ordered_remove (i);
1998 break;
1999 }
2000
2001 /* New class will be inserted and integrated to work list. */
2002 for (unsigned int i = 0; i < 2; i++)
2003 optimizer->add_class (newclasses[i]);
2004
2005 /* Two classes replace one, so that increment just by one. */
2006 optimizer->m_classes_count++;
2007
2008 /* If OLD class was presented in the worklist, we remove the class
2009 and replace it will both newly created classes. */
2010 if (in_worklist)
2011 for (unsigned int i = 0; i < 2; i++)
2012 optimizer->worklist_push (newclasses[i]);
2013 else /* Just smaller class is inserted. */
2014 {
2015 unsigned int smaller_index = newclasses[0]->members.length () <
2016 newclasses[1]->members.length () ?
2017 0 : 1;
2018 optimizer->worklist_push (newclasses[smaller_index]);
2019 }
2020
2021 if (dump_file && (dump_flags & TDF_DETAILS))
2022 {
2023 fprintf (dump_file, " congruence class splitted:\n");
2024 cls->dump (dump_file, 4);
2025
2026 fprintf (dump_file, " newly created groups:\n");
2027 for (unsigned int i = 0; i < 2; i++)
2028 newclasses[i]->dump (dump_file, 4);
2029 }
2030
2031 /* Release class if not presented in work list. */
2032 if (!in_worklist)
2033 delete cls;
2034 }
2035
2036
2037 return true;
2038 }
2039
2040 /* Tests if a class CLS used as INDEXth splits any congruence classes.
2041 Bitmap stack BMSTACK is used for bitmap allocation. */
2042
2043 void
2044 sem_item_optimizer::do_congruence_step_for_index (congruence_class *cls,
2045 unsigned int index)
2046 {
2047 hash_map <congruence_class *, bitmap> split_map;
2048
2049 for (unsigned int i = 0; i < cls->members.length (); i++)
2050 {
2051 sem_item *item = cls->members[i];
2052
2053 /* Iterate all usages that have INDEX as usage of the item. */
2054 for (unsigned int j = 0; j < item->usages.length (); j++)
2055 {
2056 sem_usage_pair *usage = item->usages[j];
2057
2058 if (usage->index != index)
2059 continue;
2060
2061 bitmap *slot = split_map.get (usage->item->cls);
2062 bitmap b;
2063
2064 if(!slot)
2065 {
2066 b = BITMAP_ALLOC (&m_bmstack);
2067 split_map.put (usage->item->cls, b);
2068 }
2069 else
2070 b = *slot;
2071
2072 #if ENABLE_CHECKING
2073 gcc_checking_assert (usage->item->cls);
2074 gcc_checking_assert (usage->item->index_in_class <
2075 usage->item->cls->members.length ());
2076 #endif
2077
2078 bitmap_set_bit (b, usage->item->index_in_class);
2079 }
2080 }
2081
2082 traverse_split_pair pair;
2083 pair.optimizer = this;
2084 pair.cls = cls;
2085
2086 splitter_class_removed = false;
2087 split_map.traverse
2088 <traverse_split_pair *, sem_item_optimizer::traverse_congruence_split> (&pair);
2089
2090 /* Bitmap clean-up. */
2091 split_map.traverse
2092 <traverse_split_pair *, sem_item_optimizer::release_split_map> (NULL);
2093 }
2094
2095 /* Every usage of a congruence class CLS is a candidate that can split the
2096 collection of classes. Bitmap stack BMSTACK is used for bitmap
2097 allocation. */
2098
2099 void
2100 sem_item_optimizer::do_congruence_step (congruence_class *cls)
2101 {
2102 bitmap_iterator bi;
2103 unsigned int i;
2104
2105 bitmap usage = BITMAP_ALLOC (&m_bmstack);
2106
2107 for (unsigned int i = 0; i < cls->members.length (); i++)
2108 bitmap_ior_into (usage, cls->members[i]->usage_index_bitmap);
2109
2110 EXECUTE_IF_SET_IN_BITMAP (usage, 0, i, bi)
2111 {
2112 if (dump_file && (dump_flags & TDF_DETAILS))
2113 fprintf (dump_file, " processing congruece step for class: %u, index: %u\n",
2114 cls->id, i);
2115
2116 do_congruence_step_for_index (cls, i);
2117
2118 if (splitter_class_removed)
2119 break;
2120 }
2121
2122 BITMAP_FREE (usage);
2123 }
2124
2125 /* Adds a newly created congruence class CLS to worklist. */
2126
2127 void
2128 sem_item_optimizer::worklist_push (congruence_class *cls)
2129 {
2130 /* Return if the class CLS is already presented in work list. */
2131 if (cls->in_worklist)
2132 return;
2133
2134 cls->in_worklist = true;
2135 worklist.push_back (cls);
2136 }
2137
2138 /* Pops a class from worklist. */
2139
2140 congruence_class *
2141 sem_item_optimizer::worklist_pop (void)
2142 {
2143 congruence_class *cls;
2144
2145 while (!worklist.empty ())
2146 {
2147 cls = worklist.front ();
2148 worklist.pop_front ();
2149 if (cls->in_worklist)
2150 {
2151 cls->in_worklist = false;
2152
2153 return cls;
2154 }
2155 else
2156 {
2157 /* Work list item was already intended to be removed.
2158 The only reason for doing it is to split a class.
2159 Thus, the class CLS is deleted. */
2160 delete cls;
2161 }
2162 }
2163
2164 return NULL;
2165 }
2166
2167 /* Iterative congruence reduction function. */
2168
2169 void
2170 sem_item_optimizer::process_cong_reduction (void)
2171 {
2172 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
2173 it != m_classes.end (); ++it)
2174 for (unsigned i = 0; i < (*it)->classes.length (); i++)
2175 if ((*it)->classes[i]->is_class_used ())
2176 worklist_push ((*it)->classes[i]);
2177
2178 if (dump_file)
2179 fprintf (dump_file, "Worklist has been filled with: %lu\n",
2180 (unsigned long) worklist.size ());
2181
2182 if (dump_file && (dump_flags & TDF_DETAILS))
2183 fprintf (dump_file, "Congruence class reduction\n");
2184
2185 congruence_class *cls;
2186 while ((cls = worklist_pop ()) != NULL)
2187 do_congruence_step (cls);
2188 }
2189
2190 /* Debug function prints all informations about congruence classes. */
2191
2192 void
2193 sem_item_optimizer::dump_cong_classes (void)
2194 {
2195 if (!dump_file)
2196 return;
2197
2198 fprintf (dump_file,
2199 "Congruence classes: %u (unique hash values: %lu), with total: %u items\n",
2200 m_classes_count, (unsigned long) m_classes.elements(), m_items.length ());
2201
2202 /* Histogram calculation. */
2203 unsigned int max_index = 0;
2204 unsigned int* histogram = XCNEWVEC (unsigned int, m_items.length () + 1);
2205
2206 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
2207 it != m_classes.end (); ++it)
2208
2209 for (unsigned i = 0; i < (*it)->classes.length (); i++)
2210 {
2211 unsigned int c = (*it)->classes[i]->members.length ();
2212 histogram[c]++;
2213
2214 if (c > max_index)
2215 max_index = c;
2216 }
2217
2218 fprintf (dump_file,
2219 "Class size histogram [num of members]: number of classe number of classess\n");
2220
2221 for (unsigned int i = 0; i <= max_index; i++)
2222 if (histogram[i])
2223 fprintf (dump_file, "[%u]: %u classes\n", i, histogram[i]);
2224
2225 fprintf (dump_file, "\n\n");
2226
2227
2228 if (dump_flags & TDF_DETAILS)
2229 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
2230 it != m_classes.end (); ++it)
2231 {
2232 fprintf (dump_file, " group: with %u classes:\n", (*it)->classes.length ());
2233
2234 for (unsigned i = 0; i < (*it)->classes.length (); i++)
2235 {
2236 (*it)->classes[i]->dump (dump_file, 4);
2237
2238 if(i < (*it)->classes.length () - 1)
2239 fprintf (dump_file, " ");
2240 }
2241 }
2242
2243 free (histogram);
2244 }
2245
2246 /* After reduction is done, we can declare all items in a group
2247 to be equal. PREV_CLASS_COUNT is start number of classes
2248 before reduction. */
2249
2250 void
2251 sem_item_optimizer::merge_classes (unsigned int prev_class_count)
2252 {
2253 unsigned int item_count = m_items.length ();
2254 unsigned int class_count = m_classes_count;
2255 unsigned int equal_items = item_count - class_count;
2256
2257 unsigned int non_singular_classes_count = 0;
2258 unsigned int non_singular_classes_sum = 0;
2259
2260 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
2261 it != m_classes.end (); ++it)
2262 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
2263 {
2264 congruence_class *c = (*it)->classes[i];
2265 if (c->members.length () > 1)
2266 {
2267 non_singular_classes_count++;
2268 non_singular_classes_sum += c->members.length ();
2269 }
2270 }
2271
2272 if (dump_file)
2273 {
2274 fprintf (dump_file, "\nItem count: %u\n", item_count);
2275 fprintf (dump_file, "Congruent classes before: %u, after: %u\n",
2276 prev_class_count, class_count);
2277 fprintf (dump_file, "Average class size before: %.2f, after: %.2f\n",
2278 prev_class_count ? 1.0f * item_count / prev_class_count : 0.0f,
2279 class_count ? 1.0f * item_count / class_count : 0.0f);
2280 fprintf (dump_file, "Average non-singular class size: %.2f, count: %u\n",
2281 non_singular_classes_count ? 1.0f * non_singular_classes_sum /
2282 non_singular_classes_count : 0.0f,
2283 non_singular_classes_count);
2284 fprintf (dump_file, "Equal symbols: %u\n", equal_items);
2285 fprintf (dump_file, "Fraction of visited symbols: %.2f%%\n\n",
2286 item_count ? 100.0f * equal_items / item_count : 0.0f);
2287 }
2288
2289 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
2290 it != m_classes.end (); ++it)
2291 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
2292 {
2293 congruence_class *c = (*it)->classes[i];
2294
2295 if (c->members.length () == 1)
2296 continue;
2297
2298 gcc_assert (c->members.length ());
2299
2300 sem_item *source = c->members[0];
2301
2302 for (unsigned int j = 1; j < c->members.length (); j++)
2303 {
2304 sem_item *alias = c->members[j];
2305 source->equals (alias, m_symtab_node_map);
2306
2307 if (dump_file)
2308 {
2309 fprintf (dump_file, "Semantic equality hit:%s->%s\n",
2310 source->name (), alias->name ());
2311 fprintf (dump_file, "Assembler symbol names:%s->%s\n",
2312 source->asm_name (), alias->asm_name ());
2313 }
2314
2315 if (dump_file && (dump_flags & TDF_DETAILS))
2316 {
2317 source->dump_to_file (dump_file);
2318 alias->dump_to_file (dump_file);
2319 }
2320
2321 source->merge (alias);
2322 }
2323 }
2324 }
2325
2326 /* Dump function prints all class members to a FILE with an INDENT. */
2327
2328 void
2329 congruence_class::dump (FILE *file, unsigned int indent) const
2330 {
2331 FPRINTF_SPACES (file, indent, "class with id: %u, hash: %u, items: %u\n",
2332 id, members[0]->get_hash (), members.length ());
2333
2334 FPUTS_SPACES (file, indent + 2, "");
2335 for (unsigned i = 0; i < members.length (); i++)
2336 fprintf (file, "%s(%p/%u) ", members[i]->asm_name (), (void *) members[i]->decl,
2337 members[i]->node->order);
2338
2339 fprintf (file, "\n");
2340 }
2341
2342 /* Returns true if there's a member that is used from another group. */
2343
2344 bool
2345 congruence_class::is_class_used (void)
2346 {
2347 for (unsigned int i = 0; i < members.length (); i++)
2348 if (members[i]->usages.length ())
2349 return true;
2350
2351 return false;
2352 }
2353
2354 /* Initialization and computation of symtab node hash, there data
2355 are propagated later on. */
2356
2357 static sem_item_optimizer *optimizer = NULL;
2358
2359 /* Generate pass summary for IPA ICF pass. */
2360
2361 static void
2362 ipa_icf_generate_summary (void)
2363 {
2364 if (!optimizer)
2365 optimizer = new sem_item_optimizer ();
2366
2367 optimizer->parse_funcs_and_vars ();
2368 }
2369
2370 /* Write pass summary for IPA ICF pass. */
2371
2372 static void
2373 ipa_icf_write_summary (void)
2374 {
2375 gcc_assert (optimizer);
2376
2377 optimizer->write_summary ();
2378 }
2379
2380 /* Read pass summary for IPA ICF pass. */
2381
2382 static void
2383 ipa_icf_read_summary (void)
2384 {
2385 if (!optimizer)
2386 optimizer = new sem_item_optimizer ();
2387
2388 optimizer->read_summary ();
2389 optimizer->register_hooks ();
2390 }
2391
2392 /* Semantic equality exection function. */
2393
2394 static unsigned int
2395 ipa_icf_driver (void)
2396 {
2397 gcc_assert (optimizer);
2398
2399 optimizer->execute ();
2400 optimizer->unregister_hooks ();
2401
2402 delete optimizer;
2403 optimizer = NULL;
2404
2405 return 0;
2406 }
2407
2408 const pass_data pass_data_ipa_icf =
2409 {
2410 IPA_PASS, /* type */
2411 "icf", /* name */
2412 OPTGROUP_IPA, /* optinfo_flags */
2413 TV_IPA_ICF, /* tv_id */
2414 0, /* properties_required */
2415 0, /* properties_provided */
2416 0, /* properties_destroyed */
2417 0, /* todo_flags_start */
2418 0, /* todo_flags_finish */
2419 };
2420
2421 class pass_ipa_icf : public ipa_opt_pass_d
2422 {
2423 public:
2424 pass_ipa_icf (gcc::context *ctxt)
2425 : ipa_opt_pass_d (pass_data_ipa_icf, ctxt,
2426 ipa_icf_generate_summary, /* generate_summary */
2427 ipa_icf_write_summary, /* write_summary */
2428 ipa_icf_read_summary, /* read_summary */
2429 NULL, /*
2430 write_optimization_summary */
2431 NULL, /*
2432 read_optimization_summary */
2433 NULL, /* stmt_fixup */
2434 0, /* function_transform_todo_flags_start */
2435 NULL, /* function_transform */
2436 NULL) /* variable_transform */
2437 {}
2438
2439 /* opt_pass methods: */
2440 virtual bool gate (function *)
2441 {
2442 return flag_ipa_icf_variables || flag_ipa_icf_functions;
2443 }
2444
2445 virtual unsigned int execute (function *)
2446 {
2447 return ipa_icf_driver();
2448 }
2449 }; // class pass_ipa_icf
2450
2451 } // ipa_icf namespace
2452
2453 ipa_opt_pass_d *
2454 make_pass_ipa_icf (gcc::context *ctxt)
2455 {
2456 return new ipa_icf::pass_ipa_icf (ctxt);
2457 }