Fix typo in t-dimode
[official-gcc.git] / gcc / ipa-param-manipulation.c
blob479c20b3871e71cf6310266d77efbc245d0ba93d
1 /* Manipulation of formal and actual parameters of functions and function
2 calls.
3 Copyright (C) 2017-2021 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 virtual void duplicate (cgraph_edge *,
118 cgraph_edge *,
119 ipa_edge_modification_info *old_info,
120 ipa_edge_modification_info *new_info)
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-sso");
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.c 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.c, 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 m_dead_ssa_debug_equiv.put (dead_ssa, *d);
1284 return (*d != NULL_TREE);
1287 tree val
1288 = unshare_expr_without_location (gimple_assign_rhs_to_tree (def));
1289 remap_with_debug_expressions (&val);
1291 tree vexpr = build_debug_expr_decl (TREE_TYPE (val));
1292 m_dead_stmt_debug_equiv.put (def, val);
1293 m_dead_ssa_debug_equiv.put (dead_ssa, vexpr);
1294 return true;
1296 else
1297 gcc_unreachable ();
1300 /* Common initialization performed by all ipa_param_body_adjustments
1301 constructors. OLD_FNDECL is the declaration we take original arguments
1302 from, (it may be the same as M_FNDECL). VARS, if non-NULL, is a pointer to
1303 a chained list of new local variables. TREE_MAP is the IPA-CP produced
1304 mapping of trees to constants.
1306 The function is rather long but it really onlu initializes all data members
1307 of the class. It creates new param DECLs, finds their new types, */
1309 void
1310 ipa_param_body_adjustments::common_initialization (tree old_fndecl,
1311 tree *vars,
1312 vec<ipa_replace_map *,
1313 va_gc> *tree_map)
1315 push_function_arg_decls (&m_oparms, old_fndecl);
1316 auto_vec<tree,16> otypes;
1317 if (TYPE_ARG_TYPES (TREE_TYPE (old_fndecl)) != NULL_TREE)
1318 push_function_arg_types (&otypes, TREE_TYPE (old_fndecl));
1319 else
1321 auto_vec<tree,16> oparms;
1322 push_function_arg_decls (&oparms, old_fndecl);
1323 unsigned ocount = oparms.length ();
1324 otypes.reserve_exact (ocount);
1325 for (unsigned i = 0; i < ocount; i++)
1326 otypes.quick_push (TREE_TYPE (oparms[i]));
1328 fill_vector_of_new_param_types (&m_new_types, &otypes, m_adj_params, true);
1330 auto_vec<bool, 16> kept;
1331 kept.reserve_exact (m_oparms.length ());
1332 kept.quick_grow_cleared (m_oparms.length ());
1333 auto_vec<bool, 16> split;
1334 split.reserve_exact (m_oparms.length ());
1335 split.quick_grow_cleared (m_oparms.length ());
1337 unsigned adj_len = vec_safe_length (m_adj_params);
1338 m_method2func = ((TREE_CODE (TREE_TYPE (m_fndecl)) == METHOD_TYPE)
1339 && (adj_len == 0
1340 || (*m_adj_params)[0].op != IPA_PARAM_OP_COPY
1341 || (*m_adj_params)[0].base_index != 0));
1343 /* The main job of the this function is to go over the vector of adjusted
1344 parameters and create declarations or find corresponding old ones and push
1345 them to m_new_decls. For IPA-SRA replacements it also creates
1346 corresponding m_id->dst_node->clone.performed_splits entries. */
1348 m_new_decls.reserve_exact (adj_len);
1349 for (unsigned i = 0; i < adj_len ; i++)
1351 ipa_adjusted_param *apm = &(*m_adj_params)[i];
1352 unsigned prev_index = apm->prev_clone_index;
1353 tree new_parm;
1354 if (apm->op == IPA_PARAM_OP_COPY
1355 || apm->prev_clone_adjustment)
1357 kept[prev_index] = true;
1358 new_parm = carry_over_param (m_oparms[prev_index]);
1359 m_new_decls.quick_push (new_parm);
1361 else if (apm->op == IPA_PARAM_OP_NEW
1362 || apm->op == IPA_PARAM_OP_SPLIT)
1364 tree new_type = m_new_types[i];
1365 gcc_checking_assert (new_type);
1366 new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
1367 new_type);
1368 const char *prefix = ipa_param_prefixes[apm->param_prefix_index];
1369 DECL_NAME (new_parm) = create_tmp_var_name (prefix);
1370 DECL_ARTIFICIAL (new_parm) = 1;
1371 DECL_ARG_TYPE (new_parm) = new_type;
1372 DECL_CONTEXT (new_parm) = m_fndecl;
1373 TREE_USED (new_parm) = 1;
1374 DECL_IGNORED_P (new_parm) = 1;
1375 layout_decl (new_parm, 0);
1376 m_new_decls.quick_push (new_parm);
1378 if (apm->op == IPA_PARAM_OP_SPLIT)
1380 m_split_modifications_p = true;
1381 split[prev_index] = true;
1382 register_replacement (apm, new_parm);
1385 else
1386 gcc_unreachable ();
1389 if (tree_map)
1391 /* Do not treat parameters which were replaced with a constant as
1392 completely vanished. */
1393 auto_vec <int, 16> index_mapping;
1394 bool need_remap = false;
1396 if (m_id)
1398 clone_info *cinfo = clone_info::get (m_id->src_node);
1399 if (cinfo && cinfo->param_adjustments)
1401 cinfo->param_adjustments->get_updated_indices (&index_mapping);
1402 need_remap = true;
1406 for (unsigned i = 0; i < tree_map->length (); i++)
1408 int parm_num = (*tree_map)[i]->parm_num;
1409 gcc_assert (parm_num >= 0);
1410 if (need_remap)
1411 parm_num = index_mapping[parm_num];
1412 kept[parm_num] = true;
1416 /* As part of body modifications, we will also have to replace remaining uses
1417 of remaining uses of removed PARM_DECLs (which do not however use the
1418 initial value) with their VAR_DECL copies.
1420 We do this differently with and without m_id. With m_id, we rely on its
1421 mapping and create a replacement straight away. Without it, we have our
1422 own mechanism for which we have to populate m_removed_decls vector. Just
1423 don't mix them, that is why you should not call
1424 replace_removed_params_ssa_names or perform_cfun_body_modifications when
1425 you construct with ID not equal to NULL. */
1427 auto_vec<tree, 8> ssas_to_process_debug;
1428 unsigned op_len = m_oparms.length ();
1429 for (unsigned i = 0; i < op_len; i++)
1430 if (!kept[i])
1432 if (m_id)
1434 gcc_assert (!m_id->decl_map->get (m_oparms[i]));
1435 tree var = copy_decl_to_var (m_oparms[i], m_id);
1436 insert_decl_map (m_id, m_oparms[i], var);
1437 /* Declare this new variable. */
1438 DECL_CHAIN (var) = *vars;
1439 *vars = var;
1441 /* If this is not a split but a real removal, init hash sets
1442 that will guide what not to copy to the new body. */
1443 if (!split[i])
1444 mark_dead_statements (m_oparms[i], &ssas_to_process_debug);
1445 if (MAY_HAVE_DEBUG_STMTS
1446 && is_gimple_reg (m_oparms[i]))
1447 m_reset_debug_decls.safe_push (m_oparms[i]);
1449 else
1451 m_removed_decls.safe_push (m_oparms[i]);
1452 m_removed_map.put (m_oparms[i], m_removed_decls.length () - 1);
1453 if (MAY_HAVE_DEBUG_STMTS
1454 && !kept[i]
1455 && is_gimple_reg (m_oparms[i]))
1456 m_reset_debug_decls.safe_push (m_oparms[i]);
1460 while (!ssas_to_process_debug.is_empty ())
1461 prepare_debug_expressions (ssas_to_process_debug.pop ());
1464 /* Constructor of ipa_param_body_adjustments from a simple list of
1465 modifications to parameters listed in ADJ_PARAMS which will prepare ground
1466 for modification of parameters of fndecl. Return value of the function will
1467 not be removed and the object will assume it does not run as a part of
1468 tree-function_versioning. */
1470 ipa_param_body_adjustments
1471 ::ipa_param_body_adjustments (vec<ipa_adjusted_param, va_gc> *adj_params,
1472 tree fndecl)
1473 : m_adj_params (adj_params), m_adjustments (NULL), m_reset_debug_decls (),
1474 m_split_modifications_p (false), m_dead_stmts (), m_dead_ssas (),
1475 m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (), m_fndecl (fndecl),
1476 m_id (NULL), m_oparms (), m_new_decls (), m_new_types (), m_replacements (),
1477 m_removed_decls (), m_removed_map (), m_method2func (false)
1479 common_initialization (fndecl, NULL, NULL);
1482 /* Constructor of ipa_param_body_adjustments from ipa_param_adjustments in
1483 ADJUSTMENTS which will prepare ground for modification of parameters of
1484 fndecl. The object will assume it does not run as a part of
1485 tree-function_versioning. */
1487 ipa_param_body_adjustments
1488 ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
1489 tree fndecl)
1490 : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
1491 m_reset_debug_decls (), m_split_modifications_p (false), m_dead_stmts (),
1492 m_dead_ssas (), m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (),
1493 m_fndecl (fndecl), m_id (NULL), m_oparms (), m_new_decls (),
1494 m_new_types (), m_replacements (), m_removed_decls (), m_removed_map (),
1495 m_method2func (false)
1497 common_initialization (fndecl, NULL, NULL);
1500 /* Constructor of ipa_param_body_adjustments which sets it up as a part of
1501 running tree_function_versioning. Planned modifications to the function are
1502 in ADJUSTMENTS. FNDECL designates the new function clone which is being
1503 modified. OLD_FNDECL is the function of which FNDECL is a clone (and which
1504 at the time of invocation still share DECL_ARGUMENTS). ID is the
1505 copy_body_data structure driving the wholy body copying process. VARS is a
1506 pointer to the head of the list of new local variables, TREE_MAP is the map
1507 that drives tree substitution in the cloning process. */
1509 ipa_param_body_adjustments
1510 ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
1511 tree fndecl, tree old_fndecl,
1512 copy_body_data *id, tree *vars,
1513 vec<ipa_replace_map *, va_gc> *tree_map)
1514 : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
1515 m_reset_debug_decls (), m_split_modifications_p (false), m_dead_stmts (),
1516 m_dead_ssas (), m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (),
1517 m_fndecl (fndecl), m_id (id), m_oparms (), m_new_decls (), m_new_types (),
1518 m_replacements (), m_removed_decls (), m_removed_map (),
1519 m_method2func (false)
1521 common_initialization (old_fndecl, vars, tree_map);
1524 /* Chain new param decls up and return them. */
1526 tree
1527 ipa_param_body_adjustments::get_new_param_chain ()
1529 tree result;
1530 tree *link = &result;
1532 unsigned len = vec_safe_length (m_adj_params);
1533 for (unsigned i = 0; i < len; i++)
1535 tree new_decl = m_new_decls[i];
1536 *link = new_decl;
1537 link = &DECL_CHAIN (new_decl);
1539 *link = NULL_TREE;
1540 return result;
1543 /* Modify the function parameters FNDECL and its type according to the plan in
1544 ADJUSTMENTS. This function needs to be called when the decl has not already
1545 been processed with ipa_param_adjustments::adjust_decl, otherwise just
1546 seting DECL_ARGUMENTS to whatever get_new_param_chain will do is enough. */
1548 void
1549 ipa_param_body_adjustments::modify_formal_parameters ()
1551 tree orig_type = TREE_TYPE (m_fndecl);
1552 DECL_ARGUMENTS (m_fndecl) = get_new_param_chain ();
1554 /* When signature changes, we need to clear builtin info. */
1555 if (fndecl_built_in_p (m_fndecl))
1556 set_decl_built_in_function (m_fndecl, NOT_BUILT_IN, 0);
1558 bool modified = false;
1559 size_t index = 0;
1560 if (m_adj_params)
1561 for (tree t = TYPE_ARG_TYPES (orig_type);
1562 t && !modified;
1563 t = TREE_CHAIN (t), index++)
1564 if (index >= m_adj_params->length ()
1565 || (*m_adj_params)[index].op != IPA_PARAM_OP_COPY
1566 || (*m_adj_params)[index].base_index != index)
1567 modified = true;
1569 /* At this point, removing return value is only implemented when going
1570 through tree_function_versioning, not when modifying function body
1571 directly. */
1572 gcc_assert (!m_adjustments || !m_adjustments->m_skip_return);
1573 tree new_type = build_adjusted_function_type (orig_type, &m_new_types,
1574 m_method2func, false, modified);
1576 TREE_TYPE (m_fndecl) = new_type;
1577 DECL_VIRTUAL_P (m_fndecl) = 0;
1578 DECL_LANG_SPECIFIC (m_fndecl) = NULL;
1579 if (m_method2func)
1580 DECL_VINDEX (m_fndecl) = NULL_TREE;
1583 /* Given BASE and UNIT_OFFSET, find the corresponding record among replacement
1584 structures. */
1586 ipa_param_body_replacement *
1587 ipa_param_body_adjustments::lookup_replacement_1 (tree base,
1588 unsigned unit_offset)
1590 unsigned int len = m_replacements.length ();
1591 for (unsigned i = 0; i < len; i++)
1593 ipa_param_body_replacement *pbr = &m_replacements[i];
1595 if (pbr->base == base
1596 && (pbr->unit_offset == unit_offset))
1597 return pbr;
1599 return NULL;
1602 /* Given BASE and UNIT_OFFSET, find the corresponding replacement expression
1603 and return it, assuming it is known it does not hold value by reference or
1604 in reverse storage order. */
1606 tree
1607 ipa_param_body_adjustments::lookup_replacement (tree base, unsigned unit_offset)
1609 ipa_param_body_replacement *pbr = lookup_replacement_1 (base, unit_offset);
1610 if (!pbr)
1611 return NULL;
1612 return pbr->repl;
1615 /* If T is an SSA_NAME, return NULL if it is not a default def or
1616 return its base variable if it is. If IGNORE_DEFAULT_DEF is true,
1617 the base variable is always returned, regardless if it is a default
1618 def. Return T if it is not an SSA_NAME. */
1620 static tree
1621 get_ssa_base_param (tree t, bool ignore_default_def)
1623 if (TREE_CODE (t) == SSA_NAME)
1625 if (ignore_default_def || SSA_NAME_IS_DEFAULT_DEF (t))
1626 return SSA_NAME_VAR (t);
1627 else
1628 return NULL_TREE;
1630 return t;
1633 /* Given an expression, return the structure describing how it should be
1634 replaced if it accesses a part of a split parameter or NULL otherwise.
1636 Do not free the result, it will be deallocated when the object is destroyed.
1638 If IGNORE_DEFAULT_DEF is cleared, consider only SSA_NAMEs of PARM_DECLs
1639 which are default definitions, if set, consider all SSA_NAMEs of
1640 PARM_DECLs. */
1642 ipa_param_body_replacement *
1643 ipa_param_body_adjustments::get_expr_replacement (tree expr,
1644 bool ignore_default_def)
1646 tree base;
1647 unsigned unit_offset;
1649 if (!isra_get_ref_base_and_offset (expr, &base, &unit_offset))
1650 return NULL;
1652 base = get_ssa_base_param (base, ignore_default_def);
1653 if (!base || TREE_CODE (base) != PARM_DECL)
1654 return NULL;
1655 return lookup_replacement_1 (base, unit_offset);
1658 /* Given OLD_DECL, which is a PARM_DECL of a parameter that is being removed
1659 (which includes it being split or replaced), return a new variable that
1660 should be used for any SSA names that will remain in the function that
1661 previously belonged to OLD_DECL. */
1663 tree
1664 ipa_param_body_adjustments::get_replacement_ssa_base (tree old_decl)
1666 unsigned *idx = m_removed_map.get (old_decl);
1667 if (!idx)
1668 return NULL;
1670 tree repl;
1671 if (TREE_CODE (m_removed_decls[*idx]) == PARM_DECL)
1673 gcc_assert (m_removed_decls[*idx] == old_decl);
1674 repl = copy_var_decl (old_decl, DECL_NAME (old_decl),
1675 TREE_TYPE (old_decl));
1676 m_removed_decls[*idx] = repl;
1678 else
1679 repl = m_removed_decls[*idx];
1680 return repl;
1683 /* If OLD_NAME, which is being defined by statement STMT, is an SSA_NAME of a
1684 parameter which is to be removed because its value is not used, create a new
1685 SSA_NAME relating to a replacement VAR_DECL, replace all uses of the
1686 original with it and return it. If there is no need to re-map, return NULL.
1687 ADJUSTMENTS is a pointer to a vector of IPA-SRA adjustments. */
1689 tree
1690 ipa_param_body_adjustments::replace_removed_params_ssa_names (tree old_name,
1691 gimple *stmt)
1693 gcc_assert (!m_id);
1694 if (TREE_CODE (old_name) != SSA_NAME)
1695 return NULL;
1697 tree decl = SSA_NAME_VAR (old_name);
1698 if (decl == NULL_TREE
1699 || TREE_CODE (decl) != PARM_DECL)
1700 return NULL;
1702 tree repl = get_replacement_ssa_base (decl);
1703 if (!repl)
1704 return NULL;
1706 tree new_name = make_ssa_name (repl, stmt);
1707 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name)
1708 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (old_name);
1710 if (dump_file && (dump_flags & TDF_DETAILS))
1712 fprintf (dump_file, "replacing an SSA name of a removed param ");
1713 print_generic_expr (dump_file, old_name);
1714 fprintf (dump_file, " with ");
1715 print_generic_expr (dump_file, new_name);
1716 fprintf (dump_file, "\n");
1719 replace_uses_by (old_name, new_name);
1720 return new_name;
1723 /* If the expression *EXPR_P should be replaced, do so. CONVERT specifies
1724 whether the function should care about type incompatibility of the current
1725 and new expressions. If it is false, the function will leave
1726 incompatibility issues to the caller - note that when the function
1727 encounters a BIT_FIELD_REF, IMAGPART_EXPR or REALPART_EXPR, it will modify
1728 their bases instead of the expressions themselves and then also performs any
1729 necessary conversions. */
1731 bool
1732 ipa_param_body_adjustments::modify_expression (tree *expr_p, bool convert)
1734 tree expr = *expr_p;
1736 if (TREE_CODE (expr) == BIT_FIELD_REF
1737 || TREE_CODE (expr) == IMAGPART_EXPR
1738 || TREE_CODE (expr) == REALPART_EXPR)
1740 expr_p = &TREE_OPERAND (expr, 0);
1741 expr = *expr_p;
1742 convert = true;
1745 ipa_param_body_replacement *pbr = get_expr_replacement (expr, false);
1746 if (!pbr)
1747 return false;
1749 tree repl = pbr->repl;
1750 if (dump_file && (dump_flags & TDF_DETAILS))
1752 fprintf (dump_file, "About to replace expr ");
1753 print_generic_expr (dump_file, expr);
1754 fprintf (dump_file, " with ");
1755 print_generic_expr (dump_file, repl);
1756 fprintf (dump_file, "\n");
1759 if (convert && !useless_type_conversion_p (TREE_TYPE (expr),
1760 TREE_TYPE (repl)))
1762 tree vce = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expr), repl);
1763 *expr_p = vce;
1765 else
1766 *expr_p = repl;
1767 return true;
1770 /* If the assignment statement STMT contains any expressions that need to
1771 replaced with a different one as noted by ADJUSTMENTS, do so. Handle any
1772 potential type incompatibilities. If any conversion sttements have to be
1773 pre-pended to STMT, they will be added to EXTRA_STMTS. Return true iff the
1774 statement was modified. */
1776 bool
1777 ipa_param_body_adjustments::modify_assignment (gimple *stmt,
1778 gimple_seq *extra_stmts)
1780 tree *lhs_p, *rhs_p;
1781 bool any;
1783 if (!gimple_assign_single_p (stmt))
1784 return false;
1786 rhs_p = gimple_assign_rhs1_ptr (stmt);
1787 lhs_p = gimple_assign_lhs_ptr (stmt);
1789 any = modify_expression (lhs_p, false);
1790 any |= modify_expression (rhs_p, false);
1791 if (any
1792 && !useless_type_conversion_p (TREE_TYPE (*lhs_p), TREE_TYPE (*rhs_p)))
1794 if (TREE_CODE (*rhs_p) == CONSTRUCTOR)
1796 /* V_C_Es of constructors can cause trouble (PR 42714). */
1797 if (is_gimple_reg_type (TREE_TYPE (*lhs_p)))
1798 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
1799 else
1800 *rhs_p = build_constructor (TREE_TYPE (*lhs_p),
1801 NULL);
1803 else
1805 tree new_rhs = fold_build1_loc (gimple_location (stmt),
1806 VIEW_CONVERT_EXPR, TREE_TYPE (*lhs_p),
1807 *rhs_p);
1808 tree tmp = force_gimple_operand (new_rhs, extra_stmts, true,
1809 NULL_TREE);
1810 gimple_assign_set_rhs1 (stmt, tmp);
1812 return true;
1815 return any;
1818 /* Record information about what modifications to call arguments have already
1819 been done by clone materialization into a summary describing CS. The
1820 information is stored in NEW_INDEX_MAP, NEW_PT_MAP and NEW_ALWAYS_COPY_DELTA
1821 and correspond to equivalent fields in ipa_edge_modification_info. Return
1822 the edge summary. */
1824 static ipa_edge_modification_info *
1825 record_argument_state_1 (cgraph_edge *cs, const vec<int> &new_index_map,
1826 const vec<pass_through_split_map> &new_pt_map,
1827 int new_always_copy_delta)
1830 ipa_edge_modification_info *sum = ipa_edge_modifications->get_create (cs);
1832 unsigned len = sum->pass_through_map.length ();
1833 for (unsigned i = 0; i < len; i++)
1835 unsigned oldnew = sum->pass_through_map[i].new_index;
1836 sum->pass_through_map[i].new_index = new_index_map[oldnew];
1839 len = sum->index_map.length ();
1840 if (len > 0)
1842 unsigned nptlen = new_pt_map.length ();
1843 for (unsigned j = 0; j < nptlen; j++)
1845 int inverse = -1;
1846 for (unsigned i = 0; i < len ; i++)
1847 if ((unsigned) sum->index_map[i] == new_pt_map[j].base_index)
1849 inverse = i;
1850 break;
1852 gcc_assert (inverse >= 0);
1853 pass_through_split_map ptm_item;
1855 ptm_item.base_index = inverse;
1856 ptm_item.unit_offset = new_pt_map[j].unit_offset;
1857 ptm_item.new_index = new_pt_map[j].new_index;
1858 sum->pass_through_map.safe_push (ptm_item);
1861 for (unsigned i = 0; i < len; i++)
1863 int idx = sum->index_map[i];
1864 if (idx < 0)
1865 continue;
1866 sum->index_map[i] = new_index_map[idx];
1869 else
1871 sum->pass_through_map.safe_splice (new_pt_map);
1872 sum->index_map.safe_splice (new_index_map);
1874 sum->always_copy_delta += new_always_copy_delta;
1875 return sum;
1878 /* Record information about what modifications to call arguments have already
1879 been done by clone materialization into a summary of an edge describing the
1880 call in this clone and all its clones. NEW_INDEX_MAP, NEW_PT_MAP and
1881 NEW_ALWAYS_COPY_DELTA have the same meaning as record_argument_state_1.
1883 In order to associate the info with the right edge summaries, we need
1884 address of the ORIG_STMT in the function from which we are cloning (because
1885 the edges have not yet been re-assigned to the new statement that has just
1886 been created) and ID, the structure governing function body copying. */
1888 static void
1889 record_argument_state (copy_body_data *id, gimple *orig_stmt,
1890 const vec<int> &new_index_map,
1891 const vec<pass_through_split_map> &new_pt_map,
1892 int new_always_copy_delta)
1894 if (!ipa_edge_modifications)
1895 ipa_edge_modifications = new ipa_edge_modification_sum (symtab);
1897 struct cgraph_node *this_node = id->dst_node;
1898 ipa_edge_modification_info *first_sum = NULL;
1899 cgraph_edge *cs = this_node->get_edge (orig_stmt);
1900 if (cs)
1901 first_sum = record_argument_state_1 (cs, new_index_map, new_pt_map,
1902 new_always_copy_delta);
1903 else
1904 gcc_assert (this_node->clones);
1906 if (!this_node->clones)
1907 return;
1908 for (cgraph_node *subclone = this_node->clones; subclone != this_node;)
1910 cs = subclone->get_edge (orig_stmt);
1911 if (cs)
1913 if (!first_sum)
1914 first_sum = record_argument_state_1 (cs, new_index_map, new_pt_map,
1915 new_always_copy_delta);
1916 else
1918 ipa_edge_modification_info *s2
1919 = ipa_edge_modifications->get_create (cs);
1920 s2->index_map.truncate (0);
1921 s2->index_map.safe_splice (first_sum->index_map);
1922 s2->pass_through_map.truncate (0);
1923 s2->pass_through_map.safe_splice (first_sum->pass_through_map);
1924 s2->always_copy_delta = first_sum->always_copy_delta;
1927 else
1928 gcc_assert (subclone->clones);
1930 if (subclone->clones)
1931 subclone = subclone->clones;
1932 else if (subclone->next_sibling_clone)
1933 subclone = subclone->next_sibling_clone;
1934 else
1936 while (subclone != this_node && !subclone->next_sibling_clone)
1937 subclone = subclone->clone_of;
1938 if (subclone != this_node)
1939 subclone = subclone->next_sibling_clone;
1944 /* If the call statement pointed at by STMT_P contains any expressions that
1945 need to replaced with a different one as noted by ADJUSTMENTS, do so. f the
1946 statement needs to be rebuilt, do so. Return true if any modifications have
1947 been performed. ORIG_STMT, if not NULL, is the original statement in the
1948 function that is being cloned from, which at this point can be used to look
1949 up call_graph edges.
1951 If the method is invoked as a part of IPA clone materialization and if any
1952 parameter split is pass-through, i.e. it applies to the functin that is
1953 being modified and also to the callee of the statement, replace the
1954 parameter passed to old callee with all of the replacement a callee might
1955 possibly want and record the performed argument modifications in
1956 ipa_edge_modifications. Likewise if any argument has already been left out
1957 because it is not necessary. */
1959 bool
1960 ipa_param_body_adjustments::modify_call_stmt (gcall **stmt_p,
1961 gimple *orig_stmt)
1963 auto_vec <unsigned, 4> pass_through_args;
1964 auto_vec <unsigned, 4> pass_through_pbr_indices;
1965 auto_vec <HOST_WIDE_INT, 4> pass_through_offsets;
1966 gcall *stmt = *stmt_p;
1967 unsigned nargs = gimple_call_num_args (stmt);
1968 bool recreate = false;
1970 for (unsigned i = 0; i < gimple_call_num_args (stmt); i++)
1972 tree t = gimple_call_arg (stmt, i);
1973 gcc_assert (TREE_CODE (t) != BIT_FIELD_REF
1974 && TREE_CODE (t) != IMAGPART_EXPR
1975 && TREE_CODE (t) != REALPART_EXPR);
1977 if (TREE_CODE (t) == SSA_NAME
1978 && m_dead_ssas.contains (t))
1979 recreate = true;
1981 if (!m_split_modifications_p)
1982 continue;
1984 tree base;
1985 unsigned agg_arg_offset;
1986 if (!isra_get_ref_base_and_offset (t, &base, &agg_arg_offset))
1987 continue;
1989 bool by_ref = false;
1990 if (TREE_CODE (base) == SSA_NAME)
1992 if (!SSA_NAME_IS_DEFAULT_DEF (base))
1993 continue;
1994 base = SSA_NAME_VAR (base);
1995 gcc_checking_assert (base);
1996 by_ref = true;
1998 if (TREE_CODE (base) != PARM_DECL)
1999 continue;
2001 bool base_among_replacements = false;
2002 unsigned j, repl_list_len = m_replacements.length ();
2003 for (j = 0; j < repl_list_len; j++)
2005 ipa_param_body_replacement *pbr = &m_replacements[j];
2006 if (pbr->base == base)
2008 base_among_replacements = true;
2009 break;
2012 if (!base_among_replacements)
2013 continue;
2015 /* We still have to distinguish between an end-use that we have to
2016 transform now and a pass-through, which happens in the following
2017 two cases. */
2019 /* TODO: After we adjust ptr_parm_has_nonarg_uses to also consider
2020 &MEM_REF[ssa_name + offset], we will also have to detect that case
2021 here. */
2023 if (TREE_CODE (t) == SSA_NAME
2024 && SSA_NAME_IS_DEFAULT_DEF (t)
2025 && SSA_NAME_VAR (t)
2026 && TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL)
2028 /* This must be a by_reference pass-through. */
2029 recreate = true;
2030 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2031 pass_through_args.safe_push (i);
2032 pass_through_pbr_indices.safe_push (j);
2033 pass_through_offsets.safe_push (agg_arg_offset);
2035 else if (!by_ref && AGGREGATE_TYPE_P (TREE_TYPE (t)))
2037 /* Currently IPA-SRA guarantees the aggregate access type
2038 exactly matches in this case. So if it does not match, it is
2039 a pass-through argument that will be sorted out at edge
2040 redirection time. */
2041 ipa_param_body_replacement *pbr
2042 = lookup_replacement_1 (base, agg_arg_offset);
2044 if (!pbr
2045 || (TYPE_MAIN_VARIANT (TREE_TYPE (t))
2046 != TYPE_MAIN_VARIANT (TREE_TYPE (pbr->repl))))
2048 recreate = true;
2049 pass_through_args.safe_push (i);
2050 pass_through_pbr_indices.safe_push (j);
2051 pass_through_offsets.safe_push (agg_arg_offset);
2056 if (!recreate)
2058 /* No need to rebuild the statement, let's just modify arguments
2059 and the LHS if/as appropriate. */
2060 bool modified = false;
2061 for (unsigned i = 0; i < nargs; i++)
2063 tree *t = gimple_call_arg_ptr (stmt, i);
2064 modified |= modify_expression (t, true);
2066 if (gimple_call_lhs (stmt))
2068 tree *t = gimple_call_lhs_ptr (stmt);
2069 modified |= modify_expression (t, false);
2071 return modified;
2074 auto_vec<int, 16> index_map;
2075 auto_vec<pass_through_split_map, 4> pass_through_map;
2076 auto_vec<tree, 16> vargs;
2077 int always_copy_delta = 0;
2078 unsigned pt_idx = 0;
2079 int new_arg_idx = 0;
2080 for (unsigned i = 0; i < nargs; i++)
2082 if (pt_idx < pass_through_args.length ()
2083 && i == pass_through_args[pt_idx])
2085 unsigned j = pass_through_pbr_indices[pt_idx];
2086 unsigned agg_arg_offset = pass_through_offsets[pt_idx];
2087 pt_idx++;
2088 always_copy_delta--;
2089 tree base = m_replacements[j].base;
2091 /* In order to be put into SSA form, we have to push all replacements
2092 pertaining to this parameter as parameters to the call statement.
2093 Edge redirection will need to use edge summary to weed out the
2094 unnecessary ones. */
2095 unsigned repl_list_len = m_replacements.length ();
2096 for (; j < repl_list_len; j++)
2098 if (m_replacements[j].base != base)
2099 break;
2100 if (m_replacements[j].unit_offset < agg_arg_offset)
2101 continue;
2102 pass_through_split_map pt_map;
2103 pt_map.base_index = i;
2104 pt_map.unit_offset
2105 = m_replacements[j].unit_offset - agg_arg_offset;
2106 pt_map.new_index = new_arg_idx;
2107 pass_through_map.safe_push (pt_map);
2108 vargs.safe_push (m_replacements[j].repl);
2109 new_arg_idx++;
2110 always_copy_delta++;
2112 index_map.safe_push (-1);
2114 else
2116 tree t = gimple_call_arg (stmt, i);
2117 if (TREE_CODE (t) == SSA_NAME
2118 && m_dead_ssas.contains (t))
2120 always_copy_delta--;
2121 index_map.safe_push (-1);
2123 else
2125 modify_expression (&t, true);
2126 vargs.safe_push (t);
2127 index_map.safe_push (new_arg_idx);
2128 new_arg_idx++;
2133 gcall *new_stmt = gimple_build_call_vec (gimple_call_fn (stmt), vargs);
2134 if (gimple_has_location (stmt))
2135 gimple_set_location (new_stmt, gimple_location (stmt));
2136 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
2137 gimple_call_copy_flags (new_stmt, stmt);
2138 if (tree lhs = gimple_call_lhs (stmt))
2140 modify_expression (&lhs, false);
2141 /* Avoid adjusting SSA_NAME_DEF_STMT of a SSA lhs, SSA names
2142 have not yet been remapped. */
2143 *gimple_call_lhs_ptr (new_stmt) = lhs;
2145 *stmt_p = new_stmt;
2147 if (orig_stmt)
2148 record_argument_state (m_id, orig_stmt, index_map, pass_through_map,
2149 always_copy_delta);
2150 return true;
2153 /* If the statement STMT contains any expressions that need to replaced with a
2154 different one as noted by ADJUSTMENTS, do so. Handle any potential type
2155 incompatibilities. If any conversion sttements have to be pre-pended to
2156 STMT, they will be added to EXTRA_STMTS. Return true iff the statement was
2157 modified. */
2159 bool
2160 ipa_param_body_adjustments::modify_gimple_stmt (gimple **stmt,
2161 gimple_seq *extra_stmts,
2162 gimple *orig_stmt)
2164 bool modified = false;
2165 tree *t;
2167 switch (gimple_code (*stmt))
2169 case GIMPLE_RETURN:
2170 t = gimple_return_retval_ptr (as_a <greturn *> (*stmt));
2171 if (m_adjustments && m_adjustments->m_skip_return)
2172 *t = NULL_TREE;
2173 else if (*t != NULL_TREE)
2174 modified |= modify_expression (t, true);
2175 break;
2177 case GIMPLE_ASSIGN:
2178 modified |= modify_assignment (*stmt, extra_stmts);
2179 break;
2181 case GIMPLE_CALL:
2182 modified |= modify_call_stmt ((gcall **) stmt, orig_stmt);
2183 break;
2185 case GIMPLE_ASM:
2187 gasm *asm_stmt = as_a <gasm *> (*stmt);
2188 for (unsigned i = 0; i < gimple_asm_ninputs (asm_stmt); i++)
2190 t = &TREE_VALUE (gimple_asm_input_op (asm_stmt, i));
2191 modified |= modify_expression (t, true);
2193 for (unsigned i = 0; i < gimple_asm_noutputs (asm_stmt); i++)
2195 t = &TREE_VALUE (gimple_asm_output_op (asm_stmt, i));
2196 modified |= modify_expression (t, false);
2199 break;
2201 default:
2202 break;
2204 return modified;
2208 /* Traverse body of the current function and perform the requested adjustments
2209 on its statements. Return true iff the CFG has been changed. */
2211 bool
2212 ipa_param_body_adjustments::modify_cfun_body ()
2214 bool cfg_changed = false;
2215 basic_block bb;
2217 FOR_EACH_BB_FN (bb, cfun)
2219 gimple_stmt_iterator gsi;
2221 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2223 gphi *phi = as_a <gphi *> (gsi_stmt (gsi));
2224 tree new_lhs, old_lhs = gimple_phi_result (phi);
2225 new_lhs = replace_removed_params_ssa_names (old_lhs, phi);
2226 if (new_lhs)
2228 gimple_phi_set_result (phi, new_lhs);
2229 release_ssa_name (old_lhs);
2233 gsi = gsi_start_bb (bb);
2234 while (!gsi_end_p (gsi))
2236 gimple *stmt = gsi_stmt (gsi);
2237 gimple *stmt_copy = stmt;
2238 gimple_seq extra_stmts = NULL;
2239 bool modified = modify_gimple_stmt (&stmt, &extra_stmts, NULL);
2240 if (stmt != stmt_copy)
2242 gcc_checking_assert (modified);
2243 gsi_replace (&gsi, stmt, false);
2245 if (!gimple_seq_empty_p (extra_stmts))
2246 gsi_insert_seq_before (&gsi, extra_stmts, GSI_SAME_STMT);
2248 def_operand_p defp;
2249 ssa_op_iter iter;
2250 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF)
2252 tree old_def = DEF_FROM_PTR (defp);
2253 if (tree new_def = replace_removed_params_ssa_names (old_def,
2254 stmt))
2256 SET_DEF (defp, new_def);
2257 release_ssa_name (old_def);
2258 modified = true;
2262 if (modified)
2264 update_stmt (stmt);
2265 if (maybe_clean_eh_stmt (stmt)
2266 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
2267 cfg_changed = true;
2269 gsi_next (&gsi);
2273 return cfg_changed;
2276 /* Call gimple_debug_bind_reset_value on all debug statements describing
2277 gimple register parameters that are being removed or replaced. */
2279 void
2280 ipa_param_body_adjustments::reset_debug_stmts ()
2282 int i, len;
2283 gimple_stmt_iterator *gsip = NULL, gsi;
2285 if (MAY_HAVE_DEBUG_STMTS && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
2287 gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
2288 gsip = &gsi;
2290 len = m_reset_debug_decls.length ();
2291 for (i = 0; i < len; i++)
2293 imm_use_iterator ui;
2294 gimple *stmt;
2295 gdebug *def_temp;
2296 tree name, vexpr, copy = NULL_TREE;
2297 use_operand_p use_p;
2298 tree decl = m_reset_debug_decls[i];
2300 gcc_checking_assert (is_gimple_reg (decl));
2301 name = ssa_default_def (cfun, decl);
2302 vexpr = NULL;
2303 if (name)
2304 FOR_EACH_IMM_USE_STMT (stmt, ui, name)
2306 if (gimple_clobber_p (stmt))
2308 gimple_stmt_iterator cgsi = gsi_for_stmt (stmt);
2309 unlink_stmt_vdef (stmt);
2310 gsi_remove (&cgsi, true);
2311 release_defs (stmt);
2312 continue;
2314 /* All other users must have been removed by function body
2315 modification. */
2316 gcc_assert (is_gimple_debug (stmt));
2317 if (vexpr == NULL && gsip != NULL)
2319 vexpr = build_debug_expr_decl (TREE_TYPE (name));
2320 /* FIXME: Is setting the mode really necessary? */
2321 SET_DECL_MODE (vexpr, DECL_MODE (decl));
2322 def_temp = gimple_build_debug_source_bind (vexpr, decl, NULL);
2323 gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
2325 if (vexpr)
2327 FOR_EACH_IMM_USE_ON_STMT (use_p, ui)
2328 SET_USE (use_p, vexpr);
2330 else
2331 gimple_debug_bind_reset_value (stmt);
2332 update_stmt (stmt);
2334 /* Create a VAR_DECL for debug info purposes. */
2335 if (!DECL_IGNORED_P (decl))
2337 copy = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
2338 VAR_DECL, DECL_NAME (decl),
2339 TREE_TYPE (decl));
2340 if (DECL_PT_UID_SET_P (decl))
2341 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
2342 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
2343 TREE_READONLY (copy) = TREE_READONLY (decl);
2344 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
2345 DECL_NOT_GIMPLE_REG_P (copy) = DECL_NOT_GIMPLE_REG_P (decl);
2346 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
2347 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
2348 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
2349 DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
2350 SET_DECL_RTL (copy, 0);
2351 TREE_USED (copy) = 1;
2352 DECL_CONTEXT (copy) = current_function_decl;
2353 add_local_decl (cfun, copy);
2354 DECL_CHAIN (copy)
2355 = BLOCK_VARS (DECL_INITIAL (current_function_decl));
2356 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = copy;
2358 if (gsip != NULL && copy && target_for_debug_bind (decl))
2360 gcc_assert (TREE_CODE (decl) == PARM_DECL);
2361 if (vexpr)
2362 def_temp = gimple_build_debug_bind (copy, vexpr, NULL);
2363 else
2364 def_temp = gimple_build_debug_source_bind (copy, decl,
2365 NULL);
2366 gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
2371 /* Perform all necessary body changes to change signature, body and debug info
2372 of fun according to adjustments passed at construction. Return true if CFG
2373 was changed in any way. The main entry point for modification of standalone
2374 functions that is not part of IPA clone materialization. */
2376 bool
2377 ipa_param_body_adjustments::perform_cfun_body_modifications ()
2379 bool cfg_changed;
2380 modify_formal_parameters ();
2381 cfg_changed = modify_cfun_body ();
2382 reset_debug_stmts ();
2384 return cfg_changed;
2388 /* Deallocate summaries which otherwise stay alive until the end of
2389 compilation. */
2391 void
2392 ipa_edge_modifications_finalize ()
2394 if (!ipa_edge_modifications)
2395 return;
2396 delete ipa_edge_modifications;
2397 ipa_edge_modifications = NULL;