LCOV - code coverage report
Current view: top level - gcc - ipa-param-manipulation.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 84.0 % 1311 1101
Test Date: 2026-11-14 16:39:50 Functions: 91.8 % 61 56
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Manipulation of formal and actual parameters of functions and function
       2              :    calls.
       3              :    Copyright (C) 2017-2026 Free Software Foundation, Inc.
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it under
       8              : the terms of the GNU General Public License as published by the Free
       9              : Software Foundation; either version 3, or (at your option) any later
      10              : version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15              : for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #define INCLUDE_ALGORITHM
      22              : #include "config.h"
      23              : #include "system.h"
      24              : #include "coretypes.h"
      25              : #include "backend.h"
      26              : #include "tree.h"
      27              : #include "gimple.h"
      28              : #include "ssa.h"
      29              : #include "cgraph.h"
      30              : #include "fold-const.h"
      31              : #include "tree-eh.h"
      32              : #include "stor-layout.h"
      33              : #include "gimplify.h"
      34              : #include "gimple-iterator.h"
      35              : #include "gimplify-me.h"
      36              : #include "tree-cfg.h"
      37              : #include "tree-dfa.h"
      38              : #include "ipa-param-manipulation.h"
      39              : #include "print-tree.h"
      40              : #include "gimple-pretty-print.h"
      41              : #include "builtins.h"
      42              : #include "tree-ssa.h"
      43              : #include "tree-inline.h"
      44              : #include "alloc-pool.h"
      45              : #include "symbol-summary.h"
      46              : #include "symtab-clones.h"
      47              : #include "tree-phinodes.h"
      48              : #include "cfgexpand.h"
      49              : #include "attribs.h"
      50              : #include "sreal.h"
      51              : #include "ipa-cp.h"
      52              : #include "ipa-prop.h"
      53              : #include "data-streamer.h"
      54              : #include "attr-callback.h"
      55              : #include "callback-info.h"
      56              : 
      57              : /* Actual prefixes of different newly synthesized parameters.  Keep in sync
      58              :    with IPA_PARAM_PREFIX_* defines.  */
      59              : 
      60              : static const char *ipa_param_prefixes[IPA_PARAM_PREFIX_COUNT]
      61              :   = {"SYNTH",
      62              :      "ISRA",
      63              :      "simd",
      64              :      "mask"};
      65              : 
      66              : /* Names of parameters for dumping.  Keep in sync with enum ipa_parm_op.  */
      67              : 
      68              : static const char *ipa_param_op_names[IPA_PARAM_PREFIX_COUNT]
      69              :   = {"IPA_PARAM_OP_UNDEFINED",
      70              :      "IPA_PARAM_OP_COPY",
      71              :      "IPA_PARAM_OP_NEW",
      72              :      "IPA_PARAM_OP_SPLIT"};
      73              : 
      74              : /* Structure to hold declarations representing pass-through IPA-SRA splits.  In
      75              :    essence, it tells new index for a combination of original index and
      76              :    offset.  */
      77              : 
      78              : struct pass_through_split_map
      79              : {
      80              :   /* Original argument index.  */
      81              :   unsigned base_index;
      82              :   /* Offset of the split part in the original argument.  */
      83              :   unsigned unit_offset;
      84              :   /* Index of the split part in the call statement - where clone
      85              :      materialization put it.  */
      86              :   int new_index;
      87              : };
      88              : 
      89              : /* Information about some call statements that needs to be conveyed from clone
      90              :    materialization to edge redirection. */
      91              : 
      92       138104 : class ipa_edge_modification_info
      93              : {
      94              :  public:
      95       138104 :   ipa_edge_modification_info ()
      96       138104 :     {}
      97              : 
      98              :   /* Mapping of original argument indices to where those arguments sit in the
      99              :      call statement now or to a negative index if they were removed.  */
     100              :   auto_vec<int> index_map;
     101              :   /* Information about ISRA replacements put into the call statement at the
     102              :      clone materialization stages.  */
     103              :   auto_vec<pass_through_split_map> pass_through_map;
     104              :   /* Necessary adjustment to ipa_param_adjustments::m_always_copy_start when
     105              :      redirecting the call.  */
     106              :   int always_copy_delta = 0;
     107              : };
     108              : 
     109              : /* Class for storing and retrieving summaries about cal statement
     110              :    modifications.  */
     111              : 
     112              : class ipa_edge_modification_sum
     113              :   : public call_summary <ipa_edge_modification_info *>
     114              : {
     115              :  public:
     116         4318 :   ipa_edge_modification_sum (symbol_table *table)
     117         8636 :     : call_summary<ipa_edge_modification_info *> (table)
     118              :   {
     119              :   }
     120              : 
     121              :   /* Hook that is called by summary when an edge is duplicated.  */
     122              : 
     123        66848 :   void duplicate (cgraph_edge *,
     124              :                   cgraph_edge *,
     125              :                   ipa_edge_modification_info *old_info,
     126              :                   ipa_edge_modification_info *new_info) final override
     127              :   {
     128        66848 :     new_info->index_map.safe_splice (old_info->index_map);
     129        66848 :     new_info->pass_through_map.safe_splice (old_info->pass_through_map);
     130        66848 :     new_info->always_copy_delta = old_info->always_copy_delta;
     131        66848 :   }
     132              : };
     133              : 
     134              : /* Call summary to store information about edges which have had their arguments
     135              :    partially modified already.  */
     136              : 
     137              : static ipa_edge_modification_sum *ipa_edge_modifications;
     138              : 
     139              : /* Fail compilation if CS has any summary associated with it in
     140              :    ipa_edge_modifications.  */
     141              : 
     142              : DEBUG_FUNCTION void
     143       314663 : ipa_verify_edge_has_no_modifications (cgraph_edge *cs)
     144              : {
     145       314663 :   gcc_assert (!ipa_edge_modifications || !ipa_edge_modifications->get (cs));
     146       314663 : }
     147              : 
     148              : /* Fill an empty vector ARGS with PARM_DECLs representing formal parameters of
     149              :    FNDECL.  The function should not be called during LTO WPA phase except for
     150              :    thunks (or functions with bodies streamed in). */
     151              : 
     152              : void
     153       198521 : push_function_arg_decls (vec<tree> *args, tree fndecl)
     154              : {
     155       198521 :   int count;
     156       198521 :   tree parm;
     157              : 
     158              :   /* Safety check that we do not attempt to use the function in WPA, except
     159              :      when the function is a thunk and then we have DECL_ARGUMENTS or when we
     160              :      have already explicitly loaded its body.  */
     161       198521 :   gcc_assert (!flag_wpa
     162              :               || DECL_ARGUMENTS (fndecl)
     163              :               || gimple_has_body_p (fndecl));
     164       198521 :   count = 0;
     165       712032 :   for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
     166       513511 :     count++;
     167              : 
     168       198521 :   args->reserve_exact (count);
     169       712032 :   for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
     170       513511 :     args->quick_push (parm);
     171       198521 : }
     172              : 
     173              : /* Fill an empty vector TYPES with trees representing formal parameters of
     174              :    function type FNTYPE.  */
     175              : 
     176              : void
     177       484834 : push_function_arg_types (vec<tree> *types, tree fntype)
     178              : {
     179       484834 :   int count = 0;
     180       484834 :   tree t;
     181              : 
     182      2205582 :   for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
     183      1720748 :     count++;
     184              : 
     185       484834 :   types->reserve_exact (count);
     186      2205582 :   for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
     187      1720748 :     types->quick_push (TREE_VALUE (t));
     188       484834 : }
     189              : 
     190              : /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
     191              :    friendly way, assuming they are meant to be applied to FNDECL.  */
     192              : 
     193              : void
     194           32 : ipa_dump_adjusted_parameters (FILE *f,
     195              :                               vec<ipa_adjusted_param, va_gc> *adj_params)
     196              : {
     197           32 :   unsigned i, len = vec_safe_length (adj_params);
     198           63 :   bool first = true;
     199              : 
     200           31 :   if (!len)
     201              :     return;
     202              : 
     203           31 :   fprintf (f, "    IPA adjusted parameters: ");
     204          106 :   for (i = 0; i < len; i++)
     205              :     {
     206           44 :       struct ipa_adjusted_param *apm;
     207           44 :       apm = &(*adj_params)[i];
     208              : 
     209           44 :       if (!first)
     210           13 :         fprintf (f, "                             ");
     211              :       else
     212              :         first = false;
     213              : 
     214           44 :       fprintf (f, "%i. %s %s", i, ipa_param_op_names[apm->op],
     215           44 :                apm->prev_clone_adjustment ? "prev_clone_adjustment " : "");
     216           44 :       switch (apm->op)
     217              :         {
     218              :         case IPA_PARAM_OP_UNDEFINED:
     219              :           break;
     220              : 
     221            8 :         case IPA_PARAM_OP_COPY:
     222            8 :           fprintf (f, ", base_index: %u", apm->base_index);
     223            8 :           fprintf (f, ", prev_clone_index: %u", apm->prev_clone_index);
     224            8 :           break;
     225              : 
     226           36 :         case IPA_PARAM_OP_SPLIT:
     227           36 :           fprintf (f, ", offset: %u", apm->unit_offset);
     228              :           /* fall-through */
     229           36 :         case IPA_PARAM_OP_NEW:
     230           36 :           fprintf (f, ", base_index: %u", apm->base_index);
     231           36 :           fprintf (f, ", prev_clone_index: %u", apm->prev_clone_index);
     232           36 :           print_node_brief (f, ", type: ", apm->type, 0);
     233           36 :           print_node_brief (f, ", alias type: ", apm->alias_ptr_type, 0);
     234           36 :           fprintf (f, " prefix: %s",
     235           36 :                    ipa_param_prefixes[apm->param_prefix_index]);
     236           36 :           if (apm->reverse)
     237            0 :             fprintf (f, ", reverse");
     238              :           break;
     239              :         }
     240           44 :       fprintf (f, "\n");
     241              :     }
     242              : }
     243              : 
     244              : /* Fill NEW_TYPES with types of a function after its current OTYPES have been
     245              :    modified as described in ADJ_PARAMS.  When USE_PREV_INDICES is true, use
     246              :    prev_clone_index from ADJ_PARAMS as opposed to base_index when the parameter
     247              :    is false.  */
     248              : 
     249              : static void
     250       480027 : fill_vector_of_new_param_types (vec<tree> *new_types, vec<tree> *otypes,
     251              :                                 vec<ipa_adjusted_param, va_gc> *adj_params,
     252              :                                 bool use_prev_indices)
     253              : {
     254       480027 :   unsigned adj_len = vec_safe_length (adj_params);
     255       480027 :   new_types->reserve_exact (adj_len);
     256      1929632 :   for (unsigned i = 0; i < adj_len ; i++)
     257              :     {
     258       969578 :       ipa_adjusted_param *apm = &(*adj_params)[i];
     259       969578 :       if (apm->op == IPA_PARAM_OP_COPY)
     260              :         {
     261      1425106 :           unsigned index
     262       712553 :             = use_prev_indices ? apm->prev_clone_index : apm->base_index;
     263              :           /* The following needs to be handled gracefully because of type
     264              :              mismatches.  This happens with LTO but apparently also in Fortran
     265              :              with -fcoarray=lib -O2 -lcaf_single -latomic.  */
     266       712553 :           if (index >= otypes->length ())
     267            0 :             continue;
     268       712553 :           new_types->quick_push ((*otypes)[index]);
     269              :         }
     270       257025 :       else if (apm->op == IPA_PARAM_OP_NEW
     271       257025 :                || apm->op == IPA_PARAM_OP_SPLIT)
     272              :         {
     273       257025 :           tree ntype = apm->type;
     274       257025 :           if (is_gimple_reg_type (ntype)
     275       257025 :               && TYPE_MODE (ntype) != BLKmode)
     276              :             {
     277       254673 :               unsigned malign = GET_MODE_ALIGNMENT (TYPE_MODE (ntype));
     278       254673 :               if (TYPE_ALIGN (ntype) != malign)
     279         2191 :                 ntype = build_aligned_type (ntype, malign);
     280              :             }
     281       257025 :           new_types->quick_push (ntype);
     282              :         }
     283              :       else
     284            0 :         gcc_unreachable ();
     285              :     }
     286       480027 : }
     287              : 
     288              : /* Return false if given attribute should prevent type adjustments.  */
     289              : 
     290              : bool
     291       404422 : ipa_param_adjustments::type_attribute_allowed_p (tree name)
     292              : {
     293       661250 :   if ((is_attribute_p ("fn spec", name) && flag_ipa_modref)
     294       175000 :       || is_attribute_p ("access", name)
     295       161203 :       || is_attribute_p ("returns_nonnull", name)
     296       159982 :       || is_attribute_p ("assume_aligned", name)
     297       159982 :       || is_attribute_p ("nocf_check", name)
     298       564355 :       || is_attribute_p ("warn_unused_result", name))
     299              :     return true;
     300              :   return false;
     301              : }
     302              : 
     303              : /* Return true if attribute should be dropped if parameter changed.  */
     304              : 
     305              : static bool
     306         4949 : drop_type_attribute_if_params_changed_p (tree name)
     307              : {
     308         4949 :   if (is_attribute_p ("fn spec", name)
     309         4949 :       || is_attribute_p ("access", name))
     310              :     return true;
     311              :   return false;
     312              : }
     313              : 
     314              : /* Return TRUE if the attribute should be dropped in the decl it is sitting on
     315              :    changes.  Primarily affects attributes working with the decls arguments.  */
     316              : static bool
     317        27279 : drop_decl_attribute_if_params_changed_p (tree name)
     318              : {
     319            0 :   if (is_attribute_p ("callback_only", name))
     320              :     return true;
     321              :   return false;
     322              : }
     323              : 
     324              : /* Build and return a function type just like ORIG_TYPE but with parameter
     325              :    types given in NEW_PARAM_TYPES - which can be NULL if, but only if,
     326              :    ORIG_TYPE itself has NULL TREE_ARG_TYPEs.  If METHOD2FUNC is true, also make
     327              :    it a FUNCTION_TYPE instead of FUNCTION_TYPE.
     328              :    If ARG_MODIFIED is true drop attributes that are no longer up to date.  */
     329              : 
     330              : static tree
     331       313472 : build_adjusted_function_type (tree orig_type, vec<tree> *new_param_types,
     332              :                               bool method2func, bool skip_return,
     333              :                               bool args_modified)
     334              : {
     335       313472 :   tree new_arg_types = NULL;
     336       313472 :   if (TYPE_ARG_TYPES (orig_type))
     337              :     {
     338       313307 :       gcc_checking_assert (new_param_types);
     339       313307 :       bool last_parm_void = (TREE_VALUE (tree_last (TYPE_ARG_TYPES (orig_type)))
     340       313307 :                              == void_type_node);
     341       313307 :       unsigned len = new_param_types->length ();
     342       938378 :       for (unsigned i = 0; i < len; i++)
     343       625071 :         new_arg_types = tree_cons (NULL_TREE, (*new_param_types)[i],
     344              :                                    new_arg_types);
     345              : 
     346       313307 :       tree new_reversed = nreverse (new_arg_types);
     347       313307 :       if (last_parm_void)
     348              :         {
     349       313298 :           if (new_reversed)
     350       285784 :             TREE_CHAIN (new_arg_types) = void_list_node;
     351              :           else
     352        27514 :             new_reversed = void_list_node;
     353              :         }
     354              :       new_arg_types = new_reversed;
     355              :     }
     356              : 
     357              :   /* Use build_distinct_type_copy to preserve as much as possible from original
     358              :      type (debug info, attribute lists etc.).  The one exception is
     359              :      METHOD_TYPEs which must have THIS argument and when we are asked to remove
     360              :      it, we need to build new FUNCTION_TYPE instead.  */
     361       313472 :   tree new_type = NULL;
     362       313472 :   if (method2func)
     363              :     {
     364       135754 :       tree ret_type;
     365       135754 :       if (skip_return)
     366        13150 :         ret_type = void_type_node;
     367              :       else
     368       122604 :         ret_type = TREE_TYPE (orig_type);
     369              : 
     370       135754 :       new_type
     371       135754 :         = build_distinct_type_copy (build_function_type (ret_type,
     372              :                                                          new_arg_types));
     373       135754 :       TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
     374              :     }
     375              :   else
     376              :     {
     377       177718 :       new_type = build_distinct_type_copy (orig_type);
     378       177718 :       TYPE_ARG_TYPES (new_type) = new_arg_types;
     379       177718 :       if (skip_return)
     380        68846 :         TREE_TYPE (new_type) = void_type_node;
     381              :     }
     382       313472 :   if (args_modified && TYPE_ATTRIBUTES (new_type))
     383              :     {
     384         4949 :       tree t = TYPE_ATTRIBUTES (new_type);
     385         4949 :       tree *last = &TYPE_ATTRIBUTES (new_type);
     386         4949 :       TYPE_ATTRIBUTES (new_type) = NULL;
     387         9898 :       for (;t; t = TREE_CHAIN (t))
     388         4949 :         if (!drop_type_attribute_if_params_changed_p
     389         4949 :                 (get_attribute_name (t)))
     390              :           {
     391            8 :             *last = copy_node (t);
     392            8 :             TREE_CHAIN (*last) = NULL;
     393            8 :             last = &TREE_CHAIN (*last);
     394              :           }
     395              :     }
     396              : 
     397       313472 :   return new_type;
     398              : }
     399              : 
     400              : /* Return the maximum index in any IPA_PARAM_OP_COPY adjustment or -1 if there
     401              :    is none.  */
     402              : 
     403              : int
     404        44534 : ipa_param_adjustments::get_max_base_index ()
     405              : {
     406        44534 :   unsigned adj_len = vec_safe_length (m_adj_params);
     407        44534 :   int max_index = -1;
     408       106396 :   for (unsigned i = 0; i < adj_len ; i++)
     409              :     {
     410        61862 :       ipa_adjusted_param *apm = &(*m_adj_params)[i];
     411        61862 :       if (apm->op == IPA_PARAM_OP_COPY
     412        55402 :           && max_index < apm->base_index)
     413        61862 :         max_index = apm->base_index;
     414              :     }
     415        44534 :   return max_index;
     416              : }
     417              : 
     418              : 
     419              : /* Fill SURVIVING_PARAMS with an array of bools where each one says whether a
     420              :    parameter that originally was at that position still survives in the given
     421              :    clone or is removed/replaced.  If the final array is smaller than an index
     422              :    of an original parameter, that parameter also did not survive.  That a
     423              :    parameter survives does not mean it has the same index as before.  */
     424              : 
     425              : void
     426        26410 : ipa_param_adjustments::get_surviving_params (vec<bool> *surviving_params)
     427              : {
     428        26410 :   unsigned adj_len = vec_safe_length (m_adj_params);
     429        26410 :   int max_index = get_max_base_index ();
     430              : 
     431        26410 :   if (max_index < 0)
     432              :     return;
     433        16748 :   surviving_params->reserve_exact (max_index + 1);
     434        16748 :   surviving_params->quick_grow_cleared (max_index + 1);
     435        63894 :   for (unsigned i = 0; i < adj_len ; i++)
     436              :     {
     437        30398 :       ipa_adjusted_param *apm = &(*m_adj_params)[i];
     438        30398 :       if (apm->op == IPA_PARAM_OP_COPY)
     439        30398 :         (*surviving_params)[apm->base_index] = true;
     440              :     }
     441              : }
     442              : 
     443              : /* Fill NEW_INDICES with new indices of each surviving parameter or -1 for
     444              :    those which do not survive.  Any parameter outside of length of the vector
     445              :    does not survive.  There is currently no support for a parameter to be
     446              :    copied to two distinct new parameters.  */
     447              : 
     448              : void
     449        18124 : ipa_param_adjustments::get_updated_indices (vec<int> *new_indices)
     450              : {
     451        18124 :   unsigned adj_len = vec_safe_length (m_adj_params);
     452        18124 :   int max_index = get_max_base_index ();
     453              : 
     454        18124 :   if (max_index < 0)
     455              :     return;
     456        12825 :   unsigned res_len = max_index + 1;
     457        12825 :   new_indices->reserve_exact (res_len);
     458        57628 :   for (unsigned i = 0; i < res_len ; i++)
     459        31978 :     new_indices->quick_push (-1);
     460        41958 :   for (unsigned i = 0; i < adj_len ; i++)
     461              :     {
     462        29133 :       ipa_adjusted_param *apm = &(*m_adj_params)[i];
     463        29133 :       if (apm->op == IPA_PARAM_OP_COPY)
     464        25004 :         (*new_indices)[apm->base_index] = i;
     465              :     }
     466              : }
     467              : 
     468              : /* Return the original index for the given new parameter index.  Return a
     469              :    negative number if not available.  */
     470              : 
     471              : int
     472       688729 : ipa_param_adjustments::get_original_index (int newidx)
     473              : {
     474       688729 :   const ipa_adjusted_param *adj = &(*m_adj_params)[newidx];
     475       688729 :   if (adj->op != IPA_PARAM_OP_COPY)
     476              :     return -1;
     477       540536 :   return adj->base_index;
     478              : }
     479              : 
     480              : /* Return true if the first parameter (assuming there was one) survives the
     481              :    transformation intact and remains the first one.  */
     482              : 
     483              : bool
     484       302993 : ipa_param_adjustments::first_param_intact_p ()
     485              : {
     486       302993 :   return (!vec_safe_is_empty (m_adj_params)
     487       284571 :           && (*m_adj_params)[0].op == IPA_PARAM_OP_COPY
     488       201734 :           && (*m_adj_params)[0].base_index == 0);
     489              : }
     490              : 
     491              : /* Return true if we have to change what has formerly been a method into a
     492              :    function.  */
     493              : 
     494              : bool
     495       476169 : ipa_param_adjustments::method2func_p (tree orig_type)
     496              : {
     497       476169 :   return ((TREE_CODE (orig_type) == METHOD_TYPE) && !first_param_intact_p ());
     498              : }
     499              : 
     500              : /* Given function type OLD_TYPE, return a new type derived from it after
     501              :    performing all atored modifications.  TYPE_ORIGINAL_P should be true when
     502              :    OLD_TYPE refers to the type before any IPA transformations, as opposed to a
     503              :    type that can be an intermediate one in between various IPA
     504              :    transformations.  Set pointee of ARGS_MODIFIED (if provided) to TRUE if the
     505              :    type's arguments were changed.  */
     506              : 
     507              : tree
     508       308936 : ipa_param_adjustments::build_new_function_type (
     509              :   tree old_type, bool type_original_p, bool *args_modified /* = NULL */)
     510              : {
     511       308936 :   auto_vec<tree,16> new_param_types, *new_param_types_p;
     512       308936 :   if (prototype_p (old_type))
     513              :     {
     514       308835 :       auto_vec<tree, 16> otypes;
     515       308835 :       push_function_arg_types (&otypes, old_type);
     516       308835 :       fill_vector_of_new_param_types (&new_param_types, &otypes, m_adj_params,
     517              :                                       !type_original_p);
     518       308835 :       new_param_types_p = &new_param_types;
     519       308835 :     }
     520              :   else
     521              :     new_param_types_p = NULL;
     522              : 
     523              :   /* Check if any params type cares about are modified.  In this case will
     524              :      need to drop some type attributes.  */
     525       308936 :   bool modified = false;
     526       308936 :   size_t index = 0;
     527       308936 :   if (m_adj_params)
     528       281743 :     for (tree t = TYPE_ARG_TYPES (old_type);
     529       784559 :          t && (int)index < m_always_copy_start && !modified;
     530       502816 :          t = TREE_CHAIN (t), index++)
     531       502816 :       if (index >= m_adj_params->length ()
     532       502816 :           || get_original_index (index) != (int)index)
     533              :         modified = true;
     534              : 
     535       308936 :   if (args_modified)
     536       167190 :     *args_modified = modified;
     537              : 
     538       308936 :   return build_adjusted_function_type (old_type, new_param_types_p,
     539              :                                        method2func_p (old_type), m_skip_return,
     540       308936 :                                        modified);
     541       308936 : }
     542              : 
     543              : /* Build variant of function decl ORIG_DECL which has no return value if
     544              :    M_SKIP_RETURN is true and, if ORIG_DECL's types or parameters is known, has
     545              :    this type adjusted as indicated in M_ADJ_PARAMS. Arguments from
     546              :    DECL_ARGUMENTS list are not processed now, since they are linked by
     547              :    TREE_CHAIN directly and not accessible in LTO during WPA.  The caller is
     548              :    responsible for eliminating them when clones are properly materialized.  */
     549              : 
     550              : tree
     551       167233 : ipa_param_adjustments::adjust_decl (tree orig_decl)
     552              : {
     553       167233 :   tree new_decl = copy_node (orig_decl);
     554       167233 :   tree orig_type = TREE_TYPE (orig_decl);
     555       167233 :   bool args_modified = false;
     556       167233 :   if (prototype_p (orig_type)
     557       167233 :       || (m_skip_return && !VOID_TYPE_P (TREE_TYPE (orig_type))))
     558              :     {
     559       167190 :       tree new_type = build_new_function_type (orig_type, false, &args_modified);
     560       167190 :       TREE_TYPE (new_decl) = new_type;
     561              :     }
     562       167233 :   if (method2func_p (orig_type))
     563        56785 :     DECL_VINDEX (new_decl) = NULL_TREE;
     564              : 
     565              :   /* When signature changes, we need to clear builtin info.  */
     566       167233 :   if (fndecl_built_in_p (new_decl))
     567           97 :     set_decl_built_in_function (new_decl, NOT_BUILT_IN, 0);
     568              : 
     569       167233 :   DECL_VIRTUAL_P (new_decl) = 0;
     570       167233 :   DECL_LANG_SPECIFIC (new_decl) = NULL;
     571              : 
     572              :   /* Drop MALLOC attribute for a void function.  */
     573       167233 :   if (m_skip_return)
     574        53144 :     DECL_IS_MALLOC (new_decl) = 0;
     575              : 
     576              :   /* If the decl's arguments changed, we might need to drop some attributes.  */
     577       167233 :   if (args_modified && DECL_ATTRIBUTES (new_decl))
     578              :     {
     579        20814 :       tree t = DECL_ATTRIBUTES (new_decl);
     580        20814 :       tree *last = &DECL_ATTRIBUTES (new_decl);
     581        20814 :       DECL_ATTRIBUTES (new_decl) = NULL;
     582        48093 :       for (; t; t = TREE_CHAIN (t))
     583        27279 :         if (!drop_decl_attribute_if_params_changed_p (get_attribute_name (t)))
     584              :           {
     585        27279 :             *last = copy_node (t);
     586        27279 :             TREE_CHAIN (*last) = NULL;
     587        27279 :             last = &TREE_CHAIN (*last);
     588              :           }
     589              :     }
     590       167233 :   return new_decl;
     591              : }
     592              : 
     593              : /* Wrapper around get_base_ref_and_offset for cases interesting for IPA-SRA
     594              :    transformations.  Return true if EXPR has an interesting form and fill in
     595              :    *BASE_P and *UNIT_OFFSET_P with the appropriate info.  */
     596              : 
     597              : static bool
     598      1214856 : isra_get_ref_base_and_offset (tree expr, tree *base_p, unsigned *unit_offset_p)
     599              : {
     600      1214856 :   HOST_WIDE_INT offset, size;
     601      1214856 :   bool reverse;
     602      1214856 :   tree base
     603      1214856 :     = get_ref_base_and_extent_hwi (expr, &offset, &size, &reverse);
     604      1214856 :   if (!base || size < 0)
     605              :     return false;
     606              : 
     607      1208886 :   if ((offset % BITS_PER_UNIT) != 0)
     608              :     return false;
     609              : 
     610      1207888 :   if (TREE_CODE (base) == MEM_REF)
     611              :     {
     612       179745 :       poly_int64 plmoff = mem_ref_offset (base).force_shwi ();
     613       179745 :       HOST_WIDE_INT moff;
     614       179745 :       bool is_cst = plmoff.is_constant (&moff);
     615       179745 :       if (!is_cst)
     616              :         return false;
     617       179745 :       offset += moff * BITS_PER_UNIT;
     618       179745 :       base = TREE_OPERAND (base, 0);
     619              :     }
     620              : 
     621      1207888 :   if (offset < 0 || (offset / BITS_PER_UNIT) > UINT_MAX)
     622              :     return false;
     623              : 
     624      1207691 :   *base_p = base;
     625      1207691 :   *unit_offset_p = offset / BITS_PER_UNIT;
     626      1207691 :   return true;
     627              : }
     628              : 
     629              : /* Remove all statements that use NAME directly or indirectly.  KILLED_SSAS
     630              :    contains the SSA_NAMEs that are already being or have been processed and new
     631              :    ones need to be added to it.  The function only has to process situations
     632              :    handled by ssa_name_only_returned_p in ipa-sra.cc with the exception that it
     633              :    can assume it must never reach a use in a return statement.  */
     634              : 
     635              : static void
     636        10582 : purge_all_uses (tree name, hash_set <tree> *killed_ssas)
     637              : {
     638        10582 :   imm_use_iterator imm_iter;
     639        10582 :   gimple *stmt;
     640        10582 :   auto_vec <tree, 4> worklist;
     641        10582 :   auto_vec <gimple *, 4> kill_list;
     642              : 
     643        10582 :   worklist.safe_push (name);
     644        33867 :   while (!worklist.is_empty ())
     645              :     {
     646        12703 :       tree cur_name = worklist.pop ();
     647        14984 :       FOR_EACH_IMM_USE_STMT (stmt, imm_iter, cur_name)
     648              :         {
     649         2281 :           if (gimple_debug_bind_p (stmt))
     650              :             {
     651              :               /* When running within tree-inline, we will never end up here but
     652              :                  adding the SSAs to killed_ssas will do the trick in this case
     653              :                  and the respective debug statements will get reset. */
     654           70 :               gimple_debug_bind_reset_value (stmt);
     655           70 :               update_stmt (stmt);
     656           70 :               continue;
     657              :             }
     658              : 
     659         2211 :           tree lhs = NULL_TREE;
     660         2211 :           if (is_gimple_assign (stmt))
     661          757 :             lhs = gimple_assign_lhs (stmt);
     662         1454 :           else if (gimple_code (stmt) == GIMPLE_PHI)
     663         1454 :             lhs = gimple_phi_result (stmt);
     664         2968 :           gcc_assert (lhs
     665              :                       && (TREE_CODE (lhs) == SSA_NAME)
     666              :                       && !gimple_vdef (stmt));
     667         2211 :           if (!killed_ssas->add (lhs))
     668              :             {
     669         2121 :               worklist.safe_push (lhs);
     670         2121 :               kill_list.safe_push (stmt);
     671              :             }
     672        12703 :         }
     673              :     }
     674              : 
     675              :   /* Remove stmts in reverse and afterwards to properly handle debug stmt
     676              :      generation and to not interfere with immediate use iteration.  */
     677        23285 :   while (!kill_list.is_empty ())
     678              :     {
     679         2121 :       stmt = kill_list.pop ();
     680         2121 :       gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
     681         2121 :       gsi_remove (&gsi, true);
     682              :     }
     683        10582 : }
     684              : 
     685              : /* Modify actual arguments of a function call in statement currently belonging
     686              :    to CS, and make it call CS->callee->decl.  Return the new statement that
     687              :    replaced the old one.  When invoked, cfun and current_function_decl have to
     688              :    be set to the caller.  When called from within tree-inline, KILLED_SSAs has
     689              :    to contain the pointer to killed_new_ssa_names within the copy_body_data
     690              :    structure and SSAs discovered to be useless (if LHS is removed) will be
     691              :    added to it, otherwise it needs to be NULL.  */
     692              : 
     693              : gcall *
     694       626912 : ipa_param_adjustments::modify_call (cgraph_edge *cs,
     695              :                                     bool update_references,
     696              :                                     hash_set <tree> *killed_ssas)
     697              : {
     698       626912 :   gcall *stmt = cs->call_stmt;
     699       626912 :   tree callee_decl = cs->callee->decl;
     700              : 
     701       626912 :   ipa_edge_modification_info *mod_info
     702       626912 :     = ipa_edge_modifications ? ipa_edge_modifications->get (cs) : NULL;
     703       434941 :   if (mod_info && symtab->dump_file)
     704              :     {
     705            0 :       fprintf (symtab->dump_file, "Information about pre-exiting "
     706              :                "modifications.\n  Index map:");
     707            0 :       unsigned idx_len = mod_info->index_map.length ();
     708            0 :       for (unsigned i = 0; i < idx_len; i++)
     709            0 :         fprintf (symtab->dump_file, " %i", mod_info->index_map[i]);
     710            0 :       fprintf (symtab->dump_file, "\n  Pass-through split map: ");
     711            0 :       unsigned ptm_len = mod_info->pass_through_map.length ();
     712            0 :       for (unsigned i = 0; i < ptm_len; i++)
     713            0 :         fprintf (symtab->dump_file,
     714              :                  " (base_index: %u, offset: %u, new_index: %i)",
     715            0 :                  mod_info->pass_through_map[i].base_index,
     716            0 :                  mod_info->pass_through_map[i].unit_offset,
     717            0 :                  mod_info->pass_through_map[i].new_index);
     718            0 :       fprintf (symtab->dump_file, "\n  Always-copy delta: %i\n",
     719              :                mod_info->always_copy_delta);
     720              :     }
     721              : 
     722       626912 :   unsigned len = vec_safe_length (m_adj_params);
     723       626912 :   auto_vec<tree, 16> vargs (len);
     724              :   /* Argument remapping has to be used in this function, because it builds the
     725              :      new call statement from the parameter descriptors, rather than the actual
     726              :      arguments, as in ipa_param_adjustments::modify_call_stmt.  */
     727       626912 :   callback_info *ci = callback_info_sum->get (cs);
     728              : 
     729              :   /* Callback edges should be here only if they are pointing towards .ASSUME
     730              :      bodies, which always have a complete argument mapping.  */
     731       626912 :   if (ci)
     732           64 :     gcc_assert (cs->callee->assume_function);
     733              : 
     734       626912 :   unsigned old_nargs = callback_call_num_args_summary (stmt, ci);
     735       626912 :   unsigned orig_nargs = mod_info ? mod_info->index_map.length () : old_nargs;
     736       626912 :   auto_vec<bool, 16> kept (old_nargs);
     737       626912 :   kept.quick_grow_cleared (old_nargs);
     738              : 
     739       626912 :   cgraph_node *current_node = cgraph_node::get (current_function_decl);
     740       626912 :   if (update_references)
     741            0 :     current_node->remove_stmt_references (stmt);
     742              : 
     743       626912 :   if (cs->callback && cs->callee->assume_function)
     744           64 :     vargs.safe_push (build_fold_addr_expr (callee_decl));
     745              : 
     746       626912 :   gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
     747       626912 :   gimple_stmt_iterator prev_gsi = gsi;
     748       626912 :   gsi_prev (&prev_gsi);
     749      2021631 :   for (unsigned i = 0; i < len; i++)
     750              :     {
     751      1394719 :       ipa_adjusted_param *apm = &(*m_adj_params)[i];
     752      1394719 :       if (apm->op == IPA_PARAM_OP_COPY)
     753              :         {
     754      1048000 :           int index = apm->base_index;
     755      1048000 :           if ((unsigned) index >= orig_nargs)
     756              :             /* Can happen if the original call has argument mismatch,
     757              :                ignore.  */
     758            0 :             continue;
     759      1048000 :           if (mod_info)
     760              :             {
     761        86001 :               index = mod_info->index_map[apm->base_index];
     762        86001 :               gcc_assert (index >= 0);
     763              :             }
     764              : 
     765      1048000 :           tree arg = callback_call_arg_summary (stmt, index, ci);
     766              : 
     767      1048000 :           vargs.quick_push (arg);
     768      1048000 :           kept[index] = true;
     769      1048000 :           continue;
     770      1048000 :         }
     771              : 
     772              :       /* At the moment the only user of IPA_PARAM_OP_NEW modifies calls itself.
     773              :          If we ever want to support it during WPA IPA stage, we'll need a
     774              :          mechanism to call into the IPA passes that introduced them.  Currently
     775              :          we simply mandate that IPA infrastructure understands all argument
     776              :          modifications.  Remember, edge redirection/modification is done only
     777              :          once, not in steps for each pass modifying the callee like clone
     778              :          materialization.  */
     779       346719 :       gcc_assert (apm->op == IPA_PARAM_OP_SPLIT);
     780              : 
     781              :       /* We have to handle pass-through changes differently using the map
     782              :          clone materialziation might have left behind.  */
     783       346719 :       tree repl = NULL_TREE;
     784       346719 :       unsigned ptm_len = mod_info ? mod_info->pass_through_map.length () : 0;
     785       371386 :       for (unsigned j = 0; j < ptm_len; j++)
     786        48277 :         if (mod_info->pass_through_map[j].base_index == apm->base_index
     787        48277 :             && mod_info->pass_through_map[j].unit_offset == apm->unit_offset)
     788              :           {
     789        23610 :             int repl_idx = mod_info->pass_through_map[j].new_index;
     790        23610 :             gcc_assert (repl_idx >= 0);
     791        23610 :             repl = gimple_call_arg (stmt, repl_idx);
     792        23610 :             break;
     793              :           }
     794       346719 :       if (repl)
     795              :         {
     796        23610 :           if (!useless_type_conversion_p(apm->type, repl->typed.type))
     797              :             {
     798            0 :               repl = force_value_to_type (apm->type, repl);
     799            0 :               repl = force_gimple_operand_gsi (&gsi, repl,
     800              :                                                true, NULL, true, GSI_SAME_STMT);
     801              :             }
     802        23610 :           vargs.quick_push (repl);
     803        23610 :           continue;
     804              :         }
     805              : 
     806       323109 :       int index = apm->base_index;
     807       323109 :       if ((unsigned) index >= orig_nargs)
     808              :         /* Can happen if the original call has argument mismatch, ignore.  */
     809            1 :         continue;
     810       323108 :       if (mod_info)
     811              :         {
     812         3340 :           index = mod_info->index_map[apm->base_index];
     813         3340 :           gcc_assert (index >= 0);
     814              :         }
     815       323108 :       tree base = callback_call_arg_summary (stmt, index, ci);
     816              : 
     817              :       /* We create a new parameter out of the value of the old one, we can
     818              :          do the following kind of transformations:
     819              : 
     820              :          - A scalar passed by reference, potentially as a part of a larger
     821              :          aggregate, is converted to a scalar passed by value.
     822              : 
     823              :          - A part of an aggregate is passed instead of the whole aggregate.  */
     824              : 
     825       323108 :       location_t loc = gimple_location (stmt);
     826       323108 :       tree off;
     827       323108 :       bool deref_base = false;
     828       323108 :       unsigned int deref_align = 0;
     829       323108 :       if (TREE_CODE (base) != ADDR_EXPR
     830       323108 :           && is_gimple_reg_type (TREE_TYPE (base)))
     831              :         {
     832              :           /* Detect type mismatches in calls in invalid programs and make a
     833              :              poor attempt to gracefully convert them so that we don't ICE.  */
     834       102211 :           if (!POINTER_TYPE_P (TREE_TYPE (base)))
     835           11 :             base = force_value_to_type (ptr_type_node, base);
     836              : 
     837       102211 :           off = build_int_cst (apm->alias_ptr_type, apm->unit_offset);
     838              :         }
     839              :       else
     840              :         {
     841       220897 :           bool addrof;
     842       220897 :           if (TREE_CODE (base) == ADDR_EXPR)
     843              :             {
     844       183196 :               base = TREE_OPERAND (base, 0);
     845       183196 :               addrof = true;
     846              :             }
     847              :           else
     848              :             addrof = false;
     849              : 
     850       220897 :           tree prev_base = base;
     851       220897 :           poly_int64 base_offset;
     852       220897 :           base = get_addr_base_and_unit_offset (base, &base_offset);
     853              : 
     854              :           /* Aggregate arguments can have non-invariant addresses.  */
     855       220897 :           if (!base)
     856              :             {
     857            0 :               base = build_fold_addr_expr (prev_base);
     858            0 :               off = build_int_cst (apm->alias_ptr_type, apm->unit_offset);
     859              :             }
     860       220897 :           else if (TREE_CODE (base) == MEM_REF)
     861              :             {
     862         5876 :               if (!addrof)
     863              :                 {
     864         5876 :                   deref_base = true;
     865         5876 :                   deref_align = TYPE_ALIGN (TREE_TYPE (base));
     866              :                 }
     867        11752 :               off = build_int_cst (apm->alias_ptr_type,
     868         5876 :                                    base_offset + apm->unit_offset);
     869         5876 :               off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
     870              :                                      off);
     871         5876 :               base = TREE_OPERAND (base, 0);
     872              :             }
     873              :           else
     874              :             {
     875       430042 :               off = build_int_cst (apm->alias_ptr_type,
     876       215021 :                                    base_offset + apm->unit_offset);
     877       215021 :               base = build_fold_addr_expr (base);
     878              :             }
     879              :         }
     880              : 
     881       323108 :       tree type = apm->type;
     882       323108 :       unsigned int align;
     883       323108 :       unsigned HOST_WIDE_INT misalign;
     884              : 
     885       323108 :       if (deref_base)
     886              :         {
     887         5876 :           align = deref_align;
     888         5876 :           misalign = 0;
     889              :         }
     890              :       else
     891              :         {
     892       317232 :           get_pointer_alignment_1 (base, &align, &misalign);
     893              :           /* All users must make sure that we can be optimistic when it
     894              :              comes to alignment in this case (by inspecting the final users
     895              :              of these new parameters).  */
     896       317232 :           if (TYPE_ALIGN (type) > align)
     897       100697 :             align = TYPE_ALIGN (type);
     898              :         }
     899       323108 :       misalign
     900       323108 :         += (offset_int::from (wi::to_wide (off), SIGNED).to_short_addr ()
     901       323108 :             * BITS_PER_UNIT);
     902       323108 :       misalign = misalign & (align - 1);
     903       323108 :       if (misalign != 0)
     904         3091 :         align = least_bit_hwi (misalign);
     905       323108 :       if (align < TYPE_ALIGN (type))
     906            0 :         type = build_aligned_type (type, align);
     907       323108 :       base = force_gimple_operand_gsi (&gsi, base,
     908              :                                        true, NULL, true, GSI_SAME_STMT);
     909       323108 :       tree expr = fold_build2_loc (loc, MEM_REF, type, base, off);
     910       323108 :       REF_REVERSE_STORAGE_ORDER (expr) = apm->reverse;
     911              :       /* If expr is not a valid gimple call argument emit
     912              :          a load into a temporary.  */
     913       323108 :       if (is_gimple_reg_type (TREE_TYPE (expr)))
     914              :         {
     915       319761 :           gimple *tem = gimple_build_assign (NULL_TREE, expr);
     916       319761 :           if (gimple_in_ssa_p (cfun))
     917              :             {
     918       639522 :               gimple_set_vuse (tem, gimple_vuse (stmt));
     919       319761 :               expr = make_ssa_name (TREE_TYPE (expr), tem);
     920              :             }
     921              :           else
     922            0 :             expr = create_tmp_reg (TREE_TYPE (expr));
     923       319761 :           gimple_assign_set_lhs (tem, expr);
     924       319761 :           gsi_insert_before (&gsi, tem, GSI_SAME_STMT);
     925              :         }
     926       323108 :       vargs.quick_push (expr);
     927              :     }
     928              : 
     929       626912 :   if (m_always_copy_start >= 0)
     930              :     {
     931       626912 :       int always_copy_start = m_always_copy_start;
     932       626912 :       if (mod_info)
     933              :         {
     934        71183 :           always_copy_start += mod_info->always_copy_delta;
     935        71183 :           gcc_assert (always_copy_start >= 0);
     936              :         }
     937              : 
     938       626912 :       gcc_assert (!ci || always_copy_start >= (int) old_nargs);
     939       626917 :       for (unsigned i = always_copy_start; i < old_nargs; i++)
     940            5 :         vargs.safe_push (gimple_call_arg (stmt, i));
     941              :     }
     942              : 
     943              :   /* For optimized away parameters, add on the caller side
     944              :      before the call
     945              :      DEBUG D#X => parm_Y(D)
     946              :      stmts and associate D#X with parm in decl_debug_args_lookup
     947              :      vector to say for debug info that if parameter parm had been passed,
     948              :      it would have value parm_Y(D).  */
     949       626912 :   tree old_decl = gimple_call_fndecl (stmt);
     950       626912 :   if (MAY_HAVE_DEBUG_BIND_STMTS && old_decl && callee_decl)
     951              :     {
     952       502731 :       vec<tree, va_gc> **debug_args = NULL;
     953       502731 :       unsigned i = 0;
     954       502731 :       cgraph_node *callee_node = cgraph_node::get (callee_decl);
     955              : 
     956              :       /* FIXME: we don't seem to be able to insert debug args before clone
     957              :          is materialized.  Materializing them early leads to extra memory
     958              :          use.  */
     959       502731 :       if (callee_node->clone_of)
     960            0 :         callee_node->get_untransformed_body ();
     961       502731 :       for (tree old_parm = DECL_ARGUMENTS (old_decl);
     962      1582343 :            old_parm && i < old_nargs && ((int) i) < m_always_copy_start;
     963      1079612 :            old_parm = DECL_CHAIN (old_parm), i++)
     964              :         {
     965      1969534 :           if (!is_gimple_reg (old_parm) || kept[i])
     966       828338 :             continue;
     967       259509 :           tree arg;
     968       259509 :           if (mod_info)
     969              :             {
     970        25547 :               if (mod_info->index_map[i] < 0)
     971         8234 :                 continue;
     972        17313 :               arg = gimple_call_arg (stmt, mod_info->index_map[i]);
     973              :             }
     974              :           else
     975       233962 :             arg = gimple_call_arg (stmt, i);
     976              : 
     977       251275 :           tree origin = DECL_ORIGIN (old_parm);
     978       251275 :           if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
     979              :             {
     980            8 :               if (!fold_convertible_p (TREE_TYPE (origin), arg))
     981            1 :                 continue;
     982            7 :               tree rhs1;
     983            7 :               if (TREE_CODE (arg) == SSA_NAME
     984            0 :                   && gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg))
     985            0 :                   && (rhs1
     986            0 :                       = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (arg)))
     987            7 :                   && useless_type_conversion_p (TREE_TYPE (origin),
     988            0 :                                                 TREE_TYPE (rhs1)))
     989              :                 arg = rhs1;
     990              :               else
     991            7 :                 arg = fold_convert_loc (gimple_location (stmt),
     992            7 :                                         TREE_TYPE (origin), arg);
     993              :             }
     994       251274 :           if (debug_args == NULL)
     995       212017 :             debug_args = decl_debug_args_insert (callee_decl);
     996       251274 :           unsigned int ix;
     997       251274 :           tree ddecl = NULL_TREE;
     998       336557 :           for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl); ix += 2)
     999       320891 :             if (ddecl == origin)
    1000              :               {
    1001       241898 :                 ddecl = (**debug_args)[ix + 1];
    1002       241898 :                 break;
    1003              :               }
    1004       251274 :           if (ddecl == NULL)
    1005              :             {
    1006         9376 :               ddecl = build_debug_expr_decl (TREE_TYPE (origin));
    1007              :               /* FIXME: Is setting the mode really necessary? */
    1008         9376 :               SET_DECL_MODE (ddecl, DECL_MODE (origin));
    1009              : 
    1010         9376 :               vec_safe_push (*debug_args, origin);
    1011         9376 :               vec_safe_push (*debug_args, ddecl);
    1012              :             }
    1013       251274 :           gimple *def_temp = gimple_build_debug_bind (ddecl,
    1014              :                                                       unshare_expr (arg), stmt);
    1015       251274 :           gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
    1016              :         }
    1017              :     }
    1018              : 
    1019       626912 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1020              :     {
    1021           10 :       fprintf (dump_file, "replacing stmt:");
    1022           10 :       print_gimple_stmt (dump_file, gsi_stmt (gsi), 0);
    1023              :     }
    1024              : 
    1025       626912 :   gcall *new_stmt;
    1026       626912 :   if (cs->callback && cs->callee->assume_function)
    1027           64 :     new_stmt = gimple_build_call_internal_vec (IFN_ASSUME, vargs);
    1028              :   else
    1029       626848 :     new_stmt = gimple_build_call_vec (callee_decl, vargs);
    1030              : 
    1031       626912 :   hash_set <tree> *ssas_to_remove = NULL;
    1032       626912 :   if (tree lhs = gimple_call_lhs (stmt))
    1033              :     {
    1034       253177 :       if (!m_skip_return)
    1035       242595 :         gimple_call_set_lhs (new_stmt, lhs);
    1036        10582 :       else if (TREE_CODE (lhs) == SSA_NAME)
    1037              :         {
    1038        10582 :           if (!killed_ssas)
    1039              :             {
    1040          805 :               ssas_to_remove = new hash_set<tree> (8);
    1041          805 :               killed_ssas = ssas_to_remove;
    1042              :             }
    1043        10582 :           killed_ssas->add (lhs);
    1044        10582 :           purge_all_uses (lhs, killed_ssas);
    1045              :         }
    1046              :     }
    1047              : 
    1048       626912 :   gimple_set_block (new_stmt, gimple_block (stmt));
    1049       626912 :   if (gimple_has_location (stmt))
    1050      1113407 :     gimple_set_location (new_stmt, gimple_location (stmt));
    1051       626912 :   gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
    1052       626912 :   gimple_call_copy_flags (new_stmt, stmt);
    1053       626912 :   if (gimple_in_ssa_p (cfun))
    1054       626912 :     gimple_move_vops (new_stmt, stmt);
    1055              : 
    1056       626912 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1057              :     {
    1058           10 :       fprintf (dump_file, "with stmt:");
    1059           10 :       print_gimple_stmt (dump_file, new_stmt, 0);
    1060           10 :       fprintf (dump_file, "\n");
    1061              :     }
    1062       626912 :   gsi_replace (&gsi, new_stmt, true);
    1063       626912 :   if (ssas_to_remove)
    1064              :     {
    1065          805 :       ipa_release_ssas_in_hash (ssas_to_remove);
    1066          805 :       delete ssas_to_remove;
    1067              :     }
    1068       626912 :   if (update_references)
    1069            0 :     do
    1070              :       {
    1071            0 :         current_node->record_stmt_references (gsi_stmt (gsi));
    1072            0 :         gsi_prev (&gsi);
    1073              :       }
    1074            0 :     while (gsi_stmt (gsi) != gsi_stmt (prev_gsi));
    1075              : 
    1076       626912 :   if (mod_info)
    1077        71183 :     ipa_edge_modifications->remove (cs);
    1078       626912 :   return new_stmt;
    1079       626912 : }
    1080              : 
    1081              : /* Dump information contained in the object in textual form to F.  */
    1082              : 
    1083              : void
    1084           32 : ipa_param_adjustments::dump (FILE *f)
    1085              : {
    1086           32 :   fprintf (f, "    m_always_copy_start: %i\n", m_always_copy_start);
    1087           32 :   ipa_dump_adjusted_parameters (f, m_adj_params);
    1088           32 :   if (m_skip_return)
    1089            5 :     fprintf (f, "    Will SKIP return.\n");
    1090           32 : }
    1091              : 
    1092              : /* Dump information contained in the object in textual form to stderr.  */
    1093              : 
    1094              : void
    1095            0 : ipa_param_adjustments::debug ()
    1096              : {
    1097            0 :   dump (stderr);
    1098            0 : }
    1099              : 
    1100              : /* Register a REPLACEMENT for accesses to BASE at UNIT_OFFSET.  */
    1101              : 
    1102              : void
    1103        97807 : ipa_param_body_adjustments::register_replacement (tree base,
    1104              :                                                   unsigned unit_offset,
    1105              :                                                   tree replacement)
    1106              : {
    1107        97807 :   ipa_param_body_replacement psr;
    1108        97807 :   psr.base = base;
    1109        97807 :   psr.repl = replacement;
    1110        97807 :   psr.dummy = NULL_TREE;
    1111        97807 :   psr.unit_offset = unit_offset;
    1112        97807 :   m_replacements.safe_push (psr);
    1113        97807 :   m_sorted_replacements_p = false;
    1114        97807 : }
    1115              : 
    1116              : /* Register that REPLACEMENT should replace parameter described in APM.  */
    1117              : 
    1118              : void
    1119        85234 : ipa_param_body_adjustments::register_replacement (ipa_adjusted_param *apm,
    1120              :                                                   tree replacement)
    1121              : {
    1122        85234 :   gcc_checking_assert (apm->op == IPA_PARAM_OP_SPLIT
    1123              :                        || apm->op == IPA_PARAM_OP_NEW);
    1124        85234 :   gcc_checking_assert (!apm->prev_clone_adjustment);
    1125        85234 :   register_replacement (m_oparms[apm->prev_clone_index], apm->unit_offset,
    1126              :                         replacement);
    1127        85234 : }
    1128              : 
    1129              : /* Comparator for sorting and searching
    1130              :    ipa_param_body_adjustments::m_replacements.  */
    1131              : 
    1132              : static int
    1133      1884734 : compare_param_body_replacement (const void *va, const void *vb)
    1134              : {
    1135      1884734 :   const ipa_param_body_replacement *a = (const ipa_param_body_replacement *) va;
    1136      1884734 :   const ipa_param_body_replacement *b = (const ipa_param_body_replacement *) vb;
    1137              : 
    1138      1884734 :   if (DECL_UID (a->base) < DECL_UID (b->base))
    1139              :     return -1;
    1140      1553731 :   if (DECL_UID (a->base) > DECL_UID (b->base))
    1141              :     return 1;
    1142      1184671 :   if (a->unit_offset < b->unit_offset)
    1143              :     return -1;
    1144       649885 :   if (a->unit_offset > b->unit_offset)
    1145       556486 :     return 1;
    1146              :   return 0;
    1147              : }
    1148              : 
    1149              : /* Sort m_replacements and set m_sorted_replacements_p to true.  */
    1150              : 
    1151              : void
    1152       187036 : ipa_param_body_adjustments::sort_replacements ()
    1153              : {
    1154       187036 :   if (m_sorted_replacements_p)
    1155              :     return;
    1156        54576 :   m_replacements.qsort (compare_param_body_replacement);
    1157        54576 :   m_sorted_replacements_p = true;
    1158              : }
    1159              : 
    1160              : /* Copy or not, as appropriate given m_id and decl context, a pre-existing
    1161              :    PARM_DECL T so that it can be included in the parameters of the modified
    1162              :    function.  */
    1163              : 
    1164              : tree
    1165       267106 : ipa_param_body_adjustments::carry_over_param (tree t)
    1166              : {
    1167       267106 :   tree new_parm;
    1168       267106 :   if (m_id)
    1169              :     {
    1170       263185 :       new_parm = remap_decl (t, m_id);
    1171       263185 :       if (TREE_CODE (new_parm) != PARM_DECL)
    1172            0 :         new_parm = m_id->copy_decl (t, m_id);
    1173              :     }
    1174         3921 :   else if (DECL_CONTEXT (t) != m_fndecl)
    1175              :     {
    1176           15 :       new_parm = copy_node (t);
    1177           15 :       DECL_CONTEXT (new_parm) = m_fndecl;
    1178              :     }
    1179              :   else
    1180              :     new_parm = t;
    1181       267106 :   return new_parm;
    1182              : }
    1183              : 
    1184              : /* If DECL is a gimple register that has a default definition SSA name and that
    1185              :    has some uses, return the default definition, otherwise return NULL_TREE.  */
    1186              : 
    1187              : tree
    1188       159374 : ipa_param_body_adjustments::get_ddef_if_exists_and_is_used (tree decl)
    1189              : {
    1190       159374 :  if (!is_gimple_reg (decl))
    1191              :     return NULL_TREE;
    1192       126704 :   tree ddef = ssa_default_def (m_id->src_cfun, decl);
    1193       126704 :   if (!ddef || has_zero_uses (ddef))
    1194        42409 :     return NULL_TREE;
    1195              :   return ddef;
    1196              : }
    1197              : 
    1198              : /* Populate m_dead_stmts given that DEAD_PARAM is going to be removed without
    1199              :    any replacement or splitting.  REPL is the replacement VAR_SECL to base any
    1200              :    remaining uses of a removed parameter on.  Push all removed SSA names that
    1201              :    are used within debug statements to DEBUGSTACK.  */
    1202              : 
    1203              : void
    1204       116769 : ipa_param_body_adjustments::mark_dead_statements (tree dead_param,
    1205              :                                                   vec<tree> *debugstack)
    1206              : {
    1207              :   /* Current IPA analyses which remove unused parameters never remove a
    1208              :      non-gimple register ones which have any use except as parameters in other
    1209              :      calls, so we can safely leave them as they are.  */
    1210       116769 :   tree parm_ddef = get_ddef_if_exists_and_is_used (dead_param);
    1211       116769 :   if (!parm_ddef)
    1212        80482 :     return;
    1213              : 
    1214        41690 :   auto_vec<tree, 4> stack;
    1215        41690 :   hash_set<tree> used_in_debug;
    1216        41690 :   m_dead_ssas.add (parm_ddef);
    1217        41690 :   stack.safe_push (parm_ddef);
    1218       131750 :   while (!stack.is_empty ())
    1219              :     {
    1220        48370 :       imm_use_iterator imm_iter;
    1221        48370 :       use_operand_p use_p;
    1222        48370 :       tree t = stack.pop ();
    1223              : 
    1224        48370 :       insert_decl_map (m_id, t, error_mark_node);
    1225       173887 :       FOR_EACH_IMM_USE_FAST (use_p, imm_iter, t)
    1226              :         {
    1227       125517 :           gimple *stmt = USE_STMT (use_p);
    1228              : 
    1229              :           /* Calls containing dead arguments cannot be deleted,
    1230              :              modify_call_stmt will instead remove just the argument later on.
    1231              :              If isra_track_scalar_value_uses in ipa-sra.cc is extended to look
    1232              :              through const functions, we will need to do so here too.  */
    1233       125517 :           if (is_gimple_call (stmt)
    1234       125517 :               || (m_id->blocks_to_copy
    1235        79068 :                   && !bitmap_bit_p (m_id->blocks_to_copy,
    1236        79068 :                                     gimple_bb (stmt)->index)))
    1237       101784 :             continue;
    1238              : 
    1239        23733 :           if (is_gimple_debug (stmt))
    1240              :             {
    1241        16554 :               m_dead_stmts.add (stmt);
    1242        16554 :               gcc_assert (gimple_debug_bind_p (stmt));
    1243        16554 :               if (!used_in_debug.contains (t))
    1244              :                 {
    1245         8927 :                   used_in_debug.add (t);
    1246         8927 :                   debugstack->safe_push (t);
    1247              :                 }
    1248              :             }
    1249         7179 :           else if (gimple_code (stmt) == GIMPLE_PHI)
    1250              :             {
    1251          404 :               gphi *phi = as_a <gphi *> (stmt);
    1252          404 :               int ix = PHI_ARG_INDEX_FROM_USE (use_p);
    1253              : 
    1254          404 :               if (!m_id->blocks_to_copy
    1255          446 :                   || bitmap_bit_p (m_id->blocks_to_copy,
    1256           42 :                                    gimple_phi_arg_edge (phi, ix)->src->index))
    1257              :                 {
    1258          362 :                   m_dead_stmts.add (phi);
    1259          362 :                   tree res = gimple_phi_result (phi);
    1260          362 :                   if (!m_dead_ssas.add (res))
    1261          246 :                     stack.safe_push (res);
    1262              :                 }
    1263              :             }
    1264         6775 :           else if (is_gimple_assign (stmt))
    1265              :             {
    1266         6692 :               m_dead_stmts.add (stmt);
    1267         6692 :               if (!gimple_clobber_p (stmt))
    1268              :                 {
    1269         6676 :                   tree lhs = gimple_assign_lhs (stmt);
    1270         6676 :                   gcc_assert (TREE_CODE (lhs) == SSA_NAME);
    1271         6676 :                   if (!m_dead_ssas.add (lhs))
    1272         6434 :                     stack.safe_push (lhs);
    1273              :                 }
    1274              :             }
    1275           83 :           else if (gimple_code (stmt) == GIMPLE_RETURN)
    1276           83 :             gcc_assert (m_adjustments && m_adjustments->m_skip_return);
    1277              :           else
    1278              :             /* IPA-SRA does not analyze other types of statements.  */
    1279            0 :             gcc_unreachable ();
    1280        48370 :         }
    1281              :     }
    1282              : 
    1283        41690 :   if (!MAY_HAVE_DEBUG_STMTS)
    1284              :     {
    1285         5403 :       gcc_assert (debugstack->is_empty ());
    1286         5403 :       return;
    1287              :     }
    1288              : 
    1289        36287 :   tree dp_ddecl = build_debug_expr_decl (TREE_TYPE (dead_param));
    1290              :   /* FIXME: Is setting the mode really necessary? */
    1291        36287 :   SET_DECL_MODE (dp_ddecl, DECL_MODE (dead_param));
    1292        36287 :   m_dead_ssa_debug_equiv.put (parm_ddef, dp_ddecl);
    1293        41690 : }
    1294              : 
    1295              : /* Put all clobbers of of dereference of default definition of PARAM into
    1296              :    m_dead_stmts.  If there are returns among uses of the default definition of
    1297              :    PARAM, verify they will be stripped off the return value.  */
    1298              : 
    1299              : void
    1300        49534 : ipa_param_body_adjustments::mark_clobbers_dead (tree param)
    1301              : {
    1302        49534 :   if (!is_gimple_reg (param))
    1303         6929 :     return;
    1304        42605 :   tree ddef = get_ddef_if_exists_and_is_used (param);
    1305        42605 :   if (!ddef)
    1306              :     return;
    1307              : 
    1308        42605 :  imm_use_iterator imm_iter;
    1309        42605 :  use_operand_p use_p;
    1310       188516 :  FOR_EACH_IMM_USE_FAST (use_p, imm_iter, ddef)
    1311              :    {
    1312       145911 :      gimple *stmt = USE_STMT (use_p);
    1313       145911 :      if (gimple_clobber_p (stmt))
    1314          382 :        m_dead_stmts.add (stmt);
    1315       145529 :      else if (gimple_code (stmt) == GIMPLE_RETURN)
    1316          176 :        gcc_assert (m_adjustments && m_adjustments->m_skip_return);
    1317        42605 :    }
    1318              : }
    1319              : 
    1320              : /* Callback to walk_tree.  If REMAP is an SSA_NAME that is present in hash_map
    1321              :    passed in DATA, replace it with unshared version of what it was mapped to.
    1322              :    If an SSA argument would be remapped to NULL, the whole operation needs to
    1323              :    abort which is signaled by returning error_mark_node.  */
    1324              : 
    1325              : static tree
    1326        29484 : replace_with_mapped_expr (tree *remap, int *walk_subtrees, void *data)
    1327              : {
    1328        29484 :   if (TYPE_P (*remap))
    1329              :     {
    1330            0 :       *walk_subtrees = 0;
    1331            0 :       return 0;
    1332              :     }
    1333        29484 :   if (TREE_CODE (*remap) != SSA_NAME)
    1334              :     return 0;
    1335              : 
    1336         5031 :   *walk_subtrees = 0;
    1337              : 
    1338         5031 :   hash_map<tree, tree> *equivs = (hash_map<tree, tree> *) data;
    1339         5031 :   if (tree *p = equivs->get (*remap))
    1340              :     {
    1341         4978 :       if (!*p)
    1342            1 :         return error_mark_node;
    1343         4977 :       *remap = unshare_expr (*p);
    1344              :     }
    1345              :   return 0;
    1346              : }
    1347              : 
    1348              : /* Replace all occurrences of SSAs in m_dead_ssa_debug_equiv in t with what they
    1349              :    are mapped to.  */
    1350              : 
    1351              : void
    1352        19894 : ipa_param_body_adjustments::remap_with_debug_expressions (tree *t)
    1353              : {
    1354              :   /* If *t is an SSA_NAME which should have its debug statements reset, it is
    1355              :      mapped to NULL in the hash_map.
    1356              : 
    1357              :      It is perhaps simpler to handle the SSA_NAME cases directly and only
    1358              :      invoke walk_tree on more complex expressions.  When
    1359              :      remap_with_debug_expressions is called from tree-inline.cc, a to-be-reset
    1360              :      SSA_NAME can be an operand to such expressions and the entire debug
    1361              :      variable we are remapping should be reset.  This is signaled by walk_tree
    1362              :      returning error_mark_node and done by setting *t to NULL.  */
    1363        19894 :   if (TREE_CODE (*t) == SSA_NAME)
    1364              :     {
    1365        14974 :       if (tree *p = m_dead_ssa_debug_equiv.get (*t))
    1366        14974 :         *t = *p;
    1367              :     }
    1368         4920 :   else if (walk_tree (t, replace_with_mapped_expr,
    1369         4920 :                       &m_dead_ssa_debug_equiv, NULL) == error_mark_node)
    1370            1 :     *t = NULL_TREE;
    1371        19894 : }
    1372              : 
    1373              : /* For an SSA_NAME DEAD_SSA which is about to be DCEd because it is based on a
    1374              :    useless parameter, prepare an expression that should represent it in
    1375              :    debug_binds in the cloned function and add a mapping from DEAD_SSA to
    1376              :    m_dead_ssa_debug_equiv.  That mapping is to NULL when the associated
    1377              :    debug_statement has to be reset instead.  In such case return false,
    1378              :    ottherwise return true.  If DEAD_SSA comes from a basic block which is not
    1379              :    about to be copied, ignore it and return true.  */
    1380              : 
    1381              : bool
    1382        12559 : ipa_param_body_adjustments::prepare_debug_expressions (tree dead_ssa)
    1383              : {
    1384        12559 :   gcc_checking_assert (m_dead_ssas.contains (dead_ssa));
    1385        12559 :   if (tree *d = m_dead_ssa_debug_equiv.get (dead_ssa))
    1386         8868 :     return (*d != NULL_TREE);
    1387              : 
    1388         3691 :   gcc_assert (!SSA_NAME_IS_DEFAULT_DEF (dead_ssa));
    1389         3691 :   gimple *def = SSA_NAME_DEF_STMT (dead_ssa);
    1390         3691 :   if (m_id->blocks_to_copy
    1391         3691 :       && !bitmap_bit_p (m_id->blocks_to_copy, gimple_bb (def)->index))
    1392              :     return true;
    1393              : 
    1394         3691 :   if (gimple_code (def) == GIMPLE_PHI)
    1395              :     {
    1396              :       /* In theory, we could ignore all SSAs coming from BBs not in
    1397              :          m_id->blocks_to_copy but at the time of the writing this code that
    1398              :          should never really be the case because only fnsplit uses that bitmap,
    1399              :          so don't bother.  */
    1400          169 :       tree value = degenerate_phi_result (as_a <gphi *> (def));
    1401          169 :       if (!value
    1402          169 :           || (m_dead_ssas.contains (value)
    1403            0 :               && !prepare_debug_expressions (value)))
    1404              :         {
    1405          169 :           m_dead_ssa_debug_equiv.put (dead_ssa, NULL_TREE);
    1406          169 :           return false;
    1407              :         }
    1408              : 
    1409            0 :       gcc_assert (TREE_CODE (value) == SSA_NAME);
    1410            0 :       tree d = *m_dead_ssa_debug_equiv.get (value);
    1411            0 :       m_dead_ssa_debug_equiv.put (dead_ssa, d);
    1412            0 :       return true;
    1413              :     }
    1414              : 
    1415         3522 :   bool lost = false;
    1416         3522 :   use_operand_p use_p;
    1417         3522 :   ssa_op_iter oi;
    1418         7049 :   FOR_EACH_PHI_OR_STMT_USE (use_p, def, oi, SSA_OP_USE)
    1419              :     {
    1420         3709 :       tree use = USE_FROM_PTR (use_p);
    1421         3709 :       if (m_dead_ssas.contains (use)
    1422         3709 :           && !prepare_debug_expressions (use))
    1423              :         {
    1424          182 :           lost = true;
    1425          182 :           break;
    1426              :         }
    1427              :     }
    1428              : 
    1429         3522 :   if (lost)
    1430              :     {
    1431          182 :       m_dead_ssa_debug_equiv.put (dead_ssa, NULL_TREE);
    1432          182 :       return false;
    1433              :     }
    1434              : 
    1435         3340 :   if (is_gimple_assign (def))
    1436              :     {
    1437         3340 :       gcc_assert (!gimple_clobber_p (def));
    1438         3340 :       if (gimple_assign_copy_p (def)
    1439         3340 :           && TREE_CODE (gimple_assign_rhs1 (def)) == SSA_NAME)
    1440              :         {
    1441            0 :           tree d = *m_dead_ssa_debug_equiv.get (gimple_assign_rhs1 (def));
    1442            0 :           gcc_assert (d);
    1443            0 :           m_dead_ssa_debug_equiv.put (dead_ssa, d);
    1444            0 :           return true;
    1445              :         }
    1446              : 
    1447         3340 :       tree val
    1448         3340 :         = unshare_expr_without_location (gimple_assign_rhs_to_tree (def));
    1449         3340 :       remap_with_debug_expressions (&val);
    1450              : 
    1451         3340 :       tree vexpr = build_debug_expr_decl (TREE_TYPE (val));
    1452         3340 :       m_dead_stmt_debug_equiv.put (def, val);
    1453         3340 :       m_dead_ssa_debug_equiv.put (dead_ssa, vexpr);
    1454         3340 :       return true;
    1455              :     }
    1456              :   else
    1457            0 :     gcc_unreachable ();
    1458              : }
    1459              : 
    1460              : /* Common initialization performed by all ipa_param_body_adjustments
    1461              :    constructors.  OLD_FNDECL is the declaration we take original arguments
    1462              :    from, (it may be the same as M_FNDECL).  VARS, if non-NULL, is a pointer to
    1463              :    a chained list of new local variables.  TREE_MAP is the IPA-CP produced
    1464              :    mapping of trees to constants.
    1465              : 
    1466              :    The function is rather long but it really onlu initializes all data members
    1467              :    of the class.  It creates new param DECLs, finds their new types,   */
    1468              : 
    1469              : void
    1470       171192 : ipa_param_body_adjustments::common_initialization (tree old_fndecl,
    1471              :                                                    tree *vars,
    1472              :                                                    vec<ipa_replace_map *,
    1473              :                                                        va_gc> *tree_map)
    1474              : {
    1475       171192 :   push_function_arg_decls (&m_oparms, old_fndecl);
    1476       171192 :   auto_vec<tree,16> otypes;
    1477       171192 :   if (TYPE_ARG_TYPES (TREE_TYPE (old_fndecl)) != NULL_TREE)
    1478       171052 :     push_function_arg_types (&otypes, TREE_TYPE (old_fndecl));
    1479              :   else
    1480              :     {
    1481          140 :       auto_vec<tree,16> oparms;
    1482          140 :       push_function_arg_decls (&oparms, old_fndecl);
    1483          140 :       unsigned ocount = oparms.length ();
    1484          140 :       otypes.reserve_exact (ocount);
    1485          447 :       for (unsigned i = 0; i < ocount; i++)
    1486          167 :         otypes.quick_push (TREE_TYPE (oparms[i]));
    1487          140 :     }
    1488       171192 :   fill_vector_of_new_param_types (&m_new_types, &otypes, m_adj_params, true);
    1489              : 
    1490       171192 :   auto_vec<bool, 16> kept;
    1491       342384 :   kept.reserve_exact (m_oparms.length ());
    1492       342384 :   kept.quick_grow_cleared (m_oparms.length ());
    1493       171192 :   auto_vec<bool, 16> split;
    1494       171192 :   split.reserve_exact (m_oparms.length ());
    1495       342384 :   split.quick_grow_cleared (m_oparms.length ());
    1496              : 
    1497       171192 :   unsigned adj_len = vec_safe_length (m_adj_params);
    1498       171192 :   m_method2func = ((TREE_CODE (TREE_TYPE (m_fndecl)) == METHOD_TYPE)
    1499       171192 :                    && (adj_len == 0
    1500        41544 :                        || (*m_adj_params)[0].op != IPA_PARAM_OP_COPY
    1501        41424 :                        || (*m_adj_params)[0].base_index != 0));
    1502              : 
    1503              :   /* The main job of the this function is to go over the vector of adjusted
    1504              :      parameters and create declarations or find corresponding old ones and push
    1505              :      them to m_new_decls.  For IPA-SRA replacements it also creates
    1506              :      corresponding m_id->dst_node->clone.performed_splits entries.  */
    1507              : 
    1508       171192 :   m_new_decls.reserve_exact (adj_len);
    1509       699135 :   for (unsigned i = 0; i < adj_len ; i++)
    1510              :     {
    1511       356751 :       ipa_adjusted_param *apm = &(*m_adj_params)[i];
    1512       356751 :       unsigned prev_index = apm->prev_clone_index;
    1513       356751 :       tree new_parm;
    1514       356751 :       if (apm->op == IPA_PARAM_OP_COPY
    1515        89645 :           || apm->prev_clone_adjustment)
    1516              :         {
    1517       267106 :           kept[prev_index] = true;
    1518       267106 :           new_parm = carry_over_param (m_oparms[prev_index]);
    1519       267106 :           m_new_decls.quick_push (new_parm);
    1520              :         }
    1521        89645 :       else if (apm->op == IPA_PARAM_OP_NEW
    1522        89645 :                || apm->op == IPA_PARAM_OP_SPLIT)
    1523              :         {
    1524        89645 :           tree new_type = m_new_types[i];
    1525        89645 :           gcc_checking_assert (new_type);
    1526        89645 :           new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
    1527              :                                  new_type);
    1528        89645 :           const char *prefix = ipa_param_prefixes[apm->param_prefix_index];
    1529        89645 :           DECL_NAME (new_parm) = create_tmp_var_name (prefix);
    1530        89645 :           DECL_ARTIFICIAL (new_parm) = 1;
    1531        89645 :           DECL_ARG_TYPE (new_parm) = new_type;
    1532        89645 :           DECL_CONTEXT (new_parm) = m_fndecl;
    1533        89645 :           TREE_USED (new_parm) = 1;
    1534        89645 :           DECL_IGNORED_P (new_parm) = 1;
    1535        89645 :           layout_decl (new_parm, 0);
    1536        89645 :           m_new_decls.quick_push (new_parm);
    1537              : 
    1538        89645 :           if (apm->op == IPA_PARAM_OP_SPLIT)
    1539              :             {
    1540        80729 :               split[prev_index] = true;
    1541        80729 :               register_replacement (apm, new_parm);
    1542              :             }
    1543              :         }
    1544              :       else
    1545            0 :         gcc_unreachable ();
    1546              :     }
    1547              : 
    1548       171192 :   auto_vec <int, 16> index_mapping;
    1549       171192 :   bool need_remap = false;
    1550       171192 :   if (m_id)
    1551              :     {
    1552       166656 :       clone_info *cinfo = clone_info::get (m_id->src_node);
    1553       166656 :       if (cinfo && cinfo->param_adjustments)
    1554              :         {
    1555         2496 :           cinfo->param_adjustments->get_updated_indices (&index_mapping);
    1556         2496 :           need_remap = true;
    1557              :         }
    1558              : 
    1559       166656 :       if (ipcp_transformation *ipcp_ts
    1560       166656 :           = ipcp_get_transformation_summary (m_id->src_node))
    1561              :         {
    1562        41676 :           for (const ipa_argagg_value &av : ipcp_ts->m_agg_values)
    1563              :             {
    1564        15907 :               int parm_num = av.index;
    1565              : 
    1566        15907 :               if (need_remap)
    1567              :                 {
    1568              :                   /* FIXME: We cannot handle the situation when IPA-CP
    1569              :                      identified that a parameter is a pointer to a global
    1570              :                      variable and at the same time the variable has some known
    1571              :                      constant contents (PR 107640).  The best place to make
    1572              :                      sure we don't drop such constants on the floor probably is
    1573              :                      not here, but we have to make sure that it does not
    1574              :                      confuse the remapping.  */
    1575         6707 :                   if (parm_num >= (int) index_mapping.length ())
    1576           23 :                     continue;
    1577         6684 :                   parm_num = index_mapping[parm_num];
    1578         6684 :                   if (parm_num < 0)
    1579           38 :                     continue;
    1580              :                 }
    1581              : 
    1582        15846 :               if (!kept[parm_num])
    1583              :                 {
    1584              :                   /* IPA-CP has detected an aggregate constant in a parameter
    1585              :                      that will not be kept, which means that IPA-SRA would have
    1586              :                      split it if there wasn't a constant.  Because we are about
    1587              :                      to remove the original, this is the last chance where we
    1588              :                      can substitute the uses with a constant (for values passed
    1589              :                      by reference) or do the split but initialize the
    1590              :                      replacement with a constant (for split aggregates passed
    1591              :                      by value).  */
    1592              : 
    1593        12576 :                   if (split[parm_num])
    1594              :                     {
    1595              :                       /* We must be careful not to add a duplicate
    1596              :                          replacement. */
    1597        11323 :                       sort_replacements ();
    1598        11323 :                       ipa_param_body_replacement *pbr
    1599        11323 :                         = lookup_replacement_1 (m_oparms[parm_num],
    1600        11323 :                                                 av.unit_offset);
    1601        11323 :                       if (pbr)
    1602              :                         {
    1603              :                           /* Otherwise IPA-SRA should have bailed out.  */
    1604            3 :                           gcc_assert (AGGREGATE_TYPE_P (TREE_TYPE (pbr->repl)));
    1605            3 :                           continue;
    1606              :                         }
    1607              :                     }
    1608              : 
    1609        12573 :                   tree repl;
    1610        12573 :                   if (av.by_ref)
    1611        11418 :                     repl = av.value;
    1612              :                   else
    1613              :                     {
    1614         1155 :                       repl = create_tmp_var (TREE_TYPE (av.value),
    1615              :                                              "removed_ipa_cp");
    1616         1155 :                       gimple *init_stmt = gimple_build_assign (repl, av.value);
    1617         1155 :                       m_split_agg_csts_inits.safe_push (init_stmt);
    1618              :                     }
    1619        12573 :                   register_replacement (m_oparms[parm_num], av.unit_offset,
    1620              :                                         repl);
    1621        12573 :                   split[parm_num] = true;
    1622              :                 }
    1623              :             }
    1624              :         }
    1625              :     }
    1626       171192 :   sort_replacements ();
    1627              : 
    1628       171192 :   if (tree_map)
    1629              :     {
    1630              :       /* Do not treat parameters which were replaced with a constant as
    1631              :          completely vanished.  */
    1632        34220 :       for (unsigned i = 0; i < tree_map->length (); i++)
    1633              :         {
    1634        21033 :           int parm_num = (*tree_map)[i]->parm_num;
    1635        21033 :           gcc_assert (parm_num >= 0);
    1636        21033 :           if (need_remap)
    1637            0 :             parm_num = index_mapping[parm_num];
    1638        21033 :           kept[parm_num] = true;
    1639              :         }
    1640              :     }
    1641              : 
    1642              :   /* As part of body modifications, we will also have to replace remaining uses
    1643              :      of remaining uses of removed PARM_DECLs (which do not however use the
    1644              :      initial value) with their VAR_DECL copies.
    1645              : 
    1646              :      We do this differently with and without m_id.  With m_id, we rely on its
    1647              :      mapping and create a replacement straight away.  Without it, we have our
    1648              :      own mechanism for which we have to populate m_removed_decls vector.  Just
    1649              :      don't mix them, that is why you should not call
    1650              :      replace_removed_params_ssa_names or perform_cfun_body_modifications when
    1651              :      you construct with ID not equal to NULL.  */
    1652              : 
    1653       171192 :   auto_vec<tree, 8> ssas_to_process_debug;
    1654       171192 :   unsigned op_len = m_oparms.length ();
    1655       630160 :   for (unsigned i = 0; i < op_len; i++)
    1656       458968 :     if (!kept[i])
    1657              :       {
    1658       170829 :         if (m_id)
    1659              :           {
    1660       166303 :             gcc_assert (!m_id->decl_map->get (m_oparms[i]));
    1661       166303 :             tree var = copy_decl_to_var (m_oparms[i], m_id);
    1662       166303 :             insert_decl_map (m_id, m_oparms[i], var);
    1663              :             /* Declare this new variable.  */
    1664       166303 :             DECL_CHAIN (var) = *vars;
    1665       166303 :             *vars = var;
    1666              : 
    1667              :             /* If this is not a split but a real removal, init hash sets
    1668              :                that will guide what not to copy to the new body.  */
    1669       166303 :             if (!split[i])
    1670       116769 :               mark_dead_statements (m_oparms[i], &ssas_to_process_debug);
    1671              :             else
    1672        49534 :               mark_clobbers_dead (m_oparms[i]);
    1673        16525 :             if (MAY_HAVE_DEBUG_STMTS
    1674       166303 :                 && is_gimple_reg (m_oparms[i]))
    1675       112179 :               m_reset_debug_decls.safe_push (m_oparms[i]);
    1676              :           }
    1677              :         else
    1678              :           {
    1679         4526 :             m_removed_decls.safe_push (m_oparms[i]);
    1680         9052 :             m_removed_map.put (m_oparms[i], m_removed_decls.length () - 1);
    1681         4262 :             if (MAY_HAVE_DEBUG_STMTS
    1682          264 :                 && !kept[i]
    1683         4790 :                 && is_gimple_reg (m_oparms[i]))
    1684          224 :               m_reset_debug_decls.safe_push (m_oparms[i]);
    1685              :           }
    1686              :       }
    1687              : 
    1688       351311 :   while (!ssas_to_process_debug.is_empty ())
    1689         8927 :     prepare_debug_expressions (ssas_to_process_debug.pop ());
    1690       171192 : }
    1691              : 
    1692              : /* Constructor of ipa_param_body_adjustments from a simple list of
    1693              :    modifications to parameters listed in ADJ_PARAMS which will prepare ground
    1694              :    for modification of parameters of fndecl.  Return value of the function will
    1695              :    not be removed and the object will assume it does not run as a part of
    1696              :    tree-function_versioning.  */
    1697              : 
    1698         4521 : ipa_param_body_adjustments
    1699              : ::ipa_param_body_adjustments (vec<ipa_adjusted_param, va_gc> *adj_params,
    1700              :                               tree fndecl)
    1701         4521 :   : m_adj_params (adj_params), m_adjustments (NULL), m_reset_debug_decls (),
    1702         4521 :     m_dead_stmts (), m_dead_ssas (), m_dead_ssa_debug_equiv (),
    1703         4521 :     m_dead_stmt_debug_equiv (), m_fndecl (fndecl), m_id (NULL), m_oparms (),
    1704         4521 :     m_new_decls (), m_new_types (), m_replacements (),
    1705         4521 :     m_split_agg_csts_inits (), m_removed_decls (), m_removed_map (),
    1706         4521 :     m_method2func (false), m_sorted_replacements_p (true)
    1707              : {
    1708         4521 :   common_initialization (fndecl, NULL, NULL);
    1709         4521 : }
    1710              : 
    1711              : /* Constructor of ipa_param_body_adjustments from ipa_param_adjustments in
    1712              :    ADJUSTMENTS which will prepare ground for modification of parameters of
    1713              :    fndecl.  The object will assume it does not run as a part of
    1714              :    tree-function_versioning.  */
    1715              : 
    1716           15 : ipa_param_body_adjustments
    1717              : ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
    1718              :                               tree fndecl)
    1719           15 :   : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
    1720           15 :     m_reset_debug_decls (), m_dead_stmts (), m_dead_ssas (),
    1721           15 :     m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (), m_fndecl (fndecl),
    1722           15 :     m_id (NULL), m_oparms (), m_new_decls (), m_new_types (), m_replacements (),
    1723           15 :     m_split_agg_csts_inits (), m_removed_decls (), m_removed_map (),
    1724           15 :     m_method2func (false), m_sorted_replacements_p (true)
    1725              : {
    1726           15 :   common_initialization (fndecl, NULL, NULL);
    1727           15 : }
    1728              : 
    1729              : /* Constructor of ipa_param_body_adjustments which sets it up as a part of
    1730              :    running tree_function_versioning.  Planned modifications to the function are
    1731              :    in ADJUSTMENTS.  FNDECL designates the new function clone which is being
    1732              :    modified.  OLD_FNDECL is the function of which FNDECL is a clone (and which
    1733              :    at the time of invocation still share DECL_ARGUMENTS).  ID is the
    1734              :    copy_body_data structure driving the whole body copying process.  VARS is a
    1735              :    pointer to the head of the list of new local variables, TREE_MAP is the map
    1736              :    that drives tree substitution in the cloning process.  */
    1737              : 
    1738       166656 : ipa_param_body_adjustments
    1739              : ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
    1740              :                               tree fndecl, tree old_fndecl,
    1741              :                               copy_body_data *id, tree *vars,
    1742              :                               vec<ipa_replace_map *, va_gc> *tree_map)
    1743       166656 :   : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
    1744       166656 :     m_reset_debug_decls (), m_dead_stmts (), m_dead_ssas (),
    1745       166656 :     m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (), m_fndecl (fndecl),
    1746       166656 :     m_id (id), m_oparms (), m_new_decls (), m_new_types (), m_replacements (),
    1747       166656 :     m_split_agg_csts_inits (), m_removed_decls (), m_removed_map (),
    1748       166656 :     m_method2func (false), m_sorted_replacements_p (true)
    1749              : {
    1750       166656 :   common_initialization (old_fndecl, vars, tree_map);
    1751       166656 : }
    1752              : 
    1753              : /* Chain new param decls up and return them.  */
    1754              : 
    1755              : tree
    1756       171192 : ipa_param_body_adjustments::get_new_param_chain ()
    1757              : {
    1758       171192 :   tree result;
    1759       171192 :   tree *link = &result;
    1760              : 
    1761       171192 :   unsigned len = vec_safe_length (m_adj_params);
    1762       527943 :   for (unsigned i = 0; i < len; i++)
    1763              :     {
    1764       356751 :       tree new_decl = m_new_decls[i];
    1765       356751 :       *link = new_decl;
    1766       356751 :       link = &DECL_CHAIN (new_decl);
    1767              :     }
    1768       171192 :   *link = NULL_TREE;
    1769       171192 :   return result;
    1770              : }
    1771              : 
    1772              : /* Modify the function parameters FNDECL and its type according to the plan in
    1773              :    ADJUSTMENTS.  This function needs to be called when the decl has not already
    1774              :    been processed with ipa_param_adjustments::adjust_decl, otherwise just
    1775              :    setting DECL_ARGUMENTS to whatever get_new_param_chain will do is enough.  */
    1776              : 
    1777              : void
    1778         4536 : ipa_param_body_adjustments::modify_formal_parameters ()
    1779              : {
    1780         4536 :   tree orig_type = TREE_TYPE (m_fndecl);
    1781         4536 :   DECL_ARGUMENTS (m_fndecl) = get_new_param_chain ();
    1782              : 
    1783              :   /* When signature changes, we need to clear builtin info.  */
    1784         4536 :   if (fndecl_built_in_p (m_fndecl))
    1785            0 :     set_decl_built_in_function (m_fndecl, NOT_BUILT_IN, 0);
    1786              : 
    1787         4536 :   bool modified = false;
    1788         4536 :   size_t index = 0;
    1789         4536 :   if (m_adj_params)
    1790         4160 :     for (tree t = TYPE_ARG_TYPES (orig_type);
    1791        10645 :          t && !modified;
    1792         6485 :          t = TREE_CHAIN (t), index++)
    1793         6485 :       if (index >= m_adj_params->length ()
    1794         5912 :           || (*m_adj_params)[index].op != IPA_PARAM_OP_COPY
    1795         8850 :           || (*m_adj_params)[index].base_index != index)
    1796              :         modified = true;
    1797              : 
    1798              :   /* At this point, removing return value is only implemented when going
    1799              :      through tree_function_versioning, not when modifying function body
    1800              :      directly.  */
    1801         4536 :   gcc_assert (!m_adjustments || !m_adjustments->m_skip_return);
    1802         4536 :   tree new_type = build_adjusted_function_type (orig_type, &m_new_types,
    1803              :                                                 m_method2func, false, modified);
    1804              : 
    1805         4536 :   TREE_TYPE (m_fndecl) = new_type;
    1806         4536 :   DECL_VIRTUAL_P (m_fndecl) = 0;
    1807         4536 :   DECL_LANG_SPECIFIC (m_fndecl) = NULL;
    1808         4536 :   if (m_method2func)
    1809          120 :     DECL_VINDEX (m_fndecl) = NULL_TREE;
    1810         4536 : }
    1811              : 
    1812              : /* Given BASE and UNIT_OFFSET, find the corresponding record among replacement
    1813              :    structures.  */
    1814              : 
    1815              : ipa_param_body_replacement *
    1816       171363 : ipa_param_body_adjustments::lookup_replacement_1 (tree base,
    1817              :                                                   unsigned unit_offset)
    1818              : {
    1819       171363 :   gcc_assert (m_sorted_replacements_p);
    1820       171363 :   ipa_param_body_replacement key;
    1821       171363 :   key.base = base;
    1822       171363 :   key.unit_offset = unit_offset;
    1823       171363 :   ipa_param_body_replacement *res
    1824       342726 :     = std::lower_bound (m_replacements.begin (), m_replacements.end (), key,
    1825       335538 :                         [] (const ipa_param_body_replacement &elt,
    1826              :                             const ipa_param_body_replacement &val)
    1827              :                         {
    1828       335538 :                           return (compare_param_body_replacement (&elt, &val)
    1829              :                                   < 0);
    1830              :                         });
    1831              : 
    1832       171363 :   if (res == m_replacements.end ()
    1833       136384 :       || res->base != base
    1834       266220 :       || res->unit_offset != unit_offset)
    1835        77964 :     return NULL;
    1836              :   return res;
    1837              : }
    1838              : 
    1839              : /* Find the first replacement for BASE among m_replacements and return pointer
    1840              :    to it, or NULL if there is none.  */
    1841              : 
    1842              : ipa_param_body_replacement *
    1843        27489 : ipa_param_body_adjustments::lookup_first_base_replacement (tree base)
    1844              : {
    1845        27489 :   gcc_assert (m_sorted_replacements_p);
    1846        27489 :   ipa_param_body_replacement key;
    1847        27489 :   key.base = base;
    1848        27489 :   ipa_param_body_replacement *res
    1849        54978 :     = std::lower_bound (m_replacements.begin (), m_replacements.end (), key,
    1850        40628 :                         [] (const ipa_param_body_replacement &elt,
    1851              :                             const ipa_param_body_replacement &val)
    1852              :                         {
    1853        40628 :                           if (DECL_UID (elt.base) < DECL_UID (val.base))
    1854        14737 :                             return true;
    1855              :                           return false;
    1856              :                         });
    1857              : 
    1858        27489 :   if (res == m_replacements.end ()
    1859        27489 :       || res->base != base)
    1860        22200 :     return NULL;
    1861              :   return res;
    1862              : }
    1863              : 
    1864              : /* Given BASE and UNIT_OFFSET, find the corresponding replacement expression
    1865              :    and return it, assuming it is known it does not hold value by reference or
    1866              :    in reverse storage order.  */
    1867              : 
    1868              : tree
    1869         4077 : ipa_param_body_adjustments::lookup_replacement (tree base, unsigned unit_offset)
    1870              : {
    1871         4077 :   ipa_param_body_replacement *pbr = lookup_replacement_1 (base, unit_offset);
    1872         4077 :   if (!pbr)
    1873              :     return NULL;
    1874         4077 :   return pbr->repl;
    1875              : }
    1876              : 
    1877              : /* If T is an SSA_NAME, return NULL if it is not a default def or
    1878              :    return its base variable if it is.  If IGNORE_DEFAULT_DEF is true,
    1879              :    the base variable is always returned, regardless if it is a default
    1880              :    def.  Return T if it is not an SSA_NAME.  */
    1881              : 
    1882              : static tree
    1883      1015581 : get_ssa_base_param (tree t, bool ignore_default_def)
    1884              : {
    1885      1015581 :   if (TREE_CODE (t) == SSA_NAME)
    1886              :     {
    1887       614012 :       if (ignore_default_def || SSA_NAME_IS_DEFAULT_DEF (t))
    1888       134451 :         return SSA_NAME_VAR (t);
    1889              :       else
    1890              :         return NULL_TREE;
    1891              :     }
    1892              :   return t;
    1893              : }
    1894              : 
    1895              : /* Given an expression, return the structure describing how it should be
    1896              :    replaced if it accesses a part of a split parameter or NULL otherwise.
    1897              : 
    1898              :    Do not free the result, it will be deallocated when the object is destroyed.
    1899              : 
    1900              :    If IGNORE_DEFAULT_DEF is cleared, consider only SSA_NAMEs of PARM_DECLs
    1901              :    which are default definitions, if set, consider all SSA_NAMEs of
    1902              :    PARM_DECLs.  */
    1903              : 
    1904              : ipa_param_body_replacement *
    1905      1022745 : ipa_param_body_adjustments::get_expr_replacement (tree expr,
    1906              :                                                   bool ignore_default_def)
    1907              : {
    1908      1022745 :   tree base;
    1909      1022745 :   unsigned unit_offset;
    1910              : 
    1911      1022745 :   if (!isra_get_ref_base_and_offset (expr, &base, &unit_offset))
    1912              :     return NULL;
    1913              : 
    1914      1015581 :   base = get_ssa_base_param (base, ignore_default_def);
    1915      1015581 :   if (!base || TREE_CODE (base) != PARM_DECL)
    1916              :     return NULL;
    1917       155000 :   return lookup_replacement_1 (base, unit_offset);
    1918              : }
    1919              : 
    1920              : /* Given OLD_DECL, which is a PARM_DECL of a parameter that is being removed
    1921              :    (which includes it being split or replaced), return a new variable that
    1922              :    should be used for any SSA names that will remain in the function that
    1923              :    previously belonged to OLD_DECL.  */
    1924              : 
    1925              : tree
    1926         7963 : ipa_param_body_adjustments::get_replacement_ssa_base (tree old_decl)
    1927              : {
    1928         7963 :   unsigned *idx = m_removed_map.get (old_decl);
    1929         7963 :   if (!idx)
    1930              :     return NULL;
    1931              : 
    1932         4509 :   tree repl;
    1933         4509 :   if (TREE_CODE (m_removed_decls[*idx]) == PARM_DECL)
    1934              :     {
    1935         4077 :       gcc_assert (m_removed_decls[*idx] == old_decl);
    1936         4077 :       repl = copy_var_decl (old_decl, DECL_NAME (old_decl),
    1937         4077 :                             TREE_TYPE (old_decl));
    1938         4077 :       m_removed_decls[*idx] = repl;
    1939              :     }
    1940              :   else
    1941              :     repl = m_removed_decls[*idx];
    1942              :   return repl;
    1943              : }
    1944              : 
    1945              : /* If OLD_NAME, which is being defined by statement STMT, is an SSA_NAME of a
    1946              :    parameter which is to be removed because its value is not used, create a new
    1947              :    SSA_NAME relating to a replacement VAR_DECL, replace all uses of the
    1948              :    original with it and return it.  If there is no need to re-map, return NULL.
    1949              :    ADJUSTMENTS is a pointer to a vector of IPA-SRA adjustments.  */
    1950              : 
    1951              : tree
    1952            0 : ipa_param_body_adjustments::replace_removed_params_ssa_names (tree old_name,
    1953              :                                                               gimple *stmt)
    1954              : {
    1955            0 :   gcc_assert (!m_id);
    1956            0 :   if (TREE_CODE (old_name) != SSA_NAME)
    1957              :     return NULL;
    1958              : 
    1959            0 :   tree decl = SSA_NAME_VAR (old_name);
    1960            0 :   if (decl == NULL_TREE
    1961            0 :       || TREE_CODE (decl) != PARM_DECL)
    1962              :     return NULL;
    1963              : 
    1964            0 :   tree repl = get_replacement_ssa_base (decl);
    1965            0 :   if (!repl)
    1966              :     return NULL;
    1967              : 
    1968            0 :   tree new_name = make_ssa_name (repl, stmt);
    1969            0 :   SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name)
    1970            0 :     = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (old_name);
    1971              : 
    1972            0 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1973              :     {
    1974            0 :       fprintf (dump_file, "replacing an SSA name of a removed param ");
    1975            0 :       print_generic_expr (dump_file, old_name);
    1976            0 :       fprintf (dump_file, " with ");
    1977            0 :       print_generic_expr (dump_file, new_name);
    1978            0 :       fprintf (dump_file, "\n");
    1979              :     }
    1980              : 
    1981            0 :   replace_uses_by (old_name, new_name);
    1982            0 :   return new_name;
    1983              : }
    1984              : 
    1985              : /* If the expression *EXPR_P should be replaced, do so.  CONVERT specifies
    1986              :    whether the function should care about type incompatibility of the current
    1987              :    and new expressions.  If it is false, the function will leave
    1988              :    incompatibility issues to the caller - note that when the function
    1989              :    encounters a BIT_FIELD_REF, IMAGPART_EXPR or REALPART_EXPR, it will modify
    1990              :    their bases instead of the expressions themselves and then also performs any
    1991              :    necessary conversions.  */
    1992              : 
    1993              : bool
    1994      2703215 : ipa_param_body_adjustments::modify_expression (tree *expr_p, bool convert,
    1995              :                                                gimple_seq *extra_stmts)
    1996              : {
    1997      2703215 :   tree expr = *expr_p;
    1998              : 
    1999      2703215 :   if (m_replacements.is_empty ())
    2000              :     return false;
    2001      1020661 :   if (TREE_CODE (expr) == BIT_FIELD_REF
    2002      1020661 :       || TREE_CODE (expr) == IMAGPART_EXPR
    2003      1018679 :       || TREE_CODE (expr) == REALPART_EXPR)
    2004              :     {
    2005              :       /* For a BIT_FIELD_REF do not bother to VIEW_CONVERT the base,
    2006              :          instead reference the replacement directly.  */
    2007         2492 :       convert = TREE_CODE (expr) != BIT_FIELD_REF;
    2008         2492 :       expr_p = &TREE_OPERAND (expr, 0);
    2009         2492 :       expr = *expr_p;
    2010              :     }
    2011              : 
    2012      1020661 :   ipa_param_body_replacement *pbr = get_expr_replacement (expr, false);
    2013      1020661 :   if (!pbr)
    2014              :     return false;
    2015              : 
    2016        87064 :   tree repl = pbr->repl;
    2017        87064 :   if (dump_file && (dump_flags & TDF_DETAILS))
    2018              :     {
    2019            8 :       fprintf (dump_file, "About to replace expr ");
    2020            8 :       print_generic_expr (dump_file, expr);
    2021            8 :       fprintf (dump_file, " with ");
    2022            8 :       print_generic_expr (dump_file, repl);
    2023            8 :       fprintf (dump_file, "\n");
    2024              :     }
    2025              : 
    2026        87316 :   if (convert && !useless_type_conversion_p (TREE_TYPE (expr),
    2027          252 :                                              TREE_TYPE (repl)))
    2028              :     {
    2029            8 :       gcc_checking_assert (tree_to_shwi (TYPE_SIZE (TREE_TYPE (expr)))
    2030              :                            == tree_to_shwi (TYPE_SIZE (TREE_TYPE (repl))));
    2031            8 :       tree vce = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expr), repl);
    2032            8 :       if (is_gimple_reg (repl)
    2033            8 :           && is_gimple_reg_type (TREE_TYPE (expr)))
    2034              :         {
    2035            0 :           gcc_assert (extra_stmts);
    2036            0 :           vce = force_gimple_operand (vce, extra_stmts, true, NULL_TREE);
    2037              :         }
    2038            8 :       *expr_p = vce;
    2039              :     }
    2040              :   else
    2041        87056 :     *expr_p = repl;
    2042              :   return true;
    2043              : }
    2044              : 
    2045              : /* If the assignment statement STMT contains any expressions that need to
    2046              :    replaced with a different one as noted by ADJUSTMENTS, do so.  Handle any
    2047              :    potential type incompatibilities.  If any conversion sttements have to be
    2048              :    pre-pended to STMT, they will be added to EXTRA_STMTS.  Return true iff the
    2049              :    statement was modified.  */
    2050              : 
    2051              : bool
    2052      2234253 : ipa_param_body_adjustments::modify_assignment (gimple *stmt,
    2053              :                                                gimple_seq *extra_stmts)
    2054              : {
    2055      2234253 :   tree *lhs_p, *rhs_p;
    2056      2234253 :   bool any;
    2057              : 
    2058      2799800 :   if (m_replacements.is_empty () || !gimple_assign_single_p (stmt))
    2059              :     return false;
    2060              : 
    2061       382963 :   rhs_p = gimple_assign_rhs1_ptr (stmt);
    2062       382963 :   lhs_p = gimple_assign_lhs_ptr (stmt);
    2063              : 
    2064       382963 :   any = modify_expression (lhs_p, false);
    2065       382963 :   any |= modify_expression (rhs_p, false, extra_stmts);
    2066       382963 :   if (any
    2067       382963 :       && !useless_type_conversion_p (TREE_TYPE (*lhs_p), TREE_TYPE (*rhs_p)))
    2068              :     {
    2069           60 :       if (TREE_CODE (*rhs_p) == CONSTRUCTOR)
    2070              :         {
    2071              :           /* V_C_Es of constructors can cause trouble (PR 42714).  */
    2072            0 :           if (is_gimple_reg_type (TREE_TYPE (*lhs_p)))
    2073            0 :             *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
    2074              :           else
    2075            0 :             *rhs_p = build_constructor (TREE_TYPE (*lhs_p),
    2076              :                                         NULL);
    2077              :         }
    2078              :       else
    2079              :         {
    2080           60 :           gcc_checking_assert (tree_to_shwi (TYPE_SIZE (TREE_TYPE (*lhs_p)))
    2081              :                               == tree_to_shwi (TYPE_SIZE (TREE_TYPE (*rhs_p))));
    2082           60 :           tree new_rhs = fold_build1_loc (gimple_location (stmt),
    2083           60 :                                           VIEW_CONVERT_EXPR, TREE_TYPE (*lhs_p),
    2084              :                                           *rhs_p);
    2085           60 :           tree tmp = force_gimple_operand (new_rhs, extra_stmts, true,
    2086              :                                            NULL_TREE);
    2087           60 :           gimple_assign_set_rhs1 (stmt, tmp);
    2088              :         }
    2089              :       return true;
    2090              :     }
    2091              : 
    2092              :   return any;
    2093              : }
    2094              : 
    2095              : /* Record information about what modifications to call arguments have already
    2096              :    been done by clone materialization into a summary describing CS.  The
    2097              :    information is stored in NEW_INDEX_MAP, NEW_PT_MAP and NEW_ALWAYS_COPY_DELTA
    2098              :    and correspond to equivalent fields in ipa_edge_modification_info.  Return
    2099              :    the edge summary.  */
    2100              : 
    2101              : static ipa_edge_modification_info *
    2102        17643 : record_argument_state_1 (cgraph_edge *cs, const vec<int> &new_index_map,
    2103              :                          const vec<pass_through_split_map> &new_pt_map,
    2104              :                          int new_always_copy_delta)
    2105              : 
    2106              : {
    2107        17643 :   ipa_edge_modification_info *sum = ipa_edge_modifications->get_create (cs);
    2108              : 
    2109        17643 :   unsigned len = sum->pass_through_map.length ();
    2110        17643 :   for (unsigned i = 0; i < len; i++)
    2111              :     {
    2112            0 :       unsigned oldnew = sum->pass_through_map[i].new_index;
    2113            0 :       sum->pass_through_map[i].new_index = new_index_map[oldnew];
    2114              :     }
    2115              : 
    2116        17643 :   len = sum->index_map.length ();
    2117            0 :   if (len > 0)
    2118              :     {
    2119            0 :       unsigned nptlen = new_pt_map.length ();
    2120            0 :       for (unsigned j = 0; j < nptlen; j++)
    2121              :         {
    2122            0 :           int inverse = -1;
    2123            0 :           for (unsigned i = 0; i < len ; i++)
    2124            0 :             if ((unsigned) sum->index_map[i] == new_pt_map[j].base_index)
    2125              :             {
    2126            0 :               inverse = i;
    2127            0 :               break;
    2128              :             }
    2129            0 :           gcc_assert (inverse >= 0);
    2130            0 :           pass_through_split_map ptm_item;
    2131              : 
    2132            0 :           ptm_item.base_index = inverse;
    2133            0 :           ptm_item.unit_offset = new_pt_map[j].unit_offset;
    2134            0 :           ptm_item.new_index = new_pt_map[j].new_index;
    2135            0 :           sum->pass_through_map.safe_push (ptm_item);
    2136              :         }
    2137              : 
    2138            0 :       for (unsigned i = 0; i < len; i++)
    2139              :         {
    2140            0 :           int idx = sum->index_map[i];
    2141            0 :           if (idx < 0)
    2142            0 :             continue;
    2143            0 :           sum->index_map[i] = new_index_map[idx];
    2144              :         }
    2145              :     }
    2146              :   else
    2147              :     {
    2148        17643 :       sum->pass_through_map.safe_splice (new_pt_map);
    2149        17643 :       sum->index_map.safe_splice (new_index_map);
    2150              :     }
    2151        17643 :   sum->always_copy_delta += new_always_copy_delta;
    2152        17643 :   return sum;
    2153              : }
    2154              : 
    2155              : /* Record information about what modifications to call arguments have already
    2156              :    been done by clone materialization into a summary of an edge describing the
    2157              :    call in this clone and all its clones.  NEW_INDEX_MAP, NEW_PT_MAP and
    2158              :    NEW_ALWAYS_COPY_DELTA have the same meaning as record_argument_state_1.
    2159              : 
    2160              :    In order to associate the info with the right edge summaries, we need
    2161              :    address of the ORIG_STMT in the function from which we are cloning (because
    2162              :    the edges have not yet been re-assigned to the new statement that has just
    2163              :    been created) and ID, the structure governing function body copying.  */
    2164              : 
    2165              : static void
    2166        17643 : record_argument_state (copy_body_data *id, gimple *orig_stmt,
    2167              :                        const vec<int> &new_index_map,
    2168              :                        const vec<pass_through_split_map> &new_pt_map,
    2169              :                        int new_always_copy_delta)
    2170              : {
    2171        17643 :   if (!ipa_edge_modifications)
    2172         4318 :     ipa_edge_modifications = new ipa_edge_modification_sum (symtab);
    2173              : 
    2174        17643 :   struct cgraph_node *this_node = id->dst_node;
    2175        17643 :   ipa_edge_modification_info *first_sum = NULL;
    2176        17643 :   cgraph_edge *cs = this_node->get_edge (orig_stmt);
    2177        17643 :   if (cs)
    2178        17643 :     first_sum = record_argument_state_1 (cs, new_index_map, new_pt_map,
    2179              :                                          new_always_copy_delta);
    2180              :   else
    2181            0 :     gcc_assert (this_node->clones);
    2182              : 
    2183        17643 :   if (!this_node->clones)
    2184              :     return;
    2185        60966 :   for (cgraph_node *subclone = this_node->clones; subclone != this_node;)
    2186              :     {
    2187        53613 :       cs = subclone->get_edge (orig_stmt);
    2188        53613 :       if (cs)
    2189              :         {
    2190        53613 :           if (!first_sum)
    2191            0 :             first_sum = record_argument_state_1 (cs, new_index_map, new_pt_map,
    2192              :                                                  new_always_copy_delta);
    2193              :           else
    2194              :             {
    2195        53613 :               ipa_edge_modification_info *s2
    2196        53613 :                 = ipa_edge_modifications->get_create (cs);
    2197        53613 :               s2->index_map.truncate (0);
    2198        53613 :               s2->index_map.safe_splice (first_sum->index_map);
    2199        53613 :               s2->pass_through_map.truncate (0);
    2200        53613 :               s2->pass_through_map.safe_splice (first_sum->pass_through_map);
    2201        53613 :               s2->always_copy_delta = first_sum->always_copy_delta;
    2202              :             }
    2203              :         }
    2204              :       else
    2205            0 :         gcc_assert (subclone->clones);
    2206              : 
    2207        53613 :       if (subclone->clones)
    2208              :         subclone = subclone->clones;
    2209        49636 :       else if (subclone->next_sibling_clone)
    2210              :         subclone = subclone->next_sibling_clone;
    2211              :       else
    2212              :         {
    2213        20884 :           while (subclone != this_node && !subclone->next_sibling_clone)
    2214        11330 :             subclone = subclone->clone_of;
    2215         9554 :           if (subclone != this_node)
    2216         2201 :             subclone = subclone->next_sibling_clone;
    2217              :         }
    2218              :     }
    2219              : }
    2220              : 
    2221              : /* If the call statement pointed at by STMT_P contains any expressions that
    2222              :    need to replaced with a different one as noted by ADJUSTMENTS, do so.  f the
    2223              :    statement needs to be rebuilt, do so.  Return true if any modifications have
    2224              :    been performed.  ORIG_STMT, if not NULL, is the original statement in the
    2225              :    function that is being cloned from, which at this point can be used to look
    2226              :    up call_graph edges.
    2227              : 
    2228              :    If the method is invoked as a part of IPA clone materialization and if any
    2229              :    parameter split is pass-through, i.e. it applies to the function that is
    2230              :    being modified and also to the callee of the statement, replace the
    2231              :    parameter passed to old callee with all of the replacement a callee might
    2232              :    possibly want and record the performed argument modifications in
    2233              :    ipa_edge_modifications.  Likewise if any argument has already been left out
    2234              :    because it is not necessary.  */
    2235              : 
    2236              : bool
    2237       704533 : ipa_param_body_adjustments::modify_call_stmt (gcall **stmt_p,
    2238              :                                               gimple *orig_stmt)
    2239              : {
    2240       704533 :   auto_vec <unsigned, 4> pass_through_args;
    2241       704533 :   auto_vec <unsigned, 4> pass_through_pbr_indices;
    2242       704533 :   auto_vec <HOST_WIDE_INT, 4> pass_through_offsets;
    2243       704533 :   gcall *stmt = *stmt_p;
    2244       704533 :   unsigned nargs = gimple_call_num_args (stmt);
    2245       704533 :   bool recreate = false;
    2246       704533 :   gcc_assert (m_sorted_replacements_p);
    2247              : 
    2248       704533 :   callback_info *ci = NULL;
    2249       704533 :   if (callback_ifn_assume_call_p (orig_stmt))
    2250              :     {
    2251           12 :       cgraph_edge *cbei = cgraph_node::get (m_fndecl)->get_edge (orig_stmt);
    2252           12 :       if (cbei)
    2253              :         {
    2254           12 :           gcc_checking_assert (cbei->callback);
    2255           12 :           ci = callback_info_sum->get (cbei);
    2256           12 :           gcc_assert (ci);
    2257              :         }
    2258              :     }
    2259              : 
    2260      2317445 :   for (unsigned i = 0; i < gimple_call_num_args (stmt); i++)
    2261              :     {
    2262      1612912 :       tree t = gimple_call_arg (stmt, i);
    2263      1612912 :       gcc_assert (TREE_CODE (t) != BIT_FIELD_REF
    2264              :                   && TREE_CODE (t) != IMAGPART_EXPR
    2265              :                   && TREE_CODE (t) != REALPART_EXPR);
    2266              : 
    2267      1612912 :       if (TREE_CODE (t) == SSA_NAME
    2268      1612912 :           && m_dead_ssas.contains (t))
    2269              :         recreate = true;
    2270              : 
    2271      1612912 :       if (m_replacements.is_empty ())
    2272      1607623 :         continue;
    2273              : 
    2274       192111 :       tree base;
    2275       192111 :       unsigned agg_arg_offset;
    2276       192111 :       if (!isra_get_ref_base_and_offset (t, &base, &agg_arg_offset))
    2277            1 :         continue;
    2278              : 
    2279       192110 :       bool by_ref = false;
    2280       192110 :       if (TREE_CODE (base) == SSA_NAME)
    2281              :         {
    2282        97177 :           if (!SSA_NAME_IS_DEFAULT_DEF (base))
    2283        71851 :             continue;
    2284        25326 :           base = SSA_NAME_VAR (base);
    2285        25326 :           gcc_checking_assert (base);
    2286              :           by_ref = true;
    2287              :         }
    2288       120259 :       if (TREE_CODE (base) != PARM_DECL)
    2289        92770 :         continue;
    2290              : 
    2291        27489 :       ipa_param_body_replacement *first_rep
    2292        27489 :         = lookup_first_base_replacement (base);
    2293        27489 :       if (!first_rep)
    2294        22200 :         continue;
    2295         5289 :       unsigned first_rep_index = first_rep - m_replacements.begin ();
    2296              : 
    2297              :       /* We still have to distinguish between an end-use that we have to
    2298              :          transform now and a pass-through, which happens in the following
    2299              :          two cases.  */
    2300              : 
    2301              :       /* TODO: After we adjust ptr_parm_has_nonarg_uses to also consider
    2302              :          &MEM_REF[ssa_name + offset], we will also have to detect that case
    2303              :          here.    */
    2304              : 
    2305         5289 :       if (TREE_CODE (t) == SSA_NAME
    2306         4314 :           && SSA_NAME_IS_DEFAULT_DEF (t)
    2307         4314 :           && SSA_NAME_VAR (t)
    2308         9603 :           && TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL)
    2309              :         {
    2310              :           /* This must be a by_reference pass-through.  */
    2311         4314 :           recreate = true;
    2312         4314 :           gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
    2313         4314 :           pass_through_args.safe_push (i);
    2314         4314 :           pass_through_pbr_indices.safe_push (first_rep_index);
    2315         4314 :           pass_through_offsets.safe_push (agg_arg_offset);
    2316              :         }
    2317          975 :       else if (!by_ref && AGGREGATE_TYPE_P (TREE_TYPE (t)))
    2318              :         {
    2319              :           /* Currently IPA-SRA guarantees the aggregate access type
    2320              :              exactly matches in this case.  So if it does not match, it is
    2321              :              a pass-through argument that will be sorted out at edge
    2322              :              redirection time.  */
    2323          963 :           ipa_param_body_replacement *pbr
    2324          963 :             = lookup_replacement_1 (base, agg_arg_offset);
    2325              : 
    2326          963 :           if (!pbr
    2327          963 :               || (TYPE_MAIN_VARIANT (TREE_TYPE (t))
    2328          951 :                   != TYPE_MAIN_VARIANT (TREE_TYPE (pbr->repl))))
    2329              :             {
    2330          942 :               recreate = true;
    2331          942 :               pass_through_args.safe_push (i);
    2332          942 :               pass_through_pbr_indices.safe_push (first_rep_index);
    2333          942 :               pass_through_offsets.safe_push (agg_arg_offset);
    2334              :             }
    2335              :         }
    2336              :     }
    2337              : 
    2338       704533 :   if (!recreate)
    2339              :     {
    2340              :       /* No need to rebuild the statement, let's just modify arguments
    2341              :          and the LHS if/as appropriate.  */
    2342              :       bool modified = false;
    2343      2243660 :       for (unsigned i = 0; i < nargs; i++)
    2344              :         {
    2345      1556770 :           tree *t = gimple_call_arg_ptr (stmt, i);
    2346      1556770 :           modified |= modify_expression (t, true);
    2347              :         }
    2348       686890 :       if (gimple_call_lhs (stmt))
    2349              :         {
    2350       262059 :           tree *t = gimple_call_lhs_ptr (stmt);
    2351       262059 :           modified |= modify_expression (t, false);
    2352              :         }
    2353              :       return modified;
    2354              :     }
    2355              : 
    2356        17643 :   auto_vec<int, 16> index_map;
    2357        17643 :   auto_vec<pass_through_split_map, 4> pass_through_map;
    2358        17643 :   auto_vec<tree, 16> vargs;
    2359        17643 :   int always_copy_delta = 0;
    2360        17643 :   unsigned pt_idx = 0;
    2361        17643 :   int new_arg_idx = 0;
    2362        73785 :   for (unsigned i = 0; i < nargs; i++)
    2363              :     {
    2364        56142 :       if (pt_idx < pass_through_args.length ()
    2365        64069 :           && i == pass_through_args[pt_idx])
    2366              :         {
    2367         5256 :           unsigned j = pass_through_pbr_indices[pt_idx];
    2368         5256 :           unsigned agg_arg_offset = pass_through_offsets[pt_idx];
    2369         5256 :           pt_idx++;
    2370         5256 :           always_copy_delta--;
    2371         5256 :           tree base = m_replacements[j].base;
    2372              : 
    2373              :           /* In order to be put into SSA form, we have to push all replacements
    2374              :              pertaining to this parameter as parameters to the call statement.
    2375              :              Edge redirection will need to use edge summary to weed out the
    2376              :              unnecessary ones.  */
    2377         5256 :           unsigned repl_list_len = m_replacements.length ();
    2378        14334 :           for (; j < repl_list_len; j++)
    2379              :             {
    2380         9775 :               if (m_replacements[j].base != base)
    2381              :                 break;
    2382         9078 :               if (m_replacements[j].unit_offset < agg_arg_offset)
    2383            1 :                 continue;
    2384         9077 :               pass_through_split_map pt_map;
    2385              :               /* The base index wouldn't match for callback calls, as the
    2386              :                  analysis is done on their remapped indices.  */
    2387         9077 :               if (ci)
    2388              :                 {
    2389           11 :                   int remapped_idx = ci->inverted_mapping[i];
    2390           11 :                   gcc_checking_assert (remapped_idx != -1);
    2391           11 :                   pt_map.base_index = remapped_idx;
    2392              :                 }
    2393              :               else
    2394         9066 :                 pt_map.base_index = i;
    2395         9077 :               pt_map.unit_offset
    2396         9077 :                 = m_replacements[j].unit_offset - agg_arg_offset;
    2397         9077 :               pt_map.new_index = new_arg_idx;
    2398         9077 :               pass_through_map.safe_push (pt_map);
    2399         9077 :               vargs.safe_push (m_replacements[j].repl);
    2400         9077 :               new_arg_idx++;
    2401         9077 :               always_copy_delta++;
    2402              :             }
    2403         5256 :           index_map.safe_push (-1);
    2404              :         }
    2405              :       else
    2406              :         {
    2407        50886 :           tree t = gimple_call_arg (stmt, i);
    2408        50886 :           if (TREE_CODE (t) == SSA_NAME
    2409        50886 :               && m_dead_ssas.contains (t))
    2410              :             {
    2411        13355 :               always_copy_delta--;
    2412        13355 :               index_map.safe_push (-1);
    2413              :             }
    2414              :           else
    2415              :             {
    2416        37531 :               modify_expression (&t, true);
    2417        37531 :               vargs.safe_push (t);
    2418        37531 :               index_map.safe_push (new_arg_idx);
    2419        37531 :               new_arg_idx++;
    2420              :             }
    2421              :         }
    2422              :     }
    2423              : 
    2424        17643 :   gcall *new_stmt;
    2425        17643 :   if (callback_ifn_assume_call_p (orig_stmt))
    2426            7 :     new_stmt = gimple_build_call_internal_vec (IFN_ASSUME, vargs);
    2427              :   else
    2428        17636 :     new_stmt = gimple_build_call_vec (gimple_call_fn (stmt), vargs);
    2429        17643 :   if (gimple_has_location (stmt))
    2430        16622 :     gimple_set_location (new_stmt, gimple_location (stmt));
    2431        17643 :   gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
    2432        17643 :   gimple_call_copy_flags (new_stmt, stmt);
    2433        17643 :   if (tree lhs = gimple_call_lhs (stmt))
    2434              :     {
    2435        14190 :       modify_expression (&lhs, false);
    2436              :       /* Avoid adjusting SSA_NAME_DEF_STMT of a SSA lhs, SSA names
    2437              :          have not yet been remapped.  */
    2438        14190 :       *gimple_call_lhs_ptr (new_stmt) = lhs;
    2439              :     }
    2440        17643 :   *stmt_p = new_stmt;
    2441              : 
    2442        17643 :   if (orig_stmt)
    2443        17643 :     record_argument_state (m_id, orig_stmt, index_map, pass_through_map,
    2444              :                            always_copy_delta);
    2445        17643 :   return true;
    2446       722176 : }
    2447              : 
    2448              : /* If the statement STMT contains any expressions that need to replaced with a
    2449              :    different one as noted by ADJUSTMENTS, do so.  Handle any potential type
    2450              :    incompatibilities.  If any conversion sttements have to be pre-pended to
    2451              :    STMT, they will be added to EXTRA_STMTS.  Return true iff the statement was
    2452              :    modified.  */
    2453              : 
    2454              : bool
    2455      3711915 : ipa_param_body_adjustments::modify_gimple_stmt (gimple **stmt,
    2456              :                                                 gimple_seq *extra_stmts,
    2457              :                                                 gimple *orig_stmt)
    2458              : {
    2459      3711915 :   bool modified = false;
    2460      3711915 :   tree *t;
    2461              : 
    2462      3711915 :   switch (gimple_code (*stmt))
    2463              :     {
    2464       151316 :     case GIMPLE_RETURN:
    2465       151316 :       t = gimple_return_retval_ptr (as_a <greturn *> (*stmt));
    2466       151316 :       if (m_adjustments && m_adjustments->m_skip_return)
    2467        37940 :         *t = NULL_TREE;
    2468       113376 :       else if (*t != NULL_TREE)
    2469        64060 :         modified |= modify_expression (t, true);
    2470              :       break;
    2471              : 
    2472      2234253 :     case GIMPLE_ASSIGN:
    2473      2234253 :       modified |= modify_assignment (*stmt, extra_stmts);
    2474      2234253 :       break;
    2475              : 
    2476       704533 :     case GIMPLE_CALL:
    2477       704533 :       modified |= modify_call_stmt ((gcall **) stmt, orig_stmt);
    2478       704533 :       break;
    2479              : 
    2480          965 :     case GIMPLE_ASM:
    2481          965 :       {
    2482          965 :         gasm *asm_stmt = as_a <gasm *> (*stmt);
    2483         3033 :         for (unsigned i = 0; i < gimple_asm_ninputs (asm_stmt); i++)
    2484              :           {
    2485         1103 :             t = &TREE_VALUE (gimple_asm_input_op (asm_stmt, i));
    2486         1103 :             modified |= modify_expression (t, true);
    2487              :           }
    2488         2541 :         for (unsigned i = 0; i < gimple_asm_noutputs (asm_stmt); i++)
    2489              :           {
    2490         1576 :             t = &TREE_VALUE (gimple_asm_output_op (asm_stmt, i));
    2491         1576 :             modified |= modify_expression (t, false);
    2492              :           }
    2493              :       }
    2494              :       break;
    2495              : 
    2496              :     default:
    2497              :       break;
    2498              :     }
    2499      3711915 :   return modified;
    2500              : }
    2501              : 
    2502              : 
    2503              : /* Traverse body of the current function and perform the requested adjustments
    2504              :    on its statements.  Return true iff the CFG has been changed.  */
    2505              : 
    2506              : bool
    2507            0 : ipa_param_body_adjustments::modify_cfun_body ()
    2508              : {
    2509            0 :   bool cfg_changed = false;
    2510            0 :   basic_block bb;
    2511              : 
    2512            0 :   FOR_EACH_BB_FN (bb, cfun)
    2513              :     {
    2514            0 :       gimple_stmt_iterator gsi;
    2515              : 
    2516            0 :       for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    2517              :         {
    2518            0 :           gphi *phi = as_a <gphi *> (gsi_stmt (gsi));
    2519            0 :           tree new_lhs, old_lhs = gimple_phi_result (phi);
    2520            0 :           new_lhs = replace_removed_params_ssa_names (old_lhs, phi);
    2521            0 :           if (new_lhs)
    2522              :             {
    2523            0 :               gimple_phi_set_result (phi, new_lhs);
    2524            0 :               release_ssa_name (old_lhs);
    2525              :             }
    2526              :         }
    2527              : 
    2528            0 :       gsi = gsi_start_bb (bb);
    2529            0 :       while (!gsi_end_p (gsi))
    2530              :         {
    2531            0 :           gimple *stmt = gsi_stmt (gsi);
    2532            0 :           gimple *stmt_copy = stmt;
    2533            0 :           gimple_seq extra_stmts = NULL;
    2534            0 :           bool modified = modify_gimple_stmt (&stmt, &extra_stmts, NULL);
    2535            0 :           if (stmt != stmt_copy)
    2536              :             {
    2537            0 :               gcc_checking_assert (modified);
    2538            0 :               gsi_replace (&gsi, stmt, false);
    2539              :             }
    2540            0 :           if (!gimple_seq_empty_p (extra_stmts))
    2541            0 :             gsi_insert_seq_before (&gsi, extra_stmts, GSI_SAME_STMT);
    2542              : 
    2543            0 :           def_operand_p defp;
    2544            0 :           ssa_op_iter iter;
    2545            0 :           FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF)
    2546              :             {
    2547            0 :               tree old_def = DEF_FROM_PTR (defp);
    2548            0 :               if (tree new_def = replace_removed_params_ssa_names (old_def,
    2549              :                                                                    stmt))
    2550              :                 {
    2551            0 :                   SET_DEF (defp, new_def);
    2552            0 :                   release_ssa_name (old_def);
    2553            0 :                   modified = true;
    2554              :                 }
    2555              :             }
    2556              : 
    2557            0 :           if (modified)
    2558              :             {
    2559            0 :               update_stmt (stmt);
    2560            0 :               if (maybe_clean_eh_stmt (stmt)
    2561            0 :                   && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
    2562              :                 cfg_changed = true;
    2563              :             }
    2564            0 :           gsi_next (&gsi);
    2565              :         }
    2566              :     }
    2567              : 
    2568            0 :   return cfg_changed;
    2569              : }
    2570              : 
    2571              : /* Call gimple_debug_bind_reset_value on all debug statements describing
    2572              :    gimple register parameters that are being removed or replaced.  */
    2573              : 
    2574              : void
    2575            0 : ipa_param_body_adjustments::reset_debug_stmts ()
    2576              : {
    2577            0 :   int i, len;
    2578            0 :   gimple_stmt_iterator *gsip = NULL, gsi;
    2579              : 
    2580            0 :   if (MAY_HAVE_DEBUG_STMTS && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
    2581              :     {
    2582            0 :       gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
    2583            0 :       gsip = &gsi;
    2584              :     }
    2585            0 :   len = m_reset_debug_decls.length ();
    2586            0 :   for (i = 0; i < len; i++)
    2587              :     {
    2588            0 :       imm_use_iterator ui;
    2589            0 :       gimple *stmt;
    2590            0 :       gdebug *def_temp;
    2591            0 :       tree name, vexpr, copy = NULL_TREE;
    2592            0 :       use_operand_p use_p;
    2593            0 :       tree decl = m_reset_debug_decls[i];
    2594              : 
    2595            0 :       gcc_checking_assert (is_gimple_reg (decl));
    2596            0 :       name = ssa_default_def (cfun, decl);
    2597            0 :       vexpr = NULL;
    2598            0 :       if (name)
    2599            0 :         FOR_EACH_IMM_USE_STMT (stmt, ui, name)
    2600              :           {
    2601            0 :             if (gimple_clobber_p (stmt))
    2602              :               {
    2603            0 :                 gimple_stmt_iterator cgsi = gsi_for_stmt (stmt);
    2604            0 :                 unlink_stmt_vdef (stmt);
    2605            0 :                 gsi_remove (&cgsi, true);
    2606            0 :                 release_defs (stmt);
    2607            0 :                 continue;
    2608            0 :               }
    2609              :             /* All other users must have been removed by function body
    2610              :                modification.  */
    2611            0 :             gcc_assert (is_gimple_debug (stmt));
    2612            0 :             if (vexpr == NULL && gsip != NULL)
    2613              :               {
    2614            0 :                 vexpr = build_debug_expr_decl (TREE_TYPE (name));
    2615              :                 /* FIXME: Is setting the mode really necessary? */
    2616            0 :                 SET_DECL_MODE (vexpr, DECL_MODE (decl));
    2617            0 :                 def_temp = gimple_build_debug_source_bind (vexpr, decl, NULL);
    2618            0 :                 gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
    2619              :               }
    2620            0 :             if (vexpr)
    2621              :               {
    2622            0 :                 FOR_EACH_IMM_USE_ON_STMT (use_p, ui)
    2623            0 :                   SET_USE (use_p, vexpr);
    2624              :               }
    2625              :             else
    2626            0 :               gimple_debug_bind_reset_value (stmt);
    2627            0 :             update_stmt (stmt);
    2628            0 :           }
    2629              :       /* Create a VAR_DECL for debug info purposes.  */
    2630            0 :       if (!DECL_IGNORED_P (decl))
    2631              :         {
    2632            0 :           copy = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
    2633            0 :                              VAR_DECL, DECL_NAME (decl),
    2634            0 :                              TREE_TYPE (decl));
    2635            0 :           if (DECL_PT_UID_SET_P (decl))
    2636            0 :             SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
    2637            0 :           TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
    2638            0 :           TREE_READONLY (copy) = TREE_READONLY (decl);
    2639            0 :           TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
    2640            0 :           DECL_NOT_GIMPLE_REG_P (copy) = DECL_NOT_GIMPLE_REG_P (decl);
    2641            0 :           DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
    2642            0 :           DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
    2643            0 :           DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
    2644            0 :           DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
    2645            0 :           SET_DECL_RTL (copy, 0);
    2646            0 :           TREE_USED (copy) = 1;
    2647            0 :           DECL_CONTEXT (copy) = current_function_decl;
    2648            0 :           add_local_decl (cfun, copy);
    2649            0 :           DECL_CHAIN (copy)
    2650            0 :             = BLOCK_VARS (DECL_INITIAL (current_function_decl));
    2651            0 :           BLOCK_VARS (DECL_INITIAL (current_function_decl)) = copy;
    2652              :         }
    2653            0 :       if (gsip != NULL && copy && target_for_debug_bind (decl))
    2654              :         {
    2655            0 :           gcc_assert (TREE_CODE (decl) == PARM_DECL);
    2656            0 :           if (vexpr)
    2657            0 :             def_temp = gimple_build_debug_bind (copy, vexpr, NULL);
    2658              :           else
    2659            0 :             def_temp = gimple_build_debug_source_bind (copy, decl,
    2660              :                                                        NULL);
    2661            0 :           gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
    2662              :         }
    2663              :     }
    2664            0 : }
    2665              : 
    2666              : /* Perform all necessary body changes to change signature, body and debug info
    2667              :    of fun according to adjustments passed at construction.  Return true if CFG
    2668              :    was changed in any way.  The main entry point for modification of standalone
    2669              :    functions that is not part of IPA clone materialization.  */
    2670              : 
    2671              : bool
    2672            0 : ipa_param_body_adjustments::perform_cfun_body_modifications ()
    2673              : {
    2674            0 :   bool cfg_changed;
    2675            0 :   modify_formal_parameters ();
    2676            0 :   cfg_changed = modify_cfun_body ();
    2677            0 :   reset_debug_stmts ();
    2678              : 
    2679            0 :   return cfg_changed;
    2680              : }
    2681              : 
    2682              : 
    2683              : /* If there are any initialization statements that need to be emitted into
    2684              :    the basic block BB right at the start of the new function, do so.  */
    2685              : void
    2686       166656 : ipa_param_body_adjustments::append_init_stmts (basic_block bb)
    2687              : {
    2688       166656 :   gimple_stmt_iterator si = gsi_last_bb (bb);
    2689       167811 :   while (!m_split_agg_csts_inits.is_empty ())
    2690         1155 :     gsi_insert_after (&si, m_split_agg_csts_inits.pop (), GSI_NEW_STMT);
    2691       166656 : }
    2692              : 
    2693              : /* Deallocate summaries which otherwise stay alive until the end of
    2694              :    compilation.  */
    2695              : 
    2696              : void
    2697       266934 : ipa_edge_modifications_finalize ()
    2698              : {
    2699       266934 :   if (!ipa_edge_modifications)
    2700              :     return;
    2701         4318 :   delete ipa_edge_modifications;
    2702         4318 :   ipa_edge_modifications = NULL;
    2703              : }
    2704              : 
    2705              : /* Helper used to sort a vector of SSA_NAMES. */
    2706              : 
    2707              : static int
    2708        18443 : compare_ssa_versions (const void *va, const void *vb)
    2709              : {
    2710        18443 :   const_tree const a = *(const_tree const*)va;
    2711        18443 :   const_tree const b = *(const_tree const*)vb;
    2712              : 
    2713        18443 :   if (SSA_NAME_VERSION (a) < SSA_NAME_VERSION (b))
    2714              :     return -1;
    2715         8055 :   if (SSA_NAME_VERSION (a) > SSA_NAME_VERSION (b))
    2716         8055 :     return 1;
    2717              :   return 0;
    2718              : }
    2719              : 
    2720              : /* Call release_ssa_name on all elements in KILLED_SSAS in a defined order.  */
    2721              : 
    2722              : void
    2723      2261774 : ipa_release_ssas_in_hash (hash_set <tree> *killed_ssas)
    2724              : {
    2725      2261774 :   auto_vec<tree, 16> ssas_to_release;
    2726      2274477 :   for (tree sn : *killed_ssas)
    2727        12703 :     ssas_to_release.safe_push (sn);
    2728      2261774 :   ssas_to_release.qsort (compare_ssa_versions);
    2729      6798025 :   for (tree sn : ssas_to_release)
    2730        12703 :     release_ssa_name (sn);
    2731      2261774 : }
        

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