1 /* Manipulation of formal and actual parameters of functions and function
2    calls.
3    Copyright (C) 2017-2021 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 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "ssa.h"
28 #include "cgraph.h"
29 #include "fold-const.h"
30 #include "tree-eh.h"
31 #include "stor-layout.h"
32 #include "gimplify.h"
33 #include "gimple-iterator.h"
34 #include "gimplify-me.h"
35 #include "tree-cfg.h"
36 #include "tree-dfa.h"
37 #include "ipa-param-manipulation.h"
38 #include "print-tree.h"
39 #include "gimple-pretty-print.h"
40 #include "builtins.h"
41 #include "tree-ssa.h"
42 #include "tree-inline.h"
43 #include "alloc-pool.h"
44 #include "symbol-summary.h"
45 #include "symtab-clones.h"
46 
47 
48 /* Actual prefixes of different newly synthetized parameters.  Keep in sync
49    with IPA_PARAM_PREFIX_* defines.  */
50 
51 static const char *ipa_param_prefixes[IPA_PARAM_PREFIX_COUNT]
52   = {"SYNTH",
53      "ISRA",
54      "simd",
55      "mask"};
56 
57 /* Names of parameters for dumping.  Keep in sync with enum ipa_parm_op.  */
58 
59 static const char *ipa_param_op_names[IPA_PARAM_PREFIX_COUNT]
60   = {"IPA_PARAM_OP_UNDEFINED",
61      "IPA_PARAM_OP_COPY",
62      "IPA_PARAM_OP_NEW",
63      "IPA_PARAM_OP_SPLIT"};
64 
65 /* Fill an empty vector ARGS with PARM_DECLs representing formal parameters of
66    FNDECL.  The function should not be called during LTO WPA phase except for
67    thunks (or functions with bodies streamed in). */
68 
69 void
push_function_arg_decls(vec<tree> * args,tree fndecl)70 push_function_arg_decls (vec<tree> *args, tree fndecl)
71 {
72   int count;
73   tree parm;
74 
75   /* Safety check that we do not attempt to use the function in WPA, except
76      when the function is a thunk and then we have DECL_ARGUMENTS or when we
77      have already explicitely loaded its body.  */
78   gcc_assert (!flag_wpa
79 	      || DECL_ARGUMENTS (fndecl)
80 	      || gimple_has_body_p (fndecl));
81   count = 0;
82   for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
83     count++;
84 
85   args->reserve_exact (count);
86   for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
87     args->quick_push (parm);
88 }
89 
90 /* Fill an empty vector TYPES with trees representing formal parameters of
91    function type FNTYPE.  */
92 
93 void
push_function_arg_types(vec<tree> * types,tree fntype)94 push_function_arg_types (vec<tree> *types, tree fntype)
95 {
96   int count = 0;
97   tree t;
98 
99   for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
100     count++;
101 
102   types->reserve_exact (count);
103   for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
104     types->quick_push (TREE_VALUE (t));
105 }
106 
107 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
108    friendly way, assuming they are meant to be applied to FNDECL.  */
109 
110 void
ipa_dump_adjusted_parameters(FILE * f,vec<ipa_adjusted_param,va_gc> * adj_params)111 ipa_dump_adjusted_parameters (FILE *f,
112 			      vec<ipa_adjusted_param, va_gc> *adj_params)
113 {
114   unsigned i, len = vec_safe_length (adj_params);
115   bool first = true;
116 
117   if (!len)
118     return;
119 
120   fprintf (f, "    IPA adjusted parameters: ");
121   for (i = 0; i < len; i++)
122     {
123       struct ipa_adjusted_param *apm;
124       apm = &(*adj_params)[i];
125 
126       if (!first)
127 	fprintf (f, "                             ");
128       else
129 	first = false;
130 
131       fprintf (f, "%i. %s %s", i, ipa_param_op_names[apm->op],
132 	       apm->prev_clone_adjustment ? "prev_clone_adjustment " : "");
133       switch (apm->op)
134 	{
135 	case IPA_PARAM_OP_UNDEFINED:
136 	  break;
137 
138 	case IPA_PARAM_OP_COPY:
139 	  fprintf (f, ", base_index: %u", apm->base_index);
140 	  fprintf (f, ", prev_clone_index: %u", apm->prev_clone_index);
141 	  break;
142 
143 	case IPA_PARAM_OP_SPLIT:
144 	  fprintf (f, ", offset: %u", apm->unit_offset);
145 	  /* fall-through */
146 	case IPA_PARAM_OP_NEW:
147 	  fprintf (f, ", base_index: %u", apm->base_index);
148 	  fprintf (f, ", prev_clone_index: %u", apm->prev_clone_index);
149 	  print_node_brief (f, ", type: ", apm->type, 0);
150 	  print_node_brief (f, ", alias type: ", apm->alias_ptr_type, 0);
151 	  fprintf (f, " prefix: %s",
152 		   ipa_param_prefixes[apm->param_prefix_index]);
153 	  if (apm->reverse)
154 	    fprintf (f, ", reverse-sso");
155 	  break;
156 	}
157       fprintf (f, "\n");
158     }
159 }
160 
161 /* Fill NEW_TYPES with types of a function after its current OTYPES have been
162    modified as described in ADJ_PARAMS.  When USE_PREV_INDICES is true, use
163    prev_clone_index from ADJ_PARAMS as opposed to base_index when the parameter
164    is false.  */
165 
166 static void
fill_vector_of_new_param_types(vec<tree> * new_types,vec<tree> * otypes,vec<ipa_adjusted_param,va_gc> * adj_params,bool use_prev_indices)167 fill_vector_of_new_param_types (vec<tree> *new_types, vec<tree> *otypes,
168 				vec<ipa_adjusted_param, va_gc> *adj_params,
169 				bool use_prev_indices)
170 {
171   unsigned adj_len = vec_safe_length (adj_params);
172   new_types->reserve_exact (adj_len);
173   for (unsigned i = 0; i < adj_len ; i++)
174     {
175       ipa_adjusted_param *apm = &(*adj_params)[i];
176       if (apm->op == IPA_PARAM_OP_COPY)
177 	{
178 	  unsigned index
179 	    = use_prev_indices ? apm->prev_clone_index : apm->base_index;
180 	  /* The following needs to be handled gracefully because of type
181 	     mismatches.  This happens with LTO but apparently also in Fortran
182 	     with -fcoarray=lib -O2 -lcaf_single -latomic.  */
183 	  if (index >= otypes->length ())
184 	    continue;
185 	  new_types->quick_push ((*otypes)[index]);
186 	}
187       else if (apm->op == IPA_PARAM_OP_NEW
188 	       || apm->op == IPA_PARAM_OP_SPLIT)
189 	{
190 	  tree ntype = apm->type;
191 	  if (is_gimple_reg_type (ntype)
192 	      && TYPE_MODE (ntype) != BLKmode)
193 	    {
194 	      unsigned malign = GET_MODE_ALIGNMENT (TYPE_MODE (ntype));
195 	      if (TYPE_ALIGN (ntype) != malign)
196 		ntype = build_aligned_type (ntype, malign);
197 	    }
198 	  new_types->quick_push (ntype);
199 	}
200       else
201 	gcc_unreachable ();
202     }
203 }
204 
205 /* Build and return a function type just like ORIG_TYPE but with parameter
206    types given in NEW_PARAM_TYPES - which can be NULL if, but only if,
207    ORIG_TYPE itself has NULL TREE_ARG_TYPEs.  If METHOD2FUNC is true, also make
208    it a FUNCTION_TYPE instead of FUNCTION_TYPE.  */
209 
210 static tree
build_adjusted_function_type(tree orig_type,vec<tree> * new_param_types,bool method2func,bool skip_return)211 build_adjusted_function_type (tree orig_type, vec<tree> *new_param_types,
212 			      bool method2func, bool skip_return)
213 {
214   tree new_arg_types = NULL;
215   if (TYPE_ARG_TYPES (orig_type))
216     {
217       gcc_checking_assert (new_param_types);
218       bool last_parm_void = (TREE_VALUE (tree_last (TYPE_ARG_TYPES (orig_type)))
219 			     == void_type_node);
220       unsigned len = new_param_types->length ();
221       for (unsigned i = 0; i < len; i++)
222 	new_arg_types = tree_cons (NULL_TREE, (*new_param_types)[i],
223 				   new_arg_types);
224 
225       tree new_reversed = nreverse (new_arg_types);
226       if (last_parm_void)
227 	{
228 	  if (new_reversed)
229 	    TREE_CHAIN (new_arg_types) = void_list_node;
230 	  else
231 	    new_reversed = void_list_node;
232 	}
233       new_arg_types = new_reversed;
234     }
235 
236   /* Use build_distinct_type_copy to preserve as much as possible from original
237      type (debug info, attribute lists etc.).  The one exception is
238      METHOD_TYPEs which must have THIS argument and when we are asked to remove
239      it, we need to build new FUNCTION_TYPE instead.  */
240   tree new_type = NULL;
241   if (method2func)
242     {
243       tree ret_type;
244       if (skip_return)
245 	ret_type = void_type_node;
246       else
247 	ret_type = TREE_TYPE (orig_type);
248 
249       new_type
250 	= build_distinct_type_copy (build_function_type (ret_type,
251 							 new_arg_types));
252       TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
253     }
254   else
255     {
256       new_type = build_distinct_type_copy (orig_type);
257       TYPE_ARG_TYPES (new_type) = new_arg_types;
258       if (skip_return)
259 	TREE_TYPE (new_type) = void_type_node;
260     }
261 
262   return new_type;
263 }
264 
265 /* Return the maximum index in any IPA_PARAM_OP_COPY adjustment or -1 if there
266    is none.  */
267 
268 int
get_max_base_index()269 ipa_param_adjustments::get_max_base_index ()
270 {
271   unsigned adj_len = vec_safe_length (m_adj_params);
272   int max_index = -1;
273   for (unsigned i = 0; i < adj_len ; i++)
274     {
275       ipa_adjusted_param *apm = &(*m_adj_params)[i];
276       if (apm->op == IPA_PARAM_OP_COPY
277 	  && max_index < apm->base_index)
278 	max_index = apm->base_index;
279     }
280   return max_index;
281 }
282 
283 
284 /* Fill SURVIVING_PARAMS with an array of bools where each one says whether a
285    parameter that originally was at that position still survives in the given
286    clone or is removed/replaced.  If the final array is smaller than an index
287    of an original parameter, that parameter also did not survive.  That a
288    parameter survives does not mean it has the same index as before.  */
289 
290 void
get_surviving_params(vec<bool> * surviving_params)291 ipa_param_adjustments::get_surviving_params (vec<bool> *surviving_params)
292 {
293   unsigned adj_len = vec_safe_length (m_adj_params);
294   int max_index = get_max_base_index ();
295 
296   if (max_index < 0)
297     return;
298   surviving_params->reserve_exact (max_index + 1);
299   surviving_params->quick_grow_cleared (max_index + 1);
300   for (unsigned i = 0; i < adj_len ; i++)
301     {
302       ipa_adjusted_param *apm = &(*m_adj_params)[i];
303       if (apm->op == IPA_PARAM_OP_COPY)
304 	(*surviving_params)[apm->base_index] = true;
305     }
306 }
307 
308 /* Fill NEW_INDICES with new indices of each surviving parameter or -1 for
309    those which do not survive.  Any parameter outside of lenght of the vector
310    does not survive.  There is currently no support for a parameter to be
311    copied to two distinct new parameters.  */
312 
313 void
get_updated_indices(vec<int> * new_indices)314 ipa_param_adjustments::get_updated_indices (vec<int> *new_indices)
315 {
316   unsigned adj_len = vec_safe_length (m_adj_params);
317   int max_index = get_max_base_index ();
318 
319   if (max_index < 0)
320     return;
321   unsigned res_len = max_index + 1;
322   new_indices->reserve_exact (res_len);
323   for (unsigned i = 0; i < res_len ; i++)
324     new_indices->quick_push (-1);
325   for (unsigned i = 0; i < adj_len ; i++)
326     {
327       ipa_adjusted_param *apm = &(*m_adj_params)[i];
328       if (apm->op == IPA_PARAM_OP_COPY)
329 	(*new_indices)[apm->base_index] = i;
330     }
331 }
332 
333 /* Return the original index for the given new parameter index.  Return a
334    negative number if not available.  */
335 
336 int
get_original_index(int newidx)337 ipa_param_adjustments::get_original_index (int newidx)
338 {
339   const ipa_adjusted_param *adj = &(*m_adj_params)[newidx];
340   if (adj->op != IPA_PARAM_OP_COPY)
341     return -1;
342   return adj->base_index;
343 }
344 
345 /* Return true if the first parameter (assuming there was one) survives the
346    transformation intact and remains the first one.  */
347 
348 bool
first_param_intact_p()349 ipa_param_adjustments::first_param_intact_p ()
350 {
351   return (!vec_safe_is_empty (m_adj_params)
352 	  && (*m_adj_params)[0].op == IPA_PARAM_OP_COPY
353 	  && (*m_adj_params)[0].base_index == 0);
354 }
355 
356 /* Return true if we have to change what has formerly been a method into a
357    function.  */
358 
359 bool
method2func_p(tree orig_type)360 ipa_param_adjustments::method2func_p (tree orig_type)
361 {
362   return ((TREE_CODE (orig_type) == METHOD_TYPE) && !first_param_intact_p ());
363 }
364 
365 /* Given function type OLD_TYPE, return a new type derived from it after
366    performing all atored modifications.  TYPE_ORIGINAL_P should be true when
367    OLD_TYPE refers to the type before any IPA transformations, as opposed to a
368    type that can be an intermediate one in between various IPA
369    transformations.  */
370 
371 tree
build_new_function_type(tree old_type,bool type_original_p)372 ipa_param_adjustments::build_new_function_type (tree old_type,
373 						bool type_original_p)
374 {
375   auto_vec<tree,16> new_param_types, *new_param_types_p;
376   if (prototype_p (old_type))
377     {
378       auto_vec<tree, 16> otypes;
379       push_function_arg_types (&otypes, old_type);
380       fill_vector_of_new_param_types (&new_param_types, &otypes, m_adj_params,
381 				      !type_original_p);
382       new_param_types_p = &new_param_types;
383     }
384   else
385     new_param_types_p = NULL;
386 
387   return build_adjusted_function_type (old_type, new_param_types_p,
388 				       method2func_p (old_type), m_skip_return);
389 }
390 
391 /* Build variant of function decl ORIG_DECL which has no return value if
392    M_SKIP_RETURN is true and, if ORIG_DECL's types or parameters is known, has
393    this type adjusted as indicated in M_ADJ_PARAMS. Arguments from
394    DECL_ARGUMENTS list are not processed now, since they are linked by
395    TREE_CHAIN directly and not accessible in LTO during WPA.  The caller is
396    responsible for eliminating them when clones are properly materialized.  */
397 
398 tree
adjust_decl(tree orig_decl)399 ipa_param_adjustments::adjust_decl (tree orig_decl)
400 {
401   tree new_decl = copy_node (orig_decl);
402   tree orig_type = TREE_TYPE (orig_decl);
403   if (prototype_p (orig_type)
404       || (m_skip_return && !VOID_TYPE_P (TREE_TYPE (orig_type))))
405     {
406       tree new_type = build_new_function_type (orig_type, false);
407       TREE_TYPE (new_decl) = new_type;
408     }
409   if (method2func_p (orig_type))
410     DECL_VINDEX (new_decl) = NULL_TREE;
411 
412   /* When signature changes, we need to clear builtin info.  */
413   if (fndecl_built_in_p (new_decl))
414     set_decl_built_in_function (new_decl, NOT_BUILT_IN, 0);
415 
416   DECL_VIRTUAL_P (new_decl) = 0;
417   DECL_LANG_SPECIFIC (new_decl) = NULL;
418 
419   /* Drop MALLOC attribute for a void function.  */
420   if (m_skip_return)
421     DECL_IS_MALLOC (new_decl) = 0;
422 
423   return new_decl;
424 }
425 
426 /* Wrapper around get_base_ref_and_offset for cases interesting for IPA-SRA
427    transformations.  Return true if EXPR has an interesting form and fill in
428    *BASE_P and *UNIT_OFFSET_P with the appropriate info.  */
429 
430 static bool
isra_get_ref_base_and_offset(tree expr,tree * base_p,unsigned * unit_offset_p)431 isra_get_ref_base_and_offset (tree expr, tree *base_p, unsigned *unit_offset_p)
432 {
433   HOST_WIDE_INT offset, size;
434   bool reverse;
435   tree base
436     = get_ref_base_and_extent_hwi (expr, &offset, &size, &reverse);
437   if (!base || size < 0)
438     return false;
439 
440   if ((offset % BITS_PER_UNIT) != 0)
441     return false;
442 
443   if (TREE_CODE (base) == MEM_REF)
444     {
445       poly_int64 plmoff = mem_ref_offset (base).force_shwi ();
446       HOST_WIDE_INT moff;
447       bool is_cst = plmoff.is_constant (&moff);
448       if (!is_cst)
449 	return false;
450       offset += moff * BITS_PER_UNIT;
451       base = TREE_OPERAND (base, 0);
452     }
453 
454   if (offset < 0 || (offset / BITS_PER_UNIT) > UINT_MAX)
455     return false;
456 
457   *base_p = base;
458   *unit_offset_p = offset / BITS_PER_UNIT;
459   return true;
460 }
461 
462 /* Return true if EXPR describes a transitive split (i.e. one that happened for
463    both the caller and the callee) as recorded in PERFORMED_SPLITS.  In that
464    case, store index of the respective record in PERFORMED_SPLITS into
465    *SM_IDX_P and the unit offset from all handled components in EXPR into
466    *UNIT_OFFSET_P.  */
467 
468 static bool
transitive_split_p(vec<ipa_param_performed_split,va_gc> * performed_splits,tree expr,unsigned * sm_idx_p,unsigned * unit_offset_p)469 transitive_split_p (vec<ipa_param_performed_split, va_gc> *performed_splits,
470 		    tree expr, unsigned *sm_idx_p, unsigned *unit_offset_p)
471 {
472   tree base;
473   if (!isra_get_ref_base_and_offset (expr, &base, unit_offset_p))
474     return false;
475 
476   if (TREE_CODE (base) == SSA_NAME)
477     {
478       base = SSA_NAME_VAR (base);
479       if (!base)
480 	return false;
481     }
482 
483   unsigned len = vec_safe_length (performed_splits);
484   for (unsigned i = 0 ; i < len; i++)
485     {
486       ipa_param_performed_split *sm = &(*performed_splits)[i];
487       if (sm->dummy_decl == base)
488 	{
489 	  *sm_idx_p = i;
490 	  return true;
491 	}
492     }
493   return false;
494 }
495 
496 /* Structure to hold declarations representing transitive IPA-SRA splits.  In
497    essence, if we need to pass UNIT_OFFSET of a parameter which originally has
498    number BASE_INDEX, we should pass down REPL.  */
499 
500 struct transitive_split_map
501 {
502   tree repl;
503   unsigned base_index;
504   unsigned unit_offset;
505 };
506 
507 /* If call STMT contains any parameters representing transitive splits as
508    described by PERFORMED_SPLITS, return the number of extra parameters that
509    were addded during clone materialization and fill in INDEX_MAP with adjusted
510    indices of corresponding original parameters and TRANS_MAP with description
511    of all transitive replacement descriptions.  Otherwise return zero. */
512 
513 static unsigned
init_transitive_splits(vec<ipa_param_performed_split,va_gc> * performed_splits,gcall * stmt,vec<unsigned> * index_map,auto_vec<transitive_split_map> * trans_map)514 init_transitive_splits (vec<ipa_param_performed_split, va_gc> *performed_splits,
515 			gcall *stmt, vec <unsigned> *index_map,
516 			auto_vec <transitive_split_map> *trans_map)
517 {
518   unsigned phony_arguments = 0;
519   unsigned stmt_idx = 0, base_index = 0;
520   unsigned nargs = gimple_call_num_args (stmt);
521   while (stmt_idx < nargs)
522     {
523       unsigned unit_offset_delta;
524       tree base_arg = gimple_call_arg (stmt, stmt_idx);
525 
526       if (phony_arguments > 0)
527 	index_map->safe_push (stmt_idx);
528 
529       unsigned sm_idx;
530       stmt_idx++;
531       if (transitive_split_p (performed_splits, base_arg, &sm_idx,
532 			      &unit_offset_delta))
533 	{
534 	  if (phony_arguments == 0)
535 	    /* We have optimistically avoided constructing index_map do far but
536 	       now it is clear it will be necessary, so let's create the easy
537 	       bit we skipped until now.  */
538 	    for (unsigned k = 0; k < stmt_idx; k++)
539 	      index_map->safe_push (k);
540 
541 	  tree dummy = (*performed_splits)[sm_idx].dummy_decl;
542 	  for (unsigned j = sm_idx; j < performed_splits->length (); j++)
543 	    {
544 	      ipa_param_performed_split *caller_split
545 		= &(*performed_splits)[j];
546 	      if (caller_split->dummy_decl != dummy)
547 		break;
548 
549 	      tree arg = gimple_call_arg (stmt, stmt_idx);
550 	      struct transitive_split_map tsm;
551 	      tsm.repl = arg;
552 	      tsm.base_index = base_index;
553 	      if (caller_split->unit_offset >= unit_offset_delta)
554 		{
555 		  tsm.unit_offset
556 		    = (caller_split->unit_offset - unit_offset_delta);
557 		  trans_map->safe_push (tsm);
558 		}
559 
560 	      phony_arguments++;
561 	      stmt_idx++;
562 	    }
563 	}
564       base_index++;
565     }
566   return phony_arguments;
567 }
568 
569 /* Modify actual arguments of a function call in statement STMT, assuming it
570    calls CALLEE_DECL.  CALLER_ADJ must be the description of parameter
571    adjustments of the caller or NULL if there are none.  Return the new
572    statement that replaced the old one.  When invoked, cfun and
573    current_function_decl have to be set to the caller.  */
574 
575 gcall *
modify_call(gcall * stmt,vec<ipa_param_performed_split,va_gc> * performed_splits,tree callee_decl,bool update_references)576 ipa_param_adjustments::modify_call (gcall *stmt,
577 				    vec<ipa_param_performed_split,
578 				        va_gc> *performed_splits,
579 				    tree callee_decl, bool update_references)
580 {
581   unsigned len = vec_safe_length (m_adj_params);
582   auto_vec<tree, 16> vargs (len);
583   tree old_decl = gimple_call_fndecl (stmt);
584   unsigned old_nargs = gimple_call_num_args (stmt);
585   auto_vec<bool, 16> kept (old_nargs);
586   kept.quick_grow_cleared (old_nargs);
587 
588   auto_vec <unsigned, 16> index_map;
589   auto_vec <transitive_split_map> trans_map;
590   bool transitive_remapping = false;
591 
592   if (performed_splits)
593     {
594       unsigned removed = init_transitive_splits (performed_splits,
595 						 stmt, &index_map, &trans_map);
596       if (removed > 0)
597 	{
598 	  transitive_remapping = true;
599 	  old_nargs -= removed;
600 	}
601     }
602 
603   cgraph_node *current_node = cgraph_node::get (current_function_decl);
604   if (update_references)
605     current_node->remove_stmt_references (stmt);
606 
607   gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
608   gimple_stmt_iterator prev_gsi = gsi;
609   gsi_prev (&prev_gsi);
610   for (unsigned i = 0; i < len; i++)
611     {
612       ipa_adjusted_param *apm = &(*m_adj_params)[i];
613       if (apm->op == IPA_PARAM_OP_COPY)
614 	{
615 	  unsigned index = apm->base_index;
616 	  if (index >= old_nargs)
617 	    /* Can happen if the original call has argument mismatch,
618 	       ignore.  */
619 	    continue;
620 	  if (transitive_remapping)
621 	    index = index_map[apm->base_index];
622 
623 	  tree arg = gimple_call_arg (stmt, index);
624 
625 	  vargs.quick_push (arg);
626 	  kept[index] = true;
627 	  continue;
628 	}
629 
630       /* At the moment the only user of IPA_PARAM_OP_NEW modifies calls itself.
631 	 If we ever want to support it during WPA IPA stage, we'll need a
632 	 mechanism to call into the IPA passes that introduced them.  Currently
633 	 we simply mandate that IPA infrastructure understands all argument
634 	 modifications.  Remember, edge redirection/modification is done only
635 	 once, not in steps for each pass modifying the callee like clone
636 	 materialization.  */
637       gcc_assert (apm->op == IPA_PARAM_OP_SPLIT);
638 
639       /* We have to handle transitive changes differently using the maps we
640 	 have created before.  So look into them first.  */
641       tree repl = NULL_TREE;
642       for (unsigned j = 0; j < trans_map.length (); j++)
643 	if (trans_map[j].base_index == apm->base_index
644 	    && trans_map[j].unit_offset == apm->unit_offset)
645 	  {
646 	    repl = trans_map[j].repl;
647 	    break;
648 	  }
649       if (repl)
650 	{
651 	  vargs.quick_push (repl);
652 	  continue;
653 	}
654 
655       unsigned index = apm->base_index;
656       if (index >= old_nargs)
657 	/* Can happen if the original call has argument mismatch, ignore.  */
658 	continue;
659       if (transitive_remapping)
660 	index = index_map[apm->base_index];
661       tree base = gimple_call_arg (stmt, index);
662 
663       /* We create a new parameter out of the value of the old one, we can
664 	 do the following kind of transformations:
665 
666 	 - A scalar passed by reference, potentially as a part of a larger
667 	 aggregate, is converted to a scalar passed by value.
668 
669 	 - A part of an aggregate is passed instead of the whole aggregate.  */
670 
671       location_t loc = gimple_location (stmt);
672       tree off;
673       bool deref_base = false;
674       unsigned int deref_align = 0;
675       if (TREE_CODE (base) != ADDR_EXPR
676 	  && is_gimple_reg_type (TREE_TYPE (base)))
677 	{
678 	  /* Detect type mismatches in calls in invalid programs and make a
679 	     poor attempt to gracefully convert them so that we don't ICE.  */
680 	  if (!POINTER_TYPE_P (TREE_TYPE (base)))
681 	    base = force_value_to_type (ptr_type_node, base);
682 
683 	  off = build_int_cst (apm->alias_ptr_type, apm->unit_offset);
684 	}
685       else
686 	{
687 	  bool addrof;
688 	  if (TREE_CODE (base) == ADDR_EXPR)
689 	    {
690 	      base = TREE_OPERAND (base, 0);
691 	      addrof = true;
692 	    }
693 	  else
694 	    addrof = false;
695 
696 	  tree prev_base = base;
697 	  poly_int64 base_offset;
698 	  base = get_addr_base_and_unit_offset (base, &base_offset);
699 
700 	  /* Aggregate arguments can have non-invariant addresses.  */
701 	  if (!base)
702 	    {
703 	      base = build_fold_addr_expr (prev_base);
704 	      off = build_int_cst (apm->alias_ptr_type, apm->unit_offset);
705 	    }
706 	  else if (TREE_CODE (base) == MEM_REF)
707 	    {
708 	      if (!addrof)
709 		{
710 		  deref_base = true;
711 		  deref_align = TYPE_ALIGN (TREE_TYPE (base));
712 		}
713 	      off = build_int_cst (apm->alias_ptr_type,
714 				   base_offset + apm->unit_offset);
715 	      off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
716 				     off);
717 	      base = TREE_OPERAND (base, 0);
718 	    }
719 	  else
720 	    {
721 	      off = build_int_cst (apm->alias_ptr_type,
722 				   base_offset + apm->unit_offset);
723 	      base = build_fold_addr_expr (base);
724 	    }
725 	}
726 
727       tree type = apm->type;
728       unsigned int align;
729       unsigned HOST_WIDE_INT misalign;
730 
731       if (deref_base)
732 	{
733 	  align = deref_align;
734 	  misalign = 0;
735 	}
736       else
737 	{
738 	  get_pointer_alignment_1 (base, &align, &misalign);
739 	  /* All users must make sure that we can be optimistic when it
740 	     comes to alignment in this case (by inspecting the final users
741 	     of these new parameters).  */
742 	  if (TYPE_ALIGN (type) > align)
743 	    align = TYPE_ALIGN (type);
744 	}
745       misalign
746 	+= (offset_int::from (wi::to_wide (off), SIGNED).to_short_addr ()
747 	    * BITS_PER_UNIT);
748       misalign = misalign & (align - 1);
749       if (misalign != 0)
750 	align = least_bit_hwi (misalign);
751       if (align < TYPE_ALIGN (type))
752 	type = build_aligned_type (type, align);
753       base = force_gimple_operand_gsi (&gsi, base,
754 				       true, NULL, true, GSI_SAME_STMT);
755       tree expr = fold_build2_loc (loc, MEM_REF, type, base, off);
756       REF_REVERSE_STORAGE_ORDER (expr) = apm->reverse;
757       /* If expr is not a valid gimple call argument emit
758 	 a load into a temporary.  */
759       if (is_gimple_reg_type (TREE_TYPE (expr)))
760 	{
761 	  gimple *tem = gimple_build_assign (NULL_TREE, expr);
762 	  if (gimple_in_ssa_p (cfun))
763 	    {
764 	      gimple_set_vuse (tem, gimple_vuse (stmt));
765 	      expr = make_ssa_name (TREE_TYPE (expr), tem);
766 	    }
767 	  else
768 	    expr = create_tmp_reg (TREE_TYPE (expr));
769 	  gimple_assign_set_lhs (tem, expr);
770 	  gsi_insert_before (&gsi, tem, GSI_SAME_STMT);
771 	}
772       vargs.quick_push (expr);
773     }
774 
775   if (m_always_copy_start >= 0)
776     for (unsigned i = m_always_copy_start; i < old_nargs; i++)
777       vargs.safe_push (gimple_call_arg (stmt, i));
778 
779   /* For optimized away parameters, add on the caller side
780      before the call
781      DEBUG D#X => parm_Y(D)
782      stmts and associate D#X with parm in decl_debug_args_lookup
783      vector to say for debug info that if parameter parm had been passed,
784      it would have value parm_Y(D).  */
785   if (MAY_HAVE_DEBUG_BIND_STMTS && old_decl && callee_decl)
786     {
787       vec<tree, va_gc> **debug_args = NULL;
788       unsigned i = 0;
789       cgraph_node *callee_node = cgraph_node::get (callee_decl);
790 
791       /* FIXME: we don't seem to be able to insert debug args before clone
792 	 is materialized.  Materializing them early leads to extra memory
793 	 use.  */
794       if (callee_node->clone_of)
795 	callee_node->get_untransformed_body ();
796       for (tree old_parm = DECL_ARGUMENTS (old_decl);
797 	   old_parm && i < old_nargs && ((int) i) < m_always_copy_start;
798 	   old_parm = DECL_CHAIN (old_parm), i++)
799 	{
800 	  if (!is_gimple_reg (old_parm) || kept[i])
801 	    continue;
802 	  tree origin = DECL_ORIGIN (old_parm);
803 	  tree arg;
804 	  if (transitive_remapping)
805 	    arg = gimple_call_arg (stmt, index_map[i]);
806 	  else
807 	    arg = gimple_call_arg (stmt, i);
808 
809 	  if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
810 	    {
811 	      if (!fold_convertible_p (TREE_TYPE (origin), arg))
812 		continue;
813 	      tree rhs1;
814 	      if (TREE_CODE (arg) == SSA_NAME
815 		  && gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg))
816 		  && (rhs1
817 		      = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (arg)))
818 		  && useless_type_conversion_p (TREE_TYPE (origin),
819 						TREE_TYPE (rhs1)))
820 		arg = rhs1;
821 	      else
822 		arg = fold_convert_loc (gimple_location (stmt),
823 					TREE_TYPE (origin), arg);
824 	    }
825 	  if (debug_args == NULL)
826 	    debug_args = decl_debug_args_insert (callee_decl);
827 	  unsigned int ix;
828 	  tree ddecl = NULL_TREE;
829 	  for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl); ix += 2)
830 	    if (ddecl == origin)
831 	      {
832 		ddecl = (**debug_args)[ix + 1];
833 		break;
834 	      }
835 	  if (ddecl == NULL)
836 	    {
837 	      ddecl = make_node (DEBUG_EXPR_DECL);
838 	      DECL_ARTIFICIAL (ddecl) = 1;
839 	      TREE_TYPE (ddecl) = TREE_TYPE (origin);
840 	      SET_DECL_MODE (ddecl, DECL_MODE (origin));
841 
842 	      vec_safe_push (*debug_args, origin);
843 	      vec_safe_push (*debug_args, ddecl);
844 	    }
845 	  gimple *def_temp = gimple_build_debug_bind (ddecl,
846 						      unshare_expr (arg), stmt);
847 	  gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
848 	}
849     }
850 
851   if (dump_file && (dump_flags & TDF_DETAILS))
852     {
853       fprintf (dump_file, "replacing stmt:");
854       print_gimple_stmt (dump_file, gsi_stmt (gsi), 0);
855     }
856 
857   gcall *new_stmt = gimple_build_call_vec (callee_decl, vargs);
858 
859   if (tree lhs = gimple_call_lhs (stmt))
860     {
861       if (!m_skip_return)
862 	gimple_call_set_lhs (new_stmt, lhs);
863       else if (TREE_CODE (lhs) == SSA_NAME)
864 	{
865 	  /* LHS should now by a default-def SSA.  Unfortunately default-def
866 	     SSA_NAMEs need a backing variable (or at least some code examining
867 	     SSAs assumes it is non-NULL).  So we either have to re-use the
868 	     decl we have at hand or introdice a new one.  */
869 	  tree repl = create_tmp_var (TREE_TYPE (lhs), "removed_return");
870 	  repl = get_or_create_ssa_default_def (cfun, repl);
871 	  SSA_NAME_IS_DEFAULT_DEF (repl) = true;
872 	  imm_use_iterator ui;
873 	  use_operand_p use_p;
874 	  gimple *using_stmt;
875 	  FOR_EACH_IMM_USE_STMT (using_stmt, ui, lhs)
876 	    {
877 	      FOR_EACH_IMM_USE_ON_STMT (use_p, ui)
878 		{
879 		  SET_USE (use_p, repl);
880 		}
881 	      update_stmt (using_stmt);
882 	    }
883 	}
884     }
885 
886   gimple_set_block (new_stmt, gimple_block (stmt));
887   if (gimple_has_location (stmt))
888     gimple_set_location (new_stmt, gimple_location (stmt));
889   gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
890   gimple_call_copy_flags (new_stmt, stmt);
891   if (gimple_in_ssa_p (cfun))
892     gimple_move_vops (new_stmt, stmt);
893 
894   if (dump_file && (dump_flags & TDF_DETAILS))
895     {
896       fprintf (dump_file, "with stmt:");
897       print_gimple_stmt (dump_file, new_stmt, 0);
898       fprintf (dump_file, "\n");
899     }
900   gsi_replace (&gsi, new_stmt, true);
901   if (update_references)
902     do
903       {
904 	current_node->record_stmt_references (gsi_stmt (gsi));
905 	gsi_prev (&gsi);
906       }
907     while (gsi_stmt (gsi) != gsi_stmt (prev_gsi));
908   return new_stmt;
909 }
910 
911 /* Dump information contained in the object in textual form to F.  */
912 
913 void
dump(FILE * f)914 ipa_param_adjustments::dump (FILE *f)
915 {
916   fprintf (f, "    m_always_copy_start: %i\n", m_always_copy_start);
917   ipa_dump_adjusted_parameters (f, m_adj_params);
918   if (m_skip_return)
919     fprintf (f, "    Will SKIP return.\n");
920 }
921 
922 /* Dump information contained in the object in textual form to stderr.  */
923 
924 void
debug()925 ipa_param_adjustments::debug ()
926 {
927   dump (stderr);
928 }
929 
930 /* Register that REPLACEMENT should replace parameter described in APM and
931    optionally as DUMMY to mark transitive splits across calls.  */
932 
933 void
register_replacement(ipa_adjusted_param * apm,tree replacement,tree dummy)934 ipa_param_body_adjustments::register_replacement (ipa_adjusted_param *apm,
935 						  tree replacement,
936 						  tree dummy)
937 {
938   gcc_checking_assert (apm->op == IPA_PARAM_OP_SPLIT
939 		       || apm->op == IPA_PARAM_OP_NEW);
940   gcc_checking_assert (!apm->prev_clone_adjustment);
941   ipa_param_body_replacement psr;
942   psr.base = m_oparms[apm->prev_clone_index];
943   psr.repl = replacement;
944   psr.dummy = dummy;
945   psr.unit_offset = apm->unit_offset;
946   m_replacements.safe_push (psr);
947 }
948 
949 /* Copy or not, as appropriate given m_id and decl context, a pre-existing
950    PARM_DECL T so that it can be included in the parameters of the modified
951    function.  */
952 
953 tree
carry_over_param(tree t)954 ipa_param_body_adjustments::carry_over_param (tree t)
955 {
956   tree new_parm;
957   if (m_id)
958     {
959       new_parm = remap_decl (t, m_id);
960       if (TREE_CODE (new_parm) != PARM_DECL)
961 	new_parm = m_id->copy_decl (t, m_id);
962     }
963   else if (DECL_CONTEXT (t) != m_fndecl)
964     {
965       new_parm = copy_node (t);
966       DECL_CONTEXT (new_parm) = m_fndecl;
967     }
968   else
969     new_parm = t;
970   return new_parm;
971 }
972 
973 /* Common initialization performed by all ipa_param_body_adjustments
974    constructors.  OLD_FNDECL is the declaration we take original arguments
975    from, (it may be the same as M_FNDECL).  VARS, if non-NULL, is a pointer to
976    a chained list of new local variables.  TREE_MAP is the IPA-CP produced
977    mapping of trees to constants.
978 
979    The function is rather long but it really onlu initializes all data members
980    of the class.  It creates new param DECLs, finds their new types,   */
981 
982 void
common_initialization(tree old_fndecl,tree * vars,vec<ipa_replace_map *,va_gc> * tree_map)983 ipa_param_body_adjustments::common_initialization (tree old_fndecl,
984 						   tree *vars,
985 						   vec<ipa_replace_map *,
986 						       va_gc> *tree_map)
987 {
988   push_function_arg_decls (&m_oparms, old_fndecl);
989   auto_vec<tree,16> otypes;
990   if (TYPE_ARG_TYPES (TREE_TYPE (old_fndecl)) != NULL_TREE)
991     push_function_arg_types (&otypes, TREE_TYPE (old_fndecl));
992   else
993     {
994       auto_vec<tree,16> oparms;
995       push_function_arg_decls (&oparms, old_fndecl);
996       unsigned ocount = oparms.length ();
997       otypes.reserve_exact (ocount);
998       for (unsigned i = 0; i < ocount; i++)
999 	otypes.quick_push (TREE_TYPE (oparms[i]));
1000     }
1001   fill_vector_of_new_param_types (&m_new_types, &otypes, m_adj_params, true);
1002 
1003   auto_vec<bool, 16> kept;
1004   kept.reserve_exact (m_oparms.length ());
1005   kept.quick_grow_cleared (m_oparms.length ());
1006   auto_vec<tree, 16> isra_dummy_decls;
1007   isra_dummy_decls.reserve_exact (m_oparms.length ());
1008   isra_dummy_decls.quick_grow_cleared (m_oparms.length ());
1009 
1010   unsigned adj_len = vec_safe_length (m_adj_params);
1011   m_method2func = ((TREE_CODE (TREE_TYPE (m_fndecl)) == METHOD_TYPE)
1012 		   && (adj_len == 0
1013 		       || (*m_adj_params)[0].op != IPA_PARAM_OP_COPY
1014 		       || (*m_adj_params)[0].base_index != 0));
1015 
1016   /* The main job of the this function is to go over the vector of adjusted
1017      parameters and create declarations or find corresponding old ones and push
1018      them to m_new_decls.  For IPA-SRA replacements it also creates
1019      corresponding m_id->dst_node->clone.performed_splits entries.  */
1020 
1021   m_new_decls.reserve_exact (adj_len);
1022   for (unsigned i = 0; i < adj_len ; i++)
1023     {
1024       ipa_adjusted_param *apm = &(*m_adj_params)[i];
1025       unsigned prev_index = apm->prev_clone_index;
1026       tree new_parm;
1027       if (apm->op == IPA_PARAM_OP_COPY
1028 	  || apm->prev_clone_adjustment)
1029 	{
1030 	  kept[prev_index] = true;
1031 	  new_parm = carry_over_param (m_oparms[prev_index]);
1032 	  m_new_decls.quick_push (new_parm);
1033 	}
1034       else if (apm->op == IPA_PARAM_OP_NEW
1035 	       || apm->op == IPA_PARAM_OP_SPLIT)
1036 	{
1037 	  tree new_type = m_new_types[i];
1038 	  gcc_checking_assert (new_type);
1039 	  new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
1040 				 new_type);
1041 	  const char *prefix = ipa_param_prefixes[apm->param_prefix_index];
1042 	  DECL_NAME (new_parm) = create_tmp_var_name (prefix);
1043 	  DECL_ARTIFICIAL (new_parm) = 1;
1044 	  DECL_ARG_TYPE (new_parm) = new_type;
1045 	  DECL_CONTEXT (new_parm) = m_fndecl;
1046 	  TREE_USED (new_parm) = 1;
1047 	  DECL_IGNORED_P (new_parm) = 1;
1048 	  layout_decl (new_parm, 0);
1049 	  m_new_decls.quick_push (new_parm);
1050 
1051 	  if (apm->op == IPA_PARAM_OP_SPLIT)
1052 	    {
1053 	      m_split_modifications_p = true;
1054 
1055 	      if (m_id)
1056 		{
1057 		  tree dummy_decl;
1058 		  if (!isra_dummy_decls[prev_index])
1059 		    {
1060 		      dummy_decl = copy_decl_to_var (m_oparms[prev_index],
1061 						     m_id);
1062 		      /* Any attempt to remap this dummy in this particular
1063 			 instance of clone materialization should yield
1064 			 itself.  */
1065 		      insert_decl_map (m_id, dummy_decl, dummy_decl);
1066 
1067 		      DECL_CHAIN (dummy_decl) = *vars;
1068 		      *vars = dummy_decl;
1069 		      isra_dummy_decls[prev_index] = dummy_decl;
1070 		    }
1071 		  else
1072 		    dummy_decl = isra_dummy_decls[prev_index];
1073 
1074 		  register_replacement (apm, new_parm, dummy_decl);
1075 		  ipa_param_performed_split ps;
1076 		  ps.dummy_decl = dummy_decl;
1077 		  ps.unit_offset = apm->unit_offset;
1078 		  vec_safe_push (clone_info::get_create
1079 				   (m_id->dst_node)->performed_splits, ps);
1080 		}
1081 	      else
1082 		register_replacement (apm, new_parm);
1083 	    }
1084         }
1085       else
1086 	gcc_unreachable ();
1087     }
1088 
1089 
1090   /* As part of body modifications, we will also have to replace remaining uses
1091      of remaining uses of removed PARM_DECLs (which do not however use the
1092      initial value) with their VAR_DECL copies.
1093 
1094      We do this differently with and without m_id.  With m_id, we rely on its
1095      mapping and create a replacement straight away.  Without it, we have our
1096      own mechanism for which we have to populate m_removed_decls vector.  Just
1097      don't mix them, that is why you should not call
1098      replace_removed_params_ssa_names or perform_cfun_body_modifications when
1099      you construct with ID not equal to NULL.  */
1100 
1101   unsigned op_len = m_oparms.length ();
1102   for (unsigned i = 0; i < op_len; i++)
1103     if (!kept[i])
1104       {
1105 	if (m_id)
1106 	  {
1107 	    if (!m_id->decl_map->get (m_oparms[i]))
1108 	      {
1109 		/* TODO: Perhaps at least aggregate-type params could re-use
1110 		   their isra_dummy_decl here?  */
1111 		tree var = copy_decl_to_var (m_oparms[i], m_id);
1112 		insert_decl_map (m_id, m_oparms[i], var);
1113 		/* Declare this new variable.  */
1114 		DECL_CHAIN (var) = *vars;
1115 		*vars = var;
1116 	      }
1117 	  }
1118 	else
1119 	  {
1120 	    m_removed_decls.safe_push (m_oparms[i]);
1121 	    m_removed_map.put (m_oparms[i], m_removed_decls.length () - 1);
1122 	  }
1123       }
1124 
1125   if (!MAY_HAVE_DEBUG_STMTS)
1126     return;
1127 
1128   /* Finally, when generating debug info, we fill vector m_reset_debug_decls
1129     with removed parameters declarations.  We do this in order to re-map their
1130     debug bind statements and create debug decls for them.  */
1131 
1132   if (tree_map)
1133     {
1134       /* Do not output debuginfo for parameter declarations as if they vanished
1135 	 when they were in fact replaced by a constant.  */
1136       auto_vec <int, 16> index_mapping;
1137       bool need_remap = false;
1138       clone_info *info = clone_info::get (m_id->src_node);
1139 
1140       if (m_id && info && info->param_adjustments)
1141 	{
1142 	  ipa_param_adjustments *prev_adjustments = info->param_adjustments;
1143 	  prev_adjustments->get_updated_indices (&index_mapping);
1144 	  need_remap = true;
1145 	}
1146 
1147       for (unsigned i = 0; i < tree_map->length (); i++)
1148 	{
1149 	  int parm_num = (*tree_map)[i]->parm_num;
1150 	  gcc_assert (parm_num >= 0);
1151 	  if (need_remap)
1152 	    parm_num = index_mapping[parm_num];
1153 	  kept[parm_num] = true;
1154 	}
1155     }
1156 
1157   for (unsigned i = 0; i < op_len; i++)
1158     if (!kept[i] && is_gimple_reg (m_oparms[i]))
1159       m_reset_debug_decls.safe_push (m_oparms[i]);
1160 }
1161 
1162 /* Constructor of ipa_param_body_adjustments from a simple list of
1163    modifications to parameters listed in ADJ_PARAMS which will prepare ground
1164    for modification of parameters of fndecl.  Return value of the function will
1165    not be removed and the object will assume it does not run as a part of
1166    tree-function_versioning.  */
1167 
1168 ipa_param_body_adjustments
ipa_param_body_adjustments(vec<ipa_adjusted_param,va_gc> * adj_params,tree fndecl)1169 ::ipa_param_body_adjustments (vec<ipa_adjusted_param, va_gc> *adj_params,
1170 			      tree fndecl)
1171   : m_adj_params (adj_params), m_adjustments (NULL), m_reset_debug_decls (),
1172     m_split_modifications_p (false), m_fndecl (fndecl), m_id (NULL),
1173     m_oparms (), m_new_decls (), m_new_types (), m_replacements (),
1174     m_removed_decls (), m_removed_map (), m_method2func (false)
1175 {
1176   common_initialization (fndecl, NULL, NULL);
1177 }
1178 
1179 /* Constructor of ipa_param_body_adjustments from ipa_param_adjustments in
1180    ADJUSTMENTS which will prepare ground for modification of parameters of
1181    fndecl.  The object will assume it does not run as a part of
1182    tree-function_versioning.  */
1183 
1184 ipa_param_body_adjustments
ipa_param_body_adjustments(ipa_param_adjustments * adjustments,tree fndecl)1185 ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
1186 			      tree fndecl)
1187   : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
1188     m_reset_debug_decls (), m_split_modifications_p (false), m_fndecl (fndecl),
1189     m_id (NULL), m_oparms (), m_new_decls (), m_new_types (),
1190     m_replacements (), m_removed_decls (), m_removed_map (),
1191     m_method2func (false)
1192 {
1193   common_initialization (fndecl, NULL, NULL);
1194 }
1195 
1196 /* Constructor of ipa_param_body_adjustments which sets it up as a part of
1197    running tree_function_versioning.  Planned modifications to the function are
1198    in ADJUSTMENTS.  FNDECL designates the new function clone which is being
1199    modified.  OLD_FNDECL is the function of which FNDECL is a clone (and which
1200    at the time of invocation still share DECL_ARGUMENTS).  ID is the
1201    copy_body_data structure driving the wholy body copying process.  VARS is a
1202    pointer to the head of the list of new local variables, TREE_MAP is the map
1203    that drives tree substitution in the cloning process.  */
1204 
1205 ipa_param_body_adjustments
ipa_param_body_adjustments(ipa_param_adjustments * adjustments,tree fndecl,tree old_fndecl,copy_body_data * id,tree * vars,vec<ipa_replace_map *,va_gc> * tree_map)1206 ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
1207 			      tree fndecl, tree old_fndecl,
1208 			      copy_body_data *id, tree *vars,
1209 			      vec<ipa_replace_map *, va_gc> *tree_map)
1210   : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
1211     m_reset_debug_decls (), m_split_modifications_p (false), m_fndecl (fndecl),
1212     m_id (id), m_oparms (), m_new_decls (), m_new_types (), m_replacements (),
1213     m_removed_decls (), m_removed_map (), m_method2func (false)
1214 {
1215   common_initialization (old_fndecl, vars, tree_map);
1216 }
1217 
1218 /* Chain new param decls up and return them.  */
1219 
1220 tree
get_new_param_chain()1221 ipa_param_body_adjustments::get_new_param_chain ()
1222 {
1223   tree result;
1224   tree *link = &result;
1225 
1226   unsigned len = vec_safe_length (m_adj_params);
1227   for (unsigned i = 0; i < len; i++)
1228     {
1229       tree new_decl = m_new_decls[i];
1230       *link = new_decl;
1231       link = &DECL_CHAIN (new_decl);
1232     }
1233   *link = NULL_TREE;
1234   return result;
1235 }
1236 
1237 /* Modify the function parameters FNDECL and its type according to the plan in
1238    ADJUSTMENTS.  This function needs to be called when the decl has not already
1239    been processed with ipa_param_adjustments::adjust_decl, otherwise just
1240    seting DECL_ARGUMENTS to whatever get_new_param_chain will do is enough.  */
1241 
1242 void
modify_formal_parameters()1243 ipa_param_body_adjustments::modify_formal_parameters ()
1244 {
1245   tree orig_type = TREE_TYPE (m_fndecl);
1246   DECL_ARGUMENTS (m_fndecl) = get_new_param_chain ();
1247 
1248   /* When signature changes, we need to clear builtin info.  */
1249   if (fndecl_built_in_p (m_fndecl))
1250     set_decl_built_in_function (m_fndecl, NOT_BUILT_IN, 0);
1251 
1252   /* At this point, removing return value is only implemented when going
1253      through tree_function_versioning, not when modifying function body
1254      directly.  */
1255   gcc_assert (!m_adjustments || !m_adjustments->m_skip_return);
1256   tree new_type = build_adjusted_function_type (orig_type, &m_new_types,
1257 						m_method2func, false);
1258 
1259   TREE_TYPE (m_fndecl) = new_type;
1260   DECL_VIRTUAL_P (m_fndecl) = 0;
1261   DECL_LANG_SPECIFIC (m_fndecl) = NULL;
1262   if (m_method2func)
1263     DECL_VINDEX (m_fndecl) = NULL_TREE;
1264 }
1265 
1266 /* Given BASE and UNIT_OFFSET, find the corresponding record among replacement
1267    structures.  */
1268 
1269 ipa_param_body_replacement *
lookup_replacement_1(tree base,unsigned unit_offset)1270 ipa_param_body_adjustments::lookup_replacement_1 (tree base,
1271 						  unsigned unit_offset)
1272 {
1273   unsigned int len = m_replacements.length ();
1274   for (unsigned i = 0; i < len; i++)
1275     {
1276       ipa_param_body_replacement *pbr = &m_replacements[i];
1277 
1278       if (pbr->base == base
1279 	  && (pbr->unit_offset == unit_offset))
1280 	return pbr;
1281     }
1282   return NULL;
1283 }
1284 
1285 /* Given BASE and UNIT_OFFSET, find the corresponding replacement expression
1286    and return it, assuming it is known it does not hold value by reference or
1287    in reverse storage order.  */
1288 
1289 tree
lookup_replacement(tree base,unsigned unit_offset)1290 ipa_param_body_adjustments::lookup_replacement (tree base, unsigned unit_offset)
1291 {
1292   ipa_param_body_replacement *pbr = lookup_replacement_1 (base, unit_offset);
1293   if (!pbr)
1294     return NULL;
1295   return pbr->repl;
1296 }
1297 
1298 /* If T is an SSA_NAME, return NULL if it is not a default def or
1299    return its base variable if it is.  If IGNORE_DEFAULT_DEF is true,
1300    the base variable is always returned, regardless if it is a default
1301    def.  Return T if it is not an SSA_NAME.  */
1302 
1303 static tree
get_ssa_base_param(tree t,bool ignore_default_def)1304 get_ssa_base_param (tree t, bool ignore_default_def)
1305 {
1306   if (TREE_CODE (t) == SSA_NAME)
1307     {
1308       if (ignore_default_def || SSA_NAME_IS_DEFAULT_DEF (t))
1309 	return SSA_NAME_VAR (t);
1310       else
1311 	return NULL_TREE;
1312     }
1313   return t;
1314 }
1315 
1316 /* Given an expression, return the structure describing how it should be
1317    replaced if it accesses a part of a split parameter or NULL otherwise.
1318 
1319    Do not free the result, it will be deallocated when the object is destroyed.
1320 
1321    If IGNORE_DEFAULT_DEF is cleared, consider only SSA_NAMEs of PARM_DECLs
1322    which are default definitions, if set, consider all SSA_NAMEs of
1323    PARM_DECLs.  */
1324 
1325 ipa_param_body_replacement *
get_expr_replacement(tree expr,bool ignore_default_def)1326 ipa_param_body_adjustments::get_expr_replacement (tree expr,
1327 						  bool ignore_default_def)
1328 {
1329   tree base;
1330   unsigned unit_offset;
1331 
1332   if (!isra_get_ref_base_and_offset (expr, &base, &unit_offset))
1333     return NULL;
1334 
1335   base = get_ssa_base_param (base, ignore_default_def);
1336   if (!base || TREE_CODE (base) != PARM_DECL)
1337     return NULL;
1338   return lookup_replacement_1 (base, unit_offset);
1339 }
1340 
1341 /* Given OLD_DECL, which is a PARM_DECL of a parameter that is being removed
1342    (which includes it being split or replaced), return a new variable that
1343    should be used for any SSA names that will remain in the function that
1344    previously belonged to OLD_DECL.  */
1345 
1346 tree
get_replacement_ssa_base(tree old_decl)1347 ipa_param_body_adjustments::get_replacement_ssa_base (tree old_decl)
1348 {
1349   unsigned *idx = m_removed_map.get (old_decl);
1350   if (!idx)
1351     return NULL;
1352 
1353   tree repl;
1354   if (TREE_CODE (m_removed_decls[*idx]) == PARM_DECL)
1355     {
1356       gcc_assert (m_removed_decls[*idx] == old_decl);
1357       repl = copy_var_decl (old_decl, DECL_NAME (old_decl),
1358 			    TREE_TYPE (old_decl));
1359       m_removed_decls[*idx] = repl;
1360     }
1361   else
1362     repl = m_removed_decls[*idx];
1363   return repl;
1364 }
1365 
1366 /* If OLD_NAME, which is being defined by statement STMT, is an SSA_NAME of a
1367    parameter which is to be removed because its value is not used, create a new
1368    SSA_NAME relating to a replacement VAR_DECL, replace all uses of the
1369    original with it and return it.  If there is no need to re-map, return NULL.
1370    ADJUSTMENTS is a pointer to a vector of IPA-SRA adjustments.  */
1371 
1372 tree
replace_removed_params_ssa_names(tree old_name,gimple * stmt)1373 ipa_param_body_adjustments::replace_removed_params_ssa_names (tree old_name,
1374 							      gimple *stmt)
1375 {
1376   gcc_assert (!m_id);
1377   if (TREE_CODE (old_name) != SSA_NAME)
1378     return NULL;
1379 
1380   tree decl = SSA_NAME_VAR (old_name);
1381   if (decl == NULL_TREE
1382       || TREE_CODE (decl) != PARM_DECL)
1383     return NULL;
1384 
1385   tree repl = get_replacement_ssa_base (decl);
1386   if (!repl)
1387     return NULL;
1388 
1389   tree new_name = make_ssa_name (repl, stmt);
1390   SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name)
1391     = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (old_name);
1392 
1393   if (dump_file && (dump_flags & TDF_DETAILS))
1394     {
1395       fprintf (dump_file, "replacing an SSA name of a removed param ");
1396       print_generic_expr (dump_file, old_name);
1397       fprintf (dump_file, " with ");
1398       print_generic_expr (dump_file, new_name);
1399       fprintf (dump_file, "\n");
1400     }
1401 
1402   replace_uses_by (old_name, new_name);
1403   return new_name;
1404 }
1405 
1406 /* If the expression *EXPR_P should be replaced, do so.  CONVERT specifies
1407    whether the function should care about type incompatibility of the current
1408    and new expressions.  If it is false, the function will leave
1409    incompatibility issues to the caller - note that when the function
1410    encounters a BIT_FIELD_REF, IMAGPART_EXPR or REALPART_EXPR, it will modify
1411    their bases instead of the expressions themselves and then also performs any
1412    necessary conversions.  */
1413 
1414 bool
modify_expression(tree * expr_p,bool convert)1415 ipa_param_body_adjustments::modify_expression (tree *expr_p, bool convert)
1416 {
1417   tree expr = *expr_p;
1418 
1419   if (TREE_CODE (expr) == BIT_FIELD_REF
1420       || TREE_CODE (expr) == IMAGPART_EXPR
1421       || TREE_CODE (expr) == REALPART_EXPR)
1422     {
1423       expr_p = &TREE_OPERAND (expr, 0);
1424       expr = *expr_p;
1425       convert = true;
1426     }
1427 
1428   ipa_param_body_replacement *pbr = get_expr_replacement (expr, false);
1429   if (!pbr)
1430     return false;
1431 
1432   tree repl = pbr->repl;
1433   if (dump_file && (dump_flags & TDF_DETAILS))
1434     {
1435       fprintf (dump_file, "About to replace expr ");
1436       print_generic_expr (dump_file, expr);
1437       fprintf (dump_file, " with ");
1438       print_generic_expr (dump_file, repl);
1439       fprintf (dump_file, "\n");
1440     }
1441 
1442   if (convert && !useless_type_conversion_p (TREE_TYPE (expr),
1443 					     TREE_TYPE (repl)))
1444     {
1445       tree vce = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expr), repl);
1446       *expr_p = vce;
1447     }
1448   else
1449     *expr_p = repl;
1450   return true;
1451 }
1452 
1453 /* If the assignment statement STMT contains any expressions that need to
1454    replaced with a different one as noted by ADJUSTMENTS, do so.  Handle any
1455    potential type incompatibilities.  If any conversion sttements have to be
1456    pre-pended to STMT, they will be added to EXTRA_STMTS.  Return true iff the
1457    statement was modified.  */
1458 
1459 bool
modify_assignment(gimple * stmt,gimple_seq * extra_stmts)1460 ipa_param_body_adjustments::modify_assignment (gimple *stmt,
1461 					       gimple_seq *extra_stmts)
1462 {
1463   tree *lhs_p, *rhs_p;
1464   bool any;
1465 
1466   if (!gimple_assign_single_p (stmt))
1467     return false;
1468 
1469   rhs_p = gimple_assign_rhs1_ptr (stmt);
1470   lhs_p = gimple_assign_lhs_ptr (stmt);
1471 
1472   any = modify_expression (lhs_p, false);
1473   any |= modify_expression (rhs_p, false);
1474   if (any
1475       && !useless_type_conversion_p (TREE_TYPE (*lhs_p), TREE_TYPE (*rhs_p)))
1476     {
1477       if (TREE_CODE (*rhs_p) == CONSTRUCTOR)
1478 	{
1479 	  /* V_C_Es of constructors can cause trouble (PR 42714).  */
1480 	  if (is_gimple_reg_type (TREE_TYPE (*lhs_p)))
1481 	    *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
1482 	  else
1483 	    *rhs_p = build_constructor (TREE_TYPE (*lhs_p),
1484 					NULL);
1485 	}
1486       else
1487 	{
1488 	  tree new_rhs = fold_build1_loc (gimple_location (stmt),
1489 					  VIEW_CONVERT_EXPR, TREE_TYPE (*lhs_p),
1490 					  *rhs_p);
1491 	  tree tmp = force_gimple_operand (new_rhs, extra_stmts, true,
1492 					   NULL_TREE);
1493 	  gimple_assign_set_rhs1 (stmt, tmp);
1494 	}
1495       return true;
1496     }
1497 
1498   return any;
1499 }
1500 
1501 /* Data passed to remap_split_decl_to_dummy through walk_tree.  */
1502 
1503 struct simple_tree_swap_info
1504 {
1505   /* Change FROM to TO.  */
1506   tree from, to;
1507   /* And set DONE to true when doing so.  */
1508   bool done;
1509 };
1510 
1511 /* Simple remapper to remap a split parameter to the same expression based on a
1512    special dummy decl so that edge redirections can detect transitive splitting
1513    and finish them.  */
1514 
1515 static tree
remap_split_decl_to_dummy(tree * tp,int * walk_subtrees,void * data)1516 remap_split_decl_to_dummy (tree *tp, int *walk_subtrees, void *data)
1517 {
1518   tree t = *tp;
1519 
1520   if (DECL_P (t) || TREE_CODE (t) == SSA_NAME)
1521     {
1522       struct simple_tree_swap_info *swapinfo
1523 	= (struct simple_tree_swap_info *) data;
1524       if (t == swapinfo->from
1525 	  || (TREE_CODE (t) == SSA_NAME
1526 	      && SSA_NAME_VAR (t) == swapinfo->from))
1527 	{
1528 	  *tp = swapinfo->to;
1529 	  swapinfo->done = true;
1530 	}
1531       *walk_subtrees = 0;
1532     }
1533   else if (TYPE_P (t))
1534       *walk_subtrees = 0;
1535   else
1536     *walk_subtrees = 1;
1537   return NULL_TREE;
1538 }
1539 
1540 
1541 /* If the call statement pointed at by STMT_P contains any expressions that
1542    need to replaced with a different one as noted by ADJUSTMENTS, do so.  f the
1543    statement needs to be rebuilt, do so.  Return true if any modifications have
1544    been performed.
1545 
1546    If the method is invoked as a part of IPA clone materialization and if any
1547    parameter split is transitive, i.e. it applies to the functin that is being
1548    modified and also to the callee of the statement, replace the parameter
1549    passed to old callee with an equivalent expression based on a dummy decl
1550    followed by PARM_DECLs representing the actual replacements.  The actual
1551    replacements will be then converted into SSA_NAMEs and then
1552    ipa_param_adjustments::modify_call will find the appropriate ones and leave
1553    only those in the call.  */
1554 
1555 bool
modify_call_stmt(gcall ** stmt_p)1556 ipa_param_body_adjustments::modify_call_stmt (gcall **stmt_p)
1557 {
1558   gcall *stmt = *stmt_p;
1559   auto_vec <unsigned, 4> pass_through_args;
1560   auto_vec <unsigned, 4> pass_through_pbr_indices;
1561 
1562   if (m_split_modifications_p && m_id)
1563     {
1564       for (unsigned i = 0; i < gimple_call_num_args (stmt); i++)
1565 	{
1566 	  tree t = gimple_call_arg (stmt, i);
1567 	  gcc_assert (TREE_CODE (t) != BIT_FIELD_REF
1568 		      && TREE_CODE (t) != IMAGPART_EXPR
1569 		      && TREE_CODE (t) != REALPART_EXPR);
1570 
1571 	  tree base;
1572 	  unsigned unit_offset;
1573 	  if (!isra_get_ref_base_and_offset (t, &base, &unit_offset))
1574 	    continue;
1575 
1576 	  bool by_ref = false;
1577 	  if (TREE_CODE (base) == SSA_NAME)
1578 	    {
1579 	      if (!SSA_NAME_IS_DEFAULT_DEF (base))
1580 		continue;
1581 	      base = SSA_NAME_VAR (base);
1582 	      gcc_checking_assert (base);
1583 	      by_ref = true;
1584 	    }
1585 	  if (TREE_CODE (base) != PARM_DECL)
1586 	    continue;
1587 
1588 	  bool base_among_replacements = false;
1589 	  unsigned j, repl_list_len = m_replacements.length ();
1590 	  for (j = 0; j < repl_list_len; j++)
1591 	    {
1592 	      ipa_param_body_replacement *pbr = &m_replacements[j];
1593 	      if (pbr->base == base)
1594 		{
1595 		  base_among_replacements = true;
1596 		  break;
1597 		}
1598 	    }
1599 	  if (!base_among_replacements)
1600 	    continue;
1601 
1602 	  /* We still have to distinguish between an end-use that we have to
1603 	     transform now and a pass-through, which happens in the following
1604 	     two cases.  */
1605 
1606 	  /* TODO: After we adjust ptr_parm_has_nonarg_uses to also consider
1607 	     &MEM_REF[ssa_name + offset], we will also have to detect that case
1608 	     here.    */
1609 
1610 	  if (TREE_CODE (t) == SSA_NAME
1611 	      && SSA_NAME_IS_DEFAULT_DEF (t)
1612 	      && SSA_NAME_VAR (t)
1613 	      && TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL)
1614 	    {
1615 	      /* This must be a by_reference pass-through.  */
1616 	      gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
1617 	      pass_through_args.safe_push (i);
1618 	      pass_through_pbr_indices.safe_push (j);
1619 	    }
1620 	  else if (!by_ref && AGGREGATE_TYPE_P (TREE_TYPE (t)))
1621 	    {
1622 	      /* Currently IPA-SRA guarantees the aggregate access type
1623 		 exactly matches in this case.  So if it does not match, it is
1624 		 a pass-through argument that will be sorted out at edge
1625 		 redirection time.  */
1626 	      ipa_param_body_replacement *pbr
1627 		= lookup_replacement_1 (base, unit_offset);
1628 
1629 	      if (!pbr
1630 		  || (TYPE_MAIN_VARIANT (TREE_TYPE (t))
1631 		      != TYPE_MAIN_VARIANT (TREE_TYPE (pbr->repl))))
1632 		{
1633 		  pass_through_args.safe_push (i);
1634 		  pass_through_pbr_indices.safe_push (j);
1635 		}
1636 	    }
1637 	}
1638     }
1639 
1640   unsigned nargs = gimple_call_num_args (stmt);
1641   if (!pass_through_args.is_empty ())
1642     {
1643       auto_vec<tree, 16> vargs;
1644       unsigned pt_idx = 0;
1645       for (unsigned i = 0; i < nargs; i++)
1646 	{
1647 	  if (pt_idx < pass_through_args.length ()
1648 	      && i == pass_through_args[pt_idx])
1649 	    {
1650 	      unsigned j = pass_through_pbr_indices[pt_idx];
1651 	      pt_idx++;
1652 	      tree base = m_replacements[j].base;
1653 
1654 	      /* Map base will get mapped to the special transitive-isra marker
1655 		 dummy decl. */
1656 	      struct simple_tree_swap_info swapinfo;
1657 	      swapinfo.from = base;
1658 	      swapinfo.to = m_replacements[j].dummy;
1659 	      swapinfo.done = false;
1660 	      tree arg = gimple_call_arg (stmt, i);
1661 	      walk_tree (&arg, remap_split_decl_to_dummy, &swapinfo, NULL);
1662 	      gcc_assert (swapinfo.done);
1663 	      vargs.safe_push (arg);
1664 	      /* Now let's push all replacements pertaining to this parameter
1665 		 so that all gimple register ones get correct SSA_NAMES.  Edge
1666 		 redirection will weed out the dummy argument as well as all
1667 		 unused replacements later.  */
1668 	      unsigned int repl_list_len = m_replacements.length ();
1669 	      for (; j < repl_list_len; j++)
1670 		{
1671 		  if (m_replacements[j].base != base)
1672 		    break;
1673 		  vargs.safe_push (m_replacements[j].repl);
1674 		}
1675 	    }
1676 	  else
1677 	    {
1678 	      tree t = gimple_call_arg (stmt, i);
1679 	      modify_expression (&t, true);
1680 	      vargs.safe_push (t);
1681 	    }
1682 	}
1683       gcall *new_stmt = gimple_build_call_vec (gimple_call_fn (stmt), vargs);
1684       if (gimple_has_location (stmt))
1685 	gimple_set_location (new_stmt, gimple_location (stmt));
1686       gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
1687       gimple_call_copy_flags (new_stmt, stmt);
1688       if (tree lhs = gimple_call_lhs (stmt))
1689 	{
1690 	  modify_expression (&lhs, false);
1691 	  /* Avoid adjusting SSA_NAME_DEF_STMT of a SSA lhs, SSA names
1692 	     have not yet been remapped.  */
1693 	  *gimple_call_lhs_ptr (new_stmt) = lhs;
1694 	}
1695       *stmt_p = new_stmt;
1696       return true;
1697     }
1698 
1699   /* Otherwise, no need to rebuild the statement, let's just modify arguments
1700      and the LHS if/as appropriate.  */
1701   bool modified = false;
1702   for (unsigned i = 0; i < nargs; i++)
1703     {
1704       tree *t = gimple_call_arg_ptr (stmt, i);
1705       modified |= modify_expression (t, true);
1706     }
1707 
1708   if (gimple_call_lhs (stmt))
1709     {
1710       tree *t = gimple_call_lhs_ptr (stmt);
1711       modified |= modify_expression (t, false);
1712     }
1713 
1714   return modified;
1715 }
1716 
1717 /* If the statement STMT contains any expressions that need to replaced with a
1718    different one as noted by ADJUSTMENTS, do so.  Handle any potential type
1719    incompatibilities.  If any conversion sttements have to be pre-pended to
1720    STMT, they will be added to EXTRA_STMTS.  Return true iff the statement was
1721    modified.  */
1722 
1723 bool
modify_gimple_stmt(gimple ** stmt,gimple_seq * extra_stmts)1724 ipa_param_body_adjustments::modify_gimple_stmt (gimple **stmt,
1725 						gimple_seq *extra_stmts)
1726 {
1727   bool modified = false;
1728   tree *t;
1729 
1730   switch (gimple_code (*stmt))
1731     {
1732     case GIMPLE_RETURN:
1733       t = gimple_return_retval_ptr (as_a <greturn *> (*stmt));
1734       if (m_adjustments && m_adjustments->m_skip_return)
1735 	*t = NULL_TREE;
1736       else if (*t != NULL_TREE)
1737 	modified |= modify_expression (t, true);
1738       break;
1739 
1740     case GIMPLE_ASSIGN:
1741       modified |= modify_assignment (*stmt, extra_stmts);
1742       break;
1743 
1744     case GIMPLE_CALL:
1745       modified |= modify_call_stmt ((gcall **) stmt);
1746       break;
1747 
1748     case GIMPLE_ASM:
1749       {
1750 	gasm *asm_stmt = as_a <gasm *> (*stmt);
1751 	for (unsigned i = 0; i < gimple_asm_ninputs (asm_stmt); i++)
1752 	  {
1753 	    t = &TREE_VALUE (gimple_asm_input_op (asm_stmt, i));
1754 	    modified |= modify_expression (t, true);
1755 	  }
1756 	for (unsigned i = 0; i < gimple_asm_noutputs (asm_stmt); i++)
1757 	  {
1758 	    t = &TREE_VALUE (gimple_asm_output_op (asm_stmt, i));
1759 	    modified |= modify_expression (t, false);
1760 	  }
1761       }
1762       break;
1763 
1764     default:
1765       break;
1766     }
1767   return modified;
1768 }
1769 
1770 
1771 /* Traverse body of the current function and perform the requested adjustments
1772    on its statements.  Return true iff the CFG has been changed.  */
1773 
1774 bool
modify_cfun_body()1775 ipa_param_body_adjustments::modify_cfun_body ()
1776 {
1777   bool cfg_changed = false;
1778   basic_block bb;
1779 
1780   FOR_EACH_BB_FN (bb, cfun)
1781     {
1782       gimple_stmt_iterator gsi;
1783 
1784       for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1785 	{
1786 	  gphi *phi = as_a <gphi *> (gsi_stmt (gsi));
1787 	  tree new_lhs, old_lhs = gimple_phi_result (phi);
1788 	  new_lhs = replace_removed_params_ssa_names (old_lhs, phi);
1789 	  if (new_lhs)
1790 	    {
1791 	      gimple_phi_set_result (phi, new_lhs);
1792 	      release_ssa_name (old_lhs);
1793 	    }
1794 	}
1795 
1796       gsi = gsi_start_bb (bb);
1797       while (!gsi_end_p (gsi))
1798 	{
1799 	  gimple *stmt = gsi_stmt (gsi);
1800 	  gimple *stmt_copy = stmt;
1801 	  gimple_seq extra_stmts = NULL;
1802 	  bool modified = modify_gimple_stmt (&stmt, &extra_stmts);
1803 	  if (stmt != stmt_copy)
1804 	    {
1805 	      gcc_checking_assert (modified);
1806 	      gsi_replace (&gsi, stmt, false);
1807 	    }
1808 	  if (!gimple_seq_empty_p (extra_stmts))
1809 	    gsi_insert_seq_before (&gsi, extra_stmts, GSI_SAME_STMT);
1810 
1811 	  def_operand_p defp;
1812 	  ssa_op_iter iter;
1813 	  FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF)
1814 	    {
1815 	      tree old_def = DEF_FROM_PTR (defp);
1816 	      if (tree new_def = replace_removed_params_ssa_names (old_def,
1817 								   stmt))
1818 		{
1819 		  SET_DEF (defp, new_def);
1820 		  release_ssa_name (old_def);
1821 		  modified = true;
1822 		}
1823 	    }
1824 
1825 	  if (modified)
1826 	    {
1827 	      update_stmt (stmt);
1828 	      if (maybe_clean_eh_stmt (stmt)
1829 		  && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
1830 		cfg_changed = true;
1831 	    }
1832 	  gsi_next (&gsi);
1833 	}
1834     }
1835 
1836   return cfg_changed;
1837 }
1838 
1839 /* Call gimple_debug_bind_reset_value on all debug statements describing
1840    gimple register parameters that are being removed or replaced.  */
1841 
1842 void
reset_debug_stmts()1843 ipa_param_body_adjustments::reset_debug_stmts ()
1844 {
1845   int i, len;
1846   gimple_stmt_iterator *gsip = NULL, gsi;
1847 
1848   if (MAY_HAVE_DEBUG_STMTS && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
1849     {
1850       gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1851       gsip = &gsi;
1852     }
1853   len = m_reset_debug_decls.length ();
1854   for (i = 0; i < len; i++)
1855     {
1856       imm_use_iterator ui;
1857       gimple *stmt;
1858       gdebug *def_temp;
1859       tree name, vexpr, copy = NULL_TREE;
1860       use_operand_p use_p;
1861       tree decl = m_reset_debug_decls[i];
1862 
1863       gcc_checking_assert (is_gimple_reg (decl));
1864       name = ssa_default_def (cfun, decl);
1865       vexpr = NULL;
1866       if (name)
1867 	FOR_EACH_IMM_USE_STMT (stmt, ui, name)
1868 	  {
1869 	    if (gimple_clobber_p (stmt))
1870 	      {
1871 		gimple_stmt_iterator cgsi = gsi_for_stmt (stmt);
1872 		unlink_stmt_vdef (stmt);
1873 		gsi_remove (&cgsi, true);
1874 		release_defs (stmt);
1875 		continue;
1876 	      }
1877 	    /* All other users must have been removed by function body
1878 	       modification.  */
1879 	    gcc_assert (is_gimple_debug (stmt));
1880 	    if (vexpr == NULL && gsip != NULL)
1881 	      {
1882 		vexpr = make_node (DEBUG_EXPR_DECL);
1883 		def_temp = gimple_build_debug_source_bind (vexpr, decl, NULL);
1884 		DECL_ARTIFICIAL (vexpr) = 1;
1885 		TREE_TYPE (vexpr) = TREE_TYPE (name);
1886 		SET_DECL_MODE (vexpr, DECL_MODE (decl));
1887 		gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
1888 	      }
1889 	    if (vexpr)
1890 	      {
1891 		FOR_EACH_IMM_USE_ON_STMT (use_p, ui)
1892 		  SET_USE (use_p, vexpr);
1893 	      }
1894 	    else
1895 	      gimple_debug_bind_reset_value (stmt);
1896 	    update_stmt (stmt);
1897 	  }
1898       /* Create a VAR_DECL for debug info purposes.  */
1899       if (!DECL_IGNORED_P (decl))
1900 	{
1901 	  copy = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1902 			     VAR_DECL, DECL_NAME (decl),
1903 			     TREE_TYPE (decl));
1904 	  if (DECL_PT_UID_SET_P (decl))
1905 	    SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
1906 	  TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
1907 	  TREE_READONLY (copy) = TREE_READONLY (decl);
1908 	  TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
1909 	  DECL_NOT_GIMPLE_REG_P (copy) = DECL_NOT_GIMPLE_REG_P (decl);
1910 	  DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
1911 	  DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
1912 	  DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
1913 	  DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
1914 	  SET_DECL_RTL (copy, 0);
1915 	  TREE_USED (copy) = 1;
1916 	  DECL_CONTEXT (copy) = current_function_decl;
1917 	  add_local_decl (cfun, copy);
1918 	  DECL_CHAIN (copy)
1919 	    = BLOCK_VARS (DECL_INITIAL (current_function_decl));
1920 	  BLOCK_VARS (DECL_INITIAL (current_function_decl)) = copy;
1921 	}
1922       if (gsip != NULL && copy && target_for_debug_bind (decl))
1923 	{
1924 	  gcc_assert (TREE_CODE (decl) == PARM_DECL);
1925 	  if (vexpr)
1926 	    def_temp = gimple_build_debug_bind (copy, vexpr, NULL);
1927 	  else
1928 	    def_temp = gimple_build_debug_source_bind (copy, decl,
1929 						       NULL);
1930 	  gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
1931 	}
1932     }
1933 }
1934 
1935 /* Perform all necessary body changes to change signature, body and debug info
1936    of fun according to adjustments passed at construction.  Return true if CFG
1937    was changed in any way.  The main entry point for modification of standalone
1938    functions that is not part of IPA clone materialization.  */
1939 
1940 bool
perform_cfun_body_modifications()1941 ipa_param_body_adjustments::perform_cfun_body_modifications ()
1942 {
1943   bool cfg_changed;
1944   modify_formal_parameters ();
1945   cfg_changed = modify_cfun_body ();
1946   reset_debug_stmts ();
1947 
1948   return cfg_changed;
1949 }
1950 
1951