OSDN Git Service

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