fix __builtin___clear_cache overrider fallout
[official-gcc.git] / gcc / ipa-icf.c
blob5bbbec6c806fac05054010fea795a91f73020f5e
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 "tree-streamer.h"
70 #include "fold-const.h"
71 #include "calls.h"
72 #include "varasm.h"
73 #include "gimple-iterator.h"
74 #include "tree-cfg.h"
75 #include "symbol-summary.h"
76 #include "ipa-prop.h"
77 #include "ipa-fnsummary.h"
78 #include "except.h"
79 #include "attribs.h"
80 #include "print-tree.h"
81 #include "ipa-utils.h"
82 #include "tree-ssa-alias-compare.h"
83 #include "ipa-icf-gimple.h"
84 #include "fibonacci_heap.h"
85 #include "ipa-icf.h"
86 #include "stor-layout.h"
87 #include "dbgcnt.h"
88 #include "tree-vector-builder.h"
89 #include "symtab-thunks.h"
90 #include "alias.h"
92 using namespace ipa_icf_gimple;
94 namespace ipa_icf {
96 /* Initialization and computation of symtab node hash, there data
97 are propagated later on. */
99 static sem_item_optimizer *optimizer = NULL;
101 /* Constructor. */
103 symbol_compare_collection::symbol_compare_collection (symtab_node *node)
105 m_references.create (0);
106 m_interposables.create (0);
108 ipa_ref *ref;
110 if (is_a <varpool_node *> (node) && DECL_VIRTUAL_P (node->decl))
111 return;
113 for (unsigned i = 0; node->iterate_reference (i, ref); i++)
115 if (ref->address_matters_p ())
116 m_references.safe_push (ref->referred);
118 if (ref->referred->get_availability () <= AVAIL_INTERPOSABLE)
120 if (ref->address_matters_p ())
121 m_references.safe_push (ref->referred);
122 else
123 m_interposables.safe_push (ref->referred);
127 if (is_a <cgraph_node *> (node))
129 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
131 for (cgraph_edge *e = cnode->callees; e; e = e->next_callee)
132 if (e->callee->get_availability () <= AVAIL_INTERPOSABLE)
133 m_interposables.safe_push (e->callee);
137 /* Constructor for key value pair, where _ITEM is key and _INDEX is a target. */
139 sem_usage_pair::sem_usage_pair (sem_item *_item, unsigned int _index)
140 : item (_item), index (_index)
144 sem_item::sem_item (sem_item_type _type, bitmap_obstack *stack)
145 : type (_type), referenced_by_count (0), m_hash (-1), m_hash_set (false)
147 setup (stack);
150 sem_item::sem_item (sem_item_type _type, symtab_node *_node,
151 bitmap_obstack *stack)
152 : type (_type), node (_node), referenced_by_count (0), m_hash (-1),
153 m_hash_set (false)
155 decl = node->decl;
156 setup (stack);
159 /* Add reference to a semantic TARGET. */
161 void
162 sem_item::add_reference (ref_map *refs,
163 sem_item *target)
165 unsigned index = reference_count++;
166 bool existed;
168 vec<sem_item *> &v
169 = refs->get_or_insert (new sem_usage_pair (target, index), &existed);
170 v.safe_push (this);
171 bitmap_set_bit (target->usage_index_bitmap, index);
172 refs_set.add (target->node);
173 ++target->referenced_by_count;
176 /* Initialize internal data structures. Bitmap STACK is used for
177 bitmap memory allocation process. */
179 void
180 sem_item::setup (bitmap_obstack *stack)
182 gcc_checking_assert (node);
184 reference_count = 0;
185 tree_refs.create (0);
186 usage_index_bitmap = BITMAP_ALLOC (stack);
189 sem_item::~sem_item ()
191 tree_refs.release ();
193 BITMAP_FREE (usage_index_bitmap);
196 /* Dump function for debugging purpose. */
198 DEBUG_FUNCTION void
199 sem_item::dump (void)
201 if (dump_file)
203 fprintf (dump_file, "[%s] %s (tree:%p)\n", type == FUNC ? "func" : "var",
204 node->dump_name (), (void *) node->decl);
205 fprintf (dump_file, " hash: %u\n", get_hash ());
209 /* Return true if target supports alias symbols. */
211 bool
212 sem_item::target_supports_symbol_aliases_p (void)
214 #if !defined (ASM_OUTPUT_DEF) || (!defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL))
215 return false;
216 #else
217 return true;
218 #endif
221 void sem_item::set_hash (hashval_t hash)
223 m_hash = hash;
224 m_hash_set = true;
227 hash_map<const_tree, hashval_t> sem_item::m_type_hash_cache;
229 /* Semantic function constructor that uses STACK as bitmap memory stack. */
231 sem_function::sem_function (bitmap_obstack *stack)
232 : sem_item (FUNC, stack), memory_access_types (), m_alias_sets_hash (0),
233 m_checker (NULL), m_compared_func (NULL)
235 bb_sizes.create (0);
236 bb_sorted.create (0);
239 sem_function::sem_function (cgraph_node *node, bitmap_obstack *stack)
240 : sem_item (FUNC, node, stack), memory_access_types (),
241 m_alias_sets_hash (0), m_checker (NULL), m_compared_func (NULL)
243 bb_sizes.create (0);
244 bb_sorted.create (0);
247 sem_function::~sem_function ()
249 for (unsigned i = 0; i < bb_sorted.length (); i++)
250 delete (bb_sorted[i]);
252 bb_sizes.release ();
253 bb_sorted.release ();
256 /* Calculates hash value based on a BASIC_BLOCK. */
258 hashval_t
259 sem_function::get_bb_hash (const sem_bb *basic_block)
261 inchash::hash hstate;
263 hstate.add_int (basic_block->nondbg_stmt_count);
264 hstate.add_int (basic_block->edge_count);
266 return hstate.end ();
269 /* References independent hash function. */
271 hashval_t
272 sem_function::get_hash (void)
274 if (!m_hash_set)
276 inchash::hash hstate;
277 hstate.add_int (177454); /* Random number for function type. */
279 hstate.add_int (arg_count);
280 hstate.add_int (cfg_checksum);
281 hstate.add_int (gcode_hash);
283 for (unsigned i = 0; i < bb_sorted.length (); i++)
284 hstate.merge_hash (get_bb_hash (bb_sorted[i]));
286 for (unsigned i = 0; i < bb_sizes.length (); i++)
287 hstate.add_int (bb_sizes[i]);
289 /* Add common features of declaration itself. */
290 if (DECL_FUNCTION_SPECIFIC_TARGET (decl))
291 hstate.add_hwi
292 (cl_target_option_hash
293 (TREE_TARGET_OPTION (DECL_FUNCTION_SPECIFIC_TARGET (decl))));
294 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))
295 hstate.add_hwi
296 (cl_optimization_hash
297 (TREE_OPTIMIZATION (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))));
298 hstate.add_flag (DECL_CXX_CONSTRUCTOR_P (decl));
299 hstate.add_flag (DECL_CXX_DESTRUCTOR_P (decl));
301 set_hash (hstate.end ());
304 return m_hash;
307 /* Compare properties of symbols N1 and N2 that does not affect semantics of
308 symbol itself but affects semantics of its references from USED_BY (which
309 may be NULL if it is unknown). If comparison is false, symbols
310 can still be merged but any symbols referring them can't.
312 If ADDRESS is true, do extra checking needed for IPA_REF_ADDR.
314 TODO: We can also split attributes to those that determine codegen of
315 a function body/variable constructor itself and those that are used when
316 referring to it. */
318 bool
319 sem_item::compare_referenced_symbol_properties (symtab_node *used_by,
320 symtab_node *n1,
321 symtab_node *n2,
322 bool address)
324 if (is_a <cgraph_node *> (n1))
326 /* Inline properties matters: we do now want to merge uses of inline
327 function to uses of normal function because inline hint would be lost.
328 We however can merge inline function to noinline because the alias
329 will keep its DECL_DECLARED_INLINE flag.
331 Also ignore inline flag when optimizing for size or when function
332 is known to not be inlinable.
334 TODO: the optimize_size checks can also be assumed to be true if
335 unit has no !optimize_size functions. */
337 if ((!used_by || address || !is_a <cgraph_node *> (used_by)
338 || !opt_for_fn (used_by->decl, optimize_size))
339 && !opt_for_fn (n1->decl, optimize_size)
340 && n1->get_availability () > AVAIL_INTERPOSABLE
341 && (!DECL_UNINLINABLE (n1->decl) || !DECL_UNINLINABLE (n2->decl)))
343 if (DECL_DISREGARD_INLINE_LIMITS (n1->decl)
344 != DECL_DISREGARD_INLINE_LIMITS (n2->decl))
345 return return_false_with_msg
346 ("DECL_DISREGARD_INLINE_LIMITS are different");
348 if (DECL_DECLARED_INLINE_P (n1->decl)
349 != DECL_DECLARED_INLINE_P (n2->decl))
350 return return_false_with_msg ("inline attributes are different");
353 if (DECL_IS_OPERATOR_NEW_P (n1->decl)
354 != DECL_IS_OPERATOR_NEW_P (n2->decl))
355 return return_false_with_msg ("operator new flags are different");
357 if (DECL_IS_REPLACEABLE_OPERATOR (n1->decl)
358 != DECL_IS_REPLACEABLE_OPERATOR (n2->decl))
359 return return_false_with_msg ("replaceable operator flags are different");
362 /* Merging two definitions with a reference to equivalent vtables, but
363 belonging to a different type may result in ipa-polymorphic-call analysis
364 giving a wrong answer about the dynamic type of instance. */
365 if (is_a <varpool_node *> (n1))
367 if ((DECL_VIRTUAL_P (n1->decl) || DECL_VIRTUAL_P (n2->decl))
368 && (DECL_VIRTUAL_P (n1->decl) != DECL_VIRTUAL_P (n2->decl)
369 || !types_must_be_same_for_odr (DECL_CONTEXT (n1->decl),
370 DECL_CONTEXT (n2->decl)))
371 && (!used_by || !is_a <cgraph_node *> (used_by) || address
372 || opt_for_fn (used_by->decl, flag_devirtualize)))
373 return return_false_with_msg
374 ("references to virtual tables cannot be merged");
376 if (address && DECL_ALIGN (n1->decl) != DECL_ALIGN (n2->decl))
377 return return_false_with_msg ("alignment mismatch");
379 /* For functions we compare attributes in equals_wpa, because we do
380 not know what attributes may cause codegen differences, but for
381 variables just compare attributes for references - the codegen
382 for constructors is affected only by those attributes that we lower
383 to explicit representation (such as DECL_ALIGN or DECL_SECTION). */
384 if (!attribute_list_equal (DECL_ATTRIBUTES (n1->decl),
385 DECL_ATTRIBUTES (n2->decl)))
386 return return_false_with_msg ("different var decl attributes");
387 if (comp_type_attributes (TREE_TYPE (n1->decl),
388 TREE_TYPE (n2->decl)) != 1)
389 return return_false_with_msg ("different var type attributes");
392 /* When matching virtual tables, be sure to also match information
393 relevant for polymorphic call analysis. */
394 if (used_by && is_a <varpool_node *> (used_by)
395 && DECL_VIRTUAL_P (used_by->decl))
397 if (DECL_VIRTUAL_P (n1->decl) != DECL_VIRTUAL_P (n2->decl))
398 return return_false_with_msg ("virtual flag mismatch");
399 if (DECL_VIRTUAL_P (n1->decl) && is_a <cgraph_node *> (n1)
400 && (DECL_FINAL_P (n1->decl) != DECL_FINAL_P (n2->decl)))
401 return return_false_with_msg ("final flag mismatch");
403 return true;
406 /* Hash properties that are compared by compare_referenced_symbol_properties. */
408 void
409 sem_item::hash_referenced_symbol_properties (symtab_node *ref,
410 inchash::hash &hstate,
411 bool address)
413 if (is_a <cgraph_node *> (ref))
415 if ((type != FUNC || address || !opt_for_fn (decl, optimize_size))
416 && !opt_for_fn (ref->decl, optimize_size)
417 && !DECL_UNINLINABLE (ref->decl))
419 hstate.add_flag (DECL_DISREGARD_INLINE_LIMITS (ref->decl));
420 hstate.add_flag (DECL_DECLARED_INLINE_P (ref->decl));
422 hstate.add_flag (DECL_IS_OPERATOR_NEW_P (ref->decl));
424 else if (is_a <varpool_node *> (ref))
426 hstate.add_flag (DECL_VIRTUAL_P (ref->decl));
427 if (address)
428 hstate.add_int (DECL_ALIGN (ref->decl));
433 /* For a given symbol table nodes N1 and N2, we check that FUNCTION_DECLs
434 point to a same function. Comparison can be skipped if IGNORED_NODES
435 contains these nodes. ADDRESS indicate if address is taken. */
437 bool
438 sem_item::compare_symbol_references (
439 hash_map <symtab_node *, sem_item *> &ignored_nodes,
440 symtab_node *n1, symtab_node *n2, bool address)
442 enum availability avail1, avail2;
444 if (n1 == n2)
445 return true;
447 /* Never match variable and function. */
448 if (is_a <varpool_node *> (n1) != is_a <varpool_node *> (n2))
449 return false;
451 if (!compare_referenced_symbol_properties (node, n1, n2, address))
452 return false;
453 if (address && n1->equal_address_to (n2) == 1)
454 return true;
455 if (!address && n1->semantically_equivalent_p (n2))
456 return true;
458 n1 = n1->ultimate_alias_target (&avail1);
459 n2 = n2->ultimate_alias_target (&avail2);
461 if (avail1 > AVAIL_INTERPOSABLE && ignored_nodes.get (n1)
462 && avail2 > AVAIL_INTERPOSABLE && ignored_nodes.get (n2))
463 return true;
465 return return_false_with_msg ("different references");
468 /* If cgraph edges E1 and E2 are indirect calls, verify that
469 ECF flags are the same. */
471 bool sem_function::compare_edge_flags (cgraph_edge *e1, cgraph_edge *e2)
473 if (e1->indirect_info && e2->indirect_info)
475 int e1_flags = e1->indirect_info->ecf_flags;
476 int e2_flags = e2->indirect_info->ecf_flags;
478 if (e1_flags != e2_flags)
479 return return_false_with_msg ("ICF flags are different");
481 else if (e1->indirect_info || e2->indirect_info)
482 return false;
484 return true;
487 /* Return true if parameter I may be used. */
489 bool
490 sem_function::param_used_p (unsigned int i)
492 if (ipa_node_params_sum == NULL)
493 return true;
495 class ipa_node_params *parms_info = IPA_NODE_REF (get_node ());
497 if (!parms_info || vec_safe_length (parms_info->descriptors) <= i)
498 return true;
500 return ipa_is_param_used (IPA_NODE_REF (get_node ()), i);
503 /* Perform additional check needed to match types function parameters that are
504 used. Unlike for normal decls it matters if type is TYPE_RESTRICT and we
505 make an assumption that REFERENCE_TYPE parameters are always non-NULL. */
507 bool
508 sem_function::compatible_parm_types_p (tree parm1, tree parm2)
510 /* Be sure that parameters are TBAA compatible. */
511 if (!func_checker::compatible_types_p (parm1, parm2))
512 return return_false_with_msg ("parameter type is not compatible");
514 if (POINTER_TYPE_P (parm1)
515 && (TYPE_RESTRICT (parm1) != TYPE_RESTRICT (parm2)))
516 return return_false_with_msg ("argument restrict flag mismatch");
518 /* nonnull_arg_p implies non-zero range to REFERENCE types. */
519 if (POINTER_TYPE_P (parm1)
520 && TREE_CODE (parm1) != TREE_CODE (parm2)
521 && opt_for_fn (decl, flag_delete_null_pointer_checks))
522 return return_false_with_msg ("pointer wrt reference mismatch");
524 return true;
527 /* Fast equality function based on knowledge known in WPA. */
529 bool
530 sem_function::equals_wpa (sem_item *item,
531 hash_map <symtab_node *, sem_item *> &ignored_nodes)
533 gcc_assert (item->type == FUNC);
534 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
535 cgraph_node *cnode2 = dyn_cast <cgraph_node *> (item->node);
537 m_compared_func = static_cast<sem_function *> (item);
539 if (cnode->thunk != cnode2->thunk)
540 return return_false_with_msg ("thunk mismatch");
541 if (cnode->former_thunk_p () != cnode2->former_thunk_p ())
542 return return_false_with_msg ("former_thunk_p mismatch");
544 if ((cnode->thunk || cnode->former_thunk_p ())
545 && thunk_info::get (cnode) != thunk_info::get (cnode2))
546 return return_false_with_msg ("thunk_info mismatch");
548 /* Compare special function DECL attributes. */
549 if (DECL_FUNCTION_PERSONALITY (decl)
550 != DECL_FUNCTION_PERSONALITY (item->decl))
551 return return_false_with_msg ("function personalities are different");
553 if (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl)
554 != DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (item->decl))
555 return return_false_with_msg ("instrument function entry exit "
556 "attributes are different");
558 if (DECL_NO_LIMIT_STACK (decl) != DECL_NO_LIMIT_STACK (item->decl))
559 return return_false_with_msg ("no stack limit attributes are different");
561 if (DECL_CXX_CONSTRUCTOR_P (decl) != DECL_CXX_CONSTRUCTOR_P (item->decl))
562 return return_false_with_msg ("DECL_CXX_CONSTRUCTOR mismatch");
564 if (DECL_CXX_DESTRUCTOR_P (decl) != DECL_CXX_DESTRUCTOR_P (item->decl))
565 return return_false_with_msg ("DECL_CXX_DESTRUCTOR mismatch");
567 /* TODO: pure/const flags mostly matters only for references, except for
568 the fact that codegen takes LOOPING flag as a hint that loops are
569 finite. We may arrange the code to always pick leader that has least
570 specified flags and then this can go into comparing symbol properties. */
571 if (flags_from_decl_or_type (decl) != flags_from_decl_or_type (item->decl))
572 return return_false_with_msg ("decl_or_type flags are different");
574 /* Do not match polymorphic constructors of different types. They calls
575 type memory location for ipa-polymorphic-call and we do not want
576 it to get confused by wrong type. */
577 if (DECL_CXX_CONSTRUCTOR_P (decl)
578 && opt_for_fn (decl, flag_devirtualize)
579 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
581 if (TREE_CODE (TREE_TYPE (item->decl)) != METHOD_TYPE)
582 return return_false_with_msg ("DECL_CXX_CONSTRUCTOR type mismatch");
583 else if (!func_checker::compatible_polymorphic_types_p
584 (TYPE_METHOD_BASETYPE (TREE_TYPE (decl)),
585 TYPE_METHOD_BASETYPE (TREE_TYPE (item->decl)), false))
586 return return_false_with_msg ("ctor polymorphic type mismatch");
589 /* Checking function TARGET and OPTIMIZATION flags. */
590 cl_target_option *tar1 = target_opts_for_fn (decl);
591 cl_target_option *tar2 = target_opts_for_fn (item->decl);
593 if (tar1 != tar2 && !cl_target_option_eq (tar1, tar2))
595 if (dump_file && (dump_flags & TDF_DETAILS))
597 fprintf (dump_file, "target flags difference");
598 cl_target_option_print_diff (dump_file, 2, tar1, tar2);
601 return return_false_with_msg ("Target flags are different");
604 cl_optimization *opt1 = opts_for_fn (decl);
605 cl_optimization *opt2 = opts_for_fn (item->decl);
607 if (opt1 != opt2 && !cl_optimization_option_eq (opt1, opt2))
609 if (dump_file && (dump_flags & TDF_DETAILS))
611 fprintf (dump_file, "optimization flags difference");
612 cl_optimization_print_diff (dump_file, 2, opt1, opt2);
615 return return_false_with_msg ("optimization flags are different");
618 /* Result type checking. */
619 if (!func_checker::compatible_types_p
620 (TREE_TYPE (TREE_TYPE (decl)),
621 TREE_TYPE (TREE_TYPE (m_compared_func->decl))))
622 return return_false_with_msg ("result types are different");
624 /* Checking types of arguments. */
625 tree list1 = TYPE_ARG_TYPES (TREE_TYPE (decl)),
626 list2 = TYPE_ARG_TYPES (TREE_TYPE (m_compared_func->decl));
627 for (unsigned i = 0; list1 && list2;
628 list1 = TREE_CHAIN (list1), list2 = TREE_CHAIN (list2), i++)
630 tree parm1 = TREE_VALUE (list1);
631 tree parm2 = TREE_VALUE (list2);
633 /* This guard is here for function pointer with attributes (pr59927.c). */
634 if (!parm1 || !parm2)
635 return return_false_with_msg ("NULL argument type");
637 /* Verify that types are compatible to ensure that both functions
638 have same calling conventions. */
639 if (!types_compatible_p (parm1, parm2))
640 return return_false_with_msg ("parameter types are not compatible");
642 if (!param_used_p (i))
643 continue;
645 /* Perform additional checks for used parameters. */
646 if (!compatible_parm_types_p (parm1, parm2))
647 return false;
650 if (list1 || list2)
651 return return_false_with_msg ("Mismatched number of parameters");
653 if (node->num_references () != item->node->num_references ())
654 return return_false_with_msg ("different number of references");
656 /* Checking function attributes.
657 This is quadratic in number of attributes */
658 if (comp_type_attributes (TREE_TYPE (decl),
659 TREE_TYPE (item->decl)) != 1)
660 return return_false_with_msg ("different type attributes");
661 if (!attribute_list_equal (DECL_ATTRIBUTES (decl),
662 DECL_ATTRIBUTES (item->decl)))
663 return return_false_with_msg ("different decl attributes");
665 /* The type of THIS pointer type memory location for
666 ipa-polymorphic-call-analysis. */
667 if (opt_for_fn (decl, flag_devirtualize)
668 && (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
669 || TREE_CODE (TREE_TYPE (item->decl)) == METHOD_TYPE)
670 && param_used_p (0)
671 && compare_polymorphic_p ())
673 if (TREE_CODE (TREE_TYPE (decl)) != TREE_CODE (TREE_TYPE (item->decl)))
674 return return_false_with_msg ("METHOD_TYPE and FUNCTION_TYPE mismatch");
675 if (!func_checker::compatible_polymorphic_types_p
676 (TYPE_METHOD_BASETYPE (TREE_TYPE (decl)),
677 TYPE_METHOD_BASETYPE (TREE_TYPE (item->decl)), false))
678 return return_false_with_msg ("THIS pointer ODR type mismatch");
681 ipa_ref *ref = NULL, *ref2 = NULL;
682 for (unsigned i = 0; node->iterate_reference (i, ref); i++)
684 item->node->iterate_reference (i, ref2);
686 if (ref->use != ref2->use)
687 return return_false_with_msg ("reference use mismatch");
689 if (!compare_symbol_references (ignored_nodes, ref->referred,
690 ref2->referred,
691 ref->address_matters_p ()))
692 return false;
695 cgraph_edge *e1 = dyn_cast <cgraph_node *> (node)->callees;
696 cgraph_edge *e2 = dyn_cast <cgraph_node *> (item->node)->callees;
698 while (e1 && e2)
700 if (!compare_symbol_references (ignored_nodes, e1->callee,
701 e2->callee, false))
702 return false;
703 if (!compare_edge_flags (e1, e2))
704 return false;
706 e1 = e1->next_callee;
707 e2 = e2->next_callee;
710 if (e1 || e2)
711 return return_false_with_msg ("different number of calls");
713 e1 = dyn_cast <cgraph_node *> (node)->indirect_calls;
714 e2 = dyn_cast <cgraph_node *> (item->node)->indirect_calls;
716 while (e1 && e2)
718 if (!compare_edge_flags (e1, e2))
719 return false;
721 e1 = e1->next_callee;
722 e2 = e2->next_callee;
725 if (e1 || e2)
726 return return_false_with_msg ("different number of indirect calls");
728 return true;
731 /* Update hash by address sensitive references. We iterate over all
732 sensitive references (address_matters_p) and we hash ultimate alias
733 target of these nodes, which can improve a semantic item hash.
735 Also hash in referenced symbols properties. This can be done at any time
736 (as the properties should not change), but it is convenient to do it here
737 while we walk the references anyway. */
739 void
740 sem_item::update_hash_by_addr_refs (hash_map <symtab_node *,
741 sem_item *> &m_symtab_node_map)
743 ipa_ref* ref;
744 inchash::hash hstate (get_hash ());
746 for (unsigned i = 0; node->iterate_reference (i, ref); i++)
748 hstate.add_int (ref->use);
749 hash_referenced_symbol_properties (ref->referred, hstate,
750 ref->use == IPA_REF_ADDR);
751 if (ref->address_matters_p () || !m_symtab_node_map.get (ref->referred))
752 hstate.add_int (ref->referred->ultimate_alias_target ()->order);
755 if (is_a <cgraph_node *> (node))
757 for (cgraph_edge *e = dyn_cast <cgraph_node *> (node)->callers; e;
758 e = e->next_caller)
760 sem_item **result = m_symtab_node_map.get (e->callee);
761 hash_referenced_symbol_properties (e->callee, hstate, false);
762 if (!result)
763 hstate.add_int (e->callee->ultimate_alias_target ()->order);
767 set_hash (hstate.end ());
770 /* Update hash by computed local hash values taken from different
771 semantic items.
772 TODO: stronger SCC based hashing would be desirable here. */
774 void
775 sem_item::update_hash_by_local_refs (hash_map <symtab_node *,
776 sem_item *> &m_symtab_node_map)
778 ipa_ref* ref;
779 inchash::hash state (get_hash ());
781 for (unsigned j = 0; node->iterate_reference (j, ref); j++)
783 sem_item **result = m_symtab_node_map.get (ref->referring);
784 if (result)
785 state.merge_hash ((*result)->get_hash ());
788 if (type == FUNC)
790 for (cgraph_edge *e = dyn_cast <cgraph_node *> (node)->callees; e;
791 e = e->next_callee)
793 sem_item **result = m_symtab_node_map.get (e->caller);
794 if (result)
795 state.merge_hash ((*result)->get_hash ());
799 global_hash = state.end ();
802 /* Returns true if the item equals to ITEM given as argument. */
804 bool
805 sem_function::equals (sem_item *item,
806 hash_map <symtab_node *, sem_item *> &)
808 gcc_assert (item->type == FUNC);
809 bool eq = equals_private (item);
811 if (m_checker != NULL)
813 delete m_checker;
814 m_checker = NULL;
817 if (dump_file && (dump_flags & TDF_DETAILS))
818 fprintf (dump_file,
819 "Equals called for: %s:%s with result: %s\n\n",
820 node->dump_name (),
821 item->node->dump_name (),
822 eq ? "true" : "false");
824 return eq;
827 /* Processes function equality comparison. */
829 bool
830 sem_function::equals_private (sem_item *item)
832 if (item->type != FUNC)
833 return false;
835 basic_block bb1, bb2;
836 edge e1, e2;
837 edge_iterator ei1, ei2;
838 bool result = true;
839 tree arg1, arg2;
841 m_compared_func = static_cast<sem_function *> (item);
843 gcc_assert (decl != item->decl);
845 if (bb_sorted.length () != m_compared_func->bb_sorted.length ()
846 || edge_count != m_compared_func->edge_count
847 || cfg_checksum != m_compared_func->cfg_checksum)
848 return return_false ();
850 m_checker = new func_checker (decl, m_compared_func->decl,
851 false,
852 opt_for_fn (m_compared_func->decl,
853 flag_strict_aliasing),
854 &refs_set,
855 &m_compared_func->refs_set);
856 arg1 = DECL_ARGUMENTS (decl);
857 arg2 = DECL_ARGUMENTS (m_compared_func->decl);
858 for (unsigned i = 0;
859 arg1 && arg2; arg1 = DECL_CHAIN (arg1), arg2 = DECL_CHAIN (arg2), i++)
861 if (!types_compatible_p (TREE_TYPE (arg1), TREE_TYPE (arg2)))
862 return return_false_with_msg ("argument types are not compatible");
863 if (!param_used_p (i))
864 continue;
865 /* Perform additional checks for used parameters. */
866 if (!compatible_parm_types_p (TREE_TYPE (arg1), TREE_TYPE (arg2)))
867 return false;
868 if (!m_checker->compare_decl (arg1, arg2))
869 return return_false ();
871 if (arg1 || arg2)
872 return return_false_with_msg ("Mismatched number of arguments");
874 if (!dyn_cast <cgraph_node *> (node)->has_gimple_body_p ())
875 return true;
877 /* Fill-up label dictionary. */
878 for (unsigned i = 0; i < bb_sorted.length (); ++i)
880 m_checker->parse_labels (bb_sorted[i]);
881 m_checker->parse_labels (m_compared_func->bb_sorted[i]);
884 /* Checking all basic blocks. */
885 for (unsigned i = 0; i < bb_sorted.length (); ++i)
886 if(!m_checker->compare_bb (bb_sorted[i], m_compared_func->bb_sorted[i]))
887 return return_false ();
889 auto_vec <int> bb_dict;
891 /* Basic block edges check. */
892 for (unsigned i = 0; i < bb_sorted.length (); ++i)
894 bb1 = bb_sorted[i]->bb;
895 bb2 = m_compared_func->bb_sorted[i]->bb;
897 ei2 = ei_start (bb2->preds);
899 for (ei1 = ei_start (bb1->preds); ei_cond (ei1, &e1); ei_next (&ei1))
901 ei_cond (ei2, &e2);
903 if (e1->flags != e2->flags)
904 return return_false_with_msg ("flags comparison returns false");
906 if (!bb_dict_test (&bb_dict, e1->src->index, e2->src->index))
907 return return_false_with_msg ("edge comparison returns false");
909 if (!bb_dict_test (&bb_dict, e1->dest->index, e2->dest->index))
910 return return_false_with_msg ("BB comparison returns false");
912 if (!m_checker->compare_edge (e1, e2))
913 return return_false_with_msg ("edge comparison returns false");
915 ei_next (&ei2);
919 /* Basic block PHI nodes comparison. */
920 for (unsigned i = 0; i < bb_sorted.length (); i++)
921 if (!compare_phi_node (bb_sorted[i]->bb, m_compared_func->bb_sorted[i]->bb))
922 return return_false_with_msg ("PHI node comparison returns false");
924 return result;
927 /* Set LOCAL_P of NODE to true if DATA is non-NULL.
928 Helper for call_for_symbol_thunks_and_aliases. */
930 static bool
931 set_local (cgraph_node *node, void *data)
933 node->local = data != NULL;
934 return false;
937 /* TREE_ADDRESSABLE of NODE to true.
938 Helper for call_for_symbol_thunks_and_aliases. */
940 static bool
941 set_addressable (varpool_node *node, void *)
943 TREE_ADDRESSABLE (node->decl) = 1;
944 return false;
947 /* Clear DECL_RTL of NODE.
948 Helper for call_for_symbol_thunks_and_aliases. */
950 static bool
951 clear_decl_rtl (symtab_node *node, void *)
953 SET_DECL_RTL (node->decl, NULL);
954 return false;
957 /* Redirect all callers of N and its aliases to TO. Remove aliases if
958 possible. Return number of redirections made. */
960 static int
961 redirect_all_callers (cgraph_node *n, cgraph_node *to)
963 int nredirected = 0;
964 ipa_ref *ref;
965 cgraph_edge *e = n->callers;
967 while (e)
969 /* Redirecting thunks to interposable symbols or symbols in other sections
970 may not be supported by target output code. Play safe for now and
971 punt on redirection. */
972 if (!e->caller->thunk)
974 struct cgraph_edge *nexte = e->next_caller;
975 e->redirect_callee (to);
976 e = nexte;
977 nredirected++;
979 else
980 e = e->next_callee;
982 for (unsigned i = 0; n->iterate_direct_aliases (i, ref);)
984 bool removed = false;
985 cgraph_node *n_alias = dyn_cast <cgraph_node *> (ref->referring);
987 if ((DECL_COMDAT_GROUP (n->decl)
988 && (DECL_COMDAT_GROUP (n->decl)
989 == DECL_COMDAT_GROUP (n_alias->decl)))
990 || (n_alias->get_availability () > AVAIL_INTERPOSABLE
991 && n->get_availability () > AVAIL_INTERPOSABLE))
993 nredirected += redirect_all_callers (n_alias, to);
994 if (n_alias->can_remove_if_no_direct_calls_p ()
995 && !n_alias->call_for_symbol_and_aliases (cgraph_node::has_thunk_p,
996 NULL, true)
997 && !n_alias->has_aliases_p ())
998 n_alias->remove ();
1000 if (!removed)
1001 i++;
1003 return nredirected;
1006 /* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can
1007 be applied. */
1009 bool
1010 sem_function::merge (sem_item *alias_item)
1012 gcc_assert (alias_item->type == FUNC);
1014 sem_function *alias_func = static_cast<sem_function *> (alias_item);
1016 cgraph_node *original = get_node ();
1017 cgraph_node *local_original = NULL;
1018 cgraph_node *alias = alias_func->get_node ();
1020 bool create_wrapper = false;
1021 bool create_alias = false;
1022 bool redirect_callers = false;
1023 bool remove = false;
1025 bool original_discardable = false;
1026 bool original_discarded = false;
1028 bool original_address_matters = original->address_matters_p ();
1029 bool alias_address_matters = alias->address_matters_p ();
1031 AUTO_DUMP_SCOPE ("merge",
1032 dump_user_location_t::from_function_decl (decl));
1034 if (DECL_EXTERNAL (alias->decl))
1036 if (dump_enabled_p ())
1037 dump_printf (MSG_MISSED_OPTIMIZATION,
1038 "Not unifying; alias is external.\n");
1039 return false;
1042 if (DECL_NO_INLINE_WARNING_P (original->decl)
1043 != DECL_NO_INLINE_WARNING_P (alias->decl))
1045 if (dump_enabled_p ())
1046 dump_printf (MSG_MISSED_OPTIMIZATION,
1047 "Not unifying; DECL_NO_INLINE_WARNING mismatch.\n");
1048 return false;
1051 /* Do not attempt to mix functions from different user sections;
1052 we do not know what user intends with those. */
1053 if (((DECL_SECTION_NAME (original->decl) && !original->implicit_section)
1054 || (DECL_SECTION_NAME (alias->decl) && !alias->implicit_section))
1055 && DECL_SECTION_NAME (original->decl) != DECL_SECTION_NAME (alias->decl))
1057 if (dump_enabled_p ())
1058 dump_printf (MSG_MISSED_OPTIMIZATION,
1059 "Not unifying; "
1060 "original and alias are in different sections.\n");
1061 return false;
1064 if (!original->in_same_comdat_group_p (alias)
1065 || original->comdat_local_p ())
1067 if (dump_enabled_p ())
1068 dump_printf (MSG_MISSED_OPTIMIZATION,
1069 "Not unifying; alias nor wrapper cannot be created; "
1070 "across comdat group boundary\n");
1071 return false;
1074 /* See if original is in a section that can be discarded if the main
1075 symbol is not used. */
1077 if (original->can_be_discarded_p ())
1078 original_discardable = true;
1079 /* Also consider case where we have resolution info and we know that
1080 original's definition is not going to be used. In this case we cannot
1081 create alias to original. */
1082 if (node->resolution != LDPR_UNKNOWN
1083 && !decl_binds_to_current_def_p (node->decl))
1084 original_discardable = original_discarded = true;
1086 /* Creating a symtab alias is the optimal way to merge.
1087 It however cannot be used in the following cases:
1089 1) if ORIGINAL and ALIAS may be possibly compared for address equality.
1090 2) if ORIGINAL is in a section that may be discarded by linker or if
1091 it is an external functions where we cannot create an alias
1092 (ORIGINAL_DISCARDABLE)
1093 3) if target do not support symbol aliases.
1094 4) original and alias lie in different comdat groups.
1096 If we cannot produce alias, we will turn ALIAS into WRAPPER of ORIGINAL
1097 and/or redirect all callers from ALIAS to ORIGINAL. */
1098 if ((original_address_matters && alias_address_matters)
1099 || (original_discardable
1100 && (!DECL_COMDAT_GROUP (alias->decl)
1101 || (DECL_COMDAT_GROUP (alias->decl)
1102 != DECL_COMDAT_GROUP (original->decl))))
1103 || original_discarded
1104 || !sem_item::target_supports_symbol_aliases_p ()
1105 || DECL_COMDAT_GROUP (alias->decl) != DECL_COMDAT_GROUP (original->decl))
1107 /* First see if we can produce wrapper. */
1109 /* Symbol properties that matter for references must be preserved.
1110 TODO: We can produce wrapper, but we need to produce alias of ORIGINAL
1111 with proper properties. */
1112 if (!sem_item::compare_referenced_symbol_properties (NULL, original, alias,
1113 alias->address_taken))
1115 if (dump_enabled_p ())
1116 dump_printf (MSG_MISSED_OPTIMIZATION,
1117 "Wrapper cannot be created because referenced symbol "
1118 "properties mismatch\n");
1120 /* Do not turn function in one comdat group into wrapper to another
1121 comdat group. Other compiler producing the body of the
1122 another comdat group may make opposite decision and with unfortunate
1123 linker choices this may close a loop. */
1124 else if (DECL_COMDAT_GROUP (original->decl)
1125 && DECL_COMDAT_GROUP (alias->decl)
1126 && (DECL_COMDAT_GROUP (alias->decl)
1127 != DECL_COMDAT_GROUP (original->decl)))
1129 if (dump_enabled_p ())
1130 dump_printf (MSG_MISSED_OPTIMIZATION,
1131 "Wrapper cannot be created because of COMDAT\n");
1133 else if (DECL_STATIC_CHAIN (alias->decl)
1134 || DECL_STATIC_CHAIN (original->decl))
1136 if (dump_enabled_p ())
1137 dump_printf (MSG_MISSED_OPTIMIZATION,
1138 "Cannot create wrapper of nested function.\n");
1140 /* TODO: We can also deal with variadic functions never calling
1141 VA_START. */
1142 else if (stdarg_p (TREE_TYPE (alias->decl)))
1144 if (dump_enabled_p ())
1145 dump_printf (MSG_MISSED_OPTIMIZATION,
1146 "cannot create wrapper of stdarg function.\n");
1148 else if (ipa_fn_summaries
1149 && ipa_size_summaries->get (alias) != NULL
1150 && ipa_size_summaries->get (alias)->self_size <= 2)
1152 if (dump_enabled_p ())
1153 dump_printf (MSG_MISSED_OPTIMIZATION, "Wrapper creation is not "
1154 "profitable (function is too small).\n");
1156 /* If user paid attention to mark function noinline, assume it is
1157 somewhat special and do not try to turn it into a wrapper that
1158 cannot be undone by inliner. */
1159 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (alias->decl)))
1161 if (dump_enabled_p ())
1162 dump_printf (MSG_MISSED_OPTIMIZATION,
1163 "Wrappers are not created for noinline.\n");
1165 else
1166 create_wrapper = true;
1168 /* We can redirect local calls in the case both alias and original
1169 are not interposable. */
1170 redirect_callers
1171 = alias->get_availability () > AVAIL_INTERPOSABLE
1172 && original->get_availability () > AVAIL_INTERPOSABLE;
1173 /* TODO: We can redirect, but we need to produce alias of ORIGINAL
1174 with proper properties. */
1175 if (!sem_item::compare_referenced_symbol_properties (NULL, original, alias,
1176 alias->address_taken))
1177 redirect_callers = false;
1179 if (!redirect_callers && !create_wrapper)
1181 if (dump_enabled_p ())
1182 dump_printf (MSG_MISSED_OPTIMIZATION,
1183 "Not unifying; cannot redirect callers nor "
1184 "produce wrapper\n");
1185 return false;
1188 /* Work out the symbol the wrapper should call.
1189 If ORIGINAL is interposable, we need to call a local alias.
1190 Also produce local alias (if possible) as an optimization.
1192 Local aliases cannot be created inside comdat groups because that
1193 prevents inlining. */
1194 if (!original_discardable && !original->get_comdat_group ())
1196 local_original
1197 = dyn_cast <cgraph_node *> (original->noninterposable_alias ());
1198 if (!local_original
1199 && original->get_availability () > AVAIL_INTERPOSABLE)
1200 local_original = original;
1202 /* If we cannot use local alias, fallback to the original
1203 when possible. */
1204 else if (original->get_availability () > AVAIL_INTERPOSABLE)
1205 local_original = original;
1207 /* If original is COMDAT local, we cannot really redirect calls outside
1208 of its comdat group to it. */
1209 if (original->comdat_local_p ())
1210 redirect_callers = false;
1211 if (!local_original)
1213 if (dump_enabled_p ())
1214 dump_printf (MSG_MISSED_OPTIMIZATION,
1215 "Not unifying; cannot produce local alias.\n");
1216 return false;
1219 if (!redirect_callers && !create_wrapper)
1221 if (dump_enabled_p ())
1222 dump_printf (MSG_MISSED_OPTIMIZATION,
1223 "Not unifying; "
1224 "cannot redirect callers nor produce a wrapper\n");
1225 return false;
1227 if (!create_wrapper
1228 && !alias->call_for_symbol_and_aliases (cgraph_node::has_thunk_p,
1229 NULL, true)
1230 && !alias->can_remove_if_no_direct_calls_p ())
1232 if (dump_enabled_p ())
1233 dump_printf (MSG_MISSED_OPTIMIZATION,
1234 "Not unifying; cannot make wrapper and "
1235 "function has other uses than direct calls\n");
1236 return false;
1239 else
1240 create_alias = true;
1242 if (redirect_callers)
1244 int nredirected = redirect_all_callers (alias, local_original);
1246 if (nredirected)
1248 alias->icf_merged = true;
1249 local_original->icf_merged = true;
1251 if (dump_enabled_p ())
1252 dump_printf (MSG_NOTE,
1253 "%i local calls have been "
1254 "redirected.\n", nredirected);
1257 /* If all callers was redirected, do not produce wrapper. */
1258 if (alias->can_remove_if_no_direct_calls_p ()
1259 && !DECL_VIRTUAL_P (alias->decl)
1260 && !alias->has_aliases_p ())
1262 create_wrapper = false;
1263 remove = true;
1265 gcc_assert (!create_alias);
1267 else if (create_alias)
1269 alias->icf_merged = true;
1271 /* Remove the function's body. */
1272 ipa_merge_profiles (original, alias);
1273 symtab->call_cgraph_removal_hooks (alias);
1274 alias->release_body (true);
1275 alias->reset ();
1276 /* Notice global symbol possibly produced RTL. */
1277 ((symtab_node *)alias)->call_for_symbol_and_aliases (clear_decl_rtl,
1278 NULL, true);
1280 /* Create the alias. */
1281 cgraph_node::create_alias (alias_func->decl, decl);
1282 alias->resolve_alias (original);
1284 original->call_for_symbol_thunks_and_aliases
1285 (set_local, (void *)(size_t) original->local_p (), true);
1287 if (dump_enabled_p ())
1288 dump_printf (MSG_OPTIMIZED_LOCATIONS,
1289 "Unified; Function alias has been created.\n");
1291 if (create_wrapper)
1293 gcc_assert (!create_alias);
1294 alias->icf_merged = true;
1295 symtab->call_cgraph_removal_hooks (alias);
1296 local_original->icf_merged = true;
1298 /* FIXME update local_original counts. */
1299 ipa_merge_profiles (original, alias, true);
1300 alias->create_wrapper (local_original);
1301 symtab->call_cgraph_insertion_hooks (alias);
1303 if (dump_enabled_p ())
1304 dump_printf (MSG_OPTIMIZED_LOCATIONS,
1305 "Unified; Wrapper has been created.\n");
1308 /* It's possible that redirection can hit thunks that block
1309 redirection opportunities. */
1310 gcc_assert (alias->icf_merged || remove || redirect_callers);
1311 original->icf_merged = true;
1313 /* We use merged flag to track cases where COMDAT function is known to be
1314 compatible its callers. If we merged in non-COMDAT, we need to give up
1315 on this optimization. */
1316 if (original->merged_comdat && !alias->merged_comdat)
1318 if (dump_enabled_p ())
1319 dump_printf (MSG_NOTE, "Dropping merged_comdat flag.\n");
1320 if (local_original)
1321 local_original->merged_comdat = false;
1322 original->merged_comdat = false;
1325 if (remove)
1327 ipa_merge_profiles (original, alias);
1328 alias->release_body ();
1329 alias->reset ();
1330 alias->body_removed = true;
1331 alias->icf_merged = true;
1332 if (dump_enabled_p ())
1333 dump_printf (MSG_OPTIMIZED_LOCATIONS,
1334 "Unified; Function body was removed.\n");
1337 return true;
1340 /* Semantic item initialization function. */
1342 void
1343 sem_function::init (ipa_icf_gimple::func_checker *checker)
1345 m_checker = checker;
1346 if (in_lto_p)
1347 get_node ()->get_untransformed_body ();
1349 tree fndecl = node->decl;
1350 function *func = DECL_STRUCT_FUNCTION (fndecl);
1352 gcc_assert (func);
1353 gcc_assert (SSANAMES (func));
1355 ssa_names_size = SSANAMES (func)->length ();
1356 node = node;
1358 decl = fndecl;
1359 region_tree = func->eh->region_tree;
1361 /* iterating all function arguments. */
1362 arg_count = count_formal_params (fndecl);
1364 edge_count = n_edges_for_fn (func);
1365 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1366 if (!cnode->thunk)
1368 cfg_checksum = coverage_compute_cfg_checksum (func);
1370 inchash::hash hstate;
1372 basic_block bb;
1373 FOR_EACH_BB_FN (bb, func)
1375 unsigned nondbg_stmt_count = 0;
1377 edge e;
1378 for (edge_iterator ei = ei_start (bb->preds); ei_cond (ei, &e);
1379 ei_next (&ei))
1380 cfg_checksum = iterative_hash_host_wide_int (e->flags,
1381 cfg_checksum);
1383 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1384 gsi_next (&gsi))
1386 gimple *stmt = gsi_stmt (gsi);
1388 if (gimple_code (stmt) != GIMPLE_DEBUG
1389 && gimple_code (stmt) != GIMPLE_PREDICT)
1391 hash_stmt (stmt, hstate);
1392 nondbg_stmt_count++;
1396 hstate.commit_flag ();
1397 gcode_hash = hstate.end ();
1398 bb_sizes.safe_push (nondbg_stmt_count);
1400 /* Inserting basic block to hash table. */
1401 sem_bb *semantic_bb = new sem_bb (bb, nondbg_stmt_count,
1402 EDGE_COUNT (bb->preds)
1403 + EDGE_COUNT (bb->succs));
1405 bb_sorted.safe_push (semantic_bb);
1408 else
1410 cfg_checksum = 0;
1411 gcode_hash = thunk_info::get (cnode)->hash ();
1414 m_checker = NULL;
1417 /* Improve accumulated hash for HSTATE based on a gimple statement STMT. */
1419 void
1420 sem_function::hash_stmt (gimple *stmt, inchash::hash &hstate)
1422 enum gimple_code code = gimple_code (stmt);
1424 hstate.add_int (code);
1426 switch (code)
1428 case GIMPLE_SWITCH:
1429 m_checker->hash_operand (gimple_switch_index (as_a <gswitch *> (stmt)),
1430 hstate, 0, func_checker::OP_NORMAL);
1431 break;
1432 case GIMPLE_ASSIGN:
1433 hstate.add_int (gimple_assign_rhs_code (stmt));
1434 /* fall through */
1435 case GIMPLE_CALL:
1436 case GIMPLE_ASM:
1437 case GIMPLE_COND:
1438 case GIMPLE_GOTO:
1439 case GIMPLE_RETURN:
1441 func_checker::operand_access_type_map map (5);
1442 func_checker::classify_operands (stmt, &map);
1444 /* All these statements are equivalent if their operands are. */
1445 for (unsigned i = 0; i < gimple_num_ops (stmt); ++i)
1447 func_checker::operand_access_type
1448 access_type = func_checker::get_operand_access_type
1449 (&map, gimple_op (stmt, i));
1450 m_checker->hash_operand (gimple_op (stmt, i), hstate, 0,
1451 access_type);
1452 /* For memory accesses when hasing for LTO stremaing record
1453 base and ref alias ptr types so we can compare them at WPA
1454 time without having to read actual function body. */
1455 if (access_type == func_checker::OP_MEMORY
1456 && lto_streaming_expected_p ()
1457 && flag_strict_aliasing)
1459 ao_ref ref;
1461 ao_ref_init (&ref, gimple_op (stmt, i));
1462 tree t = ao_ref_alias_ptr_type (&ref);
1463 if (!variably_modified_type_p (t, NULL_TREE))
1464 memory_access_types.safe_push (t);
1465 t = ao_ref_base_alias_ptr_type (&ref);
1466 if (!variably_modified_type_p (t, NULL_TREE))
1467 memory_access_types.safe_push (t);
1470 /* Consider nocf_check attribute in hash as it affects code
1471 generation. */
1472 if (code == GIMPLE_CALL
1473 && flag_cf_protection & CF_BRANCH)
1474 hstate.add_flag (gimple_call_nocf_check_p (as_a <gcall *> (stmt)));
1476 break;
1477 default:
1478 break;
1483 /* Return true if polymorphic comparison must be processed. */
1485 bool
1486 sem_function::compare_polymorphic_p (void)
1488 struct cgraph_edge *e;
1490 if (!opt_for_fn (get_node ()->decl, flag_devirtualize))
1491 return false;
1492 if (get_node ()->indirect_calls != NULL)
1493 return true;
1494 /* TODO: We can do simple propagation determining what calls may lead to
1495 a polymorphic call. */
1496 for (e = get_node ()->callees; e; e = e->next_callee)
1497 if (e->callee->definition
1498 && opt_for_fn (e->callee->decl, flag_devirtualize))
1499 return true;
1500 return false;
1503 /* For a given call graph NODE, the function constructs new
1504 semantic function item. */
1506 sem_function *
1507 sem_function::parse (cgraph_node *node, bitmap_obstack *stack,
1508 func_checker *checker)
1510 tree fndecl = node->decl;
1511 function *func = DECL_STRUCT_FUNCTION (fndecl);
1513 if (!func || (!node->has_gimple_body_p () && !node->thunk))
1514 return NULL;
1516 if (lookup_attribute_by_prefix ("omp ", DECL_ATTRIBUTES (node->decl)) != NULL)
1517 return NULL;
1519 if (lookup_attribute_by_prefix ("oacc ",
1520 DECL_ATTRIBUTES (node->decl)) != NULL)
1521 return NULL;
1523 /* PR ipa/70306. */
1524 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1525 || DECL_STATIC_DESTRUCTOR (node->decl))
1526 return NULL;
1528 sem_function *f = new sem_function (node, stack);
1529 f->init (checker);
1531 return f;
1534 /* For given basic blocks BB1 and BB2 (from functions FUNC1 and FUNC),
1535 return true if phi nodes are semantically equivalent in these blocks . */
1537 bool
1538 sem_function::compare_phi_node (basic_block bb1, basic_block bb2)
1540 gphi_iterator si1, si2;
1541 gphi *phi1, *phi2;
1542 unsigned size1, size2, i;
1543 tree t1, t2;
1544 edge e1, e2;
1546 gcc_assert (bb1 != NULL);
1547 gcc_assert (bb2 != NULL);
1549 si2 = gsi_start_nonvirtual_phis (bb2);
1550 for (si1 = gsi_start_nonvirtual_phis (bb1); !gsi_end_p (si1);
1551 gsi_next_nonvirtual_phi (&si1))
1553 if (gsi_end_p (si1) && gsi_end_p (si2))
1554 break;
1556 if (gsi_end_p (si1) || gsi_end_p (si2))
1557 return return_false();
1559 phi1 = si1.phi ();
1560 phi2 = si2.phi ();
1562 tree phi_result1 = gimple_phi_result (phi1);
1563 tree phi_result2 = gimple_phi_result (phi2);
1565 if (!m_checker->compare_operand (phi_result1, phi_result2,
1566 func_checker::OP_NORMAL))
1567 return return_false_with_msg ("PHI results are different");
1569 size1 = gimple_phi_num_args (phi1);
1570 size2 = gimple_phi_num_args (phi2);
1572 if (size1 != size2)
1573 return return_false ();
1575 for (i = 0; i < size1; ++i)
1577 t1 = gimple_phi_arg (phi1, i)->def;
1578 t2 = gimple_phi_arg (phi2, i)->def;
1580 if (!m_checker->compare_operand (t1, t2, func_checker::OP_NORMAL))
1581 return return_false ();
1583 e1 = gimple_phi_arg_edge (phi1, i);
1584 e2 = gimple_phi_arg_edge (phi2, i);
1586 if (!m_checker->compare_edge (e1, e2))
1587 return return_false ();
1590 gsi_next_nonvirtual_phi (&si2);
1593 return true;
1596 /* Basic blocks dictionary BB_DICT returns true if SOURCE index BB
1597 corresponds to TARGET. */
1599 bool
1600 sem_function::bb_dict_test (vec<int> *bb_dict, int source, int target)
1602 source++;
1603 target++;
1605 if (bb_dict->length () <= (unsigned)source)
1606 bb_dict->safe_grow_cleared (source + 1, true);
1608 if ((*bb_dict)[source] == 0)
1610 (*bb_dict)[source] = target;
1611 return true;
1613 else
1614 return (*bb_dict)[source] == target;
1617 sem_variable::sem_variable (bitmap_obstack *stack): sem_item (VAR, stack)
1621 sem_variable::sem_variable (varpool_node *node, bitmap_obstack *stack)
1622 : sem_item (VAR, node, stack)
1624 gcc_checking_assert (node);
1625 gcc_checking_assert (get_node ());
1628 /* Fast equality function based on knowledge known in WPA. */
1630 bool
1631 sem_variable::equals_wpa (sem_item *item,
1632 hash_map <symtab_node *, sem_item *> &ignored_nodes)
1634 gcc_assert (item->type == VAR);
1636 if (node->num_references () != item->node->num_references ())
1637 return return_false_with_msg ("different number of references");
1639 if (DECL_TLS_MODEL (decl) || DECL_TLS_MODEL (item->decl))
1640 return return_false_with_msg ("TLS model");
1642 /* DECL_ALIGN is safe to merge, because we will always chose the largest
1643 alignment out of all aliases. */
1645 if (DECL_VIRTUAL_P (decl) != DECL_VIRTUAL_P (item->decl))
1646 return return_false_with_msg ("Virtual flag mismatch");
1648 if (DECL_SIZE (decl) != DECL_SIZE (item->decl)
1649 && ((!DECL_SIZE (decl) || !DECL_SIZE (item->decl))
1650 || !operand_equal_p (DECL_SIZE (decl),
1651 DECL_SIZE (item->decl), OEP_ONLY_CONST)))
1652 return return_false_with_msg ("size mismatch");
1654 /* Do not attempt to mix data from different user sections;
1655 we do not know what user intends with those. */
1656 if (((DECL_SECTION_NAME (decl) && !node->implicit_section)
1657 || (DECL_SECTION_NAME (item->decl) && !item->node->implicit_section))
1658 && DECL_SECTION_NAME (decl) != DECL_SECTION_NAME (item->decl))
1659 return return_false_with_msg ("user section mismatch");
1661 if (DECL_IN_TEXT_SECTION (decl) != DECL_IN_TEXT_SECTION (item->decl))
1662 return return_false_with_msg ("text section");
1664 ipa_ref *ref = NULL, *ref2 = NULL;
1665 for (unsigned i = 0; node->iterate_reference (i, ref); i++)
1667 item->node->iterate_reference (i, ref2);
1669 if (ref->use != ref2->use)
1670 return return_false_with_msg ("reference use mismatch");
1672 if (!compare_symbol_references (ignored_nodes,
1673 ref->referred, ref2->referred,
1674 ref->address_matters_p ()))
1675 return false;
1678 return true;
1681 /* Returns true if the item equals to ITEM given as argument. */
1683 bool
1684 sem_variable::equals (sem_item *item,
1685 hash_map <symtab_node *, sem_item *> &)
1687 gcc_assert (item->type == VAR);
1688 bool ret;
1690 if (DECL_INITIAL (decl) == error_mark_node && in_lto_p)
1691 dyn_cast <varpool_node *>(node)->get_constructor ();
1692 if (DECL_INITIAL (item->decl) == error_mark_node && in_lto_p)
1693 dyn_cast <varpool_node *>(item->node)->get_constructor ();
1695 /* As seen in PR ipa/65303 we have to compare variables types. */
1696 if (!func_checker::compatible_types_p (TREE_TYPE (decl),
1697 TREE_TYPE (item->decl)))
1698 return return_false_with_msg ("variables types are different");
1700 ret = sem_variable::equals (DECL_INITIAL (decl),
1701 DECL_INITIAL (item->node->decl));
1702 if (dump_file && (dump_flags & TDF_DETAILS))
1703 fprintf (dump_file,
1704 "Equals called for vars: %s:%s with result: %s\n\n",
1705 node->dump_name (), item->node->dump_name (),
1706 ret ? "true" : "false");
1708 return ret;
1711 /* Compares trees T1 and T2 for semantic equality. */
1713 bool
1714 sem_variable::equals (tree t1, tree t2)
1716 if (!t1 || !t2)
1717 return return_with_debug (t1 == t2);
1718 if (t1 == t2)
1719 return true;
1720 tree_code tc1 = TREE_CODE (t1);
1721 tree_code tc2 = TREE_CODE (t2);
1723 if (tc1 != tc2)
1724 return return_false_with_msg ("TREE_CODE mismatch");
1726 switch (tc1)
1728 case CONSTRUCTOR:
1730 vec<constructor_elt, va_gc> *v1, *v2;
1731 unsigned HOST_WIDE_INT idx;
1733 enum tree_code typecode = TREE_CODE (TREE_TYPE (t1));
1734 if (typecode != TREE_CODE (TREE_TYPE (t2)))
1735 return return_false_with_msg ("constructor type mismatch");
1737 if (typecode == ARRAY_TYPE)
1739 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
1740 /* For arrays, check that the sizes all match. */
1741 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
1742 || size_1 == -1
1743 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
1744 return return_false_with_msg ("constructor array size mismatch");
1746 else if (!func_checker::compatible_types_p (TREE_TYPE (t1),
1747 TREE_TYPE (t2)))
1748 return return_false_with_msg ("constructor type incompatible");
1750 v1 = CONSTRUCTOR_ELTS (t1);
1751 v2 = CONSTRUCTOR_ELTS (t2);
1752 if (vec_safe_length (v1) != vec_safe_length (v2))
1753 return return_false_with_msg ("constructor number of elts mismatch");
1755 for (idx = 0; idx < vec_safe_length (v1); ++idx)
1757 constructor_elt *c1 = &(*v1)[idx];
1758 constructor_elt *c2 = &(*v2)[idx];
1760 /* Check that each value is the same... */
1761 if (!sem_variable::equals (c1->value, c2->value))
1762 return false;
1763 /* ... and that they apply to the same fields! */
1764 if (!sem_variable::equals (c1->index, c2->index))
1765 return false;
1767 return true;
1769 case MEM_REF:
1771 tree x1 = TREE_OPERAND (t1, 0);
1772 tree x2 = TREE_OPERAND (t2, 0);
1773 tree y1 = TREE_OPERAND (t1, 1);
1774 tree y2 = TREE_OPERAND (t2, 1);
1776 if (!func_checker::compatible_types_p (TREE_TYPE (x1), TREE_TYPE (x2)))
1777 return return_false ();
1779 /* Type of the offset on MEM_REF does not matter. */
1780 return return_with_debug (sem_variable::equals (x1, x2)
1781 && known_eq (wi::to_poly_offset (y1),
1782 wi::to_poly_offset (y2)));
1784 case ADDR_EXPR:
1785 case FDESC_EXPR:
1787 tree op1 = TREE_OPERAND (t1, 0);
1788 tree op2 = TREE_OPERAND (t2, 0);
1789 return sem_variable::equals (op1, op2);
1791 /* References to other vars/decls are compared using ipa-ref. */
1792 case FUNCTION_DECL:
1793 case VAR_DECL:
1794 if (decl_in_symtab_p (t1) && decl_in_symtab_p (t2))
1795 return true;
1796 return return_false_with_msg ("Declaration mismatch");
1797 case CONST_DECL:
1798 /* TODO: We can check CONST_DECL by its DECL_INITIAL, but for that we
1799 need to process its VAR/FUNCTION references without relying on ipa-ref
1800 compare. */
1801 case FIELD_DECL:
1802 case LABEL_DECL:
1803 return return_false_with_msg ("Declaration mismatch");
1804 case INTEGER_CST:
1805 /* Integer constants are the same only if the same width of type. */
1806 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
1807 return return_false_with_msg ("INTEGER_CST precision mismatch");
1808 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
1809 return return_false_with_msg ("INTEGER_CST mode mismatch");
1810 return return_with_debug (tree_int_cst_equal (t1, t2));
1811 case STRING_CST:
1812 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
1813 return return_false_with_msg ("STRING_CST mode mismatch");
1814 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2))
1815 return return_false_with_msg ("STRING_CST length mismatch");
1816 if (memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1817 TREE_STRING_LENGTH (t1)))
1818 return return_false_with_msg ("STRING_CST mismatch");
1819 return true;
1820 case FIXED_CST:
1821 /* Fixed constants are the same only if the same width of type. */
1822 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
1823 return return_false_with_msg ("FIXED_CST precision mismatch");
1825 return return_with_debug (FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
1826 TREE_FIXED_CST (t2)));
1827 case COMPLEX_CST:
1828 return (sem_variable::equals (TREE_REALPART (t1), TREE_REALPART (t2))
1829 && sem_variable::equals (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
1830 case REAL_CST:
1831 /* Real constants are the same only if the same width of type. */
1832 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
1833 return return_false_with_msg ("REAL_CST precision mismatch");
1834 return return_with_debug (real_identical (&TREE_REAL_CST (t1),
1835 &TREE_REAL_CST (t2)));
1836 case VECTOR_CST:
1838 if (maybe_ne (VECTOR_CST_NELTS (t1), VECTOR_CST_NELTS (t2)))
1839 return return_false_with_msg ("VECTOR_CST nelts mismatch");
1841 unsigned int count
1842 = tree_vector_builder::binary_encoded_nelts (t1, t2);
1843 for (unsigned int i = 0; i < count; ++i)
1844 if (!sem_variable::equals (VECTOR_CST_ENCODED_ELT (t1, i),
1845 VECTOR_CST_ENCODED_ELT (t2, i)))
1846 return false;
1848 return true;
1850 case ARRAY_REF:
1851 case ARRAY_RANGE_REF:
1853 tree x1 = TREE_OPERAND (t1, 0);
1854 tree x2 = TREE_OPERAND (t2, 0);
1855 tree y1 = TREE_OPERAND (t1, 1);
1856 tree y2 = TREE_OPERAND (t2, 1);
1858 if (!sem_variable::equals (x1, x2) || !sem_variable::equals (y1, y2))
1859 return false;
1860 if (!sem_variable::equals (array_ref_low_bound (t1),
1861 array_ref_low_bound (t2)))
1862 return false;
1863 if (!sem_variable::equals (array_ref_element_size (t1),
1864 array_ref_element_size (t2)))
1865 return false;
1866 return true;
1869 case COMPONENT_REF:
1870 case POINTER_PLUS_EXPR:
1871 case PLUS_EXPR:
1872 case MINUS_EXPR:
1873 case RANGE_EXPR:
1875 tree x1 = TREE_OPERAND (t1, 0);
1876 tree x2 = TREE_OPERAND (t2, 0);
1877 tree y1 = TREE_OPERAND (t1, 1);
1878 tree y2 = TREE_OPERAND (t2, 1);
1880 return sem_variable::equals (x1, x2) && sem_variable::equals (y1, y2);
1883 CASE_CONVERT:
1884 case VIEW_CONVERT_EXPR:
1885 if (!func_checker::compatible_types_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1886 return return_false ();
1887 return sem_variable::equals (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1888 case ERROR_MARK:
1889 return return_false_with_msg ("ERROR_MARK");
1890 default:
1891 return return_false_with_msg ("Unknown TREE code reached");
1895 /* Parser function that visits a varpool NODE. */
1897 sem_variable *
1898 sem_variable::parse (varpool_node *node, bitmap_obstack *stack,
1899 func_checker *checker)
1901 if (TREE_THIS_VOLATILE (node->decl) || DECL_HARD_REGISTER (node->decl)
1902 || node->alias)
1903 return NULL;
1905 sem_variable *v = new sem_variable (node, stack);
1906 v->init (checker);
1908 return v;
1911 /* Semantic variable initialization function. */
1913 void
1914 sem_variable::init (ipa_icf_gimple::func_checker *checker)
1916 decl = get_node ()->decl;
1918 /* All WPA streamed in symbols should have their hashes computed at compile
1919 time. At this point, the constructor may not be in memory at all.
1920 DECL_INITIAL (decl) would be error_mark_node in that case. */
1921 if (!m_hash_set)
1923 gcc_assert (!node->lto_file_data);
1924 inchash::hash hstate;
1925 hstate.add_int (456346417);
1926 checker->hash_operand (DECL_INITIAL (decl), hstate, 0);
1927 set_hash (hstate.end ());
1931 /* References independent hash function. */
1933 hashval_t
1934 sem_variable::get_hash (void)
1936 gcc_checking_assert (m_hash_set);
1937 return m_hash;
1940 /* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can
1941 be applied. */
1943 bool
1944 sem_variable::merge (sem_item *alias_item)
1946 gcc_assert (alias_item->type == VAR);
1948 AUTO_DUMP_SCOPE ("merge",
1949 dump_user_location_t::from_function_decl (decl));
1950 if (!sem_item::target_supports_symbol_aliases_p ())
1952 if (dump_enabled_p ())
1953 dump_printf (MSG_MISSED_OPTIMIZATION, "Not unifying; "
1954 "Symbol aliases are not supported by target\n");
1955 return false;
1958 if (DECL_EXTERNAL (alias_item->decl))
1960 if (dump_enabled_p ())
1961 dump_printf (MSG_MISSED_OPTIMIZATION,
1962 "Not unifying; alias is external.\n");
1963 return false;
1966 sem_variable *alias_var = static_cast<sem_variable *> (alias_item);
1968 varpool_node *original = get_node ();
1969 varpool_node *alias = alias_var->get_node ();
1970 bool original_discardable = false;
1972 bool alias_address_matters = alias->address_matters_p ();
1974 /* See if original is in a section that can be discarded if the main
1975 symbol is not used.
1976 Also consider case where we have resolution info and we know that
1977 original's definition is not going to be used. In this case we cannot
1978 create alias to original. */
1979 if (original->can_be_discarded_p ()
1980 || (node->resolution != LDPR_UNKNOWN
1981 && !decl_binds_to_current_def_p (node->decl)))
1982 original_discardable = true;
1984 gcc_assert (!TREE_ASM_WRITTEN (alias->decl));
1986 /* Constant pool machinery is not quite ready for aliases.
1987 TODO: varasm code contains logic for merging DECL_IN_CONSTANT_POOL.
1988 For LTO merging does not happen that is an important missing feature.
1989 We can enable merging with LTO if the DECL_IN_CONSTANT_POOL
1990 flag is dropped and non-local symbol name is assigned. */
1991 if (DECL_IN_CONSTANT_POOL (alias->decl)
1992 || DECL_IN_CONSTANT_POOL (original->decl))
1994 if (dump_enabled_p ())
1995 dump_printf (MSG_MISSED_OPTIMIZATION,
1996 "Not unifying; constant pool variables.\n");
1997 return false;
2000 /* Do not attempt to mix functions from different user sections;
2001 we do not know what user intends with those. */
2002 if (((DECL_SECTION_NAME (original->decl) && !original->implicit_section)
2003 || (DECL_SECTION_NAME (alias->decl) && !alias->implicit_section))
2004 && DECL_SECTION_NAME (original->decl) != DECL_SECTION_NAME (alias->decl))
2006 if (dump_enabled_p ())
2007 dump_printf (MSG_MISSED_OPTIMIZATION,
2008 "Not unifying; "
2009 "original and alias are in different sections.\n");
2010 return false;
2013 /* We cannot merge if address comparison matters. */
2014 if (alias_address_matters && flag_merge_constants < 2)
2016 if (dump_enabled_p ())
2017 dump_printf (MSG_MISSED_OPTIMIZATION,
2018 "Not unifying; address of original may be compared.\n");
2019 return false;
2022 if (DECL_ALIGN (original->decl) < DECL_ALIGN (alias->decl))
2024 if (dump_enabled_p ())
2025 dump_printf (MSG_MISSED_OPTIMIZATION,
2026 "Not unifying; "
2027 "original and alias have incompatible alignments\n");
2029 return false;
2032 if (DECL_COMDAT_GROUP (original->decl) != DECL_COMDAT_GROUP (alias->decl))
2034 if (dump_enabled_p ())
2035 dump_printf (MSG_MISSED_OPTIMIZATION,
2036 "Not unifying; alias cannot be created; "
2037 "across comdat group boundary\n");
2039 return false;
2042 if (original_discardable)
2044 if (dump_enabled_p ())
2045 dump_printf (MSG_MISSED_OPTIMIZATION,
2046 "Not unifying; alias cannot be created; "
2047 "target is discardable\n");
2049 return false;
2051 else
2053 gcc_assert (!original->alias);
2054 gcc_assert (!alias->alias);
2056 alias->analyzed = false;
2058 DECL_INITIAL (alias->decl) = NULL;
2059 ((symtab_node *)alias)->call_for_symbol_and_aliases (clear_decl_rtl,
2060 NULL, true);
2061 alias->remove_all_references ();
2062 if (TREE_ADDRESSABLE (alias->decl))
2063 original->call_for_symbol_and_aliases (set_addressable, NULL, true);
2065 varpool_node::create_alias (alias_var->decl, decl);
2066 alias->resolve_alias (original);
2068 if (dump_enabled_p ())
2069 dump_printf (MSG_OPTIMIZED_LOCATIONS,
2070 "Unified; Variable alias has been created.\n");
2072 return true;
2076 /* Dump symbol to FILE. */
2078 void
2079 sem_variable::dump_to_file (FILE *file)
2081 gcc_assert (file);
2083 print_node (file, "", decl, 0);
2084 fprintf (file, "\n\n");
2087 unsigned int sem_item_optimizer::class_id = 0;
2089 sem_item_optimizer::sem_item_optimizer ()
2090 : worklist (0), m_classes (0), m_classes_count (0), m_cgraph_node_hooks (NULL),
2091 m_varpool_node_hooks (NULL), m_merged_variables (), m_references ()
2093 m_items.create (0);
2094 bitmap_obstack_initialize (&m_bmstack);
2097 sem_item_optimizer::~sem_item_optimizer ()
2099 for (unsigned int i = 0; i < m_items.length (); i++)
2100 delete m_items[i];
2103 for (hash_table<congruence_class_hash>::iterator it = m_classes.begin ();
2104 it != m_classes.end (); ++it)
2106 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
2107 delete (*it)->classes[i];
2109 (*it)->classes.release ();
2110 free (*it);
2113 m_items.release ();
2115 bitmap_obstack_release (&m_bmstack);
2116 m_merged_variables.release ();
2119 /* Write IPA ICF summary for symbols. */
2121 void
2122 sem_item_optimizer::write_summary (void)
2124 unsigned int count = 0;
2126 output_block *ob = create_output_block (LTO_section_ipa_icf);
2127 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
2128 ob->symbol = NULL;
2130 /* Calculate number of symbols to be serialized. */
2131 for (lto_symtab_encoder_iterator lsei = lsei_start_in_partition (encoder);
2132 !lsei_end_p (lsei);
2133 lsei_next_in_partition (&lsei))
2135 symtab_node *node = lsei_node (lsei);
2137 if (m_symtab_node_map.get (node))
2138 count++;
2141 streamer_write_uhwi (ob, count);
2143 /* Process all of the symbols. */
2144 for (lto_symtab_encoder_iterator lsei = lsei_start_in_partition (encoder);
2145 !lsei_end_p (lsei);
2146 lsei_next_in_partition (&lsei))
2148 symtab_node *node = lsei_node (lsei);
2150 sem_item **item = m_symtab_node_map.get (node);
2152 if (item && *item)
2154 int node_ref = lto_symtab_encoder_encode (encoder, node);
2155 streamer_write_uhwi_stream (ob->main_stream, node_ref);
2157 streamer_write_uhwi (ob, (*item)->get_hash ());
2159 if ((*item)->type == FUNC)
2161 sem_function *fn = static_cast<sem_function *> (*item);
2162 streamer_write_uhwi (ob, fn->memory_access_types.length ());
2163 for (unsigned i = 0; i < fn->memory_access_types.length (); i++)
2164 stream_write_tree (ob, fn->memory_access_types[i], true);
2169 streamer_write_char_stream (ob->main_stream, 0);
2170 produce_asm (ob, NULL);
2171 destroy_output_block (ob);
2174 /* Reads a section from LTO stream file FILE_DATA. Input block for DATA
2175 contains LEN bytes. */
2177 void
2178 sem_item_optimizer::read_section (lto_file_decl_data *file_data,
2179 const char *data, size_t len)
2181 const lto_function_header *header
2182 = (const lto_function_header *) data;
2183 const int cfg_offset = sizeof (lto_function_header);
2184 const int main_offset = cfg_offset + header->cfg_size;
2185 const int string_offset = main_offset + header->main_size;
2186 data_in *data_in;
2187 unsigned int i;
2188 unsigned int count;
2190 lto_input_block ib_main ((const char *) data + main_offset, 0,
2191 header->main_size, file_data->mode_table);
2193 data_in
2194 = lto_data_in_create (file_data, (const char *) data + string_offset,
2195 header->string_size, vNULL);
2197 count = streamer_read_uhwi (&ib_main);
2199 for (i = 0; i < count; i++)
2201 unsigned int index;
2202 symtab_node *node;
2203 lto_symtab_encoder_t encoder;
2205 index = streamer_read_uhwi (&ib_main);
2206 encoder = file_data->symtab_node_encoder;
2207 node = lto_symtab_encoder_deref (encoder, index);
2209 hashval_t hash = streamer_read_uhwi (&ib_main);
2210 gcc_assert (node->definition);
2212 if (is_a<cgraph_node *> (node))
2214 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2216 sem_function *fn = new sem_function (cnode, &m_bmstack);
2217 unsigned count = streamer_read_uhwi (&ib_main);
2218 inchash::hash hstate (0);
2219 if (flag_incremental_link == INCREMENTAL_LINK_LTO)
2220 fn->memory_access_types.reserve_exact (count);
2221 for (unsigned i = 0; i < count; i++)
2223 tree type = stream_read_tree (&ib_main, data_in);
2224 hstate.add_int (get_deref_alias_set (type));
2225 if (flag_incremental_link == INCREMENTAL_LINK_LTO)
2226 fn->memory_access_types.quick_push (type);
2228 fn->m_alias_sets_hash = hstate.end ();
2229 fn->set_hash (hash);
2230 m_items.safe_push (fn);
2232 else
2234 varpool_node *vnode = dyn_cast <varpool_node *> (node);
2236 sem_variable *var = new sem_variable (vnode, &m_bmstack);
2237 var->set_hash (hash);
2238 m_items.safe_push (var);
2242 lto_free_section_data (file_data, LTO_section_ipa_icf, NULL, data,
2243 len);
2244 lto_data_in_delete (data_in);
2247 /* Read IPA ICF summary for symbols. */
2249 void
2250 sem_item_optimizer::read_summary (void)
2252 lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2253 lto_file_decl_data *file_data;
2254 unsigned int j = 0;
2256 while ((file_data = file_data_vec[j++]))
2258 size_t len;
2259 const char *data
2260 = lto_get_summary_section_data (file_data, LTO_section_ipa_icf, &len);
2261 if (data)
2262 read_section (file_data, data, len);
2266 /* Register callgraph and varpool hooks. */
2268 void
2269 sem_item_optimizer::register_hooks (void)
2271 if (!m_cgraph_node_hooks)
2272 m_cgraph_node_hooks = symtab->add_cgraph_removal_hook
2273 (&sem_item_optimizer::cgraph_removal_hook, this);
2275 if (!m_varpool_node_hooks)
2276 m_varpool_node_hooks = symtab->add_varpool_removal_hook
2277 (&sem_item_optimizer::varpool_removal_hook, this);
2280 /* Unregister callgraph and varpool hooks. */
2282 void
2283 sem_item_optimizer::unregister_hooks (void)
2285 if (m_cgraph_node_hooks)
2286 symtab->remove_cgraph_removal_hook (m_cgraph_node_hooks);
2288 if (m_varpool_node_hooks)
2289 symtab->remove_varpool_removal_hook (m_varpool_node_hooks);
2292 /* Adds a CLS to hashtable associated by hash value. */
2294 void
2295 sem_item_optimizer::add_class (congruence_class *cls)
2297 gcc_assert (cls->members.length ());
2299 congruence_class_group *group
2300 = get_group_by_hash (cls->members[0]->get_hash (),
2301 cls->members[0]->type);
2302 group->classes.safe_push (cls);
2305 /* Gets a congruence class group based on given HASH value and TYPE. */
2307 congruence_class_group *
2308 sem_item_optimizer::get_group_by_hash (hashval_t hash, sem_item_type type)
2310 congruence_class_group *item = XNEW (congruence_class_group);
2311 item->hash = hash;
2312 item->type = type;
2314 congruence_class_group **slot = m_classes.find_slot (item, INSERT);
2316 if (*slot)
2317 free (item);
2318 else
2320 item->classes.create (1);
2321 *slot = item;
2324 return *slot;
2327 /* Callgraph removal hook called for a NODE with a custom DATA. */
2329 void
2330 sem_item_optimizer::cgraph_removal_hook (cgraph_node *node, void *data)
2332 sem_item_optimizer *optimizer = (sem_item_optimizer *) data;
2333 optimizer->remove_symtab_node (node);
2336 /* Varpool removal hook called for a NODE with a custom DATA. */
2338 void
2339 sem_item_optimizer::varpool_removal_hook (varpool_node *node, void *data)
2341 sem_item_optimizer *optimizer = (sem_item_optimizer *) data;
2342 optimizer->remove_symtab_node (node);
2345 /* Remove symtab NODE triggered by symtab removal hooks. */
2347 void
2348 sem_item_optimizer::remove_symtab_node (symtab_node *node)
2350 gcc_assert (m_classes.is_empty ());
2352 m_removed_items_set.add (node);
2355 void
2356 sem_item_optimizer::remove_item (sem_item *item)
2358 if (m_symtab_node_map.get (item->node))
2359 m_symtab_node_map.remove (item->node);
2360 delete item;
2363 /* Removes all callgraph and varpool nodes that are marked by symtab
2364 as deleted. */
2366 void
2367 sem_item_optimizer::filter_removed_items (void)
2369 auto_vec <sem_item *> filtered;
2371 for (unsigned int i = 0; i < m_items.length(); i++)
2373 sem_item *item = m_items[i];
2375 if (m_removed_items_set.contains (item->node))
2377 remove_item (item);
2378 continue;
2381 if (item->type == FUNC)
2383 cgraph_node *cnode = static_cast <sem_function *>(item)->get_node ();
2385 if (in_lto_p && (cnode->alias || cnode->body_removed))
2386 remove_item (item);
2387 else
2388 filtered.safe_push (item);
2390 else /* VAR. */
2392 if (!flag_ipa_icf_variables)
2393 remove_item (item);
2394 else
2396 /* Filter out non-readonly variables. */
2397 tree decl = item->decl;
2398 if (TREE_READONLY (decl))
2399 filtered.safe_push (item);
2400 else
2401 remove_item (item);
2406 /* Clean-up of released semantic items. */
2408 m_items.release ();
2409 for (unsigned int i = 0; i < filtered.length(); i++)
2410 m_items.safe_push (filtered[i]);
2413 /* Optimizer entry point which returns true in case it processes
2414 a merge operation. True is returned if there's a merge operation
2415 processed. */
2417 bool
2418 sem_item_optimizer::execute (void)
2420 filter_removed_items ();
2421 unregister_hooks ();
2423 build_graph ();
2424 update_hash_by_addr_refs ();
2425 update_hash_by_memory_access_type ();
2426 build_hash_based_classes ();
2428 if (dump_file)
2429 fprintf (dump_file, "Dump after hash based groups\n");
2430 dump_cong_classes ();
2432 subdivide_classes_by_equality (true);
2434 if (dump_file)
2435 fprintf (dump_file, "Dump after WPA based types groups\n");
2437 dump_cong_classes ();
2439 process_cong_reduction ();
2440 checking_verify_classes ();
2442 if (dump_file)
2443 fprintf (dump_file, "Dump after callgraph-based congruence reduction\n");
2445 dump_cong_classes ();
2447 unsigned int loaded_symbols = parse_nonsingleton_classes ();
2448 subdivide_classes_by_equality ();
2450 if (dump_file)
2451 fprintf (dump_file, "Dump after full equality comparison of groups\n");
2453 dump_cong_classes ();
2455 unsigned int prev_class_count = m_classes_count;
2457 process_cong_reduction ();
2458 dump_cong_classes ();
2459 checking_verify_classes ();
2460 bool merged_p = merge_classes (prev_class_count, loaded_symbols);
2462 if (dump_file && (dump_flags & TDF_DETAILS))
2463 symtab->dump (dump_file);
2465 return merged_p;
2468 /* Function responsible for visiting all potential functions and
2469 read-only variables that can be merged. */
2471 void
2472 sem_item_optimizer::parse_funcs_and_vars (void)
2474 cgraph_node *cnode;
2476 /* Create dummy func_checker for hashing purpose. */
2477 func_checker checker;
2479 if (flag_ipa_icf_functions)
2480 FOR_EACH_DEFINED_FUNCTION (cnode)
2482 sem_function *f = sem_function::parse (cnode, &m_bmstack, &checker);
2483 if (f)
2485 m_items.safe_push (f);
2486 m_symtab_node_map.put (cnode, f);
2490 varpool_node *vnode;
2492 if (flag_ipa_icf_variables)
2493 FOR_EACH_DEFINED_VARIABLE (vnode)
2495 sem_variable *v = sem_variable::parse (vnode, &m_bmstack, &checker);
2497 if (v)
2499 m_items.safe_push (v);
2500 m_symtab_node_map.put (vnode, v);
2505 /* Makes pairing between a congruence class CLS and semantic ITEM. */
2507 void
2508 sem_item_optimizer::add_item_to_class (congruence_class *cls, sem_item *item)
2510 item->index_in_class = cls->members.length ();
2511 cls->members.safe_push (item);
2512 cls->referenced_by_count += item->referenced_by_count;
2513 item->cls = cls;
2516 /* For each semantic item, append hash values of references. */
2518 void
2519 sem_item_optimizer::update_hash_by_addr_refs ()
2521 /* First, append to hash sensitive references and class type if it need to
2522 be matched for ODR. */
2523 for (unsigned i = 0; i < m_items.length (); i++)
2525 m_items[i]->update_hash_by_addr_refs (m_symtab_node_map);
2526 if (m_items[i]->type == FUNC)
2528 if (TREE_CODE (TREE_TYPE (m_items[i]->decl)) == METHOD_TYPE
2529 && contains_polymorphic_type_p
2530 (TYPE_METHOD_BASETYPE (TREE_TYPE (m_items[i]->decl)))
2531 && (DECL_CXX_CONSTRUCTOR_P (m_items[i]->decl)
2532 || (static_cast<sem_function *> (m_items[i])->param_used_p (0)
2533 && static_cast<sem_function *> (m_items[i])
2534 ->compare_polymorphic_p ())))
2536 tree class_type
2537 = TYPE_METHOD_BASETYPE (TREE_TYPE (m_items[i]->decl));
2538 inchash::hash hstate (m_items[i]->get_hash ());
2540 /* Hash ODR types by mangled name if it is defined.
2541 If not we know that type is anonymous of free_lang_data
2542 was not run and in that case type main variants are
2543 unique. */
2544 if (TYPE_NAME (class_type)
2545 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (class_type))
2546 && !type_in_anonymous_namespace_p
2547 (class_type))
2548 hstate.add_hwi
2549 (IDENTIFIER_HASH_VALUE
2550 (DECL_ASSEMBLER_NAME (TYPE_NAME (class_type))));
2551 else
2553 gcc_checking_assert
2554 (!in_lto_p
2555 || type_in_anonymous_namespace_p (class_type));
2556 hstate.add_hwi (TYPE_UID (TYPE_MAIN_VARIANT (class_type)));
2559 m_items[i]->set_hash (hstate.end ());
2564 /* Once all symbols have enhanced hash value, we can append
2565 hash values of symbols that are seen by IPA ICF and are
2566 references by a semantic item. Newly computed values
2567 are saved to global_hash member variable. */
2568 for (unsigned i = 0; i < m_items.length (); i++)
2569 m_items[i]->update_hash_by_local_refs (m_symtab_node_map);
2571 /* Global hash value replace current hash values. */
2572 for (unsigned i = 0; i < m_items.length (); i++)
2573 m_items[i]->set_hash (m_items[i]->global_hash);
2576 void
2577 sem_item_optimizer::update_hash_by_memory_access_type ()
2579 for (unsigned i = 0; i < m_items.length (); i++)
2581 if (m_items[i]->type == FUNC)
2583 sem_function *fn = static_cast<sem_function *> (m_items[i]);
2584 inchash::hash hstate (fn->get_hash ());
2585 hstate.add_int (fn->m_alias_sets_hash);
2586 fn->set_hash (hstate.end ());
2591 /* Congruence classes are built by hash value. */
2593 void
2594 sem_item_optimizer::build_hash_based_classes (void)
2596 for (unsigned i = 0; i < m_items.length (); i++)
2598 sem_item *item = m_items[i];
2600 congruence_class_group *group
2601 = get_group_by_hash (item->get_hash (), item->type);
2603 if (!group->classes.length ())
2605 m_classes_count++;
2606 group->classes.safe_push (new congruence_class (class_id++));
2609 add_item_to_class (group->classes[0], item);
2613 /* Build references according to call graph. */
2615 void
2616 sem_item_optimizer::build_graph (void)
2618 for (unsigned i = 0; i < m_items.length (); i++)
2620 sem_item *item = m_items[i];
2621 m_symtab_node_map.put (item->node, item);
2623 /* Initialize hash values if we are not in LTO mode. */
2624 if (!in_lto_p)
2625 item->get_hash ();
2628 for (unsigned i = 0; i < m_items.length (); i++)
2630 sem_item *item = m_items[i];
2632 if (item->type == FUNC)
2634 cgraph_node *cnode = dyn_cast <cgraph_node *> (item->node);
2636 cgraph_edge *e = cnode->callees;
2637 while (e)
2639 sem_item **slot = m_symtab_node_map.get
2640 (e->callee->ultimate_alias_target ());
2641 if (slot)
2642 item->add_reference (&m_references, *slot);
2644 e = e->next_callee;
2648 ipa_ref *ref = NULL;
2649 for (unsigned i = 0; item->node->iterate_reference (i, ref); i++)
2651 sem_item **slot = m_symtab_node_map.get
2652 (ref->referred->ultimate_alias_target ());
2653 if (slot)
2654 item->add_reference (&m_references, *slot);
2659 /* Semantic items in classes having more than one element and initialized.
2660 In case of WPA, we load function body. */
2662 unsigned int
2663 sem_item_optimizer::parse_nonsingleton_classes (void)
2665 unsigned int counter = 0;
2667 /* Create dummy func_checker for hashing purpose. */
2668 func_checker checker;
2670 for (unsigned i = 0; i < m_items.length (); i++)
2671 if (m_items[i]->cls->members.length () > 1)
2673 m_items[i]->init (&checker);
2674 ++counter;
2677 if (dump_file)
2679 float f = m_items.length () ? 100.0f * counter / m_items.length () : 0.0f;
2680 fprintf (dump_file, "Init called for %u items (%.2f%%).\n", counter, f);
2683 return counter;
2686 /* Equality function for semantic items is used to subdivide existing
2687 classes. If IN_WPA, fast equality function is invoked. */
2689 void
2690 sem_item_optimizer::subdivide_classes_by_equality (bool in_wpa)
2692 for (hash_table <congruence_class_hash>::iterator it = m_classes.begin ();
2693 it != m_classes.end (); ++it)
2695 unsigned int class_count = (*it)->classes.length ();
2697 for (unsigned i = 0; i < class_count; i++)
2699 congruence_class *c = (*it)->classes[i];
2701 if (c->members.length() > 1)
2703 auto_vec <sem_item *> new_vector;
2705 sem_item *first = c->members[0];
2706 new_vector.safe_push (first);
2708 unsigned class_split_first = (*it)->classes.length ();
2710 for (unsigned j = 1; j < c->members.length (); j++)
2712 sem_item *item = c->members[j];
2714 bool equals
2715 = in_wpa ? first->equals_wpa (item, m_symtab_node_map)
2716 : first->equals (item, m_symtab_node_map);
2718 if (equals)
2719 new_vector.safe_push (item);
2720 else
2722 bool integrated = false;
2724 for (unsigned k = class_split_first;
2725 k < (*it)->classes.length (); k++)
2727 sem_item *x = (*it)->classes[k]->members[0];
2728 bool equals
2729 = in_wpa ? x->equals_wpa (item, m_symtab_node_map)
2730 : x->equals (item, m_symtab_node_map);
2732 if (equals)
2734 integrated = true;
2735 add_item_to_class ((*it)->classes[k], item);
2737 break;
2741 if (!integrated)
2743 congruence_class *c
2744 = new congruence_class (class_id++);
2745 m_classes_count++;
2746 add_item_to_class (c, item);
2748 (*it)->classes.safe_push (c);
2753 // We replace newly created new_vector for the class we've just
2754 // splitted.
2755 c->members.release ();
2756 c->members.create (new_vector.length ());
2758 for (unsigned int j = 0; j < new_vector.length (); j++)
2759 add_item_to_class (c, new_vector[j]);
2764 checking_verify_classes ();
2767 /* Subdivide classes by address references that members of the class
2768 reference. Example can be a pair of functions that have an address
2769 taken from a function. If these addresses are different the class
2770 is split. */
2772 unsigned
2773 sem_item_optimizer::subdivide_classes_by_sensitive_refs ()
2775 typedef hash_map <symbol_compare_hash, vec <sem_item *> > subdivide_hash_map;
2777 unsigned newly_created_classes = 0;
2779 for (hash_table <congruence_class_hash>::iterator it = m_classes.begin ();
2780 it != m_classes.end (); ++it)
2782 unsigned int class_count = (*it)->classes.length ();
2783 auto_vec<congruence_class *> new_classes;
2785 for (unsigned i = 0; i < class_count; i++)
2787 congruence_class *c = (*it)->classes[i];
2789 if (c->members.length() > 1)
2791 subdivide_hash_map split_map;
2793 for (unsigned j = 0; j < c->members.length (); j++)
2795 sem_item *source_node = c->members[j];
2797 symbol_compare_collection *collection
2798 = new symbol_compare_collection (source_node->node);
2800 bool existed;
2801 vec <sem_item *> *slot
2802 = &split_map.get_or_insert (collection, &existed);
2803 gcc_checking_assert (slot);
2805 slot->safe_push (source_node);
2807 if (existed)
2808 delete collection;
2811 /* If the map contains more than one key, we have to split
2812 the map appropriately. */
2813 if (split_map.elements () != 1)
2815 bool first_class = true;
2817 for (subdivide_hash_map::iterator it2 = split_map.begin ();
2818 it2 != split_map.end (); ++it2)
2820 congruence_class *new_cls;
2821 new_cls = new congruence_class (class_id++);
2823 for (unsigned k = 0; k < (*it2).second.length (); k++)
2824 add_item_to_class (new_cls, (*it2).second[k]);
2826 worklist_push (new_cls);
2827 newly_created_classes++;
2829 if (first_class)
2831 (*it)->classes[i] = new_cls;
2832 first_class = false;
2834 else
2836 new_classes.safe_push (new_cls);
2837 m_classes_count++;
2842 /* Release memory. */
2843 for (subdivide_hash_map::iterator it2 = split_map.begin ();
2844 it2 != split_map.end (); ++it2)
2846 delete (*it2).first;
2847 (*it2).second.release ();
2852 for (unsigned i = 0; i < new_classes.length (); i++)
2853 (*it)->classes.safe_push (new_classes[i]);
2856 return newly_created_classes;
2859 /* Verify congruence classes, if checking is enabled. */
2861 void
2862 sem_item_optimizer::checking_verify_classes (void)
2864 if (flag_checking)
2865 verify_classes ();
2868 /* Verify congruence classes. */
2870 void
2871 sem_item_optimizer::verify_classes (void)
2873 for (hash_table<congruence_class_hash>::iterator it = m_classes.begin ();
2874 it != m_classes.end (); ++it)
2876 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
2878 congruence_class *cls = (*it)->classes[i];
2880 gcc_assert (cls);
2881 gcc_assert (cls->members.length () > 0);
2883 for (unsigned int j = 0; j < cls->members.length (); j++)
2885 sem_item *item = cls->members[j];
2887 gcc_assert (item);
2888 gcc_assert (item->cls == cls);
2894 /* Disposes split map traverse function. CLS_PTR is pointer to congruence
2895 class, BSLOT is bitmap slot we want to release. DATA is mandatory,
2896 but unused argument. */
2898 bool
2899 sem_item_optimizer::release_split_map (congruence_class * const &,
2900 bitmap const &b, traverse_split_pair *)
2902 bitmap bmp = b;
2904 BITMAP_FREE (bmp);
2906 return true;
2909 /* Process split operation for a class given as pointer CLS_PTR,
2910 where bitmap B splits congruence class members. DATA is used
2911 as argument of split pair. */
2913 bool
2914 sem_item_optimizer::traverse_congruence_split (congruence_class * const &cls,
2915 bitmap const &b,
2916 traverse_split_pair *pair)
2918 sem_item_optimizer *optimizer = pair->optimizer;
2919 const congruence_class *splitter_cls = pair->cls;
2921 /* If counted bits are greater than zero and less than the number of members
2922 a group will be splitted. */
2923 unsigned popcount = bitmap_count_bits (b);
2925 if (popcount > 0 && popcount < cls->members.length ())
2927 auto_vec <congruence_class *, 2> newclasses;
2928 newclasses.quick_push (new congruence_class (class_id++));
2929 newclasses.quick_push (new congruence_class (class_id++));
2931 for (unsigned int i = 0; i < cls->members.length (); i++)
2933 int target = bitmap_bit_p (b, i);
2934 congruence_class *tc = newclasses[target];
2936 add_item_to_class (tc, cls->members[i]);
2939 if (flag_checking)
2941 for (unsigned int i = 0; i < 2; i++)
2942 gcc_assert (newclasses[i]->members.length ());
2945 if (splitter_cls == cls)
2946 optimizer->splitter_class_removed = true;
2948 /* Remove old class from worklist if presented. */
2949 bool in_worklist = cls->in_worklist;
2951 if (in_worklist)
2952 cls->in_worklist = false;
2954 congruence_class_group g;
2955 g.hash = cls->members[0]->get_hash ();
2956 g.type = cls->members[0]->type;
2958 congruence_class_group *slot = optimizer->m_classes.find (&g);
2960 for (unsigned int i = 0; i < slot->classes.length (); i++)
2961 if (slot->classes[i] == cls)
2963 slot->classes.ordered_remove (i);
2964 break;
2967 /* New class will be inserted and integrated to work list. */
2968 for (unsigned int i = 0; i < 2; i++)
2969 optimizer->add_class (newclasses[i]);
2971 /* Two classes replace one, so that increment just by one. */
2972 optimizer->m_classes_count++;
2974 /* If OLD class was presented in the worklist, we remove the class
2975 and replace it will both newly created classes. */
2976 if (in_worklist)
2977 for (unsigned int i = 0; i < 2; i++)
2978 optimizer->worklist_push (newclasses[i]);
2979 else /* Just smaller class is inserted. */
2981 unsigned int smaller_index
2982 = (newclasses[0]->members.length ()
2983 < newclasses[1]->members.length ()
2984 ? 0 : 1);
2985 optimizer->worklist_push (newclasses[smaller_index]);
2988 if (dump_file && (dump_flags & TDF_DETAILS))
2990 fprintf (dump_file, " congruence class splitted:\n");
2991 cls->dump (dump_file, 4);
2993 fprintf (dump_file, " newly created groups:\n");
2994 for (unsigned int i = 0; i < 2; i++)
2995 newclasses[i]->dump (dump_file, 4);
2998 /* Release class if not presented in work list. */
2999 if (!in_worklist)
3000 delete cls;
3002 return true;
3005 return false;
3008 /* Compare function for sorting pairs in do_congruence_step_f. */
3011 sem_item_optimizer::sort_congruence_split (const void *a_, const void *b_)
3013 const std::pair<congruence_class *, bitmap> *a
3014 = (const std::pair<congruence_class *, bitmap> *)a_;
3015 const std::pair<congruence_class *, bitmap> *b
3016 = (const std::pair<congruence_class *, bitmap> *)b_;
3017 if (a->first->id < b->first->id)
3018 return -1;
3019 else if (a->first->id > b->first->id)
3020 return 1;
3021 return 0;
3024 /* Tests if a class CLS used as INDEXth splits any congruence classes.
3025 Bitmap stack BMSTACK is used for bitmap allocation. */
3027 bool
3028 sem_item_optimizer::do_congruence_step_for_index (congruence_class *cls,
3029 unsigned int index)
3031 hash_map <congruence_class *, bitmap> split_map;
3033 for (unsigned int i = 0; i < cls->members.length (); i++)
3035 sem_item *item = cls->members[i];
3036 sem_usage_pair needle (item, index);
3037 vec<sem_item *> *callers = m_references.get (&needle);
3038 if (callers == NULL)
3039 continue;
3041 for (unsigned int j = 0; j < callers->length (); j++)
3043 sem_item *caller = (*callers)[j];
3044 if (caller->cls->members.length () < 2)
3045 continue;
3046 bitmap *slot = split_map.get (caller->cls);
3047 bitmap b;
3049 if(!slot)
3051 b = BITMAP_ALLOC (&m_bmstack);
3052 split_map.put (caller->cls, b);
3054 else
3055 b = *slot;
3057 gcc_checking_assert (caller->cls);
3058 gcc_checking_assert (caller->index_in_class
3059 < caller->cls->members.length ());
3061 bitmap_set_bit (b, caller->index_in_class);
3065 auto_vec<std::pair<congruence_class *, bitmap> > to_split;
3066 to_split.reserve_exact (split_map.elements ());
3067 for (hash_map <congruence_class *, bitmap>::iterator i = split_map.begin ();
3068 i != split_map.end (); ++i)
3069 to_split.safe_push (*i);
3070 to_split.qsort (sort_congruence_split);
3072 traverse_split_pair pair;
3073 pair.optimizer = this;
3074 pair.cls = cls;
3076 splitter_class_removed = false;
3077 bool r = false;
3078 for (unsigned i = 0; i < to_split.length (); ++i)
3079 r |= traverse_congruence_split (to_split[i].first, to_split[i].second,
3080 &pair);
3082 /* Bitmap clean-up. */
3083 split_map.traverse <traverse_split_pair *,
3084 sem_item_optimizer::release_split_map> (NULL);
3086 return r;
3089 /* Every usage of a congruence class CLS is a candidate that can split the
3090 collection of classes. Bitmap stack BMSTACK is used for bitmap
3091 allocation. */
3093 void
3094 sem_item_optimizer::do_congruence_step (congruence_class *cls)
3096 bitmap_iterator bi;
3097 unsigned int i;
3099 bitmap usage = BITMAP_ALLOC (&m_bmstack);
3101 for (unsigned int i = 0; i < cls->members.length (); i++)
3102 bitmap_ior_into (usage, cls->members[i]->usage_index_bitmap);
3104 EXECUTE_IF_SET_IN_BITMAP (usage, 0, i, bi)
3106 if (dump_file && (dump_flags & TDF_DETAILS))
3107 fprintf (dump_file, " processing congruence step for class: %u "
3108 "(%u items, %u references), index: %u\n", cls->id,
3109 cls->referenced_by_count, cls->members.length (), i);
3110 do_congruence_step_for_index (cls, i);
3112 if (splitter_class_removed)
3113 break;
3116 BITMAP_FREE (usage);
3119 /* Adds a newly created congruence class CLS to worklist. */
3121 void
3122 sem_item_optimizer::worklist_push (congruence_class *cls)
3124 /* Return if the class CLS is already presented in work list. */
3125 if (cls->in_worklist)
3126 return;
3128 cls->in_worklist = true;
3129 worklist.insert (cls->referenced_by_count, cls);
3132 /* Pops a class from worklist. */
3134 congruence_class *
3135 sem_item_optimizer::worklist_pop (void)
3137 congruence_class *cls;
3139 while (!worklist.empty ())
3141 cls = worklist.extract_min ();
3142 if (cls->in_worklist)
3144 cls->in_worklist = false;
3146 return cls;
3148 else
3150 /* Work list item was already intended to be removed.
3151 The only reason for doing it is to split a class.
3152 Thus, the class CLS is deleted. */
3153 delete cls;
3157 return NULL;
3160 /* Iterative congruence reduction function. */
3162 void
3163 sem_item_optimizer::process_cong_reduction (void)
3165 for (hash_table<congruence_class_hash>::iterator it = m_classes.begin ();
3166 it != m_classes.end (); ++it)
3167 for (unsigned i = 0; i < (*it)->classes.length (); i++)
3168 if ((*it)->classes[i]->is_class_used ())
3169 worklist_push ((*it)->classes[i]);
3171 if (dump_file)
3172 fprintf (dump_file, "Worklist has been filled with: %lu\n",
3173 (unsigned long) worklist.nodes ());
3175 if (dump_file && (dump_flags & TDF_DETAILS))
3176 fprintf (dump_file, "Congruence class reduction\n");
3178 congruence_class *cls;
3180 /* Process complete congruence reduction. */
3181 while ((cls = worklist_pop ()) != NULL)
3182 do_congruence_step (cls);
3184 /* Subdivide newly created classes according to references. */
3185 unsigned new_classes = subdivide_classes_by_sensitive_refs ();
3187 if (dump_file)
3188 fprintf (dump_file, "Address reference subdivision created: %u "
3189 "new classes.\n", new_classes);
3192 /* Debug function prints all informations about congruence classes. */
3194 void
3195 sem_item_optimizer::dump_cong_classes (void)
3197 if (!dump_file)
3198 return;
3200 /* Histogram calculation. */
3201 unsigned int max_index = 0;
3202 unsigned int single_element_classes = 0;
3203 unsigned int* histogram = XCNEWVEC (unsigned int, m_items.length () + 1);
3205 for (hash_table<congruence_class_hash>::iterator it = m_classes.begin ();
3206 it != m_classes.end (); ++it)
3207 for (unsigned i = 0; i < (*it)->classes.length (); i++)
3209 unsigned int c = (*it)->classes[i]->members.length ();
3210 histogram[c]++;
3212 if (c > max_index)
3213 max_index = c;
3215 if (c == 1)
3216 ++single_element_classes;
3219 fprintf (dump_file,
3220 "Congruence classes: %lu with total: %u items (in a non-singular "
3221 "class: %u)\n", (unsigned long) m_classes.elements (),
3222 m_items.length (), m_items.length () - single_element_classes);
3223 fprintf (dump_file,
3224 "Class size histogram [number of members]: number of classes\n");
3225 for (unsigned int i = 0; i <= max_index; i++)
3226 if (histogram[i])
3227 fprintf (dump_file, "%6u: %6u\n", i, histogram[i]);
3229 if (dump_flags & TDF_DETAILS)
3230 for (hash_table<congruence_class_hash>::iterator it = m_classes.begin ();
3231 it != m_classes.end (); ++it)
3233 fprintf (dump_file, " group: with %u classes:\n",
3234 (*it)->classes.length ());
3236 for (unsigned i = 0; i < (*it)->classes.length (); i++)
3238 (*it)->classes[i]->dump (dump_file, 4);
3240 if (i < (*it)->classes.length () - 1)
3241 fprintf (dump_file, " ");
3245 free (histogram);
3248 /* Sort pair of sem_items A and B by DECL_UID. */
3250 static int
3251 sort_sem_items_by_decl_uid (const void *a, const void *b)
3253 const sem_item *i1 = *(const sem_item * const *)a;
3254 const sem_item *i2 = *(const sem_item * const *)b;
3256 int uid1 = DECL_UID (i1->decl);
3257 int uid2 = DECL_UID (i2->decl);
3258 return uid1 - uid2;
3261 /* Sort pair of congruence_classes A and B by DECL_UID of the first member. */
3263 static int
3264 sort_congruence_classes_by_decl_uid (const void *a, const void *b)
3266 const congruence_class *c1 = *(const congruence_class * const *)a;
3267 const congruence_class *c2 = *(const congruence_class * const *)b;
3269 int uid1 = DECL_UID (c1->members[0]->decl);
3270 int uid2 = DECL_UID (c2->members[0]->decl);
3271 return uid1 - uid2;
3274 /* Sort pair of congruence_class_groups A and B by
3275 DECL_UID of the first member of a first group. */
3277 static int
3278 sort_congruence_class_groups_by_decl_uid (const void *a, const void *b)
3280 const std::pair<congruence_class_group *, int> *g1
3281 = (const std::pair<congruence_class_group *, int> *) a;
3282 const std::pair<congruence_class_group *, int> *g2
3283 = (const std::pair<congruence_class_group *, int> *) b;
3284 return g1->second - g2->second;
3287 /* After reduction is done, we can declare all items in a group
3288 to be equal. PREV_CLASS_COUNT is start number of classes
3289 before reduction. True is returned if there's a merge operation
3290 processed. LOADED_SYMBOLS is number of symbols that were loaded
3291 in WPA. */
3293 bool
3294 sem_item_optimizer::merge_classes (unsigned int prev_class_count,
3295 unsigned int loaded_symbols)
3297 unsigned int item_count = m_items.length ();
3298 unsigned int class_count = m_classes_count;
3299 unsigned int equal_items = item_count - class_count;
3301 unsigned int non_singular_classes_count = 0;
3302 unsigned int non_singular_classes_sum = 0;
3304 bool merged_p = false;
3306 /* PR lto/78211
3307 Sort functions in congruence classes by DECL_UID and do the same
3308 for the classes to not to break -fcompare-debug. */
3310 for (hash_table<congruence_class_hash>::iterator it = m_classes.begin ();
3311 it != m_classes.end (); ++it)
3313 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
3315 congruence_class *c = (*it)->classes[i];
3316 c->members.qsort (sort_sem_items_by_decl_uid);
3319 (*it)->classes.qsort (sort_congruence_classes_by_decl_uid);
3322 for (hash_table<congruence_class_hash>::iterator it = m_classes.begin ();
3323 it != m_classes.end (); ++it)
3324 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
3326 congruence_class *c = (*it)->classes[i];
3327 if (c->members.length () > 1)
3329 non_singular_classes_count++;
3330 non_singular_classes_sum += c->members.length ();
3334 auto_vec<std::pair<congruence_class_group *, int> > classes (
3335 m_classes.elements ());
3336 for (hash_table<congruence_class_hash>::iterator it = m_classes.begin ();
3337 it != m_classes.end (); ++it)
3339 int uid = DECL_UID ((*it)->classes[0]->members[0]->decl);
3340 classes.quick_push (std::pair<congruence_class_group *, int> (*it, uid));
3343 classes.qsort (sort_congruence_class_groups_by_decl_uid);
3345 if (dump_file)
3347 fprintf (dump_file, "\nItem count: %u\n", item_count);
3348 fprintf (dump_file, "Congruent classes before: %u, after: %u\n",
3349 prev_class_count, class_count);
3350 fprintf (dump_file, "Average class size before: %.2f, after: %.2f\n",
3351 prev_class_count ? 1.0f * item_count / prev_class_count : 0.0f,
3352 class_count ? 1.0f * item_count / class_count : 0.0f);
3353 fprintf (dump_file, "Average non-singular class size: %.2f, count: %u\n",
3354 non_singular_classes_count ? 1.0f * non_singular_classes_sum /
3355 non_singular_classes_count : 0.0f,
3356 non_singular_classes_count);
3357 fprintf (dump_file, "Equal symbols: %u\n", equal_items);
3358 unsigned total = equal_items + non_singular_classes_count;
3359 fprintf (dump_file, "Totally needed symbols: %u"
3360 ", fraction of loaded symbols: %.2f%%\n\n", total,
3361 loaded_symbols ? 100.0f * total / loaded_symbols: 0.0f);
3364 unsigned int l;
3365 std::pair<congruence_class_group *, int> *it;
3366 FOR_EACH_VEC_ELT (classes, l, it)
3367 for (unsigned int i = 0; i < it->first->classes.length (); i++)
3369 congruence_class *c = it->first->classes[i];
3371 if (c->members.length () == 1)
3372 continue;
3374 sem_item *source = c->members[0];
3376 if (DECL_NAME (source->decl)
3377 && MAIN_NAME_P (DECL_NAME (source->decl)))
3378 /* If merge via wrappers, picking main as the target can be
3379 problematic. */
3380 source = c->members[1];
3382 for (unsigned int j = 0; j < c->members.length (); j++)
3384 sem_item *alias = c->members[j];
3386 if (alias == source)
3387 continue;
3389 dump_user_location_t loc
3390 = dump_user_location_t::from_function_decl (source->decl);
3391 if (dump_enabled_p ())
3393 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
3394 "Semantic equality hit:%s->%s\n",
3395 source->node->dump_name (),
3396 alias->node->dump_name ());
3397 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
3398 "Assembler symbol names:%s->%s\n",
3399 source->node->dump_asm_name (),
3400 alias->node->dump_asm_name ());
3403 if (lookup_attribute ("no_icf", DECL_ATTRIBUTES (alias->decl)))
3405 if (dump_enabled_p ())
3406 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
3407 "Merge operation is skipped due to no_icf "
3408 "attribute.\n");
3409 continue;
3412 if (dump_file && (dump_flags & TDF_DETAILS))
3414 source->dump_to_file (dump_file);
3415 alias->dump_to_file (dump_file);
3418 if (dbg_cnt (merged_ipa_icf))
3420 bool merged = source->merge (alias);
3421 merged_p |= merged;
3423 if (merged && alias->type == VAR)
3425 symtab_pair p = symtab_pair (source->node, alias->node);
3426 m_merged_variables.safe_push (p);
3432 if (!m_merged_variables.is_empty ())
3433 fixup_points_to_sets ();
3435 return merged_p;
3438 /* Fixup points to set PT. */
3440 void
3441 sem_item_optimizer::fixup_pt_set (struct pt_solution *pt)
3443 if (pt->vars == NULL)
3444 return;
3446 unsigned i;
3447 symtab_pair *item;
3448 FOR_EACH_VEC_ELT (m_merged_variables, i, item)
3449 if (bitmap_bit_p (pt->vars, DECL_UID (item->second->decl)))
3450 bitmap_set_bit (pt->vars, DECL_UID (item->first->decl));
3453 /* Set all points-to UIDs of aliases pointing to node N as UID. */
3455 static void
3456 set_alias_uids (symtab_node *n, int uid)
3458 ipa_ref *ref;
3459 FOR_EACH_ALIAS (n, ref)
3461 if (dump_file)
3462 fprintf (dump_file, " Setting points-to UID of [%s] as %d\n",
3463 ref->referring->dump_asm_name (), uid);
3465 SET_DECL_PT_UID (ref->referring->decl, uid);
3466 set_alias_uids (ref->referring, uid);
3470 /* Fixup points to analysis info. */
3472 void
3473 sem_item_optimizer::fixup_points_to_sets (void)
3475 /* TODO: remove in GCC 9 and trigger PTA re-creation after IPA passes. */
3476 cgraph_node *cnode;
3478 FOR_EACH_DEFINED_FUNCTION (cnode)
3480 tree name;
3481 unsigned i;
3482 function *fn = DECL_STRUCT_FUNCTION (cnode->decl);
3483 if (!gimple_in_ssa_p (fn))
3484 continue;
3486 FOR_EACH_SSA_NAME (i, name, fn)
3487 if (POINTER_TYPE_P (TREE_TYPE (name))
3488 && SSA_NAME_PTR_INFO (name))
3489 fixup_pt_set (&SSA_NAME_PTR_INFO (name)->pt);
3490 fixup_pt_set (&fn->gimple_df->escaped);
3492 /* The above gets us to 99% I guess, at least catching the
3493 address compares. Below also gets us aliasing correct
3494 but as said we're giving leeway to the situation with
3495 readonly vars anyway, so ... */
3496 basic_block bb;
3497 FOR_EACH_BB_FN (bb, fn)
3498 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
3499 gsi_next (&gsi))
3501 gcall *call = dyn_cast<gcall *> (gsi_stmt (gsi));
3502 if (call)
3504 fixup_pt_set (gimple_call_use_set (call));
3505 fixup_pt_set (gimple_call_clobber_set (call));
3510 unsigned i;
3511 symtab_pair *item;
3512 FOR_EACH_VEC_ELT (m_merged_variables, i, item)
3513 set_alias_uids (item->first, DECL_UID (item->first->decl));
3516 /* Dump function prints all class members to a FILE with an INDENT. */
3518 void
3519 congruence_class::dump (FILE *file, unsigned int indent) const
3521 FPRINTF_SPACES (file, indent, "class with id: %u, hash: %u, items: %u\n",
3522 id, members[0]->get_hash (), members.length ());
3524 FPUTS_SPACES (file, indent + 2, "");
3525 for (unsigned i = 0; i < members.length (); i++)
3526 fprintf (file, "%s ", members[i]->node->dump_asm_name ());
3528 fprintf (file, "\n");
3531 /* Returns true if there's a member that is used from another group. */
3533 bool
3534 congruence_class::is_class_used (void)
3536 for (unsigned int i = 0; i < members.length (); i++)
3537 if (members[i]->referenced_by_count)
3538 return true;
3540 return false;
3543 /* Generate pass summary for IPA ICF pass. */
3545 static void
3546 ipa_icf_generate_summary (void)
3548 if (!optimizer)
3549 optimizer = new sem_item_optimizer ();
3551 optimizer->register_hooks ();
3552 optimizer->parse_funcs_and_vars ();
3555 /* Write pass summary for IPA ICF pass. */
3557 static void
3558 ipa_icf_write_summary (void)
3560 gcc_assert (optimizer);
3562 optimizer->write_summary ();
3565 /* Read pass summary for IPA ICF pass. */
3567 static void
3568 ipa_icf_read_summary (void)
3570 if (!optimizer)
3571 optimizer = new sem_item_optimizer ();
3573 optimizer->read_summary ();
3574 optimizer->register_hooks ();
3577 /* Semantic equality execution function. */
3579 static unsigned int
3580 ipa_icf_driver (void)
3582 gcc_assert (optimizer);
3584 bool merged_p = optimizer->execute ();
3586 delete optimizer;
3587 optimizer = NULL;
3589 return merged_p ? TODO_remove_functions : 0;
3592 const pass_data pass_data_ipa_icf =
3594 IPA_PASS, /* type */
3595 "icf", /* name */
3596 OPTGROUP_IPA, /* optinfo_flags */
3597 TV_IPA_ICF, /* tv_id */
3598 0, /* properties_required */
3599 0, /* properties_provided */
3600 0, /* properties_destroyed */
3601 0, /* todo_flags_start */
3602 0, /* todo_flags_finish */
3605 class pass_ipa_icf : public ipa_opt_pass_d
3607 public:
3608 pass_ipa_icf (gcc::context *ctxt)
3609 : ipa_opt_pass_d (pass_data_ipa_icf, ctxt,
3610 ipa_icf_generate_summary, /* generate_summary */
3611 ipa_icf_write_summary, /* write_summary */
3612 ipa_icf_read_summary, /* read_summary */
3613 NULL, /*
3614 write_optimization_summary */
3615 NULL, /*
3616 read_optimization_summary */
3617 NULL, /* stmt_fixup */
3618 0, /* function_transform_todo_flags_start */
3619 NULL, /* function_transform */
3620 NULL) /* variable_transform */
3623 /* opt_pass methods: */
3624 virtual bool gate (function *)
3626 return in_lto_p || flag_ipa_icf_variables || flag_ipa_icf_functions;
3629 virtual unsigned int execute (function *)
3631 return ipa_icf_driver();
3633 }; // class pass_ipa_icf
3635 } // ipa_icf namespace
3637 ipa_opt_pass_d *
3638 make_pass_ipa_icf (gcc::context *ctxt)
3640 return new ipa_icf::pass_ipa_icf (ctxt);