Debian Codegraph @

gcc/calls.cc

raw
1 /* Convert function calls to rtl insns, for GNU C compiler.
2 Copyright (C) 1989-2025 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "predict.h"
29 #include "memmodel.h"
30 #include "tm_p.h"
31 #include "stringpool.h"
32 #include "expmed.h"
33 #include "optabs.h"
34 #include "emit-rtl.h"
35 #include "cgraph.h"
36 #include "diagnostic-core.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "internal-fn.h"
41 #include "dojump.h"
42 #include "explow.h"
43 #include "calls.h"
44 #include "expr.h"
45 #include "output.h"
46 #include "langhooks.h"
47 #include "except.h"
48 #include "dbgcnt.h"
49 #include "rtl-iter.h"
50 #include "tree-vrp.h"
51 #include "tree-ssanames.h"
52 #include "intl.h"
53 #include "stringpool.h"
54 #include "hash-map.h"
55 #include "hash-traits.h"
56 #include "attribs.h"
57 #include "builtins.h"
58 #include "gimple-iterator.h"
59 #include "gimple-fold.h"
60 #include "attr-fnspec.h"
61 #include "value-query.h"
62 #include "tree-pretty-print.h"
63 #include "tree-eh.h"
64
65 /* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits. */
66 #define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
67
68 /* Data structure and subroutines used within expand_call. */
69
70 struct arg_data
71 {
72 /* Tree node for this argument. */
73 tree tree_value;
74 /* Mode for value; TYPE_MODE unless promoted. */
75 machine_mode mode;
76 /* Current RTL value for argument, or 0 if it isn't precomputed. */
77 rtx value;
78 /* Initially-compute RTL value for argument; only for const functions. */
79 rtx initial_value;
80 /* Register to pass this argument in, 0 if passed on stack, or an
81 PARALLEL if the arg is to be copied into multiple non-contiguous
82 registers. */
83 rtx reg;
84 /* Register to pass this argument in when generating tail call sequence.
85 This is not the same register as for normal calls on machines with
86 register windows. */
87 rtx tail_call_reg;
88 /* If REG is a PARALLEL, this is a copy of VALUE pulled into the correct
89 form for emit_group_move. */
90 rtx parallel_value;
91 /* If REG was promoted from the actual mode of the argument expression,
92 indicates whether the promotion is sign- or zero-extended. */
93 int unsignedp;
94 /* Number of bytes to put in registers. 0 means put the whole arg
95 in registers. Also 0 if not passed in registers. */
96 int partial;
97 /* True if argument must be passed on stack.
98 Note that some arguments may be passed on the stack
99 even though pass_on_stack is false, just because FUNCTION_ARG says so.
100 pass_on_stack identifies arguments that *cannot* go in registers. */
101 bool pass_on_stack;
102 /* Some fields packaged up for locate_and_pad_parm. */
103 struct locate_and_pad_arg_data locate;
104 /* Location on the stack at which parameter should be stored. The store
105 has already been done if STACK == VALUE. */
106 rtx stack;
107 /* Location on the stack of the start of this argument slot. This can
108 differ from STACK if this arg pads downward. This location is known
109 to be aligned to TARGET_FUNCTION_ARG_BOUNDARY. */
110 rtx stack_slot;
111 /* Place that this stack area has been saved, if needed. */
112 rtx save_area;
113 /* If an argument's alignment does not permit direct copying into registers,
114 copy in smaller-sized pieces into pseudos. These are stored in a
115 block pointed to by this field. The next field says how many
116 word-sized pseudos we made. */
117 rtx *aligned_regs;
118 int n_aligned_regs;
119 };
120
121 /* A vector of one char per byte of stack space. A byte if nonzero if
122 the corresponding stack location has been used.
123 This vector is used to prevent a function call within an argument from
124 clobbering any stack already set up. */
125 static char *stack_usage_map;
126
127 /* Size of STACK_USAGE_MAP. */
128 static unsigned int highest_outgoing_arg_in_use;
129
130 /* Assume that any stack location at this byte index is used,
131 without checking the contents of stack_usage_map. */
132 static unsigned HOST_WIDE_INT stack_usage_watermark = HOST_WIDE_INT_M1U;
133
134 /* A bitmap of virtual-incoming stack space. Bit is set if the corresponding
135 stack location's tail call argument has been already stored into the stack.
136 This bitmap is used to prevent sibling call optimization if function tries
137 to use parent's incoming argument slots when they have been already
138 overwritten with tail call arguments. */
139 static sbitmap stored_args_map;
140
141 /* Assume that any virtual-incoming location at this byte index has been
142 stored, without checking the contents of stored_args_map. */
143 static unsigned HOST_WIDE_INT stored_args_watermark;
144
145 /* stack_arg_under_construction is nonzero when an argument may be
146 initialized with a constructor call (including a C function that
147 returns a BLKmode struct) and expand_call must take special action
148 to make sure the object being constructed does not overlap the
149 argument list for the constructor call. */
150 static int stack_arg_under_construction;
151
152 static void precompute_register_parameters (int, struct arg_data *, int *);
153 static bool store_one_arg (struct arg_data *, rtx, int, int, int);
154 static void store_unaligned_arguments_into_pseudos (struct arg_data *, int);
155 static bool finalize_must_preallocate (bool, int, struct arg_data *,
156 struct args_size *);
157 static void precompute_arguments (int, struct arg_data *);
158 static void compute_argument_addresses (struct arg_data *, rtx, int);
159 static rtx rtx_for_function_call (tree, tree);
160 static void load_register_parameters (struct arg_data *, int, rtx *, int,
161 int, bool *);
162 static int special_function_p (const_tree, int);
163 static bool check_sibcall_argument_overlap_1 (rtx);
164 static bool check_sibcall_argument_overlap (rtx_insn *, struct arg_data *,
165 bool);
166 static tree split_complex_types (tree);
167
168 #ifdef REG_PARM_STACK_SPACE
169 static rtx save_fixed_argument_area (int, rtx, int *, int *);
170 static void restore_fixed_argument_area (rtx, rtx, int, int);
171 #endif
172
173 /* Return true if bytes [LOWER_BOUND, UPPER_BOUND) of the outgoing
174 stack region might already be in use. */
175
176 static bool
177 stack_region_maybe_used_p (poly_uint64 lower_bound, poly_uint64 upper_bound,
178 unsigned int reg_parm_stack_space)
179 {
180 unsigned HOST_WIDE_INT const_lower, const_upper;
181 const_lower = constant_lower_bound (lower_bound);
182 if (!upper_bound.is_constant (&const_upper))
183 const_upper = HOST_WIDE_INT_M1U;
184
185 if (const_upper > stack_usage_watermark)
186 return true;
187
188 /* Don't worry about things in the fixed argument area;
189 it has already been saved. */
190 const_lower = MAX (const_lower, reg_parm_stack_space);
191 const_upper = MIN (const_upper, highest_outgoing_arg_in_use);
192 for (unsigned HOST_WIDE_INT i = const_lower; i < const_upper; ++i)
193 if (stack_usage_map[i])
194 return true;
195 return false;
196 }
197
198 /* Record that bytes [LOWER_BOUND, UPPER_BOUND) of the outgoing
199 stack region are now in use. */
200
201 static void
202 mark_stack_region_used (poly_uint64 lower_bound, poly_uint64 upper_bound)
203 {
204 unsigned HOST_WIDE_INT const_lower, const_upper;
205 const_lower = constant_lower_bound (lower_bound);
206 if (upper_bound.is_constant (&const_upper)
207 && const_upper <= highest_outgoing_arg_in_use)
208 for (unsigned HOST_WIDE_INT i = const_lower; i < const_upper; ++i)
209 stack_usage_map[i] = 1;
210 else
211 stack_usage_watermark = MIN (stack_usage_watermark, const_lower);
212 }
213
214 /* Force FUNEXP into a form suitable for the address of a CALL,
215 and return that as an rtx. Also load the static chain register
216 if FNDECL is a nested function.
217
218 CALL_FUSAGE points to a variable holding the prospective
219 CALL_INSN_FUNCTION_USAGE information. */
220
221 rtx
222 prepare_call_address (tree fndecl_or_type, rtx funexp, rtx static_chain_value,
223 rtx *call_fusage, int reg_parm_seen, int flags)
224 {
225 /* Make a valid memory address and copy constants through pseudo-regs,
226 but not for a constant address if -fno-function-cse. */
227 if (GET_CODE (funexp) != SYMBOL_REF)
228 {
229 /* If it's an indirect call by descriptor, generate code to perform
230 runtime identification of the pointer and load the descriptor. */
231 if ((flags & ECF_BY_DESCRIPTOR) && !flag_trampolines)
232 {
233 const int bit_val = targetm.calls.custom_function_descriptors;
234 rtx call_lab = gen_label_rtx ();
235
236 gcc_assert (fndecl_or_type && TYPE_P (fndecl_or_type));
237 fndecl_or_type
238 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
239 fndecl_or_type);
240 DECL_STATIC_CHAIN (fndecl_or_type) = 1;
241 rtx chain = targetm.calls.static_chain (fndecl_or_type, false);
242
243 if (GET_MODE (funexp) != Pmode)
244 funexp = convert_memory_address (Pmode, funexp);
245
246 /* Avoid long live ranges around function calls. */
247 funexp = copy_to_mode_reg (Pmode, funexp);
248
249 if (REG_P (chain))
250 emit_insn (gen_rtx_CLOBBER (VOIDmode, chain));
251
252 /* Emit the runtime identification pattern. */
253 rtx mask = gen_rtx_AND (Pmode, funexp, GEN_INT (bit_val));
254 emit_cmp_and_jump_insns (mask, const0_rtx, EQ, NULL_RTX, Pmode, 1,
255 call_lab);
256
257 /* Statically predict the branch to very likely taken. */
258 rtx_insn *insn = get_last_insn ();
259 if (JUMP_P (insn))
260 predict_insn_def (insn, PRED_BUILTIN_EXPECT, TAKEN);
261
262 /* Load the descriptor. */
263 rtx mem = gen_rtx_MEM (ptr_mode,
264 plus_constant (Pmode, funexp, - bit_val));
265 MEM_NOTRAP_P (mem) = 1;
266 mem = convert_memory_address (Pmode, mem);
267 emit_move_insn (chain, mem);
268
269 mem = gen_rtx_MEM (ptr_mode,
270 plus_constant (Pmode, funexp,
271 POINTER_SIZE / BITS_PER_UNIT
272 - bit_val));
273 MEM_NOTRAP_P (mem) = 1;
274 mem = convert_memory_address (Pmode, mem);
275 emit_move_insn (funexp, mem);
276
277 emit_label (call_lab);
278
279 if (REG_P (chain))
280 {
281 use_reg (call_fusage, chain);
282 STATIC_CHAIN_REG_P (chain) = 1;
283 }
284
285 /* Make sure we're not going to be overwritten below. */
286 gcc_assert (!static_chain_value);
287 }
288
289 /* If we are using registers for parameters, force the
290 function address into a register now. */
291 funexp = ((reg_parm_seen
292 && targetm.small_register_classes_for_mode_p (FUNCTION_MODE))
293 ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
294 : memory_address (FUNCTION_MODE, funexp));
295 }
296 else
297 {
298 /* funexp could be a SYMBOL_REF represents a function pointer which is
299 of ptr_mode. In this case, it should be converted into address mode
300 to be a valid address for memory rtx pattern. See PR 64971. */
301 if (GET_MODE (funexp) != Pmode)
302 funexp = convert_memory_address (Pmode, funexp);
303
304 if (!(flags & ECF_SIBCALL))
305 {
306 if (!NO_FUNCTION_CSE && optimize && ! flag_no_function_cse)
307 funexp = force_reg (Pmode, funexp);
308 }
309 }
310
311 if (static_chain_value != 0
312 && (TREE_CODE (fndecl_or_type) != FUNCTION_DECL
313 || DECL_STATIC_CHAIN (fndecl_or_type)))
314 {
315 rtx chain;
316
317 chain = targetm.calls.static_chain (fndecl_or_type, false);
318 static_chain_value = convert_memory_address (Pmode, static_chain_value);
319
320 emit_move_insn (chain, static_chain_value);
321 if (REG_P (chain))
322 {
323 use_reg (call_fusage, chain);
324 STATIC_CHAIN_REG_P (chain) = 1;
325 }
326 }
327
328 return funexp;
329 }
330
331 /* Generate instructions to call function FUNEXP,
332 and optionally pop the results.
333 The CALL_INSN is the first insn generated.
334
335 FNDECL is the declaration node of the function. This is given to the
336 hook TARGET_RETURN_POPS_ARGS to determine whether this function pops
337 its own args.
338
339 FUNTYPE is the data type of the function. This is given to the hook
340 TARGET_RETURN_POPS_ARGS to determine whether this function pops its
341 own args. We used to allow an identifier for library functions, but
342 that doesn't work when the return type is an aggregate type and the
343 calling convention says that the pointer to this aggregate is to be
344 popped by the callee.
345
346 STACK_SIZE is the number of bytes of arguments on the stack,
347 ROUNDED_STACK_SIZE is that number rounded up to
348 PREFERRED_STACK_BOUNDARY; zero if the size is variable. This is
349 both to put into the call insn and to generate explicit popping
350 code if necessary.
351
352 STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
353 It is zero if this call doesn't want a structure value.
354
355 NEXT_ARG_REG is the rtx that results from executing
356 targetm.calls.function_arg (&args_so_far,
357 function_arg_info::end_marker ());
358 just after all the args have had their registers assigned.
359 This could be whatever you like, but normally it is the first
360 arg-register beyond those used for args in this call,
361 or 0 if all the arg-registers are used in this call.
362 It is passed on to `gen_call' so you can put this info in the call insn.
363
364 VALREG is a hard register in which a value is returned,
365 or 0 if the call does not return a value.
366
367 OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
368 the args to this call were processed.
369 We restore `inhibit_defer_pop' to that value.
370
371 CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
372 denote registers used by the called function. */
373
374 static void
375 emit_call_1 (rtx funexp, tree fntree ATTRIBUTE_UNUSED, tree fndecl ATTRIBUTE_UNUSED,
376 tree funtype ATTRIBUTE_UNUSED,
377 poly_int64 stack_size ATTRIBUTE_UNUSED,
378 poly_int64 rounded_stack_size,
379 poly_int64 struct_value_size ATTRIBUTE_UNUSED,
380 rtx next_arg_reg ATTRIBUTE_UNUSED, rtx valreg,
381 int old_inhibit_defer_pop, rtx call_fusage, int ecf_flags,
382 cumulative_args_t args_so_far ATTRIBUTE_UNUSED)
383 {
384 rtx rounded_stack_size_rtx = gen_int_mode (rounded_stack_size, Pmode);
385 rtx call, funmem, pat;
386 bool already_popped = false;
387 poly_int64 n_popped = 0;
388
389 /* Sibling call patterns never pop arguments (no sibcall(_value)_pop
390 patterns exist). Any popping that the callee does on return will
391 be from our caller's frame rather than ours. */
392 if (!(ecf_flags & ECF_SIBCALL))
393 {
394 n_popped += targetm.calls.return_pops_args (fndecl, funtype, stack_size);
395
396 #ifdef CALL_POPS_ARGS
397 n_popped += CALL_POPS_ARGS (*get_cumulative_args (args_so_far));
398 #endif
399 }
400
401 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
402 and we don't want to load it into a register as an optimization,
403 because prepare_call_address already did it if it should be done. */
404 if (GET_CODE (funexp) != SYMBOL_REF)
405 funexp = memory_address (FUNCTION_MODE, funexp);
406
407 funmem = gen_rtx_MEM (FUNCTION_MODE, funexp);
408 if (fndecl && TREE_CODE (fndecl) == FUNCTION_DECL)
409 {
410 tree t = fndecl;
411
412 /* Although a built-in FUNCTION_DECL and its non-__builtin
413 counterpart compare equal and get a shared mem_attrs, they
414 produce different dump output in compare-debug compilations,
415 if an entry gets garbage collected in one compilation, then
416 adds a different (but equivalent) entry, while the other
417 doesn't run the garbage collector at the same spot and then
418 shares the mem_attr with the equivalent entry. */
419 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
420 {
421 tree t2 = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
422 if (t2)
423 t = t2;
424 }
425
426 set_mem_expr (funmem, t);
427 }
428 else if (fntree)
429 set_mem_expr (funmem, build_simple_mem_ref (CALL_EXPR_FN (fntree)));
430
431 if (ecf_flags & ECF_SIBCALL)
432 {
433 if (valreg)
434 pat = targetm.gen_sibcall_value (valreg, funmem,
435 rounded_stack_size_rtx,
436 next_arg_reg, NULL_RTX);
437 else
438 pat = targetm.gen_sibcall (funmem, rounded_stack_size_rtx,
439 next_arg_reg,
440 gen_int_mode (struct_value_size, Pmode));
441 }
442 /* If the target has "call" or "call_value" insns, then prefer them
443 if no arguments are actually popped. If the target does not have
444 "call" or "call_value" insns, then we must use the popping versions
445 even if the call has no arguments to pop. */
446 else if (maybe_ne (n_popped, 0)
447 || !(valreg
448 ? targetm.have_call_value ()
449 : targetm.have_call ()))
450 {
451 rtx n_pop = gen_int_mode (n_popped, Pmode);
452
453 /* If this subroutine pops its own args, record that in the call insn
454 if possible, for the sake of frame pointer elimination. */
455
456 if (valreg)
457 pat = targetm.gen_call_value_pop (valreg, funmem,
458 rounded_stack_size_rtx,
459 next_arg_reg, n_pop);
460 else
461 pat = targetm.gen_call_pop (funmem, rounded_stack_size_rtx,
462 next_arg_reg, n_pop);
463
464 already_popped = true;
465 }
466 else
467 {
468 if (valreg)
469 pat = targetm.gen_call_value (valreg, funmem, rounded_stack_size_rtx,
470 next_arg_reg, NULL_RTX);
471 else
472 pat = targetm.gen_call (funmem, rounded_stack_size_rtx, next_arg_reg,
473 gen_int_mode (struct_value_size, Pmode));
474 }
475 emit_insn (pat);
476
477 /* Find the call we just emitted. */
478 rtx_call_insn *call_insn = last_call_insn ();
479
480 /* Some target create a fresh MEM instead of reusing the one provided
481 above. Set its MEM_EXPR. */
482 call = get_call_rtx_from (call_insn);
483 if (call
484 && MEM_EXPR (XEXP (call, 0)) == NULL_TREE
485 && MEM_EXPR (funmem) != NULL_TREE)
486 set_mem_expr (XEXP (call, 0), MEM_EXPR (funmem));
487
488 /* Put the register usage information there. */
489 add_function_usage_to (call_insn, call_fusage);
490
491 /* If this is a const call, then set the insn's unchanging bit. */
492 if (ecf_flags & ECF_CONST)
493 RTL_CONST_CALL_P (call_insn) = 1;
494
495 /* If this is a pure call, then set the insn's unchanging bit. */
496 if (ecf_flags & ECF_PURE)
497 RTL_PURE_CALL_P (call_insn) = 1;
498
499 /* If this is a const call, then set the insn's unchanging bit. */
500 if (ecf_flags & ECF_LOOPING_CONST_OR_PURE)
501 RTL_LOOPING_CONST_OR_PURE_CALL_P (call_insn) = 1;
502
503 /* Create a nothrow REG_EH_REGION note, if needed. */
504 make_reg_eh_region_note (call_insn, ecf_flags, 0);
505
506 if (ecf_flags & ECF_NORETURN)
507 add_reg_note (call_insn, REG_NORETURN, const0_rtx);
508
509 if (ecf_flags & ECF_RETURNS_TWICE)
510 {
511 add_reg_note (call_insn, REG_SETJMP, const0_rtx);
512 cfun->calls_setjmp = 1;
513 }
514
515 SIBLING_CALL_P (call_insn) = ((ecf_flags & ECF_SIBCALL) != 0);
516
517 /* Restore this now, so that we do defer pops for this call's args
518 if the context of the call as a whole permits. */
519 inhibit_defer_pop = old_inhibit_defer_pop;
520
521 if (maybe_ne (n_popped, 0))
522 {
523 if (!already_popped)
524 CALL_INSN_FUNCTION_USAGE (call_insn)
525 = gen_rtx_EXPR_LIST (VOIDmode,
526 gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx),
527 CALL_INSN_FUNCTION_USAGE (call_insn));
528 rounded_stack_size -= n_popped;
529 rounded_stack_size_rtx = gen_int_mode (rounded_stack_size, Pmode);
530 stack_pointer_delta -= n_popped;
531
532 add_args_size_note (call_insn, stack_pointer_delta);
533
534 /* If popup is needed, stack realign must use DRAP */
535 if (SUPPORTS_STACK_ALIGNMENT)
536 crtl->need_drap = true;
537 }
538 /* For noreturn calls when not accumulating outgoing args force
539 REG_ARGS_SIZE note to prevent crossjumping of calls with different
540 args sizes. */
541 else if (!ACCUMULATE_OUTGOING_ARGS && (ecf_flags & ECF_NORETURN) != 0)
542 add_args_size_note (call_insn, stack_pointer_delta);
543
544 if (!ACCUMULATE_OUTGOING_ARGS)
545 {
546 /* If returning from the subroutine does not automatically pop the args,
547 we need an instruction to pop them sooner or later.
548 Perhaps do it now; perhaps just record how much space to pop later.
549
550 If returning from the subroutine does pop the args, indicate that the
551 stack pointer will be changed. */
552
553 if (maybe_ne (rounded_stack_size, 0))
554 {
555 if (ecf_flags & ECF_NORETURN)
556 /* Just pretend we did the pop. */
557 stack_pointer_delta -= rounded_stack_size;
558 else if (flag_defer_pop && inhibit_defer_pop == 0
559 && ! (ecf_flags & (ECF_CONST | ECF_PURE)))
560 pending_stack_adjust += rounded_stack_size;
561 else
562 adjust_stack (rounded_stack_size_rtx);
563 }
564 }
565 /* When we accumulate outgoing args, we must avoid any stack manipulations.
566 Restore the stack pointer to its original value now. Usually
567 ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
568 On i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
569 popping variants of functions exist as well.
570
571 ??? We may optimize similar to defer_pop above, but it is
572 probably not worthwhile.
573
574 ??? It will be worthwhile to enable combine_stack_adjustments even for
575 such machines. */
576 else if (maybe_ne (n_popped, 0))
577 anti_adjust_stack (gen_int_mode (n_popped, Pmode));
578 }
579
580 /* Determine if the function identified by FNDECL is one with
581 special properties we wish to know about. Modify FLAGS accordingly.
582
583 For example, if the function might return more than one time (setjmp), then
584 set ECF_RETURNS_TWICE.
585
586 Set ECF_MAY_BE_ALLOCA for any memory allocation function that might allocate
587 space from the stack such as alloca. */
588
589 static int
590 special_function_p (const_tree fndecl, int flags)
591 {
592 tree name_decl = DECL_NAME (fndecl);
593
594 if (maybe_special_function_p (fndecl)
595 && IDENTIFIER_LENGTH (name_decl) <= 11)
596 {
597 const char *name = IDENTIFIER_POINTER (name_decl);
598 const char *tname = name;
599
600 /* We assume that alloca will always be called by name. It
601 makes no sense to pass it as a pointer-to-function to
602 anything that does not understand its behavior. */
603 if (IDENTIFIER_LENGTH (name_decl) == 6
604 && name[0] == 'a'
605 && ! strcmp (name, "alloca"))
606 flags |= ECF_MAY_BE_ALLOCA;
607
608 /* Disregard prefix _ or __. */
609 if (name[0] == '_')
610 {
611 if (name[1] == '_')
612 tname += 2;
613 else
614 tname += 1;
615 }
616
617 /* ECF_RETURNS_TWICE is safe even for -ffreestanding. */
618 if (! strcmp (tname, "setjmp")
619 || ! strcmp (tname, "sigsetjmp")
620 || ! strcmp (name, "savectx")
621 || ! strcmp (name, "vfork")
622 || ! strcmp (name, "getcontext"))
623 flags |= ECF_RETURNS_TWICE;
624 }
625
626 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
627 && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (fndecl)))
628 flags |= ECF_MAY_BE_ALLOCA;
629
630 return flags;
631 }
632
633 /* Return fnspec for DECL. */
634
635 static attr_fnspec
636 decl_fnspec (tree fndecl)
637 {
638 tree attr;
639 tree type = TREE_TYPE (fndecl);
640 if (type)
641 {
642 attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
643 if (attr)
644 {
645 return TREE_VALUE (TREE_VALUE (attr));
646 }
647 }
648 if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
649 return builtin_fnspec (fndecl);
650 return "";
651 }
652
653 /* Similar to special_function_p; return a set of ERF_ flags for the
654 function FNDECL. */
655 static int
656 decl_return_flags (tree fndecl)
657 {
658 attr_fnspec fnspec = decl_fnspec (fndecl);
659
660 unsigned int arg;
661 if (fnspec.returns_arg (&arg))
662 return ERF_RETURNS_ARG | arg;
663
664 if (fnspec.returns_noalias_p ())
665 return ERF_NOALIAS;
666 return 0;
667 }
668
669 /* Return true when FNDECL represents a call to setjmp. */
670
671 bool
672 setjmp_call_p (const_tree fndecl)
673 {
674 if (DECL_IS_RETURNS_TWICE (fndecl))
675 return true;
676 if (special_function_p (fndecl, 0) & ECF_RETURNS_TWICE)
677 return true;
678
679 return false;
680 }
681
682
683 /* Return true if STMT may be an alloca call. */
684
685 bool
686 gimple_maybe_alloca_call_p (const gimple *stmt)
687 {
688 tree fndecl;
689
690 if (!is_gimple_call (stmt))
691 return false;
692
693 fndecl = gimple_call_fndecl (stmt);
694 if (fndecl && (special_function_p (fndecl, 0) & ECF_MAY_BE_ALLOCA))
695 return true;
696
697 return false;
698 }
699
700 /* Return true if STMT is a builtin alloca call. */
701
702 bool
703 gimple_alloca_call_p (const gimple *stmt)
704 {
705 tree fndecl;
706
707 if (!is_gimple_call (stmt))
708 return false;
709
710 fndecl = gimple_call_fndecl (stmt);
711 if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
712 switch (DECL_FUNCTION_CODE (fndecl))
713 {
714 CASE_BUILT_IN_ALLOCA:
715 return gimple_call_num_args (stmt) > 0;
716 default:
717 break;
718 }
719
720 return false;
721 }
722
723 /* Return true when exp contains a builtin alloca call. */
724
725 bool
726 alloca_call_p (const_tree exp)
727 {
728 tree fndecl;
729 if (TREE_CODE (exp) == CALL_EXPR
730 && (fndecl = get_callee_fndecl (exp))
731 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
732 switch (DECL_FUNCTION_CODE (fndecl))
733 {
734 CASE_BUILT_IN_ALLOCA:
735 return true;
736 default:
737 break;
738 }
739
740 return false;
741 }
742
743 /* Return TRUE if FNDECL is either a TM builtin or a TM cloned
744 function. Return FALSE otherwise. */
745
746 static bool
747 is_tm_builtin (const_tree fndecl)
748 {
749 if (fndecl == NULL)
750 return false;
751
752 if (decl_is_tm_clone (fndecl))
753 return true;
754
755 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
756 {
757 switch (DECL_FUNCTION_CODE (fndecl))
758 {
759 case BUILT_IN_TM_COMMIT:
760 case BUILT_IN_TM_COMMIT_EH:
761 case BUILT_IN_TM_ABORT:
762 case BUILT_IN_TM_IRREVOCABLE:
763 case BUILT_IN_TM_GETTMCLONE_IRR:
764 case BUILT_IN_TM_MEMCPY:
765 case BUILT_IN_TM_MEMMOVE:
766 case BUILT_IN_TM_MEMSET:
767 CASE_BUILT_IN_TM_STORE (1):
768 CASE_BUILT_IN_TM_STORE (2):
769 CASE_BUILT_IN_TM_STORE (4):
770 CASE_BUILT_IN_TM_STORE (8):
771 CASE_BUILT_IN_TM_STORE (FLOAT):
772 CASE_BUILT_IN_TM_STORE (DOUBLE):
773 CASE_BUILT_IN_TM_STORE (LDOUBLE):
774 CASE_BUILT_IN_TM_STORE (M64):
775 CASE_BUILT_IN_TM_STORE (M128):
776 CASE_BUILT_IN_TM_STORE (M256):
777 CASE_BUILT_IN_TM_LOAD (1):
778 CASE_BUILT_IN_TM_LOAD (2):
779 CASE_BUILT_IN_TM_LOAD (4):
780 CASE_BUILT_IN_TM_LOAD (8):
781 CASE_BUILT_IN_TM_LOAD (FLOAT):
782 CASE_BUILT_IN_TM_LOAD (DOUBLE):
783 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
784 CASE_BUILT_IN_TM_LOAD (M64):
785 CASE_BUILT_IN_TM_LOAD (M128):
786 CASE_BUILT_IN_TM_LOAD (M256):
787 case BUILT_IN_TM_LOG:
788 case BUILT_IN_TM_LOG_1:
789 case BUILT_IN_TM_LOG_2:
790 case BUILT_IN_TM_LOG_4:
791 case BUILT_IN_TM_LOG_8:
792 case BUILT_IN_TM_LOG_FLOAT:
793 case BUILT_IN_TM_LOG_DOUBLE:
794 case BUILT_IN_TM_LOG_LDOUBLE:
795 case BUILT_IN_TM_LOG_M64:
796 case BUILT_IN_TM_LOG_M128:
797 case BUILT_IN_TM_LOG_M256:
798 return true;
799 default:
800 break;
801 }
802 }
803 return false;
804 }
805
806 /* Detect flags (function attributes) from the function decl or type node. */
807
808 int
809 flags_from_decl_or_type (const_tree exp)
810 {
811 int flags = 0;
812
813 if (DECL_P (exp))
814 {
815 /* The function exp may have the `malloc' attribute. */
816 if (DECL_IS_MALLOC (exp))
817 flags |= ECF_MALLOC;
818
819 /* The function exp may have the `returns_twice' attribute. */
820 if (DECL_IS_RETURNS_TWICE (exp))
821 flags |= ECF_RETURNS_TWICE;
822
823 /* Process the pure and const attributes. */
824 if (TREE_READONLY (exp))
825 flags |= ECF_CONST;
826 if (DECL_PURE_P (exp))
827 flags |= ECF_PURE;
828 if (DECL_LOOPING_CONST_OR_PURE_P (exp))
829 flags |= ECF_LOOPING_CONST_OR_PURE;
830
831 if (DECL_IS_NOVOPS (exp))
832 flags |= ECF_NOVOPS;
833 if (lookup_attribute ("leaf", DECL_ATTRIBUTES (exp)))
834 flags |= ECF_LEAF;
835 if (lookup_attribute ("cold", DECL_ATTRIBUTES (exp)))
836 flags |= ECF_COLD;
837
838 if (TREE_NOTHROW (exp))
839 flags |= ECF_NOTHROW;
840
841 if (flag_tm)
842 {
843 if (is_tm_builtin (exp))
844 flags |= ECF_TM_BUILTIN;
845 else if ((flags & (ECF_CONST|ECF_NOVOPS)) != 0
846 || lookup_attribute ("transaction_pure",
847 TYPE_ATTRIBUTES (TREE_TYPE (exp))))
848 flags |= ECF_TM_PURE;
849 }
850
851 if (lookup_attribute ("expected_throw", DECL_ATTRIBUTES (exp)))
852 flags |= ECF_XTHROW;
853
854 flags = special_function_p (exp, flags);
855
856 if ((flags & ECF_CONST) == 0
857 && lookup_attribute ("unsequenced noptr",
858 TYPE_ATTRIBUTES (TREE_TYPE (exp))))
859 {
860 /* [[unsequenced]] with no pointers in arguments is like
861 [[gnu::const]] without finite guarantee. */
862 flags |= ECF_CONST;
863 if ((flags & ECF_PURE) == 0)
864 flags |= ECF_LOOPING_CONST_OR_PURE;
865 }
866 if ((flags & (ECF_CONST | ECF_PURE)) == 0
867 && lookup_attribute ("reproducible noptr",
868 TYPE_ATTRIBUTES (TREE_TYPE (exp))))
869 /* [[reproducible]] with no pointers in arguments is like
870 [[gnu::pure]] without finite guarantee. */
871 flags |= ECF_PURE | ECF_LOOPING_CONST_OR_PURE;
872 }
873 else if (TYPE_P (exp))
874 {
875 if (TYPE_READONLY (exp))
876 flags |= ECF_CONST;
877
878 if (flag_tm
879 && ((flags & ECF_CONST) != 0
880 || lookup_attribute ("transaction_pure", TYPE_ATTRIBUTES (exp))))
881 flags |= ECF_TM_PURE;
882
883 if ((flags & ECF_CONST) == 0
884 && lookup_attribute ("unsequenced noptr", TYPE_ATTRIBUTES (exp)))
885 /* [[unsequenced]] with no pointers in arguments is like
886 [[gnu::const]] without finite guarantee. */
887 flags |= ECF_CONST | ECF_LOOPING_CONST_OR_PURE;
888 if ((flags & ECF_CONST) == 0
889 && lookup_attribute ("reproducible noptr", TYPE_ATTRIBUTES (exp)))
890 /* [[reproducible]] with no pointers in arguments is like
891 [[gnu::pure]] without finite guarantee. */
892 flags |= ECF_PURE | ECF_LOOPING_CONST_OR_PURE;
893 }
894 else
895 gcc_unreachable ();
896
897 if (TREE_THIS_VOLATILE (exp))
898 {
899 flags |= ECF_NORETURN;
900 if (flags & (ECF_CONST|ECF_PURE))
901 flags |= ECF_LOOPING_CONST_OR_PURE;
902 }
903
904 return flags;
905 }
906
907 /* Detect flags from a CALL_EXPR. */
908
909 int
910 call_expr_flags (const_tree t)
911 {
912 int flags;
913 tree decl = get_callee_fndecl (t);
914
915 if (decl)
916 flags = flags_from_decl_or_type (decl);
917 else if (CALL_EXPR_FN (t) == NULL_TREE)
918 flags = internal_fn_flags (CALL_EXPR_IFN (t));
919 else
920 {
921 tree type = TREE_TYPE (CALL_EXPR_FN (t));
922 if (type && TREE_CODE (type) == POINTER_TYPE)
923 flags = flags_from_decl_or_type (TREE_TYPE (type));
924 else
925 flags = 0;
926 if (CALL_EXPR_BY_DESCRIPTOR (t))
927 flags |= ECF_BY_DESCRIPTOR;
928 }
929
930 return flags;
931 }
932
933 /* Return true if ARG should be passed by invisible reference. */
934
935 bool
936 pass_by_reference (CUMULATIVE_ARGS *ca, function_arg_info arg)
937 {
938 if (tree type = arg.type)
939 {
940 /* If this type contains non-trivial constructors, then it is
941 forbidden for the middle-end to create any new copies. */
942 if (TREE_ADDRESSABLE (type))
943 return true;
944
945 /* GCC post 3.4 passes *all* variable sized types by reference. */
946 if (!TYPE_SIZE (type) || !poly_int_tree_p (TYPE_SIZE (type)))
947 return true;
948
949 /* If a record type should be passed the same as its first (and only)
950 member, use the type and mode of that member. */
951 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
952 {
953 arg.type = TREE_TYPE (first_field (type));
954 arg.mode = TYPE_MODE (arg.type);
955 }
956 }
957
958 return targetm.calls.pass_by_reference (pack_cumulative_args (ca), arg);
959 }
960
961 /* Return true if TYPE should be passed by reference when passed to
962 the "..." arguments of a function. */
963
964 bool
965 pass_va_arg_by_reference (tree type)
966 {
967 return pass_by_reference (NULL, function_arg_info (type, /*named=*/false));
968 }
969
970 /* Decide whether ARG, which occurs in the state described by CA,
971 should be passed by reference. Return true if so and update
972 ARG accordingly. */
973
974 bool
975 apply_pass_by_reference_rules (CUMULATIVE_ARGS *ca, function_arg_info &arg)
976 {
977 if (pass_by_reference (ca, arg))
978 {
979 arg.type = build_pointer_type (arg.type);
980 arg.mode = TYPE_MODE (arg.type);
981 arg.pass_by_reference = true;
982 return true;
983 }
984 return false;
985 }
986
987 /* Return true if ARG, which is passed by reference, should be callee
988 copied instead of caller copied. */
989
990 bool
991 reference_callee_copied (CUMULATIVE_ARGS *ca, const function_arg_info &arg)
992 {
993 if (arg.type && TREE_ADDRESSABLE (arg.type))
994 return false;
995 return targetm.calls.callee_copies (pack_cumulative_args (ca), arg);
996 }
997
998
999 /* Precompute all register parameters as described by ARGS, storing values
1000 into fields within the ARGS array.
1001
1002 NUM_ACTUALS indicates the total number elements in the ARGS array.
1003
1004 Set REG_PARM_SEEN if we encounter a register parameter. */
1005
1006 static void
1007 precompute_register_parameters (int num_actuals, struct arg_data *args,
1008 int *reg_parm_seen)
1009 {
1010 int i;
1011
1012 *reg_parm_seen = 0;
1013
1014 for (i = 0; i < num_actuals; i++)
1015 if (args[i].reg != 0 && ! args[i].pass_on_stack)
1016 {
1017 *reg_parm_seen = 1;
1018
1019 if (args[i].value == 0)
1020 {
1021 push_temp_slots ();
1022 args[i].value = expand_normal (args[i].tree_value);
1023 preserve_temp_slots (args[i].value);
1024 pop_temp_slots ();
1025 }
1026
1027 /* If we are to promote the function arg to a wider mode,
1028 do it now. */
1029
1030 machine_mode old_mode = TYPE_MODE (TREE_TYPE (args[i].tree_value));
1031
1032 /* Some ABIs require scalar floating point modes to be returned
1033 in a wider scalar integer mode. We need to explicitly
1034 reinterpret to an integer mode of the correct precision
1035 before extending to the desired result. */
1036 if (SCALAR_INT_MODE_P (args[i].mode)
1037 && SCALAR_FLOAT_MODE_P (old_mode)
1038 && known_gt (GET_MODE_SIZE (args[i].mode),
1039 GET_MODE_SIZE (old_mode)))
1040 args[i].value = convert_float_to_wider_int (args[i].mode, old_mode,
1041 args[i].value);
1042 else if (args[i].mode != old_mode)
1043 args[i].value = convert_modes (args[i].mode, old_mode,
1044 args[i].value, args[i].unsignedp);
1045
1046 /* If the value is a non-legitimate constant, force it into a
1047 pseudo now. TLS symbols sometimes need a call to resolve. */
1048 if (CONSTANT_P (args[i].value)
1049 && (!targetm.legitimate_constant_p (args[i].mode, args[i].value)
1050 || targetm.precompute_tls_p (args[i].mode, args[i].value)))
1051 args[i].value = force_reg (args[i].mode, args[i].value);
1052
1053 /* If we're going to have to load the value by parts, pull the
1054 parts into pseudos. The part extraction process can involve
1055 non-trivial computation. */
1056 if (GET_CODE (args[i].reg) == PARALLEL)
1057 {
1058 tree type = TREE_TYPE (args[i].tree_value);
1059 args[i].parallel_value
1060 = emit_group_load_into_temps (args[i].reg, args[i].value,
1061 type, int_size_in_bytes (type));
1062 }
1063
1064 /* If the value is expensive, and we are inside an appropriately
1065 short loop, put the value into a pseudo and then put the pseudo
1066 into the hard reg.
1067
1068 For small register classes, also do this if this call uses
1069 register parameters. This is to avoid reload conflicts while
1070 loading the parameters registers. */
1071
1072 else if ((! (REG_P (args[i].value)
1073 || (GET_CODE (args[i].value) == SUBREG
1074 && REG_P (SUBREG_REG (args[i].value)))))
1075 && args[i].mode != BLKmode
1076 && (set_src_cost (args[i].value, args[i].mode,
1077 optimize_insn_for_speed_p ())
1078 > COSTS_N_INSNS (1))
1079 && ((*reg_parm_seen
1080 && targetm.small_register_classes_for_mode_p (args[i].mode))
1081 || optimize))
1082 args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
1083 }
1084 }
1085
1086 #ifdef REG_PARM_STACK_SPACE
1087
1088 /* The argument list is the property of the called routine and it
1089 may clobber it. If the fixed area has been used for previous
1090 parameters, we must save and restore it. */
1091
1092 static rtx
1093 save_fixed_argument_area (int reg_parm_stack_space, rtx argblock, int *low_to_save, int *high_to_save)
1094 {
1095 unsigned int low;
1096 unsigned int high;
1097
1098 /* Compute the boundary of the area that needs to be saved, if any. */
1099 high = reg_parm_stack_space;
1100 if (ARGS_GROW_DOWNWARD)
1101 high += 1;
1102
1103 if (high > highest_outgoing_arg_in_use)
1104 high = highest_outgoing_arg_in_use;
1105
1106 for (low = 0; low < high; low++)
1107 if (stack_usage_map[low] != 0 || low >= stack_usage_watermark)
1108 {
1109 int num_to_save;
1110 machine_mode save_mode;
1111 int delta;
1112 rtx addr;
1113 rtx stack_area;
1114 rtx save_area;
1115
1116 while (stack_usage_map[--high] == 0)
1117 ;
1118
1119 *low_to_save = low;
1120 *high_to_save = high;
1121
1122 num_to_save = high - low + 1;
1123
1124 /* If we don't have the required alignment, must do this
1125 in BLKmode. */
1126 scalar_int_mode imode;
1127 if (int_mode_for_size (num_to_save * BITS_PER_UNIT, 1).exists (&imode)
1128 && (low & (MIN (GET_MODE_SIZE (imode),
1129 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)) == 0)
1130 save_mode = imode;
1131 else
1132 save_mode = BLKmode;
1133
1134 if (ARGS_GROW_DOWNWARD)
1135 delta = -high;
1136 else
1137 delta = low;
1138
1139 addr = plus_constant (Pmode, argblock, delta);
1140 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
1141
1142 set_mem_align (stack_area, PARM_BOUNDARY);
1143 if (save_mode == BLKmode)
1144 {
1145 save_area = assign_stack_temp (BLKmode, num_to_save);
1146 emit_block_move (validize_mem (save_area), stack_area,
1147 GEN_INT (num_to_save), BLOCK_OP_CALL_PARM);
1148 }
1149 else
1150 {
1151 save_area = gen_reg_rtx (save_mode);
1152 emit_move_insn (save_area, stack_area);
1153 }
1154
1155 return save_area;
1156 }
1157
1158 return NULL_RTX;
1159 }
1160
1161 static void
1162 restore_fixed_argument_area (rtx save_area, rtx argblock, int high_to_save, int low_to_save)
1163 {
1164 machine_mode save_mode = GET_MODE (save_area);
1165 int delta;
1166 rtx addr, stack_area;
1167
1168 if (ARGS_GROW_DOWNWARD)
1169 delta = -high_to_save;
1170 else
1171 delta = low_to_save;
1172
1173 addr = plus_constant (Pmode, argblock, delta);
1174 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
1175 set_mem_align (stack_area, PARM_BOUNDARY);
1176
1177 if (save_mode != BLKmode)
1178 emit_move_insn (stack_area, save_area);
1179 else
1180 emit_block_move (stack_area, validize_mem (save_area),
1181 GEN_INT (high_to_save - low_to_save + 1),
1182 BLOCK_OP_CALL_PARM);
1183 }
1184 #endif /* REG_PARM_STACK_SPACE */
1185
1186 /* If any elements in ARGS refer to parameters that are to be passed in
1187 registers, but not in memory, and whose alignment does not permit a
1188 direct copy into registers. Copy the values into a group of pseudos
1189 which we will later copy into the appropriate hard registers.
1190
1191 Pseudos for each unaligned argument will be stored into the array
1192 args[argnum].aligned_regs. The caller is responsible for deallocating
1193 the aligned_regs array if it is nonzero. */
1194
1195 static void
1196 store_unaligned_arguments_into_pseudos (struct arg_data *args, int num_actuals)
1197 {
1198 int i, j;
1199
1200 for (i = 0; i < num_actuals; i++)
1201 if (args[i].reg != 0 && ! args[i].pass_on_stack
1202 && GET_CODE (args[i].reg) != PARALLEL
1203 && args[i].mode == BLKmode
1204 && MEM_P (args[i].value)
1205 && (MEM_ALIGN (args[i].value)
1206 < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
1207 {
1208 int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1209 int endian_correction = 0;
1210
1211 if (args[i].partial)
1212 {
1213 gcc_assert (args[i].partial % UNITS_PER_WORD == 0);
1214 args[i].n_aligned_regs = args[i].partial / UNITS_PER_WORD;
1215 }
1216 else
1217 {
1218 args[i].n_aligned_regs
1219 = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1220 }
1221
1222 args[i].aligned_regs = XNEWVEC (rtx, args[i].n_aligned_regs);
1223
1224 /* Structures smaller than a word are normally aligned to the
1225 least significant byte. On a BYTES_BIG_ENDIAN machine,
1226 this means we must skip the empty high order bytes when
1227 calculating the bit offset. */
1228 if (bytes < UNITS_PER_WORD
1229 #ifdef BLOCK_REG_PADDING
1230 && (BLOCK_REG_PADDING (args[i].mode,
1231 TREE_TYPE (args[i].tree_value), 1)
1232 == PAD_DOWNWARD)
1233 #else
1234 && BYTES_BIG_ENDIAN
1235 #endif
1236 )
1237 endian_correction = BITS_PER_WORD - bytes * BITS_PER_UNIT;
1238
1239 for (j = 0; j < args[i].n_aligned_regs; j++)
1240 {
1241 rtx reg = gen_reg_rtx (word_mode);
1242 rtx word = operand_subword_force (args[i].value, j, BLKmode);
1243 int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
1244
1245 args[i].aligned_regs[j] = reg;
1246 word = extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
1247 word_mode, word_mode, false, NULL);
1248
1249 /* There is no need to restrict this code to loading items
1250 in TYPE_ALIGN sized hunks. The bitfield instructions can
1251 load up entire word sized registers efficiently.
1252
1253 ??? This may not be needed anymore.
1254 We use to emit a clobber here but that doesn't let later
1255 passes optimize the instructions we emit. By storing 0 into
1256 the register later passes know the first AND to zero out the
1257 bitfield being set in the register is unnecessary. The store
1258 of 0 will be deleted as will at least the first AND. */
1259
1260 emit_move_insn (reg, const0_rtx);
1261
1262 bytes -= bitsize / BITS_PER_UNIT;
1263 store_bit_field (reg, bitsize, endian_correction, 0, 0,
1264 word_mode, word, false, false);
1265 }
1266 }
1267 }
1268
1269 /* Issue an error if CALL_EXPR was flagged as requiring
1270 tall-call optimization. */
1271
1272 void
1273 maybe_complain_about_tail_call (tree call_expr, const char *reason)
1274 {
1275 gcc_assert (TREE_CODE (call_expr) == CALL_EXPR);
1276 if (CALL_EXPR_TAILCALL (call_expr)
1277 && dump_file
1278 && (dump_flags & TDF_DETAILS))
1279 {
1280 fprintf (dump_file, ";; Cannot tail-call: %s: ", reason);
1281 print_generic_expr (dump_file, call_expr, TDF_SLIM);
1282 fprintf (dump_file, "\n");
1283 }
1284 if (CALL_EXPR_MUST_TAIL_CALL (call_expr))
1285 {
1286 error_at (EXPR_LOCATION (call_expr), "cannot tail-call: %s", reason);
1287 CALL_EXPR_MUST_TAIL_CALL (call_expr) = 0;
1288 }
1289 }
1290
1291 /* Fill in ARGS_SIZE and ARGS array based on the parameters found in
1292 CALL_EXPR EXP.
1293
1294 NUM_ACTUALS is the total number of parameters.
1295
1296 N_NAMED_ARGS is the total number of named arguments.
1297
1298 STRUCT_VALUE_ADDR_VALUE is the implicit argument for a struct return
1299 value, or null.
1300
1301 FNDECL is the tree code for the target of this call (if known)
1302
1303 ARGS_SO_FAR holds state needed by the target to know where to place
1304 the next argument.
1305
1306 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
1307 for arguments which are passed in registers.
1308
1309 OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
1310 and may be modified by this routine.
1311
1312 OLD_PENDING_ADJ and FLAGS are pointers to integer flags which
1313 may be modified by this routine.
1314
1315 MUST_PREALLOCATE is a pointer to bool which may be
1316 modified by this routine.
1317
1318 MAY_TAILCALL is cleared if we encounter an invisible pass-by-reference
1319 that requires allocation of stack space.
1320
1321 CALL_FROM_THUNK_P is true if this call is the jump from a thunk to
1322 the thunked-to function. */
1323
1324 static void
1325 initialize_argument_information (int num_actuals ATTRIBUTE_UNUSED,
1326 struct arg_data *args,
1327 struct args_size *args_size,
1328 int n_named_args ATTRIBUTE_UNUSED,
1329 tree exp, tree struct_value_addr_value,
1330 tree fndecl, tree fntype,
1331 cumulative_args_t args_so_far,
1332 int reg_parm_stack_space,
1333 rtx *old_stack_level,
1334 poly_int64 *old_pending_adj,
1335 bool *must_preallocate, int *ecf_flags,
1336 bool *may_tailcall, bool call_from_thunk_p)
1337 {
1338 CUMULATIVE_ARGS *args_so_far_pnt = get_cumulative_args (args_so_far);
1339 location_t loc = EXPR_LOCATION (exp);
1340
1341 /* Count arg position in order args appear. */
1342 int argpos;
1343
1344 int i;
1345
1346 args_size->constant = 0;
1347 args_size->var = 0;
1348
1349 /* In this loop, we consider args in the order they are written.
1350 We fill up ARGS from the back. */
1351
1352 i = num_actuals - 1;
1353 {
1354 int j = i;
1355 call_expr_arg_iterator iter;
1356 tree arg;
1357
1358 if (struct_value_addr_value)
1359 {
1360 args[j].tree_value = struct_value_addr_value;
1361 j--;
1362 }
1363 argpos = 0;
1364 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
1365 {
1366 tree argtype = TREE_TYPE (arg);
1367
1368 if (targetm.calls.split_complex_arg
1369 && argtype
1370 && TREE_CODE (argtype) == COMPLEX_TYPE
1371 && targetm.calls.split_complex_arg (argtype))
1372 {
1373 tree subtype = TREE_TYPE (argtype);
1374 args[j].tree_value = build1 (REALPART_EXPR, subtype, arg);
1375 j--;
1376 args[j].tree_value = build1 (IMAGPART_EXPR, subtype, arg);
1377 }
1378 else
1379 args[j].tree_value = arg;
1380 j--;
1381 argpos++;
1382 }
1383 }
1384
1385 /* I counts args in order (to be) pushed; ARGPOS counts in order written. */
1386 for (argpos = 0; argpos < num_actuals; i--, argpos++)
1387 {
1388 tree type = TREE_TYPE (args[i].tree_value);
1389 int unsignedp;
1390
1391 /* Replace erroneous argument with constant zero. */
1392 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1393 args[i].tree_value = integer_zero_node, type = integer_type_node;
1394
1395 /* If TYPE is a transparent union or record, pass things the way
1396 we would pass the first field of the union or record. We have
1397 already verified that the modes are the same. */
1398 if (RECORD_OR_UNION_TYPE_P (type) && TYPE_TRANSPARENT_AGGR (type))
1399 type = TREE_TYPE (first_field (type));
1400
1401 /* Decide where to pass this arg.
1402
1403 args[i].reg is nonzero if all or part is passed in registers.
1404
1405 args[i].partial is nonzero if part but not all is passed in registers,
1406 and the exact value says how many bytes are passed in registers.
1407
1408 args[i].pass_on_stack is true if the argument must at least be
1409 computed on the stack. It may then be loaded back into registers
1410 if args[i].reg is nonzero.
1411
1412 These decisions are driven by the FUNCTION_... macros and must agree
1413 with those made by function.cc. */
1414
1415 /* See if this argument should be passed by invisible reference. */
1416 function_arg_info arg (type, argpos < n_named_args);
1417 if (pass_by_reference (args_so_far_pnt, arg))
1418 {
1419 const bool callee_copies
1420 = reference_callee_copied (args_so_far_pnt, arg);
1421 tree base;
1422
1423 /* If we're compiling a thunk, pass directly the address of an object
1424 already in memory, instead of making a copy. Likewise if we want
1425 to make the copy in the callee instead of the caller. */
1426 if ((call_from_thunk_p || callee_copies)
1427 && TREE_CODE (args[i].tree_value) != WITH_SIZE_EXPR
1428 && ((base = get_base_address (args[i].tree_value)), true)
1429 && TREE_CODE (base) != SSA_NAME
1430 && (!DECL_P (base) || MEM_P (DECL_RTL (base))))
1431 {
1432 /* We may have turned the parameter value into an SSA name.
1433 Go back to the original parameter so we can take the
1434 address. */
1435 if (TREE_CODE (args[i].tree_value) == SSA_NAME)
1436 {
1437 gcc_assert (SSA_NAME_IS_DEFAULT_DEF (args[i].tree_value));
1438 args[i].tree_value = SSA_NAME_VAR (args[i].tree_value);
1439 gcc_assert (TREE_CODE (args[i].tree_value) == PARM_DECL);
1440 }
1441 /* Argument setup code may have copied the value to register. We
1442 revert that optimization now because the tail call code must
1443 use the original location. */
1444 if (TREE_CODE (args[i].tree_value) == PARM_DECL
1445 && !MEM_P (DECL_RTL (args[i].tree_value))
1446 && DECL_INCOMING_RTL (args[i].tree_value)
1447 && MEM_P (DECL_INCOMING_RTL (args[i].tree_value)))
1448 set_decl_rtl (args[i].tree_value,
1449 DECL_INCOMING_RTL (args[i].tree_value));
1450
1451 mark_addressable (args[i].tree_value);
1452
1453 /* We can't use sibcalls if a callee-copied argument is
1454 stored in the current function's frame. */
1455 if (!call_from_thunk_p && DECL_P (base) && !TREE_STATIC (base))
1456 {
1457 *may_tailcall = false;
1458 maybe_complain_about_tail_call (exp, _("a callee-copied "
1459 "argument is stored "
1460 "in the current "
1461 "function's frame"));
1462 }
1463
1464 args[i].tree_value = build_fold_addr_expr_loc (loc,
1465 args[i].tree_value);
1466 type = TREE_TYPE (args[i].tree_value);
1467
1468 if (*ecf_flags & ECF_CONST)
1469 *ecf_flags &= ~(ECF_CONST | ECF_LOOPING_CONST_OR_PURE);
1470 }
1471 else
1472 {
1473 /* We make a copy of the object and pass the address to the
1474 function being called. */
1475 rtx copy;
1476
1477 if (!COMPLETE_TYPE_P (type)
1478 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
1479 || (flag_stack_check == GENERIC_STACK_CHECK
1480 && compare_tree_int (TYPE_SIZE_UNIT (type),
1481 STACK_CHECK_MAX_VAR_SIZE) > 0))
1482 {
1483 /* This is a variable-sized object. Make space on the stack
1484 for it. */
1485 rtx size_rtx = expr_size (args[i].tree_value);
1486
1487 if (*old_stack_level == 0)
1488 {
1489 emit_stack_save (SAVE_BLOCK, old_stack_level);
1490 *old_pending_adj = pending_stack_adjust;
1491 pending_stack_adjust = 0;
1492 }
1493
1494 /* We can pass TRUE as the 4th argument because we just
1495 saved the stack pointer and will restore it right after
1496 the call. */
1497 copy = allocate_dynamic_stack_space (size_rtx,
1498 TYPE_ALIGN (type),
1499 TYPE_ALIGN (type),
1500 max_int_size_in_bytes
1501 (type),
1502 true);
1503 copy = gen_rtx_MEM (BLKmode, copy);
1504 set_mem_attributes (copy, type, 1);
1505 }
1506 else
1507 copy = assign_temp (type, 1, 0);
1508
1509 store_expr (args[i].tree_value, copy, 0, false, false);
1510
1511 /* Just change the const function to pure and then let
1512 the next test clear the pure based on
1513 callee_copies. */
1514 if (*ecf_flags & ECF_CONST)
1515 {
1516 *ecf_flags &= ~ECF_CONST;
1517 *ecf_flags |= ECF_PURE;
1518 }
1519
1520 if (!callee_copies && *ecf_flags & ECF_PURE)
1521 *ecf_flags &= ~(ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
1522
1523 args[i].tree_value
1524 = build_fold_addr_expr_loc (loc, make_tree (type, copy));
1525 type = TREE_TYPE (args[i].tree_value);
1526 *may_tailcall = false;
1527 maybe_complain_about_tail_call (exp,
1528 _("argument must be passed"
1529 " by copying"));
1530 }
1531 arg.pass_by_reference = true;
1532 }
1533
1534 unsignedp = TYPE_UNSIGNED (type);
1535 arg.type = type;
1536 arg.mode
1537 = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
1538 fndecl ? TREE_TYPE (fndecl) : fntype, 0);
1539
1540 args[i].unsignedp = unsignedp;
1541 args[i].mode = arg.mode;
1542
1543 targetm.calls.warn_parameter_passing_abi (args_so_far, type);
1544
1545 args[i].reg = targetm.calls.function_arg (args_so_far, arg);
1546
1547 /* If this is a sibling call and the machine has register windows, the
1548 register window has to be unwinded before calling the routine, so
1549 arguments have to go into the incoming registers. */
1550 if (targetm.calls.function_incoming_arg != targetm.calls.function_arg)
1551 args[i].tail_call_reg
1552 = targetm.calls.function_incoming_arg (args_so_far, arg);
1553 else
1554 args[i].tail_call_reg = args[i].reg;
1555
1556 if (args[i].reg)
1557 args[i].partial = targetm.calls.arg_partial_bytes (args_so_far, arg);
1558
1559 args[i].pass_on_stack = targetm.calls.must_pass_in_stack (arg);
1560
1561 /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
1562 it means that we are to pass this arg in the register(s) designated
1563 by the PARALLEL, but also to pass it in the stack. */
1564 if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
1565 && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
1566 args[i].pass_on_stack = true;
1567
1568 /* If this is an addressable type, we must preallocate the stack
1569 since we must evaluate the object into its final location.
1570
1571 If this is to be passed in both registers and the stack, it is simpler
1572 to preallocate. */
1573 if (TREE_ADDRESSABLE (type)
1574 || (args[i].pass_on_stack && args[i].reg != 0))
1575 *must_preallocate = true;
1576
1577 /* Compute the stack-size of this argument. */
1578 if (args[i].reg == 0 || args[i].partial != 0
1579 || reg_parm_stack_space > 0
1580 || args[i].pass_on_stack)
1581 locate_and_pad_parm (arg.mode, type,
1582 #ifdef STACK_PARMS_IN_REG_PARM_AREA
1583 1,
1584 #else
1585 args[i].reg != 0,
1586 #endif
1587 reg_parm_stack_space,
1588 args[i].pass_on_stack ? 0 : args[i].partial,
1589 fndecl, args_size, &args[i].locate);
1590 #ifdef BLOCK_REG_PADDING
1591 else
1592 /* The argument is passed entirely in registers. See at which
1593 end it should be padded. */
1594 args[i].locate.where_pad =
1595 BLOCK_REG_PADDING (arg.mode, type,
1596 int_size_in_bytes (type) <= UNITS_PER_WORD);
1597 #endif
1598
1599 /* Update ARGS_SIZE, the total stack space for args so far. */
1600
1601 args_size->constant += args[i].locate.size.constant;
1602 if (args[i].locate.size.var)
1603 ADD_PARM_SIZE (*args_size, args[i].locate.size.var);
1604
1605 /* Increment ARGS_SO_FAR, which has info about which arg-registers
1606 have been used, etc. */
1607
1608 /* ??? Traditionally we've passed TYPE_MODE here, instead of the
1609 promoted_mode used for function_arg above. However, the
1610 corresponding handling of incoming arguments in function.cc
1611 does pass the promoted mode. */
1612 arg.mode = TYPE_MODE (type);
1613 targetm.calls.function_arg_advance (args_so_far, arg);
1614 }
1615 }
1616
1617 /* Update ARGS_SIZE to contain the total size for the argument block.
1618 Return the original constant component of the argument block's size.
1619
1620 REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
1621 for arguments passed in registers. */
1622
1623 static poly_int64
1624 compute_argument_block_size (int reg_parm_stack_space,
1625 struct args_size *args_size,
1626 tree fndecl ATTRIBUTE_UNUSED,
1627 tree fntype ATTRIBUTE_UNUSED,
1628 int preferred_stack_boundary ATTRIBUTE_UNUSED)
1629 {
1630 poly_int64 unadjusted_args_size = args_size->constant;
1631
1632 /* For accumulate outgoing args mode we don't need to align, since the frame
1633 will be already aligned. Align to STACK_BOUNDARY in order to prevent
1634 backends from generating misaligned frame sizes. */
1635 if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
1636 preferred_stack_boundary = STACK_BOUNDARY;
1637
1638 /* Compute the actual size of the argument block required. The variable
1639 and constant sizes must be combined, the size may have to be rounded,
1640 and there may be a minimum required size. */
1641
1642 if (args_size->var)
1643 {
1644 args_size->var = ARGS_SIZE_TREE (*args_size);
1645 args_size->constant = 0;
1646
1647 preferred_stack_boundary /= BITS_PER_UNIT;
1648 if (preferred_stack_boundary > 1)
1649 {
1650 /* We don't handle this case yet. To handle it correctly we have
1651 to add the delta, round and subtract the delta.
1652 Currently no machine description requires this support. */
1653 gcc_assert (multiple_p (stack_pointer_delta,
1654 preferred_stack_boundary));
1655 args_size->var = round_up (args_size->var, preferred_stack_boundary);
1656 }
1657
1658 if (reg_parm_stack_space > 0)
1659 {
1660 args_size->var
1661 = size_binop (MAX_EXPR, args_size->var,
1662 ssize_int (reg_parm_stack_space));
1663
1664 /* The area corresponding to register parameters is not to count in
1665 the size of the block we need. So make the adjustment. */
1666 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1667 args_size->var
1668 = size_binop (MINUS_EXPR, args_size->var,
1669 ssize_int (reg_parm_stack_space));
1670 }
1671 }
1672 else
1673 {
1674 preferred_stack_boundary /= BITS_PER_UNIT;
1675 if (preferred_stack_boundary < 1)
1676 preferred_stack_boundary = 1;
1677 args_size->constant = (aligned_upper_bound (args_size->constant
1678 + stack_pointer_delta,
1679 preferred_stack_boundary)
1680 - stack_pointer_delta);
1681
1682 args_size->constant = upper_bound (args_size->constant,
1683 reg_parm_stack_space);
1684
1685 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1686 args_size->constant -= reg_parm_stack_space;
1687 }
1688 return unadjusted_args_size;
1689 }
1690
1691 /* Precompute parameters as needed for a function call.
1692
1693 FLAGS is mask of ECF_* constants.
1694
1695 NUM_ACTUALS is the number of arguments.
1696
1697 ARGS is an array containing information for each argument; this
1698 routine fills in the INITIAL_VALUE and VALUE fields for each
1699 precomputed argument. */
1700
1701 static void
1702 precompute_arguments (int num_actuals, struct arg_data *args)
1703 {
1704 int i;
1705
1706 /* If this is a libcall, then precompute all arguments so that we do not
1707 get extraneous instructions emitted as part of the libcall sequence. */
1708
1709 /* If we preallocated the stack space, and some arguments must be passed
1710 on the stack, then we must precompute any parameter which contains a
1711 function call which will store arguments on the stack.
1712 Otherwise, evaluating the parameter may clobber previous parameters
1713 which have already been stored into the stack. (we have code to avoid
1714 such case by saving the outgoing stack arguments, but it results in
1715 worse code) */
1716 if (!ACCUMULATE_OUTGOING_ARGS)
1717 return;
1718
1719 for (i = 0; i < num_actuals; i++)
1720 {
1721 tree type;
1722 machine_mode mode;
1723
1724 if (TREE_CODE (args[i].tree_value) != CALL_EXPR)
1725 continue;
1726
1727 /* If this is an addressable type, we cannot pre-evaluate it. */
1728 type = TREE_TYPE (args[i].tree_value);
1729 gcc_assert (!TREE_ADDRESSABLE (type));
1730
1731 args[i].initial_value = args[i].value
1732 = expand_normal (args[i].tree_value);
1733
1734 mode = TYPE_MODE (type);
1735 if (mode != args[i].mode)
1736 {
1737 int unsignedp = args[i].unsignedp;
1738 args[i].value
1739 = convert_modes (args[i].mode, mode,
1740 args[i].value, args[i].unsignedp);
1741
1742 /* CSE will replace this only if it contains args[i].value
1743 pseudo, so convert it down to the declared mode using
1744 a SUBREG. */
1745 if (REG_P (args[i].value)
1746 && GET_MODE_CLASS (args[i].mode) == MODE_INT
1747 && promote_mode (type, mode, &unsignedp) != args[i].mode)
1748 {
1749 args[i].initial_value
1750 = gen_lowpart_SUBREG (mode, args[i].value);
1751 SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1752 SUBREG_PROMOTED_SET (args[i].initial_value, args[i].unsignedp);
1753 }
1754 }
1755 }
1756 }
1757
1758 /* Given the current state of MUST_PREALLOCATE and information about
1759 arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
1760 compute and return the final value for MUST_PREALLOCATE. */
1761
1762 static bool
1763 finalize_must_preallocate (bool must_preallocate, int num_actuals,
1764 struct arg_data *args, struct args_size *args_size)
1765 {
1766 /* See if we have or want to preallocate stack space.
1767
1768 If we would have to push a partially-in-regs parm
1769 before other stack parms, preallocate stack space instead.
1770
1771 If the size of some parm is not a multiple of the required stack
1772 alignment, we must preallocate.
1773
1774 If the total size of arguments that would otherwise create a copy in
1775 a temporary (such as a CALL) is more than half the total argument list
1776 size, preallocation is faster.
1777
1778 Another reason to preallocate is if we have a machine (like the m88k)
1779 where stack alignment is required to be maintained between every
1780 pair of insns, not just when the call is made. However, we assume here
1781 that such machines either do not have push insns (and hence preallocation
1782 would occur anyway) or the problem is taken care of with
1783 PUSH_ROUNDING. */
1784
1785 if (! must_preallocate)
1786 {
1787 bool partial_seen = false;
1788 poly_int64 copy_to_evaluate_size = 0;
1789 int i;
1790
1791 for (i = 0; i < num_actuals && ! must_preallocate; i++)
1792 {
1793 if (args[i].partial > 0 && ! args[i].pass_on_stack)
1794 partial_seen = true;
1795 else if (partial_seen && args[i].reg == 0)
1796 must_preallocate = true;
1797
1798 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1799 && (TREE_CODE (args[i].tree_value) == CALL_EXPR
1800 || TREE_CODE (args[i].tree_value) == TARGET_EXPR
1801 || TREE_CODE (args[i].tree_value) == COND_EXPR
1802 || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
1803 copy_to_evaluate_size
1804 += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1805 }
1806
1807 if (maybe_ne (args_size->constant, 0)
1808 && maybe_ge (copy_to_evaluate_size * 2, args_size->constant))
1809 must_preallocate = true;
1810 }
1811 return must_preallocate;
1812 }
1813
1814 /* If we preallocated stack space, compute the address of each argument
1815 and store it into the ARGS array.
1816
1817 We need not ensure it is a valid memory address here; it will be
1818 validized when it is used.
1819
1820 ARGBLOCK is an rtx for the address of the outgoing arguments. */
1821
1822 static void
1823 compute_argument_addresses (struct arg_data *args, rtx argblock, int num_actuals)
1824 {
1825 if (argblock)
1826 {
1827 rtx arg_reg = argblock;
1828 int i;
1829 poly_int64 arg_offset = 0;
1830
1831 if (GET_CODE (argblock) == PLUS)
1832 {
1833 arg_reg = XEXP (argblock, 0);
1834 arg_offset = rtx_to_poly_int64 (XEXP (argblock, 1));
1835 }
1836
1837 for (i = 0; i < num_actuals; i++)
1838 {
1839 rtx offset = ARGS_SIZE_RTX (args[i].locate.offset);
1840 rtx slot_offset = ARGS_SIZE_RTX (args[i].locate.slot_offset);
1841 rtx addr;
1842 unsigned int align, boundary;
1843 poly_uint64 units_on_stack = 0;
1844 machine_mode partial_mode = VOIDmode;
1845
1846 /* Skip this parm if it will not be passed on the stack. */
1847 if (! args[i].pass_on_stack
1848 && args[i].reg != 0
1849 && args[i].partial == 0)
1850 continue;
1851
1852 if (TYPE_EMPTY_P (TREE_TYPE (args[i].tree_value)))
1853 continue;
1854
1855 addr = simplify_gen_binary (PLUS, Pmode, arg_reg, offset);
1856 addr = plus_constant (Pmode, addr, arg_offset);
1857
1858 if (args[i].partial != 0)
1859 {
1860 /* Only part of the parameter is being passed on the stack.
1861 Generate a simple memory reference of the correct size. */
1862 units_on_stack = args[i].locate.size.constant;
1863 poly_uint64 bits_on_stack = units_on_stack * BITS_PER_UNIT;
1864 partial_mode = int_mode_for_size (bits_on_stack, 1).else_blk ();
1865 args[i].stack = gen_rtx_MEM (partial_mode, addr);
1866 set_mem_size (args[i].stack, units_on_stack);
1867 }
1868 else
1869 {
1870 args[i].stack = gen_rtx_MEM (args[i].mode, addr);
1871 set_mem_attributes (args[i].stack,
1872 TREE_TYPE (args[i].tree_value), 1);
1873 }
1874 align = BITS_PER_UNIT;
1875 boundary = args[i].locate.boundary;
1876 poly_int64 offset_val;
1877 if (args[i].locate.where_pad != PAD_DOWNWARD)
1878 align = boundary;
1879 else if (poly_int_rtx_p (offset, &offset_val))
1880 {
1881 align = least_bit_hwi (boundary);
1882 unsigned int offset_align
1883 = known_alignment (offset_val) * BITS_PER_UNIT;
1884 if (offset_align != 0)
1885 align = MIN (align, offset_align);
1886 }
1887 set_mem_align (args[i].stack, align);
1888
1889 addr = simplify_gen_binary (PLUS, Pmode, arg_reg, slot_offset);
1890 addr = plus_constant (Pmode, addr, arg_offset);
1891
1892 if (args[i].partial != 0)
1893 {
1894 /* Only part of the parameter is being passed on the stack.
1895 Generate a simple memory reference of the correct size.
1896 */
1897 args[i].stack_slot = gen_rtx_MEM (partial_mode, addr);
1898 set_mem_size (args[i].stack_slot, units_on_stack);
1899 }
1900 else
1901 {
1902 args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
1903 set_mem_attributes (args[i].stack_slot,
1904 TREE_TYPE (args[i].tree_value), 1);
1905 }
1906 set_mem_align (args[i].stack_slot, args[i].locate.boundary);
1907
1908 /* Function incoming arguments may overlap with sibling call
1909 outgoing arguments and we cannot allow reordering of reads
1910 from function arguments with stores to outgoing arguments
1911 of sibling calls. */
1912 set_mem_alias_set (args[i].stack, 0);
1913 set_mem_alias_set (args[i].stack_slot, 0);
1914 }
1915 }
1916 }
1917
1918 /* Given a FNDECL and EXP, return an rtx suitable for use as a target address
1919 in a call instruction.
1920
1921 FNDECL is the tree node for the target function. For an indirect call
1922 FNDECL will be NULL_TREE.
1923
1924 ADDR is the operand 0 of CALL_EXPR for this call. */
1925
1926 static rtx
1927 rtx_for_function_call (tree fndecl, tree addr)
1928 {
1929 rtx funexp;
1930
1931 /* Get the function to call, in the form of RTL. */
1932 if (fndecl)
1933 {
1934 if (!TREE_USED (fndecl) && fndecl != current_function_decl)
1935 TREE_USED (fndecl) = 1;
1936
1937 /* Get a SYMBOL_REF rtx for the function address. */
1938 funexp = XEXP (DECL_RTL (fndecl), 0);
1939 }
1940 else
1941 /* Generate an rtx (probably a pseudo-register) for the address. */
1942 {
1943 push_temp_slots ();
1944 funexp = expand_normal (addr);
1945 pop_temp_slots (); /* FUNEXP can't be BLKmode. */
1946 }
1947 return funexp;
1948 }
1949
1950 /* Return the static chain for this function, if any. */
1951
1952 rtx
1953 rtx_for_static_chain (const_tree fndecl_or_type, bool incoming_p)
1954 {
1955 if (DECL_P (fndecl_or_type) && !DECL_STATIC_CHAIN (fndecl_or_type))
1956 return NULL;
1957
1958 return targetm.calls.static_chain (fndecl_or_type, incoming_p);
1959 }
1960
1961 /* Internal state for internal_arg_pointer_based_exp and its helpers. */
1962 static struct
1963 {
1964 /* Last insn that has been scanned by internal_arg_pointer_based_exp_scan,
1965 or NULL_RTX if none has been scanned yet. */
1966 rtx_insn *scan_start;
1967 /* Vector indexed by REGNO - FIRST_PSEUDO_REGISTER, recording if a pseudo is
1968 based on crtl->args.internal_arg_pointer. The element is NULL_RTX if the
1969 pseudo isn't based on it, a CONST_INT offset if the pseudo is based on it
1970 with fixed offset, or PC if this is with variable or unknown offset. */
1971 vec<rtx> cache;
1972 } internal_arg_pointer_exp_state;
1973
1974 static rtx internal_arg_pointer_based_exp (const_rtx, bool);
1975
1976 /* Helper function for internal_arg_pointer_based_exp. Scan insns in
1977 the tail call sequence, starting with first insn that hasn't been
1978 scanned yet, and note for each pseudo on the LHS whether it is based
1979 on crtl->args.internal_arg_pointer or not, and what offset from that
1980 that pointer it has. */
1981
1982 static void
1983 internal_arg_pointer_based_exp_scan (void)
1984 {
1985 rtx_insn *insn, *scan_start = internal_arg_pointer_exp_state.scan_start;
1986
1987 if (scan_start == NULL_RTX)
1988 insn = get_insns ();
1989 else
1990 insn = NEXT_INSN (scan_start);
1991
1992 while (insn)
1993 {
1994 rtx set = single_set (insn);
1995 if (set && REG_P (SET_DEST (set)) && !HARD_REGISTER_P (SET_DEST (set)))
1996 {
1997 rtx val = NULL_RTX;
1998 unsigned int idx = REGNO (SET_DEST (set)) - FIRST_PSEUDO_REGISTER;
1999 /* Punt on pseudos set multiple times. */
2000 if (idx < internal_arg_pointer_exp_state.cache.length ()
2001 && (internal_arg_pointer_exp_state.cache[idx]
2002 != NULL_RTX))
2003 val = pc_rtx;
2004 else
2005 val = internal_arg_pointer_based_exp (SET_SRC (set), false);
2006 if (val != NULL_RTX)
2007 {
2008 if (idx >= internal_arg_pointer_exp_state.cache.length ())
2009 internal_arg_pointer_exp_state.cache
2010 .safe_grow_cleared (idx + 1, true);
2011 internal_arg_pointer_exp_state.cache[idx] = val;
2012 }
2013 }
2014 if (NEXT_INSN (insn) == NULL_RTX)
2015 scan_start = insn;
2016 insn = NEXT_INSN (insn);
2017 }
2018
2019 internal_arg_pointer_exp_state.scan_start = scan_start;
2020 }
2021
2022 /* Compute whether RTL is based on crtl->args.internal_arg_pointer. Return
2023 NULL_RTX if RTL isn't based on it, a CONST_INT offset if RTL is based on
2024 it with fixed offset, or PC if this is with variable or unknown offset.
2025 TOPLEVEL is true if the function is invoked at the topmost level. */
2026
2027 static rtx
2028 internal_arg_pointer_based_exp (const_rtx rtl, bool toplevel)
2029 {
2030 if (CONSTANT_P (rtl))
2031 return NULL_RTX;
2032
2033 if (rtl == crtl->args.internal_arg_pointer)
2034 return const0_rtx;
2035
2036 if (REG_P (rtl) && HARD_REGISTER_P (rtl))
2037 return NULL_RTX;
2038
2039 poly_int64 offset;
2040 if (GET_CODE (rtl) == PLUS && poly_int_rtx_p (XEXP (rtl, 1), &offset))
2041 {
2042 rtx val = internal_arg_pointer_based_exp (XEXP (rtl, 0), toplevel);
2043 if (val == NULL_RTX || val == pc_rtx)
2044 return val;
2045 return plus_constant (Pmode, val, offset);
2046 }
2047
2048 /* When called at the topmost level, scan pseudo assignments in between the
2049 last scanned instruction in the tail call sequence and the latest insn
2050 in that sequence. */
2051 if (toplevel)
2052 internal_arg_pointer_based_exp_scan ();
2053
2054 if (REG_P (rtl))
2055 {
2056 unsigned int idx = REGNO (rtl) - FIRST_PSEUDO_REGISTER;
2057 if (idx < internal_arg_pointer_exp_state.cache.length ())
2058 return internal_arg_pointer_exp_state.cache[idx];
2059
2060 return NULL_RTX;
2061 }
2062
2063 subrtx_iterator::array_type array;
2064 FOR_EACH_SUBRTX (iter, array, rtl, NONCONST)
2065 {
2066 const_rtx x = *iter;
2067 if (REG_P (x) && internal_arg_pointer_based_exp (x, false) != NULL_RTX)
2068 return pc_rtx;
2069 if (MEM_P (x))
2070 iter.skip_subrtxes ();
2071 }
2072
2073 return NULL_RTX;
2074 }
2075
2076 /* Return true if SIZE bytes starting from address ADDR might overlap an
2077 already-clobbered argument area. This function is used to determine
2078 if we should give up a sibcall. */
2079
2080 static bool
2081 mem_might_overlap_already_clobbered_arg_p (rtx addr, poly_uint64 size)
2082 {
2083 poly_int64 i;
2084 unsigned HOST_WIDE_INT start, end;
2085 rtx val;
2086
2087 if (bitmap_empty_p (stored_args_map)
2088 && stored_args_watermark == HOST_WIDE_INT_M1U)
2089 return false;
2090 val = internal_arg_pointer_based_exp (addr, true);
2091 if (val == NULL_RTX)
2092 return false;
2093 else if (!poly_int_rtx_p (val, &i))
2094 return true;
2095
2096 if (known_eq (size, 0U))
2097 return false;
2098
2099 if (STACK_GROWS_DOWNWARD)
2100 i -= crtl->args.pretend_args_size;
2101 else
2102 i += crtl->args.pretend_args_size;
2103
2104 if (ARGS_GROW_DOWNWARD)
2105 i = -i - size;
2106
2107 /* We can ignore any references to the function's pretend args,
2108 which at this point would manifest as negative values of I. */
2109 if (known_le (i, 0) && known_le (size, poly_uint64 (-i)))
2110 return false;
2111
2112 start = maybe_lt (i, 0) ? 0 : constant_lower_bound (i);
2113 if (!(i + size).is_constant (&end))
2114 end = HOST_WIDE_INT_M1U;
2115
2116 if (end > stored_args_watermark)
2117 return true;
2118
2119 end = MIN (end, SBITMAP_SIZE (stored_args_map));
2120 for (unsigned HOST_WIDE_INT k = start; k < end; ++k)
2121 if (bitmap_bit_p (stored_args_map, k))
2122 return true;
2123
2124 return false;
2125 }
2126
2127 /* Do the register loads required for any wholly-register parms or any
2128 parms which are passed both on the stack and in a register. Their
2129 expressions were already evaluated.
2130
2131 Mark all register-parms as living through the call, putting these USE
2132 insns in the CALL_INSN_FUNCTION_USAGE field.
2133
2134 When IS_SIBCALL, perform the check_sibcall_argument_overlap
2135 checking, setting *SIBCALL_FAILURE if appropriate. */
2136
2137 static void
2138 load_register_parameters (struct arg_data *args, int num_actuals,
2139 rtx *call_fusage, int flags, int is_sibcall,
2140 bool *sibcall_failure)
2141 {
2142 int i, j;
2143
2144 for (i = 0; i < num_actuals; i++)
2145 {
2146 rtx reg = ((flags & ECF_SIBCALL)
2147 ? args[i].tail_call_reg : args[i].reg);
2148 if (reg)
2149 {
2150 int partial = args[i].partial;
2151 int nregs;
2152 poly_int64 size = 0;
2153 HOST_WIDE_INT const_size = 0;
2154 rtx_insn *before_arg = get_last_insn ();
2155 tree tree_value = args[i].tree_value;
2156 tree type = TREE_TYPE (tree_value);
2157 if (RECORD_OR_UNION_TYPE_P (type) && TYPE_TRANSPARENT_AGGR (type))
2158 type = TREE_TYPE (first_field (type));
2159 /* Set non-negative if we must move a word at a time, even if
2160 just one word (e.g, partial == 4 && mode == DFmode). Set
2161 to -1 if we just use a normal move insn. This value can be
2162 zero if the argument is a zero size structure. */
2163 nregs = -1;
2164 if (GET_CODE (reg) == PARALLEL)
2165 ;
2166 else if (partial)
2167 {
2168 gcc_assert (partial % UNITS_PER_WORD == 0);
2169 nregs = partial / UNITS_PER_WORD;
2170 }
2171 else if (TYPE_MODE (type) == BLKmode)
2172 {
2173 /* Variable-sized parameters should be described by a
2174 PARALLEL instead. */
2175 const_size = int_size_in_bytes (type);
2176 gcc_assert (const_size >= 0);
2177 nregs = (const_size + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
2178 size = const_size;
2179 }
2180 else
2181 size = GET_MODE_SIZE (args[i].mode);
2182
2183 /* Handle calls that pass values in multiple non-contiguous
2184 locations. The Irix 6 ABI has examples of this. */
2185
2186 if (GET_CODE (reg) == PARALLEL)
2187 emit_group_move (reg, args[i].parallel_value);
2188
2189 /* If simple case, just do move. If normal partial, store_one_arg
2190 has already loaded the register for us. In all other cases,
2191 load the register(s) from memory. */
2192
2193 else if (nregs == -1)
2194 {
2195 emit_move_insn (reg, args[i].value);
2196 #ifdef BLOCK_REG_PADDING
2197 /* Handle case where we have a value that needs shifting
2198 up to the msb. eg. a QImode value and we're padding
2199 upward on a BYTES_BIG_ENDIAN machine. */
2200 if (args[i].locate.where_pad
2201 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD))
2202 {
2203 gcc_checking_assert (ordered_p (size, UNITS_PER_WORD));
2204 if (maybe_lt (size, UNITS_PER_WORD))
2205 {
2206 rtx x;
2207 poly_int64 shift
2208 = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2209
2210 /* Assigning REG here rather than a temp makes
2211 CALL_FUSAGE report the whole reg as used.
2212 Strictly speaking, the call only uses SIZE
2213 bytes at the msb end, but it doesn't seem worth
2214 generating rtl to say that. */
2215 reg = gen_rtx_REG (word_mode, REGNO (reg));
2216 x = expand_shift (LSHIFT_EXPR, word_mode,
2217 reg, shift, reg, 1);
2218 if (x != reg)
2219 emit_move_insn (reg, x);
2220 }
2221 }
2222 #endif
2223 }
2224
2225 /* If we have pre-computed the values to put in the registers in
2226 the case of non-aligned structures, copy them in now. */
2227
2228 else if (args[i].n_aligned_regs != 0)
2229 for (j = 0; j < args[i].n_aligned_regs; j++)
2230 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
2231 args[i].aligned_regs[j]);
2232
2233 /* If we need a single register and the source is a constant
2234 VAR_DECL with a simple constructor, expand that constructor
2235 via a pseudo rather than read from (possibly misaligned)
2236 memory. PR middle-end/95126. */
2237 else if (nregs == 1
2238 && partial == 0
2239 && !args[i].pass_on_stack
2240 && VAR_P (tree_value)
2241 && TREE_READONLY (tree_value)
2242 && !TREE_SIDE_EFFECTS (tree_value)
2243 && immediate_const_ctor_p (DECL_INITIAL (tree_value)))
2244 {
2245 rtx target = gen_reg_rtx (word_mode);
2246 store_constructor (DECL_INITIAL (tree_value), target, 0,
2247 int_expr_size (DECL_INITIAL (tree_value)),
2248 false);
2249 reg = gen_rtx_REG (word_mode, REGNO (reg));
2250 emit_move_insn (reg, target);
2251 }
2252 else if (partial == 0 || args[i].pass_on_stack)
2253 {
2254 /* SIZE and CONST_SIZE are 0 for partial arguments and
2255 the size of a BLKmode type otherwise. */
2256 gcc_checking_assert (known_eq (size, const_size));
2257 rtx mem = validize_mem (copy_rtx (args[i].value));
2258
2259 /* Check for overlap with already clobbered argument area,
2260 providing that this has non-zero size. */
2261 if (is_sibcall
2262 && const_size != 0
2263 && (mem_might_overlap_already_clobbered_arg_p
2264 (XEXP (args[i].value, 0), const_size)))
2265 *sibcall_failure = true;
2266
2267 if (const_size % UNITS_PER_WORD == 0
2268 || MEM_ALIGN (mem) % BITS_PER_WORD == 0)
2269 move_block_to_reg (REGNO (reg), mem, nregs, args[i].mode);
2270 else
2271 {
2272 if (nregs > 1)
2273 move_block_to_reg (REGNO (reg), mem, nregs - 1,
2274 args[i].mode);
2275 rtx dest = gen_rtx_REG (word_mode, REGNO (reg) + nregs - 1);
2276 unsigned int bitoff = (nregs - 1) * BITS_PER_WORD;
2277 unsigned int bitsize = const_size * BITS_PER_UNIT - bitoff;
2278 rtx x = extract_bit_field (mem, bitsize, bitoff, 1, dest,
2279 word_mode, word_mode, false,
2280 NULL);
2281 if (BYTES_BIG_ENDIAN)
2282 x = expand_shift (LSHIFT_EXPR, word_mode, x,
2283 BITS_PER_WORD - bitsize, dest, 1);
2284 if (x != dest)
2285 emit_move_insn (dest, x);
2286 }
2287
2288 /* Handle a BLKmode that needs shifting. */
2289 if (nregs == 1 && const_size < UNITS_PER_WORD
2290 #ifdef BLOCK_REG_PADDING
2291 && args[i].locate.where_pad == PAD_DOWNWARD
2292 #else
2293 && BYTES_BIG_ENDIAN
2294 #endif
2295 )
2296 {
2297 rtx dest = gen_rtx_REG (word_mode, REGNO (reg));
2298 int shift = (UNITS_PER_WORD - const_size) * BITS_PER_UNIT;
2299 enum tree_code dir = (BYTES_BIG_ENDIAN
2300 ? RSHIFT_EXPR : LSHIFT_EXPR);
2301 rtx x;
2302
2303 x = expand_shift (dir, word_mode, dest, shift, dest, 1);
2304 if (x != dest)
2305 emit_move_insn (dest, x);
2306 }
2307 }
2308
2309 /* When a parameter is a block, and perhaps in other cases, it is
2310 possible that it did a load from an argument slot that was
2311 already clobbered. */
2312 if (is_sibcall
2313 && check_sibcall_argument_overlap (before_arg, &args[i], false))
2314 *sibcall_failure = true;
2315
2316 /* Handle calls that pass values in multiple non-contiguous
2317 locations. The Irix 6 ABI has examples of this. */
2318 if (GET_CODE (reg) == PARALLEL)
2319 use_group_regs (call_fusage, reg);
2320 else if (nregs == -1)
2321 use_reg_mode (call_fusage, reg, TYPE_MODE (type));
2322 else if (nregs > 0)
2323 use_regs (call_fusage, REGNO (reg), nregs);
2324 }
2325 }
2326 }
2327
2328 /* We need to pop PENDING_STACK_ADJUST bytes. But, if the arguments
2329 wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
2330 bytes, then we would need to push some additional bytes to pad the
2331 arguments. So, we try to compute an adjust to the stack pointer for an
2332 amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
2333 bytes. Then, when the arguments are pushed the stack will be perfectly
2334 aligned.
2335
2336 Return true if this optimization is possible, storing the adjustment
2337 in ADJUSTMENT_OUT and setting ARGS_SIZE->CONSTANT to the number of
2338 bytes that should be popped after the call. */
2339
2340 static bool
2341 combine_pending_stack_adjustment_and_call (poly_int64 *adjustment_out,
2342 poly_int64 unadjusted_args_size,
2343 struct args_size *args_size,
2344 unsigned int preferred_unit_stack_boundary)
2345 {
2346 /* The number of bytes to pop so that the stack will be
2347 under-aligned by UNADJUSTED_ARGS_SIZE bytes. */
2348 poly_int64 adjustment;
2349 /* The alignment of the stack after the arguments are pushed, if we
2350 just pushed the arguments without adjust the stack here. */
2351 unsigned HOST_WIDE_INT unadjusted_alignment;
2352
2353 if (!known_misalignment (stack_pointer_delta + unadjusted_args_size,
2354 preferred_unit_stack_boundary,
2355 &unadjusted_alignment))
2356 return false;
2357
2358 /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
2359 as possible -- leaving just enough left to cancel out the
2360 UNADJUSTED_ALIGNMENT. In other words, we want to ensure that the
2361 PENDING_STACK_ADJUST is non-negative, and congruent to
2362 -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY. */
2363
2364 /* Begin by trying to pop all the bytes. */
2365 unsigned HOST_WIDE_INT tmp_misalignment;
2366 if (!known_misalignment (pending_stack_adjust,
2367 preferred_unit_stack_boundary,
2368 &tmp_misalignment))
2369 return false;
2370 unadjusted_alignment -= tmp_misalignment;
2371 adjustment = pending_stack_adjust;
2372 /* Push enough additional bytes that the stack will be aligned
2373 after the arguments are pushed. */
2374 if (preferred_unit_stack_boundary > 1 && unadjusted_alignment)
2375 adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
2376
2377 /* We need to know whether the adjusted argument size
2378 (UNADJUSTED_ARGS_SIZE - ADJUSTMENT) constitutes an allocation
2379 or a deallocation. */
2380 if (!ordered_p (adjustment, unadjusted_args_size))
2381 return false;
2382
2383 /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
2384 bytes after the call. The right number is the entire
2385 PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
2386 by the arguments in the first place. */
2387 args_size->constant
2388 = pending_stack_adjust - adjustment + unadjusted_args_size;
2389
2390 *adjustment_out = adjustment;
2391 return true;
2392 }
2393
2394 /* Scan X expression if it does not dereference any argument slots
2395 we already clobbered by tail call arguments (as noted in stored_args_map
2396 bitmap).
2397 Return true if X expression dereferences such argument slots,
2398 false otherwise. */
2399
2400 static bool
2401 check_sibcall_argument_overlap_1 (rtx x)
2402 {
2403 RTX_CODE code;
2404 int i, j;
2405 const char *fmt;
2406
2407 if (x == NULL_RTX)
2408 return false;
2409
2410 code = GET_CODE (x);
2411
2412 /* We need not check the operands of the CALL expression itself. */
2413 if (code == CALL)
2414 return false;
2415
2416 if (code == MEM)
2417 return (mem_might_overlap_already_clobbered_arg_p
2418 (XEXP (x, 0), GET_MODE_SIZE (GET_MODE (x))));
2419
2420 /* Scan all subexpressions. */
2421 fmt = GET_RTX_FORMAT (code);
2422 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2423 {
2424 if (*fmt == 'e')
2425 {
2426 if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
2427 return true;
2428 }
2429 else if (*fmt == 'E')
2430 {
2431 for (j = 0; j < XVECLEN (x, i); j++)
2432 if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
2433 return true;
2434 }
2435 }
2436 return false;
2437 }
2438
2439 /* Scan sequence after INSN if it does not dereference any argument slots
2440 we already clobbered by tail call arguments (as noted in stored_args_map
2441 bitmap). If MARK_STORED_ARGS_MAP, add stack slots for ARG to
2442 stored_args_map bitmap afterwards (when ARG is a register
2443 MARK_STORED_ARGS_MAP should be false). Return true if sequence after
2444 INSN dereferences such argument slots, false otherwise. */
2445
2446 static bool
2447 check_sibcall_argument_overlap (rtx_insn *insn, struct arg_data *arg,
2448 bool mark_stored_args_map)
2449 {
2450 poly_uint64 low, high;
2451 unsigned HOST_WIDE_INT const_low, const_high;
2452
2453 if (insn == NULL_RTX)
2454 insn = get_insns ();
2455 else
2456 insn = NEXT_INSN (insn);
2457
2458 for (; insn; insn = NEXT_INSN (insn))
2459 if (INSN_P (insn)
2460 && check_sibcall_argument_overlap_1 (PATTERN (insn)))
2461 break;
2462
2463 if (mark_stored_args_map)
2464 {
2465 if (ARGS_GROW_DOWNWARD)
2466 low = -arg->locate.slot_offset.constant - arg->locate.size.constant;
2467 else
2468 low = arg->locate.slot_offset.constant;
2469 high = low + arg->locate.size.constant;
2470
2471 const_low = constant_lower_bound (low);
2472 if (high.is_constant (&const_high))
2473 for (unsigned HOST_WIDE_INT i = const_low; i < const_high; ++i)
2474 bitmap_set_bit (stored_args_map, i);
2475 else
2476 stored_args_watermark = MIN (stored_args_watermark, const_low);
2477 }
2478 return insn != NULL_RTX;
2479 }
2480
2481 /* Given that a function returns a value of mode MODE at the most
2482 significant end of hard register VALUE, shift VALUE left or right
2483 as specified by LEFT_P. Return true if some action was needed. */
2484
2485 bool
2486 shift_return_value (machine_mode mode, bool left_p, rtx value)
2487 {
2488 gcc_assert (REG_P (value) && HARD_REGISTER_P (value));
2489 machine_mode value_mode = GET_MODE (value);
2490 poly_int64 shift = GET_MODE_BITSIZE (value_mode) - GET_MODE_BITSIZE (mode);
2491
2492 if (known_eq (shift, 0))
2493 return false;
2494
2495 /* Use ashr rather than lshr for right shifts. This is for the benefit
2496 of the MIPS port, which requires SImode values to be sign-extended
2497 when stored in 64-bit registers. */
2498 if (!force_expand_binop (value_mode, left_p ? ashl_optab : ashr_optab,
2499 value, gen_int_shift_amount (value_mode, shift),
2500 value, 1, OPTAB_WIDEN))
2501 gcc_unreachable ();
2502 return true;
2503 }
2504
2505 /* If X is a likely-spilled register value, copy it to a pseudo
2506 register and return that register. Return X otherwise. */
2507
2508 static rtx
2509 avoid_likely_spilled_reg (rtx x)
2510 {
2511 rtx new_rtx;
2512
2513 if (REG_P (x)
2514 && HARD_REGISTER_P (x)
2515 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (x))))
2516 {
2517 /* Make sure that we generate a REG rather than a CONCAT.
2518 Moves into CONCATs can need nontrivial instructions,
2519 and the whole point of this function is to avoid
2520 using the hard register directly in such a situation. */
2521 generating_concat_p = 0;
2522 new_rtx = gen_reg_rtx (GET_MODE (x));
2523 generating_concat_p = 1;
2524 emit_move_insn (new_rtx, x);
2525 return new_rtx;
2526 }
2527 return x;
2528 }
2529
2530 /* Helper function for expand_call.
2531 Return false is EXP is not implementable as a sibling call. */
2532
2533 static bool
2534 can_implement_as_sibling_call_p (tree exp,
2535 rtx structure_value_addr,
2536 tree funtype,
2537 tree fndecl,
2538 int flags,
2539 tree addr,
2540 const args_size &args_size)
2541 {
2542 if (!targetm.have_sibcall_epilogue ()
2543 && !targetm.emit_epilogue_for_sibcall)
2544 {
2545 maybe_complain_about_tail_call (exp, _("machine description does not "
2546 "have a sibcall_epilogue "
2547 "instruction pattern"));
2548 return false;
2549 }
2550
2551 /* Doing sibling call optimization needs some work, since
2552 structure_value_addr can be allocated on the stack.
2553 It does not seem worth the effort since few optimizable
2554 sibling calls will return a structure. */
2555 if (structure_value_addr != NULL_RTX)
2556 {
2557 maybe_complain_about_tail_call (exp, _("callee returns a structure"));
2558 return false;
2559 }
2560
2561 /* Check whether the target is able to optimize the call
2562 into a sibcall. */
2563 if (!targetm.function_ok_for_sibcall (fndecl, exp))
2564 {
2565 maybe_complain_about_tail_call (exp, _("target is not able to optimize "
2566 "the call into a sibling call"));
2567 return false;
2568 }
2569
2570 /* Functions that do not return exactly once may not be sibcall
2571 optimized. */
2572 if (flags & ECF_RETURNS_TWICE)
2573 {
2574 maybe_complain_about_tail_call (exp, _("callee returns twice"));
2575 return false;
2576 }
2577 if ((flags & ECF_NORETURN) && !CALL_EXPR_MUST_TAIL_CALL (exp))
2578 {
2579 maybe_complain_about_tail_call (exp, _("callee does not return"));
2580 return false;
2581 }
2582
2583 if (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (addr)))
2584 && !CALL_EXPR_MUST_TAIL_CALL (exp))
2585 {
2586 maybe_complain_about_tail_call (exp, _("volatile function type"));
2587 return false;
2588 }
2589
2590 /* __sanitizer_cov_trace_pc is supposed to inspect its return address
2591 to identify the caller, and therefore should not be tailcalled. */
2592 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
2593 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SANITIZER_COV_TRACE_PC)
2594 {
2595 /* No need for maybe_complain_about_tail_call here:
2596 the call is synthesized by the compiler. */
2597 return false;
2598 }
2599
2600 /* If the called function is nested in the current one, it might access
2601 some of the caller's arguments, but could clobber them beforehand if
2602 the argument areas are shared. */
2603 if (fndecl && decl_function_context (fndecl) == current_function_decl)
2604 {
2605 maybe_complain_about_tail_call (exp, _("nested function"));
2606 return false;
2607 }
2608
2609 /* If this function requires more stack slots than the current
2610 function, we cannot change it into a sibling call.
2611 crtl->args.pretend_args_size is not part of the
2612 stack allocated by our caller. */
2613 if (maybe_gt (args_size.constant,
2614 crtl->args.size - crtl->args.pretend_args_size))
2615 {
2616 maybe_complain_about_tail_call (exp, _("callee required more stack "
2617 "slots than the caller"));
2618 return false;
2619 }
2620
2621 /* If the callee pops its own arguments, then it must pop exactly
2622 the same number of arguments as the current function. */
2623 if (maybe_ne (targetm.calls.return_pops_args (fndecl, funtype,
2624 args_size.constant),
2625 targetm.calls.return_pops_args (current_function_decl,
2626 TREE_TYPE
2627 (current_function_decl),
2628 crtl->args.size)))
2629 {
2630 maybe_complain_about_tail_call (exp, _("inconsistent number of"
2631 " popped arguments"));
2632 return false;
2633 }
2634
2635 if (!lang_hooks.decls.ok_for_sibcall (fndecl))
2636 {
2637 maybe_complain_about_tail_call (exp, _("frontend does not support"
2638 " sibling call"));
2639 return false;
2640 }
2641
2642 /* All checks passed. */
2643 return true;
2644 }
2645
2646 /* Update stack alignment when the parameter is passed in the stack
2647 since the outgoing parameter requires extra alignment on the calling
2648 function side. */
2649
2650 static void
2651 update_stack_alignment_for_call (struct locate_and_pad_arg_data *locate)
2652 {
2653 if (crtl->stack_alignment_needed < locate->boundary)
2654 crtl->stack_alignment_needed = locate->boundary;
2655 if (crtl->preferred_stack_boundary < locate->boundary)
2656 crtl->preferred_stack_boundary = locate->boundary;
2657 }
2658
2659 /* Generate all the code for a CALL_EXPR exp
2660 and return an rtx for its value.
2661 Store the value in TARGET (specified as an rtx) if convenient.
2662 If the value is stored in TARGET then TARGET is returned.
2663 If IGNORE is nonzero, then we ignore the value of the function call. */
2664
2665 rtx
2666 expand_call (tree exp, rtx target, int ignore)
2667 {
2668 /* Nonzero if we are currently expanding a call. */
2669 static int currently_expanding_call = 0;
2670
2671 /* RTX for the function to be called. */
2672 rtx funexp;
2673 /* Sequence of insns to perform a normal "call". */
2674 rtx_insn *normal_call_insns = NULL;
2675 /* Sequence of insns to perform a tail "call". */
2676 rtx_insn *tail_call_insns = NULL;
2677 /* Data type of the function. */
2678 tree funtype;
2679 tree type_arg_types;
2680 tree rettype;
2681 /* Declaration of the function being called,
2682 or 0 if the function is computed (not known by name). */
2683 tree fndecl = 0;
2684 /* The type of the function being called. */
2685 tree fntype;
2686 bool try_tail_call = CALL_EXPR_TAILCALL (exp);
2687 /* tree-tailcall decided not to do tail calls. Error for the musttail case,
2688 unfortunately we don't know the reason so it's fairly vague.
2689 When tree-tailcall reported an error it already cleared the flag,
2690 so this shouldn't really happen unless the
2691 the musttail pass gave up walking before finding the call. */
2692 if (!try_tail_call)
2693 maybe_complain_about_tail_call (exp, _("other reasons"));
2694 int pass;
2695
2696 /* Register in which non-BLKmode value will be returned,
2697 or 0 if no value or if value is BLKmode. */
2698 rtx valreg;
2699 /* Address where we should return a BLKmode value;
2700 0 if value not BLKmode. */
2701 rtx structure_value_addr = 0;
2702 /* Nonzero if that address is being passed by treating it as
2703 an extra, implicit first parameter. Otherwise,
2704 it is passed by being copied directly into struct_value_rtx. */
2705 int structure_value_addr_parm = 0;
2706 /* Holds the value of implicit argument for the struct value. */
2707 tree structure_value_addr_value = NULL_TREE;
2708 /* Size of aggregate value wanted, or zero if none wanted
2709 or if we are using the non-reentrant PCC calling convention
2710 or expecting the value in registers. */
2711 poly_int64 struct_value_size = 0;
2712 /* True if called function returns an aggregate in memory PCC style,
2713 by returning the address of where to find it. */
2714 bool pcc_struct_value = false;
2715 rtx struct_value = 0;
2716
2717 /* Number of actual parameters in this call, including struct value addr. */
2718 int num_actuals;
2719 /* Number of named args. Args after this are anonymous ones
2720 and they must all go on the stack. */
2721 int n_named_args;
2722 /* Number of complex actual arguments that need to be split. */
2723 int num_complex_actuals = 0;
2724
2725 /* Vector of information about each argument.
2726 Arguments are numbered in the order they will be pushed,
2727 not the order they are written. */
2728 struct arg_data *args;
2729
2730 /* Total size in bytes of all the stack-parms scanned so far. */
2731 struct args_size args_size;
2732 struct args_size adjusted_args_size;
2733 /* Size of arguments before any adjustments (such as rounding). */
2734 poly_int64 unadjusted_args_size;
2735 /* Data on reg parms scanned so far. */
2736 CUMULATIVE_ARGS args_so_far_v;
2737 cumulative_args_t args_so_far;
2738 /* Nonzero if a reg parm has been scanned. */
2739 int reg_parm_seen;
2740
2741 /* True if we must avoid push-insns in the args for this call.
2742 If stack space is allocated for register parameters, but not by the
2743 caller, then it is preallocated in the fixed part of the stack frame.
2744 So the entire argument block must then be preallocated (i.e., we
2745 ignore PUSH_ROUNDING in that case). */
2746 bool must_preallocate = !targetm.calls.push_argument (0);
2747
2748 /* Size of the stack reserved for parameter registers. */
2749 int reg_parm_stack_space = 0;
2750
2751 /* Address of space preallocated for stack parms
2752 (on machines that lack push insns), or 0 if space not preallocated. */
2753 rtx argblock = 0;
2754
2755 /* Mask of ECF_ and ERF_ flags. */
2756 int flags = 0;
2757 int return_flags = 0;
2758 #ifdef REG_PARM_STACK_SPACE
2759 /* Define the boundary of the register parm stack space that needs to be
2760 saved, if any. */
2761 int low_to_save, high_to_save;
2762 rtx save_area = 0; /* Place that it is saved */
2763 #endif
2764
2765 unsigned int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
2766 char *initial_stack_usage_map = stack_usage_map;
2767 unsigned HOST_WIDE_INT initial_stack_usage_watermark = stack_usage_watermark;
2768 char *stack_usage_map_buf = NULL;
2769
2770 poly_int64 old_stack_allocated;
2771
2772 /* State variables to track stack modifications. */
2773 rtx old_stack_level = 0;
2774 int old_stack_arg_under_construction = 0;
2775 poly_int64 old_pending_adj = 0;
2776 int old_inhibit_defer_pop = inhibit_defer_pop;
2777
2778 /* Some stack pointer alterations we make are performed via
2779 allocate_dynamic_stack_space. This modifies the stack_pointer_delta,
2780 which we then also need to save/restore along the way. */
2781 poly_int64 old_stack_pointer_delta = 0;
2782
2783 rtx call_fusage;
2784 tree addr = CALL_EXPR_FN (exp);
2785 int i;
2786 /* The alignment of the stack, in bits. */
2787 unsigned HOST_WIDE_INT preferred_stack_boundary;
2788 /* The alignment of the stack, in bytes. */
2789 unsigned HOST_WIDE_INT preferred_unit_stack_boundary;
2790 /* The static chain value to use for this call. */
2791 rtx static_chain_value;
2792 /* See if this is "nothrow" function call. */
2793 if (TREE_NOTHROW (exp))
2794 flags |= ECF_NOTHROW;
2795
2796 /* See if we can find a DECL-node for the actual function, and get the
2797 function attributes (flags) from the function decl or type node. */
2798 fndecl = get_callee_fndecl (exp);
2799 if (fndecl)
2800 {
2801 fntype = TREE_TYPE (fndecl);
2802 flags |= flags_from_decl_or_type (fndecl);
2803 return_flags |= decl_return_flags (fndecl);
2804 }
2805 else
2806 {
2807 fntype = TREE_TYPE (TREE_TYPE (addr));
2808 flags |= flags_from_decl_or_type (fntype);
2809 if (CALL_EXPR_BY_DESCRIPTOR (exp))
2810 flags |= ECF_BY_DESCRIPTOR;
2811 }
2812 rettype = TREE_TYPE (exp);
2813
2814 struct_value = targetm.calls.struct_value_rtx (fntype, 0);
2815
2816 /* Warn if this value is an aggregate type,
2817 regardless of which calling convention we are using for it. */
2818 if (AGGREGATE_TYPE_P (rettype))
2819 warning (OPT_Waggregate_return, "function call has aggregate value");
2820
2821 /* If the result of a non looping pure or const function call is
2822 ignored (or void), and none of its arguments are volatile, we can
2823 avoid expanding the call and just evaluate the arguments for
2824 side-effects. */
2825 if ((flags & (ECF_CONST | ECF_PURE))
2826 && (!(flags & ECF_LOOPING_CONST_OR_PURE))
2827 && (flags & ECF_NOTHROW)
2828 && (ignore || target == const0_rtx
2829 || TYPE_MODE (rettype) == VOIDmode))
2830 {
2831 bool volatilep = false;
2832 tree arg;
2833 call_expr_arg_iterator iter;
2834
2835 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2836 if (TREE_THIS_VOLATILE (arg))
2837 {
2838 volatilep = true;
2839 break;
2840 }
2841
2842 if (! volatilep)
2843 {
2844 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2845 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
2846 return const0_rtx;
2847 }
2848 }
2849
2850 #ifdef REG_PARM_STACK_SPACE
2851 reg_parm_stack_space = REG_PARM_STACK_SPACE (!fndecl ? fntype : fndecl);
2852 #endif
2853
2854 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
2855 && reg_parm_stack_space > 0 && targetm.calls.push_argument (0))
2856 must_preallocate = true;
2857
2858 /* Set up a place to return a structure. */
2859
2860 /* Cater to broken compilers. */
2861 if (aggregate_value_p (exp, fntype))
2862 {
2863 /* This call returns a big structure. */
2864 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
2865
2866 #ifdef PCC_STATIC_STRUCT_RETURN
2867 {
2868 pcc_struct_value = true;
2869 }
2870 #else /* not PCC_STATIC_STRUCT_RETURN */
2871 {
2872 if (!poly_int_tree_p (TYPE_SIZE_UNIT (rettype), &struct_value_size))
2873 struct_value_size = -1;
2874
2875 /* Even if it is semantically safe to use the target as the return
2876 slot, it may be not sufficiently aligned for the return type. */
2877 if (CALL_EXPR_RETURN_SLOT_OPT (exp)
2878 && target
2879 && MEM_P (target)
2880 /* If rettype is addressable, we may not create a temporary.
2881 If target is properly aligned at runtime and the compiler
2882 just doesn't know about it, it will work fine, otherwise it
2883 will be UB. */
2884 && (TREE_ADDRESSABLE (rettype)
2885 || !(MEM_ALIGN (target) < TYPE_ALIGN (rettype)
2886 && targetm.slow_unaligned_access (TYPE_MODE (rettype),
2887 MEM_ALIGN (target)))))
2888 structure_value_addr = XEXP (target, 0);
2889 else
2890 {
2891 /* For variable-sized objects, we must be called with a target
2892 specified. If we were to allocate space on the stack here,
2893 we would have no way of knowing when to free it. */
2894 rtx d = assign_temp (rettype, 1, 1);
2895 structure_value_addr = XEXP (d, 0);
2896 target = 0;
2897 }
2898 }
2899 #endif /* not PCC_STATIC_STRUCT_RETURN */
2900 }
2901
2902 /* Figure out the amount to which the stack should be aligned. */
2903 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
2904 if (fndecl)
2905 {
2906 struct cgraph_rtl_info *i = cgraph_node::rtl_info (fndecl);
2907 /* Without automatic stack alignment, we can't increase preferred
2908 stack boundary. With automatic stack alignment, it is
2909 unnecessary since unless we can guarantee that all callers will
2910 align the outgoing stack properly, callee has to align its
2911 stack anyway. */
2912 if (i
2913 && i->preferred_incoming_stack_boundary
2914 && i->preferred_incoming_stack_boundary < preferred_stack_boundary)
2915 preferred_stack_boundary = i->preferred_incoming_stack_boundary;
2916 }
2917
2918 /* Operand 0 is a pointer-to-function; get the type of the function. */
2919 funtype = TREE_TYPE (addr);
2920 gcc_assert (POINTER_TYPE_P (funtype));
2921 funtype = TREE_TYPE (funtype);
2922
2923 /* Count whether there are actual complex arguments that need to be split
2924 into their real and imaginary parts. Munge the type_arg_types
2925 appropriately here as well. */
2926 if (targetm.calls.split_complex_arg)
2927 {
2928 call_expr_arg_iterator iter;
2929 tree arg;
2930 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2931 {
2932 tree type = TREE_TYPE (arg);
2933 if (type && TREE_CODE (type) == COMPLEX_TYPE
2934 && targetm.calls.split_complex_arg (type))
2935 num_complex_actuals++;
2936 }
2937 type_arg_types = split_complex_types (TYPE_ARG_TYPES (funtype));
2938 }
2939 else
2940 type_arg_types = TYPE_ARG_TYPES (funtype);
2941
2942 if (flags & ECF_MAY_BE_ALLOCA)
2943 cfun->calls_alloca = 1;
2944
2945 /* If struct_value_rtx is 0, it means pass the address
2946 as if it were an extra parameter. Put the argument expression
2947 in structure_value_addr_value. */
2948 if (structure_value_addr && struct_value == 0)
2949 {
2950 /* If structure_value_addr is a REG other than
2951 virtual_outgoing_args_rtx, we can use always use it. If it
2952 is not a REG, we must always copy it into a register.
2953 If it is virtual_outgoing_args_rtx, we must copy it to another
2954 register in some cases. */
2955 rtx temp = (!REG_P (structure_value_addr)
2956 || (ACCUMULATE_OUTGOING_ARGS
2957 && stack_arg_under_construction
2958 && structure_value_addr == virtual_outgoing_args_rtx)
2959 ? copy_addr_to_reg (convert_memory_address
2960 (Pmode, structure_value_addr))
2961 : structure_value_addr);
2962
2963 structure_value_addr_value =
2964 make_tree (build_pointer_type (TREE_TYPE (funtype)), temp);
2965 structure_value_addr_parm = 1;
2966 }
2967
2968 /* Count the arguments and set NUM_ACTUALS. */
2969 num_actuals
2970 = call_expr_nargs (exp) + num_complex_actuals + structure_value_addr_parm;
2971
2972 /* Compute number of named args.
2973 First, do a raw count of the args for INIT_CUMULATIVE_ARGS. */
2974
2975 if (type_arg_types != 0)
2976 n_named_args
2977 = (list_length (type_arg_types)
2978 /* Count the struct value address, if it is passed as a parm. */
2979 + structure_value_addr_parm);
2980 else if (TYPE_NO_NAMED_ARGS_STDARG_P (funtype))
2981 n_named_args = structure_value_addr_parm;
2982 else
2983 /* If we know nothing, treat all args as named. */
2984 n_named_args = num_actuals;
2985
2986 /* Start updating where the next arg would go.
2987
2988 On some machines (such as the PA) indirect calls have a different
2989 calling convention than normal calls. The fourth argument in
2990 INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
2991 or not. */
2992 INIT_CUMULATIVE_ARGS (args_so_far_v, funtype, NULL_RTX, fndecl, n_named_args);
2993 args_so_far = pack_cumulative_args (&args_so_far_v);
2994
2995 /* Now possibly adjust the number of named args.
2996 Normally, don't include the last named arg if anonymous args follow.
2997 We do include the last named arg if
2998 targetm.calls.strict_argument_naming() returns nonzero.
2999 (If no anonymous args follow, the result of list_length is actually
3000 one too large. This is harmless.)
3001
3002 If targetm.calls.pretend_outgoing_varargs_named() returns
3003 nonzero, and targetm.calls.strict_argument_naming() returns zero,
3004 this machine will be able to place unnamed args that were passed
3005 in registers into the stack. So treat all args as named. This
3006 allows the insns emitting for a specific argument list to be
3007 independent of the function declaration.
3008
3009 If targetm.calls.pretend_outgoing_varargs_named() returns zero,
3010 we do not have any reliable way to pass unnamed args in
3011 registers, so we must force them into memory. */
3012
3013 if ((type_arg_types != 0 || TYPE_NO_NAMED_ARGS_STDARG_P (funtype))
3014 && targetm.calls.strict_argument_naming (args_so_far))
3015 ;
3016 else if (type_arg_types != 0
3017 && ! targetm.calls.pretend_outgoing_varargs_named (args_so_far))
3018 /* Don't include the last named arg. */
3019 --n_named_args;
3020 else if (TYPE_NO_NAMED_ARGS_STDARG_P (funtype)
3021 && ! targetm.calls.pretend_outgoing_varargs_named (args_so_far))
3022 n_named_args = 0;
3023 else
3024 /* Treat all args as named. */
3025 n_named_args = num_actuals;
3026
3027 /* Make a vector to hold all the information about each arg. */
3028 args = XCNEWVEC (struct arg_data, num_actuals);
3029
3030 /* Build up entries in the ARGS array, compute the size of the
3031 arguments into ARGS_SIZE, etc. */
3032 initialize_argument_information (num_actuals, args, &args_size,
3033 n_named_args, exp,
3034 structure_value_addr_value, fndecl, fntype,
3035 args_so_far, reg_parm_stack_space,
3036 &old_stack_level, &old_pending_adj,
3037 &must_preallocate, &flags,
3038 &try_tail_call, CALL_FROM_THUNK_P (exp));
3039
3040 if (args_size.var)
3041 must_preallocate = true;
3042
3043 /* Now make final decision about preallocating stack space. */
3044 must_preallocate = finalize_must_preallocate (must_preallocate,
3045 num_actuals, args,
3046 &args_size);
3047
3048 /* If the structure value address will reference the stack pointer, we
3049 must stabilize it. We don't need to do this if we know that we are
3050 not going to adjust the stack pointer in processing this call. */
3051
3052 if (structure_value_addr
3053 && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
3054 || reg_mentioned_p (virtual_outgoing_args_rtx,
3055 structure_value_addr))
3056 && (args_size.var
3057 || (!ACCUMULATE_OUTGOING_ARGS
3058 && maybe_ne (args_size.constant, 0))))
3059 structure_value_addr = copy_to_reg (structure_value_addr);
3060
3061 /* Tail calls can make things harder to debug, and we've traditionally
3062 pushed these optimizations into -O2. Don't try if we're already
3063 expanding a call, as that means we're an argument. Don't try if
3064 there's cleanups, as we know there's code to follow the call. */
3065 if (currently_expanding_call++ != 0)
3066 {
3067 maybe_complain_about_tail_call (exp, _("inside another call"));
3068 try_tail_call = 0;
3069 }
3070 if (!flag_optimize_sibling_calls
3071 && !CALL_FROM_THUNK_P (exp)
3072 && !CALL_EXPR_MUST_TAIL_CALL (exp))
3073 try_tail_call = 0;
3074 if (args_size.var)
3075 {
3076 maybe_complain_about_tail_call (exp, _("variable size arguments"));
3077 try_tail_call = 0;
3078 }
3079 if (dbg_cnt (tail_call) == false)
3080 try_tail_call = 0;
3081
3082 /* Workaround buggy C/C++ wrappers around Fortran routines with
3083 character(len=constant) arguments if the hidden string length arguments
3084 are passed on the stack; if the callers forget to pass those arguments,
3085 attempting to tail call in such routines leads to stack corruption.
3086 Avoid tail calls in functions where at least one such hidden string
3087 length argument is passed (partially or fully) on the stack in the
3088 caller and the callee needs to pass any arguments on the stack.
3089 See PR90329. */
3090 if (try_tail_call && maybe_ne (args_size.constant, 0))
3091 for (tree arg = DECL_ARGUMENTS (current_function_decl);
3092 arg; arg = DECL_CHAIN (arg))
3093 if (DECL_HIDDEN_STRING_LENGTH (arg) && DECL_INCOMING_RTL (arg))
3094 {
3095 subrtx_iterator::array_type array;
3096 FOR_EACH_SUBRTX (iter, array, DECL_INCOMING_RTL (arg), NONCONST)
3097 if (MEM_P (*iter))
3098 {
3099 try_tail_call = 0;
3100 maybe_complain_about_tail_call (exp, _("hidden string length "
3101 "argument passed on "
3102 "stack"));
3103 break;
3104 }
3105 }
3106
3107 /* If the user has marked the function as requiring tail-call
3108 optimization, attempt it. */
3109 if (CALL_EXPR_MUST_TAIL_CALL (exp))
3110 try_tail_call = 1;
3111
3112 /* Rest of purposes for tail call optimizations to fail. */
3113 if (try_tail_call)
3114 try_tail_call = can_implement_as_sibling_call_p (exp,
3115 structure_value_addr,
3116 funtype,
3117 fndecl,
3118 flags, addr, args_size);
3119
3120 /* Check if caller and callee disagree in promotion of function
3121 return value. */
3122 if (try_tail_call)
3123 {
3124 machine_mode caller_mode, caller_promoted_mode;
3125 machine_mode callee_mode, callee_promoted_mode;
3126 int caller_unsignedp, callee_unsignedp;
3127 tree caller_res = DECL_RESULT (current_function_decl);
3128
3129 caller_unsignedp = TYPE_UNSIGNED (TREE_TYPE (caller_res));
3130 caller_mode = DECL_MODE (caller_res);
3131 callee_unsignedp = TYPE_UNSIGNED (TREE_TYPE (funtype));
3132 callee_mode = TYPE_MODE (TREE_TYPE (funtype));
3133 caller_promoted_mode
3134 = promote_function_mode (TREE_TYPE (caller_res), caller_mode,
3135 &caller_unsignedp,
3136 TREE_TYPE (current_function_decl), 1);
3137 callee_promoted_mode
3138 = promote_function_mode (TREE_TYPE (funtype), callee_mode,
3139 &callee_unsignedp,
3140 funtype, 1);
3141 if (caller_mode != VOIDmode
3142 && (caller_promoted_mode != callee_promoted_mode
3143 || ((caller_mode != caller_promoted_mode
3144 || callee_mode != callee_promoted_mode)
3145 && (caller_unsignedp != callee_unsignedp
3146 || partial_subreg_p (caller_mode, callee_mode)))))
3147 {
3148 try_tail_call = 0;
3149 maybe_complain_about_tail_call (exp, _("caller and callee disagree "
3150 "in promotion of function "
3151 "return value"));
3152 }
3153 }
3154
3155 /* Ensure current function's preferred stack boundary is at least
3156 what we need. Stack alignment may also increase preferred stack
3157 boundary. */
3158 for (i = 0; i < num_actuals; i++)
3159 if (reg_parm_stack_space > 0
3160 || args[i].reg == 0
3161 || args[i].partial != 0
3162 || args[i].pass_on_stack)
3163 update_stack_alignment_for_call (&args[i].locate);
3164 if (crtl->preferred_stack_boundary < preferred_stack_boundary)
3165 crtl->preferred_stack_boundary = preferred_stack_boundary;
3166 else
3167 preferred_stack_boundary = crtl->preferred_stack_boundary;
3168
3169 preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
3170
3171 if (flag_callgraph_info)
3172 record_final_call (fndecl, EXPR_LOCATION (exp));
3173
3174 /* We want to make two insn chains; one for a sibling call, the other
3175 for a normal call. We will select one of the two chains after
3176 initial RTL generation is complete. */
3177 for (pass = try_tail_call ? 0 : 1; pass < 2; pass++)
3178 {
3179 bool sibcall_failure = false;
3180 bool normal_failure = false;
3181 /* We want to emit any pending stack adjustments before the tail
3182 recursion "call". That way we know any adjustment after the tail
3183 recursion call can be ignored if we indeed use the tail
3184 call expansion. */
3185 saved_pending_stack_adjust save;
3186 rtx_insn *insns, *before_call, *after_args;
3187 rtx next_arg_reg;
3188
3189 if (pass == 0)
3190 {
3191 /* State variables we need to save and restore between
3192 iterations. */
3193 save_pending_stack_adjust (&save);
3194 }
3195 if (pass)
3196 flags &= ~ECF_SIBCALL;
3197 else
3198 flags |= ECF_SIBCALL;
3199
3200 /* Other state variables that we must reinitialize each time
3201 through the loop (that are not initialized by the loop itself). */
3202 argblock = 0;
3203 call_fusage = 0;
3204
3205 /* Start a new sequence for the normal call case.
3206
3207 From this point on, if the sibling call fails, we want to set
3208 sibcall_failure instead of continuing the loop. */
3209 start_sequence ();
3210
3211 /* Don't let pending stack adjusts add up to too much.
3212 Also, do all pending adjustments now if there is any chance
3213 this might be a call to alloca or if we are expanding a sibling
3214 call sequence.
3215 Also do the adjustments before a throwing call, otherwise
3216 exception handling can fail; PR 19225. */
3217 if (maybe_ge (pending_stack_adjust, 32)
3218 || (maybe_ne (pending_stack_adjust, 0)
3219 && (flags & ECF_MAY_BE_ALLOCA))
3220 || (maybe_ne (pending_stack_adjust, 0)
3221 && flag_exceptions && !(flags & ECF_NOTHROW))
3222 || pass == 0)
3223 do_pending_stack_adjust ();
3224
3225 /* Precompute any arguments as needed. */
3226 if (pass)
3227 precompute_arguments (num_actuals, args);
3228
3229 /* Now we are about to start emitting insns that can be deleted
3230 if a libcall is deleted. */
3231 if (pass && (flags & ECF_MALLOC))
3232 start_sequence ();
3233
3234 /* Check the canary value for sibcall or function which doesn't
3235 return and could throw. */
3236 if ((pass == 0
3237 || ((flags & ECF_NORETURN) != 0 && tree_could_throw_p (exp)))
3238 && crtl->stack_protect_guard
3239 && targetm.stack_protect_runtime_enabled_p ())
3240 stack_protect_epilogue ();
3241
3242 adjusted_args_size = args_size;
3243 /* Compute the actual size of the argument block required. The variable
3244 and constant sizes must be combined, the size may have to be rounded,
3245 and there may be a minimum required size. When generating a sibcall
3246 pattern, do not round up, since we'll be re-using whatever space our
3247 caller provided. */
3248 unadjusted_args_size
3249 = compute_argument_block_size (reg_parm_stack_space,
3250 &adjusted_args_size,
3251 fndecl, fntype,
3252 (pass == 0 ? 0
3253 : preferred_stack_boundary));
3254
3255 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
3256
3257 /* The argument block when performing a sibling call is the
3258 incoming argument block. */
3259 if (pass == 0)
3260 {
3261 argblock = crtl->args.internal_arg_pointer;
3262 if (STACK_GROWS_DOWNWARD)
3263 argblock
3264 = plus_constant (Pmode, argblock, crtl->args.pretend_args_size);
3265 else
3266 argblock
3267 = plus_constant (Pmode, argblock, -crtl->args.pretend_args_size);
3268
3269 HOST_WIDE_INT map_size = constant_lower_bound (args_size.constant);
3270 stored_args_map = sbitmap_alloc (map_size);
3271 bitmap_clear (stored_args_map);
3272 stored_args_watermark = HOST_WIDE_INT_M1U;
3273 }
3274
3275 /* If we have no actual push instructions, or shouldn't use them,
3276 make space for all args right now. */
3277 else if (adjusted_args_size.var != 0)
3278 {
3279 if (old_stack_level == 0)
3280 {
3281 emit_stack_save (SAVE_BLOCK, &old_stack_level);
3282 old_stack_pointer_delta = stack_pointer_delta;
3283 old_pending_adj = pending_stack_adjust;
3284 pending_stack_adjust = 0;
3285 /* stack_arg_under_construction says whether a stack arg is
3286 being constructed at the old stack level. Pushing the stack
3287 gets a clean outgoing argument block. */
3288 old_stack_arg_under_construction = stack_arg_under_construction;
3289 stack_arg_under_construction = 0;
3290 }
3291 argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
3292 if (flag_stack_usage_info)
3293 current_function_has_unbounded_dynamic_stack_size = 1;
3294 }
3295 else
3296 {
3297 /* Note that we must go through the motions of allocating an argument
3298 block even if the size is zero because we may be storing args
3299 in the area reserved for register arguments, which may be part of
3300 the stack frame. */
3301
3302 poly_int64 needed = adjusted_args_size.constant;
3303
3304 /* Store the maximum argument space used. It will be pushed by
3305 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
3306 checking). */
3307
3308 crtl->outgoing_args_size = upper_bound (crtl->outgoing_args_size,
3309 needed);
3310
3311 if (must_preallocate)
3312 {
3313 if (ACCUMULATE_OUTGOING_ARGS)
3314 {
3315 /* Since the stack pointer will never be pushed, it is
3316 possible for the evaluation of a parm to clobber
3317 something we have already written to the stack.
3318 Since most function calls on RISC machines do not use
3319 the stack, this is uncommon, but must work correctly.
3320
3321 Therefore, we save any area of the stack that was already
3322 written and that we are using. Here we set up to do this
3323 by making a new stack usage map from the old one. The
3324 actual save will be done by store_one_arg.
3325
3326 Another approach might be to try to reorder the argument
3327 evaluations to avoid this conflicting stack usage. */
3328
3329 /* Since we will be writing into the entire argument area,
3330 the map must be allocated for its entire size, not just
3331 the part that is the responsibility of the caller. */
3332 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
3333 needed += reg_parm_stack_space;
3334
3335 poly_int64 limit = needed;
3336 if (ARGS_GROW_DOWNWARD)
3337 limit += 1;
3338
3339 /* For polynomial sizes, this is the maximum possible
3340 size needed for arguments with a constant size
3341 and offset. */
3342 HOST_WIDE_INT const_limit = constant_lower_bound (limit);
3343 highest_outgoing_arg_in_use
3344 = MAX (initial_highest_arg_in_use, const_limit);
3345
3346 free (stack_usage_map_buf);
3347 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
3348 stack_usage_map = stack_usage_map_buf;
3349
3350 if (initial_highest_arg_in_use)
3351 memcpy (stack_usage_map, initial_stack_usage_map,
3352 initial_highest_arg_in_use);
3353
3354 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
3355 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
3356 (highest_outgoing_arg_in_use
3357 - initial_highest_arg_in_use));
3358 needed = 0;
3359
3360 /* The address of the outgoing argument list must not be
3361 copied to a register here, because argblock would be left
3362 pointing to the wrong place after the call to
3363 allocate_dynamic_stack_space below. */
3364
3365 argblock = virtual_outgoing_args_rtx;
3366 }
3367 else
3368 {
3369 /* Try to reuse some or all of the pending_stack_adjust
3370 to get this space. */
3371 if (inhibit_defer_pop == 0
3372 && (combine_pending_stack_adjustment_and_call
3373 (&needed,
3374 unadjusted_args_size,
3375 &adjusted_args_size,
3376 preferred_unit_stack_boundary)))
3377 {
3378 /* combine_pending_stack_adjustment_and_call computes
3379 an adjustment before the arguments are allocated.
3380 Account for them and see whether or not the stack
3381 needs to go up or down. */
3382 needed = unadjusted_args_size - needed;
3383
3384 /* Checked by
3385 combine_pending_stack_adjustment_and_call. */
3386 gcc_checking_assert (ordered_p (needed, 0));
3387 if (maybe_lt (needed, 0))
3388 {
3389 /* We're releasing stack space. */
3390 /* ??? We can avoid any adjustment at all if we're
3391 already aligned. FIXME. */
3392 pending_stack_adjust = -needed;
3393 do_pending_stack_adjust ();
3394 needed = 0;
3395 }
3396 else
3397 /* We need to allocate space. We'll do that in
3398 push_block below. */
3399 pending_stack_adjust = 0;
3400 }
3401
3402 /* Special case this because overhead of `push_block' in
3403 this case is non-trivial. */
3404 if (known_eq (needed, 0))
3405 argblock = virtual_outgoing_args_rtx;
3406 else
3407 {
3408 rtx needed_rtx = gen_int_mode (needed, Pmode);
3409 argblock = push_block (needed_rtx, 0, 0);
3410 if (ARGS_GROW_DOWNWARD)
3411 argblock = plus_constant (Pmode, argblock, needed);
3412 }
3413
3414 /* We only really need to call `copy_to_reg' in the case
3415 where push insns are going to be used to pass ARGBLOCK
3416 to a function call in ARGS. In that case, the stack
3417 pointer changes value from the allocation point to the
3418 call point, and hence the value of
3419 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
3420 as well always do it. */
3421 argblock = copy_to_reg (argblock);
3422 }
3423 }
3424 }
3425
3426 if (ACCUMULATE_OUTGOING_ARGS)
3427 {
3428 /* The save/restore code in store_one_arg handles all
3429 cases except one: a constructor call (including a C
3430 function returning a BLKmode struct) to initialize
3431 an argument. */
3432 if (stack_arg_under_construction)
3433 {
3434 rtx push_size
3435 = (gen_int_mode
3436 (adjusted_args_size.constant
3437 + (OUTGOING_REG_PARM_STACK_SPACE (!fndecl ? fntype
3438 : TREE_TYPE (fndecl))
3439 ? 0 : reg_parm_stack_space), Pmode));
3440 if (old_stack_level == 0)
3441 {
3442 emit_stack_save (SAVE_BLOCK, &old_stack_level);
3443 old_stack_pointer_delta = stack_pointer_delta;
3444 old_pending_adj = pending_stack_adjust;
3445 pending_stack_adjust = 0;
3446 /* stack_arg_under_construction says whether a stack
3447 arg is being constructed at the old stack level.
3448 Pushing the stack gets a clean outgoing argument
3449 block. */
3450 old_stack_arg_under_construction
3451 = stack_arg_under_construction;
3452 stack_arg_under_construction = 0;
3453 /* Make a new map for the new argument list. */
3454 free (stack_usage_map_buf);
3455 stack_usage_map_buf = XCNEWVEC (char, highest_outgoing_arg_in_use);
3456 stack_usage_map = stack_usage_map_buf;
3457 highest_outgoing_arg_in_use = 0;
3458 stack_usage_watermark = HOST_WIDE_INT_M1U;
3459 }
3460 /* We can pass TRUE as the 4th argument because we just
3461 saved the stack pointer and will restore it right after
3462 the call. */
3463 allocate_dynamic_stack_space (push_size, 0, BIGGEST_ALIGNMENT,
3464 -1, true);
3465 }
3466
3467 /* If argument evaluation might modify the stack pointer,
3468 copy the address of the argument list to a register. */
3469 for (i = 0; i < num_actuals; i++)
3470 if (args[i].pass_on_stack)
3471 {
3472 argblock = copy_addr_to_reg (argblock);
3473 break;
3474 }
3475 }
3476
3477 compute_argument_addresses (args, argblock, num_actuals);
3478
3479 /* Stack is properly aligned, pops can't safely be deferred during
3480 the evaluation of the arguments. */
3481 NO_DEFER_POP;
3482
3483 /* Precompute all register parameters. It isn't safe to compute
3484 anything once we have started filling any specific hard regs.
3485 TLS symbols sometimes need a call to resolve. Precompute
3486 register parameters before any stack pointer manipulation
3487 to avoid unaligned stack in the called function. */
3488 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
3489
3490 OK_DEFER_POP;
3491
3492 /* Perform stack alignment before the first push (the last arg). */
3493 if (argblock == 0
3494 && maybe_gt (adjusted_args_size.constant, reg_parm_stack_space)
3495 && maybe_ne (adjusted_args_size.constant, unadjusted_args_size))
3496 {
3497 /* When the stack adjustment is pending, we get better code
3498 by combining the adjustments. */
3499 if (maybe_ne (pending_stack_adjust, 0)
3500 && ! inhibit_defer_pop
3501 && (combine_pending_stack_adjustment_and_call
3502 (&pending_stack_adjust,
3503 unadjusted_args_size,
3504 &adjusted_args_size,
3505 preferred_unit_stack_boundary)))
3506 do_pending_stack_adjust ();
3507 else if (argblock == 0)
3508 anti_adjust_stack (gen_int_mode (adjusted_args_size.constant
3509 - unadjusted_args_size,
3510 Pmode));
3511 }
3512 /* Now that the stack is properly aligned, pops can't safely
3513 be deferred during the evaluation of the arguments. */
3514 NO_DEFER_POP;
3515
3516 /* Record the maximum pushed stack space size. We need to delay
3517 doing it this far to take into account the optimization done
3518 by combine_pending_stack_adjustment_and_call. */
3519 if (flag_stack_usage_info
3520 && !ACCUMULATE_OUTGOING_ARGS
3521 && pass
3522 && adjusted_args_size.var == 0)
3523 {
3524 poly_int64 pushed = (adjusted_args_size.constant
3525 + pending_stack_adjust);
3526 current_function_pushed_stack_size
3527 = upper_bound (current_function_pushed_stack_size, pushed);
3528 }
3529
3530 funexp = rtx_for_function_call (fndecl, addr);
3531
3532 if (CALL_EXPR_STATIC_CHAIN (exp))
3533 static_chain_value = expand_normal (CALL_EXPR_STATIC_CHAIN (exp));
3534 else
3535 static_chain_value = 0;
3536
3537 #ifdef REG_PARM_STACK_SPACE
3538 /* Save the fixed argument area if it's part of the caller's frame and
3539 is clobbered by argument setup for this call. */
3540 if (ACCUMULATE_OUTGOING_ARGS && pass)
3541 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3542 &low_to_save, &high_to_save);
3543 #endif
3544
3545 /* Now store (and compute if necessary) all non-register parms.
3546 These come before register parms, since they can require block-moves,
3547 which could clobber the registers used for register parms.
3548 Parms which have partial registers are not stored here,
3549 but we do preallocate space here if they want that. */
3550
3551 for (i = 0; i < num_actuals; i++)
3552 {
3553 if (args[i].reg == 0 || args[i].pass_on_stack)
3554 {
3555 rtx_insn *before_arg = get_last_insn ();
3556
3557 /* We don't allow passing huge (> 2^30 B) arguments
3558 by value. It would cause an overflow later on. */
3559 if (constant_lower_bound (adjusted_args_size.constant)
3560 >= (1 << (HOST_BITS_PER_INT - 2)))
3561 {
3562 sorry ("passing too large argument on stack");
3563 /* Don't worry about stack clean-up. */
3564 if (pass == 0)
3565 sibcall_failure = true;
3566 else
3567 normal_failure = true;
3568 continue;
3569 }
3570
3571 if (store_one_arg (&args[i], argblock, flags,
3572 adjusted_args_size.var != 0,
3573 reg_parm_stack_space)
3574 || (pass == 0
3575 && check_sibcall_argument_overlap (before_arg,
3576 &args[i], true)))
3577 sibcall_failure = true;
3578 }
3579
3580 if (args[i].stack)
3581 call_fusage
3582 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[i].tree_value)),
3583 gen_rtx_USE (VOIDmode, args[i].stack),
3584 call_fusage);
3585 }
3586
3587 /* If we have a parm that is passed in registers but not in memory
3588 and whose alignment does not permit a direct copy into registers,
3589 make a group of pseudos that correspond to each register that we
3590 will later fill. */
3591 if (STRICT_ALIGNMENT)
3592 store_unaligned_arguments_into_pseudos (args, num_actuals);
3593
3594 /* Now store any partially-in-registers parm.
3595 This is the last place a block-move can happen. */
3596 if (reg_parm_seen)
3597 for (i = 0; i < num_actuals; i++)
3598 if (args[i].partial != 0 && ! args[i].pass_on_stack)
3599 {
3600 rtx_insn *before_arg = get_last_insn ();
3601
3602 /* On targets with weird calling conventions (e.g. PA) it's
3603 hard to ensure that all cases of argument overlap between
3604 stack and registers work. Play it safe and bail out. */
3605 if (ARGS_GROW_DOWNWARD && !STACK_GROWS_DOWNWARD)
3606 {
3607 sibcall_failure = true;
3608 break;
3609 }
3610
3611 if (store_one_arg (&args[i], argblock, flags,
3612 adjusted_args_size.var != 0,
3613 reg_parm_stack_space)
3614 || (pass == 0
3615 && check_sibcall_argument_overlap (before_arg,
3616 &args[i], true)))
3617 sibcall_failure = true;
3618 }
3619
3620 /* Set up the next argument register. For sibling calls on machines
3621 with register windows this should be the incoming register. */
3622 if (pass == 0)
3623 next_arg_reg = targetm.calls.function_incoming_arg
3624 (args_so_far, function_arg_info::end_marker ());
3625 else
3626 next_arg_reg = targetm.calls.function_arg
3627 (args_so_far, function_arg_info::end_marker ());
3628
3629 targetm.calls.start_call_args (args_so_far);
3630
3631 bool any_regs = false;
3632 for (i = 0; i < num_actuals; i++)
3633 if (args[i].reg != NULL_RTX)
3634 {
3635 any_regs = true;
3636 targetm.calls.call_args (args_so_far, args[i].reg, funtype);
3637 }
3638 if (!any_regs)
3639 targetm.calls.call_args (args_so_far, pc_rtx, funtype);
3640
3641 /* Figure out the register where the value, if any, will come back. */
3642 valreg = 0;
3643 if (TYPE_MODE (rettype) != VOIDmode
3644 && ! structure_value_addr)
3645 {
3646 if (pcc_struct_value)
3647 valreg = hard_function_value (build_pointer_type (rettype),
3648 fndecl, NULL, (pass == 0));
3649 else
3650 valreg = hard_function_value (rettype, fndecl, fntype,
3651 (pass == 0));
3652
3653 /* If VALREG is a PARALLEL whose first member has a zero
3654 offset, use that. This is for targets such as m68k that
3655 return the same value in multiple places. */
3656 if (GET_CODE (valreg) == PARALLEL)
3657 {
3658 rtx elem = XVECEXP (valreg, 0, 0);
3659 rtx where = XEXP (elem, 0);
3660 rtx offset = XEXP (elem, 1);
3661 if (offset == const0_rtx
3662 && GET_MODE (where) == GET_MODE (valreg))
3663 valreg = where;
3664 }
3665 }
3666
3667 /* If register arguments require space on the stack and stack space
3668 was not preallocated, allocate stack space here for arguments
3669 passed in registers. */
3670 if (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
3671 && !ACCUMULATE_OUTGOING_ARGS
3672 && !must_preallocate && reg_parm_stack_space > 0)
3673 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
3674
3675 /* Pass the function the address in which to return a
3676 structure value. */
3677 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
3678 {
3679 structure_value_addr
3680 = convert_memory_address (Pmode, structure_value_addr);
3681 emit_move_insn (struct_value,
3682 force_reg (Pmode,
3683 force_operand (structure_value_addr,
3684 NULL_RTX)));
3685
3686 if (REG_P (struct_value))
3687 use_reg (&call_fusage, struct_value);
3688 }
3689
3690 after_args = get_last_insn ();
3691 funexp = prepare_call_address (fndecl ? fndecl : fntype, funexp,
3692 static_chain_value, &call_fusage,
3693 reg_parm_seen, flags);
3694
3695 load_register_parameters (args, num_actuals, &call_fusage, flags,
3696 pass == 0, &sibcall_failure);
3697
3698 /* Save a pointer to the last insn before the call, so that we can
3699 later safely search backwards to find the CALL_INSN. */
3700 before_call = get_last_insn ();
3701
3702 if (pass == 1 && (return_flags & ERF_RETURNS_ARG))
3703 {
3704 int arg_nr = return_flags & ERF_RETURN_ARG_MASK;
3705 arg_nr = num_actuals - arg_nr - 1;
3706 if (arg_nr >= 0
3707 && arg_nr < num_actuals
3708 && args[arg_nr].reg
3709 && valreg
3710 && REG_P (valreg)
3711 && GET_MODE (args[arg_nr].reg) == GET_MODE (valreg))
3712 call_fusage
3713 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[arg_nr].tree_value)),
3714 gen_rtx_SET (valreg, args[arg_nr].reg),
3715 call_fusage);
3716 }
3717 /* All arguments and registers used for the call must be set up by
3718 now! */
3719
3720 /* Stack must be properly aligned now. */
3721 gcc_assert (!pass
3722 || multiple_p (stack_pointer_delta,
3723 preferred_unit_stack_boundary));
3724
3725 /* Generate the actual call instruction. */
3726 emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
3727 adjusted_args_size.constant, struct_value_size,
3728 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
3729 flags, args_so_far);
3730
3731 if (flag_ipa_ra)
3732 {
3733 rtx_call_insn *last;
3734 rtx datum = NULL_RTX;
3735 if (fndecl != NULL_TREE)
3736 {
3737 datum = XEXP (DECL_RTL (fndecl), 0);
3738 gcc_assert (datum != NULL_RTX
3739 && GET_CODE (datum) == SYMBOL_REF);
3740 }
3741 last = last_call_insn ();
3742 add_reg_note (last, REG_CALL_DECL, datum);
3743 }
3744
3745 /* If the call setup or the call itself overlaps with anything
3746 of the argument setup we probably clobbered our call address.
3747 In that case we can't do sibcalls. */
3748 if (pass == 0
3749 && check_sibcall_argument_overlap (after_args, 0, false))
3750 sibcall_failure = true;
3751
3752 /* If a non-BLKmode value is returned at the most significant end
3753 of a register, shift the register right by the appropriate amount
3754 and update VALREG accordingly. BLKmode values are handled by the
3755 group load/store machinery below. */
3756 if (!structure_value_addr
3757 && !pcc_struct_value
3758 && TYPE_MODE (rettype) != VOIDmode
3759 && TYPE_MODE (rettype) != BLKmode
3760 && REG_P (valreg)
3761 && targetm.calls.return_in_msb (rettype))
3762 {
3763 if (shift_return_value (TYPE_MODE (rettype), false, valreg))
3764 sibcall_failure = true;
3765 valreg = gen_rtx_REG (TYPE_MODE (rettype), REGNO (valreg));
3766 }
3767
3768 if (pass && (flags & ECF_MALLOC))
3769 {
3770 rtx temp = gen_reg_rtx (GET_MODE (valreg));
3771 rtx_insn *last, *insns;
3772
3773 /* The return value from a malloc-like function is a pointer. */
3774 if (TREE_CODE (rettype) == POINTER_TYPE)
3775 mark_reg_pointer (temp, MALLOC_ABI_ALIGNMENT);
3776
3777 emit_move_insn (temp, valreg);
3778
3779 /* The return value from a malloc-like function cannot alias
3780 anything else. */
3781 last = get_last_insn ();
3782 add_reg_note (last, REG_NOALIAS, temp);
3783
3784 /* Write out the sequence. */
3785 insns = get_insns ();
3786 end_sequence ();
3787 emit_insn (insns);
3788 valreg = temp;
3789 }
3790
3791 /* For calls to `setjmp', etc., inform
3792 function.cc:setjmp_warnings that it should complain if
3793 nonvolatile values are live. For functions that cannot
3794 return, inform flow that control does not fall through. */
3795
3796 if ((flags & ECF_NORETURN) || pass == 0)
3797 {
3798 /* The barrier must be emitted
3799 immediately after the CALL_INSN. Some ports emit more
3800 than just a CALL_INSN above, so we must search for it here. */
3801
3802 rtx_insn *last = get_last_insn ();
3803 while (!CALL_P (last))
3804 {
3805 last = PREV_INSN (last);
3806 /* There was no CALL_INSN? */
3807 gcc_assert (last != before_call);
3808 }
3809
3810 emit_barrier_after (last);
3811
3812 /* Stack adjustments after a noreturn call are dead code.
3813 However when NO_DEFER_POP is in effect, we must preserve
3814 stack_pointer_delta. */
3815 if (inhibit_defer_pop == 0)
3816 {
3817 stack_pointer_delta = old_stack_allocated;
3818 pending_stack_adjust = 0;
3819 }
3820 }
3821
3822 /* If value type not void, return an rtx for the value. */
3823
3824 if (TYPE_MODE (rettype) == VOIDmode
3825 || ignore)
3826 target = const0_rtx;
3827 else if (structure_value_addr)
3828 {
3829 if (target == 0 || !MEM_P (target))
3830 {
3831 target
3832 = gen_rtx_MEM (TYPE_MODE (rettype),
3833 memory_address (TYPE_MODE (rettype),
3834 structure_value_addr));
3835 set_mem_attributes (target, rettype, 1);
3836 }
3837 }
3838 else if (pcc_struct_value)
3839 {
3840 /* This is the special C++ case where we need to
3841 know what the true target was. We take care to
3842 never use this value more than once in one expression. */
3843 target = gen_rtx_MEM (TYPE_MODE (rettype),
3844 copy_to_reg (valreg));
3845 set_mem_attributes (target, rettype, 1);
3846 }
3847 /* Handle calls that return values in multiple non-contiguous locations.
3848 The Irix 6 ABI has examples of this. */
3849 else if (GET_CODE (valreg) == PARALLEL)
3850 {
3851 if (target == 0)
3852 target = emit_group_move_into_temps (valreg);
3853 else if (rtx_equal_p (target, valreg))
3854 ;
3855 else if (GET_CODE (target) == PARALLEL)
3856 /* Handle the result of a emit_group_move_into_temps
3857 call in the previous pass. */
3858 emit_group_move (target, valreg);
3859 else
3860 emit_group_store (target, valreg, rettype,
3861 int_size_in_bytes (rettype));
3862 }
3863 else if (target
3864 && GET_MODE (target) == TYPE_MODE (rettype)
3865 && GET_MODE (target) == GET_MODE (valreg))
3866 {
3867 bool may_overlap = false;
3868
3869 /* We have to copy a return value in a CLASS_LIKELY_SPILLED hard
3870 reg to a plain register. */
3871 if (!REG_P (target) || HARD_REGISTER_P (target))
3872 valreg = avoid_likely_spilled_reg (valreg);
3873
3874 /* If TARGET is a MEM in the argument area, and we have
3875 saved part of the argument area, then we can't store
3876 directly into TARGET as it may get overwritten when we
3877 restore the argument save area below. Don't work too
3878 hard though and simply force TARGET to a register if it
3879 is a MEM; the optimizer is quite likely to sort it out. */
3880 if (ACCUMULATE_OUTGOING_ARGS && pass && MEM_P (target))
3881 for (i = 0; i < num_actuals; i++)
3882 if (args[i].save_area)
3883 {
3884 may_overlap = true;
3885 break;
3886 }
3887
3888 if (may_overlap)
3889 target = copy_to_reg (valreg);
3890 else
3891 {
3892 /* TARGET and VALREG cannot be equal at this point
3893 because the latter would not have
3894 REG_FUNCTION_VALUE_P true, while the former would if
3895 it were referring to the same register.
3896
3897 If they refer to the same register, this move will be
3898 a no-op, except when function inlining is being
3899 done. */
3900 emit_move_insn (target, valreg);
3901
3902 /* If we are setting a MEM, this code must be executed.
3903 Since it is emitted after the call insn, sibcall
3904 optimization cannot be performed in that case. */
3905 if (MEM_P (target))
3906 sibcall_failure = true;
3907 }
3908 }
3909 else
3910 target = copy_to_reg (avoid_likely_spilled_reg (valreg));
3911
3912 /* If we promoted this return value, make the proper SUBREG.
3913 TARGET might be const0_rtx here, so be careful. */
3914 if (REG_P (target)
3915 && TYPE_MODE (rettype) != BLKmode
3916 && GET_MODE (target) != TYPE_MODE (rettype))
3917 {
3918 tree type = rettype;
3919 int unsignedp = TYPE_UNSIGNED (type);
3920 machine_mode ret_mode = TYPE_MODE (type);
3921 machine_mode pmode;
3922
3923 /* Ensure we promote as expected, and get the new unsignedness. */
3924 pmode = promote_function_mode (type, ret_mode, &unsignedp,
3925 funtype, 1);
3926 gcc_assert (GET_MODE (target) == pmode);
3927
3928 if (SCALAR_INT_MODE_P (pmode)
3929 && SCALAR_FLOAT_MODE_P (ret_mode)
3930 && known_gt (GET_MODE_SIZE (pmode), GET_MODE_SIZE (ret_mode)))
3931 target = convert_wider_int_to_float (ret_mode, pmode, target);
3932 else
3933 {
3934 target = gen_lowpart_SUBREG (ret_mode, target);
3935 SUBREG_PROMOTED_VAR_P (target) = 1;
3936 SUBREG_PROMOTED_SET (target, unsignedp);
3937 }
3938 }
3939
3940 /* If size of args is variable or this was a constructor call for a stack
3941 argument, restore saved stack-pointer value. */
3942
3943 if (old_stack_level)
3944 {
3945 rtx_insn *prev = get_last_insn ();
3946
3947 emit_stack_restore (SAVE_BLOCK, old_stack_level);
3948 stack_pointer_delta = old_stack_pointer_delta;
3949
3950 fixup_args_size_notes (prev, get_last_insn (), stack_pointer_delta);
3951
3952 pending_stack_adjust = old_pending_adj;
3953 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
3954 stack_arg_under_construction = old_stack_arg_under_construction;
3955 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3956 stack_usage_map = initial_stack_usage_map;
3957 stack_usage_watermark = initial_stack_usage_watermark;
3958 sibcall_failure = true;
3959 }
3960 else if (ACCUMULATE_OUTGOING_ARGS && pass)
3961 {
3962 #ifdef REG_PARM_STACK_SPACE
3963 if (save_area)
3964 restore_fixed_argument_area (save_area, argblock,
3965 high_to_save, low_to_save);
3966 #endif
3967
3968 /* If we saved any argument areas, restore them. */
3969 for (i = 0; i < num_actuals; i++)
3970 if (args[i].save_area)
3971 {
3972 machine_mode save_mode = GET_MODE (args[i].save_area);
3973 rtx stack_area
3974 = gen_rtx_MEM (save_mode,
3975 memory_address (save_mode,
3976 XEXP (args[i].stack_slot, 0)));
3977
3978 if (save_mode != BLKmode)
3979 emit_move_insn (stack_area, args[i].save_area);
3980 else
3981 emit_block_move (stack_area, args[i].save_area,
3982 (gen_int_mode
3983 (args[i].locate.size.constant, Pmode)),
3984 BLOCK_OP_CALL_PARM);
3985 }
3986
3987 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3988 stack_usage_map = initial_stack_usage_map;
3989 stack_usage_watermark = initial_stack_usage_watermark;
3990 }
3991
3992 /* If this was alloca, record the new stack level. */
3993 if (flags & ECF_MAY_BE_ALLOCA)
3994 record_new_stack_level ();
3995
3996 /* Free up storage we no longer need. */
3997 for (i = 0; i < num_actuals; ++i)
3998 free (args[i].aligned_regs);
3999
4000 targetm.calls.end_call_args (args_so_far);
4001
4002 insns = get_insns ();
4003 end_sequence ();
4004
4005 if (pass == 0)
4006 {
4007 tail_call_insns = insns;
4008
4009 /* Restore the pending stack adjustment now that we have
4010 finished generating the sibling call sequence. */
4011
4012 restore_pending_stack_adjust (&save);
4013
4014 /* Prepare arg structure for next iteration. */
4015 for (i = 0; i < num_actuals; i++)
4016 {
4017 args[i].value = 0;
4018 args[i].aligned_regs = 0;
4019 args[i].stack = 0;
4020 }
4021
4022 sbitmap_free (stored_args_map);
4023 internal_arg_pointer_exp_state.scan_start = NULL;
4024 internal_arg_pointer_exp_state.cache.release ();
4025 }
4026 else
4027 {
4028 normal_call_insns = insns;
4029
4030 /* Verify that we've deallocated all the stack we used. */
4031 gcc_assert ((flags & ECF_NORETURN)
4032 || normal_failure
4033 || known_eq (old_stack_allocated,
4034 stack_pointer_delta
4035 - pending_stack_adjust));
4036 if (normal_failure)
4037 normal_call_insns = NULL;
4038 }
4039
4040 /* If something prevents making this a sibling call,
4041 zero out the sequence. */
4042 if (sibcall_failure)
4043 tail_call_insns = NULL;
4044 else
4045 break;
4046 }
4047
4048 /* If tail call production succeeded, we need to remove REG_EQUIV notes on
4049 arguments too, as argument area is now clobbered by the call. */
4050 if (tail_call_insns)
4051 {
4052 emit_insn (tail_call_insns);
4053 crtl->tail_call_emit = true;
4054 }
4055 else
4056 {
4057 emit_insn (normal_call_insns);
4058 if (try_tail_call)
4059 /* Ideally we'd emit a message for all of the ways that it could
4060 have failed. */
4061 maybe_complain_about_tail_call (exp, _("tail call production failed"));
4062 }
4063
4064 currently_expanding_call--;
4065
4066 free (stack_usage_map_buf);
4067 free (args);
4068 return target;
4069 }
4070
4071 /* A sibling call sequence invalidates any REG_EQUIV notes made for
4072 this function's incoming arguments.
4073
4074 At the start of RTL generation we know the only REG_EQUIV notes
4075 in the rtl chain are those for incoming arguments, so we can look
4076 for REG_EQUIV notes between the start of the function and the
4077 NOTE_INSN_FUNCTION_BEG.
4078
4079 This is (slight) overkill. We could keep track of the highest
4080 argument we clobber and be more selective in removing notes, but it
4081 does not seem to be worth the effort. */
4082
4083 void
4084 fixup_tail_calls (void)
4085 {
4086 rtx_insn *insn;
4087
4088 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4089 {
4090 rtx note;
4091
4092 /* There are never REG_EQUIV notes for the incoming arguments
4093 after the NOTE_INSN_FUNCTION_BEG note, so stop if we see it. */
4094 if (NOTE_P (insn)
4095 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
4096 break;
4097
4098 note = find_reg_note (insn, REG_EQUIV, 0);
4099 if (note)
4100 remove_note (insn, note);
4101 note = find_reg_note (insn, REG_EQUIV, 0);
4102 gcc_assert (!note);
4103 }
4104 }
4105
4106 /* Traverse a list of TYPES and expand all complex types into their
4107 components. */
4108 static tree
4109 split_complex_types (tree types)
4110 {
4111 tree p;
4112
4113 /* Before allocating memory, check for the common case of no complex. */
4114 for (p = types; p; p = TREE_CHAIN (p))
4115 {
4116 tree type = TREE_VALUE (p);
4117 if (TREE_CODE (type) == COMPLEX_TYPE
4118 && targetm.calls.split_complex_arg (type))
4119 goto found;
4120 }
4121 return types;
4122
4123 found:
4124 types = copy_list (types);
4125
4126 for (p = types; p; p = TREE_CHAIN (p))
4127 {
4128 tree complex_type = TREE_VALUE (p);
4129
4130 if (TREE_CODE (complex_type) == COMPLEX_TYPE
4131 && targetm.calls.split_complex_arg (complex_type))
4132 {
4133 tree next, imag;
4134
4135 /* Rewrite complex type with component type. */
4136 TREE_VALUE (p) = TREE_TYPE (complex_type);
4137 next = TREE_CHAIN (p);
4138
4139 /* Add another component type for the imaginary part. */
4140 imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
4141 TREE_CHAIN (p) = imag;
4142 TREE_CHAIN (imag) = next;
4143
4144 /* Skip the newly created node. */
4145 p = TREE_CHAIN (p);
4146 }
4147 }
4148
4149 return types;
4150 }
4151
4152 /* Output a library call to function ORGFUN (a SYMBOL_REF rtx)
4153 for a value of mode OUTMODE,
4154 with NARGS different arguments, passed as ARGS.
4155 Store the return value if RETVAL is nonzero: store it in VALUE if
4156 VALUE is nonnull, otherwise pick a convenient location. In either
4157 case return the location of the stored value.
4158
4159 FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for
4160 `const' calls, LCT_PURE for `pure' calls, or another LCT_ value for
4161 other types of library calls. */
4162
4163 rtx
4164 emit_library_call_value_1 (int retval, rtx orgfun, rtx value,
4165 enum libcall_type fn_type,
4166 machine_mode outmode, int nargs, rtx_mode_t *args)
4167 {
4168 /* Total size in bytes of all the stack-parms scanned so far. */
4169 struct args_size args_size;
4170 /* Size of arguments before any adjustments (such as rounding). */
4171 struct args_size original_args_size;
4172 int argnum;
4173 rtx fun;
4174 /* Todo, choose the correct decl type of orgfun. Sadly this information
4175 isn't present here, so we default to native calling abi here. */
4176 tree fndecl ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
4177 tree fntype ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
4178 int count;
4179 rtx argblock = 0;
4180 CUMULATIVE_ARGS args_so_far_v;
4181 cumulative_args_t args_so_far;
4182 struct arg
4183 {
4184 rtx value;
4185 machine_mode mode;
4186 rtx reg;
4187 int partial;
4188 struct locate_and_pad_arg_data locate;
4189 rtx save_area;
4190 };
4191 struct arg *argvec;
4192 int old_inhibit_defer_pop = inhibit_defer_pop;
4193 rtx call_fusage = 0;
4194 rtx mem_value = 0;
4195 rtx valreg;
4196 bool pcc_struct_value = false;
4197 poly_int64 struct_value_size = 0;
4198 int flags;
4199 int reg_parm_stack_space = 0;
4200 poly_int64 needed;
4201 rtx_insn *before_call;
4202 bool have_push_fusage;
4203 tree tfom; /* type_for_mode (outmode, 0) */
4204
4205 #ifdef REG_PARM_STACK_SPACE
4206 /* Define the boundary of the register parm stack space that needs to be
4207 save, if any. */
4208 int low_to_save = 0, high_to_save = 0;
4209 rtx save_area = 0; /* Place that it is saved. */
4210 #endif
4211
4212 /* Size of the stack reserved for parameter registers. */
4213 unsigned int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
4214 char *initial_stack_usage_map = stack_usage_map;
4215 unsigned HOST_WIDE_INT initial_stack_usage_watermark = stack_usage_watermark;
4216 char *stack_usage_map_buf = NULL;
4217
4218 rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
4219
4220 #ifdef REG_PARM_STACK_SPACE
4221 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
4222 #endif
4223
4224 /* By default, library functions cannot throw. */
4225 flags = ECF_NOTHROW;
4226
4227 switch (fn_type)
4228 {
4229 case LCT_NORMAL:
4230 break;
4231 case LCT_CONST:
4232 flags |= ECF_CONST;
4233 break;
4234 case LCT_PURE:
4235 flags |= ECF_PURE;
4236 break;
4237 case LCT_NORETURN:
4238 flags |= ECF_NORETURN;
4239 break;
4240 case LCT_THROW:
4241 flags &= ~ECF_NOTHROW;
4242 break;
4243 case LCT_RETURNS_TWICE:
4244 flags = ECF_RETURNS_TWICE;
4245 break;
4246 }
4247 fun = orgfun;
4248
4249 /* Ensure current function's preferred stack boundary is at least
4250 what we need. */
4251 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
4252 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
4253
4254 /* If this kind of value comes back in memory,
4255 decide where in memory it should come back. */
4256 if (outmode != VOIDmode)
4257 {
4258 tfom = lang_hooks.types.type_for_mode (outmode, 0);
4259 if (aggregate_value_p (tfom, 0))
4260 {
4261 #ifdef PCC_STATIC_STRUCT_RETURN
4262 rtx pointer_reg
4263 = hard_function_value (build_pointer_type (tfom), 0, 0, 0);
4264 mem_value = gen_rtx_MEM (outmode, pointer_reg);
4265 pcc_struct_value = true;
4266 if (value == 0)
4267 value = gen_reg_rtx (outmode);
4268 #else /* not PCC_STATIC_STRUCT_RETURN */
4269 struct_value_size = GET_MODE_SIZE (outmode);
4270 if (value != 0 && MEM_P (value))
4271 mem_value = value;
4272 else
4273 mem_value = assign_temp (tfom, 1, 1);
4274 #endif
4275 /* This call returns a big structure. */
4276 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
4277 }
4278 }
4279 else
4280 tfom = void_type_node;
4281
4282 /* ??? Unfinished: must pass the memory address as an argument. */
4283
4284 /* Copy all the libcall-arguments out of the varargs data
4285 and into a vector ARGVEC.
4286
4287 Compute how to pass each argument. We only support a very small subset
4288 of the full argument passing conventions to limit complexity here since
4289 library functions shouldn't have many args. */
4290
4291 argvec = XALLOCAVEC (struct arg, nargs + 1);
4292 memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
4293
4294 #ifdef INIT_CUMULATIVE_LIBCALL_ARGS
4295 INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far_v, outmode, fun);
4296 #else
4297 INIT_CUMULATIVE_ARGS (args_so_far_v, NULL_TREE, fun, 0, nargs);
4298 #endif
4299 args_so_far = pack_cumulative_args (&args_so_far_v);
4300
4301 args_size.constant = 0;
4302 args_size.var = 0;
4303
4304 count = 0;
4305
4306 push_temp_slots ();
4307
4308 /* If there's a structure value address to be passed,
4309 either pass it in the special place, or pass it as an extra argument. */
4310 if (mem_value && struct_value == 0 && ! pcc_struct_value)
4311 {
4312 rtx addr = XEXP (mem_value, 0);
4313
4314 nargs++;
4315
4316 /* Make sure it is a reasonable operand for a move or push insn. */
4317 if (!REG_P (addr) && !MEM_P (addr)
4318 && !(CONSTANT_P (addr)
4319 && targetm.legitimate_constant_p (Pmode, addr)))
4320 addr = force_operand (addr, NULL_RTX);
4321
4322 argvec[count].value = addr;
4323 argvec[count].mode = Pmode;
4324 argvec[count].partial = 0;
4325
4326 function_arg_info ptr_arg (Pmode, /*named=*/true);
4327 argvec[count].reg = targetm.calls.function_arg (args_so_far, ptr_arg);
4328 gcc_assert (targetm.calls.arg_partial_bytes (args_so_far, ptr_arg) == 0);
4329
4330 locate_and_pad_parm (Pmode, NULL_TREE,
4331 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4332 1,
4333 #else
4334 argvec[count].reg != 0,
4335 #endif
4336 reg_parm_stack_space, 0,
4337 NULL_TREE, &args_size, &argvec[count].locate);
4338
4339 if (argvec[count].reg == 0 || argvec[count].partial != 0
4340 || reg_parm_stack_space > 0)
4341 args_size.constant += argvec[count].locate.size.constant;
4342
4343 targetm.calls.function_arg_advance (args_so_far, ptr_arg);
4344
4345 count++;
4346 }
4347
4348 for (unsigned int i = 0; count < nargs; i++, count++)
4349 {
4350 rtx val = args[i].first;
4351 function_arg_info arg (args[i].second, /*named=*/true);
4352 int unsigned_p = 0;
4353
4354 /* We cannot convert the arg value to the mode the library wants here;
4355 must do it earlier where we know the signedness of the arg. */
4356 gcc_assert (arg.mode != BLKmode
4357 && (GET_MODE (val) == arg.mode
4358 || GET_MODE (val) == VOIDmode));
4359
4360 /* Make sure it is a reasonable operand for a move or push insn. */
4361 if (!REG_P (val) && !MEM_P (val)
4362 && !(CONSTANT_P (val)
4363 && targetm.legitimate_constant_p (arg.mode, val)))
4364 val = force_operand (val, NULL_RTX);
4365
4366 if (pass_by_reference (&args_so_far_v, arg))
4367 {
4368 rtx slot;
4369 int must_copy = !reference_callee_copied (&args_so_far_v, arg);
4370
4371 /* If this was a CONST function, it is now PURE since it now
4372 reads memory. */
4373 if (flags & ECF_CONST)
4374 {
4375 flags &= ~ECF_CONST;
4376 flags |= ECF_PURE;
4377 }
4378
4379 if (MEM_P (val) && !must_copy)
4380 {
4381 tree val_expr = MEM_EXPR (val);
4382 if (val_expr)
4383 mark_addressable (val_expr);
4384 slot = val;
4385 }
4386 else
4387 {
4388 slot = assign_temp (lang_hooks.types.type_for_mode (arg.mode, 0),
4389 1, 1);
4390 emit_move_insn (slot, val);
4391 }
4392
4393 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4394 gen_rtx_USE (VOIDmode, slot),
4395 call_fusage);
4396 if (must_copy)
4397 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4398 gen_rtx_CLOBBER (VOIDmode,
4399 slot),
4400 call_fusage);
4401
4402 arg.mode = Pmode;
4403 arg.pass_by_reference = true;
4404 val = force_operand (XEXP (slot, 0), NULL_RTX);
4405 }
4406
4407 arg.mode = promote_function_mode (NULL_TREE, arg.mode, &unsigned_p,
4408 NULL_TREE, 0);
4409 argvec[count].mode = arg.mode;
4410 argvec[count].value = convert_modes (arg.mode, GET_MODE (val), val,
4411 unsigned_p);
4412 argvec[count].reg = targetm.calls.function_arg (args_so_far, arg);
4413
4414 argvec[count].partial
4415 = targetm.calls.arg_partial_bytes (args_so_far, arg);
4416
4417 if (argvec[count].reg == 0
4418 || argvec[count].partial != 0
4419 || reg_parm_stack_space > 0)
4420 {
4421 locate_and_pad_parm (arg.mode, NULL_TREE,
4422 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4423 1,
4424 #else
4425 argvec[count].reg != 0,
4426 #endif
4427 reg_parm_stack_space, argvec[count].partial,
4428 NULL_TREE, &args_size, &argvec[count].locate);
4429 args_size.constant += argvec[count].locate.size.constant;
4430 gcc_assert (!argvec[count].locate.size.var);
4431 }
4432 #ifdef BLOCK_REG_PADDING
4433 else
4434 /* The argument is passed entirely in registers. See at which
4435 end it should be padded. */
4436 argvec[count].locate.where_pad =
4437 BLOCK_REG_PADDING (arg.mode, NULL_TREE,
4438 known_le (GET_MODE_SIZE (arg.mode),
4439 UNITS_PER_WORD));
4440 #endif
4441
4442 targetm.calls.function_arg_advance (args_so_far, arg);
4443 }
4444
4445 for (int i = 0; i < nargs; i++)
4446 if (reg_parm_stack_space > 0
4447 || argvec[i].reg == 0
4448 || argvec[i].partial != 0)
4449 update_stack_alignment_for_call (&argvec[i].locate);
4450
4451 /* If this machine requires an external definition for library
4452 functions, write one out. */
4453 assemble_external_libcall (fun);
4454
4455 original_args_size = args_size;
4456 args_size.constant = (aligned_upper_bound (args_size.constant
4457 + stack_pointer_delta,
4458 STACK_BYTES)
4459 - stack_pointer_delta);
4460
4461 args_size.constant = upper_bound (args_size.constant,
4462 reg_parm_stack_space);
4463
4464 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4465 args_size.constant -= reg_parm_stack_space;
4466
4467 crtl->outgoing_args_size = upper_bound (crtl->outgoing_args_size,
4468 args_size.constant);
4469
4470 if (flag_stack_usage_info && !ACCUMULATE_OUTGOING_ARGS)
4471 {
4472 poly_int64 pushed = args_size.constant + pending_stack_adjust;
4473 current_function_pushed_stack_size
4474 = upper_bound (current_function_pushed_stack_size, pushed);
4475 }
4476
4477 if (ACCUMULATE_OUTGOING_ARGS)
4478 {
4479 /* Since the stack pointer will never be pushed, it is possible for
4480 the evaluation of a parm to clobber something we have already
4481 written to the stack. Since most function calls on RISC machines
4482 do not use the stack, this is uncommon, but must work correctly.
4483
4484 Therefore, we save any area of the stack that was already written
4485 and that we are using. Here we set up to do this by making a new
4486 stack usage map from the old one.
4487
4488 Another approach might be to try to reorder the argument
4489 evaluations to avoid this conflicting stack usage. */
4490
4491 needed = args_size.constant;
4492
4493 /* Since we will be writing into the entire argument area, the
4494 map must be allocated for its entire size, not just the part that
4495 is the responsibility of the caller. */
4496 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4497 needed += reg_parm_stack_space;
4498
4499 poly_int64 limit = needed;
4500 if (ARGS_GROW_DOWNWARD)
4501 limit += 1;
4502
4503 /* For polynomial sizes, this is the maximum possible size needed
4504 for arguments with a constant size and offset. */
4505 HOST_WIDE_INT const_limit = constant_lower_bound (limit);
4506 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
4507 const_limit);
4508
4509 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
4510 stack_usage_map = stack_usage_map_buf;
4511
4512 if (initial_highest_arg_in_use)
4513 memcpy (stack_usage_map, initial_stack_usage_map,
4514 initial_highest_arg_in_use);
4515
4516 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
4517 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
4518 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
4519 needed = 0;
4520
4521 /* We must be careful to use virtual regs before they're instantiated,
4522 and real regs afterwards. Loop optimization, for example, can create
4523 new libcalls after we've instantiated the virtual regs, and if we
4524 use virtuals anyway, they won't match the rtl patterns. */
4525
4526 if (virtuals_instantiated)
4527 argblock = plus_constant (Pmode, stack_pointer_rtx,
4528 STACK_POINTER_OFFSET);
4529 else
4530 argblock = virtual_outgoing_args_rtx;
4531 }
4532 else
4533 {
4534 if (!targetm.calls.push_argument (0))
4535 argblock = push_block (gen_int_mode (args_size.constant, Pmode), 0, 0);
4536 }
4537
4538 /* We push args individually in reverse order, perform stack alignment
4539 before the first push (the last arg). */
4540 if (argblock == 0)
4541 anti_adjust_stack (gen_int_mode (args_size.constant
4542 - original_args_size.constant,
4543 Pmode));
4544
4545 argnum = nargs - 1;
4546
4547 #ifdef REG_PARM_STACK_SPACE
4548 if (ACCUMULATE_OUTGOING_ARGS)
4549 {
4550 /* The argument list is the property of the called routine and it
4551 may clobber it. If the fixed area has been used for previous
4552 parameters, we must save and restore it. */
4553 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
4554 &low_to_save, &high_to_save);
4555 }
4556 #endif
4557
4558 rtx call_cookie
4559 = targetm.calls.function_arg (args_so_far,
4560 function_arg_info::end_marker ());
4561
4562 /* Push the args that need to be pushed. */
4563
4564 have_push_fusage = false;
4565
4566 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4567 are to be pushed. */
4568 for (count = 0; count < nargs; count++, argnum--)
4569 {
4570 machine_mode mode = argvec[argnum].mode;
4571 rtx val = argvec[argnum].value;
4572 rtx reg = argvec[argnum].reg;
4573 int partial = argvec[argnum].partial;
4574 unsigned int parm_align = argvec[argnum].locate.boundary;
4575 poly_int64 lower_bound = 0, upper_bound = 0;
4576
4577 if (! (reg != 0 && partial == 0))
4578 {
4579 rtx use;
4580
4581 if (ACCUMULATE_OUTGOING_ARGS)
4582 {
4583 /* If this is being stored into a pre-allocated, fixed-size,
4584 stack area, save any previous data at that location. */
4585
4586 if (ARGS_GROW_DOWNWARD)
4587 {
4588 /* stack_slot is negative, but we want to index stack_usage_map
4589 with positive values. */
4590 upper_bound = -argvec[argnum].locate.slot_offset.constant + 1;
4591 lower_bound = upper_bound - argvec[argnum].locate.size.constant;
4592 }
4593 else
4594 {
4595 lower_bound = argvec[argnum].locate.slot_offset.constant;
4596 upper_bound = lower_bound + argvec[argnum].locate.size.constant;
4597 }
4598
4599 if (stack_region_maybe_used_p (lower_bound, upper_bound,
4600 reg_parm_stack_space))
4601 {
4602 /* We need to make a save area. */
4603 poly_uint64 size
4604 = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
4605 machine_mode save_mode
4606 = int_mode_for_size (size, 1).else_blk ();
4607 rtx adr
4608 = plus_constant (Pmode, argblock,
4609 argvec[argnum].locate.offset.constant);
4610 rtx stack_area
4611 = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
4612
4613 if (save_mode == BLKmode)
4614 {
4615 argvec[argnum].save_area
4616 = assign_stack_temp (BLKmode,
4617 argvec[argnum].locate.size.constant
4618 );
4619
4620 emit_block_move (validize_mem
4621 (copy_rtx (argvec[argnum].save_area)),
4622 stack_area,
4623 (gen_int_mode
4624 (argvec[argnum].locate.size.constant,
4625 Pmode)),
4626 BLOCK_OP_CALL_PARM);
4627 }
4628 else
4629 {
4630 argvec[argnum].save_area = gen_reg_rtx (save_mode);
4631
4632 emit_move_insn (argvec[argnum].save_area, stack_area);
4633 }
4634 }
4635 }
4636
4637 emit_push_insn (val, mode, lang_hooks.types.type_for_mode (mode, 0),
4638 NULL_RTX, parm_align, partial, reg, 0, argblock,
4639 (gen_int_mode
4640 (argvec[argnum].locate.offset.constant, Pmode)),
4641 reg_parm_stack_space,
4642 ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad), false);
4643
4644 /* Now mark the segment we just used. */
4645 if (ACCUMULATE_OUTGOING_ARGS)
4646 mark_stack_region_used (lower_bound, upper_bound);
4647
4648 NO_DEFER_POP;
4649
4650 /* Indicate argument access so that alias.cc knows that these
4651 values are live. */
4652 if (argblock)
4653 use = plus_constant (Pmode, argblock,
4654 argvec[argnum].locate.offset.constant);
4655 else if (have_push_fusage)
4656 continue;
4657 else
4658 {
4659 /* When arguments are pushed, trying to tell alias.cc where
4660 exactly this argument is won't work, because the
4661 auto-increment causes confusion. So we merely indicate
4662 that we access something with a known mode somewhere on
4663 the stack. */
4664 use = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4665 gen_rtx_SCRATCH (Pmode));
4666 have_push_fusage = true;
4667 }
4668 use = gen_rtx_MEM (argvec[argnum].mode, use);
4669 use = gen_rtx_USE (VOIDmode, use);
4670 call_fusage = gen_rtx_EXPR_LIST (VOIDmode, use, call_fusage);
4671 }
4672 }
4673
4674 argnum = nargs - 1;
4675
4676 fun = prepare_call_address (NULL, fun, NULL, &call_fusage, 0, 0);
4677
4678 targetm.calls.start_call_args (args_so_far);
4679
4680 /* When expanding a normal call, args are stored in push order,
4681 which is the reverse of what we have here. */
4682 bool any_regs = false;
4683 for (int i = nargs; i-- > 0; )
4684 if (argvec[i].reg != NULL_RTX)
4685 {
4686 targetm.calls.call_args (args_so_far, argvec[i].reg, NULL_TREE);
4687 any_regs = true;
4688 }
4689 if (!any_regs)
4690 targetm.calls.call_args (args_so_far, pc_rtx, NULL_TREE);
4691
4692 /* Now load any reg parms into their regs. */
4693
4694 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4695 are to be pushed. */
4696 for (count = 0; count < nargs; count++, argnum--)
4697 {
4698 machine_mode mode = argvec[argnum].mode;
4699 rtx val = argvec[argnum].value;
4700 rtx reg = argvec[argnum].reg;
4701 int partial = argvec[argnum].partial;
4702
4703 /* Handle calls that pass values in multiple non-contiguous
4704 locations. The PA64 has examples of this for library calls. */
4705 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4706 emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (mode));
4707 else if (reg != 0 && partial == 0)
4708 {
4709 emit_move_insn (reg, val);
4710 #ifdef BLOCK_REG_PADDING
4711 poly_int64 size = GET_MODE_SIZE (argvec[argnum].mode);
4712
4713 /* Copied from load_register_parameters. */
4714
4715 /* Handle case where we have a value that needs shifting
4716 up to the msb. eg. a QImode value and we're padding
4717 upward on a BYTES_BIG_ENDIAN machine. */
4718 if (known_lt (size, UNITS_PER_WORD)
4719 && (argvec[argnum].locate.where_pad
4720 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)))
4721 {
4722 rtx x;
4723 poly_int64 shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
4724
4725 /* Assigning REG here rather than a temp makes CALL_FUSAGE
4726 report the whole reg as used. Strictly speaking, the
4727 call only uses SIZE bytes at the msb end, but it doesn't
4728 seem worth generating rtl to say that. */
4729 reg = gen_rtx_REG (word_mode, REGNO (reg));
4730 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
4731 if (x != reg)
4732 emit_move_insn (reg, x);
4733 }
4734 #endif
4735 }
4736
4737 NO_DEFER_POP;
4738 }
4739
4740 /* Any regs containing parms remain in use through the call. */
4741 for (count = 0; count < nargs; count++)
4742 {
4743 rtx reg = argvec[count].reg;
4744 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4745 use_group_regs (&call_fusage, reg);
4746 else if (reg != 0)
4747 {
4748 int partial = argvec[count].partial;
4749 if (partial)
4750 {
4751 int nregs;
4752 gcc_assert (partial % UNITS_PER_WORD == 0);
4753 nregs = partial / UNITS_PER_WORD;
4754 use_regs (&call_fusage, REGNO (reg), nregs);
4755 }
4756 else
4757 use_reg (&call_fusage, reg);
4758 }
4759 }
4760
4761 /* Pass the function the address in which to return a structure value. */
4762 if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
4763 {
4764 emit_move_insn (struct_value,
4765 force_reg (Pmode,
4766 force_operand (XEXP (mem_value, 0),
4767 NULL_RTX)));
4768 if (REG_P (struct_value))
4769 use_reg (&call_fusage, struct_value);
4770 }
4771
4772 /* Don't allow popping to be deferred, since then
4773 cse'ing of library calls could delete a call and leave the pop. */
4774 NO_DEFER_POP;
4775 valreg = (mem_value == 0 && outmode != VOIDmode
4776 ? hard_libcall_value (outmode, orgfun) : NULL_RTX);
4777
4778 /* Stack must be properly aligned now. */
4779 gcc_assert (multiple_p (stack_pointer_delta,
4780 PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT));
4781
4782 before_call = get_last_insn ();
4783
4784 if (flag_callgraph_info)
4785 record_final_call (SYMBOL_REF_DECL (orgfun), UNKNOWN_LOCATION);
4786
4787 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
4788 will set inhibit_defer_pop to that value. */
4789 /* The return type is needed to decide how many bytes the function pops.
4790 Signedness plays no role in that, so for simplicity, we pretend it's
4791 always signed. We also assume that the list of arguments passed has
4792 no impact, so we pretend it is unknown. */
4793
4794 emit_call_1 (fun, NULL,
4795 get_identifier (XSTR (orgfun, 0)),
4796 build_function_type (tfom, NULL_TREE),
4797 original_args_size.constant, args_size.constant,
4798 struct_value_size, call_cookie, valreg,
4799 old_inhibit_defer_pop + 1, call_fusage, flags, args_so_far);
4800
4801 if (flag_ipa_ra)
4802 {
4803 rtx datum = orgfun;
4804 gcc_assert (GET_CODE (datum) == SYMBOL_REF);
4805 rtx_call_insn *last = last_call_insn ();
4806 add_reg_note (last, REG_CALL_DECL, datum);
4807 }
4808
4809 /* Right-shift returned value if necessary. */
4810 if (!pcc_struct_value
4811 && TYPE_MODE (tfom) != BLKmode
4812 && targetm.calls.return_in_msb (tfom))
4813 {
4814 shift_return_value (TYPE_MODE (tfom), false, valreg);
4815 valreg = gen_rtx_REG (TYPE_MODE (tfom), REGNO (valreg));
4816 }
4817
4818 targetm.calls.end_call_args (args_so_far);
4819
4820 /* For calls to `setjmp', etc., inform function.cc:setjmp_warnings
4821 that it should complain if nonvolatile values are live. For
4822 functions that cannot return, inform flow that control does not
4823 fall through. */
4824 if (flags & ECF_NORETURN)
4825 {
4826 /* The barrier note must be emitted
4827 immediately after the CALL_INSN. Some ports emit more than
4828 just a CALL_INSN above, so we must search for it here. */
4829 rtx_insn *last = get_last_insn ();
4830 while (!CALL_P (last))
4831 {
4832 last = PREV_INSN (last);
4833 /* There was no CALL_INSN? */
4834 gcc_assert (last != before_call);
4835 }
4836
4837 emit_barrier_after (last);
4838 }
4839
4840 /* Consider that "regular" libcalls, i.e. all of them except for LCT_THROW
4841 and LCT_RETURNS_TWICE, cannot perform non-local gotos. */
4842 if (flags & ECF_NOTHROW)
4843 {
4844 rtx_insn *last = get_last_insn ();
4845 while (!CALL_P (last))
4846 {
4847 last = PREV_INSN (last);
4848 /* There was no CALL_INSN? */
4849 gcc_assert (last != before_call);
4850 }
4851
4852 make_reg_eh_region_note_nothrow_nononlocal (last);
4853 }
4854
4855 /* Now restore inhibit_defer_pop to its actual original value. */
4856 OK_DEFER_POP;
4857
4858 pop_temp_slots ();
4859
4860 /* Copy the value to the right place. */
4861 if (outmode != VOIDmode && retval)
4862 {
4863 if (mem_value)
4864 {
4865 if (value == 0)
4866 value = mem_value;
4867 if (value != mem_value)
4868 emit_move_insn (value, mem_value);
4869 }
4870 else if (GET_CODE (valreg) == PARALLEL)
4871 {
4872 if (value == 0)
4873 value = gen_reg_rtx (outmode);
4874 emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
4875 }
4876 else
4877 {
4878 /* Convert to the proper mode if a promotion has been active. */
4879 if (GET_MODE (valreg) != outmode)
4880 {
4881 int unsignedp = TYPE_UNSIGNED (tfom);
4882
4883 gcc_assert (promote_function_mode (tfom, outmode, &unsignedp,
4884 fndecl ? TREE_TYPE (fndecl) : fntype, 1)
4885 == GET_MODE (valreg));
4886 valreg = convert_modes (outmode, GET_MODE (valreg), valreg, 0);
4887 }
4888
4889 if (value != 0)
4890 emit_move_insn (value, valreg);
4891 else
4892 value = valreg;
4893 }
4894 }
4895
4896 if (ACCUMULATE_OUTGOING_ARGS)
4897 {
4898 #ifdef REG_PARM_STACK_SPACE
4899 if (save_area)
4900 restore_fixed_argument_area (save_area, argblock,
4901 high_to_save, low_to_save);
4902 #endif
4903
4904 /* If we saved any argument areas, restore them. */
4905 for (count = 0; count < nargs; count++)
4906 if (argvec[count].save_area)
4907 {
4908 machine_mode save_mode = GET_MODE (argvec[count].save_area);
4909 rtx adr = plus_constant (Pmode, argblock,
4910 argvec[count].locate.offset.constant);
4911 rtx stack_area = gen_rtx_MEM (save_mode,
4912 memory_address (save_mode, adr));
4913
4914 if (save_mode == BLKmode)
4915 emit_block_move (stack_area,
4916 validize_mem
4917 (copy_rtx (argvec[count].save_area)),
4918 (gen_int_mode
4919 (argvec[count].locate.size.constant, Pmode)),
4920 BLOCK_OP_CALL_PARM);
4921 else
4922 emit_move_insn (stack_area, argvec[count].save_area);
4923 }
4924
4925 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
4926 stack_usage_map = initial_stack_usage_map;
4927 stack_usage_watermark = initial_stack_usage_watermark;
4928 }
4929
4930 free (stack_usage_map_buf);
4931
4932 return value;
4933
4934 }
4935
4936
4937 /* Store a single argument for a function call
4938 into the register or memory area where it must be passed.
4939 *ARG describes the argument value and where to pass it.
4940
4941 ARGBLOCK is the address of the stack-block for all the arguments,
4942 or 0 on a machine where arguments are pushed individually.
4943
4944 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
4945 so must be careful about how the stack is used.
4946
4947 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
4948 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
4949 that we need not worry about saving and restoring the stack.
4950
4951 FNDECL is the declaration of the function we are calling.
4952
4953 Return true if this arg should cause sibcall failure,
4954 false otherwise. */
4955
4956 static bool
4957 store_one_arg (struct arg_data *arg, rtx argblock, int flags,
4958 int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
4959 {
4960 tree pval = arg->tree_value;
4961 rtx reg = 0;
4962 int partial = 0;
4963 poly_int64 used = 0;
4964 poly_int64 lower_bound = 0, upper_bound = 0;
4965 bool sibcall_failure = false;
4966
4967 if (TREE_CODE (pval) == ERROR_MARK)
4968 return true;
4969
4970 /* Push a new temporary level for any temporaries we make for
4971 this argument. */
4972 push_temp_slots ();
4973
4974 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
4975 {
4976 /* If this is being stored into a pre-allocated, fixed-size, stack area,
4977 save any previous data at that location. */
4978 if (argblock && ! variable_size && arg->stack)
4979 {
4980 if (ARGS_GROW_DOWNWARD)
4981 {
4982 /* stack_slot is negative, but we want to index stack_usage_map
4983 with positive values. */
4984 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4985 {
4986 rtx offset = XEXP (XEXP (arg->stack_slot, 0), 1);
4987 upper_bound = -rtx_to_poly_int64 (offset) + 1;
4988 }
4989 else
4990 upper_bound = 0;
4991
4992 lower_bound = upper_bound - arg->locate.size.constant;
4993 }
4994 else
4995 {
4996 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4997 {
4998 rtx offset = XEXP (XEXP (arg->stack_slot, 0), 1);
4999 lower_bound = rtx_to_poly_int64 (offset);
5000 }
5001 else
5002 lower_bound = 0;
5003
5004 upper_bound = lower_bound + arg->locate.size.constant;
5005 }
5006
5007 if (stack_region_maybe_used_p (lower_bound, upper_bound,
5008 reg_parm_stack_space))
5009 {
5010 /* We need to make a save area. */
5011 poly_uint64 size = arg->locate.size.constant * BITS_PER_UNIT;
5012 machine_mode save_mode
5013 = int_mode_for_size (size, 1).else_blk ();
5014 rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
5015 rtx stack_area = gen_rtx_MEM (save_mode, adr);
5016
5017 if (save_mode == BLKmode)
5018 {
5019 arg->save_area
5020 = assign_temp (TREE_TYPE (arg->tree_value), 1, 1);
5021 preserve_temp_slots (arg->save_area);
5022 emit_block_move (validize_mem (copy_rtx (arg->save_area)),
5023 stack_area,
5024 (gen_int_mode
5025 (arg->locate.size.constant, Pmode)),
5026 BLOCK_OP_CALL_PARM);
5027 }
5028 else
5029 {
5030 arg->save_area = gen_reg_rtx (save_mode);
5031 emit_move_insn (arg->save_area, stack_area);
5032 }
5033 }
5034 }
5035 }
5036
5037 /* If this isn't going to be placed on both the stack and in registers,
5038 set up the register and number of words. */
5039 if (! arg->pass_on_stack)
5040 {
5041 if (flags & ECF_SIBCALL)
5042 reg = arg->tail_call_reg;
5043 else
5044 reg = arg->reg;
5045 partial = arg->partial;
5046 }
5047
5048 /* Being passed entirely in a register. We shouldn't be called in
5049 this case. */
5050 gcc_assert (reg == 0 || partial != 0);
5051
5052 /* If this arg needs special alignment, don't load the registers
5053 here. */
5054 if (arg->n_aligned_regs != 0)
5055 reg = 0;
5056
5057 /* If this is being passed partially in a register, we can't evaluate
5058 it directly into its stack slot. Otherwise, we can. */
5059 if (arg->value == 0)
5060 {
5061 /* stack_arg_under_construction is nonzero if a function argument is
5062 being evaluated directly into the outgoing argument list and
5063 expand_call must take special action to preserve the argument list
5064 if it is called recursively.
5065
5066 For scalar function arguments stack_usage_map is sufficient to
5067 determine which stack slots must be saved and restored. Scalar
5068 arguments in general have pass_on_stack == false.
5069
5070 If this argument is initialized by a function which takes the
5071 address of the argument (a C++ constructor or a C function
5072 returning a BLKmode structure), then stack_usage_map is
5073 insufficient and expand_call must push the stack around the
5074 function call. Such arguments have pass_on_stack == true.
5075
5076 Note that it is always safe to set stack_arg_under_construction,
5077 but this generates suboptimal code if set when not needed. */
5078
5079 if (arg->pass_on_stack)
5080 stack_arg_under_construction++;
5081
5082 arg->value = expand_expr (pval,
5083 (partial
5084 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
5085 ? NULL_RTX : arg->stack,
5086 VOIDmode, EXPAND_STACK_PARM);
5087
5088 /* If we are promoting object (or for any other reason) the mode
5089 doesn't agree, convert the mode. */
5090
5091 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
5092 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
5093 arg->value, arg->unsignedp);
5094
5095 if (arg->pass_on_stack)
5096 stack_arg_under_construction--;
5097 }
5098
5099 /* Check for overlap with already clobbered argument area. */
5100 if ((flags & ECF_SIBCALL)
5101 && MEM_P (arg->value)
5102 && mem_might_overlap_already_clobbered_arg_p (XEXP (arg->value, 0),
5103 arg->locate.size.constant))
5104 sibcall_failure = true;
5105
5106 /* Don't allow anything left on stack from computation
5107 of argument to alloca. */
5108 if (flags & ECF_MAY_BE_ALLOCA)
5109 do_pending_stack_adjust ();
5110
5111 if (arg->value == arg->stack)
5112 /* If the value is already in the stack slot, we are done. */
5113 ;
5114 else if (arg->mode != BLKmode)
5115 {
5116 unsigned int parm_align;
5117
5118 /* Argument is a scalar, not entirely passed in registers.
5119 (If part is passed in registers, arg->partial says how much
5120 and emit_push_insn will take care of putting it there.)
5121
5122 Push it, and if its size is less than the
5123 amount of space allocated to it,
5124 also bump stack pointer by the additional space.
5125 Note that in C the default argument promotions
5126 will prevent such mismatches. */
5127
5128 poly_int64 size = (TYPE_EMPTY_P (TREE_TYPE (pval))
5129 ? 0 : GET_MODE_SIZE (arg->mode));
5130
5131 /* Compute how much space the push instruction will push.
5132 On many machines, pushing a byte will advance the stack
5133 pointer by a halfword. */
5134 #ifdef PUSH_ROUNDING
5135 size = PUSH_ROUNDING (size);
5136 #endif
5137 used = size;
5138
5139 /* Compute how much space the argument should get:
5140 round up to a multiple of the alignment for arguments. */
5141 if (targetm.calls.function_arg_padding (arg->mode, TREE_TYPE (pval))
5142 != PAD_NONE)
5143 /* At the moment we don't (need to) support ABIs for which the
5144 padding isn't known at compile time. In principle it should
5145 be easy to add though. */
5146 used = force_align_up (size, PARM_BOUNDARY / BITS_PER_UNIT);
5147
5148 /* Compute the alignment of the pushed argument. */
5149 parm_align = arg->locate.boundary;
5150 if (targetm.calls.function_arg_padding (arg->mode, TREE_TYPE (pval))
5151 == PAD_DOWNWARD)
5152 {
5153 poly_int64 pad = used - size;
5154 unsigned int pad_align = known_alignment (pad) * BITS_PER_UNIT;
5155 if (pad_align != 0)
5156 parm_align = MIN (parm_align, pad_align);
5157 }
5158
5159 /* This isn't already where we want it on the stack, so put it there.
5160 This can either be done with push or copy insns. */
5161 if (maybe_ne (used, 0)
5162 && !emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval),
5163 NULL_RTX, parm_align, partial, reg, used - size,
5164 argblock, ARGS_SIZE_RTX (arg->locate.offset),
5165 reg_parm_stack_space,
5166 ARGS_SIZE_RTX (arg->locate.alignment_pad), true))
5167 sibcall_failure = true;
5168
5169 /* Unless this is a partially-in-register argument, the argument is now
5170 in the stack. */
5171 if (partial == 0)
5172 arg->value = arg->stack;
5173 }
5174 else
5175 {
5176 /* BLKmode, at least partly to be pushed. */
5177
5178 unsigned int parm_align;
5179 poly_int64 excess;
5180 rtx size_rtx;
5181
5182 /* Pushing a nonscalar.
5183 If part is passed in registers, PARTIAL says how much
5184 and emit_push_insn will take care of putting it there. */
5185
5186 /* Round its size up to a multiple
5187 of the allocation unit for arguments. */
5188
5189 if (arg->locate.size.var != 0)
5190 {
5191 excess = 0;
5192 size_rtx = ARGS_SIZE_RTX (arg->locate.size);
5193 }
5194 else
5195 {
5196 /* PUSH_ROUNDING has no effect on us, because emit_push_insn
5197 for BLKmode is careful to avoid it. */
5198 excess = (arg->locate.size.constant
5199 - arg_int_size_in_bytes (TREE_TYPE (pval))
5200 + partial);
5201 size_rtx = expand_expr (arg_size_in_bytes (TREE_TYPE (pval)),
5202 NULL_RTX, TYPE_MODE (sizetype),
5203 EXPAND_NORMAL);
5204 }
5205
5206 parm_align = arg->locate.boundary;
5207
5208 /* When an argument is padded down, the block is aligned to
5209 PARM_BOUNDARY, but the actual argument isn't. */
5210 if (targetm.calls.function_arg_padding (arg->mode, TREE_TYPE (pval))
5211 == PAD_DOWNWARD)
5212 {
5213 if (arg->locate.size.var)
5214 parm_align = BITS_PER_UNIT;
5215 else
5216 {
5217 unsigned int excess_align
5218 = known_alignment (excess) * BITS_PER_UNIT;
5219 if (excess_align != 0)
5220 parm_align = MIN (parm_align, excess_align);
5221 }
5222 }
5223
5224 if ((flags & ECF_SIBCALL) && MEM_P (arg->value))
5225 {
5226 /* emit_push_insn might not work properly if arg->value and
5227 argblock + arg->locate.offset areas overlap. */
5228 rtx x = arg->value;
5229 poly_int64 i = 0;
5230
5231 if (strip_offset (XEXP (x, 0), &i)
5232 == crtl->args.internal_arg_pointer)
5233 {
5234 /* arg.locate doesn't contain the pretend_args_size offset,
5235 it's part of argblock. Ensure we don't count it in I. */
5236 if (STACK_GROWS_DOWNWARD)
5237 i -= crtl->args.pretend_args_size;
5238 else
5239 i += crtl->args.pretend_args_size;
5240
5241 /* expand_call should ensure this. */
5242 gcc_assert (!arg->locate.offset.var
5243 && arg->locate.size.var == 0);
5244 poly_int64 size_val = rtx_to_poly_int64 (size_rtx);
5245
5246 if (known_eq (arg->locate.offset.constant, i))
5247 {
5248 /* Even though they appear to be at the same location,
5249 if part of the outgoing argument is in registers,
5250 they aren't really at the same location. Check for
5251 this by making sure that the incoming size is the
5252 same as the outgoing size. */
5253 if (partial != 0)
5254 sibcall_failure = true;
5255 }
5256 else if (maybe_in_range_p (arg->locate.offset.constant,
5257 i, size_val))
5258 sibcall_failure = true;
5259 /* Use arg->locate.size.constant instead of size_rtx
5260 because we only care about the part of the argument
5261 on the stack. */
5262 else if (maybe_in_range_p (i, arg->locate.offset.constant,
5263 arg->locate.size.constant))
5264 sibcall_failure = true;
5265 }
5266 }
5267
5268 if (!CONST_INT_P (size_rtx) || INTVAL (size_rtx) != 0)
5269 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
5270 parm_align, partial, reg, excess, argblock,
5271 ARGS_SIZE_RTX (arg->locate.offset),
5272 reg_parm_stack_space,
5273 ARGS_SIZE_RTX (arg->locate.alignment_pad), false);
5274 /* If we bypass emit_push_insn because it is a zero sized argument,
5275 we still might need to adjust stack if such argument requires
5276 extra alignment. See PR104558. */
5277 else if ((arg->locate.alignment_pad.var
5278 || maybe_ne (arg->locate.alignment_pad.constant, 0))
5279 && !argblock)
5280 anti_adjust_stack (ARGS_SIZE_RTX (arg->locate.alignment_pad));
5281
5282 /* Unless this is a partially-in-register argument, the argument is now
5283 in the stack.
5284
5285 ??? Unlike the case above, in which we want the actual
5286 address of the data, so that we can load it directly into a
5287 register, here we want the address of the stack slot, so that
5288 it's properly aligned for word-by-word copying or something
5289 like that. It's not clear that this is always correct. */
5290 if (partial == 0)
5291 arg->value = arg->stack_slot;
5292 }
5293
5294 if (arg->reg && GET_CODE (arg->reg) == PARALLEL)
5295 {
5296 tree type = TREE_TYPE (arg->tree_value);
5297 arg->parallel_value
5298 = emit_group_load_into_temps (arg->reg, arg->value, type,
5299 int_size_in_bytes (type));
5300 }
5301
5302 /* Mark all slots this store used. */
5303 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
5304 && argblock && ! variable_size && arg->stack)
5305 mark_stack_region_used (lower_bound, upper_bound);
5306
5307 /* Once we have pushed something, pops can't safely
5308 be deferred during the rest of the arguments. */
5309 NO_DEFER_POP;
5310
5311 /* Free any temporary slots made in processing this argument. */
5312 pop_temp_slots ();
5313
5314 return sibcall_failure;
5315 }
5316
5317 /* Nonzero if we do not know how to pass ARG solely in registers. */
5318
5319 bool
5320 must_pass_in_stack_var_size (const function_arg_info &arg)
5321 {
5322 if (!arg.type)
5323 return false;
5324
5325 /* If the type has variable size... */
5326 if (!poly_int_tree_p (TYPE_SIZE (arg.type)))
5327 return true;
5328
5329 /* If the type is marked as addressable (it is required
5330 to be constructed into the stack)... */
5331 if (TREE_ADDRESSABLE (arg.type))
5332 return true;
5333
5334 return false;
5335 }
5336
5337 /* Another version of the TARGET_MUST_PASS_IN_STACK hook. This one
5338 takes trailing padding of a structure into account. */
5339 /* ??? Should be able to merge these two by examining BLOCK_REG_PADDING. */
5340
5341 bool
5342 must_pass_in_stack_var_size_or_pad (const function_arg_info &arg)
5343 {
5344 if (!arg.type)
5345 return false;
5346
5347 /* If the type has variable size... */
5348 if (TREE_CODE (TYPE_SIZE (arg.type)) != INTEGER_CST)
5349 return true;
5350
5351 /* If the type is marked as addressable (it is required
5352 to be constructed into the stack)... */
5353 if (TREE_ADDRESSABLE (arg.type))
5354 return true;
5355
5356 if (TYPE_EMPTY_P (arg.type))
5357 return false;
5358
5359 /* If the padding and mode of the type is such that a copy into
5360 a register would put it into the wrong part of the register. */
5361 if (arg.mode == BLKmode
5362 && int_size_in_bytes (arg.type) % (PARM_BOUNDARY / BITS_PER_UNIT)
5363 && (targetm.calls.function_arg_padding (arg.mode, arg.type)
5364 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)))
5365 return true;
5366
5367 return false;
5368 }
5369
5370 /* Return true if TYPE must be passed on the stack when passed to
5371 the "..." arguments of a function. */
5372
5373 bool
5374 must_pass_va_arg_in_stack (tree type)
5375 {
5376 function_arg_info arg (type, /*named=*/false);
5377 return targetm.calls.must_pass_in_stack (arg);
5378 }
5379
5380 /* Return true if FIELD is the C++17 empty base field that should
5381 be ignored for ABI calling convention decisions in order to
5382 maintain ABI compatibility between C++14 and earlier, which doesn't
5383 add this FIELD to classes with empty bases, and C++17 and later
5384 which does. */
5385
5386 bool
5387 cxx17_empty_base_field_p (const_tree field)
5388 {
5389 return (DECL_FIELD_ABI_IGNORED (field)
5390 && DECL_ARTIFICIAL (field)
5391 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
5392 && !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (field)));
5393 }