OSDN Git Service

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