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