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