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