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