]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/calls.c
i386.c (ix86_gimplify_va_arg): Don't need temporary for passing arguments the contain...
[thirdparty/gcc.git] / gcc / calls.c
CommitLineData
51bbfa0c 1/* Convert function calls to rtl insns, for GNU C compiler.
61f71b34 2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
d9221e01 3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
51bbfa0c 4
1322177d 5This file is part of GCC.
51bbfa0c 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
51bbfa0c 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
51bbfa0c
RS
16
17You should have received a copy of the GNU General Public License
1322177d
LB
18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
51bbfa0c
RS
21
22#include "config.h"
670ee920 23#include "system.h"
4977bab6
ZW
24#include "coretypes.h"
25#include "tm.h"
670ee920
KG
26#include "rtl.h"
27#include "tree.h"
28#include "flags.h"
29#include "expr.h"
6e985040 30#include "optabs.h"
e78d8e51 31#include "libfuncs.h"
49ad7cfa 32#include "function.h"
670ee920 33#include "regs.h"
5f6da302 34#include "toplev.h"
d6f4ec51 35#include "output.h"
b1474bb7 36#include "tm_p.h"
ea11ca7e 37#include "timevar.h"
c67846f2 38#include "sbitmap.h"
b0c48229 39#include "langhooks.h"
23626154 40#include "target.h"
b255a036 41#include "cgraph.h"
b2dd096b 42#include "except.h"
51bbfa0c 43
c795bca9
BS
44/* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits. */
45#define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
51bbfa0c
RS
46
47/* Data structure and subroutines used within expand_call. */
48
49struct arg_data
50{
51 /* Tree node for this argument. */
52 tree tree_value;
1efe6448
RK
53 /* Mode for value; TYPE_MODE unless promoted. */
54 enum machine_mode mode;
51bbfa0c
RS
55 /* Current RTL value for argument, or 0 if it isn't precomputed. */
56 rtx value;
57 /* Initially-compute RTL value for argument; only for const functions. */
58 rtx initial_value;
59 /* Register to pass this argument in, 0 if passed on stack, or an
cacbd532 60 PARALLEL if the arg is to be copied into multiple non-contiguous
51bbfa0c
RS
61 registers. */
62 rtx reg;
099e9712
JH
63 /* Register to pass this argument in when generating tail call sequence.
64 This is not the same register as for normal calls on machines with
65 register windows. */
66 rtx tail_call_reg;
84b55618
RK
67 /* If REG was promoted from the actual mode of the argument expression,
68 indicates whether the promotion is sign- or zero-extended. */
69 int unsignedp;
51bbfa0c
RS
70 /* Number of registers to use. 0 means put the whole arg in registers.
71 Also 0 if not passed in registers. */
72 int partial;
da7d8304 73 /* Nonzero if argument must be passed on stack.
d64f5a78
RS
74 Note that some arguments may be passed on the stack
75 even though pass_on_stack is zero, just because FUNCTION_ARG says so.
76 pass_on_stack identifies arguments that *cannot* go in registers. */
51bbfa0c 77 int pass_on_stack;
e7949876
AM
78 /* Some fields packaged up for locate_and_pad_parm. */
79 struct locate_and_pad_arg_data locate;
51bbfa0c
RS
80 /* Location on the stack at which parameter should be stored. The store
81 has already been done if STACK == VALUE. */
82 rtx stack;
83 /* Location on the stack of the start of this argument slot. This can
84 differ from STACK if this arg pads downward. This location is known
85 to be aligned to FUNCTION_ARG_BOUNDARY. */
86 rtx stack_slot;
51bbfa0c
RS
87 /* Place that this stack area has been saved, if needed. */
88 rtx save_area;
4ab56118
RK
89 /* If an argument's alignment does not permit direct copying into registers,
90 copy in smaller-sized pieces into pseudos. These are stored in a
91 block pointed to by this field. The next field says how many
92 word-sized pseudos we made. */
93 rtx *aligned_regs;
94 int n_aligned_regs;
51bbfa0c
RS
95};
96
da7d8304 97/* A vector of one char per byte of stack space. A byte if nonzero if
51bbfa0c
RS
98 the corresponding stack location has been used.
99 This vector is used to prevent a function call within an argument from
100 clobbering any stack already set up. */
101static char *stack_usage_map;
102
103/* Size of STACK_USAGE_MAP. */
104static int highest_outgoing_arg_in_use;
2f4aa534 105
c67846f2
JJ
106/* A bitmap of virtual-incoming stack space. Bit is set if the corresponding
107 stack location's tail call argument has been already stored into the stack.
108 This bitmap is used to prevent sibling call optimization if function tries
109 to use parent's incoming argument slots when they have been already
110 overwritten with tail call arguments. */
111static sbitmap stored_args_map;
112
2f4aa534
RS
113/* stack_arg_under_construction is nonzero when an argument may be
114 initialized with a constructor call (including a C function that
115 returns a BLKmode struct) and expand_call must take special action
116 to make sure the object being constructed does not overlap the
117 argument list for the constructor call. */
118int stack_arg_under_construction;
51bbfa0c 119
6de9cd9a 120static void emit_call_1 (rtx, tree, tree, tree, HOST_WIDE_INT, HOST_WIDE_INT,
d329e058
AJ
121 HOST_WIDE_INT, rtx, rtx, int, rtx, int,
122 CUMULATIVE_ARGS *);
123static void precompute_register_parameters (int, struct arg_data *, int *);
124static int store_one_arg (struct arg_data *, rtx, int, int, int);
125static void store_unaligned_arguments_into_pseudos (struct arg_data *, int);
126static int finalize_must_preallocate (int, int, struct arg_data *,
127 struct args_size *);
128static void precompute_arguments (int, int, struct arg_data *);
129static int compute_argument_block_size (int, struct args_size *, int);
130static void initialize_argument_information (int, struct arg_data *,
131 struct args_size *, int, tree,
132 tree, CUMULATIVE_ARGS *, int,
dd292d0a 133 rtx *, int *, int *, int *,
6de9cd9a 134 bool *, bool);
d329e058
AJ
135static void compute_argument_addresses (struct arg_data *, rtx, int);
136static rtx rtx_for_function_call (tree, tree);
137static void load_register_parameters (struct arg_data *, int, rtx *, int,
138 int, int *);
139static rtx emit_library_call_value_1 (int, rtx, rtx, enum libcall_type,
140 enum machine_mode, int, va_list);
141static int special_function_p (tree, int);
d329e058
AJ
142static int check_sibcall_argument_overlap_1 (rtx);
143static int check_sibcall_argument_overlap (rtx, struct arg_data *, int);
144
145static int combine_pending_stack_adjustment_and_call (int, struct args_size *,
146 int);
147static tree fix_unsafe_tree (tree);
c988af2b 148static bool shift_returned_value (tree, rtx *);
21a3b983 149
f73ad30e 150#ifdef REG_PARM_STACK_SPACE
d329e058
AJ
151static rtx save_fixed_argument_area (int, rtx, int *, int *);
152static void restore_fixed_argument_area (rtx, rtx, int, int);
20efdf74 153#endif
51bbfa0c 154\f
51bbfa0c
RS
155/* Force FUNEXP into a form suitable for the address of a CALL,
156 and return that as an rtx. Also load the static chain register
157 if FNDECL is a nested function.
158
77cac2f2
RK
159 CALL_FUSAGE points to a variable holding the prospective
160 CALL_INSN_FUNCTION_USAGE information. */
51bbfa0c 161
03dacb02 162rtx
6de9cd9a
DN
163prepare_call_address (rtx funexp, rtx static_chain_value,
164 rtx *call_fusage, int reg_parm_seen, int sibcallp)
51bbfa0c 165{
51bbfa0c
RS
166 funexp = protect_from_queue (funexp, 0);
167
ba228239 168 /* Make a valid memory address and copy constants through pseudo-regs,
51bbfa0c
RS
169 but not for a constant address if -fno-function-cse. */
170 if (GET_CODE (funexp) != SYMBOL_REF)
01368078 171 /* If we are using registers for parameters, force the
e9a25f70
JL
172 function address into a register now. */
173 funexp = ((SMALL_REGISTER_CLASSES && reg_parm_seen)
174 ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
175 : memory_address (FUNCTION_MODE, funexp));
3affaf29 176 else if (! sibcallp)
51bbfa0c
RS
177 {
178#ifndef NO_FUNCTION_CSE
179 if (optimize && ! flag_no_function_cse)
082a099c 180 funexp = force_reg (Pmode, funexp);
51bbfa0c
RS
181#endif
182 }
183
184 if (static_chain_value != 0)
185 {
186 emit_move_insn (static_chain_rtx, static_chain_value);
187
f8cfc6aa 188 if (REG_P (static_chain_rtx))
f991a240 189 use_reg (call_fusage, static_chain_rtx);
51bbfa0c
RS
190 }
191
192 return funexp;
193}
194
195/* Generate instructions to call function FUNEXP,
196 and optionally pop the results.
197 The CALL_INSN is the first insn generated.
198
607ea900 199 FNDECL is the declaration node of the function. This is given to the
2c8da025
RK
200 macro RETURN_POPS_ARGS to determine whether this function pops its own args.
201
334c4f0f
RK
202 FUNTYPE is the data type of the function. This is given to the macro
203 RETURN_POPS_ARGS to determine whether this function pops its own args.
204 We used to allow an identifier for library functions, but that doesn't
205 work when the return type is an aggregate type and the calling convention
206 says that the pointer to this aggregate is to be popped by the callee.
51bbfa0c
RS
207
208 STACK_SIZE is the number of bytes of arguments on the stack,
c2732da3
JM
209 ROUNDED_STACK_SIZE is that number rounded up to
210 PREFERRED_STACK_BOUNDARY; zero if the size is variable. This is
211 both to put into the call insn and to generate explicit popping
212 code if necessary.
51bbfa0c
RS
213
214 STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
215 It is zero if this call doesn't want a structure value.
216
217 NEXT_ARG_REG is the rtx that results from executing
218 FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1)
219 just after all the args have had their registers assigned.
220 This could be whatever you like, but normally it is the first
221 arg-register beyond those used for args in this call,
222 or 0 if all the arg-registers are used in this call.
223 It is passed on to `gen_call' so you can put this info in the call insn.
224
225 VALREG is a hard register in which a value is returned,
226 or 0 if the call does not return a value.
227
228 OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
229 the args to this call were processed.
230 We restore `inhibit_defer_pop' to that value.
231
94b25f81 232 CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
6d2f8887 233 denote registers used by the called function. */
f725a3ec 234
322e3e34 235static void
6de9cd9a
DN
236emit_call_1 (rtx funexp, tree fntree, tree fndecl ATTRIBUTE_UNUSED,
237 tree funtype ATTRIBUTE_UNUSED,
d329e058
AJ
238 HOST_WIDE_INT stack_size ATTRIBUTE_UNUSED,
239 HOST_WIDE_INT rounded_stack_size,
240 HOST_WIDE_INT struct_value_size ATTRIBUTE_UNUSED,
241 rtx next_arg_reg ATTRIBUTE_UNUSED, rtx valreg,
242 int old_inhibit_defer_pop, rtx call_fusage, int ecf_flags,
243 CUMULATIVE_ARGS *args_so_far ATTRIBUTE_UNUSED)
51bbfa0c 244{
062e7fd8 245 rtx rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
51bbfa0c
RS
246 rtx call_insn;
247 int already_popped = 0;
fb5eebb9 248 HOST_WIDE_INT n_popped = RETURN_POPS_ARGS (fndecl, funtype, stack_size);
f45c9d95
ZW
249#if defined (HAVE_call) && defined (HAVE_call_value)
250 rtx struct_value_size_rtx;
251 struct_value_size_rtx = GEN_INT (struct_value_size);
252#endif
51bbfa0c 253
fa5322fa
AO
254#ifdef CALL_POPS_ARGS
255 n_popped += CALL_POPS_ARGS (* args_so_far);
256#endif
d329e058 257
51bbfa0c
RS
258 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
259 and we don't want to load it into a register as an optimization,
260 because prepare_call_address already did it if it should be done. */
261 if (GET_CODE (funexp) != SYMBOL_REF)
262 funexp = memory_address (FUNCTION_MODE, funexp);
263
0a1c58a2
JL
264#if defined (HAVE_sibcall_pop) && defined (HAVE_sibcall_value_pop)
265 if ((ecf_flags & ECF_SIBCALL)
266 && HAVE_sibcall_pop && HAVE_sibcall_value_pop
f132f529 267 && (n_popped > 0 || stack_size == 0))
0a1c58a2 268 {
8ac61af7 269 rtx n_pop = GEN_INT (n_popped);
0a1c58a2
JL
270 rtx pat;
271
272 /* If this subroutine pops its own args, record that in the call insn
273 if possible, for the sake of frame pointer elimination. */
274
275 if (valreg)
f45c9d95 276 pat = GEN_SIBCALL_VALUE_POP (valreg,
0a1c58a2
JL
277 gen_rtx_MEM (FUNCTION_MODE, funexp),
278 rounded_stack_size_rtx, next_arg_reg,
279 n_pop);
280 else
f45c9d95 281 pat = GEN_SIBCALL_POP (gen_rtx_MEM (FUNCTION_MODE, funexp),
0a1c58a2
JL
282 rounded_stack_size_rtx, next_arg_reg, n_pop);
283
284 emit_call_insn (pat);
285 already_popped = 1;
286 }
287 else
288#endif
289
51bbfa0c 290#if defined (HAVE_call_pop) && defined (HAVE_call_value_pop)
8ac61af7
RK
291 /* If the target has "call" or "call_value" insns, then prefer them
292 if no arguments are actually popped. If the target does not have
293 "call" or "call_value" insns, then we must use the popping versions
294 even if the call has no arguments to pop. */
8bcafee3
JDA
295#if defined (HAVE_call) && defined (HAVE_call_value)
296 if (HAVE_call && HAVE_call_value && HAVE_call_pop && HAVE_call_value_pop
7393c642 297 && n_popped > 0 && ! (ecf_flags & ECF_SP_DEPRESSED))
8bcafee3
JDA
298#else
299 if (HAVE_call_pop && HAVE_call_value_pop)
300#endif
51bbfa0c 301 {
fb5eebb9 302 rtx n_pop = GEN_INT (n_popped);
51bbfa0c
RS
303 rtx pat;
304
305 /* If this subroutine pops its own args, record that in the call insn
306 if possible, for the sake of frame pointer elimination. */
2c8da025 307
51bbfa0c 308 if (valreg)
f45c9d95 309 pat = GEN_CALL_VALUE_POP (valreg,
38a448ca 310 gen_rtx_MEM (FUNCTION_MODE, funexp),
062e7fd8 311 rounded_stack_size_rtx, next_arg_reg, n_pop);
51bbfa0c 312 else
f45c9d95 313 pat = GEN_CALL_POP (gen_rtx_MEM (FUNCTION_MODE, funexp),
062e7fd8 314 rounded_stack_size_rtx, next_arg_reg, n_pop);
51bbfa0c
RS
315
316 emit_call_insn (pat);
317 already_popped = 1;
318 }
319 else
320#endif
51bbfa0c 321
0a1c58a2
JL
322#if defined (HAVE_sibcall) && defined (HAVE_sibcall_value)
323 if ((ecf_flags & ECF_SIBCALL)
324 && HAVE_sibcall && HAVE_sibcall_value)
325 {
326 if (valreg)
f45c9d95 327 emit_call_insn (GEN_SIBCALL_VALUE (valreg,
0a1c58a2
JL
328 gen_rtx_MEM (FUNCTION_MODE, funexp),
329 rounded_stack_size_rtx,
330 next_arg_reg, NULL_RTX));
331 else
f45c9d95 332 emit_call_insn (GEN_SIBCALL (gen_rtx_MEM (FUNCTION_MODE, funexp),
0a1c58a2
JL
333 rounded_stack_size_rtx, next_arg_reg,
334 struct_value_size_rtx));
335 }
336 else
337#endif
338
51bbfa0c
RS
339#if defined (HAVE_call) && defined (HAVE_call_value)
340 if (HAVE_call && HAVE_call_value)
341 {
342 if (valreg)
f45c9d95 343 emit_call_insn (GEN_CALL_VALUE (valreg,
38a448ca 344 gen_rtx_MEM (FUNCTION_MODE, funexp),
062e7fd8 345 rounded_stack_size_rtx, next_arg_reg,
e992302c 346 NULL_RTX));
51bbfa0c 347 else
f45c9d95 348 emit_call_insn (GEN_CALL (gen_rtx_MEM (FUNCTION_MODE, funexp),
062e7fd8 349 rounded_stack_size_rtx, next_arg_reg,
51bbfa0c
RS
350 struct_value_size_rtx));
351 }
352 else
353#endif
354 abort ();
355
ee960939
OH
356 /* Find the call we just emitted. */
357 call_insn = last_call_insn ();
51bbfa0c 358
2a8f6b90
JH
359 /* Mark memory as used for "pure" function call. */
360 if (ecf_flags & ECF_PURE)
8ac61af7
RK
361 call_fusage
362 = gen_rtx_EXPR_LIST
363 (VOIDmode,
364 gen_rtx_USE (VOIDmode,
365 gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode))),
366 call_fusage);
2a8f6b90 367
ee960939
OH
368 /* Put the register usage information there. */
369 add_function_usage_to (call_insn, call_fusage);
51bbfa0c
RS
370
371 /* If this is a const call, then set the insn's unchanging bit. */
2a8f6b90 372 if (ecf_flags & (ECF_CONST | ECF_PURE))
24a28584 373 CONST_OR_PURE_CALL_P (call_insn) = 1;
51bbfa0c 374
12a22e76
JM
375 /* If this call can't throw, attach a REG_EH_REGION reg note to that
376 effect. */
0a1c58a2 377 if (ecf_flags & ECF_NOTHROW)
54cea123 378 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, const0_rtx,
12a22e76 379 REG_NOTES (call_insn));
b2dd096b 380 else
6de9cd9a
DN
381 {
382 int rn = lookup_stmt_eh_region (fntree);
383
384 /* If rn < 0, then either (1) tree-ssa not used or (2) doesn't
385 throw, which we already took care of. */
386 if (rn > 0)
387 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, GEN_INT (rn),
388 REG_NOTES (call_insn));
389 note_current_region_may_contain_throw ();
390 }
12a22e76 391
ca3920ad
JW
392 if (ecf_flags & ECF_NORETURN)
393 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_NORETURN, const0_rtx,
394 REG_NOTES (call_insn));
9d98f8f9
JH
395 if (ecf_flags & ECF_ALWAYS_RETURN)
396 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_ALWAYS_RETURN, const0_rtx,
397 REG_NOTES (call_insn));
ca3920ad 398
570a98eb 399 if (ecf_flags & ECF_RETURNS_TWICE)
9defc9b7
RH
400 {
401 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_SETJMP, const0_rtx,
402 REG_NOTES (call_insn));
403 current_function_calls_setjmp = 1;
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;
51bbfa0c
RS
422 }
423
f73ad30e 424 if (!ACCUMULATE_OUTGOING_ARGS)
51bbfa0c 425 {
f73ad30e
JH
426 /* If returning from the subroutine does not automatically pop the args,
427 we need an instruction to pop them sooner or later.
428 Perhaps do it now; perhaps just record how much space to pop later.
429
430 If returning from the subroutine does pop the args, indicate that the
431 stack pointer will be changed. */
432
f79a65c0 433 if (rounded_stack_size != 0)
f73ad30e 434 {
f451eeef 435 if (ecf_flags & (ECF_SP_DEPRESSED | ECF_NORETURN | ECF_LONGJMP))
f79a65c0
RK
436 /* Just pretend we did the pop. */
437 stack_pointer_delta -= rounded_stack_size;
438 else if (flag_defer_pop && inhibit_defer_pop == 0
7393c642 439 && ! (ecf_flags & (ECF_CONST | ECF_PURE)))
f73ad30e
JH
440 pending_stack_adjust += rounded_stack_size;
441 else
442 adjust_stack (rounded_stack_size_rtx);
443 }
51bbfa0c 444 }
f73ad30e
JH
445 /* When we accumulate outgoing args, we must avoid any stack manipulations.
446 Restore the stack pointer to its original value now. Usually
447 ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
448 On i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
449 popping variants of functions exist as well.
450
451 ??? We may optimize similar to defer_pop above, but it is
452 probably not worthwhile.
f725a3ec 453
f73ad30e
JH
454 ??? It will be worthwhile to enable combine_stack_adjustments even for
455 such machines. */
456 else if (n_popped)
457 anti_adjust_stack (GEN_INT (n_popped));
51bbfa0c
RS
458}
459
20efdf74
JL
460/* Determine if the function identified by NAME and FNDECL is one with
461 special properties we wish to know about.
462
463 For example, if the function might return more than one time (setjmp), then
464 set RETURNS_TWICE to a nonzero value.
465
f2d33f13 466 Similarly set LONGJMP for if the function is in the longjmp family.
20efdf74 467
20efdf74
JL
468 Set MAY_BE_ALLOCA for any memory allocation function that might allocate
469 space from the stack such as alloca. */
470
f2d33f13 471static int
d329e058 472special_function_p (tree fndecl, int flags)
20efdf74 473{
6de9cd9a 474 if (fndecl && DECL_NAME (fndecl)
140592a0 475 && IDENTIFIER_LENGTH (DECL_NAME (fndecl)) <= 17
20efdf74
JL
476 /* Exclude functions not at the file scope, or not `extern',
477 since they are not the magic functions we would otherwise
d1bd0ded
GK
478 think they are.
479 FIXME: this should be handled with attributes, not with this
480 hacky imitation of DECL_ASSEMBLER_NAME. It's (also) wrong
481 because you can declare fork() inside a function if you
482 wish. */
483 && (DECL_CONTEXT (fndecl) == NULL_TREE
484 || TREE_CODE (DECL_CONTEXT (fndecl)) == TRANSLATION_UNIT_DECL)
485 && TREE_PUBLIC (fndecl))
20efdf74 486 {
63ad61ed
ZW
487 const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
488 const char *tname = name;
20efdf74 489
ca54603f
JL
490 /* We assume that alloca will always be called by name. It
491 makes no sense to pass it as a pointer-to-function to
492 anything that does not understand its behavior. */
f2d33f13
JH
493 if (((IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 6
494 && name[0] == 'a'
495 && ! strcmp (name, "alloca"))
496 || (IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 16
497 && name[0] == '_'
498 && ! strcmp (name, "__builtin_alloca"))))
499 flags |= ECF_MAY_BE_ALLOCA;
ca54603f 500
20efdf74
JL
501 /* Disregard prefix _, __ or __x. */
502 if (name[0] == '_')
503 {
504 if (name[1] == '_' && name[2] == 'x')
505 tname += 3;
506 else if (name[1] == '_')
507 tname += 2;
508 else
509 tname += 1;
510 }
511
512 if (tname[0] == 's')
513 {
f2d33f13
JH
514 if ((tname[1] == 'e'
515 && (! strcmp (tname, "setjmp")
516 || ! strcmp (tname, "setjmp_syscall")))
517 || (tname[1] == 'i'
518 && ! strcmp (tname, "sigsetjmp"))
519 || (tname[1] == 'a'
520 && ! strcmp (tname, "savectx")))
521 flags |= ECF_RETURNS_TWICE;
522
20efdf74
JL
523 if (tname[1] == 'i'
524 && ! strcmp (tname, "siglongjmp"))
f2d33f13 525 flags |= ECF_LONGJMP;
20efdf74
JL
526 }
527 else if ((tname[0] == 'q' && tname[1] == 's'
528 && ! strcmp (tname, "qsetjmp"))
529 || (tname[0] == 'v' && tname[1] == 'f'
530 && ! strcmp (tname, "vfork")))
f2d33f13 531 flags |= ECF_RETURNS_TWICE;
20efdf74
JL
532
533 else if (tname[0] == 'l' && tname[1] == 'o'
534 && ! strcmp (tname, "longjmp"))
f2d33f13 535 flags |= ECF_LONGJMP;
20efdf74 536 }
d1c38823 537
f2d33f13 538 return flags;
20efdf74
JL
539}
540
f2d33f13 541/* Return nonzero when tree represent call to longjmp. */
7393c642 542
f2d33f13 543int
d329e058 544setjmp_call_p (tree fndecl)
f2d33f13
JH
545{
546 return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
547}
548
c986baf6
JH
549/* Return true when exp contains alloca call. */
550bool
d329e058 551alloca_call_p (tree exp)
c986baf6
JH
552{
553 if (TREE_CODE (exp) == CALL_EXPR
554 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
555 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
556 == FUNCTION_DECL)
557 && (special_function_p (TREE_OPERAND (TREE_OPERAND (exp, 0), 0),
558 0) & ECF_MAY_BE_ALLOCA))
559 return true;
560 return false;
561}
562
b5cd4ed4 563/* Detect flags (function attributes) from the function decl or type node. */
7393c642 564
4977bab6 565int
d329e058 566flags_from_decl_or_type (tree exp)
f2d33f13
JH
567{
568 int flags = 0;
b5cd4ed4 569 tree type = exp;
36dbb93d 570
f2d33f13
JH
571 if (DECL_P (exp))
572 {
b255a036 573 struct cgraph_rtl_info *i = cgraph_rtl_info (exp);
b5cd4ed4
RK
574 type = TREE_TYPE (exp);
575
36dbb93d
RS
576 if (i)
577 {
578 if (i->pure_function)
579 flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
580 if (i->const_function)
581 flags |= ECF_CONST | ECF_LIBCALL_BLOCK;
582 }
b255a036 583
f2d33f13 584 /* The function exp may have the `malloc' attribute. */
36dbb93d 585 if (DECL_IS_MALLOC (exp))
f2d33f13
JH
586 flags |= ECF_MALLOC;
587
2a8f6b90 588 /* The function exp may have the `pure' attribute. */
36dbb93d 589 if (DECL_IS_PURE (exp))
53d4257f 590 flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
2a8f6b90 591
f2d33f13
JH
592 if (TREE_NOTHROW (exp))
593 flags |= ECF_NOTHROW;
2b187c63
MM
594
595 if (TREE_READONLY (exp) && ! TREE_THIS_VOLATILE (exp))
4f976745 596 flags |= ECF_LIBCALL_BLOCK | ECF_CONST;
6de9cd9a
DN
597
598 flags = special_function_p (exp, flags);
f2d33f13 599 }
4f976745 600 else if (TYPE_P (exp) && TYPE_READONLY (exp) && ! TREE_THIS_VOLATILE (exp))
2b187c63 601 flags |= ECF_CONST;
f2d33f13
JH
602
603 if (TREE_THIS_VOLATILE (exp))
604 flags |= ECF_NORETURN;
605
b5cd4ed4
RK
606 /* Mark if the function returns with the stack pointer depressed. We
607 cannot consider it pure or constant in that case. */
608 if (TREE_CODE (type) == FUNCTION_TYPE && TYPE_RETURNS_STACK_DEPRESSED (type))
609 {
610 flags |= ECF_SP_DEPRESSED;
53d4257f 611 flags &= ~(ECF_PURE | ECF_CONST | ECF_LIBCALL_BLOCK);
b5cd4ed4
RK
612 }
613
f2d33f13
JH
614 return flags;
615}
616
f027e0a2
JM
617/* Detect flags from a CALL_EXPR. */
618
619int
620call_expr_flags (tree t)
621{
622 int flags;
623 tree decl = get_callee_fndecl (t);
624
625 if (decl)
626 flags = flags_from_decl_or_type (decl);
627 else
628 {
629 t = TREE_TYPE (TREE_OPERAND (t, 0));
630 if (t && TREE_CODE (t) == POINTER_TYPE)
631 flags = flags_from_decl_or_type (TREE_TYPE (t));
632 else
633 flags = 0;
634 }
635
636 return flags;
637}
638
20efdf74
JL
639/* Precompute all register parameters as described by ARGS, storing values
640 into fields within the ARGS array.
641
642 NUM_ACTUALS indicates the total number elements in the ARGS array.
643
644 Set REG_PARM_SEEN if we encounter a register parameter. */
645
646static void
d329e058 647precompute_register_parameters (int num_actuals, struct arg_data *args, int *reg_parm_seen)
20efdf74
JL
648{
649 int i;
650
651 *reg_parm_seen = 0;
652
653 for (i = 0; i < num_actuals; i++)
654 if (args[i].reg != 0 && ! args[i].pass_on_stack)
655 {
656 *reg_parm_seen = 1;
657
658 if (args[i].value == 0)
659 {
660 push_temp_slots ();
661 args[i].value = expand_expr (args[i].tree_value, NULL_RTX,
662 VOIDmode, 0);
663 preserve_temp_slots (args[i].value);
664 pop_temp_slots ();
665
666 /* ANSI doesn't require a sequence point here,
667 but PCC has one, so this will avoid some problems. */
668 emit_queue ();
669 }
670
fd1e5d25
RH
671 /* If the value is a non-legitimate constant, force it into a
672 pseudo now. TLS symbols sometimes need a call to resolve. */
673 if (CONSTANT_P (args[i].value)
674 && !LEGITIMATE_CONSTANT_P (args[i].value))
675 args[i].value = force_reg (args[i].mode, args[i].value);
676
20efdf74
JL
677 /* If we are to promote the function arg to a wider mode,
678 do it now. */
679
680 if (args[i].mode != TYPE_MODE (TREE_TYPE (args[i].tree_value)))
681 args[i].value
682 = convert_modes (args[i].mode,
683 TYPE_MODE (TREE_TYPE (args[i].tree_value)),
684 args[i].value, args[i].unsignedp);
685
f725a3ec 686 /* If the value is expensive, and we are inside an appropriately
20efdf74
JL
687 short loop, put the value into a pseudo and then put the pseudo
688 into the hard reg.
689
690 For small register classes, also do this if this call uses
691 register parameters. This is to avoid reload conflicts while
692 loading the parameters registers. */
693
f8cfc6aa 694 if ((! (REG_P (args[i].value)
20efdf74 695 || (GET_CODE (args[i].value) == SUBREG
f8cfc6aa 696 && REG_P (SUBREG_REG (args[i].value)))))
20efdf74 697 && args[i].mode != BLKmode
b437f1a7 698 && rtx_cost (args[i].value, SET) > COSTS_N_INSNS (1)
20efdf74
JL
699 && ((SMALL_REGISTER_CLASSES && *reg_parm_seen)
700 || preserve_subexpressions_p ()))
701 args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
702 }
703}
704
f73ad30e 705#ifdef REG_PARM_STACK_SPACE
20efdf74
JL
706
707 /* The argument list is the property of the called routine and it
708 may clobber it. If the fixed area has been used for previous
709 parameters, we must save and restore it. */
3bdf5ad1 710
20efdf74 711static rtx
d329e058 712save_fixed_argument_area (int reg_parm_stack_space, rtx argblock, int *low_to_save, int *high_to_save)
20efdf74 713{
b820d2b8
AM
714 int low;
715 int high;
20efdf74 716
b820d2b8
AM
717 /* Compute the boundary of the area that needs to be saved, if any. */
718 high = reg_parm_stack_space;
20efdf74 719#ifdef ARGS_GROW_DOWNWARD
b820d2b8 720 high += 1;
20efdf74 721#endif
b820d2b8
AM
722 if (high > highest_outgoing_arg_in_use)
723 high = highest_outgoing_arg_in_use;
20efdf74 724
b820d2b8
AM
725 for (low = 0; low < high; low++)
726 if (stack_usage_map[low] != 0)
727 {
728 int num_to_save;
729 enum machine_mode save_mode;
730 int delta;
731 rtx stack_area;
732 rtx save_area;
20efdf74 733
b820d2b8
AM
734 while (stack_usage_map[--high] == 0)
735 ;
20efdf74 736
b820d2b8
AM
737 *low_to_save = low;
738 *high_to_save = high;
739
740 num_to_save = high - low + 1;
741 save_mode = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
20efdf74 742
b820d2b8
AM
743 /* If we don't have the required alignment, must do this
744 in BLKmode. */
745 if ((low & (MIN (GET_MODE_SIZE (save_mode),
746 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
747 save_mode = BLKmode;
20efdf74
JL
748
749#ifdef ARGS_GROW_DOWNWARD
b820d2b8 750 delta = -high;
20efdf74 751#else
b820d2b8 752 delta = low;
20efdf74 753#endif
b820d2b8
AM
754 stack_area = gen_rtx_MEM (save_mode,
755 memory_address (save_mode,
756 plus_constant (argblock,
757 delta)));
8ac61af7 758
b820d2b8
AM
759 set_mem_align (stack_area, PARM_BOUNDARY);
760 if (save_mode == BLKmode)
761 {
762 save_area = assign_stack_temp (BLKmode, num_to_save, 0);
763 emit_block_move (validize_mem (save_area), stack_area,
764 GEN_INT (num_to_save), BLOCK_OP_CALL_PARM);
765 }
766 else
767 {
768 save_area = gen_reg_rtx (save_mode);
769 emit_move_insn (save_area, stack_area);
770 }
8ac61af7 771
b820d2b8
AM
772 return save_area;
773 }
774
775 return NULL_RTX;
20efdf74
JL
776}
777
778static void
d329e058 779restore_fixed_argument_area (rtx save_area, rtx argblock, int high_to_save, int low_to_save)
20efdf74
JL
780{
781 enum machine_mode save_mode = GET_MODE (save_area);
b820d2b8
AM
782 int delta;
783 rtx stack_area;
784
20efdf74 785#ifdef ARGS_GROW_DOWNWARD
b820d2b8 786 delta = -high_to_save;
20efdf74 787#else
b820d2b8 788 delta = low_to_save;
20efdf74 789#endif
b820d2b8
AM
790 stack_area = gen_rtx_MEM (save_mode,
791 memory_address (save_mode,
792 plus_constant (argblock, delta)));
793 set_mem_align (stack_area, PARM_BOUNDARY);
20efdf74
JL
794
795 if (save_mode != BLKmode)
796 emit_move_insn (stack_area, save_area);
797 else
44bb111a
RH
798 emit_block_move (stack_area, validize_mem (save_area),
799 GEN_INT (high_to_save - low_to_save + 1),
800 BLOCK_OP_CALL_PARM);
20efdf74 801}
19652adf 802#endif /* REG_PARM_STACK_SPACE */
f725a3ec 803
20efdf74
JL
804/* If any elements in ARGS refer to parameters that are to be passed in
805 registers, but not in memory, and whose alignment does not permit a
806 direct copy into registers. Copy the values into a group of pseudos
f725a3ec 807 which we will later copy into the appropriate hard registers.
8e6a59fe
MM
808
809 Pseudos for each unaligned argument will be stored into the array
810 args[argnum].aligned_regs. The caller is responsible for deallocating
811 the aligned_regs array if it is nonzero. */
812
20efdf74 813static void
d329e058 814store_unaligned_arguments_into_pseudos (struct arg_data *args, int num_actuals)
20efdf74
JL
815{
816 int i, j;
f725a3ec 817
20efdf74
JL
818 for (i = 0; i < num_actuals; i++)
819 if (args[i].reg != 0 && ! args[i].pass_on_stack
820 && args[i].mode == BLKmode
821 && (TYPE_ALIGN (TREE_TYPE (args[i].tree_value))
822 < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
823 {
824 int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
6e985040
AM
825 int nregs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
826 int endian_correction = 0;
20efdf74 827
6e985040 828 args[i].n_aligned_regs = args[i].partial ? args[i].partial : nregs;
703ad42b 829 args[i].aligned_regs = xmalloc (sizeof (rtx) * args[i].n_aligned_regs);
20efdf74 830
6e985040
AM
831 /* Structures smaller than a word are normally aligned to the
832 least significant byte. On a BYTES_BIG_ENDIAN machine,
20efdf74
JL
833 this means we must skip the empty high order bytes when
834 calculating the bit offset. */
6e985040
AM
835 if (bytes < UNITS_PER_WORD
836#ifdef BLOCK_REG_PADDING
837 && (BLOCK_REG_PADDING (args[i].mode,
838 TREE_TYPE (args[i].tree_value), 1)
839 == downward)
840#else
841 && BYTES_BIG_ENDIAN
842#endif
843 )
844 endian_correction = BITS_PER_WORD - bytes * BITS_PER_UNIT;
20efdf74
JL
845
846 for (j = 0; j < args[i].n_aligned_regs; j++)
847 {
848 rtx reg = gen_reg_rtx (word_mode);
849 rtx word = operand_subword_force (args[i].value, j, BLKmode);
850 int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
20efdf74
JL
851
852 args[i].aligned_regs[j] = reg;
6e985040
AM
853 word = extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
854 word_mode, word_mode, BITS_PER_WORD);
20efdf74
JL
855
856 /* There is no need to restrict this code to loading items
857 in TYPE_ALIGN sized hunks. The bitfield instructions can
858 load up entire word sized registers efficiently.
859
860 ??? This may not be needed anymore.
861 We use to emit a clobber here but that doesn't let later
862 passes optimize the instructions we emit. By storing 0 into
863 the register later passes know the first AND to zero out the
864 bitfield being set in the register is unnecessary. The store
865 of 0 will be deleted as will at least the first AND. */
866
867 emit_move_insn (reg, const0_rtx);
868
869 bytes -= bitsize / BITS_PER_UNIT;
6e985040
AM
870 store_bit_field (reg, bitsize, endian_correction, word_mode,
871 word, BITS_PER_WORD);
20efdf74
JL
872 }
873 }
874}
875
d7cdf113 876/* Fill in ARGS_SIZE and ARGS array based on the parameters found in
f725a3ec 877 ACTPARMS.
d7cdf113
JL
878
879 NUM_ACTUALS is the total number of parameters.
880
881 N_NAMED_ARGS is the total number of named arguments.
882
883 FNDECL is the tree code for the target of this call (if known)
884
885 ARGS_SO_FAR holds state needed by the target to know where to place
886 the next argument.
887
888 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
889 for arguments which are passed in registers.
890
891 OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
892 and may be modified by this routine.
893
f2d33f13 894 OLD_PENDING_ADJ, MUST_PREALLOCATE and FLAGS are pointers to integer
dd292d0a
MM
895 flags which may may be modified by this routine.
896
6de9cd9a
DN
897 MAY_TAILCALL is cleared if we encounter an invisible pass-by-reference
898 that requires allocation of stack space.
899
dd292d0a
MM
900 CALL_FROM_THUNK_P is true if this call is the jump from a thunk to
901 the thunked-to function. */
d7cdf113
JL
902
903static void
d329e058
AJ
904initialize_argument_information (int num_actuals ATTRIBUTE_UNUSED,
905 struct arg_data *args,
906 struct args_size *args_size,
907 int n_named_args ATTRIBUTE_UNUSED,
908 tree actparms, tree fndecl,
909 CUMULATIVE_ARGS *args_so_far,
910 int reg_parm_stack_space,
911 rtx *old_stack_level, int *old_pending_adj,
dd292d0a 912 int *must_preallocate, int *ecf_flags,
6de9cd9a 913 bool *may_tailcall, bool call_from_thunk_p)
d7cdf113
JL
914{
915 /* 1 if scanning parms front to back, -1 if scanning back to front. */
916 int inc;
917
918 /* Count arg position in order args appear. */
919 int argpos;
920
921 int i;
922 tree p;
f725a3ec 923
d7cdf113
JL
924 args_size->constant = 0;
925 args_size->var = 0;
926
927 /* In this loop, we consider args in the order they are written.
928 We fill up ARGS from the front or from the back if necessary
929 so that in any case the first arg to be pushed ends up at the front. */
930
f73ad30e
JH
931 if (PUSH_ARGS_REVERSED)
932 {
933 i = num_actuals - 1, inc = -1;
934 /* In this case, must reverse order of args
935 so that we compute and push the last arg first. */
936 }
937 else
938 {
939 i = 0, inc = 1;
940 }
d7cdf113
JL
941
942 /* I counts args in order (to be) pushed; ARGPOS counts in order written. */
943 for (p = actparms, argpos = 0; p; p = TREE_CHAIN (p), i += inc, argpos++)
944 {
945 tree type = TREE_TYPE (TREE_VALUE (p));
946 int unsignedp;
947 enum machine_mode mode;
948
949 args[i].tree_value = TREE_VALUE (p);
950
951 /* Replace erroneous argument with constant zero. */
d0f062fb 952 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
d7cdf113
JL
953 args[i].tree_value = integer_zero_node, type = integer_type_node;
954
955 /* If TYPE is a transparent union, pass things the way we would
956 pass the first field of the union. We have already verified that
957 the modes are the same. */
2bf105ab 958 if (TREE_CODE (type) == UNION_TYPE && TYPE_TRANSPARENT_UNION (type))
d7cdf113
JL
959 type = TREE_TYPE (TYPE_FIELDS (type));
960
961 /* Decide where to pass this arg.
962
963 args[i].reg is nonzero if all or part is passed in registers.
964
965 args[i].partial is nonzero if part but not all is passed in registers,
966 and the exact value says how many words are passed in registers.
967
968 args[i].pass_on_stack is nonzero if the argument must at least be
969 computed on the stack. It may then be loaded back into registers
970 if args[i].reg is nonzero.
971
972 These decisions are driven by the FUNCTION_... macros and must agree
973 with those made by function.c. */
974
975 /* See if this argument should be passed by invisible reference. */
7a6cdb44 976 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
d7cdf113
JL
977 || TREE_ADDRESSABLE (type)
978#ifdef FUNCTION_ARG_PASS_BY_REFERENCE
959f3a06 979 || FUNCTION_ARG_PASS_BY_REFERENCE (*args_so_far, TYPE_MODE (type),
d7cdf113
JL
980 type, argpos < n_named_args)
981#endif
982 )
983 {
984 /* If we're compiling a thunk, pass through invisible
985 references instead of making a copy. */
dd292d0a 986 if (call_from_thunk_p
d7cdf113 987#ifdef FUNCTION_ARG_CALLEE_COPIES
959f3a06 988 || (FUNCTION_ARG_CALLEE_COPIES (*args_so_far, TYPE_MODE (type),
d7cdf113
JL
989 type, argpos < n_named_args)
990 /* If it's in a register, we must make a copy of it too. */
991 /* ??? Is this a sufficient test? Is there a better one? */
992 && !(TREE_CODE (args[i].tree_value) == VAR_DECL
993 && REG_P (DECL_RTL (args[i].tree_value)))
994 && ! TREE_ADDRESSABLE (type))
995#endif
996 )
997 {
998 /* C++ uses a TARGET_EXPR to indicate that we want to make a
999 new object from the argument. If we are passing by
1000 invisible reference, the callee will do that for us, so we
1001 can strip off the TARGET_EXPR. This is not always safe,
1002 but it is safe in the only case where this is a useful
1003 optimization; namely, when the argument is a plain object.
1004 In that case, the frontend is just asking the backend to
f725a3ec
KH
1005 make a bitwise copy of the argument. */
1006
d7cdf113 1007 if (TREE_CODE (args[i].tree_value) == TARGET_EXPR
2f939d94 1008 && (DECL_P (TREE_OPERAND (args[i].tree_value, 1)))
d7cdf113
JL
1009 && ! REG_P (DECL_RTL (TREE_OPERAND (args[i].tree_value, 1))))
1010 args[i].tree_value = TREE_OPERAND (args[i].tree_value, 1);
1011
9fd47435
RS
1012 /* We can't use sibcalls if a callee-copied argument is stored
1013 in the current function's frame. */
1014 if (!call_from_thunk_p
1015 && (!DECL_P (args[i].tree_value)
1016 || !TREE_STATIC (args[i].tree_value)))
1017 *may_tailcall = false;
1018
d7cdf113
JL
1019 args[i].tree_value = build1 (ADDR_EXPR,
1020 build_pointer_type (type),
1021 args[i].tree_value);
1022 type = build_pointer_type (type);
1023 }
f21add07
JM
1024 else if (TREE_CODE (args[i].tree_value) == TARGET_EXPR)
1025 {
1026 /* In the V3 C++ ABI, parameters are destroyed in the caller.
1027 We implement this by passing the address of the temporary
1028 rather than expanding it into another allocated slot. */
1029 args[i].tree_value = build1 (ADDR_EXPR,
1030 build_pointer_type (type),
1031 args[i].tree_value);
1032 type = build_pointer_type (type);
6de9cd9a 1033 *may_tailcall = false;
f21add07 1034 }
d7cdf113
JL
1035 else
1036 {
1037 /* We make a copy of the object and pass the address to the
1038 function being called. */
1039 rtx copy;
1040
d0f062fb 1041 if (!COMPLETE_TYPE_P (type)
d7cdf113
JL
1042 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
1043 || (flag_stack_check && ! STACK_CHECK_BUILTIN
05bccae2
RK
1044 && (0 < compare_tree_int (TYPE_SIZE_UNIT (type),
1045 STACK_CHECK_MAX_VAR_SIZE))))
d7cdf113
JL
1046 {
1047 /* This is a variable-sized object. Make space on the stack
1048 for it. */
1049 rtx size_rtx = expr_size (TREE_VALUE (p));
1050
1051 if (*old_stack_level == 0)
1052 {
1053 emit_stack_save (SAVE_BLOCK, old_stack_level, NULL_RTX);
1054 *old_pending_adj = pending_stack_adjust;
1055 pending_stack_adjust = 0;
1056 }
1057
1058 copy = gen_rtx_MEM (BLKmode,
3bdf5ad1
RK
1059 allocate_dynamic_stack_space
1060 (size_rtx, NULL_RTX, TYPE_ALIGN (type)));
1061 set_mem_attributes (copy, type, 1);
d7cdf113
JL
1062 }
1063 else
3bdf5ad1 1064 copy = assign_temp (type, 0, 1, 0);
d7cdf113
JL
1065
1066 store_expr (args[i].tree_value, copy, 0);
53d4257f 1067 *ecf_flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
d7cdf113
JL
1068
1069 args[i].tree_value = build1 (ADDR_EXPR,
1070 build_pointer_type (type),
1071 make_tree (type, copy));
1072 type = build_pointer_type (type);
6de9cd9a 1073 *may_tailcall = false;
d7cdf113
JL
1074 }
1075 }
1076
1077 mode = TYPE_MODE (type);
8df83eae 1078 unsignedp = TYPE_UNSIGNED (type);
d7cdf113 1079
61f71b34
DD
1080 if (targetm.calls.promote_function_args (fndecl ? TREE_TYPE (fndecl) : 0))
1081 mode = promote_mode (type, mode, &unsignedp, 1);
d7cdf113
JL
1082
1083 args[i].unsignedp = unsignedp;
1084 args[i].mode = mode;
7d167afd 1085
099e9712
JH
1086 args[i].reg = FUNCTION_ARG (*args_so_far, mode, type,
1087 argpos < n_named_args);
7d167afd
JJ
1088#ifdef FUNCTION_INCOMING_ARG
1089 /* If this is a sibling call and the machine has register windows, the
1090 register window has to be unwinded before calling the routine, so
1091 arguments have to go into the incoming registers. */
099e9712 1092 args[i].tail_call_reg = FUNCTION_INCOMING_ARG (*args_so_far, mode, type,
f725a3ec 1093 argpos < n_named_args);
099e9712
JH
1094#else
1095 args[i].tail_call_reg = args[i].reg;
7d167afd 1096#endif
7d167afd 1097
d7cdf113
JL
1098#ifdef FUNCTION_ARG_PARTIAL_NREGS
1099 if (args[i].reg)
1100 args[i].partial
959f3a06 1101 = FUNCTION_ARG_PARTIAL_NREGS (*args_so_far, mode, type,
d7cdf113
JL
1102 argpos < n_named_args);
1103#endif
1104
1105 args[i].pass_on_stack = MUST_PASS_IN_STACK (mode, type);
1106
1107 /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
1108 it means that we are to pass this arg in the register(s) designated
1109 by the PARALLEL, but also to pass it in the stack. */
1110 if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
1111 && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
1112 args[i].pass_on_stack = 1;
1113
1114 /* If this is an addressable type, we must preallocate the stack
1115 since we must evaluate the object into its final location.
1116
1117 If this is to be passed in both registers and the stack, it is simpler
1118 to preallocate. */
1119 if (TREE_ADDRESSABLE (type)
1120 || (args[i].pass_on_stack && args[i].reg != 0))
1121 *must_preallocate = 1;
1122
1123 /* If this is an addressable type, we cannot pre-evaluate it. Thus,
1124 we cannot consider this function call constant. */
1125 if (TREE_ADDRESSABLE (type))
53d4257f 1126 *ecf_flags &= ~ECF_LIBCALL_BLOCK;
d7cdf113
JL
1127
1128 /* Compute the stack-size of this argument. */
1129 if (args[i].reg == 0 || args[i].partial != 0
1130 || reg_parm_stack_space > 0
1131 || args[i].pass_on_stack)
1132 locate_and_pad_parm (mode, type,
1133#ifdef STACK_PARMS_IN_REG_PARM_AREA
1134 1,
1135#else
1136 args[i].reg != 0,
1137#endif
e7949876
AM
1138 args[i].pass_on_stack ? 0 : args[i].partial,
1139 fndecl, args_size, &args[i].locate);
648bb159
RS
1140#ifdef BLOCK_REG_PADDING
1141 else
1142 /* The argument is passed entirely in registers. See at which
1143 end it should be padded. */
1144 args[i].locate.where_pad =
1145 BLOCK_REG_PADDING (mode, type,
1146 int_size_in_bytes (type) <= UNITS_PER_WORD);
1147#endif
f725a3ec 1148
d7cdf113
JL
1149 /* Update ARGS_SIZE, the total stack space for args so far. */
1150
e7949876
AM
1151 args_size->constant += args[i].locate.size.constant;
1152 if (args[i].locate.size.var)
1153 ADD_PARM_SIZE (*args_size, args[i].locate.size.var);
d7cdf113
JL
1154
1155 /* Increment ARGS_SO_FAR, which has info about which arg-registers
1156 have been used, etc. */
1157
959f3a06 1158 FUNCTION_ARG_ADVANCE (*args_so_far, TYPE_MODE (type), type,
d7cdf113
JL
1159 argpos < n_named_args);
1160 }
1161}
1162
599f37b6
JL
1163/* Update ARGS_SIZE to contain the total size for the argument block.
1164 Return the original constant component of the argument block's size.
1165
1166 REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
1167 for arguments passed in registers. */
1168
1169static int
d329e058
AJ
1170compute_argument_block_size (int reg_parm_stack_space,
1171 struct args_size *args_size,
1172 int preferred_stack_boundary ATTRIBUTE_UNUSED)
599f37b6
JL
1173{
1174 int unadjusted_args_size = args_size->constant;
1175
f73ad30e
JH
1176 /* For accumulate outgoing args mode we don't need to align, since the frame
1177 will be already aligned. Align to STACK_BOUNDARY in order to prevent
f5143c46 1178 backends from generating misaligned frame sizes. */
f73ad30e
JH
1179 if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
1180 preferred_stack_boundary = STACK_BOUNDARY;
f73ad30e 1181
599f37b6
JL
1182 /* Compute the actual size of the argument block required. The variable
1183 and constant sizes must be combined, the size may have to be rounded,
1184 and there may be a minimum required size. */
1185
1186 if (args_size->var)
1187 {
1188 args_size->var = ARGS_SIZE_TREE (*args_size);
1189 args_size->constant = 0;
1190
c2f8b491
JH
1191 preferred_stack_boundary /= BITS_PER_UNIT;
1192 if (preferred_stack_boundary > 1)
1503a7ec
JH
1193 {
1194 /* We don't handle this case yet. To handle it correctly we have
f5143c46 1195 to add the delta, round and subtract the delta.
1503a7ec
JH
1196 Currently no machine description requires this support. */
1197 if (stack_pointer_delta & (preferred_stack_boundary - 1))
f725a3ec 1198 abort ();
1503a7ec
JH
1199 args_size->var = round_up (args_size->var, preferred_stack_boundary);
1200 }
599f37b6
JL
1201
1202 if (reg_parm_stack_space > 0)
1203 {
1204 args_size->var
1205 = size_binop (MAX_EXPR, args_size->var,
fed3cef0 1206 ssize_int (reg_parm_stack_space));
599f37b6
JL
1207
1208#ifndef OUTGOING_REG_PARM_STACK_SPACE
1209 /* The area corresponding to register parameters is not to count in
1210 the size of the block we need. So make the adjustment. */
1211 args_size->var
1212 = size_binop (MINUS_EXPR, args_size->var,
fed3cef0 1213 ssize_int (reg_parm_stack_space));
599f37b6
JL
1214#endif
1215 }
1216 }
1217 else
1218 {
c2f8b491 1219 preferred_stack_boundary /= BITS_PER_UNIT;
0a1c58a2
JL
1220 if (preferred_stack_boundary < 1)
1221 preferred_stack_boundary = 1;
fb5eebb9 1222 args_size->constant = (((args_size->constant
1503a7ec 1223 + stack_pointer_delta
c2f8b491
JH
1224 + preferred_stack_boundary - 1)
1225 / preferred_stack_boundary
1226 * preferred_stack_boundary)
1503a7ec 1227 - stack_pointer_delta);
599f37b6
JL
1228
1229 args_size->constant = MAX (args_size->constant,
1230 reg_parm_stack_space);
1231
599f37b6
JL
1232#ifndef OUTGOING_REG_PARM_STACK_SPACE
1233 args_size->constant -= reg_parm_stack_space;
1234#endif
1235 }
1236 return unadjusted_args_size;
1237}
1238
19832c77 1239/* Precompute parameters as needed for a function call.
cc0b1adc 1240
f2d33f13 1241 FLAGS is mask of ECF_* constants.
cc0b1adc 1242
cc0b1adc
JL
1243 NUM_ACTUALS is the number of arguments.
1244
f725a3ec
KH
1245 ARGS is an array containing information for each argument; this
1246 routine fills in the INITIAL_VALUE and VALUE fields for each
1247 precomputed argument. */
cc0b1adc
JL
1248
1249static void
d329e058 1250precompute_arguments (int flags, int num_actuals, struct arg_data *args)
cc0b1adc
JL
1251{
1252 int i;
1253
3638733b 1254 /* If this is a libcall, then precompute all arguments so that we do not
82c82743
RH
1255 get extraneous instructions emitted as part of the libcall sequence. */
1256 if ((flags & ECF_LIBCALL_BLOCK) == 0)
1257 return;
1258
cc0b1adc 1259 for (i = 0; i < num_actuals; i++)
82c82743
RH
1260 {
1261 enum machine_mode mode;
ddef6bc7 1262
82c82743
RH
1263 /* If this is an addressable type, we cannot pre-evaluate it. */
1264 if (TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value)))
1265 abort ();
cc0b1adc 1266
82c82743
RH
1267 args[i].value
1268 = expand_expr (args[i].tree_value, NULL_RTX, VOIDmode, 0);
cc0b1adc 1269
82c82743
RH
1270 /* ANSI doesn't require a sequence point here,
1271 but PCC has one, so this will avoid some problems. */
1272 emit_queue ();
cc0b1adc 1273
82c82743
RH
1274 args[i].initial_value = args[i].value
1275 = protect_from_queue (args[i].value, 0);
cc0b1adc 1276
82c82743
RH
1277 mode = TYPE_MODE (TREE_TYPE (args[i].tree_value));
1278 if (mode != args[i].mode)
1279 {
1280 args[i].value
1281 = convert_modes (args[i].mode, mode,
1282 args[i].value, args[i].unsignedp);
9e1622ed 1283#if defined(PROMOTE_FUNCTION_MODE) && !defined(PROMOTE_MODE)
82c82743
RH
1284 /* CSE will replace this only if it contains args[i].value
1285 pseudo, so convert it down to the declared mode using
1286 a SUBREG. */
1287 if (REG_P (args[i].value)
1288 && GET_MODE_CLASS (args[i].mode) == MODE_INT)
1289 {
1290 args[i].initial_value
1291 = gen_lowpart_SUBREG (mode, args[i].value);
1292 SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1293 SUBREG_PROMOTED_UNSIGNED_SET (args[i].initial_value,
1294 args[i].unsignedp);
1295 }
47841d1b 1296#endif
82c82743
RH
1297 }
1298 }
cc0b1adc
JL
1299}
1300
0f9b3ea6
JL
1301/* Given the current state of MUST_PREALLOCATE and information about
1302 arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
1303 compute and return the final value for MUST_PREALLOCATE. */
1304
1305static int
d329e058 1306finalize_must_preallocate (int must_preallocate, int num_actuals, struct arg_data *args, struct args_size *args_size)
0f9b3ea6
JL
1307{
1308 /* See if we have or want to preallocate stack space.
1309
1310 If we would have to push a partially-in-regs parm
1311 before other stack parms, preallocate stack space instead.
1312
1313 If the size of some parm is not a multiple of the required stack
1314 alignment, we must preallocate.
1315
1316 If the total size of arguments that would otherwise create a copy in
1317 a temporary (such as a CALL) is more than half the total argument list
1318 size, preallocation is faster.
1319
1320 Another reason to preallocate is if we have a machine (like the m88k)
1321 where stack alignment is required to be maintained between every
1322 pair of insns, not just when the call is made. However, we assume here
1323 that such machines either do not have push insns (and hence preallocation
1324 would occur anyway) or the problem is taken care of with
1325 PUSH_ROUNDING. */
1326
1327 if (! must_preallocate)
1328 {
1329 int partial_seen = 0;
1330 int copy_to_evaluate_size = 0;
1331 int i;
1332
1333 for (i = 0; i < num_actuals && ! must_preallocate; i++)
1334 {
1335 if (args[i].partial > 0 && ! args[i].pass_on_stack)
1336 partial_seen = 1;
1337 else if (partial_seen && args[i].reg == 0)
1338 must_preallocate = 1;
1339
1340 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1341 && (TREE_CODE (args[i].tree_value) == CALL_EXPR
1342 || TREE_CODE (args[i].tree_value) == TARGET_EXPR
1343 || TREE_CODE (args[i].tree_value) == COND_EXPR
1344 || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
1345 copy_to_evaluate_size
1346 += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1347 }
1348
1349 if (copy_to_evaluate_size * 2 >= args_size->constant
1350 && args_size->constant > 0)
1351 must_preallocate = 1;
1352 }
1353 return must_preallocate;
1354}
599f37b6 1355
a45bdd02
JL
1356/* If we preallocated stack space, compute the address of each argument
1357 and store it into the ARGS array.
1358
f725a3ec 1359 We need not ensure it is a valid memory address here; it will be
a45bdd02
JL
1360 validized when it is used.
1361
1362 ARGBLOCK is an rtx for the address of the outgoing arguments. */
1363
1364static void
d329e058 1365compute_argument_addresses (struct arg_data *args, rtx argblock, int num_actuals)
a45bdd02
JL
1366{
1367 if (argblock)
1368 {
1369 rtx arg_reg = argblock;
1370 int i, arg_offset = 0;
1371
1372 if (GET_CODE (argblock) == PLUS)
1373 arg_reg = XEXP (argblock, 0), arg_offset = INTVAL (XEXP (argblock, 1));
1374
1375 for (i = 0; i < num_actuals; i++)
1376 {
e7949876
AM
1377 rtx offset = ARGS_SIZE_RTX (args[i].locate.offset);
1378 rtx slot_offset = ARGS_SIZE_RTX (args[i].locate.slot_offset);
a45bdd02
JL
1379 rtx addr;
1380
1381 /* Skip this parm if it will not be passed on the stack. */
1382 if (! args[i].pass_on_stack && args[i].reg != 0)
1383 continue;
1384
1385 if (GET_CODE (offset) == CONST_INT)
1386 addr = plus_constant (arg_reg, INTVAL (offset));
1387 else
1388 addr = gen_rtx_PLUS (Pmode, arg_reg, offset);
1389
1390 addr = plus_constant (addr, arg_offset);
1391 args[i].stack = gen_rtx_MEM (args[i].mode, addr);
d1a74aa7 1392 set_mem_align (args[i].stack, PARM_BOUNDARY);
3bdf5ad1
RK
1393 set_mem_attributes (args[i].stack,
1394 TREE_TYPE (args[i].tree_value), 1);
a45bdd02
JL
1395
1396 if (GET_CODE (slot_offset) == CONST_INT)
1397 addr = plus_constant (arg_reg, INTVAL (slot_offset));
1398 else
1399 addr = gen_rtx_PLUS (Pmode, arg_reg, slot_offset);
1400
1401 addr = plus_constant (addr, arg_offset);
1402 args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
d1a74aa7 1403 set_mem_align (args[i].stack_slot, PARM_BOUNDARY);
3bdf5ad1
RK
1404 set_mem_attributes (args[i].stack_slot,
1405 TREE_TYPE (args[i].tree_value), 1);
7ab923cc
JJ
1406
1407 /* Function incoming arguments may overlap with sibling call
1408 outgoing arguments and we cannot allow reordering of reads
1409 from function arguments with stores to outgoing arguments
1410 of sibling calls. */
ba4828e0
RK
1411 set_mem_alias_set (args[i].stack, 0);
1412 set_mem_alias_set (args[i].stack_slot, 0);
a45bdd02
JL
1413 }
1414 }
1415}
f725a3ec 1416
a45bdd02
JL
1417/* Given a FNDECL and EXP, return an rtx suitable for use as a target address
1418 in a call instruction.
1419
1420 FNDECL is the tree node for the target function. For an indirect call
1421 FNDECL will be NULL_TREE.
1422
09e2bf48 1423 ADDR is the operand 0 of CALL_EXPR for this call. */
a45bdd02
JL
1424
1425static rtx
d329e058 1426rtx_for_function_call (tree fndecl, tree addr)
a45bdd02
JL
1427{
1428 rtx funexp;
1429
1430 /* Get the function to call, in the form of RTL. */
1431 if (fndecl)
1432 {
1433 /* If this is the first use of the function, see if we need to
1434 make an external definition for it. */
1435 if (! TREE_USED (fndecl))
1436 {
1437 assemble_external (fndecl);
1438 TREE_USED (fndecl) = 1;
1439 }
1440
1441 /* Get a SYMBOL_REF rtx for the function address. */
1442 funexp = XEXP (DECL_RTL (fndecl), 0);
1443 }
1444 else
1445 /* Generate an rtx (probably a pseudo-register) for the address. */
1446 {
1447 push_temp_slots ();
09e2bf48 1448 funexp = expand_expr (addr, NULL_RTX, VOIDmode, 0);
f725a3ec 1449 pop_temp_slots (); /* FUNEXP can't be BLKmode. */
a45bdd02
JL
1450 emit_queue ();
1451 }
1452 return funexp;
1453}
1454
21a3b983
JL
1455/* Do the register loads required for any wholly-register parms or any
1456 parms which are passed both on the stack and in a register. Their
f725a3ec 1457 expressions were already evaluated.
21a3b983
JL
1458
1459 Mark all register-parms as living through the call, putting these USE
d329e058
AJ
1460 insns in the CALL_INSN_FUNCTION_USAGE field.
1461
0cdca92b
DJ
1462 When IS_SIBCALL, perform the check_sibcall_overlap_argument_overlap
1463 checking, setting *SIBCALL_FAILURE if appropriate. */
21a3b983
JL
1464
1465static void
d329e058
AJ
1466load_register_parameters (struct arg_data *args, int num_actuals,
1467 rtx *call_fusage, int flags, int is_sibcall,
1468 int *sibcall_failure)
21a3b983
JL
1469{
1470 int i, j;
1471
21a3b983 1472 for (i = 0; i < num_actuals; i++)
21a3b983 1473 {
099e9712
JH
1474 rtx reg = ((flags & ECF_SIBCALL)
1475 ? args[i].tail_call_reg : args[i].reg);
21a3b983
JL
1476 if (reg)
1477 {
6e985040
AM
1478 int partial = args[i].partial;
1479 int nregs;
1480 int size = 0;
0cdca92b 1481 rtx before_arg = get_last_insn ();
21a3b983
JL
1482 /* Set to non-negative if must move a word at a time, even if just
1483 one word (e.g, partial == 1 && mode == DFmode). Set to -1 if
1484 we just use a normal move insn. This value can be zero if the
1485 argument is a zero size structure with no fields. */
6e985040
AM
1486 nregs = -1;
1487 if (partial)
1488 nregs = partial;
1489 else if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode)
1490 {
1491 size = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1492 nregs = (size + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
1493 }
1494 else
1495 size = GET_MODE_SIZE (args[i].mode);
21a3b983
JL
1496
1497 /* Handle calls that pass values in multiple non-contiguous
1498 locations. The Irix 6 ABI has examples of this. */
1499
1500 if (GET_CODE (reg) == PARALLEL)
6e985040
AM
1501 {
1502 tree type = TREE_TYPE (args[i].tree_value);
1503 emit_group_load (reg, args[i].value, type,
1504 int_size_in_bytes (type));
1505 }
21a3b983
JL
1506
1507 /* If simple case, just do move. If normal partial, store_one_arg
1508 has already loaded the register for us. In all other cases,
1509 load the register(s) from memory. */
1510
9206d736
AM
1511 else if (nregs == -1)
1512 {
1513 emit_move_insn (reg, args[i].value);
6e985040 1514#ifdef BLOCK_REG_PADDING
9206d736
AM
1515 /* Handle case where we have a value that needs shifting
1516 up to the msb. eg. a QImode value and we're padding
1517 upward on a BYTES_BIG_ENDIAN machine. */
1518 if (size < UNITS_PER_WORD
1519 && (args[i].locate.where_pad
1520 == (BYTES_BIG_ENDIAN ? upward : downward)))
1521 {
9206d736
AM
1522 rtx x;
1523 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
980f6e8e
AM
1524
1525 /* Assigning REG here rather than a temp makes CALL_FUSAGE
1526 report the whole reg as used. Strictly speaking, the
1527 call only uses SIZE bytes at the msb end, but it doesn't
1528 seem worth generating rtl to say that. */
1529 reg = gen_rtx_REG (word_mode, REGNO (reg));
09b52670
RS
1530 x = expand_shift (LSHIFT_EXPR, word_mode, reg,
1531 build_int_2 (shift, 0), reg, 1);
980f6e8e
AM
1532 if (x != reg)
1533 emit_move_insn (reg, x);
9206d736 1534 }
6e985040 1535#endif
9206d736 1536 }
21a3b983
JL
1537
1538 /* If we have pre-computed the values to put in the registers in
1539 the case of non-aligned structures, copy them in now. */
1540
1541 else if (args[i].n_aligned_regs != 0)
1542 for (j = 0; j < args[i].n_aligned_regs; j++)
1543 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
1544 args[i].aligned_regs[j]);
1545
1546 else if (partial == 0 || args[i].pass_on_stack)
6e985040
AM
1547 {
1548 rtx mem = validize_mem (args[i].value);
1549
6e985040 1550 /* Handle a BLKmode that needs shifting. */
9206d736 1551 if (nregs == 1 && size < UNITS_PER_WORD
03ca1672
UW
1552#ifdef BLOCK_REG_PADDING
1553 && args[i].locate.where_pad == downward
1554#else
1555 && BYTES_BIG_ENDIAN
1556#endif
1557 )
6e985040
AM
1558 {
1559 rtx tem = operand_subword_force (mem, 0, args[i].mode);
1560 rtx ri = gen_rtx_REG (word_mode, REGNO (reg));
1561 rtx x = gen_reg_rtx (word_mode);
1562 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
09b52670
RS
1563 enum tree_code dir = BYTES_BIG_ENDIAN ? RSHIFT_EXPR
1564 : LSHIFT_EXPR;
6e985040
AM
1565
1566 emit_move_insn (x, tem);
09b52670
RS
1567 x = expand_shift (dir, word_mode, x,
1568 build_int_2 (shift, 0), ri, 1);
6e985040
AM
1569 if (x != ri)
1570 emit_move_insn (ri, x);
1571 }
1572 else
6e985040
AM
1573 move_block_to_reg (REGNO (reg), mem, nregs, args[i].mode);
1574 }
21a3b983 1575
0cdca92b
DJ
1576 /* When a parameter is a block, and perhaps in other cases, it is
1577 possible that it did a load from an argument slot that was
32dd366d 1578 already clobbered. */
0cdca92b
DJ
1579 if (is_sibcall
1580 && check_sibcall_argument_overlap (before_arg, &args[i], 0))
1581 *sibcall_failure = 1;
1582
21a3b983
JL
1583 /* Handle calls that pass values in multiple non-contiguous
1584 locations. The Irix 6 ABI has examples of this. */
1585 if (GET_CODE (reg) == PARALLEL)
1586 use_group_regs (call_fusage, reg);
1587 else if (nregs == -1)
1588 use_reg (call_fusage, reg);
1589 else
1590 use_regs (call_fusage, REGNO (reg), nregs == 0 ? 1 : nregs);
1591 }
1592 }
1593}
1594
739fb049
MM
1595/* We need to pop PENDING_STACK_ADJUST bytes. But, if the arguments
1596 wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
1597 bytes, then we would need to push some additional bytes to pad the
ce48579b
RH
1598 arguments. So, we compute an adjust to the stack pointer for an
1599 amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
1600 bytes. Then, when the arguments are pushed the stack will be perfectly
1601 aligned. ARGS_SIZE->CONSTANT is set to the number of bytes that should
1602 be popped after the call. Returns the adjustment. */
739fb049 1603
ce48579b 1604static int
d329e058
AJ
1605combine_pending_stack_adjustment_and_call (int unadjusted_args_size,
1606 struct args_size *args_size,
1607 int preferred_unit_stack_boundary)
739fb049
MM
1608{
1609 /* The number of bytes to pop so that the stack will be
1610 under-aligned by UNADJUSTED_ARGS_SIZE bytes. */
1611 HOST_WIDE_INT adjustment;
1612 /* The alignment of the stack after the arguments are pushed, if we
1613 just pushed the arguments without adjust the stack here. */
1614 HOST_WIDE_INT unadjusted_alignment;
1615
f725a3ec 1616 unadjusted_alignment
739fb049
MM
1617 = ((stack_pointer_delta + unadjusted_args_size)
1618 % preferred_unit_stack_boundary);
1619
1620 /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
1621 as possible -- leaving just enough left to cancel out the
1622 UNADJUSTED_ALIGNMENT. In other words, we want to ensure that the
1623 PENDING_STACK_ADJUST is non-negative, and congruent to
1624 -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY. */
1625
1626 /* Begin by trying to pop all the bytes. */
f725a3ec
KH
1627 unadjusted_alignment
1628 = (unadjusted_alignment
739fb049
MM
1629 - (pending_stack_adjust % preferred_unit_stack_boundary));
1630 adjustment = pending_stack_adjust;
1631 /* Push enough additional bytes that the stack will be aligned
1632 after the arguments are pushed. */
e079dcdb
HB
1633 if (preferred_unit_stack_boundary > 1)
1634 {
3e555c7d 1635 if (unadjusted_alignment > 0)
f725a3ec 1636 adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
e079dcdb 1637 else
f725a3ec 1638 adjustment += unadjusted_alignment;
e079dcdb 1639 }
f725a3ec 1640
739fb049
MM
1641 /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
1642 bytes after the call. The right number is the entire
1643 PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
1644 by the arguments in the first place. */
f725a3ec 1645 args_size->constant
739fb049
MM
1646 = pending_stack_adjust - adjustment + unadjusted_args_size;
1647
ce48579b 1648 return adjustment;
739fb049
MM
1649}
1650
c67846f2
JJ
1651/* Scan X expression if it does not dereference any argument slots
1652 we already clobbered by tail call arguments (as noted in stored_args_map
1653 bitmap).
da7d8304 1654 Return nonzero if X expression dereferences such argument slots,
c67846f2
JJ
1655 zero otherwise. */
1656
1657static int
d329e058 1658check_sibcall_argument_overlap_1 (rtx x)
c67846f2
JJ
1659{
1660 RTX_CODE code;
1661 int i, j;
1662 unsigned int k;
1663 const char *fmt;
1664
1665 if (x == NULL_RTX)
1666 return 0;
1667
1668 code = GET_CODE (x);
1669
1670 if (code == MEM)
1671 {
1672 if (XEXP (x, 0) == current_function_internal_arg_pointer)
1673 i = 0;
4c6b3b2a
JJ
1674 else if (GET_CODE (XEXP (x, 0)) == PLUS
1675 && XEXP (XEXP (x, 0), 0) ==
1676 current_function_internal_arg_pointer
1677 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
c67846f2
JJ
1678 i = INTVAL (XEXP (XEXP (x, 0), 1));
1679 else
1680 return 0;
1681
d60eab50
AO
1682#ifdef ARGS_GROW_DOWNWARD
1683 i = -i - GET_MODE_SIZE (GET_MODE (x));
1684#endif
1685
c67846f2
JJ
1686 for (k = 0; k < GET_MODE_SIZE (GET_MODE (x)); k++)
1687 if (i + k < stored_args_map->n_bits
1688 && TEST_BIT (stored_args_map, i + k))
1689 return 1;
1690
1691 return 0;
1692 }
1693
f725a3ec 1694 /* Scan all subexpressions. */
c67846f2
JJ
1695 fmt = GET_RTX_FORMAT (code);
1696 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
1697 {
1698 if (*fmt == 'e')
f725a3ec
KH
1699 {
1700 if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
1701 return 1;
1702 }
c67846f2 1703 else if (*fmt == 'E')
f725a3ec
KH
1704 {
1705 for (j = 0; j < XVECLEN (x, i); j++)
1706 if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
1707 return 1;
1708 }
c67846f2
JJ
1709 }
1710 return 0;
c67846f2
JJ
1711}
1712
1713/* Scan sequence after INSN if it does not dereference any argument slots
1714 we already clobbered by tail call arguments (as noted in stored_args_map
0cdca92b
DJ
1715 bitmap). If MARK_STORED_ARGS_MAP, add stack slots for ARG to
1716 stored_args_map bitmap afterwards (when ARG is a register MARK_STORED_ARGS_MAP
1717 should be 0). Return nonzero if sequence after INSN dereferences such argument
1718 slots, zero otherwise. */
c67846f2
JJ
1719
1720static int
d329e058 1721check_sibcall_argument_overlap (rtx insn, struct arg_data *arg, int mark_stored_args_map)
f725a3ec 1722{
c67846f2
JJ
1723 int low, high;
1724
1725 if (insn == NULL_RTX)
1726 insn = get_insns ();
1727 else
1728 insn = NEXT_INSN (insn);
1729
1730 for (; insn; insn = NEXT_INSN (insn))
f725a3ec
KH
1731 if (INSN_P (insn)
1732 && check_sibcall_argument_overlap_1 (PATTERN (insn)))
c67846f2
JJ
1733 break;
1734
0cdca92b
DJ
1735 if (mark_stored_args_map)
1736 {
d60eab50 1737#ifdef ARGS_GROW_DOWNWARD
e7949876 1738 low = -arg->locate.slot_offset.constant - arg->locate.size.constant;
d60eab50 1739#else
e7949876 1740 low = arg->locate.slot_offset.constant;
d60eab50
AO
1741#endif
1742
e7949876 1743 for (high = low + arg->locate.size.constant; low < high; low++)
0cdca92b
DJ
1744 SET_BIT (stored_args_map, low);
1745 }
c67846f2
JJ
1746 return insn != NULL_RTX;
1747}
1748
292e35a3 1749static tree
d329e058 1750fix_unsafe_tree (tree t)
292e35a3
CE
1751{
1752 switch (unsafe_for_reeval (t))
1753 {
1754 case 0: /* Safe. */
1755 break;
1756
1757 case 1: /* Mildly unsafe. */
1758 t = unsave_expr (t);
1759 break;
1760
1761 case 2: /* Wildly unsafe. */
1762 {
1763 tree var = build_decl (VAR_DECL, NULL_TREE,
1764 TREE_TYPE (t));
1765 SET_DECL_RTL (var,
1766 expand_expr (t, NULL_RTX, VOIDmode, EXPAND_NORMAL));
1767 t = var;
1768 }
1769 break;
1770
1771 default:
1772 abort ();
1773 }
1774 return t;
1775}
1776
c988af2b
RS
1777
1778/* If function value *VALUE was returned at the most significant end of a
1779 register, shift it towards the least significant end and convert it to
1780 TYPE's mode. Return true and update *VALUE if some action was needed.
1781
1782 TYPE is the type of the function's return value, which is known not
1783 to have mode BLKmode. */
1784
1785static bool
1786shift_returned_value (tree type, rtx *value)
1787{
1788 if (targetm.calls.return_in_msb (type))
1789 {
1790 HOST_WIDE_INT shift;
1791
1792 shift = (GET_MODE_BITSIZE (GET_MODE (*value))
1793 - BITS_PER_UNIT * int_size_in_bytes (type));
1794 if (shift > 0)
1795 {
5a1f3953 1796 /* Shift the value into the low part of the register. */
c988af2b
RS
1797 *value = expand_binop (GET_MODE (*value), lshr_optab, *value,
1798 GEN_INT (shift), 0, 1, OPTAB_WIDEN);
5a1f3953
RS
1799
1800 /* Truncate it to the type's mode, or its integer equivalent.
1801 This is subject to TRULY_NOOP_TRUNCATION. */
1802 *value = convert_to_mode (int_mode_for_mode (TYPE_MODE (type)),
1803 *value, 0);
1804
1805 /* Now convert it to the final form. */
1806 *value = gen_lowpart (TYPE_MODE (type), *value);
c988af2b
RS
1807 return true;
1808 }
1809 }
1810 return false;
1811}
1812
6de9cd9a
DN
1813/* Remove all REG_EQUIV notes found in the insn chain. */
1814
1815static void
1816purge_reg_equiv_notes (void)
1817{
1818 rtx insn;
1819
1820 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1821 {
1822 while (1)
1823 {
1824 rtx note = find_reg_note (insn, REG_EQUIV, 0);
1825 if (note)
1826 {
1827 /* Remove the note and keep looking at the notes for
1828 this insn. */
1829 remove_note (insn, note);
1830 continue;
1831 }
1832 break;
1833 }
1834 }
1835}
1836
1837/* Clear RTX_UNCHANGING_P flag of incoming argument MEMs. */
1838
1839static void
1840purge_mem_unchanging_flag (rtx x)
1841{
1842 RTX_CODE code;
1843 int i, j;
1844 const char *fmt;
1845
1846 if (x == NULL_RTX)
1847 return;
1848
1849 code = GET_CODE (x);
1850
1851 if (code == MEM)
1852 {
1853 if (RTX_UNCHANGING_P (x)
1854 && (XEXP (x, 0) == current_function_internal_arg_pointer
1855 || (GET_CODE (XEXP (x, 0)) == PLUS
1856 && XEXP (XEXP (x, 0), 0) ==
1857 current_function_internal_arg_pointer
1858 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
1859 RTX_UNCHANGING_P (x) = 0;
1860 return;
1861 }
1862
1863 /* Scan all subexpressions. */
1864 fmt = GET_RTX_FORMAT (code);
1865 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
1866 {
1867 if (*fmt == 'e')
1868 purge_mem_unchanging_flag (XEXP (x, i));
1869 else if (*fmt == 'E')
1870 for (j = 0; j < XVECLEN (x, i); j++)
1871 purge_mem_unchanging_flag (XVECEXP (x, i, j));
1872 }
1873}
1874
1875
51bbfa0c
RS
1876/* Generate all the code for a function call
1877 and return an rtx for its value.
1878 Store the value in TARGET (specified as an rtx) if convenient.
1879 If the value is stored in TARGET then TARGET is returned.
1880 If IGNORE is nonzero, then we ignore the value of the function call. */
1881
1882rtx
d329e058 1883expand_call (tree exp, rtx target, int ignore)
51bbfa0c 1884{
0a1c58a2
JL
1885 /* Nonzero if we are currently expanding a call. */
1886 static int currently_expanding_call = 0;
1887
51bbfa0c
RS
1888 /* List of actual parameters. */
1889 tree actparms = TREE_OPERAND (exp, 1);
1890 /* RTX for the function to be called. */
1891 rtx funexp;
0a1c58a2
JL
1892 /* Sequence of insns to perform a normal "call". */
1893 rtx normal_call_insns = NULL_RTX;
6de9cd9a 1894 /* Sequence of insns to perform a tail "call". */
0a1c58a2 1895 rtx tail_call_insns = NULL_RTX;
51bbfa0c
RS
1896 /* Data type of the function. */
1897 tree funtype;
ded9bf77 1898 tree type_arg_types;
51bbfa0c
RS
1899 /* Declaration of the function being called,
1900 or 0 if the function is computed (not known by name). */
1901 tree fndecl = 0;
57782ad8
MM
1902 /* The type of the function being called. */
1903 tree fntype;
6de9cd9a 1904 bool try_tail_call = CALL_EXPR_TAILCALL (exp);
0a1c58a2 1905 int pass;
51bbfa0c
RS
1906
1907 /* Register in which non-BLKmode value will be returned,
1908 or 0 if no value or if value is BLKmode. */
1909 rtx valreg;
1910 /* Address where we should return a BLKmode value;
1911 0 if value not BLKmode. */
1912 rtx structure_value_addr = 0;
1913 /* Nonzero if that address is being passed by treating it as
1914 an extra, implicit first parameter. Otherwise,
1915 it is passed by being copied directly into struct_value_rtx. */
1916 int structure_value_addr_parm = 0;
1917 /* Size of aggregate value wanted, or zero if none wanted
1918 or if we are using the non-reentrant PCC calling convention
1919 or expecting the value in registers. */
e5e809f4 1920 HOST_WIDE_INT struct_value_size = 0;
51bbfa0c
RS
1921 /* Nonzero if called function returns an aggregate in memory PCC style,
1922 by returning the address of where to find it. */
1923 int pcc_struct_value = 0;
61f71b34 1924 rtx struct_value = 0;
51bbfa0c
RS
1925
1926 /* Number of actual parameters in this call, including struct value addr. */
1927 int num_actuals;
1928 /* Number of named args. Args after this are anonymous ones
1929 and they must all go on the stack. */
1930 int n_named_args;
51bbfa0c
RS
1931
1932 /* Vector of information about each argument.
1933 Arguments are numbered in the order they will be pushed,
1934 not the order they are written. */
1935 struct arg_data *args;
1936
1937 /* Total size in bytes of all the stack-parms scanned so far. */
1938 struct args_size args_size;
099e9712 1939 struct args_size adjusted_args_size;
51bbfa0c 1940 /* Size of arguments before any adjustments (such as rounding). */
599f37b6 1941 int unadjusted_args_size;
51bbfa0c
RS
1942 /* Data on reg parms scanned so far. */
1943 CUMULATIVE_ARGS args_so_far;
1944 /* Nonzero if a reg parm has been scanned. */
1945 int reg_parm_seen;
efd65a8b 1946 /* Nonzero if this is an indirect function call. */
51bbfa0c 1947
f725a3ec 1948 /* Nonzero if we must avoid push-insns in the args for this call.
51bbfa0c
RS
1949 If stack space is allocated for register parameters, but not by the
1950 caller, then it is preallocated in the fixed part of the stack frame.
1951 So the entire argument block must then be preallocated (i.e., we
1952 ignore PUSH_ROUNDING in that case). */
1953
f73ad30e 1954 int must_preallocate = !PUSH_ARGS;
51bbfa0c 1955
f72aed24 1956 /* Size of the stack reserved for parameter registers. */
6f90e075
JW
1957 int reg_parm_stack_space = 0;
1958
51bbfa0c
RS
1959 /* Address of space preallocated for stack parms
1960 (on machines that lack push insns), or 0 if space not preallocated. */
1961 rtx argblock = 0;
1962
f2d33f13
JH
1963 /* Mask of ECF_ flags. */
1964 int flags = 0;
f73ad30e 1965#ifdef REG_PARM_STACK_SPACE
51bbfa0c 1966 /* Define the boundary of the register parm stack space that needs to be
b820d2b8
AM
1967 saved, if any. */
1968 int low_to_save, high_to_save;
51bbfa0c
RS
1969 rtx save_area = 0; /* Place that it is saved */
1970#endif
1971
51bbfa0c
RS
1972 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
1973 char *initial_stack_usage_map = stack_usage_map;
51bbfa0c 1974
38afb23f
OH
1975 int old_stack_allocated;
1976
1977 /* State variables to track stack modifications. */
51bbfa0c 1978 rtx old_stack_level = 0;
38afb23f 1979 int old_stack_arg_under_construction = 0;
79be3418 1980 int old_pending_adj = 0;
51bbfa0c 1981 int old_inhibit_defer_pop = inhibit_defer_pop;
38afb23f
OH
1982
1983 /* Some stack pointer alterations we make are performed via
1984 allocate_dynamic_stack_space. This modifies the stack_pointer_delta,
1985 which we then also need to save/restore along the way. */
a259f218 1986 int old_stack_pointer_delta = 0;
38afb23f 1987
0a1c58a2 1988 rtx call_fusage;
b3694847 1989 tree p = TREE_OPERAND (exp, 0);
09e2bf48 1990 tree addr = TREE_OPERAND (exp, 0);
b3694847 1991 int i;
739fb049
MM
1992 /* The alignment of the stack, in bits. */
1993 HOST_WIDE_INT preferred_stack_boundary;
1994 /* The alignment of the stack, in bytes. */
1995 HOST_WIDE_INT preferred_unit_stack_boundary;
6de9cd9a
DN
1996 /* The static chain value to use for this call. */
1997 rtx static_chain_value;
f2d33f13
JH
1998 /* See if this is "nothrow" function call. */
1999 if (TREE_NOTHROW (exp))
2000 flags |= ECF_NOTHROW;
2001
6de9cd9a
DN
2002 /* See if we can find a DECL-node for the actual function, and get the
2003 function attributes (flags) from the function decl or type node. */
39b0dce7
JM
2004 fndecl = get_callee_fndecl (exp);
2005 if (fndecl)
51bbfa0c 2006 {
57782ad8 2007 fntype = TREE_TYPE (fndecl);
39b0dce7 2008 flags |= flags_from_decl_or_type (fndecl);
51bbfa0c 2009 }
39b0dce7 2010 else
72954a4f 2011 {
57782ad8 2012 fntype = TREE_TYPE (TREE_TYPE (p));
57782ad8 2013 flags |= flags_from_decl_or_type (fntype);
72954a4f 2014 }
7393c642 2015
57782ad8 2016 struct_value = targetm.calls.struct_value_rtx (fntype, 0);
61f71b34 2017
8c6a8269
RS
2018 /* Warn if this value is an aggregate type,
2019 regardless of which calling convention we are using for it. */
2020 if (warn_aggregate_return && AGGREGATE_TYPE_P (TREE_TYPE (exp)))
2021 warning ("function call has aggregate value");
2022
2023 /* If the result of a pure or const function call is ignored (or void),
2024 and none of its arguments are volatile, we can avoid expanding the
2025 call and just evaluate the arguments for side-effects. */
2026 if ((flags & (ECF_CONST | ECF_PURE))
2027 && (ignore || target == const0_rtx
2028 || TYPE_MODE (TREE_TYPE (exp)) == VOIDmode))
2029 {
2030 bool volatilep = false;
2031 tree arg;
2032
2033 for (arg = actparms; arg; arg = TREE_CHAIN (arg))
2034 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
2035 {
2036 volatilep = true;
2037 break;
2038 }
2039
2040 if (! volatilep)
2041 {
2042 for (arg = actparms; arg; arg = TREE_CHAIN (arg))
2043 expand_expr (TREE_VALUE (arg), const0_rtx,
2044 VOIDmode, EXPAND_NORMAL);
2045 return const0_rtx;
2046 }
2047 }
2048
6f90e075 2049#ifdef REG_PARM_STACK_SPACE
6f90e075
JW
2050 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
2051#endif
6f90e075 2052
f73ad30e
JH
2053#ifndef OUTGOING_REG_PARM_STACK_SPACE
2054 if (reg_parm_stack_space > 0 && PUSH_ARGS)
e5e809f4
JL
2055 must_preallocate = 1;
2056#endif
2057
51bbfa0c
RS
2058 /* Set up a place to return a structure. */
2059
2060 /* Cater to broken compilers. */
61f71b34 2061 if (aggregate_value_p (exp, fndecl))
51bbfa0c
RS
2062 {
2063 /* This call returns a big structure. */
53d4257f 2064 flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
51bbfa0c
RS
2065
2066#ifdef PCC_STATIC_STRUCT_RETURN
9e7b1d0a
RS
2067 {
2068 pcc_struct_value = 1;
9e7b1d0a
RS
2069 }
2070#else /* not PCC_STATIC_STRUCT_RETURN */
2071 {
2072 struct_value_size = int_size_in_bytes (TREE_TYPE (exp));
51bbfa0c 2073
4977bab6 2074 if (CALL_EXPR_HAS_RETURN_SLOT_ADDR (exp))
89ea02fb 2075 {
f32ac70d
JM
2076 /* The structure value address arg is already in actparms.
2077 Pull it out. It might be nice to just leave it there, but
2078 we need to set structure_value_addr. */
2079 tree return_arg = TREE_VALUE (actparms);
2080 actparms = TREE_CHAIN (actparms);
2081 structure_value_addr = expand_expr (return_arg, NULL_RTX,
2082 VOIDmode, EXPAND_NORMAL);
89ea02fb 2083 }
3c0cb5de 2084 else if (target && MEM_P (target))
9e7b1d0a
RS
2085 structure_value_addr = XEXP (target, 0);
2086 else
2087 {
9e7b1d0a
RS
2088 /* For variable-sized objects, we must be called with a target
2089 specified. If we were to allocate space on the stack here,
2090 we would have no way of knowing when to free it. */
1da68f56 2091 rtx d = assign_temp (TREE_TYPE (exp), 1, 1, 1);
51bbfa0c 2092
4361b41d
MM
2093 mark_temp_addr_taken (d);
2094 structure_value_addr = XEXP (d, 0);
9e7b1d0a
RS
2095 target = 0;
2096 }
2097 }
2098#endif /* not PCC_STATIC_STRUCT_RETURN */
51bbfa0c
RS
2099 }
2100
099e9712 2101 /* Figure out the amount to which the stack should be aligned. */
099e9712 2102 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
b255a036
JH
2103 if (fndecl)
2104 {
2105 struct cgraph_rtl_info *i = cgraph_rtl_info (fndecl);
2106 if (i && i->preferred_incoming_stack_boundary)
2107 preferred_stack_boundary = i->preferred_incoming_stack_boundary;
2108 }
099e9712
JH
2109
2110 /* Operand 0 is a pointer-to-function; get the type of the function. */
09e2bf48 2111 funtype = TREE_TYPE (addr);
099e9712
JH
2112 if (! POINTER_TYPE_P (funtype))
2113 abort ();
2114 funtype = TREE_TYPE (funtype);
2115
ded9bf77
AH
2116 /* Munge the tree to split complex arguments into their imaginary
2117 and real parts. */
42ba5130 2118 if (targetm.calls.split_complex_arg)
ded9bf77
AH
2119 {
2120 type_arg_types = split_complex_types (TYPE_ARG_TYPES (funtype));
2121 actparms = split_complex_values (actparms);
2122 }
2123 else
2124 type_arg_types = TYPE_ARG_TYPES (funtype);
2125
099e9712
JH
2126 if (flags & ECF_MAY_BE_ALLOCA)
2127 current_function_calls_alloca = 1;
2128
2129 /* If struct_value_rtx is 0, it means pass the address
2130 as if it were an extra parameter. */
61f71b34 2131 if (structure_value_addr && struct_value == 0)
099e9712
JH
2132 {
2133 /* If structure_value_addr is a REG other than
2134 virtual_outgoing_args_rtx, we can use always use it. If it
2135 is not a REG, we must always copy it into a register.
2136 If it is virtual_outgoing_args_rtx, we must copy it to another
2137 register in some cases. */
f8cfc6aa 2138 rtx temp = (!REG_P (structure_value_addr)
099e9712
JH
2139 || (ACCUMULATE_OUTGOING_ARGS
2140 && stack_arg_under_construction
2141 && structure_value_addr == virtual_outgoing_args_rtx)
57782ad8
MM
2142 ? copy_addr_to_reg (convert_memory_address
2143 (Pmode, structure_value_addr))
099e9712
JH
2144 : structure_value_addr);
2145
2146 actparms
2147 = tree_cons (error_mark_node,
2148 make_tree (build_pointer_type (TREE_TYPE (funtype)),
2149 temp),
2150 actparms);
2151 structure_value_addr_parm = 1;
2152 }
2153
2154 /* Count the arguments and set NUM_ACTUALS. */
2155 for (p = actparms, num_actuals = 0; p; p = TREE_CHAIN (p))
2156 num_actuals++;
2157
2158 /* Compute number of named args.
3a4d587b
AM
2159 First, do a raw count of the args for INIT_CUMULATIVE_ARGS. */
2160
2161 if (type_arg_types != 0)
2162 n_named_args
2163 = (list_length (type_arg_types)
2164 /* Count the struct value address, if it is passed as a parm. */
2165 + structure_value_addr_parm);
2166 else
2167 /* If we know nothing, treat all args as named. */
2168 n_named_args = num_actuals;
2169
2170 /* Start updating where the next arg would go.
2171
2172 On some machines (such as the PA) indirect calls have a different
2173 calling convention than normal calls. The fourth argument in
2174 INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
2175 or not. */
2176 INIT_CUMULATIVE_ARGS (args_so_far, funtype, NULL_RTX, fndecl, n_named_args);
2177
2178 /* Now possibly adjust the number of named args.
099e9712 2179 Normally, don't include the last named arg if anonymous args follow.
3a179764
KH
2180 We do include the last named arg if
2181 targetm.calls.strict_argument_naming() returns nonzero.
099e9712
JH
2182 (If no anonymous args follow, the result of list_length is actually
2183 one too large. This is harmless.)
2184
4ac8340c 2185 If targetm.calls.pretend_outgoing_varargs_named() returns
3a179764
KH
2186 nonzero, and targetm.calls.strict_argument_naming() returns zero,
2187 this machine will be able to place unnamed args that were passed
2188 in registers into the stack. So treat all args as named. This
2189 allows the insns emitting for a specific argument list to be
2190 independent of the function declaration.
4ac8340c
KH
2191
2192 If targetm.calls.pretend_outgoing_varargs_named() returns zero,
2193 we do not have any reliable way to pass unnamed args in
2194 registers, so we must force them into memory. */
099e9712 2195
3a4d587b
AM
2196 if (type_arg_types != 0
2197 && targetm.calls.strict_argument_naming (&args_so_far))
2198 ;
2199 else if (type_arg_types != 0
2200 && ! targetm.calls.pretend_outgoing_varargs_named (&args_so_far))
2201 /* Don't include the last named arg. */
2202 --n_named_args;
099e9712 2203 else
3a4d587b 2204 /* Treat all args as named. */
099e9712
JH
2205 n_named_args = num_actuals;
2206
099e9712 2207 /* Make a vector to hold all the information about each arg. */
703ad42b
KG
2208 args = alloca (num_actuals * sizeof (struct arg_data));
2209 memset (args, 0, num_actuals * sizeof (struct arg_data));
099e9712 2210
d80d2d2a
KH
2211 /* Build up entries in the ARGS array, compute the size of the
2212 arguments into ARGS_SIZE, etc. */
099e9712
JH
2213 initialize_argument_information (num_actuals, args, &args_size,
2214 n_named_args, actparms, fndecl,
2215 &args_so_far, reg_parm_stack_space,
2216 &old_stack_level, &old_pending_adj,
dd292d0a 2217 &must_preallocate, &flags,
6de9cd9a 2218 &try_tail_call, CALL_FROM_THUNK_P (exp));
099e9712
JH
2219
2220 if (args_size.var)
2221 {
2222 /* If this function requires a variable-sized argument list, don't
2223 try to make a cse'able block for this call. We may be able to
2224 do this eventually, but it is too complicated to keep track of
6d2f8887 2225 what insns go in the cse'able block and which don't. */
099e9712 2226
53d4257f 2227 flags &= ~ECF_LIBCALL_BLOCK;
099e9712
JH
2228 must_preallocate = 1;
2229 }
2230
2231 /* Now make final decision about preallocating stack space. */
2232 must_preallocate = finalize_must_preallocate (must_preallocate,
2233 num_actuals, args,
2234 &args_size);
2235
2236 /* If the structure value address will reference the stack pointer, we
2237 must stabilize it. We don't need to do this if we know that we are
2238 not going to adjust the stack pointer in processing this call. */
2239
2240 if (structure_value_addr
2241 && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
2242 || reg_mentioned_p (virtual_outgoing_args_rtx,
2243 structure_value_addr))
2244 && (args_size.var
2245 || (!ACCUMULATE_OUTGOING_ARGS && args_size.constant)))
2246 structure_value_addr = copy_to_reg (structure_value_addr);
0a1c58a2 2247
194c7c45
RH
2248 /* Tail calls can make things harder to debug, and we're traditionally
2249 pushed these optimizations into -O2. Don't try if we're already
fb158467
JH
2250 expanding a call, as that means we're an argument. Don't try if
2251 there's cleanups, as we know there's code to follow the call.
e2ee9912 2252
f725a3ec 2253 If rtx_equal_function_value_matters is false, that means we've
e2ee9912
RH
2254 finished with regular parsing. Which means that some of the
2255 machinery we use to generate tail-calls is no longer in place.
2256 This is most often true of sjlj-exceptions, which we couldn't
a6a5a42d 2257 tail-call to anyway.
0a1c58a2 2258
a6a5a42d
RS
2259 If current_nesting_level () == 0, we're being called after
2260 the function body has been expanded. This can happen when
2261 setting up trampolines in expand_function_end. */
099e9712
JH
2262 if (currently_expanding_call++ != 0
2263 || !flag_optimize_sibling_calls
2264 || !rtx_equal_function_value_matters
a6a5a42d 2265 || current_nesting_level () == 0
de1f5659 2266 || any_pending_cleanups ()
6de9cd9a
DN
2267 || args_size.var
2268 || lookup_stmt_eh_region (exp) >= 0)
2269 try_tail_call = 0;
099e9712
JH
2270
2271 /* Rest of purposes for tail call optimizations to fail. */
2272 if (
2273#ifdef HAVE_sibcall_epilogue
2274 !HAVE_sibcall_epilogue
2275#else
2276 1
2277#endif
2278 || !try_tail_call
2279 /* Doing sibling call optimization needs some work, since
2280 structure_value_addr can be allocated on the stack.
2281 It does not seem worth the effort since few optimizable
2282 sibling calls will return a structure. */
2283 || structure_value_addr != NULL_RTX
4977bab6
ZW
2284 /* Check whether the target is able to optimize the call
2285 into a sibcall. */
5fd9b178 2286 || !targetm.function_ok_for_sibcall (fndecl, exp)
4977bab6
ZW
2287 /* Functions that do not return exactly once may not be sibcall
2288 optimized. */
db655634 2289 || (flags & (ECF_RETURNS_TWICE | ECF_LONGJMP | ECF_NORETURN))
09e2bf48 2290 || TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (addr)))
6a48df45
OH
2291 /* If the called function is nested in the current one, it might access
2292 some of the caller's arguments, but could clobber them beforehand if
2293 the argument areas are shared. */
2294 || (fndecl && decl_function_context (fndecl) == current_function_decl)
099e9712
JH
2295 /* If this function requires more stack slots than the current
2296 function, we cannot change it into a sibling call. */
2297 || args_size.constant > current_function_args_size
2298 /* If the callee pops its own arguments, then it must pop exactly
2299 the same number of arguments as the current function. */
e076f71a
AH
2300 || (RETURN_POPS_ARGS (fndecl, funtype, args_size.constant)
2301 != RETURN_POPS_ARGS (current_function_decl,
2302 TREE_TYPE (current_function_decl),
2303 current_function_args_size))
ae2bcd98 2304 || !lang_hooks.decls.ok_for_sibcall (fndecl))
e6f64875 2305 try_tail_call = 0;
497eb8c3 2306
6de9cd9a 2307 if (try_tail_call)
099e9712
JH
2308 {
2309 int end, inc;
2310 actparms = NULL_TREE;
194c7c45
RH
2311 /* Ok, we're going to give the tail call the old college try.
2312 This means we're going to evaluate the function arguments
2313 up to three times. There are two degrees of badness we can
2314 encounter, those that can be unsaved and those that can't.
2315 (See unsafe_for_reeval commentary for details.)
2316
2317 Generate a new argument list. Pass safe arguments through
f725a3ec 2318 unchanged. For the easy badness wrap them in UNSAVE_EXPRs.
194c7c45 2319 For hard badness, evaluate them now and put their resulting
099e9712
JH
2320 rtx in a temporary VAR_DECL.
2321
2322 initialize_argument_information has ordered the array for the
2323 order to be pushed, and we must remember this when reconstructing
f5143c46 2324 the original argument order. */
4d393a0b 2325
099e9712
JH
2326 if (PUSH_ARGS_REVERSED)
2327 {
2328 inc = 1;
2329 i = 0;
2330 end = num_actuals;
2331 }
2332 else
f725a3ec 2333 {
099e9712
JH
2334 inc = -1;
2335 i = num_actuals - 1;
2336 end = -1;
2337 }
2338
2339 for (; i != end; i += inc)
2340 {
292e35a3 2341 args[i].tree_value = fix_unsafe_tree (args[i].tree_value);
099e9712 2342 }
3dc575ff 2343 /* Do the same for the function address if it is an expression. */
292e35a3 2344 if (!fndecl)
09e2bf48 2345 addr = fix_unsafe_tree (addr);
194c7c45
RH
2346 /* Expanding one of those dangerous arguments could have added
2347 cleanups, but otherwise give it a whirl. */
de1f5659 2348 if (any_pending_cleanups ())
6de9cd9a 2349 try_tail_call = 0;
0a1c58a2
JL
2350 }
2351
0a1c58a2 2352
c2f8b491
JH
2353 /* Ensure current function's preferred stack boundary is at least
2354 what we need. We don't have to increase alignment for recursive
2355 functions. */
2356 if (cfun->preferred_stack_boundary < preferred_stack_boundary
2357 && fndecl != current_function_decl)
2358 cfun->preferred_stack_boundary = preferred_stack_boundary;
b255a036
JH
2359 if (fndecl == current_function_decl)
2360 cfun->recursive_call_emit = true;
c2f8b491 2361
099e9712 2362 preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
497eb8c3 2363
0a1c58a2
JL
2364 /* We want to make two insn chains; one for a sibling call, the other
2365 for a normal call. We will select one of the two chains after
2366 initial RTL generation is complete. */
b820d2b8 2367 for (pass = try_tail_call ? 0 : 1; pass < 2; pass++)
0a1c58a2
JL
2368 {
2369 int sibcall_failure = 0;
f5143c46 2370 /* We want to emit any pending stack adjustments before the tail
0a1c58a2 2371 recursion "call". That way we know any adjustment after the tail
6de9cd9a 2372 recursion call can be ignored if we indeed use the tail
0a1c58a2 2373 call expansion. */
5ac9118e
KG
2374 int save_pending_stack_adjust = 0;
2375 int save_stack_pointer_delta = 0;
0a1c58a2 2376 rtx insns;
7d167afd 2377 rtx before_call, next_arg_reg;
39842893 2378
0a1c58a2
JL
2379 if (pass == 0)
2380 {
1c81f9fe
JM
2381 /* Emit any queued insns now; otherwise they would end up in
2382 only one of the alternates. */
2383 emit_queue ();
2384
0a1c58a2
JL
2385 /* State variables we need to save and restore between
2386 iterations. */
2387 save_pending_stack_adjust = pending_stack_adjust;
1503a7ec 2388 save_stack_pointer_delta = stack_pointer_delta;
0a1c58a2 2389 }
f2d33f13
JH
2390 if (pass)
2391 flags &= ~ECF_SIBCALL;
2392 else
2393 flags |= ECF_SIBCALL;
51bbfa0c 2394
0a1c58a2 2395 /* Other state variables that we must reinitialize each time
f2d33f13 2396 through the loop (that are not initialized by the loop itself). */
0a1c58a2
JL
2397 argblock = 0;
2398 call_fusage = 0;
fa76d9e0 2399
f725a3ec 2400 /* Start a new sequence for the normal call case.
51bbfa0c 2401
0a1c58a2
JL
2402 From this point on, if the sibling call fails, we want to set
2403 sibcall_failure instead of continuing the loop. */
2404 start_sequence ();
eecb6f50 2405
b8d254e6
JJ
2406 if (pass == 0)
2407 {
2408 /* We know at this point that there are not currently any
2409 pending cleanups. If, however, in the process of evaluating
2410 the arguments we were to create some, we'll need to be
2411 able to get rid of them. */
2412 expand_start_target_temps ();
2413 }
2414
0a1c58a2
JL
2415 /* Don't let pending stack adjusts add up to too much.
2416 Also, do all pending adjustments now if there is any chance
2417 this might be a call to alloca or if we are expanding a sibling
b5cd4ed4
RK
2418 call sequence or if we are calling a function that is to return
2419 with stack pointer depressed. */
0a1c58a2 2420 if (pending_stack_adjust >= 32
b5cd4ed4
RK
2421 || (pending_stack_adjust > 0
2422 && (flags & (ECF_MAY_BE_ALLOCA | ECF_SP_DEPRESSED)))
0a1c58a2
JL
2423 || pass == 0)
2424 do_pending_stack_adjust ();
51bbfa0c 2425
54fef245
RH
2426 /* When calling a const function, we must pop the stack args right away,
2427 so that the pop is deleted or moved with the call. */
53d4257f 2428 if (pass && (flags & ECF_LIBCALL_BLOCK))
54fef245
RH
2429 NO_DEFER_POP;
2430
0a1c58a2 2431 /* Precompute any arguments as needed. */
f8a097cd
JH
2432 if (pass)
2433 precompute_arguments (flags, num_actuals, args);
51bbfa0c 2434
0a1c58a2
JL
2435 /* Now we are about to start emitting insns that can be deleted
2436 if a libcall is deleted. */
53d4257f 2437 if (pass && (flags & (ECF_LIBCALL_BLOCK | ECF_MALLOC)))
0a1c58a2 2438 start_sequence ();
51bbfa0c 2439
099e9712 2440 adjusted_args_size = args_size;
ce48579b
RH
2441 /* Compute the actual size of the argument block required. The variable
2442 and constant sizes must be combined, the size may have to be rounded,
2443 and there may be a minimum required size. When generating a sibcall
2444 pattern, do not round up, since we'll be re-using whatever space our
2445 caller provided. */
2446 unadjusted_args_size
f725a3ec
KH
2447 = compute_argument_block_size (reg_parm_stack_space,
2448 &adjusted_args_size,
ce48579b
RH
2449 (pass == 0 ? 0
2450 : preferred_stack_boundary));
2451
f725a3ec 2452 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
ce48579b 2453
f8a097cd
JH
2454 /* The argument block when performing a sibling call is the
2455 incoming argument block. */
2456 if (pass == 0)
c67846f2
JJ
2457 {
2458 argblock = virtual_incoming_args_rtx;
fcae219a
R
2459 argblock
2460#ifdef STACK_GROWS_DOWNWARD
2461 = plus_constant (argblock, current_function_pretend_args_size);
2462#else
2463 = plus_constant (argblock, -current_function_pretend_args_size);
2464#endif
c67846f2
JJ
2465 stored_args_map = sbitmap_alloc (args_size.constant);
2466 sbitmap_zero (stored_args_map);
2467 }
ce48579b 2468
0a1c58a2
JL
2469 /* If we have no actual push instructions, or shouldn't use them,
2470 make space for all args right now. */
099e9712 2471 else if (adjusted_args_size.var != 0)
51bbfa0c 2472 {
0a1c58a2
JL
2473 if (old_stack_level == 0)
2474 {
2475 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
38afb23f 2476 old_stack_pointer_delta = stack_pointer_delta;
0a1c58a2
JL
2477 old_pending_adj = pending_stack_adjust;
2478 pending_stack_adjust = 0;
0a1c58a2
JL
2479 /* stack_arg_under_construction says whether a stack arg is
2480 being constructed at the old stack level. Pushing the stack
2481 gets a clean outgoing argument block. */
2482 old_stack_arg_under_construction = stack_arg_under_construction;
2483 stack_arg_under_construction = 0;
0a1c58a2 2484 }
099e9712 2485 argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
51bbfa0c 2486 }
0a1c58a2
JL
2487 else
2488 {
2489 /* Note that we must go through the motions of allocating an argument
2490 block even if the size is zero because we may be storing args
2491 in the area reserved for register arguments, which may be part of
2492 the stack frame. */
26a258fe 2493
099e9712 2494 int needed = adjusted_args_size.constant;
51bbfa0c 2495
0a1c58a2
JL
2496 /* Store the maximum argument space used. It will be pushed by
2497 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
2498 checking). */
51bbfa0c 2499
0a1c58a2
JL
2500 if (needed > current_function_outgoing_args_size)
2501 current_function_outgoing_args_size = needed;
51bbfa0c 2502
0a1c58a2
JL
2503 if (must_preallocate)
2504 {
f73ad30e
JH
2505 if (ACCUMULATE_OUTGOING_ARGS)
2506 {
f8a097cd
JH
2507 /* Since the stack pointer will never be pushed, it is
2508 possible for the evaluation of a parm to clobber
2509 something we have already written to the stack.
2510 Since most function calls on RISC machines do not use
2511 the stack, this is uncommon, but must work correctly.
26a258fe 2512
f73ad30e 2513 Therefore, we save any area of the stack that was already
f8a097cd
JH
2514 written and that we are using. Here we set up to do this
2515 by making a new stack usage map from the old one. The
f725a3ec 2516 actual save will be done by store_one_arg.
26a258fe 2517
f73ad30e
JH
2518 Another approach might be to try to reorder the argument
2519 evaluations to avoid this conflicting stack usage. */
26a258fe 2520
e5e809f4 2521#ifndef OUTGOING_REG_PARM_STACK_SPACE
f8a097cd
JH
2522 /* Since we will be writing into the entire argument area,
2523 the map must be allocated for its entire size, not just
2524 the part that is the responsibility of the caller. */
f73ad30e 2525 needed += reg_parm_stack_space;
51bbfa0c
RS
2526#endif
2527
2528#ifdef ARGS_GROW_DOWNWARD
f73ad30e
JH
2529 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2530 needed + 1);
51bbfa0c 2531#else
f73ad30e
JH
2532 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2533 needed);
51bbfa0c 2534#endif
703ad42b 2535 stack_usage_map = alloca (highest_outgoing_arg_in_use);
51bbfa0c 2536
f73ad30e 2537 if (initial_highest_arg_in_use)
2e09e75a
JM
2538 memcpy (stack_usage_map, initial_stack_usage_map,
2539 initial_highest_arg_in_use);
2f4aa534 2540
f73ad30e 2541 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
961192e1 2542 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
f73ad30e
JH
2543 (highest_outgoing_arg_in_use
2544 - initial_highest_arg_in_use));
2545 needed = 0;
2f4aa534 2546
f8a097cd
JH
2547 /* The address of the outgoing argument list must not be
2548 copied to a register here, because argblock would be left
2549 pointing to the wrong place after the call to
f725a3ec 2550 allocate_dynamic_stack_space below. */
2f4aa534 2551
f73ad30e 2552 argblock = virtual_outgoing_args_rtx;
f725a3ec 2553 }
f73ad30e 2554 else
26a258fe 2555 {
f73ad30e 2556 if (inhibit_defer_pop == 0)
0a1c58a2 2557 {
f73ad30e 2558 /* Try to reuse some or all of the pending_stack_adjust
ce48579b
RH
2559 to get this space. */
2560 needed
f725a3ec 2561 = (combine_pending_stack_adjustment_and_call
ce48579b 2562 (unadjusted_args_size,
099e9712 2563 &adjusted_args_size,
ce48579b
RH
2564 preferred_unit_stack_boundary));
2565
2566 /* combine_pending_stack_adjustment_and_call computes
2567 an adjustment before the arguments are allocated.
2568 Account for them and see whether or not the stack
2569 needs to go up or down. */
2570 needed = unadjusted_args_size - needed;
2571
2572 if (needed < 0)
f73ad30e 2573 {
ce48579b
RH
2574 /* We're releasing stack space. */
2575 /* ??? We can avoid any adjustment at all if we're
2576 already aligned. FIXME. */
2577 pending_stack_adjust = -needed;
2578 do_pending_stack_adjust ();
f73ad30e
JH
2579 needed = 0;
2580 }
f725a3ec 2581 else
ce48579b
RH
2582 /* We need to allocate space. We'll do that in
2583 push_block below. */
2584 pending_stack_adjust = 0;
0a1c58a2 2585 }
ce48579b
RH
2586
2587 /* Special case this because overhead of `push_block' in
2588 this case is non-trivial. */
f73ad30e
JH
2589 if (needed == 0)
2590 argblock = virtual_outgoing_args_rtx;
0a1c58a2 2591 else
d892f288
DD
2592 {
2593 argblock = push_block (GEN_INT (needed), 0, 0);
2594#ifdef ARGS_GROW_DOWNWARD
2595 argblock = plus_constant (argblock, needed);
2596#endif
2597 }
f73ad30e 2598
f8a097cd
JH
2599 /* We only really need to call `copy_to_reg' in the case
2600 where push insns are going to be used to pass ARGBLOCK
2601 to a function call in ARGS. In that case, the stack
2602 pointer changes value from the allocation point to the
2603 call point, and hence the value of
2604 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
2605 as well always do it. */
f73ad30e 2606 argblock = copy_to_reg (argblock);
38afb23f
OH
2607 }
2608 }
2609 }
0a1c58a2 2610
38afb23f
OH
2611 if (ACCUMULATE_OUTGOING_ARGS)
2612 {
2613 /* The save/restore code in store_one_arg handles all
2614 cases except one: a constructor call (including a C
2615 function returning a BLKmode struct) to initialize
2616 an argument. */
2617 if (stack_arg_under_construction)
2618 {
e5e809f4 2619#ifndef OUTGOING_REG_PARM_STACK_SPACE
38afb23f
OH
2620 rtx push_size = GEN_INT (reg_parm_stack_space
2621 + adjusted_args_size.constant);
bfbf933a 2622#else
38afb23f 2623 rtx push_size = GEN_INT (adjusted_args_size.constant);
bfbf933a 2624#endif
38afb23f
OH
2625 if (old_stack_level == 0)
2626 {
2627 emit_stack_save (SAVE_BLOCK, &old_stack_level,
2628 NULL_RTX);
2629 old_stack_pointer_delta = stack_pointer_delta;
2630 old_pending_adj = pending_stack_adjust;
2631 pending_stack_adjust = 0;
2632 /* stack_arg_under_construction says whether a stack
2633 arg is being constructed at the old stack level.
2634 Pushing the stack gets a clean outgoing argument
2635 block. */
2636 old_stack_arg_under_construction
2637 = stack_arg_under_construction;
2638 stack_arg_under_construction = 0;
2639 /* Make a new map for the new argument list. */
703ad42b 2640 stack_usage_map = alloca (highest_outgoing_arg_in_use);
38afb23f
OH
2641 memset (stack_usage_map, 0, highest_outgoing_arg_in_use);
2642 highest_outgoing_arg_in_use = 0;
f73ad30e 2643 }
38afb23f
OH
2644 allocate_dynamic_stack_space (push_size, NULL_RTX,
2645 BITS_PER_UNIT);
0a1c58a2 2646 }
bfbf933a 2647
38afb23f
OH
2648 /* If argument evaluation might modify the stack pointer,
2649 copy the address of the argument list to a register. */
2650 for (i = 0; i < num_actuals; i++)
2651 if (args[i].pass_on_stack)
2652 {
2653 argblock = copy_addr_to_reg (argblock);
2654 break;
2655 }
2656 }
d329e058 2657
0a1c58a2 2658 compute_argument_addresses (args, argblock, num_actuals);
bfbf933a 2659
0a1c58a2
JL
2660 /* If we push args individually in reverse order, perform stack alignment
2661 before the first push (the last arg). */
f73ad30e 2662 if (PUSH_ARGS_REVERSED && argblock == 0
099e9712 2663 && adjusted_args_size.constant != unadjusted_args_size)
4e217aed 2664 {
0a1c58a2
JL
2665 /* When the stack adjustment is pending, we get better code
2666 by combining the adjustments. */
f725a3ec 2667 if (pending_stack_adjust
53d4257f 2668 && ! (flags & ECF_LIBCALL_BLOCK)
0a1c58a2 2669 && ! inhibit_defer_pop)
ce48579b
RH
2670 {
2671 pending_stack_adjust
f725a3ec 2672 = (combine_pending_stack_adjustment_and_call
ce48579b 2673 (unadjusted_args_size,
099e9712 2674 &adjusted_args_size,
ce48579b
RH
2675 preferred_unit_stack_boundary));
2676 do_pending_stack_adjust ();
2677 }
0a1c58a2 2678 else if (argblock == 0)
099e9712 2679 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
0a1c58a2 2680 - unadjusted_args_size));
0a1c58a2 2681 }
ebcd0b57
JH
2682 /* Now that the stack is properly aligned, pops can't safely
2683 be deferred during the evaluation of the arguments. */
2684 NO_DEFER_POP;
51bbfa0c 2685
09e2bf48 2686 funexp = rtx_for_function_call (fndecl, addr);
51bbfa0c 2687
0a1c58a2
JL
2688 /* Figure out the register where the value, if any, will come back. */
2689 valreg = 0;
2690 if (TYPE_MODE (TREE_TYPE (exp)) != VOIDmode
2691 && ! structure_value_addr)
2692 {
2693 if (pcc_struct_value)
2694 valreg = hard_function_value (build_pointer_type (TREE_TYPE (exp)),
7d167afd 2695 fndecl, (pass == 0));
0a1c58a2 2696 else
7d167afd 2697 valreg = hard_function_value (TREE_TYPE (exp), fndecl, (pass == 0));
0a1c58a2 2698 }
51bbfa0c 2699
0a1c58a2
JL
2700 /* Precompute all register parameters. It isn't safe to compute anything
2701 once we have started filling any specific hard regs. */
2702 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
51bbfa0c 2703
6de9cd9a
DN
2704 if (TREE_OPERAND (exp, 2))
2705 static_chain_value = expand_expr (TREE_OPERAND (exp, 2),
2706 NULL_RTX, VOIDmode, 0);
2707 else
2708 static_chain_value = 0;
2709
f73ad30e 2710#ifdef REG_PARM_STACK_SPACE
0a1c58a2
JL
2711 /* Save the fixed argument area if it's part of the caller's frame and
2712 is clobbered by argument setup for this call. */
f8a097cd 2713 if (ACCUMULATE_OUTGOING_ARGS && pass)
f73ad30e
JH
2714 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
2715 &low_to_save, &high_to_save);
b94301c2 2716#endif
51bbfa0c 2717
0a1c58a2
JL
2718 /* Now store (and compute if necessary) all non-register parms.
2719 These come before register parms, since they can require block-moves,
2720 which could clobber the registers used for register parms.
2721 Parms which have partial registers are not stored here,
2722 but we do preallocate space here if they want that. */
51bbfa0c 2723
0a1c58a2
JL
2724 for (i = 0; i < num_actuals; i++)
2725 if (args[i].reg == 0 || args[i].pass_on_stack)
c67846f2
JJ
2726 {
2727 rtx before_arg = get_last_insn ();
2728
4c6b3b2a
JJ
2729 if (store_one_arg (&args[i], argblock, flags,
2730 adjusted_args_size.var != 0,
2731 reg_parm_stack_space)
2732 || (pass == 0
2733 && check_sibcall_argument_overlap (before_arg,
0cdca92b 2734 &args[i], 1)))
c67846f2 2735 sibcall_failure = 1;
2fabc3d6
JDA
2736
2737 if (flags & ECF_CONST
2738 && args[i].stack
2739 && args[i].value == args[i].stack)
2740 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
2741 gen_rtx_USE (VOIDmode,
2742 args[i].value),
2743 call_fusage);
c67846f2 2744 }
0a1c58a2
JL
2745
2746 /* If we have a parm that is passed in registers but not in memory
2747 and whose alignment does not permit a direct copy into registers,
2748 make a group of pseudos that correspond to each register that we
2749 will later fill. */
2750 if (STRICT_ALIGNMENT)
2751 store_unaligned_arguments_into_pseudos (args, num_actuals);
2752
2753 /* Now store any partially-in-registers parm.
2754 This is the last place a block-move can happen. */
2755 if (reg_parm_seen)
2756 for (i = 0; i < num_actuals; i++)
2757 if (args[i].partial != 0 && ! args[i].pass_on_stack)
c67846f2
JJ
2758 {
2759 rtx before_arg = get_last_insn ();
2760
4c6b3b2a
JJ
2761 if (store_one_arg (&args[i], argblock, flags,
2762 adjusted_args_size.var != 0,
2763 reg_parm_stack_space)
2764 || (pass == 0
2765 && check_sibcall_argument_overlap (before_arg,
0cdca92b 2766 &args[i], 1)))
c67846f2
JJ
2767 sibcall_failure = 1;
2768 }
51bbfa0c 2769
0a1c58a2
JL
2770 /* If we pushed args in forward order, perform stack alignment
2771 after pushing the last arg. */
f73ad30e 2772 if (!PUSH_ARGS_REVERSED && argblock == 0)
099e9712 2773 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
0a1c58a2 2774 - unadjusted_args_size));
51bbfa0c 2775
0a1c58a2
JL
2776 /* If register arguments require space on the stack and stack space
2777 was not preallocated, allocate stack space here for arguments
2778 passed in registers. */
f73ad30e
JH
2779#ifdef OUTGOING_REG_PARM_STACK_SPACE
2780 if (!ACCUMULATE_OUTGOING_ARGS
f725a3ec 2781 && must_preallocate == 0 && reg_parm_stack_space > 0)
0a1c58a2 2782 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
756e0e12
RS
2783#endif
2784
0a1c58a2
JL
2785 /* Pass the function the address in which to return a
2786 structure value. */
2787 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
2788 {
5ae6cd0d
MM
2789 structure_value_addr
2790 = convert_memory_address (Pmode, structure_value_addr);
61f71b34 2791 emit_move_insn (struct_value,
0a1c58a2
JL
2792 force_reg (Pmode,
2793 force_operand (structure_value_addr,
2794 NULL_RTX)));
2795
f8cfc6aa 2796 if (REG_P (struct_value))
61f71b34 2797 use_reg (&call_fusage, struct_value);
0a1c58a2 2798 }
c2939b57 2799
6de9cd9a
DN
2800 funexp = prepare_call_address (funexp, static_chain_value,
2801 &call_fusage, reg_parm_seen, pass == 0);
51bbfa0c 2802
0cdca92b
DJ
2803 load_register_parameters (args, num_actuals, &call_fusage, flags,
2804 pass == 0, &sibcall_failure);
f725a3ec 2805
0a1c58a2
JL
2806 /* Perform postincrements before actually calling the function. */
2807 emit_queue ();
51bbfa0c 2808
0a1c58a2
JL
2809 /* Save a pointer to the last insn before the call, so that we can
2810 later safely search backwards to find the CALL_INSN. */
2811 before_call = get_last_insn ();
51bbfa0c 2812
7d167afd
JJ
2813 /* Set up next argument register. For sibling calls on machines
2814 with register windows this should be the incoming register. */
2815#ifdef FUNCTION_INCOMING_ARG
2816 if (pass == 0)
2817 next_arg_reg = FUNCTION_INCOMING_ARG (args_so_far, VOIDmode,
2818 void_type_node, 1);
2819 else
2820#endif
2821 next_arg_reg = FUNCTION_ARG (args_so_far, VOIDmode,
2822 void_type_node, 1);
2823
0a1c58a2
JL
2824 /* All arguments and registers used for the call must be set up by
2825 now! */
2826
ce48579b
RH
2827 /* Stack must be properly aligned now. */
2828 if (pass && stack_pointer_delta % preferred_unit_stack_boundary)
d9a7d592 2829 abort ();
ebcd0b57 2830
0a1c58a2 2831 /* Generate the actual call instruction. */
6de9cd9a 2832 emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
099e9712 2833 adjusted_args_size.constant, struct_value_size,
7d167afd 2834 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
fa5322fa 2835 flags, & args_so_far);
0a1c58a2
JL
2836
2837 /* If call is cse'able, make appropriate pair of reg-notes around it.
2838 Test valreg so we don't crash; may safely ignore `const'
2839 if return type is void. Disable for PARALLEL return values, because
2840 we have no way to move such values into a pseudo register. */
53d4257f 2841 if (pass && (flags & ECF_LIBCALL_BLOCK))
9ae8ffe7 2842 {
0a1c58a2 2843 rtx insns;
9778f2f8
JH
2844 rtx insn;
2845 bool failed = valreg == 0 || GET_CODE (valreg) == PARALLEL;
9ae8ffe7 2846
9778f2f8
JH
2847 insns = get_insns ();
2848
2849 /* Expansion of block moves possibly introduced a loop that may
2850 not appear inside libcall block. */
2851 for (insn = insns; insn; insn = NEXT_INSN (insn))
2852 if (GET_CODE (insn) == JUMP_INSN)
2853 failed = true;
2854
2855 if (failed)
e4abc3d5 2856 {
e4abc3d5 2857 end_sequence ();
2f937369 2858 emit_insn (insns);
e4abc3d5
RH
2859 }
2860 else
2861 {
2862 rtx note = 0;
2863 rtx temp = gen_reg_rtx (GET_MODE (valreg));
2864
2865 /* Mark the return value as a pointer if needed. */
2866 if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
2867 mark_reg_pointer (temp,
2868 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp))));
2869
e4abc3d5 2870 end_sequence ();
8f23fc81
MM
2871 if (flag_unsafe_math_optimizations
2872 && fndecl
2873 && DECL_BUILT_IN (fndecl)
2874 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRT
2875 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRTF
2876 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRTL))
2877 note = gen_rtx_fmt_e (SQRT,
2878 GET_MODE (temp),
2879 args[0].initial_value);
2880 else
2881 {
2882 /* Construct an "equal form" for the value which
2883 mentions all the arguments in order as well as
2884 the function name. */
2885 for (i = 0; i < num_actuals; i++)
2886 note = gen_rtx_EXPR_LIST (VOIDmode,
2887 args[i].initial_value, note);
2888 note = gen_rtx_EXPR_LIST (VOIDmode, funexp, note);
2889
2890 if (flags & ECF_PURE)
2891 note = gen_rtx_EXPR_LIST (VOIDmode,
e4abc3d5
RH
2892 gen_rtx_USE (VOIDmode,
2893 gen_rtx_MEM (BLKmode,
2894 gen_rtx_SCRATCH (VOIDmode))),
2895 note);
8f23fc81 2896 }
e4abc3d5
RH
2897 emit_libcall_block (insns, temp, valreg, note);
2898
2899 valreg = temp;
2900 }
0a1c58a2 2901 }
53d4257f 2902 else if (pass && (flags & ECF_MALLOC))
0a1c58a2
JL
2903 {
2904 rtx temp = gen_reg_rtx (GET_MODE (valreg));
2905 rtx last, insns;
2906
f725a3ec 2907 /* The return value from a malloc-like function is a pointer. */
0a1c58a2 2908 if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
bdb429a5 2909 mark_reg_pointer (temp, BIGGEST_ALIGNMENT);
0a1c58a2
JL
2910
2911 emit_move_insn (temp, valreg);
2912
2913 /* The return value from a malloc-like function can not alias
2914 anything else. */
2915 last = get_last_insn ();
f725a3ec 2916 REG_NOTES (last) =
0a1c58a2
JL
2917 gen_rtx_EXPR_LIST (REG_NOALIAS, temp, REG_NOTES (last));
2918
2919 /* Write out the sequence. */
2920 insns = get_insns ();
2921 end_sequence ();
2f937369 2922 emit_insn (insns);
0a1c58a2
JL
2923 valreg = temp;
2924 }
51bbfa0c 2925
0a1c58a2
JL
2926 /* For calls to `setjmp', etc., inform flow.c it should complain
2927 if nonvolatile values are live. For functions that cannot return,
2928 inform flow that control does not fall through. */
51bbfa0c 2929
570a98eb 2930 if ((flags & (ECF_NORETURN | ECF_LONGJMP)) || pass == 0)
c2939b57 2931 {
570a98eb 2932 /* The barrier must be emitted
0a1c58a2
JL
2933 immediately after the CALL_INSN. Some ports emit more
2934 than just a CALL_INSN above, so we must search for it here. */
51bbfa0c 2935
0a1c58a2
JL
2936 rtx last = get_last_insn ();
2937 while (GET_CODE (last) != CALL_INSN)
2938 {
2939 last = PREV_INSN (last);
2940 /* There was no CALL_INSN? */
2941 if (last == before_call)
2942 abort ();
2943 }
51bbfa0c 2944
570a98eb 2945 emit_barrier_after (last);
8af61113 2946
f451eeef
JS
2947 /* Stack adjustments after a noreturn call are dead code.
2948 However when NO_DEFER_POP is in effect, we must preserve
2949 stack_pointer_delta. */
2950 if (inhibit_defer_pop == 0)
2951 {
2952 stack_pointer_delta = old_stack_allocated;
2953 pending_stack_adjust = 0;
2954 }
0a1c58a2 2955 }
51bbfa0c 2956
f2d33f13 2957 if (flags & ECF_LONGJMP)
099e9712 2958 current_function_calls_longjmp = 1;
51bbfa0c 2959
0a1c58a2 2960 /* If value type not void, return an rtx for the value. */
51bbfa0c 2961
0a1c58a2
JL
2962 /* If there are cleanups to be called, don't use a hard reg as target.
2963 We need to double check this and see if it matters anymore. */
de1f5659 2964 if (any_pending_cleanups ())
194c7c45
RH
2965 {
2966 if (target && REG_P (target)
2967 && REGNO (target) < FIRST_PSEUDO_REGISTER)
2968 target = 0;
2969 sibcall_failure = 1;
2970 }
51bbfa0c 2971
0a1c58a2
JL
2972 if (TYPE_MODE (TREE_TYPE (exp)) == VOIDmode
2973 || ignore)
b5cd4ed4 2974 target = const0_rtx;
0a1c58a2
JL
2975 else if (structure_value_addr)
2976 {
3c0cb5de 2977 if (target == 0 || !MEM_P (target))
0a1c58a2 2978 {
3bdf5ad1
RK
2979 target
2980 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
2981 memory_address (TYPE_MODE (TREE_TYPE (exp)),
2982 structure_value_addr));
2983 set_mem_attributes (target, exp, 1);
0a1c58a2
JL
2984 }
2985 }
2986 else if (pcc_struct_value)
cacbd532 2987 {
0a1c58a2
JL
2988 /* This is the special C++ case where we need to
2989 know what the true target was. We take care to
2990 never use this value more than once in one expression. */
2991 target = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
2992 copy_to_reg (valreg));
3bdf5ad1 2993 set_mem_attributes (target, exp, 1);
cacbd532 2994 }
0a1c58a2
JL
2995 /* Handle calls that return values in multiple non-contiguous locations.
2996 The Irix 6 ABI has examples of this. */
2997 else if (GET_CODE (valreg) == PARALLEL)
2998 {
6de9cd9a 2999 if (target == 0)
0a1c58a2 3000 {
1da68f56
RK
3001 /* This will only be assigned once, so it can be readonly. */
3002 tree nt = build_qualified_type (TREE_TYPE (exp),
3003 (TYPE_QUALS (TREE_TYPE (exp))
3004 | TYPE_QUAL_CONST));
3005
3006 target = assign_temp (nt, 0, 1, 1);
0a1c58a2
JL
3007 preserve_temp_slots (target);
3008 }
3009
3010 if (! rtx_equal_p (target, valreg))
6e985040 3011 emit_group_store (target, valreg, TREE_TYPE (exp),
04050c69 3012 int_size_in_bytes (TREE_TYPE (exp)));
19caa751 3013
0a1c58a2
JL
3014 /* We can not support sibling calls for this case. */
3015 sibcall_failure = 1;
3016 }
3017 else if (target
3018 && GET_MODE (target) == TYPE_MODE (TREE_TYPE (exp))
3019 && GET_MODE (target) == GET_MODE (valreg))
3020 {
3021 /* TARGET and VALREG cannot be equal at this point because the
3022 latter would not have REG_FUNCTION_VALUE_P true, while the
3023 former would if it were referring to the same register.
3024
3025 If they refer to the same register, this move will be a no-op,
3026 except when function inlining is being done. */
3027 emit_move_insn (target, valreg);
0219237c
OH
3028
3029 /* If we are setting a MEM, this code must be executed. Since it is
3030 emitted after the call insn, sibcall optimization cannot be
3031 performed in that case. */
3c0cb5de 3032 if (MEM_P (target))
0219237c 3033 sibcall_failure = 1;
0a1c58a2
JL
3034 }
3035 else if (TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
8eb99146
RH
3036 {
3037 target = copy_blkmode_from_reg (target, valreg, TREE_TYPE (exp));
3038
3039 /* We can not support sibling calls for this case. */
3040 sibcall_failure = 1;
3041 }
0a1c58a2 3042 else
c988af2b
RS
3043 {
3044 if (shift_returned_value (TREE_TYPE (exp), &valreg))
3045 sibcall_failure = 1;
3046
3047 target = copy_to_reg (valreg);
3048 }
51bbfa0c 3049
61f71b34
DD
3050 if (targetm.calls.promote_function_return(funtype))
3051 {
0a1c58a2
JL
3052 /* If we promoted this return value, make the proper SUBREG. TARGET
3053 might be const0_rtx here, so be careful. */
f8cfc6aa 3054 if (REG_P (target)
0a1c58a2
JL
3055 && TYPE_MODE (TREE_TYPE (exp)) != BLKmode
3056 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
3057 {
3058 tree type = TREE_TYPE (exp);
8df83eae 3059 int unsignedp = TYPE_UNSIGNED (type);
ddef6bc7 3060 int offset = 0;
84b55618 3061
0a1c58a2
JL
3062 /* If we don't promote as expected, something is wrong. */
3063 if (GET_MODE (target)
3064 != promote_mode (type, TYPE_MODE (type), &unsignedp, 1))
3065 abort ();
5d2ac65e 3066
ddef6bc7
JJ
3067 if ((WORDS_BIG_ENDIAN || BYTES_BIG_ENDIAN)
3068 && GET_MODE_SIZE (GET_MODE (target))
3069 > GET_MODE_SIZE (TYPE_MODE (type)))
3070 {
3071 offset = GET_MODE_SIZE (GET_MODE (target))
3072 - GET_MODE_SIZE (TYPE_MODE (type));
3073 if (! BYTES_BIG_ENDIAN)
3074 offset = (offset / UNITS_PER_WORD) * UNITS_PER_WORD;
3075 else if (! WORDS_BIG_ENDIAN)
3076 offset %= UNITS_PER_WORD;
3077 }
3078 target = gen_rtx_SUBREG (TYPE_MODE (type), target, offset);
0a1c58a2 3079 SUBREG_PROMOTED_VAR_P (target) = 1;
7879b81e 3080 SUBREG_PROMOTED_UNSIGNED_SET (target, unsignedp);
0a1c58a2 3081 }
61f71b34 3082 }
84b55618 3083
0a1c58a2
JL
3084 /* If size of args is variable or this was a constructor call for a stack
3085 argument, restore saved stack-pointer value. */
51bbfa0c 3086
7393c642 3087 if (old_stack_level && ! (flags & ECF_SP_DEPRESSED))
0a1c58a2
JL
3088 {
3089 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
38afb23f 3090 stack_pointer_delta = old_stack_pointer_delta;
0a1c58a2 3091 pending_stack_adjust = old_pending_adj;
0a1c58a2
JL
3092 stack_arg_under_construction = old_stack_arg_under_construction;
3093 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3094 stack_usage_map = initial_stack_usage_map;
0a1c58a2
JL
3095 sibcall_failure = 1;
3096 }
f8a097cd 3097 else if (ACCUMULATE_OUTGOING_ARGS && pass)
0a1c58a2 3098 {
51bbfa0c 3099#ifdef REG_PARM_STACK_SPACE
0a1c58a2 3100 if (save_area)
b820d2b8
AM
3101 restore_fixed_argument_area (save_area, argblock,
3102 high_to_save, low_to_save);
b94301c2 3103#endif
51bbfa0c 3104
0a1c58a2
JL
3105 /* If we saved any argument areas, restore them. */
3106 for (i = 0; i < num_actuals; i++)
3107 if (args[i].save_area)
3108 {
3109 enum machine_mode save_mode = GET_MODE (args[i].save_area);
3110 rtx stack_area
3111 = gen_rtx_MEM (save_mode,
3112 memory_address (save_mode,
3113 XEXP (args[i].stack_slot, 0)));
3114
3115 if (save_mode != BLKmode)
3116 emit_move_insn (stack_area, args[i].save_area);
3117 else
44bb111a 3118 emit_block_move (stack_area, args[i].save_area,
e7949876 3119 GEN_INT (args[i].locate.size.constant),
44bb111a 3120 BLOCK_OP_CALL_PARM);
0a1c58a2 3121 }
51bbfa0c 3122
0a1c58a2
JL
3123 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3124 stack_usage_map = initial_stack_usage_map;
3125 }
51bbfa0c 3126
f725a3ec 3127 /* If this was alloca, record the new stack level for nonlocal gotos.
0a1c58a2
JL
3128 Check for the handler slots since we might not have a save area
3129 for non-local gotos. */
59257ff7 3130
6de9cd9a
DN
3131 if ((flags & ECF_MAY_BE_ALLOCA) && cfun->nonlocal_goto_save_area != 0)
3132 update_nonlocal_goto_save_area ();
51bbfa0c 3133
0a1c58a2
JL
3134 /* Free up storage we no longer need. */
3135 for (i = 0; i < num_actuals; ++i)
3136 if (args[i].aligned_regs)
3137 free (args[i].aligned_regs);
3138
e245d3af
RH
3139 if (pass == 0)
3140 {
3141 /* Undo the fake expand_start_target_temps we did earlier. If
3142 there had been any cleanups created, we've already set
3143 sibcall_failure. */
3144 expand_end_target_temps ();
3145 }
3146
ee960939
OH
3147 /* If this function is returning into a memory location marked as
3148 readonly, it means it is initializing that location. We normally treat
3149 functions as not clobbering such locations, so we need to specify that
3150 this one does. We do this by adding the appropriate CLOBBER to the
3151 CALL_INSN function usage list. This cannot be done by emitting a
3152 standalone CLOBBER after the call because the latter would be ignored
3153 by at least the delay slot scheduling pass. We do this now instead of
3154 adding to call_fusage before the call to emit_call_1 because TARGET
3155 may be modified in the meantime. */
d329e058 3156 if (structure_value_addr != 0 && target != 0
3c0cb5de 3157 && MEM_P (target) && RTX_UNCHANGING_P (target))
ee960939
OH
3158 add_function_usage_to
3159 (last_call_insn (),
3160 gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_CLOBBER (VOIDmode, target),
3161 NULL_RTX));
d329e058 3162
0a1c58a2
JL
3163 insns = get_insns ();
3164 end_sequence ();
3165
3166 if (pass == 0)
3167 {
3168 tail_call_insns = insns;
3169
0a1c58a2
JL
3170 /* Restore the pending stack adjustment now that we have
3171 finished generating the sibling call sequence. */
1503a7ec 3172
0a1c58a2 3173 pending_stack_adjust = save_pending_stack_adjust;
1503a7ec 3174 stack_pointer_delta = save_stack_pointer_delta;
099e9712
JH
3175
3176 /* Prepare arg structure for next iteration. */
f725a3ec 3177 for (i = 0; i < num_actuals; i++)
099e9712
JH
3178 {
3179 args[i].value = 0;
3180 args[i].aligned_regs = 0;
3181 args[i].stack = 0;
3182 }
c67846f2
JJ
3183
3184 sbitmap_free (stored_args_map);
0a1c58a2
JL
3185 }
3186 else
38afb23f
OH
3187 {
3188 normal_call_insns = insns;
3189
3190 /* Verify that we've deallocated all the stack we used. */
8af61113
RS
3191 if (! (flags & (ECF_NORETURN | ECF_LONGJMP))
3192 && old_stack_allocated != stack_pointer_delta
3193 - pending_stack_adjust)
38afb23f
OH
3194 abort ();
3195 }
fadb729c
JJ
3196
3197 /* If something prevents making this a sibling call,
3198 zero out the sequence. */
3199 if (sibcall_failure)
3200 tail_call_insns = NULL_RTX;
6de9cd9a
DN
3201 else
3202 break;
0a1c58a2
JL
3203 }
3204
1ea7e6ad 3205 /* If tail call production succeeded, we need to remove REG_EQUIV notes on
6de9cd9a
DN
3206 arguments too, as argument area is now clobbered by the call. */
3207 if (tail_call_insns)
0a1c58a2 3208 {
6de9cd9a
DN
3209 emit_insn (tail_call_insns);
3210 cfun->tail_call_emit = true;
0a1c58a2
JL
3211 }
3212 else
2f937369 3213 emit_insn (normal_call_insns);
51bbfa0c 3214
0a1c58a2 3215 currently_expanding_call--;
8e6a59fe 3216
7393c642
RK
3217 /* If this function returns with the stack pointer depressed, ensure
3218 this block saves and restores the stack pointer, show it was
3219 changed, and adjust for any outgoing arg space. */
3220 if (flags & ECF_SP_DEPRESSED)
3221 {
3222 clear_pending_stack_adjust ();
f84d109f 3223 emit_insn (gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx));
7393c642
RK
3224 emit_move_insn (virtual_stack_dynamic_rtx, stack_pointer_rtx);
3225 save_stack_pointer ();
3226 }
3227
51bbfa0c
RS
3228 return target;
3229}
ded9bf77 3230
6de9cd9a
DN
3231/* A sibling call sequence invalidates any REG_EQUIV notes made for
3232 this function's incoming arguments.
3233
3234 At the start of RTL generation we know the only REG_EQUIV notes
3235 in the rtl chain are those for incoming arguments, so we can safely
3236 flush any REG_EQUIV note.
3237
3238 This is (slight) overkill. We could keep track of the highest
3239 argument we clobber and be more selective in removing notes, but it
3240 does not seem to be worth the effort. */
3241void
3242fixup_tail_calls (void)
3243{
3244 rtx insn;
3245 tree arg;
3246
3247 purge_reg_equiv_notes ();
3248
3249 /* A sibling call sequence also may invalidate RTX_UNCHANGING_P
3250 flag of some incoming arguments MEM RTLs, because it can write into
3251 those slots. We clear all those bits now.
3252
3253 This is (slight) overkill, we could keep track of which arguments
3254 we actually write into. */
3255 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3256 {
3257 if (INSN_P (insn))
3258 purge_mem_unchanging_flag (PATTERN (insn));
3259 }
3260
3261 /* Similarly, invalidate RTX_UNCHANGING_P for any incoming
3262 arguments passed in registers. */
3263 for (arg = DECL_ARGUMENTS (current_function_decl);
3264 arg;
3265 arg = TREE_CHAIN (arg))
3266 {
3267 if (REG_P (DECL_RTL (arg)))
3268 RTX_UNCHANGING_P (DECL_RTL (arg)) = false;
3269 }
3270}
3271
ded9bf77
AH
3272/* Traverse an argument list in VALUES and expand all complex
3273 arguments into their components. */
3274tree
3275split_complex_values (tree values)
3276{
3277 tree p;
3278
42ba5130
RH
3279 /* Before allocating memory, check for the common case of no complex. */
3280 for (p = values; p; p = TREE_CHAIN (p))
3281 {
3282 tree type = TREE_TYPE (TREE_VALUE (p));
3283 if (type && TREE_CODE (type) == COMPLEX_TYPE
3284 && targetm.calls.split_complex_arg (type))
3285 goto found;
3286 }
3287 return values;
3288
3289 found:
ded9bf77
AH
3290 values = copy_list (values);
3291
3292 for (p = values; p; p = TREE_CHAIN (p))
3293 {
3294 tree complex_value = TREE_VALUE (p);
3295 tree complex_type;
3296
3297 complex_type = TREE_TYPE (complex_value);
3298 if (!complex_type)
3299 continue;
3300
42ba5130
RH
3301 if (TREE_CODE (complex_type) == COMPLEX_TYPE
3302 && targetm.calls.split_complex_arg (complex_type))
ded9bf77
AH
3303 {
3304 tree subtype;
3305 tree real, imag, next;
3306
3307 subtype = TREE_TYPE (complex_type);
3308 complex_value = save_expr (complex_value);
3309 real = build1 (REALPART_EXPR, subtype, complex_value);
3310 imag = build1 (IMAGPART_EXPR, subtype, complex_value);
3311
3312 TREE_VALUE (p) = real;
3313 next = TREE_CHAIN (p);
3314 imag = build_tree_list (NULL_TREE, imag);
3315 TREE_CHAIN (p) = imag;
3316 TREE_CHAIN (imag) = next;
3317
3318 /* Skip the newly created node. */
3319 p = TREE_CHAIN (p);
3320 }
3321 }
3322
3323 return values;
3324}
3325
3326/* Traverse a list of TYPES and expand all complex types into their
3327 components. */
3328tree
3329split_complex_types (tree types)
3330{
3331 tree p;
3332
42ba5130
RH
3333 /* Before allocating memory, check for the common case of no complex. */
3334 for (p = types; p; p = TREE_CHAIN (p))
3335 {
3336 tree type = TREE_VALUE (p);
3337 if (TREE_CODE (type) == COMPLEX_TYPE
3338 && targetm.calls.split_complex_arg (type))
3339 goto found;
3340 }
3341 return types;
3342
3343 found:
ded9bf77
AH
3344 types = copy_list (types);
3345
3346 for (p = types; p; p = TREE_CHAIN (p))
3347 {
3348 tree complex_type = TREE_VALUE (p);
3349
42ba5130
RH
3350 if (TREE_CODE (complex_type) == COMPLEX_TYPE
3351 && targetm.calls.split_complex_arg (complex_type))
ded9bf77
AH
3352 {
3353 tree next, imag;
3354
3355 /* Rewrite complex type with component type. */
3356 TREE_VALUE (p) = TREE_TYPE (complex_type);
3357 next = TREE_CHAIN (p);
3358
3359 /* Add another component type for the imaginary part. */
3360 imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
3361 TREE_CHAIN (p) = imag;
3362 TREE_CHAIN (imag) = next;
3363
3364 /* Skip the newly created node. */
3365 p = TREE_CHAIN (p);
3366 }
3367 }
3368
3369 return types;
3370}
51bbfa0c 3371\f
de76b467 3372/* Output a library call to function FUN (a SYMBOL_REF rtx).
f725a3ec 3373 The RETVAL parameter specifies whether return value needs to be saved, other
0407c02b 3374 parameters are documented in the emit_library_call function below. */
8ac61af7 3375
de76b467 3376static rtx
d329e058
AJ
3377emit_library_call_value_1 (int retval, rtx orgfun, rtx value,
3378 enum libcall_type fn_type,
3379 enum machine_mode outmode, int nargs, va_list p)
43bc5f13 3380{
3c0fca12
RH
3381 /* Total size in bytes of all the stack-parms scanned so far. */
3382 struct args_size args_size;
3383 /* Size of arguments before any adjustments (such as rounding). */
3384 struct args_size original_args_size;
b3694847 3385 int argnum;
3c0fca12
RH
3386 rtx fun;
3387 int inc;
3388 int count;
3c0fca12
RH
3389 rtx argblock = 0;
3390 CUMULATIVE_ARGS args_so_far;
f725a3ec
KH
3391 struct arg
3392 {
3393 rtx value;
3394 enum machine_mode mode;
3395 rtx reg;
3396 int partial;
e7949876 3397 struct locate_and_pad_arg_data locate;
f725a3ec
KH
3398 rtx save_area;
3399 };
3c0fca12
RH
3400 struct arg *argvec;
3401 int old_inhibit_defer_pop = inhibit_defer_pop;
3402 rtx call_fusage = 0;
3403 rtx mem_value = 0;
5591ee6f 3404 rtx valreg;
3c0fca12
RH
3405 int pcc_struct_value = 0;
3406 int struct_value_size = 0;
52a11cbf 3407 int flags;
3c0fca12 3408 int reg_parm_stack_space = 0;
3c0fca12 3409 int needed;
695ee791 3410 rtx before_call;
b0c48229 3411 tree tfom; /* type_for_mode (outmode, 0) */
3c0fca12 3412
f73ad30e 3413#ifdef REG_PARM_STACK_SPACE
3c0fca12
RH
3414 /* Define the boundary of the register parm stack space that needs to be
3415 save, if any. */
b820d2b8 3416 int low_to_save, high_to_save;
f725a3ec 3417 rtx save_area = 0; /* Place that it is saved. */
3c0fca12
RH
3418#endif
3419
3c0fca12
RH
3420 /* Size of the stack reserved for parameter registers. */
3421 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
3422 char *initial_stack_usage_map = stack_usage_map;
3c0fca12 3423
61f71b34
DD
3424 rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
3425
3c0fca12 3426#ifdef REG_PARM_STACK_SPACE
3c0fca12 3427 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
3c0fca12
RH
3428#endif
3429
9555a122 3430 /* By default, library functions can not throw. */
52a11cbf
RH
3431 flags = ECF_NOTHROW;
3432
9555a122
RH
3433 switch (fn_type)
3434 {
3435 case LCT_NORMAL:
53d4257f 3436 break;
9555a122 3437 case LCT_CONST:
53d4257f
JH
3438 flags |= ECF_CONST;
3439 break;
9555a122 3440 case LCT_PURE:
53d4257f 3441 flags |= ECF_PURE;
9555a122
RH
3442 break;
3443 case LCT_CONST_MAKE_BLOCK:
53d4257f 3444 flags |= ECF_CONST | ECF_LIBCALL_BLOCK;
9555a122
RH
3445 break;
3446 case LCT_PURE_MAKE_BLOCK:
53d4257f 3447 flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
9555a122
RH
3448 break;
3449 case LCT_NORETURN:
3450 flags |= ECF_NORETURN;
3451 break;
3452 case LCT_THROW:
3453 flags = ECF_NORETURN;
3454 break;
9d98f8f9
JH
3455 case LCT_ALWAYS_RETURN:
3456 flags = ECF_ALWAYS_RETURN;
3457 break;
9defc9b7
RH
3458 case LCT_RETURNS_TWICE:
3459 flags = ECF_RETURNS_TWICE;
3460 break;
9555a122 3461 }
3c0fca12
RH
3462 fun = orgfun;
3463
3c0fca12
RH
3464 /* Ensure current function's preferred stack boundary is at least
3465 what we need. */
3466 if (cfun->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
3467 cfun->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3c0fca12
RH
3468
3469 /* If this kind of value comes back in memory,
3470 decide where in memory it should come back. */
b0c48229 3471 if (outmode != VOIDmode)
3c0fca12 3472 {
ae2bcd98 3473 tfom = lang_hooks.types.type_for_mode (outmode, 0);
61f71b34 3474 if (aggregate_value_p (tfom, 0))
b0c48229 3475 {
3c0fca12 3476#ifdef PCC_STATIC_STRUCT_RETURN
b0c48229
NB
3477 rtx pointer_reg
3478 = hard_function_value (build_pointer_type (tfom), 0, 0);
3479 mem_value = gen_rtx_MEM (outmode, pointer_reg);
3480 pcc_struct_value = 1;
3481 if (value == 0)
3482 value = gen_reg_rtx (outmode);
3c0fca12 3483#else /* not PCC_STATIC_STRUCT_RETURN */
b0c48229 3484 struct_value_size = GET_MODE_SIZE (outmode);
3c0cb5de 3485 if (value != 0 && MEM_P (value))
b0c48229
NB
3486 mem_value = value;
3487 else
3488 mem_value = assign_temp (tfom, 0, 1, 1);
3c0fca12 3489#endif
b0c48229
NB
3490 /* This call returns a big structure. */
3491 flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
3492 }
3c0fca12 3493 }
b0c48229
NB
3494 else
3495 tfom = void_type_node;
3c0fca12
RH
3496
3497 /* ??? Unfinished: must pass the memory address as an argument. */
3498
3499 /* Copy all the libcall-arguments out of the varargs data
3500 and into a vector ARGVEC.
3501
3502 Compute how to pass each argument. We only support a very small subset
3503 of the full argument passing conventions to limit complexity here since
3504 library functions shouldn't have many args. */
3505
703ad42b
KG
3506 argvec = alloca ((nargs + 1) * sizeof (struct arg));
3507 memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
3c0fca12 3508
97fc4caf
AO
3509#ifdef INIT_CUMULATIVE_LIBCALL_ARGS
3510 INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far, outmode, fun);
3511#else
0f6937fe 3512 INIT_CUMULATIVE_ARGS (args_so_far, NULL_TREE, fun, 0, nargs);
97fc4caf 3513#endif
3c0fca12
RH
3514
3515 args_size.constant = 0;
3516 args_size.var = 0;
3517
3518 count = 0;
3519
ebb1b59a
BS
3520 /* Now we are about to start emitting insns that can be deleted
3521 if a libcall is deleted. */
53d4257f 3522 if (flags & ECF_LIBCALL_BLOCK)
ebb1b59a
BS
3523 start_sequence ();
3524
3c0fca12
RH
3525 push_temp_slots ();
3526
3527 /* If there's a structure value address to be passed,
3528 either pass it in the special place, or pass it as an extra argument. */
61f71b34 3529 if (mem_value && struct_value == 0 && ! pcc_struct_value)
3c0fca12
RH
3530 {
3531 rtx addr = XEXP (mem_value, 0);
3532 nargs++;
3533
3534 /* Make sure it is a reasonable operand for a move or push insn. */
3c0cb5de 3535 if (!REG_P (addr) && !MEM_P (addr)
3c0fca12
RH
3536 && ! (CONSTANT_P (addr) && LEGITIMATE_CONSTANT_P (addr)))
3537 addr = force_operand (addr, NULL_RTX);
3538
3539 argvec[count].value = addr;
3540 argvec[count].mode = Pmode;
3541 argvec[count].partial = 0;
3542
3543 argvec[count].reg = FUNCTION_ARG (args_so_far, Pmode, NULL_TREE, 1);
3544#ifdef FUNCTION_ARG_PARTIAL_NREGS
3545 if (FUNCTION_ARG_PARTIAL_NREGS (args_so_far, Pmode, NULL_TREE, 1))
3546 abort ();
3547#endif
3548
3549 locate_and_pad_parm (Pmode, NULL_TREE,
a4d5044f
CM
3550#ifdef STACK_PARMS_IN_REG_PARM_AREA
3551 1,
3552#else
3553 argvec[count].reg != 0,
3554#endif
e7949876 3555 0, NULL_TREE, &args_size, &argvec[count].locate);
3c0fca12 3556
3c0fca12
RH
3557 if (argvec[count].reg == 0 || argvec[count].partial != 0
3558 || reg_parm_stack_space > 0)
e7949876 3559 args_size.constant += argvec[count].locate.size.constant;
3c0fca12
RH
3560
3561 FUNCTION_ARG_ADVANCE (args_so_far, Pmode, (tree) 0, 1);
3562
3563 count++;
3564 }
3565
3566 for (; count < nargs; count++)
3567 {
3568 rtx val = va_arg (p, rtx);
3569 enum machine_mode mode = va_arg (p, enum machine_mode);
3570
3571 /* We cannot convert the arg value to the mode the library wants here;
3572 must do it earlier where we know the signedness of the arg. */
3573 if (mode == BLKmode
3574 || (GET_MODE (val) != mode && GET_MODE (val) != VOIDmode))
3575 abort ();
3576
3c0fca12
RH
3577 /* There's no need to call protect_from_queue, because
3578 either emit_move_insn or emit_push_insn will do that. */
3579
3580 /* Make sure it is a reasonable operand for a move or push insn. */
3c0cb5de 3581 if (!REG_P (val) && !MEM_P (val)
3c0fca12
RH
3582 && ! (CONSTANT_P (val) && LEGITIMATE_CONSTANT_P (val)))
3583 val = force_operand (val, NULL_RTX);
3584
3585#ifdef FUNCTION_ARG_PASS_BY_REFERENCE
3586 if (FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, mode, NULL_TREE, 1))
3587 {
f474c6f8
AO
3588 rtx slot;
3589 int must_copy = 1
d329e058 3590#ifdef FUNCTION_ARG_CALLEE_COPIES
f474c6f8
AO
3591 && ! FUNCTION_ARG_CALLEE_COPIES (args_so_far, mode,
3592 NULL_TREE, 1)
3593#endif
3594 ;
3595
a0dc500c
R
3596 /* loop.c won't look at CALL_INSN_FUNCTION_USAGE of const/pure
3597 functions, so we have to pretend this isn't such a function. */
3598 if (flags & ECF_LIBCALL_BLOCK)
3599 {
3600 rtx insns = get_insns ();
3601 end_sequence ();
3602 emit_insn (insns);
3603 }
3604 flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
3605
99a32567
DM
3606 /* If this was a CONST function, it is now PURE since
3607 it now reads memory. */
3608 if (flags & ECF_CONST)
3609 {
3610 flags &= ~ECF_CONST;
3611 flags |= ECF_PURE;
3612 }
3613
f474c6f8
AO
3614 if (GET_MODE (val) == MEM && ! must_copy)
3615 slot = val;
3616 else if (must_copy)
3617 {
ae2bcd98 3618 slot = assign_temp (lang_hooks.types.type_for_mode (mode, 0),
b0c48229 3619 0, 1, 1);
f474c6f8
AO
3620 emit_move_insn (slot, val);
3621 }
3622 else
3623 {
ae2bcd98 3624 tree type = lang_hooks.types.type_for_mode (mode, 0);
f474c6f8 3625
546ff777
AM
3626 slot
3627 = gen_rtx_MEM (mode,
3628 expand_expr (build1 (ADDR_EXPR,
3629 build_pointer_type (type),
3630 make_tree (type, val)),
3631 NULL_RTX, VOIDmode, 0));
f474c6f8 3632 }
1da68f56 3633
6b5273c3
AO
3634 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
3635 gen_rtx_USE (VOIDmode, slot),
3636 call_fusage);
f474c6f8
AO
3637 if (must_copy)
3638 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
3639 gen_rtx_CLOBBER (VOIDmode,
3640 slot),
3641 call_fusage);
3642
3c0fca12 3643 mode = Pmode;
f474c6f8 3644 val = force_operand (XEXP (slot, 0), NULL_RTX);
3c0fca12
RH
3645 }
3646#endif
3647
3648 argvec[count].value = val;
3649 argvec[count].mode = mode;
3650
3651 argvec[count].reg = FUNCTION_ARG (args_so_far, mode, NULL_TREE, 1);
3652
3653#ifdef FUNCTION_ARG_PARTIAL_NREGS
3654 argvec[count].partial
3655 = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, mode, NULL_TREE, 1);
3656#else
3657 argvec[count].partial = 0;
3658#endif
3659
3660 locate_and_pad_parm (mode, NULL_TREE,
a4d5044f 3661#ifdef STACK_PARMS_IN_REG_PARM_AREA
f725a3ec 3662 1,
a4d5044f
CM
3663#else
3664 argvec[count].reg != 0,
3665#endif
e7949876
AM
3666 argvec[count].partial,
3667 NULL_TREE, &args_size, &argvec[count].locate);
3c0fca12 3668
e7949876 3669 if (argvec[count].locate.size.var)
3c0fca12
RH
3670 abort ();
3671
3c0fca12
RH
3672 if (argvec[count].reg == 0 || argvec[count].partial != 0
3673 || reg_parm_stack_space > 0)
e7949876 3674 args_size.constant += argvec[count].locate.size.constant;
3c0fca12
RH
3675
3676 FUNCTION_ARG_ADVANCE (args_so_far, mode, (tree) 0, 1);
3677 }
3c0fca12 3678
3c0fca12
RH
3679 /* If this machine requires an external definition for library
3680 functions, write one out. */
3681 assemble_external_libcall (fun);
3682
3683 original_args_size = args_size;
1503a7ec
JH
3684 args_size.constant = (((args_size.constant
3685 + stack_pointer_delta
3686 + STACK_BYTES - 1)
3687 / STACK_BYTES
3688 * STACK_BYTES)
3689 - stack_pointer_delta);
3c0fca12
RH
3690
3691 args_size.constant = MAX (args_size.constant,
3692 reg_parm_stack_space);
3693
3694#ifndef OUTGOING_REG_PARM_STACK_SPACE
3695 args_size.constant -= reg_parm_stack_space;
3696#endif
3697
3698 if (args_size.constant > current_function_outgoing_args_size)
3699 current_function_outgoing_args_size = args_size.constant;
3700
f73ad30e
JH
3701 if (ACCUMULATE_OUTGOING_ARGS)
3702 {
3703 /* Since the stack pointer will never be pushed, it is possible for
3704 the evaluation of a parm to clobber something we have already
3705 written to the stack. Since most function calls on RISC machines
3706 do not use the stack, this is uncommon, but must work correctly.
3c0fca12 3707
f73ad30e
JH
3708 Therefore, we save any area of the stack that was already written
3709 and that we are using. Here we set up to do this by making a new
3710 stack usage map from the old one.
3c0fca12 3711
f73ad30e
JH
3712 Another approach might be to try to reorder the argument
3713 evaluations to avoid this conflicting stack usage. */
3c0fca12 3714
f73ad30e 3715 needed = args_size.constant;
3c0fca12
RH
3716
3717#ifndef OUTGOING_REG_PARM_STACK_SPACE
f73ad30e
JH
3718 /* Since we will be writing into the entire argument area, the
3719 map must be allocated for its entire size, not just the part that
3720 is the responsibility of the caller. */
3721 needed += reg_parm_stack_space;
3c0fca12
RH
3722#endif
3723
3724#ifdef ARGS_GROW_DOWNWARD
f73ad30e
JH
3725 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3726 needed + 1);
3c0fca12 3727#else
f73ad30e
JH
3728 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3729 needed);
3c0fca12 3730#endif
703ad42b 3731 stack_usage_map = alloca (highest_outgoing_arg_in_use);
3c0fca12 3732
f73ad30e 3733 if (initial_highest_arg_in_use)
2e09e75a
JM
3734 memcpy (stack_usage_map, initial_stack_usage_map,
3735 initial_highest_arg_in_use);
3c0fca12 3736
f73ad30e 3737 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
961192e1 3738 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
f73ad30e
JH
3739 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
3740 needed = 0;
3c0fca12 3741
c39ada04
DD
3742 /* We must be careful to use virtual regs before they're instantiated,
3743 and real regs afterwards. Loop optimization, for example, can create
3744 new libcalls after we've instantiated the virtual regs, and if we
3745 use virtuals anyway, they won't match the rtl patterns. */
3c0fca12 3746
c39ada04
DD
3747 if (virtuals_instantiated)
3748 argblock = plus_constant (stack_pointer_rtx, STACK_POINTER_OFFSET);
3749 else
3750 argblock = virtual_outgoing_args_rtx;
f73ad30e
JH
3751 }
3752 else
3753 {
3754 if (!PUSH_ARGS)
3755 argblock = push_block (GEN_INT (args_size.constant), 0, 0);
3756 }
3c0fca12 3757
3c0fca12
RH
3758 /* If we push args individually in reverse order, perform stack alignment
3759 before the first push (the last arg). */
f73ad30e 3760 if (argblock == 0 && PUSH_ARGS_REVERSED)
3c0fca12
RH
3761 anti_adjust_stack (GEN_INT (args_size.constant
3762 - original_args_size.constant));
3c0fca12 3763
f73ad30e
JH
3764 if (PUSH_ARGS_REVERSED)
3765 {
3766 inc = -1;
3767 argnum = nargs - 1;
3768 }
3769 else
3770 {
3771 inc = 1;
3772 argnum = 0;
3773 }
3c0fca12 3774
f73ad30e
JH
3775#ifdef REG_PARM_STACK_SPACE
3776 if (ACCUMULATE_OUTGOING_ARGS)
3777 {
3778 /* The argument list is the property of the called routine and it
3779 may clobber it. If the fixed area has been used for previous
b820d2b8
AM
3780 parameters, we must save and restore it. */
3781 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3782 &low_to_save, &high_to_save);
3c0fca12
RH
3783 }
3784#endif
f725a3ec 3785
3c0fca12
RH
3786 /* Push the args that need to be pushed. */
3787
3788 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
3789 are to be pushed. */
3790 for (count = 0; count < nargs; count++, argnum += inc)
3791 {
b3694847
SS
3792 enum machine_mode mode = argvec[argnum].mode;
3793 rtx val = argvec[argnum].value;
3c0fca12
RH
3794 rtx reg = argvec[argnum].reg;
3795 int partial = argvec[argnum].partial;
f73ad30e 3796 int lower_bound = 0, upper_bound = 0, i;
3c0fca12
RH
3797
3798 if (! (reg != 0 && partial == 0))
3799 {
f73ad30e
JH
3800 if (ACCUMULATE_OUTGOING_ARGS)
3801 {
f8a097cd
JH
3802 /* If this is being stored into a pre-allocated, fixed-size,
3803 stack area, save any previous data at that location. */
3c0fca12
RH
3804
3805#ifdef ARGS_GROW_DOWNWARD
f73ad30e
JH
3806 /* stack_slot is negative, but we want to index stack_usage_map
3807 with positive values. */
e7949876
AM
3808 upper_bound = -argvec[argnum].locate.offset.constant + 1;
3809 lower_bound = upper_bound - argvec[argnum].locate.size.constant;
3c0fca12 3810#else
e7949876
AM
3811 lower_bound = argvec[argnum].locate.offset.constant;
3812 upper_bound = lower_bound + argvec[argnum].locate.size.constant;
3c0fca12
RH
3813#endif
3814
546ff777
AM
3815 i = lower_bound;
3816 /* Don't worry about things in the fixed argument area;
3817 it has already been saved. */
3818 if (i < reg_parm_stack_space)
3819 i = reg_parm_stack_space;
3820 while (i < upper_bound && stack_usage_map[i] == 0)
3821 i++;
3c0fca12 3822
546ff777 3823 if (i < upper_bound)
f73ad30e 3824 {
e7949876
AM
3825 /* We need to make a save area. */
3826 unsigned int size
3827 = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
f73ad30e 3828 enum machine_mode save_mode
e7949876
AM
3829 = mode_for_size (size, MODE_INT, 1);
3830 rtx adr
3831 = plus_constant (argblock,
3832 argvec[argnum].locate.offset.constant);
f73ad30e 3833 rtx stack_area
e7949876 3834 = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
f73ad30e 3835
9778f2f8
JH
3836 if (save_mode == BLKmode)
3837 {
3838 argvec[argnum].save_area
3839 = assign_stack_temp (BLKmode,
3840 argvec[argnum].locate.size.constant,
3841 0);
3842
3843 emit_block_move (validize_mem (argvec[argnum].save_area),
3844 stack_area,
3845 GEN_INT (argvec[argnum].locate.size.constant),
3846 BLOCK_OP_CALL_PARM);
3847 }
3848 else
3849 {
3850 argvec[argnum].save_area = gen_reg_rtx (save_mode);
3851
3852 emit_move_insn (argvec[argnum].save_area, stack_area);
3853 }
f73ad30e 3854 }
3c0fca12 3855 }
19caa751 3856
44bb111a
RH
3857 emit_push_insn (val, mode, NULL_TREE, NULL_RTX, PARM_BOUNDARY,
3858 partial, reg, 0, argblock,
e7949876
AM
3859 GEN_INT (argvec[argnum].locate.offset.constant),
3860 reg_parm_stack_space,
3861 ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad));
3c0fca12 3862
3c0fca12 3863 /* Now mark the segment we just used. */
f73ad30e
JH
3864 if (ACCUMULATE_OUTGOING_ARGS)
3865 for (i = lower_bound; i < upper_bound; i++)
3866 stack_usage_map[i] = 1;
3c0fca12
RH
3867
3868 NO_DEFER_POP;
3869 }
3870 }
3871
3c0fca12
RH
3872 /* If we pushed args in forward order, perform stack alignment
3873 after pushing the last arg. */
f73ad30e 3874 if (argblock == 0 && !PUSH_ARGS_REVERSED)
3c0fca12
RH
3875 anti_adjust_stack (GEN_INT (args_size.constant
3876 - original_args_size.constant));
3c0fca12 3877
f73ad30e
JH
3878 if (PUSH_ARGS_REVERSED)
3879 argnum = nargs - 1;
3880 else
3881 argnum = 0;
3c0fca12 3882
6de9cd9a 3883 fun = prepare_call_address (fun, NULL, &call_fusage, 0, 0);
3c0fca12
RH
3884
3885 /* Now load any reg parms into their regs. */
3886
3887 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
3888 are to be pushed. */
3889 for (count = 0; count < nargs; count++, argnum += inc)
3890 {
b3694847 3891 rtx val = argvec[argnum].value;
3c0fca12
RH
3892 rtx reg = argvec[argnum].reg;
3893 int partial = argvec[argnum].partial;
3894
3895 /* Handle calls that pass values in multiple non-contiguous
3896 locations. The PA64 has examples of this for library calls. */
3897 if (reg != 0 && GET_CODE (reg) == PARALLEL)
6e985040 3898 emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (GET_MODE (val)));
3c0fca12
RH
3899 else if (reg != 0 && partial == 0)
3900 emit_move_insn (reg, val);
3901
3902 NO_DEFER_POP;
3903 }
3904
3c0fca12
RH
3905 /* Any regs containing parms remain in use through the call. */
3906 for (count = 0; count < nargs; count++)
3907 {
3908 rtx reg = argvec[count].reg;
3909 if (reg != 0 && GET_CODE (reg) == PARALLEL)
3910 use_group_regs (&call_fusage, reg);
3911 else if (reg != 0)
3912 use_reg (&call_fusage, reg);
3913 }
3914
3915 /* Pass the function the address in which to return a structure value. */
61f71b34 3916 if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
3c0fca12 3917 {
61f71b34 3918 emit_move_insn (struct_value,
3c0fca12
RH
3919 force_reg (Pmode,
3920 force_operand (XEXP (mem_value, 0),
3921 NULL_RTX)));
f8cfc6aa 3922 if (REG_P (struct_value))
61f71b34 3923 use_reg (&call_fusage, struct_value);
3c0fca12
RH
3924 }
3925
3926 /* Don't allow popping to be deferred, since then
3927 cse'ing of library calls could delete a call and leave the pop. */
3928 NO_DEFER_POP;
5591ee6f
JH
3929 valreg = (mem_value == 0 && outmode != VOIDmode
3930 ? hard_libcall_value (outmode) : NULL_RTX);
3c0fca12 3931
ce48579b 3932 /* Stack must be properly aligned now. */
ebcd0b57 3933 if (stack_pointer_delta & (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1))
f725a3ec 3934 abort ();
ebcd0b57 3935
695ee791
RH
3936 before_call = get_last_insn ();
3937
3c0fca12
RH
3938 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
3939 will set inhibit_defer_pop to that value. */
de76b467
JH
3940 /* The return type is needed to decide how many bytes the function pops.
3941 Signedness plays no role in that, so for simplicity, we pretend it's
3942 always signed. We also assume that the list of arguments passed has
3943 no impact, so we pretend it is unknown. */
3c0fca12 3944
6de9cd9a 3945 emit_call_1 (fun, NULL,
f725a3ec 3946 get_identifier (XSTR (orgfun, 0)),
b0c48229 3947 build_function_type (tfom, NULL_TREE),
f725a3ec 3948 original_args_size.constant, args_size.constant,
3c0fca12
RH
3949 struct_value_size,
3950 FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1),
5591ee6f 3951 valreg,
fa5322fa 3952 old_inhibit_defer_pop + 1, call_fusage, flags, & args_so_far);
3c0fca12 3953
695ee791
RH
3954 /* For calls to `setjmp', etc., inform flow.c it should complain
3955 if nonvolatile values are live. For functions that cannot return,
3956 inform flow that control does not fall through. */
3957
570a98eb 3958 if (flags & (ECF_NORETURN | ECF_LONGJMP))
695ee791 3959 {
570a98eb 3960 /* The barrier note must be emitted
695ee791
RH
3961 immediately after the CALL_INSN. Some ports emit more than
3962 just a CALL_INSN above, so we must search for it here. */
3963
3964 rtx last = get_last_insn ();
3965 while (GET_CODE (last) != CALL_INSN)
3966 {
3967 last = PREV_INSN (last);
3968 /* There was no CALL_INSN? */
3969 if (last == before_call)
3970 abort ();
3971 }
3972
570a98eb 3973 emit_barrier_after (last);
695ee791
RH
3974 }
3975
3c0fca12
RH
3976 /* Now restore inhibit_defer_pop to its actual original value. */
3977 OK_DEFER_POP;
3978
ebb1b59a
BS
3979 /* If call is cse'able, make appropriate pair of reg-notes around it.
3980 Test valreg so we don't crash; may safely ignore `const'
3981 if return type is void. Disable for PARALLEL return values, because
3982 we have no way to move such values into a pseudo register. */
53d4257f 3983 if (flags & ECF_LIBCALL_BLOCK)
ebb1b59a 3984 {
ebb1b59a 3985 rtx insns;
ebb1b59a 3986
c3297561 3987 if (valreg == 0)
e4abc3d5
RH
3988 {
3989 insns = get_insns ();
3990 end_sequence ();
2f937369 3991 emit_insn (insns);
e4abc3d5
RH
3992 }
3993 else
3994 {
3995 rtx note = 0;
c3297561 3996 rtx temp;
e4abc3d5 3997 int i;
ebb1b59a 3998
c3297561
AO
3999 if (GET_CODE (valreg) == PARALLEL)
4000 {
4001 temp = gen_reg_rtx (outmode);
643642eb
DJ
4002 emit_group_store (temp, valreg, NULL_TREE,
4003 GET_MODE_SIZE (outmode));
c3297561
AO
4004 valreg = temp;
4005 }
4006
4007 temp = gen_reg_rtx (GET_MODE (valreg));
4008
e4abc3d5
RH
4009 /* Construct an "equal form" for the value which mentions all the
4010 arguments in order as well as the function name. */
4011 for (i = 0; i < nargs; i++)
4012 note = gen_rtx_EXPR_LIST (VOIDmode, argvec[i].value, note);
4013 note = gen_rtx_EXPR_LIST (VOIDmode, fun, note);
ebb1b59a 4014
e4abc3d5
RH
4015 insns = get_insns ();
4016 end_sequence ();
ebb1b59a 4017
e4abc3d5
RH
4018 if (flags & ECF_PURE)
4019 note = gen_rtx_EXPR_LIST (VOIDmode,
4020 gen_rtx_USE (VOIDmode,
4021 gen_rtx_MEM (BLKmode,
4022 gen_rtx_SCRATCH (VOIDmode))),
4023 note);
4024
4025 emit_libcall_block (insns, temp, valreg, note);
ebb1b59a 4026
e4abc3d5
RH
4027 valreg = temp;
4028 }
ebb1b59a 4029 }
3c0fca12
RH
4030 pop_temp_slots ();
4031
4032 /* Copy the value to the right place. */
de76b467 4033 if (outmode != VOIDmode && retval)
3c0fca12
RH
4034 {
4035 if (mem_value)
4036 {
4037 if (value == 0)
4038 value = mem_value;
4039 if (value != mem_value)
4040 emit_move_insn (value, mem_value);
4041 }
c3297561
AO
4042 else if (GET_CODE (valreg) == PARALLEL)
4043 {
4044 if (value == 0)
4045 value = gen_reg_rtx (outmode);
643642eb 4046 emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
c3297561 4047 }
3c0fca12 4048 else if (value != 0)
d57551c7 4049 emit_move_insn (value, valreg);
3c0fca12 4050 else
d57551c7 4051 value = valreg;
3c0fca12
RH
4052 }
4053
f73ad30e 4054 if (ACCUMULATE_OUTGOING_ARGS)
3c0fca12 4055 {
f73ad30e
JH
4056#ifdef REG_PARM_STACK_SPACE
4057 if (save_area)
b820d2b8
AM
4058 restore_fixed_argument_area (save_area, argblock,
4059 high_to_save, low_to_save);
3c0fca12 4060#endif
f725a3ec 4061
f73ad30e
JH
4062 /* If we saved any argument areas, restore them. */
4063 for (count = 0; count < nargs; count++)
4064 if (argvec[count].save_area)
4065 {
4066 enum machine_mode save_mode = GET_MODE (argvec[count].save_area);
e7949876
AM
4067 rtx adr = plus_constant (argblock,
4068 argvec[count].locate.offset.constant);
4069 rtx stack_area = gen_rtx_MEM (save_mode,
4070 memory_address (save_mode, adr));
f73ad30e 4071
9778f2f8
JH
4072 if (save_mode == BLKmode)
4073 emit_block_move (stack_area,
4074 validize_mem (argvec[count].save_area),
4075 GEN_INT (argvec[count].locate.size.constant),
4076 BLOCK_OP_CALL_PARM);
4077 else
4078 emit_move_insn (stack_area, argvec[count].save_area);
f73ad30e 4079 }
3c0fca12 4080
f73ad30e
JH
4081 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
4082 stack_usage_map = initial_stack_usage_map;
4083 }
43bc5f13 4084
de76b467
JH
4085 return value;
4086
4087}
4088\f
4089/* Output a library call to function FUN (a SYMBOL_REF rtx)
4090 (emitting the queue unless NO_QUEUE is nonzero),
4091 for a value of mode OUTMODE,
4092 with NARGS different arguments, passed as alternating rtx values
4093 and machine_modes to convert them to.
4094 The rtx values should have been passed through protect_from_queue already.
4095
1258ee80
JJ
4096 FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for `const'
4097 calls, LCT_PURE for `pure' calls, LCT_CONST_MAKE_BLOCK for `const' calls
4098 which should be enclosed in REG_LIBCALL/REG_RETVAL notes,
4099 LCT_PURE_MAKE_BLOCK for `purep' calls which should be enclosed in
4100 REG_LIBCALL/REG_RETVAL notes with extra (use (memory (scratch)),
4101 or other LCT_ value for other types of library calls. */
de76b467
JH
4102
4103void
e34d07f2
KG
4104emit_library_call (rtx orgfun, enum libcall_type fn_type,
4105 enum machine_mode outmode, int nargs, ...)
de76b467 4106{
e34d07f2 4107 va_list p;
d329e058 4108
e34d07f2 4109 va_start (p, nargs);
2a8f6b90 4110 emit_library_call_value_1 (0, orgfun, NULL_RTX, fn_type, outmode, nargs, p);
e34d07f2 4111 va_end (p);
de76b467
JH
4112}
4113\f
4114/* Like emit_library_call except that an extra argument, VALUE,
4115 comes second and says where to store the result.
4116 (If VALUE is zero, this function chooses a convenient way
4117 to return the value.
4118
4119 This function returns an rtx for where the value is to be found.
4120 If VALUE is nonzero, VALUE is returned. */
4121
4122rtx
e34d07f2
KG
4123emit_library_call_value (rtx orgfun, rtx value,
4124 enum libcall_type fn_type,
4125 enum machine_mode outmode, int nargs, ...)
de76b467 4126{
6268b922 4127 rtx result;
e34d07f2 4128 va_list p;
d329e058 4129
e34d07f2 4130 va_start (p, nargs);
6268b922
KG
4131 result = emit_library_call_value_1 (1, orgfun, value, fn_type, outmode,
4132 nargs, p);
e34d07f2 4133 va_end (p);
de76b467 4134
6268b922 4135 return result;
322e3e34
RK
4136}
4137\f
51bbfa0c
RS
4138/* Store a single argument for a function call
4139 into the register or memory area where it must be passed.
4140 *ARG describes the argument value and where to pass it.
4141
4142 ARGBLOCK is the address of the stack-block for all the arguments,
d45cf215 4143 or 0 on a machine where arguments are pushed individually.
51bbfa0c
RS
4144
4145 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
f725a3ec 4146 so must be careful about how the stack is used.
51bbfa0c
RS
4147
4148 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
4149 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
4150 that we need not worry about saving and restoring the stack.
4151
4c6b3b2a 4152 FNDECL is the declaration of the function we are calling.
f725a3ec 4153
da7d8304 4154 Return nonzero if this arg should cause sibcall failure,
4c6b3b2a 4155 zero otherwise. */
51bbfa0c 4156
4c6b3b2a 4157static int
d329e058
AJ
4158store_one_arg (struct arg_data *arg, rtx argblock, int flags,
4159 int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
51bbfa0c 4160{
b3694847 4161 tree pval = arg->tree_value;
51bbfa0c
RS
4162 rtx reg = 0;
4163 int partial = 0;
4164 int used = 0;
6a651371 4165 int i, lower_bound = 0, upper_bound = 0;
4c6b3b2a 4166 int sibcall_failure = 0;
51bbfa0c
RS
4167
4168 if (TREE_CODE (pval) == ERROR_MARK)
4c6b3b2a 4169 return 1;
51bbfa0c 4170
cc79451b
RK
4171 /* Push a new temporary level for any temporaries we make for
4172 this argument. */
4173 push_temp_slots ();
4174
f8a097cd 4175 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
51bbfa0c 4176 {
f73ad30e
JH
4177 /* If this is being stored into a pre-allocated, fixed-size, stack area,
4178 save any previous data at that location. */
4179 if (argblock && ! variable_size && arg->stack)
4180 {
51bbfa0c 4181#ifdef ARGS_GROW_DOWNWARD
f73ad30e
JH
4182 /* stack_slot is negative, but we want to index stack_usage_map
4183 with positive values. */
4184 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4185 upper_bound = -INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1)) + 1;
4186 else
4187 upper_bound = 0;
51bbfa0c 4188
e7949876 4189 lower_bound = upper_bound - arg->locate.size.constant;
51bbfa0c 4190#else
f73ad30e
JH
4191 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4192 lower_bound = INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1));
4193 else
4194 lower_bound = 0;
51bbfa0c 4195
e7949876 4196 upper_bound = lower_bound + arg->locate.size.constant;
51bbfa0c
RS
4197#endif
4198
546ff777
AM
4199 i = lower_bound;
4200 /* Don't worry about things in the fixed argument area;
4201 it has already been saved. */
4202 if (i < reg_parm_stack_space)
4203 i = reg_parm_stack_space;
4204 while (i < upper_bound && stack_usage_map[i] == 0)
4205 i++;
51bbfa0c 4206
546ff777 4207 if (i < upper_bound)
51bbfa0c 4208 {
e7949876
AM
4209 /* We need to make a save area. */
4210 unsigned int size = arg->locate.size.constant * BITS_PER_UNIT;
4211 enum machine_mode save_mode = mode_for_size (size, MODE_INT, 1);
4212 rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
4213 rtx stack_area = gen_rtx_MEM (save_mode, adr);
f73ad30e
JH
4214
4215 if (save_mode == BLKmode)
4216 {
1da68f56
RK
4217 tree ot = TREE_TYPE (arg->tree_value);
4218 tree nt = build_qualified_type (ot, (TYPE_QUALS (ot)
4219 | TYPE_QUAL_CONST));
4220
4221 arg->save_area = assign_temp (nt, 0, 1, 1);
f73ad30e
JH
4222 preserve_temp_slots (arg->save_area);
4223 emit_block_move (validize_mem (arg->save_area), stack_area,
44bb111a
RH
4224 expr_size (arg->tree_value),
4225 BLOCK_OP_CALL_PARM);
f73ad30e
JH
4226 }
4227 else
4228 {
4229 arg->save_area = gen_reg_rtx (save_mode);
4230 emit_move_insn (arg->save_area, stack_area);
4231 }
51bbfa0c
RS
4232 }
4233 }
4234 }
b564df06 4235
51bbfa0c
RS
4236 /* If this isn't going to be placed on both the stack and in registers,
4237 set up the register and number of words. */
4238 if (! arg->pass_on_stack)
aa7634dd
DM
4239 {
4240 if (flags & ECF_SIBCALL)
4241 reg = arg->tail_call_reg;
4242 else
4243 reg = arg->reg;
4244 partial = arg->partial;
4245 }
51bbfa0c
RS
4246
4247 if (reg != 0 && partial == 0)
4248 /* Being passed entirely in a register. We shouldn't be called in
6d2f8887 4249 this case. */
51bbfa0c
RS
4250 abort ();
4251
4ab56118
RK
4252 /* If this arg needs special alignment, don't load the registers
4253 here. */
4254 if (arg->n_aligned_regs != 0)
4255 reg = 0;
f725a3ec 4256
4ab56118 4257 /* If this is being passed partially in a register, we can't evaluate
51bbfa0c
RS
4258 it directly into its stack slot. Otherwise, we can. */
4259 if (arg->value == 0)
d64f5a78 4260 {
d64f5a78
RS
4261 /* stack_arg_under_construction is nonzero if a function argument is
4262 being evaluated directly into the outgoing argument list and
4263 expand_call must take special action to preserve the argument list
4264 if it is called recursively.
4265
4266 For scalar function arguments stack_usage_map is sufficient to
4267 determine which stack slots must be saved and restored. Scalar
4268 arguments in general have pass_on_stack == 0.
4269
4270 If this argument is initialized by a function which takes the
4271 address of the argument (a C++ constructor or a C function
4272 returning a BLKmode structure), then stack_usage_map is
4273 insufficient and expand_call must push the stack around the
4274 function call. Such arguments have pass_on_stack == 1.
4275
4276 Note that it is always safe to set stack_arg_under_construction,
4277 but this generates suboptimal code if set when not needed. */
4278
4279 if (arg->pass_on_stack)
4280 stack_arg_under_construction++;
f73ad30e 4281
3a08477a
RK
4282 arg->value = expand_expr (pval,
4283 (partial
4284 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
4285 ? NULL_RTX : arg->stack,
8403445a 4286 VOIDmode, EXPAND_STACK_PARM);
1efe6448
RK
4287
4288 /* If we are promoting object (or for any other reason) the mode
4289 doesn't agree, convert the mode. */
4290
7373d92d
RK
4291 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
4292 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
4293 arg->value, arg->unsignedp);
1efe6448 4294
d64f5a78
RS
4295 if (arg->pass_on_stack)
4296 stack_arg_under_construction--;
d64f5a78 4297 }
51bbfa0c
RS
4298
4299 /* Don't allow anything left on stack from computation
4300 of argument to alloca. */
f8a097cd 4301 if (flags & ECF_MAY_BE_ALLOCA)
51bbfa0c
RS
4302 do_pending_stack_adjust ();
4303
4304 if (arg->value == arg->stack)
37a08a29
RK
4305 /* If the value is already in the stack slot, we are done. */
4306 ;
1efe6448 4307 else if (arg->mode != BLKmode)
51bbfa0c 4308 {
b3694847 4309 int size;
51bbfa0c
RS
4310
4311 /* Argument is a scalar, not entirely passed in registers.
4312 (If part is passed in registers, arg->partial says how much
4313 and emit_push_insn will take care of putting it there.)
f725a3ec 4314
51bbfa0c
RS
4315 Push it, and if its size is less than the
4316 amount of space allocated to it,
4317 also bump stack pointer by the additional space.
4318 Note that in C the default argument promotions
4319 will prevent such mismatches. */
4320
1efe6448 4321 size = GET_MODE_SIZE (arg->mode);
51bbfa0c
RS
4322 /* Compute how much space the push instruction will push.
4323 On many machines, pushing a byte will advance the stack
4324 pointer by a halfword. */
4325#ifdef PUSH_ROUNDING
4326 size = PUSH_ROUNDING (size);
4327#endif
4328 used = size;
4329
4330 /* Compute how much space the argument should get:
4331 round up to a multiple of the alignment for arguments. */
1efe6448 4332 if (none != FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)))
51bbfa0c
RS
4333 used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
4334 / (PARM_BOUNDARY / BITS_PER_UNIT))
4335 * (PARM_BOUNDARY / BITS_PER_UNIT));
4336
4337 /* This isn't already where we want it on the stack, so put it there.
4338 This can either be done with push or copy insns. */
d329e058 4339 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), NULL_RTX,
44bb111a 4340 PARM_BOUNDARY, partial, reg, used - size, argblock,
e7949876
AM
4341 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4342 ARGS_SIZE_RTX (arg->locate.alignment_pad));
841404cd
AO
4343
4344 /* Unless this is a partially-in-register argument, the argument is now
4345 in the stack. */
4346 if (partial == 0)
4347 arg->value = arg->stack;
51bbfa0c
RS
4348 }
4349 else
4350 {
4351 /* BLKmode, at least partly to be pushed. */
4352
1b1f20ca 4353 unsigned int parm_align;
b3694847 4354 int excess;
51bbfa0c
RS
4355 rtx size_rtx;
4356
4357 /* Pushing a nonscalar.
4358 If part is passed in registers, PARTIAL says how much
4359 and emit_push_insn will take care of putting it there. */
4360
4361 /* Round its size up to a multiple
4362 of the allocation unit for arguments. */
4363
e7949876 4364 if (arg->locate.size.var != 0)
51bbfa0c
RS
4365 {
4366 excess = 0;
e7949876 4367 size_rtx = ARGS_SIZE_RTX (arg->locate.size);
51bbfa0c
RS
4368 }
4369 else
4370 {
51bbfa0c
RS
4371 /* PUSH_ROUNDING has no effect on us, because
4372 emit_push_insn for BLKmode is careful to avoid it. */
531547e9
FJ
4373 if (reg && GET_CODE (reg) == PARALLEL)
4374 {
4375 /* Use the size of the elt to compute excess. */
4376 rtx elt = XEXP (XVECEXP (reg, 0, 0), 0);
4377 excess = (arg->locate.size.constant
4378 - int_size_in_bytes (TREE_TYPE (pval))
4379 + partial * GET_MODE_SIZE (GET_MODE (elt)));
4380 }
4381 else
4382 excess = (arg->locate.size.constant
4383 - int_size_in_bytes (TREE_TYPE (pval))
4384 + partial * UNITS_PER_WORD);
db4c55f6
JM
4385 size_rtx = expand_expr (size_in_bytes (TREE_TYPE (pval)),
4386 NULL_RTX, TYPE_MODE (sizetype), 0);
51bbfa0c
RS
4387 }
4388
1b1f20ca
RH
4389 /* Some types will require stricter alignment, which will be
4390 provided for elsewhere in argument layout. */
4391 parm_align = MAX (PARM_BOUNDARY, TYPE_ALIGN (TREE_TYPE (pval)));
4392
4393 /* When an argument is padded down, the block is aligned to
4394 PARM_BOUNDARY, but the actual argument isn't. */
4395 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
4396 {
e7949876 4397 if (arg->locate.size.var)
1b1f20ca
RH
4398 parm_align = BITS_PER_UNIT;
4399 else if (excess)
4400 {
97d05bfd 4401 unsigned int excess_align = (excess & -excess) * BITS_PER_UNIT;
1b1f20ca
RH
4402 parm_align = MIN (parm_align, excess_align);
4403 }
4404 }
4405
3c0cb5de 4406 if ((flags & ECF_SIBCALL) && MEM_P (arg->value))
4c6b3b2a
JJ
4407 {
4408 /* emit_push_insn might not work properly if arg->value and
e7949876 4409 argblock + arg->locate.offset areas overlap. */
4c6b3b2a
JJ
4410 rtx x = arg->value;
4411 int i = 0;
4412
4413 if (XEXP (x, 0) == current_function_internal_arg_pointer
4414 || (GET_CODE (XEXP (x, 0)) == PLUS
4415 && XEXP (XEXP (x, 0), 0) ==
4416 current_function_internal_arg_pointer
4417 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4418 {
4419 if (XEXP (x, 0) != current_function_internal_arg_pointer)
4420 i = INTVAL (XEXP (XEXP (x, 0), 1));
4421
e0a21ab9 4422 /* expand_call should ensure this. */
e7949876 4423 if (arg->locate.offset.var || GET_CODE (size_rtx) != CONST_INT)
4c6b3b2a
JJ
4424 abort ();
4425
e7949876 4426 if (arg->locate.offset.constant > i)
4c6b3b2a 4427 {
e7949876 4428 if (arg->locate.offset.constant < i + INTVAL (size_rtx))
4c6b3b2a
JJ
4429 sibcall_failure = 1;
4430 }
e7949876 4431 else if (arg->locate.offset.constant < i)
4c6b3b2a 4432 {
e7949876 4433 if (i < arg->locate.offset.constant + INTVAL (size_rtx))
4c6b3b2a
JJ
4434 sibcall_failure = 1;
4435 }
4436 }
4437 }
4438
1efe6448 4439 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
1b1f20ca 4440 parm_align, partial, reg, excess, argblock,
e7949876
AM
4441 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4442 ARGS_SIZE_RTX (arg->locate.alignment_pad));
51bbfa0c 4443
841404cd
AO
4444 /* Unless this is a partially-in-register argument, the argument is now
4445 in the stack.
51bbfa0c 4446
841404cd
AO
4447 ??? Unlike the case above, in which we want the actual
4448 address of the data, so that we can load it directly into a
4449 register, here we want the address of the stack slot, so that
4450 it's properly aligned for word-by-word copying or something
4451 like that. It's not clear that this is always correct. */
4452 if (partial == 0)
4453 arg->value = arg->stack_slot;
4454 }
51bbfa0c 4455
8403445a
AM
4456 /* Mark all slots this store used. */
4457 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
4458 && argblock && ! variable_size && arg->stack)
4459 for (i = lower_bound; i < upper_bound; i++)
4460 stack_usage_map[i] = 1;
4461
51bbfa0c
RS
4462 /* Once we have pushed something, pops can't safely
4463 be deferred during the rest of the arguments. */
4464 NO_DEFER_POP;
4465
4466 /* ANSI doesn't require a sequence point here,
4467 but PCC has one, so this will avoid some problems. */
4468 emit_queue ();
4469
db907e7b
RK
4470 /* Free any temporary slots made in processing this argument. Show
4471 that we might have taken the address of something and pushed that
4472 as an operand. */
4473 preserve_temp_slots (NULL_RTX);
51bbfa0c 4474 free_temp_slots ();
cc79451b 4475 pop_temp_slots ();
4c6b3b2a
JJ
4476
4477 return sibcall_failure;
51bbfa0c 4478}
a4b1b92a 4479
a4b1b92a
RH
4480/* Nonzero if we do not know how to pass TYPE solely in registers.
4481 We cannot do so in the following cases:
4482
4483 - if the type has variable size
4484 - if the type is marked as addressable (it is required to be constructed
4485 into the stack)
4486 - if the padding and mode of the type is such that a copy into a register
4487 would put it into the wrong part of the register.
4488
4489 Which padding can't be supported depends on the byte endianness.
4490
4491 A value in a register is implicitly padded at the most significant end.
4492 On a big-endian machine, that is the lower end in memory.
4493 So a value padded in memory at the upper end can't go in a register.
4494 For a little-endian machine, the reverse is true. */
4495
4496bool
d329e058 4497default_must_pass_in_stack (enum machine_mode mode, tree type)
a4b1b92a
RH
4498{
4499 if (!type)
40cdfd5a 4500 return false;
a4b1b92a
RH
4501
4502 /* If the type has variable size... */
4503 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4504 return true;
4505
4506 /* If the type is marked as addressable (it is required
4507 to be constructed into the stack)... */
4508 if (TREE_ADDRESSABLE (type))
4509 return true;
4510
4511 /* If the padding and mode of the type is such that a copy into
4512 a register would put it into the wrong part of the register. */
4513 if (mode == BLKmode
4514 && int_size_in_bytes (type) % (PARM_BOUNDARY / BITS_PER_UNIT)
4515 && (FUNCTION_ARG_PADDING (mode, type)
4516 == (BYTES_BIG_ENDIAN ? upward : downward)))
4517 return true;
4518
4519 return false;
4520}