OSDN Git Service

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