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