* gnu/regexp/CharIndexedReader.java: Removed.
[official-gcc.git] / gcc / calls.c
blob35021ea137fae1cbd26ced7b9748f2b64925217a
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 int calls_function (tree, int);
121 static int calls_function_1 (tree, int);
123 static void emit_call_1 (rtx, tree, tree, tree, HOST_WIDE_INT, HOST_WIDE_INT,
124 HOST_WIDE_INT, rtx, rtx, int, rtx, int,
125 CUMULATIVE_ARGS *);
126 static void precompute_register_parameters (int, struct arg_data *, int *);
127 static int store_one_arg (struct arg_data *, rtx, int, int, int);
128 static void store_unaligned_arguments_into_pseudos (struct arg_data *, int);
129 static int finalize_must_preallocate (int, int, struct arg_data *,
130 struct args_size *);
131 static void precompute_arguments (int, int, struct arg_data *);
132 static int compute_argument_block_size (int, struct args_size *, int);
133 static void initialize_argument_information (int, struct arg_data *,
134 struct args_size *, int, tree,
135 tree, CUMULATIVE_ARGS *, int,
136 rtx *, int *, int *, int *,
137 bool *, bool);
138 static void compute_argument_addresses (struct arg_data *, rtx, int);
139 static rtx rtx_for_function_call (tree, tree);
140 static void load_register_parameters (struct arg_data *, int, rtx *, int,
141 int, int *);
142 static rtx emit_library_call_value_1 (int, rtx, rtx, enum libcall_type,
143 enum machine_mode, int, va_list);
144 static int special_function_p (tree, int);
145 static int check_sibcall_argument_overlap_1 (rtx);
146 static int check_sibcall_argument_overlap (rtx, struct arg_data *, int);
148 static int combine_pending_stack_adjustment_and_call (int, struct args_size *,
149 int);
150 static tree fix_unsafe_tree (tree);
151 static bool shift_returned_value (tree, rtx *);
153 #ifdef REG_PARM_STACK_SPACE
154 static rtx save_fixed_argument_area (int, rtx, int *, int *);
155 static void restore_fixed_argument_area (rtx, rtx, int, int);
156 #endif
158 /* If WHICH is 1, return 1 if EXP contains a call to the built-in function
159 `alloca'.
161 If WHICH is 0, return 1 if EXP contains a call to any function.
162 Actually, we only need return 1 if evaluating EXP would require pushing
163 arguments on the stack, but that is too difficult to compute, so we just
164 assume any function call might require the stack. */
166 static tree calls_function_save_exprs;
168 static int
169 calls_function (tree exp, int which)
171 int val;
173 calls_function_save_exprs = 0;
174 val = calls_function_1 (exp, which);
175 calls_function_save_exprs = 0;
176 return val;
179 /* Recursive function to do the work of above function. */
181 static int
182 calls_function_1 (tree exp, int which)
184 int i;
185 enum tree_code code = TREE_CODE (exp);
186 int class = TREE_CODE_CLASS (code);
187 int length = first_rtl_op (code);
189 /* If this code is language-specific, we don't know what it will do. */
190 if ((int) code >= NUM_TREE_CODES)
191 return 1;
193 switch (code)
195 case CALL_EXPR:
196 if (which == 0)
197 return 1;
198 else if ((TREE_CODE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))))
199 == FUNCTION_TYPE)
200 && (TYPE_RETURNS_STACK_DEPRESSED
201 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))))))
202 return 1;
203 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
204 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
205 == FUNCTION_DECL)
206 && (special_function_p (TREE_OPERAND (TREE_OPERAND (exp, 0), 0),
208 & ECF_MAY_BE_ALLOCA))
209 return 1;
211 break;
213 case CONSTRUCTOR:
215 tree tem;
217 for (tem = CONSTRUCTOR_ELTS (exp); tem != 0; tem = TREE_CHAIN (tem))
218 if (calls_function_1 (TREE_VALUE (tem), which))
219 return 1;
222 return 0;
224 case SAVE_EXPR:
225 if (SAVE_EXPR_RTL (exp) != 0)
226 return 0;
227 if (value_member (exp, calls_function_save_exprs))
228 return 0;
229 calls_function_save_exprs = tree_cons (NULL_TREE, exp,
230 calls_function_save_exprs);
231 return (TREE_OPERAND (exp, 0) != 0
232 && calls_function_1 (TREE_OPERAND (exp, 0), which));
234 case BLOCK:
236 tree local;
237 tree subblock;
239 for (local = BLOCK_VARS (exp); local; local = TREE_CHAIN (local))
240 if (DECL_INITIAL (local) != 0
241 && calls_function_1 (DECL_INITIAL (local), which))
242 return 1;
244 for (subblock = BLOCK_SUBBLOCKS (exp);
245 subblock;
246 subblock = TREE_CHAIN (subblock))
247 if (calls_function_1 (subblock, which))
248 return 1;
250 return 0;
252 case TREE_LIST:
253 for (; exp != 0; exp = TREE_CHAIN (exp))
254 if (calls_function_1 (TREE_VALUE (exp), which))
255 return 1;
256 return 0;
258 default:
259 break;
262 /* Only expressions and blocks can contain calls.
263 Blocks were handled above. */
264 if (! IS_EXPR_CODE_CLASS (class))
265 return 0;
267 for (i = 0; i < length; i++)
268 if (TREE_OPERAND (exp, i) != 0
269 && calls_function_1 (TREE_OPERAND (exp, i), which))
270 return 1;
272 return 0;
275 /* Force FUNEXP into a form suitable for the address of a CALL,
276 and return that as an rtx. Also load the static chain register
277 if FNDECL is a nested function.
279 CALL_FUSAGE points to a variable holding the prospective
280 CALL_INSN_FUNCTION_USAGE information. */
283 prepare_call_address (rtx funexp, rtx static_chain_value,
284 rtx *call_fusage, int reg_parm_seen, int sibcallp)
286 funexp = protect_from_queue (funexp, 0);
288 /* Make a valid memory address and copy constants through pseudo-regs,
289 but not for a constant address if -fno-function-cse. */
290 if (GET_CODE (funexp) != SYMBOL_REF)
291 /* If we are using registers for parameters, force the
292 function address into a register now. */
293 funexp = ((SMALL_REGISTER_CLASSES && reg_parm_seen)
294 ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
295 : memory_address (FUNCTION_MODE, funexp));
296 else if (! sibcallp)
298 #ifndef NO_FUNCTION_CSE
299 if (optimize && ! flag_no_function_cse)
300 #ifdef NO_RECURSIVE_FUNCTION_CSE
301 if (fndecl != current_function_decl)
302 #endif
303 funexp = force_reg (Pmode, funexp);
304 #endif
307 if (static_chain_value != 0)
309 emit_move_insn (static_chain_rtx, static_chain_value);
311 if (GET_CODE (static_chain_rtx) == REG)
312 use_reg (call_fusage, static_chain_rtx);
315 return funexp;
318 /* Generate instructions to call function FUNEXP,
319 and optionally pop the results.
320 The CALL_INSN is the first insn generated.
322 FNDECL is the declaration node of the function. This is given to the
323 macro RETURN_POPS_ARGS to determine whether this function pops its own args.
325 FUNTYPE is the data type of the function. This is given to the macro
326 RETURN_POPS_ARGS to determine whether this function pops its own args.
327 We used to allow an identifier for library functions, but that doesn't
328 work when the return type is an aggregate type and the calling convention
329 says that the pointer to this aggregate is to be popped by the callee.
331 STACK_SIZE is the number of bytes of arguments on the stack,
332 ROUNDED_STACK_SIZE is that number rounded up to
333 PREFERRED_STACK_BOUNDARY; zero if the size is variable. This is
334 both to put into the call insn and to generate explicit popping
335 code if necessary.
337 STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
338 It is zero if this call doesn't want a structure value.
340 NEXT_ARG_REG is the rtx that results from executing
341 FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1)
342 just after all the args have had their registers assigned.
343 This could be whatever you like, but normally it is the first
344 arg-register beyond those used for args in this call,
345 or 0 if all the arg-registers are used in this call.
346 It is passed on to `gen_call' so you can put this info in the call insn.
348 VALREG is a hard register in which a value is returned,
349 or 0 if the call does not return a value.
351 OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
352 the args to this call were processed.
353 We restore `inhibit_defer_pop' to that value.
355 CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
356 denote registers used by the called function. */
358 static void
359 emit_call_1 (rtx funexp, tree fntree, tree fndecl ATTRIBUTE_UNUSED,
360 tree funtype ATTRIBUTE_UNUSED,
361 HOST_WIDE_INT stack_size ATTRIBUTE_UNUSED,
362 HOST_WIDE_INT rounded_stack_size,
363 HOST_WIDE_INT struct_value_size ATTRIBUTE_UNUSED,
364 rtx next_arg_reg ATTRIBUTE_UNUSED, rtx valreg,
365 int old_inhibit_defer_pop, rtx call_fusage, int ecf_flags,
366 CUMULATIVE_ARGS *args_so_far ATTRIBUTE_UNUSED)
368 rtx rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
369 rtx call_insn;
370 int already_popped = 0;
371 HOST_WIDE_INT n_popped = RETURN_POPS_ARGS (fndecl, funtype, stack_size);
372 #if defined (HAVE_call) && defined (HAVE_call_value)
373 rtx struct_value_size_rtx;
374 struct_value_size_rtx = GEN_INT (struct_value_size);
375 #endif
377 #ifdef CALL_POPS_ARGS
378 n_popped += CALL_POPS_ARGS (* args_so_far);
379 #endif
381 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
382 and we don't want to load it into a register as an optimization,
383 because prepare_call_address already did it if it should be done. */
384 if (GET_CODE (funexp) != SYMBOL_REF)
385 funexp = memory_address (FUNCTION_MODE, funexp);
387 #if defined (HAVE_sibcall_pop) && defined (HAVE_sibcall_value_pop)
388 if ((ecf_flags & ECF_SIBCALL)
389 && HAVE_sibcall_pop && HAVE_sibcall_value_pop
390 && (n_popped > 0 || stack_size == 0))
392 rtx n_pop = GEN_INT (n_popped);
393 rtx pat;
395 /* If this subroutine pops its own args, record that in the call insn
396 if possible, for the sake of frame pointer elimination. */
398 if (valreg)
399 pat = GEN_SIBCALL_VALUE_POP (valreg,
400 gen_rtx_MEM (FUNCTION_MODE, funexp),
401 rounded_stack_size_rtx, next_arg_reg,
402 n_pop);
403 else
404 pat = GEN_SIBCALL_POP (gen_rtx_MEM (FUNCTION_MODE, funexp),
405 rounded_stack_size_rtx, next_arg_reg, n_pop);
407 emit_call_insn (pat);
408 already_popped = 1;
410 else
411 #endif
413 #if defined (HAVE_call_pop) && defined (HAVE_call_value_pop)
414 /* If the target has "call" or "call_value" insns, then prefer them
415 if no arguments are actually popped. If the target does not have
416 "call" or "call_value" insns, then we must use the popping versions
417 even if the call has no arguments to pop. */
418 #if defined (HAVE_call) && defined (HAVE_call_value)
419 if (HAVE_call && HAVE_call_value && HAVE_call_pop && HAVE_call_value_pop
420 && n_popped > 0 && ! (ecf_flags & ECF_SP_DEPRESSED))
421 #else
422 if (HAVE_call_pop && HAVE_call_value_pop)
423 #endif
425 rtx n_pop = GEN_INT (n_popped);
426 rtx pat;
428 /* If this subroutine pops its own args, record that in the call insn
429 if possible, for the sake of frame pointer elimination. */
431 if (valreg)
432 pat = GEN_CALL_VALUE_POP (valreg,
433 gen_rtx_MEM (FUNCTION_MODE, funexp),
434 rounded_stack_size_rtx, next_arg_reg, n_pop);
435 else
436 pat = GEN_CALL_POP (gen_rtx_MEM (FUNCTION_MODE, funexp),
437 rounded_stack_size_rtx, next_arg_reg, n_pop);
439 emit_call_insn (pat);
440 already_popped = 1;
442 else
443 #endif
445 #if defined (HAVE_sibcall) && defined (HAVE_sibcall_value)
446 if ((ecf_flags & ECF_SIBCALL)
447 && HAVE_sibcall && HAVE_sibcall_value)
449 if (valreg)
450 emit_call_insn (GEN_SIBCALL_VALUE (valreg,
451 gen_rtx_MEM (FUNCTION_MODE, funexp),
452 rounded_stack_size_rtx,
453 next_arg_reg, NULL_RTX));
454 else
455 emit_call_insn (GEN_SIBCALL (gen_rtx_MEM (FUNCTION_MODE, funexp),
456 rounded_stack_size_rtx, next_arg_reg,
457 struct_value_size_rtx));
459 else
460 #endif
462 #if defined (HAVE_call) && defined (HAVE_call_value)
463 if (HAVE_call && HAVE_call_value)
465 if (valreg)
466 emit_call_insn (GEN_CALL_VALUE (valreg,
467 gen_rtx_MEM (FUNCTION_MODE, funexp),
468 rounded_stack_size_rtx, next_arg_reg,
469 NULL_RTX));
470 else
471 emit_call_insn (GEN_CALL (gen_rtx_MEM (FUNCTION_MODE, funexp),
472 rounded_stack_size_rtx, next_arg_reg,
473 struct_value_size_rtx));
475 else
476 #endif
477 abort ();
479 /* Find the call we just emitted. */
480 call_insn = last_call_insn ();
482 /* Mark memory as used for "pure" function call. */
483 if (ecf_flags & ECF_PURE)
484 call_fusage
485 = gen_rtx_EXPR_LIST
486 (VOIDmode,
487 gen_rtx_USE (VOIDmode,
488 gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode))),
489 call_fusage);
491 /* Put the register usage information there. */
492 add_function_usage_to (call_insn, call_fusage);
494 /* If this is a const call, then set the insn's unchanging bit. */
495 if (ecf_flags & (ECF_CONST | ECF_PURE))
496 CONST_OR_PURE_CALL_P (call_insn) = 1;
498 /* If this call can't throw, attach a REG_EH_REGION reg note to that
499 effect. */
500 if (ecf_flags & ECF_NOTHROW)
501 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, const0_rtx,
502 REG_NOTES (call_insn));
503 else
505 int rn = lookup_stmt_eh_region (fntree);
507 /* If rn < 0, then either (1) tree-ssa not used or (2) doesn't
508 throw, which we already took care of. */
509 if (rn > 0)
510 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, GEN_INT (rn),
511 REG_NOTES (call_insn));
512 note_current_region_may_contain_throw ();
515 if (ecf_flags & ECF_NORETURN)
516 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_NORETURN, const0_rtx,
517 REG_NOTES (call_insn));
518 if (ecf_flags & ECF_ALWAYS_RETURN)
519 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_ALWAYS_RETURN, const0_rtx,
520 REG_NOTES (call_insn));
522 if (ecf_flags & ECF_RETURNS_TWICE)
524 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_SETJMP, const0_rtx,
525 REG_NOTES (call_insn));
526 current_function_calls_setjmp = 1;
529 SIBLING_CALL_P (call_insn) = ((ecf_flags & ECF_SIBCALL) != 0);
531 /* Restore this now, so that we do defer pops for this call's args
532 if the context of the call as a whole permits. */
533 inhibit_defer_pop = old_inhibit_defer_pop;
535 if (n_popped > 0)
537 if (!already_popped)
538 CALL_INSN_FUNCTION_USAGE (call_insn)
539 = gen_rtx_EXPR_LIST (VOIDmode,
540 gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx),
541 CALL_INSN_FUNCTION_USAGE (call_insn));
542 rounded_stack_size -= n_popped;
543 rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
544 stack_pointer_delta -= n_popped;
547 if (!ACCUMULATE_OUTGOING_ARGS)
549 /* If returning from the subroutine does not automatically pop the args,
550 we need an instruction to pop them sooner or later.
551 Perhaps do it now; perhaps just record how much space to pop later.
553 If returning from the subroutine does pop the args, indicate that the
554 stack pointer will be changed. */
556 if (rounded_stack_size != 0)
558 if (ecf_flags & (ECF_SP_DEPRESSED | ECF_NORETURN | ECF_LONGJMP))
559 /* Just pretend we did the pop. */
560 stack_pointer_delta -= rounded_stack_size;
561 else if (flag_defer_pop && inhibit_defer_pop == 0
562 && ! (ecf_flags & (ECF_CONST | ECF_PURE)))
563 pending_stack_adjust += rounded_stack_size;
564 else
565 adjust_stack (rounded_stack_size_rtx);
568 /* When we accumulate outgoing args, we must avoid any stack manipulations.
569 Restore the stack pointer to its original value now. Usually
570 ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
571 On i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
572 popping variants of functions exist as well.
574 ??? We may optimize similar to defer_pop above, but it is
575 probably not worthwhile.
577 ??? It will be worthwhile to enable combine_stack_adjustments even for
578 such machines. */
579 else if (n_popped)
580 anti_adjust_stack (GEN_INT (n_popped));
583 /* Determine if the function identified by NAME and FNDECL is one with
584 special properties we wish to know about.
586 For example, if the function might return more than one time (setjmp), then
587 set RETURNS_TWICE to a nonzero value.
589 Similarly set LONGJMP for if the function is in the longjmp family.
591 Set MAY_BE_ALLOCA for any memory allocation function that might allocate
592 space from the stack such as alloca. */
594 static int
595 special_function_p (tree fndecl, int flags)
597 if (fndecl && DECL_NAME (fndecl)
598 && IDENTIFIER_LENGTH (DECL_NAME (fndecl)) <= 17
599 /* Exclude functions not at the file scope, or not `extern',
600 since they are not the magic functions we would otherwise
601 think they are.
602 FIXME: this should be handled with attributes, not with this
603 hacky imitation of DECL_ASSEMBLER_NAME. It's (also) wrong
604 because you can declare fork() inside a function if you
605 wish. */
606 && (DECL_CONTEXT (fndecl) == NULL_TREE
607 || TREE_CODE (DECL_CONTEXT (fndecl)) == TRANSLATION_UNIT_DECL)
608 && TREE_PUBLIC (fndecl))
610 const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
611 const char *tname = name;
613 /* We assume that alloca will always be called by name. It
614 makes no sense to pass it as a pointer-to-function to
615 anything that does not understand its behavior. */
616 if (((IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 6
617 && name[0] == 'a'
618 && ! strcmp (name, "alloca"))
619 || (IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 16
620 && name[0] == '_'
621 && ! strcmp (name, "__builtin_alloca"))))
622 flags |= ECF_MAY_BE_ALLOCA;
624 /* Disregard prefix _, __ or __x. */
625 if (name[0] == '_')
627 if (name[1] == '_' && name[2] == 'x')
628 tname += 3;
629 else if (name[1] == '_')
630 tname += 2;
631 else
632 tname += 1;
635 if (tname[0] == 's')
637 if ((tname[1] == 'e'
638 && (! strcmp (tname, "setjmp")
639 || ! strcmp (tname, "setjmp_syscall")))
640 || (tname[1] == 'i'
641 && ! strcmp (tname, "sigsetjmp"))
642 || (tname[1] == 'a'
643 && ! strcmp (tname, "savectx")))
644 flags |= ECF_RETURNS_TWICE;
646 if (tname[1] == 'i'
647 && ! strcmp (tname, "siglongjmp"))
648 flags |= ECF_LONGJMP;
650 else if ((tname[0] == 'q' && tname[1] == 's'
651 && ! strcmp (tname, "qsetjmp"))
652 || (tname[0] == 'v' && tname[1] == 'f'
653 && ! strcmp (tname, "vfork")))
654 flags |= ECF_RETURNS_TWICE;
656 else if (tname[0] == 'l' && tname[1] == 'o'
657 && ! strcmp (tname, "longjmp"))
658 flags |= ECF_LONGJMP;
661 return flags;
664 /* Return nonzero when tree represent call to longjmp. */
667 setjmp_call_p (tree fndecl)
669 return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
672 /* Return true when exp contains alloca call. */
673 bool
674 alloca_call_p (tree exp)
676 if (TREE_CODE (exp) == CALL_EXPR
677 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
678 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
679 == FUNCTION_DECL)
680 && (special_function_p (TREE_OPERAND (TREE_OPERAND (exp, 0), 0),
681 0) & ECF_MAY_BE_ALLOCA))
682 return true;
683 return false;
686 /* Detect flags (function attributes) from the function decl or type node. */
689 flags_from_decl_or_type (tree exp)
691 int flags = 0;
692 tree type = exp;
694 if (DECL_P (exp))
696 struct cgraph_rtl_info *i = cgraph_rtl_info (exp);
697 type = TREE_TYPE (exp);
699 if (i)
701 if (i->pure_function)
702 flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
703 if (i->const_function)
704 flags |= ECF_CONST | ECF_LIBCALL_BLOCK;
707 /* The function exp may have the `malloc' attribute. */
708 if (DECL_IS_MALLOC (exp))
709 flags |= ECF_MALLOC;
711 /* The function exp may have the `pure' attribute. */
712 if (DECL_IS_PURE (exp))
713 flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
715 if (TREE_NOTHROW (exp))
716 flags |= ECF_NOTHROW;
718 if (TREE_READONLY (exp) && ! TREE_THIS_VOLATILE (exp))
719 flags |= ECF_LIBCALL_BLOCK | ECF_CONST;
721 flags = special_function_p (exp, flags);
723 else if (TYPE_P (exp) && TYPE_READONLY (exp) && ! TREE_THIS_VOLATILE (exp))
724 flags |= ECF_CONST;
726 if (TREE_THIS_VOLATILE (exp))
727 flags |= ECF_NORETURN;
729 /* Mark if the function returns with the stack pointer depressed. We
730 cannot consider it pure or constant in that case. */
731 if (TREE_CODE (type) == FUNCTION_TYPE && TYPE_RETURNS_STACK_DEPRESSED (type))
733 flags |= ECF_SP_DEPRESSED;
734 flags &= ~(ECF_PURE | ECF_CONST | ECF_LIBCALL_BLOCK);
737 return flags;
740 /* Detect flags from a CALL_EXPR. */
743 call_expr_flags (tree t)
745 int flags;
746 tree decl = get_callee_fndecl (t);
748 if (decl)
749 flags = flags_from_decl_or_type (decl);
750 else
752 t = TREE_TYPE (TREE_OPERAND (t, 0));
753 if (t && TREE_CODE (t) == POINTER_TYPE)
754 flags = flags_from_decl_or_type (TREE_TYPE (t));
755 else
756 flags = 0;
759 return flags;
762 /* Precompute all register parameters as described by ARGS, storing values
763 into fields within the ARGS array.
765 NUM_ACTUALS indicates the total number elements in the ARGS array.
767 Set REG_PARM_SEEN if we encounter a register parameter. */
769 static void
770 precompute_register_parameters (int num_actuals, struct arg_data *args, int *reg_parm_seen)
772 int i;
774 *reg_parm_seen = 0;
776 for (i = 0; i < num_actuals; i++)
777 if (args[i].reg != 0 && ! args[i].pass_on_stack)
779 *reg_parm_seen = 1;
781 if (args[i].value == 0)
783 push_temp_slots ();
784 args[i].value = expand_expr (args[i].tree_value, NULL_RTX,
785 VOIDmode, 0);
786 preserve_temp_slots (args[i].value);
787 pop_temp_slots ();
789 /* ANSI doesn't require a sequence point here,
790 but PCC has one, so this will avoid some problems. */
791 emit_queue ();
794 /* If the value is a non-legitimate constant, force it into a
795 pseudo now. TLS symbols sometimes need a call to resolve. */
796 if (CONSTANT_P (args[i].value)
797 && !LEGITIMATE_CONSTANT_P (args[i].value))
798 args[i].value = force_reg (args[i].mode, args[i].value);
800 /* If we are to promote the function arg to a wider mode,
801 do it now. */
803 if (args[i].mode != TYPE_MODE (TREE_TYPE (args[i].tree_value)))
804 args[i].value
805 = convert_modes (args[i].mode,
806 TYPE_MODE (TREE_TYPE (args[i].tree_value)),
807 args[i].value, args[i].unsignedp);
809 /* If the value is expensive, and we are inside an appropriately
810 short loop, put the value into a pseudo and then put the pseudo
811 into the hard reg.
813 For small register classes, also do this if this call uses
814 register parameters. This is to avoid reload conflicts while
815 loading the parameters registers. */
817 if ((! (GET_CODE (args[i].value) == REG
818 || (GET_CODE (args[i].value) == SUBREG
819 && GET_CODE (SUBREG_REG (args[i].value)) == REG)))
820 && args[i].mode != BLKmode
821 && rtx_cost (args[i].value, SET) > COSTS_N_INSNS (1)
822 && ((SMALL_REGISTER_CLASSES && *reg_parm_seen)
823 || preserve_subexpressions_p ()))
824 args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
828 #ifdef REG_PARM_STACK_SPACE
830 /* The argument list is the property of the called routine and it
831 may clobber it. If the fixed area has been used for previous
832 parameters, we must save and restore it. */
834 static rtx
835 save_fixed_argument_area (int reg_parm_stack_space, rtx argblock, int *low_to_save, int *high_to_save)
837 int low;
838 int high;
840 /* Compute the boundary of the area that needs to be saved, if any. */
841 high = reg_parm_stack_space;
842 #ifdef ARGS_GROW_DOWNWARD
843 high += 1;
844 #endif
845 if (high > highest_outgoing_arg_in_use)
846 high = highest_outgoing_arg_in_use;
848 for (low = 0; low < high; low++)
849 if (stack_usage_map[low] != 0)
851 int num_to_save;
852 enum machine_mode save_mode;
853 int delta;
854 rtx stack_area;
855 rtx save_area;
857 while (stack_usage_map[--high] == 0)
860 *low_to_save = low;
861 *high_to_save = high;
863 num_to_save = high - low + 1;
864 save_mode = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
866 /* If we don't have the required alignment, must do this
867 in BLKmode. */
868 if ((low & (MIN (GET_MODE_SIZE (save_mode),
869 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
870 save_mode = BLKmode;
872 #ifdef ARGS_GROW_DOWNWARD
873 delta = -high;
874 #else
875 delta = low;
876 #endif
877 stack_area = gen_rtx_MEM (save_mode,
878 memory_address (save_mode,
879 plus_constant (argblock,
880 delta)));
882 set_mem_align (stack_area, PARM_BOUNDARY);
883 if (save_mode == BLKmode)
885 save_area = assign_stack_temp (BLKmode, num_to_save, 0);
886 emit_block_move (validize_mem (save_area), stack_area,
887 GEN_INT (num_to_save), BLOCK_OP_CALL_PARM);
889 else
891 save_area = gen_reg_rtx (save_mode);
892 emit_move_insn (save_area, stack_area);
895 return save_area;
898 return NULL_RTX;
901 static void
902 restore_fixed_argument_area (rtx save_area, rtx argblock, int high_to_save, int low_to_save)
904 enum machine_mode save_mode = GET_MODE (save_area);
905 int delta;
906 rtx stack_area;
908 #ifdef ARGS_GROW_DOWNWARD
909 delta = -high_to_save;
910 #else
911 delta = low_to_save;
912 #endif
913 stack_area = gen_rtx_MEM (save_mode,
914 memory_address (save_mode,
915 plus_constant (argblock, delta)));
916 set_mem_align (stack_area, PARM_BOUNDARY);
918 if (save_mode != BLKmode)
919 emit_move_insn (stack_area, save_area);
920 else
921 emit_block_move (stack_area, validize_mem (save_area),
922 GEN_INT (high_to_save - low_to_save + 1),
923 BLOCK_OP_CALL_PARM);
925 #endif /* REG_PARM_STACK_SPACE */
927 /* If any elements in ARGS refer to parameters that are to be passed in
928 registers, but not in memory, and whose alignment does not permit a
929 direct copy into registers. Copy the values into a group of pseudos
930 which we will later copy into the appropriate hard registers.
932 Pseudos for each unaligned argument will be stored into the array
933 args[argnum].aligned_regs. The caller is responsible for deallocating
934 the aligned_regs array if it is nonzero. */
936 static void
937 store_unaligned_arguments_into_pseudos (struct arg_data *args, int num_actuals)
939 int i, j;
941 for (i = 0; i < num_actuals; i++)
942 if (args[i].reg != 0 && ! args[i].pass_on_stack
943 && args[i].mode == BLKmode
944 && (TYPE_ALIGN (TREE_TYPE (args[i].tree_value))
945 < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
947 int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
948 int nregs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
949 int endian_correction = 0;
951 args[i].n_aligned_regs = args[i].partial ? args[i].partial : nregs;
952 args[i].aligned_regs = xmalloc (sizeof (rtx) * args[i].n_aligned_regs);
954 /* Structures smaller than a word are normally aligned to the
955 least significant byte. On a BYTES_BIG_ENDIAN machine,
956 this means we must skip the empty high order bytes when
957 calculating the bit offset. */
958 if (bytes < UNITS_PER_WORD
959 #ifdef BLOCK_REG_PADDING
960 && (BLOCK_REG_PADDING (args[i].mode,
961 TREE_TYPE (args[i].tree_value), 1)
962 == downward)
963 #else
964 && BYTES_BIG_ENDIAN
965 #endif
967 endian_correction = BITS_PER_WORD - bytes * BITS_PER_UNIT;
969 for (j = 0; j < args[i].n_aligned_regs; j++)
971 rtx reg = gen_reg_rtx (word_mode);
972 rtx word = operand_subword_force (args[i].value, j, BLKmode);
973 int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
975 args[i].aligned_regs[j] = reg;
976 word = extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
977 word_mode, word_mode, BITS_PER_WORD);
979 /* There is no need to restrict this code to loading items
980 in TYPE_ALIGN sized hunks. The bitfield instructions can
981 load up entire word sized registers efficiently.
983 ??? This may not be needed anymore.
984 We use to emit a clobber here but that doesn't let later
985 passes optimize the instructions we emit. By storing 0 into
986 the register later passes know the first AND to zero out the
987 bitfield being set in the register is unnecessary. The store
988 of 0 will be deleted as will at least the first AND. */
990 emit_move_insn (reg, const0_rtx);
992 bytes -= bitsize / BITS_PER_UNIT;
993 store_bit_field (reg, bitsize, endian_correction, word_mode,
994 word, BITS_PER_WORD);
999 /* Fill in ARGS_SIZE and ARGS array based on the parameters found in
1000 ACTPARMS.
1002 NUM_ACTUALS is the total number of parameters.
1004 N_NAMED_ARGS is the total number of named arguments.
1006 FNDECL is the tree code for the target of this call (if known)
1008 ARGS_SO_FAR holds state needed by the target to know where to place
1009 the next argument.
1011 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
1012 for arguments which are passed in registers.
1014 OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
1015 and may be modified by this routine.
1017 OLD_PENDING_ADJ, MUST_PREALLOCATE and FLAGS are pointers to integer
1018 flags which may may be modified by this routine.
1020 MAY_TAILCALL is cleared if we encounter an invisible pass-by-reference
1021 that requires allocation of stack space.
1023 CALL_FROM_THUNK_P is true if this call is the jump from a thunk to
1024 the thunked-to function. */
1026 static void
1027 initialize_argument_information (int num_actuals ATTRIBUTE_UNUSED,
1028 struct arg_data *args,
1029 struct args_size *args_size,
1030 int n_named_args ATTRIBUTE_UNUSED,
1031 tree actparms, tree fndecl,
1032 CUMULATIVE_ARGS *args_so_far,
1033 int reg_parm_stack_space,
1034 rtx *old_stack_level, int *old_pending_adj,
1035 int *must_preallocate, int *ecf_flags,
1036 bool *may_tailcall, bool call_from_thunk_p)
1038 /* 1 if scanning parms front to back, -1 if scanning back to front. */
1039 int inc;
1041 /* Count arg position in order args appear. */
1042 int argpos;
1044 int i;
1045 tree p;
1047 args_size->constant = 0;
1048 args_size->var = 0;
1050 /* In this loop, we consider args in the order they are written.
1051 We fill up ARGS from the front or from the back if necessary
1052 so that in any case the first arg to be pushed ends up at the front. */
1054 if (PUSH_ARGS_REVERSED)
1056 i = num_actuals - 1, inc = -1;
1057 /* In this case, must reverse order of args
1058 so that we compute and push the last arg first. */
1060 else
1062 i = 0, inc = 1;
1065 /* I counts args in order (to be) pushed; ARGPOS counts in order written. */
1066 for (p = actparms, argpos = 0; p; p = TREE_CHAIN (p), i += inc, argpos++)
1068 tree type = TREE_TYPE (TREE_VALUE (p));
1069 int unsignedp;
1070 enum machine_mode mode;
1072 args[i].tree_value = TREE_VALUE (p);
1074 /* Replace erroneous argument with constant zero. */
1075 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1076 args[i].tree_value = integer_zero_node, type = integer_type_node;
1078 /* If TYPE is a transparent union, pass things the way we would
1079 pass the first field of the union. We have already verified that
1080 the modes are the same. */
1081 if (TREE_CODE (type) == UNION_TYPE && TYPE_TRANSPARENT_UNION (type))
1082 type = TREE_TYPE (TYPE_FIELDS (type));
1084 /* Decide where to pass this arg.
1086 args[i].reg is nonzero if all or part is passed in registers.
1088 args[i].partial is nonzero if part but not all is passed in registers,
1089 and the exact value says how many words are passed in registers.
1091 args[i].pass_on_stack is nonzero if the argument must at least be
1092 computed on the stack. It may then be loaded back into registers
1093 if args[i].reg is nonzero.
1095 These decisions are driven by the FUNCTION_... macros and must agree
1096 with those made by function.c. */
1098 /* See if this argument should be passed by invisible reference. */
1099 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
1100 || TREE_ADDRESSABLE (type)
1101 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
1102 || FUNCTION_ARG_PASS_BY_REFERENCE (*args_so_far, TYPE_MODE (type),
1103 type, argpos < n_named_args)
1104 #endif
1107 /* If we're compiling a thunk, pass through invisible
1108 references instead of making a copy. */
1109 if (call_from_thunk_p
1110 #ifdef FUNCTION_ARG_CALLEE_COPIES
1111 || (FUNCTION_ARG_CALLEE_COPIES (*args_so_far, TYPE_MODE (type),
1112 type, argpos < n_named_args)
1113 /* If it's in a register, we must make a copy of it too. */
1114 /* ??? Is this a sufficient test? Is there a better one? */
1115 && !(TREE_CODE (args[i].tree_value) == VAR_DECL
1116 && REG_P (DECL_RTL (args[i].tree_value)))
1117 && ! TREE_ADDRESSABLE (type))
1118 #endif
1121 /* C++ uses a TARGET_EXPR to indicate that we want to make a
1122 new object from the argument. If we are passing by
1123 invisible reference, the callee will do that for us, so we
1124 can strip off the TARGET_EXPR. This is not always safe,
1125 but it is safe in the only case where this is a useful
1126 optimization; namely, when the argument is a plain object.
1127 In that case, the frontend is just asking the backend to
1128 make a bitwise copy of the argument. */
1130 if (TREE_CODE (args[i].tree_value) == TARGET_EXPR
1131 && (DECL_P (TREE_OPERAND (args[i].tree_value, 1)))
1132 && ! REG_P (DECL_RTL (TREE_OPERAND (args[i].tree_value, 1))))
1133 args[i].tree_value = TREE_OPERAND (args[i].tree_value, 1);
1135 args[i].tree_value = build1 (ADDR_EXPR,
1136 build_pointer_type (type),
1137 args[i].tree_value);
1138 type = build_pointer_type (type);
1140 else if (TREE_CODE (args[i].tree_value) == TARGET_EXPR)
1142 /* In the V3 C++ ABI, parameters are destroyed in the caller.
1143 We implement this by passing the address of the temporary
1144 rather than expanding it into another allocated slot. */
1145 args[i].tree_value = build1 (ADDR_EXPR,
1146 build_pointer_type (type),
1147 args[i].tree_value);
1148 type = build_pointer_type (type);
1149 *may_tailcall = false;
1151 else
1153 /* We make a copy of the object and pass the address to the
1154 function being called. */
1155 rtx copy;
1157 if (!COMPLETE_TYPE_P (type)
1158 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
1159 || (flag_stack_check && ! STACK_CHECK_BUILTIN
1160 && (0 < compare_tree_int (TYPE_SIZE_UNIT (type),
1161 STACK_CHECK_MAX_VAR_SIZE))))
1163 /* This is a variable-sized object. Make space on the stack
1164 for it. */
1165 rtx size_rtx = expr_size (TREE_VALUE (p));
1167 if (*old_stack_level == 0)
1169 emit_stack_save (SAVE_BLOCK, old_stack_level, NULL_RTX);
1170 *old_pending_adj = pending_stack_adjust;
1171 pending_stack_adjust = 0;
1174 copy = gen_rtx_MEM (BLKmode,
1175 allocate_dynamic_stack_space
1176 (size_rtx, NULL_RTX, TYPE_ALIGN (type)));
1177 set_mem_attributes (copy, type, 1);
1179 else
1180 copy = assign_temp (type, 0, 1, 0);
1182 store_expr (args[i].tree_value, copy, 0);
1183 *ecf_flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
1185 args[i].tree_value = build1 (ADDR_EXPR,
1186 build_pointer_type (type),
1187 make_tree (type, copy));
1188 type = build_pointer_type (type);
1189 *may_tailcall = false;
1193 mode = TYPE_MODE (type);
1194 unsignedp = TYPE_UNSIGNED (type);
1196 if (targetm.calls.promote_function_args (fndecl ? TREE_TYPE (fndecl) : 0))
1197 mode = promote_mode (type, mode, &unsignedp, 1);
1199 args[i].unsignedp = unsignedp;
1200 args[i].mode = mode;
1202 args[i].reg = FUNCTION_ARG (*args_so_far, mode, type,
1203 argpos < n_named_args);
1204 #ifdef FUNCTION_INCOMING_ARG
1205 /* If this is a sibling call and the machine has register windows, the
1206 register window has to be unwinded before calling the routine, so
1207 arguments have to go into the incoming registers. */
1208 args[i].tail_call_reg = FUNCTION_INCOMING_ARG (*args_so_far, mode, type,
1209 argpos < n_named_args);
1210 #else
1211 args[i].tail_call_reg = args[i].reg;
1212 #endif
1214 #ifdef FUNCTION_ARG_PARTIAL_NREGS
1215 if (args[i].reg)
1216 args[i].partial
1217 = FUNCTION_ARG_PARTIAL_NREGS (*args_so_far, mode, type,
1218 argpos < n_named_args);
1219 #endif
1221 args[i].pass_on_stack = MUST_PASS_IN_STACK (mode, type);
1223 /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
1224 it means that we are to pass this arg in the register(s) designated
1225 by the PARALLEL, but also to pass it in the stack. */
1226 if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
1227 && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
1228 args[i].pass_on_stack = 1;
1230 /* If this is an addressable type, we must preallocate the stack
1231 since we must evaluate the object into its final location.
1233 If this is to be passed in both registers and the stack, it is simpler
1234 to preallocate. */
1235 if (TREE_ADDRESSABLE (type)
1236 || (args[i].pass_on_stack && args[i].reg != 0))
1237 *must_preallocate = 1;
1239 /* If this is an addressable type, we cannot pre-evaluate it. Thus,
1240 we cannot consider this function call constant. */
1241 if (TREE_ADDRESSABLE (type))
1242 *ecf_flags &= ~ECF_LIBCALL_BLOCK;
1244 /* Compute the stack-size of this argument. */
1245 if (args[i].reg == 0 || args[i].partial != 0
1246 || reg_parm_stack_space > 0
1247 || args[i].pass_on_stack)
1248 locate_and_pad_parm (mode, type,
1249 #ifdef STACK_PARMS_IN_REG_PARM_AREA
1251 #else
1252 args[i].reg != 0,
1253 #endif
1254 args[i].pass_on_stack ? 0 : args[i].partial,
1255 fndecl, args_size, &args[i].locate);
1256 #ifdef BLOCK_REG_PADDING
1257 else
1258 /* The argument is passed entirely in registers. See at which
1259 end it should be padded. */
1260 args[i].locate.where_pad =
1261 BLOCK_REG_PADDING (mode, type,
1262 int_size_in_bytes (type) <= UNITS_PER_WORD);
1263 #endif
1265 /* Update ARGS_SIZE, the total stack space for args so far. */
1267 args_size->constant += args[i].locate.size.constant;
1268 if (args[i].locate.size.var)
1269 ADD_PARM_SIZE (*args_size, args[i].locate.size.var);
1271 /* Increment ARGS_SO_FAR, which has info about which arg-registers
1272 have been used, etc. */
1274 FUNCTION_ARG_ADVANCE (*args_so_far, TYPE_MODE (type), type,
1275 argpos < n_named_args);
1279 /* Update ARGS_SIZE to contain the total size for the argument block.
1280 Return the original constant component of the argument block's size.
1282 REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
1283 for arguments passed in registers. */
1285 static int
1286 compute_argument_block_size (int reg_parm_stack_space,
1287 struct args_size *args_size,
1288 int preferred_stack_boundary ATTRIBUTE_UNUSED)
1290 int unadjusted_args_size = args_size->constant;
1292 /* For accumulate outgoing args mode we don't need to align, since the frame
1293 will be already aligned. Align to STACK_BOUNDARY in order to prevent
1294 backends from generating misaligned frame sizes. */
1295 if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
1296 preferred_stack_boundary = STACK_BOUNDARY;
1298 /* Compute the actual size of the argument block required. The variable
1299 and constant sizes must be combined, the size may have to be rounded,
1300 and there may be a minimum required size. */
1302 if (args_size->var)
1304 args_size->var = ARGS_SIZE_TREE (*args_size);
1305 args_size->constant = 0;
1307 preferred_stack_boundary /= BITS_PER_UNIT;
1308 if (preferred_stack_boundary > 1)
1310 /* We don't handle this case yet. To handle it correctly we have
1311 to add the delta, round and subtract the delta.
1312 Currently no machine description requires this support. */
1313 if (stack_pointer_delta & (preferred_stack_boundary - 1))
1314 abort ();
1315 args_size->var = round_up (args_size->var, preferred_stack_boundary);
1318 if (reg_parm_stack_space > 0)
1320 args_size->var
1321 = size_binop (MAX_EXPR, args_size->var,
1322 ssize_int (reg_parm_stack_space));
1324 #ifndef OUTGOING_REG_PARM_STACK_SPACE
1325 /* The area corresponding to register parameters is not to count in
1326 the size of the block we need. So make the adjustment. */
1327 args_size->var
1328 = size_binop (MINUS_EXPR, args_size->var,
1329 ssize_int (reg_parm_stack_space));
1330 #endif
1333 else
1335 preferred_stack_boundary /= BITS_PER_UNIT;
1336 if (preferred_stack_boundary < 1)
1337 preferred_stack_boundary = 1;
1338 args_size->constant = (((args_size->constant
1339 + stack_pointer_delta
1340 + preferred_stack_boundary - 1)
1341 / preferred_stack_boundary
1342 * preferred_stack_boundary)
1343 - stack_pointer_delta);
1345 args_size->constant = MAX (args_size->constant,
1346 reg_parm_stack_space);
1348 #ifndef OUTGOING_REG_PARM_STACK_SPACE
1349 args_size->constant -= reg_parm_stack_space;
1350 #endif
1352 return unadjusted_args_size;
1355 /* Precompute parameters as needed for a function call.
1357 FLAGS is mask of ECF_* constants.
1359 NUM_ACTUALS is the number of arguments.
1361 ARGS is an array containing information for each argument; this
1362 routine fills in the INITIAL_VALUE and VALUE fields for each
1363 precomputed argument. */
1365 static void
1366 precompute_arguments (int flags, int num_actuals, struct arg_data *args)
1368 int i;
1370 /* If this is a libcall, then precompute all arguments so that we do not
1371 get extraneous instructions emitted as part of the libcall sequence.
1373 If this target defines ACCUMULATE_OUTGOING_ARGS to true, then we must
1374 precompute all arguments that contain function calls. Otherwise,
1375 computing arguments for a subcall may clobber arguments for this call.
1377 If this target defines ACCUMULATE_OUTGOING_ARGS to false, then we only
1378 need to precompute arguments that change the stack pointer, such as calls
1379 to alloca, and calls that do not pop all of their arguments. */
1381 for (i = 0; i < num_actuals; i++)
1382 if ((flags & ECF_LIBCALL_BLOCK)
1383 || calls_function (args[i].tree_value, !ACCUMULATE_OUTGOING_ARGS))
1385 enum machine_mode mode;
1387 /* If this is an addressable type, we cannot pre-evaluate it. */
1388 if (TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value)))
1389 abort ();
1391 args[i].value
1392 = expand_expr (args[i].tree_value, NULL_RTX, VOIDmode, 0);
1394 /* ANSI doesn't require a sequence point here,
1395 but PCC has one, so this will avoid some problems. */
1396 emit_queue ();
1398 args[i].initial_value = args[i].value
1399 = protect_from_queue (args[i].value, 0);
1401 mode = TYPE_MODE (TREE_TYPE (args[i].tree_value));
1402 if (mode != args[i].mode)
1404 args[i].value
1405 = convert_modes (args[i].mode, mode,
1406 args[i].value, args[i].unsignedp);
1407 #if defined(PROMOTE_FUNCTION_MODE) && !defined(PROMOTE_MODE)
1408 /* CSE will replace this only if it contains args[i].value
1409 pseudo, so convert it down to the declared mode using
1410 a SUBREG. */
1411 if (GET_CODE (args[i].value) == REG
1412 && GET_MODE_CLASS (args[i].mode) == MODE_INT)
1414 args[i].initial_value
1415 = gen_lowpart_SUBREG (mode, args[i].value);
1416 SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1417 SUBREG_PROMOTED_UNSIGNED_SET (args[i].initial_value,
1418 args[i].unsignedp);
1420 #endif
1425 /* Given the current state of MUST_PREALLOCATE and information about
1426 arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
1427 compute and return the final value for MUST_PREALLOCATE. */
1429 static int
1430 finalize_must_preallocate (int must_preallocate, int num_actuals, struct arg_data *args, struct args_size *args_size)
1432 /* See if we have or want to preallocate stack space.
1434 If we would have to push a partially-in-regs parm
1435 before other stack parms, preallocate stack space instead.
1437 If the size of some parm is not a multiple of the required stack
1438 alignment, we must preallocate.
1440 If the total size of arguments that would otherwise create a copy in
1441 a temporary (such as a CALL) is more than half the total argument list
1442 size, preallocation is faster.
1444 Another reason to preallocate is if we have a machine (like the m88k)
1445 where stack alignment is required to be maintained between every
1446 pair of insns, not just when the call is made. However, we assume here
1447 that such machines either do not have push insns (and hence preallocation
1448 would occur anyway) or the problem is taken care of with
1449 PUSH_ROUNDING. */
1451 if (! must_preallocate)
1453 int partial_seen = 0;
1454 int copy_to_evaluate_size = 0;
1455 int i;
1457 for (i = 0; i < num_actuals && ! must_preallocate; i++)
1459 if (args[i].partial > 0 && ! args[i].pass_on_stack)
1460 partial_seen = 1;
1461 else if (partial_seen && args[i].reg == 0)
1462 must_preallocate = 1;
1464 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1465 && (TREE_CODE (args[i].tree_value) == CALL_EXPR
1466 || TREE_CODE (args[i].tree_value) == TARGET_EXPR
1467 || TREE_CODE (args[i].tree_value) == COND_EXPR
1468 || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
1469 copy_to_evaluate_size
1470 += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1473 if (copy_to_evaluate_size * 2 >= args_size->constant
1474 && args_size->constant > 0)
1475 must_preallocate = 1;
1477 return must_preallocate;
1480 /* If we preallocated stack space, compute the address of each argument
1481 and store it into the ARGS array.
1483 We need not ensure it is a valid memory address here; it will be
1484 validized when it is used.
1486 ARGBLOCK is an rtx for the address of the outgoing arguments. */
1488 static void
1489 compute_argument_addresses (struct arg_data *args, rtx argblock, int num_actuals)
1491 if (argblock)
1493 rtx arg_reg = argblock;
1494 int i, arg_offset = 0;
1496 if (GET_CODE (argblock) == PLUS)
1497 arg_reg = XEXP (argblock, 0), arg_offset = INTVAL (XEXP (argblock, 1));
1499 for (i = 0; i < num_actuals; i++)
1501 rtx offset = ARGS_SIZE_RTX (args[i].locate.offset);
1502 rtx slot_offset = ARGS_SIZE_RTX (args[i].locate.slot_offset);
1503 rtx addr;
1505 /* Skip this parm if it will not be passed on the stack. */
1506 if (! args[i].pass_on_stack && args[i].reg != 0)
1507 continue;
1509 if (GET_CODE (offset) == CONST_INT)
1510 addr = plus_constant (arg_reg, INTVAL (offset));
1511 else
1512 addr = gen_rtx_PLUS (Pmode, arg_reg, offset);
1514 addr = plus_constant (addr, arg_offset);
1515 args[i].stack = gen_rtx_MEM (args[i].mode, addr);
1516 set_mem_align (args[i].stack, PARM_BOUNDARY);
1517 set_mem_attributes (args[i].stack,
1518 TREE_TYPE (args[i].tree_value), 1);
1520 if (GET_CODE (slot_offset) == CONST_INT)
1521 addr = plus_constant (arg_reg, INTVAL (slot_offset));
1522 else
1523 addr = gen_rtx_PLUS (Pmode, arg_reg, slot_offset);
1525 addr = plus_constant (addr, arg_offset);
1526 args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
1527 set_mem_align (args[i].stack_slot, PARM_BOUNDARY);
1528 set_mem_attributes (args[i].stack_slot,
1529 TREE_TYPE (args[i].tree_value), 1);
1531 /* Function incoming arguments may overlap with sibling call
1532 outgoing arguments and we cannot allow reordering of reads
1533 from function arguments with stores to outgoing arguments
1534 of sibling calls. */
1535 set_mem_alias_set (args[i].stack, 0);
1536 set_mem_alias_set (args[i].stack_slot, 0);
1541 /* Given a FNDECL and EXP, return an rtx suitable for use as a target address
1542 in a call instruction.
1544 FNDECL is the tree node for the target function. For an indirect call
1545 FNDECL will be NULL_TREE.
1547 ADDR is the operand 0 of CALL_EXPR for this call. */
1549 static rtx
1550 rtx_for_function_call (tree fndecl, tree addr)
1552 rtx funexp;
1554 /* Get the function to call, in the form of RTL. */
1555 if (fndecl)
1557 /* If this is the first use of the function, see if we need to
1558 make an external definition for it. */
1559 if (! TREE_USED (fndecl))
1561 assemble_external (fndecl);
1562 TREE_USED (fndecl) = 1;
1565 /* Get a SYMBOL_REF rtx for the function address. */
1566 funexp = XEXP (DECL_RTL (fndecl), 0);
1568 else
1569 /* Generate an rtx (probably a pseudo-register) for the address. */
1571 push_temp_slots ();
1572 funexp = expand_expr (addr, NULL_RTX, VOIDmode, 0);
1573 pop_temp_slots (); /* FUNEXP can't be BLKmode. */
1574 emit_queue ();
1576 return funexp;
1579 /* Do the register loads required for any wholly-register parms or any
1580 parms which are passed both on the stack and in a register. Their
1581 expressions were already evaluated.
1583 Mark all register-parms as living through the call, putting these USE
1584 insns in the CALL_INSN_FUNCTION_USAGE field.
1586 When IS_SIBCALL, perform the check_sibcall_overlap_argument_overlap
1587 checking, setting *SIBCALL_FAILURE if appropriate. */
1589 static void
1590 load_register_parameters (struct arg_data *args, int num_actuals,
1591 rtx *call_fusage, int flags, int is_sibcall,
1592 int *sibcall_failure)
1594 int i, j;
1596 for (i = 0; i < num_actuals; i++)
1598 rtx reg = ((flags & ECF_SIBCALL)
1599 ? args[i].tail_call_reg : args[i].reg);
1600 if (reg)
1602 int partial = args[i].partial;
1603 int nregs;
1604 int size = 0;
1605 rtx before_arg = get_last_insn ();
1606 /* Set to non-negative if must move a word at a time, even if just
1607 one word (e.g, partial == 1 && mode == DFmode). Set to -1 if
1608 we just use a normal move insn. This value can be zero if the
1609 argument is a zero size structure with no fields. */
1610 nregs = -1;
1611 if (partial)
1612 nregs = partial;
1613 else if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode)
1615 size = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1616 nregs = (size + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
1618 else
1619 size = GET_MODE_SIZE (args[i].mode);
1621 /* Handle calls that pass values in multiple non-contiguous
1622 locations. The Irix 6 ABI has examples of this. */
1624 if (GET_CODE (reg) == PARALLEL)
1626 tree type = TREE_TYPE (args[i].tree_value);
1627 emit_group_load (reg, args[i].value, type,
1628 int_size_in_bytes (type));
1631 /* If simple case, just do move. If normal partial, store_one_arg
1632 has already loaded the register for us. In all other cases,
1633 load the register(s) from memory. */
1635 else if (nregs == -1)
1637 emit_move_insn (reg, args[i].value);
1638 #ifdef BLOCK_REG_PADDING
1639 /* Handle case where we have a value that needs shifting
1640 up to the msb. eg. a QImode value and we're padding
1641 upward on a BYTES_BIG_ENDIAN machine. */
1642 if (size < UNITS_PER_WORD
1643 && (args[i].locate.where_pad
1644 == (BYTES_BIG_ENDIAN ? upward : downward)))
1646 rtx x;
1647 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
1649 /* Assigning REG here rather than a temp makes CALL_FUSAGE
1650 report the whole reg as used. Strictly speaking, the
1651 call only uses SIZE bytes at the msb end, but it doesn't
1652 seem worth generating rtl to say that. */
1653 reg = gen_rtx_REG (word_mode, REGNO (reg));
1654 x = expand_binop (word_mode, ashl_optab, reg,
1655 GEN_INT (shift), reg, 1, OPTAB_WIDEN);
1656 if (x != reg)
1657 emit_move_insn (reg, x);
1659 #endif
1662 /* If we have pre-computed the values to put in the registers in
1663 the case of non-aligned structures, copy them in now. */
1665 else if (args[i].n_aligned_regs != 0)
1666 for (j = 0; j < args[i].n_aligned_regs; j++)
1667 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
1668 args[i].aligned_regs[j]);
1670 else if (partial == 0 || args[i].pass_on_stack)
1672 rtx mem = validize_mem (args[i].value);
1674 /* Handle a BLKmode that needs shifting. */
1675 if (nregs == 1 && size < UNITS_PER_WORD
1676 #ifdef BLOCK_REG_PADDING
1677 && args[i].locate.where_pad == downward
1678 #else
1679 && BYTES_BIG_ENDIAN
1680 #endif
1683 rtx tem = operand_subword_force (mem, 0, args[i].mode);
1684 rtx ri = gen_rtx_REG (word_mode, REGNO (reg));
1685 rtx x = gen_reg_rtx (word_mode);
1686 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
1687 optab dir = BYTES_BIG_ENDIAN ? lshr_optab : ashl_optab;
1689 emit_move_insn (x, tem);
1690 x = expand_binop (word_mode, dir, x, GEN_INT (shift),
1691 ri, 1, OPTAB_WIDEN);
1692 if (x != ri)
1693 emit_move_insn (ri, x);
1695 else
1696 move_block_to_reg (REGNO (reg), mem, nregs, args[i].mode);
1699 /* When a parameter is a block, and perhaps in other cases, it is
1700 possible that it did a load from an argument slot that was
1701 already clobbered. */
1702 if (is_sibcall
1703 && check_sibcall_argument_overlap (before_arg, &args[i], 0))
1704 *sibcall_failure = 1;
1706 /* Handle calls that pass values in multiple non-contiguous
1707 locations. The Irix 6 ABI has examples of this. */
1708 if (GET_CODE (reg) == PARALLEL)
1709 use_group_regs (call_fusage, reg);
1710 else if (nregs == -1)
1711 use_reg (call_fusage, reg);
1712 else
1713 use_regs (call_fusage, REGNO (reg), nregs == 0 ? 1 : nregs);
1718 /* We need to pop PENDING_STACK_ADJUST bytes. But, if the arguments
1719 wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
1720 bytes, then we would need to push some additional bytes to pad the
1721 arguments. So, we compute an adjust to the stack pointer for an
1722 amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
1723 bytes. Then, when the arguments are pushed the stack will be perfectly
1724 aligned. ARGS_SIZE->CONSTANT is set to the number of bytes that should
1725 be popped after the call. Returns the adjustment. */
1727 static int
1728 combine_pending_stack_adjustment_and_call (int unadjusted_args_size,
1729 struct args_size *args_size,
1730 int preferred_unit_stack_boundary)
1732 /* The number of bytes to pop so that the stack will be
1733 under-aligned by UNADJUSTED_ARGS_SIZE bytes. */
1734 HOST_WIDE_INT adjustment;
1735 /* The alignment of the stack after the arguments are pushed, if we
1736 just pushed the arguments without adjust the stack here. */
1737 HOST_WIDE_INT unadjusted_alignment;
1739 unadjusted_alignment
1740 = ((stack_pointer_delta + unadjusted_args_size)
1741 % preferred_unit_stack_boundary);
1743 /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
1744 as possible -- leaving just enough left to cancel out the
1745 UNADJUSTED_ALIGNMENT. In other words, we want to ensure that the
1746 PENDING_STACK_ADJUST is non-negative, and congruent to
1747 -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY. */
1749 /* Begin by trying to pop all the bytes. */
1750 unadjusted_alignment
1751 = (unadjusted_alignment
1752 - (pending_stack_adjust % preferred_unit_stack_boundary));
1753 adjustment = pending_stack_adjust;
1754 /* Push enough additional bytes that the stack will be aligned
1755 after the arguments are pushed. */
1756 if (preferred_unit_stack_boundary > 1)
1758 if (unadjusted_alignment > 0)
1759 adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
1760 else
1761 adjustment += unadjusted_alignment;
1764 /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
1765 bytes after the call. The right number is the entire
1766 PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
1767 by the arguments in the first place. */
1768 args_size->constant
1769 = pending_stack_adjust - adjustment + unadjusted_args_size;
1771 return adjustment;
1774 /* Scan X expression if it does not dereference any argument slots
1775 we already clobbered by tail call arguments (as noted in stored_args_map
1776 bitmap).
1777 Return nonzero if X expression dereferences such argument slots,
1778 zero otherwise. */
1780 static int
1781 check_sibcall_argument_overlap_1 (rtx x)
1783 RTX_CODE code;
1784 int i, j;
1785 unsigned int k;
1786 const char *fmt;
1788 if (x == NULL_RTX)
1789 return 0;
1791 code = GET_CODE (x);
1793 if (code == MEM)
1795 if (XEXP (x, 0) == current_function_internal_arg_pointer)
1796 i = 0;
1797 else if (GET_CODE (XEXP (x, 0)) == PLUS
1798 && XEXP (XEXP (x, 0), 0) ==
1799 current_function_internal_arg_pointer
1800 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
1801 i = INTVAL (XEXP (XEXP (x, 0), 1));
1802 else
1803 return 0;
1805 #ifdef ARGS_GROW_DOWNWARD
1806 i = -i - GET_MODE_SIZE (GET_MODE (x));
1807 #endif
1809 for (k = 0; k < GET_MODE_SIZE (GET_MODE (x)); k++)
1810 if (i + k < stored_args_map->n_bits
1811 && TEST_BIT (stored_args_map, i + k))
1812 return 1;
1814 return 0;
1817 /* Scan all subexpressions. */
1818 fmt = GET_RTX_FORMAT (code);
1819 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
1821 if (*fmt == 'e')
1823 if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
1824 return 1;
1826 else if (*fmt == 'E')
1828 for (j = 0; j < XVECLEN (x, i); j++)
1829 if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
1830 return 1;
1833 return 0;
1836 /* Scan sequence after INSN if it does not dereference any argument slots
1837 we already clobbered by tail call arguments (as noted in stored_args_map
1838 bitmap). If MARK_STORED_ARGS_MAP, add stack slots for ARG to
1839 stored_args_map bitmap afterwards (when ARG is a register MARK_STORED_ARGS_MAP
1840 should be 0). Return nonzero if sequence after INSN dereferences such argument
1841 slots, zero otherwise. */
1843 static int
1844 check_sibcall_argument_overlap (rtx insn, struct arg_data *arg, int mark_stored_args_map)
1846 int low, high;
1848 if (insn == NULL_RTX)
1849 insn = get_insns ();
1850 else
1851 insn = NEXT_INSN (insn);
1853 for (; insn; insn = NEXT_INSN (insn))
1854 if (INSN_P (insn)
1855 && check_sibcall_argument_overlap_1 (PATTERN (insn)))
1856 break;
1858 if (mark_stored_args_map)
1860 #ifdef ARGS_GROW_DOWNWARD
1861 low = -arg->locate.slot_offset.constant - arg->locate.size.constant;
1862 #else
1863 low = arg->locate.slot_offset.constant;
1864 #endif
1866 for (high = low + arg->locate.size.constant; low < high; low++)
1867 SET_BIT (stored_args_map, low);
1869 return insn != NULL_RTX;
1872 static tree
1873 fix_unsafe_tree (tree t)
1875 switch (unsafe_for_reeval (t))
1877 case 0: /* Safe. */
1878 break;
1880 case 1: /* Mildly unsafe. */
1881 t = unsave_expr (t);
1882 break;
1884 case 2: /* Wildly unsafe. */
1886 tree var = build_decl (VAR_DECL, NULL_TREE,
1887 TREE_TYPE (t));
1888 SET_DECL_RTL (var,
1889 expand_expr (t, NULL_RTX, VOIDmode, EXPAND_NORMAL));
1890 t = var;
1892 break;
1894 default:
1895 abort ();
1897 return t;
1901 /* If function value *VALUE was returned at the most significant end of a
1902 register, shift it towards the least significant end and convert it to
1903 TYPE's mode. Return true and update *VALUE if some action was needed.
1905 TYPE is the type of the function's return value, which is known not
1906 to have mode BLKmode. */
1908 static bool
1909 shift_returned_value (tree type, rtx *value)
1911 if (targetm.calls.return_in_msb (type))
1913 HOST_WIDE_INT shift;
1915 shift = (GET_MODE_BITSIZE (GET_MODE (*value))
1916 - BITS_PER_UNIT * int_size_in_bytes (type));
1917 if (shift > 0)
1919 *value = expand_binop (GET_MODE (*value), lshr_optab, *value,
1920 GEN_INT (shift), 0, 1, OPTAB_WIDEN);
1921 *value = convert_to_mode (TYPE_MODE (type), *value, 0);
1922 return true;
1925 return false;
1928 /* Remove all REG_EQUIV notes found in the insn chain. */
1930 static void
1931 purge_reg_equiv_notes (void)
1933 rtx insn;
1935 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1937 while (1)
1939 rtx note = find_reg_note (insn, REG_EQUIV, 0);
1940 if (note)
1942 /* Remove the note and keep looking at the notes for
1943 this insn. */
1944 remove_note (insn, note);
1945 continue;
1947 break;
1952 /* Clear RTX_UNCHANGING_P flag of incoming argument MEMs. */
1954 static void
1955 purge_mem_unchanging_flag (rtx x)
1957 RTX_CODE code;
1958 int i, j;
1959 const char *fmt;
1961 if (x == NULL_RTX)
1962 return;
1964 code = GET_CODE (x);
1966 if (code == MEM)
1968 if (RTX_UNCHANGING_P (x)
1969 && (XEXP (x, 0) == current_function_internal_arg_pointer
1970 || (GET_CODE (XEXP (x, 0)) == PLUS
1971 && XEXP (XEXP (x, 0), 0) ==
1972 current_function_internal_arg_pointer
1973 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
1974 RTX_UNCHANGING_P (x) = 0;
1975 return;
1978 /* Scan all subexpressions. */
1979 fmt = GET_RTX_FORMAT (code);
1980 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
1982 if (*fmt == 'e')
1983 purge_mem_unchanging_flag (XEXP (x, i));
1984 else if (*fmt == 'E')
1985 for (j = 0; j < XVECLEN (x, i); j++)
1986 purge_mem_unchanging_flag (XVECEXP (x, i, j));
1991 /* Generate all the code for a function call
1992 and return an rtx for its value.
1993 Store the value in TARGET (specified as an rtx) if convenient.
1994 If the value is stored in TARGET then TARGET is returned.
1995 If IGNORE is nonzero, then we ignore the value of the function call. */
1998 expand_call (tree exp, rtx target, int ignore)
2000 /* Nonzero if we are currently expanding a call. */
2001 static int currently_expanding_call = 0;
2003 /* List of actual parameters. */
2004 tree actparms = TREE_OPERAND (exp, 1);
2005 /* RTX for the function to be called. */
2006 rtx funexp;
2007 /* Sequence of insns to perform a normal "call". */
2008 rtx normal_call_insns = NULL_RTX;
2009 /* Sequence of insns to perform a tail "call". */
2010 rtx tail_call_insns = NULL_RTX;
2011 /* Data type of the function. */
2012 tree funtype;
2013 tree type_arg_types;
2014 /* Declaration of the function being called,
2015 or 0 if the function is computed (not known by name). */
2016 tree fndecl = 0;
2017 /* The type of the function being called. */
2018 tree fntype;
2019 bool try_tail_call = CALL_EXPR_TAILCALL (exp);
2020 int pass;
2022 /* Register in which non-BLKmode value will be returned,
2023 or 0 if no value or if value is BLKmode. */
2024 rtx valreg;
2025 /* Address where we should return a BLKmode value;
2026 0 if value not BLKmode. */
2027 rtx structure_value_addr = 0;
2028 /* Nonzero if that address is being passed by treating it as
2029 an extra, implicit first parameter. Otherwise,
2030 it is passed by being copied directly into struct_value_rtx. */
2031 int structure_value_addr_parm = 0;
2032 /* Size of aggregate value wanted, or zero if none wanted
2033 or if we are using the non-reentrant PCC calling convention
2034 or expecting the value in registers. */
2035 HOST_WIDE_INT struct_value_size = 0;
2036 /* Nonzero if called function returns an aggregate in memory PCC style,
2037 by returning the address of where to find it. */
2038 int pcc_struct_value = 0;
2039 rtx struct_value = 0;
2041 /* Number of actual parameters in this call, including struct value addr. */
2042 int num_actuals;
2043 /* Number of named args. Args after this are anonymous ones
2044 and they must all go on the stack. */
2045 int n_named_args;
2047 /* Vector of information about each argument.
2048 Arguments are numbered in the order they will be pushed,
2049 not the order they are written. */
2050 struct arg_data *args;
2052 /* Total size in bytes of all the stack-parms scanned so far. */
2053 struct args_size args_size;
2054 struct args_size adjusted_args_size;
2055 /* Size of arguments before any adjustments (such as rounding). */
2056 int unadjusted_args_size;
2057 /* Data on reg parms scanned so far. */
2058 CUMULATIVE_ARGS args_so_far;
2059 /* Nonzero if a reg parm has been scanned. */
2060 int reg_parm_seen;
2061 /* Nonzero if this is an indirect function call. */
2063 /* Nonzero if we must avoid push-insns in the args for this call.
2064 If stack space is allocated for register parameters, but not by the
2065 caller, then it is preallocated in the fixed part of the stack frame.
2066 So the entire argument block must then be preallocated (i.e., we
2067 ignore PUSH_ROUNDING in that case). */
2069 int must_preallocate = !PUSH_ARGS;
2071 /* Size of the stack reserved for parameter registers. */
2072 int reg_parm_stack_space = 0;
2074 /* Address of space preallocated for stack parms
2075 (on machines that lack push insns), or 0 if space not preallocated. */
2076 rtx argblock = 0;
2078 /* Mask of ECF_ flags. */
2079 int flags = 0;
2080 #ifdef REG_PARM_STACK_SPACE
2081 /* Define the boundary of the register parm stack space that needs to be
2082 saved, if any. */
2083 int low_to_save, high_to_save;
2084 rtx save_area = 0; /* Place that it is saved */
2085 #endif
2087 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
2088 char *initial_stack_usage_map = stack_usage_map;
2090 int old_stack_allocated;
2092 /* State variables to track stack modifications. */
2093 rtx old_stack_level = 0;
2094 int old_stack_arg_under_construction = 0;
2095 int old_pending_adj = 0;
2096 int old_inhibit_defer_pop = inhibit_defer_pop;
2098 /* Some stack pointer alterations we make are performed via
2099 allocate_dynamic_stack_space. This modifies the stack_pointer_delta,
2100 which we then also need to save/restore along the way. */
2101 int old_stack_pointer_delta = 0;
2103 rtx call_fusage;
2104 tree p = TREE_OPERAND (exp, 0);
2105 tree addr = TREE_OPERAND (exp, 0);
2106 int i;
2107 /* The alignment of the stack, in bits. */
2108 HOST_WIDE_INT preferred_stack_boundary;
2109 /* The alignment of the stack, in bytes. */
2110 HOST_WIDE_INT preferred_unit_stack_boundary;
2111 /* The static chain value to use for this call. */
2112 rtx static_chain_value;
2113 /* See if this is "nothrow" function call. */
2114 if (TREE_NOTHROW (exp))
2115 flags |= ECF_NOTHROW;
2117 /* See if we can find a DECL-node for the actual function, and get the
2118 function attributes (flags) from the function decl or type node. */
2119 fndecl = get_callee_fndecl (exp);
2120 if (fndecl)
2122 fntype = TREE_TYPE (fndecl);
2123 flags |= flags_from_decl_or_type (fndecl);
2125 else
2127 fntype = TREE_TYPE (TREE_TYPE (p));
2128 flags |= flags_from_decl_or_type (fntype);
2131 struct_value = targetm.calls.struct_value_rtx (fntype, 0);
2133 /* Warn if this value is an aggregate type,
2134 regardless of which calling convention we are using for it. */
2135 if (warn_aggregate_return && AGGREGATE_TYPE_P (TREE_TYPE (exp)))
2136 warning ("function call has aggregate value");
2138 /* If the result of a pure or const function call is ignored (or void),
2139 and none of its arguments are volatile, we can avoid expanding the
2140 call and just evaluate the arguments for side-effects. */
2141 if ((flags & (ECF_CONST | ECF_PURE))
2142 && (ignore || target == const0_rtx
2143 || TYPE_MODE (TREE_TYPE (exp)) == VOIDmode))
2145 bool volatilep = false;
2146 tree arg;
2148 for (arg = actparms; arg; arg = TREE_CHAIN (arg))
2149 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
2151 volatilep = true;
2152 break;
2155 if (! volatilep)
2157 for (arg = actparms; arg; arg = TREE_CHAIN (arg))
2158 expand_expr (TREE_VALUE (arg), const0_rtx,
2159 VOIDmode, EXPAND_NORMAL);
2160 return const0_rtx;
2164 #ifdef REG_PARM_STACK_SPACE
2165 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
2166 #endif
2168 #ifndef OUTGOING_REG_PARM_STACK_SPACE
2169 if (reg_parm_stack_space > 0 && PUSH_ARGS)
2170 must_preallocate = 1;
2171 #endif
2173 /* Set up a place to return a structure. */
2175 /* Cater to broken compilers. */
2176 if (aggregate_value_p (exp, fndecl))
2178 /* This call returns a big structure. */
2179 flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
2181 #ifdef PCC_STATIC_STRUCT_RETURN
2183 pcc_struct_value = 1;
2185 #else /* not PCC_STATIC_STRUCT_RETURN */
2187 struct_value_size = int_size_in_bytes (TREE_TYPE (exp));
2189 if (CALL_EXPR_HAS_RETURN_SLOT_ADDR (exp))
2191 /* The structure value address arg is already in actparms.
2192 Pull it out. It might be nice to just leave it there, but
2193 we need to set structure_value_addr. */
2194 tree return_arg = TREE_VALUE (actparms);
2195 actparms = TREE_CHAIN (actparms);
2196 structure_value_addr = expand_expr (return_arg, NULL_RTX,
2197 VOIDmode, EXPAND_NORMAL);
2199 else if (target && GET_CODE (target) == MEM)
2200 structure_value_addr = XEXP (target, 0);
2201 else
2203 /* For variable-sized objects, we must be called with a target
2204 specified. If we were to allocate space on the stack here,
2205 we would have no way of knowing when to free it. */
2206 rtx d = assign_temp (TREE_TYPE (exp), 1, 1, 1);
2208 mark_temp_addr_taken (d);
2209 structure_value_addr = XEXP (d, 0);
2210 target = 0;
2213 #endif /* not PCC_STATIC_STRUCT_RETURN */
2216 /* Figure out the amount to which the stack should be aligned. */
2217 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
2218 if (fndecl)
2220 struct cgraph_rtl_info *i = cgraph_rtl_info (fndecl);
2221 if (i && i->preferred_incoming_stack_boundary)
2222 preferred_stack_boundary = i->preferred_incoming_stack_boundary;
2225 /* Operand 0 is a pointer-to-function; get the type of the function. */
2226 funtype = TREE_TYPE (addr);
2227 if (! POINTER_TYPE_P (funtype))
2228 abort ();
2229 funtype = TREE_TYPE (funtype);
2231 /* Munge the tree to split complex arguments into their imaginary
2232 and real parts. */
2233 if (targetm.calls.split_complex_arg)
2235 type_arg_types = split_complex_types (TYPE_ARG_TYPES (funtype));
2236 actparms = split_complex_values (actparms);
2238 else
2239 type_arg_types = TYPE_ARG_TYPES (funtype);
2241 if (flags & ECF_MAY_BE_ALLOCA)
2242 current_function_calls_alloca = 1;
2244 /* If struct_value_rtx is 0, it means pass the address
2245 as if it were an extra parameter. */
2246 if (structure_value_addr && struct_value == 0)
2248 /* If structure_value_addr is a REG other than
2249 virtual_outgoing_args_rtx, we can use always use it. If it
2250 is not a REG, we must always copy it into a register.
2251 If it is virtual_outgoing_args_rtx, we must copy it to another
2252 register in some cases. */
2253 rtx temp = (GET_CODE (structure_value_addr) != REG
2254 || (ACCUMULATE_OUTGOING_ARGS
2255 && stack_arg_under_construction
2256 && structure_value_addr == virtual_outgoing_args_rtx)
2257 ? copy_addr_to_reg (convert_memory_address
2258 (Pmode, structure_value_addr))
2259 : structure_value_addr);
2261 actparms
2262 = tree_cons (error_mark_node,
2263 make_tree (build_pointer_type (TREE_TYPE (funtype)),
2264 temp),
2265 actparms);
2266 structure_value_addr_parm = 1;
2269 /* Count the arguments and set NUM_ACTUALS. */
2270 for (p = actparms, num_actuals = 0; p; p = TREE_CHAIN (p))
2271 num_actuals++;
2273 /* Compute number of named args.
2274 Normally, don't include the last named arg if anonymous args follow.
2275 We do include the last named arg if
2276 targetm.calls.strict_argument_naming() returns nonzero.
2277 (If no anonymous args follow, the result of list_length is actually
2278 one too large. This is harmless.)
2280 If targetm.calls.pretend_outgoing_varargs_named() returns
2281 nonzero, and targetm.calls.strict_argument_naming() returns zero,
2282 this machine will be able to place unnamed args that were passed
2283 in registers into the stack. So treat all args as named. This
2284 allows the insns emitting for a specific argument list to be
2285 independent of the function declaration.
2287 If targetm.calls.pretend_outgoing_varargs_named() returns zero,
2288 we do not have any reliable way to pass unnamed args in
2289 registers, so we must force them into memory. */
2291 if ((targetm.calls.strict_argument_naming (&args_so_far)
2292 || ! targetm.calls.pretend_outgoing_varargs_named (&args_so_far))
2293 && type_arg_types != 0)
2294 n_named_args
2295 = (list_length (type_arg_types)
2296 /* Don't include the last named arg. */
2297 - (targetm.calls.strict_argument_naming (&args_so_far) ? 0 : 1)
2298 /* Count the struct value address, if it is passed as a parm. */
2299 + structure_value_addr_parm);
2300 else
2301 /* If we know nothing, treat all args as named. */
2302 n_named_args = num_actuals;
2304 /* Start updating where the next arg would go.
2306 On some machines (such as the PA) indirect calls have a different
2307 calling convention than normal calls. The fourth argument in
2308 INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
2309 or not. */
2310 INIT_CUMULATIVE_ARGS (args_so_far, funtype, NULL_RTX, fndecl, n_named_args);
2312 /* Make a vector to hold all the information about each arg. */
2313 args = alloca (num_actuals * sizeof (struct arg_data));
2314 memset (args, 0, num_actuals * sizeof (struct arg_data));
2316 /* Build up entries in the ARGS array, compute the size of the
2317 arguments into ARGS_SIZE, etc. */
2318 initialize_argument_information (num_actuals, args, &args_size,
2319 n_named_args, actparms, fndecl,
2320 &args_so_far, reg_parm_stack_space,
2321 &old_stack_level, &old_pending_adj,
2322 &must_preallocate, &flags,
2323 &try_tail_call, CALL_FROM_THUNK_P (exp));
2325 if (args_size.var)
2327 /* If this function requires a variable-sized argument list, don't
2328 try to make a cse'able block for this call. We may be able to
2329 do this eventually, but it is too complicated to keep track of
2330 what insns go in the cse'able block and which don't. */
2332 flags &= ~ECF_LIBCALL_BLOCK;
2333 must_preallocate = 1;
2336 /* Now make final decision about preallocating stack space. */
2337 must_preallocate = finalize_must_preallocate (must_preallocate,
2338 num_actuals, args,
2339 &args_size);
2341 /* If the structure value address will reference the stack pointer, we
2342 must stabilize it. We don't need to do this if we know that we are
2343 not going to adjust the stack pointer in processing this call. */
2345 if (structure_value_addr
2346 && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
2347 || reg_mentioned_p (virtual_outgoing_args_rtx,
2348 structure_value_addr))
2349 && (args_size.var
2350 || (!ACCUMULATE_OUTGOING_ARGS && args_size.constant)))
2351 structure_value_addr = copy_to_reg (structure_value_addr);
2353 /* Tail calls can make things harder to debug, and we're traditionally
2354 pushed these optimizations into -O2. Don't try if we're already
2355 expanding a call, as that means we're an argument. Don't try if
2356 there's cleanups, as we know there's code to follow the call.
2358 If rtx_equal_function_value_matters is false, that means we've
2359 finished with regular parsing. Which means that some of the
2360 machinery we use to generate tail-calls is no longer in place.
2361 This is most often true of sjlj-exceptions, which we couldn't
2362 tail-call to anyway.
2364 If current_nesting_level () == 0, we're being called after
2365 the function body has been expanded. This can happen when
2366 setting up trampolines in expand_function_end. */
2367 if (currently_expanding_call++ != 0
2368 || !flag_optimize_sibling_calls
2369 || !rtx_equal_function_value_matters
2370 || current_nesting_level () == 0
2371 || any_pending_cleanups ()
2372 || args_size.var
2373 || lookup_stmt_eh_region (exp) >= 0)
2374 try_tail_call = 0;
2376 /* Rest of purposes for tail call optimizations to fail. */
2377 if (
2378 #ifdef HAVE_sibcall_epilogue
2379 !HAVE_sibcall_epilogue
2380 #else
2382 #endif
2383 || !try_tail_call
2384 /* Doing sibling call optimization needs some work, since
2385 structure_value_addr can be allocated on the stack.
2386 It does not seem worth the effort since few optimizable
2387 sibling calls will return a structure. */
2388 || structure_value_addr != NULL_RTX
2389 /* Check whether the target is able to optimize the call
2390 into a sibcall. */
2391 || !targetm.function_ok_for_sibcall (fndecl, exp)
2392 /* Functions that do not return exactly once may not be sibcall
2393 optimized. */
2394 || (flags & (ECF_RETURNS_TWICE | ECF_LONGJMP | ECF_NORETURN))
2395 || TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (addr)))
2396 /* If the called function is nested in the current one, it might access
2397 some of the caller's arguments, but could clobber them beforehand if
2398 the argument areas are shared. */
2399 || (fndecl && decl_function_context (fndecl) == current_function_decl)
2400 /* If this function requires more stack slots than the current
2401 function, we cannot change it into a sibling call. */
2402 || args_size.constant > current_function_args_size
2403 /* If the callee pops its own arguments, then it must pop exactly
2404 the same number of arguments as the current function. */
2405 || (RETURN_POPS_ARGS (fndecl, funtype, args_size.constant)
2406 != RETURN_POPS_ARGS (current_function_decl,
2407 TREE_TYPE (current_function_decl),
2408 current_function_args_size))
2409 || !lang_hooks.decls.ok_for_sibcall (fndecl))
2410 try_tail_call = 0;
2412 if (try_tail_call)
2414 int end, inc;
2415 actparms = NULL_TREE;
2416 /* Ok, we're going to give the tail call the old college try.
2417 This means we're going to evaluate the function arguments
2418 up to three times. There are two degrees of badness we can
2419 encounter, those that can be unsaved and those that can't.
2420 (See unsafe_for_reeval commentary for details.)
2422 Generate a new argument list. Pass safe arguments through
2423 unchanged. For the easy badness wrap them in UNSAVE_EXPRs.
2424 For hard badness, evaluate them now and put their resulting
2425 rtx in a temporary VAR_DECL.
2427 initialize_argument_information has ordered the array for the
2428 order to be pushed, and we must remember this when reconstructing
2429 the original argument order. */
2431 if (PUSH_ARGS_REVERSED)
2433 inc = 1;
2434 i = 0;
2435 end = num_actuals;
2437 else
2439 inc = -1;
2440 i = num_actuals - 1;
2441 end = -1;
2444 for (; i != end; i += inc)
2446 args[i].tree_value = fix_unsafe_tree (args[i].tree_value);
2448 /* Do the same for the function address if it is an expression. */
2449 if (!fndecl)
2450 addr = fix_unsafe_tree (addr);
2451 /* Expanding one of those dangerous arguments could have added
2452 cleanups, but otherwise give it a whirl. */
2453 if (any_pending_cleanups ())
2454 try_tail_call = 0;
2458 /* Ensure current function's preferred stack boundary is at least
2459 what we need. We don't have to increase alignment for recursive
2460 functions. */
2461 if (cfun->preferred_stack_boundary < preferred_stack_boundary
2462 && fndecl != current_function_decl)
2463 cfun->preferred_stack_boundary = preferred_stack_boundary;
2464 if (fndecl == current_function_decl)
2465 cfun->recursive_call_emit = true;
2467 preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
2469 function_call_count++;
2471 /* We want to make two insn chains; one for a sibling call, the other
2472 for a normal call. We will select one of the two chains after
2473 initial RTL generation is complete. */
2474 for (pass = try_tail_call ? 0 : 1; pass < 2; pass++)
2476 int sibcall_failure = 0;
2477 /* We want to emit any pending stack adjustments before the tail
2478 recursion "call". That way we know any adjustment after the tail
2479 recursion call can be ignored if we indeed use the tail
2480 call expansion. */
2481 int save_pending_stack_adjust = 0;
2482 int save_stack_pointer_delta = 0;
2483 rtx insns;
2484 rtx before_call, next_arg_reg;
2486 if (pass == 0)
2488 /* Emit any queued insns now; otherwise they would end up in
2489 only one of the alternates. */
2490 emit_queue ();
2492 /* State variables we need to save and restore between
2493 iterations. */
2494 save_pending_stack_adjust = pending_stack_adjust;
2495 save_stack_pointer_delta = stack_pointer_delta;
2497 if (pass)
2498 flags &= ~ECF_SIBCALL;
2499 else
2500 flags |= ECF_SIBCALL;
2502 /* Other state variables that we must reinitialize each time
2503 through the loop (that are not initialized by the loop itself). */
2504 argblock = 0;
2505 call_fusage = 0;
2507 /* Start a new sequence for the normal call case.
2509 From this point on, if the sibling call fails, we want to set
2510 sibcall_failure instead of continuing the loop. */
2511 start_sequence ();
2513 if (pass == 0)
2515 /* We know at this point that there are not currently any
2516 pending cleanups. If, however, in the process of evaluating
2517 the arguments we were to create some, we'll need to be
2518 able to get rid of them. */
2519 expand_start_target_temps ();
2522 /* Don't let pending stack adjusts add up to too much.
2523 Also, do all pending adjustments now if there is any chance
2524 this might be a call to alloca or if we are expanding a sibling
2525 call sequence or if we are calling a function that is to return
2526 with stack pointer depressed. */
2527 if (pending_stack_adjust >= 32
2528 || (pending_stack_adjust > 0
2529 && (flags & (ECF_MAY_BE_ALLOCA | ECF_SP_DEPRESSED)))
2530 || pass == 0)
2531 do_pending_stack_adjust ();
2533 /* When calling a const function, we must pop the stack args right away,
2534 so that the pop is deleted or moved with the call. */
2535 if (pass && (flags & ECF_LIBCALL_BLOCK))
2536 NO_DEFER_POP;
2538 /* Precompute any arguments as needed. */
2539 if (pass)
2540 precompute_arguments (flags, num_actuals, args);
2542 /* Now we are about to start emitting insns that can be deleted
2543 if a libcall is deleted. */
2544 if (pass && (flags & (ECF_LIBCALL_BLOCK | ECF_MALLOC)))
2545 start_sequence ();
2547 adjusted_args_size = args_size;
2548 /* Compute the actual size of the argument block required. The variable
2549 and constant sizes must be combined, the size may have to be rounded,
2550 and there may be a minimum required size. When generating a sibcall
2551 pattern, do not round up, since we'll be re-using whatever space our
2552 caller provided. */
2553 unadjusted_args_size
2554 = compute_argument_block_size (reg_parm_stack_space,
2555 &adjusted_args_size,
2556 (pass == 0 ? 0
2557 : preferred_stack_boundary));
2559 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
2561 /* The argument block when performing a sibling call is the
2562 incoming argument block. */
2563 if (pass == 0)
2565 argblock = virtual_incoming_args_rtx;
2566 argblock
2567 #ifdef STACK_GROWS_DOWNWARD
2568 = plus_constant (argblock, current_function_pretend_args_size);
2569 #else
2570 = plus_constant (argblock, -current_function_pretend_args_size);
2571 #endif
2572 stored_args_map = sbitmap_alloc (args_size.constant);
2573 sbitmap_zero (stored_args_map);
2576 /* If we have no actual push instructions, or shouldn't use them,
2577 make space for all args right now. */
2578 else if (adjusted_args_size.var != 0)
2580 if (old_stack_level == 0)
2582 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
2583 old_stack_pointer_delta = stack_pointer_delta;
2584 old_pending_adj = pending_stack_adjust;
2585 pending_stack_adjust = 0;
2586 /* stack_arg_under_construction says whether a stack arg is
2587 being constructed at the old stack level. Pushing the stack
2588 gets a clean outgoing argument block. */
2589 old_stack_arg_under_construction = stack_arg_under_construction;
2590 stack_arg_under_construction = 0;
2592 argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
2594 else
2596 /* Note that we must go through the motions of allocating an argument
2597 block even if the size is zero because we may be storing args
2598 in the area reserved for register arguments, which may be part of
2599 the stack frame. */
2601 int needed = adjusted_args_size.constant;
2603 /* Store the maximum argument space used. It will be pushed by
2604 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
2605 checking). */
2607 if (needed > current_function_outgoing_args_size)
2608 current_function_outgoing_args_size = needed;
2610 if (must_preallocate)
2612 if (ACCUMULATE_OUTGOING_ARGS)
2614 /* Since the stack pointer will never be pushed, it is
2615 possible for the evaluation of a parm to clobber
2616 something we have already written to the stack.
2617 Since most function calls on RISC machines do not use
2618 the stack, this is uncommon, but must work correctly.
2620 Therefore, we save any area of the stack that was already
2621 written and that we are using. Here we set up to do this
2622 by making a new stack usage map from the old one. The
2623 actual save will be done by store_one_arg.
2625 Another approach might be to try to reorder the argument
2626 evaluations to avoid this conflicting stack usage. */
2628 #ifndef OUTGOING_REG_PARM_STACK_SPACE
2629 /* Since we will be writing into the entire argument area,
2630 the map must be allocated for its entire size, not just
2631 the part that is the responsibility of the caller. */
2632 needed += reg_parm_stack_space;
2633 #endif
2635 #ifdef ARGS_GROW_DOWNWARD
2636 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2637 needed + 1);
2638 #else
2639 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2640 needed);
2641 #endif
2642 stack_usage_map = alloca (highest_outgoing_arg_in_use);
2644 if (initial_highest_arg_in_use)
2645 memcpy (stack_usage_map, initial_stack_usage_map,
2646 initial_highest_arg_in_use);
2648 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
2649 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
2650 (highest_outgoing_arg_in_use
2651 - initial_highest_arg_in_use));
2652 needed = 0;
2654 /* The address of the outgoing argument list must not be
2655 copied to a register here, because argblock would be left
2656 pointing to the wrong place after the call to
2657 allocate_dynamic_stack_space below. */
2659 argblock = virtual_outgoing_args_rtx;
2661 else
2663 if (inhibit_defer_pop == 0)
2665 /* Try to reuse some or all of the pending_stack_adjust
2666 to get this space. */
2667 needed
2668 = (combine_pending_stack_adjustment_and_call
2669 (unadjusted_args_size,
2670 &adjusted_args_size,
2671 preferred_unit_stack_boundary));
2673 /* combine_pending_stack_adjustment_and_call computes
2674 an adjustment before the arguments are allocated.
2675 Account for them and see whether or not the stack
2676 needs to go up or down. */
2677 needed = unadjusted_args_size - needed;
2679 if (needed < 0)
2681 /* We're releasing stack space. */
2682 /* ??? We can avoid any adjustment at all if we're
2683 already aligned. FIXME. */
2684 pending_stack_adjust = -needed;
2685 do_pending_stack_adjust ();
2686 needed = 0;
2688 else
2689 /* We need to allocate space. We'll do that in
2690 push_block below. */
2691 pending_stack_adjust = 0;
2694 /* Special case this because overhead of `push_block' in
2695 this case is non-trivial. */
2696 if (needed == 0)
2697 argblock = virtual_outgoing_args_rtx;
2698 else
2700 argblock = push_block (GEN_INT (needed), 0, 0);
2701 #ifdef ARGS_GROW_DOWNWARD
2702 argblock = plus_constant (argblock, needed);
2703 #endif
2706 /* We only really need to call `copy_to_reg' in the case
2707 where push insns are going to be used to pass ARGBLOCK
2708 to a function call in ARGS. In that case, the stack
2709 pointer changes value from the allocation point to the
2710 call point, and hence the value of
2711 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
2712 as well always do it. */
2713 argblock = copy_to_reg (argblock);
2718 if (ACCUMULATE_OUTGOING_ARGS)
2720 /* The save/restore code in store_one_arg handles all
2721 cases except one: a constructor call (including a C
2722 function returning a BLKmode struct) to initialize
2723 an argument. */
2724 if (stack_arg_under_construction)
2726 #ifndef OUTGOING_REG_PARM_STACK_SPACE
2727 rtx push_size = GEN_INT (reg_parm_stack_space
2728 + adjusted_args_size.constant);
2729 #else
2730 rtx push_size = GEN_INT (adjusted_args_size.constant);
2731 #endif
2732 if (old_stack_level == 0)
2734 emit_stack_save (SAVE_BLOCK, &old_stack_level,
2735 NULL_RTX);
2736 old_stack_pointer_delta = stack_pointer_delta;
2737 old_pending_adj = pending_stack_adjust;
2738 pending_stack_adjust = 0;
2739 /* stack_arg_under_construction says whether a stack
2740 arg is being constructed at the old stack level.
2741 Pushing the stack gets a clean outgoing argument
2742 block. */
2743 old_stack_arg_under_construction
2744 = stack_arg_under_construction;
2745 stack_arg_under_construction = 0;
2746 /* Make a new map for the new argument list. */
2747 stack_usage_map = alloca (highest_outgoing_arg_in_use);
2748 memset (stack_usage_map, 0, highest_outgoing_arg_in_use);
2749 highest_outgoing_arg_in_use = 0;
2751 allocate_dynamic_stack_space (push_size, NULL_RTX,
2752 BITS_PER_UNIT);
2755 /* If argument evaluation might modify the stack pointer,
2756 copy the address of the argument list to a register. */
2757 for (i = 0; i < num_actuals; i++)
2758 if (args[i].pass_on_stack)
2760 argblock = copy_addr_to_reg (argblock);
2761 break;
2765 compute_argument_addresses (args, argblock, num_actuals);
2767 /* If we push args individually in reverse order, perform stack alignment
2768 before the first push (the last arg). */
2769 if (PUSH_ARGS_REVERSED && argblock == 0
2770 && adjusted_args_size.constant != unadjusted_args_size)
2772 /* When the stack adjustment is pending, we get better code
2773 by combining the adjustments. */
2774 if (pending_stack_adjust
2775 && ! (flags & ECF_LIBCALL_BLOCK)
2776 && ! inhibit_defer_pop)
2778 pending_stack_adjust
2779 = (combine_pending_stack_adjustment_and_call
2780 (unadjusted_args_size,
2781 &adjusted_args_size,
2782 preferred_unit_stack_boundary));
2783 do_pending_stack_adjust ();
2785 else if (argblock == 0)
2786 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
2787 - unadjusted_args_size));
2789 /* Now that the stack is properly aligned, pops can't safely
2790 be deferred during the evaluation of the arguments. */
2791 NO_DEFER_POP;
2793 funexp = rtx_for_function_call (fndecl, addr);
2795 /* Figure out the register where the value, if any, will come back. */
2796 valreg = 0;
2797 if (TYPE_MODE (TREE_TYPE (exp)) != VOIDmode
2798 && ! structure_value_addr)
2800 if (pcc_struct_value)
2801 valreg = hard_function_value (build_pointer_type (TREE_TYPE (exp)),
2802 fndecl, (pass == 0));
2803 else
2804 valreg = hard_function_value (TREE_TYPE (exp), fndecl, (pass == 0));
2807 /* Precompute all register parameters. It isn't safe to compute anything
2808 once we have started filling any specific hard regs. */
2809 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
2811 if (TREE_OPERAND (exp, 2))
2812 static_chain_value = expand_expr (TREE_OPERAND (exp, 2),
2813 NULL_RTX, VOIDmode, 0);
2814 else
2815 static_chain_value = 0;
2817 #ifdef REG_PARM_STACK_SPACE
2818 /* Save the fixed argument area if it's part of the caller's frame and
2819 is clobbered by argument setup for this call. */
2820 if (ACCUMULATE_OUTGOING_ARGS && pass)
2821 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
2822 &low_to_save, &high_to_save);
2823 #endif
2825 /* Now store (and compute if necessary) all non-register parms.
2826 These come before register parms, since they can require block-moves,
2827 which could clobber the registers used for register parms.
2828 Parms which have partial registers are not stored here,
2829 but we do preallocate space here if they want that. */
2831 for (i = 0; i < num_actuals; i++)
2832 if (args[i].reg == 0 || args[i].pass_on_stack)
2834 rtx before_arg = get_last_insn ();
2836 if (store_one_arg (&args[i], argblock, flags,
2837 adjusted_args_size.var != 0,
2838 reg_parm_stack_space)
2839 || (pass == 0
2840 && check_sibcall_argument_overlap (before_arg,
2841 &args[i], 1)))
2842 sibcall_failure = 1;
2844 if (flags & ECF_CONST
2845 && args[i].stack
2846 && args[i].value == args[i].stack)
2847 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
2848 gen_rtx_USE (VOIDmode,
2849 args[i].value),
2850 call_fusage);
2853 /* If we have a parm that is passed in registers but not in memory
2854 and whose alignment does not permit a direct copy into registers,
2855 make a group of pseudos that correspond to each register that we
2856 will later fill. */
2857 if (STRICT_ALIGNMENT)
2858 store_unaligned_arguments_into_pseudos (args, num_actuals);
2860 /* Now store any partially-in-registers parm.
2861 This is the last place a block-move can happen. */
2862 if (reg_parm_seen)
2863 for (i = 0; i < num_actuals; i++)
2864 if (args[i].partial != 0 && ! args[i].pass_on_stack)
2866 rtx before_arg = get_last_insn ();
2868 if (store_one_arg (&args[i], argblock, flags,
2869 adjusted_args_size.var != 0,
2870 reg_parm_stack_space)
2871 || (pass == 0
2872 && check_sibcall_argument_overlap (before_arg,
2873 &args[i], 1)))
2874 sibcall_failure = 1;
2877 /* If we pushed args in forward order, perform stack alignment
2878 after pushing the last arg. */
2879 if (!PUSH_ARGS_REVERSED && argblock == 0)
2880 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
2881 - unadjusted_args_size));
2883 /* If register arguments require space on the stack and stack space
2884 was not preallocated, allocate stack space here for arguments
2885 passed in registers. */
2886 #ifdef OUTGOING_REG_PARM_STACK_SPACE
2887 if (!ACCUMULATE_OUTGOING_ARGS
2888 && must_preallocate == 0 && reg_parm_stack_space > 0)
2889 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
2890 #endif
2892 /* Pass the function the address in which to return a
2893 structure value. */
2894 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
2896 structure_value_addr
2897 = convert_memory_address (Pmode, structure_value_addr);
2898 emit_move_insn (struct_value,
2899 force_reg (Pmode,
2900 force_operand (structure_value_addr,
2901 NULL_RTX)));
2903 if (GET_CODE (struct_value) == REG)
2904 use_reg (&call_fusage, struct_value);
2907 funexp = prepare_call_address (funexp, static_chain_value,
2908 &call_fusage, reg_parm_seen, pass == 0);
2910 load_register_parameters (args, num_actuals, &call_fusage, flags,
2911 pass == 0, &sibcall_failure);
2913 /* Perform postincrements before actually calling the function. */
2914 emit_queue ();
2916 /* Save a pointer to the last insn before the call, so that we can
2917 later safely search backwards to find the CALL_INSN. */
2918 before_call = get_last_insn ();
2920 /* Set up next argument register. For sibling calls on machines
2921 with register windows this should be the incoming register. */
2922 #ifdef FUNCTION_INCOMING_ARG
2923 if (pass == 0)
2924 next_arg_reg = FUNCTION_INCOMING_ARG (args_so_far, VOIDmode,
2925 void_type_node, 1);
2926 else
2927 #endif
2928 next_arg_reg = FUNCTION_ARG (args_so_far, VOIDmode,
2929 void_type_node, 1);
2931 /* All arguments and registers used for the call must be set up by
2932 now! */
2934 /* Stack must be properly aligned now. */
2935 if (pass && stack_pointer_delta % preferred_unit_stack_boundary)
2936 abort ();
2938 /* Generate the actual call instruction. */
2939 emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
2940 adjusted_args_size.constant, struct_value_size,
2941 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
2942 flags, & args_so_far);
2944 /* If call is cse'able, make appropriate pair of reg-notes around it.
2945 Test valreg so we don't crash; may safely ignore `const'
2946 if return type is void. Disable for PARALLEL return values, because
2947 we have no way to move such values into a pseudo register. */
2948 if (pass && (flags & ECF_LIBCALL_BLOCK))
2950 rtx insns;
2951 rtx insn;
2952 bool failed = valreg == 0 || GET_CODE (valreg) == PARALLEL;
2954 insns = get_insns ();
2956 /* Expansion of block moves possibly introduced a loop that may
2957 not appear inside libcall block. */
2958 for (insn = insns; insn; insn = NEXT_INSN (insn))
2959 if (GET_CODE (insn) == JUMP_INSN)
2960 failed = true;
2962 if (failed)
2964 end_sequence ();
2965 emit_insn (insns);
2967 else
2969 rtx note = 0;
2970 rtx temp = gen_reg_rtx (GET_MODE (valreg));
2972 /* Mark the return value as a pointer if needed. */
2973 if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
2974 mark_reg_pointer (temp,
2975 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp))));
2977 end_sequence ();
2978 if (flag_unsafe_math_optimizations
2979 && fndecl
2980 && DECL_BUILT_IN (fndecl)
2981 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRT
2982 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRTF
2983 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRTL))
2984 note = gen_rtx_fmt_e (SQRT,
2985 GET_MODE (temp),
2986 args[0].initial_value);
2987 else
2989 /* Construct an "equal form" for the value which
2990 mentions all the arguments in order as well as
2991 the function name. */
2992 for (i = 0; i < num_actuals; i++)
2993 note = gen_rtx_EXPR_LIST (VOIDmode,
2994 args[i].initial_value, note);
2995 note = gen_rtx_EXPR_LIST (VOIDmode, funexp, note);
2997 if (flags & ECF_PURE)
2998 note = gen_rtx_EXPR_LIST (VOIDmode,
2999 gen_rtx_USE (VOIDmode,
3000 gen_rtx_MEM (BLKmode,
3001 gen_rtx_SCRATCH (VOIDmode))),
3002 note);
3004 emit_libcall_block (insns, temp, valreg, note);
3006 valreg = temp;
3009 else if (pass && (flags & ECF_MALLOC))
3011 rtx temp = gen_reg_rtx (GET_MODE (valreg));
3012 rtx last, insns;
3014 /* The return value from a malloc-like function is a pointer. */
3015 if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
3016 mark_reg_pointer (temp, BIGGEST_ALIGNMENT);
3018 emit_move_insn (temp, valreg);
3020 /* The return value from a malloc-like function can not alias
3021 anything else. */
3022 last = get_last_insn ();
3023 REG_NOTES (last) =
3024 gen_rtx_EXPR_LIST (REG_NOALIAS, temp, REG_NOTES (last));
3026 /* Write out the sequence. */
3027 insns = get_insns ();
3028 end_sequence ();
3029 emit_insn (insns);
3030 valreg = temp;
3033 /* For calls to `setjmp', etc., inform flow.c it should complain
3034 if nonvolatile values are live. For functions that cannot return,
3035 inform flow that control does not fall through. */
3037 if ((flags & (ECF_NORETURN | ECF_LONGJMP)) || pass == 0)
3039 /* The barrier must be emitted
3040 immediately after the CALL_INSN. Some ports emit more
3041 than just a CALL_INSN above, so we must search for it here. */
3043 rtx last = get_last_insn ();
3044 while (GET_CODE (last) != CALL_INSN)
3046 last = PREV_INSN (last);
3047 /* There was no CALL_INSN? */
3048 if (last == before_call)
3049 abort ();
3052 emit_barrier_after (last);
3054 /* Stack adjustments after a noreturn call are dead code.
3055 However when NO_DEFER_POP is in effect, we must preserve
3056 stack_pointer_delta. */
3057 if (inhibit_defer_pop == 0)
3059 stack_pointer_delta = old_stack_allocated;
3060 pending_stack_adjust = 0;
3064 if (flags & ECF_LONGJMP)
3065 current_function_calls_longjmp = 1;
3067 /* If value type not void, return an rtx for the value. */
3069 /* If there are cleanups to be called, don't use a hard reg as target.
3070 We need to double check this and see if it matters anymore. */
3071 if (any_pending_cleanups ())
3073 if (target && REG_P (target)
3074 && REGNO (target) < FIRST_PSEUDO_REGISTER)
3075 target = 0;
3076 sibcall_failure = 1;
3079 if (TYPE_MODE (TREE_TYPE (exp)) == VOIDmode
3080 || ignore)
3081 target = const0_rtx;
3082 else if (structure_value_addr)
3084 if (target == 0 || GET_CODE (target) != MEM)
3086 target
3087 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
3088 memory_address (TYPE_MODE (TREE_TYPE (exp)),
3089 structure_value_addr));
3090 set_mem_attributes (target, exp, 1);
3093 else if (pcc_struct_value)
3095 /* This is the special C++ case where we need to
3096 know what the true target was. We take care to
3097 never use this value more than once in one expression. */
3098 target = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
3099 copy_to_reg (valreg));
3100 set_mem_attributes (target, exp, 1);
3102 /* Handle calls that return values in multiple non-contiguous locations.
3103 The Irix 6 ABI has examples of this. */
3104 else if (GET_CODE (valreg) == PARALLEL)
3106 if (target == 0)
3108 /* This will only be assigned once, so it can be readonly. */
3109 tree nt = build_qualified_type (TREE_TYPE (exp),
3110 (TYPE_QUALS (TREE_TYPE (exp))
3111 | TYPE_QUAL_CONST));
3113 target = assign_temp (nt, 0, 1, 1);
3114 preserve_temp_slots (target);
3117 if (! rtx_equal_p (target, valreg))
3118 emit_group_store (target, valreg, TREE_TYPE (exp),
3119 int_size_in_bytes (TREE_TYPE (exp)));
3121 /* We can not support sibling calls for this case. */
3122 sibcall_failure = 1;
3124 else if (target
3125 && GET_MODE (target) == TYPE_MODE (TREE_TYPE (exp))
3126 && GET_MODE (target) == GET_MODE (valreg))
3128 /* TARGET and VALREG cannot be equal at this point because the
3129 latter would not have REG_FUNCTION_VALUE_P true, while the
3130 former would if it were referring to the same register.
3132 If they refer to the same register, this move will be a no-op,
3133 except when function inlining is being done. */
3134 emit_move_insn (target, valreg);
3136 /* If we are setting a MEM, this code must be executed. Since it is
3137 emitted after the call insn, sibcall optimization cannot be
3138 performed in that case. */
3139 if (GET_CODE (target) == MEM)
3140 sibcall_failure = 1;
3142 else if (TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
3144 target = copy_blkmode_from_reg (target, valreg, TREE_TYPE (exp));
3146 /* We can not support sibling calls for this case. */
3147 sibcall_failure = 1;
3149 else
3151 if (shift_returned_value (TREE_TYPE (exp), &valreg))
3152 sibcall_failure = 1;
3154 target = copy_to_reg (valreg);
3157 if (targetm.calls.promote_function_return(funtype))
3159 /* If we promoted this return value, make the proper SUBREG. TARGET
3160 might be const0_rtx here, so be careful. */
3161 if (GET_CODE (target) == REG
3162 && TYPE_MODE (TREE_TYPE (exp)) != BLKmode
3163 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
3165 tree type = TREE_TYPE (exp);
3166 int unsignedp = TYPE_UNSIGNED (type);
3167 int offset = 0;
3169 /* If we don't promote as expected, something is wrong. */
3170 if (GET_MODE (target)
3171 != promote_mode (type, TYPE_MODE (type), &unsignedp, 1))
3172 abort ();
3174 if ((WORDS_BIG_ENDIAN || BYTES_BIG_ENDIAN)
3175 && GET_MODE_SIZE (GET_MODE (target))
3176 > GET_MODE_SIZE (TYPE_MODE (type)))
3178 offset = GET_MODE_SIZE (GET_MODE (target))
3179 - GET_MODE_SIZE (TYPE_MODE (type));
3180 if (! BYTES_BIG_ENDIAN)
3181 offset = (offset / UNITS_PER_WORD) * UNITS_PER_WORD;
3182 else if (! WORDS_BIG_ENDIAN)
3183 offset %= UNITS_PER_WORD;
3185 target = gen_rtx_SUBREG (TYPE_MODE (type), target, offset);
3186 SUBREG_PROMOTED_VAR_P (target) = 1;
3187 SUBREG_PROMOTED_UNSIGNED_SET (target, unsignedp);
3191 /* If size of args is variable or this was a constructor call for a stack
3192 argument, restore saved stack-pointer value. */
3194 if (old_stack_level && ! (flags & ECF_SP_DEPRESSED))
3196 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
3197 stack_pointer_delta = old_stack_pointer_delta;
3198 pending_stack_adjust = old_pending_adj;
3199 stack_arg_under_construction = old_stack_arg_under_construction;
3200 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3201 stack_usage_map = initial_stack_usage_map;
3202 sibcall_failure = 1;
3204 else if (ACCUMULATE_OUTGOING_ARGS && pass)
3206 #ifdef REG_PARM_STACK_SPACE
3207 if (save_area)
3208 restore_fixed_argument_area (save_area, argblock,
3209 high_to_save, low_to_save);
3210 #endif
3212 /* If we saved any argument areas, restore them. */
3213 for (i = 0; i < num_actuals; i++)
3214 if (args[i].save_area)
3216 enum machine_mode save_mode = GET_MODE (args[i].save_area);
3217 rtx stack_area
3218 = gen_rtx_MEM (save_mode,
3219 memory_address (save_mode,
3220 XEXP (args[i].stack_slot, 0)));
3222 if (save_mode != BLKmode)
3223 emit_move_insn (stack_area, args[i].save_area);
3224 else
3225 emit_block_move (stack_area, args[i].save_area,
3226 GEN_INT (args[i].locate.size.constant),
3227 BLOCK_OP_CALL_PARM);
3230 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3231 stack_usage_map = initial_stack_usage_map;
3234 /* If this was alloca, record the new stack level for nonlocal gotos.
3235 Check for the handler slots since we might not have a save area
3236 for non-local gotos. */
3238 if ((flags & ECF_MAY_BE_ALLOCA) && cfun->nonlocal_goto_save_area != 0)
3239 update_nonlocal_goto_save_area ();
3241 /* Free up storage we no longer need. */
3242 for (i = 0; i < num_actuals; ++i)
3243 if (args[i].aligned_regs)
3244 free (args[i].aligned_regs);
3246 if (pass == 0)
3248 /* Undo the fake expand_start_target_temps we did earlier. If
3249 there had been any cleanups created, we've already set
3250 sibcall_failure. */
3251 expand_end_target_temps ();
3254 /* If this function is returning into a memory location marked as
3255 readonly, it means it is initializing that location. We normally treat
3256 functions as not clobbering such locations, so we need to specify that
3257 this one does. We do this by adding the appropriate CLOBBER to the
3258 CALL_INSN function usage list. This cannot be done by emitting a
3259 standalone CLOBBER after the call because the latter would be ignored
3260 by at least the delay slot scheduling pass. We do this now instead of
3261 adding to call_fusage before the call to emit_call_1 because TARGET
3262 may be modified in the meantime. */
3263 if (structure_value_addr != 0 && target != 0
3264 && GET_CODE (target) == MEM && RTX_UNCHANGING_P (target))
3265 add_function_usage_to
3266 (last_call_insn (),
3267 gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_CLOBBER (VOIDmode, target),
3268 NULL_RTX));
3270 insns = get_insns ();
3271 end_sequence ();
3273 if (pass == 0)
3275 tail_call_insns = insns;
3277 /* Restore the pending stack adjustment now that we have
3278 finished generating the sibling call sequence. */
3280 pending_stack_adjust = save_pending_stack_adjust;
3281 stack_pointer_delta = save_stack_pointer_delta;
3283 /* Prepare arg structure for next iteration. */
3284 for (i = 0; i < num_actuals; i++)
3286 args[i].value = 0;
3287 args[i].aligned_regs = 0;
3288 args[i].stack = 0;
3291 sbitmap_free (stored_args_map);
3293 else
3295 normal_call_insns = insns;
3297 /* Verify that we've deallocated all the stack we used. */
3298 if (! (flags & (ECF_NORETURN | ECF_LONGJMP))
3299 && old_stack_allocated != stack_pointer_delta
3300 - pending_stack_adjust)
3301 abort ();
3304 /* If something prevents making this a sibling call,
3305 zero out the sequence. */
3306 if (sibcall_failure)
3307 tail_call_insns = NULL_RTX;
3308 else
3309 break;
3312 /* If tail call production suceeded, we need to remove REG_EQUIV notes on
3313 arguments too, as argument area is now clobbered by the call. */
3314 if (tail_call_insns)
3316 emit_insn (tail_call_insns);
3317 cfun->tail_call_emit = true;
3319 else
3320 emit_insn (normal_call_insns);
3322 currently_expanding_call--;
3324 /* If this function returns with the stack pointer depressed, ensure
3325 this block saves and restores the stack pointer, show it was
3326 changed, and adjust for any outgoing arg space. */
3327 if (flags & ECF_SP_DEPRESSED)
3329 clear_pending_stack_adjust ();
3330 emit_insn (gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx));
3331 emit_move_insn (virtual_stack_dynamic_rtx, stack_pointer_rtx);
3332 save_stack_pointer ();
3335 return target;
3338 /* A sibling call sequence invalidates any REG_EQUIV notes made for
3339 this function's incoming arguments.
3341 At the start of RTL generation we know the only REG_EQUIV notes
3342 in the rtl chain are those for incoming arguments, so we can safely
3343 flush any REG_EQUIV note.
3345 This is (slight) overkill. We could keep track of the highest
3346 argument we clobber and be more selective in removing notes, but it
3347 does not seem to be worth the effort. */
3348 void
3349 fixup_tail_calls (void)
3351 rtx insn;
3352 tree arg;
3354 purge_reg_equiv_notes ();
3356 /* A sibling call sequence also may invalidate RTX_UNCHANGING_P
3357 flag of some incoming arguments MEM RTLs, because it can write into
3358 those slots. We clear all those bits now.
3360 This is (slight) overkill, we could keep track of which arguments
3361 we actually write into. */
3362 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3364 if (INSN_P (insn))
3365 purge_mem_unchanging_flag (PATTERN (insn));
3368 /* Similarly, invalidate RTX_UNCHANGING_P for any incoming
3369 arguments passed in registers. */
3370 for (arg = DECL_ARGUMENTS (current_function_decl);
3371 arg;
3372 arg = TREE_CHAIN (arg))
3374 if (REG_P (DECL_RTL (arg)))
3375 RTX_UNCHANGING_P (DECL_RTL (arg)) = false;
3379 /* Traverse an argument list in VALUES and expand all complex
3380 arguments into their components. */
3381 tree
3382 split_complex_values (tree values)
3384 tree p;
3386 /* Before allocating memory, check for the common case of no complex. */
3387 for (p = values; p; p = TREE_CHAIN (p))
3389 tree type = TREE_TYPE (TREE_VALUE (p));
3390 if (type && TREE_CODE (type) == COMPLEX_TYPE
3391 && targetm.calls.split_complex_arg (type))
3392 goto found;
3394 return values;
3396 found:
3397 values = copy_list (values);
3399 for (p = values; p; p = TREE_CHAIN (p))
3401 tree complex_value = TREE_VALUE (p);
3402 tree complex_type;
3404 complex_type = TREE_TYPE (complex_value);
3405 if (!complex_type)
3406 continue;
3408 if (TREE_CODE (complex_type) == COMPLEX_TYPE
3409 && targetm.calls.split_complex_arg (complex_type))
3411 tree subtype;
3412 tree real, imag, next;
3414 subtype = TREE_TYPE (complex_type);
3415 complex_value = save_expr (complex_value);
3416 real = build1 (REALPART_EXPR, subtype, complex_value);
3417 imag = build1 (IMAGPART_EXPR, subtype, complex_value);
3419 TREE_VALUE (p) = real;
3420 next = TREE_CHAIN (p);
3421 imag = build_tree_list (NULL_TREE, imag);
3422 TREE_CHAIN (p) = imag;
3423 TREE_CHAIN (imag) = next;
3425 /* Skip the newly created node. */
3426 p = TREE_CHAIN (p);
3430 return values;
3433 /* Traverse a list of TYPES and expand all complex types into their
3434 components. */
3435 tree
3436 split_complex_types (tree types)
3438 tree p;
3440 /* Before allocating memory, check for the common case of no complex. */
3441 for (p = types; p; p = TREE_CHAIN (p))
3443 tree type = TREE_VALUE (p);
3444 if (TREE_CODE (type) == COMPLEX_TYPE
3445 && targetm.calls.split_complex_arg (type))
3446 goto found;
3448 return types;
3450 found:
3451 types = copy_list (types);
3453 for (p = types; p; p = TREE_CHAIN (p))
3455 tree complex_type = TREE_VALUE (p);
3457 if (TREE_CODE (complex_type) == COMPLEX_TYPE
3458 && targetm.calls.split_complex_arg (complex_type))
3460 tree next, imag;
3462 /* Rewrite complex type with component type. */
3463 TREE_VALUE (p) = TREE_TYPE (complex_type);
3464 next = TREE_CHAIN (p);
3466 /* Add another component type for the imaginary part. */
3467 imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
3468 TREE_CHAIN (p) = imag;
3469 TREE_CHAIN (imag) = next;
3471 /* Skip the newly created node. */
3472 p = TREE_CHAIN (p);
3476 return types;
3479 /* Output a library call to function FUN (a SYMBOL_REF rtx).
3480 The RETVAL parameter specifies whether return value needs to be saved, other
3481 parameters are documented in the emit_library_call function below. */
3483 static rtx
3484 emit_library_call_value_1 (int retval, rtx orgfun, rtx value,
3485 enum libcall_type fn_type,
3486 enum machine_mode outmode, int nargs, va_list p)
3488 /* Total size in bytes of all the stack-parms scanned so far. */
3489 struct args_size args_size;
3490 /* Size of arguments before any adjustments (such as rounding). */
3491 struct args_size original_args_size;
3492 int argnum;
3493 rtx fun;
3494 int inc;
3495 int count;
3496 rtx argblock = 0;
3497 CUMULATIVE_ARGS args_so_far;
3498 struct arg
3500 rtx value;
3501 enum machine_mode mode;
3502 rtx reg;
3503 int partial;
3504 struct locate_and_pad_arg_data locate;
3505 rtx save_area;
3507 struct arg *argvec;
3508 int old_inhibit_defer_pop = inhibit_defer_pop;
3509 rtx call_fusage = 0;
3510 rtx mem_value = 0;
3511 rtx valreg;
3512 int pcc_struct_value = 0;
3513 int struct_value_size = 0;
3514 int flags;
3515 int reg_parm_stack_space = 0;
3516 int needed;
3517 rtx before_call;
3518 tree tfom; /* type_for_mode (outmode, 0) */
3520 #ifdef REG_PARM_STACK_SPACE
3521 /* Define the boundary of the register parm stack space that needs to be
3522 save, if any. */
3523 int low_to_save, high_to_save;
3524 rtx save_area = 0; /* Place that it is saved. */
3525 #endif
3527 /* Size of the stack reserved for parameter registers. */
3528 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
3529 char *initial_stack_usage_map = stack_usage_map;
3531 rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
3533 #ifdef REG_PARM_STACK_SPACE
3534 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
3535 #endif
3537 /* By default, library functions can not throw. */
3538 flags = ECF_NOTHROW;
3540 switch (fn_type)
3542 case LCT_NORMAL:
3543 break;
3544 case LCT_CONST:
3545 flags |= ECF_CONST;
3546 break;
3547 case LCT_PURE:
3548 flags |= ECF_PURE;
3549 break;
3550 case LCT_CONST_MAKE_BLOCK:
3551 flags |= ECF_CONST | ECF_LIBCALL_BLOCK;
3552 break;
3553 case LCT_PURE_MAKE_BLOCK:
3554 flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
3555 break;
3556 case LCT_NORETURN:
3557 flags |= ECF_NORETURN;
3558 break;
3559 case LCT_THROW:
3560 flags = ECF_NORETURN;
3561 break;
3562 case LCT_ALWAYS_RETURN:
3563 flags = ECF_ALWAYS_RETURN;
3564 break;
3565 case LCT_RETURNS_TWICE:
3566 flags = ECF_RETURNS_TWICE;
3567 break;
3569 fun = orgfun;
3571 /* Ensure current function's preferred stack boundary is at least
3572 what we need. */
3573 if (cfun->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
3574 cfun->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3576 /* If this kind of value comes back in memory,
3577 decide where in memory it should come back. */
3578 if (outmode != VOIDmode)
3580 tfom = lang_hooks.types.type_for_mode (outmode, 0);
3581 if (aggregate_value_p (tfom, 0))
3583 #ifdef PCC_STATIC_STRUCT_RETURN
3584 rtx pointer_reg
3585 = hard_function_value (build_pointer_type (tfom), 0, 0);
3586 mem_value = gen_rtx_MEM (outmode, pointer_reg);
3587 pcc_struct_value = 1;
3588 if (value == 0)
3589 value = gen_reg_rtx (outmode);
3590 #else /* not PCC_STATIC_STRUCT_RETURN */
3591 struct_value_size = GET_MODE_SIZE (outmode);
3592 if (value != 0 && GET_CODE (value) == MEM)
3593 mem_value = value;
3594 else
3595 mem_value = assign_temp (tfom, 0, 1, 1);
3596 #endif
3597 /* This call returns a big structure. */
3598 flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
3601 else
3602 tfom = void_type_node;
3604 /* ??? Unfinished: must pass the memory address as an argument. */
3606 /* Copy all the libcall-arguments out of the varargs data
3607 and into a vector ARGVEC.
3609 Compute how to pass each argument. We only support a very small subset
3610 of the full argument passing conventions to limit complexity here since
3611 library functions shouldn't have many args. */
3613 argvec = alloca ((nargs + 1) * sizeof (struct arg));
3614 memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
3616 #ifdef INIT_CUMULATIVE_LIBCALL_ARGS
3617 INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far, outmode, fun);
3618 #else
3619 INIT_CUMULATIVE_ARGS (args_so_far, NULL_TREE, fun, 0, nargs);
3620 #endif
3622 args_size.constant = 0;
3623 args_size.var = 0;
3625 count = 0;
3627 /* Now we are about to start emitting insns that can be deleted
3628 if a libcall is deleted. */
3629 if (flags & ECF_LIBCALL_BLOCK)
3630 start_sequence ();
3632 push_temp_slots ();
3634 /* If there's a structure value address to be passed,
3635 either pass it in the special place, or pass it as an extra argument. */
3636 if (mem_value && struct_value == 0 && ! pcc_struct_value)
3638 rtx addr = XEXP (mem_value, 0);
3639 nargs++;
3641 /* Make sure it is a reasonable operand for a move or push insn. */
3642 if (GET_CODE (addr) != REG && GET_CODE (addr) != MEM
3643 && ! (CONSTANT_P (addr) && LEGITIMATE_CONSTANT_P (addr)))
3644 addr = force_operand (addr, NULL_RTX);
3646 argvec[count].value = addr;
3647 argvec[count].mode = Pmode;
3648 argvec[count].partial = 0;
3650 argvec[count].reg = FUNCTION_ARG (args_so_far, Pmode, NULL_TREE, 1);
3651 #ifdef FUNCTION_ARG_PARTIAL_NREGS
3652 if (FUNCTION_ARG_PARTIAL_NREGS (args_so_far, Pmode, NULL_TREE, 1))
3653 abort ();
3654 #endif
3656 locate_and_pad_parm (Pmode, NULL_TREE,
3657 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3659 #else
3660 argvec[count].reg != 0,
3661 #endif
3662 0, NULL_TREE, &args_size, &argvec[count].locate);
3664 if (argvec[count].reg == 0 || argvec[count].partial != 0
3665 || reg_parm_stack_space > 0)
3666 args_size.constant += argvec[count].locate.size.constant;
3668 FUNCTION_ARG_ADVANCE (args_so_far, Pmode, (tree) 0, 1);
3670 count++;
3673 for (; count < nargs; count++)
3675 rtx val = va_arg (p, rtx);
3676 enum machine_mode mode = va_arg (p, enum machine_mode);
3678 /* We cannot convert the arg value to the mode the library wants here;
3679 must do it earlier where we know the signedness of the arg. */
3680 if (mode == BLKmode
3681 || (GET_MODE (val) != mode && GET_MODE (val) != VOIDmode))
3682 abort ();
3684 /* There's no need to call protect_from_queue, because
3685 either emit_move_insn or emit_push_insn will do that. */
3687 /* Make sure it is a reasonable operand for a move or push insn. */
3688 if (GET_CODE (val) != REG && GET_CODE (val) != MEM
3689 && ! (CONSTANT_P (val) && LEGITIMATE_CONSTANT_P (val)))
3690 val = force_operand (val, NULL_RTX);
3692 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
3693 if (FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, mode, NULL_TREE, 1))
3695 rtx slot;
3696 int must_copy = 1
3697 #ifdef FUNCTION_ARG_CALLEE_COPIES
3698 && ! FUNCTION_ARG_CALLEE_COPIES (args_so_far, mode,
3699 NULL_TREE, 1)
3700 #endif
3703 /* loop.c won't look at CALL_INSN_FUNCTION_USAGE of const/pure
3704 functions, so we have to pretend this isn't such a function. */
3705 if (flags & ECF_LIBCALL_BLOCK)
3707 rtx insns = get_insns ();
3708 end_sequence ();
3709 emit_insn (insns);
3711 flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
3713 /* If this was a CONST function, it is now PURE since
3714 it now reads memory. */
3715 if (flags & ECF_CONST)
3717 flags &= ~ECF_CONST;
3718 flags |= ECF_PURE;
3721 if (GET_MODE (val) == MEM && ! must_copy)
3722 slot = val;
3723 else if (must_copy)
3725 slot = assign_temp (lang_hooks.types.type_for_mode (mode, 0),
3726 0, 1, 1);
3727 emit_move_insn (slot, val);
3729 else
3731 tree type = lang_hooks.types.type_for_mode (mode, 0);
3733 slot
3734 = gen_rtx_MEM (mode,
3735 expand_expr (build1 (ADDR_EXPR,
3736 build_pointer_type (type),
3737 make_tree (type, val)),
3738 NULL_RTX, VOIDmode, 0));
3741 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
3742 gen_rtx_USE (VOIDmode, slot),
3743 call_fusage);
3744 if (must_copy)
3745 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
3746 gen_rtx_CLOBBER (VOIDmode,
3747 slot),
3748 call_fusage);
3750 mode = Pmode;
3751 val = force_operand (XEXP (slot, 0), NULL_RTX);
3753 #endif
3755 argvec[count].value = val;
3756 argvec[count].mode = mode;
3758 argvec[count].reg = FUNCTION_ARG (args_so_far, mode, NULL_TREE, 1);
3760 #ifdef FUNCTION_ARG_PARTIAL_NREGS
3761 argvec[count].partial
3762 = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, mode, NULL_TREE, 1);
3763 #else
3764 argvec[count].partial = 0;
3765 #endif
3767 locate_and_pad_parm (mode, NULL_TREE,
3768 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3770 #else
3771 argvec[count].reg != 0,
3772 #endif
3773 argvec[count].partial,
3774 NULL_TREE, &args_size, &argvec[count].locate);
3776 if (argvec[count].locate.size.var)
3777 abort ();
3779 if (argvec[count].reg == 0 || argvec[count].partial != 0
3780 || reg_parm_stack_space > 0)
3781 args_size.constant += argvec[count].locate.size.constant;
3783 FUNCTION_ARG_ADVANCE (args_so_far, mode, (tree) 0, 1);
3786 /* If this machine requires an external definition for library
3787 functions, write one out. */
3788 assemble_external_libcall (fun);
3790 original_args_size = args_size;
3791 args_size.constant = (((args_size.constant
3792 + stack_pointer_delta
3793 + STACK_BYTES - 1)
3794 / STACK_BYTES
3795 * STACK_BYTES)
3796 - stack_pointer_delta);
3798 args_size.constant = MAX (args_size.constant,
3799 reg_parm_stack_space);
3801 #ifndef OUTGOING_REG_PARM_STACK_SPACE
3802 args_size.constant -= reg_parm_stack_space;
3803 #endif
3805 if (args_size.constant > current_function_outgoing_args_size)
3806 current_function_outgoing_args_size = args_size.constant;
3808 if (ACCUMULATE_OUTGOING_ARGS)
3810 /* Since the stack pointer will never be pushed, it is possible for
3811 the evaluation of a parm to clobber something we have already
3812 written to the stack. Since most function calls on RISC machines
3813 do not use the stack, this is uncommon, but must work correctly.
3815 Therefore, we save any area of the stack that was already written
3816 and that we are using. Here we set up to do this by making a new
3817 stack usage map from the old one.
3819 Another approach might be to try to reorder the argument
3820 evaluations to avoid this conflicting stack usage. */
3822 needed = args_size.constant;
3824 #ifndef OUTGOING_REG_PARM_STACK_SPACE
3825 /* Since we will be writing into the entire argument area, the
3826 map must be allocated for its entire size, not just the part that
3827 is the responsibility of the caller. */
3828 needed += reg_parm_stack_space;
3829 #endif
3831 #ifdef ARGS_GROW_DOWNWARD
3832 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3833 needed + 1);
3834 #else
3835 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3836 needed);
3837 #endif
3838 stack_usage_map = alloca (highest_outgoing_arg_in_use);
3840 if (initial_highest_arg_in_use)
3841 memcpy (stack_usage_map, initial_stack_usage_map,
3842 initial_highest_arg_in_use);
3844 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
3845 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
3846 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
3847 needed = 0;
3849 /* We must be careful to use virtual regs before they're instantiated,
3850 and real regs afterwards. Loop optimization, for example, can create
3851 new libcalls after we've instantiated the virtual regs, and if we
3852 use virtuals anyway, they won't match the rtl patterns. */
3854 if (virtuals_instantiated)
3855 argblock = plus_constant (stack_pointer_rtx, STACK_POINTER_OFFSET);
3856 else
3857 argblock = virtual_outgoing_args_rtx;
3859 else
3861 if (!PUSH_ARGS)
3862 argblock = push_block (GEN_INT (args_size.constant), 0, 0);
3865 /* If we push args individually in reverse order, perform stack alignment
3866 before the first push (the last arg). */
3867 if (argblock == 0 && PUSH_ARGS_REVERSED)
3868 anti_adjust_stack (GEN_INT (args_size.constant
3869 - original_args_size.constant));
3871 if (PUSH_ARGS_REVERSED)
3873 inc = -1;
3874 argnum = nargs - 1;
3876 else
3878 inc = 1;
3879 argnum = 0;
3882 #ifdef REG_PARM_STACK_SPACE
3883 if (ACCUMULATE_OUTGOING_ARGS)
3885 /* The argument list is the property of the called routine and it
3886 may clobber it. If the fixed area has been used for previous
3887 parameters, we must save and restore it. */
3888 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3889 &low_to_save, &high_to_save);
3891 #endif
3893 /* Push the args that need to be pushed. */
3895 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
3896 are to be pushed. */
3897 for (count = 0; count < nargs; count++, argnum += inc)
3899 enum machine_mode mode = argvec[argnum].mode;
3900 rtx val = argvec[argnum].value;
3901 rtx reg = argvec[argnum].reg;
3902 int partial = argvec[argnum].partial;
3903 int lower_bound = 0, upper_bound = 0, i;
3905 if (! (reg != 0 && partial == 0))
3907 if (ACCUMULATE_OUTGOING_ARGS)
3909 /* If this is being stored into a pre-allocated, fixed-size,
3910 stack area, save any previous data at that location. */
3912 #ifdef ARGS_GROW_DOWNWARD
3913 /* stack_slot is negative, but we want to index stack_usage_map
3914 with positive values. */
3915 upper_bound = -argvec[argnum].locate.offset.constant + 1;
3916 lower_bound = upper_bound - argvec[argnum].locate.size.constant;
3917 #else
3918 lower_bound = argvec[argnum].locate.offset.constant;
3919 upper_bound = lower_bound + argvec[argnum].locate.size.constant;
3920 #endif
3922 i = lower_bound;
3923 /* Don't worry about things in the fixed argument area;
3924 it has already been saved. */
3925 if (i < reg_parm_stack_space)
3926 i = reg_parm_stack_space;
3927 while (i < upper_bound && stack_usage_map[i] == 0)
3928 i++;
3930 if (i < upper_bound)
3932 /* We need to make a save area. */
3933 unsigned int size
3934 = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
3935 enum machine_mode save_mode
3936 = mode_for_size (size, MODE_INT, 1);
3937 rtx adr
3938 = plus_constant (argblock,
3939 argvec[argnum].locate.offset.constant);
3940 rtx stack_area
3941 = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
3943 if (save_mode == BLKmode)
3945 argvec[argnum].save_area
3946 = assign_stack_temp (BLKmode,
3947 argvec[argnum].locate.size.constant,
3950 emit_block_move (validize_mem (argvec[argnum].save_area),
3951 stack_area,
3952 GEN_INT (argvec[argnum].locate.size.constant),
3953 BLOCK_OP_CALL_PARM);
3955 else
3957 argvec[argnum].save_area = gen_reg_rtx (save_mode);
3959 emit_move_insn (argvec[argnum].save_area, stack_area);
3964 emit_push_insn (val, mode, NULL_TREE, NULL_RTX, PARM_BOUNDARY,
3965 partial, reg, 0, argblock,
3966 GEN_INT (argvec[argnum].locate.offset.constant),
3967 reg_parm_stack_space,
3968 ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad));
3970 /* Now mark the segment we just used. */
3971 if (ACCUMULATE_OUTGOING_ARGS)
3972 for (i = lower_bound; i < upper_bound; i++)
3973 stack_usage_map[i] = 1;
3975 NO_DEFER_POP;
3979 /* If we pushed args in forward order, perform stack alignment
3980 after pushing the last arg. */
3981 if (argblock == 0 && !PUSH_ARGS_REVERSED)
3982 anti_adjust_stack (GEN_INT (args_size.constant
3983 - original_args_size.constant));
3985 if (PUSH_ARGS_REVERSED)
3986 argnum = nargs - 1;
3987 else
3988 argnum = 0;
3990 fun = prepare_call_address (fun, NULL, &call_fusage, 0, 0);
3992 /* Now load any reg parms into their regs. */
3994 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
3995 are to be pushed. */
3996 for (count = 0; count < nargs; count++, argnum += inc)
3998 rtx val = argvec[argnum].value;
3999 rtx reg = argvec[argnum].reg;
4000 int partial = argvec[argnum].partial;
4002 /* Handle calls that pass values in multiple non-contiguous
4003 locations. The PA64 has examples of this for library calls. */
4004 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4005 emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (GET_MODE (val)));
4006 else if (reg != 0 && partial == 0)
4007 emit_move_insn (reg, val);
4009 NO_DEFER_POP;
4012 /* Any regs containing parms remain in use through the call. */
4013 for (count = 0; count < nargs; count++)
4015 rtx reg = argvec[count].reg;
4016 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4017 use_group_regs (&call_fusage, reg);
4018 else if (reg != 0)
4019 use_reg (&call_fusage, reg);
4022 /* Pass the function the address in which to return a structure value. */
4023 if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
4025 emit_move_insn (struct_value,
4026 force_reg (Pmode,
4027 force_operand (XEXP (mem_value, 0),
4028 NULL_RTX)));
4029 if (GET_CODE (struct_value) == REG)
4030 use_reg (&call_fusage, struct_value);
4033 /* Don't allow popping to be deferred, since then
4034 cse'ing of library calls could delete a call and leave the pop. */
4035 NO_DEFER_POP;
4036 valreg = (mem_value == 0 && outmode != VOIDmode
4037 ? hard_libcall_value (outmode) : NULL_RTX);
4039 /* Stack must be properly aligned now. */
4040 if (stack_pointer_delta & (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1))
4041 abort ();
4043 before_call = get_last_insn ();
4045 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
4046 will set inhibit_defer_pop to that value. */
4047 /* The return type is needed to decide how many bytes the function pops.
4048 Signedness plays no role in that, so for simplicity, we pretend it's
4049 always signed. We also assume that the list of arguments passed has
4050 no impact, so we pretend it is unknown. */
4052 emit_call_1 (fun, NULL,
4053 get_identifier (XSTR (orgfun, 0)),
4054 build_function_type (tfom, NULL_TREE),
4055 original_args_size.constant, args_size.constant,
4056 struct_value_size,
4057 FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1),
4058 valreg,
4059 old_inhibit_defer_pop + 1, call_fusage, flags, & args_so_far);
4061 /* For calls to `setjmp', etc., inform flow.c it should complain
4062 if nonvolatile values are live. For functions that cannot return,
4063 inform flow that control does not fall through. */
4065 if (flags & (ECF_NORETURN | ECF_LONGJMP))
4067 /* The barrier note must be emitted
4068 immediately after the CALL_INSN. Some ports emit more than
4069 just a CALL_INSN above, so we must search for it here. */
4071 rtx last = get_last_insn ();
4072 while (GET_CODE (last) != CALL_INSN)
4074 last = PREV_INSN (last);
4075 /* There was no CALL_INSN? */
4076 if (last == before_call)
4077 abort ();
4080 emit_barrier_after (last);
4083 /* Now restore inhibit_defer_pop to its actual original value. */
4084 OK_DEFER_POP;
4086 /* If call is cse'able, make appropriate pair of reg-notes around it.
4087 Test valreg so we don't crash; may safely ignore `const'
4088 if return type is void. Disable for PARALLEL return values, because
4089 we have no way to move such values into a pseudo register. */
4090 if (flags & ECF_LIBCALL_BLOCK)
4092 rtx insns;
4094 if (valreg == 0)
4096 insns = get_insns ();
4097 end_sequence ();
4098 emit_insn (insns);
4100 else
4102 rtx note = 0;
4103 rtx temp;
4104 int i;
4106 if (GET_CODE (valreg) == PARALLEL)
4108 temp = gen_reg_rtx (outmode);
4109 emit_group_store (temp, valreg, NULL_TREE,
4110 GET_MODE_SIZE (outmode));
4111 valreg = temp;
4114 temp = gen_reg_rtx (GET_MODE (valreg));
4116 /* Construct an "equal form" for the value which mentions all the
4117 arguments in order as well as the function name. */
4118 for (i = 0; i < nargs; i++)
4119 note = gen_rtx_EXPR_LIST (VOIDmode, argvec[i].value, note);
4120 note = gen_rtx_EXPR_LIST (VOIDmode, fun, note);
4122 insns = get_insns ();
4123 end_sequence ();
4125 if (flags & ECF_PURE)
4126 note = gen_rtx_EXPR_LIST (VOIDmode,
4127 gen_rtx_USE (VOIDmode,
4128 gen_rtx_MEM (BLKmode,
4129 gen_rtx_SCRATCH (VOIDmode))),
4130 note);
4132 emit_libcall_block (insns, temp, valreg, note);
4134 valreg = temp;
4137 pop_temp_slots ();
4139 /* Copy the value to the right place. */
4140 if (outmode != VOIDmode && retval)
4142 if (mem_value)
4144 if (value == 0)
4145 value = mem_value;
4146 if (value != mem_value)
4147 emit_move_insn (value, mem_value);
4149 else if (GET_CODE (valreg) == PARALLEL)
4151 if (value == 0)
4152 value = gen_reg_rtx (outmode);
4153 emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
4155 else if (value != 0)
4156 emit_move_insn (value, valreg);
4157 else
4158 value = valreg;
4161 if (ACCUMULATE_OUTGOING_ARGS)
4163 #ifdef REG_PARM_STACK_SPACE
4164 if (save_area)
4165 restore_fixed_argument_area (save_area, argblock,
4166 high_to_save, low_to_save);
4167 #endif
4169 /* If we saved any argument areas, restore them. */
4170 for (count = 0; count < nargs; count++)
4171 if (argvec[count].save_area)
4173 enum machine_mode save_mode = GET_MODE (argvec[count].save_area);
4174 rtx adr = plus_constant (argblock,
4175 argvec[count].locate.offset.constant);
4176 rtx stack_area = gen_rtx_MEM (save_mode,
4177 memory_address (save_mode, adr));
4179 if (save_mode == BLKmode)
4180 emit_block_move (stack_area,
4181 validize_mem (argvec[count].save_area),
4182 GEN_INT (argvec[count].locate.size.constant),
4183 BLOCK_OP_CALL_PARM);
4184 else
4185 emit_move_insn (stack_area, argvec[count].save_area);
4188 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
4189 stack_usage_map = initial_stack_usage_map;
4192 return value;
4196 /* Output a library call to function FUN (a SYMBOL_REF rtx)
4197 (emitting the queue unless NO_QUEUE is nonzero),
4198 for a value of mode OUTMODE,
4199 with NARGS different arguments, passed as alternating rtx values
4200 and machine_modes to convert them to.
4201 The rtx values should have been passed through protect_from_queue already.
4203 FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for `const'
4204 calls, LCT_PURE for `pure' calls, LCT_CONST_MAKE_BLOCK for `const' calls
4205 which should be enclosed in REG_LIBCALL/REG_RETVAL notes,
4206 LCT_PURE_MAKE_BLOCK for `purep' calls which should be enclosed in
4207 REG_LIBCALL/REG_RETVAL notes with extra (use (memory (scratch)),
4208 or other LCT_ value for other types of library calls. */
4210 void
4211 emit_library_call (rtx orgfun, enum libcall_type fn_type,
4212 enum machine_mode outmode, int nargs, ...)
4214 va_list p;
4216 va_start (p, nargs);
4217 emit_library_call_value_1 (0, orgfun, NULL_RTX, fn_type, outmode, nargs, p);
4218 va_end (p);
4221 /* Like emit_library_call except that an extra argument, VALUE,
4222 comes second and says where to store the result.
4223 (If VALUE is zero, this function chooses a convenient way
4224 to return the value.
4226 This function returns an rtx for where the value is to be found.
4227 If VALUE is nonzero, VALUE is returned. */
4230 emit_library_call_value (rtx orgfun, rtx value,
4231 enum libcall_type fn_type,
4232 enum machine_mode outmode, int nargs, ...)
4234 rtx result;
4235 va_list p;
4237 va_start (p, nargs);
4238 result = emit_library_call_value_1 (1, orgfun, value, fn_type, outmode,
4239 nargs, p);
4240 va_end (p);
4242 return result;
4245 /* Store a single argument for a function call
4246 into the register or memory area where it must be passed.
4247 *ARG describes the argument value and where to pass it.
4249 ARGBLOCK is the address of the stack-block for all the arguments,
4250 or 0 on a machine where arguments are pushed individually.
4252 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
4253 so must be careful about how the stack is used.
4255 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
4256 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
4257 that we need not worry about saving and restoring the stack.
4259 FNDECL is the declaration of the function we are calling.
4261 Return nonzero if this arg should cause sibcall failure,
4262 zero otherwise. */
4264 static int
4265 store_one_arg (struct arg_data *arg, rtx argblock, int flags,
4266 int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
4268 tree pval = arg->tree_value;
4269 rtx reg = 0;
4270 int partial = 0;
4271 int used = 0;
4272 int i, lower_bound = 0, upper_bound = 0;
4273 int sibcall_failure = 0;
4275 if (TREE_CODE (pval) == ERROR_MARK)
4276 return 1;
4278 /* Push a new temporary level for any temporaries we make for
4279 this argument. */
4280 push_temp_slots ();
4282 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
4284 /* If this is being stored into a pre-allocated, fixed-size, stack area,
4285 save any previous data at that location. */
4286 if (argblock && ! variable_size && arg->stack)
4288 #ifdef ARGS_GROW_DOWNWARD
4289 /* stack_slot is negative, but we want to index stack_usage_map
4290 with positive values. */
4291 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4292 upper_bound = -INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1)) + 1;
4293 else
4294 upper_bound = 0;
4296 lower_bound = upper_bound - arg->locate.size.constant;
4297 #else
4298 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4299 lower_bound = INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1));
4300 else
4301 lower_bound = 0;
4303 upper_bound = lower_bound + arg->locate.size.constant;
4304 #endif
4306 i = lower_bound;
4307 /* Don't worry about things in the fixed argument area;
4308 it has already been saved. */
4309 if (i < reg_parm_stack_space)
4310 i = reg_parm_stack_space;
4311 while (i < upper_bound && stack_usage_map[i] == 0)
4312 i++;
4314 if (i < upper_bound)
4316 /* We need to make a save area. */
4317 unsigned int size = arg->locate.size.constant * BITS_PER_UNIT;
4318 enum machine_mode save_mode = mode_for_size (size, MODE_INT, 1);
4319 rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
4320 rtx stack_area = gen_rtx_MEM (save_mode, adr);
4322 if (save_mode == BLKmode)
4324 tree ot = TREE_TYPE (arg->tree_value);
4325 tree nt = build_qualified_type (ot, (TYPE_QUALS (ot)
4326 | TYPE_QUAL_CONST));
4328 arg->save_area = assign_temp (nt, 0, 1, 1);
4329 preserve_temp_slots (arg->save_area);
4330 emit_block_move (validize_mem (arg->save_area), stack_area,
4331 expr_size (arg->tree_value),
4332 BLOCK_OP_CALL_PARM);
4334 else
4336 arg->save_area = gen_reg_rtx (save_mode);
4337 emit_move_insn (arg->save_area, stack_area);
4343 /* If this isn't going to be placed on both the stack and in registers,
4344 set up the register and number of words. */
4345 if (! arg->pass_on_stack)
4347 if (flags & ECF_SIBCALL)
4348 reg = arg->tail_call_reg;
4349 else
4350 reg = arg->reg;
4351 partial = arg->partial;
4354 if (reg != 0 && partial == 0)
4355 /* Being passed entirely in a register. We shouldn't be called in
4356 this case. */
4357 abort ();
4359 /* If this arg needs special alignment, don't load the registers
4360 here. */
4361 if (arg->n_aligned_regs != 0)
4362 reg = 0;
4364 /* If this is being passed partially in a register, we can't evaluate
4365 it directly into its stack slot. Otherwise, we can. */
4366 if (arg->value == 0)
4368 /* stack_arg_under_construction is nonzero if a function argument is
4369 being evaluated directly into the outgoing argument list and
4370 expand_call must take special action to preserve the argument list
4371 if it is called recursively.
4373 For scalar function arguments stack_usage_map is sufficient to
4374 determine which stack slots must be saved and restored. Scalar
4375 arguments in general have pass_on_stack == 0.
4377 If this argument is initialized by a function which takes the
4378 address of the argument (a C++ constructor or a C function
4379 returning a BLKmode structure), then stack_usage_map is
4380 insufficient and expand_call must push the stack around the
4381 function call. Such arguments have pass_on_stack == 1.
4383 Note that it is always safe to set stack_arg_under_construction,
4384 but this generates suboptimal code if set when not needed. */
4386 if (arg->pass_on_stack)
4387 stack_arg_under_construction++;
4389 arg->value = expand_expr (pval,
4390 (partial
4391 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
4392 ? NULL_RTX : arg->stack,
4393 VOIDmode, EXPAND_STACK_PARM);
4395 /* If we are promoting object (or for any other reason) the mode
4396 doesn't agree, convert the mode. */
4398 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
4399 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
4400 arg->value, arg->unsignedp);
4402 if (arg->pass_on_stack)
4403 stack_arg_under_construction--;
4406 /* Don't allow anything left on stack from computation
4407 of argument to alloca. */
4408 if (flags & ECF_MAY_BE_ALLOCA)
4409 do_pending_stack_adjust ();
4411 if (arg->value == arg->stack)
4412 /* If the value is already in the stack slot, we are done. */
4414 else if (arg->mode != BLKmode)
4416 int size;
4418 /* Argument is a scalar, not entirely passed in registers.
4419 (If part is passed in registers, arg->partial says how much
4420 and emit_push_insn will take care of putting it there.)
4422 Push it, and if its size is less than the
4423 amount of space allocated to it,
4424 also bump stack pointer by the additional space.
4425 Note that in C the default argument promotions
4426 will prevent such mismatches. */
4428 size = GET_MODE_SIZE (arg->mode);
4429 /* Compute how much space the push instruction will push.
4430 On many machines, pushing a byte will advance the stack
4431 pointer by a halfword. */
4432 #ifdef PUSH_ROUNDING
4433 size = PUSH_ROUNDING (size);
4434 #endif
4435 used = size;
4437 /* Compute how much space the argument should get:
4438 round up to a multiple of the alignment for arguments. */
4439 if (none != FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)))
4440 used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
4441 / (PARM_BOUNDARY / BITS_PER_UNIT))
4442 * (PARM_BOUNDARY / BITS_PER_UNIT));
4444 /* This isn't already where we want it on the stack, so put it there.
4445 This can either be done with push or copy insns. */
4446 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), NULL_RTX,
4447 PARM_BOUNDARY, partial, reg, used - size, argblock,
4448 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4449 ARGS_SIZE_RTX (arg->locate.alignment_pad));
4451 /* Unless this is a partially-in-register argument, the argument is now
4452 in the stack. */
4453 if (partial == 0)
4454 arg->value = arg->stack;
4456 else
4458 /* BLKmode, at least partly to be pushed. */
4460 unsigned int parm_align;
4461 int excess;
4462 rtx size_rtx;
4464 /* Pushing a nonscalar.
4465 If part is passed in registers, PARTIAL says how much
4466 and emit_push_insn will take care of putting it there. */
4468 /* Round its size up to a multiple
4469 of the allocation unit for arguments. */
4471 if (arg->locate.size.var != 0)
4473 excess = 0;
4474 size_rtx = ARGS_SIZE_RTX (arg->locate.size);
4476 else
4478 /* PUSH_ROUNDING has no effect on us, because
4479 emit_push_insn for BLKmode is careful to avoid it. */
4480 if (reg && GET_CODE (reg) == PARALLEL)
4482 /* Use the size of the elt to compute excess. */
4483 rtx elt = XEXP (XVECEXP (reg, 0, 0), 0);
4484 excess = (arg->locate.size.constant
4485 - int_size_in_bytes (TREE_TYPE (pval))
4486 + partial * GET_MODE_SIZE (GET_MODE (elt)));
4488 else
4489 excess = (arg->locate.size.constant
4490 - int_size_in_bytes (TREE_TYPE (pval))
4491 + partial * UNITS_PER_WORD);
4492 size_rtx = expand_expr (size_in_bytes (TREE_TYPE (pval)),
4493 NULL_RTX, TYPE_MODE (sizetype), 0);
4496 /* Some types will require stricter alignment, which will be
4497 provided for elsewhere in argument layout. */
4498 parm_align = MAX (PARM_BOUNDARY, TYPE_ALIGN (TREE_TYPE (pval)));
4500 /* When an argument is padded down, the block is aligned to
4501 PARM_BOUNDARY, but the actual argument isn't. */
4502 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
4504 if (arg->locate.size.var)
4505 parm_align = BITS_PER_UNIT;
4506 else if (excess)
4508 unsigned int excess_align = (excess & -excess) * BITS_PER_UNIT;
4509 parm_align = MIN (parm_align, excess_align);
4513 if ((flags & ECF_SIBCALL) && GET_CODE (arg->value) == MEM)
4515 /* emit_push_insn might not work properly if arg->value and
4516 argblock + arg->locate.offset areas overlap. */
4517 rtx x = arg->value;
4518 int i = 0;
4520 if (XEXP (x, 0) == current_function_internal_arg_pointer
4521 || (GET_CODE (XEXP (x, 0)) == PLUS
4522 && XEXP (XEXP (x, 0), 0) ==
4523 current_function_internal_arg_pointer
4524 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4526 if (XEXP (x, 0) != current_function_internal_arg_pointer)
4527 i = INTVAL (XEXP (XEXP (x, 0), 1));
4529 /* expand_call should ensure this. */
4530 if (arg->locate.offset.var || GET_CODE (size_rtx) != CONST_INT)
4531 abort ();
4533 if (arg->locate.offset.constant > i)
4535 if (arg->locate.offset.constant < i + INTVAL (size_rtx))
4536 sibcall_failure = 1;
4538 else if (arg->locate.offset.constant < i)
4540 if (i < arg->locate.offset.constant + INTVAL (size_rtx))
4541 sibcall_failure = 1;
4546 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
4547 parm_align, partial, reg, excess, argblock,
4548 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4549 ARGS_SIZE_RTX (arg->locate.alignment_pad));
4551 /* Unless this is a partially-in-register argument, the argument is now
4552 in the stack.
4554 ??? Unlike the case above, in which we want the actual
4555 address of the data, so that we can load it directly into a
4556 register, here we want the address of the stack slot, so that
4557 it's properly aligned for word-by-word copying or something
4558 like that. It's not clear that this is always correct. */
4559 if (partial == 0)
4560 arg->value = arg->stack_slot;
4563 /* Mark all slots this store used. */
4564 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
4565 && argblock && ! variable_size && arg->stack)
4566 for (i = lower_bound; i < upper_bound; i++)
4567 stack_usage_map[i] = 1;
4569 /* Once we have pushed something, pops can't safely
4570 be deferred during the rest of the arguments. */
4571 NO_DEFER_POP;
4573 /* ANSI doesn't require a sequence point here,
4574 but PCC has one, so this will avoid some problems. */
4575 emit_queue ();
4577 /* Free any temporary slots made in processing this argument. Show
4578 that we might have taken the address of something and pushed that
4579 as an operand. */
4580 preserve_temp_slots (NULL_RTX);
4581 free_temp_slots ();
4582 pop_temp_slots ();
4584 return sibcall_failure;
4587 /* Nonzero if we do not know how to pass TYPE solely in registers.
4588 We cannot do so in the following cases:
4590 - if the type has variable size
4591 - if the type is marked as addressable (it is required to be constructed
4592 into the stack)
4593 - if the padding and mode of the type is such that a copy into a register
4594 would put it into the wrong part of the register.
4596 Which padding can't be supported depends on the byte endianness.
4598 A value in a register is implicitly padded at the most significant end.
4599 On a big-endian machine, that is the lower end in memory.
4600 So a value padded in memory at the upper end can't go in a register.
4601 For a little-endian machine, the reverse is true. */
4603 bool
4604 default_must_pass_in_stack (enum machine_mode mode, tree type)
4606 if (!type)
4607 return false;
4609 /* If the type has variable size... */
4610 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4611 return true;
4613 /* If the type is marked as addressable (it is required
4614 to be constructed into the stack)... */
4615 if (TREE_ADDRESSABLE (type))
4616 return true;
4618 /* If the padding and mode of the type is such that a copy into
4619 a register would put it into the wrong part of the register. */
4620 if (mode == BLKmode
4621 && int_size_in_bytes (type) % (PARM_BOUNDARY / BITS_PER_UNIT)
4622 && (FUNCTION_ARG_PADDING (mode, type)
4623 == (BYTES_BIG_ENDIAN ? upward : downward)))
4624 return true;
4626 return false;