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