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