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