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