118334Speter/* Convert function calls to rtl insns, for GNU C compiler.
2132718Skan   Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3169689Skan   1999, 2000, 2001, 2002, 2003, 2004, 2005
4169689Skan   Free Software Foundation, Inc.
518334Speter
690075SobrienThis file is part of GCC.
718334Speter
890075SobrienGCC is free software; you can redistribute it and/or modify it under
990075Sobrienthe terms of the GNU General Public License as published by the Free
1090075SobrienSoftware Foundation; either version 2, or (at your option) any later
1190075Sobrienversion.
1218334Speter
1390075SobrienGCC is distributed in the hope that it will be useful, but WITHOUT ANY
1490075SobrienWARRANTY; without even the implied warranty of MERCHANTABILITY or
1590075SobrienFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1690075Sobrienfor more details.
1718334Speter
1818334SpeterYou should have received a copy of the GNU General Public License
1990075Sobrienalong with GCC; see the file COPYING.  If not, write to the Free
20169689SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21169689Skan02110-1301, USA.  */
2218334Speter
2318334Speter#include "config.h"
2450397Sobrien#include "system.h"
25132718Skan#include "coretypes.h"
26132718Skan#include "tm.h"
2750397Sobrien#include "rtl.h"
2850397Sobrien#include "tree.h"
2950397Sobrien#include "flags.h"
3050397Sobrien#include "expr.h"
31132718Skan#include "optabs.h"
3290075Sobrien#include "libfuncs.h"
3390075Sobrien#include "function.h"
3450397Sobrien#include "regs.h"
3550397Sobrien#include "toplev.h"
3650397Sobrien#include "output.h"
3790075Sobrien#include "tm_p.h"
3890075Sobrien#include "timevar.h"
3990075Sobrien#include "sbitmap.h"
40117395Skan#include "langhooks.h"
41117395Skan#include "target.h"
42132718Skan#include "cgraph.h"
43117395Skan#include "except.h"
4418334Speter
4552284Sobrien/* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits.  */
4652284Sobrien#define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
4718334Speter
4818334Speter/* Data structure and subroutines used within expand_call.  */
4918334Speter
5018334Speterstruct arg_data
5118334Speter{
5218334Speter  /* Tree node for this argument.  */
5318334Speter  tree tree_value;
5418334Speter  /* Mode for value; TYPE_MODE unless promoted.  */
5518334Speter  enum machine_mode mode;
5618334Speter  /* Current RTL value for argument, or 0 if it isn't precomputed.  */
5718334Speter  rtx value;
5818334Speter  /* Initially-compute RTL value for argument; only for const functions.  */
5918334Speter  rtx initial_value;
6018334Speter  /* Register to pass this argument in, 0 if passed on stack, or an
6150397Sobrien     PARALLEL if the arg is to be copied into multiple non-contiguous
6218334Speter     registers.  */
6318334Speter  rtx reg;
6490075Sobrien  /* Register to pass this argument in when generating tail call sequence.
6590075Sobrien     This is not the same register as for normal calls on machines with
6690075Sobrien     register windows.  */
6790075Sobrien  rtx tail_call_reg;
68169689Skan  /* If REG is a PARALLEL, this is a copy of VALUE pulled into the correct
69169689Skan     form for emit_group_move.  */
70169689Skan  rtx parallel_value;
7118334Speter  /* If REG was promoted from the actual mode of the argument expression,
7218334Speter     indicates whether the promotion is sign- or zero-extended.  */
7318334Speter  int unsignedp;
74169689Skan  /* Number of bytes to put in registers.  0 means put the whole arg
75169689Skan     in registers.  Also 0 if not passed in registers.  */
7618334Speter  int partial;
77117395Skan  /* Nonzero if argument must be passed on stack.
7818334Speter     Note that some arguments may be passed on the stack
7918334Speter     even though pass_on_stack is zero, just because FUNCTION_ARG says so.
8018334Speter     pass_on_stack identifies arguments that *cannot* go in registers.  */
8118334Speter  int pass_on_stack;
82132718Skan  /* Some fields packaged up for locate_and_pad_parm.  */
83132718Skan  struct locate_and_pad_arg_data locate;
8418334Speter  /* Location on the stack at which parameter should be stored.  The store
8518334Speter     has already been done if STACK == VALUE.  */
8618334Speter  rtx stack;
8718334Speter  /* Location on the stack of the start of this argument slot.  This can
8818334Speter     differ from STACK if this arg pads downward.  This location is known
8918334Speter     to be aligned to FUNCTION_ARG_BOUNDARY.  */
9018334Speter  rtx stack_slot;
9118334Speter  /* Place that this stack area has been saved, if needed.  */
9218334Speter  rtx save_area;
9318334Speter  /* If an argument's alignment does not permit direct copying into registers,
9418334Speter     copy in smaller-sized pieces into pseudos.  These are stored in a
9518334Speter     block pointed to by this field.  The next field says how many
9618334Speter     word-sized pseudos we made.  */
9718334Speter  rtx *aligned_regs;
9818334Speter  int n_aligned_regs;
9918334Speter};
10018334Speter
101117395Skan/* A vector of one char per byte of stack space.  A byte if nonzero if
10218334Speter   the corresponding stack location has been used.
10318334Speter   This vector is used to prevent a function call within an argument from
10418334Speter   clobbering any stack already set up.  */
10518334Speterstatic char *stack_usage_map;
10618334Speter
10718334Speter/* Size of STACK_USAGE_MAP.  */
10818334Speterstatic int highest_outgoing_arg_in_use;
10918334Speter
11090075Sobrien/* A bitmap of virtual-incoming stack space.  Bit is set if the corresponding
11190075Sobrien   stack location's tail call argument has been already stored into the stack.
11290075Sobrien   This bitmap is used to prevent sibling call optimization if function tries
11390075Sobrien   to use parent's incoming argument slots when they have been already
11490075Sobrien   overwritten with tail call arguments.  */
11590075Sobrienstatic sbitmap stored_args_map;
11690075Sobrien
11718334Speter/* stack_arg_under_construction is nonzero when an argument may be
11818334Speter   initialized with a constructor call (including a C function that
11918334Speter   returns a BLKmode struct) and expand_call must take special action
12018334Speter   to make sure the object being constructed does not overlap the
12118334Speter   argument list for the constructor call.  */
122169689Skanstatic int stack_arg_under_construction;
12318334Speter
124169689Skanstatic void emit_call_1 (rtx, tree, tree, tree, HOST_WIDE_INT, HOST_WIDE_INT,
125132718Skan			 HOST_WIDE_INT, rtx, rtx, int, rtx, int,
126132718Skan			 CUMULATIVE_ARGS *);
127132718Skanstatic void precompute_register_parameters (int, struct arg_data *, int *);
128132718Skanstatic int store_one_arg (struct arg_data *, rtx, int, int, int);
129132718Skanstatic void store_unaligned_arguments_into_pseudos (struct arg_data *, int);
130132718Skanstatic int finalize_must_preallocate (int, int, struct arg_data *,
131132718Skan				      struct args_size *);
132132718Skanstatic void precompute_arguments (int, int, struct arg_data *);
133132718Skanstatic int compute_argument_block_size (int, struct args_size *, int);
134132718Skanstatic void initialize_argument_information (int, struct arg_data *,
135132718Skan					     struct args_size *, int, tree,
136132718Skan					     tree, CUMULATIVE_ARGS *, int,
137132718Skan					     rtx *, int *, int *, int *,
138169689Skan					     bool *, bool);
139132718Skanstatic void compute_argument_addresses (struct arg_data *, rtx, int);
140132718Skanstatic rtx rtx_for_function_call (tree, tree);
141132718Skanstatic void load_register_parameters (struct arg_data *, int, rtx *, int,
142132718Skan				      int, int *);
143132718Skanstatic rtx emit_library_call_value_1 (int, rtx, rtx, enum libcall_type,
144132718Skan				      enum machine_mode, int, va_list);
145132718Skanstatic int special_function_p (tree, int);
146132718Skanstatic int check_sibcall_argument_overlap_1 (rtx);
147132718Skanstatic int check_sibcall_argument_overlap (rtx, struct arg_data *, int);
14890075Sobrien
149132718Skanstatic int combine_pending_stack_adjustment_and_call (int, struct args_size *,
150169689Skan						      unsigned int);
151169689Skanstatic tree split_complex_values (tree);
152169689Skanstatic tree split_complex_types (tree);
15390075Sobrien
15490075Sobrien#ifdef REG_PARM_STACK_SPACE
155132718Skanstatic rtx save_fixed_argument_area (int, rtx, int *, int *);
156132718Skanstatic void restore_fixed_argument_area (rtx, rtx, int, int);
15752284Sobrien#endif
15818334Speter
15918334Speter/* Force FUNEXP into a form suitable for the address of a CALL,
16018334Speter   and return that as an rtx.  Also load the static chain register
16118334Speter   if FNDECL is a nested function.
16218334Speter
16318334Speter   CALL_FUSAGE points to a variable holding the prospective
16418334Speter   CALL_INSN_FUNCTION_USAGE information.  */
16518334Speter
16618334Speterrtx
167169689Skanprepare_call_address (rtx funexp, rtx static_chain_value,
168169689Skan		      rtx *call_fusage, int reg_parm_seen, int sibcallp)
16918334Speter{
170132718Skan  /* Make a valid memory address and copy constants through pseudo-regs,
17118334Speter     but not for a constant address if -fno-function-cse.  */
17218334Speter  if (GET_CODE (funexp) != SYMBOL_REF)
17318334Speter    /* If we are using registers for parameters, force the
17450397Sobrien       function address into a register now.  */
17550397Sobrien    funexp = ((SMALL_REGISTER_CLASSES && reg_parm_seen)
17650397Sobrien	      ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
17750397Sobrien	      : memory_address (FUNCTION_MODE, funexp));
17890075Sobrien  else if (! sibcallp)
17918334Speter    {
18018334Speter#ifndef NO_FUNCTION_CSE
18118334Speter      if (optimize && ! flag_no_function_cse)
182169689Skan	funexp = force_reg (Pmode, funexp);
18318334Speter#endif
18418334Speter    }
18518334Speter
18618334Speter  if (static_chain_value != 0)
18718334Speter    {
188169689Skan      static_chain_value = convert_memory_address (Pmode, static_chain_value);
18918334Speter      emit_move_insn (static_chain_rtx, static_chain_value);
19018334Speter
191169689Skan      if (REG_P (static_chain_rtx))
19218334Speter	use_reg (call_fusage, static_chain_rtx);
19318334Speter    }
19418334Speter
19518334Speter  return funexp;
19618334Speter}
19718334Speter
19818334Speter/* Generate instructions to call function FUNEXP,
19918334Speter   and optionally pop the results.
20018334Speter   The CALL_INSN is the first insn generated.
20118334Speter
20250397Sobrien   FNDECL is the declaration node of the function.  This is given to the
20318334Speter   macro RETURN_POPS_ARGS to determine whether this function pops its own args.
20418334Speter
20550397Sobrien   FUNTYPE is the data type of the function.  This is given to the macro
20650397Sobrien   RETURN_POPS_ARGS to determine whether this function pops its own args.
20750397Sobrien   We used to allow an identifier for library functions, but that doesn't
20850397Sobrien   work when the return type is an aggregate type and the calling convention
20950397Sobrien   says that the pointer to this aggregate is to be popped by the callee.
21018334Speter
21118334Speter   STACK_SIZE is the number of bytes of arguments on the stack,
21290075Sobrien   ROUNDED_STACK_SIZE is that number rounded up to
21390075Sobrien   PREFERRED_STACK_BOUNDARY; zero if the size is variable.  This is
21490075Sobrien   both to put into the call insn and to generate explicit popping
21590075Sobrien   code if necessary.
21618334Speter
21718334Speter   STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
21818334Speter   It is zero if this call doesn't want a structure value.
21918334Speter
22018334Speter   NEXT_ARG_REG is the rtx that results from executing
22118334Speter     FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1)
22218334Speter   just after all the args have had their registers assigned.
22318334Speter   This could be whatever you like, but normally it is the first
22418334Speter   arg-register beyond those used for args in this call,
22518334Speter   or 0 if all the arg-registers are used in this call.
22618334Speter   It is passed on to `gen_call' so you can put this info in the call insn.
22718334Speter
22818334Speter   VALREG is a hard register in which a value is returned,
22918334Speter   or 0 if the call does not return a value.
23018334Speter
23118334Speter   OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
23218334Speter   the args to this call were processed.
23318334Speter   We restore `inhibit_defer_pop' to that value.
23418334Speter
23518334Speter   CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
23690075Sobrien   denote registers used by the called function.  */
23718334Speter
23818334Speterstatic void
239169689Skanemit_call_1 (rtx funexp, tree fntree, tree fndecl ATTRIBUTE_UNUSED,
240169689Skan	     tree funtype ATTRIBUTE_UNUSED,
241132718Skan	     HOST_WIDE_INT stack_size ATTRIBUTE_UNUSED,
242132718Skan	     HOST_WIDE_INT rounded_stack_size,
243132718Skan	     HOST_WIDE_INT struct_value_size ATTRIBUTE_UNUSED,
244132718Skan	     rtx next_arg_reg ATTRIBUTE_UNUSED, rtx valreg,
245132718Skan	     int old_inhibit_defer_pop, rtx call_fusage, int ecf_flags,
246132718Skan	     CUMULATIVE_ARGS *args_so_far ATTRIBUTE_UNUSED)
24718334Speter{
24852284Sobrien  rtx rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
24918334Speter  rtx call_insn;
25018334Speter  int already_popped = 0;
25152284Sobrien  HOST_WIDE_INT n_popped = RETURN_POPS_ARGS (fndecl, funtype, stack_size);
25290075Sobrien#if defined (HAVE_call) && defined (HAVE_call_value)
25390075Sobrien  rtx struct_value_size_rtx;
25490075Sobrien  struct_value_size_rtx = GEN_INT (struct_value_size);
25550397Sobrien#endif
25618334Speter
25796263Sobrien#ifdef CALL_POPS_ARGS
25896263Sobrien  n_popped += CALL_POPS_ARGS (* args_so_far);
25996263Sobrien#endif
260132718Skan
26118334Speter  /* Ensure address is valid.  SYMBOL_REF is already valid, so no need,
26218334Speter     and we don't want to load it into a register as an optimization,
26318334Speter     because prepare_call_address already did it if it should be done.  */
26418334Speter  if (GET_CODE (funexp) != SYMBOL_REF)
26518334Speter    funexp = memory_address (FUNCTION_MODE, funexp);
26618334Speter
26790075Sobrien#if defined (HAVE_sibcall_pop) && defined (HAVE_sibcall_value_pop)
26890075Sobrien  if ((ecf_flags & ECF_SIBCALL)
26990075Sobrien      && HAVE_sibcall_pop && HAVE_sibcall_value_pop
27090075Sobrien      && (n_popped > 0 || stack_size == 0))
27190075Sobrien    {
27290075Sobrien      rtx n_pop = GEN_INT (n_popped);
27390075Sobrien      rtx pat;
27490075Sobrien
27590075Sobrien      /* If this subroutine pops its own args, record that in the call insn
27690075Sobrien	 if possible, for the sake of frame pointer elimination.  */
27790075Sobrien
27890075Sobrien      if (valreg)
27990075Sobrien	pat = GEN_SIBCALL_VALUE_POP (valreg,
28090075Sobrien				     gen_rtx_MEM (FUNCTION_MODE, funexp),
28190075Sobrien				     rounded_stack_size_rtx, next_arg_reg,
28290075Sobrien				     n_pop);
28390075Sobrien      else
28490075Sobrien	pat = GEN_SIBCALL_POP (gen_rtx_MEM (FUNCTION_MODE, funexp),
28590075Sobrien			       rounded_stack_size_rtx, next_arg_reg, n_pop);
28690075Sobrien
28790075Sobrien      emit_call_insn (pat);
28890075Sobrien      already_popped = 1;
28990075Sobrien    }
29090075Sobrien  else
29190075Sobrien#endif
29290075Sobrien
29318334Speter#if defined (HAVE_call_pop) && defined (HAVE_call_value_pop)
29490075Sobrien  /* If the target has "call" or "call_value" insns, then prefer them
29590075Sobrien     if no arguments are actually popped.  If the target does not have
29690075Sobrien     "call" or "call_value" insns, then we must use the popping versions
29790075Sobrien     even if the call has no arguments to pop.  */
29870635Sobrien#if defined (HAVE_call) && defined (HAVE_call_value)
29990075Sobrien  if (HAVE_call && HAVE_call_value && HAVE_call_pop && HAVE_call_value_pop
30090075Sobrien      && n_popped > 0 && ! (ecf_flags & ECF_SP_DEPRESSED))
30190075Sobrien#else
30290075Sobrien  if (HAVE_call_pop && HAVE_call_value_pop)
30370635Sobrien#endif
30418334Speter    {
30552284Sobrien      rtx n_pop = GEN_INT (n_popped);
30618334Speter      rtx pat;
30718334Speter
30818334Speter      /* If this subroutine pops its own args, record that in the call insn
30918334Speter	 if possible, for the sake of frame pointer elimination.  */
31018334Speter
31118334Speter      if (valreg)
31290075Sobrien	pat = GEN_CALL_VALUE_POP (valreg,
31350397Sobrien				  gen_rtx_MEM (FUNCTION_MODE, funexp),
31452284Sobrien				  rounded_stack_size_rtx, next_arg_reg, n_pop);
31518334Speter      else
31690075Sobrien	pat = GEN_CALL_POP (gen_rtx_MEM (FUNCTION_MODE, funexp),
31752284Sobrien			    rounded_stack_size_rtx, next_arg_reg, n_pop);
31818334Speter
31918334Speter      emit_call_insn (pat);
32018334Speter      already_popped = 1;
32118334Speter    }
32218334Speter  else
32318334Speter#endif
32490075Sobrien
32590075Sobrien#if defined (HAVE_sibcall) && defined (HAVE_sibcall_value)
32690075Sobrien  if ((ecf_flags & ECF_SIBCALL)
32790075Sobrien      && HAVE_sibcall && HAVE_sibcall_value)
32890075Sobrien    {
32990075Sobrien      if (valreg)
33090075Sobrien	emit_call_insn (GEN_SIBCALL_VALUE (valreg,
33190075Sobrien					   gen_rtx_MEM (FUNCTION_MODE, funexp),
33290075Sobrien					   rounded_stack_size_rtx,
33390075Sobrien					   next_arg_reg, NULL_RTX));
33490075Sobrien      else
33590075Sobrien	emit_call_insn (GEN_SIBCALL (gen_rtx_MEM (FUNCTION_MODE, funexp),
33690075Sobrien				     rounded_stack_size_rtx, next_arg_reg,
33790075Sobrien				     struct_value_size_rtx));
33890075Sobrien    }
33990075Sobrien  else
34018334Speter#endif
34118334Speter
34218334Speter#if defined (HAVE_call) && defined (HAVE_call_value)
34318334Speter  if (HAVE_call && HAVE_call_value)
34418334Speter    {
34518334Speter      if (valreg)
34690075Sobrien	emit_call_insn (GEN_CALL_VALUE (valreg,
34750397Sobrien					gen_rtx_MEM (FUNCTION_MODE, funexp),
34852284Sobrien					rounded_stack_size_rtx, next_arg_reg,
34918334Speter					NULL_RTX));
35018334Speter      else
35190075Sobrien	emit_call_insn (GEN_CALL (gen_rtx_MEM (FUNCTION_MODE, funexp),
35252284Sobrien				  rounded_stack_size_rtx, next_arg_reg,
35318334Speter				  struct_value_size_rtx));
35418334Speter    }
35518334Speter  else
35618334Speter#endif
357169689Skan    gcc_unreachable ();
35818334Speter
359132718Skan  /* Find the call we just emitted.  */
360132718Skan  call_insn = last_call_insn ();
36118334Speter
36290075Sobrien  /* Mark memory as used for "pure" function call.  */
36390075Sobrien  if (ecf_flags & ECF_PURE)
36490075Sobrien    call_fusage
36590075Sobrien      = gen_rtx_EXPR_LIST
36690075Sobrien	(VOIDmode,
36790075Sobrien	 gen_rtx_USE (VOIDmode,
36890075Sobrien		      gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode))),
36990075Sobrien	 call_fusage);
37090075Sobrien
371132718Skan  /* Put the register usage information there.  */
372132718Skan  add_function_usage_to (call_insn, call_fusage);
37318334Speter
37418334Speter  /* If this is a const call, then set the insn's unchanging bit.  */
37590075Sobrien  if (ecf_flags & (ECF_CONST | ECF_PURE))
37690075Sobrien    CONST_OR_PURE_CALL_P (call_insn) = 1;
37718334Speter
37890075Sobrien  /* If this call can't throw, attach a REG_EH_REGION reg note to that
37990075Sobrien     effect.  */
38090075Sobrien  if (ecf_flags & ECF_NOTHROW)
38190075Sobrien    REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, const0_rtx,
38290075Sobrien					       REG_NOTES (call_insn));
383117395Skan  else
384169689Skan    {
385169689Skan      int rn = lookup_stmt_eh_region (fntree);
38690075Sobrien
387169689Skan      /* If rn < 0, then either (1) tree-ssa not used or (2) doesn't
388169689Skan	 throw, which we already took care of.  */
389169689Skan      if (rn > 0)
390169689Skan	REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, GEN_INT (rn),
391169689Skan						   REG_NOTES (call_insn));
392169689Skan      note_current_region_may_contain_throw ();
393169689Skan    }
394169689Skan
39590075Sobrien  if (ecf_flags & ECF_NORETURN)
39690075Sobrien    REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_NORETURN, const0_rtx,
39790075Sobrien					       REG_NOTES (call_insn));
39890075Sobrien
39990075Sobrien  if (ecf_flags & ECF_RETURNS_TWICE)
40090075Sobrien    {
40190075Sobrien      REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_SETJMP, const0_rtx,
402169689Skan						 REG_NOTES (call_insn));
40390075Sobrien      current_function_calls_setjmp = 1;
40490075Sobrien    }
40590075Sobrien
40690075Sobrien  SIBLING_CALL_P (call_insn) = ((ecf_flags & ECF_SIBCALL) != 0);
40790075Sobrien
40818334Speter  /* Restore this now, so that we do defer pops for this call's args
40918334Speter     if the context of the call as a whole permits.  */
41018334Speter  inhibit_defer_pop = old_inhibit_defer_pop;
41118334Speter
41252284Sobrien  if (n_popped > 0)
41318334Speter    {
41418334Speter      if (!already_popped)
41550397Sobrien	CALL_INSN_FUNCTION_USAGE (call_insn)
41650397Sobrien	  = gen_rtx_EXPR_LIST (VOIDmode,
41750397Sobrien			       gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx),
41850397Sobrien			       CALL_INSN_FUNCTION_USAGE (call_insn));
41952284Sobrien      rounded_stack_size -= n_popped;
42052284Sobrien      rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
42190075Sobrien      stack_pointer_delta -= n_popped;
42218334Speter    }
42318334Speter
42490075Sobrien  if (!ACCUMULATE_OUTGOING_ARGS)
42518334Speter    {
42690075Sobrien      /* If returning from the subroutine does not automatically pop the args,
42790075Sobrien	 we need an instruction to pop them sooner or later.
42890075Sobrien	 Perhaps do it now; perhaps just record how much space to pop later.
42990075Sobrien
43090075Sobrien	 If returning from the subroutine does pop the args, indicate that the
43190075Sobrien	 stack pointer will be changed.  */
43290075Sobrien
43390075Sobrien      if (rounded_stack_size != 0)
43490075Sobrien	{
435169689Skan	  if (ecf_flags & (ECF_SP_DEPRESSED | ECF_NORETURN))
43690075Sobrien	    /* Just pretend we did the pop.  */
43790075Sobrien	    stack_pointer_delta -= rounded_stack_size;
43890075Sobrien	  else if (flag_defer_pop && inhibit_defer_pop == 0
43990075Sobrien	      && ! (ecf_flags & (ECF_CONST | ECF_PURE)))
44090075Sobrien	    pending_stack_adjust += rounded_stack_size;
44190075Sobrien	  else
44290075Sobrien	    adjust_stack (rounded_stack_size_rtx);
44390075Sobrien	}
44418334Speter    }
44590075Sobrien  /* When we accumulate outgoing args, we must avoid any stack manipulations.
44690075Sobrien     Restore the stack pointer to its original value now.  Usually
44790075Sobrien     ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
44890075Sobrien     On  i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
44990075Sobrien     popping variants of functions exist as well.
45090075Sobrien
45190075Sobrien     ??? We may optimize similar to defer_pop above, but it is
45290075Sobrien     probably not worthwhile.
45390075Sobrien
45490075Sobrien     ??? It will be worthwhile to enable combine_stack_adjustments even for
45590075Sobrien     such machines.  */
45690075Sobrien  else if (n_popped)
45790075Sobrien    anti_adjust_stack (GEN_INT (n_popped));
45818334Speter}
45918334Speter
46052284Sobrien/* Determine if the function identified by NAME and FNDECL is one with
46152284Sobrien   special properties we wish to know about.
46252284Sobrien
46352284Sobrien   For example, if the function might return more than one time (setjmp), then
46452284Sobrien   set RETURNS_TWICE to a nonzero value.
46552284Sobrien
466169689Skan   Similarly set NORETURN if the function is in the longjmp family.
46752284Sobrien
46852284Sobrien   Set MAY_BE_ALLOCA for any memory allocation function that might allocate
46952284Sobrien   space from the stack such as alloca.  */
47052284Sobrien
47190075Sobrienstatic int
472132718Skanspecial_function_p (tree fndecl, int flags)
47352284Sobrien{
474169689Skan  if (fndecl && DECL_NAME (fndecl)
47590075Sobrien      && IDENTIFIER_LENGTH (DECL_NAME (fndecl)) <= 17
47652284Sobrien      /* Exclude functions not at the file scope, or not `extern',
47752284Sobrien	 since they are not the magic functions we would otherwise
478132718Skan	 think they are.
479169689Skan	 FIXME: this should be handled with attributes, not with this
480169689Skan	 hacky imitation of DECL_ASSEMBLER_NAME.  It's (also) wrong
481169689Skan	 because you can declare fork() inside a function if you
482169689Skan	 wish.  */
483169689Skan      && (DECL_CONTEXT (fndecl) == NULL_TREE
484132718Skan	  || TREE_CODE (DECL_CONTEXT (fndecl)) == TRANSLATION_UNIT_DECL)
485132718Skan      && TREE_PUBLIC (fndecl))
48652284Sobrien    {
48790075Sobrien      const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
48890075Sobrien      const char *tname = name;
48952284Sobrien
49052284Sobrien      /* We assume that alloca will always be called by name.  It
49152284Sobrien	 makes no sense to pass it as a pointer-to-function to
49252284Sobrien	 anything that does not understand its behavior.  */
49390075Sobrien      if (((IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 6
49490075Sobrien	    && name[0] == 'a'
49590075Sobrien	    && ! strcmp (name, "alloca"))
49690075Sobrien	   || (IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 16
49790075Sobrien	       && name[0] == '_'
49890075Sobrien	       && ! strcmp (name, "__builtin_alloca"))))
49990075Sobrien	flags |= ECF_MAY_BE_ALLOCA;
50052284Sobrien
50152284Sobrien      /* Disregard prefix _, __ or __x.  */
50252284Sobrien      if (name[0] == '_')
50352284Sobrien	{
50452284Sobrien	  if (name[1] == '_' && name[2] == 'x')
50552284Sobrien	    tname += 3;
50652284Sobrien	  else if (name[1] == '_')
50752284Sobrien	    tname += 2;
50852284Sobrien	  else
50952284Sobrien	    tname += 1;
51052284Sobrien	}
51152284Sobrien
51252284Sobrien      if (tname[0] == 's')
51352284Sobrien	{
51490075Sobrien	  if ((tname[1] == 'e'
51590075Sobrien	       && (! strcmp (tname, "setjmp")
51690075Sobrien		   || ! strcmp (tname, "setjmp_syscall")))
51790075Sobrien	      || (tname[1] == 'i'
51890075Sobrien		  && ! strcmp (tname, "sigsetjmp"))
51990075Sobrien	      || (tname[1] == 'a'
52090075Sobrien		  && ! strcmp (tname, "savectx")))
52190075Sobrien	    flags |= ECF_RETURNS_TWICE;
52290075Sobrien
52352284Sobrien	  if (tname[1] == 'i'
52452284Sobrien	      && ! strcmp (tname, "siglongjmp"))
525169689Skan	    flags |= ECF_NORETURN;
52652284Sobrien	}
52752284Sobrien      else if ((tname[0] == 'q' && tname[1] == 's'
52852284Sobrien		&& ! strcmp (tname, "qsetjmp"))
52952284Sobrien	       || (tname[0] == 'v' && tname[1] == 'f'
530169689Skan		   && ! strcmp (tname, "vfork"))
531169689Skan	       || (tname[0] == 'g' && tname[1] == 'e'
532169689Skan		   && !strcmp (tname, "getcontext")))
53390075Sobrien	flags |= ECF_RETURNS_TWICE;
53452284Sobrien
53552284Sobrien      else if (tname[0] == 'l' && tname[1] == 'o'
53652284Sobrien	       && ! strcmp (tname, "longjmp"))
537169689Skan	flags |= ECF_NORETURN;
538169689Skan    }
53990075Sobrien
54090075Sobrien  return flags;
54152284Sobrien}
54252284Sobrien
543169689Skan/* Return nonzero when FNDECL represents a call to setjmp.  */
54490075Sobrien
54590075Sobrienint
546132718Skansetjmp_call_p (tree fndecl)
54790075Sobrien{
54890075Sobrien  return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
54990075Sobrien}
55090075Sobrien
551117395Skan/* Return true when exp contains alloca call.  */
552117395Skanbool
553132718Skanalloca_call_p (tree exp)
554117395Skan{
555117395Skan  if (TREE_CODE (exp) == CALL_EXPR
556117395Skan      && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
557117395Skan      && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
558117395Skan	  == FUNCTION_DECL)
559117395Skan      && (special_function_p (TREE_OPERAND (TREE_OPERAND (exp, 0), 0),
560117395Skan			      0) & ECF_MAY_BE_ALLOCA))
561117395Skan    return true;
562117395Skan  return false;
563117395Skan}
564117395Skan
56590075Sobrien/* Detect flags (function attributes) from the function decl or type node.  */
56690075Sobrien
567132718Skanint
568132718Skanflags_from_decl_or_type (tree exp)
56990075Sobrien{
57090075Sobrien  int flags = 0;
57190075Sobrien  tree type = exp;
572132718Skan
57390075Sobrien  if (DECL_P (exp))
57490075Sobrien    {
57590075Sobrien      type = TREE_TYPE (exp);
57690075Sobrien
57790075Sobrien      /* The function exp may have the `malloc' attribute.  */
578132718Skan      if (DECL_IS_MALLOC (exp))
57990075Sobrien	flags |= ECF_MALLOC;
58090075Sobrien
581169689Skan      /* The function exp may have the `returns_twice' attribute.  */
582169689Skan      if (DECL_IS_RETURNS_TWICE (exp))
583169689Skan	flags |= ECF_RETURNS_TWICE;
584169689Skan
58590075Sobrien      /* The function exp may have the `pure' attribute.  */
586132718Skan      if (DECL_IS_PURE (exp))
587169689Skan	flags |= ECF_PURE;
58890075Sobrien
589169689Skan      if (DECL_IS_NOVOPS (exp))
590169689Skan	flags |= ECF_NOVOPS;
591169689Skan
59290075Sobrien      if (TREE_NOTHROW (exp))
59390075Sobrien	flags |= ECF_NOTHROW;
594119256Skan
595119256Skan      if (TREE_READONLY (exp) && ! TREE_THIS_VOLATILE (exp))
596169689Skan	flags |= ECF_CONST;
597169689Skan
598169689Skan      flags = special_function_p (exp, flags);
59990075Sobrien    }
600169689Skan  else if (TYPE_P (exp) && TYPE_READONLY (exp) && ! TREE_THIS_VOLATILE (exp))
601119256Skan    flags |= ECF_CONST;
60290075Sobrien
60390075Sobrien  if (TREE_THIS_VOLATILE (exp))
60490075Sobrien    flags |= ECF_NORETURN;
60590075Sobrien
60690075Sobrien  /* Mark if the function returns with the stack pointer depressed.   We
60790075Sobrien     cannot consider it pure or constant in that case.  */
60890075Sobrien  if (TREE_CODE (type) == FUNCTION_TYPE && TYPE_RETURNS_STACK_DEPRESSED (type))
60990075Sobrien    {
61090075Sobrien      flags |= ECF_SP_DEPRESSED;
611169689Skan      flags &= ~(ECF_PURE | ECF_CONST);
61290075Sobrien    }
61390075Sobrien
61490075Sobrien  return flags;
61590075Sobrien}
61690075Sobrien
617132718Skan/* Detect flags from a CALL_EXPR.  */
618132718Skan
619132718Skanint
620132718Skancall_expr_flags (tree t)
621132718Skan{
622132718Skan  int flags;
623132718Skan  tree decl = get_callee_fndecl (t);
624132718Skan
625132718Skan  if (decl)
626132718Skan    flags = flags_from_decl_or_type (decl);
627132718Skan  else
628132718Skan    {
629132718Skan      t = TREE_TYPE (TREE_OPERAND (t, 0));
630132718Skan      if (t && TREE_CODE (t) == POINTER_TYPE)
631132718Skan	flags = flags_from_decl_or_type (TREE_TYPE (t));
632132718Skan      else
633132718Skan	flags = 0;
634132718Skan    }
635132718Skan
636132718Skan  return flags;
637132718Skan}
638132718Skan
63952284Sobrien/* Precompute all register parameters as described by ARGS, storing values
64052284Sobrien   into fields within the ARGS array.
64152284Sobrien
64252284Sobrien   NUM_ACTUALS indicates the total number elements in the ARGS array.
64352284Sobrien
64452284Sobrien   Set REG_PARM_SEEN if we encounter a register parameter.  */
64552284Sobrien
64652284Sobrienstatic void
647169689Skanprecompute_register_parameters (int num_actuals, struct arg_data *args,
648169689Skan				int *reg_parm_seen)
64952284Sobrien{
65052284Sobrien  int i;
65152284Sobrien
65252284Sobrien  *reg_parm_seen = 0;
65352284Sobrien
65452284Sobrien  for (i = 0; i < num_actuals; i++)
65552284Sobrien    if (args[i].reg != 0 && ! args[i].pass_on_stack)
65652284Sobrien      {
65752284Sobrien	*reg_parm_seen = 1;
65852284Sobrien
65952284Sobrien	if (args[i].value == 0)
66052284Sobrien	  {
66152284Sobrien	    push_temp_slots ();
662169689Skan	    args[i].value = expand_normal (args[i].tree_value);
66352284Sobrien	    preserve_temp_slots (args[i].value);
66452284Sobrien	    pop_temp_slots ();
66552284Sobrien	  }
66652284Sobrien
667117395Skan	/* If the value is a non-legitimate constant, force it into a
668117395Skan	   pseudo now.  TLS symbols sometimes need a call to resolve.  */
669117395Skan	if (CONSTANT_P (args[i].value)
670117395Skan	    && !LEGITIMATE_CONSTANT_P (args[i].value))
671117395Skan	  args[i].value = force_reg (args[i].mode, args[i].value);
672117395Skan
67352284Sobrien	/* If we are to promote the function arg to a wider mode,
67452284Sobrien	   do it now.  */
67552284Sobrien
67652284Sobrien	if (args[i].mode != TYPE_MODE (TREE_TYPE (args[i].tree_value)))
67752284Sobrien	  args[i].value
67852284Sobrien	    = convert_modes (args[i].mode,
67952284Sobrien			     TYPE_MODE (TREE_TYPE (args[i].tree_value)),
68052284Sobrien			     args[i].value, args[i].unsignedp);
68152284Sobrien
682169689Skan	/* If we're going to have to load the value by parts, pull the
683169689Skan	   parts into pseudos.  The part extraction process can involve
684169689Skan	   non-trivial computation.  */
685169689Skan	if (GET_CODE (args[i].reg) == PARALLEL)
686169689Skan	  {
687169689Skan	    tree type = TREE_TYPE (args[i].tree_value);
688169689Skan	    args[i].parallel_value
689169689Skan	      = emit_group_load_into_temps (args[i].reg, args[i].value,
690169689Skan					    type, int_size_in_bytes (type));
691169689Skan	  }
692169689Skan
69390075Sobrien	/* If the value is expensive, and we are inside an appropriately
69452284Sobrien	   short loop, put the value into a pseudo and then put the pseudo
69552284Sobrien	   into the hard reg.
69652284Sobrien
69752284Sobrien	   For small register classes, also do this if this call uses
69852284Sobrien	   register parameters.  This is to avoid reload conflicts while
69952284Sobrien	   loading the parameters registers.  */
70052284Sobrien
701169689Skan	else if ((! (REG_P (args[i].value)
702169689Skan		     || (GET_CODE (args[i].value) == SUBREG
703169689Skan			 && REG_P (SUBREG_REG (args[i].value)))))
704169689Skan		 && args[i].mode != BLKmode
705169689Skan		 && rtx_cost (args[i].value, SET) > COSTS_N_INSNS (1)
706169689Skan		 && ((SMALL_REGISTER_CLASSES && *reg_parm_seen)
707169689Skan		     || optimize))
70852284Sobrien	  args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
70952284Sobrien      }
71052284Sobrien}
71152284Sobrien
71290075Sobrien#ifdef REG_PARM_STACK_SPACE
71352284Sobrien
71452284Sobrien  /* The argument list is the property of the called routine and it
71552284Sobrien     may clobber it.  If the fixed area has been used for previous
71652284Sobrien     parameters, we must save and restore it.  */
71790075Sobrien
71852284Sobrienstatic rtx
719132718Skansave_fixed_argument_area (int reg_parm_stack_space, rtx argblock, int *low_to_save, int *high_to_save)
72052284Sobrien{
721132718Skan  int low;
722132718Skan  int high;
72352284Sobrien
724132718Skan  /* Compute the boundary of the area that needs to be saved, if any.  */
725132718Skan  high = reg_parm_stack_space;
72652284Sobrien#ifdef ARGS_GROW_DOWNWARD
727132718Skan  high += 1;
72852284Sobrien#endif
729132718Skan  if (high > highest_outgoing_arg_in_use)
730132718Skan    high = highest_outgoing_arg_in_use;
73152284Sobrien
732132718Skan  for (low = 0; low < high; low++)
733132718Skan    if (stack_usage_map[low] != 0)
734132718Skan      {
735132718Skan	int num_to_save;
736132718Skan	enum machine_mode save_mode;
737132718Skan	int delta;
738132718Skan	rtx stack_area;
739132718Skan	rtx save_area;
74052284Sobrien
741132718Skan	while (stack_usage_map[--high] == 0)
742132718Skan	  ;
74352284Sobrien
744132718Skan	*low_to_save = low;
745132718Skan	*high_to_save = high;
74652284Sobrien
747132718Skan	num_to_save = high - low + 1;
748132718Skan	save_mode = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
74952284Sobrien
750132718Skan	/* If we don't have the required alignment, must do this
751132718Skan	   in BLKmode.  */
752132718Skan	if ((low & (MIN (GET_MODE_SIZE (save_mode),
753132718Skan			 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
754132718Skan	  save_mode = BLKmode;
755132718Skan
75652284Sobrien#ifdef ARGS_GROW_DOWNWARD
757132718Skan	delta = -high;
75852284Sobrien#else
759132718Skan	delta = low;
76052284Sobrien#endif
761132718Skan	stack_area = gen_rtx_MEM (save_mode,
762132718Skan				  memory_address (save_mode,
763132718Skan						  plus_constant (argblock,
764132718Skan								 delta)));
76590075Sobrien
766132718Skan	set_mem_align (stack_area, PARM_BOUNDARY);
767132718Skan	if (save_mode == BLKmode)
768132718Skan	  {
769132718Skan	    save_area = assign_stack_temp (BLKmode, num_to_save, 0);
770132718Skan	    emit_block_move (validize_mem (save_area), stack_area,
771132718Skan			     GEN_INT (num_to_save), BLOCK_OP_CALL_PARM);
772132718Skan	  }
773132718Skan	else
774132718Skan	  {
775132718Skan	    save_area = gen_reg_rtx (save_mode);
776132718Skan	    emit_move_insn (save_area, stack_area);
777132718Skan	  }
77890075Sobrien
779132718Skan	return save_area;
780132718Skan      }
781132718Skan
782132718Skan  return NULL_RTX;
78352284Sobrien}
78452284Sobrien
78552284Sobrienstatic void
786132718Skanrestore_fixed_argument_area (rtx save_area, rtx argblock, int high_to_save, int low_to_save)
78752284Sobrien{
78852284Sobrien  enum machine_mode save_mode = GET_MODE (save_area);
789132718Skan  int delta;
790132718Skan  rtx stack_area;
791132718Skan
79252284Sobrien#ifdef ARGS_GROW_DOWNWARD
793132718Skan  delta = -high_to_save;
79452284Sobrien#else
795132718Skan  delta = low_to_save;
79652284Sobrien#endif
797132718Skan  stack_area = gen_rtx_MEM (save_mode,
798132718Skan			    memory_address (save_mode,
799132718Skan					    plus_constant (argblock, delta)));
800132718Skan  set_mem_align (stack_area, PARM_BOUNDARY);
80152284Sobrien
80252284Sobrien  if (save_mode != BLKmode)
80352284Sobrien    emit_move_insn (stack_area, save_area);
80452284Sobrien  else
805117395Skan    emit_block_move (stack_area, validize_mem (save_area),
806117395Skan		     GEN_INT (high_to_save - low_to_save + 1),
807117395Skan		     BLOCK_OP_CALL_PARM);
80852284Sobrien}
80990075Sobrien#endif /* REG_PARM_STACK_SPACE */
81090075Sobrien
81152284Sobrien/* If any elements in ARGS refer to parameters that are to be passed in
81252284Sobrien   registers, but not in memory, and whose alignment does not permit a
81352284Sobrien   direct copy into registers.  Copy the values into a group of pseudos
81490075Sobrien   which we will later copy into the appropriate hard registers.
81552284Sobrien
81652284Sobrien   Pseudos for each unaligned argument will be stored into the array
81752284Sobrien   args[argnum].aligned_regs.  The caller is responsible for deallocating
81852284Sobrien   the aligned_regs array if it is nonzero.  */
81952284Sobrien
82052284Sobrienstatic void
821132718Skanstore_unaligned_arguments_into_pseudos (struct arg_data *args, int num_actuals)
82252284Sobrien{
82352284Sobrien  int i, j;
82490075Sobrien
82552284Sobrien  for (i = 0; i < num_actuals; i++)
82652284Sobrien    if (args[i].reg != 0 && ! args[i].pass_on_stack
82752284Sobrien	&& args[i].mode == BLKmode
82852284Sobrien	&& (TYPE_ALIGN (TREE_TYPE (args[i].tree_value))
82952284Sobrien	    < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
83052284Sobrien      {
83152284Sobrien	int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
832132718Skan	int endian_correction = 0;
83352284Sobrien
834169689Skan	if (args[i].partial)
835169689Skan	  {
836169689Skan	    gcc_assert (args[i].partial % UNITS_PER_WORD == 0);
837169689Skan	    args[i].n_aligned_regs = args[i].partial / UNITS_PER_WORD;
838169689Skan	  }
839169689Skan	else
840169689Skan	  {
841169689Skan	    args[i].n_aligned_regs
842169689Skan	      = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
843169689Skan	  }
84452284Sobrien
845169689Skan	args[i].aligned_regs = XNEWVEC (rtx, args[i].n_aligned_regs);
846169689Skan
847132718Skan	/* Structures smaller than a word are normally aligned to the
848132718Skan	   least significant byte.  On a BYTES_BIG_ENDIAN machine,
84952284Sobrien	   this means we must skip the empty high order bytes when
85052284Sobrien	   calculating the bit offset.  */
851132718Skan	if (bytes < UNITS_PER_WORD
852132718Skan#ifdef BLOCK_REG_PADDING
853132718Skan	    && (BLOCK_REG_PADDING (args[i].mode,
854132718Skan				   TREE_TYPE (args[i].tree_value), 1)
855132718Skan		== downward)
856132718Skan#else
857132718Skan	    && BYTES_BIG_ENDIAN
858132718Skan#endif
859132718Skan	    )
860132718Skan	  endian_correction = BITS_PER_WORD - bytes * BITS_PER_UNIT;
86152284Sobrien
86252284Sobrien	for (j = 0; j < args[i].n_aligned_regs; j++)
86352284Sobrien	  {
86452284Sobrien	    rtx reg = gen_reg_rtx (word_mode);
86552284Sobrien	    rtx word = operand_subword_force (args[i].value, j, BLKmode);
86652284Sobrien	    int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
86752284Sobrien
86852284Sobrien	    args[i].aligned_regs[j] = reg;
869132718Skan	    word = extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
870169689Skan				      word_mode, word_mode);
87152284Sobrien
87252284Sobrien	    /* There is no need to restrict this code to loading items
87352284Sobrien	       in TYPE_ALIGN sized hunks.  The bitfield instructions can
87452284Sobrien	       load up entire word sized registers efficiently.
87552284Sobrien
87652284Sobrien	       ??? This may not be needed anymore.
87752284Sobrien	       We use to emit a clobber here but that doesn't let later
87852284Sobrien	       passes optimize the instructions we emit.  By storing 0 into
87952284Sobrien	       the register later passes know the first AND to zero out the
88052284Sobrien	       bitfield being set in the register is unnecessary.  The store
88152284Sobrien	       of 0 will be deleted as will at least the first AND.  */
88252284Sobrien
88352284Sobrien	    emit_move_insn (reg, const0_rtx);
88452284Sobrien
88552284Sobrien	    bytes -= bitsize / BITS_PER_UNIT;
886132718Skan	    store_bit_field (reg, bitsize, endian_correction, word_mode,
887169689Skan			     word);
88852284Sobrien	  }
88952284Sobrien      }
89052284Sobrien}
89152284Sobrien
89252284Sobrien/* Fill in ARGS_SIZE and ARGS array based on the parameters found in
89390075Sobrien   ACTPARMS.
89452284Sobrien
89552284Sobrien   NUM_ACTUALS is the total number of parameters.
89652284Sobrien
89752284Sobrien   N_NAMED_ARGS is the total number of named arguments.
89852284Sobrien
89952284Sobrien   FNDECL is the tree code for the target of this call (if known)
90052284Sobrien
90152284Sobrien   ARGS_SO_FAR holds state needed by the target to know where to place
90252284Sobrien   the next argument.
90352284Sobrien
90452284Sobrien   REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
90552284Sobrien   for arguments which are passed in registers.
90652284Sobrien
90752284Sobrien   OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
90852284Sobrien   and may be modified by this routine.
90952284Sobrien
91090075Sobrien   OLD_PENDING_ADJ, MUST_PREALLOCATE and FLAGS are pointers to integer
911169689Skan   flags which may may be modified by this routine.
91252284Sobrien
913169689Skan   MAY_TAILCALL is cleared if we encounter an invisible pass-by-reference
914169689Skan   that requires allocation of stack space.
915169689Skan
916132718Skan   CALL_FROM_THUNK_P is true if this call is the jump from a thunk to
917132718Skan   the thunked-to function.  */
918132718Skan
91952284Sobrienstatic void
920132718Skaninitialize_argument_information (int num_actuals ATTRIBUTE_UNUSED,
921132718Skan				 struct arg_data *args,
922132718Skan				 struct args_size *args_size,
923132718Skan				 int n_named_args ATTRIBUTE_UNUSED,
924132718Skan				 tree actparms, tree fndecl,
925132718Skan				 CUMULATIVE_ARGS *args_so_far,
926132718Skan				 int reg_parm_stack_space,
927132718Skan				 rtx *old_stack_level, int *old_pending_adj,
928132718Skan				 int *must_preallocate, int *ecf_flags,
929169689Skan				 bool *may_tailcall, bool call_from_thunk_p)
93052284Sobrien{
93152284Sobrien  /* 1 if scanning parms front to back, -1 if scanning back to front.  */
93252284Sobrien  int inc;
93352284Sobrien
93452284Sobrien  /* Count arg position in order args appear.  */
93552284Sobrien  int argpos;
93652284Sobrien
93752284Sobrien  int i;
93852284Sobrien  tree p;
93990075Sobrien
94052284Sobrien  args_size->constant = 0;
94152284Sobrien  args_size->var = 0;
94252284Sobrien
94352284Sobrien  /* In this loop, we consider args in the order they are written.
94452284Sobrien     We fill up ARGS from the front or from the back if necessary
94552284Sobrien     so that in any case the first arg to be pushed ends up at the front.  */
94652284Sobrien
94790075Sobrien  if (PUSH_ARGS_REVERSED)
94890075Sobrien    {
94990075Sobrien      i = num_actuals - 1, inc = -1;
95090075Sobrien      /* In this case, must reverse order of args
95190075Sobrien	 so that we compute and push the last arg first.  */
95290075Sobrien    }
95390075Sobrien  else
95490075Sobrien    {
95590075Sobrien      i = 0, inc = 1;
95690075Sobrien    }
95752284Sobrien
95852284Sobrien  /* I counts args in order (to be) pushed; ARGPOS counts in order written.  */
95952284Sobrien  for (p = actparms, argpos = 0; p; p = TREE_CHAIN (p), i += inc, argpos++)
96052284Sobrien    {
96152284Sobrien      tree type = TREE_TYPE (TREE_VALUE (p));
96252284Sobrien      int unsignedp;
96352284Sobrien      enum machine_mode mode;
96452284Sobrien
96552284Sobrien      args[i].tree_value = TREE_VALUE (p);
96652284Sobrien
96752284Sobrien      /* Replace erroneous argument with constant zero.  */
96890075Sobrien      if (type == error_mark_node || !COMPLETE_TYPE_P (type))
96952284Sobrien	args[i].tree_value = integer_zero_node, type = integer_type_node;
97052284Sobrien
97152284Sobrien      /* If TYPE is a transparent union, pass things the way we would
97252284Sobrien	 pass the first field of the union.  We have already verified that
97352284Sobrien	 the modes are the same.  */
97490075Sobrien      if (TREE_CODE (type) == UNION_TYPE && TYPE_TRANSPARENT_UNION (type))
97552284Sobrien	type = TREE_TYPE (TYPE_FIELDS (type));
97652284Sobrien
97752284Sobrien      /* Decide where to pass this arg.
97852284Sobrien
97952284Sobrien	 args[i].reg is nonzero if all or part is passed in registers.
98052284Sobrien
98152284Sobrien	 args[i].partial is nonzero if part but not all is passed in registers,
982169689Skan	 and the exact value says how many bytes are passed in registers.
98352284Sobrien
98452284Sobrien	 args[i].pass_on_stack is nonzero if the argument must at least be
98552284Sobrien	 computed on the stack.  It may then be loaded back into registers
98652284Sobrien	 if args[i].reg is nonzero.
98752284Sobrien
98852284Sobrien	 These decisions are driven by the FUNCTION_... macros and must agree
98952284Sobrien	 with those made by function.c.  */
99052284Sobrien
99152284Sobrien      /* See if this argument should be passed by invisible reference.  */
992169689Skan      if (pass_by_reference (args_so_far, TYPE_MODE (type),
993169689Skan			     type, argpos < n_named_args))
99452284Sobrien	{
995169689Skan	  bool callee_copies;
996169689Skan	  tree base;
997169689Skan
998169689Skan	  callee_copies
999169689Skan	    = reference_callee_copied (args_so_far, TYPE_MODE (type),
1000169689Skan				       type, argpos < n_named_args);
1001169689Skan
1002169689Skan	  /* If we're compiling a thunk, pass through invisible references
1003169689Skan	     instead of making a copy.  */
1004132718Skan	  if (call_from_thunk_p
1005169689Skan	      || (callee_copies
1006169689Skan		  && !TREE_ADDRESSABLE (type)
1007169689Skan		  && (base = get_base_address (args[i].tree_value))
1008169689Skan		  && (!DECL_P (base) || MEM_P (DECL_RTL (base)))))
100952284Sobrien	    {
1010169689Skan	      /* We can't use sibcalls if a callee-copied argument is
1011169689Skan		 stored in the current function's frame.  */
1012169689Skan	      if (!call_from_thunk_p && DECL_P (base) && !TREE_STATIC (base))
1013169689Skan		*may_tailcall = false;
101490075Sobrien
1015169689Skan	      args[i].tree_value = build_fold_addr_expr (args[i].tree_value);
1016169689Skan	      type = TREE_TYPE (args[i].tree_value);
101752284Sobrien
1018169689Skan	      *ecf_flags &= ~(ECF_CONST | ECF_LIBCALL_BLOCK);
101952284Sobrien	    }
102052284Sobrien	  else
102152284Sobrien	    {
102252284Sobrien	      /* We make a copy of the object and pass the address to the
102352284Sobrien		 function being called.  */
102452284Sobrien	      rtx copy;
102552284Sobrien
102690075Sobrien	      if (!COMPLETE_TYPE_P (type)
102752284Sobrien		  || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
102852284Sobrien		  || (flag_stack_check && ! STACK_CHECK_BUILTIN
102990075Sobrien		      && (0 < compare_tree_int (TYPE_SIZE_UNIT (type),
103090075Sobrien						STACK_CHECK_MAX_VAR_SIZE))))
103152284Sobrien		{
103252284Sobrien		  /* This is a variable-sized object.  Make space on the stack
103352284Sobrien		     for it.  */
103452284Sobrien		  rtx size_rtx = expr_size (TREE_VALUE (p));
103552284Sobrien
103652284Sobrien		  if (*old_stack_level == 0)
103752284Sobrien		    {
103852284Sobrien		      emit_stack_save (SAVE_BLOCK, old_stack_level, NULL_RTX);
103952284Sobrien		      *old_pending_adj = pending_stack_adjust;
104052284Sobrien		      pending_stack_adjust = 0;
104152284Sobrien		    }
104252284Sobrien
104352284Sobrien		  copy = gen_rtx_MEM (BLKmode,
104490075Sobrien				      allocate_dynamic_stack_space
104590075Sobrien				      (size_rtx, NULL_RTX, TYPE_ALIGN (type)));
104690075Sobrien		  set_mem_attributes (copy, type, 1);
104752284Sobrien		}
104852284Sobrien	      else
104990075Sobrien		copy = assign_temp (type, 0, 1, 0);
105052284Sobrien
105152284Sobrien	      store_expr (args[i].tree_value, copy, 0);
105252284Sobrien
1053169689Skan	      if (callee_copies)
1054169689Skan		*ecf_flags &= ~(ECF_CONST | ECF_LIBCALL_BLOCK);
1055169689Skan	      else
1056169689Skan		*ecf_flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
1057169689Skan
1058169689Skan	      args[i].tree_value
1059169689Skan		= build_fold_addr_expr (make_tree (type, copy));
1060169689Skan	      type = TREE_TYPE (args[i].tree_value);
1061169689Skan	      *may_tailcall = false;
106252284Sobrien	    }
106352284Sobrien	}
106452284Sobrien
106552284Sobrien      mode = TYPE_MODE (type);
1066169689Skan      unsignedp = TYPE_UNSIGNED (type);
106752284Sobrien
1068132718Skan      if (targetm.calls.promote_function_args (fndecl ? TREE_TYPE (fndecl) : 0))
1069132718Skan	mode = promote_mode (type, mode, &unsignedp, 1);
107052284Sobrien
107152284Sobrien      args[i].unsignedp = unsignedp;
107252284Sobrien      args[i].mode = mode;
107390075Sobrien
107452284Sobrien      args[i].reg = FUNCTION_ARG (*args_so_far, mode, type,
107552284Sobrien				  argpos < n_named_args);
107690075Sobrien#ifdef FUNCTION_INCOMING_ARG
107790075Sobrien      /* If this is a sibling call and the machine has register windows, the
107890075Sobrien	 register window has to be unwinded before calling the routine, so
107990075Sobrien	 arguments have to go into the incoming registers.  */
108090075Sobrien      args[i].tail_call_reg = FUNCTION_INCOMING_ARG (*args_so_far, mode, type,
108190075Sobrien						     argpos < n_named_args);
108290075Sobrien#else
108390075Sobrien      args[i].tail_call_reg = args[i].reg;
108490075Sobrien#endif
108590075Sobrien
108652284Sobrien      if (args[i].reg)
108752284Sobrien	args[i].partial
1088169689Skan	  = targetm.calls.arg_partial_bytes (args_so_far, mode, type,
1089169689Skan					     argpos < n_named_args);
109052284Sobrien
1091169689Skan      args[i].pass_on_stack = targetm.calls.must_pass_in_stack (mode, type);
109252284Sobrien
109352284Sobrien      /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
109452284Sobrien	 it means that we are to pass this arg in the register(s) designated
109552284Sobrien	 by the PARALLEL, but also to pass it in the stack.  */
109652284Sobrien      if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
109752284Sobrien	  && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
109852284Sobrien	args[i].pass_on_stack = 1;
109952284Sobrien
110052284Sobrien      /* If this is an addressable type, we must preallocate the stack
110152284Sobrien	 since we must evaluate the object into its final location.
110252284Sobrien
110352284Sobrien	 If this is to be passed in both registers and the stack, it is simpler
110452284Sobrien	 to preallocate.  */
110552284Sobrien      if (TREE_ADDRESSABLE (type)
110652284Sobrien	  || (args[i].pass_on_stack && args[i].reg != 0))
110752284Sobrien	*must_preallocate = 1;
110852284Sobrien
110952284Sobrien      /* If this is an addressable type, we cannot pre-evaluate it.  Thus,
111052284Sobrien	 we cannot consider this function call constant.  */
111152284Sobrien      if (TREE_ADDRESSABLE (type))
111290075Sobrien	*ecf_flags &= ~ECF_LIBCALL_BLOCK;
111352284Sobrien
111452284Sobrien      /* Compute the stack-size of this argument.  */
111552284Sobrien      if (args[i].reg == 0 || args[i].partial != 0
111652284Sobrien	  || reg_parm_stack_space > 0
111752284Sobrien	  || args[i].pass_on_stack)
111852284Sobrien	locate_and_pad_parm (mode, type,
111952284Sobrien#ifdef STACK_PARMS_IN_REG_PARM_AREA
112052284Sobrien			     1,
112152284Sobrien#else
112252284Sobrien			     args[i].reg != 0,
112352284Sobrien#endif
1124132718Skan			     args[i].pass_on_stack ? 0 : args[i].partial,
1125132718Skan			     fndecl, args_size, &args[i].locate);
1126132718Skan#ifdef BLOCK_REG_PADDING
1127132718Skan      else
1128132718Skan	/* The argument is passed entirely in registers.  See at which
1129132718Skan	   end it should be padded.  */
1130132718Skan	args[i].locate.where_pad =
1131132718Skan	  BLOCK_REG_PADDING (mode, type,
1132132718Skan			     int_size_in_bytes (type) <= UNITS_PER_WORD);
113352284Sobrien#endif
113452284Sobrien
113552284Sobrien      /* Update ARGS_SIZE, the total stack space for args so far.  */
113652284Sobrien
1137132718Skan      args_size->constant += args[i].locate.size.constant;
1138132718Skan      if (args[i].locate.size.var)
1139132718Skan	ADD_PARM_SIZE (*args_size, args[i].locate.size.var);
114052284Sobrien
114152284Sobrien      /* Increment ARGS_SO_FAR, which has info about which arg-registers
114252284Sobrien	 have been used, etc.  */
114352284Sobrien
114452284Sobrien      FUNCTION_ARG_ADVANCE (*args_so_far, TYPE_MODE (type), type,
114552284Sobrien			    argpos < n_named_args);
114652284Sobrien    }
114752284Sobrien}
114852284Sobrien
114952284Sobrien/* Update ARGS_SIZE to contain the total size for the argument block.
115052284Sobrien   Return the original constant component of the argument block's size.
115152284Sobrien
115252284Sobrien   REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
115352284Sobrien   for arguments passed in registers.  */
115452284Sobrien
115552284Sobrienstatic int
1156132718Skancompute_argument_block_size (int reg_parm_stack_space,
1157132718Skan			     struct args_size *args_size,
1158132718Skan			     int preferred_stack_boundary ATTRIBUTE_UNUSED)
115952284Sobrien{
116052284Sobrien  int unadjusted_args_size = args_size->constant;
116152284Sobrien
116290075Sobrien  /* For accumulate outgoing args mode we don't need to align, since the frame
116390075Sobrien     will be already aligned.  Align to STACK_BOUNDARY in order to prevent
116490075Sobrien     backends from generating misaligned frame sizes.  */
116590075Sobrien  if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
116690075Sobrien    preferred_stack_boundary = STACK_BOUNDARY;
116790075Sobrien
116852284Sobrien  /* Compute the actual size of the argument block required.  The variable
116952284Sobrien     and constant sizes must be combined, the size may have to be rounded,
117052284Sobrien     and there may be a minimum required size.  */
117152284Sobrien
117252284Sobrien  if (args_size->var)
117352284Sobrien    {
117452284Sobrien      args_size->var = ARGS_SIZE_TREE (*args_size);
117552284Sobrien      args_size->constant = 0;
117652284Sobrien
117790075Sobrien      preferred_stack_boundary /= BITS_PER_UNIT;
117890075Sobrien      if (preferred_stack_boundary > 1)
117990075Sobrien	{
118090075Sobrien	  /* We don't handle this case yet.  To handle it correctly we have
118190075Sobrien	     to add the delta, round and subtract the delta.
118290075Sobrien	     Currently no machine description requires this support.  */
1183169689Skan	  gcc_assert (!(stack_pointer_delta & (preferred_stack_boundary - 1)));
118490075Sobrien	  args_size->var = round_up (args_size->var, preferred_stack_boundary);
118590075Sobrien	}
118652284Sobrien
118752284Sobrien      if (reg_parm_stack_space > 0)
118852284Sobrien	{
118952284Sobrien	  args_size->var
119052284Sobrien	    = size_binop (MAX_EXPR, args_size->var,
119190075Sobrien			  ssize_int (reg_parm_stack_space));
119252284Sobrien
119352284Sobrien#ifndef OUTGOING_REG_PARM_STACK_SPACE
119452284Sobrien	  /* The area corresponding to register parameters is not to count in
119552284Sobrien	     the size of the block we need.  So make the adjustment.  */
119652284Sobrien	  args_size->var
119752284Sobrien	    = size_binop (MINUS_EXPR, args_size->var,
119890075Sobrien			  ssize_int (reg_parm_stack_space));
119952284Sobrien#endif
120052284Sobrien	}
120152284Sobrien    }
120252284Sobrien  else
120352284Sobrien    {
120490075Sobrien      preferred_stack_boundary /= BITS_PER_UNIT;
120590075Sobrien      if (preferred_stack_boundary < 1)
120690075Sobrien	preferred_stack_boundary = 1;
120752284Sobrien      args_size->constant = (((args_size->constant
120890075Sobrien			       + stack_pointer_delta
120990075Sobrien			       + preferred_stack_boundary - 1)
121090075Sobrien			      / preferred_stack_boundary
121190075Sobrien			      * preferred_stack_boundary)
121290075Sobrien			     - stack_pointer_delta);
121352284Sobrien
121452284Sobrien      args_size->constant = MAX (args_size->constant,
121552284Sobrien				 reg_parm_stack_space);
121652284Sobrien
121752284Sobrien#ifndef OUTGOING_REG_PARM_STACK_SPACE
121852284Sobrien      args_size->constant -= reg_parm_stack_space;
121952284Sobrien#endif
122052284Sobrien    }
122152284Sobrien  return unadjusted_args_size;
122252284Sobrien}
122352284Sobrien
122490075Sobrien/* Precompute parameters as needed for a function call.
122552284Sobrien
122690075Sobrien   FLAGS is mask of ECF_* constants.
122752284Sobrien
122852284Sobrien   NUM_ACTUALS is the number of arguments.
122952284Sobrien
123090075Sobrien   ARGS is an array containing information for each argument; this
123190075Sobrien   routine fills in the INITIAL_VALUE and VALUE fields for each
123290075Sobrien   precomputed argument.  */
123352284Sobrien
123452284Sobrienstatic void
1235132718Skanprecompute_arguments (int flags, int num_actuals, struct arg_data *args)
123652284Sobrien{
123752284Sobrien  int i;
123852284Sobrien
1239169689Skan  /* If this is a libcall, then precompute all arguments so that we do not
1240169689Skan     get extraneous instructions emitted as part of the libcall sequence.  */
1241171825Skan
1242171825Skan  /* If we preallocated the stack space, and some arguments must be passed
1243171825Skan     on the stack, then we must precompute any parameter which contains a
1244171825Skan     function call which will store arguments on the stack.
1245171825Skan     Otherwise, evaluating the parameter may clobber previous parameters
1246171825Skan     which have already been stored into the stack.  (we have code to avoid
1247171825Skan     such case by saving the outgoing stack arguments, but it results in
1248171825Skan     worse code)  */
1249171825Skan  if ((flags & ECF_LIBCALL_BLOCK) == 0 && !ACCUMULATE_OUTGOING_ARGS)
1250169689Skan    return;
125152284Sobrien
125252284Sobrien  for (i = 0; i < num_actuals; i++)
1253169689Skan    {
1254169689Skan      enum machine_mode mode;
125590075Sobrien
1256171825Skan      if ((flags & ECF_LIBCALL_BLOCK) == 0
1257171825Skan	  && TREE_CODE (args[i].tree_value) != CALL_EXPR)
1258171825Skan	continue;
1259171825Skan
1260169689Skan      /* If this is an addressable type, we cannot pre-evaluate it.  */
1261169689Skan      gcc_assert (!TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value)));
126252284Sobrien
1263169689Skan      args[i].initial_value = args[i].value
1264169689Skan	= expand_normal (args[i].tree_value);
126552284Sobrien
1266169689Skan      mode = TYPE_MODE (TREE_TYPE (args[i].tree_value));
1267169689Skan      if (mode != args[i].mode)
1268169689Skan	{
1269169689Skan	  args[i].value
1270169689Skan	    = convert_modes (args[i].mode, mode,
1271169689Skan			     args[i].value, args[i].unsignedp);
1272169689Skan#if defined(PROMOTE_FUNCTION_MODE) && !defined(PROMOTE_MODE)
1273169689Skan	  /* CSE will replace this only if it contains args[i].value
1274169689Skan	     pseudo, so convert it down to the declared mode using
1275169689Skan	     a SUBREG.  */
1276169689Skan	  if (REG_P (args[i].value)
1277169689Skan	      && GET_MODE_CLASS (args[i].mode) == MODE_INT)
1278169689Skan	    {
1279169689Skan	      args[i].initial_value
1280169689Skan		= gen_lowpart_SUBREG (mode, args[i].value);
1281169689Skan	      SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1282169689Skan	      SUBREG_PROMOTED_UNSIGNED_SET (args[i].initial_value,
1283169689Skan					    args[i].unsignedp);
1284169689Skan	    }
128590075Sobrien#endif
1286169689Skan	}
1287169689Skan    }
128852284Sobrien}
128952284Sobrien
129052284Sobrien/* Given the current state of MUST_PREALLOCATE and information about
129152284Sobrien   arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
129252284Sobrien   compute and return the final value for MUST_PREALLOCATE.  */
129352284Sobrien
129452284Sobrienstatic int
1295132718Skanfinalize_must_preallocate (int must_preallocate, int num_actuals, struct arg_data *args, struct args_size *args_size)
129652284Sobrien{
129752284Sobrien  /* See if we have or want to preallocate stack space.
129852284Sobrien
129952284Sobrien     If we would have to push a partially-in-regs parm
130052284Sobrien     before other stack parms, preallocate stack space instead.
130152284Sobrien
130252284Sobrien     If the size of some parm is not a multiple of the required stack
130352284Sobrien     alignment, we must preallocate.
130452284Sobrien
130552284Sobrien     If the total size of arguments that would otherwise create a copy in
130652284Sobrien     a temporary (such as a CALL) is more than half the total argument list
130752284Sobrien     size, preallocation is faster.
130852284Sobrien
130952284Sobrien     Another reason to preallocate is if we have a machine (like the m88k)
131052284Sobrien     where stack alignment is required to be maintained between every
131152284Sobrien     pair of insns, not just when the call is made.  However, we assume here
131252284Sobrien     that such machines either do not have push insns (and hence preallocation
131352284Sobrien     would occur anyway) or the problem is taken care of with
131452284Sobrien     PUSH_ROUNDING.  */
131552284Sobrien
131652284Sobrien  if (! must_preallocate)
131752284Sobrien    {
131852284Sobrien      int partial_seen = 0;
131952284Sobrien      int copy_to_evaluate_size = 0;
132052284Sobrien      int i;
132152284Sobrien
132252284Sobrien      for (i = 0; i < num_actuals && ! must_preallocate; i++)
132352284Sobrien	{
132452284Sobrien	  if (args[i].partial > 0 && ! args[i].pass_on_stack)
132552284Sobrien	    partial_seen = 1;
132652284Sobrien	  else if (partial_seen && args[i].reg == 0)
132752284Sobrien	    must_preallocate = 1;
132852284Sobrien
132952284Sobrien	  if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
133052284Sobrien	      && (TREE_CODE (args[i].tree_value) == CALL_EXPR
133152284Sobrien		  || TREE_CODE (args[i].tree_value) == TARGET_EXPR
133252284Sobrien		  || TREE_CODE (args[i].tree_value) == COND_EXPR
133352284Sobrien		  || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
133452284Sobrien	    copy_to_evaluate_size
133552284Sobrien	      += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
133652284Sobrien	}
133752284Sobrien
133852284Sobrien      if (copy_to_evaluate_size * 2 >= args_size->constant
133952284Sobrien	  && args_size->constant > 0)
134052284Sobrien	must_preallocate = 1;
134152284Sobrien    }
134252284Sobrien  return must_preallocate;
134352284Sobrien}
134452284Sobrien
134552284Sobrien/* If we preallocated stack space, compute the address of each argument
134652284Sobrien   and store it into the ARGS array.
134752284Sobrien
134890075Sobrien   We need not ensure it is a valid memory address here; it will be
134952284Sobrien   validized when it is used.
135052284Sobrien
135152284Sobrien   ARGBLOCK is an rtx for the address of the outgoing arguments.  */
135252284Sobrien
135352284Sobrienstatic void
1354132718Skancompute_argument_addresses (struct arg_data *args, rtx argblock, int num_actuals)
135552284Sobrien{
135652284Sobrien  if (argblock)
135752284Sobrien    {
135852284Sobrien      rtx arg_reg = argblock;
135952284Sobrien      int i, arg_offset = 0;
136052284Sobrien
136152284Sobrien      if (GET_CODE (argblock) == PLUS)
136252284Sobrien	arg_reg = XEXP (argblock, 0), arg_offset = INTVAL (XEXP (argblock, 1));
136352284Sobrien
136452284Sobrien      for (i = 0; i < num_actuals; i++)
136552284Sobrien	{
1366132718Skan	  rtx offset = ARGS_SIZE_RTX (args[i].locate.offset);
1367132718Skan	  rtx slot_offset = ARGS_SIZE_RTX (args[i].locate.slot_offset);
136852284Sobrien	  rtx addr;
1369169689Skan	  unsigned int align, boundary;
1370169689Skan	  unsigned int units_on_stack = 0;
1371169689Skan	  enum machine_mode partial_mode = VOIDmode;
137252284Sobrien
137352284Sobrien	  /* Skip this parm if it will not be passed on the stack.  */
1374169689Skan	  if (! args[i].pass_on_stack
1375169689Skan	      && args[i].reg != 0
1376169689Skan	      && args[i].partial == 0)
137752284Sobrien	    continue;
137852284Sobrien
137952284Sobrien	  if (GET_CODE (offset) == CONST_INT)
138052284Sobrien	    addr = plus_constant (arg_reg, INTVAL (offset));
138152284Sobrien	  else
138252284Sobrien	    addr = gen_rtx_PLUS (Pmode, arg_reg, offset);
138352284Sobrien
138452284Sobrien	  addr = plus_constant (addr, arg_offset);
138552284Sobrien
1386169689Skan	  if (args[i].partial != 0)
1387169689Skan	    {
1388169689Skan	      /* Only part of the parameter is being passed on the stack.
1389169689Skan		 Generate a simple memory reference of the correct size.  */
1390169689Skan	      units_on_stack = args[i].locate.size.constant;
1391169689Skan	      partial_mode = mode_for_size (units_on_stack * BITS_PER_UNIT,
1392169689Skan					    MODE_INT, 1);
1393169689Skan	      args[i].stack = gen_rtx_MEM (partial_mode, addr);
1394169689Skan	      set_mem_size (args[i].stack, GEN_INT (units_on_stack));
1395169689Skan	    }
1396169689Skan	  else
1397169689Skan	    {
1398169689Skan	      args[i].stack = gen_rtx_MEM (args[i].mode, addr);
1399169689Skan	      set_mem_attributes (args[i].stack,
1400169689Skan				  TREE_TYPE (args[i].tree_value), 1);
1401169689Skan	    }
1402169689Skan	  align = BITS_PER_UNIT;
1403169689Skan	  boundary = args[i].locate.boundary;
1404169689Skan	  if (args[i].locate.where_pad != downward)
1405169689Skan	    align = boundary;
1406169689Skan	  else if (GET_CODE (offset) == CONST_INT)
1407169689Skan	    {
1408169689Skan	      align = INTVAL (offset) * BITS_PER_UNIT | boundary;
1409169689Skan	      align = align & -align;
1410169689Skan	    }
1411169689Skan	  set_mem_align (args[i].stack, align);
1412169689Skan
141352284Sobrien	  if (GET_CODE (slot_offset) == CONST_INT)
141452284Sobrien	    addr = plus_constant (arg_reg, INTVAL (slot_offset));
141552284Sobrien	  else
141652284Sobrien	    addr = gen_rtx_PLUS (Pmode, arg_reg, slot_offset);
141752284Sobrien
141852284Sobrien	  addr = plus_constant (addr, arg_offset);
141990075Sobrien
1420169689Skan	  if (args[i].partial != 0)
1421169689Skan	    {
1422169689Skan	      /* Only part of the parameter is being passed on the stack.
1423169689Skan		 Generate a simple memory reference of the correct size.  */
1424169689Skan	      args[i].stack_slot = gen_rtx_MEM (partial_mode, addr);
1425169689Skan	      set_mem_size (args[i].stack_slot, GEN_INT (units_on_stack));
1426169689Skan	    }
1427169689Skan	  else
1428169689Skan	    {
1429169689Skan	      args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
1430169689Skan	      set_mem_attributes (args[i].stack_slot,
1431169689Skan				  TREE_TYPE (args[i].tree_value), 1);
1432169689Skan	    }
1433169689Skan	  set_mem_align (args[i].stack_slot, args[i].locate.boundary);
1434169689Skan
143590075Sobrien	  /* Function incoming arguments may overlap with sibling call
143690075Sobrien	     outgoing arguments and we cannot allow reordering of reads
143790075Sobrien	     from function arguments with stores to outgoing arguments
143890075Sobrien	     of sibling calls.  */
143990075Sobrien	  set_mem_alias_set (args[i].stack, 0);
144090075Sobrien	  set_mem_alias_set (args[i].stack_slot, 0);
144152284Sobrien	}
144252284Sobrien    }
144352284Sobrien}
144490075Sobrien
144552284Sobrien/* Given a FNDECL and EXP, return an rtx suitable for use as a target address
144652284Sobrien   in a call instruction.
144752284Sobrien
144852284Sobrien   FNDECL is the tree node for the target function.  For an indirect call
144952284Sobrien   FNDECL will be NULL_TREE.
145052284Sobrien
1451117395Skan   ADDR is the operand 0 of CALL_EXPR for this call.  */
145252284Sobrien
145352284Sobrienstatic rtx
1454132718Skanrtx_for_function_call (tree fndecl, tree addr)
145552284Sobrien{
145652284Sobrien  rtx funexp;
145752284Sobrien
145852284Sobrien  /* Get the function to call, in the form of RTL.  */
145952284Sobrien  if (fndecl)
146052284Sobrien    {
146152284Sobrien      /* If this is the first use of the function, see if we need to
146252284Sobrien	 make an external definition for it.  */
146352284Sobrien      if (! TREE_USED (fndecl))
146452284Sobrien	{
146552284Sobrien	  assemble_external (fndecl);
146652284Sobrien	  TREE_USED (fndecl) = 1;
146752284Sobrien	}
146852284Sobrien
146952284Sobrien      /* Get a SYMBOL_REF rtx for the function address.  */
147052284Sobrien      funexp = XEXP (DECL_RTL (fndecl), 0);
147152284Sobrien    }
147252284Sobrien  else
147352284Sobrien    /* Generate an rtx (probably a pseudo-register) for the address.  */
147452284Sobrien    {
147552284Sobrien      push_temp_slots ();
1476169689Skan      funexp = expand_normal (addr);
147790075Sobrien      pop_temp_slots ();	/* FUNEXP can't be BLKmode.  */
147852284Sobrien    }
147952284Sobrien  return funexp;
148052284Sobrien}
148152284Sobrien
1482169689Skan/* Return true if and only if SIZE storage units (usually bytes)
1483169689Skan   starting from address ADDR overlap with already clobbered argument
1484169689Skan   area.  This function is used to determine if we should give up a
1485169689Skan   sibcall.  */
1486169689Skan
1487169689Skanstatic bool
1488169689Skanmem_overlaps_already_clobbered_arg_p (rtx addr, unsigned HOST_WIDE_INT size)
1489169689Skan{
1490169689Skan  HOST_WIDE_INT i;
1491169689Skan
1492169689Skan  if (addr == current_function_internal_arg_pointer)
1493169689Skan    i = 0;
1494169689Skan  else if (GET_CODE (addr) == PLUS
1495169689Skan	   && XEXP (addr, 0) == current_function_internal_arg_pointer
1496169689Skan	   && GET_CODE (XEXP (addr, 1)) == CONST_INT)
1497169689Skan    i = INTVAL (XEXP (addr, 1));
1498169689Skan  /* Return true for arg pointer based indexed addressing.  */
1499169689Skan  else if (GET_CODE (addr) == PLUS
1500169689Skan	   && (XEXP (addr, 0) == current_function_internal_arg_pointer
1501169689Skan	       || XEXP (addr, 1) == current_function_internal_arg_pointer))
1502169689Skan    return true;
1503169689Skan  else
1504169689Skan    return false;
1505169689Skan
1506169689Skan#ifdef ARGS_GROW_DOWNWARD
1507169689Skan  i = -i - size;
1508169689Skan#endif
1509169689Skan  if (size > 0)
1510169689Skan    {
1511169689Skan      unsigned HOST_WIDE_INT k;
1512169689Skan
1513169689Skan      for (k = 0; k < size; k++)
1514169689Skan	if (i + k < stored_args_map->n_bits
1515169689Skan	    && TEST_BIT (stored_args_map, i + k))
1516169689Skan	  return true;
1517169689Skan    }
1518169689Skan
1519169689Skan  return false;
1520169689Skan}
1521169689Skan
152252284Sobrien/* Do the register loads required for any wholly-register parms or any
152352284Sobrien   parms which are passed both on the stack and in a register.  Their
152490075Sobrien   expressions were already evaluated.
152552284Sobrien
152652284Sobrien   Mark all register-parms as living through the call, putting these USE
1527132718Skan   insns in the CALL_INSN_FUNCTION_USAGE field.
152852284Sobrien
1529169689Skan   When IS_SIBCALL, perform the check_sibcall_argument_overlap
1530132718Skan   checking, setting *SIBCALL_FAILURE if appropriate.  */
1531132718Skan
153252284Sobrienstatic void
1533132718Skanload_register_parameters (struct arg_data *args, int num_actuals,
1534132718Skan			  rtx *call_fusage, int flags, int is_sibcall,
1535132718Skan			  int *sibcall_failure)
153652284Sobrien{
153752284Sobrien  int i, j;
153852284Sobrien
153952284Sobrien  for (i = 0; i < num_actuals; i++)
154052284Sobrien    {
154190075Sobrien      rtx reg = ((flags & ECF_SIBCALL)
154290075Sobrien		 ? args[i].tail_call_reg : args[i].reg);
154352284Sobrien      if (reg)
154452284Sobrien	{
1545132718Skan	  int partial = args[i].partial;
1546132718Skan	  int nregs;
1547132718Skan	  int size = 0;
1548132718Skan	  rtx before_arg = get_last_insn ();
1549169689Skan	  /* Set non-negative if we must move a word at a time, even if
1550169689Skan	     just one word (e.g, partial == 4 && mode == DFmode).  Set
1551169689Skan	     to -1 if we just use a normal move insn.  This value can be
1552169689Skan	     zero if the argument is a zero size structure.  */
1553132718Skan	  nregs = -1;
1554169689Skan	  if (GET_CODE (reg) == PARALLEL)
1555169689Skan	    ;
1556169689Skan	  else if (partial)
1557169689Skan	    {
1558169689Skan	      gcc_assert (partial % UNITS_PER_WORD == 0);
1559169689Skan	      nregs = partial / UNITS_PER_WORD;
1560169689Skan	    }
1561132718Skan	  else if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode)
1562132718Skan	    {
1563132718Skan	      size = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1564132718Skan	      nregs = (size + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
1565132718Skan	    }
1566132718Skan	  else
1567132718Skan	    size = GET_MODE_SIZE (args[i].mode);
156852284Sobrien
156952284Sobrien	  /* Handle calls that pass values in multiple non-contiguous
157052284Sobrien	     locations.  The Irix 6 ABI has examples of this.  */
157152284Sobrien
157252284Sobrien	  if (GET_CODE (reg) == PARALLEL)
1573169689Skan	    emit_group_move (reg, args[i].parallel_value);
157452284Sobrien
157552284Sobrien	  /* If simple case, just do move.  If normal partial, store_one_arg
157652284Sobrien	     has already loaded the register for us.  In all other cases,
157752284Sobrien	     load the register(s) from memory.  */
157852284Sobrien
157952284Sobrien	  else if (nregs == -1)
1580132718Skan	    {
1581132718Skan	      emit_move_insn (reg, args[i].value);
1582132718Skan#ifdef BLOCK_REG_PADDING
1583132718Skan	      /* Handle case where we have a value that needs shifting
1584132718Skan		 up to the msb.  eg. a QImode value and we're padding
1585132718Skan		 upward on a BYTES_BIG_ENDIAN machine.  */
1586132718Skan	      if (size < UNITS_PER_WORD
1587132718Skan		  && (args[i].locate.where_pad
1588132718Skan		      == (BYTES_BIG_ENDIAN ? upward : downward)))
1589132718Skan		{
1590132718Skan		  rtx x;
1591132718Skan		  int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
159252284Sobrien
1593132718Skan		  /* Assigning REG here rather than a temp makes CALL_FUSAGE
1594132718Skan		     report the whole reg as used.  Strictly speaking, the
1595132718Skan		     call only uses SIZE bytes at the msb end, but it doesn't
1596132718Skan		     seem worth generating rtl to say that.  */
1597132718Skan		  reg = gen_rtx_REG (word_mode, REGNO (reg));
1598169689Skan		  x = expand_shift (LSHIFT_EXPR, word_mode, reg,
1599169689Skan				    build_int_cst (NULL_TREE, shift),
1600169689Skan				    reg, 1);
1601132718Skan		  if (x != reg)
1602132718Skan		    emit_move_insn (reg, x);
1603132718Skan		}
1604132718Skan#endif
1605132718Skan	    }
1606132718Skan
160752284Sobrien	  /* If we have pre-computed the values to put in the registers in
160852284Sobrien	     the case of non-aligned structures, copy them in now.  */
160952284Sobrien
161052284Sobrien	  else if (args[i].n_aligned_regs != 0)
161152284Sobrien	    for (j = 0; j < args[i].n_aligned_regs; j++)
161252284Sobrien	      emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
161352284Sobrien			      args[i].aligned_regs[j]);
161452284Sobrien
161552284Sobrien	  else if (partial == 0 || args[i].pass_on_stack)
1616132718Skan	    {
1617132718Skan	      rtx mem = validize_mem (args[i].value);
161852284Sobrien
1619169689Skan	      /* Check for overlap with already clobbered argument area.  */
1620169689Skan	      if (is_sibcall
1621169689Skan		  && mem_overlaps_already_clobbered_arg_p (XEXP (args[i].value, 0),
1622169689Skan							   size))
1623169689Skan		*sibcall_failure = 1;
1624169689Skan
1625132718Skan	      /* Handle a BLKmode that needs shifting.  */
1626132718Skan	      if (nregs == 1 && size < UNITS_PER_WORD
1627169689Skan#ifdef BLOCK_REG_PADDING
1628169689Skan		  && args[i].locate.where_pad == downward
1629169689Skan#else
1630169689Skan		  && BYTES_BIG_ENDIAN
1631169689Skan#endif
1632169689Skan		 )
1633132718Skan		{
1634132718Skan		  rtx tem = operand_subword_force (mem, 0, args[i].mode);
1635132718Skan		  rtx ri = gen_rtx_REG (word_mode, REGNO (reg));
1636132718Skan		  rtx x = gen_reg_rtx (word_mode);
1637132718Skan		  int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
1638169689Skan		  enum tree_code dir = BYTES_BIG_ENDIAN ? RSHIFT_EXPR
1639169689Skan							: LSHIFT_EXPR;
1640132718Skan
1641132718Skan		  emit_move_insn (x, tem);
1642169689Skan		  x = expand_shift (dir, word_mode, x,
1643169689Skan				    build_int_cst (NULL_TREE, shift),
1644169689Skan				    ri, 1);
1645132718Skan		  if (x != ri)
1646132718Skan		    emit_move_insn (ri, x);
1647132718Skan		}
1648132718Skan	      else
1649132718Skan		move_block_to_reg (REGNO (reg), mem, nregs, args[i].mode);
1650132718Skan	    }
1651132718Skan
1652132718Skan	  /* When a parameter is a block, and perhaps in other cases, it is
1653132718Skan	     possible that it did a load from an argument slot that was
1654132718Skan	     already clobbered.  */
1655132718Skan	  if (is_sibcall
1656132718Skan	      && check_sibcall_argument_overlap (before_arg, &args[i], 0))
1657132718Skan	    *sibcall_failure = 1;
1658132718Skan
165952284Sobrien	  /* Handle calls that pass values in multiple non-contiguous
166052284Sobrien	     locations.  The Irix 6 ABI has examples of this.  */
166152284Sobrien	  if (GET_CODE (reg) == PARALLEL)
166252284Sobrien	    use_group_regs (call_fusage, reg);
166352284Sobrien	  else if (nregs == -1)
166452284Sobrien	    use_reg (call_fusage, reg);
1665146895Skan	  else if (nregs > 0)
1666146895Skan	    use_regs (call_fusage, REGNO (reg), nregs);
166752284Sobrien	}
166852284Sobrien    }
166952284Sobrien}
167052284Sobrien
167190075Sobrien/* We need to pop PENDING_STACK_ADJUST bytes.  But, if the arguments
167290075Sobrien   wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
167390075Sobrien   bytes, then we would need to push some additional bytes to pad the
167490075Sobrien   arguments.  So, we compute an adjust to the stack pointer for an
167590075Sobrien   amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
167690075Sobrien   bytes.  Then, when the arguments are pushed the stack will be perfectly
167790075Sobrien   aligned.  ARGS_SIZE->CONSTANT is set to the number of bytes that should
167890075Sobrien   be popped after the call.  Returns the adjustment.  */
167990075Sobrien
168090075Sobrienstatic int
1681132718Skancombine_pending_stack_adjustment_and_call (int unadjusted_args_size,
1682132718Skan					   struct args_size *args_size,
1683169689Skan					   unsigned int preferred_unit_stack_boundary)
168490075Sobrien{
168590075Sobrien  /* The number of bytes to pop so that the stack will be
168690075Sobrien     under-aligned by UNADJUSTED_ARGS_SIZE bytes.  */
168790075Sobrien  HOST_WIDE_INT adjustment;
168890075Sobrien  /* The alignment of the stack after the arguments are pushed, if we
168990075Sobrien     just pushed the arguments without adjust the stack here.  */
1690169689Skan  unsigned HOST_WIDE_INT unadjusted_alignment;
169190075Sobrien
169290075Sobrien  unadjusted_alignment
169390075Sobrien    = ((stack_pointer_delta + unadjusted_args_size)
169490075Sobrien       % preferred_unit_stack_boundary);
169590075Sobrien
169690075Sobrien  /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
169790075Sobrien     as possible -- leaving just enough left to cancel out the
169890075Sobrien     UNADJUSTED_ALIGNMENT.  In other words, we want to ensure that the
169990075Sobrien     PENDING_STACK_ADJUST is non-negative, and congruent to
170090075Sobrien     -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY.  */
170190075Sobrien
170290075Sobrien  /* Begin by trying to pop all the bytes.  */
170390075Sobrien  unadjusted_alignment
170490075Sobrien    = (unadjusted_alignment
170590075Sobrien       - (pending_stack_adjust % preferred_unit_stack_boundary));
170690075Sobrien  adjustment = pending_stack_adjust;
170790075Sobrien  /* Push enough additional bytes that the stack will be aligned
170890075Sobrien     after the arguments are pushed.  */
170990075Sobrien  if (preferred_unit_stack_boundary > 1)
171090075Sobrien    {
171190075Sobrien      if (unadjusted_alignment > 0)
171290075Sobrien	adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
171390075Sobrien      else
171490075Sobrien	adjustment += unadjusted_alignment;
171590075Sobrien    }
171690075Sobrien
171790075Sobrien  /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
171890075Sobrien     bytes after the call.  The right number is the entire
171990075Sobrien     PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
172090075Sobrien     by the arguments in the first place.  */
172190075Sobrien  args_size->constant
172290075Sobrien    = pending_stack_adjust - adjustment + unadjusted_args_size;
172390075Sobrien
172490075Sobrien  return adjustment;
172590075Sobrien}
172690075Sobrien
172790075Sobrien/* Scan X expression if it does not dereference any argument slots
172890075Sobrien   we already clobbered by tail call arguments (as noted in stored_args_map
172990075Sobrien   bitmap).
1730117395Skan   Return nonzero if X expression dereferences such argument slots,
173190075Sobrien   zero otherwise.  */
173290075Sobrien
173390075Sobrienstatic int
1734132718Skancheck_sibcall_argument_overlap_1 (rtx x)
173590075Sobrien{
173690075Sobrien  RTX_CODE code;
173790075Sobrien  int i, j;
173890075Sobrien  const char *fmt;
173990075Sobrien
174090075Sobrien  if (x == NULL_RTX)
174190075Sobrien    return 0;
174290075Sobrien
174390075Sobrien  code = GET_CODE (x);
174490075Sobrien
174590075Sobrien  if (code == MEM)
1746169689Skan    return mem_overlaps_already_clobbered_arg_p (XEXP (x, 0),
1747169689Skan						 GET_MODE_SIZE (GET_MODE (x)));
174890075Sobrien
174990075Sobrien  /* Scan all subexpressions.  */
175090075Sobrien  fmt = GET_RTX_FORMAT (code);
175190075Sobrien  for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
175290075Sobrien    {
175390075Sobrien      if (*fmt == 'e')
175490075Sobrien	{
175590075Sobrien	  if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
175690075Sobrien	    return 1;
175790075Sobrien	}
175890075Sobrien      else if (*fmt == 'E')
175990075Sobrien	{
176090075Sobrien	  for (j = 0; j < XVECLEN (x, i); j++)
176190075Sobrien	    if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
176290075Sobrien	      return 1;
176390075Sobrien	}
176490075Sobrien    }
176590075Sobrien  return 0;
176690075Sobrien}
176790075Sobrien
176890075Sobrien/* Scan sequence after INSN if it does not dereference any argument slots
176990075Sobrien   we already clobbered by tail call arguments (as noted in stored_args_map
1770132718Skan   bitmap).  If MARK_STORED_ARGS_MAP, add stack slots for ARG to
1771132718Skan   stored_args_map bitmap afterwards (when ARG is a register MARK_STORED_ARGS_MAP
1772132718Skan   should be 0).  Return nonzero if sequence after INSN dereferences such argument
1773132718Skan   slots, zero otherwise.  */
177490075Sobrien
177590075Sobrienstatic int
1776132718Skancheck_sibcall_argument_overlap (rtx insn, struct arg_data *arg, int mark_stored_args_map)
177790075Sobrien{
177890075Sobrien  int low, high;
177990075Sobrien
178090075Sobrien  if (insn == NULL_RTX)
178190075Sobrien    insn = get_insns ();
178290075Sobrien  else
178390075Sobrien    insn = NEXT_INSN (insn);
178490075Sobrien
178590075Sobrien  for (; insn; insn = NEXT_INSN (insn))
178690075Sobrien    if (INSN_P (insn)
178790075Sobrien	&& check_sibcall_argument_overlap_1 (PATTERN (insn)))
178890075Sobrien      break;
178990075Sobrien
1790132718Skan  if (mark_stored_args_map)
1791132718Skan    {
179290075Sobrien#ifdef ARGS_GROW_DOWNWARD
1793132718Skan      low = -arg->locate.slot_offset.constant - arg->locate.size.constant;
179490075Sobrien#else
1795132718Skan      low = arg->locate.slot_offset.constant;
179690075Sobrien#endif
179790075Sobrien
1798132718Skan      for (high = low + arg->locate.size.constant; low < high; low++)
1799132718Skan	SET_BIT (stored_args_map, low);
1800132718Skan    }
180190075Sobrien  return insn != NULL_RTX;
180290075Sobrien}
180390075Sobrien
1804169689Skan/* Given that a function returns a value of mode MODE at the most
1805169689Skan   significant end of hard register VALUE, shift VALUE left or right
1806169689Skan   as specified by LEFT_P.  Return true if some action was needed.  */
1807169689Skan
1808169689Skanbool
1809169689Skanshift_return_value (enum machine_mode mode, bool left_p, rtx value)
1810110611Skan{
1811169689Skan  HOST_WIDE_INT shift;
1812110611Skan
1813169689Skan  gcc_assert (REG_P (value) && HARD_REGISTER_P (value));
1814169689Skan  shift = GET_MODE_BITSIZE (GET_MODE (value)) - GET_MODE_BITSIZE (mode);
1815169689Skan  if (shift == 0)
1816169689Skan    return false;
1817110611Skan
1818169689Skan  /* Use ashr rather than lshr for right shifts.  This is for the benefit
1819169689Skan     of the MIPS port, which requires SImode values to be sign-extended
1820169689Skan     when stored in 64-bit registers.  */
1821169689Skan  if (!force_expand_binop (GET_MODE (value), left_p ? ashl_optab : ashr_optab,
1822169689Skan			   value, GEN_INT (shift), value, 1, OPTAB_WIDEN))
1823169689Skan    gcc_unreachable ();
1824169689Skan  return true;
1825110611Skan}
1826110611Skan
182718334Speter/* Generate all the code for a function call
182818334Speter   and return an rtx for its value.
182918334Speter   Store the value in TARGET (specified as an rtx) if convenient.
183018334Speter   If the value is stored in TARGET then TARGET is returned.
183118334Speter   If IGNORE is nonzero, then we ignore the value of the function call.  */
183218334Speter
183318334Speterrtx
1834132718Skanexpand_call (tree exp, rtx target, int ignore)
183518334Speter{
183690075Sobrien  /* Nonzero if we are currently expanding a call.  */
183790075Sobrien  static int currently_expanding_call = 0;
183890075Sobrien
183918334Speter  /* List of actual parameters.  */
184018334Speter  tree actparms = TREE_OPERAND (exp, 1);
184118334Speter  /* RTX for the function to be called.  */
184218334Speter  rtx funexp;
184390075Sobrien  /* Sequence of insns to perform a normal "call".  */
184490075Sobrien  rtx normal_call_insns = NULL_RTX;
1845169689Skan  /* Sequence of insns to perform a tail "call".  */
184690075Sobrien  rtx tail_call_insns = NULL_RTX;
184718334Speter  /* Data type of the function.  */
184818334Speter  tree funtype;
1849132718Skan  tree type_arg_types;
185018334Speter  /* Declaration of the function being called,
185118334Speter     or 0 if the function is computed (not known by name).  */
185218334Speter  tree fndecl = 0;
1853132718Skan  /* The type of the function being called.  */
1854132718Skan  tree fntype;
1855169689Skan  bool try_tail_call = CALL_EXPR_TAILCALL (exp);
185690075Sobrien  int pass;
185718334Speter
185818334Speter  /* Register in which non-BLKmode value will be returned,
185918334Speter     or 0 if no value or if value is BLKmode.  */
186018334Speter  rtx valreg;
186118334Speter  /* Address where we should return a BLKmode value;
186218334Speter     0 if value not BLKmode.  */
186318334Speter  rtx structure_value_addr = 0;
186418334Speter  /* Nonzero if that address is being passed by treating it as
186518334Speter     an extra, implicit first parameter.  Otherwise,
186618334Speter     it is passed by being copied directly into struct_value_rtx.  */
186718334Speter  int structure_value_addr_parm = 0;
186818334Speter  /* Size of aggregate value wanted, or zero if none wanted
186918334Speter     or if we are using the non-reentrant PCC calling convention
187018334Speter     or expecting the value in registers.  */
187150397Sobrien  HOST_WIDE_INT struct_value_size = 0;
187218334Speter  /* Nonzero if called function returns an aggregate in memory PCC style,
187318334Speter     by returning the address of where to find it.  */
187418334Speter  int pcc_struct_value = 0;
1875132718Skan  rtx struct_value = 0;
187618334Speter
187718334Speter  /* Number of actual parameters in this call, including struct value addr.  */
187818334Speter  int num_actuals;
187918334Speter  /* Number of named args.  Args after this are anonymous ones
188018334Speter     and they must all go on the stack.  */
188118334Speter  int n_named_args;
188218334Speter
188318334Speter  /* Vector of information about each argument.
188418334Speter     Arguments are numbered in the order they will be pushed,
188518334Speter     not the order they are written.  */
188618334Speter  struct arg_data *args;
188718334Speter
188818334Speter  /* Total size in bytes of all the stack-parms scanned so far.  */
188918334Speter  struct args_size args_size;
189090075Sobrien  struct args_size adjusted_args_size;
189118334Speter  /* Size of arguments before any adjustments (such as rounding).  */
189252284Sobrien  int unadjusted_args_size;
189318334Speter  /* Data on reg parms scanned so far.  */
189418334Speter  CUMULATIVE_ARGS args_so_far;
189518334Speter  /* Nonzero if a reg parm has been scanned.  */
189618334Speter  int reg_parm_seen;
189718334Speter  /* Nonzero if this is an indirect function call.  */
189818334Speter
189990075Sobrien  /* Nonzero if we must avoid push-insns in the args for this call.
190018334Speter     If stack space is allocated for register parameters, but not by the
190118334Speter     caller, then it is preallocated in the fixed part of the stack frame.
190218334Speter     So the entire argument block must then be preallocated (i.e., we
190318334Speter     ignore PUSH_ROUNDING in that case).  */
190418334Speter
190590075Sobrien  int must_preallocate = !PUSH_ARGS;
190618334Speter
190718334Speter  /* Size of the stack reserved for parameter registers.  */
190818334Speter  int reg_parm_stack_space = 0;
190918334Speter
191018334Speter  /* Address of space preallocated for stack parms
191118334Speter     (on machines that lack push insns), or 0 if space not preallocated.  */
191218334Speter  rtx argblock = 0;
191318334Speter
191490075Sobrien  /* Mask of ECF_ flags.  */
191590075Sobrien  int flags = 0;
191690075Sobrien#ifdef REG_PARM_STACK_SPACE
191718334Speter  /* Define the boundary of the register parm stack space that needs to be
1918132718Skan     saved, if any.  */
1919132718Skan  int low_to_save, high_to_save;
192018334Speter  rtx save_area = 0;		/* Place that it is saved */
192118334Speter#endif
192218334Speter
192318334Speter  int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
192418334Speter  char *initial_stack_usage_map = stack_usage_map;
1925169689Skan  char *stack_usage_map_buf = NULL;
192618334Speter
1927132718Skan  int old_stack_allocated;
1928132718Skan
1929132718Skan  /* State variables to track stack modifications.  */
193018334Speter  rtx old_stack_level = 0;
1931132718Skan  int old_stack_arg_under_construction = 0;
193218334Speter  int old_pending_adj = 0;
193318334Speter  int old_inhibit_defer_pop = inhibit_defer_pop;
1934132718Skan
1935132718Skan  /* Some stack pointer alterations we make are performed via
1936132718Skan     allocate_dynamic_stack_space. This modifies the stack_pointer_delta,
1937132718Skan     which we then also need to save/restore along the way.  */
1938132718Skan  int old_stack_pointer_delta = 0;
1939132718Skan
194090075Sobrien  rtx call_fusage;
194190075Sobrien  tree p = TREE_OPERAND (exp, 0);
1942117395Skan  tree addr = TREE_OPERAND (exp, 0);
194390075Sobrien  int i;
194490075Sobrien  /* The alignment of the stack, in bits.  */
1945169689Skan  unsigned HOST_WIDE_INT preferred_stack_boundary;
194690075Sobrien  /* The alignment of the stack, in bytes.  */
1947169689Skan  unsigned HOST_WIDE_INT preferred_unit_stack_boundary;
1948169689Skan  /* The static chain value to use for this call.  */
1949169689Skan  rtx static_chain_value;
195090075Sobrien  /* See if this is "nothrow" function call.  */
195190075Sobrien  if (TREE_NOTHROW (exp))
195290075Sobrien    flags |= ECF_NOTHROW;
195350397Sobrien
1954169689Skan  /* See if we can find a DECL-node for the actual function, and get the
1955169689Skan     function attributes (flags) from the function decl or type node.  */
195690075Sobrien  fndecl = get_callee_fndecl (exp);
195790075Sobrien  if (fndecl)
195818334Speter    {
1959132718Skan      fntype = TREE_TYPE (fndecl);
196090075Sobrien      flags |= flags_from_decl_or_type (fndecl);
196118334Speter    }
196290075Sobrien  else
1963132718Skan    {
1964132718Skan      fntype = TREE_TYPE (TREE_TYPE (p));
1965132718Skan      flags |= flags_from_decl_or_type (fntype);
1966132718Skan    }
196790075Sobrien
1968132718Skan  struct_value = targetm.calls.struct_value_rtx (fntype, 0);
1969132718Skan
1970132718Skan  /* Warn if this value is an aggregate type,
1971132718Skan     regardless of which calling convention we are using for it.  */
1972169689Skan  if (AGGREGATE_TYPE_P (TREE_TYPE (exp)))
1973169689Skan    warning (OPT_Waggregate_return, "function call has aggregate value");
1974132718Skan
1975132718Skan  /* If the result of a pure or const function call is ignored (or void),
1976132718Skan     and none of its arguments are volatile, we can avoid expanding the
1977132718Skan     call and just evaluate the arguments for side-effects.  */
1978132718Skan  if ((flags & (ECF_CONST | ECF_PURE))
1979132718Skan      && (ignore || target == const0_rtx
1980132718Skan	  || TYPE_MODE (TREE_TYPE (exp)) == VOIDmode))
1981132718Skan    {
1982132718Skan      bool volatilep = false;
1983132718Skan      tree arg;
1984132718Skan
1985132718Skan      for (arg = actparms; arg; arg = TREE_CHAIN (arg))
1986132718Skan	if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
1987132718Skan	  {
1988132718Skan	    volatilep = true;
1989132718Skan	    break;
1990132718Skan	  }
1991132718Skan
1992132718Skan      if (! volatilep)
1993132718Skan	{
1994132718Skan	  for (arg = actparms; arg; arg = TREE_CHAIN (arg))
1995132718Skan	    expand_expr (TREE_VALUE (arg), const0_rtx,
1996132718Skan			 VOIDmode, EXPAND_NORMAL);
1997132718Skan	  return const0_rtx;
1998132718Skan	}
1999132718Skan    }
2000132718Skan
200118334Speter#ifdef REG_PARM_STACK_SPACE
200218334Speter  reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
200318334Speter#endif
200418334Speter
200590075Sobrien#ifndef OUTGOING_REG_PARM_STACK_SPACE
200690075Sobrien  if (reg_parm_stack_space > 0 && PUSH_ARGS)
200750397Sobrien    must_preallocate = 1;
200850397Sobrien#endif
200950397Sobrien
201018334Speter  /* Set up a place to return a structure.  */
201118334Speter
201218334Speter  /* Cater to broken compilers.  */
2013132718Skan  if (aggregate_value_p (exp, fndecl))
201418334Speter    {
201518334Speter      /* This call returns a big structure.  */
201690075Sobrien      flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
201718334Speter
201818334Speter#ifdef PCC_STATIC_STRUCT_RETURN
201918334Speter      {
202018334Speter	pcc_struct_value = 1;
202118334Speter      }
202218334Speter#else /* not PCC_STATIC_STRUCT_RETURN */
202318334Speter      {
202418334Speter	struct_value_size = int_size_in_bytes (TREE_TYPE (exp));
202518334Speter
2026169689Skan	if (target && MEM_P (target) && CALL_EXPR_RETURN_SLOT_OPT (exp))
202718334Speter	  structure_value_addr = XEXP (target, 0);
202818334Speter	else
202918334Speter	  {
203018334Speter	    /* For variable-sized objects, we must be called with a target
203118334Speter	       specified.  If we were to allocate space on the stack here,
203218334Speter	       we would have no way of knowing when to free it.  */
2033169689Skan	    rtx d = assign_temp (TREE_TYPE (exp), 0, 1, 1);
203418334Speter
203590075Sobrien	    mark_temp_addr_taken (d);
203690075Sobrien	    structure_value_addr = XEXP (d, 0);
203718334Speter	    target = 0;
203818334Speter	  }
203918334Speter      }
204018334Speter#endif /* not PCC_STATIC_STRUCT_RETURN */
204118334Speter    }
204218334Speter
204390075Sobrien  /* Figure out the amount to which the stack should be aligned.  */
204490075Sobrien  preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
2045132718Skan  if (fndecl)
2046132718Skan    {
2047132718Skan      struct cgraph_rtl_info *i = cgraph_rtl_info (fndecl);
2048132718Skan      if (i && i->preferred_incoming_stack_boundary)
2049132718Skan	preferred_stack_boundary = i->preferred_incoming_stack_boundary;
2050132718Skan    }
205118334Speter
205252284Sobrien  /* Operand 0 is a pointer-to-function; get the type of the function.  */
2053117395Skan  funtype = TREE_TYPE (addr);
2054261188Spfg  /* APPLE LOCAL blocks */
2055261188Spfg  gcc_assert (POINTER_TYPE_P (funtype) || TREE_CODE (funtype) == BLOCK_POINTER_TYPE);
205652284Sobrien  funtype = TREE_TYPE (funtype);
205718334Speter
2058132718Skan  /* Munge the tree to split complex arguments into their imaginary
2059132718Skan     and real parts.  */
2060132718Skan  if (targetm.calls.split_complex_arg)
2061132718Skan    {
2062132718Skan      type_arg_types = split_complex_types (TYPE_ARG_TYPES (funtype));
2063132718Skan      actparms = split_complex_values (actparms);
2064132718Skan    }
2065132718Skan  else
2066132718Skan    type_arg_types = TYPE_ARG_TYPES (funtype);
2067132718Skan
206890075Sobrien  if (flags & ECF_MAY_BE_ALLOCA)
206990075Sobrien    current_function_calls_alloca = 1;
207018334Speter
207118334Speter  /* If struct_value_rtx is 0, it means pass the address
207218334Speter     as if it were an extra parameter.  */
2073132718Skan  if (structure_value_addr && struct_value == 0)
207418334Speter    {
207518334Speter      /* If structure_value_addr is a REG other than
207618334Speter	 virtual_outgoing_args_rtx, we can use always use it.  If it
207718334Speter	 is not a REG, we must always copy it into a register.
207818334Speter	 If it is virtual_outgoing_args_rtx, we must copy it to another
207918334Speter	 register in some cases.  */
2080169689Skan      rtx temp = (!REG_P (structure_value_addr)
208190075Sobrien		  || (ACCUMULATE_OUTGOING_ARGS
208290075Sobrien		      && stack_arg_under_construction
208318334Speter		      && structure_value_addr == virtual_outgoing_args_rtx)
2084169689Skan		  ? copy_addr_to_reg (convert_memory_address
2085132718Skan				      (Pmode, structure_value_addr))
208618334Speter		  : structure_value_addr);
208718334Speter
208818334Speter      actparms
208918334Speter	= tree_cons (error_mark_node,
209018334Speter		     make_tree (build_pointer_type (TREE_TYPE (funtype)),
209118334Speter				temp),
209218334Speter		     actparms);
209318334Speter      structure_value_addr_parm = 1;
209418334Speter    }
209518334Speter
209618334Speter  /* Count the arguments and set NUM_ACTUALS.  */
209790075Sobrien  for (p = actparms, num_actuals = 0; p; p = TREE_CHAIN (p))
209890075Sobrien    num_actuals++;
209918334Speter
210018334Speter  /* Compute number of named args.
2101132718Skan     First, do a raw count of the args for INIT_CUMULATIVE_ARGS.  */
210218334Speter
2103132718Skan  if (type_arg_types != 0)
210418334Speter    n_named_args
2105132718Skan      = (list_length (type_arg_types)
210618334Speter	 /* Count the struct value address, if it is passed as a parm.  */
210718334Speter	 + structure_value_addr_parm);
210818334Speter  else
210918334Speter    /* If we know nothing, treat all args as named.  */
211018334Speter    n_named_args = num_actuals;
211118334Speter
211290075Sobrien  /* Start updating where the next arg would go.
211390075Sobrien
211490075Sobrien     On some machines (such as the PA) indirect calls have a different
2115132718Skan     calling convention than normal calls.  The fourth argument in
211690075Sobrien     INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
211790075Sobrien     or not.  */
2118132718Skan  INIT_CUMULATIVE_ARGS (args_so_far, funtype, NULL_RTX, fndecl, n_named_args);
211990075Sobrien
2120132718Skan  /* Now possibly adjust the number of named args.
2121132718Skan     Normally, don't include the last named arg if anonymous args follow.
2122132718Skan     We do include the last named arg if
2123132718Skan     targetm.calls.strict_argument_naming() returns nonzero.
2124132718Skan     (If no anonymous args follow, the result of list_length is actually
2125132718Skan     one too large.  This is harmless.)
2126132718Skan
2127132718Skan     If targetm.calls.pretend_outgoing_varargs_named() returns
2128132718Skan     nonzero, and targetm.calls.strict_argument_naming() returns zero,
2129132718Skan     this machine will be able to place unnamed args that were passed
2130132718Skan     in registers into the stack.  So treat all args as named.  This
2131132718Skan     allows the insns emitting for a specific argument list to be
2132132718Skan     independent of the function declaration.
2133132718Skan
2134132718Skan     If targetm.calls.pretend_outgoing_varargs_named() returns zero,
2135132718Skan     we do not have any reliable way to pass unnamed args in
2136132718Skan     registers, so we must force them into memory.  */
2137132718Skan
2138132718Skan  if (type_arg_types != 0
2139132718Skan      && targetm.calls.strict_argument_naming (&args_so_far))
2140132718Skan    ;
2141132718Skan  else if (type_arg_types != 0
2142132718Skan	   && ! targetm.calls.pretend_outgoing_varargs_named (&args_so_far))
2143132718Skan    /* Don't include the last named arg.  */
2144132718Skan    --n_named_args;
2145132718Skan  else
2146132718Skan    /* Treat all args as named.  */
2147132718Skan    n_named_args = num_actuals;
2148132718Skan
214918334Speter  /* Make a vector to hold all the information about each arg.  */
2150132718Skan  args = alloca (num_actuals * sizeof (struct arg_data));
2151132718Skan  memset (args, 0, num_actuals * sizeof (struct arg_data));
215218334Speter
215390075Sobrien  /* Build up entries in the ARGS array, compute the size of the
215490075Sobrien     arguments into ARGS_SIZE, etc.  */
215590075Sobrien  initialize_argument_information (num_actuals, args, &args_size,
215690075Sobrien				   n_named_args, actparms, fndecl,
215790075Sobrien				   &args_so_far, reg_parm_stack_space,
215890075Sobrien				   &old_stack_level, &old_pending_adj,
2159132718Skan				   &must_preallocate, &flags,
2160169689Skan				   &try_tail_call, CALL_FROM_THUNK_P (exp));
216118334Speter
216218334Speter  if (args_size.var)
216318334Speter    {
216490075Sobrien      /* If this function requires a variable-sized argument list, don't
216590075Sobrien	 try to make a cse'able block for this call.  We may be able to
216690075Sobrien	 do this eventually, but it is too complicated to keep track of
216790075Sobrien	 what insns go in the cse'able block and which don't.  */
216818334Speter
216990075Sobrien      flags &= ~ECF_LIBCALL_BLOCK;
217018334Speter      must_preallocate = 1;
217118334Speter    }
217218334Speter
217352284Sobrien  /* Now make final decision about preallocating stack space.  */
217452284Sobrien  must_preallocate = finalize_must_preallocate (must_preallocate,
217590075Sobrien						num_actuals, args,
217690075Sobrien						&args_size);
217750397Sobrien
217890075Sobrien  /* If the structure value address will reference the stack pointer, we
217990075Sobrien     must stabilize it.  We don't need to do this if we know that we are
218090075Sobrien     not going to adjust the stack pointer in processing this call.  */
218118334Speter
218218334Speter  if (structure_value_addr
218318334Speter      && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
218490075Sobrien	  || reg_mentioned_p (virtual_outgoing_args_rtx,
218590075Sobrien			      structure_value_addr))
218618334Speter      && (args_size.var
218790075Sobrien	  || (!ACCUMULATE_OUTGOING_ARGS && args_size.constant)))
218818334Speter    structure_value_addr = copy_to_reg (structure_value_addr);
218918334Speter
2190169689Skan  /* Tail calls can make things harder to debug, and we've traditionally
219190075Sobrien     pushed these optimizations into -O2.  Don't try if we're already
219290075Sobrien     expanding a call, as that means we're an argument.  Don't try if
2193169689Skan     there's cleanups, as we know there's code to follow the call.  */
219418334Speter
219590075Sobrien  if (currently_expanding_call++ != 0
219690075Sobrien      || !flag_optimize_sibling_calls
2197169689Skan      || args_size.var
2198169689Skan      || lookup_stmt_eh_region (exp) >= 0)
2199169689Skan    try_tail_call = 0;
220018334Speter
220190075Sobrien  /*  Rest of purposes for tail call optimizations to fail.  */
220290075Sobrien  if (
220390075Sobrien#ifdef HAVE_sibcall_epilogue
220490075Sobrien      !HAVE_sibcall_epilogue
220590075Sobrien#else
220690075Sobrien      1
220718334Speter#endif
220890075Sobrien      || !try_tail_call
220990075Sobrien      /* Doing sibling call optimization needs some work, since
221090075Sobrien	 structure_value_addr can be allocated on the stack.
221190075Sobrien	 It does not seem worth the effort since few optimizable
221290075Sobrien	 sibling calls will return a structure.  */
221390075Sobrien      || structure_value_addr != NULL_RTX
2214132718Skan      /* Check whether the target is able to optimize the call
2215132718Skan	 into a sibcall.  */
2216169689Skan      || !targetm.function_ok_for_sibcall (fndecl, exp)
2217132718Skan      /* Functions that do not return exactly once may not be sibcall
2218169689Skan	 optimized.  */
2219169689Skan      || (flags & (ECF_RETURNS_TWICE | ECF_NORETURN))
2220132718Skan      || TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (addr)))
2221132718Skan      /* If the called function is nested in the current one, it might access
2222169689Skan	 some of the caller's arguments, but could clobber them beforehand if
2223169689Skan	 the argument areas are shared.  */
2224132718Skan      || (fndecl && decl_function_context (fndecl) == current_function_decl)
222590075Sobrien      /* If this function requires more stack slots than the current
2226169689Skan	 function, we cannot change it into a sibling call.
2227169689Skan	 current_function_pretend_args_size is not part of the
2228169689Skan	 stack allocated by our caller.  */
2229169689Skan      || args_size.constant > (current_function_args_size
2230169689Skan			       - current_function_pretend_args_size)
223190075Sobrien      /* If the callee pops its own arguments, then it must pop exactly
223290075Sobrien	 the same number of arguments as the current function.  */
2233117395Skan      || (RETURN_POPS_ARGS (fndecl, funtype, args_size.constant)
2234117395Skan	  != RETURN_POPS_ARGS (current_function_decl,
2235117395Skan			       TREE_TYPE (current_function_decl),
2236117395Skan			       current_function_args_size))
2237169689Skan      || !lang_hooks.decls.ok_for_sibcall (fndecl))
2238117395Skan    try_tail_call = 0;
223990075Sobrien
224090075Sobrien  /* Ensure current function's preferred stack boundary is at least
224190075Sobrien     what we need.  We don't have to increase alignment for recursive
224290075Sobrien     functions.  */
224390075Sobrien  if (cfun->preferred_stack_boundary < preferred_stack_boundary
224490075Sobrien      && fndecl != current_function_decl)
224590075Sobrien    cfun->preferred_stack_boundary = preferred_stack_boundary;
2246132718Skan  if (fndecl == current_function_decl)
2247132718Skan    cfun->recursive_call_emit = true;
224818334Speter
224990075Sobrien  preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
225018334Speter
225190075Sobrien  /* We want to make two insn chains; one for a sibling call, the other
225290075Sobrien     for a normal call.  We will select one of the two chains after
225390075Sobrien     initial RTL generation is complete.  */
2254132718Skan  for (pass = try_tail_call ? 0 : 1; pass < 2; pass++)
225518334Speter    {
225690075Sobrien      int sibcall_failure = 0;
225790075Sobrien      /* We want to emit any pending stack adjustments before the tail
225890075Sobrien	 recursion "call".  That way we know any adjustment after the tail
2259169689Skan	 recursion call can be ignored if we indeed use the tail
226090075Sobrien	 call expansion.  */
226190075Sobrien      int save_pending_stack_adjust = 0;
226290075Sobrien      int save_stack_pointer_delta = 0;
226390075Sobrien      rtx insns;
226490075Sobrien      rtx before_call, next_arg_reg;
226518334Speter
226690075Sobrien      if (pass == 0)
226790075Sobrien	{
226890075Sobrien	  /* State variables we need to save and restore between
226990075Sobrien	     iterations.  */
227090075Sobrien	  save_pending_stack_adjust = pending_stack_adjust;
227190075Sobrien	  save_stack_pointer_delta = stack_pointer_delta;
227290075Sobrien	}
227390075Sobrien      if (pass)
227490075Sobrien	flags &= ~ECF_SIBCALL;
227590075Sobrien      else
227690075Sobrien	flags |= ECF_SIBCALL;
227718334Speter
227890075Sobrien      /* Other state variables that we must reinitialize each time
227990075Sobrien	 through the loop (that are not initialized by the loop itself).  */
228090075Sobrien      argblock = 0;
228190075Sobrien      call_fusage = 0;
228218334Speter
228390075Sobrien      /* Start a new sequence for the normal call case.
228418334Speter
228590075Sobrien	 From this point on, if the sibling call fails, we want to set
228690075Sobrien	 sibcall_failure instead of continuing the loop.  */
228790075Sobrien      start_sequence ();
228818334Speter
228990075Sobrien      /* Don't let pending stack adjusts add up to too much.
229090075Sobrien	 Also, do all pending adjustments now if there is any chance
229190075Sobrien	 this might be a call to alloca or if we are expanding a sibling
229290075Sobrien	 call sequence or if we are calling a function that is to return
2293146895Skan	 with stack pointer depressed.
2294146895Skan	 Also do the adjustments before a throwing call, otherwise
2295146895Skan	 exception handling can fail; PR 19225. */
229690075Sobrien      if (pending_stack_adjust >= 32
229790075Sobrien	  || (pending_stack_adjust > 0
229890075Sobrien	      && (flags & (ECF_MAY_BE_ALLOCA | ECF_SP_DEPRESSED)))
2299146895Skan	  || (pending_stack_adjust > 0
2300146895Skan	      && flag_exceptions && !(flags & ECF_NOTHROW))
230190075Sobrien	  || pass == 0)
230290075Sobrien	do_pending_stack_adjust ();
230318334Speter
230490075Sobrien      /* When calling a const function, we must pop the stack args right away,
230590075Sobrien	 so that the pop is deleted or moved with the call.  */
230690075Sobrien      if (pass && (flags & ECF_LIBCALL_BLOCK))
230790075Sobrien	NO_DEFER_POP;
230818334Speter
230990075Sobrien      /* Precompute any arguments as needed.  */
231090075Sobrien      if (pass)
231190075Sobrien	precompute_arguments (flags, num_actuals, args);
231250397Sobrien
231390075Sobrien      /* Now we are about to start emitting insns that can be deleted
231490075Sobrien	 if a libcall is deleted.  */
231590075Sobrien      if (pass && (flags & (ECF_LIBCALL_BLOCK | ECF_MALLOC)))
231690075Sobrien	start_sequence ();
231718334Speter
2318169689Skan      if (pass == 0 && cfun->stack_protect_guard)
2319169689Skan	stack_protect_epilogue ();
2320169689Skan
232190075Sobrien      adjusted_args_size = args_size;
232290075Sobrien      /* Compute the actual size of the argument block required.  The variable
232390075Sobrien	 and constant sizes must be combined, the size may have to be rounded,
232490075Sobrien	 and there may be a minimum required size.  When generating a sibcall
232590075Sobrien	 pattern, do not round up, since we'll be re-using whatever space our
232690075Sobrien	 caller provided.  */
232790075Sobrien      unadjusted_args_size
232890075Sobrien	= compute_argument_block_size (reg_parm_stack_space,
232990075Sobrien				       &adjusted_args_size,
233090075Sobrien				       (pass == 0 ? 0
233190075Sobrien					: preferred_stack_boundary));
233218334Speter
233390075Sobrien      old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
233418334Speter
233590075Sobrien      /* The argument block when performing a sibling call is the
2336169689Skan	 incoming argument block.  */
233790075Sobrien      if (pass == 0)
233890075Sobrien	{
233990075Sobrien	  argblock = virtual_incoming_args_rtx;
2340107590Sobrien	  argblock
2341107590Sobrien#ifdef STACK_GROWS_DOWNWARD
2342107590Sobrien	    = plus_constant (argblock, current_function_pretend_args_size);
2343107590Sobrien#else
2344107590Sobrien	    = plus_constant (argblock, -current_function_pretend_args_size);
2345107590Sobrien#endif
234690075Sobrien	  stored_args_map = sbitmap_alloc (args_size.constant);
234790075Sobrien	  sbitmap_zero (stored_args_map);
234890075Sobrien	}
234918334Speter
235090075Sobrien      /* If we have no actual push instructions, or shouldn't use them,
235190075Sobrien	 make space for all args right now.  */
235290075Sobrien      else if (adjusted_args_size.var != 0)
235350397Sobrien	{
235490075Sobrien	  if (old_stack_level == 0)
235590075Sobrien	    {
235690075Sobrien	      emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
2357132718Skan	      old_stack_pointer_delta = stack_pointer_delta;
235890075Sobrien	      old_pending_adj = pending_stack_adjust;
235990075Sobrien	      pending_stack_adjust = 0;
236090075Sobrien	      /* stack_arg_under_construction says whether a stack arg is
236190075Sobrien		 being constructed at the old stack level.  Pushing the stack
236290075Sobrien		 gets a clean outgoing argument block.  */
236390075Sobrien	      old_stack_arg_under_construction = stack_arg_under_construction;
236490075Sobrien	      stack_arg_under_construction = 0;
236590075Sobrien	    }
236690075Sobrien	  argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
236750397Sobrien	}
236890075Sobrien      else
236990075Sobrien	{
237090075Sobrien	  /* Note that we must go through the motions of allocating an argument
237190075Sobrien	     block even if the size is zero because we may be storing args
237290075Sobrien	     in the area reserved for register arguments, which may be part of
237390075Sobrien	     the stack frame.  */
237450397Sobrien
237590075Sobrien	  int needed = adjusted_args_size.constant;
237618334Speter
237790075Sobrien	  /* Store the maximum argument space used.  It will be pushed by
237890075Sobrien	     the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
237990075Sobrien	     checking).  */
238018334Speter
238190075Sobrien	  if (needed > current_function_outgoing_args_size)
238290075Sobrien	    current_function_outgoing_args_size = needed;
238318334Speter
238490075Sobrien	  if (must_preallocate)
238590075Sobrien	    {
238690075Sobrien	      if (ACCUMULATE_OUTGOING_ARGS)
238790075Sobrien		{
238890075Sobrien		  /* Since the stack pointer will never be pushed, it is
238990075Sobrien		     possible for the evaluation of a parm to clobber
239090075Sobrien		     something we have already written to the stack.
239190075Sobrien		     Since most function calls on RISC machines do not use
239290075Sobrien		     the stack, this is uncommon, but must work correctly.
239318334Speter
239490075Sobrien		     Therefore, we save any area of the stack that was already
239590075Sobrien		     written and that we are using.  Here we set up to do this
239690075Sobrien		     by making a new stack usage map from the old one.  The
239790075Sobrien		     actual save will be done by store_one_arg.
239818334Speter
239990075Sobrien		     Another approach might be to try to reorder the argument
240090075Sobrien		     evaluations to avoid this conflicting stack usage.  */
240150397Sobrien
240290075Sobrien#ifndef OUTGOING_REG_PARM_STACK_SPACE
240390075Sobrien		  /* Since we will be writing into the entire argument area,
240490075Sobrien		     the map must be allocated for its entire size, not just
240590075Sobrien		     the part that is the responsibility of the caller.  */
240690075Sobrien		  needed += reg_parm_stack_space;
240790075Sobrien#endif
240850397Sobrien
240990075Sobrien#ifdef ARGS_GROW_DOWNWARD
241090075Sobrien		  highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
241190075Sobrien						     needed + 1);
241290075Sobrien#else
241390075Sobrien		  highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
241490075Sobrien						     needed);
241590075Sobrien#endif
2416169689Skan		  if (stack_usage_map_buf)
2417169689Skan		    free (stack_usage_map_buf);
2418169689Skan		  stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
2419169689Skan		  stack_usage_map = stack_usage_map_buf;
242050397Sobrien
242190075Sobrien		  if (initial_highest_arg_in_use)
242290075Sobrien		    memcpy (stack_usage_map, initial_stack_usage_map,
242390075Sobrien			    initial_highest_arg_in_use);
242450397Sobrien
242590075Sobrien		  if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
242690075Sobrien		    memset (&stack_usage_map[initial_highest_arg_in_use], 0,
242790075Sobrien			   (highest_outgoing_arg_in_use
242890075Sobrien			    - initial_highest_arg_in_use));
242990075Sobrien		  needed = 0;
243018334Speter
243190075Sobrien		  /* The address of the outgoing argument list must not be
243290075Sobrien		     copied to a register here, because argblock would be left
243390075Sobrien		     pointing to the wrong place after the call to
243490075Sobrien		     allocate_dynamic_stack_space below.  */
243518334Speter
243690075Sobrien		  argblock = virtual_outgoing_args_rtx;
243790075Sobrien		}
243890075Sobrien	      else
243990075Sobrien		{
244090075Sobrien		  if (inhibit_defer_pop == 0)
244190075Sobrien		    {
244290075Sobrien		      /* Try to reuse some or all of the pending_stack_adjust
244390075Sobrien			 to get this space.  */
244490075Sobrien		      needed
244590075Sobrien			= (combine_pending_stack_adjustment_and_call
244690075Sobrien			   (unadjusted_args_size,
244790075Sobrien			    &adjusted_args_size,
244890075Sobrien			    preferred_unit_stack_boundary));
244918334Speter
245090075Sobrien		      /* combine_pending_stack_adjustment_and_call computes
245190075Sobrien			 an adjustment before the arguments are allocated.
245290075Sobrien			 Account for them and see whether or not the stack
245390075Sobrien			 needs to go up or down.  */
245490075Sobrien		      needed = unadjusted_args_size - needed;
245518334Speter
245690075Sobrien		      if (needed < 0)
245790075Sobrien			{
245890075Sobrien			  /* We're releasing stack space.  */
245990075Sobrien			  /* ??? We can avoid any adjustment at all if we're
246090075Sobrien			     already aligned.  FIXME.  */
246190075Sobrien			  pending_stack_adjust = -needed;
246290075Sobrien			  do_pending_stack_adjust ();
246390075Sobrien			  needed = 0;
246490075Sobrien			}
246590075Sobrien		      else
246690075Sobrien			/* We need to allocate space.  We'll do that in
246790075Sobrien			   push_block below.  */
246890075Sobrien			pending_stack_adjust = 0;
246990075Sobrien		    }
247018334Speter
247190075Sobrien		  /* Special case this because overhead of `push_block' in
247290075Sobrien		     this case is non-trivial.  */
247390075Sobrien		  if (needed == 0)
247490075Sobrien		    argblock = virtual_outgoing_args_rtx;
247590075Sobrien		  else
2476132718Skan		    {
2477132718Skan		      argblock = push_block (GEN_INT (needed), 0, 0);
2478132718Skan#ifdef ARGS_GROW_DOWNWARD
2479132718Skan		      argblock = plus_constant (argblock, needed);
2480132718Skan#endif
2481132718Skan		    }
248218334Speter
248390075Sobrien		  /* We only really need to call `copy_to_reg' in the case
248490075Sobrien		     where push insns are going to be used to pass ARGBLOCK
248590075Sobrien		     to a function call in ARGS.  In that case, the stack
248690075Sobrien		     pointer changes value from the allocation point to the
248790075Sobrien		     call point, and hence the value of
248890075Sobrien		     VIRTUAL_OUTGOING_ARGS_RTX changes as well.  But might
248990075Sobrien		     as well always do it.  */
249090075Sobrien		  argblock = copy_to_reg (argblock);
2491132718Skan		}
2492132718Skan	    }
2493132718Skan	}
249418334Speter
2495132718Skan      if (ACCUMULATE_OUTGOING_ARGS)
2496132718Skan	{
2497132718Skan	  /* The save/restore code in store_one_arg handles all
2498132718Skan	     cases except one: a constructor call (including a C
2499132718Skan	     function returning a BLKmode struct) to initialize
2500132718Skan	     an argument.  */
2501132718Skan	  if (stack_arg_under_construction)
2502132718Skan	    {
250390075Sobrien#ifndef OUTGOING_REG_PARM_STACK_SPACE
2504132718Skan	      rtx push_size = GEN_INT (reg_parm_stack_space
2505132718Skan				       + adjusted_args_size.constant);
250690075Sobrien#else
2507132718Skan	      rtx push_size = GEN_INT (adjusted_args_size.constant);
250890075Sobrien#endif
2509132718Skan	      if (old_stack_level == 0)
2510132718Skan		{
2511132718Skan		  emit_stack_save (SAVE_BLOCK, &old_stack_level,
2512132718Skan				   NULL_RTX);
2513132718Skan		  old_stack_pointer_delta = stack_pointer_delta;
2514132718Skan		  old_pending_adj = pending_stack_adjust;
2515132718Skan		  pending_stack_adjust = 0;
2516132718Skan		  /* stack_arg_under_construction says whether a stack
2517132718Skan		     arg is being constructed at the old stack level.
2518132718Skan		     Pushing the stack gets a clean outgoing argument
2519132718Skan		     block.  */
2520132718Skan		  old_stack_arg_under_construction
2521132718Skan		    = stack_arg_under_construction;
2522132718Skan		  stack_arg_under_construction = 0;
2523132718Skan		  /* Make a new map for the new argument list.  */
2524169689Skan		  if (stack_usage_map_buf)
2525169689Skan		    free (stack_usage_map_buf);
2526169689Skan		  stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
2527169689Skan		  stack_usage_map = stack_usage_map_buf;
2528132718Skan		  memset (stack_usage_map, 0, highest_outgoing_arg_in_use);
2529132718Skan		  highest_outgoing_arg_in_use = 0;
253090075Sobrien		}
2531132718Skan	      allocate_dynamic_stack_space (push_size, NULL_RTX,
2532132718Skan					    BITS_PER_UNIT);
253390075Sobrien	    }
2534132718Skan
2535132718Skan	  /* If argument evaluation might modify the stack pointer,
2536132718Skan	     copy the address of the argument list to a register.  */
2537132718Skan	  for (i = 0; i < num_actuals; i++)
2538132718Skan	    if (args[i].pass_on_stack)
2539132718Skan	      {
2540132718Skan		argblock = copy_addr_to_reg (argblock);
2541132718Skan		break;
2542132718Skan	      }
254318334Speter	}
254450397Sobrien
254590075Sobrien      compute_argument_addresses (args, argblock, num_actuals);
254690075Sobrien
254790075Sobrien      /* If we push args individually in reverse order, perform stack alignment
254890075Sobrien	 before the first push (the last arg).  */
254990075Sobrien      if (PUSH_ARGS_REVERSED && argblock == 0
255090075Sobrien	  && adjusted_args_size.constant != unadjusted_args_size)
255118334Speter	{
255290075Sobrien	  /* When the stack adjustment is pending, we get better code
255390075Sobrien	     by combining the adjustments.  */
255490075Sobrien	  if (pending_stack_adjust
255590075Sobrien	      && ! (flags & ECF_LIBCALL_BLOCK)
255690075Sobrien	      && ! inhibit_defer_pop)
255790075Sobrien	    {
255890075Sobrien	      pending_stack_adjust
255990075Sobrien		= (combine_pending_stack_adjustment_and_call
256090075Sobrien		   (unadjusted_args_size,
256190075Sobrien		    &adjusted_args_size,
256290075Sobrien		    preferred_unit_stack_boundary));
256390075Sobrien	      do_pending_stack_adjust ();
256490075Sobrien	    }
256590075Sobrien	  else if (argblock == 0)
256690075Sobrien	    anti_adjust_stack (GEN_INT (adjusted_args_size.constant
256790075Sobrien					- unadjusted_args_size));
256818334Speter	}
256990075Sobrien      /* Now that the stack is properly aligned, pops can't safely
257090075Sobrien	 be deferred during the evaluation of the arguments.  */
257190075Sobrien      NO_DEFER_POP;
257218334Speter
2573117395Skan      funexp = rtx_for_function_call (fndecl, addr);
257418334Speter
257590075Sobrien      /* Figure out the register where the value, if any, will come back.  */
257690075Sobrien      valreg = 0;
257790075Sobrien      if (TYPE_MODE (TREE_TYPE (exp)) != VOIDmode
257890075Sobrien	  && ! structure_value_addr)
257990075Sobrien	{
258090075Sobrien	  if (pcc_struct_value)
258190075Sobrien	    valreg = hard_function_value (build_pointer_type (TREE_TYPE (exp)),
2582169689Skan					  fndecl, NULL, (pass == 0));
258390075Sobrien	  else
2584169689Skan	    valreg = hard_function_value (TREE_TYPE (exp), fndecl, fntype,
2585169689Skan					  (pass == 0));
258690075Sobrien	}
258718334Speter
258890075Sobrien      /* Precompute all register parameters.  It isn't safe to compute anything
258990075Sobrien	 once we have started filling any specific hard regs.  */
259090075Sobrien      precompute_register_parameters (num_actuals, args, &reg_parm_seen);
259118334Speter
2592169689Skan      if (TREE_OPERAND (exp, 2))
2593169689Skan	static_chain_value = expand_normal (TREE_OPERAND (exp, 2));
2594169689Skan      else
2595169689Skan	static_chain_value = 0;
2596169689Skan
259790075Sobrien#ifdef REG_PARM_STACK_SPACE
259890075Sobrien      /* Save the fixed argument area if it's part of the caller's frame and
259990075Sobrien	 is clobbered by argument setup for this call.  */
260090075Sobrien      if (ACCUMULATE_OUTGOING_ARGS && pass)
260190075Sobrien	save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
260290075Sobrien					      &low_to_save, &high_to_save);
260318334Speter#endif
260418334Speter
260590075Sobrien      /* Now store (and compute if necessary) all non-register parms.
260690075Sobrien	 These come before register parms, since they can require block-moves,
260790075Sobrien	 which could clobber the registers used for register parms.
260890075Sobrien	 Parms which have partial registers are not stored here,
260990075Sobrien	 but we do preallocate space here if they want that.  */
261018334Speter
261118334Speter      for (i = 0; i < num_actuals; i++)
261290075Sobrien	if (args[i].reg == 0 || args[i].pass_on_stack)
261318334Speter	  {
261490075Sobrien	    rtx before_arg = get_last_insn ();
261518334Speter
261690075Sobrien	    if (store_one_arg (&args[i], argblock, flags,
261790075Sobrien			       adjusted_args_size.var != 0,
261890075Sobrien			       reg_parm_stack_space)
261990075Sobrien		|| (pass == 0
262090075Sobrien		    && check_sibcall_argument_overlap (before_arg,
2621132718Skan						       &args[i], 1)))
262290075Sobrien	      sibcall_failure = 1;
2623132718Skan
2624132718Skan	    if (flags & ECF_CONST
2625132718Skan		&& args[i].stack
2626132718Skan		&& args[i].value == args[i].stack)
2627132718Skan	      call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
2628132718Skan					       gen_rtx_USE (VOIDmode,
2629132718Skan							    args[i].value),
2630132718Skan					       call_fusage);
263118334Speter	  }
263218334Speter
263390075Sobrien      /* If we have a parm that is passed in registers but not in memory
263490075Sobrien	 and whose alignment does not permit a direct copy into registers,
263590075Sobrien	 make a group of pseudos that correspond to each register that we
263690075Sobrien	 will later fill.  */
263790075Sobrien      if (STRICT_ALIGNMENT)
263890075Sobrien	store_unaligned_arguments_into_pseudos (args, num_actuals);
263918334Speter
264090075Sobrien      /* Now store any partially-in-registers parm.
264190075Sobrien	 This is the last place a block-move can happen.  */
264290075Sobrien      if (reg_parm_seen)
264390075Sobrien	for (i = 0; i < num_actuals; i++)
264490075Sobrien	  if (args[i].partial != 0 && ! args[i].pass_on_stack)
264590075Sobrien	    {
264690075Sobrien	      rtx before_arg = get_last_insn ();
264718334Speter
264890075Sobrien	      if (store_one_arg (&args[i], argblock, flags,
264990075Sobrien				 adjusted_args_size.var != 0,
265090075Sobrien				 reg_parm_stack_space)
265190075Sobrien		  || (pass == 0
265290075Sobrien		      && check_sibcall_argument_overlap (before_arg,
2653132718Skan							 &args[i], 1)))
265490075Sobrien		sibcall_failure = 1;
265590075Sobrien	    }
265618334Speter
265790075Sobrien      /* If we pushed args in forward order, perform stack alignment
265890075Sobrien	 after pushing the last arg.  */
265990075Sobrien      if (!PUSH_ARGS_REVERSED && argblock == 0)
266090075Sobrien	anti_adjust_stack (GEN_INT (adjusted_args_size.constant
266190075Sobrien				    - unadjusted_args_size));
266218334Speter
266390075Sobrien      /* If register arguments require space on the stack and stack space
266490075Sobrien	 was not preallocated, allocate stack space here for arguments
266590075Sobrien	 passed in registers.  */
266690075Sobrien#ifdef OUTGOING_REG_PARM_STACK_SPACE
266790075Sobrien      if (!ACCUMULATE_OUTGOING_ARGS
266890075Sobrien	  && must_preallocate == 0 && reg_parm_stack_space > 0)
266990075Sobrien	anti_adjust_stack (GEN_INT (reg_parm_stack_space));
267090075Sobrien#endif
267152284Sobrien
267290075Sobrien      /* Pass the function the address in which to return a
267390075Sobrien	 structure value.  */
267490075Sobrien      if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
267590075Sobrien	{
2676169689Skan	  structure_value_addr
2677132718Skan	    = convert_memory_address (Pmode, structure_value_addr);
2678132718Skan	  emit_move_insn (struct_value,
267990075Sobrien			  force_reg (Pmode,
268090075Sobrien				     force_operand (structure_value_addr,
268190075Sobrien						    NULL_RTX)));
268218334Speter
2683169689Skan	  if (REG_P (struct_value))
2684132718Skan	    use_reg (&call_fusage, struct_value);
268590075Sobrien	}
268618334Speter
2687169689Skan      funexp = prepare_call_address (funexp, static_chain_value,
2688169689Skan				     &call_fusage, reg_parm_seen, pass == 0);
268918334Speter
2690132718Skan      load_register_parameters (args, num_actuals, &call_fusage, flags,
2691132718Skan				pass == 0, &sibcall_failure);
269218334Speter
269390075Sobrien      /* Save a pointer to the last insn before the call, so that we can
269490075Sobrien	 later safely search backwards to find the CALL_INSN.  */
269590075Sobrien      before_call = get_last_insn ();
269650397Sobrien
269790075Sobrien      /* Set up next argument register.  For sibling calls on machines
269890075Sobrien	 with register windows this should be the incoming register.  */
269990075Sobrien#ifdef FUNCTION_INCOMING_ARG
270090075Sobrien      if (pass == 0)
270190075Sobrien	next_arg_reg = FUNCTION_INCOMING_ARG (args_so_far, VOIDmode,
270290075Sobrien					      void_type_node, 1);
270390075Sobrien      else
270450397Sobrien#endif
270590075Sobrien	next_arg_reg = FUNCTION_ARG (args_so_far, VOIDmode,
270690075Sobrien				     void_type_node, 1);
270750397Sobrien
270890075Sobrien      /* All arguments and registers used for the call must be set up by
270990075Sobrien	 now!  */
271018334Speter
271190075Sobrien      /* Stack must be properly aligned now.  */
2712169689Skan      gcc_assert (!pass
2713169689Skan		  || !(stack_pointer_delta % preferred_unit_stack_boundary));
271418334Speter
271590075Sobrien      /* Generate the actual call instruction.  */
2716169689Skan      emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
271790075Sobrien		   adjusted_args_size.constant, struct_value_size,
271890075Sobrien		   next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
271996263Sobrien		   flags, & args_so_far);
272018334Speter
2721169689Skan      /* If a non-BLKmode value is returned at the most significant end
2722169689Skan	 of a register, shift the register right by the appropriate amount
2723169689Skan	 and update VALREG accordingly.  BLKmode values are handled by the
2724169689Skan	 group load/store machinery below.  */
2725169689Skan      if (!structure_value_addr
2726169689Skan	  && !pcc_struct_value
2727169689Skan	  && TYPE_MODE (TREE_TYPE (exp)) != BLKmode
2728169689Skan	  && targetm.calls.return_in_msb (TREE_TYPE (exp)))
2729169689Skan	{
2730169689Skan	  if (shift_return_value (TYPE_MODE (TREE_TYPE (exp)), false, valreg))
2731169689Skan	    sibcall_failure = 1;
2732169689Skan	  valreg = gen_rtx_REG (TYPE_MODE (TREE_TYPE (exp)), REGNO (valreg));
2733169689Skan	}
2734169689Skan
273590075Sobrien      /* If call is cse'able, make appropriate pair of reg-notes around it.
273690075Sobrien	 Test valreg so we don't crash; may safely ignore `const'
273790075Sobrien	 if return type is void.  Disable for PARALLEL return values, because
273890075Sobrien	 we have no way to move such values into a pseudo register.  */
273990075Sobrien      if (pass && (flags & ECF_LIBCALL_BLOCK))
274090075Sobrien	{
274190075Sobrien	  rtx insns;
2742132718Skan	  rtx insn;
2743132718Skan	  bool failed = valreg == 0 || GET_CODE (valreg) == PARALLEL;
274418334Speter
2745169689Skan	  insns = get_insns ();
2746132718Skan
2747132718Skan	  /* Expansion of block moves possibly introduced a loop that may
2748132718Skan	     not appear inside libcall block.  */
2749132718Skan	  for (insn = insns; insn; insn = NEXT_INSN (insn))
2750169689Skan	    if (JUMP_P (insn))
2751132718Skan	      failed = true;
2752132718Skan
2753132718Skan	  if (failed)
275490075Sobrien	    {
275590075Sobrien	      end_sequence ();
2756117395Skan	      emit_insn (insns);
275790075Sobrien	    }
275890075Sobrien	  else
275990075Sobrien	    {
276090075Sobrien	      rtx note = 0;
276190075Sobrien	      rtx temp = gen_reg_rtx (GET_MODE (valreg));
276218334Speter
276390075Sobrien	      /* Mark the return value as a pointer if needed.  */
276490075Sobrien	      if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
276590075Sobrien		mark_reg_pointer (temp,
276690075Sobrien				  TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp))));
276718334Speter
276890075Sobrien	      end_sequence ();
2769132718Skan	      if (flag_unsafe_math_optimizations
2770132718Skan		  && fndecl
2771169689Skan		  && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
2772132718Skan		  && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRT
2773132718Skan		      || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRTF
2774132718Skan		      || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRTL))
2775169689Skan		note = gen_rtx_fmt_e (SQRT,
2776169689Skan				      GET_MODE (temp),
2777132718Skan				      args[0].initial_value);
2778132718Skan	      else
2779132718Skan		{
2780132718Skan		  /* Construct an "equal form" for the value which
2781132718Skan		     mentions all the arguments in order as well as
2782132718Skan		     the function name.  */
2783132718Skan		  for (i = 0; i < num_actuals; i++)
2784132718Skan		    note = gen_rtx_EXPR_LIST (VOIDmode,
2785132718Skan					      args[i].initial_value, note);
2786132718Skan		  note = gen_rtx_EXPR_LIST (VOIDmode, funexp, note);
2787169689Skan
2788132718Skan		  if (flags & ECF_PURE)
2789132718Skan		    note = gen_rtx_EXPR_LIST (VOIDmode,
279090075Sobrien			gen_rtx_USE (VOIDmode,
279190075Sobrien				     gen_rtx_MEM (BLKmode,
279290075Sobrien						  gen_rtx_SCRATCH (VOIDmode))),
279390075Sobrien			note);
2794132718Skan		}
279590075Sobrien	      emit_libcall_block (insns, temp, valreg, note);
279618334Speter
279790075Sobrien	      valreg = temp;
279890075Sobrien	    }
279990075Sobrien	}
280090075Sobrien      else if (pass && (flags & ECF_MALLOC))
280190075Sobrien	{
280290075Sobrien	  rtx temp = gen_reg_rtx (GET_MODE (valreg));
280390075Sobrien	  rtx last, insns;
280418334Speter
280590075Sobrien	  /* The return value from a malloc-like function is a pointer.  */
280690075Sobrien	  if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
280790075Sobrien	    mark_reg_pointer (temp, BIGGEST_ALIGNMENT);
280818334Speter
280990075Sobrien	  emit_move_insn (temp, valreg);
281018334Speter
281190075Sobrien	  /* The return value from a malloc-like function can not alias
281290075Sobrien	     anything else.  */
281390075Sobrien	  last = get_last_insn ();
281490075Sobrien	  REG_NOTES (last) =
281590075Sobrien	    gen_rtx_EXPR_LIST (REG_NOALIAS, temp, REG_NOTES (last));
281618334Speter
281790075Sobrien	  /* Write out the sequence.  */
281890075Sobrien	  insns = get_insns ();
281990075Sobrien	  end_sequence ();
2820117395Skan	  emit_insn (insns);
282190075Sobrien	  valreg = temp;
282218334Speter	}
282318334Speter
282490075Sobrien      /* For calls to `setjmp', etc., inform flow.c it should complain
282590075Sobrien	 if nonvolatile values are live.  For functions that cannot return,
282690075Sobrien	 inform flow that control does not fall through.  */
282718334Speter
2828169689Skan      if ((flags & ECF_NORETURN) || pass == 0)
282990075Sobrien	{
283090075Sobrien	  /* The barrier must be emitted
283190075Sobrien	     immediately after the CALL_INSN.  Some ports emit more
283290075Sobrien	     than just a CALL_INSN above, so we must search for it here.  */
283318334Speter
283490075Sobrien	  rtx last = get_last_insn ();
2835169689Skan	  while (!CALL_P (last))
283690075Sobrien	    {
283790075Sobrien	      last = PREV_INSN (last);
283890075Sobrien	      /* There was no CALL_INSN?  */
2839169689Skan	      gcc_assert (last != before_call);
284090075Sobrien	    }
284118334Speter
284290075Sobrien	  emit_barrier_after (last);
2843122180Skan
2844132718Skan	  /* Stack adjustments after a noreturn call are dead code.
2845132718Skan	     However when NO_DEFER_POP is in effect, we must preserve
2846132718Skan	     stack_pointer_delta.  */
2847132718Skan	  if (inhibit_defer_pop == 0)
2848132718Skan	    {
2849132718Skan	      stack_pointer_delta = old_stack_allocated;
2850132718Skan	      pending_stack_adjust = 0;
2851132718Skan	    }
285290075Sobrien	}
285318334Speter
285490075Sobrien      /* If value type not void, return an rtx for the value.  */
285518334Speter
285690075Sobrien      if (TYPE_MODE (TREE_TYPE (exp)) == VOIDmode
285790075Sobrien	  || ignore)
285890075Sobrien	target = const0_rtx;
285990075Sobrien      else if (structure_value_addr)
286090075Sobrien	{
2861169689Skan	  if (target == 0 || !MEM_P (target))
286290075Sobrien	    {
286390075Sobrien	      target
286490075Sobrien		= gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
286590075Sobrien			       memory_address (TYPE_MODE (TREE_TYPE (exp)),
286690075Sobrien					       structure_value_addr));
286790075Sobrien	      set_mem_attributes (target, exp, 1);
286890075Sobrien	    }
286990075Sobrien	}
287090075Sobrien      else if (pcc_struct_value)
287190075Sobrien	{
287290075Sobrien	  /* This is the special C++ case where we need to
287390075Sobrien	     know what the true target was.  We take care to
287490075Sobrien	     never use this value more than once in one expression.  */
287590075Sobrien	  target = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
287690075Sobrien				copy_to_reg (valreg));
287790075Sobrien	  set_mem_attributes (target, exp, 1);
287890075Sobrien	}
287990075Sobrien      /* Handle calls that return values in multiple non-contiguous locations.
288090075Sobrien	 The Irix 6 ABI has examples of this.  */
288190075Sobrien      else if (GET_CODE (valreg) == PARALLEL)
288290075Sobrien	{
2883169689Skan	  if (target == 0)
288490075Sobrien	    {
288590075Sobrien	      /* This will only be assigned once, so it can be readonly.  */
288690075Sobrien	      tree nt = build_qualified_type (TREE_TYPE (exp),
288790075Sobrien					      (TYPE_QUALS (TREE_TYPE (exp))
288890075Sobrien					       | TYPE_QUAL_CONST));
288918334Speter
289090075Sobrien	      target = assign_temp (nt, 0, 1, 1);
289190075Sobrien	    }
289250397Sobrien
289390075Sobrien	  if (! rtx_equal_p (target, valreg))
2894132718Skan	    emit_group_store (target, valreg, TREE_TYPE (exp),
289590075Sobrien			      int_size_in_bytes (TREE_TYPE (exp)));
289618334Speter
289790075Sobrien	  /* We can not support sibling calls for this case.  */
289890075Sobrien	  sibcall_failure = 1;
289990075Sobrien	}
290090075Sobrien      else if (target
290190075Sobrien	       && GET_MODE (target) == TYPE_MODE (TREE_TYPE (exp))
290290075Sobrien	       && GET_MODE (target) == GET_MODE (valreg))
290390075Sobrien	{
2904169689Skan	  bool may_overlap = false;
290518334Speter
2906169689Skan	  /* We have to copy a return value in a CLASS_LIKELY_SPILLED hard
2907169689Skan	     reg to a plain register.  */
2908169689Skan	  if (REG_P (valreg)
2909169689Skan	      && HARD_REGISTER_P (valreg)
2910169689Skan	      && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (valreg)))
2911169689Skan	      && !(REG_P (target) && !HARD_REGISTER_P (target)))
2912169689Skan	    valreg = copy_to_reg (valreg);
2913132718Skan
2914169689Skan	  /* If TARGET is a MEM in the argument area, and we have
2915169689Skan	     saved part of the argument area, then we can't store
2916169689Skan	     directly into TARGET as it may get overwritten when we
2917169689Skan	     restore the argument save area below.  Don't work too
2918169689Skan	     hard though and simply force TARGET to a register if it
2919169689Skan	     is a MEM; the optimizer is quite likely to sort it out.  */
2920169689Skan	  if (ACCUMULATE_OUTGOING_ARGS && pass && MEM_P (target))
2921169689Skan	    for (i = 0; i < num_actuals; i++)
2922169689Skan	      if (args[i].save_area)
2923169689Skan		{
2924169689Skan		  may_overlap = true;
2925169689Skan		  break;
2926169689Skan		}
2927169689Skan
2928169689Skan	  if (may_overlap)
2929169689Skan	    target = copy_to_reg (valreg);
2930169689Skan	  else
2931169689Skan	    {
2932169689Skan	      /* TARGET and VALREG cannot be equal at this point
2933169689Skan		 because the latter would not have
2934169689Skan		 REG_FUNCTION_VALUE_P true, while the former would if
2935169689Skan		 it were referring to the same register.
2936169689Skan
2937169689Skan		 If they refer to the same register, this move will be
2938169689Skan		 a no-op, except when function inlining is being
2939169689Skan		 done.  */
2940169689Skan	      emit_move_insn (target, valreg);
2941169689Skan
2942169689Skan	      /* If we are setting a MEM, this code must be executed.
2943169689Skan		 Since it is emitted after the call insn, sibcall
2944169689Skan		 optimization cannot be performed in that case.  */
2945169689Skan	      if (MEM_P (target))
2946169689Skan		sibcall_failure = 1;
2947169689Skan	    }
294890075Sobrien	}
294990075Sobrien      else if (TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
295090075Sobrien	{
295190075Sobrien	  target = copy_blkmode_from_reg (target, valreg, TREE_TYPE (exp));
295250397Sobrien
295390075Sobrien	  /* We can not support sibling calls for this case.  */
295490075Sobrien	  sibcall_failure = 1;
295590075Sobrien	}
295690075Sobrien      else
2957169689Skan	target = copy_to_reg (valreg);
295850397Sobrien
2959132718Skan      if (targetm.calls.promote_function_return(funtype))
2960132718Skan	{
2961169689Skan	  /* If we promoted this return value, make the proper SUBREG.
2962169689Skan	     TARGET might be const0_rtx here, so be careful.  */
2963169689Skan	  if (REG_P (target)
2964169689Skan	      && TYPE_MODE (TREE_TYPE (exp)) != BLKmode
2965169689Skan	      && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
2966169689Skan	    {
2967169689Skan	      tree type = TREE_TYPE (exp);
2968169689Skan	      int unsignedp = TYPE_UNSIGNED (type);
2969169689Skan	      int offset = 0;
2970169689Skan	      enum machine_mode pmode;
297150397Sobrien
2972169689Skan	      pmode = promote_mode (type, TYPE_MODE (type), &unsignedp, 1);
2973169689Skan	      /* If we don't promote as expected, something is wrong.  */
2974169689Skan	      gcc_assert (GET_MODE (target) == pmode);
297550397Sobrien
2976169689Skan	      if ((WORDS_BIG_ENDIAN || BYTES_BIG_ENDIAN)
2977169689Skan		  && (GET_MODE_SIZE (GET_MODE (target))
2978169689Skan		      > GET_MODE_SIZE (TYPE_MODE (type))))
2979169689Skan		{
2980169689Skan		  offset = GET_MODE_SIZE (GET_MODE (target))
2981169689Skan		    - GET_MODE_SIZE (TYPE_MODE (type));
2982169689Skan		  if (! BYTES_BIG_ENDIAN)
2983169689Skan		    offset = (offset / UNITS_PER_WORD) * UNITS_PER_WORD;
2984169689Skan		  else if (! WORDS_BIG_ENDIAN)
2985169689Skan		    offset %= UNITS_PER_WORD;
2986169689Skan		}
2987169689Skan	      target = gen_rtx_SUBREG (TYPE_MODE (type), target, offset);
2988169689Skan	      SUBREG_PROMOTED_VAR_P (target) = 1;
2989169689Skan	      SUBREG_PROMOTED_UNSIGNED_SET (target, unsignedp);
2990169689Skan	    }
299190075Sobrien	}
299218334Speter
299390075Sobrien      /* If size of args is variable or this was a constructor call for a stack
299490075Sobrien	 argument, restore saved stack-pointer value.  */
299550397Sobrien
299690075Sobrien      if (old_stack_level && ! (flags & ECF_SP_DEPRESSED))
299790075Sobrien	{
299890075Sobrien	  emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
2999132718Skan	  stack_pointer_delta = old_stack_pointer_delta;
300090075Sobrien	  pending_stack_adjust = old_pending_adj;
3001169689Skan	  old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
300290075Sobrien	  stack_arg_under_construction = old_stack_arg_under_construction;
300390075Sobrien	  highest_outgoing_arg_in_use = initial_highest_arg_in_use;
300490075Sobrien	  stack_usage_map = initial_stack_usage_map;
300590075Sobrien	  sibcall_failure = 1;
300690075Sobrien	}
300790075Sobrien      else if (ACCUMULATE_OUTGOING_ARGS && pass)
300890075Sobrien	{
300990075Sobrien#ifdef REG_PARM_STACK_SPACE
301090075Sobrien	  if (save_area)
3011132718Skan	    restore_fixed_argument_area (save_area, argblock,
3012132718Skan					 high_to_save, low_to_save);
301318334Speter#endif
301418334Speter
301590075Sobrien	  /* If we saved any argument areas, restore them.  */
301690075Sobrien	  for (i = 0; i < num_actuals; i++)
301790075Sobrien	    if (args[i].save_area)
301890075Sobrien	      {
301990075Sobrien		enum machine_mode save_mode = GET_MODE (args[i].save_area);
302090075Sobrien		rtx stack_area
302190075Sobrien		  = gen_rtx_MEM (save_mode,
302290075Sobrien				 memory_address (save_mode,
302390075Sobrien						 XEXP (args[i].stack_slot, 0)));
302418334Speter
302590075Sobrien		if (save_mode != BLKmode)
302690075Sobrien		  emit_move_insn (stack_area, args[i].save_area);
302790075Sobrien		else
3028117395Skan		  emit_block_move (stack_area, args[i].save_area,
3029132718Skan				   GEN_INT (args[i].locate.size.constant),
3030117395Skan				   BLOCK_OP_CALL_PARM);
303190075Sobrien	      }
303218334Speter
303390075Sobrien	  highest_outgoing_arg_in_use = initial_highest_arg_in_use;
303490075Sobrien	  stack_usage_map = initial_stack_usage_map;
303590075Sobrien	}
303650397Sobrien
303790075Sobrien      /* If this was alloca, record the new stack level for nonlocal gotos.
303890075Sobrien	 Check for the handler slots since we might not have a save area
303990075Sobrien	 for non-local gotos.  */
304050397Sobrien
3041169689Skan      if ((flags & ECF_MAY_BE_ALLOCA) && cfun->nonlocal_goto_save_area != 0)
3042169689Skan	update_nonlocal_goto_save_area ();
304350397Sobrien
304490075Sobrien      /* Free up storage we no longer need.  */
304590075Sobrien      for (i = 0; i < num_actuals; ++i)
304690075Sobrien	if (args[i].aligned_regs)
304790075Sobrien	  free (args[i].aligned_regs);
304850397Sobrien
304990075Sobrien      insns = get_insns ();
305090075Sobrien      end_sequence ();
305118334Speter
305290075Sobrien      if (pass == 0)
305350397Sobrien	{
305490075Sobrien	  tail_call_insns = insns;
305550397Sobrien
305690075Sobrien	  /* Restore the pending stack adjustment now that we have
305790075Sobrien	     finished generating the sibling call sequence.  */
305850397Sobrien
305990075Sobrien	  pending_stack_adjust = save_pending_stack_adjust;
306090075Sobrien	  stack_pointer_delta = save_stack_pointer_delta;
306150397Sobrien
306290075Sobrien	  /* Prepare arg structure for next iteration.  */
306390075Sobrien	  for (i = 0; i < num_actuals; i++)
306450397Sobrien	    {
306590075Sobrien	      args[i].value = 0;
306690075Sobrien	      args[i].aligned_regs = 0;
306790075Sobrien	      args[i].stack = 0;
306850397Sobrien	    }
306950397Sobrien
307090075Sobrien	  sbitmap_free (stored_args_map);
307190075Sobrien	}
307290075Sobrien      else
3073132718Skan	{
3074132718Skan	  normal_call_insns = insns;
307550397Sobrien
3076132718Skan	  /* Verify that we've deallocated all the stack we used.  */
3077169689Skan	  gcc_assert ((flags & ECF_NORETURN)
3078169689Skan		      || (old_stack_allocated
3079169689Skan			  == stack_pointer_delta - pending_stack_adjust));
3080132718Skan	}
3081132718Skan
308290075Sobrien      /* If something prevents making this a sibling call,
308390075Sobrien	 zero out the sequence.  */
308490075Sobrien      if (sibcall_failure)
308590075Sobrien	tail_call_insns = NULL_RTX;
3086169689Skan      else
3087169689Skan	break;
308818334Speter    }
308918334Speter
3090169689Skan  /* If tail call production succeeded, we need to remove REG_EQUIV notes on
3091169689Skan     arguments too, as argument area is now clobbered by the call.  */
3092169689Skan  if (tail_call_insns)
309318334Speter    {
3094169689Skan      emit_insn (tail_call_insns);
3095169689Skan      cfun->tail_call_emit = true;
309618334Speter    }
309790075Sobrien  else
3098117395Skan    emit_insn (normal_call_insns);
309918334Speter
310090075Sobrien  currently_expanding_call--;
310118334Speter
310290075Sobrien  /* If this function returns with the stack pointer depressed, ensure
310390075Sobrien     this block saves and restores the stack pointer, show it was
310490075Sobrien     changed, and adjust for any outgoing arg space.  */
310590075Sobrien  if (flags & ECF_SP_DEPRESSED)
310650397Sobrien    {
310790075Sobrien      clear_pending_stack_adjust ();
3108169689Skan      emit_insn (gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx));
310990075Sobrien      emit_move_insn (virtual_stack_dynamic_rtx, stack_pointer_rtx);
311050397Sobrien    }
311150397Sobrien
3112169689Skan  if (stack_usage_map_buf)
3113169689Skan    free (stack_usage_map_buf);
3114169689Skan
311590075Sobrien  return target;
311618334Speter}
3117132718Skan
3118169689Skan/* A sibling call sequence invalidates any REG_EQUIV notes made for
3119169689Skan   this function's incoming arguments.
3120169689Skan
3121169689Skan   At the start of RTL generation we know the only REG_EQUIV notes
3122169689Skan   in the rtl chain are those for incoming arguments, so we can look
3123169689Skan   for REG_EQUIV notes between the start of the function and the
3124169689Skan   NOTE_INSN_FUNCTION_BEG.
3125169689Skan
3126169689Skan   This is (slight) overkill.  We could keep track of the highest
3127169689Skan   argument we clobber and be more selective in removing notes, but it
3128169689Skan   does not seem to be worth the effort.  */
3129169689Skan
3130169689Skanvoid
3131169689Skanfixup_tail_calls (void)
3132169689Skan{
3133169689Skan  rtx insn;
3134169689Skan
3135169689Skan  for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3136169689Skan    {
3137169689Skan      /* There are never REG_EQUIV notes for the incoming arguments
3138169689Skan	 after the NOTE_INSN_FUNCTION_BEG note, so stop if we see it.  */
3139169689Skan      if (NOTE_P (insn)
3140169689Skan	  && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
3141169689Skan	break;
3142169689Skan
3143169689Skan      while (1)
3144169689Skan	{
3145169689Skan	  rtx note = find_reg_note (insn, REG_EQUIV, 0);
3146169689Skan	  if (note)
3147169689Skan	    {
3148169689Skan	      /* Remove the note and keep looking at the notes for
3149169689Skan		 this insn.  */
3150169689Skan	      remove_note (insn, note);
3151169689Skan	      continue;
3152169689Skan	    }
3153169689Skan	  break;
3154169689Skan	}
3155169689Skan    }
3156169689Skan}
3157169689Skan
3158132718Skan/* Traverse an argument list in VALUES and expand all complex
3159132718Skan   arguments into their components.  */
3160169689Skanstatic tree
3161132718Skansplit_complex_values (tree values)
3162132718Skan{
3163132718Skan  tree p;
3164132718Skan
3165132718Skan  /* Before allocating memory, check for the common case of no complex.  */
3166132718Skan  for (p = values; p; p = TREE_CHAIN (p))
3167132718Skan    {
3168132718Skan      tree type = TREE_TYPE (TREE_VALUE (p));
3169132718Skan      if (type && TREE_CODE (type) == COMPLEX_TYPE
3170132718Skan	  && targetm.calls.split_complex_arg (type))
3171169689Skan	goto found;
3172132718Skan    }
3173132718Skan  return values;
3174132718Skan
3175132718Skan found:
3176132718Skan  values = copy_list (values);
3177132718Skan
3178132718Skan  for (p = values; p; p = TREE_CHAIN (p))
3179132718Skan    {
3180132718Skan      tree complex_value = TREE_VALUE (p);
3181132718Skan      tree complex_type;
3182132718Skan
3183132718Skan      complex_type = TREE_TYPE (complex_value);
3184132718Skan      if (!complex_type)
3185132718Skan	continue;
3186132718Skan
3187132718Skan      if (TREE_CODE (complex_type) == COMPLEX_TYPE
3188132718Skan	  && targetm.calls.split_complex_arg (complex_type))
3189132718Skan	{
3190132718Skan	  tree subtype;
3191132718Skan	  tree real, imag, next;
3192132718Skan
3193132718Skan	  subtype = TREE_TYPE (complex_type);
3194132718Skan	  complex_value = save_expr (complex_value);
3195132718Skan	  real = build1 (REALPART_EXPR, subtype, complex_value);
3196132718Skan	  imag = build1 (IMAGPART_EXPR, subtype, complex_value);
3197132718Skan
3198132718Skan	  TREE_VALUE (p) = real;
3199132718Skan	  next = TREE_CHAIN (p);
3200132718Skan	  imag = build_tree_list (NULL_TREE, imag);
3201132718Skan	  TREE_CHAIN (p) = imag;
3202132718Skan	  TREE_CHAIN (imag) = next;
3203132718Skan
3204132718Skan	  /* Skip the newly created node.  */
3205132718Skan	  p = TREE_CHAIN (p);
3206132718Skan	}
3207132718Skan    }
3208132718Skan
3209132718Skan  return values;
3210132718Skan}
3211132718Skan
3212132718Skan/* Traverse a list of TYPES and expand all complex types into their
3213132718Skan   components.  */
3214169689Skanstatic tree
3215132718Skansplit_complex_types (tree types)
3216132718Skan{
3217132718Skan  tree p;
3218132718Skan
3219132718Skan  /* Before allocating memory, check for the common case of no complex.  */
3220132718Skan  for (p = types; p; p = TREE_CHAIN (p))
3221132718Skan    {
3222132718Skan      tree type = TREE_VALUE (p);
3223132718Skan      if (TREE_CODE (type) == COMPLEX_TYPE
3224132718Skan	  && targetm.calls.split_complex_arg (type))
3225169689Skan	goto found;
3226132718Skan    }
3227132718Skan  return types;
3228132718Skan
3229132718Skan found:
3230132718Skan  types = copy_list (types);
3231132718Skan
3232132718Skan  for (p = types; p; p = TREE_CHAIN (p))
3233132718Skan    {
3234132718Skan      tree complex_type = TREE_VALUE (p);
3235132718Skan
3236132718Skan      if (TREE_CODE (complex_type) == COMPLEX_TYPE
3237132718Skan	  && targetm.calls.split_complex_arg (complex_type))
3238132718Skan	{
3239132718Skan	  tree next, imag;
3240132718Skan
3241132718Skan	  /* Rewrite complex type with component type.  */
3242132718Skan	  TREE_VALUE (p) = TREE_TYPE (complex_type);
3243132718Skan	  next = TREE_CHAIN (p);
3244132718Skan
3245132718Skan	  /* Add another component type for the imaginary part.  */
3246132718Skan	  imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
3247132718Skan	  TREE_CHAIN (p) = imag;
3248132718Skan	  TREE_CHAIN (imag) = next;
3249132718Skan
3250132718Skan	  /* Skip the newly created node.  */
3251132718Skan	  p = TREE_CHAIN (p);
3252132718Skan	}
3253132718Skan    }
3254132718Skan
3255132718Skan  return types;
3256132718Skan}
325718334Speter
325890075Sobrien/* Output a library call to function FUN (a SYMBOL_REF rtx).
325990075Sobrien   The RETVAL parameter specifies whether return value needs to be saved, other
326090075Sobrien   parameters are documented in the emit_library_call function below.  */
326118334Speter
326290075Sobrienstatic rtx
3263132718Skanemit_library_call_value_1 (int retval, rtx orgfun, rtx value,
3264132718Skan			   enum libcall_type fn_type,
3265132718Skan			   enum machine_mode outmode, int nargs, va_list p)
326618334Speter{
326718334Speter  /* Total size in bytes of all the stack-parms scanned so far.  */
326818334Speter  struct args_size args_size;
326918334Speter  /* Size of arguments before any adjustments (such as rounding).  */
327018334Speter  struct args_size original_args_size;
327190075Sobrien  int argnum;
327218334Speter  rtx fun;
327318334Speter  int inc;
327418334Speter  int count;
327518334Speter  rtx argblock = 0;
327618334Speter  CUMULATIVE_ARGS args_so_far;
327790075Sobrien  struct arg
327890075Sobrien  {
327990075Sobrien    rtx value;
328090075Sobrien    enum machine_mode mode;
328190075Sobrien    rtx reg;
328290075Sobrien    int partial;
3283132718Skan    struct locate_and_pad_arg_data locate;
328490075Sobrien    rtx save_area;
328590075Sobrien  };
328618334Speter  struct arg *argvec;
328718334Speter  int old_inhibit_defer_pop = inhibit_defer_pop;
328818334Speter  rtx call_fusage = 0;
328918334Speter  rtx mem_value = 0;
329090075Sobrien  rtx valreg;
329118334Speter  int pcc_struct_value = 0;
329218334Speter  int struct_value_size = 0;
329390075Sobrien  int flags;
329450397Sobrien  int reg_parm_stack_space = 0;
329550397Sobrien  int needed;
329690075Sobrien  rtx before_call;
3297117395Skan  tree tfom;			/* type_for_mode (outmode, 0) */
329818334Speter
329990075Sobrien#ifdef REG_PARM_STACK_SPACE
330050397Sobrien  /* Define the boundary of the register parm stack space that needs to be
330150397Sobrien     save, if any.  */
3302132718Skan  int low_to_save, high_to_save;
330390075Sobrien  rtx save_area = 0;            /* Place that it is saved.  */
330450397Sobrien#endif
330550397Sobrien
330650397Sobrien  /* Size of the stack reserved for parameter registers.  */
330750397Sobrien  int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
330850397Sobrien  char *initial_stack_usage_map = stack_usage_map;
3309169689Skan  char *stack_usage_map_buf = NULL;
331050397Sobrien
3311132718Skan  rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
3312132718Skan
331350397Sobrien#ifdef REG_PARM_STACK_SPACE
331452284Sobrien  reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
331550397Sobrien#endif
331650397Sobrien
331790075Sobrien  /* By default, library functions can not throw.  */
331890075Sobrien  flags = ECF_NOTHROW;
331918334Speter
332090075Sobrien  switch (fn_type)
332190075Sobrien    {
332290075Sobrien    case LCT_NORMAL:
332390075Sobrien      break;
332490075Sobrien    case LCT_CONST:
332590075Sobrien      flags |= ECF_CONST;
332690075Sobrien      break;
332790075Sobrien    case LCT_PURE:
332890075Sobrien      flags |= ECF_PURE;
332990075Sobrien      break;
333090075Sobrien    case LCT_CONST_MAKE_BLOCK:
333190075Sobrien      flags |= ECF_CONST | ECF_LIBCALL_BLOCK;
333290075Sobrien      break;
333390075Sobrien    case LCT_PURE_MAKE_BLOCK:
333490075Sobrien      flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
333590075Sobrien      break;
333690075Sobrien    case LCT_NORETURN:
333790075Sobrien      flags |= ECF_NORETURN;
333890075Sobrien      break;
333990075Sobrien    case LCT_THROW:
334090075Sobrien      flags = ECF_NORETURN;
334190075Sobrien      break;
334290075Sobrien    case LCT_RETURNS_TWICE:
334390075Sobrien      flags = ECF_RETURNS_TWICE;
334490075Sobrien      break;
334590075Sobrien    }
334618334Speter  fun = orgfun;
334718334Speter
334890075Sobrien  /* Ensure current function's preferred stack boundary is at least
334990075Sobrien     what we need.  */
335090075Sobrien  if (cfun->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
335190075Sobrien    cfun->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
335290075Sobrien
335318334Speter  /* If this kind of value comes back in memory,
335418334Speter     decide where in memory it should come back.  */
3355117395Skan  if (outmode != VOIDmode)
335618334Speter    {
3357169689Skan      tfom = lang_hooks.types.type_for_mode (outmode, 0);
3358132718Skan      if (aggregate_value_p (tfom, 0))
3359117395Skan	{
336018334Speter#ifdef PCC_STATIC_STRUCT_RETURN
3361117395Skan	  rtx pointer_reg
3362169689Skan	    = hard_function_value (build_pointer_type (tfom), 0, 0, 0);
3363117395Skan	  mem_value = gen_rtx_MEM (outmode, pointer_reg);
3364117395Skan	  pcc_struct_value = 1;
3365117395Skan	  if (value == 0)
3366117395Skan	    value = gen_reg_rtx (outmode);
336718334Speter#else /* not PCC_STATIC_STRUCT_RETURN */
3368117395Skan	  struct_value_size = GET_MODE_SIZE (outmode);
3369169689Skan	  if (value != 0 && MEM_P (value))
3370117395Skan	    mem_value = value;
3371117395Skan	  else
3372117395Skan	    mem_value = assign_temp (tfom, 0, 1, 1);
337318334Speter#endif
3374117395Skan	  /* This call returns a big structure.  */
3375117395Skan	  flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
3376117395Skan	}
337718334Speter    }
3378117395Skan  else
3379117395Skan    tfom = void_type_node;
338018334Speter
338118334Speter  /* ??? Unfinished: must pass the memory address as an argument.  */
338218334Speter
338318334Speter  /* Copy all the libcall-arguments out of the varargs data
338418334Speter     and into a vector ARGVEC.
338518334Speter
338618334Speter     Compute how to pass each argument.  We only support a very small subset
338718334Speter     of the full argument passing conventions to limit complexity here since
338818334Speter     library functions shouldn't have many args.  */
338918334Speter
3390132718Skan  argvec = alloca ((nargs + 1) * sizeof (struct arg));
3391132718Skan  memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
339218334Speter
339390075Sobrien#ifdef INIT_CUMULATIVE_LIBCALL_ARGS
339490075Sobrien  INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far, outmode, fun);
339590075Sobrien#else
3396132718Skan  INIT_CUMULATIVE_ARGS (args_so_far, NULL_TREE, fun, 0, nargs);
339790075Sobrien#endif
339818334Speter
339918334Speter  args_size.constant = 0;
340018334Speter  args_size.var = 0;
340118334Speter
340218334Speter  count = 0;
340318334Speter
340490075Sobrien  /* Now we are about to start emitting insns that can be deleted
340590075Sobrien     if a libcall is deleted.  */
340690075Sobrien  if (flags & ECF_LIBCALL_BLOCK)
340790075Sobrien    start_sequence ();
340890075Sobrien
340918334Speter  push_temp_slots ();
341018334Speter
341118334Speter  /* If there's a structure value address to be passed,
341218334Speter     either pass it in the special place, or pass it as an extra argument.  */
3413132718Skan  if (mem_value && struct_value == 0 && ! pcc_struct_value)
341418334Speter    {
341518334Speter      rtx addr = XEXP (mem_value, 0);
3416169689Skan
341718334Speter      nargs++;
341818334Speter
341918334Speter      /* Make sure it is a reasonable operand for a move or push insn.  */
3420169689Skan      if (!REG_P (addr) && !MEM_P (addr)
342118334Speter	  && ! (CONSTANT_P (addr) && LEGITIMATE_CONSTANT_P (addr)))
342218334Speter	addr = force_operand (addr, NULL_RTX);
342318334Speter
342418334Speter      argvec[count].value = addr;
342518334Speter      argvec[count].mode = Pmode;
342618334Speter      argvec[count].partial = 0;
342718334Speter
342818334Speter      argvec[count].reg = FUNCTION_ARG (args_so_far, Pmode, NULL_TREE, 1);
3429169689Skan      gcc_assert (targetm.calls.arg_partial_bytes (&args_so_far, Pmode,
3430169689Skan						   NULL_TREE, 1) == 0);
343118334Speter
343218334Speter      locate_and_pad_parm (Pmode, NULL_TREE,
343390075Sobrien#ifdef STACK_PARMS_IN_REG_PARM_AREA
3434169689Skan			   1,
343590075Sobrien#else
343690075Sobrien			   argvec[count].reg != 0,
343790075Sobrien#endif
3438132718Skan			   0, NULL_TREE, &args_size, &argvec[count].locate);
343918334Speter
344018334Speter      if (argvec[count].reg == 0 || argvec[count].partial != 0
344150397Sobrien	  || reg_parm_stack_space > 0)
3442132718Skan	args_size.constant += argvec[count].locate.size.constant;
344318334Speter
344450397Sobrien      FUNCTION_ARG_ADVANCE (args_so_far, Pmode, (tree) 0, 1);
344518334Speter
344618334Speter      count++;
344718334Speter    }
344818334Speter
344918334Speter  for (; count < nargs; count++)
345018334Speter    {
345118334Speter      rtx val = va_arg (p, rtx);
345218334Speter      enum machine_mode mode = va_arg (p, enum machine_mode);
345318334Speter
345418334Speter      /* We cannot convert the arg value to the mode the library wants here;
345518334Speter	 must do it earlier where we know the signedness of the arg.  */
3456169689Skan      gcc_assert (mode != BLKmode
3457169689Skan		  && (GET_MODE (val) == mode || GET_MODE (val) == VOIDmode));
345818334Speter
345918334Speter      /* Make sure it is a reasonable operand for a move or push insn.  */
3460169689Skan      if (!REG_P (val) && !MEM_P (val)
346118334Speter	  && ! (CONSTANT_P (val) && LEGITIMATE_CONSTANT_P (val)))
346218334Speter	val = force_operand (val, NULL_RTX);
346318334Speter
3464169689Skan      if (pass_by_reference (&args_so_far, mode, NULL_TREE, 1))
346518334Speter	{
346690075Sobrien	  rtx slot;
3467169689Skan	  int must_copy
3468169689Skan	    = !reference_callee_copied (&args_so_far, mode, NULL_TREE, 1);
346990075Sobrien
3470117395Skan	  /* loop.c won't look at CALL_INSN_FUNCTION_USAGE of const/pure
3471117395Skan	     functions, so we have to pretend this isn't such a function.  */
3472117395Skan	  if (flags & ECF_LIBCALL_BLOCK)
3473117395Skan	    {
3474117395Skan	      rtx insns = get_insns ();
3475117395Skan	      end_sequence ();
3476117395Skan	      emit_insn (insns);
3477117395Skan	    }
3478117395Skan	  flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
3479117395Skan
3480104752Skan	  /* If this was a CONST function, it is now PURE since
3481104752Skan	     it now reads memory.  */
3482104752Skan	  if (flags & ECF_CONST)
3483104752Skan	    {
3484104752Skan	      flags &= ~ECF_CONST;
3485104752Skan	      flags |= ECF_PURE;
3486104752Skan	    }
3487104752Skan
3488169689Skan	  if (GET_MODE (val) == MEM && !must_copy)
348990075Sobrien	    slot = val;
3490169689Skan	  else
349190075Sobrien	    {
3492169689Skan	      slot = assign_temp (lang_hooks.types.type_for_mode (mode, 0),
3493117395Skan				  0, 1, 1);
349490075Sobrien	      emit_move_insn (slot, val);
349590075Sobrien	    }
349690075Sobrien
349790075Sobrien	  call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
349890075Sobrien					   gen_rtx_USE (VOIDmode, slot),
349990075Sobrien					   call_fusage);
350090075Sobrien	  if (must_copy)
350190075Sobrien	    call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
350290075Sobrien					     gen_rtx_CLOBBER (VOIDmode,
350390075Sobrien							      slot),
350490075Sobrien					     call_fusage);
350590075Sobrien
350618334Speter	  mode = Pmode;
350790075Sobrien	  val = force_operand (XEXP (slot, 0), NULL_RTX);
350818334Speter	}
350918334Speter
351018334Speter      argvec[count].value = val;
351118334Speter      argvec[count].mode = mode;
351218334Speter
351318334Speter      argvec[count].reg = FUNCTION_ARG (args_so_far, mode, NULL_TREE, 1);
351490075Sobrien
351518334Speter      argvec[count].partial
3516169689Skan	= targetm.calls.arg_partial_bytes (&args_so_far, mode, NULL_TREE, 1);
351718334Speter
351818334Speter      locate_and_pad_parm (mode, NULL_TREE,
351990075Sobrien#ifdef STACK_PARMS_IN_REG_PARM_AREA
352090075Sobrien			   1,
352190075Sobrien#else
352290075Sobrien			   argvec[count].reg != 0,
352390075Sobrien#endif
3524132718Skan			   argvec[count].partial,
3525132718Skan			   NULL_TREE, &args_size, &argvec[count].locate);
352618334Speter
3527169689Skan      gcc_assert (!argvec[count].locate.size.var);
352818334Speter
352918334Speter      if (argvec[count].reg == 0 || argvec[count].partial != 0
353050397Sobrien	  || reg_parm_stack_space > 0)
3531132718Skan	args_size.constant += argvec[count].locate.size.constant;
353218334Speter
353350397Sobrien      FUNCTION_ARG_ADVANCE (args_so_far, mode, (tree) 0, 1);
353418334Speter    }
353518334Speter
353618334Speter  /* If this machine requires an external definition for library
353718334Speter     functions, write one out.  */
353818334Speter  assemble_external_libcall (fun);
353918334Speter
354018334Speter  original_args_size = args_size;
354190075Sobrien  args_size.constant = (((args_size.constant
354290075Sobrien			  + stack_pointer_delta
354390075Sobrien			  + STACK_BYTES - 1)
354490075Sobrien			  / STACK_BYTES
354590075Sobrien			  * STACK_BYTES)
354690075Sobrien			 - stack_pointer_delta);
354718334Speter
354818334Speter  args_size.constant = MAX (args_size.constant,
354950397Sobrien			    reg_parm_stack_space);
355050397Sobrien
355118334Speter#ifndef OUTGOING_REG_PARM_STACK_SPACE
355250397Sobrien  args_size.constant -= reg_parm_stack_space;
355318334Speter#endif
355418334Speter
355518334Speter  if (args_size.constant > current_function_outgoing_args_size)
355618334Speter    current_function_outgoing_args_size = args_size.constant;
355718334Speter
355890075Sobrien  if (ACCUMULATE_OUTGOING_ARGS)
355990075Sobrien    {
356090075Sobrien      /* Since the stack pointer will never be pushed, it is possible for
356190075Sobrien	 the evaluation of a parm to clobber something we have already
356290075Sobrien	 written to the stack.  Since most function calls on RISC machines
356390075Sobrien	 do not use the stack, this is uncommon, but must work correctly.
356450397Sobrien
356590075Sobrien	 Therefore, we save any area of the stack that was already written
356690075Sobrien	 and that we are using.  Here we set up to do this by making a new
356790075Sobrien	 stack usage map from the old one.
356850397Sobrien
356990075Sobrien	 Another approach might be to try to reorder the argument
357090075Sobrien	 evaluations to avoid this conflicting stack usage.  */
357150397Sobrien
357290075Sobrien      needed = args_size.constant;
357350397Sobrien
357450397Sobrien#ifndef OUTGOING_REG_PARM_STACK_SPACE
357590075Sobrien      /* Since we will be writing into the entire argument area, the
357690075Sobrien	 map must be allocated for its entire size, not just the part that
357790075Sobrien	 is the responsibility of the caller.  */
357890075Sobrien      needed += reg_parm_stack_space;
357918334Speter#endif
358018334Speter
358150397Sobrien#ifdef ARGS_GROW_DOWNWARD
358290075Sobrien      highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
358390075Sobrien					 needed + 1);
358450397Sobrien#else
358590075Sobrien      highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
358690075Sobrien					 needed);
358750397Sobrien#endif
3588169689Skan      stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
3589169689Skan      stack_usage_map = stack_usage_map_buf;
359050397Sobrien
359190075Sobrien      if (initial_highest_arg_in_use)
359290075Sobrien	memcpy (stack_usage_map, initial_stack_usage_map,
359390075Sobrien		initial_highest_arg_in_use);
359450397Sobrien
359590075Sobrien      if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
359690075Sobrien	memset (&stack_usage_map[initial_highest_arg_in_use], 0,
359790075Sobrien	       highest_outgoing_arg_in_use - initial_highest_arg_in_use);
359890075Sobrien      needed = 0;
359950397Sobrien
360090075Sobrien      /* We must be careful to use virtual regs before they're instantiated,
3601169689Skan	 and real regs afterwards.  Loop optimization, for example, can create
360290075Sobrien	 new libcalls after we've instantiated the virtual regs, and if we
360390075Sobrien	 use virtuals anyway, they won't match the rtl patterns.  */
360450397Sobrien
360590075Sobrien      if (virtuals_instantiated)
360690075Sobrien	argblock = plus_constant (stack_pointer_rtx, STACK_POINTER_OFFSET);
360790075Sobrien      else
360890075Sobrien	argblock = virtual_outgoing_args_rtx;
360990075Sobrien    }
361090075Sobrien  else
361190075Sobrien    {
361290075Sobrien      if (!PUSH_ARGS)
361390075Sobrien	argblock = push_block (GEN_INT (args_size.constant), 0, 0);
361490075Sobrien    }
361518334Speter
361618334Speter  /* If we push args individually in reverse order, perform stack alignment
361718334Speter     before the first push (the last arg).  */
361890075Sobrien  if (argblock == 0 && PUSH_ARGS_REVERSED)
361918334Speter    anti_adjust_stack (GEN_INT (args_size.constant
362018334Speter				- original_args_size.constant));
362118334Speter
362290075Sobrien  if (PUSH_ARGS_REVERSED)
362390075Sobrien    {
362490075Sobrien      inc = -1;
362590075Sobrien      argnum = nargs - 1;
362690075Sobrien    }
362790075Sobrien  else
362890075Sobrien    {
362990075Sobrien      inc = 1;
363090075Sobrien      argnum = 0;
363190075Sobrien    }
363218334Speter
363390075Sobrien#ifdef REG_PARM_STACK_SPACE
363490075Sobrien  if (ACCUMULATE_OUTGOING_ARGS)
363590075Sobrien    {
363690075Sobrien      /* The argument list is the property of the called routine and it
363790075Sobrien	 may clobber it.  If the fixed area has been used for previous
3638132718Skan	 parameters, we must save and restore it.  */
3639132718Skan      save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3640132718Skan					    &low_to_save, &high_to_save);
364150397Sobrien    }
364250397Sobrien#endif
364390075Sobrien
364418334Speter  /* Push the args that need to be pushed.  */
364518334Speter
364650397Sobrien  /* ARGNUM indexes the ARGVEC array in the order in which the arguments
364750397Sobrien     are to be pushed.  */
364818334Speter  for (count = 0; count < nargs; count++, argnum += inc)
364918334Speter    {
365090075Sobrien      enum machine_mode mode = argvec[argnum].mode;
365190075Sobrien      rtx val = argvec[argnum].value;
365218334Speter      rtx reg = argvec[argnum].reg;
365318334Speter      int partial = argvec[argnum].partial;
365490075Sobrien      int lower_bound = 0, upper_bound = 0, i;
365518334Speter
365618334Speter      if (! (reg != 0 && partial == 0))
365750397Sobrien	{
365890075Sobrien	  if (ACCUMULATE_OUTGOING_ARGS)
365990075Sobrien	    {
366090075Sobrien	      /* If this is being stored into a pre-allocated, fixed-size,
366190075Sobrien		 stack area, save any previous data at that location.  */
366250397Sobrien
366350397Sobrien#ifdef ARGS_GROW_DOWNWARD
366490075Sobrien	      /* stack_slot is negative, but we want to index stack_usage_map
366590075Sobrien		 with positive values.  */
3666132718Skan	      upper_bound = -argvec[argnum].locate.offset.constant + 1;
3667132718Skan	      lower_bound = upper_bound - argvec[argnum].locate.size.constant;
366850397Sobrien#else
3669132718Skan	      lower_bound = argvec[argnum].locate.offset.constant;
3670132718Skan	      upper_bound = lower_bound + argvec[argnum].locate.size.constant;
367150397Sobrien#endif
367250397Sobrien
3673132718Skan	      i = lower_bound;
3674132718Skan	      /* Don't worry about things in the fixed argument area;
3675132718Skan		 it has already been saved.  */
3676132718Skan	      if (i < reg_parm_stack_space)
3677132718Skan		i = reg_parm_stack_space;
3678132718Skan	      while (i < upper_bound && stack_usage_map[i] == 0)
3679132718Skan		i++;
368050397Sobrien
3681132718Skan	      if (i < upper_bound)
368290075Sobrien		{
3683132718Skan		  /* We need to make a save area.  */
3684132718Skan		  unsigned int size
3685132718Skan		    = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
368690075Sobrien		  enum machine_mode save_mode
3687132718Skan		    = mode_for_size (size, MODE_INT, 1);
3688132718Skan		  rtx adr
3689132718Skan		    = plus_constant (argblock,
3690132718Skan				     argvec[argnum].locate.offset.constant);
369190075Sobrien		  rtx stack_area
3692132718Skan		    = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
3693132718Skan
3694122180Skan		  if (save_mode == BLKmode)
3695122180Skan		    {
3696122180Skan		      argvec[argnum].save_area
3697122180Skan			= assign_stack_temp (BLKmode,
3698169689Skan					     argvec[argnum].locate.size.constant,
3699132718Skan					     0);
370090075Sobrien
3701122180Skan		      emit_block_move (validize_mem (argvec[argnum].save_area),
3702169689Skan				       stack_area,
3703132718Skan				       GEN_INT (argvec[argnum].locate.size.constant),
3704122180Skan				       BLOCK_OP_CALL_PARM);
3705122180Skan		    }
3706122180Skan		  else
3707122180Skan		    {
3708122180Skan		      argvec[argnum].save_area = gen_reg_rtx (save_mode);
3709122180Skan
3710122180Skan		      emit_move_insn (argvec[argnum].save_area, stack_area);
3711122180Skan		    }
371290075Sobrien		}
371350397Sobrien	    }
371490075Sobrien
3715117395Skan	  emit_push_insn (val, mode, NULL_TREE, NULL_RTX, PARM_BOUNDARY,
3716117395Skan			  partial, reg, 0, argblock,
3717132718Skan			  GEN_INT (argvec[argnum].locate.offset.constant),
3718132718Skan			  reg_parm_stack_space,
3719132718Skan			  ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad));
372050397Sobrien
372150397Sobrien	  /* Now mark the segment we just used.  */
372290075Sobrien	  if (ACCUMULATE_OUTGOING_ARGS)
372390075Sobrien	    for (i = lower_bound; i < upper_bound; i++)
372490075Sobrien	      stack_usage_map[i] = 1;
372550397Sobrien
372650397Sobrien	  NO_DEFER_POP;
3727169689Skan
3728169689Skan	  if (flags & ECF_CONST)
3729169689Skan	    {
3730169689Skan	      rtx use;
3731169689Skan
3732169689Skan	      /* Indicate argument access so that alias.c knows that these
3733169689Skan		 values are live.  */
3734169689Skan	      if (argblock)
3735169689Skan		use = plus_constant (argblock,
3736169689Skan				     argvec[argnum].locate.offset.constant);
3737169689Skan	      else
3738169689Skan		/* When arguments are pushed, trying to tell alias.c where
3739169689Skan		   exactly this argument is won't work, because the
3740169689Skan		   auto-increment causes confusion.  So we merely indicate
3741169689Skan		   that we access something with a known mode somewhere on
3742169689Skan		   the stack.  */
3743169689Skan		use = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3744169689Skan				    gen_rtx_SCRATCH (Pmode));
3745169689Skan	      use = gen_rtx_MEM (argvec[argnum].mode, use);
3746169689Skan	      use = gen_rtx_USE (VOIDmode, use);
3747169689Skan	      call_fusage = gen_rtx_EXPR_LIST (VOIDmode, use, call_fusage);
3748169689Skan	    }
374950397Sobrien	}
375018334Speter    }
375118334Speter
375218334Speter  /* If we pushed args in forward order, perform stack alignment
375318334Speter     after pushing the last arg.  */
375490075Sobrien  if (argblock == 0 && !PUSH_ARGS_REVERSED)
375518334Speter    anti_adjust_stack (GEN_INT (args_size.constant
375618334Speter				- original_args_size.constant));
375718334Speter
375890075Sobrien  if (PUSH_ARGS_REVERSED)
375990075Sobrien    argnum = nargs - 1;
376090075Sobrien  else
376190075Sobrien    argnum = 0;
376218334Speter
3763169689Skan  fun = prepare_call_address (fun, NULL, &call_fusage, 0, 0);
376418334Speter
376518334Speter  /* Now load any reg parms into their regs.  */
376618334Speter
376750397Sobrien  /* ARGNUM indexes the ARGVEC array in the order in which the arguments
376850397Sobrien     are to be pushed.  */
376918334Speter  for (count = 0; count < nargs; count++, argnum += inc)
377018334Speter    {
3771169689Skan      enum machine_mode mode = argvec[argnum].mode;
377290075Sobrien      rtx val = argvec[argnum].value;
377318334Speter      rtx reg = argvec[argnum].reg;
377418334Speter      int partial = argvec[argnum].partial;
377518334Speter
377690075Sobrien      /* Handle calls that pass values in multiple non-contiguous
377790075Sobrien	 locations.  The PA64 has examples of this for library calls.  */
377890075Sobrien      if (reg != 0 && GET_CODE (reg) == PARALLEL)
3779169689Skan	emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (mode));
378090075Sobrien      else if (reg != 0 && partial == 0)
378118334Speter	emit_move_insn (reg, val);
378290075Sobrien
378318334Speter      NO_DEFER_POP;
378418334Speter    }
378518334Speter
378618334Speter  /* Any regs containing parms remain in use through the call.  */
378718334Speter  for (count = 0; count < nargs; count++)
378890075Sobrien    {
378990075Sobrien      rtx reg = argvec[count].reg;
379090075Sobrien      if (reg != 0 && GET_CODE (reg) == PARALLEL)
379190075Sobrien	use_group_regs (&call_fusage, reg);
379290075Sobrien      else if (reg != 0)
379390075Sobrien	use_reg (&call_fusage, reg);
379490075Sobrien    }
379518334Speter
379618334Speter  /* Pass the function the address in which to return a structure value.  */
3797132718Skan  if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
379818334Speter    {
3799132718Skan      emit_move_insn (struct_value,
380018334Speter		      force_reg (Pmode,
380118334Speter				 force_operand (XEXP (mem_value, 0),
380218334Speter						NULL_RTX)));
3803169689Skan      if (REG_P (struct_value))
3804132718Skan	use_reg (&call_fusage, struct_value);
380518334Speter    }
380618334Speter
380718334Speter  /* Don't allow popping to be deferred, since then
380818334Speter     cse'ing of library calls could delete a call and leave the pop.  */
380918334Speter  NO_DEFER_POP;
381090075Sobrien  valreg = (mem_value == 0 && outmode != VOIDmode
381190075Sobrien	    ? hard_libcall_value (outmode) : NULL_RTX);
381218334Speter
381390075Sobrien  /* Stack must be properly aligned now.  */
3814169689Skan  gcc_assert (!(stack_pointer_delta
3815169689Skan		& (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1)));
381690075Sobrien
381790075Sobrien  before_call = get_last_insn ();
381890075Sobrien
381918334Speter  /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
382018334Speter     will set inhibit_defer_pop to that value.  */
382190075Sobrien  /* The return type is needed to decide how many bytes the function pops.
382290075Sobrien     Signedness plays no role in that, so for simplicity, we pretend it's
382390075Sobrien     always signed.  We also assume that the list of arguments passed has
382490075Sobrien     no impact, so we pretend it is unknown.  */
382518334Speter
3826169689Skan  emit_call_1 (fun, NULL,
382790075Sobrien	       get_identifier (XSTR (orgfun, 0)),
3828117395Skan	       build_function_type (tfom, NULL_TREE),
382990075Sobrien	       original_args_size.constant, args_size.constant,
383052284Sobrien	       struct_value_size,
383118334Speter	       FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1),
383290075Sobrien	       valreg,
383396263Sobrien	       old_inhibit_defer_pop + 1, call_fusage, flags, & args_so_far);
383418334Speter
383590075Sobrien  /* For calls to `setjmp', etc., inform flow.c it should complain
383690075Sobrien     if nonvolatile values are live.  For functions that cannot return,
383790075Sobrien     inform flow that control does not fall through.  */
383890075Sobrien
3839169689Skan  if (flags & ECF_NORETURN)
384090075Sobrien    {
384190075Sobrien      /* The barrier note must be emitted
384290075Sobrien	 immediately after the CALL_INSN.  Some ports emit more than
384390075Sobrien	 just a CALL_INSN above, so we must search for it here.  */
384490075Sobrien
384590075Sobrien      rtx last = get_last_insn ();
3846169689Skan      while (!CALL_P (last))
384790075Sobrien	{
384890075Sobrien	  last = PREV_INSN (last);
384990075Sobrien	  /* There was no CALL_INSN?  */
3850169689Skan	  gcc_assert (last != before_call);
385190075Sobrien	}
385290075Sobrien
385390075Sobrien      emit_barrier_after (last);
385490075Sobrien    }
385590075Sobrien
385618334Speter  /* Now restore inhibit_defer_pop to its actual original value.  */
385718334Speter  OK_DEFER_POP;
385818334Speter
385990075Sobrien  /* If call is cse'able, make appropriate pair of reg-notes around it.
386090075Sobrien     Test valreg so we don't crash; may safely ignore `const'
386190075Sobrien     if return type is void.  Disable for PARALLEL return values, because
386290075Sobrien     we have no way to move such values into a pseudo register.  */
386390075Sobrien  if (flags & ECF_LIBCALL_BLOCK)
386490075Sobrien    {
386590075Sobrien      rtx insns;
386690075Sobrien
3867117395Skan      if (valreg == 0)
386890075Sobrien	{
386990075Sobrien	  insns = get_insns ();
387090075Sobrien	  end_sequence ();
3871117395Skan	  emit_insn (insns);
387290075Sobrien	}
387390075Sobrien      else
387490075Sobrien	{
387590075Sobrien	  rtx note = 0;
3876117395Skan	  rtx temp;
387790075Sobrien	  int i;
387890075Sobrien
3879117395Skan	  if (GET_CODE (valreg) == PARALLEL)
3880117395Skan	    {
3881117395Skan	      temp = gen_reg_rtx (outmode);
3882169689Skan	      emit_group_store (temp, valreg, NULL_TREE,
3883132718Skan				GET_MODE_SIZE (outmode));
3884117395Skan	      valreg = temp;
3885117395Skan	    }
3886117395Skan
3887117395Skan	  temp = gen_reg_rtx (GET_MODE (valreg));
3888117395Skan
388990075Sobrien	  /* Construct an "equal form" for the value which mentions all the
389090075Sobrien	     arguments in order as well as the function name.  */
389190075Sobrien	  for (i = 0; i < nargs; i++)
389290075Sobrien	    note = gen_rtx_EXPR_LIST (VOIDmode, argvec[i].value, note);
389390075Sobrien	  note = gen_rtx_EXPR_LIST (VOIDmode, fun, note);
389490075Sobrien
389590075Sobrien	  insns = get_insns ();
389690075Sobrien	  end_sequence ();
389790075Sobrien
389890075Sobrien	  if (flags & ECF_PURE)
389990075Sobrien	    note = gen_rtx_EXPR_LIST (VOIDmode,
390090075Sobrien			gen_rtx_USE (VOIDmode,
390190075Sobrien				     gen_rtx_MEM (BLKmode,
390290075Sobrien						  gen_rtx_SCRATCH (VOIDmode))),
390390075Sobrien			note);
390490075Sobrien
390590075Sobrien	  emit_libcall_block (insns, temp, valreg, note);
390690075Sobrien
390790075Sobrien	  valreg = temp;
390890075Sobrien	}
390990075Sobrien    }
391018334Speter  pop_temp_slots ();
391118334Speter
391218334Speter  /* Copy the value to the right place.  */
391390075Sobrien  if (outmode != VOIDmode && retval)
391418334Speter    {
391518334Speter      if (mem_value)
391618334Speter	{
391718334Speter	  if (value == 0)
391818334Speter	    value = mem_value;
391918334Speter	  if (value != mem_value)
392018334Speter	    emit_move_insn (value, mem_value);
392118334Speter	}
3922117395Skan      else if (GET_CODE (valreg) == PARALLEL)
3923117395Skan	{
3924117395Skan	  if (value == 0)
3925117395Skan	    value = gen_reg_rtx (outmode);
3926132718Skan	  emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
3927117395Skan	}
392818334Speter      else if (value != 0)
3929117395Skan	emit_move_insn (value, valreg);
393018334Speter      else
3931117395Skan	value = valreg;
393218334Speter    }
393318334Speter
393490075Sobrien  if (ACCUMULATE_OUTGOING_ARGS)
393590075Sobrien    {
393650397Sobrien#ifdef REG_PARM_STACK_SPACE
393790075Sobrien      if (save_area)
3938132718Skan	restore_fixed_argument_area (save_area, argblock,
3939132718Skan				     high_to_save, low_to_save);
394050397Sobrien#endif
394190075Sobrien
394290075Sobrien      /* If we saved any argument areas, restore them.  */
394390075Sobrien      for (count = 0; count < nargs; count++)
394490075Sobrien	if (argvec[count].save_area)
394590075Sobrien	  {
394690075Sobrien	    enum machine_mode save_mode = GET_MODE (argvec[count].save_area);
3947132718Skan	    rtx adr = plus_constant (argblock,
3948132718Skan				     argvec[count].locate.offset.constant);
3949132718Skan	    rtx stack_area = gen_rtx_MEM (save_mode,
3950132718Skan					  memory_address (save_mode, adr));
395150397Sobrien
3952122180Skan	    if (save_mode == BLKmode)
3953122180Skan	      emit_block_move (stack_area,
3954169689Skan			       validize_mem (argvec[count].save_area),
3955132718Skan			       GEN_INT (argvec[count].locate.size.constant),
3956122180Skan			       BLOCK_OP_CALL_PARM);
3957122180Skan	    else
3958122180Skan	      emit_move_insn (stack_area, argvec[count].save_area);
395990075Sobrien	  }
396050397Sobrien
396190075Sobrien      highest_outgoing_arg_in_use = initial_highest_arg_in_use;
396290075Sobrien      stack_usage_map = initial_stack_usage_map;
396390075Sobrien    }
396490075Sobrien
3965169689Skan  if (stack_usage_map_buf)
3966169689Skan    free (stack_usage_map_buf);
3967169689Skan
396818334Speter  return value;
396990075Sobrien
397018334Speter}
397118334Speter
397290075Sobrien/* Output a library call to function FUN (a SYMBOL_REF rtx)
397390075Sobrien   (emitting the queue unless NO_QUEUE is nonzero),
397490075Sobrien   for a value of mode OUTMODE,
397590075Sobrien   with NARGS different arguments, passed as alternating rtx values
397690075Sobrien   and machine_modes to convert them to.
397718334Speter
397890075Sobrien   FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for `const'
397990075Sobrien   calls, LCT_PURE for `pure' calls, LCT_CONST_MAKE_BLOCK for `const' calls
398090075Sobrien   which should be enclosed in REG_LIBCALL/REG_RETVAL notes,
398190075Sobrien   LCT_PURE_MAKE_BLOCK for `purep' calls which should be enclosed in
398290075Sobrien   REG_LIBCALL/REG_RETVAL notes with extra (use (memory (scratch)),
398390075Sobrien   or other LCT_ value for other types of library calls.  */
398418334Speter
398590075Sobrienvoid
3986132718Skanemit_library_call (rtx orgfun, enum libcall_type fn_type,
3987132718Skan		   enum machine_mode outmode, int nargs, ...)
398818334Speter{
3989132718Skan  va_list p;
399018334Speter
3991132718Skan  va_start (p, nargs);
399290075Sobrien  emit_library_call_value_1 (0, orgfun, NULL_RTX, fn_type, outmode, nargs, p);
3993132718Skan  va_end (p);
399418334Speter}
399518334Speter
399690075Sobrien/* Like emit_library_call except that an extra argument, VALUE,
399790075Sobrien   comes second and says where to store the result.
399890075Sobrien   (If VALUE is zero, this function chooses a convenient way
399990075Sobrien   to return the value.
400090075Sobrien
400190075Sobrien   This function returns an rtx for where the value is to be found.
400290075Sobrien   If VALUE is nonzero, VALUE is returned.  */
400390075Sobrien
400490075Sobrienrtx
4005132718Skanemit_library_call_value (rtx orgfun, rtx value,
4006132718Skan			 enum libcall_type fn_type,
4007132718Skan			 enum machine_mode outmode, int nargs, ...)
400890075Sobrien{
400990075Sobrien  rtx result;
4010132718Skan  va_list p;
401190075Sobrien
4012132718Skan  va_start (p, nargs);
401390075Sobrien  result = emit_library_call_value_1 (1, orgfun, value, fn_type, outmode,
401490075Sobrien				      nargs, p);
4015132718Skan  va_end (p);
401690075Sobrien
401790075Sobrien  return result;
401890075Sobrien}
401990075Sobrien
402018334Speter/* Store a single argument for a function call
402118334Speter   into the register or memory area where it must be passed.
402218334Speter   *ARG describes the argument value and where to pass it.
402318334Speter
402418334Speter   ARGBLOCK is the address of the stack-block for all the arguments,
402518334Speter   or 0 on a machine where arguments are pushed individually.
402618334Speter
402718334Speter   MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
402890075Sobrien   so must be careful about how the stack is used.
402918334Speter
403018334Speter   VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
403118334Speter   argument stack.  This is used if ACCUMULATE_OUTGOING_ARGS to indicate
403218334Speter   that we need not worry about saving and restoring the stack.
403318334Speter
403490075Sobrien   FNDECL is the declaration of the function we are calling.
403518334Speter
4036117395Skan   Return nonzero if this arg should cause sibcall failure,
403790075Sobrien   zero otherwise.  */
403890075Sobrien
403990075Sobrienstatic int
4040132718Skanstore_one_arg (struct arg_data *arg, rtx argblock, int flags,
4041132718Skan	       int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
404218334Speter{
404390075Sobrien  tree pval = arg->tree_value;
404418334Speter  rtx reg = 0;
404518334Speter  int partial = 0;
404618334Speter  int used = 0;
404790075Sobrien  int i, lower_bound = 0, upper_bound = 0;
404890075Sobrien  int sibcall_failure = 0;
404918334Speter
405018334Speter  if (TREE_CODE (pval) == ERROR_MARK)
405190075Sobrien    return 1;
405218334Speter
405318334Speter  /* Push a new temporary level for any temporaries we make for
405418334Speter     this argument.  */
405518334Speter  push_temp_slots ();
405618334Speter
405790075Sobrien  if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
405818334Speter    {
405990075Sobrien      /* If this is being stored into a pre-allocated, fixed-size, stack area,
406090075Sobrien	 save any previous data at that location.  */
406190075Sobrien      if (argblock && ! variable_size && arg->stack)
406290075Sobrien	{
406318334Speter#ifdef ARGS_GROW_DOWNWARD
406490075Sobrien	  /* stack_slot is negative, but we want to index stack_usage_map
406590075Sobrien	     with positive values.  */
406690075Sobrien	  if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
406790075Sobrien	    upper_bound = -INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1)) + 1;
406890075Sobrien	  else
406990075Sobrien	    upper_bound = 0;
407018334Speter
4071132718Skan	  lower_bound = upper_bound - arg->locate.size.constant;
407218334Speter#else
407390075Sobrien	  if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
407490075Sobrien	    lower_bound = INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1));
407590075Sobrien	  else
407690075Sobrien	    lower_bound = 0;
407718334Speter
4078132718Skan	  upper_bound = lower_bound + arg->locate.size.constant;
407918334Speter#endif
408018334Speter
4081132718Skan	  i = lower_bound;
4082132718Skan	  /* Don't worry about things in the fixed argument area;
4083132718Skan	     it has already been saved.  */
4084132718Skan	  if (i < reg_parm_stack_space)
4085132718Skan	    i = reg_parm_stack_space;
4086132718Skan	  while (i < upper_bound && stack_usage_map[i] == 0)
4087132718Skan	    i++;
408818334Speter
4089132718Skan	  if (i < upper_bound)
409090075Sobrien	    {
4091132718Skan	      /* We need to make a save area.  */
4092132718Skan	      unsigned int size = arg->locate.size.constant * BITS_PER_UNIT;
4093132718Skan	      enum machine_mode save_mode = mode_for_size (size, MODE_INT, 1);
4094132718Skan	      rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
4095132718Skan	      rtx stack_area = gen_rtx_MEM (save_mode, adr);
409618334Speter
409790075Sobrien	      if (save_mode == BLKmode)
409890075Sobrien		{
409990075Sobrien		  tree ot = TREE_TYPE (arg->tree_value);
410090075Sobrien		  tree nt = build_qualified_type (ot, (TYPE_QUALS (ot)
410190075Sobrien						       | TYPE_QUAL_CONST));
410290075Sobrien
410390075Sobrien		  arg->save_area = assign_temp (nt, 0, 1, 1);
410490075Sobrien		  preserve_temp_slots (arg->save_area);
410590075Sobrien		  emit_block_move (validize_mem (arg->save_area), stack_area,
4106169689Skan				   GEN_INT (arg->locate.size.constant),
4107117395Skan				   BLOCK_OP_CALL_PARM);
410890075Sobrien		}
410990075Sobrien	      else
411090075Sobrien		{
411190075Sobrien		  arg->save_area = gen_reg_rtx (save_mode);
411290075Sobrien		  emit_move_insn (arg->save_area, stack_area);
411390075Sobrien		}
411418334Speter	    }
411518334Speter	}
411618334Speter    }
411752284Sobrien
411818334Speter  /* If this isn't going to be placed on both the stack and in registers,
411918334Speter     set up the register and number of words.  */
412018334Speter  if (! arg->pass_on_stack)
412196263Sobrien    {
412296263Sobrien      if (flags & ECF_SIBCALL)
412396263Sobrien	reg = arg->tail_call_reg;
412496263Sobrien      else
412596263Sobrien	reg = arg->reg;
412696263Sobrien      partial = arg->partial;
412796263Sobrien    }
412818334Speter
4129169689Skan  /* Being passed entirely in a register.  We shouldn't be called in
4130169689Skan     this case.  */
4131169689Skan  gcc_assert (reg == 0 || partial != 0);
413218334Speter
413318334Speter  /* If this arg needs special alignment, don't load the registers
413418334Speter     here.  */
413518334Speter  if (arg->n_aligned_regs != 0)
413618334Speter    reg = 0;
413790075Sobrien
413818334Speter  /* If this is being passed partially in a register, we can't evaluate
413918334Speter     it directly into its stack slot.  Otherwise, we can.  */
414018334Speter  if (arg->value == 0)
414118334Speter    {
414218334Speter      /* stack_arg_under_construction is nonzero if a function argument is
414318334Speter	 being evaluated directly into the outgoing argument list and
414418334Speter	 expand_call must take special action to preserve the argument list
414518334Speter	 if it is called recursively.
414618334Speter
414718334Speter	 For scalar function arguments stack_usage_map is sufficient to
414818334Speter	 determine which stack slots must be saved and restored.  Scalar
414918334Speter	 arguments in general have pass_on_stack == 0.
415018334Speter
415118334Speter	 If this argument is initialized by a function which takes the
415218334Speter	 address of the argument (a C++ constructor or a C function
415318334Speter	 returning a BLKmode structure), then stack_usage_map is
415418334Speter	 insufficient and expand_call must push the stack around the
415518334Speter	 function call.  Such arguments have pass_on_stack == 1.
415618334Speter
415718334Speter	 Note that it is always safe to set stack_arg_under_construction,
415818334Speter	 but this generates suboptimal code if set when not needed.  */
415918334Speter
416018334Speter      if (arg->pass_on_stack)
416118334Speter	stack_arg_under_construction++;
416290075Sobrien
416318334Speter      arg->value = expand_expr (pval,
416418334Speter				(partial
416518334Speter				 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
416618334Speter				? NULL_RTX : arg->stack,
4167117395Skan				VOIDmode, EXPAND_STACK_PARM);
416818334Speter
416918334Speter      /* If we are promoting object (or for any other reason) the mode
417018334Speter	 doesn't agree, convert the mode.  */
417118334Speter
417218334Speter      if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
417318334Speter	arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
417418334Speter				    arg->value, arg->unsignedp);
417518334Speter
417618334Speter      if (arg->pass_on_stack)
417718334Speter	stack_arg_under_construction--;
417818334Speter    }
417918334Speter
4180169689Skan  /* Check for overlap with already clobbered argument area.  */
4181169689Skan  if ((flags & ECF_SIBCALL)
4182169689Skan      && MEM_P (arg->value)
4183169689Skan      && mem_overlaps_already_clobbered_arg_p (XEXP (arg->value, 0),
4184169689Skan					       arg->locate.size.constant))
4185169689Skan    sibcall_failure = 1;
4186169689Skan
418718334Speter  /* Don't allow anything left on stack from computation
418818334Speter     of argument to alloca.  */
418990075Sobrien  if (flags & ECF_MAY_BE_ALLOCA)
419018334Speter    do_pending_stack_adjust ();
419118334Speter
419218334Speter  if (arg->value == arg->stack)
419390075Sobrien    /* If the value is already in the stack slot, we are done.  */
419490075Sobrien    ;
419518334Speter  else if (arg->mode != BLKmode)
419618334Speter    {
419790075Sobrien      int size;
419818334Speter
419918334Speter      /* Argument is a scalar, not entirely passed in registers.
420018334Speter	 (If part is passed in registers, arg->partial says how much
420118334Speter	 and emit_push_insn will take care of putting it there.)
420290075Sobrien
420318334Speter	 Push it, and if its size is less than the
420418334Speter	 amount of space allocated to it,
420518334Speter	 also bump stack pointer by the additional space.
420618334Speter	 Note that in C the default argument promotions
420718334Speter	 will prevent such mismatches.  */
420818334Speter
420918334Speter      size = GET_MODE_SIZE (arg->mode);
421018334Speter      /* Compute how much space the push instruction will push.
421118334Speter	 On many machines, pushing a byte will advance the stack
421218334Speter	 pointer by a halfword.  */
421318334Speter#ifdef PUSH_ROUNDING
421418334Speter      size = PUSH_ROUNDING (size);
421518334Speter#endif
421618334Speter      used = size;
421718334Speter
421818334Speter      /* Compute how much space the argument should get:
421918334Speter	 round up to a multiple of the alignment for arguments.  */
422018334Speter      if (none != FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)))
422118334Speter	used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
422218334Speter		 / (PARM_BOUNDARY / BITS_PER_UNIT))
422318334Speter		* (PARM_BOUNDARY / BITS_PER_UNIT));
422418334Speter
422518334Speter      /* This isn't already where we want it on the stack, so put it there.
422618334Speter	 This can either be done with push or copy insns.  */
4227132718Skan      emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), NULL_RTX,
4228117395Skan		      PARM_BOUNDARY, partial, reg, used - size, argblock,
4229132718Skan		      ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4230132718Skan		      ARGS_SIZE_RTX (arg->locate.alignment_pad));
423196263Sobrien
423296263Sobrien      /* Unless this is a partially-in-register argument, the argument is now
423396263Sobrien	 in the stack.  */
423496263Sobrien      if (partial == 0)
423596263Sobrien	arg->value = arg->stack;
423618334Speter    }
423718334Speter  else
423818334Speter    {
423918334Speter      /* BLKmode, at least partly to be pushed.  */
424018334Speter
4241117395Skan      unsigned int parm_align;
424290075Sobrien      int excess;
424318334Speter      rtx size_rtx;
424418334Speter
424518334Speter      /* Pushing a nonscalar.
424618334Speter	 If part is passed in registers, PARTIAL says how much
424718334Speter	 and emit_push_insn will take care of putting it there.  */
424818334Speter
424918334Speter      /* Round its size up to a multiple
425018334Speter	 of the allocation unit for arguments.  */
425118334Speter
4252132718Skan      if (arg->locate.size.var != 0)
425318334Speter	{
425418334Speter	  excess = 0;
4255132718Skan	  size_rtx = ARGS_SIZE_RTX (arg->locate.size);
425618334Speter	}
425718334Speter      else
425818334Speter	{
4259169689Skan	  /* PUSH_ROUNDING has no effect on us, because emit_push_insn
4260169689Skan	     for BLKmode is careful to avoid it.  */
4261169689Skan	  excess = (arg->locate.size.constant
4262169689Skan		    - int_size_in_bytes (TREE_TYPE (pval))
4263169689Skan		    + partial);
4264103445Skan	  size_rtx = expand_expr (size_in_bytes (TREE_TYPE (pval)),
4265103445Skan				  NULL_RTX, TYPE_MODE (sizetype), 0);
426618334Speter	}
426718334Speter
4268169689Skan      parm_align = arg->locate.boundary;
4269117395Skan
4270117395Skan      /* When an argument is padded down, the block is aligned to
4271117395Skan	 PARM_BOUNDARY, but the actual argument isn't.  */
4272117395Skan      if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
4273117395Skan	{
4274132718Skan	  if (arg->locate.size.var)
4275117395Skan	    parm_align = BITS_PER_UNIT;
4276117395Skan	  else if (excess)
4277117395Skan	    {
4278117395Skan	      unsigned int excess_align = (excess & -excess) * BITS_PER_UNIT;
4279117395Skan	      parm_align = MIN (parm_align, excess_align);
4280117395Skan	    }
4281117395Skan	}
4282117395Skan
4283169689Skan      if ((flags & ECF_SIBCALL) && MEM_P (arg->value))
428490075Sobrien	{
428590075Sobrien	  /* emit_push_insn might not work properly if arg->value and
4286132718Skan	     argblock + arg->locate.offset areas overlap.  */
428790075Sobrien	  rtx x = arg->value;
428890075Sobrien	  int i = 0;
428990075Sobrien
429090075Sobrien	  if (XEXP (x, 0) == current_function_internal_arg_pointer
429190075Sobrien	      || (GET_CODE (XEXP (x, 0)) == PLUS
429290075Sobrien		  && XEXP (XEXP (x, 0), 0) ==
429390075Sobrien		     current_function_internal_arg_pointer
429490075Sobrien		  && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
429590075Sobrien	    {
429690075Sobrien	      if (XEXP (x, 0) != current_function_internal_arg_pointer)
429790075Sobrien		i = INTVAL (XEXP (XEXP (x, 0), 1));
429890075Sobrien
4299132718Skan	      /* expand_call should ensure this.  */
4300169689Skan	      gcc_assert (!arg->locate.offset.var
4301260139Spfg			  && arg->locate.size.var == 0
4302169689Skan			  && GET_CODE (size_rtx) == CONST_INT);
430390075Sobrien
4304132718Skan	      if (arg->locate.offset.constant > i)
430590075Sobrien		{
4306132718Skan		  if (arg->locate.offset.constant < i + INTVAL (size_rtx))
430790075Sobrien		    sibcall_failure = 1;
430890075Sobrien		}
4309132718Skan	      else if (arg->locate.offset.constant < i)
431090075Sobrien		{
4311260139Spfg		  /* Use arg->locate.size.constant instead of size_rtx
4312260139Spfg		     because we only care about the part of the argument
4313260139Spfg		     on the stack.  */
4314260139Spfg		  if (i < (arg->locate.offset.constant
4315260139Spfg			   + arg->locate.size.constant))
431690075Sobrien		    sibcall_failure = 1;
431790075Sobrien		}
4318260139Spfg	      else
4319260139Spfg		{
4320260139Spfg		  /* Even though they appear to be at the same location,
4321260139Spfg		     if part of the outgoing argument is in registers,
4322260139Spfg		     they aren't really at the same location.  Check for
4323260139Spfg		     this by making sure that the incoming size is the
4324260139Spfg		     same as the outgoing size.  */
4325260139Spfg		  if (arg->locate.size.constant != INTVAL (size_rtx))
4326260139Spfg		    sibcall_failure = 1;
4327260139Spfg		}
432890075Sobrien	    }
432990075Sobrien	}
433090075Sobrien
433118334Speter      emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
4332117395Skan		      parm_align, partial, reg, excess, argblock,
4333132718Skan		      ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4334132718Skan		      ARGS_SIZE_RTX (arg->locate.alignment_pad));
433518334Speter
433696263Sobrien      /* Unless this is a partially-in-register argument, the argument is now
433796263Sobrien	 in the stack.
433818334Speter
433996263Sobrien	 ??? Unlike the case above, in which we want the actual
434096263Sobrien	 address of the data, so that we can load it directly into a
434196263Sobrien	 register, here we want the address of the stack slot, so that
434296263Sobrien	 it's properly aligned for word-by-word copying or something
434396263Sobrien	 like that.  It's not clear that this is always correct.  */
434496263Sobrien      if (partial == 0)
434596263Sobrien	arg->value = arg->stack_slot;
434696263Sobrien    }
434718334Speter
4348169689Skan  if (arg->reg && GET_CODE (arg->reg) == PARALLEL)
4349169689Skan    {
4350169689Skan      tree type = TREE_TYPE (arg->tree_value);
4351169689Skan      arg->parallel_value
4352169689Skan	= emit_group_load_into_temps (arg->reg, arg->value, type,
4353169689Skan				      int_size_in_bytes (type));
4354169689Skan    }
4355169689Skan
4356117395Skan  /* Mark all slots this store used.  */
4357107590Sobrien  if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
4358107590Sobrien      && argblock && ! variable_size && arg->stack)
4359107590Sobrien    for (i = lower_bound; i < upper_bound; i++)
4360107590Sobrien      stack_usage_map[i] = 1;
4361107590Sobrien
436218334Speter  /* Once we have pushed something, pops can't safely
436318334Speter     be deferred during the rest of the arguments.  */
436418334Speter  NO_DEFER_POP;
436518334Speter
436618334Speter  /* Free any temporary slots made in processing this argument.  Show
436718334Speter     that we might have taken the address of something and pushed that
436818334Speter     as an operand.  */
436918334Speter  preserve_temp_slots (NULL_RTX);
437018334Speter  free_temp_slots ();
437118334Speter  pop_temp_slots ();
437290075Sobrien
437390075Sobrien  return sibcall_failure;
437418334Speter}
4375132718Skan
4376169689Skan/* Nonzero if we do not know how to pass TYPE solely in registers.  */
4377132718Skan
4378169689Skanbool
4379169689Skanmust_pass_in_stack_var_size (enum machine_mode mode ATTRIBUTE_UNUSED,
4380169689Skan			     tree type)
4381169689Skan{
4382169689Skan  if (!type)
4383169689Skan    return false;
4384132718Skan
4385169689Skan  /* If the type has variable size...  */
4386169689Skan  if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4387169689Skan    return true;
4388132718Skan
4389169689Skan  /* If the type is marked as addressable (it is required
4390169689Skan     to be constructed into the stack)...  */
4391169689Skan  if (TREE_ADDRESSABLE (type))
4392169689Skan    return true;
4393132718Skan
4394169689Skan  return false;
4395169689Skan}
4396169689Skan
4397169689Skan/* Another version of the TARGET_MUST_PASS_IN_STACK hook.  This one
4398169689Skan   takes trailing padding of a structure into account.  */
4399169689Skan/* ??? Should be able to merge these two by examining BLOCK_REG_PADDING.  */
4400169689Skan
4401132718Skanbool
4402169689Skanmust_pass_in_stack_var_size_or_pad (enum machine_mode mode, tree type)
4403132718Skan{
4404132718Skan  if (!type)
4405132718Skan    return false;
4406132718Skan
4407132718Skan  /* If the type has variable size...  */
4408132718Skan  if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4409132718Skan    return true;
4410132718Skan
4411132718Skan  /* If the type is marked as addressable (it is required
4412132718Skan     to be constructed into the stack)...  */
4413132718Skan  if (TREE_ADDRESSABLE (type))
4414132718Skan    return true;
4415132718Skan
4416132718Skan  /* If the padding and mode of the type is such that a copy into
4417132718Skan     a register would put it into the wrong part of the register.  */
4418132718Skan  if (mode == BLKmode
4419132718Skan      && int_size_in_bytes (type) % (PARM_BOUNDARY / BITS_PER_UNIT)
4420132718Skan      && (FUNCTION_ARG_PADDING (mode, type)
4421132718Skan	  == (BYTES_BIG_ENDIAN ? upward : downward)))
4422132718Skan    return true;
4423132718Skan
4424132718Skan  return false;
4425132718Skan}
4426