Fix omp target issue with Fortran optional arguments
[official-gcc.git] / gcc / ipa-param-manipulation.c
blobbbf646726e230d32c749a0790fd2c35333e38583
1 /* Manipulation of formal and actual parameters of functions and function
2 calls.
3 Copyright (C) 2017-2019 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"
45 /* Actual prefixes of different newly synthetized parameters. Keep in sync
46 with IPA_PARAM_PREFIX_* defines. */
48 static const char *ipa_param_prefixes[IPA_PARAM_PREFIX_COUNT]
49 = {"SYNTH",
50 "ISRA",
51 "simd",
52 "mask"};
54 /* Names of parameters for dumping. Keep in sync with enum ipa_parm_op. */
56 static const char *ipa_param_op_names[IPA_PARAM_PREFIX_COUNT]
57 = {"IPA_PARAM_OP_UNDEFINED",
58 "IPA_PARAM_OP_COPY",
59 "IPA_PARAM_OP_NEW",
60 "IPA_PARAM_OP_SPLIT"};
62 /* Fill an empty vector ARGS with PARM_DECLs representing formal parameters of
63 FNDECL. The function should not be called during LTO WPA phase except for
64 thunks (or functions with bodies streamed in). */
66 void
67 push_function_arg_decls (vec<tree> *args, tree fndecl)
69 int count;
70 tree parm;
72 /* Safety check that we do not attempt to use the function in WPA, except
73 when the function is a thunk and then we have DECL_ARGUMENTS or when we
74 have already explicitely loaded its body. */
75 gcc_assert (!flag_wpa
76 || DECL_ARGUMENTS (fndecl)
77 || gimple_has_body_p (fndecl));
78 count = 0;
79 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
80 count++;
82 args->reserve_exact (count);
83 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
84 args->quick_push (parm);
87 /* Fill an empty vector TYPES with trees representing formal parameters of
88 function type FNTYPE. */
90 void
91 push_function_arg_types (vec<tree> *types, tree fntype)
93 int count = 0;
94 tree t;
96 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
97 count++;
99 types->reserve_exact (count);
100 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
101 types->quick_push (TREE_VALUE (t));
104 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
105 friendly way, assuming they are meant to be applied to FNDECL. */
107 void
108 ipa_dump_adjusted_parameters (FILE *f,
109 vec<ipa_adjusted_param, va_gc> *adj_params)
111 unsigned i, len = vec_safe_length (adj_params);
112 bool first = true;
114 fprintf (f, " IPA adjusted parameters: ");
115 for (i = 0; i < len; i++)
117 struct ipa_adjusted_param *apm;
118 apm = &(*adj_params)[i];
120 if (!first)
121 fprintf (f, " ");
122 else
123 first = false;
125 fprintf (f, "%i. %s %s", i, ipa_param_op_names[apm->op],
126 apm->prev_clone_adjustment ? "prev_clone_adjustment " : "");
127 switch (apm->op)
129 case IPA_PARAM_OP_UNDEFINED:
130 break;
132 case IPA_PARAM_OP_COPY:
133 fprintf (f, ", base_index: %u", apm->base_index);
134 fprintf (f, ", prev_clone_index: %u", apm->prev_clone_index);
135 break;
137 case IPA_PARAM_OP_SPLIT:
138 fprintf (f, ", offset: %u", apm->unit_offset);
139 /* fall-through */
140 case IPA_PARAM_OP_NEW:
141 fprintf (f, ", base_index: %u", apm->base_index);
142 fprintf (f, ", prev_clone_index: %u", apm->prev_clone_index);
143 print_node_brief (f, ", type: ", apm->type, 0);
144 print_node_brief (f, ", alias type: ", apm->alias_ptr_type, 0);
145 fprintf (f, " prefix: %s",
146 ipa_param_prefixes[apm->param_prefix_index]);
147 if (apm->reverse)
148 fprintf (f, ", reverse-sso");
149 break;
151 fprintf (f, "\n");
155 /* Fill NEW_TYPES with types of a function after its current OTYPES have been
156 modified as described in ADJ_PARAMS. When USE_PREV_INDICES is true, use
157 prev_clone_index from ADJ_PARAMS as opposed to base_index when the parameter
158 is false. */
160 static void
161 fill_vector_of_new_param_types (vec<tree> *new_types, vec<tree> *otypes,
162 vec<ipa_adjusted_param, va_gc> *adj_params,
163 bool use_prev_indices)
165 unsigned adj_len = vec_safe_length (adj_params);
166 new_types->reserve_exact (adj_len);
167 for (unsigned i = 0; i < adj_len ; i++)
169 ipa_adjusted_param *apm = &(*adj_params)[i];
170 if (apm->op == IPA_PARAM_OP_COPY)
172 unsigned index
173 = use_prev_indices ? apm->prev_clone_index : apm->base_index;
174 /* The following needs to be handled gracefully because of type
175 mismatches. This happens with LTO but apparently also in Fortran
176 with -fcoarray=lib -O2 -lcaf_single -latomic. */
177 if (index >= otypes->length ())
178 continue;
179 new_types->quick_push ((*otypes)[index]);
181 else if (apm->op == IPA_PARAM_OP_NEW
182 || apm->op == IPA_PARAM_OP_SPLIT)
184 tree ntype = apm->type;
185 if (is_gimple_reg_type (ntype)
186 && TYPE_MODE (ntype) != BLKmode)
188 unsigned malign = GET_MODE_ALIGNMENT (TYPE_MODE (ntype));
189 if (TYPE_ALIGN (ntype) != malign)
190 ntype = build_aligned_type (ntype, malign);
192 new_types->quick_push (ntype);
194 else
195 gcc_unreachable ();
199 /* Build and return a function type just like ORIG_TYPE but with parameter
200 types given in NEW_PARAM_TYPES - which can be NULL if, but only if,
201 ORIG_TYPE itself has NULL TREE_ARG_TYPEs. If METHOD2FUNC is true, also make
202 it a FUNCTION_TYPE instead of FUNCTION_TYPE. */
204 static tree
205 build_adjusted_function_type (tree orig_type, vec<tree> *new_param_types,
206 bool method2func, bool skip_return)
208 tree new_arg_types = NULL;
209 if (TYPE_ARG_TYPES (orig_type))
211 gcc_checking_assert (new_param_types);
212 bool last_parm_void = (TREE_VALUE (tree_last (TYPE_ARG_TYPES (orig_type)))
213 == void_type_node);
214 unsigned len = new_param_types->length ();
215 for (unsigned i = 0; i < len; i++)
216 new_arg_types = tree_cons (NULL_TREE, (*new_param_types)[i],
217 new_arg_types);
219 tree new_reversed = nreverse (new_arg_types);
220 if (last_parm_void)
222 if (new_reversed)
223 TREE_CHAIN (new_arg_types) = void_list_node;
224 else
225 new_reversed = void_list_node;
227 new_arg_types = new_reversed;
230 /* Use build_distinct_type_copy to preserve as much as possible from original
231 type (debug info, attribute lists etc.). The one exception is
232 METHOD_TYPEs which must have THIS argument and when we are asked to remove
233 it, we need to build new FUNCTION_TYPE instead. */
234 tree new_type = NULL;
235 if (method2func)
237 tree ret_type;
238 if (skip_return)
239 ret_type = void_type_node;
240 else
241 ret_type = TREE_TYPE (orig_type);
243 new_type
244 = build_distinct_type_copy (build_function_type (ret_type,
245 new_arg_types));
246 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
248 else
250 new_type = build_distinct_type_copy (orig_type);
251 TYPE_ARG_TYPES (new_type) = new_arg_types;
252 if (skip_return)
253 TREE_TYPE (new_type) = void_type_node;
256 return new_type;
259 /* Return the maximum index in any IPA_PARAM_OP_COPY adjustment or -1 if there
260 is none. */
263 ipa_param_adjustments::get_max_base_index ()
265 unsigned adj_len = vec_safe_length (m_adj_params);
266 int max_index = -1;
267 for (unsigned i = 0; i < adj_len ; i++)
269 ipa_adjusted_param *apm = &(*m_adj_params)[i];
270 if (apm->op == IPA_PARAM_OP_COPY
271 && max_index < apm->base_index)
272 max_index = apm->base_index;
274 return max_index;
278 /* Fill SURVIVING_PARAMS with an array of bools where each one says whether a
279 parameter that originally was at that position still survives in the given
280 clone or is removed/replaced. If the final array is smaller than an index
281 of an original parameter, that parameter also did not survive. That a
282 parameter survives does not mean it has the same index as before. */
284 void
285 ipa_param_adjustments::get_surviving_params (vec<bool> *surviving_params)
287 unsigned adj_len = vec_safe_length (m_adj_params);
288 int max_index = get_max_base_index ();
290 if (max_index < 0)
291 return;
292 surviving_params->reserve_exact (max_index + 1);
293 surviving_params->quick_grow_cleared (max_index + 1);
294 for (unsigned i = 0; i < adj_len ; i++)
296 ipa_adjusted_param *apm = &(*m_adj_params)[i];
297 if (apm->op == IPA_PARAM_OP_COPY)
298 (*surviving_params)[apm->base_index] = true;
302 /* Fill NEW_INDICES with new indices of each surviving parameter or -1 for
303 those which do not survive. Any parameter outside of lenght of the vector
304 does not survive. There is currently no support for a parameter to be
305 copied to two distinct new parameters. */
307 void
308 ipa_param_adjustments::get_updated_indices (vec<int> *new_indices)
310 unsigned adj_len = vec_safe_length (m_adj_params);
311 int max_index = get_max_base_index ();
313 if (max_index < 0)
314 return;
315 unsigned res_len = max_index + 1;
316 new_indices->reserve_exact (res_len);
317 for (unsigned i = 0; i < res_len ; i++)
318 new_indices->quick_push (-1);
319 for (unsigned i = 0; i < adj_len ; i++)
321 ipa_adjusted_param *apm = &(*m_adj_params)[i];
322 if (apm->op == IPA_PARAM_OP_COPY)
323 (*new_indices)[apm->base_index] = i;
327 /* Return true if the first parameter (assuming there was one) survives the
328 transformation intact and remains the first one. */
330 bool
331 ipa_param_adjustments::first_param_intact_p ()
333 return (!vec_safe_is_empty (m_adj_params)
334 && (*m_adj_params)[0].op == IPA_PARAM_OP_COPY
335 && (*m_adj_params)[0].base_index == 0);
338 /* Return true if we have to change what has formerly been a method into a
339 function. */
341 bool
342 ipa_param_adjustments::method2func_p (tree orig_type)
344 return ((TREE_CODE (orig_type) == METHOD_TYPE) && !first_param_intact_p ());
347 /* Given function type OLD_TYPE, return a new type derived from it after
348 performing all atored modifications. TYPE_ORIGINAL_P should be true when
349 OLD_TYPE refers to the type before any IPA transformations, as opposed to a
350 type that can be an intermediate one in between various IPA
351 transformations. */
353 tree
354 ipa_param_adjustments::build_new_function_type (tree old_type,
355 bool type_original_p)
357 auto_vec<tree,16> new_param_types, *new_param_types_p;
358 if (prototype_p (old_type))
360 auto_vec<tree, 16> otypes;
361 push_function_arg_types (&otypes, old_type);
362 fill_vector_of_new_param_types (&new_param_types, &otypes, m_adj_params,
363 !type_original_p);
364 new_param_types_p = &new_param_types;
366 else
367 new_param_types_p = NULL;
369 return build_adjusted_function_type (old_type, new_param_types_p,
370 method2func_p (old_type), m_skip_return);
373 /* Build variant of function decl ORIG_DECL which has no return value if
374 M_SKIP_RETURN is true and, if ORIG_DECL's types or parameters is known, has
375 this type adjusted as indicated in M_ADJ_PARAMS. Arguments from
376 DECL_ARGUMENTS list are not processed now, since they are linked by
377 TREE_CHAIN directly and not accessible in LTO during WPA. The caller is
378 responsible for eliminating them when clones are properly materialized. */
380 tree
381 ipa_param_adjustments::adjust_decl (tree orig_decl)
383 tree new_decl = copy_node (orig_decl);
384 tree orig_type = TREE_TYPE (orig_decl);
385 if (prototype_p (orig_type)
386 || (m_skip_return && !VOID_TYPE_P (TREE_TYPE (orig_type))))
388 tree new_type = build_new_function_type (orig_type, false);
389 TREE_TYPE (new_decl) = new_type;
391 if (method2func_p (orig_type))
392 DECL_VINDEX (new_decl) = NULL_TREE;
394 /* When signature changes, we need to clear builtin info. */
395 if (fndecl_built_in_p (new_decl))
396 set_decl_built_in_function (new_decl, NOT_BUILT_IN, 0);
398 DECL_VIRTUAL_P (new_decl) = 0;
399 DECL_LANG_SPECIFIC (new_decl) = NULL;
401 return new_decl;
404 /* Wrapper around get_base_ref_and_offset for cases interesting for IPA-SRA
405 transformations. Return true if EXPR has an interesting form and fill in
406 *BASE_P and *UNIT_OFFSET_P with the appropriate info. */
408 static bool
409 isra_get_ref_base_and_offset (tree expr, tree *base_p, unsigned *unit_offset_p)
411 HOST_WIDE_INT offset, size;
412 bool reverse;
413 tree base
414 = get_ref_base_and_extent_hwi (expr, &offset, &size, &reverse);
415 if (!base || size < 0)
416 return false;
418 if ((offset % BITS_PER_UNIT) != 0)
419 return false;
421 if (TREE_CODE (base) == MEM_REF)
423 poly_int64 plmoff = mem_ref_offset (base).force_shwi ();
424 HOST_WIDE_INT moff;
425 bool is_cst = plmoff.is_constant (&moff);
426 if (!is_cst)
427 return false;
428 offset += moff * BITS_PER_UNIT;
429 base = TREE_OPERAND (base, 0);
432 if (offset < 0 || (offset / BITS_PER_UNIT) > UINT_MAX)
433 return false;
435 *base_p = base;
436 *unit_offset_p = offset / BITS_PER_UNIT;
437 return true;
440 /* Return true if EXPR describes a transitive split (i.e. one that happened for
441 both the caller and the callee) as recorded in PERFORMED_SPLITS. In that
442 case, store index of the respective record in PERFORMED_SPLITS into
443 *SM_IDX_P and the unit offset from all handled components in EXPR into
444 *UNIT_OFFSET_P. */
446 static bool
447 transitive_split_p (vec<ipa_param_performed_split, va_gc> *performed_splits,
448 tree expr, unsigned *sm_idx_p, unsigned *unit_offset_p)
450 tree base;
451 if (!isra_get_ref_base_and_offset (expr, &base, unit_offset_p))
452 return false;
454 if (TREE_CODE (base) == SSA_NAME)
456 base = SSA_NAME_VAR (base);
457 if (!base)
458 return false;
461 unsigned len = vec_safe_length (performed_splits);
462 for (unsigned i = 0 ; i < len; i++)
464 ipa_param_performed_split *sm = &(*performed_splits)[i];
465 if (sm->dummy_decl == base)
467 *sm_idx_p = i;
468 return true;
471 return false;
474 /* Structure to hold declarations representing transitive IPA-SRA splits. In
475 essence, if we need to pass UNIT_OFFSET of a parameter which originally has
476 number BASE_INDEX, we should pass down REPL. */
478 struct transitive_split_map
480 tree repl;
481 unsigned base_index;
482 unsigned unit_offset;
485 /* If call STMT contains any parameters representing transitive splits as
486 described by PERFORMED_SPLITS, return the number of extra parameters that
487 were addded during clone materialization and fill in INDEX_MAP with adjusted
488 indices of corresponding original parameters and TRANS_MAP with description
489 of all transitive replacement descriptions. Otherwise return zero. */
491 static unsigned
492 init_transitive_splits (vec<ipa_param_performed_split, va_gc> *performed_splits,
493 gcall *stmt, vec <unsigned> *index_map,
494 auto_vec <transitive_split_map> *trans_map)
496 unsigned phony_arguments = 0;
497 unsigned stmt_idx = 0, base_index = 0;
498 unsigned nargs = gimple_call_num_args (stmt);
499 while (stmt_idx < nargs)
501 unsigned unit_offset_delta;
502 tree base_arg = gimple_call_arg (stmt, stmt_idx);
504 if (phony_arguments > 0)
505 index_map->safe_push (stmt_idx);
507 unsigned sm_idx;
508 stmt_idx++;
509 if (transitive_split_p (performed_splits, base_arg, &sm_idx,
510 &unit_offset_delta))
512 if (phony_arguments == 0)
513 /* We have optimistically avoided constructing index_map do far but
514 now it is clear it will be necessary, so let's create the easy
515 bit we skipped until now. */
516 for (unsigned k = 0; k < stmt_idx; k++)
517 index_map->safe_push (k);
519 tree dummy = (*performed_splits)[sm_idx].dummy_decl;
520 for (unsigned j = sm_idx; j < performed_splits->length (); j++)
522 ipa_param_performed_split *caller_split
523 = &(*performed_splits)[j];
524 if (caller_split->dummy_decl != dummy)
525 break;
527 tree arg = gimple_call_arg (stmt, stmt_idx);
528 struct transitive_split_map tsm;
529 tsm.repl = arg;
530 tsm.base_index = base_index;
531 if (caller_split->unit_offset >= unit_offset_delta)
533 tsm.unit_offset
534 = (caller_split->unit_offset - unit_offset_delta);
535 trans_map->safe_push (tsm);
538 phony_arguments++;
539 stmt_idx++;
542 base_index++;
544 return phony_arguments;
547 /* Modify actual arguments of a function call in statement STMT, assuming it
548 calls CALLEE_DECL. CALLER_ADJ must be the description of parameter
549 adjustments of the caller or NULL if there are none. Return the new
550 statement that replaced the old one. When invoked, cfun and
551 current_function_decl have to be set to the caller. */
553 gcall *
554 ipa_param_adjustments::modify_call (gcall *stmt,
555 vec<ipa_param_performed_split,
556 va_gc> *performed_splits,
557 tree callee_decl, bool update_references)
559 unsigned len = vec_safe_length (m_adj_params);
560 auto_vec<tree, 16> vargs (len);
561 tree old_decl = gimple_call_fndecl (stmt);
562 unsigned old_nargs = gimple_call_num_args (stmt);
563 auto_vec<bool, 16> kept (old_nargs);
564 kept.quick_grow_cleared (old_nargs);
566 auto_vec <unsigned, 16> index_map;
567 auto_vec <transitive_split_map> trans_map;
568 bool transitive_remapping = false;
570 if (performed_splits)
572 unsigned removed = init_transitive_splits (performed_splits,
573 stmt, &index_map, &trans_map);
574 if (removed > 0)
576 transitive_remapping = true;
577 old_nargs -= removed;
581 cgraph_node *current_node = cgraph_node::get (current_function_decl);
582 if (update_references)
583 current_node->remove_stmt_references (stmt);
585 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
586 gimple_stmt_iterator prev_gsi = gsi;
587 gsi_prev (&prev_gsi);
588 for (unsigned i = 0; i < len; i++)
590 ipa_adjusted_param *apm = &(*m_adj_params)[i];
591 if (apm->op == IPA_PARAM_OP_COPY)
593 unsigned index = apm->base_index;
594 if (index >= old_nargs)
595 /* Can happen if the original call has argument mismatch,
596 ignore. */
597 continue;
598 if (transitive_remapping)
599 index = index_map[apm->base_index];
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 transitive changes differently using the maps we
618 have created before. So look into them first. */
619 tree repl = NULL_TREE;
620 for (unsigned j = 0; j < trans_map.length (); j++)
621 if (trans_map[j].base_index == apm->base_index
622 && trans_map[j].unit_offset == apm->unit_offset)
624 repl = trans_map[j].repl;
625 break;
627 if (repl)
629 vargs.quick_push (repl);
630 continue;
633 unsigned index = apm->base_index;
634 if (index >= old_nargs)
635 /* Can happen if the original call has argument mismatch, ignore. */
636 continue;
637 if (transitive_remapping)
638 index = index_map[apm->base_index];
639 tree base = gimple_call_arg (stmt, index);
641 /* We create a new parameter out of the value of the old one, we can
642 do the following kind of transformations:
644 - A scalar passed by reference, potentially as a part of a larger
645 aggregate, is converted to a scalar passed by value.
647 - A part of an aggregate is passed instead of the whole aggregate. */
649 location_t loc = gimple_location (stmt);
650 tree off;
651 bool deref_base = false;
652 unsigned int deref_align = 0;
653 if (TREE_CODE (base) != ADDR_EXPR
654 && is_gimple_reg_type (TREE_TYPE (base)))
656 /* Detect type mismatches in calls in invalid programs and make a
657 poor attempt to gracefully convert them so that we don't ICE. */
658 if (!POINTER_TYPE_P (TREE_TYPE (base)))
659 base = force_value_to_type (ptr_type_node, base);
661 off = build_int_cst (apm->alias_ptr_type, apm->unit_offset);
663 else
665 bool addrof;
666 if (TREE_CODE (base) == ADDR_EXPR)
668 base = TREE_OPERAND (base, 0);
669 addrof = true;
671 else
672 addrof = false;
674 tree prev_base = base;
675 poly_int64 base_offset;
676 base = get_addr_base_and_unit_offset (base, &base_offset);
678 /* Aggregate arguments can have non-invariant addresses. */
679 if (!base)
681 base = build_fold_addr_expr (prev_base);
682 off = build_int_cst (apm->alias_ptr_type, apm->unit_offset);
684 else if (TREE_CODE (base) == MEM_REF)
686 if (!addrof)
688 deref_base = true;
689 deref_align = TYPE_ALIGN (TREE_TYPE (base));
691 off = build_int_cst (apm->alias_ptr_type,
692 base_offset + apm->unit_offset);
693 off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
694 off);
695 base = TREE_OPERAND (base, 0);
697 else
699 off = build_int_cst (apm->alias_ptr_type,
700 base_offset + apm->unit_offset);
701 base = build_fold_addr_expr (base);
705 tree type = apm->type;
706 unsigned int align;
707 unsigned HOST_WIDE_INT misalign;
709 if (deref_base)
711 align = deref_align;
712 misalign = 0;
714 else
716 get_pointer_alignment_1 (base, &align, &misalign);
717 /* All users must make sure that we can be optimistic when it
718 comes to alignment in this case (by inspecting the final users
719 of these new parameters). */
720 if (TYPE_ALIGN (type) > align)
721 align = TYPE_ALIGN (type);
723 misalign
724 += (offset_int::from (wi::to_wide (off), SIGNED).to_short_addr ()
725 * BITS_PER_UNIT);
726 misalign = misalign & (align - 1);
727 if (misalign != 0)
728 align = least_bit_hwi (misalign);
729 if (align < TYPE_ALIGN (type))
730 type = build_aligned_type (type, align);
731 base = force_gimple_operand_gsi (&gsi, base,
732 true, NULL, true, GSI_SAME_STMT);
733 tree expr = fold_build2_loc (loc, MEM_REF, type, base, off);
734 REF_REVERSE_STORAGE_ORDER (expr) = apm->reverse;
735 /* If expr is not a valid gimple call argument emit
736 a load into a temporary. */
737 if (is_gimple_reg_type (TREE_TYPE (expr)))
739 gimple *tem = gimple_build_assign (NULL_TREE, expr);
740 if (gimple_in_ssa_p (cfun))
742 gimple_set_vuse (tem, gimple_vuse (stmt));
743 expr = make_ssa_name (TREE_TYPE (expr), tem);
745 else
746 expr = create_tmp_reg (TREE_TYPE (expr));
747 gimple_assign_set_lhs (tem, expr);
748 gsi_insert_before (&gsi, tem, GSI_SAME_STMT);
750 vargs.quick_push (expr);
753 if (m_always_copy_start >= 0)
754 for (unsigned i = m_always_copy_start; i < old_nargs; i++)
755 vargs.safe_push (gimple_call_arg (stmt, i));
757 /* For optimized away parameters, add on the caller side
758 before the call
759 DEBUG D#X => parm_Y(D)
760 stmts and associate D#X with parm in decl_debug_args_lookup
761 vector to say for debug info that if parameter parm had been passed,
762 it would have value parm_Y(D). */
763 if (MAY_HAVE_DEBUG_BIND_STMTS && old_decl && callee_decl)
765 vec<tree, va_gc> **debug_args = NULL;
766 unsigned i = 0;
767 for (tree old_parm = DECL_ARGUMENTS (old_decl);
768 old_parm && i < old_nargs && ((int) i) < m_always_copy_start;
769 old_parm = DECL_CHAIN (old_parm), i++)
771 if (!is_gimple_reg (old_parm) || kept[i])
772 continue;
773 tree origin = DECL_ORIGIN (old_parm);
774 tree arg = gimple_call_arg (stmt, i);
776 if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
778 if (!fold_convertible_p (TREE_TYPE (origin), arg))
779 continue;
780 tree rhs1;
781 if (TREE_CODE (arg) == SSA_NAME
782 && gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg))
783 && (rhs1
784 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (arg)))
785 && useless_type_conversion_p (TREE_TYPE (origin),
786 TREE_TYPE (rhs1)))
787 arg = rhs1;
788 else
789 arg = fold_convert_loc (gimple_location (stmt),
790 TREE_TYPE (origin), arg);
792 if (debug_args == NULL)
793 debug_args = decl_debug_args_insert (callee_decl);
794 unsigned int ix;
795 tree ddecl = NULL_TREE;
796 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl); ix += 2)
797 if (ddecl == origin)
799 ddecl = (**debug_args)[ix + 1];
800 break;
802 if (ddecl == NULL)
804 ddecl = make_node (DEBUG_EXPR_DECL);
805 DECL_ARTIFICIAL (ddecl) = 1;
806 TREE_TYPE (ddecl) = TREE_TYPE (origin);
807 SET_DECL_MODE (ddecl, DECL_MODE (origin));
809 vec_safe_push (*debug_args, origin);
810 vec_safe_push (*debug_args, ddecl);
812 gimple *def_temp = gimple_build_debug_bind (ddecl,
813 unshare_expr (arg), stmt);
814 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
818 if (dump_file && (dump_flags & TDF_DETAILS))
820 fprintf (dump_file, "replacing stmt:");
821 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0);
824 gcall *new_stmt = gimple_build_call_vec (callee_decl, vargs);
826 if (tree lhs = gimple_call_lhs (stmt))
828 if (!m_skip_return)
829 gimple_call_set_lhs (new_stmt, lhs);
830 else if (TREE_CODE (lhs) == SSA_NAME)
832 /* LHS should now by a default-def SSA. Unfortunately default-def
833 SSA_NAMEs need a backing variable (or at least some code examining
834 SSAs assumes it is non-NULL). So we either have to re-use the
835 decl we have at hand or introdice a new one. */
836 tree repl = create_tmp_var (TREE_TYPE (lhs), "removed_return");
837 repl = get_or_create_ssa_default_def (cfun, repl);
838 SSA_NAME_IS_DEFAULT_DEF (repl) = true;
839 imm_use_iterator ui;
840 use_operand_p use_p;
841 gimple *using_stmt;
842 FOR_EACH_IMM_USE_STMT (using_stmt, ui, lhs)
844 FOR_EACH_IMM_USE_ON_STMT (use_p, ui)
846 SET_USE (use_p, repl);
848 update_stmt (using_stmt);
853 gimple_set_block (new_stmt, gimple_block (stmt));
854 if (gimple_has_location (stmt))
855 gimple_set_location (new_stmt, gimple_location (stmt));
856 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
857 gimple_call_copy_flags (new_stmt, stmt);
858 if (gimple_in_ssa_p (cfun))
859 gimple_move_vops (new_stmt, stmt);
861 if (dump_file && (dump_flags & TDF_DETAILS))
863 fprintf (dump_file, "with stmt:");
864 print_gimple_stmt (dump_file, new_stmt, 0);
865 fprintf (dump_file, "\n");
867 gsi_replace (&gsi, new_stmt, true);
868 if (update_references)
871 current_node->record_stmt_references (gsi_stmt (gsi));
872 gsi_prev (&gsi);
874 while (gsi_stmt (gsi) != gsi_stmt (prev_gsi));
875 return new_stmt;
878 /* Dump information contained in the object in textual form to F. */
880 void
881 ipa_param_adjustments::dump (FILE *f)
883 fprintf (f, " m_always_copy_start: %i\n", m_always_copy_start);
884 ipa_dump_adjusted_parameters (f, m_adj_params);
885 if (m_skip_return)
886 fprintf (f, " Will SKIP return.\n");
889 /* Dump information contained in the object in textual form to stderr. */
891 void
892 ipa_param_adjustments::debug ()
894 dump (stderr);
897 /* Register that REPLACEMENT should replace parameter described in APM and
898 optionally as DUMMY to mark transitive splits accross calls. */
900 void
901 ipa_param_body_adjustments::register_replacement (ipa_adjusted_param *apm,
902 tree replacement,
903 tree dummy)
905 gcc_checking_assert (apm->op == IPA_PARAM_OP_SPLIT
906 || apm->op == IPA_PARAM_OP_NEW);
907 gcc_checking_assert (!apm->prev_clone_adjustment);
908 ipa_param_body_replacement psr;
909 psr.base = m_oparms[apm->prev_clone_index];
910 psr.repl = replacement;
911 psr.dummy = dummy;
912 psr.unit_offset = apm->unit_offset;
913 m_replacements.safe_push (psr);
916 /* Copy or not, as appropriate given m_id and decl context, a pre-existing
917 PARM_DECL T so that it can be included in the parameters of the modified
918 function. */
920 tree
921 ipa_param_body_adjustments::carry_over_param (tree t)
923 tree new_parm;
924 if (m_id)
926 new_parm = remap_decl (t, m_id);
927 if (TREE_CODE (new_parm) != PARM_DECL)
928 new_parm = m_id->copy_decl (t, m_id);
930 else if (DECL_CONTEXT (t) != m_fndecl)
932 new_parm = copy_node (t);
933 DECL_CONTEXT (new_parm) = m_fndecl;
935 else
936 new_parm = t;
937 return new_parm;
940 /* Common initialization performed by all ipa_param_body_adjustments
941 constructors. OLD_FNDECL is the declaration we take original arguments
942 from, (it may be the same as M_FNDECL). VARS, if non-NULL, is a pointer to
943 a chained list of new local variables. TREE_MAP is the IPA-CP produced
944 mapping of trees to constants.
946 The function is rather long but it really onlu initializes all data members
947 of the class. It creates new param DECLs, finds their new types, */
949 void
950 ipa_param_body_adjustments::common_initialization (tree old_fndecl,
951 tree *vars,
952 vec<ipa_replace_map *,
953 va_gc> *tree_map)
955 push_function_arg_decls (&m_oparms, old_fndecl);
956 auto_vec<tree,16> otypes;
957 if (TYPE_ARG_TYPES (TREE_TYPE (old_fndecl)) != NULL_TREE)
958 push_function_arg_types (&otypes, TREE_TYPE (old_fndecl));
959 else
961 auto_vec<tree,16> oparms;
962 push_function_arg_decls (&oparms, old_fndecl);
963 unsigned ocount = oparms.length ();
964 otypes.reserve_exact (ocount);
965 for (unsigned i = 0; i < ocount; i++)
966 otypes.quick_push (TREE_TYPE (oparms[i]));
968 fill_vector_of_new_param_types (&m_new_types, &otypes, m_adj_params, true);
970 auto_vec<bool, 16> kept;
971 kept.reserve_exact (m_oparms.length ());
972 kept.quick_grow_cleared (m_oparms.length ());
973 auto_vec<tree, 16> isra_dummy_decls;
974 isra_dummy_decls.reserve_exact (m_oparms.length ());
975 isra_dummy_decls.quick_grow_cleared (m_oparms.length ());
977 unsigned adj_len = vec_safe_length (m_adj_params);
978 m_method2func = ((TREE_CODE (TREE_TYPE (m_fndecl)) == METHOD_TYPE)
979 && (adj_len == 0
980 || (*m_adj_params)[0].op != IPA_PARAM_OP_COPY
981 || (*m_adj_params)[0].base_index != 0));
983 /* The main job of the this function is to go over the vector of adjusted
984 parameters and create declarations or find corresponding old ones and push
985 them to m_new_decls. For IPA-SRA replacements it also creates
986 corresponding m_id->dst_node->clone.performed_splits entries. */
988 m_new_decls.reserve_exact (adj_len);
989 for (unsigned i = 0; i < adj_len ; i++)
991 ipa_adjusted_param *apm = &(*m_adj_params)[i];
992 unsigned prev_index = apm->prev_clone_index;
993 tree new_parm;
994 if (apm->op == IPA_PARAM_OP_COPY
995 || apm->prev_clone_adjustment)
997 kept[prev_index] = true;
998 new_parm = carry_over_param (m_oparms[prev_index]);
999 m_new_decls.quick_push (new_parm);
1001 else if (apm->op == IPA_PARAM_OP_NEW
1002 || apm->op == IPA_PARAM_OP_SPLIT)
1004 tree new_type = m_new_types[i];
1005 gcc_checking_assert (new_type);
1006 new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
1007 new_type);
1008 const char *prefix = ipa_param_prefixes[apm->param_prefix_index];
1009 DECL_NAME (new_parm) = create_tmp_var_name (prefix);
1010 DECL_ARTIFICIAL (new_parm) = 1;
1011 DECL_ARG_TYPE (new_parm) = new_type;
1012 DECL_CONTEXT (new_parm) = m_fndecl;
1013 TREE_USED (new_parm) = 1;
1014 DECL_IGNORED_P (new_parm) = 1;
1015 /* We assume all newly created arguments are not addressable. */
1016 if (TREE_CODE (new_type) == COMPLEX_TYPE
1017 || TREE_CODE (new_type) == VECTOR_TYPE)
1018 DECL_GIMPLE_REG_P (new_parm) = 1;
1019 layout_decl (new_parm, 0);
1020 m_new_decls.quick_push (new_parm);
1022 if (apm->op == IPA_PARAM_OP_SPLIT)
1024 m_split_modifications_p = true;
1026 if (m_id)
1028 tree dummy_decl;
1029 if (!isra_dummy_decls[prev_index])
1031 dummy_decl = copy_decl_to_var (m_oparms[prev_index],
1032 m_id);
1033 /* Any attempt to remap this dummy in this particular
1034 instance of clone materialization should yield
1035 itself. */
1036 insert_decl_map (m_id, dummy_decl, dummy_decl);
1038 DECL_CHAIN (dummy_decl) = *vars;
1039 *vars = dummy_decl;
1040 isra_dummy_decls[prev_index] = dummy_decl;
1042 else
1043 dummy_decl = isra_dummy_decls[prev_index];
1045 register_replacement (apm, new_parm, dummy_decl);
1046 ipa_param_performed_split ps;
1047 ps.dummy_decl = dummy_decl;
1048 ps.unit_offset = apm->unit_offset;
1049 vec_safe_push (m_id->dst_node->clone.performed_splits, ps);
1051 else
1052 register_replacement (apm, new_parm);
1055 else
1056 gcc_unreachable ();
1060 /* As part of body modifications, we will also have to replace remaining uses
1061 of remaining uses of removed PARM_DECLs (which do not however use the
1062 initial value) with their VAR_DECL copies.
1064 We do this differently with and without m_id. With m_id, we rely on its
1065 mapping and create a replacement straight away. Without it, we have our
1066 own mechanism for which we have to populate m_removed_decls vector. Just
1067 don't mix them, that is why you should not call
1068 replace_removed_params_ssa_names or perform_cfun_body_modifications when
1069 you construct with ID not equal to NULL. */
1071 unsigned op_len = m_oparms.length ();
1072 for (unsigned i = 0; i < op_len; i++)
1073 if (!kept[i])
1075 if (m_id)
1077 if (!m_id->decl_map->get (m_oparms[i]))
1079 /* TODO: Perhaps at least aggregate-type params could re-use
1080 their isra_dummy_decl here? */
1081 tree var = copy_decl_to_var (m_oparms[i], m_id);
1082 insert_decl_map (m_id, m_oparms[i], var);
1083 /* Declare this new variable. */
1084 DECL_CHAIN (var) = *vars;
1085 *vars = var;
1088 else
1090 m_removed_decls.safe_push (m_oparms[i]);
1091 m_removed_map.put (m_oparms[i], m_removed_decls.length () - 1);
1095 if (!MAY_HAVE_DEBUG_STMTS)
1096 return;
1098 /* Finally, when generating debug info, we fill vector m_reset_debug_decls
1099 with removed parameters declarations. We do this in order to re-map their
1100 debug bind statements and create debug decls for them. */
1102 if (tree_map)
1104 /* Do not output debuginfo for parameter declarations as if they vanished
1105 when they were in fact replaced by a constant. */
1106 auto_vec <int, 16> index_mapping;
1107 bool need_remap = false;
1109 if (m_id && m_id->src_node->clone.param_adjustments)
1111 ipa_param_adjustments *prev_adjustments
1112 = m_id->src_node->clone.param_adjustments;
1113 prev_adjustments->get_updated_indices (&index_mapping);
1114 need_remap = true;
1117 for (unsigned i = 0; i < tree_map->length (); i++)
1119 int parm_num = (*tree_map)[i]->parm_num;
1120 gcc_assert (parm_num >= 0);
1121 if (need_remap)
1122 parm_num = index_mapping[parm_num];
1123 kept[parm_num] = true;
1127 for (unsigned i = 0; i < op_len; i++)
1128 if (!kept[i] && is_gimple_reg (m_oparms[i]))
1129 m_reset_debug_decls.safe_push (m_oparms[i]);
1132 /* Constructor of ipa_param_body_adjustments from a simple list of
1133 modifications to parameters listed in ADJ_PARAMS which will prepare ground
1134 for modification of parameters of fndecl. Return value of the function will
1135 not be removed and the object will assume it does not run as a part of
1136 tree-function_versioning. */
1138 ipa_param_body_adjustments
1139 ::ipa_param_body_adjustments (vec<ipa_adjusted_param, va_gc> *adj_params,
1140 tree fndecl)
1141 : m_adj_params (adj_params), m_adjustments (NULL), m_reset_debug_decls (),
1142 m_split_modifications_p (false), m_fndecl (fndecl), m_id (NULL),
1143 m_oparms (), m_new_decls (), m_new_types (), m_replacements (),
1144 m_removed_decls (), m_removed_map (), m_method2func (false)
1146 common_initialization (fndecl, NULL, NULL);
1149 /* Constructor of ipa_param_body_adjustments from ipa_param_adjustments in
1150 ADJUSTMENTS which will prepare ground for modification of parameters of
1151 fndecl. The object will assume it does not run as a part of
1152 tree-function_versioning. */
1154 ipa_param_body_adjustments
1155 ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
1156 tree fndecl)
1157 : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
1158 m_reset_debug_decls (), m_split_modifications_p (false), m_fndecl (fndecl),
1159 m_id (NULL), m_oparms (), m_new_decls (), m_new_types (),
1160 m_replacements (), m_removed_decls (), m_removed_map (),
1161 m_method2func (false)
1163 common_initialization (fndecl, NULL, NULL);
1166 /* Constructor of ipa_param_body_adjustments which sets it up as a part of
1167 running tree_function_versioning. Planned modifications to the function are
1168 in ADJUSTMENTS. FNDECL designates the new function clone which is being
1169 modified. OLD_FNDECL is the function of which FNDECL is a clone (and which
1170 at the time of invocation still share DECL_ARGUMENTS). ID is the
1171 copy_body_data structure driving the wholy body copying process. VARS is a
1172 pointer to the head of the list of new local variables, TREE_MAP is the map
1173 that drives tree substitution in the cloning process. */
1175 ipa_param_body_adjustments
1176 ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
1177 tree fndecl, tree old_fndecl,
1178 copy_body_data *id, tree *vars,
1179 vec<ipa_replace_map *, va_gc> *tree_map)
1180 : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
1181 m_reset_debug_decls (), m_split_modifications_p (false), m_fndecl (fndecl),
1182 m_id (id), m_oparms (), m_new_decls (), m_new_types (), m_replacements (),
1183 m_removed_decls (), m_removed_map (), m_method2func (false)
1185 common_initialization (old_fndecl, vars, tree_map);
1188 /* Chain new param decls up and return them. */
1190 tree
1191 ipa_param_body_adjustments::get_new_param_chain ()
1193 tree result;
1194 tree *link = &result;
1196 unsigned len = vec_safe_length (m_adj_params);
1197 for (unsigned i = 0; i < len; i++)
1199 tree new_decl = m_new_decls[i];
1200 *link = new_decl;
1201 link = &DECL_CHAIN (new_decl);
1203 *link = NULL_TREE;
1204 return result;
1207 /* Modify the function parameters FNDECL and its type according to the plan in
1208 ADJUSTMENTS. This function needs to be called when the decl has not already
1209 been processed with ipa_param_adjustments::adjust_decl, otherwise just
1210 seting DECL_ARGUMENTS to whatever get_new_param_chain will do is enough. */
1212 void
1213 ipa_param_body_adjustments::modify_formal_parameters ()
1215 tree orig_type = TREE_TYPE (m_fndecl);
1216 DECL_ARGUMENTS (m_fndecl) = get_new_param_chain ();
1218 /* When signature changes, we need to clear builtin info. */
1219 if (fndecl_built_in_p (m_fndecl))
1220 set_decl_built_in_function (m_fndecl, NOT_BUILT_IN, 0);
1222 /* At this point, removing return value is only implemented when going
1223 through tree_function_versioning, not when modifying function body
1224 directly. */
1225 gcc_assert (!m_adjustments || !m_adjustments->m_skip_return);
1226 tree new_type = build_adjusted_function_type (orig_type, &m_new_types,
1227 m_method2func, false);
1229 TREE_TYPE (m_fndecl) = new_type;
1230 DECL_VIRTUAL_P (m_fndecl) = 0;
1231 DECL_LANG_SPECIFIC (m_fndecl) = NULL;
1232 if (m_method2func)
1233 DECL_VINDEX (m_fndecl) = NULL_TREE;
1236 /* Given BASE and UNIT_OFFSET, find the corresponding record among replacement
1237 structures. */
1239 ipa_param_body_replacement *
1240 ipa_param_body_adjustments::lookup_replacement_1 (tree base,
1241 unsigned unit_offset)
1243 unsigned int len = m_replacements.length ();
1244 for (unsigned i = 0; i < len; i++)
1246 ipa_param_body_replacement *pbr = &m_replacements[i];
1248 if (pbr->base == base
1249 && (pbr->unit_offset == unit_offset))
1250 return pbr;
1252 return NULL;
1255 /* Given BASE and UNIT_OFFSET, find the corresponding replacement expression
1256 and return it, assuming it is known it does not hold value by reference or
1257 in reverse storage order. */
1259 tree
1260 ipa_param_body_adjustments::lookup_replacement (tree base, unsigned unit_offset)
1262 ipa_param_body_replacement *pbr = lookup_replacement_1 (base, unit_offset);
1263 if (!pbr)
1264 return NULL;
1265 return pbr->repl;
1268 /* If T is an SSA_NAME, return NULL if it is not a default def or
1269 return its base variable if it is. If IGNORE_DEFAULT_DEF is true,
1270 the base variable is always returned, regardless if it is a default
1271 def. Return T if it is not an SSA_NAME. */
1273 static tree
1274 get_ssa_base_param (tree t, bool ignore_default_def)
1276 if (TREE_CODE (t) == SSA_NAME)
1278 if (ignore_default_def || SSA_NAME_IS_DEFAULT_DEF (t))
1279 return SSA_NAME_VAR (t);
1280 else
1281 return NULL_TREE;
1283 return t;
1286 /* Given an expression, return the structure describing how it should be
1287 replaced if it accesses a part of a split parameter or NULL otherwise.
1289 Do not free the result, it will be deallocated when the object is destroyed.
1291 If IGNORE_DEFAULT_DEF is cleared, consider only SSA_NAMEs of PARM_DECLs
1292 which are default definitions, if set, consider all SSA_NAMEs of
1293 PARM_DECLs. */
1295 ipa_param_body_replacement *
1296 ipa_param_body_adjustments::get_expr_replacement (tree expr,
1297 bool ignore_default_def)
1299 tree base;
1300 unsigned unit_offset;
1302 if (!isra_get_ref_base_and_offset (expr, &base, &unit_offset))
1303 return NULL;
1305 base = get_ssa_base_param (base, ignore_default_def);
1306 if (!base || TREE_CODE (base) != PARM_DECL)
1307 return NULL;
1308 return lookup_replacement_1 (base, unit_offset);
1311 /* Given OLD_DECL, which is a PARM_DECL of a parameter that is being removed
1312 (which includes it being split or replaced), return a new variable that
1313 should be used for any SSA names that will remain in the function that
1314 previously belonged to OLD_DECL. */
1316 tree
1317 ipa_param_body_adjustments::get_replacement_ssa_base (tree old_decl)
1319 unsigned *idx = m_removed_map.get (old_decl);
1320 if (!idx)
1321 return NULL;
1323 tree repl;
1324 if (TREE_CODE (m_removed_decls[*idx]) == PARM_DECL)
1326 gcc_assert (m_removed_decls[*idx] == old_decl);
1327 repl = copy_var_decl (old_decl, DECL_NAME (old_decl),
1328 TREE_TYPE (old_decl));
1329 m_removed_decls[*idx] = repl;
1331 else
1332 repl = m_removed_decls[*idx];
1333 return repl;
1336 /* If OLD_NAME, which is being defined by statement STMT, is an SSA_NAME of a
1337 parameter which is to be removed because its value is not used, create a new
1338 SSA_NAME relating to a replacement VAR_DECL, replace all uses of the
1339 original with it and return it. If there is no need to re-map, return NULL.
1340 ADJUSTMENTS is a pointer to a vector of IPA-SRA adjustments. */
1342 tree
1343 ipa_param_body_adjustments::replace_removed_params_ssa_names (tree old_name,
1344 gimple *stmt)
1346 gcc_assert (!m_id);
1347 if (TREE_CODE (old_name) != SSA_NAME)
1348 return NULL;
1350 tree decl = SSA_NAME_VAR (old_name);
1351 if (decl == NULL_TREE
1352 || TREE_CODE (decl) != PARM_DECL)
1353 return NULL;
1355 tree repl = get_replacement_ssa_base (decl);
1356 if (!repl)
1357 return NULL;
1359 tree new_name = make_ssa_name (repl, stmt);
1360 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name)
1361 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (old_name);
1363 if (dump_file && (dump_flags & TDF_DETAILS))
1365 fprintf (dump_file, "replacing an SSA name of a removed param ");
1366 print_generic_expr (dump_file, old_name);
1367 fprintf (dump_file, " with ");
1368 print_generic_expr (dump_file, new_name);
1369 fprintf (dump_file, "\n");
1372 replace_uses_by (old_name, new_name);
1373 return new_name;
1376 /* If the expression *EXPR_P should be replaced, do so. CONVERT specifies
1377 whether the function should care about type incompatibility of the current
1378 and new expressions. If it is false, the function will leave
1379 incompatibility issues to the caller - note that when the function
1380 encounters a BIT_FIELD_REF, IMAGPART_EXPR or REALPART_EXPR, it will modify
1381 their bases instead of the expressions themselves and then also performs any
1382 necessary conversions. */
1384 bool
1385 ipa_param_body_adjustments::modify_expression (tree *expr_p, bool convert)
1387 tree expr = *expr_p;
1389 if (TREE_CODE (expr) == BIT_FIELD_REF
1390 || TREE_CODE (expr) == IMAGPART_EXPR
1391 || TREE_CODE (expr) == REALPART_EXPR)
1393 expr_p = &TREE_OPERAND (expr, 0);
1394 expr = *expr_p;
1395 convert = true;
1398 ipa_param_body_replacement *pbr = get_expr_replacement (expr, false);
1399 if (!pbr)
1400 return false;
1402 tree repl = pbr->repl;
1403 if (dump_file && (dump_flags & TDF_DETAILS))
1405 fprintf (dump_file, "About to replace expr ");
1406 print_generic_expr (dump_file, expr);
1407 fprintf (dump_file, " with ");
1408 print_generic_expr (dump_file, repl);
1409 fprintf (dump_file, "\n");
1412 if (convert && !useless_type_conversion_p (TREE_TYPE (expr),
1413 TREE_TYPE (repl)))
1415 tree vce = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expr), repl);
1416 *expr_p = vce;
1418 else
1419 *expr_p = repl;
1420 return true;
1423 /* If the assignment statement STMT contains any expressions that need to
1424 replaced with a different one as noted by ADJUSTMENTS, do so. Handle any
1425 potential type incompatibilities. If any conversion sttements have to be
1426 pre-pended to STMT, they will be added to EXTRA_STMTS. Return true iff the
1427 statement was modified. */
1429 bool
1430 ipa_param_body_adjustments::modify_assignment (gimple *stmt,
1431 gimple_seq *extra_stmts)
1433 tree *lhs_p, *rhs_p;
1434 bool any;
1436 if (!gimple_assign_single_p (stmt))
1437 return false;
1439 rhs_p = gimple_assign_rhs1_ptr (stmt);
1440 lhs_p = gimple_assign_lhs_ptr (stmt);
1442 any = modify_expression (lhs_p, false);
1443 any |= modify_expression (rhs_p, false);
1444 if (any
1445 && !useless_type_conversion_p (TREE_TYPE (*lhs_p), TREE_TYPE (*rhs_p)))
1447 if (TREE_CODE (*rhs_p) == CONSTRUCTOR)
1449 /* V_C_Es of constructors can cause trouble (PR 42714). */
1450 if (is_gimple_reg_type (TREE_TYPE (*lhs_p)))
1451 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
1452 else
1453 *rhs_p = build_constructor (TREE_TYPE (*lhs_p),
1454 NULL);
1456 else
1458 tree new_rhs = fold_build1_loc (gimple_location (stmt),
1459 VIEW_CONVERT_EXPR, TREE_TYPE (*lhs_p),
1460 *rhs_p);
1461 tree tmp = force_gimple_operand (new_rhs, extra_stmts, true,
1462 NULL_TREE);
1463 gimple_assign_set_rhs1 (stmt, tmp);
1465 return true;
1468 return any;
1471 /* Data passed to remap_split_decl_to_dummy through walk_tree. */
1473 struct simple_tree_swap_info
1475 /* Change FROM to TO. */
1476 tree from, to;
1477 /* And set DONE to true when doing so. */
1478 bool done;
1481 /* Simple remapper to remap a split parameter to the same expression based on a
1482 special dummy decl so that edge redirections can detect transitive splitting
1483 and finish them. */
1485 static tree
1486 remap_split_decl_to_dummy (tree *tp, int *walk_subtrees, void *data)
1488 tree t = *tp;
1490 if (DECL_P (t) || TREE_CODE (t) == SSA_NAME)
1492 struct simple_tree_swap_info *swapinfo
1493 = (struct simple_tree_swap_info *) data;
1494 if (t == swapinfo->from
1495 || (TREE_CODE (t) == SSA_NAME
1496 && SSA_NAME_VAR (t) == swapinfo->from))
1498 *tp = swapinfo->to;
1499 swapinfo->done = true;
1501 *walk_subtrees = 0;
1503 else if (TYPE_P (t))
1504 *walk_subtrees = 0;
1505 else
1506 *walk_subtrees = 1;
1507 return NULL_TREE;
1511 /* If the call statement pointed at by STMT_P contains any expressions that
1512 need to replaced with a different one as noted by ADJUSTMENTS, do so. f the
1513 statement needs to be rebuilt, do so. Return true if any modifications have
1514 been performed.
1516 If the method is invoked as a part of IPA clone materialization and if any
1517 parameter split is transitive, i.e. it applies to the functin that is being
1518 modified and also to the callee of the statement, replace the parameter
1519 passed to old callee with an equivalent expression based on a dummy decl
1520 followed by PARM_DECLs representing the actual replacements. The actual
1521 replacements will be then converted into SSA_NAMEs and then
1522 ipa_param_adjustments::modify_call will find the appropriate ones and leave
1523 only those in the call. */
1525 bool
1526 ipa_param_body_adjustments::modify_call_stmt (gcall **stmt_p)
1528 gcall *stmt = *stmt_p;
1529 auto_vec <unsigned, 4> pass_through_args;
1530 auto_vec <unsigned, 4> pass_through_pbr_indices;
1532 if (m_split_modifications_p && m_id)
1534 for (unsigned i = 0; i < gimple_call_num_args (stmt); i++)
1536 tree t = gimple_call_arg (stmt, i);
1537 gcc_assert (TREE_CODE (t) != BIT_FIELD_REF
1538 && TREE_CODE (t) != IMAGPART_EXPR
1539 && TREE_CODE (t) != REALPART_EXPR);
1541 tree base;
1542 unsigned unit_offset;
1543 if (!isra_get_ref_base_and_offset (t, &base, &unit_offset))
1544 continue;
1546 bool by_ref = false;
1547 if (TREE_CODE (base) == SSA_NAME)
1549 if (!SSA_NAME_IS_DEFAULT_DEF (base))
1550 continue;
1551 base = SSA_NAME_VAR (base);
1552 gcc_checking_assert (base);
1553 by_ref = true;
1555 if (TREE_CODE (base) != PARM_DECL)
1556 continue;
1558 bool base_among_replacements = false;
1559 unsigned j, repl_list_len = m_replacements.length ();
1560 for (j = 0; j < repl_list_len; j++)
1562 ipa_param_body_replacement *pbr = &m_replacements[j];
1563 if (pbr->base == base)
1565 base_among_replacements = true;
1566 break;
1569 if (!base_among_replacements)
1570 continue;
1572 /* We still have to distinguish between an end-use that we have to
1573 transform now and a pass-through, which happens in the following
1574 two cases. */
1576 /* TODO: After we adjust ptr_parm_has_nonarg_uses to also consider
1577 &MEM_REF[ssa_name + offset], we will also have to detect that case
1578 here. */
1580 if (TREE_CODE (t) == SSA_NAME
1581 && SSA_NAME_IS_DEFAULT_DEF (t)
1582 && SSA_NAME_VAR (t)
1583 && TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL)
1585 /* This must be a by_reference pass-through. */
1586 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
1587 pass_through_args.safe_push (i);
1588 pass_through_pbr_indices.safe_push (j);
1590 else if (!by_ref && AGGREGATE_TYPE_P (TREE_TYPE (t)))
1592 /* Currently IPA-SRA guarantees the aggregate access type
1593 exactly matches in this case. So if it does not match, it is
1594 a pass-through argument that will be sorted out at edge
1595 redirection time. */
1596 ipa_param_body_replacement *pbr
1597 = lookup_replacement_1 (base, unit_offset);
1599 if (!pbr
1600 || (TYPE_MAIN_VARIANT (TREE_TYPE (t))
1601 != TYPE_MAIN_VARIANT (TREE_TYPE (pbr->repl))))
1603 pass_through_args.safe_push (i);
1604 pass_through_pbr_indices.safe_push (j);
1610 unsigned nargs = gimple_call_num_args (stmt);
1611 if (!pass_through_args.is_empty ())
1613 auto_vec<tree, 16> vargs;
1614 unsigned pt_idx = 0;
1615 for (unsigned i = 0; i < nargs; i++)
1617 if (pt_idx < pass_through_args.length ()
1618 && i == pass_through_args[pt_idx])
1620 unsigned j = pass_through_pbr_indices[pt_idx];
1621 pt_idx++;
1622 tree base = m_replacements[j].base;
1624 /* Map base will get mapped to the special transitive-isra marker
1625 dummy decl. */
1626 struct simple_tree_swap_info swapinfo;
1627 swapinfo.from = base;
1628 swapinfo.to = m_replacements[j].dummy;
1629 swapinfo.done = false;
1630 tree arg = gimple_call_arg (stmt, i);
1631 walk_tree (&arg, remap_split_decl_to_dummy, &swapinfo, NULL);
1632 gcc_assert (swapinfo.done);
1633 vargs.safe_push (arg);
1634 /* Now let's push all replacements pertaining to this parameter
1635 so that all gimple register ones get correct SSA_NAMES. Edge
1636 redirection will weed out the dummy argument as well as all
1637 unused replacements later. */
1638 unsigned int repl_list_len = m_replacements.length ();
1639 for (; j < repl_list_len; j++)
1641 if (m_replacements[j].base != base)
1642 break;
1643 vargs.safe_push (m_replacements[j].repl);
1646 else
1648 tree t = gimple_call_arg (stmt, i);
1649 modify_expression (&t, true);
1650 vargs.safe_push (t);
1653 gcall *new_stmt = gimple_build_call_vec (gimple_call_fn (stmt), vargs);
1654 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
1655 gimple_call_copy_flags (new_stmt, stmt);
1656 if (tree lhs = gimple_call_lhs (stmt))
1658 modify_expression (&lhs, false);
1659 gimple_call_set_lhs (new_stmt, lhs);
1661 *stmt_p = new_stmt;
1662 return true;
1665 /* Otherwise, no need to rebuild the statement, let's just modify arguments
1666 and the LHS if/as appropriate. */
1667 bool modified = false;
1668 for (unsigned i = 0; i < nargs; i++)
1670 tree *t = gimple_call_arg_ptr (stmt, i);
1671 modified |= modify_expression (t, true);
1674 if (gimple_call_lhs (stmt))
1676 tree *t = gimple_call_lhs_ptr (stmt);
1677 modified |= modify_expression (t, false);
1680 return modified;
1683 /* If the statement STMT contains any expressions that need to replaced with a
1684 different one as noted by ADJUSTMENTS, do so. Handle any potential type
1685 incompatibilities. If any conversion sttements have to be pre-pended to
1686 STMT, they will be added to EXTRA_STMTS. Return true iff the statement was
1687 modified. */
1689 bool
1690 ipa_param_body_adjustments::modify_gimple_stmt (gimple **stmt,
1691 gimple_seq *extra_stmts)
1693 bool modified = false;
1694 tree *t;
1696 switch (gimple_code (*stmt))
1698 case GIMPLE_RETURN:
1699 t = gimple_return_retval_ptr (as_a <greturn *> (*stmt));
1700 if (m_adjustments && m_adjustments->m_skip_return)
1701 *t = NULL_TREE;
1702 else if (*t != NULL_TREE)
1703 modified |= modify_expression (t, true);
1704 break;
1706 case GIMPLE_ASSIGN:
1707 modified |= modify_assignment (*stmt, extra_stmts);
1708 break;
1710 case GIMPLE_CALL:
1711 modified |= modify_call_stmt ((gcall **) stmt);
1712 break;
1714 case GIMPLE_ASM:
1716 gasm *asm_stmt = as_a <gasm *> (*stmt);
1717 for (unsigned i = 0; i < gimple_asm_ninputs (asm_stmt); i++)
1719 t = &TREE_VALUE (gimple_asm_input_op (asm_stmt, i));
1720 modified |= modify_expression (t, true);
1722 for (unsigned i = 0; i < gimple_asm_noutputs (asm_stmt); i++)
1724 t = &TREE_VALUE (gimple_asm_output_op (asm_stmt, i));
1725 modified |= modify_expression (t, false);
1728 break;
1730 default:
1731 break;
1733 return modified;
1737 /* Traverse body of the current function and perform the requested adjustments
1738 on its statements. Return true iff the CFG has been changed. */
1740 bool
1741 ipa_param_body_adjustments::modify_cfun_body ()
1743 bool cfg_changed = false;
1744 basic_block bb;
1746 FOR_EACH_BB_FN (bb, cfun)
1748 gimple_stmt_iterator gsi;
1750 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1752 gphi *phi = as_a <gphi *> (gsi_stmt (gsi));
1753 tree new_lhs, old_lhs = gimple_phi_result (phi);
1754 new_lhs = replace_removed_params_ssa_names (old_lhs, phi);
1755 if (new_lhs)
1757 gimple_phi_set_result (phi, new_lhs);
1758 release_ssa_name (old_lhs);
1762 gsi = gsi_start_bb (bb);
1763 while (!gsi_end_p (gsi))
1765 gimple *stmt = gsi_stmt (gsi);
1766 gimple *stmt_copy = stmt;
1767 gimple_seq extra_stmts = NULL;
1768 bool modified = modify_gimple_stmt (&stmt, &extra_stmts);
1769 if (stmt != stmt_copy)
1771 gcc_checking_assert (modified);
1772 gsi_replace (&gsi, stmt, false);
1774 if (!gimple_seq_empty_p (extra_stmts))
1775 gsi_insert_seq_before (&gsi, extra_stmts, GSI_SAME_STMT);
1777 def_operand_p defp;
1778 ssa_op_iter iter;
1779 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF)
1781 tree old_def = DEF_FROM_PTR (defp);
1782 if (tree new_def = replace_removed_params_ssa_names (old_def,
1783 stmt))
1785 SET_DEF (defp, new_def);
1786 release_ssa_name (old_def);
1787 modified = true;
1791 if (modified)
1793 update_stmt (stmt);
1794 if (maybe_clean_eh_stmt (stmt)
1795 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
1796 cfg_changed = true;
1798 gsi_next (&gsi);
1802 return cfg_changed;
1805 /* Call gimple_debug_bind_reset_value on all debug statements describing
1806 gimple register parameters that are being removed or replaced. */
1808 void
1809 ipa_param_body_adjustments::reset_debug_stmts ()
1811 int i, len;
1812 gimple_stmt_iterator *gsip = NULL, gsi;
1814 if (MAY_HAVE_DEBUG_STMTS && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
1816 gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1817 gsip = &gsi;
1819 len = m_reset_debug_decls.length ();
1820 for (i = 0; i < len; i++)
1822 imm_use_iterator ui;
1823 gimple *stmt;
1824 gdebug *def_temp;
1825 tree name, vexpr, copy = NULL_TREE;
1826 use_operand_p use_p;
1827 tree decl = m_reset_debug_decls[i];
1829 gcc_checking_assert (is_gimple_reg (decl));
1830 name = ssa_default_def (cfun, decl);
1831 vexpr = NULL;
1832 if (name)
1833 FOR_EACH_IMM_USE_STMT (stmt, ui, name)
1835 if (gimple_clobber_p (stmt))
1837 gimple_stmt_iterator cgsi = gsi_for_stmt (stmt);
1838 unlink_stmt_vdef (stmt);
1839 gsi_remove (&cgsi, true);
1840 release_defs (stmt);
1841 continue;
1843 /* All other users must have been removed by function body
1844 modification. */
1845 gcc_assert (is_gimple_debug (stmt));
1846 if (vexpr == NULL && gsip != NULL)
1848 vexpr = make_node (DEBUG_EXPR_DECL);
1849 def_temp = gimple_build_debug_source_bind (vexpr, decl, NULL);
1850 DECL_ARTIFICIAL (vexpr) = 1;
1851 TREE_TYPE (vexpr) = TREE_TYPE (name);
1852 SET_DECL_MODE (vexpr, DECL_MODE (decl));
1853 gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
1855 if (vexpr)
1857 FOR_EACH_IMM_USE_ON_STMT (use_p, ui)
1858 SET_USE (use_p, vexpr);
1860 else
1861 gimple_debug_bind_reset_value (stmt);
1862 update_stmt (stmt);
1864 /* Create a VAR_DECL for debug info purposes. */
1865 if (!DECL_IGNORED_P (decl))
1867 copy = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1868 VAR_DECL, DECL_NAME (decl),
1869 TREE_TYPE (decl));
1870 if (DECL_PT_UID_SET_P (decl))
1871 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
1872 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
1873 TREE_READONLY (copy) = TREE_READONLY (decl);
1874 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
1875 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
1876 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
1877 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
1878 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
1879 DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
1880 SET_DECL_RTL (copy, 0);
1881 TREE_USED (copy) = 1;
1882 DECL_CONTEXT (copy) = current_function_decl;
1883 add_local_decl (cfun, copy);
1884 DECL_CHAIN (copy)
1885 = BLOCK_VARS (DECL_INITIAL (current_function_decl));
1886 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = copy;
1888 if (gsip != NULL && copy && target_for_debug_bind (decl))
1890 gcc_assert (TREE_CODE (decl) == PARM_DECL);
1891 if (vexpr)
1892 def_temp = gimple_build_debug_bind (copy, vexpr, NULL);
1893 else
1894 def_temp = gimple_build_debug_source_bind (copy, decl,
1895 NULL);
1896 gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
1901 /* Perform all necessary body changes to change signature, body and debug info
1902 of fun according to adjustments passed at construction. Return true if CFG
1903 was changed in any way. The main entry point for modification of standalone
1904 functions that is not part of IPA clone materialization. */
1906 bool
1907 ipa_param_body_adjustments::perform_cfun_body_modifications ()
1909 bool cfg_changed;
1910 modify_formal_parameters ();
1911 cfg_changed = modify_cfun_body ();
1912 reset_debug_stmts ();
1914 return cfg_changed;