OSDN Git Service

* config/sparc/sparc.md (save_register_windowdi): Add missing mode.
[pf3gnuchains/gcc-fork.git] / gcc / calls.c
1 /* Convert function calls to rtl insns, for GNU C compiler.
2    Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "flags.h"
29 #include "expr.h"
30 #include "optabs.h"
31 #include "libfuncs.h"
32 #include "function.h"
33 #include "regs.h"
34 #include "toplev.h"
35 #include "output.h"
36 #include "tm_p.h"
37 #include "timevar.h"
38 #include "sbitmap.h"
39 #include "langhooks.h"
40 #include "target.h"
41 #include "cgraph.h"
42 #include "except.h"
43
44 /* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits.  */
45 #define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
46
47 /* Data structure and subroutines used within expand_call.  */
48
49 struct arg_data
50 {
51   /* Tree node for this argument.  */
52   tree tree_value;
53   /* Mode for value; TYPE_MODE unless promoted.  */
54   enum machine_mode mode;
55   /* Current RTL value for argument, or 0 if it isn't precomputed.  */
56   rtx value;
57   /* Initially-compute RTL value for argument; only for const functions.  */
58   rtx initial_value;
59   /* Register to pass this argument in, 0 if passed on stack, or an
60      PARALLEL if the arg is to be copied into multiple non-contiguous
61      registers.  */
62   rtx reg;
63   /* Register to pass this argument in when generating tail call sequence.
64      This is not the same register as for normal calls on machines with
65      register windows.  */
66   rtx tail_call_reg;
67   /* If REG is a PARALLEL, this is a copy of VALUE pulled into the correct
68      form for emit_group_move.  */
69   rtx parallel_value;
70   /* If REG was promoted from the actual mode of the argument expression,
71      indicates whether the promotion is sign- or zero-extended.  */
72   int unsignedp;
73   /* Number of registers to use.  0 means put the whole arg in registers.
74      Also 0 if not passed in registers.  */
75   int partial;
76   /* Nonzero if argument must be passed on stack.
77      Note that some arguments may be passed on the stack
78      even though pass_on_stack is zero, just because FUNCTION_ARG says so.
79      pass_on_stack identifies arguments that *cannot* go in registers.  */
80   int pass_on_stack;
81   /* Some fields packaged up for locate_and_pad_parm.  */
82   struct locate_and_pad_arg_data locate;
83   /* Location on the stack at which parameter should be stored.  The store
84      has already been done if STACK == VALUE.  */
85   rtx stack;
86   /* Location on the stack of the start of this argument slot.  This can
87      differ from STACK if this arg pads downward.  This location is known
88      to be aligned to FUNCTION_ARG_BOUNDARY.  */
89   rtx stack_slot;
90   /* Place that this stack area has been saved, if needed.  */
91   rtx save_area;
92   /* If an argument's alignment does not permit direct copying into registers,
93      copy in smaller-sized pieces into pseudos.  These are stored in a
94      block pointed to by this field.  The next field says how many
95      word-sized pseudos we made.  */
96   rtx *aligned_regs;
97   int n_aligned_regs;
98 };
99
100 /* A vector of one char per byte of stack space.  A byte if nonzero if
101    the corresponding stack location has been used.
102    This vector is used to prevent a function call within an argument from
103    clobbering any stack already set up.  */
104 static char *stack_usage_map;
105
106 /* Size of STACK_USAGE_MAP.  */
107 static int highest_outgoing_arg_in_use;
108
109 /* A bitmap of virtual-incoming stack space.  Bit is set if the corresponding
110    stack location's tail call argument has been already stored into the stack.
111    This bitmap is used to prevent sibling call optimization if function tries
112    to use parent's incoming argument slots when they have been already
113    overwritten with tail call arguments.  */
114 static sbitmap stored_args_map;
115
116 /* stack_arg_under_construction is nonzero when an argument may be
117    initialized with a constructor call (including a C function that
118    returns a BLKmode struct) and expand_call must take special action
119    to make sure the object being constructed does not overlap the
120    argument list for the constructor call.  */
121 int stack_arg_under_construction;
122
123 static void emit_call_1 (rtx, tree, tree, tree, HOST_WIDE_INT, HOST_WIDE_INT,
124                          HOST_WIDE_INT, rtx, rtx, int, rtx, int,
125                          CUMULATIVE_ARGS *);
126 static void precompute_register_parameters (int, struct arg_data *, int *);
127 static int store_one_arg (struct arg_data *, rtx, int, int, int);
128 static void store_unaligned_arguments_into_pseudos (struct arg_data *, int);
129 static int finalize_must_preallocate (int, int, struct arg_data *,
130                                       struct args_size *);
131 static void precompute_arguments (int, int, struct arg_data *);
132 static int compute_argument_block_size (int, struct args_size *, int);
133 static void initialize_argument_information (int, struct arg_data *,
134                                              struct args_size *, int, tree,
135                                              tree, CUMULATIVE_ARGS *, int,
136                                              rtx *, int *, int *, int *,
137                                              bool *, bool);
138 static void compute_argument_addresses (struct arg_data *, rtx, int);
139 static rtx rtx_for_function_call (tree, tree);
140 static void load_register_parameters (struct arg_data *, int, rtx *, int,
141                                       int, int *);
142 static rtx emit_library_call_value_1 (int, rtx, rtx, enum libcall_type,
143                                       enum machine_mode, int, va_list);
144 static int special_function_p (tree, int);
145 static int check_sibcall_argument_overlap_1 (rtx);
146 static int check_sibcall_argument_overlap (rtx, struct arg_data *, int);
147
148 static int combine_pending_stack_adjustment_and_call (int, struct args_size *,
149                                                       unsigned int);
150 static tree split_complex_values (tree);
151 static tree split_complex_types (tree);
152
153 #ifdef REG_PARM_STACK_SPACE
154 static rtx save_fixed_argument_area (int, rtx, int *, int *);
155 static void restore_fixed_argument_area (rtx, rtx, int, int);
156 #endif
157 \f
158 /* Force FUNEXP into a form suitable for the address of a CALL,
159    and return that as an rtx.  Also load the static chain register
160    if FNDECL is a nested function.
161
162    CALL_FUSAGE points to a variable holding the prospective
163    CALL_INSN_FUNCTION_USAGE information.  */
164
165 rtx
166 prepare_call_address (rtx funexp, rtx static_chain_value,
167                       rtx *call_fusage, int reg_parm_seen, int sibcallp)
168 {
169   /* Make a valid memory address and copy constants through pseudo-regs,
170      but not for a constant address if -fno-function-cse.  */
171   if (GET_CODE (funexp) != SYMBOL_REF)
172     /* If we are using registers for parameters, force the
173        function address into a register now.  */
174     funexp = ((SMALL_REGISTER_CLASSES && reg_parm_seen)
175               ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
176               : memory_address (FUNCTION_MODE, funexp));
177   else if (! sibcallp)
178     {
179 #ifndef NO_FUNCTION_CSE
180       if (optimize && ! flag_no_function_cse)
181         funexp = force_reg (Pmode, funexp);
182 #endif
183     }
184
185   if (static_chain_value != 0)
186     {
187       static_chain_value = convert_memory_address (Pmode, static_chain_value);
188       emit_move_insn (static_chain_rtx, static_chain_value);
189
190       if (REG_P (static_chain_rtx))
191         use_reg (call_fusage, static_chain_rtx);
192     }
193
194   return funexp;
195 }
196
197 /* Generate instructions to call function FUNEXP,
198    and optionally pop the results.
199    The CALL_INSN is the first insn generated.
200
201    FNDECL is the declaration node of the function.  This is given to the
202    macro RETURN_POPS_ARGS to determine whether this function pops its own args.
203
204    FUNTYPE is the data type of the function.  This is given to the macro
205    RETURN_POPS_ARGS to determine whether this function pops its own args.
206    We used to allow an identifier for library functions, but that doesn't
207    work when the return type is an aggregate type and the calling convention
208    says that the pointer to this aggregate is to be popped by the callee.
209
210    STACK_SIZE is the number of bytes of arguments on the stack,
211    ROUNDED_STACK_SIZE is that number rounded up to
212    PREFERRED_STACK_BOUNDARY; zero if the size is variable.  This is
213    both to put into the call insn and to generate explicit popping
214    code if necessary.
215
216    STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
217    It is zero if this call doesn't want a structure value.
218
219    NEXT_ARG_REG is the rtx that results from executing
220      FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1)
221    just after all the args have had their registers assigned.
222    This could be whatever you like, but normally it is the first
223    arg-register beyond those used for args in this call,
224    or 0 if all the arg-registers are used in this call.
225    It is passed on to `gen_call' so you can put this info in the call insn.
226
227    VALREG is a hard register in which a value is returned,
228    or 0 if the call does not return a value.
229
230    OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
231    the args to this call were processed.
232    We restore `inhibit_defer_pop' to that value.
233
234    CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
235    denote registers used by the called function.  */
236
237 static void
238 emit_call_1 (rtx funexp, tree fntree, tree fndecl ATTRIBUTE_UNUSED,
239              tree funtype ATTRIBUTE_UNUSED,
240              HOST_WIDE_INT stack_size ATTRIBUTE_UNUSED,
241              HOST_WIDE_INT rounded_stack_size,
242              HOST_WIDE_INT struct_value_size ATTRIBUTE_UNUSED,
243              rtx next_arg_reg ATTRIBUTE_UNUSED, rtx valreg,
244              int old_inhibit_defer_pop, rtx call_fusage, int ecf_flags,
245              CUMULATIVE_ARGS *args_so_far ATTRIBUTE_UNUSED)
246 {
247   rtx rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
248   rtx call_insn;
249   int already_popped = 0;
250   HOST_WIDE_INT n_popped = RETURN_POPS_ARGS (fndecl, funtype, stack_size);
251 #if defined (HAVE_call) && defined (HAVE_call_value)
252   rtx struct_value_size_rtx;
253   struct_value_size_rtx = GEN_INT (struct_value_size);
254 #endif
255
256 #ifdef CALL_POPS_ARGS
257   n_popped += CALL_POPS_ARGS (* args_so_far);
258 #endif
259
260   /* Ensure address is valid.  SYMBOL_REF is already valid, so no need,
261      and we don't want to load it into a register as an optimization,
262      because prepare_call_address already did it if it should be done.  */
263   if (GET_CODE (funexp) != SYMBOL_REF)
264     funexp = memory_address (FUNCTION_MODE, funexp);
265
266 #if defined (HAVE_sibcall_pop) && defined (HAVE_sibcall_value_pop)
267   if ((ecf_flags & ECF_SIBCALL)
268       && HAVE_sibcall_pop && HAVE_sibcall_value_pop
269       && (n_popped > 0 || stack_size == 0))
270     {
271       rtx n_pop = GEN_INT (n_popped);
272       rtx pat;
273
274       /* If this subroutine pops its own args, record that in the call insn
275          if possible, for the sake of frame pointer elimination.  */
276
277       if (valreg)
278         pat = GEN_SIBCALL_VALUE_POP (valreg,
279                                      gen_rtx_MEM (FUNCTION_MODE, funexp),
280                                      rounded_stack_size_rtx, next_arg_reg,
281                                      n_pop);
282       else
283         pat = GEN_SIBCALL_POP (gen_rtx_MEM (FUNCTION_MODE, funexp),
284                                rounded_stack_size_rtx, next_arg_reg, n_pop);
285
286       emit_call_insn (pat);
287       already_popped = 1;
288     }
289   else
290 #endif
291
292 #if defined (HAVE_call_pop) && defined (HAVE_call_value_pop)
293   /* If the target has "call" or "call_value" insns, then prefer them
294      if no arguments are actually popped.  If the target does not have
295      "call" or "call_value" insns, then we must use the popping versions
296      even if the call has no arguments to pop.  */
297 #if defined (HAVE_call) && defined (HAVE_call_value)
298   if (HAVE_call && HAVE_call_value && HAVE_call_pop && HAVE_call_value_pop
299       && n_popped > 0 && ! (ecf_flags & ECF_SP_DEPRESSED))
300 #else
301   if (HAVE_call_pop && HAVE_call_value_pop)
302 #endif
303     {
304       rtx n_pop = GEN_INT (n_popped);
305       rtx pat;
306
307       /* If this subroutine pops its own args, record that in the call insn
308          if possible, for the sake of frame pointer elimination.  */
309
310       if (valreg)
311         pat = GEN_CALL_VALUE_POP (valreg,
312                                   gen_rtx_MEM (FUNCTION_MODE, funexp),
313                                   rounded_stack_size_rtx, next_arg_reg, n_pop);
314       else
315         pat = GEN_CALL_POP (gen_rtx_MEM (FUNCTION_MODE, funexp),
316                             rounded_stack_size_rtx, next_arg_reg, n_pop);
317
318       emit_call_insn (pat);
319       already_popped = 1;
320     }
321   else
322 #endif
323
324 #if defined (HAVE_sibcall) && defined (HAVE_sibcall_value)
325   if ((ecf_flags & ECF_SIBCALL)
326       && HAVE_sibcall && HAVE_sibcall_value)
327     {
328       if (valreg)
329         emit_call_insn (GEN_SIBCALL_VALUE (valreg,
330                                            gen_rtx_MEM (FUNCTION_MODE, funexp),
331                                            rounded_stack_size_rtx,
332                                            next_arg_reg, NULL_RTX));
333       else
334         emit_call_insn (GEN_SIBCALL (gen_rtx_MEM (FUNCTION_MODE, funexp),
335                                      rounded_stack_size_rtx, next_arg_reg,
336                                      struct_value_size_rtx));
337     }
338   else
339 #endif
340
341 #if defined (HAVE_call) && defined (HAVE_call_value)
342   if (HAVE_call && HAVE_call_value)
343     {
344       if (valreg)
345         emit_call_insn (GEN_CALL_VALUE (valreg,
346                                         gen_rtx_MEM (FUNCTION_MODE, funexp),
347                                         rounded_stack_size_rtx, next_arg_reg,
348                                         NULL_RTX));
349       else
350         emit_call_insn (GEN_CALL (gen_rtx_MEM (FUNCTION_MODE, funexp),
351                                   rounded_stack_size_rtx, next_arg_reg,
352                                   struct_value_size_rtx));
353     }
354   else
355 #endif
356     gcc_unreachable ();
357
358   /* Find the call we just emitted.  */
359   call_insn = last_call_insn ();
360
361   /* Mark memory as used for "pure" function call.  */
362   if (ecf_flags & ECF_PURE)
363     call_fusage
364       = gen_rtx_EXPR_LIST
365         (VOIDmode,
366          gen_rtx_USE (VOIDmode,
367                       gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode))),
368          call_fusage);
369
370   /* Put the register usage information there.  */
371   add_function_usage_to (call_insn, call_fusage);
372
373   /* If this is a const call, then set the insn's unchanging bit.  */
374   if (ecf_flags & (ECF_CONST | ECF_PURE))
375     CONST_OR_PURE_CALL_P (call_insn) = 1;
376
377   /* If this call can't throw, attach a REG_EH_REGION reg note to that
378      effect.  */
379   if (ecf_flags & ECF_NOTHROW)
380     REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, const0_rtx,
381                                                REG_NOTES (call_insn));
382   else
383     {
384       int rn = lookup_stmt_eh_region (fntree);
385
386       /* If rn < 0, then either (1) tree-ssa not used or (2) doesn't
387          throw, which we already took care of.  */
388       if (rn > 0)
389         REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, GEN_INT (rn),
390                                                    REG_NOTES (call_insn));
391       note_current_region_may_contain_throw ();
392     }
393
394   if (ecf_flags & ECF_NORETURN)
395     REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_NORETURN, const0_rtx,
396                                                REG_NOTES (call_insn));
397   if (ecf_flags & ECF_ALWAYS_RETURN)
398     REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_ALWAYS_RETURN, const0_rtx,
399                                                REG_NOTES (call_insn));
400
401   if (ecf_flags & ECF_RETURNS_TWICE)
402     {
403       REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_SETJMP, const0_rtx,
404                                                  REG_NOTES (call_insn));
405       current_function_calls_setjmp = 1;
406     }
407
408   SIBLING_CALL_P (call_insn) = ((ecf_flags & ECF_SIBCALL) != 0);
409
410   /* Restore this now, so that we do defer pops for this call's args
411      if the context of the call as a whole permits.  */
412   inhibit_defer_pop = old_inhibit_defer_pop;
413
414   if (n_popped > 0)
415     {
416       if (!already_popped)
417         CALL_INSN_FUNCTION_USAGE (call_insn)
418           = gen_rtx_EXPR_LIST (VOIDmode,
419                                gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx),
420                                CALL_INSN_FUNCTION_USAGE (call_insn));
421       rounded_stack_size -= n_popped;
422       rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
423       stack_pointer_delta -= n_popped;
424     }
425
426   if (!ACCUMULATE_OUTGOING_ARGS)
427     {
428       /* If returning from the subroutine does not automatically pop the args,
429          we need an instruction to pop them sooner or later.
430          Perhaps do it now; perhaps just record how much space to pop later.
431
432          If returning from the subroutine does pop the args, indicate that the
433          stack pointer will be changed.  */
434
435       if (rounded_stack_size != 0)
436         {
437           if (ecf_flags & (ECF_SP_DEPRESSED | ECF_NORETURN))
438             /* Just pretend we did the pop.  */
439             stack_pointer_delta -= rounded_stack_size;
440           else if (flag_defer_pop && inhibit_defer_pop == 0
441               && ! (ecf_flags & (ECF_CONST | ECF_PURE)))
442             pending_stack_adjust += rounded_stack_size;
443           else
444             adjust_stack (rounded_stack_size_rtx);
445         }
446     }
447   /* When we accumulate outgoing args, we must avoid any stack manipulations.
448      Restore the stack pointer to its original value now.  Usually
449      ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
450      On  i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
451      popping variants of functions exist as well.
452
453      ??? We may optimize similar to defer_pop above, but it is
454      probably not worthwhile.
455
456      ??? It will be worthwhile to enable combine_stack_adjustments even for
457      such machines.  */
458   else if (n_popped)
459     anti_adjust_stack (GEN_INT (n_popped));
460 }
461
462 /* Determine if the function identified by NAME and FNDECL is one with
463    special properties we wish to know about.
464
465    For example, if the function might return more than one time (setjmp), then
466    set RETURNS_TWICE to a nonzero value.
467
468    Similarly set LONGJMP for if the function is in the longjmp family.
469
470    Set MAY_BE_ALLOCA for any memory allocation function that might allocate
471    space from the stack such as alloca.  */
472
473 static int
474 special_function_p (tree fndecl, int flags)
475 {
476   if (fndecl && DECL_NAME (fndecl)
477       && IDENTIFIER_LENGTH (DECL_NAME (fndecl)) <= 17
478       /* Exclude functions not at the file scope, or not `extern',
479          since they are not the magic functions we would otherwise
480          think they are.
481          FIXME: this should be handled with attributes, not with this
482          hacky imitation of DECL_ASSEMBLER_NAME.  It's (also) wrong
483          because you can declare fork() inside a function if you
484          wish.  */
485       && (DECL_CONTEXT (fndecl) == NULL_TREE
486           || TREE_CODE (DECL_CONTEXT (fndecl)) == TRANSLATION_UNIT_DECL)
487       && TREE_PUBLIC (fndecl))
488     {
489       const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
490       const char *tname = name;
491
492       /* We assume that alloca will always be called by name.  It
493          makes no sense to pass it as a pointer-to-function to
494          anything that does not understand its behavior.  */
495       if (((IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 6
496             && name[0] == 'a'
497             && ! strcmp (name, "alloca"))
498            || (IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 16
499                && name[0] == '_'
500                && ! strcmp (name, "__builtin_alloca"))))
501         flags |= ECF_MAY_BE_ALLOCA;
502
503       /* Disregard prefix _, __ or __x.  */
504       if (name[0] == '_')
505         {
506           if (name[1] == '_' && name[2] == 'x')
507             tname += 3;
508           else if (name[1] == '_')
509             tname += 2;
510           else
511             tname += 1;
512         }
513
514       if (tname[0] == 's')
515         {
516           if ((tname[1] == 'e'
517                && (! strcmp (tname, "setjmp")
518                    || ! strcmp (tname, "setjmp_syscall")))
519               || (tname[1] == 'i'
520                   && ! strcmp (tname, "sigsetjmp"))
521               || (tname[1] == 'a'
522                   && ! strcmp (tname, "savectx")))
523             flags |= ECF_RETURNS_TWICE;
524
525           if (tname[1] == 'i'
526               && ! strcmp (tname, "siglongjmp"))
527             flags |= ECF_NORETURN;
528         }
529       else if ((tname[0] == 'q' && tname[1] == 's'
530                 && ! strcmp (tname, "qsetjmp"))
531                || (tname[0] == 'v' && tname[1] == 'f'
532                    && ! strcmp (tname, "vfork")))
533         flags |= ECF_RETURNS_TWICE;
534
535       else if (tname[0] == 'l' && tname[1] == 'o'
536                && ! strcmp (tname, "longjmp"))
537         flags |= ECF_NORETURN;
538     }
539
540   return flags;
541 }
542
543 /* Return nonzero when tree represent call to longjmp.  */
544
545 int
546 setjmp_call_p (tree fndecl)
547 {
548   return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
549 }
550
551 /* Return true when exp contains alloca call.  */
552 bool
553 alloca_call_p (tree exp)
554 {
555   if (TREE_CODE (exp) == CALL_EXPR
556       && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
557       && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
558           == FUNCTION_DECL)
559       && (special_function_p (TREE_OPERAND (TREE_OPERAND (exp, 0), 0),
560                               0) & ECF_MAY_BE_ALLOCA))
561     return true;
562   return false;
563 }
564
565 /* Detect flags (function attributes) from the function decl or type node.  */
566
567 int
568 flags_from_decl_or_type (tree exp)
569 {
570   int flags = 0;
571   tree type = exp;
572
573   if (DECL_P (exp))
574     {
575       struct cgraph_rtl_info *i = cgraph_rtl_info (exp);
576       type = TREE_TYPE (exp);
577
578       if (i)
579         {
580           if (i->pure_function)
581             flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
582           if (i->const_function)
583             flags |= ECF_CONST | ECF_LIBCALL_BLOCK;
584         }
585
586       /* The function exp may have the `malloc' attribute.  */
587       if (DECL_IS_MALLOC (exp))
588         flags |= ECF_MALLOC;
589
590       /* The function exp may have the `pure' attribute.  */
591       if (DECL_IS_PURE (exp))
592         flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
593
594       if (TREE_NOTHROW (exp))
595         flags |= ECF_NOTHROW;
596
597       if (TREE_READONLY (exp) && ! TREE_THIS_VOLATILE (exp))
598         flags |= ECF_LIBCALL_BLOCK | ECF_CONST;
599
600       flags = special_function_p (exp, flags);
601     }
602   else if (TYPE_P (exp) && TYPE_READONLY (exp) && ! TREE_THIS_VOLATILE (exp))
603     flags |= ECF_CONST;
604
605   if (TREE_THIS_VOLATILE (exp))
606     flags |= ECF_NORETURN;
607
608   /* Mark if the function returns with the stack pointer depressed.   We
609      cannot consider it pure or constant in that case.  */
610   if (TREE_CODE (type) == FUNCTION_TYPE && TYPE_RETURNS_STACK_DEPRESSED (type))
611     {
612       flags |= ECF_SP_DEPRESSED;
613       flags &= ~(ECF_PURE | ECF_CONST | ECF_LIBCALL_BLOCK);
614     }
615
616   return flags;
617 }
618
619 /* Detect flags from a CALL_EXPR.  */
620
621 int
622 call_expr_flags (tree t)
623 {
624   int flags;
625   tree decl = get_callee_fndecl (t);
626
627   if (decl)
628     flags = flags_from_decl_or_type (decl);
629   else
630     {
631       t = TREE_TYPE (TREE_OPERAND (t, 0));
632       if (t && TREE_CODE (t) == POINTER_TYPE)
633         flags = flags_from_decl_or_type (TREE_TYPE (t));
634       else
635         flags = 0;
636     }
637
638   return flags;
639 }
640
641 /* Precompute all register parameters as described by ARGS, storing values
642    into fields within the ARGS array.
643
644    NUM_ACTUALS indicates the total number elements in the ARGS array.
645
646    Set REG_PARM_SEEN if we encounter a register parameter.  */
647
648 static void
649 precompute_register_parameters (int num_actuals, struct arg_data *args,
650                                 int *reg_parm_seen)
651 {
652   int i;
653
654   *reg_parm_seen = 0;
655
656   for (i = 0; i < num_actuals; i++)
657     if (args[i].reg != 0 && ! args[i].pass_on_stack)
658       {
659         *reg_parm_seen = 1;
660
661         if (args[i].value == 0)
662           {
663             push_temp_slots ();
664             args[i].value = expand_expr (args[i].tree_value, NULL_RTX,
665                                          VOIDmode, 0);
666             preserve_temp_slots (args[i].value);
667             pop_temp_slots ();
668           }
669
670         /* If the value is a non-legitimate constant, force it into a
671            pseudo now.  TLS symbols sometimes need a call to resolve.  */
672         if (CONSTANT_P (args[i].value)
673             && !LEGITIMATE_CONSTANT_P (args[i].value))
674           args[i].value = force_reg (args[i].mode, args[i].value);
675
676         /* If we are to promote the function arg to a wider mode,
677            do it now.  */
678
679         if (args[i].mode != TYPE_MODE (TREE_TYPE (args[i].tree_value)))
680           args[i].value
681             = convert_modes (args[i].mode,
682                              TYPE_MODE (TREE_TYPE (args[i].tree_value)),
683                              args[i].value, args[i].unsignedp);
684
685         /* If we're going to have to load the value by parts, pull the
686            parts into pseudos.  The part extraction process can involve
687            non-trivial computation.  */
688         if (GET_CODE (args[i].reg) == PARALLEL)
689           {
690             tree type = TREE_TYPE (args[i].tree_value);
691             args[i].parallel_value
692               = emit_group_load_into_temps (args[i].reg, args[i].value,
693                                             type, int_size_in_bytes (type));
694           }
695
696         /* If the value is expensive, and we are inside an appropriately
697            short loop, put the value into a pseudo and then put the pseudo
698            into the hard reg.
699
700            For small register classes, also do this if this call uses
701            register parameters.  This is to avoid reload conflicts while
702            loading the parameters registers.  */
703
704         else if ((! (REG_P (args[i].value)
705                      || (GET_CODE (args[i].value) == SUBREG
706                          && REG_P (SUBREG_REG (args[i].value)))))
707                  && args[i].mode != BLKmode
708                  && rtx_cost (args[i].value, SET) > COSTS_N_INSNS (1)
709                  && ((SMALL_REGISTER_CLASSES && *reg_parm_seen)
710                      || optimize))
711           args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
712       }
713 }
714
715 #ifdef REG_PARM_STACK_SPACE
716
717   /* The argument list is the property of the called routine and it
718      may clobber it.  If the fixed area has been used for previous
719      parameters, we must save and restore it.  */
720
721 static rtx
722 save_fixed_argument_area (int reg_parm_stack_space, rtx argblock, int *low_to_save, int *high_to_save)
723 {
724   int low;
725   int high;
726
727   /* Compute the boundary of the area that needs to be saved, if any.  */
728   high = reg_parm_stack_space;
729 #ifdef ARGS_GROW_DOWNWARD
730   high += 1;
731 #endif
732   if (high > highest_outgoing_arg_in_use)
733     high = highest_outgoing_arg_in_use;
734
735   for (low = 0; low < high; low++)
736     if (stack_usage_map[low] != 0)
737       {
738         int num_to_save;
739         enum machine_mode save_mode;
740         int delta;
741         rtx stack_area;
742         rtx save_area;
743
744         while (stack_usage_map[--high] == 0)
745           ;
746
747         *low_to_save = low;
748         *high_to_save = high;
749
750         num_to_save = high - low + 1;
751         save_mode = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
752
753         /* If we don't have the required alignment, must do this
754            in BLKmode.  */
755         if ((low & (MIN (GET_MODE_SIZE (save_mode),
756                          BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
757           save_mode = BLKmode;
758
759 #ifdef ARGS_GROW_DOWNWARD
760         delta = -high;
761 #else
762         delta = low;
763 #endif
764         stack_area = gen_rtx_MEM (save_mode,
765                                   memory_address (save_mode,
766                                                   plus_constant (argblock,
767                                                                  delta)));
768
769         set_mem_align (stack_area, PARM_BOUNDARY);
770         if (save_mode == BLKmode)
771           {
772             save_area = assign_stack_temp (BLKmode, num_to_save, 0);
773             emit_block_move (validize_mem (save_area), stack_area,
774                              GEN_INT (num_to_save), BLOCK_OP_CALL_PARM);
775           }
776         else
777           {
778             save_area = gen_reg_rtx (save_mode);
779             emit_move_insn (save_area, stack_area);
780           }
781
782         return save_area;
783       }
784
785   return NULL_RTX;
786 }
787
788 static void
789 restore_fixed_argument_area (rtx save_area, rtx argblock, int high_to_save, int low_to_save)
790 {
791   enum machine_mode save_mode = GET_MODE (save_area);
792   int delta;
793   rtx stack_area;
794
795 #ifdef ARGS_GROW_DOWNWARD
796   delta = -high_to_save;
797 #else
798   delta = low_to_save;
799 #endif
800   stack_area = gen_rtx_MEM (save_mode,
801                             memory_address (save_mode,
802                                             plus_constant (argblock, delta)));
803   set_mem_align (stack_area, PARM_BOUNDARY);
804
805   if (save_mode != BLKmode)
806     emit_move_insn (stack_area, save_area);
807   else
808     emit_block_move (stack_area, validize_mem (save_area),
809                      GEN_INT (high_to_save - low_to_save + 1),
810                      BLOCK_OP_CALL_PARM);
811 }
812 #endif /* REG_PARM_STACK_SPACE */
813
814 /* If any elements in ARGS refer to parameters that are to be passed in
815    registers, but not in memory, and whose alignment does not permit a
816    direct copy into registers.  Copy the values into a group of pseudos
817    which we will later copy into the appropriate hard registers.
818
819    Pseudos for each unaligned argument will be stored into the array
820    args[argnum].aligned_regs.  The caller is responsible for deallocating
821    the aligned_regs array if it is nonzero.  */
822
823 static void
824 store_unaligned_arguments_into_pseudos (struct arg_data *args, int num_actuals)
825 {
826   int i, j;
827
828   for (i = 0; i < num_actuals; i++)
829     if (args[i].reg != 0 && ! args[i].pass_on_stack
830         && args[i].mode == BLKmode
831         && (TYPE_ALIGN (TREE_TYPE (args[i].tree_value))
832             < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
833       {
834         int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
835         int endian_correction = 0;
836
837         if (args[i].partial)
838           {
839             gcc_assert (args[i].partial % UNITS_PER_WORD == 0);
840             args[i].n_aligned_regs = args[i].partial / UNITS_PER_WORD;
841           }
842         else
843           {
844             args[i].n_aligned_regs
845               = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
846           }
847
848         args[i].aligned_regs = xmalloc (sizeof (rtx) * args[i].n_aligned_regs);
849
850         /* Structures smaller than a word are normally aligned to the
851            least significant byte.  On a BYTES_BIG_ENDIAN machine,
852            this means we must skip the empty high order bytes when
853            calculating the bit offset.  */
854         if (bytes < UNITS_PER_WORD
855 #ifdef BLOCK_REG_PADDING
856             && (BLOCK_REG_PADDING (args[i].mode,
857                                    TREE_TYPE (args[i].tree_value), 1)
858                 == downward)
859 #else
860             && BYTES_BIG_ENDIAN
861 #endif
862             )
863           endian_correction = BITS_PER_WORD - bytes * BITS_PER_UNIT;
864
865         for (j = 0; j < args[i].n_aligned_regs; j++)
866           {
867             rtx reg = gen_reg_rtx (word_mode);
868             rtx word = operand_subword_force (args[i].value, j, BLKmode);
869             int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
870
871             args[i].aligned_regs[j] = reg;
872             word = extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
873                                       word_mode, word_mode);
874
875             /* There is no need to restrict this code to loading items
876                in TYPE_ALIGN sized hunks.  The bitfield instructions can
877                load up entire word sized registers efficiently.
878
879                ??? This may not be needed anymore.
880                We use to emit a clobber here but that doesn't let later
881                passes optimize the instructions we emit.  By storing 0 into
882                the register later passes know the first AND to zero out the
883                bitfield being set in the register is unnecessary.  The store
884                of 0 will be deleted as will at least the first AND.  */
885
886             emit_move_insn (reg, const0_rtx);
887
888             bytes -= bitsize / BITS_PER_UNIT;
889             store_bit_field (reg, bitsize, endian_correction, word_mode,
890                              word);
891           }
892       }
893 }
894
895 /* Fill in ARGS_SIZE and ARGS array based on the parameters found in
896    ACTPARMS.
897
898    NUM_ACTUALS is the total number of parameters.
899
900    N_NAMED_ARGS is the total number of named arguments.
901
902    FNDECL is the tree code for the target of this call (if known)
903
904    ARGS_SO_FAR holds state needed by the target to know where to place
905    the next argument.
906
907    REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
908    for arguments which are passed in registers.
909
910    OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
911    and may be modified by this routine.
912
913    OLD_PENDING_ADJ, MUST_PREALLOCATE and FLAGS are pointers to integer
914    flags which may may be modified by this routine.
915
916    MAY_TAILCALL is cleared if we encounter an invisible pass-by-reference
917    that requires allocation of stack space.
918
919    CALL_FROM_THUNK_P is true if this call is the jump from a thunk to
920    the thunked-to function.  */
921
922 static void
923 initialize_argument_information (int num_actuals ATTRIBUTE_UNUSED,
924                                  struct arg_data *args,
925                                  struct args_size *args_size,
926                                  int n_named_args ATTRIBUTE_UNUSED,
927                                  tree actparms, tree fndecl,
928                                  CUMULATIVE_ARGS *args_so_far,
929                                  int reg_parm_stack_space,
930                                  rtx *old_stack_level, int *old_pending_adj,
931                                  int *must_preallocate, int *ecf_flags,
932                                  bool *may_tailcall, bool call_from_thunk_p)
933 {
934   /* 1 if scanning parms front to back, -1 if scanning back to front.  */
935   int inc;
936
937   /* Count arg position in order args appear.  */
938   int argpos;
939
940   int i;
941   tree p;
942
943   args_size->constant = 0;
944   args_size->var = 0;
945
946   /* In this loop, we consider args in the order they are written.
947      We fill up ARGS from the front or from the back if necessary
948      so that in any case the first arg to be pushed ends up at the front.  */
949
950   if (PUSH_ARGS_REVERSED)
951     {
952       i = num_actuals - 1, inc = -1;
953       /* In this case, must reverse order of args
954          so that we compute and push the last arg first.  */
955     }
956   else
957     {
958       i = 0, inc = 1;
959     }
960
961   /* I counts args in order (to be) pushed; ARGPOS counts in order written.  */
962   for (p = actparms, argpos = 0; p; p = TREE_CHAIN (p), i += inc, argpos++)
963     {
964       tree type = TREE_TYPE (TREE_VALUE (p));
965       int unsignedp;
966       enum machine_mode mode;
967
968       args[i].tree_value = TREE_VALUE (p);
969
970       /* Replace erroneous argument with constant zero.  */
971       if (type == error_mark_node || !COMPLETE_TYPE_P (type))
972         args[i].tree_value = integer_zero_node, type = integer_type_node;
973
974       /* If TYPE is a transparent union, pass things the way we would
975          pass the first field of the union.  We have already verified that
976          the modes are the same.  */
977       if (TREE_CODE (type) == UNION_TYPE && TYPE_TRANSPARENT_UNION (type))
978         type = TREE_TYPE (TYPE_FIELDS (type));
979
980       /* Decide where to pass this arg.
981
982          args[i].reg is nonzero if all or part is passed in registers.
983
984          args[i].partial is nonzero if part but not all is passed in registers,
985          and the exact value says how many bytes are passed in registers.
986
987          args[i].pass_on_stack is nonzero if the argument must at least be
988          computed on the stack.  It may then be loaded back into registers
989          if args[i].reg is nonzero.
990
991          These decisions are driven by the FUNCTION_... macros and must agree
992          with those made by function.c.  */
993
994       /* See if this argument should be passed by invisible reference.  */
995       if (pass_by_reference (args_so_far, TYPE_MODE (type),
996                              type, argpos < n_named_args))
997         {
998           bool callee_copies;
999           tree base;
1000
1001           callee_copies
1002             = reference_callee_copied (args_so_far, TYPE_MODE (type),
1003                                        type, argpos < n_named_args);
1004
1005           /* If we're compiling a thunk, pass through invisible references
1006              instead of making a copy.  */
1007           if (call_from_thunk_p
1008               || (callee_copies
1009                   && !TREE_ADDRESSABLE (type)
1010                   && (base = get_base_address (args[i].tree_value))
1011                   && (!DECL_P (base) || MEM_P (DECL_RTL (base)))))
1012             {
1013               /* We can't use sibcalls if a callee-copied argument is
1014                  stored in the current function's frame.  */
1015               if (!call_from_thunk_p && DECL_P (base) && !TREE_STATIC (base))
1016                 *may_tailcall = false;
1017
1018               args[i].tree_value = build_fold_addr_expr (args[i].tree_value);
1019               type = TREE_TYPE (args[i].tree_value);
1020
1021               *ecf_flags &= ~(ECF_CONST | ECF_LIBCALL_BLOCK);
1022             }
1023           else
1024             {
1025               /* We make a copy of the object and pass the address to the
1026                  function being called.  */
1027               rtx copy;
1028
1029               if (!COMPLETE_TYPE_P (type)
1030                   || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
1031                   || (flag_stack_check && ! STACK_CHECK_BUILTIN
1032                       && (0 < compare_tree_int (TYPE_SIZE_UNIT (type),
1033                                                 STACK_CHECK_MAX_VAR_SIZE))))
1034                 {
1035                   /* This is a variable-sized object.  Make space on the stack
1036                      for it.  */
1037                   rtx size_rtx = expr_size (TREE_VALUE (p));
1038
1039                   if (*old_stack_level == 0)
1040                     {
1041                       emit_stack_save (SAVE_BLOCK, old_stack_level, NULL_RTX);
1042                       *old_pending_adj = pending_stack_adjust;
1043                       pending_stack_adjust = 0;
1044                     }
1045
1046                   copy = gen_rtx_MEM (BLKmode,
1047                                       allocate_dynamic_stack_space
1048                                       (size_rtx, NULL_RTX, TYPE_ALIGN (type)));
1049                   set_mem_attributes (copy, type, 1);
1050                 }
1051               else
1052                 copy = assign_temp (type, 0, 1, 0);
1053
1054               store_expr (args[i].tree_value, copy, 0);
1055
1056               if (callee_copies)
1057                 *ecf_flags &= ~(ECF_CONST | ECF_LIBCALL_BLOCK);
1058               else
1059                 *ecf_flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
1060
1061               args[i].tree_value
1062                 = build_fold_addr_expr (make_tree (type, copy));
1063               type = TREE_TYPE (args[i].tree_value);
1064               *may_tailcall = false;
1065             }
1066         }
1067
1068       mode = TYPE_MODE (type);
1069       unsignedp = TYPE_UNSIGNED (type);
1070
1071       if (targetm.calls.promote_function_args (fndecl ? TREE_TYPE (fndecl) : 0))
1072         mode = promote_mode (type, mode, &unsignedp, 1);
1073
1074       args[i].unsignedp = unsignedp;
1075       args[i].mode = mode;
1076
1077       args[i].reg = FUNCTION_ARG (*args_so_far, mode, type,
1078                                   argpos < n_named_args);
1079 #ifdef FUNCTION_INCOMING_ARG
1080       /* If this is a sibling call and the machine has register windows, the
1081          register window has to be unwinded before calling the routine, so
1082          arguments have to go into the incoming registers.  */
1083       args[i].tail_call_reg = FUNCTION_INCOMING_ARG (*args_so_far, mode, type,
1084                                                      argpos < n_named_args);
1085 #else
1086       args[i].tail_call_reg = args[i].reg;
1087 #endif
1088
1089       if (args[i].reg)
1090         args[i].partial
1091           = targetm.calls.arg_partial_bytes (args_so_far, mode, type,
1092                                              argpos < n_named_args);
1093
1094       args[i].pass_on_stack = targetm.calls.must_pass_in_stack (mode, type);
1095
1096       /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
1097          it means that we are to pass this arg in the register(s) designated
1098          by the PARALLEL, but also to pass it in the stack.  */
1099       if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
1100           && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
1101         args[i].pass_on_stack = 1;
1102
1103       /* If this is an addressable type, we must preallocate the stack
1104          since we must evaluate the object into its final location.
1105
1106          If this is to be passed in both registers and the stack, it is simpler
1107          to preallocate.  */
1108       if (TREE_ADDRESSABLE (type)
1109           || (args[i].pass_on_stack && args[i].reg != 0))
1110         *must_preallocate = 1;
1111
1112       /* If this is an addressable type, we cannot pre-evaluate it.  Thus,
1113          we cannot consider this function call constant.  */
1114       if (TREE_ADDRESSABLE (type))
1115         *ecf_flags &= ~ECF_LIBCALL_BLOCK;
1116
1117       /* Compute the stack-size of this argument.  */
1118       if (args[i].reg == 0 || args[i].partial != 0
1119           || reg_parm_stack_space > 0
1120           || args[i].pass_on_stack)
1121         locate_and_pad_parm (mode, type,
1122 #ifdef STACK_PARMS_IN_REG_PARM_AREA
1123                              1,
1124 #else
1125                              args[i].reg != 0,
1126 #endif
1127                              args[i].pass_on_stack ? 0 : args[i].partial,
1128                              fndecl, args_size, &args[i].locate);
1129 #ifdef BLOCK_REG_PADDING
1130       else
1131         /* The argument is passed entirely in registers.  See at which
1132            end it should be padded.  */
1133         args[i].locate.where_pad =
1134           BLOCK_REG_PADDING (mode, type,
1135                              int_size_in_bytes (type) <= UNITS_PER_WORD);
1136 #endif
1137
1138       /* Update ARGS_SIZE, the total stack space for args so far.  */
1139
1140       args_size->constant += args[i].locate.size.constant;
1141       if (args[i].locate.size.var)
1142         ADD_PARM_SIZE (*args_size, args[i].locate.size.var);
1143
1144       /* Increment ARGS_SO_FAR, which has info about which arg-registers
1145          have been used, etc.  */
1146
1147       FUNCTION_ARG_ADVANCE (*args_so_far, TYPE_MODE (type), type,
1148                             argpos < n_named_args);
1149     }
1150 }
1151
1152 /* Update ARGS_SIZE to contain the total size for the argument block.
1153    Return the original constant component of the argument block's size.
1154
1155    REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
1156    for arguments passed in registers.  */
1157
1158 static int
1159 compute_argument_block_size (int reg_parm_stack_space,
1160                              struct args_size *args_size,
1161                              int preferred_stack_boundary ATTRIBUTE_UNUSED)
1162 {
1163   int unadjusted_args_size = args_size->constant;
1164
1165   /* For accumulate outgoing args mode we don't need to align, since the frame
1166      will be already aligned.  Align to STACK_BOUNDARY in order to prevent
1167      backends from generating misaligned frame sizes.  */
1168   if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
1169     preferred_stack_boundary = STACK_BOUNDARY;
1170
1171   /* Compute the actual size of the argument block required.  The variable
1172      and constant sizes must be combined, the size may have to be rounded,
1173      and there may be a minimum required size.  */
1174
1175   if (args_size->var)
1176     {
1177       args_size->var = ARGS_SIZE_TREE (*args_size);
1178       args_size->constant = 0;
1179
1180       preferred_stack_boundary /= BITS_PER_UNIT;
1181       if (preferred_stack_boundary > 1)
1182         {
1183           /* We don't handle this case yet.  To handle it correctly we have
1184              to add the delta, round and subtract the delta.
1185              Currently no machine description requires this support.  */
1186           gcc_assert (!(stack_pointer_delta & (preferred_stack_boundary - 1)));
1187           args_size->var = round_up (args_size->var, preferred_stack_boundary);
1188         }
1189
1190       if (reg_parm_stack_space > 0)
1191         {
1192           args_size->var
1193             = size_binop (MAX_EXPR, args_size->var,
1194                           ssize_int (reg_parm_stack_space));
1195
1196 #ifndef OUTGOING_REG_PARM_STACK_SPACE
1197           /* The area corresponding to register parameters is not to count in
1198              the size of the block we need.  So make the adjustment.  */
1199           args_size->var
1200             = size_binop (MINUS_EXPR, args_size->var,
1201                           ssize_int (reg_parm_stack_space));
1202 #endif
1203         }
1204     }
1205   else
1206     {
1207       preferred_stack_boundary /= BITS_PER_UNIT;
1208       if (preferred_stack_boundary < 1)
1209         preferred_stack_boundary = 1;
1210       args_size->constant = (((args_size->constant
1211                                + stack_pointer_delta
1212                                + preferred_stack_boundary - 1)
1213                               / preferred_stack_boundary
1214                               * preferred_stack_boundary)
1215                              - stack_pointer_delta);
1216
1217       args_size->constant = MAX (args_size->constant,
1218                                  reg_parm_stack_space);
1219
1220 #ifndef OUTGOING_REG_PARM_STACK_SPACE
1221       args_size->constant -= reg_parm_stack_space;
1222 #endif
1223     }
1224   return unadjusted_args_size;
1225 }
1226
1227 /* Precompute parameters as needed for a function call.
1228
1229    FLAGS is mask of ECF_* constants.
1230
1231    NUM_ACTUALS is the number of arguments.
1232
1233    ARGS is an array containing information for each argument; this
1234    routine fills in the INITIAL_VALUE and VALUE fields for each
1235    precomputed argument.  */
1236
1237 static void
1238 precompute_arguments (int flags, int num_actuals, struct arg_data *args)
1239 {
1240   int i;
1241
1242   /* If this is a libcall, then precompute all arguments so that we do not
1243      get extraneous instructions emitted as part of the libcall sequence.  */
1244   if ((flags & ECF_LIBCALL_BLOCK) == 0)
1245     return;
1246
1247   for (i = 0; i < num_actuals; i++)
1248     {
1249       enum machine_mode mode;
1250
1251       /* If this is an addressable type, we cannot pre-evaluate it.  */
1252       gcc_assert (!TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value)));
1253
1254       args[i].initial_value = args[i].value
1255         = expand_expr (args[i].tree_value, NULL_RTX, VOIDmode, 0);
1256
1257       mode = TYPE_MODE (TREE_TYPE (args[i].tree_value));
1258       if (mode != args[i].mode)
1259         {
1260           args[i].value
1261             = convert_modes (args[i].mode, mode,
1262                              args[i].value, args[i].unsignedp);
1263 #if defined(PROMOTE_FUNCTION_MODE) && !defined(PROMOTE_MODE)
1264           /* CSE will replace this only if it contains args[i].value
1265              pseudo, so convert it down to the declared mode using
1266              a SUBREG.  */
1267           if (REG_P (args[i].value)
1268               && GET_MODE_CLASS (args[i].mode) == MODE_INT)
1269             {
1270               args[i].initial_value
1271                 = gen_lowpart_SUBREG (mode, args[i].value);
1272               SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1273               SUBREG_PROMOTED_UNSIGNED_SET (args[i].initial_value,
1274                                             args[i].unsignedp);
1275             }
1276 #endif
1277         }
1278     }
1279 }
1280
1281 /* Given the current state of MUST_PREALLOCATE and information about
1282    arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
1283    compute and return the final value for MUST_PREALLOCATE.  */
1284
1285 static int
1286 finalize_must_preallocate (int must_preallocate, int num_actuals, struct arg_data *args, struct args_size *args_size)
1287 {
1288   /* See if we have or want to preallocate stack space.
1289
1290      If we would have to push a partially-in-regs parm
1291      before other stack parms, preallocate stack space instead.
1292
1293      If the size of some parm is not a multiple of the required stack
1294      alignment, we must preallocate.
1295
1296      If the total size of arguments that would otherwise create a copy in
1297      a temporary (such as a CALL) is more than half the total argument list
1298      size, preallocation is faster.
1299
1300      Another reason to preallocate is if we have a machine (like the m88k)
1301      where stack alignment is required to be maintained between every
1302      pair of insns, not just when the call is made.  However, we assume here
1303      that such machines either do not have push insns (and hence preallocation
1304      would occur anyway) or the problem is taken care of with
1305      PUSH_ROUNDING.  */
1306
1307   if (! must_preallocate)
1308     {
1309       int partial_seen = 0;
1310       int copy_to_evaluate_size = 0;
1311       int i;
1312
1313       for (i = 0; i < num_actuals && ! must_preallocate; i++)
1314         {
1315           if (args[i].partial > 0 && ! args[i].pass_on_stack)
1316             partial_seen = 1;
1317           else if (partial_seen && args[i].reg == 0)
1318             must_preallocate = 1;
1319
1320           if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1321               && (TREE_CODE (args[i].tree_value) == CALL_EXPR
1322                   || TREE_CODE (args[i].tree_value) == TARGET_EXPR
1323                   || TREE_CODE (args[i].tree_value) == COND_EXPR
1324                   || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
1325             copy_to_evaluate_size
1326               += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1327         }
1328
1329       if (copy_to_evaluate_size * 2 >= args_size->constant
1330           && args_size->constant > 0)
1331         must_preallocate = 1;
1332     }
1333   return must_preallocate;
1334 }
1335
1336 /* If we preallocated stack space, compute the address of each argument
1337    and store it into the ARGS array.
1338
1339    We need not ensure it is a valid memory address here; it will be
1340    validized when it is used.
1341
1342    ARGBLOCK is an rtx for the address of the outgoing arguments.  */
1343
1344 static void
1345 compute_argument_addresses (struct arg_data *args, rtx argblock, int num_actuals)
1346 {
1347   if (argblock)
1348     {
1349       rtx arg_reg = argblock;
1350       int i, arg_offset = 0;
1351
1352       if (GET_CODE (argblock) == PLUS)
1353         arg_reg = XEXP (argblock, 0), arg_offset = INTVAL (XEXP (argblock, 1));
1354
1355       for (i = 0; i < num_actuals; i++)
1356         {
1357           rtx offset = ARGS_SIZE_RTX (args[i].locate.offset);
1358           rtx slot_offset = ARGS_SIZE_RTX (args[i].locate.slot_offset);
1359           rtx addr;
1360
1361           /* Skip this parm if it will not be passed on the stack.  */
1362           if (! args[i].pass_on_stack && args[i].reg != 0)
1363             continue;
1364
1365           if (GET_CODE (offset) == CONST_INT)
1366             addr = plus_constant (arg_reg, INTVAL (offset));
1367           else
1368             addr = gen_rtx_PLUS (Pmode, arg_reg, offset);
1369
1370           addr = plus_constant (addr, arg_offset);
1371           args[i].stack = gen_rtx_MEM (args[i].mode, addr);
1372           set_mem_align (args[i].stack, PARM_BOUNDARY);
1373           set_mem_attributes (args[i].stack,
1374                               TREE_TYPE (args[i].tree_value), 1);
1375
1376           if (GET_CODE (slot_offset) == CONST_INT)
1377             addr = plus_constant (arg_reg, INTVAL (slot_offset));
1378           else
1379             addr = gen_rtx_PLUS (Pmode, arg_reg, slot_offset);
1380
1381           addr = plus_constant (addr, arg_offset);
1382           args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
1383           set_mem_align (args[i].stack_slot, PARM_BOUNDARY);
1384           set_mem_attributes (args[i].stack_slot,
1385                               TREE_TYPE (args[i].tree_value), 1);
1386
1387           /* Function incoming arguments may overlap with sibling call
1388              outgoing arguments and we cannot allow reordering of reads
1389              from function arguments with stores to outgoing arguments
1390              of sibling calls.  */
1391           set_mem_alias_set (args[i].stack, 0);
1392           set_mem_alias_set (args[i].stack_slot, 0);
1393         }
1394     }
1395 }
1396
1397 /* Given a FNDECL and EXP, return an rtx suitable for use as a target address
1398    in a call instruction.
1399
1400    FNDECL is the tree node for the target function.  For an indirect call
1401    FNDECL will be NULL_TREE.
1402
1403    ADDR is the operand 0 of CALL_EXPR for this call.  */
1404
1405 static rtx
1406 rtx_for_function_call (tree fndecl, tree addr)
1407 {
1408   rtx funexp;
1409
1410   /* Get the function to call, in the form of RTL.  */
1411   if (fndecl)
1412     {
1413       /* If this is the first use of the function, see if we need to
1414          make an external definition for it.  */
1415       if (! TREE_USED (fndecl))
1416         {
1417           assemble_external (fndecl);
1418           TREE_USED (fndecl) = 1;
1419         }
1420
1421       /* Get a SYMBOL_REF rtx for the function address.  */
1422       funexp = XEXP (DECL_RTL (fndecl), 0);
1423     }
1424   else
1425     /* Generate an rtx (probably a pseudo-register) for the address.  */
1426     {
1427       push_temp_slots ();
1428       funexp = expand_expr (addr, NULL_RTX, VOIDmode, 0);
1429       pop_temp_slots ();        /* FUNEXP can't be BLKmode.  */
1430     }
1431   return funexp;
1432 }
1433
1434 /* Do the register loads required for any wholly-register parms or any
1435    parms which are passed both on the stack and in a register.  Their
1436    expressions were already evaluated.
1437
1438    Mark all register-parms as living through the call, putting these USE
1439    insns in the CALL_INSN_FUNCTION_USAGE field.
1440
1441    When IS_SIBCALL, perform the check_sibcall_overlap_argument_overlap
1442    checking, setting *SIBCALL_FAILURE if appropriate.  */
1443
1444 static void
1445 load_register_parameters (struct arg_data *args, int num_actuals,
1446                           rtx *call_fusage, int flags, int is_sibcall,
1447                           int *sibcall_failure)
1448 {
1449   int i, j;
1450
1451   for (i = 0; i < num_actuals; i++)
1452     {
1453       rtx reg = ((flags & ECF_SIBCALL)
1454                  ? args[i].tail_call_reg : args[i].reg);
1455       if (reg)
1456         {
1457           int partial = args[i].partial;
1458           int nregs;
1459           int size = 0;
1460           rtx before_arg = get_last_insn ();
1461           /* Set to non-negative if must move a word at a time, even if just
1462              one word (e.g, partial == 1 && mode == DFmode).  Set to -1 if
1463              we just use a normal move insn.  This value can be zero if the
1464              argument is a zero size structure with no fields.  */
1465           nregs = -1;
1466           if (GET_CODE (reg) == PARALLEL)
1467             ;
1468           else if (partial)
1469             {
1470               gcc_assert (partial % UNITS_PER_WORD == 0);
1471               nregs = partial / UNITS_PER_WORD;
1472             }
1473           else if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode)
1474             {
1475               size = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1476               nregs = (size + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
1477             }
1478           else
1479             size = GET_MODE_SIZE (args[i].mode);
1480
1481           /* Handle calls that pass values in multiple non-contiguous
1482              locations.  The Irix 6 ABI has examples of this.  */
1483
1484           if (GET_CODE (reg) == PARALLEL)
1485             emit_group_move (reg, args[i].parallel_value);
1486
1487           /* If simple case, just do move.  If normal partial, store_one_arg
1488              has already loaded the register for us.  In all other cases,
1489              load the register(s) from memory.  */
1490
1491           else if (nregs == -1)
1492             {
1493               emit_move_insn (reg, args[i].value);
1494 #ifdef BLOCK_REG_PADDING
1495               /* Handle case where we have a value that needs shifting
1496                  up to the msb.  eg. a QImode value and we're padding
1497                  upward on a BYTES_BIG_ENDIAN machine.  */
1498               if (size < UNITS_PER_WORD
1499                   && (args[i].locate.where_pad
1500                       == (BYTES_BIG_ENDIAN ? upward : downward)))
1501                 {
1502                   rtx x;
1503                   int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
1504
1505                   /* Assigning REG here rather than a temp makes CALL_FUSAGE
1506                      report the whole reg as used.  Strictly speaking, the
1507                      call only uses SIZE bytes at the msb end, but it doesn't
1508                      seem worth generating rtl to say that.  */
1509                   reg = gen_rtx_REG (word_mode, REGNO (reg));
1510                   x = expand_shift (LSHIFT_EXPR, word_mode, reg,
1511                                     build_int_cst (NULL_TREE, shift),
1512                                     reg, 1);
1513                   if (x != reg)
1514                     emit_move_insn (reg, x);
1515                 }
1516 #endif
1517             }
1518
1519           /* If we have pre-computed the values to put in the registers in
1520              the case of non-aligned structures, copy them in now.  */
1521
1522           else if (args[i].n_aligned_regs != 0)
1523             for (j = 0; j < args[i].n_aligned_regs; j++)
1524               emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
1525                               args[i].aligned_regs[j]);
1526
1527           else if (partial == 0 || args[i].pass_on_stack)
1528             {
1529               rtx mem = validize_mem (args[i].value);
1530
1531               /* Handle a BLKmode that needs shifting.  */
1532               if (nregs == 1 && size < UNITS_PER_WORD
1533 #ifdef BLOCK_REG_PADDING
1534                   && args[i].locate.where_pad == downward
1535 #else
1536                   && BYTES_BIG_ENDIAN
1537 #endif
1538                  )
1539                 {
1540                   rtx tem = operand_subword_force (mem, 0, args[i].mode);
1541                   rtx ri = gen_rtx_REG (word_mode, REGNO (reg));
1542                   rtx x = gen_reg_rtx (word_mode);
1543                   int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
1544                   enum tree_code dir = BYTES_BIG_ENDIAN ? RSHIFT_EXPR
1545                                                         : LSHIFT_EXPR;
1546
1547                   emit_move_insn (x, tem);
1548                   x = expand_shift (dir, word_mode, x,
1549                                     build_int_cst (NULL_TREE, shift),
1550                                     ri, 1);
1551                   if (x != ri)
1552                     emit_move_insn (ri, x);
1553                 }
1554               else
1555                 move_block_to_reg (REGNO (reg), mem, nregs, args[i].mode);
1556             }
1557
1558           /* When a parameter is a block, and perhaps in other cases, it is
1559              possible that it did a load from an argument slot that was
1560              already clobbered.  */
1561           if (is_sibcall
1562               && check_sibcall_argument_overlap (before_arg, &args[i], 0))
1563             *sibcall_failure = 1;
1564
1565           /* Handle calls that pass values in multiple non-contiguous
1566              locations.  The Irix 6 ABI has examples of this.  */
1567           if (GET_CODE (reg) == PARALLEL)
1568             use_group_regs (call_fusage, reg);
1569           else if (nregs == -1)
1570             use_reg (call_fusage, reg);
1571           else if (nregs > 0)
1572             use_regs (call_fusage, REGNO (reg), nregs);
1573         }
1574     }
1575 }
1576
1577 /* We need to pop PENDING_STACK_ADJUST bytes.  But, if the arguments
1578    wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
1579    bytes, then we would need to push some additional bytes to pad the
1580    arguments.  So, we compute an adjust to the stack pointer for an
1581    amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
1582    bytes.  Then, when the arguments are pushed the stack will be perfectly
1583    aligned.  ARGS_SIZE->CONSTANT is set to the number of bytes that should
1584    be popped after the call.  Returns the adjustment.  */
1585
1586 static int
1587 combine_pending_stack_adjustment_and_call (int unadjusted_args_size,
1588                                            struct args_size *args_size,
1589                                            unsigned int preferred_unit_stack_boundary)
1590 {
1591   /* The number of bytes to pop so that the stack will be
1592      under-aligned by UNADJUSTED_ARGS_SIZE bytes.  */
1593   HOST_WIDE_INT adjustment;
1594   /* The alignment of the stack after the arguments are pushed, if we
1595      just pushed the arguments without adjust the stack here.  */
1596   unsigned HOST_WIDE_INT unadjusted_alignment;
1597
1598   unadjusted_alignment
1599     = ((stack_pointer_delta + unadjusted_args_size)
1600        % preferred_unit_stack_boundary);
1601
1602   /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
1603      as possible -- leaving just enough left to cancel out the
1604      UNADJUSTED_ALIGNMENT.  In other words, we want to ensure that the
1605      PENDING_STACK_ADJUST is non-negative, and congruent to
1606      -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY.  */
1607
1608   /* Begin by trying to pop all the bytes.  */
1609   unadjusted_alignment
1610     = (unadjusted_alignment
1611        - (pending_stack_adjust % preferred_unit_stack_boundary));
1612   adjustment = pending_stack_adjust;
1613   /* Push enough additional bytes that the stack will be aligned
1614      after the arguments are pushed.  */
1615   if (preferred_unit_stack_boundary > 1)
1616     {
1617       if (unadjusted_alignment > 0)
1618         adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
1619       else
1620         adjustment += unadjusted_alignment;
1621     }
1622
1623   /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
1624      bytes after the call.  The right number is the entire
1625      PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
1626      by the arguments in the first place.  */
1627   args_size->constant
1628     = pending_stack_adjust - adjustment + unadjusted_args_size;
1629
1630   return adjustment;
1631 }
1632
1633 /* Scan X expression if it does not dereference any argument slots
1634    we already clobbered by tail call arguments (as noted in stored_args_map
1635    bitmap).
1636    Return nonzero if X expression dereferences such argument slots,
1637    zero otherwise.  */
1638
1639 static int
1640 check_sibcall_argument_overlap_1 (rtx x)
1641 {
1642   RTX_CODE code;
1643   int i, j;
1644   unsigned int k;
1645   const char *fmt;
1646
1647   if (x == NULL_RTX)
1648     return 0;
1649
1650   code = GET_CODE (x);
1651
1652   if (code == MEM)
1653     {
1654       if (XEXP (x, 0) == current_function_internal_arg_pointer)
1655         i = 0;
1656       else if (GET_CODE (XEXP (x, 0)) == PLUS
1657                && XEXP (XEXP (x, 0), 0) ==
1658                   current_function_internal_arg_pointer
1659                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
1660         i = INTVAL (XEXP (XEXP (x, 0), 1));
1661       else
1662         return 1;
1663
1664 #ifdef ARGS_GROW_DOWNWARD
1665       i = -i - GET_MODE_SIZE (GET_MODE (x));
1666 #endif
1667
1668       for (k = 0; k < GET_MODE_SIZE (GET_MODE (x)); k++)
1669         if (i + k < stored_args_map->n_bits
1670             && TEST_BIT (stored_args_map, i + k))
1671           return 1;
1672
1673       return 0;
1674     }
1675
1676   /* Scan all subexpressions.  */
1677   fmt = GET_RTX_FORMAT (code);
1678   for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
1679     {
1680       if (*fmt == 'e')
1681         {
1682           if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
1683             return 1;
1684         }
1685       else if (*fmt == 'E')
1686         {
1687           for (j = 0; j < XVECLEN (x, i); j++)
1688             if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
1689               return 1;
1690         }
1691     }
1692   return 0;
1693 }
1694
1695 /* Scan sequence after INSN if it does not dereference any argument slots
1696    we already clobbered by tail call arguments (as noted in stored_args_map
1697    bitmap).  If MARK_STORED_ARGS_MAP, add stack slots for ARG to
1698    stored_args_map bitmap afterwards (when ARG is a register MARK_STORED_ARGS_MAP
1699    should be 0).  Return nonzero if sequence after INSN dereferences such argument
1700    slots, zero otherwise.  */
1701
1702 static int
1703 check_sibcall_argument_overlap (rtx insn, struct arg_data *arg, int mark_stored_args_map)
1704 {
1705   int low, high;
1706
1707   if (insn == NULL_RTX)
1708     insn = get_insns ();
1709   else
1710     insn = NEXT_INSN (insn);
1711
1712   for (; insn; insn = NEXT_INSN (insn))
1713     if (INSN_P (insn)
1714         && check_sibcall_argument_overlap_1 (PATTERN (insn)))
1715       break;
1716
1717   if (mark_stored_args_map)
1718     {
1719 #ifdef ARGS_GROW_DOWNWARD
1720       low = -arg->locate.slot_offset.constant - arg->locate.size.constant;
1721 #else
1722       low = arg->locate.slot_offset.constant;
1723 #endif
1724
1725       for (high = low + arg->locate.size.constant; low < high; low++)
1726         SET_BIT (stored_args_map, low);
1727     }
1728   return insn != NULL_RTX;
1729 }
1730
1731 /* Given that a function returns a value of mode MODE at the most
1732    significant end of hard register VALUE, shift VALUE left or right
1733    as specified by LEFT_P.  Return true if some action was needed.  */
1734
1735 bool
1736 shift_return_value (enum machine_mode mode, bool left_p, rtx value)
1737 {
1738   HOST_WIDE_INT shift;
1739
1740   gcc_assert (REG_P (value) && HARD_REGISTER_P (value));
1741   shift = GET_MODE_BITSIZE (GET_MODE (value)) - GET_MODE_BITSIZE (mode);
1742   if (shift == 0)
1743     return false;
1744
1745   /* Use ashr rather than lshr for right shifts.  This is for the benefit
1746      of the MIPS port, which requires SImode values to be sign-extended
1747      when stored in 64-bit registers.  */
1748   if (!force_expand_binop (GET_MODE (value), left_p ? ashl_optab : ashr_optab,
1749                            value, GEN_INT (shift), value, 1, OPTAB_WIDEN))
1750     gcc_unreachable ();
1751   return true;
1752 }
1753
1754 /* Remove all REG_EQUIV notes found in the insn chain.  */
1755
1756 static void
1757 purge_reg_equiv_notes (void)
1758 {
1759   rtx insn;
1760
1761   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1762     {
1763       while (1)
1764         {
1765           rtx note = find_reg_note (insn, REG_EQUIV, 0);
1766           if (note)
1767             {
1768               /* Remove the note and keep looking at the notes for
1769                  this insn.  */
1770               remove_note (insn, note);
1771               continue;
1772             }
1773           break;
1774         }
1775     }
1776 }
1777
1778 /* Generate all the code for a function call
1779    and return an rtx for its value.
1780    Store the value in TARGET (specified as an rtx) if convenient.
1781    If the value is stored in TARGET then TARGET is returned.
1782    If IGNORE is nonzero, then we ignore the value of the function call.  */
1783
1784 rtx
1785 expand_call (tree exp, rtx target, int ignore)
1786 {
1787   /* Nonzero if we are currently expanding a call.  */
1788   static int currently_expanding_call = 0;
1789
1790   /* List of actual parameters.  */
1791   tree actparms = TREE_OPERAND (exp, 1);
1792   /* RTX for the function to be called.  */
1793   rtx funexp;
1794   /* Sequence of insns to perform a normal "call".  */
1795   rtx normal_call_insns = NULL_RTX;
1796   /* Sequence of insns to perform a tail "call".  */
1797   rtx tail_call_insns = NULL_RTX;
1798   /* Data type of the function.  */
1799   tree funtype;
1800   tree type_arg_types;
1801   /* Declaration of the function being called,
1802      or 0 if the function is computed (not known by name).  */
1803   tree fndecl = 0;
1804   /* The type of the function being called.  */
1805   tree fntype;
1806   bool try_tail_call = CALL_EXPR_TAILCALL (exp);
1807   int pass;
1808
1809   /* Register in which non-BLKmode value will be returned,
1810      or 0 if no value or if value is BLKmode.  */
1811   rtx valreg;
1812   /* Address where we should return a BLKmode value;
1813      0 if value not BLKmode.  */
1814   rtx structure_value_addr = 0;
1815   /* Nonzero if that address is being passed by treating it as
1816      an extra, implicit first parameter.  Otherwise,
1817      it is passed by being copied directly into struct_value_rtx.  */
1818   int structure_value_addr_parm = 0;
1819   /* Size of aggregate value wanted, or zero if none wanted
1820      or if we are using the non-reentrant PCC calling convention
1821      or expecting the value in registers.  */
1822   HOST_WIDE_INT struct_value_size = 0;
1823   /* Nonzero if called function returns an aggregate in memory PCC style,
1824      by returning the address of where to find it.  */
1825   int pcc_struct_value = 0;
1826   rtx struct_value = 0;
1827
1828   /* Number of actual parameters in this call, including struct value addr.  */
1829   int num_actuals;
1830   /* Number of named args.  Args after this are anonymous ones
1831      and they must all go on the stack.  */
1832   int n_named_args;
1833
1834   /* Vector of information about each argument.
1835      Arguments are numbered in the order they will be pushed,
1836      not the order they are written.  */
1837   struct arg_data *args;
1838
1839   /* Total size in bytes of all the stack-parms scanned so far.  */
1840   struct args_size args_size;
1841   struct args_size adjusted_args_size;
1842   /* Size of arguments before any adjustments (such as rounding).  */
1843   int unadjusted_args_size;
1844   /* Data on reg parms scanned so far.  */
1845   CUMULATIVE_ARGS args_so_far;
1846   /* Nonzero if a reg parm has been scanned.  */
1847   int reg_parm_seen;
1848   /* Nonzero if this is an indirect function call.  */
1849
1850   /* Nonzero if we must avoid push-insns in the args for this call.
1851      If stack space is allocated for register parameters, but not by the
1852      caller, then it is preallocated in the fixed part of the stack frame.
1853      So the entire argument block must then be preallocated (i.e., we
1854      ignore PUSH_ROUNDING in that case).  */
1855
1856   int must_preallocate = !PUSH_ARGS;
1857
1858   /* Size of the stack reserved for parameter registers.  */
1859   int reg_parm_stack_space = 0;
1860
1861   /* Address of space preallocated for stack parms
1862      (on machines that lack push insns), or 0 if space not preallocated.  */
1863   rtx argblock = 0;
1864
1865   /* Mask of ECF_ flags.  */
1866   int flags = 0;
1867 #ifdef REG_PARM_STACK_SPACE
1868   /* Define the boundary of the register parm stack space that needs to be
1869      saved, if any.  */
1870   int low_to_save, high_to_save;
1871   rtx save_area = 0;            /* Place that it is saved */
1872 #endif
1873
1874   int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
1875   char *initial_stack_usage_map = stack_usage_map;
1876
1877   int old_stack_allocated;
1878
1879   /* State variables to track stack modifications.  */
1880   rtx old_stack_level = 0;
1881   int old_stack_arg_under_construction = 0;
1882   int old_pending_adj = 0;
1883   int old_inhibit_defer_pop = inhibit_defer_pop;
1884
1885   /* Some stack pointer alterations we make are performed via
1886      allocate_dynamic_stack_space. This modifies the stack_pointer_delta,
1887      which we then also need to save/restore along the way.  */
1888   int old_stack_pointer_delta = 0;
1889
1890   rtx call_fusage;
1891   tree p = TREE_OPERAND (exp, 0);
1892   tree addr = TREE_OPERAND (exp, 0);
1893   int i;
1894   /* The alignment of the stack, in bits.  */
1895   unsigned HOST_WIDE_INT preferred_stack_boundary;
1896   /* The alignment of the stack, in bytes.  */
1897   unsigned HOST_WIDE_INT preferred_unit_stack_boundary;
1898   /* The static chain value to use for this call.  */
1899   rtx static_chain_value;
1900   /* See if this is "nothrow" function call.  */
1901   if (TREE_NOTHROW (exp))
1902     flags |= ECF_NOTHROW;
1903
1904   /* See if we can find a DECL-node for the actual function, and get the
1905      function attributes (flags) from the function decl or type node.  */
1906   fndecl = get_callee_fndecl (exp);
1907   if (fndecl)
1908     {
1909       fntype = TREE_TYPE (fndecl);
1910       flags |= flags_from_decl_or_type (fndecl);
1911     }
1912   else
1913     {
1914       fntype = TREE_TYPE (TREE_TYPE (p));
1915       flags |= flags_from_decl_or_type (fntype);
1916     }
1917
1918   struct_value = targetm.calls.struct_value_rtx (fntype, 0);
1919
1920   /* Warn if this value is an aggregate type,
1921      regardless of which calling convention we are using for it.  */
1922   if (warn_aggregate_return && AGGREGATE_TYPE_P (TREE_TYPE (exp)))
1923     warning ("function call has aggregate value");
1924
1925   /* If the result of a pure or const function call is ignored (or void),
1926      and none of its arguments are volatile, we can avoid expanding the
1927      call and just evaluate the arguments for side-effects.  */
1928   if ((flags & (ECF_CONST | ECF_PURE))
1929       && (ignore || target == const0_rtx
1930           || TYPE_MODE (TREE_TYPE (exp)) == VOIDmode))
1931     {
1932       bool volatilep = false;
1933       tree arg;
1934
1935       for (arg = actparms; arg; arg = TREE_CHAIN (arg))
1936         if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
1937           {
1938             volatilep = true;
1939             break;
1940           }
1941
1942       if (! volatilep)
1943         {
1944           for (arg = actparms; arg; arg = TREE_CHAIN (arg))
1945             expand_expr (TREE_VALUE (arg), const0_rtx,
1946                          VOIDmode, EXPAND_NORMAL);
1947           return const0_rtx;
1948         }
1949     }
1950
1951 #ifdef REG_PARM_STACK_SPACE
1952   reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
1953 #endif
1954
1955 #ifndef OUTGOING_REG_PARM_STACK_SPACE
1956   if (reg_parm_stack_space > 0 && PUSH_ARGS)
1957     must_preallocate = 1;
1958 #endif
1959
1960   /* Set up a place to return a structure.  */
1961
1962   /* Cater to broken compilers.  */
1963   if (aggregate_value_p (exp, fndecl))
1964     {
1965       /* This call returns a big structure.  */
1966       flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
1967
1968 #ifdef PCC_STATIC_STRUCT_RETURN
1969       {
1970         pcc_struct_value = 1;
1971       }
1972 #else /* not PCC_STATIC_STRUCT_RETURN */
1973       {
1974         struct_value_size = int_size_in_bytes (TREE_TYPE (exp));
1975
1976         if (CALL_EXPR_HAS_RETURN_SLOT_ADDR (exp))
1977           {
1978             /* The structure value address arg is already in actparms.
1979                Pull it out.  It might be nice to just leave it there, but
1980                we need to set structure_value_addr.  */
1981             tree return_arg = TREE_VALUE (actparms);
1982             actparms = TREE_CHAIN (actparms);
1983             structure_value_addr = expand_expr (return_arg, NULL_RTX,
1984                                                 VOIDmode, EXPAND_NORMAL);
1985           }
1986         else if (target && MEM_P (target))
1987           structure_value_addr = XEXP (target, 0);
1988         else
1989           {
1990             /* For variable-sized objects, we must be called with a target
1991                specified.  If we were to allocate space on the stack here,
1992                we would have no way of knowing when to free it.  */
1993             rtx d = assign_temp (TREE_TYPE (exp), 1, 1, 1);
1994
1995             mark_temp_addr_taken (d);
1996             structure_value_addr = XEXP (d, 0);
1997             target = 0;
1998           }
1999       }
2000 #endif /* not PCC_STATIC_STRUCT_RETURN */
2001     }
2002
2003   /* Figure out the amount to which the stack should be aligned.  */
2004   preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
2005   if (fndecl)
2006     {
2007       struct cgraph_rtl_info *i = cgraph_rtl_info (fndecl);
2008       if (i && i->preferred_incoming_stack_boundary)
2009         preferred_stack_boundary = i->preferred_incoming_stack_boundary;
2010     }
2011
2012   /* Operand 0 is a pointer-to-function; get the type of the function.  */
2013   funtype = TREE_TYPE (addr);
2014   gcc_assert (POINTER_TYPE_P (funtype));
2015   funtype = TREE_TYPE (funtype);
2016
2017   /* Munge the tree to split complex arguments into their imaginary
2018      and real parts.  */
2019   if (targetm.calls.split_complex_arg)
2020     {
2021       type_arg_types = split_complex_types (TYPE_ARG_TYPES (funtype));
2022       actparms = split_complex_values (actparms);
2023     }
2024   else
2025     type_arg_types = TYPE_ARG_TYPES (funtype);
2026
2027   if (flags & ECF_MAY_BE_ALLOCA)
2028     current_function_calls_alloca = 1;
2029
2030   /* If struct_value_rtx is 0, it means pass the address
2031      as if it were an extra parameter.  */
2032   if (structure_value_addr && struct_value == 0)
2033     {
2034       /* If structure_value_addr is a REG other than
2035          virtual_outgoing_args_rtx, we can use always use it.  If it
2036          is not a REG, we must always copy it into a register.
2037          If it is virtual_outgoing_args_rtx, we must copy it to another
2038          register in some cases.  */
2039       rtx temp = (!REG_P (structure_value_addr)
2040                   || (ACCUMULATE_OUTGOING_ARGS
2041                       && stack_arg_under_construction
2042                       && structure_value_addr == virtual_outgoing_args_rtx)
2043                   ? copy_addr_to_reg (convert_memory_address
2044                                       (Pmode, structure_value_addr))
2045                   : structure_value_addr);
2046
2047       actparms
2048         = tree_cons (error_mark_node,
2049                      make_tree (build_pointer_type (TREE_TYPE (funtype)),
2050                                 temp),
2051                      actparms);
2052       structure_value_addr_parm = 1;
2053     }
2054
2055   /* Count the arguments and set NUM_ACTUALS.  */
2056   for (p = actparms, num_actuals = 0; p; p = TREE_CHAIN (p))
2057     num_actuals++;
2058
2059   /* Compute number of named args.
2060      First, do a raw count of the args for INIT_CUMULATIVE_ARGS.  */
2061
2062   if (type_arg_types != 0)
2063     n_named_args
2064       = (list_length (type_arg_types)
2065          /* Count the struct value address, if it is passed as a parm.  */
2066          + structure_value_addr_parm);
2067   else
2068     /* If we know nothing, treat all args as named.  */
2069     n_named_args = num_actuals;
2070
2071   /* Start updating where the next arg would go.
2072
2073      On some machines (such as the PA) indirect calls have a different
2074      calling convention than normal calls.  The fourth argument in
2075      INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
2076      or not.  */
2077   INIT_CUMULATIVE_ARGS (args_so_far, funtype, NULL_RTX, fndecl, n_named_args);
2078
2079   /* Now possibly adjust the number of named args.
2080      Normally, don't include the last named arg if anonymous args follow.
2081      We do include the last named arg if
2082      targetm.calls.strict_argument_naming() returns nonzero.
2083      (If no anonymous args follow, the result of list_length is actually
2084      one too large.  This is harmless.)
2085
2086      If targetm.calls.pretend_outgoing_varargs_named() returns
2087      nonzero, and targetm.calls.strict_argument_naming() returns zero,
2088      this machine will be able to place unnamed args that were passed
2089      in registers into the stack.  So treat all args as named.  This
2090      allows the insns emitting for a specific argument list to be
2091      independent of the function declaration.
2092
2093      If targetm.calls.pretend_outgoing_varargs_named() returns zero,
2094      we do not have any reliable way to pass unnamed args in
2095      registers, so we must force them into memory.  */
2096
2097   if (type_arg_types != 0
2098       && targetm.calls.strict_argument_naming (&args_so_far))
2099     ;
2100   else if (type_arg_types != 0
2101            && ! targetm.calls.pretend_outgoing_varargs_named (&args_so_far))
2102     /* Don't include the last named arg.  */
2103     --n_named_args;
2104   else
2105     /* Treat all args as named.  */
2106     n_named_args = num_actuals;
2107
2108   /* Make a vector to hold all the information about each arg.  */
2109   args = alloca (num_actuals * sizeof (struct arg_data));
2110   memset (args, 0, num_actuals * sizeof (struct arg_data));
2111
2112   /* Build up entries in the ARGS array, compute the size of the
2113      arguments into ARGS_SIZE, etc.  */
2114   initialize_argument_information (num_actuals, args, &args_size,
2115                                    n_named_args, actparms, fndecl,
2116                                    &args_so_far, reg_parm_stack_space,
2117                                    &old_stack_level, &old_pending_adj,
2118                                    &must_preallocate, &flags,
2119                                    &try_tail_call, CALL_FROM_THUNK_P (exp));
2120
2121   if (args_size.var)
2122     {
2123       /* If this function requires a variable-sized argument list, don't
2124          try to make a cse'able block for this call.  We may be able to
2125          do this eventually, but it is too complicated to keep track of
2126          what insns go in the cse'able block and which don't.  */
2127
2128       flags &= ~ECF_LIBCALL_BLOCK;
2129       must_preallocate = 1;
2130     }
2131
2132   /* Now make final decision about preallocating stack space.  */
2133   must_preallocate = finalize_must_preallocate (must_preallocate,
2134                                                 num_actuals, args,
2135                                                 &args_size);
2136
2137   /* If the structure value address will reference the stack pointer, we
2138      must stabilize it.  We don't need to do this if we know that we are
2139      not going to adjust the stack pointer in processing this call.  */
2140
2141   if (structure_value_addr
2142       && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
2143           || reg_mentioned_p (virtual_outgoing_args_rtx,
2144                               structure_value_addr))
2145       && (args_size.var
2146           || (!ACCUMULATE_OUTGOING_ARGS && args_size.constant)))
2147     structure_value_addr = copy_to_reg (structure_value_addr);
2148
2149   /* Tail calls can make things harder to debug, and we've traditionally
2150      pushed these optimizations into -O2.  Don't try if we're already
2151      expanding a call, as that means we're an argument.  Don't try if
2152      there's cleanups, as we know there's code to follow the call.  */
2153
2154   if (currently_expanding_call++ != 0
2155       || !flag_optimize_sibling_calls
2156       || args_size.var
2157       || lookup_stmt_eh_region (exp) >= 0)
2158     try_tail_call = 0;
2159
2160   /*  Rest of purposes for tail call optimizations to fail.  */
2161   if (
2162 #ifdef HAVE_sibcall_epilogue
2163       !HAVE_sibcall_epilogue
2164 #else
2165       1
2166 #endif
2167       || !try_tail_call
2168       /* Doing sibling call optimization needs some work, since
2169          structure_value_addr can be allocated on the stack.
2170          It does not seem worth the effort since few optimizable
2171          sibling calls will return a structure.  */
2172       || structure_value_addr != NULL_RTX
2173       /* Check whether the target is able to optimize the call
2174          into a sibcall.  */
2175       || !targetm.function_ok_for_sibcall (fndecl, exp)
2176       /* Functions that do not return exactly once may not be sibcall
2177          optimized.  */
2178       || (flags & (ECF_RETURNS_TWICE | ECF_NORETURN))
2179       || TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (addr)))
2180       /* If the called function is nested in the current one, it might access
2181          some of the caller's arguments, but could clobber them beforehand if
2182          the argument areas are shared.  */
2183       || (fndecl && decl_function_context (fndecl) == current_function_decl)
2184       /* If this function requires more stack slots than the current
2185          function, we cannot change it into a sibling call.  */
2186       || args_size.constant > current_function_args_size
2187       /* If the callee pops its own arguments, then it must pop exactly
2188          the same number of arguments as the current function.  */
2189       || (RETURN_POPS_ARGS (fndecl, funtype, args_size.constant)
2190           != RETURN_POPS_ARGS (current_function_decl,
2191                                TREE_TYPE (current_function_decl),
2192                                current_function_args_size))
2193       || !lang_hooks.decls.ok_for_sibcall (fndecl))
2194     try_tail_call = 0;
2195
2196   /* Ensure current function's preferred stack boundary is at least
2197      what we need.  We don't have to increase alignment for recursive
2198      functions.  */
2199   if (cfun->preferred_stack_boundary < preferred_stack_boundary
2200       && fndecl != current_function_decl)
2201     cfun->preferred_stack_boundary = preferred_stack_boundary;
2202   if (fndecl == current_function_decl)
2203     cfun->recursive_call_emit = true;
2204
2205   preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
2206
2207   /* We want to make two insn chains; one for a sibling call, the other
2208      for a normal call.  We will select one of the two chains after
2209      initial RTL generation is complete.  */
2210   for (pass = try_tail_call ? 0 : 1; pass < 2; pass++)
2211     {
2212       int sibcall_failure = 0;
2213       /* We want to emit any pending stack adjustments before the tail
2214          recursion "call".  That way we know any adjustment after the tail
2215          recursion call can be ignored if we indeed use the tail
2216          call expansion.  */
2217       int save_pending_stack_adjust = 0;
2218       int save_stack_pointer_delta = 0;
2219       rtx insns;
2220       rtx before_call, next_arg_reg;
2221
2222       if (pass == 0)
2223         {
2224           /* State variables we need to save and restore between
2225              iterations.  */
2226           save_pending_stack_adjust = pending_stack_adjust;
2227           save_stack_pointer_delta = stack_pointer_delta;
2228         }
2229       if (pass)
2230         flags &= ~ECF_SIBCALL;
2231       else
2232         flags |= ECF_SIBCALL;
2233
2234       /* Other state variables that we must reinitialize each time
2235          through the loop (that are not initialized by the loop itself).  */
2236       argblock = 0;
2237       call_fusage = 0;
2238
2239       /* Start a new sequence for the normal call case.
2240
2241          From this point on, if the sibling call fails, we want to set
2242          sibcall_failure instead of continuing the loop.  */
2243       start_sequence ();
2244
2245       /* Don't let pending stack adjusts add up to too much.
2246          Also, do all pending adjustments now if there is any chance
2247          this might be a call to alloca or if we are expanding a sibling
2248          call sequence or if we are calling a function that is to return
2249          with stack pointer depressed.  */
2250       if (pending_stack_adjust >= 32
2251           || (pending_stack_adjust > 0
2252               && (flags & (ECF_MAY_BE_ALLOCA | ECF_SP_DEPRESSED)))
2253           || pass == 0)
2254         do_pending_stack_adjust ();
2255
2256       /* When calling a const function, we must pop the stack args right away,
2257          so that the pop is deleted or moved with the call.  */
2258       if (pass && (flags & ECF_LIBCALL_BLOCK))
2259         NO_DEFER_POP;
2260
2261       /* Precompute any arguments as needed.  */
2262       if (pass)
2263         precompute_arguments (flags, num_actuals, args);
2264
2265       /* Now we are about to start emitting insns that can be deleted
2266          if a libcall is deleted.  */
2267       if (pass && (flags & (ECF_LIBCALL_BLOCK | ECF_MALLOC)))
2268         start_sequence ();
2269
2270       adjusted_args_size = args_size;
2271       /* Compute the actual size of the argument block required.  The variable
2272          and constant sizes must be combined, the size may have to be rounded,
2273          and there may be a minimum required size.  When generating a sibcall
2274          pattern, do not round up, since we'll be re-using whatever space our
2275          caller provided.  */
2276       unadjusted_args_size
2277         = compute_argument_block_size (reg_parm_stack_space,
2278                                        &adjusted_args_size,
2279                                        (pass == 0 ? 0
2280                                         : preferred_stack_boundary));
2281
2282       old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
2283
2284       /* The argument block when performing a sibling call is the
2285          incoming argument block.  */
2286       if (pass == 0)
2287         {
2288           argblock = virtual_incoming_args_rtx;
2289           argblock
2290 #ifdef STACK_GROWS_DOWNWARD
2291             = plus_constant (argblock, current_function_pretend_args_size);
2292 #else
2293             = plus_constant (argblock, -current_function_pretend_args_size);
2294 #endif
2295           stored_args_map = sbitmap_alloc (args_size.constant);
2296           sbitmap_zero (stored_args_map);
2297         }
2298
2299       /* If we have no actual push instructions, or shouldn't use them,
2300          make space for all args right now.  */
2301       else if (adjusted_args_size.var != 0)
2302         {
2303           if (old_stack_level == 0)
2304             {
2305               emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
2306               old_stack_pointer_delta = stack_pointer_delta;
2307               old_pending_adj = pending_stack_adjust;
2308               pending_stack_adjust = 0;
2309               /* stack_arg_under_construction says whether a stack arg is
2310                  being constructed at the old stack level.  Pushing the stack
2311                  gets a clean outgoing argument block.  */
2312               old_stack_arg_under_construction = stack_arg_under_construction;
2313               stack_arg_under_construction = 0;
2314             }
2315           argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
2316         }
2317       else
2318         {
2319           /* Note that we must go through the motions of allocating an argument
2320              block even if the size is zero because we may be storing args
2321              in the area reserved for register arguments, which may be part of
2322              the stack frame.  */
2323
2324           int needed = adjusted_args_size.constant;
2325
2326           /* Store the maximum argument space used.  It will be pushed by
2327              the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
2328              checking).  */
2329
2330           if (needed > current_function_outgoing_args_size)
2331             current_function_outgoing_args_size = needed;
2332
2333           if (must_preallocate)
2334             {
2335               if (ACCUMULATE_OUTGOING_ARGS)
2336                 {
2337                   /* Since the stack pointer will never be pushed, it is
2338                      possible for the evaluation of a parm to clobber
2339                      something we have already written to the stack.
2340                      Since most function calls on RISC machines do not use
2341                      the stack, this is uncommon, but must work correctly.
2342
2343                      Therefore, we save any area of the stack that was already
2344                      written and that we are using.  Here we set up to do this
2345                      by making a new stack usage map from the old one.  The
2346                      actual save will be done by store_one_arg.
2347
2348                      Another approach might be to try to reorder the argument
2349                      evaluations to avoid this conflicting stack usage.  */
2350
2351 #ifndef OUTGOING_REG_PARM_STACK_SPACE
2352                   /* Since we will be writing into the entire argument area,
2353                      the map must be allocated for its entire size, not just
2354                      the part that is the responsibility of the caller.  */
2355                   needed += reg_parm_stack_space;
2356 #endif
2357
2358 #ifdef ARGS_GROW_DOWNWARD
2359                   highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2360                                                      needed + 1);
2361 #else
2362                   highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2363                                                      needed);
2364 #endif
2365                   stack_usage_map = alloca (highest_outgoing_arg_in_use);
2366
2367                   if (initial_highest_arg_in_use)
2368                     memcpy (stack_usage_map, initial_stack_usage_map,
2369                             initial_highest_arg_in_use);
2370
2371                   if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
2372                     memset (&stack_usage_map[initial_highest_arg_in_use], 0,
2373                            (highest_outgoing_arg_in_use
2374                             - initial_highest_arg_in_use));
2375                   needed = 0;
2376
2377                   /* The address of the outgoing argument list must not be
2378                      copied to a register here, because argblock would be left
2379                      pointing to the wrong place after the call to
2380                      allocate_dynamic_stack_space below.  */
2381
2382                   argblock = virtual_outgoing_args_rtx;
2383                 }
2384               else
2385                 {
2386                   if (inhibit_defer_pop == 0)
2387                     {
2388                       /* Try to reuse some or all of the pending_stack_adjust
2389                          to get this space.  */
2390                       needed
2391                         = (combine_pending_stack_adjustment_and_call
2392                            (unadjusted_args_size,
2393                             &adjusted_args_size,
2394                             preferred_unit_stack_boundary));
2395
2396                       /* combine_pending_stack_adjustment_and_call computes
2397                          an adjustment before the arguments are allocated.
2398                          Account for them and see whether or not the stack
2399                          needs to go up or down.  */
2400                       needed = unadjusted_args_size - needed;
2401
2402                       if (needed < 0)
2403                         {
2404                           /* We're releasing stack space.  */
2405                           /* ??? We can avoid any adjustment at all if we're
2406                              already aligned.  FIXME.  */
2407                           pending_stack_adjust = -needed;
2408                           do_pending_stack_adjust ();
2409                           needed = 0;
2410                         }
2411                       else
2412                         /* We need to allocate space.  We'll do that in
2413                            push_block below.  */
2414                         pending_stack_adjust = 0;
2415                     }
2416
2417                   /* Special case this because overhead of `push_block' in
2418                      this case is non-trivial.  */
2419                   if (needed == 0)
2420                     argblock = virtual_outgoing_args_rtx;
2421                   else
2422                     {
2423                       argblock = push_block (GEN_INT (needed), 0, 0);
2424 #ifdef ARGS_GROW_DOWNWARD
2425                       argblock = plus_constant (argblock, needed);
2426 #endif
2427                     }
2428
2429                   /* We only really need to call `copy_to_reg' in the case
2430                      where push insns are going to be used to pass ARGBLOCK
2431                      to a function call in ARGS.  In that case, the stack
2432                      pointer changes value from the allocation point to the
2433                      call point, and hence the value of
2434                      VIRTUAL_OUTGOING_ARGS_RTX changes as well.  But might
2435                      as well always do it.  */
2436                   argblock = copy_to_reg (argblock);
2437                 }
2438             }
2439         }
2440
2441       if (ACCUMULATE_OUTGOING_ARGS)
2442         {
2443           /* The save/restore code in store_one_arg handles all
2444              cases except one: a constructor call (including a C
2445              function returning a BLKmode struct) to initialize
2446              an argument.  */
2447           if (stack_arg_under_construction)
2448             {
2449 #ifndef OUTGOING_REG_PARM_STACK_SPACE
2450               rtx push_size = GEN_INT (reg_parm_stack_space
2451                                        + adjusted_args_size.constant);
2452 #else
2453               rtx push_size = GEN_INT (adjusted_args_size.constant);
2454 #endif
2455               if (old_stack_level == 0)
2456                 {
2457                   emit_stack_save (SAVE_BLOCK, &old_stack_level,
2458                                    NULL_RTX);
2459                   old_stack_pointer_delta = stack_pointer_delta;
2460                   old_pending_adj = pending_stack_adjust;
2461                   pending_stack_adjust = 0;
2462                   /* stack_arg_under_construction says whether a stack
2463                      arg is being constructed at the old stack level.
2464                      Pushing the stack gets a clean outgoing argument
2465                      block.  */
2466                   old_stack_arg_under_construction
2467                     = stack_arg_under_construction;
2468                   stack_arg_under_construction = 0;
2469                   /* Make a new map for the new argument list.  */
2470                   stack_usage_map = alloca (highest_outgoing_arg_in_use);
2471                   memset (stack_usage_map, 0, highest_outgoing_arg_in_use);
2472                   highest_outgoing_arg_in_use = 0;
2473                 }
2474               allocate_dynamic_stack_space (push_size, NULL_RTX,
2475                                             BITS_PER_UNIT);
2476             }
2477
2478           /* If argument evaluation might modify the stack pointer,
2479              copy the address of the argument list to a register.  */
2480           for (i = 0; i < num_actuals; i++)
2481             if (args[i].pass_on_stack)
2482               {
2483                 argblock = copy_addr_to_reg (argblock);
2484                 break;
2485               }
2486         }
2487
2488       compute_argument_addresses (args, argblock, num_actuals);
2489
2490       /* If we push args individually in reverse order, perform stack alignment
2491          before the first push (the last arg).  */
2492       if (PUSH_ARGS_REVERSED && argblock == 0
2493           && adjusted_args_size.constant != unadjusted_args_size)
2494         {
2495           /* When the stack adjustment is pending, we get better code
2496              by combining the adjustments.  */
2497           if (pending_stack_adjust
2498               && ! (flags & ECF_LIBCALL_BLOCK)
2499               && ! inhibit_defer_pop)
2500             {
2501               pending_stack_adjust
2502                 = (combine_pending_stack_adjustment_and_call
2503                    (unadjusted_args_size,
2504                     &adjusted_args_size,
2505                     preferred_unit_stack_boundary));
2506               do_pending_stack_adjust ();
2507             }
2508           else if (argblock == 0)
2509             anti_adjust_stack (GEN_INT (adjusted_args_size.constant
2510                                         - unadjusted_args_size));
2511         }
2512       /* Now that the stack is properly aligned, pops can't safely
2513          be deferred during the evaluation of the arguments.  */
2514       NO_DEFER_POP;
2515
2516       funexp = rtx_for_function_call (fndecl, addr);
2517
2518       /* Figure out the register where the value, if any, will come back.  */
2519       valreg = 0;
2520       if (TYPE_MODE (TREE_TYPE (exp)) != VOIDmode
2521           && ! structure_value_addr)
2522         {
2523           if (pcc_struct_value)
2524             valreg = hard_function_value (build_pointer_type (TREE_TYPE (exp)),
2525                                           fndecl, (pass == 0));
2526           else
2527             valreg = hard_function_value (TREE_TYPE (exp), fndecl, (pass == 0));
2528         }
2529
2530       /* Precompute all register parameters.  It isn't safe to compute anything
2531          once we have started filling any specific hard regs.  */
2532       precompute_register_parameters (num_actuals, args, &reg_parm_seen);
2533
2534       if (TREE_OPERAND (exp, 2))
2535         static_chain_value = expand_expr (TREE_OPERAND (exp, 2),
2536                                           NULL_RTX, VOIDmode, 0);
2537       else
2538         static_chain_value = 0;
2539
2540 #ifdef REG_PARM_STACK_SPACE
2541       /* Save the fixed argument area if it's part of the caller's frame and
2542          is clobbered by argument setup for this call.  */
2543       if (ACCUMULATE_OUTGOING_ARGS && pass)
2544         save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
2545                                               &low_to_save, &high_to_save);
2546 #endif
2547
2548       /* Now store (and compute if necessary) all non-register parms.
2549          These come before register parms, since they can require block-moves,
2550          which could clobber the registers used for register parms.
2551          Parms which have partial registers are not stored here,
2552          but we do preallocate space here if they want that.  */
2553
2554       for (i = 0; i < num_actuals; i++)
2555         if (args[i].reg == 0 || args[i].pass_on_stack)
2556           {
2557             rtx before_arg = get_last_insn ();
2558
2559             if (store_one_arg (&args[i], argblock, flags,
2560                                adjusted_args_size.var != 0,
2561                                reg_parm_stack_space)
2562                 || (pass == 0
2563                     && check_sibcall_argument_overlap (before_arg,
2564                                                        &args[i], 1)))
2565               sibcall_failure = 1;
2566
2567             if (flags & ECF_CONST
2568                 && args[i].stack
2569                 && args[i].value == args[i].stack)
2570               call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
2571                                                gen_rtx_USE (VOIDmode,
2572                                                             args[i].value),
2573                                                call_fusage);
2574           }
2575
2576       /* If we have a parm that is passed in registers but not in memory
2577          and whose alignment does not permit a direct copy into registers,
2578          make a group of pseudos that correspond to each register that we
2579          will later fill.  */
2580       if (STRICT_ALIGNMENT)
2581         store_unaligned_arguments_into_pseudos (args, num_actuals);
2582
2583       /* Now store any partially-in-registers parm.
2584          This is the last place a block-move can happen.  */
2585       if (reg_parm_seen)
2586         for (i = 0; i < num_actuals; i++)
2587           if (args[i].partial != 0 && ! args[i].pass_on_stack)
2588             {
2589               rtx before_arg = get_last_insn ();
2590
2591               if (store_one_arg (&args[i], argblock, flags,
2592                                  adjusted_args_size.var != 0,
2593                                  reg_parm_stack_space)
2594                   || (pass == 0
2595                       && check_sibcall_argument_overlap (before_arg,
2596                                                          &args[i], 1)))
2597                 sibcall_failure = 1;
2598             }
2599
2600       /* If we pushed args in forward order, perform stack alignment
2601          after pushing the last arg.  */
2602       if (!PUSH_ARGS_REVERSED && argblock == 0)
2603         anti_adjust_stack (GEN_INT (adjusted_args_size.constant
2604                                     - unadjusted_args_size));
2605
2606       /* If register arguments require space on the stack and stack space
2607          was not preallocated, allocate stack space here for arguments
2608          passed in registers.  */
2609 #ifdef OUTGOING_REG_PARM_STACK_SPACE
2610       if (!ACCUMULATE_OUTGOING_ARGS
2611           && must_preallocate == 0 && reg_parm_stack_space > 0)
2612         anti_adjust_stack (GEN_INT (reg_parm_stack_space));
2613 #endif
2614
2615       /* Pass the function the address in which to return a
2616          structure value.  */
2617       if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
2618         {
2619           structure_value_addr
2620             = convert_memory_address (Pmode, structure_value_addr);
2621           emit_move_insn (struct_value,
2622                           force_reg (Pmode,
2623                                      force_operand (structure_value_addr,
2624                                                     NULL_RTX)));
2625
2626           if (REG_P (struct_value))
2627             use_reg (&call_fusage, struct_value);
2628         }
2629
2630       funexp = prepare_call_address (funexp, static_chain_value,
2631                                      &call_fusage, reg_parm_seen, pass == 0);
2632
2633       load_register_parameters (args, num_actuals, &call_fusage, flags,
2634                                 pass == 0, &sibcall_failure);
2635
2636       /* Save a pointer to the last insn before the call, so that we can
2637          later safely search backwards to find the CALL_INSN.  */
2638       before_call = get_last_insn ();
2639
2640       /* Set up next argument register.  For sibling calls on machines
2641          with register windows this should be the incoming register.  */
2642 #ifdef FUNCTION_INCOMING_ARG
2643       if (pass == 0)
2644         next_arg_reg = FUNCTION_INCOMING_ARG (args_so_far, VOIDmode,
2645                                               void_type_node, 1);
2646       else
2647 #endif
2648         next_arg_reg = FUNCTION_ARG (args_so_far, VOIDmode,
2649                                      void_type_node, 1);
2650
2651       /* All arguments and registers used for the call must be set up by
2652          now!  */
2653
2654       /* Stack must be properly aligned now.  */
2655       gcc_assert (!pass
2656                   || !(stack_pointer_delta % preferred_unit_stack_boundary));
2657
2658       /* Generate the actual call instruction.  */
2659       emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
2660                    adjusted_args_size.constant, struct_value_size,
2661                    next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
2662                    flags, & args_so_far);
2663
2664       /* If a non-BLKmode value is returned at the most significant end
2665          of a register, shift the register right by the appropriate amount
2666          and update VALREG accordingly.  BLKmode values are handled by the
2667          group load/store machinery below.  */
2668       if (!structure_value_addr
2669           && !pcc_struct_value
2670           && TYPE_MODE (TREE_TYPE (exp)) != BLKmode
2671           && targetm.calls.return_in_msb (TREE_TYPE (exp)))
2672         {
2673           if (shift_return_value (TYPE_MODE (TREE_TYPE (exp)), false, valreg))
2674             sibcall_failure = 1;
2675           valreg = gen_rtx_REG (TYPE_MODE (TREE_TYPE (exp)), REGNO (valreg));
2676         }
2677
2678       /* If call is cse'able, make appropriate pair of reg-notes around it.
2679          Test valreg so we don't crash; may safely ignore `const'
2680          if return type is void.  Disable for PARALLEL return values, because
2681          we have no way to move such values into a pseudo register.  */
2682       if (pass && (flags & ECF_LIBCALL_BLOCK))
2683         {
2684           rtx insns;
2685           rtx insn;
2686           bool failed = valreg == 0 || GET_CODE (valreg) == PARALLEL;
2687
2688           insns = get_insns ();
2689
2690           /* Expansion of block moves possibly introduced a loop that may
2691              not appear inside libcall block.  */
2692           for (insn = insns; insn; insn = NEXT_INSN (insn))
2693             if (JUMP_P (insn))
2694               failed = true;
2695
2696           if (failed)
2697             {
2698               end_sequence ();
2699               emit_insn (insns);
2700             }
2701           else
2702             {
2703               rtx note = 0;
2704               rtx temp = gen_reg_rtx (GET_MODE (valreg));
2705
2706               /* Mark the return value as a pointer if needed.  */
2707               if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
2708                 mark_reg_pointer (temp,
2709                                   TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp))));
2710
2711               end_sequence ();
2712               if (flag_unsafe_math_optimizations
2713                   && fndecl
2714                   && DECL_BUILT_IN (fndecl)
2715                   && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRT
2716                       || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRTF
2717                       || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRTL))
2718                 note = gen_rtx_fmt_e (SQRT,
2719                                       GET_MODE (temp),
2720                                       args[0].initial_value);
2721               else
2722                 {
2723                   /* Construct an "equal form" for the value which
2724                      mentions all the arguments in order as well as
2725                      the function name.  */
2726                   for (i = 0; i < num_actuals; i++)
2727                     note = gen_rtx_EXPR_LIST (VOIDmode,
2728                                               args[i].initial_value, note);
2729                   note = gen_rtx_EXPR_LIST (VOIDmode, funexp, note);
2730
2731                   if (flags & ECF_PURE)
2732                     note = gen_rtx_EXPR_LIST (VOIDmode,
2733                         gen_rtx_USE (VOIDmode,
2734                                      gen_rtx_MEM (BLKmode,
2735                                                   gen_rtx_SCRATCH (VOIDmode))),
2736                         note);
2737                 }
2738               emit_libcall_block (insns, temp, valreg, note);
2739
2740               valreg = temp;
2741             }
2742         }
2743       else if (pass && (flags & ECF_MALLOC))
2744         {
2745           rtx temp = gen_reg_rtx (GET_MODE (valreg));
2746           rtx last, insns;
2747
2748           /* The return value from a malloc-like function is a pointer.  */
2749           if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
2750             mark_reg_pointer (temp, BIGGEST_ALIGNMENT);
2751
2752           emit_move_insn (temp, valreg);
2753
2754           /* The return value from a malloc-like function can not alias
2755              anything else.  */
2756           last = get_last_insn ();
2757           REG_NOTES (last) =
2758             gen_rtx_EXPR_LIST (REG_NOALIAS, temp, REG_NOTES (last));
2759
2760           /* Write out the sequence.  */
2761           insns = get_insns ();
2762           end_sequence ();
2763           emit_insn (insns);
2764           valreg = temp;
2765         }
2766
2767       /* For calls to `setjmp', etc., inform flow.c it should complain
2768          if nonvolatile values are live.  For functions that cannot return,
2769          inform flow that control does not fall through.  */
2770
2771       if ((flags & ECF_NORETURN) || pass == 0)
2772         {
2773           /* The barrier must be emitted
2774              immediately after the CALL_INSN.  Some ports emit more
2775              than just a CALL_INSN above, so we must search for it here.  */
2776
2777           rtx last = get_last_insn ();
2778           while (!CALL_P (last))
2779             {
2780               last = PREV_INSN (last);
2781               /* There was no CALL_INSN?  */
2782               gcc_assert (last != before_call);
2783             }
2784
2785           emit_barrier_after (last);
2786
2787           /* Stack adjustments after a noreturn call are dead code.
2788              However when NO_DEFER_POP is in effect, we must preserve
2789              stack_pointer_delta.  */
2790           if (inhibit_defer_pop == 0)
2791             {
2792               stack_pointer_delta = old_stack_allocated;
2793               pending_stack_adjust = 0;
2794             }
2795         }
2796
2797       /* If value type not void, return an rtx for the value.  */
2798
2799       if (TYPE_MODE (TREE_TYPE (exp)) == VOIDmode
2800           || ignore)
2801         target = const0_rtx;
2802       else if (structure_value_addr)
2803         {
2804           if (target == 0 || !MEM_P (target))
2805             {
2806               target
2807                 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
2808                                memory_address (TYPE_MODE (TREE_TYPE (exp)),
2809                                                structure_value_addr));
2810               set_mem_attributes (target, exp, 1);
2811             }
2812         }
2813       else if (pcc_struct_value)
2814         {
2815           /* This is the special C++ case where we need to
2816              know what the true target was.  We take care to
2817              never use this value more than once in one expression.  */
2818           target = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
2819                                 copy_to_reg (valreg));
2820           set_mem_attributes (target, exp, 1);
2821         }
2822       /* Handle calls that return values in multiple non-contiguous locations.
2823          The Irix 6 ABI has examples of this.  */
2824       else if (GET_CODE (valreg) == PARALLEL)
2825         {
2826           if (target == 0)
2827             {
2828               /* This will only be assigned once, so it can be readonly.  */
2829               tree nt = build_qualified_type (TREE_TYPE (exp),
2830                                               (TYPE_QUALS (TREE_TYPE (exp))
2831                                                | TYPE_QUAL_CONST));
2832
2833               target = assign_temp (nt, 0, 1, 1);
2834             }
2835
2836           if (! rtx_equal_p (target, valreg))
2837             emit_group_store (target, valreg, TREE_TYPE (exp),
2838                               int_size_in_bytes (TREE_TYPE (exp)));
2839
2840           /* We can not support sibling calls for this case.  */
2841           sibcall_failure = 1;
2842         }
2843       else if (target
2844                && GET_MODE (target) == TYPE_MODE (TREE_TYPE (exp))
2845                && GET_MODE (target) == GET_MODE (valreg))
2846         {
2847           /* TARGET and VALREG cannot be equal at this point because the
2848              latter would not have REG_FUNCTION_VALUE_P true, while the
2849              former would if it were referring to the same register.
2850
2851              If they refer to the same register, this move will be a no-op,
2852              except when function inlining is being done.  */
2853           emit_move_insn (target, valreg);
2854
2855           /* If we are setting a MEM, this code must be executed.  Since it is
2856              emitted after the call insn, sibcall optimization cannot be
2857              performed in that case.  */
2858           if (MEM_P (target))
2859             sibcall_failure = 1;
2860         }
2861       else if (TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
2862         {
2863           target = copy_blkmode_from_reg (target, valreg, TREE_TYPE (exp));
2864
2865           /* We can not support sibling calls for this case.  */
2866           sibcall_failure = 1;
2867         }
2868       else
2869         target = copy_to_reg (valreg);
2870
2871       if (targetm.calls.promote_function_return(funtype))
2872         {
2873           /* If we promoted this return value, make the proper SUBREG.
2874              TARGET might be const0_rtx here, so be careful.  */
2875           if (REG_P (target)
2876               && TYPE_MODE (TREE_TYPE (exp)) != BLKmode
2877               && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
2878             {
2879               tree type = TREE_TYPE (exp);
2880               int unsignedp = TYPE_UNSIGNED (type);
2881               int offset = 0;
2882               enum machine_mode pmode;
2883               
2884               pmode = promote_mode (type, TYPE_MODE (type), &unsignedp, 1);
2885               /* If we don't promote as expected, something is wrong.  */
2886               gcc_assert (GET_MODE (target) == pmode);
2887               
2888               if ((WORDS_BIG_ENDIAN || BYTES_BIG_ENDIAN)
2889                   && (GET_MODE_SIZE (GET_MODE (target))
2890                       > GET_MODE_SIZE (TYPE_MODE (type))))
2891                 {
2892                   offset = GET_MODE_SIZE (GET_MODE (target))
2893                     - GET_MODE_SIZE (TYPE_MODE (type));
2894                   if (! BYTES_BIG_ENDIAN)
2895                     offset = (offset / UNITS_PER_WORD) * UNITS_PER_WORD;
2896                   else if (! WORDS_BIG_ENDIAN)
2897                     offset %= UNITS_PER_WORD;
2898                 }
2899               target = gen_rtx_SUBREG (TYPE_MODE (type), target, offset);
2900               SUBREG_PROMOTED_VAR_P (target) = 1;
2901               SUBREG_PROMOTED_UNSIGNED_SET (target, unsignedp);
2902             }
2903         }
2904
2905       /* If size of args is variable or this was a constructor call for a stack
2906          argument, restore saved stack-pointer value.  */
2907
2908       if (old_stack_level && ! (flags & ECF_SP_DEPRESSED))
2909         {
2910           emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
2911           stack_pointer_delta = old_stack_pointer_delta;
2912           pending_stack_adjust = old_pending_adj;
2913           old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
2914           stack_arg_under_construction = old_stack_arg_under_construction;
2915           highest_outgoing_arg_in_use = initial_highest_arg_in_use;
2916           stack_usage_map = initial_stack_usage_map;
2917           sibcall_failure = 1;
2918         }
2919       else if (ACCUMULATE_OUTGOING_ARGS && pass)
2920         {
2921 #ifdef REG_PARM_STACK_SPACE
2922           if (save_area)
2923             restore_fixed_argument_area (save_area, argblock,
2924                                          high_to_save, low_to_save);
2925 #endif
2926
2927           /* If we saved any argument areas, restore them.  */
2928           for (i = 0; i < num_actuals; i++)
2929             if (args[i].save_area)
2930               {
2931                 enum machine_mode save_mode = GET_MODE (args[i].save_area);
2932                 rtx stack_area
2933                   = gen_rtx_MEM (save_mode,
2934                                  memory_address (save_mode,
2935                                                  XEXP (args[i].stack_slot, 0)));
2936
2937                 if (save_mode != BLKmode)
2938                   emit_move_insn (stack_area, args[i].save_area);
2939                 else
2940                   emit_block_move (stack_area, args[i].save_area,
2941                                    GEN_INT (args[i].locate.size.constant),
2942                                    BLOCK_OP_CALL_PARM);
2943               }
2944
2945           highest_outgoing_arg_in_use = initial_highest_arg_in_use;
2946           stack_usage_map = initial_stack_usage_map;
2947         }
2948
2949       /* If this was alloca, record the new stack level for nonlocal gotos.
2950          Check for the handler slots since we might not have a save area
2951          for non-local gotos.  */
2952
2953       if ((flags & ECF_MAY_BE_ALLOCA) && cfun->nonlocal_goto_save_area != 0)
2954         update_nonlocal_goto_save_area ();
2955
2956       /* Free up storage we no longer need.  */
2957       for (i = 0; i < num_actuals; ++i)
2958         if (args[i].aligned_regs)
2959           free (args[i].aligned_regs);
2960
2961       insns = get_insns ();
2962       end_sequence ();
2963
2964       if (pass == 0)
2965         {
2966           tail_call_insns = insns;
2967
2968           /* Restore the pending stack adjustment now that we have
2969              finished generating the sibling call sequence.  */
2970
2971           pending_stack_adjust = save_pending_stack_adjust;
2972           stack_pointer_delta = save_stack_pointer_delta;
2973
2974           /* Prepare arg structure for next iteration.  */
2975           for (i = 0; i < num_actuals; i++)
2976             {
2977               args[i].value = 0;
2978               args[i].aligned_regs = 0;
2979               args[i].stack = 0;
2980             }
2981
2982           sbitmap_free (stored_args_map);
2983         }
2984       else
2985         {
2986           normal_call_insns = insns;
2987
2988           /* Verify that we've deallocated all the stack we used.  */
2989           gcc_assert ((flags & ECF_NORETURN)
2990                       || (old_stack_allocated
2991                           == stack_pointer_delta - pending_stack_adjust));
2992         }
2993
2994       /* If something prevents making this a sibling call,
2995          zero out the sequence.  */
2996       if (sibcall_failure)
2997         tail_call_insns = NULL_RTX;
2998       else
2999         break;
3000     }
3001
3002   /* If tail call production succeeded, we need to remove REG_EQUIV notes on
3003      arguments too, as argument area is now clobbered by the call.  */
3004   if (tail_call_insns)
3005     {
3006       emit_insn (tail_call_insns);
3007       cfun->tail_call_emit = true;
3008     }
3009   else
3010     emit_insn (normal_call_insns);
3011
3012   currently_expanding_call--;
3013
3014   /* If this function returns with the stack pointer depressed, ensure
3015      this block saves and restores the stack pointer, show it was
3016      changed, and adjust for any outgoing arg space.  */
3017   if (flags & ECF_SP_DEPRESSED)
3018     {
3019       clear_pending_stack_adjust ();
3020       emit_insn (gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx));
3021       emit_move_insn (virtual_stack_dynamic_rtx, stack_pointer_rtx);
3022     }
3023
3024   return target;
3025 }
3026
3027 /* A sibling call sequence invalidates any REG_EQUIV notes made for
3028    this function's incoming arguments.
3029
3030    At the start of RTL generation we know the only REG_EQUIV notes
3031    in the rtl chain are those for incoming arguments, so we can safely
3032    flush any REG_EQUIV note.
3033
3034    This is (slight) overkill.  We could keep track of the highest
3035    argument we clobber and be more selective in removing notes, but it
3036    does not seem to be worth the effort.  */
3037 void
3038 fixup_tail_calls (void)
3039 {
3040   purge_reg_equiv_notes ();
3041 }
3042
3043 /* Traverse an argument list in VALUES and expand all complex
3044    arguments into their components.  */
3045 static tree
3046 split_complex_values (tree values)
3047 {
3048   tree p;
3049
3050   /* Before allocating memory, check for the common case of no complex.  */
3051   for (p = values; p; p = TREE_CHAIN (p))
3052     {
3053       tree type = TREE_TYPE (TREE_VALUE (p));
3054       if (type && TREE_CODE (type) == COMPLEX_TYPE
3055           && targetm.calls.split_complex_arg (type))
3056         goto found;
3057     }
3058   return values;
3059
3060  found:
3061   values = copy_list (values);
3062
3063   for (p = values; p; p = TREE_CHAIN (p))
3064     {
3065       tree complex_value = TREE_VALUE (p);
3066       tree complex_type;
3067
3068       complex_type = TREE_TYPE (complex_value);
3069       if (!complex_type)
3070         continue;
3071
3072       if (TREE_CODE (complex_type) == COMPLEX_TYPE
3073           && targetm.calls.split_complex_arg (complex_type))
3074         {
3075           tree subtype;
3076           tree real, imag, next;
3077
3078           subtype = TREE_TYPE (complex_type);
3079           complex_value = save_expr (complex_value);
3080           real = build1 (REALPART_EXPR, subtype, complex_value);
3081           imag = build1 (IMAGPART_EXPR, subtype, complex_value);
3082
3083           TREE_VALUE (p) = real;
3084           next = TREE_CHAIN (p);
3085           imag = build_tree_list (NULL_TREE, imag);
3086           TREE_CHAIN (p) = imag;
3087           TREE_CHAIN (imag) = next;
3088
3089           /* Skip the newly created node.  */
3090           p = TREE_CHAIN (p);
3091         }
3092     }
3093
3094   return values;
3095 }
3096
3097 /* Traverse a list of TYPES and expand all complex types into their
3098    components.  */
3099 static tree
3100 split_complex_types (tree types)
3101 {
3102   tree p;
3103
3104   /* Before allocating memory, check for the common case of no complex.  */
3105   for (p = types; p; p = TREE_CHAIN (p))
3106     {
3107       tree type = TREE_VALUE (p);
3108       if (TREE_CODE (type) == COMPLEX_TYPE
3109           && targetm.calls.split_complex_arg (type))
3110         goto found;
3111     }
3112   return types;
3113
3114  found:
3115   types = copy_list (types);
3116
3117   for (p = types; p; p = TREE_CHAIN (p))
3118     {
3119       tree complex_type = TREE_VALUE (p);
3120
3121       if (TREE_CODE (complex_type) == COMPLEX_TYPE
3122           && targetm.calls.split_complex_arg (complex_type))
3123         {
3124           tree next, imag;
3125
3126           /* Rewrite complex type with component type.  */
3127           TREE_VALUE (p) = TREE_TYPE (complex_type);
3128           next = TREE_CHAIN (p);
3129
3130           /* Add another component type for the imaginary part.  */
3131           imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
3132           TREE_CHAIN (p) = imag;
3133           TREE_CHAIN (imag) = next;
3134
3135           /* Skip the newly created node.  */
3136           p = TREE_CHAIN (p);
3137         }
3138     }
3139
3140   return types;
3141 }
3142 \f
3143 /* Output a library call to function FUN (a SYMBOL_REF rtx).
3144    The RETVAL parameter specifies whether return value needs to be saved, other
3145    parameters are documented in the emit_library_call function below.  */
3146
3147 static rtx
3148 emit_library_call_value_1 (int retval, rtx orgfun, rtx value,
3149                            enum libcall_type fn_type,
3150                            enum machine_mode outmode, int nargs, va_list p)
3151 {
3152   /* Total size in bytes of all the stack-parms scanned so far.  */
3153   struct args_size args_size;
3154   /* Size of arguments before any adjustments (such as rounding).  */
3155   struct args_size original_args_size;
3156   int argnum;
3157   rtx fun;
3158   int inc;
3159   int count;
3160   rtx argblock = 0;
3161   CUMULATIVE_ARGS args_so_far;
3162   struct arg
3163   {
3164     rtx value;
3165     enum machine_mode mode;
3166     rtx reg;
3167     int partial;
3168     struct locate_and_pad_arg_data locate;
3169     rtx save_area;
3170   };
3171   struct arg *argvec;
3172   int old_inhibit_defer_pop = inhibit_defer_pop;
3173   rtx call_fusage = 0;
3174   rtx mem_value = 0;
3175   rtx valreg;
3176   int pcc_struct_value = 0;
3177   int struct_value_size = 0;
3178   int flags;
3179   int reg_parm_stack_space = 0;
3180   int needed;
3181   rtx before_call;
3182   tree tfom;                    /* type_for_mode (outmode, 0) */
3183
3184 #ifdef REG_PARM_STACK_SPACE
3185   /* Define the boundary of the register parm stack space that needs to be
3186      save, if any.  */
3187   int low_to_save, high_to_save;
3188   rtx save_area = 0;            /* Place that it is saved.  */
3189 #endif
3190
3191   /* Size of the stack reserved for parameter registers.  */
3192   int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
3193   char *initial_stack_usage_map = stack_usage_map;
3194
3195   rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
3196
3197 #ifdef REG_PARM_STACK_SPACE
3198   reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
3199 #endif
3200
3201   /* By default, library functions can not throw.  */
3202   flags = ECF_NOTHROW;
3203
3204   switch (fn_type)
3205     {
3206     case LCT_NORMAL:
3207       break;
3208     case LCT_CONST:
3209       flags |= ECF_CONST;
3210       break;
3211     case LCT_PURE:
3212       flags |= ECF_PURE;
3213       break;
3214     case LCT_CONST_MAKE_BLOCK:
3215       flags |= ECF_CONST | ECF_LIBCALL_BLOCK;
3216       break;
3217     case LCT_PURE_MAKE_BLOCK:
3218       flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
3219       break;
3220     case LCT_NORETURN:
3221       flags |= ECF_NORETURN;
3222       break;
3223     case LCT_THROW:
3224       flags = ECF_NORETURN;
3225       break;
3226     case LCT_ALWAYS_RETURN:
3227       flags = ECF_ALWAYS_RETURN;
3228       break;
3229     case LCT_RETURNS_TWICE:
3230       flags = ECF_RETURNS_TWICE;
3231       break;
3232     }
3233   fun = orgfun;
3234
3235   /* Ensure current function's preferred stack boundary is at least
3236      what we need.  */
3237   if (cfun->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
3238     cfun->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3239
3240   /* If this kind of value comes back in memory,
3241      decide where in memory it should come back.  */
3242   if (outmode != VOIDmode)
3243     {
3244       tfom = lang_hooks.types.type_for_mode (outmode, 0);
3245       if (aggregate_value_p (tfom, 0))
3246         {
3247 #ifdef PCC_STATIC_STRUCT_RETURN
3248           rtx pointer_reg
3249             = hard_function_value (build_pointer_type (tfom), 0, 0);
3250           mem_value = gen_rtx_MEM (outmode, pointer_reg);
3251           pcc_struct_value = 1;
3252           if (value == 0)
3253             value = gen_reg_rtx (outmode);
3254 #else /* not PCC_STATIC_STRUCT_RETURN */
3255           struct_value_size = GET_MODE_SIZE (outmode);
3256           if (value != 0 && MEM_P (value))
3257             mem_value = value;
3258           else
3259             mem_value = assign_temp (tfom, 0, 1, 1);
3260 #endif
3261           /* This call returns a big structure.  */
3262           flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
3263         }
3264     }
3265   else
3266     tfom = void_type_node;
3267
3268   /* ??? Unfinished: must pass the memory address as an argument.  */
3269
3270   /* Copy all the libcall-arguments out of the varargs data
3271      and into a vector ARGVEC.
3272
3273      Compute how to pass each argument.  We only support a very small subset
3274      of the full argument passing conventions to limit complexity here since
3275      library functions shouldn't have many args.  */
3276
3277   argvec = alloca ((nargs + 1) * sizeof (struct arg));
3278   memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
3279
3280 #ifdef INIT_CUMULATIVE_LIBCALL_ARGS
3281   INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far, outmode, fun);
3282 #else
3283   INIT_CUMULATIVE_ARGS (args_so_far, NULL_TREE, fun, 0, nargs);
3284 #endif
3285
3286   args_size.constant = 0;
3287   args_size.var = 0;
3288
3289   count = 0;
3290
3291   /* Now we are about to start emitting insns that can be deleted
3292      if a libcall is deleted.  */
3293   if (flags & ECF_LIBCALL_BLOCK)
3294     start_sequence ();
3295
3296   push_temp_slots ();
3297
3298   /* If there's a structure value address to be passed,
3299      either pass it in the special place, or pass it as an extra argument.  */
3300   if (mem_value && struct_value == 0 && ! pcc_struct_value)
3301     {
3302       rtx addr = XEXP (mem_value, 0);
3303       
3304       nargs++;
3305
3306       /* Make sure it is a reasonable operand for a move or push insn.  */
3307       if (!REG_P (addr) && !MEM_P (addr)
3308           && ! (CONSTANT_P (addr) && LEGITIMATE_CONSTANT_P (addr)))
3309         addr = force_operand (addr, NULL_RTX);
3310
3311       argvec[count].value = addr;
3312       argvec[count].mode = Pmode;
3313       argvec[count].partial = 0;
3314
3315       argvec[count].reg = FUNCTION_ARG (args_so_far, Pmode, NULL_TREE, 1);
3316       gcc_assert (targetm.calls.arg_partial_bytes (&args_so_far, Pmode,
3317                                                    NULL_TREE, 1) == 0);
3318
3319       locate_and_pad_parm (Pmode, NULL_TREE,
3320 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3321                            1,
3322 #else
3323                            argvec[count].reg != 0,
3324 #endif
3325                            0, NULL_TREE, &args_size, &argvec[count].locate);
3326
3327       if (argvec[count].reg == 0 || argvec[count].partial != 0
3328           || reg_parm_stack_space > 0)
3329         args_size.constant += argvec[count].locate.size.constant;
3330
3331       FUNCTION_ARG_ADVANCE (args_so_far, Pmode, (tree) 0, 1);
3332
3333       count++;
3334     }
3335
3336   for (; count < nargs; count++)
3337     {
3338       rtx val = va_arg (p, rtx);
3339       enum machine_mode mode = va_arg (p, enum machine_mode);
3340
3341       /* We cannot convert the arg value to the mode the library wants here;
3342          must do it earlier where we know the signedness of the arg.  */
3343       gcc_assert (mode != BLKmode
3344                   && (GET_MODE (val) == mode || GET_MODE (val) == VOIDmode));
3345
3346       /* Make sure it is a reasonable operand for a move or push insn.  */
3347       if (!REG_P (val) && !MEM_P (val)
3348           && ! (CONSTANT_P (val) && LEGITIMATE_CONSTANT_P (val)))
3349         val = force_operand (val, NULL_RTX);
3350
3351       if (pass_by_reference (&args_so_far, mode, NULL_TREE, 1))
3352         {
3353           rtx slot;
3354           int must_copy
3355             = !reference_callee_copied (&args_so_far, mode, NULL_TREE, 1);
3356
3357           /* loop.c won't look at CALL_INSN_FUNCTION_USAGE of const/pure
3358              functions, so we have to pretend this isn't such a function.  */
3359           if (flags & ECF_LIBCALL_BLOCK)
3360             {
3361               rtx insns = get_insns ();
3362               end_sequence ();
3363               emit_insn (insns);
3364             }
3365           flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
3366
3367           /* If this was a CONST function, it is now PURE since
3368              it now reads memory.  */
3369           if (flags & ECF_CONST)
3370             {
3371               flags &= ~ECF_CONST;
3372               flags |= ECF_PURE;
3373             }
3374
3375           if (GET_MODE (val) == MEM && !must_copy)
3376             slot = val;
3377           else
3378             {
3379               slot = assign_temp (lang_hooks.types.type_for_mode (mode, 0),
3380                                   0, 1, 1);
3381               emit_move_insn (slot, val);
3382             }
3383
3384           call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
3385                                            gen_rtx_USE (VOIDmode, slot),
3386                                            call_fusage);
3387           if (must_copy)
3388             call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
3389                                              gen_rtx_CLOBBER (VOIDmode,
3390                                                               slot),
3391                                              call_fusage);
3392
3393           mode = Pmode;
3394           val = force_operand (XEXP (slot, 0), NULL_RTX);
3395         }
3396
3397       argvec[count].value = val;
3398       argvec[count].mode = mode;
3399
3400       argvec[count].reg = FUNCTION_ARG (args_so_far, mode, NULL_TREE, 1);
3401
3402       argvec[count].partial
3403         = targetm.calls.arg_partial_bytes (&args_so_far, mode, NULL_TREE, 1);
3404
3405       locate_and_pad_parm (mode, NULL_TREE,
3406 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3407                            1,
3408 #else
3409                            argvec[count].reg != 0,
3410 #endif
3411                            argvec[count].partial,
3412                            NULL_TREE, &args_size, &argvec[count].locate);
3413
3414       gcc_assert (!argvec[count].locate.size.var);
3415
3416       if (argvec[count].reg == 0 || argvec[count].partial != 0
3417           || reg_parm_stack_space > 0)
3418         args_size.constant += argvec[count].locate.size.constant;
3419
3420       FUNCTION_ARG_ADVANCE (args_so_far, mode, (tree) 0, 1);
3421     }
3422
3423   /* If this machine requires an external definition for library
3424      functions, write one out.  */
3425   assemble_external_libcall (fun);
3426
3427   original_args_size = args_size;
3428   args_size.constant = (((args_size.constant
3429                           + stack_pointer_delta
3430                           + STACK_BYTES - 1)
3431                           / STACK_BYTES
3432                           * STACK_BYTES)
3433                          - stack_pointer_delta);
3434
3435   args_size.constant = MAX (args_size.constant,
3436                             reg_parm_stack_space);
3437
3438 #ifndef OUTGOING_REG_PARM_STACK_SPACE
3439   args_size.constant -= reg_parm_stack_space;
3440 #endif
3441
3442   if (args_size.constant > current_function_outgoing_args_size)
3443     current_function_outgoing_args_size = args_size.constant;
3444
3445   if (ACCUMULATE_OUTGOING_ARGS)
3446     {
3447       /* Since the stack pointer will never be pushed, it is possible for
3448          the evaluation of a parm to clobber something we have already
3449          written to the stack.  Since most function calls on RISC machines
3450          do not use the stack, this is uncommon, but must work correctly.
3451
3452          Therefore, we save any area of the stack that was already written
3453          and that we are using.  Here we set up to do this by making a new
3454          stack usage map from the old one.
3455
3456          Another approach might be to try to reorder the argument
3457          evaluations to avoid this conflicting stack usage.  */
3458
3459       needed = args_size.constant;
3460
3461 #ifndef OUTGOING_REG_PARM_STACK_SPACE
3462       /* Since we will be writing into the entire argument area, the
3463          map must be allocated for its entire size, not just the part that
3464          is the responsibility of the caller.  */
3465       needed += reg_parm_stack_space;
3466 #endif
3467
3468 #ifdef ARGS_GROW_DOWNWARD
3469       highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3470                                          needed + 1);
3471 #else
3472       highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3473                                          needed);
3474 #endif
3475       stack_usage_map = alloca (highest_outgoing_arg_in_use);
3476
3477       if (initial_highest_arg_in_use)
3478         memcpy (stack_usage_map, initial_stack_usage_map,
3479                 initial_highest_arg_in_use);
3480
3481       if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
3482         memset (&stack_usage_map[initial_highest_arg_in_use], 0,
3483                highest_outgoing_arg_in_use - initial_highest_arg_in_use);
3484       needed = 0;
3485
3486       /* We must be careful to use virtual regs before they're instantiated,
3487          and real regs afterwards.  Loop optimization, for example, can create
3488          new libcalls after we've instantiated the virtual regs, and if we
3489          use virtuals anyway, they won't match the rtl patterns.  */
3490
3491       if (virtuals_instantiated)
3492         argblock = plus_constant (stack_pointer_rtx, STACK_POINTER_OFFSET);
3493       else
3494         argblock = virtual_outgoing_args_rtx;
3495     }
3496   else
3497     {
3498       if (!PUSH_ARGS)
3499         argblock = push_block (GEN_INT (args_size.constant), 0, 0);
3500     }
3501
3502   /* If we push args individually in reverse order, perform stack alignment
3503      before the first push (the last arg).  */
3504   if (argblock == 0 && PUSH_ARGS_REVERSED)
3505     anti_adjust_stack (GEN_INT (args_size.constant
3506                                 - original_args_size.constant));
3507
3508   if (PUSH_ARGS_REVERSED)
3509     {
3510       inc = -1;
3511       argnum = nargs - 1;
3512     }
3513   else
3514     {
3515       inc = 1;
3516       argnum = 0;
3517     }
3518
3519 #ifdef REG_PARM_STACK_SPACE
3520   if (ACCUMULATE_OUTGOING_ARGS)
3521     {
3522       /* The argument list is the property of the called routine and it
3523          may clobber it.  If the fixed area has been used for previous
3524          parameters, we must save and restore it.  */
3525       save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3526                                             &low_to_save, &high_to_save);
3527     }
3528 #endif
3529
3530   /* Push the args that need to be pushed.  */
3531
3532   /* ARGNUM indexes the ARGVEC array in the order in which the arguments
3533      are to be pushed.  */
3534   for (count = 0; count < nargs; count++, argnum += inc)
3535     {
3536       enum machine_mode mode = argvec[argnum].mode;
3537       rtx val = argvec[argnum].value;
3538       rtx reg = argvec[argnum].reg;
3539       int partial = argvec[argnum].partial;
3540       int lower_bound = 0, upper_bound = 0, i;
3541
3542       if (! (reg != 0 && partial == 0))
3543         {
3544           if (ACCUMULATE_OUTGOING_ARGS)
3545             {
3546               /* If this is being stored into a pre-allocated, fixed-size,
3547                  stack area, save any previous data at that location.  */
3548
3549 #ifdef ARGS_GROW_DOWNWARD
3550               /* stack_slot is negative, but we want to index stack_usage_map
3551                  with positive values.  */
3552               upper_bound = -argvec[argnum].locate.offset.constant + 1;
3553               lower_bound = upper_bound - argvec[argnum].locate.size.constant;
3554 #else
3555               lower_bound = argvec[argnum].locate.offset.constant;
3556               upper_bound = lower_bound + argvec[argnum].locate.size.constant;
3557 #endif
3558
3559               i = lower_bound;
3560               /* Don't worry about things in the fixed argument area;
3561                  it has already been saved.  */
3562               if (i < reg_parm_stack_space)
3563                 i = reg_parm_stack_space;
3564               while (i < upper_bound && stack_usage_map[i] == 0)
3565                 i++;
3566
3567               if (i < upper_bound)
3568                 {
3569                   /* We need to make a save area.  */
3570                   unsigned int size
3571                     = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
3572                   enum machine_mode save_mode
3573                     = mode_for_size (size, MODE_INT, 1);
3574                   rtx adr
3575                     = plus_constant (argblock,
3576                                      argvec[argnum].locate.offset.constant);
3577                   rtx stack_area
3578                     = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
3579
3580                   if (save_mode == BLKmode)
3581                     {
3582                       argvec[argnum].save_area
3583                         = assign_stack_temp (BLKmode,
3584                                              argvec[argnum].locate.size.constant,
3585                                              0);
3586
3587                       emit_block_move (validize_mem (argvec[argnum].save_area),
3588                                        stack_area,
3589                                        GEN_INT (argvec[argnum].locate.size.constant),
3590                                        BLOCK_OP_CALL_PARM);
3591                     }
3592                   else
3593                     {
3594                       argvec[argnum].save_area = gen_reg_rtx (save_mode);
3595
3596                       emit_move_insn (argvec[argnum].save_area, stack_area);
3597                     }
3598                 }
3599             }
3600
3601           emit_push_insn (val, mode, NULL_TREE, NULL_RTX, PARM_BOUNDARY,
3602                           partial, reg, 0, argblock,
3603                           GEN_INT (argvec[argnum].locate.offset.constant),
3604                           reg_parm_stack_space,
3605                           ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad));
3606
3607           /* Now mark the segment we just used.  */
3608           if (ACCUMULATE_OUTGOING_ARGS)
3609             for (i = lower_bound; i < upper_bound; i++)
3610               stack_usage_map[i] = 1;
3611
3612           NO_DEFER_POP;
3613         }
3614     }
3615
3616   /* If we pushed args in forward order, perform stack alignment
3617      after pushing the last arg.  */
3618   if (argblock == 0 && !PUSH_ARGS_REVERSED)
3619     anti_adjust_stack (GEN_INT (args_size.constant
3620                                 - original_args_size.constant));
3621
3622   if (PUSH_ARGS_REVERSED)
3623     argnum = nargs - 1;
3624   else
3625     argnum = 0;
3626
3627   fun = prepare_call_address (fun, NULL, &call_fusage, 0, 0);
3628
3629   /* Now load any reg parms into their regs.  */
3630
3631   /* ARGNUM indexes the ARGVEC array in the order in which the arguments
3632      are to be pushed.  */
3633   for (count = 0; count < nargs; count++, argnum += inc)
3634     {
3635       enum machine_mode mode = argvec[argnum].mode;
3636       rtx val = argvec[argnum].value;
3637       rtx reg = argvec[argnum].reg;
3638       int partial = argvec[argnum].partial;
3639
3640       /* Handle calls that pass values in multiple non-contiguous
3641          locations.  The PA64 has examples of this for library calls.  */
3642       if (reg != 0 && GET_CODE (reg) == PARALLEL)
3643         emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (mode));
3644       else if (reg != 0 && partial == 0)
3645         emit_move_insn (reg, val);
3646
3647       NO_DEFER_POP;
3648     }
3649
3650   /* Any regs containing parms remain in use through the call.  */
3651   for (count = 0; count < nargs; count++)
3652     {
3653       rtx reg = argvec[count].reg;
3654       if (reg != 0 && GET_CODE (reg) == PARALLEL)
3655         use_group_regs (&call_fusage, reg);
3656       else if (reg != 0)
3657         use_reg (&call_fusage, reg);
3658     }
3659
3660   /* Pass the function the address in which to return a structure value.  */
3661   if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
3662     {
3663       emit_move_insn (struct_value,
3664                       force_reg (Pmode,
3665                                  force_operand (XEXP (mem_value, 0),
3666                                                 NULL_RTX)));
3667       if (REG_P (struct_value))
3668         use_reg (&call_fusage, struct_value);
3669     }
3670
3671   /* Don't allow popping to be deferred, since then
3672      cse'ing of library calls could delete a call and leave the pop.  */
3673   NO_DEFER_POP;
3674   valreg = (mem_value == 0 && outmode != VOIDmode
3675             ? hard_libcall_value (outmode) : NULL_RTX);
3676
3677   /* Stack must be properly aligned now.  */
3678   gcc_assert (!(stack_pointer_delta
3679                 & (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1)));
3680
3681   before_call = get_last_insn ();
3682
3683   /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
3684      will set inhibit_defer_pop to that value.  */
3685   /* The return type is needed to decide how many bytes the function pops.
3686      Signedness plays no role in that, so for simplicity, we pretend it's
3687      always signed.  We also assume that the list of arguments passed has
3688      no impact, so we pretend it is unknown.  */
3689
3690   emit_call_1 (fun, NULL,
3691                get_identifier (XSTR (orgfun, 0)),
3692                build_function_type (tfom, NULL_TREE),
3693                original_args_size.constant, args_size.constant,
3694                struct_value_size,
3695                FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1),
3696                valreg,
3697                old_inhibit_defer_pop + 1, call_fusage, flags, & args_so_far);
3698
3699   /* For calls to `setjmp', etc., inform flow.c it should complain
3700      if nonvolatile values are live.  For functions that cannot return,
3701      inform flow that control does not fall through.  */
3702
3703   if (flags & ECF_NORETURN)
3704     {
3705       /* The barrier note must be emitted
3706          immediately after the CALL_INSN.  Some ports emit more than
3707          just a CALL_INSN above, so we must search for it here.  */
3708
3709       rtx last = get_last_insn ();
3710       while (!CALL_P (last))
3711         {
3712           last = PREV_INSN (last);
3713           /* There was no CALL_INSN?  */
3714           gcc_assert (last != before_call);
3715         }
3716
3717       emit_barrier_after (last);
3718     }
3719
3720   /* Now restore inhibit_defer_pop to its actual original value.  */
3721   OK_DEFER_POP;
3722
3723   /* If call is cse'able, make appropriate pair of reg-notes around it.
3724      Test valreg so we don't crash; may safely ignore `const'
3725      if return type is void.  Disable for PARALLEL return values, because
3726      we have no way to move such values into a pseudo register.  */
3727   if (flags & ECF_LIBCALL_BLOCK)
3728     {
3729       rtx insns;
3730
3731       if (valreg == 0)
3732         {
3733           insns = get_insns ();
3734           end_sequence ();
3735           emit_insn (insns);
3736         }
3737       else
3738         {
3739           rtx note = 0;
3740           rtx temp;
3741           int i;
3742
3743           if (GET_CODE (valreg) == PARALLEL)
3744             {
3745               temp = gen_reg_rtx (outmode);
3746               emit_group_store (temp, valreg, NULL_TREE,
3747                                 GET_MODE_SIZE (outmode));
3748               valreg = temp;
3749             }
3750
3751           temp = gen_reg_rtx (GET_MODE (valreg));
3752
3753           /* Construct an "equal form" for the value which mentions all the
3754              arguments in order as well as the function name.  */
3755           for (i = 0; i < nargs; i++)
3756             note = gen_rtx_EXPR_LIST (VOIDmode, argvec[i].value, note);
3757           note = gen_rtx_EXPR_LIST (VOIDmode, fun, note);
3758
3759           insns = get_insns ();
3760           end_sequence ();
3761
3762           if (flags & ECF_PURE)
3763             note = gen_rtx_EXPR_LIST (VOIDmode,
3764                         gen_rtx_USE (VOIDmode,
3765                                      gen_rtx_MEM (BLKmode,
3766                                                   gen_rtx_SCRATCH (VOIDmode))),
3767                         note);
3768
3769           emit_libcall_block (insns, temp, valreg, note);
3770
3771           valreg = temp;
3772         }
3773     }
3774   pop_temp_slots ();
3775
3776   /* Copy the value to the right place.  */
3777   if (outmode != VOIDmode && retval)
3778     {
3779       if (mem_value)
3780         {
3781           if (value == 0)
3782             value = mem_value;
3783           if (value != mem_value)
3784             emit_move_insn (value, mem_value);
3785         }
3786       else if (GET_CODE (valreg) == PARALLEL)
3787         {
3788           if (value == 0)
3789             value = gen_reg_rtx (outmode);
3790           emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
3791         }
3792       else if (value != 0)
3793         emit_move_insn (value, valreg);
3794       else
3795         value = valreg;
3796     }
3797
3798   if (ACCUMULATE_OUTGOING_ARGS)
3799     {
3800 #ifdef REG_PARM_STACK_SPACE
3801       if (save_area)
3802         restore_fixed_argument_area (save_area, argblock,
3803                                      high_to_save, low_to_save);
3804 #endif
3805
3806       /* If we saved any argument areas, restore them.  */
3807       for (count = 0; count < nargs; count++)
3808         if (argvec[count].save_area)
3809           {
3810             enum machine_mode save_mode = GET_MODE (argvec[count].save_area);
3811             rtx adr = plus_constant (argblock,
3812                                      argvec[count].locate.offset.constant);
3813             rtx stack_area = gen_rtx_MEM (save_mode,
3814                                           memory_address (save_mode, adr));
3815
3816             if (save_mode == BLKmode)
3817               emit_block_move (stack_area,
3818                                validize_mem (argvec[count].save_area),
3819                                GEN_INT (argvec[count].locate.size.constant),
3820                                BLOCK_OP_CALL_PARM);
3821             else
3822               emit_move_insn (stack_area, argvec[count].save_area);
3823           }
3824
3825       highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3826       stack_usage_map = initial_stack_usage_map;
3827     }
3828
3829   return value;
3830
3831 }
3832 \f
3833 /* Output a library call to function FUN (a SYMBOL_REF rtx)
3834    (emitting the queue unless NO_QUEUE is nonzero),
3835    for a value of mode OUTMODE,
3836    with NARGS different arguments, passed as alternating rtx values
3837    and machine_modes to convert them to.
3838
3839    FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for `const'
3840    calls, LCT_PURE for `pure' calls, LCT_CONST_MAKE_BLOCK for `const' calls
3841    which should be enclosed in REG_LIBCALL/REG_RETVAL notes,
3842    LCT_PURE_MAKE_BLOCK for `purep' calls which should be enclosed in
3843    REG_LIBCALL/REG_RETVAL notes with extra (use (memory (scratch)),
3844    or other LCT_ value for other types of library calls.  */
3845
3846 void
3847 emit_library_call (rtx orgfun, enum libcall_type fn_type,
3848                    enum machine_mode outmode, int nargs, ...)
3849 {
3850   va_list p;
3851
3852   va_start (p, nargs);
3853   emit_library_call_value_1 (0, orgfun, NULL_RTX, fn_type, outmode, nargs, p);
3854   va_end (p);
3855 }
3856 \f
3857 /* Like emit_library_call except that an extra argument, VALUE,
3858    comes second and says where to store the result.
3859    (If VALUE is zero, this function chooses a convenient way
3860    to return the value.
3861
3862    This function returns an rtx for where the value is to be found.
3863    If VALUE is nonzero, VALUE is returned.  */
3864
3865 rtx
3866 emit_library_call_value (rtx orgfun, rtx value,
3867                          enum libcall_type fn_type,
3868                          enum machine_mode outmode, int nargs, ...)
3869 {
3870   rtx result;
3871   va_list p;
3872
3873   va_start (p, nargs);
3874   result = emit_library_call_value_1 (1, orgfun, value, fn_type, outmode,
3875                                       nargs, p);
3876   va_end (p);
3877
3878   return result;
3879 }
3880 \f
3881 /* Store a single argument for a function call
3882    into the register or memory area where it must be passed.
3883    *ARG describes the argument value and where to pass it.
3884
3885    ARGBLOCK is the address of the stack-block for all the arguments,
3886    or 0 on a machine where arguments are pushed individually.
3887
3888    MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
3889    so must be careful about how the stack is used.
3890
3891    VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
3892    argument stack.  This is used if ACCUMULATE_OUTGOING_ARGS to indicate
3893    that we need not worry about saving and restoring the stack.
3894
3895    FNDECL is the declaration of the function we are calling.
3896
3897    Return nonzero if this arg should cause sibcall failure,
3898    zero otherwise.  */
3899
3900 static int
3901 store_one_arg (struct arg_data *arg, rtx argblock, int flags,
3902                int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
3903 {
3904   tree pval = arg->tree_value;
3905   rtx reg = 0;
3906   int partial = 0;
3907   int used = 0;
3908   int i, lower_bound = 0, upper_bound = 0;
3909   int sibcall_failure = 0;
3910
3911   if (TREE_CODE (pval) == ERROR_MARK)
3912     return 1;
3913
3914   /* Push a new temporary level for any temporaries we make for
3915      this argument.  */
3916   push_temp_slots ();
3917
3918   if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
3919     {
3920       /* If this is being stored into a pre-allocated, fixed-size, stack area,
3921          save any previous data at that location.  */
3922       if (argblock && ! variable_size && arg->stack)
3923         {
3924 #ifdef ARGS_GROW_DOWNWARD
3925           /* stack_slot is negative, but we want to index stack_usage_map
3926              with positive values.  */
3927           if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
3928             upper_bound = -INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1)) + 1;
3929           else
3930             upper_bound = 0;
3931
3932           lower_bound = upper_bound - arg->locate.size.constant;
3933 #else
3934           if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
3935             lower_bound = INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1));
3936           else
3937             lower_bound = 0;
3938
3939           upper_bound = lower_bound + arg->locate.size.constant;
3940 #endif
3941
3942           i = lower_bound;
3943           /* Don't worry about things in the fixed argument area;
3944              it has already been saved.  */
3945           if (i < reg_parm_stack_space)
3946             i = reg_parm_stack_space;
3947           while (i < upper_bound && stack_usage_map[i] == 0)
3948             i++;
3949
3950           if (i < upper_bound)
3951             {
3952               /* We need to make a save area.  */
3953               unsigned int size = arg->locate.size.constant * BITS_PER_UNIT;
3954               enum machine_mode save_mode = mode_for_size (size, MODE_INT, 1);
3955               rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
3956               rtx stack_area = gen_rtx_MEM (save_mode, adr);
3957
3958               if (save_mode == BLKmode)
3959                 {
3960                   tree ot = TREE_TYPE (arg->tree_value);
3961                   tree nt = build_qualified_type (ot, (TYPE_QUALS (ot)
3962                                                        | TYPE_QUAL_CONST));
3963
3964                   arg->save_area = assign_temp (nt, 0, 1, 1);
3965                   preserve_temp_slots (arg->save_area);
3966                   emit_block_move (validize_mem (arg->save_area), stack_area,
3967                                    expr_size (arg->tree_value),
3968                                    BLOCK_OP_CALL_PARM);
3969                 }
3970               else
3971                 {
3972                   arg->save_area = gen_reg_rtx (save_mode);
3973                   emit_move_insn (arg->save_area, stack_area);
3974                 }
3975             }
3976         }
3977     }
3978
3979   /* If this isn't going to be placed on both the stack and in registers,
3980      set up the register and number of words.  */
3981   if (! arg->pass_on_stack)
3982     {
3983       if (flags & ECF_SIBCALL)
3984         reg = arg->tail_call_reg;
3985       else
3986         reg = arg->reg;
3987       partial = arg->partial;
3988     }
3989
3990   /* Being passed entirely in a register.  We shouldn't be called in
3991      this case.  */
3992   gcc_assert (reg == 0 || partial != 0);
3993   
3994   /* If this arg needs special alignment, don't load the registers
3995      here.  */
3996   if (arg->n_aligned_regs != 0)
3997     reg = 0;
3998
3999   /* If this is being passed partially in a register, we can't evaluate
4000      it directly into its stack slot.  Otherwise, we can.  */
4001   if (arg->value == 0)
4002     {
4003       /* stack_arg_under_construction is nonzero if a function argument is
4004          being evaluated directly into the outgoing argument list and
4005          expand_call must take special action to preserve the argument list
4006          if it is called recursively.
4007
4008          For scalar function arguments stack_usage_map is sufficient to
4009          determine which stack slots must be saved and restored.  Scalar
4010          arguments in general have pass_on_stack == 0.
4011
4012          If this argument is initialized by a function which takes the
4013          address of the argument (a C++ constructor or a C function
4014          returning a BLKmode structure), then stack_usage_map is
4015          insufficient and expand_call must push the stack around the
4016          function call.  Such arguments have pass_on_stack == 1.
4017
4018          Note that it is always safe to set stack_arg_under_construction,
4019          but this generates suboptimal code if set when not needed.  */
4020
4021       if (arg->pass_on_stack)
4022         stack_arg_under_construction++;
4023
4024       arg->value = expand_expr (pval,
4025                                 (partial
4026                                  || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
4027                                 ? NULL_RTX : arg->stack,
4028                                 VOIDmode, EXPAND_STACK_PARM);
4029
4030       /* If we are promoting object (or for any other reason) the mode
4031          doesn't agree, convert the mode.  */
4032
4033       if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
4034         arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
4035                                     arg->value, arg->unsignedp);
4036
4037       if (arg->pass_on_stack)
4038         stack_arg_under_construction--;
4039     }
4040
4041   /* Don't allow anything left on stack from computation
4042      of argument to alloca.  */
4043   if (flags & ECF_MAY_BE_ALLOCA)
4044     do_pending_stack_adjust ();
4045
4046   if (arg->value == arg->stack)
4047     /* If the value is already in the stack slot, we are done.  */
4048     ;
4049   else if (arg->mode != BLKmode)
4050     {
4051       int size;
4052
4053       /* Argument is a scalar, not entirely passed in registers.
4054          (If part is passed in registers, arg->partial says how much
4055          and emit_push_insn will take care of putting it there.)
4056
4057          Push it, and if its size is less than the
4058          amount of space allocated to it,
4059          also bump stack pointer by the additional space.
4060          Note that in C the default argument promotions
4061          will prevent such mismatches.  */
4062
4063       size = GET_MODE_SIZE (arg->mode);
4064       /* Compute how much space the push instruction will push.
4065          On many machines, pushing a byte will advance the stack
4066          pointer by a halfword.  */
4067 #ifdef PUSH_ROUNDING
4068       size = PUSH_ROUNDING (size);
4069 #endif
4070       used = size;
4071
4072       /* Compute how much space the argument should get:
4073          round up to a multiple of the alignment for arguments.  */
4074       if (none != FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)))
4075         used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
4076                  / (PARM_BOUNDARY / BITS_PER_UNIT))
4077                 * (PARM_BOUNDARY / BITS_PER_UNIT));
4078
4079       /* This isn't already where we want it on the stack, so put it there.
4080          This can either be done with push or copy insns.  */
4081       emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), NULL_RTX,
4082                       PARM_BOUNDARY, partial, reg, used - size, argblock,
4083                       ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4084                       ARGS_SIZE_RTX (arg->locate.alignment_pad));
4085
4086       /* Unless this is a partially-in-register argument, the argument is now
4087          in the stack.  */
4088       if (partial == 0)
4089         arg->value = arg->stack;
4090     }
4091   else
4092     {
4093       /* BLKmode, at least partly to be pushed.  */
4094
4095       unsigned int parm_align;
4096       int excess;
4097       rtx size_rtx;
4098
4099       /* Pushing a nonscalar.
4100          If part is passed in registers, PARTIAL says how much
4101          and emit_push_insn will take care of putting it there.  */
4102
4103       /* Round its size up to a multiple
4104          of the allocation unit for arguments.  */
4105
4106       if (arg->locate.size.var != 0)
4107         {
4108           excess = 0;
4109           size_rtx = ARGS_SIZE_RTX (arg->locate.size);
4110         }
4111       else
4112         {
4113           /* PUSH_ROUNDING has no effect on us, because emit_push_insn
4114              for BLKmode is careful to avoid it.  */
4115           excess = (arg->locate.size.constant
4116                     - int_size_in_bytes (TREE_TYPE (pval))
4117                     + partial);
4118           size_rtx = expand_expr (size_in_bytes (TREE_TYPE (pval)),
4119                                   NULL_RTX, TYPE_MODE (sizetype), 0);
4120         }
4121
4122       /* Some types will require stricter alignment, which will be
4123          provided for elsewhere in argument layout.  */
4124       parm_align = MAX (PARM_BOUNDARY, TYPE_ALIGN (TREE_TYPE (pval)));
4125
4126       /* When an argument is padded down, the block is aligned to
4127          PARM_BOUNDARY, but the actual argument isn't.  */
4128       if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
4129         {
4130           if (arg->locate.size.var)
4131             parm_align = BITS_PER_UNIT;
4132           else if (excess)
4133             {
4134               unsigned int excess_align = (excess & -excess) * BITS_PER_UNIT;
4135               parm_align = MIN (parm_align, excess_align);
4136             }
4137         }
4138
4139       if ((flags & ECF_SIBCALL) && MEM_P (arg->value))
4140         {
4141           /* emit_push_insn might not work properly if arg->value and
4142              argblock + arg->locate.offset areas overlap.  */
4143           rtx x = arg->value;
4144           int i = 0;
4145
4146           if (XEXP (x, 0) == current_function_internal_arg_pointer
4147               || (GET_CODE (XEXP (x, 0)) == PLUS
4148                   && XEXP (XEXP (x, 0), 0) ==
4149                      current_function_internal_arg_pointer
4150                   && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4151             {
4152               if (XEXP (x, 0) != current_function_internal_arg_pointer)
4153                 i = INTVAL (XEXP (XEXP (x, 0), 1));
4154
4155               /* expand_call should ensure this.  */
4156               gcc_assert (!arg->locate.offset.var
4157                           && GET_CODE (size_rtx) == CONST_INT);
4158
4159               if (arg->locate.offset.constant > i)
4160                 {
4161                   if (arg->locate.offset.constant < i + INTVAL (size_rtx))
4162                     sibcall_failure = 1;
4163                 }
4164               else if (arg->locate.offset.constant < i)
4165                 {
4166                   if (i < arg->locate.offset.constant + INTVAL (size_rtx))
4167                     sibcall_failure = 1;
4168                 }
4169             }
4170         }
4171
4172       emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
4173                       parm_align, partial, reg, excess, argblock,
4174                       ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4175                       ARGS_SIZE_RTX (arg->locate.alignment_pad));
4176
4177       /* Unless this is a partially-in-register argument, the argument is now
4178          in the stack.
4179
4180          ??? Unlike the case above, in which we want the actual
4181          address of the data, so that we can load it directly into a
4182          register, here we want the address of the stack slot, so that
4183          it's properly aligned for word-by-word copying or something
4184          like that.  It's not clear that this is always correct.  */
4185       if (partial == 0)
4186         arg->value = arg->stack_slot;
4187     }
4188
4189   if (arg->reg && GET_CODE (arg->reg) == PARALLEL)
4190     {
4191       tree type = TREE_TYPE (arg->tree_value);
4192       arg->parallel_value
4193         = emit_group_load_into_temps (arg->reg, arg->value, type,
4194                                       int_size_in_bytes (type));
4195     }
4196
4197   /* Mark all slots this store used.  */
4198   if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
4199       && argblock && ! variable_size && arg->stack)
4200     for (i = lower_bound; i < upper_bound; i++)
4201       stack_usage_map[i] = 1;
4202
4203   /* Once we have pushed something, pops can't safely
4204      be deferred during the rest of the arguments.  */
4205   NO_DEFER_POP;
4206
4207   /* Free any temporary slots made in processing this argument.  Show
4208      that we might have taken the address of something and pushed that
4209      as an operand.  */
4210   preserve_temp_slots (NULL_RTX);
4211   free_temp_slots ();
4212   pop_temp_slots ();
4213
4214   return sibcall_failure;
4215 }
4216
4217 /* Nonzero if we do not know how to pass TYPE solely in registers.  */
4218
4219 bool
4220 must_pass_in_stack_var_size (enum machine_mode mode ATTRIBUTE_UNUSED,
4221                              tree type)
4222 {
4223   if (!type)
4224     return false;
4225
4226   /* If the type has variable size...  */
4227   if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4228     return true;
4229
4230   /* If the type is marked as addressable (it is required
4231      to be constructed into the stack)...  */
4232   if (TREE_ADDRESSABLE (type))
4233     return true;
4234
4235   return false;
4236 }
4237
4238 /* Another version of the TARGET_MUST_PASS_IN_STACK hook.  This one
4239    takes trailing padding of a structure into account.  */
4240 /* ??? Should be able to merge these two by examining BLOCK_REG_PADDING.  */
4241
4242 bool
4243 must_pass_in_stack_var_size_or_pad (enum machine_mode mode, tree type)
4244 {
4245   if (!type)
4246     return false;
4247
4248   /* If the type has variable size...  */
4249   if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4250     return true;
4251
4252   /* If the type is marked as addressable (it is required
4253      to be constructed into the stack)...  */
4254   if (TREE_ADDRESSABLE (type))
4255     return true;
4256
4257   /* If the padding and mode of the type is such that a copy into
4258      a register would put it into the wrong part of the register.  */
4259   if (mode == BLKmode
4260       && int_size_in_bytes (type) % (PARM_BOUNDARY / BITS_PER_UNIT)
4261       && (FUNCTION_ARG_PADDING (mode, type)
4262           == (BYTES_BIG_ENDIAN ? upward : downward)))
4263     return true;
4264
4265   return false;
4266 }