2013-10-31 Steve Ellcey <sellcey@mips.com>
[official-gcc.git] / gcc / calls.c
blobc04ed32801fb3cfa9e09c6e0995f4b689dd7e2d1
1 /* Convert function calls to rtl insns, for GNU C compiler.
2 Copyright (C) 1989-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "flags.h"
28 #include "expr.h"
29 #include "optabs.h"
30 #include "libfuncs.h"
31 #include "function.h"
32 #include "regs.h"
33 #include "diagnostic-core.h"
34 #include "output.h"
35 #include "tm_p.h"
36 #include "timevar.h"
37 #include "sbitmap.h"
38 #include "langhooks.h"
39 #include "target.h"
40 #include "cgraph.h"
41 #include "except.h"
42 #include "dbgcnt.h"
44 /* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits. */
45 #define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
47 /* Data structure and subroutines used within expand_call. */
49 struct arg_data
51 /* Tree node for this argument. */
52 tree tree_value;
53 /* Mode for value; TYPE_MODE unless promoted. */
54 enum machine_mode mode;
55 /* Current RTL value for argument, or 0 if it isn't precomputed. */
56 rtx value;
57 /* Initially-compute RTL value for argument; only for const functions. */
58 rtx initial_value;
59 /* Register to pass this argument in, 0 if passed on stack, or an
60 PARALLEL if the arg is to be copied into multiple non-contiguous
61 registers. */
62 rtx reg;
63 /* Register to pass this argument in when generating tail call sequence.
64 This is not the same register as for normal calls on machines with
65 register windows. */
66 rtx tail_call_reg;
67 /* If REG is a PARALLEL, this is a copy of VALUE pulled into the correct
68 form for emit_group_move. */
69 rtx parallel_value;
70 /* If REG was promoted from the actual mode of the argument expression,
71 indicates whether the promotion is sign- or zero-extended. */
72 int unsignedp;
73 /* Number of bytes to put in registers. 0 means put the whole arg
74 in registers. Also 0 if not passed in registers. */
75 int partial;
76 /* Nonzero if argument must be passed on stack.
77 Note that some arguments may be passed on the stack
78 even though pass_on_stack is zero, just because FUNCTION_ARG says so.
79 pass_on_stack identifies arguments that *cannot* go in registers. */
80 int pass_on_stack;
81 /* Some fields packaged up for locate_and_pad_parm. */
82 struct locate_and_pad_arg_data locate;
83 /* Location on the stack at which parameter should be stored. The store
84 has already been done if STACK == VALUE. */
85 rtx stack;
86 /* Location on the stack of the start of this argument slot. This can
87 differ from STACK if this arg pads downward. This location is known
88 to be aligned to TARGET_FUNCTION_ARG_BOUNDARY. */
89 rtx stack_slot;
90 /* Place that this stack area has been saved, if needed. */
91 rtx save_area;
92 /* If an argument's alignment does not permit direct copying into registers,
93 copy in smaller-sized pieces into pseudos. These are stored in a
94 block pointed to by this field. The next field says how many
95 word-sized pseudos we made. */
96 rtx *aligned_regs;
97 int n_aligned_regs;
100 /* A vector of one char per byte of stack space. A byte if nonzero if
101 the corresponding stack location has been used.
102 This vector is used to prevent a function call within an argument from
103 clobbering any stack already set up. */
104 static char *stack_usage_map;
106 /* Size of STACK_USAGE_MAP. */
107 static int highest_outgoing_arg_in_use;
109 /* A bitmap of virtual-incoming stack space. Bit is set if the corresponding
110 stack location's tail call argument has been already stored into the stack.
111 This bitmap is used to prevent sibling call optimization if function tries
112 to use parent's incoming argument slots when they have been already
113 overwritten with tail call arguments. */
114 static sbitmap stored_args_map;
116 /* stack_arg_under_construction is nonzero when an argument may be
117 initialized with a constructor call (including a C function that
118 returns a BLKmode struct) and expand_call must take special action
119 to make sure the object being constructed does not overlap the
120 argument list for the constructor call. */
121 static int stack_arg_under_construction;
123 static void emit_call_1 (rtx, tree, tree, tree, HOST_WIDE_INT, HOST_WIDE_INT,
124 HOST_WIDE_INT, rtx, rtx, int, rtx, int,
125 cumulative_args_t);
126 static void precompute_register_parameters (int, struct arg_data *, int *);
127 static int store_one_arg (struct arg_data *, rtx, int, int, int);
128 static void store_unaligned_arguments_into_pseudos (struct arg_data *, int);
129 static int finalize_must_preallocate (int, int, struct arg_data *,
130 struct args_size *);
131 static void precompute_arguments (int, struct arg_data *);
132 static int compute_argument_block_size (int, struct args_size *, tree, tree, int);
133 static void initialize_argument_information (int, struct arg_data *,
134 struct args_size *, int,
135 tree, tree,
136 tree, tree, cumulative_args_t, int,
137 rtx *, int *, int *, int *,
138 bool *, bool);
139 static void compute_argument_addresses (struct arg_data *, rtx, int);
140 static rtx rtx_for_function_call (tree, tree);
141 static void load_register_parameters (struct arg_data *, int, rtx *, int,
142 int, int *);
143 static rtx emit_library_call_value_1 (int, rtx, rtx, enum libcall_type,
144 enum machine_mode, int, va_list);
145 static int special_function_p (const_tree, int);
146 static int check_sibcall_argument_overlap_1 (rtx);
147 static int check_sibcall_argument_overlap (rtx, struct arg_data *, int);
149 static int combine_pending_stack_adjustment_and_call (int, struct args_size *,
150 unsigned int);
151 static tree split_complex_types (tree);
153 #ifdef REG_PARM_STACK_SPACE
154 static rtx save_fixed_argument_area (int, rtx, int *, int *);
155 static void restore_fixed_argument_area (rtx, rtx, int, int);
156 #endif
158 /* Force FUNEXP into a form suitable for the address of a CALL,
159 and return that as an rtx. Also load the static chain register
160 if FNDECL is a nested function.
162 CALL_FUSAGE points to a variable holding the prospective
163 CALL_INSN_FUNCTION_USAGE information. */
166 prepare_call_address (tree fndecl, rtx funexp, rtx static_chain_value,
167 rtx *call_fusage, int reg_parm_seen, int sibcallp)
169 /* Make a valid memory address and copy constants through pseudo-regs,
170 but not for a constant address if -fno-function-cse. */
171 if (GET_CODE (funexp) != SYMBOL_REF)
172 /* If we are using registers for parameters, force the
173 function address into a register now. */
174 funexp = ((reg_parm_seen
175 && targetm.small_register_classes_for_mode_p (FUNCTION_MODE))
176 ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
177 : memory_address (FUNCTION_MODE, funexp));
178 else if (! sibcallp)
180 #ifndef NO_FUNCTION_CSE
181 if (optimize && ! flag_no_function_cse)
182 funexp = force_reg (Pmode, funexp);
183 #endif
186 if (static_chain_value != 0)
188 rtx chain;
190 gcc_assert (fndecl);
191 chain = targetm.calls.static_chain (fndecl, false);
192 static_chain_value = convert_memory_address (Pmode, static_chain_value);
194 emit_move_insn (chain, static_chain_value);
195 if (REG_P (chain))
196 use_reg (call_fusage, chain);
199 return funexp;
202 /* Generate instructions to call function FUNEXP,
203 and optionally pop the results.
204 The CALL_INSN is the first insn generated.
206 FNDECL is the declaration node of the function. This is given to the
207 hook TARGET_RETURN_POPS_ARGS to determine whether this function pops
208 its own args.
210 FUNTYPE is the data type of the function. This is given to the hook
211 TARGET_RETURN_POPS_ARGS to determine whether this function pops its
212 own args. We used to allow an identifier for library functions, but
213 that doesn't work when the return type is an aggregate type and the
214 calling convention says that the pointer to this aggregate is to be
215 popped by the callee.
217 STACK_SIZE is the number of bytes of arguments on the stack,
218 ROUNDED_STACK_SIZE is that number rounded up to
219 PREFERRED_STACK_BOUNDARY; zero if the size is variable. This is
220 both to put into the call insn and to generate explicit popping
221 code if necessary.
223 STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
224 It is zero if this call doesn't want a structure value.
226 NEXT_ARG_REG is the rtx that results from executing
227 targetm.calls.function_arg (&args_so_far, VOIDmode, void_type_node, true)
228 just after all the args have had their registers assigned.
229 This could be whatever you like, but normally it is the first
230 arg-register beyond those used for args in this call,
231 or 0 if all the arg-registers are used in this call.
232 It is passed on to `gen_call' so you can put this info in the call insn.
234 VALREG is a hard register in which a value is returned,
235 or 0 if the call does not return a value.
237 OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
238 the args to this call were processed.
239 We restore `inhibit_defer_pop' to that value.
241 CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
242 denote registers used by the called function. */
244 static void
245 emit_call_1 (rtx funexp, tree fntree ATTRIBUTE_UNUSED, tree fndecl ATTRIBUTE_UNUSED,
246 tree funtype ATTRIBUTE_UNUSED,
247 HOST_WIDE_INT stack_size ATTRIBUTE_UNUSED,
248 HOST_WIDE_INT rounded_stack_size,
249 HOST_WIDE_INT struct_value_size ATTRIBUTE_UNUSED,
250 rtx next_arg_reg ATTRIBUTE_UNUSED, rtx valreg,
251 int old_inhibit_defer_pop, rtx call_fusage, int ecf_flags,
252 cumulative_args_t args_so_far ATTRIBUTE_UNUSED)
254 rtx rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
255 rtx call_insn, call, funmem;
256 int already_popped = 0;
257 HOST_WIDE_INT n_popped
258 = targetm.calls.return_pops_args (fndecl, funtype, stack_size);
260 #ifdef CALL_POPS_ARGS
261 n_popped += CALL_POPS_ARGS (*get_cumulative_args (args_so_far));
262 #endif
264 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
265 and we don't want to load it into a register as an optimization,
266 because prepare_call_address already did it if it should be done. */
267 if (GET_CODE (funexp) != SYMBOL_REF)
268 funexp = memory_address (FUNCTION_MODE, funexp);
270 funmem = gen_rtx_MEM (FUNCTION_MODE, funexp);
271 if (fndecl && TREE_CODE (fndecl) == FUNCTION_DECL)
273 tree t = fndecl;
275 /* Although a built-in FUNCTION_DECL and its non-__builtin
276 counterpart compare equal and get a shared mem_attrs, they
277 produce different dump output in compare-debug compilations,
278 if an entry gets garbage collected in one compilation, then
279 adds a different (but equivalent) entry, while the other
280 doesn't run the garbage collector at the same spot and then
281 shares the mem_attr with the equivalent entry. */
282 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
284 tree t2 = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
285 if (t2)
286 t = t2;
289 set_mem_expr (funmem, t);
291 else if (fntree)
292 set_mem_expr (funmem, build_simple_mem_ref (CALL_EXPR_FN (fntree)));
294 #if defined (HAVE_sibcall_pop) && defined (HAVE_sibcall_value_pop)
295 if ((ecf_flags & ECF_SIBCALL)
296 && HAVE_sibcall_pop && HAVE_sibcall_value_pop
297 && (n_popped > 0 || stack_size == 0))
299 rtx n_pop = GEN_INT (n_popped);
300 rtx pat;
302 /* If this subroutine pops its own args, record that in the call insn
303 if possible, for the sake of frame pointer elimination. */
305 if (valreg)
306 pat = GEN_SIBCALL_VALUE_POP (valreg, funmem, rounded_stack_size_rtx,
307 next_arg_reg, n_pop);
308 else
309 pat = GEN_SIBCALL_POP (funmem, rounded_stack_size_rtx, next_arg_reg,
310 n_pop);
312 emit_call_insn (pat);
313 already_popped = 1;
315 else
316 #endif
318 #if defined (HAVE_call_pop) && defined (HAVE_call_value_pop)
319 /* If the target has "call" or "call_value" insns, then prefer them
320 if no arguments are actually popped. If the target does not have
321 "call" or "call_value" insns, then we must use the popping versions
322 even if the call has no arguments to pop. */
323 #if defined (HAVE_call) && defined (HAVE_call_value)
324 if (HAVE_call && HAVE_call_value && HAVE_call_pop && HAVE_call_value_pop
325 && n_popped > 0)
326 #else
327 if (HAVE_call_pop && HAVE_call_value_pop)
328 #endif
330 rtx n_pop = GEN_INT (n_popped);
331 rtx pat;
333 /* If this subroutine pops its own args, record that in the call insn
334 if possible, for the sake of frame pointer elimination. */
336 if (valreg)
337 pat = GEN_CALL_VALUE_POP (valreg, funmem, rounded_stack_size_rtx,
338 next_arg_reg, n_pop);
339 else
340 pat = GEN_CALL_POP (funmem, rounded_stack_size_rtx, next_arg_reg,
341 n_pop);
343 emit_call_insn (pat);
344 already_popped = 1;
346 else
347 #endif
349 #if defined (HAVE_sibcall) && defined (HAVE_sibcall_value)
350 if ((ecf_flags & ECF_SIBCALL)
351 && HAVE_sibcall && HAVE_sibcall_value)
353 if (valreg)
354 emit_call_insn (GEN_SIBCALL_VALUE (valreg, funmem,
355 rounded_stack_size_rtx,
356 next_arg_reg, NULL_RTX));
357 else
358 emit_call_insn (GEN_SIBCALL (funmem, rounded_stack_size_rtx,
359 next_arg_reg,
360 GEN_INT (struct_value_size)));
362 else
363 #endif
365 #if defined (HAVE_call) && defined (HAVE_call_value)
366 if (HAVE_call && HAVE_call_value)
368 if (valreg)
369 emit_call_insn (GEN_CALL_VALUE (valreg, funmem, rounded_stack_size_rtx,
370 next_arg_reg, NULL_RTX));
371 else
372 emit_call_insn (GEN_CALL (funmem, rounded_stack_size_rtx, next_arg_reg,
373 GEN_INT (struct_value_size)));
375 else
376 #endif
377 gcc_unreachable ();
379 /* Find the call we just emitted. */
380 call_insn = last_call_insn ();
382 /* Some target create a fresh MEM instead of reusing the one provided
383 above. Set its MEM_EXPR. */
384 call = get_call_rtx_from (call_insn);
385 if (call
386 && MEM_EXPR (XEXP (call, 0)) == NULL_TREE
387 && MEM_EXPR (funmem) != NULL_TREE)
388 set_mem_expr (XEXP (call, 0), MEM_EXPR (funmem));
390 /* Put the register usage information there. */
391 add_function_usage_to (call_insn, call_fusage);
393 /* If this is a const call, then set the insn's unchanging bit. */
394 if (ecf_flags & ECF_CONST)
395 RTL_CONST_CALL_P (call_insn) = 1;
397 /* If this is a pure call, then set the insn's unchanging bit. */
398 if (ecf_flags & ECF_PURE)
399 RTL_PURE_CALL_P (call_insn) = 1;
401 /* If this is a const call, then set the insn's unchanging bit. */
402 if (ecf_flags & ECF_LOOPING_CONST_OR_PURE)
403 RTL_LOOPING_CONST_OR_PURE_CALL_P (call_insn) = 1;
405 /* Create a nothrow REG_EH_REGION note, if needed. */
406 make_reg_eh_region_note (call_insn, ecf_flags, 0);
408 if (ecf_flags & ECF_NORETURN)
409 add_reg_note (call_insn, REG_NORETURN, const0_rtx);
411 if (ecf_flags & ECF_RETURNS_TWICE)
413 add_reg_note (call_insn, REG_SETJMP, const0_rtx);
414 cfun->calls_setjmp = 1;
417 SIBLING_CALL_P (call_insn) = ((ecf_flags & ECF_SIBCALL) != 0);
419 /* Restore this now, so that we do defer pops for this call's args
420 if the context of the call as a whole permits. */
421 inhibit_defer_pop = old_inhibit_defer_pop;
423 if (n_popped > 0)
425 if (!already_popped)
426 CALL_INSN_FUNCTION_USAGE (call_insn)
427 = gen_rtx_EXPR_LIST (VOIDmode,
428 gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx),
429 CALL_INSN_FUNCTION_USAGE (call_insn));
430 rounded_stack_size -= n_popped;
431 rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
432 stack_pointer_delta -= n_popped;
434 add_reg_note (call_insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
436 /* If popup is needed, stack realign must use DRAP */
437 if (SUPPORTS_STACK_ALIGNMENT)
438 crtl->need_drap = true;
440 /* For noreturn calls when not accumulating outgoing args force
441 REG_ARGS_SIZE note to prevent crossjumping of calls with different
442 args sizes. */
443 else if (!ACCUMULATE_OUTGOING_ARGS && (ecf_flags & ECF_NORETURN) != 0)
444 add_reg_note (call_insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
446 if (!ACCUMULATE_OUTGOING_ARGS)
448 /* If returning from the subroutine does not automatically pop the args,
449 we need an instruction to pop them sooner or later.
450 Perhaps do it now; perhaps just record how much space to pop later.
452 If returning from the subroutine does pop the args, indicate that the
453 stack pointer will be changed. */
455 if (rounded_stack_size != 0)
457 if (ecf_flags & ECF_NORETURN)
458 /* Just pretend we did the pop. */
459 stack_pointer_delta -= rounded_stack_size;
460 else if (flag_defer_pop && inhibit_defer_pop == 0
461 && ! (ecf_flags & (ECF_CONST | ECF_PURE)))
462 pending_stack_adjust += rounded_stack_size;
463 else
464 adjust_stack (rounded_stack_size_rtx);
467 /* When we accumulate outgoing args, we must avoid any stack manipulations.
468 Restore the stack pointer to its original value now. Usually
469 ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
470 On i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
471 popping variants of functions exist as well.
473 ??? We may optimize similar to defer_pop above, but it is
474 probably not worthwhile.
476 ??? It will be worthwhile to enable combine_stack_adjustments even for
477 such machines. */
478 else if (n_popped)
479 anti_adjust_stack (GEN_INT (n_popped));
482 /* Determine if the function identified by NAME and FNDECL is one with
483 special properties we wish to know about.
485 For example, if the function might return more than one time (setjmp), then
486 set RETURNS_TWICE to a nonzero value.
488 Similarly set NORETURN if the function is in the longjmp family.
490 Set MAY_BE_ALLOCA for any memory allocation function that might allocate
491 space from the stack such as alloca. */
493 static int
494 special_function_p (const_tree fndecl, int flags)
496 if (fndecl && DECL_NAME (fndecl)
497 && IDENTIFIER_LENGTH (DECL_NAME (fndecl)) <= 17
498 /* Exclude functions not at the file scope, or not `extern',
499 since they are not the magic functions we would otherwise
500 think they are.
501 FIXME: this should be handled with attributes, not with this
502 hacky imitation of DECL_ASSEMBLER_NAME. It's (also) wrong
503 because you can declare fork() inside a function if you
504 wish. */
505 && (DECL_CONTEXT (fndecl) == NULL_TREE
506 || TREE_CODE (DECL_CONTEXT (fndecl)) == TRANSLATION_UNIT_DECL)
507 && TREE_PUBLIC (fndecl))
509 const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
510 const char *tname = name;
512 /* We assume that alloca will always be called by name. It
513 makes no sense to pass it as a pointer-to-function to
514 anything that does not understand its behavior. */
515 if (((IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 6
516 && name[0] == 'a'
517 && ! strcmp (name, "alloca"))
518 || (IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 16
519 && name[0] == '_'
520 && ! strcmp (name, "__builtin_alloca"))))
521 flags |= ECF_MAY_BE_ALLOCA;
523 /* Disregard prefix _, __, __x or __builtin_. */
524 if (name[0] == '_')
526 if (name[1] == '_'
527 && name[2] == 'b'
528 && !strncmp (name + 3, "uiltin_", 7))
529 tname += 10;
530 else if (name[1] == '_' && name[2] == 'x')
531 tname += 3;
532 else if (name[1] == '_')
533 tname += 2;
534 else
535 tname += 1;
538 if (tname[0] == 's')
540 if ((tname[1] == 'e'
541 && (! strcmp (tname, "setjmp")
542 || ! strcmp (tname, "setjmp_syscall")))
543 || (tname[1] == 'i'
544 && ! strcmp (tname, "sigsetjmp"))
545 || (tname[1] == 'a'
546 && ! strcmp (tname, "savectx")))
547 flags |= ECF_RETURNS_TWICE | ECF_LEAF;
549 if (tname[1] == 'i'
550 && ! strcmp (tname, "siglongjmp"))
551 flags |= ECF_NORETURN;
553 else if ((tname[0] == 'q' && tname[1] == 's'
554 && ! strcmp (tname, "qsetjmp"))
555 || (tname[0] == 'v' && tname[1] == 'f'
556 && ! strcmp (tname, "vfork"))
557 || (tname[0] == 'g' && tname[1] == 'e'
558 && !strcmp (tname, "getcontext")))
559 flags |= ECF_RETURNS_TWICE | ECF_LEAF;
561 else if (tname[0] == 'l' && tname[1] == 'o'
562 && ! strcmp (tname, "longjmp"))
563 flags |= ECF_NORETURN;
566 return flags;
569 /* Similar to special_function_p; return a set of ERF_ flags for the
570 function FNDECL. */
571 static int
572 decl_return_flags (tree fndecl)
574 tree attr;
575 tree type = TREE_TYPE (fndecl);
576 if (!type)
577 return 0;
579 attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
580 if (!attr)
581 return 0;
583 attr = TREE_VALUE (TREE_VALUE (attr));
584 if (!attr || TREE_STRING_LENGTH (attr) < 1)
585 return 0;
587 switch (TREE_STRING_POINTER (attr)[0])
589 case '1':
590 case '2':
591 case '3':
592 case '4':
593 return ERF_RETURNS_ARG | (TREE_STRING_POINTER (attr)[0] - '1');
595 case 'm':
596 return ERF_NOALIAS;
598 case '.':
599 default:
600 return 0;
604 /* Return nonzero when FNDECL represents a call to setjmp. */
607 setjmp_call_p (const_tree fndecl)
609 if (DECL_IS_RETURNS_TWICE (fndecl))
610 return ECF_RETURNS_TWICE;
611 return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
615 /* Return true if STMT is an alloca call. */
617 bool
618 gimple_alloca_call_p (const_gimple stmt)
620 tree fndecl;
622 if (!is_gimple_call (stmt))
623 return false;
625 fndecl = gimple_call_fndecl (stmt);
626 if (fndecl && (special_function_p (fndecl, 0) & ECF_MAY_BE_ALLOCA))
627 return true;
629 return false;
632 /* Return true when exp contains alloca call. */
634 bool
635 alloca_call_p (const_tree exp)
637 tree fndecl;
638 if (TREE_CODE (exp) == CALL_EXPR
639 && (fndecl = get_callee_fndecl (exp))
640 && (special_function_p (fndecl, 0) & ECF_MAY_BE_ALLOCA))
641 return true;
642 return false;
645 /* Return TRUE if FNDECL is either a TM builtin or a TM cloned
646 function. Return FALSE otherwise. */
648 static bool
649 is_tm_builtin (const_tree fndecl)
651 if (fndecl == NULL)
652 return false;
654 if (decl_is_tm_clone (fndecl))
655 return true;
657 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
659 switch (DECL_FUNCTION_CODE (fndecl))
661 case BUILT_IN_TM_COMMIT:
662 case BUILT_IN_TM_COMMIT_EH:
663 case BUILT_IN_TM_ABORT:
664 case BUILT_IN_TM_IRREVOCABLE:
665 case BUILT_IN_TM_GETTMCLONE_IRR:
666 case BUILT_IN_TM_MEMCPY:
667 case BUILT_IN_TM_MEMMOVE:
668 case BUILT_IN_TM_MEMSET:
669 CASE_BUILT_IN_TM_STORE (1):
670 CASE_BUILT_IN_TM_STORE (2):
671 CASE_BUILT_IN_TM_STORE (4):
672 CASE_BUILT_IN_TM_STORE (8):
673 CASE_BUILT_IN_TM_STORE (FLOAT):
674 CASE_BUILT_IN_TM_STORE (DOUBLE):
675 CASE_BUILT_IN_TM_STORE (LDOUBLE):
676 CASE_BUILT_IN_TM_STORE (M64):
677 CASE_BUILT_IN_TM_STORE (M128):
678 CASE_BUILT_IN_TM_STORE (M256):
679 CASE_BUILT_IN_TM_LOAD (1):
680 CASE_BUILT_IN_TM_LOAD (2):
681 CASE_BUILT_IN_TM_LOAD (4):
682 CASE_BUILT_IN_TM_LOAD (8):
683 CASE_BUILT_IN_TM_LOAD (FLOAT):
684 CASE_BUILT_IN_TM_LOAD (DOUBLE):
685 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
686 CASE_BUILT_IN_TM_LOAD (M64):
687 CASE_BUILT_IN_TM_LOAD (M128):
688 CASE_BUILT_IN_TM_LOAD (M256):
689 case BUILT_IN_TM_LOG:
690 case BUILT_IN_TM_LOG_1:
691 case BUILT_IN_TM_LOG_2:
692 case BUILT_IN_TM_LOG_4:
693 case BUILT_IN_TM_LOG_8:
694 case BUILT_IN_TM_LOG_FLOAT:
695 case BUILT_IN_TM_LOG_DOUBLE:
696 case BUILT_IN_TM_LOG_LDOUBLE:
697 case BUILT_IN_TM_LOG_M64:
698 case BUILT_IN_TM_LOG_M128:
699 case BUILT_IN_TM_LOG_M256:
700 return true;
701 default:
702 break;
705 return false;
708 /* Detect flags (function attributes) from the function decl or type node. */
711 flags_from_decl_or_type (const_tree exp)
713 int flags = 0;
715 if (DECL_P (exp))
717 /* The function exp may have the `malloc' attribute. */
718 if (DECL_IS_MALLOC (exp))
719 flags |= ECF_MALLOC;
721 /* The function exp may have the `returns_twice' attribute. */
722 if (DECL_IS_RETURNS_TWICE (exp))
723 flags |= ECF_RETURNS_TWICE;
725 /* Process the pure and const attributes. */
726 if (TREE_READONLY (exp))
727 flags |= ECF_CONST;
728 if (DECL_PURE_P (exp))
729 flags |= ECF_PURE;
730 if (DECL_LOOPING_CONST_OR_PURE_P (exp))
731 flags |= ECF_LOOPING_CONST_OR_PURE;
733 if (DECL_IS_NOVOPS (exp))
734 flags |= ECF_NOVOPS;
735 if (lookup_attribute ("leaf", DECL_ATTRIBUTES (exp)))
736 flags |= ECF_LEAF;
738 if (TREE_NOTHROW (exp))
739 flags |= ECF_NOTHROW;
741 if (flag_tm)
743 if (is_tm_builtin (exp))
744 flags |= ECF_TM_BUILTIN;
745 else if ((flags & (ECF_CONST|ECF_NOVOPS)) != 0
746 || lookup_attribute ("transaction_pure",
747 TYPE_ATTRIBUTES (TREE_TYPE (exp))))
748 flags |= ECF_TM_PURE;
751 flags = special_function_p (exp, flags);
753 else if (TYPE_P (exp))
755 if (TYPE_READONLY (exp))
756 flags |= ECF_CONST;
758 if (flag_tm
759 && ((flags & ECF_CONST) != 0
760 || lookup_attribute ("transaction_pure", TYPE_ATTRIBUTES (exp))))
761 flags |= ECF_TM_PURE;
764 if (TREE_THIS_VOLATILE (exp))
766 flags |= ECF_NORETURN;
767 if (flags & (ECF_CONST|ECF_PURE))
768 flags |= ECF_LOOPING_CONST_OR_PURE;
771 return flags;
774 /* Detect flags from a CALL_EXPR. */
777 call_expr_flags (const_tree t)
779 int flags;
780 tree decl = get_callee_fndecl (t);
782 if (decl)
783 flags = flags_from_decl_or_type (decl);
784 else
786 t = TREE_TYPE (CALL_EXPR_FN (t));
787 if (t && TREE_CODE (t) == POINTER_TYPE)
788 flags = flags_from_decl_or_type (TREE_TYPE (t));
789 else
790 flags = 0;
793 return flags;
796 /* Precompute all register parameters as described by ARGS, storing values
797 into fields within the ARGS array.
799 NUM_ACTUALS indicates the total number elements in the ARGS array.
801 Set REG_PARM_SEEN if we encounter a register parameter. */
803 static void
804 precompute_register_parameters (int num_actuals, struct arg_data *args,
805 int *reg_parm_seen)
807 int i;
809 *reg_parm_seen = 0;
811 for (i = 0; i < num_actuals; i++)
812 if (args[i].reg != 0 && ! args[i].pass_on_stack)
814 *reg_parm_seen = 1;
816 if (args[i].value == 0)
818 push_temp_slots ();
819 args[i].value = expand_normal (args[i].tree_value);
820 preserve_temp_slots (args[i].value);
821 pop_temp_slots ();
824 /* If we are to promote the function arg to a wider mode,
825 do it now. */
827 if (args[i].mode != TYPE_MODE (TREE_TYPE (args[i].tree_value)))
828 args[i].value
829 = convert_modes (args[i].mode,
830 TYPE_MODE (TREE_TYPE (args[i].tree_value)),
831 args[i].value, args[i].unsignedp);
833 /* If the value is a non-legitimate constant, force it into a
834 pseudo now. TLS symbols sometimes need a call to resolve. */
835 if (CONSTANT_P (args[i].value)
836 && !targetm.legitimate_constant_p (args[i].mode, args[i].value))
837 args[i].value = force_reg (args[i].mode, args[i].value);
839 /* If we're going to have to load the value by parts, pull the
840 parts into pseudos. The part extraction process can involve
841 non-trivial computation. */
842 if (GET_CODE (args[i].reg) == PARALLEL)
844 tree type = TREE_TYPE (args[i].tree_value);
845 args[i].parallel_value
846 = emit_group_load_into_temps (args[i].reg, args[i].value,
847 type, int_size_in_bytes (type));
850 /* If the value is expensive, and we are inside an appropriately
851 short loop, put the value into a pseudo and then put the pseudo
852 into the hard reg.
854 For small register classes, also do this if this call uses
855 register parameters. This is to avoid reload conflicts while
856 loading the parameters registers. */
858 else if ((! (REG_P (args[i].value)
859 || (GET_CODE (args[i].value) == SUBREG
860 && REG_P (SUBREG_REG (args[i].value)))))
861 && args[i].mode != BLKmode
862 && set_src_cost (args[i].value, optimize_insn_for_speed_p ())
863 > COSTS_N_INSNS (1)
864 && ((*reg_parm_seen
865 && targetm.small_register_classes_for_mode_p (args[i].mode))
866 || optimize))
867 args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
871 #ifdef REG_PARM_STACK_SPACE
873 /* The argument list is the property of the called routine and it
874 may clobber it. If the fixed area has been used for previous
875 parameters, we must save and restore it. */
877 static rtx
878 save_fixed_argument_area (int reg_parm_stack_space, rtx argblock, int *low_to_save, int *high_to_save)
880 int low;
881 int high;
883 /* Compute the boundary of the area that needs to be saved, if any. */
884 high = reg_parm_stack_space;
885 #ifdef ARGS_GROW_DOWNWARD
886 high += 1;
887 #endif
888 if (high > highest_outgoing_arg_in_use)
889 high = highest_outgoing_arg_in_use;
891 for (low = 0; low < high; low++)
892 if (stack_usage_map[low] != 0)
894 int num_to_save;
895 enum machine_mode save_mode;
896 int delta;
897 rtx addr;
898 rtx stack_area;
899 rtx save_area;
901 while (stack_usage_map[--high] == 0)
904 *low_to_save = low;
905 *high_to_save = high;
907 num_to_save = high - low + 1;
908 save_mode = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
910 /* If we don't have the required alignment, must do this
911 in BLKmode. */
912 if ((low & (MIN (GET_MODE_SIZE (save_mode),
913 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
914 save_mode = BLKmode;
916 #ifdef ARGS_GROW_DOWNWARD
917 delta = -high;
918 #else
919 delta = low;
920 #endif
921 addr = plus_constant (Pmode, argblock, delta);
922 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
924 set_mem_align (stack_area, PARM_BOUNDARY);
925 if (save_mode == BLKmode)
927 save_area = assign_stack_temp (BLKmode, num_to_save);
928 emit_block_move (validize_mem (save_area), stack_area,
929 GEN_INT (num_to_save), BLOCK_OP_CALL_PARM);
931 else
933 save_area = gen_reg_rtx (save_mode);
934 emit_move_insn (save_area, stack_area);
937 return save_area;
940 return NULL_RTX;
943 static void
944 restore_fixed_argument_area (rtx save_area, rtx argblock, int high_to_save, int low_to_save)
946 enum machine_mode save_mode = GET_MODE (save_area);
947 int delta;
948 rtx addr, stack_area;
950 #ifdef ARGS_GROW_DOWNWARD
951 delta = -high_to_save;
952 #else
953 delta = low_to_save;
954 #endif
955 addr = plus_constant (Pmode, argblock, delta);
956 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
957 set_mem_align (stack_area, PARM_BOUNDARY);
959 if (save_mode != BLKmode)
960 emit_move_insn (stack_area, save_area);
961 else
962 emit_block_move (stack_area, validize_mem (save_area),
963 GEN_INT (high_to_save - low_to_save + 1),
964 BLOCK_OP_CALL_PARM);
966 #endif /* REG_PARM_STACK_SPACE */
968 /* If any elements in ARGS refer to parameters that are to be passed in
969 registers, but not in memory, and whose alignment does not permit a
970 direct copy into registers. Copy the values into a group of pseudos
971 which we will later copy into the appropriate hard registers.
973 Pseudos for each unaligned argument will be stored into the array
974 args[argnum].aligned_regs. The caller is responsible for deallocating
975 the aligned_regs array if it is nonzero. */
977 static void
978 store_unaligned_arguments_into_pseudos (struct arg_data *args, int num_actuals)
980 int i, j;
982 for (i = 0; i < num_actuals; i++)
983 if (args[i].reg != 0 && ! args[i].pass_on_stack
984 && args[i].mode == BLKmode
985 && MEM_P (args[i].value)
986 && (MEM_ALIGN (args[i].value)
987 < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
989 int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
990 int endian_correction = 0;
992 if (args[i].partial)
994 gcc_assert (args[i].partial % UNITS_PER_WORD == 0);
995 args[i].n_aligned_regs = args[i].partial / UNITS_PER_WORD;
997 else
999 args[i].n_aligned_regs
1000 = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1003 args[i].aligned_regs = XNEWVEC (rtx, args[i].n_aligned_regs);
1005 /* Structures smaller than a word are normally aligned to the
1006 least significant byte. On a BYTES_BIG_ENDIAN machine,
1007 this means we must skip the empty high order bytes when
1008 calculating the bit offset. */
1009 if (bytes < UNITS_PER_WORD
1010 #ifdef BLOCK_REG_PADDING
1011 && (BLOCK_REG_PADDING (args[i].mode,
1012 TREE_TYPE (args[i].tree_value), 1)
1013 == downward)
1014 #else
1015 && BYTES_BIG_ENDIAN
1016 #endif
1018 endian_correction = BITS_PER_WORD - bytes * BITS_PER_UNIT;
1020 for (j = 0; j < args[i].n_aligned_regs; j++)
1022 rtx reg = gen_reg_rtx (word_mode);
1023 rtx word = operand_subword_force (args[i].value, j, BLKmode);
1024 int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
1026 args[i].aligned_regs[j] = reg;
1027 word = extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
1028 word_mode, word_mode);
1030 /* There is no need to restrict this code to loading items
1031 in TYPE_ALIGN sized hunks. The bitfield instructions can
1032 load up entire word sized registers efficiently.
1034 ??? This may not be needed anymore.
1035 We use to emit a clobber here but that doesn't let later
1036 passes optimize the instructions we emit. By storing 0 into
1037 the register later passes know the first AND to zero out the
1038 bitfield being set in the register is unnecessary. The store
1039 of 0 will be deleted as will at least the first AND. */
1041 emit_move_insn (reg, const0_rtx);
1043 bytes -= bitsize / BITS_PER_UNIT;
1044 store_bit_field (reg, bitsize, endian_correction, 0, 0,
1045 word_mode, word);
1050 /* Fill in ARGS_SIZE and ARGS array based on the parameters found in
1051 CALL_EXPR EXP.
1053 NUM_ACTUALS is the total number of parameters.
1055 N_NAMED_ARGS is the total number of named arguments.
1057 STRUCT_VALUE_ADDR_VALUE is the implicit argument for a struct return
1058 value, or null.
1060 FNDECL is the tree code for the target of this call (if known)
1062 ARGS_SO_FAR holds state needed by the target to know where to place
1063 the next argument.
1065 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
1066 for arguments which are passed in registers.
1068 OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
1069 and may be modified by this routine.
1071 OLD_PENDING_ADJ, MUST_PREALLOCATE and FLAGS are pointers to integer
1072 flags which may may be modified by this routine.
1074 MAY_TAILCALL is cleared if we encounter an invisible pass-by-reference
1075 that requires allocation of stack space.
1077 CALL_FROM_THUNK_P is true if this call is the jump from a thunk to
1078 the thunked-to function. */
1080 static void
1081 initialize_argument_information (int num_actuals ATTRIBUTE_UNUSED,
1082 struct arg_data *args,
1083 struct args_size *args_size,
1084 int n_named_args ATTRIBUTE_UNUSED,
1085 tree exp, tree struct_value_addr_value,
1086 tree fndecl, tree fntype,
1087 cumulative_args_t args_so_far,
1088 int reg_parm_stack_space,
1089 rtx *old_stack_level, int *old_pending_adj,
1090 int *must_preallocate, int *ecf_flags,
1091 bool *may_tailcall, bool call_from_thunk_p)
1093 CUMULATIVE_ARGS *args_so_far_pnt = get_cumulative_args (args_so_far);
1094 location_t loc = EXPR_LOCATION (exp);
1095 /* 1 if scanning parms front to back, -1 if scanning back to front. */
1096 int inc;
1098 /* Count arg position in order args appear. */
1099 int argpos;
1101 int i;
1103 args_size->constant = 0;
1104 args_size->var = 0;
1106 /* In this loop, we consider args in the order they are written.
1107 We fill up ARGS from the front or from the back if necessary
1108 so that in any case the first arg to be pushed ends up at the front. */
1110 if (PUSH_ARGS_REVERSED)
1112 i = num_actuals - 1, inc = -1;
1113 /* In this case, must reverse order of args
1114 so that we compute and push the last arg first. */
1116 else
1118 i = 0, inc = 1;
1121 /* First fill in the actual arguments in the ARGS array, splitting
1122 complex arguments if necessary. */
1124 int j = i;
1125 call_expr_arg_iterator iter;
1126 tree arg;
1128 if (struct_value_addr_value)
1130 args[j].tree_value = struct_value_addr_value;
1131 j += inc;
1133 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
1135 tree argtype = TREE_TYPE (arg);
1136 if (targetm.calls.split_complex_arg
1137 && argtype
1138 && TREE_CODE (argtype) == COMPLEX_TYPE
1139 && targetm.calls.split_complex_arg (argtype))
1141 tree subtype = TREE_TYPE (argtype);
1142 args[j].tree_value = build1 (REALPART_EXPR, subtype, arg);
1143 j += inc;
1144 args[j].tree_value = build1 (IMAGPART_EXPR, subtype, arg);
1146 else
1147 args[j].tree_value = arg;
1148 j += inc;
1152 /* I counts args in order (to be) pushed; ARGPOS counts in order written. */
1153 for (argpos = 0; argpos < num_actuals; i += inc, argpos++)
1155 tree type = TREE_TYPE (args[i].tree_value);
1156 int unsignedp;
1157 enum machine_mode mode;
1159 /* Replace erroneous argument with constant zero. */
1160 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1161 args[i].tree_value = integer_zero_node, type = integer_type_node;
1163 /* If TYPE is a transparent union or record, pass things the way
1164 we would pass the first field of the union or record. We have
1165 already verified that the modes are the same. */
1166 if ((TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == RECORD_TYPE)
1167 && TYPE_TRANSPARENT_AGGR (type))
1168 type = TREE_TYPE (first_field (type));
1170 /* Decide where to pass this arg.
1172 args[i].reg is nonzero if all or part is passed in registers.
1174 args[i].partial is nonzero if part but not all is passed in registers,
1175 and the exact value says how many bytes are passed in registers.
1177 args[i].pass_on_stack is nonzero if the argument must at least be
1178 computed on the stack. It may then be loaded back into registers
1179 if args[i].reg is nonzero.
1181 These decisions are driven by the FUNCTION_... macros and must agree
1182 with those made by function.c. */
1184 /* See if this argument should be passed by invisible reference. */
1185 if (pass_by_reference (args_so_far_pnt, TYPE_MODE (type),
1186 type, argpos < n_named_args))
1188 bool callee_copies;
1189 tree base = NULL_TREE;
1191 callee_copies
1192 = reference_callee_copied (args_so_far_pnt, TYPE_MODE (type),
1193 type, argpos < n_named_args);
1195 /* If we're compiling a thunk, pass through invisible references
1196 instead of making a copy. */
1197 if (call_from_thunk_p
1198 || (callee_copies
1199 && !TREE_ADDRESSABLE (type)
1200 && (base = get_base_address (args[i].tree_value))
1201 && TREE_CODE (base) != SSA_NAME
1202 && (!DECL_P (base) || MEM_P (DECL_RTL (base)))))
1204 mark_addressable (args[i].tree_value);
1206 /* We can't use sibcalls if a callee-copied argument is
1207 stored in the current function's frame. */
1208 if (!call_from_thunk_p && DECL_P (base) && !TREE_STATIC (base))
1209 *may_tailcall = false;
1211 args[i].tree_value = build_fold_addr_expr_loc (loc,
1212 args[i].tree_value);
1213 type = TREE_TYPE (args[i].tree_value);
1215 if (*ecf_flags & ECF_CONST)
1216 *ecf_flags &= ~(ECF_CONST | ECF_LOOPING_CONST_OR_PURE);
1218 else
1220 /* We make a copy of the object and pass the address to the
1221 function being called. */
1222 rtx copy;
1224 if (!COMPLETE_TYPE_P (type)
1225 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
1226 || (flag_stack_check == GENERIC_STACK_CHECK
1227 && compare_tree_int (TYPE_SIZE_UNIT (type),
1228 STACK_CHECK_MAX_VAR_SIZE) > 0))
1230 /* This is a variable-sized object. Make space on the stack
1231 for it. */
1232 rtx size_rtx = expr_size (args[i].tree_value);
1234 if (*old_stack_level == 0)
1236 emit_stack_save (SAVE_BLOCK, old_stack_level);
1237 *old_pending_adj = pending_stack_adjust;
1238 pending_stack_adjust = 0;
1241 /* We can pass TRUE as the 4th argument because we just
1242 saved the stack pointer and will restore it right after
1243 the call. */
1244 copy = allocate_dynamic_stack_space (size_rtx,
1245 TYPE_ALIGN (type),
1246 TYPE_ALIGN (type),
1247 true);
1248 copy = gen_rtx_MEM (BLKmode, copy);
1249 set_mem_attributes (copy, type, 1);
1251 else
1252 copy = assign_temp (type, 1, 0);
1254 store_expr (args[i].tree_value, copy, 0, false);
1256 /* Just change the const function to pure and then let
1257 the next test clear the pure based on
1258 callee_copies. */
1259 if (*ecf_flags & ECF_CONST)
1261 *ecf_flags &= ~ECF_CONST;
1262 *ecf_flags |= ECF_PURE;
1265 if (!callee_copies && *ecf_flags & ECF_PURE)
1266 *ecf_flags &= ~(ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
1268 args[i].tree_value
1269 = build_fold_addr_expr_loc (loc, make_tree (type, copy));
1270 type = TREE_TYPE (args[i].tree_value);
1271 *may_tailcall = false;
1275 unsignedp = TYPE_UNSIGNED (type);
1276 mode = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
1277 fndecl ? TREE_TYPE (fndecl) : fntype, 0);
1279 args[i].unsignedp = unsignedp;
1280 args[i].mode = mode;
1282 args[i].reg = targetm.calls.function_arg (args_so_far, mode, type,
1283 argpos < n_named_args);
1285 /* If this is a sibling call and the machine has register windows, the
1286 register window has to be unwinded before calling the routine, so
1287 arguments have to go into the incoming registers. */
1288 if (targetm.calls.function_incoming_arg != targetm.calls.function_arg)
1289 args[i].tail_call_reg
1290 = targetm.calls.function_incoming_arg (args_so_far, mode, type,
1291 argpos < n_named_args);
1292 else
1293 args[i].tail_call_reg = args[i].reg;
1295 if (args[i].reg)
1296 args[i].partial
1297 = targetm.calls.arg_partial_bytes (args_so_far, mode, type,
1298 argpos < n_named_args);
1300 args[i].pass_on_stack = targetm.calls.must_pass_in_stack (mode, type);
1302 /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
1303 it means that we are to pass this arg in the register(s) designated
1304 by the PARALLEL, but also to pass it in the stack. */
1305 if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
1306 && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
1307 args[i].pass_on_stack = 1;
1309 /* If this is an addressable type, we must preallocate the stack
1310 since we must evaluate the object into its final location.
1312 If this is to be passed in both registers and the stack, it is simpler
1313 to preallocate. */
1314 if (TREE_ADDRESSABLE (type)
1315 || (args[i].pass_on_stack && args[i].reg != 0))
1316 *must_preallocate = 1;
1318 /* Compute the stack-size of this argument. */
1319 if (args[i].reg == 0 || args[i].partial != 0
1320 || reg_parm_stack_space > 0
1321 || args[i].pass_on_stack)
1322 locate_and_pad_parm (mode, type,
1323 #ifdef STACK_PARMS_IN_REG_PARM_AREA
1325 #else
1326 args[i].reg != 0,
1327 #endif
1328 args[i].pass_on_stack ? 0 : args[i].partial,
1329 fndecl, args_size, &args[i].locate);
1330 #ifdef BLOCK_REG_PADDING
1331 else
1332 /* The argument is passed entirely in registers. See at which
1333 end it should be padded. */
1334 args[i].locate.where_pad =
1335 BLOCK_REG_PADDING (mode, type,
1336 int_size_in_bytes (type) <= UNITS_PER_WORD);
1337 #endif
1339 /* Update ARGS_SIZE, the total stack space for args so far. */
1341 args_size->constant += args[i].locate.size.constant;
1342 if (args[i].locate.size.var)
1343 ADD_PARM_SIZE (*args_size, args[i].locate.size.var);
1345 /* Increment ARGS_SO_FAR, which has info about which arg-registers
1346 have been used, etc. */
1348 targetm.calls.function_arg_advance (args_so_far, TYPE_MODE (type),
1349 type, argpos < n_named_args);
1353 /* Update ARGS_SIZE to contain the total size for the argument block.
1354 Return the original constant component of the argument block's size.
1356 REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
1357 for arguments passed in registers. */
1359 static int
1360 compute_argument_block_size (int reg_parm_stack_space,
1361 struct args_size *args_size,
1362 tree fndecl ATTRIBUTE_UNUSED,
1363 tree fntype ATTRIBUTE_UNUSED,
1364 int preferred_stack_boundary ATTRIBUTE_UNUSED)
1366 int unadjusted_args_size = args_size->constant;
1368 /* For accumulate outgoing args mode we don't need to align, since the frame
1369 will be already aligned. Align to STACK_BOUNDARY in order to prevent
1370 backends from generating misaligned frame sizes. */
1371 if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
1372 preferred_stack_boundary = STACK_BOUNDARY;
1374 /* Compute the actual size of the argument block required. The variable
1375 and constant sizes must be combined, the size may have to be rounded,
1376 and there may be a minimum required size. */
1378 if (args_size->var)
1380 args_size->var = ARGS_SIZE_TREE (*args_size);
1381 args_size->constant = 0;
1383 preferred_stack_boundary /= BITS_PER_UNIT;
1384 if (preferred_stack_boundary > 1)
1386 /* We don't handle this case yet. To handle it correctly we have
1387 to add the delta, round and subtract the delta.
1388 Currently no machine description requires this support. */
1389 gcc_assert (!(stack_pointer_delta & (preferred_stack_boundary - 1)));
1390 args_size->var = round_up (args_size->var, preferred_stack_boundary);
1393 if (reg_parm_stack_space > 0)
1395 args_size->var
1396 = size_binop (MAX_EXPR, args_size->var,
1397 ssize_int (reg_parm_stack_space));
1399 /* The area corresponding to register parameters is not to count in
1400 the size of the block we need. So make the adjustment. */
1401 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1402 args_size->var
1403 = size_binop (MINUS_EXPR, args_size->var,
1404 ssize_int (reg_parm_stack_space));
1407 else
1409 preferred_stack_boundary /= BITS_PER_UNIT;
1410 if (preferred_stack_boundary < 1)
1411 preferred_stack_boundary = 1;
1412 args_size->constant = (((args_size->constant
1413 + stack_pointer_delta
1414 + preferred_stack_boundary - 1)
1415 / preferred_stack_boundary
1416 * preferred_stack_boundary)
1417 - stack_pointer_delta);
1419 args_size->constant = MAX (args_size->constant,
1420 reg_parm_stack_space);
1422 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1423 args_size->constant -= reg_parm_stack_space;
1425 return unadjusted_args_size;
1428 /* Precompute parameters as needed for a function call.
1430 FLAGS is mask of ECF_* constants.
1432 NUM_ACTUALS is the number of arguments.
1434 ARGS is an array containing information for each argument; this
1435 routine fills in the INITIAL_VALUE and VALUE fields for each
1436 precomputed argument. */
1438 static void
1439 precompute_arguments (int num_actuals, struct arg_data *args)
1441 int i;
1443 /* If this is a libcall, then precompute all arguments so that we do not
1444 get extraneous instructions emitted as part of the libcall sequence. */
1446 /* If we preallocated the stack space, and some arguments must be passed
1447 on the stack, then we must precompute any parameter which contains a
1448 function call which will store arguments on the stack.
1449 Otherwise, evaluating the parameter may clobber previous parameters
1450 which have already been stored into the stack. (we have code to avoid
1451 such case by saving the outgoing stack arguments, but it results in
1452 worse code) */
1453 if (!ACCUMULATE_OUTGOING_ARGS)
1454 return;
1456 for (i = 0; i < num_actuals; i++)
1458 tree type;
1459 enum machine_mode mode;
1461 if (TREE_CODE (args[i].tree_value) != CALL_EXPR)
1462 continue;
1464 /* If this is an addressable type, we cannot pre-evaluate it. */
1465 type = TREE_TYPE (args[i].tree_value);
1466 gcc_assert (!TREE_ADDRESSABLE (type));
1468 args[i].initial_value = args[i].value
1469 = expand_normal (args[i].tree_value);
1471 mode = TYPE_MODE (type);
1472 if (mode != args[i].mode)
1474 int unsignedp = args[i].unsignedp;
1475 args[i].value
1476 = convert_modes (args[i].mode, mode,
1477 args[i].value, args[i].unsignedp);
1479 /* CSE will replace this only if it contains args[i].value
1480 pseudo, so convert it down to the declared mode using
1481 a SUBREG. */
1482 if (REG_P (args[i].value)
1483 && GET_MODE_CLASS (args[i].mode) == MODE_INT
1484 && promote_mode (type, mode, &unsignedp) != args[i].mode)
1486 args[i].initial_value
1487 = gen_lowpart_SUBREG (mode, args[i].value);
1488 SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1489 SUBREG_PROMOTED_UNSIGNED_SET (args[i].initial_value,
1490 args[i].unsignedp);
1496 /* Given the current state of MUST_PREALLOCATE and information about
1497 arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
1498 compute and return the final value for MUST_PREALLOCATE. */
1500 static int
1501 finalize_must_preallocate (int must_preallocate, int num_actuals,
1502 struct arg_data *args, struct args_size *args_size)
1504 /* See if we have or want to preallocate stack space.
1506 If we would have to push a partially-in-regs parm
1507 before other stack parms, preallocate stack space instead.
1509 If the size of some parm is not a multiple of the required stack
1510 alignment, we must preallocate.
1512 If the total size of arguments that would otherwise create a copy in
1513 a temporary (such as a CALL) is more than half the total argument list
1514 size, preallocation is faster.
1516 Another reason to preallocate is if we have a machine (like the m88k)
1517 where stack alignment is required to be maintained between every
1518 pair of insns, not just when the call is made. However, we assume here
1519 that such machines either do not have push insns (and hence preallocation
1520 would occur anyway) or the problem is taken care of with
1521 PUSH_ROUNDING. */
1523 if (! must_preallocate)
1525 int partial_seen = 0;
1526 int copy_to_evaluate_size = 0;
1527 int i;
1529 for (i = 0; i < num_actuals && ! must_preallocate; i++)
1531 if (args[i].partial > 0 && ! args[i].pass_on_stack)
1532 partial_seen = 1;
1533 else if (partial_seen && args[i].reg == 0)
1534 must_preallocate = 1;
1536 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1537 && (TREE_CODE (args[i].tree_value) == CALL_EXPR
1538 || TREE_CODE (args[i].tree_value) == TARGET_EXPR
1539 || TREE_CODE (args[i].tree_value) == COND_EXPR
1540 || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
1541 copy_to_evaluate_size
1542 += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1545 if (copy_to_evaluate_size * 2 >= args_size->constant
1546 && args_size->constant > 0)
1547 must_preallocate = 1;
1549 return must_preallocate;
1552 /* If we preallocated stack space, compute the address of each argument
1553 and store it into the ARGS array.
1555 We need not ensure it is a valid memory address here; it will be
1556 validized when it is used.
1558 ARGBLOCK is an rtx for the address of the outgoing arguments. */
1560 static void
1561 compute_argument_addresses (struct arg_data *args, rtx argblock, int num_actuals)
1563 if (argblock)
1565 rtx arg_reg = argblock;
1566 int i, arg_offset = 0;
1568 if (GET_CODE (argblock) == PLUS)
1569 arg_reg = XEXP (argblock, 0), arg_offset = INTVAL (XEXP (argblock, 1));
1571 for (i = 0; i < num_actuals; i++)
1573 rtx offset = ARGS_SIZE_RTX (args[i].locate.offset);
1574 rtx slot_offset = ARGS_SIZE_RTX (args[i].locate.slot_offset);
1575 rtx addr;
1576 unsigned int align, boundary;
1577 unsigned int units_on_stack = 0;
1578 enum machine_mode partial_mode = VOIDmode;
1580 /* Skip this parm if it will not be passed on the stack. */
1581 if (! args[i].pass_on_stack
1582 && args[i].reg != 0
1583 && args[i].partial == 0)
1584 continue;
1586 if (CONST_INT_P (offset))
1587 addr = plus_constant (Pmode, arg_reg, INTVAL (offset));
1588 else
1589 addr = gen_rtx_PLUS (Pmode, arg_reg, offset);
1591 addr = plus_constant (Pmode, addr, arg_offset);
1593 if (args[i].partial != 0)
1595 /* Only part of the parameter is being passed on the stack.
1596 Generate a simple memory reference of the correct size. */
1597 units_on_stack = args[i].locate.size.constant;
1598 partial_mode = mode_for_size (units_on_stack * BITS_PER_UNIT,
1599 MODE_INT, 1);
1600 args[i].stack = gen_rtx_MEM (partial_mode, addr);
1601 set_mem_size (args[i].stack, units_on_stack);
1603 else
1605 args[i].stack = gen_rtx_MEM (args[i].mode, addr);
1606 set_mem_attributes (args[i].stack,
1607 TREE_TYPE (args[i].tree_value), 1);
1609 align = BITS_PER_UNIT;
1610 boundary = args[i].locate.boundary;
1611 if (args[i].locate.where_pad != downward)
1612 align = boundary;
1613 else if (CONST_INT_P (offset))
1615 align = INTVAL (offset) * BITS_PER_UNIT | boundary;
1616 align = align & -align;
1618 set_mem_align (args[i].stack, align);
1620 if (CONST_INT_P (slot_offset))
1621 addr = plus_constant (Pmode, arg_reg, INTVAL (slot_offset));
1622 else
1623 addr = gen_rtx_PLUS (Pmode, arg_reg, slot_offset);
1625 addr = plus_constant (Pmode, addr, arg_offset);
1627 if (args[i].partial != 0)
1629 /* Only part of the parameter is being passed on the stack.
1630 Generate a simple memory reference of the correct size.
1632 args[i].stack_slot = gen_rtx_MEM (partial_mode, addr);
1633 set_mem_size (args[i].stack_slot, units_on_stack);
1635 else
1637 args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
1638 set_mem_attributes (args[i].stack_slot,
1639 TREE_TYPE (args[i].tree_value), 1);
1641 set_mem_align (args[i].stack_slot, args[i].locate.boundary);
1643 /* Function incoming arguments may overlap with sibling call
1644 outgoing arguments and we cannot allow reordering of reads
1645 from function arguments with stores to outgoing arguments
1646 of sibling calls. */
1647 set_mem_alias_set (args[i].stack, 0);
1648 set_mem_alias_set (args[i].stack_slot, 0);
1653 /* Given a FNDECL and EXP, return an rtx suitable for use as a target address
1654 in a call instruction.
1656 FNDECL is the tree node for the target function. For an indirect call
1657 FNDECL will be NULL_TREE.
1659 ADDR is the operand 0 of CALL_EXPR for this call. */
1661 static rtx
1662 rtx_for_function_call (tree fndecl, tree addr)
1664 rtx funexp;
1666 /* Get the function to call, in the form of RTL. */
1667 if (fndecl)
1669 if (!TREE_USED (fndecl) && fndecl != current_function_decl)
1670 TREE_USED (fndecl) = 1;
1672 /* Get a SYMBOL_REF rtx for the function address. */
1673 funexp = XEXP (DECL_RTL (fndecl), 0);
1675 else
1676 /* Generate an rtx (probably a pseudo-register) for the address. */
1678 push_temp_slots ();
1679 funexp = expand_normal (addr);
1680 pop_temp_slots (); /* FUNEXP can't be BLKmode. */
1682 return funexp;
1685 /* Internal state for internal_arg_pointer_based_exp and its helpers. */
1686 static struct
1688 /* Last insn that has been scanned by internal_arg_pointer_based_exp_scan,
1689 or NULL_RTX if none has been scanned yet. */
1690 rtx scan_start;
1691 /* Vector indexed by REGNO - FIRST_PSEUDO_REGISTER, recording if a pseudo is
1692 based on crtl->args.internal_arg_pointer. The element is NULL_RTX if the
1693 pseudo isn't based on it, a CONST_INT offset if the pseudo is based on it
1694 with fixed offset, or PC if this is with variable or unknown offset. */
1695 vec<rtx> cache;
1696 } internal_arg_pointer_exp_state;
1698 static rtx internal_arg_pointer_based_exp (rtx, bool);
1700 /* Helper function for internal_arg_pointer_based_exp. Scan insns in
1701 the tail call sequence, starting with first insn that hasn't been
1702 scanned yet, and note for each pseudo on the LHS whether it is based
1703 on crtl->args.internal_arg_pointer or not, and what offset from that
1704 that pointer it has. */
1706 static void
1707 internal_arg_pointer_based_exp_scan (void)
1709 rtx insn, scan_start = internal_arg_pointer_exp_state.scan_start;
1711 if (scan_start == NULL_RTX)
1712 insn = get_insns ();
1713 else
1714 insn = NEXT_INSN (scan_start);
1716 while (insn)
1718 rtx set = single_set (insn);
1719 if (set && REG_P (SET_DEST (set)) && !HARD_REGISTER_P (SET_DEST (set)))
1721 rtx val = NULL_RTX;
1722 unsigned int idx = REGNO (SET_DEST (set)) - FIRST_PSEUDO_REGISTER;
1723 /* Punt on pseudos set multiple times. */
1724 if (idx < internal_arg_pointer_exp_state.cache.length ()
1725 && (internal_arg_pointer_exp_state.cache[idx]
1726 != NULL_RTX))
1727 val = pc_rtx;
1728 else
1729 val = internal_arg_pointer_based_exp (SET_SRC (set), false);
1730 if (val != NULL_RTX)
1732 if (idx >= internal_arg_pointer_exp_state.cache.length ())
1733 internal_arg_pointer_exp_state.cache
1734 .safe_grow_cleared (idx + 1);
1735 internal_arg_pointer_exp_state.cache[idx] = val;
1738 if (NEXT_INSN (insn) == NULL_RTX)
1739 scan_start = insn;
1740 insn = NEXT_INSN (insn);
1743 internal_arg_pointer_exp_state.scan_start = scan_start;
1746 /* Helper function for internal_arg_pointer_based_exp, called through
1747 for_each_rtx. Return 1 if *LOC is a register based on
1748 crtl->args.internal_arg_pointer. Return -1 if *LOC is not based on it
1749 and the subexpressions need not be examined. Otherwise return 0. */
1751 static int
1752 internal_arg_pointer_based_exp_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
1754 if (REG_P (*loc) && internal_arg_pointer_based_exp (*loc, false) != NULL_RTX)
1755 return 1;
1756 if (MEM_P (*loc))
1757 return -1;
1758 return 0;
1761 /* Compute whether RTL is based on crtl->args.internal_arg_pointer. Return
1762 NULL_RTX if RTL isn't based on it, a CONST_INT offset if RTL is based on
1763 it with fixed offset, or PC if this is with variable or unknown offset.
1764 TOPLEVEL is true if the function is invoked at the topmost level. */
1766 static rtx
1767 internal_arg_pointer_based_exp (rtx rtl, bool toplevel)
1769 if (CONSTANT_P (rtl))
1770 return NULL_RTX;
1772 if (rtl == crtl->args.internal_arg_pointer)
1773 return const0_rtx;
1775 if (REG_P (rtl) && HARD_REGISTER_P (rtl))
1776 return NULL_RTX;
1778 if (GET_CODE (rtl) == PLUS && CONST_INT_P (XEXP (rtl, 1)))
1780 rtx val = internal_arg_pointer_based_exp (XEXP (rtl, 0), toplevel);
1781 if (val == NULL_RTX || val == pc_rtx)
1782 return val;
1783 return plus_constant (Pmode, val, INTVAL (XEXP (rtl, 1)));
1786 /* When called at the topmost level, scan pseudo assignments in between the
1787 last scanned instruction in the tail call sequence and the latest insn
1788 in that sequence. */
1789 if (toplevel)
1790 internal_arg_pointer_based_exp_scan ();
1792 if (REG_P (rtl))
1794 unsigned int idx = REGNO (rtl) - FIRST_PSEUDO_REGISTER;
1795 if (idx < internal_arg_pointer_exp_state.cache.length ())
1796 return internal_arg_pointer_exp_state.cache[idx];
1798 return NULL_RTX;
1801 if (for_each_rtx (&rtl, internal_arg_pointer_based_exp_1, NULL))
1802 return pc_rtx;
1804 return NULL_RTX;
1807 /* Return true if and only if SIZE storage units (usually bytes)
1808 starting from address ADDR overlap with already clobbered argument
1809 area. This function is used to determine if we should give up a
1810 sibcall. */
1812 static bool
1813 mem_overlaps_already_clobbered_arg_p (rtx addr, unsigned HOST_WIDE_INT size)
1815 HOST_WIDE_INT i;
1816 rtx val;
1818 if (bitmap_empty_p (stored_args_map))
1819 return false;
1820 val = internal_arg_pointer_based_exp (addr, true);
1821 if (val == NULL_RTX)
1822 return false;
1823 else if (val == pc_rtx)
1824 return true;
1825 else
1826 i = INTVAL (val);
1827 #ifdef STACK_GROWS_DOWNWARD
1828 i -= crtl->args.pretend_args_size;
1829 #else
1830 i += crtl->args.pretend_args_size;
1831 #endif
1833 #ifdef ARGS_GROW_DOWNWARD
1834 i = -i - size;
1835 #endif
1836 if (size > 0)
1838 unsigned HOST_WIDE_INT k;
1840 for (k = 0; k < size; k++)
1841 if (i + k < SBITMAP_SIZE (stored_args_map)
1842 && bitmap_bit_p (stored_args_map, i + k))
1843 return true;
1846 return false;
1849 /* Do the register loads required for any wholly-register parms or any
1850 parms which are passed both on the stack and in a register. Their
1851 expressions were already evaluated.
1853 Mark all register-parms as living through the call, putting these USE
1854 insns in the CALL_INSN_FUNCTION_USAGE field.
1856 When IS_SIBCALL, perform the check_sibcall_argument_overlap
1857 checking, setting *SIBCALL_FAILURE if appropriate. */
1859 static void
1860 load_register_parameters (struct arg_data *args, int num_actuals,
1861 rtx *call_fusage, int flags, int is_sibcall,
1862 int *sibcall_failure)
1864 int i, j;
1866 for (i = 0; i < num_actuals; i++)
1868 rtx reg = ((flags & ECF_SIBCALL)
1869 ? args[i].tail_call_reg : args[i].reg);
1870 if (reg)
1872 int partial = args[i].partial;
1873 int nregs;
1874 int size = 0;
1875 rtx before_arg = get_last_insn ();
1876 /* Set non-negative if we must move a word at a time, even if
1877 just one word (e.g, partial == 4 && mode == DFmode). Set
1878 to -1 if we just use a normal move insn. This value can be
1879 zero if the argument is a zero size structure. */
1880 nregs = -1;
1881 if (GET_CODE (reg) == PARALLEL)
1883 else if (partial)
1885 gcc_assert (partial % UNITS_PER_WORD == 0);
1886 nregs = partial / UNITS_PER_WORD;
1888 else if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode)
1890 size = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1891 nregs = (size + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
1893 else
1894 size = GET_MODE_SIZE (args[i].mode);
1896 /* Handle calls that pass values in multiple non-contiguous
1897 locations. The Irix 6 ABI has examples of this. */
1899 if (GET_CODE (reg) == PARALLEL)
1900 emit_group_move (reg, args[i].parallel_value);
1902 /* If simple case, just do move. If normal partial, store_one_arg
1903 has already loaded the register for us. In all other cases,
1904 load the register(s) from memory. */
1906 else if (nregs == -1)
1908 emit_move_insn (reg, args[i].value);
1909 #ifdef BLOCK_REG_PADDING
1910 /* Handle case where we have a value that needs shifting
1911 up to the msb. eg. a QImode value and we're padding
1912 upward on a BYTES_BIG_ENDIAN machine. */
1913 if (size < UNITS_PER_WORD
1914 && (args[i].locate.where_pad
1915 == (BYTES_BIG_ENDIAN ? upward : downward)))
1917 rtx x;
1918 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
1920 /* Assigning REG here rather than a temp makes CALL_FUSAGE
1921 report the whole reg as used. Strictly speaking, the
1922 call only uses SIZE bytes at the msb end, but it doesn't
1923 seem worth generating rtl to say that. */
1924 reg = gen_rtx_REG (word_mode, REGNO (reg));
1925 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
1926 if (x != reg)
1927 emit_move_insn (reg, x);
1929 #endif
1932 /* If we have pre-computed the values to put in the registers in
1933 the case of non-aligned structures, copy them in now. */
1935 else if (args[i].n_aligned_regs != 0)
1936 for (j = 0; j < args[i].n_aligned_regs; j++)
1937 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
1938 args[i].aligned_regs[j]);
1940 else if (partial == 0 || args[i].pass_on_stack)
1942 rtx mem = validize_mem (args[i].value);
1944 /* Check for overlap with already clobbered argument area,
1945 providing that this has non-zero size. */
1946 if (is_sibcall
1947 && (size == 0
1948 || mem_overlaps_already_clobbered_arg_p
1949 (XEXP (args[i].value, 0), size)))
1950 *sibcall_failure = 1;
1952 /* Handle a BLKmode that needs shifting. */
1953 if (nregs == 1 && size < UNITS_PER_WORD
1954 #ifdef BLOCK_REG_PADDING
1955 && args[i].locate.where_pad == downward
1956 #else
1957 && BYTES_BIG_ENDIAN
1958 #endif
1961 rtx tem = operand_subword_force (mem, 0, args[i].mode);
1962 rtx ri = gen_rtx_REG (word_mode, REGNO (reg));
1963 rtx x = gen_reg_rtx (word_mode);
1964 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
1965 enum tree_code dir = BYTES_BIG_ENDIAN ? RSHIFT_EXPR
1966 : LSHIFT_EXPR;
1968 emit_move_insn (x, tem);
1969 x = expand_shift (dir, word_mode, x, shift, ri, 1);
1970 if (x != ri)
1971 emit_move_insn (ri, x);
1973 else
1974 move_block_to_reg (REGNO (reg), mem, nregs, args[i].mode);
1977 /* When a parameter is a block, and perhaps in other cases, it is
1978 possible that it did a load from an argument slot that was
1979 already clobbered. */
1980 if (is_sibcall
1981 && check_sibcall_argument_overlap (before_arg, &args[i], 0))
1982 *sibcall_failure = 1;
1984 /* Handle calls that pass values in multiple non-contiguous
1985 locations. The Irix 6 ABI has examples of this. */
1986 if (GET_CODE (reg) == PARALLEL)
1987 use_group_regs (call_fusage, reg);
1988 else if (nregs == -1)
1989 use_reg_mode (call_fusage, reg,
1990 TYPE_MODE (TREE_TYPE (args[i].tree_value)));
1991 else if (nregs > 0)
1992 use_regs (call_fusage, REGNO (reg), nregs);
1997 /* We need to pop PENDING_STACK_ADJUST bytes. But, if the arguments
1998 wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
1999 bytes, then we would need to push some additional bytes to pad the
2000 arguments. So, we compute an adjust to the stack pointer for an
2001 amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
2002 bytes. Then, when the arguments are pushed the stack will be perfectly
2003 aligned. ARGS_SIZE->CONSTANT is set to the number of bytes that should
2004 be popped after the call. Returns the adjustment. */
2006 static int
2007 combine_pending_stack_adjustment_and_call (int unadjusted_args_size,
2008 struct args_size *args_size,
2009 unsigned int preferred_unit_stack_boundary)
2011 /* The number of bytes to pop so that the stack will be
2012 under-aligned by UNADJUSTED_ARGS_SIZE bytes. */
2013 HOST_WIDE_INT adjustment;
2014 /* The alignment of the stack after the arguments are pushed, if we
2015 just pushed the arguments without adjust the stack here. */
2016 unsigned HOST_WIDE_INT unadjusted_alignment;
2018 unadjusted_alignment
2019 = ((stack_pointer_delta + unadjusted_args_size)
2020 % preferred_unit_stack_boundary);
2022 /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
2023 as possible -- leaving just enough left to cancel out the
2024 UNADJUSTED_ALIGNMENT. In other words, we want to ensure that the
2025 PENDING_STACK_ADJUST is non-negative, and congruent to
2026 -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY. */
2028 /* Begin by trying to pop all the bytes. */
2029 unadjusted_alignment
2030 = (unadjusted_alignment
2031 - (pending_stack_adjust % preferred_unit_stack_boundary));
2032 adjustment = pending_stack_adjust;
2033 /* Push enough additional bytes that the stack will be aligned
2034 after the arguments are pushed. */
2035 if (preferred_unit_stack_boundary > 1)
2037 if (unadjusted_alignment > 0)
2038 adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
2039 else
2040 adjustment += unadjusted_alignment;
2043 /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
2044 bytes after the call. The right number is the entire
2045 PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
2046 by the arguments in the first place. */
2047 args_size->constant
2048 = pending_stack_adjust - adjustment + unadjusted_args_size;
2050 return adjustment;
2053 /* Scan X expression if it does not dereference any argument slots
2054 we already clobbered by tail call arguments (as noted in stored_args_map
2055 bitmap).
2056 Return nonzero if X expression dereferences such argument slots,
2057 zero otherwise. */
2059 static int
2060 check_sibcall_argument_overlap_1 (rtx x)
2062 RTX_CODE code;
2063 int i, j;
2064 const char *fmt;
2066 if (x == NULL_RTX)
2067 return 0;
2069 code = GET_CODE (x);
2071 /* We need not check the operands of the CALL expression itself. */
2072 if (code == CALL)
2073 return 0;
2075 if (code == MEM)
2076 return mem_overlaps_already_clobbered_arg_p (XEXP (x, 0),
2077 GET_MODE_SIZE (GET_MODE (x)));
2079 /* Scan all subexpressions. */
2080 fmt = GET_RTX_FORMAT (code);
2081 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2083 if (*fmt == 'e')
2085 if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
2086 return 1;
2088 else if (*fmt == 'E')
2090 for (j = 0; j < XVECLEN (x, i); j++)
2091 if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
2092 return 1;
2095 return 0;
2098 /* Scan sequence after INSN if it does not dereference any argument slots
2099 we already clobbered by tail call arguments (as noted in stored_args_map
2100 bitmap). If MARK_STORED_ARGS_MAP, add stack slots for ARG to
2101 stored_args_map bitmap afterwards (when ARG is a register MARK_STORED_ARGS_MAP
2102 should be 0). Return nonzero if sequence after INSN dereferences such argument
2103 slots, zero otherwise. */
2105 static int
2106 check_sibcall_argument_overlap (rtx insn, struct arg_data *arg, int mark_stored_args_map)
2108 int low, high;
2110 if (insn == NULL_RTX)
2111 insn = get_insns ();
2112 else
2113 insn = NEXT_INSN (insn);
2115 for (; insn; insn = NEXT_INSN (insn))
2116 if (INSN_P (insn)
2117 && check_sibcall_argument_overlap_1 (PATTERN (insn)))
2118 break;
2120 if (mark_stored_args_map)
2122 #ifdef ARGS_GROW_DOWNWARD
2123 low = -arg->locate.slot_offset.constant - arg->locate.size.constant;
2124 #else
2125 low = arg->locate.slot_offset.constant;
2126 #endif
2128 for (high = low + arg->locate.size.constant; low < high; low++)
2129 bitmap_set_bit (stored_args_map, low);
2131 return insn != NULL_RTX;
2134 /* Given that a function returns a value of mode MODE at the most
2135 significant end of hard register VALUE, shift VALUE left or right
2136 as specified by LEFT_P. Return true if some action was needed. */
2138 bool
2139 shift_return_value (enum machine_mode mode, bool left_p, rtx value)
2141 HOST_WIDE_INT shift;
2143 gcc_assert (REG_P (value) && HARD_REGISTER_P (value));
2144 shift = GET_MODE_BITSIZE (GET_MODE (value)) - GET_MODE_BITSIZE (mode);
2145 if (shift == 0)
2146 return false;
2148 /* Use ashr rather than lshr for right shifts. This is for the benefit
2149 of the MIPS port, which requires SImode values to be sign-extended
2150 when stored in 64-bit registers. */
2151 if (!force_expand_binop (GET_MODE (value), left_p ? ashl_optab : ashr_optab,
2152 value, GEN_INT (shift), value, 1, OPTAB_WIDEN))
2153 gcc_unreachable ();
2154 return true;
2157 /* If X is a likely-spilled register value, copy it to a pseudo
2158 register and return that register. Return X otherwise. */
2160 static rtx
2161 avoid_likely_spilled_reg (rtx x)
2163 rtx new_rtx;
2165 if (REG_P (x)
2166 && HARD_REGISTER_P (x)
2167 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (x))))
2169 /* Make sure that we generate a REG rather than a CONCAT.
2170 Moves into CONCATs can need nontrivial instructions,
2171 and the whole point of this function is to avoid
2172 using the hard register directly in such a situation. */
2173 generating_concat_p = 0;
2174 new_rtx = gen_reg_rtx (GET_MODE (x));
2175 generating_concat_p = 1;
2176 emit_move_insn (new_rtx, x);
2177 return new_rtx;
2179 return x;
2182 /* Generate all the code for a CALL_EXPR exp
2183 and return an rtx for its value.
2184 Store the value in TARGET (specified as an rtx) if convenient.
2185 If the value is stored in TARGET then TARGET is returned.
2186 If IGNORE is nonzero, then we ignore the value of the function call. */
2189 expand_call (tree exp, rtx target, int ignore)
2191 /* Nonzero if we are currently expanding a call. */
2192 static int currently_expanding_call = 0;
2194 /* RTX for the function to be called. */
2195 rtx funexp;
2196 /* Sequence of insns to perform a normal "call". */
2197 rtx normal_call_insns = NULL_RTX;
2198 /* Sequence of insns to perform a tail "call". */
2199 rtx tail_call_insns = NULL_RTX;
2200 /* Data type of the function. */
2201 tree funtype;
2202 tree type_arg_types;
2203 tree rettype;
2204 /* Declaration of the function being called,
2205 or 0 if the function is computed (not known by name). */
2206 tree fndecl = 0;
2207 /* The type of the function being called. */
2208 tree fntype;
2209 bool try_tail_call = CALL_EXPR_TAILCALL (exp);
2210 int pass;
2212 /* Register in which non-BLKmode value will be returned,
2213 or 0 if no value or if value is BLKmode. */
2214 rtx valreg;
2215 /* Address where we should return a BLKmode value;
2216 0 if value not BLKmode. */
2217 rtx structure_value_addr = 0;
2218 /* Nonzero if that address is being passed by treating it as
2219 an extra, implicit first parameter. Otherwise,
2220 it is passed by being copied directly into struct_value_rtx. */
2221 int structure_value_addr_parm = 0;
2222 /* Holds the value of implicit argument for the struct value. */
2223 tree structure_value_addr_value = NULL_TREE;
2224 /* Size of aggregate value wanted, or zero if none wanted
2225 or if we are using the non-reentrant PCC calling convention
2226 or expecting the value in registers. */
2227 HOST_WIDE_INT struct_value_size = 0;
2228 /* Nonzero if called function returns an aggregate in memory PCC style,
2229 by returning the address of where to find it. */
2230 int pcc_struct_value = 0;
2231 rtx struct_value = 0;
2233 /* Number of actual parameters in this call, including struct value addr. */
2234 int num_actuals;
2235 /* Number of named args. Args after this are anonymous ones
2236 and they must all go on the stack. */
2237 int n_named_args;
2238 /* Number of complex actual arguments that need to be split. */
2239 int num_complex_actuals = 0;
2241 /* Vector of information about each argument.
2242 Arguments are numbered in the order they will be pushed,
2243 not the order they are written. */
2244 struct arg_data *args;
2246 /* Total size in bytes of all the stack-parms scanned so far. */
2247 struct args_size args_size;
2248 struct args_size adjusted_args_size;
2249 /* Size of arguments before any adjustments (such as rounding). */
2250 int unadjusted_args_size;
2251 /* Data on reg parms scanned so far. */
2252 CUMULATIVE_ARGS args_so_far_v;
2253 cumulative_args_t args_so_far;
2254 /* Nonzero if a reg parm has been scanned. */
2255 int reg_parm_seen;
2256 /* Nonzero if this is an indirect function call. */
2258 /* Nonzero if we must avoid push-insns in the args for this call.
2259 If stack space is allocated for register parameters, but not by the
2260 caller, then it is preallocated in the fixed part of the stack frame.
2261 So the entire argument block must then be preallocated (i.e., we
2262 ignore PUSH_ROUNDING in that case). */
2264 int must_preallocate = !PUSH_ARGS;
2266 /* Size of the stack reserved for parameter registers. */
2267 int reg_parm_stack_space = 0;
2269 /* Address of space preallocated for stack parms
2270 (on machines that lack push insns), or 0 if space not preallocated. */
2271 rtx argblock = 0;
2273 /* Mask of ECF_ and ERF_ flags. */
2274 int flags = 0;
2275 int return_flags = 0;
2276 #ifdef REG_PARM_STACK_SPACE
2277 /* Define the boundary of the register parm stack space that needs to be
2278 saved, if any. */
2279 int low_to_save, high_to_save;
2280 rtx save_area = 0; /* Place that it is saved */
2281 #endif
2283 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
2284 char *initial_stack_usage_map = stack_usage_map;
2285 char *stack_usage_map_buf = NULL;
2287 int old_stack_allocated;
2289 /* State variables to track stack modifications. */
2290 rtx old_stack_level = 0;
2291 int old_stack_arg_under_construction = 0;
2292 int old_pending_adj = 0;
2293 int old_inhibit_defer_pop = inhibit_defer_pop;
2295 /* Some stack pointer alterations we make are performed via
2296 allocate_dynamic_stack_space. This modifies the stack_pointer_delta,
2297 which we then also need to save/restore along the way. */
2298 int old_stack_pointer_delta = 0;
2300 rtx call_fusage;
2301 tree addr = CALL_EXPR_FN (exp);
2302 int i;
2303 /* The alignment of the stack, in bits. */
2304 unsigned HOST_WIDE_INT preferred_stack_boundary;
2305 /* The alignment of the stack, in bytes. */
2306 unsigned HOST_WIDE_INT preferred_unit_stack_boundary;
2307 /* The static chain value to use for this call. */
2308 rtx static_chain_value;
2309 /* See if this is "nothrow" function call. */
2310 if (TREE_NOTHROW (exp))
2311 flags |= ECF_NOTHROW;
2313 /* See if we can find a DECL-node for the actual function, and get the
2314 function attributes (flags) from the function decl or type node. */
2315 fndecl = get_callee_fndecl (exp);
2316 if (fndecl)
2318 fntype = TREE_TYPE (fndecl);
2319 flags |= flags_from_decl_or_type (fndecl);
2320 return_flags |= decl_return_flags (fndecl);
2322 else
2324 fntype = TREE_TYPE (TREE_TYPE (addr));
2325 flags |= flags_from_decl_or_type (fntype);
2327 rettype = TREE_TYPE (exp);
2329 struct_value = targetm.calls.struct_value_rtx (fntype, 0);
2331 /* Warn if this value is an aggregate type,
2332 regardless of which calling convention we are using for it. */
2333 if (AGGREGATE_TYPE_P (rettype))
2334 warning (OPT_Waggregate_return, "function call has aggregate value");
2336 /* If the result of a non looping pure or const function call is
2337 ignored (or void), and none of its arguments are volatile, we can
2338 avoid expanding the call and just evaluate the arguments for
2339 side-effects. */
2340 if ((flags & (ECF_CONST | ECF_PURE))
2341 && (!(flags & ECF_LOOPING_CONST_OR_PURE))
2342 && (ignore || target == const0_rtx
2343 || TYPE_MODE (rettype) == VOIDmode))
2345 bool volatilep = false;
2346 tree arg;
2347 call_expr_arg_iterator iter;
2349 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2350 if (TREE_THIS_VOLATILE (arg))
2352 volatilep = true;
2353 break;
2356 if (! volatilep)
2358 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2359 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
2360 return const0_rtx;
2364 #ifdef REG_PARM_STACK_SPACE
2365 reg_parm_stack_space = REG_PARM_STACK_SPACE (!fndecl ? fntype : fndecl);
2366 #endif
2368 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
2369 && reg_parm_stack_space > 0 && PUSH_ARGS)
2370 must_preallocate = 1;
2372 /* Set up a place to return a structure. */
2374 /* Cater to broken compilers. */
2375 if (aggregate_value_p (exp, fntype))
2377 /* This call returns a big structure. */
2378 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
2380 #ifdef PCC_STATIC_STRUCT_RETURN
2382 pcc_struct_value = 1;
2384 #else /* not PCC_STATIC_STRUCT_RETURN */
2386 struct_value_size = int_size_in_bytes (rettype);
2388 if (target && MEM_P (target) && CALL_EXPR_RETURN_SLOT_OPT (exp))
2389 structure_value_addr = XEXP (target, 0);
2390 else
2392 /* For variable-sized objects, we must be called with a target
2393 specified. If we were to allocate space on the stack here,
2394 we would have no way of knowing when to free it. */
2395 rtx d = assign_temp (rettype, 1, 1);
2396 structure_value_addr = XEXP (d, 0);
2397 target = 0;
2400 #endif /* not PCC_STATIC_STRUCT_RETURN */
2403 /* Figure out the amount to which the stack should be aligned. */
2404 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
2405 if (fndecl)
2407 struct cgraph_rtl_info *i = cgraph_rtl_info (fndecl);
2408 /* Without automatic stack alignment, we can't increase preferred
2409 stack boundary. With automatic stack alignment, it is
2410 unnecessary since unless we can guarantee that all callers will
2411 align the outgoing stack properly, callee has to align its
2412 stack anyway. */
2413 if (i
2414 && i->preferred_incoming_stack_boundary
2415 && i->preferred_incoming_stack_boundary < preferred_stack_boundary)
2416 preferred_stack_boundary = i->preferred_incoming_stack_boundary;
2419 /* Operand 0 is a pointer-to-function; get the type of the function. */
2420 funtype = TREE_TYPE (addr);
2421 gcc_assert (POINTER_TYPE_P (funtype));
2422 funtype = TREE_TYPE (funtype);
2424 /* Count whether there are actual complex arguments that need to be split
2425 into their real and imaginary parts. Munge the type_arg_types
2426 appropriately here as well. */
2427 if (targetm.calls.split_complex_arg)
2429 call_expr_arg_iterator iter;
2430 tree arg;
2431 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2433 tree type = TREE_TYPE (arg);
2434 if (type && TREE_CODE (type) == COMPLEX_TYPE
2435 && targetm.calls.split_complex_arg (type))
2436 num_complex_actuals++;
2438 type_arg_types = split_complex_types (TYPE_ARG_TYPES (funtype));
2440 else
2441 type_arg_types = TYPE_ARG_TYPES (funtype);
2443 if (flags & ECF_MAY_BE_ALLOCA)
2444 cfun->calls_alloca = 1;
2446 /* If struct_value_rtx is 0, it means pass the address
2447 as if it were an extra parameter. Put the argument expression
2448 in structure_value_addr_value. */
2449 if (structure_value_addr && struct_value == 0)
2451 /* If structure_value_addr is a REG other than
2452 virtual_outgoing_args_rtx, we can use always use it. If it
2453 is not a REG, we must always copy it into a register.
2454 If it is virtual_outgoing_args_rtx, we must copy it to another
2455 register in some cases. */
2456 rtx temp = (!REG_P (structure_value_addr)
2457 || (ACCUMULATE_OUTGOING_ARGS
2458 && stack_arg_under_construction
2459 && structure_value_addr == virtual_outgoing_args_rtx)
2460 ? copy_addr_to_reg (convert_memory_address
2461 (Pmode, structure_value_addr))
2462 : structure_value_addr);
2464 structure_value_addr_value =
2465 make_tree (build_pointer_type (TREE_TYPE (funtype)), temp);
2466 structure_value_addr_parm = 1;
2469 /* Count the arguments and set NUM_ACTUALS. */
2470 num_actuals =
2471 call_expr_nargs (exp) + num_complex_actuals + structure_value_addr_parm;
2473 /* Compute number of named args.
2474 First, do a raw count of the args for INIT_CUMULATIVE_ARGS. */
2476 if (type_arg_types != 0)
2477 n_named_args
2478 = (list_length (type_arg_types)
2479 /* Count the struct value address, if it is passed as a parm. */
2480 + structure_value_addr_parm);
2481 else
2482 /* If we know nothing, treat all args as named. */
2483 n_named_args = num_actuals;
2485 /* Start updating where the next arg would go.
2487 On some machines (such as the PA) indirect calls have a different
2488 calling convention than normal calls. The fourth argument in
2489 INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
2490 or not. */
2491 INIT_CUMULATIVE_ARGS (args_so_far_v, funtype, NULL_RTX, fndecl, n_named_args);
2492 args_so_far = pack_cumulative_args (&args_so_far_v);
2494 /* Now possibly adjust the number of named args.
2495 Normally, don't include the last named arg if anonymous args follow.
2496 We do include the last named arg if
2497 targetm.calls.strict_argument_naming() returns nonzero.
2498 (If no anonymous args follow, the result of list_length is actually
2499 one too large. This is harmless.)
2501 If targetm.calls.pretend_outgoing_varargs_named() returns
2502 nonzero, and targetm.calls.strict_argument_naming() returns zero,
2503 this machine will be able to place unnamed args that were passed
2504 in registers into the stack. So treat all args as named. This
2505 allows the insns emitting for a specific argument list to be
2506 independent of the function declaration.
2508 If targetm.calls.pretend_outgoing_varargs_named() returns zero,
2509 we do not have any reliable way to pass unnamed args in
2510 registers, so we must force them into memory. */
2512 if (type_arg_types != 0
2513 && targetm.calls.strict_argument_naming (args_so_far))
2515 else if (type_arg_types != 0
2516 && ! targetm.calls.pretend_outgoing_varargs_named (args_so_far))
2517 /* Don't include the last named arg. */
2518 --n_named_args;
2519 else
2520 /* Treat all args as named. */
2521 n_named_args = num_actuals;
2523 /* Make a vector to hold all the information about each arg. */
2524 args = XALLOCAVEC (struct arg_data, num_actuals);
2525 memset (args, 0, num_actuals * sizeof (struct arg_data));
2527 /* Build up entries in the ARGS array, compute the size of the
2528 arguments into ARGS_SIZE, etc. */
2529 initialize_argument_information (num_actuals, args, &args_size,
2530 n_named_args, exp,
2531 structure_value_addr_value, fndecl, fntype,
2532 args_so_far, reg_parm_stack_space,
2533 &old_stack_level, &old_pending_adj,
2534 &must_preallocate, &flags,
2535 &try_tail_call, CALL_FROM_THUNK_P (exp));
2537 if (args_size.var)
2538 must_preallocate = 1;
2540 /* Now make final decision about preallocating stack space. */
2541 must_preallocate = finalize_must_preallocate (must_preallocate,
2542 num_actuals, args,
2543 &args_size);
2545 /* If the structure value address will reference the stack pointer, we
2546 must stabilize it. We don't need to do this if we know that we are
2547 not going to adjust the stack pointer in processing this call. */
2549 if (structure_value_addr
2550 && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
2551 || reg_mentioned_p (virtual_outgoing_args_rtx,
2552 structure_value_addr))
2553 && (args_size.var
2554 || (!ACCUMULATE_OUTGOING_ARGS && args_size.constant)))
2555 structure_value_addr = copy_to_reg (structure_value_addr);
2557 /* Tail calls can make things harder to debug, and we've traditionally
2558 pushed these optimizations into -O2. Don't try if we're already
2559 expanding a call, as that means we're an argument. Don't try if
2560 there's cleanups, as we know there's code to follow the call. */
2562 if (currently_expanding_call++ != 0
2563 || !flag_optimize_sibling_calls
2564 || args_size.var
2565 || dbg_cnt (tail_call) == false)
2566 try_tail_call = 0;
2568 /* Rest of purposes for tail call optimizations to fail. */
2569 if (
2570 #ifdef HAVE_sibcall_epilogue
2571 !HAVE_sibcall_epilogue
2572 #else
2574 #endif
2575 || !try_tail_call
2576 /* Doing sibling call optimization needs some work, since
2577 structure_value_addr can be allocated on the stack.
2578 It does not seem worth the effort since few optimizable
2579 sibling calls will return a structure. */
2580 || structure_value_addr != NULL_RTX
2581 #ifdef REG_PARM_STACK_SPACE
2582 /* If outgoing reg parm stack space changes, we can not do sibcall. */
2583 || (OUTGOING_REG_PARM_STACK_SPACE (funtype)
2584 != OUTGOING_REG_PARM_STACK_SPACE (TREE_TYPE (current_function_decl)))
2585 || (reg_parm_stack_space != REG_PARM_STACK_SPACE (fndecl))
2586 #endif
2587 /* Check whether the target is able to optimize the call
2588 into a sibcall. */
2589 || !targetm.function_ok_for_sibcall (fndecl, exp)
2590 /* Functions that do not return exactly once may not be sibcall
2591 optimized. */
2592 || (flags & (ECF_RETURNS_TWICE | ECF_NORETURN))
2593 || TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (addr)))
2594 /* If the called function is nested in the current one, it might access
2595 some of the caller's arguments, but could clobber them beforehand if
2596 the argument areas are shared. */
2597 || (fndecl && decl_function_context (fndecl) == current_function_decl)
2598 /* If this function requires more stack slots than the current
2599 function, we cannot change it into a sibling call.
2600 crtl->args.pretend_args_size is not part of the
2601 stack allocated by our caller. */
2602 || args_size.constant > (crtl->args.size
2603 - crtl->args.pretend_args_size)
2604 /* If the callee pops its own arguments, then it must pop exactly
2605 the same number of arguments as the current function. */
2606 || (targetm.calls.return_pops_args (fndecl, funtype, args_size.constant)
2607 != targetm.calls.return_pops_args (current_function_decl,
2608 TREE_TYPE (current_function_decl),
2609 crtl->args.size))
2610 || !lang_hooks.decls.ok_for_sibcall (fndecl))
2611 try_tail_call = 0;
2613 /* Check if caller and callee disagree in promotion of function
2614 return value. */
2615 if (try_tail_call)
2617 enum machine_mode caller_mode, caller_promoted_mode;
2618 enum machine_mode callee_mode, callee_promoted_mode;
2619 int caller_unsignedp, callee_unsignedp;
2620 tree caller_res = DECL_RESULT (current_function_decl);
2622 caller_unsignedp = TYPE_UNSIGNED (TREE_TYPE (caller_res));
2623 caller_mode = DECL_MODE (caller_res);
2624 callee_unsignedp = TYPE_UNSIGNED (TREE_TYPE (funtype));
2625 callee_mode = TYPE_MODE (TREE_TYPE (funtype));
2626 caller_promoted_mode
2627 = promote_function_mode (TREE_TYPE (caller_res), caller_mode,
2628 &caller_unsignedp,
2629 TREE_TYPE (current_function_decl), 1);
2630 callee_promoted_mode
2631 = promote_function_mode (TREE_TYPE (funtype), callee_mode,
2632 &callee_unsignedp,
2633 funtype, 1);
2634 if (caller_mode != VOIDmode
2635 && (caller_promoted_mode != callee_promoted_mode
2636 || ((caller_mode != caller_promoted_mode
2637 || callee_mode != callee_promoted_mode)
2638 && (caller_unsignedp != callee_unsignedp
2639 || GET_MODE_BITSIZE (caller_mode)
2640 < GET_MODE_BITSIZE (callee_mode)))))
2641 try_tail_call = 0;
2644 /* Ensure current function's preferred stack boundary is at least
2645 what we need. Stack alignment may also increase preferred stack
2646 boundary. */
2647 if (crtl->preferred_stack_boundary < preferred_stack_boundary)
2648 crtl->preferred_stack_boundary = preferred_stack_boundary;
2649 else
2650 preferred_stack_boundary = crtl->preferred_stack_boundary;
2652 preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
2654 /* We want to make two insn chains; one for a sibling call, the other
2655 for a normal call. We will select one of the two chains after
2656 initial RTL generation is complete. */
2657 for (pass = try_tail_call ? 0 : 1; pass < 2; pass++)
2659 int sibcall_failure = 0;
2660 /* We want to emit any pending stack adjustments before the tail
2661 recursion "call". That way we know any adjustment after the tail
2662 recursion call can be ignored if we indeed use the tail
2663 call expansion. */
2664 int save_pending_stack_adjust = 0;
2665 int save_stack_pointer_delta = 0;
2666 rtx insns;
2667 rtx before_call, next_arg_reg, after_args;
2669 if (pass == 0)
2671 /* State variables we need to save and restore between
2672 iterations. */
2673 save_pending_stack_adjust = pending_stack_adjust;
2674 save_stack_pointer_delta = stack_pointer_delta;
2676 if (pass)
2677 flags &= ~ECF_SIBCALL;
2678 else
2679 flags |= ECF_SIBCALL;
2681 /* Other state variables that we must reinitialize each time
2682 through the loop (that are not initialized by the loop itself). */
2683 argblock = 0;
2684 call_fusage = 0;
2686 /* Start a new sequence for the normal call case.
2688 From this point on, if the sibling call fails, we want to set
2689 sibcall_failure instead of continuing the loop. */
2690 start_sequence ();
2692 /* Don't let pending stack adjusts add up to too much.
2693 Also, do all pending adjustments now if there is any chance
2694 this might be a call to alloca or if we are expanding a sibling
2695 call sequence.
2696 Also do the adjustments before a throwing call, otherwise
2697 exception handling can fail; PR 19225. */
2698 if (pending_stack_adjust >= 32
2699 || (pending_stack_adjust > 0
2700 && (flags & ECF_MAY_BE_ALLOCA))
2701 || (pending_stack_adjust > 0
2702 && flag_exceptions && !(flags & ECF_NOTHROW))
2703 || pass == 0)
2704 do_pending_stack_adjust ();
2706 /* Precompute any arguments as needed. */
2707 if (pass)
2708 precompute_arguments (num_actuals, args);
2710 /* Now we are about to start emitting insns that can be deleted
2711 if a libcall is deleted. */
2712 if (pass && (flags & ECF_MALLOC))
2713 start_sequence ();
2715 if (pass == 0 && crtl->stack_protect_guard)
2716 stack_protect_epilogue ();
2718 adjusted_args_size = args_size;
2719 /* Compute the actual size of the argument block required. The variable
2720 and constant sizes must be combined, the size may have to be rounded,
2721 and there may be a minimum required size. When generating a sibcall
2722 pattern, do not round up, since we'll be re-using whatever space our
2723 caller provided. */
2724 unadjusted_args_size
2725 = compute_argument_block_size (reg_parm_stack_space,
2726 &adjusted_args_size,
2727 fndecl, fntype,
2728 (pass == 0 ? 0
2729 : preferred_stack_boundary));
2731 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
2733 /* The argument block when performing a sibling call is the
2734 incoming argument block. */
2735 if (pass == 0)
2737 argblock = crtl->args.internal_arg_pointer;
2738 argblock
2739 #ifdef STACK_GROWS_DOWNWARD
2740 = plus_constant (Pmode, argblock, crtl->args.pretend_args_size);
2741 #else
2742 = plus_constant (Pmode, argblock, -crtl->args.pretend_args_size);
2743 #endif
2744 stored_args_map = sbitmap_alloc (args_size.constant);
2745 bitmap_clear (stored_args_map);
2748 /* If we have no actual push instructions, or shouldn't use them,
2749 make space for all args right now. */
2750 else if (adjusted_args_size.var != 0)
2752 if (old_stack_level == 0)
2754 emit_stack_save (SAVE_BLOCK, &old_stack_level);
2755 old_stack_pointer_delta = stack_pointer_delta;
2756 old_pending_adj = pending_stack_adjust;
2757 pending_stack_adjust = 0;
2758 /* stack_arg_under_construction says whether a stack arg is
2759 being constructed at the old stack level. Pushing the stack
2760 gets a clean outgoing argument block. */
2761 old_stack_arg_under_construction = stack_arg_under_construction;
2762 stack_arg_under_construction = 0;
2764 argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
2765 if (flag_stack_usage_info)
2766 current_function_has_unbounded_dynamic_stack_size = 1;
2768 else
2770 /* Note that we must go through the motions of allocating an argument
2771 block even if the size is zero because we may be storing args
2772 in the area reserved for register arguments, which may be part of
2773 the stack frame. */
2775 int needed = adjusted_args_size.constant;
2777 /* Store the maximum argument space used. It will be pushed by
2778 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
2779 checking). */
2781 if (needed > crtl->outgoing_args_size)
2782 crtl->outgoing_args_size = needed;
2784 if (must_preallocate)
2786 if (ACCUMULATE_OUTGOING_ARGS)
2788 /* Since the stack pointer will never be pushed, it is
2789 possible for the evaluation of a parm to clobber
2790 something we have already written to the stack.
2791 Since most function calls on RISC machines do not use
2792 the stack, this is uncommon, but must work correctly.
2794 Therefore, we save any area of the stack that was already
2795 written and that we are using. Here we set up to do this
2796 by making a new stack usage map from the old one. The
2797 actual save will be done by store_one_arg.
2799 Another approach might be to try to reorder the argument
2800 evaluations to avoid this conflicting stack usage. */
2802 /* Since we will be writing into the entire argument area,
2803 the map must be allocated for its entire size, not just
2804 the part that is the responsibility of the caller. */
2805 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
2806 needed += reg_parm_stack_space;
2808 #ifdef ARGS_GROW_DOWNWARD
2809 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2810 needed + 1);
2811 #else
2812 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2813 needed);
2814 #endif
2815 free (stack_usage_map_buf);
2816 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
2817 stack_usage_map = stack_usage_map_buf;
2819 if (initial_highest_arg_in_use)
2820 memcpy (stack_usage_map, initial_stack_usage_map,
2821 initial_highest_arg_in_use);
2823 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
2824 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
2825 (highest_outgoing_arg_in_use
2826 - initial_highest_arg_in_use));
2827 needed = 0;
2829 /* The address of the outgoing argument list must not be
2830 copied to a register here, because argblock would be left
2831 pointing to the wrong place after the call to
2832 allocate_dynamic_stack_space below. */
2834 argblock = virtual_outgoing_args_rtx;
2836 else
2838 if (inhibit_defer_pop == 0)
2840 /* Try to reuse some or all of the pending_stack_adjust
2841 to get this space. */
2842 needed
2843 = (combine_pending_stack_adjustment_and_call
2844 (unadjusted_args_size,
2845 &adjusted_args_size,
2846 preferred_unit_stack_boundary));
2848 /* combine_pending_stack_adjustment_and_call computes
2849 an adjustment before the arguments are allocated.
2850 Account for them and see whether or not the stack
2851 needs to go up or down. */
2852 needed = unadjusted_args_size - needed;
2854 if (needed < 0)
2856 /* We're releasing stack space. */
2857 /* ??? We can avoid any adjustment at all if we're
2858 already aligned. FIXME. */
2859 pending_stack_adjust = -needed;
2860 do_pending_stack_adjust ();
2861 needed = 0;
2863 else
2864 /* We need to allocate space. We'll do that in
2865 push_block below. */
2866 pending_stack_adjust = 0;
2869 /* Special case this because overhead of `push_block' in
2870 this case is non-trivial. */
2871 if (needed == 0)
2872 argblock = virtual_outgoing_args_rtx;
2873 else
2875 argblock = push_block (GEN_INT (needed), 0, 0);
2876 #ifdef ARGS_GROW_DOWNWARD
2877 argblock = plus_constant (Pmode, argblock, needed);
2878 #endif
2881 /* We only really need to call `copy_to_reg' in the case
2882 where push insns are going to be used to pass ARGBLOCK
2883 to a function call in ARGS. In that case, the stack
2884 pointer changes value from the allocation point to the
2885 call point, and hence the value of
2886 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
2887 as well always do it. */
2888 argblock = copy_to_reg (argblock);
2893 if (ACCUMULATE_OUTGOING_ARGS)
2895 /* The save/restore code in store_one_arg handles all
2896 cases except one: a constructor call (including a C
2897 function returning a BLKmode struct) to initialize
2898 an argument. */
2899 if (stack_arg_under_construction)
2901 rtx push_size
2902 = GEN_INT (adjusted_args_size.constant
2903 + (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype
2904 : TREE_TYPE (fndecl))) ? 0
2905 : reg_parm_stack_space));
2906 if (old_stack_level == 0)
2908 emit_stack_save (SAVE_BLOCK, &old_stack_level);
2909 old_stack_pointer_delta = stack_pointer_delta;
2910 old_pending_adj = pending_stack_adjust;
2911 pending_stack_adjust = 0;
2912 /* stack_arg_under_construction says whether a stack
2913 arg is being constructed at the old stack level.
2914 Pushing the stack gets a clean outgoing argument
2915 block. */
2916 old_stack_arg_under_construction
2917 = stack_arg_under_construction;
2918 stack_arg_under_construction = 0;
2919 /* Make a new map for the new argument list. */
2920 free (stack_usage_map_buf);
2921 stack_usage_map_buf = XCNEWVEC (char, highest_outgoing_arg_in_use);
2922 stack_usage_map = stack_usage_map_buf;
2923 highest_outgoing_arg_in_use = 0;
2925 /* We can pass TRUE as the 4th argument because we just
2926 saved the stack pointer and will restore it right after
2927 the call. */
2928 allocate_dynamic_stack_space (push_size, 0,
2929 BIGGEST_ALIGNMENT, true);
2932 /* If argument evaluation might modify the stack pointer,
2933 copy the address of the argument list to a register. */
2934 for (i = 0; i < num_actuals; i++)
2935 if (args[i].pass_on_stack)
2937 argblock = copy_addr_to_reg (argblock);
2938 break;
2942 compute_argument_addresses (args, argblock, num_actuals);
2944 /* If we push args individually in reverse order, perform stack alignment
2945 before the first push (the last arg). */
2946 if (PUSH_ARGS_REVERSED && argblock == 0
2947 && adjusted_args_size.constant != unadjusted_args_size)
2949 /* When the stack adjustment is pending, we get better code
2950 by combining the adjustments. */
2951 if (pending_stack_adjust
2952 && ! inhibit_defer_pop)
2954 pending_stack_adjust
2955 = (combine_pending_stack_adjustment_and_call
2956 (unadjusted_args_size,
2957 &adjusted_args_size,
2958 preferred_unit_stack_boundary));
2959 do_pending_stack_adjust ();
2961 else if (argblock == 0)
2962 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
2963 - unadjusted_args_size));
2965 /* Now that the stack is properly aligned, pops can't safely
2966 be deferred during the evaluation of the arguments. */
2967 NO_DEFER_POP;
2969 /* Record the maximum pushed stack space size. We need to delay
2970 doing it this far to take into account the optimization done
2971 by combine_pending_stack_adjustment_and_call. */
2972 if (flag_stack_usage_info
2973 && !ACCUMULATE_OUTGOING_ARGS
2974 && pass
2975 && adjusted_args_size.var == 0)
2977 int pushed = adjusted_args_size.constant + pending_stack_adjust;
2978 if (pushed > current_function_pushed_stack_size)
2979 current_function_pushed_stack_size = pushed;
2982 funexp = rtx_for_function_call (fndecl, addr);
2984 /* Figure out the register where the value, if any, will come back. */
2985 valreg = 0;
2986 if (TYPE_MODE (rettype) != VOIDmode
2987 && ! structure_value_addr)
2989 if (pcc_struct_value)
2990 valreg = hard_function_value (build_pointer_type (rettype),
2991 fndecl, NULL, (pass == 0));
2992 else
2993 valreg = hard_function_value (rettype, fndecl, fntype,
2994 (pass == 0));
2996 /* If VALREG is a PARALLEL whose first member has a zero
2997 offset, use that. This is for targets such as m68k that
2998 return the same value in multiple places. */
2999 if (GET_CODE (valreg) == PARALLEL)
3001 rtx elem = XVECEXP (valreg, 0, 0);
3002 rtx where = XEXP (elem, 0);
3003 rtx offset = XEXP (elem, 1);
3004 if (offset == const0_rtx
3005 && GET_MODE (where) == GET_MODE (valreg))
3006 valreg = where;
3010 /* Precompute all register parameters. It isn't safe to compute anything
3011 once we have started filling any specific hard regs. */
3012 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
3014 if (CALL_EXPR_STATIC_CHAIN (exp))
3015 static_chain_value = expand_normal (CALL_EXPR_STATIC_CHAIN (exp));
3016 else
3017 static_chain_value = 0;
3019 #ifdef REG_PARM_STACK_SPACE
3020 /* Save the fixed argument area if it's part of the caller's frame and
3021 is clobbered by argument setup for this call. */
3022 if (ACCUMULATE_OUTGOING_ARGS && pass)
3023 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3024 &low_to_save, &high_to_save);
3025 #endif
3027 /* Now store (and compute if necessary) all non-register parms.
3028 These come before register parms, since they can require block-moves,
3029 which could clobber the registers used for register parms.
3030 Parms which have partial registers are not stored here,
3031 but we do preallocate space here if they want that. */
3033 for (i = 0; i < num_actuals; i++)
3035 if (args[i].reg == 0 || args[i].pass_on_stack)
3037 rtx before_arg = get_last_insn ();
3039 if (store_one_arg (&args[i], argblock, flags,
3040 adjusted_args_size.var != 0,
3041 reg_parm_stack_space)
3042 || (pass == 0
3043 && check_sibcall_argument_overlap (before_arg,
3044 &args[i], 1)))
3045 sibcall_failure = 1;
3048 if (args[i].stack)
3049 call_fusage
3050 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[i].tree_value)),
3051 gen_rtx_USE (VOIDmode, args[i].stack),
3052 call_fusage);
3055 /* If we have a parm that is passed in registers but not in memory
3056 and whose alignment does not permit a direct copy into registers,
3057 make a group of pseudos that correspond to each register that we
3058 will later fill. */
3059 if (STRICT_ALIGNMENT)
3060 store_unaligned_arguments_into_pseudos (args, num_actuals);
3062 /* Now store any partially-in-registers parm.
3063 This is the last place a block-move can happen. */
3064 if (reg_parm_seen)
3065 for (i = 0; i < num_actuals; i++)
3066 if (args[i].partial != 0 && ! args[i].pass_on_stack)
3068 rtx before_arg = get_last_insn ();
3070 if (store_one_arg (&args[i], argblock, flags,
3071 adjusted_args_size.var != 0,
3072 reg_parm_stack_space)
3073 || (pass == 0
3074 && check_sibcall_argument_overlap (before_arg,
3075 &args[i], 1)))
3076 sibcall_failure = 1;
3079 /* If we pushed args in forward order, perform stack alignment
3080 after pushing the last arg. */
3081 if (!PUSH_ARGS_REVERSED && argblock == 0)
3082 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
3083 - unadjusted_args_size));
3085 /* If register arguments require space on the stack and stack space
3086 was not preallocated, allocate stack space here for arguments
3087 passed in registers. */
3088 if (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
3089 && !ACCUMULATE_OUTGOING_ARGS
3090 && must_preallocate == 0 && reg_parm_stack_space > 0)
3091 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
3093 /* Pass the function the address in which to return a
3094 structure value. */
3095 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
3097 structure_value_addr
3098 = convert_memory_address (Pmode, structure_value_addr);
3099 emit_move_insn (struct_value,
3100 force_reg (Pmode,
3101 force_operand (structure_value_addr,
3102 NULL_RTX)));
3104 if (REG_P (struct_value))
3105 use_reg (&call_fusage, struct_value);
3108 after_args = get_last_insn ();
3109 funexp = prepare_call_address (fndecl, funexp, static_chain_value,
3110 &call_fusage, reg_parm_seen, pass == 0);
3112 load_register_parameters (args, num_actuals, &call_fusage, flags,
3113 pass == 0, &sibcall_failure);
3115 /* Save a pointer to the last insn before the call, so that we can
3116 later safely search backwards to find the CALL_INSN. */
3117 before_call = get_last_insn ();
3119 /* Set up next argument register. For sibling calls on machines
3120 with register windows this should be the incoming register. */
3121 if (pass == 0)
3122 next_arg_reg = targetm.calls.function_incoming_arg (args_so_far,
3123 VOIDmode,
3124 void_type_node,
3125 true);
3126 else
3127 next_arg_reg = targetm.calls.function_arg (args_so_far,
3128 VOIDmode, void_type_node,
3129 true);
3131 if (pass == 1 && (return_flags & ERF_RETURNS_ARG))
3133 int arg_nr = return_flags & ERF_RETURN_ARG_MASK;
3134 if (PUSH_ARGS_REVERSED)
3135 arg_nr = num_actuals - arg_nr - 1;
3136 if (arg_nr >= 0
3137 && arg_nr < num_actuals
3138 && args[arg_nr].reg
3139 && valreg
3140 && REG_P (valreg)
3141 && GET_MODE (args[arg_nr].reg) == GET_MODE (valreg))
3142 call_fusage
3143 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[arg_nr].tree_value)),
3144 gen_rtx_SET (VOIDmode, valreg, args[arg_nr].reg),
3145 call_fusage);
3147 /* All arguments and registers used for the call must be set up by
3148 now! */
3150 /* Stack must be properly aligned now. */
3151 gcc_assert (!pass
3152 || !(stack_pointer_delta % preferred_unit_stack_boundary));
3154 /* Generate the actual call instruction. */
3155 emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
3156 adjusted_args_size.constant, struct_value_size,
3157 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
3158 flags, args_so_far);
3160 /* If the call setup or the call itself overlaps with anything
3161 of the argument setup we probably clobbered our call address.
3162 In that case we can't do sibcalls. */
3163 if (pass == 0
3164 && check_sibcall_argument_overlap (after_args, 0, 0))
3165 sibcall_failure = 1;
3167 /* If a non-BLKmode value is returned at the most significant end
3168 of a register, shift the register right by the appropriate amount
3169 and update VALREG accordingly. BLKmode values are handled by the
3170 group load/store machinery below. */
3171 if (!structure_value_addr
3172 && !pcc_struct_value
3173 && TYPE_MODE (rettype) != VOIDmode
3174 && TYPE_MODE (rettype) != BLKmode
3175 && REG_P (valreg)
3176 && targetm.calls.return_in_msb (rettype))
3178 if (shift_return_value (TYPE_MODE (rettype), false, valreg))
3179 sibcall_failure = 1;
3180 valreg = gen_rtx_REG (TYPE_MODE (rettype), REGNO (valreg));
3183 if (pass && (flags & ECF_MALLOC))
3185 rtx temp = gen_reg_rtx (GET_MODE (valreg));
3186 rtx last, insns;
3188 /* The return value from a malloc-like function is a pointer. */
3189 if (TREE_CODE (rettype) == POINTER_TYPE)
3190 mark_reg_pointer (temp, MALLOC_ABI_ALIGNMENT);
3192 emit_move_insn (temp, valreg);
3194 /* The return value from a malloc-like function can not alias
3195 anything else. */
3196 last = get_last_insn ();
3197 add_reg_note (last, REG_NOALIAS, temp);
3199 /* Write out the sequence. */
3200 insns = get_insns ();
3201 end_sequence ();
3202 emit_insn (insns);
3203 valreg = temp;
3206 /* For calls to `setjmp', etc., inform
3207 function.c:setjmp_warnings that it should complain if
3208 nonvolatile values are live. For functions that cannot
3209 return, inform flow that control does not fall through. */
3211 if ((flags & ECF_NORETURN) || pass == 0)
3213 /* The barrier must be emitted
3214 immediately after the CALL_INSN. Some ports emit more
3215 than just a CALL_INSN above, so we must search for it here. */
3217 rtx last = get_last_insn ();
3218 while (!CALL_P (last))
3220 last = PREV_INSN (last);
3221 /* There was no CALL_INSN? */
3222 gcc_assert (last != before_call);
3225 emit_barrier_after (last);
3227 /* Stack adjustments after a noreturn call are dead code.
3228 However when NO_DEFER_POP is in effect, we must preserve
3229 stack_pointer_delta. */
3230 if (inhibit_defer_pop == 0)
3232 stack_pointer_delta = old_stack_allocated;
3233 pending_stack_adjust = 0;
3237 /* If value type not void, return an rtx for the value. */
3239 if (TYPE_MODE (rettype) == VOIDmode
3240 || ignore)
3241 target = const0_rtx;
3242 else if (structure_value_addr)
3244 if (target == 0 || !MEM_P (target))
3246 target
3247 = gen_rtx_MEM (TYPE_MODE (rettype),
3248 memory_address (TYPE_MODE (rettype),
3249 structure_value_addr));
3250 set_mem_attributes (target, rettype, 1);
3253 else if (pcc_struct_value)
3255 /* This is the special C++ case where we need to
3256 know what the true target was. We take care to
3257 never use this value more than once in one expression. */
3258 target = gen_rtx_MEM (TYPE_MODE (rettype),
3259 copy_to_reg (valreg));
3260 set_mem_attributes (target, rettype, 1);
3262 /* Handle calls that return values in multiple non-contiguous locations.
3263 The Irix 6 ABI has examples of this. */
3264 else if (GET_CODE (valreg) == PARALLEL)
3266 if (target == 0)
3267 target = emit_group_move_into_temps (valreg);
3268 else if (rtx_equal_p (target, valreg))
3270 else if (GET_CODE (target) == PARALLEL)
3271 /* Handle the result of a emit_group_move_into_temps
3272 call in the previous pass. */
3273 emit_group_move (target, valreg);
3274 else
3275 emit_group_store (target, valreg, rettype,
3276 int_size_in_bytes (rettype));
3278 else if (target
3279 && GET_MODE (target) == TYPE_MODE (rettype)
3280 && GET_MODE (target) == GET_MODE (valreg))
3282 bool may_overlap = false;
3284 /* We have to copy a return value in a CLASS_LIKELY_SPILLED hard
3285 reg to a plain register. */
3286 if (!REG_P (target) || HARD_REGISTER_P (target))
3287 valreg = avoid_likely_spilled_reg (valreg);
3289 /* If TARGET is a MEM in the argument area, and we have
3290 saved part of the argument area, then we can't store
3291 directly into TARGET as it may get overwritten when we
3292 restore the argument save area below. Don't work too
3293 hard though and simply force TARGET to a register if it
3294 is a MEM; the optimizer is quite likely to sort it out. */
3295 if (ACCUMULATE_OUTGOING_ARGS && pass && MEM_P (target))
3296 for (i = 0; i < num_actuals; i++)
3297 if (args[i].save_area)
3299 may_overlap = true;
3300 break;
3303 if (may_overlap)
3304 target = copy_to_reg (valreg);
3305 else
3307 /* TARGET and VALREG cannot be equal at this point
3308 because the latter would not have
3309 REG_FUNCTION_VALUE_P true, while the former would if
3310 it were referring to the same register.
3312 If they refer to the same register, this move will be
3313 a no-op, except when function inlining is being
3314 done. */
3315 emit_move_insn (target, valreg);
3317 /* If we are setting a MEM, this code must be executed.
3318 Since it is emitted after the call insn, sibcall
3319 optimization cannot be performed in that case. */
3320 if (MEM_P (target))
3321 sibcall_failure = 1;
3324 else
3325 target = copy_to_reg (avoid_likely_spilled_reg (valreg));
3327 /* If we promoted this return value, make the proper SUBREG.
3328 TARGET might be const0_rtx here, so be careful. */
3329 if (REG_P (target)
3330 && TYPE_MODE (rettype) != BLKmode
3331 && GET_MODE (target) != TYPE_MODE (rettype))
3333 tree type = rettype;
3334 int unsignedp = TYPE_UNSIGNED (type);
3335 int offset = 0;
3336 enum machine_mode pmode;
3338 /* Ensure we promote as expected, and get the new unsignedness. */
3339 pmode = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
3340 funtype, 1);
3341 gcc_assert (GET_MODE (target) == pmode);
3343 if ((WORDS_BIG_ENDIAN || BYTES_BIG_ENDIAN)
3344 && (GET_MODE_SIZE (GET_MODE (target))
3345 > GET_MODE_SIZE (TYPE_MODE (type))))
3347 offset = GET_MODE_SIZE (GET_MODE (target))
3348 - GET_MODE_SIZE (TYPE_MODE (type));
3349 if (! BYTES_BIG_ENDIAN)
3350 offset = (offset / UNITS_PER_WORD) * UNITS_PER_WORD;
3351 else if (! WORDS_BIG_ENDIAN)
3352 offset %= UNITS_PER_WORD;
3355 target = gen_rtx_SUBREG (TYPE_MODE (type), target, offset);
3356 SUBREG_PROMOTED_VAR_P (target) = 1;
3357 SUBREG_PROMOTED_UNSIGNED_SET (target, unsignedp);
3360 /* If size of args is variable or this was a constructor call for a stack
3361 argument, restore saved stack-pointer value. */
3363 if (old_stack_level)
3365 rtx prev = get_last_insn ();
3367 emit_stack_restore (SAVE_BLOCK, old_stack_level);
3368 stack_pointer_delta = old_stack_pointer_delta;
3370 fixup_args_size_notes (prev, get_last_insn (), stack_pointer_delta);
3372 pending_stack_adjust = old_pending_adj;
3373 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
3374 stack_arg_under_construction = old_stack_arg_under_construction;
3375 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3376 stack_usage_map = initial_stack_usage_map;
3377 sibcall_failure = 1;
3379 else if (ACCUMULATE_OUTGOING_ARGS && pass)
3381 #ifdef REG_PARM_STACK_SPACE
3382 if (save_area)
3383 restore_fixed_argument_area (save_area, argblock,
3384 high_to_save, low_to_save);
3385 #endif
3387 /* If we saved any argument areas, restore them. */
3388 for (i = 0; i < num_actuals; i++)
3389 if (args[i].save_area)
3391 enum machine_mode save_mode = GET_MODE (args[i].save_area);
3392 rtx stack_area
3393 = gen_rtx_MEM (save_mode,
3394 memory_address (save_mode,
3395 XEXP (args[i].stack_slot, 0)));
3397 if (save_mode != BLKmode)
3398 emit_move_insn (stack_area, args[i].save_area);
3399 else
3400 emit_block_move (stack_area, args[i].save_area,
3401 GEN_INT (args[i].locate.size.constant),
3402 BLOCK_OP_CALL_PARM);
3405 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3406 stack_usage_map = initial_stack_usage_map;
3409 /* If this was alloca, record the new stack level for nonlocal gotos.
3410 Check for the handler slots since we might not have a save area
3411 for non-local gotos. */
3413 if ((flags & ECF_MAY_BE_ALLOCA) && cfun->nonlocal_goto_save_area != 0)
3414 update_nonlocal_goto_save_area ();
3416 /* Free up storage we no longer need. */
3417 for (i = 0; i < num_actuals; ++i)
3418 free (args[i].aligned_regs);
3420 insns = get_insns ();
3421 end_sequence ();
3423 if (pass == 0)
3425 tail_call_insns = insns;
3427 /* Restore the pending stack adjustment now that we have
3428 finished generating the sibling call sequence. */
3430 pending_stack_adjust = save_pending_stack_adjust;
3431 stack_pointer_delta = save_stack_pointer_delta;
3433 /* Prepare arg structure for next iteration. */
3434 for (i = 0; i < num_actuals; i++)
3436 args[i].value = 0;
3437 args[i].aligned_regs = 0;
3438 args[i].stack = 0;
3441 sbitmap_free (stored_args_map);
3442 internal_arg_pointer_exp_state.scan_start = NULL_RTX;
3443 internal_arg_pointer_exp_state.cache.release ();
3445 else
3447 normal_call_insns = insns;
3449 /* Verify that we've deallocated all the stack we used. */
3450 gcc_assert ((flags & ECF_NORETURN)
3451 || (old_stack_allocated
3452 == stack_pointer_delta - pending_stack_adjust));
3455 /* If something prevents making this a sibling call,
3456 zero out the sequence. */
3457 if (sibcall_failure)
3458 tail_call_insns = NULL_RTX;
3459 else
3460 break;
3463 /* If tail call production succeeded, we need to remove REG_EQUIV notes on
3464 arguments too, as argument area is now clobbered by the call. */
3465 if (tail_call_insns)
3467 emit_insn (tail_call_insns);
3468 crtl->tail_call_emit = true;
3470 else
3471 emit_insn (normal_call_insns);
3473 currently_expanding_call--;
3475 free (stack_usage_map_buf);
3477 return target;
3480 /* A sibling call sequence invalidates any REG_EQUIV notes made for
3481 this function's incoming arguments.
3483 At the start of RTL generation we know the only REG_EQUIV notes
3484 in the rtl chain are those for incoming arguments, so we can look
3485 for REG_EQUIV notes between the start of the function and the
3486 NOTE_INSN_FUNCTION_BEG.
3488 This is (slight) overkill. We could keep track of the highest
3489 argument we clobber and be more selective in removing notes, but it
3490 does not seem to be worth the effort. */
3492 void
3493 fixup_tail_calls (void)
3495 rtx insn;
3497 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3499 rtx note;
3501 /* There are never REG_EQUIV notes for the incoming arguments
3502 after the NOTE_INSN_FUNCTION_BEG note, so stop if we see it. */
3503 if (NOTE_P (insn)
3504 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3505 break;
3507 note = find_reg_note (insn, REG_EQUIV, 0);
3508 if (note)
3509 remove_note (insn, note);
3510 note = find_reg_note (insn, REG_EQUIV, 0);
3511 gcc_assert (!note);
3515 /* Traverse a list of TYPES and expand all complex types into their
3516 components. */
3517 static tree
3518 split_complex_types (tree types)
3520 tree p;
3522 /* Before allocating memory, check for the common case of no complex. */
3523 for (p = types; p; p = TREE_CHAIN (p))
3525 tree type = TREE_VALUE (p);
3526 if (TREE_CODE (type) == COMPLEX_TYPE
3527 && targetm.calls.split_complex_arg (type))
3528 goto found;
3530 return types;
3532 found:
3533 types = copy_list (types);
3535 for (p = types; p; p = TREE_CHAIN (p))
3537 tree complex_type = TREE_VALUE (p);
3539 if (TREE_CODE (complex_type) == COMPLEX_TYPE
3540 && targetm.calls.split_complex_arg (complex_type))
3542 tree next, imag;
3544 /* Rewrite complex type with component type. */
3545 TREE_VALUE (p) = TREE_TYPE (complex_type);
3546 next = TREE_CHAIN (p);
3548 /* Add another component type for the imaginary part. */
3549 imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
3550 TREE_CHAIN (p) = imag;
3551 TREE_CHAIN (imag) = next;
3553 /* Skip the newly created node. */
3554 p = TREE_CHAIN (p);
3558 return types;
3561 /* Output a library call to function FUN (a SYMBOL_REF rtx).
3562 The RETVAL parameter specifies whether return value needs to be saved, other
3563 parameters are documented in the emit_library_call function below. */
3565 static rtx
3566 emit_library_call_value_1 (int retval, rtx orgfun, rtx value,
3567 enum libcall_type fn_type,
3568 enum machine_mode outmode, int nargs, va_list p)
3570 /* Total size in bytes of all the stack-parms scanned so far. */
3571 struct args_size args_size;
3572 /* Size of arguments before any adjustments (such as rounding). */
3573 struct args_size original_args_size;
3574 int argnum;
3575 rtx fun;
3576 /* Todo, choose the correct decl type of orgfun. Sadly this information
3577 isn't present here, so we default to native calling abi here. */
3578 tree fndecl ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
3579 tree fntype ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
3580 int inc;
3581 int count;
3582 rtx argblock = 0;
3583 CUMULATIVE_ARGS args_so_far_v;
3584 cumulative_args_t args_so_far;
3585 struct arg
3587 rtx value;
3588 enum machine_mode mode;
3589 rtx reg;
3590 int partial;
3591 struct locate_and_pad_arg_data locate;
3592 rtx save_area;
3594 struct arg *argvec;
3595 int old_inhibit_defer_pop = inhibit_defer_pop;
3596 rtx call_fusage = 0;
3597 rtx mem_value = 0;
3598 rtx valreg;
3599 int pcc_struct_value = 0;
3600 int struct_value_size = 0;
3601 int flags;
3602 int reg_parm_stack_space = 0;
3603 int needed;
3604 rtx before_call;
3605 tree tfom; /* type_for_mode (outmode, 0) */
3607 #ifdef REG_PARM_STACK_SPACE
3608 /* Define the boundary of the register parm stack space that needs to be
3609 save, if any. */
3610 int low_to_save = 0, high_to_save = 0;
3611 rtx save_area = 0; /* Place that it is saved. */
3612 #endif
3614 /* Size of the stack reserved for parameter registers. */
3615 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
3616 char *initial_stack_usage_map = stack_usage_map;
3617 char *stack_usage_map_buf = NULL;
3619 rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
3621 #ifdef REG_PARM_STACK_SPACE
3622 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
3623 #endif
3625 /* By default, library functions can not throw. */
3626 flags = ECF_NOTHROW;
3628 switch (fn_type)
3630 case LCT_NORMAL:
3631 break;
3632 case LCT_CONST:
3633 flags |= ECF_CONST;
3634 break;
3635 case LCT_PURE:
3636 flags |= ECF_PURE;
3637 break;
3638 case LCT_NORETURN:
3639 flags |= ECF_NORETURN;
3640 break;
3641 case LCT_THROW:
3642 flags = ECF_NORETURN;
3643 break;
3644 case LCT_RETURNS_TWICE:
3645 flags = ECF_RETURNS_TWICE;
3646 break;
3648 fun = orgfun;
3650 /* Ensure current function's preferred stack boundary is at least
3651 what we need. */
3652 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
3653 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3655 /* If this kind of value comes back in memory,
3656 decide where in memory it should come back. */
3657 if (outmode != VOIDmode)
3659 tfom = lang_hooks.types.type_for_mode (outmode, 0);
3660 if (aggregate_value_p (tfom, 0))
3662 #ifdef PCC_STATIC_STRUCT_RETURN
3663 rtx pointer_reg
3664 = hard_function_value (build_pointer_type (tfom), 0, 0, 0);
3665 mem_value = gen_rtx_MEM (outmode, pointer_reg);
3666 pcc_struct_value = 1;
3667 if (value == 0)
3668 value = gen_reg_rtx (outmode);
3669 #else /* not PCC_STATIC_STRUCT_RETURN */
3670 struct_value_size = GET_MODE_SIZE (outmode);
3671 if (value != 0 && MEM_P (value))
3672 mem_value = value;
3673 else
3674 mem_value = assign_temp (tfom, 1, 1);
3675 #endif
3676 /* This call returns a big structure. */
3677 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
3680 else
3681 tfom = void_type_node;
3683 /* ??? Unfinished: must pass the memory address as an argument. */
3685 /* Copy all the libcall-arguments out of the varargs data
3686 and into a vector ARGVEC.
3688 Compute how to pass each argument. We only support a very small subset
3689 of the full argument passing conventions to limit complexity here since
3690 library functions shouldn't have many args. */
3692 argvec = XALLOCAVEC (struct arg, nargs + 1);
3693 memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
3695 #ifdef INIT_CUMULATIVE_LIBCALL_ARGS
3696 INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far_v, outmode, fun);
3697 #else
3698 INIT_CUMULATIVE_ARGS (args_so_far_v, NULL_TREE, fun, 0, nargs);
3699 #endif
3700 args_so_far = pack_cumulative_args (&args_so_far_v);
3702 args_size.constant = 0;
3703 args_size.var = 0;
3705 count = 0;
3707 push_temp_slots ();
3709 /* If there's a structure value address to be passed,
3710 either pass it in the special place, or pass it as an extra argument. */
3711 if (mem_value && struct_value == 0 && ! pcc_struct_value)
3713 rtx addr = XEXP (mem_value, 0);
3715 nargs++;
3717 /* Make sure it is a reasonable operand for a move or push insn. */
3718 if (!REG_P (addr) && !MEM_P (addr)
3719 && !(CONSTANT_P (addr)
3720 && targetm.legitimate_constant_p (Pmode, addr)))
3721 addr = force_operand (addr, NULL_RTX);
3723 argvec[count].value = addr;
3724 argvec[count].mode = Pmode;
3725 argvec[count].partial = 0;
3727 argvec[count].reg = targetm.calls.function_arg (args_so_far,
3728 Pmode, NULL_TREE, true);
3729 gcc_assert (targetm.calls.arg_partial_bytes (args_so_far, Pmode,
3730 NULL_TREE, 1) == 0);
3732 locate_and_pad_parm (Pmode, NULL_TREE,
3733 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3735 #else
3736 argvec[count].reg != 0,
3737 #endif
3738 0, NULL_TREE, &args_size, &argvec[count].locate);
3740 if (argvec[count].reg == 0 || argvec[count].partial != 0
3741 || reg_parm_stack_space > 0)
3742 args_size.constant += argvec[count].locate.size.constant;
3744 targetm.calls.function_arg_advance (args_so_far, Pmode, (tree) 0, true);
3746 count++;
3749 for (; count < nargs; count++)
3751 rtx val = va_arg (p, rtx);
3752 enum machine_mode mode = (enum machine_mode) va_arg (p, int);
3753 int unsigned_p = 0;
3755 /* We cannot convert the arg value to the mode the library wants here;
3756 must do it earlier where we know the signedness of the arg. */
3757 gcc_assert (mode != BLKmode
3758 && (GET_MODE (val) == mode || GET_MODE (val) == VOIDmode));
3760 /* Make sure it is a reasonable operand for a move or push insn. */
3761 if (!REG_P (val) && !MEM_P (val)
3762 && !(CONSTANT_P (val) && targetm.legitimate_constant_p (mode, val)))
3763 val = force_operand (val, NULL_RTX);
3765 if (pass_by_reference (&args_so_far_v, mode, NULL_TREE, 1))
3767 rtx slot;
3768 int must_copy
3769 = !reference_callee_copied (&args_so_far_v, mode, NULL_TREE, 1);
3771 /* If this was a CONST function, it is now PURE since it now
3772 reads memory. */
3773 if (flags & ECF_CONST)
3775 flags &= ~ECF_CONST;
3776 flags |= ECF_PURE;
3779 if (MEM_P (val) && !must_copy)
3781 tree val_expr = MEM_EXPR (val);
3782 if (val_expr)
3783 mark_addressable (val_expr);
3784 slot = val;
3786 else
3788 slot = assign_temp (lang_hooks.types.type_for_mode (mode, 0),
3789 1, 1);
3790 emit_move_insn (slot, val);
3793 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
3794 gen_rtx_USE (VOIDmode, slot),
3795 call_fusage);
3796 if (must_copy)
3797 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
3798 gen_rtx_CLOBBER (VOIDmode,
3799 slot),
3800 call_fusage);
3802 mode = Pmode;
3803 val = force_operand (XEXP (slot, 0), NULL_RTX);
3806 mode = promote_function_mode (NULL_TREE, mode, &unsigned_p, NULL_TREE, 0);
3807 argvec[count].mode = mode;
3808 argvec[count].value = convert_modes (mode, GET_MODE (val), val, unsigned_p);
3809 argvec[count].reg = targetm.calls.function_arg (args_so_far, mode,
3810 NULL_TREE, true);
3812 argvec[count].partial
3813 = targetm.calls.arg_partial_bytes (args_so_far, mode, NULL_TREE, 1);
3815 if (argvec[count].reg == 0
3816 || argvec[count].partial != 0
3817 || reg_parm_stack_space > 0)
3819 locate_and_pad_parm (mode, NULL_TREE,
3820 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3822 #else
3823 argvec[count].reg != 0,
3824 #endif
3825 argvec[count].partial,
3826 NULL_TREE, &args_size, &argvec[count].locate);
3827 args_size.constant += argvec[count].locate.size.constant;
3828 gcc_assert (!argvec[count].locate.size.var);
3830 #ifdef BLOCK_REG_PADDING
3831 else
3832 /* The argument is passed entirely in registers. See at which
3833 end it should be padded. */
3834 argvec[count].locate.where_pad =
3835 BLOCK_REG_PADDING (mode, NULL_TREE,
3836 GET_MODE_SIZE (mode) <= UNITS_PER_WORD);
3837 #endif
3839 targetm.calls.function_arg_advance (args_so_far, mode, (tree) 0, true);
3842 /* If this machine requires an external definition for library
3843 functions, write one out. */
3844 assemble_external_libcall (fun);
3846 original_args_size = args_size;
3847 args_size.constant = (((args_size.constant
3848 + stack_pointer_delta
3849 + STACK_BYTES - 1)
3850 / STACK_BYTES
3851 * STACK_BYTES)
3852 - stack_pointer_delta);
3854 args_size.constant = MAX (args_size.constant,
3855 reg_parm_stack_space);
3857 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
3858 args_size.constant -= reg_parm_stack_space;
3860 if (args_size.constant > crtl->outgoing_args_size)
3861 crtl->outgoing_args_size = args_size.constant;
3863 if (flag_stack_usage_info && !ACCUMULATE_OUTGOING_ARGS)
3865 int pushed = args_size.constant + pending_stack_adjust;
3866 if (pushed > current_function_pushed_stack_size)
3867 current_function_pushed_stack_size = pushed;
3870 if (ACCUMULATE_OUTGOING_ARGS)
3872 /* Since the stack pointer will never be pushed, it is possible for
3873 the evaluation of a parm to clobber something we have already
3874 written to the stack. Since most function calls on RISC machines
3875 do not use the stack, this is uncommon, but must work correctly.
3877 Therefore, we save any area of the stack that was already written
3878 and that we are using. Here we set up to do this by making a new
3879 stack usage map from the old one.
3881 Another approach might be to try to reorder the argument
3882 evaluations to avoid this conflicting stack usage. */
3884 needed = args_size.constant;
3886 /* Since we will be writing into the entire argument area, the
3887 map must be allocated for its entire size, not just the part that
3888 is the responsibility of the caller. */
3889 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
3890 needed += reg_parm_stack_space;
3892 #ifdef ARGS_GROW_DOWNWARD
3893 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3894 needed + 1);
3895 #else
3896 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3897 needed);
3898 #endif
3899 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
3900 stack_usage_map = stack_usage_map_buf;
3902 if (initial_highest_arg_in_use)
3903 memcpy (stack_usage_map, initial_stack_usage_map,
3904 initial_highest_arg_in_use);
3906 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
3907 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
3908 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
3909 needed = 0;
3911 /* We must be careful to use virtual regs before they're instantiated,
3912 and real regs afterwards. Loop optimization, for example, can create
3913 new libcalls after we've instantiated the virtual regs, and if we
3914 use virtuals anyway, they won't match the rtl patterns. */
3916 if (virtuals_instantiated)
3917 argblock = plus_constant (Pmode, stack_pointer_rtx,
3918 STACK_POINTER_OFFSET);
3919 else
3920 argblock = virtual_outgoing_args_rtx;
3922 else
3924 if (!PUSH_ARGS)
3925 argblock = push_block (GEN_INT (args_size.constant), 0, 0);
3928 /* If we push args individually in reverse order, perform stack alignment
3929 before the first push (the last arg). */
3930 if (argblock == 0 && PUSH_ARGS_REVERSED)
3931 anti_adjust_stack (GEN_INT (args_size.constant
3932 - original_args_size.constant));
3934 if (PUSH_ARGS_REVERSED)
3936 inc = -1;
3937 argnum = nargs - 1;
3939 else
3941 inc = 1;
3942 argnum = 0;
3945 #ifdef REG_PARM_STACK_SPACE
3946 if (ACCUMULATE_OUTGOING_ARGS)
3948 /* The argument list is the property of the called routine and it
3949 may clobber it. If the fixed area has been used for previous
3950 parameters, we must save and restore it. */
3951 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3952 &low_to_save, &high_to_save);
3954 #endif
3956 /* Push the args that need to be pushed. */
3958 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
3959 are to be pushed. */
3960 for (count = 0; count < nargs; count++, argnum += inc)
3962 enum machine_mode mode = argvec[argnum].mode;
3963 rtx val = argvec[argnum].value;
3964 rtx reg = argvec[argnum].reg;
3965 int partial = argvec[argnum].partial;
3966 unsigned int parm_align = argvec[argnum].locate.boundary;
3967 int lower_bound = 0, upper_bound = 0, i;
3969 if (! (reg != 0 && partial == 0))
3971 rtx use;
3973 if (ACCUMULATE_OUTGOING_ARGS)
3975 /* If this is being stored into a pre-allocated, fixed-size,
3976 stack area, save any previous data at that location. */
3978 #ifdef ARGS_GROW_DOWNWARD
3979 /* stack_slot is negative, but we want to index stack_usage_map
3980 with positive values. */
3981 upper_bound = -argvec[argnum].locate.slot_offset.constant + 1;
3982 lower_bound = upper_bound - argvec[argnum].locate.size.constant;
3983 #else
3984 lower_bound = argvec[argnum].locate.slot_offset.constant;
3985 upper_bound = lower_bound + argvec[argnum].locate.size.constant;
3986 #endif
3988 i = lower_bound;
3989 /* Don't worry about things in the fixed argument area;
3990 it has already been saved. */
3991 if (i < reg_parm_stack_space)
3992 i = reg_parm_stack_space;
3993 while (i < upper_bound && stack_usage_map[i] == 0)
3994 i++;
3996 if (i < upper_bound)
3998 /* We need to make a save area. */
3999 unsigned int size
4000 = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
4001 enum machine_mode save_mode
4002 = mode_for_size (size, MODE_INT, 1);
4003 rtx adr
4004 = plus_constant (Pmode, argblock,
4005 argvec[argnum].locate.offset.constant);
4006 rtx stack_area
4007 = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
4009 if (save_mode == BLKmode)
4011 argvec[argnum].save_area
4012 = assign_stack_temp (BLKmode,
4013 argvec[argnum].locate.size.constant
4016 emit_block_move (validize_mem (argvec[argnum].save_area),
4017 stack_area,
4018 GEN_INT (argvec[argnum].locate.size.constant),
4019 BLOCK_OP_CALL_PARM);
4021 else
4023 argvec[argnum].save_area = gen_reg_rtx (save_mode);
4025 emit_move_insn (argvec[argnum].save_area, stack_area);
4030 emit_push_insn (val, mode, NULL_TREE, NULL_RTX, parm_align,
4031 partial, reg, 0, argblock,
4032 GEN_INT (argvec[argnum].locate.offset.constant),
4033 reg_parm_stack_space,
4034 ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad));
4036 /* Now mark the segment we just used. */
4037 if (ACCUMULATE_OUTGOING_ARGS)
4038 for (i = lower_bound; i < upper_bound; i++)
4039 stack_usage_map[i] = 1;
4041 NO_DEFER_POP;
4043 /* Indicate argument access so that alias.c knows that these
4044 values are live. */
4045 if (argblock)
4046 use = plus_constant (Pmode, argblock,
4047 argvec[argnum].locate.offset.constant);
4048 else
4049 /* When arguments are pushed, trying to tell alias.c where
4050 exactly this argument is won't work, because the
4051 auto-increment causes confusion. So we merely indicate
4052 that we access something with a known mode somewhere on
4053 the stack. */
4054 use = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4055 gen_rtx_SCRATCH (Pmode));
4056 use = gen_rtx_MEM (argvec[argnum].mode, use);
4057 use = gen_rtx_USE (VOIDmode, use);
4058 call_fusage = gen_rtx_EXPR_LIST (VOIDmode, use, call_fusage);
4062 /* If we pushed args in forward order, perform stack alignment
4063 after pushing the last arg. */
4064 if (argblock == 0 && !PUSH_ARGS_REVERSED)
4065 anti_adjust_stack (GEN_INT (args_size.constant
4066 - original_args_size.constant));
4068 if (PUSH_ARGS_REVERSED)
4069 argnum = nargs - 1;
4070 else
4071 argnum = 0;
4073 fun = prepare_call_address (NULL, fun, NULL, &call_fusage, 0, 0);
4075 /* Now load any reg parms into their regs. */
4077 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4078 are to be pushed. */
4079 for (count = 0; count < nargs; count++, argnum += inc)
4081 enum machine_mode mode = argvec[argnum].mode;
4082 rtx val = argvec[argnum].value;
4083 rtx reg = argvec[argnum].reg;
4084 int partial = argvec[argnum].partial;
4085 #ifdef BLOCK_REG_PADDING
4086 int size = 0;
4087 #endif
4089 /* Handle calls that pass values in multiple non-contiguous
4090 locations. The PA64 has examples of this for library calls. */
4091 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4092 emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (mode));
4093 else if (reg != 0 && partial == 0)
4095 emit_move_insn (reg, val);
4096 #ifdef BLOCK_REG_PADDING
4097 size = GET_MODE_SIZE (argvec[argnum].mode);
4099 /* Copied from load_register_parameters. */
4101 /* Handle case where we have a value that needs shifting
4102 up to the msb. eg. a QImode value and we're padding
4103 upward on a BYTES_BIG_ENDIAN machine. */
4104 if (size < UNITS_PER_WORD
4105 && (argvec[argnum].locate.where_pad
4106 == (BYTES_BIG_ENDIAN ? upward : downward)))
4108 rtx x;
4109 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
4111 /* Assigning REG here rather than a temp makes CALL_FUSAGE
4112 report the whole reg as used. Strictly speaking, the
4113 call only uses SIZE bytes at the msb end, but it doesn't
4114 seem worth generating rtl to say that. */
4115 reg = gen_rtx_REG (word_mode, REGNO (reg));
4116 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
4117 if (x != reg)
4118 emit_move_insn (reg, x);
4120 #endif
4123 NO_DEFER_POP;
4126 /* Any regs containing parms remain in use through the call. */
4127 for (count = 0; count < nargs; count++)
4129 rtx reg = argvec[count].reg;
4130 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4131 use_group_regs (&call_fusage, reg);
4132 else if (reg != 0)
4134 int partial = argvec[count].partial;
4135 if (partial)
4137 int nregs;
4138 gcc_assert (partial % UNITS_PER_WORD == 0);
4139 nregs = partial / UNITS_PER_WORD;
4140 use_regs (&call_fusage, REGNO (reg), nregs);
4142 else
4143 use_reg (&call_fusage, reg);
4147 /* Pass the function the address in which to return a structure value. */
4148 if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
4150 emit_move_insn (struct_value,
4151 force_reg (Pmode,
4152 force_operand (XEXP (mem_value, 0),
4153 NULL_RTX)));
4154 if (REG_P (struct_value))
4155 use_reg (&call_fusage, struct_value);
4158 /* Don't allow popping to be deferred, since then
4159 cse'ing of library calls could delete a call and leave the pop. */
4160 NO_DEFER_POP;
4161 valreg = (mem_value == 0 && outmode != VOIDmode
4162 ? hard_libcall_value (outmode, orgfun) : NULL_RTX);
4164 /* Stack must be properly aligned now. */
4165 gcc_assert (!(stack_pointer_delta
4166 & (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1)));
4168 before_call = get_last_insn ();
4170 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
4171 will set inhibit_defer_pop to that value. */
4172 /* The return type is needed to decide how many bytes the function pops.
4173 Signedness plays no role in that, so for simplicity, we pretend it's
4174 always signed. We also assume that the list of arguments passed has
4175 no impact, so we pretend it is unknown. */
4177 emit_call_1 (fun, NULL,
4178 get_identifier (XSTR (orgfun, 0)),
4179 build_function_type (tfom, NULL_TREE),
4180 original_args_size.constant, args_size.constant,
4181 struct_value_size,
4182 targetm.calls.function_arg (args_so_far,
4183 VOIDmode, void_type_node, true),
4184 valreg,
4185 old_inhibit_defer_pop + 1, call_fusage, flags, args_so_far);
4187 /* Right-shift returned value if necessary. */
4188 if (!pcc_struct_value
4189 && TYPE_MODE (tfom) != BLKmode
4190 && targetm.calls.return_in_msb (tfom))
4192 shift_return_value (TYPE_MODE (tfom), false, valreg);
4193 valreg = gen_rtx_REG (TYPE_MODE (tfom), REGNO (valreg));
4196 /* For calls to `setjmp', etc., inform function.c:setjmp_warnings
4197 that it should complain if nonvolatile values are live. For
4198 functions that cannot return, inform flow that control does not
4199 fall through. */
4200 if (flags & ECF_NORETURN)
4202 /* The barrier note must be emitted
4203 immediately after the CALL_INSN. Some ports emit more than
4204 just a CALL_INSN above, so we must search for it here. */
4205 rtx last = get_last_insn ();
4206 while (!CALL_P (last))
4208 last = PREV_INSN (last);
4209 /* There was no CALL_INSN? */
4210 gcc_assert (last != before_call);
4213 emit_barrier_after (last);
4216 /* Consider that "regular" libcalls, i.e. all of them except for LCT_THROW
4217 and LCT_RETURNS_TWICE, cannot perform non-local gotos. */
4218 if (flags & ECF_NOTHROW)
4220 rtx last = get_last_insn ();
4221 while (!CALL_P (last))
4223 last = PREV_INSN (last);
4224 /* There was no CALL_INSN? */
4225 gcc_assert (last != before_call);
4228 make_reg_eh_region_note_nothrow_nononlocal (last);
4231 /* Now restore inhibit_defer_pop to its actual original value. */
4232 OK_DEFER_POP;
4234 pop_temp_slots ();
4236 /* Copy the value to the right place. */
4237 if (outmode != VOIDmode && retval)
4239 if (mem_value)
4241 if (value == 0)
4242 value = mem_value;
4243 if (value != mem_value)
4244 emit_move_insn (value, mem_value);
4246 else if (GET_CODE (valreg) == PARALLEL)
4248 if (value == 0)
4249 value = gen_reg_rtx (outmode);
4250 emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
4252 else
4254 /* Convert to the proper mode if a promotion has been active. */
4255 if (GET_MODE (valreg) != outmode)
4257 int unsignedp = TYPE_UNSIGNED (tfom);
4259 gcc_assert (promote_function_mode (tfom, outmode, &unsignedp,
4260 fndecl ? TREE_TYPE (fndecl) : fntype, 1)
4261 == GET_MODE (valreg));
4262 valreg = convert_modes (outmode, GET_MODE (valreg), valreg, 0);
4265 if (value != 0)
4266 emit_move_insn (value, valreg);
4267 else
4268 value = valreg;
4272 if (ACCUMULATE_OUTGOING_ARGS)
4274 #ifdef REG_PARM_STACK_SPACE
4275 if (save_area)
4276 restore_fixed_argument_area (save_area, argblock,
4277 high_to_save, low_to_save);
4278 #endif
4280 /* If we saved any argument areas, restore them. */
4281 for (count = 0; count < nargs; count++)
4282 if (argvec[count].save_area)
4284 enum machine_mode save_mode = GET_MODE (argvec[count].save_area);
4285 rtx adr = plus_constant (Pmode, argblock,
4286 argvec[count].locate.offset.constant);
4287 rtx stack_area = gen_rtx_MEM (save_mode,
4288 memory_address (save_mode, adr));
4290 if (save_mode == BLKmode)
4291 emit_block_move (stack_area,
4292 validize_mem (argvec[count].save_area),
4293 GEN_INT (argvec[count].locate.size.constant),
4294 BLOCK_OP_CALL_PARM);
4295 else
4296 emit_move_insn (stack_area, argvec[count].save_area);
4299 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
4300 stack_usage_map = initial_stack_usage_map;
4303 free (stack_usage_map_buf);
4305 return value;
4309 /* Output a library call to function FUN (a SYMBOL_REF rtx)
4310 (emitting the queue unless NO_QUEUE is nonzero),
4311 for a value of mode OUTMODE,
4312 with NARGS different arguments, passed as alternating rtx values
4313 and machine_modes to convert them to.
4315 FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for
4316 `const' calls, LCT_PURE for `pure' calls, or other LCT_ value for
4317 other types of library calls. */
4319 void
4320 emit_library_call (rtx orgfun, enum libcall_type fn_type,
4321 enum machine_mode outmode, int nargs, ...)
4323 va_list p;
4325 va_start (p, nargs);
4326 emit_library_call_value_1 (0, orgfun, NULL_RTX, fn_type, outmode, nargs, p);
4327 va_end (p);
4330 /* Like emit_library_call except that an extra argument, VALUE,
4331 comes second and says where to store the result.
4332 (If VALUE is zero, this function chooses a convenient way
4333 to return the value.
4335 This function returns an rtx for where the value is to be found.
4336 If VALUE is nonzero, VALUE is returned. */
4339 emit_library_call_value (rtx orgfun, rtx value,
4340 enum libcall_type fn_type,
4341 enum machine_mode outmode, int nargs, ...)
4343 rtx result;
4344 va_list p;
4346 va_start (p, nargs);
4347 result = emit_library_call_value_1 (1, orgfun, value, fn_type, outmode,
4348 nargs, p);
4349 va_end (p);
4351 return result;
4354 /* Store a single argument for a function call
4355 into the register or memory area where it must be passed.
4356 *ARG describes the argument value and where to pass it.
4358 ARGBLOCK is the address of the stack-block for all the arguments,
4359 or 0 on a machine where arguments are pushed individually.
4361 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
4362 so must be careful about how the stack is used.
4364 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
4365 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
4366 that we need not worry about saving and restoring the stack.
4368 FNDECL is the declaration of the function we are calling.
4370 Return nonzero if this arg should cause sibcall failure,
4371 zero otherwise. */
4373 static int
4374 store_one_arg (struct arg_data *arg, rtx argblock, int flags,
4375 int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
4377 tree pval = arg->tree_value;
4378 rtx reg = 0;
4379 int partial = 0;
4380 int used = 0;
4381 int i, lower_bound = 0, upper_bound = 0;
4382 int sibcall_failure = 0;
4384 if (TREE_CODE (pval) == ERROR_MARK)
4385 return 1;
4387 /* Push a new temporary level for any temporaries we make for
4388 this argument. */
4389 push_temp_slots ();
4391 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
4393 /* If this is being stored into a pre-allocated, fixed-size, stack area,
4394 save any previous data at that location. */
4395 if (argblock && ! variable_size && arg->stack)
4397 #ifdef ARGS_GROW_DOWNWARD
4398 /* stack_slot is negative, but we want to index stack_usage_map
4399 with positive values. */
4400 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4401 upper_bound = -INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1)) + 1;
4402 else
4403 upper_bound = 0;
4405 lower_bound = upper_bound - arg->locate.size.constant;
4406 #else
4407 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4408 lower_bound = INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1));
4409 else
4410 lower_bound = 0;
4412 upper_bound = lower_bound + arg->locate.size.constant;
4413 #endif
4415 i = lower_bound;
4416 /* Don't worry about things in the fixed argument area;
4417 it has already been saved. */
4418 if (i < reg_parm_stack_space)
4419 i = reg_parm_stack_space;
4420 while (i < upper_bound && stack_usage_map[i] == 0)
4421 i++;
4423 if (i < upper_bound)
4425 /* We need to make a save area. */
4426 unsigned int size = arg->locate.size.constant * BITS_PER_UNIT;
4427 enum machine_mode save_mode = mode_for_size (size, MODE_INT, 1);
4428 rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
4429 rtx stack_area = gen_rtx_MEM (save_mode, adr);
4431 if (save_mode == BLKmode)
4433 tree ot = TREE_TYPE (arg->tree_value);
4434 tree nt = build_qualified_type (ot, (TYPE_QUALS (ot)
4435 | TYPE_QUAL_CONST));
4437 arg->save_area = assign_temp (nt, 1, 1);
4438 preserve_temp_slots (arg->save_area);
4439 emit_block_move (validize_mem (arg->save_area), stack_area,
4440 GEN_INT (arg->locate.size.constant),
4441 BLOCK_OP_CALL_PARM);
4443 else
4445 arg->save_area = gen_reg_rtx (save_mode);
4446 emit_move_insn (arg->save_area, stack_area);
4452 /* If this isn't going to be placed on both the stack and in registers,
4453 set up the register and number of words. */
4454 if (! arg->pass_on_stack)
4456 if (flags & ECF_SIBCALL)
4457 reg = arg->tail_call_reg;
4458 else
4459 reg = arg->reg;
4460 partial = arg->partial;
4463 /* Being passed entirely in a register. We shouldn't be called in
4464 this case. */
4465 gcc_assert (reg == 0 || partial != 0);
4467 /* If this arg needs special alignment, don't load the registers
4468 here. */
4469 if (arg->n_aligned_regs != 0)
4470 reg = 0;
4472 /* If this is being passed partially in a register, we can't evaluate
4473 it directly into its stack slot. Otherwise, we can. */
4474 if (arg->value == 0)
4476 /* stack_arg_under_construction is nonzero if a function argument is
4477 being evaluated directly into the outgoing argument list and
4478 expand_call must take special action to preserve the argument list
4479 if it is called recursively.
4481 For scalar function arguments stack_usage_map is sufficient to
4482 determine which stack slots must be saved and restored. Scalar
4483 arguments in general have pass_on_stack == 0.
4485 If this argument is initialized by a function which takes the
4486 address of the argument (a C++ constructor or a C function
4487 returning a BLKmode structure), then stack_usage_map is
4488 insufficient and expand_call must push the stack around the
4489 function call. Such arguments have pass_on_stack == 1.
4491 Note that it is always safe to set stack_arg_under_construction,
4492 but this generates suboptimal code if set when not needed. */
4494 if (arg->pass_on_stack)
4495 stack_arg_under_construction++;
4497 arg->value = expand_expr (pval,
4498 (partial
4499 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
4500 ? NULL_RTX : arg->stack,
4501 VOIDmode, EXPAND_STACK_PARM);
4503 /* If we are promoting object (or for any other reason) the mode
4504 doesn't agree, convert the mode. */
4506 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
4507 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
4508 arg->value, arg->unsignedp);
4510 if (arg->pass_on_stack)
4511 stack_arg_under_construction--;
4514 /* Check for overlap with already clobbered argument area. */
4515 if ((flags & ECF_SIBCALL)
4516 && MEM_P (arg->value)
4517 && mem_overlaps_already_clobbered_arg_p (XEXP (arg->value, 0),
4518 arg->locate.size.constant))
4519 sibcall_failure = 1;
4521 /* Don't allow anything left on stack from computation
4522 of argument to alloca. */
4523 if (flags & ECF_MAY_BE_ALLOCA)
4524 do_pending_stack_adjust ();
4526 if (arg->value == arg->stack)
4527 /* If the value is already in the stack slot, we are done. */
4529 else if (arg->mode != BLKmode)
4531 int size;
4532 unsigned int parm_align;
4534 /* Argument is a scalar, not entirely passed in registers.
4535 (If part is passed in registers, arg->partial says how much
4536 and emit_push_insn will take care of putting it there.)
4538 Push it, and if its size is less than the
4539 amount of space allocated to it,
4540 also bump stack pointer by the additional space.
4541 Note that in C the default argument promotions
4542 will prevent such mismatches. */
4544 size = GET_MODE_SIZE (arg->mode);
4545 /* Compute how much space the push instruction will push.
4546 On many machines, pushing a byte will advance the stack
4547 pointer by a halfword. */
4548 #ifdef PUSH_ROUNDING
4549 size = PUSH_ROUNDING (size);
4550 #endif
4551 used = size;
4553 /* Compute how much space the argument should get:
4554 round up to a multiple of the alignment for arguments. */
4555 if (none != FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)))
4556 used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
4557 / (PARM_BOUNDARY / BITS_PER_UNIT))
4558 * (PARM_BOUNDARY / BITS_PER_UNIT));
4560 /* Compute the alignment of the pushed argument. */
4561 parm_align = arg->locate.boundary;
4562 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
4564 int pad = used - size;
4565 if (pad)
4567 unsigned int pad_align = (pad & -pad) * BITS_PER_UNIT;
4568 parm_align = MIN (parm_align, pad_align);
4572 /* This isn't already where we want it on the stack, so put it there.
4573 This can either be done with push or copy insns. */
4574 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), NULL_RTX,
4575 parm_align, partial, reg, used - size, argblock,
4576 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4577 ARGS_SIZE_RTX (arg->locate.alignment_pad));
4579 /* Unless this is a partially-in-register argument, the argument is now
4580 in the stack. */
4581 if (partial == 0)
4582 arg->value = arg->stack;
4584 else
4586 /* BLKmode, at least partly to be pushed. */
4588 unsigned int parm_align;
4589 int excess;
4590 rtx size_rtx;
4592 /* Pushing a nonscalar.
4593 If part is passed in registers, PARTIAL says how much
4594 and emit_push_insn will take care of putting it there. */
4596 /* Round its size up to a multiple
4597 of the allocation unit for arguments. */
4599 if (arg->locate.size.var != 0)
4601 excess = 0;
4602 size_rtx = ARGS_SIZE_RTX (arg->locate.size);
4604 else
4606 /* PUSH_ROUNDING has no effect on us, because emit_push_insn
4607 for BLKmode is careful to avoid it. */
4608 excess = (arg->locate.size.constant
4609 - int_size_in_bytes (TREE_TYPE (pval))
4610 + partial);
4611 size_rtx = expand_expr (size_in_bytes (TREE_TYPE (pval)),
4612 NULL_RTX, TYPE_MODE (sizetype),
4613 EXPAND_NORMAL);
4616 parm_align = arg->locate.boundary;
4618 /* When an argument is padded down, the block is aligned to
4619 PARM_BOUNDARY, but the actual argument isn't. */
4620 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
4622 if (arg->locate.size.var)
4623 parm_align = BITS_PER_UNIT;
4624 else if (excess)
4626 unsigned int excess_align = (excess & -excess) * BITS_PER_UNIT;
4627 parm_align = MIN (parm_align, excess_align);
4631 if ((flags & ECF_SIBCALL) && MEM_P (arg->value))
4633 /* emit_push_insn might not work properly if arg->value and
4634 argblock + arg->locate.offset areas overlap. */
4635 rtx x = arg->value;
4636 int i = 0;
4638 if (XEXP (x, 0) == crtl->args.internal_arg_pointer
4639 || (GET_CODE (XEXP (x, 0)) == PLUS
4640 && XEXP (XEXP (x, 0), 0) ==
4641 crtl->args.internal_arg_pointer
4642 && CONST_INT_P (XEXP (XEXP (x, 0), 1))))
4644 if (XEXP (x, 0) != crtl->args.internal_arg_pointer)
4645 i = INTVAL (XEXP (XEXP (x, 0), 1));
4647 /* expand_call should ensure this. */
4648 gcc_assert (!arg->locate.offset.var
4649 && arg->locate.size.var == 0
4650 && CONST_INT_P (size_rtx));
4652 if (arg->locate.offset.constant > i)
4654 if (arg->locate.offset.constant < i + INTVAL (size_rtx))
4655 sibcall_failure = 1;
4657 else if (arg->locate.offset.constant < i)
4659 /* Use arg->locate.size.constant instead of size_rtx
4660 because we only care about the part of the argument
4661 on the stack. */
4662 if (i < (arg->locate.offset.constant
4663 + arg->locate.size.constant))
4664 sibcall_failure = 1;
4666 else
4668 /* Even though they appear to be at the same location,
4669 if part of the outgoing argument is in registers,
4670 they aren't really at the same location. Check for
4671 this by making sure that the incoming size is the
4672 same as the outgoing size. */
4673 if (arg->locate.size.constant != INTVAL (size_rtx))
4674 sibcall_failure = 1;
4679 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
4680 parm_align, partial, reg, excess, argblock,
4681 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4682 ARGS_SIZE_RTX (arg->locate.alignment_pad));
4684 /* Unless this is a partially-in-register argument, the argument is now
4685 in the stack.
4687 ??? Unlike the case above, in which we want the actual
4688 address of the data, so that we can load it directly into a
4689 register, here we want the address of the stack slot, so that
4690 it's properly aligned for word-by-word copying or something
4691 like that. It's not clear that this is always correct. */
4692 if (partial == 0)
4693 arg->value = arg->stack_slot;
4696 if (arg->reg && GET_CODE (arg->reg) == PARALLEL)
4698 tree type = TREE_TYPE (arg->tree_value);
4699 arg->parallel_value
4700 = emit_group_load_into_temps (arg->reg, arg->value, type,
4701 int_size_in_bytes (type));
4704 /* Mark all slots this store used. */
4705 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
4706 && argblock && ! variable_size && arg->stack)
4707 for (i = lower_bound; i < upper_bound; i++)
4708 stack_usage_map[i] = 1;
4710 /* Once we have pushed something, pops can't safely
4711 be deferred during the rest of the arguments. */
4712 NO_DEFER_POP;
4714 /* Free any temporary slots made in processing this argument. */
4715 pop_temp_slots ();
4717 return sibcall_failure;
4720 /* Nonzero if we do not know how to pass TYPE solely in registers. */
4722 bool
4723 must_pass_in_stack_var_size (enum machine_mode mode ATTRIBUTE_UNUSED,
4724 const_tree type)
4726 if (!type)
4727 return false;
4729 /* If the type has variable size... */
4730 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4731 return true;
4733 /* If the type is marked as addressable (it is required
4734 to be constructed into the stack)... */
4735 if (TREE_ADDRESSABLE (type))
4736 return true;
4738 return false;
4741 /* Another version of the TARGET_MUST_PASS_IN_STACK hook. This one
4742 takes trailing padding of a structure into account. */
4743 /* ??? Should be able to merge these two by examining BLOCK_REG_PADDING. */
4745 bool
4746 must_pass_in_stack_var_size_or_pad (enum machine_mode mode, const_tree type)
4748 if (!type)
4749 return false;
4751 /* If the type has variable size... */
4752 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4753 return true;
4755 /* If the type is marked as addressable (it is required
4756 to be constructed into the stack)... */
4757 if (TREE_ADDRESSABLE (type))
4758 return true;
4760 /* If the padding and mode of the type is such that a copy into
4761 a register would put it into the wrong part of the register. */
4762 if (mode == BLKmode
4763 && int_size_in_bytes (type) % (PARM_BOUNDARY / BITS_PER_UNIT)
4764 && (FUNCTION_ARG_PADDING (mode, type)
4765 == (BYTES_BIG_ENDIAN ? upward : downward)))
4766 return true;
4768 return false;