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