Backport from GCC mainline.
[platform/upstream/linaro-gcc.git] / gcc / ipa-icf.c
1 /* Interprocedural Identical Code Folding pass
2    Copyright (C) 2014-2016 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 #define INCLUDE_LIST
56 #include "system.h"
57 #include "coretypes.h"
58 #include "backend.h"
59 #include "target.h"
60 #include "rtl.h"
61 #include "tree.h"
62 #include "gimple.h"
63 #include "alloc-pool.h"
64 #include "tree-pass.h"
65 #include "ssa.h"
66 #include "cgraph.h"
67 #include "coverage.h"
68 #include "gimple-pretty-print.h"
69 #include "data-streamer.h"
70 #include "fold-const.h"
71 #include "calls.h"
72 #include "varasm.h"
73 #include "gimple-iterator.h"
74 #include "tree-cfg.h"
75 #include "symbol-summary.h"
76 #include "ipa-prop.h"
77 #include "ipa-inline.h"
78 #include "except.h"
79 #include "attribs.h"
80 #include "print-tree.h"
81 #include "ipa-utils.h"
82 #include "ipa-icf-gimple.h"
83 #include "ipa-icf.h"
84 #include "stor-layout.h"
85 #include "dbgcnt.h"
86
87 using namespace ipa_icf_gimple;
88
89 namespace ipa_icf {
90
91 /* Initialization and computation of symtab node hash, there data
92    are propagated later on.  */
93
94 static sem_item_optimizer *optimizer = NULL;
95
96 /* Constructor.  */
97
98 symbol_compare_collection::symbol_compare_collection (symtab_node *node)
99 {
100   m_references.create (0);
101   m_interposables.create (0);
102
103   ipa_ref *ref;
104
105   if (is_a <varpool_node *> (node) && DECL_VIRTUAL_P (node->decl))
106     return;
107
108   for (unsigned i = 0; node->iterate_reference (i, ref); i++)
109     {
110       if (ref->address_matters_p ())
111         m_references.safe_push (ref->referred);
112
113       if (ref->referred->get_availability () <= AVAIL_INTERPOSABLE)
114         {
115           if (ref->address_matters_p ())
116             m_references.safe_push (ref->referred);
117           else
118             m_interposables.safe_push (ref->referred);
119         }
120     }
121
122   if (is_a <cgraph_node *> (node))
123     {
124       cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
125
126       for (cgraph_edge *e = cnode->callees; e; e = e->next_callee)
127         if (e->callee->get_availability () <= AVAIL_INTERPOSABLE)
128           m_interposables.safe_push (e->callee);
129     }
130 }
131
132 /* Constructor for key value pair, where _ITEM is key and _INDEX is a target.  */
133
134 sem_usage_pair::sem_usage_pair (sem_item *_item, unsigned int _index)
135 : item (_item), index (_index)
136 {
137 }
138
139 sem_item::sem_item (sem_item_type _type, bitmap_obstack *stack)
140 : type (_type), m_hash (-1), m_hash_set (false)
141 {
142   setup (stack);
143 }
144
145 sem_item::sem_item (sem_item_type _type, symtab_node *_node,
146                     bitmap_obstack *stack)
147 : type (_type), node (_node), m_hash (-1), m_hash_set (false)
148 {
149   decl = node->decl;
150   setup (stack);
151 }
152
153 /* Add reference to a semantic TARGET.  */
154
155 void
156 sem_item::add_reference (sem_item *target)
157 {
158   refs.safe_push (target);
159   unsigned index = refs.length ();
160   target->usages.safe_push (new sem_usage_pair(this, index));
161   bitmap_set_bit (target->usage_index_bitmap, index);
162   refs_set.add (target->node);
163 }
164
165 /* Initialize internal data structures. Bitmap STACK is used for
166    bitmap memory allocation process.  */
167
168 void
169 sem_item::setup (bitmap_obstack *stack)
170 {
171   gcc_checking_assert (node);
172
173   refs.create (0);
174   tree_refs.create (0);
175   usages.create (0);
176   usage_index_bitmap = BITMAP_ALLOC (stack);
177 }
178
179 sem_item::~sem_item ()
180 {
181   for (unsigned i = 0; i < usages.length (); i++)
182     delete usages[i];
183
184   refs.release ();
185   tree_refs.release ();
186   usages.release ();
187
188   BITMAP_FREE (usage_index_bitmap);
189 }
190
191 /* Dump function for debugging purpose.  */
192
193 DEBUG_FUNCTION void
194 sem_item::dump (void)
195 {
196   if (dump_file)
197     {
198       fprintf (dump_file, "[%s] %s (%u) (tree:%p)\n", type == FUNC ? "func" : "var",
199                node->name(), node->order, (void *) node->decl);
200       fprintf (dump_file, "  hash: %u\n", get_hash ());
201       fprintf (dump_file, "  references: ");
202
203       for (unsigned i = 0; i < refs.length (); i++)
204         fprintf (dump_file, "%s%s ", refs[i]->node->name (),
205                  i < refs.length() - 1 ? "," : "");
206
207       fprintf (dump_file, "\n");
208     }
209 }
210
211 /* Return true if target supports alias symbols.  */
212
213 bool
214 sem_item::target_supports_symbol_aliases_p (void)
215 {
216 #if !defined (ASM_OUTPUT_DEF) || (!defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL))
217   return false;
218 #else
219   return true;
220 #endif
221 }
222
223 void sem_item::set_hash (hashval_t hash)
224 {
225   m_hash = hash;
226   m_hash_set = true;
227 }
228
229 /* Semantic function constructor that uses STACK as bitmap memory stack.  */
230
231 sem_function::sem_function (bitmap_obstack *stack)
232 : sem_item (FUNC, stack), m_checker (NULL), m_compared_func (NULL)
233 {
234   bb_sizes.create (0);
235   bb_sorted.create (0);
236 }
237
238 sem_function::sem_function (cgraph_node *node, bitmap_obstack *stack)
239 : sem_item (FUNC, node, stack), m_checker (NULL), m_compared_func (NULL)
240 {
241   bb_sizes.create (0);
242   bb_sorted.create (0);
243 }
244
245 sem_function::~sem_function ()
246 {
247   for (unsigned i = 0; i < bb_sorted.length (); i++)
248     delete (bb_sorted[i]);
249
250   bb_sizes.release ();
251   bb_sorted.release ();
252 }
253
254 /* Calculates hash value based on a BASIC_BLOCK.  */
255
256 hashval_t
257 sem_function::get_bb_hash (const sem_bb *basic_block)
258 {
259   inchash::hash hstate;
260
261   hstate.add_int (basic_block->nondbg_stmt_count);
262   hstate.add_int (basic_block->edge_count);
263
264   return hstate.end ();
265 }
266
267 /* References independent hash function.  */
268
269 hashval_t
270 sem_function::get_hash (void)
271 {
272   if (!m_hash_set)
273     {
274       inchash::hash hstate;
275       hstate.add_int (177454); /* Random number for function type.  */
276
277       hstate.add_int (arg_count);
278       hstate.add_int (cfg_checksum);
279       hstate.add_int (gcode_hash);
280
281       for (unsigned i = 0; i < bb_sorted.length (); i++)
282         hstate.merge_hash (get_bb_hash (bb_sorted[i]));
283
284       for (unsigned i = 0; i < bb_sizes.length (); i++)
285         hstate.add_int (bb_sizes[i]);
286
287       /* Add common features of declaration itself.  */
288       if (DECL_FUNCTION_SPECIFIC_TARGET (decl))
289         hstate.add_wide_int
290          (cl_target_option_hash
291            (TREE_TARGET_OPTION (DECL_FUNCTION_SPECIFIC_TARGET (decl))));
292       if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))
293          (cl_optimization_hash
294            (TREE_OPTIMIZATION (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))));
295       hstate.add_flag (DECL_CXX_CONSTRUCTOR_P (decl));
296       hstate.add_flag (DECL_CXX_DESTRUCTOR_P (decl));
297
298       set_hash (hstate.end ());
299     }
300
301   return m_hash;
302 }
303
304 /* Return ture if A1 and A2 represent equivalent function attribute lists.
305    Based on comp_type_attributes.  */
306
307 bool
308 sem_item::compare_attributes (const_tree a1, const_tree a2)
309 {
310   const_tree a;
311   if (a1 == a2)
312     return true;
313   for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
314     {
315       const struct attribute_spec *as;
316       const_tree attr;
317
318       as = lookup_attribute_spec (get_attribute_name (a));
319       /* TODO: We can introduce as->affects_decl_identity
320          and as->affects_decl_reference_identity if attribute mismatch
321          gets a common reason to give up on merging.  It may not be worth
322          the effort.
323          For example returns_nonnull affects only references, while
324          optimize attribute can be ignored because it is already lowered
325          into flags representation and compared separately.  */
326       if (!as)
327         continue;
328
329       attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
330       if (!attr || !attribute_value_equal (a, attr))
331         break;
332     }
333   if (!a)
334     {
335       for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
336         {
337           const struct attribute_spec *as;
338
339           as = lookup_attribute_spec (get_attribute_name (a));
340           if (!as)
341             continue;
342
343           if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
344             break;
345           /* We don't need to compare trees again, as we did this
346              already in first loop.  */
347         }
348       if (!a)
349         return true;
350     }
351   /* TODO: As in comp_type_attributes we may want to introduce target hook.  */
352   return false;
353 }
354
355 /* Compare properties of symbols N1 and N2 that does not affect semantics of
356    symbol itself but affects semantics of its references from USED_BY (which
357    may be NULL if it is unknown).  If comparsion is false, symbols
358    can still be merged but any symbols referring them can't.
359
360    If ADDRESS is true, do extra checking needed for IPA_REF_ADDR.
361
362    TODO: We can also split attributes to those that determine codegen of
363    a function body/variable constructor itself and those that are used when
364    referring to it.  */
365
366 bool
367 sem_item::compare_referenced_symbol_properties (symtab_node *used_by,
368                                                 symtab_node *n1,
369                                                 symtab_node *n2,
370                                                 bool address)
371 {
372   if (is_a <cgraph_node *> (n1))
373     {
374       /* Inline properties matters: we do now want to merge uses of inline
375          function to uses of normal function because inline hint would be lost.
376          We however can merge inline function to noinline because the alias
377          will keep its DECL_DECLARED_INLINE flag.
378
379          Also ignore inline flag when optimizing for size or when function
380          is known to not be inlinable.
381
382          TODO: the optimize_size checks can also be assumed to be true if
383          unit has no !optimize_size functions. */
384
385       if ((!used_by || address || !is_a <cgraph_node *> (used_by)
386            || !opt_for_fn (used_by->decl, optimize_size))
387           && !opt_for_fn (n1->decl, optimize_size)
388           && n1->get_availability () > AVAIL_INTERPOSABLE
389           && (!DECL_UNINLINABLE (n1->decl) || !DECL_UNINLINABLE (n2->decl)))
390         {
391           if (DECL_DISREGARD_INLINE_LIMITS (n1->decl)
392               != DECL_DISREGARD_INLINE_LIMITS (n2->decl))
393             return return_false_with_msg
394                      ("DECL_DISREGARD_INLINE_LIMITS are different");
395
396           if (DECL_DECLARED_INLINE_P (n1->decl)
397               != DECL_DECLARED_INLINE_P (n2->decl))
398             return return_false_with_msg ("inline attributes are different");
399         }
400
401       if (DECL_IS_OPERATOR_NEW (n1->decl)
402           != DECL_IS_OPERATOR_NEW (n2->decl))
403         return return_false_with_msg ("operator new flags are different");
404     }
405
406   /* Merging two definitions with a reference to equivalent vtables, but
407      belonging to a different type may result in ipa-polymorphic-call analysis
408      giving a wrong answer about the dynamic type of instance.  */
409   if (is_a <varpool_node *> (n1))
410     {
411       if ((DECL_VIRTUAL_P (n1->decl) || DECL_VIRTUAL_P (n2->decl))
412           && (DECL_VIRTUAL_P (n1->decl) != DECL_VIRTUAL_P (n2->decl)
413               || !types_must_be_same_for_odr (DECL_CONTEXT (n1->decl),
414                                               DECL_CONTEXT (n2->decl)))
415           && (!used_by || !is_a <cgraph_node *> (used_by) || address
416               || opt_for_fn (used_by->decl, flag_devirtualize)))
417         return return_false_with_msg
418                  ("references to virtual tables can not be merged");
419
420       if (address && DECL_ALIGN (n1->decl) != DECL_ALIGN (n2->decl))
421         return return_false_with_msg ("alignment mismatch");
422
423       /* For functions we compare attributes in equals_wpa, because we do
424          not know what attributes may cause codegen differences, but for
425          variables just compare attributes for references - the codegen
426          for constructors is affected only by those attributes that we lower
427          to explicit representation (such as DECL_ALIGN or DECL_SECTION).  */
428       if (!compare_attributes (DECL_ATTRIBUTES (n1->decl),
429                                DECL_ATTRIBUTES (n2->decl)))
430         return return_false_with_msg ("different var decl attributes");
431       if (comp_type_attributes (TREE_TYPE (n1->decl),
432                                 TREE_TYPE (n2->decl)) != 1)
433         return return_false_with_msg ("different var type attributes");
434     }
435
436   /* When matching virtual tables, be sure to also match information
437      relevant for polymorphic call analysis.  */
438   if (used_by && is_a <varpool_node *> (used_by)
439       && DECL_VIRTUAL_P (used_by->decl))
440     {
441       if (DECL_VIRTUAL_P (n1->decl) != DECL_VIRTUAL_P (n2->decl))
442         return return_false_with_msg ("virtual flag mismatch");
443       if (DECL_VIRTUAL_P (n1->decl) && is_a <cgraph_node *> (n1)
444           && (DECL_FINAL_P (n1->decl) != DECL_FINAL_P (n2->decl)))
445         return return_false_with_msg ("final flag mismatch");
446     }
447   return true;
448 }
449
450 /* Hash properties that are compared by compare_referenced_symbol_properties. */
451
452 void
453 sem_item::hash_referenced_symbol_properties (symtab_node *ref,
454                                              inchash::hash &hstate,
455                                              bool address)
456 {
457   if (is_a <cgraph_node *> (ref))
458     {
459       if ((type != FUNC || address || !opt_for_fn (decl, optimize_size))
460           && !opt_for_fn (ref->decl, optimize_size)
461           && !DECL_UNINLINABLE (ref->decl))
462         {
463           hstate.add_flag (DECL_DISREGARD_INLINE_LIMITS (ref->decl));
464           hstate.add_flag (DECL_DECLARED_INLINE_P (ref->decl));
465         }
466       hstate.add_flag (DECL_IS_OPERATOR_NEW (ref->decl));
467     }
468   else if (is_a <varpool_node *> (ref))
469     {
470       hstate.add_flag (DECL_VIRTUAL_P (ref->decl));
471       if (address)
472         hstate.add_int (DECL_ALIGN (ref->decl));
473     }
474 }
475
476
477 /* For a given symbol table nodes N1 and N2, we check that FUNCTION_DECLs
478    point to a same function. Comparison can be skipped if IGNORED_NODES
479    contains these nodes.  ADDRESS indicate if address is taken.  */
480
481 bool
482 sem_item::compare_symbol_references (
483     hash_map <symtab_node *, sem_item *> &ignored_nodes,
484     symtab_node *n1, symtab_node *n2, bool address)
485 {
486   enum availability avail1, avail2;
487
488   if (n1 == n2)
489     return true;
490
491   /* Never match variable and function.  */
492   if (is_a <varpool_node *> (n1) != is_a <varpool_node *> (n2))
493     return false;
494
495   if (!compare_referenced_symbol_properties (node, n1, n2, address))
496     return false;
497   if (address && n1->equal_address_to (n2) == 1)
498     return true;
499   if (!address && n1->semantically_equivalent_p (n2))
500     return true;
501
502   n1 = n1->ultimate_alias_target (&avail1);
503   n2 = n2->ultimate_alias_target (&avail2);
504
505   if (avail1 > AVAIL_INTERPOSABLE && ignored_nodes.get (n1)
506       && avail2 > AVAIL_INTERPOSABLE && ignored_nodes.get (n2))
507     return true;
508
509   return return_false_with_msg ("different references");
510 }
511
512 /* If cgraph edges E1 and E2 are indirect calls, verify that
513    ECF flags are the same.  */
514
515 bool sem_function::compare_edge_flags (cgraph_edge *e1, cgraph_edge *e2)
516 {
517   if (e1->indirect_info && e2->indirect_info)
518     {
519       int e1_flags = e1->indirect_info->ecf_flags;
520       int e2_flags = e2->indirect_info->ecf_flags;
521
522       if (e1_flags != e2_flags)
523         return return_false_with_msg ("ICF flags are different");
524     }
525   else if (e1->indirect_info || e2->indirect_info)
526     return false;
527
528   return true;
529 }
530
531 /* Return true if parameter I may be used.  */
532
533 bool
534 sem_function::param_used_p (unsigned int i)
535 {
536   if (ipa_node_params_sum == NULL)
537     return true;
538
539   struct ipa_node_params *parms_info = IPA_NODE_REF (get_node ());
540
541   if (parms_info->descriptors.is_empty ()
542       || parms_info->descriptors.length () <= i)
543      return true;
544
545   return ipa_is_param_used (IPA_NODE_REF (get_node ()), i);
546 }
547
548 /* Perform additional check needed to match types function parameters that are
549    used.  Unlike for normal decls it matters if type is TYPE_RESTRICT and we
550    make an assumption that REFERENCE_TYPE parameters are always non-NULL.  */
551
552 bool
553 sem_function::compatible_parm_types_p (tree parm1, tree parm2)
554 {
555   /* Be sure that parameters are TBAA compatible.  */
556   if (!func_checker::compatible_types_p (parm1, parm2))
557     return return_false_with_msg ("parameter type is not compatible");
558
559   if (POINTER_TYPE_P (parm1)
560       && (TYPE_RESTRICT (parm1) != TYPE_RESTRICT (parm2)))
561     return return_false_with_msg ("argument restrict flag mismatch");
562
563   /* nonnull_arg_p implies non-zero range to REFERENCE types.  */
564   if (POINTER_TYPE_P (parm1)
565       && TREE_CODE (parm1) != TREE_CODE (parm2)
566       && opt_for_fn (decl, flag_delete_null_pointer_checks))
567     return return_false_with_msg ("pointer wrt reference mismatch");
568
569   return true;
570 }
571
572 /* Fast equality function based on knowledge known in WPA.  */
573
574 bool
575 sem_function::equals_wpa (sem_item *item,
576                           hash_map <symtab_node *, sem_item *> &ignored_nodes)
577 {
578   gcc_assert (item->type == FUNC);
579   cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
580   cgraph_node *cnode2 = dyn_cast <cgraph_node *> (item->node);
581
582   m_compared_func = static_cast<sem_function *> (item);
583
584   if (cnode->thunk.thunk_p != cnode2->thunk.thunk_p)
585     return return_false_with_msg ("thunk_p mismatch");
586
587   if (cnode->thunk.thunk_p)
588     {
589       if (cnode->thunk.fixed_offset != cnode2->thunk.fixed_offset)
590         return return_false_with_msg ("thunk fixed_offset mismatch");
591       if (cnode->thunk.virtual_value != cnode2->thunk.virtual_value)
592         return return_false_with_msg ("thunk virtual_value mismatch");
593       if (cnode->thunk.this_adjusting != cnode2->thunk.this_adjusting)
594         return return_false_with_msg ("thunk this_adjusting mismatch");
595       if (cnode->thunk.virtual_offset_p != cnode2->thunk.virtual_offset_p)
596         return return_false_with_msg ("thunk virtual_offset_p mismatch");
597       if (cnode->thunk.add_pointer_bounds_args
598           != cnode2->thunk.add_pointer_bounds_args)
599         return return_false_with_msg ("thunk add_pointer_bounds_args mismatch");
600     }
601
602   /* Compare special function DECL attributes.  */
603   if (DECL_FUNCTION_PERSONALITY (decl)
604       != DECL_FUNCTION_PERSONALITY (item->decl))
605     return return_false_with_msg ("function personalities are different");
606
607   if (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl)
608        != DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (item->decl))
609     return return_false_with_msg ("intrument function entry exit "
610                                   "attributes are different");
611
612   if (DECL_NO_LIMIT_STACK (decl) != DECL_NO_LIMIT_STACK (item->decl))
613     return return_false_with_msg ("no stack limit attributes are different");
614
615   if (DECL_CXX_CONSTRUCTOR_P (decl) != DECL_CXX_CONSTRUCTOR_P (item->decl))
616     return return_false_with_msg ("DECL_CXX_CONSTRUCTOR mismatch");
617
618   if (DECL_CXX_DESTRUCTOR_P (decl) != DECL_CXX_DESTRUCTOR_P (item->decl))
619     return return_false_with_msg ("DECL_CXX_DESTRUCTOR mismatch");
620
621   /* TODO: pure/const flags mostly matters only for references, except for
622      the fact that codegen takes LOOPING flag as a hint that loops are
623      finite.  We may arrange the code to always pick leader that has least
624      specified flags and then this can go into comparing symbol properties.  */
625   if (flags_from_decl_or_type (decl) != flags_from_decl_or_type (item->decl))
626     return return_false_with_msg ("decl_or_type flags are different");
627
628   /* Do not match polymorphic constructors of different types.  They calls
629      type memory location for ipa-polymorphic-call and we do not want
630      it to get confused by wrong type.  */
631   if (DECL_CXX_CONSTRUCTOR_P (decl)
632       && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
633     {
634       if (TREE_CODE (TREE_TYPE (item->decl)) != METHOD_TYPE)
635         return return_false_with_msg ("DECL_CXX_CONSTURCTOR type mismatch");
636       else if (!func_checker::compatible_polymorphic_types_p
637                  (TYPE_METHOD_BASETYPE (TREE_TYPE (decl)),
638                   TYPE_METHOD_BASETYPE (TREE_TYPE (item->decl)), false))
639         return return_false_with_msg ("ctor polymorphic type mismatch");
640     }
641
642   /* Checking function TARGET and OPTIMIZATION flags.  */
643   cl_target_option *tar1 = target_opts_for_fn (decl);
644   cl_target_option *tar2 = target_opts_for_fn (item->decl);
645
646   if (tar1 != tar2 && !cl_target_option_eq (tar1, tar2))
647     {
648       if (dump_file && (dump_flags & TDF_DETAILS))
649         {
650           fprintf (dump_file, "target flags difference");
651           cl_target_option_print_diff (dump_file, 2, tar1, tar2);
652         }
653
654       return return_false_with_msg ("Target flags are different");
655     }
656
657   cl_optimization *opt1 = opts_for_fn (decl);
658   cl_optimization *opt2 = opts_for_fn (item->decl);
659
660   if (opt1 != opt2 && memcmp (opt1, opt2, sizeof(cl_optimization)))
661     {
662       if (dump_file && (dump_flags & TDF_DETAILS))
663         {
664           fprintf (dump_file, "optimization flags difference");
665           cl_optimization_print_diff (dump_file, 2, opt1, opt2);
666         }
667
668       return return_false_with_msg ("optimization flags are different");
669     }
670
671   /* Result type checking.  */
672   if (!func_checker::compatible_types_p
673          (TREE_TYPE (TREE_TYPE (decl)),
674           TREE_TYPE (TREE_TYPE (m_compared_func->decl))))
675     return return_false_with_msg ("result types are different");
676
677   /* Checking types of arguments.  */
678   tree list1 = TYPE_ARG_TYPES (TREE_TYPE (decl)),
679        list2 = TYPE_ARG_TYPES (TREE_TYPE (m_compared_func->decl));
680   for (unsigned i = 0; list1 && list2;
681        list1 = TREE_CHAIN (list1), list2 = TREE_CHAIN (list2), i++)
682     {
683       tree parm1 = TREE_VALUE (list1);
684       tree parm2 = TREE_VALUE (list2);
685
686       /* This guard is here for function pointer with attributes (pr59927.c).  */
687       if (!parm1 || !parm2)
688         return return_false_with_msg ("NULL argument type");
689
690       /* Verify that types are compatible to ensure that both functions
691          have same calling conventions.  */
692       if (!types_compatible_p (parm1, parm2))
693         return return_false_with_msg ("parameter types are not compatible");
694
695       if (!param_used_p (i))
696         continue;
697
698       /* Perform additional checks for used parameters.  */
699       if (!compatible_parm_types_p (parm1, parm2))
700         return false;
701     }
702
703   if (list1 || list2)
704     return return_false_with_msg ("Mismatched number of parameters");
705
706   if (node->num_references () != item->node->num_references ())
707     return return_false_with_msg ("different number of references");
708
709   /* Checking function attributes.
710      This is quadratic in number of attributes  */
711   if (comp_type_attributes (TREE_TYPE (decl),
712                             TREE_TYPE (item->decl)) != 1)
713     return return_false_with_msg ("different type attributes");
714   if (!compare_attributes (DECL_ATTRIBUTES (decl),
715                            DECL_ATTRIBUTES (item->decl)))
716     return return_false_with_msg ("different decl attributes");
717
718   /* The type of THIS pointer type memory location for
719      ipa-polymorphic-call-analysis.  */
720   if (opt_for_fn (decl, flag_devirtualize)
721       && (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
722           || TREE_CODE (TREE_TYPE (item->decl)) == METHOD_TYPE)
723       && param_used_p (0)
724       && compare_polymorphic_p ())
725     {
726       if (TREE_CODE (TREE_TYPE (decl)) != TREE_CODE (TREE_TYPE (item->decl)))
727         return return_false_with_msg ("METHOD_TYPE and FUNCTION_TYPE mismatch");
728       if (!func_checker::compatible_polymorphic_types_p
729            (TYPE_METHOD_BASETYPE (TREE_TYPE (decl)),
730             TYPE_METHOD_BASETYPE (TREE_TYPE (item->decl)), false))
731         return return_false_with_msg ("THIS pointer ODR type mismatch");
732     }
733
734   ipa_ref *ref = NULL, *ref2 = NULL;
735   for (unsigned i = 0; node->iterate_reference (i, ref); i++)
736     {
737       item->node->iterate_reference (i, ref2);
738
739       if (ref->use != ref2->use)
740         return return_false_with_msg ("reference use mismatch");
741
742       if (!compare_symbol_references (ignored_nodes, ref->referred,
743                                       ref2->referred,
744                                       ref->address_matters_p ()))
745         return false;
746     }
747
748   cgraph_edge *e1 = dyn_cast <cgraph_node *> (node)->callees;
749   cgraph_edge *e2 = dyn_cast <cgraph_node *> (item->node)->callees;
750
751   while (e1 && e2)
752     {
753       if (!compare_symbol_references (ignored_nodes, e1->callee,
754                                       e2->callee, false))
755         return false;
756       if (!compare_edge_flags (e1, e2))
757         return false;
758
759       e1 = e1->next_callee;
760       e2 = e2->next_callee;
761     }
762
763   if (e1 || e2)
764     return return_false_with_msg ("different number of calls");
765
766   e1 = dyn_cast <cgraph_node *> (node)->indirect_calls;
767   e2 = dyn_cast <cgraph_node *> (item->node)->indirect_calls;
768
769   while (e1 && e2)
770     {
771       if (!compare_edge_flags (e1, e2))
772         return false;
773
774       e1 = e1->next_callee;
775       e2 = e2->next_callee;
776     }
777
778   if (e1 || e2)
779     return return_false_with_msg ("different number of indirect calls");
780
781   return true;
782 }
783
784 /* Update hash by address sensitive references. We iterate over all
785    sensitive references (address_matters_p) and we hash ultime alias
786    target of these nodes, which can improve a semantic item hash.
787
788    Also hash in referenced symbols properties.  This can be done at any time
789    (as the properties should not change), but it is convenient to do it here
790    while we walk the references anyway.  */
791
792 void
793 sem_item::update_hash_by_addr_refs (hash_map <symtab_node *,
794                                     sem_item *> &m_symtab_node_map)
795 {
796   ipa_ref* ref;
797   inchash::hash hstate (get_hash ());
798
799   for (unsigned i = 0; node->iterate_reference (i, ref); i++)
800     {
801       hstate.add_int (ref->use);
802       hash_referenced_symbol_properties (ref->referred, hstate,
803                                          ref->use == IPA_REF_ADDR);
804       if (ref->address_matters_p () || !m_symtab_node_map.get (ref->referred))
805         hstate.add_int (ref->referred->ultimate_alias_target ()->order);
806     }
807
808   if (is_a <cgraph_node *> (node))
809     {
810       for (cgraph_edge *e = dyn_cast <cgraph_node *> (node)->callers; e;
811            e = e->next_caller)
812         {
813           sem_item **result = m_symtab_node_map.get (e->callee);
814           hash_referenced_symbol_properties (e->callee, hstate, false);
815           if (!result)
816             hstate.add_int (e->callee->ultimate_alias_target ()->order);
817         }
818     }
819
820   set_hash (hstate.end ());
821 }
822
823 /* Update hash by computed local hash values taken from different
824    semantic items.
825    TODO: stronger SCC based hashing would be desirable here.  */
826
827 void
828 sem_item::update_hash_by_local_refs (hash_map <symtab_node *,
829                                      sem_item *> &m_symtab_node_map)
830 {
831   ipa_ref* ref;
832   inchash::hash state (get_hash ());
833
834   for (unsigned j = 0; node->iterate_reference (j, ref); j++)
835     {
836       sem_item **result = m_symtab_node_map.get (ref->referring);
837       if (result)
838         state.merge_hash ((*result)->get_hash ());
839     }
840
841   if (type == FUNC)
842     {
843       for (cgraph_edge *e = dyn_cast <cgraph_node *> (node)->callees; e;
844            e = e->next_callee)
845         {
846           sem_item **result = m_symtab_node_map.get (e->caller);
847           if (result)
848             state.merge_hash ((*result)->get_hash ());
849         }
850     }
851
852   global_hash = state.end ();
853 }
854
855 /* Returns true if the item equals to ITEM given as argument.  */
856
857 bool
858 sem_function::equals (sem_item *item,
859                       hash_map <symtab_node *, sem_item *> &)
860 {
861   gcc_assert (item->type == FUNC);
862   bool eq = equals_private (item);
863
864   if (m_checker != NULL)
865     {
866       delete m_checker;
867       m_checker = NULL;
868     }
869
870   if (dump_file && (dump_flags & TDF_DETAILS))
871     fprintf (dump_file,
872              "Equals called for:%s:%s (%u:%u) (%s:%s) with result: %s\n\n",
873              xstrdup_for_dump (node->name()),
874              xstrdup_for_dump (item->node->name ()),
875              node->order,
876              item->node->order,
877              xstrdup_for_dump (node->asm_name ()),
878              xstrdup_for_dump (item->node->asm_name ()),
879              eq ? "true" : "false");
880
881   return eq;
882 }
883
884 /* Processes function equality comparison.  */
885
886 bool
887 sem_function::equals_private (sem_item *item)
888 {
889   if (item->type != FUNC)
890     return false;
891
892   basic_block bb1, bb2;
893   edge e1, e2;
894   edge_iterator ei1, ei2;
895   bool result = true;
896   tree arg1, arg2;
897
898   m_compared_func = static_cast<sem_function *> (item);
899
900   gcc_assert (decl != item->decl);
901
902   if (bb_sorted.length () != m_compared_func->bb_sorted.length ()
903       || edge_count != m_compared_func->edge_count
904       || cfg_checksum != m_compared_func->cfg_checksum)
905     return return_false ();
906
907   m_checker = new func_checker (decl, m_compared_func->decl,
908                                 compare_polymorphic_p (),
909                                 false,
910                                 &refs_set,
911                                 &m_compared_func->refs_set);
912   arg1 = DECL_ARGUMENTS (decl);
913   arg2 = DECL_ARGUMENTS (m_compared_func->decl);
914   for (unsigned i = 0;
915        arg1 && arg2; arg1 = DECL_CHAIN (arg1), arg2 = DECL_CHAIN (arg2), i++)
916     {
917       if (!types_compatible_p (TREE_TYPE (arg1), TREE_TYPE (arg2)))
918         return return_false_with_msg ("argument types are not compatible");
919       if (!param_used_p (i))
920         continue;
921       /* Perform additional checks for used parameters.  */
922       if (!compatible_parm_types_p (TREE_TYPE (arg1), TREE_TYPE (arg2)))
923         return false;
924       if (!m_checker->compare_decl (arg1, arg2))
925         return return_false ();
926     }
927   if (arg1 || arg2)
928     return return_false_with_msg ("Mismatched number of arguments");
929
930   if (!dyn_cast <cgraph_node *> (node)->has_gimple_body_p ())
931     return true;
932
933   /* Fill-up label dictionary.  */
934   for (unsigned i = 0; i < bb_sorted.length (); ++i)
935     {
936       m_checker->parse_labels (bb_sorted[i]);
937       m_checker->parse_labels (m_compared_func->bb_sorted[i]);
938     }
939
940   /* Checking all basic blocks.  */
941   for (unsigned i = 0; i < bb_sorted.length (); ++i)
942     if(!m_checker->compare_bb (bb_sorted[i], m_compared_func->bb_sorted[i]))
943       return return_false();
944
945   dump_message ("All BBs are equal\n");
946
947   auto_vec <int> bb_dict;
948
949   /* Basic block edges check.  */
950   for (unsigned i = 0; i < bb_sorted.length (); ++i)
951     {
952       bb1 = bb_sorted[i]->bb;
953       bb2 = m_compared_func->bb_sorted[i]->bb;
954
955       ei2 = ei_start (bb2->preds);
956
957       for (ei1 = ei_start (bb1->preds); ei_cond (ei1, &e1); ei_next (&ei1))
958         {
959           ei_cond (ei2, &e2);
960
961           if (e1->flags != e2->flags)
962             return return_false_with_msg ("flags comparison returns false");
963
964           if (!bb_dict_test (&bb_dict, e1->src->index, e2->src->index))
965             return return_false_with_msg ("edge comparison returns false");
966
967           if (!bb_dict_test (&bb_dict, e1->dest->index, e2->dest->index))
968             return return_false_with_msg ("BB comparison returns false");
969
970           if (!m_checker->compare_edge (e1, e2))
971             return return_false_with_msg ("edge comparison returns false");
972
973           ei_next (&ei2);
974         }
975     }
976
977   /* Basic block PHI nodes comparison.  */
978   for (unsigned i = 0; i < bb_sorted.length (); i++)
979     if (!compare_phi_node (bb_sorted[i]->bb, m_compared_func->bb_sorted[i]->bb))
980       return return_false_with_msg ("PHI node comparison returns false");
981
982   return result;
983 }
984
985 /* Set LOCAL_P of NODE to true if DATA is non-NULL.
986    Helper for call_for_symbol_thunks_and_aliases.  */
987
988 static bool
989 set_local (cgraph_node *node, void *data)
990 {
991   node->local.local = data != NULL;
992   return false;
993 }
994
995 /* TREE_ADDRESSABLE of NODE to true.
996    Helper for call_for_symbol_thunks_and_aliases.  */
997
998 static bool
999 set_addressable (varpool_node *node, void *)
1000 {
1001   TREE_ADDRESSABLE (node->decl) = 1;
1002   return false;
1003 }
1004
1005 /* Clear DECL_RTL of NODE. 
1006    Helper for call_for_symbol_thunks_and_aliases.  */
1007
1008 static bool
1009 clear_decl_rtl (symtab_node *node, void *)
1010 {
1011   SET_DECL_RTL (node->decl, NULL);
1012   return false;
1013 }
1014
1015 /* Redirect all callers of N and its aliases to TO.  Remove aliases if
1016    possible.  Return number of redirections made.  */
1017
1018 static int
1019 redirect_all_callers (cgraph_node *n, cgraph_node *to)
1020 {
1021   int nredirected = 0;
1022   ipa_ref *ref;
1023   cgraph_edge *e = n->callers;
1024
1025   while (e)
1026     {
1027       /* Redirecting thunks to interposable symbols or symbols in other sections
1028          may not be supported by target output code.  Play safe for now and
1029          punt on redirection.  */
1030       if (!e->caller->thunk.thunk_p)
1031         {
1032           struct cgraph_edge *nexte = e->next_caller;
1033           e->redirect_callee (to);
1034           e = nexte;
1035           nredirected++;
1036         }
1037       else
1038         e = e->next_callee;
1039     }
1040   for (unsigned i = 0; n->iterate_direct_aliases (i, ref);)
1041     {
1042       bool removed = false;
1043       cgraph_node *n_alias = dyn_cast <cgraph_node *> (ref->referring);
1044
1045       if ((DECL_COMDAT_GROUP (n->decl)
1046            && (DECL_COMDAT_GROUP (n->decl)
1047                == DECL_COMDAT_GROUP (n_alias->decl)))
1048           || (n_alias->get_availability () > AVAIL_INTERPOSABLE
1049               && n->get_availability () > AVAIL_INTERPOSABLE))
1050         {
1051           nredirected += redirect_all_callers (n_alias, to);
1052           if (n_alias->can_remove_if_no_direct_calls_p ()
1053               && !n_alias->call_for_symbol_and_aliases (cgraph_node::has_thunk_p,
1054                                                         NULL, true)
1055               && !n_alias->has_aliases_p ())
1056             n_alias->remove ();
1057         }
1058       if (!removed)
1059         i++;
1060     }
1061   return nredirected;
1062 }
1063
1064 /* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can
1065    be applied.  */
1066
1067 bool
1068 sem_function::merge (sem_item *alias_item)
1069 {
1070   gcc_assert (alias_item->type == FUNC);
1071
1072   sem_function *alias_func = static_cast<sem_function *> (alias_item);
1073
1074   cgraph_node *original = get_node ();
1075   cgraph_node *local_original = NULL;
1076   cgraph_node *alias = alias_func->get_node ();
1077
1078   bool create_wrapper = false;
1079   bool create_alias = false;
1080   bool redirect_callers = false;
1081   bool remove = false;
1082
1083   bool original_discardable = false;
1084   bool original_discarded = false;
1085
1086   bool original_address_matters = original->address_matters_p ();
1087   bool alias_address_matters = alias->address_matters_p ();
1088
1089   if (DECL_EXTERNAL (alias->decl))
1090     {
1091       if (dump_file)
1092         fprintf (dump_file, "Not unifying; alias is external.\n\n");
1093       return false;
1094     }
1095
1096   if (DECL_NO_INLINE_WARNING_P (original->decl)
1097       != DECL_NO_INLINE_WARNING_P (alias->decl))
1098     {
1099       if (dump_file)
1100         fprintf (dump_file,
1101                  "Not unifying; "
1102                  "DECL_NO_INLINE_WARNING mismatch.\n\n");
1103       return false;
1104     }
1105
1106   /* Do not attempt to mix functions from different user sections;
1107      we do not know what user intends with those.  */
1108   if (((DECL_SECTION_NAME (original->decl) && !original->implicit_section)
1109        || (DECL_SECTION_NAME (alias->decl) && !alias->implicit_section))
1110       && DECL_SECTION_NAME (original->decl) != DECL_SECTION_NAME (alias->decl))
1111     {
1112       if (dump_file)
1113         fprintf (dump_file,
1114                  "Not unifying; "
1115                  "original and alias are in different sections.\n\n");
1116       return false;
1117     }
1118
1119   /* See if original is in a section that can be discarded if the main
1120      symbol is not used.  */
1121
1122   if (original->can_be_discarded_p ())
1123     original_discardable = true;
1124   /* Also consider case where we have resolution info and we know that
1125      original's definition is not going to be used.  In this case we can not
1126      create alias to original.  */
1127   if (node->resolution != LDPR_UNKNOWN
1128       && !decl_binds_to_current_def_p (node->decl))
1129     original_discardable = original_discarded = true;
1130
1131   /* Creating a symtab alias is the optimal way to merge.
1132      It however can not be used in the following cases:
1133
1134      1) if ORIGINAL and ALIAS may be possibly compared for address equality.
1135      2) if ORIGINAL is in a section that may be discarded by linker or if
1136         it is an external functions where we can not create an alias
1137         (ORIGINAL_DISCARDABLE)
1138      3) if target do not support symbol aliases.
1139      4) original and alias lie in different comdat groups.
1140
1141      If we can not produce alias, we will turn ALIAS into WRAPPER of ORIGINAL
1142      and/or redirect all callers from ALIAS to ORIGINAL.  */
1143   if ((original_address_matters && alias_address_matters)
1144       || (original_discardable
1145           && (!DECL_COMDAT_GROUP (alias->decl)
1146               || (DECL_COMDAT_GROUP (alias->decl)
1147                   != DECL_COMDAT_GROUP (original->decl))))
1148       || original_discarded
1149       || !sem_item::target_supports_symbol_aliases_p ()
1150       || DECL_COMDAT_GROUP (alias->decl) != DECL_COMDAT_GROUP (original->decl))
1151     {
1152       /* First see if we can produce wrapper.  */
1153
1154       /* Symbol properties that matter for references must be preserved.
1155          TODO: We can produce wrapper, but we need to produce alias of ORIGINAL
1156          with proper properties.  */
1157       if (!sem_item::compare_referenced_symbol_properties (NULL, original, alias,
1158                                                            alias->address_taken))
1159         {
1160           if (dump_file)
1161             fprintf (dump_file,
1162                      "Wrapper cannot be created because referenced symbol "
1163                      "properties mismatch\n");
1164         }
1165       /* Do not turn function in one comdat group into wrapper to another
1166          comdat group. Other compiler producing the body of the
1167          another comdat group may make opossite decision and with unfortunate
1168          linker choices this may close a loop.  */
1169       else if (DECL_COMDAT_GROUP (original->decl)
1170                && DECL_COMDAT_GROUP (alias->decl)
1171                && (DECL_COMDAT_GROUP (alias->decl)
1172                    != DECL_COMDAT_GROUP (original->decl)))
1173         {
1174           if (dump_file)
1175             fprintf (dump_file,
1176                      "Wrapper cannot be created because of COMDAT\n");
1177         }
1178       else if (DECL_STATIC_CHAIN (alias->decl)
1179                || DECL_STATIC_CHAIN (original->decl))
1180         {
1181           if (dump_file)
1182             fprintf (dump_file,
1183                      "Cannot create wrapper of nested function.\n");
1184         }
1185       /* TODO: We can also deal with variadic functions never calling
1186          VA_START.  */
1187       else if (stdarg_p (TREE_TYPE (alias->decl)))
1188         {
1189           if (dump_file)
1190             fprintf (dump_file,
1191                      "can not create wrapper of stdarg function.\n");
1192         }
1193       else if (inline_summaries
1194                && inline_summaries->get (alias)->self_size <= 2)
1195         {
1196           if (dump_file)
1197             fprintf (dump_file, "Wrapper creation is not "
1198                      "profitable (function is too small).\n");
1199         }
1200       /* If user paid attention to mark function noinline, assume it is
1201          somewhat special and do not try to turn it into a wrapper that can
1202          not be undone by inliner.  */
1203       else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (alias->decl)))
1204         {
1205           if (dump_file)
1206             fprintf (dump_file, "Wrappers are not created for noinline.\n");
1207         }
1208       else
1209         create_wrapper = true;
1210
1211       /* We can redirect local calls in the case both alias and orignal
1212          are not interposable.  */
1213       redirect_callers
1214         = alias->get_availability () > AVAIL_INTERPOSABLE
1215           && original->get_availability () > AVAIL_INTERPOSABLE
1216           && !alias->instrumented_version;
1217       /* TODO: We can redirect, but we need to produce alias of ORIGINAL
1218          with proper properties.  */
1219       if (!sem_item::compare_referenced_symbol_properties (NULL, original, alias,
1220                                                            alias->address_taken))
1221         redirect_callers = false;
1222
1223       if (!redirect_callers && !create_wrapper)
1224         {
1225           if (dump_file)
1226             fprintf (dump_file, "Not unifying; can not redirect callers nor "
1227                      "produce wrapper\n\n");
1228           return false;
1229         }
1230
1231       /* Work out the symbol the wrapper should call.
1232          If ORIGINAL is interposable, we need to call a local alias.
1233          Also produce local alias (if possible) as an optimization.
1234
1235          Local aliases can not be created inside comdat groups because that
1236          prevents inlining.  */
1237       if (!original_discardable && !original->get_comdat_group ())
1238         {
1239           local_original
1240             = dyn_cast <cgraph_node *> (original->noninterposable_alias ());
1241           if (!local_original
1242               && original->get_availability () > AVAIL_INTERPOSABLE)
1243             local_original = original;
1244         }
1245       /* If we can not use local alias, fallback to the original
1246          when possible.  */
1247       else if (original->get_availability () > AVAIL_INTERPOSABLE)
1248         local_original = original;
1249
1250       /* If original is COMDAT local, we can not really redirect calls outside
1251          of its comdat group to it.  */
1252       if (original->comdat_local_p ())
1253         redirect_callers = false;
1254       if (!local_original)
1255         {
1256           if (dump_file)
1257             fprintf (dump_file, "Not unifying; "
1258                      "can not produce local alias.\n\n");
1259           return false;
1260         }
1261
1262       if (!redirect_callers && !create_wrapper)
1263         {
1264           if (dump_file)
1265             fprintf (dump_file, "Not unifying; "
1266                      "can not redirect callers nor produce a wrapper\n\n");
1267           return false;
1268         }
1269       if (!create_wrapper
1270           && !alias->call_for_symbol_and_aliases (cgraph_node::has_thunk_p,
1271                                                   NULL, true)
1272           && !alias->can_remove_if_no_direct_calls_p ())
1273         {
1274           if (dump_file)
1275             fprintf (dump_file, "Not unifying; can not make wrapper and "
1276                      "function has other uses than direct calls\n\n");
1277           return false;
1278         }
1279     }
1280   else
1281     create_alias = true;
1282
1283   if (redirect_callers)
1284     {
1285       int nredirected = redirect_all_callers (alias, local_original);
1286
1287       if (nredirected)
1288         {
1289           alias->icf_merged = true;
1290           local_original->icf_merged = true;
1291
1292           if (dump_file && nredirected)
1293             fprintf (dump_file, "%i local calls have been "
1294                      "redirected.\n", nredirected);
1295         }
1296
1297       /* If all callers was redirected, do not produce wrapper.  */
1298       if (alias->can_remove_if_no_direct_calls_p ()
1299           && !DECL_VIRTUAL_P (alias->decl)
1300           && !alias->has_aliases_p ())
1301         {
1302           create_wrapper = false;
1303           remove = true;
1304         }
1305       gcc_assert (!create_alias);
1306     }
1307   else if (create_alias)
1308     {
1309       alias->icf_merged = true;
1310
1311       /* Remove the function's body.  */
1312       ipa_merge_profiles (original, alias);
1313       alias->release_body (true);
1314       alias->reset ();
1315       /* Notice global symbol possibly produced RTL.  */
1316       ((symtab_node *)alias)->call_for_symbol_and_aliases (clear_decl_rtl,
1317                                                            NULL, true);
1318
1319       /* Create the alias.  */
1320       cgraph_node::create_alias (alias_func->decl, decl);
1321       alias->resolve_alias (original);
1322
1323       original->call_for_symbol_thunks_and_aliases
1324          (set_local, (void *)(size_t) original->local_p (), true);
1325
1326       if (dump_file)
1327         fprintf (dump_file, "Unified; Function alias has been created.\n\n");
1328     }
1329   if (create_wrapper)
1330     {
1331       gcc_assert (!create_alias);
1332       alias->icf_merged = true;
1333       local_original->icf_merged = true;
1334
1335       ipa_merge_profiles (local_original, alias, true);
1336       alias->create_wrapper (local_original);
1337
1338       if (dump_file)
1339         fprintf (dump_file, "Unified; Wrapper has been created.\n\n");
1340     }
1341
1342   /* It's possible that redirection can hit thunks that block
1343      redirection opportunities.  */
1344   gcc_assert (alias->icf_merged || remove || redirect_callers);
1345   original->icf_merged = true;
1346
1347   /* We use merged flag to track cases where COMDAT function is known to be
1348      compatible its callers.  If we merged in non-COMDAT, we need to give up
1349      on this optimization.  */
1350   if (original->merged_comdat && !alias->merged_comdat)
1351     {
1352       if (dump_file)
1353         fprintf (dump_file, "Dropping merged_comdat flag.\n\n");
1354       if (local_original)
1355         local_original->merged_comdat = false;
1356       original->merged_comdat = false;
1357     }
1358
1359   if (remove)
1360     {
1361       ipa_merge_profiles (original, alias);
1362       alias->release_body ();
1363       alias->reset ();
1364       alias->body_removed = true;
1365       alias->icf_merged = true;
1366       if (dump_file)
1367         fprintf (dump_file, "Unified; Function body was removed.\n");
1368     }
1369
1370   return true;
1371 }
1372
1373 /* Semantic item initialization function.  */
1374
1375 void
1376 sem_function::init (void)
1377 {
1378   if (in_lto_p)
1379     get_node ()->get_untransformed_body ();
1380
1381   tree fndecl = node->decl;
1382   function *func = DECL_STRUCT_FUNCTION (fndecl);
1383
1384   gcc_assert (func);
1385   gcc_assert (SSANAMES (func));
1386
1387   ssa_names_size = SSANAMES (func)->length ();
1388   node = node;
1389
1390   decl = fndecl;
1391   region_tree = func->eh->region_tree;
1392
1393   /* iterating all function arguments.  */
1394   arg_count = count_formal_params (fndecl);
1395
1396   edge_count = n_edges_for_fn (func);
1397   cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1398   if (!cnode->thunk.thunk_p)
1399     {
1400       cfg_checksum = coverage_compute_cfg_checksum (func);
1401
1402       inchash::hash hstate;
1403
1404       basic_block bb;
1405       FOR_EACH_BB_FN (bb, func)
1406       {
1407         unsigned nondbg_stmt_count = 0;
1408
1409         edge e;
1410         for (edge_iterator ei = ei_start (bb->preds); ei_cond (ei, &e);
1411              ei_next (&ei))
1412           cfg_checksum = iterative_hash_host_wide_int (e->flags,
1413                          cfg_checksum);
1414
1415         for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1416              gsi_next (&gsi))
1417           {
1418             gimple *stmt = gsi_stmt (gsi);
1419
1420             if (gimple_code (stmt) != GIMPLE_DEBUG
1421                 && gimple_code (stmt) != GIMPLE_PREDICT)
1422               {
1423                 hash_stmt (stmt, hstate);
1424                 nondbg_stmt_count++;
1425               }
1426           }
1427
1428         gcode_hash = hstate.end ();
1429         bb_sizes.safe_push (nondbg_stmt_count);
1430
1431         /* Inserting basic block to hash table.  */
1432         sem_bb *semantic_bb = new sem_bb (bb, nondbg_stmt_count,
1433                                           EDGE_COUNT (bb->preds)
1434                                           + EDGE_COUNT (bb->succs));
1435
1436         bb_sorted.safe_push (semantic_bb);
1437       }
1438     }
1439   else
1440     {
1441       cfg_checksum = 0;
1442       inchash::hash hstate;
1443       hstate.add_wide_int (cnode->thunk.fixed_offset);
1444       hstate.add_wide_int (cnode->thunk.virtual_value);
1445       hstate.add_flag (cnode->thunk.this_adjusting);
1446       hstate.add_flag (cnode->thunk.virtual_offset_p);
1447       hstate.add_flag (cnode->thunk.add_pointer_bounds_args);
1448       gcode_hash = hstate.end ();
1449     }
1450 }
1451
1452 /* Accumulate to HSTATE a hash of expression EXP.
1453    Identical to inchash::add_expr, but guaranteed to be stable across LTO
1454    and DECL equality classes.  */
1455
1456 void
1457 sem_item::add_expr (const_tree exp, inchash::hash &hstate)
1458 {
1459   if (exp == NULL_TREE)
1460     {
1461       hstate.merge_hash (0);
1462       return;
1463     }
1464
1465   /* Handled component can be matched in a cureful way proving equivalence
1466      even if they syntactically differ.  Just skip them.  */
1467   STRIP_NOPS (exp);
1468   while (handled_component_p (exp))
1469     exp = TREE_OPERAND (exp, 0);
1470
1471   enum tree_code code = TREE_CODE (exp);
1472   hstate.add_int (code);
1473
1474   switch (code)
1475     {
1476     /* Use inchash::add_expr for everything that is LTO stable.  */
1477     case VOID_CST:
1478     case INTEGER_CST:
1479     case REAL_CST:
1480     case FIXED_CST:
1481     case STRING_CST:
1482     case COMPLEX_CST:
1483     case VECTOR_CST:
1484       inchash::add_expr (exp, hstate);
1485       break;
1486     case CONSTRUCTOR:
1487       {
1488         unsigned HOST_WIDE_INT idx;
1489         tree value;
1490
1491         hstate.add_wide_int (int_size_in_bytes (TREE_TYPE (exp)));
1492
1493         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
1494           if (value)
1495             add_expr (value, hstate);
1496         break;
1497       }
1498     case ADDR_EXPR:
1499     case FDESC_EXPR:
1500       add_expr (get_base_address (TREE_OPERAND (exp, 0)), hstate);
1501       break;
1502     case SSA_NAME:
1503     case VAR_DECL:
1504     case CONST_DECL:
1505     case PARM_DECL:
1506       hstate.add_wide_int (int_size_in_bytes (TREE_TYPE (exp)));
1507       break;
1508     case MEM_REF:
1509     case POINTER_PLUS_EXPR:
1510     case MINUS_EXPR:
1511     case RANGE_EXPR:
1512       add_expr (TREE_OPERAND (exp, 0), hstate);
1513       add_expr (TREE_OPERAND (exp, 1), hstate);
1514       break;
1515     case PLUS_EXPR:
1516       {
1517         inchash::hash one, two;
1518         add_expr (TREE_OPERAND (exp, 0), one);
1519         add_expr (TREE_OPERAND (exp, 1), two);
1520         hstate.add_commutative (one, two);
1521       }
1522       break;
1523     CASE_CONVERT:
1524       hstate.add_wide_int (int_size_in_bytes (TREE_TYPE (exp)));
1525       return add_expr (TREE_OPERAND (exp, 0), hstate);
1526     default:
1527       break;
1528     }
1529 }
1530
1531 /* Accumulate to HSTATE a hash of type t.
1532    TYpes that may end up being compatible after LTO type merging needs to have
1533    the same hash.  */
1534
1535 void
1536 sem_item::add_type (const_tree type, inchash::hash &hstate)
1537 {
1538   if (type == NULL_TREE)
1539     {
1540       hstate.merge_hash (0);
1541       return;
1542     }
1543
1544   type = TYPE_MAIN_VARIANT (type);
1545
1546   hstate.add_int (TYPE_MODE (type));
1547
1548   if (TREE_CODE (type) == COMPLEX_TYPE)
1549     {
1550       hstate.add_int (COMPLEX_TYPE);
1551       sem_item::add_type (TREE_TYPE (type), hstate);
1552     }
1553   else if (INTEGRAL_TYPE_P (type))
1554     {
1555       hstate.add_int (INTEGER_TYPE);
1556       hstate.add_flag (TYPE_UNSIGNED (type));
1557       hstate.add_int (TYPE_PRECISION (type));
1558     }
1559   else if (VECTOR_TYPE_P (type))
1560     {
1561       hstate.add_int (VECTOR_TYPE);
1562       hstate.add_int (TYPE_PRECISION (type));
1563       sem_item::add_type (TREE_TYPE (type), hstate);
1564     }
1565   else if (TREE_CODE (type) == ARRAY_TYPE)
1566     {
1567       hstate.add_int (ARRAY_TYPE);
1568       /* Do not hash size, so complete and incomplete types can match.  */
1569       sem_item::add_type (TREE_TYPE (type), hstate);
1570     }
1571   else if (RECORD_OR_UNION_TYPE_P (type))
1572     {
1573       gcc_checking_assert (COMPLETE_TYPE_P (type));
1574       hashval_t *val = optimizer->m_type_hash_cache.get (type);
1575
1576       if (!val)
1577         {
1578           inchash::hash hstate2;
1579           unsigned nf;
1580           tree f;
1581           hashval_t hash;
1582
1583           hstate2.add_int (RECORD_TYPE);
1584           gcc_assert (COMPLETE_TYPE_P (type));
1585
1586           for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
1587             if (TREE_CODE (f) == FIELD_DECL)
1588               {
1589                 add_type (TREE_TYPE (f), hstate2);
1590                 nf++;
1591               }
1592
1593           hstate2.add_int (nf);
1594           hash = hstate2.end ();
1595           hstate.add_wide_int (hash);
1596           optimizer->m_type_hash_cache.put (type, hash);
1597         }
1598       else
1599         hstate.add_wide_int (*val);
1600     }
1601 }
1602
1603 /* Improve accumulated hash for HSTATE based on a gimple statement STMT.  */
1604
1605 void
1606 sem_function::hash_stmt (gimple *stmt, inchash::hash &hstate)
1607 {
1608   enum gimple_code code = gimple_code (stmt);
1609
1610   hstate.add_int (code);
1611
1612   switch (code)
1613     {
1614     case GIMPLE_SWITCH:
1615       add_expr (gimple_switch_index (as_a <gswitch *> (stmt)), hstate);
1616       break;
1617     case GIMPLE_ASSIGN:
1618       hstate.add_int (gimple_assign_rhs_code (stmt));
1619       if (commutative_tree_code (gimple_assign_rhs_code (stmt))
1620           || commutative_ternary_tree_code (gimple_assign_rhs_code (stmt)))
1621         {
1622           inchash::hash one, two;
1623
1624           add_expr (gimple_assign_rhs1 (stmt), one);
1625           add_type (TREE_TYPE (gimple_assign_rhs1 (stmt)), one);
1626           add_expr (gimple_assign_rhs2 (stmt), two);
1627           hstate.add_commutative (one, two);
1628           if (commutative_ternary_tree_code (gimple_assign_rhs_code (stmt)))
1629             {
1630               add_expr (gimple_assign_rhs3 (stmt), hstate);
1631               add_type (TREE_TYPE (gimple_assign_rhs3 (stmt)), hstate);
1632             }
1633           add_expr (gimple_assign_lhs (stmt), hstate);
1634           add_type (TREE_TYPE (gimple_assign_lhs (stmt)), two);
1635           break;
1636         }
1637       /* ... fall through ... */
1638     case GIMPLE_CALL:
1639     case GIMPLE_ASM:
1640     case GIMPLE_COND:
1641     case GIMPLE_GOTO:
1642     case GIMPLE_RETURN:
1643       /* All these statements are equivalent if their operands are.  */
1644       for (unsigned i = 0; i < gimple_num_ops (stmt); ++i)
1645         {
1646           add_expr (gimple_op (stmt, i), hstate);
1647           if (gimple_op (stmt, i))
1648             add_type (TREE_TYPE (gimple_op (stmt, i)), hstate);
1649         }
1650     default:
1651       break;
1652     }
1653 }
1654
1655
1656 /* Return true if polymorphic comparison must be processed.  */
1657
1658 bool
1659 sem_function::compare_polymorphic_p (void)
1660 {
1661   struct cgraph_edge *e;
1662
1663   if (!opt_for_fn (get_node ()->decl, flag_devirtualize))
1664     return false;
1665   if (get_node ()->indirect_calls != NULL)
1666     return true;
1667   /* TODO: We can do simple propagation determining what calls may lead to
1668      a polymorphic call.  */
1669   for (e = get_node ()->callees; e; e = e->next_callee)
1670     if (e->callee->definition
1671         && opt_for_fn (e->callee->decl, flag_devirtualize))
1672       return true;
1673   return false;
1674 }
1675
1676 /* For a given call graph NODE, the function constructs new
1677    semantic function item.  */
1678
1679 sem_function *
1680 sem_function::parse (cgraph_node *node, bitmap_obstack *stack)
1681 {
1682   tree fndecl = node->decl;
1683   function *func = DECL_STRUCT_FUNCTION (fndecl);
1684
1685   if (!func || (!node->has_gimple_body_p () && !node->thunk.thunk_p))
1686     return NULL;
1687
1688   if (lookup_attribute_by_prefix ("omp ", DECL_ATTRIBUTES (node->decl)) != NULL)
1689     return NULL;
1690
1691   /* PR ipa/70306.  */
1692   if (DECL_STATIC_CONSTRUCTOR (node->decl)
1693       || DECL_STATIC_DESTRUCTOR (node->decl))
1694     return NULL;
1695
1696   sem_function *f = new sem_function (node, stack);
1697
1698   f->init ();
1699
1700   return f;
1701 }
1702
1703 /* For given basic blocks BB1 and BB2 (from functions FUNC1 and FUNC),
1704    return true if phi nodes are semantically equivalent in these blocks .  */
1705
1706 bool
1707 sem_function::compare_phi_node (basic_block bb1, basic_block bb2)
1708 {
1709   gphi_iterator si1, si2;
1710   gphi *phi1, *phi2;
1711   unsigned size1, size2, i;
1712   tree t1, t2;
1713   edge e1, e2;
1714
1715   gcc_assert (bb1 != NULL);
1716   gcc_assert (bb2 != NULL);
1717
1718   si2 = gsi_start_phis (bb2);
1719   for (si1 = gsi_start_phis (bb1); !gsi_end_p (si1);
1720        gsi_next (&si1))
1721     {
1722       gsi_next_nonvirtual_phi (&si1);
1723       gsi_next_nonvirtual_phi (&si2);
1724
1725       if (gsi_end_p (si1) && gsi_end_p (si2))
1726         break;
1727
1728       if (gsi_end_p (si1) || gsi_end_p (si2))
1729         return return_false();
1730
1731       phi1 = si1.phi ();
1732       phi2 = si2.phi ();
1733
1734       tree phi_result1 = gimple_phi_result (phi1);
1735       tree phi_result2 = gimple_phi_result (phi2);
1736
1737       if (!m_checker->compare_operand (phi_result1, phi_result2))
1738         return return_false_with_msg ("PHI results are different");
1739
1740       size1 = gimple_phi_num_args (phi1);
1741       size2 = gimple_phi_num_args (phi2);
1742
1743       if (size1 != size2)
1744         return return_false ();
1745
1746       for (i = 0; i < size1; ++i)
1747         {
1748           t1 = gimple_phi_arg (phi1, i)->def;
1749           t2 = gimple_phi_arg (phi2, i)->def;
1750
1751           if (!m_checker->compare_operand (t1, t2))
1752             return return_false ();
1753
1754           e1 = gimple_phi_arg_edge (phi1, i);
1755           e2 = gimple_phi_arg_edge (phi2, i);
1756
1757           if (!m_checker->compare_edge (e1, e2))
1758             return return_false ();
1759         }
1760
1761       gsi_next (&si2);
1762     }
1763
1764   return true;
1765 }
1766
1767 /* Returns true if tree T can be compared as a handled component.  */
1768
1769 bool
1770 sem_function::icf_handled_component_p (tree t)
1771 {
1772   tree_code tc = TREE_CODE (t);
1773
1774   return (handled_component_p (t)
1775           || tc == ADDR_EXPR || tc == MEM_REF || tc == OBJ_TYPE_REF);
1776 }
1777
1778 /* Basic blocks dictionary BB_DICT returns true if SOURCE index BB
1779    corresponds to TARGET.  */
1780
1781 bool
1782 sem_function::bb_dict_test (vec<int> *bb_dict, int source, int target)
1783 {
1784   source++;
1785   target++;
1786
1787   if (bb_dict->length () <= (unsigned)source)
1788     bb_dict->safe_grow_cleared (source + 1);
1789
1790   if ((*bb_dict)[source] == 0)
1791     {
1792       (*bb_dict)[source] = target;
1793       return true;
1794     }
1795   else
1796     return (*bb_dict)[source] == target;
1797 }
1798
1799 sem_variable::sem_variable (bitmap_obstack *stack): sem_item (VAR, stack)
1800 {
1801 }
1802
1803 sem_variable::sem_variable (varpool_node *node, bitmap_obstack *stack)
1804 : sem_item (VAR, node, stack)
1805 {
1806   gcc_checking_assert (node);
1807   gcc_checking_assert (get_node ());
1808 }
1809
1810 /* Fast equality function based on knowledge known in WPA.  */
1811
1812 bool
1813 sem_variable::equals_wpa (sem_item *item,
1814                           hash_map <symtab_node *, sem_item *> &ignored_nodes)
1815 {
1816   gcc_assert (item->type == VAR);
1817
1818   if (node->num_references () != item->node->num_references ())
1819     return return_false_with_msg ("different number of references");
1820
1821   if (DECL_TLS_MODEL (decl) || DECL_TLS_MODEL (item->decl))
1822     return return_false_with_msg ("TLS model");
1823
1824   /* DECL_ALIGN is safe to merge, because we will always chose the largest
1825      alignment out of all aliases.  */
1826
1827   if (DECL_VIRTUAL_P (decl) != DECL_VIRTUAL_P (item->decl))
1828     return return_false_with_msg ("Virtual flag mismatch");
1829
1830   if (DECL_SIZE (decl) != DECL_SIZE (item->decl)
1831       && ((!DECL_SIZE (decl) || !DECL_SIZE (item->decl))
1832           || !operand_equal_p (DECL_SIZE (decl),
1833                                DECL_SIZE (item->decl), OEP_ONLY_CONST)))
1834     return return_false_with_msg ("size mismatch");
1835
1836   /* Do not attempt to mix data from different user sections;
1837      we do not know what user intends with those.  */
1838   if (((DECL_SECTION_NAME (decl) && !node->implicit_section)
1839        || (DECL_SECTION_NAME (item->decl) && !item->node->implicit_section))
1840       && DECL_SECTION_NAME (decl) != DECL_SECTION_NAME (item->decl))
1841     return return_false_with_msg ("user section mismatch");
1842
1843   if (DECL_IN_TEXT_SECTION (decl) != DECL_IN_TEXT_SECTION (item->decl))
1844     return return_false_with_msg ("text section");
1845
1846   ipa_ref *ref = NULL, *ref2 = NULL;
1847   for (unsigned i = 0; node->iterate_reference (i, ref); i++)
1848     {
1849       item->node->iterate_reference (i, ref2);
1850
1851       if (ref->use != ref2->use)
1852         return return_false_with_msg ("reference use mismatch");
1853
1854       if (!compare_symbol_references (ignored_nodes,
1855                                       ref->referred, ref2->referred,
1856                                       ref->address_matters_p ()))
1857         return false;
1858     }
1859
1860   return true;
1861 }
1862
1863 /* Returns true if the item equals to ITEM given as argument.  */
1864
1865 bool
1866 sem_variable::equals (sem_item *item,
1867                       hash_map <symtab_node *, sem_item *> &)
1868 {
1869   gcc_assert (item->type == VAR);
1870   bool ret;
1871
1872   if (DECL_INITIAL (decl) == error_mark_node && in_lto_p)
1873     dyn_cast <varpool_node *>(node)->get_constructor ();
1874   if (DECL_INITIAL (item->decl) == error_mark_node && in_lto_p)
1875     dyn_cast <varpool_node *>(item->node)->get_constructor ();
1876
1877   /* As seen in PR ipa/65303 we have to compare variables types.  */
1878   if (!func_checker::compatible_types_p (TREE_TYPE (decl),
1879                                          TREE_TYPE (item->decl)))
1880     return return_false_with_msg ("variables types are different");
1881
1882   ret = sem_variable::equals (DECL_INITIAL (decl),
1883                               DECL_INITIAL (item->node->decl));
1884   if (dump_file && (dump_flags & TDF_DETAILS))
1885     fprintf (dump_file,
1886              "Equals called for vars:%s:%s (%u:%u) (%s:%s) with result: %s\n\n",
1887              xstrdup_for_dump (node->name()),
1888              xstrdup_for_dump (item->node->name ()),
1889              node->order, item->node->order,
1890              xstrdup_for_dump (node->asm_name ()),
1891              xstrdup_for_dump (item->node->asm_name ()), ret ? "true" : "false");
1892
1893   return ret;
1894 }
1895
1896 /* Compares trees T1 and T2 for semantic equality.  */
1897
1898 bool
1899 sem_variable::equals (tree t1, tree t2)
1900 {
1901   if (!t1 || !t2)
1902     return return_with_debug (t1 == t2);
1903   if (t1 == t2)
1904     return true;
1905   tree_code tc1 = TREE_CODE (t1);
1906   tree_code tc2 = TREE_CODE (t2);
1907
1908   if (tc1 != tc2)
1909     return return_false_with_msg ("TREE_CODE mismatch");
1910
1911   switch (tc1)
1912     {
1913     case CONSTRUCTOR:
1914       {
1915         vec<constructor_elt, va_gc> *v1, *v2;
1916         unsigned HOST_WIDE_INT idx;
1917
1918         enum tree_code typecode = TREE_CODE (TREE_TYPE (t1));
1919         if (typecode != TREE_CODE (TREE_TYPE (t2)))
1920           return return_false_with_msg ("constructor type mismatch");
1921
1922         if (typecode == ARRAY_TYPE)
1923           {
1924             HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
1925             /* For arrays, check that the sizes all match.  */
1926             if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
1927                 || size_1 == -1
1928                 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
1929               return return_false_with_msg ("constructor array size mismatch");
1930           }
1931         else if (!func_checker::compatible_types_p (TREE_TYPE (t1),
1932                                                     TREE_TYPE (t2)))
1933           return return_false_with_msg ("constructor type incompatible");
1934
1935         v1 = CONSTRUCTOR_ELTS (t1);
1936         v2 = CONSTRUCTOR_ELTS (t2);
1937         if (vec_safe_length (v1) != vec_safe_length (v2))
1938           return return_false_with_msg ("constructor number of elts mismatch");
1939
1940         for (idx = 0; idx < vec_safe_length (v1); ++idx)
1941           {
1942             constructor_elt *c1 = &(*v1)[idx];
1943             constructor_elt *c2 = &(*v2)[idx];
1944
1945             /* Check that each value is the same...  */
1946             if (!sem_variable::equals (c1->value, c2->value))
1947               return false;
1948             /* ... and that they apply to the same fields!  */
1949             if (!sem_variable::equals (c1->index, c2->index))
1950               return false;
1951           }
1952         return true;
1953       }
1954     case MEM_REF:
1955       {
1956         tree x1 = TREE_OPERAND (t1, 0);
1957         tree x2 = TREE_OPERAND (t2, 0);
1958         tree y1 = TREE_OPERAND (t1, 1);
1959         tree y2 = TREE_OPERAND (t2, 1);
1960
1961         if (!func_checker::compatible_types_p (TREE_TYPE (x1), TREE_TYPE (x2)))
1962           return return_false ();
1963
1964         /* Type of the offset on MEM_REF does not matter.  */
1965         return return_with_debug (sem_variable::equals (x1, x2)
1966                                   && wi::to_offset  (y1)
1967                                      == wi::to_offset  (y2));
1968       }
1969     case ADDR_EXPR:
1970     case FDESC_EXPR:
1971       {
1972         tree op1 = TREE_OPERAND (t1, 0);
1973         tree op2 = TREE_OPERAND (t2, 0);
1974         return sem_variable::equals (op1, op2);
1975       }
1976     /* References to other vars/decls are compared using ipa-ref.  */
1977     case FUNCTION_DECL:
1978     case VAR_DECL:
1979       if (decl_in_symtab_p (t1) && decl_in_symtab_p (t2))
1980         return true;
1981       return return_false_with_msg ("Declaration mismatch");
1982     case CONST_DECL:
1983       /* TODO: We can check CONST_DECL by its DECL_INITIAL, but for that we
1984          need to process its VAR/FUNCTION references without relying on ipa-ref
1985          compare.  */
1986     case FIELD_DECL:
1987     case LABEL_DECL:
1988       return return_false_with_msg ("Declaration mismatch");
1989     case INTEGER_CST:
1990       /* Integer constants are the same only if the same width of type.  */
1991       if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
1992         return return_false_with_msg ("INTEGER_CST precision mismatch");
1993       if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
1994         return return_false_with_msg ("INTEGER_CST mode mismatch");
1995       return return_with_debug (tree_int_cst_equal (t1, t2));
1996     case STRING_CST:
1997       if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
1998         return return_false_with_msg ("STRING_CST mode mismatch");
1999       if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2))
2000         return return_false_with_msg ("STRING_CST length mismatch");
2001       if (memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2002                     TREE_STRING_LENGTH (t1)))
2003         return return_false_with_msg ("STRING_CST mismatch");
2004       return true;
2005     case FIXED_CST:
2006       /* Fixed constants are the same only if the same width of type.  */
2007       if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2008         return return_false_with_msg ("FIXED_CST precision mismatch");
2009
2010       return return_with_debug (FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
2011                                                         TREE_FIXED_CST (t2)));
2012     case COMPLEX_CST:
2013       return (sem_variable::equals (TREE_REALPART (t1), TREE_REALPART (t2))
2014               && sem_variable::equals (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2015     case REAL_CST:
2016       /* Real constants are the same only if the same width of type.  */
2017       if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2018         return return_false_with_msg ("REAL_CST precision mismatch");
2019       return return_with_debug (real_identical (&TREE_REAL_CST (t1),
2020                                                 &TREE_REAL_CST (t2)));
2021     case VECTOR_CST:
2022       {
2023         unsigned i;
2024
2025         if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
2026           return return_false_with_msg ("VECTOR_CST nelts mismatch");
2027
2028         for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
2029           if (!sem_variable::equals (VECTOR_CST_ELT (t1, i),
2030                                      VECTOR_CST_ELT (t2, i)))
2031             return 0;
2032
2033         return 1;
2034       }
2035     case ARRAY_REF:
2036     case ARRAY_RANGE_REF:
2037       {
2038         tree x1 = TREE_OPERAND (t1, 0);
2039         tree x2 = TREE_OPERAND (t2, 0);
2040         tree y1 = TREE_OPERAND (t1, 1);
2041         tree y2 = TREE_OPERAND (t2, 1);
2042
2043         if (!sem_variable::equals (x1, x2) || !sem_variable::equals (y1, y2))
2044           return false;
2045         if (!sem_variable::equals (array_ref_low_bound (t1),
2046                                    array_ref_low_bound (t2)))
2047           return false;
2048         if (!sem_variable::equals (array_ref_element_size (t1),
2049                                    array_ref_element_size (t2)))
2050           return false;
2051         return true;
2052       }
2053      
2054     case COMPONENT_REF:
2055     case POINTER_PLUS_EXPR:
2056     case PLUS_EXPR:
2057     case MINUS_EXPR:
2058     case RANGE_EXPR:
2059       {
2060         tree x1 = TREE_OPERAND (t1, 0);
2061         tree x2 = TREE_OPERAND (t2, 0);
2062         tree y1 = TREE_OPERAND (t1, 1);
2063         tree y2 = TREE_OPERAND (t2, 1);
2064
2065         return sem_variable::equals (x1, x2) && sem_variable::equals (y1, y2);
2066       }
2067
2068     CASE_CONVERT:
2069     case VIEW_CONVERT_EXPR:
2070       if (!func_checker::compatible_types_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2071           return return_false ();
2072       return sem_variable::equals (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2073     case ERROR_MARK:
2074       return return_false_with_msg ("ERROR_MARK");
2075     default:
2076       return return_false_with_msg ("Unknown TREE code reached");
2077     }
2078 }
2079
2080 /* Parser function that visits a varpool NODE.  */
2081
2082 sem_variable *
2083 sem_variable::parse (varpool_node *node, bitmap_obstack *stack)
2084 {
2085   if (TREE_THIS_VOLATILE (node->decl) || DECL_HARD_REGISTER (node->decl)
2086       || node->alias)
2087     return NULL;
2088
2089   sem_variable *v = new sem_variable (node, stack);
2090
2091   v->init ();
2092
2093   return v;
2094 }
2095
2096 /* References independent hash function.  */
2097
2098 hashval_t
2099 sem_variable::get_hash (void)
2100 {
2101   if (m_hash_set)
2102     return m_hash;
2103
2104   /* All WPA streamed in symbols should have their hashes computed at compile
2105      time.  At this point, the constructor may not be in memory at all.
2106      DECL_INITIAL (decl) would be error_mark_node in that case.  */
2107   gcc_assert (!node->lto_file_data);
2108   tree ctor = DECL_INITIAL (decl);
2109   inchash::hash hstate;
2110
2111   hstate.add_int (456346417);
2112   if (DECL_SIZE (decl) && tree_fits_shwi_p (DECL_SIZE (decl)))
2113     hstate.add_wide_int (tree_to_shwi (DECL_SIZE (decl)));
2114   add_expr (ctor, hstate);
2115   set_hash (hstate.end ());
2116
2117   return m_hash;
2118 }
2119
2120 /* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can
2121    be applied.  */
2122
2123 bool
2124 sem_variable::merge (sem_item *alias_item)
2125 {
2126   gcc_assert (alias_item->type == VAR);
2127
2128   if (!sem_item::target_supports_symbol_aliases_p ())
2129     {
2130       if (dump_file)
2131         fprintf (dump_file, "Not unifying; "
2132                  "Symbol aliases are not supported by target\n\n");
2133       return false;
2134     }
2135
2136   if (DECL_EXTERNAL (alias_item->decl))
2137     {
2138       if (dump_file)
2139         fprintf (dump_file, "Not unifying; alias is external.\n\n");
2140       return false;
2141     }
2142
2143   sem_variable *alias_var = static_cast<sem_variable *> (alias_item);
2144
2145   varpool_node *original = get_node ();
2146   varpool_node *alias = alias_var->get_node ();
2147   bool original_discardable = false;
2148
2149   bool original_address_matters = original->address_matters_p ();
2150   bool alias_address_matters = alias->address_matters_p ();
2151
2152   /* See if original is in a section that can be discarded if the main
2153      symbol is not used.
2154      Also consider case where we have resolution info and we know that
2155      original's definition is not going to be used.  In this case we can not
2156      create alias to original.  */
2157   if (original->can_be_discarded_p ()
2158       || (node->resolution != LDPR_UNKNOWN
2159           && !decl_binds_to_current_def_p (node->decl)))
2160     original_discardable = true;
2161
2162   gcc_assert (!TREE_ASM_WRITTEN (alias->decl));
2163
2164   /* Constant pool machinery is not quite ready for aliases.
2165      TODO: varasm code contains logic for merging DECL_IN_CONSTANT_POOL.
2166      For LTO merging does not happen that is an important missing feature.
2167      We can enable merging with LTO if the DECL_IN_CONSTANT_POOL
2168      flag is dropped and non-local symbol name is assigned.  */
2169   if (DECL_IN_CONSTANT_POOL (alias->decl)
2170       || DECL_IN_CONSTANT_POOL (original->decl))
2171     {
2172       if (dump_file)
2173         fprintf (dump_file,
2174                  "Not unifying; constant pool variables.\n\n");
2175       return false;
2176     }
2177
2178   /* Do not attempt to mix functions from different user sections;
2179      we do not know what user intends with those.  */
2180   if (((DECL_SECTION_NAME (original->decl) && !original->implicit_section)
2181        || (DECL_SECTION_NAME (alias->decl) && !alias->implicit_section))
2182       && DECL_SECTION_NAME (original->decl) != DECL_SECTION_NAME (alias->decl))
2183     {
2184       if (dump_file)
2185         fprintf (dump_file,
2186                  "Not unifying; "
2187                  "original and alias are in different sections.\n\n");
2188       return false;
2189     }
2190
2191   /* We can not merge if address comparsion metters.  */
2192   if (original_address_matters && alias_address_matters
2193       && flag_merge_constants < 2)
2194     {
2195       if (dump_file)
2196         fprintf (dump_file,
2197                  "Not unifying; "
2198                  "adress of original and alias may be compared.\n\n");
2199       return false;
2200     }
2201
2202   if (DECL_ALIGN (original->decl) < DECL_ALIGN (alias->decl))
2203     {
2204       if (dump_file)
2205         fprintf (dump_file, "Not unifying; "
2206                  "original and alias have incompatible alignments\n\n");
2207
2208       return false;
2209     }
2210
2211   if (DECL_COMDAT_GROUP (original->decl) != DECL_COMDAT_GROUP (alias->decl))
2212     {
2213       if (dump_file)
2214         fprintf (dump_file, "Not unifying; alias cannot be created; "
2215                  "across comdat group boundary\n\n");
2216
2217       return false;
2218     }
2219
2220   if (original_discardable)
2221     {
2222       if (dump_file)
2223         fprintf (dump_file, "Not unifying; alias cannot be created; "
2224                  "target is discardable\n\n");
2225
2226       return false;
2227     }
2228   else
2229     {
2230       gcc_assert (!original->alias);
2231       gcc_assert (!alias->alias);
2232
2233       alias->analyzed = false;
2234
2235       DECL_INITIAL (alias->decl) = NULL;
2236       ((symtab_node *)alias)->call_for_symbol_and_aliases (clear_decl_rtl,
2237                                                            NULL, true);
2238       alias->need_bounds_init = false;
2239       alias->remove_all_references ();
2240       if (TREE_ADDRESSABLE (alias->decl))
2241         original->call_for_symbol_and_aliases (set_addressable, NULL, true);
2242
2243       varpool_node::create_alias (alias_var->decl, decl);
2244       alias->resolve_alias (original);
2245       if (DECL_PT_UID_SET_P (original->decl))
2246         SET_DECL_PT_UID (alias->decl, DECL_PT_UID (original->decl));
2247
2248       if (dump_file)
2249         fprintf (dump_file, "Unified; Variable alias has been created.\n\n");
2250
2251       return true;
2252     }
2253 }
2254
2255 /* Dump symbol to FILE.  */
2256
2257 void
2258 sem_variable::dump_to_file (FILE *file)
2259 {
2260   gcc_assert (file);
2261
2262   print_node (file, "", decl, 0);
2263   fprintf (file, "\n\n");
2264 }
2265
2266 unsigned int sem_item_optimizer::class_id = 0;
2267
2268 sem_item_optimizer::sem_item_optimizer ()
2269 : worklist (0), m_classes (0), m_classes_count (0), m_cgraph_node_hooks (NULL),
2270   m_varpool_node_hooks (NULL)
2271 {
2272   m_items.create (0);
2273   bitmap_obstack_initialize (&m_bmstack);
2274 }
2275
2276 sem_item_optimizer::~sem_item_optimizer ()
2277 {
2278   for (unsigned int i = 0; i < m_items.length (); i++)
2279     delete m_items[i];
2280
2281   for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
2282        it != m_classes.end (); ++it)
2283     {
2284       for (unsigned int i = 0; i < (*it)->classes.length (); i++)
2285         delete (*it)->classes[i];
2286
2287       (*it)->classes.release ();
2288       free (*it);
2289     }
2290
2291   m_items.release ();
2292
2293   bitmap_obstack_release (&m_bmstack);
2294 }
2295
2296 /* Write IPA ICF summary for symbols.  */
2297
2298 void
2299 sem_item_optimizer::write_summary (void)
2300 {
2301   unsigned int count = 0;
2302
2303   output_block *ob = create_output_block (LTO_section_ipa_icf);
2304   lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
2305   ob->symbol = NULL;
2306
2307   /* Calculate number of symbols to be serialized.  */
2308   for (lto_symtab_encoder_iterator lsei = lsei_start_in_partition (encoder);
2309        !lsei_end_p (lsei);
2310        lsei_next_in_partition (&lsei))
2311     {
2312       symtab_node *node = lsei_node (lsei);
2313
2314       if (m_symtab_node_map.get (node))
2315         count++;
2316     }
2317
2318   streamer_write_uhwi (ob, count);
2319
2320   /* Process all of the symbols.  */
2321   for (lto_symtab_encoder_iterator lsei = lsei_start_in_partition (encoder);
2322        !lsei_end_p (lsei);
2323        lsei_next_in_partition (&lsei))
2324     {
2325       symtab_node *node = lsei_node (lsei);
2326
2327       sem_item **item = m_symtab_node_map.get (node);
2328
2329       if (item && *item)
2330         {
2331           int node_ref = lto_symtab_encoder_encode (encoder, node);
2332           streamer_write_uhwi_stream (ob->main_stream, node_ref);
2333
2334           streamer_write_uhwi (ob, (*item)->get_hash ());
2335         }
2336     }
2337
2338   streamer_write_char_stream (ob->main_stream, 0);
2339   produce_asm (ob, NULL);
2340   destroy_output_block (ob);
2341 }
2342
2343 /* Reads a section from LTO stream file FILE_DATA. Input block for DATA
2344    contains LEN bytes.  */
2345
2346 void
2347 sem_item_optimizer::read_section (lto_file_decl_data *file_data,
2348                                   const char *data, size_t len)
2349 {
2350   const lto_function_header *header =
2351     (const lto_function_header *) data;
2352   const int cfg_offset = sizeof (lto_function_header);
2353   const int main_offset = cfg_offset + header->cfg_size;
2354   const int string_offset = main_offset + header->main_size;
2355   data_in *data_in;
2356   unsigned int i;
2357   unsigned int count;
2358
2359   lto_input_block ib_main ((const char *) data + main_offset, 0,
2360                            header->main_size, file_data->mode_table);
2361
2362   data_in =
2363     lto_data_in_create (file_data, (const char *) data + string_offset,
2364                         header->string_size, vNULL);
2365
2366   count = streamer_read_uhwi (&ib_main);
2367
2368   for (i = 0; i < count; i++)
2369     {
2370       unsigned int index;
2371       symtab_node *node;
2372       lto_symtab_encoder_t encoder;
2373
2374       index = streamer_read_uhwi (&ib_main);
2375       encoder = file_data->symtab_node_encoder;
2376       node = lto_symtab_encoder_deref (encoder, index);
2377
2378       hashval_t hash = streamer_read_uhwi (&ib_main);
2379
2380       gcc_assert (node->definition);
2381
2382       if (dump_file)
2383         fprintf (dump_file, "Symbol added:%s (tree: %p, uid:%u)\n",
2384                  node->asm_name (), (void *) node->decl, node->order);
2385
2386       if (is_a<cgraph_node *> (node))
2387         {
2388           cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2389
2390           sem_function *fn = new sem_function (cnode, &m_bmstack);
2391           fn->set_hash (hash);
2392           m_items.safe_push (fn);
2393         }
2394       else
2395         {
2396           varpool_node *vnode = dyn_cast <varpool_node *> (node);
2397
2398           sem_variable *var = new sem_variable (vnode, &m_bmstack);
2399           var->set_hash (hash);
2400           m_items.safe_push (var);
2401         }
2402     }
2403
2404   lto_free_section_data (file_data, LTO_section_ipa_icf, NULL, data,
2405                          len);
2406   lto_data_in_delete (data_in);
2407 }
2408
2409 /* Read IPA ICF summary for symbols.  */
2410
2411 void
2412 sem_item_optimizer::read_summary (void)
2413 {
2414   lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2415   lto_file_decl_data *file_data;
2416   unsigned int j = 0;
2417
2418   while ((file_data = file_data_vec[j++]))
2419     {
2420       size_t len;
2421       const char *data = lto_get_section_data (file_data,
2422                          LTO_section_ipa_icf, NULL, &len);
2423
2424       if (data)
2425         read_section (file_data, data, len);
2426     }
2427 }
2428
2429 /* Register callgraph and varpool hooks.  */
2430
2431 void
2432 sem_item_optimizer::register_hooks (void)
2433 {
2434   if (!m_cgraph_node_hooks)
2435     m_cgraph_node_hooks = symtab->add_cgraph_removal_hook
2436                           (&sem_item_optimizer::cgraph_removal_hook, this);
2437
2438   if (!m_varpool_node_hooks)
2439     m_varpool_node_hooks = symtab->add_varpool_removal_hook
2440                            (&sem_item_optimizer::varpool_removal_hook, this);
2441 }
2442
2443 /* Unregister callgraph and varpool hooks.  */
2444
2445 void
2446 sem_item_optimizer::unregister_hooks (void)
2447 {
2448   if (m_cgraph_node_hooks)
2449     symtab->remove_cgraph_removal_hook (m_cgraph_node_hooks);
2450
2451   if (m_varpool_node_hooks)
2452     symtab->remove_varpool_removal_hook (m_varpool_node_hooks);
2453 }
2454
2455 /* Adds a CLS to hashtable associated by hash value.  */
2456
2457 void
2458 sem_item_optimizer::add_class (congruence_class *cls)
2459 {
2460   gcc_assert (cls->members.length ());
2461
2462   congruence_class_group *group = get_group_by_hash (
2463                                     cls->members[0]->get_hash (),
2464                                     cls->members[0]->type);
2465   group->classes.safe_push (cls);
2466 }
2467
2468 /* Gets a congruence class group based on given HASH value and TYPE.  */
2469
2470 congruence_class_group *
2471 sem_item_optimizer::get_group_by_hash (hashval_t hash, sem_item_type type)
2472 {
2473   congruence_class_group *item = XNEW (congruence_class_group);
2474   item->hash = hash;
2475   item->type = type;
2476
2477   congruence_class_group **slot = m_classes.find_slot (item, INSERT);
2478
2479   if (*slot)
2480     free (item);
2481   else
2482     {
2483       item->classes.create (1);
2484       *slot = item;
2485     }
2486
2487   return *slot;
2488 }
2489
2490 /* Callgraph removal hook called for a NODE with a custom DATA.  */
2491
2492 void
2493 sem_item_optimizer::cgraph_removal_hook (cgraph_node *node, void *data)
2494 {
2495   sem_item_optimizer *optimizer = (sem_item_optimizer *) data;
2496   optimizer->remove_symtab_node (node);
2497 }
2498
2499 /* Varpool removal hook called for a NODE with a custom DATA.  */
2500
2501 void
2502 sem_item_optimizer::varpool_removal_hook (varpool_node *node, void *data)
2503 {
2504   sem_item_optimizer *optimizer = (sem_item_optimizer *) data;
2505   optimizer->remove_symtab_node (node);
2506 }
2507
2508 /* Remove symtab NODE triggered by symtab removal hooks.  */
2509
2510 void
2511 sem_item_optimizer::remove_symtab_node (symtab_node *node)
2512 {
2513   gcc_assert (!m_classes.elements());
2514
2515   m_removed_items_set.add (node);
2516 }
2517
2518 void
2519 sem_item_optimizer::remove_item (sem_item *item)
2520 {
2521   if (m_symtab_node_map.get (item->node))
2522     m_symtab_node_map.remove (item->node);
2523   delete item;
2524 }
2525
2526 /* Removes all callgraph and varpool nodes that are marked by symtab
2527    as deleted.  */
2528
2529 void
2530 sem_item_optimizer::filter_removed_items (void)
2531 {
2532   auto_vec <sem_item *> filtered;
2533
2534   for (unsigned int i = 0; i < m_items.length(); i++)
2535     {
2536       sem_item *item = m_items[i];
2537
2538       if (m_removed_items_set.contains (item->node))
2539         {
2540           remove_item (item);
2541           continue;
2542         }
2543
2544       if (item->type == FUNC)
2545         {
2546           cgraph_node *cnode = static_cast <sem_function *>(item)->get_node ();
2547
2548           if (in_lto_p && (cnode->alias || cnode->body_removed))
2549             remove_item (item);
2550           else
2551             filtered.safe_push (item);
2552         }
2553       else /* VAR.  */
2554         {
2555           if (!flag_ipa_icf_variables)
2556             remove_item (item);
2557           else
2558             {
2559               /* Filter out non-readonly variables.  */
2560               tree decl = item->decl;
2561               if (TREE_READONLY (decl))
2562                 filtered.safe_push (item);
2563               else
2564                 remove_item (item);
2565             }
2566         }
2567     }
2568
2569   /* Clean-up of released semantic items.  */
2570
2571   m_items.release ();
2572   for (unsigned int i = 0; i < filtered.length(); i++)
2573     m_items.safe_push (filtered[i]);
2574 }
2575
2576 /* Optimizer entry point which returns true in case it processes
2577    a merge operation. True is returned if there's a merge operation
2578    processed.  */
2579
2580 bool
2581 sem_item_optimizer::execute (void)
2582 {
2583   filter_removed_items ();
2584   unregister_hooks ();
2585
2586   build_graph ();
2587   update_hash_by_addr_refs ();
2588   build_hash_based_classes ();
2589
2590   if (dump_file)
2591     fprintf (dump_file, "Dump after hash based groups\n");
2592   dump_cong_classes ();
2593
2594   for (unsigned int i = 0; i < m_items.length(); i++)
2595     m_items[i]->init_wpa ();
2596
2597   subdivide_classes_by_equality (true);
2598
2599   if (dump_file)
2600     fprintf (dump_file, "Dump after WPA based types groups\n");
2601
2602   dump_cong_classes ();
2603
2604   process_cong_reduction ();
2605   checking_verify_classes ();
2606
2607   if (dump_file)
2608     fprintf (dump_file, "Dump after callgraph-based congruence reduction\n");
2609
2610   dump_cong_classes ();
2611
2612   parse_nonsingleton_classes ();
2613   subdivide_classes_by_equality ();
2614
2615   if (dump_file)
2616     fprintf (dump_file, "Dump after full equality comparison of groups\n");
2617
2618   dump_cong_classes ();
2619
2620   unsigned int prev_class_count = m_classes_count;
2621
2622   process_cong_reduction ();
2623   dump_cong_classes ();
2624   checking_verify_classes ();
2625   bool merged_p = merge_classes (prev_class_count);
2626
2627   if (dump_file && (dump_flags & TDF_DETAILS))
2628     symtab_node::dump_table (dump_file);
2629
2630   return merged_p;
2631 }
2632
2633 /* Function responsible for visiting all potential functions and
2634    read-only variables that can be merged.  */
2635
2636 void
2637 sem_item_optimizer::parse_funcs_and_vars (void)
2638 {
2639   cgraph_node *cnode;
2640
2641   if (flag_ipa_icf_functions)
2642     FOR_EACH_DEFINED_FUNCTION (cnode)
2643     {
2644       sem_function *f = sem_function::parse (cnode, &m_bmstack);
2645       if (f)
2646         {
2647           m_items.safe_push (f);
2648           m_symtab_node_map.put (cnode, f);
2649
2650           if (dump_file)
2651             fprintf (dump_file, "Parsed function:%s\n", f->node->asm_name ());
2652
2653           if (dump_file && (dump_flags & TDF_DETAILS))
2654             f->dump_to_file (dump_file);
2655         }
2656       else if (dump_file)
2657         fprintf (dump_file, "Not parsed function:%s\n", cnode->asm_name ());
2658     }
2659
2660   varpool_node *vnode;
2661
2662   if (flag_ipa_icf_variables)
2663     FOR_EACH_DEFINED_VARIABLE (vnode)
2664     {
2665       sem_variable *v = sem_variable::parse (vnode, &m_bmstack);
2666
2667       if (v)
2668         {
2669           m_items.safe_push (v);
2670           m_symtab_node_map.put (vnode, v);
2671         }
2672     }
2673 }
2674
2675 /* Makes pairing between a congruence class CLS and semantic ITEM.  */
2676
2677 void
2678 sem_item_optimizer::add_item_to_class (congruence_class *cls, sem_item *item)
2679 {
2680   item->index_in_class = cls->members.length ();
2681   cls->members.safe_push (item);
2682   item->cls = cls;
2683 }
2684
2685 /* For each semantic item, append hash values of references.  */
2686
2687 void
2688 sem_item_optimizer::update_hash_by_addr_refs ()
2689 {
2690   /* First, append to hash sensitive references and class type if it need to
2691      be matched for ODR.  */
2692   for (unsigned i = 0; i < m_items.length (); i++)
2693     {
2694       m_items[i]->update_hash_by_addr_refs (m_symtab_node_map);
2695       if (m_items[i]->type == FUNC)
2696         {
2697           if (TREE_CODE (TREE_TYPE (m_items[i]->decl)) == METHOD_TYPE
2698               && contains_polymorphic_type_p
2699                    (TYPE_METHOD_BASETYPE (TREE_TYPE (m_items[i]->decl)))
2700               && (DECL_CXX_CONSTRUCTOR_P (m_items[i]->decl)
2701                   || (static_cast<sem_function *> (m_items[i])->param_used_p (0)
2702                       && static_cast<sem_function *> (m_items[i])
2703                            ->compare_polymorphic_p ())))
2704              {
2705                 tree class_type
2706                   = TYPE_METHOD_BASETYPE (TREE_TYPE (m_items[i]->decl));
2707                 inchash::hash hstate (m_items[i]->get_hash ());
2708
2709                 if (TYPE_NAME (class_type)
2710                      && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (class_type)))
2711                   hstate.add_wide_int
2712                     (IDENTIFIER_HASH_VALUE
2713                        (DECL_ASSEMBLER_NAME (TYPE_NAME (class_type))));
2714
2715                 m_items[i]->set_hash (hstate.end ());
2716              }
2717         }
2718     }
2719
2720   /* Once all symbols have enhanced hash value, we can append
2721      hash values of symbols that are seen by IPA ICF and are
2722      references by a semantic item. Newly computed values
2723      are saved to global_hash member variable.  */
2724   for (unsigned i = 0; i < m_items.length (); i++)
2725     m_items[i]->update_hash_by_local_refs (m_symtab_node_map);
2726
2727   /* Global hash value replace current hash values.  */
2728   for (unsigned i = 0; i < m_items.length (); i++)
2729     m_items[i]->set_hash (m_items[i]->global_hash);
2730 }
2731
2732 /* Congruence classes are built by hash value.  */
2733
2734 void
2735 sem_item_optimizer::build_hash_based_classes (void)
2736 {
2737   for (unsigned i = 0; i < m_items.length (); i++)
2738     {
2739       sem_item *item = m_items[i];
2740
2741       congruence_class_group *group = get_group_by_hash (item->get_hash (),
2742                                       item->type);
2743
2744       if (!group->classes.length ())
2745         {
2746           m_classes_count++;
2747           group->classes.safe_push (new congruence_class (class_id++));
2748         }
2749
2750       add_item_to_class (group->classes[0], item);
2751     }
2752 }
2753
2754 /* Build references according to call graph.  */
2755
2756 void
2757 sem_item_optimizer::build_graph (void)
2758 {
2759   for (unsigned i = 0; i < m_items.length (); i++)
2760     {
2761       sem_item *item = m_items[i];
2762       m_symtab_node_map.put (item->node, item);
2763
2764       /* Initialize hash values if we are not in LTO mode.  */
2765       if (!in_lto_p)
2766         item->get_hash ();
2767     }
2768
2769   for (unsigned i = 0; i < m_items.length (); i++)
2770     {
2771       sem_item *item = m_items[i];
2772
2773       if (item->type == FUNC)
2774         {
2775           cgraph_node *cnode = dyn_cast <cgraph_node *> (item->node);
2776
2777           cgraph_edge *e = cnode->callees;
2778           while (e)
2779             {
2780               sem_item **slot = m_symtab_node_map.get
2781                 (e->callee->ultimate_alias_target ());
2782               if (slot)
2783                 item->add_reference (*slot);
2784
2785               e = e->next_callee;
2786             }
2787         }
2788
2789       ipa_ref *ref = NULL;
2790       for (unsigned i = 0; item->node->iterate_reference (i, ref); i++)
2791         {
2792           sem_item **slot = m_symtab_node_map.get
2793             (ref->referred->ultimate_alias_target ());
2794           if (slot)
2795             item->add_reference (*slot);
2796         }
2797     }
2798 }
2799
2800 /* Semantic items in classes having more than one element and initialized.
2801    In case of WPA, we load function body.  */
2802
2803 void
2804 sem_item_optimizer::parse_nonsingleton_classes (void)
2805 {
2806   unsigned int init_called_count = 0;
2807
2808   for (unsigned i = 0; i < m_items.length (); i++)
2809     if (m_items[i]->cls->members.length () > 1)
2810       {
2811         m_items[i]->init ();
2812         init_called_count++;
2813       }
2814
2815   if (dump_file)
2816     fprintf (dump_file, "Init called for %u items (%.2f%%).\n", init_called_count,
2817              m_items.length () ? 100.0f * init_called_count / m_items.length (): 0.0f);
2818 }
2819
2820 /* Equality function for semantic items is used to subdivide existing
2821    classes. If IN_WPA, fast equality function is invoked.  */
2822
2823 void
2824 sem_item_optimizer::subdivide_classes_by_equality (bool in_wpa)
2825 {
2826   for (hash_table <congruence_class_group_hash>::iterator it = m_classes.begin ();
2827        it != m_classes.end (); ++it)
2828     {
2829       unsigned int class_count = (*it)->classes.length ();
2830
2831       for (unsigned i = 0; i < class_count; i++)
2832         {
2833           congruence_class *c = (*it)->classes [i];
2834
2835           if (c->members.length() > 1)
2836             {
2837               auto_vec <sem_item *> new_vector;
2838
2839               sem_item *first = c->members[0];
2840               new_vector.safe_push (first);
2841
2842               unsigned class_split_first = (*it)->classes.length ();
2843
2844               for (unsigned j = 1; j < c->members.length (); j++)
2845                 {
2846                   sem_item *item = c->members[j];
2847
2848                   bool equals = in_wpa ? first->equals_wpa (item,
2849                                 m_symtab_node_map) : first->equals (item, m_symtab_node_map);
2850
2851                   if (equals)
2852                     new_vector.safe_push (item);
2853                   else
2854                     {
2855                       bool integrated = false;
2856
2857                       for (unsigned k = class_split_first; k < (*it)->classes.length (); k++)
2858                         {
2859                           sem_item *x = (*it)->classes[k]->members[0];
2860                           bool equals = in_wpa ? x->equals_wpa (item,
2861                                                                 m_symtab_node_map) : x->equals (item, m_symtab_node_map);
2862
2863                           if (equals)
2864                             {
2865                               integrated = true;
2866                               add_item_to_class ((*it)->classes[k], item);
2867
2868                               break;
2869                             }
2870                         }
2871
2872                       if (!integrated)
2873                         {
2874                           congruence_class *c = new congruence_class (class_id++);
2875                           m_classes_count++;
2876                           add_item_to_class (c, item);
2877
2878                           (*it)->classes.safe_push (c);
2879                         }
2880                     }
2881                 }
2882
2883               // we replace newly created new_vector for the class we've just splitted
2884               c->members.release ();
2885               c->members.create (new_vector.length ());
2886
2887               for (unsigned int j = 0; j < new_vector.length (); j++)
2888                 add_item_to_class (c, new_vector[j]);
2889             }
2890         }
2891     }
2892
2893   checking_verify_classes ();
2894 }
2895
2896 /* Subdivide classes by address references that members of the class
2897    reference. Example can be a pair of functions that have an address
2898    taken from a function. If these addresses are different the class
2899    is split.  */
2900
2901 unsigned
2902 sem_item_optimizer::subdivide_classes_by_sensitive_refs ()
2903 {
2904   typedef hash_map <symbol_compare_hash, vec <sem_item *> > subdivide_hash_map;
2905
2906   unsigned newly_created_classes = 0;
2907
2908   for (hash_table <congruence_class_group_hash>::iterator it = m_classes.begin ();
2909        it != m_classes.end (); ++it)
2910     {
2911       unsigned int class_count = (*it)->classes.length ();
2912       auto_vec<congruence_class *> new_classes;
2913
2914       for (unsigned i = 0; i < class_count; i++)
2915         {
2916           congruence_class *c = (*it)->classes [i];
2917
2918           if (c->members.length() > 1)
2919             {
2920               subdivide_hash_map split_map;
2921
2922               for (unsigned j = 0; j < c->members.length (); j++)
2923                 {
2924                   sem_item *source_node = c->members[j];
2925
2926                   symbol_compare_collection *collection = new symbol_compare_collection (source_node->node);
2927
2928                   bool existed;
2929                   vec <sem_item *> *slot = &split_map.get_or_insert (collection,
2930                                                                      &existed);
2931                   gcc_checking_assert (slot);
2932
2933                   slot->safe_push (source_node);
2934
2935                   if (existed)
2936                     delete collection;
2937                 }
2938
2939                /* If the map contains more than one key, we have to split the map
2940                   appropriately.  */
2941               if (split_map.elements () != 1)
2942                 {
2943                   bool first_class = true;
2944
2945                   for (subdivide_hash_map::iterator it2 = split_map.begin ();
2946                        it2 != split_map.end (); ++it2)
2947                     {
2948                       congruence_class *new_cls;
2949                       new_cls = new congruence_class (class_id++);
2950
2951                       for (unsigned k = 0; k < (*it2).second.length (); k++)
2952                         add_item_to_class (new_cls, (*it2).second[k]);
2953
2954                       worklist_push (new_cls);
2955                       newly_created_classes++;
2956
2957                       if (first_class)
2958                         {
2959                           (*it)->classes[i] = new_cls;
2960                           first_class = false;
2961                         }
2962                       else
2963                         {
2964                           new_classes.safe_push (new_cls);
2965                           m_classes_count++;
2966                         }
2967                     }
2968                 }
2969
2970               /* Release memory.  */
2971               for (subdivide_hash_map::iterator it2 = split_map.begin ();
2972                    it2 != split_map.end (); ++it2)
2973                 {
2974                   delete (*it2).first;
2975                   (*it2).second.release ();
2976                 }
2977             }
2978           }
2979
2980         for (unsigned i = 0; i < new_classes.length (); i++)
2981           (*it)->classes.safe_push (new_classes[i]);
2982     }
2983
2984   return newly_created_classes;
2985 }
2986
2987 /* Verify congruence classes, if checking is enabled.  */
2988
2989 void
2990 sem_item_optimizer::checking_verify_classes (void)
2991 {
2992   if (flag_checking)
2993     verify_classes ();
2994 }
2995
2996 /* Verify congruence classes.  */
2997
2998 void
2999 sem_item_optimizer::verify_classes (void)
3000 {
3001   for (hash_table <congruence_class_group_hash>::iterator it = m_classes.begin ();
3002        it != m_classes.end (); ++it)
3003     {
3004       for (unsigned int i = 0; i < (*it)->classes.length (); i++)
3005         {
3006           congruence_class *cls = (*it)->classes[i];
3007
3008           gcc_assert (cls);
3009           gcc_assert (cls->members.length () > 0);
3010
3011           for (unsigned int j = 0; j < cls->members.length (); j++)
3012             {
3013               sem_item *item = cls->members[j];
3014
3015               gcc_assert (item);
3016               gcc_assert (item->cls == cls);
3017
3018               for (unsigned k = 0; k < item->usages.length (); k++)
3019                 {
3020                   sem_usage_pair *usage = item->usages[k];
3021                   gcc_assert (usage->item->index_in_class <
3022                               usage->item->cls->members.length ());
3023                 }
3024             }
3025         }
3026     }
3027 }
3028
3029 /* Disposes split map traverse function. CLS_PTR is pointer to congruence
3030    class, BSLOT is bitmap slot we want to release. DATA is mandatory,
3031    but unused argument.  */
3032
3033 bool
3034 sem_item_optimizer::release_split_map (congruence_class * const &,
3035                                        bitmap const &b, traverse_split_pair *)
3036 {
3037   bitmap bmp = b;
3038
3039   BITMAP_FREE (bmp);
3040
3041   return true;
3042 }
3043
3044 /* Process split operation for a class given as pointer CLS_PTR,
3045    where bitmap B splits congruence class members. DATA is used
3046    as argument of split pair.  */
3047
3048 bool
3049 sem_item_optimizer::traverse_congruence_split (congruence_class * const &cls,
3050     bitmap const &b, traverse_split_pair *pair)
3051 {
3052   sem_item_optimizer *optimizer = pair->optimizer;
3053   const congruence_class *splitter_cls = pair->cls;
3054
3055   /* If counted bits are greater than zero and less than the number of members
3056      a group will be splitted.  */
3057   unsigned popcount = bitmap_count_bits (b);
3058
3059   if (popcount > 0 && popcount < cls->members.length ())
3060     {
3061       auto_vec <congruence_class *, 2> newclasses;
3062       newclasses.quick_push (new congruence_class (class_id++));
3063       newclasses.quick_push (new congruence_class (class_id++));
3064
3065       for (unsigned int i = 0; i < cls->members.length (); i++)
3066         {
3067           int target = bitmap_bit_p (b, i);
3068           congruence_class *tc = newclasses[target];
3069
3070           add_item_to_class (tc, cls->members[i]);
3071         }
3072
3073       if (flag_checking)
3074         {
3075           for (unsigned int i = 0; i < 2; i++)
3076             gcc_assert (newclasses[i]->members.length ());
3077         }
3078
3079       if (splitter_cls == cls)
3080         optimizer->splitter_class_removed = true;
3081
3082       /* Remove old class from worklist if presented.  */
3083       bool in_worklist = cls->in_worklist;
3084
3085       if (in_worklist)
3086         cls->in_worklist = false;
3087
3088       congruence_class_group g;
3089       g.hash = cls->members[0]->get_hash ();
3090       g.type = cls->members[0]->type;
3091
3092       congruence_class_group *slot = optimizer->m_classes.find(&g);
3093
3094       for (unsigned int i = 0; i < slot->classes.length (); i++)
3095         if (slot->classes[i] == cls)
3096           {
3097             slot->classes.ordered_remove (i);
3098             break;
3099           }
3100
3101       /* New class will be inserted and integrated to work list.  */
3102       for (unsigned int i = 0; i < 2; i++)
3103         optimizer->add_class (newclasses[i]);
3104
3105       /* Two classes replace one, so that increment just by one.  */
3106       optimizer->m_classes_count++;
3107
3108       /* If OLD class was presented in the worklist, we remove the class
3109          and replace it will both newly created classes.  */
3110       if (in_worklist)
3111         for (unsigned int i = 0; i < 2; i++)
3112           optimizer->worklist_push (newclasses[i]);
3113       else /* Just smaller class is inserted.  */
3114         {
3115           unsigned int smaller_index = newclasses[0]->members.length () <
3116                                        newclasses[1]->members.length () ?
3117                                        0 : 1;
3118           optimizer->worklist_push (newclasses[smaller_index]);
3119         }
3120
3121       if (dump_file && (dump_flags & TDF_DETAILS))
3122         {
3123           fprintf (dump_file, "  congruence class splitted:\n");
3124           cls->dump (dump_file, 4);
3125
3126           fprintf (dump_file, "  newly created groups:\n");
3127           for (unsigned int i = 0; i < 2; i++)
3128             newclasses[i]->dump (dump_file, 4);
3129         }
3130
3131       /* Release class if not presented in work list.  */
3132       if (!in_worklist)
3133         delete cls;
3134     }
3135
3136
3137   return true;
3138 }
3139
3140 /* Tests if a class CLS used as INDEXth splits any congruence classes.
3141    Bitmap stack BMSTACK is used for bitmap allocation.  */
3142
3143 void
3144 sem_item_optimizer::do_congruence_step_for_index (congruence_class *cls,
3145     unsigned int index)
3146 {
3147   hash_map <congruence_class *, bitmap> split_map;
3148
3149   for (unsigned int i = 0; i < cls->members.length (); i++)
3150     {
3151       sem_item *item = cls->members[i];
3152
3153       /* Iterate all usages that have INDEX as usage of the item.  */
3154       for (unsigned int j = 0; j < item->usages.length (); j++)
3155         {
3156           sem_usage_pair *usage = item->usages[j];
3157
3158           if (usage->index != index)
3159             continue;
3160
3161           bitmap *slot = split_map.get (usage->item->cls);
3162           bitmap b;
3163
3164           if(!slot)
3165             {
3166               b = BITMAP_ALLOC (&m_bmstack);
3167               split_map.put (usage->item->cls, b);
3168             }
3169           else
3170             b = *slot;
3171
3172           gcc_checking_assert (usage->item->cls);
3173           gcc_checking_assert (usage->item->index_in_class <
3174                                usage->item->cls->members.length ());
3175
3176           bitmap_set_bit (b, usage->item->index_in_class);
3177         }
3178     }
3179
3180   traverse_split_pair pair;
3181   pair.optimizer = this;
3182   pair.cls = cls;
3183
3184   splitter_class_removed = false;
3185   split_map.traverse
3186   <traverse_split_pair *, sem_item_optimizer::traverse_congruence_split> (&pair);
3187
3188   /* Bitmap clean-up.  */
3189   split_map.traverse
3190   <traverse_split_pair *, sem_item_optimizer::release_split_map> (NULL);
3191 }
3192
3193 /* Every usage of a congruence class CLS is a candidate that can split the
3194    collection of classes. Bitmap stack BMSTACK is used for bitmap
3195    allocation.  */
3196
3197 void
3198 sem_item_optimizer::do_congruence_step (congruence_class *cls)
3199 {
3200   bitmap_iterator bi;
3201   unsigned int i;
3202
3203   bitmap usage = BITMAP_ALLOC (&m_bmstack);
3204
3205   for (unsigned int i = 0; i < cls->members.length (); i++)
3206     bitmap_ior_into (usage, cls->members[i]->usage_index_bitmap);
3207
3208   EXECUTE_IF_SET_IN_BITMAP (usage, 0, i, bi)
3209   {
3210     if (dump_file && (dump_flags & TDF_DETAILS))
3211       fprintf (dump_file, "  processing congruence step for class: %u, index: %u\n",
3212                cls->id, i);
3213
3214     do_congruence_step_for_index (cls, i);
3215
3216     if (splitter_class_removed)
3217       break;
3218   }
3219
3220   BITMAP_FREE (usage);
3221 }
3222
3223 /* Adds a newly created congruence class CLS to worklist.  */
3224
3225 void
3226 sem_item_optimizer::worklist_push (congruence_class *cls)
3227 {
3228   /* Return if the class CLS is already presented in work list.  */
3229   if (cls->in_worklist)
3230     return;
3231
3232   cls->in_worklist = true;
3233   worklist.push_back (cls);
3234 }
3235
3236 /* Pops a class from worklist. */
3237
3238 congruence_class *
3239 sem_item_optimizer::worklist_pop (void)
3240 {
3241   congruence_class *cls;
3242
3243   while (!worklist.empty ())
3244     {
3245       cls = worklist.front ();
3246       worklist.pop_front ();
3247       if (cls->in_worklist)
3248         {
3249           cls->in_worklist = false;
3250
3251           return cls;
3252         }
3253       else
3254         {
3255           /* Work list item was already intended to be removed.
3256              The only reason for doing it is to split a class.
3257              Thus, the class CLS is deleted.  */
3258           delete cls;
3259         }
3260     }
3261
3262   return NULL;
3263 }
3264
3265 /* Iterative congruence reduction function.  */
3266
3267 void
3268 sem_item_optimizer::process_cong_reduction (void)
3269 {
3270   for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
3271        it != m_classes.end (); ++it)
3272     for (unsigned i = 0; i < (*it)->classes.length (); i++)
3273       if ((*it)->classes[i]->is_class_used ())
3274         worklist_push ((*it)->classes[i]);
3275
3276   if (dump_file)
3277     fprintf (dump_file, "Worklist has been filled with: %lu\n",
3278              (unsigned long) worklist.size ());
3279
3280   if (dump_file && (dump_flags & TDF_DETAILS))
3281     fprintf (dump_file, "Congruence class reduction\n");
3282
3283   congruence_class *cls;
3284
3285   /* Process complete congruence reduction.  */
3286   while ((cls = worklist_pop ()) != NULL)
3287     do_congruence_step (cls);
3288
3289   /* Subdivide newly created classes according to references.  */
3290   unsigned new_classes = subdivide_classes_by_sensitive_refs ();
3291
3292   if (dump_file)
3293     fprintf (dump_file, "Address reference subdivision created: %u "
3294              "new classes.\n", new_classes);
3295 }
3296
3297 /* Debug function prints all informations about congruence classes.  */
3298
3299 void
3300 sem_item_optimizer::dump_cong_classes (void)
3301 {
3302   if (!dump_file)
3303     return;
3304
3305   fprintf (dump_file,
3306            "Congruence classes: %u (unique hash values: %lu), with total: %u items\n",
3307            m_classes_count, (unsigned long) m_classes.elements(), m_items.length ());
3308
3309   /* Histogram calculation.  */
3310   unsigned int max_index = 0;
3311   unsigned int* histogram = XCNEWVEC (unsigned int, m_items.length () + 1);
3312
3313   for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
3314        it != m_classes.end (); ++it)
3315
3316     for (unsigned i = 0; i < (*it)->classes.length (); i++)
3317       {
3318         unsigned int c = (*it)->classes[i]->members.length ();
3319         histogram[c]++;
3320
3321         if (c > max_index)
3322           max_index = c;
3323       }
3324
3325   fprintf (dump_file,
3326            "Class size histogram [num of members]: number of classe number of classess\n");
3327
3328   for (unsigned int i = 0; i <= max_index; i++)
3329     if (histogram[i])
3330       fprintf (dump_file, "[%u]: %u classes\n", i, histogram[i]);
3331
3332   fprintf (dump_file, "\n\n");
3333
3334
3335   if (dump_flags & TDF_DETAILS)
3336     for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
3337          it != m_classes.end (); ++it)
3338       {
3339         fprintf (dump_file, "  group: with %u classes:\n", (*it)->classes.length ());
3340
3341         for (unsigned i = 0; i < (*it)->classes.length (); i++)
3342           {
3343             (*it)->classes[i]->dump (dump_file, 4);
3344
3345             if(i < (*it)->classes.length () - 1)
3346               fprintf (dump_file, " ");
3347           }
3348       }
3349
3350   free (histogram);
3351 }
3352
3353 /* After reduction is done, we can declare all items in a group
3354    to be equal. PREV_CLASS_COUNT is start number of classes
3355    before reduction. True is returned if there's a merge operation
3356    processed. */
3357
3358 bool
3359 sem_item_optimizer::merge_classes (unsigned int prev_class_count)
3360 {
3361   unsigned int item_count = m_items.length ();
3362   unsigned int class_count = m_classes_count;
3363   unsigned int equal_items = item_count - class_count;
3364
3365   unsigned int non_singular_classes_count = 0;
3366   unsigned int non_singular_classes_sum = 0;
3367
3368   bool merged_p = false;
3369
3370   for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
3371        it != m_classes.end (); ++it)
3372     for (unsigned int i = 0; i < (*it)->classes.length (); i++)
3373       {
3374         congruence_class *c = (*it)->classes[i];
3375         if (c->members.length () > 1)
3376           {
3377             non_singular_classes_count++;
3378             non_singular_classes_sum += c->members.length ();
3379           }
3380       }
3381
3382   if (dump_file)
3383     {
3384       fprintf (dump_file, "\nItem count: %u\n", item_count);
3385       fprintf (dump_file, "Congruent classes before: %u, after: %u\n",
3386                prev_class_count, class_count);
3387       fprintf (dump_file, "Average class size before: %.2f, after: %.2f\n",
3388                prev_class_count ? 1.0f * item_count / prev_class_count : 0.0f,
3389                class_count ? 1.0f * item_count / class_count : 0.0f);
3390       fprintf (dump_file, "Average non-singular class size: %.2f, count: %u\n",
3391                non_singular_classes_count ? 1.0f * non_singular_classes_sum /
3392                non_singular_classes_count : 0.0f,
3393                non_singular_classes_count);
3394       fprintf (dump_file, "Equal symbols: %u\n", equal_items);
3395       fprintf (dump_file, "Fraction of visited symbols: %.2f%%\n\n",
3396                item_count ? 100.0f * equal_items / item_count : 0.0f);
3397     }
3398
3399   for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
3400        it != m_classes.end (); ++it)
3401     for (unsigned int i = 0; i < (*it)->classes.length (); i++)
3402       {
3403         congruence_class *c = (*it)->classes[i];
3404
3405         if (c->members.length () == 1)
3406           continue;
3407
3408         sem_item *source = c->members[0];
3409
3410         if (DECL_NAME (source->decl)
3411             && MAIN_NAME_P (DECL_NAME (source->decl)))
3412           /* If merge via wrappers, picking main as the target can be
3413              problematic.  */
3414           source = c->members[1];
3415
3416         for (unsigned int j = 0; j < c->members.length (); j++)
3417           {
3418             sem_item *alias = c->members[j];
3419
3420             if (alias == source)
3421               continue;
3422
3423             if (dump_file)
3424               {
3425                 fprintf (dump_file, "Semantic equality hit:%s->%s\n",
3426                          xstrdup_for_dump (source->node->name ()),
3427                          xstrdup_for_dump (alias->node->name ()));
3428                 fprintf (dump_file, "Assembler symbol names:%s->%s\n",
3429                          xstrdup_for_dump (source->node->asm_name ()),
3430                          xstrdup_for_dump (alias->node->asm_name ()));
3431               }
3432
3433             if (lookup_attribute ("no_icf", DECL_ATTRIBUTES (alias->decl)))
3434               {
3435                 if (dump_file)
3436                   fprintf (dump_file,
3437                            "Merge operation is skipped due to no_icf "
3438                            "attribute.\n\n");
3439
3440                 continue;
3441               }
3442
3443             if (dump_file && (dump_flags & TDF_DETAILS))
3444               {
3445                 source->dump_to_file (dump_file);
3446                 alias->dump_to_file (dump_file);
3447               }
3448
3449             if (dbg_cnt (merged_ipa_icf))
3450               merged_p |= source->merge (alias);
3451           }
3452       }
3453
3454   return merged_p;
3455 }
3456
3457 /* Dump function prints all class members to a FILE with an INDENT.  */
3458
3459 void
3460 congruence_class::dump (FILE *file, unsigned int indent) const
3461 {
3462   FPRINTF_SPACES (file, indent, "class with id: %u, hash: %u, items: %u\n",
3463                   id, members[0]->get_hash (), members.length ());
3464
3465   FPUTS_SPACES (file, indent + 2, "");
3466   for (unsigned i = 0; i < members.length (); i++)
3467     fprintf (file, "%s(%p/%u) ", members[i]->node->asm_name (),
3468              (void *) members[i]->decl,
3469              members[i]->node->order);
3470
3471   fprintf (file, "\n");
3472 }
3473
3474 /* Returns true if there's a member that is used from another group.  */
3475
3476 bool
3477 congruence_class::is_class_used (void)
3478 {
3479   for (unsigned int i = 0; i < members.length (); i++)
3480     if (members[i]->usages.length ())
3481       return true;
3482
3483   return false;
3484 }
3485
3486 /* Generate pass summary for IPA ICF pass.  */
3487
3488 static void
3489 ipa_icf_generate_summary (void)
3490 {
3491   if (!optimizer)
3492     optimizer = new sem_item_optimizer ();
3493
3494   optimizer->register_hooks ();
3495   optimizer->parse_funcs_and_vars ();
3496 }
3497
3498 /* Write pass summary for IPA ICF pass.  */
3499
3500 static void
3501 ipa_icf_write_summary (void)
3502 {
3503   gcc_assert (optimizer);
3504
3505   optimizer->write_summary ();
3506 }
3507
3508 /* Read pass summary for IPA ICF pass.  */
3509
3510 static void
3511 ipa_icf_read_summary (void)
3512 {
3513   if (!optimizer)
3514     optimizer = new sem_item_optimizer ();
3515
3516   optimizer->read_summary ();
3517   optimizer->register_hooks ();
3518 }
3519
3520 /* Semantic equality exection function.  */
3521
3522 static unsigned int
3523 ipa_icf_driver (void)
3524 {
3525   gcc_assert (optimizer);
3526
3527   bool merged_p = optimizer->execute ();
3528
3529   delete optimizer;
3530   optimizer = NULL;
3531
3532   return merged_p ? TODO_remove_functions : 0;
3533 }
3534
3535 const pass_data pass_data_ipa_icf =
3536 {
3537   IPA_PASS,                 /* type */
3538   "icf",                    /* name */
3539   OPTGROUP_IPA,             /* optinfo_flags */
3540   TV_IPA_ICF,               /* tv_id */
3541   0,                        /* properties_required */
3542   0,                        /* properties_provided */
3543   0,                        /* properties_destroyed */
3544   0,                        /* todo_flags_start */
3545   0,                        /* todo_flags_finish */
3546 };
3547
3548 class pass_ipa_icf : public ipa_opt_pass_d
3549 {
3550 public:
3551   pass_ipa_icf (gcc::context *ctxt)
3552     : ipa_opt_pass_d (pass_data_ipa_icf, ctxt,
3553                       ipa_icf_generate_summary, /* generate_summary */
3554                       ipa_icf_write_summary, /* write_summary */
3555                       ipa_icf_read_summary, /* read_summary */
3556                       NULL, /*
3557                       write_optimization_summary */
3558                       NULL, /*
3559                       read_optimization_summary */
3560                       NULL, /* stmt_fixup */
3561                       0, /* function_transform_todo_flags_start */
3562                       NULL, /* function_transform */
3563                       NULL) /* variable_transform */
3564   {}
3565
3566   /* opt_pass methods: */
3567   virtual bool gate (function *)
3568   {
3569     return in_lto_p || flag_ipa_icf_variables || flag_ipa_icf_functions;
3570   }
3571
3572   virtual unsigned int execute (function *)
3573   {
3574     return ipa_icf_driver();
3575   }
3576 }; // class pass_ipa_icf
3577
3578 } // ipa_icf namespace
3579
3580 ipa_opt_pass_d *
3581 make_pass_ipa_icf (gcc::context *ctxt)
3582 {
3583   return new ipa_icf::pass_ipa_icf (ctxt);
3584 }