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