* MAINTAINERS: Add self as a profile feedback maintainer.
[official-gcc.git] / gcc / calls.c
blob9f84199990e9956d8181f3c25ae5386f7ab9cd17
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 int);
147 static tree fix_unsafe_tree (tree);
148 static bool shift_returned_value (tree, rtx *);
150 #ifdef REG_PARM_STACK_SPACE
151 static rtx save_fixed_argument_area (int, rtx, int *, int *);
152 static void restore_fixed_argument_area (rtx, rtx, int, int);
153 #endif
155 /* Force FUNEXP into a form suitable for the address of a CALL,
156 and return that as an rtx. Also load the static chain register
157 if FNDECL is a nested function.
159 CALL_FUSAGE points to a variable holding the prospective
160 CALL_INSN_FUNCTION_USAGE information. */
163 prepare_call_address (rtx funexp, rtx static_chain_value,
164 rtx *call_fusage, int reg_parm_seen, int sibcallp)
166 funexp = protect_from_queue (funexp, 0);
168 /* Make a valid memory address and copy constants through pseudo-regs,
169 but not for a constant address if -fno-function-cse. */
170 if (GET_CODE (funexp) != SYMBOL_REF)
171 /* If we are using registers for parameters, force the
172 function address into a register now. */
173 funexp = ((SMALL_REGISTER_CLASSES && reg_parm_seen)
174 ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
175 : memory_address (FUNCTION_MODE, funexp));
176 else if (! sibcallp)
178 #ifndef NO_FUNCTION_CSE
179 if (optimize && ! flag_no_function_cse)
180 funexp = force_reg (Pmode, funexp);
181 #endif
184 if (static_chain_value != 0)
186 emit_move_insn (static_chain_rtx, static_chain_value);
188 if (REG_P (static_chain_rtx))
189 use_reg (call_fusage, static_chain_rtx);
192 return funexp;
195 /* Generate instructions to call function FUNEXP,
196 and optionally pop the results.
197 The CALL_INSN is the first insn generated.
199 FNDECL is the declaration node of the function. This is given to the
200 macro RETURN_POPS_ARGS to determine whether this function pops its own args.
202 FUNTYPE is the data type of the function. This is given to the macro
203 RETURN_POPS_ARGS to determine whether this function pops its own args.
204 We used to allow an identifier for library functions, but that doesn't
205 work when the return type is an aggregate type and the calling convention
206 says that the pointer to this aggregate is to be popped by the callee.
208 STACK_SIZE is the number of bytes of arguments on the stack,
209 ROUNDED_STACK_SIZE is that number rounded up to
210 PREFERRED_STACK_BOUNDARY; zero if the size is variable. This is
211 both to put into the call insn and to generate explicit popping
212 code if necessary.
214 STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
215 It is zero if this call doesn't want a structure value.
217 NEXT_ARG_REG is the rtx that results from executing
218 FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1)
219 just after all the args have had their registers assigned.
220 This could be whatever you like, but normally it is the first
221 arg-register beyond those used for args in this call,
222 or 0 if all the arg-registers are used in this call.
223 It is passed on to `gen_call' so you can put this info in the call insn.
225 VALREG is a hard register in which a value is returned,
226 or 0 if the call does not return a value.
228 OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
229 the args to this call were processed.
230 We restore `inhibit_defer_pop' to that value.
232 CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
233 denote registers used by the called function. */
235 static void
236 emit_call_1 (rtx funexp, tree fntree, tree fndecl ATTRIBUTE_UNUSED,
237 tree funtype ATTRIBUTE_UNUSED,
238 HOST_WIDE_INT stack_size ATTRIBUTE_UNUSED,
239 HOST_WIDE_INT rounded_stack_size,
240 HOST_WIDE_INT struct_value_size ATTRIBUTE_UNUSED,
241 rtx next_arg_reg ATTRIBUTE_UNUSED, rtx valreg,
242 int old_inhibit_defer_pop, rtx call_fusage, int ecf_flags,
243 CUMULATIVE_ARGS *args_so_far ATTRIBUTE_UNUSED)
245 rtx rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
246 rtx call_insn;
247 int already_popped = 0;
248 HOST_WIDE_INT n_popped = RETURN_POPS_ARGS (fndecl, funtype, stack_size);
249 #if defined (HAVE_call) && defined (HAVE_call_value)
250 rtx struct_value_size_rtx;
251 struct_value_size_rtx = GEN_INT (struct_value_size);
252 #endif
254 #ifdef CALL_POPS_ARGS
255 n_popped += CALL_POPS_ARGS (* args_so_far);
256 #endif
258 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
259 and we don't want to load it into a register as an optimization,
260 because prepare_call_address already did it if it should be done. */
261 if (GET_CODE (funexp) != SYMBOL_REF)
262 funexp = memory_address (FUNCTION_MODE, funexp);
264 #if defined (HAVE_sibcall_pop) && defined (HAVE_sibcall_value_pop)
265 if ((ecf_flags & ECF_SIBCALL)
266 && HAVE_sibcall_pop && HAVE_sibcall_value_pop
267 && (n_popped > 0 || stack_size == 0))
269 rtx n_pop = GEN_INT (n_popped);
270 rtx pat;
272 /* If this subroutine pops its own args, record that in the call insn
273 if possible, for the sake of frame pointer elimination. */
275 if (valreg)
276 pat = GEN_SIBCALL_VALUE_POP (valreg,
277 gen_rtx_MEM (FUNCTION_MODE, funexp),
278 rounded_stack_size_rtx, next_arg_reg,
279 n_pop);
280 else
281 pat = GEN_SIBCALL_POP (gen_rtx_MEM (FUNCTION_MODE, funexp),
282 rounded_stack_size_rtx, next_arg_reg, n_pop);
284 emit_call_insn (pat);
285 already_popped = 1;
287 else
288 #endif
290 #if defined (HAVE_call_pop) && defined (HAVE_call_value_pop)
291 /* If the target has "call" or "call_value" insns, then prefer them
292 if no arguments are actually popped. If the target does not have
293 "call" or "call_value" insns, then we must use the popping versions
294 even if the call has no arguments to pop. */
295 #if defined (HAVE_call) && defined (HAVE_call_value)
296 if (HAVE_call && HAVE_call_value && HAVE_call_pop && HAVE_call_value_pop
297 && n_popped > 0 && ! (ecf_flags & ECF_SP_DEPRESSED))
298 #else
299 if (HAVE_call_pop && HAVE_call_value_pop)
300 #endif
302 rtx n_pop = GEN_INT (n_popped);
303 rtx pat;
305 /* If this subroutine pops its own args, record that in the call insn
306 if possible, for the sake of frame pointer elimination. */
308 if (valreg)
309 pat = GEN_CALL_VALUE_POP (valreg,
310 gen_rtx_MEM (FUNCTION_MODE, funexp),
311 rounded_stack_size_rtx, next_arg_reg, n_pop);
312 else
313 pat = GEN_CALL_POP (gen_rtx_MEM (FUNCTION_MODE, funexp),
314 rounded_stack_size_rtx, next_arg_reg, n_pop);
316 emit_call_insn (pat);
317 already_popped = 1;
319 else
320 #endif
322 #if defined (HAVE_sibcall) && defined (HAVE_sibcall_value)
323 if ((ecf_flags & ECF_SIBCALL)
324 && HAVE_sibcall && HAVE_sibcall_value)
326 if (valreg)
327 emit_call_insn (GEN_SIBCALL_VALUE (valreg,
328 gen_rtx_MEM (FUNCTION_MODE, funexp),
329 rounded_stack_size_rtx,
330 next_arg_reg, NULL_RTX));
331 else
332 emit_call_insn (GEN_SIBCALL (gen_rtx_MEM (FUNCTION_MODE, funexp),
333 rounded_stack_size_rtx, next_arg_reg,
334 struct_value_size_rtx));
336 else
337 #endif
339 #if defined (HAVE_call) && defined (HAVE_call_value)
340 if (HAVE_call && HAVE_call_value)
342 if (valreg)
343 emit_call_insn (GEN_CALL_VALUE (valreg,
344 gen_rtx_MEM (FUNCTION_MODE, funexp),
345 rounded_stack_size_rtx, next_arg_reg,
346 NULL_RTX));
347 else
348 emit_call_insn (GEN_CALL (gen_rtx_MEM (FUNCTION_MODE, funexp),
349 rounded_stack_size_rtx, next_arg_reg,
350 struct_value_size_rtx));
352 else
353 #endif
354 abort ();
356 /* Find the call we just emitted. */
357 call_insn = last_call_insn ();
359 /* Mark memory as used for "pure" function call. */
360 if (ecf_flags & ECF_PURE)
361 call_fusage
362 = gen_rtx_EXPR_LIST
363 (VOIDmode,
364 gen_rtx_USE (VOIDmode,
365 gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode))),
366 call_fusage);
368 /* Put the register usage information there. */
369 add_function_usage_to (call_insn, call_fusage);
371 /* If this is a const call, then set the insn's unchanging bit. */
372 if (ecf_flags & (ECF_CONST | ECF_PURE))
373 CONST_OR_PURE_CALL_P (call_insn) = 1;
375 /* If this call can't throw, attach a REG_EH_REGION reg note to that
376 effect. */
377 if (ecf_flags & ECF_NOTHROW)
378 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, const0_rtx,
379 REG_NOTES (call_insn));
380 else
382 int rn = lookup_stmt_eh_region (fntree);
384 /* If rn < 0, then either (1) tree-ssa not used or (2) doesn't
385 throw, which we already took care of. */
386 if (rn > 0)
387 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, GEN_INT (rn),
388 REG_NOTES (call_insn));
389 note_current_region_may_contain_throw ();
392 if (ecf_flags & ECF_NORETURN)
393 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_NORETURN, const0_rtx,
394 REG_NOTES (call_insn));
395 if (ecf_flags & ECF_ALWAYS_RETURN)
396 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_ALWAYS_RETURN, const0_rtx,
397 REG_NOTES (call_insn));
399 if (ecf_flags & ECF_RETURNS_TWICE)
401 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_SETJMP, const0_rtx,
402 REG_NOTES (call_insn));
403 current_function_calls_setjmp = 1;
406 SIBLING_CALL_P (call_insn) = ((ecf_flags & ECF_SIBCALL) != 0);
408 /* Restore this now, so that we do defer pops for this call's args
409 if the context of the call as a whole permits. */
410 inhibit_defer_pop = old_inhibit_defer_pop;
412 if (n_popped > 0)
414 if (!already_popped)
415 CALL_INSN_FUNCTION_USAGE (call_insn)
416 = gen_rtx_EXPR_LIST (VOIDmode,
417 gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx),
418 CALL_INSN_FUNCTION_USAGE (call_insn));
419 rounded_stack_size -= n_popped;
420 rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
421 stack_pointer_delta -= n_popped;
424 if (!ACCUMULATE_OUTGOING_ARGS)
426 /* If returning from the subroutine does not automatically pop the args,
427 we need an instruction to pop them sooner or later.
428 Perhaps do it now; perhaps just record how much space to pop later.
430 If returning from the subroutine does pop the args, indicate that the
431 stack pointer will be changed. */
433 if (rounded_stack_size != 0)
435 if (ecf_flags & (ECF_SP_DEPRESSED | ECF_NORETURN | ECF_LONGJMP))
436 /* Just pretend we did the pop. */
437 stack_pointer_delta -= rounded_stack_size;
438 else if (flag_defer_pop && inhibit_defer_pop == 0
439 && ! (ecf_flags & (ECF_CONST | ECF_PURE)))
440 pending_stack_adjust += rounded_stack_size;
441 else
442 adjust_stack (rounded_stack_size_rtx);
445 /* When we accumulate outgoing args, we must avoid any stack manipulations.
446 Restore the stack pointer to its original value now. Usually
447 ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
448 On i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
449 popping variants of functions exist as well.
451 ??? We may optimize similar to defer_pop above, but it is
452 probably not worthwhile.
454 ??? It will be worthwhile to enable combine_stack_adjustments even for
455 such machines. */
456 else if (n_popped)
457 anti_adjust_stack (GEN_INT (n_popped));
460 /* Determine if the function identified by NAME and FNDECL is one with
461 special properties we wish to know about.
463 For example, if the function might return more than one time (setjmp), then
464 set RETURNS_TWICE to a nonzero value.
466 Similarly set LONGJMP for if the function is in the longjmp family.
468 Set MAY_BE_ALLOCA for any memory allocation function that might allocate
469 space from the stack such as alloca. */
471 static int
472 special_function_p (tree fndecl, int flags)
474 if (fndecl && DECL_NAME (fndecl)
475 && IDENTIFIER_LENGTH (DECL_NAME (fndecl)) <= 17
476 /* Exclude functions not at the file scope, or not `extern',
477 since they are not the magic functions we would otherwise
478 think they are.
479 FIXME: this should be handled with attributes, not with this
480 hacky imitation of DECL_ASSEMBLER_NAME. It's (also) wrong
481 because you can declare fork() inside a function if you
482 wish. */
483 && (DECL_CONTEXT (fndecl) == NULL_TREE
484 || TREE_CODE (DECL_CONTEXT (fndecl)) == TRANSLATION_UNIT_DECL)
485 && TREE_PUBLIC (fndecl))
487 const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
488 const char *tname = name;
490 /* We assume that alloca will always be called by name. It
491 makes no sense to pass it as a pointer-to-function to
492 anything that does not understand its behavior. */
493 if (((IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 6
494 && name[0] == 'a'
495 && ! strcmp (name, "alloca"))
496 || (IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 16
497 && name[0] == '_'
498 && ! strcmp (name, "__builtin_alloca"))))
499 flags |= ECF_MAY_BE_ALLOCA;
501 /* Disregard prefix _, __ or __x. */
502 if (name[0] == '_')
504 if (name[1] == '_' && name[2] == 'x')
505 tname += 3;
506 else if (name[1] == '_')
507 tname += 2;
508 else
509 tname += 1;
512 if (tname[0] == 's')
514 if ((tname[1] == 'e'
515 && (! strcmp (tname, "setjmp")
516 || ! strcmp (tname, "setjmp_syscall")))
517 || (tname[1] == 'i'
518 && ! strcmp (tname, "sigsetjmp"))
519 || (tname[1] == 'a'
520 && ! strcmp (tname, "savectx")))
521 flags |= ECF_RETURNS_TWICE;
523 if (tname[1] == 'i'
524 && ! strcmp (tname, "siglongjmp"))
525 flags |= ECF_LONGJMP;
527 else if ((tname[0] == 'q' && tname[1] == 's'
528 && ! strcmp (tname, "qsetjmp"))
529 || (tname[0] == 'v' && tname[1] == 'f'
530 && ! strcmp (tname, "vfork")))
531 flags |= ECF_RETURNS_TWICE;
533 else if (tname[0] == 'l' && tname[1] == 'o'
534 && ! strcmp (tname, "longjmp"))
535 flags |= ECF_LONGJMP;
538 return flags;
541 /* Return nonzero when tree represent call to longjmp. */
544 setjmp_call_p (tree fndecl)
546 return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
549 /* Return true when exp contains alloca call. */
550 bool
551 alloca_call_p (tree exp)
553 if (TREE_CODE (exp) == CALL_EXPR
554 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
555 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
556 == FUNCTION_DECL)
557 && (special_function_p (TREE_OPERAND (TREE_OPERAND (exp, 0), 0),
558 0) & ECF_MAY_BE_ALLOCA))
559 return true;
560 return false;
563 /* Detect flags (function attributes) from the function decl or type node. */
566 flags_from_decl_or_type (tree exp)
568 int flags = 0;
569 tree type = exp;
571 if (DECL_P (exp))
573 struct cgraph_rtl_info *i = cgraph_rtl_info (exp);
574 type = TREE_TYPE (exp);
576 if (i)
578 if (i->pure_function)
579 flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
580 if (i->const_function)
581 flags |= ECF_CONST | ECF_LIBCALL_BLOCK;
584 /* The function exp may have the `malloc' attribute. */
585 if (DECL_IS_MALLOC (exp))
586 flags |= ECF_MALLOC;
588 /* The function exp may have the `pure' attribute. */
589 if (DECL_IS_PURE (exp))
590 flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
592 if (TREE_NOTHROW (exp))
593 flags |= ECF_NOTHROW;
595 if (TREE_READONLY (exp) && ! TREE_THIS_VOLATILE (exp))
596 flags |= ECF_LIBCALL_BLOCK | ECF_CONST;
598 flags = special_function_p (exp, flags);
600 else if (TYPE_P (exp) && TYPE_READONLY (exp) && ! TREE_THIS_VOLATILE (exp))
601 flags |= ECF_CONST;
603 if (TREE_THIS_VOLATILE (exp))
604 flags |= ECF_NORETURN;
606 /* Mark if the function returns with the stack pointer depressed. We
607 cannot consider it pure or constant in that case. */
608 if (TREE_CODE (type) == FUNCTION_TYPE && TYPE_RETURNS_STACK_DEPRESSED (type))
610 flags |= ECF_SP_DEPRESSED;
611 flags &= ~(ECF_PURE | ECF_CONST | ECF_LIBCALL_BLOCK);
614 return flags;
617 /* Detect flags from a CALL_EXPR. */
620 call_expr_flags (tree t)
622 int flags;
623 tree decl = get_callee_fndecl (t);
625 if (decl)
626 flags = flags_from_decl_or_type (decl);
627 else
629 t = TREE_TYPE (TREE_OPERAND (t, 0));
630 if (t && TREE_CODE (t) == POINTER_TYPE)
631 flags = flags_from_decl_or_type (TREE_TYPE (t));
632 else
633 flags = 0;
636 return flags;
639 /* Precompute all register parameters as described by ARGS, storing values
640 into fields within the ARGS array.
642 NUM_ACTUALS indicates the total number elements in the ARGS array.
644 Set REG_PARM_SEEN if we encounter a register parameter. */
646 static void
647 precompute_register_parameters (int num_actuals, struct arg_data *args, int *reg_parm_seen)
649 int i;
651 *reg_parm_seen = 0;
653 for (i = 0; i < num_actuals; i++)
654 if (args[i].reg != 0 && ! args[i].pass_on_stack)
656 *reg_parm_seen = 1;
658 if (args[i].value == 0)
660 push_temp_slots ();
661 args[i].value = expand_expr (args[i].tree_value, NULL_RTX,
662 VOIDmode, 0);
663 preserve_temp_slots (args[i].value);
664 pop_temp_slots ();
666 /* ANSI doesn't require a sequence point here,
667 but PCC has one, so this will avoid some problems. */
668 emit_queue ();
671 /* If the value is a non-legitimate constant, force it into a
672 pseudo now. TLS symbols sometimes need a call to resolve. */
673 if (CONSTANT_P (args[i].value)
674 && !LEGITIMATE_CONSTANT_P (args[i].value))
675 args[i].value = force_reg (args[i].mode, args[i].value);
677 /* If we are to promote the function arg to a wider mode,
678 do it now. */
680 if (args[i].mode != TYPE_MODE (TREE_TYPE (args[i].tree_value)))
681 args[i].value
682 = convert_modes (args[i].mode,
683 TYPE_MODE (TREE_TYPE (args[i].tree_value)),
684 args[i].value, args[i].unsignedp);
686 /* If the value is expensive, and we are inside an appropriately
687 short loop, put the value into a pseudo and then put the pseudo
688 into the hard reg.
690 For small register classes, also do this if this call uses
691 register parameters. This is to avoid reload conflicts while
692 loading the parameters registers. */
694 if ((! (REG_P (args[i].value)
695 || (GET_CODE (args[i].value) == SUBREG
696 && REG_P (SUBREG_REG (args[i].value)))))
697 && args[i].mode != BLKmode
698 && rtx_cost (args[i].value, SET) > COSTS_N_INSNS (1)
699 && ((SMALL_REGISTER_CLASSES && *reg_parm_seen)
700 || preserve_subexpressions_p ()))
701 args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
705 #ifdef REG_PARM_STACK_SPACE
707 /* The argument list is the property of the called routine and it
708 may clobber it. If the fixed area has been used for previous
709 parameters, we must save and restore it. */
711 static rtx
712 save_fixed_argument_area (int reg_parm_stack_space, rtx argblock, int *low_to_save, int *high_to_save)
714 int low;
715 int high;
717 /* Compute the boundary of the area that needs to be saved, if any. */
718 high = reg_parm_stack_space;
719 #ifdef ARGS_GROW_DOWNWARD
720 high += 1;
721 #endif
722 if (high > highest_outgoing_arg_in_use)
723 high = highest_outgoing_arg_in_use;
725 for (low = 0; low < high; low++)
726 if (stack_usage_map[low] != 0)
728 int num_to_save;
729 enum machine_mode save_mode;
730 int delta;
731 rtx stack_area;
732 rtx save_area;
734 while (stack_usage_map[--high] == 0)
737 *low_to_save = low;
738 *high_to_save = high;
740 num_to_save = high - low + 1;
741 save_mode = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
743 /* If we don't have the required alignment, must do this
744 in BLKmode. */
745 if ((low & (MIN (GET_MODE_SIZE (save_mode),
746 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
747 save_mode = BLKmode;
749 #ifdef ARGS_GROW_DOWNWARD
750 delta = -high;
751 #else
752 delta = low;
753 #endif
754 stack_area = gen_rtx_MEM (save_mode,
755 memory_address (save_mode,
756 plus_constant (argblock,
757 delta)));
759 set_mem_align (stack_area, PARM_BOUNDARY);
760 if (save_mode == BLKmode)
762 save_area = assign_stack_temp (BLKmode, num_to_save, 0);
763 emit_block_move (validize_mem (save_area), stack_area,
764 GEN_INT (num_to_save), BLOCK_OP_CALL_PARM);
766 else
768 save_area = gen_reg_rtx (save_mode);
769 emit_move_insn (save_area, stack_area);
772 return save_area;
775 return NULL_RTX;
778 static void
779 restore_fixed_argument_area (rtx save_area, rtx argblock, int high_to_save, int low_to_save)
781 enum machine_mode save_mode = GET_MODE (save_area);
782 int delta;
783 rtx stack_area;
785 #ifdef ARGS_GROW_DOWNWARD
786 delta = -high_to_save;
787 #else
788 delta = low_to_save;
789 #endif
790 stack_area = gen_rtx_MEM (save_mode,
791 memory_address (save_mode,
792 plus_constant (argblock, delta)));
793 set_mem_align (stack_area, PARM_BOUNDARY);
795 if (save_mode != BLKmode)
796 emit_move_insn (stack_area, save_area);
797 else
798 emit_block_move (stack_area, validize_mem (save_area),
799 GEN_INT (high_to_save - low_to_save + 1),
800 BLOCK_OP_CALL_PARM);
802 #endif /* REG_PARM_STACK_SPACE */
804 /* If any elements in ARGS refer to parameters that are to be passed in
805 registers, but not in memory, and whose alignment does not permit a
806 direct copy into registers. Copy the values into a group of pseudos
807 which we will later copy into the appropriate hard registers.
809 Pseudos for each unaligned argument will be stored into the array
810 args[argnum].aligned_regs. The caller is responsible for deallocating
811 the aligned_regs array if it is nonzero. */
813 static void
814 store_unaligned_arguments_into_pseudos (struct arg_data *args, int num_actuals)
816 int i, j;
818 for (i = 0; i < num_actuals; i++)
819 if (args[i].reg != 0 && ! args[i].pass_on_stack
820 && args[i].mode == BLKmode
821 && (TYPE_ALIGN (TREE_TYPE (args[i].tree_value))
822 < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
824 int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
825 int nregs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
826 int endian_correction = 0;
828 args[i].n_aligned_regs = args[i].partial ? args[i].partial : nregs;
829 args[i].aligned_regs = xmalloc (sizeof (rtx) * args[i].n_aligned_regs);
831 /* Structures smaller than a word are normally aligned to the
832 least significant byte. On a BYTES_BIG_ENDIAN machine,
833 this means we must skip the empty high order bytes when
834 calculating the bit offset. */
835 if (bytes < UNITS_PER_WORD
836 #ifdef BLOCK_REG_PADDING
837 && (BLOCK_REG_PADDING (args[i].mode,
838 TREE_TYPE (args[i].tree_value), 1)
839 == downward)
840 #else
841 && BYTES_BIG_ENDIAN
842 #endif
844 endian_correction = BITS_PER_WORD - bytes * BITS_PER_UNIT;
846 for (j = 0; j < args[i].n_aligned_regs; j++)
848 rtx reg = gen_reg_rtx (word_mode);
849 rtx word = operand_subword_force (args[i].value, j, BLKmode);
850 int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
852 args[i].aligned_regs[j] = reg;
853 word = extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
854 word_mode, word_mode, BITS_PER_WORD);
856 /* There is no need to restrict this code to loading items
857 in TYPE_ALIGN sized hunks. The bitfield instructions can
858 load up entire word sized registers efficiently.
860 ??? This may not be needed anymore.
861 We use to emit a clobber here but that doesn't let later
862 passes optimize the instructions we emit. By storing 0 into
863 the register later passes know the first AND to zero out the
864 bitfield being set in the register is unnecessary. The store
865 of 0 will be deleted as will at least the first AND. */
867 emit_move_insn (reg, const0_rtx);
869 bytes -= bitsize / BITS_PER_UNIT;
870 store_bit_field (reg, bitsize, endian_correction, word_mode,
871 word, BITS_PER_WORD);
876 /* Fill in ARGS_SIZE and ARGS array based on the parameters found in
877 ACTPARMS.
879 NUM_ACTUALS is the total number of parameters.
881 N_NAMED_ARGS is the total number of named arguments.
883 FNDECL is the tree code for the target of this call (if known)
885 ARGS_SO_FAR holds state needed by the target to know where to place
886 the next argument.
888 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
889 for arguments which are passed in registers.
891 OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
892 and may be modified by this routine.
894 OLD_PENDING_ADJ, MUST_PREALLOCATE and FLAGS are pointers to integer
895 flags which may may be modified by this routine.
897 MAY_TAILCALL is cleared if we encounter an invisible pass-by-reference
898 that requires allocation of stack space.
900 CALL_FROM_THUNK_P is true if this call is the jump from a thunk to
901 the thunked-to function. */
903 static void
904 initialize_argument_information (int num_actuals ATTRIBUTE_UNUSED,
905 struct arg_data *args,
906 struct args_size *args_size,
907 int n_named_args ATTRIBUTE_UNUSED,
908 tree actparms, tree fndecl,
909 CUMULATIVE_ARGS *args_so_far,
910 int reg_parm_stack_space,
911 rtx *old_stack_level, int *old_pending_adj,
912 int *must_preallocate, int *ecf_flags,
913 bool *may_tailcall, bool call_from_thunk_p)
915 /* 1 if scanning parms front to back, -1 if scanning back to front. */
916 int inc;
918 /* Count arg position in order args appear. */
919 int argpos;
921 int i;
922 tree p;
924 args_size->constant = 0;
925 args_size->var = 0;
927 /* In this loop, we consider args in the order they are written.
928 We fill up ARGS from the front or from the back if necessary
929 so that in any case the first arg to be pushed ends up at the front. */
931 if (PUSH_ARGS_REVERSED)
933 i = num_actuals - 1, inc = -1;
934 /* In this case, must reverse order of args
935 so that we compute and push the last arg first. */
937 else
939 i = 0, inc = 1;
942 /* I counts args in order (to be) pushed; ARGPOS counts in order written. */
943 for (p = actparms, argpos = 0; p; p = TREE_CHAIN (p), i += inc, argpos++)
945 tree type = TREE_TYPE (TREE_VALUE (p));
946 int unsignedp;
947 enum machine_mode mode;
949 args[i].tree_value = TREE_VALUE (p);
951 /* Replace erroneous argument with constant zero. */
952 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
953 args[i].tree_value = integer_zero_node, type = integer_type_node;
955 /* If TYPE is a transparent union, pass things the way we would
956 pass the first field of the union. We have already verified that
957 the modes are the same. */
958 if (TREE_CODE (type) == UNION_TYPE && TYPE_TRANSPARENT_UNION (type))
959 type = TREE_TYPE (TYPE_FIELDS (type));
961 /* Decide where to pass this arg.
963 args[i].reg is nonzero if all or part is passed in registers.
965 args[i].partial is nonzero if part but not all is passed in registers,
966 and the exact value says how many words are passed in registers.
968 args[i].pass_on_stack is nonzero if the argument must at least be
969 computed on the stack. It may then be loaded back into registers
970 if args[i].reg is nonzero.
972 These decisions are driven by the FUNCTION_... macros and must agree
973 with those made by function.c. */
975 /* See if this argument should be passed by invisible reference. */
976 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
977 || TREE_ADDRESSABLE (type)
978 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
979 || FUNCTION_ARG_PASS_BY_REFERENCE (*args_so_far, TYPE_MODE (type),
980 type, argpos < n_named_args)
981 #endif
984 /* If we're compiling a thunk, pass through invisible
985 references instead of making a copy. */
986 if (call_from_thunk_p
987 #ifdef FUNCTION_ARG_CALLEE_COPIES
988 || (FUNCTION_ARG_CALLEE_COPIES (*args_so_far, TYPE_MODE (type),
989 type, argpos < n_named_args)
990 /* If it's in a register, we must make a copy of it too. */
991 /* ??? Is this a sufficient test? Is there a better one? */
992 && !(TREE_CODE (args[i].tree_value) == VAR_DECL
993 && REG_P (DECL_RTL (args[i].tree_value)))
994 && ! TREE_ADDRESSABLE (type))
995 #endif
998 /* C++ uses a TARGET_EXPR to indicate that we want to make a
999 new object from the argument. If we are passing by
1000 invisible reference, the callee will do that for us, so we
1001 can strip off the TARGET_EXPR. This is not always safe,
1002 but it is safe in the only case where this is a useful
1003 optimization; namely, when the argument is a plain object.
1004 In that case, the frontend is just asking the backend to
1005 make a bitwise copy of the argument. */
1007 if (TREE_CODE (args[i].tree_value) == TARGET_EXPR
1008 && (DECL_P (TREE_OPERAND (args[i].tree_value, 1)))
1009 && ! REG_P (DECL_RTL (TREE_OPERAND (args[i].tree_value, 1))))
1010 args[i].tree_value = TREE_OPERAND (args[i].tree_value, 1);
1012 /* We can't use sibcalls if a callee-copied argument is stored
1013 in the current function's frame. */
1014 if (!call_from_thunk_p
1015 && (!DECL_P (args[i].tree_value)
1016 || !TREE_STATIC (args[i].tree_value)))
1017 *may_tailcall = false;
1019 args[i].tree_value = build1 (ADDR_EXPR,
1020 build_pointer_type (type),
1021 args[i].tree_value);
1022 type = build_pointer_type (type);
1024 else if (TREE_CODE (args[i].tree_value) == TARGET_EXPR)
1026 /* In the V3 C++ ABI, parameters are destroyed in the caller.
1027 We implement this by passing the address of the temporary
1028 rather than expanding it into another allocated slot. */
1029 args[i].tree_value = build1 (ADDR_EXPR,
1030 build_pointer_type (type),
1031 args[i].tree_value);
1032 type = build_pointer_type (type);
1033 *may_tailcall = false;
1035 else
1037 /* We make a copy of the object and pass the address to the
1038 function being called. */
1039 rtx copy;
1041 if (!COMPLETE_TYPE_P (type)
1042 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
1043 || (flag_stack_check && ! STACK_CHECK_BUILTIN
1044 && (0 < compare_tree_int (TYPE_SIZE_UNIT (type),
1045 STACK_CHECK_MAX_VAR_SIZE))))
1047 /* This is a variable-sized object. Make space on the stack
1048 for it. */
1049 rtx size_rtx = expr_size (TREE_VALUE (p));
1051 if (*old_stack_level == 0)
1053 emit_stack_save (SAVE_BLOCK, old_stack_level, NULL_RTX);
1054 *old_pending_adj = pending_stack_adjust;
1055 pending_stack_adjust = 0;
1058 copy = gen_rtx_MEM (BLKmode,
1059 allocate_dynamic_stack_space
1060 (size_rtx, NULL_RTX, TYPE_ALIGN (type)));
1061 set_mem_attributes (copy, type, 1);
1063 else
1064 copy = assign_temp (type, 0, 1, 0);
1066 store_expr (args[i].tree_value, copy, 0);
1067 *ecf_flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
1069 args[i].tree_value = build1 (ADDR_EXPR,
1070 build_pointer_type (type),
1071 make_tree (type, copy));
1072 type = build_pointer_type (type);
1073 *may_tailcall = false;
1077 mode = TYPE_MODE (type);
1078 unsignedp = TYPE_UNSIGNED (type);
1080 if (targetm.calls.promote_function_args (fndecl ? TREE_TYPE (fndecl) : 0))
1081 mode = promote_mode (type, mode, &unsignedp, 1);
1083 args[i].unsignedp = unsignedp;
1084 args[i].mode = mode;
1086 args[i].reg = FUNCTION_ARG (*args_so_far, mode, type,
1087 argpos < n_named_args);
1088 #ifdef FUNCTION_INCOMING_ARG
1089 /* If this is a sibling call and the machine has register windows, the
1090 register window has to be unwinded before calling the routine, so
1091 arguments have to go into the incoming registers. */
1092 args[i].tail_call_reg = FUNCTION_INCOMING_ARG (*args_so_far, mode, type,
1093 argpos < n_named_args);
1094 #else
1095 args[i].tail_call_reg = args[i].reg;
1096 #endif
1098 #ifdef FUNCTION_ARG_PARTIAL_NREGS
1099 if (args[i].reg)
1100 args[i].partial
1101 = FUNCTION_ARG_PARTIAL_NREGS (*args_so_far, mode, type,
1102 argpos < n_named_args);
1103 #endif
1105 args[i].pass_on_stack = MUST_PASS_IN_STACK (mode, type);
1107 /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
1108 it means that we are to pass this arg in the register(s) designated
1109 by the PARALLEL, but also to pass it in the stack. */
1110 if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
1111 && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
1112 args[i].pass_on_stack = 1;
1114 /* If this is an addressable type, we must preallocate the stack
1115 since we must evaluate the object into its final location.
1117 If this is to be passed in both registers and the stack, it is simpler
1118 to preallocate. */
1119 if (TREE_ADDRESSABLE (type)
1120 || (args[i].pass_on_stack && args[i].reg != 0))
1121 *must_preallocate = 1;
1123 /* If this is an addressable type, we cannot pre-evaluate it. Thus,
1124 we cannot consider this function call constant. */
1125 if (TREE_ADDRESSABLE (type))
1126 *ecf_flags &= ~ECF_LIBCALL_BLOCK;
1128 /* Compute the stack-size of this argument. */
1129 if (args[i].reg == 0 || args[i].partial != 0
1130 || reg_parm_stack_space > 0
1131 || args[i].pass_on_stack)
1132 locate_and_pad_parm (mode, type,
1133 #ifdef STACK_PARMS_IN_REG_PARM_AREA
1135 #else
1136 args[i].reg != 0,
1137 #endif
1138 args[i].pass_on_stack ? 0 : args[i].partial,
1139 fndecl, args_size, &args[i].locate);
1140 #ifdef BLOCK_REG_PADDING
1141 else
1142 /* The argument is passed entirely in registers. See at which
1143 end it should be padded. */
1144 args[i].locate.where_pad =
1145 BLOCK_REG_PADDING (mode, type,
1146 int_size_in_bytes (type) <= UNITS_PER_WORD);
1147 #endif
1149 /* Update ARGS_SIZE, the total stack space for args so far. */
1151 args_size->constant += args[i].locate.size.constant;
1152 if (args[i].locate.size.var)
1153 ADD_PARM_SIZE (*args_size, args[i].locate.size.var);
1155 /* Increment ARGS_SO_FAR, which has info about which arg-registers
1156 have been used, etc. */
1158 FUNCTION_ARG_ADVANCE (*args_so_far, TYPE_MODE (type), type,
1159 argpos < n_named_args);
1163 /* Update ARGS_SIZE to contain the total size for the argument block.
1164 Return the original constant component of the argument block's size.
1166 REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
1167 for arguments passed in registers. */
1169 static int
1170 compute_argument_block_size (int reg_parm_stack_space,
1171 struct args_size *args_size,
1172 int preferred_stack_boundary ATTRIBUTE_UNUSED)
1174 int unadjusted_args_size = args_size->constant;
1176 /* For accumulate outgoing args mode we don't need to align, since the frame
1177 will be already aligned. Align to STACK_BOUNDARY in order to prevent
1178 backends from generating misaligned frame sizes. */
1179 if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
1180 preferred_stack_boundary = STACK_BOUNDARY;
1182 /* Compute the actual size of the argument block required. The variable
1183 and constant sizes must be combined, the size may have to be rounded,
1184 and there may be a minimum required size. */
1186 if (args_size->var)
1188 args_size->var = ARGS_SIZE_TREE (*args_size);
1189 args_size->constant = 0;
1191 preferred_stack_boundary /= BITS_PER_UNIT;
1192 if (preferred_stack_boundary > 1)
1194 /* We don't handle this case yet. To handle it correctly we have
1195 to add the delta, round and subtract the delta.
1196 Currently no machine description requires this support. */
1197 if (stack_pointer_delta & (preferred_stack_boundary - 1))
1198 abort ();
1199 args_size->var = round_up (args_size->var, preferred_stack_boundary);
1202 if (reg_parm_stack_space > 0)
1204 args_size->var
1205 = size_binop (MAX_EXPR, args_size->var,
1206 ssize_int (reg_parm_stack_space));
1208 #ifndef OUTGOING_REG_PARM_STACK_SPACE
1209 /* The area corresponding to register parameters is not to count in
1210 the size of the block we need. So make the adjustment. */
1211 args_size->var
1212 = size_binop (MINUS_EXPR, args_size->var,
1213 ssize_int (reg_parm_stack_space));
1214 #endif
1217 else
1219 preferred_stack_boundary /= BITS_PER_UNIT;
1220 if (preferred_stack_boundary < 1)
1221 preferred_stack_boundary = 1;
1222 args_size->constant = (((args_size->constant
1223 + stack_pointer_delta
1224 + preferred_stack_boundary - 1)
1225 / preferred_stack_boundary
1226 * preferred_stack_boundary)
1227 - stack_pointer_delta);
1229 args_size->constant = MAX (args_size->constant,
1230 reg_parm_stack_space);
1232 #ifndef OUTGOING_REG_PARM_STACK_SPACE
1233 args_size->constant -= reg_parm_stack_space;
1234 #endif
1236 return unadjusted_args_size;
1239 /* Precompute parameters as needed for a function call.
1241 FLAGS is mask of ECF_* constants.
1243 NUM_ACTUALS is the number of arguments.
1245 ARGS is an array containing information for each argument; this
1246 routine fills in the INITIAL_VALUE and VALUE fields for each
1247 precomputed argument. */
1249 static void
1250 precompute_arguments (int flags, int num_actuals, struct arg_data *args)
1252 int i;
1254 /* If this is a libcall, then precompute all arguments so that we do not
1255 get extraneous instructions emitted as part of the libcall sequence. */
1256 if ((flags & ECF_LIBCALL_BLOCK) == 0)
1257 return;
1259 for (i = 0; i < num_actuals; i++)
1261 enum machine_mode mode;
1263 /* If this is an addressable type, we cannot pre-evaluate it. */
1264 if (TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value)))
1265 abort ();
1267 args[i].value
1268 = expand_expr (args[i].tree_value, NULL_RTX, VOIDmode, 0);
1270 /* ANSI doesn't require a sequence point here,
1271 but PCC has one, so this will avoid some problems. */
1272 emit_queue ();
1274 args[i].initial_value = args[i].value
1275 = protect_from_queue (args[i].value, 0);
1277 mode = TYPE_MODE (TREE_TYPE (args[i].tree_value));
1278 if (mode != args[i].mode)
1280 args[i].value
1281 = convert_modes (args[i].mode, mode,
1282 args[i].value, args[i].unsignedp);
1283 #if defined(PROMOTE_FUNCTION_MODE) && !defined(PROMOTE_MODE)
1284 /* CSE will replace this only if it contains args[i].value
1285 pseudo, so convert it down to the declared mode using
1286 a SUBREG. */
1287 if (REG_P (args[i].value)
1288 && GET_MODE_CLASS (args[i].mode) == MODE_INT)
1290 args[i].initial_value
1291 = gen_lowpart_SUBREG (mode, args[i].value);
1292 SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1293 SUBREG_PROMOTED_UNSIGNED_SET (args[i].initial_value,
1294 args[i].unsignedp);
1296 #endif
1301 /* Given the current state of MUST_PREALLOCATE and information about
1302 arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
1303 compute and return the final value for MUST_PREALLOCATE. */
1305 static int
1306 finalize_must_preallocate (int must_preallocate, int num_actuals, struct arg_data *args, struct args_size *args_size)
1308 /* See if we have or want to preallocate stack space.
1310 If we would have to push a partially-in-regs parm
1311 before other stack parms, preallocate stack space instead.
1313 If the size of some parm is not a multiple of the required stack
1314 alignment, we must preallocate.
1316 If the total size of arguments that would otherwise create a copy in
1317 a temporary (such as a CALL) is more than half the total argument list
1318 size, preallocation is faster.
1320 Another reason to preallocate is if we have a machine (like the m88k)
1321 where stack alignment is required to be maintained between every
1322 pair of insns, not just when the call is made. However, we assume here
1323 that such machines either do not have push insns (and hence preallocation
1324 would occur anyway) or the problem is taken care of with
1325 PUSH_ROUNDING. */
1327 if (! must_preallocate)
1329 int partial_seen = 0;
1330 int copy_to_evaluate_size = 0;
1331 int i;
1333 for (i = 0; i < num_actuals && ! must_preallocate; i++)
1335 if (args[i].partial > 0 && ! args[i].pass_on_stack)
1336 partial_seen = 1;
1337 else if (partial_seen && args[i].reg == 0)
1338 must_preallocate = 1;
1340 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1341 && (TREE_CODE (args[i].tree_value) == CALL_EXPR
1342 || TREE_CODE (args[i].tree_value) == TARGET_EXPR
1343 || TREE_CODE (args[i].tree_value) == COND_EXPR
1344 || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
1345 copy_to_evaluate_size
1346 += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1349 if (copy_to_evaluate_size * 2 >= args_size->constant
1350 && args_size->constant > 0)
1351 must_preallocate = 1;
1353 return must_preallocate;
1356 /* If we preallocated stack space, compute the address of each argument
1357 and store it into the ARGS array.
1359 We need not ensure it is a valid memory address here; it will be
1360 validized when it is used.
1362 ARGBLOCK is an rtx for the address of the outgoing arguments. */
1364 static void
1365 compute_argument_addresses (struct arg_data *args, rtx argblock, int num_actuals)
1367 if (argblock)
1369 rtx arg_reg = argblock;
1370 int i, arg_offset = 0;
1372 if (GET_CODE (argblock) == PLUS)
1373 arg_reg = XEXP (argblock, 0), arg_offset = INTVAL (XEXP (argblock, 1));
1375 for (i = 0; i < num_actuals; i++)
1377 rtx offset = ARGS_SIZE_RTX (args[i].locate.offset);
1378 rtx slot_offset = ARGS_SIZE_RTX (args[i].locate.slot_offset);
1379 rtx addr;
1381 /* Skip this parm if it will not be passed on the stack. */
1382 if (! args[i].pass_on_stack && args[i].reg != 0)
1383 continue;
1385 if (GET_CODE (offset) == CONST_INT)
1386 addr = plus_constant (arg_reg, INTVAL (offset));
1387 else
1388 addr = gen_rtx_PLUS (Pmode, arg_reg, offset);
1390 addr = plus_constant (addr, arg_offset);
1391 args[i].stack = gen_rtx_MEM (args[i].mode, addr);
1392 set_mem_align (args[i].stack, PARM_BOUNDARY);
1393 set_mem_attributes (args[i].stack,
1394 TREE_TYPE (args[i].tree_value), 1);
1396 if (GET_CODE (slot_offset) == CONST_INT)
1397 addr = plus_constant (arg_reg, INTVAL (slot_offset));
1398 else
1399 addr = gen_rtx_PLUS (Pmode, arg_reg, slot_offset);
1401 addr = plus_constant (addr, arg_offset);
1402 args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
1403 set_mem_align (args[i].stack_slot, PARM_BOUNDARY);
1404 set_mem_attributes (args[i].stack_slot,
1405 TREE_TYPE (args[i].tree_value), 1);
1407 /* Function incoming arguments may overlap with sibling call
1408 outgoing arguments and we cannot allow reordering of reads
1409 from function arguments with stores to outgoing arguments
1410 of sibling calls. */
1411 set_mem_alias_set (args[i].stack, 0);
1412 set_mem_alias_set (args[i].stack_slot, 0);
1417 /* Given a FNDECL and EXP, return an rtx suitable for use as a target address
1418 in a call instruction.
1420 FNDECL is the tree node for the target function. For an indirect call
1421 FNDECL will be NULL_TREE.
1423 ADDR is the operand 0 of CALL_EXPR for this call. */
1425 static rtx
1426 rtx_for_function_call (tree fndecl, tree addr)
1428 rtx funexp;
1430 /* Get the function to call, in the form of RTL. */
1431 if (fndecl)
1433 /* If this is the first use of the function, see if we need to
1434 make an external definition for it. */
1435 if (! TREE_USED (fndecl))
1437 assemble_external (fndecl);
1438 TREE_USED (fndecl) = 1;
1441 /* Get a SYMBOL_REF rtx for the function address. */
1442 funexp = XEXP (DECL_RTL (fndecl), 0);
1444 else
1445 /* Generate an rtx (probably a pseudo-register) for the address. */
1447 push_temp_slots ();
1448 funexp = expand_expr (addr, NULL_RTX, VOIDmode, 0);
1449 pop_temp_slots (); /* FUNEXP can't be BLKmode. */
1450 emit_queue ();
1452 return funexp;
1455 /* Do the register loads required for any wholly-register parms or any
1456 parms which are passed both on the stack and in a register. Their
1457 expressions were already evaluated.
1459 Mark all register-parms as living through the call, putting these USE
1460 insns in the CALL_INSN_FUNCTION_USAGE field.
1462 When IS_SIBCALL, perform the check_sibcall_overlap_argument_overlap
1463 checking, setting *SIBCALL_FAILURE if appropriate. */
1465 static void
1466 load_register_parameters (struct arg_data *args, int num_actuals,
1467 rtx *call_fusage, int flags, int is_sibcall,
1468 int *sibcall_failure)
1470 int i, j;
1472 for (i = 0; i < num_actuals; i++)
1474 rtx reg = ((flags & ECF_SIBCALL)
1475 ? args[i].tail_call_reg : args[i].reg);
1476 if (reg)
1478 int partial = args[i].partial;
1479 int nregs;
1480 int size = 0;
1481 rtx before_arg = get_last_insn ();
1482 /* Set to non-negative if must move a word at a time, even if just
1483 one word (e.g, partial == 1 && mode == DFmode). Set to -1 if
1484 we just use a normal move insn. This value can be zero if the
1485 argument is a zero size structure with no fields. */
1486 nregs = -1;
1487 if (partial)
1488 nregs = partial;
1489 else if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode)
1491 size = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1492 nregs = (size + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
1494 else
1495 size = GET_MODE_SIZE (args[i].mode);
1497 /* Handle calls that pass values in multiple non-contiguous
1498 locations. The Irix 6 ABI has examples of this. */
1500 if (GET_CODE (reg) == PARALLEL)
1502 tree type = TREE_TYPE (args[i].tree_value);
1503 emit_group_load (reg, args[i].value, type,
1504 int_size_in_bytes (type));
1507 /* If simple case, just do move. If normal partial, store_one_arg
1508 has already loaded the register for us. In all other cases,
1509 load the register(s) from memory. */
1511 else if (nregs == -1)
1513 emit_move_insn (reg, args[i].value);
1514 #ifdef BLOCK_REG_PADDING
1515 /* Handle case where we have a value that needs shifting
1516 up to the msb. eg. a QImode value and we're padding
1517 upward on a BYTES_BIG_ENDIAN machine. */
1518 if (size < UNITS_PER_WORD
1519 && (args[i].locate.where_pad
1520 == (BYTES_BIG_ENDIAN ? upward : downward)))
1522 rtx x;
1523 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
1525 /* Assigning REG here rather than a temp makes CALL_FUSAGE
1526 report the whole reg as used. Strictly speaking, the
1527 call only uses SIZE bytes at the msb end, but it doesn't
1528 seem worth generating rtl to say that. */
1529 reg = gen_rtx_REG (word_mode, REGNO (reg));
1530 x = expand_shift (LSHIFT_EXPR, word_mode, reg,
1531 build_int_2 (shift, 0), reg, 1);
1532 if (x != reg)
1533 emit_move_insn (reg, x);
1535 #endif
1538 /* If we have pre-computed the values to put in the registers in
1539 the case of non-aligned structures, copy them in now. */
1541 else if (args[i].n_aligned_regs != 0)
1542 for (j = 0; j < args[i].n_aligned_regs; j++)
1543 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
1544 args[i].aligned_regs[j]);
1546 else if (partial == 0 || args[i].pass_on_stack)
1548 rtx mem = validize_mem (args[i].value);
1550 /* Handle a BLKmode that needs shifting. */
1551 if (nregs == 1 && size < UNITS_PER_WORD
1552 #ifdef BLOCK_REG_PADDING
1553 && args[i].locate.where_pad == downward
1554 #else
1555 && BYTES_BIG_ENDIAN
1556 #endif
1559 rtx tem = operand_subword_force (mem, 0, args[i].mode);
1560 rtx ri = gen_rtx_REG (word_mode, REGNO (reg));
1561 rtx x = gen_reg_rtx (word_mode);
1562 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
1563 enum tree_code dir = BYTES_BIG_ENDIAN ? RSHIFT_EXPR
1564 : LSHIFT_EXPR;
1566 emit_move_insn (x, tem);
1567 x = expand_shift (dir, word_mode, x,
1568 build_int_2 (shift, 0), ri, 1);
1569 if (x != ri)
1570 emit_move_insn (ri, x);
1572 else
1573 move_block_to_reg (REGNO (reg), mem, nregs, args[i].mode);
1576 /* When a parameter is a block, and perhaps in other cases, it is
1577 possible that it did a load from an argument slot that was
1578 already clobbered. */
1579 if (is_sibcall
1580 && check_sibcall_argument_overlap (before_arg, &args[i], 0))
1581 *sibcall_failure = 1;
1583 /* Handle calls that pass values in multiple non-contiguous
1584 locations. The Irix 6 ABI has examples of this. */
1585 if (GET_CODE (reg) == PARALLEL)
1586 use_group_regs (call_fusage, reg);
1587 else if (nregs == -1)
1588 use_reg (call_fusage, reg);
1589 else
1590 use_regs (call_fusage, REGNO (reg), nregs == 0 ? 1 : nregs);
1595 /* We need to pop PENDING_STACK_ADJUST bytes. But, if the arguments
1596 wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
1597 bytes, then we would need to push some additional bytes to pad the
1598 arguments. So, we compute an adjust to the stack pointer for an
1599 amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
1600 bytes. Then, when the arguments are pushed the stack will be perfectly
1601 aligned. ARGS_SIZE->CONSTANT is set to the number of bytes that should
1602 be popped after the call. Returns the adjustment. */
1604 static int
1605 combine_pending_stack_adjustment_and_call (int unadjusted_args_size,
1606 struct args_size *args_size,
1607 int preferred_unit_stack_boundary)
1609 /* The number of bytes to pop so that the stack will be
1610 under-aligned by UNADJUSTED_ARGS_SIZE bytes. */
1611 HOST_WIDE_INT adjustment;
1612 /* The alignment of the stack after the arguments are pushed, if we
1613 just pushed the arguments without adjust the stack here. */
1614 HOST_WIDE_INT unadjusted_alignment;
1616 unadjusted_alignment
1617 = ((stack_pointer_delta + unadjusted_args_size)
1618 % preferred_unit_stack_boundary);
1620 /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
1621 as possible -- leaving just enough left to cancel out the
1622 UNADJUSTED_ALIGNMENT. In other words, we want to ensure that the
1623 PENDING_STACK_ADJUST is non-negative, and congruent to
1624 -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY. */
1626 /* Begin by trying to pop all the bytes. */
1627 unadjusted_alignment
1628 = (unadjusted_alignment
1629 - (pending_stack_adjust % preferred_unit_stack_boundary));
1630 adjustment = pending_stack_adjust;
1631 /* Push enough additional bytes that the stack will be aligned
1632 after the arguments are pushed. */
1633 if (preferred_unit_stack_boundary > 1)
1635 if (unadjusted_alignment > 0)
1636 adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
1637 else
1638 adjustment += unadjusted_alignment;
1641 /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
1642 bytes after the call. The right number is the entire
1643 PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
1644 by the arguments in the first place. */
1645 args_size->constant
1646 = pending_stack_adjust - adjustment + unadjusted_args_size;
1648 return adjustment;
1651 /* Scan X expression if it does not dereference any argument slots
1652 we already clobbered by tail call arguments (as noted in stored_args_map
1653 bitmap).
1654 Return nonzero if X expression dereferences such argument slots,
1655 zero otherwise. */
1657 static int
1658 check_sibcall_argument_overlap_1 (rtx x)
1660 RTX_CODE code;
1661 int i, j;
1662 unsigned int k;
1663 const char *fmt;
1665 if (x == NULL_RTX)
1666 return 0;
1668 code = GET_CODE (x);
1670 if (code == MEM)
1672 if (XEXP (x, 0) == current_function_internal_arg_pointer)
1673 i = 0;
1674 else if (GET_CODE (XEXP (x, 0)) == PLUS
1675 && XEXP (XEXP (x, 0), 0) ==
1676 current_function_internal_arg_pointer
1677 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
1678 i = INTVAL (XEXP (XEXP (x, 0), 1));
1679 else
1680 return 0;
1682 #ifdef ARGS_GROW_DOWNWARD
1683 i = -i - GET_MODE_SIZE (GET_MODE (x));
1684 #endif
1686 for (k = 0; k < GET_MODE_SIZE (GET_MODE (x)); k++)
1687 if (i + k < stored_args_map->n_bits
1688 && TEST_BIT (stored_args_map, i + k))
1689 return 1;
1691 return 0;
1694 /* Scan all subexpressions. */
1695 fmt = GET_RTX_FORMAT (code);
1696 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
1698 if (*fmt == 'e')
1700 if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
1701 return 1;
1703 else if (*fmt == 'E')
1705 for (j = 0; j < XVECLEN (x, i); j++)
1706 if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
1707 return 1;
1710 return 0;
1713 /* Scan sequence after INSN if it does not dereference any argument slots
1714 we already clobbered by tail call arguments (as noted in stored_args_map
1715 bitmap). If MARK_STORED_ARGS_MAP, add stack slots for ARG to
1716 stored_args_map bitmap afterwards (when ARG is a register MARK_STORED_ARGS_MAP
1717 should be 0). Return nonzero if sequence after INSN dereferences such argument
1718 slots, zero otherwise. */
1720 static int
1721 check_sibcall_argument_overlap (rtx insn, struct arg_data *arg, int mark_stored_args_map)
1723 int low, high;
1725 if (insn == NULL_RTX)
1726 insn = get_insns ();
1727 else
1728 insn = NEXT_INSN (insn);
1730 for (; insn; insn = NEXT_INSN (insn))
1731 if (INSN_P (insn)
1732 && check_sibcall_argument_overlap_1 (PATTERN (insn)))
1733 break;
1735 if (mark_stored_args_map)
1737 #ifdef ARGS_GROW_DOWNWARD
1738 low = -arg->locate.slot_offset.constant - arg->locate.size.constant;
1739 #else
1740 low = arg->locate.slot_offset.constant;
1741 #endif
1743 for (high = low + arg->locate.size.constant; low < high; low++)
1744 SET_BIT (stored_args_map, low);
1746 return insn != NULL_RTX;
1749 static tree
1750 fix_unsafe_tree (tree t)
1752 switch (unsafe_for_reeval (t))
1754 case 0: /* Safe. */
1755 break;
1757 case 1: /* Mildly unsafe. */
1758 t = unsave_expr (t);
1759 break;
1761 case 2: /* Wildly unsafe. */
1763 tree var = build_decl (VAR_DECL, NULL_TREE,
1764 TREE_TYPE (t));
1765 SET_DECL_RTL (var,
1766 expand_expr (t, NULL_RTX, VOIDmode, EXPAND_NORMAL));
1767 t = var;
1769 break;
1771 default:
1772 abort ();
1774 return t;
1778 /* If function value *VALUE was returned at the most significant end of a
1779 register, shift it towards the least significant end and convert it to
1780 TYPE's mode. Return true and update *VALUE if some action was needed.
1782 TYPE is the type of the function's return value, which is known not
1783 to have mode BLKmode. */
1785 static bool
1786 shift_returned_value (tree type, rtx *value)
1788 if (targetm.calls.return_in_msb (type))
1790 HOST_WIDE_INT shift;
1792 shift = (GET_MODE_BITSIZE (GET_MODE (*value))
1793 - BITS_PER_UNIT * int_size_in_bytes (type));
1794 if (shift > 0)
1796 /* Shift the value into the low part of the register. */
1797 *value = expand_binop (GET_MODE (*value), lshr_optab, *value,
1798 GEN_INT (shift), 0, 1, OPTAB_WIDEN);
1800 /* Truncate it to the type's mode, or its integer equivalent.
1801 This is subject to TRULY_NOOP_TRUNCATION. */
1802 *value = convert_to_mode (int_mode_for_mode (TYPE_MODE (type)),
1803 *value, 0);
1805 /* Now convert it to the final form. */
1806 *value = gen_lowpart (TYPE_MODE (type), *value);
1807 return true;
1810 return false;
1813 /* Remove all REG_EQUIV notes found in the insn chain. */
1815 static void
1816 purge_reg_equiv_notes (void)
1818 rtx insn;
1820 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1822 while (1)
1824 rtx note = find_reg_note (insn, REG_EQUIV, 0);
1825 if (note)
1827 /* Remove the note and keep looking at the notes for
1828 this insn. */
1829 remove_note (insn, note);
1830 continue;
1832 break;
1837 /* Clear RTX_UNCHANGING_P flag of incoming argument MEMs. */
1839 static void
1840 purge_mem_unchanging_flag (rtx x)
1842 RTX_CODE code;
1843 int i, j;
1844 const char *fmt;
1846 if (x == NULL_RTX)
1847 return;
1849 code = GET_CODE (x);
1851 if (code == MEM)
1853 if (RTX_UNCHANGING_P (x)
1854 && (XEXP (x, 0) == current_function_internal_arg_pointer
1855 || (GET_CODE (XEXP (x, 0)) == PLUS
1856 && XEXP (XEXP (x, 0), 0) ==
1857 current_function_internal_arg_pointer
1858 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
1859 RTX_UNCHANGING_P (x) = 0;
1860 return;
1863 /* Scan all subexpressions. */
1864 fmt = GET_RTX_FORMAT (code);
1865 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
1867 if (*fmt == 'e')
1868 purge_mem_unchanging_flag (XEXP (x, i));
1869 else if (*fmt == 'E')
1870 for (j = 0; j < XVECLEN (x, i); j++)
1871 purge_mem_unchanging_flag (XVECEXP (x, i, j));
1876 /* Generate all the code for a function call
1877 and return an rtx for its value.
1878 Store the value in TARGET (specified as an rtx) if convenient.
1879 If the value is stored in TARGET then TARGET is returned.
1880 If IGNORE is nonzero, then we ignore the value of the function call. */
1883 expand_call (tree exp, rtx target, int ignore)
1885 /* Nonzero if we are currently expanding a call. */
1886 static int currently_expanding_call = 0;
1888 /* List of actual parameters. */
1889 tree actparms = TREE_OPERAND (exp, 1);
1890 /* RTX for the function to be called. */
1891 rtx funexp;
1892 /* Sequence of insns to perform a normal "call". */
1893 rtx normal_call_insns = NULL_RTX;
1894 /* Sequence of insns to perform a tail "call". */
1895 rtx tail_call_insns = NULL_RTX;
1896 /* Data type of the function. */
1897 tree funtype;
1898 tree type_arg_types;
1899 /* Declaration of the function being called,
1900 or 0 if the function is computed (not known by name). */
1901 tree fndecl = 0;
1902 /* The type of the function being called. */
1903 tree fntype;
1904 bool try_tail_call = CALL_EXPR_TAILCALL (exp);
1905 int pass;
1907 /* Register in which non-BLKmode value will be returned,
1908 or 0 if no value or if value is BLKmode. */
1909 rtx valreg;
1910 /* Address where we should return a BLKmode value;
1911 0 if value not BLKmode. */
1912 rtx structure_value_addr = 0;
1913 /* Nonzero if that address is being passed by treating it as
1914 an extra, implicit first parameter. Otherwise,
1915 it is passed by being copied directly into struct_value_rtx. */
1916 int structure_value_addr_parm = 0;
1917 /* Size of aggregate value wanted, or zero if none wanted
1918 or if we are using the non-reentrant PCC calling convention
1919 or expecting the value in registers. */
1920 HOST_WIDE_INT struct_value_size = 0;
1921 /* Nonzero if called function returns an aggregate in memory PCC style,
1922 by returning the address of where to find it. */
1923 int pcc_struct_value = 0;
1924 rtx struct_value = 0;
1926 /* Number of actual parameters in this call, including struct value addr. */
1927 int num_actuals;
1928 /* Number of named args. Args after this are anonymous ones
1929 and they must all go on the stack. */
1930 int n_named_args;
1932 /* Vector of information about each argument.
1933 Arguments are numbered in the order they will be pushed,
1934 not the order they are written. */
1935 struct arg_data *args;
1937 /* Total size in bytes of all the stack-parms scanned so far. */
1938 struct args_size args_size;
1939 struct args_size adjusted_args_size;
1940 /* Size of arguments before any adjustments (such as rounding). */
1941 int unadjusted_args_size;
1942 /* Data on reg parms scanned so far. */
1943 CUMULATIVE_ARGS args_so_far;
1944 /* Nonzero if a reg parm has been scanned. */
1945 int reg_parm_seen;
1946 /* Nonzero if this is an indirect function call. */
1948 /* Nonzero if we must avoid push-insns in the args for this call.
1949 If stack space is allocated for register parameters, but not by the
1950 caller, then it is preallocated in the fixed part of the stack frame.
1951 So the entire argument block must then be preallocated (i.e., we
1952 ignore PUSH_ROUNDING in that case). */
1954 int must_preallocate = !PUSH_ARGS;
1956 /* Size of the stack reserved for parameter registers. */
1957 int reg_parm_stack_space = 0;
1959 /* Address of space preallocated for stack parms
1960 (on machines that lack push insns), or 0 if space not preallocated. */
1961 rtx argblock = 0;
1963 /* Mask of ECF_ flags. */
1964 int flags = 0;
1965 #ifdef REG_PARM_STACK_SPACE
1966 /* Define the boundary of the register parm stack space that needs to be
1967 saved, if any. */
1968 int low_to_save, high_to_save;
1969 rtx save_area = 0; /* Place that it is saved */
1970 #endif
1972 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
1973 char *initial_stack_usage_map = stack_usage_map;
1975 int old_stack_allocated;
1977 /* State variables to track stack modifications. */
1978 rtx old_stack_level = 0;
1979 int old_stack_arg_under_construction = 0;
1980 int old_pending_adj = 0;
1981 int old_inhibit_defer_pop = inhibit_defer_pop;
1983 /* Some stack pointer alterations we make are performed via
1984 allocate_dynamic_stack_space. This modifies the stack_pointer_delta,
1985 which we then also need to save/restore along the way. */
1986 int old_stack_pointer_delta = 0;
1988 rtx call_fusage;
1989 tree p = TREE_OPERAND (exp, 0);
1990 tree addr = TREE_OPERAND (exp, 0);
1991 int i;
1992 /* The alignment of the stack, in bits. */
1993 HOST_WIDE_INT preferred_stack_boundary;
1994 /* The alignment of the stack, in bytes. */
1995 HOST_WIDE_INT preferred_unit_stack_boundary;
1996 /* The static chain value to use for this call. */
1997 rtx static_chain_value;
1998 /* See if this is "nothrow" function call. */
1999 if (TREE_NOTHROW (exp))
2000 flags |= ECF_NOTHROW;
2002 /* See if we can find a DECL-node for the actual function, and get the
2003 function attributes (flags) from the function decl or type node. */
2004 fndecl = get_callee_fndecl (exp);
2005 if (fndecl)
2007 fntype = TREE_TYPE (fndecl);
2008 flags |= flags_from_decl_or_type (fndecl);
2010 else
2012 fntype = TREE_TYPE (TREE_TYPE (p));
2013 flags |= flags_from_decl_or_type (fntype);
2016 struct_value = targetm.calls.struct_value_rtx (fntype, 0);
2018 /* Warn if this value is an aggregate type,
2019 regardless of which calling convention we are using for it. */
2020 if (warn_aggregate_return && AGGREGATE_TYPE_P (TREE_TYPE (exp)))
2021 warning ("function call has aggregate value");
2023 /* If the result of a pure or const function call is ignored (or void),
2024 and none of its arguments are volatile, we can avoid expanding the
2025 call and just evaluate the arguments for side-effects. */
2026 if ((flags & (ECF_CONST | ECF_PURE))
2027 && (ignore || target == const0_rtx
2028 || TYPE_MODE (TREE_TYPE (exp)) == VOIDmode))
2030 bool volatilep = false;
2031 tree arg;
2033 for (arg = actparms; arg; arg = TREE_CHAIN (arg))
2034 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
2036 volatilep = true;
2037 break;
2040 if (! volatilep)
2042 for (arg = actparms; arg; arg = TREE_CHAIN (arg))
2043 expand_expr (TREE_VALUE (arg), const0_rtx,
2044 VOIDmode, EXPAND_NORMAL);
2045 return const0_rtx;
2049 #ifdef REG_PARM_STACK_SPACE
2050 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
2051 #endif
2053 #ifndef OUTGOING_REG_PARM_STACK_SPACE
2054 if (reg_parm_stack_space > 0 && PUSH_ARGS)
2055 must_preallocate = 1;
2056 #endif
2058 /* Set up a place to return a structure. */
2060 /* Cater to broken compilers. */
2061 if (aggregate_value_p (exp, fndecl))
2063 /* This call returns a big structure. */
2064 flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
2066 #ifdef PCC_STATIC_STRUCT_RETURN
2068 pcc_struct_value = 1;
2070 #else /* not PCC_STATIC_STRUCT_RETURN */
2072 struct_value_size = int_size_in_bytes (TREE_TYPE (exp));
2074 if (CALL_EXPR_HAS_RETURN_SLOT_ADDR (exp))
2076 /* The structure value address arg is already in actparms.
2077 Pull it out. It might be nice to just leave it there, but
2078 we need to set structure_value_addr. */
2079 tree return_arg = TREE_VALUE (actparms);
2080 actparms = TREE_CHAIN (actparms);
2081 structure_value_addr = expand_expr (return_arg, NULL_RTX,
2082 VOIDmode, EXPAND_NORMAL);
2084 else if (target && MEM_P (target))
2085 structure_value_addr = XEXP (target, 0);
2086 else
2088 /* For variable-sized objects, we must be called with a target
2089 specified. If we were to allocate space on the stack here,
2090 we would have no way of knowing when to free it. */
2091 rtx d = assign_temp (TREE_TYPE (exp), 1, 1, 1);
2093 mark_temp_addr_taken (d);
2094 structure_value_addr = XEXP (d, 0);
2095 target = 0;
2098 #endif /* not PCC_STATIC_STRUCT_RETURN */
2101 /* Figure out the amount to which the stack should be aligned. */
2102 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
2103 if (fndecl)
2105 struct cgraph_rtl_info *i = cgraph_rtl_info (fndecl);
2106 if (i && i->preferred_incoming_stack_boundary)
2107 preferred_stack_boundary = i->preferred_incoming_stack_boundary;
2110 /* Operand 0 is a pointer-to-function; get the type of the function. */
2111 funtype = TREE_TYPE (addr);
2112 if (! POINTER_TYPE_P (funtype))
2113 abort ();
2114 funtype = TREE_TYPE (funtype);
2116 /* Munge the tree to split complex arguments into their imaginary
2117 and real parts. */
2118 if (targetm.calls.split_complex_arg)
2120 type_arg_types = split_complex_types (TYPE_ARG_TYPES (funtype));
2121 actparms = split_complex_values (actparms);
2123 else
2124 type_arg_types = TYPE_ARG_TYPES (funtype);
2126 if (flags & ECF_MAY_BE_ALLOCA)
2127 current_function_calls_alloca = 1;
2129 /* If struct_value_rtx is 0, it means pass the address
2130 as if it were an extra parameter. */
2131 if (structure_value_addr && struct_value == 0)
2133 /* If structure_value_addr is a REG other than
2134 virtual_outgoing_args_rtx, we can use always use it. If it
2135 is not a REG, we must always copy it into a register.
2136 If it is virtual_outgoing_args_rtx, we must copy it to another
2137 register in some cases. */
2138 rtx temp = (!REG_P (structure_value_addr)
2139 || (ACCUMULATE_OUTGOING_ARGS
2140 && stack_arg_under_construction
2141 && structure_value_addr == virtual_outgoing_args_rtx)
2142 ? copy_addr_to_reg (convert_memory_address
2143 (Pmode, structure_value_addr))
2144 : structure_value_addr);
2146 actparms
2147 = tree_cons (error_mark_node,
2148 make_tree (build_pointer_type (TREE_TYPE (funtype)),
2149 temp),
2150 actparms);
2151 structure_value_addr_parm = 1;
2154 /* Count the arguments and set NUM_ACTUALS. */
2155 for (p = actparms, num_actuals = 0; p; p = TREE_CHAIN (p))
2156 num_actuals++;
2158 /* Compute number of named args.
2159 First, do a raw count of the args for INIT_CUMULATIVE_ARGS. */
2161 if (type_arg_types != 0)
2162 n_named_args
2163 = (list_length (type_arg_types)
2164 /* Count the struct value address, if it is passed as a parm. */
2165 + structure_value_addr_parm);
2166 else
2167 /* If we know nothing, treat all args as named. */
2168 n_named_args = num_actuals;
2170 /* Start updating where the next arg would go.
2172 On some machines (such as the PA) indirect calls have a different
2173 calling convention than normal calls. The fourth argument in
2174 INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
2175 or not. */
2176 INIT_CUMULATIVE_ARGS (args_so_far, funtype, NULL_RTX, fndecl, n_named_args);
2178 /* Now possibly adjust the number of named args.
2179 Normally, don't include the last named arg if anonymous args follow.
2180 We do include the last named arg if
2181 targetm.calls.strict_argument_naming() returns nonzero.
2182 (If no anonymous args follow, the result of list_length is actually
2183 one too large. This is harmless.)
2185 If targetm.calls.pretend_outgoing_varargs_named() returns
2186 nonzero, and targetm.calls.strict_argument_naming() returns zero,
2187 this machine will be able to place unnamed args that were passed
2188 in registers into the stack. So treat all args as named. This
2189 allows the insns emitting for a specific argument list to be
2190 independent of the function declaration.
2192 If targetm.calls.pretend_outgoing_varargs_named() returns zero,
2193 we do not have any reliable way to pass unnamed args in
2194 registers, so we must force them into memory. */
2196 if (type_arg_types != 0
2197 && targetm.calls.strict_argument_naming (&args_so_far))
2199 else if (type_arg_types != 0
2200 && ! targetm.calls.pretend_outgoing_varargs_named (&args_so_far))
2201 /* Don't include the last named arg. */
2202 --n_named_args;
2203 else
2204 /* Treat all args as named. */
2205 n_named_args = num_actuals;
2207 /* Make a vector to hold all the information about each arg. */
2208 args = alloca (num_actuals * sizeof (struct arg_data));
2209 memset (args, 0, num_actuals * sizeof (struct arg_data));
2211 /* Build up entries in the ARGS array, compute the size of the
2212 arguments into ARGS_SIZE, etc. */
2213 initialize_argument_information (num_actuals, args, &args_size,
2214 n_named_args, actparms, fndecl,
2215 &args_so_far, reg_parm_stack_space,
2216 &old_stack_level, &old_pending_adj,
2217 &must_preallocate, &flags,
2218 &try_tail_call, CALL_FROM_THUNK_P (exp));
2220 if (args_size.var)
2222 /* If this function requires a variable-sized argument list, don't
2223 try to make a cse'able block for this call. We may be able to
2224 do this eventually, but it is too complicated to keep track of
2225 what insns go in the cse'able block and which don't. */
2227 flags &= ~ECF_LIBCALL_BLOCK;
2228 must_preallocate = 1;
2231 /* Now make final decision about preallocating stack space. */
2232 must_preallocate = finalize_must_preallocate (must_preallocate,
2233 num_actuals, args,
2234 &args_size);
2236 /* If the structure value address will reference the stack pointer, we
2237 must stabilize it. We don't need to do this if we know that we are
2238 not going to adjust the stack pointer in processing this call. */
2240 if (structure_value_addr
2241 && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
2242 || reg_mentioned_p (virtual_outgoing_args_rtx,
2243 structure_value_addr))
2244 && (args_size.var
2245 || (!ACCUMULATE_OUTGOING_ARGS && args_size.constant)))
2246 structure_value_addr = copy_to_reg (structure_value_addr);
2248 /* Tail calls can make things harder to debug, and we're traditionally
2249 pushed these optimizations into -O2. Don't try if we're already
2250 expanding a call, as that means we're an argument. Don't try if
2251 there's cleanups, as we know there's code to follow the call.
2253 If rtx_equal_function_value_matters is false, that means we've
2254 finished with regular parsing. Which means that some of the
2255 machinery we use to generate tail-calls is no longer in place.
2256 This is most often true of sjlj-exceptions, which we couldn't
2257 tail-call to anyway.
2259 If current_nesting_level () == 0, we're being called after
2260 the function body has been expanded. This can happen when
2261 setting up trampolines in expand_function_end. */
2262 if (currently_expanding_call++ != 0
2263 || !flag_optimize_sibling_calls
2264 || !rtx_equal_function_value_matters
2265 || current_nesting_level () == 0
2266 || any_pending_cleanups ()
2267 || args_size.var
2268 || lookup_stmt_eh_region (exp) >= 0)
2269 try_tail_call = 0;
2271 /* Rest of purposes for tail call optimizations to fail. */
2272 if (
2273 #ifdef HAVE_sibcall_epilogue
2274 !HAVE_sibcall_epilogue
2275 #else
2277 #endif
2278 || !try_tail_call
2279 /* Doing sibling call optimization needs some work, since
2280 structure_value_addr can be allocated on the stack.
2281 It does not seem worth the effort since few optimizable
2282 sibling calls will return a structure. */
2283 || structure_value_addr != NULL_RTX
2284 /* Check whether the target is able to optimize the call
2285 into a sibcall. */
2286 || !targetm.function_ok_for_sibcall (fndecl, exp)
2287 /* Functions that do not return exactly once may not be sibcall
2288 optimized. */
2289 || (flags & (ECF_RETURNS_TWICE | ECF_LONGJMP | ECF_NORETURN))
2290 || TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (addr)))
2291 /* If the called function is nested in the current one, it might access
2292 some of the caller's arguments, but could clobber them beforehand if
2293 the argument areas are shared. */
2294 || (fndecl && decl_function_context (fndecl) == current_function_decl)
2295 /* If this function requires more stack slots than the current
2296 function, we cannot change it into a sibling call. */
2297 || args_size.constant > current_function_args_size
2298 /* If the callee pops its own arguments, then it must pop exactly
2299 the same number of arguments as the current function. */
2300 || (RETURN_POPS_ARGS (fndecl, funtype, args_size.constant)
2301 != RETURN_POPS_ARGS (current_function_decl,
2302 TREE_TYPE (current_function_decl),
2303 current_function_args_size))
2304 || !lang_hooks.decls.ok_for_sibcall (fndecl))
2305 try_tail_call = 0;
2307 if (try_tail_call)
2309 int end, inc;
2310 actparms = NULL_TREE;
2311 /* Ok, we're going to give the tail call the old college try.
2312 This means we're going to evaluate the function arguments
2313 up to three times. There are two degrees of badness we can
2314 encounter, those that can be unsaved and those that can't.
2315 (See unsafe_for_reeval commentary for details.)
2317 Generate a new argument list. Pass safe arguments through
2318 unchanged. For the easy badness wrap them in UNSAVE_EXPRs.
2319 For hard badness, evaluate them now and put their resulting
2320 rtx in a temporary VAR_DECL.
2322 initialize_argument_information has ordered the array for the
2323 order to be pushed, and we must remember this when reconstructing
2324 the original argument order. */
2326 if (PUSH_ARGS_REVERSED)
2328 inc = 1;
2329 i = 0;
2330 end = num_actuals;
2332 else
2334 inc = -1;
2335 i = num_actuals - 1;
2336 end = -1;
2339 for (; i != end; i += inc)
2341 args[i].tree_value = fix_unsafe_tree (args[i].tree_value);
2343 /* Do the same for the function address if it is an expression. */
2344 if (!fndecl)
2345 addr = fix_unsafe_tree (addr);
2346 /* Expanding one of those dangerous arguments could have added
2347 cleanups, but otherwise give it a whirl. */
2348 if (any_pending_cleanups ())
2349 try_tail_call = 0;
2353 /* Ensure current function's preferred stack boundary is at least
2354 what we need. We don't have to increase alignment for recursive
2355 functions. */
2356 if (cfun->preferred_stack_boundary < preferred_stack_boundary
2357 && fndecl != current_function_decl)
2358 cfun->preferred_stack_boundary = preferred_stack_boundary;
2359 if (fndecl == current_function_decl)
2360 cfun->recursive_call_emit = true;
2362 preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
2364 /* We want to make two insn chains; one for a sibling call, the other
2365 for a normal call. We will select one of the two chains after
2366 initial RTL generation is complete. */
2367 for (pass = try_tail_call ? 0 : 1; pass < 2; pass++)
2369 int sibcall_failure = 0;
2370 /* We want to emit any pending stack adjustments before the tail
2371 recursion "call". That way we know any adjustment after the tail
2372 recursion call can be ignored if we indeed use the tail
2373 call expansion. */
2374 int save_pending_stack_adjust = 0;
2375 int save_stack_pointer_delta = 0;
2376 rtx insns;
2377 rtx before_call, next_arg_reg;
2379 if (pass == 0)
2381 /* Emit any queued insns now; otherwise they would end up in
2382 only one of the alternates. */
2383 emit_queue ();
2385 /* State variables we need to save and restore between
2386 iterations. */
2387 save_pending_stack_adjust = pending_stack_adjust;
2388 save_stack_pointer_delta = stack_pointer_delta;
2390 if (pass)
2391 flags &= ~ECF_SIBCALL;
2392 else
2393 flags |= ECF_SIBCALL;
2395 /* Other state variables that we must reinitialize each time
2396 through the loop (that are not initialized by the loop itself). */
2397 argblock = 0;
2398 call_fusage = 0;
2400 /* Start a new sequence for the normal call case.
2402 From this point on, if the sibling call fails, we want to set
2403 sibcall_failure instead of continuing the loop. */
2404 start_sequence ();
2406 if (pass == 0)
2408 /* We know at this point that there are not currently any
2409 pending cleanups. If, however, in the process of evaluating
2410 the arguments we were to create some, we'll need to be
2411 able to get rid of them. */
2412 expand_start_target_temps ();
2415 /* Don't let pending stack adjusts add up to too much.
2416 Also, do all pending adjustments now if there is any chance
2417 this might be a call to alloca or if we are expanding a sibling
2418 call sequence or if we are calling a function that is to return
2419 with stack pointer depressed. */
2420 if (pending_stack_adjust >= 32
2421 || (pending_stack_adjust > 0
2422 && (flags & (ECF_MAY_BE_ALLOCA | ECF_SP_DEPRESSED)))
2423 || pass == 0)
2424 do_pending_stack_adjust ();
2426 /* When calling a const function, we must pop the stack args right away,
2427 so that the pop is deleted or moved with the call. */
2428 if (pass && (flags & ECF_LIBCALL_BLOCK))
2429 NO_DEFER_POP;
2431 /* Precompute any arguments as needed. */
2432 if (pass)
2433 precompute_arguments (flags, num_actuals, args);
2435 /* Now we are about to start emitting insns that can be deleted
2436 if a libcall is deleted. */
2437 if (pass && (flags & (ECF_LIBCALL_BLOCK | ECF_MALLOC)))
2438 start_sequence ();
2440 adjusted_args_size = args_size;
2441 /* Compute the actual size of the argument block required. The variable
2442 and constant sizes must be combined, the size may have to be rounded,
2443 and there may be a minimum required size. When generating a sibcall
2444 pattern, do not round up, since we'll be re-using whatever space our
2445 caller provided. */
2446 unadjusted_args_size
2447 = compute_argument_block_size (reg_parm_stack_space,
2448 &adjusted_args_size,
2449 (pass == 0 ? 0
2450 : preferred_stack_boundary));
2452 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
2454 /* The argument block when performing a sibling call is the
2455 incoming argument block. */
2456 if (pass == 0)
2458 argblock = virtual_incoming_args_rtx;
2459 argblock
2460 #ifdef STACK_GROWS_DOWNWARD
2461 = plus_constant (argblock, current_function_pretend_args_size);
2462 #else
2463 = plus_constant (argblock, -current_function_pretend_args_size);
2464 #endif
2465 stored_args_map = sbitmap_alloc (args_size.constant);
2466 sbitmap_zero (stored_args_map);
2469 /* If we have no actual push instructions, or shouldn't use them,
2470 make space for all args right now. */
2471 else if (adjusted_args_size.var != 0)
2473 if (old_stack_level == 0)
2475 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
2476 old_stack_pointer_delta = stack_pointer_delta;
2477 old_pending_adj = pending_stack_adjust;
2478 pending_stack_adjust = 0;
2479 /* stack_arg_under_construction says whether a stack arg is
2480 being constructed at the old stack level. Pushing the stack
2481 gets a clean outgoing argument block. */
2482 old_stack_arg_under_construction = stack_arg_under_construction;
2483 stack_arg_under_construction = 0;
2485 argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
2487 else
2489 /* Note that we must go through the motions of allocating an argument
2490 block even if the size is zero because we may be storing args
2491 in the area reserved for register arguments, which may be part of
2492 the stack frame. */
2494 int needed = adjusted_args_size.constant;
2496 /* Store the maximum argument space used. It will be pushed by
2497 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
2498 checking). */
2500 if (needed > current_function_outgoing_args_size)
2501 current_function_outgoing_args_size = needed;
2503 if (must_preallocate)
2505 if (ACCUMULATE_OUTGOING_ARGS)
2507 /* Since the stack pointer will never be pushed, it is
2508 possible for the evaluation of a parm to clobber
2509 something we have already written to the stack.
2510 Since most function calls on RISC machines do not use
2511 the stack, this is uncommon, but must work correctly.
2513 Therefore, we save any area of the stack that was already
2514 written and that we are using. Here we set up to do this
2515 by making a new stack usage map from the old one. The
2516 actual save will be done by store_one_arg.
2518 Another approach might be to try to reorder the argument
2519 evaluations to avoid this conflicting stack usage. */
2521 #ifndef OUTGOING_REG_PARM_STACK_SPACE
2522 /* Since we will be writing into the entire argument area,
2523 the map must be allocated for its entire size, not just
2524 the part that is the responsibility of the caller. */
2525 needed += reg_parm_stack_space;
2526 #endif
2528 #ifdef ARGS_GROW_DOWNWARD
2529 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2530 needed + 1);
2531 #else
2532 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2533 needed);
2534 #endif
2535 stack_usage_map = alloca (highest_outgoing_arg_in_use);
2537 if (initial_highest_arg_in_use)
2538 memcpy (stack_usage_map, initial_stack_usage_map,
2539 initial_highest_arg_in_use);
2541 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
2542 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
2543 (highest_outgoing_arg_in_use
2544 - initial_highest_arg_in_use));
2545 needed = 0;
2547 /* The address of the outgoing argument list must not be
2548 copied to a register here, because argblock would be left
2549 pointing to the wrong place after the call to
2550 allocate_dynamic_stack_space below. */
2552 argblock = virtual_outgoing_args_rtx;
2554 else
2556 if (inhibit_defer_pop == 0)
2558 /* Try to reuse some or all of the pending_stack_adjust
2559 to get this space. */
2560 needed
2561 = (combine_pending_stack_adjustment_and_call
2562 (unadjusted_args_size,
2563 &adjusted_args_size,
2564 preferred_unit_stack_boundary));
2566 /* combine_pending_stack_adjustment_and_call computes
2567 an adjustment before the arguments are allocated.
2568 Account for them and see whether or not the stack
2569 needs to go up or down. */
2570 needed = unadjusted_args_size - needed;
2572 if (needed < 0)
2574 /* We're releasing stack space. */
2575 /* ??? We can avoid any adjustment at all if we're
2576 already aligned. FIXME. */
2577 pending_stack_adjust = -needed;
2578 do_pending_stack_adjust ();
2579 needed = 0;
2581 else
2582 /* We need to allocate space. We'll do that in
2583 push_block below. */
2584 pending_stack_adjust = 0;
2587 /* Special case this because overhead of `push_block' in
2588 this case is non-trivial. */
2589 if (needed == 0)
2590 argblock = virtual_outgoing_args_rtx;
2591 else
2593 argblock = push_block (GEN_INT (needed), 0, 0);
2594 #ifdef ARGS_GROW_DOWNWARD
2595 argblock = plus_constant (argblock, needed);
2596 #endif
2599 /* We only really need to call `copy_to_reg' in the case
2600 where push insns are going to be used to pass ARGBLOCK
2601 to a function call in ARGS. In that case, the stack
2602 pointer changes value from the allocation point to the
2603 call point, and hence the value of
2604 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
2605 as well always do it. */
2606 argblock = copy_to_reg (argblock);
2611 if (ACCUMULATE_OUTGOING_ARGS)
2613 /* The save/restore code in store_one_arg handles all
2614 cases except one: a constructor call (including a C
2615 function returning a BLKmode struct) to initialize
2616 an argument. */
2617 if (stack_arg_under_construction)
2619 #ifndef OUTGOING_REG_PARM_STACK_SPACE
2620 rtx push_size = GEN_INT (reg_parm_stack_space
2621 + adjusted_args_size.constant);
2622 #else
2623 rtx push_size = GEN_INT (adjusted_args_size.constant);
2624 #endif
2625 if (old_stack_level == 0)
2627 emit_stack_save (SAVE_BLOCK, &old_stack_level,
2628 NULL_RTX);
2629 old_stack_pointer_delta = stack_pointer_delta;
2630 old_pending_adj = pending_stack_adjust;
2631 pending_stack_adjust = 0;
2632 /* stack_arg_under_construction says whether a stack
2633 arg is being constructed at the old stack level.
2634 Pushing the stack gets a clean outgoing argument
2635 block. */
2636 old_stack_arg_under_construction
2637 = stack_arg_under_construction;
2638 stack_arg_under_construction = 0;
2639 /* Make a new map for the new argument list. */
2640 stack_usage_map = alloca (highest_outgoing_arg_in_use);
2641 memset (stack_usage_map, 0, highest_outgoing_arg_in_use);
2642 highest_outgoing_arg_in_use = 0;
2644 allocate_dynamic_stack_space (push_size, NULL_RTX,
2645 BITS_PER_UNIT);
2648 /* If argument evaluation might modify the stack pointer,
2649 copy the address of the argument list to a register. */
2650 for (i = 0; i < num_actuals; i++)
2651 if (args[i].pass_on_stack)
2653 argblock = copy_addr_to_reg (argblock);
2654 break;
2658 compute_argument_addresses (args, argblock, num_actuals);
2660 /* If we push args individually in reverse order, perform stack alignment
2661 before the first push (the last arg). */
2662 if (PUSH_ARGS_REVERSED && argblock == 0
2663 && adjusted_args_size.constant != unadjusted_args_size)
2665 /* When the stack adjustment is pending, we get better code
2666 by combining the adjustments. */
2667 if (pending_stack_adjust
2668 && ! (flags & ECF_LIBCALL_BLOCK)
2669 && ! inhibit_defer_pop)
2671 pending_stack_adjust
2672 = (combine_pending_stack_adjustment_and_call
2673 (unadjusted_args_size,
2674 &adjusted_args_size,
2675 preferred_unit_stack_boundary));
2676 do_pending_stack_adjust ();
2678 else if (argblock == 0)
2679 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
2680 - unadjusted_args_size));
2682 /* Now that the stack is properly aligned, pops can't safely
2683 be deferred during the evaluation of the arguments. */
2684 NO_DEFER_POP;
2686 funexp = rtx_for_function_call (fndecl, addr);
2688 /* Figure out the register where the value, if any, will come back. */
2689 valreg = 0;
2690 if (TYPE_MODE (TREE_TYPE (exp)) != VOIDmode
2691 && ! structure_value_addr)
2693 if (pcc_struct_value)
2694 valreg = hard_function_value (build_pointer_type (TREE_TYPE (exp)),
2695 fndecl, (pass == 0));
2696 else
2697 valreg = hard_function_value (TREE_TYPE (exp), fndecl, (pass == 0));
2700 /* Precompute all register parameters. It isn't safe to compute anything
2701 once we have started filling any specific hard regs. */
2702 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
2704 if (TREE_OPERAND (exp, 2))
2705 static_chain_value = expand_expr (TREE_OPERAND (exp, 2),
2706 NULL_RTX, VOIDmode, 0);
2707 else
2708 static_chain_value = 0;
2710 #ifdef REG_PARM_STACK_SPACE
2711 /* Save the fixed argument area if it's part of the caller's frame and
2712 is clobbered by argument setup for this call. */
2713 if (ACCUMULATE_OUTGOING_ARGS && pass)
2714 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
2715 &low_to_save, &high_to_save);
2716 #endif
2718 /* Now store (and compute if necessary) all non-register parms.
2719 These come before register parms, since they can require block-moves,
2720 which could clobber the registers used for register parms.
2721 Parms which have partial registers are not stored here,
2722 but we do preallocate space here if they want that. */
2724 for (i = 0; i < num_actuals; i++)
2725 if (args[i].reg == 0 || args[i].pass_on_stack)
2727 rtx before_arg = get_last_insn ();
2729 if (store_one_arg (&args[i], argblock, flags,
2730 adjusted_args_size.var != 0,
2731 reg_parm_stack_space)
2732 || (pass == 0
2733 && check_sibcall_argument_overlap (before_arg,
2734 &args[i], 1)))
2735 sibcall_failure = 1;
2737 if (flags & ECF_CONST
2738 && args[i].stack
2739 && args[i].value == args[i].stack)
2740 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
2741 gen_rtx_USE (VOIDmode,
2742 args[i].value),
2743 call_fusage);
2746 /* If we have a parm that is passed in registers but not in memory
2747 and whose alignment does not permit a direct copy into registers,
2748 make a group of pseudos that correspond to each register that we
2749 will later fill. */
2750 if (STRICT_ALIGNMENT)
2751 store_unaligned_arguments_into_pseudos (args, num_actuals);
2753 /* Now store any partially-in-registers parm.
2754 This is the last place a block-move can happen. */
2755 if (reg_parm_seen)
2756 for (i = 0; i < num_actuals; i++)
2757 if (args[i].partial != 0 && ! args[i].pass_on_stack)
2759 rtx before_arg = get_last_insn ();
2761 if (store_one_arg (&args[i], argblock, flags,
2762 adjusted_args_size.var != 0,
2763 reg_parm_stack_space)
2764 || (pass == 0
2765 && check_sibcall_argument_overlap (before_arg,
2766 &args[i], 1)))
2767 sibcall_failure = 1;
2770 /* If we pushed args in forward order, perform stack alignment
2771 after pushing the last arg. */
2772 if (!PUSH_ARGS_REVERSED && argblock == 0)
2773 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
2774 - unadjusted_args_size));
2776 /* If register arguments require space on the stack and stack space
2777 was not preallocated, allocate stack space here for arguments
2778 passed in registers. */
2779 #ifdef OUTGOING_REG_PARM_STACK_SPACE
2780 if (!ACCUMULATE_OUTGOING_ARGS
2781 && must_preallocate == 0 && reg_parm_stack_space > 0)
2782 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
2783 #endif
2785 /* Pass the function the address in which to return a
2786 structure value. */
2787 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
2789 structure_value_addr
2790 = convert_memory_address (Pmode, structure_value_addr);
2791 emit_move_insn (struct_value,
2792 force_reg (Pmode,
2793 force_operand (structure_value_addr,
2794 NULL_RTX)));
2796 if (REG_P (struct_value))
2797 use_reg (&call_fusage, struct_value);
2800 funexp = prepare_call_address (funexp, static_chain_value,
2801 &call_fusage, reg_parm_seen, pass == 0);
2803 load_register_parameters (args, num_actuals, &call_fusage, flags,
2804 pass == 0, &sibcall_failure);
2806 /* Perform postincrements before actually calling the function. */
2807 emit_queue ();
2809 /* Save a pointer to the last insn before the call, so that we can
2810 later safely search backwards to find the CALL_INSN. */
2811 before_call = get_last_insn ();
2813 /* Set up next argument register. For sibling calls on machines
2814 with register windows this should be the incoming register. */
2815 #ifdef FUNCTION_INCOMING_ARG
2816 if (pass == 0)
2817 next_arg_reg = FUNCTION_INCOMING_ARG (args_so_far, VOIDmode,
2818 void_type_node, 1);
2819 else
2820 #endif
2821 next_arg_reg = FUNCTION_ARG (args_so_far, VOIDmode,
2822 void_type_node, 1);
2824 /* All arguments and registers used for the call must be set up by
2825 now! */
2827 /* Stack must be properly aligned now. */
2828 if (pass && stack_pointer_delta % preferred_unit_stack_boundary)
2829 abort ();
2831 /* Generate the actual call instruction. */
2832 emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
2833 adjusted_args_size.constant, struct_value_size,
2834 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
2835 flags, & args_so_far);
2837 /* If call is cse'able, make appropriate pair of reg-notes around it.
2838 Test valreg so we don't crash; may safely ignore `const'
2839 if return type is void. Disable for PARALLEL return values, because
2840 we have no way to move such values into a pseudo register. */
2841 if (pass && (flags & ECF_LIBCALL_BLOCK))
2843 rtx insns;
2844 rtx insn;
2845 bool failed = valreg == 0 || GET_CODE (valreg) == PARALLEL;
2847 insns = get_insns ();
2849 /* Expansion of block moves possibly introduced a loop that may
2850 not appear inside libcall block. */
2851 for (insn = insns; insn; insn = NEXT_INSN (insn))
2852 if (GET_CODE (insn) == JUMP_INSN)
2853 failed = true;
2855 if (failed)
2857 end_sequence ();
2858 emit_insn (insns);
2860 else
2862 rtx note = 0;
2863 rtx temp = gen_reg_rtx (GET_MODE (valreg));
2865 /* Mark the return value as a pointer if needed. */
2866 if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
2867 mark_reg_pointer (temp,
2868 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp))));
2870 end_sequence ();
2871 if (flag_unsafe_math_optimizations
2872 && fndecl
2873 && DECL_BUILT_IN (fndecl)
2874 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRT
2875 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRTF
2876 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRTL))
2877 note = gen_rtx_fmt_e (SQRT,
2878 GET_MODE (temp),
2879 args[0].initial_value);
2880 else
2882 /* Construct an "equal form" for the value which
2883 mentions all the arguments in order as well as
2884 the function name. */
2885 for (i = 0; i < num_actuals; i++)
2886 note = gen_rtx_EXPR_LIST (VOIDmode,
2887 args[i].initial_value, note);
2888 note = gen_rtx_EXPR_LIST (VOIDmode, funexp, note);
2890 if (flags & ECF_PURE)
2891 note = gen_rtx_EXPR_LIST (VOIDmode,
2892 gen_rtx_USE (VOIDmode,
2893 gen_rtx_MEM (BLKmode,
2894 gen_rtx_SCRATCH (VOIDmode))),
2895 note);
2897 emit_libcall_block (insns, temp, valreg, note);
2899 valreg = temp;
2902 else if (pass && (flags & ECF_MALLOC))
2904 rtx temp = gen_reg_rtx (GET_MODE (valreg));
2905 rtx last, insns;
2907 /* The return value from a malloc-like function is a pointer. */
2908 if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
2909 mark_reg_pointer (temp, BIGGEST_ALIGNMENT);
2911 emit_move_insn (temp, valreg);
2913 /* The return value from a malloc-like function can not alias
2914 anything else. */
2915 last = get_last_insn ();
2916 REG_NOTES (last) =
2917 gen_rtx_EXPR_LIST (REG_NOALIAS, temp, REG_NOTES (last));
2919 /* Write out the sequence. */
2920 insns = get_insns ();
2921 end_sequence ();
2922 emit_insn (insns);
2923 valreg = temp;
2926 /* For calls to `setjmp', etc., inform flow.c it should complain
2927 if nonvolatile values are live. For functions that cannot return,
2928 inform flow that control does not fall through. */
2930 if ((flags & (ECF_NORETURN | ECF_LONGJMP)) || pass == 0)
2932 /* The barrier must be emitted
2933 immediately after the CALL_INSN. Some ports emit more
2934 than just a CALL_INSN above, so we must search for it here. */
2936 rtx last = get_last_insn ();
2937 while (GET_CODE (last) != CALL_INSN)
2939 last = PREV_INSN (last);
2940 /* There was no CALL_INSN? */
2941 if (last == before_call)
2942 abort ();
2945 emit_barrier_after (last);
2947 /* Stack adjustments after a noreturn call are dead code.
2948 However when NO_DEFER_POP is in effect, we must preserve
2949 stack_pointer_delta. */
2950 if (inhibit_defer_pop == 0)
2952 stack_pointer_delta = old_stack_allocated;
2953 pending_stack_adjust = 0;
2957 if (flags & ECF_LONGJMP)
2958 current_function_calls_longjmp = 1;
2960 /* If value type not void, return an rtx for the value. */
2962 /* If there are cleanups to be called, don't use a hard reg as target.
2963 We need to double check this and see if it matters anymore. */
2964 if (any_pending_cleanups ())
2966 if (target && REG_P (target)
2967 && REGNO (target) < FIRST_PSEUDO_REGISTER)
2968 target = 0;
2969 sibcall_failure = 1;
2972 if (TYPE_MODE (TREE_TYPE (exp)) == VOIDmode
2973 || ignore)
2974 target = const0_rtx;
2975 else if (structure_value_addr)
2977 if (target == 0 || !MEM_P (target))
2979 target
2980 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
2981 memory_address (TYPE_MODE (TREE_TYPE (exp)),
2982 structure_value_addr));
2983 set_mem_attributes (target, exp, 1);
2986 else if (pcc_struct_value)
2988 /* This is the special C++ case where we need to
2989 know what the true target was. We take care to
2990 never use this value more than once in one expression. */
2991 target = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
2992 copy_to_reg (valreg));
2993 set_mem_attributes (target, exp, 1);
2995 /* Handle calls that return values in multiple non-contiguous locations.
2996 The Irix 6 ABI has examples of this. */
2997 else if (GET_CODE (valreg) == PARALLEL)
2999 if (target == 0)
3001 /* This will only be assigned once, so it can be readonly. */
3002 tree nt = build_qualified_type (TREE_TYPE (exp),
3003 (TYPE_QUALS (TREE_TYPE (exp))
3004 | TYPE_QUAL_CONST));
3006 target = assign_temp (nt, 0, 1, 1);
3007 preserve_temp_slots (target);
3010 if (! rtx_equal_p (target, valreg))
3011 emit_group_store (target, valreg, TREE_TYPE (exp),
3012 int_size_in_bytes (TREE_TYPE (exp)));
3014 /* We can not support sibling calls for this case. */
3015 sibcall_failure = 1;
3017 else if (target
3018 && GET_MODE (target) == TYPE_MODE (TREE_TYPE (exp))
3019 && GET_MODE (target) == GET_MODE (valreg))
3021 /* TARGET and VALREG cannot be equal at this point because the
3022 latter would not have REG_FUNCTION_VALUE_P true, while the
3023 former would if it were referring to the same register.
3025 If they refer to the same register, this move will be a no-op,
3026 except when function inlining is being done. */
3027 emit_move_insn (target, valreg);
3029 /* If we are setting a MEM, this code must be executed. Since it is
3030 emitted after the call insn, sibcall optimization cannot be
3031 performed in that case. */
3032 if (MEM_P (target))
3033 sibcall_failure = 1;
3035 else if (TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
3037 target = copy_blkmode_from_reg (target, valreg, TREE_TYPE (exp));
3039 /* We can not support sibling calls for this case. */
3040 sibcall_failure = 1;
3042 else
3044 if (shift_returned_value (TREE_TYPE (exp), &valreg))
3045 sibcall_failure = 1;
3047 target = copy_to_reg (valreg);
3050 if (targetm.calls.promote_function_return(funtype))
3052 /* If we promoted this return value, make the proper SUBREG. TARGET
3053 might be const0_rtx here, so be careful. */
3054 if (REG_P (target)
3055 && TYPE_MODE (TREE_TYPE (exp)) != BLKmode
3056 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
3058 tree type = TREE_TYPE (exp);
3059 int unsignedp = TYPE_UNSIGNED (type);
3060 int offset = 0;
3062 /* If we don't promote as expected, something is wrong. */
3063 if (GET_MODE (target)
3064 != promote_mode (type, TYPE_MODE (type), &unsignedp, 1))
3065 abort ();
3067 if ((WORDS_BIG_ENDIAN || BYTES_BIG_ENDIAN)
3068 && GET_MODE_SIZE (GET_MODE (target))
3069 > GET_MODE_SIZE (TYPE_MODE (type)))
3071 offset = GET_MODE_SIZE (GET_MODE (target))
3072 - GET_MODE_SIZE (TYPE_MODE (type));
3073 if (! BYTES_BIG_ENDIAN)
3074 offset = (offset / UNITS_PER_WORD) * UNITS_PER_WORD;
3075 else if (! WORDS_BIG_ENDIAN)
3076 offset %= UNITS_PER_WORD;
3078 target = gen_rtx_SUBREG (TYPE_MODE (type), target, offset);
3079 SUBREG_PROMOTED_VAR_P (target) = 1;
3080 SUBREG_PROMOTED_UNSIGNED_SET (target, unsignedp);
3084 /* If size of args is variable or this was a constructor call for a stack
3085 argument, restore saved stack-pointer value. */
3087 if (old_stack_level && ! (flags & ECF_SP_DEPRESSED))
3089 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
3090 stack_pointer_delta = old_stack_pointer_delta;
3091 pending_stack_adjust = old_pending_adj;
3092 stack_arg_under_construction = old_stack_arg_under_construction;
3093 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3094 stack_usage_map = initial_stack_usage_map;
3095 sibcall_failure = 1;
3097 else if (ACCUMULATE_OUTGOING_ARGS && pass)
3099 #ifdef REG_PARM_STACK_SPACE
3100 if (save_area)
3101 restore_fixed_argument_area (save_area, argblock,
3102 high_to_save, low_to_save);
3103 #endif
3105 /* If we saved any argument areas, restore them. */
3106 for (i = 0; i < num_actuals; i++)
3107 if (args[i].save_area)
3109 enum machine_mode save_mode = GET_MODE (args[i].save_area);
3110 rtx stack_area
3111 = gen_rtx_MEM (save_mode,
3112 memory_address (save_mode,
3113 XEXP (args[i].stack_slot, 0)));
3115 if (save_mode != BLKmode)
3116 emit_move_insn (stack_area, args[i].save_area);
3117 else
3118 emit_block_move (stack_area, args[i].save_area,
3119 GEN_INT (args[i].locate.size.constant),
3120 BLOCK_OP_CALL_PARM);
3123 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3124 stack_usage_map = initial_stack_usage_map;
3127 /* If this was alloca, record the new stack level for nonlocal gotos.
3128 Check for the handler slots since we might not have a save area
3129 for non-local gotos. */
3131 if ((flags & ECF_MAY_BE_ALLOCA) && cfun->nonlocal_goto_save_area != 0)
3132 update_nonlocal_goto_save_area ();
3134 /* Free up storage we no longer need. */
3135 for (i = 0; i < num_actuals; ++i)
3136 if (args[i].aligned_regs)
3137 free (args[i].aligned_regs);
3139 if (pass == 0)
3141 /* Undo the fake expand_start_target_temps we did earlier. If
3142 there had been any cleanups created, we've already set
3143 sibcall_failure. */
3144 expand_end_target_temps ();
3147 /* If this function is returning into a memory location marked as
3148 readonly, it means it is initializing that location. We normally treat
3149 functions as not clobbering such locations, so we need to specify that
3150 this one does. We do this by adding the appropriate CLOBBER to the
3151 CALL_INSN function usage list. This cannot be done by emitting a
3152 standalone CLOBBER after the call because the latter would be ignored
3153 by at least the delay slot scheduling pass. We do this now instead of
3154 adding to call_fusage before the call to emit_call_1 because TARGET
3155 may be modified in the meantime. */
3156 if (structure_value_addr != 0 && target != 0
3157 && MEM_P (target) && RTX_UNCHANGING_P (target))
3158 add_function_usage_to
3159 (last_call_insn (),
3160 gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_CLOBBER (VOIDmode, target),
3161 NULL_RTX));
3163 insns = get_insns ();
3164 end_sequence ();
3166 if (pass == 0)
3168 tail_call_insns = insns;
3170 /* Restore the pending stack adjustment now that we have
3171 finished generating the sibling call sequence. */
3173 pending_stack_adjust = save_pending_stack_adjust;
3174 stack_pointer_delta = save_stack_pointer_delta;
3176 /* Prepare arg structure for next iteration. */
3177 for (i = 0; i < num_actuals; i++)
3179 args[i].value = 0;
3180 args[i].aligned_regs = 0;
3181 args[i].stack = 0;
3184 sbitmap_free (stored_args_map);
3186 else
3188 normal_call_insns = insns;
3190 /* Verify that we've deallocated all the stack we used. */
3191 if (! (flags & (ECF_NORETURN | ECF_LONGJMP))
3192 && old_stack_allocated != stack_pointer_delta
3193 - pending_stack_adjust)
3194 abort ();
3197 /* If something prevents making this a sibling call,
3198 zero out the sequence. */
3199 if (sibcall_failure)
3200 tail_call_insns = NULL_RTX;
3201 else
3202 break;
3205 /* If tail call production succeeded, we need to remove REG_EQUIV notes on
3206 arguments too, as argument area is now clobbered by the call. */
3207 if (tail_call_insns)
3209 emit_insn (tail_call_insns);
3210 cfun->tail_call_emit = true;
3212 else
3213 emit_insn (normal_call_insns);
3215 currently_expanding_call--;
3217 /* If this function returns with the stack pointer depressed, ensure
3218 this block saves and restores the stack pointer, show it was
3219 changed, and adjust for any outgoing arg space. */
3220 if (flags & ECF_SP_DEPRESSED)
3222 clear_pending_stack_adjust ();
3223 emit_insn (gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx));
3224 emit_move_insn (virtual_stack_dynamic_rtx, stack_pointer_rtx);
3225 save_stack_pointer ();
3228 return target;
3231 /* A sibling call sequence invalidates any REG_EQUIV notes made for
3232 this function's incoming arguments.
3234 At the start of RTL generation we know the only REG_EQUIV notes
3235 in the rtl chain are those for incoming arguments, so we can safely
3236 flush any REG_EQUIV note.
3238 This is (slight) overkill. We could keep track of the highest
3239 argument we clobber and be more selective in removing notes, but it
3240 does not seem to be worth the effort. */
3241 void
3242 fixup_tail_calls (void)
3244 rtx insn;
3245 tree arg;
3247 purge_reg_equiv_notes ();
3249 /* A sibling call sequence also may invalidate RTX_UNCHANGING_P
3250 flag of some incoming arguments MEM RTLs, because it can write into
3251 those slots. We clear all those bits now.
3253 This is (slight) overkill, we could keep track of which arguments
3254 we actually write into. */
3255 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3257 if (INSN_P (insn))
3258 purge_mem_unchanging_flag (PATTERN (insn));
3261 /* Similarly, invalidate RTX_UNCHANGING_P for any incoming
3262 arguments passed in registers. */
3263 for (arg = DECL_ARGUMENTS (current_function_decl);
3264 arg;
3265 arg = TREE_CHAIN (arg))
3267 if (REG_P (DECL_RTL (arg)))
3268 RTX_UNCHANGING_P (DECL_RTL (arg)) = false;
3272 /* Traverse an argument list in VALUES and expand all complex
3273 arguments into their components. */
3274 tree
3275 split_complex_values (tree values)
3277 tree p;
3279 /* Before allocating memory, check for the common case of no complex. */
3280 for (p = values; p; p = TREE_CHAIN (p))
3282 tree type = TREE_TYPE (TREE_VALUE (p));
3283 if (type && TREE_CODE (type) == COMPLEX_TYPE
3284 && targetm.calls.split_complex_arg (type))
3285 goto found;
3287 return values;
3289 found:
3290 values = copy_list (values);
3292 for (p = values; p; p = TREE_CHAIN (p))
3294 tree complex_value = TREE_VALUE (p);
3295 tree complex_type;
3297 complex_type = TREE_TYPE (complex_value);
3298 if (!complex_type)
3299 continue;
3301 if (TREE_CODE (complex_type) == COMPLEX_TYPE
3302 && targetm.calls.split_complex_arg (complex_type))
3304 tree subtype;
3305 tree real, imag, next;
3307 subtype = TREE_TYPE (complex_type);
3308 complex_value = save_expr (complex_value);
3309 real = build1 (REALPART_EXPR, subtype, complex_value);
3310 imag = build1 (IMAGPART_EXPR, subtype, complex_value);
3312 TREE_VALUE (p) = real;
3313 next = TREE_CHAIN (p);
3314 imag = build_tree_list (NULL_TREE, imag);
3315 TREE_CHAIN (p) = imag;
3316 TREE_CHAIN (imag) = next;
3318 /* Skip the newly created node. */
3319 p = TREE_CHAIN (p);
3323 return values;
3326 /* Traverse a list of TYPES and expand all complex types into their
3327 components. */
3328 tree
3329 split_complex_types (tree types)
3331 tree p;
3333 /* Before allocating memory, check for the common case of no complex. */
3334 for (p = types; p; p = TREE_CHAIN (p))
3336 tree type = TREE_VALUE (p);
3337 if (TREE_CODE (type) == COMPLEX_TYPE
3338 && targetm.calls.split_complex_arg (type))
3339 goto found;
3341 return types;
3343 found:
3344 types = copy_list (types);
3346 for (p = types; p; p = TREE_CHAIN (p))
3348 tree complex_type = TREE_VALUE (p);
3350 if (TREE_CODE (complex_type) == COMPLEX_TYPE
3351 && targetm.calls.split_complex_arg (complex_type))
3353 tree next, imag;
3355 /* Rewrite complex type with component type. */
3356 TREE_VALUE (p) = TREE_TYPE (complex_type);
3357 next = TREE_CHAIN (p);
3359 /* Add another component type for the imaginary part. */
3360 imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
3361 TREE_CHAIN (p) = imag;
3362 TREE_CHAIN (imag) = next;
3364 /* Skip the newly created node. */
3365 p = TREE_CHAIN (p);
3369 return types;
3372 /* Output a library call to function FUN (a SYMBOL_REF rtx).
3373 The RETVAL parameter specifies whether return value needs to be saved, other
3374 parameters are documented in the emit_library_call function below. */
3376 static rtx
3377 emit_library_call_value_1 (int retval, rtx orgfun, rtx value,
3378 enum libcall_type fn_type,
3379 enum machine_mode outmode, int nargs, va_list p)
3381 /* Total size in bytes of all the stack-parms scanned so far. */
3382 struct args_size args_size;
3383 /* Size of arguments before any adjustments (such as rounding). */
3384 struct args_size original_args_size;
3385 int argnum;
3386 rtx fun;
3387 int inc;
3388 int count;
3389 rtx argblock = 0;
3390 CUMULATIVE_ARGS args_so_far;
3391 struct arg
3393 rtx value;
3394 enum machine_mode mode;
3395 rtx reg;
3396 int partial;
3397 struct locate_and_pad_arg_data locate;
3398 rtx save_area;
3400 struct arg *argvec;
3401 int old_inhibit_defer_pop = inhibit_defer_pop;
3402 rtx call_fusage = 0;
3403 rtx mem_value = 0;
3404 rtx valreg;
3405 int pcc_struct_value = 0;
3406 int struct_value_size = 0;
3407 int flags;
3408 int reg_parm_stack_space = 0;
3409 int needed;
3410 rtx before_call;
3411 tree tfom; /* type_for_mode (outmode, 0) */
3413 #ifdef REG_PARM_STACK_SPACE
3414 /* Define the boundary of the register parm stack space that needs to be
3415 save, if any. */
3416 int low_to_save, high_to_save;
3417 rtx save_area = 0; /* Place that it is saved. */
3418 #endif
3420 /* Size of the stack reserved for parameter registers. */
3421 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
3422 char *initial_stack_usage_map = stack_usage_map;
3424 rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
3426 #ifdef REG_PARM_STACK_SPACE
3427 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
3428 #endif
3430 /* By default, library functions can not throw. */
3431 flags = ECF_NOTHROW;
3433 switch (fn_type)
3435 case LCT_NORMAL:
3436 break;
3437 case LCT_CONST:
3438 flags |= ECF_CONST;
3439 break;
3440 case LCT_PURE:
3441 flags |= ECF_PURE;
3442 break;
3443 case LCT_CONST_MAKE_BLOCK:
3444 flags |= ECF_CONST | ECF_LIBCALL_BLOCK;
3445 break;
3446 case LCT_PURE_MAKE_BLOCK:
3447 flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
3448 break;
3449 case LCT_NORETURN:
3450 flags |= ECF_NORETURN;
3451 break;
3452 case LCT_THROW:
3453 flags = ECF_NORETURN;
3454 break;
3455 case LCT_ALWAYS_RETURN:
3456 flags = ECF_ALWAYS_RETURN;
3457 break;
3458 case LCT_RETURNS_TWICE:
3459 flags = ECF_RETURNS_TWICE;
3460 break;
3462 fun = orgfun;
3464 /* Ensure current function's preferred stack boundary is at least
3465 what we need. */
3466 if (cfun->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
3467 cfun->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3469 /* If this kind of value comes back in memory,
3470 decide where in memory it should come back. */
3471 if (outmode != VOIDmode)
3473 tfom = lang_hooks.types.type_for_mode (outmode, 0);
3474 if (aggregate_value_p (tfom, 0))
3476 #ifdef PCC_STATIC_STRUCT_RETURN
3477 rtx pointer_reg
3478 = hard_function_value (build_pointer_type (tfom), 0, 0);
3479 mem_value = gen_rtx_MEM (outmode, pointer_reg);
3480 pcc_struct_value = 1;
3481 if (value == 0)
3482 value = gen_reg_rtx (outmode);
3483 #else /* not PCC_STATIC_STRUCT_RETURN */
3484 struct_value_size = GET_MODE_SIZE (outmode);
3485 if (value != 0 && MEM_P (value))
3486 mem_value = value;
3487 else
3488 mem_value = assign_temp (tfom, 0, 1, 1);
3489 #endif
3490 /* This call returns a big structure. */
3491 flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
3494 else
3495 tfom = void_type_node;
3497 /* ??? Unfinished: must pass the memory address as an argument. */
3499 /* Copy all the libcall-arguments out of the varargs data
3500 and into a vector ARGVEC.
3502 Compute how to pass each argument. We only support a very small subset
3503 of the full argument passing conventions to limit complexity here since
3504 library functions shouldn't have many args. */
3506 argvec = alloca ((nargs + 1) * sizeof (struct arg));
3507 memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
3509 #ifdef INIT_CUMULATIVE_LIBCALL_ARGS
3510 INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far, outmode, fun);
3511 #else
3512 INIT_CUMULATIVE_ARGS (args_so_far, NULL_TREE, fun, 0, nargs);
3513 #endif
3515 args_size.constant = 0;
3516 args_size.var = 0;
3518 count = 0;
3520 /* Now we are about to start emitting insns that can be deleted
3521 if a libcall is deleted. */
3522 if (flags & ECF_LIBCALL_BLOCK)
3523 start_sequence ();
3525 push_temp_slots ();
3527 /* If there's a structure value address to be passed,
3528 either pass it in the special place, or pass it as an extra argument. */
3529 if (mem_value && struct_value == 0 && ! pcc_struct_value)
3531 rtx addr = XEXP (mem_value, 0);
3532 nargs++;
3534 /* Make sure it is a reasonable operand for a move or push insn. */
3535 if (!REG_P (addr) && !MEM_P (addr)
3536 && ! (CONSTANT_P (addr) && LEGITIMATE_CONSTANT_P (addr)))
3537 addr = force_operand (addr, NULL_RTX);
3539 argvec[count].value = addr;
3540 argvec[count].mode = Pmode;
3541 argvec[count].partial = 0;
3543 argvec[count].reg = FUNCTION_ARG (args_so_far, Pmode, NULL_TREE, 1);
3544 #ifdef FUNCTION_ARG_PARTIAL_NREGS
3545 if (FUNCTION_ARG_PARTIAL_NREGS (args_so_far, Pmode, NULL_TREE, 1))
3546 abort ();
3547 #endif
3549 locate_and_pad_parm (Pmode, NULL_TREE,
3550 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3552 #else
3553 argvec[count].reg != 0,
3554 #endif
3555 0, NULL_TREE, &args_size, &argvec[count].locate);
3557 if (argvec[count].reg == 0 || argvec[count].partial != 0
3558 || reg_parm_stack_space > 0)
3559 args_size.constant += argvec[count].locate.size.constant;
3561 FUNCTION_ARG_ADVANCE (args_so_far, Pmode, (tree) 0, 1);
3563 count++;
3566 for (; count < nargs; count++)
3568 rtx val = va_arg (p, rtx);
3569 enum machine_mode mode = va_arg (p, enum machine_mode);
3571 /* We cannot convert the arg value to the mode the library wants here;
3572 must do it earlier where we know the signedness of the arg. */
3573 if (mode == BLKmode
3574 || (GET_MODE (val) != mode && GET_MODE (val) != VOIDmode))
3575 abort ();
3577 /* There's no need to call protect_from_queue, because
3578 either emit_move_insn or emit_push_insn will do that. */
3580 /* Make sure it is a reasonable operand for a move or push insn. */
3581 if (!REG_P (val) && !MEM_P (val)
3582 && ! (CONSTANT_P (val) && LEGITIMATE_CONSTANT_P (val)))
3583 val = force_operand (val, NULL_RTX);
3585 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
3586 if (FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, mode, NULL_TREE, 1))
3588 rtx slot;
3589 int must_copy = 1
3590 #ifdef FUNCTION_ARG_CALLEE_COPIES
3591 && ! FUNCTION_ARG_CALLEE_COPIES (args_so_far, mode,
3592 NULL_TREE, 1)
3593 #endif
3596 /* loop.c won't look at CALL_INSN_FUNCTION_USAGE of const/pure
3597 functions, so we have to pretend this isn't such a function. */
3598 if (flags & ECF_LIBCALL_BLOCK)
3600 rtx insns = get_insns ();
3601 end_sequence ();
3602 emit_insn (insns);
3604 flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
3606 /* If this was a CONST function, it is now PURE since
3607 it now reads memory. */
3608 if (flags & ECF_CONST)
3610 flags &= ~ECF_CONST;
3611 flags |= ECF_PURE;
3614 if (GET_MODE (val) == MEM && ! must_copy)
3615 slot = val;
3616 else if (must_copy)
3618 slot = assign_temp (lang_hooks.types.type_for_mode (mode, 0),
3619 0, 1, 1);
3620 emit_move_insn (slot, val);
3622 else
3624 tree type = lang_hooks.types.type_for_mode (mode, 0);
3626 slot
3627 = gen_rtx_MEM (mode,
3628 expand_expr (build1 (ADDR_EXPR,
3629 build_pointer_type (type),
3630 make_tree (type, val)),
3631 NULL_RTX, VOIDmode, 0));
3634 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
3635 gen_rtx_USE (VOIDmode, slot),
3636 call_fusage);
3637 if (must_copy)
3638 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
3639 gen_rtx_CLOBBER (VOIDmode,
3640 slot),
3641 call_fusage);
3643 mode = Pmode;
3644 val = force_operand (XEXP (slot, 0), NULL_RTX);
3646 #endif
3648 argvec[count].value = val;
3649 argvec[count].mode = mode;
3651 argvec[count].reg = FUNCTION_ARG (args_so_far, mode, NULL_TREE, 1);
3653 #ifdef FUNCTION_ARG_PARTIAL_NREGS
3654 argvec[count].partial
3655 = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, mode, NULL_TREE, 1);
3656 #else
3657 argvec[count].partial = 0;
3658 #endif
3660 locate_and_pad_parm (mode, NULL_TREE,
3661 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3663 #else
3664 argvec[count].reg != 0,
3665 #endif
3666 argvec[count].partial,
3667 NULL_TREE, &args_size, &argvec[count].locate);
3669 if (argvec[count].locate.size.var)
3670 abort ();
3672 if (argvec[count].reg == 0 || argvec[count].partial != 0
3673 || reg_parm_stack_space > 0)
3674 args_size.constant += argvec[count].locate.size.constant;
3676 FUNCTION_ARG_ADVANCE (args_so_far, mode, (tree) 0, 1);
3679 /* If this machine requires an external definition for library
3680 functions, write one out. */
3681 assemble_external_libcall (fun);
3683 original_args_size = args_size;
3684 args_size.constant = (((args_size.constant
3685 + stack_pointer_delta
3686 + STACK_BYTES - 1)
3687 / STACK_BYTES
3688 * STACK_BYTES)
3689 - stack_pointer_delta);
3691 args_size.constant = MAX (args_size.constant,
3692 reg_parm_stack_space);
3694 #ifndef OUTGOING_REG_PARM_STACK_SPACE
3695 args_size.constant -= reg_parm_stack_space;
3696 #endif
3698 if (args_size.constant > current_function_outgoing_args_size)
3699 current_function_outgoing_args_size = args_size.constant;
3701 if (ACCUMULATE_OUTGOING_ARGS)
3703 /* Since the stack pointer will never be pushed, it is possible for
3704 the evaluation of a parm to clobber something we have already
3705 written to the stack. Since most function calls on RISC machines
3706 do not use the stack, this is uncommon, but must work correctly.
3708 Therefore, we save any area of the stack that was already written
3709 and that we are using. Here we set up to do this by making a new
3710 stack usage map from the old one.
3712 Another approach might be to try to reorder the argument
3713 evaluations to avoid this conflicting stack usage. */
3715 needed = args_size.constant;
3717 #ifndef OUTGOING_REG_PARM_STACK_SPACE
3718 /* Since we will be writing into the entire argument area, the
3719 map must be allocated for its entire size, not just the part that
3720 is the responsibility of the caller. */
3721 needed += reg_parm_stack_space;
3722 #endif
3724 #ifdef ARGS_GROW_DOWNWARD
3725 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3726 needed + 1);
3727 #else
3728 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3729 needed);
3730 #endif
3731 stack_usage_map = alloca (highest_outgoing_arg_in_use);
3733 if (initial_highest_arg_in_use)
3734 memcpy (stack_usage_map, initial_stack_usage_map,
3735 initial_highest_arg_in_use);
3737 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
3738 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
3739 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
3740 needed = 0;
3742 /* We must be careful to use virtual regs before they're instantiated,
3743 and real regs afterwards. Loop optimization, for example, can create
3744 new libcalls after we've instantiated the virtual regs, and if we
3745 use virtuals anyway, they won't match the rtl patterns. */
3747 if (virtuals_instantiated)
3748 argblock = plus_constant (stack_pointer_rtx, STACK_POINTER_OFFSET);
3749 else
3750 argblock = virtual_outgoing_args_rtx;
3752 else
3754 if (!PUSH_ARGS)
3755 argblock = push_block (GEN_INT (args_size.constant), 0, 0);
3758 /* If we push args individually in reverse order, perform stack alignment
3759 before the first push (the last arg). */
3760 if (argblock == 0 && PUSH_ARGS_REVERSED)
3761 anti_adjust_stack (GEN_INT (args_size.constant
3762 - original_args_size.constant));
3764 if (PUSH_ARGS_REVERSED)
3766 inc = -1;
3767 argnum = nargs - 1;
3769 else
3771 inc = 1;
3772 argnum = 0;
3775 #ifdef REG_PARM_STACK_SPACE
3776 if (ACCUMULATE_OUTGOING_ARGS)
3778 /* The argument list is the property of the called routine and it
3779 may clobber it. If the fixed area has been used for previous
3780 parameters, we must save and restore it. */
3781 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3782 &low_to_save, &high_to_save);
3784 #endif
3786 /* Push the args that need to be pushed. */
3788 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
3789 are to be pushed. */
3790 for (count = 0; count < nargs; count++, argnum += inc)
3792 enum machine_mode mode = argvec[argnum].mode;
3793 rtx val = argvec[argnum].value;
3794 rtx reg = argvec[argnum].reg;
3795 int partial = argvec[argnum].partial;
3796 int lower_bound = 0, upper_bound = 0, i;
3798 if (! (reg != 0 && partial == 0))
3800 if (ACCUMULATE_OUTGOING_ARGS)
3802 /* If this is being stored into a pre-allocated, fixed-size,
3803 stack area, save any previous data at that location. */
3805 #ifdef ARGS_GROW_DOWNWARD
3806 /* stack_slot is negative, but we want to index stack_usage_map
3807 with positive values. */
3808 upper_bound = -argvec[argnum].locate.offset.constant + 1;
3809 lower_bound = upper_bound - argvec[argnum].locate.size.constant;
3810 #else
3811 lower_bound = argvec[argnum].locate.offset.constant;
3812 upper_bound = lower_bound + argvec[argnum].locate.size.constant;
3813 #endif
3815 i = lower_bound;
3816 /* Don't worry about things in the fixed argument area;
3817 it has already been saved. */
3818 if (i < reg_parm_stack_space)
3819 i = reg_parm_stack_space;
3820 while (i < upper_bound && stack_usage_map[i] == 0)
3821 i++;
3823 if (i < upper_bound)
3825 /* We need to make a save area. */
3826 unsigned int size
3827 = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
3828 enum machine_mode save_mode
3829 = mode_for_size (size, MODE_INT, 1);
3830 rtx adr
3831 = plus_constant (argblock,
3832 argvec[argnum].locate.offset.constant);
3833 rtx stack_area
3834 = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
3836 if (save_mode == BLKmode)
3838 argvec[argnum].save_area
3839 = assign_stack_temp (BLKmode,
3840 argvec[argnum].locate.size.constant,
3843 emit_block_move (validize_mem (argvec[argnum].save_area),
3844 stack_area,
3845 GEN_INT (argvec[argnum].locate.size.constant),
3846 BLOCK_OP_CALL_PARM);
3848 else
3850 argvec[argnum].save_area = gen_reg_rtx (save_mode);
3852 emit_move_insn (argvec[argnum].save_area, stack_area);
3857 emit_push_insn (val, mode, NULL_TREE, NULL_RTX, PARM_BOUNDARY,
3858 partial, reg, 0, argblock,
3859 GEN_INT (argvec[argnum].locate.offset.constant),
3860 reg_parm_stack_space,
3861 ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad));
3863 /* Now mark the segment we just used. */
3864 if (ACCUMULATE_OUTGOING_ARGS)
3865 for (i = lower_bound; i < upper_bound; i++)
3866 stack_usage_map[i] = 1;
3868 NO_DEFER_POP;
3872 /* If we pushed args in forward order, perform stack alignment
3873 after pushing the last arg. */
3874 if (argblock == 0 && !PUSH_ARGS_REVERSED)
3875 anti_adjust_stack (GEN_INT (args_size.constant
3876 - original_args_size.constant));
3878 if (PUSH_ARGS_REVERSED)
3879 argnum = nargs - 1;
3880 else
3881 argnum = 0;
3883 fun = prepare_call_address (fun, NULL, &call_fusage, 0, 0);
3885 /* Now load any reg parms into their regs. */
3887 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
3888 are to be pushed. */
3889 for (count = 0; count < nargs; count++, argnum += inc)
3891 rtx val = argvec[argnum].value;
3892 rtx reg = argvec[argnum].reg;
3893 int partial = argvec[argnum].partial;
3895 /* Handle calls that pass values in multiple non-contiguous
3896 locations. The PA64 has examples of this for library calls. */
3897 if (reg != 0 && GET_CODE (reg) == PARALLEL)
3898 emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (GET_MODE (val)));
3899 else if (reg != 0 && partial == 0)
3900 emit_move_insn (reg, val);
3902 NO_DEFER_POP;
3905 /* Any regs containing parms remain in use through the call. */
3906 for (count = 0; count < nargs; count++)
3908 rtx reg = argvec[count].reg;
3909 if (reg != 0 && GET_CODE (reg) == PARALLEL)
3910 use_group_regs (&call_fusage, reg);
3911 else if (reg != 0)
3912 use_reg (&call_fusage, reg);
3915 /* Pass the function the address in which to return a structure value. */
3916 if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
3918 emit_move_insn (struct_value,
3919 force_reg (Pmode,
3920 force_operand (XEXP (mem_value, 0),
3921 NULL_RTX)));
3922 if (REG_P (struct_value))
3923 use_reg (&call_fusage, struct_value);
3926 /* Don't allow popping to be deferred, since then
3927 cse'ing of library calls could delete a call and leave the pop. */
3928 NO_DEFER_POP;
3929 valreg = (mem_value == 0 && outmode != VOIDmode
3930 ? hard_libcall_value (outmode) : NULL_RTX);
3932 /* Stack must be properly aligned now. */
3933 if (stack_pointer_delta & (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1))
3934 abort ();
3936 before_call = get_last_insn ();
3938 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
3939 will set inhibit_defer_pop to that value. */
3940 /* The return type is needed to decide how many bytes the function pops.
3941 Signedness plays no role in that, so for simplicity, we pretend it's
3942 always signed. We also assume that the list of arguments passed has
3943 no impact, so we pretend it is unknown. */
3945 emit_call_1 (fun, NULL,
3946 get_identifier (XSTR (orgfun, 0)),
3947 build_function_type (tfom, NULL_TREE),
3948 original_args_size.constant, args_size.constant,
3949 struct_value_size,
3950 FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1),
3951 valreg,
3952 old_inhibit_defer_pop + 1, call_fusage, flags, & args_so_far);
3954 /* For calls to `setjmp', etc., inform flow.c it should complain
3955 if nonvolatile values are live. For functions that cannot return,
3956 inform flow that control does not fall through. */
3958 if (flags & (ECF_NORETURN | ECF_LONGJMP))
3960 /* The barrier note must be emitted
3961 immediately after the CALL_INSN. Some ports emit more than
3962 just a CALL_INSN above, so we must search for it here. */
3964 rtx last = get_last_insn ();
3965 while (GET_CODE (last) != CALL_INSN)
3967 last = PREV_INSN (last);
3968 /* There was no CALL_INSN? */
3969 if (last == before_call)
3970 abort ();
3973 emit_barrier_after (last);
3976 /* Now restore inhibit_defer_pop to its actual original value. */
3977 OK_DEFER_POP;
3979 /* If call is cse'able, make appropriate pair of reg-notes around it.
3980 Test valreg so we don't crash; may safely ignore `const'
3981 if return type is void. Disable for PARALLEL return values, because
3982 we have no way to move such values into a pseudo register. */
3983 if (flags & ECF_LIBCALL_BLOCK)
3985 rtx insns;
3987 if (valreg == 0)
3989 insns = get_insns ();
3990 end_sequence ();
3991 emit_insn (insns);
3993 else
3995 rtx note = 0;
3996 rtx temp;
3997 int i;
3999 if (GET_CODE (valreg) == PARALLEL)
4001 temp = gen_reg_rtx (outmode);
4002 emit_group_store (temp, valreg, NULL_TREE,
4003 GET_MODE_SIZE (outmode));
4004 valreg = temp;
4007 temp = gen_reg_rtx (GET_MODE (valreg));
4009 /* Construct an "equal form" for the value which mentions all the
4010 arguments in order as well as the function name. */
4011 for (i = 0; i < nargs; i++)
4012 note = gen_rtx_EXPR_LIST (VOIDmode, argvec[i].value, note);
4013 note = gen_rtx_EXPR_LIST (VOIDmode, fun, note);
4015 insns = get_insns ();
4016 end_sequence ();
4018 if (flags & ECF_PURE)
4019 note = gen_rtx_EXPR_LIST (VOIDmode,
4020 gen_rtx_USE (VOIDmode,
4021 gen_rtx_MEM (BLKmode,
4022 gen_rtx_SCRATCH (VOIDmode))),
4023 note);
4025 emit_libcall_block (insns, temp, valreg, note);
4027 valreg = temp;
4030 pop_temp_slots ();
4032 /* Copy the value to the right place. */
4033 if (outmode != VOIDmode && retval)
4035 if (mem_value)
4037 if (value == 0)
4038 value = mem_value;
4039 if (value != mem_value)
4040 emit_move_insn (value, mem_value);
4042 else if (GET_CODE (valreg) == PARALLEL)
4044 if (value == 0)
4045 value = gen_reg_rtx (outmode);
4046 emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
4048 else if (value != 0)
4049 emit_move_insn (value, valreg);
4050 else
4051 value = valreg;
4054 if (ACCUMULATE_OUTGOING_ARGS)
4056 #ifdef REG_PARM_STACK_SPACE
4057 if (save_area)
4058 restore_fixed_argument_area (save_area, argblock,
4059 high_to_save, low_to_save);
4060 #endif
4062 /* If we saved any argument areas, restore them. */
4063 for (count = 0; count < nargs; count++)
4064 if (argvec[count].save_area)
4066 enum machine_mode save_mode = GET_MODE (argvec[count].save_area);
4067 rtx adr = plus_constant (argblock,
4068 argvec[count].locate.offset.constant);
4069 rtx stack_area = gen_rtx_MEM (save_mode,
4070 memory_address (save_mode, adr));
4072 if (save_mode == BLKmode)
4073 emit_block_move (stack_area,
4074 validize_mem (argvec[count].save_area),
4075 GEN_INT (argvec[count].locate.size.constant),
4076 BLOCK_OP_CALL_PARM);
4077 else
4078 emit_move_insn (stack_area, argvec[count].save_area);
4081 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
4082 stack_usage_map = initial_stack_usage_map;
4085 return value;
4089 /* Output a library call to function FUN (a SYMBOL_REF rtx)
4090 (emitting the queue unless NO_QUEUE is nonzero),
4091 for a value of mode OUTMODE,
4092 with NARGS different arguments, passed as alternating rtx values
4093 and machine_modes to convert them to.
4094 The rtx values should have been passed through protect_from_queue already.
4096 FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for `const'
4097 calls, LCT_PURE for `pure' calls, LCT_CONST_MAKE_BLOCK for `const' calls
4098 which should be enclosed in REG_LIBCALL/REG_RETVAL notes,
4099 LCT_PURE_MAKE_BLOCK for `purep' calls which should be enclosed in
4100 REG_LIBCALL/REG_RETVAL notes with extra (use (memory (scratch)),
4101 or other LCT_ value for other types of library calls. */
4103 void
4104 emit_library_call (rtx orgfun, enum libcall_type fn_type,
4105 enum machine_mode outmode, int nargs, ...)
4107 va_list p;
4109 va_start (p, nargs);
4110 emit_library_call_value_1 (0, orgfun, NULL_RTX, fn_type, outmode, nargs, p);
4111 va_end (p);
4114 /* Like emit_library_call except that an extra argument, VALUE,
4115 comes second and says where to store the result.
4116 (If VALUE is zero, this function chooses a convenient way
4117 to return the value.
4119 This function returns an rtx for where the value is to be found.
4120 If VALUE is nonzero, VALUE is returned. */
4123 emit_library_call_value (rtx orgfun, rtx value,
4124 enum libcall_type fn_type,
4125 enum machine_mode outmode, int nargs, ...)
4127 rtx result;
4128 va_list p;
4130 va_start (p, nargs);
4131 result = emit_library_call_value_1 (1, orgfun, value, fn_type, outmode,
4132 nargs, p);
4133 va_end (p);
4135 return result;
4138 /* Store a single argument for a function call
4139 into the register or memory area where it must be passed.
4140 *ARG describes the argument value and where to pass it.
4142 ARGBLOCK is the address of the stack-block for all the arguments,
4143 or 0 on a machine where arguments are pushed individually.
4145 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
4146 so must be careful about how the stack is used.
4148 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
4149 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
4150 that we need not worry about saving and restoring the stack.
4152 FNDECL is the declaration of the function we are calling.
4154 Return nonzero if this arg should cause sibcall failure,
4155 zero otherwise. */
4157 static int
4158 store_one_arg (struct arg_data *arg, rtx argblock, int flags,
4159 int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
4161 tree pval = arg->tree_value;
4162 rtx reg = 0;
4163 int partial = 0;
4164 int used = 0;
4165 int i, lower_bound = 0, upper_bound = 0;
4166 int sibcall_failure = 0;
4168 if (TREE_CODE (pval) == ERROR_MARK)
4169 return 1;
4171 /* Push a new temporary level for any temporaries we make for
4172 this argument. */
4173 push_temp_slots ();
4175 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
4177 /* If this is being stored into a pre-allocated, fixed-size, stack area,
4178 save any previous data at that location. */
4179 if (argblock && ! variable_size && arg->stack)
4181 #ifdef ARGS_GROW_DOWNWARD
4182 /* stack_slot is negative, but we want to index stack_usage_map
4183 with positive values. */
4184 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4185 upper_bound = -INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1)) + 1;
4186 else
4187 upper_bound = 0;
4189 lower_bound = upper_bound - arg->locate.size.constant;
4190 #else
4191 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4192 lower_bound = INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1));
4193 else
4194 lower_bound = 0;
4196 upper_bound = lower_bound + arg->locate.size.constant;
4197 #endif
4199 i = lower_bound;
4200 /* Don't worry about things in the fixed argument area;
4201 it has already been saved. */
4202 if (i < reg_parm_stack_space)
4203 i = reg_parm_stack_space;
4204 while (i < upper_bound && stack_usage_map[i] == 0)
4205 i++;
4207 if (i < upper_bound)
4209 /* We need to make a save area. */
4210 unsigned int size = arg->locate.size.constant * BITS_PER_UNIT;
4211 enum machine_mode save_mode = mode_for_size (size, MODE_INT, 1);
4212 rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
4213 rtx stack_area = gen_rtx_MEM (save_mode, adr);
4215 if (save_mode == BLKmode)
4217 tree ot = TREE_TYPE (arg->tree_value);
4218 tree nt = build_qualified_type (ot, (TYPE_QUALS (ot)
4219 | TYPE_QUAL_CONST));
4221 arg->save_area = assign_temp (nt, 0, 1, 1);
4222 preserve_temp_slots (arg->save_area);
4223 emit_block_move (validize_mem (arg->save_area), stack_area,
4224 expr_size (arg->tree_value),
4225 BLOCK_OP_CALL_PARM);
4227 else
4229 arg->save_area = gen_reg_rtx (save_mode);
4230 emit_move_insn (arg->save_area, stack_area);
4236 /* If this isn't going to be placed on both the stack and in registers,
4237 set up the register and number of words. */
4238 if (! arg->pass_on_stack)
4240 if (flags & ECF_SIBCALL)
4241 reg = arg->tail_call_reg;
4242 else
4243 reg = arg->reg;
4244 partial = arg->partial;
4247 if (reg != 0 && partial == 0)
4248 /* Being passed entirely in a register. We shouldn't be called in
4249 this case. */
4250 abort ();
4252 /* If this arg needs special alignment, don't load the registers
4253 here. */
4254 if (arg->n_aligned_regs != 0)
4255 reg = 0;
4257 /* If this is being passed partially in a register, we can't evaluate
4258 it directly into its stack slot. Otherwise, we can. */
4259 if (arg->value == 0)
4261 /* stack_arg_under_construction is nonzero if a function argument is
4262 being evaluated directly into the outgoing argument list and
4263 expand_call must take special action to preserve the argument list
4264 if it is called recursively.
4266 For scalar function arguments stack_usage_map is sufficient to
4267 determine which stack slots must be saved and restored. Scalar
4268 arguments in general have pass_on_stack == 0.
4270 If this argument is initialized by a function which takes the
4271 address of the argument (a C++ constructor or a C function
4272 returning a BLKmode structure), then stack_usage_map is
4273 insufficient and expand_call must push the stack around the
4274 function call. Such arguments have pass_on_stack == 1.
4276 Note that it is always safe to set stack_arg_under_construction,
4277 but this generates suboptimal code if set when not needed. */
4279 if (arg->pass_on_stack)
4280 stack_arg_under_construction++;
4282 arg->value = expand_expr (pval,
4283 (partial
4284 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
4285 ? NULL_RTX : arg->stack,
4286 VOIDmode, EXPAND_STACK_PARM);
4288 /* If we are promoting object (or for any other reason) the mode
4289 doesn't agree, convert the mode. */
4291 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
4292 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
4293 arg->value, arg->unsignedp);
4295 if (arg->pass_on_stack)
4296 stack_arg_under_construction--;
4299 /* Don't allow anything left on stack from computation
4300 of argument to alloca. */
4301 if (flags & ECF_MAY_BE_ALLOCA)
4302 do_pending_stack_adjust ();
4304 if (arg->value == arg->stack)
4305 /* If the value is already in the stack slot, we are done. */
4307 else if (arg->mode != BLKmode)
4309 int size;
4311 /* Argument is a scalar, not entirely passed in registers.
4312 (If part is passed in registers, arg->partial says how much
4313 and emit_push_insn will take care of putting it there.)
4315 Push it, and if its size is less than the
4316 amount of space allocated to it,
4317 also bump stack pointer by the additional space.
4318 Note that in C the default argument promotions
4319 will prevent such mismatches. */
4321 size = GET_MODE_SIZE (arg->mode);
4322 /* Compute how much space the push instruction will push.
4323 On many machines, pushing a byte will advance the stack
4324 pointer by a halfword. */
4325 #ifdef PUSH_ROUNDING
4326 size = PUSH_ROUNDING (size);
4327 #endif
4328 used = size;
4330 /* Compute how much space the argument should get:
4331 round up to a multiple of the alignment for arguments. */
4332 if (none != FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)))
4333 used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
4334 / (PARM_BOUNDARY / BITS_PER_UNIT))
4335 * (PARM_BOUNDARY / BITS_PER_UNIT));
4337 /* This isn't already where we want it on the stack, so put it there.
4338 This can either be done with push or copy insns. */
4339 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), NULL_RTX,
4340 PARM_BOUNDARY, partial, reg, used - size, argblock,
4341 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4342 ARGS_SIZE_RTX (arg->locate.alignment_pad));
4344 /* Unless this is a partially-in-register argument, the argument is now
4345 in the stack. */
4346 if (partial == 0)
4347 arg->value = arg->stack;
4349 else
4351 /* BLKmode, at least partly to be pushed. */
4353 unsigned int parm_align;
4354 int excess;
4355 rtx size_rtx;
4357 /* Pushing a nonscalar.
4358 If part is passed in registers, PARTIAL says how much
4359 and emit_push_insn will take care of putting it there. */
4361 /* Round its size up to a multiple
4362 of the allocation unit for arguments. */
4364 if (arg->locate.size.var != 0)
4366 excess = 0;
4367 size_rtx = ARGS_SIZE_RTX (arg->locate.size);
4369 else
4371 /* PUSH_ROUNDING has no effect on us, because
4372 emit_push_insn for BLKmode is careful to avoid it. */
4373 if (reg && GET_CODE (reg) == PARALLEL)
4375 /* Use the size of the elt to compute excess. */
4376 rtx elt = XEXP (XVECEXP (reg, 0, 0), 0);
4377 excess = (arg->locate.size.constant
4378 - int_size_in_bytes (TREE_TYPE (pval))
4379 + partial * GET_MODE_SIZE (GET_MODE (elt)));
4381 else
4382 excess = (arg->locate.size.constant
4383 - int_size_in_bytes (TREE_TYPE (pval))
4384 + partial * UNITS_PER_WORD);
4385 size_rtx = expand_expr (size_in_bytes (TREE_TYPE (pval)),
4386 NULL_RTX, TYPE_MODE (sizetype), 0);
4389 /* Some types will require stricter alignment, which will be
4390 provided for elsewhere in argument layout. */
4391 parm_align = MAX (PARM_BOUNDARY, TYPE_ALIGN (TREE_TYPE (pval)));
4393 /* When an argument is padded down, the block is aligned to
4394 PARM_BOUNDARY, but the actual argument isn't. */
4395 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
4397 if (arg->locate.size.var)
4398 parm_align = BITS_PER_UNIT;
4399 else if (excess)
4401 unsigned int excess_align = (excess & -excess) * BITS_PER_UNIT;
4402 parm_align = MIN (parm_align, excess_align);
4406 if ((flags & ECF_SIBCALL) && MEM_P (arg->value))
4408 /* emit_push_insn might not work properly if arg->value and
4409 argblock + arg->locate.offset areas overlap. */
4410 rtx x = arg->value;
4411 int i = 0;
4413 if (XEXP (x, 0) == current_function_internal_arg_pointer
4414 || (GET_CODE (XEXP (x, 0)) == PLUS
4415 && XEXP (XEXP (x, 0), 0) ==
4416 current_function_internal_arg_pointer
4417 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4419 if (XEXP (x, 0) != current_function_internal_arg_pointer)
4420 i = INTVAL (XEXP (XEXP (x, 0), 1));
4422 /* expand_call should ensure this. */
4423 if (arg->locate.offset.var || GET_CODE (size_rtx) != CONST_INT)
4424 abort ();
4426 if (arg->locate.offset.constant > i)
4428 if (arg->locate.offset.constant < i + INTVAL (size_rtx))
4429 sibcall_failure = 1;
4431 else if (arg->locate.offset.constant < i)
4433 if (i < arg->locate.offset.constant + INTVAL (size_rtx))
4434 sibcall_failure = 1;
4439 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
4440 parm_align, partial, reg, excess, argblock,
4441 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4442 ARGS_SIZE_RTX (arg->locate.alignment_pad));
4444 /* Unless this is a partially-in-register argument, the argument is now
4445 in the stack.
4447 ??? Unlike the case above, in which we want the actual
4448 address of the data, so that we can load it directly into a
4449 register, here we want the address of the stack slot, so that
4450 it's properly aligned for word-by-word copying or something
4451 like that. It's not clear that this is always correct. */
4452 if (partial == 0)
4453 arg->value = arg->stack_slot;
4456 /* Mark all slots this store used. */
4457 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
4458 && argblock && ! variable_size && arg->stack)
4459 for (i = lower_bound; i < upper_bound; i++)
4460 stack_usage_map[i] = 1;
4462 /* Once we have pushed something, pops can't safely
4463 be deferred during the rest of the arguments. */
4464 NO_DEFER_POP;
4466 /* ANSI doesn't require a sequence point here,
4467 but PCC has one, so this will avoid some problems. */
4468 emit_queue ();
4470 /* Free any temporary slots made in processing this argument. Show
4471 that we might have taken the address of something and pushed that
4472 as an operand. */
4473 preserve_temp_slots (NULL_RTX);
4474 free_temp_slots ();
4475 pop_temp_slots ();
4477 return sibcall_failure;
4480 /* Nonzero if we do not know how to pass TYPE solely in registers.
4481 We cannot do so in the following cases:
4483 - if the type has variable size
4484 - if the type is marked as addressable (it is required to be constructed
4485 into the stack)
4486 - if the padding and mode of the type is such that a copy into a register
4487 would put it into the wrong part of the register.
4489 Which padding can't be supported depends on the byte endianness.
4491 A value in a register is implicitly padded at the most significant end.
4492 On a big-endian machine, that is the lower end in memory.
4493 So a value padded in memory at the upper end can't go in a register.
4494 For a little-endian machine, the reverse is true. */
4496 bool
4497 default_must_pass_in_stack (enum machine_mode mode, tree type)
4499 if (!type)
4500 return false;
4502 /* If the type has variable size... */
4503 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4504 return true;
4506 /* If the type is marked as addressable (it is required
4507 to be constructed into the stack)... */
4508 if (TREE_ADDRESSABLE (type))
4509 return true;
4511 /* If the padding and mode of the type is such that a copy into
4512 a register would put it into the wrong part of the register. */
4513 if (mode == BLKmode
4514 && int_size_in_bytes (type) % (PARM_BOUNDARY / BITS_PER_UNIT)
4515 && (FUNCTION_ARG_PADDING (mode, type)
4516 == (BYTES_BIG_ENDIAN ? upward : downward)))
4517 return true;
4519 return false;