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