1 /* Manipulation of formal and actual parameters of functions and function
3 Copyright (C) 2017-2022 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
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
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/>. */
23 #include "coretypes.h"
29 #include "fold-const.h"
31 #include "stor-layout.h"
33 #include "gimple-iterator.h"
34 #include "gimplify-me.h"
37 #include "ipa-param-manipulation.h"
38 #include "print-tree.h"
39 #include "gimple-pretty-print.h"
42 #include "tree-inline.h"
43 #include "alloc-pool.h"
44 #include "symbol-summary.h"
45 #include "symtab-clones.h"
46 #include "tree-phinodes.h"
47 #include "cfgexpand.h"
51 /* Actual prefixes of different newly synthetized parameters. Keep in sync
52 with IPA_PARAM_PREFIX_* defines. */
54 static const char *ipa_param_prefixes
[IPA_PARAM_PREFIX_COUNT
]
60 /* Names of parameters for dumping. Keep in sync with enum ipa_parm_op. */
62 static const char *ipa_param_op_names
[IPA_PARAM_PREFIX_COUNT
]
63 = {"IPA_PARAM_OP_UNDEFINED",
66 "IPA_PARAM_OP_SPLIT"};
68 /* Structure to hold declarations representing pass-through IPA-SRA splits. In
69 essence, it tells new index for a combination of original index and
72 struct pass_through_split_map
74 /* Original argument index. */
76 /* Offset of the split part in the original argument. */
78 /* Index of the split part in the call statement - where clone
79 materialization put it. */
83 /* Information about some call statements that needs to be conveyed from clone
84 materialization to edge redirection. */
86 class ipa_edge_modification_info
89 ipa_edge_modification_info ()
92 /* Mapping of original argument indices to where those arguments sit in the
93 call statement now or to a negative index if they were removed. */
94 auto_vec
<int> index_map
;
95 /* Information about ISRA replacements put into the call statement at the
96 clone materialization stages. */
97 auto_vec
<pass_through_split_map
> pass_through_map
;
98 /* Necessary adjustment to ipa_param_adjustments::m_always_copy_start when
99 redirecting the call. */
100 int always_copy_delta
= 0;
103 /* Class for storing and retrieving summaries about cal statement
106 class ipa_edge_modification_sum
107 : public call_summary
<ipa_edge_modification_info
*>
110 ipa_edge_modification_sum (symbol_table
*table
)
111 : call_summary
<ipa_edge_modification_info
*> (table
)
115 /* Hook that is called by summary when an edge is duplicated. */
117 void duplicate (cgraph_edge
*,
119 ipa_edge_modification_info
*old_info
,
120 ipa_edge_modification_info
*new_info
) final override
122 new_info
->index_map
.safe_splice (old_info
->index_map
);
123 new_info
->pass_through_map
.safe_splice (old_info
->pass_through_map
);
124 new_info
->always_copy_delta
= old_info
->always_copy_delta
;
128 /* Call summary to store information about edges which have had their arguments
129 partially modified already. */
131 static ipa_edge_modification_sum
*ipa_edge_modifications
;
133 /* Fail compilation if CS has any summary associated with it in
134 ipa_edge_modifications. */
137 ipa_verify_edge_has_no_modifications (cgraph_edge
*cs
)
139 gcc_assert (!ipa_edge_modifications
|| !ipa_edge_modifications
->get (cs
));
142 /* Fill an empty vector ARGS with PARM_DECLs representing formal parameters of
143 FNDECL. The function should not be called during LTO WPA phase except for
144 thunks (or functions with bodies streamed in). */
147 push_function_arg_decls (vec
<tree
> *args
, tree fndecl
)
152 /* Safety check that we do not attempt to use the function in WPA, except
153 when the function is a thunk and then we have DECL_ARGUMENTS or when we
154 have already explicitely loaded its body. */
155 gcc_assert (!flag_wpa
156 || DECL_ARGUMENTS (fndecl
)
157 || gimple_has_body_p (fndecl
));
159 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= DECL_CHAIN (parm
))
162 args
->reserve_exact (count
);
163 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= DECL_CHAIN (parm
))
164 args
->quick_push (parm
);
167 /* Fill an empty vector TYPES with trees representing formal parameters of
168 function type FNTYPE. */
171 push_function_arg_types (vec
<tree
> *types
, tree fntype
)
176 for (t
= TYPE_ARG_TYPES (fntype
); t
; t
= TREE_CHAIN (t
))
179 types
->reserve_exact (count
);
180 for (t
= TYPE_ARG_TYPES (fntype
); t
; t
= TREE_CHAIN (t
))
181 types
->quick_push (TREE_VALUE (t
));
184 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
185 friendly way, assuming they are meant to be applied to FNDECL. */
188 ipa_dump_adjusted_parameters (FILE *f
,
189 vec
<ipa_adjusted_param
, va_gc
> *adj_params
)
191 unsigned i
, len
= vec_safe_length (adj_params
);
197 fprintf (f
, " IPA adjusted parameters: ");
198 for (i
= 0; i
< len
; i
++)
200 struct ipa_adjusted_param
*apm
;
201 apm
= &(*adj_params
)[i
];
208 fprintf (f
, "%i. %s %s", i
, ipa_param_op_names
[apm
->op
],
209 apm
->prev_clone_adjustment
? "prev_clone_adjustment " : "");
212 case IPA_PARAM_OP_UNDEFINED
:
215 case IPA_PARAM_OP_COPY
:
216 fprintf (f
, ", base_index: %u", apm
->base_index
);
217 fprintf (f
, ", prev_clone_index: %u", apm
->prev_clone_index
);
220 case IPA_PARAM_OP_SPLIT
:
221 fprintf (f
, ", offset: %u", apm
->unit_offset
);
223 case IPA_PARAM_OP_NEW
:
224 fprintf (f
, ", base_index: %u", apm
->base_index
);
225 fprintf (f
, ", prev_clone_index: %u", apm
->prev_clone_index
);
226 print_node_brief (f
, ", type: ", apm
->type
, 0);
227 print_node_brief (f
, ", alias type: ", apm
->alias_ptr_type
, 0);
228 fprintf (f
, " prefix: %s",
229 ipa_param_prefixes
[apm
->param_prefix_index
]);
231 fprintf (f
, ", reverse");
238 /* Fill NEW_TYPES with types of a function after its current OTYPES have been
239 modified as described in ADJ_PARAMS. When USE_PREV_INDICES is true, use
240 prev_clone_index from ADJ_PARAMS as opposed to base_index when the parameter
244 fill_vector_of_new_param_types (vec
<tree
> *new_types
, vec
<tree
> *otypes
,
245 vec
<ipa_adjusted_param
, va_gc
> *adj_params
,
246 bool use_prev_indices
)
248 unsigned adj_len
= vec_safe_length (adj_params
);
249 new_types
->reserve_exact (adj_len
);
250 for (unsigned i
= 0; i
< adj_len
; i
++)
252 ipa_adjusted_param
*apm
= &(*adj_params
)[i
];
253 if (apm
->op
== IPA_PARAM_OP_COPY
)
256 = use_prev_indices
? apm
->prev_clone_index
: apm
->base_index
;
257 /* The following needs to be handled gracefully because of type
258 mismatches. This happens with LTO but apparently also in Fortran
259 with -fcoarray=lib -O2 -lcaf_single -latomic. */
260 if (index
>= otypes
->length ())
262 new_types
->quick_push ((*otypes
)[index
]);
264 else if (apm
->op
== IPA_PARAM_OP_NEW
265 || apm
->op
== IPA_PARAM_OP_SPLIT
)
267 tree ntype
= apm
->type
;
268 if (is_gimple_reg_type (ntype
)
269 && TYPE_MODE (ntype
) != BLKmode
)
271 unsigned malign
= GET_MODE_ALIGNMENT (TYPE_MODE (ntype
));
272 if (TYPE_ALIGN (ntype
) != malign
)
273 ntype
= build_aligned_type (ntype
, malign
);
275 new_types
->quick_push (ntype
);
282 /* Return false if given attribute should prevent type adjustments. */
285 ipa_param_adjustments::type_attribute_allowed_p (tree name
)
287 if ((is_attribute_p ("fn spec", name
) && flag_ipa_modref
)
288 || is_attribute_p ("access", name
)
289 || is_attribute_p ("returns_nonnull", name
)
290 || is_attribute_p ("assume_aligned", name
)
291 || is_attribute_p ("nocf_check", name
)
292 || is_attribute_p ("warn_unused_result", name
))
297 /* Return true if attribute should be dropped if parameter changed. */
300 drop_type_attribute_if_params_changed_p (tree name
)
302 if (is_attribute_p ("fn spec", name
)
303 || is_attribute_p ("access", name
))
308 /* Build and return a function type just like ORIG_TYPE but with parameter
309 types given in NEW_PARAM_TYPES - which can be NULL if, but only if,
310 ORIG_TYPE itself has NULL TREE_ARG_TYPEs. If METHOD2FUNC is true, also make
311 it a FUNCTION_TYPE instead of FUNCTION_TYPE.
312 If ARG_MODIFIED is true drop attributes that are no longer up to date. */
315 build_adjusted_function_type (tree orig_type
, vec
<tree
> *new_param_types
,
316 bool method2func
, bool skip_return
,
319 tree new_arg_types
= NULL
;
320 if (TYPE_ARG_TYPES (orig_type
))
322 gcc_checking_assert (new_param_types
);
323 bool last_parm_void
= (TREE_VALUE (tree_last (TYPE_ARG_TYPES (orig_type
)))
325 unsigned len
= new_param_types
->length ();
326 for (unsigned i
= 0; i
< len
; i
++)
327 new_arg_types
= tree_cons (NULL_TREE
, (*new_param_types
)[i
],
330 tree new_reversed
= nreverse (new_arg_types
);
334 TREE_CHAIN (new_arg_types
) = void_list_node
;
336 new_reversed
= void_list_node
;
338 new_arg_types
= new_reversed
;
341 /* Use build_distinct_type_copy to preserve as much as possible from original
342 type (debug info, attribute lists etc.). The one exception is
343 METHOD_TYPEs which must have THIS argument and when we are asked to remove
344 it, we need to build new FUNCTION_TYPE instead. */
345 tree new_type
= NULL
;
350 ret_type
= void_type_node
;
352 ret_type
= TREE_TYPE (orig_type
);
355 = build_distinct_type_copy (build_function_type (ret_type
,
357 TYPE_CONTEXT (new_type
) = TYPE_CONTEXT (orig_type
);
361 new_type
= build_distinct_type_copy (orig_type
);
362 TYPE_ARG_TYPES (new_type
) = new_arg_types
;
364 TREE_TYPE (new_type
) = void_type_node
;
366 if (args_modified
&& TYPE_ATTRIBUTES (new_type
))
368 tree t
= TYPE_ATTRIBUTES (new_type
);
369 tree
*last
= &TYPE_ATTRIBUTES (new_type
);
370 TYPE_ATTRIBUTES (new_type
) = NULL
;
371 for (;t
; t
= TREE_CHAIN (t
))
372 if (!drop_type_attribute_if_params_changed_p
373 (get_attribute_name (t
)))
375 *last
= copy_node (t
);
376 TREE_CHAIN (*last
) = NULL
;
377 last
= &TREE_CHAIN (*last
);
384 /* Return the maximum index in any IPA_PARAM_OP_COPY adjustment or -1 if there
388 ipa_param_adjustments::get_max_base_index ()
390 unsigned adj_len
= vec_safe_length (m_adj_params
);
392 for (unsigned i
= 0; i
< adj_len
; i
++)
394 ipa_adjusted_param
*apm
= &(*m_adj_params
)[i
];
395 if (apm
->op
== IPA_PARAM_OP_COPY
396 && max_index
< apm
->base_index
)
397 max_index
= apm
->base_index
;
403 /* Fill SURVIVING_PARAMS with an array of bools where each one says whether a
404 parameter that originally was at that position still survives in the given
405 clone or is removed/replaced. If the final array is smaller than an index
406 of an original parameter, that parameter also did not survive. That a
407 parameter survives does not mean it has the same index as before. */
410 ipa_param_adjustments::get_surviving_params (vec
<bool> *surviving_params
)
412 unsigned adj_len
= vec_safe_length (m_adj_params
);
413 int max_index
= get_max_base_index ();
417 surviving_params
->reserve_exact (max_index
+ 1);
418 surviving_params
->quick_grow_cleared (max_index
+ 1);
419 for (unsigned i
= 0; i
< adj_len
; i
++)
421 ipa_adjusted_param
*apm
= &(*m_adj_params
)[i
];
422 if (apm
->op
== IPA_PARAM_OP_COPY
)
423 (*surviving_params
)[apm
->base_index
] = true;
427 /* Fill NEW_INDICES with new indices of each surviving parameter or -1 for
428 those which do not survive. Any parameter outside of lenght of the vector
429 does not survive. There is currently no support for a parameter to be
430 copied to two distinct new parameters. */
433 ipa_param_adjustments::get_updated_indices (vec
<int> *new_indices
)
435 unsigned adj_len
= vec_safe_length (m_adj_params
);
436 int max_index
= get_max_base_index ();
440 unsigned res_len
= max_index
+ 1;
441 new_indices
->reserve_exact (res_len
);
442 for (unsigned i
= 0; i
< res_len
; i
++)
443 new_indices
->quick_push (-1);
444 for (unsigned i
= 0; i
< adj_len
; i
++)
446 ipa_adjusted_param
*apm
= &(*m_adj_params
)[i
];
447 if (apm
->op
== IPA_PARAM_OP_COPY
)
448 (*new_indices
)[apm
->base_index
] = i
;
452 /* If a parameter with original INDEX has survived intact, return its new
453 index. Otherwise return -1. In that case, if it has been split and there
454 is a new parameter representing a portion at unit OFFSET for which a value
455 of a TYPE can be substituted, store its new index into SPLIT_INDEX,
456 otherwise store -1 there. */
458 ipa_param_adjustments::get_updated_index_or_split (int index
,
459 unsigned unit_offset
,
460 tree type
, int *split_index
)
462 unsigned adj_len
= vec_safe_length (m_adj_params
);
463 for (unsigned i
= 0; i
< adj_len
; i
++)
465 ipa_adjusted_param
*apm
= &(*m_adj_params
)[i
];
466 if (apm
->base_index
!= index
)
468 if (apm
->op
== IPA_PARAM_OP_COPY
)
470 if (apm
->op
== IPA_PARAM_OP_SPLIT
471 && apm
->unit_offset
== unit_offset
)
473 if (useless_type_conversion_p (apm
->type
, type
))
485 /* Return the original index for the given new parameter index. Return a
486 negative number if not available. */
489 ipa_param_adjustments::get_original_index (int newidx
)
491 const ipa_adjusted_param
*adj
= &(*m_adj_params
)[newidx
];
492 if (adj
->op
!= IPA_PARAM_OP_COPY
)
494 return adj
->base_index
;
497 /* Return true if the first parameter (assuming there was one) survives the
498 transformation intact and remains the first one. */
501 ipa_param_adjustments::first_param_intact_p ()
503 return (!vec_safe_is_empty (m_adj_params
)
504 && (*m_adj_params
)[0].op
== IPA_PARAM_OP_COPY
505 && (*m_adj_params
)[0].base_index
== 0);
508 /* Return true if we have to change what has formerly been a method into a
512 ipa_param_adjustments::method2func_p (tree orig_type
)
514 return ((TREE_CODE (orig_type
) == METHOD_TYPE
) && !first_param_intact_p ());
517 /* Given function type OLD_TYPE, return a new type derived from it after
518 performing all atored modifications. TYPE_ORIGINAL_P should be true when
519 OLD_TYPE refers to the type before any IPA transformations, as opposed to a
520 type that can be an intermediate one in between various IPA
524 ipa_param_adjustments::build_new_function_type (tree old_type
,
525 bool type_original_p
)
527 auto_vec
<tree
,16> new_param_types
, *new_param_types_p
;
528 if (prototype_p (old_type
))
530 auto_vec
<tree
, 16> otypes
;
531 push_function_arg_types (&otypes
, old_type
);
532 fill_vector_of_new_param_types (&new_param_types
, &otypes
, m_adj_params
,
534 new_param_types_p
= &new_param_types
;
537 new_param_types_p
= NULL
;
539 /* Check if any params type cares about are modified. In this case will
540 need to drop some type attributes. */
541 bool modified
= false;
544 for (tree t
= TYPE_ARG_TYPES (old_type
);
545 t
&& (int)index
< m_always_copy_start
&& !modified
;
546 t
= TREE_CHAIN (t
), index
++)
547 if (index
>= m_adj_params
->length ()
548 || get_original_index (index
) != (int)index
)
552 return build_adjusted_function_type (old_type
, new_param_types_p
,
553 method2func_p (old_type
), m_skip_return
,
557 /* Build variant of function decl ORIG_DECL which has no return value if
558 M_SKIP_RETURN is true and, if ORIG_DECL's types or parameters is known, has
559 this type adjusted as indicated in M_ADJ_PARAMS. Arguments from
560 DECL_ARGUMENTS list are not processed now, since they are linked by
561 TREE_CHAIN directly and not accessible in LTO during WPA. The caller is
562 responsible for eliminating them when clones are properly materialized. */
565 ipa_param_adjustments::adjust_decl (tree orig_decl
)
567 tree new_decl
= copy_node (orig_decl
);
568 tree orig_type
= TREE_TYPE (orig_decl
);
569 if (prototype_p (orig_type
)
570 || (m_skip_return
&& !VOID_TYPE_P (TREE_TYPE (orig_type
))))
572 tree new_type
= build_new_function_type (orig_type
, false);
573 TREE_TYPE (new_decl
) = new_type
;
575 if (method2func_p (orig_type
))
576 DECL_VINDEX (new_decl
) = NULL_TREE
;
578 /* When signature changes, we need to clear builtin info. */
579 if (fndecl_built_in_p (new_decl
))
580 set_decl_built_in_function (new_decl
, NOT_BUILT_IN
, 0);
582 DECL_VIRTUAL_P (new_decl
) = 0;
583 DECL_LANG_SPECIFIC (new_decl
) = NULL
;
585 /* Drop MALLOC attribute for a void function. */
587 DECL_IS_MALLOC (new_decl
) = 0;
592 /* Wrapper around get_base_ref_and_offset for cases interesting for IPA-SRA
593 transformations. Return true if EXPR has an interesting form and fill in
594 *BASE_P and *UNIT_OFFSET_P with the appropriate info. */
597 isra_get_ref_base_and_offset (tree expr
, tree
*base_p
, unsigned *unit_offset_p
)
599 HOST_WIDE_INT offset
, size
;
602 = get_ref_base_and_extent_hwi (expr
, &offset
, &size
, &reverse
);
603 if (!base
|| size
< 0)
606 if ((offset
% BITS_PER_UNIT
) != 0)
609 if (TREE_CODE (base
) == MEM_REF
)
611 poly_int64 plmoff
= mem_ref_offset (base
).force_shwi ();
613 bool is_cst
= plmoff
.is_constant (&moff
);
616 offset
+= moff
* BITS_PER_UNIT
;
617 base
= TREE_OPERAND (base
, 0);
620 if (offset
< 0 || (offset
/ BITS_PER_UNIT
) > UINT_MAX
)
624 *unit_offset_p
= offset
/ BITS_PER_UNIT
;
628 /* Modify actual arguments of a function call in statement currently belonging
629 to CS, and make it call CS->callee->decl. Return the new statement that
630 replaced the old one. When invoked, cfun and current_function_decl have to
631 be set to the caller. */
634 ipa_param_adjustments::modify_call (cgraph_edge
*cs
,
635 bool update_references
)
637 gcall
*stmt
= cs
->call_stmt
;
638 tree callee_decl
= cs
->callee
->decl
;
640 ipa_edge_modification_info
*mod_info
641 = ipa_edge_modifications
? ipa_edge_modifications
->get (cs
) : NULL
;
642 if (mod_info
&& symtab
->dump_file
)
644 fprintf (symtab
->dump_file
, "Information about pre-exiting "
645 "modifications.\n Index map:");
646 unsigned idx_len
= mod_info
->index_map
.length ();
647 for (unsigned i
= 0; i
< idx_len
; i
++)
648 fprintf (symtab
->dump_file
, " %i", mod_info
->index_map
[i
]);
649 fprintf (symtab
->dump_file
, "\n Pass-through split map: ");
650 unsigned ptm_len
= mod_info
->pass_through_map
.length ();
651 for (unsigned i
= 0; i
< ptm_len
; i
++)
652 fprintf (symtab
->dump_file
,
653 " (base_index: %u, offset: %u, new_index: %i)",
654 mod_info
->pass_through_map
[i
].base_index
,
655 mod_info
->pass_through_map
[i
].unit_offset
,
656 mod_info
->pass_through_map
[i
].new_index
);
657 fprintf (symtab
->dump_file
, "\n Always-copy delta: %i\n",
658 mod_info
->always_copy_delta
);
661 unsigned len
= vec_safe_length (m_adj_params
);
662 auto_vec
<tree
, 16> vargs (len
);
663 unsigned old_nargs
= gimple_call_num_args (stmt
);
664 unsigned orig_nargs
= mod_info
? mod_info
->index_map
.length () : old_nargs
;
665 auto_vec
<bool, 16> kept (old_nargs
);
666 kept
.quick_grow_cleared (old_nargs
);
668 cgraph_node
*current_node
= cgraph_node::get (current_function_decl
);
669 if (update_references
)
670 current_node
->remove_stmt_references (stmt
);
672 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
673 gimple_stmt_iterator prev_gsi
= gsi
;
674 gsi_prev (&prev_gsi
);
675 for (unsigned i
= 0; i
< len
; i
++)
677 ipa_adjusted_param
*apm
= &(*m_adj_params
)[i
];
678 if (apm
->op
== IPA_PARAM_OP_COPY
)
680 int index
= apm
->base_index
;
681 if ((unsigned) index
>= orig_nargs
)
682 /* Can happen if the original call has argument mismatch,
687 index
= mod_info
->index_map
[apm
->base_index
];
688 gcc_assert (index
>= 0);
691 tree arg
= gimple_call_arg (stmt
, index
);
693 vargs
.quick_push (arg
);
698 /* At the moment the only user of IPA_PARAM_OP_NEW modifies calls itself.
699 If we ever want to support it during WPA IPA stage, we'll need a
700 mechanism to call into the IPA passes that introduced them. Currently
701 we simply mandate that IPA infrastructure understands all argument
702 modifications. Remember, edge redirection/modification is done only
703 once, not in steps for each pass modifying the callee like clone
705 gcc_assert (apm
->op
== IPA_PARAM_OP_SPLIT
);
707 /* We have to handle pass-through changes differently using the map
708 clone materialziation might have left behind. */
709 tree repl
= NULL_TREE
;
710 unsigned ptm_len
= mod_info
? mod_info
->pass_through_map
.length () : 0;
711 for (unsigned j
= 0; j
< ptm_len
; j
++)
712 if (mod_info
->pass_through_map
[j
].base_index
== apm
->base_index
713 && mod_info
->pass_through_map
[j
].unit_offset
== apm
->unit_offset
)
715 int repl_idx
= mod_info
->pass_through_map
[j
].new_index
;
716 gcc_assert (repl_idx
>= 0);
717 repl
= gimple_call_arg (stmt
, repl_idx
);
722 vargs
.quick_push (repl
);
726 int index
= apm
->base_index
;
727 if ((unsigned) index
>= orig_nargs
)
728 /* Can happen if the original call has argument mismatch, ignore. */
732 index
= mod_info
->index_map
[apm
->base_index
];
733 gcc_assert (index
>= 0);
735 tree base
= gimple_call_arg (stmt
, index
);
737 /* We create a new parameter out of the value of the old one, we can
738 do the following kind of transformations:
740 - A scalar passed by reference, potentially as a part of a larger
741 aggregate, is converted to a scalar passed by value.
743 - A part of an aggregate is passed instead of the whole aggregate. */
745 location_t loc
= gimple_location (stmt
);
747 bool deref_base
= false;
748 unsigned int deref_align
= 0;
749 if (TREE_CODE (base
) != ADDR_EXPR
750 && is_gimple_reg_type (TREE_TYPE (base
)))
752 /* Detect type mismatches in calls in invalid programs and make a
753 poor attempt to gracefully convert them so that we don't ICE. */
754 if (!POINTER_TYPE_P (TREE_TYPE (base
)))
755 base
= force_value_to_type (ptr_type_node
, base
);
757 off
= build_int_cst (apm
->alias_ptr_type
, apm
->unit_offset
);
762 if (TREE_CODE (base
) == ADDR_EXPR
)
764 base
= TREE_OPERAND (base
, 0);
770 tree prev_base
= base
;
771 poly_int64 base_offset
;
772 base
= get_addr_base_and_unit_offset (base
, &base_offset
);
774 /* Aggregate arguments can have non-invariant addresses. */
777 base
= build_fold_addr_expr (prev_base
);
778 off
= build_int_cst (apm
->alias_ptr_type
, apm
->unit_offset
);
780 else if (TREE_CODE (base
) == MEM_REF
)
785 deref_align
= TYPE_ALIGN (TREE_TYPE (base
));
787 off
= build_int_cst (apm
->alias_ptr_type
,
788 base_offset
+ apm
->unit_offset
);
789 off
= int_const_binop (PLUS_EXPR
, TREE_OPERAND (base
, 1),
791 base
= TREE_OPERAND (base
, 0);
795 off
= build_int_cst (apm
->alias_ptr_type
,
796 base_offset
+ apm
->unit_offset
);
797 base
= build_fold_addr_expr (base
);
801 tree type
= apm
->type
;
803 unsigned HOST_WIDE_INT misalign
;
812 get_pointer_alignment_1 (base
, &align
, &misalign
);
813 /* All users must make sure that we can be optimistic when it
814 comes to alignment in this case (by inspecting the final users
815 of these new parameters). */
816 if (TYPE_ALIGN (type
) > align
)
817 align
= TYPE_ALIGN (type
);
820 += (offset_int::from (wi::to_wide (off
), SIGNED
).to_short_addr ()
822 misalign
= misalign
& (align
- 1);
824 align
= least_bit_hwi (misalign
);
825 if (align
< TYPE_ALIGN (type
))
826 type
= build_aligned_type (type
, align
);
827 base
= force_gimple_operand_gsi (&gsi
, base
,
828 true, NULL
, true, GSI_SAME_STMT
);
829 tree expr
= fold_build2_loc (loc
, MEM_REF
, type
, base
, off
);
830 REF_REVERSE_STORAGE_ORDER (expr
) = apm
->reverse
;
831 /* If expr is not a valid gimple call argument emit
832 a load into a temporary. */
833 if (is_gimple_reg_type (TREE_TYPE (expr
)))
835 gimple
*tem
= gimple_build_assign (NULL_TREE
, expr
);
836 if (gimple_in_ssa_p (cfun
))
838 gimple_set_vuse (tem
, gimple_vuse (stmt
));
839 expr
= make_ssa_name (TREE_TYPE (expr
), tem
);
842 expr
= create_tmp_reg (TREE_TYPE (expr
));
843 gimple_assign_set_lhs (tem
, expr
);
844 gsi_insert_before (&gsi
, tem
, GSI_SAME_STMT
);
846 vargs
.quick_push (expr
);
849 if (m_always_copy_start
>= 0)
851 int always_copy_start
= m_always_copy_start
;
854 always_copy_start
+= mod_info
->always_copy_delta
;
855 gcc_assert (always_copy_start
>= 0);
857 for (unsigned i
= always_copy_start
; i
< old_nargs
; i
++)
858 vargs
.safe_push (gimple_call_arg (stmt
, i
));
861 /* For optimized away parameters, add on the caller side
863 DEBUG D#X => parm_Y(D)
864 stmts and associate D#X with parm in decl_debug_args_lookup
865 vector to say for debug info that if parameter parm had been passed,
866 it would have value parm_Y(D). */
867 tree old_decl
= gimple_call_fndecl (stmt
);
868 if (MAY_HAVE_DEBUG_BIND_STMTS
&& old_decl
&& callee_decl
)
870 vec
<tree
, va_gc
> **debug_args
= NULL
;
872 cgraph_node
*callee_node
= cgraph_node::get (callee_decl
);
874 /* FIXME: we don't seem to be able to insert debug args before clone
875 is materialized. Materializing them early leads to extra memory
877 if (callee_node
->clone_of
)
878 callee_node
->get_untransformed_body ();
879 for (tree old_parm
= DECL_ARGUMENTS (old_decl
);
880 old_parm
&& i
< old_nargs
&& ((int) i
) < m_always_copy_start
;
881 old_parm
= DECL_CHAIN (old_parm
), i
++)
883 if (!is_gimple_reg (old_parm
) || kept
[i
])
888 if (mod_info
->index_map
[i
] < 0)
890 arg
= gimple_call_arg (stmt
, mod_info
->index_map
[i
]);
893 arg
= gimple_call_arg (stmt
, i
);
895 tree origin
= DECL_ORIGIN (old_parm
);
896 if (!useless_type_conversion_p (TREE_TYPE (origin
), TREE_TYPE (arg
)))
898 if (!fold_convertible_p (TREE_TYPE (origin
), arg
))
901 if (TREE_CODE (arg
) == SSA_NAME
902 && gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg
))
904 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (arg
)))
905 && useless_type_conversion_p (TREE_TYPE (origin
),
909 arg
= fold_convert_loc (gimple_location (stmt
),
910 TREE_TYPE (origin
), arg
);
912 if (debug_args
== NULL
)
913 debug_args
= decl_debug_args_insert (callee_decl
);
915 tree ddecl
= NULL_TREE
;
916 for (ix
= 0; vec_safe_iterate (*debug_args
, ix
, &ddecl
); ix
+= 2)
919 ddecl
= (**debug_args
)[ix
+ 1];
924 ddecl
= build_debug_expr_decl (TREE_TYPE (origin
));
925 /* FIXME: Is setting the mode really necessary? */
926 SET_DECL_MODE (ddecl
, DECL_MODE (origin
));
928 vec_safe_push (*debug_args
, origin
);
929 vec_safe_push (*debug_args
, ddecl
);
931 gimple
*def_temp
= gimple_build_debug_bind (ddecl
,
932 unshare_expr (arg
), stmt
);
933 gsi_insert_before (&gsi
, def_temp
, GSI_SAME_STMT
);
937 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
939 fprintf (dump_file
, "replacing stmt:");
940 print_gimple_stmt (dump_file
, gsi_stmt (gsi
), 0);
943 gcall
*new_stmt
= gimple_build_call_vec (callee_decl
, vargs
);
945 tree ssa_to_remove
= NULL
;
946 if (tree lhs
= gimple_call_lhs (stmt
))
949 gimple_call_set_lhs (new_stmt
, lhs
);
950 else if (TREE_CODE (lhs
) == SSA_NAME
)
952 /* LHS should now by a default-def SSA. Unfortunately default-def
953 SSA_NAMEs need a backing variable (or at least some code examining
954 SSAs assumes it is non-NULL). So we either have to re-use the
955 decl we have at hand or introdice a new one. */
956 tree repl
= create_tmp_var (TREE_TYPE (lhs
), "removed_return");
957 repl
= get_or_create_ssa_default_def (cfun
, repl
);
958 SSA_NAME_IS_DEFAULT_DEF (repl
) = true;
962 FOR_EACH_IMM_USE_STMT (using_stmt
, ui
, lhs
)
964 FOR_EACH_IMM_USE_ON_STMT (use_p
, ui
)
966 SET_USE (use_p
, repl
);
968 update_stmt (using_stmt
);
974 gimple_set_block (new_stmt
, gimple_block (stmt
));
975 if (gimple_has_location (stmt
))
976 gimple_set_location (new_stmt
, gimple_location (stmt
));
977 gimple_call_set_chain (new_stmt
, gimple_call_chain (stmt
));
978 gimple_call_copy_flags (new_stmt
, stmt
);
979 if (gimple_in_ssa_p (cfun
))
980 gimple_move_vops (new_stmt
, stmt
);
982 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
984 fprintf (dump_file
, "with stmt:");
985 print_gimple_stmt (dump_file
, new_stmt
, 0);
986 fprintf (dump_file
, "\n");
988 gsi_replace (&gsi
, new_stmt
, true);
990 release_ssa_name (ssa_to_remove
);
991 if (update_references
)
994 current_node
->record_stmt_references (gsi_stmt (gsi
));
997 while (gsi_stmt (gsi
) != gsi_stmt (prev_gsi
));
1000 ipa_edge_modifications
->remove (cs
);
1004 /* Dump information contained in the object in textual form to F. */
1007 ipa_param_adjustments::dump (FILE *f
)
1009 fprintf (f
, " m_always_copy_start: %i\n", m_always_copy_start
);
1010 ipa_dump_adjusted_parameters (f
, m_adj_params
);
1012 fprintf (f
, " Will SKIP return.\n");
1015 /* Dump information contained in the object in textual form to stderr. */
1018 ipa_param_adjustments::debug ()
1023 /* Register that REPLACEMENT should replace parameter described in APM. */
1026 ipa_param_body_adjustments::register_replacement (ipa_adjusted_param
*apm
,
1029 gcc_checking_assert (apm
->op
== IPA_PARAM_OP_SPLIT
1030 || apm
->op
== IPA_PARAM_OP_NEW
);
1031 gcc_checking_assert (!apm
->prev_clone_adjustment
);
1032 ipa_param_body_replacement psr
;
1033 psr
.base
= m_oparms
[apm
->prev_clone_index
];
1034 psr
.repl
= replacement
;
1035 psr
.dummy
= NULL_TREE
;
1036 psr
.unit_offset
= apm
->unit_offset
;
1037 m_replacements
.safe_push (psr
);
1040 /* Copy or not, as appropriate given m_id and decl context, a pre-existing
1041 PARM_DECL T so that it can be included in the parameters of the modified
1045 ipa_param_body_adjustments::carry_over_param (tree t
)
1050 new_parm
= remap_decl (t
, m_id
);
1051 if (TREE_CODE (new_parm
) != PARM_DECL
)
1052 new_parm
= m_id
->copy_decl (t
, m_id
);
1054 else if (DECL_CONTEXT (t
) != m_fndecl
)
1056 new_parm
= copy_node (t
);
1057 DECL_CONTEXT (new_parm
) = m_fndecl
;
1064 /* Populate m_dead_stmts given that DEAD_PARAM is going to be removed without
1065 any replacement or splitting. REPL is the replacement VAR_SECL to base any
1066 remaining uses of a removed parameter on. Push all removed SSA names that
1067 are used within debug statements to DEBUGSTACK. */
1070 ipa_param_body_adjustments::mark_dead_statements (tree dead_param
,
1071 vec
<tree
> *debugstack
)
1073 /* Current IPA analyses which remove unused parameters never remove a
1074 non-gimple register ones which have any use except as parameters in other
1075 calls, so we can safely leve them as they are. */
1076 if (!is_gimple_reg (dead_param
))
1078 tree parm_ddef
= ssa_default_def (m_id
->src_cfun
, dead_param
);
1079 if (!parm_ddef
|| has_zero_uses (parm_ddef
))
1082 auto_vec
<tree
, 4> stack
;
1083 hash_set
<tree
> used_in_debug
;
1084 m_dead_ssas
.add (parm_ddef
);
1085 stack
.safe_push (parm_ddef
);
1086 while (!stack
.is_empty ())
1088 imm_use_iterator imm_iter
;
1089 use_operand_p use_p
;
1090 tree t
= stack
.pop ();
1092 insert_decl_map (m_id
, t
, error_mark_node
);
1093 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, t
)
1095 gimple
*stmt
= USE_STMT (use_p
);
1097 /* Calls containing dead arguments cannot be deleted,
1098 modify_call_stmt will instead remove just the argument later on.
1099 If isra_track_scalar_value_uses in ipa-sra.cc is extended to look
1100 through const functions, we will need to do so here too. */
1101 if (is_gimple_call (stmt
)
1102 || (m_id
->blocks_to_copy
1103 && !bitmap_bit_p (m_id
->blocks_to_copy
,
1104 gimple_bb (stmt
)->index
)))
1107 if (is_gimple_debug (stmt
))
1109 m_dead_stmts
.add (stmt
);
1110 gcc_assert (gimple_debug_bind_p (stmt
));
1111 if (!used_in_debug
.contains (t
))
1113 used_in_debug
.add (t
);
1114 debugstack
->safe_push (t
);
1117 else if (gimple_code (stmt
) == GIMPLE_PHI
)
1119 gphi
*phi
= as_a
<gphi
*> (stmt
);
1120 int ix
= PHI_ARG_INDEX_FROM_USE (use_p
);
1122 if (!m_id
->blocks_to_copy
1123 || bitmap_bit_p (m_id
->blocks_to_copy
,
1124 gimple_phi_arg_edge (phi
, ix
)->src
->index
))
1126 m_dead_stmts
.add (phi
);
1127 tree res
= gimple_phi_result (phi
);
1128 if (!m_dead_ssas
.add (res
))
1129 stack
.safe_push (res
);
1132 else if (is_gimple_assign (stmt
))
1134 m_dead_stmts
.add (stmt
);
1135 if (!gimple_clobber_p (stmt
))
1137 tree lhs
= gimple_assign_lhs (stmt
);
1138 gcc_assert (TREE_CODE (lhs
) == SSA_NAME
);
1139 if (!m_dead_ssas
.add (lhs
))
1140 stack
.safe_push (lhs
);
1144 /* IPA-SRA does not analyze other types of statements. */
1149 if (!MAY_HAVE_DEBUG_STMTS
)
1151 gcc_assert (debugstack
->is_empty ());
1155 tree dp_ddecl
= build_debug_expr_decl (TREE_TYPE (dead_param
));
1156 /* FIXME: Is setting the mode really necessary? */
1157 SET_DECL_MODE (dp_ddecl
, DECL_MODE (dead_param
));
1158 m_dead_ssa_debug_equiv
.put (parm_ddef
, dp_ddecl
);
1161 /* Callback to walk_tree. If REMAP is an SSA_NAME that is present in hash_map
1162 passed in DATA, replace it with unshared version of what it was mapped to.
1163 If an SSA argument would be remapped to NULL, the whole operation needs to
1164 abort which is signaled by returning error_mark_node. */
1167 replace_with_mapped_expr (tree
*remap
, int *walk_subtrees
, void *data
)
1169 if (TYPE_P (*remap
))
1174 if (TREE_CODE (*remap
) != SSA_NAME
)
1179 hash_map
<tree
, tree
> *equivs
= (hash_map
<tree
, tree
> *) data
;
1180 if (tree
*p
= equivs
->get (*remap
))
1183 return error_mark_node
;
1184 *remap
= unshare_expr (*p
);
1189 /* Replace all occurances of SSAs in m_dead_ssa_debug_equiv in t with what they
1193 ipa_param_body_adjustments::remap_with_debug_expressions (tree
*t
)
1195 /* If *t is an SSA_NAME which should have its debug statements reset, it is
1196 mapped to NULL in the hash_map.
1198 It is perhaps simpler to handle the SSA_NAME cases directly and only
1199 invoke walk_tree on more complex expressions. When
1200 remap_with_debug_expressions is called from tree-inline.cc, a to-be-reset
1201 SSA_NAME can be an operand to such expressions and the entire debug
1202 variable we are remapping should be reset. This is signaled by walk_tree
1203 returning error_mark_node and done by setting *t to NULL. */
1204 if (TREE_CODE (*t
) == SSA_NAME
)
1206 if (tree
*p
= m_dead_ssa_debug_equiv
.get (*t
))
1209 else if (walk_tree (t
, replace_with_mapped_expr
,
1210 &m_dead_ssa_debug_equiv
, NULL
) == error_mark_node
)
1214 /* For an SSA_NAME DEAD_SSA which is about to be DCEd because it is based on a
1215 useless parameter, prepare an expression that should represent it in
1216 debug_binds in the cloned function and add a mapping from DEAD_SSA to
1217 m_dead_ssa_debug_equiv. That mapping is to NULL when the associated
1218 debug_statement has to be reset instead. In such case return false,
1219 ottherwise return true. If DEAD_SSA comes from a basic block which is not
1220 about to be copied, ignore it and return true. */
1223 ipa_param_body_adjustments::prepare_debug_expressions (tree dead_ssa
)
1225 gcc_checking_assert (m_dead_ssas
.contains (dead_ssa
));
1226 if (tree
*d
= m_dead_ssa_debug_equiv
.get (dead_ssa
))
1227 return (*d
!= NULL_TREE
);
1229 gcc_assert (!SSA_NAME_IS_DEFAULT_DEF (dead_ssa
));
1230 gimple
*def
= SSA_NAME_DEF_STMT (dead_ssa
);
1231 if (m_id
->blocks_to_copy
1232 && !bitmap_bit_p (m_id
->blocks_to_copy
, gimple_bb (def
)->index
))
1235 if (gimple_code (def
) == GIMPLE_PHI
)
1237 /* In theory, we could ignore all SSAs coming from BBs not in
1238 m_id->blocks_to_copy but at the time of the writing this code that
1239 should never really be the case because only fnsplit uses that bitmap,
1241 tree value
= degenerate_phi_result (as_a
<gphi
*> (def
));
1243 || (m_dead_ssas
.contains (value
)
1244 && !prepare_debug_expressions (value
)))
1246 m_dead_ssa_debug_equiv
.put (dead_ssa
, NULL_TREE
);
1250 gcc_assert (TREE_CODE (value
) == SSA_NAME
);
1251 tree
*d
= m_dead_ssa_debug_equiv
.get (value
);
1252 m_dead_ssa_debug_equiv
.put (dead_ssa
, *d
);
1257 use_operand_p use_p
;
1259 FOR_EACH_PHI_OR_STMT_USE (use_p
, def
, oi
, SSA_OP_USE
)
1261 tree use
= USE_FROM_PTR (use_p
);
1262 if (m_dead_ssas
.contains (use
)
1263 && !prepare_debug_expressions (use
))
1272 m_dead_ssa_debug_equiv
.put (dead_ssa
, NULL_TREE
);
1276 if (is_gimple_assign (def
))
1278 gcc_assert (!gimple_clobber_p (def
));
1279 if (gimple_assign_copy_p (def
)
1280 && TREE_CODE (gimple_assign_rhs1 (def
)) == SSA_NAME
)
1282 tree d
= *m_dead_ssa_debug_equiv
.get (gimple_assign_rhs1 (def
));
1284 m_dead_ssa_debug_equiv
.put (dead_ssa
, d
);
1289 = unshare_expr_without_location (gimple_assign_rhs_to_tree (def
));
1290 remap_with_debug_expressions (&val
);
1292 tree vexpr
= build_debug_expr_decl (TREE_TYPE (val
));
1293 m_dead_stmt_debug_equiv
.put (def
, val
);
1294 m_dead_ssa_debug_equiv
.put (dead_ssa
, vexpr
);
1301 /* Common initialization performed by all ipa_param_body_adjustments
1302 constructors. OLD_FNDECL is the declaration we take original arguments
1303 from, (it may be the same as M_FNDECL). VARS, if non-NULL, is a pointer to
1304 a chained list of new local variables. TREE_MAP is the IPA-CP produced
1305 mapping of trees to constants.
1307 The function is rather long but it really onlu initializes all data members
1308 of the class. It creates new param DECLs, finds their new types, */
1311 ipa_param_body_adjustments::common_initialization (tree old_fndecl
,
1313 vec
<ipa_replace_map
*,
1316 push_function_arg_decls (&m_oparms
, old_fndecl
);
1317 auto_vec
<tree
,16> otypes
;
1318 if (TYPE_ARG_TYPES (TREE_TYPE (old_fndecl
)) != NULL_TREE
)
1319 push_function_arg_types (&otypes
, TREE_TYPE (old_fndecl
));
1322 auto_vec
<tree
,16> oparms
;
1323 push_function_arg_decls (&oparms
, old_fndecl
);
1324 unsigned ocount
= oparms
.length ();
1325 otypes
.reserve_exact (ocount
);
1326 for (unsigned i
= 0; i
< ocount
; i
++)
1327 otypes
.quick_push (TREE_TYPE (oparms
[i
]));
1329 fill_vector_of_new_param_types (&m_new_types
, &otypes
, m_adj_params
, true);
1331 auto_vec
<bool, 16> kept
;
1332 kept
.reserve_exact (m_oparms
.length ());
1333 kept
.quick_grow_cleared (m_oparms
.length ());
1334 auto_vec
<bool, 16> split
;
1335 split
.reserve_exact (m_oparms
.length ());
1336 split
.quick_grow_cleared (m_oparms
.length ());
1338 unsigned adj_len
= vec_safe_length (m_adj_params
);
1339 m_method2func
= ((TREE_CODE (TREE_TYPE (m_fndecl
)) == METHOD_TYPE
)
1341 || (*m_adj_params
)[0].op
!= IPA_PARAM_OP_COPY
1342 || (*m_adj_params
)[0].base_index
!= 0));
1344 /* The main job of the this function is to go over the vector of adjusted
1345 parameters and create declarations or find corresponding old ones and push
1346 them to m_new_decls. For IPA-SRA replacements it also creates
1347 corresponding m_id->dst_node->clone.performed_splits entries. */
1349 m_new_decls
.reserve_exact (adj_len
);
1350 for (unsigned i
= 0; i
< adj_len
; i
++)
1352 ipa_adjusted_param
*apm
= &(*m_adj_params
)[i
];
1353 unsigned prev_index
= apm
->prev_clone_index
;
1355 if (apm
->op
== IPA_PARAM_OP_COPY
1356 || apm
->prev_clone_adjustment
)
1358 kept
[prev_index
] = true;
1359 new_parm
= carry_over_param (m_oparms
[prev_index
]);
1360 m_new_decls
.quick_push (new_parm
);
1362 else if (apm
->op
== IPA_PARAM_OP_NEW
1363 || apm
->op
== IPA_PARAM_OP_SPLIT
)
1365 tree new_type
= m_new_types
[i
];
1366 gcc_checking_assert (new_type
);
1367 new_parm
= build_decl (UNKNOWN_LOCATION
, PARM_DECL
, NULL_TREE
,
1369 const char *prefix
= ipa_param_prefixes
[apm
->param_prefix_index
];
1370 DECL_NAME (new_parm
) = create_tmp_var_name (prefix
);
1371 DECL_ARTIFICIAL (new_parm
) = 1;
1372 DECL_ARG_TYPE (new_parm
) = new_type
;
1373 DECL_CONTEXT (new_parm
) = m_fndecl
;
1374 TREE_USED (new_parm
) = 1;
1375 DECL_IGNORED_P (new_parm
) = 1;
1376 layout_decl (new_parm
, 0);
1377 m_new_decls
.quick_push (new_parm
);
1379 if (apm
->op
== IPA_PARAM_OP_SPLIT
)
1381 split
[prev_index
] = true;
1382 register_replacement (apm
, new_parm
);
1391 /* Do not treat parameters which were replaced with a constant as
1392 completely vanished. */
1393 auto_vec
<int, 16> index_mapping
;
1394 bool need_remap
= false;
1398 clone_info
*cinfo
= clone_info::get (m_id
->src_node
);
1399 if (cinfo
&& cinfo
->param_adjustments
)
1401 cinfo
->param_adjustments
->get_updated_indices (&index_mapping
);
1406 for (unsigned i
= 0; i
< tree_map
->length (); i
++)
1408 int parm_num
= (*tree_map
)[i
]->parm_num
;
1409 gcc_assert (parm_num
>= 0);
1411 parm_num
= index_mapping
[parm_num
];
1412 kept
[parm_num
] = true;
1416 /* As part of body modifications, we will also have to replace remaining uses
1417 of remaining uses of removed PARM_DECLs (which do not however use the
1418 initial value) with their VAR_DECL copies.
1420 We do this differently with and without m_id. With m_id, we rely on its
1421 mapping and create a replacement straight away. Without it, we have our
1422 own mechanism for which we have to populate m_removed_decls vector. Just
1423 don't mix them, that is why you should not call
1424 replace_removed_params_ssa_names or perform_cfun_body_modifications when
1425 you construct with ID not equal to NULL. */
1427 auto_vec
<tree
, 8> ssas_to_process_debug
;
1428 unsigned op_len
= m_oparms
.length ();
1429 for (unsigned i
= 0; i
< op_len
; i
++)
1434 gcc_assert (!m_id
->decl_map
->get (m_oparms
[i
]));
1435 tree var
= copy_decl_to_var (m_oparms
[i
], m_id
);
1436 insert_decl_map (m_id
, m_oparms
[i
], var
);
1437 /* Declare this new variable. */
1438 DECL_CHAIN (var
) = *vars
;
1441 /* If this is not a split but a real removal, init hash sets
1442 that will guide what not to copy to the new body. */
1444 mark_dead_statements (m_oparms
[i
], &ssas_to_process_debug
);
1445 if (MAY_HAVE_DEBUG_STMTS
1446 && is_gimple_reg (m_oparms
[i
]))
1447 m_reset_debug_decls
.safe_push (m_oparms
[i
]);
1451 m_removed_decls
.safe_push (m_oparms
[i
]);
1452 m_removed_map
.put (m_oparms
[i
], m_removed_decls
.length () - 1);
1453 if (MAY_HAVE_DEBUG_STMTS
1455 && is_gimple_reg (m_oparms
[i
]))
1456 m_reset_debug_decls
.safe_push (m_oparms
[i
]);
1460 while (!ssas_to_process_debug
.is_empty ())
1461 prepare_debug_expressions (ssas_to_process_debug
.pop ());
1464 /* Constructor of ipa_param_body_adjustments from a simple list of
1465 modifications to parameters listed in ADJ_PARAMS which will prepare ground
1466 for modification of parameters of fndecl. Return value of the function will
1467 not be removed and the object will assume it does not run as a part of
1468 tree-function_versioning. */
1470 ipa_param_body_adjustments
1471 ::ipa_param_body_adjustments (vec
<ipa_adjusted_param
, va_gc
> *adj_params
,
1473 : m_adj_params (adj_params
), m_adjustments (NULL
), m_reset_debug_decls (),
1474 m_dead_stmts (), m_dead_ssas (), m_dead_ssa_debug_equiv (),
1475 m_dead_stmt_debug_equiv (), m_fndecl (fndecl
), m_id (NULL
), m_oparms (),
1476 m_new_decls (), m_new_types (), m_replacements (), m_removed_decls (),
1477 m_removed_map (), m_method2func (false)
1479 common_initialization (fndecl
, NULL
, NULL
);
1482 /* Constructor of ipa_param_body_adjustments from ipa_param_adjustments in
1483 ADJUSTMENTS which will prepare ground for modification of parameters of
1484 fndecl. The object will assume it does not run as a part of
1485 tree-function_versioning. */
1487 ipa_param_body_adjustments
1488 ::ipa_param_body_adjustments (ipa_param_adjustments
*adjustments
,
1490 : m_adj_params (adjustments
->m_adj_params
), m_adjustments (adjustments
),
1491 m_reset_debug_decls (), m_dead_stmts (), m_dead_ssas (),
1492 m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (), m_fndecl (fndecl
),
1493 m_id (NULL
), m_oparms (), m_new_decls (), m_new_types (), m_replacements (),
1494 m_removed_decls (), m_removed_map (), m_method2func (false)
1496 common_initialization (fndecl
, NULL
, NULL
);
1499 /* Constructor of ipa_param_body_adjustments which sets it up as a part of
1500 running tree_function_versioning. Planned modifications to the function are
1501 in ADJUSTMENTS. FNDECL designates the new function clone which is being
1502 modified. OLD_FNDECL is the function of which FNDECL is a clone (and which
1503 at the time of invocation still share DECL_ARGUMENTS). ID is the
1504 copy_body_data structure driving the wholy body copying process. VARS is a
1505 pointer to the head of the list of new local variables, TREE_MAP is the map
1506 that drives tree substitution in the cloning process. */
1508 ipa_param_body_adjustments
1509 ::ipa_param_body_adjustments (ipa_param_adjustments
*adjustments
,
1510 tree fndecl
, tree old_fndecl
,
1511 copy_body_data
*id
, tree
*vars
,
1512 vec
<ipa_replace_map
*, va_gc
> *tree_map
)
1513 : m_adj_params (adjustments
->m_adj_params
), m_adjustments (adjustments
),
1514 m_reset_debug_decls (), m_dead_stmts (), m_dead_ssas (),
1515 m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (), m_fndecl (fndecl
),
1516 m_id (id
), m_oparms (), m_new_decls (), m_new_types (), m_replacements (),
1517 m_removed_decls (), m_removed_map (), m_method2func (false)
1519 common_initialization (old_fndecl
, vars
, tree_map
);
1522 /* Chain new param decls up and return them. */
1525 ipa_param_body_adjustments::get_new_param_chain ()
1528 tree
*link
= &result
;
1530 unsigned len
= vec_safe_length (m_adj_params
);
1531 for (unsigned i
= 0; i
< len
; i
++)
1533 tree new_decl
= m_new_decls
[i
];
1535 link
= &DECL_CHAIN (new_decl
);
1541 /* Modify the function parameters FNDECL and its type according to the plan in
1542 ADJUSTMENTS. This function needs to be called when the decl has not already
1543 been processed with ipa_param_adjustments::adjust_decl, otherwise just
1544 seting DECL_ARGUMENTS to whatever get_new_param_chain will do is enough. */
1547 ipa_param_body_adjustments::modify_formal_parameters ()
1549 tree orig_type
= TREE_TYPE (m_fndecl
);
1550 DECL_ARGUMENTS (m_fndecl
) = get_new_param_chain ();
1552 /* When signature changes, we need to clear builtin info. */
1553 if (fndecl_built_in_p (m_fndecl
))
1554 set_decl_built_in_function (m_fndecl
, NOT_BUILT_IN
, 0);
1556 bool modified
= false;
1559 for (tree t
= TYPE_ARG_TYPES (orig_type
);
1561 t
= TREE_CHAIN (t
), index
++)
1562 if (index
>= m_adj_params
->length ()
1563 || (*m_adj_params
)[index
].op
!= IPA_PARAM_OP_COPY
1564 || (*m_adj_params
)[index
].base_index
!= index
)
1567 /* At this point, removing return value is only implemented when going
1568 through tree_function_versioning, not when modifying function body
1570 gcc_assert (!m_adjustments
|| !m_adjustments
->m_skip_return
);
1571 tree new_type
= build_adjusted_function_type (orig_type
, &m_new_types
,
1572 m_method2func
, false, modified
);
1574 TREE_TYPE (m_fndecl
) = new_type
;
1575 DECL_VIRTUAL_P (m_fndecl
) = 0;
1576 DECL_LANG_SPECIFIC (m_fndecl
) = NULL
;
1578 DECL_VINDEX (m_fndecl
) = NULL_TREE
;
1581 /* Given BASE and UNIT_OFFSET, find the corresponding record among replacement
1584 ipa_param_body_replacement
*
1585 ipa_param_body_adjustments::lookup_replacement_1 (tree base
,
1586 unsigned unit_offset
)
1588 unsigned int len
= m_replacements
.length ();
1589 for (unsigned i
= 0; i
< len
; i
++)
1591 ipa_param_body_replacement
*pbr
= &m_replacements
[i
];
1593 if (pbr
->base
== base
1594 && (pbr
->unit_offset
== unit_offset
))
1600 /* Given BASE and UNIT_OFFSET, find the corresponding replacement expression
1601 and return it, assuming it is known it does not hold value by reference or
1602 in reverse storage order. */
1605 ipa_param_body_adjustments::lookup_replacement (tree base
, unsigned unit_offset
)
1607 ipa_param_body_replacement
*pbr
= lookup_replacement_1 (base
, unit_offset
);
1613 /* If T is an SSA_NAME, return NULL if it is not a default def or
1614 return its base variable if it is. If IGNORE_DEFAULT_DEF is true,
1615 the base variable is always returned, regardless if it is a default
1616 def. Return T if it is not an SSA_NAME. */
1619 get_ssa_base_param (tree t
, bool ignore_default_def
)
1621 if (TREE_CODE (t
) == SSA_NAME
)
1623 if (ignore_default_def
|| SSA_NAME_IS_DEFAULT_DEF (t
))
1624 return SSA_NAME_VAR (t
);
1631 /* Given an expression, return the structure describing how it should be
1632 replaced if it accesses a part of a split parameter or NULL otherwise.
1634 Do not free the result, it will be deallocated when the object is destroyed.
1636 If IGNORE_DEFAULT_DEF is cleared, consider only SSA_NAMEs of PARM_DECLs
1637 which are default definitions, if set, consider all SSA_NAMEs of
1640 ipa_param_body_replacement
*
1641 ipa_param_body_adjustments::get_expr_replacement (tree expr
,
1642 bool ignore_default_def
)
1645 unsigned unit_offset
;
1647 if (!isra_get_ref_base_and_offset (expr
, &base
, &unit_offset
))
1650 base
= get_ssa_base_param (base
, ignore_default_def
);
1651 if (!base
|| TREE_CODE (base
) != PARM_DECL
)
1653 return lookup_replacement_1 (base
, unit_offset
);
1656 /* Given OLD_DECL, which is a PARM_DECL of a parameter that is being removed
1657 (which includes it being split or replaced), return a new variable that
1658 should be used for any SSA names that will remain in the function that
1659 previously belonged to OLD_DECL. */
1662 ipa_param_body_adjustments::get_replacement_ssa_base (tree old_decl
)
1664 unsigned *idx
= m_removed_map
.get (old_decl
);
1669 if (TREE_CODE (m_removed_decls
[*idx
]) == PARM_DECL
)
1671 gcc_assert (m_removed_decls
[*idx
] == old_decl
);
1672 repl
= copy_var_decl (old_decl
, DECL_NAME (old_decl
),
1673 TREE_TYPE (old_decl
));
1674 m_removed_decls
[*idx
] = repl
;
1677 repl
= m_removed_decls
[*idx
];
1681 /* If OLD_NAME, which is being defined by statement STMT, is an SSA_NAME of a
1682 parameter which is to be removed because its value is not used, create a new
1683 SSA_NAME relating to a replacement VAR_DECL, replace all uses of the
1684 original with it and return it. If there is no need to re-map, return NULL.
1685 ADJUSTMENTS is a pointer to a vector of IPA-SRA adjustments. */
1688 ipa_param_body_adjustments::replace_removed_params_ssa_names (tree old_name
,
1692 if (TREE_CODE (old_name
) != SSA_NAME
)
1695 tree decl
= SSA_NAME_VAR (old_name
);
1696 if (decl
== NULL_TREE
1697 || TREE_CODE (decl
) != PARM_DECL
)
1700 tree repl
= get_replacement_ssa_base (decl
);
1704 tree new_name
= make_ssa_name (repl
, stmt
);
1705 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name
)
1706 = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (old_name
);
1708 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1710 fprintf (dump_file
, "replacing an SSA name of a removed param ");
1711 print_generic_expr (dump_file
, old_name
);
1712 fprintf (dump_file
, " with ");
1713 print_generic_expr (dump_file
, new_name
);
1714 fprintf (dump_file
, "\n");
1717 replace_uses_by (old_name
, new_name
);
1721 /* If the expression *EXPR_P should be replaced, do so. CONVERT specifies
1722 whether the function should care about type incompatibility of the current
1723 and new expressions. If it is false, the function will leave
1724 incompatibility issues to the caller - note that when the function
1725 encounters a BIT_FIELD_REF, IMAGPART_EXPR or REALPART_EXPR, it will modify
1726 their bases instead of the expressions themselves and then also performs any
1727 necessary conversions. */
1730 ipa_param_body_adjustments::modify_expression (tree
*expr_p
, bool convert
)
1732 tree expr
= *expr_p
;
1734 if (TREE_CODE (expr
) == BIT_FIELD_REF
1735 || TREE_CODE (expr
) == IMAGPART_EXPR
1736 || TREE_CODE (expr
) == REALPART_EXPR
)
1738 expr_p
= &TREE_OPERAND (expr
, 0);
1743 ipa_param_body_replacement
*pbr
= get_expr_replacement (expr
, false);
1747 tree repl
= pbr
->repl
;
1748 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1750 fprintf (dump_file
, "About to replace expr ");
1751 print_generic_expr (dump_file
, expr
);
1752 fprintf (dump_file
, " with ");
1753 print_generic_expr (dump_file
, repl
);
1754 fprintf (dump_file
, "\n");
1757 if (convert
&& !useless_type_conversion_p (TREE_TYPE (expr
),
1760 tree vce
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (expr
), repl
);
1768 /* If the assignment statement STMT contains any expressions that need to
1769 replaced with a different one as noted by ADJUSTMENTS, do so. Handle any
1770 potential type incompatibilities. If any conversion sttements have to be
1771 pre-pended to STMT, they will be added to EXTRA_STMTS. Return true iff the
1772 statement was modified. */
1775 ipa_param_body_adjustments::modify_assignment (gimple
*stmt
,
1776 gimple_seq
*extra_stmts
)
1778 tree
*lhs_p
, *rhs_p
;
1781 if (!gimple_assign_single_p (stmt
))
1784 rhs_p
= gimple_assign_rhs1_ptr (stmt
);
1785 lhs_p
= gimple_assign_lhs_ptr (stmt
);
1787 any
= modify_expression (lhs_p
, false);
1788 any
|= modify_expression (rhs_p
, false);
1790 && !useless_type_conversion_p (TREE_TYPE (*lhs_p
), TREE_TYPE (*rhs_p
)))
1792 if (TREE_CODE (*rhs_p
) == CONSTRUCTOR
)
1794 /* V_C_Es of constructors can cause trouble (PR 42714). */
1795 if (is_gimple_reg_type (TREE_TYPE (*lhs_p
)))
1796 *rhs_p
= build_zero_cst (TREE_TYPE (*lhs_p
));
1798 *rhs_p
= build_constructor (TREE_TYPE (*lhs_p
),
1803 tree new_rhs
= fold_build1_loc (gimple_location (stmt
),
1804 VIEW_CONVERT_EXPR
, TREE_TYPE (*lhs_p
),
1806 tree tmp
= force_gimple_operand (new_rhs
, extra_stmts
, true,
1808 gimple_assign_set_rhs1 (stmt
, tmp
);
1816 /* Record information about what modifications to call arguments have already
1817 been done by clone materialization into a summary describing CS. The
1818 information is stored in NEW_INDEX_MAP, NEW_PT_MAP and NEW_ALWAYS_COPY_DELTA
1819 and correspond to equivalent fields in ipa_edge_modification_info. Return
1820 the edge summary. */
1822 static ipa_edge_modification_info
*
1823 record_argument_state_1 (cgraph_edge
*cs
, const vec
<int> &new_index_map
,
1824 const vec
<pass_through_split_map
> &new_pt_map
,
1825 int new_always_copy_delta
)
1828 ipa_edge_modification_info
*sum
= ipa_edge_modifications
->get_create (cs
);
1830 unsigned len
= sum
->pass_through_map
.length ();
1831 for (unsigned i
= 0; i
< len
; i
++)
1833 unsigned oldnew
= sum
->pass_through_map
[i
].new_index
;
1834 sum
->pass_through_map
[i
].new_index
= new_index_map
[oldnew
];
1837 len
= sum
->index_map
.length ();
1840 unsigned nptlen
= new_pt_map
.length ();
1841 for (unsigned j
= 0; j
< nptlen
; j
++)
1844 for (unsigned i
= 0; i
< len
; i
++)
1845 if ((unsigned) sum
->index_map
[i
] == new_pt_map
[j
].base_index
)
1850 gcc_assert (inverse
>= 0);
1851 pass_through_split_map ptm_item
;
1853 ptm_item
.base_index
= inverse
;
1854 ptm_item
.unit_offset
= new_pt_map
[j
].unit_offset
;
1855 ptm_item
.new_index
= new_pt_map
[j
].new_index
;
1856 sum
->pass_through_map
.safe_push (ptm_item
);
1859 for (unsigned i
= 0; i
< len
; i
++)
1861 int idx
= sum
->index_map
[i
];
1864 sum
->index_map
[i
] = new_index_map
[idx
];
1869 sum
->pass_through_map
.safe_splice (new_pt_map
);
1870 sum
->index_map
.safe_splice (new_index_map
);
1872 sum
->always_copy_delta
+= new_always_copy_delta
;
1876 /* Record information about what modifications to call arguments have already
1877 been done by clone materialization into a summary of an edge describing the
1878 call in this clone and all its clones. NEW_INDEX_MAP, NEW_PT_MAP and
1879 NEW_ALWAYS_COPY_DELTA have the same meaning as record_argument_state_1.
1881 In order to associate the info with the right edge summaries, we need
1882 address of the ORIG_STMT in the function from which we are cloning (because
1883 the edges have not yet been re-assigned to the new statement that has just
1884 been created) and ID, the structure governing function body copying. */
1887 record_argument_state (copy_body_data
*id
, gimple
*orig_stmt
,
1888 const vec
<int> &new_index_map
,
1889 const vec
<pass_through_split_map
> &new_pt_map
,
1890 int new_always_copy_delta
)
1892 if (!ipa_edge_modifications
)
1893 ipa_edge_modifications
= new ipa_edge_modification_sum (symtab
);
1895 struct cgraph_node
*this_node
= id
->dst_node
;
1896 ipa_edge_modification_info
*first_sum
= NULL
;
1897 cgraph_edge
*cs
= this_node
->get_edge (orig_stmt
);
1899 first_sum
= record_argument_state_1 (cs
, new_index_map
, new_pt_map
,
1900 new_always_copy_delta
);
1902 gcc_assert (this_node
->clones
);
1904 if (!this_node
->clones
)
1906 for (cgraph_node
*subclone
= this_node
->clones
; subclone
!= this_node
;)
1908 cs
= subclone
->get_edge (orig_stmt
);
1912 first_sum
= record_argument_state_1 (cs
, new_index_map
, new_pt_map
,
1913 new_always_copy_delta
);
1916 ipa_edge_modification_info
*s2
1917 = ipa_edge_modifications
->get_create (cs
);
1918 s2
->index_map
.truncate (0);
1919 s2
->index_map
.safe_splice (first_sum
->index_map
);
1920 s2
->pass_through_map
.truncate (0);
1921 s2
->pass_through_map
.safe_splice (first_sum
->pass_through_map
);
1922 s2
->always_copy_delta
= first_sum
->always_copy_delta
;
1926 gcc_assert (subclone
->clones
);
1928 if (subclone
->clones
)
1929 subclone
= subclone
->clones
;
1930 else if (subclone
->next_sibling_clone
)
1931 subclone
= subclone
->next_sibling_clone
;
1934 while (subclone
!= this_node
&& !subclone
->next_sibling_clone
)
1935 subclone
= subclone
->clone_of
;
1936 if (subclone
!= this_node
)
1937 subclone
= subclone
->next_sibling_clone
;
1942 /* If the call statement pointed at by STMT_P contains any expressions that
1943 need to replaced with a different one as noted by ADJUSTMENTS, do so. f the
1944 statement needs to be rebuilt, do so. Return true if any modifications have
1945 been performed. ORIG_STMT, if not NULL, is the original statement in the
1946 function that is being cloned from, which at this point can be used to look
1947 up call_graph edges.
1949 If the method is invoked as a part of IPA clone materialization and if any
1950 parameter split is pass-through, i.e. it applies to the functin that is
1951 being modified and also to the callee of the statement, replace the
1952 parameter passed to old callee with all of the replacement a callee might
1953 possibly want and record the performed argument modifications in
1954 ipa_edge_modifications. Likewise if any argument has already been left out
1955 because it is not necessary. */
1958 ipa_param_body_adjustments::modify_call_stmt (gcall
**stmt_p
,
1961 auto_vec
<unsigned, 4> pass_through_args
;
1962 auto_vec
<unsigned, 4> pass_through_pbr_indices
;
1963 auto_vec
<HOST_WIDE_INT
, 4> pass_through_offsets
;
1964 gcall
*stmt
= *stmt_p
;
1965 unsigned nargs
= gimple_call_num_args (stmt
);
1966 bool recreate
= false;
1968 for (unsigned i
= 0; i
< gimple_call_num_args (stmt
); i
++)
1970 tree t
= gimple_call_arg (stmt
, i
);
1971 gcc_assert (TREE_CODE (t
) != BIT_FIELD_REF
1972 && TREE_CODE (t
) != IMAGPART_EXPR
1973 && TREE_CODE (t
) != REALPART_EXPR
);
1975 if (TREE_CODE (t
) == SSA_NAME
1976 && m_dead_ssas
.contains (t
))
1979 if (m_replacements
.is_empty ())
1983 unsigned agg_arg_offset
;
1984 if (!isra_get_ref_base_and_offset (t
, &base
, &agg_arg_offset
))
1987 bool by_ref
= false;
1988 if (TREE_CODE (base
) == SSA_NAME
)
1990 if (!SSA_NAME_IS_DEFAULT_DEF (base
))
1992 base
= SSA_NAME_VAR (base
);
1993 gcc_checking_assert (base
);
1996 if (TREE_CODE (base
) != PARM_DECL
)
1999 bool base_among_replacements
= false;
2000 unsigned j
, repl_list_len
= m_replacements
.length ();
2001 for (j
= 0; j
< repl_list_len
; j
++)
2003 ipa_param_body_replacement
*pbr
= &m_replacements
[j
];
2004 if (pbr
->base
== base
)
2006 base_among_replacements
= true;
2010 if (!base_among_replacements
)
2013 /* We still have to distinguish between an end-use that we have to
2014 transform now and a pass-through, which happens in the following
2017 /* TODO: After we adjust ptr_parm_has_nonarg_uses to also consider
2018 &MEM_REF[ssa_name + offset], we will also have to detect that case
2021 if (TREE_CODE (t
) == SSA_NAME
2022 && SSA_NAME_IS_DEFAULT_DEF (t
)
2024 && TREE_CODE (SSA_NAME_VAR (t
)) == PARM_DECL
)
2026 /* This must be a by_reference pass-through. */
2028 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t
)));
2029 pass_through_args
.safe_push (i
);
2030 pass_through_pbr_indices
.safe_push (j
);
2031 pass_through_offsets
.safe_push (agg_arg_offset
);
2033 else if (!by_ref
&& AGGREGATE_TYPE_P (TREE_TYPE (t
)))
2035 /* Currently IPA-SRA guarantees the aggregate access type
2036 exactly matches in this case. So if it does not match, it is
2037 a pass-through argument that will be sorted out at edge
2038 redirection time. */
2039 ipa_param_body_replacement
*pbr
2040 = lookup_replacement_1 (base
, agg_arg_offset
);
2043 || (TYPE_MAIN_VARIANT (TREE_TYPE (t
))
2044 != TYPE_MAIN_VARIANT (TREE_TYPE (pbr
->repl
))))
2047 pass_through_args
.safe_push (i
);
2048 pass_through_pbr_indices
.safe_push (j
);
2049 pass_through_offsets
.safe_push (agg_arg_offset
);
2056 /* No need to rebuild the statement, let's just modify arguments
2057 and the LHS if/as appropriate. */
2058 bool modified
= false;
2059 for (unsigned i
= 0; i
< nargs
; i
++)
2061 tree
*t
= gimple_call_arg_ptr (stmt
, i
);
2062 modified
|= modify_expression (t
, true);
2064 if (gimple_call_lhs (stmt
))
2066 tree
*t
= gimple_call_lhs_ptr (stmt
);
2067 modified
|= modify_expression (t
, false);
2072 auto_vec
<int, 16> index_map
;
2073 auto_vec
<pass_through_split_map
, 4> pass_through_map
;
2074 auto_vec
<tree
, 16> vargs
;
2075 int always_copy_delta
= 0;
2076 unsigned pt_idx
= 0;
2077 int new_arg_idx
= 0;
2078 for (unsigned i
= 0; i
< nargs
; i
++)
2080 if (pt_idx
< pass_through_args
.length ()
2081 && i
== pass_through_args
[pt_idx
])
2083 unsigned j
= pass_through_pbr_indices
[pt_idx
];
2084 unsigned agg_arg_offset
= pass_through_offsets
[pt_idx
];
2086 always_copy_delta
--;
2087 tree base
= m_replacements
[j
].base
;
2089 /* In order to be put into SSA form, we have to push all replacements
2090 pertaining to this parameter as parameters to the call statement.
2091 Edge redirection will need to use edge summary to weed out the
2092 unnecessary ones. */
2093 unsigned repl_list_len
= m_replacements
.length ();
2094 for (; j
< repl_list_len
; j
++)
2096 if (m_replacements
[j
].base
!= base
)
2098 if (m_replacements
[j
].unit_offset
< agg_arg_offset
)
2100 pass_through_split_map pt_map
;
2101 pt_map
.base_index
= i
;
2103 = m_replacements
[j
].unit_offset
- agg_arg_offset
;
2104 pt_map
.new_index
= new_arg_idx
;
2105 pass_through_map
.safe_push (pt_map
);
2106 vargs
.safe_push (m_replacements
[j
].repl
);
2108 always_copy_delta
++;
2110 index_map
.safe_push (-1);
2114 tree t
= gimple_call_arg (stmt
, i
);
2115 if (TREE_CODE (t
) == SSA_NAME
2116 && m_dead_ssas
.contains (t
))
2118 always_copy_delta
--;
2119 index_map
.safe_push (-1);
2123 modify_expression (&t
, true);
2124 vargs
.safe_push (t
);
2125 index_map
.safe_push (new_arg_idx
);
2131 gcall
*new_stmt
= gimple_build_call_vec (gimple_call_fn (stmt
), vargs
);
2132 if (gimple_has_location (stmt
))
2133 gimple_set_location (new_stmt
, gimple_location (stmt
));
2134 gimple_call_set_chain (new_stmt
, gimple_call_chain (stmt
));
2135 gimple_call_copy_flags (new_stmt
, stmt
);
2136 if (tree lhs
= gimple_call_lhs (stmt
))
2138 modify_expression (&lhs
, false);
2139 /* Avoid adjusting SSA_NAME_DEF_STMT of a SSA lhs, SSA names
2140 have not yet been remapped. */
2141 *gimple_call_lhs_ptr (new_stmt
) = lhs
;
2146 record_argument_state (m_id
, orig_stmt
, index_map
, pass_through_map
,
2151 /* If the statement STMT contains any expressions that need to replaced with a
2152 different one as noted by ADJUSTMENTS, do so. Handle any potential type
2153 incompatibilities. If any conversion sttements have to be pre-pended to
2154 STMT, they will be added to EXTRA_STMTS. Return true iff the statement was
2158 ipa_param_body_adjustments::modify_gimple_stmt (gimple
**stmt
,
2159 gimple_seq
*extra_stmts
,
2162 bool modified
= false;
2165 switch (gimple_code (*stmt
))
2168 t
= gimple_return_retval_ptr (as_a
<greturn
*> (*stmt
));
2169 if (m_adjustments
&& m_adjustments
->m_skip_return
)
2171 else if (*t
!= NULL_TREE
)
2172 modified
|= modify_expression (t
, true);
2176 modified
|= modify_assignment (*stmt
, extra_stmts
);
2180 modified
|= modify_call_stmt ((gcall
**) stmt
, orig_stmt
);
2185 gasm
*asm_stmt
= as_a
<gasm
*> (*stmt
);
2186 for (unsigned i
= 0; i
< gimple_asm_ninputs (asm_stmt
); i
++)
2188 t
= &TREE_VALUE (gimple_asm_input_op (asm_stmt
, i
));
2189 modified
|= modify_expression (t
, true);
2191 for (unsigned i
= 0; i
< gimple_asm_noutputs (asm_stmt
); i
++)
2193 t
= &TREE_VALUE (gimple_asm_output_op (asm_stmt
, i
));
2194 modified
|= modify_expression (t
, false);
2206 /* Traverse body of the current function and perform the requested adjustments
2207 on its statements. Return true iff the CFG has been changed. */
2210 ipa_param_body_adjustments::modify_cfun_body ()
2212 bool cfg_changed
= false;
2215 FOR_EACH_BB_FN (bb
, cfun
)
2217 gimple_stmt_iterator gsi
;
2219 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2221 gphi
*phi
= as_a
<gphi
*> (gsi_stmt (gsi
));
2222 tree new_lhs
, old_lhs
= gimple_phi_result (phi
);
2223 new_lhs
= replace_removed_params_ssa_names (old_lhs
, phi
);
2226 gimple_phi_set_result (phi
, new_lhs
);
2227 release_ssa_name (old_lhs
);
2231 gsi
= gsi_start_bb (bb
);
2232 while (!gsi_end_p (gsi
))
2234 gimple
*stmt
= gsi_stmt (gsi
);
2235 gimple
*stmt_copy
= stmt
;
2236 gimple_seq extra_stmts
= NULL
;
2237 bool modified
= modify_gimple_stmt (&stmt
, &extra_stmts
, NULL
);
2238 if (stmt
!= stmt_copy
)
2240 gcc_checking_assert (modified
);
2241 gsi_replace (&gsi
, stmt
, false);
2243 if (!gimple_seq_empty_p (extra_stmts
))
2244 gsi_insert_seq_before (&gsi
, extra_stmts
, GSI_SAME_STMT
);
2248 FOR_EACH_SSA_DEF_OPERAND (defp
, stmt
, iter
, SSA_OP_DEF
)
2250 tree old_def
= DEF_FROM_PTR (defp
);
2251 if (tree new_def
= replace_removed_params_ssa_names (old_def
,
2254 SET_DEF (defp
, new_def
);
2255 release_ssa_name (old_def
);
2263 if (maybe_clean_eh_stmt (stmt
)
2264 && gimple_purge_dead_eh_edges (gimple_bb (stmt
)))
2274 /* Call gimple_debug_bind_reset_value on all debug statements describing
2275 gimple register parameters that are being removed or replaced. */
2278 ipa_param_body_adjustments::reset_debug_stmts ()
2281 gimple_stmt_iterator
*gsip
= NULL
, gsi
;
2283 if (MAY_HAVE_DEBUG_STMTS
&& single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun
)))
2285 gsi
= gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)));
2288 len
= m_reset_debug_decls
.length ();
2289 for (i
= 0; i
< len
; i
++)
2291 imm_use_iterator ui
;
2294 tree name
, vexpr
, copy
= NULL_TREE
;
2295 use_operand_p use_p
;
2296 tree decl
= m_reset_debug_decls
[i
];
2298 gcc_checking_assert (is_gimple_reg (decl
));
2299 name
= ssa_default_def (cfun
, decl
);
2302 FOR_EACH_IMM_USE_STMT (stmt
, ui
, name
)
2304 if (gimple_clobber_p (stmt
))
2306 gimple_stmt_iterator cgsi
= gsi_for_stmt (stmt
);
2307 unlink_stmt_vdef (stmt
);
2308 gsi_remove (&cgsi
, true);
2309 release_defs (stmt
);
2312 /* All other users must have been removed by function body
2314 gcc_assert (is_gimple_debug (stmt
));
2315 if (vexpr
== NULL
&& gsip
!= NULL
)
2317 vexpr
= build_debug_expr_decl (TREE_TYPE (name
));
2318 /* FIXME: Is setting the mode really necessary? */
2319 SET_DECL_MODE (vexpr
, DECL_MODE (decl
));
2320 def_temp
= gimple_build_debug_source_bind (vexpr
, decl
, NULL
);
2321 gsi_insert_before (gsip
, def_temp
, GSI_SAME_STMT
);
2325 FOR_EACH_IMM_USE_ON_STMT (use_p
, ui
)
2326 SET_USE (use_p
, vexpr
);
2329 gimple_debug_bind_reset_value (stmt
);
2332 /* Create a VAR_DECL for debug info purposes. */
2333 if (!DECL_IGNORED_P (decl
))
2335 copy
= build_decl (DECL_SOURCE_LOCATION (current_function_decl
),
2336 VAR_DECL
, DECL_NAME (decl
),
2338 if (DECL_PT_UID_SET_P (decl
))
2339 SET_DECL_PT_UID (copy
, DECL_PT_UID (decl
));
2340 TREE_ADDRESSABLE (copy
) = TREE_ADDRESSABLE (decl
);
2341 TREE_READONLY (copy
) = TREE_READONLY (decl
);
2342 TREE_THIS_VOLATILE (copy
) = TREE_THIS_VOLATILE (decl
);
2343 DECL_NOT_GIMPLE_REG_P (copy
) = DECL_NOT_GIMPLE_REG_P (decl
);
2344 DECL_ARTIFICIAL (copy
) = DECL_ARTIFICIAL (decl
);
2345 DECL_IGNORED_P (copy
) = DECL_IGNORED_P (decl
);
2346 DECL_ABSTRACT_ORIGIN (copy
) = DECL_ORIGIN (decl
);
2347 DECL_SEEN_IN_BIND_EXPR_P (copy
) = 1;
2348 SET_DECL_RTL (copy
, 0);
2349 TREE_USED (copy
) = 1;
2350 DECL_CONTEXT (copy
) = current_function_decl
;
2351 add_local_decl (cfun
, copy
);
2353 = BLOCK_VARS (DECL_INITIAL (current_function_decl
));
2354 BLOCK_VARS (DECL_INITIAL (current_function_decl
)) = copy
;
2356 if (gsip
!= NULL
&& copy
&& target_for_debug_bind (decl
))
2358 gcc_assert (TREE_CODE (decl
) == PARM_DECL
);
2360 def_temp
= gimple_build_debug_bind (copy
, vexpr
, NULL
);
2362 def_temp
= gimple_build_debug_source_bind (copy
, decl
,
2364 gsi_insert_before (gsip
, def_temp
, GSI_SAME_STMT
);
2369 /* Perform all necessary body changes to change signature, body and debug info
2370 of fun according to adjustments passed at construction. Return true if CFG
2371 was changed in any way. The main entry point for modification of standalone
2372 functions that is not part of IPA clone materialization. */
2375 ipa_param_body_adjustments::perform_cfun_body_modifications ()
2378 modify_formal_parameters ();
2379 cfg_changed
= modify_cfun_body ();
2380 reset_debug_stmts ();
2386 /* Deallocate summaries which otherwise stay alive until the end of
2390 ipa_edge_modifications_finalize ()
2392 if (!ipa_edge_modifications
)
2394 delete ipa_edge_modifications
;
2395 ipa_edge_modifications
= NULL
;