Line data Source code
1 : /* Manipulation of formal and actual parameters of functions and function
2 : calls.
3 : Copyright (C) 2017-2026 Free Software Foundation, Inc.
4 :
5 : This file is part of GCC.
6 :
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.
11 :
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.
16 :
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/>. */
20 :
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"
53 : #include "data-streamer.h"
54 : #include "attr-callback.h"
55 : #include "callback-info.h"
56 :
57 : /* Actual prefixes of different newly synthesized parameters. Keep in sync
58 : with IPA_PARAM_PREFIX_* defines. */
59 :
60 : static const char *ipa_param_prefixes[IPA_PARAM_PREFIX_COUNT]
61 : = {"SYNTH",
62 : "ISRA",
63 : "simd",
64 : "mask"};
65 :
66 : /* Names of parameters for dumping. Keep in sync with enum ipa_parm_op. */
67 :
68 : static const char *ipa_param_op_names[IPA_PARAM_PREFIX_COUNT]
69 : = {"IPA_PARAM_OP_UNDEFINED",
70 : "IPA_PARAM_OP_COPY",
71 : "IPA_PARAM_OP_NEW",
72 : "IPA_PARAM_OP_SPLIT"};
73 :
74 : /* Structure to hold declarations representing pass-through IPA-SRA splits. In
75 : essence, it tells new index for a combination of original index and
76 : offset. */
77 :
78 : struct pass_through_split_map
79 : {
80 : /* Original argument index. */
81 : unsigned base_index;
82 : /* Offset of the split part in the original argument. */
83 : unsigned unit_offset;
84 : /* Index of the split part in the call statement - where clone
85 : materialization put it. */
86 : int new_index;
87 : };
88 :
89 : /* Information about some call statements that needs to be conveyed from clone
90 : materialization to edge redirection. */
91 :
92 137080 : class ipa_edge_modification_info
93 : {
94 : public:
95 137080 : ipa_edge_modification_info ()
96 137080 : {}
97 :
98 : /* Mapping of original argument indices to where those arguments sit in the
99 : call statement now or to a negative index if they were removed. */
100 : auto_vec<int> index_map;
101 : /* Information about ISRA replacements put into the call statement at the
102 : clone materialization stages. */
103 : auto_vec<pass_through_split_map> pass_through_map;
104 : /* Necessary adjustment to ipa_param_adjustments::m_always_copy_start when
105 : redirecting the call. */
106 : int always_copy_delta = 0;
107 : };
108 :
109 : /* Class for storing and retrieving summaries about cal statement
110 : modifications. */
111 :
112 : class ipa_edge_modification_sum
113 : : public call_summary <ipa_edge_modification_info *>
114 : {
115 : public:
116 4305 : ipa_edge_modification_sum (symbol_table *table)
117 8610 : : call_summary<ipa_edge_modification_info *> (table)
118 : {
119 : }
120 :
121 : /* Hook that is called by summary when an edge is duplicated. */
122 :
123 66355 : void duplicate (cgraph_edge *,
124 : cgraph_edge *,
125 : ipa_edge_modification_info *old_info,
126 : ipa_edge_modification_info *new_info) final override
127 : {
128 66355 : new_info->index_map.safe_splice (old_info->index_map);
129 66355 : new_info->pass_through_map.safe_splice (old_info->pass_through_map);
130 66355 : new_info->always_copy_delta = old_info->always_copy_delta;
131 66355 : }
132 : };
133 :
134 : /* Call summary to store information about edges which have had their arguments
135 : partially modified already. */
136 :
137 : static ipa_edge_modification_sum *ipa_edge_modifications;
138 :
139 : /* Fail compilation if CS has any summary associated with it in
140 : ipa_edge_modifications. */
141 :
142 : DEBUG_FUNCTION void
143 314197 : ipa_verify_edge_has_no_modifications (cgraph_edge *cs)
144 : {
145 314197 : gcc_assert (!ipa_edge_modifications || !ipa_edge_modifications->get (cs));
146 314197 : }
147 :
148 : /* Fill an empty vector ARGS with PARM_DECLs representing formal parameters of
149 : FNDECL. The function should not be called during LTO WPA phase except for
150 : thunks (or functions with bodies streamed in). */
151 :
152 : void
153 198228 : push_function_arg_decls (vec<tree> *args, tree fndecl)
154 : {
155 198228 : int count;
156 198228 : tree parm;
157 :
158 : /* Safety check that we do not attempt to use the function in WPA, except
159 : when the function is a thunk and then we have DECL_ARGUMENTS or when we
160 : have already explicitly loaded its body. */
161 198228 : gcc_assert (!flag_wpa
162 : || DECL_ARGUMENTS (fndecl)
163 : || gimple_has_body_p (fndecl));
164 198228 : count = 0;
165 711465 : for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
166 513237 : count++;
167 :
168 198228 : args->reserve_exact (count);
169 711465 : for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
170 513237 : args->quick_push (parm);
171 198228 : }
172 :
173 : /* Fill an empty vector TYPES with trees representing formal parameters of
174 : function type FNTYPE. */
175 :
176 : void
177 483053 : push_function_arg_types (vec<tree> *types, tree fntype)
178 : {
179 483053 : int count = 0;
180 483053 : tree t;
181 :
182 2198776 : for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
183 1715723 : count++;
184 :
185 483053 : types->reserve_exact (count);
186 2198776 : for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
187 1715723 : types->quick_push (TREE_VALUE (t));
188 483053 : }
189 :
190 : /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
191 : friendly way, assuming they are meant to be applied to FNDECL. */
192 :
193 : void
194 32 : ipa_dump_adjusted_parameters (FILE *f,
195 : vec<ipa_adjusted_param, va_gc> *adj_params)
196 : {
197 32 : unsigned i, len = vec_safe_length (adj_params);
198 63 : bool first = true;
199 :
200 31 : if (!len)
201 : return;
202 :
203 31 : fprintf (f, " IPA adjusted parameters: ");
204 106 : for (i = 0; i < len; i++)
205 : {
206 44 : struct ipa_adjusted_param *apm;
207 44 : apm = &(*adj_params)[i];
208 :
209 44 : if (!first)
210 13 : fprintf (f, " ");
211 : else
212 : first = false;
213 :
214 44 : fprintf (f, "%i. %s %s", i, ipa_param_op_names[apm->op],
215 44 : apm->prev_clone_adjustment ? "prev_clone_adjustment " : "");
216 44 : switch (apm->op)
217 : {
218 : case IPA_PARAM_OP_UNDEFINED:
219 : break;
220 :
221 8 : case IPA_PARAM_OP_COPY:
222 8 : fprintf (f, ", base_index: %u", apm->base_index);
223 8 : fprintf (f, ", prev_clone_index: %u", apm->prev_clone_index);
224 8 : break;
225 :
226 36 : case IPA_PARAM_OP_SPLIT:
227 36 : fprintf (f, ", offset: %u", apm->unit_offset);
228 : /* fall-through */
229 36 : case IPA_PARAM_OP_NEW:
230 36 : fprintf (f, ", base_index: %u", apm->base_index);
231 36 : fprintf (f, ", prev_clone_index: %u", apm->prev_clone_index);
232 36 : print_node_brief (f, ", type: ", apm->type, 0);
233 36 : print_node_brief (f, ", alias type: ", apm->alias_ptr_type, 0);
234 36 : fprintf (f, " prefix: %s",
235 36 : ipa_param_prefixes[apm->param_prefix_index]);
236 36 : if (apm->reverse)
237 0 : fprintf (f, ", reverse");
238 : break;
239 : }
240 44 : fprintf (f, "\n");
241 : }
242 : }
243 :
244 : /* Fill NEW_TYPES with types of a function after its current OTYPES have been
245 : modified as described in ADJ_PARAMS. When USE_PREV_INDICES is true, use
246 : prev_clone_index from ADJ_PARAMS as opposed to base_index when the parameter
247 : is false. */
248 :
249 : static void
250 478246 : fill_vector_of_new_param_types (vec<tree> *new_types, vec<tree> *otypes,
251 : vec<ipa_adjusted_param, va_gc> *adj_params,
252 : bool use_prev_indices)
253 : {
254 478246 : unsigned adj_len = vec_safe_length (adj_params);
255 478246 : new_types->reserve_exact (adj_len);
256 1923098 : for (unsigned i = 0; i < adj_len ; i++)
257 : {
258 966606 : ipa_adjusted_param *apm = &(*adj_params)[i];
259 966606 : if (apm->op == IPA_PARAM_OP_COPY)
260 : {
261 1421206 : unsigned index
262 710603 : = use_prev_indices ? apm->prev_clone_index : apm->base_index;
263 : /* The following needs to be handled gracefully because of type
264 : mismatches. This happens with LTO but apparently also in Fortran
265 : with -fcoarray=lib -O2 -lcaf_single -latomic. */
266 710603 : if (index >= otypes->length ())
267 0 : continue;
268 710603 : new_types->quick_push ((*otypes)[index]);
269 : }
270 256003 : else if (apm->op == IPA_PARAM_OP_NEW
271 256003 : || apm->op == IPA_PARAM_OP_SPLIT)
272 : {
273 256003 : tree ntype = apm->type;
274 256003 : if (is_gimple_reg_type (ntype)
275 256003 : && TYPE_MODE (ntype) != BLKmode)
276 : {
277 253663 : unsigned malign = GET_MODE_ALIGNMENT (TYPE_MODE (ntype));
278 253663 : if (TYPE_ALIGN (ntype) != malign)
279 2221 : ntype = build_aligned_type (ntype, malign);
280 : }
281 256003 : new_types->quick_push (ntype);
282 : }
283 : else
284 0 : gcc_unreachable ();
285 : }
286 478246 : }
287 :
288 : /* Return false if given attribute should prevent type adjustments. */
289 :
290 : bool
291 404211 : ipa_param_adjustments::type_attribute_allowed_p (tree name)
292 : {
293 660838 : if ((is_attribute_p ("fn spec", name) && flag_ipa_modref)
294 175436 : || is_attribute_p ("access", name)
295 161595 : || is_attribute_p ("returns_nonnull", name)
296 160410 : || is_attribute_p ("assume_aligned", name)
297 160410 : || is_attribute_p ("nocf_check", name)
298 564572 : || is_attribute_p ("warn_unused_result", name))
299 : return true;
300 : return false;
301 : }
302 :
303 : /* Return true if attribute should be dropped if parameter changed. */
304 :
305 : static bool
306 8667 : drop_type_attribute_if_params_changed_p (tree name)
307 : {
308 8667 : if (is_attribute_p ("fn spec", name)
309 8667 : || is_attribute_p ("access", name))
310 : return true;
311 : return false;
312 : }
313 :
314 : /* Return TRUE if the attribute should be dropped in the decl it is sitting on
315 : changes. Primarily affects attributes working with the decls arguments. */
316 : static bool
317 33002 : drop_decl_attribute_if_params_changed_p (tree name)
318 : {
319 0 : if (is_attribute_p ("callback_only", name))
320 : return true;
321 : return false;
322 : }
323 :
324 : /* Build and return a function type just like ORIG_TYPE but with parameter
325 : types given in NEW_PARAM_TYPES - which can be NULL if, but only if,
326 : ORIG_TYPE itself has NULL TREE_ARG_TYPEs. If METHOD2FUNC is true, also make
327 : it a FUNCTION_TYPE instead of FUNCTION_TYPE.
328 : If ARG_MODIFIED is true drop attributes that are no longer up to date. */
329 :
330 : static tree
331 311981 : build_adjusted_function_type (tree orig_type, vec<tree> *new_param_types,
332 : bool method2func, bool skip_return,
333 : bool args_modified)
334 : {
335 311981 : tree new_arg_types = NULL;
336 311981 : if (TYPE_ARG_TYPES (orig_type))
337 : {
338 311806 : gcc_checking_assert (new_param_types);
339 311806 : bool last_parm_void = (TREE_VALUE (tree_last (TYPE_ARG_TYPES (orig_type)))
340 311806 : == void_type_node);
341 311806 : unsigned len = new_param_types->length ();
342 934275 : for (unsigned i = 0; i < len; i++)
343 622469 : new_arg_types = tree_cons (NULL_TREE, (*new_param_types)[i],
344 : new_arg_types);
345 :
346 311806 : tree new_reversed = nreverse (new_arg_types);
347 311806 : if (last_parm_void)
348 : {
349 311797 : if (new_reversed)
350 284325 : TREE_CHAIN (new_arg_types) = void_list_node;
351 : else
352 27472 : new_reversed = void_list_node;
353 : }
354 : new_arg_types = new_reversed;
355 : }
356 :
357 : /* Use build_distinct_type_copy to preserve as much as possible from original
358 : type (debug info, attribute lists etc.). The one exception is
359 : METHOD_TYPEs which must have THIS argument and when we are asked to remove
360 : it, we need to build new FUNCTION_TYPE instead. */
361 311981 : tree new_type = NULL;
362 311981 : if (method2func)
363 : {
364 134142 : tree ret_type;
365 134142 : if (skip_return)
366 12951 : ret_type = void_type_node;
367 : else
368 121191 : ret_type = TREE_TYPE (orig_type);
369 :
370 134142 : new_type
371 134142 : = build_distinct_type_copy (build_function_type (ret_type,
372 : new_arg_types));
373 134142 : TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
374 : }
375 : else
376 : {
377 177839 : new_type = build_distinct_type_copy (orig_type);
378 177839 : TYPE_ARG_TYPES (new_type) = new_arg_types;
379 177839 : if (skip_return)
380 68514 : TREE_TYPE (new_type) = void_type_node;
381 : }
382 311981 : if (args_modified && TYPE_ATTRIBUTES (new_type))
383 : {
384 8667 : tree t = TYPE_ATTRIBUTES (new_type);
385 8667 : tree *last = &TYPE_ATTRIBUTES (new_type);
386 8667 : TYPE_ATTRIBUTES (new_type) = NULL;
387 17334 : for (;t; t = TREE_CHAIN (t))
388 8667 : if (!drop_type_attribute_if_params_changed_p
389 8667 : (get_attribute_name (t)))
390 : {
391 8 : *last = copy_node (t);
392 8 : TREE_CHAIN (*last) = NULL;
393 8 : last = &TREE_CHAIN (*last);
394 : }
395 : }
396 :
397 311981 : return new_type;
398 : }
399 :
400 : /* Return the maximum index in any IPA_PARAM_OP_COPY adjustment or -1 if there
401 : is none. */
402 :
403 : int
404 45621 : ipa_param_adjustments::get_max_base_index ()
405 : {
406 45621 : unsigned adj_len = vec_safe_length (m_adj_params);
407 45621 : int max_index = -1;
408 108784 : for (unsigned i = 0; i < adj_len ; i++)
409 : {
410 63163 : ipa_adjusted_param *apm = &(*m_adj_params)[i];
411 63163 : if (apm->op == IPA_PARAM_OP_COPY
412 56998 : && max_index < apm->base_index)
413 63163 : max_index = apm->base_index;
414 : }
415 45621 : return max_index;
416 : }
417 :
418 :
419 : /* Fill SURVIVING_PARAMS with an array of bools where each one says whether a
420 : parameter that originally was at that position still survives in the given
421 : clone or is removed/replaced. If the final array is smaller than an index
422 : of an original parameter, that parameter also did not survive. That a
423 : parameter survives does not mean it has the same index as before. */
424 :
425 : void
426 27368 : ipa_param_adjustments::get_surviving_params (vec<bool> *surviving_params)
427 : {
428 27368 : unsigned adj_len = vec_safe_length (m_adj_params);
429 27368 : int max_index = get_max_base_index ();
430 :
431 27368 : if (max_index < 0)
432 : return;
433 17376 : surviving_params->reserve_exact (max_index + 1);
434 17376 : surviving_params->quick_grow_cleared (max_index + 1);
435 66140 : for (unsigned i = 0; i < adj_len ; i++)
436 : {
437 31388 : ipa_adjusted_param *apm = &(*m_adj_params)[i];
438 31388 : if (apm->op == IPA_PARAM_OP_COPY)
439 31388 : (*surviving_params)[apm->base_index] = true;
440 : }
441 : }
442 :
443 : /* Fill NEW_INDICES with new indices of each surviving parameter or -1 for
444 : those which do not survive. Any parameter outside of length of the vector
445 : does not survive. There is currently no support for a parameter to be
446 : copied to two distinct new parameters. */
447 :
448 : void
449 18253 : ipa_param_adjustments::get_updated_indices (vec<int> *new_indices)
450 : {
451 18253 : unsigned adj_len = vec_safe_length (m_adj_params);
452 18253 : int max_index = get_max_base_index ();
453 :
454 18253 : if (max_index < 0)
455 : return;
456 13101 : unsigned res_len = max_index + 1;
457 13101 : new_indices->reserve_exact (res_len);
458 59029 : for (unsigned i = 0; i < res_len ; i++)
459 32827 : new_indices->quick_push (-1);
460 42902 : for (unsigned i = 0; i < adj_len ; i++)
461 : {
462 29801 : ipa_adjusted_param *apm = &(*m_adj_params)[i];
463 29801 : if (apm->op == IPA_PARAM_OP_COPY)
464 25610 : (*new_indices)[apm->base_index] = i;
465 : }
466 : }
467 :
468 : /* Return the original index for the given new parameter index. Return a
469 : negative number if not available. */
470 :
471 : int
472 685802 : ipa_param_adjustments::get_original_index (int newidx)
473 : {
474 685802 : const ipa_adjusted_param *adj = &(*m_adj_params)[newidx];
475 685802 : if (adj->op != IPA_PARAM_OP_COPY)
476 : return -1;
477 540873 : return adj->base_index;
478 : }
479 :
480 : /* Return true if the first parameter (assuming there was one) survives the
481 : transformation intact and remains the first one. */
482 :
483 : bool
484 300342 : ipa_param_adjustments::first_param_intact_p ()
485 : {
486 300342 : return (!vec_safe_is_empty (m_adj_params)
487 282380 : && (*m_adj_params)[0].op == IPA_PARAM_OP_COPY
488 200596 : && (*m_adj_params)[0].base_index == 0);
489 : }
490 :
491 : /* Return true if we have to change what has formerly been a method into a
492 : function. */
493 :
494 : bool
495 474398 : ipa_param_adjustments::method2func_p (tree orig_type)
496 : {
497 474398 : return ((TREE_CODE (orig_type) == METHOD_TYPE) && !first_param_intact_p ());
498 : }
499 :
500 : /* Given function type OLD_TYPE, return a new type derived from it after
501 : performing all atored modifications. TYPE_ORIGINAL_P should be true when
502 : OLD_TYPE refers to the type before any IPA transformations, as opposed to a
503 : type that can be an intermediate one in between various IPA
504 : transformations. Set pointee of ARGS_MODIFIED (if provided) to TRUE if the
505 : type's arguments were changed. */
506 :
507 : tree
508 307445 : ipa_param_adjustments::build_new_function_type (
509 : tree old_type, bool type_original_p, bool *args_modified /* = NULL */)
510 : {
511 307445 : auto_vec<tree,16> new_param_types, *new_param_types_p;
512 307445 : if (prototype_p (old_type))
513 : {
514 307334 : auto_vec<tree, 16> otypes;
515 307334 : push_function_arg_types (&otypes, old_type);
516 307334 : fill_vector_of_new_param_types (&new_param_types, &otypes, m_adj_params,
517 : !type_original_p);
518 307334 : new_param_types_p = &new_param_types;
519 307334 : }
520 : else
521 : new_param_types_p = NULL;
522 :
523 : /* Check if any params type cares about are modified. In this case will
524 : need to drop some type attributes. */
525 27159 : bool modified = m_adj_params == NULL && TYPE_ARG_TYPES (old_type)
526 334566 : && m_always_copy_start > 0;
527 307445 : size_t index = 0;
528 307445 : if (m_adj_params)
529 280286 : for (tree t = TYPE_ARG_TYPES (old_type);
530 781077 : t && (int)index < m_always_copy_start && !modified;
531 500791 : t = TREE_CHAIN (t), index++)
532 500791 : if (index >= m_adj_params->length ()
533 500791 : || get_original_index (index) != (int)index)
534 : modified = true;
535 :
536 307445 : if (args_modified)
537 166910 : *args_modified = modified;
538 :
539 307445 : return build_adjusted_function_type (old_type, new_param_types_p,
540 : method2func_p (old_type), m_skip_return,
541 307445 : modified);
542 307445 : }
543 :
544 : /* Build variant of function decl ORIG_DECL which has no return value if
545 : M_SKIP_RETURN is true and, if ORIG_DECL's types or parameters is known, has
546 : this type adjusted as indicated in M_ADJ_PARAMS. Arguments from
547 : DECL_ARGUMENTS list are not processed now, since they are linked by
548 : TREE_CHAIN directly and not accessible in LTO during WPA. The caller is
549 : responsible for eliminating them when clones are properly materialized. */
550 :
551 : tree
552 166953 : ipa_param_adjustments::adjust_decl (tree orig_decl)
553 : {
554 166953 : tree new_decl = copy_node (orig_decl);
555 166953 : tree orig_type = TREE_TYPE (orig_decl);
556 166953 : bool args_modified = false;
557 166953 : if (prototype_p (orig_type)
558 166953 : || (m_skip_return && !VOID_TYPE_P (TREE_TYPE (orig_type))))
559 : {
560 166910 : tree new_type = build_new_function_type (orig_type, false, &args_modified);
561 166910 : TREE_TYPE (new_decl) = new_type;
562 : }
563 166953 : if (method2func_p (orig_type))
564 56289 : DECL_VINDEX (new_decl) = NULL_TREE;
565 :
566 : /* When signature changes, we need to clear builtin info. */
567 166953 : if (fndecl_built_in_p (new_decl))
568 97 : set_decl_built_in_function (new_decl, NOT_BUILT_IN, 0);
569 :
570 166953 : DECL_VIRTUAL_P (new_decl) = 0;
571 166953 : DECL_LANG_SPECIFIC (new_decl) = NULL;
572 :
573 : /* Drop MALLOC attribute for a void function. */
574 166953 : if (m_skip_return)
575 52767 : DECL_IS_MALLOC (new_decl) = 0;
576 :
577 : /* If the decl's arguments changed, we might need to drop some attributes. */
578 166953 : if (args_modified && DECL_ATTRIBUTES (new_decl))
579 : {
580 26147 : tree t = DECL_ATTRIBUTES (new_decl);
581 26147 : tree *last = &DECL_ATTRIBUTES (new_decl);
582 26147 : DECL_ATTRIBUTES (new_decl) = NULL;
583 59149 : for (; t; t = TREE_CHAIN (t))
584 33002 : if (!drop_decl_attribute_if_params_changed_p (get_attribute_name (t)))
585 : {
586 33001 : *last = copy_node (t);
587 33001 : TREE_CHAIN (*last) = NULL;
588 33001 : last = &TREE_CHAIN (*last);
589 : }
590 : }
591 166953 : return new_decl;
592 : }
593 :
594 : /* Wrapper around get_base_ref_and_offset for cases interesting for IPA-SRA
595 : transformations. Return true if EXPR has an interesting form and fill in
596 : *BASE_P and *UNIT_OFFSET_P with the appropriate info. */
597 :
598 : static bool
599 1218546 : isra_get_ref_base_and_offset (tree expr, tree *base_p, unsigned *unit_offset_p)
600 : {
601 1218546 : HOST_WIDE_INT offset, size;
602 1218546 : bool reverse;
603 1218546 : tree base
604 1218546 : = get_ref_base_and_extent_hwi (expr, &offset, &size, &reverse);
605 1218546 : if (!base || size < 0)
606 : return false;
607 :
608 1212483 : if ((offset % BITS_PER_UNIT) != 0)
609 : return false;
610 :
611 1211485 : if (TREE_CODE (base) == MEM_REF)
612 : {
613 179756 : poly_int64 plmoff = mem_ref_offset (base).force_shwi ();
614 179756 : HOST_WIDE_INT moff;
615 179756 : bool is_cst = plmoff.is_constant (&moff);
616 179756 : if (!is_cst)
617 : return false;
618 179756 : offset += moff * BITS_PER_UNIT;
619 179756 : base = TREE_OPERAND (base, 0);
620 : }
621 :
622 1211485 : if (offset < 0 || (offset / BITS_PER_UNIT) > UINT_MAX)
623 : return false;
624 :
625 1211288 : *base_p = base;
626 1211288 : *unit_offset_p = offset / BITS_PER_UNIT;
627 1211288 : return true;
628 : }
629 :
630 : /* Remove all statements that use NAME directly or indirectly. KILLED_SSAS
631 : contains the SSA_NAMEs that are already being or have been processed and new
632 : ones need to be added to it. The function only has to process situations
633 : handled by ssa_name_only_returned_p in ipa-sra.cc with the exception that it
634 : can assume it must never reach a use in a return statement. */
635 :
636 : static void
637 10559 : purge_all_uses (tree name, hash_set <tree> *killed_ssas)
638 : {
639 10559 : imm_use_iterator imm_iter;
640 10559 : gimple *stmt;
641 10559 : auto_vec <tree, 4> worklist;
642 10559 : auto_vec <gimple *, 4> kill_list;
643 :
644 10559 : worklist.safe_push (name);
645 33800 : while (!worklist.is_empty ())
646 : {
647 12682 : tree cur_name = worklist.pop ();
648 14965 : FOR_EACH_IMM_USE_STMT (stmt, imm_iter, cur_name)
649 : {
650 2283 : if (gimple_debug_bind_p (stmt))
651 : {
652 : /* When running within tree-inline, we will never end up here but
653 : adding the SSAs to killed_ssas will do the trick in this case
654 : and the respective debug statements will get reset. */
655 70 : gimple_debug_bind_reset_value (stmt);
656 70 : update_stmt (stmt);
657 70 : continue;
658 : }
659 :
660 2213 : tree lhs = NULL_TREE;
661 2213 : if (gassign *a_stmt = dyn_cast<gassign *> (stmt))
662 757 : lhs = gimple_assign_lhs (a_stmt);
663 1456 : else if (gimple_code (stmt) == GIMPLE_PHI)
664 1456 : lhs = gimple_phi_result (stmt);
665 2970 : gcc_assert (lhs
666 : && (TREE_CODE (lhs) == SSA_NAME)
667 : && !gimple_vdef (stmt));
668 2213 : if (!killed_ssas->add (lhs))
669 : {
670 2123 : worklist.safe_push (lhs);
671 2123 : kill_list.safe_push (stmt);
672 : }
673 12682 : }
674 : }
675 :
676 : /* Remove stmts in reverse and afterwards to properly handle debug stmt
677 : generation and to not interfere with immediate use iteration. */
678 23241 : while (!kill_list.is_empty ())
679 : {
680 2123 : stmt = kill_list.pop ();
681 2123 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
682 2123 : gsi_remove (&gsi, true);
683 : }
684 10559 : }
685 :
686 : /* Modify actual arguments of a function call in statement currently belonging
687 : to CS, and make it call CS->callee->decl. Return the new statement that
688 : replaced the old one. When invoked, cfun and current_function_decl have to
689 : be set to the caller. When called from within tree-inline, KILLED_SSAs has
690 : to contain the pointer to killed_new_ssa_names within the copy_body_data
691 : structure and SSAs discovered to be useless (if LHS is removed) will be
692 : added to it, otherwise it needs to be NULL. */
693 :
694 : gcall *
695 623842 : ipa_param_adjustments::modify_call (cgraph_edge *cs,
696 : bool update_references,
697 : hash_set <tree> *killed_ssas)
698 : {
699 623842 : gcall *stmt = cs->call_stmt;
700 623842 : tree callee_decl = cs->callee->decl;
701 :
702 623842 : ipa_edge_modification_info *mod_info
703 623842 : = ipa_edge_modifications ? ipa_edge_modifications->get (cs) : NULL;
704 431713 : if (mod_info && symtab->dump_file)
705 : {
706 0 : fprintf (symtab->dump_file, "Information about pre-exiting "
707 : "modifications.\n Index map:");
708 0 : unsigned idx_len = mod_info->index_map.length ();
709 0 : for (unsigned i = 0; i < idx_len; i++)
710 0 : fprintf (symtab->dump_file, " %i", mod_info->index_map[i]);
711 0 : fprintf (symtab->dump_file, "\n Pass-through split map: ");
712 0 : unsigned ptm_len = mod_info->pass_through_map.length ();
713 0 : for (unsigned i = 0; i < ptm_len; i++)
714 0 : fprintf (symtab->dump_file,
715 : " (base_index: %u, offset: %u, new_index: %i)",
716 0 : mod_info->pass_through_map[i].base_index,
717 0 : mod_info->pass_through_map[i].unit_offset,
718 0 : mod_info->pass_through_map[i].new_index);
719 0 : fprintf (symtab->dump_file, "\n Always-copy delta: %i\n",
720 : mod_info->always_copy_delta);
721 : }
722 :
723 623842 : unsigned len = vec_safe_length (m_adj_params);
724 623842 : auto_vec<tree, 16> vargs (len + 1);
725 : /* Argument remapping has to be used in this function, because it builds the
726 : new call statement from the parameter descriptors, rather than the actual
727 : arguments, as in ipa_param_adjustments::modify_call_stmt. */
728 623842 : callback_info *ci = callback_info_sum->get (cs);
729 :
730 : /* Callback edges should be here only if they are pointing towards .ASSUME
731 : bodies, which always have a complete argument mapping. */
732 623842 : if (ci)
733 76 : gcc_assert (cs->callee->assume_function);
734 :
735 623842 : unsigned old_nargs = gimple_call_num_args (stmt);
736 623842 : unsigned orig_nargs = mod_info ? mod_info->index_map.length () : old_nargs;
737 623842 : auto_vec<bool, 16> kept (old_nargs);
738 623842 : kept.quick_grow_cleared (old_nargs);
739 :
740 623842 : cgraph_node *current_node = cgraph_node::get (current_function_decl);
741 623842 : if (update_references)
742 0 : current_node->remove_stmt_references (stmt);
743 :
744 623842 : if (cs->callback && cs->callee->assume_function)
745 76 : vargs.safe_push (build_fold_addr_expr (callee_decl));
746 :
747 623842 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
748 623842 : gimple_stmt_iterator prev_gsi = gsi;
749 623842 : gsi_prev (&prev_gsi);
750 2012746 : for (unsigned i = 0; i < len; i++)
751 : {
752 1388904 : ipa_adjusted_param *apm = &(*m_adj_params)[i];
753 1388904 : if (apm->op == IPA_PARAM_OP_COPY)
754 : {
755 1044977 : int index = ci ? ci->arg_mapping[apm->base_index] : apm->base_index;
756 1044977 : if ((unsigned) index >= orig_nargs)
757 : /* Can happen if the original call has argument mismatch,
758 : ignore. */
759 0 : continue;
760 1044977 : if (mod_info)
761 : {
762 85448 : index = mod_info->index_map[index];
763 85448 : gcc_assert (index >= 0);
764 : }
765 :
766 1044977 : tree arg = gimple_call_arg (stmt, index);
767 :
768 1044977 : vargs.quick_push (arg);
769 1044977 : kept[index] = true;
770 1044977 : continue;
771 1044977 : }
772 :
773 : /* At the moment the only user of IPA_PARAM_OP_NEW modifies calls itself.
774 : If we ever want to support it during WPA IPA stage, we'll need a
775 : mechanism to call into the IPA passes that introduced them. Currently
776 : we simply mandate that IPA infrastructure understands all argument
777 : modifications. Remember, edge redirection/modification is done only
778 : once, not in steps for each pass modifying the callee like clone
779 : materialization. */
780 343927 : gcc_assert (apm->op == IPA_PARAM_OP_SPLIT);
781 :
782 343927 : int index = ci ? ci->arg_mapping[apm->base_index] : apm->base_index;
783 :
784 : /* We have to handle pass-through changes differently using the map
785 : clone materialziation might have left behind. */
786 343927 : tree repl = NULL_TREE;
787 343927 : unsigned ptm_len = mod_info ? mod_info->pass_through_map.length () : 0;
788 368493 : for (unsigned j = 0; j < ptm_len; j++)
789 47957 : if (mod_info->pass_through_map[j].base_index == (unsigned) index
790 47957 : && mod_info->pass_through_map[j].unit_offset == apm->unit_offset)
791 : {
792 23391 : int repl_idx = mod_info->pass_through_map[j].new_index;
793 23391 : gcc_assert (repl_idx >= 0);
794 23391 : repl = gimple_call_arg (stmt, repl_idx);
795 23391 : break;
796 : }
797 343927 : if (repl)
798 : {
799 23391 : if (!useless_type_conversion_p(apm->type, repl->typed.type))
800 : {
801 0 : repl = force_value_to_type (apm->type, repl);
802 0 : repl = force_gimple_operand_gsi (&gsi, repl,
803 : true, NULL, true, GSI_SAME_STMT);
804 : }
805 23391 : vargs.quick_push (repl);
806 23391 : continue;
807 : }
808 :
809 320536 : if ((unsigned) index >= orig_nargs)
810 : /* Can happen if the original call has argument mismatch, ignore. */
811 1 : continue;
812 320535 : if (mod_info)
813 : {
814 3340 : index = mod_info->index_map[index];
815 3340 : gcc_assert (index >= 0);
816 : }
817 320535 : tree base = gimple_call_arg (stmt, index);
818 :
819 : /* We create a new parameter out of the value of the old one, we can
820 : do the following kind of transformations:
821 :
822 : - A scalar passed by reference, potentially as a part of a larger
823 : aggregate, is converted to a scalar passed by value.
824 :
825 : - A part of an aggregate is passed instead of the whole aggregate. */
826 :
827 320535 : location_t loc = gimple_location (stmt);
828 320535 : tree off;
829 320535 : bool deref_base = false;
830 320535 : unsigned int deref_align = 0;
831 320535 : if (TREE_CODE (base) != ADDR_EXPR
832 320535 : && is_gimple_reg_type (TREE_TYPE (base)))
833 : {
834 : /* Detect type mismatches in calls in invalid programs and make a
835 : poor attempt to gracefully convert them so that we don't ICE. */
836 101720 : if (!POINTER_TYPE_P (TREE_TYPE (base)))
837 11 : base = force_value_to_type (ptr_type_node, base);
838 :
839 101720 : off = build_int_cst (apm->alias_ptr_type, apm->unit_offset);
840 : }
841 : else
842 : {
843 218815 : bool addrof;
844 218815 : if (TREE_CODE (base) == ADDR_EXPR)
845 : {
846 181059 : base = TREE_OPERAND (base, 0);
847 181059 : addrof = true;
848 : }
849 : else
850 : addrof = false;
851 :
852 218815 : tree prev_base = base;
853 218815 : poly_int64 base_offset;
854 218815 : base = get_addr_base_and_unit_offset (base, &base_offset);
855 :
856 : /* Aggregate arguments can have non-invariant addresses. */
857 218815 : if (!base)
858 : {
859 0 : base = build_fold_addr_expr (prev_base);
860 0 : off = build_int_cst (apm->alias_ptr_type, apm->unit_offset);
861 : }
862 218815 : else if (TREE_CODE (base) == MEM_REF)
863 : {
864 5848 : if (!addrof)
865 : {
866 5848 : deref_base = true;
867 5848 : deref_align = TYPE_ALIGN (TREE_TYPE (base));
868 : }
869 11696 : off = build_int_cst (apm->alias_ptr_type,
870 5848 : base_offset + apm->unit_offset);
871 5848 : off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
872 : off);
873 5848 : base = TREE_OPERAND (base, 0);
874 : }
875 : else
876 : {
877 425934 : off = build_int_cst (apm->alias_ptr_type,
878 212967 : base_offset + apm->unit_offset);
879 212967 : base = build_fold_addr_expr (base);
880 : }
881 : }
882 :
883 320535 : tree type = apm->type;
884 320535 : unsigned int align;
885 320535 : unsigned HOST_WIDE_INT misalign;
886 :
887 320535 : if (deref_base)
888 : {
889 5848 : align = deref_align;
890 5848 : misalign = 0;
891 : }
892 : else
893 : {
894 314687 : get_pointer_alignment_1 (base, &align, &misalign);
895 : /* All users must make sure that we can be optimistic when it
896 : comes to alignment in this case (by inspecting the final users
897 : of these new parameters). */
898 314687 : if (TYPE_ALIGN (type) > align)
899 100194 : align = TYPE_ALIGN (type);
900 : }
901 320535 : misalign
902 320535 : += (offset_int::from (wi::to_wide (off), SIGNED).to_short_addr ()
903 320535 : * BITS_PER_UNIT);
904 320535 : misalign = misalign & (align - 1);
905 320535 : if (misalign != 0)
906 3244 : align = least_bit_hwi (misalign);
907 320535 : if (align < TYPE_ALIGN (type))
908 0 : type = build_aligned_type (type, align);
909 320535 : base = force_gimple_operand_gsi (&gsi, base,
910 : true, NULL, true, GSI_SAME_STMT);
911 320535 : tree expr = fold_build2_loc (loc, MEM_REF, type, base, off);
912 320535 : REF_REVERSE_STORAGE_ORDER (expr) = apm->reverse;
913 : /* If expr is not a valid gimple call argument emit
914 : a load into a temporary. */
915 320535 : if (is_gimple_reg_type (TREE_TYPE (expr)))
916 : {
917 317196 : gassign *tem = gimple_build_assign (NULL_TREE, expr);
918 317196 : if (gimple_in_ssa_p (cfun))
919 : {
920 634392 : gimple_set_vuse (tem, gimple_vuse (stmt));
921 317196 : expr = make_ssa_name (TREE_TYPE (expr), tem);
922 : }
923 : else
924 0 : expr = create_tmp_reg (TREE_TYPE (expr));
925 317196 : gimple_assign_set_lhs (tem, expr);
926 317196 : gsi_insert_before (&gsi, tem, GSI_SAME_STMT);
927 : }
928 320535 : vargs.quick_push (expr);
929 : }
930 :
931 623842 : if (m_always_copy_start >= 0 && !ci)
932 : {
933 623766 : int always_copy_start = m_always_copy_start;
934 623766 : if (mod_info)
935 : {
936 70634 : always_copy_start += mod_info->always_copy_delta;
937 70634 : gcc_assert (always_copy_start >= 0);
938 : }
939 623771 : for (unsigned i = always_copy_start; i < old_nargs; i++)
940 5 : vargs.safe_push (gimple_call_arg (stmt, i));
941 : }
942 :
943 : /* For optimized away parameters, add on the caller side
944 : before the call
945 : DEBUG D#X => parm_Y(D)
946 : stmts and associate D#X with parm in decl_debug_args_lookup
947 : vector to say for debug info that if parameter parm had been passed,
948 : it would have value parm_Y(D). */
949 623842 : tree old_decl = gimple_call_fndecl (stmt);
950 623842 : if (MAY_HAVE_DEBUG_BIND_STMTS && old_decl && callee_decl)
951 : {
952 498682 : vec<tree, va_gc> **debug_args = NULL;
953 498682 : unsigned i = 0;
954 498682 : cgraph_node *callee_node = cgraph_node::get (callee_decl);
955 :
956 : /* FIXME: we don't seem to be able to insert debug args before clone
957 : is materialized. Materializing them early leads to extra memory
958 : use. */
959 498682 : if (callee_node->clone_of)
960 0 : callee_node->get_untransformed_body ();
961 498682 : for (tree old_parm = DECL_ARGUMENTS (old_decl);
962 1572131 : old_parm && i < old_nargs && ((int) i) < m_always_copy_start;
963 1073449 : old_parm = DECL_CHAIN (old_parm), i++)
964 : {
965 1957513 : if (!is_gimple_reg (old_parm) || kept[i])
966 823372 : continue;
967 258188 : tree arg;
968 258188 : if (mod_info)
969 : {
970 25226 : if (mod_info->index_map[i] < 0)
971 8110 : continue;
972 17116 : arg = gimple_call_arg (stmt, mod_info->index_map[i]);
973 : }
974 : else
975 232962 : arg = gimple_call_arg (stmt, i);
976 :
977 250078 : tree origin = DECL_ORIGIN (old_parm);
978 250078 : if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
979 : {
980 8 : if (!fold_convertible_p (TREE_TYPE (origin), arg))
981 1 : continue;
982 7 : tree rhs1;
983 7 : if (TREE_CODE (arg) == SSA_NAME
984 0 : && gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg))
985 0 : && (rhs1
986 0 : = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (arg)))
987 7 : && useless_type_conversion_p (TREE_TYPE (origin),
988 0 : TREE_TYPE (rhs1)))
989 : arg = rhs1;
990 : else
991 7 : arg = fold_convert_loc (gimple_location (stmt),
992 7 : TREE_TYPE (origin), arg);
993 : }
994 250077 : if (debug_args == NULL)
995 210703 : debug_args = decl_debug_args_insert (callee_decl);
996 250077 : unsigned int ix;
997 250077 : tree ddecl = NULL_TREE;
998 336029 : for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl); ix += 2)
999 319712 : if (ddecl == origin)
1000 : {
1001 240314 : ddecl = (**debug_args)[ix + 1];
1002 240314 : break;
1003 : }
1004 250077 : if (ddecl == NULL)
1005 : {
1006 9763 : ddecl = build_debug_expr_decl (TREE_TYPE (origin));
1007 : /* FIXME: Is setting the mode really necessary? */
1008 9763 : SET_DECL_MODE (ddecl, DECL_MODE (origin));
1009 :
1010 9763 : vec_safe_push (*debug_args, origin);
1011 9763 : vec_safe_push (*debug_args, ddecl);
1012 : }
1013 250077 : gimple *def_temp = gimple_build_debug_bind (ddecl,
1014 : unshare_expr (arg), stmt);
1015 250077 : gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
1016 : }
1017 : }
1018 :
1019 623842 : if (dump_file && (dump_flags & TDF_DETAILS))
1020 : {
1021 23 : fprintf (dump_file, "replacing stmt:");
1022 23 : print_gimple_stmt (dump_file, gsi_stmt (gsi), 0);
1023 : }
1024 :
1025 623842 : gcall *new_stmt;
1026 623842 : if (cs->callback && cs->callee->assume_function)
1027 76 : new_stmt = gimple_build_call_internal_vec (IFN_ASSUME, vargs);
1028 : else
1029 623766 : new_stmt = gimple_build_call_vec (callee_decl, vargs);
1030 :
1031 623842 : hash_set <tree> *ssas_to_remove = NULL;
1032 623842 : if (tree lhs = gimple_call_lhs (stmt))
1033 : {
1034 251861 : if (!m_skip_return)
1035 241302 : gimple_call_set_lhs (new_stmt, lhs);
1036 10559 : else if (TREE_CODE (lhs) == SSA_NAME)
1037 : {
1038 10559 : if (!killed_ssas)
1039 : {
1040 787 : ssas_to_remove = new hash_set<tree> (8);
1041 787 : killed_ssas = ssas_to_remove;
1042 : }
1043 10559 : killed_ssas->add (lhs);
1044 10559 : purge_all_uses (lhs, killed_ssas);
1045 : }
1046 : }
1047 :
1048 623842 : gimple_set_block (new_stmt, gimple_block (stmt));
1049 623842 : if (gimple_has_location (stmt))
1050 1108451 : gimple_set_location (new_stmt, gimple_location (stmt));
1051 623842 : gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
1052 623842 : gimple_call_copy_flags (new_stmt, stmt);
1053 623842 : if (gimple_in_ssa_p (cfun))
1054 623842 : gimple_move_vops (new_stmt, stmt);
1055 :
1056 623842 : if (dump_file && (dump_flags & TDF_DETAILS))
1057 : {
1058 23 : fprintf (dump_file, "with stmt:");
1059 23 : print_gimple_stmt (dump_file, new_stmt, 0);
1060 23 : fprintf (dump_file, "\n");
1061 : }
1062 623842 : gsi_replace (&gsi, new_stmt, true);
1063 623842 : if (ssas_to_remove)
1064 : {
1065 787 : ipa_release_ssas_in_hash (ssas_to_remove);
1066 787 : delete ssas_to_remove;
1067 : }
1068 623842 : if (update_references)
1069 0 : do
1070 : {
1071 0 : current_node->record_stmt_references (gsi_stmt (gsi));
1072 0 : gsi_prev (&gsi);
1073 : }
1074 0 : while (gsi_stmt (gsi) != gsi_stmt (prev_gsi));
1075 :
1076 623842 : if (mod_info)
1077 70653 : ipa_edge_modifications->remove (cs);
1078 623842 : return new_stmt;
1079 623842 : }
1080 :
1081 : /* Dump information contained in the object in textual form to F. */
1082 :
1083 : void
1084 32 : ipa_param_adjustments::dump (FILE *f)
1085 : {
1086 32 : fprintf (f, " m_always_copy_start: %i\n", m_always_copy_start);
1087 32 : ipa_dump_adjusted_parameters (f, m_adj_params);
1088 32 : if (m_skip_return)
1089 5 : fprintf (f, " Will SKIP return.\n");
1090 32 : }
1091 :
1092 : /* Dump information contained in the object in textual form to stderr. */
1093 :
1094 : void
1095 0 : ipa_param_adjustments::debug ()
1096 : {
1097 0 : dump (stderr);
1098 0 : }
1099 :
1100 : /* Register a REPLACEMENT for accesses to BASE at UNIT_OFFSET. */
1101 :
1102 : void
1103 97937 : ipa_param_body_adjustments::register_replacement (tree base,
1104 : unsigned unit_offset,
1105 : tree replacement)
1106 : {
1107 97937 : ipa_param_body_replacement psr;
1108 97937 : psr.base = base;
1109 97937 : psr.repl = replacement;
1110 97937 : psr.dummy = NULL_TREE;
1111 97937 : psr.unit_offset = unit_offset;
1112 97937 : m_replacements.safe_push (psr);
1113 97937 : m_sorted_replacements_p = false;
1114 97937 : }
1115 :
1116 : /* Register that REPLACEMENT should replace parameter described in APM. */
1117 :
1118 : void
1119 84929 : ipa_param_body_adjustments::register_replacement (ipa_adjusted_param *apm,
1120 : tree replacement)
1121 : {
1122 84929 : gcc_checking_assert (apm->op == IPA_PARAM_OP_SPLIT
1123 : || apm->op == IPA_PARAM_OP_NEW);
1124 84929 : gcc_checking_assert (!apm->prev_clone_adjustment);
1125 84929 : register_replacement (m_oparms[apm->prev_clone_index], apm->unit_offset,
1126 : replacement);
1127 84929 : }
1128 :
1129 : /* Comparator for sorting and searching
1130 : ipa_param_body_adjustments::m_replacements. */
1131 :
1132 : static int
1133 1942070 : compare_param_body_replacement (const void *va, const void *vb)
1134 : {
1135 1942070 : const ipa_param_body_replacement *a = (const ipa_param_body_replacement *) va;
1136 1942070 : const ipa_param_body_replacement *b = (const ipa_param_body_replacement *) vb;
1137 :
1138 1942070 : if (DECL_UID (a->base) < DECL_UID (b->base))
1139 : return -1;
1140 1601486 : if (DECL_UID (a->base) > DECL_UID (b->base))
1141 : return 1;
1142 1222708 : if (a->unit_offset < b->unit_offset)
1143 : return -1;
1144 669268 : if (a->unit_offset > b->unit_offset)
1145 575978 : return 1;
1146 : return 0;
1147 : }
1148 :
1149 : /* Sort m_replacements and set m_sorted_replacements_p to true. */
1150 :
1151 : void
1152 187157 : ipa_param_body_adjustments::sort_replacements ()
1153 : {
1154 187157 : if (m_sorted_replacements_p)
1155 : return;
1156 54746 : m_replacements.qsort (compare_param_body_replacement);
1157 54746 : m_sorted_replacements_p = true;
1158 : }
1159 :
1160 : /* Copy or not, as appropriate given m_id and decl context, a pre-existing
1161 : PARM_DECL T so that it can be included in the parameters of the modified
1162 : function. */
1163 :
1164 : tree
1165 267036 : ipa_param_body_adjustments::carry_over_param (tree t)
1166 : {
1167 267036 : tree new_parm;
1168 267036 : if (m_id)
1169 : {
1170 263115 : new_parm = remap_decl (t, m_id);
1171 263115 : if (TREE_CODE (new_parm) != PARM_DECL)
1172 0 : new_parm = m_id->copy_decl (t, m_id);
1173 : }
1174 3921 : else if (DECL_CONTEXT (t) != m_fndecl)
1175 : {
1176 15 : new_parm = copy_node (t);
1177 15 : DECL_CONTEXT (new_parm) = m_fndecl;
1178 : }
1179 : else
1180 : new_parm = t;
1181 267036 : return new_parm;
1182 : }
1183 :
1184 : /* If DECL is a gimple register that has a default definition SSA name and that
1185 : has some uses, return the default definition, otherwise return NULL_TREE. */
1186 :
1187 : tree
1188 158326 : ipa_param_body_adjustments::get_ddef_if_exists_and_is_used (tree decl)
1189 : {
1190 158326 : if (!is_gimple_reg (decl))
1191 : return NULL_TREE;
1192 125708 : tree ddef = ssa_default_def (m_id->src_cfun, decl);
1193 125708 : if (!ddef || has_zero_uses (ddef))
1194 42209 : return NULL_TREE;
1195 : return ddef;
1196 : }
1197 :
1198 : /* Populate m_dead_stmts given that DEAD_PARAM is going to be removed without
1199 : any replacement or splitting. REPL is the replacement VAR_SECL to base any
1200 : remaining uses of a removed parameter on. Push all removed SSA names that
1201 : are used within debug statements to DEBUGSTACK. */
1202 :
1203 : void
1204 116030 : ipa_param_body_adjustments::mark_dead_statements (tree dead_param,
1205 : vec<tree> *debugstack)
1206 : {
1207 : /* Current IPA analyses which remove unused parameters never remove a
1208 : non-gimple register ones which have any use except as parameters in other
1209 : calls, so we can safely leave them as they are. */
1210 116030 : tree parm_ddef = get_ddef_if_exists_and_is_used (dead_param);
1211 116030 : if (!parm_ddef)
1212 80236 : return;
1213 :
1214 41203 : auto_vec<tree, 4> stack;
1215 41203 : hash_set<tree> used_in_debug;
1216 41203 : m_dead_ssas.add (parm_ddef);
1217 41203 : stack.safe_push (parm_ddef);
1218 130244 : while (!stack.is_empty ())
1219 : {
1220 47838 : imm_use_iterator imm_iter;
1221 47838 : use_operand_p use_p;
1222 47838 : tree t = stack.pop ();
1223 :
1224 47838 : insert_decl_map (m_id, t, error_mark_node);
1225 171391 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, t)
1226 : {
1227 123553 : gimple *stmt = USE_STMT (use_p);
1228 :
1229 : /* Calls containing dead arguments cannot be deleted,
1230 : modify_call_stmt will instead remove just the argument later on.
1231 : If isra_track_scalar_value_uses in ipa-sra.cc is extended to look
1232 : through const functions, we will need to do so here too. */
1233 123553 : if (is_gimple_call (stmt)
1234 123553 : || (m_id->blocks_to_copy
1235 77505 : && !bitmap_bit_p (m_id->blocks_to_copy,
1236 77505 : gimple_bb (stmt)->index)))
1237 99949 : continue;
1238 :
1239 23604 : if (is_gimple_debug (stmt))
1240 : {
1241 16464 : m_dead_stmts.add (stmt);
1242 16464 : gcc_assert (gimple_debug_bind_p (stmt));
1243 16464 : if (!used_in_debug.contains (t))
1244 : {
1245 8867 : used_in_debug.add (t);
1246 8867 : debugstack->safe_push (t);
1247 : }
1248 : }
1249 7140 : else if (gimple_code (stmt) == GIMPLE_PHI)
1250 : {
1251 410 : gphi *phi = as_a <gphi *> (stmt);
1252 410 : int ix = PHI_ARG_INDEX_FROM_USE (use_p);
1253 :
1254 410 : if (!m_id->blocks_to_copy
1255 458 : || bitmap_bit_p (m_id->blocks_to_copy,
1256 48 : gimple_phi_arg_edge (phi, ix)->src->index))
1257 : {
1258 362 : m_dead_stmts.add (phi);
1259 362 : tree res = gimple_phi_result (phi);
1260 362 : if (!m_dead_ssas.add (res))
1261 246 : stack.safe_push (res);
1262 : }
1263 : }
1264 6730 : else if (gassign *a_stmt = dyn_cast<gassign *> (stmt))
1265 : {
1266 6647 : m_dead_stmts.add (a_stmt);
1267 6647 : if (!gimple_clobber_p (a_stmt))
1268 : {
1269 6631 : tree lhs = gimple_assign_lhs (a_stmt);
1270 6631 : gcc_assert (TREE_CODE (lhs) == SSA_NAME);
1271 6631 : if (!m_dead_ssas.add (lhs))
1272 6389 : stack.safe_push (lhs);
1273 : }
1274 : }
1275 83 : else if (gimple_code (stmt) == GIMPLE_RETURN)
1276 83 : gcc_assert (m_adjustments && m_adjustments->m_skip_return);
1277 : else
1278 : /* IPA-SRA does not analyze other types of statements. */
1279 0 : gcc_unreachable ();
1280 47838 : }
1281 : }
1282 :
1283 41203 : if (!MAY_HAVE_DEBUG_STMTS)
1284 : {
1285 5409 : gcc_assert (debugstack->is_empty ());
1286 5409 : return;
1287 : }
1288 :
1289 35794 : tree dp_ddecl = build_debug_expr_decl (TREE_TYPE (dead_param));
1290 : /* FIXME: Is setting the mode really necessary? */
1291 35794 : SET_DECL_MODE (dp_ddecl, DECL_MODE (dead_param));
1292 35794 : m_dead_ssa_debug_equiv.put (parm_ddef, dp_ddecl);
1293 41203 : }
1294 :
1295 : /* Put all clobbers of of dereference of default definition of PARAM into
1296 : m_dead_stmts. If there are returns among uses of the default definition of
1297 : PARAM, verify they will be stripped off the return value. */
1298 :
1299 : void
1300 49255 : ipa_param_body_adjustments::mark_clobbers_dead (tree param)
1301 : {
1302 49255 : if (!is_gimple_reg (param))
1303 6959 : return;
1304 42296 : tree ddef = get_ddef_if_exists_and_is_used (param);
1305 42296 : if (!ddef)
1306 : return;
1307 :
1308 42296 : imm_use_iterator imm_iter;
1309 42296 : use_operand_p use_p;
1310 187900 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, ddef)
1311 : {
1312 145604 : gimple *stmt = USE_STMT (use_p);
1313 145604 : if (gimple_clobber_p (stmt))
1314 376 : m_dead_stmts.add (stmt);
1315 145228 : else if (gimple_code (stmt) == GIMPLE_RETURN)
1316 176 : gcc_assert (m_adjustments && m_adjustments->m_skip_return);
1317 42296 : }
1318 : }
1319 :
1320 : /* Callback to walk_tree. If REMAP is an SSA_NAME that is present in hash_map
1321 : passed in DATA, replace it with unshared version of what it was mapped to.
1322 : If an SSA argument would be remapped to NULL, the whole operation needs to
1323 : abort which is signaled by returning error_mark_node. */
1324 :
1325 : static tree
1326 29340 : replace_with_mapped_expr (tree *remap, int *walk_subtrees, void *data)
1327 : {
1328 29340 : if (TYPE_P (*remap))
1329 : {
1330 0 : *walk_subtrees = 0;
1331 0 : return 0;
1332 : }
1333 29340 : if (TREE_CODE (*remap) != SSA_NAME)
1334 : return 0;
1335 :
1336 5007 : *walk_subtrees = 0;
1337 :
1338 5007 : hash_map<tree, tree> *equivs = (hash_map<tree, tree> *) data;
1339 5007 : if (tree *p = equivs->get (*remap))
1340 : {
1341 4954 : if (!*p)
1342 1 : return error_mark_node;
1343 4953 : *remap = unshare_expr (*p);
1344 : }
1345 : return 0;
1346 : }
1347 :
1348 : /* Replace all occurrences of SSAs in m_dead_ssa_debug_equiv in t with what they
1349 : are mapped to. */
1350 :
1351 : void
1352 19780 : ipa_param_body_adjustments::remap_with_debug_expressions (tree *t)
1353 : {
1354 : /* If *t is an SSA_NAME which should have its debug statements reset, it is
1355 : mapped to NULL in the hash_map.
1356 :
1357 : It is perhaps simpler to handle the SSA_NAME cases directly and only
1358 : invoke walk_tree on more complex expressions. When
1359 : remap_with_debug_expressions is called from tree-inline.cc, a to-be-reset
1360 : SSA_NAME can be an operand to such expressions and the entire debug
1361 : variable we are remapping should be reset. This is signaled by walk_tree
1362 : returning error_mark_node and done by setting *t to NULL. */
1363 19780 : if (TREE_CODE (*t) == SSA_NAME)
1364 : {
1365 14884 : if (tree *p = m_dead_ssa_debug_equiv.get (*t))
1366 14884 : *t = *p;
1367 : }
1368 4896 : else if (walk_tree (t, replace_with_mapped_expr,
1369 4896 : &m_dead_ssa_debug_equiv, NULL) == error_mark_node)
1370 1 : *t = NULL_TREE;
1371 19780 : }
1372 :
1373 : /* For an SSA_NAME DEAD_SSA which is about to be DCEd because it is based on a
1374 : useless parameter, prepare an expression that should represent it in
1375 : debug_binds in the cloned function and add a mapping from DEAD_SSA to
1376 : m_dead_ssa_debug_equiv. That mapping is to NULL when the associated
1377 : debug_statement has to be reset instead. In such case return false,
1378 : ottherwise return true. If DEAD_SSA comes from a basic block which is not
1379 : about to be copied, ignore it and return true. */
1380 :
1381 : bool
1382 12475 : ipa_param_body_adjustments::prepare_debug_expressions (tree dead_ssa)
1383 : {
1384 12475 : gcc_checking_assert (m_dead_ssas.contains (dead_ssa));
1385 12475 : if (tree *d = m_dead_ssa_debug_equiv.get (dead_ssa))
1386 8808 : return (*d != NULL_TREE);
1387 :
1388 3667 : gcc_assert (!SSA_NAME_IS_DEFAULT_DEF (dead_ssa));
1389 3667 : gimple *def = SSA_NAME_DEF_STMT (dead_ssa);
1390 3667 : if (m_id->blocks_to_copy
1391 3667 : && !bitmap_bit_p (m_id->blocks_to_copy, gimple_bb (def)->index))
1392 : return true;
1393 :
1394 3667 : if (gimple_code (def) == GIMPLE_PHI)
1395 : {
1396 : /* In theory, we could ignore all SSAs coming from BBs not in
1397 : m_id->blocks_to_copy but at the time of the writing this code that
1398 : should never really be the case because only fnsplit uses that bitmap,
1399 : so don't bother. */
1400 169 : tree value = degenerate_phi_result (as_a <gphi *> (def));
1401 169 : if (!value
1402 169 : || (m_dead_ssas.contains (value)
1403 0 : && !prepare_debug_expressions (value)))
1404 : {
1405 169 : m_dead_ssa_debug_equiv.put (dead_ssa, NULL_TREE);
1406 169 : return false;
1407 : }
1408 :
1409 0 : gcc_assert (TREE_CODE (value) == SSA_NAME);
1410 0 : tree d = *m_dead_ssa_debug_equiv.get (value);
1411 0 : m_dead_ssa_debug_equiv.put (dead_ssa, d);
1412 0 : return true;
1413 : }
1414 :
1415 3498 : bool lost = false;
1416 3498 : use_operand_p use_p;
1417 3498 : ssa_op_iter oi;
1418 7001 : FOR_EACH_PHI_OR_STMT_USE (use_p, def, oi, SSA_OP_USE)
1419 : {
1420 3685 : tree use = USE_FROM_PTR (use_p);
1421 3685 : if (m_dead_ssas.contains (use)
1422 3685 : && !prepare_debug_expressions (use))
1423 : {
1424 182 : lost = true;
1425 182 : break;
1426 : }
1427 : }
1428 :
1429 3498 : if (lost)
1430 : {
1431 182 : m_dead_ssa_debug_equiv.put (dead_ssa, NULL_TREE);
1432 182 : return false;
1433 : }
1434 :
1435 3316 : if (gassign *a_def = dyn_cast<gassign *> (def))
1436 : {
1437 3316 : gcc_assert (!gimple_clobber_p (a_def));
1438 3316 : if (gimple_assign_copy_p (a_def)
1439 3316 : && TREE_CODE (gimple_assign_rhs1 (a_def)) == SSA_NAME)
1440 : {
1441 0 : tree d = *m_dead_ssa_debug_equiv.get (gimple_assign_rhs1 (a_def));
1442 0 : gcc_assert (d);
1443 0 : m_dead_ssa_debug_equiv.put (dead_ssa, d);
1444 0 : return true;
1445 : }
1446 :
1447 3316 : tree val
1448 3316 : = unshare_expr_without_location (gimple_assign_rhs_to_tree (a_def));
1449 3316 : remap_with_debug_expressions (&val);
1450 :
1451 3316 : tree vexpr = build_debug_expr_decl (TREE_TYPE (val));
1452 3316 : m_dead_stmt_debug_equiv.put (def, val);
1453 3316 : m_dead_ssa_debug_equiv.put (dead_ssa, vexpr);
1454 3316 : return true;
1455 : }
1456 : else
1457 0 : gcc_unreachable ();
1458 : }
1459 :
1460 : /* Common initialization performed by all ipa_param_body_adjustments
1461 : constructors. OLD_FNDECL is the declaration we take original arguments
1462 : from, (it may be the same as M_FNDECL). VARS, if non-NULL, is a pointer to
1463 : a chained list of new local variables. TREE_MAP is the IPA-CP produced
1464 : mapping of trees to constants.
1465 :
1466 : The function is rather long but it really onlu initializes all data members
1467 : of the class. It creates new param DECLs, finds their new types, */
1468 :
1469 : void
1470 170912 : ipa_param_body_adjustments::common_initialization (tree old_fndecl,
1471 : tree *vars,
1472 : vec<ipa_replace_map *,
1473 : va_gc> *tree_map)
1474 : {
1475 170912 : push_function_arg_decls (&m_oparms, old_fndecl);
1476 170912 : auto_vec<tree,16> otypes;
1477 170912 : if (TYPE_ARG_TYPES (TREE_TYPE (old_fndecl)) != NULL_TREE)
1478 170772 : push_function_arg_types (&otypes, TREE_TYPE (old_fndecl));
1479 : else
1480 : {
1481 140 : auto_vec<tree,16> oparms;
1482 140 : push_function_arg_decls (&oparms, old_fndecl);
1483 140 : unsigned ocount = oparms.length ();
1484 140 : otypes.reserve_exact (ocount);
1485 447 : for (unsigned i = 0; i < ocount; i++)
1486 167 : otypes.quick_push (TREE_TYPE (oparms[i]));
1487 140 : }
1488 170912 : fill_vector_of_new_param_types (&m_new_types, &otypes, m_adj_params, true);
1489 :
1490 170912 : auto_vec<bool, 16> kept;
1491 341824 : kept.reserve_exact (m_oparms.length ());
1492 341824 : kept.quick_grow_cleared (m_oparms.length ());
1493 170912 : auto_vec<bool, 16> split;
1494 170912 : split.reserve_exact (m_oparms.length ());
1495 341824 : split.quick_grow_cleared (m_oparms.length ());
1496 :
1497 170912 : unsigned adj_len = vec_safe_length (m_adj_params);
1498 170912 : m_method2func = ((TREE_CODE (TREE_TYPE (m_fndecl)) == METHOD_TYPE)
1499 170912 : && (adj_len == 0
1500 41388 : || (*m_adj_params)[0].op != IPA_PARAM_OP_COPY
1501 41268 : || (*m_adj_params)[0].base_index != 0));
1502 :
1503 : /* The main job of the this function is to go over the vector of adjusted
1504 : parameters and create declarations or find corresponding old ones and push
1505 : them to m_new_decls. For IPA-SRA replacements it also creates
1506 : corresponding m_id->dst_node->clone.performed_splits entries. */
1507 :
1508 170912 : m_new_decls.reserve_exact (adj_len);
1509 698205 : for (unsigned i = 0; i < adj_len ; i++)
1510 : {
1511 356381 : ipa_adjusted_param *apm = &(*m_adj_params)[i];
1512 356381 : unsigned prev_index = apm->prev_clone_index;
1513 356381 : tree new_parm;
1514 356381 : if (apm->op == IPA_PARAM_OP_COPY
1515 89345 : || apm->prev_clone_adjustment)
1516 : {
1517 267036 : kept[prev_index] = true;
1518 267036 : new_parm = carry_over_param (m_oparms[prev_index]);
1519 267036 : m_new_decls.quick_push (new_parm);
1520 : }
1521 89345 : else if (apm->op == IPA_PARAM_OP_NEW
1522 89345 : || apm->op == IPA_PARAM_OP_SPLIT)
1523 : {
1524 89345 : tree new_type = m_new_types[i];
1525 89345 : gcc_checking_assert (new_type);
1526 89345 : new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
1527 : new_type);
1528 89345 : const char *prefix = ipa_param_prefixes[apm->param_prefix_index];
1529 89345 : DECL_NAME (new_parm) = create_tmp_var_name (prefix);
1530 89345 : DECL_ARTIFICIAL (new_parm) = 1;
1531 89345 : DECL_ARG_TYPE (new_parm) = new_type;
1532 89345 : DECL_CONTEXT (new_parm) = m_fndecl;
1533 89345 : TREE_USED (new_parm) = 1;
1534 89345 : DECL_IGNORED_P (new_parm) = 1;
1535 89345 : layout_decl (new_parm, 0);
1536 89345 : m_new_decls.quick_push (new_parm);
1537 :
1538 89345 : if (apm->op == IPA_PARAM_OP_SPLIT)
1539 : {
1540 80424 : split[prev_index] = true;
1541 80424 : register_replacement (apm, new_parm);
1542 : }
1543 : }
1544 : else
1545 0 : gcc_unreachable ();
1546 : }
1547 :
1548 170912 : auto_vec <int, 16> index_mapping;
1549 170912 : bool need_remap = false;
1550 170912 : if (m_id)
1551 : {
1552 166376 : clone_info *cinfo = clone_info::get (m_id->src_node);
1553 166376 : if (cinfo && cinfo->param_adjustments)
1554 : {
1555 2592 : cinfo->param_adjustments->get_updated_indices (&index_mapping);
1556 2592 : need_remap = true;
1557 : }
1558 :
1559 166376 : if (ipcp_transformation *ipcp_ts
1560 166376 : = ipcp_get_transformation_summary (m_id->src_node))
1561 : {
1562 42529 : for (const ipa_argagg_value &av : ipcp_ts->m_agg_values)
1563 : {
1564 16325 : int parm_num = av.index;
1565 :
1566 16325 : if (need_remap)
1567 : {
1568 : /* FIXME: We cannot handle the situation when IPA-CP
1569 : identified that a parameter is a pointer to a global
1570 : variable and at the same time the variable has some known
1571 : constant contents (PR 107640). The best place to make
1572 : sure we don't drop such constants on the floor probably is
1573 : not here, but we have to make sure that it does not
1574 : confuse the remapping. */
1575 7110 : if (parm_num >= (int) index_mapping.length ())
1576 35 : continue;
1577 7075 : parm_num = index_mapping[parm_num];
1578 7075 : if (parm_num < 0)
1579 38 : continue;
1580 : }
1581 :
1582 16252 : if (!kept[parm_num])
1583 : {
1584 : /* IPA-CP has detected an aggregate constant in a parameter
1585 : that will not be kept, which means that IPA-SRA would have
1586 : split it if there wasn't a constant. Because we are about
1587 : to remove the original, this is the last chance where we
1588 : can substitute the uses with a constant (for values passed
1589 : by reference) or do the split but initialize the
1590 : replacement with a constant (for split aggregates passed
1591 : by value). */
1592 :
1593 13011 : if (split[parm_num])
1594 : {
1595 : /* We must be careful not to add a duplicate
1596 : replacement. */
1597 11724 : sort_replacements ();
1598 11724 : ipa_param_body_replacement *pbr
1599 11724 : = lookup_replacement_1 (m_oparms[parm_num],
1600 11724 : av.unit_offset);
1601 11724 : if (pbr)
1602 : {
1603 : /* Otherwise IPA-SRA should have bailed out. */
1604 3 : gcc_assert (AGGREGATE_TYPE_P (TREE_TYPE (pbr->repl)));
1605 3 : continue;
1606 : }
1607 : }
1608 :
1609 13008 : tree repl;
1610 13008 : if (av.by_ref)
1611 11851 : repl = av.value;
1612 : else
1613 : {
1614 1157 : repl = create_tmp_var (TREE_TYPE (av.value),
1615 : "removed_ipa_cp");
1616 1157 : gimple *init_stmt = gimple_build_assign (repl, av.value);
1617 1157 : m_split_agg_csts_inits.safe_push (init_stmt);
1618 : }
1619 13008 : register_replacement (m_oparms[parm_num], av.unit_offset,
1620 : repl);
1621 13008 : split[parm_num] = true;
1622 : }
1623 : }
1624 : }
1625 : }
1626 170912 : sort_replacements ();
1627 :
1628 170912 : if (tree_map)
1629 : {
1630 : /* Do not treat parameters which were replaced with a constant as
1631 : completely vanished. */
1632 35657 : for (unsigned i = 0; i < tree_map->length (); i++)
1633 : {
1634 21978 : int parm_num = (*tree_map)[i]->parm_num;
1635 21978 : gcc_assert (parm_num >= 0);
1636 21978 : if (need_remap)
1637 0 : parm_num = index_mapping[parm_num];
1638 21978 : kept[parm_num] = true;
1639 : }
1640 : }
1641 :
1642 : /* As part of body modifications, we will also have to replace remaining uses
1643 : of remaining uses of removed PARM_DECLs (which do not however use the
1644 : initial value) with their VAR_DECL copies.
1645 :
1646 : We do this differently with and without m_id. With m_id, we rely on its
1647 : mapping and create a replacement straight away. Without it, we have our
1648 : own mechanism for which we have to populate m_removed_decls vector. Just
1649 : don't mix them, that is why you should not call
1650 : replace_removed_params_ssa_names or perform_cfun_body_modifications when
1651 : you construct with ID not equal to NULL. */
1652 :
1653 170912 : auto_vec<tree, 8> ssas_to_process_debug;
1654 170912 : unsigned op_len = m_oparms.length ();
1655 629737 : for (unsigned i = 0; i < op_len; i++)
1656 458825 : if (!kept[i])
1657 : {
1658 169811 : if (m_id)
1659 : {
1660 165285 : gcc_assert (!m_id->decl_map->get (m_oparms[i]));
1661 165285 : tree var = copy_decl_to_var (m_oparms[i], m_id);
1662 165285 : insert_decl_map (m_id, m_oparms[i], var);
1663 : /* Declare this new variable. */
1664 165285 : DECL_CHAIN (var) = *vars;
1665 165285 : *vars = var;
1666 :
1667 : /* If this is not a split but a real removal, init hash sets
1668 : that will guide what not to copy to the new body. */
1669 165285 : if (!split[i])
1670 116030 : mark_dead_statements (m_oparms[i], &ssas_to_process_debug);
1671 : else
1672 49255 : mark_clobbers_dead (m_oparms[i]);
1673 16512 : if (MAY_HAVE_DEBUG_STMTS
1674 165285 : && is_gimple_reg (m_oparms[i]))
1675 111229 : m_reset_debug_decls.safe_push (m_oparms[i]);
1676 : }
1677 : else
1678 : {
1679 4526 : m_removed_decls.safe_push (m_oparms[i]);
1680 9052 : m_removed_map.put (m_oparms[i], m_removed_decls.length () - 1);
1681 4262 : if (MAY_HAVE_DEBUG_STMTS
1682 264 : && !kept[i]
1683 4790 : && is_gimple_reg (m_oparms[i]))
1684 224 : m_reset_debug_decls.safe_push (m_oparms[i]);
1685 : }
1686 : }
1687 :
1688 350691 : while (!ssas_to_process_debug.is_empty ())
1689 8867 : prepare_debug_expressions (ssas_to_process_debug.pop ());
1690 170912 : }
1691 :
1692 : /* Constructor of ipa_param_body_adjustments from a simple list of
1693 : modifications to parameters listed in ADJ_PARAMS which will prepare ground
1694 : for modification of parameters of fndecl. Return value of the function will
1695 : not be removed and the object will assume it does not run as a part of
1696 : tree-function_versioning. */
1697 :
1698 4521 : ipa_param_body_adjustments
1699 : ::ipa_param_body_adjustments (vec<ipa_adjusted_param, va_gc> *adj_params,
1700 : tree fndecl)
1701 4521 : : m_adj_params (adj_params), m_adjustments (NULL), m_reset_debug_decls (),
1702 4521 : m_dead_stmts (), m_dead_ssas (), m_dead_ssa_debug_equiv (),
1703 4521 : m_dead_stmt_debug_equiv (), m_fndecl (fndecl), m_id (NULL), m_oparms (),
1704 4521 : m_new_decls (), m_new_types (), m_replacements (),
1705 4521 : m_split_agg_csts_inits (), m_removed_decls (), m_removed_map (),
1706 4521 : m_method2func (false), m_sorted_replacements_p (true)
1707 : {
1708 4521 : common_initialization (fndecl, NULL, NULL);
1709 4521 : }
1710 :
1711 : /* Constructor of ipa_param_body_adjustments from ipa_param_adjustments in
1712 : ADJUSTMENTS which will prepare ground for modification of parameters of
1713 : fndecl. The object will assume it does not run as a part of
1714 : tree-function_versioning. */
1715 :
1716 15 : ipa_param_body_adjustments
1717 : ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
1718 : tree fndecl)
1719 15 : : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
1720 15 : m_reset_debug_decls (), m_dead_stmts (), m_dead_ssas (),
1721 15 : m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (), m_fndecl (fndecl),
1722 15 : m_id (NULL), m_oparms (), m_new_decls (), m_new_types (), m_replacements (),
1723 15 : m_split_agg_csts_inits (), m_removed_decls (), m_removed_map (),
1724 15 : m_method2func (false), m_sorted_replacements_p (true)
1725 : {
1726 15 : common_initialization (fndecl, NULL, NULL);
1727 15 : }
1728 :
1729 : /* Constructor of ipa_param_body_adjustments which sets it up as a part of
1730 : running tree_function_versioning. Planned modifications to the function are
1731 : in ADJUSTMENTS. FNDECL designates the new function clone which is being
1732 : modified. OLD_FNDECL is the function of which FNDECL is a clone (and which
1733 : at the time of invocation still share DECL_ARGUMENTS). ID is the
1734 : copy_body_data structure driving the whole body copying process. VARS is a
1735 : pointer to the head of the list of new local variables, TREE_MAP is the map
1736 : that drives tree substitution in the cloning process. */
1737 :
1738 166376 : ipa_param_body_adjustments
1739 : ::ipa_param_body_adjustments (ipa_param_adjustments *adjustments,
1740 : tree fndecl, tree old_fndecl,
1741 : copy_body_data *id, tree *vars,
1742 : vec<ipa_replace_map *, va_gc> *tree_map)
1743 166376 : : m_adj_params (adjustments->m_adj_params), m_adjustments (adjustments),
1744 166376 : m_reset_debug_decls (), m_dead_stmts (), m_dead_ssas (),
1745 166376 : m_dead_ssa_debug_equiv (), m_dead_stmt_debug_equiv (), m_fndecl (fndecl),
1746 166376 : m_id (id), m_oparms (), m_new_decls (), m_new_types (), m_replacements (),
1747 166376 : m_split_agg_csts_inits (), m_removed_decls (), m_removed_map (),
1748 166376 : m_method2func (false), m_sorted_replacements_p (true)
1749 : {
1750 166376 : common_initialization (old_fndecl, vars, tree_map);
1751 166376 : }
1752 :
1753 : /* Chain new param decls up and return them. */
1754 :
1755 : tree
1756 170912 : ipa_param_body_adjustments::get_new_param_chain ()
1757 : {
1758 170912 : tree result;
1759 170912 : tree *link = &result;
1760 :
1761 170912 : unsigned len = vec_safe_length (m_adj_params);
1762 527293 : for (unsigned i = 0; i < len; i++)
1763 : {
1764 356381 : tree new_decl = m_new_decls[i];
1765 356381 : *link = new_decl;
1766 356381 : link = &DECL_CHAIN (new_decl);
1767 : }
1768 170912 : *link = NULL_TREE;
1769 170912 : return result;
1770 : }
1771 :
1772 : /* Modify the function parameters FNDECL and its type according to the plan in
1773 : ADJUSTMENTS. This function needs to be called when the decl has not already
1774 : been processed with ipa_param_adjustments::adjust_decl, otherwise just
1775 : setting DECL_ARGUMENTS to whatever get_new_param_chain will do is enough. */
1776 :
1777 : void
1778 4536 : ipa_param_body_adjustments::modify_formal_parameters ()
1779 : {
1780 4536 : tree orig_type = TREE_TYPE (m_fndecl);
1781 4536 : DECL_ARGUMENTS (m_fndecl) = get_new_param_chain ();
1782 :
1783 : /* When signature changes, we need to clear builtin info. */
1784 4536 : if (fndecl_built_in_p (m_fndecl))
1785 0 : set_decl_built_in_function (m_fndecl, NOT_BUILT_IN, 0);
1786 :
1787 4536 : bool modified = false;
1788 4536 : size_t index = 0;
1789 4536 : if (m_adj_params)
1790 4160 : for (tree t = TYPE_ARG_TYPES (orig_type);
1791 10645 : t && !modified;
1792 6485 : t = TREE_CHAIN (t), index++)
1793 6485 : if (index >= m_adj_params->length ()
1794 5912 : || (*m_adj_params)[index].op != IPA_PARAM_OP_COPY
1795 8850 : || (*m_adj_params)[index].base_index != index)
1796 : modified = true;
1797 :
1798 : /* At this point, removing return value is only implemented when going
1799 : through tree_function_versioning, not when modifying function body
1800 : directly. */
1801 4536 : gcc_assert (!m_adjustments || !m_adjustments->m_skip_return);
1802 4536 : tree new_type = build_adjusted_function_type (orig_type, &m_new_types,
1803 : m_method2func, false, modified);
1804 :
1805 4536 : TREE_TYPE (m_fndecl) = new_type;
1806 4536 : DECL_VIRTUAL_P (m_fndecl) = 0;
1807 4536 : DECL_LANG_SPECIFIC (m_fndecl) = NULL;
1808 4536 : if (m_method2func)
1809 120 : DECL_VINDEX (m_fndecl) = NULL_TREE;
1810 4536 : }
1811 :
1812 : /* Given BASE and UNIT_OFFSET, find the corresponding record among replacement
1813 : structures. */
1814 :
1815 : ipa_param_body_replacement *
1816 171439 : ipa_param_body_adjustments::lookup_replacement_1 (tree base,
1817 : unsigned unit_offset)
1818 : {
1819 171439 : gcc_assert (m_sorted_replacements_p);
1820 171439 : ipa_param_body_replacement key;
1821 171439 : key.base = base;
1822 171439 : key.unit_offset = unit_offset;
1823 171439 : ipa_param_body_replacement *res
1824 342878 : = std::lower_bound (m_replacements.begin (), m_replacements.end (), key,
1825 336687 : [] (const ipa_param_body_replacement &elt,
1826 : const ipa_param_body_replacement &val)
1827 : {
1828 336687 : return (compare_param_body_replacement (&elt, &val)
1829 : < 0);
1830 : });
1831 :
1832 171439 : if (res == m_replacements.end ()
1833 136403 : || res->base != base
1834 266293 : || res->unit_offset != unit_offset)
1835 78149 : return NULL;
1836 : return res;
1837 : }
1838 :
1839 : /* Find the first replacement for BASE among m_replacements and return pointer
1840 : to it, or NULL if there is none. */
1841 :
1842 : ipa_param_body_replacement *
1843 27327 : ipa_param_body_adjustments::lookup_first_base_replacement (tree base)
1844 : {
1845 27327 : gcc_assert (m_sorted_replacements_p);
1846 27327 : ipa_param_body_replacement key;
1847 27327 : key.base = base;
1848 27327 : ipa_param_body_replacement *res
1849 54654 : = std::lower_bound (m_replacements.begin (), m_replacements.end (), key,
1850 40385 : [] (const ipa_param_body_replacement &elt,
1851 : const ipa_param_body_replacement &val)
1852 : {
1853 40385 : if (DECL_UID (elt.base) < DECL_UID (val.base))
1854 14605 : return true;
1855 : return false;
1856 : });
1857 :
1858 27327 : if (res == m_replacements.end ()
1859 27327 : || res->base != base)
1860 22074 : return NULL;
1861 : return res;
1862 : }
1863 :
1864 : /* Given BASE and UNIT_OFFSET, find the corresponding replacement expression
1865 : and return it, assuming it is known it does not hold value by reference or
1866 : in reverse storage order. */
1867 :
1868 : tree
1869 4077 : ipa_param_body_adjustments::lookup_replacement (tree base, unsigned unit_offset)
1870 : {
1871 4077 : ipa_param_body_replacement *pbr = lookup_replacement_1 (base, unit_offset);
1872 4077 : if (!pbr)
1873 : return NULL;
1874 4077 : return pbr->repl;
1875 : }
1876 :
1877 : /* If T is an SSA_NAME, return NULL if it is not a default def or
1878 : return its base variable if it is. If IGNORE_DEFAULT_DEF is true,
1879 : the base variable is always returned, regardless if it is a default
1880 : def. Return T if it is not an SSA_NAME. */
1881 :
1882 : static tree
1883 1018432 : get_ssa_base_param (tree t, bool ignore_default_def)
1884 : {
1885 1018432 : if (TREE_CODE (t) == SSA_NAME)
1886 : {
1887 614178 : if (ignore_default_def || SSA_NAME_IS_DEFAULT_DEF (t))
1888 134262 : return SSA_NAME_VAR (t);
1889 : else
1890 : return NULL_TREE;
1891 : }
1892 : return t;
1893 : }
1894 :
1895 : /* Given an expression, return the structure describing how it should be
1896 : replaced if it accesses a part of a split parameter or NULL otherwise.
1897 :
1898 : Do not free the result, it will be deallocated when the object is destroyed.
1899 :
1900 : If IGNORE_DEFAULT_DEF is cleared, consider only SSA_NAMEs of PARM_DECLs
1901 : which are default definitions, if set, consider all SSA_NAMEs of
1902 : PARM_DECLs. */
1903 :
1904 : ipa_param_body_replacement *
1905 1025689 : ipa_param_body_adjustments::get_expr_replacement (tree expr,
1906 : bool ignore_default_def)
1907 : {
1908 1025689 : tree base;
1909 1025689 : unsigned unit_offset;
1910 :
1911 1025689 : if (!isra_get_ref_base_and_offset (expr, &base, &unit_offset))
1912 : return NULL;
1913 :
1914 1018432 : base = get_ssa_base_param (base, ignore_default_def);
1915 1018432 : if (!base || TREE_CODE (base) != PARM_DECL)
1916 : return NULL;
1917 154660 : return lookup_replacement_1 (base, unit_offset);
1918 : }
1919 :
1920 : /* Given OLD_DECL, which is a PARM_DECL of a parameter that is being removed
1921 : (which includes it being split or replaced), return a new variable that
1922 : should be used for any SSA names that will remain in the function that
1923 : previously belonged to OLD_DECL. */
1924 :
1925 : tree
1926 7963 : ipa_param_body_adjustments::get_replacement_ssa_base (tree old_decl)
1927 : {
1928 7963 : unsigned *idx = m_removed_map.get (old_decl);
1929 7963 : if (!idx)
1930 : return NULL;
1931 :
1932 4509 : tree repl;
1933 4509 : if (TREE_CODE (m_removed_decls[*idx]) == PARM_DECL)
1934 : {
1935 4077 : gcc_assert (m_removed_decls[*idx] == old_decl);
1936 4077 : repl = copy_var_decl (old_decl, DECL_NAME (old_decl),
1937 4077 : TREE_TYPE (old_decl));
1938 4077 : m_removed_decls[*idx] = repl;
1939 : }
1940 : else
1941 : repl = m_removed_decls[*idx];
1942 : return repl;
1943 : }
1944 :
1945 : /* If OLD_NAME, which is being defined by statement STMT, is an SSA_NAME of a
1946 : parameter which is to be removed because its value is not used, create a new
1947 : SSA_NAME relating to a replacement VAR_DECL, replace all uses of the
1948 : original with it and return it. If there is no need to re-map, return NULL.
1949 : ADJUSTMENTS is a pointer to a vector of IPA-SRA adjustments. */
1950 :
1951 : tree
1952 0 : ipa_param_body_adjustments::replace_removed_params_ssa_names (tree old_name,
1953 : gimple *stmt)
1954 : {
1955 0 : gcc_assert (!m_id);
1956 0 : if (TREE_CODE (old_name) != SSA_NAME)
1957 : return NULL;
1958 :
1959 0 : tree decl = SSA_NAME_VAR (old_name);
1960 0 : if (decl == NULL_TREE
1961 0 : || TREE_CODE (decl) != PARM_DECL)
1962 : return NULL;
1963 :
1964 0 : tree repl = get_replacement_ssa_base (decl);
1965 0 : if (!repl)
1966 : return NULL;
1967 :
1968 0 : tree new_name = make_ssa_name (repl, stmt);
1969 0 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name)
1970 0 : = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (old_name);
1971 :
1972 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1973 : {
1974 0 : fprintf (dump_file, "replacing an SSA name of a removed param ");
1975 0 : print_generic_expr (dump_file, old_name);
1976 0 : fprintf (dump_file, " with ");
1977 0 : print_generic_expr (dump_file, new_name);
1978 0 : fprintf (dump_file, "\n");
1979 : }
1980 :
1981 0 : replace_uses_by (old_name, new_name);
1982 0 : return new_name;
1983 : }
1984 :
1985 : /* If the expression *EXPR_P should be replaced, do so. CONVERT specifies
1986 : whether the function should care about type incompatibility of the current
1987 : and new expressions. If it is false, the function will leave
1988 : incompatibility issues to the caller - note that when the function
1989 : encounters a BIT_FIELD_REF, IMAGPART_EXPR or REALPART_EXPR, it will modify
1990 : their bases instead of the expressions themselves and then also performs any
1991 : necessary conversions. */
1992 :
1993 : bool
1994 2708574 : ipa_param_body_adjustments::modify_expression (tree *expr_p, bool convert,
1995 : gimple_seq *extra_stmts)
1996 : {
1997 2708574 : tree expr = *expr_p;
1998 :
1999 2708574 : if (m_replacements.is_empty ())
2000 : return false;
2001 1023605 : if (TREE_CODE (expr) == BIT_FIELD_REF
2002 1023605 : || TREE_CODE (expr) == IMAGPART_EXPR
2003 1021655 : || TREE_CODE (expr) == REALPART_EXPR)
2004 : {
2005 : /* For a BIT_FIELD_REF do not bother to VIEW_CONVERT the base,
2006 : instead reference the replacement directly. */
2007 2460 : convert = TREE_CODE (expr) != BIT_FIELD_REF;
2008 2460 : expr_p = &TREE_OPERAND (expr, 0);
2009 2460 : expr = *expr_p;
2010 : }
2011 :
2012 1023605 : ipa_param_body_replacement *pbr = get_expr_replacement (expr, false);
2013 1023605 : if (!pbr)
2014 : return false;
2015 :
2016 86946 : tree repl = pbr->repl;
2017 86946 : if (dump_file && (dump_flags & TDF_DETAILS))
2018 : {
2019 11 : fprintf (dump_file, "About to replace expr ");
2020 11 : print_generic_expr (dump_file, expr);
2021 11 : fprintf (dump_file, " with ");
2022 11 : print_generic_expr (dump_file, repl);
2023 11 : fprintf (dump_file, "\n");
2024 : }
2025 :
2026 87198 : if (convert && !useless_type_conversion_p (TREE_TYPE (expr),
2027 252 : TREE_TYPE (repl)))
2028 : {
2029 8 : gcc_checking_assert (tree_to_shwi (TYPE_SIZE (TREE_TYPE (expr)))
2030 : == tree_to_shwi (TYPE_SIZE (TREE_TYPE (repl))));
2031 8 : tree vce = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expr), repl);
2032 8 : if (is_gimple_reg (repl)
2033 8 : && is_gimple_reg_type (TREE_TYPE (expr)))
2034 : {
2035 0 : gcc_assert (extra_stmts);
2036 0 : vce = force_gimple_operand (vce, extra_stmts, true, NULL_TREE);
2037 : }
2038 8 : *expr_p = vce;
2039 : }
2040 : else
2041 86938 : *expr_p = repl;
2042 : return true;
2043 : }
2044 :
2045 : /* If the assignment statement STMT contains any expressions that need to
2046 : replaced with a different one as noted by ADJUSTMENTS, do so. Handle any
2047 : potential type incompatibilities. If any conversion sttements have to be
2048 : pre-pended to STMT, they will be added to EXTRA_STMTS. Return true iff the
2049 : statement was modified. */
2050 :
2051 : bool
2052 2250149 : ipa_param_body_adjustments::modify_assignment (gimple *stmt,
2053 : gimple_seq *extra_stmts)
2054 : {
2055 2250149 : tree *lhs_p, *rhs_p;
2056 2250149 : bool any;
2057 :
2058 2818083 : if (m_replacements.is_empty () || !gimple_assign_single_p (stmt))
2059 : return false;
2060 :
2061 384210 : rhs_p = gimple_assign_rhs1_ptr (stmt);
2062 384210 : lhs_p = gimple_assign_lhs_ptr (stmt);
2063 :
2064 384210 : any = modify_expression (lhs_p, false);
2065 384210 : any |= modify_expression (rhs_p, false, extra_stmts);
2066 384210 : if (any
2067 384210 : && !useless_type_conversion_p (TREE_TYPE (*lhs_p), TREE_TYPE (*rhs_p)))
2068 : {
2069 60 : if (TREE_CODE (*rhs_p) == CONSTRUCTOR)
2070 : {
2071 : /* V_C_Es of constructors can cause trouble (PR 42714). */
2072 0 : if (is_gimple_reg_type (TREE_TYPE (*lhs_p)))
2073 0 : *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
2074 : else
2075 0 : *rhs_p = build_constructor (TREE_TYPE (*lhs_p),
2076 : NULL);
2077 : }
2078 : else
2079 : {
2080 60 : gcc_checking_assert (tree_to_shwi (TYPE_SIZE (TREE_TYPE (*lhs_p)))
2081 : == tree_to_shwi (TYPE_SIZE (TREE_TYPE (*rhs_p))));
2082 60 : tree new_rhs = fold_build1_loc (gimple_location (stmt),
2083 60 : VIEW_CONVERT_EXPR, TREE_TYPE (*lhs_p),
2084 : *rhs_p);
2085 60 : tree tmp = force_gimple_operand (new_rhs, extra_stmts, true,
2086 : NULL_TREE);
2087 60 : gimple_assign_set_rhs1 (stmt, tmp);
2088 : }
2089 : return true;
2090 : }
2091 :
2092 : return any;
2093 : }
2094 :
2095 : /* Record information about what modifications to call arguments have already
2096 : been done by clone materialization into a summary describing CS. The
2097 : information is stored in NEW_INDEX_MAP, NEW_PT_MAP and NEW_ALWAYS_COPY_DELTA
2098 : and correspond to equivalent fields in ipa_edge_modification_info. Return
2099 : the edge summary. */
2100 :
2101 : static ipa_edge_modification_info *
2102 17588 : record_argument_state_1 (cgraph_edge *cs, const vec<int> &new_index_map,
2103 : const vec<pass_through_split_map> &new_pt_map,
2104 : int new_always_copy_delta)
2105 :
2106 : {
2107 17588 : ipa_edge_modification_info *sum = ipa_edge_modifications->get_create (cs);
2108 :
2109 17588 : unsigned len = sum->pass_through_map.length ();
2110 17588 : for (unsigned i = 0; i < len; i++)
2111 : {
2112 0 : unsigned oldnew = sum->pass_through_map[i].new_index;
2113 0 : sum->pass_through_map[i].new_index = new_index_map[oldnew];
2114 : }
2115 :
2116 17588 : len = sum->index_map.length ();
2117 0 : if (len > 0)
2118 : {
2119 0 : unsigned nptlen = new_pt_map.length ();
2120 0 : for (unsigned j = 0; j < nptlen; j++)
2121 : {
2122 0 : int inverse = -1;
2123 0 : for (unsigned i = 0; i < len ; i++)
2124 0 : if ((unsigned) sum->index_map[i] == new_pt_map[j].base_index)
2125 : {
2126 0 : inverse = i;
2127 0 : break;
2128 : }
2129 0 : gcc_assert (inverse >= 0);
2130 0 : pass_through_split_map ptm_item;
2131 :
2132 0 : ptm_item.base_index = inverse;
2133 0 : ptm_item.unit_offset = new_pt_map[j].unit_offset;
2134 0 : ptm_item.new_index = new_pt_map[j].new_index;
2135 0 : sum->pass_through_map.safe_push (ptm_item);
2136 : }
2137 :
2138 0 : for (unsigned i = 0; i < len; i++)
2139 : {
2140 0 : int idx = sum->index_map[i];
2141 0 : if (idx < 0)
2142 0 : continue;
2143 0 : sum->index_map[i] = new_index_map[idx];
2144 : }
2145 : }
2146 : else
2147 : {
2148 17588 : sum->pass_through_map.safe_splice (new_pt_map);
2149 17588 : sum->index_map.safe_splice (new_index_map);
2150 : }
2151 17588 : sum->always_copy_delta += new_always_copy_delta;
2152 17588 : return sum;
2153 : }
2154 :
2155 : /* Record information about what modifications to call arguments have already
2156 : been done by clone materialization into a summary of an edge describing the
2157 : call in this clone and all its clones. NEW_INDEX_MAP, NEW_PT_MAP and
2158 : NEW_ALWAYS_COPY_DELTA have the same meaning as record_argument_state_1.
2159 :
2160 : In order to associate the info with the right edge summaries, we need
2161 : address of the ORIG_STMT in the function from which we are cloning (because
2162 : the edges have not yet been re-assigned to the new statement that has just
2163 : been created) and ID, the structure governing function body copying. */
2164 :
2165 : static void
2166 17588 : record_argument_state (copy_body_data *id, gimple *orig_stmt,
2167 : const vec<int> &new_index_map,
2168 : const vec<pass_through_split_map> &new_pt_map,
2169 : int new_always_copy_delta)
2170 : {
2171 17588 : if (!ipa_edge_modifications)
2172 4305 : ipa_edge_modifications = new ipa_edge_modification_sum (symtab);
2173 :
2174 17588 : struct cgraph_node *this_node = id->dst_node;
2175 17588 : ipa_edge_modification_info *first_sum = NULL;
2176 17588 : cgraph_edge *cs = this_node->get_edge (orig_stmt);
2177 17588 : if (cs)
2178 17588 : first_sum = record_argument_state_1 (cs, new_index_map, new_pt_map,
2179 : new_always_copy_delta);
2180 : else
2181 0 : gcc_assert (this_node->clones);
2182 :
2183 17588 : if (!this_node->clones)
2184 : return;
2185 60449 : for (cgraph_node *subclone = this_node->clones; subclone != this_node;)
2186 : {
2187 53137 : cs = subclone->get_edge (orig_stmt);
2188 53137 : if (cs)
2189 : {
2190 53137 : if (!first_sum)
2191 0 : first_sum = record_argument_state_1 (cs, new_index_map, new_pt_map,
2192 : new_always_copy_delta);
2193 : else
2194 : {
2195 53137 : ipa_edge_modification_info *s2
2196 53137 : = ipa_edge_modifications->get_create (cs);
2197 53137 : s2->index_map.truncate (0);
2198 53137 : s2->index_map.safe_splice (first_sum->index_map);
2199 53137 : s2->pass_through_map.truncate (0);
2200 53137 : s2->pass_through_map.safe_splice (first_sum->pass_through_map);
2201 53137 : s2->always_copy_delta = first_sum->always_copy_delta;
2202 : }
2203 : }
2204 : else
2205 0 : gcc_assert (subclone->clones);
2206 :
2207 53137 : if (subclone->clones)
2208 : subclone = subclone->clones;
2209 49221 : else if (subclone->next_sibling_clone)
2210 : subclone = subclone->next_sibling_clone;
2211 : else
2212 : {
2213 20691 : while (subclone != this_node && !subclone->next_sibling_clone)
2214 11228 : subclone = subclone->clone_of;
2215 9463 : if (subclone != this_node)
2216 2151 : subclone = subclone->next_sibling_clone;
2217 : }
2218 : }
2219 : }
2220 :
2221 : /* If the call statement pointed at by STMT_P contains any expressions that
2222 : need to replaced with a different one as noted by ADJUSTMENTS, do so. f the
2223 : statement needs to be rebuilt, do so. Return true if any modifications have
2224 : been performed. ORIG_STMT, if not NULL, is the original statement in the
2225 : function that is being cloned from, which at this point can be used to look
2226 : up call_graph edges.
2227 :
2228 : If the method is invoked as a part of IPA clone materialization and if any
2229 : parameter split is pass-through, i.e. it applies to the function that is
2230 : being modified and also to the callee of the statement, replace the
2231 : parameter passed to old callee with all of the replacement a callee might
2232 : possibly want and record the performed argument modifications in
2233 : ipa_edge_modifications. Likewise if any argument has already been left out
2234 : because it is not necessary. */
2235 :
2236 : bool
2237 705428 : ipa_param_body_adjustments::modify_call_stmt (gcall **stmt_p,
2238 : gimple *orig_stmt)
2239 : {
2240 705428 : auto_vec <unsigned, 4> pass_through_args;
2241 705428 : auto_vec <unsigned, 4> pass_through_pbr_indices;
2242 705428 : auto_vec <HOST_WIDE_INT, 4> pass_through_offsets;
2243 705428 : gcall *stmt = *stmt_p;
2244 705428 : unsigned nargs = gimple_call_num_args (stmt);
2245 705428 : bool recreate = false;
2246 705428 : gcc_assert (m_sorted_replacements_p);
2247 :
2248 2321155 : for (unsigned i = 0; i < gimple_call_num_args (stmt); i++)
2249 : {
2250 1615727 : tree t = gimple_call_arg (stmt, i);
2251 1615727 : gcc_assert (TREE_CODE (t) != BIT_FIELD_REF
2252 : && TREE_CODE (t) != IMAGPART_EXPR
2253 : && TREE_CODE (t) != REALPART_EXPR);
2254 :
2255 1615727 : if (TREE_CODE (t) == SSA_NAME
2256 1615727 : && m_dead_ssas.contains (t))
2257 : recreate = true;
2258 :
2259 1615727 : if (m_replacements.is_empty ())
2260 1610474 : continue;
2261 :
2262 192857 : tree base;
2263 192857 : unsigned agg_arg_offset;
2264 192857 : if (!isra_get_ref_base_and_offset (t, &base, &agg_arg_offset))
2265 1 : continue;
2266 :
2267 192856 : bool by_ref = false;
2268 192856 : if (TREE_CODE (base) == SSA_NAME)
2269 : {
2270 97198 : if (!SSA_NAME_IS_DEFAULT_DEF (base))
2271 72031 : continue;
2272 25167 : base = SSA_NAME_VAR (base);
2273 25167 : gcc_checking_assert (base);
2274 : by_ref = true;
2275 : }
2276 120825 : if (TREE_CODE (base) != PARM_DECL)
2277 93498 : continue;
2278 :
2279 27327 : ipa_param_body_replacement *first_rep
2280 27327 : = lookup_first_base_replacement (base);
2281 27327 : if (!first_rep)
2282 22074 : continue;
2283 5253 : unsigned first_rep_index = first_rep - m_replacements.begin ();
2284 :
2285 : /* We still have to distinguish between an end-use that we have to
2286 : transform now and a pass-through, which happens in the following
2287 : two cases. */
2288 :
2289 : /* TODO: After we adjust ptr_parm_has_nonarg_uses to also consider
2290 : &MEM_REF[ssa_name + offset], we will also have to detect that case
2291 : here. */
2292 :
2293 5253 : if (TREE_CODE (t) == SSA_NAME
2294 4263 : && SSA_NAME_IS_DEFAULT_DEF (t)
2295 4263 : && SSA_NAME_VAR (t)
2296 9516 : && TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL)
2297 : {
2298 : /* This must be a by_reference pass-through. */
2299 4263 : recreate = true;
2300 4263 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
2301 4263 : pass_through_args.safe_push (i);
2302 4263 : pass_through_pbr_indices.safe_push (first_rep_index);
2303 4263 : pass_through_offsets.safe_push (agg_arg_offset);
2304 : }
2305 990 : else if (!by_ref && AGGREGATE_TYPE_P (TREE_TYPE (t)))
2306 : {
2307 : /* Currently IPA-SRA guarantees the aggregate access type
2308 : exactly matches in this case. So if it does not match, it is
2309 : a pass-through argument that will be sorted out at edge
2310 : redirection time. */
2311 978 : ipa_param_body_replacement *pbr
2312 978 : = lookup_replacement_1 (base, agg_arg_offset);
2313 :
2314 978 : if (!pbr
2315 978 : || (TYPE_MAIN_VARIANT (TREE_TYPE (t))
2316 960 : != TYPE_MAIN_VARIANT (TREE_TYPE (pbr->repl))))
2317 : {
2318 957 : recreate = true;
2319 957 : pass_through_args.safe_push (i);
2320 957 : pass_through_pbr_indices.safe_push (first_rep_index);
2321 957 : pass_through_offsets.safe_push (agg_arg_offset);
2322 : }
2323 : }
2324 : }
2325 :
2326 705428 : if (!recreate)
2327 : {
2328 : /* No need to rebuild the statement, let's just modify arguments
2329 : and the LHS if/as appropriate. */
2330 : bool modified = false;
2331 2247415 : for (unsigned i = 0; i < nargs; i++)
2332 : {
2333 1559575 : tree *t = gimple_call_arg_ptr (stmt, i);
2334 1559575 : modified |= modify_expression (t, true);
2335 : }
2336 687840 : if (gimple_call_lhs (stmt))
2337 : {
2338 262270 : tree *t = gimple_call_lhs_ptr (stmt);
2339 262270 : modified |= modify_expression (t, false);
2340 : }
2341 : return modified;
2342 : }
2343 :
2344 17588 : auto_vec<int, 16> index_map;
2345 17588 : auto_vec<pass_through_split_map, 4> pass_through_map;
2346 17588 : auto_vec<tree, 16> vargs;
2347 17588 : int always_copy_delta = 0;
2348 17588 : unsigned pt_idx = 0;
2349 17588 : int new_arg_idx = 0;
2350 73740 : for (unsigned i = 0; i < nargs; i++)
2351 : {
2352 56152 : if (pt_idx < pass_through_args.length ()
2353 64048 : && i == pass_through_args[pt_idx])
2354 : {
2355 5220 : unsigned j = pass_through_pbr_indices[pt_idx];
2356 5220 : unsigned agg_arg_offset = pass_through_offsets[pt_idx];
2357 5220 : pt_idx++;
2358 5220 : always_copy_delta--;
2359 5220 : tree base = m_replacements[j].base;
2360 :
2361 : /* In order to be put into SSA form, we have to push all replacements
2362 : pertaining to this parameter as parameters to the call statement.
2363 : Edge redirection will need to use edge summary to weed out the
2364 : unnecessary ones. */
2365 5220 : unsigned repl_list_len = m_replacements.length ();
2366 14256 : for (; j < repl_list_len; j++)
2367 : {
2368 9739 : if (m_replacements[j].base != base)
2369 : break;
2370 9036 : if (m_replacements[j].unit_offset < agg_arg_offset)
2371 4 : continue;
2372 9032 : pass_through_split_map pt_map;
2373 9032 : pt_map.base_index = i;
2374 9032 : pt_map.unit_offset
2375 9032 : = m_replacements[j].unit_offset - agg_arg_offset;
2376 9032 : pt_map.new_index = new_arg_idx;
2377 9032 : pass_through_map.safe_push (pt_map);
2378 9032 : vargs.safe_push (m_replacements[j].repl);
2379 9032 : new_arg_idx++;
2380 9032 : always_copy_delta++;
2381 : }
2382 5220 : index_map.safe_push (-1);
2383 : }
2384 : else
2385 : {
2386 50932 : tree t = gimple_call_arg (stmt, i);
2387 50932 : if (TREE_CODE (t) == SSA_NAME
2388 50932 : && m_dead_ssas.contains (t))
2389 : {
2390 13338 : always_copy_delta--;
2391 13338 : index_map.safe_push (-1);
2392 : }
2393 : else
2394 : {
2395 37594 : modify_expression (&t, true);
2396 37594 : vargs.safe_push (t);
2397 37594 : index_map.safe_push (new_arg_idx);
2398 37594 : new_arg_idx++;
2399 : }
2400 : }
2401 : }
2402 :
2403 17588 : gcall *new_stmt;
2404 17588 : if (callback_ifn_assume_call_p (orig_stmt))
2405 19 : new_stmt = gimple_build_call_internal_vec (IFN_ASSUME, vargs);
2406 : else
2407 17569 : new_stmt = gimple_build_call_vec (gimple_call_fn (stmt), vargs);
2408 17588 : if (gimple_has_location (stmt))
2409 16582 : gimple_set_location (new_stmt, gimple_location (stmt));
2410 17588 : gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
2411 17588 : gimple_call_copy_flags (new_stmt, stmt);
2412 17588 : if (tree lhs = gimple_call_lhs (stmt))
2413 : {
2414 14136 : modify_expression (&lhs, false);
2415 : /* Avoid adjusting SSA_NAME_DEF_STMT of a SSA lhs, SSA names
2416 : have not yet been remapped. */
2417 14136 : *gimple_call_lhs_ptr (new_stmt) = lhs;
2418 : }
2419 17588 : *stmt_p = new_stmt;
2420 :
2421 17588 : if (orig_stmt)
2422 17588 : record_argument_state (m_id, orig_stmt, index_map, pass_through_map,
2423 : always_copy_delta);
2424 17588 : return true;
2425 723016 : }
2426 :
2427 : /* If the statement STMT contains any expressions that need to replaced with a
2428 : different one as noted by ADJUSTMENTS, do so. Handle any potential type
2429 : incompatibilities. If any conversion sttements have to be pre-pended to
2430 : STMT, they will be added to EXTRA_STMTS. Return true iff the statement was
2431 : modified. */
2432 :
2433 : bool
2434 3730910 : ipa_param_body_adjustments::modify_gimple_stmt (gimple **stmt,
2435 : gimple_seq *extra_stmts,
2436 : gimple *orig_stmt)
2437 : {
2438 3730910 : bool modified = false;
2439 3730910 : tree *t;
2440 :
2441 3730910 : switch (gimple_code (*stmt))
2442 : {
2443 151265 : case GIMPLE_RETURN:
2444 151265 : t = gimple_return_retval_ptr (as_a <greturn *> (*stmt));
2445 151265 : if (m_adjustments && m_adjustments->m_skip_return)
2446 37796 : *t = NULL_TREE;
2447 113469 : else if (*t != NULL_TREE)
2448 63900 : modified |= modify_expression (t, true);
2449 : break;
2450 :
2451 2250149 : case GIMPLE_ASSIGN:
2452 2250149 : modified |= modify_assignment (*stmt, extra_stmts);
2453 2250149 : break;
2454 :
2455 705428 : case GIMPLE_CALL:
2456 705428 : modified |= modify_call_stmt ((gcall **) stmt, orig_stmt);
2457 705428 : break;
2458 :
2459 965 : case GIMPLE_ASM:
2460 965 : {
2461 965 : gasm *asm_stmt = as_a <gasm *> (*stmt);
2462 3033 : for (unsigned i = 0; i < gimple_asm_ninputs (asm_stmt); i++)
2463 : {
2464 1103 : t = &TREE_VALUE (gimple_asm_input_op (asm_stmt, i));
2465 1103 : modified |= modify_expression (t, true);
2466 : }
2467 2541 : for (unsigned i = 0; i < gimple_asm_noutputs (asm_stmt); i++)
2468 : {
2469 1576 : t = &TREE_VALUE (gimple_asm_output_op (asm_stmt, i));
2470 1576 : modified |= modify_expression (t, false);
2471 : }
2472 : }
2473 : break;
2474 :
2475 : default:
2476 : break;
2477 : }
2478 3730910 : return modified;
2479 : }
2480 :
2481 :
2482 : /* Traverse body of the current function and perform the requested adjustments
2483 : on its statements. Return true iff the CFG has been changed. */
2484 :
2485 : bool
2486 0 : ipa_param_body_adjustments::modify_cfun_body ()
2487 : {
2488 0 : bool cfg_changed = false;
2489 0 : basic_block bb;
2490 :
2491 0 : FOR_EACH_BB_FN (bb, cfun)
2492 : {
2493 0 : gimple_stmt_iterator gsi;
2494 :
2495 0 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2496 : {
2497 0 : gphi *phi = as_a <gphi *> (gsi_stmt (gsi));
2498 0 : tree new_lhs, old_lhs = gimple_phi_result (phi);
2499 0 : new_lhs = replace_removed_params_ssa_names (old_lhs, phi);
2500 0 : if (new_lhs)
2501 : {
2502 0 : gimple_phi_set_result (phi, new_lhs);
2503 0 : release_ssa_name (old_lhs);
2504 : }
2505 : }
2506 :
2507 0 : gsi = gsi_start_bb (bb);
2508 0 : while (!gsi_end_p (gsi))
2509 : {
2510 0 : gimple *stmt = gsi_stmt (gsi);
2511 0 : gimple *stmt_copy = stmt;
2512 0 : gimple_seq extra_stmts = NULL;
2513 0 : bool modified = modify_gimple_stmt (&stmt, &extra_stmts, NULL);
2514 0 : if (stmt != stmt_copy)
2515 : {
2516 0 : gcc_checking_assert (modified);
2517 0 : gsi_replace (&gsi, stmt, false);
2518 : }
2519 0 : if (!gimple_seq_empty_p (extra_stmts))
2520 0 : gsi_insert_seq_before (&gsi, extra_stmts, GSI_SAME_STMT);
2521 :
2522 0 : def_operand_p defp;
2523 0 : ssa_op_iter iter;
2524 0 : FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF)
2525 : {
2526 0 : tree old_def = DEF_FROM_PTR (defp);
2527 0 : if (tree new_def = replace_removed_params_ssa_names (old_def,
2528 : stmt))
2529 : {
2530 0 : SET_DEF (defp, new_def);
2531 0 : release_ssa_name (old_def);
2532 0 : modified = true;
2533 : }
2534 : }
2535 :
2536 0 : if (modified)
2537 : {
2538 0 : update_stmt (stmt);
2539 0 : if (maybe_clean_eh_stmt (stmt)
2540 0 : && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
2541 : cfg_changed = true;
2542 : }
2543 0 : gsi_next (&gsi);
2544 : }
2545 : }
2546 :
2547 0 : return cfg_changed;
2548 : }
2549 :
2550 : /* Call gimple_debug_bind_reset_value on all debug statements describing
2551 : gimple register parameters that are being removed or replaced. */
2552 :
2553 : void
2554 0 : ipa_param_body_adjustments::reset_debug_stmts ()
2555 : {
2556 0 : int i, len;
2557 0 : gimple_stmt_iterator *gsip = NULL, gsi;
2558 :
2559 0 : if (MAY_HAVE_DEBUG_STMTS && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
2560 : {
2561 0 : gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
2562 0 : gsip = &gsi;
2563 : }
2564 0 : len = m_reset_debug_decls.length ();
2565 0 : for (i = 0; i < len; i++)
2566 : {
2567 0 : imm_use_iterator ui;
2568 0 : gimple *stmt;
2569 0 : gdebug *def_temp;
2570 0 : tree name, vexpr, copy = NULL_TREE;
2571 0 : use_operand_p use_p;
2572 0 : tree decl = m_reset_debug_decls[i];
2573 :
2574 0 : gcc_checking_assert (is_gimple_reg (decl));
2575 0 : name = ssa_default_def (cfun, decl);
2576 0 : vexpr = NULL;
2577 0 : if (name)
2578 0 : FOR_EACH_IMM_USE_STMT (stmt, ui, name)
2579 : {
2580 0 : if (gimple_clobber_p (stmt))
2581 : {
2582 0 : gimple_stmt_iterator cgsi = gsi_for_stmt (stmt);
2583 0 : unlink_stmt_vdef (stmt);
2584 0 : gsi_remove (&cgsi, true);
2585 0 : release_defs (stmt);
2586 0 : continue;
2587 0 : }
2588 : /* All other users must have been removed by function body
2589 : modification. */
2590 0 : gcc_assert (is_gimple_debug (stmt));
2591 0 : if (vexpr == NULL && gsip != NULL)
2592 : {
2593 0 : vexpr = build_debug_expr_decl (TREE_TYPE (name));
2594 : /* FIXME: Is setting the mode really necessary? */
2595 0 : SET_DECL_MODE (vexpr, DECL_MODE (decl));
2596 0 : def_temp = gimple_build_debug_source_bind (vexpr, decl, NULL);
2597 0 : gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
2598 : }
2599 0 : if (vexpr)
2600 : {
2601 0 : FOR_EACH_IMM_USE_ON_STMT (use_p, ui)
2602 0 : SET_USE (use_p, vexpr);
2603 : }
2604 : else
2605 0 : gimple_debug_bind_reset_value (stmt);
2606 0 : update_stmt (stmt);
2607 0 : }
2608 : /* Create a VAR_DECL for debug info purposes. */
2609 0 : if (!DECL_IGNORED_P (decl))
2610 : {
2611 0 : copy = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
2612 0 : VAR_DECL, DECL_NAME (decl),
2613 0 : TREE_TYPE (decl));
2614 0 : if (DECL_PT_UID_SET_P (decl))
2615 0 : SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
2616 0 : TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
2617 0 : TREE_READONLY (copy) = TREE_READONLY (decl);
2618 0 : TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
2619 0 : DECL_NOT_GIMPLE_REG_P (copy) = DECL_NOT_GIMPLE_REG_P (decl);
2620 0 : DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
2621 0 : DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
2622 0 : DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
2623 0 : DECL_SEEN_IN_BIND_EXPR_P (copy) = 1;
2624 0 : SET_DECL_RTL (copy, 0);
2625 0 : TREE_USED (copy) = 1;
2626 0 : DECL_CONTEXT (copy) = current_function_decl;
2627 0 : add_local_decl (cfun, copy);
2628 0 : DECL_CHAIN (copy)
2629 0 : = BLOCK_VARS (DECL_INITIAL (current_function_decl));
2630 0 : BLOCK_VARS (DECL_INITIAL (current_function_decl)) = copy;
2631 : }
2632 0 : if (gsip != NULL && copy && target_for_debug_bind (decl))
2633 : {
2634 0 : gcc_assert (TREE_CODE (decl) == PARM_DECL);
2635 0 : if (vexpr)
2636 0 : def_temp = gimple_build_debug_bind (copy, vexpr, NULL);
2637 : else
2638 0 : def_temp = gimple_build_debug_source_bind (copy, decl,
2639 : NULL);
2640 0 : gsi_insert_before (gsip, def_temp, GSI_SAME_STMT);
2641 : }
2642 : }
2643 0 : }
2644 :
2645 : /* Perform all necessary body changes to change signature, body and debug info
2646 : of fun according to adjustments passed at construction. Return true if CFG
2647 : was changed in any way. The main entry point for modification of standalone
2648 : functions that is not part of IPA clone materialization. */
2649 :
2650 : bool
2651 0 : ipa_param_body_adjustments::perform_cfun_body_modifications ()
2652 : {
2653 0 : bool cfg_changed;
2654 0 : modify_formal_parameters ();
2655 0 : cfg_changed = modify_cfun_body ();
2656 0 : reset_debug_stmts ();
2657 :
2658 0 : return cfg_changed;
2659 : }
2660 :
2661 :
2662 : /* If there are any initialization statements that need to be emitted into
2663 : the basic block BB right at the start of the new function, do so. */
2664 : void
2665 166376 : ipa_param_body_adjustments::append_init_stmts (basic_block bb)
2666 : {
2667 166376 : gimple_stmt_iterator si = gsi_last_bb (bb);
2668 167533 : while (!m_split_agg_csts_inits.is_empty ())
2669 1157 : gsi_insert_after (&si, m_split_agg_csts_inits.pop (), GSI_NEW_STMT);
2670 166376 : }
2671 :
2672 : /* Deallocate summaries which otherwise stay alive until the end of
2673 : compilation. */
2674 :
2675 : void
2676 272484 : ipa_edge_modifications_finalize ()
2677 : {
2678 272484 : if (!ipa_edge_modifications)
2679 : return;
2680 4305 : delete ipa_edge_modifications;
2681 4305 : ipa_edge_modifications = NULL;
2682 : }
2683 :
2684 : /* Helper used to sort a vector of SSA_NAMES. */
2685 :
2686 : static int
2687 18449 : compare_ssa_versions (const void *va, const void *vb)
2688 : {
2689 18449 : const_tree const a = *(const_tree const*)va;
2690 18449 : const_tree const b = *(const_tree const*)vb;
2691 :
2692 18449 : if (SSA_NAME_VERSION (a) < SSA_NAME_VERSION (b))
2693 : return -1;
2694 8048 : if (SSA_NAME_VERSION (a) > SSA_NAME_VERSION (b))
2695 8048 : return 1;
2696 : return 0;
2697 : }
2698 :
2699 : /* Call release_ssa_name on all elements in KILLED_SSAS in a defined order. */
2700 :
2701 : void
2702 2254018 : ipa_release_ssas_in_hash (hash_set <tree> *killed_ssas)
2703 : {
2704 2254018 : auto_vec<tree, 16> ssas_to_release;
2705 2266700 : for (tree sn : *killed_ssas)
2706 12682 : ssas_to_release.safe_push (sn);
2707 2254018 : ssas_to_release.qsort (compare_ssa_versions);
2708 6774736 : for (tree sn : ssas_to_release)
2709 12682 : release_ssa_name (sn);
2710 2254018 : }
|