]> gcc.gnu.org Git - gcc.git/blame - gcc/calls.c
Add two test-cases for (PR middle-end/82404).
[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
4b522b8f
TV
2463/* Return the static chain for this function, if any. */
2464
2465rtx
2466rtx_for_static_chain (const_tree fndecl_or_type, bool incoming_p)
2467{
2468 if (DECL_P (fndecl_or_type) && !DECL_STATIC_CHAIN (fndecl_or_type))
2469 return NULL;
2470
2471 return targetm.calls.static_chain (fndecl_or_type, incoming_p);
2472}
2473
5275901c
JJ
2474/* Internal state for internal_arg_pointer_based_exp and its helpers. */
2475static struct
2476{
2477 /* Last insn that has been scanned by internal_arg_pointer_based_exp_scan,
2478 or NULL_RTX if none has been scanned yet. */
48810515 2479 rtx_insn *scan_start;
5275901c
JJ
2480 /* Vector indexed by REGNO - FIRST_PSEUDO_REGISTER, recording if a pseudo is
2481 based on crtl->args.internal_arg_pointer. The element is NULL_RTX if the
2482 pseudo isn't based on it, a CONST_INT offset if the pseudo is based on it
2483 with fixed offset, or PC if this is with variable or unknown offset. */
9771b263 2484 vec<rtx> cache;
5275901c
JJ
2485} internal_arg_pointer_exp_state;
2486
e9f56944 2487static rtx internal_arg_pointer_based_exp (const_rtx, bool);
5275901c
JJ
2488
2489/* Helper function for internal_arg_pointer_based_exp. Scan insns in
2490 the tail call sequence, starting with first insn that hasn't been
2491 scanned yet, and note for each pseudo on the LHS whether it is based
2492 on crtl->args.internal_arg_pointer or not, and what offset from that
2493 that pointer it has. */
2494
2495static void
2496internal_arg_pointer_based_exp_scan (void)
2497{
48810515 2498 rtx_insn *insn, *scan_start = internal_arg_pointer_exp_state.scan_start;
5275901c
JJ
2499
2500 if (scan_start == NULL_RTX)
2501 insn = get_insns ();
2502 else
2503 insn = NEXT_INSN (scan_start);
2504
2505 while (insn)
2506 {
2507 rtx set = single_set (insn);
2508 if (set && REG_P (SET_DEST (set)) && !HARD_REGISTER_P (SET_DEST (set)))
2509 {
2510 rtx val = NULL_RTX;
2511 unsigned int idx = REGNO (SET_DEST (set)) - FIRST_PSEUDO_REGISTER;
2512 /* Punt on pseudos set multiple times. */
9771b263
DN
2513 if (idx < internal_arg_pointer_exp_state.cache.length ()
2514 && (internal_arg_pointer_exp_state.cache[idx]
5275901c
JJ
2515 != NULL_RTX))
2516 val = pc_rtx;
2517 else
2518 val = internal_arg_pointer_based_exp (SET_SRC (set), false);
2519 if (val != NULL_RTX)
2520 {
9771b263 2521 if (idx >= internal_arg_pointer_exp_state.cache.length ())
c3284718
RS
2522 internal_arg_pointer_exp_state.cache
2523 .safe_grow_cleared (idx + 1);
9771b263 2524 internal_arg_pointer_exp_state.cache[idx] = val;
5275901c
JJ
2525 }
2526 }
2527 if (NEXT_INSN (insn) == NULL_RTX)
2528 scan_start = insn;
2529 insn = NEXT_INSN (insn);
2530 }
2531
2532 internal_arg_pointer_exp_state.scan_start = scan_start;
2533}
2534
5275901c
JJ
2535/* Compute whether RTL is based on crtl->args.internal_arg_pointer. Return
2536 NULL_RTX if RTL isn't based on it, a CONST_INT offset if RTL is based on
2537 it with fixed offset, or PC if this is with variable or unknown offset.
2538 TOPLEVEL is true if the function is invoked at the topmost level. */
2539
2540static rtx
e9f56944 2541internal_arg_pointer_based_exp (const_rtx rtl, bool toplevel)
5275901c
JJ
2542{
2543 if (CONSTANT_P (rtl))
2544 return NULL_RTX;
2545
2546 if (rtl == crtl->args.internal_arg_pointer)
2547 return const0_rtx;
2548
2549 if (REG_P (rtl) && HARD_REGISTER_P (rtl))
2550 return NULL_RTX;
2551
2552 if (GET_CODE (rtl) == PLUS && CONST_INT_P (XEXP (rtl, 1)))
2553 {
2554 rtx val = internal_arg_pointer_based_exp (XEXP (rtl, 0), toplevel);
2555 if (val == NULL_RTX || val == pc_rtx)
2556 return val;
0a81f074 2557 return plus_constant (Pmode, val, INTVAL (XEXP (rtl, 1)));
5275901c
JJ
2558 }
2559
2560 /* When called at the topmost level, scan pseudo assignments in between the
2561 last scanned instruction in the tail call sequence and the latest insn
2562 in that sequence. */
2563 if (toplevel)
2564 internal_arg_pointer_based_exp_scan ();
2565
2566 if (REG_P (rtl))
2567 {
2568 unsigned int idx = REGNO (rtl) - FIRST_PSEUDO_REGISTER;
9771b263
DN
2569 if (idx < internal_arg_pointer_exp_state.cache.length ())
2570 return internal_arg_pointer_exp_state.cache[idx];
5275901c
JJ
2571
2572 return NULL_RTX;
2573 }
2574
e9f56944
RS
2575 subrtx_iterator::array_type array;
2576 FOR_EACH_SUBRTX (iter, array, rtl, NONCONST)
2577 {
2578 const_rtx x = *iter;
2579 if (REG_P (x) && internal_arg_pointer_based_exp (x, false) != NULL_RTX)
2580 return pc_rtx;
2581 if (MEM_P (x))
2582 iter.skip_subrtxes ();
2583 }
5275901c
JJ
2584
2585 return NULL_RTX;
2586}
2587
07eef816
KH
2588/* Return true if and only if SIZE storage units (usually bytes)
2589 starting from address ADDR overlap with already clobbered argument
2590 area. This function is used to determine if we should give up a
2591 sibcall. */
2592
2593static bool
2594mem_overlaps_already_clobbered_arg_p (rtx addr, unsigned HOST_WIDE_INT size)
2595{
2596 HOST_WIDE_INT i;
5275901c 2597 rtx val;
07eef816 2598
f61e445a 2599 if (bitmap_empty_p (stored_args_map))
4189fb53 2600 return false;
5275901c
JJ
2601 val = internal_arg_pointer_based_exp (addr, true);
2602 if (val == NULL_RTX)
2603 return false;
2604 else if (val == pc_rtx)
6c3cb698 2605 return true;
07eef816 2606 else
5275901c 2607 i = INTVAL (val);
76e048a8
KT
2608
2609 if (STACK_GROWS_DOWNWARD)
2610 i -= crtl->args.pretend_args_size;
2611 else
2612 i += crtl->args.pretend_args_size;
2613
07eef816 2614
6dad9361
TS
2615 if (ARGS_GROW_DOWNWARD)
2616 i = -i - size;
2617
07eef816
KH
2618 if (size > 0)
2619 {
2620 unsigned HOST_WIDE_INT k;
2621
2622 for (k = 0; k < size; k++)
5829cc0f 2623 if (i + k < SBITMAP_SIZE (stored_args_map)
d7c028c0 2624 && bitmap_bit_p (stored_args_map, i + k))
07eef816
KH
2625 return true;
2626 }
2627
2628 return false;
2629}
2630
21a3b983
JL
2631/* Do the register loads required for any wholly-register parms or any
2632 parms which are passed both on the stack and in a register. Their
f725a3ec 2633 expressions were already evaluated.
21a3b983
JL
2634
2635 Mark all register-parms as living through the call, putting these USE
d329e058
AJ
2636 insns in the CALL_INSN_FUNCTION_USAGE field.
2637
40b0345d 2638 When IS_SIBCALL, perform the check_sibcall_argument_overlap
0cdca92b 2639 checking, setting *SIBCALL_FAILURE if appropriate. */
21a3b983
JL
2640
2641static void
d329e058
AJ
2642load_register_parameters (struct arg_data *args, int num_actuals,
2643 rtx *call_fusage, int flags, int is_sibcall,
2644 int *sibcall_failure)
21a3b983
JL
2645{
2646 int i, j;
2647
21a3b983 2648 for (i = 0; i < num_actuals; i++)
21a3b983 2649 {
099e9712
JH
2650 rtx reg = ((flags & ECF_SIBCALL)
2651 ? args[i].tail_call_reg : args[i].reg);
21a3b983
JL
2652 if (reg)
2653 {
6e985040
AM
2654 int partial = args[i].partial;
2655 int nregs;
2656 int size = 0;
48810515 2657 rtx_insn *before_arg = get_last_insn ();
f0078f86
AM
2658 /* Set non-negative if we must move a word at a time, even if
2659 just one word (e.g, partial == 4 && mode == DFmode). Set
2660 to -1 if we just use a normal move insn. This value can be
2661 zero if the argument is a zero size structure. */
6e985040 2662 nregs = -1;
78a52f11
RH
2663 if (GET_CODE (reg) == PARALLEL)
2664 ;
2665 else if (partial)
2666 {
2667 gcc_assert (partial % UNITS_PER_WORD == 0);
2668 nregs = partial / UNITS_PER_WORD;
2669 }
6e985040
AM
2670 else if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode)
2671 {
2672 size = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
2673 nregs = (size + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
2674 }
2675 else
2676 size = GET_MODE_SIZE (args[i].mode);
21a3b983
JL
2677
2678 /* Handle calls that pass values in multiple non-contiguous
2679 locations. The Irix 6 ABI has examples of this. */
2680
2681 if (GET_CODE (reg) == PARALLEL)
8df3dbb7 2682 emit_group_move (reg, args[i].parallel_value);
21a3b983
JL
2683
2684 /* If simple case, just do move. If normal partial, store_one_arg
2685 has already loaded the register for us. In all other cases,
2686 load the register(s) from memory. */
2687
9206d736
AM
2688 else if (nregs == -1)
2689 {
2690 emit_move_insn (reg, args[i].value);
6e985040 2691#ifdef BLOCK_REG_PADDING
9206d736
AM
2692 /* Handle case where we have a value that needs shifting
2693 up to the msb. eg. a QImode value and we're padding
2694 upward on a BYTES_BIG_ENDIAN machine. */
2695 if (size < UNITS_PER_WORD
2696 && (args[i].locate.where_pad
76b0cbf8 2697 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)))
9206d736 2698 {
9206d736
AM
2699 rtx x;
2700 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
980f6e8e
AM
2701
2702 /* Assigning REG here rather than a temp makes CALL_FUSAGE
2703 report the whole reg as used. Strictly speaking, the
2704 call only uses SIZE bytes at the msb end, but it doesn't
2705 seem worth generating rtl to say that. */
2706 reg = gen_rtx_REG (word_mode, REGNO (reg));
eb6c3df1 2707 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
980f6e8e
AM
2708 if (x != reg)
2709 emit_move_insn (reg, x);
9206d736 2710 }
6e985040 2711#endif
9206d736 2712 }
21a3b983
JL
2713
2714 /* If we have pre-computed the values to put in the registers in
2715 the case of non-aligned structures, copy them in now. */
2716
2717 else if (args[i].n_aligned_regs != 0)
2718 for (j = 0; j < args[i].n_aligned_regs; j++)
2719 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
2720 args[i].aligned_regs[j]);
2721
3b2ee170 2722 else if (partial == 0 || args[i].pass_on_stack)
6e985040 2723 {
1a8cb155 2724 rtx mem = validize_mem (copy_rtx (args[i].value));
6e985040 2725
3b2ee170
IS
2726 /* Check for overlap with already clobbered argument area,
2727 providing that this has non-zero size. */
07eef816 2728 if (is_sibcall
07c10d8f
JM
2729 && size != 0
2730 && (mem_overlaps_already_clobbered_arg_p
2731 (XEXP (args[i].value, 0), size)))
07eef816
KH
2732 *sibcall_failure = 1;
2733
984b2054
AM
2734 if (size % UNITS_PER_WORD == 0
2735 || MEM_ALIGN (mem) % BITS_PER_WORD == 0)
2736 move_block_to_reg (REGNO (reg), mem, nregs, args[i].mode);
2737 else
2738 {
2739 if (nregs > 1)
2740 move_block_to_reg (REGNO (reg), mem, nregs - 1,
2741 args[i].mode);
2742 rtx dest = gen_rtx_REG (word_mode, REGNO (reg) + nregs - 1);
2743 unsigned int bitoff = (nregs - 1) * BITS_PER_WORD;
2744 unsigned int bitsize = size * BITS_PER_UNIT - bitoff;
ee45a32d 2745 rtx x = extract_bit_field (mem, bitsize, bitoff, 1, dest,
f96bf49a
JW
2746 word_mode, word_mode, false,
2747 NULL);
984b2054
AM
2748 if (BYTES_BIG_ENDIAN)
2749 x = expand_shift (LSHIFT_EXPR, word_mode, x,
2750 BITS_PER_WORD - bitsize, dest, 1);
2751 if (x != dest)
2752 emit_move_insn (dest, x);
2753 }
2754
6e985040 2755 /* Handle a BLKmode that needs shifting. */
9206d736 2756 if (nregs == 1 && size < UNITS_PER_WORD
03ca1672 2757#ifdef BLOCK_REG_PADDING
76b0cbf8 2758 && args[i].locate.where_pad == PAD_DOWNWARD
03ca1672
UW
2759#else
2760 && BYTES_BIG_ENDIAN
2761#endif
984b2054 2762 )
6e985040 2763 {
984b2054 2764 rtx dest = gen_rtx_REG (word_mode, REGNO (reg));
6e985040 2765 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
984b2054
AM
2766 enum tree_code dir = (BYTES_BIG_ENDIAN
2767 ? RSHIFT_EXPR : LSHIFT_EXPR);
2768 rtx x;
6e985040 2769
984b2054
AM
2770 x = expand_shift (dir, word_mode, dest, shift, dest, 1);
2771 if (x != dest)
2772 emit_move_insn (dest, x);
6e985040 2773 }
6e985040 2774 }
21a3b983 2775
0cdca92b
DJ
2776 /* When a parameter is a block, and perhaps in other cases, it is
2777 possible that it did a load from an argument slot that was
32dd366d 2778 already clobbered. */
0cdca92b
DJ
2779 if (is_sibcall
2780 && check_sibcall_argument_overlap (before_arg, &args[i], 0))
2781 *sibcall_failure = 1;
2782
21a3b983
JL
2783 /* Handle calls that pass values in multiple non-contiguous
2784 locations. The Irix 6 ABI has examples of this. */
2785 if (GET_CODE (reg) == PARALLEL)
2786 use_group_regs (call_fusage, reg);
2787 else if (nregs == -1)
7d810276
JJ
2788 use_reg_mode (call_fusage, reg,
2789 TYPE_MODE (TREE_TYPE (args[i].tree_value)));
faa00334
AO
2790 else if (nregs > 0)
2791 use_regs (call_fusage, REGNO (reg), nregs);
21a3b983
JL
2792 }
2793 }
2794}
2795
739fb049
MM
2796/* We need to pop PENDING_STACK_ADJUST bytes. But, if the arguments
2797 wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
2798 bytes, then we would need to push some additional bytes to pad the
ce48579b
RH
2799 arguments. So, we compute an adjust to the stack pointer for an
2800 amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
2801 bytes. Then, when the arguments are pushed the stack will be perfectly
2802 aligned. ARGS_SIZE->CONSTANT is set to the number of bytes that should
2803 be popped after the call. Returns the adjustment. */
739fb049 2804
ce48579b 2805static int
d329e058
AJ
2806combine_pending_stack_adjustment_and_call (int unadjusted_args_size,
2807 struct args_size *args_size,
95899b34 2808 unsigned int preferred_unit_stack_boundary)
739fb049
MM
2809{
2810 /* The number of bytes to pop so that the stack will be
2811 under-aligned by UNADJUSTED_ARGS_SIZE bytes. */
2812 HOST_WIDE_INT adjustment;
2813 /* The alignment of the stack after the arguments are pushed, if we
2814 just pushed the arguments without adjust the stack here. */
95899b34 2815 unsigned HOST_WIDE_INT unadjusted_alignment;
739fb049 2816
f725a3ec 2817 unadjusted_alignment
739fb049
MM
2818 = ((stack_pointer_delta + unadjusted_args_size)
2819 % preferred_unit_stack_boundary);
2820
2821 /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
2822 as possible -- leaving just enough left to cancel out the
2823 UNADJUSTED_ALIGNMENT. In other words, we want to ensure that the
2824 PENDING_STACK_ADJUST is non-negative, and congruent to
2825 -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY. */
2826
2827 /* Begin by trying to pop all the bytes. */
f725a3ec
KH
2828 unadjusted_alignment
2829 = (unadjusted_alignment
739fb049
MM
2830 - (pending_stack_adjust % preferred_unit_stack_boundary));
2831 adjustment = pending_stack_adjust;
2832 /* Push enough additional bytes that the stack will be aligned
2833 after the arguments are pushed. */
0aae1572
NS
2834 if (preferred_unit_stack_boundary > 1 && unadjusted_alignment)
2835 adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
f725a3ec 2836
739fb049
MM
2837 /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
2838 bytes after the call. The right number is the entire
2839 PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
2840 by the arguments in the first place. */
f725a3ec 2841 args_size->constant
739fb049
MM
2842 = pending_stack_adjust - adjustment + unadjusted_args_size;
2843
ce48579b 2844 return adjustment;
739fb049
MM
2845}
2846
c67846f2
JJ
2847/* Scan X expression if it does not dereference any argument slots
2848 we already clobbered by tail call arguments (as noted in stored_args_map
2849 bitmap).
da7d8304 2850 Return nonzero if X expression dereferences such argument slots,
c67846f2
JJ
2851 zero otherwise. */
2852
2853static int
d329e058 2854check_sibcall_argument_overlap_1 (rtx x)
c67846f2
JJ
2855{
2856 RTX_CODE code;
2857 int i, j;
c67846f2
JJ
2858 const char *fmt;
2859
2860 if (x == NULL_RTX)
2861 return 0;
2862
2863 code = GET_CODE (x);
2864
6c3cb698
KY
2865 /* We need not check the operands of the CALL expression itself. */
2866 if (code == CALL)
2867 return 0;
2868
c67846f2 2869 if (code == MEM)
07eef816
KH
2870 return mem_overlaps_already_clobbered_arg_p (XEXP (x, 0),
2871 GET_MODE_SIZE (GET_MODE (x)));
c67846f2 2872
f725a3ec 2873 /* Scan all subexpressions. */
c67846f2
JJ
2874 fmt = GET_RTX_FORMAT (code);
2875 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2876 {
2877 if (*fmt == 'e')
f725a3ec
KH
2878 {
2879 if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
2880 return 1;
2881 }
c67846f2 2882 else if (*fmt == 'E')
f725a3ec
KH
2883 {
2884 for (j = 0; j < XVECLEN (x, i); j++)
2885 if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
2886 return 1;
2887 }
c67846f2
JJ
2888 }
2889 return 0;
c67846f2
JJ
2890}
2891
2892/* Scan sequence after INSN if it does not dereference any argument slots
2893 we already clobbered by tail call arguments (as noted in stored_args_map
0cdca92b
DJ
2894 bitmap). If MARK_STORED_ARGS_MAP, add stack slots for ARG to
2895 stored_args_map bitmap afterwards (when ARG is a register MARK_STORED_ARGS_MAP
2896 should be 0). Return nonzero if sequence after INSN dereferences such argument
2897 slots, zero otherwise. */
c67846f2
JJ
2898
2899static int
48810515
DM
2900check_sibcall_argument_overlap (rtx_insn *insn, struct arg_data *arg,
2901 int mark_stored_args_map)
f725a3ec 2902{
c67846f2
JJ
2903 int low, high;
2904
2905 if (insn == NULL_RTX)
2906 insn = get_insns ();
2907 else
2908 insn = NEXT_INSN (insn);
2909
2910 for (; insn; insn = NEXT_INSN (insn))
f725a3ec
KH
2911 if (INSN_P (insn)
2912 && check_sibcall_argument_overlap_1 (PATTERN (insn)))
c67846f2
JJ
2913 break;
2914
0cdca92b
DJ
2915 if (mark_stored_args_map)
2916 {
6dad9361
TS
2917 if (ARGS_GROW_DOWNWARD)
2918 low = -arg->locate.slot_offset.constant - arg->locate.size.constant;
2919 else
2920 low = arg->locate.slot_offset.constant;
d60eab50 2921
e7949876 2922 for (high = low + arg->locate.size.constant; low < high; low++)
d7c028c0 2923 bitmap_set_bit (stored_args_map, low);
0cdca92b 2924 }
c67846f2
JJ
2925 return insn != NULL_RTX;
2926}
2927
bef5d8b6
RS
2928/* Given that a function returns a value of mode MODE at the most
2929 significant end of hard register VALUE, shift VALUE left or right
2930 as specified by LEFT_P. Return true if some action was needed. */
c988af2b 2931
bef5d8b6 2932bool
ef4bddc2 2933shift_return_value (machine_mode mode, bool left_p, rtx value)
c988af2b 2934{
bef5d8b6
RS
2935 HOST_WIDE_INT shift;
2936
2937 gcc_assert (REG_P (value) && HARD_REGISTER_P (value));
b4ddce36 2938 shift = GET_MODE_BITSIZE (GET_MODE (value)) - GET_MODE_BITSIZE (mode);
bef5d8b6
RS
2939 if (shift == 0)
2940 return false;
2941
2942 /* Use ashr rather than lshr for right shifts. This is for the benefit
2943 of the MIPS port, which requires SImode values to be sign-extended
2944 when stored in 64-bit registers. */
b4ddce36
RS
2945 if (!force_expand_binop (GET_MODE (value), left_p ? ashl_optab : ashr_optab,
2946 value, GEN_INT (shift), value, 1, OPTAB_WIDEN))
bef5d8b6
RS
2947 gcc_unreachable ();
2948 return true;
c988af2b
RS
2949}
2950
3fb30019
RS
2951/* If X is a likely-spilled register value, copy it to a pseudo
2952 register and return that register. Return X otherwise. */
2953
2954static rtx
2955avoid_likely_spilled_reg (rtx x)
2956{
82d6e6fc 2957 rtx new_rtx;
3fb30019
RS
2958
2959 if (REG_P (x)
2960 && HARD_REGISTER_P (x)
07b8f0a8 2961 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (x))))
3fb30019
RS
2962 {
2963 /* Make sure that we generate a REG rather than a CONCAT.
2964 Moves into CONCATs can need nontrivial instructions,
2965 and the whole point of this function is to avoid
2966 using the hard register directly in such a situation. */
2967 generating_concat_p = 0;
82d6e6fc 2968 new_rtx = gen_reg_rtx (GET_MODE (x));
3fb30019 2969 generating_concat_p = 1;
82d6e6fc
KG
2970 emit_move_insn (new_rtx, x);
2971 return new_rtx;
3fb30019
RS
2972 }
2973 return x;
2974}
2975
b40d90e6
DM
2976/* Helper function for expand_call.
2977 Return false is EXP is not implementable as a sibling call. */
2978
2979static bool
2980can_implement_as_sibling_call_p (tree exp,
2981 rtx structure_value_addr,
2982 tree funtype,
dfbdde16 2983 int reg_parm_stack_space ATTRIBUTE_UNUSED,
b40d90e6
DM
2984 tree fndecl,
2985 int flags,
2986 tree addr,
2987 const args_size &args_size)
2988{
2989 if (!targetm.have_sibcall_epilogue ())
9a385c2d
DM
2990 {
2991 maybe_complain_about_tail_call
2992 (exp,
2993 "machine description does not have"
2994 " a sibcall_epilogue instruction pattern");
2995 return false;
2996 }
b40d90e6
DM
2997
2998 /* Doing sibling call optimization needs some work, since
2999 structure_value_addr can be allocated on the stack.
3000 It does not seem worth the effort since few optimizable
3001 sibling calls will return a structure. */
3002 if (structure_value_addr != NULL_RTX)
9a385c2d
DM
3003 {
3004 maybe_complain_about_tail_call (exp, "callee returns a structure");
3005 return false;
3006 }
b40d90e6
DM
3007
3008#ifdef REG_PARM_STACK_SPACE
3009 /* If outgoing reg parm stack space changes, we can not do sibcall. */
3010 if (OUTGOING_REG_PARM_STACK_SPACE (funtype)
3011 != OUTGOING_REG_PARM_STACK_SPACE (TREE_TYPE (current_function_decl))
3012 || (reg_parm_stack_space != REG_PARM_STACK_SPACE (current_function_decl)))
9a385c2d
DM
3013 {
3014 maybe_complain_about_tail_call (exp,
3015 "inconsistent size of stack space"
3016 " allocated for arguments which are"
3017 " passed in registers");
3018 return false;
3019 }
b40d90e6
DM
3020#endif
3021
3022 /* Check whether the target is able to optimize the call
3023 into a sibcall. */
3024 if (!targetm.function_ok_for_sibcall (fndecl, exp))
9a385c2d
DM
3025 {
3026 maybe_complain_about_tail_call (exp,
3027 "target is not able to optimize the"
3028 " call into a sibling call");
3029 return false;
3030 }
b40d90e6
DM
3031
3032 /* Functions that do not return exactly once may not be sibcall
3033 optimized. */
9a385c2d
DM
3034 if (flags & ECF_RETURNS_TWICE)
3035 {
3036 maybe_complain_about_tail_call (exp, "callee returns twice");
3037 return false;
3038 }
3039 if (flags & ECF_NORETURN)
3040 {
3041 maybe_complain_about_tail_call (exp, "callee does not return");
3042 return false;
3043 }
b40d90e6
DM
3044
3045 if (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (addr))))
9a385c2d
DM
3046 {
3047 maybe_complain_about_tail_call (exp, "volatile function type");
3048 return false;
3049 }
b40d90e6
DM
3050
3051 /* If the called function is nested in the current one, it might access
3052 some of the caller's arguments, but could clobber them beforehand if
3053 the argument areas are shared. */
3054 if (fndecl && decl_function_context (fndecl) == current_function_decl)
9a385c2d
DM
3055 {
3056 maybe_complain_about_tail_call (exp, "nested function");
3057 return false;
3058 }
b40d90e6
DM
3059
3060 /* If this function requires more stack slots than the current
3061 function, we cannot change it into a sibling call.
3062 crtl->args.pretend_args_size is not part of the
3063 stack allocated by our caller. */
3064 if (args_size.constant > (crtl->args.size - crtl->args.pretend_args_size))
9a385c2d
DM
3065 {
3066 maybe_complain_about_tail_call (exp,
3067 "callee required more stack slots"
3068 " than the caller");
3069 return false;
3070 }
b40d90e6
DM
3071
3072 /* If the callee pops its own arguments, then it must pop exactly
3073 the same number of arguments as the current function. */
3074 if (targetm.calls.return_pops_args (fndecl, funtype, args_size.constant)
3075 != targetm.calls.return_pops_args (current_function_decl,
3076 TREE_TYPE (current_function_decl),
3077 crtl->args.size))
9a385c2d
DM
3078 {
3079 maybe_complain_about_tail_call (exp,
3080 "inconsistent number of"
3081 " popped arguments");
3082 return false;
3083 }
b40d90e6
DM
3084
3085 if (!lang_hooks.decls.ok_for_sibcall (fndecl))
9a385c2d
DM
3086 {
3087 maybe_complain_about_tail_call (exp, "frontend does not support"
3088 " sibling call");
3089 return false;
3090 }
b40d90e6
DM
3091
3092 /* All checks passed. */
3093 return true;
3094}
3095
5039610b 3096/* Generate all the code for a CALL_EXPR exp
51bbfa0c
RS
3097 and return an rtx for its value.
3098 Store the value in TARGET (specified as an rtx) if convenient.
3099 If the value is stored in TARGET then TARGET is returned.
3100 If IGNORE is nonzero, then we ignore the value of the function call. */
3101
3102rtx
d329e058 3103expand_call (tree exp, rtx target, int ignore)
51bbfa0c 3104{
0a1c58a2
JL
3105 /* Nonzero if we are currently expanding a call. */
3106 static int currently_expanding_call = 0;
3107
51bbfa0c
RS
3108 /* RTX for the function to be called. */
3109 rtx funexp;
0a1c58a2 3110 /* Sequence of insns to perform a normal "call". */
48810515 3111 rtx_insn *normal_call_insns = NULL;
6de9cd9a 3112 /* Sequence of insns to perform a tail "call". */
48810515 3113 rtx_insn *tail_call_insns = NULL;
51bbfa0c
RS
3114 /* Data type of the function. */
3115 tree funtype;
ded9bf77 3116 tree type_arg_types;
28ed065e 3117 tree rettype;
51bbfa0c
RS
3118 /* Declaration of the function being called,
3119 or 0 if the function is computed (not known by name). */
3120 tree fndecl = 0;
57782ad8
MM
3121 /* The type of the function being called. */
3122 tree fntype;
6de9cd9a 3123 bool try_tail_call = CALL_EXPR_TAILCALL (exp);
9a385c2d 3124 bool must_tail_call = CALL_EXPR_MUST_TAIL_CALL (exp);
0a1c58a2 3125 int pass;
51bbfa0c
RS
3126
3127 /* Register in which non-BLKmode value will be returned,
3128 or 0 if no value or if value is BLKmode. */
3129 rtx valreg;
d5e254e1
IE
3130 /* Register(s) in which bounds are returned. */
3131 rtx valbnd = NULL;
51bbfa0c
RS
3132 /* Address where we should return a BLKmode value;
3133 0 if value not BLKmode. */
3134 rtx structure_value_addr = 0;
3135 /* Nonzero if that address is being passed by treating it as
3136 an extra, implicit first parameter. Otherwise,
3137 it is passed by being copied directly into struct_value_rtx. */
3138 int structure_value_addr_parm = 0;
078a18a4
SL
3139 /* Holds the value of implicit argument for the struct value. */
3140 tree structure_value_addr_value = NULL_TREE;
51bbfa0c
RS
3141 /* Size of aggregate value wanted, or zero if none wanted
3142 or if we are using the non-reentrant PCC calling convention
3143 or expecting the value in registers. */
e5e809f4 3144 HOST_WIDE_INT struct_value_size = 0;
51bbfa0c
RS
3145 /* Nonzero if called function returns an aggregate in memory PCC style,
3146 by returning the address of where to find it. */
3147 int pcc_struct_value = 0;
61f71b34 3148 rtx struct_value = 0;
51bbfa0c
RS
3149
3150 /* Number of actual parameters in this call, including struct value addr. */
3151 int num_actuals;
3152 /* Number of named args. Args after this are anonymous ones
3153 and they must all go on the stack. */
3154 int n_named_args;
078a18a4
SL
3155 /* Number of complex actual arguments that need to be split. */
3156 int num_complex_actuals = 0;
51bbfa0c
RS
3157
3158 /* Vector of information about each argument.
3159 Arguments are numbered in the order they will be pushed,
3160 not the order they are written. */
3161 struct arg_data *args;
3162
3163 /* Total size in bytes of all the stack-parms scanned so far. */
3164 struct args_size args_size;
099e9712 3165 struct args_size adjusted_args_size;
51bbfa0c 3166 /* Size of arguments before any adjustments (such as rounding). */
599f37b6 3167 int unadjusted_args_size;
51bbfa0c 3168 /* Data on reg parms scanned so far. */
d5cc9181
JR
3169 CUMULATIVE_ARGS args_so_far_v;
3170 cumulative_args_t args_so_far;
51bbfa0c
RS
3171 /* Nonzero if a reg parm has been scanned. */
3172 int reg_parm_seen;
efd65a8b 3173 /* Nonzero if this is an indirect function call. */
51bbfa0c 3174
f725a3ec 3175 /* Nonzero if we must avoid push-insns in the args for this call.
51bbfa0c
RS
3176 If stack space is allocated for register parameters, but not by the
3177 caller, then it is preallocated in the fixed part of the stack frame.
3178 So the entire argument block must then be preallocated (i.e., we
3179 ignore PUSH_ROUNDING in that case). */
3180
f73ad30e 3181 int must_preallocate = !PUSH_ARGS;
51bbfa0c 3182
f72aed24 3183 /* Size of the stack reserved for parameter registers. */
6f90e075
JW
3184 int reg_parm_stack_space = 0;
3185
51bbfa0c
RS
3186 /* Address of space preallocated for stack parms
3187 (on machines that lack push insns), or 0 if space not preallocated. */
3188 rtx argblock = 0;
3189
e384e6b5 3190 /* Mask of ECF_ and ERF_ flags. */
f2d33f13 3191 int flags = 0;
e384e6b5 3192 int return_flags = 0;
f73ad30e 3193#ifdef REG_PARM_STACK_SPACE
51bbfa0c 3194 /* Define the boundary of the register parm stack space that needs to be
b820d2b8
AM
3195 saved, if any. */
3196 int low_to_save, high_to_save;
51bbfa0c
RS
3197 rtx save_area = 0; /* Place that it is saved */
3198#endif
3199
51bbfa0c
RS
3200 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
3201 char *initial_stack_usage_map = stack_usage_map;
d9725c41 3202 char *stack_usage_map_buf = NULL;
51bbfa0c 3203
38afb23f
OH
3204 int old_stack_allocated;
3205
3206 /* State variables to track stack modifications. */
51bbfa0c 3207 rtx old_stack_level = 0;
38afb23f 3208 int old_stack_arg_under_construction = 0;
79be3418 3209 int old_pending_adj = 0;
51bbfa0c 3210 int old_inhibit_defer_pop = inhibit_defer_pop;
38afb23f
OH
3211
3212 /* Some stack pointer alterations we make are performed via
3213 allocate_dynamic_stack_space. This modifies the stack_pointer_delta,
3214 which we then also need to save/restore along the way. */
a259f218 3215 int old_stack_pointer_delta = 0;
38afb23f 3216
0a1c58a2 3217 rtx call_fusage;
5039610b 3218 tree addr = CALL_EXPR_FN (exp);
b3694847 3219 int i;
739fb049 3220 /* The alignment of the stack, in bits. */
95899b34 3221 unsigned HOST_WIDE_INT preferred_stack_boundary;
739fb049 3222 /* The alignment of the stack, in bytes. */
95899b34 3223 unsigned HOST_WIDE_INT preferred_unit_stack_boundary;
6de9cd9a
DN
3224 /* The static chain value to use for this call. */
3225 rtx static_chain_value;
f2d33f13
JH
3226 /* See if this is "nothrow" function call. */
3227 if (TREE_NOTHROW (exp))
3228 flags |= ECF_NOTHROW;
3229
6de9cd9a
DN
3230 /* See if we can find a DECL-node for the actual function, and get the
3231 function attributes (flags) from the function decl or type node. */
39b0dce7
JM
3232 fndecl = get_callee_fndecl (exp);
3233 if (fndecl)
51bbfa0c 3234 {
57782ad8 3235 fntype = TREE_TYPE (fndecl);
39b0dce7 3236 flags |= flags_from_decl_or_type (fndecl);
e384e6b5 3237 return_flags |= decl_return_flags (fndecl);
51bbfa0c 3238 }
39b0dce7 3239 else
72954a4f 3240 {
28ed065e 3241 fntype = TREE_TYPE (TREE_TYPE (addr));
57782ad8 3242 flags |= flags_from_decl_or_type (fntype);
4c640e26
EB
3243 if (CALL_EXPR_BY_DESCRIPTOR (exp))
3244 flags |= ECF_BY_DESCRIPTOR;
72954a4f 3245 }
28ed065e 3246 rettype = TREE_TYPE (exp);
7393c642 3247
57782ad8 3248 struct_value = targetm.calls.struct_value_rtx (fntype, 0);
61f71b34 3249
8c6a8269
RS
3250 /* Warn if this value is an aggregate type,
3251 regardless of which calling convention we are using for it. */
28ed065e 3252 if (AGGREGATE_TYPE_P (rettype))
ccf08a6e 3253 warning (OPT_Waggregate_return, "function call has aggregate value");
8c6a8269 3254
becfd6e5
KZ
3255 /* If the result of a non looping pure or const function call is
3256 ignored (or void), and none of its arguments are volatile, we can
3257 avoid expanding the call and just evaluate the arguments for
3258 side-effects. */
8c6a8269 3259 if ((flags & (ECF_CONST | ECF_PURE))
becfd6e5 3260 && (!(flags & ECF_LOOPING_CONST_OR_PURE))
8c6a8269 3261 && (ignore || target == const0_rtx
28ed065e 3262 || TYPE_MODE (rettype) == VOIDmode))
8c6a8269
RS
3263 {
3264 bool volatilep = false;
3265 tree arg;
078a18a4 3266 call_expr_arg_iterator iter;
8c6a8269 3267
078a18a4
SL
3268 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
3269 if (TREE_THIS_VOLATILE (arg))
8c6a8269
RS
3270 {
3271 volatilep = true;
3272 break;
3273 }
3274
3275 if (! volatilep)
3276 {
078a18a4
SL
3277 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
3278 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
8c6a8269
RS
3279 return const0_rtx;
3280 }
3281 }
3282
6f90e075 3283#ifdef REG_PARM_STACK_SPACE
5d059ed9 3284 reg_parm_stack_space = REG_PARM_STACK_SPACE (!fndecl ? fntype : fndecl);
6f90e075 3285#endif
6f90e075 3286
5d059ed9 3287 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
81464b2c 3288 && reg_parm_stack_space > 0 && PUSH_ARGS)
e5e809f4 3289 must_preallocate = 1;
e5e809f4 3290
51bbfa0c
RS
3291 /* Set up a place to return a structure. */
3292
3293 /* Cater to broken compilers. */
d47d0a8d 3294 if (aggregate_value_p (exp, fntype))
51bbfa0c
RS
3295 {
3296 /* This call returns a big structure. */
84b8030f 3297 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
51bbfa0c
RS
3298
3299#ifdef PCC_STATIC_STRUCT_RETURN
9e7b1d0a
RS
3300 {
3301 pcc_struct_value = 1;
9e7b1d0a
RS
3302 }
3303#else /* not PCC_STATIC_STRUCT_RETURN */
3304 {
28ed065e 3305 struct_value_size = int_size_in_bytes (rettype);
51bbfa0c 3306
391756ad
EB
3307 /* Even if it is semantically safe to use the target as the return
3308 slot, it may be not sufficiently aligned for the return type. */
3309 if (CALL_EXPR_RETURN_SLOT_OPT (exp)
3310 && target
3311 && MEM_P (target)
3312 && !(MEM_ALIGN (target) < TYPE_ALIGN (rettype)
e0bd6c9f
RS
3313 && targetm.slow_unaligned_access (TYPE_MODE (rettype),
3314 MEM_ALIGN (target))))
9e7b1d0a
RS
3315 structure_value_addr = XEXP (target, 0);
3316 else
3317 {
9e7b1d0a
RS
3318 /* For variable-sized objects, we must be called with a target
3319 specified. If we were to allocate space on the stack here,
3320 we would have no way of knowing when to free it. */
9474e8ab 3321 rtx d = assign_temp (rettype, 1, 1);
4361b41d 3322 structure_value_addr = XEXP (d, 0);
9e7b1d0a
RS
3323 target = 0;
3324 }
3325 }
3326#endif /* not PCC_STATIC_STRUCT_RETURN */
51bbfa0c
RS
3327 }
3328
099e9712 3329 /* Figure out the amount to which the stack should be aligned. */
099e9712 3330 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
b255a036
JH
3331 if (fndecl)
3332 {
3dafb85c 3333 struct cgraph_rtl_info *i = cgraph_node::rtl_info (fndecl);
17b29c0a
L
3334 /* Without automatic stack alignment, we can't increase preferred
3335 stack boundary. With automatic stack alignment, it is
3336 unnecessary since unless we can guarantee that all callers will
3337 align the outgoing stack properly, callee has to align its
3338 stack anyway. */
3339 if (i
3340 && i->preferred_incoming_stack_boundary
3341 && i->preferred_incoming_stack_boundary < preferred_stack_boundary)
b255a036
JH
3342 preferred_stack_boundary = i->preferred_incoming_stack_boundary;
3343 }
099e9712
JH
3344
3345 /* Operand 0 is a pointer-to-function; get the type of the function. */
09e2bf48 3346 funtype = TREE_TYPE (addr);
366de0ce 3347 gcc_assert (POINTER_TYPE_P (funtype));
099e9712
JH
3348 funtype = TREE_TYPE (funtype);
3349
078a18a4
SL
3350 /* Count whether there are actual complex arguments that need to be split
3351 into their real and imaginary parts. Munge the type_arg_types
3352 appropriately here as well. */
42ba5130 3353 if (targetm.calls.split_complex_arg)
ded9bf77 3354 {
078a18a4
SL
3355 call_expr_arg_iterator iter;
3356 tree arg;
3357 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
3358 {
3359 tree type = TREE_TYPE (arg);
3360 if (type && TREE_CODE (type) == COMPLEX_TYPE
3361 && targetm.calls.split_complex_arg (type))
3362 num_complex_actuals++;
3363 }
ded9bf77 3364 type_arg_types = split_complex_types (TYPE_ARG_TYPES (funtype));
ded9bf77
AH
3365 }
3366 else
3367 type_arg_types = TYPE_ARG_TYPES (funtype);
3368
099e9712 3369 if (flags & ECF_MAY_BE_ALLOCA)
e3b5732b 3370 cfun->calls_alloca = 1;
099e9712
JH
3371
3372 /* If struct_value_rtx is 0, it means pass the address
078a18a4
SL
3373 as if it were an extra parameter. Put the argument expression
3374 in structure_value_addr_value. */
61f71b34 3375 if (structure_value_addr && struct_value == 0)
099e9712
JH
3376 {
3377 /* If structure_value_addr is a REG other than
3378 virtual_outgoing_args_rtx, we can use always use it. If it
3379 is not a REG, we must always copy it into a register.
3380 If it is virtual_outgoing_args_rtx, we must copy it to another
3381 register in some cases. */
f8cfc6aa 3382 rtx temp = (!REG_P (structure_value_addr)
099e9712
JH
3383 || (ACCUMULATE_OUTGOING_ARGS
3384 && stack_arg_under_construction
3385 && structure_value_addr == virtual_outgoing_args_rtx)
7ae4ad28 3386 ? copy_addr_to_reg (convert_memory_address
57782ad8 3387 (Pmode, structure_value_addr))
099e9712
JH
3388 : structure_value_addr);
3389
078a18a4
SL
3390 structure_value_addr_value =
3391 make_tree (build_pointer_type (TREE_TYPE (funtype)), temp);
d5e254e1 3392 structure_value_addr_parm = CALL_WITH_BOUNDS_P (exp) ? 2 : 1;
099e9712
JH
3393 }
3394
3395 /* Count the arguments and set NUM_ACTUALS. */
078a18a4
SL
3396 num_actuals =
3397 call_expr_nargs (exp) + num_complex_actuals + structure_value_addr_parm;
099e9712
JH
3398
3399 /* Compute number of named args.
3a4d587b
AM
3400 First, do a raw count of the args for INIT_CUMULATIVE_ARGS. */
3401
3402 if (type_arg_types != 0)
3403 n_named_args
3404 = (list_length (type_arg_types)
3405 /* Count the struct value address, if it is passed as a parm. */
3406 + structure_value_addr_parm);
3407 else
3408 /* If we know nothing, treat all args as named. */
3409 n_named_args = num_actuals;
3410
3411 /* Start updating where the next arg would go.
3412
3413 On some machines (such as the PA) indirect calls have a different
3414 calling convention than normal calls. The fourth argument in
3415 INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
3416 or not. */
d5cc9181
JR
3417 INIT_CUMULATIVE_ARGS (args_so_far_v, funtype, NULL_RTX, fndecl, n_named_args);
3418 args_so_far = pack_cumulative_args (&args_so_far_v);
3a4d587b
AM
3419
3420 /* Now possibly adjust the number of named args.
099e9712 3421 Normally, don't include the last named arg if anonymous args follow.
3a179764
KH
3422 We do include the last named arg if
3423 targetm.calls.strict_argument_naming() returns nonzero.
099e9712
JH
3424 (If no anonymous args follow, the result of list_length is actually
3425 one too large. This is harmless.)
3426
4ac8340c 3427 If targetm.calls.pretend_outgoing_varargs_named() returns
3a179764
KH
3428 nonzero, and targetm.calls.strict_argument_naming() returns zero,
3429 this machine will be able to place unnamed args that were passed
3430 in registers into the stack. So treat all args as named. This
3431 allows the insns emitting for a specific argument list to be
3432 independent of the function declaration.
4ac8340c
KH
3433
3434 If targetm.calls.pretend_outgoing_varargs_named() returns zero,
3435 we do not have any reliable way to pass unnamed args in
3436 registers, so we must force them into memory. */
099e9712 3437
3a4d587b 3438 if (type_arg_types != 0
d5cc9181 3439 && targetm.calls.strict_argument_naming (args_so_far))
3a4d587b
AM
3440 ;
3441 else if (type_arg_types != 0
d5cc9181 3442 && ! targetm.calls.pretend_outgoing_varargs_named (args_so_far))
3a4d587b
AM
3443 /* Don't include the last named arg. */
3444 --n_named_args;
099e9712 3445 else
3a4d587b 3446 /* Treat all args as named. */
099e9712
JH
3447 n_named_args = num_actuals;
3448
099e9712 3449 /* Make a vector to hold all the information about each arg. */
765fc0f7 3450 args = XCNEWVEC (struct arg_data, num_actuals);
099e9712 3451
d80d2d2a
KH
3452 /* Build up entries in the ARGS array, compute the size of the
3453 arguments into ARGS_SIZE, etc. */
099e9712 3454 initialize_argument_information (num_actuals, args, &args_size,
078a18a4 3455 n_named_args, exp,
45769134 3456 structure_value_addr_value, fndecl, fntype,
d5cc9181 3457 args_so_far, reg_parm_stack_space,
099e9712 3458 &old_stack_level, &old_pending_adj,
dd292d0a 3459 &must_preallocate, &flags,
6de9cd9a 3460 &try_tail_call, CALL_FROM_THUNK_P (exp));
099e9712
JH
3461
3462 if (args_size.var)
84b8030f 3463 must_preallocate = 1;
099e9712
JH
3464
3465 /* Now make final decision about preallocating stack space. */
3466 must_preallocate = finalize_must_preallocate (must_preallocate,
3467 num_actuals, args,
3468 &args_size);
3469
3470 /* If the structure value address will reference the stack pointer, we
3471 must stabilize it. We don't need to do this if we know that we are
3472 not going to adjust the stack pointer in processing this call. */
3473
3474 if (structure_value_addr
3475 && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
3476 || reg_mentioned_p (virtual_outgoing_args_rtx,
3477 structure_value_addr))
3478 && (args_size.var
3479 || (!ACCUMULATE_OUTGOING_ARGS && args_size.constant)))
3480 structure_value_addr = copy_to_reg (structure_value_addr);
0a1c58a2 3481
7ae4ad28 3482 /* Tail calls can make things harder to debug, and we've traditionally
194c7c45 3483 pushed these optimizations into -O2. Don't try if we're already
fb158467 3484 expanding a call, as that means we're an argument. Don't try if
3fbd86b1 3485 there's cleanups, as we know there's code to follow the call. */
0a1c58a2 3486
099e9712
JH
3487 if (currently_expanding_call++ != 0
3488 || !flag_optimize_sibling_calls
6de9cd9a 3489 || args_size.var
6fb5fa3c 3490 || dbg_cnt (tail_call) == false)
6de9cd9a 3491 try_tail_call = 0;
099e9712 3492
9a385c2d
DM
3493 /* If the user has marked the function as requiring tail-call
3494 optimization, attempt it. */
3495 if (must_tail_call)
3496 try_tail_call = 1;
3497
099e9712 3498 /* Rest of purposes for tail call optimizations to fail. */
b40d90e6 3499 if (try_tail_call)
9a385c2d
DM
3500 try_tail_call = can_implement_as_sibling_call_p (exp,
3501 structure_value_addr,
3502 funtype,
3503 reg_parm_stack_space,
3504 fndecl,
b40d90e6 3505 flags, addr, args_size);
497eb8c3 3506
c69cd1f5
JJ
3507 /* Check if caller and callee disagree in promotion of function
3508 return value. */
3509 if (try_tail_call)
3510 {
ef4bddc2
RS
3511 machine_mode caller_mode, caller_promoted_mode;
3512 machine_mode callee_mode, callee_promoted_mode;
c69cd1f5
JJ
3513 int caller_unsignedp, callee_unsignedp;
3514 tree caller_res = DECL_RESULT (current_function_decl);
3515
3516 caller_unsignedp = TYPE_UNSIGNED (TREE_TYPE (caller_res));
cde0f3fd 3517 caller_mode = DECL_MODE (caller_res);
c69cd1f5 3518 callee_unsignedp = TYPE_UNSIGNED (TREE_TYPE (funtype));
cde0f3fd
PB
3519 callee_mode = TYPE_MODE (TREE_TYPE (funtype));
3520 caller_promoted_mode
3521 = promote_function_mode (TREE_TYPE (caller_res), caller_mode,
3522 &caller_unsignedp,
3523 TREE_TYPE (current_function_decl), 1);
3524 callee_promoted_mode
666e3ceb 3525 = promote_function_mode (TREE_TYPE (funtype), callee_mode,
cde0f3fd 3526 &callee_unsignedp,
666e3ceb 3527 funtype, 1);
c69cd1f5
JJ
3528 if (caller_mode != VOIDmode
3529 && (caller_promoted_mode != callee_promoted_mode
3530 || ((caller_mode != caller_promoted_mode
3531 || callee_mode != callee_promoted_mode)
3532 && (caller_unsignedp != callee_unsignedp
bd4288c0 3533 || partial_subreg_p (caller_mode, callee_mode)))))
9a385c2d
DM
3534 {
3535 try_tail_call = 0;
3536 maybe_complain_about_tail_call (exp,
3537 "caller and callee disagree in"
3538 " promotion of function"
3539 " return value");
3540 }
c69cd1f5
JJ
3541 }
3542
01973e26
L
3543 /* Ensure current function's preferred stack boundary is at least
3544 what we need. Stack alignment may also increase preferred stack
3545 boundary. */
b5f772ce 3546 if (crtl->preferred_stack_boundary < preferred_stack_boundary)
cb91fab0 3547 crtl->preferred_stack_boundary = preferred_stack_boundary;
01973e26
L
3548 else
3549 preferred_stack_boundary = crtl->preferred_stack_boundary;
c2f8b491 3550
099e9712 3551 preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
497eb8c3 3552
0a1c58a2
JL
3553 /* We want to make two insn chains; one for a sibling call, the other
3554 for a normal call. We will select one of the two chains after
3555 initial RTL generation is complete. */
b820d2b8 3556 for (pass = try_tail_call ? 0 : 1; pass < 2; pass++)
0a1c58a2
JL
3557 {
3558 int sibcall_failure = 0;
f5143c46 3559 /* We want to emit any pending stack adjustments before the tail
0a1c58a2 3560 recursion "call". That way we know any adjustment after the tail
7ae4ad28 3561 recursion call can be ignored if we indeed use the tail
0a1c58a2 3562 call expansion. */
7f2f0a01 3563 saved_pending_stack_adjust save;
48810515
DM
3564 rtx_insn *insns, *before_call, *after_args;
3565 rtx next_arg_reg;
39842893 3566
0a1c58a2
JL
3567 if (pass == 0)
3568 {
0a1c58a2
JL
3569 /* State variables we need to save and restore between
3570 iterations. */
7f2f0a01 3571 save_pending_stack_adjust (&save);
0a1c58a2 3572 }
f2d33f13
JH
3573 if (pass)
3574 flags &= ~ECF_SIBCALL;
3575 else
3576 flags |= ECF_SIBCALL;
51bbfa0c 3577
0a1c58a2 3578 /* Other state variables that we must reinitialize each time
f2d33f13 3579 through the loop (that are not initialized by the loop itself). */
0a1c58a2
JL
3580 argblock = 0;
3581 call_fusage = 0;
fa76d9e0 3582
f725a3ec 3583 /* Start a new sequence for the normal call case.
51bbfa0c 3584
0a1c58a2
JL
3585 From this point on, if the sibling call fails, we want to set
3586 sibcall_failure instead of continuing the loop. */
3587 start_sequence ();
eecb6f50 3588
0a1c58a2
JL
3589 /* Don't let pending stack adjusts add up to too much.
3590 Also, do all pending adjustments now if there is any chance
3591 this might be a call to alloca or if we are expanding a sibling
9dd9bf80 3592 call sequence.
63579539
DJ
3593 Also do the adjustments before a throwing call, otherwise
3594 exception handling can fail; PR 19225. */
0a1c58a2 3595 if (pending_stack_adjust >= 32
b5cd4ed4 3596 || (pending_stack_adjust > 0
9dd9bf80 3597 && (flags & ECF_MAY_BE_ALLOCA))
63579539
DJ
3598 || (pending_stack_adjust > 0
3599 && flag_exceptions && !(flags & ECF_NOTHROW))
0a1c58a2
JL
3600 || pass == 0)
3601 do_pending_stack_adjust ();
51bbfa0c 3602
0a1c58a2 3603 /* Precompute any arguments as needed. */
f8a097cd 3604 if (pass)
84b8030f 3605 precompute_arguments (num_actuals, args);
51bbfa0c 3606
0a1c58a2
JL
3607 /* Now we are about to start emitting insns that can be deleted
3608 if a libcall is deleted. */
84b8030f 3609 if (pass && (flags & ECF_MALLOC))
0a1c58a2 3610 start_sequence ();
51bbfa0c 3611
87a5dc2d
JW
3612 if (pass == 0
3613 && crtl->stack_protect_guard
3614 && targetm.stack_protect_runtime_enabled_p ())
b755446c
RH
3615 stack_protect_epilogue ();
3616
099e9712 3617 adjusted_args_size = args_size;
ce48579b
RH
3618 /* Compute the actual size of the argument block required. The variable
3619 and constant sizes must be combined, the size may have to be rounded,
3620 and there may be a minimum required size. When generating a sibcall
3621 pattern, do not round up, since we'll be re-using whatever space our
3622 caller provided. */
3623 unadjusted_args_size
f725a3ec
KH
3624 = compute_argument_block_size (reg_parm_stack_space,
3625 &adjusted_args_size,
5d059ed9 3626 fndecl, fntype,
ce48579b
RH
3627 (pass == 0 ? 0
3628 : preferred_stack_boundary));
3629
f725a3ec 3630 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
ce48579b 3631
f8a097cd 3632 /* The argument block when performing a sibling call is the
c22cacf3 3633 incoming argument block. */
f8a097cd 3634 if (pass == 0)
c67846f2 3635 {
2e3f842f 3636 argblock = crtl->args.internal_arg_pointer;
76e048a8
KT
3637 if (STACK_GROWS_DOWNWARD)
3638 argblock
3639 = plus_constant (Pmode, argblock, crtl->args.pretend_args_size);
3640 else
3641 argblock
3642 = plus_constant (Pmode, argblock, -crtl->args.pretend_args_size);
3643
c67846f2 3644 stored_args_map = sbitmap_alloc (args_size.constant);
f61e445a 3645 bitmap_clear (stored_args_map);
c67846f2 3646 }
ce48579b 3647
0a1c58a2
JL
3648 /* If we have no actual push instructions, or shouldn't use them,
3649 make space for all args right now. */
099e9712 3650 else if (adjusted_args_size.var != 0)
51bbfa0c 3651 {
0a1c58a2
JL
3652 if (old_stack_level == 0)
3653 {
9eac0f2a 3654 emit_stack_save (SAVE_BLOCK, &old_stack_level);
38afb23f 3655 old_stack_pointer_delta = stack_pointer_delta;
0a1c58a2
JL
3656 old_pending_adj = pending_stack_adjust;
3657 pending_stack_adjust = 0;
0a1c58a2
JL
3658 /* stack_arg_under_construction says whether a stack arg is
3659 being constructed at the old stack level. Pushing the stack
3660 gets a clean outgoing argument block. */
3661 old_stack_arg_under_construction = stack_arg_under_construction;
3662 stack_arg_under_construction = 0;
0a1c58a2 3663 }
099e9712 3664 argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
a11e0df4 3665 if (flag_stack_usage_info)
d3c12306 3666 current_function_has_unbounded_dynamic_stack_size = 1;
51bbfa0c 3667 }
0a1c58a2
JL
3668 else
3669 {
3670 /* Note that we must go through the motions of allocating an argument
3671 block even if the size is zero because we may be storing args
3672 in the area reserved for register arguments, which may be part of
3673 the stack frame. */
26a258fe 3674
099e9712 3675 int needed = adjusted_args_size.constant;
51bbfa0c 3676
0a1c58a2
JL
3677 /* Store the maximum argument space used. It will be pushed by
3678 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
3679 checking). */
51bbfa0c 3680
38173d38
JH
3681 if (needed > crtl->outgoing_args_size)
3682 crtl->outgoing_args_size = needed;
51bbfa0c 3683
0a1c58a2
JL
3684 if (must_preallocate)
3685 {
f73ad30e
JH
3686 if (ACCUMULATE_OUTGOING_ARGS)
3687 {
f8a097cd
JH
3688 /* Since the stack pointer will never be pushed, it is
3689 possible for the evaluation of a parm to clobber
3690 something we have already written to the stack.
3691 Since most function calls on RISC machines do not use
3692 the stack, this is uncommon, but must work correctly.
26a258fe 3693
f73ad30e 3694 Therefore, we save any area of the stack that was already
f8a097cd
JH
3695 written and that we are using. Here we set up to do this
3696 by making a new stack usage map from the old one. The
f725a3ec 3697 actual save will be done by store_one_arg.
26a258fe 3698
f73ad30e
JH
3699 Another approach might be to try to reorder the argument
3700 evaluations to avoid this conflicting stack usage. */
26a258fe 3701
f8a097cd
JH
3702 /* Since we will be writing into the entire argument area,
3703 the map must be allocated for its entire size, not just
3704 the part that is the responsibility of the caller. */
5d059ed9 3705 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
ac294f0b 3706 needed += reg_parm_stack_space;
51bbfa0c 3707
6dad9361
TS
3708 if (ARGS_GROW_DOWNWARD)
3709 highest_outgoing_arg_in_use
3710 = MAX (initial_highest_arg_in_use, needed + 1);
3711 else
3712 highest_outgoing_arg_in_use
3713 = MAX (initial_highest_arg_in_use, needed);
3714
04695783 3715 free (stack_usage_map_buf);
5ed6ace5 3716 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
d9725c41 3717 stack_usage_map = stack_usage_map_buf;
51bbfa0c 3718
f73ad30e 3719 if (initial_highest_arg_in_use)
2e09e75a
JM
3720 memcpy (stack_usage_map, initial_stack_usage_map,
3721 initial_highest_arg_in_use);
2f4aa534 3722
f73ad30e 3723 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
961192e1 3724 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
f73ad30e
JH
3725 (highest_outgoing_arg_in_use
3726 - initial_highest_arg_in_use));
3727 needed = 0;
2f4aa534 3728
f8a097cd
JH
3729 /* The address of the outgoing argument list must not be
3730 copied to a register here, because argblock would be left
3731 pointing to the wrong place after the call to
f725a3ec 3732 allocate_dynamic_stack_space below. */
2f4aa534 3733
f73ad30e 3734 argblock = virtual_outgoing_args_rtx;
f725a3ec 3735 }
f73ad30e 3736 else
26a258fe 3737 {
f73ad30e 3738 if (inhibit_defer_pop == 0)
0a1c58a2 3739 {
f73ad30e 3740 /* Try to reuse some or all of the pending_stack_adjust
ce48579b
RH
3741 to get this space. */
3742 needed
f725a3ec 3743 = (combine_pending_stack_adjustment_and_call
ce48579b 3744 (unadjusted_args_size,
099e9712 3745 &adjusted_args_size,
ce48579b
RH
3746 preferred_unit_stack_boundary));
3747
3748 /* combine_pending_stack_adjustment_and_call computes
3749 an adjustment before the arguments are allocated.
3750 Account for them and see whether or not the stack
3751 needs to go up or down. */
3752 needed = unadjusted_args_size - needed;
3753
3754 if (needed < 0)
f73ad30e 3755 {
ce48579b
RH
3756 /* We're releasing stack space. */
3757 /* ??? We can avoid any adjustment at all if we're
3758 already aligned. FIXME. */
3759 pending_stack_adjust = -needed;
3760 do_pending_stack_adjust ();
f73ad30e
JH
3761 needed = 0;
3762 }
f725a3ec 3763 else
ce48579b
RH
3764 /* We need to allocate space. We'll do that in
3765 push_block below. */
3766 pending_stack_adjust = 0;
0a1c58a2 3767 }
ce48579b
RH
3768
3769 /* Special case this because overhead of `push_block' in
3770 this case is non-trivial. */
f73ad30e
JH
3771 if (needed == 0)
3772 argblock = virtual_outgoing_args_rtx;
0a1c58a2 3773 else
d892f288
DD
3774 {
3775 argblock = push_block (GEN_INT (needed), 0, 0);
6dad9361
TS
3776 if (ARGS_GROW_DOWNWARD)
3777 argblock = plus_constant (Pmode, argblock, needed);
d892f288 3778 }
f73ad30e 3779
f8a097cd
JH
3780 /* We only really need to call `copy_to_reg' in the case
3781 where push insns are going to be used to pass ARGBLOCK
3782 to a function call in ARGS. In that case, the stack
3783 pointer changes value from the allocation point to the
3784 call point, and hence the value of
3785 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
3786 as well always do it. */
f73ad30e 3787 argblock = copy_to_reg (argblock);
38afb23f
OH
3788 }
3789 }
3790 }
0a1c58a2 3791
38afb23f
OH
3792 if (ACCUMULATE_OUTGOING_ARGS)
3793 {
3794 /* The save/restore code in store_one_arg handles all
3795 cases except one: a constructor call (including a C
3796 function returning a BLKmode struct) to initialize
3797 an argument. */
3798 if (stack_arg_under_construction)
3799 {
ac294f0b
KT
3800 rtx push_size
3801 = GEN_INT (adjusted_args_size.constant
5d059ed9 3802 + (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype
81464b2c 3803 : TREE_TYPE (fndecl))) ? 0
ac294f0b 3804 : reg_parm_stack_space));
38afb23f
OH
3805 if (old_stack_level == 0)
3806 {
9eac0f2a 3807 emit_stack_save (SAVE_BLOCK, &old_stack_level);
38afb23f
OH
3808 old_stack_pointer_delta = stack_pointer_delta;
3809 old_pending_adj = pending_stack_adjust;
3810 pending_stack_adjust = 0;
3811 /* stack_arg_under_construction says whether a stack
3812 arg is being constructed at the old stack level.
3813 Pushing the stack gets a clean outgoing argument
3814 block. */
3815 old_stack_arg_under_construction
3816 = stack_arg_under_construction;
3817 stack_arg_under_construction = 0;
3818 /* Make a new map for the new argument list. */
04695783 3819 free (stack_usage_map_buf);
b9eae1a9 3820 stack_usage_map_buf = XCNEWVEC (char, highest_outgoing_arg_in_use);
d9725c41 3821 stack_usage_map = stack_usage_map_buf;
38afb23f 3822 highest_outgoing_arg_in_use = 0;
f73ad30e 3823 }
d3c12306
EB
3824 /* We can pass TRUE as the 4th argument because we just
3825 saved the stack pointer and will restore it right after
3826 the call. */
9e878cf1
EB
3827 allocate_dynamic_stack_space (push_size, 0, BIGGEST_ALIGNMENT,
3828 -1, true);
0a1c58a2 3829 }
bfbf933a 3830
38afb23f
OH
3831 /* If argument evaluation might modify the stack pointer,
3832 copy the address of the argument list to a register. */
3833 for (i = 0; i < num_actuals; i++)
3834 if (args[i].pass_on_stack)
3835 {
3836 argblock = copy_addr_to_reg (argblock);
3837 break;
3838 }
3839 }
d329e058 3840
0a1c58a2 3841 compute_argument_addresses (args, argblock, num_actuals);
bfbf933a 3842
5ba53785
UB
3843 /* Stack is properly aligned, pops can't safely be deferred during
3844 the evaluation of the arguments. */
3845 NO_DEFER_POP;
3846
ac4ee457
UB
3847 /* Precompute all register parameters. It isn't safe to compute
3848 anything once we have started filling any specific hard regs.
3849 TLS symbols sometimes need a call to resolve. Precompute
3850 register parameters before any stack pointer manipulation
3851 to avoid unaligned stack in the called function. */
3852 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
3853
5ba53785
UB
3854 OK_DEFER_POP;
3855
3d9684ae
JG
3856 /* Perform stack alignment before the first push (the last arg). */
3857 if (argblock == 0
f830ddc2 3858 && adjusted_args_size.constant > reg_parm_stack_space
099e9712 3859 && adjusted_args_size.constant != unadjusted_args_size)
4e217aed 3860 {
0a1c58a2
JL
3861 /* When the stack adjustment is pending, we get better code
3862 by combining the adjustments. */
f725a3ec 3863 if (pending_stack_adjust
0a1c58a2 3864 && ! inhibit_defer_pop)
ce48579b
RH
3865 {
3866 pending_stack_adjust
f725a3ec 3867 = (combine_pending_stack_adjustment_and_call
ce48579b 3868 (unadjusted_args_size,
099e9712 3869 &adjusted_args_size,
ce48579b
RH
3870 preferred_unit_stack_boundary));
3871 do_pending_stack_adjust ();
3872 }
0a1c58a2 3873 else if (argblock == 0)
099e9712 3874 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
0a1c58a2 3875 - unadjusted_args_size));
0a1c58a2 3876 }
ebcd0b57
JH
3877 /* Now that the stack is properly aligned, pops can't safely
3878 be deferred during the evaluation of the arguments. */
3879 NO_DEFER_POP;
51bbfa0c 3880
d3c12306
EB
3881 /* Record the maximum pushed stack space size. We need to delay
3882 doing it this far to take into account the optimization done
3883 by combine_pending_stack_adjustment_and_call. */
a11e0df4 3884 if (flag_stack_usage_info
d3c12306
EB
3885 && !ACCUMULATE_OUTGOING_ARGS
3886 && pass
3887 && adjusted_args_size.var == 0)
3888 {
3889 int pushed = adjusted_args_size.constant + pending_stack_adjust;
3890 if (pushed > current_function_pushed_stack_size)
3891 current_function_pushed_stack_size = pushed;
3892 }
3893
09e2bf48 3894 funexp = rtx_for_function_call (fndecl, addr);
51bbfa0c 3895
5039610b
SL
3896 if (CALL_EXPR_STATIC_CHAIN (exp))
3897 static_chain_value = expand_normal (CALL_EXPR_STATIC_CHAIN (exp));
6de9cd9a
DN
3898 else
3899 static_chain_value = 0;
3900
f73ad30e 3901#ifdef REG_PARM_STACK_SPACE
0a1c58a2
JL
3902 /* Save the fixed argument area if it's part of the caller's frame and
3903 is clobbered by argument setup for this call. */
f8a097cd 3904 if (ACCUMULATE_OUTGOING_ARGS && pass)
f73ad30e
JH
3905 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3906 &low_to_save, &high_to_save);
b94301c2 3907#endif
51bbfa0c 3908
0a1c58a2
JL
3909 /* Now store (and compute if necessary) all non-register parms.
3910 These come before register parms, since they can require block-moves,
3911 which could clobber the registers used for register parms.
3912 Parms which have partial registers are not stored here,
3913 but we do preallocate space here if they want that. */
51bbfa0c 3914
0a1c58a2 3915 for (i = 0; i < num_actuals; i++)
0196c95e 3916 {
d5e254e1
IE
3917 /* Delay bounds until all other args are stored. */
3918 if (POINTER_BOUNDS_P (args[i].tree_value))
3919 continue;
3920 else if (args[i].reg == 0 || args[i].pass_on_stack)
0196c95e 3921 {
48810515 3922 rtx_insn *before_arg = get_last_insn ();
0196c95e 3923
ddc923b5
MP
3924 /* We don't allow passing huge (> 2^30 B) arguments
3925 by value. It would cause an overflow later on. */
3926 if (adjusted_args_size.constant
3927 >= (1 << (HOST_BITS_PER_INT - 2)))
3928 {
3929 sorry ("passing too large argument on stack");
3930 continue;
3931 }
3932
0196c95e
JJ
3933 if (store_one_arg (&args[i], argblock, flags,
3934 adjusted_args_size.var != 0,
3935 reg_parm_stack_space)
3936 || (pass == 0
3937 && check_sibcall_argument_overlap (before_arg,
3938 &args[i], 1)))
3939 sibcall_failure = 1;
3940 }
3941
2b1c5433 3942 if (args[i].stack)
7d810276
JJ
3943 call_fusage
3944 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[i].tree_value)),
3945 gen_rtx_USE (VOIDmode, args[i].stack),
3946 call_fusage);
0196c95e 3947 }
0a1c58a2
JL
3948
3949 /* If we have a parm that is passed in registers but not in memory
3950 and whose alignment does not permit a direct copy into registers,
3951 make a group of pseudos that correspond to each register that we
3952 will later fill. */
3953 if (STRICT_ALIGNMENT)
3954 store_unaligned_arguments_into_pseudos (args, num_actuals);
3955
3956 /* Now store any partially-in-registers parm.
3957 This is the last place a block-move can happen. */
3958 if (reg_parm_seen)
3959 for (i = 0; i < num_actuals; i++)
3960 if (args[i].partial != 0 && ! args[i].pass_on_stack)
c67846f2 3961 {
48810515 3962 rtx_insn *before_arg = get_last_insn ();
c67846f2 3963
99206968
KT
3964 /* On targets with weird calling conventions (e.g. PA) it's
3965 hard to ensure that all cases of argument overlap between
3966 stack and registers work. Play it safe and bail out. */
3967 if (ARGS_GROW_DOWNWARD && !STACK_GROWS_DOWNWARD)
3968 {
3969 sibcall_failure = 1;
3970 break;
3971 }
3972
4c6b3b2a
JJ
3973 if (store_one_arg (&args[i], argblock, flags,
3974 adjusted_args_size.var != 0,
3975 reg_parm_stack_space)
3976 || (pass == 0
3977 && check_sibcall_argument_overlap (before_arg,
0cdca92b 3978 &args[i], 1)))
c67846f2
JJ
3979 sibcall_failure = 1;
3980 }
51bbfa0c 3981
2f21e1ba
BS
3982 bool any_regs = false;
3983 for (i = 0; i < num_actuals; i++)
3984 if (args[i].reg != NULL_RTX)
3985 {
3986 any_regs = true;
3987 targetm.calls.call_args (args[i].reg, funtype);
3988 }
3989 if (!any_regs)
3990 targetm.calls.call_args (pc_rtx, funtype);
3991
3992 /* Figure out the register where the value, if any, will come back. */
3993 valreg = 0;
3994 valbnd = 0;
3995 if (TYPE_MODE (rettype) != VOIDmode
3996 && ! structure_value_addr)
3997 {
3998 if (pcc_struct_value)
3999 {
4000 valreg = hard_function_value (build_pointer_type (rettype),
4001 fndecl, NULL, (pass == 0));
4002 if (CALL_WITH_BOUNDS_P (exp))
4003 valbnd = targetm.calls.
4004 chkp_function_value_bounds (build_pointer_type (rettype),
4005 fndecl, (pass == 0));
4006 }
4007 else
4008 {
4009 valreg = hard_function_value (rettype, fndecl, fntype,
4010 (pass == 0));
4011 if (CALL_WITH_BOUNDS_P (exp))
4012 valbnd = targetm.calls.chkp_function_value_bounds (rettype,
4013 fndecl,
4014 (pass == 0));
4015 }
4016
4017 /* If VALREG is a PARALLEL whose first member has a zero
4018 offset, use that. This is for targets such as m68k that
4019 return the same value in multiple places. */
4020 if (GET_CODE (valreg) == PARALLEL)
4021 {
4022 rtx elem = XVECEXP (valreg, 0, 0);
4023 rtx where = XEXP (elem, 0);
4024 rtx offset = XEXP (elem, 1);
4025 if (offset == const0_rtx
4026 && GET_MODE (where) == GET_MODE (valreg))
4027 valreg = where;
4028 }
4029 }
4030
d5e254e1
IE
4031 /* Store all bounds not passed in registers. */
4032 for (i = 0; i < num_actuals; i++)
4033 {
4034 if (POINTER_BOUNDS_P (args[i].tree_value)
4035 && !args[i].reg)
4036 store_bounds (&args[i],
4037 args[i].pointer_arg == -1
4038 ? NULL
4039 : &args[args[i].pointer_arg]);
4040 }
4041
0a1c58a2
JL
4042 /* If register arguments require space on the stack and stack space
4043 was not preallocated, allocate stack space here for arguments
4044 passed in registers. */
5d059ed9 4045 if (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
81464b2c 4046 && !ACCUMULATE_OUTGOING_ARGS
f725a3ec 4047 && must_preallocate == 0 && reg_parm_stack_space > 0)
0a1c58a2 4048 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
756e0e12 4049
0a1c58a2
JL
4050 /* Pass the function the address in which to return a
4051 structure value. */
4052 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
4053 {
7ae4ad28 4054 structure_value_addr
5ae6cd0d 4055 = convert_memory_address (Pmode, structure_value_addr);
61f71b34 4056 emit_move_insn (struct_value,
0a1c58a2
JL
4057 force_reg (Pmode,
4058 force_operand (structure_value_addr,
4059 NULL_RTX)));
4060
f8cfc6aa 4061 if (REG_P (struct_value))
61f71b34 4062 use_reg (&call_fusage, struct_value);
0a1c58a2 4063 }
c2939b57 4064
05e6ee93 4065 after_args = get_last_insn ();
78bcf3dc
EB
4066 funexp = prepare_call_address (fndecl ? fndecl : fntype, funexp,
4067 static_chain_value, &call_fusage,
4068 reg_parm_seen, flags);
6b8805cf 4069
0cdca92b
DJ
4070 load_register_parameters (args, num_actuals, &call_fusage, flags,
4071 pass == 0, &sibcall_failure);
f725a3ec 4072
0a1c58a2
JL
4073 /* Save a pointer to the last insn before the call, so that we can
4074 later safely search backwards to find the CALL_INSN. */
4075 before_call = get_last_insn ();
51bbfa0c 4076
7d167afd
JJ
4077 /* Set up next argument register. For sibling calls on machines
4078 with register windows this should be the incoming register. */
7d167afd 4079 if (pass == 0)
d5cc9181 4080 next_arg_reg = targetm.calls.function_incoming_arg (args_so_far,
3c07301f
NF
4081 VOIDmode,
4082 void_type_node,
4083 true);
7d167afd 4084 else
d5cc9181 4085 next_arg_reg = targetm.calls.function_arg (args_so_far,
3c07301f
NF
4086 VOIDmode, void_type_node,
4087 true);
7d167afd 4088
e384e6b5
BS
4089 if (pass == 1 && (return_flags & ERF_RETURNS_ARG))
4090 {
4091 int arg_nr = return_flags & ERF_RETURN_ARG_MASK;
3d9684ae 4092 arg_nr = num_actuals - arg_nr - 1;
b3681f13
TV
4093 if (arg_nr >= 0
4094 && arg_nr < num_actuals
4095 && args[arg_nr].reg
e384e6b5
BS
4096 && valreg
4097 && REG_P (valreg)
4098 && GET_MODE (args[arg_nr].reg) == GET_MODE (valreg))
4099 call_fusage
4100 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[arg_nr].tree_value)),
f7df4a84 4101 gen_rtx_SET (valreg, args[arg_nr].reg),
e384e6b5
BS
4102 call_fusage);
4103 }
0a1c58a2
JL
4104 /* All arguments and registers used for the call must be set up by
4105 now! */
4106
ce48579b 4107 /* Stack must be properly aligned now. */
366de0ce
NS
4108 gcc_assert (!pass
4109 || !(stack_pointer_delta % preferred_unit_stack_boundary));
ebcd0b57 4110
0a1c58a2 4111 /* Generate the actual call instruction. */
6de9cd9a 4112 emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
099e9712 4113 adjusted_args_size.constant, struct_value_size,
7d167afd 4114 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
d5cc9181 4115 flags, args_so_far);
0a1c58a2 4116
1e288103 4117 if (flag_ipa_ra)
4f660b15 4118 {
48810515
DM
4119 rtx_call_insn *last;
4120 rtx datum = NULL_RTX;
4f660b15
RO
4121 if (fndecl != NULL_TREE)
4122 {
4123 datum = XEXP (DECL_RTL (fndecl), 0);
4124 gcc_assert (datum != NULL_RTX
4125 && GET_CODE (datum) == SYMBOL_REF);
4126 }
4127 last = last_call_insn ();
4128 add_reg_note (last, REG_CALL_DECL, datum);
4129 }
4130
05e6ee93
MM
4131 /* If the call setup or the call itself overlaps with anything
4132 of the argument setup we probably clobbered our call address.
4133 In that case we can't do sibcalls. */
4134 if (pass == 0
4135 && check_sibcall_argument_overlap (after_args, 0, 0))
4136 sibcall_failure = 1;
4137
bef5d8b6
RS
4138 /* If a non-BLKmode value is returned at the most significant end
4139 of a register, shift the register right by the appropriate amount
4140 and update VALREG accordingly. BLKmode values are handled by the
4141 group load/store machinery below. */
4142 if (!structure_value_addr
4143 && !pcc_struct_value
66de4d7c 4144 && TYPE_MODE (rettype) != VOIDmode
28ed065e 4145 && TYPE_MODE (rettype) != BLKmode
66de4d7c 4146 && REG_P (valreg)
28ed065e 4147 && targetm.calls.return_in_msb (rettype))
bef5d8b6 4148 {
28ed065e 4149 if (shift_return_value (TYPE_MODE (rettype), false, valreg))
bef5d8b6 4150 sibcall_failure = 1;
28ed065e 4151 valreg = gen_rtx_REG (TYPE_MODE (rettype), REGNO (valreg));
bef5d8b6
RS
4152 }
4153
84b8030f 4154 if (pass && (flags & ECF_MALLOC))
0a1c58a2
JL
4155 {
4156 rtx temp = gen_reg_rtx (GET_MODE (valreg));
48810515 4157 rtx_insn *last, *insns;
0a1c58a2 4158
f725a3ec 4159 /* The return value from a malloc-like function is a pointer. */
28ed065e 4160 if (TREE_CODE (rettype) == POINTER_TYPE)
d154bfa2 4161 mark_reg_pointer (temp, MALLOC_ABI_ALIGNMENT);
0a1c58a2
JL
4162
4163 emit_move_insn (temp, valreg);
4164
4165 /* The return value from a malloc-like function can not alias
4166 anything else. */
4167 last = get_last_insn ();
65c5f2a6 4168 add_reg_note (last, REG_NOALIAS, temp);
0a1c58a2
JL
4169
4170 /* Write out the sequence. */
4171 insns = get_insns ();
4172 end_sequence ();
2f937369 4173 emit_insn (insns);
0a1c58a2
JL
4174 valreg = temp;
4175 }
51bbfa0c 4176
6fb5fa3c
DB
4177 /* For calls to `setjmp', etc., inform
4178 function.c:setjmp_warnings that it should complain if
4179 nonvolatile values are live. For functions that cannot
4180 return, inform flow that control does not fall through. */
51bbfa0c 4181
6e14af16 4182 if ((flags & ECF_NORETURN) || pass == 0)
c2939b57 4183 {
570a98eb 4184 /* The barrier must be emitted
0a1c58a2
JL
4185 immediately after the CALL_INSN. Some ports emit more
4186 than just a CALL_INSN above, so we must search for it here. */
51bbfa0c 4187
48810515 4188 rtx_insn *last = get_last_insn ();
4b4bf941 4189 while (!CALL_P (last))
0a1c58a2
JL
4190 {
4191 last = PREV_INSN (last);
4192 /* There was no CALL_INSN? */
366de0ce 4193 gcc_assert (last != before_call);
0a1c58a2 4194 }
51bbfa0c 4195
570a98eb 4196 emit_barrier_after (last);
8af61113 4197
f451eeef
JS
4198 /* Stack adjustments after a noreturn call are dead code.
4199 However when NO_DEFER_POP is in effect, we must preserve
4200 stack_pointer_delta. */
4201 if (inhibit_defer_pop == 0)
4202 {
4203 stack_pointer_delta = old_stack_allocated;
4204 pending_stack_adjust = 0;
4205 }
0a1c58a2 4206 }
51bbfa0c 4207
0a1c58a2 4208 /* If value type not void, return an rtx for the value. */
51bbfa0c 4209
28ed065e 4210 if (TYPE_MODE (rettype) == VOIDmode
0a1c58a2 4211 || ignore)
b5cd4ed4 4212 target = const0_rtx;
0a1c58a2
JL
4213 else if (structure_value_addr)
4214 {
3c0cb5de 4215 if (target == 0 || !MEM_P (target))
0a1c58a2 4216 {
3bdf5ad1 4217 target
28ed065e
MM
4218 = gen_rtx_MEM (TYPE_MODE (rettype),
4219 memory_address (TYPE_MODE (rettype),
3bdf5ad1 4220 structure_value_addr));
28ed065e 4221 set_mem_attributes (target, rettype, 1);
0a1c58a2
JL
4222 }
4223 }
4224 else if (pcc_struct_value)
cacbd532 4225 {
0a1c58a2
JL
4226 /* This is the special C++ case where we need to
4227 know what the true target was. We take care to
4228 never use this value more than once in one expression. */
28ed065e 4229 target = gen_rtx_MEM (TYPE_MODE (rettype),
0a1c58a2 4230 copy_to_reg (valreg));
28ed065e 4231 set_mem_attributes (target, rettype, 1);
cacbd532 4232 }
0a1c58a2
JL
4233 /* Handle calls that return values in multiple non-contiguous locations.
4234 The Irix 6 ABI has examples of this. */
4235 else if (GET_CODE (valreg) == PARALLEL)
4236 {
6de9cd9a 4237 if (target == 0)
5ef0b50d 4238 target = emit_group_move_into_temps (valreg);
1d1b7dc4
RS
4239 else if (rtx_equal_p (target, valreg))
4240 ;
4241 else if (GET_CODE (target) == PARALLEL)
4242 /* Handle the result of a emit_group_move_into_temps
4243 call in the previous pass. */
4244 emit_group_move (target, valreg);
4245 else
28ed065e
MM
4246 emit_group_store (target, valreg, rettype,
4247 int_size_in_bytes (rettype));
0a1c58a2
JL
4248 }
4249 else if (target
28ed065e 4250 && GET_MODE (target) == TYPE_MODE (rettype)
0a1c58a2
JL
4251 && GET_MODE (target) == GET_MODE (valreg))
4252 {
51caaefe
EB
4253 bool may_overlap = false;
4254
f2d18690
KK
4255 /* We have to copy a return value in a CLASS_LIKELY_SPILLED hard
4256 reg to a plain register. */
3fb30019
RS
4257 if (!REG_P (target) || HARD_REGISTER_P (target))
4258 valreg = avoid_likely_spilled_reg (valreg);
f2d18690 4259
51caaefe
EB
4260 /* If TARGET is a MEM in the argument area, and we have
4261 saved part of the argument area, then we can't store
4262 directly into TARGET as it may get overwritten when we
4263 restore the argument save area below. Don't work too
4264 hard though and simply force TARGET to a register if it
4265 is a MEM; the optimizer is quite likely to sort it out. */
4266 if (ACCUMULATE_OUTGOING_ARGS && pass && MEM_P (target))
4267 for (i = 0; i < num_actuals; i++)
4268 if (args[i].save_area)
4269 {
4270 may_overlap = true;
4271 break;
4272 }
0219237c 4273
51caaefe
EB
4274 if (may_overlap)
4275 target = copy_to_reg (valreg);
4276 else
4277 {
4278 /* TARGET and VALREG cannot be equal at this point
4279 because the latter would not have
4280 REG_FUNCTION_VALUE_P true, while the former would if
4281 it were referring to the same register.
4282
4283 If they refer to the same register, this move will be
4284 a no-op, except when function inlining is being
4285 done. */
4286 emit_move_insn (target, valreg);
4287
4288 /* If we are setting a MEM, this code must be executed.
4289 Since it is emitted after the call insn, sibcall
4290 optimization cannot be performed in that case. */
4291 if (MEM_P (target))
4292 sibcall_failure = 1;
4293 }
0a1c58a2 4294 }
0a1c58a2 4295 else
3fb30019 4296 target = copy_to_reg (avoid_likely_spilled_reg (valreg));
51bbfa0c 4297
cde0f3fd
PB
4298 /* If we promoted this return value, make the proper SUBREG.
4299 TARGET might be const0_rtx here, so be careful. */
4300 if (REG_P (target)
28ed065e
MM
4301 && TYPE_MODE (rettype) != BLKmode
4302 && GET_MODE (target) != TYPE_MODE (rettype))
61f71b34 4303 {
28ed065e 4304 tree type = rettype;
cde0f3fd 4305 int unsignedp = TYPE_UNSIGNED (type);
ef4bddc2 4306 machine_mode pmode;
cde0f3fd
PB
4307
4308 /* Ensure we promote as expected, and get the new unsignedness. */
4309 pmode = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
4310 funtype, 1);
4311 gcc_assert (GET_MODE (target) == pmode);
4312
610c45fc
RS
4313 unsigned int offset = subreg_lowpart_offset (TYPE_MODE (type),
4314 GET_MODE (target));
cde0f3fd
PB
4315 target = gen_rtx_SUBREG (TYPE_MODE (type), target, offset);
4316 SUBREG_PROMOTED_VAR_P (target) = 1;
362d42dc 4317 SUBREG_PROMOTED_SET (target, unsignedp);
61f71b34 4318 }
84b55618 4319
0a1c58a2
JL
4320 /* If size of args is variable or this was a constructor call for a stack
4321 argument, restore saved stack-pointer value. */
51bbfa0c 4322
9dd9bf80 4323 if (old_stack_level)
0a1c58a2 4324 {
48810515 4325 rtx_insn *prev = get_last_insn ();
9a08d230 4326
9eac0f2a 4327 emit_stack_restore (SAVE_BLOCK, old_stack_level);
38afb23f 4328 stack_pointer_delta = old_stack_pointer_delta;
9a08d230 4329
faf7a23d 4330 fixup_args_size_notes (prev, get_last_insn (), stack_pointer_delta);
9a08d230 4331
0a1c58a2 4332 pending_stack_adjust = old_pending_adj;
d25cee4d 4333 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
0a1c58a2
JL
4334 stack_arg_under_construction = old_stack_arg_under_construction;
4335 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
4336 stack_usage_map = initial_stack_usage_map;
0a1c58a2
JL
4337 sibcall_failure = 1;
4338 }
f8a097cd 4339 else if (ACCUMULATE_OUTGOING_ARGS && pass)
0a1c58a2 4340 {
51bbfa0c 4341#ifdef REG_PARM_STACK_SPACE
0a1c58a2 4342 if (save_area)
b820d2b8
AM
4343 restore_fixed_argument_area (save_area, argblock,
4344 high_to_save, low_to_save);
b94301c2 4345#endif
51bbfa0c 4346
0a1c58a2
JL
4347 /* If we saved any argument areas, restore them. */
4348 for (i = 0; i < num_actuals; i++)
4349 if (args[i].save_area)
4350 {
ef4bddc2 4351 machine_mode save_mode = GET_MODE (args[i].save_area);
0a1c58a2
JL
4352 rtx stack_area
4353 = gen_rtx_MEM (save_mode,
4354 memory_address (save_mode,
4355 XEXP (args[i].stack_slot, 0)));
4356
4357 if (save_mode != BLKmode)
4358 emit_move_insn (stack_area, args[i].save_area);
4359 else
44bb111a 4360 emit_block_move (stack_area, args[i].save_area,
e7949876 4361 GEN_INT (args[i].locate.size.constant),
44bb111a 4362 BLOCK_OP_CALL_PARM);
0a1c58a2 4363 }
51bbfa0c 4364
0a1c58a2
JL
4365 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
4366 stack_usage_map = initial_stack_usage_map;
4367 }
51bbfa0c 4368
d33606c3
EB
4369 /* If this was alloca, record the new stack level. */
4370 if (flags & ECF_MAY_BE_ALLOCA)
4371 record_new_stack_level ();
51bbfa0c 4372
0a1c58a2
JL
4373 /* Free up storage we no longer need. */
4374 for (i = 0; i < num_actuals; ++i)
04695783 4375 free (args[i].aligned_regs);
0a1c58a2 4376
2f21e1ba
BS
4377 targetm.calls.end_call_args ();
4378
0a1c58a2
JL
4379 insns = get_insns ();
4380 end_sequence ();
4381
4382 if (pass == 0)
4383 {
4384 tail_call_insns = insns;
4385
0a1c58a2
JL
4386 /* Restore the pending stack adjustment now that we have
4387 finished generating the sibling call sequence. */
1503a7ec 4388
7f2f0a01 4389 restore_pending_stack_adjust (&save);
099e9712
JH
4390
4391 /* Prepare arg structure for next iteration. */
f725a3ec 4392 for (i = 0; i < num_actuals; i++)
099e9712
JH
4393 {
4394 args[i].value = 0;
4395 args[i].aligned_regs = 0;
4396 args[i].stack = 0;
4397 }
c67846f2
JJ
4398
4399 sbitmap_free (stored_args_map);
48810515 4400 internal_arg_pointer_exp_state.scan_start = NULL;
9771b263 4401 internal_arg_pointer_exp_state.cache.release ();
0a1c58a2
JL
4402 }
4403 else
38afb23f
OH
4404 {
4405 normal_call_insns = insns;
4406
4407 /* Verify that we've deallocated all the stack we used. */
6e14af16 4408 gcc_assert ((flags & ECF_NORETURN)
366de0ce
NS
4409 || (old_stack_allocated
4410 == stack_pointer_delta - pending_stack_adjust));
38afb23f 4411 }
fadb729c
JJ
4412
4413 /* If something prevents making this a sibling call,
4414 zero out the sequence. */
4415 if (sibcall_failure)
48810515 4416 tail_call_insns = NULL;
6de9cd9a
DN
4417 else
4418 break;
0a1c58a2
JL
4419 }
4420
1ea7e6ad 4421 /* If tail call production succeeded, we need to remove REG_EQUIV notes on
6de9cd9a
DN
4422 arguments too, as argument area is now clobbered by the call. */
4423 if (tail_call_insns)
0a1c58a2 4424 {
6de9cd9a 4425 emit_insn (tail_call_insns);
e3b5732b 4426 crtl->tail_call_emit = true;
0a1c58a2
JL
4427 }
4428 else
9a385c2d
DM
4429 {
4430 emit_insn (normal_call_insns);
4431 if (try_tail_call)
4432 /* Ideally we'd emit a message for all of the ways that it could
4433 have failed. */
4434 maybe_complain_about_tail_call (exp, "tail call production failed");
4435 }
51bbfa0c 4436
0a1c58a2 4437 currently_expanding_call--;
8e6a59fe 4438
04695783 4439 free (stack_usage_map_buf);
765fc0f7 4440 free (args);
d9725c41 4441
d5e254e1
IE
4442 /* Join result with returned bounds so caller may use them if needed. */
4443 target = chkp_join_splitted_slot (target, valbnd);
4444
51bbfa0c
RS
4445 return target;
4446}
ded9bf77 4447
6de9cd9a
DN
4448/* A sibling call sequence invalidates any REG_EQUIV notes made for
4449 this function's incoming arguments.
4450
4451 At the start of RTL generation we know the only REG_EQUIV notes
29d51cdb
SB
4452 in the rtl chain are those for incoming arguments, so we can look
4453 for REG_EQUIV notes between the start of the function and the
4454 NOTE_INSN_FUNCTION_BEG.
6de9cd9a
DN
4455
4456 This is (slight) overkill. We could keep track of the highest
4457 argument we clobber and be more selective in removing notes, but it
4458 does not seem to be worth the effort. */
29d51cdb 4459
6de9cd9a
DN
4460void
4461fixup_tail_calls (void)
4462{
48810515 4463 rtx_insn *insn;
29d51cdb
SB
4464
4465 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4466 {
a31830a7
SB
4467 rtx note;
4468
29d51cdb
SB
4469 /* There are never REG_EQUIV notes for the incoming arguments
4470 after the NOTE_INSN_FUNCTION_BEG note, so stop if we see it. */
4471 if (NOTE_P (insn)
a38e7aa5 4472 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
29d51cdb
SB
4473 break;
4474
a31830a7
SB
4475 note = find_reg_note (insn, REG_EQUIV, 0);
4476 if (note)
4477 remove_note (insn, note);
4478 note = find_reg_note (insn, REG_EQUIV, 0);
4479 gcc_assert (!note);
29d51cdb 4480 }
6de9cd9a
DN
4481}
4482
ded9bf77
AH
4483/* Traverse a list of TYPES and expand all complex types into their
4484 components. */
2f2b4a02 4485static tree
ded9bf77
AH
4486split_complex_types (tree types)
4487{
4488 tree p;
4489
42ba5130
RH
4490 /* Before allocating memory, check for the common case of no complex. */
4491 for (p = types; p; p = TREE_CHAIN (p))
4492 {
4493 tree type = TREE_VALUE (p);
4494 if (TREE_CODE (type) == COMPLEX_TYPE
4495 && targetm.calls.split_complex_arg (type))
c22cacf3 4496 goto found;
42ba5130
RH
4497 }
4498 return types;
4499
4500 found:
ded9bf77
AH
4501 types = copy_list (types);
4502
4503 for (p = types; p; p = TREE_CHAIN (p))
4504 {
4505 tree complex_type = TREE_VALUE (p);
4506
42ba5130
RH
4507 if (TREE_CODE (complex_type) == COMPLEX_TYPE
4508 && targetm.calls.split_complex_arg (complex_type))
ded9bf77
AH
4509 {
4510 tree next, imag;
4511
4512 /* Rewrite complex type with component type. */
4513 TREE_VALUE (p) = TREE_TYPE (complex_type);
4514 next = TREE_CHAIN (p);
4515
4516 /* Add another component type for the imaginary part. */
4517 imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
4518 TREE_CHAIN (p) = imag;
4519 TREE_CHAIN (imag) = next;
4520
4521 /* Skip the newly created node. */
4522 p = TREE_CHAIN (p);
4523 }
4524 }
4525
4526 return types;
4527}
51bbfa0c 4528\f
db69559b
RS
4529/* Output a library call to function ORGFUN (a SYMBOL_REF rtx)
4530 for a value of mode OUTMODE,
4531 with NARGS different arguments, passed as ARGS.
4532 Store the return value if RETVAL is nonzero: store it in VALUE if
4533 VALUE is nonnull, otherwise pick a convenient location. In either
4534 case return the location of the stored value.
8ac61af7 4535
db69559b
RS
4536 FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for
4537 `const' calls, LCT_PURE for `pure' calls, or another LCT_ value for
4538 other types of library calls. */
4539
4540rtx
d329e058
AJ
4541emit_library_call_value_1 (int retval, rtx orgfun, rtx value,
4542 enum libcall_type fn_type,
db69559b 4543 machine_mode outmode, int nargs, rtx_mode_t *args)
43bc5f13 4544{
3c0fca12
RH
4545 /* Total size in bytes of all the stack-parms scanned so far. */
4546 struct args_size args_size;
4547 /* Size of arguments before any adjustments (such as rounding). */
4548 struct args_size original_args_size;
b3694847 4549 int argnum;
3c0fca12 4550 rtx fun;
81464b2c
KT
4551 /* Todo, choose the correct decl type of orgfun. Sadly this information
4552 isn't present here, so we default to native calling abi here. */
033df0b9 4553 tree fndecl ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
5d059ed9 4554 tree fntype ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
3c0fca12 4555 int count;
3c0fca12 4556 rtx argblock = 0;
d5cc9181
JR
4557 CUMULATIVE_ARGS args_so_far_v;
4558 cumulative_args_t args_so_far;
f725a3ec
KH
4559 struct arg
4560 {
4561 rtx value;
ef4bddc2 4562 machine_mode mode;
f725a3ec
KH
4563 rtx reg;
4564 int partial;
e7949876 4565 struct locate_and_pad_arg_data locate;
f725a3ec
KH
4566 rtx save_area;
4567 };
3c0fca12
RH
4568 struct arg *argvec;
4569 int old_inhibit_defer_pop = inhibit_defer_pop;
4570 rtx call_fusage = 0;
4571 rtx mem_value = 0;
5591ee6f 4572 rtx valreg;
3c0fca12
RH
4573 int pcc_struct_value = 0;
4574 int struct_value_size = 0;
52a11cbf 4575 int flags;
3c0fca12 4576 int reg_parm_stack_space = 0;
3c0fca12 4577 int needed;
48810515 4578 rtx_insn *before_call;
0ed4bf92 4579 bool have_push_fusage;
b0c48229 4580 tree tfom; /* type_for_mode (outmode, 0) */
3c0fca12 4581
f73ad30e 4582#ifdef REG_PARM_STACK_SPACE
3c0fca12
RH
4583 /* Define the boundary of the register parm stack space that needs to be
4584 save, if any. */
726a989a 4585 int low_to_save = 0, high_to_save = 0;
f725a3ec 4586 rtx save_area = 0; /* Place that it is saved. */
3c0fca12
RH
4587#endif
4588
3c0fca12
RH
4589 /* Size of the stack reserved for parameter registers. */
4590 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
4591 char *initial_stack_usage_map = stack_usage_map;
d9725c41 4592 char *stack_usage_map_buf = NULL;
3c0fca12 4593
61f71b34
DD
4594 rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
4595
3c0fca12 4596#ifdef REG_PARM_STACK_SPACE
3c0fca12 4597 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
3c0fca12
RH
4598#endif
4599
0529235d 4600 /* By default, library functions cannot throw. */
52a11cbf
RH
4601 flags = ECF_NOTHROW;
4602
9555a122
RH
4603 switch (fn_type)
4604 {
4605 case LCT_NORMAL:
53d4257f 4606 break;
9555a122 4607 case LCT_CONST:
53d4257f
JH
4608 flags |= ECF_CONST;
4609 break;
9555a122 4610 case LCT_PURE:
53d4257f 4611 flags |= ECF_PURE;
9555a122 4612 break;
9555a122
RH
4613 case LCT_NORETURN:
4614 flags |= ECF_NORETURN;
4615 break;
4616 case LCT_THROW:
0529235d 4617 flags &= ~ECF_NOTHROW;
9555a122 4618 break;
9defc9b7
RH
4619 case LCT_RETURNS_TWICE:
4620 flags = ECF_RETURNS_TWICE;
4621 break;
9555a122 4622 }
3c0fca12
RH
4623 fun = orgfun;
4624
3c0fca12
RH
4625 /* Ensure current function's preferred stack boundary is at least
4626 what we need. */
cb91fab0
JH
4627 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
4628 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3c0fca12
RH
4629
4630 /* If this kind of value comes back in memory,
4631 decide where in memory it should come back. */
b0c48229 4632 if (outmode != VOIDmode)
3c0fca12 4633 {
ae2bcd98 4634 tfom = lang_hooks.types.type_for_mode (outmode, 0);
61f71b34 4635 if (aggregate_value_p (tfom, 0))
b0c48229 4636 {
3c0fca12 4637#ifdef PCC_STATIC_STRUCT_RETURN
b0c48229 4638 rtx pointer_reg
1d636cc6 4639 = hard_function_value (build_pointer_type (tfom), 0, 0, 0);
b0c48229
NB
4640 mem_value = gen_rtx_MEM (outmode, pointer_reg);
4641 pcc_struct_value = 1;
4642 if (value == 0)
4643 value = gen_reg_rtx (outmode);
3c0fca12 4644#else /* not PCC_STATIC_STRUCT_RETURN */
b0c48229 4645 struct_value_size = GET_MODE_SIZE (outmode);
3c0cb5de 4646 if (value != 0 && MEM_P (value))
b0c48229
NB
4647 mem_value = value;
4648 else
9474e8ab 4649 mem_value = assign_temp (tfom, 1, 1);
3c0fca12 4650#endif
b0c48229 4651 /* This call returns a big structure. */
84b8030f 4652 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
b0c48229 4653 }
3c0fca12 4654 }
b0c48229
NB
4655 else
4656 tfom = void_type_node;
3c0fca12
RH
4657
4658 /* ??? Unfinished: must pass the memory address as an argument. */
4659
4660 /* Copy all the libcall-arguments out of the varargs data
4661 and into a vector ARGVEC.
4662
4663 Compute how to pass each argument. We only support a very small subset
4664 of the full argument passing conventions to limit complexity here since
4665 library functions shouldn't have many args. */
4666
f883e0a7 4667 argvec = XALLOCAVEC (struct arg, nargs + 1);
703ad42b 4668 memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
3c0fca12 4669
97fc4caf 4670#ifdef INIT_CUMULATIVE_LIBCALL_ARGS
d5cc9181 4671 INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far_v, outmode, fun);
97fc4caf 4672#else
d5cc9181 4673 INIT_CUMULATIVE_ARGS (args_so_far_v, NULL_TREE, fun, 0, nargs);
97fc4caf 4674#endif
d5cc9181 4675 args_so_far = pack_cumulative_args (&args_so_far_v);
3c0fca12
RH
4676
4677 args_size.constant = 0;
4678 args_size.var = 0;
4679
4680 count = 0;
4681
4682 push_temp_slots ();
4683
4684 /* If there's a structure value address to be passed,
4685 either pass it in the special place, or pass it as an extra argument. */
61f71b34 4686 if (mem_value && struct_value == 0 && ! pcc_struct_value)
3c0fca12
RH
4687 {
4688 rtx addr = XEXP (mem_value, 0);
c22cacf3 4689
3c0fca12
RH
4690 nargs++;
4691
ee88d9aa
MK
4692 /* Make sure it is a reasonable operand for a move or push insn. */
4693 if (!REG_P (addr) && !MEM_P (addr)
1a627b35
RS
4694 && !(CONSTANT_P (addr)
4695 && targetm.legitimate_constant_p (Pmode, addr)))
ee88d9aa
MK
4696 addr = force_operand (addr, NULL_RTX);
4697
3c0fca12
RH
4698 argvec[count].value = addr;
4699 argvec[count].mode = Pmode;
4700 argvec[count].partial = 0;
4701
d5cc9181 4702 argvec[count].reg = targetm.calls.function_arg (args_so_far,
3c07301f 4703 Pmode, NULL_TREE, true);
d5cc9181 4704 gcc_assert (targetm.calls.arg_partial_bytes (args_so_far, Pmode,
78a52f11 4705 NULL_TREE, 1) == 0);
3c0fca12
RH
4706
4707 locate_and_pad_parm (Pmode, NULL_TREE,
a4d5044f 4708#ifdef STACK_PARMS_IN_REG_PARM_AREA
c22cacf3 4709 1,
a4d5044f
CM
4710#else
4711 argvec[count].reg != 0,
4712#endif
2e4ceca5
UW
4713 reg_parm_stack_space, 0,
4714 NULL_TREE, &args_size, &argvec[count].locate);
3c0fca12 4715
3c0fca12
RH
4716 if (argvec[count].reg == 0 || argvec[count].partial != 0
4717 || reg_parm_stack_space > 0)
e7949876 4718 args_size.constant += argvec[count].locate.size.constant;
3c0fca12 4719
d5cc9181 4720 targetm.calls.function_arg_advance (args_so_far, Pmode, (tree) 0, true);
3c0fca12
RH
4721
4722 count++;
4723 }
4724
db69559b 4725 for (unsigned int i = 0; count < nargs; i++, count++)
3c0fca12 4726 {
db69559b
RS
4727 rtx val = args[i].first;
4728 machine_mode mode = args[i].second;
5e617be8 4729 int unsigned_p = 0;
3c0fca12
RH
4730
4731 /* We cannot convert the arg value to the mode the library wants here;
4732 must do it earlier where we know the signedness of the arg. */
366de0ce
NS
4733 gcc_assert (mode != BLKmode
4734 && (GET_MODE (val) == mode || GET_MODE (val) == VOIDmode));
3c0fca12 4735
ee88d9aa
MK
4736 /* Make sure it is a reasonable operand for a move or push insn. */
4737 if (!REG_P (val) && !MEM_P (val)
1a627b35 4738 && !(CONSTANT_P (val) && targetm.legitimate_constant_p (mode, val)))
ee88d9aa
MK
4739 val = force_operand (val, NULL_RTX);
4740
d5cc9181 4741 if (pass_by_reference (&args_so_far_v, mode, NULL_TREE, 1))
3c0fca12 4742 {
f474c6f8 4743 rtx slot;
6cdd5672 4744 int must_copy
d5cc9181 4745 = !reference_callee_copied (&args_so_far_v, mode, NULL_TREE, 1);
f474c6f8 4746
becfd6e5
KZ
4747 /* If this was a CONST function, it is now PURE since it now
4748 reads memory. */
99a32567
DM
4749 if (flags & ECF_CONST)
4750 {
4751 flags &= ~ECF_CONST;
4752 flags |= ECF_PURE;
4753 }
4754
e0c68ce9 4755 if (MEM_P (val) && !must_copy)
c4b9a87e
ER
4756 {
4757 tree val_expr = MEM_EXPR (val);
4758 if (val_expr)
4759 mark_addressable (val_expr);
4760 slot = val;
4761 }
9969aaf6 4762 else
f474c6f8 4763 {
ae2bcd98 4764 slot = assign_temp (lang_hooks.types.type_for_mode (mode, 0),
9474e8ab 4765 1, 1);
f474c6f8
AO
4766 emit_move_insn (slot, val);
4767 }
1da68f56 4768
6b5273c3
AO
4769 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4770 gen_rtx_USE (VOIDmode, slot),
4771 call_fusage);
f474c6f8
AO
4772 if (must_copy)
4773 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4774 gen_rtx_CLOBBER (VOIDmode,
4775 slot),
4776 call_fusage);
4777
3c0fca12 4778 mode = Pmode;
f474c6f8 4779 val = force_operand (XEXP (slot, 0), NULL_RTX);
3c0fca12 4780 }
3c0fca12 4781
5e617be8 4782 mode = promote_function_mode (NULL_TREE, mode, &unsigned_p, NULL_TREE, 0);
3c0fca12 4783 argvec[count].mode = mode;
5e617be8 4784 argvec[count].value = convert_modes (mode, GET_MODE (val), val, unsigned_p);
d5cc9181 4785 argvec[count].reg = targetm.calls.function_arg (args_so_far, mode,
3c07301f 4786 NULL_TREE, true);
3c0fca12 4787
3c0fca12 4788 argvec[count].partial
d5cc9181 4789 = targetm.calls.arg_partial_bytes (args_so_far, mode, NULL_TREE, 1);
3c0fca12 4790
3576f984
RS
4791 if (argvec[count].reg == 0
4792 || argvec[count].partial != 0
4793 || reg_parm_stack_space > 0)
4794 {
4795 locate_and_pad_parm (mode, NULL_TREE,
a4d5044f 4796#ifdef STACK_PARMS_IN_REG_PARM_AREA
3576f984 4797 1,
a4d5044f 4798#else
3576f984
RS
4799 argvec[count].reg != 0,
4800#endif
2e4ceca5 4801 reg_parm_stack_space, argvec[count].partial,
3576f984
RS
4802 NULL_TREE, &args_size, &argvec[count].locate);
4803 args_size.constant += argvec[count].locate.size.constant;
4804 gcc_assert (!argvec[count].locate.size.var);
4805 }
4806#ifdef BLOCK_REG_PADDING
4807 else
4808 /* The argument is passed entirely in registers. See at which
4809 end it should be padded. */
4810 argvec[count].locate.where_pad =
4811 BLOCK_REG_PADDING (mode, NULL_TREE,
4812 GET_MODE_SIZE (mode) <= UNITS_PER_WORD);
a4d5044f 4813#endif
3c0fca12 4814
d5cc9181 4815 targetm.calls.function_arg_advance (args_so_far, mode, (tree) 0, true);
3c0fca12 4816 }
3c0fca12 4817
3c0fca12
RH
4818 /* If this machine requires an external definition for library
4819 functions, write one out. */
4820 assemble_external_libcall (fun);
4821
4822 original_args_size = args_size;
1503a7ec
JH
4823 args_size.constant = (((args_size.constant
4824 + stack_pointer_delta
4825 + STACK_BYTES - 1)
4826 / STACK_BYTES
4827 * STACK_BYTES)
4828 - stack_pointer_delta);
3c0fca12
RH
4829
4830 args_size.constant = MAX (args_size.constant,
4831 reg_parm_stack_space);
4832
5d059ed9 4833 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
ac294f0b 4834 args_size.constant -= reg_parm_stack_space;
3c0fca12 4835
38173d38
JH
4836 if (args_size.constant > crtl->outgoing_args_size)
4837 crtl->outgoing_args_size = args_size.constant;
3c0fca12 4838
a11e0df4 4839 if (flag_stack_usage_info && !ACCUMULATE_OUTGOING_ARGS)
d3c12306
EB
4840 {
4841 int pushed = args_size.constant + pending_stack_adjust;
4842 if (pushed > current_function_pushed_stack_size)
4843 current_function_pushed_stack_size = pushed;
4844 }
4845
f73ad30e
JH
4846 if (ACCUMULATE_OUTGOING_ARGS)
4847 {
4848 /* Since the stack pointer will never be pushed, it is possible for
4849 the evaluation of a parm to clobber something we have already
4850 written to the stack. Since most function calls on RISC machines
4851 do not use the stack, this is uncommon, but must work correctly.
3c0fca12 4852
f73ad30e
JH
4853 Therefore, we save any area of the stack that was already written
4854 and that we are using. Here we set up to do this by making a new
4855 stack usage map from the old one.
3c0fca12 4856
f73ad30e
JH
4857 Another approach might be to try to reorder the argument
4858 evaluations to avoid this conflicting stack usage. */
3c0fca12 4859
f73ad30e 4860 needed = args_size.constant;
3c0fca12 4861
f73ad30e
JH
4862 /* Since we will be writing into the entire argument area, the
4863 map must be allocated for its entire size, not just the part that
4864 is the responsibility of the caller. */
5d059ed9 4865 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
ac294f0b 4866 needed += reg_parm_stack_space;
3c0fca12 4867
6dad9361
TS
4868 if (ARGS_GROW_DOWNWARD)
4869 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
4870 needed + 1);
4871 else
4872 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use, needed);
4873
5ed6ace5 4874 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
d9725c41 4875 stack_usage_map = stack_usage_map_buf;
3c0fca12 4876
f73ad30e 4877 if (initial_highest_arg_in_use)
2e09e75a
JM
4878 memcpy (stack_usage_map, initial_stack_usage_map,
4879 initial_highest_arg_in_use);
3c0fca12 4880
f73ad30e 4881 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
961192e1 4882 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
f73ad30e
JH
4883 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
4884 needed = 0;
3c0fca12 4885
c39ada04 4886 /* We must be careful to use virtual regs before they're instantiated,
c22cacf3 4887 and real regs afterwards. Loop optimization, for example, can create
c39ada04
DD
4888 new libcalls after we've instantiated the virtual regs, and if we
4889 use virtuals anyway, they won't match the rtl patterns. */
3c0fca12 4890
c39ada04 4891 if (virtuals_instantiated)
0a81f074
RS
4892 argblock = plus_constant (Pmode, stack_pointer_rtx,
4893 STACK_POINTER_OFFSET);
c39ada04
DD
4894 else
4895 argblock = virtual_outgoing_args_rtx;
f73ad30e
JH
4896 }
4897 else
4898 {
4899 if (!PUSH_ARGS)
4900 argblock = push_block (GEN_INT (args_size.constant), 0, 0);
4901 }
3c0fca12 4902
3d9684ae 4903 /* We push args individually in reverse order, perform stack alignment
3c0fca12 4904 before the first push (the last arg). */
3d9684ae 4905 if (argblock == 0)
3c0fca12
RH
4906 anti_adjust_stack (GEN_INT (args_size.constant
4907 - original_args_size.constant));
3c0fca12 4908
3d9684ae 4909 argnum = nargs - 1;
3c0fca12 4910
f73ad30e
JH
4911#ifdef REG_PARM_STACK_SPACE
4912 if (ACCUMULATE_OUTGOING_ARGS)
4913 {
4914 /* The argument list is the property of the called routine and it
4915 may clobber it. If the fixed area has been used for previous
b820d2b8
AM
4916 parameters, we must save and restore it. */
4917 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
4918 &low_to_save, &high_to_save);
3c0fca12
RH
4919 }
4920#endif
f725a3ec 4921
2f21e1ba
BS
4922 /* When expanding a normal call, args are stored in push order,
4923 which is the reverse of what we have here. */
4924 bool any_regs = false;
4925 for (int i = nargs; i-- > 0; )
4926 if (argvec[i].reg != NULL_RTX)
4927 {
4928 targetm.calls.call_args (argvec[i].reg, NULL_TREE);
4929 any_regs = true;
4930 }
4931 if (!any_regs)
4932 targetm.calls.call_args (pc_rtx, NULL_TREE);
4933
3c0fca12
RH
4934 /* Push the args that need to be pushed. */
4935
0ed4bf92
BS
4936 have_push_fusage = false;
4937
3c0fca12
RH
4938 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4939 are to be pushed. */
3d9684ae 4940 for (count = 0; count < nargs; count++, argnum--)
3c0fca12 4941 {
ef4bddc2 4942 machine_mode mode = argvec[argnum].mode;
b3694847 4943 rtx val = argvec[argnum].value;
3c0fca12
RH
4944 rtx reg = argvec[argnum].reg;
4945 int partial = argvec[argnum].partial;
6bdf8c2e 4946 unsigned int parm_align = argvec[argnum].locate.boundary;
f73ad30e 4947 int lower_bound = 0, upper_bound = 0, i;
3c0fca12
RH
4948
4949 if (! (reg != 0 && partial == 0))
4950 {
2b1c5433
JJ
4951 rtx use;
4952
f73ad30e
JH
4953 if (ACCUMULATE_OUTGOING_ARGS)
4954 {
f8a097cd
JH
4955 /* If this is being stored into a pre-allocated, fixed-size,
4956 stack area, save any previous data at that location. */
3c0fca12 4957
6dad9361
TS
4958 if (ARGS_GROW_DOWNWARD)
4959 {
4960 /* stack_slot is negative, but we want to index stack_usage_map
4961 with positive values. */
4962 upper_bound = -argvec[argnum].locate.slot_offset.constant + 1;
4963 lower_bound = upper_bound - argvec[argnum].locate.size.constant;
4964 }
4965 else
4966 {
4967 lower_bound = argvec[argnum].locate.slot_offset.constant;
4968 upper_bound = lower_bound + argvec[argnum].locate.size.constant;
4969 }
3c0fca12 4970
546ff777
AM
4971 i = lower_bound;
4972 /* Don't worry about things in the fixed argument area;
4973 it has already been saved. */
4974 if (i < reg_parm_stack_space)
4975 i = reg_parm_stack_space;
4976 while (i < upper_bound && stack_usage_map[i] == 0)
4977 i++;
3c0fca12 4978
546ff777 4979 if (i < upper_bound)
f73ad30e 4980 {
e7949876
AM
4981 /* We need to make a save area. */
4982 unsigned int size
4983 = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
ef4bddc2 4984 machine_mode save_mode
f4b31647 4985 = int_mode_for_size (size, 1).else_blk ();
e7949876 4986 rtx adr
0a81f074 4987 = plus_constant (Pmode, argblock,
e7949876 4988 argvec[argnum].locate.offset.constant);
f73ad30e 4989 rtx stack_area
e7949876 4990 = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
f73ad30e 4991
9778f2f8
JH
4992 if (save_mode == BLKmode)
4993 {
4994 argvec[argnum].save_area
4995 = assign_stack_temp (BLKmode,
9474e8ab
MM
4996 argvec[argnum].locate.size.constant
4997 );
9778f2f8 4998
1a8cb155
RS
4999 emit_block_move (validize_mem
5000 (copy_rtx (argvec[argnum].save_area)),
c22cacf3 5001 stack_area,
9778f2f8
JH
5002 GEN_INT (argvec[argnum].locate.size.constant),
5003 BLOCK_OP_CALL_PARM);
5004 }
5005 else
5006 {
5007 argvec[argnum].save_area = gen_reg_rtx (save_mode);
5008
5009 emit_move_insn (argvec[argnum].save_area, stack_area);
5010 }
f73ad30e 5011 }
3c0fca12 5012 }
19caa751 5013
6bdf8c2e 5014 emit_push_insn (val, mode, NULL_TREE, NULL_RTX, parm_align,
44bb111a 5015 partial, reg, 0, argblock,
e7949876
AM
5016 GEN_INT (argvec[argnum].locate.offset.constant),
5017 reg_parm_stack_space,
99206968 5018 ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad), false);
3c0fca12 5019
3c0fca12 5020 /* Now mark the segment we just used. */
f73ad30e
JH
5021 if (ACCUMULATE_OUTGOING_ARGS)
5022 for (i = lower_bound; i < upper_bound; i++)
5023 stack_usage_map[i] = 1;
3c0fca12
RH
5024
5025 NO_DEFER_POP;
475a3eef 5026
2b1c5433
JJ
5027 /* Indicate argument access so that alias.c knows that these
5028 values are live. */
5029 if (argblock)
0a81f074 5030 use = plus_constant (Pmode, argblock,
2b1c5433 5031 argvec[argnum].locate.offset.constant);
0ed4bf92
BS
5032 else if (have_push_fusage)
5033 continue;
2b1c5433 5034 else
0ed4bf92
BS
5035 {
5036 /* When arguments are pushed, trying to tell alias.c where
5037 exactly this argument is won't work, because the
5038 auto-increment causes confusion. So we merely indicate
5039 that we access something with a known mode somewhere on
5040 the stack. */
5041 use = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
5042 gen_rtx_SCRATCH (Pmode));
5043 have_push_fusage = true;
5044 }
2b1c5433
JJ
5045 use = gen_rtx_MEM (argvec[argnum].mode, use);
5046 use = gen_rtx_USE (VOIDmode, use);
5047 call_fusage = gen_rtx_EXPR_LIST (VOIDmode, use, call_fusage);
3c0fca12
RH
5048 }
5049 }
5050
3d9684ae 5051 argnum = nargs - 1;
3c0fca12 5052
531ca746 5053 fun = prepare_call_address (NULL, fun, NULL, &call_fusage, 0, 0);
3c0fca12
RH
5054
5055 /* Now load any reg parms into their regs. */
5056
5057 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
5058 are to be pushed. */
3d9684ae 5059 for (count = 0; count < nargs; count++, argnum--)
3c0fca12 5060 {
ef4bddc2 5061 machine_mode mode = argvec[argnum].mode;
b3694847 5062 rtx val = argvec[argnum].value;
3c0fca12
RH
5063 rtx reg = argvec[argnum].reg;
5064 int partial = argvec[argnum].partial;
ee222ce0 5065#ifdef BLOCK_REG_PADDING
460b171d 5066 int size = 0;
ee222ce0 5067#endif
460b171d 5068
3c0fca12
RH
5069 /* Handle calls that pass values in multiple non-contiguous
5070 locations. The PA64 has examples of this for library calls. */
5071 if (reg != 0 && GET_CODE (reg) == PARALLEL)
ff15c351 5072 emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (mode));
3c0fca12 5073 else if (reg != 0 && partial == 0)
460b171d
JB
5074 {
5075 emit_move_insn (reg, val);
5076#ifdef BLOCK_REG_PADDING
5077 size = GET_MODE_SIZE (argvec[argnum].mode);
5078
5079 /* Copied from load_register_parameters. */
5080
5081 /* Handle case where we have a value that needs shifting
5082 up to the msb. eg. a QImode value and we're padding
5083 upward on a BYTES_BIG_ENDIAN machine. */
5084 if (size < UNITS_PER_WORD
5085 && (argvec[argnum].locate.where_pad
76b0cbf8 5086 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)))
460b171d
JB
5087 {
5088 rtx x;
5089 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
5090
5091 /* Assigning REG here rather than a temp makes CALL_FUSAGE
5092 report the whole reg as used. Strictly speaking, the
5093 call only uses SIZE bytes at the msb end, but it doesn't
5094 seem worth generating rtl to say that. */
5095 reg = gen_rtx_REG (word_mode, REGNO (reg));
5096 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
5097 if (x != reg)
5098 emit_move_insn (reg, x);
5099 }
5100#endif
5101 }
3c0fca12
RH
5102
5103 NO_DEFER_POP;
5104 }
5105
3c0fca12
RH
5106 /* Any regs containing parms remain in use through the call. */
5107 for (count = 0; count < nargs; count++)
5108 {
5109 rtx reg = argvec[count].reg;
5110 if (reg != 0 && GET_CODE (reg) == PARALLEL)
5111 use_group_regs (&call_fusage, reg);
5112 else if (reg != 0)
3b1bf459
BS
5113 {
5114 int partial = argvec[count].partial;
5115 if (partial)
5116 {
5117 int nregs;
5118 gcc_assert (partial % UNITS_PER_WORD == 0);
5119 nregs = partial / UNITS_PER_WORD;
5120 use_regs (&call_fusage, REGNO (reg), nregs);
5121 }
5122 else
5123 use_reg (&call_fusage, reg);
5124 }
3c0fca12
RH
5125 }
5126
5127 /* Pass the function the address in which to return a structure value. */
61f71b34 5128 if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
3c0fca12 5129 {
61f71b34 5130 emit_move_insn (struct_value,
3c0fca12
RH
5131 force_reg (Pmode,
5132 force_operand (XEXP (mem_value, 0),
5133 NULL_RTX)));
f8cfc6aa 5134 if (REG_P (struct_value))
61f71b34 5135 use_reg (&call_fusage, struct_value);
3c0fca12
RH
5136 }
5137
5138 /* Don't allow popping to be deferred, since then
5139 cse'ing of library calls could delete a call and leave the pop. */
5140 NO_DEFER_POP;
5591ee6f 5141 valreg = (mem_value == 0 && outmode != VOIDmode
390b17c2 5142 ? hard_libcall_value (outmode, orgfun) : NULL_RTX);
3c0fca12 5143
ce48579b 5144 /* Stack must be properly aligned now. */
366de0ce
NS
5145 gcc_assert (!(stack_pointer_delta
5146 & (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1)));
ebcd0b57 5147
695ee791
RH
5148 before_call = get_last_insn ();
5149
3c0fca12
RH
5150 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
5151 will set inhibit_defer_pop to that value. */
de76b467
JH
5152 /* The return type is needed to decide how many bytes the function pops.
5153 Signedness plays no role in that, so for simplicity, we pretend it's
5154 always signed. We also assume that the list of arguments passed has
5155 no impact, so we pretend it is unknown. */
3c0fca12 5156
6de9cd9a 5157 emit_call_1 (fun, NULL,
f725a3ec 5158 get_identifier (XSTR (orgfun, 0)),
b0c48229 5159 build_function_type (tfom, NULL_TREE),
f725a3ec 5160 original_args_size.constant, args_size.constant,
3c0fca12 5161 struct_value_size,
d5cc9181 5162 targetm.calls.function_arg (args_so_far,
3c07301f 5163 VOIDmode, void_type_node, true),
5591ee6f 5164 valreg,
d5cc9181 5165 old_inhibit_defer_pop + 1, call_fusage, flags, args_so_far);
3c0fca12 5166
1e288103 5167 if (flag_ipa_ra)
4f660b15 5168 {
e67d1102 5169 rtx datum = orgfun;
4f660b15 5170 gcc_assert (GET_CODE (datum) == SYMBOL_REF);
e67d1102 5171 rtx_call_insn *last = last_call_insn ();
4f660b15
RO
5172 add_reg_note (last, REG_CALL_DECL, datum);
5173 }
5174
460b171d
JB
5175 /* Right-shift returned value if necessary. */
5176 if (!pcc_struct_value
5177 && TYPE_MODE (tfom) != BLKmode
5178 && targetm.calls.return_in_msb (tfom))
5179 {
5180 shift_return_value (TYPE_MODE (tfom), false, valreg);
5181 valreg = gen_rtx_REG (TYPE_MODE (tfom), REGNO (valreg));
5182 }
5183
2f21e1ba
BS
5184 targetm.calls.end_call_args ();
5185
6fb5fa3c
DB
5186 /* For calls to `setjmp', etc., inform function.c:setjmp_warnings
5187 that it should complain if nonvolatile values are live. For
5188 functions that cannot return, inform flow that control does not
5189 fall through. */
6e14af16 5190 if (flags & ECF_NORETURN)
695ee791 5191 {
570a98eb 5192 /* The barrier note must be emitted
695ee791
RH
5193 immediately after the CALL_INSN. Some ports emit more than
5194 just a CALL_INSN above, so we must search for it here. */
48810515 5195 rtx_insn *last = get_last_insn ();
4b4bf941 5196 while (!CALL_P (last))
695ee791
RH
5197 {
5198 last = PREV_INSN (last);
5199 /* There was no CALL_INSN? */
366de0ce 5200 gcc_assert (last != before_call);
695ee791
RH
5201 }
5202
570a98eb 5203 emit_barrier_after (last);
695ee791
RH
5204 }
5205
85da11a6
EB
5206 /* Consider that "regular" libcalls, i.e. all of them except for LCT_THROW
5207 and LCT_RETURNS_TWICE, cannot perform non-local gotos. */
5208 if (flags & ECF_NOTHROW)
5209 {
48810515 5210 rtx_insn *last = get_last_insn ();
85da11a6
EB
5211 while (!CALL_P (last))
5212 {
5213 last = PREV_INSN (last);
5214 /* There was no CALL_INSN? */
5215 gcc_assert (last != before_call);
5216 }
5217
5218 make_reg_eh_region_note_nothrow_nononlocal (last);
5219 }
5220
3c0fca12
RH
5221 /* Now restore inhibit_defer_pop to its actual original value. */
5222 OK_DEFER_POP;
5223
5224 pop_temp_slots ();
5225
5226 /* Copy the value to the right place. */
de76b467 5227 if (outmode != VOIDmode && retval)
3c0fca12
RH
5228 {
5229 if (mem_value)
5230 {
5231 if (value == 0)
5232 value = mem_value;
5233 if (value != mem_value)
5234 emit_move_insn (value, mem_value);
5235 }
c3297561
AO
5236 else if (GET_CODE (valreg) == PARALLEL)
5237 {
5238 if (value == 0)
5239 value = gen_reg_rtx (outmode);
643642eb 5240 emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
c3297561 5241 }
3c0fca12 5242 else
7ab0aca2 5243 {
cde0f3fd 5244 /* Convert to the proper mode if a promotion has been active. */
7ab0aca2
RH
5245 if (GET_MODE (valreg) != outmode)
5246 {
5247 int unsignedp = TYPE_UNSIGNED (tfom);
5248
cde0f3fd
PB
5249 gcc_assert (promote_function_mode (tfom, outmode, &unsignedp,
5250 fndecl ? TREE_TYPE (fndecl) : fntype, 1)
7ab0aca2 5251 == GET_MODE (valreg));
7ab0aca2
RH
5252 valreg = convert_modes (outmode, GET_MODE (valreg), valreg, 0);
5253 }
5254
5255 if (value != 0)
5256 emit_move_insn (value, valreg);
5257 else
5258 value = valreg;
5259 }
3c0fca12
RH
5260 }
5261
f73ad30e 5262 if (ACCUMULATE_OUTGOING_ARGS)
3c0fca12 5263 {
f73ad30e
JH
5264#ifdef REG_PARM_STACK_SPACE
5265 if (save_area)
b820d2b8
AM
5266 restore_fixed_argument_area (save_area, argblock,
5267 high_to_save, low_to_save);
3c0fca12 5268#endif
f725a3ec 5269
f73ad30e
JH
5270 /* If we saved any argument areas, restore them. */
5271 for (count = 0; count < nargs; count++)
5272 if (argvec[count].save_area)
5273 {
ef4bddc2 5274 machine_mode save_mode = GET_MODE (argvec[count].save_area);
0a81f074 5275 rtx adr = plus_constant (Pmode, argblock,
e7949876
AM
5276 argvec[count].locate.offset.constant);
5277 rtx stack_area = gen_rtx_MEM (save_mode,
5278 memory_address (save_mode, adr));
f73ad30e 5279
9778f2f8
JH
5280 if (save_mode == BLKmode)
5281 emit_block_move (stack_area,
1a8cb155
RS
5282 validize_mem
5283 (copy_rtx (argvec[count].save_area)),
9778f2f8
JH
5284 GEN_INT (argvec[count].locate.size.constant),
5285 BLOCK_OP_CALL_PARM);
5286 else
5287 emit_move_insn (stack_area, argvec[count].save_area);
f73ad30e 5288 }
3c0fca12 5289
f73ad30e
JH
5290 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
5291 stack_usage_map = initial_stack_usage_map;
5292 }
43bc5f13 5293
04695783 5294 free (stack_usage_map_buf);
d9725c41 5295
de76b467
JH
5296 return value;
5297
5298}
5299\f
d5e254e1
IE
5300
5301/* Store pointer bounds argument ARG into Bounds Table entry
5302 associated with PARM. */
5303static void
5304store_bounds (struct arg_data *arg, struct arg_data *parm)
5305{
5306 rtx slot = NULL, ptr = NULL, addr = NULL;
5307
5308 /* We may pass bounds not associated with any pointer. */
5309 if (!parm)
5310 {
5311 gcc_assert (arg->special_slot);
5312 slot = arg->special_slot;
5313 ptr = const0_rtx;
5314 }
5315 /* Find pointer associated with bounds and where it is
5316 passed. */
5317 else
5318 {
5319 if (!parm->reg)
5320 {
5321 gcc_assert (!arg->special_slot);
5322
5323 addr = adjust_address (parm->stack, Pmode, arg->pointer_offset);
5324 }
5325 else if (REG_P (parm->reg))
5326 {
5327 gcc_assert (arg->special_slot);
5328 slot = arg->special_slot;
5329
5330 if (MEM_P (parm->value))
5331 addr = adjust_address (parm->value, Pmode, arg->pointer_offset);
5332 else if (REG_P (parm->value))
5333 ptr = gen_rtx_SUBREG (Pmode, parm->value, arg->pointer_offset);
5334 else
5335 {
5336 gcc_assert (!arg->pointer_offset);
5337 ptr = parm->value;
5338 }
5339 }
5340 else
5341 {
5342 gcc_assert (GET_CODE (parm->reg) == PARALLEL);
5343
5344 gcc_assert (arg->special_slot);
5345 slot = arg->special_slot;
5346
5347 if (parm->parallel_value)
5348 ptr = chkp_get_value_with_offs (parm->parallel_value,
5349 GEN_INT (arg->pointer_offset));
5350 else
5351 gcc_unreachable ();
5352 }
5353 }
5354
5355 /* Expand bounds. */
5356 if (!arg->value)
5357 arg->value = expand_normal (arg->tree_value);
5358
5359 targetm.calls.store_bounds_for_arg (ptr, addr, arg->value, slot);
5360}
5361
51bbfa0c
RS
5362/* Store a single argument for a function call
5363 into the register or memory area where it must be passed.
5364 *ARG describes the argument value and where to pass it.
5365
5366 ARGBLOCK is the address of the stack-block for all the arguments,
d45cf215 5367 or 0 on a machine where arguments are pushed individually.
51bbfa0c
RS
5368
5369 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
f725a3ec 5370 so must be careful about how the stack is used.
51bbfa0c
RS
5371
5372 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
5373 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
5374 that we need not worry about saving and restoring the stack.
5375
4c6b3b2a 5376 FNDECL is the declaration of the function we are calling.
f725a3ec 5377
da7d8304 5378 Return nonzero if this arg should cause sibcall failure,
4c6b3b2a 5379 zero otherwise. */
51bbfa0c 5380
4c6b3b2a 5381static int
d329e058
AJ
5382store_one_arg (struct arg_data *arg, rtx argblock, int flags,
5383 int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
51bbfa0c 5384{
b3694847 5385 tree pval = arg->tree_value;
51bbfa0c
RS
5386 rtx reg = 0;
5387 int partial = 0;
5388 int used = 0;
6a651371 5389 int i, lower_bound = 0, upper_bound = 0;
4c6b3b2a 5390 int sibcall_failure = 0;
51bbfa0c
RS
5391
5392 if (TREE_CODE (pval) == ERROR_MARK)
4c6b3b2a 5393 return 1;
51bbfa0c 5394
cc79451b
RK
5395 /* Push a new temporary level for any temporaries we make for
5396 this argument. */
5397 push_temp_slots ();
5398
f8a097cd 5399 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
51bbfa0c 5400 {
f73ad30e
JH
5401 /* If this is being stored into a pre-allocated, fixed-size, stack area,
5402 save any previous data at that location. */
5403 if (argblock && ! variable_size && arg->stack)
5404 {
6dad9361
TS
5405 if (ARGS_GROW_DOWNWARD)
5406 {
5407 /* stack_slot is negative, but we want to index stack_usage_map
5408 with positive values. */
5409 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
5410 upper_bound = -INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1)) + 1;
5411 else
5412 upper_bound = 0;
51bbfa0c 5413
6dad9361
TS
5414 lower_bound = upper_bound - arg->locate.size.constant;
5415 }
f73ad30e 5416 else
6dad9361
TS
5417 {
5418 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
5419 lower_bound = INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1));
5420 else
5421 lower_bound = 0;
51bbfa0c 5422
6dad9361
TS
5423 upper_bound = lower_bound + arg->locate.size.constant;
5424 }
51bbfa0c 5425
546ff777
AM
5426 i = lower_bound;
5427 /* Don't worry about things in the fixed argument area;
5428 it has already been saved. */
5429 if (i < reg_parm_stack_space)
5430 i = reg_parm_stack_space;
5431 while (i < upper_bound && stack_usage_map[i] == 0)
5432 i++;
51bbfa0c 5433
546ff777 5434 if (i < upper_bound)
51bbfa0c 5435 {
e7949876
AM
5436 /* We need to make a save area. */
5437 unsigned int size = arg->locate.size.constant * BITS_PER_UNIT;
f4b31647
RS
5438 machine_mode save_mode
5439 = int_mode_for_size (size, 1).else_blk ();
e7949876
AM
5440 rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
5441 rtx stack_area = gen_rtx_MEM (save_mode, adr);
f73ad30e
JH
5442
5443 if (save_mode == BLKmode)
5444 {
9ee5337d
EB
5445 arg->save_area
5446 = assign_temp (TREE_TYPE (arg->tree_value), 1, 1);
f73ad30e 5447 preserve_temp_slots (arg->save_area);
1a8cb155
RS
5448 emit_block_move (validize_mem (copy_rtx (arg->save_area)),
5449 stack_area,
7816b87e 5450 GEN_INT (arg->locate.size.constant),
44bb111a 5451 BLOCK_OP_CALL_PARM);
f73ad30e
JH
5452 }
5453 else
5454 {
5455 arg->save_area = gen_reg_rtx (save_mode);
5456 emit_move_insn (arg->save_area, stack_area);
5457 }
51bbfa0c
RS
5458 }
5459 }
5460 }
b564df06 5461
51bbfa0c
RS
5462 /* If this isn't going to be placed on both the stack and in registers,
5463 set up the register and number of words. */
5464 if (! arg->pass_on_stack)
aa7634dd
DM
5465 {
5466 if (flags & ECF_SIBCALL)
5467 reg = arg->tail_call_reg;
5468 else
5469 reg = arg->reg;
5470 partial = arg->partial;
5471 }
51bbfa0c 5472
366de0ce
NS
5473 /* Being passed entirely in a register. We shouldn't be called in
5474 this case. */
5475 gcc_assert (reg == 0 || partial != 0);
c22cacf3 5476
4ab56118
RK
5477 /* If this arg needs special alignment, don't load the registers
5478 here. */
5479 if (arg->n_aligned_regs != 0)
5480 reg = 0;
f725a3ec 5481
4ab56118 5482 /* If this is being passed partially in a register, we can't evaluate
51bbfa0c
RS
5483 it directly into its stack slot. Otherwise, we can. */
5484 if (arg->value == 0)
d64f5a78 5485 {
d64f5a78
RS
5486 /* stack_arg_under_construction is nonzero if a function argument is
5487 being evaluated directly into the outgoing argument list and
5488 expand_call must take special action to preserve the argument list
5489 if it is called recursively.
5490
5491 For scalar function arguments stack_usage_map is sufficient to
5492 determine which stack slots must be saved and restored. Scalar
5493 arguments in general have pass_on_stack == 0.
5494
5495 If this argument is initialized by a function which takes the
5496 address of the argument (a C++ constructor or a C function
5497 returning a BLKmode structure), then stack_usage_map is
5498 insufficient and expand_call must push the stack around the
5499 function call. Such arguments have pass_on_stack == 1.
5500
5501 Note that it is always safe to set stack_arg_under_construction,
5502 but this generates suboptimal code if set when not needed. */
5503
5504 if (arg->pass_on_stack)
5505 stack_arg_under_construction++;
f73ad30e 5506
3a08477a
RK
5507 arg->value = expand_expr (pval,
5508 (partial
5509 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
5510 ? NULL_RTX : arg->stack,
8403445a 5511 VOIDmode, EXPAND_STACK_PARM);
1efe6448
RK
5512
5513 /* If we are promoting object (or for any other reason) the mode
5514 doesn't agree, convert the mode. */
5515
7373d92d
RK
5516 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
5517 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
5518 arg->value, arg->unsignedp);
1efe6448 5519
d64f5a78
RS
5520 if (arg->pass_on_stack)
5521 stack_arg_under_construction--;
d64f5a78 5522 }
51bbfa0c 5523
0dc42b03 5524 /* Check for overlap with already clobbered argument area. */
07eef816
KH
5525 if ((flags & ECF_SIBCALL)
5526 && MEM_P (arg->value)
5527 && mem_overlaps_already_clobbered_arg_p (XEXP (arg->value, 0),
5528 arg->locate.size.constant))
5529 sibcall_failure = 1;
0dc42b03 5530
51bbfa0c
RS
5531 /* Don't allow anything left on stack from computation
5532 of argument to alloca. */
f8a097cd 5533 if (flags & ECF_MAY_BE_ALLOCA)
51bbfa0c
RS
5534 do_pending_stack_adjust ();
5535
5536 if (arg->value == arg->stack)
37a08a29
RK
5537 /* If the value is already in the stack slot, we are done. */
5538 ;
1efe6448 5539 else if (arg->mode != BLKmode)
51bbfa0c 5540 {
b3694847 5541 int size;
46bd2bee 5542 unsigned int parm_align;
51bbfa0c
RS
5543
5544 /* Argument is a scalar, not entirely passed in registers.
5545 (If part is passed in registers, arg->partial says how much
5546 and emit_push_insn will take care of putting it there.)
f725a3ec 5547
51bbfa0c
RS
5548 Push it, and if its size is less than the
5549 amount of space allocated to it,
5550 also bump stack pointer by the additional space.
5551 Note that in C the default argument promotions
5552 will prevent such mismatches. */
5553
974aedcc
MP
5554 if (TYPE_EMPTY_P (TREE_TYPE (pval)))
5555 size = 0;
5556 else
5557 size = GET_MODE_SIZE (arg->mode);
5558
51bbfa0c
RS
5559 /* Compute how much space the push instruction will push.
5560 On many machines, pushing a byte will advance the stack
5561 pointer by a halfword. */
5562#ifdef PUSH_ROUNDING
5563 size = PUSH_ROUNDING (size);
5564#endif
5565 used = size;
5566
5567 /* Compute how much space the argument should get:
5568 round up to a multiple of the alignment for arguments. */
76b0cbf8
RS
5569 if (targetm.calls.function_arg_padding (arg->mode, TREE_TYPE (pval))
5570 != PAD_NONE)
51bbfa0c
RS
5571 used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
5572 / (PARM_BOUNDARY / BITS_PER_UNIT))
5573 * (PARM_BOUNDARY / BITS_PER_UNIT));
5574
46bd2bee
JM
5575 /* Compute the alignment of the pushed argument. */
5576 parm_align = arg->locate.boundary;
76b0cbf8
RS
5577 if (targetm.calls.function_arg_padding (arg->mode, TREE_TYPE (pval))
5578 == PAD_DOWNWARD)
46bd2bee
JM
5579 {
5580 int pad = used - size;
5581 if (pad)
5582 {
146ec50f 5583 unsigned int pad_align = least_bit_hwi (pad) * BITS_PER_UNIT;
46bd2bee
JM
5584 parm_align = MIN (parm_align, pad_align);
5585 }
5586 }
5587
51bbfa0c
RS
5588 /* This isn't already where we want it on the stack, so put it there.
5589 This can either be done with push or copy insns. */
974aedcc
MP
5590 if (used
5591 && !emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval),
5592 NULL_RTX, parm_align, partial, reg, used - size,
5593 argblock, ARGS_SIZE_RTX (arg->locate.offset),
5594 reg_parm_stack_space,
5595 ARGS_SIZE_RTX (arg->locate.alignment_pad), true))
99206968 5596 sibcall_failure = 1;
841404cd
AO
5597
5598 /* Unless this is a partially-in-register argument, the argument is now
5599 in the stack. */
5600 if (partial == 0)
5601 arg->value = arg->stack;
51bbfa0c
RS
5602 }
5603 else
5604 {
5605 /* BLKmode, at least partly to be pushed. */
5606
1b1f20ca 5607 unsigned int parm_align;
b3694847 5608 int excess;
51bbfa0c
RS
5609 rtx size_rtx;
5610
5611 /* Pushing a nonscalar.
5612 If part is passed in registers, PARTIAL says how much
5613 and emit_push_insn will take care of putting it there. */
5614
5615 /* Round its size up to a multiple
5616 of the allocation unit for arguments. */
5617
e7949876 5618 if (arg->locate.size.var != 0)
51bbfa0c
RS
5619 {
5620 excess = 0;
e7949876 5621 size_rtx = ARGS_SIZE_RTX (arg->locate.size);
51bbfa0c
RS
5622 }
5623 else
5624 {
78a52f11
RH
5625 /* PUSH_ROUNDING has no effect on us, because emit_push_insn
5626 for BLKmode is careful to avoid it. */
5627 excess = (arg->locate.size.constant
974aedcc 5628 - arg_int_size_in_bytes (TREE_TYPE (pval))
78a52f11 5629 + partial);
974aedcc 5630 size_rtx = expand_expr (arg_size_in_bytes (TREE_TYPE (pval)),
bbbbb16a
ILT
5631 NULL_RTX, TYPE_MODE (sizetype),
5632 EXPAND_NORMAL);
51bbfa0c
RS
5633 }
5634
bfc45551 5635 parm_align = arg->locate.boundary;
1b1f20ca
RH
5636
5637 /* When an argument is padded down, the block is aligned to
5638 PARM_BOUNDARY, but the actual argument isn't. */
76b0cbf8
RS
5639 if (targetm.calls.function_arg_padding (arg->mode, TREE_TYPE (pval))
5640 == PAD_DOWNWARD)
1b1f20ca 5641 {
e7949876 5642 if (arg->locate.size.var)
1b1f20ca
RH
5643 parm_align = BITS_PER_UNIT;
5644 else if (excess)
5645 {
146ec50f 5646 unsigned int excess_align = least_bit_hwi (excess) * BITS_PER_UNIT;
1b1f20ca
RH
5647 parm_align = MIN (parm_align, excess_align);
5648 }
5649 }
5650
3c0cb5de 5651 if ((flags & ECF_SIBCALL) && MEM_P (arg->value))
4c6b3b2a
JJ
5652 {
5653 /* emit_push_insn might not work properly if arg->value and
e7949876 5654 argblock + arg->locate.offset areas overlap. */
4c6b3b2a
JJ
5655 rtx x = arg->value;
5656 int i = 0;
5657
38173d38 5658 if (XEXP (x, 0) == crtl->args.internal_arg_pointer
4c6b3b2a
JJ
5659 || (GET_CODE (XEXP (x, 0)) == PLUS
5660 && XEXP (XEXP (x, 0), 0) ==
38173d38 5661 crtl->args.internal_arg_pointer
481683e1 5662 && CONST_INT_P (XEXP (XEXP (x, 0), 1))))
4c6b3b2a 5663 {
38173d38 5664 if (XEXP (x, 0) != crtl->args.internal_arg_pointer)
4c6b3b2a
JJ
5665 i = INTVAL (XEXP (XEXP (x, 0), 1));
5666
b3877860
KT
5667 /* arg.locate doesn't contain the pretend_args_size offset,
5668 it's part of argblock. Ensure we don't count it in I. */
5669 if (STACK_GROWS_DOWNWARD)
5670 i -= crtl->args.pretend_args_size;
5671 else
5672 i += crtl->args.pretend_args_size;
5673
e0a21ab9 5674 /* expand_call should ensure this. */
366de0ce 5675 gcc_assert (!arg->locate.offset.var
d6c2c77c 5676 && arg->locate.size.var == 0
481683e1 5677 && CONST_INT_P (size_rtx));
4c6b3b2a 5678
e7949876 5679 if (arg->locate.offset.constant > i)
4c6b3b2a 5680 {
e7949876 5681 if (arg->locate.offset.constant < i + INTVAL (size_rtx))
4c6b3b2a
JJ
5682 sibcall_failure = 1;
5683 }
e7949876 5684 else if (arg->locate.offset.constant < i)
4c6b3b2a 5685 {
d6c2c77c
JC
5686 /* Use arg->locate.size.constant instead of size_rtx
5687 because we only care about the part of the argument
5688 on the stack. */
5689 if (i < (arg->locate.offset.constant
5690 + arg->locate.size.constant))
5691 sibcall_failure = 1;
5692 }
5693 else
5694 {
5695 /* Even though they appear to be at the same location,
5696 if part of the outgoing argument is in registers,
5697 they aren't really at the same location. Check for
5698 this by making sure that the incoming size is the
5699 same as the outgoing size. */
5700 if (arg->locate.size.constant != INTVAL (size_rtx))
4c6b3b2a
JJ
5701 sibcall_failure = 1;
5702 }
5703 }
5704 }
5705
974aedcc
MP
5706 if (!CONST_INT_P (size_rtx) || INTVAL (size_rtx) != 0)
5707 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
5708 parm_align, partial, reg, excess, argblock,
5709 ARGS_SIZE_RTX (arg->locate.offset),
5710 reg_parm_stack_space,
5711 ARGS_SIZE_RTX (arg->locate.alignment_pad), false);
51bbfa0c 5712
841404cd
AO
5713 /* Unless this is a partially-in-register argument, the argument is now
5714 in the stack.
51bbfa0c 5715
841404cd
AO
5716 ??? Unlike the case above, in which we want the actual
5717 address of the data, so that we can load it directly into a
5718 register, here we want the address of the stack slot, so that
5719 it's properly aligned for word-by-word copying or something
5720 like that. It's not clear that this is always correct. */
5721 if (partial == 0)
5722 arg->value = arg->stack_slot;
5723 }
8df3dbb7
RH
5724
5725 if (arg->reg && GET_CODE (arg->reg) == PARALLEL)
5726 {
5727 tree type = TREE_TYPE (arg->tree_value);
5728 arg->parallel_value
5729 = emit_group_load_into_temps (arg->reg, arg->value, type,
5730 int_size_in_bytes (type));
5731 }
51bbfa0c 5732
8403445a
AM
5733 /* Mark all slots this store used. */
5734 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
5735 && argblock && ! variable_size && arg->stack)
5736 for (i = lower_bound; i < upper_bound; i++)
5737 stack_usage_map[i] = 1;
5738
51bbfa0c
RS
5739 /* Once we have pushed something, pops can't safely
5740 be deferred during the rest of the arguments. */
5741 NO_DEFER_POP;
5742
9474e8ab 5743 /* Free any temporary slots made in processing this argument. */
cc79451b 5744 pop_temp_slots ();
4c6b3b2a
JJ
5745
5746 return sibcall_failure;
51bbfa0c 5747}
a4b1b92a 5748
fe984136 5749/* Nonzero if we do not know how to pass TYPE solely in registers. */
a4b1b92a 5750
fe984136 5751bool
ef4bddc2 5752must_pass_in_stack_var_size (machine_mode mode ATTRIBUTE_UNUSED,
586de218 5753 const_tree type)
fe984136
RH
5754{
5755 if (!type)
5756 return false;
5757
5758 /* If the type has variable size... */
5759 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5760 return true;
a4b1b92a 5761
fe984136
RH
5762 /* If the type is marked as addressable (it is required
5763 to be constructed into the stack)... */
5764 if (TREE_ADDRESSABLE (type))
5765 return true;
5766
5767 return false;
5768}
a4b1b92a 5769
7ae4ad28 5770/* Another version of the TARGET_MUST_PASS_IN_STACK hook. This one
fe984136
RH
5771 takes trailing padding of a structure into account. */
5772/* ??? Should be able to merge these two by examining BLOCK_REG_PADDING. */
a4b1b92a
RH
5773
5774bool
ef4bddc2 5775must_pass_in_stack_var_size_or_pad (machine_mode mode, const_tree type)
a4b1b92a
RH
5776{
5777 if (!type)
40cdfd5a 5778 return false;
a4b1b92a
RH
5779
5780 /* If the type has variable size... */
5781 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5782 return true;
5783
5784 /* If the type is marked as addressable (it is required
5785 to be constructed into the stack)... */
5786 if (TREE_ADDRESSABLE (type))
5787 return true;
5788
974aedcc
MP
5789 if (TYPE_EMPTY_P (type))
5790 return false;
5791
a4b1b92a
RH
5792 /* If the padding and mode of the type is such that a copy into
5793 a register would put it into the wrong part of the register. */
5794 if (mode == BLKmode
5795 && int_size_in_bytes (type) % (PARM_BOUNDARY / BITS_PER_UNIT)
76b0cbf8
RS
5796 && (targetm.calls.function_arg_padding (mode, type)
5797 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)))
a4b1b92a
RH
5798 return true;
5799
5800 return false;
5801}
6bf29a7e
MS
5802
5803/* Tell the garbage collector about GTY markers in this source file. */
5804#include "gt-calls.h"
This page took 6.234908 seconds and 5 git commands to generate.