gccrs: dump: Fix module dumping
[official-gcc.git] / gcc / ipa-param-manipulation.cc
blob42488ee09c3719f66a657c5b1c277dc901c96680
1 /* Manipulation of formal and actual parameters of functions and function
2 calls.
3 Copyright (C) 2017-2023 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 #define INCLUDE_ALGORITHM
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "ssa.h"
29 #include "cgraph.h"
30 #include "fold-const.h"
31 #include "tree-eh.h"
32 #include "stor-layout.h"
33 #include "gimplify.h"
34 #include "gimple-iterator.h"
35 #include "gimplify-me.h"
36 #include "tree-cfg.h"
37 #include "tree-dfa.h"
38 #include "ipa-param-manipulation.h"
39 #include "print-tree.h"
40 #include "gimple-pretty-print.h"
41 #include "builtins.h"
42 #include "tree-ssa.h"
43 #include "tree-inline.h"
44 #include "alloc-pool.h"
45 #include "symbol-summary.h"
46 #include "symtab-clones.h"
47 #include "tree-phinodes.h"
48 #include "cfgexpand.h"
49 #include "attribs.h"
50 #include "ipa-prop.h"
52 /* Actual prefixes of different newly synthetized parameters. Keep in sync
53 with IPA_PARAM_PREFIX_* defines. */
55 static const char *ipa_param_prefixes[IPA_PARAM_PREFIX_COUNT]
56 = {"SYNTH",
57 "ISRA",
58 "simd",
59 "mask"};
61 /* Names of parameters for dumping. Keep in sync with enum ipa_parm_op. */
63 static const char *ipa_param_op_names[IPA_PARAM_PREFIX_COUNT]
64 = {"IPA_PARAM_OP_UNDEFINED",
65 "IPA_PARAM_OP_COPY",
66 "IPA_PARAM_OP_NEW",
67 "IPA_PARAM_OP_SPLIT"};
69 /* Structure to hold declarations representing pass-through IPA-SRA splits. In
70 essence, it tells new index for a combination of original index and
71 offset. */
73 struct pass_through_split_map
75 /* Original argument index. */
76 unsigned base_index;
77 /* Offset of the split part in the original argument. */
78 unsigned unit_offset;
79 /* Index of the split part in the call statement - where clone
80 materialization put it. */
81 int new_index;
84 /* Information about some call statements that needs to be conveyed from clone
85 materialization to edge redirection. */
87 class ipa_edge_modification_info
89 public:
90 ipa_edge_modification_info ()
93 /* Mapping of original argument indices to where those arguments sit in the
94 call statement now or to a negative index if they were removed. */
95 auto_vec<int> index_map;
96 /* Information about ISRA replacements put into the call statement at the
97 clone materialization stages. */
98 auto_vec<pass_through_split_map> pass_through_map;
99 /* Necessary adjustment to ipa_param_adjustments::m_always_copy_start when
100 redirecting the call. */
101 int always_copy_delta = 0;
104 /* Class for storing and retrieving summaries about cal statement
105 modifications. */
107 class ipa_edge_modification_sum
108 : public call_summary <ipa_edge_modification_info *>
110 public:
111 ipa_edge_modification_sum (symbol_table *table)
112 : call_summary<ipa_edge_modification_info *> (table)
116 /* Hook that is called by summary when an edge is duplicated. */
118 void duplicate (cgraph_edge *,
119 cgraph_edge *,
120 ipa_edge_modification_info *old_info,
121 ipa_edge_modification_info *new_info) final override
123 new_info->index_map.safe_splice (old_info->index_map);
124 new_info->pass_through_map.safe_splice (old_info->pass_through_map);
125 new_info->always_copy_delta = old_info->always_copy_delta;
129 /* Call summary to store information about edges which have had their arguments
130 partially modified already. */
132 static ipa_edge_modification_sum *ipa_edge_modifications;
134 /* Fail compilation if CS has any summary associated with it in
135 ipa_edge_modifications. */
137 DEBUG_FUNCTION void
138 ipa_verify_edge_has_no_modifications (cgraph_edge *cs)
140 gcc_assert (!ipa_edge_modifications || !ipa_edge_modifications->get (cs));
143 /* Fill an empty vector ARGS with PARM_DECLs representing formal parameters of
144 FNDECL. The function should not be called during LTO WPA phase except for
145 thunks (or functions with bodies streamed in). */
147 void
148 push_function_arg_decls (vec<tree> *args, tree fndecl)
150 int count;
151 tree parm;
153 /* Safety check that we do not attempt to use the function in WPA, except
154 when the function is a thunk and then we have DECL_ARGUMENTS or when we
155 have already explicitely loaded its body. */
156 gcc_assert (!flag_wpa
157 || DECL_ARGUMENTS (fndecl)
158 || gimple_has_body_p (fndecl));
159 count = 0;
160 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
161 count++;
163 args->reserve_exact (count);
164 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
165 args->quick_push (parm);
168 /* Fill an empty vector TYPES with trees representing formal parameters of
169 function type FNTYPE. */
171 void
172 push_function_arg_types (vec<tree> *types, tree fntype)
174 int count = 0;
175 tree t;
177 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
178 count++;
180 types->reserve_exact (count);
181 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
182 types->quick_push (TREE_VALUE (t));
185 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
186 friendly way, assuming they are meant to be applied to FNDECL. */
188 void
189 ipa_dump_adjusted_parameters (FILE *f,
190 vec<ipa_adjusted_param, va_gc> *adj_params)
192 unsigned i, len = vec_safe_length (adj_params);
193 bool first = true;
195 if (!len)
196 return;
198 fprintf (f, " IPA adjusted parameters: ");
199 for (i = 0; i < len; i++)
201 struct ipa_adjusted_param *apm;
202 apm = &(*adj_params)[i];
204 if (!first)
205 fprintf (f, " ");
206 else
207 first = false;
209 fprintf (f, "%i. %s %s", i, ipa_param_op_names[apm->op],
210 apm->prev_clone_adjustment ? "prev_clone_adjustment " : "");
211 switch (apm->op)
213 case IPA_PARAM_OP_UNDEFINED:
214 break;
216 case IPA_PARAM_OP_COPY:
217 fprintf (f, ", base_index: %u", apm->base_index);
218 fprintf (f, ", prev_clone_index: %u", apm->prev_clone_index);
219 break;
221 case IPA_PARAM_OP_SPLIT:
222 fprintf (f, ", offset: %u", apm->unit_offset);
223 /* fall-through */
224 case IPA_PARAM_OP_NEW:
225 fprintf (f, ", base_index: %u", apm->base_index);
226 fprintf (f, ", prev_clone_index: %u", apm->prev_clone_index);
227 print_node_brief (f, ", type: ", apm->type, 0);
228 print_node_brief (f, ", alias type: ", apm->alias_ptr_type, 0);
229 fprintf (f, " prefix: %s",
230 ipa_param_prefixes[apm->param_prefix_index]);
231 if (apm->reverse)
232 fprintf (f, ", reverse");
233 break;
235 fprintf (f, "\n");
239 /* Fill NEW_TYPES with types of a function after its current OTYPES have been
240 modified as described in ADJ_PARAMS. When USE_PREV_INDICES is true, use
241 prev_clone_index from ADJ_PARAMS as opposed to base_index when the parameter
242 is false. */
244 static void
245 fill_vector_of_new_param_types (vec<tree> *new_types, vec<tree> *otypes,
246 vec<ipa_adjusted_param, va_gc> *adj_params,
247 bool use_prev_indices)
249 unsigned adj_len = vec_safe_length (adj_params);
250 new_types->reserve_exact (adj_len);
251 for (unsigned i = 0; i < adj_len ; i++)
253 ipa_adjusted_param *apm = &(*adj_params)[i];
254 if (apm->op == IPA_PARAM_OP_COPY)
256 unsigned index
257 = use_prev_indices ? apm->prev_clone_index : apm->base_index;
258 /* The following needs to be handled gracefully because of type
259 mismatches. This happens with LTO but apparently also in Fortran
260 with -fcoarray=lib -O2 -lcaf_single -latomic. */
261 if (index >= otypes->length ())
262 continue;
263 new_types->quick_push ((*otypes)[index]);
265 else if (apm->op == IPA_PARAM_OP_NEW
266 || apm->op == IPA_PARAM_OP_SPLIT)
268 tree ntype = apm->type;
269 if (is_gimple_reg_type (ntype)
270 && TYPE_MODE (ntype) != BLKmode)
272 unsigned malign = GET_MODE_ALIGNMENT (TYPE_MODE (ntype));
273 if (TYPE_ALIGN (ntype) != malign)
274 ntype = build_aligned_type (ntype, malign);
276 new_types->quick_push (ntype);
278 else
279 gcc_unreachable ();
283 /* Return false if given attribute should prevent type adjustments. */
285 bool
286 ipa_param_adjustments::type_attribute_allowed_p (tree name)
288 if ((is_attribute_p ("fn spec", name) && flag_ipa_modref)
289 || is_attribute_p ("access", name)
290 || is_attribute_p ("returns_nonnull", name)
291 || is_attribute_p ("assume_aligned", name)
292 || is_attribute_p ("nocf_check", name)
293 || is_attribute_p ("warn_unused_result", name))
294 return true;
295 return false;
298 /* Return true if attribute should be dropped if parameter changed. */
300 static bool
301 drop_type_attribute_if_params_changed_p (tree name)
303 if (is_attribute_p ("fn spec", name)
304 || is_attribute_p ("access", name))
305 return true;
306 return false;
309 /* Build and return a function type just like ORIG_TYPE but with parameter
310 types given in NEW_PARAM_TYPES - which can be NULL if, but only if,
311 ORIG_TYPE itself has NULL TREE_ARG_TYPEs. If METHOD2FUNC is true, also make
312 it a FUNCTION_TYPE instead of FUNCTION_TYPE.
313 If ARG_MODIFIED is true drop attributes that are no longer up to date. */
315 static tree
316 build_adjusted_function_type (tree orig_type, vec<tree> *new_param_types,
317 bool method2func, bool skip_return,
318 bool args_modified)
320 tree new_arg_types = NULL;
321 if (TYPE_ARG_TYPES (orig_type))
323 gcc_checking_assert (new_param_types);
324 bool last_parm_void = (TREE_VALUE (tree_last (TYPE_ARG_TYPES (orig_type)))
325 == void_type_node);
326 unsigned len = new_param_types->length ();
327 for (unsigned i = 0; i < len; i++)
328 new_arg_types = tree_cons (NULL_TREE, (*new_param_types)[i],
329 new_arg_types);
331 tree new_reversed = nreverse (new_arg_types);
332 if (last_parm_void)
334 if (new_reversed)
335 TREE_CHAIN (new_arg_types) = void_list_node;
336 else
337 new_reversed = void_list_node;
339 new_arg_types = new_reversed;
342 /* Use build_distinct_type_copy to preserve as much as possible from original
343 type (debug info, attribute lists etc.). The one exception is
344 METHOD_TYPEs which must have THIS argument and when we are asked to remove
345 it, we need to build new FUNCTION_TYPE instead. */
346 tree new_type = NULL;
347 if (method2func)
349 tree ret_type;
350 if (skip_return)
351 ret_type = void_type_node;
352 else
353 ret_type = TREE_TYPE (orig_type);
355 new_type
356 = build_distinct_type_copy (build_function_type (ret_type,
357 new_arg_types));
358 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
360 else
362 new_type = build_distinct_type_copy (orig_type);
363 TYPE_ARG_TYPES (new_type) = new_arg_types;
364 if (skip_return)
365 TREE_TYPE (new_type) = void_type_node;
367 if (args_modified && TYPE_ATTRIBUTES (new_type))
369 tree t = TYPE_ATTRIBUTES (new_type);
370 tree *last = &TYPE_ATTRIBUTES (new_type);
371 TYPE_ATTRIBUTES (new_type) = NULL;
372 for (;t; t = TREE_CHAIN (t))
373 if (!drop_type_attribute_if_params_changed_p
374 (get_attribute_name (t)))
376 *last = copy_node (t);
377 TREE_CHAIN (*last) = NULL;
378 last = &TREE_CHAIN (*last);
382 return new_type;
385 /* Return the maximum index in any IPA_PARAM_OP_COPY adjustment or -1 if there
386 is none. */
389 ipa_param_adjustments::get_max_base_index ()
391 unsigned adj_len = vec_safe_length (m_adj_params);
392 int max_index = -1;
393 for (unsigned i = 0; i < adj_len ; i++)
395 ipa_adjusted_param *apm = &(*m_adj_params)[i];
396 if (apm->op == IPA_PARAM_OP_COPY
397 && max_index < apm->base_index)
398 max_index = apm->base_index;
400 return max_index;
404 /* Fill SURVIVING_PARAMS with an array of bools where each one says whether a
405 parameter that originally was at that position still survives in the given
406 clone or is removed/replaced. If the final array is smaller than an index
407 of an original parameter, that parameter also did not survive. That a
408 parameter survives does not mean it has the same index as before. */
410 void
411 ipa_param_adjustments::get_surviving_params (vec<bool> *surviving_params)
413 unsigned adj_len = vec_safe_length (m_adj_params);
414 int max_index = get_max_base_index ();
416 if (max_index < 0)
417 return;
418 surviving_params->reserve_exact (max_index + 1);
419 surviving_params->quick_grow_cleared (max_index + 1);
420 for (unsigned i = 0; i < adj_len ; i++)
422 ipa_adjusted_param *apm = &(*m_adj_params)[i];
423 if (apm->op == IPA_PARAM_OP_COPY)
424 (*surviving_params)[apm->base_index] = true;
428 /* Fill NEW_INDICES with new indices of each surviving parameter or -1 for
429 those which do not survive. Any parameter outside of lenght of the vector
430 does not survive. There is currently no support for a parameter to be
431 copied to two distinct new parameters. */
433 void
434 ipa_param_adjustments::get_updated_indices (vec<int> *new_indices)
436 unsigned adj_len = vec_safe_length (m_adj_params);
437 int max_index = get_max_base_index ();
439 if (max_index < 0)
440 return;
441 unsigned res_len = max_index + 1;
442 new_indices->reserve_exact (res_len);
443 for (unsigned i = 0; i < res_len ; i++)
444 new_indices->quick_push (-1);
445 for (unsigned i = 0; i < adj_len ; i++)
447 ipa_adjusted_param *apm = &(*m_adj_params)[i];
448 if (apm->op == IPA_PARAM_OP_COPY)
449 (*new_indices)[apm->base_index] = i;
453 /* Return the original index for the given new parameter index. Return a
454 negative number if not available. */
457 ipa_param_adjustments::get_original_index (int newidx)
459 const ipa_adjusted_param *adj = &(*m_adj_params)[newidx];
460 if (adj->op != IPA_PARAM_OP_COPY)
461 return -1;
462 return adj->base_index;
465 /* Return true if the first parameter (assuming there was one) survives the
466 transformation intact and remains the first one. */
468 bool
469 ipa_param_adjustments::first_param_intact_p ()
471 return (!vec_safe_is_empty (m_adj_params)
472 && (*m_adj_params)[0].op == IPA_PARAM_OP_COPY
473 && (*m_adj_params)[0].base_index == 0);
476 /* Return true if we have to change what has formerly been a method into a
477 function. */
479 bool
480 ipa_param_adjustments::method2func_p (tree orig_type)
482 return ((TREE_CODE (orig_type) == METHOD_TYPE) && !first_param_intact_p ());
485 /* Given function type OLD_TYPE, return a new type derived from it after
486 performing all atored modifications. TYPE_ORIGINAL_P should be true when
487 OLD_TYPE refers to the type before any IPA transformations, as opposed to a
488 type that can be an intermediate one in between various IPA
489 transformations. */
491 tree
492 ipa_param_adjustments::build_new_function_type (tree old_type,
493 bool type_original_p)
495 auto_vec<tree,16> new_param_types, *new_param_types_p;
496 if (prototype_p (old_type))
498 auto_vec<tree, 16> otypes;
499 push_function_arg_types (&otypes, old_type);
500 fill_vector_of_new_param_types (&new_param_types, &otypes, m_adj_params,
501 !type_original_p);
502 new_param_types_p = &new_param_types;
504 else
505 new_param_types_p = NULL;
507 /* Check if any params type cares about are modified. In this case will
508 need to drop some type attributes. */
509 bool modified = false;
510 size_t index = 0;
511 if (m_adj_params)
512 for (tree t = TYPE_ARG_TYPES (old_type);
513 t && (int)index < m_always_copy_start && !modified;
514 t = TREE_CHAIN (t), index++)
515 if (index >= m_adj_params->length ()
516 || get_original_index (index) != (int)index)
517 modified = true;
520 return build_adjusted_function_type (old_type, new_param_types_p,
521 method2func_p (old_type), m_skip_return,
522 modified);
525 /* Build variant of function decl ORIG_DECL which has no return value if
526 M_SKIP_RETURN is true and, if ORIG_DECL's types or parameters is known, has
527 this type adjusted as indicated in M_ADJ_PARAMS. Arguments from
528 DECL_ARGUMENTS list are not processed now, since they are linked by
529 TREE_CHAIN directly and not accessible in LTO during WPA. The caller is
530 responsible for eliminating them when clones are properly materialized. */
532 tree
533 ipa_param_adjustments::adjust_decl (tree orig_decl)
535 tree new_decl = copy_node (orig_decl);
536 tree orig_type = TREE_TYPE (orig_decl);
537 if (prototype_p (orig_type)
538 || (m_skip_return && !VOID_TYPE_P (TREE_TYPE (orig_type))))
540 tree new_type = build_new_function_type (orig_type, false);
541 TREE_TYPE (new_decl) = new_type;
543 if (method2func_p (orig_type))
544 DECL_VINDEX (new_decl) = NULL_TREE;
546 /* When signature changes, we need to clear builtin info. */
547 if (fndecl_built_in_p (new_decl))
548 set_decl_built_in_function (new_decl, NOT_BUILT_IN, 0);
550 DECL_VIRTUAL_P (new_decl) = 0;
551 DECL_LANG_SPECIFIC (new_decl) = NULL;
553 /* Drop MALLOC attribute for a void function. */
554 if (m_skip_return)
555 DECL_IS_MALLOC (new_decl) = 0;
557 return new_decl;
560 /* Wrapper around get_base_ref_and_offset for cases interesting for IPA-SRA
561 transformations. Return true if EXPR has an interesting form and fill in
562 *BASE_P and *UNIT_OFFSET_P with the appropriate info. */
564 static bool
565 isra_get_ref_base_and_offset (tree expr, tree *base_p, unsigned *unit_offset_p)
567 HOST_WIDE_INT offset, size;
568 bool reverse;
569 tree base
570 = get_ref_base_and_extent_hwi (expr, &offset, &size, &reverse);
571 if (!base || size < 0)
572 return false;
574 if ((offset % BITS_PER_UNIT) != 0)
575 return false;
577 if (TREE_CODE (base) == MEM_REF)
579 poly_int64 plmoff = mem_ref_offset (base).force_shwi ();
580 HOST_WIDE_INT moff;
581 bool is_cst = plmoff.is_constant (&moff);
582 if (!is_cst)
583 return false;
584 offset += moff * BITS_PER_UNIT;
585 base = TREE_OPERAND (base, 0);
588 if (offset < 0 || (offset / BITS_PER_UNIT) > UINT_MAX)
589 return false;
591 *base_p = base;
592 *unit_offset_p = offset / BITS_PER_UNIT;
593 return true;
596 /* Modify actual arguments of a function call in statement currently belonging
597 to CS, and make it call CS->callee->decl. Return the new statement that
598 replaced the old one. When invoked, cfun and current_function_decl have to
599 be set to the caller. */
601 gcall *
602 ipa_param_adjustments::modify_call (cgraph_edge *cs,
603 bool update_references)
605 gcall *stmt = cs->call_stmt;
606 tree callee_decl = cs->callee->decl;
608 ipa_edge_modification_info *mod_info
609 = ipa_edge_modifications ? ipa_edge_modifications->get (cs) : NULL;
610 if (mod_info && symtab->dump_file)
612 fprintf (symtab->dump_file, "Information about pre-exiting "
613 "modifications.\n Index map:");
614 unsigned idx_len = mod_info->index_map.length ();
615 for (unsigned i = 0; i < idx_len; i++)
616 fprintf (symtab->dump_file, " %i", mod_info->index_map[i]);
617 fprintf (symtab->dump_file, "\n Pass-through split map: ");
618 unsigned ptm_len = mod_info->pass_through_map.length ();
619 for (unsigned i = 0; i < ptm_len; i++)
620 fprintf (symtab->dump_file,
621 " (base_index: %u, offset: %u, new_index: %i)",
622 mod_info->pass_through_map[i].base_index,
623 mod_info->pass_through_map[i].unit_offset,
624 mod_info->pass_through_map[i].new_index);
625 fprintf (symtab->dump_file, "\n Always-copy delta: %i\n",
626 mod_info->always_copy_delta);
629 unsigned len = vec_safe_length (m_adj_params);
630 auto_vec<tree, 16> vargs (len);
631 unsigned old_nargs = gimple_call_num_args (stmt);
632 unsigned orig_nargs = mod_info ? mod_info->index_map.length () : old_nargs;
633 auto_vec<bool, 16> kept (old_nargs);
634 kept.quick_grow_cleared (old_nargs);
636 cgraph_node *current_node = cgraph_node::get (current_function_decl);
637 if (update_references)
638 current_node->remove_stmt_references (stmt);
640 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
641 gimple_stmt_iterator prev_gsi = gsi;
642 gsi_prev (&prev_gsi);
643 for (unsigned i = 0; i < len; i++)
645 ipa_adjusted_param *apm = &(*m_adj_params)[i];
646 if (apm->op == IPA_PARAM_OP_COPY)
648 int index = apm->base_index;
649 if ((unsigned) index >= orig_nargs)
650 /* Can happen if the original call has argument mismatch,
651 ignore. */
652 continue;
653 if (mod_info)
655 index = mod_info->index_map[apm->base_index];
656 gcc_assert (index >= 0);
659 tree arg = gimple_call_arg (stmt, index);
661 vargs.quick_push (arg);
662 kept[index] = true;
663 continue;
666 /* At the moment the only user of IPA_PARAM_OP_NEW modifies calls itself.
667 If we ever want to support it during WPA IPA stage, we'll need a
668 mechanism to call into the IPA passes that introduced them. Currently
669 we simply mandate that IPA infrastructure understands all argument
670 modifications. Remember, edge redirection/modification is done only
671 once, not in steps for each pass modifying the callee like clone
672 materialization. */
673 gcc_assert (apm->op == IPA_PARAM_OP_SPLIT);
675 /* We have to handle pass-through changes differently using the map
676 clone materialziation might have left behind. */
677 tree repl = NULL_TREE;
678 unsigned ptm_len = mod_info ? mod_info->pass_through_map.length () : 0;
679 for (unsigned j = 0; j < ptm_len; j++)
680 if (mod_info->pass_through_map[j].base_index == apm->base_index
681 && mod_info->pass_through_map[j].unit_offset == apm->unit_offset)
683 int repl_idx = mod_info->pass_through_map[j].new_index;
684 gcc_assert (repl_idx >= 0);
685 repl = gimple_call_arg (stmt, repl_idx);
686 break;
688 if (repl)
690 vargs.quick_push (repl);
691 continue;
694 int index = apm->base_index;
695 if ((unsigned) index >= orig_nargs)
696 /* Can happen if the original call has argument mismatch, ignore. */
697 continue;
698 if (mod_info)
700 index = mod_info->index_map[apm->base_index];
701 gcc_assert (index >= 0);
703 tree base = gimple_call_arg (stmt, index);
705 /* We create a new parameter out of the value of the old one, we can
706 do the following kind of transformations:
708 - A scalar passed by reference, potentially as a part of a larger
709 aggregate, is converted to a scalar passed by value.
711 - A part of an aggregate is passed instead of the whole aggregate. */
713 location_t loc = gimple_location (stmt);
714 tree off;
715 bool deref_base = false;
716 unsigned int deref_align = 0;
717 if (TREE_CODE (base) != ADDR_EXPR
718 && is_gimple_reg_type (TREE_TYPE (base)))
720 /* Detect type mismatches in calls in invalid programs and make a
721 poor attempt to gracefully convert them so that we don't ICE. */
722 if (!POINTER_TYPE_P (TREE_TYPE (base)))
723 base = force_value_to_type (ptr_type_node, base);
725 off = build_int_cst (apm->alias_ptr_type, apm->unit_offset);
727 else
729 bool addrof;
730 if (TREE_CODE (base) == ADDR_EXPR)
732 base = TREE_OPERAND (base, 0);
733 addrof = true;
735 else
736 addrof = false;
738 tree prev_base = base;
739 poly_int64 base_offset;
740 base = get_addr_base_and_unit_offset (base, &base_offset);
742 /* Aggregate arguments can have non-invariant addresses. */
743 if (!base)
745 base = build_fold_addr_expr (prev_base);
746 off = build_int_cst (apm->alias_ptr_type, apm->unit_offset);
748 else if (TREE_CODE (base) == MEM_REF)
750 if (!addrof)
752 deref_base = true;
753 deref_align = TYPE_ALIGN (TREE_TYPE (base));
755 off = build_int_cst (apm->alias_ptr_type,
756 base_offset + apm->unit_offset);
757 off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
758 off);
759 base = TREE_OPERAND (base, 0);
761 else
763 off = build_int_cst (apm->alias_ptr_type,
764 base_offset + apm->unit_offset);
765 base = build_fold_addr_expr (base);
769 tree type = apm->type;
770 unsigned int align;
771 unsigned HOST_WIDE_INT misalign;
773 if (deref_base)
775 align = deref_align;
776 misalign = 0;
778 else
780 get_pointer_alignment_1 (base, &align, &misalign);
781 /* All users must make sure that we can be optimistic when it
782 comes to alignment in this case (by inspecting the final users
783 of these new parameters). */
784 if (TYPE_ALIGN (type) > align)
785 align = TYPE_ALIGN (type);
787 misalign
788 += (offset_int::from (wi::to_wide (off), SIGNED).to_short_addr ()
789 * BITS_PER_UNIT);
790 misalign = misalign & (align - 1);
791 if (misalign != 0)
792 align = least_bit_hwi (misalign);
793 if (align < TYPE_ALIGN (type))
794 type = build_aligned_type (type, align);
795 base = force_gimple_operand_gsi (&gsi, base,
796 true, NULL, true, GSI_SAME_STMT);
797 tree expr = fold_build2_loc (loc, MEM_REF, type, base, off);
798 REF_REVERSE_STORAGE_ORDER (expr) = apm->reverse;
799 /* If expr is not a valid gimple call argument emit
800 a load into a temporary. */
801 if (is_gimple_reg_type (TREE_TYPE (expr)))
803 gimple *tem = gimple_build_assign (NULL_TREE, expr);
804 if (gimple_in_ssa_p (cfun))
806 gimple_set_vuse (tem, gimple_vuse (stmt));
807 expr = make_ssa_name (TREE_TYPE (expr), tem);
809 else
810 expr = create_tmp_reg (TREE_TYPE (expr));
811 gimple_assign_set_lhs (tem, expr);
812 gsi_insert_before (&gsi, tem, GSI_SAME_STMT);
814 vargs.quick_push (expr);
817 if (m_always_copy_start >= 0)
819 int always_copy_start = m_always_copy_start;
820 if (mod_info)
822 always_copy_start += mod_info->always_copy_delta;
823 gcc_assert (always_copy_start >= 0);
825 for (unsigned i = always_copy_start; i < old_nargs; i++)
826 vargs.safe_push (gimple_call_arg (stmt, i));
829 /* For optimized away parameters, add on the caller side
830 before the call
831 DEBUG D#X => parm_Y(D)
832 stmts and associate D#X with parm in decl_debug_args_lookup
833 vector to say for debug info that if parameter parm had been passed,
834 it would have value parm_Y(D). */
835 tree old_decl = gimple_call_fndecl (stmt);
836 if (MAY_HAVE_DEBUG_BIND_STMTS && old_decl && callee_decl)
838 vec<tree, va_gc> **debug_args = NULL;
839 unsigned i = 0;
840 cgraph_node *callee_node = cgraph_node::get (callee_decl);
842 /* FIXME: we don't seem to be able to insert debug args before clone
843 is materialized. Materializing them early leads to extra memory
844 use. */
845 if (callee_node->clone_of)
846 callee_node->get_untransformed_body ();
847 for (tree old_parm = DECL_ARGUMENTS (old_decl);
848 old_parm && i < old_nargs && ((int) i) < m_always_copy_start;
849 old_parm = DECL_CHAIN (old_parm), i++)
851 if (!is_gimple_reg (old_parm) || kept[i])
852 continue;
853 tree arg;
854 if (mod_info)
856 if (mod_info->index_map[i] < 0)
857 continue;
858 arg = gimple_call_arg (stmt, mod_info->index_map[i]);
860 else
861 arg = gimple_call_arg (stmt, i);
863 tree origin = DECL_ORIGIN (old_parm);
864 if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
866 if (!fold_convertible_p (TREE_TYPE (origin), arg))
867 continue;
868 tree rhs1;
869 if (TREE_CODE (arg) == SSA_NAME
870 && gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg))
871 && (rhs1
872 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (arg)))
873 && useless_type_conversion_p (TREE_TYPE (origin),
874 TREE_TYPE (rhs1)))
875 arg = rhs1;
876 else
877 arg = fold_convert_loc (gimple_location (stmt),
878 TREE_TYPE (origin), arg);
880 if (debug_args == NULL)
881 debug_args = decl_debug_args_insert (callee_decl);
882 unsigned int ix;
883 tree ddecl = NULL_TREE;
884 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl); ix += 2)
885 if (ddecl == origin)
887 ddecl = (**debug_args)[ix + 1];
888 break;
890 if (ddecl == NULL)
892 ddecl = build_debug_expr_decl (TREE_TYPE (origin));
893 /* FIXME: Is setting the mode really necessary? */
894 SET_DECL_MODE (ddecl, DECL_MODE (origin));
896 vec_safe_push (*debug_args, origin);
897 vec_safe_push (*debug_args, ddecl);
899 gimple *def_temp = gimple_build_debug_bind (ddecl,
900 unshare_expr (arg), stmt);
901 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
905 if (dump_file && (dump_flags & TDF_DETAILS))
907 fprintf (dump_file, "replacing stmt:");
908 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0);
911 gcall *new_stmt = gimple_build_call_vec (callee_decl, vargs);
913 tree ssa_to_remove = NULL;
914 if (tree lhs = gimple_call_lhs (stmt))
916 if (!m_skip_return)
917 gimple_call_set_lhs (new_stmt, lhs);
918 else if (TREE_CODE (lhs) == SSA_NAME)
920 /* LHS should now by a default-def SSA. Unfortunately default-def
921 SSA_NAMEs need a backing variable (or at least some code examining
922 SSAs assumes it is non-NULL). So we either have to re-use the
923 decl we have at hand or introdice a new one. */
924 tree repl = create_tmp_var (TREE_TYPE (lhs), "removed_return");
925 repl = get_or_create_ssa_default_def (cfun, repl);
926 SSA_NAME_IS_DEFAULT_DEF (repl) = true;
927 imm_use_iterator ui;
928 use_operand_p use_p;
929 gimple *using_stmt;
930 FOR_EACH_IMM_USE_STMT (using_stmt, ui, lhs)
932 FOR_EACH_IMM_USE_ON_STMT (use_p, ui)
934 SET_USE (use_p, repl);
936 update_stmt (using_stmt);
938 ssa_to_remove = lhs;
942 gimple_set_block (new_stmt, gimple_block (stmt));
943 if (gimple_has_location (stmt))
944 gimple_set_location (new_stmt, gimple_location (stmt));
945 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
946 gimple_call_copy_flags (new_stmt, stmt);
947 if (gimple_in_ssa_p (cfun))
948 gimple_move_vops (new_stmt, stmt);
950 if (dump_file && (dump_flags & TDF_DETAILS))
952 fprintf (dump_file, "with stmt:");
953 print_gimple_stmt (dump_file, new_stmt, 0);
954 fprintf (dump_file, "\n");
956 gsi_replace (&gsi, new_stmt, true);
957 if (ssa_to_remove)
958 release_ssa_name (ssa_to_remove);
959 if (update_references)
962 current_node->record_stmt_references (gsi_stmt (gsi));
963 gsi_prev (&gsi);
965 while (gsi_stmt (gsi) != gsi_stmt (prev_gsi));
967 if (mod_info)
968 ipa_edge_modifications->remove (cs);
969 return new_stmt;
972 /* Dump information contained in the object in textual form to F. */
974 void
975 ipa_param_adjustments::dump (FILE *f)
977 fprintf (f, " m_always_copy_start: %i\n", m_always_copy_start);
978 ipa_dump_adjusted_parameters (f, m_adj_params);
979 if (m_skip_return)
980 fprintf (f, " Will SKIP return.\n");
983 /* Dump information contained in the object in textual form to stderr. */
985 void
986 ipa_param_adjustments::debug ()
988 dump (stderr);
991 /* Register a REPLACEMENT for accesses to BASE at UNIT_OFFSET. */
993 void
994 ipa_param_body_adjustments::register_replacement (tree base,
995 unsigned unit_offset,
996 tree replacement)
998 ipa_param_body_replacement psr;
999 psr.base = base;
1000 psr.repl = replacement;
1001 psr.dummy = NULL_TREE;
1002 psr.unit_offset = unit_offset;
1003 m_replacements.safe_push (psr);
1004 m_sorted_replacements_p = false;
1007 /* Register that REPLACEMENT should replace parameter described in APM. */
1009 void
1010 ipa_param_body_adjustments::register_replacement (ipa_adjusted_param *apm,
1011 tree replacement)
1013 gcc_checking_assert (apm->op == IPA_PARAM_OP_SPLIT
1014 || apm->op == IPA_PARAM_OP_NEW);
1015 gcc_checking_assert (!apm->prev_clone_adjustment);
1016 register_replacement (m_oparms[apm->prev_clone_index], apm->unit_offset,
1017 replacement);
1020 /* Comparator for sorting and searching
1021 ipa_param_body_adjustments::m_replacements. */
1023 static int
1024 compare_param_body_replacement (const void *va, const void *vb)
1026 const ipa_param_body_replacement *a = (const ipa_param_body_replacement *) va;
1027 const ipa_param_body_replacement *b = (const ipa_param_body_replacement *) vb;
1029 if (DECL_UID (a->base) < DECL_UID (b->base))
1030 return -1;
1031 if (DECL_UID (a->base) > DECL_UID (b->base))
1032 return 1;
1033 if (a->unit_offset < b->unit_offset)
1034 return -1;
1035 if (a->unit_offset > b->unit_offset)
1036 return 1;
1037 return 0;
1040 /* Sort m_replacements and set m_sorted_replacements_p to true. */
1042 void
1043 ipa_param_body_adjustments::sort_replacements ()
1045 if (m_sorted_replacements_p)
1046 return;
1047 m_replacements.qsort (compare_param_body_replacement);
1048 m_sorted_replacements_p = true;
1051 /* Copy or not, as appropriate given m_id and decl context, a pre-existing
1052 PARM_DECL T so that it can be included in the parameters of the modified
1053 function. */
1055 tree
1056 ipa_param_body_adjustments::carry_over_param (tree t)
1058 tree new_parm;
1059 if (m_id)
1061 new_parm = remap_decl (t, m_id);
1062 if (TREE_CODE (new_parm) != PARM_DECL)
1063 new_parm = m_id->copy_decl (t, m_id);
1065 else if (DECL_CONTEXT (t) != m_fndecl)
1067 new_parm = copy_node (t);
1068 DECL_CONTEXT (new_parm) = m_fndecl;
1070 else
1071 new_parm = t;
1072 return new_parm;
1075 /* Populate m_dead_stmts given that DEAD_PARAM is going to be removed without
1076 any replacement or splitting. REPL is the replacement VAR_SECL to base any
1077 remaining uses of a removed parameter on. Push all removed SSA names that
1078 are used within debug statements to DEBUGSTACK. */
1080 void
1081 ipa_param_body_adjustments::mark_dead_statements (tree dead_param,
1082 vec<tree> *debugstack)
1084 /* Current IPA analyses which remove unused parameters never remove a
1085 non-gimple register ones which have any use except as parameters in other
1086 calls, so we can safely leve them as they are. */
1087 if (!is_gimple_reg (dead_param))
1088 return;
1089 tree parm_ddef = ssa_default_def (m_id->src_cfun, dead_param);
1090 if (!parm_ddef || has_zero_uses (parm_ddef))
1091 return;
1093 auto_vec<tree, 4> stack;
1094 hash_set<tree> used_in_debug;
1095 m_dead_ssas.add (parm_ddef);
1096 stack.safe_push (parm_ddef);
1097 while (!stack.is_empty ())
1099 imm_use_iterator imm_iter;
1100 use_operand_p use_p;
1101 tree t = stack.pop ();
1103 insert_decl_map (m_id, t, error_mark_node);
1104 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, t)
1106 gimple *stmt = USE_STMT (use_p);
1108 /* Calls containing dead arguments cannot be deleted,
1109 modify_call_stmt will instead remove just the argument later on.
1110 If isra_track_scalar_value_uses in ipa-sra.cc is extended to look
1111 through const functions, we will need to do so here too. */
1112 if (is_gimple_call (stmt)
1113 || (m_id->blocks_to_copy
1114 && !bitmap_bit_p (m_id->blocks_to_copy,
1115 gimple_bb (stmt)->index)))
1116 continue;
1118 if (is_gimple_debug (stmt))
1120 m_dead_stmts.add (stmt);
1121 gcc_assert (gimple_debug_bind_p (stmt));
1122 if (!used_in_debug.contains (t))
1124 used_in_debug.add (t);
1125 debugstack->safe_push (t);
1128 else if (gimple_code (stmt) == GIMPLE_PHI)
1130 gphi *phi = as_a <gphi *> (stmt);
1131 int ix = PHI_ARG_INDEX_FROM_USE (use_p);
1133 if (!m_id->blocks_to_copy
1134 || bitmap_bit_p (m_id->blocks_to_copy,
1135 gimple_phi_arg_edge (phi, ix)->src->index))
1137 m_dead_stmts.add (phi);
1138 tree res = gimple_phi_result (phi);
1139 if (!m_dead_ssas.add (res))
1140 stack.safe_push (res);
1143 else if (is_gimple_assign (stmt))
1145 m_dead_stmts.add (stmt);
1146 if (!gimple_clobber_p (stmt))
1148 tree lhs = gimple_assign_lhs (stmt);
1149 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
1150 if (!m_dead_ssas.add (lhs))
1151 stack.safe_push (lhs);
1154 else
1155 /* IPA-SRA does not analyze other types of statements. */
1156 gcc_unreachable ();
1160 if (!MAY_HAVE_DEBUG_STMTS)
1162 gcc_assert (debugstack->is_empty ());
1163 return;
1166 tree dp_ddecl = build_debug_expr_decl (TREE_TYPE (dead_param));
1167 /* FIXME: Is setting the mode really necessary? */
1168 SET_DECL_MODE (dp_ddecl, DECL_MODE (dead_param));
1169 m_dead_ssa_debug_equiv.put (parm_ddef, dp_ddecl);
1172 /* Callback to walk_tree. If REMAP is an SSA_NAME that is present in hash_map
1173 passed in DATA, replace it with unshared version of what it was mapped to.
1174 If an SSA argument would be remapped to NULL, the whole operation needs to
1175 abort which is signaled by returning error_mark_node. */
1177 static tree
1178 replace_with_mapped_expr (tree *remap, int *walk_subtrees, void *data)
1180 if (TYPE_P (*remap))
1182 *walk_subtrees = 0;
1183 return 0;
1185 if (TREE_CODE (*remap) != SSA_NAME)
1186 return 0;
1188 *walk_subtrees = 0;
1190 hash_map<tree, tree> *equivs = (hash_map<tree, tree> *) data;
1191 if (tree *p = equivs->get (*remap))
1193 if (!*p)
1194 return error_mark_node;
1195 *remap = unshare_expr (*p);
1197 return 0;
1200 /* Replace all occurances of SSAs in m_dead_ssa_debug_equiv in t with what they
1201 are mapped to. */
1203 void
1204 ipa_param_body_adjustments::remap_with_debug_expressions (tree *t)
1206 /* If *t is an SSA_NAME which should have its debug statements reset, it is
1207 mapped to NULL in the hash_map.
1209 It is perhaps simpler to handle the SSA_NAME cases directly and only
1210 invoke walk_tree on more complex expressions. When
1211 remap_with_debug_expressions is called from tree-inline.cc, a to-be-reset
1212 SSA_NAME can be an operand to such expressions and the entire debug
1213 variable we are remapping should be reset. This is signaled by walk_tree
1214 returning error_mark_node and done by setting *t to NULL. */
1215 if (TREE_CODE (*t) == SSA_NAME)
1217 if (tree *p = m_dead_ssa_debug_equiv.get (*t))
1218 *t = *p;
1220 else if (walk_tree (t, replace_with_mapped_expr,
1221 &m_dead_ssa_debug_equiv, NULL) == error_mark_node)
1222 *t = NULL_TREE;
1225 /* For an SSA_NAME DEAD_SSA which is about to be DCEd because it is based on a
1226 useless parameter, prepare an expression that should represent it in
1227 debug_binds in the cloned function and add a mapping from DEAD_SSA to
1228 m_dead_ssa_debug_equiv. That mapping is to NULL when the associated
1229 debug_statement has to be reset instead. In such case return false,
1230 ottherwise return true. If DEAD_SSA comes from a basic block which is not
1231 about to be copied, ignore it and return true. */
1233 bool
1234 ipa_param_body_adjustments::prepare_debug_expressions (tree dead_ssa)
1236 gcc_checking_assert (m_dead_ssas.contains (dead_ssa));
1237 if (tree *d = m_dead_ssa_debug_equiv.get (dead_ssa))
1238 return (*d != NULL_TREE);
1240 gcc_assert (!SSA_NAME_IS_DEFAULT_DEF (dead_ssa));
1241 gimple *def = SSA_NAME_DEF_STMT (dead_ssa);
1242 if (m_id->blocks_to_copy
1243 && !bitmap_bit_p (m_id->blocks_to_copy, gimple_bb (def)->index))
1244 return true;
1246 if (gimple_code (def) == GIMPLE_PHI)
1248 /* In theory, we could ignore all SSAs coming from BBs not in
1249 m_id->blocks_to_copy but at the time of the writing this code that
1250 should never really be the case because only fnsplit uses that bitmap,
1251 so don't bother. */
1252 tree value = degenerate_phi_result (as_a <gphi *> (def));
1253 if (!value
1254 || (m_dead_ssas.contains (value)
1255 && !prepare_debug_expressions (value)))
1257 m_dead_ssa_debug_equiv.put (dead_ssa, NULL_TREE);
1258 return false;
1261 gcc_assert (TREE_CODE (value) == SSA_NAME);
1262 tree *d = m_dead_ssa_debug_equiv.get (value);
1263 m_dead_ssa_debug_equiv.put (dead_ssa, *d);
1264 return true;
1267 bool lost = false;
1268 use_operand_p use_p;
1269 ssa_op_iter oi;
1270 FOR_EACH_PHI_OR_STMT_USE (use_p, def, oi, SSA_OP_USE)
1272 tree use = USE_FROM_PTR (use_p);
1273 if (m_dead_ssas.contains (use)
1274 && !prepare_debug_expressions (use))
1276 lost = true;
1277 break;
1281 if (lost)
1283 m_dead_ssa_debug_equiv.put (dead_ssa, NULL_TREE);
1284 return false;
1287 if (is_gimple_assign (def))
1289 gcc_assert (!gimple_clobber_p (def));
1290 if (gimple_assign_copy_p (def)
1291 && TREE_CODE (gimple_assign_rhs1 (def)) == SSA_NAME)
1293 tree d = *m_dead_ssa_debug_equiv.get (gimple_assign_rhs1 (def));
1294 gcc_assert (d);
1295 m_dead_ssa_debug_equiv.put (dead_ssa, d);
1296 return true;
1299 tree val
1300 = unshare_expr_without_location (gimple_assign_rhs_to_tree (def));
1301 remap_with_debug_expressions (&val);
1303 tree vexpr = build_debug_expr_decl (TREE_TYPE (val));
1304 m_dead_stmt_debug_equiv.put (def, val);
1305 m_dead_ssa_debug_equiv.put (dead_ssa, vexpr);
1306 return true;
1308 else
1309 gcc_unreachable ();
1312 /* Common initialization performed by all ipa_param_body_adjustments
1313 constructors. OLD_FNDECL is the declaration we take original arguments
1314 from, (it may be the same as M_FNDECL). VARS, if non-NULL, is a pointer to
1315 a chained list of new local variables. TREE_MAP is the IPA-CP produced
1316 mapping of trees to constants.
1318 The function is rather long but it really onlu initializes all data members
1319 of the class. It creates new param DECLs, finds their new types, */
1321 void
1322 ipa_param_body_adjustments::common_initialization (tree old_fndecl,
1323 tree *vars,
1324 vec<ipa_replace_map *,
1325 va_gc> *tree_map)
1327 push_function_arg_decls (&m_oparms, old_fndecl);
1328 auto_vec<tree,16> otypes;
1329 if (TYPE_ARG_TYPES (TREE_TYPE (old_fndecl)) != NULL_TREE)
1330 push_function_arg_types (&otypes, TREE_TYPE (old_fndecl));
1331 else
1333 auto_vec<tree,16> oparms;
1334 push_function_arg_decls (&oparms, old_fndecl);
1335 unsigned ocount = oparms.length ();
1336 otypes.reserve_exact (ocount);
1337 for (unsigned i = 0; i < ocount; i++)
1338 otypes.quick_push (TREE_TYPE (oparms[i]));
1340 fill_vector_of_new_param_types (&m_new_types, &otypes, m_adj_params, true);
1342 auto_vec<bool, 16> kept;
1343 kept.reserve_exact (m_oparms.length ());
1344 kept.quick_grow_cleared (m_oparms.length ());
1345 auto_vec<bool, 16> split;
1346 split.reserve_exact (m_oparms.length ());
1347 split.quick_grow_cleared (m_oparms.length ());
1349 unsigned adj_len = vec_safe_length (m_adj_params);
1350 m_method2func = ((TREE_CODE (TREE_TYPE (m_fndecl)) == METHOD_TYPE)
1351 && (adj_len == 0
1352 || (*m_adj_params)[0].op != IPA_PARAM_OP_COPY
1353 || (*m_adj_params)[0].base_index != 0));
1355 /* The main job of the this function is to go over the vector of adjusted
1356 parameters and create declarations or find corresponding old ones and push
1357 them to m_new_decls. For IPA-SRA replacements it also creates
1358 corresponding m_id->dst_node->clone.performed_splits entries. */
1360 m_new_decls.reserve_exact (adj_len);
1361 for (unsigned i = 0; i < adj_len ; i++)
1363 ipa_adjusted_param *apm = &(*m_adj_params)[i];
1364 unsigned prev_index = apm->prev_clone_index;
1365 tree new_parm;
1366 if (apm->op == IPA_PARAM_OP_COPY
1367 || apm->prev_clone_adjustment)
1369 kept[prev_index] = true;
1370 new_parm = carry_over_param (m_oparms[prev_index]);
1371 m_new_decls.quick_push (new_parm);
1373 else if (apm->op == IPA_PARAM_OP_NEW
1374 || apm->op == IPA_PARAM_OP_SPLIT)
1376 tree new_type = m_new_types[i];
1377 gcc_checking_assert (new_type);
1378 new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
1379 new_type);
1380 const char *prefix = ipa_param_prefixes[apm->param_prefix_index];
1381 DECL_NAME (new_parm) = create_tmp_var_name (prefix);
1382 DECL_ARTIFICIAL (new_parm) = 1;
1383 DECL_ARG_TYPE (new_parm) = new_type;
1384 DECL_CONTEXT (new_parm) = m_fndecl;
1385 TREE_USED (new_parm) = 1;
1386 DECL_IGNORED_P (new_parm) = 1;
1387 layout_decl (new_parm, 0);
1388 m_new_decls.quick_push (new_parm);
1390 if (apm->op == IPA_PARAM_OP_SPLIT)
1392 split[prev_index] = true;
1393 register_replacement (apm, new_parm);
1396 else
1397 gcc_unreachable ();
1400 auto_vec <int, 16> index_mapping;
1401 bool need_remap = false;
1402 if (m_id)
1404 clone_info *cinfo = clone_info::get (m_id->src_node);
1405 if (cinfo && cinfo->param_adjustments)
1407 cinfo->param_adjustments->get_updated_indices (&index_mapping);
1408 need_remap = true;
1411 if (ipcp_transformation *ipcp_ts
1412 = ipcp_get_transformation_summary (m_id->src_node))
1414 for (const ipa_argagg_value &av : ipcp_ts->m_agg_values)
1416 int parm_num = av.index;
1418 if (need_remap)
1420 /* FIXME: We cannot handle the situation when IPA-CP
1421 identified that a parameter is a pointer to a global
1422 variable and at the same time the variable has some known
1423 constant contents (PR 107640). The best place to make
1424 sure we don't drop such constants on the floor probably is
1425 not here, but we have to make sure that it does not
1426 confuse the remapping. */
1427 if (parm_num >= (int) index_mapping.length ())
1428 continue;
1429 parm_num = index_mapping[parm_num];
1430 if (parm_num < 0)
1431 continue;
1434 if (!kept[parm_num])
1436 /* IPA-CP has detected an aggregate constant in a parameter
1437 that will not be kept, which means that IPA-SRA would have
1438 split it if there wasn't a constant. Because we are about
1439 to remove the original, this is the last chance where we
1440 can substitute the uses with a constant (for values passed
1441 by reference) or do the split but initialize the
1442 replacement with a constant (for split aggregates passed
1443 by value). */
1445 tree repl;
1446 if (av.by_ref)
1447 repl = av.value;
1448 else
1450 repl = create_tmp_var (TREE_TYPE (av.value),
1451 "removed_ipa_cp");
1452 gimple *init_stmt = gimple_build_assign (repl, av.value);
1453 m_split_agg_csts_inits.safe_push (init_stmt);
1455 register_replacement (m_oparms[parm_num], av.unit_offset,
1456 repl);
1457 split[parm_num] = true;
1462 sort_replacements ();
1464 if (tree_map)
1466 /* Do not treat parameters which were replaced with a constant as
1467 completely vanished. */
1468 for (unsigned i = 0; i < tree_map->length (); i++)
1470 int parm_num = (*tree_map)[i]->parm_num;
1471 gcc_assert (parm_num >= 0);
1472 if (need_remap)
1473 parm_num = index_mapping[parm_num];
1474 kept[parm_num] = true;
1478 /* As part of body modifications, we will also have to replace remaining uses
1479 of remaining uses of removed PARM_DECLs (which do not however use the
1480 initial value) with their VAR_DECL copies.
1482 We do this differently with and without m_id. With m_id, we rely on its
1483 mapping and create a replacement straight away. Without it, we have our
1484 own mechanism for which we have to populate m_removed_decls vector. Just
1485 don't mix them, that is why you should not call
1486 replace_removed_params_ssa_names or perform_cfun_body_modifications when
1487 you construct with ID not equal to NULL. */
1489 auto_vec<tree, 8> ssas_to_process_debug;
1490 unsigned op_len = m_oparms.length ();
1491 for (unsigned i = 0; i < op_len; i++)
1492 if (!kept[i])
1494 if (m_id)
1496 gcc_assert (!m_id->decl_map->get (m_oparms[i]));
1497 tree var = copy_decl_to_var (m_oparms[i], m_id);
1498 insert_decl_map (m_id, m_oparms[i], var);
1499 /* Declare this new variable. */
1500 DECL_CHAIN (var) = *vars;
1501 *vars = var;
1503 /* If this is not a split but a real removal, init hash sets
1504 that will guide what not to copy to the new body. */
1505 if (!split[i])
1506 mark_dead_statements (m_oparms[i], &ssas_to_process_debug);
1507 if (MAY_HAVE_DEBUG_STMTS
1508 && is_gimple_reg (m_oparms[i]))
1509 m_reset_debug_decls.safe_push (m_oparms[i]);
1511 else
1513 m_removed_decls.safe_push (m_oparms[i]);
1514 m_removed_map.put (m_oparms[i], m_removed_decls.length () - 1);
1515 if (MAY_HAVE_DEBUG_STMTS
1516 && !kept[i]
1517 && is_gimple_reg (m_oparms[i]))
1518 m_reset_debug_decls.safe_push (m_oparms[i]);
1522 while (!ssas_to_process_debug.is_empty ())
1523 prepare_debug_expressions (ssas_to_process_debug.pop ());
1526 /* Constructor of ipa_param_body_adjustments from a simple list of
1527 modifications to parameters listed in ADJ_PARAMS which will prepare ground
1528 for modification of parameters of fndecl. Return value of the function will
1529 not be removed and the object will assume it does not run as a part of
1530 tree-function_versioning. */
1532 ipa_param_body_adjustments
1533 ::ipa_param_body_adjustments (vec<ipa_adjusted_param, va_gc> *adj_params,
1534 tree fndecl)
1535 : m_adj_params (adj_params), m_adjustments (NULL), m_reset_debug_decls (),
1536 m_dead_stmts (), m_dead_ssas (), m_dead_ssa_debug_equiv (),
1537 m_dead_stmt_debug_equiv (), m_fndecl (fndecl), m_id (NULL), m_oparms (),
1538 m_new_decls (), m_new_types (), m_replacements (),
1539 m_split_agg_csts_inits (), m_removed_decls (), m_removed_map (),
1540 m_method2func (false), m_sorted_replacements_p (true)
1542 common_initialization (fndecl, NULL, NULL);
1545 /* Constructor of ipa_param_body_adjustments from ipa_param_adjustments in
1546 ADJUSTMENTS which will prepare ground for modification of parameters of
1547 fndecl. The object will assume it does not run as a part of
1548 tree-function_versioning. */
1550 ipa_param_body_adjustments
1551 ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
1552 tree fndecl)
1553 : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
1554 m_reset_debug_decls (), m_dead_stmts (), m_dead_ssas (),
1555 m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (), m_fndecl (fndecl),
1556 m_id (NULL), m_oparms (), m_new_decls (), m_new_types (), m_replacements (),
1557 m_split_agg_csts_inits (), m_removed_decls (), m_removed_map (),
1558 m_method2func (false), m_sorted_replacements_p (true)
1560 common_initialization (fndecl, NULL, NULL);
1563 /* Constructor of ipa_param_body_adjustments which sets it up as a part of
1564 running tree_function_versioning. Planned modifications to the function are
1565 in ADJUSTMENTS. FNDECL designates the new function clone which is being
1566 modified. OLD_FNDECL is the function of which FNDECL is a clone (and which
1567 at the time of invocation still share DECL_ARGUMENTS). ID is the
1568 copy_body_data structure driving the wholy body copying process. VARS is a
1569 pointer to the head of the list of new local variables, TREE_MAP is the map
1570 that drives tree substitution in the cloning process. */
1572 ipa_param_body_adjustments
1573 ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
1574 tree fndecl, tree old_fndecl,
1575 copy_body_data *id, tree *vars,
1576 vec<ipa_replace_map *, va_gc> *tree_map)
1577 : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
1578 m_reset_debug_decls (), m_dead_stmts (), m_dead_ssas (),
1579 m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (), m_fndecl (fndecl),
1580 m_id (id), m_oparms (), m_new_decls (), m_new_types (), m_replacements (),
1581 m_split_agg_csts_inits (), m_removed_decls (), m_removed_map (),
1582 m_method2func (false), m_sorted_replacements_p (true)
1584 common_initialization (old_fndecl, vars, tree_map);
1587 /* Chain new param decls up and return them. */
1589 tree
1590 ipa_param_body_adjustments::get_new_param_chain ()
1592 tree result;
1593 tree *link = &result;
1595 unsigned len = vec_safe_length (m_adj_params);
1596 for (unsigned i = 0; i < len; i++)
1598 tree new_decl = m_new_decls[i];
1599 *link = new_decl;
1600 link = &DECL_CHAIN (new_decl);
1602 *link = NULL_TREE;
1603 return result;
1606 /* Modify the function parameters FNDECL and its type according to the plan in
1607 ADJUSTMENTS. This function needs to be called when the decl has not already
1608 been processed with ipa_param_adjustments::adjust_decl, otherwise just
1609 seting DECL_ARGUMENTS to whatever get_new_param_chain will do is enough. */
1611 void
1612 ipa_param_body_adjustments::modify_formal_parameters ()
1614 tree orig_type = TREE_TYPE (m_fndecl);
1615 DECL_ARGUMENTS (m_fndecl) = get_new_param_chain ();
1617 /* When signature changes, we need to clear builtin info. */
1618 if (fndecl_built_in_p (m_fndecl))
1619 set_decl_built_in_function (m_fndecl, NOT_BUILT_IN, 0);
1621 bool modified = false;
1622 size_t index = 0;
1623 if (m_adj_params)
1624 for (tree t = TYPE_ARG_TYPES (orig_type);
1625 t && !modified;
1626 t = TREE_CHAIN (t), index++)
1627 if (index >= m_adj_params->length ()
1628 || (*m_adj_params)[index].op != IPA_PARAM_OP_COPY
1629 || (*m_adj_params)[index].base_index != index)
1630 modified = true;
1632 /* At this point, removing return value is only implemented when going
1633 through tree_function_versioning, not when modifying function body
1634 directly. */
1635 gcc_assert (!m_adjustments || !m_adjustments->m_skip_return);
1636 tree new_type = build_adjusted_function_type (orig_type, &m_new_types,
1637 m_method2func, false, modified);
1639 TREE_TYPE (m_fndecl) = new_type;
1640 DECL_VIRTUAL_P (m_fndecl) = 0;
1641 DECL_LANG_SPECIFIC (m_fndecl) = NULL;
1642 if (m_method2func)
1643 DECL_VINDEX (m_fndecl) = NULL_TREE;
1646 /* Given BASE and UNIT_OFFSET, find the corresponding record among replacement
1647 structures. */
1649 ipa_param_body_replacement *
1650 ipa_param_body_adjustments::lookup_replacement_1 (tree base,
1651 unsigned unit_offset)
1653 gcc_assert (m_sorted_replacements_p);
1654 ipa_param_body_replacement key;
1655 key.base = base;
1656 key.unit_offset = unit_offset;
1657 ipa_param_body_replacement *res
1658 = std::lower_bound (m_replacements.begin (), m_replacements.end (), key,
1659 [] (const ipa_param_body_replacement &elt,
1660 const ipa_param_body_replacement &val)
1662 return (compare_param_body_replacement (&elt, &val)
1663 < 0);
1666 if (res == m_replacements.end ()
1667 || res->base != base
1668 || res->unit_offset != unit_offset)
1669 return NULL;
1670 return res;
1673 /* Find the first replacement for BASE among m_replacements and return pointer
1674 to it, or NULL if there is none. */
1676 ipa_param_body_replacement *
1677 ipa_param_body_adjustments::lookup_first_base_replacement (tree base)
1679 gcc_assert (m_sorted_replacements_p);
1680 ipa_param_body_replacement key;
1681 key.base = base;
1682 ipa_param_body_replacement *res
1683 = std::lower_bound (m_replacements.begin (), m_replacements.end (), key,
1684 [] (const ipa_param_body_replacement &elt,
1685 const ipa_param_body_replacement &val)
1687 if (DECL_UID (elt.base) < DECL_UID (val.base))
1688 return true;
1689 return false;
1692 if (res == m_replacements.end ()
1693 || res->base != base)
1694 return NULL;
1695 return res;
1698 /* Given BASE and UNIT_OFFSET, find the corresponding replacement expression
1699 and return it, assuming it is known it does not hold value by reference or
1700 in reverse storage order. */
1702 tree
1703 ipa_param_body_adjustments::lookup_replacement (tree base, unsigned unit_offset)
1705 ipa_param_body_replacement *pbr = lookup_replacement_1 (base, unit_offset);
1706 if (!pbr)
1707 return NULL;
1708 return pbr->repl;
1711 /* If T is an SSA_NAME, return NULL if it is not a default def or
1712 return its base variable if it is. If IGNORE_DEFAULT_DEF is true,
1713 the base variable is always returned, regardless if it is a default
1714 def. Return T if it is not an SSA_NAME. */
1716 static tree
1717 get_ssa_base_param (tree t, bool ignore_default_def)
1719 if (TREE_CODE (t) == SSA_NAME)
1721 if (ignore_default_def || SSA_NAME_IS_DEFAULT_DEF (t))
1722 return SSA_NAME_VAR (t);
1723 else
1724 return NULL_TREE;
1726 return t;
1729 /* Given an expression, return the structure describing how it should be
1730 replaced if it accesses a part of a split parameter or NULL otherwise.
1732 Do not free the result, it will be deallocated when the object is destroyed.
1734 If IGNORE_DEFAULT_DEF is cleared, consider only SSA_NAMEs of PARM_DECLs
1735 which are default definitions, if set, consider all SSA_NAMEs of
1736 PARM_DECLs. */
1738 ipa_param_body_replacement *
1739 ipa_param_body_adjustments::get_expr_replacement (tree expr,
1740 bool ignore_default_def)
1742 tree base;
1743 unsigned unit_offset;
1745 if (!isra_get_ref_base_and_offset (expr, &base, &unit_offset))
1746 return NULL;
1748 base = get_ssa_base_param (base, ignore_default_def);
1749 if (!base || TREE_CODE (base) != PARM_DECL)
1750 return NULL;
1751 return lookup_replacement_1 (base, unit_offset);
1754 /* Given OLD_DECL, which is a PARM_DECL of a parameter that is being removed
1755 (which includes it being split or replaced), return a new variable that
1756 should be used for any SSA names that will remain in the function that
1757 previously belonged to OLD_DECL. */
1759 tree
1760 ipa_param_body_adjustments::get_replacement_ssa_base (tree old_decl)
1762 unsigned *idx = m_removed_map.get (old_decl);
1763 if (!idx)
1764 return NULL;
1766 tree repl;
1767 if (TREE_CODE (m_removed_decls[*idx]) == PARM_DECL)
1769 gcc_assert (m_removed_decls[*idx] == old_decl);
1770 repl = copy_var_decl (old_decl, DECL_NAME (old_decl),
1771 TREE_TYPE (old_decl));
1772 m_removed_decls[*idx] = repl;
1774 else
1775 repl = m_removed_decls[*idx];
1776 return repl;
1779 /* If OLD_NAME, which is being defined by statement STMT, is an SSA_NAME of a
1780 parameter which is to be removed because its value is not used, create a new
1781 SSA_NAME relating to a replacement VAR_DECL, replace all uses of the
1782 original with it and return it. If there is no need to re-map, return NULL.
1783 ADJUSTMENTS is a pointer to a vector of IPA-SRA adjustments. */
1785 tree
1786 ipa_param_body_adjustments::replace_removed_params_ssa_names (tree old_name,
1787 gimple *stmt)
1789 gcc_assert (!m_id);
1790 if (TREE_CODE (old_name) != SSA_NAME)
1791 return NULL;
1793 tree decl = SSA_NAME_VAR (old_name);
1794 if (decl == NULL_TREE
1795 || TREE_CODE (decl) != PARM_DECL)
1796 return NULL;
1798 tree repl = get_replacement_ssa_base (decl);
1799 if (!repl)
1800 return NULL;
1802 tree new_name = make_ssa_name (repl, stmt);
1803 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name)
1804 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (old_name);
1806 if (dump_file && (dump_flags & TDF_DETAILS))
1808 fprintf (dump_file, "replacing an SSA name of a removed param ");
1809 print_generic_expr (dump_file, old_name);
1810 fprintf (dump_file, " with ");
1811 print_generic_expr (dump_file, new_name);
1812 fprintf (dump_file, "\n");
1815 replace_uses_by (old_name, new_name);
1816 return new_name;
1819 /* If the expression *EXPR_P should be replaced, do so. CONVERT specifies
1820 whether the function should care about type incompatibility of the current
1821 and new expressions. If it is false, the function will leave
1822 incompatibility issues to the caller - note that when the function
1823 encounters a BIT_FIELD_REF, IMAGPART_EXPR or REALPART_EXPR, it will modify
1824 their bases instead of the expressions themselves and then also performs any
1825 necessary conversions. */
1827 bool
1828 ipa_param_body_adjustments::modify_expression (tree *expr_p, bool convert)
1830 tree expr = *expr_p;
1832 if (m_replacements.is_empty ())
1833 return false;
1834 if (TREE_CODE (expr) == BIT_FIELD_REF
1835 || TREE_CODE (expr) == IMAGPART_EXPR
1836 || TREE_CODE (expr) == REALPART_EXPR)
1838 expr_p = &TREE_OPERAND (expr, 0);
1839 expr = *expr_p;
1840 convert = true;
1843 ipa_param_body_replacement *pbr = get_expr_replacement (expr, false);
1844 if (!pbr)
1845 return false;
1847 tree repl = pbr->repl;
1848 if (dump_file && (dump_flags & TDF_DETAILS))
1850 fprintf (dump_file, "About to replace expr ");
1851 print_generic_expr (dump_file, expr);
1852 fprintf (dump_file, " with ");
1853 print_generic_expr (dump_file, repl);
1854 fprintf (dump_file, "\n");
1857 if (convert && !useless_type_conversion_p (TREE_TYPE (expr),
1858 TREE_TYPE (repl)))
1860 gcc_checking_assert (tree_to_shwi (TYPE_SIZE (TREE_TYPE (expr)))
1861 == tree_to_shwi (TYPE_SIZE (TREE_TYPE (repl))));
1862 tree vce = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expr), repl);
1863 *expr_p = vce;
1865 else
1866 *expr_p = repl;
1867 return true;
1870 /* If the assignment statement STMT contains any expressions that need to
1871 replaced with a different one as noted by ADJUSTMENTS, do so. Handle any
1872 potential type incompatibilities. If any conversion sttements have to be
1873 pre-pended to STMT, they will be added to EXTRA_STMTS. Return true iff the
1874 statement was modified. */
1876 bool
1877 ipa_param_body_adjustments::modify_assignment (gimple *stmt,
1878 gimple_seq *extra_stmts)
1880 tree *lhs_p, *rhs_p;
1881 bool any;
1883 if (m_replacements.is_empty () || !gimple_assign_single_p (stmt))
1884 return false;
1886 rhs_p = gimple_assign_rhs1_ptr (stmt);
1887 lhs_p = gimple_assign_lhs_ptr (stmt);
1889 any = modify_expression (lhs_p, false);
1890 any |= modify_expression (rhs_p, false);
1891 if (any
1892 && !useless_type_conversion_p (TREE_TYPE (*lhs_p), TREE_TYPE (*rhs_p)))
1894 if (TREE_CODE (*rhs_p) == CONSTRUCTOR)
1896 /* V_C_Es of constructors can cause trouble (PR 42714). */
1897 if (is_gimple_reg_type (TREE_TYPE (*lhs_p)))
1898 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
1899 else
1900 *rhs_p = build_constructor (TREE_TYPE (*lhs_p),
1901 NULL);
1903 else
1905 gcc_checking_assert (tree_to_shwi (TYPE_SIZE (TREE_TYPE (*lhs_p)))
1906 == tree_to_shwi (TYPE_SIZE (TREE_TYPE (*rhs_p))));
1907 tree new_rhs = fold_build1_loc (gimple_location (stmt),
1908 VIEW_CONVERT_EXPR, TREE_TYPE (*lhs_p),
1909 *rhs_p);
1910 tree tmp = force_gimple_operand (new_rhs, extra_stmts, true,
1911 NULL_TREE);
1912 gimple_assign_set_rhs1 (stmt, tmp);
1914 return true;
1917 return any;
1920 /* Record information about what modifications to call arguments have already
1921 been done by clone materialization into a summary describing CS. The
1922 information is stored in NEW_INDEX_MAP, NEW_PT_MAP and NEW_ALWAYS_COPY_DELTA
1923 and correspond to equivalent fields in ipa_edge_modification_info. Return
1924 the edge summary. */
1926 static ipa_edge_modification_info *
1927 record_argument_state_1 (cgraph_edge *cs, const vec<int> &new_index_map,
1928 const vec<pass_through_split_map> &new_pt_map,
1929 int new_always_copy_delta)
1932 ipa_edge_modification_info *sum = ipa_edge_modifications->get_create (cs);
1934 unsigned len = sum->pass_through_map.length ();
1935 for (unsigned i = 0; i < len; i++)
1937 unsigned oldnew = sum->pass_through_map[i].new_index;
1938 sum->pass_through_map[i].new_index = new_index_map[oldnew];
1941 len = sum->index_map.length ();
1942 if (len > 0)
1944 unsigned nptlen = new_pt_map.length ();
1945 for (unsigned j = 0; j < nptlen; j++)
1947 int inverse = -1;
1948 for (unsigned i = 0; i < len ; i++)
1949 if ((unsigned) sum->index_map[i] == new_pt_map[j].base_index)
1951 inverse = i;
1952 break;
1954 gcc_assert (inverse >= 0);
1955 pass_through_split_map ptm_item;
1957 ptm_item.base_index = inverse;
1958 ptm_item.unit_offset = new_pt_map[j].unit_offset;
1959 ptm_item.new_index = new_pt_map[j].new_index;
1960 sum->pass_through_map.safe_push (ptm_item);
1963 for (unsigned i = 0; i < len; i++)
1965 int idx = sum->index_map[i];
1966 if (idx < 0)
1967 continue;
1968 sum->index_map[i] = new_index_map[idx];
1971 else
1973 sum->pass_through_map.safe_splice (new_pt_map);
1974 sum->index_map.safe_splice (new_index_map);
1976 sum->always_copy_delta += new_always_copy_delta;
1977 return sum;
1980 /* Record information about what modifications to call arguments have already
1981 been done by clone materialization into a summary of an edge describing the
1982 call in this clone and all its clones. NEW_INDEX_MAP, NEW_PT_MAP and
1983 NEW_ALWAYS_COPY_DELTA have the same meaning as record_argument_state_1.
1985 In order to associate the info with the right edge summaries, we need
1986 address of the ORIG_STMT in the function from which we are cloning (because
1987 the edges have not yet been re-assigned to the new statement that has just
1988 been created) and ID, the structure governing function body copying. */
1990 static void
1991 record_argument_state (copy_body_data *id, gimple *orig_stmt,
1992 const vec<int> &new_index_map,
1993 const vec<pass_through_split_map> &new_pt_map,
1994 int new_always_copy_delta)
1996 if (!ipa_edge_modifications)
1997 ipa_edge_modifications = new ipa_edge_modification_sum (symtab);
1999 struct cgraph_node *this_node = id->dst_node;
2000 ipa_edge_modification_info *first_sum = NULL;
2001 cgraph_edge *cs = this_node->get_edge (orig_stmt);
2002 if (cs)
2003 first_sum = record_argument_state_1 (cs, new_index_map, new_pt_map,
2004 new_always_copy_delta);
2005 else
2006 gcc_assert (this_node->clones);
2008 if (!this_node->clones)
2009 return;
2010 for (cgraph_node *subclone = this_node->clones; subclone != this_node;)
2012 cs = subclone->get_edge (orig_stmt);
2013 if (cs)
2015 if (!first_sum)
2016 first_sum = record_argument_state_1 (cs, new_index_map, new_pt_map,
2017 new_always_copy_delta);
2018 else
2020 ipa_edge_modification_info *s2
2021 = ipa_edge_modifications->get_create (cs);
2022 s2->index_map.truncate (0);
2023 s2->index_map.safe_splice (first_sum->index_map);
2024 s2->pass_through_map.truncate (0);
2025 s2->pass_through_map.safe_splice (first_sum->pass_through_map);
2026 s2->always_copy_delta = first_sum->always_copy_delta;
2029 else
2030 gcc_assert (subclone->clones);
2032 if (subclone->clones)
2033 subclone = subclone->clones;
2034 else if (subclone->next_sibling_clone)
2035 subclone = subclone->next_sibling_clone;
2036 else
2038 while (subclone != this_node && !subclone->next_sibling_clone)
2039 subclone = subclone->clone_of;
2040 if (subclone != this_node)
2041 subclone = subclone->next_sibling_clone;
2046 /* If the call statement pointed at by STMT_P contains any expressions that
2047 need to replaced with a different one as noted by ADJUSTMENTS, do so. f the
2048 statement needs to be rebuilt, do so. Return true if any modifications have
2049 been performed. ORIG_STMT, if not NULL, is the original statement in the
2050 function that is being cloned from, which at this point can be used to look
2051 up call_graph edges.
2053 If the method is invoked as a part of IPA clone materialization and if any
2054 parameter split is pass-through, i.e. it applies to the functin that is
2055 being modified and also to the callee of the statement, replace the
2056 parameter passed to old callee with all of the replacement a callee might
2057 possibly want and record the performed argument modifications in
2058 ipa_edge_modifications. Likewise if any argument has already been left out
2059 because it is not necessary. */
2061 bool
2062 ipa_param_body_adjustments::modify_call_stmt (gcall **stmt_p,
2063 gimple *orig_stmt)
2065 auto_vec <unsigned, 4> pass_through_args;
2066 auto_vec <unsigned, 4> pass_through_pbr_indices;
2067 auto_vec <HOST_WIDE_INT, 4> pass_through_offsets;
2068 gcall *stmt = *stmt_p;
2069 unsigned nargs = gimple_call_num_args (stmt);
2070 bool recreate = false;
2071 gcc_assert (m_sorted_replacements_p);
2073 for (unsigned i = 0; i < gimple_call_num_args (stmt); i++)
2075 tree t = gimple_call_arg (stmt, i);
2076 gcc_assert (TREE_CODE (t) != BIT_FIELD_REF
2077 && TREE_CODE (t) != IMAGPART_EXPR
2078 && TREE_CODE (t) != REALPART_EXPR);
2080 if (TREE_CODE (t) == SSA_NAME
2081 && m_dead_ssas.contains (t))
2082 recreate = true;
2084 if (m_replacements.is_empty ())
2085 continue;
2087 tree base;
2088 unsigned agg_arg_offset;
2089 if (!isra_get_ref_base_and_offset (t, &base, &agg_arg_offset))
2090 continue;
2092 bool by_ref = false;
2093 if (TREE_CODE (base) == SSA_NAME)
2095 if (!SSA_NAME_IS_DEFAULT_DEF (base))
2096 continue;
2097 base = SSA_NAME_VAR (base);
2098 gcc_checking_assert (base);
2099 by_ref = true;
2101 if (TREE_CODE (base) != PARM_DECL)
2102 continue;
2104 ipa_param_body_replacement *first_rep
2105 = lookup_first_base_replacement (base);
2106 if (!first_rep)
2107 continue;
2108 unsigned first_rep_index = first_rep - m_replacements.begin ();
2110 /* We still have to distinguish between an end-use that we have to
2111 transform now and a pass-through, which happens in the following
2112 two cases. */
2114 /* TODO: After we adjust ptr_parm_has_nonarg_uses to also consider
2115 &MEM_REF[ssa_name + offset], we will also have to detect that case
2116 here. */
2118 if (TREE_CODE (t) == SSA_NAME
2119 && SSA_NAME_IS_DEFAULT_DEF (t)
2120 && SSA_NAME_VAR (t)
2121 && TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL)
2123 /* This must be a by_reference pass-through. */
2124 recreate = true;
2125 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2126 pass_through_args.safe_push (i);
2127 pass_through_pbr_indices.safe_push (first_rep_index);
2128 pass_through_offsets.safe_push (agg_arg_offset);
2130 else if (!by_ref && AGGREGATE_TYPE_P (TREE_TYPE (t)))
2132 /* Currently IPA-SRA guarantees the aggregate access type
2133 exactly matches in this case. So if it does not match, it is
2134 a pass-through argument that will be sorted out at edge
2135 redirection time. */
2136 ipa_param_body_replacement *pbr
2137 = lookup_replacement_1 (base, agg_arg_offset);
2139 if (!pbr
2140 || (TYPE_MAIN_VARIANT (TREE_TYPE (t))
2141 != TYPE_MAIN_VARIANT (TREE_TYPE (pbr->repl))))
2143 recreate = true;
2144 pass_through_args.safe_push (i);
2145 pass_through_pbr_indices.safe_push (first_rep_index);
2146 pass_through_offsets.safe_push (agg_arg_offset);
2151 if (!recreate)
2153 /* No need to rebuild the statement, let's just modify arguments
2154 and the LHS if/as appropriate. */
2155 bool modified = false;
2156 for (unsigned i = 0; i < nargs; i++)
2158 tree *t = gimple_call_arg_ptr (stmt, i);
2159 modified |= modify_expression (t, true);
2161 if (gimple_call_lhs (stmt))
2163 tree *t = gimple_call_lhs_ptr (stmt);
2164 modified |= modify_expression (t, false);
2166 return modified;
2169 auto_vec<int, 16> index_map;
2170 auto_vec<pass_through_split_map, 4> pass_through_map;
2171 auto_vec<tree, 16> vargs;
2172 int always_copy_delta = 0;
2173 unsigned pt_idx = 0;
2174 int new_arg_idx = 0;
2175 for (unsigned i = 0; i < nargs; i++)
2177 if (pt_idx < pass_through_args.length ()
2178 && i == pass_through_args[pt_idx])
2180 unsigned j = pass_through_pbr_indices[pt_idx];
2181 unsigned agg_arg_offset = pass_through_offsets[pt_idx];
2182 pt_idx++;
2183 always_copy_delta--;
2184 tree base = m_replacements[j].base;
2186 /* In order to be put into SSA form, we have to push all replacements
2187 pertaining to this parameter as parameters to the call statement.
2188 Edge redirection will need to use edge summary to weed out the
2189 unnecessary ones. */
2190 unsigned repl_list_len = m_replacements.length ();
2191 for (; j < repl_list_len; j++)
2193 if (m_replacements[j].base != base)
2194 break;
2195 if (m_replacements[j].unit_offset < agg_arg_offset)
2196 continue;
2197 pass_through_split_map pt_map;
2198 pt_map.base_index = i;
2199 pt_map.unit_offset
2200 = m_replacements[j].unit_offset - agg_arg_offset;
2201 pt_map.new_index = new_arg_idx;
2202 pass_through_map.safe_push (pt_map);
2203 vargs.safe_push (m_replacements[j].repl);
2204 new_arg_idx++;
2205 always_copy_delta++;
2207 index_map.safe_push (-1);
2209 else
2211 tree t = gimple_call_arg (stmt, i);
2212 if (TREE_CODE (t) == SSA_NAME
2213 && m_dead_ssas.contains (t))
2215 always_copy_delta--;
2216 index_map.safe_push (-1);
2218 else
2220 modify_expression (&t, true);
2221 vargs.safe_push (t);
2222 index_map.safe_push (new_arg_idx);
2223 new_arg_idx++;
2228 gcall *new_stmt = gimple_build_call_vec (gimple_call_fn (stmt), vargs);
2229 if (gimple_has_location (stmt))
2230 gimple_set_location (new_stmt, gimple_location (stmt));
2231 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
2232 gimple_call_copy_flags (new_stmt, stmt);
2233 if (tree lhs = gimple_call_lhs (stmt))
2235 modify_expression (&lhs, false);
2236 /* Avoid adjusting SSA_NAME_DEF_STMT of a SSA lhs, SSA names
2237 have not yet been remapped. */
2238 *gimple_call_lhs_ptr (new_stmt) = lhs;
2240 *stmt_p = new_stmt;
2242 if (orig_stmt)
2243 record_argument_state (m_id, orig_stmt, index_map, pass_through_map,
2244 always_copy_delta);
2245 return true;
2248 /* If the statement STMT contains any expressions that need to replaced with a
2249 different one as noted by ADJUSTMENTS, do so. Handle any potential type
2250 incompatibilities. If any conversion sttements have to be pre-pended to
2251 STMT, they will be added to EXTRA_STMTS. Return true iff the statement was
2252 modified. */
2254 bool
2255 ipa_param_body_adjustments::modify_gimple_stmt (gimple **stmt,
2256 gimple_seq *extra_stmts,
2257 gimple *orig_stmt)
2259 bool modified = false;
2260 tree *t;
2262 switch (gimple_code (*stmt))
2264 case GIMPLE_RETURN:
2265 t = gimple_return_retval_ptr (as_a <greturn *> (*stmt));
2266 if (m_adjustments && m_adjustments->m_skip_return)
2267 *t = NULL_TREE;
2268 else if (*t != NULL_TREE)
2269 modified |= modify_expression (t, true);
2270 break;
2272 case GIMPLE_ASSIGN:
2273 modified |= modify_assignment (*stmt, extra_stmts);
2274 break;
2276 case GIMPLE_CALL:
2277 modified |= modify_call_stmt ((gcall **) stmt, orig_stmt);
2278 break;
2280 case GIMPLE_ASM:
2282 gasm *asm_stmt = as_a <gasm *> (*stmt);
2283 for (unsigned i = 0; i < gimple_asm_ninputs (asm_stmt); i++)
2285 t = &TREE_VALUE (gimple_asm_input_op (asm_stmt, i));
2286 modified |= modify_expression (t, true);
2288 for (unsigned i = 0; i < gimple_asm_noutputs (asm_stmt); i++)
2290 t = &TREE_VALUE (gimple_asm_output_op (asm_stmt, i));
2291 modified |= modify_expression (t, false);
2294 break;
2296 default:
2297 break;
2299 return modified;
2303 /* Traverse body of the current function and perform the requested adjustments
2304 on its statements. Return true iff the CFG has been changed. */
2306 bool
2307 ipa_param_body_adjustments::modify_cfun_body ()
2309 bool cfg_changed = false;
2310 basic_block bb;
2312 FOR_EACH_BB_FN (bb, cfun)
2314 gimple_stmt_iterator gsi;
2316 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2318 gphi *phi = as_a <gphi *> (gsi_stmt (gsi));
2319 tree new_lhs, old_lhs = gimple_phi_result (phi);
2320 new_lhs = replace_removed_params_ssa_names (old_lhs, phi);
2321 if (new_lhs)
2323 gimple_phi_set_result (phi, new_lhs);
2324 release_ssa_name (old_lhs);
2328 gsi = gsi_start_bb (bb);
2329 while (!gsi_end_p (gsi))
2331 gimple *stmt = gsi_stmt (gsi);
2332 gimple *stmt_copy = stmt;
2333 gimple_seq extra_stmts = NULL;
2334 bool modified = modify_gimple_stmt (&stmt, &extra_stmts, NULL);
2335 if (stmt != stmt_copy)
2337 gcc_checking_assert (modified);
2338 gsi_replace (&gsi, stmt, false);
2340 if (!gimple_seq_empty_p (extra_stmts))
2341 gsi_insert_seq_before (&gsi, extra_stmts, GSI_SAME_STMT);
2343 def_operand_p defp;
2344 ssa_op_iter iter;
2345 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF)
2347 tree old_def = DEF_FROM_PTR (defp);
2348 if (tree new_def = replace_removed_params_ssa_names (old_def,
2349 stmt))
2351 SET_DEF (defp, new_def);
2352 release_ssa_name (old_def);
2353 modified = true;
2357 if (modified)
2359 update_stmt (stmt);
2360 if (maybe_clean_eh_stmt (stmt)
2361 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
2362 cfg_changed = true;
2364 gsi_next (&gsi);
2368 return cfg_changed;
2371 /* Call gimple_debug_bind_reset_value on all debug statements describing
2372 gimple register parameters that are being removed or replaced. */
2374 void
2375 ipa_param_body_adjustments::reset_debug_stmts ()
2377 int i, len;
2378 gimple_stmt_iterator *gsip = NULL, gsi;
2380 if (MAY_HAVE_DEBUG_STMTS && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
2382 gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
2383 gsip = &gsi;
2385 len = m_reset_debug_decls.length ();
2386 for (i = 0; i < len; i++)
2388 imm_use_iterator ui;
2389 gimple *stmt;
2390 gdebug *def_temp;
2391 tree name, vexpr, copy = NULL_TREE;
2392 use_operand_p use_p;
2393 tree decl = m_reset_debug_decls[i];
2395 gcc_checking_assert (is_gimple_reg (decl));
2396 name = ssa_default_def (cfun, decl);
2397 vexpr = NULL;
2398 if (name)
2399 FOR_EACH_IMM_USE_STMT (stmt, ui, name)
2401 if (gimple_clobber_p (stmt))
2403 gimple_stmt_iterator cgsi = gsi_for_stmt (stmt);
2404 unlink_stmt_vdef (stmt);
2405 gsi_remove (&cgsi, true);
2406 release_defs (stmt);
2407 continue;
2409 /* All other users must have been removed by function body
2410 modification. */
2411 gcc_assert (is_gimple_debug (stmt));
2412 if (vexpr == NULL && gsip != NULL)
2414 vexpr = build_debug_expr_decl (TREE_TYPE (name));
2415 /* FIXME: Is setting the mode really necessary? */
2416 SET_DECL_MODE (vexpr, DECL_MODE (decl));
2417 def_temp = gimple_build_debug_source_bind (vexpr, decl, NULL);
2418 gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
2420 if (vexpr)
2422 FOR_EACH_IMM_USE_ON_STMT (use_p, ui)
2423 SET_USE (use_p, vexpr);
2425 else
2426 gimple_debug_bind_reset_value (stmt);
2427 update_stmt (stmt);
2429 /* Create a VAR_DECL for debug info purposes. */
2430 if (!DECL_IGNORED_P (decl))
2432 copy = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
2433 VAR_DECL, DECL_NAME (decl),
2434 TREE_TYPE (decl));
2435 if (DECL_PT_UID_SET_P (decl))
2436 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
2437 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
2438 TREE_READONLY (copy) = TREE_READONLY (decl);
2439 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
2440 DECL_NOT_GIMPLE_REG_P (copy) = DECL_NOT_GIMPLE_REG_P (decl);
2441 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
2442 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
2443 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
2444 DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
2445 SET_DECL_RTL (copy, 0);
2446 TREE_USED (copy) = 1;
2447 DECL_CONTEXT (copy) = current_function_decl;
2448 add_local_decl (cfun, copy);
2449 DECL_CHAIN (copy)
2450 = BLOCK_VARS (DECL_INITIAL (current_function_decl));
2451 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = copy;
2453 if (gsip != NULL && copy && target_for_debug_bind (decl))
2455 gcc_assert (TREE_CODE (decl) == PARM_DECL);
2456 if (vexpr)
2457 def_temp = gimple_build_debug_bind (copy, vexpr, NULL);
2458 else
2459 def_temp = gimple_build_debug_source_bind (copy, decl,
2460 NULL);
2461 gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
2466 /* Perform all necessary body changes to change signature, body and debug info
2467 of fun according to adjustments passed at construction. Return true if CFG
2468 was changed in any way. The main entry point for modification of standalone
2469 functions that is not part of IPA clone materialization. */
2471 bool
2472 ipa_param_body_adjustments::perform_cfun_body_modifications ()
2474 bool cfg_changed;
2475 modify_formal_parameters ();
2476 cfg_changed = modify_cfun_body ();
2477 reset_debug_stmts ();
2479 return cfg_changed;
2483 /* If there are any initialization statements that need to be emitted into
2484 the basic block BB right at ther start of the new function, do so. */
2485 void
2486 ipa_param_body_adjustments::append_init_stmts (basic_block bb)
2488 gimple_stmt_iterator si = gsi_last_bb (bb);
2489 while (!m_split_agg_csts_inits.is_empty ())
2490 gsi_insert_after (&si, m_split_agg_csts_inits.pop (), GSI_NEW_STMT);
2493 /* Deallocate summaries which otherwise stay alive until the end of
2494 compilation. */
2496 void
2497 ipa_edge_modifications_finalize ()
2499 if (!ipa_edge_modifications)
2500 return;
2501 delete ipa_edge_modifications;
2502 ipa_edge_modifications = NULL;