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