LCOV - code coverage report
Current view: top level - gcc - ipa-cp.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 92.3 % 3206 2958
Test Date: 2026-05-30 15:37:04 Functions: 97.0 % 168 163
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Interprocedural constant propagation
       2              :    Copyright (C) 2005-2026 Free Software Foundation, Inc.
       3              : 
       4              :    Contributed by Razya Ladelsky <RAZYA@il.ibm.com> and Martin Jambor
       5              :    <mjambor@suse.cz>
       6              : 
       7              : This file is part of GCC.
       8              : 
       9              : GCC is free software; you can redistribute it and/or modify it under
      10              : the terms of the GNU General Public License as published by the Free
      11              : Software Foundation; either version 3, or (at your option) any later
      12              : version.
      13              : 
      14              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      15              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      16              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      17              : for more details.
      18              : 
      19              : You should have received a copy of the GNU General Public License
      20              : along with GCC; see the file COPYING3.  If not see
      21              : <http://www.gnu.org/licenses/>.  */
      22              : 
      23              : /* Interprocedural constant propagation (IPA-CP).
      24              : 
      25              :    The goal of this transformation is to
      26              : 
      27              :    1) discover functions which are always invoked with some arguments with the
      28              :       same known constant values and modify the functions so that the
      29              :       subsequent optimizations can take advantage of the knowledge, and
      30              : 
      31              :    2) partial specialization - create specialized versions of functions
      32              :       transformed in this way if some parameters are known constants only in
      33              :       certain contexts but the estimated tradeoff between speedup and cost size
      34              :       is deemed good.
      35              : 
      36              :    The algorithm also propagates types and attempts to perform type based
      37              :    devirtualization.  Types are propagated much like constants.
      38              : 
      39              :    The algorithm basically consists of three stages.  In the first, functions
      40              :    are analyzed one at a time and jump functions are constructed for all known
      41              :    call-sites.  In the second phase, the pass propagates information from the
      42              :    jump functions across the call to reveal what values are available at what
      43              :    call sites, performs estimations of effects of known values on functions and
      44              :    their callees, and finally decides what specialized extra versions should be
      45              :    created.  In the third, the special versions materialize and appropriate
      46              :    calls are redirected.
      47              : 
      48              :    The algorithm used is to a certain extent based on "Interprocedural Constant
      49              :    Propagation", by David Callahan, Keith D Cooper, Ken Kennedy, Linda Torczon,
      50              :    Comp86, pg 152-161 and "A Methodology for Procedure Cloning" by Keith D
      51              :    Cooper, Mary W. Hall, and Ken Kennedy.
      52              : 
      53              : 
      54              :    First stage - intraprocedural analysis
      55              :    =======================================
      56              : 
      57              :    This phase computes jump_function and modification flags.
      58              : 
      59              :    A jump function for a call-site represents the values passed as an actual
      60              :    arguments of a given call-site. In principle, there are three types of
      61              :    values:
      62              : 
      63              :    Pass through - the caller's formal parameter is passed as an actual
      64              :                   argument, plus an operation on it can be performed.
      65              :    Constant - a constant is passed as an actual argument.
      66              :    Unknown - neither of the above.
      67              : 
      68              :    All jump function types are described in detail in ipa-prop.h, together with
      69              :    the data structures that represent them and methods of accessing them.
      70              : 
      71              :    ipcp_generate_summary() is the main function of the first stage.
      72              : 
      73              :    Second stage - interprocedural analysis
      74              :    ========================================
      75              : 
      76              :    This stage is itself divided into two phases.  In the first, we propagate
      77              :    known values over the call graph, in the second, we make cloning decisions.
      78              :    It uses a different algorithm than the original Callahan's paper.
      79              : 
      80              :    First, we traverse the functions topologically from callers to callees and,
      81              :    for each strongly connected component (SCC), we propagate constants
      82              :    according to previously computed jump functions.  We also record what known
      83              :    values depend on other known values and estimate local effects.  Finally, we
      84              :    propagate cumulative information about these effects from dependent values
      85              :    to those on which they depend.
      86              : 
      87              :    Second, we again traverse the call graph in the same topological order and
      88              :    make clones for functions which we know are called with the same values in
      89              :    all contexts and decide about extra specialized clones of functions just for
      90              :    some contexts - these decisions are based on both local estimates and
      91              :    cumulative estimates propagated from callees.
      92              : 
      93              :    ipcp_propagate_stage() and ipcp_decision_stage() together constitute the
      94              :    third stage.
      95              : 
      96              :    Third phase - materialization of clones, call statement updates.
      97              :    ============================================
      98              : 
      99              :    This stage is currently performed by call graph code (mainly in cgraphunit.cc
     100              :    and tree-inline.cc) according to instructions inserted to the call graph by
     101              :    the second stage.  */
     102              : 
     103              : #define INCLUDE_ALGORITHM
     104              : #include "config.h"
     105              : #include "system.h"
     106              : #include "coretypes.h"
     107              : #include "backend.h"
     108              : #include "tree.h"
     109              : #include "gimple-expr.h"
     110              : #include "gimple.h"
     111              : #include "predict.h"
     112              : #include "sreal.h"
     113              : #include "alloc-pool.h"
     114              : #include "tree-pass.h"
     115              : #include "cgraph.h"
     116              : #include "diagnostic.h"
     117              : #include "fold-const.h"
     118              : #include "gimple-iterator.h"
     119              : #include "gimple-fold.h"
     120              : #include "symbol-summary.h"
     121              : #include "tree-vrp.h"
     122              : #include "ipa-cp.h"
     123              : #include "ipa-prop.h"
     124              : #include "tree-pretty-print.h"
     125              : #include "tree-inline.h"
     126              : #include "ipa-fnsummary.h"
     127              : #include "ipa-utils.h"
     128              : #include "tree-ssa-ccp.h"
     129              : #include "stringpool.h"
     130              : #include "attribs.h"
     131              : #include "dbgcnt.h"
     132              : #include "symtab-clones.h"
     133              : #include "gimple-range.h"
     134              : #include "attr-callback.h"
     135              : 
     136              : /* Allocation pools for values and their sources in ipa-cp.  */
     137              : 
     138              : object_allocator<ipcp_value<tree> > ipcp_cst_values_pool
     139              :   ("IPA-CP constant values");
     140              : 
     141              : object_allocator<ipcp_value<ipa_polymorphic_call_context> >
     142              :   ipcp_poly_ctx_values_pool ("IPA-CP polymorphic contexts");
     143              : 
     144              : object_allocator<ipcp_value_source<tree> > ipcp_sources_pool
     145              :   ("IPA-CP value sources");
     146              : 
     147              : object_allocator<ipcp_agg_lattice> ipcp_agg_lattice_pool
     148              :   ("IPA_CP aggregate lattices");
     149              : 
     150              : /* Original overall size of the program.  */
     151              : 
     152              : static long overall_size, orig_overall_size;
     153              : 
     154              : /* The maximum number of IPA-CP decision sweeps that any node requested in its
     155              :    param.  */
     156              : static int max_number_sweeps;
     157              : 
     158              : /* Node name to unique clone suffix number map.  */
     159              : static hash_map<const char *, unsigned> *clone_num_suffixes;
     160              : 
     161              : /* Return the param lattices structure corresponding to the Ith formal
     162              :    parameter of the function described by INFO.  */
     163              : static inline class ipcp_param_lattices *
     164     33647381 : ipa_get_parm_lattices (class ipa_node_params *info, int i)
     165              : {
     166     67294762 :   gcc_assert (i >= 0 && i < ipa_get_param_count (info));
     167     33647381 :   gcc_checking_assert (!info->ipcp_orig_node);
     168     33647381 :   return &(info->lattices[i]);
     169              : }
     170              : 
     171              : /* Return the lattice corresponding to the scalar value of the Ith formal
     172              :    parameter of the function described by INFO.  */
     173              : static inline ipcp_lattice<tree> *
     174      5935509 : ipa_get_scalar_lat (class ipa_node_params *info, int i)
     175              : {
     176      6120034 :   class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
     177      5935509 :   return &plats->itself;
     178              : }
     179              : 
     180              : /* Return the lattice corresponding to the scalar value of the Ith formal
     181              :    parameter of the function described by INFO.  */
     182              : static inline ipcp_lattice<ipa_polymorphic_call_context> *
     183       798013 : ipa_get_poly_ctx_lat (class ipa_node_params *info, int i)
     184              : {
     185       798013 :   class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
     186       798013 :   return &plats->ctxlat;
     187              : }
     188              : 
     189              : /* Return whether LAT is a lattice with a single constant and without an
     190              :    undefined value.  */
     191              : 
     192              : template <typename valtype>
     193              : inline bool
     194     15057625 : ipcp_lattice<valtype>::is_single_const ()
     195              : {
     196      3225947 :   if (bottom || contains_variable || values_count != 1)
     197              :     return false;
     198              :   else
     199              :     return true;
     200              : }
     201              : 
     202              : /* Return true iff X and Y should be considered equal values by IPA-CP.  */
     203              : 
     204              : bool
     205      1400573 : values_equal_for_ipcp_p (tree x, tree y)
     206              : {
     207      1400573 :   gcc_checking_assert (x != NULL_TREE && y != NULL_TREE);
     208              : 
     209      1400573 :   if (x == y)
     210              :     return true;
     211              : 
     212       621625 :   if (TREE_CODE (x) == ADDR_EXPR
     213       221300 :       && TREE_CODE (y) == ADDR_EXPR
     214       220668 :       && (TREE_CODE (TREE_OPERAND (x, 0)) == CONST_DECL
     215       173295 :           || (TREE_CODE (TREE_OPERAND (x, 0)) == VAR_DECL
     216        92356 :               && DECL_IN_CONSTANT_POOL (TREE_OPERAND (x, 0))))
     217       668998 :       && (TREE_CODE (TREE_OPERAND (y, 0)) == CONST_DECL
     218           13 :           || (TREE_CODE (TREE_OPERAND (y, 0)) == VAR_DECL
     219            8 :               && DECL_IN_CONSTANT_POOL (TREE_OPERAND (y, 0)))))
     220        47360 :     return TREE_OPERAND (x, 0) == TREE_OPERAND (y, 0)
     221        94464 :            || operand_equal_p (DECL_INITIAL (TREE_OPERAND (x, 0)),
     222        47104 :                                DECL_INITIAL (TREE_OPERAND (y, 0)), 0);
     223              :   else
     224       574265 :     return operand_equal_p (x, y, 0);
     225              : }
     226              : 
     227              : /* Print V which is extracted from a value in a lattice to F.  This overloaded
     228              :    function is used to print tree constants.  */
     229              : 
     230              : static void
     231          715 : print_ipcp_constant_value (FILE * f, tree v)
     232              : {
     233            0 :   ipa_print_constant_value (f, v);
     234           44 : }
     235              : 
     236              : /* Print V which is extracted from a value in a lattice to F.  This overloaded
     237              :    function is used to print constant polymorphic call contexts.  */
     238              : 
     239              : static void
     240          214 : print_ipcp_constant_value (FILE * f, ipa_polymorphic_call_context v)
     241              : {
     242          214 :   v.dump(f, false);
     243            0 : }
     244              : 
     245              : /* Print a lattice LAT to F.  */
     246              : 
     247              : template <typename valtype>
     248              : void
     249         1985 : ipcp_lattice<valtype>::print (FILE * f, bool dump_sources, bool dump_benefits)
     250              : {
     251              :   ipcp_value<valtype> *val;
     252         1985 :   bool prev = false;
     253              : 
     254         1985 :   if (bottom)
     255              :     {
     256          844 :       fprintf (f, "BOTTOM\n");
     257          844 :       return;
     258              :     }
     259              : 
     260         1141 :   if (!values_count && !contains_variable)
     261              :     {
     262            0 :       fprintf (f, "TOP\n");
     263            0 :       return;
     264              :     }
     265              : 
     266         1141 :   if (contains_variable)
     267              :     {
     268          861 :       fprintf (f, "VARIABLE");
     269          861 :       prev = true;
     270          861 :       if (dump_benefits)
     271          861 :         fprintf (f, "\n");
     272              :     }
     273              : 
     274         1770 :   for (val = values; val; val = val->next)
     275              :     {
     276          629 :       if (dump_benefits && prev)
     277          349 :         fprintf (f, "               ");
     278          280 :       else if (!dump_benefits && prev)
     279            0 :         fprintf (f, ", ");
     280              :       else
     281              :         prev = true;
     282              : 
     283          629 :       print_ipcp_constant_value (f, val->value);
     284              : 
     285          629 :       if (dump_sources)
     286              :         {
     287              :           ipcp_value_source<valtype> *s;
     288              : 
     289          174 :           if (val->self_recursion_generated_p ())
     290           27 :             fprintf (f, " [self_gen(%i), from:",
     291              :                      val->self_recursion_generated_level);
     292              :           else
     293          147 :             fprintf (f, " [scc: %i, from:", val->scc_no);
     294          366 :           for (s = val->sources; s; s = s->next)
     295          192 :             fprintf (f, " %i(%f)", s->cs->caller->get_uid (),
     296          384 :                      s->cs->sreal_frequency ().to_double ());
     297          174 :           fprintf (f, "]");
     298              :         }
     299              : 
     300          629 :       if (dump_benefits)
     301          629 :         fprintf (f, " [loc_time: %g, loc_size: %i, "
     302              :                  "prop_time: %g, prop_size: %i]\n",
     303              :                  val->local_time_benefit.to_double (), val->local_size_cost,
     304              :                  val->prop_time_benefit.to_double (), val->prop_size_cost);
     305              :     }
     306         1141 :   if (!dump_benefits)
     307            0 :     fprintf (f, "\n");
     308              : }
     309              : 
     310              : /* Print VALUE to F in a form which in usual cases does not take thousands of
     311              :    characters. */
     312              : 
     313              : static void
     314         1450 : ipcp_print_widest_int (FILE *f, const widest_int &value)
     315              : {
     316         1450 :   if (value == -1)
     317            0 :     fprintf (f, "-1");
     318         1450 :   else if (wi::arshift (value, 128) == -1)
     319              :     {
     320          330 :       char buf[35], *p = buf + 2;
     321          330 :       widest_int v = wi::zext (value, 128);
     322          330 :       size_t len;
     323          330 :       print_hex (v, buf);
     324          330 :       len = strlen (p);
     325          330 :       if (len == 32)
     326              :         {
     327          330 :           fprintf (f, "0xf..f");
     328         9795 :           while (*p == 'f')
     329         9135 :             ++p;
     330              :         }
     331              :       else
     332            0 :         fprintf (f, "0xf..f%0*d", (int) (32 - len), 0);
     333          330 :       fputs (p, f);
     334          330 :     }
     335              :   else
     336         1120 :     print_hex (value, f);
     337         1450 : }
     338              : 
     339              : void
     340          917 : ipcp_bits_lattice::print (FILE *f)
     341              : {
     342          917 :   if (bottom_p ())
     343              :     {
     344          607 :       fprintf (f, "         Bits unusable (BOTTOM)\n");
     345          607 :       return;
     346              :     }
     347              : 
     348          310 :   if (top_p ())
     349            0 :     fprintf (f, "         Bits unknown (TOP)");
     350              :   else
     351              :     {
     352          310 :       fprintf (f, "         Bits: value = ");
     353          310 :       ipcp_print_widest_int (f, get_value ());
     354          310 :       fprintf (f, ", mask = ");
     355          310 :       ipcp_print_widest_int (f, get_mask ());
     356              :     }
     357              : 
     358          310 :   if (m_recipient_only)
     359          136 :     fprintf (f, " (recipient only)");
     360          310 :   fprintf (f, "\n");
     361              : }
     362              : 
     363              : /* Print value range lattice to F.  */
     364              : 
     365              : void
     366          917 : ipcp_vr_lattice::print (FILE * f)
     367              : {
     368          917 :   if (m_recipient_only)
     369          263 :     fprintf (f, "(recipient only) ");
     370          917 :   m_vr.dump (f);
     371          917 : }
     372              : 
     373              : /* Print all ipcp_lattices of all functions to F.  */
     374              : 
     375              : static void
     376          161 : print_all_lattices (FILE * f, bool dump_sources, bool dump_benefits)
     377              : {
     378          161 :   struct cgraph_node *node;
     379          161 :   int i, count;
     380              : 
     381          161 :   fprintf (f, "\nLattices:\n");
     382          889 :   FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
     383              :     {
     384          728 :       class ipa_node_params *info;
     385              : 
     386          728 :       info = ipa_node_params_sum->get (node);
     387              :       /* Skip unoptimized functions and constprop clones since we don't make
     388              :          lattices for them.  */
     389          728 :       if (!info || info->ipcp_orig_node)
     390            0 :         continue;
     391          728 :       fprintf (f, "  Node: %s:\n", node->dump_name ());
     392          728 :       count = ipa_get_param_count (info);
     393         1645 :       for (i = 0; i < count; i++)
     394              :         {
     395          917 :           struct ipcp_agg_lattice *aglat;
     396          917 :           class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
     397          917 :           fprintf (f, "    param [%d]: ", i);
     398          917 :           plats->itself.print (f, dump_sources, dump_benefits);
     399          917 :           fprintf (f, "         ctxs: ");
     400          917 :           plats->ctxlat.print (f, dump_sources, dump_benefits);
     401          917 :           plats->bits_lattice.print (f);
     402          917 :           fprintf (f, "         ");
     403          917 :           plats->m_value_range.print (f);
     404          917 :           fprintf (f, "\n");
     405          917 :           if (plats->virt_call)
     406           75 :             fprintf (f, "        virt_call flag set\n");
     407              : 
     408          917 :           if (plats->aggs_bottom)
     409              :             {
     410          442 :               fprintf (f, "        AGGS BOTTOM\n");
     411          442 :               continue;
     412              :             }
     413          475 :           if (plats->aggs_contain_variable)
     414          437 :             fprintf (f, "        AGGS VARIABLE\n");
     415          626 :           for (aglat = plats->aggs; aglat; aglat = aglat->next)
     416              :             {
     417          151 :               fprintf (f, "        %soffset " HOST_WIDE_INT_PRINT_DEC ": ",
     418          151 :                        plats->aggs_by_ref ? "ref " : "", aglat->offset);
     419          151 :               aglat->print (f, dump_sources, dump_benefits);
     420              :             }
     421              :         }
     422              :     }
     423          161 : }
     424              : 
     425              : /* Determine whether it is at all technically possible to create clones of NODE
     426              :    and store this information in the ipa_node_params structure associated
     427              :    with NODE.  */
     428              : 
     429              : static void
     430      1269638 : determine_versionability (struct cgraph_node *node,
     431              :                           class ipa_node_params *info)
     432              : {
     433      1269638 :   const char *reason = NULL;
     434              : 
     435              :   /* There are a number of generic reasons functions cannot be versioned.  We
     436              :      also cannot remove parameters if there are type attributes such as fnspec
     437              :      present.  */
     438      1269638 :   if (node->alias || node->thunk)
     439              :     reason = "alias or thunk";
     440      1269638 :   else if (!node->versionable)
     441              :     reason = "not a tree_versionable_function";
     442      1140708 :   else if (node->get_availability () <= AVAIL_INTERPOSABLE)
     443              :     reason = "insufficient body availability";
     444      1073723 :   else if (!opt_for_fn (node->decl, optimize)
     445      1073723 :            || !opt_for_fn (node->decl, flag_ipa_cp))
     446              :     reason = "non-optimized function";
     447      1073723 :   else if (lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (node->decl)))
     448              :     {
     449              :       /* Ideally we should clone the SIMD clones themselves and create
     450              :          vector copies of them, so IPA-cp and SIMD clones can happily
     451              :          coexist, but that may not be worth the effort.  */
     452              :       reason = "function has SIMD clones";
     453              :     }
     454      1073363 :   else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (node->decl)))
     455              :     {
     456              :       /* Ideally we should clone the target clones themselves and create
     457              :          copies of them, so IPA-cp and target clones can happily
     458              :          coexist, but that may not be worth the effort.  */
     459              :       reason = "function target_clones attribute";
     460              :     }
     461              :   /* Don't clone decls local to a comdat group; it breaks and for C++
     462              :      decloned constructors, inlining is always better anyway.  */
     463      1073363 :   else if (node->comdat_local_p ())
     464              :     reason = "comdat-local function";
     465      1071208 :   else if (node->calls_comdat_local)
     466              :     {
     467              :       /* TODO: call is versionable if we make sure that all
     468              :          callers are inside of a comdat group.  */
     469         2257 :       reason = "calls comdat-local function";
     470              :     }
     471              : 
     472              :   /* Functions calling BUILT_IN_VA_ARG_PACK and BUILT_IN_VA_ARG_PACK_LEN
     473              :      work only when inlined.  Cloning them may still lead to better code
     474              :      because ipa-cp will not give up on cloning further.  If the function is
     475              :      external this however leads to wrong code because we may end up producing
     476              :      offline copy of the function.  */
     477      1269638 :   if (DECL_EXTERNAL (node->decl))
     478       175471 :     for (cgraph_edge *edge = node->callees; !reason && edge;
     479       129050 :          edge = edge->next_callee)
     480       129050 :       if (fndecl_built_in_p (edge->callee->decl, BUILT_IN_NORMAL))
     481              :         {
     482        36366 :           if (DECL_FUNCTION_CODE (edge->callee->decl) == BUILT_IN_VA_ARG_PACK)
     483            0 :             reason = "external function which calls va_arg_pack";
     484        36366 :           if (DECL_FUNCTION_CODE (edge->callee->decl)
     485              :               == BUILT_IN_VA_ARG_PACK_LEN)
     486            0 :             reason = "external function which calls va_arg_pack_len";
     487              :         }
     488              : 
     489      1269638 :   if (reason && dump_file && !node->alias && !node->thunk)
     490           56 :     fprintf (dump_file, "Function %s is not versionable, reason: %s.\n",
     491              :              node->dump_name (), reason);
     492              : 
     493      1269638 :   info->versionable = (reason == NULL);
     494      1269638 : }
     495              : 
     496              : /* Return true if it is at all technically possible to create clones of a
     497              :    NODE.  */
     498              : 
     499              : static bool
     500      6079596 : ipcp_versionable_function_p (struct cgraph_node *node)
     501              : {
     502      6079596 :   ipa_node_params *info = ipa_node_params_sum->get (node);
     503      6079596 :   return info && info->versionable;
     504              : }
     505              : 
     506              : /* Structure holding accumulated information about callers of a node.  */
     507              : 
     508      3405795 : struct caller_statistics
     509              : {
     510              :   /* If requested (see below), self-recursive call counts are summed into this
     511              :      field.  */
     512              :   profile_count rec_count_sum;
     513              :   /* The sum of all ipa counts of all the other (non-recursive) calls.  */
     514              :   profile_count count_sum;
     515              :   /* Sum of all frequencies for all calls.  */
     516              :   sreal freq_sum;
     517              :   /* Number of calls and calls considered interesting respectively.  */
     518              :   int n_calls, n_interesting_calls;
     519              :   /* If itself is set up, also count the number of non-self-recursive
     520              :      calls.  */
     521              :   int n_nonrec_calls;
     522              :   /* If non-NULL, this is the node itself and calls from it should have their
     523              :      counts included in rec_count_sum and not count_sum.  */
     524              :   cgraph_node *itself;
     525              :   /* True if there is a caller that has no IPA profile.  */
     526              :   bool called_without_ipa_profile;
     527              : };
     528              : 
     529              : /* Initialize fields of STAT to zeroes and optionally set it up so that edges
     530              :    from IGNORED_CALLER are not counted.  */
     531              : 
     532              : static inline void
     533      2660995 : init_caller_stats (caller_statistics *stats, cgraph_node *itself = NULL)
     534              : {
     535      2660995 :   stats->rec_count_sum = profile_count::zero ();
     536      2660995 :   stats->count_sum = profile_count::zero ();
     537      2660995 :   stats->n_calls = 0;
     538      2660995 :   stats->n_interesting_calls = 0;
     539      2660995 :   stats->n_nonrec_calls = 0;
     540      2660995 :   stats->freq_sum = 0;
     541      2660995 :   stats->itself = itself;
     542      2660995 :   stats->called_without_ipa_profile = false;
     543      2660995 : }
     544              : 
     545              : /* We want to propagate across edges that may be executed, however
     546              :    we do not want to check maybe_hot, since call itself may be cold
     547              :    while calee contains some heavy loop which makes propagation still
     548              :    relevant.
     549              : 
     550              :    In particular, even edge called once may lead to significant
     551              :    improvement.  */
     552              : 
     553              : static bool
     554      4715177 : cs_interesting_for_ipcp_p (cgraph_edge *e)
     555              : {
     556              :   /* If profile says the edge is executed, we want to optimize.  */
     557      4715177 :   if (e->count.ipa ().nonzero_p ())
     558          899 :     return true;
     559              :   /* If local (possibly guseed or adjusted 0 profile) claims edge is
     560              :      not executed, do not propagate.
     561              :      Do not trust AFDO since branch needs to be executed multiple
     562              :      time to count while we want to propagate even call called
     563              :      once during the train run if callee is important.  */
     564      4714278 :   if (e->count.initialized_p () && !e->count.nonzero_p ()
     565      5381403 :       && e->count.quality () != AFDO)
     566              :     return false;
     567              :   /* If we have zero IPA profile, still consider edge for cloning
     568              :      in case we do partial training.  */
     569      4047153 :   if (e->count.ipa ().initialized_p ()
     570      4047153 :       && e->count.ipa ().quality () != AFDO
     571      4047168 :       && !opt_for_fn (e->callee->decl,flag_profile_partial_training))
     572           15 :     return false;
     573              :   return true;
     574              : }
     575              : 
     576              : /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
     577              :    non-thunk incoming edges to NODE.  */
     578              : 
     579              : static bool
     580      2837205 : gather_caller_stats (struct cgraph_node *node, void *data)
     581              : {
     582      2837205 :   struct caller_statistics *stats = (struct caller_statistics *) data;
     583      2837205 :   struct cgraph_edge *cs;
     584              : 
     585      7344420 :   for (cs = node->callers; cs; cs = cs->next_caller)
     586      4507215 :     if (!cs->caller->thunk)
     587              :       {
     588      4503714 :         ipa_node_params *info = ipa_node_params_sum->get (cs->caller);
     589      4503714 :         if (info && info->node_dead)
     590       162036 :           continue;
     591              : 
     592      4341678 :         if (cs->count.ipa ().initialized_p ())
     593              :           {
     594       336320 :             if (stats->itself && stats->itself == cs->caller)
     595            0 :               stats->rec_count_sum += cs->count.ipa ();
     596              :             else
     597       336320 :               stats->count_sum += cs->count.ipa ();
     598              :           }
     599              :         else
     600      4005358 :           stats->called_without_ipa_profile = true;
     601      4341678 :         stats->freq_sum += cs->sreal_frequency ();
     602      4341678 :         stats->n_calls++;
     603      4341678 :         if (stats->itself && stats->itself != cs->caller)
     604            8 :           stats->n_nonrec_calls++;
     605              : 
     606              :         /* If profile known to be zero, we do not want to clone for performance.
     607              :            However if call is cold, the called function may still contain
     608              :            important hot loops.  */
     609      4341678 :         if (cs_interesting_for_ipcp_p (cs))
     610      3719930 :           stats->n_interesting_calls++;
     611              :       }
     612      2837205 :   return false;
     613              : 
     614              : }
     615              : 
     616              : /* Return true if this NODE is viable candidate for cloning.  */
     617              : 
     618              : static bool
     619       791857 : ipcp_cloning_candidate_p (struct cgraph_node *node)
     620              : {
     621       791857 :   struct caller_statistics stats;
     622              : 
     623       791857 :   gcc_checking_assert (node->has_gimple_body_p ());
     624              : 
     625       791857 :   if (!opt_for_fn (node->decl, flag_ipa_cp_clone))
     626              :     {
     627       740621 :       if (dump_file)
     628           31 :         fprintf (dump_file, "Not considering %s for cloning; "
     629              :                  "-fipa-cp-clone disabled.\n",
     630              :                  node->dump_name ());
     631       740621 :       return false;
     632              :     }
     633              : 
     634              :   /* Do not use profile here since cold wrapper wrap
     635              :      hot function.  */
     636        51236 :   if (opt_for_fn (node->decl, optimize_size))
     637              :     {
     638           10 :       if (dump_file)
     639            0 :         fprintf (dump_file, "Not considering %s for cloning; "
     640              :                  "optimizing it for size.\n",
     641              :                  node->dump_name ());
     642           10 :       return false;
     643              :     }
     644              : 
     645        51226 :   init_caller_stats (&stats);
     646        51226 :   node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats, false);
     647              : 
     648        51226 :   if (ipa_size_summaries->get (node)->self_size < stats.n_calls)
     649              :     {
     650          303 :       if (dump_file)
     651            0 :         fprintf (dump_file, "Considering %s for cloning; code might shrink.\n",
     652              :                  node->dump_name ());
     653          303 :       return true;
     654              :     }
     655        50923 :   if (!stats.n_interesting_calls)
     656              :     {
     657        39062 :       if (dump_file)
     658          201 :         fprintf (dump_file, "Not considering %s for cloning; "
     659              :                  "no calls considered interesting by profile.\n",
     660              :                  node->dump_name ());
     661        39062 :       return false;
     662              :     }
     663        11861 :   if (dump_file)
     664          186 :     fprintf (dump_file, "Considering %s for cloning.\n",
     665              :              node->dump_name ());
     666              :   return true;
     667              : }
     668              : 
     669              : template <typename valtype>
     670              : class value_topo_info
     671              : {
     672              : public:
     673              :   /* Head of the linked list of topologically sorted values. */
     674              :   ipcp_value<valtype> *values_topo;
     675              :   /* Stack for creating SCCs, represented by a linked list too.  */
     676              :   ipcp_value<valtype> *stack;
     677              :   /* Counter driving the algorithm in add_val_to_toposort.  */
     678              :   int dfs_counter;
     679              : 
     680       129027 :   value_topo_info () : values_topo (NULL), stack (NULL), dfs_counter (0)
     681              :   {}
     682              :   void add_val (ipcp_value<valtype> *cur_val);
     683              :   void propagate_effects ();
     684              : };
     685              : 
     686              : /* Arrays representing a topological ordering of call graph nodes and a stack
     687              :    of nodes used during constant propagation and also data required to perform
     688              :    topological sort of values and propagation of benefits in the determined
     689              :    order.  */
     690              : 
     691              : class ipa_topo_info
     692              : {
     693              : public:
     694              :   /* Array with obtained topological order of cgraph nodes.  */
     695              :   struct cgraph_node **order;
     696              :   /* Stack of cgraph nodes used during propagation within SCC until all values
     697              :      in the SCC stabilize.  */
     698              :   struct cgraph_node **stack;
     699              :   int nnodes, stack_top;
     700              : 
     701              :   value_topo_info<tree> constants;
     702              :   value_topo_info<ipa_polymorphic_call_context> contexts;
     703              : 
     704       129027 :   ipa_topo_info () : order(NULL), stack(NULL), nnodes(0), stack_top(0),
     705       129027 :     constants ()
     706              :   {}
     707              : };
     708              : 
     709              : /* Skip edges from and to nodes without ipa_cp enabled.
     710              :    Ignore not available symbols.  */
     711              : 
     712              : static bool
     713      5269093 : ignore_edge_p (cgraph_edge *e)
     714              : {
     715      5269093 :   enum availability avail;
     716      5269093 :   cgraph_node *ultimate_target
     717      5269093 :     = e->callee->function_or_virtual_thunk_symbol (&avail, e->caller);
     718              : 
     719      5269093 :   return (avail <= AVAIL_INTERPOSABLE
     720      1857677 :           || !opt_for_fn (ultimate_target->decl, optimize)
     721      7118044 :           || !opt_for_fn (ultimate_target->decl, flag_ipa_cp));
     722              : }
     723              : 
     724              : /* Allocate the arrays in TOPO and topologically sort the nodes into order.  */
     725              : 
     726              : static void
     727       129027 : build_toporder_info (class ipa_topo_info *topo)
     728              : {
     729       129027 :   topo->order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
     730       129027 :   topo->stack = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
     731              : 
     732       129027 :   gcc_checking_assert (topo->stack_top == 0);
     733       129027 :   topo->nnodes = ipa_reduced_postorder (topo->order, true,
     734              :                                         ignore_edge_p);
     735       129027 : }
     736              : 
     737              : /* Free information about strongly connected components and the arrays in
     738              :    TOPO.  */
     739              : 
     740              : static void
     741       129027 : free_toporder_info (class ipa_topo_info *topo)
     742              : {
     743       129027 :   ipa_free_postorder_info ();
     744       129027 :   free (topo->order);
     745       129027 :   free (topo->stack);
     746       129027 : }
     747              : 
     748              : /* Add NODE to the stack in TOPO, unless it is already there.  */
     749              : 
     750              : static inline void
     751      1273791 : push_node_to_stack (class ipa_topo_info *topo, struct cgraph_node *node)
     752              : {
     753      1273791 :   ipa_node_params *info = ipa_node_params_sum->get (node);
     754      1273791 :   if (info->node_enqueued)
     755              :     return;
     756      1272822 :   info->node_enqueued = 1;
     757      1272822 :   topo->stack[topo->stack_top++] = node;
     758              : }
     759              : 
     760              : /* Pop a node from the stack in TOPO and return it or return NULL if the stack
     761              :    is empty.  */
     762              : 
     763              : static struct cgraph_node *
     764      2623424 : pop_node_from_stack (class ipa_topo_info *topo)
     765              : {
     766      2623424 :   if (topo->stack_top)
     767              :     {
     768      1272822 :       struct cgraph_node *node;
     769      1272822 :       topo->stack_top--;
     770      1272822 :       node = topo->stack[topo->stack_top];
     771      1272822 :       ipa_node_params_sum->get (node)->node_enqueued = 0;
     772      1272822 :       return node;
     773              :     }
     774              :   else
     775              :     return NULL;
     776              : }
     777              : 
     778              : /* Set lattice LAT to bottom and return true if it previously was not set as
     779              :    such.  */
     780              : 
     781              : template <typename valtype>
     782              : inline bool
     783      2112135 : ipcp_lattice<valtype>::set_to_bottom ()
     784              : {
     785      2112135 :   bool ret = !bottom;
     786      2112135 :   bottom = true;
     787              :   return ret;
     788              : }
     789              : 
     790              : /* Mark lattice as containing an unknown value and return true if it previously
     791              :    was not marked as such.  */
     792              : 
     793              : template <typename valtype>
     794              : inline bool
     795      1539476 : ipcp_lattice<valtype>::set_contains_variable ()
     796              : {
     797      1539476 :   bool ret = !contains_variable;
     798      1539476 :   contains_variable = true;
     799              :   return ret;
     800              : }
     801              : 
     802              : /* Set all aggregate lattices in PLATS to bottom and return true if they were
     803              :    not previously set as such.  */
     804              : 
     805              : static inline bool
     806      2111842 : set_agg_lats_to_bottom (class ipcp_param_lattices *plats)
     807              : {
     808      2111842 :   bool ret = !plats->aggs_bottom;
     809      2111842 :   plats->aggs_bottom = true;
     810      2111842 :   return ret;
     811              : }
     812              : 
     813              : /* Mark all aggregate lattices in PLATS as containing an unknown value and
     814              :    return true if they were not previously marked as such.  */
     815              : 
     816              : static inline bool
     817      1043895 : set_agg_lats_contain_variable (class ipcp_param_lattices *plats)
     818              : {
     819      1043895 :   bool ret = !plats->aggs_contain_variable;
     820      1043895 :   plats->aggs_contain_variable = true;
     821      1043895 :   return ret;
     822              : }
     823              : 
     824              : bool
     825            0 : ipcp_vr_lattice::meet_with (const ipcp_vr_lattice &other)
     826              : {
     827            0 :   return meet_with_1 (other.m_vr);
     828              : }
     829              : 
     830              : /* Meet the current value of the lattice with the range described by
     831              :    P_VR.  */
     832              : 
     833              : bool
     834       491549 : ipcp_vr_lattice::meet_with (const vrange &p_vr)
     835              : {
     836       491549 :   return meet_with_1 (p_vr);
     837              : }
     838              : 
     839              : /* Meet the current value of the lattice with the range described by
     840              :    OTHER_VR.  Return TRUE if anything changed.  */
     841              : 
     842              : bool
     843       491549 : ipcp_vr_lattice::meet_with_1 (const vrange &other_vr)
     844              : {
     845       491549 :   if (bottom_p ())
     846              :     return false;
     847              : 
     848       491549 :   if (other_vr.varying_p ())
     849            0 :     return set_to_bottom ();
     850              : 
     851       491549 :   bool res;
     852       491549 :   if (flag_checking)
     853              :     {
     854       491549 :       value_range save (m_vr);
     855       491549 :       res = m_vr.union_ (other_vr);
     856       491549 :       gcc_assert (res == (m_vr != save));
     857       491549 :     }
     858              :   else
     859            0 :     res = m_vr.union_ (other_vr);
     860              :   return res;
     861              : }
     862              : 
     863              : /* Return true if value range information in the lattice is yet unknown.  */
     864              : 
     865              : bool
     866              : ipcp_vr_lattice::top_p () const
     867              : {
     868       167545 :   return m_vr.undefined_p ();
     869              : }
     870              : 
     871              : /* Return true if value range information in the lattice is known to be
     872              :    unusable.  */
     873              : 
     874              : bool
     875      4833372 : ipcp_vr_lattice::bottom_p () const
     876              : {
     877       491549 :   return m_vr.varying_p ();
     878              : }
     879              : 
     880              : /* Set value range information in the lattice to bottom.  Return true if it
     881              :    previously was in a different state.  */
     882              : 
     883              : bool
     884      2385480 : ipcp_vr_lattice::set_to_bottom ()
     885              : {
     886      2385480 :   if (m_vr.varying_p ())
     887              :     return false;
     888              : 
     889              :   /* Setting an unsupported type here forces the temporary to default
     890              :      to unsupported_range, which can handle VARYING/DEFINED ranges,
     891              :      but nothing else (union, intersect, etc).  This allows us to set
     892              :      bottoms on any ranges, and is safe as all users of the lattice
     893              :      check for bottom first.  */
     894      2244452 :   m_vr.set_range_class (void_type_node);
     895      2244452 :   m_vr.set_varying (void_type_node);
     896              : 
     897      2244452 :   return true;
     898              : }
     899              : 
     900              : /* Set the flag that this lattice is a recipient only, return true if it was
     901              :    not set before.  */
     902              : 
     903              : bool
     904        29238 : ipcp_vr_lattice::set_recipient_only ()
     905              : {
     906        29238 :   if (m_recipient_only)
     907              :     return false;
     908        29238 :   m_recipient_only = true;
     909        29238 :   return true;
     910              : }
     911              : 
     912              : /* Set lattice value to bottom, if it already isn't the case.  */
     913              : 
     914              : bool
     915      2404562 : ipcp_bits_lattice::set_to_bottom ()
     916              : {
     917      2404562 :   if (bottom_p ())
     918              :     return false;
     919      2264062 :   m_lattice_val = IPA_BITS_VARYING;
     920      2264062 :   m_value = 0;
     921      2264062 :   m_mask = -1;
     922      2264062 :   return true;
     923              : }
     924              : 
     925              : /* Set to constant if it isn't already. Only meant to be called
     926              :    when switching state from TOP.  */
     927              : 
     928              : bool
     929        76357 : ipcp_bits_lattice::set_to_constant (widest_int value, widest_int mask)
     930              : {
     931        76357 :   gcc_assert (top_p ());
     932        76357 :   m_lattice_val = IPA_BITS_CONSTANT;
     933        76357 :   m_value = wi::bit_and (wi::bit_not (mask), value);
     934        76357 :   m_mask = mask;
     935        76357 :   return true;
     936              : }
     937              : 
     938              : /* Return true if any of the known bits are non-zero.  */
     939              : 
     940              : bool
     941          472 : ipcp_bits_lattice::known_nonzero_p () const
     942              : {
     943          472 :   if (!constant_p ())
     944              :     return false;
     945          472 :   return wi::ne_p (wi::bit_and (wi::bit_not (m_mask), m_value), 0);
     946              : }
     947              : 
     948              : /* Set the flag that this lattice is a recipient only, return true if it was not
     949              :    set before.  */
     950              : 
     951              : bool
     952        29238 : ipcp_bits_lattice::set_recipient_only ()
     953              : {
     954        29238 :   if (m_recipient_only)
     955              :     return false;
     956        29238 :   m_recipient_only = true;
     957        29238 :   return true;
     958              : }
     959              : 
     960              : /* Convert operand to value, mask form.  */
     961              : 
     962              : void
     963         2063 : ipcp_bits_lattice::get_value_and_mask (tree operand, widest_int *valuep, widest_int *maskp)
     964              : {
     965         2063 :   wide_int get_nonzero_bits (const_tree);
     966              : 
     967         2063 :   if (TREE_CODE (operand) == INTEGER_CST)
     968              :     {
     969         2063 :       *valuep = wi::to_widest (operand);
     970         2063 :       *maskp = 0;
     971              :     }
     972              :   else
     973              :     {
     974            0 :       *valuep = 0;
     975            0 :       *maskp = -1;
     976              :     }
     977         2063 : }
     978              : 
     979              : /* Meet operation, similar to ccp_lattice_meet, we xor values
     980              :    if this->value, value have different values at same bit positions, we want
     981              :    to drop that bit to varying. Return true if mask is changed.
     982              :    This function assumes that the lattice value is in CONSTANT state.  If
     983              :    DROP_ALL_ONES, mask out any known bits with value one afterwards.  */
     984              : 
     985              : bool
     986       302239 : ipcp_bits_lattice::meet_with_1 (widest_int value, widest_int mask,
     987              :                                 unsigned precision, bool drop_all_ones)
     988              : {
     989       302239 :   gcc_assert (constant_p ());
     990              : 
     991       302239 :   widest_int old_mask = m_mask;
     992       302239 :   m_mask = (m_mask | mask) | (m_value ^ value);
     993       302239 :   if (drop_all_ones)
     994          199 :     m_mask |= m_value;
     995              : 
     996       302239 :   widest_int cap_mask = wi::shifted_mask <widest_int> (0, precision, true);
     997       302239 :   m_mask |= cap_mask;
     998       302239 :   if (wi::sext (m_mask, precision) == -1)
     999         3504 :     return set_to_bottom ();
    1000              : 
    1001       298735 :   m_value &= ~m_mask;
    1002       298735 :   return m_mask != old_mask;
    1003       302239 : }
    1004              : 
    1005              : /* Meet the bits lattice with operand
    1006              :    described by <value, mask, sgn, precision.  */
    1007              : 
    1008              : bool
    1009       410217 : ipcp_bits_lattice::meet_with (widest_int value, widest_int mask,
    1010              :                               unsigned precision)
    1011              : {
    1012       410217 :   if (bottom_p ())
    1013              :     return false;
    1014              : 
    1015       410217 :   if (top_p ())
    1016              :     {
    1017       120599 :       if (wi::sext (mask, precision) == -1)
    1018        49499 :         return set_to_bottom ();
    1019        71100 :       return set_to_constant (value, mask);
    1020              :     }
    1021              : 
    1022       289618 :   return meet_with_1 (value, mask, precision, false);
    1023              : }
    1024              : 
    1025              : /* Meet bits lattice with the result of bit_value_binop (other, operand)
    1026              :    if code is binary operation or bit_value_unop (other) if code is unary op.
    1027              :    In the case when code is nop_expr, no adjustment is required.  If
    1028              :    DROP_ALL_ONES, mask out any known bits with value one afterwards.  */
    1029              : 
    1030              : bool
    1031        21452 : ipcp_bits_lattice::meet_with (ipcp_bits_lattice& other, unsigned precision,
    1032              :                               signop sgn, enum tree_code code, tree operand,
    1033              :                               bool drop_all_ones)
    1034              : {
    1035        21452 :   if (other.bottom_p ())
    1036            0 :     return set_to_bottom ();
    1037              : 
    1038        21452 :   if (bottom_p () || other.top_p ())
    1039              :     return false;
    1040              : 
    1041        17994 :   widest_int adjusted_value, adjusted_mask;
    1042              : 
    1043        17994 :   if (TREE_CODE_CLASS (code) == tcc_binary)
    1044              :     {
    1045         2063 :       tree type = TREE_TYPE (operand);
    1046         2063 :       widest_int o_value, o_mask;
    1047         2063 :       get_value_and_mask (operand, &o_value, &o_mask);
    1048              : 
    1049         2063 :       bit_value_binop (code, sgn, precision, &adjusted_value, &adjusted_mask,
    1050         4126 :                        sgn, precision, other.get_value (), other.get_mask (),
    1051         2063 :                        TYPE_SIGN (type), TYPE_PRECISION (type), o_value, o_mask);
    1052              : 
    1053         2063 :       if (wi::sext (adjusted_mask, precision) == -1)
    1054           87 :         return set_to_bottom ();
    1055         2063 :     }
    1056              : 
    1057        15931 :   else if (TREE_CODE_CLASS (code) == tcc_unary)
    1058              :     {
    1059        31812 :       bit_value_unop (code, sgn, precision, &adjusted_value,
    1060        31812 :                       &adjusted_mask, sgn, precision, other.get_value (),
    1061        15906 :                       other.get_mask ());
    1062              : 
    1063        15906 :       if (wi::sext (adjusted_mask, precision) == -1)
    1064            4 :         return set_to_bottom ();
    1065              :     }
    1066              : 
    1067              :   else
    1068           25 :     return set_to_bottom ();
    1069              : 
    1070        17878 :   if (top_p ())
    1071              :     {
    1072         5257 :       if (drop_all_ones)
    1073              :         {
    1074          273 :           adjusted_mask |= adjusted_value;
    1075          273 :           adjusted_value &= ~adjusted_mask;
    1076              :         }
    1077         5257 :       widest_int cap_mask = wi::shifted_mask <widest_int> (0, precision, true);
    1078         5257 :       adjusted_mask |= cap_mask;
    1079         5257 :       if (wi::sext (adjusted_mask, precision) == -1)
    1080            0 :         return set_to_bottom ();
    1081         5257 :       return set_to_constant (adjusted_value, adjusted_mask);
    1082         5257 :     }
    1083              :   else
    1084        12621 :     return meet_with_1 (adjusted_value, adjusted_mask, precision,
    1085              :                         drop_all_ones);
    1086        17994 : }
    1087              : 
    1088              : /* Dump the contents of the list to FILE.  */
    1089              : 
    1090              : void
    1091          120 : ipa_argagg_value_list::dump (FILE *f)
    1092              : {
    1093          120 :   bool comma = false;
    1094          333 :   for (const ipa_argagg_value &av : m_elts)
    1095              :     {
    1096          213 :       fprintf (f, "%s %i[%u]=", comma ? "," : "",
    1097          213 :                av.index, av.unit_offset);
    1098          213 :       print_generic_expr (f, av.value);
    1099          213 :       if (av.by_ref)
    1100          186 :         fprintf (f, "(by_ref)");
    1101          213 :       if (av.killed)
    1102            1 :         fprintf (f, "(killed)");
    1103          213 :       comma = true;
    1104              :     }
    1105          120 :   fprintf (f, "\n");
    1106          120 : }
    1107              : 
    1108              : /* Dump the contents of the list to stderr.  */
    1109              : 
    1110              : void
    1111            0 : ipa_argagg_value_list::debug ()
    1112              : {
    1113            0 :   dump (stderr);
    1114            0 : }
    1115              : 
    1116              : /* Return the item describing a constant stored for INDEX at UNIT_OFFSET or
    1117              :    NULL if there is no such constant.  */
    1118              : 
    1119              : const ipa_argagg_value *
    1120     29138191 : ipa_argagg_value_list::get_elt (int index, unsigned unit_offset) const
    1121              : {
    1122     29138191 :   ipa_argagg_value key;
    1123     29138191 :   key.index = index;
    1124     29138191 :   key.unit_offset = unit_offset;
    1125     29138191 :   const ipa_argagg_value *res
    1126     29138191 :     = std::lower_bound (m_elts.begin (), m_elts.end (), key,
    1127      6772998 :                         [] (const ipa_argagg_value &elt,
    1128              :                             const ipa_argagg_value &val)
    1129              :                         {
    1130      6772998 :                           if (elt.index < val.index)
    1131              :                             return true;
    1132      5763984 :                           if (elt.index > val.index)
    1133              :                             return false;
    1134      4622009 :                           if (elt.unit_offset < val.unit_offset)
    1135              :                             return true;
    1136              :                           return false;
    1137              :                         });
    1138              : 
    1139     29138191 :   if (res == m_elts.end ()
    1140      2908700 :       || res->index != index
    1141     31497220 :       || res->unit_offset != unit_offset)
    1142              :     res = nullptr;
    1143              : 
    1144              :   /* TODO: perhaps remove the check (that the underlying array is indeed
    1145              :      sorted) if it turns out it can be too slow? */
    1146     29138191 :   if (!flag_checking)
    1147              :     return res;
    1148              : 
    1149              :   const ipa_argagg_value *slow_res = NULL;
    1150              :   int prev_index = -1;
    1151              :   unsigned prev_unit_offset = 0;
    1152     45469402 :   for (const ipa_argagg_value &av : m_elts)
    1153              :     {
    1154     16331211 :       gcc_assert (prev_index < 0
    1155              :                   || prev_index < av.index
    1156              :                   || prev_unit_offset < av.unit_offset);
    1157     16331211 :       prev_index = av.index;
    1158     16331211 :       prev_unit_offset = av.unit_offset;
    1159     16331211 :       if (av.index == index
    1160      7494321 :           && av.unit_offset == unit_offset)
    1161     16331211 :         slow_res = &av;
    1162              :     }
    1163     29138191 :   gcc_assert (res == slow_res);
    1164              : 
    1165              :   return res;
    1166              : }
    1167              : 
    1168              : /* Return the first item describing a constant stored for parameter with INDEX,
    1169              :    regardless of offset or reference, or NULL if there is no such constant.  */
    1170              : 
    1171              : const ipa_argagg_value *
    1172       229354 : ipa_argagg_value_list::get_elt_for_index (int index) const
    1173              : {
    1174       229354 :   const ipa_argagg_value *res
    1175       229354 :     = std::lower_bound (m_elts.begin (), m_elts.end (), index,
    1176        18426 :                         [] (const ipa_argagg_value &elt, unsigned idx)
    1177              :                         {
    1178        18426 :                           return elt.index < idx;
    1179              :                         });
    1180       229354 :   if (res == m_elts.end ()
    1181       229354 :       || res->index != index)
    1182              :     res = nullptr;
    1183       229354 :   return res;
    1184              : }
    1185              : 
    1186              : /* Return the aggregate constant stored for INDEX at UNIT_OFFSET, not
    1187              :    performing any check of whether value is passed by reference, or NULL_TREE
    1188              :    if there is no such constant.  */
    1189              : 
    1190              : tree
    1191        36156 : ipa_argagg_value_list::get_value (int index, unsigned unit_offset) const
    1192              : {
    1193        36156 :   const ipa_argagg_value *av = get_elt (index, unit_offset);
    1194        36156 :   return av ? av->value : NULL_TREE;
    1195              : }
    1196              : 
    1197              : /* Return the aggregate constant stored for INDEX at UNIT_OFFSET, if it is
    1198              :    passed by reference or not according to BY_REF, or NULL_TREE if there is
    1199              :    no such constant.  */
    1200              : 
    1201              : tree
    1202     29092312 : ipa_argagg_value_list::get_value (int index, unsigned unit_offset,
    1203              :                                     bool by_ref) const
    1204              : {
    1205     29092312 :   const ipa_argagg_value *av = get_elt (index, unit_offset);
    1206     29092312 :   if (av && av->by_ref == by_ref)
    1207      1869199 :     return av->value;
    1208              :   return NULL_TREE;
    1209              : }
    1210              : 
    1211              : /* Return true if all elements present in OTHER are also present in this
    1212              :    list.  */
    1213              : 
    1214              : bool
    1215           45 : ipa_argagg_value_list::superset_of_p (const ipa_argagg_value_list &other) const
    1216              : {
    1217           45 :   unsigned j = 0;
    1218          200 :   for (unsigned i = 0; i < other.m_elts.size (); i++)
    1219              :     {
    1220          172 :       unsigned other_index = other.m_elts[i].index;
    1221          172 :       unsigned other_offset = other.m_elts[i].unit_offset;
    1222              : 
    1223          172 :       while (j < m_elts.size ()
    1224          327 :              && (m_elts[j].index < other_index
    1225          311 :                  || (m_elts[j].index == other_index
    1226          311 :                      && m_elts[j].unit_offset < other_offset)))
    1227          155 :        j++;
    1228              : 
    1229          172 :       if (j >= m_elts.size ()
    1230          159 :           || m_elts[j].index != other_index
    1231          159 :           || m_elts[j].unit_offset != other_offset
    1232          159 :           || m_elts[j].by_ref != other.m_elts[i].by_ref
    1233          159 :           || !m_elts[j].value
    1234          331 :           || !values_equal_for_ipcp_p (m_elts[j].value, other.m_elts[i].value))
    1235           17 :         return false;
    1236              :     }
    1237              :   return true;
    1238              : }
    1239              : 
    1240              : /* Push all items in this list that describe parameter SRC_INDEX into RES as
    1241              :    ones describing DST_INDEX while subtracting UNIT_DELTA from their unit
    1242              :    offsets but skip those which would end up with a negative offset.  */
    1243              : 
    1244              : void
    1245         3145 : ipa_argagg_value_list::push_adjusted_values (unsigned src_index,
    1246              :                                              unsigned dest_index,
    1247              :                                              unsigned unit_delta,
    1248              :                                              vec<ipa_argagg_value> *res) const
    1249              : {
    1250         3145 :   const ipa_argagg_value *av = get_elt_for_index (src_index);
    1251         3145 :   if (!av)
    1252              :     return;
    1253              :   unsigned prev_unit_offset = 0;
    1254              :   bool first = true;
    1255        11981 :   for (; av < m_elts.end (); ++av)
    1256              :     {
    1257         9558 :       if (av->index > src_index)
    1258              :         return;
    1259         8948 :       if (av->index == src_index
    1260         8948 :           && (av->unit_offset >= unit_delta)
    1261         8804 :           && av->value)
    1262              :         {
    1263         8804 :           ipa_argagg_value new_av;
    1264         8804 :           gcc_checking_assert (av->value);
    1265         8804 :           new_av.value = av->value;
    1266         8804 :           new_av.unit_offset = av->unit_offset - unit_delta;
    1267         8804 :           new_av.index = dest_index;
    1268         8804 :           new_av.by_ref = av->by_ref;
    1269         8804 :           gcc_assert (!av->killed);
    1270         8804 :           new_av.killed = false;
    1271              : 
    1272              :           /* Quick check that the offsets we push are indeed increasing.  */
    1273         8804 :           gcc_assert (first
    1274              :                       || new_av.unit_offset > prev_unit_offset);
    1275         8804 :           prev_unit_offset = new_av.unit_offset;
    1276         8804 :           first = false;
    1277              : 
    1278         8804 :           res->safe_push (new_av);
    1279              :         }
    1280              :     }
    1281              : }
    1282              : 
    1283              : /* Push to RES information about single lattices describing aggregate values in
    1284              :    PLATS as those describing parameter DEST_INDEX and the original offset minus
    1285              :    UNIT_DELTA.  Return true if any item has been pushed to RES.  */
    1286              : 
    1287              : static bool
    1288      4554707 : push_agg_values_from_plats (ipcp_param_lattices *plats, int dest_index,
    1289              :                             unsigned unit_delta,
    1290              :                             vec<ipa_argagg_value> *res)
    1291              : {
    1292      4554707 :   if (plats->aggs_contain_variable)
    1293              :     return false;
    1294              : 
    1295      3867357 :   bool pushed_sth = false;
    1296      3867357 :   bool first = true;
    1297      3867357 :   unsigned prev_unit_offset = 0;
    1298      3932873 :   for (struct ipcp_agg_lattice *aglat = plats->aggs; aglat; aglat = aglat->next)
    1299       129824 :     if (aglat->is_single_const ()
    1300        40321 :         && (aglat->offset / BITS_PER_UNIT - unit_delta) >= 0)
    1301              :       {
    1302        40321 :         ipa_argagg_value iav;
    1303        40321 :         iav.value = aglat->values->value;
    1304        40321 :         iav.unit_offset = aglat->offset / BITS_PER_UNIT - unit_delta;
    1305        40321 :         iav.index = dest_index;
    1306        40321 :         iav.by_ref = plats->aggs_by_ref;
    1307        40321 :         iav.killed = false;
    1308              : 
    1309        40321 :         gcc_assert (first
    1310              :                     || iav.unit_offset > prev_unit_offset);
    1311        40321 :         prev_unit_offset = iav.unit_offset;
    1312        40321 :         first = false;
    1313              : 
    1314        40321 :         pushed_sth = true;
    1315        40321 :         res->safe_push (iav);
    1316              :       }
    1317              :   return pushed_sth;
    1318              : }
    1319              : 
    1320              : /* Turn all values in LIST that are not present in OTHER into NULL_TREEs.
    1321              :    Return the number of remaining valid entries.  */
    1322              : 
    1323              : static unsigned
    1324        55087 : intersect_argaggs_with (vec<ipa_argagg_value> &elts,
    1325              :                         const vec<ipa_argagg_value> &other)
    1326              : {
    1327        55087 :   unsigned valid_entries = 0;
    1328        55087 :   unsigned j = 0;
    1329       400705 :   for (unsigned i = 0; i < elts.length (); i++)
    1330              :     {
    1331       345618 :       if (!elts[i].value)
    1332        49282 :         continue;
    1333              : 
    1334       296336 :       unsigned this_index = elts[i].index;
    1335       296336 :       unsigned this_offset = elts[i].unit_offset;
    1336              : 
    1337       296336 :       while (j < other.length ()
    1338      1106944 :              && (other[j].index < this_index
    1339       521373 :                  || (other[j].index == this_index
    1340       518009 :                      && other[j].unit_offset < this_offset)))
    1341       261331 :         j++;
    1342              : 
    1343       296336 :       if (j >= other.length ())
    1344              :         {
    1345         8390 :           elts[i].value = NULL_TREE;
    1346         8390 :           continue;
    1347              :         }
    1348              : 
    1349       287946 :       if (other[j].index == this_index
    1350       284582 :           && other[j].unit_offset == this_offset
    1351       279695 :           && other[j].by_ref == elts[i].by_ref
    1352       279695 :           && other[j].value
    1353       567641 :           && values_equal_for_ipcp_p (other[j].value, elts[i].value))
    1354       261709 :         valid_entries++;
    1355              :       else
    1356        26237 :         elts[i].value = NULL_TREE;
    1357              :     }
    1358        55087 :   return valid_entries;
    1359              : }
    1360              : 
    1361              : /* Mark bot aggregate and scalar lattices as containing an unknown variable,
    1362              :    return true is any of them has not been marked as such so far.  If if
    1363              :    MAKE_SIMPLE_RECIPIENTS is true, set the lattices that can only hold one
    1364              :    value to being recipients only, otherwise also set them to bottom.  */
    1365              : 
    1366              : static inline bool
    1367       169993 : set_all_contains_variable (class ipcp_param_lattices *plats,
    1368              :                            bool make_simple_recipients = false)
    1369              : {
    1370       169993 :   bool ret;
    1371       169993 :   ret = plats->itself.set_contains_variable ();
    1372       169993 :   ret |= plats->ctxlat.set_contains_variable ();
    1373       169993 :   ret |= set_agg_lats_contain_variable (plats);
    1374       169993 :   if (make_simple_recipients)
    1375              :     {
    1376        29238 :       ret |= plats->bits_lattice.set_recipient_only ();
    1377        29238 :       ret |= plats->m_value_range.set_recipient_only ();
    1378              :     }
    1379              :   else
    1380              :     {
    1381       140755 :       ret |= plats->bits_lattice.set_to_bottom ();
    1382       140755 :       ret |= plats->m_value_range.set_to_bottom ();
    1383              :     }
    1384       169993 :   return ret;
    1385              : }
    1386              : 
    1387              : /* Worker of call_for_symbol_thunks_and_aliases, increment the integer DATA
    1388              :    points to by the number of callers to NODE.  */
    1389              : 
    1390              : static bool
    1391        99460 : count_callers (cgraph_node *node, void *data)
    1392              : {
    1393        99460 :   int *caller_count = (int *) data;
    1394              : 
    1395       405047 :   for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
    1396              :     /* Local thunks can be handled transparently, but if the thunk cannot
    1397              :        be optimized out, count it as a real use.  */
    1398       305587 :     if (!cs->caller->thunk || !cs->caller->local)
    1399       305587 :       ++*caller_count;
    1400        99460 :   return false;
    1401              : }
    1402              : 
    1403              : /* Worker of call_for_symbol_thunks_and_aliases, it is supposed to be called on
    1404              :    the one caller of some other node.  Set the caller's corresponding flag.  */
    1405              : 
    1406              : static bool
    1407        56684 : set_single_call_flag (cgraph_node *node, void *)
    1408              : {
    1409        56684 :   cgraph_edge *cs = node->callers;
    1410              :   /* Local thunks can be handled transparently, skip them.  */
    1411        56684 :   while (cs && cs->caller->thunk && cs->caller->local)
    1412            0 :     cs = cs->next_caller;
    1413        56684 :   if (cs)
    1414        56114 :     if (ipa_node_params* info = ipa_node_params_sum->get (cs->caller))
    1415              :       {
    1416        56113 :         info->node_calling_single_call = true;
    1417        56113 :         return true;
    1418              :       }
    1419              :   return false;
    1420              : }
    1421              : 
    1422              : /* Initialize ipcp_lattices.  */
    1423              : 
    1424              : static void
    1425      1269638 : initialize_node_lattices (struct cgraph_node *node)
    1426              : {
    1427      1269638 :   ipa_node_params *info = ipa_node_params_sum->get (node);
    1428      1269638 :   struct cgraph_edge *ie;
    1429      1269638 :   bool disable = false, variable = false;
    1430      1269638 :   int i;
    1431              : 
    1432      1269638 :   gcc_checking_assert (node->has_gimple_body_p ());
    1433              : 
    1434      1269638 :   if (!ipa_get_param_count (info))
    1435              :     disable = true;
    1436      1040531 :   else if (node->local)
    1437              :     {
    1438        88513 :       int caller_count = 0;
    1439        88513 :       node->call_for_symbol_thunks_and_aliases (count_callers, &caller_count,
    1440              :                                                 true);
    1441        88513 :       if (caller_count == 1)
    1442        56114 :         node->call_for_symbol_thunks_and_aliases (set_single_call_flag,
    1443              :                                                   NULL, true);
    1444        32399 :       else if (caller_count == 0)
    1445              :         {
    1446            1 :           gcc_checking_assert (!opt_for_fn (node->decl, flag_toplevel_reorder));
    1447              :           variable = true;
    1448              :         }
    1449              :     }
    1450              :   else
    1451              :     {
    1452              :       /* When cloning is allowed, we can assume that externally visible
    1453              :          functions are not called.  We will compensate this by cloning
    1454              :          later.  */
    1455       952018 :       if (ipcp_versionable_function_p (node)
    1456       952018 :           && ipcp_cloning_candidate_p (node))
    1457              :         variable = true;
    1458              :       else
    1459              :         disable = true;
    1460              :     }
    1461              : 
    1462          728 :   if (dump_file && (dump_flags & TDF_DETAILS)
    1463      1269805 :       && !node->alias && !node->thunk)
    1464              :     {
    1465          167 :       fprintf (dump_file, "Initializing lattices of %s\n",
    1466              :                node->dump_name ());
    1467          167 :       if (disable || variable)
    1468          132 :         fprintf (dump_file, "  Marking all lattices as %s\n",
    1469              :                  disable ? "BOTTOM" : "VARIABLE");
    1470              :     }
    1471              : 
    1472      1269638 :   auto_vec<bool, 16> surviving_params;
    1473      1269638 :   bool pre_modified = false;
    1474              : 
    1475      1269638 :   clone_info *cinfo = clone_info::get (node);
    1476              : 
    1477      1269638 :   if (!disable && cinfo && cinfo->param_adjustments)
    1478              :     {
    1479              :       /* At the moment all IPA optimizations should use the number of
    1480              :          parameters of the prevailing decl as the m_always_copy_start.
    1481              :          Handling any other value would complicate the code below, so for the
    1482              :          time bing let's only assert it is so.  */
    1483            0 :       gcc_assert ((cinfo->param_adjustments->m_always_copy_start
    1484              :                    == ipa_get_param_count (info))
    1485              :                   || cinfo->param_adjustments->m_always_copy_start < 0);
    1486              : 
    1487            0 :       pre_modified = true;
    1488            0 :       cinfo->param_adjustments->get_surviving_params (&surviving_params);
    1489              : 
    1490            0 :       if (dump_file && (dump_flags & TDF_DETAILS)
    1491            0 :           && !node->alias && !node->thunk)
    1492              :         {
    1493              :           bool first = true;
    1494            0 :           for (int j = 0; j < ipa_get_param_count (info); j++)
    1495              :             {
    1496            0 :               if (j < (int) surviving_params.length ()
    1497            0 :                   && surviving_params[j])
    1498            0 :                 continue;
    1499            0 :               if (first)
    1500              :                 {
    1501            0 :                   fprintf (dump_file,
    1502              :                            "  The following parameters are dead on arrival:");
    1503            0 :                   first = false;
    1504              :                 }
    1505            0 :               fprintf (dump_file, " %u", j);
    1506              :             }
    1507            0 :           if (!first)
    1508            0 :               fprintf (dump_file, "\n");
    1509              :         }
    1510              :     }
    1511              : 
    1512      6977753 :   for (i = 0; i < ipa_get_param_count (info); i++)
    1513              :     {
    1514      2333792 :       ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
    1515      2333792 :       tree type = ipa_get_type (info, i);
    1516      2333792 :       if (disable
    1517       223023 :           || !ipa_get_type (info, i)
    1518      2556815 :           || (pre_modified && (surviving_params.length () <= (unsigned) i
    1519            0 :                                || !surviving_params[i])))
    1520              :         {
    1521      2110769 :           plats->itself.set_to_bottom ();
    1522      2110769 :           plats->ctxlat.set_to_bottom ();
    1523      2110769 :           set_agg_lats_to_bottom (plats);
    1524      2110769 :           plats->bits_lattice.set_to_bottom ();
    1525      2110769 :           plats->m_value_range.init (type);
    1526      2110769 :           plats->m_value_range.set_to_bottom ();
    1527              :         }
    1528              :       else
    1529              :         {
    1530       223023 :           plats->m_value_range.init (type);
    1531       223023 :           if (variable)
    1532        29238 :             set_all_contains_variable (plats, true);
    1533              :         }
    1534              :     }
    1535              : 
    1536      1403516 :   for (ie = node->indirect_calls; ie; ie = ie->next_callee)
    1537       133878 :     if (ie->indirect_info->param_index >= 0
    1538       142973 :         && is_a <cgraph_polymorphic_indirect_info *> (ie->indirect_info))
    1539         9095 :       ipa_get_parm_lattices (info,
    1540         9095 :                              ie->indirect_info->param_index)->virt_call = 1;
    1541      1269638 : }
    1542              : 
    1543              : /* Return VALUE if it is NULL_TREE or if it can be directly safely IPA-CP
    1544              :    propagated to a parameter of type PARAM_TYPE, or return a fold-converted
    1545              :    VALUE to PARAM_TYPE if that is possible.  Return NULL_TREE otherwise.  */
    1546              : 
    1547              : tree
    1548      5369012 : ipacp_value_safe_for_type (tree param_type, tree value)
    1549              : {
    1550      5369012 :   if (!value)
    1551              :     return NULL_TREE;
    1552      5368678 :   tree val_type = TREE_TYPE (value);
    1553      5368678 :   if (param_type == val_type
    1554      5368678 :       || useless_type_conversion_p (param_type, val_type))
    1555      5365310 :     return value;
    1556         3368 :   if (fold_convertible_p (param_type, value))
    1557         3163 :     return fold_convert (param_type, value);
    1558              :   else
    1559              :     return NULL_TREE;
    1560              : }
    1561              : 
    1562              : /* Return the result of a (possibly arithmetic) operation determined by OPCODE
    1563              :    on the constant value INPUT.  OPERAND is 2nd operand for binary operation
    1564              :    and is required for binary operations.  RES_TYPE, required when opcode is
    1565              :    not NOP_EXPR, is the type in which any operation is to be performed.  Return
    1566              :    NULL_TREE if that cannot be determined or be considered an interprocedural
    1567              :    invariant.  */
    1568              : 
    1569              : static tree
    1570        68752 : ipa_get_jf_arith_result (enum tree_code opcode, tree input, tree operand,
    1571              :                          tree res_type)
    1572              : {
    1573        68752 :   tree res;
    1574              : 
    1575        68752 :   if (opcode == NOP_EXPR)
    1576              :     return input;
    1577         6533 :   if (!is_gimple_ip_invariant (input))
    1578              :     return NULL_TREE;
    1579              : 
    1580         6533 :   if (opcode == ASSERT_EXPR)
    1581              :     {
    1582         3542 :       if (values_equal_for_ipcp_p (input, operand))
    1583              :         return input;
    1584              :       else
    1585              :         return NULL_TREE;
    1586              :     }
    1587              : 
    1588         2991 :   if (TREE_CODE_CLASS (opcode) == tcc_unary)
    1589           91 :     res = fold_unary (opcode, res_type, input);
    1590              :   else
    1591         2900 :     res = fold_binary (opcode, res_type, input, operand);
    1592              : 
    1593         2991 :   if (res && !is_gimple_ip_invariant (res))
    1594              :     return NULL_TREE;
    1595              : 
    1596              :   return res;
    1597              : }
    1598              : 
    1599              : /* Return the result of an ancestor jump function JFUNC on the constant value
    1600              :    INPUT.  Return NULL_TREE if that cannot be determined.  */
    1601              : 
    1602              : static tree
    1603         1270 : ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
    1604              : {
    1605         1270 :   gcc_checking_assert (TREE_CODE (input) != TREE_BINFO);
    1606         1270 :   if (TREE_CODE (input) == ADDR_EXPR)
    1607              :     {
    1608         1188 :       gcc_checking_assert (is_gimple_ip_invariant_address (input));
    1609         1188 :       poly_int64 off = ipa_get_jf_ancestor_offset (jfunc);
    1610         1188 :       if (known_eq (off, 0))
    1611              :         return input;
    1612         1066 :       poly_int64 byte_offset = exact_div (off, BITS_PER_UNIT);
    1613         2132 :       return build1 (ADDR_EXPR, TREE_TYPE (input),
    1614         1066 :                      fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (input)), input,
    1615         1066 :                                   build_int_cst (ptr_type_node, byte_offset)));
    1616              :     }
    1617           82 :   else if (ipa_get_jf_ancestor_keep_null (jfunc)
    1618           82 :            && zerop (input))
    1619              :     return input;
    1620              :   else
    1621           78 :     return NULL_TREE;
    1622              : }
    1623              : 
    1624              : /* Determine whether JFUNC evaluates to a single known constant value and if
    1625              :    so, return it.  Otherwise return NULL.  INFO describes the caller node or
    1626              :    the one it is inlined to, so that pass-through jump functions can be
    1627              :    evaluated.  PARM_TYPE is the type of the parameter to which the result is
    1628              :    passed.  */
    1629              : 
    1630              : tree
    1631     18134753 : ipa_value_from_jfunc (class ipa_node_params *info, struct ipa_jump_func *jfunc,
    1632              :                       tree parm_type)
    1633              : {
    1634     18134753 :   if (!parm_type)
    1635              :     return NULL_TREE;
    1636     17897583 :   if (jfunc->type == IPA_JF_CONST)
    1637      4768265 :     return ipacp_value_safe_for_type (parm_type, ipa_get_jf_constant (jfunc));
    1638     13129318 :   else if (jfunc->type == IPA_JF_PASS_THROUGH
    1639     10316574 :            || jfunc->type == IPA_JF_ANCESTOR)
    1640              :     {
    1641      3612427 :       tree input;
    1642      3612427 :       int idx;
    1643              : 
    1644      3612427 :       if (jfunc->type == IPA_JF_PASS_THROUGH)
    1645      2812744 :         idx = ipa_get_jf_pass_through_formal_id (jfunc);
    1646              :       else
    1647       799683 :         idx = ipa_get_jf_ancestor_formal_id (jfunc);
    1648              : 
    1649      3612427 :       if (info->ipcp_orig_node)
    1650        56510 :         input = info->known_csts[idx];
    1651              :       else
    1652              :         {
    1653      3555917 :           ipcp_lattice<tree> *lat;
    1654              : 
    1655      6409252 :           if (info->lattices.is_empty ()
    1656      2853335 :               || idx >= ipa_get_param_count (info))
    1657              :             return NULL_TREE;
    1658      2853335 :           lat = ipa_get_scalar_lat (info, idx);
    1659      2853335 :           if (!lat->is_single_const ())
    1660              :             return NULL_TREE;
    1661          147 :           input = lat->values->value;
    1662              :         }
    1663              : 
    1664        56657 :       if (!input)
    1665              :         return NULL_TREE;
    1666              : 
    1667        19316 :       if (jfunc->type == IPA_JF_PASS_THROUGH)
    1668              :         {
    1669        18357 :           enum tree_code opcode = ipa_get_jf_pass_through_operation (jfunc);
    1670        18357 :           tree op2 = ipa_get_jf_pass_through_operand (jfunc);
    1671        18357 :           tree op_type
    1672        18357 :             = (opcode == NOP_EXPR) ? NULL_TREE
    1673          918 :             : ipa_get_jf_pass_through_op_type (jfunc);
    1674        18357 :           tree cstval = ipa_get_jf_arith_result (opcode, input, op2, op_type);
    1675        18357 :           return ipacp_value_safe_for_type (parm_type, cstval);
    1676              :         }
    1677              :       else
    1678          959 :         return ipacp_value_safe_for_type (parm_type,
    1679              :                                           ipa_get_jf_ancestor_result (jfunc,
    1680          959 :                                                                       input));
    1681              :     }
    1682              :   else
    1683              :     return NULL_TREE;
    1684              : }
    1685              : 
    1686              : /* Determine whether JFUNC evaluates to single known polymorphic context, given
    1687              :    that INFO describes the caller node or the one it is inlined to, CS is the
    1688              :    call graph edge corresponding to JFUNC and CSIDX index of the described
    1689              :    parameter.  */
    1690              : 
    1691              : ipa_polymorphic_call_context
    1692       878342 : ipa_context_from_jfunc (ipa_node_params *info, cgraph_edge *cs, int csidx,
    1693              :                         ipa_jump_func *jfunc)
    1694              : {
    1695       878342 :   ipa_edge_args *args = ipa_edge_args_sum->get (cs);
    1696       878342 :   ipa_polymorphic_call_context ctx;
    1697       878342 :   ipa_polymorphic_call_context *edge_ctx
    1698       878342 :     = cs ? ipa_get_ith_polymorhic_call_context (args, csidx) : NULL;
    1699              : 
    1700       351756 :   if (edge_ctx && !edge_ctx->useless_p ())
    1701       346508 :     ctx = *edge_ctx;
    1702              : 
    1703       878342 :   if (jfunc->type == IPA_JF_PASS_THROUGH
    1704       786553 :       || jfunc->type == IPA_JF_ANCESTOR)
    1705              :     {
    1706        99848 :       ipa_polymorphic_call_context srcctx;
    1707        99848 :       int srcidx;
    1708        99848 :       bool type_preserved = true;
    1709        99848 :       if (jfunc->type == IPA_JF_PASS_THROUGH)
    1710              :         {
    1711        91789 :           if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
    1712         1842 :             return ctx;
    1713        89947 :           type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc);
    1714        89947 :           srcidx = ipa_get_jf_pass_through_formal_id (jfunc);
    1715              :         }
    1716              :       else
    1717              :         {
    1718         8059 :           type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc);
    1719         8059 :           srcidx = ipa_get_jf_ancestor_formal_id (jfunc);
    1720              :         }
    1721        98006 :       if (info->ipcp_orig_node)
    1722              :         {
    1723        11462 :           if (info->known_contexts.exists ())
    1724         1358 :             srcctx = info->known_contexts[srcidx];
    1725              :         }
    1726              :       else
    1727              :         {
    1728       171453 :           if (info->lattices.is_empty ()
    1729        84909 :               || srcidx >= ipa_get_param_count (info))
    1730         1635 :             return ctx;
    1731        84909 :           ipcp_lattice<ipa_polymorphic_call_context> *lat;
    1732        84909 :           lat = ipa_get_poly_ctx_lat (info, srcidx);
    1733        84909 :           if (!lat->is_single_const ())
    1734        80964 :             return ctx;
    1735         3945 :           srcctx = lat->values->value;
    1736              :         }
    1737        15407 :       if (srcctx.useless_p ())
    1738        10550 :         return ctx;
    1739         4857 :       if (jfunc->type == IPA_JF_ANCESTOR)
    1740          253 :         srcctx.offset_by (ipa_get_jf_ancestor_offset (jfunc));
    1741         4857 :       if (!type_preserved)
    1742         2889 :         srcctx.possible_dynamic_type_change (cs->in_polymorphic_cdtor);
    1743         4857 :       srcctx.combine_with (ctx);
    1744         4857 :       return srcctx;
    1745              :     }
    1746              : 
    1747       778494 :   return ctx;
    1748              : }
    1749              : 
    1750              : /* Emulate effects of unary OPERATION and/or conversion from SRC_TYPE to
    1751              :    DST_TYPE on value range in SRC_VR and store it to DST_VR.  Return true if
    1752              :    the result is a range that is not VARYING nor UNDEFINED.  */
    1753              : 
    1754              : bool
    1755      9514566 : ipa_vr_operation_and_type_effects (vrange &dst_vr,
    1756              :                                    const vrange &src_vr,
    1757              :                                    enum tree_code operation,
    1758              :                                    tree dst_type, tree src_type)
    1759              : {
    1760     17871324 :   if (!ipa_vr_supported_type_p (dst_type)
    1761            0 :       || !ipa_vr_supported_type_p (src_type))
    1762              :     return false;
    1763              : 
    1764      9514566 :   range_op_handler handler (operation);
    1765      9514566 :   if (!handler)
    1766              :     return false;
    1767              : 
    1768      9514566 :   value_range varying (dst_type);
    1769      9514566 :   varying.set_varying (dst_type);
    1770              : 
    1771      9514566 :   return (handler.operand_check_p (dst_type, src_type, dst_type)
    1772      9514566 :           && handler.fold_range (dst_vr, dst_type, src_vr, varying)
    1773      9514564 :           && !dst_vr.varying_p ()
    1774     19029070 :           && !dst_vr.undefined_p ());
    1775      9514566 : }
    1776              : 
    1777              : /* Same as above, but the SRC_VR argument is an IPA_VR which must
    1778              :    first be extracted onto a vrange.  */
    1779              : 
    1780              : bool
    1781      9422209 : ipa_vr_operation_and_type_effects (vrange &dst_vr,
    1782              :                                    const ipa_vr &src_vr,
    1783              :                                    enum tree_code operation,
    1784              :                                    tree dst_type, tree src_type)
    1785              : {
    1786      9422209 :   value_range tmp;
    1787      9422209 :   src_vr.get_vrange (tmp);
    1788      9422209 :   return ipa_vr_operation_and_type_effects (dst_vr, tmp, operation,
    1789      9422209 :                                             dst_type, src_type);
    1790      9422209 : }
    1791              : 
    1792              : /* Given a PASS_THROUGH jump function JFUNC that takes as its source SRC_VR of
    1793              :    SRC_TYPE and the result needs to be DST_TYPE, if any value range information
    1794              :    can be deduced at all, intersect VR with it.  CONTEXT_NODE is the call graph
    1795              :    node representing the function for which optimization flags should be
    1796              :    evaluated.  */
    1797              : 
    1798              : static void
    1799        92757 : ipa_vr_intersect_with_arith_jfunc (vrange &vr,
    1800              :                                    ipa_jump_func *jfunc,
    1801              :                                    cgraph_node *context_node,
    1802              :                                    const value_range &src_vr,
    1803              :                                    tree src_type,
    1804              :                                    tree dst_type)
    1805              : {
    1806        92757 :   if (src_vr.undefined_p () || src_vr.varying_p ())
    1807        91481 :     return;
    1808              : 
    1809        92298 :   enum tree_code operation = ipa_get_jf_pass_through_operation (jfunc);
    1810        92298 :   if (TREE_CODE_CLASS (operation) == tcc_unary)
    1811              :     {
    1812        91022 :       value_range op_res;
    1813        91022 :       const value_range *inter_vr;
    1814        91022 :       if (operation != NOP_EXPR)
    1815              :         {
    1816           89 :           tree operation_type = ipa_get_jf_pass_through_op_type (jfunc);
    1817           89 :           op_res.set_varying (operation_type);
    1818           89 :           if (!ipa_vr_operation_and_type_effects (op_res, src_vr, operation,
    1819              :                                                   operation_type, src_type))
    1820              :             return;
    1821           89 :           if (src_type == dst_type)
    1822              :             {
    1823           30 :               vr.intersect (op_res);
    1824           30 :               return;
    1825              :             }
    1826              :           inter_vr = &op_res;
    1827              :           src_type = operation_type;
    1828              :         }
    1829              :       else
    1830              :         inter_vr = &src_vr;
    1831              : 
    1832        90992 :       value_range tmp_res (dst_type);
    1833        90992 :       if (ipa_vr_operation_and_type_effects (tmp_res, *inter_vr, NOP_EXPR,
    1834              :                                              dst_type, src_type))
    1835        90992 :         vr.intersect (tmp_res);
    1836        90992 :       return;
    1837        91022 :     }
    1838              : 
    1839         1276 :   tree operand = ipa_get_jf_pass_through_operand (jfunc);
    1840         1276 :   range_op_handler handler (operation);
    1841         1276 :   if (!handler)
    1842              :     return;
    1843         1276 :   value_range op_vr (TREE_TYPE (operand));
    1844         1276 :   ipa_get_range_from_ip_invariant (op_vr, operand, context_node);
    1845              : 
    1846         1276 :   tree operation_type = ipa_get_jf_pass_through_op_type (jfunc);
    1847         1276 :   value_range op_res (operation_type);
    1848         1716 :   if (!ipa_vr_supported_type_p (operation_type)
    1849         1276 :       || !handler.operand_check_p (operation_type, src_type, op_vr.type ())
    1850         1276 :       || !handler.fold_range (op_res, operation_type, src_vr, op_vr))
    1851            0 :     return;
    1852              : 
    1853         1276 :   value_range tmp_res (dst_type);
    1854         1276 :   if (ipa_vr_operation_and_type_effects (tmp_res, op_res, NOP_EXPR, dst_type,
    1855              :                                          operation_type))
    1856         1228 :       vr.intersect (tmp_res);
    1857         1276 : }
    1858              : 
    1859              : /* Determine range of JFUNC given that INFO describes the caller node or
    1860              :    the one it is inlined to, CS is the call graph edge corresponding to JFUNC
    1861              :    and PARM_TYPE of the parameter.  */
    1862              : 
    1863              : void
    1864     12105647 : ipa_value_range_from_jfunc (vrange &vr,
    1865              :                             ipa_node_params *info, cgraph_edge *cs,
    1866              :                             ipa_jump_func *jfunc, tree parm_type)
    1867              : {
    1868     12105647 :   vr.set_varying (parm_type);
    1869              : 
    1870     12105647 :   if (jfunc->m_vr && jfunc->m_vr->known_p ())
    1871      8606280 :     ipa_vr_operation_and_type_effects (vr,
    1872              :                                        *jfunc->m_vr,
    1873              :                                        NOP_EXPR, parm_type,
    1874      8606280 :                                        jfunc->m_vr->type ());
    1875     12105647 :   if (vr.singleton_p ())
    1876              :     return;
    1877              : 
    1878     12105507 :   if (jfunc->type == IPA_JF_PASS_THROUGH)
    1879              :     {
    1880      2245575 :       ipcp_transformation *sum
    1881      2245575 :         = ipcp_get_transformation_summary (cs->caller->inlined_to
    1882              :                                            ? cs->caller->inlined_to
    1883              :                                            : cs->caller);
    1884      2245575 :       if (!sum || !sum->m_vr)
    1885      2168020 :         return;
    1886              : 
    1887       118883 :       int idx = ipa_get_jf_pass_through_formal_id (jfunc);
    1888              : 
    1889       118883 :       if (!(*sum->m_vr)[idx].known_p ())
    1890              :         return;
    1891        77555 :       tree src_type = ipa_get_type (info, idx);
    1892        77555 :       value_range srcvr;
    1893        77555 :       (*sum->m_vr)[idx].get_vrange (srcvr);
    1894              : 
    1895        77555 :       ipa_vr_intersect_with_arith_jfunc (vr, jfunc, cs->caller, srcvr, src_type,
    1896              :                                          parm_type);
    1897        77555 :     }
    1898              : }
    1899              : 
    1900              : /* Determine whether ITEM, jump function for an aggregate part, evaluates to a
    1901              :    single known constant value and if so, return it.  Otherwise return NULL.
    1902              :    NODE and INFO describes the caller node or the one it is inlined to, and
    1903              :    its related info.  */
    1904              : 
    1905              : tree
    1906      3184432 : ipa_agg_value_from_jfunc (ipa_node_params *info, cgraph_node *node,
    1907              :                           const ipa_agg_jf_item *item)
    1908              : {
    1909      3184432 :   tree value = NULL_TREE;
    1910      3184432 :   int src_idx;
    1911              : 
    1912      3184432 :   if (item->offset < 0
    1913      3134375 :       || item->jftype == IPA_JF_UNKNOWN
    1914      2985428 :       || item->offset >= (HOST_WIDE_INT) UINT_MAX * BITS_PER_UNIT)
    1915              :     return NULL_TREE;
    1916              : 
    1917      2985428 :   if (item->jftype == IPA_JF_CONST)
    1918      2651285 :     return item->value.constant;
    1919              : 
    1920       334143 :   gcc_checking_assert (item->jftype == IPA_JF_PASS_THROUGH
    1921              :                        || item->jftype == IPA_JF_LOAD_AGG);
    1922              : 
    1923       334143 :   src_idx = item->value.pass_through.formal_id;
    1924              : 
    1925       334143 :   if (info->ipcp_orig_node)
    1926              :     {
    1927        15573 :       if (item->jftype == IPA_JF_PASS_THROUGH)
    1928         3772 :         value = info->known_csts[src_idx];
    1929        11801 :       else if (ipcp_transformation *ts = ipcp_get_transformation_summary (node))
    1930              :         {
    1931        11801 :           ipa_argagg_value_list avl (ts);
    1932        11801 :           value = avl.get_value (src_idx,
    1933        11801 :                                  item->value.load_agg.offset / BITS_PER_UNIT,
    1934        11801 :                                  item->value.load_agg.by_ref);
    1935              :         }
    1936              :     }
    1937       318570 :   else if (!info->lattices.is_empty ())
    1938              :     {
    1939       217999 :       class ipcp_param_lattices *src_plats
    1940       217999 :         = ipa_get_parm_lattices (info, src_idx);
    1941              : 
    1942       217999 :       if (item->jftype == IPA_JF_PASS_THROUGH)
    1943              :         {
    1944       131455 :           struct ipcp_lattice<tree> *lat = &src_plats->itself;
    1945              : 
    1946       531836 :           if (!lat->is_single_const ())
    1947              :             return NULL_TREE;
    1948              : 
    1949            0 :           value = lat->values->value;
    1950              :         }
    1951        86544 :       else if (src_plats->aggs
    1952         9829 :                && !src_plats->aggs_bottom
    1953         9829 :                && !src_plats->aggs_contain_variable
    1954         1499 :                && src_plats->aggs_by_ref == item->value.load_agg.by_ref)
    1955              :         {
    1956              :           struct ipcp_agg_lattice *aglat;
    1957              : 
    1958         2366 :           for (aglat = src_plats->aggs; aglat; aglat = aglat->next)
    1959              :             {
    1960         2366 :               if (aglat->offset > item->value.load_agg.offset)
    1961              :                 break;
    1962              : 
    1963         2334 :               if (aglat->offset == item->value.load_agg.offset)
    1964              :                 {
    1965         1467 :                   if (aglat->is_single_const ())
    1966            7 :                     value = aglat->values->value;
    1967              :                   break;
    1968              :                 }
    1969              :             }
    1970              :         }
    1971              :     }
    1972              : 
    1973        15612 :   if (!value)
    1974       192862 :     return NULL_TREE;
    1975              : 
    1976         9826 :   if (item->jftype == IPA_JF_LOAD_AGG)
    1977              :     {
    1978         7516 :       tree load_type = item->value.load_agg.type;
    1979         7516 :       tree value_type = TREE_TYPE (value);
    1980              : 
    1981              :       /* Ensure value type is compatible with load type.  */
    1982         7516 :       if (!useless_type_conversion_p (load_type, value_type))
    1983              :         return NULL_TREE;
    1984              :     }
    1985              : 
    1986        19652 :   tree cstval = ipa_get_jf_arith_result (item->value.pass_through.operation,
    1987              :                                          value,
    1988         9826 :                                          item->value.pass_through.operand,
    1989         9826 :                                          item->value.pass_through.op_type);
    1990         9826 :   return ipacp_value_safe_for_type (item->type, cstval);
    1991              : }
    1992              : 
    1993              : /* Process all items in AGG_JFUNC relative to caller (or the node the original
    1994              :   caller is inlined to) NODE which described by INFO and push the results to
    1995              :   RES as describing values passed in parameter DST_INDEX.  */
    1996              : 
    1997              : void
    1998     14591007 : ipa_push_agg_values_from_jfunc (ipa_node_params *info, cgraph_node *node,
    1999              :                                 ipa_agg_jump_function *agg_jfunc,
    2000              :                                 unsigned dst_index,
    2001              :                                 vec<ipa_argagg_value> *res)
    2002              : {
    2003     14591007 :   unsigned prev_unit_offset = 0;
    2004     14591007 :   bool first = true;
    2005              : 
    2006     19063693 :   for (const ipa_agg_jf_item &item : agg_jfunc->items)
    2007              :     {
    2008      2209476 :       tree value = ipa_agg_value_from_jfunc (info, node, &item);
    2009      2209476 :       if (!value)
    2010       500679 :         continue;
    2011              : 
    2012      1708797 :       ipa_argagg_value iav;
    2013      1708797 :       iav.value = value;
    2014      1708797 :       iav.unit_offset = item.offset / BITS_PER_UNIT;
    2015      1708797 :       iav.index = dst_index;
    2016      1708797 :       iav.by_ref = agg_jfunc->by_ref;
    2017      1708797 :       iav.killed = 0;
    2018              : 
    2019      1708797 :       gcc_assert (first
    2020              :                   || iav.unit_offset > prev_unit_offset);
    2021      1708797 :       prev_unit_offset = iav.unit_offset;
    2022      1708797 :       first = false;
    2023              : 
    2024      1708797 :       res->safe_push (iav);
    2025              :     }
    2026     14591007 : }
    2027              : 
    2028              : /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
    2029              :    bottom, not containing a variable component and without any known value at
    2030              :    the same time.  */
    2031              : 
    2032              : DEBUG_FUNCTION void
    2033       129019 : ipcp_verify_propagated_values (void)
    2034              : {
    2035       129019 :   struct cgraph_node *node;
    2036              : 
    2037      1407482 :   FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
    2038              :     {
    2039      1278463 :       ipa_node_params *info = ipa_node_params_sum->get (node);
    2040      1278463 :       if (!opt_for_fn (node->decl, flag_ipa_cp)
    2041      1278463 :           || !opt_for_fn (node->decl, optimize))
    2042         8842 :         continue;
    2043      1269621 :       int i, count = ipa_get_param_count (info);
    2044              : 
    2045      3603399 :       for (i = 0; i < count; i++)
    2046              :         {
    2047      2333778 :           ipcp_lattice<tree> *lat = ipa_get_scalar_lat (info, i);
    2048              : 
    2049      2333778 :           if (!lat->bottom
    2050       222017 :               && !lat->contains_variable
    2051        31712 :               && lat->values_count == 0)
    2052              :             {
    2053            0 :               if (dump_file)
    2054              :                 {
    2055            0 :                   symtab->dump (dump_file);
    2056            0 :                   fprintf (dump_file, "\nIPA lattices after constant "
    2057              :                            "propagation, before gcc_unreachable:\n");
    2058            0 :                   print_all_lattices (dump_file, true, false);
    2059              :                 }
    2060              : 
    2061            0 :               gcc_unreachable ();
    2062              :             }
    2063              :         }
    2064              :     }
    2065       129019 : }
    2066              : 
    2067              : /* Return true iff X and Y should be considered equal contexts by IPA-CP.  */
    2068              : 
    2069              : static bool
    2070         2685 : values_equal_for_ipcp_p (ipa_polymorphic_call_context x,
    2071              :                          ipa_polymorphic_call_context y)
    2072              : {
    2073         2173 :   return x.equal_to (y);
    2074              : }
    2075              : 
    2076              : 
    2077              : /* Add a new value source to the value represented by THIS, marking that a
    2078              :    value comes from edge CS and (if the underlying jump function is a
    2079              :    pass-through or an ancestor one) from a caller value SRC_VAL of a caller
    2080              :    parameter described by SRC_INDEX.  OFFSET is negative if the source was the
    2081              :    scalar value of the parameter itself or the offset within an aggregate.  */
    2082              : 
    2083              : template <typename valtype>
    2084              : void
    2085       486227 : ipcp_value<valtype>::add_source (cgraph_edge *cs, ipcp_value *src_val,
    2086              :                                  int src_idx, HOST_WIDE_INT offset)
    2087              : {
    2088              :   ipcp_value_source<valtype> *src;
    2089              : 
    2090       486227 :   src = new (ipcp_sources_pool.allocate ()) ipcp_value_source<valtype>;
    2091       486227 :   src->offset = offset;
    2092       486227 :   src->cs = cs;
    2093       486227 :   src->val = src_val;
    2094       486227 :   src->index = src_idx;
    2095              : 
    2096       486227 :   src->next = sources;
    2097       486227 :   sources = src;
    2098       486227 : }
    2099              : 
    2100              : /* Allocate a new ipcp_value holding a tree constant, initialize its value to
    2101              :    SOURCE and clear all other fields.  */
    2102              : 
    2103              : static ipcp_value<tree> *
    2104       139155 : allocate_and_init_ipcp_value (tree cst, unsigned same_lat_gen_level)
    2105              : {
    2106       139155 :   ipcp_value<tree> *val;
    2107              : 
    2108       139155 :   val = new (ipcp_cst_values_pool.allocate ()) ipcp_value<tree>();
    2109       139155 :   val->value = cst;
    2110       139155 :   val->self_recursion_generated_level = same_lat_gen_level;
    2111       139155 :   return val;
    2112              : }
    2113              : 
    2114              : /* Allocate a new ipcp_value holding a polymorphic context, initialize its
    2115              :    value to SOURCE and clear all other fields.  */
    2116              : 
    2117              : static ipcp_value<ipa_polymorphic_call_context> *
    2118         7743 : allocate_and_init_ipcp_value (ipa_polymorphic_call_context ctx,
    2119              :                               unsigned same_lat_gen_level)
    2120              : {
    2121         7743 :   ipcp_value<ipa_polymorphic_call_context> *val;
    2122              : 
    2123         7743 :   val = new (ipcp_poly_ctx_values_pool.allocate ())
    2124         7743 :     ipcp_value<ipa_polymorphic_call_context>();
    2125         7743 :   val->value = ctx;
    2126         7743 :   val->self_recursion_generated_level = same_lat_gen_level;
    2127         7743 :   return val;
    2128              : }
    2129              : 
    2130              : /* Try to add NEWVAL to LAT, potentially creating a new ipcp_value for it.  CS,
    2131              :    SRC_VAL SRC_INDEX and OFFSET are meant for add_source and have the same
    2132              :    meaning.  OFFSET -1 means the source is scalar and not a part of an
    2133              :    aggregate.  If non-NULL, VAL_P records address of existing or newly added
    2134              :    ipcp_value.
    2135              : 
    2136              :    If the value is generated for a self-recursive call as a result of an
    2137              :    arithmetic pass-through jump-function acting on a value in the same lattice,
    2138              :    SAME_LAT_GEN_LEVEL must be the length of such chain, otherwise it must be
    2139              :    zero.  If it is non-zero, PARAM_IPA_CP_VALUE_LIST_SIZE limit is ignored.  */
    2140              : 
    2141              : template <typename valtype>
    2142              : bool
    2143       498606 : ipcp_lattice<valtype>::add_value (valtype newval, cgraph_edge *cs,
    2144              :                                   ipcp_value<valtype> *src_val,
    2145              :                                   int src_idx, HOST_WIDE_INT offset,
    2146              :                                   ipcp_value<valtype> **val_p,
    2147              :                                   unsigned same_lat_gen_level)
    2148              : {
    2149       498606 :   ipcp_value<valtype> *val, *last_val = NULL;
    2150              : 
    2151       498606 :   if (val_p)
    2152         1257 :     *val_p = NULL;
    2153              : 
    2154       498606 :   if (bottom)
    2155              :     return false;
    2156              : 
    2157       967830 :   for (val = values; val; last_val = val, val = val->next)
    2158       819566 :     if (values_equal_for_ipcp_p (val->value, newval))
    2159              :       {
    2160       346984 :         if (val_p)
    2161          416 :           *val_p = val;
    2162              : 
    2163       346984 :         if (val->self_recursion_generated_level < same_lat_gen_level)
    2164          179 :           val->self_recursion_generated_level = same_lat_gen_level;
    2165              : 
    2166       346984 :         if (ipa_edge_within_scc (cs))
    2167              :           {
    2168              :             ipcp_value_source<valtype> *s;
    2169        48562 :             for (s = val->sources; s; s = s->next)
    2170        44373 :               if (s->cs == cs && s->val == src_val)
    2171              :                 break;
    2172        11844 :             if (s)
    2173              :               return false;
    2174              :           }
    2175              : 
    2176       339329 :         val->add_source (cs, src_val, src_idx, offset);
    2177       339329 :         return false;
    2178              :       }
    2179              : 
    2180       148264 :   if (!same_lat_gen_level && values_count >= opt_for_fn (cs->callee->decl,
    2181              :                                                 param_ipa_cp_value_list_size))
    2182              :     {
    2183              :       /* We can only free sources, not the values themselves, because sources
    2184              :          of other values in this SCC might point to them.   */
    2185        12276 :       for (val = values; val; val = val->next)
    2186              :         {
    2187        40439 :           while (val->sources)
    2188              :             {
    2189        29529 :               ipcp_value_source<valtype> *src = val->sources;
    2190        29529 :               val->sources = src->next;
    2191        29529 :               ipcp_sources_pool.remove ((ipcp_value_source<tree>*)src);
    2192              :             }
    2193              :         }
    2194         1366 :       values = NULL;
    2195         1366 :       return set_to_bottom ();
    2196              :     }
    2197              : 
    2198       146898 :   values_count++;
    2199       146898 :   val = allocate_and_init_ipcp_value (newval, same_lat_gen_level);
    2200       146898 :   val->add_source (cs, src_val, src_idx, offset);
    2201       146898 :   val->next = NULL;
    2202              : 
    2203              :   /* Add the new value to end of value list, which can reduce iterations
    2204              :      of propagation stage for recursive function.  */
    2205       146898 :   if (last_val)
    2206        45331 :     last_val->next = val;
    2207              :   else
    2208       101567 :     values = val;
    2209              : 
    2210       146898 :   if (val_p)
    2211          841 :     *val_p = val;
    2212              : 
    2213              :   return true;
    2214              : }
    2215              : 
    2216              : /* A helper function that returns result of operation specified by OPCODE on
    2217              :    the value of SRC_VAL.  If non-NULL, OPND1_TYPE is expected type for the
    2218              :    value of SRC_VAL.  If the operation is binary, OPND2 is a constant value
    2219              :    acting as its second operand.  OP_TYPE is the type in which the operation is
    2220              :    performed.  */
    2221              : 
    2222              : static tree
    2223        21392 : get_val_across_arith_op (enum tree_code opcode,
    2224              :                          tree opnd1_type,
    2225              :                          tree opnd2,
    2226              :                          ipcp_value<tree> *src_val,
    2227              :                          tree op_type)
    2228              : {
    2229        21392 :   tree opnd1 = src_val->value;
    2230              : 
    2231              :   /* Skip source values that is incompatible with specified type.  */
    2232        21392 :   if (opnd1_type
    2233        21392 :       && !useless_type_conversion_p (opnd1_type, TREE_TYPE (opnd1)))
    2234              :     return NULL_TREE;
    2235              : 
    2236        21392 :   return ipa_get_jf_arith_result (opcode, opnd1, opnd2, op_type);
    2237              : }
    2238              : 
    2239              : /* Propagate values through an arithmetic transformation described by a jump
    2240              :    function associated with edge CS, taking values from SRC_LAT and putting
    2241              :    them into DEST_LAT.  OPND1_TYPE, if non-NULL, is the expected type for the
    2242              :    values in SRC_LAT.  OPND2 is a constant value if transformation is a binary
    2243              :    operation.  SRC_OFFSET specifies offset in an aggregate if SRC_LAT describes
    2244              :    lattice of a part of an aggregate, otherwise it should be -1.  SRC_IDX is
    2245              :    the index of the source parameter.  OP_TYPE is the type in which the
    2246              :    operation is performed and can be NULL when OPCODE is NOP_EXPR.  RES_TYPE is
    2247              :    the value type of result being propagated into.  Return true if DEST_LAT
    2248              :    changed.  */
    2249              : 
    2250              : static bool
    2251        76745 : propagate_vals_across_arith_jfunc (cgraph_edge *cs,
    2252              :                                    enum tree_code opcode,
    2253              :                                    tree opnd1_type,
    2254              :                                    tree opnd2,
    2255              :                                    ipcp_lattice<tree> *src_lat,
    2256              :                                    ipcp_lattice<tree> *dest_lat,
    2257              :                                    HOST_WIDE_INT src_offset,
    2258              :                                    int src_idx,
    2259              :                                    tree op_type,
    2260              :                                    tree res_type)
    2261              : {
    2262        76745 :   ipcp_value<tree> *src_val;
    2263        76745 :   bool ret = false;
    2264              : 
    2265              :   /* Due to circular dependencies, propagating within an SCC through arithmetic
    2266              :      transformation would create infinite number of values.  But for
    2267              :      self-feeding recursive function, we could allow propagation in a limited
    2268              :      count, and this can enable a simple kind of recursive function versioning.
    2269              :      For other scenario, we would just make lattices bottom.  */
    2270        76745 :   if (opcode != NOP_EXPR && ipa_edge_within_scc (cs))
    2271              :     {
    2272         2184 :       int i;
    2273              : 
    2274         2184 :       int max_recursive_depth = opt_for_fn(cs->caller->decl,
    2275              :                                            param_ipa_cp_max_recursive_depth);
    2276         2184 :       if (src_lat != dest_lat || max_recursive_depth < 1)
    2277         1666 :         return dest_lat->set_contains_variable ();
    2278              : 
    2279              :       /* No benefit if recursive execution is in low probability.  */
    2280         1300 :       if (cs->sreal_frequency () * 100
    2281         2600 :           <= ((sreal) 1) * opt_for_fn (cs->caller->decl,
    2282              :                                        param_ipa_cp_min_recursive_probability))
    2283           89 :         return dest_lat->set_contains_variable ();
    2284              : 
    2285         1211 :       auto_vec<ipcp_value<tree> *, 8> val_seeds;
    2286              : 
    2287         2258 :       for (src_val = src_lat->values; src_val; src_val = src_val->next)
    2288              :         {
    2289              :           /* Now we do not use self-recursively generated value as propagation
    2290              :              source, this is absolutely conservative, but could avoid explosion
    2291              :              of lattice's value space, especially when one recursive function
    2292              :              calls another recursive.  */
    2293         1740 :           if (src_val->self_recursion_generated_p ())
    2294              :             {
    2295          909 :               ipcp_value_source<tree> *s;
    2296              : 
    2297              :               /* If the lattice has already been propagated for the call site,
    2298              :                  no need to do that again.  */
    2299         1422 :               for (s = src_val->sources; s; s = s->next)
    2300         1206 :                 if (s->cs == cs)
    2301          693 :                   return dest_lat->set_contains_variable ();
    2302              :             }
    2303              :           else
    2304          831 :             val_seeds.safe_push (src_val);
    2305              :         }
    2306              : 
    2307         1036 :       gcc_assert ((int) val_seeds.length () <= param_ipa_cp_value_list_size);
    2308              : 
    2309              :       /* Recursively generate lattice values with a limited count.  */
    2310          836 :       FOR_EACH_VEC_ELT (val_seeds, i, src_val)
    2311              :         {
    2312         1416 :           for (int j = 1; j < max_recursive_depth; j++)
    2313              :             {
    2314         1261 :               tree cstval = get_val_across_arith_op (opcode, opnd1_type, opnd2,
    2315              :                                                      src_val, op_type);
    2316         1261 :               cstval = ipacp_value_safe_for_type (res_type, cstval);
    2317         1261 :               if (!cstval)
    2318              :                 break;
    2319              : 
    2320         1257 :               ret |= dest_lat->add_value (cstval, cs, src_val, src_idx,
    2321              :                                           src_offset, &src_val, j);
    2322         1257 :               gcc_checking_assert (src_val);
    2323              :             }
    2324              :         }
    2325          518 :       ret |= dest_lat->set_contains_variable ();
    2326         1211 :     }
    2327              :   else
    2328        94817 :     for (src_val = src_lat->values; src_val; src_val = src_val->next)
    2329              :       {
    2330              :         /* Now we do not use self-recursively generated value as propagation
    2331              :            source, otherwise it is easy to make value space of normal lattice
    2332              :            overflow.  */
    2333        20256 :         if (src_val->self_recursion_generated_p ())
    2334              :           {
    2335          125 :             ret |= dest_lat->set_contains_variable ();
    2336          125 :             continue;
    2337              :           }
    2338              : 
    2339        20131 :         tree cstval = get_val_across_arith_op (opcode, opnd1_type, opnd2,
    2340              :                                                src_val, op_type);
    2341        20131 :         cstval = ipacp_value_safe_for_type (res_type, cstval);
    2342        20131 :         if (cstval)
    2343        19930 :           ret |= dest_lat->add_value (cstval, cs, src_val, src_idx,
    2344              :                                       src_offset);
    2345              :         else
    2346          201 :           ret |= dest_lat->set_contains_variable ();
    2347              :       }
    2348              : 
    2349              :   return ret;
    2350              : }
    2351              : 
    2352              : /* Propagate values through a pass-through jump function JFUNC associated with
    2353              :    edge CS, taking values from SRC_LAT and putting them into DEST_LAT.  SRC_IDX
    2354              :    is the index of the source parameter.  PARM_TYPE is the type of the
    2355              :    parameter to which the result is passed.  */
    2356              : 
    2357              : static bool
    2358        72130 : propagate_vals_across_pass_through (cgraph_edge *cs, ipa_jump_func *jfunc,
    2359              :                                     ipcp_lattice<tree> *src_lat,
    2360              :                                     ipcp_lattice<tree> *dest_lat, int src_idx,
    2361              :                                     tree parm_type)
    2362              : {
    2363        72130 :   gcc_checking_assert (parm_type);
    2364        72130 :   enum tree_code opcode = ipa_get_jf_pass_through_operation (jfunc);
    2365        72130 :   tree op_type = (opcode == NOP_EXPR) ? NULL_TREE
    2366         2429 :     : ipa_get_jf_pass_through_op_type (jfunc);
    2367        72130 :   return propagate_vals_across_arith_jfunc (cs, opcode, NULL_TREE,
    2368              :                                 ipa_get_jf_pass_through_operand (jfunc),
    2369              :                                 src_lat, dest_lat, -1, src_idx, op_type,
    2370        72130 :                                 parm_type);
    2371              : }
    2372              : 
    2373              : /* Propagate values through an ancestor jump function JFUNC associated with
    2374              :    edge CS, taking values from SRC_LAT and putting them into DEST_LAT.  SRC_IDX
    2375              :    is the index of the source parameter.  */
    2376              : 
    2377              : static bool
    2378         2203 : propagate_vals_across_ancestor (struct cgraph_edge *cs,
    2379              :                                 struct ipa_jump_func *jfunc,
    2380              :                                 ipcp_lattice<tree> *src_lat,
    2381              :                                 ipcp_lattice<tree> *dest_lat, int src_idx,
    2382              :                                 tree param_type)
    2383              : {
    2384         2203 :   ipcp_value<tree> *src_val;
    2385         2203 :   bool ret = false;
    2386              : 
    2387         2203 :   if (ipa_edge_within_scc (cs))
    2388           14 :     return dest_lat->set_contains_variable ();
    2389              : 
    2390         2500 :   for (src_val = src_lat->values; src_val; src_val = src_val->next)
    2391              :     {
    2392          311 :       tree t = ipa_get_jf_ancestor_result (jfunc, src_val->value);
    2393          311 :       t = ipacp_value_safe_for_type (param_type, t);
    2394          311 :       if (t)
    2395          253 :         ret |= dest_lat->add_value (t, cs, src_val, src_idx);
    2396              :       else
    2397           58 :         ret |= dest_lat->set_contains_variable ();
    2398              :     }
    2399              : 
    2400              :   return ret;
    2401              : }
    2402              : 
    2403              : /* Propagate scalar values across jump function JFUNC that is associated with
    2404              :    edge CS and put the values into DEST_LAT.  PARM_TYPE is the type of the
    2405              :    parameter to which the result is passed.  */
    2406              : 
    2407              : static bool
    2408      3845318 : propagate_scalar_across_jump_function (struct cgraph_edge *cs,
    2409              :                                        struct ipa_jump_func *jfunc,
    2410              :                                        ipcp_lattice<tree> *dest_lat,
    2411              :                                        tree param_type)
    2412              : {
    2413      3845318 :   if (dest_lat->bottom)
    2414              :     return false;
    2415              : 
    2416       819190 :   if (jfunc->type == IPA_JF_CONST)
    2417              :     {
    2418       369970 :       tree val = ipa_get_jf_constant (jfunc);
    2419       369970 :       val = ipacp_value_safe_for_type (param_type, val);
    2420       369970 :       if (val)
    2421       369953 :         return dest_lat->add_value (val, cs, NULL, 0);
    2422              :       else
    2423           17 :         return dest_lat->set_contains_variable ();
    2424              :     }
    2425       449220 :   else if (jfunc->type == IPA_JF_PASS_THROUGH
    2426       269134 :            || jfunc->type == IPA_JF_ANCESTOR)
    2427              :     {
    2428       184525 :       ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
    2429       184525 :       ipcp_lattice<tree> *src_lat;
    2430       184525 :       int src_idx;
    2431       184525 :       bool ret;
    2432              : 
    2433       184525 :       if (jfunc->type == IPA_JF_PASS_THROUGH)
    2434       180086 :         src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
    2435              :       else
    2436         4439 :         src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
    2437              : 
    2438       184525 :       src_lat = ipa_get_scalar_lat (caller_info, src_idx);
    2439       184525 :       if (src_lat->bottom)
    2440       110044 :         return dest_lat->set_contains_variable ();
    2441              : 
    2442              :       /* If we would need to clone the caller and cannot, do not propagate.  */
    2443        74481 :       if (!ipcp_versionable_function_p (cs->caller)
    2444        74481 :           && (src_lat->contains_variable
    2445          132 :               || (src_lat->values_count > 1)))
    2446          148 :         return dest_lat->set_contains_variable ();
    2447              : 
    2448        74333 :       if (jfunc->type == IPA_JF_PASS_THROUGH)
    2449        72130 :         ret = propagate_vals_across_pass_through (cs, jfunc, src_lat,
    2450              :                                                   dest_lat, src_idx,
    2451              :                                                   param_type);
    2452              :       else
    2453         2203 :         ret = propagate_vals_across_ancestor (cs, jfunc, src_lat, dest_lat,
    2454              :                                               src_idx, param_type);
    2455              : 
    2456        74333 :       if (src_lat->contains_variable)
    2457        64950 :         ret |= dest_lat->set_contains_variable ();
    2458              : 
    2459        74333 :       return ret;
    2460              :     }
    2461              : 
    2462              :   /* TODO: We currently do not handle member method pointers in IPA-CP (we only
    2463              :      use it for indirect inlining), we should propagate them too.  */
    2464       264695 :   return dest_lat->set_contains_variable ();
    2465              : }
    2466              : 
    2467              : /* Propagate scalar values across jump function JFUNC that is associated with
    2468              :    edge CS and describes argument IDX and put the values into DEST_LAT.  */
    2469              : 
    2470              : static bool
    2471      3845318 : propagate_context_across_jump_function (cgraph_edge *cs,
    2472              :                           ipa_jump_func *jfunc, int idx,
    2473              :                           ipcp_lattice<ipa_polymorphic_call_context> *dest_lat)
    2474              : {
    2475      3845318 :   if (dest_lat->bottom)
    2476              :     return false;
    2477       915672 :   ipa_edge_args *args = ipa_edge_args_sum->get (cs);
    2478       915672 :   bool ret = false;
    2479       915672 :   bool added_sth = false;
    2480       915672 :   bool type_preserved = true;
    2481              : 
    2482       915672 :   ipa_polymorphic_call_context edge_ctx, *edge_ctx_ptr
    2483       929567 :     = ipa_get_ith_polymorhic_call_context (args, idx);
    2484              : 
    2485        13895 :   if (edge_ctx_ptr)
    2486        13895 :     edge_ctx = *edge_ctx_ptr;
    2487              : 
    2488       915672 :   if (jfunc->type == IPA_JF_PASS_THROUGH
    2489       735095 :       || jfunc->type == IPA_JF_ANCESTOR)
    2490              :     {
    2491       185112 :       ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
    2492       185112 :       int src_idx;
    2493       185112 :       ipcp_lattice<ipa_polymorphic_call_context> *src_lat;
    2494              : 
    2495              :       /* TODO: Once we figure out how to propagate speculations, it will
    2496              :          probably be a good idea to switch to speculation if type_preserved is
    2497              :          not set instead of punting.  */
    2498       185112 :       if (jfunc->type == IPA_JF_PASS_THROUGH)
    2499              :         {
    2500       180577 :           if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
    2501         7580 :             goto prop_fail;
    2502       172997 :           type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc);
    2503       172997 :           src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
    2504              :         }
    2505              :       else
    2506              :         {
    2507         4535 :           type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc);
    2508         4535 :           src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
    2509              :         }
    2510              : 
    2511       177532 :       src_lat = ipa_get_poly_ctx_lat (caller_info, src_idx);
    2512              :       /* If we would need to clone the caller and cannot, do not propagate.  */
    2513       177532 :       if (!ipcp_versionable_function_p (cs->caller)
    2514       177532 :           && (src_lat->contains_variable
    2515        14005 :               || (src_lat->values_count > 1)))
    2516         2485 :         goto prop_fail;
    2517              : 
    2518       175047 :       ipcp_value<ipa_polymorphic_call_context> *src_val;
    2519       176353 :       for (src_val = src_lat->values; src_val; src_val = src_val->next)
    2520              :         {
    2521         1306 :           ipa_polymorphic_call_context cur = src_val->value;
    2522              : 
    2523         1306 :           if (!type_preserved)
    2524          871 :             cur.possible_dynamic_type_change (cs->in_polymorphic_cdtor);
    2525         1306 :           if (jfunc->type == IPA_JF_ANCESTOR)
    2526          325 :             cur.offset_by (ipa_get_jf_ancestor_offset (jfunc));
    2527              :           /* TODO: In cases we know how the context is going to be used,
    2528              :              we can improve the result by passing proper OTR_TYPE.  */
    2529         1306 :           cur.combine_with (edge_ctx);
    2530         2612 :           if (!cur.useless_p ())
    2531              :             {
    2532          847 :               if (src_lat->contains_variable
    2533          847 :                   && !edge_ctx.equal_to (cur))
    2534          269 :                 ret |= dest_lat->set_contains_variable ();
    2535          847 :               ret |= dest_lat->add_value (cur, cs, src_val, src_idx);
    2536          847 :               added_sth = true;
    2537              :             }
    2538              :         }
    2539              :     }
    2540              : 
    2541       730560 :  prop_fail:
    2542       185112 :   if (!added_sth)
    2543              :     {
    2544       914888 :       if (!edge_ctx.useless_p ())
    2545         8487 :         ret |= dest_lat->add_value (edge_ctx, cs);
    2546              :       else
    2547       906401 :         ret |= dest_lat->set_contains_variable ();
    2548              :     }
    2549              : 
    2550              :   return ret;
    2551              : }
    2552              : 
    2553              : /* Propagate bits across jfunc that is associated with
    2554              :    edge cs and update dest_lattice accordingly.  */
    2555              : 
    2556              : bool
    2557      3845318 : propagate_bits_across_jump_function (cgraph_edge *cs, int idx,
    2558              :                                      ipa_jump_func *jfunc,
    2559              :                                      ipcp_bits_lattice *dest_lattice)
    2560              : {
    2561      3845318 :   if (dest_lattice->bottom_p ())
    2562              :     return false;
    2563              : 
    2564       531588 :   enum availability availability;
    2565       531588 :   cgraph_node *callee = cs->callee->function_symbol (&availability);
    2566       531588 :   ipa_node_params *callee_info = ipa_node_params_sum->get (callee);
    2567       531588 :   tree parm_type = ipa_get_type (callee_info, idx);
    2568              : 
    2569              :   /* For K&R C programs, ipa_get_type() could return NULL_TREE.  Avoid the
    2570              :      transform for these cases.  Similarly, we can have bad type mismatches
    2571              :      with LTO, avoid doing anything with those too.  */
    2572       531588 :   if (!parm_type
    2573       531588 :       || (!INTEGRAL_TYPE_P (parm_type) && !POINTER_TYPE_P (parm_type)))
    2574              :     {
    2575        29210 :       if (dump_file && (dump_flags & TDF_DETAILS))
    2576           11 :         fprintf (dump_file, "Setting dest_lattice to bottom, because type of "
    2577              :                  "param %i of %s is NULL or unsuitable for bits propagation\n",
    2578           11 :                  idx, cs->callee->dump_name ());
    2579              : 
    2580        29210 :       return dest_lattice->set_to_bottom ();
    2581              :     }
    2582              : 
    2583       502378 :   if (jfunc->type == IPA_JF_PASS_THROUGH
    2584       404710 :       || jfunc->type == IPA_JF_ANCESTOR)
    2585              :     {
    2586       100145 :       ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
    2587       100145 :       tree operand = NULL_TREE;
    2588       100145 :       tree op_type = NULL_TREE;
    2589       100145 :       enum tree_code code;
    2590       100145 :       unsigned src_idx;
    2591       100145 :       bool keep_null = false;
    2592              : 
    2593       100145 :       if (jfunc->type == IPA_JF_PASS_THROUGH)
    2594              :         {
    2595        97668 :           code = ipa_get_jf_pass_through_operation (jfunc);
    2596        97668 :           src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
    2597        97668 :           if (code != NOP_EXPR)
    2598              :             {
    2599         2069 :               operand = ipa_get_jf_pass_through_operand (jfunc);
    2600         2069 :               op_type = ipa_get_jf_pass_through_op_type (jfunc);
    2601              :             }
    2602              :         }
    2603              :       else
    2604              :         {
    2605         2477 :           code = POINTER_PLUS_EXPR;
    2606         2477 :           src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
    2607         2477 :           unsigned HOST_WIDE_INT offset
    2608         2477 :             = ipa_get_jf_ancestor_offset (jfunc) / BITS_PER_UNIT;
    2609         2477 :           keep_null = (ipa_get_jf_ancestor_keep_null (jfunc) || !offset);
    2610         2477 :           operand = build_int_cstu (size_type_node, offset);
    2611              :         }
    2612              : 
    2613       100145 :       class ipcp_param_lattices *src_lats
    2614       100145 :         = ipa_get_parm_lattices (caller_info, src_idx);
    2615              : 
    2616              :       /* Try to propagate bits if src_lattice is bottom, but jfunc is known.
    2617              :          for eg consider:
    2618              :          int f(int x)
    2619              :          {
    2620              :            g (x & 0xff);
    2621              :          }
    2622              :          Assume lattice for x is bottom, however we can still propagate
    2623              :          result of x & 0xff == 0xff, which gets computed during ccp1 pass
    2624              :          and we store it in jump function during analysis stage.  */
    2625              : 
    2626       100145 :       if (!src_lats->bits_lattice.bottom_p ()
    2627       100145 :           && !src_lats->bits_lattice.recipient_only_p ())
    2628              :         {
    2629        21452 :           if (!op_type)
    2630        20277 :             op_type = ipa_get_type (caller_info, src_idx);
    2631              : 
    2632        21452 :           unsigned precision = TYPE_PRECISION (op_type);
    2633        21452 :           signop sgn = TYPE_SIGN (op_type);
    2634        21452 :           bool drop_all_ones
    2635        21452 :             = keep_null && !src_lats->bits_lattice.known_nonzero_p ();
    2636              : 
    2637        21452 :           return dest_lattice->meet_with (src_lats->bits_lattice, precision,
    2638        21452 :                                           sgn, code, operand, drop_all_ones);
    2639              :         }
    2640              :     }
    2641              : 
    2642       480926 :   value_range vr (parm_type);
    2643       480926 :   if (jfunc->m_vr)
    2644              :     {
    2645       410217 :       jfunc->m_vr->get_vrange (vr);
    2646       410217 :       if (!vr.undefined_p () && !vr.varying_p ())
    2647              :         {
    2648       410217 :           irange_bitmask bm = vr.get_bitmask ();
    2649       410217 :           widest_int mask
    2650       410217 :             = widest_int::from (bm.mask (), TYPE_SIGN (parm_type));
    2651       410217 :           widest_int value
    2652       410217 :             = widest_int::from (bm.value (), TYPE_SIGN (parm_type));
    2653       410217 :           return dest_lattice->meet_with (value, mask,
    2654       410217 :                                           TYPE_PRECISION (parm_type));
    2655       410217 :         }
    2656              :     }
    2657        70709 :   return dest_lattice->set_to_bottom ();
    2658       480926 : }
    2659              : 
    2660              : /* Propagate value range across jump function JFUNC that is associated with
    2661              :    edge CS with param of callee of PARAM_TYPE and update DEST_PLATS
    2662              :    accordingly.  */
    2663              : 
    2664              : static bool
    2665      3844473 : propagate_vr_across_jump_function (cgraph_edge *cs, ipa_jump_func *jfunc,
    2666              :                                    class ipcp_param_lattices *dest_plats,
    2667              :                                    tree param_type)
    2668              : {
    2669      3844473 :   ipcp_vr_lattice *dest_lat = &dest_plats->m_value_range;
    2670              : 
    2671      3844473 :   if (dest_lat->bottom_p ())
    2672              :     return false;
    2673              : 
    2674       624660 :   if (!param_type
    2675       624660 :       || !ipa_vr_supported_type_p (param_type))
    2676        29150 :     return dest_lat->set_to_bottom ();
    2677              : 
    2678       595510 :   value_range vr (param_type);
    2679       595510 :   vr.set_varying (param_type);
    2680       595510 :   if (jfunc->m_vr)
    2681       514018 :     ipa_vr_operation_and_type_effects (vr, *jfunc->m_vr, NOP_EXPR,
    2682              :                                        param_type,
    2683       514018 :                                        jfunc->m_vr->type ());
    2684              : 
    2685       595510 :   if (jfunc->type == IPA_JF_PASS_THROUGH)
    2686              :     {
    2687        91815 :       ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
    2688        91815 :       int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
    2689        91815 :       class ipcp_param_lattices *src_lats
    2690        91815 :         = ipa_get_parm_lattices (caller_info, src_idx);
    2691        91815 :       tree operand_type = ipa_get_type (caller_info, src_idx);
    2692              : 
    2693        91815 :       if (src_lats->m_value_range.bottom_p ()
    2694        91815 :           || src_lats->m_value_range.recipient_only_p ())
    2695        76118 :         return dest_lat->set_to_bottom ();
    2696              : 
    2697        15697 :       if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR
    2698        15697 :           || !ipa_edge_within_scc (cs))
    2699        15202 :         ipa_vr_intersect_with_arith_jfunc (vr, jfunc, cs->caller,
    2700        15202 :                                            src_lats->m_value_range.m_vr,
    2701              :                                            operand_type, param_type);
    2702              :     }
    2703              : 
    2704       519392 :   if (!vr.undefined_p () && !vr.varying_p ())
    2705       491549 :     return dest_lat->meet_with (vr);
    2706              :   else
    2707        27843 :     return dest_lat->set_to_bottom ();
    2708       595510 : }
    2709              : 
    2710              : /* If DEST_PLATS already has aggregate items, check that aggs_by_ref matches
    2711              :    NEW_AGGS_BY_REF and if not, mark all aggs as bottoms and return true (in all
    2712              :    other cases, return false).  If there are no aggregate items, set
    2713              :    aggs_by_ref to NEW_AGGS_BY_REF.  */
    2714              : 
    2715              : static bool
    2716        41868 : set_check_aggs_by_ref (class ipcp_param_lattices *dest_plats,
    2717              :                        bool new_aggs_by_ref)
    2718              : {
    2719            0 :   if (dest_plats->aggs)
    2720              :     {
    2721        22307 :       if (dest_plats->aggs_by_ref != new_aggs_by_ref)
    2722              :         {
    2723            0 :           set_agg_lats_to_bottom (dest_plats);
    2724            0 :           return true;
    2725              :         }
    2726              :     }
    2727              :   else
    2728        19561 :     dest_plats->aggs_by_ref = new_aggs_by_ref;
    2729              :   return false;
    2730              : }
    2731              : 
    2732              : /* Walk aggregate lattices in DEST_PLATS from ***AGLAT on, until ***aglat is an
    2733              :    already existing lattice for the given OFFSET and SIZE, marking all skipped
    2734              :    lattices as containing variable and checking for overlaps.  If there is no
    2735              :    already existing lattice for the OFFSET and VAL_SIZE, create one, initialize
    2736              :    it with offset, size and contains_variable to PRE_EXISTING, and return true,
    2737              :    unless there are too many already.  If there are two many, return false.  If
    2738              :    there are overlaps turn whole DEST_PLATS to bottom and return false.  If any
    2739              :    skipped lattices were newly marked as containing variable, set *CHANGE to
    2740              :    true.  MAX_AGG_ITEMS is the maximum number of lattices.  */
    2741              : 
    2742              : static bool
    2743       112701 : merge_agg_lats_step (class ipcp_param_lattices *dest_plats,
    2744              :                      HOST_WIDE_INT offset, HOST_WIDE_INT val_size,
    2745              :                      struct ipcp_agg_lattice ***aglat,
    2746              :                      bool pre_existing, bool *change, int max_agg_items)
    2747              : {
    2748       112701 :   gcc_checking_assert (offset >= 0);
    2749              : 
    2750       116809 :   while (**aglat && (**aglat)->offset < offset)
    2751              :     {
    2752         4108 :       if ((**aglat)->offset + (**aglat)->size > offset)
    2753              :         {
    2754            0 :           set_agg_lats_to_bottom (dest_plats);
    2755            0 :           return false;
    2756              :         }
    2757         4108 :       *change |= (**aglat)->set_contains_variable ();
    2758         4108 :       *aglat = &(**aglat)->next;
    2759              :     }
    2760              : 
    2761       112701 :   if (**aglat && (**aglat)->offset == offset)
    2762              :     {
    2763        56240 :       if ((**aglat)->size != val_size)
    2764              :         {
    2765           13 :           set_agg_lats_to_bottom (dest_plats);
    2766           13 :           return false;
    2767              :         }
    2768        56227 :       gcc_assert (!(**aglat)->next
    2769              :                   || (**aglat)->next->offset >= offset + val_size);
    2770              :       return true;
    2771              :     }
    2772              :   else
    2773              :     {
    2774        56461 :       struct ipcp_agg_lattice *new_al;
    2775              : 
    2776        56461 :       if (**aglat && (**aglat)->offset < offset + val_size)
    2777              :         {
    2778            3 :           set_agg_lats_to_bottom (dest_plats);
    2779            3 :           return false;
    2780              :         }
    2781        56458 :       if (dest_plats->aggs_count == max_agg_items)
    2782              :         return false;
    2783        56419 :       dest_plats->aggs_count++;
    2784        56419 :       new_al = ipcp_agg_lattice_pool.allocate ();
    2785              : 
    2786        56419 :       new_al->offset = offset;
    2787        56419 :       new_al->size = val_size;
    2788        56419 :       new_al->contains_variable = pre_existing;
    2789              : 
    2790        56419 :       new_al->next = **aglat;
    2791        56419 :       **aglat = new_al;
    2792        56419 :       return true;
    2793              :     }
    2794              : }
    2795              : 
    2796              : /* Set all AGLAT and all other aggregate lattices reachable by next pointers as
    2797              :    containing an unknown value.  */
    2798              : 
    2799              : static bool
    2800        41850 : set_chain_of_aglats_contains_variable (struct ipcp_agg_lattice *aglat)
    2801              : {
    2802        41850 :   bool ret = false;
    2803        44308 :   while (aglat)
    2804              :     {
    2805         2458 :       ret |= aglat->set_contains_variable ();
    2806         2458 :       aglat = aglat->next;
    2807              :     }
    2808        41850 :   return ret;
    2809              : }
    2810              : 
    2811              : /* Merge existing aggregate lattices in SRC_PLATS to DEST_PLATS, subtracting
    2812              :    DELTA_OFFSET.  CS is the call graph edge and SRC_IDX the index of the source
    2813              :    parameter used for lattice value sources.  Return true if DEST_PLATS changed
    2814              :    in any way.  */
    2815              : 
    2816              : static bool
    2817         3984 : merge_aggregate_lattices (struct cgraph_edge *cs,
    2818              :                           class ipcp_param_lattices *dest_plats,
    2819              :                           class ipcp_param_lattices *src_plats,
    2820              :                           int src_idx, HOST_WIDE_INT offset_delta)
    2821              : {
    2822         3984 :   bool pre_existing = dest_plats->aggs != NULL;
    2823         3984 :   struct ipcp_agg_lattice **dst_aglat;
    2824         3984 :   bool ret = false;
    2825              : 
    2826         3984 :   if (set_check_aggs_by_ref (dest_plats, src_plats->aggs_by_ref))
    2827            0 :     return true;
    2828         3984 :   if (src_plats->aggs_bottom)
    2829            2 :     return set_agg_lats_contain_variable (dest_plats);
    2830         3982 :   if (src_plats->aggs_contain_variable)
    2831         2314 :     ret |= set_agg_lats_contain_variable (dest_plats);
    2832         3982 :   dst_aglat = &dest_plats->aggs;
    2833              : 
    2834         3982 :   int max_agg_items = opt_for_fn (cs->callee->function_symbol ()->decl,
    2835              :                                   param_ipa_max_agg_items);
    2836         3982 :   for (struct ipcp_agg_lattice *src_aglat = src_plats->aggs;
    2837        11663 :        src_aglat;
    2838         7681 :        src_aglat = src_aglat->next)
    2839              :     {
    2840         7681 :       HOST_WIDE_INT new_offset = src_aglat->offset - offset_delta;
    2841              : 
    2842         7681 :       if (new_offset < 0)
    2843           51 :         continue;
    2844         7630 :       if (merge_agg_lats_step (dest_plats, new_offset, src_aglat->size,
    2845              :                                &dst_aglat, pre_existing, &ret, max_agg_items))
    2846              :         {
    2847         7626 :           struct ipcp_agg_lattice *new_al = *dst_aglat;
    2848              : 
    2849         7626 :           dst_aglat = &(*dst_aglat)->next;
    2850         7626 :           if (src_aglat->bottom)
    2851              :             {
    2852            0 :               ret |= new_al->set_contains_variable ();
    2853            0 :               continue;
    2854              :             }
    2855         7626 :           if (src_aglat->contains_variable)
    2856         4444 :             ret |= new_al->set_contains_variable ();
    2857         7626 :           for (ipcp_value<tree> *val = src_aglat->values;
    2858        11827 :                val;
    2859         4201 :                val = val->next)
    2860         4201 :             ret |= new_al->add_value (val->value, cs, val, src_idx,
    2861              :                                       src_aglat->offset);
    2862              :         }
    2863            4 :       else if (dest_plats->aggs_bottom)
    2864              :         return true;
    2865              :     }
    2866         3982 :   ret |= set_chain_of_aglats_contains_variable (*dst_aglat);
    2867         3982 :   return ret;
    2868              : }
    2869              : 
    2870              : /* Determine whether there is anything to propagate FROM SRC_PLATS through a
    2871              :    pass-through JFUNC and if so, whether it has conform and conforms to the
    2872              :    rules about propagating values passed by reference.  */
    2873              : 
    2874              : static bool
    2875       172838 : agg_pass_through_permissible_p (class ipcp_param_lattices *src_plats,
    2876              :                                 struct ipa_jump_func *jfunc)
    2877              : {
    2878       172838 :   return src_plats->aggs
    2879       172838 :     && (!src_plats->aggs_by_ref
    2880         5019 :         || ipa_get_jf_pass_through_agg_preserved (jfunc));
    2881              : }
    2882              : 
    2883              : /* Propagate values through ITEM, jump function for a part of an aggregate,
    2884              :    into corresponding aggregate lattice AGLAT.  CS is the call graph edge
    2885              :    associated with the jump function.  Return true if AGLAT changed in any
    2886              :    way.  */
    2887              : 
    2888              : static bool
    2889       105020 : propagate_aggregate_lattice (struct cgraph_edge *cs,
    2890              :                              struct ipa_agg_jf_item *item,
    2891              :                              struct ipcp_agg_lattice *aglat)
    2892              : {
    2893       105020 :   class ipa_node_params *caller_info;
    2894       105020 :   class ipcp_param_lattices *src_plats;
    2895       105020 :   struct ipcp_lattice<tree> *src_lat;
    2896       105020 :   HOST_WIDE_INT src_offset;
    2897       105020 :   int src_idx;
    2898       105020 :   tree load_type;
    2899       105020 :   bool ret;
    2900              : 
    2901       105020 :   if (item->jftype == IPA_JF_CONST)
    2902              :     {
    2903        93678 :       tree value = item->value.constant;
    2904              : 
    2905        93678 :       gcc_checking_assert (is_gimple_ip_invariant (value));
    2906        93678 :       return aglat->add_value (value, cs, NULL, 0);
    2907              :     }
    2908              : 
    2909        11342 :   gcc_checking_assert (item->jftype == IPA_JF_PASS_THROUGH
    2910              :                        || item->jftype == IPA_JF_LOAD_AGG);
    2911              : 
    2912        11342 :   caller_info = ipa_node_params_sum->get (cs->caller);
    2913        11342 :   src_idx = item->value.pass_through.formal_id;
    2914        11342 :   src_plats = ipa_get_parm_lattices (caller_info, src_idx);
    2915              : 
    2916        11342 :   if (item->jftype == IPA_JF_PASS_THROUGH)
    2917              :     {
    2918         3294 :       load_type = NULL_TREE;
    2919         3294 :       src_lat = &src_plats->itself;
    2920         3294 :       src_offset = -1;
    2921              :     }
    2922              :   else
    2923              :     {
    2924         8048 :       HOST_WIDE_INT load_offset = item->value.load_agg.offset;
    2925         8048 :       struct ipcp_agg_lattice *src_aglat;
    2926              : 
    2927        12452 :       for (src_aglat = src_plats->aggs; src_aglat; src_aglat = src_aglat->next)
    2928         8178 :         if (src_aglat->offset >= load_offset)
    2929              :           break;
    2930              : 
    2931         8048 :       load_type = item->value.load_agg.type;
    2932         8048 :       if (!src_aglat
    2933         3774 :           || src_aglat->offset > load_offset
    2934         3432 :           || src_aglat->size != tree_to_shwi (TYPE_SIZE (load_type))
    2935        11480 :           || src_plats->aggs_by_ref != item->value.load_agg.by_ref)
    2936         4616 :         return aglat->set_contains_variable ();
    2937              : 
    2938              :       src_lat = src_aglat;
    2939              :       src_offset = load_offset;
    2940              :     }
    2941              : 
    2942         6726 :   if (src_lat->bottom
    2943         6726 :       || (!ipcp_versionable_function_p (cs->caller)
    2944         6726 :           && !src_lat->is_single_const ()))
    2945         2111 :     return aglat->set_contains_variable ();
    2946              : 
    2947         4615 :   ret = propagate_vals_across_arith_jfunc (cs,
    2948              :                                            item->value.pass_through.operation,
    2949              :                                            load_type,
    2950              :                                            item->value.pass_through.operand,
    2951              :                                            src_lat, aglat,
    2952              :                                            src_offset,
    2953              :                                            src_idx,
    2954              :                                            item->value.pass_through.op_type,
    2955              :                                            item->type);
    2956              : 
    2957         4615 :   if (src_lat->contains_variable)
    2958         2640 :     ret |= aglat->set_contains_variable ();
    2959              : 
    2960              :   return ret;
    2961              : }
    2962              : 
    2963              : /* Propagate scalar values across jump function JFUNC that is associated with
    2964              :    edge CS and put the values into DEST_LAT.  */
    2965              : 
    2966              : static bool
    2967      3845318 : propagate_aggs_across_jump_function (struct cgraph_edge *cs,
    2968              :                                      struct ipa_jump_func *jfunc,
    2969              :                                      class ipcp_param_lattices *dest_plats)
    2970              : {
    2971      3845318 :   bool ret = false;
    2972              : 
    2973      3845318 :   if (dest_plats->aggs_bottom)
    2974              :     return false;
    2975              : 
    2976       914511 :   if (jfunc->type == IPA_JF_PASS_THROUGH
    2977       914511 :       && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
    2978              :     {
    2979       172838 :       ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
    2980       172838 :       int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
    2981       172838 :       class ipcp_param_lattices *src_plats;
    2982              : 
    2983       172838 :       src_plats = ipa_get_parm_lattices (caller_info, src_idx);
    2984       172838 :       if (agg_pass_through_permissible_p (src_plats, jfunc))
    2985              :         {
    2986              :           /* Currently we do not produce clobber aggregate jump
    2987              :              functions, replace with merging when we do.  */
    2988         3854 :           gcc_assert (!jfunc->agg.items);
    2989         3854 :           ret |= merge_aggregate_lattices (cs, dest_plats, src_plats,
    2990              :                                            src_idx, 0);
    2991         3854 :           return ret;
    2992              :         }
    2993              :     }
    2994       741673 :   else if (jfunc->type == IPA_JF_ANCESTOR
    2995       741673 :            && ipa_get_jf_ancestor_agg_preserved (jfunc))
    2996              :     {
    2997         1191 :       ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
    2998         1191 :       int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
    2999         1191 :       class ipcp_param_lattices *src_plats;
    3000              : 
    3001         1191 :       src_plats = ipa_get_parm_lattices (caller_info, src_idx);
    3002         1191 :       if (src_plats->aggs && src_plats->aggs_by_ref)
    3003              :         {
    3004              :           /* Currently we do not produce clobber aggregate jump
    3005              :              functions, replace with merging when we do.  */
    3006          130 :           gcc_assert (!jfunc->agg.items);
    3007          130 :           ret |= merge_aggregate_lattices (cs, dest_plats, src_plats, src_idx,
    3008              :                                            ipa_get_jf_ancestor_offset (jfunc));
    3009              :         }
    3010         1061 :       else if (!src_plats->aggs_by_ref)
    3011         1057 :         ret |= set_agg_lats_to_bottom (dest_plats);
    3012              :       else
    3013            4 :         ret |= set_agg_lats_contain_variable (dest_plats);
    3014         1191 :       return ret;
    3015              :     }
    3016              : 
    3017       909466 :   if (jfunc->agg.items)
    3018              :     {
    3019        37884 :       bool pre_existing = dest_plats->aggs != NULL;
    3020        37884 :       struct ipcp_agg_lattice **aglat = &dest_plats->aggs;
    3021        37884 :       struct ipa_agg_jf_item *item;
    3022        37884 :       int i;
    3023              : 
    3024        37884 :       if (set_check_aggs_by_ref (dest_plats, jfunc->agg.by_ref))
    3025           16 :         return true;
    3026              : 
    3027        37884 :       int max_agg_items = opt_for_fn (cs->callee->function_symbol ()->decl,
    3028              :                                       param_ipa_max_agg_items);
    3029       143260 :       FOR_EACH_VEC_ELT (*jfunc->agg.items, i, item)
    3030              :         {
    3031       105392 :           HOST_WIDE_INT val_size;
    3032              : 
    3033       105392 :           if (item->offset < 0 || item->jftype == IPA_JF_UNKNOWN)
    3034          321 :             continue;
    3035       105071 :           val_size = tree_to_shwi (TYPE_SIZE (item->type));
    3036              : 
    3037       105071 :           if (merge_agg_lats_step (dest_plats, item->offset, val_size,
    3038              :                                    &aglat, pre_existing, &ret, max_agg_items))
    3039              :             {
    3040       105020 :               ret |= propagate_aggregate_lattice (cs, item, *aglat);
    3041       105020 :               aglat = &(*aglat)->next;
    3042              :             }
    3043           51 :           else if (dest_plats->aggs_bottom)
    3044              :             return true;
    3045              :         }
    3046              : 
    3047        75736 :       ret |= set_chain_of_aglats_contains_variable (*aglat);
    3048              :     }
    3049              :   else
    3050       871582 :     ret |= set_agg_lats_contain_variable (dest_plats);
    3051              : 
    3052       909450 :   return ret;
    3053              : }
    3054              : 
    3055              : /* Return true if on the way cfrom CS->caller to the final (non-alias and
    3056              :    non-thunk) destination, the call passes through a thunk.  */
    3057              : 
    3058              : static bool
    3059      1915750 : call_passes_through_thunk (cgraph_edge *cs)
    3060              : {
    3061      1915750 :   cgraph_node *alias_or_thunk = cs->callee;
    3062      2054454 :   while (alias_or_thunk->alias)
    3063       138704 :     alias_or_thunk = alias_or_thunk->get_alias_target ();
    3064      1915750 :   return alias_or_thunk->thunk;
    3065              : }
    3066              : 
    3067              : /* Propagate constants from the caller to the callee of CS.  INFO describes the
    3068              :    caller.  */
    3069              : 
    3070              : static bool
    3071      5275314 : propagate_constants_across_call (struct cgraph_edge *cs)
    3072              : {
    3073      5275314 :   class ipa_node_params *callee_info;
    3074      5275314 :   enum availability availability;
    3075      5275314 :   cgraph_node *callee;
    3076      5275314 :   class ipa_edge_args *args;
    3077      5275314 :   bool ret = false;
    3078      5275314 :   int i, args_count, parms_count;
    3079              : 
    3080      5275314 :   callee = cs->callee->function_symbol (&availability);
    3081      5275314 :   if (!callee->definition)
    3082              :     return false;
    3083      1938168 :   gcc_checking_assert (callee->has_gimple_body_p ());
    3084      1938168 :   callee_info = ipa_node_params_sum->get (callee);
    3085      1938168 :   if (!callee_info)
    3086              :     return false;
    3087              : 
    3088      1929682 :   args = ipa_edge_args_sum->get (cs);
    3089      1929682 :   parms_count = ipa_get_param_count (callee_info);
    3090      1736733 :   if (parms_count == 0)
    3091              :     return false;
    3092      1736733 :   if (!args
    3093      1736447 :       || !opt_for_fn (cs->caller->decl, flag_ipa_cp)
    3094      3473180 :       || !opt_for_fn (cs->caller->decl, optimize))
    3095              :     {
    3096          857 :       for (i = 0; i < parms_count; i++)
    3097          571 :         ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
    3098              :                                                                  i));
    3099              :       return ret;
    3100              :     }
    3101      1736447 :   args_count = ipa_get_cs_argument_count (args);
    3102              : 
    3103              :   /* If this call goes through a thunk we must not propagate to the first (0th)
    3104              :      parameter.  However, we might need to uncover a thunk from below a series
    3105              :      of aliases first.  */
    3106      1736447 :   if (call_passes_through_thunk (cs))
    3107              :     {
    3108          227 :       ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
    3109              :                                                                0));
    3110          227 :       i = 1;
    3111              :     }
    3112              :   else
    3113              :     i = 0;
    3114              : 
    3115      5721539 :   for (; (i < args_count) && (i < parms_count); i++)
    3116              :     {
    3117      3985092 :       struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
    3118      3985092 :       class ipcp_param_lattices *dest_plats;
    3119      3985092 :       tree param_type = ipa_get_type (callee_info, i);
    3120              : 
    3121      3985092 :       dest_plats = ipa_get_parm_lattices (callee_info, i);
    3122      3985092 :       if (availability == AVAIL_INTERPOSABLE)
    3123       139774 :         ret |= set_all_contains_variable (dest_plats);
    3124              :       else
    3125              :         {
    3126      3845318 :           ret |= propagate_scalar_across_jump_function (cs, jump_func,
    3127              :                                                         &dest_plats->itself,
    3128              :                                                         param_type);
    3129      3845318 :           ret |= propagate_context_across_jump_function (cs, jump_func, i,
    3130              :                                                          &dest_plats->ctxlat);
    3131      3845318 :           ret
    3132      3845318 :             |= propagate_bits_across_jump_function (cs, i, jump_func,
    3133              :                                                     &dest_plats->bits_lattice);
    3134      3845318 :           ret |= propagate_aggs_across_jump_function (cs, jump_func,
    3135              :                                                       dest_plats);
    3136      3845318 :           if (opt_for_fn (callee->decl, flag_ipa_vrp))
    3137      3844473 :             ret |= propagate_vr_across_jump_function (cs, jump_func,
    3138              :                                                       dest_plats, param_type);
    3139              :           else
    3140          845 :             ret |= dest_plats->m_value_range.set_to_bottom ();
    3141              :         }
    3142              :     }
    3143      1736630 :   for (; i < parms_count; i++)
    3144          183 :     ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info, i));
    3145              : 
    3146              :   return ret;
    3147              : }
    3148              : 
    3149              : /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
    3150              :    KNOWN_CONTEXTS, and known aggregates either in AVS or KNOWN_AGGS return
    3151              :    the destination.  The latter three can be NULL.  If AGG_REPS is not NULL,
    3152              :    KNOWN_AGGS is ignored.  */
    3153              : 
    3154              : static tree
    3155      1565975 : ipa_get_indirect_edge_target_1 (struct cgraph_edge *ie,
    3156              :                                 const vec<tree> &known_csts,
    3157              :                                 const vec<ipa_polymorphic_call_context> &known_contexts,
    3158              :                                 const ipa_argagg_value_list &avs,
    3159              :                                 bool *speculative)
    3160              : {
    3161      1565975 :   int param_index = ie->indirect_info->param_index;
    3162      1565975 :   *speculative = false;
    3163              : 
    3164      1565975 :   if (param_index == -1)
    3165              :     return NULL_TREE;
    3166              : 
    3167       599778 :   if (cgraph_simple_indirect_info *sii
    3168       599778 :       = dyn_cast <cgraph_simple_indirect_info *> (ie->indirect_info))
    3169              :     {
    3170       308174 :       tree t = NULL;
    3171              : 
    3172       308174 :       if (sii->agg_contents)
    3173              :         {
    3174        72015 :           t = NULL;
    3175        72015 :           if ((unsigned) param_index < known_csts.length ()
    3176        72015 :               && known_csts[param_index])
    3177        61478 :             t = ipa_find_agg_cst_from_init (known_csts[param_index],
    3178              :                                             sii->offset,
    3179              :                                             sii->by_ref);
    3180              : 
    3181        72015 :           if (!t && sii->guaranteed_unmodified)
    3182        65873 :             t = avs.get_value (param_index, sii->offset / BITS_PER_UNIT,
    3183              :                                sii->by_ref);
    3184              :         }
    3185       236159 :       else if ((unsigned) param_index < known_csts.length ())
    3186       236159 :         t = known_csts[param_index];
    3187              : 
    3188       308104 :       if (t
    3189       204956 :           && TREE_CODE (t) == ADDR_EXPR
    3190       512841 :           && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
    3191       204737 :         return TREE_OPERAND (t, 0);
    3192              :       else
    3193       103437 :         return NULL_TREE;
    3194              :     }
    3195              : 
    3196       291604 :   if (!opt_for_fn (ie->caller->decl, flag_devirtualize))
    3197              :     return NULL_TREE;
    3198              : 
    3199       291604 :   cgraph_polymorphic_indirect_info *pii
    3200       291604 :     = as_a <cgraph_polymorphic_indirect_info *> (ie->indirect_info);
    3201       291604 :   if (!pii->usable_p ())
    3202              :     return NULL_TREE;
    3203              : 
    3204       291604 :   HOST_WIDE_INT anc_offset = pii->offset;
    3205       291604 :   tree t = NULL;
    3206       291604 :   tree target = NULL;
    3207       291604 :   if ((unsigned) param_index < known_csts.length ()
    3208       291604 :       && known_csts[param_index])
    3209        16977 :     t = ipa_find_agg_cst_from_init (known_csts[param_index], anc_offset, true);
    3210              : 
    3211              :   /* Try to work out value of virtual table pointer value in replacements.  */
    3212              :   /* or known aggregate values.  */
    3213        16977 :   if (!t)
    3214       291595 :     t = avs.get_value (param_index, anc_offset / BITS_PER_UNIT, true);
    3215              : 
    3216              :   /* If we found the virtual table pointer, lookup the target.  */
    3217       291595 :   if (t)
    3218              :     {
    3219         7211 :       tree vtable;
    3220         7211 :       unsigned HOST_WIDE_INT offset;
    3221         7211 :       if (vtable_pointer_value_to_vtable (t, &vtable, &offset))
    3222              :         {
    3223         7211 :           bool can_refer;
    3224         7211 :           target = gimple_get_virt_method_for_vtable (pii->otr_token, vtable,
    3225              :                                                       offset, &can_refer);
    3226         7211 :           if (can_refer)
    3227              :             {
    3228         7148 :               if (!target
    3229         7148 :                   || fndecl_built_in_p (target, BUILT_IN_UNREACHABLE)
    3230        14176 :                   || !possible_polymorphic_call_target_p
    3231         7028 :                        (ie, cgraph_node::get (target)))
    3232              :                 {
    3233              :                   /* Do not speculate builtin_unreachable, it is stupid!  */
    3234          237 :                   if (pii->vptr_changed)
    3235         5679 :                     return NULL;
    3236          237 :                   target = ipa_impossible_devirt_target (ie, target);
    3237              :                 }
    3238         7148 :               *speculative = pii->vptr_changed;
    3239         7148 :               if (!*speculative)
    3240              :                 return target;
    3241              :             }
    3242              :         }
    3243              :     }
    3244              : 
    3245              :   /* Do we know the constant value of pointer?  */
    3246       285925 :   if (!t && (unsigned) param_index < known_csts.length ())
    3247        52902 :     t = known_csts[param_index];
    3248              : 
    3249       285925 :   ipa_polymorphic_call_context context;
    3250       285925 :   if (known_contexts.length () > (unsigned int) param_index)
    3251              :     {
    3252       285553 :       context = known_contexts[param_index];
    3253       285553 :       context.offset_by (anc_offset);
    3254       285553 :       if (pii->vptr_changed)
    3255        48929 :         context.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
    3256              :                                               pii->otr_type);
    3257       285553 :       if (t)
    3258              :         {
    3259        12020 :           ipa_polymorphic_call_context ctx2
    3260        12020 :             = ipa_polymorphic_call_context (t, pii->otr_type, anc_offset);
    3261        24040 :           if (!ctx2.useless_p ())
    3262        10493 :             context.combine_with (ctx2, pii->otr_type);
    3263              :         }
    3264              :     }
    3265          372 :   else if (t)
    3266              :     {
    3267           21 :       context = ipa_polymorphic_call_context (t, pii->otr_type, anc_offset);
    3268           21 :       if (pii->vptr_changed)
    3269            6 :         context.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
    3270              :                                               pii->otr_type);
    3271              :     }
    3272              :   else
    3273              :     return NULL_TREE;
    3274              : 
    3275       285574 :   vec <cgraph_node *>targets;
    3276       285574 :   bool final;
    3277              : 
    3278       285574 :   targets = possible_polymorphic_call_targets (pii->otr_type, pii->otr_token,
    3279              :                                                context, &final);
    3280       297159 :   if (!final || targets.length () > 1)
    3281              :     {
    3282       274687 :       struct cgraph_node *node;
    3283       274687 :       if (*speculative)
    3284              :         return target;
    3285       274655 :       if (!opt_for_fn (ie->caller->decl, flag_devirtualize_speculatively)
    3286       274655 :           || ie->speculative || !ie->maybe_hot_p ())
    3287       188420 :         return NULL;
    3288        86235 :       node = try_speculative_devirtualization (pii->otr_type, pii->otr_token,
    3289              :                                                context);
    3290        86235 :       if (node)
    3291              :         {
    3292          653 :           *speculative = true;
    3293          653 :           target = node->decl;
    3294              :         }
    3295              :       else
    3296              :         return NULL;
    3297              :     }
    3298              :   else
    3299              :     {
    3300        10887 :       *speculative = false;
    3301        10887 :       if (targets.length () == 1)
    3302        10848 :         target = targets[0]->decl;
    3303              :       else
    3304           39 :         target = ipa_impossible_devirt_target (ie, NULL_TREE);
    3305              :     }
    3306              : 
    3307        11540 :   if (target && !possible_polymorphic_call_target_p (ie,
    3308              :                                                      cgraph_node::get (target)))
    3309              :     {
    3310           54 :       if (*speculative)
    3311              :         return NULL;
    3312           40 :       target = ipa_impossible_devirt_target (ie, target);
    3313              :     }
    3314              : 
    3315              :   return target;
    3316              : }
    3317              : 
    3318              : /* If an indirect edge IE can be turned into a direct one based on data in
    3319              :    AVALS, return the destination.  Store into *SPECULATIVE a boolean determinig
    3320              :    whether the discovered target is only speculative guess.  */
    3321              : 
    3322              : tree
    3323      1396545 : ipa_get_indirect_edge_target (struct cgraph_edge *ie,
    3324              :                               ipa_call_arg_values *avals,
    3325              :                               bool *speculative)
    3326              : {
    3327      1396545 :   ipa_argagg_value_list avl (avals);
    3328      1396545 :   return ipa_get_indirect_edge_target_1 (ie, avals->m_known_vals,
    3329      1396545 :                                          avals->m_known_contexts,
    3330      1396545 :                                          avl, speculative);
    3331              : }
    3332              : 
    3333              : /* Calculate devirtualization time bonus for NODE, assuming we know information
    3334              :    about arguments stored in AVALS.
    3335              : 
    3336              :    FIXME: This function will also consider devirtualization of calls that are
    3337              :    known to be dead in the clone.  */
    3338              : 
    3339              : static sreal
    3340      1530947 : devirtualization_time_bonus (struct cgraph_node *node,
    3341              :                              ipa_auto_call_arg_values *avals)
    3342              : {
    3343      1530947 :   struct cgraph_edge *ie;
    3344      1530947 :   sreal res = 0;
    3345              : 
    3346      1698492 :   for (ie = node->indirect_calls; ie; ie = ie->next_callee)
    3347              :     {
    3348       167545 :       struct cgraph_node *callee;
    3349       167545 :       class ipa_fn_summary *isummary;
    3350       167545 :       enum availability avail;
    3351       167545 :       tree target;
    3352       167545 :       bool speculative;
    3353              : 
    3354       167545 :       ipa_argagg_value_list avl (avals);
    3355       167545 :       target = ipa_get_indirect_edge_target_1 (ie, avals->m_known_vals,
    3356              :                                                avals->m_known_contexts,
    3357              :                                                avl, &speculative);
    3358       167545 :       if (!target)
    3359       166563 :         continue;
    3360              : 
    3361              :       /* Only bare minimum benefit for clearly un-inlineable targets.  */
    3362         3244 :       res = res + ie->combined_sreal_frequency ();
    3363         3244 :       callee = cgraph_node::get (target);
    3364         3244 :       if (!callee || !callee->definition)
    3365          636 :         continue;
    3366         2608 :       callee = callee->function_symbol (&avail);
    3367         2608 :       if (avail < AVAIL_AVAILABLE)
    3368            0 :         continue;
    3369         2608 :       isummary = ipa_fn_summaries->get (callee);
    3370         2608 :       if (!isummary || !isummary->inlinable)
    3371           65 :         continue;
    3372              : 
    3373         2543 :       int savings = 0;
    3374         2543 :       int size = ipa_size_summaries->get (callee)->size;
    3375              :       /* FIXME: The values below need re-considering and perhaps also
    3376              :          integrating into the cost metrics, at lest in some very basic way.  */
    3377         2543 :       int max_inline_insns_auto
    3378         2543 :         = opt_for_fn (callee->decl, param_max_inline_insns_auto);
    3379         2543 :       if (size <= max_inline_insns_auto / 4)
    3380          402 :         savings = 31 / ((int)speculative + 1);
    3381         2141 :       else if (size <= max_inline_insns_auto / 2)
    3382          390 :         savings = 15 / ((int)speculative + 1);
    3383         3312 :       else if (size <= max_inline_insns_auto
    3384         1751 :                || DECL_DECLARED_INLINE_P (callee->decl))
    3385          190 :         savings = 7 / ((int)speculative + 1);
    3386              :       else
    3387         1561 :         continue;
    3388          982 :       res = res + ie->combined_sreal_frequency () * (sreal) savings;
    3389              :     }
    3390              : 
    3391      1530947 :   return res;
    3392              : }
    3393              : 
    3394              : /* Return time bonus incurred because of hints stored in ESTIMATES.  */
    3395              : 
    3396              : static sreal
    3397       287895 : hint_time_bonus (cgraph_node *node, const ipa_call_estimates &estimates)
    3398              : {
    3399       287895 :   sreal result = 0;
    3400       287895 :   ipa_hints hints = estimates.hints;
    3401       287895 :   if (hints & (INLINE_HINT_loop_iterations | INLINE_HINT_loop_stride))
    3402        27278 :     result += opt_for_fn (node->decl, param_ipa_cp_loop_hint_bonus);
    3403              : 
    3404       287895 :   sreal bonus_for_one = opt_for_fn (node->decl, param_ipa_cp_loop_hint_bonus);
    3405              : 
    3406       287895 :   if (hints & INLINE_HINT_loop_iterations)
    3407        19018 :     result += estimates.loops_with_known_iterations * bonus_for_one;
    3408              : 
    3409       287895 :   if (hints & INLINE_HINT_loop_stride)
    3410        11003 :     result += estimates.loops_with_known_strides * bonus_for_one;
    3411              : 
    3412       287895 :   return result;
    3413              : }
    3414              : 
    3415              : /* If there is a reason to penalize the function described by INFO in the
    3416              :    cloning goodness evaluation, do so.  */
    3417              : 
    3418              : static inline sreal
    3419       103210 : incorporate_penalties (cgraph_node *node, ipa_node_params *info,
    3420              :                        sreal evaluation)
    3421              : {
    3422       103210 :   if (info->node_within_scc && !info->node_is_self_scc)
    3423         1744 :     evaluation = (evaluation
    3424         1744 :                   * (100 - opt_for_fn (node->decl,
    3425         3488 :                                        param_ipa_cp_recursion_penalty))) / 100;
    3426              : 
    3427       103210 :   if (info->node_calling_single_call)
    3428         6869 :     evaluation = (evaluation
    3429         6869 :                   * (100 - opt_for_fn (node->decl,
    3430         6869 :                                        param_ipa_cp_single_call_penalty)))
    3431        13738 :       / 100;
    3432              : 
    3433       103210 :   return evaluation;
    3434              : }
    3435              : 
    3436              : /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
    3437              :    and SIZE_COST and with the sum of frequencies of incoming edges to the
    3438              :    potential new clone in FREQUENCIES.  CUR_SWEEP is the number of the current
    3439              :    sweep of IPA-CP over the call-graph in the decision stage.  */
    3440              : 
    3441              : static bool
    3442       343303 : good_cloning_opportunity_p (struct cgraph_node *node, sreal time_benefit,
    3443              :                             sreal freq_sum, profile_count count_sum,
    3444              :                             int size_cost, bool called_without_ipa_profile,
    3445              :                             int cur_sweep)
    3446              : {
    3447       343303 :   gcc_assert (count_sum.ipa () == count_sum);
    3448       343303 :   if (count_sum.quality () == AFDO)
    3449            0 :     count_sum = count_sum.force_nonzero ();
    3450       343303 :   if (time_benefit == 0
    3451       290397 :       || !opt_for_fn (node->decl, flag_ipa_cp_clone)
    3452              :       /* If there is no call which was executed in profiling or where
    3453              :          profile is missing, we do not want to clone.  */
    3454       103302 :       || (!called_without_ipa_profile && !count_sum.nonzero_p ()))
    3455              :     {
    3456       240093 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3457           24 :         fprintf (dump_file, "     good_cloning_opportunity_p (time: %g, "
    3458              :                  "size: %i): Definitely not good or prohibited.\n",
    3459              :                  time_benefit.to_double (), size_cost);
    3460       240093 :       return false;
    3461              :     }
    3462              : 
    3463       103210 :   gcc_assert (size_cost > 0);
    3464              : 
    3465       103210 :   ipa_node_params *info = ipa_node_params_sum->get (node);
    3466       103210 :   int num_sweeps = opt_for_fn (node->decl, param_ipa_cp_sweeps);
    3467       103210 :   int eval_threshold = opt_for_fn (node->decl, param_ipa_cp_eval_threshold);
    3468       103210 :   eval_threshold = (eval_threshold * num_sweeps) / cur_sweep;
    3469              :   /* If we know the execution IPA execution counts, we can estimate overall
    3470              :      speedup of the program.  */
    3471       103210 :   if (count_sum.nonzero_p ())
    3472              :     {
    3473          403 :       profile_count saved_time = count_sum * time_benefit;
    3474          403 :       sreal evaluation = saved_time.to_sreal_scale (profile_count::one ())
    3475          806 :                               / size_cost;
    3476          403 :       evaluation = incorporate_penalties (node, info, evaluation);
    3477              : 
    3478          403 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3479              :         {
    3480            0 :           fprintf (dump_file, "     good_cloning_opportunity_p (time: %g, "
    3481              :                    "size: %i, count_sum: ", time_benefit.to_double (),
    3482              :                    size_cost);
    3483            0 :           count_sum.dump (dump_file);
    3484            0 :           fprintf (dump_file, ", overall time saved: ");
    3485            0 :           saved_time.dump (dump_file);
    3486            0 :           fprintf (dump_file, "%s%s) -> evaluation: %.2f, threshold: %i\n",
    3487            0 :                  info->node_within_scc
    3488            0 :                    ? (info->node_is_self_scc ? ", self_scc" : ", scc") : "",
    3489            0 :                  info->node_calling_single_call ? ", single_call" : "",
    3490              :                    evaluation.to_double (), eval_threshold);
    3491              :         }
    3492          403 :       gcc_checking_assert (saved_time == saved_time.ipa ());
    3493          403 :       if (!maybe_hot_count_p (NULL, saved_time))
    3494              :         {
    3495           27 :           if (dump_file && (dump_flags & TDF_DETAILS))
    3496            0 :             fprintf (dump_file, "     not cloning: time saved is not hot\n");
    3497              :         }
    3498              :       /* Evaulation approximately corresponds to time saved per instruction
    3499              :          introduced.  This is likely almost always going to be true, since we
    3500              :          already checked that time saved is large enough to be considered
    3501              :          hot.  */
    3502          376 :       else if (evaluation >= (sreal)eval_threshold)
    3503          403 :         return true;
    3504              :       /* If all call sites have profile known; we know we do not want t clone.
    3505              :          If there are calls with unknown profile; try local heuristics.  */
    3506          377 :       if (!called_without_ipa_profile)
    3507              :         return false;
    3508              :     }
    3509       102807 :   sreal evaluation = (time_benefit * freq_sum) / size_cost;
    3510       102807 :   evaluation = incorporate_penalties (node, info, evaluation);
    3511       102807 :   evaluation *= 1000;
    3512              : 
    3513       102807 :   if (dump_file && (dump_flags & TDF_DETAILS))
    3514          356 :     fprintf (dump_file, "     good_cloning_opportunity_p (time: %g, "
    3515              :              "size: %i, freq_sum: %g%s%s) -> evaluation: %.2f, "
    3516              :              "threshold: %i\n",
    3517              :              time_benefit.to_double (), size_cost, freq_sum.to_double (),
    3518          178 :              info->node_within_scc
    3519           26 :                ? (info->node_is_self_scc ? ", self_scc" : ", scc") : "",
    3520          178 :              info->node_calling_single_call ? ", single_call" : "",
    3521              :              evaluation.to_double (), eval_threshold);
    3522              : 
    3523       102807 :   return evaluation >= eval_threshold;
    3524              : }
    3525              : 
    3526              : /* Grow vectors in AVALS and fill them with information about values of
    3527              :    parameters that are known to be independent of the context.  INFO describes
    3528              :    the function.  If REMOVABLE_PARAMS_COST is non-NULL, the movement cost of
    3529              :    all removable parameters will be stored in it.
    3530              : 
    3531              :    TODO: Also grow context independent value range vectors.  */
    3532              : 
    3533              : static bool
    3534      2179866 : gather_context_independent_values (class ipa_node_params *info,
    3535              :                                    ipa_auto_call_arg_values *avals,
    3536              :                                    int *removable_params_cost)
    3537              : {
    3538      2179866 :   int i, count = ipa_get_param_count (info);
    3539      2179866 :   bool ret = false;
    3540              : 
    3541      2179866 :   avals->m_known_vals.safe_grow_cleared (count, true);
    3542      2179866 :   avals->m_known_contexts.safe_grow_cleared (count, true);
    3543              : 
    3544      2179866 :   if (removable_params_cost)
    3545      2179866 :     *removable_params_cost = 0;
    3546              : 
    3547      7224574 :   for (i = 0; i < count; i++)
    3548              :     {
    3549      5044708 :       class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
    3550      5044708 :       ipcp_lattice<tree> *lat = &plats->itself;
    3551              : 
    3552      5044708 :       if (lat->is_single_const ())
    3553              :         {
    3554        34267 :           ipcp_value<tree> *val = lat->values;
    3555        34267 :           gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO);
    3556        34267 :           avals->m_known_vals[i] = val->value;
    3557        34267 :           if (removable_params_cost)
    3558        68534 :             *removable_params_cost
    3559        34267 :               += estimate_move_cost (TREE_TYPE (val->value), false);
    3560              :           ret = true;
    3561              :         }
    3562      5010441 :       else if (removable_params_cost
    3563      5010441 :                && !ipa_is_param_used (info, i))
    3564       969892 :         *removable_params_cost
    3565       484946 :           += ipa_get_param_move_cost (info, i);
    3566              : 
    3567      5044708 :       if (!ipa_is_param_used (info, i))
    3568       490081 :         continue;
    3569              : 
    3570      4554627 :       ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
    3571              :       /* Do not account known context as reason for cloning.  We can see
    3572              :          if it permits devirtualization.  */
    3573      4554627 :       if (ctxlat->is_single_const ())
    3574        23385 :         avals->m_known_contexts[i] = ctxlat->values->value;
    3575              : 
    3576      4554627 :       ret |= push_agg_values_from_plats (plats, i, 0, &avals->m_known_aggs);
    3577              :     }
    3578              : 
    3579      2179866 :   return ret;
    3580              : }
    3581              : 
    3582              : /* Perform time and size measurement of NODE with the context given in AVALS,
    3583              :    calculate the benefit compared to the node without specialization and store
    3584              :    it into VAL.  Take into account REMOVABLE_PARAMS_COST of all
    3585              :    context-independent or unused removable parameters and EST_MOVE_COST, the
    3586              :    estimated movement of the considered parameter.  */
    3587              : 
    3588              : static void
    3589        78094 : perform_estimation_of_a_value (cgraph_node *node,
    3590              :                                ipa_auto_call_arg_values *avals,
    3591              :                                int removable_params_cost, int est_move_cost,
    3592              :                                ipcp_value_base *val)
    3593              : {
    3594        78094 :   sreal time_benefit;
    3595        78094 :   ipa_call_estimates estimates;
    3596              : 
    3597        78094 :   estimate_ipcp_clone_size_and_time (node, avals, &estimates);
    3598              : 
    3599              :   /* Extern inline functions have no cloning local time benefits because they
    3600              :      will be inlined anyway.  The only reason to clone them is if it enables
    3601              :      optimization in any of the functions they call.  */
    3602        78094 :   if (DECL_EXTERNAL (node->decl) && DECL_DECLARED_INLINE_P (node->decl))
    3603          114 :     time_benefit = 0;
    3604              :   else
    3605        77980 :     time_benefit = (estimates.nonspecialized_time - estimates.time)
    3606       155960 :       + hint_time_bonus (node, estimates)
    3607       155960 :       + (devirtualization_time_bonus (node, avals)
    3608       155960 :          + removable_params_cost + est_move_cost);
    3609              : 
    3610        78094 :   int size = estimates.size;
    3611        78094 :   gcc_checking_assert (size >=0);
    3612              :   /* The inliner-heuristics based estimates may think that in certain
    3613              :      contexts some functions do not have any size at all but we want
    3614              :      all specializations to have at least a tiny cost, not least not to
    3615              :      divide by zero.  */
    3616        78094 :   if (size == 0)
    3617            0 :     size = 1;
    3618              : 
    3619        78094 :   val->local_time_benefit = time_benefit;
    3620        78094 :   val->local_size_cost = size;
    3621        78094 : }
    3622              : 
    3623              : /* Get the overall limit of growth based on parameters extracted from NODE.  It
    3624              :    does not really make sense to mix functions with different overall growth
    3625              :    limits or even number of sweeps but it is possible and if it happens, we do
    3626              :    not want to select one limit at random, so get the limits from NODE.  */
    3627              : 
    3628              : static long
    3629       211506 : get_max_overall_size (cgraph_node *node)
    3630              : {
    3631       211506 :   long max_new_size = orig_overall_size;
    3632       211506 :   long large_unit = opt_for_fn (node->decl, param_ipa_cp_large_unit_insns);
    3633       211506 :   if (max_new_size < large_unit)
    3634              :     max_new_size = large_unit;
    3635       211506 :   int unit_growth = opt_for_fn (node->decl, param_ipa_cp_unit_growth);
    3636       211506 :   max_new_size += max_new_size * unit_growth / 100 + 1;
    3637              : 
    3638       211506 :   return max_new_size;
    3639              : }
    3640              : 
    3641              : /* Return true if NODE should be cloned just for a parameter removal, possibly
    3642              :    dumping a reason if not.  */
    3643              : 
    3644              : static bool
    3645       193326 : clone_for_param_removal_p (cgraph_node *node)
    3646              : {
    3647       193326 :   if (!node->can_change_signature)
    3648              :     {
    3649         5017 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3650            0 :         fprintf (dump_file, "  Not considering cloning to remove parameters, "
    3651              :                  "function cannot change signature.\n");
    3652         5017 :       return false;
    3653              :     }
    3654       188309 :   if (node->can_be_local_p ())
    3655              :     {
    3656       139218 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3657            0 :         fprintf (dump_file, "  Not considering cloning to remove parameters, "
    3658              :                  "IPA-SRA can do it potentially better.\n");
    3659       139218 :       return false;
    3660              :     }
    3661              :   return true;
    3662              : }
    3663              : 
    3664              : /* Iterate over known values of parameters of NODE and estimate the local
    3665              :    effects in terms of time and size they have.  */
    3666              : 
    3667              : static void
    3668      1269638 : estimate_local_effects (struct cgraph_node *node)
    3669              : {
    3670      1269638 :   ipa_node_params *info = ipa_node_params_sum->get (node);
    3671      1269638 :   int count = ipa_get_param_count (info);
    3672      1040531 :   int removable_params_cost;
    3673              : 
    3674      1040531 :   if (!count || !ipcp_versionable_function_p (node))
    3675       396466 :     return;
    3676              : 
    3677       873172 :   if (dump_file && (dump_flags & TDF_DETAILS))
    3678          117 :     fprintf (dump_file, "\nEstimating effects for %s.\n", node->dump_name ());
    3679              : 
    3680       873172 :   ipa_auto_call_arg_values avals;
    3681       873172 :   gather_context_independent_values (info, &avals, &removable_params_cost);
    3682              : 
    3683      2912970 :   for (int i = 0; i < count; i++)
    3684              :     {
    3685      2039798 :       class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
    3686      2039798 :       ipcp_lattice<tree> *lat = &plats->itself;
    3687      2039798 :       ipcp_value<tree> *val;
    3688              : 
    3689      4058337 :       if (lat->bottom
    3690       211309 :           || !lat->values
    3691      2078267 :           || avals.m_known_vals[i])
    3692      2018539 :         continue;
    3693              : 
    3694        65158 :       for (val = lat->values; val; val = val->next)
    3695              :         {
    3696        43899 :           gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO);
    3697        43899 :           avals.m_known_vals[i] = val->value;
    3698              : 
    3699        43899 :           int emc = estimate_move_cost (TREE_TYPE (val->value), true);
    3700        43899 :           perform_estimation_of_a_value (node, &avals, removable_params_cost,
    3701              :                                          emc, val);
    3702              : 
    3703        43899 :           if (dump_file && (dump_flags & TDF_DETAILS))
    3704              :             {
    3705           44 :               fprintf (dump_file, " - estimates for value ");
    3706           44 :               print_ipcp_constant_value (dump_file, val->value);
    3707           44 :               fprintf (dump_file, " for ");
    3708           44 :               ipa_dump_param (dump_file, info, i);
    3709           44 :               fprintf (dump_file, ": time_benefit: %g, size: %i\n",
    3710              :                        val->local_time_benefit.to_double (),
    3711              :                        val->local_size_cost);
    3712              :             }
    3713              :         }
    3714        21259 :       avals.m_known_vals[i] = NULL_TREE;
    3715              :     }
    3716              : 
    3717      2912970 :   for (int i = 0; i < count; i++)
    3718              :     {
    3719      2039798 :       class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
    3720              : 
    3721      2039798 :       if (!plats->virt_call)
    3722      2032076 :         continue;
    3723              : 
    3724         7722 :       ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
    3725         7722 :       ipcp_value<ipa_polymorphic_call_context> *val;
    3726              : 
    3727        15286 :       if (ctxlat->bottom
    3728         2843 :           || !ctxlat->values
    3729        10559 :           || !avals.m_known_contexts[i].useless_p ())
    3730         7564 :         continue;
    3731              : 
    3732          384 :       for (val = ctxlat->values; val; val = val->next)
    3733              :         {
    3734          226 :           avals.m_known_contexts[i] = val->value;
    3735          226 :           perform_estimation_of_a_value (node, &avals, removable_params_cost,
    3736              :                                          0, val);
    3737              : 
    3738          226 :           if (dump_file && (dump_flags & TDF_DETAILS))
    3739              :             {
    3740            0 :               fprintf (dump_file, " - estimates for polymorphic context ");
    3741            0 :               print_ipcp_constant_value (dump_file, val->value);
    3742            0 :               fprintf (dump_file, " for ");
    3743            0 :               ipa_dump_param (dump_file, info, i);
    3744            0 :               fprintf (dump_file, ": time_benefit: %g, size: %i\n",
    3745              :                        val->local_time_benefit.to_double (),
    3746              :                        val->local_size_cost);
    3747              :             }
    3748              :         }
    3749          158 :       avals.m_known_contexts[i] = ipa_polymorphic_call_context ();
    3750              :     }
    3751              : 
    3752       873172 :   unsigned all_ctx_len = avals.m_known_aggs.length ();
    3753       873172 :   auto_vec<ipa_argagg_value, 32> all_ctx;
    3754       873172 :   all_ctx.reserve_exact (all_ctx_len);
    3755       873172 :   all_ctx.splice (avals.m_known_aggs);
    3756       873172 :   avals.m_known_aggs.safe_grow_cleared (all_ctx_len + 1);
    3757              : 
    3758       873172 :   unsigned j = 0;
    3759      2912970 :   for (int index = 0; index < count; index++)
    3760              :     {
    3761      2039798 :       class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, index);
    3762              : 
    3763      2039798 :       if (plats->aggs_bottom || !plats->aggs)
    3764      2020782 :         continue;
    3765              : 
    3766        72895 :       for (ipcp_agg_lattice *aglat = plats->aggs; aglat; aglat = aglat->next)
    3767              :         {
    3768        53879 :           ipcp_value<tree> *val;
    3769        53523 :           if (aglat->bottom || !aglat->values
    3770              :               /* If the following is true, the one value is already part of all
    3771              :                  context estimations.  */
    3772       100017 :               || (!plats->aggs_contain_variable
    3773        25056 :                   && aglat->is_single_const ()))
    3774        28442 :             continue;
    3775              : 
    3776        25437 :           unsigned unit_offset = aglat->offset / BITS_PER_UNIT;
    3777        25437 :           while (j < all_ctx_len
    3778        33753 :                  && (all_ctx[j].index < index
    3779         3300 :                      || (all_ctx[j].index == index
    3780         2333 :                          && all_ctx[j].unit_offset < unit_offset)))
    3781              :             {
    3782         3209 :               avals.m_known_aggs[j] = all_ctx[j];
    3783         3209 :               j++;
    3784              :             }
    3785              : 
    3786        34484 :           for (unsigned k = j; k < all_ctx_len; k++)
    3787         9047 :             avals.m_known_aggs[k+1] = all_ctx[k];
    3788              : 
    3789        59406 :           for (val = aglat->values; val; val = val->next)
    3790              :             {
    3791        33969 :               avals.m_known_aggs[j].value = val->value;
    3792        33969 :               avals.m_known_aggs[j].unit_offset = unit_offset;
    3793        33969 :               avals.m_known_aggs[j].index = index;
    3794        33969 :               avals.m_known_aggs[j].by_ref = plats->aggs_by_ref;
    3795        33969 :               avals.m_known_aggs[j].killed = false;
    3796              : 
    3797        33969 :               perform_estimation_of_a_value (node, &avals,
    3798              :                                              removable_params_cost, 0, val);
    3799              : 
    3800        33969 :               if (dump_file && (dump_flags & TDF_DETAILS))
    3801              :                 {
    3802           79 :                   fprintf (dump_file, " - estimates for value ");
    3803           79 :                   print_ipcp_constant_value (dump_file, val->value);
    3804           79 :                   fprintf (dump_file, " for ");
    3805           79 :                   ipa_dump_param (dump_file, info, index);
    3806          158 :                   fprintf (dump_file, "[%soffset: " HOST_WIDE_INT_PRINT_DEC
    3807              :                            "]: time_benefit: %g, size: %i\n",
    3808           79 :                            plats->aggs_by_ref ? "ref " : "",
    3809              :                            aglat->offset,
    3810              :                            val->local_time_benefit.to_double (),
    3811              :                            val->local_size_cost);
    3812              :                 }
    3813              :             }
    3814              :         }
    3815              :     }
    3816       873172 : }
    3817              : 
    3818              : 
    3819              : /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
    3820              :    topological sort of values.  */
    3821              : 
    3822              : template <typename valtype>
    3823              : void
    3824       136141 : value_topo_info<valtype>::add_val (ipcp_value<valtype> *cur_val)
    3825              : {
    3826              :   ipcp_value_source<valtype> *src;
    3827              : 
    3828       136141 :   if (cur_val->dfs)
    3829              :     return;
    3830              : 
    3831       135979 :   dfs_counter++;
    3832       135979 :   cur_val->dfs = dfs_counter;
    3833       135979 :   cur_val->low_link = dfs_counter;
    3834              : 
    3835       135979 :   cur_val->topo_next = stack;
    3836       135979 :   stack = cur_val;
    3837       135979 :   cur_val->on_stack = true;
    3838              : 
    3839       592644 :   for (src = cur_val->sources; src; src = src->next)
    3840       456665 :     if (src->val)
    3841              :       {
    3842        20947 :         if (src->val->dfs == 0)
    3843              :           {
    3844          186 :             add_val (src->val);
    3845          186 :             if (src->val->low_link < cur_val->low_link)
    3846           19 :               cur_val->low_link = src->val->low_link;
    3847              :           }
    3848        20761 :         else if (src->val->on_stack
    3849         1587 :                  && src->val->dfs < cur_val->low_link)
    3850           73 :           cur_val->low_link = src->val->dfs;
    3851              :       }
    3852              : 
    3853       135979 :   if (cur_val->dfs == cur_val->low_link)
    3854              :     {
    3855              :       ipcp_value<valtype> *v, *scc_list = NULL;
    3856              : 
    3857              :       do
    3858              :         {
    3859       135979 :           v = stack;
    3860       135979 :           stack = v->topo_next;
    3861       135979 :           v->on_stack = false;
    3862       135979 :           v->scc_no = cur_val->dfs;
    3863              : 
    3864       135979 :           v->scc_next = scc_list;
    3865       135979 :           scc_list = v;
    3866              :         }
    3867       135979 :       while (v != cur_val);
    3868              : 
    3869       135891 :       cur_val->topo_next = values_topo;
    3870       135891 :       values_topo = cur_val;
    3871              :     }
    3872              : }
    3873              : 
    3874              : /* Add all values in lattices associated with NODE to the topological sort if
    3875              :    they are not there yet.  */
    3876              : 
    3877              : static void
    3878      1269638 : add_all_node_vals_to_toposort (cgraph_node *node, ipa_topo_info *topo)
    3879              : {
    3880      1269638 :   ipa_node_params *info = ipa_node_params_sum->get (node);
    3881      1269638 :   int i, count = ipa_get_param_count (info);
    3882              : 
    3883      3603430 :   for (i = 0; i < count; i++)
    3884              :     {
    3885      2333792 :       class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
    3886      2333792 :       ipcp_lattice<tree> *lat = &plats->itself;
    3887      2333792 :       struct ipcp_agg_lattice *aglat;
    3888              : 
    3889      2333792 :       if (!lat->bottom)
    3890              :         {
    3891       222017 :           ipcp_value<tree> *val;
    3892       293903 :           for (val = lat->values; val; val = val->next)
    3893        71886 :             topo->constants.add_val (val);
    3894              :         }
    3895              : 
    3896      2333792 :       if (!plats->aggs_bottom)
    3897       278334 :         for (aglat = plats->aggs; aglat; aglat = aglat->next)
    3898        56384 :           if (!aglat->bottom)
    3899              :             {
    3900        56028 :               ipcp_value<tree> *val;
    3901       112386 :               for (val = aglat->values; val; val = val->next)
    3902        56358 :                 topo->constants.add_val (val);
    3903              :             }
    3904              : 
    3905      2333792 :       ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
    3906      2333792 :       if (!ctxlat->bottom)
    3907              :         {
    3908       223019 :           ipcp_value<ipa_polymorphic_call_context> *ctxval;
    3909       230730 :           for (ctxval = ctxlat->values; ctxval; ctxval = ctxval->next)
    3910         7711 :             topo->contexts.add_val (ctxval);
    3911              :         }
    3912              :     }
    3913      1269638 : }
    3914              : 
    3915              : /* One pass of constants propagation along the call graph edges, from callers
    3916              :    to callees (requires topological ordering in TOPO), iterate over strongly
    3917              :    connected components.  */
    3918              : 
    3919              : static void
    3920       129027 : propagate_constants_topo (class ipa_topo_info *topo)
    3921              : {
    3922       129027 :   int i;
    3923              : 
    3924      1479629 :   for (i = topo->nnodes - 1; i >= 0; i--)
    3925              :     {
    3926      1350602 :       unsigned j;
    3927      1350602 :       struct cgraph_node *v, *node = topo->order[i];
    3928      1350602 :       vec<cgraph_node *> cycle_nodes = ipa_get_nodes_in_cycle (node);
    3929              : 
    3930              :       /* First, iteratively propagate within the strongly connected component
    3931              :          until all lattices stabilize.  */
    3932      2706058 :       FOR_EACH_VEC_ELT (cycle_nodes, j, v)
    3933      1355456 :         if (v->has_gimple_body_p ())
    3934              :           {
    3935      1278480 :             if (opt_for_fn (v->decl, flag_ipa_cp)
    3936      1278480 :                 && opt_for_fn (v->decl, optimize))
    3937      1269638 :               push_node_to_stack (topo, v);
    3938              :             /* When V is not optimized, we can not push it to stack, but
    3939              :                still we need to set all its callees lattices to bottom.  */
    3940              :             else
    3941              :               {
    3942        21855 :                 for (cgraph_edge *cs = v->callees; cs; cs = cs->next_callee)
    3943        13013 :                    propagate_constants_across_call (cs);
    3944              :               }
    3945              :           }
    3946              : 
    3947      1350602 :       v = pop_node_from_stack (topo);
    3948      3974026 :       while (v)
    3949              :         {
    3950      1272822 :           struct cgraph_edge *cs;
    3951      1272822 :           class ipa_node_params *info = NULL;
    3952      1272822 :           bool self_scc = true;
    3953              : 
    3954      6561314 :           for (cs = v->callees; cs; cs = cs->next_callee)
    3955      5288492 :             if (ipa_edge_within_scc (cs))
    3956              :               {
    3957        29596 :                 cgraph_node *callee = cs->callee->function_symbol ();
    3958              : 
    3959        29596 :                 if (v != callee)
    3960        17987 :                   self_scc = false;
    3961              : 
    3962        29596 :                 if (!info)
    3963              :                   {
    3964        13888 :                     info = ipa_node_params_sum->get (v);
    3965        13888 :                     info->node_within_scc = true;
    3966              :                   }
    3967              : 
    3968        29596 :                 if (propagate_constants_across_call (cs))
    3969         4153 :                   push_node_to_stack (topo, callee);
    3970              :               }
    3971              : 
    3972      1272822 :           if (info)
    3973        13888 :             info->node_is_self_scc = self_scc;
    3974              : 
    3975      1272822 :           v = pop_node_from_stack (topo);
    3976              :         }
    3977              : 
    3978              :       /* Afterwards, propagate along edges leading out of the SCC, calculates
    3979              :          the local effects of the discovered constants and all valid values to
    3980              :          their topological sort.  */
    3981      2706058 :       FOR_EACH_VEC_ELT (cycle_nodes, j, v)
    3982      1355456 :         if (v->has_gimple_body_p ()
    3983      1278480 :             && opt_for_fn (v->decl, flag_ipa_cp)
    3984      2625094 :             && opt_for_fn (v->decl, optimize))
    3985              :           {
    3986      1269638 :             struct cgraph_edge *cs;
    3987              : 
    3988      1269638 :             estimate_local_effects (v);
    3989      1269638 :             add_all_node_vals_to_toposort (v, topo);
    3990      6524571 :             for (cs = v->callees; cs; cs = cs->next_callee)
    3991      5254933 :               if (!ipa_edge_within_scc (cs))
    3992      5232705 :                 propagate_constants_across_call (cs);
    3993              :           }
    3994      1350602 :       cycle_nodes.release ();
    3995              :     }
    3996       129027 : }
    3997              : 
    3998              : /* Propagate the estimated effects of individual values along the topological
    3999              :    from the dependent values to those they depend on.  */
    4000              : 
    4001              : template <typename valtype>
    4002              : void
    4003       258054 : value_topo_info<valtype>::propagate_effects ()
    4004              : {
    4005              :   ipcp_value<valtype> *base;
    4006       258054 :   hash_set<ipcp_value<valtype> *> processed_srcvals;
    4007              : 
    4008       393945 :   for (base = values_topo; base; base = base->topo_next)
    4009              :     {
    4010              :       ipcp_value_source<valtype> *src;
    4011              :       ipcp_value<valtype> *val;
    4012       135891 :       sreal time = 0;
    4013       135891 :       HOST_WIDE_INT size = 0;
    4014              : 
    4015       271870 :       for (val = base; val; val = val->scc_next)
    4016              :         {
    4017       135979 :           time = time + val->local_time_benefit + val->prop_time_benefit;
    4018       135979 :           size = size + val->local_size_cost + val->prop_size_cost;
    4019              :         }
    4020              : 
    4021       271870 :       for (val = base; val; val = val->scc_next)
    4022              :         {
    4023       135979 :           processed_srcvals.empty ();
    4024       592644 :           for (src = val->sources; src; src = src->next)
    4025       456665 :             if (src->val
    4026       456665 :                 && cs_interesting_for_ipcp_p (src->cs))
    4027              :               {
    4028        20907 :                 if (!processed_srcvals.add (src->val))
    4029              :                   {
    4030        16740 :                     HOST_WIDE_INT prop_size = size + src->val->prop_size_cost;
    4031        16740 :                     if (prop_size < INT_MAX)
    4032        16740 :                       src->val->prop_size_cost = prop_size;
    4033              :                     else
    4034            0 :                       continue;
    4035              :                   }
    4036              : 
    4037        20907 :                 int special_factor = 1;
    4038        20907 :                 if (val->same_scc (src->val))
    4039              :                   special_factor
    4040         1675 :                     = opt_for_fn(src->cs->caller->decl,
    4041              :                                  param_ipa_cp_recursive_freq_factor);
    4042        19232 :                 else if (val->self_recursion_generated_p ()
    4043        19232 :                          && (src->cs->callee->function_symbol ()
    4044          822 :                              == src->cs->caller))
    4045              :                   {
    4046          822 :                     int max_recur_gen_depth
    4047          822 :                       = opt_for_fn(src->cs->caller->decl,
    4048              :                                    param_ipa_cp_max_recursive_depth);
    4049          822 :                     special_factor = max_recur_gen_depth
    4050          822 :                       - val->self_recursion_generated_level + 1;
    4051              :                   }
    4052              : 
    4053        20907 :                 src->val->prop_time_benefit
    4054        41814 :                   += time * special_factor * src->cs->sreal_frequency ();
    4055              :               }
    4056              : 
    4057       135979 :           if (size < INT_MAX)
    4058              :             {
    4059       135979 :               val->prop_time_benefit = time;
    4060       135979 :               val->prop_size_cost = size;
    4061              :             }
    4062              :           else
    4063              :             {
    4064            0 :               val->prop_time_benefit = 0;
    4065            0 :               val->prop_size_cost = 0;
    4066              :             }
    4067              :         }
    4068              :     }
    4069       258054 : }
    4070              : 
    4071              : 
    4072              : /* Propagate constants, polymorphic contexts and their effects from the
    4073              :    summaries interprocedurally.  */
    4074              : 
    4075              : static void
    4076       129027 : ipcp_propagate_stage (class ipa_topo_info *topo)
    4077              : {
    4078       129027 :   struct cgraph_node *node;
    4079              : 
    4080       129027 :   if (dump_file)
    4081          161 :     fprintf (dump_file, "\n Propagating constants:\n\n");
    4082              : 
    4083      1484487 :   FOR_EACH_DEFINED_FUNCTION (node)
    4084              :   {
    4085      1355460 :     if (node->has_gimple_body_p ()
    4086      1278480 :         && opt_for_fn (node->decl, flag_ipa_cp)
    4087      2625098 :         && opt_for_fn (node->decl, optimize))
    4088              :       {
    4089      1269638 :         ipa_node_params *info = ipa_node_params_sum->get (node);
    4090      1269638 :         determine_versionability (node, info);
    4091              : 
    4092      1269638 :         unsigned nlattices = ipa_get_param_count (info);
    4093      1269638 :         info->lattices.safe_grow_cleared (nlattices, true);
    4094      1269638 :         initialize_node_lattices (node);
    4095              : 
    4096      1269638 :         int num_sweeps = opt_for_fn (node->decl, param_ipa_cp_sweeps);
    4097      1269638 :         if (max_number_sweeps < num_sweeps)
    4098       121228 :           max_number_sweeps = num_sweeps;
    4099              :       }
    4100      1355460 :     ipa_size_summary *s = ipa_size_summaries->get (node);
    4101      1355460 :     if (node->definition && !node->alias && s != NULL)
    4102      1279413 :       overall_size += s->self_size;
    4103              :   }
    4104              : 
    4105       129027 :   orig_overall_size = overall_size;
    4106              : 
    4107       129027 :   if (dump_file)
    4108          161 :     fprintf (dump_file, "\noverall_size: %li\n", overall_size);
    4109              : 
    4110       129027 :   propagate_constants_topo (topo);
    4111       129027 :   if (flag_checking)
    4112       129019 :     ipcp_verify_propagated_values ();
    4113       129027 :   topo->constants.propagate_effects ();
    4114       129027 :   topo->contexts.propagate_effects ();
    4115              : 
    4116       129027 :   if (dump_file)
    4117              :     {
    4118          161 :       fprintf (dump_file, "\nIPA lattices after all propagation:\n");
    4119          161 :       print_all_lattices (dump_file, (dump_flags & TDF_DETAILS), true);
    4120              :     }
    4121       129027 : }
    4122              : 
    4123              : /* Discover newly direct outgoing edges from NODE which is a new clone with
    4124              :    known KNOWN_CSTS and make them direct.  */
    4125              : 
    4126              : static void
    4127        21316 : ipcp_discover_new_direct_edges (struct cgraph_node *node,
    4128              :                                 vec<tree> known_csts,
    4129              :                                 vec<ipa_polymorphic_call_context>
    4130              :                                 known_contexts,
    4131              :                                 vec<ipa_argagg_value, va_gc> *aggvals)
    4132              : {
    4133        21316 :   struct cgraph_edge *ie, *next_ie;
    4134        21316 :   bool found = false;
    4135              : 
    4136        23201 :   for (ie = node->indirect_calls; ie; ie = next_ie)
    4137              :     {
    4138         1885 :       tree target;
    4139         1885 :       bool speculative;
    4140              : 
    4141         1885 :       next_ie = ie->next_callee;
    4142         1885 :       ipa_argagg_value_list avs (aggvals);
    4143         1885 :       target = ipa_get_indirect_edge_target_1 (ie, known_csts, known_contexts,
    4144              :                                                avs, &speculative);
    4145         1885 :       if (target)
    4146              :         {
    4147          555 :           cgraph_polymorphic_indirect_info *pii
    4148          555 :             = dyn_cast <cgraph_polymorphic_indirect_info *> (ie->indirect_info);
    4149          555 :           cgraph_simple_indirect_info *sii
    4150         1043 :             = dyn_cast <cgraph_simple_indirect_info *> (ie->indirect_info);
    4151          416 :           bool agg_contents = sii && sii->agg_contents;
    4152          555 :           bool polymorphic = !!pii;
    4153          555 :           int param_index = ie->indirect_info->param_index;
    4154          555 :           struct cgraph_edge *cs = ipa_make_edge_direct_to_target (ie, target,
    4155              :                                                                    speculative);
    4156          555 :           found = true;
    4157              : 
    4158          555 :           if (cs && !agg_contents && !polymorphic)
    4159              :             {
    4160          349 :               ipa_node_params *info = ipa_node_params_sum->get (node);
    4161          349 :               int c = ipa_get_controlled_uses (info, param_index);
    4162          349 :               if (c != IPA_UNDESCRIBED_USE
    4163          349 :                   && !ipa_get_param_load_dereferenced (info, param_index))
    4164              :                 {
    4165          345 :                   struct ipa_ref *to_del;
    4166              : 
    4167          345 :                   c--;
    4168          345 :                   ipa_set_controlled_uses (info, param_index, c);
    4169          345 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    4170            3 :                     fprintf (dump_file, "     controlled uses count of param "
    4171              :                              "%i bumped down to %i\n", param_index, c);
    4172          345 :                   if (c == 0
    4173          345 :                       && (to_del = node->find_reference (cs->callee, NULL, 0,
    4174              :                                                          IPA_REF_ADDR)))
    4175              :                     {
    4176          281 :                       if (dump_file && (dump_flags & TDF_DETAILS))
    4177            3 :                         fprintf (dump_file, "       and even removing its "
    4178              :                                  "cloning-created reference\n");
    4179          281 :                       to_del->remove_reference ();
    4180              :                     }
    4181              :                 }
    4182              :             }
    4183              :         }
    4184              :     }
    4185              :   /* Turning calls to direct calls will improve overall summary.  */
    4186        21316 :   if (found)
    4187          462 :     ipa_update_overall_fn_summary (node);
    4188        21316 : }
    4189              : 
    4190              : class edge_clone_summary;
    4191              : static call_summary <edge_clone_summary *> *edge_clone_summaries = NULL;
    4192              : 
    4193              : /* Edge clone summary.  */
    4194              : 
    4195              : class edge_clone_summary
    4196              : {
    4197              : public:
    4198              :   /* Default constructor.  */
    4199       377360 :   edge_clone_summary (): prev_clone (NULL), next_clone (NULL) {}
    4200              : 
    4201              :   /* Default destructor.  */
    4202       377360 :   ~edge_clone_summary ()
    4203              :   {
    4204       377360 :     if (prev_clone)
    4205        34243 :       edge_clone_summaries->get (prev_clone)->next_clone = next_clone;
    4206       377360 :     if (next_clone)
    4207       157698 :       edge_clone_summaries->get (next_clone)->prev_clone = prev_clone;
    4208       377360 :   }
    4209              : 
    4210              :   cgraph_edge *prev_clone;
    4211              :   cgraph_edge *next_clone;
    4212              : };
    4213              : 
    4214              : class edge_clone_summary_t:
    4215              :   public call_summary <edge_clone_summary *>
    4216              : {
    4217              : public:
    4218       129027 :   edge_clone_summary_t (symbol_table *symtab):
    4219       258054 :     call_summary <edge_clone_summary *> (symtab)
    4220              :     {
    4221       129027 :       m_initialize_when_cloning = true;
    4222              :     }
    4223              : 
    4224              :   void duplicate (cgraph_edge *src_edge, cgraph_edge *dst_edge,
    4225              :                   edge_clone_summary *src_data,
    4226              :                   edge_clone_summary *dst_data) final override;
    4227              : };
    4228              : 
    4229              : /* Edge duplication hook.  */
    4230              : 
    4231              : void
    4232       191328 : edge_clone_summary_t::duplicate (cgraph_edge *src_edge, cgraph_edge *dst_edge,
    4233              :                                  edge_clone_summary *src_data,
    4234              :                                  edge_clone_summary *dst_data)
    4235              : {
    4236       191328 :   if (src_data->next_clone)
    4237         5287 :     edge_clone_summaries->get (src_data->next_clone)->prev_clone = dst_edge;
    4238       191328 :   dst_data->prev_clone = src_edge;
    4239       191328 :   dst_data->next_clone = src_data->next_clone;
    4240       191328 :   src_data->next_clone = dst_edge;
    4241       191328 : }
    4242              : 
    4243              : /* Return true is CS calls DEST or its clone for all contexts.  When
    4244              :    ALLOW_RECURSION_TO_CLONE is false, also return false for self-recursive
    4245              :    edges from/to an all-context clone.  */
    4246              : 
    4247              : static bool
    4248      1812843 : calls_same_node_or_its_all_contexts_clone_p (cgraph_edge *cs, cgraph_node *dest,
    4249              :                                              bool allow_recursion_to_clone)
    4250              : {
    4251      1812843 :   enum availability availability;
    4252      1812843 :   cgraph_node *callee = cs->callee->function_symbol (&availability);
    4253              : 
    4254      1812843 :   if (availability <= AVAIL_INTERPOSABLE)
    4255              :     return false;
    4256      1806976 :   if (callee == dest)
    4257              :     return true;
    4258       617279 :   if (!allow_recursion_to_clone && cs->caller == callee)
    4259              :     return false;
    4260              : 
    4261       617122 :   ipa_node_params *info = ipa_node_params_sum->get (callee);
    4262       617122 :   return info->is_all_contexts_clone && info->ipcp_orig_node == dest;
    4263              : }
    4264              : 
    4265              : /* Return true if edge CS does bring about the value described by SRC to
    4266              :    DEST_VAL of node DEST or its clone for all contexts.  */
    4267              : 
    4268              : static bool
    4269      1802873 : cgraph_edge_brings_value_p (cgraph_edge *cs, ipcp_value_source<tree> *src,
    4270              :                             cgraph_node *dest, ipcp_value<tree> *dest_val)
    4271              : {
    4272      1802873 :   ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
    4273              : 
    4274      1802873 :   if (!calls_same_node_or_its_all_contexts_clone_p (cs, dest, !src->val)
    4275      1802873 :       || caller_info->node_dead)
    4276              :     return false;
    4277              : 
    4278       740157 :   if (!src->val)
    4279              :     return true;
    4280              : 
    4281        61193 :   if (caller_info->ipcp_orig_node)
    4282              :     {
    4283        19086 :       tree t = NULL_TREE;
    4284        19086 :       if (src->offset == -1)
    4285        13618 :         t = caller_info->known_csts[src->index];
    4286         5468 :       else if (ipcp_transformation *ts
    4287         5468 :                = ipcp_get_transformation_summary (cs->caller))
    4288              :         {
    4289         5468 :           ipa_argagg_value_list avl (ts);
    4290         5468 :           t = avl.get_value (src->index, src->offset / BITS_PER_UNIT);
    4291              :         }
    4292        19086 :       return (t != NULL_TREE
    4293        19086 :               && values_equal_for_ipcp_p (src->val->value, t));
    4294              :     }
    4295              :   else
    4296              :     {
    4297        42107 :       if (src->val == dest_val)
    4298              :         return true;
    4299              : 
    4300        36389 :       struct ipcp_agg_lattice *aglat;
    4301        36389 :       class ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
    4302              :                                                                  src->index);
    4303        36389 :       if (src->offset == -1)
    4304        26895 :         return (plats->itself.is_single_const ()
    4305           20 :                 && values_equal_for_ipcp_p (src->val->value,
    4306           20 :                                             plats->itself.values->value));
    4307              :       else
    4308              :         {
    4309         9494 :           if (plats->aggs_bottom || plats->aggs_contain_variable)
    4310              :             return false;
    4311         3867 :           for (aglat = plats->aggs; aglat; aglat = aglat->next)
    4312         3867 :             if (aglat->offset == src->offset)
    4313         1739 :               return  (aglat->is_single_const ()
    4314            8 :                        && values_equal_for_ipcp_p (src->val->value,
    4315            8 :                                                    aglat->values->value));
    4316              :         }
    4317              :       return false;
    4318              :     }
    4319              : }
    4320              : 
    4321              : /* Return true if edge CS does bring about the value described by SRC to
    4322              :    DST_VAL of node DEST or its clone for all contexts.  */
    4323              : 
    4324              : static bool
    4325         9970 : cgraph_edge_brings_value_p (cgraph_edge *cs,
    4326              :                             ipcp_value_source<ipa_polymorphic_call_context> *src,
    4327              :                             cgraph_node *dest,
    4328              :                             ipcp_value<ipa_polymorphic_call_context> *)
    4329              : {
    4330         9970 :   ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
    4331              : 
    4332         9970 :   if (!calls_same_node_or_its_all_contexts_clone_p (cs, dest, true)
    4333         9970 :       || caller_info->node_dead)
    4334              :     return false;
    4335         8987 :   if (!src->val)
    4336              :     return true;
    4337              : 
    4338         1744 :   if (caller_info->ipcp_orig_node)
    4339         2475 :     return (caller_info->known_contexts.length () > (unsigned) src->index)
    4340          484 :       && values_equal_for_ipcp_p (src->val->value,
    4341          242 :                                   caller_info->known_contexts[src->index]);
    4342              : 
    4343         1487 :   class ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
    4344              :                                                              src->index);
    4345         1487 :   return plats->ctxlat.is_single_const ()
    4346          270 :     && values_equal_for_ipcp_p (src->val->value,
    4347          270 :                                 plats->ctxlat.values->value);
    4348              : }
    4349              : 
    4350              : /* Get the next clone in the linked list of clones of an edge.  */
    4351              : 
    4352              : static inline struct cgraph_edge *
    4353      1813132 : get_next_cgraph_edge_clone (struct cgraph_edge *cs)
    4354              : {
    4355      1813132 :   edge_clone_summary *s = edge_clone_summaries->get (cs);
    4356      1813132 :   return s != NULL ? s->next_clone : NULL;
    4357              : }
    4358              : 
    4359              : /* Given VAL that is intended for DEST, iterate over all its sources and if any
    4360              :    of them is viable and hot, return true.  In that case, for those that still
    4361              :    hold, add their edge frequency and their number and cumulative profile
    4362              :    counts of self-ecursive and other edges into *FREQUENCY, *CALLER_COUNT,
    4363              :    REC_COUNT_SUM and NONREC_COUNT_SUM respectively.  */
    4364              : 
    4365              : template <typename valtype>
    4366              : static bool
    4367       210863 : get_info_about_necessary_edges (ipcp_value<valtype> *val, cgraph_node *dest,
    4368              :                                 sreal *freq_sum, int *caller_count,
    4369              :                                 profile_count *rec_count_sum,
    4370              :                                 profile_count *nonrec_count_sum,
    4371              :                                 bool *called_without_ipa_profile)
    4372              : {
    4373              :   ipcp_value_source<valtype> *src;
    4374       210863 :   sreal freq = 0;
    4375       210863 :   int count = 0;
    4376       210863 :   profile_count rec_cnt = profile_count::zero ();
    4377       210863 :   profile_count nonrec_cnt = profile_count::zero ();
    4378       210863 :   bool interesting = false;
    4379       210863 :   bool non_self_recursive = false;
    4380       210863 :   *called_without_ipa_profile = false;
    4381              : 
    4382       967229 :   for (src = val->sources; src; src = src->next)
    4383              :     {
    4384       756366 :       struct cgraph_edge *cs = src->cs;
    4385      1876702 :       while (cs)
    4386              :         {
    4387      1120336 :           if (cgraph_edge_brings_value_p (cs, src, dest, val))
    4388              :             {
    4389       352552 :               count++;
    4390       352552 :               freq += cs->sreal_frequency ();
    4391       352552 :               interesting |= cs_interesting_for_ipcp_p (cs);
    4392       352552 :               if (cs->caller != dest)
    4393              :                 {
    4394       345741 :                   non_self_recursive = true;
    4395       345741 :                   if (cs->count.ipa ().initialized_p ())
    4396          953 :                     rec_cnt += cs->count.ipa ();
    4397              :                   else
    4398       344788 :                     *called_without_ipa_profile = true;
    4399              :                 }
    4400         6811 :               else if (cs->count.ipa ().initialized_p ())
    4401            0 :                 nonrec_cnt += cs->count.ipa ();
    4402              :               else
    4403         6811 :                 *called_without_ipa_profile = true;
    4404              :             }
    4405      1120336 :           cs = get_next_cgraph_edge_clone (cs);
    4406              :         }
    4407              :     }
    4408              : 
    4409              :   /* If the only edges bringing a value are self-recursive ones, do not bother
    4410              :      evaluating it.  */
    4411       210863 :   if (!non_self_recursive)
    4412              :     return false;
    4413              : 
    4414       150641 :   *freq_sum = freq;
    4415       150641 :   *caller_count = count;
    4416       150641 :   *rec_count_sum = rec_cnt;
    4417       150641 :   *nonrec_count_sum = nonrec_cnt;
    4418              : 
    4419       150641 :   return interesting;
    4420              : }
    4421              : 
    4422              : /* Given a NODE, and a set of its CALLERS, try to adjust order of the callers
    4423              :    to let a non-self-recursive caller be the first element.  Thus, we can
    4424              :    simplify intersecting operations on values that arrive from all of these
    4425              :    callers, especially when there exists self-recursive call.  Return true if
    4426              :    this kind of adjustment is possible.  */
    4427              : 
    4428              : static bool
    4429        57481 : adjust_callers_for_value_intersection (vec<cgraph_edge *> &callers,
    4430              :                                        cgraph_node *node)
    4431              : {
    4432        61694 :   for (unsigned i = 0; i < callers.length (); i++)
    4433              :     {
    4434        61598 :       cgraph_edge *cs = callers[i];
    4435              : 
    4436        61598 :       if (cs->caller != node)
    4437              :         {
    4438        57385 :           if (i > 0)
    4439              :             {
    4440         1967 :               callers[i] = callers[0];
    4441         1967 :               callers[0] = cs;
    4442              :             }
    4443        57385 :           return true;
    4444              :         }
    4445              :     }
    4446              :   return false;
    4447              : }
    4448              : 
    4449              : /* Return a vector of incoming edges that do bring value VAL to node DEST.  It
    4450              :    is assumed their number is known and equal to CALLER_COUNT.  */
    4451              : 
    4452              : template <typename valtype>
    4453              : static auto_vec<cgraph_edge *>
    4454       150288 : gather_edges_for_value (ipcp_value<valtype> *val, cgraph_node *dest,
    4455              :                         int caller_count)
    4456              : {
    4457              :   ipcp_value_source<valtype> *src;
    4458       150288 :   auto_vec<cgraph_edge *> ret (caller_count);
    4459              : 
    4460       515954 :   for (src = val->sources; src; src = src->next)
    4461              :     {
    4462       365666 :       struct cgraph_edge *cs = src->cs;
    4463       818265 :       while (cs)
    4464              :         {
    4465       452599 :           if (cgraph_edge_brings_value_p (cs, src, dest, val))
    4466       349461 :             ret.quick_push (cs);
    4467       452599 :           cs = get_next_cgraph_edge_clone (cs);
    4468              :         }
    4469              :     }
    4470              : 
    4471       150288 :   if (caller_count > 1)
    4472        40388 :     adjust_callers_for_value_intersection (ret, dest);
    4473              : 
    4474       150288 :   return ret;
    4475              : }
    4476              : 
    4477              : /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
    4478              :    Return it or NULL if for some reason it cannot be created.  FORCE_LOAD_REF
    4479              :    should be set to true when the reference created for the constant should be
    4480              :    a load one and not an address one because the corresponding parameter p is
    4481              :    only used as *p.  */
    4482              : 
    4483              : static struct ipa_replace_map *
    4484        24293 : get_replacement_map (class ipa_node_params *info, tree value, int parm_num,
    4485              :                      bool force_load_ref)
    4486              : {
    4487        24293 :   struct ipa_replace_map *replace_map;
    4488              : 
    4489        24293 :   replace_map = ggc_alloc<ipa_replace_map> ();
    4490        24293 :   if (dump_file)
    4491              :     {
    4492          171 :       fprintf (dump_file, "    replacing ");
    4493          171 :       ipa_dump_param (dump_file, info, parm_num);
    4494              : 
    4495          171 :       fprintf (dump_file, " with const ");
    4496          171 :       print_generic_expr (dump_file, value);
    4497              : 
    4498          171 :       if (force_load_ref)
    4499           11 :         fprintf (dump_file, " - forcing load reference\n");
    4500              :       else
    4501          160 :         fprintf (dump_file, "\n");
    4502              :     }
    4503        24293 :   replace_map->parm_num = parm_num;
    4504        24293 :   replace_map->new_tree = value;
    4505        24293 :   replace_map->force_load_ref = force_load_ref;
    4506        24293 :   return replace_map;
    4507              : }
    4508              : 
    4509              : /* Dump new profiling counts of NODE.  SPEC is true when NODE is a specialzied
    4510              :    one, otherwise it will be referred to as the original node.  */
    4511              : 
    4512              : static void
    4513            4 : dump_profile_updates (cgraph_node *node, bool spec)
    4514              : {
    4515            4 :   if (spec)
    4516            2 :     fprintf (dump_file, "     setting count of the specialized node %s to ",
    4517              :              node->dump_name ());
    4518              :   else
    4519            2 :     fprintf (dump_file, "     setting count of the original node %s to ",
    4520              :              node->dump_name ());
    4521              : 
    4522            4 :   node->count.dump (dump_file);
    4523            4 :   fprintf (dump_file, "\n");
    4524            6 :   for (cgraph_edge *cs = node->callees; cs; cs = cs->next_callee)
    4525              :     {
    4526            2 :       fprintf (dump_file, "       edge to %s has count ",
    4527            2 :                cs->callee->dump_name ());
    4528            2 :       cs->count.dump (dump_file);
    4529            2 :       fprintf (dump_file, "\n");
    4530              :     }
    4531            4 : }
    4532              : 
    4533              : /* With partial train run we do not want to assume that original's count is
    4534              :    zero whenever we redurect all executed edges to clone.  Simply drop profile
    4535              :    to local one in this case.  In eany case, return the new value.  ORIG_NODE
    4536              :    is the original node and its count has not been updaed yet.  */
    4537              : 
    4538              : profile_count
    4539           16 : lenient_count_portion_handling (profile_count remainder, cgraph_node *orig_node)
    4540              : {
    4541           32 :   if (remainder.ipa_p () && !remainder.ipa ().nonzero_p ()
    4542           24 :       && orig_node->count.ipa_p () && orig_node->count.ipa ().nonzero_p ()
    4543            4 :       && opt_for_fn (orig_node->decl, flag_profile_partial_training))
    4544            0 :     remainder = orig_node->count.guessed_local ();
    4545              : 
    4546           16 :   return remainder;
    4547              : }
    4548              : 
    4549              : /* Structure to sum counts coming from nodes other than the original node and
    4550              :    its clones.  */
    4551              : 
    4552              : struct gather_other_count_struct
    4553              : {
    4554              :   cgraph_node *orig;
    4555              :   profile_count other_count;
    4556              : };
    4557              : 
    4558              : /* Worker callback of call_for_symbol_thunks_and_aliases summing the number of
    4559              :    counts that come from non-self-recursive calls..  */
    4560              : 
    4561              : static bool
    4562            8 : gather_count_of_non_rec_edges (cgraph_node *node, void *data)
    4563              : {
    4564            8 :   gather_other_count_struct *desc = (gather_other_count_struct *) data;
    4565           20 :   for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
    4566           12 :     if (cs->caller != desc->orig && cs->caller->clone_of != desc->orig)
    4567            0 :       if (cs->count.ipa ().initialized_p ())
    4568            0 :         desc->other_count += cs->count.ipa ();
    4569            8 :   return false;
    4570              : }
    4571              : 
    4572              : /* Structure to help analyze if we need to boost counts of some clones of some
    4573              :    non-recursive edges to match the new callee count.  */
    4574              : 
    4575              : struct desc_incoming_count_struct
    4576              : {
    4577              :   cgraph_node *orig;
    4578              :   hash_set <cgraph_edge *> *processed_edges;
    4579              :   profile_count count;
    4580              :   unsigned unproc_orig_rec_edges;
    4581              : };
    4582              : 
    4583              : /* Go over edges calling NODE and its thunks and gather information about
    4584              :    incoming counts so that we know if we need to make any adjustments.  */
    4585              : 
    4586              : static void
    4587            8 : analyze_clone_icoming_counts (cgraph_node *node,
    4588              :                               desc_incoming_count_struct *desc)
    4589              : {
    4590           20 :   for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
    4591           12 :     if (cs->caller->thunk)
    4592              :       {
    4593            0 :         analyze_clone_icoming_counts (cs->caller, desc);
    4594            0 :         continue;
    4595              :       }
    4596              :     else
    4597              :       {
    4598           12 :         if (cs->count.initialized_p ())
    4599           12 :           desc->count += cs->count.ipa ();
    4600           12 :         if (!desc->processed_edges->contains (cs)
    4601           12 :             && cs->caller->clone_of == desc->orig)
    4602            4 :           desc->unproc_orig_rec_edges++;
    4603              :       }
    4604            8 : }
    4605              : 
    4606              : /* If caller edge counts of a clone created for a self-recursive arithmetic
    4607              :    jump function must be adjusted because it is coming from a the "seed" clone
    4608              :    for the first value and so has been excessively scaled back as if it was not
    4609              :    a recursive call, adjust it so that the incoming counts of NODE match its
    4610              :    count. NODE is the node or its thunk.  */
    4611              : 
    4612              : static void
    4613            0 : adjust_clone_incoming_counts (cgraph_node *node,
    4614              :                               desc_incoming_count_struct *desc)
    4615              : {
    4616            0 :   for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
    4617            0 :     if (cs->caller->thunk)
    4618              :       {
    4619            0 :         adjust_clone_incoming_counts (cs->caller, desc);
    4620            0 :         profile_count sum = profile_count::zero ();
    4621            0 :         for (cgraph_edge *e = cs->caller->callers; e; e = e->next_caller)
    4622            0 :           if (e->count.initialized_p ())
    4623            0 :             sum += e->count.ipa ();
    4624            0 :         cs->count = cs->count.combine_with_ipa_count (sum);
    4625              :       }
    4626            0 :     else if (!desc->processed_edges->contains (cs)
    4627            0 :              && cs->caller->clone_of == desc->orig
    4628            0 :              && cs->count.compatible_p (desc->count))
    4629              :       {
    4630            0 :         cs->count += desc->count;
    4631            0 :         if (dump_file)
    4632              :           {
    4633            0 :             fprintf (dump_file, "       Adjusted count of an incoming edge of "
    4634            0 :                      "a clone %s -> %s to ", cs->caller->dump_name (),
    4635            0 :                      cs->callee->dump_name ());
    4636            0 :             cs->count.dump (dump_file);
    4637            0 :             fprintf (dump_file, "\n");
    4638              :           }
    4639              :       }
    4640            0 : }
    4641              : 
    4642              : /* When ORIG_NODE has been cloned for values which have been generated fora
    4643              :    self-recursive call as a result of an arithmetic pass-through
    4644              :    jump-functions, adjust its count together with counts of all such clones in
    4645              :    SELF_GEN_CLONES which also at this point contains ORIG_NODE itself.
    4646              : 
    4647              :    The function sums the counts of the original node and all its clones that
    4648              :    cannot be attributed to a specific clone because it comes from a
    4649              :    non-recursive edge.  This sum is then evenly divided between the clones and
    4650              :    on top of that each one gets all the counts which can be attributed directly
    4651              :    to it.  */
    4652              : 
    4653              : static void
    4654           33 : update_counts_for_self_gen_clones (cgraph_node *orig_node,
    4655              :                                    const vec<cgraph_node *> &self_gen_clones)
    4656              : {
    4657           33 :   profile_count redist_sum = orig_node->count.ipa ();
    4658           33 :   if (!redist_sum.nonzero_p ())
    4659              :     return;
    4660              : 
    4661            4 :   if (dump_file)
    4662            0 :     fprintf (dump_file, "     Updating profile of self recursive clone "
    4663              :              "series\n");
    4664              : 
    4665            4 :   gather_other_count_struct gocs;
    4666            4 :   gocs.orig = orig_node;
    4667            4 :   gocs.other_count = profile_count::zero ();
    4668              : 
    4669            4 :   auto_vec <profile_count, 8> other_edges_count;
    4670           20 :   for (cgraph_node *n : self_gen_clones)
    4671              :     {
    4672            8 :       gocs.other_count = profile_count::zero ();
    4673            8 :       n->call_for_symbol_thunks_and_aliases (gather_count_of_non_rec_edges,
    4674              :                                              &gocs, false);
    4675            8 :       other_edges_count.safe_push (gocs.other_count);
    4676            8 :       redist_sum -= gocs.other_count;
    4677              :     }
    4678              : 
    4679            4 :   hash_set<cgraph_edge *> processed_edges;
    4680            4 :   unsigned i = 0;
    4681           20 :   for (cgraph_node *n : self_gen_clones)
    4682              :     {
    4683            8 :       profile_count new_count
    4684           16 :         = (redist_sum / self_gen_clones.length () + other_edges_count[i]);
    4685            8 :       new_count = lenient_count_portion_handling (new_count, orig_node);
    4686            8 :       n->scale_profile_to (new_count);
    4687           16 :       for (cgraph_edge *cs = n->callees; cs; cs = cs->next_callee)
    4688            8 :         processed_edges.add (cs);
    4689              : 
    4690            8 :       i++;
    4691              :     }
    4692              : 
    4693              :   /* There are still going to be edges to ORIG_NODE that have one or more
    4694              :      clones coming from another node clone in SELF_GEN_CLONES and which we
    4695              :      scaled by the same amount, which means that the total incoming sum of
    4696              :      counts to ORIG_NODE will be too high, scale such edges back.  */
    4697            8 :   for (cgraph_edge *cs = orig_node->callees; cs; cs = cs->next_callee)
    4698              :     {
    4699            4 :       if (cs->callee->ultimate_alias_target () == orig_node)
    4700              :         {
    4701            4 :           unsigned den = 0;
    4702           18 :           for (cgraph_edge *e = cs; e; e = get_next_cgraph_edge_clone (e))
    4703           14 :             if (e->callee->ultimate_alias_target () == orig_node
    4704           14 :                 && processed_edges.contains (e))
    4705            8 :               den++;
    4706            4 :           if (den > 0)
    4707           18 :             for (cgraph_edge *e = cs; e; e = get_next_cgraph_edge_clone (e))
    4708           14 :               if (e->callee->ultimate_alias_target () == orig_node
    4709            8 :                   && processed_edges.contains (e)
    4710              :                   /* If count is not IPA, this adjustment makes verifier
    4711              :                      unhappy, since we expect bb->count to match e->count.
    4712              :                      We may add a flag to mark edge conts that has been
    4713              :                      modified by IPA code, but so far it does not seem
    4714              :                      to be worth the effort.  With local counts the profile
    4715              :                      will not propagate at IPA level.  */
    4716           30 :                   && e->count.ipa_p ())
    4717            8 :                 e->count /= den;
    4718              :         }
    4719              :     }
    4720              : 
    4721              :   /* Edges from the seeds of the valus generated for arithmetic jump-functions
    4722              :      along self-recursive edges are likely to have fairly low count and so
    4723              :      edges from them to nodes in the self_gen_clones do not correspond to the
    4724              :      artificially distributed count of the nodes, the total sum of incoming
    4725              :      edges to some clones might be too low.  Detect this situation and correct
    4726              :      it.  */
    4727           20 :   for (cgraph_node *n : self_gen_clones)
    4728              :     {
    4729            8 :       if (!n->count.ipa ().nonzero_p ())
    4730            0 :         continue;
    4731              : 
    4732            8 :       desc_incoming_count_struct desc;
    4733            8 :       desc.orig = orig_node;
    4734            8 :       desc.processed_edges = &processed_edges;
    4735            8 :       desc.count = profile_count::zero ();
    4736            8 :       desc.unproc_orig_rec_edges = 0;
    4737            8 :       analyze_clone_icoming_counts (n, &desc);
    4738              : 
    4739            8 :       if (n->count.differs_from_p (desc.count))
    4740              :         {
    4741            0 :           if (n->count > desc.count
    4742            0 :               && desc.unproc_orig_rec_edges > 0)
    4743              :             {
    4744            0 :               desc.count = n->count - desc.count;
    4745            0 :               desc.count = desc.count /= desc.unproc_orig_rec_edges;
    4746            0 :               adjust_clone_incoming_counts (n, &desc);
    4747              :             }
    4748            0 :           else if (dump_file)
    4749            0 :             fprintf (dump_file,
    4750              :                      "       Unable to fix up incoming counts for %s.\n",
    4751              :                      n->dump_name ());
    4752              :         }
    4753              :     }
    4754              : 
    4755            4 :   if (dump_file)
    4756            0 :     for (cgraph_node *n : self_gen_clones)
    4757            0 :       dump_profile_updates (n, n != orig_node);
    4758            4 :   return;
    4759            4 : }
    4760              : 
    4761              : /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
    4762              :    their profile information to reflect this.  This function should not be used
    4763              :    for clones generated for arithmetic pass-through jump functions on a
    4764              :    self-recursive call graph edge, that situation is handled by
    4765              :    update_counts_for_self_gen_clones.  */
    4766              : 
    4767              : static void
    4768         4177 : update_profiling_info (struct cgraph_node *orig_node,
    4769              :                        struct cgraph_node *new_node)
    4770              : {
    4771         4177 :   struct caller_statistics stats;
    4772         4177 :   profile_count new_sum;
    4773         4177 :   profile_count remainder, orig_node_count = orig_node->count.ipa ();
    4774              : 
    4775         4177 :   if (!orig_node_count.nonzero_p ())
    4776         4169 :     return;
    4777              : 
    4778            8 :   if (dump_file)
    4779              :     {
    4780            2 :       fprintf (dump_file, "     Updating profile from original count: ");
    4781            2 :       orig_node_count.dump (dump_file);
    4782            2 :       fprintf (dump_file, "\n");
    4783              :     }
    4784              : 
    4785            8 :   init_caller_stats (&stats, new_node);
    4786            8 :   new_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
    4787              :                                               false);
    4788            8 :   new_sum = stats.count_sum;
    4789              : 
    4790            8 :   bool orig_edges_processed = false;
    4791            8 :   if (new_sum > orig_node_count)
    4792              :     {
    4793              :       /* Profile has alreay gone astray, keep what we have but lower it
    4794              :          to global0adjusted or to local if we have partial training.  */
    4795            0 :       if (opt_for_fn (orig_node->decl, flag_profile_partial_training))
    4796            0 :         orig_node->make_profile_local ();
    4797            0 :       if (new_sum.quality () == AFDO)
    4798            0 :         orig_node->make_profile_global0 (GUESSED_GLOBAL0_AFDO);
    4799              :       else
    4800            0 :         orig_node->make_profile_global0 (GUESSED_GLOBAL0_ADJUSTED);
    4801              :       orig_edges_processed = true;
    4802              :     }
    4803            8 :   else if (stats.rec_count_sum.nonzero_p ())
    4804              :     {
    4805            0 :       int new_nonrec_calls = stats.n_nonrec_calls;
    4806              :       /* There are self-recursive edges which are likely to bring in the
    4807              :          majority of calls but which we must divide in between the original and
    4808              :          new node.  */
    4809            0 :       init_caller_stats (&stats, orig_node);
    4810            0 :       orig_node->call_for_symbol_thunks_and_aliases (gather_caller_stats,
    4811              :                                                      &stats, false);
    4812            0 :       int orig_nonrec_calls = stats.n_nonrec_calls;
    4813            0 :       profile_count orig_nonrec_call_count = stats.count_sum;
    4814              : 
    4815            0 :       if (orig_node->local)
    4816              :         {
    4817            0 :           if (!orig_nonrec_call_count.nonzero_p ())
    4818              :             {
    4819            0 :               if (dump_file)
    4820            0 :                 fprintf (dump_file, "       The original is local and the only "
    4821              :                          "incoming edges from non-dead callers with nonzero "
    4822              :                          "counts are self-recursive, assuming it is cold.\n");
    4823              :               /* The NEW_NODE count and counts of all its outgoing edges
    4824              :                  are still unmodified copies of ORIG_NODE's.  Just clear
    4825              :                  the latter and bail out.  */
    4826            0 :               if (opt_for_fn (orig_node->decl, flag_profile_partial_training))
    4827            0 :                 orig_node->make_profile_local ();
    4828            0 :               else if (orig_nonrec_call_count.quality () == AFDO)
    4829            0 :                 orig_node->make_profile_global0 (GUESSED_GLOBAL0_AFDO);
    4830              :               else
    4831            0 :                 orig_node->make_profile_global0 (GUESSED_GLOBAL0_ADJUSTED);
    4832            0 :               return;
    4833              :             }
    4834              :         }
    4835              :       else
    4836              :         {
    4837              :           /* Let's behave as if there was another caller that accounts for all
    4838              :              the calls that were either indirect or from other compilation
    4839              :              units. */
    4840            0 :           orig_nonrec_calls++;
    4841            0 :           profile_count pretend_caller_count
    4842            0 :             = (orig_node_count - new_sum - orig_nonrec_call_count
    4843            0 :                - stats.rec_count_sum);
    4844            0 :           orig_nonrec_call_count += pretend_caller_count;
    4845              :         }
    4846              : 
    4847              :       /* Divide all "unexplained" counts roughly proportionally to sums of
    4848              :          counts of non-recursive calls.
    4849              : 
    4850              :          We put rather arbitrary limits on how many counts we claim because the
    4851              :          number of non-self-recursive incoming count is only a rough guideline
    4852              :          and there are cases (such as mcf) where using it blindly just takes
    4853              :          too many.  And if lattices are considered in the opposite order we
    4854              :          could also take too few.  */
    4855            0 :       profile_count unexp = orig_node_count - new_sum - orig_nonrec_call_count;
    4856              : 
    4857            0 :       int limit_den = 2 * (orig_nonrec_calls + new_nonrec_calls);
    4858            0 :       profile_count new_part = unexp.apply_scale (limit_den - 1, limit_den);
    4859            0 :       profile_count den = new_sum + orig_nonrec_call_count;
    4860            0 :       if (den.nonzero_p ())
    4861            0 :         new_part = MIN (unexp.apply_scale (new_sum, den), new_part);
    4862            0 :       new_part = MAX (new_part,
    4863              :                       unexp.apply_scale (new_nonrec_calls, limit_den));
    4864            0 :       if (dump_file)
    4865              :         {
    4866            0 :           fprintf (dump_file, "       Claiming ");
    4867            0 :           new_part.dump (dump_file);
    4868            0 :           fprintf (dump_file, " of unexplained ");
    4869            0 :           unexp.dump (dump_file);
    4870            0 :           fprintf (dump_file, " counts because of self-recursive "
    4871              :                    "calls\n");
    4872              :         }
    4873            0 :       new_sum += new_part;
    4874            0 :       remainder = lenient_count_portion_handling (orig_node_count - new_sum,
    4875              :                                                   orig_node);
    4876              :     }
    4877              :   else
    4878            8 :     remainder = lenient_count_portion_handling (orig_node_count - new_sum,
    4879              :                                                 orig_node);
    4880              : 
    4881            8 :   new_node->scale_profile_to (new_sum);
    4882              : 
    4883            8 :   if (!orig_edges_processed)
    4884            8 :     orig_node->scale_profile_to (remainder);
    4885              : 
    4886            8 :   if (dump_file)
    4887              :     {
    4888            2 :       dump_profile_updates (new_node, true);
    4889            2 :       dump_profile_updates (orig_node, false);
    4890              :     }
    4891              : }
    4892              : 
    4893              : /* Update the respective profile of specialized NEW_NODE and the original
    4894              :    ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
    4895              :    have been redirected to the specialized version.  */
    4896              : 
    4897              : static void
    4898            0 : update_specialized_profile (struct cgraph_node *new_node,
    4899              :                             struct cgraph_node *orig_node,
    4900              :                             profile_count redirected_sum)
    4901              : {
    4902            0 :   if (dump_file)
    4903              :     {
    4904            0 :       fprintf (dump_file, "    the sum of counts of redirected  edges is ");
    4905            0 :       redirected_sum.dump (dump_file);
    4906            0 :       fprintf (dump_file, "\n    old ipa count of the original node is ");
    4907            0 :       orig_node->count.dump (dump_file);
    4908            0 :       fprintf (dump_file, "\n");
    4909              :     }
    4910            0 :   if (!orig_node->count.ipa ().nonzero_p ()
    4911            0 :       || !redirected_sum.nonzero_p ())
    4912            0 :     return;
    4913              : 
    4914            0 :   orig_node->scale_profile_to
    4915            0 :     (lenient_count_portion_handling (orig_node->count.ipa () - redirected_sum,
    4916              :                                      orig_node));
    4917              : 
    4918            0 :   new_node->scale_profile_to (new_node->count.ipa () + redirected_sum);
    4919              : 
    4920            0 :   if (dump_file)
    4921              :     {
    4922            0 :       dump_profile_updates (new_node, true);
    4923            0 :       dump_profile_updates (orig_node, false);
    4924              :     }
    4925              : }
    4926              : 
    4927              : static void adjust_references_in_caller (cgraph_edge *cs,
    4928              :                                          symtab_node *symbol, int index);
    4929              : 
    4930              : /* Simple structure to pass a symbol and index (with same meaning as parameters
    4931              :    of adjust_references_in_caller) through a void* parameter of a
    4932              :    call_for_symbol_thunks_and_aliases callback. */
    4933              : struct symbol_and_index_together
    4934              : {
    4935              :   symtab_node *symbol;
    4936              :   int index;
    4937              : };
    4938              : 
    4939              : /* Worker callback of call_for_symbol_thunks_and_aliases to recursively call
    4940              :    adjust_references_in_caller on edges up in the call-graph, if necessary. */
    4941              : static bool
    4942            8 : adjust_refs_in_act_callers (struct cgraph_node *node, void *data)
    4943              : {
    4944            8 :   symbol_and_index_together *pack = (symbol_and_index_together *) data;
    4945           38 :   for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
    4946           30 :     if (!cs->caller->thunk)
    4947           30 :       adjust_references_in_caller (cs, pack->symbol, pack->index);
    4948            8 :   return false;
    4949              : }
    4950              : 
    4951              : /* At INDEX of a function being called by CS there is an ADDR_EXPR of a
    4952              :    variable which is only dereferenced and which is represented by SYMBOL.  See
    4953              :    if we can remove ADDR reference in callers assosiated witht the call. */
    4954              : 
    4955              : static void
    4956          400 : adjust_references_in_caller (cgraph_edge *cs, symtab_node *symbol, int index)
    4957              : {
    4958          400 :   ipa_edge_args *args = ipa_edge_args_sum->get (cs);
    4959          400 :   ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, index);
    4960          400 :   if (jfunc->type == IPA_JF_CONST)
    4961              :     {
    4962          382 :       ipa_ref *to_del = cs->caller->find_reference (symbol, cs->call_stmt,
    4963              :                                                     cs->lto_stmt_uid,
    4964              :                                                     IPA_REF_ADDR);
    4965          382 :       if (!to_del)
    4966          392 :         return;
    4967          382 :       to_del->remove_reference ();
    4968          382 :       ipa_zap_jf_refdesc (jfunc);
    4969          382 :       if (dump_file)
    4970           22 :         fprintf (dump_file, "    Removed a reference from %s to %s.\n",
    4971           11 :                  cs->caller->dump_name (), symbol->dump_name ());
    4972          382 :       return;
    4973              :     }
    4974              : 
    4975           18 :   if (jfunc->type != IPA_JF_PASS_THROUGH
    4976           18 :       || ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR
    4977           36 :       || ipa_get_jf_pass_through_refdesc_decremented (jfunc))
    4978              :     return;
    4979              : 
    4980           18 :   int fidx = ipa_get_jf_pass_through_formal_id (jfunc);
    4981           18 :   cgraph_node *caller = cs->caller;
    4982           18 :   ipa_node_params *caller_info = ipa_node_params_sum->get (caller);
    4983              :   /* TODO: This consistency check may be too big and not really
    4984              :      that useful.  Consider removing it.  */
    4985           18 :   tree cst;
    4986           18 :   if (caller_info->ipcp_orig_node)
    4987           17 :     cst = caller_info->known_csts[fidx];
    4988              :   else
    4989              :     {
    4990            1 :       ipcp_lattice<tree> *lat = ipa_get_scalar_lat (caller_info, fidx);
    4991            1 :       gcc_assert (lat->is_single_const ());
    4992            1 :       cst = lat->values->value;
    4993              :     }
    4994           18 :   gcc_assert (TREE_CODE (cst) == ADDR_EXPR
    4995              :               && (symtab_node::get (get_base_address (TREE_OPERAND (cst, 0)))
    4996              :                   == symbol));
    4997              : 
    4998           18 :   int cuses = ipa_get_controlled_uses (caller_info, fidx);
    4999           18 :   if (cuses == IPA_UNDESCRIBED_USE)
    5000              :     return;
    5001           18 :   gcc_assert (cuses > 0);
    5002           18 :   cuses--;
    5003           18 :   ipa_set_controlled_uses (caller_info, fidx, cuses);
    5004           18 :   ipa_set_jf_pass_through_refdesc_decremented (jfunc, true);
    5005           18 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5006            3 :     fprintf (dump_file, "    Controlled uses of parameter %i of %s dropped "
    5007              :              "to %i.\n", fidx, caller->dump_name (), cuses);
    5008           18 :   if (cuses)
    5009              :     return;
    5010              : 
    5011            8 :   if (caller_info->ipcp_orig_node)
    5012              :     {
    5013              :       /* Cloning machinery has created a reference here, we need to either
    5014              :          remove it or change it to a read one.  */
    5015            7 :       ipa_ref *to_del = caller->find_reference (symbol, NULL, 0, IPA_REF_ADDR);
    5016            7 :       if (to_del)
    5017              :         {
    5018            7 :           to_del->remove_reference ();
    5019            7 :           if (dump_file)
    5020            6 :             fprintf (dump_file, "    Removed a reference from %s to %s.\n",
    5021            3 :                      cs->caller->dump_name (), symbol->dump_name ());
    5022            7 :           if (ipa_get_param_load_dereferenced (caller_info, fidx))
    5023              :             {
    5024            3 :               caller->create_reference (symbol, IPA_REF_LOAD, NULL);
    5025            3 :               if (dump_file)
    5026            2 :                 fprintf (dump_file,
    5027              :                          "      ...and replaced it with LOAD one.\n");
    5028              :             }
    5029              :         }
    5030              :     }
    5031              : 
    5032            8 :   symbol_and_index_together pack;
    5033            8 :   pack.symbol = symbol;
    5034            8 :   pack.index = fidx;
    5035            8 :   if (caller->can_change_signature)
    5036            8 :     caller->call_for_symbol_thunks_and_aliases (adjust_refs_in_act_callers,
    5037              :                                                 &pack, true);
    5038              : }
    5039              : 
    5040              : 
    5041              : /* Return true if we would like to remove a parameter from NODE when cloning it
    5042              :    with KNOWN_CSTS scalar constants.  */
    5043              : 
    5044              : static bool
    5045        19912 : want_remove_some_param_p (cgraph_node *node, vec<tree> known_csts)
    5046              : {
    5047        19912 :   auto_vec<bool, 16> surviving;
    5048        19912 :   bool filled_vec = false;
    5049        19912 :   ipa_node_params *info = ipa_node_params_sum->get (node);
    5050        19912 :   int i, count = ipa_get_param_count (info);
    5051              : 
    5052        39555 :   for (i = 0; i < count; i++)
    5053              :     {
    5054        35041 :       if (!known_csts[i] && ipa_is_param_used (info, i))
    5055        19643 :        continue;
    5056              : 
    5057        15398 :       if (!filled_vec)
    5058              :        {
    5059        15398 :          clone_info *info = clone_info::get (node);
    5060        15398 :          if (!info || !info->param_adjustments)
    5061              :            return true;
    5062            0 :          info->param_adjustments->get_surviving_params (&surviving);
    5063            0 :          filled_vec = true;
    5064              :        }
    5065            0 :       if (surviving.length() < (unsigned) i &&  surviving[i])
    5066              :        return true;
    5067              :     }
    5068              :   return false;
    5069        19912 : }
    5070              : 
    5071              : /* Create a specialized version of NODE with known constants in KNOWN_CSTS,
    5072              :    known contexts in KNOWN_CONTEXTS and known aggregate values in AGGVALS and
    5073              :    redirect all edges in CALLERS to it.  */
    5074              : 
    5075              : static struct cgraph_node *
    5076        21316 : create_specialized_node (struct cgraph_node *node,
    5077              :                          vec<tree> known_csts,
    5078              :                          vec<ipa_polymorphic_call_context> known_contexts,
    5079              :                          vec<ipa_argagg_value, va_gc> *aggvals,
    5080              :                          vec<cgraph_edge *> &callers)
    5081              : {
    5082        21316 :   ipa_node_params *new_info, *info = ipa_node_params_sum->get (node);
    5083        21316 :   vec<ipa_replace_map *, va_gc> *replace_trees = NULL;
    5084        21316 :   vec<ipa_adjusted_param, va_gc> *new_params = NULL;
    5085        21316 :   struct cgraph_node *new_node;
    5086        21316 :   int i, count = ipa_get_param_count (info);
    5087        21316 :   clone_info *cinfo = clone_info::get (node);
    5088            0 :   ipa_param_adjustments *old_adjustments = cinfo
    5089        21316 :                                            ? cinfo->param_adjustments : NULL;
    5090        21316 :   ipa_param_adjustments *new_adjustments;
    5091        21316 :   gcc_assert (!info->ipcp_orig_node);
    5092        21316 :   gcc_assert (node->can_change_signature
    5093              :               || !old_adjustments);
    5094              : 
    5095        19912 :   if (old_adjustments)
    5096              :     {
    5097              :       /* At the moment all IPA optimizations should use the number of
    5098              :          parameters of the prevailing decl as the m_always_copy_start.
    5099              :          Handling any other value would complicate the code below, so for the
    5100              :          time bing let's only assert it is so.  */
    5101            0 :       gcc_assert (old_adjustments->m_always_copy_start == count
    5102              :                   || old_adjustments->m_always_copy_start < 0);
    5103            0 :       int old_adj_count = vec_safe_length (old_adjustments->m_adj_params);
    5104            0 :       for (i = 0; i < old_adj_count; i++)
    5105              :         {
    5106            0 :           ipa_adjusted_param *old_adj = &(*old_adjustments->m_adj_params)[i];
    5107            0 :           if (!node->can_change_signature
    5108            0 :               || old_adj->op != IPA_PARAM_OP_COPY
    5109            0 :               || (!known_csts[old_adj->base_index]
    5110            0 :                   && ipa_is_param_used (info, old_adj->base_index)))
    5111              :             {
    5112            0 :               ipa_adjusted_param new_adj = *old_adj;
    5113              : 
    5114            0 :               new_adj.prev_clone_adjustment = true;
    5115            0 :               new_adj.prev_clone_index = i;
    5116            0 :               vec_safe_push (new_params, new_adj);
    5117              :             }
    5118              :         }
    5119            0 :       bool skip_return = old_adjustments->m_skip_return;
    5120            0 :       new_adjustments = (new (ggc_alloc <ipa_param_adjustments> ())
    5121              :                          ipa_param_adjustments (new_params, count,
    5122            0 :                                                 skip_return));
    5123              :     }
    5124        21316 :   else if (node->can_change_signature
    5125        21316 :            && want_remove_some_param_p (node, known_csts))
    5126              :     {
    5127        15398 :       ipa_adjusted_param adj;
    5128        15398 :       memset (&adj, 0, sizeof (adj));
    5129        15398 :       adj.op = IPA_PARAM_OP_COPY;
    5130        59836 :       for (i = 0; i < count; i++)
    5131        44438 :         if (!known_csts[i] && ipa_is_param_used (info, i))
    5132              :           {
    5133        15929 :             adj.base_index = i;
    5134        15929 :             adj.prev_clone_index = i;
    5135        15929 :             vec_safe_push (new_params, adj);
    5136              :           }
    5137        15398 :       new_adjustments = (new (ggc_alloc <ipa_param_adjustments> ())
    5138        15398 :                          ipa_param_adjustments (new_params, count, false));
    5139              :     }
    5140              :   else
    5141              :     new_adjustments = NULL;
    5142              : 
    5143        21316 :   auto_vec<cgraph_edge *, 2> self_recursive_calls;
    5144       186694 :   for (i = callers.length () - 1; i >= 0; i--)
    5145              :     {
    5146       144062 :       cgraph_edge *cs = callers[i];
    5147       144062 :       if (cs->caller == node)
    5148              :         {
    5149          131 :           self_recursive_calls.safe_push (cs);
    5150          131 :           callers.unordered_remove (i);
    5151              :         }
    5152              :     }
    5153        21316 :   replace_trees = cinfo ? vec_safe_copy (cinfo->tree_map) : NULL;
    5154        81076 :   for (i = 0; i < count; i++)
    5155              :     {
    5156        59760 :       tree t = known_csts[i];
    5157        59760 :       if (!t)
    5158        35467 :         continue;
    5159              : 
    5160        24293 :       gcc_checking_assert (TREE_CODE (t) != TREE_BINFO);
    5161              : 
    5162        24293 :       bool load_ref = false;
    5163        24293 :       symtab_node *ref_symbol;
    5164        24293 :       if (TREE_CODE (t) == ADDR_EXPR)
    5165              :         {
    5166         6493 :           tree base = get_base_address (TREE_OPERAND (t, 0));
    5167         6493 :           if (TREE_CODE (base) == VAR_DECL
    5168         3107 :               && ipa_get_controlled_uses (info, i) == 0
    5169          930 :               && ipa_get_param_load_dereferenced (info, i)
    5170         6878 :               && (ref_symbol = symtab_node::get (base)))
    5171              :             {
    5172          385 :               load_ref = true;
    5173          385 :               if (node->can_change_signature)
    5174         1399 :                 for (cgraph_edge *caller : callers)
    5175          370 :                   adjust_references_in_caller (caller, ref_symbol, i);
    5176              :             }
    5177              :         }
    5178              : 
    5179        24293 :       ipa_replace_map *replace_map = get_replacement_map (info, t, i, load_ref);
    5180        24293 :       if (replace_map)
    5181        24293 :         vec_safe_push (replace_trees, replace_map);
    5182              :     }
    5183              : 
    5184        63948 :   unsigned &suffix_counter = clone_num_suffixes->get_or_insert (
    5185        21316 :                                IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (
    5186              :                                  node->decl)));
    5187        21316 :   new_node = node->create_virtual_clone (callers, replace_trees,
    5188              :                                          new_adjustments, "constprop",
    5189              :                                          suffix_counter);
    5190        21316 :   suffix_counter++;
    5191              : 
    5192        21316 :   bool have_self_recursive_calls = !self_recursive_calls.is_empty ();
    5193        21447 :   for (unsigned j = 0; j < self_recursive_calls.length (); j++)
    5194              :     {
    5195          131 :       cgraph_edge *cs = get_next_cgraph_edge_clone (self_recursive_calls[j]);
    5196              :       /* Cloned edges can disappear during cloning as speculation can be
    5197              :          resolved, check that we have one and that it comes from the last
    5198              :          cloning.  */
    5199          131 :       if (cs && cs->caller == new_node)
    5200          130 :         cs->redirect_callee_duplicating_thunks (new_node);
    5201              :       /* Any future code that would make more than one clone of an outgoing
    5202              :          edge would confuse this mechanism, so let's check that does not
    5203              :          happen.  */
    5204          130 :       gcc_checking_assert (!cs
    5205              :                            || !get_next_cgraph_edge_clone (cs)
    5206              :                            || get_next_cgraph_edge_clone (cs)->caller != new_node);
    5207              :     }
    5208        21316 :   if (have_self_recursive_calls)
    5209          121 :     new_node->expand_all_artificial_thunks ();
    5210              : 
    5211        21316 :   ipa_set_node_agg_value_chain (new_node, aggvals);
    5212        50899 :   for (const ipa_argagg_value &av : aggvals)
    5213        29583 :     new_node->maybe_create_reference (av.value, NULL);
    5214              : 
    5215        21316 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5216              :     {
    5217           90 :       fprintf (dump_file, "     the new node is %s.\n", new_node->dump_name ());
    5218           90 :       if (known_contexts.exists ())
    5219              :         {
    5220            0 :           for (i = 0; i < count; i++)
    5221            0 :             if (!known_contexts[i].useless_p ())
    5222              :               {
    5223            0 :                 fprintf (dump_file, "     known ctx %i is ", i);
    5224            0 :                 known_contexts[i].dump (dump_file);
    5225              :               }
    5226              :         }
    5227           90 :       if (aggvals)
    5228              :         {
    5229           48 :           fprintf (dump_file, "     Aggregate replacements:");
    5230           48 :           ipa_argagg_value_list avs (aggvals);
    5231           48 :           avs.dump (dump_file);
    5232              :         }
    5233              :     }
    5234              : 
    5235        21316 :   new_info = ipa_node_params_sum->get (new_node);
    5236        21316 :   new_info->ipcp_orig_node = node;
    5237        21316 :   new_node->ipcp_clone = true;
    5238        21316 :   new_info->known_csts = known_csts;
    5239        21316 :   new_info->known_contexts = known_contexts;
    5240              : 
    5241        21316 :   ipcp_discover_new_direct_edges (new_node, known_csts, known_contexts,
    5242              :                                   aggvals);
    5243              : 
    5244        21316 :   return new_node;
    5245        21316 : }
    5246              : 
    5247              : /* Return true if JFUNC, which describes a i-th parameter of call CS, is a
    5248              :    pass-through function to itself when the cgraph_node involved is not an
    5249              :    IPA-CP clone.  When SIMPLE is true, further check if JFUNC is a simple
    5250              :    no-operation pass-through.  */
    5251              : 
    5252              : static bool
    5253       776991 : self_recursive_pass_through_p (cgraph_edge *cs, ipa_jump_func *jfunc, int i,
    5254              :                                bool simple = true)
    5255              : {
    5256       776991 :   enum availability availability;
    5257       776991 :   if (jfunc->type == IPA_JF_PASS_THROUGH
    5258        80027 :       && cs->caller == cs->callee->function_symbol (&availability)
    5259        19350 :       && availability > AVAIL_INTERPOSABLE
    5260        19350 :       && (!simple || ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
    5261        19350 :       && ipa_get_jf_pass_through_formal_id (jfunc) == i
    5262        19350 :       && ipa_node_params_sum->get (cs->caller)
    5263       796341 :       && !ipa_node_params_sum->get (cs->caller)->ipcp_orig_node)
    5264              :     return true;
    5265              :   return false;
    5266              : }
    5267              : 
    5268              : /* Return true if JFUNC, which describes the i-th parameter of call CS, is an
    5269              :    ancestor function with zero offset to itself when the cgraph_node involved
    5270              :    is not an IPA-CP clone.  */
    5271              : 
    5272              : static bool
    5273       757653 : self_recursive_ancestor_p (cgraph_edge *cs, ipa_jump_func *jfunc, int i)
    5274              : {
    5275       757653 :   enum availability availability;
    5276       757653 :   if (jfunc->type == IPA_JF_ANCESTOR
    5277         3226 :       && cs->caller == cs->callee->function_symbol (&availability)
    5278            1 :       && availability > AVAIL_INTERPOSABLE
    5279            1 :       && ipa_get_jf_ancestor_offset (jfunc) == 0
    5280            1 :       && ipa_get_jf_ancestor_formal_id (jfunc) == i
    5281            1 :       && ipa_node_params_sum->get (cs->caller)
    5282       757654 :       && !ipa_node_params_sum->get (cs->caller)->ipcp_orig_node)
    5283              :     return true;
    5284              :   return false;
    5285              : }
    5286              : 
    5287              : /* Return true if JFUNC, which describes a part of an aggregate represented or
    5288              :    pointed to by the i-th parameter of call CS, is a pass-through function to
    5289              :    itself when the cgraph_node involved is not an IPA-CP clone..  When
    5290              :    SIMPLE is true, further check if JFUNC is a simple no-operation
    5291              :    pass-through.  */
    5292              : 
    5293              : static bool
    5294       333298 : self_recursive_agg_pass_through_p (const cgraph_edge *cs,
    5295              :                                    const ipa_agg_jf_item *jfunc,
    5296              :                                    int i, bool simple = true)
    5297              : {
    5298       333298 :   enum availability availability;
    5299       333298 :   if (cs->caller == cs->callee->function_symbol (&availability)
    5300         3690 :       && availability > AVAIL_INTERPOSABLE
    5301         3690 :       && jfunc->jftype == IPA_JF_LOAD_AGG
    5302          487 :       && jfunc->offset == jfunc->value.load_agg.offset
    5303          487 :       && (!simple || jfunc->value.pass_through.operation == NOP_EXPR)
    5304          487 :       && jfunc->value.pass_through.formal_id == i
    5305          481 :       && useless_type_conversion_p (jfunc->value.load_agg.type, jfunc->type)
    5306          481 :       && ipa_node_params_sum->get (cs->caller)
    5307       333779 :       && !ipa_node_params_sum->get (cs->caller)->ipcp_orig_node)
    5308              :     return true;
    5309              :   return false;
    5310              : }
    5311              : 
    5312              : /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
    5313              :    KNOWN_CSTS with constants that are also known for all of the CALLERS.  */
    5314              : 
    5315              : static void
    5316       167285 : find_scalar_values_for_callers_subset (vec<tree> &known_csts,
    5317              :                                        ipa_node_params *info,
    5318              :                                        const vec<cgraph_edge *> &callers)
    5319              : {
    5320       167285 :   int i, count = ipa_get_param_count (info);
    5321              : 
    5322       731155 :   for (i = 0; i < count; i++)
    5323              :     {
    5324       563870 :       ipcp_lattice<tree> *lat = ipa_get_scalar_lat (info, i);
    5325       563870 :       if (lat->bottom)
    5326       563870 :         continue;
    5327       550253 :       if (lat->is_single_const ())
    5328              :         {
    5329        29202 :           known_csts[i] = lat->values->value;
    5330        29202 :           continue;
    5331              :         }
    5332              : 
    5333       521051 :       struct cgraph_edge *cs;
    5334       521051 :       tree newval = NULL_TREE;
    5335       521051 :       int j;
    5336       521051 :       bool first = true;
    5337       521051 :       tree type = ipa_get_type (info, i);
    5338              : 
    5339       971803 :       FOR_EACH_VEC_ELT (callers, j, cs)
    5340              :         {
    5341       776449 :           struct ipa_jump_func *jump_func;
    5342       776449 :           tree t;
    5343              : 
    5344       776449 :           ipa_edge_args *args = ipa_edge_args_sum->get (cs);
    5345       776449 :           if (!args
    5346       776449 :               || i >= ipa_get_cs_argument_count (args)
    5347      1552867 :               || (i == 0
    5348       179303 :                   && call_passes_through_thunk (cs)))
    5349              :             {
    5350              :               newval = NULL_TREE;
    5351              :               break;
    5352              :             }
    5353       776348 :           jump_func = ipa_get_ith_jump_func (args, i);
    5354              : 
    5355              :           /* Besides simple pass-through jump function, arithmetic jump
    5356              :              function could also introduce argument-direct-pass-through for
    5357              :              self-feeding recursive call.  For example,
    5358              : 
    5359              :                 fn (int i)
    5360              :                 {
    5361              :                   fn (i & 1);
    5362              :                 }
    5363              : 
    5364              :              Given that i is 0, recursive propagation via (i & 1) also gets
    5365              :              0.  */
    5366       776348 :           if (self_recursive_pass_through_p (cs, jump_func, i, false))
    5367              :             {
    5368        18702 :               gcc_assert (newval);
    5369        18702 :               enum tree_code opcode
    5370        18702 :                 = ipa_get_jf_pass_through_operation (jump_func);
    5371        18702 :               tree op_type = (opcode == NOP_EXPR) ? NULL_TREE
    5372           49 :                 : ipa_get_jf_pass_through_op_type (jump_func);
    5373        18702 :               t = ipa_get_jf_arith_result (opcode, newval,
    5374              :                                 ipa_get_jf_pass_through_operand (jump_func),
    5375              :                                 op_type);
    5376        18702 :               t = ipacp_value_safe_for_type (type, t);
    5377              :             }
    5378       757646 :           else if (self_recursive_ancestor_p (cs, jump_func, i))
    5379            0 :             continue;
    5380              :           else
    5381       757646 :             t = ipa_value_from_jfunc (ipa_node_params_sum->get (cs->caller),
    5382              :                                       jump_func, type);
    5383       776348 :           if (!t
    5384       471955 :               || (newval
    5385       250880 :                   && !values_equal_for_ipcp_p (t, newval))
    5386      1227100 :               || (!first && !newval))
    5387              :             {
    5388              :               newval = NULL_TREE;
    5389              :               break;
    5390              :             }
    5391              :           else
    5392              :             newval = t;
    5393              :           first = false;
    5394              :         }
    5395              : 
    5396       521051 :       if (newval)
    5397       195354 :         known_csts[i] = newval;
    5398              :     }
    5399       167285 : }
    5400              : 
    5401              : /* Given a NODE and a subset of its CALLERS, try to populate plank slots in
    5402              :    KNOWN_CONTEXTS with polymorphic contexts that are also known for all of the
    5403              :    CALLERS.  */
    5404              : 
    5405              : static void
    5406       167285 : find_contexts_for_caller_subset (vec<ipa_polymorphic_call_context>
    5407              :                                  &known_contexts,
    5408              :                                  ipa_node_params *info,
    5409              :                                  const vec<cgraph_edge *> &callers)
    5410              : {
    5411       167285 :   int i, count = ipa_get_param_count (info);
    5412              : 
    5413       731136 :   for (i = 0; i < count; i++)
    5414              :     {
    5415       563864 :       if (!ipa_is_param_used (info, i))
    5416        30387 :         continue;
    5417              : 
    5418       535572 :       ipcp_lattice<ipa_polymorphic_call_context> *ctxlat
    5419       535572 :         = ipa_get_poly_ctx_lat (info, i);
    5420       535572 :       if (ctxlat->bottom)
    5421          978 :         continue;
    5422       534594 :       if (ctxlat->is_single_const ())
    5423              :         {
    5424         1117 :           if (!ctxlat->values->value.useless_p ())
    5425              :             {
    5426         1117 :               if (known_contexts.is_empty ())
    5427         1056 :                 known_contexts.safe_grow_cleared (count, true);
    5428         1117 :               known_contexts[i] = ctxlat->values->value;
    5429              :             }
    5430         1117 :           continue;
    5431              :         }
    5432              : 
    5433       533477 :       cgraph_edge *cs;
    5434       533477 :       ipa_polymorphic_call_context newval;
    5435       533477 :       bool first = true;
    5436       533477 :       int j;
    5437              : 
    5438       538660 :       FOR_EACH_VEC_ELT (callers, j, cs)
    5439              :         {
    5440       534916 :           ipa_edge_args *args = ipa_edge_args_sum->get (cs);
    5441       534916 :           if (!args
    5442      1069832 :               || i >= ipa_get_cs_argument_count (args))
    5443           13 :             return;
    5444       534903 :           ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
    5445       534903 :           ipa_polymorphic_call_context ctx;
    5446       534903 :           ctx = ipa_context_from_jfunc (ipa_node_params_sum->get (cs->caller),
    5447              :                                         cs, i, jfunc);
    5448       534903 :           if (first)
    5449              :             {
    5450       533464 :               newval = ctx;
    5451       533464 :               first = false;
    5452              :             }
    5453              :           else
    5454         1439 :             newval.meet_with (ctx);
    5455      1067115 :           if (newval.useless_p ())
    5456              :             break;
    5457              :         }
    5458              : 
    5459      1066928 :       if (!newval.useless_p ())
    5460              :         {
    5461         3744 :           if (known_contexts.is_empty ())
    5462         3521 :             known_contexts.safe_grow_cleared (count, true);
    5463         3744 :           known_contexts[i] = newval;
    5464              :         }
    5465              : 
    5466              :     }
    5467              : }
    5468              : 
    5469              : /* Push all aggregate values coming along edge CS for parameter number INDEX to
    5470              :    RES.  If INTERIM is non-NULL, it contains the current interim state of
    5471              :    collected aggregate values which can be used to compute values passed over
    5472              :    self-recursive edges.
    5473              : 
    5474              :    This basically one iteration of push_agg_values_from_edge over one
    5475              :    parameter, which allows for simpler early returns.  */
    5476              : 
    5477              : static void
    5478       619701 : push_agg_values_for_index_from_edge (struct cgraph_edge *cs, int index,
    5479              :                                      vec<ipa_argagg_value> *res,
    5480              :                                      const ipa_argagg_value_list *interim)
    5481              : {
    5482       619701 :   bool agg_values_from_caller = false;
    5483       619701 :   bool agg_jf_preserved = false;
    5484       619701 :   unsigned unit_delta = UINT_MAX;
    5485       619701 :   int src_idx = -1;
    5486       619701 :   ipa_jump_func *jfunc = ipa_get_ith_jump_func (ipa_edge_args_sum->get (cs),
    5487              :                                                 index);
    5488              : 
    5489       619701 :   if (jfunc->type == IPA_JF_PASS_THROUGH
    5490       619701 :       && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
    5491              :     {
    5492        57056 :       agg_values_from_caller = true;
    5493        57056 :       agg_jf_preserved = ipa_get_jf_pass_through_agg_preserved (jfunc);
    5494        57056 :       src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
    5495        57056 :       unit_delta = 0;
    5496              :     }
    5497       562645 :   else if (jfunc->type == IPA_JF_ANCESTOR
    5498       562645 :            && ipa_get_jf_ancestor_agg_preserved (jfunc))
    5499              :     {
    5500          407 :       agg_values_from_caller = true;
    5501          407 :       agg_jf_preserved = true;
    5502          407 :       src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
    5503          407 :       unit_delta = ipa_get_jf_ancestor_offset (jfunc) / BITS_PER_UNIT;
    5504              :     }
    5505              : 
    5506       619701 :   ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
    5507       619701 :   if (agg_values_from_caller)
    5508              :     {
    5509        57463 :       if (caller_info->ipcp_orig_node)
    5510              :         {
    5511        11048 :           struct cgraph_node *orig_node = caller_info->ipcp_orig_node;
    5512        11048 :           ipcp_transformation *ts
    5513        11048 :             = ipcp_get_transformation_summary (cs->caller);
    5514        11048 :           ipa_node_params *orig_info = ipa_node_params_sum->get (orig_node);
    5515        11048 :           ipcp_param_lattices *orig_plats
    5516        11048 :             = ipa_get_parm_lattices (orig_info, src_idx);
    5517        11048 :           if (ts
    5518        11048 :               && orig_plats->aggs
    5519         2983 :               && (agg_jf_preserved || !orig_plats->aggs_by_ref))
    5520              :             {
    5521         2508 :               ipa_argagg_value_list src (ts);
    5522         2508 :               src.push_adjusted_values (src_idx, index, unit_delta, res);
    5523         2508 :               return;
    5524              :             }
    5525              :         }
    5526              :       else
    5527              :         {
    5528        46415 :           ipcp_param_lattices *src_plats
    5529        46415 :             = ipa_get_parm_lattices (caller_info, src_idx);
    5530        46415 :           if (src_plats->aggs
    5531         2440 :               && !src_plats->aggs_bottom
    5532         2440 :               && (agg_jf_preserved || !src_plats->aggs_by_ref))
    5533              :             {
    5534         1450 :               if (interim && (self_recursive_pass_through_p (cs, jfunc, index)
    5535            7 :                               || self_recursive_ancestor_p (cs, jfunc, index)))
    5536              :                 {
    5537          637 :                   interim->push_adjusted_values (src_idx, index, unit_delta,
    5538              :                                                  res);
    5539          637 :                   return;
    5540              :                 }
    5541          813 :               if (!src_plats->aggs_contain_variable)
    5542              :                 {
    5543           80 :                   push_agg_values_from_plats (src_plats, index, unit_delta,
    5544              :                                               res);
    5545           80 :                   return;
    5546              :                 }
    5547              :             }
    5548              :         }
    5549              :     }
    5550              : 
    5551       616476 :   if (!jfunc->agg.items)
    5552              :     return;
    5553       219405 :   bool first = true;
    5554       219405 :   unsigned prev_unit_offset = 0;
    5555      1193737 :   for (const ipa_agg_jf_item &agg_jf : *jfunc->agg.items)
    5556              :     {
    5557       974332 :       tree value, srcvalue;
    5558              :       /* Besides simple pass-through aggregate jump function, arithmetic
    5559              :          aggregate jump function could also bring same aggregate value as
    5560              :          parameter passed-in for self-feeding recursive call.  For example,
    5561              : 
    5562              :          fn (int *i)
    5563              :          {
    5564              :            int j = *i & 1;
    5565              :            fn (&j);
    5566              :          }
    5567              : 
    5568              :          Given that *i is 0, recursive propagation via (*i & 1) also gets 0.  */
    5569       974332 :       if (interim
    5570       333298 :           && self_recursive_agg_pass_through_p (cs, &agg_jf, index, false)
    5571       974813 :           && (srcvalue = interim->get_value(index,
    5572          481 :                                             agg_jf.offset / BITS_PER_UNIT)))
    5573              :         {
    5574          950 :           value = ipa_get_jf_arith_result (agg_jf.value.pass_through.operation,
    5575              :                                            srcvalue,
    5576          475 :                                            agg_jf.value.pass_through.operand,
    5577          475 :                                            agg_jf.value.pass_through.op_type);
    5578          475 :           value = ipacp_value_safe_for_type (agg_jf.type, value);
    5579              :         }
    5580              :       else
    5581       973857 :         value = ipa_agg_value_from_jfunc (caller_info, cs->caller,
    5582              :                                           &agg_jf);
    5583       974332 :       if (value)
    5584              :         {
    5585       951665 :           struct ipa_argagg_value iav;
    5586       951665 :           iav.value = value;
    5587       951665 :           iav.unit_offset = agg_jf.offset / BITS_PER_UNIT;
    5588       951665 :           iav.index = index;
    5589       951665 :           iav.by_ref = jfunc->agg.by_ref;
    5590       951665 :           iav.killed = false;
    5591              : 
    5592       951665 :           gcc_assert (first
    5593              :                       || iav.unit_offset > prev_unit_offset);
    5594       951665 :           prev_unit_offset = iav.unit_offset;
    5595       951665 :           first = false;
    5596              : 
    5597       951665 :           res->safe_push (iav);
    5598              :         }
    5599              :     }
    5600              :   return;
    5601              : }
    5602              : 
    5603              : /* Push all aggregate values coming along edge CS to RES.  DEST_INFO is the
    5604              :    description of ultimate callee of CS or the one it was cloned from (the
    5605              :    summary where lattices are).  If INTERIM is non-NULL, it contains the
    5606              :    current interim state of collected aggregate values which can be used to
    5607              :    compute values passed over self-recursive edges (if OPTIMIZE_SELF_RECURSION
    5608              :    is true) and to skip values which clearly will not be part of intersection
    5609              :    with INTERIM.  */
    5610              : 
    5611              : static void
    5612       222417 : push_agg_values_from_edge (struct cgraph_edge *cs,
    5613              :                            ipa_node_params *dest_info,
    5614              :                            vec<ipa_argagg_value> *res,
    5615              :                            const ipa_argagg_value_list *interim,
    5616              :                            bool optimize_self_recursion)
    5617              : {
    5618       222417 :   ipa_edge_args *args = ipa_edge_args_sum->get (cs);
    5619       222417 :   if (!args)
    5620              :     return;
    5621              : 
    5622       444834 :   int count = MIN (ipa_get_param_count (dest_info),
    5623              :                    ipa_get_cs_argument_count (args));
    5624              : 
    5625       222417 :   unsigned interim_index = 0;
    5626       929287 :   for (int index = 0; index < count; index++)
    5627              :     {
    5628       706870 :       if (interim)
    5629              :         {
    5630       273099 :           while (interim_index < interim->m_elts.size ()
    5631       249390 :                  && interim->m_elts[interim_index].value
    5632       479716 :                  && interim->m_elts[interim_index].index < index)
    5633       130068 :             interim_index++;
    5634       197843 :           if (interim_index >= interim->m_elts.size ()
    5635       143031 :               || interim->m_elts[interim_index].index > index)
    5636        54812 :             continue;
    5637              :         }
    5638              : 
    5639       652058 :       ipcp_param_lattices *plats = ipa_get_parm_lattices (dest_info, index);
    5640       652058 :       if (!ipa_is_param_used (dest_info, index)
    5641       652058 :           || plats->aggs_bottom)
    5642        32357 :         continue;
    5643       619749 :       push_agg_values_for_index_from_edge (cs, index, res,
    5644              :                                            optimize_self_recursion ? interim
    5645              :                                            : NULL);
    5646              :     }
    5647              : }
    5648              : 
    5649              : 
    5650              : /* Look at edges in CALLERS and collect all known aggregate values that arrive
    5651              :    from all of them into INTERIM.  Return how many there are.  */
    5652              : 
    5653              : static unsigned int
    5654       167285 : find_aggregate_values_for_callers_subset_1 (vec<ipa_argagg_value> &interim,
    5655              :                                             struct cgraph_node *node,
    5656              :                                             const vec<cgraph_edge *> &callers)
    5657              : {
    5658       167285 :   ipa_node_params *dest_info = ipa_node_params_sum->get (node);
    5659       167285 :   if (dest_info->ipcp_orig_node)
    5660            0 :     dest_info = ipa_node_params_sum->get (dest_info->ipcp_orig_node);
    5661              : 
    5662              :   /* gather_edges_for_value puts a non-recursive call into the first element of
    5663              :      callers if it can.  */
    5664       167285 :   push_agg_values_from_edge (callers[0], dest_info, &interim, NULL, true);
    5665              : 
    5666       252292 :   unsigned valid_entries = interim.length ();
    5667       167285 :   if (!valid_entries)
    5668              :     return 0;
    5669              : 
    5670        83957 :   unsigned caller_count = callers.length();
    5671       137365 :   for (unsigned i = 1; i < caller_count; i++)
    5672              :     {
    5673        55087 :       auto_vec<ipa_argagg_value, 32> last;
    5674        55087 :       ipa_argagg_value_list avs (&interim);
    5675        55087 :       push_agg_values_from_edge (callers[i], dest_info, &last, &avs, true);
    5676              : 
    5677        55087 :       valid_entries = intersect_argaggs_with (interim, last);
    5678        55087 :       if (!valid_entries)
    5679         1679 :         return 0;
    5680        55087 :     }
    5681              : 
    5682              :   return valid_entries;
    5683              : }
    5684              : 
    5685              : /* Look at edges in CALLERS and collect all known aggregate values that arrive
    5686              :    from all of them and return them in a garbage-collected vector.  Return
    5687              :    nullptr if there are none.  */
    5688              : 
    5689              : static void
    5690       150288 : find_aggregate_values_for_callers_subset (vec<ipa_argagg_value> &res,
    5691              :                                           struct cgraph_node *node,
    5692              :                                           const vec<cgraph_edge *> &callers)
    5693              : {
    5694       150288 :   auto_vec<ipa_argagg_value, 32> interim;
    5695       150288 :   unsigned valid_entries
    5696       150288 :     = find_aggregate_values_for_callers_subset_1 (interim, node, callers);
    5697       150288 :   if (!valid_entries)
    5698              :     return;
    5699              : 
    5700       833876 :   for (const ipa_argagg_value &av : interim)
    5701       602600 :     if (av.value)
    5702       573019 :       res.safe_push(av);
    5703              :   return;
    5704       150288 : }
    5705              : 
    5706              : /* Look at edges in CALLERS and collect all known aggregate values that arrive
    5707              :    from all of them and return them in a garbage-collected vector.  Return
    5708              :    nullptr if there are none.  */
    5709              : 
    5710              : static struct vec<ipa_argagg_value, va_gc> *
    5711        16997 : find_aggregate_values_for_callers_subset_gc (struct cgraph_node *node,
    5712              :                                              const vec<cgraph_edge *> &callers)
    5713              : {
    5714        16997 :   auto_vec<ipa_argagg_value, 32> interim;
    5715        16997 :   unsigned valid_entries
    5716        16997 :     = find_aggregate_values_for_callers_subset_1 (interim, node, callers);
    5717        16997 :   if (!valid_entries)
    5718              :     return nullptr;
    5719              : 
    5720         5186 :   vec<ipa_argagg_value, va_gc> *res = NULL;
    5721         5186 :   vec_safe_reserve_exact (res, valid_entries);
    5722        36527 :   for (const ipa_argagg_value &av : interim)
    5723        20969 :     if (av.value)
    5724        19568 :       res->quick_push(av);
    5725         5186 :   gcc_checking_assert (res->length () == valid_entries);
    5726              :   return res;
    5727        16997 : }
    5728              : 
    5729              : /* Determine whether CS also brings all scalar values that the NODE is
    5730              :    specialized for.  */
    5731              : 
    5732              : static bool
    5733           77 : cgraph_edge_brings_all_scalars_for_node (struct cgraph_edge *cs,
    5734              :                                          struct cgraph_node *node)
    5735              : {
    5736           77 :   ipa_node_params *dest_info = ipa_node_params_sum->get (node);
    5737           77 :   int count = ipa_get_param_count (dest_info);
    5738           77 :   class ipa_node_params *caller_info;
    5739           77 :   class ipa_edge_args *args;
    5740           77 :   int i;
    5741              : 
    5742           77 :   caller_info = ipa_node_params_sum->get (cs->caller);
    5743           77 :   args = ipa_edge_args_sum->get (cs);
    5744          177 :   for (i = 0; i < count; i++)
    5745              :     {
    5746          122 :       struct ipa_jump_func *jump_func;
    5747          122 :       tree val, t;
    5748              : 
    5749          122 :       val = dest_info->known_csts[i];
    5750          122 :       if (!val)
    5751           72 :         continue;
    5752              : 
    5753          100 :       if (i >= ipa_get_cs_argument_count (args))
    5754              :         return false;
    5755           50 :       jump_func = ipa_get_ith_jump_func (args, i);
    5756           50 :       t = ipa_value_from_jfunc (caller_info, jump_func,
    5757              :                                 ipa_get_type (dest_info, i));
    5758           50 :       if (!t || !values_equal_for_ipcp_p (val, t))
    5759           22 :         return false;
    5760              :     }
    5761              :   return true;
    5762              : }
    5763              : 
    5764              : /* Determine whether CS also brings all aggregate values that NODE is
    5765              :    specialized for.  */
    5766              : 
    5767              : static bool
    5768           55 : cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge *cs,
    5769              :                                           struct cgraph_node *node)
    5770              : {
    5771           55 :   ipcp_transformation *ts = ipcp_get_transformation_summary (node);
    5772           55 :   if (!ts || vec_safe_is_empty (ts->m_agg_values))
    5773              :     return true;
    5774              : 
    5775           45 :   const ipa_argagg_value_list existing (ts->m_agg_values);
    5776           45 :   auto_vec<ipa_argagg_value, 32> edge_values;
    5777           45 :   ipa_node_params *dest_info = ipa_node_params_sum->get (node);
    5778           45 :   gcc_checking_assert (dest_info->ipcp_orig_node);
    5779           45 :   dest_info = ipa_node_params_sum->get (dest_info->ipcp_orig_node);
    5780           45 :   push_agg_values_from_edge (cs, dest_info, &edge_values, &existing, false);
    5781           45 :   const ipa_argagg_value_list avl (&edge_values);
    5782           45 :   return avl.superset_of_p (existing);
    5783           45 : }
    5784              : 
    5785              : /* Given an original NODE and a VAL for which we have already created a
    5786              :    specialized clone, look whether there are incoming edges that still lead
    5787              :    into the old node but now also bring the requested value and also conform to
    5788              :    all other criteria such that they can be redirected the special node.
    5789              :    This function can therefore redirect the final edge in a SCC.  */
    5790              : 
    5791              : template <typename valtype>
    5792              : static void
    5793         8758 : perhaps_add_new_callers (cgraph_node *node, ipcp_value<valtype> *val)
    5794              : {
    5795              :   ipcp_value_source<valtype> *src;
    5796         8758 :   profile_count redirected_sum = profile_count::zero ();
    5797              : 
    5798       122814 :   for (src = val->sources; src; src = src->next)
    5799              :     {
    5800       114056 :       struct cgraph_edge *cs = src->cs;
    5801       353964 :       while (cs)
    5802              :         {
    5803       239908 :           if (cgraph_edge_brings_value_p (cs, src, node, val)
    5804           77 :               && cgraph_edge_brings_all_scalars_for_node (cs, val->spec_node)
    5805       239963 :               && cgraph_edge_brings_all_agg_vals_for_node (cs, val->spec_node))
    5806              :             {
    5807           38 :               if (dump_file)
    5808            3 :                 fprintf (dump_file, " - adding an extra caller %s of %s\n",
    5809            3 :                          cs->caller->dump_name (),
    5810            3 :                          val->spec_node->dump_name ());
    5811              : 
    5812           38 :               cs->redirect_callee_duplicating_thunks (val->spec_node);
    5813           38 :               val->spec_node->expand_all_artificial_thunks ();
    5814           38 :               if (cs->count.ipa ().initialized_p ())
    5815            0 :                 redirected_sum = redirected_sum + cs->count.ipa ();
    5816              :             }
    5817       239908 :           cs = get_next_cgraph_edge_clone (cs);
    5818              :         }
    5819              :     }
    5820              : 
    5821         8758 :   if (redirected_sum.nonzero_p ())
    5822            0 :     update_specialized_profile (val->spec_node, node, redirected_sum);
    5823         8758 : }
    5824              : 
    5825              : /* Return true if KNOWN_CONTEXTS contain at least one useful context.  */
    5826              : 
    5827              : static bool
    5828         4319 : known_contexts_useful_p (vec<ipa_polymorphic_call_context> known_contexts)
    5829              : {
    5830         4319 :   ipa_polymorphic_call_context *ctx;
    5831         4319 :   int i;
    5832              : 
    5833         4319 :   FOR_EACH_VEC_ELT (known_contexts, i, ctx)
    5834          100 :     if (!ctx->useless_p ())
    5835              :       return true;
    5836              :   return false;
    5837              : }
    5838              : 
    5839              : /* Return a copy of KNOWN_CSTS if it is not empty, otherwise return vNULL.  */
    5840              : 
    5841              : static vec<ipa_polymorphic_call_context>
    5842         4319 : copy_useful_known_contexts (const vec<ipa_polymorphic_call_context> &known_contexts)
    5843              : {
    5844         4319 :   if (known_contexts_useful_p (known_contexts))
    5845          100 :     return known_contexts.copy ();
    5846              :   else
    5847         4219 :     return vNULL;
    5848              : }
    5849              : 
    5850              : /* Return true if the VALUE is represented in KNOWN_CSTS at INDEX if OFFSET is
    5851              :    minus one or in AGGVALS for INDEX and OFFSET otherwise.  */
    5852              : 
    5853              : DEBUG_FUNCTION bool
    5854         4268 : ipcp_val_replacement_ok_p (vec<tree> &known_csts,
    5855              :                           vec<ipa_polymorphic_call_context> &,
    5856              :                           vec<ipa_argagg_value, va_gc> *aggvals,
    5857              :                           int index, HOST_WIDE_INT offset, tree value)
    5858              : {
    5859         4268 :   tree v;
    5860         4268 :   if (offset == -1)
    5861         3128 :     v = known_csts[index];
    5862              :   else
    5863              :     {
    5864         1140 :       const ipa_argagg_value_list avl (aggvals);
    5865         1140 :       v = avl.get_value (index, offset / BITS_PER_UNIT);
    5866              :     }
    5867              : 
    5868         4268 :   return v && values_equal_for_ipcp_p (v, value);
    5869              : }
    5870              : 
    5871              : /* Dump to F all the values in AVALS for which we are re-evaluating the effects
    5872              :    on the function represented b INFO.  */
    5873              : 
    5874              : DEBUG_FUNCTION void
    5875           68 : dump_reestimation_message (FILE *f, ipa_node_params *info,
    5876              :                            const ipa_auto_call_arg_values &avals)
    5877              : {
    5878           68 :   fprintf (f, "     Re-estimating effects with\n"
    5879              :            "       Scalar constants:");
    5880           68 :   int param_count = ipa_get_param_count (info);
    5881          168 :   for (int i = 0; i < param_count; i++)
    5882          100 :     if (avals.m_known_vals[i])
    5883              :       {
    5884           44 :         fprintf (f, " %i:", i);
    5885           44 :         print_ipcp_constant_value (f, avals.m_known_vals[i]);
    5886              :       }
    5887           68 :   fprintf (f, "\n");
    5888           68 :   if (!avals.m_known_contexts.is_empty ())
    5889              :     {
    5890            0 :       fprintf (f, "       Pol. contexts:");
    5891            0 :       for (int i = 0; i < param_count; i++)
    5892            0 :         if (!avals.m_known_contexts[i].useless_p ())
    5893              :           {
    5894            0 :             fprintf (f, " %i:", i);
    5895            0 :             avals.m_known_contexts[i].dump (f);
    5896              :           }
    5897            0 :       fprintf (f, "\n");
    5898              :     }
    5899           68 :   if (!avals.m_known_aggs.is_empty ())
    5900              :     {
    5901           24 :       fprintf (f, "       Aggregate replacements:");
    5902           24 :       ipa_argagg_value_list avs (&avals);
    5903           24 :       avs.dump (f);
    5904              :     }
    5905           68 : }
    5906              : 
    5907              : /* Return true if the VALUE is represented in KNOWN_CONTEXTS at INDEX and that
    5908              :    if OFFSET is is equal to minus one (because source of a polymorphic context
    5909              :    cannot be an aggregate value).  */
    5910              : 
    5911              : DEBUG_FUNCTION bool
    5912           51 : ipcp_val_replacement_ok_p (vec<tree> &,
    5913              :                            vec<ipa_polymorphic_call_context> &known_contexts,
    5914              :                            vec<ipa_argagg_value, va_gc> *,
    5915              :                            int index, HOST_WIDE_INT offset,
    5916              :                            ipa_polymorphic_call_context value)
    5917              : {
    5918           51 :   if (offset != -1
    5919           51 :       || known_contexts.length () <= (unsigned) index
    5920          102 :       || known_contexts[index].useless_p ())
    5921              :     return false;
    5922              : 
    5923           51 :   if (known_contexts[index].equal_to (value))
    5924              :     return true;
    5925              : 
    5926              :   /* In some corner cases, the final gathering of contexts can figure out that
    5927              :      the available context is actually more precise than what we wanted to
    5928              :      clone for.  Allow it.  */
    5929            0 :   value.combine_with (known_contexts[index]);
    5930            0 :   return known_contexts[index].equal_to (value);
    5931              : }
    5932              : 
    5933              : /* Decide whether to create a special version of NODE for value VAL of
    5934              :    parameter at the given INDEX.  If OFFSET is -1, the value is for the
    5935              :    parameter itself, otherwise it is stored at the given OFFSET of the
    5936              :    parameter.  AVALS describes the other already known values.  SELF_GEN_CLONES
    5937              :    is a vector which contains clones created for self-recursive calls with an
    5938              :    arithmetic pass-through jump function.  CUR_SWEEP is the number of the
    5939              :    current sweep of the call-graph during the decision stage.  */
    5940              : 
    5941              : template <typename valtype>
    5942              : static bool
    5943       220071 : decide_about_value (struct cgraph_node *node, int index, HOST_WIDE_INT offset,
    5944              :                     ipcp_value<valtype> *val,
    5945              :                     vec<cgraph_node *> *self_gen_clones, int cur_sweep)
    5946              : {
    5947              :   int caller_count;
    5948       220071 :   sreal freq_sum;
    5949              :   profile_count count_sum, rec_count_sum;
    5950              :   bool called_without_ipa_profile;
    5951              : 
    5952       220071 :   if (val->spec_node)
    5953              :     {
    5954         8758 :       perhaps_add_new_callers (node, val);
    5955         8758 :       return false;
    5956              :     }
    5957       211313 :   else if (val->local_size_cost + overall_size > get_max_overall_size (node))
    5958              :     {
    5959          450 :       if (dump_file && (dump_flags & TDF_DETAILS))
    5960            0 :         fprintf (dump_file, "   Ignoring candidate value because "
    5961              :                  "maximum unit size would be reached with %li.\n",
    5962              :                  val->local_size_cost + overall_size);
    5963          450 :       return false;
    5964              :     }
    5965       210863 :   else if (!get_info_about_necessary_edges (val, node, &freq_sum, &caller_count,
    5966              :                                             &rec_count_sum, &count_sum,
    5967              :                                             &called_without_ipa_profile))
    5968              :     return false;
    5969              : 
    5970       150288 :   if (!dbg_cnt (ipa_cp_values))
    5971              :     return false;
    5972              : 
    5973       150288 :   if (val->self_recursion_generated_p ())
    5974              :     {
    5975              :       /* The edge counts in this case might not have been adjusted yet.
    5976              :          Nevertleless, even if they were it would be only a guesswork which we
    5977              :          can do now.  The recursive part of the counts can be derived from the
    5978              :          count of the original node anyway.  */
    5979          293 :       if (node->count.ipa ().nonzero_p ())
    5980              :         {
    5981           14 :           unsigned dem = self_gen_clones->length () + 1;
    5982           14 :           rec_count_sum = node->count.ipa () / dem;
    5983              :         }
    5984              :       else
    5985          265 :         rec_count_sum = profile_count::zero ();
    5986              :     }
    5987              : 
    5988              :   /* get_info_about_necessary_edges only sums up ipa counts.  */
    5989       150288 :   count_sum += rec_count_sum;
    5990              : 
    5991       150288 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5992              :     {
    5993          133 :       fprintf (dump_file, " - considering value ");
    5994          133 :       print_ipcp_constant_value (dump_file, val->value);
    5995          133 :       fprintf (dump_file, " for ");
    5996          133 :       ipa_dump_param (dump_file, ipa_node_params_sum->get (node), index);
    5997          133 :       if (offset != -1)
    5998           61 :         fprintf (dump_file, ", offset: " HOST_WIDE_INT_PRINT_DEC, offset);
    5999          133 :       fprintf (dump_file, " (caller_count: %i)\n", caller_count);
    6000              :     }
    6001              : 
    6002       150288 :   auto_vec<cgraph_edge *> callers
    6003              :     = gather_edges_for_value (val, node, caller_count);
    6004       150288 :   ipa_node_params *info = ipa_node_params_sum->get (node);
    6005       150288 :   ipa_auto_call_arg_values avals;
    6006       150288 :   avals.m_known_vals.safe_grow_cleared (ipa_get_param_count (info), true);
    6007       150288 :   find_scalar_values_for_callers_subset (avals.m_known_vals, info, callers);
    6008       150288 :   find_contexts_for_caller_subset (avals.m_known_contexts, info, callers);
    6009       150288 :   find_aggregate_values_for_callers_subset (avals.m_known_aggs, node, callers);
    6010              : 
    6011              : 
    6012       150288 :   if (good_cloning_opportunity_p (node, val->prop_time_benefit,
    6013              :                                   freq_sum, count_sum, val->prop_size_cost,
    6014              :                                   called_without_ipa_profile, cur_sweep))
    6015              :     ;
    6016              :   else
    6017              :     {
    6018              :       /* Extern inline functions are only meaningful to clione to propagate
    6019              :          values to their callees.  */
    6020       148417 :       if (DECL_EXTERNAL (node->decl) && DECL_DECLARED_INLINE_P (node->decl))
    6021              :         {
    6022          345 :           if (dump_file && (dump_flags & TDF_DETAILS))
    6023            0 :             fprintf (dump_file, "   Skipping extern inline.\n");
    6024       145969 :           return false;
    6025              :         }
    6026       148072 :       if (dump_file && (dump_flags & TDF_DETAILS))
    6027           68 :         dump_reestimation_message (dump_file, info, avals);
    6028              : 
    6029       148072 :       ipa_call_estimates estimates;
    6030       148072 :       estimate_ipcp_clone_size_and_time (node, &avals, &estimates);
    6031       148072 :       int removable_params_cost = 0;
    6032       955846 :       for (tree t : avals.m_known_vals)
    6033       511630 :         if (t)
    6034       205371 :           removable_params_cost += estimate_move_cost (TREE_TYPE (t), true);
    6035              : 
    6036       148072 :       int size = estimates.size - caller_count * removable_params_cost;
    6037              : 
    6038       148072 :       if (size <= 0)
    6039              :         {
    6040         1799 :           if (dump_file)
    6041            0 :             fprintf (dump_file, "   Code not going to grow.\n");
    6042              :         }
    6043              :       else
    6044              :         {
    6045              :           sreal time_benefit
    6046       146273 :             = ((estimates.nonspecialized_time - estimates.time)
    6047       292546 :                + hint_time_bonus (node, estimates)
    6048       146273 :                + (devirtualization_time_bonus (node, &avals)
    6049       146273 :                   + removable_params_cost));
    6050              : 
    6051       146273 :           if (!good_cloning_opportunity_p (node, time_benefit, freq_sum,
    6052              :                                            count_sum, size,
    6053              :                                            called_without_ipa_profile,
    6054              :                                            cur_sweep))
    6055       145624 :               return false;
    6056              :         }
    6057              :     }
    6058              : 
    6059         4319 :   if (dump_file)
    6060          137 :     fprintf (dump_file, "   Creating a specialized node of %s.\n",
    6061              :              node->dump_name ());
    6062              : 
    6063         4319 :   vec<tree> known_csts = avals.m_known_vals.copy ();
    6064              :   vec<ipa_polymorphic_call_context> known_contexts
    6065         4319 :     = copy_useful_known_contexts (avals.m_known_contexts);
    6066              : 
    6067         4319 :   vec<ipa_argagg_value, va_gc> *aggvals = NULL;
    6068         4319 :   vec_safe_reserve_exact (aggvals, avals.m_known_aggs.length ());
    6069        22972 :   for (const ipa_argagg_value &av : avals.m_known_aggs)
    6070        10015 :     aggvals->quick_push (av);
    6071         4319 :   gcc_checking_assert (ipcp_val_replacement_ok_p (known_csts, known_contexts,
    6072              :                                                   aggvals, index,
    6073              :                                                   offset, val->value));
    6074         4319 :   val->spec_node = create_specialized_node (node, known_csts, known_contexts,
    6075              :                                             aggvals, callers);
    6076              : 
    6077         4319 :   if (val->self_recursion_generated_p ())
    6078          142 :     self_gen_clones->safe_push (val->spec_node);
    6079              :   else
    6080         4177 :     update_profiling_info (node, val->spec_node);
    6081              : 
    6082         4319 :   overall_size += val->local_size_cost;
    6083         4319 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6084           66 :     fprintf (dump_file, "     overall size reached %li\n",
    6085              :              overall_size);
    6086              : 
    6087              :   /* TODO: If for some lattice there is only one other known value
    6088              :      left, make a special node for it too. */
    6089              : 
    6090              :   return true;
    6091       150288 : }
    6092              : 
    6093              : /* Like irange::contains_p(), but convert VAL to the range of R if
    6094              :    necessary.  */
    6095              : 
    6096              : static inline bool
    6097        47690 : ipa_range_contains_p (const vrange &r, tree val)
    6098              : {
    6099        47690 :   if (r.undefined_p ())
    6100              :     return false;
    6101              : 
    6102        47690 :   tree type = r.type ();
    6103        47690 :   if (!wi::fits_to_tree_p (wi::to_wide (val), type))
    6104              :     return false;
    6105              : 
    6106        47690 :   val = fold_convert (type, val);
    6107        47690 :   return r.contains_p (val);
    6108              : }
    6109              : 
    6110              : /* Structure holding opportunitties so that they can be pre-sorted.  */
    6111              : 
    6112       220071 : struct cloning_opportunity_ranking
    6113              : {
    6114              :   /* A very rough evaluation of likely benefit.  */
    6115              :   sreal eval;
    6116              :   /* In the case of aggregate constants, a non-negative offset within their
    6117              :      aggregates. -1 for scalar constants, -2 for polymorphic contexts.  */
    6118              :   HOST_WIDE_INT offset;
    6119              :   /* The value being considered for evaluation for cloning.  */
    6120              :   ipcp_value_base *val;
    6121              :   /* Index of the formal parameter the value is coming in. */
    6122              :   int index;
    6123              : };
    6124              : 
    6125              : /* Helper function to qsort a vecotr of cloning opportunities.  */
    6126              : 
    6127              : static int
    6128      2072060 : compare_cloning_opportunities (const void *a, const void *b)
    6129              : {
    6130      2072060 :   const cloning_opportunity_ranking *o1
    6131              :     = (const cloning_opportunity_ranking *) a;
    6132      2072060 :   const cloning_opportunity_ranking *o2
    6133              :     = (const cloning_opportunity_ranking *) b;
    6134      2072060 :   if (o1->eval < o2->eval)
    6135              :     return 1;
    6136      1625885 :   if (o1->eval > o2->eval)
    6137       522225 :     return -1;
    6138              :   return 0;
    6139              : }
    6140              : 
    6141              : /* Use the estimations in VAL to determine how good a candidate it represents
    6142              :    for the purposes of ordering real evaluation of opportunities (which
    6143              :    includes information about incoming edges, among other things).  */
    6144              : 
    6145              : static sreal
    6146       220071 : cloning_opportunity_ranking_evaluation (const ipcp_value_base *val)
    6147              : {
    6148       220071 :   sreal e1 = (val->local_time_benefit * 1000) / MAX (val->local_size_cost, 1);
    6149       220071 :   sreal e2 = (val->prop_time_benefit * 1000) / MAX (val->prop_size_cost, 1);
    6150       220071 :   if (e2 > e1)
    6151        15323 :     return e2;
    6152              :   else
    6153       204748 :     return e1;
    6154              : }
    6155              : 
    6156              : /* Decide whether and what specialized clones of NODE should be created.
    6157              :    CUR_SWEEP is the number of the current sweep of the call-graph during the
    6158              :    decision stage.  */
    6159              : 
    6160              : static bool
    6161      3227265 : decide_whether_version_node (struct cgraph_node *node, int cur_sweep)
    6162              : {
    6163      3227265 :   ipa_node_params *info = ipa_node_params_sum->get (node);
    6164      3227265 :   int count = ipa_get_param_count (info);
    6165      3227265 :   bool ret = false;
    6166              : 
    6167      3227265 :   if (info->node_dead || count == 0)
    6168              :     return false;
    6169              : 
    6170      2609761 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6171          344 :     fprintf (dump_file, "\nEvaluating opportunities for %s.\n",
    6172              :              node->dump_name ());
    6173              : 
    6174      2609761 :   auto_vec <cloning_opportunity_ranking, 32> opp_ranking;
    6175      8704742 :   for (int i = 0; i < count;i++)
    6176              :     {
    6177      6094981 :       if (!ipa_is_param_used (info, i))
    6178       687092 :         continue;
    6179              : 
    6180      5407889 :       class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
    6181      5407889 :       ipcp_lattice<tree> *lat = &plats->itself;
    6182      5407889 :       ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
    6183              : 
    6184      5407889 :       if (!lat->bottom
    6185      5407889 :           && !lat->is_single_const ())
    6186              :         {
    6187       534134 :           ipcp_value<tree> *val;
    6188       649823 :           for (val = lat->values; val; val = val->next)
    6189              :             {
    6190              :               /* If some values generated for self-recursive calls with
    6191              :                  arithmetic jump functions fall outside of the known
    6192              :                  range for the parameter, we can skip them.  */
    6193       115751 :               if (TREE_CODE (val->value) == INTEGER_CST
    6194        70113 :                   && !plats->m_value_range.bottom_p ()
    6195       163379 :                   && !ipa_range_contains_p (plats->m_value_range.m_vr,
    6196              :                                             val->value))
    6197              :                 {
    6198              :                   /* This can happen also if a constant present in the source
    6199              :                      code falls outside of the range of parameter's type, so we
    6200              :                      cannot assert.  */
    6201           62 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    6202              :                     {
    6203            0 :                       fprintf (dump_file, " - skipping%s value ",
    6204            0 :                                val->self_recursion_generated_p ()
    6205              :                                ? " self_recursion_generated" : "");
    6206            0 :                       print_ipcp_constant_value (dump_file, val->value);
    6207            0 :                       fprintf (dump_file, " because it is outside known "
    6208              :                                "value range.\n");
    6209              :                     }
    6210           62 :                   continue;
    6211              :                 }
    6212       115627 :               cloning_opportunity_ranking opp;
    6213       115627 :               opp.eval = cloning_opportunity_ranking_evaluation (val);
    6214       115627 :               opp.offset = -1;
    6215       115627 :               opp.val = val;
    6216       115627 :               opp.index = i;
    6217       115627 :               opp_ranking.safe_push (opp);
    6218              :             }
    6219              :         }
    6220              : 
    6221      5407889 :       if (!plats->aggs_bottom)
    6222              :         {
    6223       563123 :           struct ipcp_agg_lattice *aglat;
    6224       563123 :           ipcp_value<tree> *val;
    6225       703480 :           for (aglat = plats->aggs; aglat; aglat = aglat->next)
    6226       139271 :             if (!aglat->bottom && aglat->values
    6227              :                 /* If the following is false, the one value has been considered
    6228              :                    for cloning for all contexts.  */
    6229       258536 :                 && (plats->aggs_contain_variable
    6230       192483 :                     || !aglat->is_single_const ()))
    6231       177541 :               for (val = aglat->values; val; val = val->next)
    6232              :                 {
    6233       100648 :                   cloning_opportunity_ranking opp;
    6234       100648 :                   opp.eval = cloning_opportunity_ranking_evaluation (val);
    6235       100648 :                   opp.offset = aglat->offset;
    6236       100648 :                   opp.val = val;
    6237       100648 :                   opp.index = i;
    6238       100648 :                   opp_ranking.safe_push (opp);
    6239              :                 }
    6240              :         }
    6241              : 
    6242      5407889 :       if (!ctxlat->bottom
    6243      6660983 :           && !ctxlat->is_single_const ())
    6244              :         {
    6245       548211 :           ipcp_value<ipa_polymorphic_call_context> *val;
    6246       552007 :           for (val = ctxlat->values; val; val = val->next)
    6247         7592 :             if (!val->value.useless_p ())
    6248              :               {
    6249         3796 :                 cloning_opportunity_ranking opp;
    6250         3796 :                 opp.eval = cloning_opportunity_ranking_evaluation (val);
    6251         3796 :                 opp.offset = -2;
    6252         3796 :                 opp.val = val;
    6253         3796 :                 opp.index = i;
    6254         3796 :                 opp_ranking.safe_push (opp);
    6255              :               }
    6256              :         }
    6257              :     }
    6258              : 
    6259      2609761 :   if (!opp_ranking.is_empty ())
    6260              :     {
    6261        51396 :       opp_ranking.qsort (compare_cloning_opportunities);
    6262        51396 :       auto_vec <cgraph_node *, 9> self_gen_clones;
    6263       374259 :       for (const cloning_opportunity_ranking &opp : opp_ranking)
    6264       220071 :         if (opp.offset == -2)
    6265              :           {
    6266         3796 :             ipcp_value<ipa_polymorphic_call_context> *val
    6267              :               = static_cast <ipcp_value<ipa_polymorphic_call_context> *>
    6268              :               (opp.val);
    6269         3796 :             ret |= decide_about_value (node, opp.index, -1, val,
    6270              :                                        &self_gen_clones, cur_sweep);
    6271              :           }
    6272              :         else
    6273              :           {
    6274       216275 :             ipcp_value<tree> *val = static_cast<ipcp_value<tree> *> (opp.val);
    6275       216275 :             ret |= decide_about_value (node, opp.index, opp.offset, val,
    6276              :                                        &self_gen_clones, cur_sweep);
    6277              :           }
    6278              : 
    6279       102792 :       if (!self_gen_clones.is_empty ())
    6280              :         {
    6281           33 :           self_gen_clones.safe_push (node);
    6282           33 :           update_counts_for_self_gen_clones (node, self_gen_clones);
    6283              :         }
    6284        51396 :     }
    6285              : 
    6286      2609761 :   struct caller_statistics stats;
    6287      2609761 :   init_caller_stats (&stats);
    6288      2609761 :   node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
    6289              :                                                 false);
    6290      2609761 :   if (!stats.n_calls)
    6291              :     {
    6292      1303067 :       if (dump_file)
    6293          942 :         fprintf (dump_file, "   Not cloning for all contexts because "
    6294              :                  "there are no callers of the original node (any more).\n");
    6295      1303067 :       return ret;
    6296              :     }
    6297              : 
    6298      1306694 :   bool do_clone_for_all_contexts = false;
    6299      1306694 :   ipa_auto_call_arg_values avals;
    6300      1306694 :   int removable_params_cost;
    6301      1306694 :   bool ctx_independent_const
    6302      1306694 :     = gather_context_independent_values (info, &avals, &removable_params_cost);
    6303      1306694 :   sreal devirt_bonus = devirtualization_time_bonus (node, &avals);
    6304      1292149 :   if (ctx_independent_const || devirt_bonus > 0
    6305      2598837 :       || (removable_params_cost && clone_for_param_removal_p (node)))
    6306              :     {
    6307        63642 :        ipa_call_estimates estimates;
    6308              : 
    6309        63642 :       estimate_ipcp_clone_size_and_time (node, &avals, &estimates);
    6310        63642 :       sreal time = estimates.nonspecialized_time - estimates.time;
    6311        63642 :       time += devirt_bonus;
    6312        63642 :       time += hint_time_bonus (node, estimates);
    6313        63642 :       time += removable_params_cost;
    6314        63642 :       int size = estimates.size - stats.n_calls * removable_params_cost;
    6315              : 
    6316        63642 :       if (dump_file && (dump_flags & TDF_DETAILS))
    6317           24 :         fprintf (dump_file, " - context independent values, size: %i, "
    6318              :                  "time_benefit: %f\n", size, (time).to_double ());
    6319              : 
    6320        63642 :       if (size <= 0 || node->local)
    6321              :         {
    6322        16900 :           if (!dbg_cnt (ipa_cp_values))
    6323            0 :             return ret;
    6324              : 
    6325        16900 :           do_clone_for_all_contexts = true;
    6326        16900 :           if (dump_file)
    6327          106 :             fprintf (dump_file, "   Decided to specialize for all "
    6328              :                      "known contexts, code not going to grow.\n");
    6329              :         }
    6330        46742 :       else if (good_cloning_opportunity_p (node, time, stats.freq_sum,
    6331              :                                            stats.count_sum, size,
    6332        46742 :                                            stats.called_without_ipa_profile,
    6333              :                                            cur_sweep))
    6334              :         {
    6335          193 :           if (size + overall_size <= get_max_overall_size (node))
    6336              :             {
    6337          193 :               if (!dbg_cnt (ipa_cp_values))
    6338              :                 return ret;
    6339              : 
    6340          193 :               do_clone_for_all_contexts = true;
    6341          193 :               overall_size += size;
    6342          193 :               if (dump_file)
    6343           11 :                 fprintf (dump_file, "   Decided to specialize for all "
    6344              :                          "known contexts, growth (to %li) deemed "
    6345              :                          "beneficial.\n", overall_size);
    6346              :             }
    6347            0 :           else if (dump_file && (dump_flags & TDF_DETAILS))
    6348            0 :             fprintf (dump_file, "   Not cloning for all contexts because "
    6349              :                      "maximum unit size would be reached with %li.\n",
    6350              :                      size + overall_size);
    6351              :         }
    6352        46549 :       else if (dump_file && (dump_flags & TDF_DETAILS))
    6353            0 :         fprintf (dump_file, "   Not cloning for all contexts because "
    6354              :                  "!good_cloning_opportunity_p.\n");
    6355              :     }
    6356              : 
    6357      1306694 :   if (do_clone_for_all_contexts)
    6358              :     {
    6359        17093 :       auto_vec<cgraph_edge *> callers = node->collect_callers ();
    6360              : 
    6361        87453 :       for (int i = callers.length () - 1; i >= 0; i--)
    6362              :         {
    6363        53267 :           cgraph_edge *cs = callers[i];
    6364        53267 :           ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
    6365              : 
    6366        53267 :           if (caller_info && caller_info->node_dead)
    6367         2703 :             callers.unordered_remove (i);
    6368              :         }
    6369              : 
    6370        17093 :       if (!adjust_callers_for_value_intersection (callers, node))
    6371              :         /* If node is not called by anyone, or all its caller edges are
    6372              :            self-recursive, the node is not really in use, no need to do
    6373              :            cloning.  */
    6374           96 :         return ret;
    6375              : 
    6376        16997 :       if (dump_file)
    6377          115 :         fprintf (dump_file, "   Creating a specialized node of %s "
    6378              :                  "for all known contexts.\n", node->dump_name ());
    6379              : 
    6380        16997 :       vec<tree> known_csts = vNULL;
    6381        16997 :       known_csts.safe_grow_cleared (count, true);
    6382        16997 :       find_scalar_values_for_callers_subset (known_csts, info, callers);
    6383        16997 :       vec<ipa_polymorphic_call_context> known_contexts = vNULL;
    6384        16997 :       find_contexts_for_caller_subset (known_contexts, info, callers);
    6385        16997 :       vec<ipa_argagg_value, va_gc> *aggvals
    6386        16997 :         = find_aggregate_values_for_callers_subset_gc (node, callers);
    6387              : 
    6388        16997 :       struct cgraph_node *clone = create_specialized_node (node, known_csts,
    6389              :                                                            known_contexts,
    6390              :                                                            aggvals, callers);
    6391        16997 :       ipa_node_params_sum->get (clone)->is_all_contexts_clone = true;
    6392        16997 :       ret = true;
    6393        17093 :     }
    6394              : 
    6395              :   return ret;
    6396      3916455 : }
    6397              : 
    6398              : /* Transitively mark all callees of NODE within the same SCC as not dead.  */
    6399              : 
    6400              : static void
    6401         4627 : spread_undeadness (struct cgraph_node *node)
    6402              : {
    6403         4627 :   struct cgraph_edge *cs;
    6404              : 
    6405        15622 :   for (cs = node->callees; cs; cs = cs->next_callee)
    6406        10995 :     if (ipa_edge_within_scc (cs))
    6407              :       {
    6408          832 :         struct cgraph_node *callee;
    6409          832 :         class ipa_node_params *info;
    6410              : 
    6411          832 :         callee = cs->callee->function_symbol (NULL);
    6412          832 :         info = ipa_node_params_sum->get (callee);
    6413              : 
    6414          832 :         if (info && info->node_dead)
    6415              :           {
    6416           68 :             info->node_dead = 0;
    6417           68 :             spread_undeadness (callee);
    6418              :           }
    6419              :       }
    6420         4627 : }
    6421              : 
    6422              : /* Return true if NODE has a caller from outside of its SCC that is not
    6423              :    dead.  Worker callback for cgraph_for_node_and_aliases.  */
    6424              : 
    6425              : static bool
    6426        15835 : has_undead_caller_from_outside_scc_p (struct cgraph_node *node,
    6427              :                                       void *data ATTRIBUTE_UNUSED)
    6428              : {
    6429        15835 :   struct cgraph_edge *cs;
    6430              : 
    6431        81123 :   for (cs = node->callers; cs; cs = cs->next_caller)
    6432        65741 :     if (cs->caller->thunk
    6433        65741 :         && cs->caller->call_for_symbol_thunks_and_aliases
    6434            0 :           (has_undead_caller_from_outside_scc_p, NULL, true))
    6435              :       return true;
    6436        65741 :     else if (!ipa_edge_within_scc (cs))
    6437              :       {
    6438        65489 :         ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
    6439        65489 :         if (!caller_info /* Unoptimized caller are like dead ones.  */
    6440        65487 :             || !caller_info->node_dead)
    6441              :           return true;
    6442              :       }
    6443              :   return false;
    6444              : }
    6445              : 
    6446              : 
    6447              : /* Identify nodes within the same SCC as NODE which are no longer needed
    6448              :    because of new clones and will be removed as unreachable.  */
    6449              : 
    6450              : static void
    6451        19363 : identify_dead_nodes (struct cgraph_node *node)
    6452              : {
    6453        19363 :   struct cgraph_node *v;
    6454        39006 :   for (v = node; v; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
    6455        19643 :     if (v->local)
    6456              :       {
    6457        15596 :         ipa_node_params *info = ipa_node_params_sum->get (v);
    6458        15596 :         if (info
    6459        31192 :             && !v->call_for_symbol_thunks_and_aliases
    6460        15596 :               (has_undead_caller_from_outside_scc_p, NULL, true))
    6461        15143 :           info->node_dead = 1;
    6462              :       }
    6463              : 
    6464        39006 :   for (v = node; v; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
    6465              :     {
    6466        19643 :       ipa_node_params *info = ipa_node_params_sum->get (v);
    6467        19643 :       if (info && !info->node_dead)
    6468         4559 :         spread_undeadness (v);
    6469              :     }
    6470              : 
    6471        19363 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6472              :     {
    6473          105 :       for (v = node; v; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
    6474           54 :         if (ipa_node_params_sum->get (v)
    6475           54 :             && ipa_node_params_sum->get (v)->node_dead)
    6476           32 :           fprintf (dump_file, "  Marking node as dead: %s.\n",
    6477              :                    v->dump_name ());
    6478              :     }
    6479        19363 : }
    6480              : 
    6481              : /* Removes all useless callback edges from the callgraph.  Useless callback
    6482              :    edges might mess up the callgraph, because they might be impossible to
    6483              :    redirect and so on, leading to crashes.  Their usefulness is evaluated
    6484              :    through callback_edge_useful_p.  */
    6485              : 
    6486              : static void
    6487       129027 : purge_useless_callback_edges ()
    6488              : {
    6489       129027 :   if (dump_file)
    6490          161 :     fprintf (dump_file, "\nPurging useless callback edges:\n");
    6491              : 
    6492       129027 :   cgraph_edge *e;
    6493       129027 :   cgraph_node *node;
    6494      1428823 :   FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
    6495              :     {
    6496      6757435 :       for (e = node->callees; e; e = e->next_callee)
    6497              :         {
    6498      5457639 :           if (e->has_callback)
    6499              :             {
    6500        13648 :               if (dump_file)
    6501            3 :                 fprintf (dump_file, "\tExamining callbacks of edge %s -> %s:\n",
    6502            3 :                          e->caller->dump_name (), e->callee->dump_name ());
    6503        13648 :               if (!lookup_attribute (CALLBACK_ATTR_IDENT,
    6504        13648 :                                      DECL_ATTRIBUTES (e->callee->decl))
    6505        13648 :                   && !callback_is_special_cased (e->callee->decl, e->call_stmt))
    6506              :                 {
    6507            1 :                   if (dump_file)
    6508            0 :                     fprintf (
    6509              :                       dump_file,
    6510              :                       "\t\tPurging callbacks, because the callback-dispatching"
    6511              :                       "function no longer has any callback attributes.\n");
    6512            1 :                   e->purge_callback_edges ();
    6513            1 :                   continue;
    6514              :                 }
    6515        13647 :               cgraph_edge *cbe, *next;
    6516        27294 :               for (cbe = e->first_callback_edge (); cbe; cbe = next)
    6517              :                 {
    6518        13647 :                   next = cbe->next_callback_edge ();
    6519        13647 :                   if (!callback_edge_useful_p (cbe))
    6520              :                     {
    6521        13285 :                       if (dump_file)
    6522            1 :                         fprintf (dump_file,
    6523              :                                  "\t\tCallback edge %s -> %s not deemed "
    6524              :                                  "useful, removing.\n",
    6525            1 :                                  cbe->caller->dump_name (),
    6526            1 :                                  cbe->callee->dump_name ());
    6527        13285 :                       cgraph_edge::remove (cbe);
    6528              :                     }
    6529              :                   else
    6530              :                     {
    6531          362 :                       if (dump_file)
    6532            2 :                         fprintf (dump_file,
    6533              :                                  "\t\tKept callback edge %s -> %s "
    6534              :                                  "because it looks useful.\n",
    6535            2 :                                  cbe->caller->dump_name (),
    6536            2 :                                  cbe->callee->dump_name ());
    6537              :                     }
    6538              :                 }
    6539              :             }
    6540              :         }
    6541              :     }
    6542              : 
    6543       129027 :   if (dump_file)
    6544          161 :     fprintf (dump_file, "\n");
    6545       129027 : }
    6546              : 
    6547              : /* The decision stage.  Iterate over the topological order of call graph nodes
    6548              :    TOPO and make specialized clones if deemed beneficial.  */
    6549              : 
    6550              : static void
    6551       129027 : ipcp_decision_stage (class ipa_topo_info *topo)
    6552              : {
    6553       129027 :   int i;
    6554              : 
    6555       129027 :   if (dump_file)
    6556          161 :     fprintf (dump_file, "\nIPA decision stage (%i sweeps):\n",
    6557              :              max_number_sweeps);
    6558              : 
    6559       495323 :   for (int cur_sweep = 1; cur_sweep <= max_number_sweeps; cur_sweep++)
    6560              :     {
    6561       366296 :       if (dump_file && (dump_flags & TDF_DETAILS))
    6562          144 :         fprintf (dump_file, "\nIPA decision sweep number %i (out of %i):\n",
    6563              :                  cur_sweep, max_number_sweeps);
    6564              : 
    6565      4390176 :       for (i = topo->nnodes - 1; i >= 0; i--)
    6566              :         {
    6567      4023880 :           struct cgraph_node *node = topo->order[i];
    6568      4023880 :           bool change = false, iterate = true;
    6569              : 
    6570      8067130 :           while (iterate)
    6571              :             {
    6572              :               struct cgraph_node *v;
    6573              :               iterate = false;
    6574      4058100 :               for (v = node;
    6575      8101350 :                    v;
    6576      4058100 :                    v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
    6577      4058100 :                 if (v->has_gimple_body_p ()
    6578      3830418 :                     && ipcp_versionable_function_p (v)
    6579      4058100 :                     && (cur_sweep
    6580      3227265 :                         <= opt_for_fn (node->decl, param_ipa_cp_sweeps)))
    6581      3227265 :                   iterate |= decide_whether_version_node (v, cur_sweep);
    6582              : 
    6583      4043250 :               change |= iterate;
    6584              :             }
    6585      4023880 :           if (change)
    6586        19363 :             identify_dead_nodes (node);
    6587              :         }
    6588              :     }
    6589              : 
    6590              :   /* Currently, the primary use of callback edges is constant propagation.
    6591              :      Constant propagation is now over, so we have to remove unused callback
    6592              :      edges.  */
    6593       129027 :   purge_useless_callback_edges ();
    6594       129027 : }
    6595              : 
    6596              : /* Look up all VR and bits information that we have discovered and copy it
    6597              :    over to the transformation summary.  */
    6598              : 
    6599              : static void
    6600       129027 : ipcp_store_vr_results (void)
    6601              : {
    6602       129027 :   cgraph_node *node;
    6603              : 
    6604      1428823 :   FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
    6605              :     {
    6606      1299796 :       ipa_node_params *info = ipa_node_params_sum->get (node);
    6607      1299796 :       bool dumped_sth = false;
    6608      1299796 :       bool found_useful_result = false;
    6609      1299796 :       bool do_vr = true;
    6610      1299796 :       bool do_bits = true;
    6611              : 
    6612              :       /* If the function is not local, the gathered information is only useful
    6613              :          for clones.  */
    6614      1299796 :       if (!node->local)
    6615      1134174 :         continue;
    6616              : 
    6617       165622 :       if (!info || !opt_for_fn (node->decl, flag_ipa_vrp))
    6618              :         {
    6619         4819 :           if (dump_file)
    6620            6 :             fprintf (dump_file, "Not considering %s for VR discovery "
    6621              :                      "and propagate; -fipa-ipa-vrp: disabled.\n",
    6622              :                      node->dump_name ());
    6623              :           do_vr = false;
    6624              :         }
    6625       165622 :       if (!info || !opt_for_fn (node->decl, flag_ipa_bit_cp))
    6626              :         {
    6627         4790 :           if (dump_file)
    6628            2 :             fprintf (dump_file, "Not considering %s for ipa bitwise "
    6629              :                                 "propagation ; -fipa-bit-cp: disabled.\n",
    6630              :                                 node->dump_name ());
    6631         4790 :           do_bits = false;
    6632              :         }
    6633         4790 :       if (!do_bits && !do_vr)
    6634         4784 :         continue;
    6635              : 
    6636       160838 :       if (info->ipcp_orig_node)
    6637        21123 :         info = ipa_node_params_sum->get (info->ipcp_orig_node);
    6638       160838 :       if (info->lattices.is_empty ())
    6639              :         /* Newly expanded artificial thunks do not have lattices.  */
    6640        51230 :         continue;
    6641              : 
    6642       109608 :       unsigned count = ipa_get_param_count (info);
    6643       226903 :       for (unsigned i = 0; i < count; i++)
    6644              :         {
    6645       177288 :           ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
    6646       177288 :           if (do_vr
    6647       177262 :               && !plats->m_value_range.bottom_p ()
    6648       235112 :               && !plats->m_value_range.top_p ())
    6649              :             {
    6650              :               found_useful_result = true;
    6651              :               break;
    6652              :             }
    6653       119465 :           if (do_bits && plats->bits_lattice.constant_p ())
    6654              :             {
    6655              :               found_useful_result = true;
    6656              :               break;
    6657              :             }
    6658              :         }
    6659       109608 :       if (!found_useful_result)
    6660        49615 :         continue;
    6661              : 
    6662        59993 :       ipcp_transformation_initialize ();
    6663        59993 :       ipcp_transformation *ts = ipcp_transformation_sum->get_create (node);
    6664        59993 :       vec_safe_reserve_exact (ts->m_vr, count);
    6665              : 
    6666       218177 :       for (unsigned i = 0; i < count; i++)
    6667              :         {
    6668       158184 :           ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
    6669       158184 :           ipcp_bits_lattice *bits = NULL;
    6670              : 
    6671       158184 :           if (do_bits
    6672       158180 :               && plats->bits_lattice.constant_p ()
    6673       249408 :               && dbg_cnt (ipa_cp_bits))
    6674        91224 :             bits = &plats->bits_lattice;
    6675              : 
    6676       158184 :           if (do_vr
    6677       158160 :               && !plats->m_value_range.bottom_p ()
    6678       109721 :               && !plats->m_value_range.top_p ()
    6679       267905 :               && dbg_cnt (ipa_cp_vr))
    6680              :             {
    6681       109721 :               if (bits)
    6682              :                 {
    6683        86014 :                   value_range tmp = plats->m_value_range.m_vr;
    6684        86014 :                   tree type = ipa_get_type (info, i);
    6685       172028 :                   irange_bitmask bm (wide_int::from (bits->get_value (),
    6686        86014 :                                                      TYPE_PRECISION (type),
    6687        86014 :                                                      TYPE_SIGN (type)),
    6688       172028 :                                      wide_int::from (bits->get_mask (),
    6689        86014 :                                                      TYPE_PRECISION (type),
    6690       172028 :                                                      TYPE_SIGN (type)));
    6691        86014 :                   tmp.update_bitmask (bm);
    6692              :                   // Reflecting the bitmask on the ranges can sometime
    6693              :                   // produce an UNDEFINED value if the the bitmask update
    6694              :                   // was previously deferred.  See PR 120048.
    6695        86014 :                   if (tmp.undefined_p ())
    6696            0 :                     tmp.set_varying (type);
    6697        86014 :                   ipa_vr vr (tmp);
    6698        86014 :                   ts->m_vr->quick_push (vr);
    6699        86014 :                 }
    6700              :               else
    6701              :                 {
    6702        23707 :                   ipa_vr vr (plats->m_value_range.m_vr);
    6703        23707 :                   ts->m_vr->quick_push (vr);
    6704              :                 }
    6705              :             }
    6706        48463 :           else if (bits)
    6707              :             {
    6708         5210 :               tree type = ipa_get_type (info, i);
    6709         5210 :               value_range tmp;
    6710         5210 :               tmp.set_varying (type);
    6711        10420 :               irange_bitmask bm (wide_int::from (bits->get_value (),
    6712         5210 :                                                  TYPE_PRECISION (type),
    6713         5210 :                                                  TYPE_SIGN (type)),
    6714        10420 :                                  wide_int::from (bits->get_mask (),
    6715         5210 :                                                  TYPE_PRECISION (type),
    6716        10420 :                                                  TYPE_SIGN (type)));
    6717         5210 :               tmp.update_bitmask (bm);
    6718              :               // Reflecting the bitmask on the ranges can sometime
    6719              :               // produce an UNDEFINED value if the the bitmask update
    6720              :               // was previously deferred.  See PR 120048.
    6721         5210 :               if (tmp.undefined_p ())
    6722            0 :                 tmp.set_varying (type);
    6723         5210 :               ipa_vr vr (tmp);
    6724         5210 :               ts->m_vr->quick_push (vr);
    6725         5210 :             }
    6726              :           else
    6727              :             {
    6728        43253 :               ipa_vr vr;
    6729        43253 :               ts->m_vr->quick_push (vr);
    6730              :             }
    6731              : 
    6732       158184 :           if (!dump_file || !bits)
    6733       157769 :             continue;
    6734              : 
    6735          415 :           if (!dumped_sth)
    6736              :             {
    6737          296 :               fprintf (dump_file, "Propagated bits info for function %s:\n",
    6738              :                        node->dump_name ());
    6739          296 :               dumped_sth = true;
    6740              :             }
    6741          415 :           fprintf (dump_file, " param %i: value = ", i);
    6742          415 :           ipcp_print_widest_int (dump_file, bits->get_value ());
    6743          415 :           fprintf (dump_file, ", mask = ");
    6744          415 :           ipcp_print_widest_int (dump_file, bits->get_mask ());
    6745          415 :           fprintf (dump_file, "\n");
    6746              :         }
    6747              :     }
    6748       129027 : }
    6749              : 
    6750              : /* The IPCP driver.  */
    6751              : 
    6752              : static unsigned int
    6753       129027 : ipcp_driver (void)
    6754              : {
    6755       129027 :   class ipa_topo_info topo;
    6756              : 
    6757       129027 :   if (edge_clone_summaries == NULL)
    6758       129027 :     edge_clone_summaries = new edge_clone_summary_t (symtab);
    6759              : 
    6760       129027 :   ipa_check_create_node_params ();
    6761       129027 :   ipa_check_create_edge_args ();
    6762       129027 :   clone_num_suffixes = new hash_map<const char *, unsigned>;
    6763              : 
    6764       129027 :   if (dump_file)
    6765              :     {
    6766          161 :       fprintf (dump_file, "\nIPA structures before propagation:\n");
    6767          161 :       if (dump_flags & TDF_DETAILS)
    6768           48 :         ipa_print_all_params (dump_file);
    6769          161 :       ipa_print_all_jump_functions (dump_file);
    6770              :     }
    6771              : 
    6772              :   /* Topological sort.  */
    6773       129027 :   build_toporder_info (&topo);
    6774              :   /* Do the interprocedural propagation.  */
    6775       129027 :   ipcp_propagate_stage (&topo);
    6776              :   /* Decide what constant propagation and cloning should be performed.  */
    6777       129027 :   ipcp_decision_stage (&topo);
    6778              :   /* Store results of value range and bits propagation.  */
    6779       129027 :   ipcp_store_vr_results ();
    6780              : 
    6781              :   /* Free all IPCP structures.  */
    6782       258054 :   delete clone_num_suffixes;
    6783       129027 :   free_toporder_info (&topo);
    6784       129027 :   delete edge_clone_summaries;
    6785       129027 :   edge_clone_summaries = NULL;
    6786       129027 :   ipa_free_all_structures_after_ipa_cp ();
    6787       129027 :   if (dump_file)
    6788          161 :     fprintf (dump_file, "\nIPA constant propagation end\n");
    6789       129027 :   return 0;
    6790              : }
    6791              : 
    6792              : /* Initialization and computation of IPCP data structures.  This is the initial
    6793              :    intraprocedural analysis of functions, which gathers information to be
    6794              :    propagated later on.  */
    6795              : 
    6796              : static void
    6797       125886 : ipcp_generate_summary (void)
    6798              : {
    6799       125886 :   struct cgraph_node *node;
    6800              : 
    6801       125886 :   if (dump_file)
    6802          163 :     fprintf (dump_file, "\nIPA constant propagation start:\n");
    6803       125886 :   ipa_register_cgraph_hooks ();
    6804              : 
    6805      1380770 :   FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
    6806      1254884 :     ipa_analyze_node (node);
    6807              : 
    6808       125886 :   varpool_node *vnode;
    6809      1799698 :   FOR_EACH_STATIC_INITIALIZER (vnode)
    6810      1673812 :     ipa_analyze_var_static_initializer (vnode);
    6811       125886 : }
    6812              : 
    6813              : namespace {
    6814              : 
    6815              : const pass_data pass_data_ipa_cp =
    6816              : {
    6817              :   IPA_PASS, /* type */
    6818              :   "cp", /* name */
    6819              :   OPTGROUP_NONE, /* optinfo_flags */
    6820              :   TV_IPA_CONSTANT_PROP, /* tv_id */
    6821              :   0, /* properties_required */
    6822              :   0, /* properties_provided */
    6823              :   0, /* properties_destroyed */
    6824              :   0, /* todo_flags_start */
    6825              :   ( TODO_dump_symtab | TODO_remove_functions ), /* todo_flags_finish */
    6826              : };
    6827              : 
    6828              : class pass_ipa_cp : public ipa_opt_pass_d
    6829              : {
    6830              : public:
    6831       288767 :   pass_ipa_cp (gcc::context *ctxt)
    6832              :     : ipa_opt_pass_d (pass_data_ipa_cp, ctxt,
    6833              :                       ipcp_generate_summary, /* generate_summary */
    6834              :                       NULL, /* write_summary */
    6835              :                       NULL, /* read_summary */
    6836              :                       ipcp_write_transformation_summaries, /*
    6837              :                       write_optimization_summary */
    6838              :                       ipcp_read_transformation_summaries, /*
    6839              :                       read_optimization_summary */
    6840              :                       NULL, /* stmt_fixup */
    6841              :                       0, /* function_transform_todo_flags_start */
    6842              :                       ipcp_transform_function, /* function_transform */
    6843       288767 :                       NULL) /* variable_transform */
    6844       288767 :   {}
    6845              : 
    6846              :   /* opt_pass methods: */
    6847       577224 :   bool gate (function *) final override
    6848              :     {
    6849              :       /* FIXME: We should remove the optimize check after we ensure we never run
    6850              :          IPA passes when not optimizing.  */
    6851       577224 :       return (flag_ipa_cp && optimize) || in_lto_p;
    6852              :     }
    6853              : 
    6854       129027 :   unsigned int execute (function *) final override { return ipcp_driver (); }
    6855              : 
    6856              : }; // class pass_ipa_cp
    6857              : 
    6858              : } // anon namespace
    6859              : 
    6860              : ipa_opt_pass_d *
    6861       288767 : make_pass_ipa_cp (gcc::context *ctxt)
    6862              : {
    6863       288767 :   return new pass_ipa_cp (ctxt);
    6864              : }
    6865              : 
    6866              : /* Reset all state within ipa-cp.cc so that we can rerun the compiler
    6867              :    within the same process.  For use by toplev::finalize.  */
    6868              : 
    6869              : void
    6870       259439 : ipa_cp_cc_finalize (void)
    6871              : {
    6872       259439 :   overall_size = 0;
    6873       259439 :   orig_overall_size = 0;
    6874       259439 :   ipcp_free_transformation_sum ();
    6875       259439 : }
    6876              : 
    6877              : /* Given PARAM which must be a parameter of function FNDECL described by THIS,
    6878              :    return its index in the DECL_ARGUMENTS chain, using a pre-computed
    6879              :    DECL_UID-sorted vector if available (which is pre-computed only if there are
    6880              :    many parameters).  Can return -1 if param is static chain not represented
    6881              :    among DECL_ARGUMENTS. */
    6882              : 
    6883              : int
    6884       124963 : ipcp_transformation::get_param_index (const_tree fndecl, const_tree param) const
    6885              : {
    6886       124963 :   gcc_assert (TREE_CODE (param) == PARM_DECL);
    6887       124963 :   if (m_uid_to_idx)
    6888              :     {
    6889            0 :       unsigned puid = DECL_UID (param);
    6890            0 :       const ipa_uid_to_idx_map_elt *res
    6891            0 :         = std::lower_bound (m_uid_to_idx->begin(), m_uid_to_idx->end (), puid,
    6892            0 :                             [] (const ipa_uid_to_idx_map_elt &elt, unsigned uid)
    6893              :                             {
    6894            0 :                               return elt.uid < uid;
    6895              :                             });
    6896            0 :       if (res == m_uid_to_idx->end ()
    6897            0 :           || res->uid != puid)
    6898              :         {
    6899            0 :           gcc_assert (DECL_STATIC_CHAIN (fndecl));
    6900              :           return -1;
    6901              :         }
    6902            0 :       return res->index;
    6903              :     }
    6904              : 
    6905       124963 :   unsigned index = 0;
    6906       284524 :   for (tree p = DECL_ARGUMENTS (fndecl); p; p = DECL_CHAIN (p), index++)
    6907       283070 :     if (p == param)
    6908       123509 :       return (int) index;
    6909              : 
    6910         1454 :   gcc_assert (DECL_STATIC_CHAIN (fndecl));
    6911              :   return -1;
    6912              : }
    6913              : 
    6914              : /* Helper function to qsort a vector of ipa_uid_to_idx_map_elt elements
    6915              :    according to the uid.  */
    6916              : 
    6917              : static int
    6918            0 : compare_uids (const void *a, const void *b)
    6919              : {
    6920            0 :   const ipa_uid_to_idx_map_elt *e1 = (const ipa_uid_to_idx_map_elt *) a;
    6921            0 :   const ipa_uid_to_idx_map_elt *e2 = (const ipa_uid_to_idx_map_elt *) b;
    6922            0 :   if (e1->uid < e2->uid)
    6923              :     return -1;
    6924            0 :   if (e1->uid > e2->uid)
    6925              :     return 1;
    6926            0 :   gcc_unreachable ();
    6927              : }
    6928              : 
    6929              : /* Assuming THIS describes FNDECL and it has sufficiently many parameters to
    6930              :    justify the overhead, create a DECL_UID-sorted vector to speed up mapping
    6931              :    from parameters to their indices in DECL_ARGUMENTS chain.  */
    6932              : 
    6933              : void
    6934        22912 : ipcp_transformation::maybe_create_parm_idx_map (tree fndecl)
    6935              : {
    6936        22912 :   int c = count_formal_params (fndecl);
    6937        22912 :   if (c < 32)
    6938              :     return;
    6939              : 
    6940            0 :   m_uid_to_idx = NULL;
    6941            0 :   vec_safe_reserve (m_uid_to_idx, c, true);
    6942            0 :   unsigned index = 0;
    6943            0 :   for (tree p = DECL_ARGUMENTS (fndecl); p; p = DECL_CHAIN (p), index++)
    6944              :     {
    6945            0 :       ipa_uid_to_idx_map_elt elt;
    6946            0 :       elt.uid = DECL_UID (p);
    6947            0 :       elt.index = index;
    6948            0 :       m_uid_to_idx->quick_push (elt);
    6949              :     }
    6950            0 :   m_uid_to_idx->qsort (compare_uids);
    6951              : }
        

Generated by: LCOV version 2.4-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.