LCOV - code coverage report
Current view: top level - gcc - ipa-icf.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 91.3 % 1789 1633
Test Date: 2026-12-05 16:10:02 Functions: 97.0 % 100 97
Legend: Lines:     hit not hit

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

Generated by: LCOV version 2.6-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.