hppa: Fix LO_SUM DLTIND14R address support in PRINT_OPERAND_ADDRESS
[official-gcc.git] / gcc / ipa-param-manipulation.cc
blob3e0df6a6f77a6187fdaf1b92bc841bc6247a10fd
1 /* Manipulation of formal and actual parameters of functions and function
2 calls.
3 Copyright (C) 2017-2024 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #define INCLUDE_ALGORITHM
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "ssa.h"
29 #include "cgraph.h"
30 #include "fold-const.h"
31 #include "tree-eh.h"
32 #include "stor-layout.h"
33 #include "gimplify.h"
34 #include "gimple-iterator.h"
35 #include "gimplify-me.h"
36 #include "tree-cfg.h"
37 #include "tree-dfa.h"
38 #include "ipa-param-manipulation.h"
39 #include "print-tree.h"
40 #include "gimple-pretty-print.h"
41 #include "builtins.h"
42 #include "tree-ssa.h"
43 #include "tree-inline.h"
44 #include "alloc-pool.h"
45 #include "symbol-summary.h"
46 #include "symtab-clones.h"
47 #include "tree-phinodes.h"
48 #include "cfgexpand.h"
49 #include "attribs.h"
50 #include "sreal.h"
51 #include "ipa-cp.h"
52 #include "ipa-prop.h"
54 /* Actual prefixes of different newly synthetized parameters. Keep in sync
55 with IPA_PARAM_PREFIX_* defines. */
57 static const char *ipa_param_prefixes[IPA_PARAM_PREFIX_COUNT]
58 = {"SYNTH",
59 "ISRA",
60 "simd",
61 "mask"};
63 /* Names of parameters for dumping. Keep in sync with enum ipa_parm_op. */
65 static const char *ipa_param_op_names[IPA_PARAM_PREFIX_COUNT]
66 = {"IPA_PARAM_OP_UNDEFINED",
67 "IPA_PARAM_OP_COPY",
68 "IPA_PARAM_OP_NEW",
69 "IPA_PARAM_OP_SPLIT"};
71 /* Structure to hold declarations representing pass-through IPA-SRA splits. In
72 essence, it tells new index for a combination of original index and
73 offset. */
75 struct pass_through_split_map
77 /* Original argument index. */
78 unsigned base_index;
79 /* Offset of the split part in the original argument. */
80 unsigned unit_offset;
81 /* Index of the split part in the call statement - where clone
82 materialization put it. */
83 int new_index;
86 /* Information about some call statements that needs to be conveyed from clone
87 materialization to edge redirection. */
89 class ipa_edge_modification_info
91 public:
92 ipa_edge_modification_info ()
95 /* Mapping of original argument indices to where those arguments sit in the
96 call statement now or to a negative index if they were removed. */
97 auto_vec<int> index_map;
98 /* Information about ISRA replacements put into the call statement at the
99 clone materialization stages. */
100 auto_vec<pass_through_split_map> pass_through_map;
101 /* Necessary adjustment to ipa_param_adjustments::m_always_copy_start when
102 redirecting the call. */
103 int always_copy_delta = 0;
106 /* Class for storing and retrieving summaries about cal statement
107 modifications. */
109 class ipa_edge_modification_sum
110 : public call_summary <ipa_edge_modification_info *>
112 public:
113 ipa_edge_modification_sum (symbol_table *table)
114 : call_summary<ipa_edge_modification_info *> (table)
118 /* Hook that is called by summary when an edge is duplicated. */
120 void duplicate (cgraph_edge *,
121 cgraph_edge *,
122 ipa_edge_modification_info *old_info,
123 ipa_edge_modification_info *new_info) final override
125 new_info->index_map.safe_splice (old_info->index_map);
126 new_info->pass_through_map.safe_splice (old_info->pass_through_map);
127 new_info->always_copy_delta = old_info->always_copy_delta;
131 /* Call summary to store information about edges which have had their arguments
132 partially modified already. */
134 static ipa_edge_modification_sum *ipa_edge_modifications;
136 /* Fail compilation if CS has any summary associated with it in
137 ipa_edge_modifications. */
139 DEBUG_FUNCTION void
140 ipa_verify_edge_has_no_modifications (cgraph_edge *cs)
142 gcc_assert (!ipa_edge_modifications || !ipa_edge_modifications->get (cs));
145 /* Fill an empty vector ARGS with PARM_DECLs representing formal parameters of
146 FNDECL. The function should not be called during LTO WPA phase except for
147 thunks (or functions with bodies streamed in). */
149 void
150 push_function_arg_decls (vec<tree> *args, tree fndecl)
152 int count;
153 tree parm;
155 /* Safety check that we do not attempt to use the function in WPA, except
156 when the function is a thunk and then we have DECL_ARGUMENTS or when we
157 have already explicitely loaded its body. */
158 gcc_assert (!flag_wpa
159 || DECL_ARGUMENTS (fndecl)
160 || gimple_has_body_p (fndecl));
161 count = 0;
162 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
163 count++;
165 args->reserve_exact (count);
166 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
167 args->quick_push (parm);
170 /* Fill an empty vector TYPES with trees representing formal parameters of
171 function type FNTYPE. */
173 void
174 push_function_arg_types (vec<tree> *types, tree fntype)
176 int count = 0;
177 tree t;
179 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
180 count++;
182 types->reserve_exact (count);
183 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
184 types->quick_push (TREE_VALUE (t));
187 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
188 friendly way, assuming they are meant to be applied to FNDECL. */
190 void
191 ipa_dump_adjusted_parameters (FILE *f,
192 vec<ipa_adjusted_param, va_gc> *adj_params)
194 unsigned i, len = vec_safe_length (adj_params);
195 bool first = true;
197 if (!len)
198 return;
200 fprintf (f, " IPA adjusted parameters: ");
201 for (i = 0; i < len; i++)
203 struct ipa_adjusted_param *apm;
204 apm = &(*adj_params)[i];
206 if (!first)
207 fprintf (f, " ");
208 else
209 first = false;
211 fprintf (f, "%i. %s %s", i, ipa_param_op_names[apm->op],
212 apm->prev_clone_adjustment ? "prev_clone_adjustment " : "");
213 switch (apm->op)
215 case IPA_PARAM_OP_UNDEFINED:
216 break;
218 case IPA_PARAM_OP_COPY:
219 fprintf (f, ", base_index: %u", apm->base_index);
220 fprintf (f, ", prev_clone_index: %u", apm->prev_clone_index);
221 break;
223 case IPA_PARAM_OP_SPLIT:
224 fprintf (f, ", offset: %u", apm->unit_offset);
225 /* fall-through */
226 case IPA_PARAM_OP_NEW:
227 fprintf (f, ", base_index: %u", apm->base_index);
228 fprintf (f, ", prev_clone_index: %u", apm->prev_clone_index);
229 print_node_brief (f, ", type: ", apm->type, 0);
230 print_node_brief (f, ", alias type: ", apm->alias_ptr_type, 0);
231 fprintf (f, " prefix: %s",
232 ipa_param_prefixes[apm->param_prefix_index]);
233 if (apm->reverse)
234 fprintf (f, ", reverse");
235 break;
237 fprintf (f, "\n");
241 /* Fill NEW_TYPES with types of a function after its current OTYPES have been
242 modified as described in ADJ_PARAMS. When USE_PREV_INDICES is true, use
243 prev_clone_index from ADJ_PARAMS as opposed to base_index when the parameter
244 is false. */
246 static void
247 fill_vector_of_new_param_types (vec<tree> *new_types, vec<tree> *otypes,
248 vec<ipa_adjusted_param, va_gc> *adj_params,
249 bool use_prev_indices)
251 unsigned adj_len = vec_safe_length (adj_params);
252 new_types->reserve_exact (adj_len);
253 for (unsigned i = 0; i < adj_len ; i++)
255 ipa_adjusted_param *apm = &(*adj_params)[i];
256 if (apm->op == IPA_PARAM_OP_COPY)
258 unsigned index
259 = use_prev_indices ? apm->prev_clone_index : apm->base_index;
260 /* The following needs to be handled gracefully because of type
261 mismatches. This happens with LTO but apparently also in Fortran
262 with -fcoarray=lib -O2 -lcaf_single -latomic. */
263 if (index >= otypes->length ())
264 continue;
265 new_types->quick_push ((*otypes)[index]);
267 else if (apm->op == IPA_PARAM_OP_NEW
268 || apm->op == IPA_PARAM_OP_SPLIT)
270 tree ntype = apm->type;
271 if (is_gimple_reg_type (ntype)
272 && TYPE_MODE (ntype) != BLKmode)
274 unsigned malign = GET_MODE_ALIGNMENT (TYPE_MODE (ntype));
275 if (TYPE_ALIGN (ntype) != malign)
276 ntype = build_aligned_type (ntype, malign);
278 new_types->quick_push (ntype);
280 else
281 gcc_unreachable ();
285 /* Return false if given attribute should prevent type adjustments. */
287 bool
288 ipa_param_adjustments::type_attribute_allowed_p (tree name)
290 if ((is_attribute_p ("fn spec", name) && flag_ipa_modref)
291 || is_attribute_p ("access", name)
292 || is_attribute_p ("returns_nonnull", name)
293 || is_attribute_p ("assume_aligned", name)
294 || is_attribute_p ("nocf_check", name)
295 || is_attribute_p ("warn_unused_result", name))
296 return true;
297 return false;
300 /* Return true if attribute should be dropped if parameter changed. */
302 static bool
303 drop_type_attribute_if_params_changed_p (tree name)
305 if (is_attribute_p ("fn spec", name)
306 || is_attribute_p ("access", name))
307 return true;
308 return false;
311 /* Build and return a function type just like ORIG_TYPE but with parameter
312 types given in NEW_PARAM_TYPES - which can be NULL if, but only if,
313 ORIG_TYPE itself has NULL TREE_ARG_TYPEs. If METHOD2FUNC is true, also make
314 it a FUNCTION_TYPE instead of FUNCTION_TYPE.
315 If ARG_MODIFIED is true drop attributes that are no longer up to date. */
317 static tree
318 build_adjusted_function_type (tree orig_type, vec<tree> *new_param_types,
319 bool method2func, bool skip_return,
320 bool args_modified)
322 tree new_arg_types = NULL;
323 if (TYPE_ARG_TYPES (orig_type))
325 gcc_checking_assert (new_param_types);
326 bool last_parm_void = (TREE_VALUE (tree_last (TYPE_ARG_TYPES (orig_type)))
327 == void_type_node);
328 unsigned len = new_param_types->length ();
329 for (unsigned i = 0; i < len; i++)
330 new_arg_types = tree_cons (NULL_TREE, (*new_param_types)[i],
331 new_arg_types);
333 tree new_reversed = nreverse (new_arg_types);
334 if (last_parm_void)
336 if (new_reversed)
337 TREE_CHAIN (new_arg_types) = void_list_node;
338 else
339 new_reversed = void_list_node;
341 new_arg_types = new_reversed;
344 /* Use build_distinct_type_copy to preserve as much as possible from original
345 type (debug info, attribute lists etc.). The one exception is
346 METHOD_TYPEs which must have THIS argument and when we are asked to remove
347 it, we need to build new FUNCTION_TYPE instead. */
348 tree new_type = NULL;
349 if (method2func)
351 tree ret_type;
352 if (skip_return)
353 ret_type = void_type_node;
354 else
355 ret_type = TREE_TYPE (orig_type);
357 new_type
358 = build_distinct_type_copy (build_function_type (ret_type,
359 new_arg_types));
360 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
362 else
364 new_type = build_distinct_type_copy (orig_type);
365 TYPE_ARG_TYPES (new_type) = new_arg_types;
366 if (skip_return)
367 TREE_TYPE (new_type) = void_type_node;
369 if (args_modified && TYPE_ATTRIBUTES (new_type))
371 tree t = TYPE_ATTRIBUTES (new_type);
372 tree *last = &TYPE_ATTRIBUTES (new_type);
373 TYPE_ATTRIBUTES (new_type) = NULL;
374 for (;t; t = TREE_CHAIN (t))
375 if (!drop_type_attribute_if_params_changed_p
376 (get_attribute_name (t)))
378 *last = copy_node (t);
379 TREE_CHAIN (*last) = NULL;
380 last = &TREE_CHAIN (*last);
384 return new_type;
387 /* Return the maximum index in any IPA_PARAM_OP_COPY adjustment or -1 if there
388 is none. */
391 ipa_param_adjustments::get_max_base_index ()
393 unsigned adj_len = vec_safe_length (m_adj_params);
394 int max_index = -1;
395 for (unsigned i = 0; i < adj_len ; i++)
397 ipa_adjusted_param *apm = &(*m_adj_params)[i];
398 if (apm->op == IPA_PARAM_OP_COPY
399 && max_index < apm->base_index)
400 max_index = apm->base_index;
402 return max_index;
406 /* Fill SURVIVING_PARAMS with an array of bools where each one says whether a
407 parameter that originally was at that position still survives in the given
408 clone or is removed/replaced. If the final array is smaller than an index
409 of an original parameter, that parameter also did not survive. That a
410 parameter survives does not mean it has the same index as before. */
412 void
413 ipa_param_adjustments::get_surviving_params (vec<bool> *surviving_params)
415 unsigned adj_len = vec_safe_length (m_adj_params);
416 int max_index = get_max_base_index ();
418 if (max_index < 0)
419 return;
420 surviving_params->reserve_exact (max_index + 1);
421 surviving_params->quick_grow_cleared (max_index + 1);
422 for (unsigned i = 0; i < adj_len ; i++)
424 ipa_adjusted_param *apm = &(*m_adj_params)[i];
425 if (apm->op == IPA_PARAM_OP_COPY)
426 (*surviving_params)[apm->base_index] = true;
430 /* Fill NEW_INDICES with new indices of each surviving parameter or -1 for
431 those which do not survive. Any parameter outside of lenght of the vector
432 does not survive. There is currently no support for a parameter to be
433 copied to two distinct new parameters. */
435 void
436 ipa_param_adjustments::get_updated_indices (vec<int> *new_indices)
438 unsigned adj_len = vec_safe_length (m_adj_params);
439 int max_index = get_max_base_index ();
441 if (max_index < 0)
442 return;
443 unsigned res_len = max_index + 1;
444 new_indices->reserve_exact (res_len);
445 for (unsigned i = 0; i < res_len ; i++)
446 new_indices->quick_push (-1);
447 for (unsigned i = 0; i < adj_len ; i++)
449 ipa_adjusted_param *apm = &(*m_adj_params)[i];
450 if (apm->op == IPA_PARAM_OP_COPY)
451 (*new_indices)[apm->base_index] = i;
455 /* Return the original index for the given new parameter index. Return a
456 negative number if not available. */
459 ipa_param_adjustments::get_original_index (int newidx)
461 const ipa_adjusted_param *adj = &(*m_adj_params)[newidx];
462 if (adj->op != IPA_PARAM_OP_COPY)
463 return -1;
464 return adj->base_index;
467 /* Return true if the first parameter (assuming there was one) survives the
468 transformation intact and remains the first one. */
470 bool
471 ipa_param_adjustments::first_param_intact_p ()
473 return (!vec_safe_is_empty (m_adj_params)
474 && (*m_adj_params)[0].op == IPA_PARAM_OP_COPY
475 && (*m_adj_params)[0].base_index == 0);
478 /* Return true if we have to change what has formerly been a method into a
479 function. */
481 bool
482 ipa_param_adjustments::method2func_p (tree orig_type)
484 return ((TREE_CODE (orig_type) == METHOD_TYPE) && !first_param_intact_p ());
487 /* Given function type OLD_TYPE, return a new type derived from it after
488 performing all atored modifications. TYPE_ORIGINAL_P should be true when
489 OLD_TYPE refers to the type before any IPA transformations, as opposed to a
490 type that can be an intermediate one in between various IPA
491 transformations. */
493 tree
494 ipa_param_adjustments::build_new_function_type (tree old_type,
495 bool type_original_p)
497 auto_vec<tree,16> new_param_types, *new_param_types_p;
498 if (prototype_p (old_type))
500 auto_vec<tree, 16> otypes;
501 push_function_arg_types (&otypes, old_type);
502 fill_vector_of_new_param_types (&new_param_types, &otypes, m_adj_params,
503 !type_original_p);
504 new_param_types_p = &new_param_types;
506 else
507 new_param_types_p = NULL;
509 /* Check if any params type cares about are modified. In this case will
510 need to drop some type attributes. */
511 bool modified = false;
512 size_t index = 0;
513 if (m_adj_params)
514 for (tree t = TYPE_ARG_TYPES (old_type);
515 t && (int)index < m_always_copy_start && !modified;
516 t = TREE_CHAIN (t), index++)
517 if (index >= m_adj_params->length ()
518 || get_original_index (index) != (int)index)
519 modified = true;
522 return build_adjusted_function_type (old_type, new_param_types_p,
523 method2func_p (old_type), m_skip_return,
524 modified);
527 /* Build variant of function decl ORIG_DECL which has no return value if
528 M_SKIP_RETURN is true and, if ORIG_DECL's types or parameters is known, has
529 this type adjusted as indicated in M_ADJ_PARAMS. Arguments from
530 DECL_ARGUMENTS list are not processed now, since they are linked by
531 TREE_CHAIN directly and not accessible in LTO during WPA. The caller is
532 responsible for eliminating them when clones are properly materialized. */
534 tree
535 ipa_param_adjustments::adjust_decl (tree orig_decl)
537 tree new_decl = copy_node (orig_decl);
538 tree orig_type = TREE_TYPE (orig_decl);
539 if (prototype_p (orig_type)
540 || (m_skip_return && !VOID_TYPE_P (TREE_TYPE (orig_type))))
542 tree new_type = build_new_function_type (orig_type, false);
543 TREE_TYPE (new_decl) = new_type;
545 if (method2func_p (orig_type))
546 DECL_VINDEX (new_decl) = NULL_TREE;
548 /* When signature changes, we need to clear builtin info. */
549 if (fndecl_built_in_p (new_decl))
550 set_decl_built_in_function (new_decl, NOT_BUILT_IN, 0);
552 DECL_VIRTUAL_P (new_decl) = 0;
553 DECL_LANG_SPECIFIC (new_decl) = NULL;
555 /* Drop MALLOC attribute for a void function. */
556 if (m_skip_return)
557 DECL_IS_MALLOC (new_decl) = 0;
559 return new_decl;
562 /* Wrapper around get_base_ref_and_offset for cases interesting for IPA-SRA
563 transformations. Return true if EXPR has an interesting form and fill in
564 *BASE_P and *UNIT_OFFSET_P with the appropriate info. */
566 static bool
567 isra_get_ref_base_and_offset (tree expr, tree *base_p, unsigned *unit_offset_p)
569 HOST_WIDE_INT offset, size;
570 bool reverse;
571 tree base
572 = get_ref_base_and_extent_hwi (expr, &offset, &size, &reverse);
573 if (!base || size < 0)
574 return false;
576 if ((offset % BITS_PER_UNIT) != 0)
577 return false;
579 if (TREE_CODE (base) == MEM_REF)
581 poly_int64 plmoff = mem_ref_offset (base).force_shwi ();
582 HOST_WIDE_INT moff;
583 bool is_cst = plmoff.is_constant (&moff);
584 if (!is_cst)
585 return false;
586 offset += moff * BITS_PER_UNIT;
587 base = TREE_OPERAND (base, 0);
590 if (offset < 0 || (offset / BITS_PER_UNIT) > UINT_MAX)
591 return false;
593 *base_p = base;
594 *unit_offset_p = offset / BITS_PER_UNIT;
595 return true;
598 /* Remove all statements that use NAME directly or indirectly. KILLED_SSAS
599 contains the SSA_NAMEs that are already being or have been processed and new
600 ones need to be added to it. The function only has to process situations
601 handled by ssa_name_only_returned_p in ipa-sra.cc with the exception that it
602 can assume it must never reach a use in a return statement. */
604 static void
605 purge_all_uses (tree name, hash_set <tree> *killed_ssas)
607 imm_use_iterator imm_iter;
608 gimple *stmt;
609 auto_vec <tree, 4> worklist;
611 worklist.safe_push (name);
612 while (!worklist.is_empty ())
614 tree cur_name = worklist.pop ();
615 FOR_EACH_IMM_USE_STMT (stmt, imm_iter, cur_name)
617 if (gimple_debug_bind_p (stmt))
619 /* When runing within tree-inline, we will never end up here but
620 adding the SSAs to killed_ssas will do the trick in this case
621 and the respective debug statements will get reset. */
622 gimple_debug_bind_reset_value (stmt);
623 update_stmt (stmt);
624 continue;
627 tree lhs = NULL_TREE;
628 if (is_gimple_assign (stmt))
629 lhs = gimple_assign_lhs (stmt);
630 else if (gimple_code (stmt) == GIMPLE_PHI)
631 lhs = gimple_phi_result (stmt);
632 gcc_assert (lhs
633 && (TREE_CODE (lhs) == SSA_NAME)
634 && !gimple_vdef (stmt));
635 if (!killed_ssas->add (lhs))
637 worklist.safe_push (lhs);
638 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
639 gsi_remove (&gsi, true);
645 /* Modify actual arguments of a function call in statement currently belonging
646 to CS, and make it call CS->callee->decl. Return the new statement that
647 replaced the old one. When invoked, cfun and current_function_decl have to
648 be set to the caller. When called from within tree-inline, KILLED_SSAs has
649 to contain the pointer to killed_new_ssa_names within the copy_body_data
650 structure and SSAs discovered to be useless (if LHS is removed) will be
651 added to it, otherwise it needs to be NULL. */
653 gcall *
654 ipa_param_adjustments::modify_call (cgraph_edge *cs,
655 bool update_references,
656 hash_set <tree> *killed_ssas)
658 gcall *stmt = cs->call_stmt;
659 tree callee_decl = cs->callee->decl;
661 ipa_edge_modification_info *mod_info
662 = ipa_edge_modifications ? ipa_edge_modifications->get (cs) : NULL;
663 if (mod_info && symtab->dump_file)
665 fprintf (symtab->dump_file, "Information about pre-exiting "
666 "modifications.\n Index map:");
667 unsigned idx_len = mod_info->index_map.length ();
668 for (unsigned i = 0; i < idx_len; i++)
669 fprintf (symtab->dump_file, " %i", mod_info->index_map[i]);
670 fprintf (symtab->dump_file, "\n Pass-through split map: ");
671 unsigned ptm_len = mod_info->pass_through_map.length ();
672 for (unsigned i = 0; i < ptm_len; i++)
673 fprintf (symtab->dump_file,
674 " (base_index: %u, offset: %u, new_index: %i)",
675 mod_info->pass_through_map[i].base_index,
676 mod_info->pass_through_map[i].unit_offset,
677 mod_info->pass_through_map[i].new_index);
678 fprintf (symtab->dump_file, "\n Always-copy delta: %i\n",
679 mod_info->always_copy_delta);
682 unsigned len = vec_safe_length (m_adj_params);
683 auto_vec<tree, 16> vargs (len);
684 unsigned old_nargs = gimple_call_num_args (stmt);
685 unsigned orig_nargs = mod_info ? mod_info->index_map.length () : old_nargs;
686 auto_vec<bool, 16> kept (old_nargs);
687 kept.quick_grow_cleared (old_nargs);
689 cgraph_node *current_node = cgraph_node::get (current_function_decl);
690 if (update_references)
691 current_node->remove_stmt_references (stmt);
693 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
694 gimple_stmt_iterator prev_gsi = gsi;
695 gsi_prev (&prev_gsi);
696 for (unsigned i = 0; i < len; i++)
698 ipa_adjusted_param *apm = &(*m_adj_params)[i];
699 if (apm->op == IPA_PARAM_OP_COPY)
701 int index = apm->base_index;
702 if ((unsigned) index >= orig_nargs)
703 /* Can happen if the original call has argument mismatch,
704 ignore. */
705 continue;
706 if (mod_info)
708 index = mod_info->index_map[apm->base_index];
709 gcc_assert (index >= 0);
712 tree arg = gimple_call_arg (stmt, index);
714 vargs.quick_push (arg);
715 kept[index] = true;
716 continue;
719 /* At the moment the only user of IPA_PARAM_OP_NEW modifies calls itself.
720 If we ever want to support it during WPA IPA stage, we'll need a
721 mechanism to call into the IPA passes that introduced them. Currently
722 we simply mandate that IPA infrastructure understands all argument
723 modifications. Remember, edge redirection/modification is done only
724 once, not in steps for each pass modifying the callee like clone
725 materialization. */
726 gcc_assert (apm->op == IPA_PARAM_OP_SPLIT);
728 /* We have to handle pass-through changes differently using the map
729 clone materialziation might have left behind. */
730 tree repl = NULL_TREE;
731 unsigned ptm_len = mod_info ? mod_info->pass_through_map.length () : 0;
732 for (unsigned j = 0; j < ptm_len; j++)
733 if (mod_info->pass_through_map[j].base_index == apm->base_index
734 && mod_info->pass_through_map[j].unit_offset == apm->unit_offset)
736 int repl_idx = mod_info->pass_through_map[j].new_index;
737 gcc_assert (repl_idx >= 0);
738 repl = gimple_call_arg (stmt, repl_idx);
739 break;
741 if (repl)
743 vargs.quick_push (repl);
744 continue;
747 int index = apm->base_index;
748 if ((unsigned) index >= orig_nargs)
749 /* Can happen if the original call has argument mismatch, ignore. */
750 continue;
751 if (mod_info)
753 index = mod_info->index_map[apm->base_index];
754 gcc_assert (index >= 0);
756 tree base = gimple_call_arg (stmt, index);
758 /* We create a new parameter out of the value of the old one, we can
759 do the following kind of transformations:
761 - A scalar passed by reference, potentially as a part of a larger
762 aggregate, is converted to a scalar passed by value.
764 - A part of an aggregate is passed instead of the whole aggregate. */
766 location_t loc = gimple_location (stmt);
767 tree off;
768 bool deref_base = false;
769 unsigned int deref_align = 0;
770 if (TREE_CODE (base) != ADDR_EXPR
771 && is_gimple_reg_type (TREE_TYPE (base)))
773 /* Detect type mismatches in calls in invalid programs and make a
774 poor attempt to gracefully convert them so that we don't ICE. */
775 if (!POINTER_TYPE_P (TREE_TYPE (base)))
776 base = force_value_to_type (ptr_type_node, base);
778 off = build_int_cst (apm->alias_ptr_type, apm->unit_offset);
780 else
782 bool addrof;
783 if (TREE_CODE (base) == ADDR_EXPR)
785 base = TREE_OPERAND (base, 0);
786 addrof = true;
788 else
789 addrof = false;
791 tree prev_base = base;
792 poly_int64 base_offset;
793 base = get_addr_base_and_unit_offset (base, &base_offset);
795 /* Aggregate arguments can have non-invariant addresses. */
796 if (!base)
798 base = build_fold_addr_expr (prev_base);
799 off = build_int_cst (apm->alias_ptr_type, apm->unit_offset);
801 else if (TREE_CODE (base) == MEM_REF)
803 if (!addrof)
805 deref_base = true;
806 deref_align = TYPE_ALIGN (TREE_TYPE (base));
808 off = build_int_cst (apm->alias_ptr_type,
809 base_offset + apm->unit_offset);
810 off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
811 off);
812 base = TREE_OPERAND (base, 0);
814 else
816 off = build_int_cst (apm->alias_ptr_type,
817 base_offset + apm->unit_offset);
818 base = build_fold_addr_expr (base);
822 tree type = apm->type;
823 unsigned int align;
824 unsigned HOST_WIDE_INT misalign;
826 if (deref_base)
828 align = deref_align;
829 misalign = 0;
831 else
833 get_pointer_alignment_1 (base, &align, &misalign);
834 /* All users must make sure that we can be optimistic when it
835 comes to alignment in this case (by inspecting the final users
836 of these new parameters). */
837 if (TYPE_ALIGN (type) > align)
838 align = TYPE_ALIGN (type);
840 misalign
841 += (offset_int::from (wi::to_wide (off), SIGNED).to_short_addr ()
842 * BITS_PER_UNIT);
843 misalign = misalign & (align - 1);
844 if (misalign != 0)
845 align = least_bit_hwi (misalign);
846 if (align < TYPE_ALIGN (type))
847 type = build_aligned_type (type, align);
848 base = force_gimple_operand_gsi (&gsi, base,
849 true, NULL, true, GSI_SAME_STMT);
850 tree expr = fold_build2_loc (loc, MEM_REF, type, base, off);
851 REF_REVERSE_STORAGE_ORDER (expr) = apm->reverse;
852 /* If expr is not a valid gimple call argument emit
853 a load into a temporary. */
854 if (is_gimple_reg_type (TREE_TYPE (expr)))
856 gimple *tem = gimple_build_assign (NULL_TREE, expr);
857 if (gimple_in_ssa_p (cfun))
859 gimple_set_vuse (tem, gimple_vuse (stmt));
860 expr = make_ssa_name (TREE_TYPE (expr), tem);
862 else
863 expr = create_tmp_reg (TREE_TYPE (expr));
864 gimple_assign_set_lhs (tem, expr);
865 gsi_insert_before (&gsi, tem, GSI_SAME_STMT);
867 vargs.quick_push (expr);
870 if (m_always_copy_start >= 0)
872 int always_copy_start = m_always_copy_start;
873 if (mod_info)
875 always_copy_start += mod_info->always_copy_delta;
876 gcc_assert (always_copy_start >= 0);
878 for (unsigned i = always_copy_start; i < old_nargs; i++)
879 vargs.safe_push (gimple_call_arg (stmt, i));
882 /* For optimized away parameters, add on the caller side
883 before the call
884 DEBUG D#X => parm_Y(D)
885 stmts and associate D#X with parm in decl_debug_args_lookup
886 vector to say for debug info that if parameter parm had been passed,
887 it would have value parm_Y(D). */
888 tree old_decl = gimple_call_fndecl (stmt);
889 if (MAY_HAVE_DEBUG_BIND_STMTS && old_decl && callee_decl)
891 vec<tree, va_gc> **debug_args = NULL;
892 unsigned i = 0;
893 cgraph_node *callee_node = cgraph_node::get (callee_decl);
895 /* FIXME: we don't seem to be able to insert debug args before clone
896 is materialized. Materializing them early leads to extra memory
897 use. */
898 if (callee_node->clone_of)
899 callee_node->get_untransformed_body ();
900 for (tree old_parm = DECL_ARGUMENTS (old_decl);
901 old_parm && i < old_nargs && ((int) i) < m_always_copy_start;
902 old_parm = DECL_CHAIN (old_parm), i++)
904 if (!is_gimple_reg (old_parm) || kept[i])
905 continue;
906 tree arg;
907 if (mod_info)
909 if (mod_info->index_map[i] < 0)
910 continue;
911 arg = gimple_call_arg (stmt, mod_info->index_map[i]);
913 else
914 arg = gimple_call_arg (stmt, i);
916 tree origin = DECL_ORIGIN (old_parm);
917 if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
919 if (!fold_convertible_p (TREE_TYPE (origin), arg))
920 continue;
921 tree rhs1;
922 if (TREE_CODE (arg) == SSA_NAME
923 && gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg))
924 && (rhs1
925 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (arg)))
926 && useless_type_conversion_p (TREE_TYPE (origin),
927 TREE_TYPE (rhs1)))
928 arg = rhs1;
929 else
930 arg = fold_convert_loc (gimple_location (stmt),
931 TREE_TYPE (origin), arg);
933 if (debug_args == NULL)
934 debug_args = decl_debug_args_insert (callee_decl);
935 unsigned int ix;
936 tree ddecl = NULL_TREE;
937 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl); ix += 2)
938 if (ddecl == origin)
940 ddecl = (**debug_args)[ix + 1];
941 break;
943 if (ddecl == NULL)
945 ddecl = build_debug_expr_decl (TREE_TYPE (origin));
946 /* FIXME: Is setting the mode really necessary? */
947 SET_DECL_MODE (ddecl, DECL_MODE (origin));
949 vec_safe_push (*debug_args, origin);
950 vec_safe_push (*debug_args, ddecl);
952 gimple *def_temp = gimple_build_debug_bind (ddecl,
953 unshare_expr (arg), stmt);
954 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
958 if (dump_file && (dump_flags & TDF_DETAILS))
960 fprintf (dump_file, "replacing stmt:");
961 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0);
964 gcall *new_stmt = gimple_build_call_vec (callee_decl, vargs);
966 hash_set <tree> *ssas_to_remove = NULL;
967 if (tree lhs = gimple_call_lhs (stmt))
969 if (!m_skip_return)
970 gimple_call_set_lhs (new_stmt, lhs);
971 else if (TREE_CODE (lhs) == SSA_NAME)
973 if (!killed_ssas)
975 ssas_to_remove = new hash_set<tree> (8);
976 killed_ssas = ssas_to_remove;
978 killed_ssas->add (lhs);
979 purge_all_uses (lhs, killed_ssas);
983 gimple_set_block (new_stmt, gimple_block (stmt));
984 if (gimple_has_location (stmt))
985 gimple_set_location (new_stmt, gimple_location (stmt));
986 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
987 gimple_call_copy_flags (new_stmt, stmt);
988 if (gimple_in_ssa_p (cfun))
989 gimple_move_vops (new_stmt, stmt);
991 if (dump_file && (dump_flags & TDF_DETAILS))
993 fprintf (dump_file, "with stmt:");
994 print_gimple_stmt (dump_file, new_stmt, 0);
995 fprintf (dump_file, "\n");
997 gsi_replace (&gsi, new_stmt, true);
998 if (ssas_to_remove)
1000 ipa_release_ssas_in_hash (ssas_to_remove);
1001 delete ssas_to_remove;
1003 if (update_references)
1006 current_node->record_stmt_references (gsi_stmt (gsi));
1007 gsi_prev (&gsi);
1009 while (gsi_stmt (gsi) != gsi_stmt (prev_gsi));
1011 if (mod_info)
1012 ipa_edge_modifications->remove (cs);
1013 return new_stmt;
1016 /* Dump information contained in the object in textual form to F. */
1018 void
1019 ipa_param_adjustments::dump (FILE *f)
1021 fprintf (f, " m_always_copy_start: %i\n", m_always_copy_start);
1022 ipa_dump_adjusted_parameters (f, m_adj_params);
1023 if (m_skip_return)
1024 fprintf (f, " Will SKIP return.\n");
1027 /* Dump information contained in the object in textual form to stderr. */
1029 void
1030 ipa_param_adjustments::debug ()
1032 dump (stderr);
1035 /* Register a REPLACEMENT for accesses to BASE at UNIT_OFFSET. */
1037 void
1038 ipa_param_body_adjustments::register_replacement (tree base,
1039 unsigned unit_offset,
1040 tree replacement)
1042 ipa_param_body_replacement psr;
1043 psr.base = base;
1044 psr.repl = replacement;
1045 psr.dummy = NULL_TREE;
1046 psr.unit_offset = unit_offset;
1047 m_replacements.safe_push (psr);
1048 m_sorted_replacements_p = false;
1051 /* Register that REPLACEMENT should replace parameter described in APM. */
1053 void
1054 ipa_param_body_adjustments::register_replacement (ipa_adjusted_param *apm,
1055 tree replacement)
1057 gcc_checking_assert (apm->op == IPA_PARAM_OP_SPLIT
1058 || apm->op == IPA_PARAM_OP_NEW);
1059 gcc_checking_assert (!apm->prev_clone_adjustment);
1060 register_replacement (m_oparms[apm->prev_clone_index], apm->unit_offset,
1061 replacement);
1064 /* Comparator for sorting and searching
1065 ipa_param_body_adjustments::m_replacements. */
1067 static int
1068 compare_param_body_replacement (const void *va, const void *vb)
1070 const ipa_param_body_replacement *a = (const ipa_param_body_replacement *) va;
1071 const ipa_param_body_replacement *b = (const ipa_param_body_replacement *) vb;
1073 if (DECL_UID (a->base) < DECL_UID (b->base))
1074 return -1;
1075 if (DECL_UID (a->base) > DECL_UID (b->base))
1076 return 1;
1077 if (a->unit_offset < b->unit_offset)
1078 return -1;
1079 if (a->unit_offset > b->unit_offset)
1080 return 1;
1081 return 0;
1084 /* Sort m_replacements and set m_sorted_replacements_p to true. */
1086 void
1087 ipa_param_body_adjustments::sort_replacements ()
1089 if (m_sorted_replacements_p)
1090 return;
1091 m_replacements.qsort (compare_param_body_replacement);
1092 m_sorted_replacements_p = true;
1095 /* Copy or not, as appropriate given m_id and decl context, a pre-existing
1096 PARM_DECL T so that it can be included in the parameters of the modified
1097 function. */
1099 tree
1100 ipa_param_body_adjustments::carry_over_param (tree t)
1102 tree new_parm;
1103 if (m_id)
1105 new_parm = remap_decl (t, m_id);
1106 if (TREE_CODE (new_parm) != PARM_DECL)
1107 new_parm = m_id->copy_decl (t, m_id);
1109 else if (DECL_CONTEXT (t) != m_fndecl)
1111 new_parm = copy_node (t);
1112 DECL_CONTEXT (new_parm) = m_fndecl;
1114 else
1115 new_parm = t;
1116 return new_parm;
1119 /* If DECL is a gimple register that has a default definition SSA name and that
1120 has some uses, return the default definition, otherwise return NULL_TREE. */
1122 tree
1123 ipa_param_body_adjustments::get_ddef_if_exists_and_is_used (tree decl)
1125 if (!is_gimple_reg (decl))
1126 return NULL_TREE;
1127 tree ddef = ssa_default_def (m_id->src_cfun, decl);
1128 if (!ddef || has_zero_uses (ddef))
1129 return NULL_TREE;
1130 return ddef;
1133 /* Populate m_dead_stmts given that DEAD_PARAM is going to be removed without
1134 any replacement or splitting. REPL is the replacement VAR_SECL to base any
1135 remaining uses of a removed parameter on. Push all removed SSA names that
1136 are used within debug statements to DEBUGSTACK. */
1138 void
1139 ipa_param_body_adjustments::mark_dead_statements (tree dead_param,
1140 vec<tree> *debugstack)
1142 /* Current IPA analyses which remove unused parameters never remove a
1143 non-gimple register ones which have any use except as parameters in other
1144 calls, so we can safely leve them as they are. */
1145 tree parm_ddef = get_ddef_if_exists_and_is_used (dead_param);
1146 if (!parm_ddef)
1147 return;
1149 auto_vec<tree, 4> stack;
1150 hash_set<tree> used_in_debug;
1151 m_dead_ssas.add (parm_ddef);
1152 stack.safe_push (parm_ddef);
1153 while (!stack.is_empty ())
1155 imm_use_iterator imm_iter;
1156 use_operand_p use_p;
1157 tree t = stack.pop ();
1159 insert_decl_map (m_id, t, error_mark_node);
1160 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, t)
1162 gimple *stmt = USE_STMT (use_p);
1164 /* Calls containing dead arguments cannot be deleted,
1165 modify_call_stmt will instead remove just the argument later on.
1166 If isra_track_scalar_value_uses in ipa-sra.cc is extended to look
1167 through const functions, we will need to do so here too. */
1168 if (is_gimple_call (stmt)
1169 || (m_id->blocks_to_copy
1170 && !bitmap_bit_p (m_id->blocks_to_copy,
1171 gimple_bb (stmt)->index)))
1172 continue;
1174 if (is_gimple_debug (stmt))
1176 m_dead_stmts.add (stmt);
1177 gcc_assert (gimple_debug_bind_p (stmt));
1178 if (!used_in_debug.contains (t))
1180 used_in_debug.add (t);
1181 debugstack->safe_push (t);
1184 else if (gimple_code (stmt) == GIMPLE_PHI)
1186 gphi *phi = as_a <gphi *> (stmt);
1187 int ix = PHI_ARG_INDEX_FROM_USE (use_p);
1189 if (!m_id->blocks_to_copy
1190 || bitmap_bit_p (m_id->blocks_to_copy,
1191 gimple_phi_arg_edge (phi, ix)->src->index))
1193 m_dead_stmts.add (phi);
1194 tree res = gimple_phi_result (phi);
1195 if (!m_dead_ssas.add (res))
1196 stack.safe_push (res);
1199 else if (is_gimple_assign (stmt))
1201 m_dead_stmts.add (stmt);
1202 if (!gimple_clobber_p (stmt))
1204 tree lhs = gimple_assign_lhs (stmt);
1205 gcc_assert (TREE_CODE (lhs) == SSA_NAME);
1206 if (!m_dead_ssas.add (lhs))
1207 stack.safe_push (lhs);
1210 else if (gimple_code (stmt) == GIMPLE_RETURN)
1211 gcc_assert (m_adjustments && m_adjustments->m_skip_return);
1212 else
1213 /* IPA-SRA does not analyze other types of statements. */
1214 gcc_unreachable ();
1218 if (!MAY_HAVE_DEBUG_STMTS)
1220 gcc_assert (debugstack->is_empty ());
1221 return;
1224 tree dp_ddecl = build_debug_expr_decl (TREE_TYPE (dead_param));
1225 /* FIXME: Is setting the mode really necessary? */
1226 SET_DECL_MODE (dp_ddecl, DECL_MODE (dead_param));
1227 m_dead_ssa_debug_equiv.put (parm_ddef, dp_ddecl);
1230 /* Put all clobbers of of dereference of default definition of PARAM into
1231 m_dead_stmts. If there are returns among uses of the default definition of
1232 PARAM, verify they will be stripped off the return value. */
1234 void
1235 ipa_param_body_adjustments::mark_clobbers_dead (tree param)
1237 if (!is_gimple_reg (param))
1238 return;
1239 tree ddef = get_ddef_if_exists_and_is_used (param);
1240 if (!ddef)
1241 return;
1243 imm_use_iterator imm_iter;
1244 use_operand_p use_p;
1245 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, ddef)
1247 gimple *stmt = USE_STMT (use_p);
1248 if (gimple_clobber_p (stmt))
1249 m_dead_stmts.add (stmt);
1250 else if (gimple_code (stmt) == GIMPLE_RETURN)
1251 gcc_assert (m_adjustments && m_adjustments->m_skip_return);
1255 /* Callback to walk_tree. If REMAP is an SSA_NAME that is present in hash_map
1256 passed in DATA, replace it with unshared version of what it was mapped to.
1257 If an SSA argument would be remapped to NULL, the whole operation needs to
1258 abort which is signaled by returning error_mark_node. */
1260 static tree
1261 replace_with_mapped_expr (tree *remap, int *walk_subtrees, void *data)
1263 if (TYPE_P (*remap))
1265 *walk_subtrees = 0;
1266 return 0;
1268 if (TREE_CODE (*remap) != SSA_NAME)
1269 return 0;
1271 *walk_subtrees = 0;
1273 hash_map<tree, tree> *equivs = (hash_map<tree, tree> *) data;
1274 if (tree *p = equivs->get (*remap))
1276 if (!*p)
1277 return error_mark_node;
1278 *remap = unshare_expr (*p);
1280 return 0;
1283 /* Replace all occurances of SSAs in m_dead_ssa_debug_equiv in t with what they
1284 are mapped to. */
1286 void
1287 ipa_param_body_adjustments::remap_with_debug_expressions (tree *t)
1289 /* If *t is an SSA_NAME which should have its debug statements reset, it is
1290 mapped to NULL in the hash_map.
1292 It is perhaps simpler to handle the SSA_NAME cases directly and only
1293 invoke walk_tree on more complex expressions. When
1294 remap_with_debug_expressions is called from tree-inline.cc, a to-be-reset
1295 SSA_NAME can be an operand to such expressions and the entire debug
1296 variable we are remapping should be reset. This is signaled by walk_tree
1297 returning error_mark_node and done by setting *t to NULL. */
1298 if (TREE_CODE (*t) == SSA_NAME)
1300 if (tree *p = m_dead_ssa_debug_equiv.get (*t))
1301 *t = *p;
1303 else if (walk_tree (t, replace_with_mapped_expr,
1304 &m_dead_ssa_debug_equiv, NULL) == error_mark_node)
1305 *t = NULL_TREE;
1308 /* For an SSA_NAME DEAD_SSA which is about to be DCEd because it is based on a
1309 useless parameter, prepare an expression that should represent it in
1310 debug_binds in the cloned function and add a mapping from DEAD_SSA to
1311 m_dead_ssa_debug_equiv. That mapping is to NULL when the associated
1312 debug_statement has to be reset instead. In such case return false,
1313 ottherwise return true. If DEAD_SSA comes from a basic block which is not
1314 about to be copied, ignore it and return true. */
1316 bool
1317 ipa_param_body_adjustments::prepare_debug_expressions (tree dead_ssa)
1319 gcc_checking_assert (m_dead_ssas.contains (dead_ssa));
1320 if (tree *d = m_dead_ssa_debug_equiv.get (dead_ssa))
1321 return (*d != NULL_TREE);
1323 gcc_assert (!SSA_NAME_IS_DEFAULT_DEF (dead_ssa));
1324 gimple *def = SSA_NAME_DEF_STMT (dead_ssa);
1325 if (m_id->blocks_to_copy
1326 && !bitmap_bit_p (m_id->blocks_to_copy, gimple_bb (def)->index))
1327 return true;
1329 if (gimple_code (def) == GIMPLE_PHI)
1331 /* In theory, we could ignore all SSAs coming from BBs not in
1332 m_id->blocks_to_copy but at the time of the writing this code that
1333 should never really be the case because only fnsplit uses that bitmap,
1334 so don't bother. */
1335 tree value = degenerate_phi_result (as_a <gphi *> (def));
1336 if (!value
1337 || (m_dead_ssas.contains (value)
1338 && !prepare_debug_expressions (value)))
1340 m_dead_ssa_debug_equiv.put (dead_ssa, NULL_TREE);
1341 return false;
1344 gcc_assert (TREE_CODE (value) == SSA_NAME);
1345 tree *d = m_dead_ssa_debug_equiv.get (value);
1346 m_dead_ssa_debug_equiv.put (dead_ssa, *d);
1347 return true;
1350 bool lost = false;
1351 use_operand_p use_p;
1352 ssa_op_iter oi;
1353 FOR_EACH_PHI_OR_STMT_USE (use_p, def, oi, SSA_OP_USE)
1355 tree use = USE_FROM_PTR (use_p);
1356 if (m_dead_ssas.contains (use)
1357 && !prepare_debug_expressions (use))
1359 lost = true;
1360 break;
1364 if (lost)
1366 m_dead_ssa_debug_equiv.put (dead_ssa, NULL_TREE);
1367 return false;
1370 if (is_gimple_assign (def))
1372 gcc_assert (!gimple_clobber_p (def));
1373 if (gimple_assign_copy_p (def)
1374 && TREE_CODE (gimple_assign_rhs1 (def)) == SSA_NAME)
1376 tree d = *m_dead_ssa_debug_equiv.get (gimple_assign_rhs1 (def));
1377 gcc_assert (d);
1378 m_dead_ssa_debug_equiv.put (dead_ssa, d);
1379 return true;
1382 tree val
1383 = unshare_expr_without_location (gimple_assign_rhs_to_tree (def));
1384 remap_with_debug_expressions (&val);
1386 tree vexpr = build_debug_expr_decl (TREE_TYPE (val));
1387 m_dead_stmt_debug_equiv.put (def, val);
1388 m_dead_ssa_debug_equiv.put (dead_ssa, vexpr);
1389 return true;
1391 else
1392 gcc_unreachable ();
1395 /* Common initialization performed by all ipa_param_body_adjustments
1396 constructors. OLD_FNDECL is the declaration we take original arguments
1397 from, (it may be the same as M_FNDECL). VARS, if non-NULL, is a pointer to
1398 a chained list of new local variables. TREE_MAP is the IPA-CP produced
1399 mapping of trees to constants.
1401 The function is rather long but it really onlu initializes all data members
1402 of the class. It creates new param DECLs, finds their new types, */
1404 void
1405 ipa_param_body_adjustments::common_initialization (tree old_fndecl,
1406 tree *vars,
1407 vec<ipa_replace_map *,
1408 va_gc> *tree_map)
1410 push_function_arg_decls (&m_oparms, old_fndecl);
1411 auto_vec<tree,16> otypes;
1412 if (TYPE_ARG_TYPES (TREE_TYPE (old_fndecl)) != NULL_TREE)
1413 push_function_arg_types (&otypes, TREE_TYPE (old_fndecl));
1414 else
1416 auto_vec<tree,16> oparms;
1417 push_function_arg_decls (&oparms, old_fndecl);
1418 unsigned ocount = oparms.length ();
1419 otypes.reserve_exact (ocount);
1420 for (unsigned i = 0; i < ocount; i++)
1421 otypes.quick_push (TREE_TYPE (oparms[i]));
1423 fill_vector_of_new_param_types (&m_new_types, &otypes, m_adj_params, true);
1425 auto_vec<bool, 16> kept;
1426 kept.reserve_exact (m_oparms.length ());
1427 kept.quick_grow_cleared (m_oparms.length ());
1428 auto_vec<bool, 16> split;
1429 split.reserve_exact (m_oparms.length ());
1430 split.quick_grow_cleared (m_oparms.length ());
1432 unsigned adj_len = vec_safe_length (m_adj_params);
1433 m_method2func = ((TREE_CODE (TREE_TYPE (m_fndecl)) == METHOD_TYPE)
1434 && (adj_len == 0
1435 || (*m_adj_params)[0].op != IPA_PARAM_OP_COPY
1436 || (*m_adj_params)[0].base_index != 0));
1438 /* The main job of the this function is to go over the vector of adjusted
1439 parameters and create declarations or find corresponding old ones and push
1440 them to m_new_decls. For IPA-SRA replacements it also creates
1441 corresponding m_id->dst_node->clone.performed_splits entries. */
1443 m_new_decls.reserve_exact (adj_len);
1444 for (unsigned i = 0; i < adj_len ; i++)
1446 ipa_adjusted_param *apm = &(*m_adj_params)[i];
1447 unsigned prev_index = apm->prev_clone_index;
1448 tree new_parm;
1449 if (apm->op == IPA_PARAM_OP_COPY
1450 || apm->prev_clone_adjustment)
1452 kept[prev_index] = true;
1453 new_parm = carry_over_param (m_oparms[prev_index]);
1454 m_new_decls.quick_push (new_parm);
1456 else if (apm->op == IPA_PARAM_OP_NEW
1457 || apm->op == IPA_PARAM_OP_SPLIT)
1459 tree new_type = m_new_types[i];
1460 gcc_checking_assert (new_type);
1461 new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
1462 new_type);
1463 const char *prefix = ipa_param_prefixes[apm->param_prefix_index];
1464 DECL_NAME (new_parm) = create_tmp_var_name (prefix);
1465 DECL_ARTIFICIAL (new_parm) = 1;
1466 DECL_ARG_TYPE (new_parm) = new_type;
1467 DECL_CONTEXT (new_parm) = m_fndecl;
1468 TREE_USED (new_parm) = 1;
1469 DECL_IGNORED_P (new_parm) = 1;
1470 layout_decl (new_parm, 0);
1471 m_new_decls.quick_push (new_parm);
1473 if (apm->op == IPA_PARAM_OP_SPLIT)
1475 split[prev_index] = true;
1476 register_replacement (apm, new_parm);
1479 else
1480 gcc_unreachable ();
1483 auto_vec <int, 16> index_mapping;
1484 bool need_remap = false;
1485 if (m_id)
1487 clone_info *cinfo = clone_info::get (m_id->src_node);
1488 if (cinfo && cinfo->param_adjustments)
1490 cinfo->param_adjustments->get_updated_indices (&index_mapping);
1491 need_remap = true;
1494 if (ipcp_transformation *ipcp_ts
1495 = ipcp_get_transformation_summary (m_id->src_node))
1497 for (const ipa_argagg_value &av : ipcp_ts->m_agg_values)
1499 int parm_num = av.index;
1501 if (need_remap)
1503 /* FIXME: We cannot handle the situation when IPA-CP
1504 identified that a parameter is a pointer to a global
1505 variable and at the same time the variable has some known
1506 constant contents (PR 107640). The best place to make
1507 sure we don't drop such constants on the floor probably is
1508 not here, but we have to make sure that it does not
1509 confuse the remapping. */
1510 if (parm_num >= (int) index_mapping.length ())
1511 continue;
1512 parm_num = index_mapping[parm_num];
1513 if (parm_num < 0)
1514 continue;
1517 if (!kept[parm_num])
1519 /* IPA-CP has detected an aggregate constant in a parameter
1520 that will not be kept, which means that IPA-SRA would have
1521 split it if there wasn't a constant. Because we are about
1522 to remove the original, this is the last chance where we
1523 can substitute the uses with a constant (for values passed
1524 by reference) or do the split but initialize the
1525 replacement with a constant (for split aggregates passed
1526 by value). */
1528 tree repl;
1529 if (av.by_ref)
1530 repl = av.value;
1531 else
1533 repl = create_tmp_var (TREE_TYPE (av.value),
1534 "removed_ipa_cp");
1535 gimple *init_stmt = gimple_build_assign (repl, av.value);
1536 m_split_agg_csts_inits.safe_push (init_stmt);
1538 register_replacement (m_oparms[parm_num], av.unit_offset,
1539 repl);
1540 split[parm_num] = true;
1545 sort_replacements ();
1547 if (tree_map)
1549 /* Do not treat parameters which were replaced with a constant as
1550 completely vanished. */
1551 for (unsigned i = 0; i < tree_map->length (); i++)
1553 int parm_num = (*tree_map)[i]->parm_num;
1554 gcc_assert (parm_num >= 0);
1555 if (need_remap)
1556 parm_num = index_mapping[parm_num];
1557 kept[parm_num] = true;
1561 /* As part of body modifications, we will also have to replace remaining uses
1562 of remaining uses of removed PARM_DECLs (which do not however use the
1563 initial value) with their VAR_DECL copies.
1565 We do this differently with and without m_id. With m_id, we rely on its
1566 mapping and create a replacement straight away. Without it, we have our
1567 own mechanism for which we have to populate m_removed_decls vector. Just
1568 don't mix them, that is why you should not call
1569 replace_removed_params_ssa_names or perform_cfun_body_modifications when
1570 you construct with ID not equal to NULL. */
1572 auto_vec<tree, 8> ssas_to_process_debug;
1573 unsigned op_len = m_oparms.length ();
1574 for (unsigned i = 0; i < op_len; i++)
1575 if (!kept[i])
1577 if (m_id)
1579 gcc_assert (!m_id->decl_map->get (m_oparms[i]));
1580 tree var = copy_decl_to_var (m_oparms[i], m_id);
1581 insert_decl_map (m_id, m_oparms[i], var);
1582 /* Declare this new variable. */
1583 DECL_CHAIN (var) = *vars;
1584 *vars = var;
1586 /* If this is not a split but a real removal, init hash sets
1587 that will guide what not to copy to the new body. */
1588 if (!split[i])
1589 mark_dead_statements (m_oparms[i], &ssas_to_process_debug);
1590 else
1591 mark_clobbers_dead (m_oparms[i]);
1592 if (MAY_HAVE_DEBUG_STMTS
1593 && is_gimple_reg (m_oparms[i]))
1594 m_reset_debug_decls.safe_push (m_oparms[i]);
1596 else
1598 m_removed_decls.safe_push (m_oparms[i]);
1599 m_removed_map.put (m_oparms[i], m_removed_decls.length () - 1);
1600 if (MAY_HAVE_DEBUG_STMTS
1601 && !kept[i]
1602 && is_gimple_reg (m_oparms[i]))
1603 m_reset_debug_decls.safe_push (m_oparms[i]);
1607 while (!ssas_to_process_debug.is_empty ())
1608 prepare_debug_expressions (ssas_to_process_debug.pop ());
1611 /* Constructor of ipa_param_body_adjustments from a simple list of
1612 modifications to parameters listed in ADJ_PARAMS which will prepare ground
1613 for modification of parameters of fndecl. Return value of the function will
1614 not be removed and the object will assume it does not run as a part of
1615 tree-function_versioning. */
1617 ipa_param_body_adjustments
1618 ::ipa_param_body_adjustments (vec<ipa_adjusted_param, va_gc> *adj_params,
1619 tree fndecl)
1620 : m_adj_params (adj_params), m_adjustments (NULL), m_reset_debug_decls (),
1621 m_dead_stmts (), m_dead_ssas (), m_dead_ssa_debug_equiv (),
1622 m_dead_stmt_debug_equiv (), m_fndecl (fndecl), m_id (NULL), m_oparms (),
1623 m_new_decls (), m_new_types (), m_replacements (),
1624 m_split_agg_csts_inits (), m_removed_decls (), m_removed_map (),
1625 m_method2func (false), m_sorted_replacements_p (true)
1627 common_initialization (fndecl, NULL, NULL);
1630 /* Constructor of ipa_param_body_adjustments from ipa_param_adjustments in
1631 ADJUSTMENTS which will prepare ground for modification of parameters of
1632 fndecl. The object will assume it does not run as a part of
1633 tree-function_versioning. */
1635 ipa_param_body_adjustments
1636 ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
1637 tree fndecl)
1638 : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
1639 m_reset_debug_decls (), m_dead_stmts (), m_dead_ssas (),
1640 m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (), m_fndecl (fndecl),
1641 m_id (NULL), m_oparms (), m_new_decls (), m_new_types (), m_replacements (),
1642 m_split_agg_csts_inits (), m_removed_decls (), m_removed_map (),
1643 m_method2func (false), m_sorted_replacements_p (true)
1645 common_initialization (fndecl, NULL, NULL);
1648 /* Constructor of ipa_param_body_adjustments which sets it up as a part of
1649 running tree_function_versioning. Planned modifications to the function are
1650 in ADJUSTMENTS. FNDECL designates the new function clone which is being
1651 modified. OLD_FNDECL is the function of which FNDECL is a clone (and which
1652 at the time of invocation still share DECL_ARGUMENTS). ID is the
1653 copy_body_data structure driving the wholy body copying process. VARS is a
1654 pointer to the head of the list of new local variables, TREE_MAP is the map
1655 that drives tree substitution in the cloning process. */
1657 ipa_param_body_adjustments
1658 ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
1659 tree fndecl, tree old_fndecl,
1660 copy_body_data *id, tree *vars,
1661 vec<ipa_replace_map *, va_gc> *tree_map)
1662 : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
1663 m_reset_debug_decls (), m_dead_stmts (), m_dead_ssas (),
1664 m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (), m_fndecl (fndecl),
1665 m_id (id), m_oparms (), m_new_decls (), m_new_types (), m_replacements (),
1666 m_split_agg_csts_inits (), m_removed_decls (), m_removed_map (),
1667 m_method2func (false), m_sorted_replacements_p (true)
1669 common_initialization (old_fndecl, vars, tree_map);
1672 /* Chain new param decls up and return them. */
1674 tree
1675 ipa_param_body_adjustments::get_new_param_chain ()
1677 tree result;
1678 tree *link = &result;
1680 unsigned len = vec_safe_length (m_adj_params);
1681 for (unsigned i = 0; i < len; i++)
1683 tree new_decl = m_new_decls[i];
1684 *link = new_decl;
1685 link = &DECL_CHAIN (new_decl);
1687 *link = NULL_TREE;
1688 return result;
1691 /* Modify the function parameters FNDECL and its type according to the plan in
1692 ADJUSTMENTS. This function needs to be called when the decl has not already
1693 been processed with ipa_param_adjustments::adjust_decl, otherwise just
1694 seting DECL_ARGUMENTS to whatever get_new_param_chain will do is enough. */
1696 void
1697 ipa_param_body_adjustments::modify_formal_parameters ()
1699 tree orig_type = TREE_TYPE (m_fndecl);
1700 DECL_ARGUMENTS (m_fndecl) = get_new_param_chain ();
1702 /* When signature changes, we need to clear builtin info. */
1703 if (fndecl_built_in_p (m_fndecl))
1704 set_decl_built_in_function (m_fndecl, NOT_BUILT_IN, 0);
1706 bool modified = false;
1707 size_t index = 0;
1708 if (m_adj_params)
1709 for (tree t = TYPE_ARG_TYPES (orig_type);
1710 t && !modified;
1711 t = TREE_CHAIN (t), index++)
1712 if (index >= m_adj_params->length ()
1713 || (*m_adj_params)[index].op != IPA_PARAM_OP_COPY
1714 || (*m_adj_params)[index].base_index != index)
1715 modified = true;
1717 /* At this point, removing return value is only implemented when going
1718 through tree_function_versioning, not when modifying function body
1719 directly. */
1720 gcc_assert (!m_adjustments || !m_adjustments->m_skip_return);
1721 tree new_type = build_adjusted_function_type (orig_type, &m_new_types,
1722 m_method2func, false, modified);
1724 TREE_TYPE (m_fndecl) = new_type;
1725 DECL_VIRTUAL_P (m_fndecl) = 0;
1726 DECL_LANG_SPECIFIC (m_fndecl) = NULL;
1727 if (m_method2func)
1728 DECL_VINDEX (m_fndecl) = NULL_TREE;
1731 /* Given BASE and UNIT_OFFSET, find the corresponding record among replacement
1732 structures. */
1734 ipa_param_body_replacement *
1735 ipa_param_body_adjustments::lookup_replacement_1 (tree base,
1736 unsigned unit_offset)
1738 gcc_assert (m_sorted_replacements_p);
1739 ipa_param_body_replacement key;
1740 key.base = base;
1741 key.unit_offset = unit_offset;
1742 ipa_param_body_replacement *res
1743 = std::lower_bound (m_replacements.begin (), m_replacements.end (), key,
1744 [] (const ipa_param_body_replacement &elt,
1745 const ipa_param_body_replacement &val)
1747 return (compare_param_body_replacement (&elt, &val)
1748 < 0);
1751 if (res == m_replacements.end ()
1752 || res->base != base
1753 || res->unit_offset != unit_offset)
1754 return NULL;
1755 return res;
1758 /* Find the first replacement for BASE among m_replacements and return pointer
1759 to it, or NULL if there is none. */
1761 ipa_param_body_replacement *
1762 ipa_param_body_adjustments::lookup_first_base_replacement (tree base)
1764 gcc_assert (m_sorted_replacements_p);
1765 ipa_param_body_replacement key;
1766 key.base = base;
1767 ipa_param_body_replacement *res
1768 = std::lower_bound (m_replacements.begin (), m_replacements.end (), key,
1769 [] (const ipa_param_body_replacement &elt,
1770 const ipa_param_body_replacement &val)
1772 if (DECL_UID (elt.base) < DECL_UID (val.base))
1773 return true;
1774 return false;
1777 if (res == m_replacements.end ()
1778 || res->base != base)
1779 return NULL;
1780 return res;
1783 /* Given BASE and UNIT_OFFSET, find the corresponding replacement expression
1784 and return it, assuming it is known it does not hold value by reference or
1785 in reverse storage order. */
1787 tree
1788 ipa_param_body_adjustments::lookup_replacement (tree base, unsigned unit_offset)
1790 ipa_param_body_replacement *pbr = lookup_replacement_1 (base, unit_offset);
1791 if (!pbr)
1792 return NULL;
1793 return pbr->repl;
1796 /* If T is an SSA_NAME, return NULL if it is not a default def or
1797 return its base variable if it is. If IGNORE_DEFAULT_DEF is true,
1798 the base variable is always returned, regardless if it is a default
1799 def. Return T if it is not an SSA_NAME. */
1801 static tree
1802 get_ssa_base_param (tree t, bool ignore_default_def)
1804 if (TREE_CODE (t) == SSA_NAME)
1806 if (ignore_default_def || SSA_NAME_IS_DEFAULT_DEF (t))
1807 return SSA_NAME_VAR (t);
1808 else
1809 return NULL_TREE;
1811 return t;
1814 /* Given an expression, return the structure describing how it should be
1815 replaced if it accesses a part of a split parameter or NULL otherwise.
1817 Do not free the result, it will be deallocated when the object is destroyed.
1819 If IGNORE_DEFAULT_DEF is cleared, consider only SSA_NAMEs of PARM_DECLs
1820 which are default definitions, if set, consider all SSA_NAMEs of
1821 PARM_DECLs. */
1823 ipa_param_body_replacement *
1824 ipa_param_body_adjustments::get_expr_replacement (tree expr,
1825 bool ignore_default_def)
1827 tree base;
1828 unsigned unit_offset;
1830 if (!isra_get_ref_base_and_offset (expr, &base, &unit_offset))
1831 return NULL;
1833 base = get_ssa_base_param (base, ignore_default_def);
1834 if (!base || TREE_CODE (base) != PARM_DECL)
1835 return NULL;
1836 return lookup_replacement_1 (base, unit_offset);
1839 /* Given OLD_DECL, which is a PARM_DECL of a parameter that is being removed
1840 (which includes it being split or replaced), return a new variable that
1841 should be used for any SSA names that will remain in the function that
1842 previously belonged to OLD_DECL. */
1844 tree
1845 ipa_param_body_adjustments::get_replacement_ssa_base (tree old_decl)
1847 unsigned *idx = m_removed_map.get (old_decl);
1848 if (!idx)
1849 return NULL;
1851 tree repl;
1852 if (TREE_CODE (m_removed_decls[*idx]) == PARM_DECL)
1854 gcc_assert (m_removed_decls[*idx] == old_decl);
1855 repl = copy_var_decl (old_decl, DECL_NAME (old_decl),
1856 TREE_TYPE (old_decl));
1857 m_removed_decls[*idx] = repl;
1859 else
1860 repl = m_removed_decls[*idx];
1861 return repl;
1864 /* If OLD_NAME, which is being defined by statement STMT, is an SSA_NAME of a
1865 parameter which is to be removed because its value is not used, create a new
1866 SSA_NAME relating to a replacement VAR_DECL, replace all uses of the
1867 original with it and return it. If there is no need to re-map, return NULL.
1868 ADJUSTMENTS is a pointer to a vector of IPA-SRA adjustments. */
1870 tree
1871 ipa_param_body_adjustments::replace_removed_params_ssa_names (tree old_name,
1872 gimple *stmt)
1874 gcc_assert (!m_id);
1875 if (TREE_CODE (old_name) != SSA_NAME)
1876 return NULL;
1878 tree decl = SSA_NAME_VAR (old_name);
1879 if (decl == NULL_TREE
1880 || TREE_CODE (decl) != PARM_DECL)
1881 return NULL;
1883 tree repl = get_replacement_ssa_base (decl);
1884 if (!repl)
1885 return NULL;
1887 tree new_name = make_ssa_name (repl, stmt);
1888 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name)
1889 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (old_name);
1891 if (dump_file && (dump_flags & TDF_DETAILS))
1893 fprintf (dump_file, "replacing an SSA name of a removed param ");
1894 print_generic_expr (dump_file, old_name);
1895 fprintf (dump_file, " with ");
1896 print_generic_expr (dump_file, new_name);
1897 fprintf (dump_file, "\n");
1900 replace_uses_by (old_name, new_name);
1901 return new_name;
1904 /* If the expression *EXPR_P should be replaced, do so. CONVERT specifies
1905 whether the function should care about type incompatibility of the current
1906 and new expressions. If it is false, the function will leave
1907 incompatibility issues to the caller - note that when the function
1908 encounters a BIT_FIELD_REF, IMAGPART_EXPR or REALPART_EXPR, it will modify
1909 their bases instead of the expressions themselves and then also performs any
1910 necessary conversions. */
1912 bool
1913 ipa_param_body_adjustments::modify_expression (tree *expr_p, bool convert,
1914 gimple_seq *extra_stmts)
1916 tree expr = *expr_p;
1918 if (m_replacements.is_empty ())
1919 return false;
1920 if (TREE_CODE (expr) == BIT_FIELD_REF
1921 || TREE_CODE (expr) == IMAGPART_EXPR
1922 || TREE_CODE (expr) == REALPART_EXPR)
1924 /* For a BIT_FIELD_REF do not bother to VIEW_CONVERT the base,
1925 instead reference the replacement directly. */
1926 convert = TREE_CODE (expr) != BIT_FIELD_REF;
1927 expr_p = &TREE_OPERAND (expr, 0);
1928 expr = *expr_p;
1931 ipa_param_body_replacement *pbr = get_expr_replacement (expr, false);
1932 if (!pbr)
1933 return false;
1935 tree repl = pbr->repl;
1936 if (dump_file && (dump_flags & TDF_DETAILS))
1938 fprintf (dump_file, "About to replace expr ");
1939 print_generic_expr (dump_file, expr);
1940 fprintf (dump_file, " with ");
1941 print_generic_expr (dump_file, repl);
1942 fprintf (dump_file, "\n");
1945 if (convert && !useless_type_conversion_p (TREE_TYPE (expr),
1946 TREE_TYPE (repl)))
1948 gcc_checking_assert (tree_to_shwi (TYPE_SIZE (TREE_TYPE (expr)))
1949 == tree_to_shwi (TYPE_SIZE (TREE_TYPE (repl))));
1950 tree vce = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expr), repl);
1951 if (is_gimple_reg (repl)
1952 && is_gimple_reg_type (TREE_TYPE (expr)))
1954 gcc_assert (extra_stmts);
1955 vce = force_gimple_operand (vce, extra_stmts, true, NULL_TREE);
1957 *expr_p = vce;
1959 else
1960 *expr_p = repl;
1961 return true;
1964 /* If the assignment statement STMT contains any expressions that need to
1965 replaced with a different one as noted by ADJUSTMENTS, do so. Handle any
1966 potential type incompatibilities. If any conversion sttements have to be
1967 pre-pended to STMT, they will be added to EXTRA_STMTS. Return true iff the
1968 statement was modified. */
1970 bool
1971 ipa_param_body_adjustments::modify_assignment (gimple *stmt,
1972 gimple_seq *extra_stmts)
1974 tree *lhs_p, *rhs_p;
1975 bool any;
1977 if (m_replacements.is_empty () || !gimple_assign_single_p (stmt))
1978 return false;
1980 rhs_p = gimple_assign_rhs1_ptr (stmt);
1981 lhs_p = gimple_assign_lhs_ptr (stmt);
1983 any = modify_expression (lhs_p, false);
1984 any |= modify_expression (rhs_p, false, extra_stmts);
1985 if (any
1986 && !useless_type_conversion_p (TREE_TYPE (*lhs_p), TREE_TYPE (*rhs_p)))
1988 if (TREE_CODE (*rhs_p) == CONSTRUCTOR)
1990 /* V_C_Es of constructors can cause trouble (PR 42714). */
1991 if (is_gimple_reg_type (TREE_TYPE (*lhs_p)))
1992 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
1993 else
1994 *rhs_p = build_constructor (TREE_TYPE (*lhs_p),
1995 NULL);
1997 else
1999 gcc_checking_assert (tree_to_shwi (TYPE_SIZE (TREE_TYPE (*lhs_p)))
2000 == tree_to_shwi (TYPE_SIZE (TREE_TYPE (*rhs_p))));
2001 tree new_rhs = fold_build1_loc (gimple_location (stmt),
2002 VIEW_CONVERT_EXPR, TREE_TYPE (*lhs_p),
2003 *rhs_p);
2004 tree tmp = force_gimple_operand (new_rhs, extra_stmts, true,
2005 NULL_TREE);
2006 gimple_assign_set_rhs1 (stmt, tmp);
2008 return true;
2011 return any;
2014 /* Record information about what modifications to call arguments have already
2015 been done by clone materialization into a summary describing CS. The
2016 information is stored in NEW_INDEX_MAP, NEW_PT_MAP and NEW_ALWAYS_COPY_DELTA
2017 and correspond to equivalent fields in ipa_edge_modification_info. Return
2018 the edge summary. */
2020 static ipa_edge_modification_info *
2021 record_argument_state_1 (cgraph_edge *cs, const vec<int> &new_index_map,
2022 const vec<pass_through_split_map> &new_pt_map,
2023 int new_always_copy_delta)
2026 ipa_edge_modification_info *sum = ipa_edge_modifications->get_create (cs);
2028 unsigned len = sum->pass_through_map.length ();
2029 for (unsigned i = 0; i < len; i++)
2031 unsigned oldnew = sum->pass_through_map[i].new_index;
2032 sum->pass_through_map[i].new_index = new_index_map[oldnew];
2035 len = sum->index_map.length ();
2036 if (len > 0)
2038 unsigned nptlen = new_pt_map.length ();
2039 for (unsigned j = 0; j < nptlen; j++)
2041 int inverse = -1;
2042 for (unsigned i = 0; i < len ; i++)
2043 if ((unsigned) sum->index_map[i] == new_pt_map[j].base_index)
2045 inverse = i;
2046 break;
2048 gcc_assert (inverse >= 0);
2049 pass_through_split_map ptm_item;
2051 ptm_item.base_index = inverse;
2052 ptm_item.unit_offset = new_pt_map[j].unit_offset;
2053 ptm_item.new_index = new_pt_map[j].new_index;
2054 sum->pass_through_map.safe_push (ptm_item);
2057 for (unsigned i = 0; i < len; i++)
2059 int idx = sum->index_map[i];
2060 if (idx < 0)
2061 continue;
2062 sum->index_map[i] = new_index_map[idx];
2065 else
2067 sum->pass_through_map.safe_splice (new_pt_map);
2068 sum->index_map.safe_splice (new_index_map);
2070 sum->always_copy_delta += new_always_copy_delta;
2071 return sum;
2074 /* Record information about what modifications to call arguments have already
2075 been done by clone materialization into a summary of an edge describing the
2076 call in this clone and all its clones. NEW_INDEX_MAP, NEW_PT_MAP and
2077 NEW_ALWAYS_COPY_DELTA have the same meaning as record_argument_state_1.
2079 In order to associate the info with the right edge summaries, we need
2080 address of the ORIG_STMT in the function from which we are cloning (because
2081 the edges have not yet been re-assigned to the new statement that has just
2082 been created) and ID, the structure governing function body copying. */
2084 static void
2085 record_argument_state (copy_body_data *id, gimple *orig_stmt,
2086 const vec<int> &new_index_map,
2087 const vec<pass_through_split_map> &new_pt_map,
2088 int new_always_copy_delta)
2090 if (!ipa_edge_modifications)
2091 ipa_edge_modifications = new ipa_edge_modification_sum (symtab);
2093 struct cgraph_node *this_node = id->dst_node;
2094 ipa_edge_modification_info *first_sum = NULL;
2095 cgraph_edge *cs = this_node->get_edge (orig_stmt);
2096 if (cs)
2097 first_sum = record_argument_state_1 (cs, new_index_map, new_pt_map,
2098 new_always_copy_delta);
2099 else
2100 gcc_assert (this_node->clones);
2102 if (!this_node->clones)
2103 return;
2104 for (cgraph_node *subclone = this_node->clones; subclone != this_node;)
2106 cs = subclone->get_edge (orig_stmt);
2107 if (cs)
2109 if (!first_sum)
2110 first_sum = record_argument_state_1 (cs, new_index_map, new_pt_map,
2111 new_always_copy_delta);
2112 else
2114 ipa_edge_modification_info *s2
2115 = ipa_edge_modifications->get_create (cs);
2116 s2->index_map.truncate (0);
2117 s2->index_map.safe_splice (first_sum->index_map);
2118 s2->pass_through_map.truncate (0);
2119 s2->pass_through_map.safe_splice (first_sum->pass_through_map);
2120 s2->always_copy_delta = first_sum->always_copy_delta;
2123 else
2124 gcc_assert (subclone->clones);
2126 if (subclone->clones)
2127 subclone = subclone->clones;
2128 else if (subclone->next_sibling_clone)
2129 subclone = subclone->next_sibling_clone;
2130 else
2132 while (subclone != this_node && !subclone->next_sibling_clone)
2133 subclone = subclone->clone_of;
2134 if (subclone != this_node)
2135 subclone = subclone->next_sibling_clone;
2140 /* If the call statement pointed at by STMT_P contains any expressions that
2141 need to replaced with a different one as noted by ADJUSTMENTS, do so. f the
2142 statement needs to be rebuilt, do so. Return true if any modifications have
2143 been performed. ORIG_STMT, if not NULL, is the original statement in the
2144 function that is being cloned from, which at this point can be used to look
2145 up call_graph edges.
2147 If the method is invoked as a part of IPA clone materialization and if any
2148 parameter split is pass-through, i.e. it applies to the functin that is
2149 being modified and also to the callee of the statement, replace the
2150 parameter passed to old callee with all of the replacement a callee might
2151 possibly want and record the performed argument modifications in
2152 ipa_edge_modifications. Likewise if any argument has already been left out
2153 because it is not necessary. */
2155 bool
2156 ipa_param_body_adjustments::modify_call_stmt (gcall **stmt_p,
2157 gimple *orig_stmt)
2159 auto_vec <unsigned, 4> pass_through_args;
2160 auto_vec <unsigned, 4> pass_through_pbr_indices;
2161 auto_vec <HOST_WIDE_INT, 4> pass_through_offsets;
2162 gcall *stmt = *stmt_p;
2163 unsigned nargs = gimple_call_num_args (stmt);
2164 bool recreate = false;
2165 gcc_assert (m_sorted_replacements_p);
2167 for (unsigned i = 0; i < gimple_call_num_args (stmt); i++)
2169 tree t = gimple_call_arg (stmt, i);
2170 gcc_assert (TREE_CODE (t) != BIT_FIELD_REF
2171 && TREE_CODE (t) != IMAGPART_EXPR
2172 && TREE_CODE (t) != REALPART_EXPR);
2174 if (TREE_CODE (t) == SSA_NAME
2175 && m_dead_ssas.contains (t))
2176 recreate = true;
2178 if (m_replacements.is_empty ())
2179 continue;
2181 tree base;
2182 unsigned agg_arg_offset;
2183 if (!isra_get_ref_base_and_offset (t, &base, &agg_arg_offset))
2184 continue;
2186 bool by_ref = false;
2187 if (TREE_CODE (base) == SSA_NAME)
2189 if (!SSA_NAME_IS_DEFAULT_DEF (base))
2190 continue;
2191 base = SSA_NAME_VAR (base);
2192 gcc_checking_assert (base);
2193 by_ref = true;
2195 if (TREE_CODE (base) != PARM_DECL)
2196 continue;
2198 ipa_param_body_replacement *first_rep
2199 = lookup_first_base_replacement (base);
2200 if (!first_rep)
2201 continue;
2202 unsigned first_rep_index = first_rep - m_replacements.begin ();
2204 /* We still have to distinguish between an end-use that we have to
2205 transform now and a pass-through, which happens in the following
2206 two cases. */
2208 /* TODO: After we adjust ptr_parm_has_nonarg_uses to also consider
2209 &MEM_REF[ssa_name + offset], we will also have to detect that case
2210 here. */
2212 if (TREE_CODE (t) == SSA_NAME
2213 && SSA_NAME_IS_DEFAULT_DEF (t)
2214 && SSA_NAME_VAR (t)
2215 && TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL)
2217 /* This must be a by_reference pass-through. */
2218 recreate = true;
2219 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2220 pass_through_args.safe_push (i);
2221 pass_through_pbr_indices.safe_push (first_rep_index);
2222 pass_through_offsets.safe_push (agg_arg_offset);
2224 else if (!by_ref && AGGREGATE_TYPE_P (TREE_TYPE (t)))
2226 /* Currently IPA-SRA guarantees the aggregate access type
2227 exactly matches in this case. So if it does not match, it is
2228 a pass-through argument that will be sorted out at edge
2229 redirection time. */
2230 ipa_param_body_replacement *pbr
2231 = lookup_replacement_1 (base, agg_arg_offset);
2233 if (!pbr
2234 || (TYPE_MAIN_VARIANT (TREE_TYPE (t))
2235 != TYPE_MAIN_VARIANT (TREE_TYPE (pbr->repl))))
2237 recreate = true;
2238 pass_through_args.safe_push (i);
2239 pass_through_pbr_indices.safe_push (first_rep_index);
2240 pass_through_offsets.safe_push (agg_arg_offset);
2245 if (!recreate)
2247 /* No need to rebuild the statement, let's just modify arguments
2248 and the LHS if/as appropriate. */
2249 bool modified = false;
2250 for (unsigned i = 0; i < nargs; i++)
2252 tree *t = gimple_call_arg_ptr (stmt, i);
2253 modified |= modify_expression (t, true);
2255 if (gimple_call_lhs (stmt))
2257 tree *t = gimple_call_lhs_ptr (stmt);
2258 modified |= modify_expression (t, false);
2260 return modified;
2263 auto_vec<int, 16> index_map;
2264 auto_vec<pass_through_split_map, 4> pass_through_map;
2265 auto_vec<tree, 16> vargs;
2266 int always_copy_delta = 0;
2267 unsigned pt_idx = 0;
2268 int new_arg_idx = 0;
2269 for (unsigned i = 0; i < nargs; i++)
2271 if (pt_idx < pass_through_args.length ()
2272 && i == pass_through_args[pt_idx])
2274 unsigned j = pass_through_pbr_indices[pt_idx];
2275 unsigned agg_arg_offset = pass_through_offsets[pt_idx];
2276 pt_idx++;
2277 always_copy_delta--;
2278 tree base = m_replacements[j].base;
2280 /* In order to be put into SSA form, we have to push all replacements
2281 pertaining to this parameter as parameters to the call statement.
2282 Edge redirection will need to use edge summary to weed out the
2283 unnecessary ones. */
2284 unsigned repl_list_len = m_replacements.length ();
2285 for (; j < repl_list_len; j++)
2287 if (m_replacements[j].base != base)
2288 break;
2289 if (m_replacements[j].unit_offset < agg_arg_offset)
2290 continue;
2291 pass_through_split_map pt_map;
2292 pt_map.base_index = i;
2293 pt_map.unit_offset
2294 = m_replacements[j].unit_offset - agg_arg_offset;
2295 pt_map.new_index = new_arg_idx;
2296 pass_through_map.safe_push (pt_map);
2297 vargs.safe_push (m_replacements[j].repl);
2298 new_arg_idx++;
2299 always_copy_delta++;
2301 index_map.safe_push (-1);
2303 else
2305 tree t = gimple_call_arg (stmt, i);
2306 if (TREE_CODE (t) == SSA_NAME
2307 && m_dead_ssas.contains (t))
2309 always_copy_delta--;
2310 index_map.safe_push (-1);
2312 else
2314 modify_expression (&t, true);
2315 vargs.safe_push (t);
2316 index_map.safe_push (new_arg_idx);
2317 new_arg_idx++;
2322 gcall *new_stmt = gimple_build_call_vec (gimple_call_fn (stmt), vargs);
2323 if (gimple_has_location (stmt))
2324 gimple_set_location (new_stmt, gimple_location (stmt));
2325 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
2326 gimple_call_copy_flags (new_stmt, stmt);
2327 if (tree lhs = gimple_call_lhs (stmt))
2329 modify_expression (&lhs, false);
2330 /* Avoid adjusting SSA_NAME_DEF_STMT of a SSA lhs, SSA names
2331 have not yet been remapped. */
2332 *gimple_call_lhs_ptr (new_stmt) = lhs;
2334 *stmt_p = new_stmt;
2336 if (orig_stmt)
2337 record_argument_state (m_id, orig_stmt, index_map, pass_through_map,
2338 always_copy_delta);
2339 return true;
2342 /* If the statement STMT contains any expressions that need to replaced with a
2343 different one as noted by ADJUSTMENTS, do so. Handle any potential type
2344 incompatibilities. If any conversion sttements have to be pre-pended to
2345 STMT, they will be added to EXTRA_STMTS. Return true iff the statement was
2346 modified. */
2348 bool
2349 ipa_param_body_adjustments::modify_gimple_stmt (gimple **stmt,
2350 gimple_seq *extra_stmts,
2351 gimple *orig_stmt)
2353 bool modified = false;
2354 tree *t;
2356 switch (gimple_code (*stmt))
2358 case GIMPLE_RETURN:
2359 t = gimple_return_retval_ptr (as_a <greturn *> (*stmt));
2360 if (m_adjustments && m_adjustments->m_skip_return)
2361 *t = NULL_TREE;
2362 else if (*t != NULL_TREE)
2363 modified |= modify_expression (t, true);
2364 break;
2366 case GIMPLE_ASSIGN:
2367 modified |= modify_assignment (*stmt, extra_stmts);
2368 break;
2370 case GIMPLE_CALL:
2371 modified |= modify_call_stmt ((gcall **) stmt, orig_stmt);
2372 break;
2374 case GIMPLE_ASM:
2376 gasm *asm_stmt = as_a <gasm *> (*stmt);
2377 for (unsigned i = 0; i < gimple_asm_ninputs (asm_stmt); i++)
2379 t = &TREE_VALUE (gimple_asm_input_op (asm_stmt, i));
2380 modified |= modify_expression (t, true);
2382 for (unsigned i = 0; i < gimple_asm_noutputs (asm_stmt); i++)
2384 t = &TREE_VALUE (gimple_asm_output_op (asm_stmt, i));
2385 modified |= modify_expression (t, false);
2388 break;
2390 default:
2391 break;
2393 return modified;
2397 /* Traverse body of the current function and perform the requested adjustments
2398 on its statements. Return true iff the CFG has been changed. */
2400 bool
2401 ipa_param_body_adjustments::modify_cfun_body ()
2403 bool cfg_changed = false;
2404 basic_block bb;
2406 FOR_EACH_BB_FN (bb, cfun)
2408 gimple_stmt_iterator gsi;
2410 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2412 gphi *phi = as_a <gphi *> (gsi_stmt (gsi));
2413 tree new_lhs, old_lhs = gimple_phi_result (phi);
2414 new_lhs = replace_removed_params_ssa_names (old_lhs, phi);
2415 if (new_lhs)
2417 gimple_phi_set_result (phi, new_lhs);
2418 release_ssa_name (old_lhs);
2422 gsi = gsi_start_bb (bb);
2423 while (!gsi_end_p (gsi))
2425 gimple *stmt = gsi_stmt (gsi);
2426 gimple *stmt_copy = stmt;
2427 gimple_seq extra_stmts = NULL;
2428 bool modified = modify_gimple_stmt (&stmt, &extra_stmts, NULL);
2429 if (stmt != stmt_copy)
2431 gcc_checking_assert (modified);
2432 gsi_replace (&gsi, stmt, false);
2434 if (!gimple_seq_empty_p (extra_stmts))
2435 gsi_insert_seq_before (&gsi, extra_stmts, GSI_SAME_STMT);
2437 def_operand_p defp;
2438 ssa_op_iter iter;
2439 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF)
2441 tree old_def = DEF_FROM_PTR (defp);
2442 if (tree new_def = replace_removed_params_ssa_names (old_def,
2443 stmt))
2445 SET_DEF (defp, new_def);
2446 release_ssa_name (old_def);
2447 modified = true;
2451 if (modified)
2453 update_stmt (stmt);
2454 if (maybe_clean_eh_stmt (stmt)
2455 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
2456 cfg_changed = true;
2458 gsi_next (&gsi);
2462 return cfg_changed;
2465 /* Call gimple_debug_bind_reset_value on all debug statements describing
2466 gimple register parameters that are being removed or replaced. */
2468 void
2469 ipa_param_body_adjustments::reset_debug_stmts ()
2471 int i, len;
2472 gimple_stmt_iterator *gsip = NULL, gsi;
2474 if (MAY_HAVE_DEBUG_STMTS && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
2476 gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
2477 gsip = &gsi;
2479 len = m_reset_debug_decls.length ();
2480 for (i = 0; i < len; i++)
2482 imm_use_iterator ui;
2483 gimple *stmt;
2484 gdebug *def_temp;
2485 tree name, vexpr, copy = NULL_TREE;
2486 use_operand_p use_p;
2487 tree decl = m_reset_debug_decls[i];
2489 gcc_checking_assert (is_gimple_reg (decl));
2490 name = ssa_default_def (cfun, decl);
2491 vexpr = NULL;
2492 if (name)
2493 FOR_EACH_IMM_USE_STMT (stmt, ui, name)
2495 if (gimple_clobber_p (stmt))
2497 gimple_stmt_iterator cgsi = gsi_for_stmt (stmt);
2498 unlink_stmt_vdef (stmt);
2499 gsi_remove (&cgsi, true);
2500 release_defs (stmt);
2501 continue;
2503 /* All other users must have been removed by function body
2504 modification. */
2505 gcc_assert (is_gimple_debug (stmt));
2506 if (vexpr == NULL && gsip != NULL)
2508 vexpr = build_debug_expr_decl (TREE_TYPE (name));
2509 /* FIXME: Is setting the mode really necessary? */
2510 SET_DECL_MODE (vexpr, DECL_MODE (decl));
2511 def_temp = gimple_build_debug_source_bind (vexpr, decl, NULL);
2512 gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
2514 if (vexpr)
2516 FOR_EACH_IMM_USE_ON_STMT (use_p, ui)
2517 SET_USE (use_p, vexpr);
2519 else
2520 gimple_debug_bind_reset_value (stmt);
2521 update_stmt (stmt);
2523 /* Create a VAR_DECL for debug info purposes. */
2524 if (!DECL_IGNORED_P (decl))
2526 copy = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
2527 VAR_DECL, DECL_NAME (decl),
2528 TREE_TYPE (decl));
2529 if (DECL_PT_UID_SET_P (decl))
2530 SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
2531 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
2532 TREE_READONLY (copy) = TREE_READONLY (decl);
2533 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
2534 DECL_NOT_GIMPLE_REG_P (copy) = DECL_NOT_GIMPLE_REG_P (decl);
2535 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
2536 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
2537 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
2538 DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
2539 SET_DECL_RTL (copy, 0);
2540 TREE_USED (copy) = 1;
2541 DECL_CONTEXT (copy) = current_function_decl;
2542 add_local_decl (cfun, copy);
2543 DECL_CHAIN (copy)
2544 = BLOCK_VARS (DECL_INITIAL (current_function_decl));
2545 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = copy;
2547 if (gsip != NULL && copy && target_for_debug_bind (decl))
2549 gcc_assert (TREE_CODE (decl) == PARM_DECL);
2550 if (vexpr)
2551 def_temp = gimple_build_debug_bind (copy, vexpr, NULL);
2552 else
2553 def_temp = gimple_build_debug_source_bind (copy, decl,
2554 NULL);
2555 gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
2560 /* Perform all necessary body changes to change signature, body and debug info
2561 of fun according to adjustments passed at construction. Return true if CFG
2562 was changed in any way. The main entry point for modification of standalone
2563 functions that is not part of IPA clone materialization. */
2565 bool
2566 ipa_param_body_adjustments::perform_cfun_body_modifications ()
2568 bool cfg_changed;
2569 modify_formal_parameters ();
2570 cfg_changed = modify_cfun_body ();
2571 reset_debug_stmts ();
2573 return cfg_changed;
2577 /* If there are any initialization statements that need to be emitted into
2578 the basic block BB right at ther start of the new function, do so. */
2579 void
2580 ipa_param_body_adjustments::append_init_stmts (basic_block bb)
2582 gimple_stmt_iterator si = gsi_last_bb (bb);
2583 while (!m_split_agg_csts_inits.is_empty ())
2584 gsi_insert_after (&si, m_split_agg_csts_inits.pop (), GSI_NEW_STMT);
2587 /* Deallocate summaries which otherwise stay alive until the end of
2588 compilation. */
2590 void
2591 ipa_edge_modifications_finalize ()
2593 if (!ipa_edge_modifications)
2594 return;
2595 delete ipa_edge_modifications;
2596 ipa_edge_modifications = NULL;
2599 /* Helper used to sort a vector of SSA_NAMES. */
2601 static int
2602 compare_ssa_versions (const void *va, const void *vb)
2604 const_tree const a = *(const_tree const*)va;
2605 const_tree const b = *(const_tree const*)vb;
2607 if (SSA_NAME_VERSION (a) < SSA_NAME_VERSION (b))
2608 return -1;
2609 if (SSA_NAME_VERSION (a) > SSA_NAME_VERSION (b))
2610 return 1;
2611 return 0;
2614 /* Call release_ssa_name on all elements in KILLED_SSAS in a defined order. */
2616 void
2617 ipa_release_ssas_in_hash (hash_set <tree> *killed_ssas)
2619 auto_vec<tree, 16> ssas_to_release;
2620 for (tree sn : *killed_ssas)
2621 ssas_to_release.safe_push (sn);
2622 ssas_to_release.qsort (compare_ssa_versions);
2623 for (tree sn : ssas_to_release)
2624 release_ssa_name (sn);