c++: new-expression is potentially constant in C++20
[official-gcc.git] / gcc / ipa-param-manipulation.cc
blob23a8cb84d487252472fae27f0495ce75aaa3caa3
1 /* Manipulation of formal and actual parameters of functions and function
2 calls.
3 Copyright (C) 2017-2022 Free Software Foundation, Inc.
5 This file is part of GCC.
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.
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.
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/>. */
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 #include "tree-phinodes.h"
47 #include "cfgexpand.h"
48 #include "attribs.h"
51 /* Actual prefixes of different newly synthetized parameters. Keep in sync
52 with IPA_PARAM_PREFIX_* defines. */
54 static const char *ipa_param_prefixes[IPA_PARAM_PREFIX_COUNT]
55 = {"SYNTH",
56 "ISRA",
57 "simd",
58 "mask"};
60 /* Names of parameters for dumping. Keep in sync with enum ipa_parm_op. */
62 static const char *ipa_param_op_names[IPA_PARAM_PREFIX_COUNT]
63 = {"IPA_PARAM_OP_UNDEFINED",
64 "IPA_PARAM_OP_COPY",
65 "IPA_PARAM_OP_NEW",
66 "IPA_PARAM_OP_SPLIT"};
68 /* Structure to hold declarations representing pass-through IPA-SRA splits. In
69 essence, it tells new index for a combination of original index and
70 offset. */
72 struct pass_through_split_map
74 /* Original argument index. */
75 unsigned base_index;
76 /* Offset of the split part in the original argument. */
77 unsigned unit_offset;
78 /* Index of the split part in the call statement - where clone
79 materialization put it. */
80 int new_index;
83 /* Information about some call statements that needs to be conveyed from clone
84 materialization to edge redirection. */
86 class ipa_edge_modification_info
88 public:
89 ipa_edge_modification_info ()
92 /* Mapping of original argument indices to where those arguments sit in the
93 call statement now or to a negative index if they were removed. */
94 auto_vec<int> index_map;
95 /* Information about ISRA replacements put into the call statement at the
96 clone materialization stages. */
97 auto_vec<pass_through_split_map> pass_through_map;
98 /* Necessary adjustment to ipa_param_adjustments::m_always_copy_start when
99 redirecting the call. */
100 int always_copy_delta = 0;
103 /* Class for storing and retrieving summaries about cal statement
104 modifications. */
106 class ipa_edge_modification_sum
107 : public call_summary <ipa_edge_modification_info *>
109 public:
110 ipa_edge_modification_sum (symbol_table *table)
111 : call_summary<ipa_edge_modification_info *> (table)
115 /* Hook that is called by summary when an edge is duplicated. */
117 void duplicate (cgraph_edge *,
118 cgraph_edge *,
119 ipa_edge_modification_info *old_info,
120 ipa_edge_modification_info *new_info) final override
122 new_info->index_map.safe_splice (old_info->index_map);
123 new_info->pass_through_map.safe_splice (old_info->pass_through_map);
124 new_info->always_copy_delta = old_info->always_copy_delta;
128 /* Call summary to store information about edges which have had their arguments
129 partially modified already. */
131 static ipa_edge_modification_sum *ipa_edge_modifications;
133 /* Fail compilation if CS has any summary associated with it in
134 ipa_edge_modifications. */
136 DEBUG_FUNCTION void
137 ipa_verify_edge_has_no_modifications (cgraph_edge *cs)
139 gcc_assert (!ipa_edge_modifications || !ipa_edge_modifications->get (cs));
142 /* Fill an empty vector ARGS with PARM_DECLs representing formal parameters of
143 FNDECL. The function should not be called during LTO WPA phase except for
144 thunks (or functions with bodies streamed in). */
146 void
147 push_function_arg_decls (vec<tree> *args, tree fndecl)
149 int count;
150 tree parm;
152 /* Safety check that we do not attempt to use the function in WPA, except
153 when the function is a thunk and then we have DECL_ARGUMENTS or when we
154 have already explicitely loaded its body. */
155 gcc_assert (!flag_wpa
156 || DECL_ARGUMENTS (fndecl)
157 || gimple_has_body_p (fndecl));
158 count = 0;
159 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
160 count++;
162 args->reserve_exact (count);
163 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
164 args->quick_push (parm);
167 /* Fill an empty vector TYPES with trees representing formal parameters of
168 function type FNTYPE. */
170 void
171 push_function_arg_types (vec<tree> *types, tree fntype)
173 int count = 0;
174 tree t;
176 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
177 count++;
179 types->reserve_exact (count);
180 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
181 types->quick_push (TREE_VALUE (t));
184 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
185 friendly way, assuming they are meant to be applied to FNDECL. */
187 void
188 ipa_dump_adjusted_parameters (FILE *f,
189 vec<ipa_adjusted_param, va_gc> *adj_params)
191 unsigned i, len = vec_safe_length (adj_params);
192 bool first = true;
194 if (!len)
195 return;
197 fprintf (f, " IPA adjusted parameters: ");
198 for (i = 0; i < len; i++)
200 struct ipa_adjusted_param *apm;
201 apm = &(*adj_params)[i];
203 if (!first)
204 fprintf (f, " ");
205 else
206 first = false;
208 fprintf (f, "%i. %s %s", i, ipa_param_op_names[apm->op],
209 apm->prev_clone_adjustment ? "prev_clone_adjustment " : "");
210 switch (apm->op)
212 case IPA_PARAM_OP_UNDEFINED:
213 break;
215 case IPA_PARAM_OP_COPY:
216 fprintf (f, ", base_index: %u", apm->base_index);
217 fprintf (f, ", prev_clone_index: %u", apm->prev_clone_index);
218 break;
220 case IPA_PARAM_OP_SPLIT:
221 fprintf (f, ", offset: %u", apm->unit_offset);
222 /* fall-through */
223 case IPA_PARAM_OP_NEW:
224 fprintf (f, ", base_index: %u", apm->base_index);
225 fprintf (f, ", prev_clone_index: %u", apm->prev_clone_index);
226 print_node_brief (f, ", type: ", apm->type, 0);
227 print_node_brief (f, ", alias type: ", apm->alias_ptr_type, 0);
228 fprintf (f, " prefix: %s",
229 ipa_param_prefixes[apm->param_prefix_index]);
230 if (apm->reverse)
231 fprintf (f, ", reverse");
232 break;
234 fprintf (f, "\n");
238 /* Fill NEW_TYPES with types of a function after its current OTYPES have been
239 modified as described in ADJ_PARAMS. When USE_PREV_INDICES is true, use
240 prev_clone_index from ADJ_PARAMS as opposed to base_index when the parameter
241 is false. */
243 static void
244 fill_vector_of_new_param_types (vec<tree> *new_types, vec<tree> *otypes,
245 vec<ipa_adjusted_param, va_gc> *adj_params,
246 bool use_prev_indices)
248 unsigned adj_len = vec_safe_length (adj_params);
249 new_types->reserve_exact (adj_len);
250 for (unsigned i = 0; i < adj_len ; i++)
252 ipa_adjusted_param *apm = &(*adj_params)[i];
253 if (apm->op == IPA_PARAM_OP_COPY)
255 unsigned index
256 = use_prev_indices ? apm->prev_clone_index : apm->base_index;
257 /* The following needs to be handled gracefully because of type
258 mismatches. This happens with LTO but apparently also in Fortran
259 with -fcoarray=lib -O2 -lcaf_single -latomic. */
260 if (index >= otypes->length ())
261 continue;
262 new_types->quick_push ((*otypes)[index]);
264 else if (apm->op == IPA_PARAM_OP_NEW
265 || apm->op == IPA_PARAM_OP_SPLIT)
267 tree ntype = apm->type;
268 if (is_gimple_reg_type (ntype)
269 && TYPE_MODE (ntype) != BLKmode)
271 unsigned malign = GET_MODE_ALIGNMENT (TYPE_MODE (ntype));
272 if (TYPE_ALIGN (ntype) != malign)
273 ntype = build_aligned_type (ntype, malign);
275 new_types->quick_push (ntype);
277 else
278 gcc_unreachable ();
282 /* Return false if given attribute should prevent type adjustments. */
284 bool
285 ipa_param_adjustments::type_attribute_allowed_p (tree name)
287 if ((is_attribute_p ("fn spec", name) && flag_ipa_modref)
288 || is_attribute_p ("access", name)
289 || is_attribute_p ("returns_nonnull", name)
290 || is_attribute_p ("assume_aligned", name)
291 || is_attribute_p ("nocf_check", name)
292 || is_attribute_p ("warn_unused_result", name))
293 return true;
294 return false;
297 /* Return true if attribute should be dropped if parameter changed. */
299 static bool
300 drop_type_attribute_if_params_changed_p (tree name)
302 if (is_attribute_p ("fn spec", name)
303 || is_attribute_p ("access", name))
304 return true;
305 return false;
308 /* Build and return a function type just like ORIG_TYPE but with parameter
309 types given in NEW_PARAM_TYPES - which can be NULL if, but only if,
310 ORIG_TYPE itself has NULL TREE_ARG_TYPEs. If METHOD2FUNC is true, also make
311 it a FUNCTION_TYPE instead of FUNCTION_TYPE.
312 If ARG_MODIFIED is true drop attributes that are no longer up to date. */
314 static tree
315 build_adjusted_function_type (tree orig_type, vec<tree> *new_param_types,
316 bool method2func, bool skip_return,
317 bool args_modified)
319 tree new_arg_types = NULL;
320 if (TYPE_ARG_TYPES (orig_type))
322 gcc_checking_assert (new_param_types);
323 bool last_parm_void = (TREE_VALUE (tree_last (TYPE_ARG_TYPES (orig_type)))
324 == void_type_node);
325 unsigned len = new_param_types->length ();
326 for (unsigned i = 0; i < len; i++)
327 new_arg_types = tree_cons (NULL_TREE, (*new_param_types)[i],
328 new_arg_types);
330 tree new_reversed = nreverse (new_arg_types);
331 if (last_parm_void)
333 if (new_reversed)
334 TREE_CHAIN (new_arg_types) = void_list_node;
335 else
336 new_reversed = void_list_node;
338 new_arg_types = new_reversed;
341 /* Use build_distinct_type_copy to preserve as much as possible from original
342 type (debug info, attribute lists etc.). The one exception is
343 METHOD_TYPEs which must have THIS argument and when we are asked to remove
344 it, we need to build new FUNCTION_TYPE instead. */
345 tree new_type = NULL;
346 if (method2func)
348 tree ret_type;
349 if (skip_return)
350 ret_type = void_type_node;
351 else
352 ret_type = TREE_TYPE (orig_type);
354 new_type
355 = build_distinct_type_copy (build_function_type (ret_type,
356 new_arg_types));
357 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
359 else
361 new_type = build_distinct_type_copy (orig_type);
362 TYPE_ARG_TYPES (new_type) = new_arg_types;
363 if (skip_return)
364 TREE_TYPE (new_type) = void_type_node;
366 if (args_modified && TYPE_ATTRIBUTES (new_type))
368 tree t = TYPE_ATTRIBUTES (new_type);
369 tree *last = &TYPE_ATTRIBUTES (new_type);
370 TYPE_ATTRIBUTES (new_type) = NULL;
371 for (;t; t = TREE_CHAIN (t))
372 if (!drop_type_attribute_if_params_changed_p
373 (get_attribute_name (t)))
375 *last = copy_node (t);
376 TREE_CHAIN (*last) = NULL;
377 last = &TREE_CHAIN (*last);
381 return new_type;
384 /* Return the maximum index in any IPA_PARAM_OP_COPY adjustment or -1 if there
385 is none. */
388 ipa_param_adjustments::get_max_base_index ()
390 unsigned adj_len = vec_safe_length (m_adj_params);
391 int max_index = -1;
392 for (unsigned i = 0; i < adj_len ; i++)
394 ipa_adjusted_param *apm = &(*m_adj_params)[i];
395 if (apm->op == IPA_PARAM_OP_COPY
396 && max_index < apm->base_index)
397 max_index = apm->base_index;
399 return max_index;
403 /* Fill SURVIVING_PARAMS with an array of bools where each one says whether a
404 parameter that originally was at that position still survives in the given
405 clone or is removed/replaced. If the final array is smaller than an index
406 of an original parameter, that parameter also did not survive. That a
407 parameter survives does not mean it has the same index as before. */
409 void
410 ipa_param_adjustments::get_surviving_params (vec<bool> *surviving_params)
412 unsigned adj_len = vec_safe_length (m_adj_params);
413 int max_index = get_max_base_index ();
415 if (max_index < 0)
416 return;
417 surviving_params->reserve_exact (max_index + 1);
418 surviving_params->quick_grow_cleared (max_index + 1);
419 for (unsigned i = 0; i < adj_len ; i++)
421 ipa_adjusted_param *apm = &(*m_adj_params)[i];
422 if (apm->op == IPA_PARAM_OP_COPY)
423 (*surviving_params)[apm->base_index] = true;
427 /* Fill NEW_INDICES with new indices of each surviving parameter or -1 for
428 those which do not survive. Any parameter outside of lenght of the vector
429 does not survive. There is currently no support for a parameter to be
430 copied to two distinct new parameters. */
432 void
433 ipa_param_adjustments::get_updated_indices (vec<int> *new_indices)
435 unsigned adj_len = vec_safe_length (m_adj_params);
436 int max_index = get_max_base_index ();
438 if (max_index < 0)
439 return;
440 unsigned res_len = max_index + 1;
441 new_indices->reserve_exact (res_len);
442 for (unsigned i = 0; i < res_len ; i++)
443 new_indices->quick_push (-1);
444 for (unsigned i = 0; i < adj_len ; i++)
446 ipa_adjusted_param *apm = &(*m_adj_params)[i];
447 if (apm->op == IPA_PARAM_OP_COPY)
448 (*new_indices)[apm->base_index] = i;
452 /* If a parameter with original INDEX has survived intact, return its new
453 index. Otherwise return -1. In that case, if it has been split and there
454 is a new parameter representing a portion at unit OFFSET for which a value
455 of a TYPE can be substituted, store its new index into SPLIT_INDEX,
456 otherwise store -1 there. */
458 ipa_param_adjustments::get_updated_index_or_split (int index,
459 unsigned unit_offset,
460 tree type, int *split_index)
462 unsigned adj_len = vec_safe_length (m_adj_params);
463 for (unsigned i = 0; i < adj_len ; i++)
465 ipa_adjusted_param *apm = &(*m_adj_params)[i];
466 if (apm->base_index != index)
467 continue;
468 if (apm->op == IPA_PARAM_OP_COPY)
469 return i;
470 if (apm->op == IPA_PARAM_OP_SPLIT
471 && apm->unit_offset == unit_offset)
473 if (useless_type_conversion_p (apm->type, type))
474 *split_index = i;
475 else
476 *split_index = -1;
477 return -1;
481 *split_index = -1;
482 return -1;
485 /* Return the original index for the given new parameter index. Return a
486 negative number if not available. */
489 ipa_param_adjustments::get_original_index (int newidx)
491 const ipa_adjusted_param *adj = &(*m_adj_params)[newidx];
492 if (adj->op != IPA_PARAM_OP_COPY)
493 return -1;
494 return adj->base_index;
497 /* Return true if the first parameter (assuming there was one) survives the
498 transformation intact and remains the first one. */
500 bool
501 ipa_param_adjustments::first_param_intact_p ()
503 return (!vec_safe_is_empty (m_adj_params)
504 && (*m_adj_params)[0].op == IPA_PARAM_OP_COPY
505 && (*m_adj_params)[0].base_index == 0);
508 /* Return true if we have to change what has formerly been a method into a
509 function. */
511 bool
512 ipa_param_adjustments::method2func_p (tree orig_type)
514 return ((TREE_CODE (orig_type) == METHOD_TYPE) && !first_param_intact_p ());
517 /* Given function type OLD_TYPE, return a new type derived from it after
518 performing all atored modifications. TYPE_ORIGINAL_P should be true when
519 OLD_TYPE refers to the type before any IPA transformations, as opposed to a
520 type that can be an intermediate one in between various IPA
521 transformations. */
523 tree
524 ipa_param_adjustments::build_new_function_type (tree old_type,
525 bool type_original_p)
527 auto_vec<tree,16> new_param_types, *new_param_types_p;
528 if (prototype_p (old_type))
530 auto_vec<tree, 16> otypes;
531 push_function_arg_types (&otypes, old_type);
532 fill_vector_of_new_param_types (&new_param_types, &otypes, m_adj_params,
533 !type_original_p);
534 new_param_types_p = &new_param_types;
536 else
537 new_param_types_p = NULL;
539 /* Check if any params type cares about are modified. In this case will
540 need to drop some type attributes. */
541 bool modified = false;
542 size_t index = 0;
543 if (m_adj_params)
544 for (tree t = TYPE_ARG_TYPES (old_type);
545 t && (int)index < m_always_copy_start && !modified;
546 t = TREE_CHAIN (t), index++)
547 if (index >= m_adj_params->length ()
548 || get_original_index (index) != (int)index)
549 modified = true;
552 return build_adjusted_function_type (old_type, new_param_types_p,
553 method2func_p (old_type), m_skip_return,
554 modified);
557 /* Build variant of function decl ORIG_DECL which has no return value if
558 M_SKIP_RETURN is true and, if ORIG_DECL's types or parameters is known, has
559 this type adjusted as indicated in M_ADJ_PARAMS. Arguments from
560 DECL_ARGUMENTS list are not processed now, since they are linked by
561 TREE_CHAIN directly and not accessible in LTO during WPA. The caller is
562 responsible for eliminating them when clones are properly materialized. */
564 tree
565 ipa_param_adjustments::adjust_decl (tree orig_decl)
567 tree new_decl = copy_node (orig_decl);
568 tree orig_type = TREE_TYPE (orig_decl);
569 if (prototype_p (orig_type)
570 || (m_skip_return && !VOID_TYPE_P (TREE_TYPE (orig_type))))
572 tree new_type = build_new_function_type (orig_type, false);
573 TREE_TYPE (new_decl) = new_type;
575 if (method2func_p (orig_type))
576 DECL_VINDEX (new_decl) = NULL_TREE;
578 /* When signature changes, we need to clear builtin info. */
579 if (fndecl_built_in_p (new_decl))
580 set_decl_built_in_function (new_decl, NOT_BUILT_IN, 0);
582 DECL_VIRTUAL_P (new_decl) = 0;
583 DECL_LANG_SPECIFIC (new_decl) = NULL;
585 /* Drop MALLOC attribute for a void function. */
586 if (m_skip_return)
587 DECL_IS_MALLOC (new_decl) = 0;
589 return new_decl;
592 /* Wrapper around get_base_ref_and_offset for cases interesting for IPA-SRA
593 transformations. Return true if EXPR has an interesting form and fill in
594 *BASE_P and *UNIT_OFFSET_P with the appropriate info. */
596 static bool
597 isra_get_ref_base_and_offset (tree expr, tree *base_p, unsigned *unit_offset_p)
599 HOST_WIDE_INT offset, size;
600 bool reverse;
601 tree base
602 = get_ref_base_and_extent_hwi (expr, &offset, &size, &reverse);
603 if (!base || size < 0)
604 return false;
606 if ((offset % BITS_PER_UNIT) != 0)
607 return false;
609 if (TREE_CODE (base) == MEM_REF)
611 poly_int64 plmoff = mem_ref_offset (base).force_shwi ();
612 HOST_WIDE_INT moff;
613 bool is_cst = plmoff.is_constant (&moff);
614 if (!is_cst)
615 return false;
616 offset += moff * BITS_PER_UNIT;
617 base = TREE_OPERAND (base, 0);
620 if (offset < 0 || (offset / BITS_PER_UNIT) > UINT_MAX)
621 return false;
623 *base_p = base;
624 *unit_offset_p = offset / BITS_PER_UNIT;
625 return true;
628 /* Modify actual arguments of a function call in statement currently belonging
629 to CS, and make it call CS->callee->decl. Return the new statement that
630 replaced the old one. When invoked, cfun and current_function_decl have to
631 be set to the caller. */
633 gcall *
634 ipa_param_adjustments::modify_call (cgraph_edge *cs,
635 bool update_references)
637 gcall *stmt = cs->call_stmt;
638 tree callee_decl = cs->callee->decl;
640 ipa_edge_modification_info *mod_info
641 = ipa_edge_modifications ? ipa_edge_modifications->get (cs) : NULL;
642 if (mod_info && symtab->dump_file)
644 fprintf (symtab->dump_file, "Information about pre-exiting "
645 "modifications.\n Index map:");
646 unsigned idx_len = mod_info->index_map.length ();
647 for (unsigned i = 0; i < idx_len; i++)
648 fprintf (symtab->dump_file, " %i", mod_info->index_map[i]);
649 fprintf (symtab->dump_file, "\n Pass-through split map: ");
650 unsigned ptm_len = mod_info->pass_through_map.length ();
651 for (unsigned i = 0; i < ptm_len; i++)
652 fprintf (symtab->dump_file,
653 " (base_index: %u, offset: %u, new_index: %i)",
654 mod_info->pass_through_map[i].base_index,
655 mod_info->pass_through_map[i].unit_offset,
656 mod_info->pass_through_map[i].new_index);
657 fprintf (symtab->dump_file, "\n Always-copy delta: %i\n",
658 mod_info->always_copy_delta);
661 unsigned len = vec_safe_length (m_adj_params);
662 auto_vec<tree, 16> vargs (len);
663 unsigned old_nargs = gimple_call_num_args (stmt);
664 unsigned orig_nargs = mod_info ? mod_info->index_map.length () : old_nargs;
665 auto_vec<bool, 16> kept (old_nargs);
666 kept.quick_grow_cleared (old_nargs);
668 cgraph_node *current_node = cgraph_node::get (current_function_decl);
669 if (update_references)
670 current_node->remove_stmt_references (stmt);
672 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
673 gimple_stmt_iterator prev_gsi = gsi;
674 gsi_prev (&prev_gsi);
675 for (unsigned i = 0; i < len; i++)
677 ipa_adjusted_param *apm = &(*m_adj_params)[i];
678 if (apm->op == IPA_PARAM_OP_COPY)
680 int index = apm->base_index;
681 if ((unsigned) index >= orig_nargs)
682 /* Can happen if the original call has argument mismatch,
683 ignore. */
684 continue;
685 if (mod_info)
687 index = mod_info->index_map[apm->base_index];
688 gcc_assert (index >= 0);
691 tree arg = gimple_call_arg (stmt, index);
693 vargs.quick_push (arg);
694 kept[index] = true;
695 continue;
698 /* At the moment the only user of IPA_PARAM_OP_NEW modifies calls itself.
699 If we ever want to support it during WPA IPA stage, we'll need a
700 mechanism to call into the IPA passes that introduced them. Currently
701 we simply mandate that IPA infrastructure understands all argument
702 modifications. Remember, edge redirection/modification is done only
703 once, not in steps for each pass modifying the callee like clone
704 materialization. */
705 gcc_assert (apm->op == IPA_PARAM_OP_SPLIT);
707 /* We have to handle pass-through changes differently using the map
708 clone materialziation might have left behind. */
709 tree repl = NULL_TREE;
710 unsigned ptm_len = mod_info ? mod_info->pass_through_map.length () : 0;
711 for (unsigned j = 0; j < ptm_len; j++)
712 if (mod_info->pass_through_map[j].base_index == apm->base_index
713 && mod_info->pass_through_map[j].unit_offset == apm->unit_offset)
715 int repl_idx = mod_info->pass_through_map[j].new_index;
716 gcc_assert (repl_idx >= 0);
717 repl = gimple_call_arg (stmt, repl_idx);
718 break;
720 if (repl)
722 vargs.quick_push (repl);
723 continue;
726 int index = apm->base_index;
727 if ((unsigned) index >= orig_nargs)
728 /* Can happen if the original call has argument mismatch, ignore. */
729 continue;
730 if (mod_info)
732 index = mod_info->index_map[apm->base_index];
733 gcc_assert (index >= 0);
735 tree base = gimple_call_arg (stmt, index);
737 /* We create a new parameter out of the value of the old one, we can
738 do the following kind of transformations:
740 - A scalar passed by reference, potentially as a part of a larger
741 aggregate, is converted to a scalar passed by value.
743 - A part of an aggregate is passed instead of the whole aggregate. */
745 location_t loc = gimple_location (stmt);
746 tree off;
747 bool deref_base = false;
748 unsigned int deref_align = 0;
749 if (TREE_CODE (base) != ADDR_EXPR
750 && is_gimple_reg_type (TREE_TYPE (base)))
752 /* Detect type mismatches in calls in invalid programs and make a
753 poor attempt to gracefully convert them so that we don't ICE. */
754 if (!POINTER_TYPE_P (TREE_TYPE (base)))
755 base = force_value_to_type (ptr_type_node, base);
757 off = build_int_cst (apm->alias_ptr_type, apm->unit_offset);
759 else
761 bool addrof;
762 if (TREE_CODE (base) == ADDR_EXPR)
764 base = TREE_OPERAND (base, 0);
765 addrof = true;
767 else
768 addrof = false;
770 tree prev_base = base;
771 poly_int64 base_offset;
772 base = get_addr_base_and_unit_offset (base, &base_offset);
774 /* Aggregate arguments can have non-invariant addresses. */
775 if (!base)
777 base = build_fold_addr_expr (prev_base);
778 off = build_int_cst (apm->alias_ptr_type, apm->unit_offset);
780 else if (TREE_CODE (base) == MEM_REF)
782 if (!addrof)
784 deref_base = true;
785 deref_align = TYPE_ALIGN (TREE_TYPE (base));
787 off = build_int_cst (apm->alias_ptr_type,
788 base_offset + apm->unit_offset);
789 off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
790 off);
791 base = TREE_OPERAND (base, 0);
793 else
795 off = build_int_cst (apm->alias_ptr_type,
796 base_offset + apm->unit_offset);
797 base = build_fold_addr_expr (base);
801 tree type = apm->type;
802 unsigned int align;
803 unsigned HOST_WIDE_INT misalign;
805 if (deref_base)
807 align = deref_align;
808 misalign = 0;
810 else
812 get_pointer_alignment_1 (base, &align, &misalign);
813 /* All users must make sure that we can be optimistic when it
814 comes to alignment in this case (by inspecting the final users
815 of these new parameters). */
816 if (TYPE_ALIGN (type) > align)
817 align = TYPE_ALIGN (type);
819 misalign
820 += (offset_int::from (wi::to_wide (off), SIGNED).to_short_addr ()
821 * BITS_PER_UNIT);
822 misalign = misalign & (align - 1);
823 if (misalign != 0)
824 align = least_bit_hwi (misalign);
825 if (align < TYPE_ALIGN (type))
826 type = build_aligned_type (type, align);
827 base = force_gimple_operand_gsi (&gsi, base,
828 true, NULL, true, GSI_SAME_STMT);
829 tree expr = fold_build2_loc (loc, MEM_REF, type, base, off);
830 REF_REVERSE_STORAGE_ORDER (expr) = apm->reverse;
831 /* If expr is not a valid gimple call argument emit
832 a load into a temporary. */
833 if (is_gimple_reg_type (TREE_TYPE (expr)))
835 gimple *tem = gimple_build_assign (NULL_TREE, expr);
836 if (gimple_in_ssa_p (cfun))
838 gimple_set_vuse (tem, gimple_vuse (stmt));
839 expr = make_ssa_name (TREE_TYPE (expr), tem);
841 else
842 expr = create_tmp_reg (TREE_TYPE (expr));
843 gimple_assign_set_lhs (tem, expr);
844 gsi_insert_before (&gsi, tem, GSI_SAME_STMT);
846 vargs.quick_push (expr);
849 if (m_always_copy_start >= 0)
851 int always_copy_start = m_always_copy_start;
852 if (mod_info)
854 always_copy_start += mod_info->always_copy_delta;
855 gcc_assert (always_copy_start >= 0);
857 for (unsigned i = always_copy_start; i < old_nargs; i++)
858 vargs.safe_push (gimple_call_arg (stmt, i));
861 /* For optimized away parameters, add on the caller side
862 before the call
863 DEBUG D#X => parm_Y(D)
864 stmts and associate D#X with parm in decl_debug_args_lookup
865 vector to say for debug info that if parameter parm had been passed,
866 it would have value parm_Y(D). */
867 tree old_decl = gimple_call_fndecl (stmt);
868 if (MAY_HAVE_DEBUG_BIND_STMTS && old_decl && callee_decl)
870 vec<tree, va_gc> **debug_args = NULL;
871 unsigned i = 0;
872 cgraph_node *callee_node = cgraph_node::get (callee_decl);
874 /* FIXME: we don't seem to be able to insert debug args before clone
875 is materialized. Materializing them early leads to extra memory
876 use. */
877 if (callee_node->clone_of)
878 callee_node->get_untransformed_body ();
879 for (tree old_parm = DECL_ARGUMENTS (old_decl);
880 old_parm && i < old_nargs && ((int) i) < m_always_copy_start;
881 old_parm = DECL_CHAIN (old_parm), i++)
883 if (!is_gimple_reg (old_parm) || kept[i])
884 continue;
885 tree arg;
886 if (mod_info)
888 if (mod_info->index_map[i] < 0)
889 continue;
890 arg = gimple_call_arg (stmt, mod_info->index_map[i]);
892 else
893 arg = gimple_call_arg (stmt, i);
895 tree origin = DECL_ORIGIN (old_parm);
896 if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
898 if (!fold_convertible_p (TREE_TYPE (origin), arg))
899 continue;
900 tree rhs1;
901 if (TREE_CODE (arg) == SSA_NAME
902 && gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg))
903 && (rhs1
904 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (arg)))
905 && useless_type_conversion_p (TREE_TYPE (origin),
906 TREE_TYPE (rhs1)))
907 arg = rhs1;
908 else
909 arg = fold_convert_loc (gimple_location (stmt),
910 TREE_TYPE (origin), arg);
912 if (debug_args == NULL)
913 debug_args = decl_debug_args_insert (callee_decl);
914 unsigned int ix;
915 tree ddecl = NULL_TREE;
916 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl); ix += 2)
917 if (ddecl == origin)
919 ddecl = (**debug_args)[ix + 1];
920 break;
922 if (ddecl == NULL)
924 ddecl = build_debug_expr_decl (TREE_TYPE (origin));
925 /* FIXME: Is setting the mode really necessary? */
926 SET_DECL_MODE (ddecl, DECL_MODE (origin));
928 vec_safe_push (*debug_args, origin);
929 vec_safe_push (*debug_args, ddecl);
931 gimple *def_temp = gimple_build_debug_bind (ddecl,
932 unshare_expr (arg), stmt);
933 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
937 if (dump_file && (dump_flags & TDF_DETAILS))
939 fprintf (dump_file, "replacing stmt:");
940 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0);
943 gcall *new_stmt = gimple_build_call_vec (callee_decl, vargs);
945 tree ssa_to_remove = NULL;
946 if (tree lhs = gimple_call_lhs (stmt))
948 if (!m_skip_return)
949 gimple_call_set_lhs (new_stmt, lhs);
950 else if (TREE_CODE (lhs) == SSA_NAME)
952 /* LHS should now by a default-def SSA. Unfortunately default-def
953 SSA_NAMEs need a backing variable (or at least some code examining
954 SSAs assumes it is non-NULL). So we either have to re-use the
955 decl we have at hand or introdice a new one. */
956 tree repl = create_tmp_var (TREE_TYPE (lhs), "removed_return");
957 repl = get_or_create_ssa_default_def (cfun, repl);
958 SSA_NAME_IS_DEFAULT_DEF (repl) = true;
959 imm_use_iterator ui;
960 use_operand_p use_p;
961 gimple *using_stmt;
962 FOR_EACH_IMM_USE_STMT (using_stmt, ui, lhs)
964 FOR_EACH_IMM_USE_ON_STMT (use_p, ui)
966 SET_USE (use_p, repl);
968 update_stmt (using_stmt);
970 ssa_to_remove = lhs;
974 gimple_set_block (new_stmt, gimple_block (stmt));
975 if (gimple_has_location (stmt))
976 gimple_set_location (new_stmt, gimple_location (stmt));
977 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
978 gimple_call_copy_flags (new_stmt, stmt);
979 if (gimple_in_ssa_p (cfun))
980 gimple_move_vops (new_stmt, stmt);
982 if (dump_file && (dump_flags & TDF_DETAILS))
984 fprintf (dump_file, "with stmt:");
985 print_gimple_stmt (dump_file, new_stmt, 0);
986 fprintf (dump_file, "\n");
988 gsi_replace (&gsi, new_stmt, true);
989 if (ssa_to_remove)
990 release_ssa_name (ssa_to_remove);
991 if (update_references)
994 current_node->record_stmt_references (gsi_stmt (gsi));
995 gsi_prev (&gsi);
997 while (gsi_stmt (gsi) != gsi_stmt (prev_gsi));
999 if (mod_info)
1000 ipa_edge_modifications->remove (cs);
1001 return new_stmt;
1004 /* Dump information contained in the object in textual form to F. */
1006 void
1007 ipa_param_adjustments::dump (FILE *f)
1009 fprintf (f, " m_always_copy_start: %i\n", m_always_copy_start);
1010 ipa_dump_adjusted_parameters (f, m_adj_params);
1011 if (m_skip_return)
1012 fprintf (f, " Will SKIP return.\n");
1015 /* Dump information contained in the object in textual form to stderr. */
1017 void
1018 ipa_param_adjustments::debug ()
1020 dump (stderr);
1023 /* Register that REPLACEMENT should replace parameter described in APM. */
1025 void
1026 ipa_param_body_adjustments::register_replacement (ipa_adjusted_param *apm,
1027 tree replacement)
1029 gcc_checking_assert (apm->op == IPA_PARAM_OP_SPLIT
1030 || apm->op == IPA_PARAM_OP_NEW);
1031 gcc_checking_assert (!apm->prev_clone_adjustment);
1032 ipa_param_body_replacement psr;
1033 psr.base = m_oparms[apm->prev_clone_index];
1034 psr.repl = replacement;
1035 psr.dummy = NULL_TREE;
1036 psr.unit_offset = apm->unit_offset;
1037 m_replacements.safe_push (psr);
1040 /* Copy or not, as appropriate given m_id and decl context, a pre-existing
1041 PARM_DECL T so that it can be included in the parameters of the modified
1042 function. */
1044 tree
1045 ipa_param_body_adjustments::carry_over_param (tree t)
1047 tree new_parm;
1048 if (m_id)
1050 new_parm = remap_decl (t, m_id);
1051 if (TREE_CODE (new_parm) != PARM_DECL)
1052 new_parm = m_id->copy_decl (t, m_id);
1054 else if (DECL_CONTEXT (t) != m_fndecl)
1056 new_parm = copy_node (t);
1057 DECL_CONTEXT (new_parm) = m_fndecl;
1059 else
1060 new_parm = t;
1061 return new_parm;
1064 /* Populate m_dead_stmts given that DEAD_PARAM is going to be removed without
1065 any replacement or splitting. REPL is the replacement VAR_SECL to base any
1066 remaining uses of a removed parameter on. Push all removed SSA names that
1067 are used within debug statements to DEBUGSTACK. */
1069 void
1070 ipa_param_body_adjustments::mark_dead_statements (tree dead_param,
1071 vec<tree> *debugstack)
1073 /* Current IPA analyses which remove unused parameters never remove a
1074 non-gimple register ones which have any use except as parameters in other
1075 calls, so we can safely leve them as they are. */
1076 if (!is_gimple_reg (dead_param))
1077 return;
1078 tree parm_ddef = ssa_default_def (m_id->src_cfun, dead_param);
1079 if (!parm_ddef || has_zero_uses (parm_ddef))
1080 return;
1082 auto_vec<tree, 4> stack;
1083 hash_set<tree> used_in_debug;
1084 m_dead_ssas.add (parm_ddef);
1085 stack.safe_push (parm_ddef);
1086 while (!stack.is_empty ())
1088 imm_use_iterator imm_iter;
1089 use_operand_p use_p;
1090 tree t = stack.pop ();
1092 insert_decl_map (m_id, t, error_mark_node);
1093 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, t)
1095 gimple *stmt = USE_STMT (use_p);
1097 /* Calls containing dead arguments cannot be deleted,
1098 modify_call_stmt will instead remove just the argument later on.
1099 If isra_track_scalar_value_uses in ipa-sra.cc is extended to look
1100 through const functions, we will need to do so here too. */
1101 if (is_gimple_call (stmt)
1102 || (m_id->blocks_to_copy
1103 && !bitmap_bit_p (m_id->blocks_to_copy,
1104 gimple_bb (stmt)->index)))
1105 continue;
1107 if (is_gimple_debug (stmt))
1109 m_dead_stmts.add (stmt);
1110 gcc_assert (gimple_debug_bind_p (stmt));
1111 if (!used_in_debug.contains (t))
1113 used_in_debug.add (t);
1114 debugstack->safe_push (t);
1117 else if (gimple_code (stmt) == GIMPLE_PHI)
1119 gphi *phi = as_a <gphi *> (stmt);
1120 int ix = PHI_ARG_INDEX_FROM_USE (use_p);
1122 if (!m_id->blocks_to_copy
1123 || bitmap_bit_p (m_id->blocks_to_copy,
1124 gimple_phi_arg_edge (phi, ix)->src->index))
1126 m_dead_stmts.add (phi);
1127 tree res = gimple_phi_result (phi);
1128 if (!m_dead_ssas.add (res))
1129 stack.safe_push (res);
1132 else if (is_gimple_assign (stmt))
1134 m_dead_stmts.add (stmt);
1135 if (!gimple_clobber_p (stmt))
1137 tree lhs = gimple_assign_lhs (stmt);
1138 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
1139 if (!m_dead_ssas.add (lhs))
1140 stack.safe_push (lhs);
1143 else
1144 /* IPA-SRA does not analyze other types of statements. */
1145 gcc_unreachable ();
1149 if (!MAY_HAVE_DEBUG_STMTS)
1151 gcc_assert (debugstack->is_empty ());
1152 return;
1155 tree dp_ddecl = build_debug_expr_decl (TREE_TYPE (dead_param));
1156 /* FIXME: Is setting the mode really necessary? */
1157 SET_DECL_MODE (dp_ddecl, DECL_MODE (dead_param));
1158 m_dead_ssa_debug_equiv.put (parm_ddef, dp_ddecl);
1161 /* Callback to walk_tree. If REMAP is an SSA_NAME that is present in hash_map
1162 passed in DATA, replace it with unshared version of what it was mapped to.
1163 If an SSA argument would be remapped to NULL, the whole operation needs to
1164 abort which is signaled by returning error_mark_node. */
1166 static tree
1167 replace_with_mapped_expr (tree *remap, int *walk_subtrees, void *data)
1169 if (TYPE_P (*remap))
1171 *walk_subtrees = 0;
1172 return 0;
1174 if (TREE_CODE (*remap) != SSA_NAME)
1175 return 0;
1177 *walk_subtrees = 0;
1179 hash_map<tree, tree> *equivs = (hash_map<tree, tree> *) data;
1180 if (tree *p = equivs->get (*remap))
1182 if (!*p)
1183 return error_mark_node;
1184 *remap = unshare_expr (*p);
1186 return 0;
1189 /* Replace all occurances of SSAs in m_dead_ssa_debug_equiv in t with what they
1190 are mapped to. */
1192 void
1193 ipa_param_body_adjustments::remap_with_debug_expressions (tree *t)
1195 /* If *t is an SSA_NAME which should have its debug statements reset, it is
1196 mapped to NULL in the hash_map.
1198 It is perhaps simpler to handle the SSA_NAME cases directly and only
1199 invoke walk_tree on more complex expressions. When
1200 remap_with_debug_expressions is called from tree-inline.cc, a to-be-reset
1201 SSA_NAME can be an operand to such expressions and the entire debug
1202 variable we are remapping should be reset. This is signaled by walk_tree
1203 returning error_mark_node and done by setting *t to NULL. */
1204 if (TREE_CODE (*t) == SSA_NAME)
1206 if (tree *p = m_dead_ssa_debug_equiv.get (*t))
1207 *t = *p;
1209 else if (walk_tree (t, replace_with_mapped_expr,
1210 &m_dead_ssa_debug_equiv, NULL) == error_mark_node)
1211 *t = NULL_TREE;
1214 /* For an SSA_NAME DEAD_SSA which is about to be DCEd because it is based on a
1215 useless parameter, prepare an expression that should represent it in
1216 debug_binds in the cloned function and add a mapping from DEAD_SSA to
1217 m_dead_ssa_debug_equiv. That mapping is to NULL when the associated
1218 debug_statement has to be reset instead. In such case return false,
1219 ottherwise return true. If DEAD_SSA comes from a basic block which is not
1220 about to be copied, ignore it and return true. */
1222 bool
1223 ipa_param_body_adjustments::prepare_debug_expressions (tree dead_ssa)
1225 gcc_checking_assert (m_dead_ssas.contains (dead_ssa));
1226 if (tree *d = m_dead_ssa_debug_equiv.get (dead_ssa))
1227 return (*d != NULL_TREE);
1229 gcc_assert (!SSA_NAME_IS_DEFAULT_DEF (dead_ssa));
1230 gimple *def = SSA_NAME_DEF_STMT (dead_ssa);
1231 if (m_id->blocks_to_copy
1232 && !bitmap_bit_p (m_id->blocks_to_copy, gimple_bb (def)->index))
1233 return true;
1235 if (gimple_code (def) == GIMPLE_PHI)
1237 /* In theory, we could ignore all SSAs coming from BBs not in
1238 m_id->blocks_to_copy but at the time of the writing this code that
1239 should never really be the case because only fnsplit uses that bitmap,
1240 so don't bother. */
1241 tree value = degenerate_phi_result (as_a <gphi *> (def));
1242 if (!value
1243 || (m_dead_ssas.contains (value)
1244 && !prepare_debug_expressions (value)))
1246 m_dead_ssa_debug_equiv.put (dead_ssa, NULL_TREE);
1247 return false;
1250 gcc_assert (TREE_CODE (value) == SSA_NAME);
1251 tree *d = m_dead_ssa_debug_equiv.get (value);
1252 m_dead_ssa_debug_equiv.put (dead_ssa, *d);
1253 return true;
1256 bool lost = false;
1257 use_operand_p use_p;
1258 ssa_op_iter oi;
1259 FOR_EACH_PHI_OR_STMT_USE (use_p, def, oi, SSA_OP_USE)
1261 tree use = USE_FROM_PTR (use_p);
1262 if (m_dead_ssas.contains (use)
1263 && !prepare_debug_expressions (use))
1265 lost = true;
1266 break;
1270 if (lost)
1272 m_dead_ssa_debug_equiv.put (dead_ssa, NULL_TREE);
1273 return false;
1276 if (is_gimple_assign (def))
1278 gcc_assert (!gimple_clobber_p (def));
1279 if (gimple_assign_copy_p (def)
1280 && TREE_CODE (gimple_assign_rhs1 (def)) == SSA_NAME)
1282 tree d = *m_dead_ssa_debug_equiv.get (gimple_assign_rhs1 (def));
1283 gcc_assert (d);
1284 m_dead_ssa_debug_equiv.put (dead_ssa, d);
1285 return true;
1288 tree val
1289 = unshare_expr_without_location (gimple_assign_rhs_to_tree (def));
1290 remap_with_debug_expressions (&val);
1292 tree vexpr = build_debug_expr_decl (TREE_TYPE (val));
1293 m_dead_stmt_debug_equiv.put (def, val);
1294 m_dead_ssa_debug_equiv.put (dead_ssa, vexpr);
1295 return true;
1297 else
1298 gcc_unreachable ();
1301 /* Common initialization performed by all ipa_param_body_adjustments
1302 constructors. OLD_FNDECL is the declaration we take original arguments
1303 from, (it may be the same as M_FNDECL). VARS, if non-NULL, is a pointer to
1304 a chained list of new local variables. TREE_MAP is the IPA-CP produced
1305 mapping of trees to constants.
1307 The function is rather long but it really onlu initializes all data members
1308 of the class. It creates new param DECLs, finds their new types, */
1310 void
1311 ipa_param_body_adjustments::common_initialization (tree old_fndecl,
1312 tree *vars,
1313 vec<ipa_replace_map *,
1314 va_gc> *tree_map)
1316 push_function_arg_decls (&m_oparms, old_fndecl);
1317 auto_vec<tree,16> otypes;
1318 if (TYPE_ARG_TYPES (TREE_TYPE (old_fndecl)) != NULL_TREE)
1319 push_function_arg_types (&otypes, TREE_TYPE (old_fndecl));
1320 else
1322 auto_vec<tree,16> oparms;
1323 push_function_arg_decls (&oparms, old_fndecl);
1324 unsigned ocount = oparms.length ();
1325 otypes.reserve_exact (ocount);
1326 for (unsigned i = 0; i < ocount; i++)
1327 otypes.quick_push (TREE_TYPE (oparms[i]));
1329 fill_vector_of_new_param_types (&m_new_types, &otypes, m_adj_params, true);
1331 auto_vec<bool, 16> kept;
1332 kept.reserve_exact (m_oparms.length ());
1333 kept.quick_grow_cleared (m_oparms.length ());
1334 auto_vec<bool, 16> split;
1335 split.reserve_exact (m_oparms.length ());
1336 split.quick_grow_cleared (m_oparms.length ());
1338 unsigned adj_len = vec_safe_length (m_adj_params);
1339 m_method2func = ((TREE_CODE (TREE_TYPE (m_fndecl)) == METHOD_TYPE)
1340 && (adj_len == 0
1341 || (*m_adj_params)[0].op != IPA_PARAM_OP_COPY
1342 || (*m_adj_params)[0].base_index != 0));
1344 /* The main job of the this function is to go over the vector of adjusted
1345 parameters and create declarations or find corresponding old ones and push
1346 them to m_new_decls. For IPA-SRA replacements it also creates
1347 corresponding m_id->dst_node->clone.performed_splits entries. */
1349 m_new_decls.reserve_exact (adj_len);
1350 for (unsigned i = 0; i < adj_len ; i++)
1352 ipa_adjusted_param *apm = &(*m_adj_params)[i];
1353 unsigned prev_index = apm->prev_clone_index;
1354 tree new_parm;
1355 if (apm->op == IPA_PARAM_OP_COPY
1356 || apm->prev_clone_adjustment)
1358 kept[prev_index] = true;
1359 new_parm = carry_over_param (m_oparms[prev_index]);
1360 m_new_decls.quick_push (new_parm);
1362 else if (apm->op == IPA_PARAM_OP_NEW
1363 || apm->op == IPA_PARAM_OP_SPLIT)
1365 tree new_type = m_new_types[i];
1366 gcc_checking_assert (new_type);
1367 new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
1368 new_type);
1369 const char *prefix = ipa_param_prefixes[apm->param_prefix_index];
1370 DECL_NAME (new_parm) = create_tmp_var_name (prefix);
1371 DECL_ARTIFICIAL (new_parm) = 1;
1372 DECL_ARG_TYPE (new_parm) = new_type;
1373 DECL_CONTEXT (new_parm) = m_fndecl;
1374 TREE_USED (new_parm) = 1;
1375 DECL_IGNORED_P (new_parm) = 1;
1376 layout_decl (new_parm, 0);
1377 m_new_decls.quick_push (new_parm);
1379 if (apm->op == IPA_PARAM_OP_SPLIT)
1381 m_split_modifications_p = true;
1382 split[prev_index] = true;
1383 register_replacement (apm, new_parm);
1386 else
1387 gcc_unreachable ();
1390 if (tree_map)
1392 /* Do not treat parameters which were replaced with a constant as
1393 completely vanished. */
1394 auto_vec <int, 16> index_mapping;
1395 bool need_remap = false;
1397 if (m_id)
1399 clone_info *cinfo = clone_info::get (m_id->src_node);
1400 if (cinfo && cinfo->param_adjustments)
1402 cinfo->param_adjustments->get_updated_indices (&index_mapping);
1403 need_remap = true;
1407 for (unsigned i = 0; i < tree_map->length (); i++)
1409 int parm_num = (*tree_map)[i]->parm_num;
1410 gcc_assert (parm_num >= 0);
1411 if (need_remap)
1412 parm_num = index_mapping[parm_num];
1413 kept[parm_num] = true;
1417 /* As part of body modifications, we will also have to replace remaining uses
1418 of remaining uses of removed PARM_DECLs (which do not however use the
1419 initial value) with their VAR_DECL copies.
1421 We do this differently with and without m_id. With m_id, we rely on its
1422 mapping and create a replacement straight away. Without it, we have our
1423 own mechanism for which we have to populate m_removed_decls vector. Just
1424 don't mix them, that is why you should not call
1425 replace_removed_params_ssa_names or perform_cfun_body_modifications when
1426 you construct with ID not equal to NULL. */
1428 auto_vec<tree, 8> ssas_to_process_debug;
1429 unsigned op_len = m_oparms.length ();
1430 for (unsigned i = 0; i < op_len; i++)
1431 if (!kept[i])
1433 if (m_id)
1435 gcc_assert (!m_id->decl_map->get (m_oparms[i]));
1436 tree var = copy_decl_to_var (m_oparms[i], m_id);
1437 insert_decl_map (m_id, m_oparms[i], var);
1438 /* Declare this new variable. */
1439 DECL_CHAIN (var) = *vars;
1440 *vars = var;
1442 /* If this is not a split but a real removal, init hash sets
1443 that will guide what not to copy to the new body. */
1444 if (!split[i])
1445 mark_dead_statements (m_oparms[i], &ssas_to_process_debug);
1446 if (MAY_HAVE_DEBUG_STMTS
1447 && is_gimple_reg (m_oparms[i]))
1448 m_reset_debug_decls.safe_push (m_oparms[i]);
1450 else
1452 m_removed_decls.safe_push (m_oparms[i]);
1453 m_removed_map.put (m_oparms[i], m_removed_decls.length () - 1);
1454 if (MAY_HAVE_DEBUG_STMTS
1455 && !kept[i]
1456 && is_gimple_reg (m_oparms[i]))
1457 m_reset_debug_decls.safe_push (m_oparms[i]);
1461 while (!ssas_to_process_debug.is_empty ())
1462 prepare_debug_expressions (ssas_to_process_debug.pop ());
1465 /* Constructor of ipa_param_body_adjustments from a simple list of
1466 modifications to parameters listed in ADJ_PARAMS which will prepare ground
1467 for modification of parameters of fndecl. Return value of the function will
1468 not be removed and the object will assume it does not run as a part of
1469 tree-function_versioning. */
1471 ipa_param_body_adjustments
1472 ::ipa_param_body_adjustments (vec<ipa_adjusted_param, va_gc> *adj_params,
1473 tree fndecl)
1474 : m_adj_params (adj_params), m_adjustments (NULL), m_reset_debug_decls (),
1475 m_split_modifications_p (false), m_dead_stmts (), m_dead_ssas (),
1476 m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (), m_fndecl (fndecl),
1477 m_id (NULL), m_oparms (), m_new_decls (), m_new_types (), m_replacements (),
1478 m_removed_decls (), m_removed_map (), m_method2func (false)
1480 common_initialization (fndecl, NULL, NULL);
1483 /* Constructor of ipa_param_body_adjustments from ipa_param_adjustments in
1484 ADJUSTMENTS which will prepare ground for modification of parameters of
1485 fndecl. The object will assume it does not run as a part of
1486 tree-function_versioning. */
1488 ipa_param_body_adjustments
1489 ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
1490 tree fndecl)
1491 : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
1492 m_reset_debug_decls (), m_split_modifications_p (false), m_dead_stmts (),
1493 m_dead_ssas (), m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (),
1494 m_fndecl (fndecl), m_id (NULL), m_oparms (), m_new_decls (),
1495 m_new_types (), m_replacements (), m_removed_decls (), m_removed_map (),
1496 m_method2func (false)
1498 common_initialization (fndecl, NULL, NULL);
1501 /* Constructor of ipa_param_body_adjustments which sets it up as a part of
1502 running tree_function_versioning. Planned modifications to the function are
1503 in ADJUSTMENTS. FNDECL designates the new function clone which is being
1504 modified. OLD_FNDECL is the function of which FNDECL is a clone (and which
1505 at the time of invocation still share DECL_ARGUMENTS). ID is the
1506 copy_body_data structure driving the wholy body copying process. VARS is a
1507 pointer to the head of the list of new local variables, TREE_MAP is the map
1508 that drives tree substitution in the cloning process. */
1510 ipa_param_body_adjustments
1511 ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
1512 tree fndecl, tree old_fndecl,
1513 copy_body_data *id, tree *vars,
1514 vec<ipa_replace_map *, va_gc> *tree_map)
1515 : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
1516 m_reset_debug_decls (), m_split_modifications_p (false), m_dead_stmts (),
1517 m_dead_ssas (), m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (),
1518 m_fndecl (fndecl), m_id (id), m_oparms (), m_new_decls (), m_new_types (),
1519 m_replacements (), m_removed_decls (), m_removed_map (),
1520 m_method2func (false)
1522 common_initialization (old_fndecl, vars, tree_map);
1525 /* Chain new param decls up and return them. */
1527 tree
1528 ipa_param_body_adjustments::get_new_param_chain ()
1530 tree result;
1531 tree *link = &result;
1533 unsigned len = vec_safe_length (m_adj_params);
1534 for (unsigned i = 0; i < len; i++)
1536 tree new_decl = m_new_decls[i];
1537 *link = new_decl;
1538 link = &DECL_CHAIN (new_decl);
1540 *link = NULL_TREE;
1541 return result;
1544 /* Modify the function parameters FNDECL and its type according to the plan in
1545 ADJUSTMENTS. This function needs to be called when the decl has not already
1546 been processed with ipa_param_adjustments::adjust_decl, otherwise just
1547 seting DECL_ARGUMENTS to whatever get_new_param_chain will do is enough. */
1549 void
1550 ipa_param_body_adjustments::modify_formal_parameters ()
1552 tree orig_type = TREE_TYPE (m_fndecl);
1553 DECL_ARGUMENTS (m_fndecl) = get_new_param_chain ();
1555 /* When signature changes, we need to clear builtin info. */
1556 if (fndecl_built_in_p (m_fndecl))
1557 set_decl_built_in_function (m_fndecl, NOT_BUILT_IN, 0);
1559 bool modified = false;
1560 size_t index = 0;
1561 if (m_adj_params)
1562 for (tree t = TYPE_ARG_TYPES (orig_type);
1563 t && !modified;
1564 t = TREE_CHAIN (t), index++)
1565 if (index >= m_adj_params->length ()
1566 || (*m_adj_params)[index].op != IPA_PARAM_OP_COPY
1567 || (*m_adj_params)[index].base_index != index)
1568 modified = true;
1570 /* At this point, removing return value is only implemented when going
1571 through tree_function_versioning, not when modifying function body
1572 directly. */
1573 gcc_assert (!m_adjustments || !m_adjustments->m_skip_return);
1574 tree new_type = build_adjusted_function_type (orig_type, &m_new_types,
1575 m_method2func, false, modified);
1577 TREE_TYPE (m_fndecl) = new_type;
1578 DECL_VIRTUAL_P (m_fndecl) = 0;
1579 DECL_LANG_SPECIFIC (m_fndecl) = NULL;
1580 if (m_method2func)
1581 DECL_VINDEX (m_fndecl) = NULL_TREE;
1584 /* Given BASE and UNIT_OFFSET, find the corresponding record among replacement
1585 structures. */
1587 ipa_param_body_replacement *
1588 ipa_param_body_adjustments::lookup_replacement_1 (tree base,
1589 unsigned unit_offset)
1591 unsigned int len = m_replacements.length ();
1592 for (unsigned i = 0; i < len; i++)
1594 ipa_param_body_replacement *pbr = &m_replacements[i];
1596 if (pbr->base == base
1597 && (pbr->unit_offset == unit_offset))
1598 return pbr;
1600 return NULL;
1603 /* Given BASE and UNIT_OFFSET, find the corresponding replacement expression
1604 and return it, assuming it is known it does not hold value by reference or
1605 in reverse storage order. */
1607 tree
1608 ipa_param_body_adjustments::lookup_replacement (tree base, unsigned unit_offset)
1610 ipa_param_body_replacement *pbr = lookup_replacement_1 (base, unit_offset);
1611 if (!pbr)
1612 return NULL;
1613 return pbr->repl;
1616 /* If T is an SSA_NAME, return NULL if it is not a default def or
1617 return its base variable if it is. If IGNORE_DEFAULT_DEF is true,
1618 the base variable is always returned, regardless if it is a default
1619 def. Return T if it is not an SSA_NAME. */
1621 static tree
1622 get_ssa_base_param (tree t, bool ignore_default_def)
1624 if (TREE_CODE (t) == SSA_NAME)
1626 if (ignore_default_def || SSA_NAME_IS_DEFAULT_DEF (t))
1627 return SSA_NAME_VAR (t);
1628 else
1629 return NULL_TREE;
1631 return t;
1634 /* Given an expression, return the structure describing how it should be
1635 replaced if it accesses a part of a split parameter or NULL otherwise.
1637 Do not free the result, it will be deallocated when the object is destroyed.
1639 If IGNORE_DEFAULT_DEF is cleared, consider only SSA_NAMEs of PARM_DECLs
1640 which are default definitions, if set, consider all SSA_NAMEs of
1641 PARM_DECLs. */
1643 ipa_param_body_replacement *
1644 ipa_param_body_adjustments::get_expr_replacement (tree expr,
1645 bool ignore_default_def)
1647 tree base;
1648 unsigned unit_offset;
1650 if (!isra_get_ref_base_and_offset (expr, &base, &unit_offset))
1651 return NULL;
1653 base = get_ssa_base_param (base, ignore_default_def);
1654 if (!base || TREE_CODE (base) != PARM_DECL)
1655 return NULL;
1656 return lookup_replacement_1 (base, unit_offset);
1659 /* Given OLD_DECL, which is a PARM_DECL of a parameter that is being removed
1660 (which includes it being split or replaced), return a new variable that
1661 should be used for any SSA names that will remain in the function that
1662 previously belonged to OLD_DECL. */
1664 tree
1665 ipa_param_body_adjustments::get_replacement_ssa_base (tree old_decl)
1667 unsigned *idx = m_removed_map.get (old_decl);
1668 if (!idx)
1669 return NULL;
1671 tree repl;
1672 if (TREE_CODE (m_removed_decls[*idx]) == PARM_DECL)
1674 gcc_assert (m_removed_decls[*idx] == old_decl);
1675 repl = copy_var_decl (old_decl, DECL_NAME (old_decl),
1676 TREE_TYPE (old_decl));
1677 m_removed_decls[*idx] = repl;
1679 else
1680 repl = m_removed_decls[*idx];
1681 return repl;
1684 /* If OLD_NAME, which is being defined by statement STMT, is an SSA_NAME of a
1685 parameter which is to be removed because its value is not used, create a new
1686 SSA_NAME relating to a replacement VAR_DECL, replace all uses of the
1687 original with it and return it. If there is no need to re-map, return NULL.
1688 ADJUSTMENTS is a pointer to a vector of IPA-SRA adjustments. */
1690 tree
1691 ipa_param_body_adjustments::replace_removed_params_ssa_names (tree old_name,
1692 gimple *stmt)
1694 gcc_assert (!m_id);
1695 if (TREE_CODE (old_name) != SSA_NAME)
1696 return NULL;
1698 tree decl = SSA_NAME_VAR (old_name);
1699 if (decl == NULL_TREE
1700 || TREE_CODE (decl) != PARM_DECL)
1701 return NULL;
1703 tree repl = get_replacement_ssa_base (decl);
1704 if (!repl)
1705 return NULL;
1707 tree new_name = make_ssa_name (repl, stmt);
1708 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name)
1709 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (old_name);
1711 if (dump_file && (dump_flags & TDF_DETAILS))
1713 fprintf (dump_file, "replacing an SSA name of a removed param ");
1714 print_generic_expr (dump_file, old_name);
1715 fprintf (dump_file, " with ");
1716 print_generic_expr (dump_file, new_name);
1717 fprintf (dump_file, "\n");
1720 replace_uses_by (old_name, new_name);
1721 return new_name;
1724 /* If the expression *EXPR_P should be replaced, do so. CONVERT specifies
1725 whether the function should care about type incompatibility of the current
1726 and new expressions. If it is false, the function will leave
1727 incompatibility issues to the caller - note that when the function
1728 encounters a BIT_FIELD_REF, IMAGPART_EXPR or REALPART_EXPR, it will modify
1729 their bases instead of the expressions themselves and then also performs any
1730 necessary conversions. */
1732 bool
1733 ipa_param_body_adjustments::modify_expression (tree *expr_p, bool convert)
1735 tree expr = *expr_p;
1737 if (TREE_CODE (expr) == BIT_FIELD_REF
1738 || TREE_CODE (expr) == IMAGPART_EXPR
1739 || TREE_CODE (expr) == REALPART_EXPR)
1741 expr_p = &TREE_OPERAND (expr, 0);
1742 expr = *expr_p;
1743 convert = true;
1746 ipa_param_body_replacement *pbr = get_expr_replacement (expr, false);
1747 if (!pbr)
1748 return false;
1750 tree repl = pbr->repl;
1751 if (dump_file && (dump_flags & TDF_DETAILS))
1753 fprintf (dump_file, "About to replace expr ");
1754 print_generic_expr (dump_file, expr);
1755 fprintf (dump_file, " with ");
1756 print_generic_expr (dump_file, repl);
1757 fprintf (dump_file, "\n");
1760 if (convert && !useless_type_conversion_p (TREE_TYPE (expr),
1761 TREE_TYPE (repl)))
1763 tree vce = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expr), repl);
1764 *expr_p = vce;
1766 else
1767 *expr_p = repl;
1768 return true;
1771 /* If the assignment statement STMT contains any expressions that need to
1772 replaced with a different one as noted by ADJUSTMENTS, do so. Handle any
1773 potential type incompatibilities. If any conversion sttements have to be
1774 pre-pended to STMT, they will be added to EXTRA_STMTS. Return true iff the
1775 statement was modified. */
1777 bool
1778 ipa_param_body_adjustments::modify_assignment (gimple *stmt,
1779 gimple_seq *extra_stmts)
1781 tree *lhs_p, *rhs_p;
1782 bool any;
1784 if (!gimple_assign_single_p (stmt))
1785 return false;
1787 rhs_p = gimple_assign_rhs1_ptr (stmt);
1788 lhs_p = gimple_assign_lhs_ptr (stmt);
1790 any = modify_expression (lhs_p, false);
1791 any |= modify_expression (rhs_p, false);
1792 if (any
1793 && !useless_type_conversion_p (TREE_TYPE (*lhs_p), TREE_TYPE (*rhs_p)))
1795 if (TREE_CODE (*rhs_p) == CONSTRUCTOR)
1797 /* V_C_Es of constructors can cause trouble (PR 42714). */
1798 if (is_gimple_reg_type (TREE_TYPE (*lhs_p)))
1799 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
1800 else
1801 *rhs_p = build_constructor (TREE_TYPE (*lhs_p),
1802 NULL);
1804 else
1806 tree new_rhs = fold_build1_loc (gimple_location (stmt),
1807 VIEW_CONVERT_EXPR, TREE_TYPE (*lhs_p),
1808 *rhs_p);
1809 tree tmp = force_gimple_operand (new_rhs, extra_stmts, true,
1810 NULL_TREE);
1811 gimple_assign_set_rhs1 (stmt, tmp);
1813 return true;
1816 return any;
1819 /* Record information about what modifications to call arguments have already
1820 been done by clone materialization into a summary describing CS. The
1821 information is stored in NEW_INDEX_MAP, NEW_PT_MAP and NEW_ALWAYS_COPY_DELTA
1822 and correspond to equivalent fields in ipa_edge_modification_info. Return
1823 the edge summary. */
1825 static ipa_edge_modification_info *
1826 record_argument_state_1 (cgraph_edge *cs, const vec<int> &new_index_map,
1827 const vec<pass_through_split_map> &new_pt_map,
1828 int new_always_copy_delta)
1831 ipa_edge_modification_info *sum = ipa_edge_modifications->get_create (cs);
1833 unsigned len = sum->pass_through_map.length ();
1834 for (unsigned i = 0; i < len; i++)
1836 unsigned oldnew = sum->pass_through_map[i].new_index;
1837 sum->pass_through_map[i].new_index = new_index_map[oldnew];
1840 len = sum->index_map.length ();
1841 if (len > 0)
1843 unsigned nptlen = new_pt_map.length ();
1844 for (unsigned j = 0; j < nptlen; j++)
1846 int inverse = -1;
1847 for (unsigned i = 0; i < len ; i++)
1848 if ((unsigned) sum->index_map[i] == new_pt_map[j].base_index)
1850 inverse = i;
1851 break;
1853 gcc_assert (inverse >= 0);
1854 pass_through_split_map ptm_item;
1856 ptm_item.base_index = inverse;
1857 ptm_item.unit_offset = new_pt_map[j].unit_offset;
1858 ptm_item.new_index = new_pt_map[j].new_index;
1859 sum->pass_through_map.safe_push (ptm_item);
1862 for (unsigned i = 0; i < len; i++)
1864 int idx = sum->index_map[i];
1865 if (idx < 0)
1866 continue;
1867 sum->index_map[i] = new_index_map[idx];
1870 else
1872 sum->pass_through_map.safe_splice (new_pt_map);
1873 sum->index_map.safe_splice (new_index_map);
1875 sum->always_copy_delta += new_always_copy_delta;
1876 return sum;
1879 /* Record information about what modifications to call arguments have already
1880 been done by clone materialization into a summary of an edge describing the
1881 call in this clone and all its clones. NEW_INDEX_MAP, NEW_PT_MAP and
1882 NEW_ALWAYS_COPY_DELTA have the same meaning as record_argument_state_1.
1884 In order to associate the info with the right edge summaries, we need
1885 address of the ORIG_STMT in the function from which we are cloning (because
1886 the edges have not yet been re-assigned to the new statement that has just
1887 been created) and ID, the structure governing function body copying. */
1889 static void
1890 record_argument_state (copy_body_data *id, gimple *orig_stmt,
1891 const vec<int> &new_index_map,
1892 const vec<pass_through_split_map> &new_pt_map,
1893 int new_always_copy_delta)
1895 if (!ipa_edge_modifications)
1896 ipa_edge_modifications = new ipa_edge_modification_sum (symtab);
1898 struct cgraph_node *this_node = id->dst_node;
1899 ipa_edge_modification_info *first_sum = NULL;
1900 cgraph_edge *cs = this_node->get_edge (orig_stmt);
1901 if (cs)
1902 first_sum = record_argument_state_1 (cs, new_index_map, new_pt_map,
1903 new_always_copy_delta);
1904 else
1905 gcc_assert (this_node->clones);
1907 if (!this_node->clones)
1908 return;
1909 for (cgraph_node *subclone = this_node->clones; subclone != this_node;)
1911 cs = subclone->get_edge (orig_stmt);
1912 if (cs)
1914 if (!first_sum)
1915 first_sum = record_argument_state_1 (cs, new_index_map, new_pt_map,
1916 new_always_copy_delta);
1917 else
1919 ipa_edge_modification_info *s2
1920 = ipa_edge_modifications->get_create (cs);
1921 s2->index_map.truncate (0);
1922 s2->index_map.safe_splice (first_sum->index_map);
1923 s2->pass_through_map.truncate (0);
1924 s2->pass_through_map.safe_splice (first_sum->pass_through_map);
1925 s2->always_copy_delta = first_sum->always_copy_delta;
1928 else
1929 gcc_assert (subclone->clones);
1931 if (subclone->clones)
1932 subclone = subclone->clones;
1933 else if (subclone->next_sibling_clone)
1934 subclone = subclone->next_sibling_clone;
1935 else
1937 while (subclone != this_node && !subclone->next_sibling_clone)
1938 subclone = subclone->clone_of;
1939 if (subclone != this_node)
1940 subclone = subclone->next_sibling_clone;
1945 /* If the call statement pointed at by STMT_P contains any expressions that
1946 need to replaced with a different one as noted by ADJUSTMENTS, do so. f the
1947 statement needs to be rebuilt, do so. Return true if any modifications have
1948 been performed. ORIG_STMT, if not NULL, is the original statement in the
1949 function that is being cloned from, which at this point can be used to look
1950 up call_graph edges.
1952 If the method is invoked as a part of IPA clone materialization and if any
1953 parameter split is pass-through, i.e. it applies to the functin that is
1954 being modified and also to the callee of the statement, replace the
1955 parameter passed to old callee with all of the replacement a callee might
1956 possibly want and record the performed argument modifications in
1957 ipa_edge_modifications. Likewise if any argument has already been left out
1958 because it is not necessary. */
1960 bool
1961 ipa_param_body_adjustments::modify_call_stmt (gcall **stmt_p,
1962 gimple *orig_stmt)
1964 auto_vec <unsigned, 4> pass_through_args;
1965 auto_vec <unsigned, 4> pass_through_pbr_indices;
1966 auto_vec <HOST_WIDE_INT, 4> pass_through_offsets;
1967 gcall *stmt = *stmt_p;
1968 unsigned nargs = gimple_call_num_args (stmt);
1969 bool recreate = false;
1971 for (unsigned i = 0; i < gimple_call_num_args (stmt); i++)
1973 tree t = gimple_call_arg (stmt, i);
1974 gcc_assert (TREE_CODE (t) != BIT_FIELD_REF
1975 && TREE_CODE (t) != IMAGPART_EXPR
1976 && TREE_CODE (t) != REALPART_EXPR);
1978 if (TREE_CODE (t) == SSA_NAME
1979 && m_dead_ssas.contains (t))
1980 recreate = true;
1982 if (!m_split_modifications_p)
1983 continue;
1985 tree base;
1986 unsigned agg_arg_offset;
1987 if (!isra_get_ref_base_and_offset (t, &base, &agg_arg_offset))
1988 continue;
1990 bool by_ref = false;
1991 if (TREE_CODE (base) == SSA_NAME)
1993 if (!SSA_NAME_IS_DEFAULT_DEF (base))
1994 continue;
1995 base = SSA_NAME_VAR (base);
1996 gcc_checking_assert (base);
1997 by_ref = true;
1999 if (TREE_CODE (base) != PARM_DECL)
2000 continue;
2002 bool base_among_replacements = false;
2003 unsigned j, repl_list_len = m_replacements.length ();
2004 for (j = 0; j < repl_list_len; j++)
2006 ipa_param_body_replacement *pbr = &m_replacements[j];
2007 if (pbr->base == base)
2009 base_among_replacements = true;
2010 break;
2013 if (!base_among_replacements)
2014 continue;
2016 /* We still have to distinguish between an end-use that we have to
2017 transform now and a pass-through, which happens in the following
2018 two cases. */
2020 /* TODO: After we adjust ptr_parm_has_nonarg_uses to also consider
2021 &MEM_REF[ssa_name + offset], we will also have to detect that case
2022 here. */
2024 if (TREE_CODE (t) == SSA_NAME
2025 && SSA_NAME_IS_DEFAULT_DEF (t)
2026 && SSA_NAME_VAR (t)
2027 && TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL)
2029 /* This must be a by_reference pass-through. */
2030 recreate = true;
2031 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2032 pass_through_args.safe_push (i);
2033 pass_through_pbr_indices.safe_push (j);
2034 pass_through_offsets.safe_push (agg_arg_offset);
2036 else if (!by_ref && AGGREGATE_TYPE_P (TREE_TYPE (t)))
2038 /* Currently IPA-SRA guarantees the aggregate access type
2039 exactly matches in this case. So if it does not match, it is
2040 a pass-through argument that will be sorted out at edge
2041 redirection time. */
2042 ipa_param_body_replacement *pbr
2043 = lookup_replacement_1 (base, agg_arg_offset);
2045 if (!pbr
2046 || (TYPE_MAIN_VARIANT (TREE_TYPE (t))
2047 != TYPE_MAIN_VARIANT (TREE_TYPE (pbr->repl))))
2049 recreate = true;
2050 pass_through_args.safe_push (i);
2051 pass_through_pbr_indices.safe_push (j);
2052 pass_through_offsets.safe_push (agg_arg_offset);
2057 if (!recreate)
2059 /* No need to rebuild the statement, let's just modify arguments
2060 and the LHS if/as appropriate. */
2061 bool modified = false;
2062 for (unsigned i = 0; i < nargs; i++)
2064 tree *t = gimple_call_arg_ptr (stmt, i);
2065 modified |= modify_expression (t, true);
2067 if (gimple_call_lhs (stmt))
2069 tree *t = gimple_call_lhs_ptr (stmt);
2070 modified |= modify_expression (t, false);
2072 return modified;
2075 auto_vec<int, 16> index_map;
2076 auto_vec<pass_through_split_map, 4> pass_through_map;
2077 auto_vec<tree, 16> vargs;
2078 int always_copy_delta = 0;
2079 unsigned pt_idx = 0;
2080 int new_arg_idx = 0;
2081 for (unsigned i = 0; i < nargs; i++)
2083 if (pt_idx < pass_through_args.length ()
2084 && i == pass_through_args[pt_idx])
2086 unsigned j = pass_through_pbr_indices[pt_idx];
2087 unsigned agg_arg_offset = pass_through_offsets[pt_idx];
2088 pt_idx++;
2089 always_copy_delta--;
2090 tree base = m_replacements[j].base;
2092 /* In order to be put into SSA form, we have to push all replacements
2093 pertaining to this parameter as parameters to the call statement.
2094 Edge redirection will need to use edge summary to weed out the
2095 unnecessary ones. */
2096 unsigned repl_list_len = m_replacements.length ();
2097 for (; j < repl_list_len; j++)
2099 if (m_replacements[j].base != base)
2100 break;
2101 if (m_replacements[j].unit_offset < agg_arg_offset)
2102 continue;
2103 pass_through_split_map pt_map;
2104 pt_map.base_index = i;
2105 pt_map.unit_offset
2106 = m_replacements[j].unit_offset - agg_arg_offset;
2107 pt_map.new_index = new_arg_idx;
2108 pass_through_map.safe_push (pt_map);
2109 vargs.safe_push (m_replacements[j].repl);
2110 new_arg_idx++;
2111 always_copy_delta++;
2113 index_map.safe_push (-1);
2115 else
2117 tree t = gimple_call_arg (stmt, i);
2118 if (TREE_CODE (t) == SSA_NAME
2119 && m_dead_ssas.contains (t))
2121 always_copy_delta--;
2122 index_map.safe_push (-1);
2124 else
2126 modify_expression (&t, true);
2127 vargs.safe_push (t);
2128 index_map.safe_push (new_arg_idx);
2129 new_arg_idx++;
2134 gcall *new_stmt = gimple_build_call_vec (gimple_call_fn (stmt), vargs);
2135 if (gimple_has_location (stmt))
2136 gimple_set_location (new_stmt, gimple_location (stmt));
2137 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
2138 gimple_call_copy_flags (new_stmt, stmt);
2139 if (tree lhs = gimple_call_lhs (stmt))
2141 modify_expression (&lhs, false);
2142 /* Avoid adjusting SSA_NAME_DEF_STMT of a SSA lhs, SSA names
2143 have not yet been remapped. */
2144 *gimple_call_lhs_ptr (new_stmt) = lhs;
2146 *stmt_p = new_stmt;
2148 if (orig_stmt)
2149 record_argument_state (m_id, orig_stmt, index_map, pass_through_map,
2150 always_copy_delta);
2151 return true;
2154 /* If the statement STMT contains any expressions that need to replaced with a
2155 different one as noted by ADJUSTMENTS, do so. Handle any potential type
2156 incompatibilities. If any conversion sttements have to be pre-pended to
2157 STMT, they will be added to EXTRA_STMTS. Return true iff the statement was
2158 modified. */
2160 bool
2161 ipa_param_body_adjustments::modify_gimple_stmt (gimple **stmt,
2162 gimple_seq *extra_stmts,
2163 gimple *orig_stmt)
2165 bool modified = false;
2166 tree *t;
2168 switch (gimple_code (*stmt))
2170 case GIMPLE_RETURN:
2171 t = gimple_return_retval_ptr (as_a <greturn *> (*stmt));
2172 if (m_adjustments && m_adjustments->m_skip_return)
2173 *t = NULL_TREE;
2174 else if (*t != NULL_TREE)
2175 modified |= modify_expression (t, true);
2176 break;
2178 case GIMPLE_ASSIGN:
2179 modified |= modify_assignment (*stmt, extra_stmts);
2180 break;
2182 case GIMPLE_CALL:
2183 modified |= modify_call_stmt ((gcall **) stmt, orig_stmt);
2184 break;
2186 case GIMPLE_ASM:
2188 gasm *asm_stmt = as_a <gasm *> (*stmt);
2189 for (unsigned i = 0; i < gimple_asm_ninputs (asm_stmt); i++)
2191 t = &TREE_VALUE (gimple_asm_input_op (asm_stmt, i));
2192 modified |= modify_expression (t, true);
2194 for (unsigned i = 0; i < gimple_asm_noutputs (asm_stmt); i++)
2196 t = &TREE_VALUE (gimple_asm_output_op (asm_stmt, i));
2197 modified |= modify_expression (t, false);
2200 break;
2202 default:
2203 break;
2205 return modified;
2209 /* Traverse body of the current function and perform the requested adjustments
2210 on its statements. Return true iff the CFG has been changed. */
2212 bool
2213 ipa_param_body_adjustments::modify_cfun_body ()
2215 bool cfg_changed = false;
2216 basic_block bb;
2218 FOR_EACH_BB_FN (bb, cfun)
2220 gimple_stmt_iterator gsi;
2222 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2224 gphi *phi = as_a <gphi *> (gsi_stmt (gsi));
2225 tree new_lhs, old_lhs = gimple_phi_result (phi);
2226 new_lhs = replace_removed_params_ssa_names (old_lhs, phi);
2227 if (new_lhs)
2229 gimple_phi_set_result (phi, new_lhs);
2230 release_ssa_name (old_lhs);
2234 gsi = gsi_start_bb (bb);
2235 while (!gsi_end_p (gsi))
2237 gimple *stmt = gsi_stmt (gsi);
2238 gimple *stmt_copy = stmt;
2239 gimple_seq extra_stmts = NULL;
2240 bool modified = modify_gimple_stmt (&stmt, &extra_stmts, NULL);
2241 if (stmt != stmt_copy)
2243 gcc_checking_assert (modified);
2244 gsi_replace (&gsi, stmt, false);
2246 if (!gimple_seq_empty_p (extra_stmts))
2247 gsi_insert_seq_before (&gsi, extra_stmts, GSI_SAME_STMT);
2249 def_operand_p defp;
2250 ssa_op_iter iter;
2251 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF)
2253 tree old_def = DEF_FROM_PTR (defp);
2254 if (tree new_def = replace_removed_params_ssa_names (old_def,
2255 stmt))
2257 SET_DEF (defp, new_def);
2258 release_ssa_name (old_def);
2259 modified = true;
2263 if (modified)
2265 update_stmt (stmt);
2266 if (maybe_clean_eh_stmt (stmt)
2267 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
2268 cfg_changed = true;
2270 gsi_next (&gsi);
2274 return cfg_changed;
2277 /* Call gimple_debug_bind_reset_value on all debug statements describing
2278 gimple register parameters that are being removed or replaced. */
2280 void
2281 ipa_param_body_adjustments::reset_debug_stmts ()
2283 int i, len;
2284 gimple_stmt_iterator *gsip = NULL, gsi;
2286 if (MAY_HAVE_DEBUG_STMTS && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
2288 gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
2289 gsip = &gsi;
2291 len = m_reset_debug_decls.length ();
2292 for (i = 0; i < len; i++)
2294 imm_use_iterator ui;
2295 gimple *stmt;
2296 gdebug *def_temp;
2297 tree name, vexpr, copy = NULL_TREE;
2298 use_operand_p use_p;
2299 tree decl = m_reset_debug_decls[i];
2301 gcc_checking_assert (is_gimple_reg (decl));
2302 name = ssa_default_def (cfun, decl);
2303 vexpr = NULL;
2304 if (name)
2305 FOR_EACH_IMM_USE_STMT (stmt, ui, name)
2307 if (gimple_clobber_p (stmt))
2309 gimple_stmt_iterator cgsi = gsi_for_stmt (stmt);
2310 unlink_stmt_vdef (stmt);
2311 gsi_remove (&cgsi, true);
2312 release_defs (stmt);
2313 continue;
2315 /* All other users must have been removed by function body
2316 modification. */
2317 gcc_assert (is_gimple_debug (stmt));
2318 if (vexpr == NULL && gsip != NULL)
2320 vexpr = build_debug_expr_decl (TREE_TYPE (name));
2321 /* FIXME: Is setting the mode really necessary? */
2322 SET_DECL_MODE (vexpr, DECL_MODE (decl));
2323 def_temp = gimple_build_debug_source_bind (vexpr, decl, NULL);
2324 gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
2326 if (vexpr)
2328 FOR_EACH_IMM_USE_ON_STMT (use_p, ui)
2329 SET_USE (use_p, vexpr);
2331 else
2332 gimple_debug_bind_reset_value (stmt);
2333 update_stmt (stmt);
2335 /* Create a VAR_DECL for debug info purposes. */
2336 if (!DECL_IGNORED_P (decl))
2338 copy = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
2339 VAR_DECL, DECL_NAME (decl),
2340 TREE_TYPE (decl));
2341 if (DECL_PT_UID_SET_P (decl))
2342 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
2343 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
2344 TREE_READONLY (copy) = TREE_READONLY (decl);
2345 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
2346 DECL_NOT_GIMPLE_REG_P (copy) = DECL_NOT_GIMPLE_REG_P (decl);
2347 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
2348 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
2349 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
2350 DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
2351 SET_DECL_RTL (copy, 0);
2352 TREE_USED (copy) = 1;
2353 DECL_CONTEXT (copy) = current_function_decl;
2354 add_local_decl (cfun, copy);
2355 DECL_CHAIN (copy)
2356 = BLOCK_VARS (DECL_INITIAL (current_function_decl));
2357 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = copy;
2359 if (gsip != NULL && copy && target_for_debug_bind (decl))
2361 gcc_assert (TREE_CODE (decl) == PARM_DECL);
2362 if (vexpr)
2363 def_temp = gimple_build_debug_bind (copy, vexpr, NULL);
2364 else
2365 def_temp = gimple_build_debug_source_bind (copy, decl,
2366 NULL);
2367 gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
2372 /* Perform all necessary body changes to change signature, body and debug info
2373 of fun according to adjustments passed at construction. Return true if CFG
2374 was changed in any way. The main entry point for modification of standalone
2375 functions that is not part of IPA clone materialization. */
2377 bool
2378 ipa_param_body_adjustments::perform_cfun_body_modifications ()
2380 bool cfg_changed;
2381 modify_formal_parameters ();
2382 cfg_changed = modify_cfun_body ();
2383 reset_debug_stmts ();
2385 return cfg_changed;
2389 /* Deallocate summaries which otherwise stay alive until the end of
2390 compilation. */
2392 void
2393 ipa_edge_modifications_finalize ()
2395 if (!ipa_edge_modifications)
2396 return;
2397 delete ipa_edge_modifications;
2398 ipa_edge_modifications = NULL;