Support TI mode and soft float on PA64
[official-gcc.git] / gcc / ipa-param-manipulation.c
blobc84d669521c576070a44cf2e296ee797fae0a24c
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"
50 /* Actual prefixes of different newly synthetized parameters. Keep in sync
51 with IPA_PARAM_PREFIX_* defines. */
53 static const char *ipa_param_prefixes[IPA_PARAM_PREFIX_COUNT]
54 = {"SYNTH",
55 "ISRA",
56 "simd",
57 "mask"};
59 /* Names of parameters for dumping. Keep in sync with enum ipa_parm_op. */
61 static const char *ipa_param_op_names[IPA_PARAM_PREFIX_COUNT]
62 = {"IPA_PARAM_OP_UNDEFINED",
63 "IPA_PARAM_OP_COPY",
64 "IPA_PARAM_OP_NEW",
65 "IPA_PARAM_OP_SPLIT"};
67 /* Structure to hold declarations representing pass-through IPA-SRA splits. In
68 essence, it tells new index for a combination of original index and
69 offset. */
71 struct pass_through_split_map
73 /* Original argument index. */
74 unsigned base_index;
75 /* Offset of the split part in the original argument. */
76 unsigned unit_offset;
77 /* Index of the split part in the call statement - where clone
78 materialization put it. */
79 int new_index;
82 /* Information about some call statements that needs to be conveyed from clone
83 materialization to edge redirection. */
85 class ipa_edge_modification_info
87 public:
88 ipa_edge_modification_info ()
91 /* Mapping of original argument indices to where those arguments sit in the
92 call statement now or to a negative index if they were removed. */
93 auto_vec<int> index_map;
94 /* Information about ISRA replacements put into the call statement at the
95 clone materialization stages. */
96 auto_vec<pass_through_split_map> pass_through_map;
97 /* Necessary adjustment to ipa_param_adjustments::m_always_copy_start when
98 redirecting the call. */
99 int always_copy_delta = 0;
102 /* Class for storing and retrieving summaries about cal statement
103 modifications. */
105 class ipa_edge_modification_sum
106 : public call_summary <ipa_edge_modification_info *>
108 public:
109 ipa_edge_modification_sum (symbol_table *table)
110 : call_summary<ipa_edge_modification_info *> (table)
114 /* Hook that is called by summary when an edge is duplicated. */
116 virtual void duplicate (cgraph_edge *,
117 cgraph_edge *,
118 ipa_edge_modification_info *old_info,
119 ipa_edge_modification_info *new_info)
121 new_info->index_map.safe_splice (old_info->index_map);
122 new_info->pass_through_map.safe_splice (old_info->pass_through_map);
123 new_info->always_copy_delta = old_info->always_copy_delta;
127 /* Call summary to store information about edges which have had their arguments
128 partially modified already. */
130 static ipa_edge_modification_sum *ipa_edge_modifications;
132 /* Fail compilation if CS has any summary associated with it in
133 ipa_edge_modifications. */
135 DEBUG_FUNCTION void
136 ipa_verify_edge_has_no_modifications (cgraph_edge *cs)
138 gcc_assert (!ipa_edge_modifications || !ipa_edge_modifications->get (cs));
141 /* Fill an empty vector ARGS with PARM_DECLs representing formal parameters of
142 FNDECL. The function should not be called during LTO WPA phase except for
143 thunks (or functions with bodies streamed in). */
145 void
146 push_function_arg_decls (vec<tree> *args, tree fndecl)
148 int count;
149 tree parm;
151 /* Safety check that we do not attempt to use the function in WPA, except
152 when the function is a thunk and then we have DECL_ARGUMENTS or when we
153 have already explicitely loaded its body. */
154 gcc_assert (!flag_wpa
155 || DECL_ARGUMENTS (fndecl)
156 || gimple_has_body_p (fndecl));
157 count = 0;
158 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
159 count++;
161 args->reserve_exact (count);
162 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
163 args->quick_push (parm);
166 /* Fill an empty vector TYPES with trees representing formal parameters of
167 function type FNTYPE. */
169 void
170 push_function_arg_types (vec<tree> *types, tree fntype)
172 int count = 0;
173 tree t;
175 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
176 count++;
178 types->reserve_exact (count);
179 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
180 types->quick_push (TREE_VALUE (t));
183 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
184 friendly way, assuming they are meant to be applied to FNDECL. */
186 void
187 ipa_dump_adjusted_parameters (FILE *f,
188 vec<ipa_adjusted_param, va_gc> *adj_params)
190 unsigned i, len = vec_safe_length (adj_params);
191 bool first = true;
193 if (!len)
194 return;
196 fprintf (f, " IPA adjusted parameters: ");
197 for (i = 0; i < len; i++)
199 struct ipa_adjusted_param *apm;
200 apm = &(*adj_params)[i];
202 if (!first)
203 fprintf (f, " ");
204 else
205 first = false;
207 fprintf (f, "%i. %s %s", i, ipa_param_op_names[apm->op],
208 apm->prev_clone_adjustment ? "prev_clone_adjustment " : "");
209 switch (apm->op)
211 case IPA_PARAM_OP_UNDEFINED:
212 break;
214 case IPA_PARAM_OP_COPY:
215 fprintf (f, ", base_index: %u", apm->base_index);
216 fprintf (f, ", prev_clone_index: %u", apm->prev_clone_index);
217 break;
219 case IPA_PARAM_OP_SPLIT:
220 fprintf (f, ", offset: %u", apm->unit_offset);
221 /* fall-through */
222 case IPA_PARAM_OP_NEW:
223 fprintf (f, ", base_index: %u", apm->base_index);
224 fprintf (f, ", prev_clone_index: %u", apm->prev_clone_index);
225 print_node_brief (f, ", type: ", apm->type, 0);
226 print_node_brief (f, ", alias type: ", apm->alias_ptr_type, 0);
227 fprintf (f, " prefix: %s",
228 ipa_param_prefixes[apm->param_prefix_index]);
229 if (apm->reverse)
230 fprintf (f, ", reverse-sso");
231 break;
233 fprintf (f, "\n");
237 /* Fill NEW_TYPES with types of a function after its current OTYPES have been
238 modified as described in ADJ_PARAMS. When USE_PREV_INDICES is true, use
239 prev_clone_index from ADJ_PARAMS as opposed to base_index when the parameter
240 is false. */
242 static void
243 fill_vector_of_new_param_types (vec<tree> *new_types, vec<tree> *otypes,
244 vec<ipa_adjusted_param, va_gc> *adj_params,
245 bool use_prev_indices)
247 unsigned adj_len = vec_safe_length (adj_params);
248 new_types->reserve_exact (adj_len);
249 for (unsigned i = 0; i < adj_len ; i++)
251 ipa_adjusted_param *apm = &(*adj_params)[i];
252 if (apm->op == IPA_PARAM_OP_COPY)
254 unsigned index
255 = use_prev_indices ? apm->prev_clone_index : apm->base_index;
256 /* The following needs to be handled gracefully because of type
257 mismatches. This happens with LTO but apparently also in Fortran
258 with -fcoarray=lib -O2 -lcaf_single -latomic. */
259 if (index >= otypes->length ())
260 continue;
261 new_types->quick_push ((*otypes)[index]);
263 else if (apm->op == IPA_PARAM_OP_NEW
264 || apm->op == IPA_PARAM_OP_SPLIT)
266 tree ntype = apm->type;
267 if (is_gimple_reg_type (ntype)
268 && TYPE_MODE (ntype) != BLKmode)
270 unsigned malign = GET_MODE_ALIGNMENT (TYPE_MODE (ntype));
271 if (TYPE_ALIGN (ntype) != malign)
272 ntype = build_aligned_type (ntype, malign);
274 new_types->quick_push (ntype);
276 else
277 gcc_unreachable ();
281 /* Build and return a function type just like ORIG_TYPE but with parameter
282 types given in NEW_PARAM_TYPES - which can be NULL if, but only if,
283 ORIG_TYPE itself has NULL TREE_ARG_TYPEs. If METHOD2FUNC is true, also make
284 it a FUNCTION_TYPE instead of FUNCTION_TYPE. */
286 static tree
287 build_adjusted_function_type (tree orig_type, vec<tree> *new_param_types,
288 bool method2func, bool skip_return)
290 tree new_arg_types = NULL;
291 if (TYPE_ARG_TYPES (orig_type))
293 gcc_checking_assert (new_param_types);
294 bool last_parm_void = (TREE_VALUE (tree_last (TYPE_ARG_TYPES (orig_type)))
295 == void_type_node);
296 unsigned len = new_param_types->length ();
297 for (unsigned i = 0; i < len; i++)
298 new_arg_types = tree_cons (NULL_TREE, (*new_param_types)[i],
299 new_arg_types);
301 tree new_reversed = nreverse (new_arg_types);
302 if (last_parm_void)
304 if (new_reversed)
305 TREE_CHAIN (new_arg_types) = void_list_node;
306 else
307 new_reversed = void_list_node;
309 new_arg_types = new_reversed;
312 /* Use build_distinct_type_copy to preserve as much as possible from original
313 type (debug info, attribute lists etc.). The one exception is
314 METHOD_TYPEs which must have THIS argument and when we are asked to remove
315 it, we need to build new FUNCTION_TYPE instead. */
316 tree new_type = NULL;
317 if (method2func)
319 tree ret_type;
320 if (skip_return)
321 ret_type = void_type_node;
322 else
323 ret_type = TREE_TYPE (orig_type);
325 new_type
326 = build_distinct_type_copy (build_function_type (ret_type,
327 new_arg_types));
328 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
330 else
332 new_type = build_distinct_type_copy (orig_type);
333 TYPE_ARG_TYPES (new_type) = new_arg_types;
334 if (skip_return)
335 TREE_TYPE (new_type) = void_type_node;
338 return new_type;
341 /* Return the maximum index in any IPA_PARAM_OP_COPY adjustment or -1 if there
342 is none. */
345 ipa_param_adjustments::get_max_base_index ()
347 unsigned adj_len = vec_safe_length (m_adj_params);
348 int max_index = -1;
349 for (unsigned i = 0; i < adj_len ; i++)
351 ipa_adjusted_param *apm = &(*m_adj_params)[i];
352 if (apm->op == IPA_PARAM_OP_COPY
353 && max_index < apm->base_index)
354 max_index = apm->base_index;
356 return max_index;
360 /* Fill SURVIVING_PARAMS with an array of bools where each one says whether a
361 parameter that originally was at that position still survives in the given
362 clone or is removed/replaced. If the final array is smaller than an index
363 of an original parameter, that parameter also did not survive. That a
364 parameter survives does not mean it has the same index as before. */
366 void
367 ipa_param_adjustments::get_surviving_params (vec<bool> *surviving_params)
369 unsigned adj_len = vec_safe_length (m_adj_params);
370 int max_index = get_max_base_index ();
372 if (max_index < 0)
373 return;
374 surviving_params->reserve_exact (max_index + 1);
375 surviving_params->quick_grow_cleared (max_index + 1);
376 for (unsigned i = 0; i < adj_len ; i++)
378 ipa_adjusted_param *apm = &(*m_adj_params)[i];
379 if (apm->op == IPA_PARAM_OP_COPY)
380 (*surviving_params)[apm->base_index] = true;
384 /* Fill NEW_INDICES with new indices of each surviving parameter or -1 for
385 those which do not survive. Any parameter outside of lenght of the vector
386 does not survive. There is currently no support for a parameter to be
387 copied to two distinct new parameters. */
389 void
390 ipa_param_adjustments::get_updated_indices (vec<int> *new_indices)
392 unsigned adj_len = vec_safe_length (m_adj_params);
393 int max_index = get_max_base_index ();
395 if (max_index < 0)
396 return;
397 unsigned res_len = max_index + 1;
398 new_indices->reserve_exact (res_len);
399 for (unsigned i = 0; i < res_len ; i++)
400 new_indices->quick_push (-1);
401 for (unsigned i = 0; i < adj_len ; i++)
403 ipa_adjusted_param *apm = &(*m_adj_params)[i];
404 if (apm->op == IPA_PARAM_OP_COPY)
405 (*new_indices)[apm->base_index] = i;
409 /* Return the original index for the given new parameter index. Return a
410 negative number if not available. */
413 ipa_param_adjustments::get_original_index (int newidx)
415 const ipa_adjusted_param *adj = &(*m_adj_params)[newidx];
416 if (adj->op != IPA_PARAM_OP_COPY)
417 return -1;
418 return adj->base_index;
421 /* Return true if the first parameter (assuming there was one) survives the
422 transformation intact and remains the first one. */
424 bool
425 ipa_param_adjustments::first_param_intact_p ()
427 return (!vec_safe_is_empty (m_adj_params)
428 && (*m_adj_params)[0].op == IPA_PARAM_OP_COPY
429 && (*m_adj_params)[0].base_index == 0);
432 /* Return true if we have to change what has formerly been a method into a
433 function. */
435 bool
436 ipa_param_adjustments::method2func_p (tree orig_type)
438 return ((TREE_CODE (orig_type) == METHOD_TYPE) && !first_param_intact_p ());
441 /* Given function type OLD_TYPE, return a new type derived from it after
442 performing all atored modifications. TYPE_ORIGINAL_P should be true when
443 OLD_TYPE refers to the type before any IPA transformations, as opposed to a
444 type that can be an intermediate one in between various IPA
445 transformations. */
447 tree
448 ipa_param_adjustments::build_new_function_type (tree old_type,
449 bool type_original_p)
451 auto_vec<tree,16> new_param_types, *new_param_types_p;
452 if (prototype_p (old_type))
454 auto_vec<tree, 16> otypes;
455 push_function_arg_types (&otypes, old_type);
456 fill_vector_of_new_param_types (&new_param_types, &otypes, m_adj_params,
457 !type_original_p);
458 new_param_types_p = &new_param_types;
460 else
461 new_param_types_p = NULL;
463 return build_adjusted_function_type (old_type, new_param_types_p,
464 method2func_p (old_type), m_skip_return);
467 /* Build variant of function decl ORIG_DECL which has no return value if
468 M_SKIP_RETURN is true and, if ORIG_DECL's types or parameters is known, has
469 this type adjusted as indicated in M_ADJ_PARAMS. Arguments from
470 DECL_ARGUMENTS list are not processed now, since they are linked by
471 TREE_CHAIN directly and not accessible in LTO during WPA. The caller is
472 responsible for eliminating them when clones are properly materialized. */
474 tree
475 ipa_param_adjustments::adjust_decl (tree orig_decl)
477 tree new_decl = copy_node (orig_decl);
478 tree orig_type = TREE_TYPE (orig_decl);
479 if (prototype_p (orig_type)
480 || (m_skip_return && !VOID_TYPE_P (TREE_TYPE (orig_type))))
482 tree new_type = build_new_function_type (orig_type, false);
483 TREE_TYPE (new_decl) = new_type;
485 if (method2func_p (orig_type))
486 DECL_VINDEX (new_decl) = NULL_TREE;
488 /* When signature changes, we need to clear builtin info. */
489 if (fndecl_built_in_p (new_decl))
490 set_decl_built_in_function (new_decl, NOT_BUILT_IN, 0);
492 DECL_VIRTUAL_P (new_decl) = 0;
493 DECL_LANG_SPECIFIC (new_decl) = NULL;
495 /* Drop MALLOC attribute for a void function. */
496 if (m_skip_return)
497 DECL_IS_MALLOC (new_decl) = 0;
499 return new_decl;
502 /* Wrapper around get_base_ref_and_offset for cases interesting for IPA-SRA
503 transformations. Return true if EXPR has an interesting form and fill in
504 *BASE_P and *UNIT_OFFSET_P with the appropriate info. */
506 static bool
507 isra_get_ref_base_and_offset (tree expr, tree *base_p, unsigned *unit_offset_p)
509 HOST_WIDE_INT offset, size;
510 bool reverse;
511 tree base
512 = get_ref_base_and_extent_hwi (expr, &offset, &size, &reverse);
513 if (!base || size < 0)
514 return false;
516 if ((offset % BITS_PER_UNIT) != 0)
517 return false;
519 if (TREE_CODE (base) == MEM_REF)
521 poly_int64 plmoff = mem_ref_offset (base).force_shwi ();
522 HOST_WIDE_INT moff;
523 bool is_cst = plmoff.is_constant (&moff);
524 if (!is_cst)
525 return false;
526 offset += moff * BITS_PER_UNIT;
527 base = TREE_OPERAND (base, 0);
530 if (offset < 0 || (offset / BITS_PER_UNIT) > UINT_MAX)
531 return false;
533 *base_p = base;
534 *unit_offset_p = offset / BITS_PER_UNIT;
535 return true;
538 /* Modify actual arguments of a function call in statement currently belonging
539 to CS, and make it call CS->callee->decl. Return the new statement that
540 replaced the old one. When invoked, cfun and current_function_decl have to
541 be set to the caller. */
543 gcall *
544 ipa_param_adjustments::modify_call (cgraph_edge *cs,
545 bool update_references)
547 gcall *stmt = cs->call_stmt;
548 tree callee_decl = cs->callee->decl;
550 ipa_edge_modification_info *mod_info
551 = ipa_edge_modifications ? ipa_edge_modifications->get (cs) : NULL;
552 if (mod_info && symtab->dump_file)
554 fprintf (symtab->dump_file, "Information about pre-exiting "
555 "modifications.\n Index map:");
556 unsigned idx_len = mod_info->index_map.length ();
557 for (unsigned i = 0; i < idx_len; i++)
558 fprintf (symtab->dump_file, " %i", mod_info->index_map[i]);
559 fprintf (symtab->dump_file, "\n Pass-through split map: ");
560 unsigned ptm_len = mod_info->pass_through_map.length ();
561 for (unsigned i = 0; i < ptm_len; i++)
562 fprintf (symtab->dump_file,
563 " (base_index: %u, offset: %u, new_index: %i)",
564 mod_info->pass_through_map[i].base_index,
565 mod_info->pass_through_map[i].unit_offset,
566 mod_info->pass_through_map[i].new_index);
567 fprintf (symtab->dump_file, "\n Always-copy delta: %i\n",
568 mod_info->always_copy_delta);
571 unsigned len = vec_safe_length (m_adj_params);
572 auto_vec<tree, 16> vargs (len);
573 unsigned old_nargs = gimple_call_num_args (stmt);
574 unsigned orig_nargs = mod_info ? mod_info->index_map.length () : old_nargs;
575 auto_vec<bool, 16> kept (old_nargs);
576 kept.quick_grow_cleared (old_nargs);
578 cgraph_node *current_node = cgraph_node::get (current_function_decl);
579 if (update_references)
580 current_node->remove_stmt_references (stmt);
582 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
583 gimple_stmt_iterator prev_gsi = gsi;
584 gsi_prev (&prev_gsi);
585 for (unsigned i = 0; i < len; i++)
587 ipa_adjusted_param *apm = &(*m_adj_params)[i];
588 if (apm->op == IPA_PARAM_OP_COPY)
590 int index = apm->base_index;
591 if ((unsigned) index >= orig_nargs)
592 /* Can happen if the original call has argument mismatch,
593 ignore. */
594 continue;
595 if (mod_info)
597 index = mod_info->index_map[apm->base_index];
598 gcc_assert (index >= 0);
601 tree arg = gimple_call_arg (stmt, index);
603 vargs.quick_push (arg);
604 kept[index] = true;
605 continue;
608 /* At the moment the only user of IPA_PARAM_OP_NEW modifies calls itself.
609 If we ever want to support it during WPA IPA stage, we'll need a
610 mechanism to call into the IPA passes that introduced them. Currently
611 we simply mandate that IPA infrastructure understands all argument
612 modifications. Remember, edge redirection/modification is done only
613 once, not in steps for each pass modifying the callee like clone
614 materialization. */
615 gcc_assert (apm->op == IPA_PARAM_OP_SPLIT);
617 /* We have to handle pass-through changes differently using the map
618 clone materialziation might have left behind. */
619 tree repl = NULL_TREE;
620 unsigned ptm_len = mod_info ? mod_info->pass_through_map.length () : 0;
621 for (unsigned j = 0; j < ptm_len; j++)
622 if (mod_info->pass_through_map[j].base_index == apm->base_index
623 && mod_info->pass_through_map[j].unit_offset == apm->unit_offset)
625 int repl_idx = mod_info->pass_through_map[j].new_index;
626 gcc_assert (repl_idx >= 0);
627 repl = gimple_call_arg (stmt, repl_idx);
628 break;
630 if (repl)
632 vargs.quick_push (repl);
633 continue;
636 int index = apm->base_index;
637 if ((unsigned) index >= orig_nargs)
638 /* Can happen if the original call has argument mismatch, ignore. */
639 continue;
640 if (mod_info)
642 index = mod_info->index_map[apm->base_index];
643 gcc_assert (index >= 0);
645 tree base = gimple_call_arg (stmt, index);
647 /* We create a new parameter out of the value of the old one, we can
648 do the following kind of transformations:
650 - A scalar passed by reference, potentially as a part of a larger
651 aggregate, is converted to a scalar passed by value.
653 - A part of an aggregate is passed instead of the whole aggregate. */
655 location_t loc = gimple_location (stmt);
656 tree off;
657 bool deref_base = false;
658 unsigned int deref_align = 0;
659 if (TREE_CODE (base) != ADDR_EXPR
660 && is_gimple_reg_type (TREE_TYPE (base)))
662 /* Detect type mismatches in calls in invalid programs and make a
663 poor attempt to gracefully convert them so that we don't ICE. */
664 if (!POINTER_TYPE_P (TREE_TYPE (base)))
665 base = force_value_to_type (ptr_type_node, base);
667 off = build_int_cst (apm->alias_ptr_type, apm->unit_offset);
669 else
671 bool addrof;
672 if (TREE_CODE (base) == ADDR_EXPR)
674 base = TREE_OPERAND (base, 0);
675 addrof = true;
677 else
678 addrof = false;
680 tree prev_base = base;
681 poly_int64 base_offset;
682 base = get_addr_base_and_unit_offset (base, &base_offset);
684 /* Aggregate arguments can have non-invariant addresses. */
685 if (!base)
687 base = build_fold_addr_expr (prev_base);
688 off = build_int_cst (apm->alias_ptr_type, apm->unit_offset);
690 else if (TREE_CODE (base) == MEM_REF)
692 if (!addrof)
694 deref_base = true;
695 deref_align = TYPE_ALIGN (TREE_TYPE (base));
697 off = build_int_cst (apm->alias_ptr_type,
698 base_offset + apm->unit_offset);
699 off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
700 off);
701 base = TREE_OPERAND (base, 0);
703 else
705 off = build_int_cst (apm->alias_ptr_type,
706 base_offset + apm->unit_offset);
707 base = build_fold_addr_expr (base);
711 tree type = apm->type;
712 unsigned int align;
713 unsigned HOST_WIDE_INT misalign;
715 if (deref_base)
717 align = deref_align;
718 misalign = 0;
720 else
722 get_pointer_alignment_1 (base, &align, &misalign);
723 /* All users must make sure that we can be optimistic when it
724 comes to alignment in this case (by inspecting the final users
725 of these new parameters). */
726 if (TYPE_ALIGN (type) > align)
727 align = TYPE_ALIGN (type);
729 misalign
730 += (offset_int::from (wi::to_wide (off), SIGNED).to_short_addr ()
731 * BITS_PER_UNIT);
732 misalign = misalign & (align - 1);
733 if (misalign != 0)
734 align = least_bit_hwi (misalign);
735 if (align < TYPE_ALIGN (type))
736 type = build_aligned_type (type, align);
737 base = force_gimple_operand_gsi (&gsi, base,
738 true, NULL, true, GSI_SAME_STMT);
739 tree expr = fold_build2_loc (loc, MEM_REF, type, base, off);
740 REF_REVERSE_STORAGE_ORDER (expr) = apm->reverse;
741 /* If expr is not a valid gimple call argument emit
742 a load into a temporary. */
743 if (is_gimple_reg_type (TREE_TYPE (expr)))
745 gimple *tem = gimple_build_assign (NULL_TREE, expr);
746 if (gimple_in_ssa_p (cfun))
748 gimple_set_vuse (tem, gimple_vuse (stmt));
749 expr = make_ssa_name (TREE_TYPE (expr), tem);
751 else
752 expr = create_tmp_reg (TREE_TYPE (expr));
753 gimple_assign_set_lhs (tem, expr);
754 gsi_insert_before (&gsi, tem, GSI_SAME_STMT);
756 vargs.quick_push (expr);
759 if (m_always_copy_start >= 0)
761 int always_copy_start = m_always_copy_start;
762 if (mod_info)
764 always_copy_start += mod_info->always_copy_delta;
765 gcc_assert (always_copy_start >= 0);
767 for (unsigned i = always_copy_start; i < old_nargs; i++)
768 vargs.safe_push (gimple_call_arg (stmt, i));
771 /* For optimized away parameters, add on the caller side
772 before the call
773 DEBUG D#X => parm_Y(D)
774 stmts and associate D#X with parm in decl_debug_args_lookup
775 vector to say for debug info that if parameter parm had been passed,
776 it would have value parm_Y(D). */
777 tree old_decl = gimple_call_fndecl (stmt);
778 if (MAY_HAVE_DEBUG_BIND_STMTS && old_decl && callee_decl)
780 vec<tree, va_gc> **debug_args = NULL;
781 unsigned i = 0;
782 cgraph_node *callee_node = cgraph_node::get (callee_decl);
784 /* FIXME: we don't seem to be able to insert debug args before clone
785 is materialized. Materializing them early leads to extra memory
786 use. */
787 if (callee_node->clone_of)
788 callee_node->get_untransformed_body ();
789 for (tree old_parm = DECL_ARGUMENTS (old_decl);
790 old_parm && i < old_nargs && ((int) i) < m_always_copy_start;
791 old_parm = DECL_CHAIN (old_parm), i++)
793 if (!is_gimple_reg (old_parm) || kept[i])
794 continue;
795 tree arg;
796 if (mod_info)
798 if (mod_info->index_map[i] < 0)
799 continue;
800 arg = gimple_call_arg (stmt, mod_info->index_map[i]);
802 else
803 arg = gimple_call_arg (stmt, i);
805 tree origin = DECL_ORIGIN (old_parm);
806 if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
808 if (!fold_convertible_p (TREE_TYPE (origin), arg))
809 continue;
810 tree rhs1;
811 if (TREE_CODE (arg) == SSA_NAME
812 && gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg))
813 && (rhs1
814 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (arg)))
815 && useless_type_conversion_p (TREE_TYPE (origin),
816 TREE_TYPE (rhs1)))
817 arg = rhs1;
818 else
819 arg = fold_convert_loc (gimple_location (stmt),
820 TREE_TYPE (origin), arg);
822 if (debug_args == NULL)
823 debug_args = decl_debug_args_insert (callee_decl);
824 unsigned int ix;
825 tree ddecl = NULL_TREE;
826 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl); ix += 2)
827 if (ddecl == origin)
829 ddecl = (**debug_args)[ix + 1];
830 break;
832 if (ddecl == NULL)
834 ddecl = make_node (DEBUG_EXPR_DECL);
835 DECL_ARTIFICIAL (ddecl) = 1;
836 TREE_TYPE (ddecl) = TREE_TYPE (origin);
837 SET_DECL_MODE (ddecl, DECL_MODE (origin));
839 vec_safe_push (*debug_args, origin);
840 vec_safe_push (*debug_args, ddecl);
842 gimple *def_temp = gimple_build_debug_bind (ddecl,
843 unshare_expr (arg), stmt);
844 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
848 if (dump_file && (dump_flags & TDF_DETAILS))
850 fprintf (dump_file, "replacing stmt:");
851 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0);
854 gcall *new_stmt = gimple_build_call_vec (callee_decl, vargs);
856 tree ssa_to_remove = NULL;
857 if (tree lhs = gimple_call_lhs (stmt))
859 if (!m_skip_return)
860 gimple_call_set_lhs (new_stmt, lhs);
861 else if (TREE_CODE (lhs) == SSA_NAME)
863 /* LHS should now by a default-def SSA. Unfortunately default-def
864 SSA_NAMEs need a backing variable (or at least some code examining
865 SSAs assumes it is non-NULL). So we either have to re-use the
866 decl we have at hand or introdice a new one. */
867 tree repl = create_tmp_var (TREE_TYPE (lhs), "removed_return");
868 repl = get_or_create_ssa_default_def (cfun, repl);
869 SSA_NAME_IS_DEFAULT_DEF (repl) = true;
870 imm_use_iterator ui;
871 use_operand_p use_p;
872 gimple *using_stmt;
873 FOR_EACH_IMM_USE_STMT (using_stmt, ui, lhs)
875 FOR_EACH_IMM_USE_ON_STMT (use_p, ui)
877 SET_USE (use_p, repl);
879 update_stmt (using_stmt);
881 ssa_to_remove = lhs;
885 gimple_set_block (new_stmt, gimple_block (stmt));
886 if (gimple_has_location (stmt))
887 gimple_set_location (new_stmt, gimple_location (stmt));
888 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
889 gimple_call_copy_flags (new_stmt, stmt);
890 if (gimple_in_ssa_p (cfun))
891 gimple_move_vops (new_stmt, stmt);
893 if (dump_file && (dump_flags & TDF_DETAILS))
895 fprintf (dump_file, "with stmt:");
896 print_gimple_stmt (dump_file, new_stmt, 0);
897 fprintf (dump_file, "\n");
899 gsi_replace (&gsi, new_stmt, true);
900 if (ssa_to_remove)
901 release_ssa_name (ssa_to_remove);
902 if (update_references)
905 current_node->record_stmt_references (gsi_stmt (gsi));
906 gsi_prev (&gsi);
908 while (gsi_stmt (gsi) != gsi_stmt (prev_gsi));
910 if (mod_info)
911 ipa_edge_modifications->remove (cs);
912 return new_stmt;
915 /* Dump information contained in the object in textual form to F. */
917 void
918 ipa_param_adjustments::dump (FILE *f)
920 fprintf (f, " m_always_copy_start: %i\n", m_always_copy_start);
921 ipa_dump_adjusted_parameters (f, m_adj_params);
922 if (m_skip_return)
923 fprintf (f, " Will SKIP return.\n");
926 /* Dump information contained in the object in textual form to stderr. */
928 void
929 ipa_param_adjustments::debug ()
931 dump (stderr);
934 /* Register that REPLACEMENT should replace parameter described in APM. */
936 void
937 ipa_param_body_adjustments::register_replacement (ipa_adjusted_param *apm,
938 tree replacement)
940 gcc_checking_assert (apm->op == IPA_PARAM_OP_SPLIT
941 || apm->op == IPA_PARAM_OP_NEW);
942 gcc_checking_assert (!apm->prev_clone_adjustment);
943 ipa_param_body_replacement psr;
944 psr.base = m_oparms[apm->prev_clone_index];
945 psr.repl = replacement;
946 psr.dummy = NULL_TREE;
947 psr.unit_offset = apm->unit_offset;
948 m_replacements.safe_push (psr);
951 /* Copy or not, as appropriate given m_id and decl context, a pre-existing
952 PARM_DECL T so that it can be included in the parameters of the modified
953 function. */
955 tree
956 ipa_param_body_adjustments::carry_over_param (tree t)
958 tree new_parm;
959 if (m_id)
961 new_parm = remap_decl (t, m_id);
962 if (TREE_CODE (new_parm) != PARM_DECL)
963 new_parm = m_id->copy_decl (t, m_id);
965 else if (DECL_CONTEXT (t) != m_fndecl)
967 new_parm = copy_node (t);
968 DECL_CONTEXT (new_parm) = m_fndecl;
970 else
971 new_parm = t;
972 return new_parm;
975 /* Populate m_dead_stmts given that DEAD_PARAM is going to be removed without
976 any replacement or splitting. REPL is the replacement VAR_SECL to base any
977 remaining uses of a removed parameter on. Push all removed SSA names that
978 are used within debug statements to DEBUGSTACK. */
980 void
981 ipa_param_body_adjustments::mark_dead_statements (tree dead_param,
982 vec<tree> *debugstack)
984 /* Current IPA analyses which remove unused parameters never remove a
985 non-gimple register ones which have any use except as parameters in other
986 calls, so we can safely leve them as they are. */
987 if (!is_gimple_reg (dead_param))
988 return;
989 tree parm_ddef = ssa_default_def (m_id->src_cfun, dead_param);
990 if (!parm_ddef || has_zero_uses (parm_ddef))
991 return;
993 auto_vec<tree, 4> stack;
994 hash_set<tree> used_in_debug;
995 m_dead_ssas.add (parm_ddef);
996 stack.safe_push (parm_ddef);
997 while (!stack.is_empty ())
999 imm_use_iterator imm_iter;
1000 use_operand_p use_p;
1001 tree t = stack.pop ();
1003 insert_decl_map (m_id, t, error_mark_node);
1004 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, t)
1006 gimple *stmt = USE_STMT (use_p);
1008 /* Calls containing dead arguments cannot be deleted,
1009 modify_call_stmt will instead remove just the argument later on.
1010 If isra_track_scalar_value_uses in ipa-sra.c is extended to look
1011 through const functions, we will need to do so here too. */
1012 if (is_gimple_call (stmt)
1013 || (m_id->blocks_to_copy
1014 && !bitmap_bit_p (m_id->blocks_to_copy,
1015 gimple_bb (stmt)->index)))
1016 continue;
1018 if (is_gimple_debug (stmt))
1020 m_dead_stmts.add (stmt);
1021 gcc_assert (gimple_debug_bind_p (stmt));
1022 if (!used_in_debug.contains (t))
1024 used_in_debug.add (t);
1025 debugstack->safe_push (t);
1028 else if (gimple_code (stmt) == GIMPLE_PHI)
1030 gphi *phi = as_a <gphi *> (stmt);
1031 int ix = PHI_ARG_INDEX_FROM_USE (use_p);
1033 if (!m_id->blocks_to_copy
1034 || bitmap_bit_p (m_id->blocks_to_copy,
1035 gimple_phi_arg_edge (phi, ix)->src->index))
1037 m_dead_stmts.add (phi);
1038 tree res = gimple_phi_result (phi);
1039 if (!m_dead_ssas.add (res))
1040 stack.safe_push (res);
1043 else if (is_gimple_assign (stmt))
1045 m_dead_stmts.add (stmt);
1046 if (!gimple_clobber_p (stmt))
1048 tree lhs = gimple_assign_lhs (stmt);
1049 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
1050 if (!m_dead_ssas.add (lhs))
1051 stack.safe_push (lhs);
1054 else
1055 /* IPA-SRA does not analyze other types of statements. */
1056 gcc_unreachable ();
1060 if (!MAY_HAVE_DEBUG_STMTS)
1062 gcc_assert (debugstack->is_empty ());
1063 return;
1066 tree dp_ddecl = make_node (DEBUG_EXPR_DECL);
1067 DECL_ARTIFICIAL (dp_ddecl) = 1;
1068 TREE_TYPE (dp_ddecl) = TREE_TYPE (dead_param);
1069 SET_DECL_MODE (dp_ddecl, DECL_MODE (dead_param));
1070 m_dead_ssa_debug_equiv.put (parm_ddef, dp_ddecl);
1073 /* Callback to walk_tree. If REMAP is an SSA_NAME that is present in hash_map
1074 passed in DATA, replace it with unshared version of what it was mapped
1075 to. */
1077 static tree
1078 replace_with_mapped_expr (tree *remap, int *walk_subtrees, void *data)
1080 if (TYPE_P (*remap))
1082 *walk_subtrees = 0;
1083 return 0;
1085 if (TREE_CODE (*remap) != SSA_NAME)
1086 return 0;
1088 *walk_subtrees = 0;
1090 hash_map<tree, tree> *equivs = (hash_map<tree, tree> *) data;
1091 if (tree *p = equivs->get (*remap))
1092 *remap = unshare_expr (*p);
1093 return 0;
1096 /* Replace all occurances of SSAs in m_dead_ssa_debug_equiv in t with what they
1097 are mapped to. */
1099 void
1100 ipa_param_body_adjustments::remap_with_debug_expressions (tree *t)
1102 /* If *t is an SSA_NAME which should have its debug statements reset, it is
1103 mapped to NULL in the hash_map. We need to handle that case separately or
1104 otherwise the walker would segfault. No expression that is more
1105 complicated than that can have its operands mapped to NULL. */
1106 if (TREE_CODE (*t) == SSA_NAME)
1108 if (tree *p = m_dead_ssa_debug_equiv.get (*t))
1109 *t = *p;
1111 else
1112 walk_tree (t, replace_with_mapped_expr, &m_dead_ssa_debug_equiv, NULL);
1115 /* For an SSA_NAME DEAD_SSA which is about to be DCEd because it is based on a
1116 useless parameter, prepare an expression that should represent it in
1117 debug_binds in the cloned function and add a mapping from DEAD_SSA to
1118 m_dead_ssa_debug_equiv. That mapping is to NULL when the associated
1119 debug_statement has to be reset instead. In such case return false,
1120 ottherwise return true. If DEAD_SSA comes from a basic block which is not
1121 about to be copied, ignore it and return true. */
1123 bool
1124 ipa_param_body_adjustments::prepare_debug_expressions (tree dead_ssa)
1126 gcc_checking_assert (m_dead_ssas.contains (dead_ssa));
1127 if (tree *d = m_dead_ssa_debug_equiv.get (dead_ssa))
1128 return (*d != NULL_TREE);
1130 gcc_assert (!SSA_NAME_IS_DEFAULT_DEF (dead_ssa));
1131 gimple *def = SSA_NAME_DEF_STMT (dead_ssa);
1132 if (m_id->blocks_to_copy
1133 && !bitmap_bit_p (m_id->blocks_to_copy, gimple_bb (def)->index))
1134 return true;
1136 if (gimple_code (def) == GIMPLE_PHI)
1138 /* In theory, we could ignore all SSAs coming from BBs not in
1139 m_id->blocks_to_copy but at the time of the writing this code that
1140 should never really be the case because only fnsplit uses that bitmap,
1141 so don't bother. */
1142 tree value = degenerate_phi_result (as_a <gphi *> (def));
1143 if (!value
1144 || (m_dead_ssas.contains (value)
1145 && !prepare_debug_expressions (value)))
1147 m_dead_ssa_debug_equiv.put (dead_ssa, NULL_TREE);
1148 return false;
1151 gcc_assert (TREE_CODE (value) == SSA_NAME);
1152 tree *d = m_dead_ssa_debug_equiv.get (value);
1153 m_dead_ssa_debug_equiv.put (dead_ssa, *d);
1154 return true;
1157 bool lost = false;
1158 use_operand_p use_p;
1159 ssa_op_iter oi;
1160 FOR_EACH_PHI_OR_STMT_USE (use_p, def, oi, SSA_OP_USE)
1162 tree use = USE_FROM_PTR (use_p);
1163 if (m_dead_ssas.contains (use)
1164 && !prepare_debug_expressions (use))
1166 lost = true;
1167 break;
1171 if (lost)
1173 m_dead_ssa_debug_equiv.put (dead_ssa, NULL_TREE);
1174 return false;
1177 if (is_gimple_assign (def))
1179 gcc_assert (!gimple_clobber_p (def));
1180 if (gimple_assign_copy_p (def)
1181 && TREE_CODE (gimple_assign_rhs1 (def)) == SSA_NAME)
1183 tree *d = m_dead_ssa_debug_equiv.get (gimple_assign_rhs1 (def));
1184 m_dead_ssa_debug_equiv.put (dead_ssa, *d);
1185 return (*d != NULL_TREE);
1188 tree val = gimple_assign_rhs_to_tree (def);
1189 SET_EXPR_LOCATION (val, UNKNOWN_LOCATION);
1190 remap_with_debug_expressions (&val);
1192 tree vexpr = make_node (DEBUG_EXPR_DECL);
1193 DECL_ARTIFICIAL (vexpr) = 1;
1194 TREE_TYPE (vexpr) = TREE_TYPE (val);
1195 SET_DECL_MODE (vexpr, TYPE_MODE (TREE_TYPE (val)));
1196 m_dead_stmt_debug_equiv.put (def, val);
1197 m_dead_ssa_debug_equiv.put (dead_ssa, vexpr);
1198 return true;
1200 else
1201 gcc_unreachable ();
1204 /* Common initialization performed by all ipa_param_body_adjustments
1205 constructors. OLD_FNDECL is the declaration we take original arguments
1206 from, (it may be the same as M_FNDECL). VARS, if non-NULL, is a pointer to
1207 a chained list of new local variables. TREE_MAP is the IPA-CP produced
1208 mapping of trees to constants.
1210 The function is rather long but it really onlu initializes all data members
1211 of the class. It creates new param DECLs, finds their new types, */
1213 void
1214 ipa_param_body_adjustments::common_initialization (tree old_fndecl,
1215 tree *vars,
1216 vec<ipa_replace_map *,
1217 va_gc> *tree_map)
1219 push_function_arg_decls (&m_oparms, old_fndecl);
1220 auto_vec<tree,16> otypes;
1221 if (TYPE_ARG_TYPES (TREE_TYPE (old_fndecl)) != NULL_TREE)
1222 push_function_arg_types (&otypes, TREE_TYPE (old_fndecl));
1223 else
1225 auto_vec<tree,16> oparms;
1226 push_function_arg_decls (&oparms, old_fndecl);
1227 unsigned ocount = oparms.length ();
1228 otypes.reserve_exact (ocount);
1229 for (unsigned i = 0; i < ocount; i++)
1230 otypes.quick_push (TREE_TYPE (oparms[i]));
1232 fill_vector_of_new_param_types (&m_new_types, &otypes, m_adj_params, true);
1234 auto_vec<bool, 16> kept;
1235 kept.reserve_exact (m_oparms.length ());
1236 kept.quick_grow_cleared (m_oparms.length ());
1237 auto_vec<bool, 16> split;
1238 split.reserve_exact (m_oparms.length ());
1239 split.quick_grow_cleared (m_oparms.length ());
1241 unsigned adj_len = vec_safe_length (m_adj_params);
1242 m_method2func = ((TREE_CODE (TREE_TYPE (m_fndecl)) == METHOD_TYPE)
1243 && (adj_len == 0
1244 || (*m_adj_params)[0].op != IPA_PARAM_OP_COPY
1245 || (*m_adj_params)[0].base_index != 0));
1247 /* The main job of the this function is to go over the vector of adjusted
1248 parameters and create declarations or find corresponding old ones and push
1249 them to m_new_decls. For IPA-SRA replacements it also creates
1250 corresponding m_id->dst_node->clone.performed_splits entries. */
1252 m_new_decls.reserve_exact (adj_len);
1253 for (unsigned i = 0; i < adj_len ; i++)
1255 ipa_adjusted_param *apm = &(*m_adj_params)[i];
1256 unsigned prev_index = apm->prev_clone_index;
1257 tree new_parm;
1258 if (apm->op == IPA_PARAM_OP_COPY
1259 || apm->prev_clone_adjustment)
1261 kept[prev_index] = true;
1262 new_parm = carry_over_param (m_oparms[prev_index]);
1263 m_new_decls.quick_push (new_parm);
1265 else if (apm->op == IPA_PARAM_OP_NEW
1266 || apm->op == IPA_PARAM_OP_SPLIT)
1268 tree new_type = m_new_types[i];
1269 gcc_checking_assert (new_type);
1270 new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
1271 new_type);
1272 const char *prefix = ipa_param_prefixes[apm->param_prefix_index];
1273 DECL_NAME (new_parm) = create_tmp_var_name (prefix);
1274 DECL_ARTIFICIAL (new_parm) = 1;
1275 DECL_ARG_TYPE (new_parm) = new_type;
1276 DECL_CONTEXT (new_parm) = m_fndecl;
1277 TREE_USED (new_parm) = 1;
1278 DECL_IGNORED_P (new_parm) = 1;
1279 layout_decl (new_parm, 0);
1280 m_new_decls.quick_push (new_parm);
1282 if (apm->op == IPA_PARAM_OP_SPLIT)
1284 m_split_modifications_p = true;
1285 split[prev_index] = true;
1286 register_replacement (apm, new_parm);
1289 else
1290 gcc_unreachable ();
1293 if (tree_map)
1295 /* Do not treat parameters which were replaced with a constant as
1296 completely vanished. */
1297 auto_vec <int, 16> index_mapping;
1298 bool need_remap = false;
1300 if (m_id)
1302 clone_info *cinfo = clone_info::get (m_id->src_node);
1303 if (cinfo && cinfo->param_adjustments)
1305 cinfo->param_adjustments->get_updated_indices (&index_mapping);
1306 need_remap = true;
1310 for (unsigned i = 0; i < tree_map->length (); i++)
1312 int parm_num = (*tree_map)[i]->parm_num;
1313 gcc_assert (parm_num >= 0);
1314 if (need_remap)
1315 parm_num = index_mapping[parm_num];
1316 kept[parm_num] = true;
1320 /* As part of body modifications, we will also have to replace remaining uses
1321 of remaining uses of removed PARM_DECLs (which do not however use the
1322 initial value) with their VAR_DECL copies.
1324 We do this differently with and without m_id. With m_id, we rely on its
1325 mapping and create a replacement straight away. Without it, we have our
1326 own mechanism for which we have to populate m_removed_decls vector. Just
1327 don't mix them, that is why you should not call
1328 replace_removed_params_ssa_names or perform_cfun_body_modifications when
1329 you construct with ID not equal to NULL. */
1331 auto_vec<tree, 8> ssas_to_process_debug;
1332 unsigned op_len = m_oparms.length ();
1333 for (unsigned i = 0; i < op_len; i++)
1334 if (!kept[i])
1336 if (m_id)
1338 gcc_assert (!m_id->decl_map->get (m_oparms[i]));
1339 tree var = copy_decl_to_var (m_oparms[i], m_id);
1340 insert_decl_map (m_id, m_oparms[i], var);
1341 /* Declare this new variable. */
1342 DECL_CHAIN (var) = *vars;
1343 *vars = var;
1345 /* If this is not a split but a real removal, init hash sets
1346 that will guide what not to copy to the new body. */
1347 if (!split[i])
1348 mark_dead_statements (m_oparms[i], &ssas_to_process_debug);
1349 if (MAY_HAVE_DEBUG_STMTS
1350 && is_gimple_reg (m_oparms[i]))
1351 m_reset_debug_decls.safe_push (m_oparms[i]);
1353 else
1355 m_removed_decls.safe_push (m_oparms[i]);
1356 m_removed_map.put (m_oparms[i], m_removed_decls.length () - 1);
1357 if (MAY_HAVE_DEBUG_STMTS
1358 && !kept[i]
1359 && is_gimple_reg (m_oparms[i]))
1360 m_reset_debug_decls.safe_push (m_oparms[i]);
1364 while (!ssas_to_process_debug.is_empty ())
1365 prepare_debug_expressions (ssas_to_process_debug.pop ());
1368 /* Constructor of ipa_param_body_adjustments from a simple list of
1369 modifications to parameters listed in ADJ_PARAMS which will prepare ground
1370 for modification of parameters of fndecl. Return value of the function will
1371 not be removed and the object will assume it does not run as a part of
1372 tree-function_versioning. */
1374 ipa_param_body_adjustments
1375 ::ipa_param_body_adjustments (vec<ipa_adjusted_param, va_gc> *adj_params,
1376 tree fndecl)
1377 : m_adj_params (adj_params), m_adjustments (NULL), m_reset_debug_decls (),
1378 m_split_modifications_p (false), m_dead_stmts (), m_dead_ssas (),
1379 m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (), m_fndecl (fndecl),
1380 m_id (NULL), m_oparms (), m_new_decls (), m_new_types (), m_replacements (),
1381 m_removed_decls (), m_removed_map (), m_method2func (false)
1383 common_initialization (fndecl, NULL, NULL);
1386 /* Constructor of ipa_param_body_adjustments from ipa_param_adjustments in
1387 ADJUSTMENTS which will prepare ground for modification of parameters of
1388 fndecl. The object will assume it does not run as a part of
1389 tree-function_versioning. */
1391 ipa_param_body_adjustments
1392 ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
1393 tree fndecl)
1394 : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
1395 m_reset_debug_decls (), m_split_modifications_p (false), m_dead_stmts (),
1396 m_dead_ssas (), m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (),
1397 m_fndecl (fndecl), m_id (NULL), m_oparms (), m_new_decls (),
1398 m_new_types (), m_replacements (), m_removed_decls (), m_removed_map (),
1399 m_method2func (false)
1401 common_initialization (fndecl, NULL, NULL);
1404 /* Constructor of ipa_param_body_adjustments which sets it up as a part of
1405 running tree_function_versioning. Planned modifications to the function are
1406 in ADJUSTMENTS. FNDECL designates the new function clone which is being
1407 modified. OLD_FNDECL is the function of which FNDECL is a clone (and which
1408 at the time of invocation still share DECL_ARGUMENTS). ID is the
1409 copy_body_data structure driving the wholy body copying process. VARS is a
1410 pointer to the head of the list of new local variables, TREE_MAP is the map
1411 that drives tree substitution in the cloning process. */
1413 ipa_param_body_adjustments
1414 ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
1415 tree fndecl, tree old_fndecl,
1416 copy_body_data *id, tree *vars,
1417 vec<ipa_replace_map *, va_gc> *tree_map)
1418 : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
1419 m_reset_debug_decls (), m_split_modifications_p (false), m_dead_stmts (),
1420 m_dead_ssas (), m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (),
1421 m_fndecl (fndecl), m_id (id), m_oparms (), m_new_decls (), m_new_types (),
1422 m_replacements (), m_removed_decls (), m_removed_map (),
1423 m_method2func (false)
1425 common_initialization (old_fndecl, vars, tree_map);
1428 /* Chain new param decls up and return them. */
1430 tree
1431 ipa_param_body_adjustments::get_new_param_chain ()
1433 tree result;
1434 tree *link = &result;
1436 unsigned len = vec_safe_length (m_adj_params);
1437 for (unsigned i = 0; i < len; i++)
1439 tree new_decl = m_new_decls[i];
1440 *link = new_decl;
1441 link = &DECL_CHAIN (new_decl);
1443 *link = NULL_TREE;
1444 return result;
1447 /* Modify the function parameters FNDECL and its type according to the plan in
1448 ADJUSTMENTS. This function needs to be called when the decl has not already
1449 been processed with ipa_param_adjustments::adjust_decl, otherwise just
1450 seting DECL_ARGUMENTS to whatever get_new_param_chain will do is enough. */
1452 void
1453 ipa_param_body_adjustments::modify_formal_parameters ()
1455 tree orig_type = TREE_TYPE (m_fndecl);
1456 DECL_ARGUMENTS (m_fndecl) = get_new_param_chain ();
1458 /* When signature changes, we need to clear builtin info. */
1459 if (fndecl_built_in_p (m_fndecl))
1460 set_decl_built_in_function (m_fndecl, NOT_BUILT_IN, 0);
1462 /* At this point, removing return value is only implemented when going
1463 through tree_function_versioning, not when modifying function body
1464 directly. */
1465 gcc_assert (!m_adjustments || !m_adjustments->m_skip_return);
1466 tree new_type = build_adjusted_function_type (orig_type, &m_new_types,
1467 m_method2func, false);
1469 TREE_TYPE (m_fndecl) = new_type;
1470 DECL_VIRTUAL_P (m_fndecl) = 0;
1471 DECL_LANG_SPECIFIC (m_fndecl) = NULL;
1472 if (m_method2func)
1473 DECL_VINDEX (m_fndecl) = NULL_TREE;
1476 /* Given BASE and UNIT_OFFSET, find the corresponding record among replacement
1477 structures. */
1479 ipa_param_body_replacement *
1480 ipa_param_body_adjustments::lookup_replacement_1 (tree base,
1481 unsigned unit_offset)
1483 unsigned int len = m_replacements.length ();
1484 for (unsigned i = 0; i < len; i++)
1486 ipa_param_body_replacement *pbr = &m_replacements[i];
1488 if (pbr->base == base
1489 && (pbr->unit_offset == unit_offset))
1490 return pbr;
1492 return NULL;
1495 /* Given BASE and UNIT_OFFSET, find the corresponding replacement expression
1496 and return it, assuming it is known it does not hold value by reference or
1497 in reverse storage order. */
1499 tree
1500 ipa_param_body_adjustments::lookup_replacement (tree base, unsigned unit_offset)
1502 ipa_param_body_replacement *pbr = lookup_replacement_1 (base, unit_offset);
1503 if (!pbr)
1504 return NULL;
1505 return pbr->repl;
1508 /* If T is an SSA_NAME, return NULL if it is not a default def or
1509 return its base variable if it is. If IGNORE_DEFAULT_DEF is true,
1510 the base variable is always returned, regardless if it is a default
1511 def. Return T if it is not an SSA_NAME. */
1513 static tree
1514 get_ssa_base_param (tree t, bool ignore_default_def)
1516 if (TREE_CODE (t) == SSA_NAME)
1518 if (ignore_default_def || SSA_NAME_IS_DEFAULT_DEF (t))
1519 return SSA_NAME_VAR (t);
1520 else
1521 return NULL_TREE;
1523 return t;
1526 /* Given an expression, return the structure describing how it should be
1527 replaced if it accesses a part of a split parameter or NULL otherwise.
1529 Do not free the result, it will be deallocated when the object is destroyed.
1531 If IGNORE_DEFAULT_DEF is cleared, consider only SSA_NAMEs of PARM_DECLs
1532 which are default definitions, if set, consider all SSA_NAMEs of
1533 PARM_DECLs. */
1535 ipa_param_body_replacement *
1536 ipa_param_body_adjustments::get_expr_replacement (tree expr,
1537 bool ignore_default_def)
1539 tree base;
1540 unsigned unit_offset;
1542 if (!isra_get_ref_base_and_offset (expr, &base, &unit_offset))
1543 return NULL;
1545 base = get_ssa_base_param (base, ignore_default_def);
1546 if (!base || TREE_CODE (base) != PARM_DECL)
1547 return NULL;
1548 return lookup_replacement_1 (base, unit_offset);
1551 /* Given OLD_DECL, which is a PARM_DECL of a parameter that is being removed
1552 (which includes it being split or replaced), return a new variable that
1553 should be used for any SSA names that will remain in the function that
1554 previously belonged to OLD_DECL. */
1556 tree
1557 ipa_param_body_adjustments::get_replacement_ssa_base (tree old_decl)
1559 unsigned *idx = m_removed_map.get (old_decl);
1560 if (!idx)
1561 return NULL;
1563 tree repl;
1564 if (TREE_CODE (m_removed_decls[*idx]) == PARM_DECL)
1566 gcc_assert (m_removed_decls[*idx] == old_decl);
1567 repl = copy_var_decl (old_decl, DECL_NAME (old_decl),
1568 TREE_TYPE (old_decl));
1569 m_removed_decls[*idx] = repl;
1571 else
1572 repl = m_removed_decls[*idx];
1573 return repl;
1576 /* If OLD_NAME, which is being defined by statement STMT, is an SSA_NAME of a
1577 parameter which is to be removed because its value is not used, create a new
1578 SSA_NAME relating to a replacement VAR_DECL, replace all uses of the
1579 original with it and return it. If there is no need to re-map, return NULL.
1580 ADJUSTMENTS is a pointer to a vector of IPA-SRA adjustments. */
1582 tree
1583 ipa_param_body_adjustments::replace_removed_params_ssa_names (tree old_name,
1584 gimple *stmt)
1586 gcc_assert (!m_id);
1587 if (TREE_CODE (old_name) != SSA_NAME)
1588 return NULL;
1590 tree decl = SSA_NAME_VAR (old_name);
1591 if (decl == NULL_TREE
1592 || TREE_CODE (decl) != PARM_DECL)
1593 return NULL;
1595 tree repl = get_replacement_ssa_base (decl);
1596 if (!repl)
1597 return NULL;
1599 tree new_name = make_ssa_name (repl, stmt);
1600 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name)
1601 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (old_name);
1603 if (dump_file && (dump_flags & TDF_DETAILS))
1605 fprintf (dump_file, "replacing an SSA name of a removed param ");
1606 print_generic_expr (dump_file, old_name);
1607 fprintf (dump_file, " with ");
1608 print_generic_expr (dump_file, new_name);
1609 fprintf (dump_file, "\n");
1612 replace_uses_by (old_name, new_name);
1613 return new_name;
1616 /* If the expression *EXPR_P should be replaced, do so. CONVERT specifies
1617 whether the function should care about type incompatibility of the current
1618 and new expressions. If it is false, the function will leave
1619 incompatibility issues to the caller - note that when the function
1620 encounters a BIT_FIELD_REF, IMAGPART_EXPR or REALPART_EXPR, it will modify
1621 their bases instead of the expressions themselves and then also performs any
1622 necessary conversions. */
1624 bool
1625 ipa_param_body_adjustments::modify_expression (tree *expr_p, bool convert)
1627 tree expr = *expr_p;
1629 if (TREE_CODE (expr) == BIT_FIELD_REF
1630 || TREE_CODE (expr) == IMAGPART_EXPR
1631 || TREE_CODE (expr) == REALPART_EXPR)
1633 expr_p = &TREE_OPERAND (expr, 0);
1634 expr = *expr_p;
1635 convert = true;
1638 ipa_param_body_replacement *pbr = get_expr_replacement (expr, false);
1639 if (!pbr)
1640 return false;
1642 tree repl = pbr->repl;
1643 if (dump_file && (dump_flags & TDF_DETAILS))
1645 fprintf (dump_file, "About to replace expr ");
1646 print_generic_expr (dump_file, expr);
1647 fprintf (dump_file, " with ");
1648 print_generic_expr (dump_file, repl);
1649 fprintf (dump_file, "\n");
1652 if (convert && !useless_type_conversion_p (TREE_TYPE (expr),
1653 TREE_TYPE (repl)))
1655 tree vce = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expr), repl);
1656 *expr_p = vce;
1658 else
1659 *expr_p = repl;
1660 return true;
1663 /* If the assignment statement STMT contains any expressions that need to
1664 replaced with a different one as noted by ADJUSTMENTS, do so. Handle any
1665 potential type incompatibilities. If any conversion sttements have to be
1666 pre-pended to STMT, they will be added to EXTRA_STMTS. Return true iff the
1667 statement was modified. */
1669 bool
1670 ipa_param_body_adjustments::modify_assignment (gimple *stmt,
1671 gimple_seq *extra_stmts)
1673 tree *lhs_p, *rhs_p;
1674 bool any;
1676 if (!gimple_assign_single_p (stmt))
1677 return false;
1679 rhs_p = gimple_assign_rhs1_ptr (stmt);
1680 lhs_p = gimple_assign_lhs_ptr (stmt);
1682 any = modify_expression (lhs_p, false);
1683 any |= modify_expression (rhs_p, false);
1684 if (any
1685 && !useless_type_conversion_p (TREE_TYPE (*lhs_p), TREE_TYPE (*rhs_p)))
1687 if (TREE_CODE (*rhs_p) == CONSTRUCTOR)
1689 /* V_C_Es of constructors can cause trouble (PR 42714). */
1690 if (is_gimple_reg_type (TREE_TYPE (*lhs_p)))
1691 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
1692 else
1693 *rhs_p = build_constructor (TREE_TYPE (*lhs_p),
1694 NULL);
1696 else
1698 tree new_rhs = fold_build1_loc (gimple_location (stmt),
1699 VIEW_CONVERT_EXPR, TREE_TYPE (*lhs_p),
1700 *rhs_p);
1701 tree tmp = force_gimple_operand (new_rhs, extra_stmts, true,
1702 NULL_TREE);
1703 gimple_assign_set_rhs1 (stmt, tmp);
1705 return true;
1708 return any;
1711 /* Record information about what modifications to call arguments have already
1712 been done by clone materialization into a summary describing CS. The
1713 information is stored in NEW_INDEX_MAP, NEW_PT_MAP and NEW_ALWAYS_COPY_DELTA
1714 and correspond to equivalent fields in ipa_edge_modification_info. Return
1715 the edge summary. */
1717 static ipa_edge_modification_info *
1718 record_argument_state_1 (cgraph_edge *cs, const vec<int> &new_index_map,
1719 const vec<pass_through_split_map> &new_pt_map,
1720 int new_always_copy_delta)
1723 ipa_edge_modification_info *sum = ipa_edge_modifications->get_create (cs);
1725 unsigned len = sum->pass_through_map.length ();
1726 for (unsigned i = 0; i < len; i++)
1728 unsigned oldnew = sum->pass_through_map[i].new_index;
1729 sum->pass_through_map[i].new_index = new_index_map[oldnew];
1732 len = sum->index_map.length ();
1733 if (len > 0)
1735 unsigned nptlen = new_pt_map.length ();
1736 for (unsigned j = 0; j < nptlen; j++)
1738 int inverse = -1;
1739 for (unsigned i = 0; i < len ; i++)
1740 if ((unsigned) sum->index_map[i] == new_pt_map[j].base_index)
1742 inverse = i;
1743 break;
1745 gcc_assert (inverse >= 0);
1746 pass_through_split_map ptm_item;
1748 ptm_item.base_index = inverse;
1749 ptm_item.unit_offset = new_pt_map[j].unit_offset;
1750 ptm_item.new_index = new_pt_map[j].new_index;
1751 sum->pass_through_map.safe_push (ptm_item);
1754 for (unsigned i = 0; i < len; i++)
1756 int idx = sum->index_map[i];
1757 if (idx < 0)
1758 continue;
1759 sum->index_map[i] = new_index_map[idx];
1762 else
1764 sum->pass_through_map.safe_splice (new_pt_map);
1765 sum->index_map.safe_splice (new_index_map);
1767 sum->always_copy_delta += new_always_copy_delta;
1768 return sum;
1771 /* Record information about what modifications to call arguments have already
1772 been done by clone materialization into a summary of an edge describing the
1773 call in this clone and all its clones. NEW_INDEX_MAP, NEW_PT_MAP and
1774 NEW_ALWAYS_COPY_DELTA have the same meaning as record_argument_state_1.
1776 In order to associate the info with the right edge summaries, we need
1777 address of the ORIG_STMT in the function from which we are cloning (because
1778 the edges have not yet been re-assigned to the new statement that has just
1779 been created) and ID, the structure governing function body copying. */
1781 static void
1782 record_argument_state (copy_body_data *id, gimple *orig_stmt,
1783 const vec<int> &new_index_map,
1784 const vec<pass_through_split_map> &new_pt_map,
1785 int new_always_copy_delta)
1787 if (!ipa_edge_modifications)
1788 ipa_edge_modifications = new ipa_edge_modification_sum (symtab);
1790 struct cgraph_node *this_node = id->dst_node;
1791 ipa_edge_modification_info *first_sum = NULL;
1792 cgraph_edge *cs = this_node->get_edge (orig_stmt);
1793 if (cs)
1794 first_sum = record_argument_state_1 (cs, new_index_map, new_pt_map,
1795 new_always_copy_delta);
1796 else
1797 gcc_assert (this_node->clones);
1799 if (!this_node->clones)
1800 return;
1801 for (cgraph_node *subclone = this_node->clones; subclone != this_node;)
1803 cs = subclone->get_edge (orig_stmt);
1804 if (cs)
1806 if (!first_sum)
1807 first_sum = record_argument_state_1 (cs, new_index_map, new_pt_map,
1808 new_always_copy_delta);
1809 else
1811 ipa_edge_modification_info *s2
1812 = ipa_edge_modifications->get_create (cs);
1813 s2->index_map.truncate (0);
1814 s2->index_map.safe_splice (first_sum->index_map);
1815 s2->pass_through_map.truncate (0);
1816 s2->pass_through_map.safe_splice (first_sum->pass_through_map);
1817 s2->always_copy_delta = first_sum->always_copy_delta;
1820 else
1821 gcc_assert (subclone->clones);
1823 if (subclone->clones)
1824 subclone = subclone->clones;
1825 else if (subclone->next_sibling_clone)
1826 subclone = subclone->next_sibling_clone;
1827 else
1829 while (subclone != this_node && !subclone->next_sibling_clone)
1830 subclone = subclone->clone_of;
1831 if (subclone != this_node)
1832 subclone = subclone->next_sibling_clone;
1837 /* If the call statement pointed at by STMT_P contains any expressions that
1838 need to replaced with a different one as noted by ADJUSTMENTS, do so. f the
1839 statement needs to be rebuilt, do so. Return true if any modifications have
1840 been performed. ORIG_STMT, if not NULL, is the original statement in the
1841 function that is being cloned from, which at this point can be used to look
1842 up call_graph edges.
1844 If the method is invoked as a part of IPA clone materialization and if any
1845 parameter split is pass-through, i.e. it applies to the functin that is
1846 being modified and also to the callee of the statement, replace the
1847 parameter passed to old callee with all of the replacement a callee might
1848 possibly want and record the performed argument modifications in
1849 ipa_edge_modifications. Likewise if any argument has already been left out
1850 because it is not necessary. */
1852 bool
1853 ipa_param_body_adjustments::modify_call_stmt (gcall **stmt_p,
1854 gimple *orig_stmt)
1856 auto_vec <unsigned, 4> pass_through_args;
1857 auto_vec <unsigned, 4> pass_through_pbr_indices;
1858 auto_vec <HOST_WIDE_INT, 4> pass_through_offsets;
1859 gcall *stmt = *stmt_p;
1860 unsigned nargs = gimple_call_num_args (stmt);
1861 bool recreate = false;
1863 for (unsigned i = 0; i < gimple_call_num_args (stmt); i++)
1865 tree t = gimple_call_arg (stmt, i);
1866 gcc_assert (TREE_CODE (t) != BIT_FIELD_REF
1867 && TREE_CODE (t) != IMAGPART_EXPR
1868 && TREE_CODE (t) != REALPART_EXPR);
1870 if (TREE_CODE (t) == SSA_NAME
1871 && m_dead_ssas.contains (t))
1872 recreate = true;
1874 if (!m_split_modifications_p)
1875 continue;
1877 tree base;
1878 unsigned agg_arg_offset;
1879 if (!isra_get_ref_base_and_offset (t, &base, &agg_arg_offset))
1880 continue;
1882 bool by_ref = false;
1883 if (TREE_CODE (base) == SSA_NAME)
1885 if (!SSA_NAME_IS_DEFAULT_DEF (base))
1886 continue;
1887 base = SSA_NAME_VAR (base);
1888 gcc_checking_assert (base);
1889 by_ref = true;
1891 if (TREE_CODE (base) != PARM_DECL)
1892 continue;
1894 bool base_among_replacements = false;
1895 unsigned j, repl_list_len = m_replacements.length ();
1896 for (j = 0; j < repl_list_len; j++)
1898 ipa_param_body_replacement *pbr = &m_replacements[j];
1899 if (pbr->base == base)
1901 base_among_replacements = true;
1902 break;
1905 if (!base_among_replacements)
1906 continue;
1908 /* We still have to distinguish between an end-use that we have to
1909 transform now and a pass-through, which happens in the following
1910 two cases. */
1912 /* TODO: After we adjust ptr_parm_has_nonarg_uses to also consider
1913 &MEM_REF[ssa_name + offset], we will also have to detect that case
1914 here. */
1916 if (TREE_CODE (t) == SSA_NAME
1917 && SSA_NAME_IS_DEFAULT_DEF (t)
1918 && SSA_NAME_VAR (t)
1919 && TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL)
1921 /* This must be a by_reference pass-through. */
1922 recreate = true;
1923 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
1924 pass_through_args.safe_push (i);
1925 pass_through_pbr_indices.safe_push (j);
1926 pass_through_offsets.safe_push (agg_arg_offset);
1928 else if (!by_ref && AGGREGATE_TYPE_P (TREE_TYPE (t)))
1930 /* Currently IPA-SRA guarantees the aggregate access type
1931 exactly matches in this case. So if it does not match, it is
1932 a pass-through argument that will be sorted out at edge
1933 redirection time. */
1934 ipa_param_body_replacement *pbr
1935 = lookup_replacement_1 (base, agg_arg_offset);
1937 if (!pbr
1938 || (TYPE_MAIN_VARIANT (TREE_TYPE (t))
1939 != TYPE_MAIN_VARIANT (TREE_TYPE (pbr->repl))))
1941 recreate = true;
1942 pass_through_args.safe_push (i);
1943 pass_through_pbr_indices.safe_push (j);
1944 pass_through_offsets.safe_push (agg_arg_offset);
1949 if (!recreate)
1951 /* No need to rebuild the statement, let's just modify arguments
1952 and the LHS if/as appropriate. */
1953 bool modified = false;
1954 for (unsigned i = 0; i < nargs; i++)
1956 tree *t = gimple_call_arg_ptr (stmt, i);
1957 modified |= modify_expression (t, true);
1959 if (gimple_call_lhs (stmt))
1961 tree *t = gimple_call_lhs_ptr (stmt);
1962 modified |= modify_expression (t, false);
1964 return modified;
1967 auto_vec<int, 16> index_map;
1968 auto_vec<pass_through_split_map, 4> pass_through_map;
1969 auto_vec<tree, 16> vargs;
1970 int always_copy_delta = 0;
1971 unsigned pt_idx = 0;
1972 int new_arg_idx = 0;
1973 for (unsigned i = 0; i < nargs; i++)
1975 if (pt_idx < pass_through_args.length ()
1976 && i == pass_through_args[pt_idx])
1978 unsigned j = pass_through_pbr_indices[pt_idx];
1979 unsigned agg_arg_offset = pass_through_offsets[pt_idx];
1980 pt_idx++;
1981 always_copy_delta--;
1982 tree base = m_replacements[j].base;
1984 /* In order to be put into SSA form, we have to push all replacements
1985 pertaining to this parameter as parameters to the call statement.
1986 Edge redirection will need to use edge summary to weed out the
1987 unnecessary ones. */
1988 unsigned repl_list_len = m_replacements.length ();
1989 for (; j < repl_list_len; j++)
1991 if (m_replacements[j].base != base)
1992 break;
1993 if (m_replacements[j].unit_offset < agg_arg_offset)
1994 continue;
1995 pass_through_split_map pt_map;
1996 pt_map.base_index = i;
1997 pt_map.unit_offset
1998 = m_replacements[j].unit_offset - agg_arg_offset;
1999 pt_map.new_index = new_arg_idx;
2000 pass_through_map.safe_push (pt_map);
2001 vargs.safe_push (m_replacements[j].repl);
2002 new_arg_idx++;
2003 always_copy_delta++;
2005 index_map.safe_push (-1);
2007 else
2009 tree t = gimple_call_arg (stmt, i);
2010 if (TREE_CODE (t) == SSA_NAME
2011 && m_dead_ssas.contains (t))
2013 always_copy_delta--;
2014 index_map.safe_push (-1);
2016 else
2018 modify_expression (&t, true);
2019 vargs.safe_push (t);
2020 index_map.safe_push (new_arg_idx);
2021 new_arg_idx++;
2026 gcall *new_stmt = gimple_build_call_vec (gimple_call_fn (stmt), vargs);
2027 if (gimple_has_location (stmt))
2028 gimple_set_location (new_stmt, gimple_location (stmt));
2029 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
2030 gimple_call_copy_flags (new_stmt, stmt);
2031 if (tree lhs = gimple_call_lhs (stmt))
2033 modify_expression (&lhs, false);
2034 /* Avoid adjusting SSA_NAME_DEF_STMT of a SSA lhs, SSA names
2035 have not yet been remapped. */
2036 *gimple_call_lhs_ptr (new_stmt) = lhs;
2038 *stmt_p = new_stmt;
2040 if (orig_stmt)
2041 record_argument_state (m_id, orig_stmt, index_map, pass_through_map,
2042 always_copy_delta);
2043 return true;
2046 /* If the statement STMT contains any expressions that need to replaced with a
2047 different one as noted by ADJUSTMENTS, do so. Handle any potential type
2048 incompatibilities. If any conversion sttements have to be pre-pended to
2049 STMT, they will be added to EXTRA_STMTS. Return true iff the statement was
2050 modified. */
2052 bool
2053 ipa_param_body_adjustments::modify_gimple_stmt (gimple **stmt,
2054 gimple_seq *extra_stmts,
2055 gimple *orig_stmt)
2057 bool modified = false;
2058 tree *t;
2060 switch (gimple_code (*stmt))
2062 case GIMPLE_RETURN:
2063 t = gimple_return_retval_ptr (as_a <greturn *> (*stmt));
2064 if (m_adjustments && m_adjustments->m_skip_return)
2065 *t = NULL_TREE;
2066 else if (*t != NULL_TREE)
2067 modified |= modify_expression (t, true);
2068 break;
2070 case GIMPLE_ASSIGN:
2071 modified |= modify_assignment (*stmt, extra_stmts);
2072 break;
2074 case GIMPLE_CALL:
2075 modified |= modify_call_stmt ((gcall **) stmt, orig_stmt);
2076 break;
2078 case GIMPLE_ASM:
2080 gasm *asm_stmt = as_a <gasm *> (*stmt);
2081 for (unsigned i = 0; i < gimple_asm_ninputs (asm_stmt); i++)
2083 t = &TREE_VALUE (gimple_asm_input_op (asm_stmt, i));
2084 modified |= modify_expression (t, true);
2086 for (unsigned i = 0; i < gimple_asm_noutputs (asm_stmt); i++)
2088 t = &TREE_VALUE (gimple_asm_output_op (asm_stmt, i));
2089 modified |= modify_expression (t, false);
2092 break;
2094 default:
2095 break;
2097 return modified;
2101 /* Traverse body of the current function and perform the requested adjustments
2102 on its statements. Return true iff the CFG has been changed. */
2104 bool
2105 ipa_param_body_adjustments::modify_cfun_body ()
2107 bool cfg_changed = false;
2108 basic_block bb;
2110 FOR_EACH_BB_FN (bb, cfun)
2112 gimple_stmt_iterator gsi;
2114 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2116 gphi *phi = as_a <gphi *> (gsi_stmt (gsi));
2117 tree new_lhs, old_lhs = gimple_phi_result (phi);
2118 new_lhs = replace_removed_params_ssa_names (old_lhs, phi);
2119 if (new_lhs)
2121 gimple_phi_set_result (phi, new_lhs);
2122 release_ssa_name (old_lhs);
2126 gsi = gsi_start_bb (bb);
2127 while (!gsi_end_p (gsi))
2129 gimple *stmt = gsi_stmt (gsi);
2130 gimple *stmt_copy = stmt;
2131 gimple_seq extra_stmts = NULL;
2132 bool modified = modify_gimple_stmt (&stmt, &extra_stmts, NULL);
2133 if (stmt != stmt_copy)
2135 gcc_checking_assert (modified);
2136 gsi_replace (&gsi, stmt, false);
2138 if (!gimple_seq_empty_p (extra_stmts))
2139 gsi_insert_seq_before (&gsi, extra_stmts, GSI_SAME_STMT);
2141 def_operand_p defp;
2142 ssa_op_iter iter;
2143 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF)
2145 tree old_def = DEF_FROM_PTR (defp);
2146 if (tree new_def = replace_removed_params_ssa_names (old_def,
2147 stmt))
2149 SET_DEF (defp, new_def);
2150 release_ssa_name (old_def);
2151 modified = true;
2155 if (modified)
2157 update_stmt (stmt);
2158 if (maybe_clean_eh_stmt (stmt)
2159 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
2160 cfg_changed = true;
2162 gsi_next (&gsi);
2166 return cfg_changed;
2169 /* Call gimple_debug_bind_reset_value on all debug statements describing
2170 gimple register parameters that are being removed or replaced. */
2172 void
2173 ipa_param_body_adjustments::reset_debug_stmts ()
2175 int i, len;
2176 gimple_stmt_iterator *gsip = NULL, gsi;
2178 if (MAY_HAVE_DEBUG_STMTS && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
2180 gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
2181 gsip = &gsi;
2183 len = m_reset_debug_decls.length ();
2184 for (i = 0; i < len; i++)
2186 imm_use_iterator ui;
2187 gimple *stmt;
2188 gdebug *def_temp;
2189 tree name, vexpr, copy = NULL_TREE;
2190 use_operand_p use_p;
2191 tree decl = m_reset_debug_decls[i];
2193 gcc_checking_assert (is_gimple_reg (decl));
2194 name = ssa_default_def (cfun, decl);
2195 vexpr = NULL;
2196 if (name)
2197 FOR_EACH_IMM_USE_STMT (stmt, ui, name)
2199 if (gimple_clobber_p (stmt))
2201 gimple_stmt_iterator cgsi = gsi_for_stmt (stmt);
2202 unlink_stmt_vdef (stmt);
2203 gsi_remove (&cgsi, true);
2204 release_defs (stmt);
2205 continue;
2207 /* All other users must have been removed by function body
2208 modification. */
2209 gcc_assert (is_gimple_debug (stmt));
2210 if (vexpr == NULL && gsip != NULL)
2212 vexpr = make_node (DEBUG_EXPR_DECL);
2213 def_temp = gimple_build_debug_source_bind (vexpr, decl, NULL);
2214 DECL_ARTIFICIAL (vexpr) = 1;
2215 TREE_TYPE (vexpr) = TREE_TYPE (name);
2216 SET_DECL_MODE (vexpr, DECL_MODE (decl));
2217 gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
2219 if (vexpr)
2221 FOR_EACH_IMM_USE_ON_STMT (use_p, ui)
2222 SET_USE (use_p, vexpr);
2224 else
2225 gimple_debug_bind_reset_value (stmt);
2226 update_stmt (stmt);
2228 /* Create a VAR_DECL for debug info purposes. */
2229 if (!DECL_IGNORED_P (decl))
2231 copy = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
2232 VAR_DECL, DECL_NAME (decl),
2233 TREE_TYPE (decl));
2234 if (DECL_PT_UID_SET_P (decl))
2235 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
2236 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
2237 TREE_READONLY (copy) = TREE_READONLY (decl);
2238 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
2239 DECL_NOT_GIMPLE_REG_P (copy) = DECL_NOT_GIMPLE_REG_P (decl);
2240 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
2241 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
2242 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
2243 DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
2244 SET_DECL_RTL (copy, 0);
2245 TREE_USED (copy) = 1;
2246 DECL_CONTEXT (copy) = current_function_decl;
2247 add_local_decl (cfun, copy);
2248 DECL_CHAIN (copy)
2249 = BLOCK_VARS (DECL_INITIAL (current_function_decl));
2250 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = copy;
2252 if (gsip != NULL && copy && target_for_debug_bind (decl))
2254 gcc_assert (TREE_CODE (decl) == PARM_DECL);
2255 if (vexpr)
2256 def_temp = gimple_build_debug_bind (copy, vexpr, NULL);
2257 else
2258 def_temp = gimple_build_debug_source_bind (copy, decl,
2259 NULL);
2260 gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
2265 /* Perform all necessary body changes to change signature, body and debug info
2266 of fun according to adjustments passed at construction. Return true if CFG
2267 was changed in any way. The main entry point for modification of standalone
2268 functions that is not part of IPA clone materialization. */
2270 bool
2271 ipa_param_body_adjustments::perform_cfun_body_modifications ()
2273 bool cfg_changed;
2274 modify_formal_parameters ();
2275 cfg_changed = modify_cfun_body ();
2276 reset_debug_stmts ();
2278 return cfg_changed;
2282 /* Deallocate summaries which otherwise stay alive until the end of
2283 compilation. */
2285 void
2286 ipa_edge_modifications_finalize ()
2288 if (!ipa_edge_modifications)
2289 return;
2290 delete ipa_edge_modifications;
2291 ipa_edge_modifications = NULL;