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