Fix typos in comment
[official-gcc.git] / gcc / calls.c
blob784b3b165f8919253736f1e08e9d482a95884e2d
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 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "expr.h"
28 #include "function.h"
29 #include "regs.h"
30 #include "insn-flags.h"
31 #include "toplev.h"
32 #include "output.h"
33 #include "tm_p.h"
34 #include "timevar.h"
35 #include "sbitmap.h"
37 #ifndef ACCUMULATE_OUTGOING_ARGS
38 #define ACCUMULATE_OUTGOING_ARGS 0
39 #endif
41 /* Supply a default definition for PUSH_ARGS. */
42 #ifndef PUSH_ARGS
43 #ifdef PUSH_ROUNDING
44 #define PUSH_ARGS !ACCUMULATE_OUTGOING_ARGS
45 #else
46 #define PUSH_ARGS 0
47 #endif
48 #endif
50 #if !defined FUNCTION_OK_FOR_SIBCALL
51 #define FUNCTION_OK_FOR_SIBCALL(DECL) 1
52 #endif
54 #if !defined PREFERRED_STACK_BOUNDARY && defined STACK_BOUNDARY
55 #define PREFERRED_STACK_BOUNDARY STACK_BOUNDARY
56 #endif
58 /* Decide whether a function's arguments should be processed
59 from first to last or from last to first.
61 They should if the stack and args grow in opposite directions, but
62 only if we have push insns. */
64 #ifdef PUSH_ROUNDING
66 #if defined (STACK_GROWS_DOWNWARD) != defined (ARGS_GROW_DOWNWARD)
67 #define PUSH_ARGS_REVERSED PUSH_ARGS
68 #endif
70 #endif
72 #ifndef PUSH_ARGS_REVERSED
73 #define PUSH_ARGS_REVERSED 0
74 #endif
76 /* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits. */
77 #define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
79 /* Data structure and subroutines used within expand_call. */
81 struct arg_data
83 /* Tree node for this argument. */
84 tree tree_value;
85 /* Mode for value; TYPE_MODE unless promoted. */
86 enum machine_mode mode;
87 /* Current RTL value for argument, or 0 if it isn't precomputed. */
88 rtx value;
89 /* Initially-compute RTL value for argument; only for const functions. */
90 rtx initial_value;
91 /* Register to pass this argument in, 0 if passed on stack, or an
92 PARALLEL if the arg is to be copied into multiple non-contiguous
93 registers. */
94 rtx reg;
95 /* Register to pass this argument in when generating tail call sequence.
96 This is not the same register as for normal calls on machines with
97 register windows. */
98 rtx tail_call_reg;
99 /* If REG was promoted from the actual mode of the argument expression,
100 indicates whether the promotion is sign- or zero-extended. */
101 int unsignedp;
102 /* Number of registers to use. 0 means put the whole arg in registers.
103 Also 0 if not passed in registers. */
104 int partial;
105 /* Non-zero if argument must be passed on stack.
106 Note that some arguments may be passed on the stack
107 even though pass_on_stack is zero, just because FUNCTION_ARG says so.
108 pass_on_stack identifies arguments that *cannot* go in registers. */
109 int pass_on_stack;
110 /* Offset of this argument from beginning of stack-args. */
111 struct args_size offset;
112 /* Similar, but offset to the start of the stack slot. Different from
113 OFFSET if this arg pads downward. */
114 struct args_size slot_offset;
115 /* Size of this argument on the stack, rounded up for any padding it gets,
116 parts of the argument passed in registers do not count.
117 If REG_PARM_STACK_SPACE is defined, then register parms
118 are counted here as well. */
119 struct args_size size;
120 /* Location on the stack at which parameter should be stored. The store
121 has already been done if STACK == VALUE. */
122 rtx stack;
123 /* Location on the stack of the start of this argument slot. This can
124 differ from STACK if this arg pads downward. This location is known
125 to be aligned to FUNCTION_ARG_BOUNDARY. */
126 rtx stack_slot;
127 /* Place that this stack area has been saved, if needed. */
128 rtx save_area;
129 /* If an argument's alignment does not permit direct copying into registers,
130 copy in smaller-sized pieces into pseudos. These are stored in a
131 block pointed to by this field. The next field says how many
132 word-sized pseudos we made. */
133 rtx *aligned_regs;
134 int n_aligned_regs;
135 /* The amount that the stack pointer needs to be adjusted to
136 force alignment for the next argument. */
137 struct args_size alignment_pad;
140 /* A vector of one char per byte of stack space. A byte if non-zero if
141 the corresponding stack location has been used.
142 This vector is used to prevent a function call within an argument from
143 clobbering any stack already set up. */
144 static char *stack_usage_map;
146 /* Size of STACK_USAGE_MAP. */
147 static int highest_outgoing_arg_in_use;
149 /* A bitmap of virtual-incoming stack space. Bit is set if the corresponding
150 stack location's tail call argument has been already stored into the stack.
151 This bitmap is used to prevent sibling call optimization if function tries
152 to use parent's incoming argument slots when they have been already
153 overwritten with tail call arguments. */
154 static sbitmap stored_args_map;
156 /* stack_arg_under_construction is nonzero when an argument may be
157 initialized with a constructor call (including a C function that
158 returns a BLKmode struct) and expand_call must take special action
159 to make sure the object being constructed does not overlap the
160 argument list for the constructor call. */
161 int stack_arg_under_construction;
163 static int calls_function PARAMS ((tree, int));
164 static int calls_function_1 PARAMS ((tree, int));
166 /* Nonzero if this is a call to a `const' function. */
167 #define ECF_CONST 1
168 /* Nonzero if this is a call to a `volatile' function. */
169 #define ECF_NORETURN 2
170 /* Nonzero if this is a call to malloc or a related function. */
171 #define ECF_MALLOC 4
172 /* Nonzero if it is plausible that this is a call to alloca. */
173 #define ECF_MAY_BE_ALLOCA 8
174 /* Nonzero if this is a call to a function that won't throw an exception. */
175 #define ECF_NOTHROW 16
176 /* Nonzero if this is a call to setjmp or a related function. */
177 #define ECF_RETURNS_TWICE 32
178 /* Nonzero if this is a call to `longjmp'. */
179 #define ECF_LONGJMP 64
180 /* Nonzero if this is a syscall that makes a new process in the image of
181 the current one. */
182 #define ECF_FORK_OR_EXEC 128
183 #define ECF_SIBCALL 256
184 /* Nonzero if this is a call to "pure" function (like const function,
185 but may read memory. */
186 #define ECF_PURE 512
188 static void emit_call_1 PARAMS ((rtx, tree, tree, HOST_WIDE_INT,
189 HOST_WIDE_INT, HOST_WIDE_INT, rtx,
190 rtx, int, rtx, int));
191 static void precompute_register_parameters PARAMS ((int,
192 struct arg_data *,
193 int *));
194 static void store_one_arg PARAMS ((struct arg_data *, rtx, int, int,
195 int));
196 static void store_unaligned_arguments_into_pseudos PARAMS ((struct arg_data *,
197 int));
198 static int finalize_must_preallocate PARAMS ((int, int,
199 struct arg_data *,
200 struct args_size *));
201 static void precompute_arguments PARAMS ((int, int,
202 struct arg_data *));
203 static int compute_argument_block_size PARAMS ((int,
204 struct args_size *,
205 int));
206 static void initialize_argument_information PARAMS ((int,
207 struct arg_data *,
208 struct args_size *,
209 int, tree, tree,
210 CUMULATIVE_ARGS *,
211 int, rtx *, int *,
212 int *, int *));
213 static void compute_argument_addresses PARAMS ((struct arg_data *,
214 rtx, int));
215 static rtx rtx_for_function_call PARAMS ((tree, tree));
216 static void load_register_parameters PARAMS ((struct arg_data *,
217 int, rtx *, int));
218 static int libfunc_nothrow PARAMS ((rtx));
219 static rtx emit_library_call_value_1 PARAMS ((int, rtx, rtx, int,
220 enum machine_mode,
221 int, va_list));
222 static int special_function_p PARAMS ((tree, int));
223 static int flags_from_decl_or_type PARAMS ((tree));
224 static rtx try_to_integrate PARAMS ((tree, tree, rtx,
225 int, tree, rtx));
226 static int check_sibcall_argument_overlap_1 PARAMS ((rtx));
227 static int check_sibcall_argument_overlap PARAMS ((rtx, struct arg_data *));
229 static int combine_pending_stack_adjustment_and_call
230 PARAMS ((int, struct args_size *, int));
232 #ifdef REG_PARM_STACK_SPACE
233 static rtx save_fixed_argument_area PARAMS ((int, rtx, int *, int *));
234 static void restore_fixed_argument_area PARAMS ((rtx, rtx, int, int));
235 #endif
237 /* If WHICH is 1, return 1 if EXP contains a call to the built-in function
238 `alloca'.
240 If WHICH is 0, return 1 if EXP contains a call to any function.
241 Actually, we only need return 1 if evaluating EXP would require pushing
242 arguments on the stack, but that is too difficult to compute, so we just
243 assume any function call might require the stack. */
245 static tree calls_function_save_exprs;
247 static int
248 calls_function (exp, which)
249 tree exp;
250 int which;
252 int val;
254 calls_function_save_exprs = 0;
255 val = calls_function_1 (exp, which);
256 calls_function_save_exprs = 0;
257 return val;
260 /* Recursive function to do the work of above function. */
262 static int
263 calls_function_1 (exp, which)
264 tree exp;
265 int which;
267 register int i;
268 enum tree_code code = TREE_CODE (exp);
269 int class = TREE_CODE_CLASS (code);
270 int length = first_rtl_op (code);
272 /* If this code is language-specific, we don't know what it will do. */
273 if ((int) code >= NUM_TREE_CODES)
274 return 1;
276 switch (code)
278 case CALL_EXPR:
279 if (which == 0)
280 return 1;
281 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
282 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
283 == FUNCTION_DECL)
284 && (special_function_p (TREE_OPERAND (TREE_OPERAND (exp, 0), 0),
286 & ECF_MAY_BE_ALLOCA))
287 return 1;
289 break;
291 case SAVE_EXPR:
292 if (SAVE_EXPR_RTL (exp) != 0)
293 return 0;
294 if (value_member (exp, calls_function_save_exprs))
295 return 0;
296 calls_function_save_exprs = tree_cons (NULL_TREE, exp,
297 calls_function_save_exprs);
298 return (TREE_OPERAND (exp, 0) != 0
299 && calls_function_1 (TREE_OPERAND (exp, 0), which));
301 case BLOCK:
303 register tree local;
304 register tree subblock;
306 for (local = BLOCK_VARS (exp); local; local = TREE_CHAIN (local))
307 if (DECL_INITIAL (local) != 0
308 && calls_function_1 (DECL_INITIAL (local), which))
309 return 1;
311 for (subblock = BLOCK_SUBBLOCKS (exp);
312 subblock;
313 subblock = TREE_CHAIN (subblock))
314 if (calls_function_1 (subblock, which))
315 return 1;
317 return 0;
319 case TREE_LIST:
320 for (; exp != 0; exp = TREE_CHAIN (exp))
321 if (calls_function_1 (TREE_VALUE (exp), which))
322 return 1;
323 return 0;
325 default:
326 break;
329 /* Only expressions, references, and blocks can contain calls. */
330 if (! IS_EXPR_CODE_CLASS (class) && class != 'r' && class != 'b')
331 return 0;
333 for (i = 0; i < length; i++)
334 if (TREE_OPERAND (exp, i) != 0
335 && calls_function_1 (TREE_OPERAND (exp, i), which))
336 return 1;
338 return 0;
341 /* Force FUNEXP into a form suitable for the address of a CALL,
342 and return that as an rtx. Also load the static chain register
343 if FNDECL is a nested function.
345 CALL_FUSAGE points to a variable holding the prospective
346 CALL_INSN_FUNCTION_USAGE information. */
349 prepare_call_address (funexp, fndecl, call_fusage, reg_parm_seen)
350 rtx funexp;
351 tree fndecl;
352 rtx *call_fusage;
353 int reg_parm_seen;
355 rtx static_chain_value = 0;
357 funexp = protect_from_queue (funexp, 0);
359 if (fndecl != 0)
360 /* Get possible static chain value for nested function in C. */
361 static_chain_value = lookup_static_chain (fndecl);
363 /* Make a valid memory address and copy constants thru pseudo-regs,
364 but not for a constant address if -fno-function-cse. */
365 if (GET_CODE (funexp) != SYMBOL_REF)
366 /* If we are using registers for parameters, force the
367 function address into a register now. */
368 funexp = ((SMALL_REGISTER_CLASSES && reg_parm_seen)
369 ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
370 : memory_address (FUNCTION_MODE, funexp));
371 else
373 #ifndef NO_FUNCTION_CSE
374 if (optimize && ! flag_no_function_cse)
375 #ifdef NO_RECURSIVE_FUNCTION_CSE
376 if (fndecl != current_function_decl)
377 #endif
378 funexp = force_reg (Pmode, funexp);
379 #endif
382 if (static_chain_value != 0)
384 emit_move_insn (static_chain_rtx, static_chain_value);
386 if (GET_CODE (static_chain_rtx) == REG)
387 use_reg (call_fusage, static_chain_rtx);
390 return funexp;
393 /* Generate instructions to call function FUNEXP,
394 and optionally pop the results.
395 The CALL_INSN is the first insn generated.
397 FNDECL is the declaration node of the function. This is given to the
398 macro RETURN_POPS_ARGS to determine whether this function pops its own args.
400 FUNTYPE is the data type of the function. This is given to the macro
401 RETURN_POPS_ARGS to determine whether this function pops its own args.
402 We used to allow an identifier for library functions, but that doesn't
403 work when the return type is an aggregate type and the calling convention
404 says that the pointer to this aggregate is to be popped by the callee.
406 STACK_SIZE is the number of bytes of arguments on the stack,
407 ROUNDED_STACK_SIZE is that number rounded up to
408 PREFERRED_STACK_BOUNDARY; zero if the size is variable. This is
409 both to put into the call insn and to generate explicit popping
410 code if necessary.
412 STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
413 It is zero if this call doesn't want a structure value.
415 NEXT_ARG_REG is the rtx that results from executing
416 FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1)
417 just after all the args have had their registers assigned.
418 This could be whatever you like, but normally it is the first
419 arg-register beyond those used for args in this call,
420 or 0 if all the arg-registers are used in this call.
421 It is passed on to `gen_call' so you can put this info in the call insn.
423 VALREG is a hard register in which a value is returned,
424 or 0 if the call does not return a value.
426 OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
427 the args to this call were processed.
428 We restore `inhibit_defer_pop' to that value.
430 CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
431 denote registers used by the called function. */
433 static void
434 emit_call_1 (funexp, fndecl, funtype, stack_size, rounded_stack_size,
435 struct_value_size, next_arg_reg, valreg, old_inhibit_defer_pop,
436 call_fusage, ecf_flags)
437 rtx funexp;
438 tree fndecl ATTRIBUTE_UNUSED;
439 tree funtype ATTRIBUTE_UNUSED;
440 HOST_WIDE_INT stack_size ATTRIBUTE_UNUSED;
441 HOST_WIDE_INT rounded_stack_size;
442 HOST_WIDE_INT struct_value_size ATTRIBUTE_UNUSED;
443 rtx next_arg_reg;
444 rtx valreg;
445 int old_inhibit_defer_pop;
446 rtx call_fusage;
447 int ecf_flags;
449 rtx rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
450 rtx call_insn;
451 int already_popped = 0;
452 HOST_WIDE_INT n_popped = RETURN_POPS_ARGS (fndecl, funtype, stack_size);
453 #if defined (HAVE_call) && defined (HAVE_call_value)
454 rtx struct_value_size_rtx;
455 struct_value_size_rtx = GEN_INT (struct_value_size);
456 #endif
458 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
459 and we don't want to load it into a register as an optimization,
460 because prepare_call_address already did it if it should be done. */
461 if (GET_CODE (funexp) != SYMBOL_REF)
462 funexp = memory_address (FUNCTION_MODE, funexp);
464 #if defined (HAVE_sibcall_pop) && defined (HAVE_sibcall_value_pop)
465 if ((ecf_flags & ECF_SIBCALL)
466 && HAVE_sibcall_pop && HAVE_sibcall_value_pop
467 && (RETURN_POPS_ARGS (fndecl, funtype, stack_size) > 0
468 || stack_size == 0))
470 rtx n_pop = GEN_INT (RETURN_POPS_ARGS (fndecl, funtype, stack_size));
471 rtx pat;
473 /* If this subroutine pops its own args, record that in the call insn
474 if possible, for the sake of frame pointer elimination. */
476 if (valreg)
477 pat = GEN_SIBCALL_VALUE_POP (valreg,
478 gen_rtx_MEM (FUNCTION_MODE, funexp),
479 rounded_stack_size_rtx, next_arg_reg,
480 n_pop);
481 else
482 pat = GEN_SIBCALL_POP (gen_rtx_MEM (FUNCTION_MODE, funexp),
483 rounded_stack_size_rtx, next_arg_reg, n_pop);
485 emit_call_insn (pat);
486 already_popped = 1;
488 else
489 #endif
491 #if defined (HAVE_call_pop) && defined (HAVE_call_value_pop)
492 /* If the target has "call" or "call_value" insns, then prefer them
493 if no arguments are actually popped. If the target does not have
494 "call" or "call_value" insns, then we must use the popping versions
495 even if the call has no arguments to pop. */
496 #if defined (HAVE_call) && defined (HAVE_call_value)
497 if (HAVE_call && HAVE_call_value && HAVE_call_pop && HAVE_call_value_pop
498 && n_popped > 0)
499 #else
500 if (HAVE_call_pop && HAVE_call_value_pop)
501 #endif
503 rtx n_pop = GEN_INT (n_popped);
504 rtx pat;
506 /* If this subroutine pops its own args, record that in the call insn
507 if possible, for the sake of frame pointer elimination. */
509 if (valreg)
510 pat = GEN_CALL_VALUE_POP (valreg,
511 gen_rtx_MEM (FUNCTION_MODE, funexp),
512 rounded_stack_size_rtx, next_arg_reg, n_pop);
513 else
514 pat = GEN_CALL_POP (gen_rtx_MEM (FUNCTION_MODE, funexp),
515 rounded_stack_size_rtx, next_arg_reg, n_pop);
517 emit_call_insn (pat);
518 already_popped = 1;
520 else
521 #endif
523 #if defined (HAVE_sibcall) && defined (HAVE_sibcall_value)
524 if ((ecf_flags & ECF_SIBCALL)
525 && HAVE_sibcall && HAVE_sibcall_value)
527 if (valreg)
528 emit_call_insn (GEN_SIBCALL_VALUE (valreg,
529 gen_rtx_MEM (FUNCTION_MODE, funexp),
530 rounded_stack_size_rtx,
531 next_arg_reg, NULL_RTX));
532 else
533 emit_call_insn (GEN_SIBCALL (gen_rtx_MEM (FUNCTION_MODE, funexp),
534 rounded_stack_size_rtx, next_arg_reg,
535 struct_value_size_rtx));
537 else
538 #endif
540 #if defined (HAVE_call) && defined (HAVE_call_value)
541 if (HAVE_call && HAVE_call_value)
543 if (valreg)
544 emit_call_insn (GEN_CALL_VALUE (valreg,
545 gen_rtx_MEM (FUNCTION_MODE, funexp),
546 rounded_stack_size_rtx, next_arg_reg,
547 NULL_RTX));
548 else
549 emit_call_insn (GEN_CALL (gen_rtx_MEM (FUNCTION_MODE, funexp),
550 rounded_stack_size_rtx, next_arg_reg,
551 struct_value_size_rtx));
553 else
554 #endif
555 abort ();
557 /* Find the CALL insn we just emitted. */
558 for (call_insn = get_last_insn ();
559 call_insn && GET_CODE (call_insn) != CALL_INSN;
560 call_insn = PREV_INSN (call_insn))
563 if (! call_insn)
564 abort ();
566 /* Mark memory as used for "pure" function call. */
567 if (ecf_flags & ECF_PURE)
569 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
570 gen_rtx_USE (VOIDmode,
571 gen_rtx_MEM (BLKmode,
572 gen_rtx_SCRATCH (VOIDmode))), call_fusage);
575 /* Put the register usage information on the CALL. If there is already
576 some usage information, put ours at the end. */
577 if (CALL_INSN_FUNCTION_USAGE (call_insn))
579 rtx link;
581 for (link = CALL_INSN_FUNCTION_USAGE (call_insn); XEXP (link, 1) != 0;
582 link = XEXP (link, 1))
585 XEXP (link, 1) = call_fusage;
587 else
588 CALL_INSN_FUNCTION_USAGE (call_insn) = call_fusage;
590 /* If this is a const call, then set the insn's unchanging bit. */
591 if (ecf_flags & (ECF_CONST | ECF_PURE))
592 CONST_CALL_P (call_insn) = 1;
594 /* If this call can't throw, attach a REG_EH_REGION reg note to that
595 effect. */
596 if (ecf_flags & ECF_NOTHROW)
597 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, const0_rtx,
598 REG_NOTES (call_insn));
600 SIBLING_CALL_P (call_insn) = ((ecf_flags & ECF_SIBCALL) != 0);
602 /* Restore this now, so that we do defer pops for this call's args
603 if the context of the call as a whole permits. */
604 inhibit_defer_pop = old_inhibit_defer_pop;
606 if (n_popped > 0)
608 if (!already_popped)
609 CALL_INSN_FUNCTION_USAGE (call_insn)
610 = gen_rtx_EXPR_LIST (VOIDmode,
611 gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx),
612 CALL_INSN_FUNCTION_USAGE (call_insn));
613 rounded_stack_size -= n_popped;
614 rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
615 stack_pointer_delta -= n_popped;
618 if (!ACCUMULATE_OUTGOING_ARGS)
620 /* If returning from the subroutine does not automatically pop the args,
621 we need an instruction to pop them sooner or later.
622 Perhaps do it now; perhaps just record how much space to pop later.
624 If returning from the subroutine does pop the args, indicate that the
625 stack pointer will be changed. */
627 if (rounded_stack_size != 0)
629 if (flag_defer_pop && inhibit_defer_pop == 0
630 && !(ecf_flags & (ECF_CONST | ECF_PURE)))
631 pending_stack_adjust += rounded_stack_size;
632 else
633 adjust_stack (rounded_stack_size_rtx);
636 /* When we accumulate outgoing args, we must avoid any stack manipulations.
637 Restore the stack pointer to its original value now. Usually
638 ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
639 On i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
640 popping variants of functions exist as well.
642 ??? We may optimize similar to defer_pop above, but it is
643 probably not worthwhile.
645 ??? It will be worthwhile to enable combine_stack_adjustments even for
646 such machines. */
647 else if (n_popped)
648 anti_adjust_stack (GEN_INT (n_popped));
651 /* Determine if the function identified by NAME and FNDECL is one with
652 special properties we wish to know about.
654 For example, if the function might return more than one time (setjmp), then
655 set RETURNS_TWICE to a nonzero value.
657 Similarly set LONGJMP for if the function is in the longjmp family.
659 Set MALLOC for any of the standard memory allocation functions which
660 allocate from the heap.
662 Set MAY_BE_ALLOCA for any memory allocation function that might allocate
663 space from the stack such as alloca. */
665 static int
666 special_function_p (fndecl, flags)
667 tree fndecl;
668 int flags;
670 if (! (flags & ECF_MALLOC)
671 && fndecl && DECL_NAME (fndecl)
672 && IDENTIFIER_LENGTH (DECL_NAME (fndecl)) <= 17
673 /* Exclude functions not at the file scope, or not `extern',
674 since they are not the magic functions we would otherwise
675 think they are. */
676 && DECL_CONTEXT (fndecl) == NULL_TREE && TREE_PUBLIC (fndecl))
678 char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
679 char *tname = name;
681 /* We assume that alloca will always be called by name. It
682 makes no sense to pass it as a pointer-to-function to
683 anything that does not understand its behavior. */
684 if (((IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 6
685 && name[0] == 'a'
686 && ! strcmp (name, "alloca"))
687 || (IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 16
688 && name[0] == '_'
689 && ! strcmp (name, "__builtin_alloca"))))
690 flags |= ECF_MAY_BE_ALLOCA;
692 /* Disregard prefix _, __ or __x. */
693 if (name[0] == '_')
695 if (name[1] == '_' && name[2] == 'x')
696 tname += 3;
697 else if (name[1] == '_')
698 tname += 2;
699 else
700 tname += 1;
703 if (tname[0] == 's')
705 if ((tname[1] == 'e'
706 && (! strcmp (tname, "setjmp")
707 || ! strcmp (tname, "setjmp_syscall")))
708 || (tname[1] == 'i'
709 && ! strcmp (tname, "sigsetjmp"))
710 || (tname[1] == 'a'
711 && ! strcmp (tname, "savectx")))
712 flags |= ECF_RETURNS_TWICE;
714 if (tname[1] == 'i'
715 && ! strcmp (tname, "siglongjmp"))
716 flags |= ECF_LONGJMP;
718 else if ((tname[0] == 'q' && tname[1] == 's'
719 && ! strcmp (tname, "qsetjmp"))
720 || (tname[0] == 'v' && tname[1] == 'f'
721 && ! strcmp (tname, "vfork")))
722 flags |= ECF_RETURNS_TWICE;
724 else if (tname[0] == 'l' && tname[1] == 'o'
725 && ! strcmp (tname, "longjmp"))
726 flags |= ECF_LONGJMP;
728 else if ((tname[0] == 'f' && tname[1] == 'o'
729 && ! strcmp (tname, "fork"))
730 /* Linux specific: __clone. check NAME to insist on the
731 leading underscores, to avoid polluting the ISO / POSIX
732 namespace. */
733 || (name[0] == '_' && name[1] == '_'
734 && ! strcmp (tname, "clone"))
735 || (tname[0] == 'e' && tname[1] == 'x' && tname[2] == 'e'
736 && tname[3] == 'c' && (tname[4] == 'l' || tname[4] == 'v')
737 && (tname[5] == '\0'
738 || ((tname[5] == 'p' || tname[5] == 'e')
739 && tname[6] == '\0'))))
740 flags |= ECF_FORK_OR_EXEC;
742 /* Do not add any more malloc-like functions to this list,
743 instead mark them as malloc functions using the malloc attribute.
744 Note, realloc is not suitable for attribute malloc since
745 it may return the same address across multiple calls.
746 C++ operator new is not suitable because it is not required
747 to return a unique pointer; indeed, the standard placement new
748 just returns its argument. */
749 else if (TYPE_MODE (TREE_TYPE (TREE_TYPE (fndecl))) == Pmode
750 && (! strcmp (tname, "malloc")
751 || ! strcmp (tname, "calloc")
752 || ! strcmp (tname, "strdup")))
753 flags |= ECF_MALLOC;
755 return flags;
758 /* Return nonzero when tree represent call to longjmp. */
760 setjmp_call_p (fndecl)
761 tree fndecl;
763 return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
766 /* Detect flags (function attributes) from the function type node. */
767 static int
768 flags_from_decl_or_type (exp)
769 tree exp;
771 int flags = 0;
772 /* ??? We can't set IS_MALLOC for function types? */
773 if (DECL_P (exp))
775 /* The function exp may have the `malloc' attribute. */
776 if (DECL_P (exp) && DECL_IS_MALLOC (exp))
777 flags |= ECF_MALLOC;
779 /* The function exp may have the `pure' attribute. */
780 if (DECL_P (exp) && DECL_IS_PURE (exp))
781 flags |= ECF_PURE;
783 if (TREE_NOTHROW (exp))
784 flags |= ECF_NOTHROW;
787 if (TREE_READONLY (exp) && !TREE_THIS_VOLATILE (exp))
788 flags |= ECF_CONST;
790 if (TREE_THIS_VOLATILE (exp))
791 flags |= ECF_NORETURN;
793 return flags;
797 /* Precompute all register parameters as described by ARGS, storing values
798 into fields within the ARGS array.
800 NUM_ACTUALS indicates the total number elements in the ARGS array.
802 Set REG_PARM_SEEN if we encounter a register parameter. */
804 static void
805 precompute_register_parameters (num_actuals, args, reg_parm_seen)
806 int num_actuals;
807 struct arg_data *args;
808 int *reg_parm_seen;
810 int i;
812 *reg_parm_seen = 0;
814 for (i = 0; i < num_actuals; i++)
815 if (args[i].reg != 0 && ! args[i].pass_on_stack)
817 *reg_parm_seen = 1;
819 if (args[i].value == 0)
821 push_temp_slots ();
822 args[i].value = expand_expr (args[i].tree_value, NULL_RTX,
823 VOIDmode, 0);
824 preserve_temp_slots (args[i].value);
825 pop_temp_slots ();
827 /* ANSI doesn't require a sequence point here,
828 but PCC has one, so this will avoid some problems. */
829 emit_queue ();
832 /* If we are to promote the function arg to a wider mode,
833 do it now. */
835 if (args[i].mode != TYPE_MODE (TREE_TYPE (args[i].tree_value)))
836 args[i].value
837 = convert_modes (args[i].mode,
838 TYPE_MODE (TREE_TYPE (args[i].tree_value)),
839 args[i].value, args[i].unsignedp);
841 /* If the value is expensive, and we are inside an appropriately
842 short loop, put the value into a pseudo and then put the pseudo
843 into the hard reg.
845 For small register classes, also do this if this call uses
846 register parameters. This is to avoid reload conflicts while
847 loading the parameters registers. */
849 if ((! (GET_CODE (args[i].value) == REG
850 || (GET_CODE (args[i].value) == SUBREG
851 && GET_CODE (SUBREG_REG (args[i].value)) == REG)))
852 && args[i].mode != BLKmode
853 && rtx_cost (args[i].value, SET) > 2
854 && ((SMALL_REGISTER_CLASSES && *reg_parm_seen)
855 || preserve_subexpressions_p ()))
856 args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
860 #ifdef REG_PARM_STACK_SPACE
862 /* The argument list is the property of the called routine and it
863 may clobber it. If the fixed area has been used for previous
864 parameters, we must save and restore it. */
866 static rtx
867 save_fixed_argument_area (reg_parm_stack_space, argblock,
868 low_to_save, high_to_save)
869 int reg_parm_stack_space;
870 rtx argblock;
871 int *low_to_save;
872 int *high_to_save;
874 int i;
875 rtx save_area = NULL_RTX;
877 /* Compute the boundary of the that needs to be saved, if any. */
878 #ifdef ARGS_GROW_DOWNWARD
879 for (i = 0; i < reg_parm_stack_space + 1; i++)
880 #else
881 for (i = 0; i < reg_parm_stack_space; i++)
882 #endif
884 if (i >= highest_outgoing_arg_in_use
885 || stack_usage_map[i] == 0)
886 continue;
888 if (*low_to_save == -1)
889 *low_to_save = i;
891 *high_to_save = i;
894 if (*low_to_save >= 0)
896 int num_to_save = *high_to_save - *low_to_save + 1;
897 enum machine_mode save_mode
898 = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
899 rtx stack_area;
901 /* If we don't have the required alignment, must do this in BLKmode. */
902 if ((*low_to_save & (MIN (GET_MODE_SIZE (save_mode),
903 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
904 save_mode = BLKmode;
906 #ifdef ARGS_GROW_DOWNWARD
907 stack_area
908 = gen_rtx_MEM (save_mode,
909 memory_address (save_mode,
910 plus_constant (argblock,
911 - *high_to_save)));
912 #else
913 stack_area = gen_rtx_MEM (save_mode,
914 memory_address (save_mode,
915 plus_constant (argblock,
916 *low_to_save)));
917 #endif
918 if (save_mode == BLKmode)
920 save_area = assign_stack_temp (BLKmode, num_to_save, 0);
921 /* Cannot use emit_block_move here because it can be done by a
922 library call which in turn gets into this place again and deadly
923 infinite recursion happens. */
924 move_by_pieces (validize_mem (save_area), stack_area, num_to_save,
925 PARM_BOUNDARY);
927 else
929 save_area = gen_reg_rtx (save_mode);
930 emit_move_insn (save_area, stack_area);
933 return save_area;
936 static void
937 restore_fixed_argument_area (save_area, argblock, high_to_save, low_to_save)
938 rtx save_area;
939 rtx argblock;
940 int high_to_save;
941 int low_to_save;
943 enum machine_mode save_mode = GET_MODE (save_area);
944 #ifdef ARGS_GROW_DOWNWARD
945 rtx stack_area
946 = gen_rtx_MEM (save_mode,
947 memory_address (save_mode,
948 plus_constant (argblock,
949 - high_to_save)));
950 #else
951 rtx stack_area
952 = gen_rtx_MEM (save_mode,
953 memory_address (save_mode,
954 plus_constant (argblock,
955 low_to_save)));
956 #endif
958 if (save_mode != BLKmode)
959 emit_move_insn (stack_area, save_area);
960 else
961 /* Cannot use emit_block_move here because it can be done by a library
962 call which in turn gets into this place again and deadly infinite
963 recursion happens. */
964 move_by_pieces (stack_area, validize_mem (save_area),
965 high_to_save - low_to_save + 1, PARM_BOUNDARY);
967 #endif
969 /* If any elements in ARGS refer to parameters that are to be passed in
970 registers, but not in memory, and whose alignment does not permit a
971 direct copy into registers. Copy the values into a group of pseudos
972 which we will later copy into the appropriate hard registers.
974 Pseudos for each unaligned argument will be stored into the array
975 args[argnum].aligned_regs. The caller is responsible for deallocating
976 the aligned_regs array if it is nonzero. */
978 static void
979 store_unaligned_arguments_into_pseudos (args, num_actuals)
980 struct arg_data *args;
981 int num_actuals;
983 int i, j;
985 for (i = 0; i < num_actuals; i++)
986 if (args[i].reg != 0 && ! args[i].pass_on_stack
987 && args[i].mode == BLKmode
988 && (TYPE_ALIGN (TREE_TYPE (args[i].tree_value))
989 < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
991 int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
992 int big_endian_correction = 0;
994 args[i].n_aligned_regs
995 = args[i].partial ? args[i].partial
996 : (bytes + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
998 args[i].aligned_regs = (rtx *) xmalloc (sizeof (rtx)
999 * args[i].n_aligned_regs);
1001 /* Structures smaller than a word are aligned to the least
1002 significant byte (to the right). On a BYTES_BIG_ENDIAN machine,
1003 this means we must skip the empty high order bytes when
1004 calculating the bit offset. */
1005 if (BYTES_BIG_ENDIAN && bytes < UNITS_PER_WORD)
1006 big_endian_correction = (BITS_PER_WORD - (bytes * BITS_PER_UNIT));
1008 for (j = 0; j < args[i].n_aligned_regs; j++)
1010 rtx reg = gen_reg_rtx (word_mode);
1011 rtx word = operand_subword_force (args[i].value, j, BLKmode);
1012 int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
1013 int bitalign = TYPE_ALIGN (TREE_TYPE (args[i].tree_value));
1015 args[i].aligned_regs[j] = reg;
1017 /* There is no need to restrict this code to loading items
1018 in TYPE_ALIGN sized hunks. The bitfield instructions can
1019 load up entire word sized registers efficiently.
1021 ??? This may not be needed anymore.
1022 We use to emit a clobber here but that doesn't let later
1023 passes optimize the instructions we emit. By storing 0 into
1024 the register later passes know the first AND to zero out the
1025 bitfield being set in the register is unnecessary. The store
1026 of 0 will be deleted as will at least the first AND. */
1028 emit_move_insn (reg, const0_rtx);
1030 bytes -= bitsize / BITS_PER_UNIT;
1031 store_bit_field (reg, bitsize, big_endian_correction, word_mode,
1032 extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
1033 word_mode, word_mode, bitalign,
1034 BITS_PER_WORD),
1035 bitalign, BITS_PER_WORD);
1040 /* Fill in ARGS_SIZE and ARGS array based on the parameters found in
1041 ACTPARMS.
1043 NUM_ACTUALS is the total number of parameters.
1045 N_NAMED_ARGS is the total number of named arguments.
1047 FNDECL is the tree code for the target of this call (if known)
1049 ARGS_SO_FAR holds state needed by the target to know where to place
1050 the next argument.
1052 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
1053 for arguments which are passed in registers.
1055 OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
1056 and may be modified by this routine.
1058 OLD_PENDING_ADJ, MUST_PREALLOCATE and FLAGS are pointers to integer
1059 flags which may may be modified by this routine. */
1061 static void
1062 initialize_argument_information (num_actuals, args, args_size, n_named_args,
1063 actparms, fndecl, args_so_far,
1064 reg_parm_stack_space, old_stack_level,
1065 old_pending_adj, must_preallocate,
1066 ecf_flags)
1067 int num_actuals ATTRIBUTE_UNUSED;
1068 struct arg_data *args;
1069 struct args_size *args_size;
1070 int n_named_args ATTRIBUTE_UNUSED;
1071 tree actparms;
1072 tree fndecl;
1073 CUMULATIVE_ARGS *args_so_far;
1074 int reg_parm_stack_space;
1075 rtx *old_stack_level;
1076 int *old_pending_adj;
1077 int *must_preallocate;
1078 int *ecf_flags;
1080 /* 1 if scanning parms front to back, -1 if scanning back to front. */
1081 int inc;
1083 /* Count arg position in order args appear. */
1084 int argpos;
1086 struct args_size alignment_pad;
1087 int i;
1088 tree p;
1090 args_size->constant = 0;
1091 args_size->var = 0;
1093 /* In this loop, we consider args in the order they are written.
1094 We fill up ARGS from the front or from the back if necessary
1095 so that in any case the first arg to be pushed ends up at the front. */
1097 if (PUSH_ARGS_REVERSED)
1099 i = num_actuals - 1, inc = -1;
1100 /* In this case, must reverse order of args
1101 so that we compute and push the last arg first. */
1103 else
1105 i = 0, inc = 1;
1108 /* I counts args in order (to be) pushed; ARGPOS counts in order written. */
1109 for (p = actparms, argpos = 0; p; p = TREE_CHAIN (p), i += inc, argpos++)
1111 tree type = TREE_TYPE (TREE_VALUE (p));
1112 int unsignedp;
1113 enum machine_mode mode;
1115 args[i].tree_value = TREE_VALUE (p);
1117 /* Replace erroneous argument with constant zero. */
1118 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1119 args[i].tree_value = integer_zero_node, type = integer_type_node;
1121 /* If TYPE is a transparent union, pass things the way we would
1122 pass the first field of the union. We have already verified that
1123 the modes are the same. */
1124 if (TREE_CODE (type) == UNION_TYPE && TYPE_TRANSPARENT_UNION (type))
1125 type = TREE_TYPE (TYPE_FIELDS (type));
1127 /* Decide where to pass this arg.
1129 args[i].reg is nonzero if all or part is passed in registers.
1131 args[i].partial is nonzero if part but not all is passed in registers,
1132 and the exact value says how many words are passed in registers.
1134 args[i].pass_on_stack is nonzero if the argument must at least be
1135 computed on the stack. It may then be loaded back into registers
1136 if args[i].reg is nonzero.
1138 These decisions are driven by the FUNCTION_... macros and must agree
1139 with those made by function.c. */
1141 /* See if this argument should be passed by invisible reference. */
1142 if ((TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
1143 && contains_placeholder_p (TYPE_SIZE (type)))
1144 || TREE_ADDRESSABLE (type)
1145 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
1146 || FUNCTION_ARG_PASS_BY_REFERENCE (*args_so_far, TYPE_MODE (type),
1147 type, argpos < n_named_args)
1148 #endif
1151 /* If we're compiling a thunk, pass through invisible
1152 references instead of making a copy. */
1153 if (current_function_is_thunk
1154 #ifdef FUNCTION_ARG_CALLEE_COPIES
1155 || (FUNCTION_ARG_CALLEE_COPIES (*args_so_far, TYPE_MODE (type),
1156 type, argpos < n_named_args)
1157 /* If it's in a register, we must make a copy of it too. */
1158 /* ??? Is this a sufficient test? Is there a better one? */
1159 && !(TREE_CODE (args[i].tree_value) == VAR_DECL
1160 && REG_P (DECL_RTL (args[i].tree_value)))
1161 && ! TREE_ADDRESSABLE (type))
1162 #endif
1165 /* C++ uses a TARGET_EXPR to indicate that we want to make a
1166 new object from the argument. If we are passing by
1167 invisible reference, the callee will do that for us, so we
1168 can strip off the TARGET_EXPR. This is not always safe,
1169 but it is safe in the only case where this is a useful
1170 optimization; namely, when the argument is a plain object.
1171 In that case, the frontend is just asking the backend to
1172 make a bitwise copy of the argument. */
1174 if (TREE_CODE (args[i].tree_value) == TARGET_EXPR
1175 && (DECL_P (TREE_OPERAND (args[i].tree_value, 1)))
1176 && ! REG_P (DECL_RTL (TREE_OPERAND (args[i].tree_value, 1))))
1177 args[i].tree_value = TREE_OPERAND (args[i].tree_value, 1);
1179 args[i].tree_value = build1 (ADDR_EXPR,
1180 build_pointer_type (type),
1181 args[i].tree_value);
1182 type = build_pointer_type (type);
1184 else
1186 /* We make a copy of the object and pass the address to the
1187 function being called. */
1188 rtx copy;
1190 if (!COMPLETE_TYPE_P (type)
1191 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
1192 || (flag_stack_check && ! STACK_CHECK_BUILTIN
1193 && (0 < compare_tree_int (TYPE_SIZE_UNIT (type),
1194 STACK_CHECK_MAX_VAR_SIZE))))
1196 /* This is a variable-sized object. Make space on the stack
1197 for it. */
1198 rtx size_rtx = expr_size (TREE_VALUE (p));
1200 if (*old_stack_level == 0)
1202 emit_stack_save (SAVE_BLOCK, old_stack_level, NULL_RTX);
1203 *old_pending_adj = pending_stack_adjust;
1204 pending_stack_adjust = 0;
1207 copy = gen_rtx_MEM (BLKmode,
1208 allocate_dynamic_stack_space
1209 (size_rtx, NULL_RTX, TYPE_ALIGN (type)));
1210 set_mem_attributes (copy, type, 1);
1212 else
1213 copy = assign_temp (type, 0, 1, 0);
1215 store_expr (args[i].tree_value, copy, 0);
1216 *ecf_flags &= ~(ECF_CONST | ECF_PURE);
1218 args[i].tree_value = build1 (ADDR_EXPR,
1219 build_pointer_type (type),
1220 make_tree (type, copy));
1221 type = build_pointer_type (type);
1225 mode = TYPE_MODE (type);
1226 unsignedp = TREE_UNSIGNED (type);
1228 #ifdef PROMOTE_FUNCTION_ARGS
1229 mode = promote_mode (type, mode, &unsignedp, 1);
1230 #endif
1232 args[i].unsignedp = unsignedp;
1233 args[i].mode = mode;
1235 args[i].reg = FUNCTION_ARG (*args_so_far, mode, type,
1236 argpos < n_named_args);
1237 #ifdef FUNCTION_INCOMING_ARG
1238 /* If this is a sibling call and the machine has register windows, the
1239 register window has to be unwinded before calling the routine, so
1240 arguments have to go into the incoming registers. */
1241 args[i].tail_call_reg = FUNCTION_INCOMING_ARG (*args_so_far, mode, type,
1242 argpos < n_named_args);
1243 #else
1244 args[i].tail_call_reg = args[i].reg;
1245 #endif
1247 #ifdef FUNCTION_ARG_PARTIAL_NREGS
1248 if (args[i].reg)
1249 args[i].partial
1250 = FUNCTION_ARG_PARTIAL_NREGS (*args_so_far, mode, type,
1251 argpos < n_named_args);
1252 #endif
1254 args[i].pass_on_stack = MUST_PASS_IN_STACK (mode, type);
1256 /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
1257 it means that we are to pass this arg in the register(s) designated
1258 by the PARALLEL, but also to pass it in the stack. */
1259 if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
1260 && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
1261 args[i].pass_on_stack = 1;
1263 /* If this is an addressable type, we must preallocate the stack
1264 since we must evaluate the object into its final location.
1266 If this is to be passed in both registers and the stack, it is simpler
1267 to preallocate. */
1268 if (TREE_ADDRESSABLE (type)
1269 || (args[i].pass_on_stack && args[i].reg != 0))
1270 *must_preallocate = 1;
1272 /* If this is an addressable type, we cannot pre-evaluate it. Thus,
1273 we cannot consider this function call constant. */
1274 if (TREE_ADDRESSABLE (type))
1275 *ecf_flags &= ~(ECF_CONST | ECF_PURE);
1277 /* Compute the stack-size of this argument. */
1278 if (args[i].reg == 0 || args[i].partial != 0
1279 || reg_parm_stack_space > 0
1280 || args[i].pass_on_stack)
1281 locate_and_pad_parm (mode, type,
1282 #ifdef STACK_PARMS_IN_REG_PARM_AREA
1284 #else
1285 args[i].reg != 0,
1286 #endif
1287 fndecl, args_size, &args[i].offset,
1288 &args[i].size, &alignment_pad);
1290 #ifndef ARGS_GROW_DOWNWARD
1291 args[i].slot_offset = *args_size;
1292 #endif
1294 args[i].alignment_pad = alignment_pad;
1296 /* If a part of the arg was put into registers,
1297 don't include that part in the amount pushed. */
1298 if (reg_parm_stack_space == 0 && ! args[i].pass_on_stack)
1299 args[i].size.constant -= ((args[i].partial * UNITS_PER_WORD)
1300 / (PARM_BOUNDARY / BITS_PER_UNIT)
1301 * (PARM_BOUNDARY / BITS_PER_UNIT));
1303 /* Update ARGS_SIZE, the total stack space for args so far. */
1305 args_size->constant += args[i].size.constant;
1306 if (args[i].size.var)
1308 ADD_PARM_SIZE (*args_size, args[i].size.var);
1311 /* Since the slot offset points to the bottom of the slot,
1312 we must record it after incrementing if the args grow down. */
1313 #ifdef ARGS_GROW_DOWNWARD
1314 args[i].slot_offset = *args_size;
1316 args[i].slot_offset.constant = -args_size->constant;
1317 if (args_size->var)
1318 SUB_PARM_SIZE (args[i].slot_offset, args_size->var);
1319 #endif
1321 /* Increment ARGS_SO_FAR, which has info about which arg-registers
1322 have been used, etc. */
1324 FUNCTION_ARG_ADVANCE (*args_so_far, TYPE_MODE (type), type,
1325 argpos < n_named_args);
1329 /* Update ARGS_SIZE to contain the total size for the argument block.
1330 Return the original constant component of the argument block's size.
1332 REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
1333 for arguments passed in registers. */
1335 static int
1336 compute_argument_block_size (reg_parm_stack_space, args_size,
1337 preferred_stack_boundary)
1338 int reg_parm_stack_space;
1339 struct args_size *args_size;
1340 int preferred_stack_boundary ATTRIBUTE_UNUSED;
1342 int unadjusted_args_size = args_size->constant;
1344 /* For accumulate outgoing args mode we don't need to align, since the frame
1345 will be already aligned. Align to STACK_BOUNDARY in order to prevent
1346 backends from generating missaligned frame sizes. */
1347 #ifdef STACK_BOUNDARY
1348 if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
1349 preferred_stack_boundary = STACK_BOUNDARY;
1350 #endif
1352 /* Compute the actual size of the argument block required. The variable
1353 and constant sizes must be combined, the size may have to be rounded,
1354 and there may be a minimum required size. */
1356 if (args_size->var)
1358 args_size->var = ARGS_SIZE_TREE (*args_size);
1359 args_size->constant = 0;
1361 #ifdef PREFERRED_STACK_BOUNDARY
1362 preferred_stack_boundary /= BITS_PER_UNIT;
1363 if (preferred_stack_boundary > 1)
1365 /* We don't handle this case yet. To handle it correctly we have
1366 to add the delta, round and substract the delta.
1367 Currently no machine description requires this support. */
1368 if (stack_pointer_delta & (preferred_stack_boundary - 1))
1369 abort();
1370 args_size->var = round_up (args_size->var, preferred_stack_boundary);
1372 #endif
1374 if (reg_parm_stack_space > 0)
1376 args_size->var
1377 = size_binop (MAX_EXPR, args_size->var,
1378 ssize_int (reg_parm_stack_space));
1380 #ifndef OUTGOING_REG_PARM_STACK_SPACE
1381 /* The area corresponding to register parameters is not to count in
1382 the size of the block we need. So make the adjustment. */
1383 args_size->var
1384 = size_binop (MINUS_EXPR, args_size->var,
1385 ssize_int (reg_parm_stack_space));
1386 #endif
1389 else
1391 #ifdef PREFERRED_STACK_BOUNDARY
1392 preferred_stack_boundary /= BITS_PER_UNIT;
1393 if (preferred_stack_boundary < 1)
1394 preferred_stack_boundary = 1;
1395 args_size->constant = (((args_size->constant
1396 + stack_pointer_delta
1397 + preferred_stack_boundary - 1)
1398 / preferred_stack_boundary
1399 * preferred_stack_boundary)
1400 - stack_pointer_delta);
1401 #endif
1403 args_size->constant = MAX (args_size->constant,
1404 reg_parm_stack_space);
1406 #ifdef MAYBE_REG_PARM_STACK_SPACE
1407 if (reg_parm_stack_space == 0)
1408 args_size->constant = 0;
1409 #endif
1411 #ifndef OUTGOING_REG_PARM_STACK_SPACE
1412 args_size->constant -= reg_parm_stack_space;
1413 #endif
1415 return unadjusted_args_size;
1418 /* Precompute parameters as needed for a function call.
1420 FLAGS is mask of ECF_* constants.
1422 NUM_ACTUALS is the number of arguments.
1424 ARGS is an array containing information for each argument; this routine
1425 fills in the INITIAL_VALUE and VALUE fields for each precomputed argument.
1428 static void
1429 precompute_arguments (flags, num_actuals, args)
1430 int flags;
1431 int num_actuals;
1432 struct arg_data *args;
1434 int i;
1436 /* If this function call is cse'able, precompute all the parameters.
1437 Note that if the parameter is constructed into a temporary, this will
1438 cause an additional copy because the parameter will be constructed
1439 into a temporary location and then copied into the outgoing arguments.
1440 If a parameter contains a call to alloca and this function uses the
1441 stack, precompute the parameter. */
1443 /* If we preallocated the stack space, and some arguments must be passed
1444 on the stack, then we must precompute any parameter which contains a
1445 function call which will store arguments on the stack.
1446 Otherwise, evaluating the parameter may clobber previous parameters
1447 which have already been stored into the stack. (we have code to avoid
1448 such case by saving the ougoing stack arguments, but it results in
1449 worse code) */
1451 for (i = 0; i < num_actuals; i++)
1452 if ((flags & (ECF_CONST | ECF_PURE))
1453 || calls_function (args[i].tree_value, !ACCUMULATE_OUTGOING_ARGS))
1455 /* If this is an addressable type, we cannot pre-evaluate it. */
1456 if (TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value)))
1457 abort ();
1459 push_temp_slots ();
1461 args[i].value
1462 = expand_expr (args[i].tree_value, NULL_RTX, VOIDmode, 0);
1464 preserve_temp_slots (args[i].value);
1465 pop_temp_slots ();
1467 /* ANSI doesn't require a sequence point here,
1468 but PCC has one, so this will avoid some problems. */
1469 emit_queue ();
1471 args[i].initial_value = args[i].value
1472 = protect_from_queue (args[i].value, 0);
1474 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) != args[i].mode)
1476 args[i].value
1477 = convert_modes (args[i].mode,
1478 TYPE_MODE (TREE_TYPE (args[i].tree_value)),
1479 args[i].value, args[i].unsignedp);
1480 #ifdef PROMOTE_FOR_CALL_ONLY
1481 /* CSE will replace this only if it contains args[i].value
1482 pseudo, so convert it down to the declared mode using
1483 a SUBREG. */
1484 if (GET_CODE (args[i].value) == REG
1485 && GET_MODE_CLASS (args[i].mode) == MODE_INT)
1487 args[i].initial_value
1488 = gen_rtx_SUBREG (TYPE_MODE (TREE_TYPE (args[i].tree_value)),
1489 args[i].value, 0);
1490 SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1491 SUBREG_PROMOTED_UNSIGNED_P (args[i].initial_value)
1492 = args[i].unsignedp;
1494 #endif
1499 /* Given the current state of MUST_PREALLOCATE and information about
1500 arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
1501 compute and return the final value for MUST_PREALLOCATE. */
1503 static int
1504 finalize_must_preallocate (must_preallocate, num_actuals, args, args_size)
1505 int must_preallocate;
1506 int num_actuals;
1507 struct arg_data *args;
1508 struct args_size *args_size;
1510 /* See if we have or want to preallocate stack space.
1512 If we would have to push a partially-in-regs parm
1513 before other stack parms, preallocate stack space instead.
1515 If the size of some parm is not a multiple of the required stack
1516 alignment, we must preallocate.
1518 If the total size of arguments that would otherwise create a copy in
1519 a temporary (such as a CALL) is more than half the total argument list
1520 size, preallocation is faster.
1522 Another reason to preallocate is if we have a machine (like the m88k)
1523 where stack alignment is required to be maintained between every
1524 pair of insns, not just when the call is made. However, we assume here
1525 that such machines either do not have push insns (and hence preallocation
1526 would occur anyway) or the problem is taken care of with
1527 PUSH_ROUNDING. */
1529 if (! must_preallocate)
1531 int partial_seen = 0;
1532 int copy_to_evaluate_size = 0;
1533 int i;
1535 for (i = 0; i < num_actuals && ! must_preallocate; i++)
1537 if (args[i].partial > 0 && ! args[i].pass_on_stack)
1538 partial_seen = 1;
1539 else if (partial_seen && args[i].reg == 0)
1540 must_preallocate = 1;
1542 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1543 && (TREE_CODE (args[i].tree_value) == CALL_EXPR
1544 || TREE_CODE (args[i].tree_value) == TARGET_EXPR
1545 || TREE_CODE (args[i].tree_value) == COND_EXPR
1546 || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
1547 copy_to_evaluate_size
1548 += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1551 if (copy_to_evaluate_size * 2 >= args_size->constant
1552 && args_size->constant > 0)
1553 must_preallocate = 1;
1555 return must_preallocate;
1558 /* If we preallocated stack space, compute the address of each argument
1559 and store it into the ARGS array.
1561 We need not ensure it is a valid memory address here; it will be
1562 validized when it is used.
1564 ARGBLOCK is an rtx for the address of the outgoing arguments. */
1566 static void
1567 compute_argument_addresses (args, argblock, num_actuals)
1568 struct arg_data *args;
1569 rtx argblock;
1570 int num_actuals;
1572 if (argblock)
1574 rtx arg_reg = argblock;
1575 int i, arg_offset = 0;
1577 if (GET_CODE (argblock) == PLUS)
1578 arg_reg = XEXP (argblock, 0), arg_offset = INTVAL (XEXP (argblock, 1));
1580 for (i = 0; i < num_actuals; i++)
1582 rtx offset = ARGS_SIZE_RTX (args[i].offset);
1583 rtx slot_offset = ARGS_SIZE_RTX (args[i].slot_offset);
1584 rtx addr;
1586 /* Skip this parm if it will not be passed on the stack. */
1587 if (! args[i].pass_on_stack && args[i].reg != 0)
1588 continue;
1590 if (GET_CODE (offset) == CONST_INT)
1591 addr = plus_constant (arg_reg, INTVAL (offset));
1592 else
1593 addr = gen_rtx_PLUS (Pmode, arg_reg, offset);
1595 addr = plus_constant (addr, arg_offset);
1596 args[i].stack = gen_rtx_MEM (args[i].mode, addr);
1597 set_mem_attributes (args[i].stack,
1598 TREE_TYPE (args[i].tree_value), 1);
1600 if (GET_CODE (slot_offset) == CONST_INT)
1601 addr = plus_constant (arg_reg, INTVAL (slot_offset));
1602 else
1603 addr = gen_rtx_PLUS (Pmode, arg_reg, slot_offset);
1605 addr = plus_constant (addr, arg_offset);
1606 args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
1607 set_mem_attributes (args[i].stack_slot,
1608 TREE_TYPE (args[i].tree_value), 1);
1610 /* Function incoming arguments may overlap with sibling call
1611 outgoing arguments and we cannot allow reordering of reads
1612 from function arguments with stores to outgoing arguments
1613 of sibling calls. */
1614 MEM_ALIAS_SET (args[i].stack) = 0;
1615 MEM_ALIAS_SET (args[i].stack_slot) = 0;
1620 /* Given a FNDECL and EXP, return an rtx suitable for use as a target address
1621 in a call instruction.
1623 FNDECL is the tree node for the target function. For an indirect call
1624 FNDECL will be NULL_TREE.
1626 EXP is the CALL_EXPR for this call. */
1628 static rtx
1629 rtx_for_function_call (fndecl, exp)
1630 tree fndecl;
1631 tree exp;
1633 rtx funexp;
1635 /* Get the function to call, in the form of RTL. */
1636 if (fndecl)
1638 /* If this is the first use of the function, see if we need to
1639 make an external definition for it. */
1640 if (! TREE_USED (fndecl))
1642 assemble_external (fndecl);
1643 TREE_USED (fndecl) = 1;
1646 /* Get a SYMBOL_REF rtx for the function address. */
1647 funexp = XEXP (DECL_RTL (fndecl), 0);
1649 else
1650 /* Generate an rtx (probably a pseudo-register) for the address. */
1652 rtx funaddr;
1653 push_temp_slots ();
1654 funaddr = funexp =
1655 expand_expr (TREE_OPERAND (exp, 0), NULL_RTX, VOIDmode, 0);
1656 pop_temp_slots (); /* FUNEXP can't be BLKmode */
1658 /* Check the function is executable. */
1659 if (current_function_check_memory_usage)
1661 #ifdef POINTERS_EXTEND_UNSIGNED
1662 /* It might be OK to convert funexp in place, but there's
1663 a lot going on between here and when it happens naturally
1664 that this seems safer. */
1665 funaddr = convert_memory_address (Pmode, funexp);
1666 #endif
1667 emit_library_call (chkr_check_exec_libfunc, 1,
1668 VOIDmode, 1,
1669 funaddr, Pmode);
1671 emit_queue ();
1673 return funexp;
1676 /* Do the register loads required for any wholly-register parms or any
1677 parms which are passed both on the stack and in a register. Their
1678 expressions were already evaluated.
1680 Mark all register-parms as living through the call, putting these USE
1681 insns in the CALL_INSN_FUNCTION_USAGE field. */
1683 static void
1684 load_register_parameters (args, num_actuals, call_fusage, flags)
1685 struct arg_data *args;
1686 int num_actuals;
1687 rtx *call_fusage;
1688 int flags;
1690 int i, j;
1692 #ifdef LOAD_ARGS_REVERSED
1693 for (i = num_actuals - 1; i >= 0; i--)
1694 #else
1695 for (i = 0; i < num_actuals; i++)
1696 #endif
1698 rtx reg = ((flags & ECF_SIBCALL)
1699 ? args[i].tail_call_reg : args[i].reg);
1700 int partial = args[i].partial;
1701 int nregs;
1703 if (reg)
1705 /* Set to non-negative if must move a word at a time, even if just
1706 one word (e.g, partial == 1 && mode == DFmode). Set to -1 if
1707 we just use a normal move insn. This value can be zero if the
1708 argument is a zero size structure with no fields. */
1709 nregs = (partial ? partial
1710 : (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1711 ? ((int_size_in_bytes (TREE_TYPE (args[i].tree_value))
1712 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
1713 : -1));
1715 /* Handle calls that pass values in multiple non-contiguous
1716 locations. The Irix 6 ABI has examples of this. */
1718 if (GET_CODE (reg) == PARALLEL)
1719 emit_group_load (reg, args[i].value,
1720 int_size_in_bytes (TREE_TYPE (args[i].tree_value)),
1721 TYPE_ALIGN (TREE_TYPE (args[i].tree_value)));
1723 /* If simple case, just do move. If normal partial, store_one_arg
1724 has already loaded the register for us. In all other cases,
1725 load the register(s) from memory. */
1727 else if (nregs == -1)
1728 emit_move_insn (reg, args[i].value);
1730 /* If we have pre-computed the values to put in the registers in
1731 the case of non-aligned structures, copy them in now. */
1733 else if (args[i].n_aligned_regs != 0)
1734 for (j = 0; j < args[i].n_aligned_regs; j++)
1735 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
1736 args[i].aligned_regs[j]);
1738 else if (partial == 0 || args[i].pass_on_stack)
1739 move_block_to_reg (REGNO (reg),
1740 validize_mem (args[i].value), nregs,
1741 args[i].mode);
1743 /* Handle calls that pass values in multiple non-contiguous
1744 locations. The Irix 6 ABI has examples of this. */
1745 if (GET_CODE (reg) == PARALLEL)
1746 use_group_regs (call_fusage, reg);
1747 else if (nregs == -1)
1748 use_reg (call_fusage, reg);
1749 else
1750 use_regs (call_fusage, REGNO (reg), nregs == 0 ? 1 : nregs);
1755 /* Try to integrate function. See expand_inline_function for documentation
1756 about the parameters. */
1758 static rtx
1759 try_to_integrate (fndecl, actparms, target, ignore, type, structure_value_addr)
1760 tree fndecl;
1761 tree actparms;
1762 rtx target;
1763 int ignore;
1764 tree type;
1765 rtx structure_value_addr;
1767 rtx temp;
1768 rtx before_call;
1769 int i;
1770 rtx old_stack_level = 0;
1771 int reg_parm_stack_space = 0;
1773 #ifdef REG_PARM_STACK_SPACE
1774 #ifdef MAYBE_REG_PARM_STACK_SPACE
1775 reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
1776 #else
1777 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
1778 #endif
1779 #endif
1781 before_call = get_last_insn ();
1783 timevar_push (TV_INTEGRATION);
1785 temp = expand_inline_function (fndecl, actparms, target,
1786 ignore, type,
1787 structure_value_addr);
1789 timevar_pop (TV_INTEGRATION);
1791 /* If inlining succeeded, return. */
1792 if (temp != (rtx) (HOST_WIDE_INT) - 1)
1794 if (ACCUMULATE_OUTGOING_ARGS)
1796 /* If the outgoing argument list must be preserved, push
1797 the stack before executing the inlined function if it
1798 makes any calls. */
1800 for (i = reg_parm_stack_space - 1; i >= 0; i--)
1801 if (i < highest_outgoing_arg_in_use && stack_usage_map[i] != 0)
1802 break;
1804 if (stack_arg_under_construction || i >= 0)
1806 rtx first_insn
1807 = before_call ? NEXT_INSN (before_call) : get_insns ();
1808 rtx insn = NULL_RTX, seq;
1810 /* Look for a call in the inline function code.
1811 If DECL_SAVED_INSNS (fndecl)->outgoing_args_size is
1812 nonzero then there is a call and it is not necessary
1813 to scan the insns. */
1815 if (DECL_SAVED_INSNS (fndecl)->outgoing_args_size == 0)
1816 for (insn = first_insn; insn; insn = NEXT_INSN (insn))
1817 if (GET_CODE (insn) == CALL_INSN)
1818 break;
1820 if (insn)
1822 /* Reserve enough stack space so that the largest
1823 argument list of any function call in the inline
1824 function does not overlap the argument list being
1825 evaluated. This is usually an overestimate because
1826 allocate_dynamic_stack_space reserves space for an
1827 outgoing argument list in addition to the requested
1828 space, but there is no way to ask for stack space such
1829 that an argument list of a certain length can be
1830 safely constructed.
1832 Add the stack space reserved for register arguments, if
1833 any, in the inline function. What is really needed is the
1834 largest value of reg_parm_stack_space in the inline
1835 function, but that is not available. Using the current
1836 value of reg_parm_stack_space is wrong, but gives
1837 correct results on all supported machines. */
1839 int adjust = (DECL_SAVED_INSNS (fndecl)->outgoing_args_size
1840 + reg_parm_stack_space);
1842 start_sequence ();
1843 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1844 allocate_dynamic_stack_space (GEN_INT (adjust),
1845 NULL_RTX, BITS_PER_UNIT);
1846 seq = get_insns ();
1847 end_sequence ();
1848 emit_insns_before (seq, first_insn);
1849 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1854 /* If the result is equivalent to TARGET, return TARGET to simplify
1855 checks in store_expr. They can be equivalent but not equal in the
1856 case of a function that returns BLKmode. */
1857 if (temp != target && rtx_equal_p (temp, target))
1858 return target;
1859 return temp;
1862 /* If inlining failed, mark FNDECL as needing to be compiled
1863 separately after all. If function was declared inline,
1864 give a warning. */
1865 if (DECL_INLINE (fndecl) && warn_inline && !flag_no_inline
1866 && optimize > 0 && !TREE_ADDRESSABLE (fndecl))
1868 warning_with_decl (fndecl, "inlining failed in call to `%s'");
1869 warning ("called from here");
1871 mark_addressable (fndecl);
1872 return (rtx) (HOST_WIDE_INT) - 1;
1875 /* We need to pop PENDING_STACK_ADJUST bytes. But, if the arguments
1876 wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
1877 bytes, then we would need to push some additional bytes to pad the
1878 arguments. So, we compute an adjust to the stack pointer for an
1879 amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
1880 bytes. Then, when the arguments are pushed the stack will be perfectly
1881 aligned. ARGS_SIZE->CONSTANT is set to the number of bytes that should
1882 be popped after the call. Returns the adjustment. */
1884 static int
1885 combine_pending_stack_adjustment_and_call (unadjusted_args_size,
1886 args_size,
1887 preferred_unit_stack_boundary)
1888 int unadjusted_args_size;
1889 struct args_size *args_size;
1890 int preferred_unit_stack_boundary;
1892 /* The number of bytes to pop so that the stack will be
1893 under-aligned by UNADJUSTED_ARGS_SIZE bytes. */
1894 HOST_WIDE_INT adjustment;
1895 /* The alignment of the stack after the arguments are pushed, if we
1896 just pushed the arguments without adjust the stack here. */
1897 HOST_WIDE_INT unadjusted_alignment;
1899 unadjusted_alignment
1900 = ((stack_pointer_delta + unadjusted_args_size)
1901 % preferred_unit_stack_boundary);
1903 /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
1904 as possible -- leaving just enough left to cancel out the
1905 UNADJUSTED_ALIGNMENT. In other words, we want to ensure that the
1906 PENDING_STACK_ADJUST is non-negative, and congruent to
1907 -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY. */
1909 /* Begin by trying to pop all the bytes. */
1910 unadjusted_alignment
1911 = (unadjusted_alignment
1912 - (pending_stack_adjust % preferred_unit_stack_boundary));
1913 adjustment = pending_stack_adjust;
1914 /* Push enough additional bytes that the stack will be aligned
1915 after the arguments are pushed. */
1916 if (unadjusted_alignment >= 0)
1917 adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
1918 else
1919 adjustment += unadjusted_alignment;
1921 /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
1922 bytes after the call. The right number is the entire
1923 PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
1924 by the arguments in the first place. */
1925 args_size->constant
1926 = pending_stack_adjust - adjustment + unadjusted_args_size;
1928 return adjustment;
1931 /* Scan X expression if it does not dereference any argument slots
1932 we already clobbered by tail call arguments (as noted in stored_args_map
1933 bitmap).
1934 Return non-zero if X expression dereferences such argument slots,
1935 zero otherwise. */
1937 static int
1938 check_sibcall_argument_overlap_1 (x)
1939 rtx x;
1941 RTX_CODE code;
1942 int i, j;
1943 unsigned int k;
1944 const char *fmt;
1946 if (x == NULL_RTX)
1947 return 0;
1949 code = GET_CODE (x);
1951 if (code == MEM)
1953 if (XEXP (x, 0) == current_function_internal_arg_pointer)
1954 i = 0;
1955 else if (GET_CODE (XEXP (x, 0)) == PLUS &&
1956 XEXP (XEXP (x, 0), 0) ==
1957 current_function_internal_arg_pointer &&
1958 GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
1959 i = INTVAL (XEXP (XEXP (x, 0), 1));
1960 else
1961 return 0;
1963 for (k = 0; k < GET_MODE_SIZE (GET_MODE (x)); k++)
1964 if (i + k < stored_args_map->n_bits
1965 && TEST_BIT (stored_args_map, i + k))
1966 return 1;
1968 return 0;
1971 /* Scan all subexpressions. */
1972 fmt = GET_RTX_FORMAT (code);
1973 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
1975 if (*fmt == 'e')
1977 if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
1978 return 1;
1980 else if (*fmt == 'E')
1982 for (j = 0; j < XVECLEN (x, i); j++)
1983 if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
1984 return 1;
1987 return 0;
1991 /* Scan sequence after INSN if it does not dereference any argument slots
1992 we already clobbered by tail call arguments (as noted in stored_args_map
1993 bitmap). Add stack slots for ARG to stored_args_map bitmap afterwards.
1994 Return non-zero if sequence after INSN dereferences such argument slots,
1995 zero otherwise. */
1997 static int
1998 check_sibcall_argument_overlap (insn, arg)
1999 rtx insn;
2000 struct arg_data *arg;
2002 int low, high;
2004 if (insn == NULL_RTX)
2005 insn = get_insns ();
2006 else
2007 insn = NEXT_INSN (insn);
2009 for (; insn; insn = NEXT_INSN (insn))
2010 if (INSN_P (insn) &&
2011 check_sibcall_argument_overlap_1 (PATTERN (insn)))
2012 break;
2014 low = arg->offset.constant;
2015 for (high = low + arg->size.constant; low < high; low++)
2016 SET_BIT (stored_args_map, low);
2017 return insn != NULL_RTX;
2020 /* Generate all the code for a function call
2021 and return an rtx for its value.
2022 Store the value in TARGET (specified as an rtx) if convenient.
2023 If the value is stored in TARGET then TARGET is returned.
2024 If IGNORE is nonzero, then we ignore the value of the function call. */
2027 expand_call (exp, target, ignore)
2028 tree exp;
2029 rtx target;
2030 int ignore;
2032 /* Nonzero if we are currently expanding a call. */
2033 static int currently_expanding_call = 0;
2035 /* List of actual parameters. */
2036 tree actparms = TREE_OPERAND (exp, 1);
2037 /* RTX for the function to be called. */
2038 rtx funexp;
2039 /* Sequence of insns to perform a tail recursive "call". */
2040 rtx tail_recursion_insns = NULL_RTX;
2041 /* Sequence of insns to perform a normal "call". */
2042 rtx normal_call_insns = NULL_RTX;
2043 /* Sequence of insns to perform a tail recursive "call". */
2044 rtx tail_call_insns = NULL_RTX;
2045 /* Data type of the function. */
2046 tree funtype;
2047 /* Declaration of the function being called,
2048 or 0 if the function is computed (not known by name). */
2049 tree fndecl = 0;
2050 char *name = 0;
2051 rtx insn;
2052 int try_tail_call = 1;
2053 int try_tail_recursion = 1;
2054 int pass;
2056 /* Register in which non-BLKmode value will be returned,
2057 or 0 if no value or if value is BLKmode. */
2058 rtx valreg;
2059 /* Address where we should return a BLKmode value;
2060 0 if value not BLKmode. */
2061 rtx structure_value_addr = 0;
2062 /* Nonzero if that address is being passed by treating it as
2063 an extra, implicit first parameter. Otherwise,
2064 it is passed by being copied directly into struct_value_rtx. */
2065 int structure_value_addr_parm = 0;
2066 /* Size of aggregate value wanted, or zero if none wanted
2067 or if we are using the non-reentrant PCC calling convention
2068 or expecting the value in registers. */
2069 HOST_WIDE_INT struct_value_size = 0;
2070 /* Nonzero if called function returns an aggregate in memory PCC style,
2071 by returning the address of where to find it. */
2072 int pcc_struct_value = 0;
2074 /* Number of actual parameters in this call, including struct value addr. */
2075 int num_actuals;
2076 /* Number of named args. Args after this are anonymous ones
2077 and they must all go on the stack. */
2078 int n_named_args;
2080 /* Vector of information about each argument.
2081 Arguments are numbered in the order they will be pushed,
2082 not the order they are written. */
2083 struct arg_data *args;
2085 /* Total size in bytes of all the stack-parms scanned so far. */
2086 struct args_size args_size;
2087 struct args_size adjusted_args_size;
2088 /* Size of arguments before any adjustments (such as rounding). */
2089 int unadjusted_args_size;
2090 /* Data on reg parms scanned so far. */
2091 CUMULATIVE_ARGS args_so_far;
2092 /* Nonzero if a reg parm has been scanned. */
2093 int reg_parm_seen;
2094 /* Nonzero if this is an indirect function call. */
2096 /* Nonzero if we must avoid push-insns in the args for this call.
2097 If stack space is allocated for register parameters, but not by the
2098 caller, then it is preallocated in the fixed part of the stack frame.
2099 So the entire argument block must then be preallocated (i.e., we
2100 ignore PUSH_ROUNDING in that case). */
2102 int must_preallocate = !PUSH_ARGS;
2104 /* Size of the stack reserved for parameter registers. */
2105 int reg_parm_stack_space = 0;
2107 /* Address of space preallocated for stack parms
2108 (on machines that lack push insns), or 0 if space not preallocated. */
2109 rtx argblock = 0;
2111 /* Mask of ECF_ flags. */
2112 int flags = 0;
2113 /* Nonzero if this is a call to an inline function. */
2114 int is_integrable = 0;
2115 #ifdef REG_PARM_STACK_SPACE
2116 /* Define the boundary of the register parm stack space that needs to be
2117 save, if any. */
2118 int low_to_save = -1, high_to_save;
2119 rtx save_area = 0; /* Place that it is saved */
2120 #endif
2122 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
2123 char *initial_stack_usage_map = stack_usage_map;
2124 int old_stack_arg_under_construction = 0;
2126 rtx old_stack_level = 0;
2127 int old_pending_adj = 0;
2128 int old_inhibit_defer_pop = inhibit_defer_pop;
2129 int old_stack_allocated;
2130 rtx call_fusage;
2131 register tree p;
2132 register int i;
2133 /* The alignment of the stack, in bits. */
2134 HOST_WIDE_INT preferred_stack_boundary;
2135 /* The alignment of the stack, in bytes. */
2136 HOST_WIDE_INT preferred_unit_stack_boundary;
2138 /* The value of the function call can be put in a hard register. But
2139 if -fcheck-memory-usage, code which invokes functions (and thus
2140 damages some hard registers) can be inserted before using the value.
2141 So, target is always a pseudo-register in that case. */
2142 if (current_function_check_memory_usage)
2143 target = 0;
2145 /* See if this is "nothrow" function call. */
2146 if (TREE_NOTHROW (exp))
2147 flags |= ECF_NOTHROW;
2149 /* See if we can find a DECL-node for the actual function.
2150 As a result, decide whether this is a call to an integrable function. */
2152 fndecl = get_callee_fndecl (exp);
2153 if (fndecl)
2155 if (!flag_no_inline
2156 && fndecl != current_function_decl
2157 && DECL_INLINE (fndecl)
2158 && DECL_SAVED_INSNS (fndecl)
2159 && DECL_SAVED_INSNS (fndecl)->inlinable)
2160 is_integrable = 1;
2161 else if (! TREE_ADDRESSABLE (fndecl))
2163 /* In case this function later becomes inlinable,
2164 record that there was already a non-inline call to it.
2166 Use abstraction instead of setting TREE_ADDRESSABLE
2167 directly. */
2168 if (DECL_INLINE (fndecl) && warn_inline && !flag_no_inline
2169 && optimize > 0)
2171 warning_with_decl (fndecl, "can't inline call to `%s'");
2172 warning ("called from here");
2174 mark_addressable (fndecl);
2177 flags |= flags_from_decl_or_type (fndecl);
2180 /* If we don't have specific function to call, see if we have a
2181 attributes set in the type. */
2182 else
2184 p = TREE_OPERAND (exp, 0);
2185 flags |= flags_from_decl_or_type (TREE_TYPE (TREE_TYPE (p)));
2188 #ifdef REG_PARM_STACK_SPACE
2189 #ifdef MAYBE_REG_PARM_STACK_SPACE
2190 reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
2191 #else
2192 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
2193 #endif
2194 #endif
2196 #ifndef OUTGOING_REG_PARM_STACK_SPACE
2197 if (reg_parm_stack_space > 0 && PUSH_ARGS)
2198 must_preallocate = 1;
2199 #endif
2201 /* Warn if this value is an aggregate type,
2202 regardless of which calling convention we are using for it. */
2203 if (warn_aggregate_return && AGGREGATE_TYPE_P (TREE_TYPE (exp)))
2204 warning ("function call has aggregate value");
2206 /* Set up a place to return a structure. */
2208 /* Cater to broken compilers. */
2209 if (aggregate_value_p (exp))
2211 /* This call returns a big structure. */
2212 flags &= ~(ECF_CONST | ECF_PURE);
2214 #ifdef PCC_STATIC_STRUCT_RETURN
2216 pcc_struct_value = 1;
2217 /* Easier than making that case work right. */
2218 if (is_integrable)
2220 /* In case this is a static function, note that it has been
2221 used. */
2222 if (! TREE_ADDRESSABLE (fndecl))
2223 mark_addressable (fndecl);
2224 is_integrable = 0;
2227 #else /* not PCC_STATIC_STRUCT_RETURN */
2229 struct_value_size = int_size_in_bytes (TREE_TYPE (exp));
2231 if (target && GET_CODE (target) == MEM)
2232 structure_value_addr = XEXP (target, 0);
2233 else
2235 /* Assign a temporary to hold the value. */
2236 tree d;
2238 /* For variable-sized objects, we must be called with a target
2239 specified. If we were to allocate space on the stack here,
2240 we would have no way of knowing when to free it. */
2242 if (struct_value_size < 0)
2243 abort ();
2245 /* This DECL is just something to feed to mark_addressable;
2246 it doesn't get pushed. */
2247 d = build_decl (VAR_DECL, NULL_TREE, TREE_TYPE (exp));
2248 DECL_RTL (d) = assign_temp (TREE_TYPE (exp), 1, 0, 1);
2249 mark_addressable (d);
2250 mark_temp_addr_taken (DECL_RTL (d));
2251 structure_value_addr = XEXP (DECL_RTL (d), 0);
2252 TREE_USED (d) = 1;
2253 target = 0;
2256 #endif /* not PCC_STATIC_STRUCT_RETURN */
2259 /* If called function is inline, try to integrate it. */
2261 if (is_integrable)
2263 rtx temp = try_to_integrate (fndecl, actparms, target,
2264 ignore, TREE_TYPE (exp),
2265 structure_value_addr);
2266 if (temp != (rtx) (HOST_WIDE_INT) - 1)
2267 return temp;
2270 if (fndecl && DECL_NAME (fndecl))
2271 name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
2273 /* Figure out the amount to which the stack should be aligned. */
2274 #ifdef PREFERRED_STACK_BOUNDARY
2275 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
2276 #else
2277 preferred_stack_boundary = STACK_BOUNDARY;
2278 #endif
2280 /* Operand 0 is a pointer-to-function; get the type of the function. */
2281 funtype = TREE_TYPE (TREE_OPERAND (exp, 0));
2282 if (! POINTER_TYPE_P (funtype))
2283 abort ();
2284 funtype = TREE_TYPE (funtype);
2286 /* See if this is a call to a function that can return more than once
2287 or a call to longjmp or malloc. */
2288 flags |= special_function_p (fndecl, flags);
2290 if (flags & ECF_MAY_BE_ALLOCA)
2291 current_function_calls_alloca = 1;
2293 /* If struct_value_rtx is 0, it means pass the address
2294 as if it were an extra parameter. */
2295 if (structure_value_addr && struct_value_rtx == 0)
2297 /* If structure_value_addr is a REG other than
2298 virtual_outgoing_args_rtx, we can use always use it. If it
2299 is not a REG, we must always copy it into a register.
2300 If it is virtual_outgoing_args_rtx, we must copy it to another
2301 register in some cases. */
2302 rtx temp = (GET_CODE (structure_value_addr) != REG
2303 || (ACCUMULATE_OUTGOING_ARGS
2304 && stack_arg_under_construction
2305 && structure_value_addr == virtual_outgoing_args_rtx)
2306 ? copy_addr_to_reg (structure_value_addr)
2307 : structure_value_addr);
2309 actparms
2310 = tree_cons (error_mark_node,
2311 make_tree (build_pointer_type (TREE_TYPE (funtype)),
2312 temp),
2313 actparms);
2314 structure_value_addr_parm = 1;
2317 /* Count the arguments and set NUM_ACTUALS. */
2318 for (p = actparms, num_actuals = 0; p; p = TREE_CHAIN (p))
2319 num_actuals++;
2321 /* Compute number of named args.
2322 Normally, don't include the last named arg if anonymous args follow.
2323 We do include the last named arg if STRICT_ARGUMENT_NAMING is nonzero.
2324 (If no anonymous args follow, the result of list_length is actually
2325 one too large. This is harmless.)
2327 If PRETEND_OUTGOING_VARARGS_NAMED is set and STRICT_ARGUMENT_NAMING is
2328 zero, this machine will be able to place unnamed args that were
2329 passed in registers into the stack. So treat all args as named.
2330 This allows the insns emitting for a specific argument list to be
2331 independent of the function declaration.
2333 If PRETEND_OUTGOING_VARARGS_NAMED is not set, we do not have any
2334 reliable way to pass unnamed args in registers, so we must force
2335 them into memory. */
2337 if ((STRICT_ARGUMENT_NAMING
2338 || ! PRETEND_OUTGOING_VARARGS_NAMED)
2339 && TYPE_ARG_TYPES (funtype) != 0)
2340 n_named_args
2341 = (list_length (TYPE_ARG_TYPES (funtype))
2342 /* Don't include the last named arg. */
2343 - (STRICT_ARGUMENT_NAMING ? 0 : 1)
2344 /* Count the struct value address, if it is passed as a parm. */
2345 + structure_value_addr_parm);
2346 else
2347 /* If we know nothing, treat all args as named. */
2348 n_named_args = num_actuals;
2350 /* Start updating where the next arg would go.
2352 On some machines (such as the PA) indirect calls have a different
2353 calling convention than normal calls. The last argument in
2354 INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
2355 or not. */
2356 INIT_CUMULATIVE_ARGS (args_so_far, funtype, NULL_RTX, (fndecl == 0));
2359 /* Make a vector to hold all the information about each arg. */
2360 args = (struct arg_data *) alloca (num_actuals
2361 * sizeof (struct arg_data));
2362 bzero ((char *) args, num_actuals * sizeof (struct arg_data));
2364 /* Build up entries inthe ARGS array, compute the size of the arguments
2365 into ARGS_SIZE, etc. */
2366 initialize_argument_information (num_actuals, args, &args_size,
2367 n_named_args, actparms, fndecl,
2368 &args_so_far, reg_parm_stack_space,
2369 &old_stack_level, &old_pending_adj,
2370 &must_preallocate, &flags);
2372 if (args_size.var)
2374 /* If this function requires a variable-sized argument list, don't
2375 try to make a cse'able block for this call. We may be able to
2376 do this eventually, but it is too complicated to keep track of
2377 what insns go in the cse'able block and which don't. */
2379 flags &= ~(ECF_CONST | ECF_PURE);
2380 must_preallocate = 1;
2383 /* Now make final decision about preallocating stack space. */
2384 must_preallocate = finalize_must_preallocate (must_preallocate,
2385 num_actuals, args,
2386 &args_size);
2388 /* If the structure value address will reference the stack pointer, we
2389 must stabilize it. We don't need to do this if we know that we are
2390 not going to adjust the stack pointer in processing this call. */
2392 if (structure_value_addr
2393 && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
2394 || reg_mentioned_p (virtual_outgoing_args_rtx,
2395 structure_value_addr))
2396 && (args_size.var
2397 || (!ACCUMULATE_OUTGOING_ARGS && args_size.constant)))
2398 structure_value_addr = copy_to_reg (structure_value_addr);
2400 /* Tail calls can make things harder to debug, and we're traditionally
2401 pushed these optimizations into -O2. Don't try if we're already
2402 expanding a call, as that means we're an argument. Similarly, if
2403 there's pending loops or cleanups we know there's code to follow
2404 the call.
2406 If rtx_equal_function_value_matters is false, that means we've
2407 finished with regular parsing. Which means that some of the
2408 machinery we use to generate tail-calls is no longer in place.
2409 This is most often true of sjlj-exceptions, which we couldn't
2410 tail-call to anyway. */
2412 if (currently_expanding_call++ != 0
2413 || !flag_optimize_sibling_calls
2414 || !rtx_equal_function_value_matters
2415 || !stmt_loop_nest_empty ()
2416 || any_pending_cleanups (1)
2417 || args_size.var)
2418 try_tail_call = try_tail_recursion = 0;
2420 /* Tail recursion fails, when we are not dealing with recursive calls. */
2421 if (!try_tail_recursion
2422 || TREE_CODE (TREE_OPERAND (exp, 0)) != ADDR_EXPR
2423 || TREE_OPERAND (TREE_OPERAND (exp, 0), 0) != current_function_decl)
2424 try_tail_recursion = 0;
2426 /* Rest of purposes for tail call optimizations to fail. */
2427 if (
2428 #ifdef HAVE_sibcall_epilogue
2429 !HAVE_sibcall_epilogue
2430 #else
2432 #endif
2433 || !try_tail_call
2434 /* Doing sibling call optimization needs some work, since
2435 structure_value_addr can be allocated on the stack.
2436 It does not seem worth the effort since few optimizable
2437 sibling calls will return a structure. */
2438 || structure_value_addr != NULL_RTX
2439 /* If the register holding the address is a callee saved
2440 register, then we lose. We have no way to prevent that,
2441 so we only allow calls to named functions. */
2442 /* ??? This could be done by having the insn constraints
2443 use a register class that is all call-clobbered. Any
2444 reload insns generated to fix things up would appear
2445 before the sibcall_epilogue. */
2446 || fndecl == NULL_TREE
2447 || (flags & (ECF_RETURNS_TWICE | ECF_LONGJMP))
2448 || !FUNCTION_OK_FOR_SIBCALL (fndecl)
2449 /* If this function requires more stack slots than the current
2450 function, we cannot change it into a sibling call. */
2451 || args_size.constant > current_function_args_size
2452 /* If the callee pops its own arguments, then it must pop exactly
2453 the same number of arguments as the current function. */
2454 || RETURN_POPS_ARGS (fndecl, funtype, args_size.constant)
2455 != RETURN_POPS_ARGS (current_function_decl,
2456 TREE_TYPE (current_function_decl),
2457 current_function_args_size))
2458 try_tail_call = 0;
2460 if (try_tail_call || try_tail_recursion)
2462 int end, inc;
2463 actparms = NULL_TREE;
2464 /* Ok, we're going to give the tail call the old college try.
2465 This means we're going to evaluate the function arguments
2466 up to three times. There are two degrees of badness we can
2467 encounter, those that can be unsaved and those that can't.
2468 (See unsafe_for_reeval commentary for details.)
2470 Generate a new argument list. Pass safe arguments through
2471 unchanged. For the easy badness wrap them in UNSAVE_EXPRs.
2472 For hard badness, evaluate them now and put their resulting
2473 rtx in a temporary VAR_DECL.
2475 initialize_argument_information has ordered the array for the
2476 order to be pushed, and we must remember this when reconstructing
2477 the original argument orde. */
2479 if (PUSH_ARGS_REVERSED)
2481 inc = 1;
2482 i = 0;
2483 end = num_actuals;
2485 else
2487 inc = -1;
2488 i = num_actuals - 1;
2489 end = -1;
2492 for (; i != end; i += inc)
2494 switch (unsafe_for_reeval (args[i].tree_value))
2496 case 0: /* Safe. */
2497 break;
2499 case 1: /* Mildly unsafe. */
2500 args[i].tree_value = unsave_expr (args[i].tree_value);
2501 break;
2503 case 2: /* Wildly unsafe. */
2505 tree var = build_decl (VAR_DECL, NULL_TREE,
2506 TREE_TYPE (args[i].tree_value));
2507 DECL_RTL (var) = expand_expr (args[i].tree_value, NULL_RTX,
2508 VOIDmode, EXPAND_NORMAL);
2509 args[i].tree_value = var;
2511 break;
2513 default:
2514 abort ();
2516 /* We need to build actparms for optimize_tail_recursion. We can
2517 safely trash away TREE_PURPOSE, since it is unused by this
2518 function. */
2519 if (try_tail_recursion)
2520 actparms = tree_cons (NULL_TREE, args[i].tree_value, actparms);
2522 /* Expanding one of those dangerous arguments could have added
2523 cleanups, but otherwise give it a whirl. */
2524 if (any_pending_cleanups (1))
2525 try_tail_call = try_tail_recursion = 0;
2528 /* Generate a tail recursion sequence when calling ourselves. */
2530 if (try_tail_recursion)
2532 /* We want to emit any pending stack adjustments before the tail
2533 recursion "call". That way we know any adjustment after the tail
2534 recursion call can be ignored if we indeed use the tail recursion
2535 call expansion. */
2536 int save_pending_stack_adjust = pending_stack_adjust;
2537 int save_stack_pointer_delta = stack_pointer_delta;
2539 /* Use a new sequence to hold any RTL we generate. We do not even
2540 know if we will use this RTL yet. The final decision can not be
2541 made until after RTL generation for the entire function is
2542 complete. */
2543 start_sequence ();
2544 /* If expanding any of the arguments creates cleanups, we can't
2545 do a tailcall. So, we'll need to pop the pending cleanups
2546 list. If, however, all goes well, and there are no cleanups
2547 then the call to expand_start_target_temps will have no
2548 effect. */
2549 expand_start_target_temps ();
2550 if (optimize_tail_recursion (actparms, get_last_insn ()))
2552 if (any_pending_cleanups (1))
2553 try_tail_call = try_tail_recursion = 0;
2554 else
2555 tail_recursion_insns = get_insns ();
2557 expand_end_target_temps ();
2558 end_sequence ();
2560 /* Restore the original pending stack adjustment for the sibling and
2561 normal call cases below. */
2562 pending_stack_adjust = save_pending_stack_adjust;
2563 stack_pointer_delta = save_stack_pointer_delta;
2566 if (profile_arc_flag && (flags & ECF_FORK_OR_EXEC))
2568 /* A fork duplicates the profile information, and an exec discards
2569 it. We can't rely on fork/exec to be paired. So write out the
2570 profile information we have gathered so far, and clear it. */
2571 /* ??? When Linux's __clone is called with CLONE_VM set, profiling
2572 is subject to race conditions, just as with multithreaded
2573 programs. */
2575 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__bb_fork_func"), 0,
2576 VOIDmode, 0);
2579 /* Ensure current function's preferred stack boundary is at least
2580 what we need. We don't have to increase alignment for recursive
2581 functions. */
2582 if (cfun->preferred_stack_boundary < preferred_stack_boundary
2583 && fndecl != current_function_decl)
2584 cfun->preferred_stack_boundary = preferred_stack_boundary;
2586 preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
2588 function_call_count++;
2590 /* We want to make two insn chains; one for a sibling call, the other
2591 for a normal call. We will select one of the two chains after
2592 initial RTL generation is complete. */
2593 for (pass = 0; pass < 2; pass++)
2595 int sibcall_failure = 0;
2596 /* We want to emit ay pending stack adjustments before the tail
2597 recursion "call". That way we know any adjustment after the tail
2598 recursion call can be ignored if we indeed use the tail recursion
2599 call expansion. */
2600 int save_pending_stack_adjust = 0;
2601 int save_stack_pointer_delta = 0;
2602 rtx insns;
2603 rtx before_call, next_arg_reg;
2605 if (pass == 0)
2607 if (! try_tail_call)
2608 continue;
2610 /* Emit any queued insns now; otherwise they would end up in
2611 only one of the alternates. */
2612 emit_queue ();
2614 /* State variables we need to save and restore between
2615 iterations. */
2616 save_pending_stack_adjust = pending_stack_adjust;
2617 save_stack_pointer_delta = stack_pointer_delta;
2619 if (pass)
2620 flags &= ~ECF_SIBCALL;
2621 else
2622 flags |= ECF_SIBCALL;
2624 /* Other state variables that we must reinitialize each time
2625 through the loop (that are not initialized by the loop itself). */
2626 argblock = 0;
2627 call_fusage = 0;
2629 /* Start a new sequence for the normal call case.
2631 From this point on, if the sibling call fails, we want to set
2632 sibcall_failure instead of continuing the loop. */
2633 start_sequence ();
2635 if (pass == 0)
2637 /* We know at this point that there are not currently any
2638 pending cleanups. If, however, in the process of evaluating
2639 the arguments we were to create some, we'll need to be
2640 able to get rid of them. */
2641 expand_start_target_temps ();
2644 /* When calling a const function, we must pop the stack args right away,
2645 so that the pop is deleted or moved with the call. */
2646 if (flags & (ECF_CONST | ECF_PURE))
2647 NO_DEFER_POP;
2649 /* Don't let pending stack adjusts add up to too much.
2650 Also, do all pending adjustments now if there is any chance
2651 this might be a call to alloca or if we are expanding a sibling
2652 call sequence. */
2653 if (pending_stack_adjust >= 32
2654 || (pending_stack_adjust > 0 && (flags & ECF_MAY_BE_ALLOCA))
2655 || pass == 0)
2656 do_pending_stack_adjust ();
2658 /* Push the temporary stack slot level so that we can free any
2659 temporaries we make. */
2660 push_temp_slots ();
2663 #ifdef FINAL_REG_PARM_STACK_SPACE
2664 reg_parm_stack_space = FINAL_REG_PARM_STACK_SPACE (args_size.constant,
2665 args_size.var);
2666 #endif
2667 /* Precompute any arguments as needed. */
2668 if (pass)
2669 precompute_arguments (flags, num_actuals, args);
2671 /* Now we are about to start emitting insns that can be deleted
2672 if a libcall is deleted. */
2673 if (flags & (ECF_CONST | ECF_PURE | ECF_MALLOC))
2674 start_sequence ();
2676 adjusted_args_size = args_size;
2677 /* Compute the actual size of the argument block required. The variable
2678 and constant sizes must be combined, the size may have to be rounded,
2679 and there may be a minimum required size. When generating a sibcall
2680 pattern, do not round up, since we'll be re-using whatever space our
2681 caller provided. */
2682 unadjusted_args_size
2683 = compute_argument_block_size (reg_parm_stack_space, &adjusted_args_size,
2684 (pass == 0 ? 0
2685 : preferred_stack_boundary));
2687 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
2689 /* The argument block when performing a sibling call is the
2690 incoming argument block. */
2691 if (pass == 0)
2693 argblock = virtual_incoming_args_rtx;
2694 stored_args_map = sbitmap_alloc (args_size.constant);
2695 sbitmap_zero (stored_args_map);
2698 /* If we have no actual push instructions, or shouldn't use them,
2699 make space for all args right now. */
2700 else if (adjusted_args_size.var != 0)
2702 if (old_stack_level == 0)
2704 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
2705 old_pending_adj = pending_stack_adjust;
2706 pending_stack_adjust = 0;
2707 /* stack_arg_under_construction says whether a stack arg is
2708 being constructed at the old stack level. Pushing the stack
2709 gets a clean outgoing argument block. */
2710 old_stack_arg_under_construction = stack_arg_under_construction;
2711 stack_arg_under_construction = 0;
2713 argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
2715 else
2717 /* Note that we must go through the motions of allocating an argument
2718 block even if the size is zero because we may be storing args
2719 in the area reserved for register arguments, which may be part of
2720 the stack frame. */
2722 int needed = adjusted_args_size.constant;
2724 /* Store the maximum argument space used. It will be pushed by
2725 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
2726 checking). */
2728 if (needed > current_function_outgoing_args_size)
2729 current_function_outgoing_args_size = needed;
2731 if (must_preallocate)
2733 if (ACCUMULATE_OUTGOING_ARGS)
2735 /* Since the stack pointer will never be pushed, it is
2736 possible for the evaluation of a parm to clobber
2737 something we have already written to the stack.
2738 Since most function calls on RISC machines do not use
2739 the stack, this is uncommon, but must work correctly.
2741 Therefore, we save any area of the stack that was already
2742 written and that we are using. Here we set up to do this
2743 by making a new stack usage map from the old one. The
2744 actual save will be done by store_one_arg.
2746 Another approach might be to try to reorder the argument
2747 evaluations to avoid this conflicting stack usage. */
2749 #ifndef OUTGOING_REG_PARM_STACK_SPACE
2750 /* Since we will be writing into the entire argument area,
2751 the map must be allocated for its entire size, not just
2752 the part that is the responsibility of the caller. */
2753 needed += reg_parm_stack_space;
2754 #endif
2756 #ifdef ARGS_GROW_DOWNWARD
2757 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2758 needed + 1);
2759 #else
2760 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2761 needed);
2762 #endif
2763 stack_usage_map
2764 = (char *) alloca (highest_outgoing_arg_in_use);
2766 if (initial_highest_arg_in_use)
2767 bcopy (initial_stack_usage_map, stack_usage_map,
2768 initial_highest_arg_in_use);
2770 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
2771 bzero (&stack_usage_map[initial_highest_arg_in_use],
2772 (highest_outgoing_arg_in_use
2773 - initial_highest_arg_in_use));
2774 needed = 0;
2776 /* The address of the outgoing argument list must not be
2777 copied to a register here, because argblock would be left
2778 pointing to the wrong place after the call to
2779 allocate_dynamic_stack_space below. */
2781 argblock = virtual_outgoing_args_rtx;
2783 else
2785 if (inhibit_defer_pop == 0)
2787 /* Try to reuse some or all of the pending_stack_adjust
2788 to get this space. */
2789 needed
2790 = (combine_pending_stack_adjustment_and_call
2791 (unadjusted_args_size,
2792 &adjusted_args_size,
2793 preferred_unit_stack_boundary));
2795 /* combine_pending_stack_adjustment_and_call computes
2796 an adjustment before the arguments are allocated.
2797 Account for them and see whether or not the stack
2798 needs to go up or down. */
2799 needed = unadjusted_args_size - needed;
2801 if (needed < 0)
2803 /* We're releasing stack space. */
2804 /* ??? We can avoid any adjustment at all if we're
2805 already aligned. FIXME. */
2806 pending_stack_adjust = -needed;
2807 do_pending_stack_adjust ();
2808 needed = 0;
2810 else
2811 /* We need to allocate space. We'll do that in
2812 push_block below. */
2813 pending_stack_adjust = 0;
2816 /* Special case this because overhead of `push_block' in
2817 this case is non-trivial. */
2818 if (needed == 0)
2819 argblock = virtual_outgoing_args_rtx;
2820 else
2821 argblock = push_block (GEN_INT (needed), 0, 0);
2823 /* We only really need to call `copy_to_reg' in the case
2824 where push insns are going to be used to pass ARGBLOCK
2825 to a function call in ARGS. In that case, the stack
2826 pointer changes value from the allocation point to the
2827 call point, and hence the value of
2828 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
2829 as well always do it. */
2830 argblock = copy_to_reg (argblock);
2832 /* The save/restore code in store_one_arg handles all
2833 cases except one: a constructor call (including a C
2834 function returning a BLKmode struct) to initialize
2835 an argument. */
2836 if (stack_arg_under_construction)
2838 #ifndef OUTGOING_REG_PARM_STACK_SPACE
2839 rtx push_size = GEN_INT (reg_parm_stack_space
2840 + adjusted_args_size.constant);
2841 #else
2842 rtx push_size = GEN_INT (adjusted_args_size.constant);
2843 #endif
2844 if (old_stack_level == 0)
2846 emit_stack_save (SAVE_BLOCK, &old_stack_level,
2847 NULL_RTX);
2848 old_pending_adj = pending_stack_adjust;
2849 pending_stack_adjust = 0;
2850 /* stack_arg_under_construction says whether a stack
2851 arg is being constructed at the old stack level.
2852 Pushing the stack gets a clean outgoing argument
2853 block. */
2854 old_stack_arg_under_construction
2855 = stack_arg_under_construction;
2856 stack_arg_under_construction = 0;
2857 /* Make a new map for the new argument list. */
2858 stack_usage_map = (char *)
2859 alloca (highest_outgoing_arg_in_use);
2860 bzero (stack_usage_map, highest_outgoing_arg_in_use);
2861 highest_outgoing_arg_in_use = 0;
2863 allocate_dynamic_stack_space (push_size, NULL_RTX,
2864 BITS_PER_UNIT);
2866 /* If argument evaluation might modify the stack pointer,
2867 copy the address of the argument list to a register. */
2868 for (i = 0; i < num_actuals; i++)
2869 if (args[i].pass_on_stack)
2871 argblock = copy_addr_to_reg (argblock);
2872 break;
2878 compute_argument_addresses (args, argblock, num_actuals);
2880 #ifdef PREFERRED_STACK_BOUNDARY
2881 /* If we push args individually in reverse order, perform stack alignment
2882 before the first push (the last arg). */
2883 if (PUSH_ARGS_REVERSED && argblock == 0
2884 && adjusted_args_size.constant != unadjusted_args_size)
2886 /* When the stack adjustment is pending, we get better code
2887 by combining the adjustments. */
2888 if (pending_stack_adjust
2889 && ! (flags & (ECF_CONST | ECF_PURE))
2890 && ! inhibit_defer_pop)
2892 pending_stack_adjust
2893 = (combine_pending_stack_adjustment_and_call
2894 (unadjusted_args_size,
2895 &adjusted_args_size,
2896 preferred_unit_stack_boundary));
2897 do_pending_stack_adjust ();
2899 else if (argblock == 0)
2900 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
2901 - unadjusted_args_size));
2903 /* Now that the stack is properly aligned, pops can't safely
2904 be deferred during the evaluation of the arguments. */
2905 NO_DEFER_POP;
2906 #endif
2908 /* Don't try to defer pops if preallocating, not even from the first arg,
2909 since ARGBLOCK probably refers to the SP. */
2910 if (argblock)
2911 NO_DEFER_POP;
2913 funexp = rtx_for_function_call (fndecl, exp);
2915 /* Figure out the register where the value, if any, will come back. */
2916 valreg = 0;
2917 if (TYPE_MODE (TREE_TYPE (exp)) != VOIDmode
2918 && ! structure_value_addr)
2920 if (pcc_struct_value)
2921 valreg = hard_function_value (build_pointer_type (TREE_TYPE (exp)),
2922 fndecl, (pass == 0));
2923 else
2924 valreg = hard_function_value (TREE_TYPE (exp), fndecl, (pass == 0));
2927 /* Precompute all register parameters. It isn't safe to compute anything
2928 once we have started filling any specific hard regs. */
2929 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
2931 #ifdef REG_PARM_STACK_SPACE
2932 /* Save the fixed argument area if it's part of the caller's frame and
2933 is clobbered by argument setup for this call. */
2934 if (ACCUMULATE_OUTGOING_ARGS && pass)
2935 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
2936 &low_to_save, &high_to_save);
2937 #endif
2939 /* Now store (and compute if necessary) all non-register parms.
2940 These come before register parms, since they can require block-moves,
2941 which could clobber the registers used for register parms.
2942 Parms which have partial registers are not stored here,
2943 but we do preallocate space here if they want that. */
2945 for (i = 0; i < num_actuals; i++)
2946 if (args[i].reg == 0 || args[i].pass_on_stack)
2948 rtx before_arg = get_last_insn ();
2950 store_one_arg (&args[i], argblock, flags,
2951 adjusted_args_size.var != 0, reg_parm_stack_space);
2952 if (pass == 0 &&
2953 check_sibcall_argument_overlap (before_arg, &args[i]))
2954 sibcall_failure = 1;
2957 /* If we have a parm that is passed in registers but not in memory
2958 and whose alignment does not permit a direct copy into registers,
2959 make a group of pseudos that correspond to each register that we
2960 will later fill. */
2961 if (STRICT_ALIGNMENT)
2962 store_unaligned_arguments_into_pseudos (args, num_actuals);
2964 /* Now store any partially-in-registers parm.
2965 This is the last place a block-move can happen. */
2966 if (reg_parm_seen)
2967 for (i = 0; i < num_actuals; i++)
2968 if (args[i].partial != 0 && ! args[i].pass_on_stack)
2970 rtx before_arg = get_last_insn ();
2972 store_one_arg (&args[i], argblock, flags,
2973 adjusted_args_size.var != 0, reg_parm_stack_space);
2974 if (pass == 0 &&
2975 check_sibcall_argument_overlap (before_arg, &args[i]))
2976 sibcall_failure = 1;
2979 #ifdef PREFERRED_STACK_BOUNDARY
2980 /* If we pushed args in forward order, perform stack alignment
2981 after pushing the last arg. */
2982 if (!PUSH_ARGS_REVERSED && argblock == 0)
2983 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
2984 - unadjusted_args_size));
2985 #endif
2987 /* If register arguments require space on the stack and stack space
2988 was not preallocated, allocate stack space here for arguments
2989 passed in registers. */
2990 #ifdef OUTGOING_REG_PARM_STACK_SPACE
2991 if (!ACCUMULATE_OUTGOING_ARGS
2992 && must_preallocate == 0 && reg_parm_stack_space > 0)
2993 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
2994 #endif
2996 /* Pass the function the address in which to return a
2997 structure value. */
2998 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
3000 emit_move_insn (struct_value_rtx,
3001 force_reg (Pmode,
3002 force_operand (structure_value_addr,
3003 NULL_RTX)));
3005 /* Mark the memory for the aggregate as write-only. */
3006 if (current_function_check_memory_usage)
3007 emit_library_call (chkr_set_right_libfunc, 1,
3008 VOIDmode, 3,
3009 structure_value_addr, ptr_mode,
3010 GEN_INT (struct_value_size),
3011 TYPE_MODE (sizetype),
3012 GEN_INT (MEMORY_USE_WO),
3013 TYPE_MODE (integer_type_node));
3015 if (GET_CODE (struct_value_rtx) == REG)
3016 use_reg (&call_fusage, struct_value_rtx);
3019 funexp = prepare_call_address (funexp, fndecl, &call_fusage,
3020 reg_parm_seen);
3022 load_register_parameters (args, num_actuals, &call_fusage, flags);
3024 /* Perform postincrements before actually calling the function. */
3025 emit_queue ();
3027 /* Save a pointer to the last insn before the call, so that we can
3028 later safely search backwards to find the CALL_INSN. */
3029 before_call = get_last_insn ();
3031 /* Set up next argument register. For sibling calls on machines
3032 with register windows this should be the incoming register. */
3033 #ifdef FUNCTION_INCOMING_ARG
3034 if (pass == 0)
3035 next_arg_reg = FUNCTION_INCOMING_ARG (args_so_far, VOIDmode,
3036 void_type_node, 1);
3037 else
3038 #endif
3039 next_arg_reg = FUNCTION_ARG (args_so_far, VOIDmode,
3040 void_type_node, 1);
3042 /* All arguments and registers used for the call must be set up by
3043 now! */
3045 #ifdef PREFERRED_STACK_BOUNDARY
3046 /* Stack must be properly aligned now. */
3047 if (pass && stack_pointer_delta % preferred_unit_stack_boundary)
3048 abort ();
3049 #endif
3051 /* Generate the actual call instruction. */
3052 emit_call_1 (funexp, fndecl, funtype, unadjusted_args_size,
3053 adjusted_args_size.constant, struct_value_size,
3054 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
3055 flags);
3057 /* Verify that we've deallocated all the stack we used. */
3058 if (pass
3059 && old_stack_allocated != stack_pointer_delta - pending_stack_adjust)
3060 abort();
3062 /* If call is cse'able, make appropriate pair of reg-notes around it.
3063 Test valreg so we don't crash; may safely ignore `const'
3064 if return type is void. Disable for PARALLEL return values, because
3065 we have no way to move such values into a pseudo register. */
3066 if (pass
3067 && (flags & (ECF_CONST | ECF_PURE))
3068 && valreg != 0 && GET_CODE (valreg) != PARALLEL)
3070 rtx note = 0;
3071 rtx temp = gen_reg_rtx (GET_MODE (valreg));
3072 rtx insns;
3074 /* Mark the return value as a pointer if needed. */
3075 if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
3076 mark_reg_pointer (temp, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp))));
3078 /* Construct an "equal form" for the value which mentions all the
3079 arguments in order as well as the function name. */
3080 for (i = 0; i < num_actuals; i++)
3081 note = gen_rtx_EXPR_LIST (VOIDmode, args[i].initial_value, note);
3082 note = gen_rtx_EXPR_LIST (VOIDmode, funexp, note);
3084 insns = get_insns ();
3085 end_sequence ();
3087 if (flags & ECF_PURE)
3088 note = gen_rtx_EXPR_LIST (VOIDmode,
3089 gen_rtx_USE (VOIDmode,
3090 gen_rtx_MEM (BLKmode,
3091 gen_rtx_SCRATCH (VOIDmode))), note);
3093 emit_libcall_block (insns, temp, valreg, note);
3095 valreg = temp;
3097 else if (flags & (ECF_CONST | ECF_PURE))
3099 /* Otherwise, just write out the sequence without a note. */
3100 rtx insns = get_insns ();
3102 end_sequence ();
3103 emit_insns (insns);
3105 else if (flags & ECF_MALLOC)
3107 rtx temp = gen_reg_rtx (GET_MODE (valreg));
3108 rtx last, insns;
3110 /* The return value from a malloc-like function is a pointer. */
3111 if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
3112 mark_reg_pointer (temp, BIGGEST_ALIGNMENT);
3114 emit_move_insn (temp, valreg);
3116 /* The return value from a malloc-like function can not alias
3117 anything else. */
3118 last = get_last_insn ();
3119 REG_NOTES (last) =
3120 gen_rtx_EXPR_LIST (REG_NOALIAS, temp, REG_NOTES (last));
3122 /* Write out the sequence. */
3123 insns = get_insns ();
3124 end_sequence ();
3125 emit_insns (insns);
3126 valreg = temp;
3129 /* For calls to `setjmp', etc., inform flow.c it should complain
3130 if nonvolatile values are live. For functions that cannot return,
3131 inform flow that control does not fall through. */
3133 if ((flags & (ECF_RETURNS_TWICE | ECF_NORETURN | ECF_LONGJMP)) || pass == 0)
3135 /* The barrier or NOTE_INSN_SETJMP note must be emitted
3136 immediately after the CALL_INSN. Some ports emit more
3137 than just a CALL_INSN above, so we must search for it here. */
3139 rtx last = get_last_insn ();
3140 while (GET_CODE (last) != CALL_INSN)
3142 last = PREV_INSN (last);
3143 /* There was no CALL_INSN? */
3144 if (last == before_call)
3145 abort ();
3148 if (flags & ECF_RETURNS_TWICE)
3150 emit_note_after (NOTE_INSN_SETJMP, last);
3151 current_function_calls_setjmp = 1;
3153 else
3154 emit_barrier_after (last);
3157 if (flags & ECF_LONGJMP)
3158 current_function_calls_longjmp = 1;
3160 /* If this function is returning into a memory location marked as
3161 readonly, it means it is initializing that location. But we normally
3162 treat functions as not clobbering such locations, so we need to
3163 specify that this one does. */
3164 if (target != 0 && GET_CODE (target) == MEM
3165 && structure_value_addr != 0 && RTX_UNCHANGING_P (target))
3166 emit_insn (gen_rtx_CLOBBER (VOIDmode, target));
3168 /* If value type not void, return an rtx for the value. */
3170 /* If there are cleanups to be called, don't use a hard reg as target.
3171 We need to double check this and see if it matters anymore. */
3172 if (any_pending_cleanups (1))
3174 if (target && REG_P (target)
3175 && REGNO (target) < FIRST_PSEUDO_REGISTER)
3176 target = 0;
3177 sibcall_failure = 1;
3180 if (TYPE_MODE (TREE_TYPE (exp)) == VOIDmode
3181 || ignore)
3183 target = const0_rtx;
3185 else if (structure_value_addr)
3187 if (target == 0 || GET_CODE (target) != MEM)
3189 target
3190 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
3191 memory_address (TYPE_MODE (TREE_TYPE (exp)),
3192 structure_value_addr));
3193 set_mem_attributes (target, exp, 1);
3196 else if (pcc_struct_value)
3198 /* This is the special C++ case where we need to
3199 know what the true target was. We take care to
3200 never use this value more than once in one expression. */
3201 target = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
3202 copy_to_reg (valreg));
3203 set_mem_attributes (target, exp, 1);
3205 /* Handle calls that return values in multiple non-contiguous locations.
3206 The Irix 6 ABI has examples of this. */
3207 else if (GET_CODE (valreg) == PARALLEL)
3209 int bytes = int_size_in_bytes (TREE_TYPE (exp));
3211 if (target == 0)
3213 target = assign_stack_temp (TYPE_MODE (TREE_TYPE (exp)),
3214 bytes, 0);
3215 MEM_SET_IN_STRUCT_P (target, AGGREGATE_TYPE_P (TREE_TYPE (exp)));
3216 preserve_temp_slots (target);
3219 if (! rtx_equal_p (target, valreg))
3220 emit_group_store (target, valreg, bytes,
3221 TYPE_ALIGN (TREE_TYPE (exp)));
3223 /* We can not support sibling calls for this case. */
3224 sibcall_failure = 1;
3226 else if (target
3227 && GET_MODE (target) == TYPE_MODE (TREE_TYPE (exp))
3228 && GET_MODE (target) == GET_MODE (valreg))
3230 /* TARGET and VALREG cannot be equal at this point because the
3231 latter would not have REG_FUNCTION_VALUE_P true, while the
3232 former would if it were referring to the same register.
3234 If they refer to the same register, this move will be a no-op,
3235 except when function inlining is being done. */
3236 emit_move_insn (target, valreg);
3238 else if (TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
3239 target = copy_blkmode_from_reg (target, valreg, TREE_TYPE (exp));
3240 else
3241 target = copy_to_reg (valreg);
3243 #ifdef PROMOTE_FUNCTION_RETURN
3244 /* If we promoted this return value, make the proper SUBREG. TARGET
3245 might be const0_rtx here, so be careful. */
3246 if (GET_CODE (target) == REG
3247 && TYPE_MODE (TREE_TYPE (exp)) != BLKmode
3248 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
3250 tree type = TREE_TYPE (exp);
3251 int unsignedp = TREE_UNSIGNED (type);
3253 /* If we don't promote as expected, something is wrong. */
3254 if (GET_MODE (target)
3255 != promote_mode (type, TYPE_MODE (type), &unsignedp, 1))
3256 abort ();
3258 target = gen_rtx_SUBREG (TYPE_MODE (type), target, 0);
3259 SUBREG_PROMOTED_VAR_P (target) = 1;
3260 SUBREG_PROMOTED_UNSIGNED_P (target) = unsignedp;
3262 #endif
3264 /* If size of args is variable or this was a constructor call for a stack
3265 argument, restore saved stack-pointer value. */
3267 if (old_stack_level)
3269 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
3270 pending_stack_adjust = old_pending_adj;
3271 stack_arg_under_construction = old_stack_arg_under_construction;
3272 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3273 stack_usage_map = initial_stack_usage_map;
3274 sibcall_failure = 1;
3276 else if (ACCUMULATE_OUTGOING_ARGS && pass)
3278 #ifdef REG_PARM_STACK_SPACE
3279 if (save_area)
3281 restore_fixed_argument_area (save_area, argblock,
3282 high_to_save, low_to_save);
3284 #endif
3286 /* If we saved any argument areas, restore them. */
3287 for (i = 0; i < num_actuals; i++)
3288 if (args[i].save_area)
3290 enum machine_mode save_mode = GET_MODE (args[i].save_area);
3291 rtx stack_area
3292 = gen_rtx_MEM (save_mode,
3293 memory_address (save_mode,
3294 XEXP (args[i].stack_slot, 0)));
3296 if (save_mode != BLKmode)
3297 emit_move_insn (stack_area, args[i].save_area);
3298 else
3299 emit_block_move (stack_area,
3300 validize_mem (args[i].save_area),
3301 GEN_INT (args[i].size.constant),
3302 PARM_BOUNDARY);
3305 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3306 stack_usage_map = initial_stack_usage_map;
3309 /* If this was alloca, record the new stack level for nonlocal gotos.
3310 Check for the handler slots since we might not have a save area
3311 for non-local gotos. */
3313 if ((flags & ECF_MAY_BE_ALLOCA) && nonlocal_goto_handler_slots != 0)
3314 emit_stack_save (SAVE_NONLOCAL, &nonlocal_goto_stack_level, NULL_RTX);
3316 pop_temp_slots ();
3318 /* Free up storage we no longer need. */
3319 for (i = 0; i < num_actuals; ++i)
3320 if (args[i].aligned_regs)
3321 free (args[i].aligned_regs);
3323 if (pass == 0)
3325 /* Undo the fake expand_start_target_temps we did earlier. If
3326 there had been any cleanups created, we've already set
3327 sibcall_failure. */
3328 expand_end_target_temps ();
3331 insns = get_insns ();
3332 end_sequence ();
3334 if (pass == 0)
3336 tail_call_insns = insns;
3338 /* If something prevents making this a sibling call,
3339 zero out the sequence. */
3340 if (sibcall_failure)
3341 tail_call_insns = NULL_RTX;
3342 /* Restore the pending stack adjustment now that we have
3343 finished generating the sibling call sequence. */
3345 pending_stack_adjust = save_pending_stack_adjust;
3346 stack_pointer_delta = save_stack_pointer_delta;
3348 /* Prepare arg structure for next iteration. */
3349 for (i = 0 ; i < num_actuals ; i++)
3351 args[i].value = 0;
3352 args[i].aligned_regs = 0;
3353 args[i].stack = 0;
3356 sbitmap_free (stored_args_map);
3358 else
3359 normal_call_insns = insns;
3362 /* The function optimize_sibling_and_tail_recursive_calls doesn't
3363 handle CALL_PLACEHOLDERs inside other CALL_PLACEHOLDERs. This
3364 can happen if the arguments to this function call an inline
3365 function who's expansion contains another CALL_PLACEHOLDER.
3367 If there are any C_Ps in any of these sequences, replace them
3368 with their normal call. */
3370 for (insn = normal_call_insns; insn; insn = NEXT_INSN (insn))
3371 if (GET_CODE (insn) == CALL_INSN
3372 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
3373 replace_call_placeholder (insn, sibcall_use_normal);
3375 for (insn = tail_call_insns; insn; insn = NEXT_INSN (insn))
3376 if (GET_CODE (insn) == CALL_INSN
3377 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
3378 replace_call_placeholder (insn, sibcall_use_normal);
3380 for (insn = tail_recursion_insns; insn; insn = NEXT_INSN (insn))
3381 if (GET_CODE (insn) == CALL_INSN
3382 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
3383 replace_call_placeholder (insn, sibcall_use_normal);
3385 /* If this was a potential tail recursion site, then emit a
3386 CALL_PLACEHOLDER with the normal and the tail recursion streams.
3387 One of them will be selected later. */
3388 if (tail_recursion_insns || tail_call_insns)
3390 /* The tail recursion label must be kept around. We could expose
3391 its use in the CALL_PLACEHOLDER, but that creates unwanted edges
3392 and makes determining true tail recursion sites difficult.
3394 So we set LABEL_PRESERVE_P here, then clear it when we select
3395 one of the call sequences after rtl generation is complete. */
3396 if (tail_recursion_insns)
3397 LABEL_PRESERVE_P (tail_recursion_label) = 1;
3398 emit_call_insn (gen_rtx_CALL_PLACEHOLDER (VOIDmode, normal_call_insns,
3399 tail_call_insns,
3400 tail_recursion_insns,
3401 tail_recursion_label));
3403 else
3404 emit_insns (normal_call_insns);
3406 currently_expanding_call--;
3408 return target;
3411 /* Returns nonzero if FUN is the symbol for a library function which can
3412 not throw. */
3414 static int
3415 libfunc_nothrow (fun)
3416 rtx fun;
3418 if (fun == throw_libfunc
3419 || fun == rethrow_libfunc
3420 || fun == sjthrow_libfunc
3421 || fun == sjpopnthrow_libfunc)
3422 return 0;
3424 return 1;
3427 /* Output a library call to function FUN (a SYMBOL_REF rtx).
3428 The RETVAL parameter specifies whether return value needs to be saved, other
3429 parameters are documented in the emit_library_call function bellow. */
3430 static rtx
3431 emit_library_call_value_1 (retval, orgfun, value, fn_type, outmode, nargs, p)
3432 int retval;
3433 rtx orgfun;
3434 rtx value;
3435 int fn_type;
3436 enum machine_mode outmode;
3437 int nargs;
3438 va_list p;
3440 /* Total size in bytes of all the stack-parms scanned so far. */
3441 struct args_size args_size;
3442 /* Size of arguments before any adjustments (such as rounding). */
3443 struct args_size original_args_size;
3444 register int argnum;
3445 rtx fun;
3446 int inc;
3447 int count;
3448 struct args_size alignment_pad;
3449 rtx argblock = 0;
3450 CUMULATIVE_ARGS args_so_far;
3451 struct arg { rtx value; enum machine_mode mode; rtx reg; int partial;
3452 struct args_size offset; struct args_size size; rtx save_area; };
3453 struct arg *argvec;
3454 int old_inhibit_defer_pop = inhibit_defer_pop;
3455 rtx call_fusage = 0;
3456 rtx mem_value = 0;
3457 rtx valreg;
3458 int pcc_struct_value = 0;
3459 int struct_value_size = 0;
3460 int flags = 0;
3461 int reg_parm_stack_space = 0;
3462 int needed;
3464 #ifdef REG_PARM_STACK_SPACE
3465 /* Define the boundary of the register parm stack space that needs to be
3466 save, if any. */
3467 int low_to_save = -1, high_to_save = 0;
3468 rtx save_area = 0; /* Place that it is saved */
3469 #endif
3471 /* Size of the stack reserved for parameter registers. */
3472 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
3473 char *initial_stack_usage_map = stack_usage_map;
3475 #ifdef REG_PARM_STACK_SPACE
3476 #ifdef MAYBE_REG_PARM_STACK_SPACE
3477 reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
3478 #else
3479 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
3480 #endif
3481 #endif
3483 if (fn_type == 1)
3484 flags |= ECF_CONST;
3485 else if (fn_type == 2)
3486 flags |= ECF_PURE;
3487 fun = orgfun;
3489 if (libfunc_nothrow (fun))
3490 flags |= ECF_NOTHROW;
3492 #ifdef PREFERRED_STACK_BOUNDARY
3493 /* Ensure current function's preferred stack boundary is at least
3494 what we need. */
3495 if (cfun->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
3496 cfun->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3497 #endif
3499 /* If this kind of value comes back in memory,
3500 decide where in memory it should come back. */
3501 if (outmode != VOIDmode && aggregate_value_p (type_for_mode (outmode, 0)))
3503 #ifdef PCC_STATIC_STRUCT_RETURN
3504 rtx pointer_reg
3505 = hard_function_value (build_pointer_type (type_for_mode (outmode, 0)),
3506 0, 0);
3507 mem_value = gen_rtx_MEM (outmode, pointer_reg);
3508 pcc_struct_value = 1;
3509 if (value == 0)
3510 value = gen_reg_rtx (outmode);
3511 #else /* not PCC_STATIC_STRUCT_RETURN */
3512 struct_value_size = GET_MODE_SIZE (outmode);
3513 if (value != 0 && GET_CODE (value) == MEM)
3514 mem_value = value;
3515 else
3516 mem_value = assign_stack_temp (outmode, GET_MODE_SIZE (outmode), 0);
3517 #endif
3519 /* This call returns a big structure. */
3520 flags &= ~(ECF_CONST | ECF_PURE);
3523 /* ??? Unfinished: must pass the memory address as an argument. */
3525 /* Copy all the libcall-arguments out of the varargs data
3526 and into a vector ARGVEC.
3528 Compute how to pass each argument. We only support a very small subset
3529 of the full argument passing conventions to limit complexity here since
3530 library functions shouldn't have many args. */
3532 argvec = (struct arg *) alloca ((nargs + 1) * sizeof (struct arg));
3533 bzero ((char *) argvec, (nargs + 1) * sizeof (struct arg));
3535 INIT_CUMULATIVE_ARGS (args_so_far, NULL_TREE, fun, 0);
3537 args_size.constant = 0;
3538 args_size.var = 0;
3540 count = 0;
3542 /* Now we are about to start emitting insns that can be deleted
3543 if a libcall is deleted. */
3544 if (flags & (ECF_CONST | ECF_PURE))
3545 start_sequence ();
3547 push_temp_slots ();
3549 /* If there's a structure value address to be passed,
3550 either pass it in the special place, or pass it as an extra argument. */
3551 if (mem_value && struct_value_rtx == 0 && ! pcc_struct_value)
3553 rtx addr = XEXP (mem_value, 0);
3554 nargs++;
3556 /* Make sure it is a reasonable operand for a move or push insn. */
3557 if (GET_CODE (addr) != REG && GET_CODE (addr) != MEM
3558 && ! (CONSTANT_P (addr) && LEGITIMATE_CONSTANT_P (addr)))
3559 addr = force_operand (addr, NULL_RTX);
3561 argvec[count].value = addr;
3562 argvec[count].mode = Pmode;
3563 argvec[count].partial = 0;
3565 argvec[count].reg = FUNCTION_ARG (args_so_far, Pmode, NULL_TREE, 1);
3566 #ifdef FUNCTION_ARG_PARTIAL_NREGS
3567 if (FUNCTION_ARG_PARTIAL_NREGS (args_so_far, Pmode, NULL_TREE, 1))
3568 abort ();
3569 #endif
3571 locate_and_pad_parm (Pmode, NULL_TREE,
3572 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3574 #else
3575 argvec[count].reg != 0,
3576 #endif
3577 NULL_TREE, &args_size, &argvec[count].offset,
3578 &argvec[count].size, &alignment_pad);
3581 if (argvec[count].reg == 0 || argvec[count].partial != 0
3582 || reg_parm_stack_space > 0)
3583 args_size.constant += argvec[count].size.constant;
3585 FUNCTION_ARG_ADVANCE (args_so_far, Pmode, (tree) 0, 1);
3587 count++;
3590 for (; count < nargs; count++)
3592 rtx val = va_arg (p, rtx);
3593 enum machine_mode mode = va_arg (p, enum machine_mode);
3595 /* We cannot convert the arg value to the mode the library wants here;
3596 must do it earlier where we know the signedness of the arg. */
3597 if (mode == BLKmode
3598 || (GET_MODE (val) != mode && GET_MODE (val) != VOIDmode))
3599 abort ();
3601 /* On some machines, there's no way to pass a float to a library fcn.
3602 Pass it as a double instead. */
3603 #ifdef LIBGCC_NEEDS_DOUBLE
3604 if (LIBGCC_NEEDS_DOUBLE && mode == SFmode)
3605 val = convert_modes (DFmode, SFmode, val, 0), mode = DFmode;
3606 #endif
3608 /* There's no need to call protect_from_queue, because
3609 either emit_move_insn or emit_push_insn will do that. */
3611 /* Make sure it is a reasonable operand for a move or push insn. */
3612 if (GET_CODE (val) != REG && GET_CODE (val) != MEM
3613 && ! (CONSTANT_P (val) && LEGITIMATE_CONSTANT_P (val)))
3614 val = force_operand (val, NULL_RTX);
3616 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
3617 if (FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, mode, NULL_TREE, 1))
3619 /* We do not support FUNCTION_ARG_CALLEE_COPIES here since it can
3620 be viewed as just an efficiency improvement. */
3621 rtx slot = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
3622 emit_move_insn (slot, val);
3623 val = force_operand (XEXP (slot, 0), NULL_RTX);
3624 mode = Pmode;
3626 #endif
3628 argvec[count].value = val;
3629 argvec[count].mode = mode;
3631 argvec[count].reg = FUNCTION_ARG (args_so_far, mode, NULL_TREE, 1);
3633 #ifdef FUNCTION_ARG_PARTIAL_NREGS
3634 argvec[count].partial
3635 = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, mode, NULL_TREE, 1);
3636 #else
3637 argvec[count].partial = 0;
3638 #endif
3640 locate_and_pad_parm (mode, NULL_TREE,
3641 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3643 #else
3644 argvec[count].reg != 0,
3645 #endif
3646 NULL_TREE, &args_size, &argvec[count].offset,
3647 &argvec[count].size, &alignment_pad);
3649 if (argvec[count].size.var)
3650 abort ();
3652 if (reg_parm_stack_space == 0 && argvec[count].partial)
3653 argvec[count].size.constant -= argvec[count].partial * UNITS_PER_WORD;
3655 if (argvec[count].reg == 0 || argvec[count].partial != 0
3656 || reg_parm_stack_space > 0)
3657 args_size.constant += argvec[count].size.constant;
3659 FUNCTION_ARG_ADVANCE (args_so_far, mode, (tree) 0, 1);
3662 #ifdef FINAL_REG_PARM_STACK_SPACE
3663 reg_parm_stack_space = FINAL_REG_PARM_STACK_SPACE (args_size.constant,
3664 args_size.var);
3665 #endif
3666 /* If this machine requires an external definition for library
3667 functions, write one out. */
3668 assemble_external_libcall (fun);
3670 original_args_size = args_size;
3671 #ifdef PREFERRED_STACK_BOUNDARY
3672 args_size.constant = (((args_size.constant
3673 + stack_pointer_delta
3674 + STACK_BYTES - 1)
3675 / STACK_BYTES
3676 * STACK_BYTES)
3677 - stack_pointer_delta);
3678 #endif
3680 args_size.constant = MAX (args_size.constant,
3681 reg_parm_stack_space);
3683 #ifndef OUTGOING_REG_PARM_STACK_SPACE
3684 args_size.constant -= reg_parm_stack_space;
3685 #endif
3687 if (args_size.constant > current_function_outgoing_args_size)
3688 current_function_outgoing_args_size = args_size.constant;
3690 if (ACCUMULATE_OUTGOING_ARGS)
3692 /* Since the stack pointer will never be pushed, it is possible for
3693 the evaluation of a parm to clobber something we have already
3694 written to the stack. Since most function calls on RISC machines
3695 do not use the stack, this is uncommon, but must work correctly.
3697 Therefore, we save any area of the stack that was already written
3698 and that we are using. Here we set up to do this by making a new
3699 stack usage map from the old one.
3701 Another approach might be to try to reorder the argument
3702 evaluations to avoid this conflicting stack usage. */
3704 needed = args_size.constant;
3706 #ifndef OUTGOING_REG_PARM_STACK_SPACE
3707 /* Since we will be writing into the entire argument area, the
3708 map must be allocated for its entire size, not just the part that
3709 is the responsibility of the caller. */
3710 needed += reg_parm_stack_space;
3711 #endif
3713 #ifdef ARGS_GROW_DOWNWARD
3714 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3715 needed + 1);
3716 #else
3717 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3718 needed);
3719 #endif
3720 stack_usage_map = (char *) alloca (highest_outgoing_arg_in_use);
3722 if (initial_highest_arg_in_use)
3723 bcopy (initial_stack_usage_map, stack_usage_map,
3724 initial_highest_arg_in_use);
3726 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
3727 bzero (&stack_usage_map[initial_highest_arg_in_use],
3728 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
3729 needed = 0;
3731 /* The address of the outgoing argument list must not be copied to a
3732 register here, because argblock would be left pointing to the
3733 wrong place after the call to allocate_dynamic_stack_space below.
3736 argblock = virtual_outgoing_args_rtx;
3738 else
3740 if (!PUSH_ARGS)
3741 argblock = push_block (GEN_INT (args_size.constant), 0, 0);
3744 #ifdef PREFERRED_STACK_BOUNDARY
3745 /* If we push args individually in reverse order, perform stack alignment
3746 before the first push (the last arg). */
3747 if (argblock == 0 && PUSH_ARGS_REVERSED)
3748 anti_adjust_stack (GEN_INT (args_size.constant
3749 - original_args_size.constant));
3750 #endif
3752 if (PUSH_ARGS_REVERSED)
3754 inc = -1;
3755 argnum = nargs - 1;
3757 else
3759 inc = 1;
3760 argnum = 0;
3763 #ifdef REG_PARM_STACK_SPACE
3764 if (ACCUMULATE_OUTGOING_ARGS)
3766 /* The argument list is the property of the called routine and it
3767 may clobber it. If the fixed area has been used for previous
3768 parameters, we must save and restore it.
3770 Here we compute the boundary of the that needs to be saved, if any. */
3772 #ifdef ARGS_GROW_DOWNWARD
3773 for (count = 0; count < reg_parm_stack_space + 1; count++)
3774 #else
3775 for (count = 0; count < reg_parm_stack_space; count++)
3776 #endif
3778 if (count >= highest_outgoing_arg_in_use
3779 || stack_usage_map[count] == 0)
3780 continue;
3782 if (low_to_save == -1)
3783 low_to_save = count;
3785 high_to_save = count;
3788 if (low_to_save >= 0)
3790 int num_to_save = high_to_save - low_to_save + 1;
3791 enum machine_mode save_mode
3792 = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
3793 rtx stack_area;
3795 /* If we don't have the required alignment, must do this in BLKmode. */
3796 if ((low_to_save & (MIN (GET_MODE_SIZE (save_mode),
3797 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
3798 save_mode = BLKmode;
3800 #ifdef ARGS_GROW_DOWNWARD
3801 stack_area = gen_rtx_MEM (save_mode,
3802 memory_address (save_mode,
3803 plus_constant (argblock,
3804 - high_to_save)));
3805 #else
3806 stack_area = gen_rtx_MEM (save_mode,
3807 memory_address (save_mode,
3808 plus_constant (argblock,
3809 low_to_save)));
3810 #endif
3811 if (save_mode == BLKmode)
3813 save_area = assign_stack_temp (BLKmode, num_to_save, 0);
3814 emit_block_move (validize_mem (save_area), stack_area,
3815 GEN_INT (num_to_save), PARM_BOUNDARY);
3817 else
3819 save_area = gen_reg_rtx (save_mode);
3820 emit_move_insn (save_area, stack_area);
3824 #endif
3826 /* Push the args that need to be pushed. */
3828 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
3829 are to be pushed. */
3830 for (count = 0; count < nargs; count++, argnum += inc)
3832 register enum machine_mode mode = argvec[argnum].mode;
3833 register rtx val = argvec[argnum].value;
3834 rtx reg = argvec[argnum].reg;
3835 int partial = argvec[argnum].partial;
3836 int lower_bound = 0, upper_bound = 0, i;
3838 if (! (reg != 0 && partial == 0))
3840 if (ACCUMULATE_OUTGOING_ARGS)
3842 /* If this is being stored into a pre-allocated, fixed-size,
3843 stack area, save any previous data at that location. */
3845 #ifdef ARGS_GROW_DOWNWARD
3846 /* stack_slot is negative, but we want to index stack_usage_map
3847 with positive values. */
3848 upper_bound = -argvec[argnum].offset.constant + 1;
3849 lower_bound = upper_bound - argvec[argnum].size.constant;
3850 #else
3851 lower_bound = argvec[argnum].offset.constant;
3852 upper_bound = lower_bound + argvec[argnum].size.constant;
3853 #endif
3855 for (i = lower_bound; i < upper_bound; i++)
3856 if (stack_usage_map[i]
3857 /* Don't store things in the fixed argument area at this
3858 point; it has already been saved. */
3859 && i > reg_parm_stack_space)
3860 break;
3862 if (i != upper_bound)
3864 /* We need to make a save area. See what mode we can make
3865 it. */
3866 enum machine_mode save_mode
3867 = mode_for_size (argvec[argnum].size.constant
3868 * BITS_PER_UNIT,
3869 MODE_INT, 1);
3870 rtx stack_area
3871 = gen_rtx_MEM
3872 (save_mode,
3873 memory_address
3874 (save_mode,
3875 plus_constant (argblock,
3876 argvec[argnum].offset.constant)));
3877 argvec[argnum].save_area = gen_reg_rtx (save_mode);
3879 emit_move_insn (argvec[argnum].save_area, stack_area);
3883 emit_push_insn (val, mode, NULL_TREE, NULL_RTX, 0, partial, reg, 0,
3884 argblock, GEN_INT (argvec[argnum].offset.constant),
3885 reg_parm_stack_space, ARGS_SIZE_RTX (alignment_pad));
3887 /* Now mark the segment we just used. */
3888 if (ACCUMULATE_OUTGOING_ARGS)
3889 for (i = lower_bound; i < upper_bound; i++)
3890 stack_usage_map[i] = 1;
3892 NO_DEFER_POP;
3896 #ifdef PREFERRED_STACK_BOUNDARY
3897 /* If we pushed args in forward order, perform stack alignment
3898 after pushing the last arg. */
3899 if (argblock == 0 && !PUSH_ARGS_REVERSED)
3900 anti_adjust_stack (GEN_INT (args_size.constant
3901 - original_args_size.constant));
3902 #endif
3904 if (PUSH_ARGS_REVERSED)
3905 argnum = nargs - 1;
3906 else
3907 argnum = 0;
3909 fun = prepare_call_address (fun, NULL_TREE, &call_fusage, 0);
3911 /* Now load any reg parms into their regs. */
3913 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
3914 are to be pushed. */
3915 for (count = 0; count < nargs; count++, argnum += inc)
3917 register rtx val = argvec[argnum].value;
3918 rtx reg = argvec[argnum].reg;
3919 int partial = argvec[argnum].partial;
3921 /* Handle calls that pass values in multiple non-contiguous
3922 locations. The PA64 has examples of this for library calls. */
3923 if (reg != 0 && GET_CODE (reg) == PARALLEL)
3924 emit_group_load (reg, val,
3925 GET_MODE_SIZE (GET_MODE (val)),
3926 GET_MODE_ALIGNMENT (GET_MODE (val)));
3927 else if (reg != 0 && partial == 0)
3928 emit_move_insn (reg, val);
3930 NO_DEFER_POP;
3933 /* Any regs containing parms remain in use through the call. */
3934 for (count = 0; count < nargs; count++)
3936 rtx reg = argvec[count].reg;
3937 if (reg != 0 && GET_CODE (reg) == PARALLEL)
3938 use_group_regs (&call_fusage, reg);
3939 else if (reg != 0)
3940 use_reg (&call_fusage, reg);
3943 /* Pass the function the address in which to return a structure value. */
3944 if (mem_value != 0 && struct_value_rtx != 0 && ! pcc_struct_value)
3946 emit_move_insn (struct_value_rtx,
3947 force_reg (Pmode,
3948 force_operand (XEXP (mem_value, 0),
3949 NULL_RTX)));
3950 if (GET_CODE (struct_value_rtx) == REG)
3951 use_reg (&call_fusage, struct_value_rtx);
3954 /* Don't allow popping to be deferred, since then
3955 cse'ing of library calls could delete a call and leave the pop. */
3956 NO_DEFER_POP;
3957 valreg = (mem_value == 0 && outmode != VOIDmode
3958 ? hard_libcall_value (outmode) : NULL_RTX);
3960 #ifdef PREFERRED_STACK_BOUNDARY
3961 /* Stack must be properly aligned now. */
3962 if (stack_pointer_delta & (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1))
3963 abort();
3964 #endif
3966 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
3967 will set inhibit_defer_pop to that value. */
3968 /* The return type is needed to decide how many bytes the function pops.
3969 Signedness plays no role in that, so for simplicity, we pretend it's
3970 always signed. We also assume that the list of arguments passed has
3971 no impact, so we pretend it is unknown. */
3973 emit_call_1 (fun,
3974 get_identifier (XSTR (orgfun, 0)),
3975 build_function_type (outmode == VOIDmode ? void_type_node
3976 : type_for_mode (outmode, 0), NULL_TREE),
3977 original_args_size.constant, args_size.constant,
3978 struct_value_size,
3979 FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1),
3980 valreg,
3981 old_inhibit_defer_pop + 1, call_fusage, flags);
3983 /* Now restore inhibit_defer_pop to its actual original value. */
3984 OK_DEFER_POP;
3986 /* If call is cse'able, make appropriate pair of reg-notes around it.
3987 Test valreg so we don't crash; may safely ignore `const'
3988 if return type is void. Disable for PARALLEL return values, because
3989 we have no way to move such values into a pseudo register. */
3990 if ((flags & (ECF_CONST | ECF_PURE))
3991 && valreg != 0 && GET_CODE (valreg) != PARALLEL)
3993 rtx note = 0;
3994 rtx temp = gen_reg_rtx (GET_MODE (valreg));
3995 rtx insns;
3996 int i;
3998 /* Construct an "equal form" for the value which mentions all the
3999 arguments in order as well as the function name. */
4000 for (i = 0; i < nargs; i++)
4001 note = gen_rtx_EXPR_LIST (VOIDmode, argvec[i].value, note);
4002 note = gen_rtx_EXPR_LIST (VOIDmode, fun, note);
4004 insns = get_insns ();
4005 end_sequence ();
4007 if (flags & ECF_PURE)
4008 note = gen_rtx_EXPR_LIST (VOIDmode,
4009 gen_rtx_USE (VOIDmode,
4010 gen_rtx_MEM (BLKmode,
4011 gen_rtx_SCRATCH (VOIDmode))), note);
4013 emit_libcall_block (insns, temp, valreg, note);
4015 valreg = temp;
4017 else if (flags & (ECF_CONST | ECF_PURE))
4019 /* Otherwise, just write out the sequence without a note. */
4020 rtx insns = get_insns ();
4022 end_sequence ();
4023 emit_insns (insns);
4025 pop_temp_slots ();
4027 /* Copy the value to the right place. */
4028 if (outmode != VOIDmode && retval)
4030 if (mem_value)
4032 if (value == 0)
4033 value = mem_value;
4034 if (value != mem_value)
4035 emit_move_insn (value, mem_value);
4037 else if (value != 0)
4038 emit_move_insn (value, hard_libcall_value (outmode));
4039 else
4040 value = hard_libcall_value (outmode);
4043 if (ACCUMULATE_OUTGOING_ARGS)
4045 #ifdef REG_PARM_STACK_SPACE
4046 if (save_area)
4048 enum machine_mode save_mode = GET_MODE (save_area);
4049 #ifdef ARGS_GROW_DOWNWARD
4050 rtx stack_area
4051 = gen_rtx_MEM (save_mode,
4052 memory_address (save_mode,
4053 plus_constant (argblock,
4054 - high_to_save)));
4055 #else
4056 rtx stack_area
4057 = gen_rtx_MEM (save_mode,
4058 memory_address (save_mode,
4059 plus_constant (argblock, low_to_save)));
4060 #endif
4061 if (save_mode != BLKmode)
4062 emit_move_insn (stack_area, save_area);
4063 else
4064 emit_block_move (stack_area, validize_mem (save_area),
4065 GEN_INT (high_to_save - low_to_save + 1),
4066 PARM_BOUNDARY);
4068 #endif
4070 /* If we saved any argument areas, restore them. */
4071 for (count = 0; count < nargs; count++)
4072 if (argvec[count].save_area)
4074 enum machine_mode save_mode = GET_MODE (argvec[count].save_area);
4075 rtx stack_area
4076 = gen_rtx_MEM (save_mode,
4077 memory_address
4078 (save_mode,
4079 plus_constant (argblock,
4080 argvec[count].offset.constant)));
4082 emit_move_insn (stack_area, argvec[count].save_area);
4085 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
4086 stack_usage_map = initial_stack_usage_map;
4089 return value;
4093 /* Output a library call to function FUN (a SYMBOL_REF rtx)
4094 (emitting the queue unless NO_QUEUE is nonzero),
4095 for a value of mode OUTMODE,
4096 with NARGS different arguments, passed as alternating rtx values
4097 and machine_modes to convert them to.
4098 The rtx values should have been passed through protect_from_queue already.
4100 FN_TYPE will is zero for `normal' calls, one for `const' calls, wich
4101 which will be enclosed in REG_LIBCALL/REG_RETVAL notes and two for `pure'
4102 calls, that are handled like `const' calls with extra
4103 (use (memory (scratch)). */
4105 void
4106 emit_library_call VPARAMS((rtx orgfun, int fn_type, enum machine_mode outmode,
4107 int nargs, ...))
4109 #ifndef ANSI_PROTOTYPES
4110 rtx orgfun;
4111 int fn_type;
4112 enum machine_mode outmode;
4113 int nargs;
4114 #endif
4115 va_list p;
4117 VA_START (p, nargs);
4119 #ifndef ANSI_PROTOTYPES
4120 orgfun = va_arg (p, rtx);
4121 fn_type = va_arg (p, int);
4122 outmode = va_arg (p, enum machine_mode);
4123 nargs = va_arg (p, int);
4124 #endif
4126 emit_library_call_value_1 (0, orgfun, NULL_RTX, fn_type, outmode, nargs, p);
4128 va_end (p);
4131 /* Like emit_library_call except that an extra argument, VALUE,
4132 comes second and says where to store the result.
4133 (If VALUE is zero, this function chooses a convenient way
4134 to return the value.
4136 This function returns an rtx for where the value is to be found.
4137 If VALUE is nonzero, VALUE is returned. */
4140 emit_library_call_value VPARAMS((rtx orgfun, rtx value, int fn_type,
4141 enum machine_mode outmode, int nargs, ...))
4143 #ifndef ANSI_PROTOTYPES
4144 rtx orgfun;
4145 rtx value;
4146 int fn_type;
4147 enum machine_mode outmode;
4148 int nargs;
4149 #endif
4150 va_list p;
4152 VA_START (p, nargs);
4154 #ifndef ANSI_PROTOTYPES
4155 orgfun = va_arg (p, rtx);
4156 value = va_arg (p, rtx);
4157 fn_type = va_arg (p, int);
4158 outmode = va_arg (p, enum machine_mode);
4159 nargs = va_arg (p, int);
4160 #endif
4162 value = emit_library_call_value_1 (1, orgfun, value, fn_type, outmode, nargs, p);
4164 va_end (p);
4166 return value;
4169 #if 0
4170 /* Return an rtx which represents a suitable home on the stack
4171 given TYPE, the type of the argument looking for a home.
4172 This is called only for BLKmode arguments.
4174 SIZE is the size needed for this target.
4175 ARGS_ADDR is the address of the bottom of the argument block for this call.
4176 OFFSET describes this parameter's offset into ARGS_ADDR. It is meaningless
4177 if this machine uses push insns. */
4179 static rtx
4180 target_for_arg (type, size, args_addr, offset)
4181 tree type;
4182 rtx size;
4183 rtx args_addr;
4184 struct args_size offset;
4186 rtx target;
4187 rtx offset_rtx = ARGS_SIZE_RTX (offset);
4189 /* We do not call memory_address if possible,
4190 because we want to address as close to the stack
4191 as possible. For non-variable sized arguments,
4192 this will be stack-pointer relative addressing. */
4193 if (GET_CODE (offset_rtx) == CONST_INT)
4194 target = plus_constant (args_addr, INTVAL (offset_rtx));
4195 else
4197 /* I have no idea how to guarantee that this
4198 will work in the presence of register parameters. */
4199 target = gen_rtx_PLUS (Pmode, args_addr, offset_rtx);
4200 target = memory_address (QImode, target);
4203 return gen_rtx_MEM (BLKmode, target);
4205 #endif
4207 /* Store a single argument for a function call
4208 into the register or memory area where it must be passed.
4209 *ARG describes the argument value and where to pass it.
4211 ARGBLOCK is the address of the stack-block for all the arguments,
4212 or 0 on a machine where arguments are pushed individually.
4214 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
4215 so must be careful about how the stack is used.
4217 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
4218 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
4219 that we need not worry about saving and restoring the stack.
4221 FNDECL is the declaration of the function we are calling. */
4223 static void
4224 store_one_arg (arg, argblock, flags, variable_size,
4225 reg_parm_stack_space)
4226 struct arg_data *arg;
4227 rtx argblock;
4228 int flags;
4229 int variable_size ATTRIBUTE_UNUSED;
4230 int reg_parm_stack_space;
4232 register tree pval = arg->tree_value;
4233 rtx reg = 0;
4234 int partial = 0;
4235 int used = 0;
4236 int i, lower_bound = 0, upper_bound = 0;
4238 if (TREE_CODE (pval) == ERROR_MARK)
4239 return;
4241 /* Push a new temporary level for any temporaries we make for
4242 this argument. */
4243 push_temp_slots ();
4245 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
4247 /* If this is being stored into a pre-allocated, fixed-size, stack area,
4248 save any previous data at that location. */
4249 if (argblock && ! variable_size && arg->stack)
4251 #ifdef ARGS_GROW_DOWNWARD
4252 /* stack_slot is negative, but we want to index stack_usage_map
4253 with positive values. */
4254 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4255 upper_bound = -INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1)) + 1;
4256 else
4257 upper_bound = 0;
4259 lower_bound = upper_bound - arg->size.constant;
4260 #else
4261 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4262 lower_bound = INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1));
4263 else
4264 lower_bound = 0;
4266 upper_bound = lower_bound + arg->size.constant;
4267 #endif
4269 for (i = lower_bound; i < upper_bound; i++)
4270 if (stack_usage_map[i]
4271 /* Don't store things in the fixed argument area at this point;
4272 it has already been saved. */
4273 && i > reg_parm_stack_space)
4274 break;
4276 if (i != upper_bound)
4278 /* We need to make a save area. See what mode we can make it. */
4279 enum machine_mode save_mode
4280 = mode_for_size (arg->size.constant * BITS_PER_UNIT, MODE_INT, 1);
4281 rtx stack_area
4282 = gen_rtx_MEM (save_mode,
4283 memory_address (save_mode,
4284 XEXP (arg->stack_slot, 0)));
4286 if (save_mode == BLKmode)
4288 arg->save_area = assign_stack_temp (BLKmode,
4289 arg->size.constant, 0);
4290 MEM_SET_IN_STRUCT_P (arg->save_area,
4291 AGGREGATE_TYPE_P (TREE_TYPE
4292 (arg->tree_value)));
4293 preserve_temp_slots (arg->save_area);
4294 emit_block_move (validize_mem (arg->save_area), stack_area,
4295 GEN_INT (arg->size.constant),
4296 PARM_BOUNDARY);
4298 else
4300 arg->save_area = gen_reg_rtx (save_mode);
4301 emit_move_insn (arg->save_area, stack_area);
4305 /* Now that we have saved any slots that will be overwritten by this
4306 store, mark all slots this store will use. We must do this before
4307 we actually expand the argument since the expansion itself may
4308 trigger library calls which might need to use the same stack slot. */
4309 if (argblock && ! variable_size && arg->stack)
4310 for (i = lower_bound; i < upper_bound; i++)
4311 stack_usage_map[i] = 1;
4314 /* If this isn't going to be placed on both the stack and in registers,
4315 set up the register and number of words. */
4316 if (! arg->pass_on_stack)
4317 reg = arg->reg, partial = arg->partial;
4319 if (reg != 0 && partial == 0)
4320 /* Being passed entirely in a register. We shouldn't be called in
4321 this case. */
4322 abort ();
4324 /* If this arg needs special alignment, don't load the registers
4325 here. */
4326 if (arg->n_aligned_regs != 0)
4327 reg = 0;
4329 /* If this is being passed partially in a register, we can't evaluate
4330 it directly into its stack slot. Otherwise, we can. */
4331 if (arg->value == 0)
4333 /* stack_arg_under_construction is nonzero if a function argument is
4334 being evaluated directly into the outgoing argument list and
4335 expand_call must take special action to preserve the argument list
4336 if it is called recursively.
4338 For scalar function arguments stack_usage_map is sufficient to
4339 determine which stack slots must be saved and restored. Scalar
4340 arguments in general have pass_on_stack == 0.
4342 If this argument is initialized by a function which takes the
4343 address of the argument (a C++ constructor or a C function
4344 returning a BLKmode structure), then stack_usage_map is
4345 insufficient and expand_call must push the stack around the
4346 function call. Such arguments have pass_on_stack == 1.
4348 Note that it is always safe to set stack_arg_under_construction,
4349 but this generates suboptimal code if set when not needed. */
4351 if (arg->pass_on_stack)
4352 stack_arg_under_construction++;
4354 arg->value = expand_expr (pval,
4355 (partial
4356 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
4357 ? NULL_RTX : arg->stack,
4358 VOIDmode, 0);
4360 /* If we are promoting object (or for any other reason) the mode
4361 doesn't agree, convert the mode. */
4363 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
4364 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
4365 arg->value, arg->unsignedp);
4367 if (arg->pass_on_stack)
4368 stack_arg_under_construction--;
4371 /* Don't allow anything left on stack from computation
4372 of argument to alloca. */
4373 if (flags & ECF_MAY_BE_ALLOCA)
4374 do_pending_stack_adjust ();
4376 if (arg->value == arg->stack)
4378 /* If the value is already in the stack slot, we are done. */
4379 if (current_function_check_memory_usage && GET_CODE (arg->stack) == MEM)
4381 emit_library_call (chkr_set_right_libfunc, 1, VOIDmode, 3,
4382 XEXP (arg->stack, 0), Pmode,
4383 ARGS_SIZE_RTX (arg->size),
4384 TYPE_MODE (sizetype),
4385 GEN_INT (MEMORY_USE_RW),
4386 TYPE_MODE (integer_type_node));
4389 else if (arg->mode != BLKmode)
4391 register int size;
4393 /* Argument is a scalar, not entirely passed in registers.
4394 (If part is passed in registers, arg->partial says how much
4395 and emit_push_insn will take care of putting it there.)
4397 Push it, and if its size is less than the
4398 amount of space allocated to it,
4399 also bump stack pointer by the additional space.
4400 Note that in C the default argument promotions
4401 will prevent such mismatches. */
4403 size = GET_MODE_SIZE (arg->mode);
4404 /* Compute how much space the push instruction will push.
4405 On many machines, pushing a byte will advance the stack
4406 pointer by a halfword. */
4407 #ifdef PUSH_ROUNDING
4408 size = PUSH_ROUNDING (size);
4409 #endif
4410 used = size;
4412 /* Compute how much space the argument should get:
4413 round up to a multiple of the alignment for arguments. */
4414 if (none != FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)))
4415 used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
4416 / (PARM_BOUNDARY / BITS_PER_UNIT))
4417 * (PARM_BOUNDARY / BITS_PER_UNIT));
4419 /* This isn't already where we want it on the stack, so put it there.
4420 This can either be done with push or copy insns. */
4421 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), NULL_RTX, 0,
4422 partial, reg, used - size, argblock,
4423 ARGS_SIZE_RTX (arg->offset), reg_parm_stack_space,
4424 ARGS_SIZE_RTX (arg->alignment_pad));
4426 else
4428 /* BLKmode, at least partly to be pushed. */
4430 register int excess;
4431 rtx size_rtx;
4433 /* Pushing a nonscalar.
4434 If part is passed in registers, PARTIAL says how much
4435 and emit_push_insn will take care of putting it there. */
4437 /* Round its size up to a multiple
4438 of the allocation unit for arguments. */
4440 if (arg->size.var != 0)
4442 excess = 0;
4443 size_rtx = ARGS_SIZE_RTX (arg->size);
4445 else
4447 /* PUSH_ROUNDING has no effect on us, because
4448 emit_push_insn for BLKmode is careful to avoid it. */
4449 excess = (arg->size.constant - int_size_in_bytes (TREE_TYPE (pval))
4450 + partial * UNITS_PER_WORD);
4451 size_rtx = expr_size (pval);
4454 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
4455 TYPE_ALIGN (TREE_TYPE (pval)), partial, reg, excess,
4456 argblock, ARGS_SIZE_RTX (arg->offset),
4457 reg_parm_stack_space,
4458 ARGS_SIZE_RTX (arg->alignment_pad));
4462 /* Unless this is a partially-in-register argument, the argument is now
4463 in the stack.
4465 ??? Note that this can change arg->value from arg->stack to
4466 arg->stack_slot and it matters when they are not the same.
4467 It isn't totally clear that this is correct in all cases. */
4468 if (partial == 0)
4469 arg->value = arg->stack_slot;
4471 /* Once we have pushed something, pops can't safely
4472 be deferred during the rest of the arguments. */
4473 NO_DEFER_POP;
4475 /* ANSI doesn't require a sequence point here,
4476 but PCC has one, so this will avoid some problems. */
4477 emit_queue ();
4479 /* Free any temporary slots made in processing this argument. Show
4480 that we might have taken the address of something and pushed that
4481 as an operand. */
4482 preserve_temp_slots (NULL_RTX);
4483 free_temp_slots ();
4484 pop_temp_slots ();