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