* configure.ac: Don't test for [build] __cxa_atexit when building a
[official-gcc.git] / gcc / calls.c
blob09c24d7c27b2cb2930f1f4392cccc9aa1439136f
1 /* Convert function calls to rtl insns, for GNU C compiler.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "flags.h"
29 #include "expr.h"
30 #include "optabs.h"
31 #include "libfuncs.h"
32 #include "function.h"
33 #include "regs.h"
34 #include "toplev.h"
35 #include "output.h"
36 #include "tm_p.h"
37 #include "timevar.h"
38 #include "sbitmap.h"
39 #include "langhooks.h"
40 #include "target.h"
41 #include "cgraph.h"
42 #include "except.h"
44 /* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits. */
45 #define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
47 /* Data structure and subroutines used within expand_call. */
49 struct arg_data
51 /* Tree node for this argument. */
52 tree tree_value;
53 /* Mode for value; TYPE_MODE unless promoted. */
54 enum machine_mode mode;
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
60 PARALLEL if the arg is to be copied into multiple non-contiguous
61 registers. */
62 rtx reg;
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;
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;
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;
73 /* Nonzero if argument must be passed on stack.
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. */
77 int pass_on_stack;
78 /* Some fields packaged up for locate_and_pad_parm. */
79 struct locate_and_pad_arg_data locate;
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;
87 /* Place that this stack area has been saved, if needed. */
88 rtx save_area;
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;
97 /* A vector of one char per byte of stack space. A byte if nonzero if
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. */
101 static char *stack_usage_map;
103 /* Size of STACK_USAGE_MAP. */
104 static int highest_outgoing_arg_in_use;
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. */
111 static sbitmap stored_args_map;
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. */
118 int stack_arg_under_construction;
120 static void emit_call_1 (rtx, tree, tree, tree, HOST_WIDE_INT, HOST_WIDE_INT,
121 HOST_WIDE_INT, rtx, rtx, int, rtx, int,
122 CUMULATIVE_ARGS *);
123 static void precompute_register_parameters (int, struct arg_data *, int *);
124 static int store_one_arg (struct arg_data *, rtx, int, int, int);
125 static void store_unaligned_arguments_into_pseudos (struct arg_data *, int);
126 static int finalize_must_preallocate (int, int, struct arg_data *,
127 struct args_size *);
128 static void precompute_arguments (int, int, struct arg_data *);
129 static int compute_argument_block_size (int, struct args_size *, int);
130 static void initialize_argument_information (int, struct arg_data *,
131 struct args_size *, int, tree,
132 tree, CUMULATIVE_ARGS *, int,
133 rtx *, int *, int *, int *,
134 bool *, bool);
135 static void compute_argument_addresses (struct arg_data *, rtx, int);
136 static rtx rtx_for_function_call (tree, tree);
137 static void load_register_parameters (struct arg_data *, int, rtx *, int,
138 int, int *);
139 static rtx emit_library_call_value_1 (int, rtx, rtx, enum libcall_type,
140 enum machine_mode, int, va_list);
141 static int special_function_p (tree, int);
142 static int check_sibcall_argument_overlap_1 (rtx);
143 static int check_sibcall_argument_overlap (rtx, struct arg_data *, int);
145 static int combine_pending_stack_adjustment_and_call (int, struct args_size *,
146 unsigned int);
147 static bool shift_returned_value (tree, rtx *);
149 #ifdef REG_PARM_STACK_SPACE
150 static rtx save_fixed_argument_area (int, rtx, int *, int *);
151 static void restore_fixed_argument_area (rtx, rtx, int, int);
152 #endif
154 /* Force FUNEXP into a form suitable for the address of a CALL,
155 and return that as an rtx. Also load the static chain register
156 if FNDECL is a nested function.
158 CALL_FUSAGE points to a variable holding the prospective
159 CALL_INSN_FUNCTION_USAGE information. */
162 prepare_call_address (rtx funexp, rtx static_chain_value,
163 rtx *call_fusage, int reg_parm_seen, int sibcallp)
165 /* Make a valid memory address and copy constants through pseudo-regs,
166 but not for a constant address if -fno-function-cse. */
167 if (GET_CODE (funexp) != SYMBOL_REF)
168 /* If we are using registers for parameters, force the
169 function address into a register now. */
170 funexp = ((SMALL_REGISTER_CLASSES && reg_parm_seen)
171 ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
172 : memory_address (FUNCTION_MODE, funexp));
173 else if (! sibcallp)
175 #ifndef NO_FUNCTION_CSE
176 if (optimize && ! flag_no_function_cse)
177 funexp = force_reg (Pmode, funexp);
178 #endif
181 if (static_chain_value != 0)
183 static_chain_value = convert_memory_address (Pmode, static_chain_value);
184 emit_move_insn (static_chain_rtx, static_chain_value);
186 if (REG_P (static_chain_rtx))
187 use_reg (call_fusage, static_chain_rtx);
190 return funexp;
193 /* Generate instructions to call function FUNEXP,
194 and optionally pop the results.
195 The CALL_INSN is the first insn generated.
197 FNDECL is the declaration node of the function. This is given to the
198 macro RETURN_POPS_ARGS to determine whether this function pops its own args.
200 FUNTYPE is the data type of the function. This is given to the macro
201 RETURN_POPS_ARGS to determine whether this function pops its own args.
202 We used to allow an identifier for library functions, but that doesn't
203 work when the return type is an aggregate type and the calling convention
204 says that the pointer to this aggregate is to be popped by the callee.
206 STACK_SIZE is the number of bytes of arguments on the stack,
207 ROUNDED_STACK_SIZE is that number rounded up to
208 PREFERRED_STACK_BOUNDARY; zero if the size is variable. This is
209 both to put into the call insn and to generate explicit popping
210 code if necessary.
212 STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
213 It is zero if this call doesn't want a structure value.
215 NEXT_ARG_REG is the rtx that results from executing
216 FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1)
217 just after all the args have had their registers assigned.
218 This could be whatever you like, but normally it is the first
219 arg-register beyond those used for args in this call,
220 or 0 if all the arg-registers are used in this call.
221 It is passed on to `gen_call' so you can put this info in the call insn.
223 VALREG is a hard register in which a value is returned,
224 or 0 if the call does not return a value.
226 OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
227 the args to this call were processed.
228 We restore `inhibit_defer_pop' to that value.
230 CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
231 denote registers used by the called function. */
233 static void
234 emit_call_1 (rtx funexp, tree fntree, tree fndecl ATTRIBUTE_UNUSED,
235 tree funtype ATTRIBUTE_UNUSED,
236 HOST_WIDE_INT stack_size ATTRIBUTE_UNUSED,
237 HOST_WIDE_INT rounded_stack_size,
238 HOST_WIDE_INT struct_value_size ATTRIBUTE_UNUSED,
239 rtx next_arg_reg ATTRIBUTE_UNUSED, rtx valreg,
240 int old_inhibit_defer_pop, rtx call_fusage, int ecf_flags,
241 CUMULATIVE_ARGS *args_so_far ATTRIBUTE_UNUSED)
243 rtx rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
244 rtx call_insn;
245 int already_popped = 0;
246 HOST_WIDE_INT n_popped = RETURN_POPS_ARGS (fndecl, funtype, stack_size);
247 #if defined (HAVE_call) && defined (HAVE_call_value)
248 rtx struct_value_size_rtx;
249 struct_value_size_rtx = GEN_INT (struct_value_size);
250 #endif
252 #ifdef CALL_POPS_ARGS
253 n_popped += CALL_POPS_ARGS (* args_so_far);
254 #endif
256 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
257 and we don't want to load it into a register as an optimization,
258 because prepare_call_address already did it if it should be done. */
259 if (GET_CODE (funexp) != SYMBOL_REF)
260 funexp = memory_address (FUNCTION_MODE, funexp);
262 #if defined (HAVE_sibcall_pop) && defined (HAVE_sibcall_value_pop)
263 if ((ecf_flags & ECF_SIBCALL)
264 && HAVE_sibcall_pop && HAVE_sibcall_value_pop
265 && (n_popped > 0 || stack_size == 0))
267 rtx n_pop = GEN_INT (n_popped);
268 rtx pat;
270 /* If this subroutine pops its own args, record that in the call insn
271 if possible, for the sake of frame pointer elimination. */
273 if (valreg)
274 pat = GEN_SIBCALL_VALUE_POP (valreg,
275 gen_rtx_MEM (FUNCTION_MODE, funexp),
276 rounded_stack_size_rtx, next_arg_reg,
277 n_pop);
278 else
279 pat = GEN_SIBCALL_POP (gen_rtx_MEM (FUNCTION_MODE, funexp),
280 rounded_stack_size_rtx, next_arg_reg, n_pop);
282 emit_call_insn (pat);
283 already_popped = 1;
285 else
286 #endif
288 #if defined (HAVE_call_pop) && defined (HAVE_call_value_pop)
289 /* If the target has "call" or "call_value" insns, then prefer them
290 if no arguments are actually popped. If the target does not have
291 "call" or "call_value" insns, then we must use the popping versions
292 even if the call has no arguments to pop. */
293 #if defined (HAVE_call) && defined (HAVE_call_value)
294 if (HAVE_call && HAVE_call_value && HAVE_call_pop && HAVE_call_value_pop
295 && n_popped > 0 && ! (ecf_flags & ECF_SP_DEPRESSED))
296 #else
297 if (HAVE_call_pop && HAVE_call_value_pop)
298 #endif
300 rtx n_pop = GEN_INT (n_popped);
301 rtx pat;
303 /* If this subroutine pops its own args, record that in the call insn
304 if possible, for the sake of frame pointer elimination. */
306 if (valreg)
307 pat = GEN_CALL_VALUE_POP (valreg,
308 gen_rtx_MEM (FUNCTION_MODE, funexp),
309 rounded_stack_size_rtx, next_arg_reg, n_pop);
310 else
311 pat = GEN_CALL_POP (gen_rtx_MEM (FUNCTION_MODE, funexp),
312 rounded_stack_size_rtx, next_arg_reg, n_pop);
314 emit_call_insn (pat);
315 already_popped = 1;
317 else
318 #endif
320 #if defined (HAVE_sibcall) && defined (HAVE_sibcall_value)
321 if ((ecf_flags & ECF_SIBCALL)
322 && HAVE_sibcall && HAVE_sibcall_value)
324 if (valreg)
325 emit_call_insn (GEN_SIBCALL_VALUE (valreg,
326 gen_rtx_MEM (FUNCTION_MODE, funexp),
327 rounded_stack_size_rtx,
328 next_arg_reg, NULL_RTX));
329 else
330 emit_call_insn (GEN_SIBCALL (gen_rtx_MEM (FUNCTION_MODE, funexp),
331 rounded_stack_size_rtx, next_arg_reg,
332 struct_value_size_rtx));
334 else
335 #endif
337 #if defined (HAVE_call) && defined (HAVE_call_value)
338 if (HAVE_call && HAVE_call_value)
340 if (valreg)
341 emit_call_insn (GEN_CALL_VALUE (valreg,
342 gen_rtx_MEM (FUNCTION_MODE, funexp),
343 rounded_stack_size_rtx, next_arg_reg,
344 NULL_RTX));
345 else
346 emit_call_insn (GEN_CALL (gen_rtx_MEM (FUNCTION_MODE, funexp),
347 rounded_stack_size_rtx, next_arg_reg,
348 struct_value_size_rtx));
350 else
351 #endif
352 gcc_unreachable ();
354 /* Find the call we just emitted. */
355 call_insn = last_call_insn ();
357 /* Mark memory as used for "pure" function call. */
358 if (ecf_flags & ECF_PURE)
359 call_fusage
360 = gen_rtx_EXPR_LIST
361 (VOIDmode,
362 gen_rtx_USE (VOIDmode,
363 gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode))),
364 call_fusage);
366 /* Put the register usage information there. */
367 add_function_usage_to (call_insn, call_fusage);
369 /* If this is a const call, then set the insn's unchanging bit. */
370 if (ecf_flags & (ECF_CONST | ECF_PURE))
371 CONST_OR_PURE_CALL_P (call_insn) = 1;
373 /* If this call can't throw, attach a REG_EH_REGION reg note to that
374 effect. */
375 if (ecf_flags & ECF_NOTHROW)
376 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, const0_rtx,
377 REG_NOTES (call_insn));
378 else
380 int rn = lookup_stmt_eh_region (fntree);
382 /* If rn < 0, then either (1) tree-ssa not used or (2) doesn't
383 throw, which we already took care of. */
384 if (rn > 0)
385 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, GEN_INT (rn),
386 REG_NOTES (call_insn));
387 note_current_region_may_contain_throw ();
390 if (ecf_flags & ECF_NORETURN)
391 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_NORETURN, const0_rtx,
392 REG_NOTES (call_insn));
393 if (ecf_flags & ECF_ALWAYS_RETURN)
394 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_ALWAYS_RETURN, const0_rtx,
395 REG_NOTES (call_insn));
397 if (ecf_flags & ECF_RETURNS_TWICE)
399 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_SETJMP, const0_rtx,
400 REG_NOTES (call_insn));
401 current_function_calls_setjmp = 1;
404 SIBLING_CALL_P (call_insn) = ((ecf_flags & ECF_SIBCALL) != 0);
406 /* Restore this now, so that we do defer pops for this call's args
407 if the context of the call as a whole permits. */
408 inhibit_defer_pop = old_inhibit_defer_pop;
410 if (n_popped > 0)
412 if (!already_popped)
413 CALL_INSN_FUNCTION_USAGE (call_insn)
414 = gen_rtx_EXPR_LIST (VOIDmode,
415 gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx),
416 CALL_INSN_FUNCTION_USAGE (call_insn));
417 rounded_stack_size -= n_popped;
418 rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
419 stack_pointer_delta -= n_popped;
422 if (!ACCUMULATE_OUTGOING_ARGS)
424 /* If returning from the subroutine does not automatically pop the args,
425 we need an instruction to pop them sooner or later.
426 Perhaps do it now; perhaps just record how much space to pop later.
428 If returning from the subroutine does pop the args, indicate that the
429 stack pointer will be changed. */
431 if (rounded_stack_size != 0)
433 if (ecf_flags & (ECF_SP_DEPRESSED | ECF_NORETURN | ECF_LONGJMP))
434 /* Just pretend we did the pop. */
435 stack_pointer_delta -= rounded_stack_size;
436 else if (flag_defer_pop && inhibit_defer_pop == 0
437 && ! (ecf_flags & (ECF_CONST | ECF_PURE)))
438 pending_stack_adjust += rounded_stack_size;
439 else
440 adjust_stack (rounded_stack_size_rtx);
443 /* When we accumulate outgoing args, we must avoid any stack manipulations.
444 Restore the stack pointer to its original value now. Usually
445 ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
446 On i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
447 popping variants of functions exist as well.
449 ??? We may optimize similar to defer_pop above, but it is
450 probably not worthwhile.
452 ??? It will be worthwhile to enable combine_stack_adjustments even for
453 such machines. */
454 else if (n_popped)
455 anti_adjust_stack (GEN_INT (n_popped));
458 /* Determine if the function identified by NAME and FNDECL is one with
459 special properties we wish to know about.
461 For example, if the function might return more than one time (setjmp), then
462 set RETURNS_TWICE to a nonzero value.
464 Similarly set LONGJMP for if the function is in the longjmp family.
466 Set MAY_BE_ALLOCA for any memory allocation function that might allocate
467 space from the stack such as alloca. */
469 static int
470 special_function_p (tree fndecl, int flags)
472 if (fndecl && DECL_NAME (fndecl)
473 && IDENTIFIER_LENGTH (DECL_NAME (fndecl)) <= 17
474 /* Exclude functions not at the file scope, or not `extern',
475 since they are not the magic functions we would otherwise
476 think they are.
477 FIXME: this should be handled with attributes, not with this
478 hacky imitation of DECL_ASSEMBLER_NAME. It's (also) wrong
479 because you can declare fork() inside a function if you
480 wish. */
481 && (DECL_CONTEXT (fndecl) == NULL_TREE
482 || TREE_CODE (DECL_CONTEXT (fndecl)) == TRANSLATION_UNIT_DECL)
483 && TREE_PUBLIC (fndecl))
485 const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
486 const char *tname = name;
488 /* We assume that alloca will always be called by name. It
489 makes no sense to pass it as a pointer-to-function to
490 anything that does not understand its behavior. */
491 if (((IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 6
492 && name[0] == 'a'
493 && ! strcmp (name, "alloca"))
494 || (IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 16
495 && name[0] == '_'
496 && ! strcmp (name, "__builtin_alloca"))))
497 flags |= ECF_MAY_BE_ALLOCA;
499 /* Disregard prefix _, __ or __x. */
500 if (name[0] == '_')
502 if (name[1] == '_' && name[2] == 'x')
503 tname += 3;
504 else if (name[1] == '_')
505 tname += 2;
506 else
507 tname += 1;
510 if (tname[0] == 's')
512 if ((tname[1] == 'e'
513 && (! strcmp (tname, "setjmp")
514 || ! strcmp (tname, "setjmp_syscall")))
515 || (tname[1] == 'i'
516 && ! strcmp (tname, "sigsetjmp"))
517 || (tname[1] == 'a'
518 && ! strcmp (tname, "savectx")))
519 flags |= ECF_RETURNS_TWICE;
521 if (tname[1] == 'i'
522 && ! strcmp (tname, "siglongjmp"))
523 flags |= ECF_LONGJMP;
525 else if ((tname[0] == 'q' && tname[1] == 's'
526 && ! strcmp (tname, "qsetjmp"))
527 || (tname[0] == 'v' && tname[1] == 'f'
528 && ! strcmp (tname, "vfork")))
529 flags |= ECF_RETURNS_TWICE;
531 else if (tname[0] == 'l' && tname[1] == 'o'
532 && ! strcmp (tname, "longjmp"))
533 flags |= ECF_LONGJMP;
536 return flags;
539 /* Return nonzero when tree represent call to longjmp. */
542 setjmp_call_p (tree fndecl)
544 return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
547 /* Return true when exp contains alloca call. */
548 bool
549 alloca_call_p (tree exp)
551 if (TREE_CODE (exp) == CALL_EXPR
552 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
553 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
554 == FUNCTION_DECL)
555 && (special_function_p (TREE_OPERAND (TREE_OPERAND (exp, 0), 0),
556 0) & ECF_MAY_BE_ALLOCA))
557 return true;
558 return false;
561 /* Detect flags (function attributes) from the function decl or type node. */
564 flags_from_decl_or_type (tree exp)
566 int flags = 0;
567 tree type = exp;
569 if (DECL_P (exp))
571 struct cgraph_rtl_info *i = cgraph_rtl_info (exp);
572 type = TREE_TYPE (exp);
574 if (i)
576 if (i->pure_function)
577 flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
578 if (i->const_function)
579 flags |= ECF_CONST | ECF_LIBCALL_BLOCK;
582 /* The function exp may have the `malloc' attribute. */
583 if (DECL_IS_MALLOC (exp))
584 flags |= ECF_MALLOC;
586 /* The function exp may have the `pure' attribute. */
587 if (DECL_IS_PURE (exp))
588 flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
590 if (TREE_NOTHROW (exp))
591 flags |= ECF_NOTHROW;
593 if (TREE_READONLY (exp) && ! TREE_THIS_VOLATILE (exp))
594 flags |= ECF_LIBCALL_BLOCK | ECF_CONST;
596 flags = special_function_p (exp, flags);
598 else if (TYPE_P (exp) && TYPE_READONLY (exp) && ! TREE_THIS_VOLATILE (exp))
599 flags |= ECF_CONST;
601 if (TREE_THIS_VOLATILE (exp))
602 flags |= ECF_NORETURN;
604 /* Mark if the function returns with the stack pointer depressed. We
605 cannot consider it pure or constant in that case. */
606 if (TREE_CODE (type) == FUNCTION_TYPE && TYPE_RETURNS_STACK_DEPRESSED (type))
608 flags |= ECF_SP_DEPRESSED;
609 flags &= ~(ECF_PURE | ECF_CONST | ECF_LIBCALL_BLOCK);
612 return flags;
615 /* Detect flags from a CALL_EXPR. */
618 call_expr_flags (tree t)
620 int flags;
621 tree decl = get_callee_fndecl (t);
623 if (decl)
624 flags = flags_from_decl_or_type (decl);
625 else
627 t = TREE_TYPE (TREE_OPERAND (t, 0));
628 if (t && TREE_CODE (t) == POINTER_TYPE)
629 flags = flags_from_decl_or_type (TREE_TYPE (t));
630 else
631 flags = 0;
634 return flags;
637 /* Precompute all register parameters as described by ARGS, storing values
638 into fields within the ARGS array.
640 NUM_ACTUALS indicates the total number elements in the ARGS array.
642 Set REG_PARM_SEEN if we encounter a register parameter. */
644 static void
645 precompute_register_parameters (int num_actuals, struct arg_data *args, int *reg_parm_seen)
647 int i;
649 *reg_parm_seen = 0;
651 for (i = 0; i < num_actuals; i++)
652 if (args[i].reg != 0 && ! args[i].pass_on_stack)
654 *reg_parm_seen = 1;
656 if (args[i].value == 0)
658 push_temp_slots ();
659 args[i].value = expand_expr (args[i].tree_value, NULL_RTX,
660 VOIDmode, 0);
661 preserve_temp_slots (args[i].value);
662 pop_temp_slots ();
665 /* If the value is a non-legitimate constant, force it into a
666 pseudo now. TLS symbols sometimes need a call to resolve. */
667 if (CONSTANT_P (args[i].value)
668 && !LEGITIMATE_CONSTANT_P (args[i].value))
669 args[i].value = force_reg (args[i].mode, args[i].value);
671 /* If we are to promote the function arg to a wider mode,
672 do it now. */
674 if (args[i].mode != TYPE_MODE (TREE_TYPE (args[i].tree_value)))
675 args[i].value
676 = convert_modes (args[i].mode,
677 TYPE_MODE (TREE_TYPE (args[i].tree_value)),
678 args[i].value, args[i].unsignedp);
680 /* If the value is expensive, and we are inside an appropriately
681 short loop, put the value into a pseudo and then put the pseudo
682 into the hard reg.
684 For small register classes, also do this if this call uses
685 register parameters. This is to avoid reload conflicts while
686 loading the parameters registers. */
688 if ((! (REG_P (args[i].value)
689 || (GET_CODE (args[i].value) == SUBREG
690 && REG_P (SUBREG_REG (args[i].value)))))
691 && args[i].mode != BLKmode
692 && rtx_cost (args[i].value, SET) > COSTS_N_INSNS (1)
693 && ((SMALL_REGISTER_CLASSES && *reg_parm_seen)
694 || optimize))
695 args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
699 #ifdef REG_PARM_STACK_SPACE
701 /* The argument list is the property of the called routine and it
702 may clobber it. If the fixed area has been used for previous
703 parameters, we must save and restore it. */
705 static rtx
706 save_fixed_argument_area (int reg_parm_stack_space, rtx argblock, int *low_to_save, int *high_to_save)
708 int low;
709 int high;
711 /* Compute the boundary of the area that needs to be saved, if any. */
712 high = reg_parm_stack_space;
713 #ifdef ARGS_GROW_DOWNWARD
714 high += 1;
715 #endif
716 if (high > highest_outgoing_arg_in_use)
717 high = highest_outgoing_arg_in_use;
719 for (low = 0; low < high; low++)
720 if (stack_usage_map[low] != 0)
722 int num_to_save;
723 enum machine_mode save_mode;
724 int delta;
725 rtx stack_area;
726 rtx save_area;
728 while (stack_usage_map[--high] == 0)
731 *low_to_save = low;
732 *high_to_save = high;
734 num_to_save = high - low + 1;
735 save_mode = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
737 /* If we don't have the required alignment, must do this
738 in BLKmode. */
739 if ((low & (MIN (GET_MODE_SIZE (save_mode),
740 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
741 save_mode = BLKmode;
743 #ifdef ARGS_GROW_DOWNWARD
744 delta = -high;
745 #else
746 delta = low;
747 #endif
748 stack_area = gen_rtx_MEM (save_mode,
749 memory_address (save_mode,
750 plus_constant (argblock,
751 delta)));
753 set_mem_align (stack_area, PARM_BOUNDARY);
754 if (save_mode == BLKmode)
756 save_area = assign_stack_temp (BLKmode, num_to_save, 0);
757 emit_block_move (validize_mem (save_area), stack_area,
758 GEN_INT (num_to_save), BLOCK_OP_CALL_PARM);
760 else
762 save_area = gen_reg_rtx (save_mode);
763 emit_move_insn (save_area, stack_area);
766 return save_area;
769 return NULL_RTX;
772 static void
773 restore_fixed_argument_area (rtx save_area, rtx argblock, int high_to_save, int low_to_save)
775 enum machine_mode save_mode = GET_MODE (save_area);
776 int delta;
777 rtx stack_area;
779 #ifdef ARGS_GROW_DOWNWARD
780 delta = -high_to_save;
781 #else
782 delta = low_to_save;
783 #endif
784 stack_area = gen_rtx_MEM (save_mode,
785 memory_address (save_mode,
786 plus_constant (argblock, delta)));
787 set_mem_align (stack_area, PARM_BOUNDARY);
789 if (save_mode != BLKmode)
790 emit_move_insn (stack_area, save_area);
791 else
792 emit_block_move (stack_area, validize_mem (save_area),
793 GEN_INT (high_to_save - low_to_save + 1),
794 BLOCK_OP_CALL_PARM);
796 #endif /* REG_PARM_STACK_SPACE */
798 /* If any elements in ARGS refer to parameters that are to be passed in
799 registers, but not in memory, and whose alignment does not permit a
800 direct copy into registers. Copy the values into a group of pseudos
801 which we will later copy into the appropriate hard registers.
803 Pseudos for each unaligned argument will be stored into the array
804 args[argnum].aligned_regs. The caller is responsible for deallocating
805 the aligned_regs array if it is nonzero. */
807 static void
808 store_unaligned_arguments_into_pseudos (struct arg_data *args, int num_actuals)
810 int i, j;
812 for (i = 0; i < num_actuals; i++)
813 if (args[i].reg != 0 && ! args[i].pass_on_stack
814 && args[i].mode == BLKmode
815 && (TYPE_ALIGN (TREE_TYPE (args[i].tree_value))
816 < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
818 int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
819 int nregs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
820 int endian_correction = 0;
822 args[i].n_aligned_regs = args[i].partial ? args[i].partial : nregs;
823 args[i].aligned_regs = xmalloc (sizeof (rtx) * args[i].n_aligned_regs);
825 /* Structures smaller than a word are normally aligned to the
826 least significant byte. On a BYTES_BIG_ENDIAN machine,
827 this means we must skip the empty high order bytes when
828 calculating the bit offset. */
829 if (bytes < UNITS_PER_WORD
830 #ifdef BLOCK_REG_PADDING
831 && (BLOCK_REG_PADDING (args[i].mode,
832 TREE_TYPE (args[i].tree_value), 1)
833 == downward)
834 #else
835 && BYTES_BIG_ENDIAN
836 #endif
838 endian_correction = BITS_PER_WORD - bytes * BITS_PER_UNIT;
840 for (j = 0; j < args[i].n_aligned_regs; j++)
842 rtx reg = gen_reg_rtx (word_mode);
843 rtx word = operand_subword_force (args[i].value, j, BLKmode);
844 int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
846 args[i].aligned_regs[j] = reg;
847 word = extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
848 word_mode, word_mode);
850 /* There is no need to restrict this code to loading items
851 in TYPE_ALIGN sized hunks. The bitfield instructions can
852 load up entire word sized registers efficiently.
854 ??? This may not be needed anymore.
855 We use to emit a clobber here but that doesn't let later
856 passes optimize the instructions we emit. By storing 0 into
857 the register later passes know the first AND to zero out the
858 bitfield being set in the register is unnecessary. The store
859 of 0 will be deleted as will at least the first AND. */
861 emit_move_insn (reg, const0_rtx);
863 bytes -= bitsize / BITS_PER_UNIT;
864 store_bit_field (reg, bitsize, endian_correction, word_mode,
865 word);
870 /* Fill in ARGS_SIZE and ARGS array based on the parameters found in
871 ACTPARMS.
873 NUM_ACTUALS is the total number of parameters.
875 N_NAMED_ARGS is the total number of named arguments.
877 FNDECL is the tree code for the target of this call (if known)
879 ARGS_SO_FAR holds state needed by the target to know where to place
880 the next argument.
882 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
883 for arguments which are passed in registers.
885 OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
886 and may be modified by this routine.
888 OLD_PENDING_ADJ, MUST_PREALLOCATE and FLAGS are pointers to integer
889 flags which may may be modified by this routine.
891 MAY_TAILCALL is cleared if we encounter an invisible pass-by-reference
892 that requires allocation of stack space.
894 CALL_FROM_THUNK_P is true if this call is the jump from a thunk to
895 the thunked-to function. */
897 static void
898 initialize_argument_information (int num_actuals ATTRIBUTE_UNUSED,
899 struct arg_data *args,
900 struct args_size *args_size,
901 int n_named_args ATTRIBUTE_UNUSED,
902 tree actparms, tree fndecl,
903 CUMULATIVE_ARGS *args_so_far,
904 int reg_parm_stack_space,
905 rtx *old_stack_level, int *old_pending_adj,
906 int *must_preallocate, int *ecf_flags,
907 bool *may_tailcall, bool call_from_thunk_p)
909 /* 1 if scanning parms front to back, -1 if scanning back to front. */
910 int inc;
912 /* Count arg position in order args appear. */
913 int argpos;
915 int i;
916 tree p;
918 args_size->constant = 0;
919 args_size->var = 0;
921 /* In this loop, we consider args in the order they are written.
922 We fill up ARGS from the front or from the back if necessary
923 so that in any case the first arg to be pushed ends up at the front. */
925 if (PUSH_ARGS_REVERSED)
927 i = num_actuals - 1, inc = -1;
928 /* In this case, must reverse order of args
929 so that we compute and push the last arg first. */
931 else
933 i = 0, inc = 1;
936 /* I counts args in order (to be) pushed; ARGPOS counts in order written. */
937 for (p = actparms, argpos = 0; p; p = TREE_CHAIN (p), i += inc, argpos++)
939 tree type = TREE_TYPE (TREE_VALUE (p));
940 int unsignedp;
941 enum machine_mode mode;
943 args[i].tree_value = TREE_VALUE (p);
945 /* Replace erroneous argument with constant zero. */
946 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
947 args[i].tree_value = integer_zero_node, type = integer_type_node;
949 /* If TYPE is a transparent union, pass things the way we would
950 pass the first field of the union. We have already verified that
951 the modes are the same. */
952 if (TREE_CODE (type) == UNION_TYPE && TYPE_TRANSPARENT_UNION (type))
953 type = TREE_TYPE (TYPE_FIELDS (type));
955 /* Decide where to pass this arg.
957 args[i].reg is nonzero if all or part is passed in registers.
959 args[i].partial is nonzero if part but not all is passed in registers,
960 and the exact value says how many words are passed in registers.
962 args[i].pass_on_stack is nonzero if the argument must at least be
963 computed on the stack. It may then be loaded back into registers
964 if args[i].reg is nonzero.
966 These decisions are driven by the FUNCTION_... macros and must agree
967 with those made by function.c. */
969 /* See if this argument should be passed by invisible reference. */
970 if (pass_by_reference (args_so_far, TYPE_MODE (type),
971 type, argpos < n_named_args))
973 bool callee_copies;
974 tree base;
976 callee_copies
977 = reference_callee_copied (args_so_far, TYPE_MODE (type),
978 type, argpos < n_named_args);
980 /* If we're compiling a thunk, pass through invisible references
981 instead of making a copy. */
982 if (call_from_thunk_p
983 || (callee_copies
984 && !TREE_ADDRESSABLE (type)
985 && (base = get_base_address (args[i].tree_value))
986 && (!DECL_P (base) || MEM_P (DECL_RTL (base)))))
988 /* We can't use sibcalls if a callee-copied argument is
989 stored in the current function's frame. */
990 if (!call_from_thunk_p && DECL_P (base) && !TREE_STATIC (base))
991 *may_tailcall = false;
993 args[i].tree_value = build_fold_addr_expr (args[i].tree_value);
994 type = TREE_TYPE (args[i].tree_value);
996 *ecf_flags &= ~(ECF_CONST | ECF_LIBCALL_BLOCK);
998 else
1000 /* We make a copy of the object and pass the address to the
1001 function being called. */
1002 rtx copy;
1004 if (!COMPLETE_TYPE_P (type)
1005 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
1006 || (flag_stack_check && ! STACK_CHECK_BUILTIN
1007 && (0 < compare_tree_int (TYPE_SIZE_UNIT (type),
1008 STACK_CHECK_MAX_VAR_SIZE))))
1010 /* This is a variable-sized object. Make space on the stack
1011 for it. */
1012 rtx size_rtx = expr_size (TREE_VALUE (p));
1014 if (*old_stack_level == 0)
1016 emit_stack_save (SAVE_BLOCK, old_stack_level, NULL_RTX);
1017 *old_pending_adj = pending_stack_adjust;
1018 pending_stack_adjust = 0;
1021 copy = gen_rtx_MEM (BLKmode,
1022 allocate_dynamic_stack_space
1023 (size_rtx, NULL_RTX, TYPE_ALIGN (type)));
1024 set_mem_attributes (copy, type, 1);
1026 else
1027 copy = assign_temp (type, 0, 1, 0);
1029 store_expr (args[i].tree_value, copy, 0);
1031 if (callee_copies)
1032 *ecf_flags &= ~(ECF_CONST | ECF_LIBCALL_BLOCK);
1033 else
1034 *ecf_flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
1036 args[i].tree_value
1037 = build_fold_addr_expr (make_tree (type, copy));
1038 type = TREE_TYPE (args[i].tree_value);
1039 *may_tailcall = false;
1043 mode = TYPE_MODE (type);
1044 unsignedp = TYPE_UNSIGNED (type);
1046 if (targetm.calls.promote_function_args (fndecl ? TREE_TYPE (fndecl) : 0))
1047 mode = promote_mode (type, mode, &unsignedp, 1);
1049 args[i].unsignedp = unsignedp;
1050 args[i].mode = mode;
1052 args[i].reg = FUNCTION_ARG (*args_so_far, mode, type,
1053 argpos < n_named_args);
1054 #ifdef FUNCTION_INCOMING_ARG
1055 /* If this is a sibling call and the machine has register windows, the
1056 register window has to be unwinded before calling the routine, so
1057 arguments have to go into the incoming registers. */
1058 args[i].tail_call_reg = FUNCTION_INCOMING_ARG (*args_so_far, mode, type,
1059 argpos < n_named_args);
1060 #else
1061 args[i].tail_call_reg = args[i].reg;
1062 #endif
1064 if (args[i].reg)
1065 args[i].partial
1066 = FUNCTION_ARG_PARTIAL_NREGS (*args_so_far, mode, type,
1067 argpos < n_named_args);
1069 args[i].pass_on_stack = targetm.calls.must_pass_in_stack (mode, type);
1071 /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
1072 it means that we are to pass this arg in the register(s) designated
1073 by the PARALLEL, but also to pass it in the stack. */
1074 if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
1075 && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
1076 args[i].pass_on_stack = 1;
1078 /* If this is an addressable type, we must preallocate the stack
1079 since we must evaluate the object into its final location.
1081 If this is to be passed in both registers and the stack, it is simpler
1082 to preallocate. */
1083 if (TREE_ADDRESSABLE (type)
1084 || (args[i].pass_on_stack && args[i].reg != 0))
1085 *must_preallocate = 1;
1087 /* If this is an addressable type, we cannot pre-evaluate it. Thus,
1088 we cannot consider this function call constant. */
1089 if (TREE_ADDRESSABLE (type))
1090 *ecf_flags &= ~ECF_LIBCALL_BLOCK;
1092 /* Compute the stack-size of this argument. */
1093 if (args[i].reg == 0 || args[i].partial != 0
1094 || reg_parm_stack_space > 0
1095 || args[i].pass_on_stack)
1096 locate_and_pad_parm (mode, type,
1097 #ifdef STACK_PARMS_IN_REG_PARM_AREA
1099 #else
1100 args[i].reg != 0,
1101 #endif
1102 args[i].pass_on_stack ? 0 : args[i].partial,
1103 fndecl, args_size, &args[i].locate);
1104 #ifdef BLOCK_REG_PADDING
1105 else
1106 /* The argument is passed entirely in registers. See at which
1107 end it should be padded. */
1108 args[i].locate.where_pad =
1109 BLOCK_REG_PADDING (mode, type,
1110 int_size_in_bytes (type) <= UNITS_PER_WORD);
1111 #endif
1113 /* Update ARGS_SIZE, the total stack space for args so far. */
1115 args_size->constant += args[i].locate.size.constant;
1116 if (args[i].locate.size.var)
1117 ADD_PARM_SIZE (*args_size, args[i].locate.size.var);
1119 /* Increment ARGS_SO_FAR, which has info about which arg-registers
1120 have been used, etc. */
1122 FUNCTION_ARG_ADVANCE (*args_so_far, TYPE_MODE (type), type,
1123 argpos < n_named_args);
1127 /* Update ARGS_SIZE to contain the total size for the argument block.
1128 Return the original constant component of the argument block's size.
1130 REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
1131 for arguments passed in registers. */
1133 static int
1134 compute_argument_block_size (int reg_parm_stack_space,
1135 struct args_size *args_size,
1136 int preferred_stack_boundary ATTRIBUTE_UNUSED)
1138 int unadjusted_args_size = args_size->constant;
1140 /* For accumulate outgoing args mode we don't need to align, since the frame
1141 will be already aligned. Align to STACK_BOUNDARY in order to prevent
1142 backends from generating misaligned frame sizes. */
1143 if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
1144 preferred_stack_boundary = STACK_BOUNDARY;
1146 /* Compute the actual size of the argument block required. The variable
1147 and constant sizes must be combined, the size may have to be rounded,
1148 and there may be a minimum required size. */
1150 if (args_size->var)
1152 args_size->var = ARGS_SIZE_TREE (*args_size);
1153 args_size->constant = 0;
1155 preferred_stack_boundary /= BITS_PER_UNIT;
1156 if (preferred_stack_boundary > 1)
1158 /* We don't handle this case yet. To handle it correctly we have
1159 to add the delta, round and subtract the delta.
1160 Currently no machine description requires this support. */
1161 gcc_assert (!(stack_pointer_delta & (preferred_stack_boundary - 1)));
1162 args_size->var = round_up (args_size->var, preferred_stack_boundary);
1165 if (reg_parm_stack_space > 0)
1167 args_size->var
1168 = size_binop (MAX_EXPR, args_size->var,
1169 ssize_int (reg_parm_stack_space));
1171 #ifndef OUTGOING_REG_PARM_STACK_SPACE
1172 /* The area corresponding to register parameters is not to count in
1173 the size of the block we need. So make the adjustment. */
1174 args_size->var
1175 = size_binop (MINUS_EXPR, args_size->var,
1176 ssize_int (reg_parm_stack_space));
1177 #endif
1180 else
1182 preferred_stack_boundary /= BITS_PER_UNIT;
1183 if (preferred_stack_boundary < 1)
1184 preferred_stack_boundary = 1;
1185 args_size->constant = (((args_size->constant
1186 + stack_pointer_delta
1187 + preferred_stack_boundary - 1)
1188 / preferred_stack_boundary
1189 * preferred_stack_boundary)
1190 - stack_pointer_delta);
1192 args_size->constant = MAX (args_size->constant,
1193 reg_parm_stack_space);
1195 #ifndef OUTGOING_REG_PARM_STACK_SPACE
1196 args_size->constant -= reg_parm_stack_space;
1197 #endif
1199 return unadjusted_args_size;
1202 /* Precompute parameters as needed for a function call.
1204 FLAGS is mask of ECF_* constants.
1206 NUM_ACTUALS is the number of arguments.
1208 ARGS is an array containing information for each argument; this
1209 routine fills in the INITIAL_VALUE and VALUE fields for each
1210 precomputed argument. */
1212 static void
1213 precompute_arguments (int flags, int num_actuals, struct arg_data *args)
1215 int i;
1217 /* If this is a libcall, then precompute all arguments so that we do not
1218 get extraneous instructions emitted as part of the libcall sequence. */
1219 if ((flags & ECF_LIBCALL_BLOCK) == 0)
1220 return;
1222 for (i = 0; i < num_actuals; i++)
1224 enum machine_mode mode;
1226 /* If this is an addressable type, we cannot pre-evaluate it. */
1227 gcc_assert (!TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value)));
1229 args[i].initial_value = args[i].value
1230 = expand_expr (args[i].tree_value, NULL_RTX, VOIDmode, 0);
1232 mode = TYPE_MODE (TREE_TYPE (args[i].tree_value));
1233 if (mode != args[i].mode)
1235 args[i].value
1236 = convert_modes (args[i].mode, mode,
1237 args[i].value, args[i].unsignedp);
1238 #if defined(PROMOTE_FUNCTION_MODE) && !defined(PROMOTE_MODE)
1239 /* CSE will replace this only if it contains args[i].value
1240 pseudo, so convert it down to the declared mode using
1241 a SUBREG. */
1242 if (REG_P (args[i].value)
1243 && GET_MODE_CLASS (args[i].mode) == MODE_INT)
1245 args[i].initial_value
1246 = gen_lowpart_SUBREG (mode, args[i].value);
1247 SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1248 SUBREG_PROMOTED_UNSIGNED_SET (args[i].initial_value,
1249 args[i].unsignedp);
1251 #endif
1256 /* Given the current state of MUST_PREALLOCATE and information about
1257 arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
1258 compute and return the final value for MUST_PREALLOCATE. */
1260 static int
1261 finalize_must_preallocate (int must_preallocate, int num_actuals, struct arg_data *args, struct args_size *args_size)
1263 /* See if we have or want to preallocate stack space.
1265 If we would have to push a partially-in-regs parm
1266 before other stack parms, preallocate stack space instead.
1268 If the size of some parm is not a multiple of the required stack
1269 alignment, we must preallocate.
1271 If the total size of arguments that would otherwise create a copy in
1272 a temporary (such as a CALL) is more than half the total argument list
1273 size, preallocation is faster.
1275 Another reason to preallocate is if we have a machine (like the m88k)
1276 where stack alignment is required to be maintained between every
1277 pair of insns, not just when the call is made. However, we assume here
1278 that such machines either do not have push insns (and hence preallocation
1279 would occur anyway) or the problem is taken care of with
1280 PUSH_ROUNDING. */
1282 if (! must_preallocate)
1284 int partial_seen = 0;
1285 int copy_to_evaluate_size = 0;
1286 int i;
1288 for (i = 0; i < num_actuals && ! must_preallocate; i++)
1290 if (args[i].partial > 0 && ! args[i].pass_on_stack)
1291 partial_seen = 1;
1292 else if (partial_seen && args[i].reg == 0)
1293 must_preallocate = 1;
1295 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1296 && (TREE_CODE (args[i].tree_value) == CALL_EXPR
1297 || TREE_CODE (args[i].tree_value) == TARGET_EXPR
1298 || TREE_CODE (args[i].tree_value) == COND_EXPR
1299 || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
1300 copy_to_evaluate_size
1301 += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1304 if (copy_to_evaluate_size * 2 >= args_size->constant
1305 && args_size->constant > 0)
1306 must_preallocate = 1;
1308 return must_preallocate;
1311 /* If we preallocated stack space, compute the address of each argument
1312 and store it into the ARGS array.
1314 We need not ensure it is a valid memory address here; it will be
1315 validized when it is used.
1317 ARGBLOCK is an rtx for the address of the outgoing arguments. */
1319 static void
1320 compute_argument_addresses (struct arg_data *args, rtx argblock, int num_actuals)
1322 if (argblock)
1324 rtx arg_reg = argblock;
1325 int i, arg_offset = 0;
1327 if (GET_CODE (argblock) == PLUS)
1328 arg_reg = XEXP (argblock, 0), arg_offset = INTVAL (XEXP (argblock, 1));
1330 for (i = 0; i < num_actuals; i++)
1332 rtx offset = ARGS_SIZE_RTX (args[i].locate.offset);
1333 rtx slot_offset = ARGS_SIZE_RTX (args[i].locate.slot_offset);
1334 rtx addr;
1336 /* Skip this parm if it will not be passed on the stack. */
1337 if (! args[i].pass_on_stack && args[i].reg != 0)
1338 continue;
1340 if (GET_CODE (offset) == CONST_INT)
1341 addr = plus_constant (arg_reg, INTVAL (offset));
1342 else
1343 addr = gen_rtx_PLUS (Pmode, arg_reg, offset);
1345 addr = plus_constant (addr, arg_offset);
1346 args[i].stack = gen_rtx_MEM (args[i].mode, addr);
1347 set_mem_align (args[i].stack, PARM_BOUNDARY);
1348 set_mem_attributes (args[i].stack,
1349 TREE_TYPE (args[i].tree_value), 1);
1351 if (GET_CODE (slot_offset) == CONST_INT)
1352 addr = plus_constant (arg_reg, INTVAL (slot_offset));
1353 else
1354 addr = gen_rtx_PLUS (Pmode, arg_reg, slot_offset);
1356 addr = plus_constant (addr, arg_offset);
1357 args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
1358 set_mem_align (args[i].stack_slot, PARM_BOUNDARY);
1359 set_mem_attributes (args[i].stack_slot,
1360 TREE_TYPE (args[i].tree_value), 1);
1362 /* Function incoming arguments may overlap with sibling call
1363 outgoing arguments and we cannot allow reordering of reads
1364 from function arguments with stores to outgoing arguments
1365 of sibling calls. */
1366 set_mem_alias_set (args[i].stack, 0);
1367 set_mem_alias_set (args[i].stack_slot, 0);
1372 /* Given a FNDECL and EXP, return an rtx suitable for use as a target address
1373 in a call instruction.
1375 FNDECL is the tree node for the target function. For an indirect call
1376 FNDECL will be NULL_TREE.
1378 ADDR is the operand 0 of CALL_EXPR for this call. */
1380 static rtx
1381 rtx_for_function_call (tree fndecl, tree addr)
1383 rtx funexp;
1385 /* Get the function to call, in the form of RTL. */
1386 if (fndecl)
1388 /* If this is the first use of the function, see if we need to
1389 make an external definition for it. */
1390 if (! TREE_USED (fndecl))
1392 assemble_external (fndecl);
1393 TREE_USED (fndecl) = 1;
1396 /* Get a SYMBOL_REF rtx for the function address. */
1397 funexp = XEXP (DECL_RTL (fndecl), 0);
1399 else
1400 /* Generate an rtx (probably a pseudo-register) for the address. */
1402 push_temp_slots ();
1403 funexp = expand_expr (addr, NULL_RTX, VOIDmode, 0);
1404 pop_temp_slots (); /* FUNEXP can't be BLKmode. */
1406 return funexp;
1409 /* Do the register loads required for any wholly-register parms or any
1410 parms which are passed both on the stack and in a register. Their
1411 expressions were already evaluated.
1413 Mark all register-parms as living through the call, putting these USE
1414 insns in the CALL_INSN_FUNCTION_USAGE field.
1416 When IS_SIBCALL, perform the check_sibcall_overlap_argument_overlap
1417 checking, setting *SIBCALL_FAILURE if appropriate. */
1419 static void
1420 load_register_parameters (struct arg_data *args, int num_actuals,
1421 rtx *call_fusage, int flags, int is_sibcall,
1422 int *sibcall_failure)
1424 int i, j;
1426 for (i = 0; i < num_actuals; i++)
1428 rtx reg = ((flags & ECF_SIBCALL)
1429 ? args[i].tail_call_reg : args[i].reg);
1430 if (reg)
1432 int partial = args[i].partial;
1433 int nregs;
1434 int size = 0;
1435 rtx before_arg = get_last_insn ();
1436 /* Set to non-negative if must move a word at a time, even if just
1437 one word (e.g, partial == 1 && mode == DFmode). Set to -1 if
1438 we just use a normal move insn. This value can be zero if the
1439 argument is a zero size structure with no fields. */
1440 nregs = -1;
1441 if (partial)
1442 nregs = partial;
1443 else if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode)
1445 size = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1446 nregs = (size + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
1448 else
1449 size = GET_MODE_SIZE (args[i].mode);
1451 /* Handle calls that pass values in multiple non-contiguous
1452 locations. The Irix 6 ABI has examples of this. */
1454 if (GET_CODE (reg) == PARALLEL)
1456 tree type = TREE_TYPE (args[i].tree_value);
1457 emit_group_load (reg, args[i].value, type,
1458 int_size_in_bytes (type));
1461 /* If simple case, just do move. If normal partial, store_one_arg
1462 has already loaded the register for us. In all other cases,
1463 load the register(s) from memory. */
1465 else if (nregs == -1)
1467 emit_move_insn (reg, args[i].value);
1468 #ifdef BLOCK_REG_PADDING
1469 /* Handle case where we have a value that needs shifting
1470 up to the msb. eg. a QImode value and we're padding
1471 upward on a BYTES_BIG_ENDIAN machine. */
1472 if (size < UNITS_PER_WORD
1473 && (args[i].locate.where_pad
1474 == (BYTES_BIG_ENDIAN ? upward : downward)))
1476 rtx x;
1477 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
1479 /* Assigning REG here rather than a temp makes CALL_FUSAGE
1480 report the whole reg as used. Strictly speaking, the
1481 call only uses SIZE bytes at the msb end, but it doesn't
1482 seem worth generating rtl to say that. */
1483 reg = gen_rtx_REG (word_mode, REGNO (reg));
1484 x = expand_shift (LSHIFT_EXPR, word_mode, reg,
1485 build_int_cst (NULL_TREE, shift),
1486 reg, 1);
1487 if (x != reg)
1488 emit_move_insn (reg, x);
1490 #endif
1493 /* If we have pre-computed the values to put in the registers in
1494 the case of non-aligned structures, copy them in now. */
1496 else if (args[i].n_aligned_regs != 0)
1497 for (j = 0; j < args[i].n_aligned_regs; j++)
1498 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
1499 args[i].aligned_regs[j]);
1501 else if (partial == 0 || args[i].pass_on_stack)
1503 rtx mem = validize_mem (args[i].value);
1505 /* Handle a BLKmode that needs shifting. */
1506 if (nregs == 1 && size < UNITS_PER_WORD
1507 #ifdef BLOCK_REG_PADDING
1508 && args[i].locate.where_pad == downward
1509 #else
1510 && BYTES_BIG_ENDIAN
1511 #endif
1514 rtx tem = operand_subword_force (mem, 0, args[i].mode);
1515 rtx ri = gen_rtx_REG (word_mode, REGNO (reg));
1516 rtx x = gen_reg_rtx (word_mode);
1517 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
1518 enum tree_code dir = BYTES_BIG_ENDIAN ? RSHIFT_EXPR
1519 : LSHIFT_EXPR;
1521 emit_move_insn (x, tem);
1522 x = expand_shift (dir, word_mode, x,
1523 build_int_cst (NULL_TREE, shift),
1524 ri, 1);
1525 if (x != ri)
1526 emit_move_insn (ri, x);
1528 else
1529 move_block_to_reg (REGNO (reg), mem, nregs, args[i].mode);
1532 /* When a parameter is a block, and perhaps in other cases, it is
1533 possible that it did a load from an argument slot that was
1534 already clobbered. */
1535 if (is_sibcall
1536 && check_sibcall_argument_overlap (before_arg, &args[i], 0))
1537 *sibcall_failure = 1;
1539 /* Handle calls that pass values in multiple non-contiguous
1540 locations. The Irix 6 ABI has examples of this. */
1541 if (GET_CODE (reg) == PARALLEL)
1542 use_group_regs (call_fusage, reg);
1543 else if (nregs == -1)
1544 use_reg (call_fusage, reg);
1545 else
1546 use_regs (call_fusage, REGNO (reg), nregs == 0 ? 1 : nregs);
1551 /* We need to pop PENDING_STACK_ADJUST bytes. But, if the arguments
1552 wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
1553 bytes, then we would need to push some additional bytes to pad the
1554 arguments. So, we compute an adjust to the stack pointer for an
1555 amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
1556 bytes. Then, when the arguments are pushed the stack will be perfectly
1557 aligned. ARGS_SIZE->CONSTANT is set to the number of bytes that should
1558 be popped after the call. Returns the adjustment. */
1560 static int
1561 combine_pending_stack_adjustment_and_call (int unadjusted_args_size,
1562 struct args_size *args_size,
1563 unsigned int preferred_unit_stack_boundary)
1565 /* The number of bytes to pop so that the stack will be
1566 under-aligned by UNADJUSTED_ARGS_SIZE bytes. */
1567 HOST_WIDE_INT adjustment;
1568 /* The alignment of the stack after the arguments are pushed, if we
1569 just pushed the arguments without adjust the stack here. */
1570 unsigned HOST_WIDE_INT unadjusted_alignment;
1572 unadjusted_alignment
1573 = ((stack_pointer_delta + unadjusted_args_size)
1574 % preferred_unit_stack_boundary);
1576 /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
1577 as possible -- leaving just enough left to cancel out the
1578 UNADJUSTED_ALIGNMENT. In other words, we want to ensure that the
1579 PENDING_STACK_ADJUST is non-negative, and congruent to
1580 -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY. */
1582 /* Begin by trying to pop all the bytes. */
1583 unadjusted_alignment
1584 = (unadjusted_alignment
1585 - (pending_stack_adjust % preferred_unit_stack_boundary));
1586 adjustment = pending_stack_adjust;
1587 /* Push enough additional bytes that the stack will be aligned
1588 after the arguments are pushed. */
1589 if (preferred_unit_stack_boundary > 1)
1591 if (unadjusted_alignment > 0)
1592 adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
1593 else
1594 adjustment += unadjusted_alignment;
1597 /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
1598 bytes after the call. The right number is the entire
1599 PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
1600 by the arguments in the first place. */
1601 args_size->constant
1602 = pending_stack_adjust - adjustment + unadjusted_args_size;
1604 return adjustment;
1607 /* Scan X expression if it does not dereference any argument slots
1608 we already clobbered by tail call arguments (as noted in stored_args_map
1609 bitmap).
1610 Return nonzero if X expression dereferences such argument slots,
1611 zero otherwise. */
1613 static int
1614 check_sibcall_argument_overlap_1 (rtx x)
1616 RTX_CODE code;
1617 int i, j;
1618 unsigned int k;
1619 const char *fmt;
1621 if (x == NULL_RTX)
1622 return 0;
1624 code = GET_CODE (x);
1626 if (code == MEM)
1628 if (XEXP (x, 0) == current_function_internal_arg_pointer)
1629 i = 0;
1630 else if (GET_CODE (XEXP (x, 0)) == PLUS
1631 && XEXP (XEXP (x, 0), 0) ==
1632 current_function_internal_arg_pointer
1633 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
1634 i = INTVAL (XEXP (XEXP (x, 0), 1));
1635 else
1636 return 1;
1638 #ifdef ARGS_GROW_DOWNWARD
1639 i = -i - GET_MODE_SIZE (GET_MODE (x));
1640 #endif
1642 for (k = 0; k < GET_MODE_SIZE (GET_MODE (x)); k++)
1643 if (i + k < stored_args_map->n_bits
1644 && TEST_BIT (stored_args_map, i + k))
1645 return 1;
1647 return 0;
1650 /* Scan all subexpressions. */
1651 fmt = GET_RTX_FORMAT (code);
1652 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
1654 if (*fmt == 'e')
1656 if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
1657 return 1;
1659 else if (*fmt == 'E')
1661 for (j = 0; j < XVECLEN (x, i); j++)
1662 if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
1663 return 1;
1666 return 0;
1669 /* Scan sequence after INSN if it does not dereference any argument slots
1670 we already clobbered by tail call arguments (as noted in stored_args_map
1671 bitmap). If MARK_STORED_ARGS_MAP, add stack slots for ARG to
1672 stored_args_map bitmap afterwards (when ARG is a register MARK_STORED_ARGS_MAP
1673 should be 0). Return nonzero if sequence after INSN dereferences such argument
1674 slots, zero otherwise. */
1676 static int
1677 check_sibcall_argument_overlap (rtx insn, struct arg_data *arg, int mark_stored_args_map)
1679 int low, high;
1681 if (insn == NULL_RTX)
1682 insn = get_insns ();
1683 else
1684 insn = NEXT_INSN (insn);
1686 for (; insn; insn = NEXT_INSN (insn))
1687 if (INSN_P (insn)
1688 && check_sibcall_argument_overlap_1 (PATTERN (insn)))
1689 break;
1691 if (mark_stored_args_map)
1693 #ifdef ARGS_GROW_DOWNWARD
1694 low = -arg->locate.slot_offset.constant - arg->locate.size.constant;
1695 #else
1696 low = arg->locate.slot_offset.constant;
1697 #endif
1699 for (high = low + arg->locate.size.constant; low < high; low++)
1700 SET_BIT (stored_args_map, low);
1702 return insn != NULL_RTX;
1705 /* If function value *VALUE was returned at the most significant end of a
1706 register, shift it towards the least significant end and convert it to
1707 TYPE's mode. Return true and update *VALUE if some action was needed.
1709 TYPE is the type of the function's return value, which is known not
1710 to have mode BLKmode. */
1712 static bool
1713 shift_returned_value (tree type, rtx *value)
1715 if (targetm.calls.return_in_msb (type))
1717 HOST_WIDE_INT shift;
1719 shift = (GET_MODE_BITSIZE (GET_MODE (*value))
1720 - BITS_PER_UNIT * int_size_in_bytes (type));
1721 if (shift > 0)
1723 /* Shift the value into the low part of the register. */
1724 *value = expand_binop (GET_MODE (*value), lshr_optab, *value,
1725 GEN_INT (shift), 0, 1, OPTAB_WIDEN);
1727 /* Truncate it to the type's mode, or its integer equivalent.
1728 This is subject to TRULY_NOOP_TRUNCATION. */
1729 *value = convert_to_mode (int_mode_for_mode (TYPE_MODE (type)),
1730 *value, 0);
1732 /* Now convert it to the final form. */
1733 *value = gen_lowpart (TYPE_MODE (type), *value);
1734 return true;
1737 return false;
1740 /* Remove all REG_EQUIV notes found in the insn chain. */
1742 static void
1743 purge_reg_equiv_notes (void)
1745 rtx insn;
1747 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1749 while (1)
1751 rtx note = find_reg_note (insn, REG_EQUIV, 0);
1752 if (note)
1754 /* Remove the note and keep looking at the notes for
1755 this insn. */
1756 remove_note (insn, note);
1757 continue;
1759 break;
1764 /* Generate all the code for a function call
1765 and return an rtx for its value.
1766 Store the value in TARGET (specified as an rtx) if convenient.
1767 If the value is stored in TARGET then TARGET is returned.
1768 If IGNORE is nonzero, then we ignore the value of the function call. */
1771 expand_call (tree exp, rtx target, int ignore)
1773 /* Nonzero if we are currently expanding a call. */
1774 static int currently_expanding_call = 0;
1776 /* List of actual parameters. */
1777 tree actparms = TREE_OPERAND (exp, 1);
1778 /* RTX for the function to be called. */
1779 rtx funexp;
1780 /* Sequence of insns to perform a normal "call". */
1781 rtx normal_call_insns = NULL_RTX;
1782 /* Sequence of insns to perform a tail "call". */
1783 rtx tail_call_insns = NULL_RTX;
1784 /* Data type of the function. */
1785 tree funtype;
1786 tree type_arg_types;
1787 /* Declaration of the function being called,
1788 or 0 if the function is computed (not known by name). */
1789 tree fndecl = 0;
1790 /* The type of the function being called. */
1791 tree fntype;
1792 bool try_tail_call = CALL_EXPR_TAILCALL (exp);
1793 int pass;
1795 /* Register in which non-BLKmode value will be returned,
1796 or 0 if no value or if value is BLKmode. */
1797 rtx valreg;
1798 /* Address where we should return a BLKmode value;
1799 0 if value not BLKmode. */
1800 rtx structure_value_addr = 0;
1801 /* Nonzero if that address is being passed by treating it as
1802 an extra, implicit first parameter. Otherwise,
1803 it is passed by being copied directly into struct_value_rtx. */
1804 int structure_value_addr_parm = 0;
1805 /* Size of aggregate value wanted, or zero if none wanted
1806 or if we are using the non-reentrant PCC calling convention
1807 or expecting the value in registers. */
1808 HOST_WIDE_INT struct_value_size = 0;
1809 /* Nonzero if called function returns an aggregate in memory PCC style,
1810 by returning the address of where to find it. */
1811 int pcc_struct_value = 0;
1812 rtx struct_value = 0;
1814 /* Number of actual parameters in this call, including struct value addr. */
1815 int num_actuals;
1816 /* Number of named args. Args after this are anonymous ones
1817 and they must all go on the stack. */
1818 int n_named_args;
1820 /* Vector of information about each argument.
1821 Arguments are numbered in the order they will be pushed,
1822 not the order they are written. */
1823 struct arg_data *args;
1825 /* Total size in bytes of all the stack-parms scanned so far. */
1826 struct args_size args_size;
1827 struct args_size adjusted_args_size;
1828 /* Size of arguments before any adjustments (such as rounding). */
1829 int unadjusted_args_size;
1830 /* Data on reg parms scanned so far. */
1831 CUMULATIVE_ARGS args_so_far;
1832 /* Nonzero if a reg parm has been scanned. */
1833 int reg_parm_seen;
1834 /* Nonzero if this is an indirect function call. */
1836 /* Nonzero if we must avoid push-insns in the args for this call.
1837 If stack space is allocated for register parameters, but not by the
1838 caller, then it is preallocated in the fixed part of the stack frame.
1839 So the entire argument block must then be preallocated (i.e., we
1840 ignore PUSH_ROUNDING in that case). */
1842 int must_preallocate = !PUSH_ARGS;
1844 /* Size of the stack reserved for parameter registers. */
1845 int reg_parm_stack_space = 0;
1847 /* Address of space preallocated for stack parms
1848 (on machines that lack push insns), or 0 if space not preallocated. */
1849 rtx argblock = 0;
1851 /* Mask of ECF_ flags. */
1852 int flags = 0;
1853 #ifdef REG_PARM_STACK_SPACE
1854 /* Define the boundary of the register parm stack space that needs to be
1855 saved, if any. */
1856 int low_to_save, high_to_save;
1857 rtx save_area = 0; /* Place that it is saved */
1858 #endif
1860 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
1861 char *initial_stack_usage_map = stack_usage_map;
1863 int old_stack_allocated;
1865 /* State variables to track stack modifications. */
1866 rtx old_stack_level = 0;
1867 int old_stack_arg_under_construction = 0;
1868 int old_pending_adj = 0;
1869 int old_inhibit_defer_pop = inhibit_defer_pop;
1871 /* Some stack pointer alterations we make are performed via
1872 allocate_dynamic_stack_space. This modifies the stack_pointer_delta,
1873 which we then also need to save/restore along the way. */
1874 int old_stack_pointer_delta = 0;
1876 rtx call_fusage;
1877 tree p = TREE_OPERAND (exp, 0);
1878 tree addr = TREE_OPERAND (exp, 0);
1879 int i;
1880 /* The alignment of the stack, in bits. */
1881 unsigned HOST_WIDE_INT preferred_stack_boundary;
1882 /* The alignment of the stack, in bytes. */
1883 unsigned HOST_WIDE_INT preferred_unit_stack_boundary;
1884 /* The static chain value to use for this call. */
1885 rtx static_chain_value;
1886 /* See if this is "nothrow" function call. */
1887 if (TREE_NOTHROW (exp))
1888 flags |= ECF_NOTHROW;
1890 /* See if we can find a DECL-node for the actual function, and get the
1891 function attributes (flags) from the function decl or type node. */
1892 fndecl = get_callee_fndecl (exp);
1893 if (fndecl)
1895 fntype = TREE_TYPE (fndecl);
1896 flags |= flags_from_decl_or_type (fndecl);
1898 else
1900 fntype = TREE_TYPE (TREE_TYPE (p));
1901 flags |= flags_from_decl_or_type (fntype);
1904 struct_value = targetm.calls.struct_value_rtx (fntype, 0);
1906 /* Warn if this value is an aggregate type,
1907 regardless of which calling convention we are using for it. */
1908 if (warn_aggregate_return && AGGREGATE_TYPE_P (TREE_TYPE (exp)))
1909 warning ("function call has aggregate value");
1911 /* If the result of a pure or const function call is ignored (or void),
1912 and none of its arguments are volatile, we can avoid expanding the
1913 call and just evaluate the arguments for side-effects. */
1914 if ((flags & (ECF_CONST | ECF_PURE))
1915 && (ignore || target == const0_rtx
1916 || TYPE_MODE (TREE_TYPE (exp)) == VOIDmode))
1918 bool volatilep = false;
1919 tree arg;
1921 for (arg = actparms; arg; arg = TREE_CHAIN (arg))
1922 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
1924 volatilep = true;
1925 break;
1928 if (! volatilep)
1930 for (arg = actparms; arg; arg = TREE_CHAIN (arg))
1931 expand_expr (TREE_VALUE (arg), const0_rtx,
1932 VOIDmode, EXPAND_NORMAL);
1933 return const0_rtx;
1937 #ifdef REG_PARM_STACK_SPACE
1938 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
1939 #endif
1941 #ifndef OUTGOING_REG_PARM_STACK_SPACE
1942 if (reg_parm_stack_space > 0 && PUSH_ARGS)
1943 must_preallocate = 1;
1944 #endif
1946 /* Set up a place to return a structure. */
1948 /* Cater to broken compilers. */
1949 if (aggregate_value_p (exp, fndecl))
1951 /* This call returns a big structure. */
1952 flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
1954 #ifdef PCC_STATIC_STRUCT_RETURN
1956 pcc_struct_value = 1;
1958 #else /* not PCC_STATIC_STRUCT_RETURN */
1960 struct_value_size = int_size_in_bytes (TREE_TYPE (exp));
1962 if (CALL_EXPR_HAS_RETURN_SLOT_ADDR (exp))
1964 /* The structure value address arg is already in actparms.
1965 Pull it out. It might be nice to just leave it there, but
1966 we need to set structure_value_addr. */
1967 tree return_arg = TREE_VALUE (actparms);
1968 actparms = TREE_CHAIN (actparms);
1969 structure_value_addr = expand_expr (return_arg, NULL_RTX,
1970 VOIDmode, EXPAND_NORMAL);
1972 else if (target && MEM_P (target))
1973 structure_value_addr = XEXP (target, 0);
1974 else
1976 /* For variable-sized objects, we must be called with a target
1977 specified. If we were to allocate space on the stack here,
1978 we would have no way of knowing when to free it. */
1979 rtx d = assign_temp (TREE_TYPE (exp), 1, 1, 1);
1981 mark_temp_addr_taken (d);
1982 structure_value_addr = XEXP (d, 0);
1983 target = 0;
1986 #endif /* not PCC_STATIC_STRUCT_RETURN */
1989 /* Figure out the amount to which the stack should be aligned. */
1990 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
1991 if (fndecl)
1993 struct cgraph_rtl_info *i = cgraph_rtl_info (fndecl);
1994 if (i && i->preferred_incoming_stack_boundary)
1995 preferred_stack_boundary = i->preferred_incoming_stack_boundary;
1998 /* Operand 0 is a pointer-to-function; get the type of the function. */
1999 funtype = TREE_TYPE (addr);
2000 gcc_assert (POINTER_TYPE_P (funtype));
2001 funtype = TREE_TYPE (funtype);
2003 /* Munge the tree to split complex arguments into their imaginary
2004 and real parts. */
2005 if (targetm.calls.split_complex_arg)
2007 type_arg_types = split_complex_types (TYPE_ARG_TYPES (funtype));
2008 actparms = split_complex_values (actparms);
2010 else
2011 type_arg_types = TYPE_ARG_TYPES (funtype);
2013 if (flags & ECF_MAY_BE_ALLOCA)
2014 current_function_calls_alloca = 1;
2016 /* If struct_value_rtx is 0, it means pass the address
2017 as if it were an extra parameter. */
2018 if (structure_value_addr && struct_value == 0)
2020 /* If structure_value_addr is a REG other than
2021 virtual_outgoing_args_rtx, we can use always use it. If it
2022 is not a REG, we must always copy it into a register.
2023 If it is virtual_outgoing_args_rtx, we must copy it to another
2024 register in some cases. */
2025 rtx temp = (!REG_P (structure_value_addr)
2026 || (ACCUMULATE_OUTGOING_ARGS
2027 && stack_arg_under_construction
2028 && structure_value_addr == virtual_outgoing_args_rtx)
2029 ? copy_addr_to_reg (convert_memory_address
2030 (Pmode, structure_value_addr))
2031 : structure_value_addr);
2033 actparms
2034 = tree_cons (error_mark_node,
2035 make_tree (build_pointer_type (TREE_TYPE (funtype)),
2036 temp),
2037 actparms);
2038 structure_value_addr_parm = 1;
2041 /* Count the arguments and set NUM_ACTUALS. */
2042 for (p = actparms, num_actuals = 0; p; p = TREE_CHAIN (p))
2043 num_actuals++;
2045 /* Compute number of named args.
2046 First, do a raw count of the args for INIT_CUMULATIVE_ARGS. */
2048 if (type_arg_types != 0)
2049 n_named_args
2050 = (list_length (type_arg_types)
2051 /* Count the struct value address, if it is passed as a parm. */
2052 + structure_value_addr_parm);
2053 else
2054 /* If we know nothing, treat all args as named. */
2055 n_named_args = num_actuals;
2057 /* Start updating where the next arg would go.
2059 On some machines (such as the PA) indirect calls have a different
2060 calling convention than normal calls. The fourth argument in
2061 INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
2062 or not. */
2063 INIT_CUMULATIVE_ARGS (args_so_far, funtype, NULL_RTX, fndecl, n_named_args);
2065 /* Now possibly adjust the number of named args.
2066 Normally, don't include the last named arg if anonymous args follow.
2067 We do include the last named arg if
2068 targetm.calls.strict_argument_naming() returns nonzero.
2069 (If no anonymous args follow, the result of list_length is actually
2070 one too large. This is harmless.)
2072 If targetm.calls.pretend_outgoing_varargs_named() returns
2073 nonzero, and targetm.calls.strict_argument_naming() returns zero,
2074 this machine will be able to place unnamed args that were passed
2075 in registers into the stack. So treat all args as named. This
2076 allows the insns emitting for a specific argument list to be
2077 independent of the function declaration.
2079 If targetm.calls.pretend_outgoing_varargs_named() returns zero,
2080 we do not have any reliable way to pass unnamed args in
2081 registers, so we must force them into memory. */
2083 if (type_arg_types != 0
2084 && targetm.calls.strict_argument_naming (&args_so_far))
2086 else if (type_arg_types != 0
2087 && ! targetm.calls.pretend_outgoing_varargs_named (&args_so_far))
2088 /* Don't include the last named arg. */
2089 --n_named_args;
2090 else
2091 /* Treat all args as named. */
2092 n_named_args = num_actuals;
2094 /* Make a vector to hold all the information about each arg. */
2095 args = alloca (num_actuals * sizeof (struct arg_data));
2096 memset (args, 0, num_actuals * sizeof (struct arg_data));
2098 /* Build up entries in the ARGS array, compute the size of the
2099 arguments into ARGS_SIZE, etc. */
2100 initialize_argument_information (num_actuals, args, &args_size,
2101 n_named_args, actparms, fndecl,
2102 &args_so_far, reg_parm_stack_space,
2103 &old_stack_level, &old_pending_adj,
2104 &must_preallocate, &flags,
2105 &try_tail_call, CALL_FROM_THUNK_P (exp));
2107 if (args_size.var)
2109 /* If this function requires a variable-sized argument list, don't
2110 try to make a cse'able block for this call. We may be able to
2111 do this eventually, but it is too complicated to keep track of
2112 what insns go in the cse'able block and which don't. */
2114 flags &= ~ECF_LIBCALL_BLOCK;
2115 must_preallocate = 1;
2118 /* Now make final decision about preallocating stack space. */
2119 must_preallocate = finalize_must_preallocate (must_preallocate,
2120 num_actuals, args,
2121 &args_size);
2123 /* If the structure value address will reference the stack pointer, we
2124 must stabilize it. We don't need to do this if we know that we are
2125 not going to adjust the stack pointer in processing this call. */
2127 if (structure_value_addr
2128 && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
2129 || reg_mentioned_p (virtual_outgoing_args_rtx,
2130 structure_value_addr))
2131 && (args_size.var
2132 || (!ACCUMULATE_OUTGOING_ARGS && args_size.constant)))
2133 structure_value_addr = copy_to_reg (structure_value_addr);
2135 /* Tail calls can make things harder to debug, and we've traditionally
2136 pushed these optimizations into -O2. Don't try if we're already
2137 expanding a call, as that means we're an argument. Don't try if
2138 there's cleanups, as we know there's code to follow the call. */
2140 if (currently_expanding_call++ != 0
2141 || !flag_optimize_sibling_calls
2142 || args_size.var
2143 || lookup_stmt_eh_region (exp) >= 0)
2144 try_tail_call = 0;
2146 /* Rest of purposes for tail call optimizations to fail. */
2147 if (
2148 #ifdef HAVE_sibcall_epilogue
2149 !HAVE_sibcall_epilogue
2150 #else
2152 #endif
2153 || !try_tail_call
2154 /* Doing sibling call optimization needs some work, since
2155 structure_value_addr can be allocated on the stack.
2156 It does not seem worth the effort since few optimizable
2157 sibling calls will return a structure. */
2158 || structure_value_addr != NULL_RTX
2159 /* Check whether the target is able to optimize the call
2160 into a sibcall. */
2161 || !targetm.function_ok_for_sibcall (fndecl, exp)
2162 /* Functions that do not return exactly once may not be sibcall
2163 optimized. */
2164 || (flags & (ECF_RETURNS_TWICE | ECF_LONGJMP | ECF_NORETURN))
2165 || TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (addr)))
2166 /* If the called function is nested in the current one, it might access
2167 some of the caller's arguments, but could clobber them beforehand if
2168 the argument areas are shared. */
2169 || (fndecl && decl_function_context (fndecl) == current_function_decl)
2170 /* If this function requires more stack slots than the current
2171 function, we cannot change it into a sibling call. */
2172 || args_size.constant > current_function_args_size
2173 /* If the callee pops its own arguments, then it must pop exactly
2174 the same number of arguments as the current function. */
2175 || (RETURN_POPS_ARGS (fndecl, funtype, args_size.constant)
2176 != RETURN_POPS_ARGS (current_function_decl,
2177 TREE_TYPE (current_function_decl),
2178 current_function_args_size))
2179 || !lang_hooks.decls.ok_for_sibcall (fndecl))
2180 try_tail_call = 0;
2182 /* Ensure current function's preferred stack boundary is at least
2183 what we need. We don't have to increase alignment for recursive
2184 functions. */
2185 if (cfun->preferred_stack_boundary < preferred_stack_boundary
2186 && fndecl != current_function_decl)
2187 cfun->preferred_stack_boundary = preferred_stack_boundary;
2188 if (fndecl == current_function_decl)
2189 cfun->recursive_call_emit = true;
2191 preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
2193 /* We want to make two insn chains; one for a sibling call, the other
2194 for a normal call. We will select one of the two chains after
2195 initial RTL generation is complete. */
2196 for (pass = try_tail_call ? 0 : 1; pass < 2; pass++)
2198 int sibcall_failure = 0;
2199 /* We want to emit any pending stack adjustments before the tail
2200 recursion "call". That way we know any adjustment after the tail
2201 recursion call can be ignored if we indeed use the tail
2202 call expansion. */
2203 int save_pending_stack_adjust = 0;
2204 int save_stack_pointer_delta = 0;
2205 rtx insns;
2206 rtx before_call, next_arg_reg;
2208 if (pass == 0)
2210 /* State variables we need to save and restore between
2211 iterations. */
2212 save_pending_stack_adjust = pending_stack_adjust;
2213 save_stack_pointer_delta = stack_pointer_delta;
2215 if (pass)
2216 flags &= ~ECF_SIBCALL;
2217 else
2218 flags |= ECF_SIBCALL;
2220 /* Other state variables that we must reinitialize each time
2221 through the loop (that are not initialized by the loop itself). */
2222 argblock = 0;
2223 call_fusage = 0;
2225 /* Start a new sequence for the normal call case.
2227 From this point on, if the sibling call fails, we want to set
2228 sibcall_failure instead of continuing the loop. */
2229 start_sequence ();
2231 /* Don't let pending stack adjusts add up to too much.
2232 Also, do all pending adjustments now if there is any chance
2233 this might be a call to alloca or if we are expanding a sibling
2234 call sequence or if we are calling a function that is to return
2235 with stack pointer depressed. */
2236 if (pending_stack_adjust >= 32
2237 || (pending_stack_adjust > 0
2238 && (flags & (ECF_MAY_BE_ALLOCA | ECF_SP_DEPRESSED)))
2239 || pass == 0)
2240 do_pending_stack_adjust ();
2242 /* When calling a const function, we must pop the stack args right away,
2243 so that the pop is deleted or moved with the call. */
2244 if (pass && (flags & ECF_LIBCALL_BLOCK))
2245 NO_DEFER_POP;
2247 /* Precompute any arguments as needed. */
2248 if (pass)
2249 precompute_arguments (flags, num_actuals, args);
2251 /* Now we are about to start emitting insns that can be deleted
2252 if a libcall is deleted. */
2253 if (pass && (flags & (ECF_LIBCALL_BLOCK | ECF_MALLOC)))
2254 start_sequence ();
2256 adjusted_args_size = args_size;
2257 /* Compute the actual size of the argument block required. The variable
2258 and constant sizes must be combined, the size may have to be rounded,
2259 and there may be a minimum required size. When generating a sibcall
2260 pattern, do not round up, since we'll be re-using whatever space our
2261 caller provided. */
2262 unadjusted_args_size
2263 = compute_argument_block_size (reg_parm_stack_space,
2264 &adjusted_args_size,
2265 (pass == 0 ? 0
2266 : preferred_stack_boundary));
2268 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
2270 /* The argument block when performing a sibling call is the
2271 incoming argument block. */
2272 if (pass == 0)
2274 argblock = virtual_incoming_args_rtx;
2275 argblock
2276 #ifdef STACK_GROWS_DOWNWARD
2277 = plus_constant (argblock, current_function_pretend_args_size);
2278 #else
2279 = plus_constant (argblock, -current_function_pretend_args_size);
2280 #endif
2281 stored_args_map = sbitmap_alloc (args_size.constant);
2282 sbitmap_zero (stored_args_map);
2285 /* If we have no actual push instructions, or shouldn't use them,
2286 make space for all args right now. */
2287 else if (adjusted_args_size.var != 0)
2289 if (old_stack_level == 0)
2291 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
2292 old_stack_pointer_delta = stack_pointer_delta;
2293 old_pending_adj = pending_stack_adjust;
2294 pending_stack_adjust = 0;
2295 /* stack_arg_under_construction says whether a stack arg is
2296 being constructed at the old stack level. Pushing the stack
2297 gets a clean outgoing argument block. */
2298 old_stack_arg_under_construction = stack_arg_under_construction;
2299 stack_arg_under_construction = 0;
2301 argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
2303 else
2305 /* Note that we must go through the motions of allocating an argument
2306 block even if the size is zero because we may be storing args
2307 in the area reserved for register arguments, which may be part of
2308 the stack frame. */
2310 int needed = adjusted_args_size.constant;
2312 /* Store the maximum argument space used. It will be pushed by
2313 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
2314 checking). */
2316 if (needed > current_function_outgoing_args_size)
2317 current_function_outgoing_args_size = needed;
2319 if (must_preallocate)
2321 if (ACCUMULATE_OUTGOING_ARGS)
2323 /* Since the stack pointer will never be pushed, it is
2324 possible for the evaluation of a parm to clobber
2325 something we have already written to the stack.
2326 Since most function calls on RISC machines do not use
2327 the stack, this is uncommon, but must work correctly.
2329 Therefore, we save any area of the stack that was already
2330 written and that we are using. Here we set up to do this
2331 by making a new stack usage map from the old one. The
2332 actual save will be done by store_one_arg.
2334 Another approach might be to try to reorder the argument
2335 evaluations to avoid this conflicting stack usage. */
2337 #ifndef OUTGOING_REG_PARM_STACK_SPACE
2338 /* Since we will be writing into the entire argument area,
2339 the map must be allocated for its entire size, not just
2340 the part that is the responsibility of the caller. */
2341 needed += reg_parm_stack_space;
2342 #endif
2344 #ifdef ARGS_GROW_DOWNWARD
2345 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2346 needed + 1);
2347 #else
2348 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2349 needed);
2350 #endif
2351 stack_usage_map = alloca (highest_outgoing_arg_in_use);
2353 if (initial_highest_arg_in_use)
2354 memcpy (stack_usage_map, initial_stack_usage_map,
2355 initial_highest_arg_in_use);
2357 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
2358 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
2359 (highest_outgoing_arg_in_use
2360 - initial_highest_arg_in_use));
2361 needed = 0;
2363 /* The address of the outgoing argument list must not be
2364 copied to a register here, because argblock would be left
2365 pointing to the wrong place after the call to
2366 allocate_dynamic_stack_space below. */
2368 argblock = virtual_outgoing_args_rtx;
2370 else
2372 if (inhibit_defer_pop == 0)
2374 /* Try to reuse some or all of the pending_stack_adjust
2375 to get this space. */
2376 needed
2377 = (combine_pending_stack_adjustment_and_call
2378 (unadjusted_args_size,
2379 &adjusted_args_size,
2380 preferred_unit_stack_boundary));
2382 /* combine_pending_stack_adjustment_and_call computes
2383 an adjustment before the arguments are allocated.
2384 Account for them and see whether or not the stack
2385 needs to go up or down. */
2386 needed = unadjusted_args_size - needed;
2388 if (needed < 0)
2390 /* We're releasing stack space. */
2391 /* ??? We can avoid any adjustment at all if we're
2392 already aligned. FIXME. */
2393 pending_stack_adjust = -needed;
2394 do_pending_stack_adjust ();
2395 needed = 0;
2397 else
2398 /* We need to allocate space. We'll do that in
2399 push_block below. */
2400 pending_stack_adjust = 0;
2403 /* Special case this because overhead of `push_block' in
2404 this case is non-trivial. */
2405 if (needed == 0)
2406 argblock = virtual_outgoing_args_rtx;
2407 else
2409 argblock = push_block (GEN_INT (needed), 0, 0);
2410 #ifdef ARGS_GROW_DOWNWARD
2411 argblock = plus_constant (argblock, needed);
2412 #endif
2415 /* We only really need to call `copy_to_reg' in the case
2416 where push insns are going to be used to pass ARGBLOCK
2417 to a function call in ARGS. In that case, the stack
2418 pointer changes value from the allocation point to the
2419 call point, and hence the value of
2420 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
2421 as well always do it. */
2422 argblock = copy_to_reg (argblock);
2427 if (ACCUMULATE_OUTGOING_ARGS)
2429 /* The save/restore code in store_one_arg handles all
2430 cases except one: a constructor call (including a C
2431 function returning a BLKmode struct) to initialize
2432 an argument. */
2433 if (stack_arg_under_construction)
2435 #ifndef OUTGOING_REG_PARM_STACK_SPACE
2436 rtx push_size = GEN_INT (reg_parm_stack_space
2437 + adjusted_args_size.constant);
2438 #else
2439 rtx push_size = GEN_INT (adjusted_args_size.constant);
2440 #endif
2441 if (old_stack_level == 0)
2443 emit_stack_save (SAVE_BLOCK, &old_stack_level,
2444 NULL_RTX);
2445 old_stack_pointer_delta = stack_pointer_delta;
2446 old_pending_adj = pending_stack_adjust;
2447 pending_stack_adjust = 0;
2448 /* stack_arg_under_construction says whether a stack
2449 arg is being constructed at the old stack level.
2450 Pushing the stack gets a clean outgoing argument
2451 block. */
2452 old_stack_arg_under_construction
2453 = stack_arg_under_construction;
2454 stack_arg_under_construction = 0;
2455 /* Make a new map for the new argument list. */
2456 stack_usage_map = alloca (highest_outgoing_arg_in_use);
2457 memset (stack_usage_map, 0, highest_outgoing_arg_in_use);
2458 highest_outgoing_arg_in_use = 0;
2460 allocate_dynamic_stack_space (push_size, NULL_RTX,
2461 BITS_PER_UNIT);
2464 /* If argument evaluation might modify the stack pointer,
2465 copy the address of the argument list to a register. */
2466 for (i = 0; i < num_actuals; i++)
2467 if (args[i].pass_on_stack)
2469 argblock = copy_addr_to_reg (argblock);
2470 break;
2474 compute_argument_addresses (args, argblock, num_actuals);
2476 /* If we push args individually in reverse order, perform stack alignment
2477 before the first push (the last arg). */
2478 if (PUSH_ARGS_REVERSED && argblock == 0
2479 && adjusted_args_size.constant != unadjusted_args_size)
2481 /* When the stack adjustment is pending, we get better code
2482 by combining the adjustments. */
2483 if (pending_stack_adjust
2484 && ! (flags & ECF_LIBCALL_BLOCK)
2485 && ! inhibit_defer_pop)
2487 pending_stack_adjust
2488 = (combine_pending_stack_adjustment_and_call
2489 (unadjusted_args_size,
2490 &adjusted_args_size,
2491 preferred_unit_stack_boundary));
2492 do_pending_stack_adjust ();
2494 else if (argblock == 0)
2495 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
2496 - unadjusted_args_size));
2498 /* Now that the stack is properly aligned, pops can't safely
2499 be deferred during the evaluation of the arguments. */
2500 NO_DEFER_POP;
2502 funexp = rtx_for_function_call (fndecl, addr);
2504 /* Figure out the register where the value, if any, will come back. */
2505 valreg = 0;
2506 if (TYPE_MODE (TREE_TYPE (exp)) != VOIDmode
2507 && ! structure_value_addr)
2509 if (pcc_struct_value)
2510 valreg = hard_function_value (build_pointer_type (TREE_TYPE (exp)),
2511 fndecl, (pass == 0));
2512 else
2513 valreg = hard_function_value (TREE_TYPE (exp), fndecl, (pass == 0));
2516 /* Precompute all register parameters. It isn't safe to compute anything
2517 once we have started filling any specific hard regs. */
2518 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
2520 if (TREE_OPERAND (exp, 2))
2521 static_chain_value = expand_expr (TREE_OPERAND (exp, 2),
2522 NULL_RTX, VOIDmode, 0);
2523 else
2524 static_chain_value = 0;
2526 #ifdef REG_PARM_STACK_SPACE
2527 /* Save the fixed argument area if it's part of the caller's frame and
2528 is clobbered by argument setup for this call. */
2529 if (ACCUMULATE_OUTGOING_ARGS && pass)
2530 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
2531 &low_to_save, &high_to_save);
2532 #endif
2534 /* Now store (and compute if necessary) all non-register parms.
2535 These come before register parms, since they can require block-moves,
2536 which could clobber the registers used for register parms.
2537 Parms which have partial registers are not stored here,
2538 but we do preallocate space here if they want that. */
2540 for (i = 0; i < num_actuals; i++)
2541 if (args[i].reg == 0 || args[i].pass_on_stack)
2543 rtx before_arg = get_last_insn ();
2545 if (store_one_arg (&args[i], argblock, flags,
2546 adjusted_args_size.var != 0,
2547 reg_parm_stack_space)
2548 || (pass == 0
2549 && check_sibcall_argument_overlap (before_arg,
2550 &args[i], 1)))
2551 sibcall_failure = 1;
2553 if (flags & ECF_CONST
2554 && args[i].stack
2555 && args[i].value == args[i].stack)
2556 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
2557 gen_rtx_USE (VOIDmode,
2558 args[i].value),
2559 call_fusage);
2562 /* If we have a parm that is passed in registers but not in memory
2563 and whose alignment does not permit a direct copy into registers,
2564 make a group of pseudos that correspond to each register that we
2565 will later fill. */
2566 if (STRICT_ALIGNMENT)
2567 store_unaligned_arguments_into_pseudos (args, num_actuals);
2569 /* Now store any partially-in-registers parm.
2570 This is the last place a block-move can happen. */
2571 if (reg_parm_seen)
2572 for (i = 0; i < num_actuals; i++)
2573 if (args[i].partial != 0 && ! args[i].pass_on_stack)
2575 rtx before_arg = get_last_insn ();
2577 if (store_one_arg (&args[i], argblock, flags,
2578 adjusted_args_size.var != 0,
2579 reg_parm_stack_space)
2580 || (pass == 0
2581 && check_sibcall_argument_overlap (before_arg,
2582 &args[i], 1)))
2583 sibcall_failure = 1;
2586 /* If we pushed args in forward order, perform stack alignment
2587 after pushing the last arg. */
2588 if (!PUSH_ARGS_REVERSED && argblock == 0)
2589 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
2590 - unadjusted_args_size));
2592 /* If register arguments require space on the stack and stack space
2593 was not preallocated, allocate stack space here for arguments
2594 passed in registers. */
2595 #ifdef OUTGOING_REG_PARM_STACK_SPACE
2596 if (!ACCUMULATE_OUTGOING_ARGS
2597 && must_preallocate == 0 && reg_parm_stack_space > 0)
2598 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
2599 #endif
2601 /* Pass the function the address in which to return a
2602 structure value. */
2603 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
2605 structure_value_addr
2606 = convert_memory_address (Pmode, structure_value_addr);
2607 emit_move_insn (struct_value,
2608 force_reg (Pmode,
2609 force_operand (structure_value_addr,
2610 NULL_RTX)));
2612 if (REG_P (struct_value))
2613 use_reg (&call_fusage, struct_value);
2616 funexp = prepare_call_address (funexp, static_chain_value,
2617 &call_fusage, reg_parm_seen, pass == 0);
2619 load_register_parameters (args, num_actuals, &call_fusage, flags,
2620 pass == 0, &sibcall_failure);
2622 /* Save a pointer to the last insn before the call, so that we can
2623 later safely search backwards to find the CALL_INSN. */
2624 before_call = get_last_insn ();
2626 /* Set up next argument register. For sibling calls on machines
2627 with register windows this should be the incoming register. */
2628 #ifdef FUNCTION_INCOMING_ARG
2629 if (pass == 0)
2630 next_arg_reg = FUNCTION_INCOMING_ARG (args_so_far, VOIDmode,
2631 void_type_node, 1);
2632 else
2633 #endif
2634 next_arg_reg = FUNCTION_ARG (args_so_far, VOIDmode,
2635 void_type_node, 1);
2637 /* All arguments and registers used for the call must be set up by
2638 now! */
2640 /* Stack must be properly aligned now. */
2641 gcc_assert (!pass
2642 || !(stack_pointer_delta % preferred_unit_stack_boundary));
2644 /* Generate the actual call instruction. */
2645 emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
2646 adjusted_args_size.constant, struct_value_size,
2647 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
2648 flags, & args_so_far);
2650 /* If call is cse'able, make appropriate pair of reg-notes around it.
2651 Test valreg so we don't crash; may safely ignore `const'
2652 if return type is void. Disable for PARALLEL return values, because
2653 we have no way to move such values into a pseudo register. */
2654 if (pass && (flags & ECF_LIBCALL_BLOCK))
2656 rtx insns;
2657 rtx insn;
2658 bool failed = valreg == 0 || GET_CODE (valreg) == PARALLEL;
2660 insns = get_insns ();
2662 /* Expansion of block moves possibly introduced a loop that may
2663 not appear inside libcall block. */
2664 for (insn = insns; insn; insn = NEXT_INSN (insn))
2665 if (JUMP_P (insn))
2666 failed = true;
2668 if (failed)
2670 end_sequence ();
2671 emit_insn (insns);
2673 else
2675 rtx note = 0;
2676 rtx temp = gen_reg_rtx (GET_MODE (valreg));
2678 /* Mark the return value as a pointer if needed. */
2679 if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
2680 mark_reg_pointer (temp,
2681 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp))));
2683 end_sequence ();
2684 if (flag_unsafe_math_optimizations
2685 && fndecl
2686 && DECL_BUILT_IN (fndecl)
2687 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRT
2688 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRTF
2689 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRTL))
2690 note = gen_rtx_fmt_e (SQRT,
2691 GET_MODE (temp),
2692 args[0].initial_value);
2693 else
2695 /* Construct an "equal form" for the value which
2696 mentions all the arguments in order as well as
2697 the function name. */
2698 for (i = 0; i < num_actuals; i++)
2699 note = gen_rtx_EXPR_LIST (VOIDmode,
2700 args[i].initial_value, note);
2701 note = gen_rtx_EXPR_LIST (VOIDmode, funexp, note);
2703 if (flags & ECF_PURE)
2704 note = gen_rtx_EXPR_LIST (VOIDmode,
2705 gen_rtx_USE (VOIDmode,
2706 gen_rtx_MEM (BLKmode,
2707 gen_rtx_SCRATCH (VOIDmode))),
2708 note);
2710 emit_libcall_block (insns, temp, valreg, note);
2712 valreg = temp;
2715 else if (pass && (flags & ECF_MALLOC))
2717 rtx temp = gen_reg_rtx (GET_MODE (valreg));
2718 rtx last, insns;
2720 /* The return value from a malloc-like function is a pointer. */
2721 if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
2722 mark_reg_pointer (temp, BIGGEST_ALIGNMENT);
2724 emit_move_insn (temp, valreg);
2726 /* The return value from a malloc-like function can not alias
2727 anything else. */
2728 last = get_last_insn ();
2729 REG_NOTES (last) =
2730 gen_rtx_EXPR_LIST (REG_NOALIAS, temp, REG_NOTES (last));
2732 /* Write out the sequence. */
2733 insns = get_insns ();
2734 end_sequence ();
2735 emit_insn (insns);
2736 valreg = temp;
2739 /* For calls to `setjmp', etc., inform flow.c it should complain
2740 if nonvolatile values are live. For functions that cannot return,
2741 inform flow that control does not fall through. */
2743 if ((flags & (ECF_NORETURN | ECF_LONGJMP)) || pass == 0)
2745 /* The barrier must be emitted
2746 immediately after the CALL_INSN. Some ports emit more
2747 than just a CALL_INSN above, so we must search for it here. */
2749 rtx last = get_last_insn ();
2750 while (!CALL_P (last))
2752 last = PREV_INSN (last);
2753 /* There was no CALL_INSN? */
2754 gcc_assert (last != before_call);
2757 emit_barrier_after (last);
2759 /* Stack adjustments after a noreturn call are dead code.
2760 However when NO_DEFER_POP is in effect, we must preserve
2761 stack_pointer_delta. */
2762 if (inhibit_defer_pop == 0)
2764 stack_pointer_delta = old_stack_allocated;
2765 pending_stack_adjust = 0;
2769 if (flags & ECF_LONGJMP)
2770 current_function_calls_longjmp = 1;
2772 /* If value type not void, return an rtx for the value. */
2774 if (TYPE_MODE (TREE_TYPE (exp)) == VOIDmode
2775 || ignore)
2776 target = const0_rtx;
2777 else if (structure_value_addr)
2779 if (target == 0 || !MEM_P (target))
2781 target
2782 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
2783 memory_address (TYPE_MODE (TREE_TYPE (exp)),
2784 structure_value_addr));
2785 set_mem_attributes (target, exp, 1);
2788 else if (pcc_struct_value)
2790 /* This is the special C++ case where we need to
2791 know what the true target was. We take care to
2792 never use this value more than once in one expression. */
2793 target = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
2794 copy_to_reg (valreg));
2795 set_mem_attributes (target, exp, 1);
2797 /* Handle calls that return values in multiple non-contiguous locations.
2798 The Irix 6 ABI has examples of this. */
2799 else if (GET_CODE (valreg) == PARALLEL)
2801 if (target == 0)
2803 /* This will only be assigned once, so it can be readonly. */
2804 tree nt = build_qualified_type (TREE_TYPE (exp),
2805 (TYPE_QUALS (TREE_TYPE (exp))
2806 | TYPE_QUAL_CONST));
2808 target = assign_temp (nt, 0, 1, 1);
2809 preserve_temp_slots (target);
2812 if (! rtx_equal_p (target, valreg))
2813 emit_group_store (target, valreg, TREE_TYPE (exp),
2814 int_size_in_bytes (TREE_TYPE (exp)));
2816 /* We can not support sibling calls for this case. */
2817 sibcall_failure = 1;
2819 else if (target
2820 && GET_MODE (target) == TYPE_MODE (TREE_TYPE (exp))
2821 && GET_MODE (target) == GET_MODE (valreg))
2823 /* TARGET and VALREG cannot be equal at this point because the
2824 latter would not have REG_FUNCTION_VALUE_P true, while the
2825 former would if it were referring to the same register.
2827 If they refer to the same register, this move will be a no-op,
2828 except when function inlining is being done. */
2829 emit_move_insn (target, valreg);
2831 /* If we are setting a MEM, this code must be executed. Since it is
2832 emitted after the call insn, sibcall optimization cannot be
2833 performed in that case. */
2834 if (MEM_P (target))
2835 sibcall_failure = 1;
2837 else if (TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
2839 target = copy_blkmode_from_reg (target, valreg, TREE_TYPE (exp));
2841 /* We can not support sibling calls for this case. */
2842 sibcall_failure = 1;
2844 else
2846 if (shift_returned_value (TREE_TYPE (exp), &valreg))
2847 sibcall_failure = 1;
2849 target = copy_to_reg (valreg);
2852 if (targetm.calls.promote_function_return(funtype))
2854 /* If we promoted this return value, make the proper SUBREG.
2855 TARGET might be const0_rtx here, so be careful. */
2856 if (REG_P (target)
2857 && TYPE_MODE (TREE_TYPE (exp)) != BLKmode
2858 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
2860 tree type = TREE_TYPE (exp);
2861 int unsignedp = TYPE_UNSIGNED (type);
2862 int offset = 0;
2863 enum machine_mode pmode;
2865 pmode = promote_mode (type, TYPE_MODE (type), &unsignedp, 1);
2866 /* If we don't promote as expected, something is wrong. */
2867 gcc_assert (GET_MODE (target) == pmode);
2869 if ((WORDS_BIG_ENDIAN || BYTES_BIG_ENDIAN)
2870 && (GET_MODE_SIZE (GET_MODE (target))
2871 > GET_MODE_SIZE (TYPE_MODE (type))))
2873 offset = GET_MODE_SIZE (GET_MODE (target))
2874 - GET_MODE_SIZE (TYPE_MODE (type));
2875 if (! BYTES_BIG_ENDIAN)
2876 offset = (offset / UNITS_PER_WORD) * UNITS_PER_WORD;
2877 else if (! WORDS_BIG_ENDIAN)
2878 offset %= UNITS_PER_WORD;
2880 target = gen_rtx_SUBREG (TYPE_MODE (type), target, offset);
2881 SUBREG_PROMOTED_VAR_P (target) = 1;
2882 SUBREG_PROMOTED_UNSIGNED_SET (target, unsignedp);
2886 /* If size of args is variable or this was a constructor call for a stack
2887 argument, restore saved stack-pointer value. */
2889 if (old_stack_level && ! (flags & ECF_SP_DEPRESSED))
2891 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
2892 stack_pointer_delta = old_stack_pointer_delta;
2893 pending_stack_adjust = old_pending_adj;
2894 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
2895 stack_arg_under_construction = old_stack_arg_under_construction;
2896 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
2897 stack_usage_map = initial_stack_usage_map;
2898 sibcall_failure = 1;
2900 else if (ACCUMULATE_OUTGOING_ARGS && pass)
2902 #ifdef REG_PARM_STACK_SPACE
2903 if (save_area)
2904 restore_fixed_argument_area (save_area, argblock,
2905 high_to_save, low_to_save);
2906 #endif
2908 /* If we saved any argument areas, restore them. */
2909 for (i = 0; i < num_actuals; i++)
2910 if (args[i].save_area)
2912 enum machine_mode save_mode = GET_MODE (args[i].save_area);
2913 rtx stack_area
2914 = gen_rtx_MEM (save_mode,
2915 memory_address (save_mode,
2916 XEXP (args[i].stack_slot, 0)));
2918 if (save_mode != BLKmode)
2919 emit_move_insn (stack_area, args[i].save_area);
2920 else
2921 emit_block_move (stack_area, args[i].save_area,
2922 GEN_INT (args[i].locate.size.constant),
2923 BLOCK_OP_CALL_PARM);
2926 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
2927 stack_usage_map = initial_stack_usage_map;
2930 /* If this was alloca, record the new stack level for nonlocal gotos.
2931 Check for the handler slots since we might not have a save area
2932 for non-local gotos. */
2934 if ((flags & ECF_MAY_BE_ALLOCA) && cfun->nonlocal_goto_save_area != 0)
2935 update_nonlocal_goto_save_area ();
2937 /* Free up storage we no longer need. */
2938 for (i = 0; i < num_actuals; ++i)
2939 if (args[i].aligned_regs)
2940 free (args[i].aligned_regs);
2942 insns = get_insns ();
2943 end_sequence ();
2945 if (pass == 0)
2947 tail_call_insns = insns;
2949 /* Restore the pending stack adjustment now that we have
2950 finished generating the sibling call sequence. */
2952 pending_stack_adjust = save_pending_stack_adjust;
2953 stack_pointer_delta = save_stack_pointer_delta;
2955 /* Prepare arg structure for next iteration. */
2956 for (i = 0; i < num_actuals; i++)
2958 args[i].value = 0;
2959 args[i].aligned_regs = 0;
2960 args[i].stack = 0;
2963 sbitmap_free (stored_args_map);
2965 else
2967 normal_call_insns = insns;
2969 /* Verify that we've deallocated all the stack we used. */
2970 gcc_assert ((flags & (ECF_NORETURN | ECF_LONGJMP))
2971 || (old_stack_allocated
2972 == stack_pointer_delta - pending_stack_adjust));
2975 /* If something prevents making this a sibling call,
2976 zero out the sequence. */
2977 if (sibcall_failure)
2978 tail_call_insns = NULL_RTX;
2979 else
2980 break;
2983 /* If tail call production succeeded, we need to remove REG_EQUIV notes on
2984 arguments too, as argument area is now clobbered by the call. */
2985 if (tail_call_insns)
2987 emit_insn (tail_call_insns);
2988 cfun->tail_call_emit = true;
2990 else
2991 emit_insn (normal_call_insns);
2993 currently_expanding_call--;
2995 /* If this function returns with the stack pointer depressed, ensure
2996 this block saves and restores the stack pointer, show it was
2997 changed, and adjust for any outgoing arg space. */
2998 if (flags & ECF_SP_DEPRESSED)
3000 clear_pending_stack_adjust ();
3001 emit_insn (gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx));
3002 emit_move_insn (virtual_stack_dynamic_rtx, stack_pointer_rtx);
3005 return target;
3008 /* A sibling call sequence invalidates any REG_EQUIV notes made for
3009 this function's incoming arguments.
3011 At the start of RTL generation we know the only REG_EQUIV notes
3012 in the rtl chain are those for incoming arguments, so we can safely
3013 flush any REG_EQUIV note.
3015 This is (slight) overkill. We could keep track of the highest
3016 argument we clobber and be more selective in removing notes, but it
3017 does not seem to be worth the effort. */
3018 void
3019 fixup_tail_calls (void)
3021 purge_reg_equiv_notes ();
3024 /* Traverse an argument list in VALUES and expand all complex
3025 arguments into their components. */
3026 tree
3027 split_complex_values (tree values)
3029 tree p;
3031 /* Before allocating memory, check for the common case of no complex. */
3032 for (p = values; p; p = TREE_CHAIN (p))
3034 tree type = TREE_TYPE (TREE_VALUE (p));
3035 if (type && TREE_CODE (type) == COMPLEX_TYPE
3036 && targetm.calls.split_complex_arg (type))
3037 goto found;
3039 return values;
3041 found:
3042 values = copy_list (values);
3044 for (p = values; p; p = TREE_CHAIN (p))
3046 tree complex_value = TREE_VALUE (p);
3047 tree complex_type;
3049 complex_type = TREE_TYPE (complex_value);
3050 if (!complex_type)
3051 continue;
3053 if (TREE_CODE (complex_type) == COMPLEX_TYPE
3054 && targetm.calls.split_complex_arg (complex_type))
3056 tree subtype;
3057 tree real, imag, next;
3059 subtype = TREE_TYPE (complex_type);
3060 complex_value = save_expr (complex_value);
3061 real = build1 (REALPART_EXPR, subtype, complex_value);
3062 imag = build1 (IMAGPART_EXPR, subtype, complex_value);
3064 TREE_VALUE (p) = real;
3065 next = TREE_CHAIN (p);
3066 imag = build_tree_list (NULL_TREE, imag);
3067 TREE_CHAIN (p) = imag;
3068 TREE_CHAIN (imag) = next;
3070 /* Skip the newly created node. */
3071 p = TREE_CHAIN (p);
3075 return values;
3078 /* Traverse a list of TYPES and expand all complex types into their
3079 components. */
3080 tree
3081 split_complex_types (tree types)
3083 tree p;
3085 /* Before allocating memory, check for the common case of no complex. */
3086 for (p = types; p; p = TREE_CHAIN (p))
3088 tree type = TREE_VALUE (p);
3089 if (TREE_CODE (type) == COMPLEX_TYPE
3090 && targetm.calls.split_complex_arg (type))
3091 goto found;
3093 return types;
3095 found:
3096 types = copy_list (types);
3098 for (p = types; p; p = TREE_CHAIN (p))
3100 tree complex_type = TREE_VALUE (p);
3102 if (TREE_CODE (complex_type) == COMPLEX_TYPE
3103 && targetm.calls.split_complex_arg (complex_type))
3105 tree next, imag;
3107 /* Rewrite complex type with component type. */
3108 TREE_VALUE (p) = TREE_TYPE (complex_type);
3109 next = TREE_CHAIN (p);
3111 /* Add another component type for the imaginary part. */
3112 imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
3113 TREE_CHAIN (p) = imag;
3114 TREE_CHAIN (imag) = next;
3116 /* Skip the newly created node. */
3117 p = TREE_CHAIN (p);
3121 return types;
3124 /* Output a library call to function FUN (a SYMBOL_REF rtx).
3125 The RETVAL parameter specifies whether return value needs to be saved, other
3126 parameters are documented in the emit_library_call function below. */
3128 static rtx
3129 emit_library_call_value_1 (int retval, rtx orgfun, rtx value,
3130 enum libcall_type fn_type,
3131 enum machine_mode outmode, int nargs, va_list p)
3133 /* Total size in bytes of all the stack-parms scanned so far. */
3134 struct args_size args_size;
3135 /* Size of arguments before any adjustments (such as rounding). */
3136 struct args_size original_args_size;
3137 int argnum;
3138 rtx fun;
3139 int inc;
3140 int count;
3141 rtx argblock = 0;
3142 CUMULATIVE_ARGS args_so_far;
3143 struct arg
3145 rtx value;
3146 enum machine_mode mode;
3147 rtx reg;
3148 int partial;
3149 struct locate_and_pad_arg_data locate;
3150 rtx save_area;
3152 struct arg *argvec;
3153 int old_inhibit_defer_pop = inhibit_defer_pop;
3154 rtx call_fusage = 0;
3155 rtx mem_value = 0;
3156 rtx valreg;
3157 int pcc_struct_value = 0;
3158 int struct_value_size = 0;
3159 int flags;
3160 int reg_parm_stack_space = 0;
3161 int needed;
3162 rtx before_call;
3163 tree tfom; /* type_for_mode (outmode, 0) */
3165 #ifdef REG_PARM_STACK_SPACE
3166 /* Define the boundary of the register parm stack space that needs to be
3167 save, if any. */
3168 int low_to_save, high_to_save;
3169 rtx save_area = 0; /* Place that it is saved. */
3170 #endif
3172 /* Size of the stack reserved for parameter registers. */
3173 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
3174 char *initial_stack_usage_map = stack_usage_map;
3176 rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
3178 #ifdef REG_PARM_STACK_SPACE
3179 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
3180 #endif
3182 /* By default, library functions can not throw. */
3183 flags = ECF_NOTHROW;
3185 switch (fn_type)
3187 case LCT_NORMAL:
3188 break;
3189 case LCT_CONST:
3190 flags |= ECF_CONST;
3191 break;
3192 case LCT_PURE:
3193 flags |= ECF_PURE;
3194 break;
3195 case LCT_CONST_MAKE_BLOCK:
3196 flags |= ECF_CONST | ECF_LIBCALL_BLOCK;
3197 break;
3198 case LCT_PURE_MAKE_BLOCK:
3199 flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
3200 break;
3201 case LCT_NORETURN:
3202 flags |= ECF_NORETURN;
3203 break;
3204 case LCT_THROW:
3205 flags = ECF_NORETURN;
3206 break;
3207 case LCT_ALWAYS_RETURN:
3208 flags = ECF_ALWAYS_RETURN;
3209 break;
3210 case LCT_RETURNS_TWICE:
3211 flags = ECF_RETURNS_TWICE;
3212 break;
3214 fun = orgfun;
3216 /* Ensure current function's preferred stack boundary is at least
3217 what we need. */
3218 if (cfun->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
3219 cfun->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3221 /* If this kind of value comes back in memory,
3222 decide where in memory it should come back. */
3223 if (outmode != VOIDmode)
3225 tfom = lang_hooks.types.type_for_mode (outmode, 0);
3226 if (aggregate_value_p (tfom, 0))
3228 #ifdef PCC_STATIC_STRUCT_RETURN
3229 rtx pointer_reg
3230 = hard_function_value (build_pointer_type (tfom), 0, 0);
3231 mem_value = gen_rtx_MEM (outmode, pointer_reg);
3232 pcc_struct_value = 1;
3233 if (value == 0)
3234 value = gen_reg_rtx (outmode);
3235 #else /* not PCC_STATIC_STRUCT_RETURN */
3236 struct_value_size = GET_MODE_SIZE (outmode);
3237 if (value != 0 && MEM_P (value))
3238 mem_value = value;
3239 else
3240 mem_value = assign_temp (tfom, 0, 1, 1);
3241 #endif
3242 /* This call returns a big structure. */
3243 flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
3246 else
3247 tfom = void_type_node;
3249 /* ??? Unfinished: must pass the memory address as an argument. */
3251 /* Copy all the libcall-arguments out of the varargs data
3252 and into a vector ARGVEC.
3254 Compute how to pass each argument. We only support a very small subset
3255 of the full argument passing conventions to limit complexity here since
3256 library functions shouldn't have many args. */
3258 argvec = alloca ((nargs + 1) * sizeof (struct arg));
3259 memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
3261 #ifdef INIT_CUMULATIVE_LIBCALL_ARGS
3262 INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far, outmode, fun);
3263 #else
3264 INIT_CUMULATIVE_ARGS (args_so_far, NULL_TREE, fun, 0, nargs);
3265 #endif
3267 args_size.constant = 0;
3268 args_size.var = 0;
3270 count = 0;
3272 /* Now we are about to start emitting insns that can be deleted
3273 if a libcall is deleted. */
3274 if (flags & ECF_LIBCALL_BLOCK)
3275 start_sequence ();
3277 push_temp_slots ();
3279 /* If there's a structure value address to be passed,
3280 either pass it in the special place, or pass it as an extra argument. */
3281 if (mem_value && struct_value == 0 && ! pcc_struct_value)
3283 rtx addr = XEXP (mem_value, 0);
3284 int partial;
3286 nargs++;
3288 /* Make sure it is a reasonable operand for a move or push insn. */
3289 if (!REG_P (addr) && !MEM_P (addr)
3290 && ! (CONSTANT_P (addr) && LEGITIMATE_CONSTANT_P (addr)))
3291 addr = force_operand (addr, NULL_RTX);
3293 argvec[count].value = addr;
3294 argvec[count].mode = Pmode;
3295 argvec[count].partial = 0;
3297 argvec[count].reg = FUNCTION_ARG (args_so_far, Pmode, NULL_TREE, 1);
3298 partial = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, Pmode, NULL_TREE, 1);
3299 gcc_assert (!partial);
3301 locate_and_pad_parm (Pmode, NULL_TREE,
3302 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3304 #else
3305 argvec[count].reg != 0,
3306 #endif
3307 0, NULL_TREE, &args_size, &argvec[count].locate);
3309 if (argvec[count].reg == 0 || argvec[count].partial != 0
3310 || reg_parm_stack_space > 0)
3311 args_size.constant += argvec[count].locate.size.constant;
3313 FUNCTION_ARG_ADVANCE (args_so_far, Pmode, (tree) 0, 1);
3315 count++;
3318 for (; count < nargs; count++)
3320 rtx val = va_arg (p, rtx);
3321 enum machine_mode mode = va_arg (p, enum machine_mode);
3323 /* We cannot convert the arg value to the mode the library wants here;
3324 must do it earlier where we know the signedness of the arg. */
3325 gcc_assert (mode != BLKmode
3326 && (GET_MODE (val) == mode || GET_MODE (val) == VOIDmode));
3328 /* Make sure it is a reasonable operand for a move or push insn. */
3329 if (!REG_P (val) && !MEM_P (val)
3330 && ! (CONSTANT_P (val) && LEGITIMATE_CONSTANT_P (val)))
3331 val = force_operand (val, NULL_RTX);
3333 if (pass_by_reference (&args_so_far, mode, NULL_TREE, 1))
3335 rtx slot;
3336 int must_copy
3337 = !reference_callee_copied (&args_so_far, mode, NULL_TREE, 1);
3339 /* loop.c won't look at CALL_INSN_FUNCTION_USAGE of const/pure
3340 functions, so we have to pretend this isn't such a function. */
3341 if (flags & ECF_LIBCALL_BLOCK)
3343 rtx insns = get_insns ();
3344 end_sequence ();
3345 emit_insn (insns);
3347 flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
3349 /* If this was a CONST function, it is now PURE since
3350 it now reads memory. */
3351 if (flags & ECF_CONST)
3353 flags &= ~ECF_CONST;
3354 flags |= ECF_PURE;
3357 if (GET_MODE (val) == MEM && !must_copy)
3358 slot = val;
3359 else
3361 slot = assign_temp (lang_hooks.types.type_for_mode (mode, 0),
3362 0, 1, 1);
3363 emit_move_insn (slot, val);
3366 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
3367 gen_rtx_USE (VOIDmode, slot),
3368 call_fusage);
3369 if (must_copy)
3370 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
3371 gen_rtx_CLOBBER (VOIDmode,
3372 slot),
3373 call_fusage);
3375 mode = Pmode;
3376 val = force_operand (XEXP (slot, 0), NULL_RTX);
3379 argvec[count].value = val;
3380 argvec[count].mode = mode;
3382 argvec[count].reg = FUNCTION_ARG (args_so_far, mode, NULL_TREE, 1);
3384 argvec[count].partial
3385 = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, mode, NULL_TREE, 1);
3387 locate_and_pad_parm (mode, NULL_TREE,
3388 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3390 #else
3391 argvec[count].reg != 0,
3392 #endif
3393 argvec[count].partial,
3394 NULL_TREE, &args_size, &argvec[count].locate);
3396 gcc_assert (!argvec[count].locate.size.var);
3398 if (argvec[count].reg == 0 || argvec[count].partial != 0
3399 || reg_parm_stack_space > 0)
3400 args_size.constant += argvec[count].locate.size.constant;
3402 FUNCTION_ARG_ADVANCE (args_so_far, mode, (tree) 0, 1);
3405 /* If this machine requires an external definition for library
3406 functions, write one out. */
3407 assemble_external_libcall (fun);
3409 original_args_size = args_size;
3410 args_size.constant = (((args_size.constant
3411 + stack_pointer_delta
3412 + STACK_BYTES - 1)
3413 / STACK_BYTES
3414 * STACK_BYTES)
3415 - stack_pointer_delta);
3417 args_size.constant = MAX (args_size.constant,
3418 reg_parm_stack_space);
3420 #ifndef OUTGOING_REG_PARM_STACK_SPACE
3421 args_size.constant -= reg_parm_stack_space;
3422 #endif
3424 if (args_size.constant > current_function_outgoing_args_size)
3425 current_function_outgoing_args_size = args_size.constant;
3427 if (ACCUMULATE_OUTGOING_ARGS)
3429 /* Since the stack pointer will never be pushed, it is possible for
3430 the evaluation of a parm to clobber something we have already
3431 written to the stack. Since most function calls on RISC machines
3432 do not use the stack, this is uncommon, but must work correctly.
3434 Therefore, we save any area of the stack that was already written
3435 and that we are using. Here we set up to do this by making a new
3436 stack usage map from the old one.
3438 Another approach might be to try to reorder the argument
3439 evaluations to avoid this conflicting stack usage. */
3441 needed = args_size.constant;
3443 #ifndef OUTGOING_REG_PARM_STACK_SPACE
3444 /* Since we will be writing into the entire argument area, the
3445 map must be allocated for its entire size, not just the part that
3446 is the responsibility of the caller. */
3447 needed += reg_parm_stack_space;
3448 #endif
3450 #ifdef ARGS_GROW_DOWNWARD
3451 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3452 needed + 1);
3453 #else
3454 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3455 needed);
3456 #endif
3457 stack_usage_map = alloca (highest_outgoing_arg_in_use);
3459 if (initial_highest_arg_in_use)
3460 memcpy (stack_usage_map, initial_stack_usage_map,
3461 initial_highest_arg_in_use);
3463 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
3464 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
3465 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
3466 needed = 0;
3468 /* We must be careful to use virtual regs before they're instantiated,
3469 and real regs afterwards. Loop optimization, for example, can create
3470 new libcalls after we've instantiated the virtual regs, and if we
3471 use virtuals anyway, they won't match the rtl patterns. */
3473 if (virtuals_instantiated)
3474 argblock = plus_constant (stack_pointer_rtx, STACK_POINTER_OFFSET);
3475 else
3476 argblock = virtual_outgoing_args_rtx;
3478 else
3480 if (!PUSH_ARGS)
3481 argblock = push_block (GEN_INT (args_size.constant), 0, 0);
3484 /* If we push args individually in reverse order, perform stack alignment
3485 before the first push (the last arg). */
3486 if (argblock == 0 && PUSH_ARGS_REVERSED)
3487 anti_adjust_stack (GEN_INT (args_size.constant
3488 - original_args_size.constant));
3490 if (PUSH_ARGS_REVERSED)
3492 inc = -1;
3493 argnum = nargs - 1;
3495 else
3497 inc = 1;
3498 argnum = 0;
3501 #ifdef REG_PARM_STACK_SPACE
3502 if (ACCUMULATE_OUTGOING_ARGS)
3504 /* The argument list is the property of the called routine and it
3505 may clobber it. If the fixed area has been used for previous
3506 parameters, we must save and restore it. */
3507 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3508 &low_to_save, &high_to_save);
3510 #endif
3512 /* Push the args that need to be pushed. */
3514 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
3515 are to be pushed. */
3516 for (count = 0; count < nargs; count++, argnum += inc)
3518 enum machine_mode mode = argvec[argnum].mode;
3519 rtx val = argvec[argnum].value;
3520 rtx reg = argvec[argnum].reg;
3521 int partial = argvec[argnum].partial;
3522 int lower_bound = 0, upper_bound = 0, i;
3524 if (! (reg != 0 && partial == 0))
3526 if (ACCUMULATE_OUTGOING_ARGS)
3528 /* If this is being stored into a pre-allocated, fixed-size,
3529 stack area, save any previous data at that location. */
3531 #ifdef ARGS_GROW_DOWNWARD
3532 /* stack_slot is negative, but we want to index stack_usage_map
3533 with positive values. */
3534 upper_bound = -argvec[argnum].locate.offset.constant + 1;
3535 lower_bound = upper_bound - argvec[argnum].locate.size.constant;
3536 #else
3537 lower_bound = argvec[argnum].locate.offset.constant;
3538 upper_bound = lower_bound + argvec[argnum].locate.size.constant;
3539 #endif
3541 i = lower_bound;
3542 /* Don't worry about things in the fixed argument area;
3543 it has already been saved. */
3544 if (i < reg_parm_stack_space)
3545 i = reg_parm_stack_space;
3546 while (i < upper_bound && stack_usage_map[i] == 0)
3547 i++;
3549 if (i < upper_bound)
3551 /* We need to make a save area. */
3552 unsigned int size
3553 = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
3554 enum machine_mode save_mode
3555 = mode_for_size (size, MODE_INT, 1);
3556 rtx adr
3557 = plus_constant (argblock,
3558 argvec[argnum].locate.offset.constant);
3559 rtx stack_area
3560 = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
3562 if (save_mode == BLKmode)
3564 argvec[argnum].save_area
3565 = assign_stack_temp (BLKmode,
3566 argvec[argnum].locate.size.constant,
3569 emit_block_move (validize_mem (argvec[argnum].save_area),
3570 stack_area,
3571 GEN_INT (argvec[argnum].locate.size.constant),
3572 BLOCK_OP_CALL_PARM);
3574 else
3576 argvec[argnum].save_area = gen_reg_rtx (save_mode);
3578 emit_move_insn (argvec[argnum].save_area, stack_area);
3583 emit_push_insn (val, mode, NULL_TREE, NULL_RTX, PARM_BOUNDARY,
3584 partial, reg, 0, argblock,
3585 GEN_INT (argvec[argnum].locate.offset.constant),
3586 reg_parm_stack_space,
3587 ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad));
3589 /* Now mark the segment we just used. */
3590 if (ACCUMULATE_OUTGOING_ARGS)
3591 for (i = lower_bound; i < upper_bound; i++)
3592 stack_usage_map[i] = 1;
3594 NO_DEFER_POP;
3598 /* If we pushed args in forward order, perform stack alignment
3599 after pushing the last arg. */
3600 if (argblock == 0 && !PUSH_ARGS_REVERSED)
3601 anti_adjust_stack (GEN_INT (args_size.constant
3602 - original_args_size.constant));
3604 if (PUSH_ARGS_REVERSED)
3605 argnum = nargs - 1;
3606 else
3607 argnum = 0;
3609 fun = prepare_call_address (fun, NULL, &call_fusage, 0, 0);
3611 /* Now load any reg parms into their regs. */
3613 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
3614 are to be pushed. */
3615 for (count = 0; count < nargs; count++, argnum += inc)
3617 enum machine_mode mode = argvec[argnum].mode;
3618 rtx val = argvec[argnum].value;
3619 rtx reg = argvec[argnum].reg;
3620 int partial = argvec[argnum].partial;
3622 /* Handle calls that pass values in multiple non-contiguous
3623 locations. The PA64 has examples of this for library calls. */
3624 if (reg != 0 && GET_CODE (reg) == PARALLEL)
3625 emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (mode));
3626 else if (reg != 0 && partial == 0)
3627 emit_move_insn (reg, val);
3629 NO_DEFER_POP;
3632 /* Any regs containing parms remain in use through the call. */
3633 for (count = 0; count < nargs; count++)
3635 rtx reg = argvec[count].reg;
3636 if (reg != 0 && GET_CODE (reg) == PARALLEL)
3637 use_group_regs (&call_fusage, reg);
3638 else if (reg != 0)
3639 use_reg (&call_fusage, reg);
3642 /* Pass the function the address in which to return a structure value. */
3643 if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
3645 emit_move_insn (struct_value,
3646 force_reg (Pmode,
3647 force_operand (XEXP (mem_value, 0),
3648 NULL_RTX)));
3649 if (REG_P (struct_value))
3650 use_reg (&call_fusage, struct_value);
3653 /* Don't allow popping to be deferred, since then
3654 cse'ing of library calls could delete a call and leave the pop. */
3655 NO_DEFER_POP;
3656 valreg = (mem_value == 0 && outmode != VOIDmode
3657 ? hard_libcall_value (outmode) : NULL_RTX);
3659 /* Stack must be properly aligned now. */
3660 gcc_assert (!(stack_pointer_delta
3661 & (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1)));
3663 before_call = get_last_insn ();
3665 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
3666 will set inhibit_defer_pop to that value. */
3667 /* The return type is needed to decide how many bytes the function pops.
3668 Signedness plays no role in that, so for simplicity, we pretend it's
3669 always signed. We also assume that the list of arguments passed has
3670 no impact, so we pretend it is unknown. */
3672 emit_call_1 (fun, NULL,
3673 get_identifier (XSTR (orgfun, 0)),
3674 build_function_type (tfom, NULL_TREE),
3675 original_args_size.constant, args_size.constant,
3676 struct_value_size,
3677 FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1),
3678 valreg,
3679 old_inhibit_defer_pop + 1, call_fusage, flags, & args_so_far);
3681 /* For calls to `setjmp', etc., inform flow.c it should complain
3682 if nonvolatile values are live. For functions that cannot return,
3683 inform flow that control does not fall through. */
3685 if (flags & (ECF_NORETURN | ECF_LONGJMP))
3687 /* The barrier note must be emitted
3688 immediately after the CALL_INSN. Some ports emit more than
3689 just a CALL_INSN above, so we must search for it here. */
3691 rtx last = get_last_insn ();
3692 while (!CALL_P (last))
3694 last = PREV_INSN (last);
3695 /* There was no CALL_INSN? */
3696 gcc_assert (last != before_call);
3699 emit_barrier_after (last);
3702 /* Now restore inhibit_defer_pop to its actual original value. */
3703 OK_DEFER_POP;
3705 /* If call is cse'able, make appropriate pair of reg-notes around it.
3706 Test valreg so we don't crash; may safely ignore `const'
3707 if return type is void. Disable for PARALLEL return values, because
3708 we have no way to move such values into a pseudo register. */
3709 if (flags & ECF_LIBCALL_BLOCK)
3711 rtx insns;
3713 if (valreg == 0)
3715 insns = get_insns ();
3716 end_sequence ();
3717 emit_insn (insns);
3719 else
3721 rtx note = 0;
3722 rtx temp;
3723 int i;
3725 if (GET_CODE (valreg) == PARALLEL)
3727 temp = gen_reg_rtx (outmode);
3728 emit_group_store (temp, valreg, NULL_TREE,
3729 GET_MODE_SIZE (outmode));
3730 valreg = temp;
3733 temp = gen_reg_rtx (GET_MODE (valreg));
3735 /* Construct an "equal form" for the value which mentions all the
3736 arguments in order as well as the function name. */
3737 for (i = 0; i < nargs; i++)
3738 note = gen_rtx_EXPR_LIST (VOIDmode, argvec[i].value, note);
3739 note = gen_rtx_EXPR_LIST (VOIDmode, fun, note);
3741 insns = get_insns ();
3742 end_sequence ();
3744 if (flags & ECF_PURE)
3745 note = gen_rtx_EXPR_LIST (VOIDmode,
3746 gen_rtx_USE (VOIDmode,
3747 gen_rtx_MEM (BLKmode,
3748 gen_rtx_SCRATCH (VOIDmode))),
3749 note);
3751 emit_libcall_block (insns, temp, valreg, note);
3753 valreg = temp;
3756 pop_temp_slots ();
3758 /* Copy the value to the right place. */
3759 if (outmode != VOIDmode && retval)
3761 if (mem_value)
3763 if (value == 0)
3764 value = mem_value;
3765 if (value != mem_value)
3766 emit_move_insn (value, mem_value);
3768 else if (GET_CODE (valreg) == PARALLEL)
3770 if (value == 0)
3771 value = gen_reg_rtx (outmode);
3772 emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
3774 else if (value != 0)
3775 emit_move_insn (value, valreg);
3776 else
3777 value = valreg;
3780 if (ACCUMULATE_OUTGOING_ARGS)
3782 #ifdef REG_PARM_STACK_SPACE
3783 if (save_area)
3784 restore_fixed_argument_area (save_area, argblock,
3785 high_to_save, low_to_save);
3786 #endif
3788 /* If we saved any argument areas, restore them. */
3789 for (count = 0; count < nargs; count++)
3790 if (argvec[count].save_area)
3792 enum machine_mode save_mode = GET_MODE (argvec[count].save_area);
3793 rtx adr = plus_constant (argblock,
3794 argvec[count].locate.offset.constant);
3795 rtx stack_area = gen_rtx_MEM (save_mode,
3796 memory_address (save_mode, adr));
3798 if (save_mode == BLKmode)
3799 emit_block_move (stack_area,
3800 validize_mem (argvec[count].save_area),
3801 GEN_INT (argvec[count].locate.size.constant),
3802 BLOCK_OP_CALL_PARM);
3803 else
3804 emit_move_insn (stack_area, argvec[count].save_area);
3807 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3808 stack_usage_map = initial_stack_usage_map;
3811 return value;
3815 /* Output a library call to function FUN (a SYMBOL_REF rtx)
3816 (emitting the queue unless NO_QUEUE is nonzero),
3817 for a value of mode OUTMODE,
3818 with NARGS different arguments, passed as alternating rtx values
3819 and machine_modes to convert them to.
3821 FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for `const'
3822 calls, LCT_PURE for `pure' calls, LCT_CONST_MAKE_BLOCK for `const' calls
3823 which should be enclosed in REG_LIBCALL/REG_RETVAL notes,
3824 LCT_PURE_MAKE_BLOCK for `purep' calls which should be enclosed in
3825 REG_LIBCALL/REG_RETVAL notes with extra (use (memory (scratch)),
3826 or other LCT_ value for other types of library calls. */
3828 void
3829 emit_library_call (rtx orgfun, enum libcall_type fn_type,
3830 enum machine_mode outmode, int nargs, ...)
3832 va_list p;
3834 va_start (p, nargs);
3835 emit_library_call_value_1 (0, orgfun, NULL_RTX, fn_type, outmode, nargs, p);
3836 va_end (p);
3839 /* Like emit_library_call except that an extra argument, VALUE,
3840 comes second and says where to store the result.
3841 (If VALUE is zero, this function chooses a convenient way
3842 to return the value.
3844 This function returns an rtx for where the value is to be found.
3845 If VALUE is nonzero, VALUE is returned. */
3848 emit_library_call_value (rtx orgfun, rtx value,
3849 enum libcall_type fn_type,
3850 enum machine_mode outmode, int nargs, ...)
3852 rtx result;
3853 va_list p;
3855 va_start (p, nargs);
3856 result = emit_library_call_value_1 (1, orgfun, value, fn_type, outmode,
3857 nargs, p);
3858 va_end (p);
3860 return result;
3863 /* Store a single argument for a function call
3864 into the register or memory area where it must be passed.
3865 *ARG describes the argument value and where to pass it.
3867 ARGBLOCK is the address of the stack-block for all the arguments,
3868 or 0 on a machine where arguments are pushed individually.
3870 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
3871 so must be careful about how the stack is used.
3873 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
3874 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
3875 that we need not worry about saving and restoring the stack.
3877 FNDECL is the declaration of the function we are calling.
3879 Return nonzero if this arg should cause sibcall failure,
3880 zero otherwise. */
3882 static int
3883 store_one_arg (struct arg_data *arg, rtx argblock, int flags,
3884 int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
3886 tree pval = arg->tree_value;
3887 rtx reg = 0;
3888 int partial = 0;
3889 int used = 0;
3890 int i, lower_bound = 0, upper_bound = 0;
3891 int sibcall_failure = 0;
3893 if (TREE_CODE (pval) == ERROR_MARK)
3894 return 1;
3896 /* Push a new temporary level for any temporaries we make for
3897 this argument. */
3898 push_temp_slots ();
3900 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
3902 /* If this is being stored into a pre-allocated, fixed-size, stack area,
3903 save any previous data at that location. */
3904 if (argblock && ! variable_size && arg->stack)
3906 #ifdef ARGS_GROW_DOWNWARD
3907 /* stack_slot is negative, but we want to index stack_usage_map
3908 with positive values. */
3909 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
3910 upper_bound = -INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1)) + 1;
3911 else
3912 upper_bound = 0;
3914 lower_bound = upper_bound - arg->locate.size.constant;
3915 #else
3916 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
3917 lower_bound = INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1));
3918 else
3919 lower_bound = 0;
3921 upper_bound = lower_bound + arg->locate.size.constant;
3922 #endif
3924 i = lower_bound;
3925 /* Don't worry about things in the fixed argument area;
3926 it has already been saved. */
3927 if (i < reg_parm_stack_space)
3928 i = reg_parm_stack_space;
3929 while (i < upper_bound && stack_usage_map[i] == 0)
3930 i++;
3932 if (i < upper_bound)
3934 /* We need to make a save area. */
3935 unsigned int size = arg->locate.size.constant * BITS_PER_UNIT;
3936 enum machine_mode save_mode = mode_for_size (size, MODE_INT, 1);
3937 rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
3938 rtx stack_area = gen_rtx_MEM (save_mode, adr);
3940 if (save_mode == BLKmode)
3942 tree ot = TREE_TYPE (arg->tree_value);
3943 tree nt = build_qualified_type (ot, (TYPE_QUALS (ot)
3944 | TYPE_QUAL_CONST));
3946 arg->save_area = assign_temp (nt, 0, 1, 1);
3947 preserve_temp_slots (arg->save_area);
3948 emit_block_move (validize_mem (arg->save_area), stack_area,
3949 expr_size (arg->tree_value),
3950 BLOCK_OP_CALL_PARM);
3952 else
3954 arg->save_area = gen_reg_rtx (save_mode);
3955 emit_move_insn (arg->save_area, stack_area);
3961 /* If this isn't going to be placed on both the stack and in registers,
3962 set up the register and number of words. */
3963 if (! arg->pass_on_stack)
3965 if (flags & ECF_SIBCALL)
3966 reg = arg->tail_call_reg;
3967 else
3968 reg = arg->reg;
3969 partial = arg->partial;
3972 /* Being passed entirely in a register. We shouldn't be called in
3973 this case. */
3974 gcc_assert (reg == 0 || partial != 0);
3976 /* If this arg needs special alignment, don't load the registers
3977 here. */
3978 if (arg->n_aligned_regs != 0)
3979 reg = 0;
3981 /* If this is being passed partially in a register, we can't evaluate
3982 it directly into its stack slot. Otherwise, we can. */
3983 if (arg->value == 0)
3985 /* stack_arg_under_construction is nonzero if a function argument is
3986 being evaluated directly into the outgoing argument list and
3987 expand_call must take special action to preserve the argument list
3988 if it is called recursively.
3990 For scalar function arguments stack_usage_map is sufficient to
3991 determine which stack slots must be saved and restored. Scalar
3992 arguments in general have pass_on_stack == 0.
3994 If this argument is initialized by a function which takes the
3995 address of the argument (a C++ constructor or a C function
3996 returning a BLKmode structure), then stack_usage_map is
3997 insufficient and expand_call must push the stack around the
3998 function call. Such arguments have pass_on_stack == 1.
4000 Note that it is always safe to set stack_arg_under_construction,
4001 but this generates suboptimal code if set when not needed. */
4003 if (arg->pass_on_stack)
4004 stack_arg_under_construction++;
4006 arg->value = expand_expr (pval,
4007 (partial
4008 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
4009 ? NULL_RTX : arg->stack,
4010 VOIDmode, EXPAND_STACK_PARM);
4012 /* If we are promoting object (or for any other reason) the mode
4013 doesn't agree, convert the mode. */
4015 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
4016 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
4017 arg->value, arg->unsignedp);
4019 if (arg->pass_on_stack)
4020 stack_arg_under_construction--;
4023 /* Don't allow anything left on stack from computation
4024 of argument to alloca. */
4025 if (flags & ECF_MAY_BE_ALLOCA)
4026 do_pending_stack_adjust ();
4028 if (arg->value == arg->stack)
4029 /* If the value is already in the stack slot, we are done. */
4031 else if (arg->mode != BLKmode)
4033 int size;
4035 /* Argument is a scalar, not entirely passed in registers.
4036 (If part is passed in registers, arg->partial says how much
4037 and emit_push_insn will take care of putting it there.)
4039 Push it, and if its size is less than the
4040 amount of space allocated to it,
4041 also bump stack pointer by the additional space.
4042 Note that in C the default argument promotions
4043 will prevent such mismatches. */
4045 size = GET_MODE_SIZE (arg->mode);
4046 /* Compute how much space the push instruction will push.
4047 On many machines, pushing a byte will advance the stack
4048 pointer by a halfword. */
4049 #ifdef PUSH_ROUNDING
4050 size = PUSH_ROUNDING (size);
4051 #endif
4052 used = size;
4054 /* Compute how much space the argument should get:
4055 round up to a multiple of the alignment for arguments. */
4056 if (none != FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)))
4057 used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
4058 / (PARM_BOUNDARY / BITS_PER_UNIT))
4059 * (PARM_BOUNDARY / BITS_PER_UNIT));
4061 /* This isn't already where we want it on the stack, so put it there.
4062 This can either be done with push or copy insns. */
4063 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), NULL_RTX,
4064 PARM_BOUNDARY, partial, reg, used - size, argblock,
4065 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4066 ARGS_SIZE_RTX (arg->locate.alignment_pad));
4068 /* Unless this is a partially-in-register argument, the argument is now
4069 in the stack. */
4070 if (partial == 0)
4071 arg->value = arg->stack;
4073 else
4075 /* BLKmode, at least partly to be pushed. */
4077 unsigned int parm_align;
4078 int excess;
4079 rtx size_rtx;
4081 /* Pushing a nonscalar.
4082 If part is passed in registers, PARTIAL says how much
4083 and emit_push_insn will take care of putting it there. */
4085 /* Round its size up to a multiple
4086 of the allocation unit for arguments. */
4088 if (arg->locate.size.var != 0)
4090 excess = 0;
4091 size_rtx = ARGS_SIZE_RTX (arg->locate.size);
4093 else
4095 /* PUSH_ROUNDING has no effect on us, because
4096 emit_push_insn for BLKmode is careful to avoid it. */
4097 if (reg && GET_CODE (reg) == PARALLEL)
4099 /* Use the size of the elt to compute excess. */
4100 rtx elt = XEXP (XVECEXP (reg, 0, 0), 0);
4101 excess = (arg->locate.size.constant
4102 - int_size_in_bytes (TREE_TYPE (pval))
4103 + partial * GET_MODE_SIZE (GET_MODE (elt)));
4105 else
4106 excess = (arg->locate.size.constant
4107 - int_size_in_bytes (TREE_TYPE (pval))
4108 + partial * UNITS_PER_WORD);
4109 size_rtx = expand_expr (size_in_bytes (TREE_TYPE (pval)),
4110 NULL_RTX, TYPE_MODE (sizetype), 0);
4113 /* Some types will require stricter alignment, which will be
4114 provided for elsewhere in argument layout. */
4115 parm_align = MAX (PARM_BOUNDARY, TYPE_ALIGN (TREE_TYPE (pval)));
4117 /* When an argument is padded down, the block is aligned to
4118 PARM_BOUNDARY, but the actual argument isn't. */
4119 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
4121 if (arg->locate.size.var)
4122 parm_align = BITS_PER_UNIT;
4123 else if (excess)
4125 unsigned int excess_align = (excess & -excess) * BITS_PER_UNIT;
4126 parm_align = MIN (parm_align, excess_align);
4130 if ((flags & ECF_SIBCALL) && MEM_P (arg->value))
4132 /* emit_push_insn might not work properly if arg->value and
4133 argblock + arg->locate.offset areas overlap. */
4134 rtx x = arg->value;
4135 int i = 0;
4137 if (XEXP (x, 0) == current_function_internal_arg_pointer
4138 || (GET_CODE (XEXP (x, 0)) == PLUS
4139 && XEXP (XEXP (x, 0), 0) ==
4140 current_function_internal_arg_pointer
4141 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4143 if (XEXP (x, 0) != current_function_internal_arg_pointer)
4144 i = INTVAL (XEXP (XEXP (x, 0), 1));
4146 /* expand_call should ensure this. */
4147 gcc_assert (!arg->locate.offset.var
4148 && GET_CODE (size_rtx) == CONST_INT);
4150 if (arg->locate.offset.constant > i)
4152 if (arg->locate.offset.constant < i + INTVAL (size_rtx))
4153 sibcall_failure = 1;
4155 else if (arg->locate.offset.constant < i)
4157 if (i < arg->locate.offset.constant + INTVAL (size_rtx))
4158 sibcall_failure = 1;
4163 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
4164 parm_align, partial, reg, excess, argblock,
4165 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4166 ARGS_SIZE_RTX (arg->locate.alignment_pad));
4168 /* Unless this is a partially-in-register argument, the argument is now
4169 in the stack.
4171 ??? Unlike the case above, in which we want the actual
4172 address of the data, so that we can load it directly into a
4173 register, here we want the address of the stack slot, so that
4174 it's properly aligned for word-by-word copying or something
4175 like that. It's not clear that this is always correct. */
4176 if (partial == 0)
4177 arg->value = arg->stack_slot;
4180 /* Mark all slots this store used. */
4181 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
4182 && argblock && ! variable_size && arg->stack)
4183 for (i = lower_bound; i < upper_bound; i++)
4184 stack_usage_map[i] = 1;
4186 /* Once we have pushed something, pops can't safely
4187 be deferred during the rest of the arguments. */
4188 NO_DEFER_POP;
4190 /* Free any temporary slots made in processing this argument. Show
4191 that we might have taken the address of something and pushed that
4192 as an operand. */
4193 preserve_temp_slots (NULL_RTX);
4194 free_temp_slots ();
4195 pop_temp_slots ();
4197 return sibcall_failure;
4200 /* Nonzero if we do not know how to pass TYPE solely in registers. */
4202 bool
4203 must_pass_in_stack_var_size (enum machine_mode mode ATTRIBUTE_UNUSED,
4204 tree type)
4206 if (!type)
4207 return false;
4209 /* If the type has variable size... */
4210 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4211 return true;
4213 /* If the type is marked as addressable (it is required
4214 to be constructed into the stack)... */
4215 if (TREE_ADDRESSABLE (type))
4216 return true;
4218 return false;
4221 /* Another version of the TARGET_MUST_PASS_IN_STACK hook. This one
4222 takes trailing padding of a structure into account. */
4223 /* ??? Should be able to merge these two by examining BLOCK_REG_PADDING. */
4225 bool
4226 must_pass_in_stack_var_size_or_pad (enum machine_mode mode, tree type)
4228 if (!type)
4229 return false;
4231 /* If the type has variable size... */
4232 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4233 return true;
4235 /* If the type is marked as addressable (it is required
4236 to be constructed into the stack)... */
4237 if (TREE_ADDRESSABLE (type))
4238 return true;
4240 /* If the padding and mode of the type is such that a copy into
4241 a register would put it into the wrong part of the register. */
4242 if (mode == BLKmode
4243 && int_size_in_bytes (type) % (PARM_BOUNDARY / BITS_PER_UNIT)
4244 && (FUNCTION_ARG_PADDING (mode, type)
4245 == (BYTES_BIG_ENDIAN ? upward : downward)))
4246 return true;
4248 return false;