testsuite: Correct requirements for vadsdu*, vslv and vsrv testcases.
[official-gcc.git] / gcc / ipa-icf.c
blob8cae076b3cef806008c280d061dca483c003b6b3
1 /* Interprocedural Identical Code Folding pass
2 Copyright (C) 2014-2020 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka <hubicka@ucw.cz> and Martin Liska <mliska@suse.cz>
6 This file is part of GCC.
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.
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.
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/>. */
22 /* Interprocedural Identical Code Folding for functions and
23 read-only variables.
25 The goal of this transformation is to discover functions and read-only
26 variables which do have exactly the same semantics.
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.
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.
54 #include "config.h"
55 #include "system.h"
56 #include "coretypes.h"
57 #include "backend.h"
58 #include "target.h"
59 #include "rtl.h"
60 #include "tree.h"
61 #include "gimple.h"
62 #include "alloc-pool.h"
63 #include "tree-pass.h"
64 #include "ssa.h"
65 #include "cgraph.h"
66 #include "coverage.h"
67 #include "gimple-pretty-print.h"
68 #include "data-streamer.h"
69 #include "fold-const.h"
70 #include "calls.h"
71 #include "varasm.h"
72 #include "gimple-iterator.h"
73 #include "tree-cfg.h"
74 #include "symbol-summary.h"
75 #include "ipa-prop.h"
76 #include "ipa-fnsummary.h"
77 #include "except.h"
78 #include "attribs.h"
79 #include "print-tree.h"
80 #include "ipa-utils.h"
81 #include "ipa-icf-gimple.h"
82 #include "fibonacci_heap.h"
83 #include "ipa-icf.h"
84 #include "stor-layout.h"
85 #include "dbgcnt.h"
86 #include "tree-vector-builder.h"
87 #include "symtab-thunks.h"
89 using namespace ipa_icf_gimple;
91 namespace ipa_icf {
93 /* Initialization and computation of symtab node hash, there data
94 are propagated later on. */
96 static sem_item_optimizer *optimizer = NULL;
98 /* Constructor. */
100 symbol_compare_collection::symbol_compare_collection (symtab_node *node)
102 m_references.create (0);
103 m_interposables.create (0);
105 ipa_ref *ref;
107 if (is_a <varpool_node *> (node) && DECL_VIRTUAL_P (node->decl))
108 return;
110 for (unsigned i = 0; node->iterate_reference (i, ref); i++)
112 if (ref->address_matters_p ())
113 m_references.safe_push (ref->referred);
115 if (ref->referred->get_availability () <= AVAIL_INTERPOSABLE)
117 if (ref->address_matters_p ())
118 m_references.safe_push (ref->referred);
119 else
120 m_interposables.safe_push (ref->referred);
124 if (is_a <cgraph_node *> (node))
126 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
128 for (cgraph_edge *e = cnode->callees; e; e = e->next_callee)
129 if (e->callee->get_availability () <= AVAIL_INTERPOSABLE)
130 m_interposables.safe_push (e->callee);
134 /* Constructor for key value pair, where _ITEM is key and _INDEX is a target. */
136 sem_usage_pair::sem_usage_pair (sem_item *_item, unsigned int _index)
137 : item (_item), index (_index)
141 sem_item::sem_item (sem_item_type _type, bitmap_obstack *stack)
142 : type (_type), referenced_by_count (0), m_hash (-1), m_hash_set (false)
144 setup (stack);
147 sem_item::sem_item (sem_item_type _type, symtab_node *_node,
148 bitmap_obstack *stack)
149 : type (_type), node (_node), referenced_by_count (0), m_hash (-1),
150 m_hash_set (false)
152 decl = node->decl;
153 setup (stack);
156 /* Add reference to a semantic TARGET. */
158 void
159 sem_item::add_reference (ref_map *refs,
160 sem_item *target)
162 unsigned index = reference_count++;
163 bool existed;
165 vec<sem_item *> &v
166 = refs->get_or_insert (new sem_usage_pair (target, index), &existed);
167 v.safe_push (this);
168 bitmap_set_bit (target->usage_index_bitmap, index);
169 refs_set.add (target->node);
170 ++target->referenced_by_count;
173 /* Initialize internal data structures. Bitmap STACK is used for
174 bitmap memory allocation process. */
176 void
177 sem_item::setup (bitmap_obstack *stack)
179 gcc_checking_assert (node);
181 reference_count = 0;
182 tree_refs.create (0);
183 usage_index_bitmap = BITMAP_ALLOC (stack);
186 sem_item::~sem_item ()
188 tree_refs.release ();
190 BITMAP_FREE (usage_index_bitmap);
193 /* Dump function for debugging purpose. */
195 DEBUG_FUNCTION void
196 sem_item::dump (void)
198 if (dump_file)
200 fprintf (dump_file, "[%s] %s (tree:%p)\n", type == FUNC ? "func" : "var",
201 node->dump_name (), (void *) node->decl);
202 fprintf (dump_file, " hash: %u\n", get_hash ());
206 /* Return true if target supports alias symbols. */
208 bool
209 sem_item::target_supports_symbol_aliases_p (void)
211 #if !defined (ASM_OUTPUT_DEF) || (!defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL))
212 return false;
213 #else
214 return true;
215 #endif
218 void sem_item::set_hash (hashval_t hash)
220 m_hash = hash;
221 m_hash_set = true;
224 hash_map<const_tree, hashval_t> sem_item::m_type_hash_cache;
226 /* Semantic function constructor that uses STACK as bitmap memory stack. */
228 sem_function::sem_function (bitmap_obstack *stack)
229 : sem_item (FUNC, stack), m_checker (NULL), m_compared_func (NULL)
231 bb_sizes.create (0);
232 bb_sorted.create (0);
235 sem_function::sem_function (cgraph_node *node, bitmap_obstack *stack)
236 : sem_item (FUNC, node, stack), m_checker (NULL), m_compared_func (NULL)
238 bb_sizes.create (0);
239 bb_sorted.create (0);
242 sem_function::~sem_function ()
244 for (unsigned i = 0; i < bb_sorted.length (); i++)
245 delete (bb_sorted[i]);
247 bb_sizes.release ();
248 bb_sorted.release ();
251 /* Calculates hash value based on a BASIC_BLOCK. */
253 hashval_t
254 sem_function::get_bb_hash (const sem_bb *basic_block)
256 inchash::hash hstate;
258 hstate.add_int (basic_block->nondbg_stmt_count);
259 hstate.add_int (basic_block->edge_count);
261 return hstate.end ();
264 /* References independent hash function. */
266 hashval_t
267 sem_function::get_hash (void)
269 if (!m_hash_set)
271 inchash::hash hstate;
272 hstate.add_int (177454); /* Random number for function type. */
274 hstate.add_int (arg_count);
275 hstate.add_int (cfg_checksum);
276 hstate.add_int (gcode_hash);
278 for (unsigned i = 0; i < bb_sorted.length (); i++)
279 hstate.merge_hash (get_bb_hash (bb_sorted[i]));
281 for (unsigned i = 0; i < bb_sizes.length (); i++)
282 hstate.add_int (bb_sizes[i]);
284 /* Add common features of declaration itself. */
285 if (DECL_FUNCTION_SPECIFIC_TARGET (decl))
286 hstate.add_hwi
287 (cl_target_option_hash
288 (TREE_TARGET_OPTION (DECL_FUNCTION_SPECIFIC_TARGET (decl))));
289 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))
290 hstate.add_hwi
291 (cl_optimization_hash
292 (TREE_OPTIMIZATION (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))));
293 hstate.add_flag (DECL_CXX_CONSTRUCTOR_P (decl));
294 hstate.add_flag (DECL_CXX_DESTRUCTOR_P (decl));
296 set_hash (hstate.end ());
299 return m_hash;
302 /* Compare properties of symbols N1 and N2 that does not affect semantics of
303 symbol itself but affects semantics of its references from USED_BY (which
304 may be NULL if it is unknown). If comparison is false, symbols
305 can still be merged but any symbols referring them can't.
307 If ADDRESS is true, do extra checking needed for IPA_REF_ADDR.
309 TODO: We can also split attributes to those that determine codegen of
310 a function body/variable constructor itself and those that are used when
311 referring to it. */
313 bool
314 sem_item::compare_referenced_symbol_properties (symtab_node *used_by,
315 symtab_node *n1,
316 symtab_node *n2,
317 bool address)
319 if (is_a <cgraph_node *> (n1))
321 /* Inline properties matters: we do now want to merge uses of inline
322 function to uses of normal function because inline hint would be lost.
323 We however can merge inline function to noinline because the alias
324 will keep its DECL_DECLARED_INLINE flag.
326 Also ignore inline flag when optimizing for size or when function
327 is known to not be inlinable.
329 TODO: the optimize_size checks can also be assumed to be true if
330 unit has no !optimize_size functions. */
332 if ((!used_by || address || !is_a <cgraph_node *> (used_by)
333 || !opt_for_fn (used_by->decl, optimize_size))
334 && !opt_for_fn (n1->decl, optimize_size)
335 && n1->get_availability () > AVAIL_INTERPOSABLE
336 && (!DECL_UNINLINABLE (n1->decl) || !DECL_UNINLINABLE (n2->decl)))
338 if (DECL_DISREGARD_INLINE_LIMITS (n1->decl)
339 != DECL_DISREGARD_INLINE_LIMITS (n2->decl))
340 return return_false_with_msg
341 ("DECL_DISREGARD_INLINE_LIMITS are different");
343 if (DECL_DECLARED_INLINE_P (n1->decl)
344 != DECL_DECLARED_INLINE_P (n2->decl))
345 return return_false_with_msg ("inline attributes are different");
348 if (DECL_IS_OPERATOR_NEW_P (n1->decl)
349 != DECL_IS_OPERATOR_NEW_P (n2->decl))
350 return return_false_with_msg ("operator new flags are different");
352 if (DECL_IS_REPLACEABLE_OPERATOR (n1->decl)
353 != DECL_IS_REPLACEABLE_OPERATOR (n2->decl))
354 return return_false_with_msg ("replaceable operator flags are different");
357 /* Merging two definitions with a reference to equivalent vtables, but
358 belonging to a different type may result in ipa-polymorphic-call analysis
359 giving a wrong answer about the dynamic type of instance. */
360 if (is_a <varpool_node *> (n1))
362 if ((DECL_VIRTUAL_P (n1->decl) || DECL_VIRTUAL_P (n2->decl))
363 && (DECL_VIRTUAL_P (n1->decl) != DECL_VIRTUAL_P (n2->decl)
364 || !types_must_be_same_for_odr (DECL_CONTEXT (n1->decl),
365 DECL_CONTEXT (n2->decl)))
366 && (!used_by || !is_a <cgraph_node *> (used_by) || address
367 || opt_for_fn (used_by->decl, flag_devirtualize)))
368 return return_false_with_msg
369 ("references to virtual tables cannot be merged");
371 if (address && DECL_ALIGN (n1->decl) != DECL_ALIGN (n2->decl))
372 return return_false_with_msg ("alignment mismatch");
374 /* For functions we compare attributes in equals_wpa, because we do
375 not know what attributes may cause codegen differences, but for
376 variables just compare attributes for references - the codegen
377 for constructors is affected only by those attributes that we lower
378 to explicit representation (such as DECL_ALIGN or DECL_SECTION). */
379 if (!attribute_list_equal (DECL_ATTRIBUTES (n1->decl),
380 DECL_ATTRIBUTES (n2->decl)))
381 return return_false_with_msg ("different var decl attributes");
382 if (comp_type_attributes (TREE_TYPE (n1->decl),
383 TREE_TYPE (n2->decl)) != 1)
384 return return_false_with_msg ("different var type attributes");
387 /* When matching virtual tables, be sure to also match information
388 relevant for polymorphic call analysis. */
389 if (used_by && is_a <varpool_node *> (used_by)
390 && DECL_VIRTUAL_P (used_by->decl))
392 if (DECL_VIRTUAL_P (n1->decl) != DECL_VIRTUAL_P (n2->decl))
393 return return_false_with_msg ("virtual flag mismatch");
394 if (DECL_VIRTUAL_P (n1->decl) && is_a <cgraph_node *> (n1)
395 && (DECL_FINAL_P (n1->decl) != DECL_FINAL_P (n2->decl)))
396 return return_false_with_msg ("final flag mismatch");
398 return true;
401 /* Hash properties that are compared by compare_referenced_symbol_properties. */
403 void
404 sem_item::hash_referenced_symbol_properties (symtab_node *ref,
405 inchash::hash &hstate,
406 bool address)
408 if (is_a <cgraph_node *> (ref))
410 if ((type != FUNC || address || !opt_for_fn (decl, optimize_size))
411 && !opt_for_fn (ref->decl, optimize_size)
412 && !DECL_UNINLINABLE (ref->decl))
414 hstate.add_flag (DECL_DISREGARD_INLINE_LIMITS (ref->decl));
415 hstate.add_flag (DECL_DECLARED_INLINE_P (ref->decl));
417 hstate.add_flag (DECL_IS_OPERATOR_NEW_P (ref->decl));
419 else if (is_a <varpool_node *> (ref))
421 hstate.add_flag (DECL_VIRTUAL_P (ref->decl));
422 if (address)
423 hstate.add_int (DECL_ALIGN (ref->decl));
428 /* For a given symbol table nodes N1 and N2, we check that FUNCTION_DECLs
429 point to a same function. Comparison can be skipped if IGNORED_NODES
430 contains these nodes. ADDRESS indicate if address is taken. */
432 bool
433 sem_item::compare_symbol_references (
434 hash_map <symtab_node *, sem_item *> &ignored_nodes,
435 symtab_node *n1, symtab_node *n2, bool address)
437 enum availability avail1, avail2;
439 if (n1 == n2)
440 return true;
442 /* Never match variable and function. */
443 if (is_a <varpool_node *> (n1) != is_a <varpool_node *> (n2))
444 return false;
446 if (!compare_referenced_symbol_properties (node, n1, n2, address))
447 return false;
448 if (address && n1->equal_address_to (n2) == 1)
449 return true;
450 if (!address && n1->semantically_equivalent_p (n2))
451 return true;
453 n1 = n1->ultimate_alias_target (&avail1);
454 n2 = n2->ultimate_alias_target (&avail2);
456 if (avail1 > AVAIL_INTERPOSABLE && ignored_nodes.get (n1)
457 && avail2 > AVAIL_INTERPOSABLE && ignored_nodes.get (n2))
458 return true;
460 return return_false_with_msg ("different references");
463 /* If cgraph edges E1 and E2 are indirect calls, verify that
464 ECF flags are the same. */
466 bool sem_function::compare_edge_flags (cgraph_edge *e1, cgraph_edge *e2)
468 if (e1->indirect_info && e2->indirect_info)
470 int e1_flags = e1->indirect_info->ecf_flags;
471 int e2_flags = e2->indirect_info->ecf_flags;
473 if (e1_flags != e2_flags)
474 return return_false_with_msg ("ICF flags are different");
476 else if (e1->indirect_info || e2->indirect_info)
477 return false;
479 return true;
482 /* Return true if parameter I may be used. */
484 bool
485 sem_function::param_used_p (unsigned int i)
487 if (ipa_node_params_sum == NULL)
488 return true;
490 class ipa_node_params *parms_info = IPA_NODE_REF (get_node ());
492 if (!parms_info || vec_safe_length (parms_info->descriptors) <= i)
493 return true;
495 return ipa_is_param_used (IPA_NODE_REF (get_node ()), i);
498 /* Perform additional check needed to match types function parameters that are
499 used. Unlike for normal decls it matters if type is TYPE_RESTRICT and we
500 make an assumption that REFERENCE_TYPE parameters are always non-NULL. */
502 bool
503 sem_function::compatible_parm_types_p (tree parm1, tree parm2)
505 /* Be sure that parameters are TBAA compatible. */
506 if (!func_checker::compatible_types_p (parm1, parm2))
507 return return_false_with_msg ("parameter type is not compatible");
509 if (POINTER_TYPE_P (parm1)
510 && (TYPE_RESTRICT (parm1) != TYPE_RESTRICT (parm2)))
511 return return_false_with_msg ("argument restrict flag mismatch");
513 /* nonnull_arg_p implies non-zero range to REFERENCE types. */
514 if (POINTER_TYPE_P (parm1)
515 && TREE_CODE (parm1) != TREE_CODE (parm2)
516 && opt_for_fn (decl, flag_delete_null_pointer_checks))
517 return return_false_with_msg ("pointer wrt reference mismatch");
519 return true;
522 /* Fast equality function based on knowledge known in WPA. */
524 bool
525 sem_function::equals_wpa (sem_item *item,
526 hash_map <symtab_node *, sem_item *> &ignored_nodes)
528 gcc_assert (item->type == FUNC);
529 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
530 cgraph_node *cnode2 = dyn_cast <cgraph_node *> (item->node);
532 m_compared_func = static_cast<sem_function *> (item);
534 if (cnode->thunk != cnode2->thunk)
535 return return_false_with_msg ("thunk mismatch");
536 if (cnode->former_thunk_p () != cnode2->former_thunk_p ())
537 return return_false_with_msg ("former_thunk_p mismatch");
539 if ((cnode->thunk || cnode->former_thunk_p ())
540 && thunk_info::get (cnode) != thunk_info::get (cnode2))
541 return return_false_with_msg ("thunk_info mismatch");
543 /* Compare special function DECL attributes. */
544 if (DECL_FUNCTION_PERSONALITY (decl)
545 != DECL_FUNCTION_PERSONALITY (item->decl))
546 return return_false_with_msg ("function personalities are different");
548 if (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl)
549 != DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (item->decl))
550 return return_false_with_msg ("instrument function entry exit "
551 "attributes are different");
553 if (DECL_NO_LIMIT_STACK (decl) != DECL_NO_LIMIT_STACK (item->decl))
554 return return_false_with_msg ("no stack limit attributes are different");
556 if (DECL_CXX_CONSTRUCTOR_P (decl) != DECL_CXX_CONSTRUCTOR_P (item->decl))
557 return return_false_with_msg ("DECL_CXX_CONSTRUCTOR mismatch");
559 if (DECL_CXX_DESTRUCTOR_P (decl) != DECL_CXX_DESTRUCTOR_P (item->decl))
560 return return_false_with_msg ("DECL_CXX_DESTRUCTOR mismatch");
562 /* TODO: pure/const flags mostly matters only for references, except for
563 the fact that codegen takes LOOPING flag as a hint that loops are
564 finite. We may arrange the code to always pick leader that has least
565 specified flags and then this can go into comparing symbol properties. */
566 if (flags_from_decl_or_type (decl) != flags_from_decl_or_type (item->decl))
567 return return_false_with_msg ("decl_or_type flags are different");
569 /* Do not match polymorphic constructors of different types. They calls
570 type memory location for ipa-polymorphic-call and we do not want
571 it to get confused by wrong type. */
572 if (DECL_CXX_CONSTRUCTOR_P (decl)
573 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
575 if (TREE_CODE (TREE_TYPE (item->decl)) != METHOD_TYPE)
576 return return_false_with_msg ("DECL_CXX_CONSTRUCTOR type mismatch");
577 else if (!func_checker::compatible_polymorphic_types_p
578 (TYPE_METHOD_BASETYPE (TREE_TYPE (decl)),
579 TYPE_METHOD_BASETYPE (TREE_TYPE (item->decl)), false))
580 return return_false_with_msg ("ctor polymorphic type mismatch");
583 /* Checking function TARGET and OPTIMIZATION flags. */
584 cl_target_option *tar1 = target_opts_for_fn (decl);
585 cl_target_option *tar2 = target_opts_for_fn (item->decl);
587 if (tar1 != tar2 && !cl_target_option_eq (tar1, tar2))
589 if (dump_file && (dump_flags & TDF_DETAILS))
591 fprintf (dump_file, "target flags difference");
592 cl_target_option_print_diff (dump_file, 2, tar1, tar2);
595 return return_false_with_msg ("Target flags are different");
598 cl_optimization *opt1 = opts_for_fn (decl);
599 cl_optimization *opt2 = opts_for_fn (item->decl);
601 if (opt1 != opt2 && !cl_optimization_option_eq (opt1, opt2))
603 if (dump_file && (dump_flags & TDF_DETAILS))
605 fprintf (dump_file, "optimization flags difference");
606 cl_optimization_print_diff (dump_file, 2, opt1, opt2);
609 return return_false_with_msg ("optimization flags are different");
612 /* Result type checking. */
613 if (!func_checker::compatible_types_p
614 (TREE_TYPE (TREE_TYPE (decl)),
615 TREE_TYPE (TREE_TYPE (m_compared_func->decl))))
616 return return_false_with_msg ("result types are different");
618 /* Checking types of arguments. */
619 tree list1 = TYPE_ARG_TYPES (TREE_TYPE (decl)),
620 list2 = TYPE_ARG_TYPES (TREE_TYPE (m_compared_func->decl));
621 for (unsigned i = 0; list1 && list2;
622 list1 = TREE_CHAIN (list1), list2 = TREE_CHAIN (list2), i++)
624 tree parm1 = TREE_VALUE (list1);
625 tree parm2 = TREE_VALUE (list2);
627 /* This guard is here for function pointer with attributes (pr59927.c). */
628 if (!parm1 || !parm2)
629 return return_false_with_msg ("NULL argument type");
631 /* Verify that types are compatible to ensure that both functions
632 have same calling conventions. */
633 if (!types_compatible_p (parm1, parm2))
634 return return_false_with_msg ("parameter types are not compatible");
636 if (!param_used_p (i))
637 continue;
639 /* Perform additional checks for used parameters. */
640 if (!compatible_parm_types_p (parm1, parm2))
641 return false;
644 if (list1 || list2)
645 return return_false_with_msg ("Mismatched number of parameters");
647 if (node->num_references () != item->node->num_references ())
648 return return_false_with_msg ("different number of references");
650 /* Checking function attributes.
651 This is quadratic in number of attributes */
652 if (comp_type_attributes (TREE_TYPE (decl),
653 TREE_TYPE (item->decl)) != 1)
654 return return_false_with_msg ("different type attributes");
655 if (!attribute_list_equal (DECL_ATTRIBUTES (decl),
656 DECL_ATTRIBUTES (item->decl)))
657 return return_false_with_msg ("different decl attributes");
659 /* The type of THIS pointer type memory location for
660 ipa-polymorphic-call-analysis. */
661 if (opt_for_fn (decl, flag_devirtualize)
662 && (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
663 || TREE_CODE (TREE_TYPE (item->decl)) == METHOD_TYPE)
664 && param_used_p (0)
665 && compare_polymorphic_p ())
667 if (TREE_CODE (TREE_TYPE (decl)) != TREE_CODE (TREE_TYPE (item->decl)))
668 return return_false_with_msg ("METHOD_TYPE and FUNCTION_TYPE mismatch");
669 if (!func_checker::compatible_polymorphic_types_p
670 (TYPE_METHOD_BASETYPE (TREE_TYPE (decl)),
671 TYPE_METHOD_BASETYPE (TREE_TYPE (item->decl)), false))
672 return return_false_with_msg ("THIS pointer ODR type mismatch");
675 ipa_ref *ref = NULL, *ref2 = NULL;
676 for (unsigned i = 0; node->iterate_reference (i, ref); i++)
678 item->node->iterate_reference (i, ref2);
680 if (ref->use != ref2->use)
681 return return_false_with_msg ("reference use mismatch");
683 if (!compare_symbol_references (ignored_nodes, ref->referred,
684 ref2->referred,
685 ref->address_matters_p ()))
686 return false;
689 cgraph_edge *e1 = dyn_cast <cgraph_node *> (node)->callees;
690 cgraph_edge *e2 = dyn_cast <cgraph_node *> (item->node)->callees;
692 while (e1 && e2)
694 if (!compare_symbol_references (ignored_nodes, e1->callee,
695 e2->callee, false))
696 return false;
697 if (!compare_edge_flags (e1, e2))
698 return false;
700 e1 = e1->next_callee;
701 e2 = e2->next_callee;
704 if (e1 || e2)
705 return return_false_with_msg ("different number of calls");
707 e1 = dyn_cast <cgraph_node *> (node)->indirect_calls;
708 e2 = dyn_cast <cgraph_node *> (item->node)->indirect_calls;
710 while (e1 && e2)
712 if (!compare_edge_flags (e1, e2))
713 return false;
715 e1 = e1->next_callee;
716 e2 = e2->next_callee;
719 if (e1 || e2)
720 return return_false_with_msg ("different number of indirect calls");
722 return true;
725 /* Update hash by address sensitive references. We iterate over all
726 sensitive references (address_matters_p) and we hash ultimate alias
727 target of these nodes, which can improve a semantic item hash.
729 Also hash in referenced symbols properties. This can be done at any time
730 (as the properties should not change), but it is convenient to do it here
731 while we walk the references anyway. */
733 void
734 sem_item::update_hash_by_addr_refs (hash_map <symtab_node *,
735 sem_item *> &m_symtab_node_map)
737 ipa_ref* ref;
738 inchash::hash hstate (get_hash ());
740 for (unsigned i = 0; node->iterate_reference (i, ref); i++)
742 hstate.add_int (ref->use);
743 hash_referenced_symbol_properties (ref->referred, hstate,
744 ref->use == IPA_REF_ADDR);
745 if (ref->address_matters_p () || !m_symtab_node_map.get (ref->referred))
746 hstate.add_int (ref->referred->ultimate_alias_target ()->order);
749 if (is_a <cgraph_node *> (node))
751 for (cgraph_edge *e = dyn_cast <cgraph_node *> (node)->callers; e;
752 e = e->next_caller)
754 sem_item **result = m_symtab_node_map.get (e->callee);
755 hash_referenced_symbol_properties (e->callee, hstate, false);
756 if (!result)
757 hstate.add_int (e->callee->ultimate_alias_target ()->order);
761 set_hash (hstate.end ());
764 /* Update hash by computed local hash values taken from different
765 semantic items.
766 TODO: stronger SCC based hashing would be desirable here. */
768 void
769 sem_item::update_hash_by_local_refs (hash_map <symtab_node *,
770 sem_item *> &m_symtab_node_map)
772 ipa_ref* ref;
773 inchash::hash state (get_hash ());
775 for (unsigned j = 0; node->iterate_reference (j, ref); j++)
777 sem_item **result = m_symtab_node_map.get (ref->referring);
778 if (result)
779 state.merge_hash ((*result)->get_hash ());
782 if (type == FUNC)
784 for (cgraph_edge *e = dyn_cast <cgraph_node *> (node)->callees; e;
785 e = e->next_callee)
787 sem_item **result = m_symtab_node_map.get (e->caller);
788 if (result)
789 state.merge_hash ((*result)->get_hash ());
793 global_hash = state.end ();
796 /* Returns true if the item equals to ITEM given as argument. */
798 bool
799 sem_function::equals (sem_item *item,
800 hash_map <symtab_node *, sem_item *> &)
802 gcc_assert (item->type == FUNC);
803 bool eq = equals_private (item);
805 if (m_checker != NULL)
807 delete m_checker;
808 m_checker = NULL;
811 if (dump_file && (dump_flags & TDF_DETAILS))
812 fprintf (dump_file,
813 "Equals called for: %s:%s with result: %s\n\n",
814 node->dump_name (),
815 item->node->dump_name (),
816 eq ? "true" : "false");
818 return eq;
821 /* Processes function equality comparison. */
823 bool
824 sem_function::equals_private (sem_item *item)
826 if (item->type != FUNC)
827 return false;
829 basic_block bb1, bb2;
830 edge e1, e2;
831 edge_iterator ei1, ei2;
832 bool result = true;
833 tree arg1, arg2;
835 m_compared_func = static_cast<sem_function *> (item);
837 gcc_assert (decl != item->decl);
839 if (bb_sorted.length () != m_compared_func->bb_sorted.length ()
840 || edge_count != m_compared_func->edge_count
841 || cfg_checksum != m_compared_func->cfg_checksum)
842 return return_false ();
844 m_checker = new func_checker (decl, m_compared_func->decl,
845 false,
846 &refs_set,
847 &m_compared_func->refs_set);
848 arg1 = DECL_ARGUMENTS (decl);
849 arg2 = DECL_ARGUMENTS (m_compared_func->decl);
850 for (unsigned i = 0;
851 arg1 && arg2; arg1 = DECL_CHAIN (arg1), arg2 = DECL_CHAIN (arg2), i++)
853 if (!types_compatible_p (TREE_TYPE (arg1), TREE_TYPE (arg2)))
854 return return_false_with_msg ("argument types are not compatible");
855 if (!param_used_p (i))
856 continue;
857 /* Perform additional checks for used parameters. */
858 if (!compatible_parm_types_p (TREE_TYPE (arg1), TREE_TYPE (arg2)))
859 return false;
860 if (!m_checker->compare_decl (arg1, arg2))
861 return return_false ();
863 if (arg1 || arg2)
864 return return_false_with_msg ("Mismatched number of arguments");
866 if (!dyn_cast <cgraph_node *> (node)->has_gimple_body_p ())
867 return true;
869 /* Fill-up label dictionary. */
870 for (unsigned i = 0; i < bb_sorted.length (); ++i)
872 m_checker->parse_labels (bb_sorted[i]);
873 m_checker->parse_labels (m_compared_func->bb_sorted[i]);
876 /* Checking all basic blocks. */
877 for (unsigned i = 0; i < bb_sorted.length (); ++i)
878 if(!m_checker->compare_bb (bb_sorted[i], m_compared_func->bb_sorted[i]))
879 return return_false ();
881 auto_vec <int> bb_dict;
883 /* Basic block edges check. */
884 for (unsigned i = 0; i < bb_sorted.length (); ++i)
886 bb1 = bb_sorted[i]->bb;
887 bb2 = m_compared_func->bb_sorted[i]->bb;
889 ei2 = ei_start (bb2->preds);
891 for (ei1 = ei_start (bb1->preds); ei_cond (ei1, &e1); ei_next (&ei1))
893 ei_cond (ei2, &e2);
895 if (e1->flags != e2->flags)
896 return return_false_with_msg ("flags comparison returns false");
898 if (!bb_dict_test (&bb_dict, e1->src->index, e2->src->index))
899 return return_false_with_msg ("edge comparison returns false");
901 if (!bb_dict_test (&bb_dict, e1->dest->index, e2->dest->index))
902 return return_false_with_msg ("BB comparison returns false");
904 if (!m_checker->compare_edge (e1, e2))
905 return return_false_with_msg ("edge comparison returns false");
907 ei_next (&ei2);
911 /* Basic block PHI nodes comparison. */
912 for (unsigned i = 0; i < bb_sorted.length (); i++)
913 if (!compare_phi_node (bb_sorted[i]->bb, m_compared_func->bb_sorted[i]->bb))
914 return return_false_with_msg ("PHI node comparison returns false");
916 return result;
919 /* Set LOCAL_P of NODE to true if DATA is non-NULL.
920 Helper for call_for_symbol_thunks_and_aliases. */
922 static bool
923 set_local (cgraph_node *node, void *data)
925 node->local = data != NULL;
926 return false;
929 /* TREE_ADDRESSABLE of NODE to true.
930 Helper for call_for_symbol_thunks_and_aliases. */
932 static bool
933 set_addressable (varpool_node *node, void *)
935 TREE_ADDRESSABLE (node->decl) = 1;
936 return false;
939 /* Clear DECL_RTL of NODE.
940 Helper for call_for_symbol_thunks_and_aliases. */
942 static bool
943 clear_decl_rtl (symtab_node *node, void *)
945 SET_DECL_RTL (node->decl, NULL);
946 return false;
949 /* Redirect all callers of N and its aliases to TO. Remove aliases if
950 possible. Return number of redirections made. */
952 static int
953 redirect_all_callers (cgraph_node *n, cgraph_node *to)
955 int nredirected = 0;
956 ipa_ref *ref;
957 cgraph_edge *e = n->callers;
959 while (e)
961 /* Redirecting thunks to interposable symbols or symbols in other sections
962 may not be supported by target output code. Play safe for now and
963 punt on redirection. */
964 if (!e->caller->thunk)
966 struct cgraph_edge *nexte = e->next_caller;
967 e->redirect_callee (to);
968 e = nexte;
969 nredirected++;
971 else
972 e = e->next_callee;
974 for (unsigned i = 0; n->iterate_direct_aliases (i, ref);)
976 bool removed = false;
977 cgraph_node *n_alias = dyn_cast <cgraph_node *> (ref->referring);
979 if ((DECL_COMDAT_GROUP (n->decl)
980 && (DECL_COMDAT_GROUP (n->decl)
981 == DECL_COMDAT_GROUP (n_alias->decl)))
982 || (n_alias->get_availability () > AVAIL_INTERPOSABLE
983 && n->get_availability () > AVAIL_INTERPOSABLE))
985 nredirected += redirect_all_callers (n_alias, to);
986 if (n_alias->can_remove_if_no_direct_calls_p ()
987 && !n_alias->call_for_symbol_and_aliases (cgraph_node::has_thunk_p,
988 NULL, true)
989 && !n_alias->has_aliases_p ())
990 n_alias->remove ();
992 if (!removed)
993 i++;
995 return nredirected;
998 /* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can
999 be applied. */
1001 bool
1002 sem_function::merge (sem_item *alias_item)
1004 gcc_assert (alias_item->type == FUNC);
1006 sem_function *alias_func = static_cast<sem_function *> (alias_item);
1008 cgraph_node *original = get_node ();
1009 cgraph_node *local_original = NULL;
1010 cgraph_node *alias = alias_func->get_node ();
1012 bool create_wrapper = false;
1013 bool create_alias = false;
1014 bool redirect_callers = false;
1015 bool remove = false;
1017 bool original_discardable = false;
1018 bool original_discarded = false;
1020 bool original_address_matters = original->address_matters_p ();
1021 bool alias_address_matters = alias->address_matters_p ();
1023 AUTO_DUMP_SCOPE ("merge",
1024 dump_user_location_t::from_function_decl (decl));
1026 if (DECL_EXTERNAL (alias->decl))
1028 if (dump_enabled_p ())
1029 dump_printf (MSG_MISSED_OPTIMIZATION,
1030 "Not unifying; alias is external.\n");
1031 return false;
1034 if (DECL_NO_INLINE_WARNING_P (original->decl)
1035 != DECL_NO_INLINE_WARNING_P (alias->decl))
1037 if (dump_enabled_p ())
1038 dump_printf (MSG_MISSED_OPTIMIZATION,
1039 "Not unifying; DECL_NO_INLINE_WARNING mismatch.\n");
1040 return false;
1043 /* Do not attempt to mix functions from different user sections;
1044 we do not know what user intends with those. */
1045 if (((DECL_SECTION_NAME (original->decl) && !original->implicit_section)
1046 || (DECL_SECTION_NAME (alias->decl) && !alias->implicit_section))
1047 && DECL_SECTION_NAME (original->decl) != DECL_SECTION_NAME (alias->decl))
1049 if (dump_enabled_p ())
1050 dump_printf (MSG_MISSED_OPTIMIZATION,
1051 "Not unifying; "
1052 "original and alias are in different sections.\n");
1053 return false;
1056 if (!original->in_same_comdat_group_p (alias)
1057 || original->comdat_local_p ())
1059 if (dump_enabled_p ())
1060 dump_printf (MSG_MISSED_OPTIMIZATION,
1061 "Not unifying; alias nor wrapper cannot be created; "
1062 "across comdat group boundary\n");
1063 return false;
1066 /* See if original is in a section that can be discarded if the main
1067 symbol is not used. */
1069 if (original->can_be_discarded_p ())
1070 original_discardable = true;
1071 /* Also consider case where we have resolution info and we know that
1072 original's definition is not going to be used. In this case we cannot
1073 create alias to original. */
1074 if (node->resolution != LDPR_UNKNOWN
1075 && !decl_binds_to_current_def_p (node->decl))
1076 original_discardable = original_discarded = true;
1078 /* Creating a symtab alias is the optimal way to merge.
1079 It however cannot be used in the following cases:
1081 1) if ORIGINAL and ALIAS may be possibly compared for address equality.
1082 2) if ORIGINAL is in a section that may be discarded by linker or if
1083 it is an external functions where we cannot create an alias
1084 (ORIGINAL_DISCARDABLE)
1085 3) if target do not support symbol aliases.
1086 4) original and alias lie in different comdat groups.
1088 If we cannot produce alias, we will turn ALIAS into WRAPPER of ORIGINAL
1089 and/or redirect all callers from ALIAS to ORIGINAL. */
1090 if ((original_address_matters && alias_address_matters)
1091 || (original_discardable
1092 && (!DECL_COMDAT_GROUP (alias->decl)
1093 || (DECL_COMDAT_GROUP (alias->decl)
1094 != DECL_COMDAT_GROUP (original->decl))))
1095 || original_discarded
1096 || !sem_item::target_supports_symbol_aliases_p ()
1097 || DECL_COMDAT_GROUP (alias->decl) != DECL_COMDAT_GROUP (original->decl))
1099 /* First see if we can produce wrapper. */
1101 /* Symbol properties that matter for references must be preserved.
1102 TODO: We can produce wrapper, but we need to produce alias of ORIGINAL
1103 with proper properties. */
1104 if (!sem_item::compare_referenced_symbol_properties (NULL, original, alias,
1105 alias->address_taken))
1107 if (dump_enabled_p ())
1108 dump_printf (MSG_MISSED_OPTIMIZATION,
1109 "Wrapper cannot be created because referenced symbol "
1110 "properties mismatch\n");
1112 /* Do not turn function in one comdat group into wrapper to another
1113 comdat group. Other compiler producing the body of the
1114 another comdat group may make opposite decision and with unfortunate
1115 linker choices this may close a loop. */
1116 else if (DECL_COMDAT_GROUP (original->decl)
1117 && DECL_COMDAT_GROUP (alias->decl)
1118 && (DECL_COMDAT_GROUP (alias->decl)
1119 != DECL_COMDAT_GROUP (original->decl)))
1121 if (dump_enabled_p ())
1122 dump_printf (MSG_MISSED_OPTIMIZATION,
1123 "Wrapper cannot be created because of COMDAT\n");
1125 else if (DECL_STATIC_CHAIN (alias->decl)
1126 || DECL_STATIC_CHAIN (original->decl))
1128 if (dump_enabled_p ())
1129 dump_printf (MSG_MISSED_OPTIMIZATION,
1130 "Cannot create wrapper of nested function.\n");
1132 /* TODO: We can also deal with variadic functions never calling
1133 VA_START. */
1134 else if (stdarg_p (TREE_TYPE (alias->decl)))
1136 if (dump_enabled_p ())
1137 dump_printf (MSG_MISSED_OPTIMIZATION,
1138 "cannot create wrapper of stdarg function.\n");
1140 else if (ipa_fn_summaries
1141 && ipa_size_summaries->get (alias) != NULL
1142 && ipa_size_summaries->get (alias)->self_size <= 2)
1144 if (dump_enabled_p ())
1145 dump_printf (MSG_MISSED_OPTIMIZATION, "Wrapper creation is not "
1146 "profitable (function is too small).\n");
1148 /* If user paid attention to mark function noinline, assume it is
1149 somewhat special and do not try to turn it into a wrapper that
1150 cannot be undone by inliner. */
1151 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (alias->decl)))
1153 if (dump_enabled_p ())
1154 dump_printf (MSG_MISSED_OPTIMIZATION,
1155 "Wrappers are not created for noinline.\n");
1157 else
1158 create_wrapper = true;
1160 /* We can redirect local calls in the case both alias and original
1161 are not interposable. */
1162 redirect_callers
1163 = alias->get_availability () > AVAIL_INTERPOSABLE
1164 && original->get_availability () > AVAIL_INTERPOSABLE;
1165 /* TODO: We can redirect, but we need to produce alias of ORIGINAL
1166 with proper properties. */
1167 if (!sem_item::compare_referenced_symbol_properties (NULL, original, alias,
1168 alias->address_taken))
1169 redirect_callers = false;
1171 if (!redirect_callers && !create_wrapper)
1173 if (dump_enabled_p ())
1174 dump_printf (MSG_MISSED_OPTIMIZATION,
1175 "Not unifying; cannot redirect callers nor "
1176 "produce wrapper\n");
1177 return false;
1180 /* Work out the symbol the wrapper should call.
1181 If ORIGINAL is interposable, we need to call a local alias.
1182 Also produce local alias (if possible) as an optimization.
1184 Local aliases cannot be created inside comdat groups because that
1185 prevents inlining. */
1186 if (!original_discardable && !original->get_comdat_group ())
1188 local_original
1189 = dyn_cast <cgraph_node *> (original->noninterposable_alias ());
1190 if (!local_original
1191 && original->get_availability () > AVAIL_INTERPOSABLE)
1192 local_original = original;
1194 /* If we cannot use local alias, fallback to the original
1195 when possible. */
1196 else if (original->get_availability () > AVAIL_INTERPOSABLE)
1197 local_original = original;
1199 /* If original is COMDAT local, we cannot really redirect calls outside
1200 of its comdat group to it. */
1201 if (original->comdat_local_p ())
1202 redirect_callers = false;
1203 if (!local_original)
1205 if (dump_enabled_p ())
1206 dump_printf (MSG_MISSED_OPTIMIZATION,
1207 "Not unifying; cannot produce local alias.\n");
1208 return false;
1211 if (!redirect_callers && !create_wrapper)
1213 if (dump_enabled_p ())
1214 dump_printf (MSG_MISSED_OPTIMIZATION,
1215 "Not unifying; "
1216 "cannot redirect callers nor produce a wrapper\n");
1217 return false;
1219 if (!create_wrapper
1220 && !alias->call_for_symbol_and_aliases (cgraph_node::has_thunk_p,
1221 NULL, true)
1222 && !alias->can_remove_if_no_direct_calls_p ())
1224 if (dump_enabled_p ())
1225 dump_printf (MSG_MISSED_OPTIMIZATION,
1226 "Not unifying; cannot make wrapper and "
1227 "function has other uses than direct calls\n");
1228 return false;
1231 else
1232 create_alias = true;
1234 if (redirect_callers)
1236 int nredirected = redirect_all_callers (alias, local_original);
1238 if (nredirected)
1240 alias->icf_merged = true;
1241 local_original->icf_merged = true;
1243 if (dump_enabled_p ())
1244 dump_printf (MSG_NOTE,
1245 "%i local calls have been "
1246 "redirected.\n", nredirected);
1249 /* If all callers was redirected, do not produce wrapper. */
1250 if (alias->can_remove_if_no_direct_calls_p ()
1251 && !DECL_VIRTUAL_P (alias->decl)
1252 && !alias->has_aliases_p ())
1254 create_wrapper = false;
1255 remove = true;
1257 gcc_assert (!create_alias);
1259 else if (create_alias)
1261 alias->icf_merged = true;
1263 /* Remove the function's body. */
1264 ipa_merge_profiles (original, alias);
1265 symtab->call_cgraph_removal_hooks (alias);
1266 alias->release_body (true);
1267 alias->reset ();
1268 /* Notice global symbol possibly produced RTL. */
1269 ((symtab_node *)alias)->call_for_symbol_and_aliases (clear_decl_rtl,
1270 NULL, true);
1272 /* Create the alias. */
1273 cgraph_node::create_alias (alias_func->decl, decl);
1274 alias->resolve_alias (original);
1276 original->call_for_symbol_thunks_and_aliases
1277 (set_local, (void *)(size_t) original->local_p (), true);
1279 if (dump_enabled_p ())
1280 dump_printf (MSG_OPTIMIZED_LOCATIONS,
1281 "Unified; Function alias has been created.\n");
1283 if (create_wrapper)
1285 gcc_assert (!create_alias);
1286 alias->icf_merged = true;
1287 symtab->call_cgraph_removal_hooks (alias);
1288 local_original->icf_merged = true;
1290 /* FIXME update local_original counts. */
1291 ipa_merge_profiles (original, alias, true);
1292 alias->create_wrapper (local_original);
1293 symtab->call_cgraph_insertion_hooks (alias);
1295 if (dump_enabled_p ())
1296 dump_printf (MSG_OPTIMIZED_LOCATIONS,
1297 "Unified; Wrapper has been created.\n");
1300 /* It's possible that redirection can hit thunks that block
1301 redirection opportunities. */
1302 gcc_assert (alias->icf_merged || remove || redirect_callers);
1303 original->icf_merged = true;
1305 /* We use merged flag to track cases where COMDAT function is known to be
1306 compatible its callers. If we merged in non-COMDAT, we need to give up
1307 on this optimization. */
1308 if (original->merged_comdat && !alias->merged_comdat)
1310 if (dump_enabled_p ())
1311 dump_printf (MSG_NOTE, "Dropping merged_comdat flag.\n");
1312 if (local_original)
1313 local_original->merged_comdat = false;
1314 original->merged_comdat = false;
1317 if (remove)
1319 ipa_merge_profiles (original, alias);
1320 alias->release_body ();
1321 alias->reset ();
1322 alias->body_removed = true;
1323 alias->icf_merged = true;
1324 if (dump_enabled_p ())
1325 dump_printf (MSG_OPTIMIZED_LOCATIONS,
1326 "Unified; Function body was removed.\n");
1329 return true;
1332 /* Semantic item initialization function. */
1334 void
1335 sem_function::init (ipa_icf_gimple::func_checker *checker)
1337 m_checker = checker;
1338 if (in_lto_p)
1339 get_node ()->get_untransformed_body ();
1341 tree fndecl = node->decl;
1342 function *func = DECL_STRUCT_FUNCTION (fndecl);
1344 gcc_assert (func);
1345 gcc_assert (SSANAMES (func));
1347 ssa_names_size = SSANAMES (func)->length ();
1348 node = node;
1350 decl = fndecl;
1351 region_tree = func->eh->region_tree;
1353 /* iterating all function arguments. */
1354 arg_count = count_formal_params (fndecl);
1356 edge_count = n_edges_for_fn (func);
1357 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1358 if (!cnode->thunk)
1360 cfg_checksum = coverage_compute_cfg_checksum (func);
1362 inchash::hash hstate;
1364 basic_block bb;
1365 FOR_EACH_BB_FN (bb, func)
1367 unsigned nondbg_stmt_count = 0;
1369 edge e;
1370 for (edge_iterator ei = ei_start (bb->preds); ei_cond (ei, &e);
1371 ei_next (&ei))
1372 cfg_checksum = iterative_hash_host_wide_int (e->flags,
1373 cfg_checksum);
1375 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1376 gsi_next (&gsi))
1378 gimple *stmt = gsi_stmt (gsi);
1380 if (gimple_code (stmt) != GIMPLE_DEBUG
1381 && gimple_code (stmt) != GIMPLE_PREDICT)
1383 hash_stmt (stmt, hstate);
1384 nondbg_stmt_count++;
1388 hstate.commit_flag ();
1389 gcode_hash = hstate.end ();
1390 bb_sizes.safe_push (nondbg_stmt_count);
1392 /* Inserting basic block to hash table. */
1393 sem_bb *semantic_bb = new sem_bb (bb, nondbg_stmt_count,
1394 EDGE_COUNT (bb->preds)
1395 + EDGE_COUNT (bb->succs));
1397 bb_sorted.safe_push (semantic_bb);
1400 else
1402 cfg_checksum = 0;
1403 gcode_hash = thunk_info::get (cnode)->hash ();
1406 m_checker = NULL;
1409 /* Improve accumulated hash for HSTATE based on a gimple statement STMT. */
1411 void
1412 sem_function::hash_stmt (gimple *stmt, inchash::hash &hstate)
1414 enum gimple_code code = gimple_code (stmt);
1416 hstate.add_int (code);
1418 switch (code)
1420 case GIMPLE_SWITCH:
1421 m_checker->hash_operand (gimple_switch_index (as_a <gswitch *> (stmt)),
1422 hstate, 0);
1423 break;
1424 case GIMPLE_ASSIGN:
1425 hstate.add_int (gimple_assign_rhs_code (stmt));
1426 if (commutative_tree_code (gimple_assign_rhs_code (stmt))
1427 || commutative_ternary_tree_code (gimple_assign_rhs_code (stmt)))
1429 m_checker->hash_operand (gimple_assign_rhs1 (stmt), hstate, 0);
1430 m_checker->hash_operand (gimple_assign_rhs2 (stmt), hstate, 0);
1431 if (commutative_ternary_tree_code (gimple_assign_rhs_code (stmt)))
1432 m_checker->hash_operand (gimple_assign_rhs3 (stmt), hstate, 0);
1433 m_checker->hash_operand (gimple_assign_lhs (stmt), hstate, 0);
1435 /* fall through */
1436 case GIMPLE_CALL:
1437 case GIMPLE_ASM:
1438 case GIMPLE_COND:
1439 case GIMPLE_GOTO:
1440 case GIMPLE_RETURN:
1441 /* All these statements are equivalent if their operands are. */
1442 for (unsigned i = 0; i < gimple_num_ops (stmt); ++i)
1443 m_checker->hash_operand (gimple_op (stmt, i), hstate, 0);
1444 /* Consider nocf_check attribute in hash as it affects code
1445 generation. */
1446 if (code == GIMPLE_CALL
1447 && flag_cf_protection & CF_BRANCH)
1448 hstate.add_flag (gimple_call_nocf_check_p (as_a <gcall *> (stmt)));
1449 default:
1450 break;
1455 /* Return true if polymorphic comparison must be processed. */
1457 bool
1458 sem_function::compare_polymorphic_p (void)
1460 struct cgraph_edge *e;
1462 if (!opt_for_fn (get_node ()->decl, flag_devirtualize))
1463 return false;
1464 if (get_node ()->indirect_calls != NULL)
1465 return true;
1466 /* TODO: We can do simple propagation determining what calls may lead to
1467 a polymorphic call. */
1468 for (e = get_node ()->callees; e; e = e->next_callee)
1469 if (e->callee->definition
1470 && opt_for_fn (e->callee->decl, flag_devirtualize))
1471 return true;
1472 return false;
1475 /* For a given call graph NODE, the function constructs new
1476 semantic function item. */
1478 sem_function *
1479 sem_function::parse (cgraph_node *node, bitmap_obstack *stack,
1480 func_checker *checker)
1482 tree fndecl = node->decl;
1483 function *func = DECL_STRUCT_FUNCTION (fndecl);
1485 if (!func || (!node->has_gimple_body_p () && !node->thunk))
1486 return NULL;
1488 if (lookup_attribute_by_prefix ("omp ", DECL_ATTRIBUTES (node->decl)) != NULL)
1489 return NULL;
1491 if (lookup_attribute_by_prefix ("oacc ",
1492 DECL_ATTRIBUTES (node->decl)) != NULL)
1493 return NULL;
1495 /* PR ipa/70306. */
1496 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1497 || DECL_STATIC_DESTRUCTOR (node->decl))
1498 return NULL;
1500 sem_function *f = new sem_function (node, stack);
1501 f->init (checker);
1503 return f;
1506 /* For given basic blocks BB1 and BB2 (from functions FUNC1 and FUNC),
1507 return true if phi nodes are semantically equivalent in these blocks . */
1509 bool
1510 sem_function::compare_phi_node (basic_block bb1, basic_block bb2)
1512 gphi_iterator si1, si2;
1513 gphi *phi1, *phi2;
1514 unsigned size1, size2, i;
1515 tree t1, t2;
1516 edge e1, e2;
1518 gcc_assert (bb1 != NULL);
1519 gcc_assert (bb2 != NULL);
1521 si2 = gsi_start_nonvirtual_phis (bb2);
1522 for (si1 = gsi_start_nonvirtual_phis (bb1); !gsi_end_p (si1);
1523 gsi_next_nonvirtual_phi (&si1))
1525 if (gsi_end_p (si1) && gsi_end_p (si2))
1526 break;
1528 if (gsi_end_p (si1) || gsi_end_p (si2))
1529 return return_false();
1531 phi1 = si1.phi ();
1532 phi2 = si2.phi ();
1534 tree phi_result1 = gimple_phi_result (phi1);
1535 tree phi_result2 = gimple_phi_result (phi2);
1537 if (!m_checker->compare_operand (phi_result1, phi_result2))
1538 return return_false_with_msg ("PHI results are different");
1540 size1 = gimple_phi_num_args (phi1);
1541 size2 = gimple_phi_num_args (phi2);
1543 if (size1 != size2)
1544 return return_false ();
1546 for (i = 0; i < size1; ++i)
1548 t1 = gimple_phi_arg (phi1, i)->def;
1549 t2 = gimple_phi_arg (phi2, i)->def;
1551 if (!m_checker->compare_operand (t1, t2))
1552 return return_false ();
1554 e1 = gimple_phi_arg_edge (phi1, i);
1555 e2 = gimple_phi_arg_edge (phi2, i);
1557 if (!m_checker->compare_edge (e1, e2))
1558 return return_false ();
1561 gsi_next_nonvirtual_phi (&si2);
1564 return true;
1567 /* Basic blocks dictionary BB_DICT returns true if SOURCE index BB
1568 corresponds to TARGET. */
1570 bool
1571 sem_function::bb_dict_test (vec<int> *bb_dict, int source, int target)
1573 source++;
1574 target++;
1576 if (bb_dict->length () <= (unsigned)source)
1577 bb_dict->safe_grow_cleared (source + 1, true);
1579 if ((*bb_dict)[source] == 0)
1581 (*bb_dict)[source] = target;
1582 return true;
1584 else
1585 return (*bb_dict)[source] == target;
1588 sem_variable::sem_variable (bitmap_obstack *stack): sem_item (VAR, stack)
1592 sem_variable::sem_variable (varpool_node *node, bitmap_obstack *stack)
1593 : sem_item (VAR, node, stack)
1595 gcc_checking_assert (node);
1596 gcc_checking_assert (get_node ());
1599 /* Fast equality function based on knowledge known in WPA. */
1601 bool
1602 sem_variable::equals_wpa (sem_item *item,
1603 hash_map <symtab_node *, sem_item *> &ignored_nodes)
1605 gcc_assert (item->type == VAR);
1607 if (node->num_references () != item->node->num_references ())
1608 return return_false_with_msg ("different number of references");
1610 if (DECL_TLS_MODEL (decl) || DECL_TLS_MODEL (item->decl))
1611 return return_false_with_msg ("TLS model");
1613 /* DECL_ALIGN is safe to merge, because we will always chose the largest
1614 alignment out of all aliases. */
1616 if (DECL_VIRTUAL_P (decl) != DECL_VIRTUAL_P (item->decl))
1617 return return_false_with_msg ("Virtual flag mismatch");
1619 if (DECL_SIZE (decl) != DECL_SIZE (item->decl)
1620 && ((!DECL_SIZE (decl) || !DECL_SIZE (item->decl))
1621 || !operand_equal_p (DECL_SIZE (decl),
1622 DECL_SIZE (item->decl), OEP_ONLY_CONST)))
1623 return return_false_with_msg ("size mismatch");
1625 /* Do not attempt to mix data from different user sections;
1626 we do not know what user intends with those. */
1627 if (((DECL_SECTION_NAME (decl) && !node->implicit_section)
1628 || (DECL_SECTION_NAME (item->decl) && !item->node->implicit_section))
1629 && DECL_SECTION_NAME (decl) != DECL_SECTION_NAME (item->decl))
1630 return return_false_with_msg ("user section mismatch");
1632 if (DECL_IN_TEXT_SECTION (decl) != DECL_IN_TEXT_SECTION (item->decl))
1633 return return_false_with_msg ("text section");
1635 ipa_ref *ref = NULL, *ref2 = NULL;
1636 for (unsigned i = 0; node->iterate_reference (i, ref); i++)
1638 item->node->iterate_reference (i, ref2);
1640 if (ref->use != ref2->use)
1641 return return_false_with_msg ("reference use mismatch");
1643 if (!compare_symbol_references (ignored_nodes,
1644 ref->referred, ref2->referred,
1645 ref->address_matters_p ()))
1646 return false;
1649 return true;
1652 /* Returns true if the item equals to ITEM given as argument. */
1654 bool
1655 sem_variable::equals (sem_item *item,
1656 hash_map <symtab_node *, sem_item *> &)
1658 gcc_assert (item->type == VAR);
1659 bool ret;
1661 if (DECL_INITIAL (decl) == error_mark_node && in_lto_p)
1662 dyn_cast <varpool_node *>(node)->get_constructor ();
1663 if (DECL_INITIAL (item->decl) == error_mark_node && in_lto_p)
1664 dyn_cast <varpool_node *>(item->node)->get_constructor ();
1666 /* As seen in PR ipa/65303 we have to compare variables types. */
1667 if (!func_checker::compatible_types_p (TREE_TYPE (decl),
1668 TREE_TYPE (item->decl)))
1669 return return_false_with_msg ("variables types are different");
1671 ret = sem_variable::equals (DECL_INITIAL (decl),
1672 DECL_INITIAL (item->node->decl));
1673 if (dump_file && (dump_flags & TDF_DETAILS))
1674 fprintf (dump_file,
1675 "Equals called for vars: %s:%s with result: %s\n\n",
1676 node->dump_name (), item->node->dump_name (),
1677 ret ? "true" : "false");
1679 return ret;
1682 /* Compares trees T1 and T2 for semantic equality. */
1684 bool
1685 sem_variable::equals (tree t1, tree t2)
1687 if (!t1 || !t2)
1688 return return_with_debug (t1 == t2);
1689 if (t1 == t2)
1690 return true;
1691 tree_code tc1 = TREE_CODE (t1);
1692 tree_code tc2 = TREE_CODE (t2);
1694 if (tc1 != tc2)
1695 return return_false_with_msg ("TREE_CODE mismatch");
1697 switch (tc1)
1699 case CONSTRUCTOR:
1701 vec<constructor_elt, va_gc> *v1, *v2;
1702 unsigned HOST_WIDE_INT idx;
1704 enum tree_code typecode = TREE_CODE (TREE_TYPE (t1));
1705 if (typecode != TREE_CODE (TREE_TYPE (t2)))
1706 return return_false_with_msg ("constructor type mismatch");
1708 if (typecode == ARRAY_TYPE)
1710 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
1711 /* For arrays, check that the sizes all match. */
1712 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
1713 || size_1 == -1
1714 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
1715 return return_false_with_msg ("constructor array size mismatch");
1717 else if (!func_checker::compatible_types_p (TREE_TYPE (t1),
1718 TREE_TYPE (t2)))
1719 return return_false_with_msg ("constructor type incompatible");
1721 v1 = CONSTRUCTOR_ELTS (t1);
1722 v2 = CONSTRUCTOR_ELTS (t2);
1723 if (vec_safe_length (v1) != vec_safe_length (v2))
1724 return return_false_with_msg ("constructor number of elts mismatch");
1726 for (idx = 0; idx < vec_safe_length (v1); ++idx)
1728 constructor_elt *c1 = &(*v1)[idx];
1729 constructor_elt *c2 = &(*v2)[idx];
1731 /* Check that each value is the same... */
1732 if (!sem_variable::equals (c1->value, c2->value))
1733 return false;
1734 /* ... and that they apply to the same fields! */
1735 if (!sem_variable::equals (c1->index, c2->index))
1736 return false;
1738 return true;
1740 case MEM_REF:
1742 tree x1 = TREE_OPERAND (t1, 0);
1743 tree x2 = TREE_OPERAND (t2, 0);
1744 tree y1 = TREE_OPERAND (t1, 1);
1745 tree y2 = TREE_OPERAND (t2, 1);
1747 if (!func_checker::compatible_types_p (TREE_TYPE (x1), TREE_TYPE (x2)))
1748 return return_false ();
1750 /* Type of the offset on MEM_REF does not matter. */
1751 return return_with_debug (sem_variable::equals (x1, x2)
1752 && known_eq (wi::to_poly_offset (y1),
1753 wi::to_poly_offset (y2)));
1755 case ADDR_EXPR:
1756 case FDESC_EXPR:
1758 tree op1 = TREE_OPERAND (t1, 0);
1759 tree op2 = TREE_OPERAND (t2, 0);
1760 return sem_variable::equals (op1, op2);
1762 /* References to other vars/decls are compared using ipa-ref. */
1763 case FUNCTION_DECL:
1764 case VAR_DECL:
1765 if (decl_in_symtab_p (t1) && decl_in_symtab_p (t2))
1766 return true;
1767 return return_false_with_msg ("Declaration mismatch");
1768 case CONST_DECL:
1769 /* TODO: We can check CONST_DECL by its DECL_INITIAL, but for that we
1770 need to process its VAR/FUNCTION references without relying on ipa-ref
1771 compare. */
1772 case FIELD_DECL:
1773 case LABEL_DECL:
1774 return return_false_with_msg ("Declaration mismatch");
1775 case INTEGER_CST:
1776 /* Integer constants are the same only if the same width of type. */
1777 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
1778 return return_false_with_msg ("INTEGER_CST precision mismatch");
1779 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
1780 return return_false_with_msg ("INTEGER_CST mode mismatch");
1781 return return_with_debug (tree_int_cst_equal (t1, t2));
1782 case STRING_CST:
1783 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
1784 return return_false_with_msg ("STRING_CST mode mismatch");
1785 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2))
1786 return return_false_with_msg ("STRING_CST length mismatch");
1787 if (memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1788 TREE_STRING_LENGTH (t1)))
1789 return return_false_with_msg ("STRING_CST mismatch");
1790 return true;
1791 case FIXED_CST:
1792 /* Fixed constants are the same only if the same width of type. */
1793 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
1794 return return_false_with_msg ("FIXED_CST precision mismatch");
1796 return return_with_debug (FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
1797 TREE_FIXED_CST (t2)));
1798 case COMPLEX_CST:
1799 return (sem_variable::equals (TREE_REALPART (t1), TREE_REALPART (t2))
1800 && sem_variable::equals (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
1801 case REAL_CST:
1802 /* Real constants are the same only if the same width of type. */
1803 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
1804 return return_false_with_msg ("REAL_CST precision mismatch");
1805 return return_with_debug (real_identical (&TREE_REAL_CST (t1),
1806 &TREE_REAL_CST (t2)));
1807 case VECTOR_CST:
1809 if (maybe_ne (VECTOR_CST_NELTS (t1), VECTOR_CST_NELTS (t2)))
1810 return return_false_with_msg ("VECTOR_CST nelts mismatch");
1812 unsigned int count
1813 = tree_vector_builder::binary_encoded_nelts (t1, t2);
1814 for (unsigned int i = 0; i < count; ++i)
1815 if (!sem_variable::equals (VECTOR_CST_ENCODED_ELT (t1, i),
1816 VECTOR_CST_ENCODED_ELT (t2, i)))
1817 return false;
1819 return true;
1821 case ARRAY_REF:
1822 case ARRAY_RANGE_REF:
1824 tree x1 = TREE_OPERAND (t1, 0);
1825 tree x2 = TREE_OPERAND (t2, 0);
1826 tree y1 = TREE_OPERAND (t1, 1);
1827 tree y2 = TREE_OPERAND (t2, 1);
1829 if (!sem_variable::equals (x1, x2) || !sem_variable::equals (y1, y2))
1830 return false;
1831 if (!sem_variable::equals (array_ref_low_bound (t1),
1832 array_ref_low_bound (t2)))
1833 return false;
1834 if (!sem_variable::equals (array_ref_element_size (t1),
1835 array_ref_element_size (t2)))
1836 return false;
1837 return true;
1840 case COMPONENT_REF:
1841 case POINTER_PLUS_EXPR:
1842 case PLUS_EXPR:
1843 case MINUS_EXPR:
1844 case RANGE_EXPR:
1846 tree x1 = TREE_OPERAND (t1, 0);
1847 tree x2 = TREE_OPERAND (t2, 0);
1848 tree y1 = TREE_OPERAND (t1, 1);
1849 tree y2 = TREE_OPERAND (t2, 1);
1851 return sem_variable::equals (x1, x2) && sem_variable::equals (y1, y2);
1854 CASE_CONVERT:
1855 case VIEW_CONVERT_EXPR:
1856 if (!func_checker::compatible_types_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1857 return return_false ();
1858 return sem_variable::equals (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1859 case ERROR_MARK:
1860 return return_false_with_msg ("ERROR_MARK");
1861 default:
1862 return return_false_with_msg ("Unknown TREE code reached");
1866 /* Parser function that visits a varpool NODE. */
1868 sem_variable *
1869 sem_variable::parse (varpool_node *node, bitmap_obstack *stack,
1870 func_checker *checker)
1872 if (TREE_THIS_VOLATILE (node->decl) || DECL_HARD_REGISTER (node->decl)
1873 || node->alias)
1874 return NULL;
1876 sem_variable *v = new sem_variable (node, stack);
1877 v->init (checker);
1879 return v;
1882 /* Semantic variable initialization function. */
1884 void
1885 sem_variable::init (ipa_icf_gimple::func_checker *checker)
1887 decl = get_node ()->decl;
1889 /* All WPA streamed in symbols should have their hashes computed at compile
1890 time. At this point, the constructor may not be in memory at all.
1891 DECL_INITIAL (decl) would be error_mark_node in that case. */
1892 if (!m_hash_set)
1894 gcc_assert (!node->lto_file_data);
1895 inchash::hash hstate;
1896 hstate.add_int (456346417);
1897 checker->hash_operand (DECL_INITIAL (decl), hstate, 0);
1898 set_hash (hstate.end ());
1902 /* References independent hash function. */
1904 hashval_t
1905 sem_variable::get_hash (void)
1907 gcc_checking_assert (m_hash_set);
1908 return m_hash;
1911 /* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can
1912 be applied. */
1914 bool
1915 sem_variable::merge (sem_item *alias_item)
1917 gcc_assert (alias_item->type == VAR);
1919 AUTO_DUMP_SCOPE ("merge",
1920 dump_user_location_t::from_function_decl (decl));
1921 if (!sem_item::target_supports_symbol_aliases_p ())
1923 if (dump_enabled_p ())
1924 dump_printf (MSG_MISSED_OPTIMIZATION, "Not unifying; "
1925 "Symbol aliases are not supported by target\n");
1926 return false;
1929 if (DECL_EXTERNAL (alias_item->decl))
1931 if (dump_enabled_p ())
1932 dump_printf (MSG_MISSED_OPTIMIZATION,
1933 "Not unifying; alias is external.\n");
1934 return false;
1937 sem_variable *alias_var = static_cast<sem_variable *> (alias_item);
1939 varpool_node *original = get_node ();
1940 varpool_node *alias = alias_var->get_node ();
1941 bool original_discardable = false;
1943 bool alias_address_matters = alias->address_matters_p ();
1945 /* See if original is in a section that can be discarded if the main
1946 symbol is not used.
1947 Also consider case where we have resolution info and we know that
1948 original's definition is not going to be used. In this case we cannot
1949 create alias to original. */
1950 if (original->can_be_discarded_p ()
1951 || (node->resolution != LDPR_UNKNOWN
1952 && !decl_binds_to_current_def_p (node->decl)))
1953 original_discardable = true;
1955 gcc_assert (!TREE_ASM_WRITTEN (alias->decl));
1957 /* Constant pool machinery is not quite ready for aliases.
1958 TODO: varasm code contains logic for merging DECL_IN_CONSTANT_POOL.
1959 For LTO merging does not happen that is an important missing feature.
1960 We can enable merging with LTO if the DECL_IN_CONSTANT_POOL
1961 flag is dropped and non-local symbol name is assigned. */
1962 if (DECL_IN_CONSTANT_POOL (alias->decl)
1963 || DECL_IN_CONSTANT_POOL (original->decl))
1965 if (dump_enabled_p ())
1966 dump_printf (MSG_MISSED_OPTIMIZATION,
1967 "Not unifying; constant pool variables.\n");
1968 return false;
1971 /* Do not attempt to mix functions from different user sections;
1972 we do not know what user intends with those. */
1973 if (((DECL_SECTION_NAME (original->decl) && !original->implicit_section)
1974 || (DECL_SECTION_NAME (alias->decl) && !alias->implicit_section))
1975 && DECL_SECTION_NAME (original->decl) != DECL_SECTION_NAME (alias->decl))
1977 if (dump_enabled_p ())
1978 dump_printf (MSG_MISSED_OPTIMIZATION,
1979 "Not unifying; "
1980 "original and alias are in different sections.\n");
1981 return false;
1984 /* We cannot merge if address comparison matters. */
1985 if (alias_address_matters && flag_merge_constants < 2)
1987 if (dump_enabled_p ())
1988 dump_printf (MSG_MISSED_OPTIMIZATION,
1989 "Not unifying; address of original may be compared.\n");
1990 return false;
1993 if (DECL_ALIGN (original->decl) < DECL_ALIGN (alias->decl))
1995 if (dump_enabled_p ())
1996 dump_printf (MSG_MISSED_OPTIMIZATION,
1997 "Not unifying; "
1998 "original and alias have incompatible alignments\n");
2000 return false;
2003 if (DECL_COMDAT_GROUP (original->decl) != DECL_COMDAT_GROUP (alias->decl))
2005 if (dump_enabled_p ())
2006 dump_printf (MSG_MISSED_OPTIMIZATION,
2007 "Not unifying; alias cannot be created; "
2008 "across comdat group boundary\n");
2010 return false;
2013 if (original_discardable)
2015 if (dump_enabled_p ())
2016 dump_printf (MSG_MISSED_OPTIMIZATION,
2017 "Not unifying; alias cannot be created; "
2018 "target is discardable\n");
2020 return false;
2022 else
2024 gcc_assert (!original->alias);
2025 gcc_assert (!alias->alias);
2027 alias->analyzed = false;
2029 DECL_INITIAL (alias->decl) = NULL;
2030 ((symtab_node *)alias)->call_for_symbol_and_aliases (clear_decl_rtl,
2031 NULL, true);
2032 alias->remove_all_references ();
2033 if (TREE_ADDRESSABLE (alias->decl))
2034 original->call_for_symbol_and_aliases (set_addressable, NULL, true);
2036 varpool_node::create_alias (alias_var->decl, decl);
2037 alias->resolve_alias (original);
2039 if (dump_enabled_p ())
2040 dump_printf (MSG_OPTIMIZED_LOCATIONS,
2041 "Unified; Variable alias has been created.\n");
2043 return true;
2047 /* Dump symbol to FILE. */
2049 void
2050 sem_variable::dump_to_file (FILE *file)
2052 gcc_assert (file);
2054 print_node (file, "", decl, 0);
2055 fprintf (file, "\n\n");
2058 unsigned int sem_item_optimizer::class_id = 0;
2060 sem_item_optimizer::sem_item_optimizer ()
2061 : worklist (0), m_classes (0), m_classes_count (0), m_cgraph_node_hooks (NULL),
2062 m_varpool_node_hooks (NULL), m_merged_variables (), m_references ()
2064 m_items.create (0);
2065 bitmap_obstack_initialize (&m_bmstack);
2068 sem_item_optimizer::~sem_item_optimizer ()
2070 for (unsigned int i = 0; i < m_items.length (); i++)
2071 delete m_items[i];
2074 for (hash_table<congruence_class_hash>::iterator it = m_classes.begin ();
2075 it != m_classes.end (); ++it)
2077 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
2078 delete (*it)->classes[i];
2080 (*it)->classes.release ();
2081 free (*it);
2084 m_items.release ();
2086 bitmap_obstack_release (&m_bmstack);
2087 m_merged_variables.release ();
2090 /* Write IPA ICF summary for symbols. */
2092 void
2093 sem_item_optimizer::write_summary (void)
2095 unsigned int count = 0;
2097 output_block *ob = create_output_block (LTO_section_ipa_icf);
2098 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
2099 ob->symbol = NULL;
2101 /* Calculate number of symbols to be serialized. */
2102 for (lto_symtab_encoder_iterator lsei = lsei_start_in_partition (encoder);
2103 !lsei_end_p (lsei);
2104 lsei_next_in_partition (&lsei))
2106 symtab_node *node = lsei_node (lsei);
2108 if (m_symtab_node_map.get (node))
2109 count++;
2112 streamer_write_uhwi (ob, count);
2114 /* Process all of the symbols. */
2115 for (lto_symtab_encoder_iterator lsei = lsei_start_in_partition (encoder);
2116 !lsei_end_p (lsei);
2117 lsei_next_in_partition (&lsei))
2119 symtab_node *node = lsei_node (lsei);
2121 sem_item **item = m_symtab_node_map.get (node);
2123 if (item && *item)
2125 int node_ref = lto_symtab_encoder_encode (encoder, node);
2126 streamer_write_uhwi_stream (ob->main_stream, node_ref);
2128 streamer_write_uhwi (ob, (*item)->get_hash ());
2132 streamer_write_char_stream (ob->main_stream, 0);
2133 produce_asm (ob, NULL);
2134 destroy_output_block (ob);
2137 /* Reads a section from LTO stream file FILE_DATA. Input block for DATA
2138 contains LEN bytes. */
2140 void
2141 sem_item_optimizer::read_section (lto_file_decl_data *file_data,
2142 const char *data, size_t len)
2144 const lto_function_header *header
2145 = (const lto_function_header *) data;
2146 const int cfg_offset = sizeof (lto_function_header);
2147 const int main_offset = cfg_offset + header->cfg_size;
2148 const int string_offset = main_offset + header->main_size;
2149 data_in *data_in;
2150 unsigned int i;
2151 unsigned int count;
2153 lto_input_block ib_main ((const char *) data + main_offset, 0,
2154 header->main_size, file_data->mode_table);
2156 data_in
2157 = lto_data_in_create (file_data, (const char *) data + string_offset,
2158 header->string_size, vNULL);
2160 count = streamer_read_uhwi (&ib_main);
2162 for (i = 0; i < count; i++)
2164 unsigned int index;
2165 symtab_node *node;
2166 lto_symtab_encoder_t encoder;
2168 index = streamer_read_uhwi (&ib_main);
2169 encoder = file_data->symtab_node_encoder;
2170 node = lto_symtab_encoder_deref (encoder, index);
2172 hashval_t hash = streamer_read_uhwi (&ib_main);
2173 gcc_assert (node->definition);
2175 if (is_a<cgraph_node *> (node))
2177 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2179 sem_function *fn = new sem_function (cnode, &m_bmstack);
2180 fn->set_hash (hash);
2181 m_items.safe_push (fn);
2183 else
2185 varpool_node *vnode = dyn_cast <varpool_node *> (node);
2187 sem_variable *var = new sem_variable (vnode, &m_bmstack);
2188 var->set_hash (hash);
2189 m_items.safe_push (var);
2193 lto_free_section_data (file_data, LTO_section_ipa_icf, NULL, data,
2194 len);
2195 lto_data_in_delete (data_in);
2198 /* Read IPA ICF summary for symbols. */
2200 void
2201 sem_item_optimizer::read_summary (void)
2203 lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2204 lto_file_decl_data *file_data;
2205 unsigned int j = 0;
2207 while ((file_data = file_data_vec[j++]))
2209 size_t len;
2210 const char *data
2211 = lto_get_summary_section_data (file_data, LTO_section_ipa_icf, &len);
2212 if (data)
2213 read_section (file_data, data, len);
2217 /* Register callgraph and varpool hooks. */
2219 void
2220 sem_item_optimizer::register_hooks (void)
2222 if (!m_cgraph_node_hooks)
2223 m_cgraph_node_hooks = symtab->add_cgraph_removal_hook
2224 (&sem_item_optimizer::cgraph_removal_hook, this);
2226 if (!m_varpool_node_hooks)
2227 m_varpool_node_hooks = symtab->add_varpool_removal_hook
2228 (&sem_item_optimizer::varpool_removal_hook, this);
2231 /* Unregister callgraph and varpool hooks. */
2233 void
2234 sem_item_optimizer::unregister_hooks (void)
2236 if (m_cgraph_node_hooks)
2237 symtab->remove_cgraph_removal_hook (m_cgraph_node_hooks);
2239 if (m_varpool_node_hooks)
2240 symtab->remove_varpool_removal_hook (m_varpool_node_hooks);
2243 /* Adds a CLS to hashtable associated by hash value. */
2245 void
2246 sem_item_optimizer::add_class (congruence_class *cls)
2248 gcc_assert (cls->members.length ());
2250 congruence_class_group *group
2251 = get_group_by_hash (cls->members[0]->get_hash (),
2252 cls->members[0]->type);
2253 group->classes.safe_push (cls);
2256 /* Gets a congruence class group based on given HASH value and TYPE. */
2258 congruence_class_group *
2259 sem_item_optimizer::get_group_by_hash (hashval_t hash, sem_item_type type)
2261 congruence_class_group *item = XNEW (congruence_class_group);
2262 item->hash = hash;
2263 item->type = type;
2265 congruence_class_group **slot = m_classes.find_slot (item, INSERT);
2267 if (*slot)
2268 free (item);
2269 else
2271 item->classes.create (1);
2272 *slot = item;
2275 return *slot;
2278 /* Callgraph removal hook called for a NODE with a custom DATA. */
2280 void
2281 sem_item_optimizer::cgraph_removal_hook (cgraph_node *node, void *data)
2283 sem_item_optimizer *optimizer = (sem_item_optimizer *) data;
2284 optimizer->remove_symtab_node (node);
2287 /* Varpool removal hook called for a NODE with a custom DATA. */
2289 void
2290 sem_item_optimizer::varpool_removal_hook (varpool_node *node, void *data)
2292 sem_item_optimizer *optimizer = (sem_item_optimizer *) data;
2293 optimizer->remove_symtab_node (node);
2296 /* Remove symtab NODE triggered by symtab removal hooks. */
2298 void
2299 sem_item_optimizer::remove_symtab_node (symtab_node *node)
2301 gcc_assert (m_classes.is_empty ());
2303 m_removed_items_set.add (node);
2306 void
2307 sem_item_optimizer::remove_item (sem_item *item)
2309 if (m_symtab_node_map.get (item->node))
2310 m_symtab_node_map.remove (item->node);
2311 delete item;
2314 /* Removes all callgraph and varpool nodes that are marked by symtab
2315 as deleted. */
2317 void
2318 sem_item_optimizer::filter_removed_items (void)
2320 auto_vec <sem_item *> filtered;
2322 for (unsigned int i = 0; i < m_items.length(); i++)
2324 sem_item *item = m_items[i];
2326 if (m_removed_items_set.contains (item->node))
2328 remove_item (item);
2329 continue;
2332 if (item->type == FUNC)
2334 cgraph_node *cnode = static_cast <sem_function *>(item)->get_node ();
2336 if (in_lto_p && (cnode->alias || cnode->body_removed))
2337 remove_item (item);
2338 else
2339 filtered.safe_push (item);
2341 else /* VAR. */
2343 if (!flag_ipa_icf_variables)
2344 remove_item (item);
2345 else
2347 /* Filter out non-readonly variables. */
2348 tree decl = item->decl;
2349 if (TREE_READONLY (decl))
2350 filtered.safe_push (item);
2351 else
2352 remove_item (item);
2357 /* Clean-up of released semantic items. */
2359 m_items.release ();
2360 for (unsigned int i = 0; i < filtered.length(); i++)
2361 m_items.safe_push (filtered[i]);
2364 /* Optimizer entry point which returns true in case it processes
2365 a merge operation. True is returned if there's a merge operation
2366 processed. */
2368 bool
2369 sem_item_optimizer::execute (void)
2371 filter_removed_items ();
2372 unregister_hooks ();
2374 build_graph ();
2375 update_hash_by_addr_refs ();
2376 build_hash_based_classes ();
2378 if (dump_file)
2379 fprintf (dump_file, "Dump after hash based groups\n");
2380 dump_cong_classes ();
2382 subdivide_classes_by_equality (true);
2384 if (dump_file)
2385 fprintf (dump_file, "Dump after WPA based types groups\n");
2387 dump_cong_classes ();
2389 process_cong_reduction ();
2390 checking_verify_classes ();
2392 if (dump_file)
2393 fprintf (dump_file, "Dump after callgraph-based congruence reduction\n");
2395 dump_cong_classes ();
2397 unsigned int loaded_symbols = parse_nonsingleton_classes ();
2398 subdivide_classes_by_equality ();
2400 if (dump_file)
2401 fprintf (dump_file, "Dump after full equality comparison of groups\n");
2403 dump_cong_classes ();
2405 unsigned int prev_class_count = m_classes_count;
2407 process_cong_reduction ();
2408 dump_cong_classes ();
2409 checking_verify_classes ();
2410 bool merged_p = merge_classes (prev_class_count, loaded_symbols);
2412 if (dump_file && (dump_flags & TDF_DETAILS))
2413 symtab->dump (dump_file);
2415 return merged_p;
2418 /* Function responsible for visiting all potential functions and
2419 read-only variables that can be merged. */
2421 void
2422 sem_item_optimizer::parse_funcs_and_vars (void)
2424 cgraph_node *cnode;
2426 /* Create dummy func_checker for hashing purpose. */
2427 func_checker checker;
2429 if (flag_ipa_icf_functions)
2430 FOR_EACH_DEFINED_FUNCTION (cnode)
2432 sem_function *f = sem_function::parse (cnode, &m_bmstack, &checker);
2433 if (f)
2435 m_items.safe_push (f);
2436 m_symtab_node_map.put (cnode, f);
2440 varpool_node *vnode;
2442 if (flag_ipa_icf_variables)
2443 FOR_EACH_DEFINED_VARIABLE (vnode)
2445 sem_variable *v = sem_variable::parse (vnode, &m_bmstack, &checker);
2447 if (v)
2449 m_items.safe_push (v);
2450 m_symtab_node_map.put (vnode, v);
2455 /* Makes pairing between a congruence class CLS and semantic ITEM. */
2457 void
2458 sem_item_optimizer::add_item_to_class (congruence_class *cls, sem_item *item)
2460 item->index_in_class = cls->members.length ();
2461 cls->members.safe_push (item);
2462 cls->referenced_by_count += item->referenced_by_count;
2463 item->cls = cls;
2466 /* For each semantic item, append hash values of references. */
2468 void
2469 sem_item_optimizer::update_hash_by_addr_refs ()
2471 /* First, append to hash sensitive references and class type if it need to
2472 be matched for ODR. */
2473 for (unsigned i = 0; i < m_items.length (); i++)
2475 m_items[i]->update_hash_by_addr_refs (m_symtab_node_map);
2476 if (m_items[i]->type == FUNC)
2478 if (TREE_CODE (TREE_TYPE (m_items[i]->decl)) == METHOD_TYPE
2479 && contains_polymorphic_type_p
2480 (TYPE_METHOD_BASETYPE (TREE_TYPE (m_items[i]->decl)))
2481 && (DECL_CXX_CONSTRUCTOR_P (m_items[i]->decl)
2482 || (static_cast<sem_function *> (m_items[i])->param_used_p (0)
2483 && static_cast<sem_function *> (m_items[i])
2484 ->compare_polymorphic_p ())))
2486 tree class_type
2487 = TYPE_METHOD_BASETYPE (TREE_TYPE (m_items[i]->decl));
2488 inchash::hash hstate (m_items[i]->get_hash ());
2490 if (TYPE_NAME (class_type)
2491 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (class_type)))
2492 hstate.add_hwi
2493 (IDENTIFIER_HASH_VALUE
2494 (DECL_ASSEMBLER_NAME (TYPE_NAME (class_type))));
2496 m_items[i]->set_hash (hstate.end ());
2501 /* Once all symbols have enhanced hash value, we can append
2502 hash values of symbols that are seen by IPA ICF and are
2503 references by a semantic item. Newly computed values
2504 are saved to global_hash member variable. */
2505 for (unsigned i = 0; i < m_items.length (); i++)
2506 m_items[i]->update_hash_by_local_refs (m_symtab_node_map);
2508 /* Global hash value replace current hash values. */
2509 for (unsigned i = 0; i < m_items.length (); i++)
2510 m_items[i]->set_hash (m_items[i]->global_hash);
2513 /* Congruence classes are built by hash value. */
2515 void
2516 sem_item_optimizer::build_hash_based_classes (void)
2518 for (unsigned i = 0; i < m_items.length (); i++)
2520 sem_item *item = m_items[i];
2522 congruence_class_group *group
2523 = get_group_by_hash (item->get_hash (), item->type);
2525 if (!group->classes.length ())
2527 m_classes_count++;
2528 group->classes.safe_push (new congruence_class (class_id++));
2531 add_item_to_class (group->classes[0], item);
2535 /* Build references according to call graph. */
2537 void
2538 sem_item_optimizer::build_graph (void)
2540 for (unsigned i = 0; i < m_items.length (); i++)
2542 sem_item *item = m_items[i];
2543 m_symtab_node_map.put (item->node, item);
2545 /* Initialize hash values if we are not in LTO mode. */
2546 if (!in_lto_p)
2547 item->get_hash ();
2550 for (unsigned i = 0; i < m_items.length (); i++)
2552 sem_item *item = m_items[i];
2554 if (item->type == FUNC)
2556 cgraph_node *cnode = dyn_cast <cgraph_node *> (item->node);
2558 cgraph_edge *e = cnode->callees;
2559 while (e)
2561 sem_item **slot = m_symtab_node_map.get
2562 (e->callee->ultimate_alias_target ());
2563 if (slot)
2564 item->add_reference (&m_references, *slot);
2566 e = e->next_callee;
2570 ipa_ref *ref = NULL;
2571 for (unsigned i = 0; item->node->iterate_reference (i, ref); i++)
2573 sem_item **slot = m_symtab_node_map.get
2574 (ref->referred->ultimate_alias_target ());
2575 if (slot)
2576 item->add_reference (&m_references, *slot);
2581 /* Semantic items in classes having more than one element and initialized.
2582 In case of WPA, we load function body. */
2584 unsigned int
2585 sem_item_optimizer::parse_nonsingleton_classes (void)
2587 unsigned int counter = 0;
2589 /* Create dummy func_checker for hashing purpose. */
2590 func_checker checker;
2592 for (unsigned i = 0; i < m_items.length (); i++)
2593 if (m_items[i]->cls->members.length () > 1)
2595 m_items[i]->init (&checker);
2596 ++counter;
2599 if (dump_file)
2601 float f = m_items.length () ? 100.0f * counter / m_items.length () : 0.0f;
2602 fprintf (dump_file, "Init called for %u items (%.2f%%).\n", counter, f);
2605 return counter;
2608 /* Equality function for semantic items is used to subdivide existing
2609 classes. If IN_WPA, fast equality function is invoked. */
2611 void
2612 sem_item_optimizer::subdivide_classes_by_equality (bool in_wpa)
2614 for (hash_table <congruence_class_hash>::iterator it = m_classes.begin ();
2615 it != m_classes.end (); ++it)
2617 unsigned int class_count = (*it)->classes.length ();
2619 for (unsigned i = 0; i < class_count; i++)
2621 congruence_class *c = (*it)->classes[i];
2623 if (c->members.length() > 1)
2625 auto_vec <sem_item *> new_vector;
2627 sem_item *first = c->members[0];
2628 new_vector.safe_push (first);
2630 unsigned class_split_first = (*it)->classes.length ();
2632 for (unsigned j = 1; j < c->members.length (); j++)
2634 sem_item *item = c->members[j];
2636 bool equals
2637 = in_wpa ? first->equals_wpa (item, m_symtab_node_map)
2638 : first->equals (item, m_symtab_node_map);
2640 if (equals)
2641 new_vector.safe_push (item);
2642 else
2644 bool integrated = false;
2646 for (unsigned k = class_split_first;
2647 k < (*it)->classes.length (); k++)
2649 sem_item *x = (*it)->classes[k]->members[0];
2650 bool equals
2651 = in_wpa ? x->equals_wpa (item, m_symtab_node_map)
2652 : x->equals (item, m_symtab_node_map);
2654 if (equals)
2656 integrated = true;
2657 add_item_to_class ((*it)->classes[k], item);
2659 break;
2663 if (!integrated)
2665 congruence_class *c
2666 = new congruence_class (class_id++);
2667 m_classes_count++;
2668 add_item_to_class (c, item);
2670 (*it)->classes.safe_push (c);
2675 // We replace newly created new_vector for the class we've just
2676 // splitted.
2677 c->members.release ();
2678 c->members.create (new_vector.length ());
2680 for (unsigned int j = 0; j < new_vector.length (); j++)
2681 add_item_to_class (c, new_vector[j]);
2686 checking_verify_classes ();
2689 /* Subdivide classes by address references that members of the class
2690 reference. Example can be a pair of functions that have an address
2691 taken from a function. If these addresses are different the class
2692 is split. */
2694 unsigned
2695 sem_item_optimizer::subdivide_classes_by_sensitive_refs ()
2697 typedef hash_map <symbol_compare_hash, vec <sem_item *> > subdivide_hash_map;
2699 unsigned newly_created_classes = 0;
2701 for (hash_table <congruence_class_hash>::iterator it = m_classes.begin ();
2702 it != m_classes.end (); ++it)
2704 unsigned int class_count = (*it)->classes.length ();
2705 auto_vec<congruence_class *> new_classes;
2707 for (unsigned i = 0; i < class_count; i++)
2709 congruence_class *c = (*it)->classes[i];
2711 if (c->members.length() > 1)
2713 subdivide_hash_map split_map;
2715 for (unsigned j = 0; j < c->members.length (); j++)
2717 sem_item *source_node = c->members[j];
2719 symbol_compare_collection *collection
2720 = new symbol_compare_collection (source_node->node);
2722 bool existed;
2723 vec <sem_item *> *slot
2724 = &split_map.get_or_insert (collection, &existed);
2725 gcc_checking_assert (slot);
2727 slot->safe_push (source_node);
2729 if (existed)
2730 delete collection;
2733 /* If the map contains more than one key, we have to split
2734 the map appropriately. */
2735 if (split_map.elements () != 1)
2737 bool first_class = true;
2739 for (subdivide_hash_map::iterator it2 = split_map.begin ();
2740 it2 != split_map.end (); ++it2)
2742 congruence_class *new_cls;
2743 new_cls = new congruence_class (class_id++);
2745 for (unsigned k = 0; k < (*it2).second.length (); k++)
2746 add_item_to_class (new_cls, (*it2).second[k]);
2748 worklist_push (new_cls);
2749 newly_created_classes++;
2751 if (first_class)
2753 (*it)->classes[i] = new_cls;
2754 first_class = false;
2756 else
2758 new_classes.safe_push (new_cls);
2759 m_classes_count++;
2764 /* Release memory. */
2765 for (subdivide_hash_map::iterator it2 = split_map.begin ();
2766 it2 != split_map.end (); ++it2)
2768 delete (*it2).first;
2769 (*it2).second.release ();
2774 for (unsigned i = 0; i < new_classes.length (); i++)
2775 (*it)->classes.safe_push (new_classes[i]);
2778 return newly_created_classes;
2781 /* Verify congruence classes, if checking is enabled. */
2783 void
2784 sem_item_optimizer::checking_verify_classes (void)
2786 if (flag_checking)
2787 verify_classes ();
2790 /* Verify congruence classes. */
2792 void
2793 sem_item_optimizer::verify_classes (void)
2795 for (hash_table<congruence_class_hash>::iterator it = m_classes.begin ();
2796 it != m_classes.end (); ++it)
2798 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
2800 congruence_class *cls = (*it)->classes[i];
2802 gcc_assert (cls);
2803 gcc_assert (cls->members.length () > 0);
2805 for (unsigned int j = 0; j < cls->members.length (); j++)
2807 sem_item *item = cls->members[j];
2809 gcc_assert (item);
2810 gcc_assert (item->cls == cls);
2816 /* Disposes split map traverse function. CLS_PTR is pointer to congruence
2817 class, BSLOT is bitmap slot we want to release. DATA is mandatory,
2818 but unused argument. */
2820 bool
2821 sem_item_optimizer::release_split_map (congruence_class * const &,
2822 bitmap const &b, traverse_split_pair *)
2824 bitmap bmp = b;
2826 BITMAP_FREE (bmp);
2828 return true;
2831 /* Process split operation for a class given as pointer CLS_PTR,
2832 where bitmap B splits congruence class members. DATA is used
2833 as argument of split pair. */
2835 bool
2836 sem_item_optimizer::traverse_congruence_split (congruence_class * const &cls,
2837 bitmap const &b,
2838 traverse_split_pair *pair)
2840 sem_item_optimizer *optimizer = pair->optimizer;
2841 const congruence_class *splitter_cls = pair->cls;
2843 /* If counted bits are greater than zero and less than the number of members
2844 a group will be splitted. */
2845 unsigned popcount = bitmap_count_bits (b);
2847 if (popcount > 0 && popcount < cls->members.length ())
2849 auto_vec <congruence_class *, 2> newclasses;
2850 newclasses.quick_push (new congruence_class (class_id++));
2851 newclasses.quick_push (new congruence_class (class_id++));
2853 for (unsigned int i = 0; i < cls->members.length (); i++)
2855 int target = bitmap_bit_p (b, i);
2856 congruence_class *tc = newclasses[target];
2858 add_item_to_class (tc, cls->members[i]);
2861 if (flag_checking)
2863 for (unsigned int i = 0; i < 2; i++)
2864 gcc_assert (newclasses[i]->members.length ());
2867 if (splitter_cls == cls)
2868 optimizer->splitter_class_removed = true;
2870 /* Remove old class from worklist if presented. */
2871 bool in_worklist = cls->in_worklist;
2873 if (in_worklist)
2874 cls->in_worklist = false;
2876 congruence_class_group g;
2877 g.hash = cls->members[0]->get_hash ();
2878 g.type = cls->members[0]->type;
2880 congruence_class_group *slot = optimizer->m_classes.find (&g);
2882 for (unsigned int i = 0; i < slot->classes.length (); i++)
2883 if (slot->classes[i] == cls)
2885 slot->classes.ordered_remove (i);
2886 break;
2889 /* New class will be inserted and integrated to work list. */
2890 for (unsigned int i = 0; i < 2; i++)
2891 optimizer->add_class (newclasses[i]);
2893 /* Two classes replace one, so that increment just by one. */
2894 optimizer->m_classes_count++;
2896 /* If OLD class was presented in the worklist, we remove the class
2897 and replace it will both newly created classes. */
2898 if (in_worklist)
2899 for (unsigned int i = 0; i < 2; i++)
2900 optimizer->worklist_push (newclasses[i]);
2901 else /* Just smaller class is inserted. */
2903 unsigned int smaller_index
2904 = (newclasses[0]->members.length ()
2905 < newclasses[1]->members.length ()
2906 ? 0 : 1);
2907 optimizer->worklist_push (newclasses[smaller_index]);
2910 if (dump_file && (dump_flags & TDF_DETAILS))
2912 fprintf (dump_file, " congruence class splitted:\n");
2913 cls->dump (dump_file, 4);
2915 fprintf (dump_file, " newly created groups:\n");
2916 for (unsigned int i = 0; i < 2; i++)
2917 newclasses[i]->dump (dump_file, 4);
2920 /* Release class if not presented in work list. */
2921 if (!in_worklist)
2922 delete cls;
2924 return true;
2927 return false;
2930 /* Compare function for sorting pairs in do_congruence_step_f. */
2933 sem_item_optimizer::sort_congruence_split (const void *a_, const void *b_)
2935 const std::pair<congruence_class *, bitmap> *a
2936 = (const std::pair<congruence_class *, bitmap> *)a_;
2937 const std::pair<congruence_class *, bitmap> *b
2938 = (const std::pair<congruence_class *, bitmap> *)b_;
2939 if (a->first->id < b->first->id)
2940 return -1;
2941 else if (a->first->id > b->first->id)
2942 return 1;
2943 return 0;
2946 /* Tests if a class CLS used as INDEXth splits any congruence classes.
2947 Bitmap stack BMSTACK is used for bitmap allocation. */
2949 bool
2950 sem_item_optimizer::do_congruence_step_for_index (congruence_class *cls,
2951 unsigned int index)
2953 hash_map <congruence_class *, bitmap> split_map;
2955 for (unsigned int i = 0; i < cls->members.length (); i++)
2957 sem_item *item = cls->members[i];
2958 sem_usage_pair needle (item, index);
2959 vec<sem_item *> *callers = m_references.get (&needle);
2960 if (callers == NULL)
2961 continue;
2963 for (unsigned int j = 0; j < callers->length (); j++)
2965 sem_item *caller = (*callers)[j];
2966 if (caller->cls->members.length () < 2)
2967 continue;
2968 bitmap *slot = split_map.get (caller->cls);
2969 bitmap b;
2971 if(!slot)
2973 b = BITMAP_ALLOC (&m_bmstack);
2974 split_map.put (caller->cls, b);
2976 else
2977 b = *slot;
2979 gcc_checking_assert (caller->cls);
2980 gcc_checking_assert (caller->index_in_class
2981 < caller->cls->members.length ());
2983 bitmap_set_bit (b, caller->index_in_class);
2987 auto_vec<std::pair<congruence_class *, bitmap> > to_split;
2988 to_split.reserve_exact (split_map.elements ());
2989 for (hash_map <congruence_class *, bitmap>::iterator i = split_map.begin ();
2990 i != split_map.end (); ++i)
2991 to_split.safe_push (*i);
2992 to_split.qsort (sort_congruence_split);
2994 traverse_split_pair pair;
2995 pair.optimizer = this;
2996 pair.cls = cls;
2998 splitter_class_removed = false;
2999 bool r = false;
3000 for (unsigned i = 0; i < to_split.length (); ++i)
3001 r |= traverse_congruence_split (to_split[i].first, to_split[i].second,
3002 &pair);
3004 /* Bitmap clean-up. */
3005 split_map.traverse <traverse_split_pair *,
3006 sem_item_optimizer::release_split_map> (NULL);
3008 return r;
3011 /* Every usage of a congruence class CLS is a candidate that can split the
3012 collection of classes. Bitmap stack BMSTACK is used for bitmap
3013 allocation. */
3015 void
3016 sem_item_optimizer::do_congruence_step (congruence_class *cls)
3018 bitmap_iterator bi;
3019 unsigned int i;
3021 bitmap usage = BITMAP_ALLOC (&m_bmstack);
3023 for (unsigned int i = 0; i < cls->members.length (); i++)
3024 bitmap_ior_into (usage, cls->members[i]->usage_index_bitmap);
3026 EXECUTE_IF_SET_IN_BITMAP (usage, 0, i, bi)
3028 if (dump_file && (dump_flags & TDF_DETAILS))
3029 fprintf (dump_file, " processing congruence step for class: %u "
3030 "(%u items, %u references), index: %u\n", cls->id,
3031 cls->referenced_by_count, cls->members.length (), i);
3032 do_congruence_step_for_index (cls, i);
3034 if (splitter_class_removed)
3035 break;
3038 BITMAP_FREE (usage);
3041 /* Adds a newly created congruence class CLS to worklist. */
3043 void
3044 sem_item_optimizer::worklist_push (congruence_class *cls)
3046 /* Return if the class CLS is already presented in work list. */
3047 if (cls->in_worklist)
3048 return;
3050 cls->in_worklist = true;
3051 worklist.insert (cls->referenced_by_count, cls);
3054 /* Pops a class from worklist. */
3056 congruence_class *
3057 sem_item_optimizer::worklist_pop (void)
3059 congruence_class *cls;
3061 while (!worklist.empty ())
3063 cls = worklist.extract_min ();
3064 if (cls->in_worklist)
3066 cls->in_worklist = false;
3068 return cls;
3070 else
3072 /* Work list item was already intended to be removed.
3073 The only reason for doing it is to split a class.
3074 Thus, the class CLS is deleted. */
3075 delete cls;
3079 return NULL;
3082 /* Iterative congruence reduction function. */
3084 void
3085 sem_item_optimizer::process_cong_reduction (void)
3087 for (hash_table<congruence_class_hash>::iterator it = m_classes.begin ();
3088 it != m_classes.end (); ++it)
3089 for (unsigned i = 0; i < (*it)->classes.length (); i++)
3090 if ((*it)->classes[i]->is_class_used ())
3091 worklist_push ((*it)->classes[i]);
3093 if (dump_file)
3094 fprintf (dump_file, "Worklist has been filled with: %lu\n",
3095 (unsigned long) worklist.nodes ());
3097 if (dump_file && (dump_flags & TDF_DETAILS))
3098 fprintf (dump_file, "Congruence class reduction\n");
3100 congruence_class *cls;
3102 /* Process complete congruence reduction. */
3103 while ((cls = worklist_pop ()) != NULL)
3104 do_congruence_step (cls);
3106 /* Subdivide newly created classes according to references. */
3107 unsigned new_classes = subdivide_classes_by_sensitive_refs ();
3109 if (dump_file)
3110 fprintf (dump_file, "Address reference subdivision created: %u "
3111 "new classes.\n", new_classes);
3114 /* Debug function prints all informations about congruence classes. */
3116 void
3117 sem_item_optimizer::dump_cong_classes (void)
3119 if (!dump_file)
3120 return;
3122 /* Histogram calculation. */
3123 unsigned int max_index = 0;
3124 unsigned int single_element_classes = 0;
3125 unsigned int* histogram = XCNEWVEC (unsigned int, m_items.length () + 1);
3127 for (hash_table<congruence_class_hash>::iterator it = m_classes.begin ();
3128 it != m_classes.end (); ++it)
3129 for (unsigned i = 0; i < (*it)->classes.length (); i++)
3131 unsigned int c = (*it)->classes[i]->members.length ();
3132 histogram[c]++;
3134 if (c > max_index)
3135 max_index = c;
3137 if (c == 1)
3138 ++single_element_classes;
3141 fprintf (dump_file,
3142 "Congruence classes: %lu with total: %u items (in a non-singular "
3143 "class: %u)\n", (unsigned long) m_classes.elements (),
3144 m_items.length (), m_items.length () - single_element_classes);
3145 fprintf (dump_file,
3146 "Class size histogram [number of members]: number of classes\n");
3147 for (unsigned int i = 0; i <= max_index; i++)
3148 if (histogram[i])
3149 fprintf (dump_file, "%6u: %6u\n", i, histogram[i]);
3151 if (dump_flags & TDF_DETAILS)
3152 for (hash_table<congruence_class_hash>::iterator it = m_classes.begin ();
3153 it != m_classes.end (); ++it)
3155 fprintf (dump_file, " group: with %u classes:\n",
3156 (*it)->classes.length ());
3158 for (unsigned i = 0; i < (*it)->classes.length (); i++)
3160 (*it)->classes[i]->dump (dump_file, 4);
3162 if (i < (*it)->classes.length () - 1)
3163 fprintf (dump_file, " ");
3167 free (histogram);
3170 /* Sort pair of sem_items A and B by DECL_UID. */
3172 static int
3173 sort_sem_items_by_decl_uid (const void *a, const void *b)
3175 const sem_item *i1 = *(const sem_item * const *)a;
3176 const sem_item *i2 = *(const sem_item * const *)b;
3178 int uid1 = DECL_UID (i1->decl);
3179 int uid2 = DECL_UID (i2->decl);
3180 return uid1 - uid2;
3183 /* Sort pair of congruence_classes A and B by DECL_UID of the first member. */
3185 static int
3186 sort_congruence_classes_by_decl_uid (const void *a, const void *b)
3188 const congruence_class *c1 = *(const congruence_class * const *)a;
3189 const congruence_class *c2 = *(const congruence_class * const *)b;
3191 int uid1 = DECL_UID (c1->members[0]->decl);
3192 int uid2 = DECL_UID (c2->members[0]->decl);
3193 return uid1 - uid2;
3196 /* Sort pair of congruence_class_groups A and B by
3197 DECL_UID of the first member of a first group. */
3199 static int
3200 sort_congruence_class_groups_by_decl_uid (const void *a, const void *b)
3202 const std::pair<congruence_class_group *, int> *g1
3203 = (const std::pair<congruence_class_group *, int> *) a;
3204 const std::pair<congruence_class_group *, int> *g2
3205 = (const std::pair<congruence_class_group *, int> *) b;
3206 return g1->second - g2->second;
3209 /* After reduction is done, we can declare all items in a group
3210 to be equal. PREV_CLASS_COUNT is start number of classes
3211 before reduction. True is returned if there's a merge operation
3212 processed. LOADED_SYMBOLS is number of symbols that were loaded
3213 in WPA. */
3215 bool
3216 sem_item_optimizer::merge_classes (unsigned int prev_class_count,
3217 unsigned int loaded_symbols)
3219 unsigned int item_count = m_items.length ();
3220 unsigned int class_count = m_classes_count;
3221 unsigned int equal_items = item_count - class_count;
3223 unsigned int non_singular_classes_count = 0;
3224 unsigned int non_singular_classes_sum = 0;
3226 bool merged_p = false;
3228 /* PR lto/78211
3229 Sort functions in congruence classes by DECL_UID and do the same
3230 for the classes to not to break -fcompare-debug. */
3232 for (hash_table<congruence_class_hash>::iterator it = m_classes.begin ();
3233 it != m_classes.end (); ++it)
3235 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
3237 congruence_class *c = (*it)->classes[i];
3238 c->members.qsort (sort_sem_items_by_decl_uid);
3241 (*it)->classes.qsort (sort_congruence_classes_by_decl_uid);
3244 for (hash_table<congruence_class_hash>::iterator it = m_classes.begin ();
3245 it != m_classes.end (); ++it)
3246 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
3248 congruence_class *c = (*it)->classes[i];
3249 if (c->members.length () > 1)
3251 non_singular_classes_count++;
3252 non_singular_classes_sum += c->members.length ();
3256 auto_vec<std::pair<congruence_class_group *, int> > classes (
3257 m_classes.elements ());
3258 for (hash_table<congruence_class_hash>::iterator it = m_classes.begin ();
3259 it != m_classes.end (); ++it)
3261 int uid = DECL_UID ((*it)->classes[0]->members[0]->decl);
3262 classes.quick_push (std::pair<congruence_class_group *, int> (*it, uid));
3265 classes.qsort (sort_congruence_class_groups_by_decl_uid);
3267 if (dump_file)
3269 fprintf (dump_file, "\nItem count: %u\n", item_count);
3270 fprintf (dump_file, "Congruent classes before: %u, after: %u\n",
3271 prev_class_count, class_count);
3272 fprintf (dump_file, "Average class size before: %.2f, after: %.2f\n",
3273 prev_class_count ? 1.0f * item_count / prev_class_count : 0.0f,
3274 class_count ? 1.0f * item_count / class_count : 0.0f);
3275 fprintf (dump_file, "Average non-singular class size: %.2f, count: %u\n",
3276 non_singular_classes_count ? 1.0f * non_singular_classes_sum /
3277 non_singular_classes_count : 0.0f,
3278 non_singular_classes_count);
3279 fprintf (dump_file, "Equal symbols: %u\n", equal_items);
3280 unsigned total = equal_items + non_singular_classes_count;
3281 fprintf (dump_file, "Totally needed symbols: %u"
3282 ", fraction of loaded symbols: %.2f%%\n\n", total,
3283 loaded_symbols ? 100.0f * total / loaded_symbols: 0.0f);
3286 unsigned int l;
3287 std::pair<congruence_class_group *, int> *it;
3288 FOR_EACH_VEC_ELT (classes, l, it)
3289 for (unsigned int i = 0; i < it->first->classes.length (); i++)
3291 congruence_class *c = it->first->classes[i];
3293 if (c->members.length () == 1)
3294 continue;
3296 sem_item *source = c->members[0];
3298 if (DECL_NAME (source->decl)
3299 && MAIN_NAME_P (DECL_NAME (source->decl)))
3300 /* If merge via wrappers, picking main as the target can be
3301 problematic. */
3302 source = c->members[1];
3304 for (unsigned int j = 0; j < c->members.length (); j++)
3306 sem_item *alias = c->members[j];
3308 if (alias == source)
3309 continue;
3311 dump_user_location_t loc
3312 = dump_user_location_t::from_function_decl (source->decl);
3313 if (dump_enabled_p ())
3315 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
3316 "Semantic equality hit:%s->%s\n",
3317 source->node->dump_name (),
3318 alias->node->dump_name ());
3319 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
3320 "Assembler symbol names:%s->%s\n",
3321 source->node->dump_asm_name (),
3322 alias->node->dump_asm_name ());
3325 if (lookup_attribute ("no_icf", DECL_ATTRIBUTES (alias->decl)))
3327 if (dump_enabled_p ())
3328 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
3329 "Merge operation is skipped due to no_icf "
3330 "attribute.\n");
3331 continue;
3334 if (dump_file && (dump_flags & TDF_DETAILS))
3336 source->dump_to_file (dump_file);
3337 alias->dump_to_file (dump_file);
3340 if (dbg_cnt (merged_ipa_icf))
3342 bool merged = source->merge (alias);
3343 merged_p |= merged;
3345 if (merged && alias->type == VAR)
3347 symtab_pair p = symtab_pair (source->node, alias->node);
3348 m_merged_variables.safe_push (p);
3354 if (!m_merged_variables.is_empty ())
3355 fixup_points_to_sets ();
3357 return merged_p;
3360 /* Fixup points to set PT. */
3362 void
3363 sem_item_optimizer::fixup_pt_set (struct pt_solution *pt)
3365 if (pt->vars == NULL)
3366 return;
3368 unsigned i;
3369 symtab_pair *item;
3370 FOR_EACH_VEC_ELT (m_merged_variables, i, item)
3371 if (bitmap_bit_p (pt->vars, DECL_UID (item->second->decl)))
3372 bitmap_set_bit (pt->vars, DECL_UID (item->first->decl));
3375 /* Set all points-to UIDs of aliases pointing to node N as UID. */
3377 static void
3378 set_alias_uids (symtab_node *n, int uid)
3380 ipa_ref *ref;
3381 FOR_EACH_ALIAS (n, ref)
3383 if (dump_file)
3384 fprintf (dump_file, " Setting points-to UID of [%s] as %d\n",
3385 ref->referring->dump_asm_name (), uid);
3387 SET_DECL_PT_UID (ref->referring->decl, uid);
3388 set_alias_uids (ref->referring, uid);
3392 /* Fixup points to analysis info. */
3394 void
3395 sem_item_optimizer::fixup_points_to_sets (void)
3397 /* TODO: remove in GCC 9 and trigger PTA re-creation after IPA passes. */
3398 cgraph_node *cnode;
3400 FOR_EACH_DEFINED_FUNCTION (cnode)
3402 tree name;
3403 unsigned i;
3404 function *fn = DECL_STRUCT_FUNCTION (cnode->decl);
3405 if (!gimple_in_ssa_p (fn))
3406 continue;
3408 FOR_EACH_SSA_NAME (i, name, fn)
3409 if (POINTER_TYPE_P (TREE_TYPE (name))
3410 && SSA_NAME_PTR_INFO (name))
3411 fixup_pt_set (&SSA_NAME_PTR_INFO (name)->pt);
3412 fixup_pt_set (&fn->gimple_df->escaped);
3414 /* The above gets us to 99% I guess, at least catching the
3415 address compares. Below also gets us aliasing correct
3416 but as said we're giving leeway to the situation with
3417 readonly vars anyway, so ... */
3418 basic_block bb;
3419 FOR_EACH_BB_FN (bb, fn)
3420 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
3421 gsi_next (&gsi))
3423 gcall *call = dyn_cast<gcall *> (gsi_stmt (gsi));
3424 if (call)
3426 fixup_pt_set (gimple_call_use_set (call));
3427 fixup_pt_set (gimple_call_clobber_set (call));
3432 unsigned i;
3433 symtab_pair *item;
3434 FOR_EACH_VEC_ELT (m_merged_variables, i, item)
3435 set_alias_uids (item->first, DECL_UID (item->first->decl));
3438 /* Dump function prints all class members to a FILE with an INDENT. */
3440 void
3441 congruence_class::dump (FILE *file, unsigned int indent) const
3443 FPRINTF_SPACES (file, indent, "class with id: %u, hash: %u, items: %u\n",
3444 id, members[0]->get_hash (), members.length ());
3446 FPUTS_SPACES (file, indent + 2, "");
3447 for (unsigned i = 0; i < members.length (); i++)
3448 fprintf (file, "%s ", members[i]->node->dump_asm_name ());
3450 fprintf (file, "\n");
3453 /* Returns true if there's a member that is used from another group. */
3455 bool
3456 congruence_class::is_class_used (void)
3458 for (unsigned int i = 0; i < members.length (); i++)
3459 if (members[i]->referenced_by_count)
3460 return true;
3462 return false;
3465 /* Generate pass summary for IPA ICF pass. */
3467 static void
3468 ipa_icf_generate_summary (void)
3470 if (!optimizer)
3471 optimizer = new sem_item_optimizer ();
3473 optimizer->register_hooks ();
3474 optimizer->parse_funcs_and_vars ();
3477 /* Write pass summary for IPA ICF pass. */
3479 static void
3480 ipa_icf_write_summary (void)
3482 gcc_assert (optimizer);
3484 optimizer->write_summary ();
3487 /* Read pass summary for IPA ICF pass. */
3489 static void
3490 ipa_icf_read_summary (void)
3492 if (!optimizer)
3493 optimizer = new sem_item_optimizer ();
3495 optimizer->read_summary ();
3496 optimizer->register_hooks ();
3499 /* Semantic equality execution function. */
3501 static unsigned int
3502 ipa_icf_driver (void)
3504 gcc_assert (optimizer);
3506 bool merged_p = optimizer->execute ();
3508 delete optimizer;
3509 optimizer = NULL;
3511 return merged_p ? TODO_remove_functions : 0;
3514 const pass_data pass_data_ipa_icf =
3516 IPA_PASS, /* type */
3517 "icf", /* name */
3518 OPTGROUP_IPA, /* optinfo_flags */
3519 TV_IPA_ICF, /* tv_id */
3520 0, /* properties_required */
3521 0, /* properties_provided */
3522 0, /* properties_destroyed */
3523 0, /* todo_flags_start */
3524 0, /* todo_flags_finish */
3527 class pass_ipa_icf : public ipa_opt_pass_d
3529 public:
3530 pass_ipa_icf (gcc::context *ctxt)
3531 : ipa_opt_pass_d (pass_data_ipa_icf, ctxt,
3532 ipa_icf_generate_summary, /* generate_summary */
3533 ipa_icf_write_summary, /* write_summary */
3534 ipa_icf_read_summary, /* read_summary */
3535 NULL, /*
3536 write_optimization_summary */
3537 NULL, /*
3538 read_optimization_summary */
3539 NULL, /* stmt_fixup */
3540 0, /* function_transform_todo_flags_start */
3541 NULL, /* function_transform */
3542 NULL) /* variable_transform */
3545 /* opt_pass methods: */
3546 virtual bool gate (function *)
3548 return in_lto_p || flag_ipa_icf_variables || flag_ipa_icf_functions;
3551 virtual unsigned int execute (function *)
3553 return ipa_icf_driver();
3555 }; // class pass_ipa_icf
3557 } // ipa_icf namespace
3559 ipa_opt_pass_d *
3560 make_pass_ipa_icf (gcc::context *ctxt)
3562 return new ipa_icf::pass_ipa_icf (ctxt);