Support TI mode and soft float on PA64
[official-gcc.git] / gcc / calls.c
blob27b59f26ad3de061e3fbd819a520f8546972170a
1 /* Convert function calls to rtl insns, for GNU C compiler.
2 Copyright (C) 1989-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
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"
63 /* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits. */
64 #define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
66 /* Data structure and subroutines used within expand_call. */
68 struct arg_data
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;
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;
125 /* Size of STACK_USAGE_MAP. */
126 static unsigned int highest_outgoing_arg_in_use;
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;
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;
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;
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;
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);
164 static tree split_complex_types (tree);
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
171 /* Return true if bytes [LOWER_BOUND, UPPER_BOUND) of the outgoing
172 stack region might already be in use. */
174 static bool
175 stack_region_maybe_used_p (poly_uint64 lower_bound, poly_uint64 upper_bound,
176 unsigned int reg_parm_stack_space)
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;
183 if (const_upper > stack_usage_watermark)
184 return true;
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;
196 /* Record that bytes [LOWER_BOUND, UPPER_BOUND) of the outgoing
197 stack region are now in use. */
199 static void
200 mark_stack_region_used (poly_uint64 lower_bound, poly_uint64 upper_bound)
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 for (unsigned HOST_WIDE_INT i = const_lower; i < const_upper; ++i)
206 stack_usage_map[i] = 1;
207 else
208 stack_usage_watermark = MIN (stack_usage_watermark, const_lower);
211 /* Force FUNEXP into a form suitable for the address of a CALL,
212 and return that as an rtx. Also load the static chain register
213 if FNDECL is a nested function.
215 CALL_FUSAGE points to a variable holding the prospective
216 CALL_INSN_FUNCTION_USAGE information. */
219 prepare_call_address (tree fndecl_or_type, rtx funexp, rtx static_chain_value,
220 rtx *call_fusage, int reg_parm_seen, int flags)
222 /* Make a valid memory address and copy constants through pseudo-regs,
223 but not for a constant address if -fno-function-cse. */
224 if (GET_CODE (funexp) != SYMBOL_REF)
226 /* If it's an indirect call by descriptor, generate code to perform
227 runtime identification of the pointer and load the descriptor. */
228 if ((flags & ECF_BY_DESCRIPTOR) && !flag_trampolines)
230 const int bit_val = targetm.calls.custom_function_descriptors;
231 rtx call_lab = gen_label_rtx ();
233 gcc_assert (fndecl_or_type && TYPE_P (fndecl_or_type));
234 fndecl_or_type
235 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
236 fndecl_or_type);
237 DECL_STATIC_CHAIN (fndecl_or_type) = 1;
238 rtx chain = targetm.calls.static_chain (fndecl_or_type, false);
240 if (GET_MODE (funexp) != Pmode)
241 funexp = convert_memory_address (Pmode, funexp);
243 /* Avoid long live ranges around function calls. */
244 funexp = copy_to_mode_reg (Pmode, funexp);
246 if (REG_P (chain))
247 emit_insn (gen_rtx_CLOBBER (VOIDmode, chain));
249 /* Emit the runtime identification pattern. */
250 rtx mask = gen_rtx_AND (Pmode, funexp, GEN_INT (bit_val));
251 emit_cmp_and_jump_insns (mask, const0_rtx, EQ, NULL_RTX, Pmode, 1,
252 call_lab);
254 /* Statically predict the branch to very likely taken. */
255 rtx_insn *insn = get_last_insn ();
256 if (JUMP_P (insn))
257 predict_insn_def (insn, PRED_BUILTIN_EXPECT, TAKEN);
259 /* Load the descriptor. */
260 rtx mem = gen_rtx_MEM (ptr_mode,
261 plus_constant (Pmode, funexp, - bit_val));
262 MEM_NOTRAP_P (mem) = 1;
263 mem = convert_memory_address (Pmode, mem);
264 emit_move_insn (chain, mem);
266 mem = gen_rtx_MEM (ptr_mode,
267 plus_constant (Pmode, funexp,
268 POINTER_SIZE / BITS_PER_UNIT
269 - bit_val));
270 MEM_NOTRAP_P (mem) = 1;
271 mem = convert_memory_address (Pmode, mem);
272 emit_move_insn (funexp, mem);
274 emit_label (call_lab);
276 if (REG_P (chain))
278 use_reg (call_fusage, chain);
279 STATIC_CHAIN_REG_P (chain) = 1;
282 /* Make sure we're not going to be overwritten below. */
283 gcc_assert (!static_chain_value);
286 /* If we are using registers for parameters, force the
287 function address into a register now. */
288 funexp = ((reg_parm_seen
289 && targetm.small_register_classes_for_mode_p (FUNCTION_MODE))
290 ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
291 : memory_address (FUNCTION_MODE, funexp));
293 else
295 /* funexp could be a SYMBOL_REF represents a function pointer which is
296 of ptr_mode. In this case, it should be converted into address mode
297 to be a valid address for memory rtx pattern. See PR 64971. */
298 if (GET_MODE (funexp) != Pmode)
299 funexp = convert_memory_address (Pmode, funexp);
301 if (!(flags & ECF_SIBCALL))
303 if (!NO_FUNCTION_CSE && optimize && ! flag_no_function_cse)
304 funexp = force_reg (Pmode, funexp);
308 if (static_chain_value != 0
309 && (TREE_CODE (fndecl_or_type) != FUNCTION_DECL
310 || DECL_STATIC_CHAIN (fndecl_or_type)))
312 rtx chain;
314 chain = targetm.calls.static_chain (fndecl_or_type, false);
315 static_chain_value = convert_memory_address (Pmode, static_chain_value);
317 emit_move_insn (chain, static_chain_value);
318 if (REG_P (chain))
320 use_reg (call_fusage, chain);
321 STATIC_CHAIN_REG_P (chain) = 1;
325 return funexp;
328 /* Generate instructions to call function FUNEXP,
329 and optionally pop the results.
330 The CALL_INSN is the first insn generated.
332 FNDECL is the declaration node of the function. This is given to the
333 hook TARGET_RETURN_POPS_ARGS to determine whether this function pops
334 its own args.
336 FUNTYPE is the data type of the function. This is given to the hook
337 TARGET_RETURN_POPS_ARGS to determine whether this function pops its
338 own args. We used to allow an identifier for library functions, but
339 that doesn't work when the return type is an aggregate type and the
340 calling convention says that the pointer to this aggregate is to be
341 popped by the callee.
343 STACK_SIZE is the number of bytes of arguments on the stack,
344 ROUNDED_STACK_SIZE is that number rounded up to
345 PREFERRED_STACK_BOUNDARY; zero if the size is variable. This is
346 both to put into the call insn and to generate explicit popping
347 code if necessary.
349 STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
350 It is zero if this call doesn't want a structure value.
352 NEXT_ARG_REG is the rtx that results from executing
353 targetm.calls.function_arg (&args_so_far,
354 function_arg_info::end_marker ());
355 just after all the args have had their registers assigned.
356 This could be whatever you like, but normally it is the first
357 arg-register beyond those used for args in this call,
358 or 0 if all the arg-registers are used in this call.
359 It is passed on to `gen_call' so you can put this info in the call insn.
361 VALREG is a hard register in which a value is returned,
362 or 0 if the call does not return a value.
364 OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
365 the args to this call were processed.
366 We restore `inhibit_defer_pop' to that value.
368 CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
369 denote registers used by the called function. */
371 static void
372 emit_call_1 (rtx funexp, tree fntree ATTRIBUTE_UNUSED, tree fndecl ATTRIBUTE_UNUSED,
373 tree funtype ATTRIBUTE_UNUSED,
374 poly_int64 stack_size ATTRIBUTE_UNUSED,
375 poly_int64 rounded_stack_size,
376 poly_int64 struct_value_size ATTRIBUTE_UNUSED,
377 rtx next_arg_reg ATTRIBUTE_UNUSED, rtx valreg,
378 int old_inhibit_defer_pop, rtx call_fusage, int ecf_flags,
379 cumulative_args_t args_so_far ATTRIBUTE_UNUSED)
381 rtx rounded_stack_size_rtx = gen_int_mode (rounded_stack_size, Pmode);
382 rtx call, funmem, pat;
383 int already_popped = 0;
384 poly_int64 n_popped = 0;
386 /* Sibling call patterns never pop arguments (no sibcall(_value)_pop
387 patterns exist). Any popping that the callee does on return will
388 be from our caller's frame rather than ours. */
389 if (!(ecf_flags & ECF_SIBCALL))
391 n_popped += targetm.calls.return_pops_args (fndecl, funtype, stack_size);
393 #ifdef CALL_POPS_ARGS
394 n_popped += CALL_POPS_ARGS (*get_cumulative_args (args_so_far));
395 #endif
398 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
399 and we don't want to load it into a register as an optimization,
400 because prepare_call_address already did it if it should be done. */
401 if (GET_CODE (funexp) != SYMBOL_REF)
402 funexp = memory_address (FUNCTION_MODE, funexp);
404 funmem = gen_rtx_MEM (FUNCTION_MODE, funexp);
405 if (fndecl && TREE_CODE (fndecl) == FUNCTION_DECL)
407 tree t = fndecl;
409 /* Although a built-in FUNCTION_DECL and its non-__builtin
410 counterpart compare equal and get a shared mem_attrs, they
411 produce different dump output in compare-debug compilations,
412 if an entry gets garbage collected in one compilation, then
413 adds a different (but equivalent) entry, while the other
414 doesn't run the garbage collector at the same spot and then
415 shares the mem_attr with the equivalent entry. */
416 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
418 tree t2 = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
419 if (t2)
420 t = t2;
423 set_mem_expr (funmem, t);
425 else if (fntree)
426 set_mem_expr (funmem, build_simple_mem_ref (CALL_EXPR_FN (fntree)));
428 if (ecf_flags & ECF_SIBCALL)
430 if (valreg)
431 pat = targetm.gen_sibcall_value (valreg, funmem,
432 rounded_stack_size_rtx,
433 next_arg_reg, NULL_RTX);
434 else
435 pat = targetm.gen_sibcall (funmem, rounded_stack_size_rtx,
436 next_arg_reg,
437 gen_int_mode (struct_value_size, Pmode));
439 /* If the target has "call" or "call_value" insns, then prefer them
440 if no arguments are actually popped. If the target does not have
441 "call" or "call_value" insns, then we must use the popping versions
442 even if the call has no arguments to pop. */
443 else if (maybe_ne (n_popped, 0)
444 || !(valreg
445 ? targetm.have_call_value ()
446 : targetm.have_call ()))
448 rtx n_pop = gen_int_mode (n_popped, Pmode);
450 /* If this subroutine pops its own args, record that in the call insn
451 if possible, for the sake of frame pointer elimination. */
453 if (valreg)
454 pat = targetm.gen_call_value_pop (valreg, funmem,
455 rounded_stack_size_rtx,
456 next_arg_reg, n_pop);
457 else
458 pat = targetm.gen_call_pop (funmem, rounded_stack_size_rtx,
459 next_arg_reg, n_pop);
461 already_popped = 1;
463 else
465 if (valreg)
466 pat = targetm.gen_call_value (valreg, funmem, rounded_stack_size_rtx,
467 next_arg_reg, NULL_RTX);
468 else
469 pat = targetm.gen_call (funmem, rounded_stack_size_rtx, next_arg_reg,
470 gen_int_mode (struct_value_size, Pmode));
472 emit_insn (pat);
474 /* Find the call we just emitted. */
475 rtx_call_insn *call_insn = last_call_insn ();
477 /* Some target create a fresh MEM instead of reusing the one provided
478 above. Set its MEM_EXPR. */
479 call = get_call_rtx_from (call_insn);
480 if (call
481 && MEM_EXPR (XEXP (call, 0)) == NULL_TREE
482 && MEM_EXPR (funmem) != NULL_TREE)
483 set_mem_expr (XEXP (call, 0), MEM_EXPR (funmem));
485 /* Put the register usage information there. */
486 add_function_usage_to (call_insn, call_fusage);
488 /* If this is a const call, then set the insn's unchanging bit. */
489 if (ecf_flags & ECF_CONST)
490 RTL_CONST_CALL_P (call_insn) = 1;
492 /* If this is a pure call, then set the insn's unchanging bit. */
493 if (ecf_flags & ECF_PURE)
494 RTL_PURE_CALL_P (call_insn) = 1;
496 /* If this is a const call, then set the insn's unchanging bit. */
497 if (ecf_flags & ECF_LOOPING_CONST_OR_PURE)
498 RTL_LOOPING_CONST_OR_PURE_CALL_P (call_insn) = 1;
500 /* Create a nothrow REG_EH_REGION note, if needed. */
501 make_reg_eh_region_note (call_insn, ecf_flags, 0);
503 if (ecf_flags & ECF_NORETURN)
504 add_reg_note (call_insn, REG_NORETURN, const0_rtx);
506 if (ecf_flags & ECF_RETURNS_TWICE)
508 add_reg_note (call_insn, REG_SETJMP, const0_rtx);
509 cfun->calls_setjmp = 1;
512 SIBLING_CALL_P (call_insn) = ((ecf_flags & ECF_SIBCALL) != 0);
514 /* Restore this now, so that we do defer pops for this call's args
515 if the context of the call as a whole permits. */
516 inhibit_defer_pop = old_inhibit_defer_pop;
518 if (maybe_ne (n_popped, 0))
520 if (!already_popped)
521 CALL_INSN_FUNCTION_USAGE (call_insn)
522 = gen_rtx_EXPR_LIST (VOIDmode,
523 gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx),
524 CALL_INSN_FUNCTION_USAGE (call_insn));
525 rounded_stack_size -= n_popped;
526 rounded_stack_size_rtx = gen_int_mode (rounded_stack_size, Pmode);
527 stack_pointer_delta -= n_popped;
529 add_args_size_note (call_insn, stack_pointer_delta);
531 /* If popup is needed, stack realign must use DRAP */
532 if (SUPPORTS_STACK_ALIGNMENT)
533 crtl->need_drap = true;
535 /* For noreturn calls when not accumulating outgoing args force
536 REG_ARGS_SIZE note to prevent crossjumping of calls with different
537 args sizes. */
538 else if (!ACCUMULATE_OUTGOING_ARGS && (ecf_flags & ECF_NORETURN) != 0)
539 add_args_size_note (call_insn, stack_pointer_delta);
541 if (!ACCUMULATE_OUTGOING_ARGS)
543 /* If returning from the subroutine does not automatically pop the args,
544 we need an instruction to pop them sooner or later.
545 Perhaps do it now; perhaps just record how much space to pop later.
547 If returning from the subroutine does pop the args, indicate that the
548 stack pointer will be changed. */
550 if (maybe_ne (rounded_stack_size, 0))
552 if (ecf_flags & ECF_NORETURN)
553 /* Just pretend we did the pop. */
554 stack_pointer_delta -= rounded_stack_size;
555 else if (flag_defer_pop && inhibit_defer_pop == 0
556 && ! (ecf_flags & (ECF_CONST | ECF_PURE)))
557 pending_stack_adjust += rounded_stack_size;
558 else
559 adjust_stack (rounded_stack_size_rtx);
562 /* When we accumulate outgoing args, we must avoid any stack manipulations.
563 Restore the stack pointer to its original value now. Usually
564 ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
565 On i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
566 popping variants of functions exist as well.
568 ??? We may optimize similar to defer_pop above, but it is
569 probably not worthwhile.
571 ??? It will be worthwhile to enable combine_stack_adjustments even for
572 such machines. */
573 else if (maybe_ne (n_popped, 0))
574 anti_adjust_stack (gen_int_mode (n_popped, Pmode));
577 /* Determine if the function identified by FNDECL is one with
578 special properties we wish to know about. Modify FLAGS accordingly.
580 For example, if the function might return more than one time (setjmp), then
581 set ECF_RETURNS_TWICE.
583 Set ECF_MAY_BE_ALLOCA for any memory allocation function that might allocate
584 space from the stack such as alloca. */
586 static int
587 special_function_p (const_tree fndecl, int flags)
589 tree name_decl = DECL_NAME (fndecl);
591 if (maybe_special_function_p (fndecl)
592 && IDENTIFIER_LENGTH (name_decl) <= 11)
594 const char *name = IDENTIFIER_POINTER (name_decl);
595 const char *tname = name;
597 /* We assume that alloca will always be called by name. It
598 makes no sense to pass it as a pointer-to-function to
599 anything that does not understand its behavior. */
600 if (IDENTIFIER_LENGTH (name_decl) == 6
601 && name[0] == 'a'
602 && ! strcmp (name, "alloca"))
603 flags |= ECF_MAY_BE_ALLOCA;
605 /* Disregard prefix _ or __. */
606 if (name[0] == '_')
608 if (name[1] == '_')
609 tname += 2;
610 else
611 tname += 1;
614 /* ECF_RETURNS_TWICE is safe even for -ffreestanding. */
615 if (! strcmp (tname, "setjmp")
616 || ! strcmp (tname, "sigsetjmp")
617 || ! strcmp (name, "savectx")
618 || ! strcmp (name, "vfork")
619 || ! strcmp (name, "getcontext"))
620 flags |= ECF_RETURNS_TWICE;
623 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
624 && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (fndecl)))
625 flags |= ECF_MAY_BE_ALLOCA;
627 return flags;
630 /* Return fnspec for DECL. */
632 static attr_fnspec
633 decl_fnspec (tree fndecl)
635 tree attr;
636 tree type = TREE_TYPE (fndecl);
637 if (type)
639 attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
640 if (attr)
642 return TREE_VALUE (TREE_VALUE (attr));
645 if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
646 return builtin_fnspec (fndecl);
647 return "";
650 /* Similar to special_function_p; return a set of ERF_ flags for the
651 function FNDECL. */
652 static int
653 decl_return_flags (tree fndecl)
655 attr_fnspec fnspec = decl_fnspec (fndecl);
657 unsigned int arg;
658 if (fnspec.returns_arg (&arg))
659 return ERF_RETURNS_ARG | arg;
661 if (fnspec.returns_noalias_p ())
662 return ERF_NOALIAS;
663 return 0;
666 /* Return nonzero when FNDECL represents a call to setjmp. */
669 setjmp_call_p (const_tree fndecl)
671 if (DECL_IS_RETURNS_TWICE (fndecl))
672 return ECF_RETURNS_TWICE;
673 return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
677 /* Return true if STMT may be an alloca call. */
679 bool
680 gimple_maybe_alloca_call_p (const gimple *stmt)
682 tree fndecl;
684 if (!is_gimple_call (stmt))
685 return false;
687 fndecl = gimple_call_fndecl (stmt);
688 if (fndecl && (special_function_p (fndecl, 0) & ECF_MAY_BE_ALLOCA))
689 return true;
691 return false;
694 /* Return true if STMT is a builtin alloca call. */
696 bool
697 gimple_alloca_call_p (const gimple *stmt)
699 tree fndecl;
701 if (!is_gimple_call (stmt))
702 return false;
704 fndecl = gimple_call_fndecl (stmt);
705 if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
706 switch (DECL_FUNCTION_CODE (fndecl))
708 CASE_BUILT_IN_ALLOCA:
709 return gimple_call_num_args (stmt) > 0;
710 default:
711 break;
714 return false;
717 /* Return true when exp contains a builtin alloca call. */
719 bool
720 alloca_call_p (const_tree exp)
722 tree fndecl;
723 if (TREE_CODE (exp) == CALL_EXPR
724 && (fndecl = get_callee_fndecl (exp))
725 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
726 switch (DECL_FUNCTION_CODE (fndecl))
728 CASE_BUILT_IN_ALLOCA:
729 return true;
730 default:
731 break;
734 return false;
737 /* Return TRUE if FNDECL is either a TM builtin or a TM cloned
738 function. Return FALSE otherwise. */
740 static bool
741 is_tm_builtin (const_tree fndecl)
743 if (fndecl == NULL)
744 return false;
746 if (decl_is_tm_clone (fndecl))
747 return true;
749 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
751 switch (DECL_FUNCTION_CODE (fndecl))
753 case BUILT_IN_TM_COMMIT:
754 case BUILT_IN_TM_COMMIT_EH:
755 case BUILT_IN_TM_ABORT:
756 case BUILT_IN_TM_IRREVOCABLE:
757 case BUILT_IN_TM_GETTMCLONE_IRR:
758 case BUILT_IN_TM_MEMCPY:
759 case BUILT_IN_TM_MEMMOVE:
760 case BUILT_IN_TM_MEMSET:
761 CASE_BUILT_IN_TM_STORE (1):
762 CASE_BUILT_IN_TM_STORE (2):
763 CASE_BUILT_IN_TM_STORE (4):
764 CASE_BUILT_IN_TM_STORE (8):
765 CASE_BUILT_IN_TM_STORE (FLOAT):
766 CASE_BUILT_IN_TM_STORE (DOUBLE):
767 CASE_BUILT_IN_TM_STORE (LDOUBLE):
768 CASE_BUILT_IN_TM_STORE (M64):
769 CASE_BUILT_IN_TM_STORE (M128):
770 CASE_BUILT_IN_TM_STORE (M256):
771 CASE_BUILT_IN_TM_LOAD (1):
772 CASE_BUILT_IN_TM_LOAD (2):
773 CASE_BUILT_IN_TM_LOAD (4):
774 CASE_BUILT_IN_TM_LOAD (8):
775 CASE_BUILT_IN_TM_LOAD (FLOAT):
776 CASE_BUILT_IN_TM_LOAD (DOUBLE):
777 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
778 CASE_BUILT_IN_TM_LOAD (M64):
779 CASE_BUILT_IN_TM_LOAD (M128):
780 CASE_BUILT_IN_TM_LOAD (M256):
781 case BUILT_IN_TM_LOG:
782 case BUILT_IN_TM_LOG_1:
783 case BUILT_IN_TM_LOG_2:
784 case BUILT_IN_TM_LOG_4:
785 case BUILT_IN_TM_LOG_8:
786 case BUILT_IN_TM_LOG_FLOAT:
787 case BUILT_IN_TM_LOG_DOUBLE:
788 case BUILT_IN_TM_LOG_LDOUBLE:
789 case BUILT_IN_TM_LOG_M64:
790 case BUILT_IN_TM_LOG_M128:
791 case BUILT_IN_TM_LOG_M256:
792 return true;
793 default:
794 break;
797 return false;
800 /* Detect flags (function attributes) from the function decl or type node. */
803 flags_from_decl_or_type (const_tree exp)
805 int flags = 0;
807 if (DECL_P (exp))
809 /* The function exp may have the `malloc' attribute. */
810 if (DECL_IS_MALLOC (exp))
811 flags |= ECF_MALLOC;
813 /* The function exp may have the `returns_twice' attribute. */
814 if (DECL_IS_RETURNS_TWICE (exp))
815 flags |= ECF_RETURNS_TWICE;
817 /* Process the pure and const attributes. */
818 if (TREE_READONLY (exp))
819 flags |= ECF_CONST;
820 if (DECL_PURE_P (exp))
821 flags |= ECF_PURE;
822 if (DECL_LOOPING_CONST_OR_PURE_P (exp))
823 flags |= ECF_LOOPING_CONST_OR_PURE;
825 if (DECL_IS_NOVOPS (exp))
826 flags |= ECF_NOVOPS;
827 if (lookup_attribute ("leaf", DECL_ATTRIBUTES (exp)))
828 flags |= ECF_LEAF;
829 if (lookup_attribute ("cold", DECL_ATTRIBUTES (exp)))
830 flags |= ECF_COLD;
832 if (TREE_NOTHROW (exp))
833 flags |= ECF_NOTHROW;
835 if (flag_tm)
837 if (is_tm_builtin (exp))
838 flags |= ECF_TM_BUILTIN;
839 else if ((flags & (ECF_CONST|ECF_NOVOPS)) != 0
840 || lookup_attribute ("transaction_pure",
841 TYPE_ATTRIBUTES (TREE_TYPE (exp))))
842 flags |= ECF_TM_PURE;
845 flags = special_function_p (exp, flags);
847 else if (TYPE_P (exp))
849 if (TYPE_READONLY (exp))
850 flags |= ECF_CONST;
852 if (flag_tm
853 && ((flags & ECF_CONST) != 0
854 || lookup_attribute ("transaction_pure", TYPE_ATTRIBUTES (exp))))
855 flags |= ECF_TM_PURE;
857 else
858 gcc_unreachable ();
860 if (TREE_THIS_VOLATILE (exp))
862 flags |= ECF_NORETURN;
863 if (flags & (ECF_CONST|ECF_PURE))
864 flags |= ECF_LOOPING_CONST_OR_PURE;
867 return flags;
870 /* Detect flags from a CALL_EXPR. */
873 call_expr_flags (const_tree t)
875 int flags;
876 tree decl = get_callee_fndecl (t);
878 if (decl)
879 flags = flags_from_decl_or_type (decl);
880 else if (CALL_EXPR_FN (t) == NULL_TREE)
881 flags = internal_fn_flags (CALL_EXPR_IFN (t));
882 else
884 tree type = TREE_TYPE (CALL_EXPR_FN (t));
885 if (type && TREE_CODE (type) == POINTER_TYPE)
886 flags = flags_from_decl_or_type (TREE_TYPE (type));
887 else
888 flags = 0;
889 if (CALL_EXPR_BY_DESCRIPTOR (t))
890 flags |= ECF_BY_DESCRIPTOR;
893 return flags;
896 /* Return true if ARG should be passed by invisible reference. */
898 bool
899 pass_by_reference (CUMULATIVE_ARGS *ca, function_arg_info arg)
901 if (tree type = arg.type)
903 /* If this type contains non-trivial constructors, then it is
904 forbidden for the middle-end to create any new copies. */
905 if (TREE_ADDRESSABLE (type))
906 return true;
908 /* GCC post 3.4 passes *all* variable sized types by reference. */
909 if (!TYPE_SIZE (type) || !poly_int_tree_p (TYPE_SIZE (type)))
910 return true;
912 /* If a record type should be passed the same as its first (and only)
913 member, use the type and mode of that member. */
914 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
916 arg.type = TREE_TYPE (first_field (type));
917 arg.mode = TYPE_MODE (arg.type);
921 return targetm.calls.pass_by_reference (pack_cumulative_args (ca), arg);
924 /* Return true if TYPE should be passed by reference when passed to
925 the "..." arguments of a function. */
927 bool
928 pass_va_arg_by_reference (tree type)
930 return pass_by_reference (NULL, function_arg_info (type, /*named=*/false));
933 /* Decide whether ARG, which occurs in the state described by CA,
934 should be passed by reference. Return true if so and update
935 ARG accordingly. */
937 bool
938 apply_pass_by_reference_rules (CUMULATIVE_ARGS *ca, function_arg_info &arg)
940 if (pass_by_reference (ca, arg))
942 arg.type = build_pointer_type (arg.type);
943 arg.mode = TYPE_MODE (arg.type);
944 arg.pass_by_reference = true;
945 return true;
947 return false;
950 /* Return true if ARG, which is passed by reference, should be callee
951 copied instead of caller copied. */
953 bool
954 reference_callee_copied (CUMULATIVE_ARGS *ca, const function_arg_info &arg)
956 if (arg.type && TREE_ADDRESSABLE (arg.type))
957 return false;
958 return targetm.calls.callee_copies (pack_cumulative_args (ca), arg);
962 /* Precompute all register parameters as described by ARGS, storing values
963 into fields within the ARGS array.
965 NUM_ACTUALS indicates the total number elements in the ARGS array.
967 Set REG_PARM_SEEN if we encounter a register parameter. */
969 static void
970 precompute_register_parameters (int num_actuals, struct arg_data *args,
971 int *reg_parm_seen)
973 int i;
975 *reg_parm_seen = 0;
977 for (i = 0; i < num_actuals; i++)
978 if (args[i].reg != 0 && ! args[i].pass_on_stack)
980 *reg_parm_seen = 1;
982 if (args[i].value == 0)
984 push_temp_slots ();
985 args[i].value = expand_normal (args[i].tree_value);
986 preserve_temp_slots (args[i].value);
987 pop_temp_slots ();
990 /* If we are to promote the function arg to a wider mode,
991 do it now. */
993 if (args[i].mode != TYPE_MODE (TREE_TYPE (args[i].tree_value)))
994 args[i].value
995 = convert_modes (args[i].mode,
996 TYPE_MODE (TREE_TYPE (args[i].tree_value)),
997 args[i].value, args[i].unsignedp);
999 /* If the value is a non-legitimate constant, force it into a
1000 pseudo now. TLS symbols sometimes need a call to resolve. */
1001 if (CONSTANT_P (args[i].value)
1002 && (!targetm.legitimate_constant_p (args[i].mode, args[i].value)
1003 || targetm.precompute_tls_p (args[i].mode, args[i].value)))
1004 args[i].value = force_reg (args[i].mode, args[i].value);
1006 /* If we're going to have to load the value by parts, pull the
1007 parts into pseudos. The part extraction process can involve
1008 non-trivial computation. */
1009 if (GET_CODE (args[i].reg) == PARALLEL)
1011 tree type = TREE_TYPE (args[i].tree_value);
1012 args[i].parallel_value
1013 = emit_group_load_into_temps (args[i].reg, args[i].value,
1014 type, int_size_in_bytes (type));
1017 /* If the value is expensive, and we are inside an appropriately
1018 short loop, put the value into a pseudo and then put the pseudo
1019 into the hard reg.
1021 For small register classes, also do this if this call uses
1022 register parameters. This is to avoid reload conflicts while
1023 loading the parameters registers. */
1025 else if ((! (REG_P (args[i].value)
1026 || (GET_CODE (args[i].value) == SUBREG
1027 && REG_P (SUBREG_REG (args[i].value)))))
1028 && args[i].mode != BLKmode
1029 && (set_src_cost (args[i].value, args[i].mode,
1030 optimize_insn_for_speed_p ())
1031 > COSTS_N_INSNS (1))
1032 && ((*reg_parm_seen
1033 && targetm.small_register_classes_for_mode_p (args[i].mode))
1034 || optimize))
1035 args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
1039 #ifdef REG_PARM_STACK_SPACE
1041 /* The argument list is the property of the called routine and it
1042 may clobber it. If the fixed area has been used for previous
1043 parameters, we must save and restore it. */
1045 static rtx
1046 save_fixed_argument_area (int reg_parm_stack_space, rtx argblock, int *low_to_save, int *high_to_save)
1048 unsigned int low;
1049 unsigned int high;
1051 /* Compute the boundary of the area that needs to be saved, if any. */
1052 high = reg_parm_stack_space;
1053 if (ARGS_GROW_DOWNWARD)
1054 high += 1;
1056 if (high > highest_outgoing_arg_in_use)
1057 high = highest_outgoing_arg_in_use;
1059 for (low = 0; low < high; low++)
1060 if (stack_usage_map[low] != 0 || low >= stack_usage_watermark)
1062 int num_to_save;
1063 machine_mode save_mode;
1064 int delta;
1065 rtx addr;
1066 rtx stack_area;
1067 rtx save_area;
1069 while (stack_usage_map[--high] == 0)
1072 *low_to_save = low;
1073 *high_to_save = high;
1075 num_to_save = high - low + 1;
1077 /* If we don't have the required alignment, must do this
1078 in BLKmode. */
1079 scalar_int_mode imode;
1080 if (int_mode_for_size (num_to_save * BITS_PER_UNIT, 1).exists (&imode)
1081 && (low & (MIN (GET_MODE_SIZE (imode),
1082 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)) == 0)
1083 save_mode = imode;
1084 else
1085 save_mode = BLKmode;
1087 if (ARGS_GROW_DOWNWARD)
1088 delta = -high;
1089 else
1090 delta = low;
1092 addr = plus_constant (Pmode, argblock, delta);
1093 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
1095 set_mem_align (stack_area, PARM_BOUNDARY);
1096 if (save_mode == BLKmode)
1098 save_area = assign_stack_temp (BLKmode, num_to_save);
1099 emit_block_move (validize_mem (save_area), stack_area,
1100 GEN_INT (num_to_save), BLOCK_OP_CALL_PARM);
1102 else
1104 save_area = gen_reg_rtx (save_mode);
1105 emit_move_insn (save_area, stack_area);
1108 return save_area;
1111 return NULL_RTX;
1114 static void
1115 restore_fixed_argument_area (rtx save_area, rtx argblock, int high_to_save, int low_to_save)
1117 machine_mode save_mode = GET_MODE (save_area);
1118 int delta;
1119 rtx addr, stack_area;
1121 if (ARGS_GROW_DOWNWARD)
1122 delta = -high_to_save;
1123 else
1124 delta = low_to_save;
1126 addr = plus_constant (Pmode, argblock, delta);
1127 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
1128 set_mem_align (stack_area, PARM_BOUNDARY);
1130 if (save_mode != BLKmode)
1131 emit_move_insn (stack_area, save_area);
1132 else
1133 emit_block_move (stack_area, validize_mem (save_area),
1134 GEN_INT (high_to_save - low_to_save + 1),
1135 BLOCK_OP_CALL_PARM);
1137 #endif /* REG_PARM_STACK_SPACE */
1139 /* If any elements in ARGS refer to parameters that are to be passed in
1140 registers, but not in memory, and whose alignment does not permit a
1141 direct copy into registers. Copy the values into a group of pseudos
1142 which we will later copy into the appropriate hard registers.
1144 Pseudos for each unaligned argument will be stored into the array
1145 args[argnum].aligned_regs. The caller is responsible for deallocating
1146 the aligned_regs array if it is nonzero. */
1148 static void
1149 store_unaligned_arguments_into_pseudos (struct arg_data *args, int num_actuals)
1151 int i, j;
1153 for (i = 0; i < num_actuals; i++)
1154 if (args[i].reg != 0 && ! args[i].pass_on_stack
1155 && GET_CODE (args[i].reg) != PARALLEL
1156 && args[i].mode == BLKmode
1157 && MEM_P (args[i].value)
1158 && (MEM_ALIGN (args[i].value)
1159 < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
1161 int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1162 int endian_correction = 0;
1164 if (args[i].partial)
1166 gcc_assert (args[i].partial % UNITS_PER_WORD == 0);
1167 args[i].n_aligned_regs = args[i].partial / UNITS_PER_WORD;
1169 else
1171 args[i].n_aligned_regs
1172 = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1175 args[i].aligned_regs = XNEWVEC (rtx, args[i].n_aligned_regs);
1177 /* Structures smaller than a word are normally aligned to the
1178 least significant byte. On a BYTES_BIG_ENDIAN machine,
1179 this means we must skip the empty high order bytes when
1180 calculating the bit offset. */
1181 if (bytes < UNITS_PER_WORD
1182 #ifdef BLOCK_REG_PADDING
1183 && (BLOCK_REG_PADDING (args[i].mode,
1184 TREE_TYPE (args[i].tree_value), 1)
1185 == PAD_DOWNWARD)
1186 #else
1187 && BYTES_BIG_ENDIAN
1188 #endif
1190 endian_correction = BITS_PER_WORD - bytes * BITS_PER_UNIT;
1192 for (j = 0; j < args[i].n_aligned_regs; j++)
1194 rtx reg = gen_reg_rtx (word_mode);
1195 rtx word = operand_subword_force (args[i].value, j, BLKmode);
1196 int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
1198 args[i].aligned_regs[j] = reg;
1199 word = extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
1200 word_mode, word_mode, false, NULL);
1202 /* There is no need to restrict this code to loading items
1203 in TYPE_ALIGN sized hunks. The bitfield instructions can
1204 load up entire word sized registers efficiently.
1206 ??? This may not be needed anymore.
1207 We use to emit a clobber here but that doesn't let later
1208 passes optimize the instructions we emit. By storing 0 into
1209 the register later passes know the first AND to zero out the
1210 bitfield being set in the register is unnecessary. The store
1211 of 0 will be deleted as will at least the first AND. */
1213 emit_move_insn (reg, const0_rtx);
1215 bytes -= bitsize / BITS_PER_UNIT;
1216 store_bit_field (reg, bitsize, endian_correction, 0, 0,
1217 word_mode, word, false);
1222 /* Issue an error if CALL_EXPR was flagged as requiring
1223 tall-call optimization. */
1225 void
1226 maybe_complain_about_tail_call (tree call_expr, const char *reason)
1228 gcc_assert (TREE_CODE (call_expr) == CALL_EXPR);
1229 if (!CALL_EXPR_MUST_TAIL_CALL (call_expr))
1230 return;
1232 error_at (EXPR_LOCATION (call_expr), "cannot tail-call: %s", reason);
1235 /* Fill in ARGS_SIZE and ARGS array based on the parameters found in
1236 CALL_EXPR EXP.
1238 NUM_ACTUALS is the total number of parameters.
1240 N_NAMED_ARGS is the total number of named arguments.
1242 STRUCT_VALUE_ADDR_VALUE is the implicit argument for a struct return
1243 value, or null.
1245 FNDECL is the tree code for the target of this call (if known)
1247 ARGS_SO_FAR holds state needed by the target to know where to place
1248 the next argument.
1250 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
1251 for arguments which are passed in registers.
1253 OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
1254 and may be modified by this routine.
1256 OLD_PENDING_ADJ, MUST_PREALLOCATE and FLAGS are pointers to integer
1257 flags which may be modified by this routine.
1259 MAY_TAILCALL is cleared if we encounter an invisible pass-by-reference
1260 that requires allocation of stack space.
1262 CALL_FROM_THUNK_P is true if this call is the jump from a thunk to
1263 the thunked-to function. */
1265 static void
1266 initialize_argument_information (int num_actuals ATTRIBUTE_UNUSED,
1267 struct arg_data *args,
1268 struct args_size *args_size,
1269 int n_named_args ATTRIBUTE_UNUSED,
1270 tree exp, tree struct_value_addr_value,
1271 tree fndecl, tree fntype,
1272 cumulative_args_t args_so_far,
1273 int reg_parm_stack_space,
1274 rtx *old_stack_level,
1275 poly_int64_pod *old_pending_adj,
1276 int *must_preallocate, int *ecf_flags,
1277 bool *may_tailcall, bool call_from_thunk_p)
1279 CUMULATIVE_ARGS *args_so_far_pnt = get_cumulative_args (args_so_far);
1280 location_t loc = EXPR_LOCATION (exp);
1282 /* Count arg position in order args appear. */
1283 int argpos;
1285 int i;
1287 args_size->constant = 0;
1288 args_size->var = 0;
1290 /* In this loop, we consider args in the order they are written.
1291 We fill up ARGS from the back. */
1293 i = num_actuals - 1;
1295 int j = i;
1296 call_expr_arg_iterator iter;
1297 tree arg;
1299 if (struct_value_addr_value)
1301 args[j].tree_value = struct_value_addr_value;
1302 j--;
1304 argpos = 0;
1305 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
1307 tree argtype = TREE_TYPE (arg);
1309 if (targetm.calls.split_complex_arg
1310 && argtype
1311 && TREE_CODE (argtype) == COMPLEX_TYPE
1312 && targetm.calls.split_complex_arg (argtype))
1314 tree subtype = TREE_TYPE (argtype);
1315 args[j].tree_value = build1 (REALPART_EXPR, subtype, arg);
1316 j--;
1317 args[j].tree_value = build1 (IMAGPART_EXPR, subtype, arg);
1319 else
1320 args[j].tree_value = arg;
1321 j--;
1322 argpos++;
1326 /* I counts args in order (to be) pushed; ARGPOS counts in order written. */
1327 for (argpos = 0; argpos < num_actuals; i--, argpos++)
1329 tree type = TREE_TYPE (args[i].tree_value);
1330 int unsignedp;
1332 /* Replace erroneous argument with constant zero. */
1333 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1334 args[i].tree_value = integer_zero_node, type = integer_type_node;
1336 /* If TYPE is a transparent union or record, pass things the way
1337 we would pass the first field of the union or record. We have
1338 already verified that the modes are the same. */
1339 if (RECORD_OR_UNION_TYPE_P (type) && TYPE_TRANSPARENT_AGGR (type))
1340 type = TREE_TYPE (first_field (type));
1342 /* Decide where to pass this arg.
1344 args[i].reg is nonzero if all or part is passed in registers.
1346 args[i].partial is nonzero if part but not all is passed in registers,
1347 and the exact value says how many bytes are passed in registers.
1349 args[i].pass_on_stack is nonzero if the argument must at least be
1350 computed on the stack. It may then be loaded back into registers
1351 if args[i].reg is nonzero.
1353 These decisions are driven by the FUNCTION_... macros and must agree
1354 with those made by function.c. */
1356 /* See if this argument should be passed by invisible reference. */
1357 function_arg_info arg (type, argpos < n_named_args);
1358 if (pass_by_reference (args_so_far_pnt, arg))
1360 const bool callee_copies
1361 = reference_callee_copied (args_so_far_pnt, arg);
1362 tree base;
1364 /* If we're compiling a thunk, pass directly the address of an object
1365 already in memory, instead of making a copy. Likewise if we want
1366 to make the copy in the callee instead of the caller. */
1367 if ((call_from_thunk_p || callee_copies)
1368 && TREE_CODE (args[i].tree_value) != WITH_SIZE_EXPR
1369 && ((base = get_base_address (args[i].tree_value)), true)
1370 && TREE_CODE (base) != SSA_NAME
1371 && (!DECL_P (base) || MEM_P (DECL_RTL (base))))
1373 /* We may have turned the parameter value into an SSA name.
1374 Go back to the original parameter so we can take the
1375 address. */
1376 if (TREE_CODE (args[i].tree_value) == SSA_NAME)
1378 gcc_assert (SSA_NAME_IS_DEFAULT_DEF (args[i].tree_value));
1379 args[i].tree_value = SSA_NAME_VAR (args[i].tree_value);
1380 gcc_assert (TREE_CODE (args[i].tree_value) == PARM_DECL);
1382 /* Argument setup code may have copied the value to register. We
1383 revert that optimization now because the tail call code must
1384 use the original location. */
1385 if (TREE_CODE (args[i].tree_value) == PARM_DECL
1386 && !MEM_P (DECL_RTL (args[i].tree_value))
1387 && DECL_INCOMING_RTL (args[i].tree_value)
1388 && MEM_P (DECL_INCOMING_RTL (args[i].tree_value)))
1389 set_decl_rtl (args[i].tree_value,
1390 DECL_INCOMING_RTL (args[i].tree_value));
1392 mark_addressable (args[i].tree_value);
1394 /* We can't use sibcalls if a callee-copied argument is
1395 stored in the current function's frame. */
1396 if (!call_from_thunk_p && DECL_P (base) && !TREE_STATIC (base))
1398 *may_tailcall = false;
1399 maybe_complain_about_tail_call (exp,
1400 "a callee-copied argument is"
1401 " stored in the current"
1402 " function's frame");
1405 args[i].tree_value = build_fold_addr_expr_loc (loc,
1406 args[i].tree_value);
1407 type = TREE_TYPE (args[i].tree_value);
1409 if (*ecf_flags & ECF_CONST)
1410 *ecf_flags &= ~(ECF_CONST | ECF_LOOPING_CONST_OR_PURE);
1412 else
1414 /* We make a copy of the object and pass the address to the
1415 function being called. */
1416 rtx copy;
1418 if (!COMPLETE_TYPE_P (type)
1419 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
1420 || (flag_stack_check == GENERIC_STACK_CHECK
1421 && compare_tree_int (TYPE_SIZE_UNIT (type),
1422 STACK_CHECK_MAX_VAR_SIZE) > 0))
1424 /* This is a variable-sized object. Make space on the stack
1425 for it. */
1426 rtx size_rtx = expr_size (args[i].tree_value);
1428 if (*old_stack_level == 0)
1430 emit_stack_save (SAVE_BLOCK, old_stack_level);
1431 *old_pending_adj = pending_stack_adjust;
1432 pending_stack_adjust = 0;
1435 /* We can pass TRUE as the 4th argument because we just
1436 saved the stack pointer and will restore it right after
1437 the call. */
1438 copy = allocate_dynamic_stack_space (size_rtx,
1439 TYPE_ALIGN (type),
1440 TYPE_ALIGN (type),
1441 max_int_size_in_bytes
1442 (type),
1443 true);
1444 copy = gen_rtx_MEM (BLKmode, copy);
1445 set_mem_attributes (copy, type, 1);
1447 else
1448 copy = assign_temp (type, 1, 0);
1450 store_expr (args[i].tree_value, copy, 0, false, false);
1452 /* Just change the const function to pure and then let
1453 the next test clear the pure based on
1454 callee_copies. */
1455 if (*ecf_flags & ECF_CONST)
1457 *ecf_flags &= ~ECF_CONST;
1458 *ecf_flags |= ECF_PURE;
1461 if (!callee_copies && *ecf_flags & ECF_PURE)
1462 *ecf_flags &= ~(ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
1464 args[i].tree_value
1465 = build_fold_addr_expr_loc (loc, make_tree (type, copy));
1466 type = TREE_TYPE (args[i].tree_value);
1467 *may_tailcall = false;
1468 maybe_complain_about_tail_call (exp,
1469 "argument must be passed"
1470 " by copying");
1472 arg.pass_by_reference = true;
1475 unsignedp = TYPE_UNSIGNED (type);
1476 arg.type = type;
1477 arg.mode
1478 = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
1479 fndecl ? TREE_TYPE (fndecl) : fntype, 0);
1481 args[i].unsignedp = unsignedp;
1482 args[i].mode = arg.mode;
1484 targetm.calls.warn_parameter_passing_abi (args_so_far, type);
1486 args[i].reg = targetm.calls.function_arg (args_so_far, arg);
1488 /* If this is a sibling call and the machine has register windows, the
1489 register window has to be unwinded before calling the routine, so
1490 arguments have to go into the incoming registers. */
1491 if (targetm.calls.function_incoming_arg != targetm.calls.function_arg)
1492 args[i].tail_call_reg
1493 = targetm.calls.function_incoming_arg (args_so_far, arg);
1494 else
1495 args[i].tail_call_reg = args[i].reg;
1497 if (args[i].reg)
1498 args[i].partial = targetm.calls.arg_partial_bytes (args_so_far, arg);
1500 args[i].pass_on_stack = targetm.calls.must_pass_in_stack (arg);
1502 /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
1503 it means that we are to pass this arg in the register(s) designated
1504 by the PARALLEL, but also to pass it in the stack. */
1505 if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
1506 && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
1507 args[i].pass_on_stack = 1;
1509 /* If this is an addressable type, we must preallocate the stack
1510 since we must evaluate the object into its final location.
1512 If this is to be passed in both registers and the stack, it is simpler
1513 to preallocate. */
1514 if (TREE_ADDRESSABLE (type)
1515 || (args[i].pass_on_stack && args[i].reg != 0))
1516 *must_preallocate = 1;
1518 /* Compute the stack-size of this argument. */
1519 if (args[i].reg == 0 || args[i].partial != 0
1520 || reg_parm_stack_space > 0
1521 || args[i].pass_on_stack)
1522 locate_and_pad_parm (arg.mode, type,
1523 #ifdef STACK_PARMS_IN_REG_PARM_AREA
1525 #else
1526 args[i].reg != 0,
1527 #endif
1528 reg_parm_stack_space,
1529 args[i].pass_on_stack ? 0 : args[i].partial,
1530 fndecl, args_size, &args[i].locate);
1531 #ifdef BLOCK_REG_PADDING
1532 else
1533 /* The argument is passed entirely in registers. See at which
1534 end it should be padded. */
1535 args[i].locate.where_pad =
1536 BLOCK_REG_PADDING (arg.mode, type,
1537 int_size_in_bytes (type) <= UNITS_PER_WORD);
1538 #endif
1540 /* Update ARGS_SIZE, the total stack space for args so far. */
1542 args_size->constant += args[i].locate.size.constant;
1543 if (args[i].locate.size.var)
1544 ADD_PARM_SIZE (*args_size, args[i].locate.size.var);
1546 /* Increment ARGS_SO_FAR, which has info about which arg-registers
1547 have been used, etc. */
1549 /* ??? Traditionally we've passed TYPE_MODE here, instead of the
1550 promoted_mode used for function_arg above. However, the
1551 corresponding handling of incoming arguments in function.c
1552 does pass the promoted mode. */
1553 arg.mode = TYPE_MODE (type);
1554 targetm.calls.function_arg_advance (args_so_far, arg);
1558 /* Update ARGS_SIZE to contain the total size for the argument block.
1559 Return the original constant component of the argument block's size.
1561 REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
1562 for arguments passed in registers. */
1564 static poly_int64
1565 compute_argument_block_size (int reg_parm_stack_space,
1566 struct args_size *args_size,
1567 tree fndecl ATTRIBUTE_UNUSED,
1568 tree fntype ATTRIBUTE_UNUSED,
1569 int preferred_stack_boundary ATTRIBUTE_UNUSED)
1571 poly_int64 unadjusted_args_size = args_size->constant;
1573 /* For accumulate outgoing args mode we don't need to align, since the frame
1574 will be already aligned. Align to STACK_BOUNDARY in order to prevent
1575 backends from generating misaligned frame sizes. */
1576 if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
1577 preferred_stack_boundary = STACK_BOUNDARY;
1579 /* Compute the actual size of the argument block required. The variable
1580 and constant sizes must be combined, the size may have to be rounded,
1581 and there may be a minimum required size. */
1583 if (args_size->var)
1585 args_size->var = ARGS_SIZE_TREE (*args_size);
1586 args_size->constant = 0;
1588 preferred_stack_boundary /= BITS_PER_UNIT;
1589 if (preferred_stack_boundary > 1)
1591 /* We don't handle this case yet. To handle it correctly we have
1592 to add the delta, round and subtract the delta.
1593 Currently no machine description requires this support. */
1594 gcc_assert (multiple_p (stack_pointer_delta,
1595 preferred_stack_boundary));
1596 args_size->var = round_up (args_size->var, preferred_stack_boundary);
1599 if (reg_parm_stack_space > 0)
1601 args_size->var
1602 = size_binop (MAX_EXPR, args_size->var,
1603 ssize_int (reg_parm_stack_space));
1605 /* The area corresponding to register parameters is not to count in
1606 the size of the block we need. So make the adjustment. */
1607 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1608 args_size->var
1609 = size_binop (MINUS_EXPR, args_size->var,
1610 ssize_int (reg_parm_stack_space));
1613 else
1615 preferred_stack_boundary /= BITS_PER_UNIT;
1616 if (preferred_stack_boundary < 1)
1617 preferred_stack_boundary = 1;
1618 args_size->constant = (aligned_upper_bound (args_size->constant
1619 + stack_pointer_delta,
1620 preferred_stack_boundary)
1621 - stack_pointer_delta);
1623 args_size->constant = upper_bound (args_size->constant,
1624 reg_parm_stack_space);
1626 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1627 args_size->constant -= reg_parm_stack_space;
1629 return unadjusted_args_size;
1632 /* Precompute parameters as needed for a function call.
1634 FLAGS is mask of ECF_* constants.
1636 NUM_ACTUALS is the number of arguments.
1638 ARGS is an array containing information for each argument; this
1639 routine fills in the INITIAL_VALUE and VALUE fields for each
1640 precomputed argument. */
1642 static void
1643 precompute_arguments (int num_actuals, struct arg_data *args)
1645 int i;
1647 /* If this is a libcall, then precompute all arguments so that we do not
1648 get extraneous instructions emitted as part of the libcall sequence. */
1650 /* If we preallocated the stack space, and some arguments must be passed
1651 on the stack, then we must precompute any parameter which contains a
1652 function call which will store arguments on the stack.
1653 Otherwise, evaluating the parameter may clobber previous parameters
1654 which have already been stored into the stack. (we have code to avoid
1655 such case by saving the outgoing stack arguments, but it results in
1656 worse code) */
1657 if (!ACCUMULATE_OUTGOING_ARGS)
1658 return;
1660 for (i = 0; i < num_actuals; i++)
1662 tree type;
1663 machine_mode mode;
1665 if (TREE_CODE (args[i].tree_value) != CALL_EXPR)
1666 continue;
1668 /* If this is an addressable type, we cannot pre-evaluate it. */
1669 type = TREE_TYPE (args[i].tree_value);
1670 gcc_assert (!TREE_ADDRESSABLE (type));
1672 args[i].initial_value = args[i].value
1673 = expand_normal (args[i].tree_value);
1675 mode = TYPE_MODE (type);
1676 if (mode != args[i].mode)
1678 int unsignedp = args[i].unsignedp;
1679 args[i].value
1680 = convert_modes (args[i].mode, mode,
1681 args[i].value, args[i].unsignedp);
1683 /* CSE will replace this only if it contains args[i].value
1684 pseudo, so convert it down to the declared mode using
1685 a SUBREG. */
1686 if (REG_P (args[i].value)
1687 && GET_MODE_CLASS (args[i].mode) == MODE_INT
1688 && promote_mode (type, mode, &unsignedp) != args[i].mode)
1690 args[i].initial_value
1691 = gen_lowpart_SUBREG (mode, args[i].value);
1692 SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1693 SUBREG_PROMOTED_SET (args[i].initial_value, args[i].unsignedp);
1699 /* Given the current state of MUST_PREALLOCATE and information about
1700 arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
1701 compute and return the final value for MUST_PREALLOCATE. */
1703 static int
1704 finalize_must_preallocate (int must_preallocate, int num_actuals,
1705 struct arg_data *args, struct args_size *args_size)
1707 /* See if we have or want to preallocate stack space.
1709 If we would have to push a partially-in-regs parm
1710 before other stack parms, preallocate stack space instead.
1712 If the size of some parm is not a multiple of the required stack
1713 alignment, we must preallocate.
1715 If the total size of arguments that would otherwise create a copy in
1716 a temporary (such as a CALL) is more than half the total argument list
1717 size, preallocation is faster.
1719 Another reason to preallocate is if we have a machine (like the m88k)
1720 where stack alignment is required to be maintained between every
1721 pair of insns, not just when the call is made. However, we assume here
1722 that such machines either do not have push insns (and hence preallocation
1723 would occur anyway) or the problem is taken care of with
1724 PUSH_ROUNDING. */
1726 if (! must_preallocate)
1728 int partial_seen = 0;
1729 poly_int64 copy_to_evaluate_size = 0;
1730 int i;
1732 for (i = 0; i < num_actuals && ! must_preallocate; i++)
1734 if (args[i].partial > 0 && ! args[i].pass_on_stack)
1735 partial_seen = 1;
1736 else if (partial_seen && args[i].reg == 0)
1737 must_preallocate = 1;
1739 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1740 && (TREE_CODE (args[i].tree_value) == CALL_EXPR
1741 || TREE_CODE (args[i].tree_value) == TARGET_EXPR
1742 || TREE_CODE (args[i].tree_value) == COND_EXPR
1743 || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
1744 copy_to_evaluate_size
1745 += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1748 if (maybe_ne (args_size->constant, 0)
1749 && maybe_ge (copy_to_evaluate_size * 2, args_size->constant))
1750 must_preallocate = 1;
1752 return must_preallocate;
1755 /* If we preallocated stack space, compute the address of each argument
1756 and store it into the ARGS array.
1758 We need not ensure it is a valid memory address here; it will be
1759 validized when it is used.
1761 ARGBLOCK is an rtx for the address of the outgoing arguments. */
1763 static void
1764 compute_argument_addresses (struct arg_data *args, rtx argblock, int num_actuals)
1766 if (argblock)
1768 rtx arg_reg = argblock;
1769 int i;
1770 poly_int64 arg_offset = 0;
1772 if (GET_CODE (argblock) == PLUS)
1774 arg_reg = XEXP (argblock, 0);
1775 arg_offset = rtx_to_poly_int64 (XEXP (argblock, 1));
1778 for (i = 0; i < num_actuals; i++)
1780 rtx offset = ARGS_SIZE_RTX (args[i].locate.offset);
1781 rtx slot_offset = ARGS_SIZE_RTX (args[i].locate.slot_offset);
1782 rtx addr;
1783 unsigned int align, boundary;
1784 poly_uint64 units_on_stack = 0;
1785 machine_mode partial_mode = VOIDmode;
1787 /* Skip this parm if it will not be passed on the stack. */
1788 if (! args[i].pass_on_stack
1789 && args[i].reg != 0
1790 && args[i].partial == 0)
1791 continue;
1793 if (TYPE_EMPTY_P (TREE_TYPE (args[i].tree_value)))
1794 continue;
1796 addr = simplify_gen_binary (PLUS, Pmode, arg_reg, offset);
1797 addr = plus_constant (Pmode, addr, arg_offset);
1799 if (args[i].partial != 0)
1801 /* Only part of the parameter is being passed on the stack.
1802 Generate a simple memory reference of the correct size. */
1803 units_on_stack = args[i].locate.size.constant;
1804 poly_uint64 bits_on_stack = units_on_stack * BITS_PER_UNIT;
1805 partial_mode = int_mode_for_size (bits_on_stack, 1).else_blk ();
1806 args[i].stack = gen_rtx_MEM (partial_mode, addr);
1807 set_mem_size (args[i].stack, units_on_stack);
1809 else
1811 args[i].stack = gen_rtx_MEM (args[i].mode, addr);
1812 set_mem_attributes (args[i].stack,
1813 TREE_TYPE (args[i].tree_value), 1);
1815 align = BITS_PER_UNIT;
1816 boundary = args[i].locate.boundary;
1817 poly_int64 offset_val;
1818 if (args[i].locate.where_pad != PAD_DOWNWARD)
1819 align = boundary;
1820 else if (poly_int_rtx_p (offset, &offset_val))
1822 align = least_bit_hwi (boundary);
1823 unsigned int offset_align
1824 = known_alignment (offset_val) * BITS_PER_UNIT;
1825 if (offset_align != 0)
1826 align = MIN (align, offset_align);
1828 set_mem_align (args[i].stack, align);
1830 addr = simplify_gen_binary (PLUS, Pmode, arg_reg, slot_offset);
1831 addr = plus_constant (Pmode, addr, arg_offset);
1833 if (args[i].partial != 0)
1835 /* Only part of the parameter is being passed on the stack.
1836 Generate a simple memory reference of the correct size.
1838 args[i].stack_slot = gen_rtx_MEM (partial_mode, addr);
1839 set_mem_size (args[i].stack_slot, units_on_stack);
1841 else
1843 args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
1844 set_mem_attributes (args[i].stack_slot,
1845 TREE_TYPE (args[i].tree_value), 1);
1847 set_mem_align (args[i].stack_slot, args[i].locate.boundary);
1849 /* Function incoming arguments may overlap with sibling call
1850 outgoing arguments and we cannot allow reordering of reads
1851 from function arguments with stores to outgoing arguments
1852 of sibling calls. */
1853 set_mem_alias_set (args[i].stack, 0);
1854 set_mem_alias_set (args[i].stack_slot, 0);
1859 /* Given a FNDECL and EXP, return an rtx suitable for use as a target address
1860 in a call instruction.
1862 FNDECL is the tree node for the target function. For an indirect call
1863 FNDECL will be NULL_TREE.
1865 ADDR is the operand 0 of CALL_EXPR for this call. */
1867 static rtx
1868 rtx_for_function_call (tree fndecl, tree addr)
1870 rtx funexp;
1872 /* Get the function to call, in the form of RTL. */
1873 if (fndecl)
1875 if (!TREE_USED (fndecl) && fndecl != current_function_decl)
1876 TREE_USED (fndecl) = 1;
1878 /* Get a SYMBOL_REF rtx for the function address. */
1879 funexp = XEXP (DECL_RTL (fndecl), 0);
1881 else
1882 /* Generate an rtx (probably a pseudo-register) for the address. */
1884 push_temp_slots ();
1885 funexp = expand_normal (addr);
1886 pop_temp_slots (); /* FUNEXP can't be BLKmode. */
1888 return funexp;
1891 /* Return the static chain for this function, if any. */
1894 rtx_for_static_chain (const_tree fndecl_or_type, bool incoming_p)
1896 if (DECL_P (fndecl_or_type) && !DECL_STATIC_CHAIN (fndecl_or_type))
1897 return NULL;
1899 return targetm.calls.static_chain (fndecl_or_type, incoming_p);
1902 /* Internal state for internal_arg_pointer_based_exp and its helpers. */
1903 static struct
1905 /* Last insn that has been scanned by internal_arg_pointer_based_exp_scan,
1906 or NULL_RTX if none has been scanned yet. */
1907 rtx_insn *scan_start;
1908 /* Vector indexed by REGNO - FIRST_PSEUDO_REGISTER, recording if a pseudo is
1909 based on crtl->args.internal_arg_pointer. The element is NULL_RTX if the
1910 pseudo isn't based on it, a CONST_INT offset if the pseudo is based on it
1911 with fixed offset, or PC if this is with variable or unknown offset. */
1912 vec<rtx> cache;
1913 } internal_arg_pointer_exp_state;
1915 static rtx internal_arg_pointer_based_exp (const_rtx, bool);
1917 /* Helper function for internal_arg_pointer_based_exp. Scan insns in
1918 the tail call sequence, starting with first insn that hasn't been
1919 scanned yet, and note for each pseudo on the LHS whether it is based
1920 on crtl->args.internal_arg_pointer or not, and what offset from that
1921 that pointer it has. */
1923 static void
1924 internal_arg_pointer_based_exp_scan (void)
1926 rtx_insn *insn, *scan_start = internal_arg_pointer_exp_state.scan_start;
1928 if (scan_start == NULL_RTX)
1929 insn = get_insns ();
1930 else
1931 insn = NEXT_INSN (scan_start);
1933 while (insn)
1935 rtx set = single_set (insn);
1936 if (set && REG_P (SET_DEST (set)) && !HARD_REGISTER_P (SET_DEST (set)))
1938 rtx val = NULL_RTX;
1939 unsigned int idx = REGNO (SET_DEST (set)) - FIRST_PSEUDO_REGISTER;
1940 /* Punt on pseudos set multiple times. */
1941 if (idx < internal_arg_pointer_exp_state.cache.length ()
1942 && (internal_arg_pointer_exp_state.cache[idx]
1943 != NULL_RTX))
1944 val = pc_rtx;
1945 else
1946 val = internal_arg_pointer_based_exp (SET_SRC (set), false);
1947 if (val != NULL_RTX)
1949 if (idx >= internal_arg_pointer_exp_state.cache.length ())
1950 internal_arg_pointer_exp_state.cache
1951 .safe_grow_cleared (idx + 1, true);
1952 internal_arg_pointer_exp_state.cache[idx] = val;
1955 if (NEXT_INSN (insn) == NULL_RTX)
1956 scan_start = insn;
1957 insn = NEXT_INSN (insn);
1960 internal_arg_pointer_exp_state.scan_start = scan_start;
1963 /* Compute whether RTL is based on crtl->args.internal_arg_pointer. Return
1964 NULL_RTX if RTL isn't based on it, a CONST_INT offset if RTL is based on
1965 it with fixed offset, or PC if this is with variable or unknown offset.
1966 TOPLEVEL is true if the function is invoked at the topmost level. */
1968 static rtx
1969 internal_arg_pointer_based_exp (const_rtx rtl, bool toplevel)
1971 if (CONSTANT_P (rtl))
1972 return NULL_RTX;
1974 if (rtl == crtl->args.internal_arg_pointer)
1975 return const0_rtx;
1977 if (REG_P (rtl) && HARD_REGISTER_P (rtl))
1978 return NULL_RTX;
1980 poly_int64 offset;
1981 if (GET_CODE (rtl) == PLUS && poly_int_rtx_p (XEXP (rtl, 1), &offset))
1983 rtx val = internal_arg_pointer_based_exp (XEXP (rtl, 0), toplevel);
1984 if (val == NULL_RTX || val == pc_rtx)
1985 return val;
1986 return plus_constant (Pmode, val, offset);
1989 /* When called at the topmost level, scan pseudo assignments in between the
1990 last scanned instruction in the tail call sequence and the latest insn
1991 in that sequence. */
1992 if (toplevel)
1993 internal_arg_pointer_based_exp_scan ();
1995 if (REG_P (rtl))
1997 unsigned int idx = REGNO (rtl) - FIRST_PSEUDO_REGISTER;
1998 if (idx < internal_arg_pointer_exp_state.cache.length ())
1999 return internal_arg_pointer_exp_state.cache[idx];
2001 return NULL_RTX;
2004 subrtx_iterator::array_type array;
2005 FOR_EACH_SUBRTX (iter, array, rtl, NONCONST)
2007 const_rtx x = *iter;
2008 if (REG_P (x) && internal_arg_pointer_based_exp (x, false) != NULL_RTX)
2009 return pc_rtx;
2010 if (MEM_P (x))
2011 iter.skip_subrtxes ();
2014 return NULL_RTX;
2017 /* Return true if SIZE bytes starting from address ADDR might overlap an
2018 already-clobbered argument area. This function is used to determine
2019 if we should give up a sibcall. */
2021 static bool
2022 mem_might_overlap_already_clobbered_arg_p (rtx addr, poly_uint64 size)
2024 poly_int64 i;
2025 unsigned HOST_WIDE_INT start, end;
2026 rtx val;
2028 if (bitmap_empty_p (stored_args_map)
2029 && stored_args_watermark == HOST_WIDE_INT_M1U)
2030 return false;
2031 val = internal_arg_pointer_based_exp (addr, true);
2032 if (val == NULL_RTX)
2033 return false;
2034 else if (!poly_int_rtx_p (val, &i))
2035 return true;
2037 if (known_eq (size, 0U))
2038 return false;
2040 if (STACK_GROWS_DOWNWARD)
2041 i -= crtl->args.pretend_args_size;
2042 else
2043 i += crtl->args.pretend_args_size;
2045 if (ARGS_GROW_DOWNWARD)
2046 i = -i - size;
2048 /* We can ignore any references to the function's pretend args,
2049 which at this point would manifest as negative values of I. */
2050 if (known_le (i, 0) && known_le (size, poly_uint64 (-i)))
2051 return false;
2053 start = maybe_lt (i, 0) ? 0 : constant_lower_bound (i);
2054 if (!(i + size).is_constant (&end))
2055 end = HOST_WIDE_INT_M1U;
2057 if (end > stored_args_watermark)
2058 return true;
2060 end = MIN (end, SBITMAP_SIZE (stored_args_map));
2061 for (unsigned HOST_WIDE_INT k = start; k < end; ++k)
2062 if (bitmap_bit_p (stored_args_map, k))
2063 return true;
2065 return false;
2068 /* Do the register loads required for any wholly-register parms or any
2069 parms which are passed both on the stack and in a register. Their
2070 expressions were already evaluated.
2072 Mark all register-parms as living through the call, putting these USE
2073 insns in the CALL_INSN_FUNCTION_USAGE field.
2075 When IS_SIBCALL, perform the check_sibcall_argument_overlap
2076 checking, setting *SIBCALL_FAILURE if appropriate. */
2078 static void
2079 load_register_parameters (struct arg_data *args, int num_actuals,
2080 rtx *call_fusage, int flags, int is_sibcall,
2081 int *sibcall_failure)
2083 int i, j;
2085 for (i = 0; i < num_actuals; i++)
2087 rtx reg = ((flags & ECF_SIBCALL)
2088 ? args[i].tail_call_reg : args[i].reg);
2089 if (reg)
2091 int partial = args[i].partial;
2092 int nregs;
2093 poly_int64 size = 0;
2094 HOST_WIDE_INT const_size = 0;
2095 rtx_insn *before_arg = get_last_insn ();
2096 tree type = TREE_TYPE (args[i].tree_value);
2097 if (RECORD_OR_UNION_TYPE_P (type) && TYPE_TRANSPARENT_AGGR (type))
2098 type = TREE_TYPE (first_field (type));
2099 /* Set non-negative if we must move a word at a time, even if
2100 just one word (e.g, partial == 4 && mode == DFmode). Set
2101 to -1 if we just use a normal move insn. This value can be
2102 zero if the argument is a zero size structure. */
2103 nregs = -1;
2104 if (GET_CODE (reg) == PARALLEL)
2106 else if (partial)
2108 gcc_assert (partial % UNITS_PER_WORD == 0);
2109 nregs = partial / UNITS_PER_WORD;
2111 else if (TYPE_MODE (type) == BLKmode)
2113 /* Variable-sized parameters should be described by a
2114 PARALLEL instead. */
2115 const_size = int_size_in_bytes (type);
2116 gcc_assert (const_size >= 0);
2117 nregs = (const_size + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
2118 size = const_size;
2120 else
2121 size = GET_MODE_SIZE (args[i].mode);
2123 /* Handle calls that pass values in multiple non-contiguous
2124 locations. The Irix 6 ABI has examples of this. */
2126 if (GET_CODE (reg) == PARALLEL)
2127 emit_group_move (reg, args[i].parallel_value);
2129 /* If simple case, just do move. If normal partial, store_one_arg
2130 has already loaded the register for us. In all other cases,
2131 load the register(s) from memory. */
2133 else if (nregs == -1)
2135 emit_move_insn (reg, args[i].value);
2136 #ifdef BLOCK_REG_PADDING
2137 /* Handle case where we have a value that needs shifting
2138 up to the msb. eg. a QImode value and we're padding
2139 upward on a BYTES_BIG_ENDIAN machine. */
2140 if (args[i].locate.where_pad
2141 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD))
2143 gcc_checking_assert (ordered_p (size, UNITS_PER_WORD));
2144 if (maybe_lt (size, UNITS_PER_WORD))
2146 rtx x;
2147 poly_int64 shift
2148 = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2150 /* Assigning REG here rather than a temp makes
2151 CALL_FUSAGE report the whole reg as used.
2152 Strictly speaking, the call only uses SIZE
2153 bytes at the msb end, but it doesn't seem worth
2154 generating rtl to say that. */
2155 reg = gen_rtx_REG (word_mode, REGNO (reg));
2156 x = expand_shift (LSHIFT_EXPR, word_mode,
2157 reg, shift, reg, 1);
2158 if (x != reg)
2159 emit_move_insn (reg, x);
2162 #endif
2165 /* If we have pre-computed the values to put in the registers in
2166 the case of non-aligned structures, copy them in now. */
2168 else if (args[i].n_aligned_regs != 0)
2169 for (j = 0; j < args[i].n_aligned_regs; j++)
2170 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
2171 args[i].aligned_regs[j]);
2173 else if (partial == 0 || args[i].pass_on_stack)
2175 /* SIZE and CONST_SIZE are 0 for partial arguments and
2176 the size of a BLKmode type otherwise. */
2177 gcc_checking_assert (known_eq (size, const_size));
2178 rtx mem = validize_mem (copy_rtx (args[i].value));
2180 /* Check for overlap with already clobbered argument area,
2181 providing that this has non-zero size. */
2182 if (is_sibcall
2183 && const_size != 0
2184 && (mem_might_overlap_already_clobbered_arg_p
2185 (XEXP (args[i].value, 0), const_size)))
2186 *sibcall_failure = 1;
2188 if (const_size % UNITS_PER_WORD == 0
2189 || MEM_ALIGN (mem) % BITS_PER_WORD == 0)
2190 move_block_to_reg (REGNO (reg), mem, nregs, args[i].mode);
2191 else
2193 if (nregs > 1)
2194 move_block_to_reg (REGNO (reg), mem, nregs - 1,
2195 args[i].mode);
2196 rtx dest = gen_rtx_REG (word_mode, REGNO (reg) + nregs - 1);
2197 unsigned int bitoff = (nregs - 1) * BITS_PER_WORD;
2198 unsigned int bitsize = const_size * BITS_PER_UNIT - bitoff;
2199 rtx x = extract_bit_field (mem, bitsize, bitoff, 1, dest,
2200 word_mode, word_mode, false,
2201 NULL);
2202 if (BYTES_BIG_ENDIAN)
2203 x = expand_shift (LSHIFT_EXPR, word_mode, x,
2204 BITS_PER_WORD - bitsize, dest, 1);
2205 if (x != dest)
2206 emit_move_insn (dest, x);
2209 /* Handle a BLKmode that needs shifting. */
2210 if (nregs == 1 && const_size < UNITS_PER_WORD
2211 #ifdef BLOCK_REG_PADDING
2212 && args[i].locate.where_pad == PAD_DOWNWARD
2213 #else
2214 && BYTES_BIG_ENDIAN
2215 #endif
2218 rtx dest = gen_rtx_REG (word_mode, REGNO (reg));
2219 int shift = (UNITS_PER_WORD - const_size) * BITS_PER_UNIT;
2220 enum tree_code dir = (BYTES_BIG_ENDIAN
2221 ? RSHIFT_EXPR : LSHIFT_EXPR);
2222 rtx x;
2224 x = expand_shift (dir, word_mode, dest, shift, dest, 1);
2225 if (x != dest)
2226 emit_move_insn (dest, x);
2230 /* When a parameter is a block, and perhaps in other cases, it is
2231 possible that it did a load from an argument slot that was
2232 already clobbered. */
2233 if (is_sibcall
2234 && check_sibcall_argument_overlap (before_arg, &args[i], 0))
2235 *sibcall_failure = 1;
2237 /* Handle calls that pass values in multiple non-contiguous
2238 locations. The Irix 6 ABI has examples of this. */
2239 if (GET_CODE (reg) == PARALLEL)
2240 use_group_regs (call_fusage, reg);
2241 else if (nregs == -1)
2242 use_reg_mode (call_fusage, reg, TYPE_MODE (type));
2243 else if (nregs > 0)
2244 use_regs (call_fusage, REGNO (reg), nregs);
2249 /* We need to pop PENDING_STACK_ADJUST bytes. But, if the arguments
2250 wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
2251 bytes, then we would need to push some additional bytes to pad the
2252 arguments. So, we try to compute an adjust to the stack pointer for an
2253 amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
2254 bytes. Then, when the arguments are pushed the stack will be perfectly
2255 aligned.
2257 Return true if this optimization is possible, storing the adjustment
2258 in ADJUSTMENT_OUT and setting ARGS_SIZE->CONSTANT to the number of
2259 bytes that should be popped after the call. */
2261 static bool
2262 combine_pending_stack_adjustment_and_call (poly_int64_pod *adjustment_out,
2263 poly_int64 unadjusted_args_size,
2264 struct args_size *args_size,
2265 unsigned int preferred_unit_stack_boundary)
2267 /* The number of bytes to pop so that the stack will be
2268 under-aligned by UNADJUSTED_ARGS_SIZE bytes. */
2269 poly_int64 adjustment;
2270 /* The alignment of the stack after the arguments are pushed, if we
2271 just pushed the arguments without adjust the stack here. */
2272 unsigned HOST_WIDE_INT unadjusted_alignment;
2274 if (!known_misalignment (stack_pointer_delta + unadjusted_args_size,
2275 preferred_unit_stack_boundary,
2276 &unadjusted_alignment))
2277 return false;
2279 /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
2280 as possible -- leaving just enough left to cancel out the
2281 UNADJUSTED_ALIGNMENT. In other words, we want to ensure that the
2282 PENDING_STACK_ADJUST is non-negative, and congruent to
2283 -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY. */
2285 /* Begin by trying to pop all the bytes. */
2286 unsigned HOST_WIDE_INT tmp_misalignment;
2287 if (!known_misalignment (pending_stack_adjust,
2288 preferred_unit_stack_boundary,
2289 &tmp_misalignment))
2290 return false;
2291 unadjusted_alignment -= tmp_misalignment;
2292 adjustment = pending_stack_adjust;
2293 /* Push enough additional bytes that the stack will be aligned
2294 after the arguments are pushed. */
2295 if (preferred_unit_stack_boundary > 1 && unadjusted_alignment)
2296 adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
2298 /* We need to know whether the adjusted argument size
2299 (UNADJUSTED_ARGS_SIZE - ADJUSTMENT) constitutes an allocation
2300 or a deallocation. */
2301 if (!ordered_p (adjustment, unadjusted_args_size))
2302 return false;
2304 /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
2305 bytes after the call. The right number is the entire
2306 PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
2307 by the arguments in the first place. */
2308 args_size->constant
2309 = pending_stack_adjust - adjustment + unadjusted_args_size;
2311 *adjustment_out = adjustment;
2312 return true;
2315 /* Scan X expression if it does not dereference any argument slots
2316 we already clobbered by tail call arguments (as noted in stored_args_map
2317 bitmap).
2318 Return nonzero if X expression dereferences such argument slots,
2319 zero otherwise. */
2321 static int
2322 check_sibcall_argument_overlap_1 (rtx x)
2324 RTX_CODE code;
2325 int i, j;
2326 const char *fmt;
2328 if (x == NULL_RTX)
2329 return 0;
2331 code = GET_CODE (x);
2333 /* We need not check the operands of the CALL expression itself. */
2334 if (code == CALL)
2335 return 0;
2337 if (code == MEM)
2338 return (mem_might_overlap_already_clobbered_arg_p
2339 (XEXP (x, 0), GET_MODE_SIZE (GET_MODE (x))));
2341 /* Scan all subexpressions. */
2342 fmt = GET_RTX_FORMAT (code);
2343 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2345 if (*fmt == 'e')
2347 if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
2348 return 1;
2350 else if (*fmt == 'E')
2352 for (j = 0; j < XVECLEN (x, i); j++)
2353 if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
2354 return 1;
2357 return 0;
2360 /* Scan sequence after INSN if it does not dereference any argument slots
2361 we already clobbered by tail call arguments (as noted in stored_args_map
2362 bitmap). If MARK_STORED_ARGS_MAP, add stack slots for ARG to
2363 stored_args_map bitmap afterwards (when ARG is a register MARK_STORED_ARGS_MAP
2364 should be 0). Return nonzero if sequence after INSN dereferences such argument
2365 slots, zero otherwise. */
2367 static int
2368 check_sibcall_argument_overlap (rtx_insn *insn, struct arg_data *arg,
2369 int mark_stored_args_map)
2371 poly_uint64 low, high;
2372 unsigned HOST_WIDE_INT const_low, const_high;
2374 if (insn == NULL_RTX)
2375 insn = get_insns ();
2376 else
2377 insn = NEXT_INSN (insn);
2379 for (; insn; insn = NEXT_INSN (insn))
2380 if (INSN_P (insn)
2381 && check_sibcall_argument_overlap_1 (PATTERN (insn)))
2382 break;
2384 if (mark_stored_args_map)
2386 if (ARGS_GROW_DOWNWARD)
2387 low = -arg->locate.slot_offset.constant - arg->locate.size.constant;
2388 else
2389 low = arg->locate.slot_offset.constant;
2390 high = low + arg->locate.size.constant;
2392 const_low = constant_lower_bound (low);
2393 if (high.is_constant (&const_high))
2394 for (unsigned HOST_WIDE_INT i = const_low; i < const_high; ++i)
2395 bitmap_set_bit (stored_args_map, i);
2396 else
2397 stored_args_watermark = MIN (stored_args_watermark, const_low);
2399 return insn != NULL_RTX;
2402 /* Given that a function returns a value of mode MODE at the most
2403 significant end of hard register VALUE, shift VALUE left or right
2404 as specified by LEFT_P. Return true if some action was needed. */
2406 bool
2407 shift_return_value (machine_mode mode, bool left_p, rtx value)
2409 gcc_assert (REG_P (value) && HARD_REGISTER_P (value));
2410 machine_mode value_mode = GET_MODE (value);
2411 poly_int64 shift = GET_MODE_BITSIZE (value_mode) - GET_MODE_BITSIZE (mode);
2413 if (known_eq (shift, 0))
2414 return false;
2416 /* Use ashr rather than lshr for right shifts. This is for the benefit
2417 of the MIPS port, which requires SImode values to be sign-extended
2418 when stored in 64-bit registers. */
2419 if (!force_expand_binop (value_mode, left_p ? ashl_optab : ashr_optab,
2420 value, gen_int_shift_amount (value_mode, shift),
2421 value, 1, OPTAB_WIDEN))
2422 gcc_unreachable ();
2423 return true;
2426 /* If X is a likely-spilled register value, copy it to a pseudo
2427 register and return that register. Return X otherwise. */
2429 static rtx
2430 avoid_likely_spilled_reg (rtx x)
2432 rtx new_rtx;
2434 if (REG_P (x)
2435 && HARD_REGISTER_P (x)
2436 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (x))))
2438 /* Make sure that we generate a REG rather than a CONCAT.
2439 Moves into CONCATs can need nontrivial instructions,
2440 and the whole point of this function is to avoid
2441 using the hard register directly in such a situation. */
2442 generating_concat_p = 0;
2443 new_rtx = gen_reg_rtx (GET_MODE (x));
2444 generating_concat_p = 1;
2445 emit_move_insn (new_rtx, x);
2446 return new_rtx;
2448 return x;
2451 /* Helper function for expand_call.
2452 Return false is EXP is not implementable as a sibling call. */
2454 static bool
2455 can_implement_as_sibling_call_p (tree exp,
2456 rtx structure_value_addr,
2457 tree funtype,
2458 tree fndecl,
2459 int flags,
2460 tree addr,
2461 const args_size &args_size)
2463 if (!targetm.have_sibcall_epilogue ())
2465 maybe_complain_about_tail_call
2466 (exp,
2467 "machine description does not have"
2468 " a sibcall_epilogue instruction pattern");
2469 return false;
2472 /* Doing sibling call optimization needs some work, since
2473 structure_value_addr can be allocated on the stack.
2474 It does not seem worth the effort since few optimizable
2475 sibling calls will return a structure. */
2476 if (structure_value_addr != NULL_RTX)
2478 maybe_complain_about_tail_call (exp, "callee returns a structure");
2479 return false;
2482 /* Check whether the target is able to optimize the call
2483 into a sibcall. */
2484 if (!targetm.function_ok_for_sibcall (fndecl, exp))
2486 maybe_complain_about_tail_call (exp,
2487 "target is not able to optimize the"
2488 " call into a sibling call");
2489 return false;
2492 /* Functions that do not return exactly once may not be sibcall
2493 optimized. */
2494 if (flags & ECF_RETURNS_TWICE)
2496 maybe_complain_about_tail_call (exp, "callee returns twice");
2497 return false;
2499 if (flags & ECF_NORETURN)
2501 maybe_complain_about_tail_call (exp, "callee does not return");
2502 return false;
2505 if (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (addr))))
2507 maybe_complain_about_tail_call (exp, "volatile function type");
2508 return false;
2511 /* If the called function is nested in the current one, it might access
2512 some of the caller's arguments, but could clobber them beforehand if
2513 the argument areas are shared. */
2514 if (fndecl && decl_function_context (fndecl) == current_function_decl)
2516 maybe_complain_about_tail_call (exp, "nested function");
2517 return false;
2520 /* If this function requires more stack slots than the current
2521 function, we cannot change it into a sibling call.
2522 crtl->args.pretend_args_size is not part of the
2523 stack allocated by our caller. */
2524 if (maybe_gt (args_size.constant,
2525 crtl->args.size - crtl->args.pretend_args_size))
2527 maybe_complain_about_tail_call (exp,
2528 "callee required more stack slots"
2529 " than the caller");
2530 return false;
2533 /* If the callee pops its own arguments, then it must pop exactly
2534 the same number of arguments as the current function. */
2535 if (maybe_ne (targetm.calls.return_pops_args (fndecl, funtype,
2536 args_size.constant),
2537 targetm.calls.return_pops_args (current_function_decl,
2538 TREE_TYPE
2539 (current_function_decl),
2540 crtl->args.size)))
2542 maybe_complain_about_tail_call (exp,
2543 "inconsistent number of"
2544 " popped arguments");
2545 return false;
2548 if (!lang_hooks.decls.ok_for_sibcall (fndecl))
2550 maybe_complain_about_tail_call (exp, "frontend does not support"
2551 " sibling call");
2552 return false;
2555 /* All checks passed. */
2556 return true;
2559 /* Update stack alignment when the parameter is passed in the stack
2560 since the outgoing parameter requires extra alignment on the calling
2561 function side. */
2563 static void
2564 update_stack_alignment_for_call (struct locate_and_pad_arg_data *locate)
2566 if (crtl->stack_alignment_needed < locate->boundary)
2567 crtl->stack_alignment_needed = locate->boundary;
2568 if (crtl->preferred_stack_boundary < locate->boundary)
2569 crtl->preferred_stack_boundary = locate->boundary;
2572 /* Generate all the code for a CALL_EXPR exp
2573 and return an rtx for its value.
2574 Store the value in TARGET (specified as an rtx) if convenient.
2575 If the value is stored in TARGET then TARGET is returned.
2576 If IGNORE is nonzero, then we ignore the value of the function call. */
2579 expand_call (tree exp, rtx target, int ignore)
2581 /* Nonzero if we are currently expanding a call. */
2582 static int currently_expanding_call = 0;
2584 /* RTX for the function to be called. */
2585 rtx funexp;
2586 /* Sequence of insns to perform a normal "call". */
2587 rtx_insn *normal_call_insns = NULL;
2588 /* Sequence of insns to perform a tail "call". */
2589 rtx_insn *tail_call_insns = NULL;
2590 /* Data type of the function. */
2591 tree funtype;
2592 tree type_arg_types;
2593 tree rettype;
2594 /* Declaration of the function being called,
2595 or 0 if the function is computed (not known by name). */
2596 tree fndecl = 0;
2597 /* The type of the function being called. */
2598 tree fntype;
2599 bool try_tail_call = CALL_EXPR_TAILCALL (exp);
2600 bool must_tail_call = CALL_EXPR_MUST_TAIL_CALL (exp);
2601 int pass;
2603 /* Register in which non-BLKmode value will be returned,
2604 or 0 if no value or if value is BLKmode. */
2605 rtx valreg;
2606 /* Address where we should return a BLKmode value;
2607 0 if value not BLKmode. */
2608 rtx structure_value_addr = 0;
2609 /* Nonzero if that address is being passed by treating it as
2610 an extra, implicit first parameter. Otherwise,
2611 it is passed by being copied directly into struct_value_rtx. */
2612 int structure_value_addr_parm = 0;
2613 /* Holds the value of implicit argument for the struct value. */
2614 tree structure_value_addr_value = NULL_TREE;
2615 /* Size of aggregate value wanted, or zero if none wanted
2616 or if we are using the non-reentrant PCC calling convention
2617 or expecting the value in registers. */
2618 poly_int64 struct_value_size = 0;
2619 /* Nonzero if called function returns an aggregate in memory PCC style,
2620 by returning the address of where to find it. */
2621 int pcc_struct_value = 0;
2622 rtx struct_value = 0;
2624 /* Number of actual parameters in this call, including struct value addr. */
2625 int num_actuals;
2626 /* Number of named args. Args after this are anonymous ones
2627 and they must all go on the stack. */
2628 int n_named_args;
2629 /* Number of complex actual arguments that need to be split. */
2630 int num_complex_actuals = 0;
2632 /* Vector of information about each argument.
2633 Arguments are numbered in the order they will be pushed,
2634 not the order they are written. */
2635 struct arg_data *args;
2637 /* Total size in bytes of all the stack-parms scanned so far. */
2638 struct args_size args_size;
2639 struct args_size adjusted_args_size;
2640 /* Size of arguments before any adjustments (such as rounding). */
2641 poly_int64 unadjusted_args_size;
2642 /* Data on reg parms scanned so far. */
2643 CUMULATIVE_ARGS args_so_far_v;
2644 cumulative_args_t args_so_far;
2645 /* Nonzero if a reg parm has been scanned. */
2646 int reg_parm_seen;
2647 /* Nonzero if this is an indirect function call. */
2649 /* Nonzero if we must avoid push-insns in the args for this call.
2650 If stack space is allocated for register parameters, but not by the
2651 caller, then it is preallocated in the fixed part of the stack frame.
2652 So the entire argument block must then be preallocated (i.e., we
2653 ignore PUSH_ROUNDING in that case). */
2655 int must_preallocate = !targetm.calls.push_argument (0);
2657 /* Size of the stack reserved for parameter registers. */
2658 int reg_parm_stack_space = 0;
2660 /* Address of space preallocated for stack parms
2661 (on machines that lack push insns), or 0 if space not preallocated. */
2662 rtx argblock = 0;
2664 /* Mask of ECF_ and ERF_ flags. */
2665 int flags = 0;
2666 int return_flags = 0;
2667 #ifdef REG_PARM_STACK_SPACE
2668 /* Define the boundary of the register parm stack space that needs to be
2669 saved, if any. */
2670 int low_to_save, high_to_save;
2671 rtx save_area = 0; /* Place that it is saved */
2672 #endif
2674 unsigned int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
2675 char *initial_stack_usage_map = stack_usage_map;
2676 unsigned HOST_WIDE_INT initial_stack_usage_watermark = stack_usage_watermark;
2677 char *stack_usage_map_buf = NULL;
2679 poly_int64 old_stack_allocated;
2681 /* State variables to track stack modifications. */
2682 rtx old_stack_level = 0;
2683 int old_stack_arg_under_construction = 0;
2684 poly_int64 old_pending_adj = 0;
2685 int old_inhibit_defer_pop = inhibit_defer_pop;
2687 /* Some stack pointer alterations we make are performed via
2688 allocate_dynamic_stack_space. This modifies the stack_pointer_delta,
2689 which we then also need to save/restore along the way. */
2690 poly_int64 old_stack_pointer_delta = 0;
2692 rtx call_fusage;
2693 tree addr = CALL_EXPR_FN (exp);
2694 int i;
2695 /* The alignment of the stack, in bits. */
2696 unsigned HOST_WIDE_INT preferred_stack_boundary;
2697 /* The alignment of the stack, in bytes. */
2698 unsigned HOST_WIDE_INT preferred_unit_stack_boundary;
2699 /* The static chain value to use for this call. */
2700 rtx static_chain_value;
2701 /* See if this is "nothrow" function call. */
2702 if (TREE_NOTHROW (exp))
2703 flags |= ECF_NOTHROW;
2705 /* See if we can find a DECL-node for the actual function, and get the
2706 function attributes (flags) from the function decl or type node. */
2707 fndecl = get_callee_fndecl (exp);
2708 if (fndecl)
2710 fntype = TREE_TYPE (fndecl);
2711 flags |= flags_from_decl_or_type (fndecl);
2712 return_flags |= decl_return_flags (fndecl);
2714 else
2716 fntype = TREE_TYPE (TREE_TYPE (addr));
2717 flags |= flags_from_decl_or_type (fntype);
2718 if (CALL_EXPR_BY_DESCRIPTOR (exp))
2719 flags |= ECF_BY_DESCRIPTOR;
2721 rettype = TREE_TYPE (exp);
2723 struct_value = targetm.calls.struct_value_rtx (fntype, 0);
2725 /* Warn if this value is an aggregate type,
2726 regardless of which calling convention we are using for it. */
2727 if (AGGREGATE_TYPE_P (rettype))
2728 warning (OPT_Waggregate_return, "function call has aggregate value");
2730 /* If the result of a non looping pure or const function call is
2731 ignored (or void), and none of its arguments are volatile, we can
2732 avoid expanding the call and just evaluate the arguments for
2733 side-effects. */
2734 if ((flags & (ECF_CONST | ECF_PURE))
2735 && (!(flags & ECF_LOOPING_CONST_OR_PURE))
2736 && (flags & ECF_NOTHROW)
2737 && (ignore || target == const0_rtx
2738 || TYPE_MODE (rettype) == VOIDmode))
2740 bool volatilep = false;
2741 tree arg;
2742 call_expr_arg_iterator iter;
2744 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2745 if (TREE_THIS_VOLATILE (arg))
2747 volatilep = true;
2748 break;
2751 if (! volatilep)
2753 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2754 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
2755 return const0_rtx;
2759 #ifdef REG_PARM_STACK_SPACE
2760 reg_parm_stack_space = REG_PARM_STACK_SPACE (!fndecl ? fntype : fndecl);
2761 #endif
2763 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
2764 && reg_parm_stack_space > 0 && targetm.calls.push_argument (0))
2765 must_preallocate = 1;
2767 /* Set up a place to return a structure. */
2769 /* Cater to broken compilers. */
2770 if (aggregate_value_p (exp, fntype))
2772 /* This call returns a big structure. */
2773 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
2775 #ifdef PCC_STATIC_STRUCT_RETURN
2777 pcc_struct_value = 1;
2779 #else /* not PCC_STATIC_STRUCT_RETURN */
2781 if (!poly_int_tree_p (TYPE_SIZE_UNIT (rettype), &struct_value_size))
2782 struct_value_size = -1;
2784 /* Even if it is semantically safe to use the target as the return
2785 slot, it may be not sufficiently aligned for the return type. */
2786 if (CALL_EXPR_RETURN_SLOT_OPT (exp)
2787 && target
2788 && MEM_P (target)
2789 /* If rettype is addressable, we may not create a temporary.
2790 If target is properly aligned at runtime and the compiler
2791 just doesn't know about it, it will work fine, otherwise it
2792 will be UB. */
2793 && (TREE_ADDRESSABLE (rettype)
2794 || !(MEM_ALIGN (target) < TYPE_ALIGN (rettype)
2795 && targetm.slow_unaligned_access (TYPE_MODE (rettype),
2796 MEM_ALIGN (target)))))
2797 structure_value_addr = XEXP (target, 0);
2798 else
2800 /* For variable-sized objects, we must be called with a target
2801 specified. If we were to allocate space on the stack here,
2802 we would have no way of knowing when to free it. */
2803 rtx d = assign_temp (rettype, 1, 1);
2804 structure_value_addr = XEXP (d, 0);
2805 target = 0;
2808 #endif /* not PCC_STATIC_STRUCT_RETURN */
2811 /* Figure out the amount to which the stack should be aligned. */
2812 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
2813 if (fndecl)
2815 struct cgraph_rtl_info *i = cgraph_node::rtl_info (fndecl);
2816 /* Without automatic stack alignment, we can't increase preferred
2817 stack boundary. With automatic stack alignment, it is
2818 unnecessary since unless we can guarantee that all callers will
2819 align the outgoing stack properly, callee has to align its
2820 stack anyway. */
2821 if (i
2822 && i->preferred_incoming_stack_boundary
2823 && i->preferred_incoming_stack_boundary < preferred_stack_boundary)
2824 preferred_stack_boundary = i->preferred_incoming_stack_boundary;
2827 /* Operand 0 is a pointer-to-function; get the type of the function. */
2828 funtype = TREE_TYPE (addr);
2829 gcc_assert (POINTER_TYPE_P (funtype));
2830 funtype = TREE_TYPE (funtype);
2832 /* Count whether there are actual complex arguments that need to be split
2833 into their real and imaginary parts. Munge the type_arg_types
2834 appropriately here as well. */
2835 if (targetm.calls.split_complex_arg)
2837 call_expr_arg_iterator iter;
2838 tree arg;
2839 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2841 tree type = TREE_TYPE (arg);
2842 if (type && TREE_CODE (type) == COMPLEX_TYPE
2843 && targetm.calls.split_complex_arg (type))
2844 num_complex_actuals++;
2846 type_arg_types = split_complex_types (TYPE_ARG_TYPES (funtype));
2848 else
2849 type_arg_types = TYPE_ARG_TYPES (funtype);
2851 if (flags & ECF_MAY_BE_ALLOCA)
2852 cfun->calls_alloca = 1;
2854 /* If struct_value_rtx is 0, it means pass the address
2855 as if it were an extra parameter. Put the argument expression
2856 in structure_value_addr_value. */
2857 if (structure_value_addr && struct_value == 0)
2859 /* If structure_value_addr is a REG other than
2860 virtual_outgoing_args_rtx, we can use always use it. If it
2861 is not a REG, we must always copy it into a register.
2862 If it is virtual_outgoing_args_rtx, we must copy it to another
2863 register in some cases. */
2864 rtx temp = (!REG_P (structure_value_addr)
2865 || (ACCUMULATE_OUTGOING_ARGS
2866 && stack_arg_under_construction
2867 && structure_value_addr == virtual_outgoing_args_rtx)
2868 ? copy_addr_to_reg (convert_memory_address
2869 (Pmode, structure_value_addr))
2870 : structure_value_addr);
2872 structure_value_addr_value =
2873 make_tree (build_pointer_type (TREE_TYPE (funtype)), temp);
2874 structure_value_addr_parm = 1;
2877 /* Count the arguments and set NUM_ACTUALS. */
2878 num_actuals =
2879 call_expr_nargs (exp) + num_complex_actuals + structure_value_addr_parm;
2881 /* Compute number of named args.
2882 First, do a raw count of the args for INIT_CUMULATIVE_ARGS. */
2884 if (type_arg_types != 0)
2885 n_named_args
2886 = (list_length (type_arg_types)
2887 /* Count the struct value address, if it is passed as a parm. */
2888 + structure_value_addr_parm);
2889 else
2890 /* If we know nothing, treat all args as named. */
2891 n_named_args = num_actuals;
2893 /* Start updating where the next arg would go.
2895 On some machines (such as the PA) indirect calls have a different
2896 calling convention than normal calls. The fourth argument in
2897 INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
2898 or not. */
2899 INIT_CUMULATIVE_ARGS (args_so_far_v, funtype, NULL_RTX, fndecl, n_named_args);
2900 args_so_far = pack_cumulative_args (&args_so_far_v);
2902 /* Now possibly adjust the number of named args.
2903 Normally, don't include the last named arg if anonymous args follow.
2904 We do include the last named arg if
2905 targetm.calls.strict_argument_naming() returns nonzero.
2906 (If no anonymous args follow, the result of list_length is actually
2907 one too large. This is harmless.)
2909 If targetm.calls.pretend_outgoing_varargs_named() returns
2910 nonzero, and targetm.calls.strict_argument_naming() returns zero,
2911 this machine will be able to place unnamed args that were passed
2912 in registers into the stack. So treat all args as named. This
2913 allows the insns emitting for a specific argument list to be
2914 independent of the function declaration.
2916 If targetm.calls.pretend_outgoing_varargs_named() returns zero,
2917 we do not have any reliable way to pass unnamed args in
2918 registers, so we must force them into memory. */
2920 if (type_arg_types != 0
2921 && targetm.calls.strict_argument_naming (args_so_far))
2923 else if (type_arg_types != 0
2924 && ! targetm.calls.pretend_outgoing_varargs_named (args_so_far))
2925 /* Don't include the last named arg. */
2926 --n_named_args;
2927 else
2928 /* Treat all args as named. */
2929 n_named_args = num_actuals;
2931 /* Make a vector to hold all the information about each arg. */
2932 args = XCNEWVEC (struct arg_data, num_actuals);
2934 /* Build up entries in the ARGS array, compute the size of the
2935 arguments into ARGS_SIZE, etc. */
2936 initialize_argument_information (num_actuals, args, &args_size,
2937 n_named_args, exp,
2938 structure_value_addr_value, fndecl, fntype,
2939 args_so_far, reg_parm_stack_space,
2940 &old_stack_level, &old_pending_adj,
2941 &must_preallocate, &flags,
2942 &try_tail_call, CALL_FROM_THUNK_P (exp));
2944 if (args_size.var)
2945 must_preallocate = 1;
2947 /* Now make final decision about preallocating stack space. */
2948 must_preallocate = finalize_must_preallocate (must_preallocate,
2949 num_actuals, args,
2950 &args_size);
2952 /* If the structure value address will reference the stack pointer, we
2953 must stabilize it. We don't need to do this if we know that we are
2954 not going to adjust the stack pointer in processing this call. */
2956 if (structure_value_addr
2957 && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
2958 || reg_mentioned_p (virtual_outgoing_args_rtx,
2959 structure_value_addr))
2960 && (args_size.var
2961 || (!ACCUMULATE_OUTGOING_ARGS
2962 && maybe_ne (args_size.constant, 0))))
2963 structure_value_addr = copy_to_reg (structure_value_addr);
2965 /* Tail calls can make things harder to debug, and we've traditionally
2966 pushed these optimizations into -O2. Don't try if we're already
2967 expanding a call, as that means we're an argument. Don't try if
2968 there's cleanups, as we know there's code to follow the call. */
2969 if (currently_expanding_call++ != 0
2970 || (!flag_optimize_sibling_calls && !CALL_FROM_THUNK_P (exp))
2971 || args_size.var
2972 || dbg_cnt (tail_call) == false)
2973 try_tail_call = 0;
2975 /* Workaround buggy C/C++ wrappers around Fortran routines with
2976 character(len=constant) arguments if the hidden string length arguments
2977 are passed on the stack; if the callers forget to pass those arguments,
2978 attempting to tail call in such routines leads to stack corruption.
2979 Avoid tail calls in functions where at least one such hidden string
2980 length argument is passed (partially or fully) on the stack in the
2981 caller and the callee needs to pass any arguments on the stack.
2982 See PR90329. */
2983 if (try_tail_call && maybe_ne (args_size.constant, 0))
2984 for (tree arg = DECL_ARGUMENTS (current_function_decl);
2985 arg; arg = DECL_CHAIN (arg))
2986 if (DECL_HIDDEN_STRING_LENGTH (arg) && DECL_INCOMING_RTL (arg))
2988 subrtx_iterator::array_type array;
2989 FOR_EACH_SUBRTX (iter, array, DECL_INCOMING_RTL (arg), NONCONST)
2990 if (MEM_P (*iter))
2992 try_tail_call = 0;
2993 break;
2997 /* If the user has marked the function as requiring tail-call
2998 optimization, attempt it. */
2999 if (must_tail_call)
3000 try_tail_call = 1;
3002 /* Rest of purposes for tail call optimizations to fail. */
3003 if (try_tail_call)
3004 try_tail_call = can_implement_as_sibling_call_p (exp,
3005 structure_value_addr,
3006 funtype,
3007 fndecl,
3008 flags, addr, args_size);
3010 /* Check if caller and callee disagree in promotion of function
3011 return value. */
3012 if (try_tail_call)
3014 machine_mode caller_mode, caller_promoted_mode;
3015 machine_mode callee_mode, callee_promoted_mode;
3016 int caller_unsignedp, callee_unsignedp;
3017 tree caller_res = DECL_RESULT (current_function_decl);
3019 caller_unsignedp = TYPE_UNSIGNED (TREE_TYPE (caller_res));
3020 caller_mode = DECL_MODE (caller_res);
3021 callee_unsignedp = TYPE_UNSIGNED (TREE_TYPE (funtype));
3022 callee_mode = TYPE_MODE (TREE_TYPE (funtype));
3023 caller_promoted_mode
3024 = promote_function_mode (TREE_TYPE (caller_res), caller_mode,
3025 &caller_unsignedp,
3026 TREE_TYPE (current_function_decl), 1);
3027 callee_promoted_mode
3028 = promote_function_mode (TREE_TYPE (funtype), callee_mode,
3029 &callee_unsignedp,
3030 funtype, 1);
3031 if (caller_mode != VOIDmode
3032 && (caller_promoted_mode != callee_promoted_mode
3033 || ((caller_mode != caller_promoted_mode
3034 || callee_mode != callee_promoted_mode)
3035 && (caller_unsignedp != callee_unsignedp
3036 || partial_subreg_p (caller_mode, callee_mode)))))
3038 try_tail_call = 0;
3039 maybe_complain_about_tail_call (exp,
3040 "caller and callee disagree in"
3041 " promotion of function"
3042 " return value");
3046 /* Ensure current function's preferred stack boundary is at least
3047 what we need. Stack alignment may also increase preferred stack
3048 boundary. */
3049 for (i = 0; i < num_actuals; i++)
3050 if (reg_parm_stack_space > 0
3051 || args[i].reg == 0
3052 || args[i].partial != 0
3053 || args[i].pass_on_stack)
3054 update_stack_alignment_for_call (&args[i].locate);
3055 if (crtl->preferred_stack_boundary < preferred_stack_boundary)
3056 crtl->preferred_stack_boundary = preferred_stack_boundary;
3057 else
3058 preferred_stack_boundary = crtl->preferred_stack_boundary;
3060 preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
3062 if (flag_callgraph_info)
3063 record_final_call (fndecl, EXPR_LOCATION (exp));
3065 /* We want to make two insn chains; one for a sibling call, the other
3066 for a normal call. We will select one of the two chains after
3067 initial RTL generation is complete. */
3068 for (pass = try_tail_call ? 0 : 1; pass < 2; pass++)
3070 int sibcall_failure = 0;
3071 /* We want to emit any pending stack adjustments before the tail
3072 recursion "call". That way we know any adjustment after the tail
3073 recursion call can be ignored if we indeed use the tail
3074 call expansion. */
3075 saved_pending_stack_adjust save;
3076 rtx_insn *insns, *before_call, *after_args;
3077 rtx next_arg_reg;
3079 if (pass == 0)
3081 /* State variables we need to save and restore between
3082 iterations. */
3083 save_pending_stack_adjust (&save);
3085 if (pass)
3086 flags &= ~ECF_SIBCALL;
3087 else
3088 flags |= ECF_SIBCALL;
3090 /* Other state variables that we must reinitialize each time
3091 through the loop (that are not initialized by the loop itself). */
3092 argblock = 0;
3093 call_fusage = 0;
3095 /* Start a new sequence for the normal call case.
3097 From this point on, if the sibling call fails, we want to set
3098 sibcall_failure instead of continuing the loop. */
3099 start_sequence ();
3101 /* Don't let pending stack adjusts add up to too much.
3102 Also, do all pending adjustments now if there is any chance
3103 this might be a call to alloca or if we are expanding a sibling
3104 call sequence.
3105 Also do the adjustments before a throwing call, otherwise
3106 exception handling can fail; PR 19225. */
3107 if (maybe_ge (pending_stack_adjust, 32)
3108 || (maybe_ne (pending_stack_adjust, 0)
3109 && (flags & ECF_MAY_BE_ALLOCA))
3110 || (maybe_ne (pending_stack_adjust, 0)
3111 && flag_exceptions && !(flags & ECF_NOTHROW))
3112 || pass == 0)
3113 do_pending_stack_adjust ();
3115 /* Precompute any arguments as needed. */
3116 if (pass)
3117 precompute_arguments (num_actuals, args);
3119 /* Now we are about to start emitting insns that can be deleted
3120 if a libcall is deleted. */
3121 if (pass && (flags & ECF_MALLOC))
3122 start_sequence ();
3124 if (pass == 0
3125 && crtl->stack_protect_guard
3126 && targetm.stack_protect_runtime_enabled_p ())
3127 stack_protect_epilogue ();
3129 adjusted_args_size = args_size;
3130 /* Compute the actual size of the argument block required. The variable
3131 and constant sizes must be combined, the size may have to be rounded,
3132 and there may be a minimum required size. When generating a sibcall
3133 pattern, do not round up, since we'll be re-using whatever space our
3134 caller provided. */
3135 unadjusted_args_size
3136 = compute_argument_block_size (reg_parm_stack_space,
3137 &adjusted_args_size,
3138 fndecl, fntype,
3139 (pass == 0 ? 0
3140 : preferred_stack_boundary));
3142 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
3144 /* The argument block when performing a sibling call is the
3145 incoming argument block. */
3146 if (pass == 0)
3148 argblock = crtl->args.internal_arg_pointer;
3149 if (STACK_GROWS_DOWNWARD)
3150 argblock
3151 = plus_constant (Pmode, argblock, crtl->args.pretend_args_size);
3152 else
3153 argblock
3154 = plus_constant (Pmode, argblock, -crtl->args.pretend_args_size);
3156 HOST_WIDE_INT map_size = constant_lower_bound (args_size.constant);
3157 stored_args_map = sbitmap_alloc (map_size);
3158 bitmap_clear (stored_args_map);
3159 stored_args_watermark = HOST_WIDE_INT_M1U;
3162 /* If we have no actual push instructions, or shouldn't use them,
3163 make space for all args right now. */
3164 else if (adjusted_args_size.var != 0)
3166 if (old_stack_level == 0)
3168 emit_stack_save (SAVE_BLOCK, &old_stack_level);
3169 old_stack_pointer_delta = stack_pointer_delta;
3170 old_pending_adj = pending_stack_adjust;
3171 pending_stack_adjust = 0;
3172 /* stack_arg_under_construction says whether a stack arg is
3173 being constructed at the old stack level. Pushing the stack
3174 gets a clean outgoing argument block. */
3175 old_stack_arg_under_construction = stack_arg_under_construction;
3176 stack_arg_under_construction = 0;
3178 argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
3179 if (flag_stack_usage_info)
3180 current_function_has_unbounded_dynamic_stack_size = 1;
3182 else
3184 /* Note that we must go through the motions of allocating an argument
3185 block even if the size is zero because we may be storing args
3186 in the area reserved for register arguments, which may be part of
3187 the stack frame. */
3189 poly_int64 needed = adjusted_args_size.constant;
3191 /* Store the maximum argument space used. It will be pushed by
3192 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
3193 checking). */
3195 crtl->outgoing_args_size = upper_bound (crtl->outgoing_args_size,
3196 needed);
3198 if (must_preallocate)
3200 if (ACCUMULATE_OUTGOING_ARGS)
3202 /* Since the stack pointer will never be pushed, it is
3203 possible for the evaluation of a parm to clobber
3204 something we have already written to the stack.
3205 Since most function calls on RISC machines do not use
3206 the stack, this is uncommon, but must work correctly.
3208 Therefore, we save any area of the stack that was already
3209 written and that we are using. Here we set up to do this
3210 by making a new stack usage map from the old one. The
3211 actual save will be done by store_one_arg.
3213 Another approach might be to try to reorder the argument
3214 evaluations to avoid this conflicting stack usage. */
3216 /* Since we will be writing into the entire argument area,
3217 the map must be allocated for its entire size, not just
3218 the part that is the responsibility of the caller. */
3219 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
3220 needed += reg_parm_stack_space;
3222 poly_int64 limit = needed;
3223 if (ARGS_GROW_DOWNWARD)
3224 limit += 1;
3226 /* For polynomial sizes, this is the maximum possible
3227 size needed for arguments with a constant size
3228 and offset. */
3229 HOST_WIDE_INT const_limit = constant_lower_bound (limit);
3230 highest_outgoing_arg_in_use
3231 = MAX (initial_highest_arg_in_use, const_limit);
3233 free (stack_usage_map_buf);
3234 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
3235 stack_usage_map = stack_usage_map_buf;
3237 if (initial_highest_arg_in_use)
3238 memcpy (stack_usage_map, initial_stack_usage_map,
3239 initial_highest_arg_in_use);
3241 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
3242 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
3243 (highest_outgoing_arg_in_use
3244 - initial_highest_arg_in_use));
3245 needed = 0;
3247 /* The address of the outgoing argument list must not be
3248 copied to a register here, because argblock would be left
3249 pointing to the wrong place after the call to
3250 allocate_dynamic_stack_space below. */
3252 argblock = virtual_outgoing_args_rtx;
3254 else
3256 /* Try to reuse some or all of the pending_stack_adjust
3257 to get this space. */
3258 if (inhibit_defer_pop == 0
3259 && (combine_pending_stack_adjustment_and_call
3260 (&needed,
3261 unadjusted_args_size,
3262 &adjusted_args_size,
3263 preferred_unit_stack_boundary)))
3265 /* combine_pending_stack_adjustment_and_call computes
3266 an adjustment before the arguments are allocated.
3267 Account for them and see whether or not the stack
3268 needs to go up or down. */
3269 needed = unadjusted_args_size - needed;
3271 /* Checked by
3272 combine_pending_stack_adjustment_and_call. */
3273 gcc_checking_assert (ordered_p (needed, 0));
3274 if (maybe_lt (needed, 0))
3276 /* We're releasing stack space. */
3277 /* ??? We can avoid any adjustment at all if we're
3278 already aligned. FIXME. */
3279 pending_stack_adjust = -needed;
3280 do_pending_stack_adjust ();
3281 needed = 0;
3283 else
3284 /* We need to allocate space. We'll do that in
3285 push_block below. */
3286 pending_stack_adjust = 0;
3289 /* Special case this because overhead of `push_block' in
3290 this case is non-trivial. */
3291 if (known_eq (needed, 0))
3292 argblock = virtual_outgoing_args_rtx;
3293 else
3295 rtx needed_rtx = gen_int_mode (needed, Pmode);
3296 argblock = push_block (needed_rtx, 0, 0);
3297 if (ARGS_GROW_DOWNWARD)
3298 argblock = plus_constant (Pmode, argblock, needed);
3301 /* We only really need to call `copy_to_reg' in the case
3302 where push insns are going to be used to pass ARGBLOCK
3303 to a function call in ARGS. In that case, the stack
3304 pointer changes value from the allocation point to the
3305 call point, and hence the value of
3306 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
3307 as well always do it. */
3308 argblock = copy_to_reg (argblock);
3313 if (ACCUMULATE_OUTGOING_ARGS)
3315 /* The save/restore code in store_one_arg handles all
3316 cases except one: a constructor call (including a C
3317 function returning a BLKmode struct) to initialize
3318 an argument. */
3319 if (stack_arg_under_construction)
3321 rtx push_size
3322 = (gen_int_mode
3323 (adjusted_args_size.constant
3324 + (OUTGOING_REG_PARM_STACK_SPACE (!fndecl ? fntype
3325 : TREE_TYPE (fndecl))
3326 ? 0 : reg_parm_stack_space), Pmode));
3327 if (old_stack_level == 0)
3329 emit_stack_save (SAVE_BLOCK, &old_stack_level);
3330 old_stack_pointer_delta = stack_pointer_delta;
3331 old_pending_adj = pending_stack_adjust;
3332 pending_stack_adjust = 0;
3333 /* stack_arg_under_construction says whether a stack
3334 arg is being constructed at the old stack level.
3335 Pushing the stack gets a clean outgoing argument
3336 block. */
3337 old_stack_arg_under_construction
3338 = stack_arg_under_construction;
3339 stack_arg_under_construction = 0;
3340 /* Make a new map for the new argument list. */
3341 free (stack_usage_map_buf);
3342 stack_usage_map_buf = XCNEWVEC (char, highest_outgoing_arg_in_use);
3343 stack_usage_map = stack_usage_map_buf;
3344 highest_outgoing_arg_in_use = 0;
3345 stack_usage_watermark = HOST_WIDE_INT_M1U;
3347 /* We can pass TRUE as the 4th argument because we just
3348 saved the stack pointer and will restore it right after
3349 the call. */
3350 allocate_dynamic_stack_space (push_size, 0, BIGGEST_ALIGNMENT,
3351 -1, true);
3354 /* If argument evaluation might modify the stack pointer,
3355 copy the address of the argument list to a register. */
3356 for (i = 0; i < num_actuals; i++)
3357 if (args[i].pass_on_stack)
3359 argblock = copy_addr_to_reg (argblock);
3360 break;
3364 compute_argument_addresses (args, argblock, num_actuals);
3366 /* Stack is properly aligned, pops can't safely be deferred during
3367 the evaluation of the arguments. */
3368 NO_DEFER_POP;
3370 /* Precompute all register parameters. It isn't safe to compute
3371 anything once we have started filling any specific hard regs.
3372 TLS symbols sometimes need a call to resolve. Precompute
3373 register parameters before any stack pointer manipulation
3374 to avoid unaligned stack in the called function. */
3375 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
3377 OK_DEFER_POP;
3379 /* Perform stack alignment before the first push (the last arg). */
3380 if (argblock == 0
3381 && maybe_gt (adjusted_args_size.constant, reg_parm_stack_space)
3382 && maybe_ne (adjusted_args_size.constant, unadjusted_args_size))
3384 /* When the stack adjustment is pending, we get better code
3385 by combining the adjustments. */
3386 if (maybe_ne (pending_stack_adjust, 0)
3387 && ! inhibit_defer_pop
3388 && (combine_pending_stack_adjustment_and_call
3389 (&pending_stack_adjust,
3390 unadjusted_args_size,
3391 &adjusted_args_size,
3392 preferred_unit_stack_boundary)))
3393 do_pending_stack_adjust ();
3394 else if (argblock == 0)
3395 anti_adjust_stack (gen_int_mode (adjusted_args_size.constant
3396 - unadjusted_args_size,
3397 Pmode));
3399 /* Now that the stack is properly aligned, pops can't safely
3400 be deferred during the evaluation of the arguments. */
3401 NO_DEFER_POP;
3403 /* Record the maximum pushed stack space size. We need to delay
3404 doing it this far to take into account the optimization done
3405 by combine_pending_stack_adjustment_and_call. */
3406 if (flag_stack_usage_info
3407 && !ACCUMULATE_OUTGOING_ARGS
3408 && pass
3409 && adjusted_args_size.var == 0)
3411 poly_int64 pushed = (adjusted_args_size.constant
3412 + pending_stack_adjust);
3413 current_function_pushed_stack_size
3414 = upper_bound (current_function_pushed_stack_size, pushed);
3417 funexp = rtx_for_function_call (fndecl, addr);
3419 if (CALL_EXPR_STATIC_CHAIN (exp))
3420 static_chain_value = expand_normal (CALL_EXPR_STATIC_CHAIN (exp));
3421 else
3422 static_chain_value = 0;
3424 #ifdef REG_PARM_STACK_SPACE
3425 /* Save the fixed argument area if it's part of the caller's frame and
3426 is clobbered by argument setup for this call. */
3427 if (ACCUMULATE_OUTGOING_ARGS && pass)
3428 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3429 &low_to_save, &high_to_save);
3430 #endif
3432 /* Now store (and compute if necessary) all non-register parms.
3433 These come before register parms, since they can require block-moves,
3434 which could clobber the registers used for register parms.
3435 Parms which have partial registers are not stored here,
3436 but we do preallocate space here if they want that. */
3438 for (i = 0; i < num_actuals; i++)
3440 if (args[i].reg == 0 || args[i].pass_on_stack)
3442 rtx_insn *before_arg = get_last_insn ();
3444 /* We don't allow passing huge (> 2^30 B) arguments
3445 by value. It would cause an overflow later on. */
3446 if (constant_lower_bound (adjusted_args_size.constant)
3447 >= (1 << (HOST_BITS_PER_INT - 2)))
3449 sorry ("passing too large argument on stack");
3450 continue;
3453 if (store_one_arg (&args[i], argblock, flags,
3454 adjusted_args_size.var != 0,
3455 reg_parm_stack_space)
3456 || (pass == 0
3457 && check_sibcall_argument_overlap (before_arg,
3458 &args[i], 1)))
3459 sibcall_failure = 1;
3462 if (args[i].stack)
3463 call_fusage
3464 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[i].tree_value)),
3465 gen_rtx_USE (VOIDmode, args[i].stack),
3466 call_fusage);
3469 /* If we have a parm that is passed in registers but not in memory
3470 and whose alignment does not permit a direct copy into registers,
3471 make a group of pseudos that correspond to each register that we
3472 will later fill. */
3473 if (STRICT_ALIGNMENT)
3474 store_unaligned_arguments_into_pseudos (args, num_actuals);
3476 /* Now store any partially-in-registers parm.
3477 This is the last place a block-move can happen. */
3478 if (reg_parm_seen)
3479 for (i = 0; i < num_actuals; i++)
3480 if (args[i].partial != 0 && ! args[i].pass_on_stack)
3482 rtx_insn *before_arg = get_last_insn ();
3484 /* On targets with weird calling conventions (e.g. PA) it's
3485 hard to ensure that all cases of argument overlap between
3486 stack and registers work. Play it safe and bail out. */
3487 if (ARGS_GROW_DOWNWARD && !STACK_GROWS_DOWNWARD)
3489 sibcall_failure = 1;
3490 break;
3493 if (store_one_arg (&args[i], argblock, flags,
3494 adjusted_args_size.var != 0,
3495 reg_parm_stack_space)
3496 || (pass == 0
3497 && check_sibcall_argument_overlap (before_arg,
3498 &args[i], 1)))
3499 sibcall_failure = 1;
3502 bool any_regs = false;
3503 for (i = 0; i < num_actuals; i++)
3504 if (args[i].reg != NULL_RTX)
3506 any_regs = true;
3507 targetm.calls.call_args (args[i].reg, funtype);
3509 if (!any_regs)
3510 targetm.calls.call_args (pc_rtx, funtype);
3512 /* Figure out the register where the value, if any, will come back. */
3513 valreg = 0;
3514 if (TYPE_MODE (rettype) != VOIDmode
3515 && ! structure_value_addr)
3517 if (pcc_struct_value)
3518 valreg = hard_function_value (build_pointer_type (rettype),
3519 fndecl, NULL, (pass == 0));
3520 else
3521 valreg = hard_function_value (rettype, fndecl, fntype,
3522 (pass == 0));
3524 /* If VALREG is a PARALLEL whose first member has a zero
3525 offset, use that. This is for targets such as m68k that
3526 return the same value in multiple places. */
3527 if (GET_CODE (valreg) == PARALLEL)
3529 rtx elem = XVECEXP (valreg, 0, 0);
3530 rtx where = XEXP (elem, 0);
3531 rtx offset = XEXP (elem, 1);
3532 if (offset == const0_rtx
3533 && GET_MODE (where) == GET_MODE (valreg))
3534 valreg = where;
3538 /* If register arguments require space on the stack and stack space
3539 was not preallocated, allocate stack space here for arguments
3540 passed in registers. */
3541 if (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
3542 && !ACCUMULATE_OUTGOING_ARGS
3543 && must_preallocate == 0 && reg_parm_stack_space > 0)
3544 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
3546 /* Pass the function the address in which to return a
3547 structure value. */
3548 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
3550 structure_value_addr
3551 = convert_memory_address (Pmode, structure_value_addr);
3552 emit_move_insn (struct_value,
3553 force_reg (Pmode,
3554 force_operand (structure_value_addr,
3555 NULL_RTX)));
3557 if (REG_P (struct_value))
3558 use_reg (&call_fusage, struct_value);
3561 after_args = get_last_insn ();
3562 funexp = prepare_call_address (fndecl ? fndecl : fntype, funexp,
3563 static_chain_value, &call_fusage,
3564 reg_parm_seen, flags);
3566 load_register_parameters (args, num_actuals, &call_fusage, flags,
3567 pass == 0, &sibcall_failure);
3569 /* Save a pointer to the last insn before the call, so that we can
3570 later safely search backwards to find the CALL_INSN. */
3571 before_call = get_last_insn ();
3573 /* Set up next argument register. For sibling calls on machines
3574 with register windows this should be the incoming register. */
3575 if (pass == 0)
3576 next_arg_reg = targetm.calls.function_incoming_arg
3577 (args_so_far, function_arg_info::end_marker ());
3578 else
3579 next_arg_reg = targetm.calls.function_arg
3580 (args_so_far, function_arg_info::end_marker ());
3582 if (pass == 1 && (return_flags & ERF_RETURNS_ARG))
3584 int arg_nr = return_flags & ERF_RETURN_ARG_MASK;
3585 arg_nr = num_actuals - arg_nr - 1;
3586 if (arg_nr >= 0
3587 && arg_nr < num_actuals
3588 && args[arg_nr].reg
3589 && valreg
3590 && REG_P (valreg)
3591 && GET_MODE (args[arg_nr].reg) == GET_MODE (valreg))
3592 call_fusage
3593 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[arg_nr].tree_value)),
3594 gen_rtx_SET (valreg, args[arg_nr].reg),
3595 call_fusage);
3597 /* All arguments and registers used for the call must be set up by
3598 now! */
3600 /* Stack must be properly aligned now. */
3601 gcc_assert (!pass
3602 || multiple_p (stack_pointer_delta,
3603 preferred_unit_stack_boundary));
3605 /* Generate the actual call instruction. */
3606 emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
3607 adjusted_args_size.constant, struct_value_size,
3608 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
3609 flags, args_so_far);
3611 if (flag_ipa_ra)
3613 rtx_call_insn *last;
3614 rtx datum = NULL_RTX;
3615 if (fndecl != NULL_TREE)
3617 datum = XEXP (DECL_RTL (fndecl), 0);
3618 gcc_assert (datum != NULL_RTX
3619 && GET_CODE (datum) == SYMBOL_REF);
3621 last = last_call_insn ();
3622 add_reg_note (last, REG_CALL_DECL, datum);
3625 /* If the call setup or the call itself overlaps with anything
3626 of the argument setup we probably clobbered our call address.
3627 In that case we can't do sibcalls. */
3628 if (pass == 0
3629 && check_sibcall_argument_overlap (after_args, 0, 0))
3630 sibcall_failure = 1;
3632 /* If a non-BLKmode value is returned at the most significant end
3633 of a register, shift the register right by the appropriate amount
3634 and update VALREG accordingly. BLKmode values are handled by the
3635 group load/store machinery below. */
3636 if (!structure_value_addr
3637 && !pcc_struct_value
3638 && TYPE_MODE (rettype) != VOIDmode
3639 && TYPE_MODE (rettype) != BLKmode
3640 && REG_P (valreg)
3641 && targetm.calls.return_in_msb (rettype))
3643 if (shift_return_value (TYPE_MODE (rettype), false, valreg))
3644 sibcall_failure = 1;
3645 valreg = gen_rtx_REG (TYPE_MODE (rettype), REGNO (valreg));
3648 if (pass && (flags & ECF_MALLOC))
3650 rtx temp = gen_reg_rtx (GET_MODE (valreg));
3651 rtx_insn *last, *insns;
3653 /* The return value from a malloc-like function is a pointer. */
3654 if (TREE_CODE (rettype) == POINTER_TYPE)
3655 mark_reg_pointer (temp, MALLOC_ABI_ALIGNMENT);
3657 emit_move_insn (temp, valreg);
3659 /* The return value from a malloc-like function cannot alias
3660 anything else. */
3661 last = get_last_insn ();
3662 add_reg_note (last, REG_NOALIAS, temp);
3664 /* Write out the sequence. */
3665 insns = get_insns ();
3666 end_sequence ();
3667 emit_insn (insns);
3668 valreg = temp;
3671 /* For calls to `setjmp', etc., inform
3672 function.c:setjmp_warnings that it should complain if
3673 nonvolatile values are live. For functions that cannot
3674 return, inform flow that control does not fall through. */
3676 if ((flags & ECF_NORETURN) || pass == 0)
3678 /* The barrier must be emitted
3679 immediately after the CALL_INSN. Some ports emit more
3680 than just a CALL_INSN above, so we must search for it here. */
3682 rtx_insn *last = get_last_insn ();
3683 while (!CALL_P (last))
3685 last = PREV_INSN (last);
3686 /* There was no CALL_INSN? */
3687 gcc_assert (last != before_call);
3690 emit_barrier_after (last);
3692 /* Stack adjustments after a noreturn call are dead code.
3693 However when NO_DEFER_POP is in effect, we must preserve
3694 stack_pointer_delta. */
3695 if (inhibit_defer_pop == 0)
3697 stack_pointer_delta = old_stack_allocated;
3698 pending_stack_adjust = 0;
3702 /* If value type not void, return an rtx for the value. */
3704 if (TYPE_MODE (rettype) == VOIDmode
3705 || ignore)
3706 target = const0_rtx;
3707 else if (structure_value_addr)
3709 if (target == 0 || !MEM_P (target))
3711 target
3712 = gen_rtx_MEM (TYPE_MODE (rettype),
3713 memory_address (TYPE_MODE (rettype),
3714 structure_value_addr));
3715 set_mem_attributes (target, rettype, 1);
3718 else if (pcc_struct_value)
3720 /* This is the special C++ case where we need to
3721 know what the true target was. We take care to
3722 never use this value more than once in one expression. */
3723 target = gen_rtx_MEM (TYPE_MODE (rettype),
3724 copy_to_reg (valreg));
3725 set_mem_attributes (target, rettype, 1);
3727 /* Handle calls that return values in multiple non-contiguous locations.
3728 The Irix 6 ABI has examples of this. */
3729 else if (GET_CODE (valreg) == PARALLEL)
3731 if (target == 0)
3732 target = emit_group_move_into_temps (valreg);
3733 else if (rtx_equal_p (target, valreg))
3735 else if (GET_CODE (target) == PARALLEL)
3736 /* Handle the result of a emit_group_move_into_temps
3737 call in the previous pass. */
3738 emit_group_move (target, valreg);
3739 else
3740 emit_group_store (target, valreg, rettype,
3741 int_size_in_bytes (rettype));
3743 else if (target
3744 && GET_MODE (target) == TYPE_MODE (rettype)
3745 && GET_MODE (target) == GET_MODE (valreg))
3747 bool may_overlap = false;
3749 /* We have to copy a return value in a CLASS_LIKELY_SPILLED hard
3750 reg to a plain register. */
3751 if (!REG_P (target) || HARD_REGISTER_P (target))
3752 valreg = avoid_likely_spilled_reg (valreg);
3754 /* If TARGET is a MEM in the argument area, and we have
3755 saved part of the argument area, then we can't store
3756 directly into TARGET as it may get overwritten when we
3757 restore the argument save area below. Don't work too
3758 hard though and simply force TARGET to a register if it
3759 is a MEM; the optimizer is quite likely to sort it out. */
3760 if (ACCUMULATE_OUTGOING_ARGS && pass && MEM_P (target))
3761 for (i = 0; i < num_actuals; i++)
3762 if (args[i].save_area)
3764 may_overlap = true;
3765 break;
3768 if (may_overlap)
3769 target = copy_to_reg (valreg);
3770 else
3772 /* TARGET and VALREG cannot be equal at this point
3773 because the latter would not have
3774 REG_FUNCTION_VALUE_P true, while the former would if
3775 it were referring to the same register.
3777 If they refer to the same register, this move will be
3778 a no-op, except when function inlining is being
3779 done. */
3780 emit_move_insn (target, valreg);
3782 /* If we are setting a MEM, this code must be executed.
3783 Since it is emitted after the call insn, sibcall
3784 optimization cannot be performed in that case. */
3785 if (MEM_P (target))
3786 sibcall_failure = 1;
3789 else
3790 target = copy_to_reg (avoid_likely_spilled_reg (valreg));
3792 /* If we promoted this return value, make the proper SUBREG.
3793 TARGET might be const0_rtx here, so be careful. */
3794 if (REG_P (target)
3795 && TYPE_MODE (rettype) != BLKmode
3796 && GET_MODE (target) != TYPE_MODE (rettype))
3798 tree type = rettype;
3799 int unsignedp = TYPE_UNSIGNED (type);
3800 machine_mode pmode;
3802 /* Ensure we promote as expected, and get the new unsignedness. */
3803 pmode = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
3804 funtype, 1);
3805 gcc_assert (GET_MODE (target) == pmode);
3807 poly_uint64 offset = subreg_lowpart_offset (TYPE_MODE (type),
3808 GET_MODE (target));
3809 target = gen_rtx_SUBREG (TYPE_MODE (type), target, offset);
3810 SUBREG_PROMOTED_VAR_P (target) = 1;
3811 SUBREG_PROMOTED_SET (target, unsignedp);
3814 /* If size of args is variable or this was a constructor call for a stack
3815 argument, restore saved stack-pointer value. */
3817 if (old_stack_level)
3819 rtx_insn *prev = get_last_insn ();
3821 emit_stack_restore (SAVE_BLOCK, old_stack_level);
3822 stack_pointer_delta = old_stack_pointer_delta;
3824 fixup_args_size_notes (prev, get_last_insn (), stack_pointer_delta);
3826 pending_stack_adjust = old_pending_adj;
3827 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
3828 stack_arg_under_construction = old_stack_arg_under_construction;
3829 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3830 stack_usage_map = initial_stack_usage_map;
3831 stack_usage_watermark = initial_stack_usage_watermark;
3832 sibcall_failure = 1;
3834 else if (ACCUMULATE_OUTGOING_ARGS && pass)
3836 #ifdef REG_PARM_STACK_SPACE
3837 if (save_area)
3838 restore_fixed_argument_area (save_area, argblock,
3839 high_to_save, low_to_save);
3840 #endif
3842 /* If we saved any argument areas, restore them. */
3843 for (i = 0; i < num_actuals; i++)
3844 if (args[i].save_area)
3846 machine_mode save_mode = GET_MODE (args[i].save_area);
3847 rtx stack_area
3848 = gen_rtx_MEM (save_mode,
3849 memory_address (save_mode,
3850 XEXP (args[i].stack_slot, 0)));
3852 if (save_mode != BLKmode)
3853 emit_move_insn (stack_area, args[i].save_area);
3854 else
3855 emit_block_move (stack_area, args[i].save_area,
3856 (gen_int_mode
3857 (args[i].locate.size.constant, Pmode)),
3858 BLOCK_OP_CALL_PARM);
3861 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3862 stack_usage_map = initial_stack_usage_map;
3863 stack_usage_watermark = initial_stack_usage_watermark;
3866 /* If this was alloca, record the new stack level. */
3867 if (flags & ECF_MAY_BE_ALLOCA)
3868 record_new_stack_level ();
3870 /* Free up storage we no longer need. */
3871 for (i = 0; i < num_actuals; ++i)
3872 free (args[i].aligned_regs);
3874 targetm.calls.end_call_args ();
3876 insns = get_insns ();
3877 end_sequence ();
3879 if (pass == 0)
3881 tail_call_insns = insns;
3883 /* Restore the pending stack adjustment now that we have
3884 finished generating the sibling call sequence. */
3886 restore_pending_stack_adjust (&save);
3888 /* Prepare arg structure for next iteration. */
3889 for (i = 0; i < num_actuals; i++)
3891 args[i].value = 0;
3892 args[i].aligned_regs = 0;
3893 args[i].stack = 0;
3896 sbitmap_free (stored_args_map);
3897 internal_arg_pointer_exp_state.scan_start = NULL;
3898 internal_arg_pointer_exp_state.cache.release ();
3900 else
3902 normal_call_insns = insns;
3904 /* Verify that we've deallocated all the stack we used. */
3905 gcc_assert ((flags & ECF_NORETURN)
3906 || known_eq (old_stack_allocated,
3907 stack_pointer_delta
3908 - pending_stack_adjust));
3911 /* If something prevents making this a sibling call,
3912 zero out the sequence. */
3913 if (sibcall_failure)
3914 tail_call_insns = NULL;
3915 else
3916 break;
3919 /* If tail call production succeeded, we need to remove REG_EQUIV notes on
3920 arguments too, as argument area is now clobbered by the call. */
3921 if (tail_call_insns)
3923 emit_insn (tail_call_insns);
3924 crtl->tail_call_emit = true;
3926 else
3928 emit_insn (normal_call_insns);
3929 if (try_tail_call)
3930 /* Ideally we'd emit a message for all of the ways that it could
3931 have failed. */
3932 maybe_complain_about_tail_call (exp, "tail call production failed");
3935 currently_expanding_call--;
3937 free (stack_usage_map_buf);
3938 free (args);
3939 return target;
3942 /* A sibling call sequence invalidates any REG_EQUIV notes made for
3943 this function's incoming arguments.
3945 At the start of RTL generation we know the only REG_EQUIV notes
3946 in the rtl chain are those for incoming arguments, so we can look
3947 for REG_EQUIV notes between the start of the function and the
3948 NOTE_INSN_FUNCTION_BEG.
3950 This is (slight) overkill. We could keep track of the highest
3951 argument we clobber and be more selective in removing notes, but it
3952 does not seem to be worth the effort. */
3954 void
3955 fixup_tail_calls (void)
3957 rtx_insn *insn;
3959 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3961 rtx note;
3963 /* There are never REG_EQUIV notes for the incoming arguments
3964 after the NOTE_INSN_FUNCTION_BEG note, so stop if we see it. */
3965 if (NOTE_P (insn)
3966 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3967 break;
3969 note = find_reg_note (insn, REG_EQUIV, 0);
3970 if (note)
3971 remove_note (insn, note);
3972 note = find_reg_note (insn, REG_EQUIV, 0);
3973 gcc_assert (!note);
3977 /* Traverse a list of TYPES and expand all complex types into their
3978 components. */
3979 static tree
3980 split_complex_types (tree types)
3982 tree p;
3984 /* Before allocating memory, check for the common case of no complex. */
3985 for (p = types; p; p = TREE_CHAIN (p))
3987 tree type = TREE_VALUE (p);
3988 if (TREE_CODE (type) == COMPLEX_TYPE
3989 && targetm.calls.split_complex_arg (type))
3990 goto found;
3992 return types;
3994 found:
3995 types = copy_list (types);
3997 for (p = types; p; p = TREE_CHAIN (p))
3999 tree complex_type = TREE_VALUE (p);
4001 if (TREE_CODE (complex_type) == COMPLEX_TYPE
4002 && targetm.calls.split_complex_arg (complex_type))
4004 tree next, imag;
4006 /* Rewrite complex type with component type. */
4007 TREE_VALUE (p) = TREE_TYPE (complex_type);
4008 next = TREE_CHAIN (p);
4010 /* Add another component type for the imaginary part. */
4011 imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
4012 TREE_CHAIN (p) = imag;
4013 TREE_CHAIN (imag) = next;
4015 /* Skip the newly created node. */
4016 p = TREE_CHAIN (p);
4020 return types;
4023 /* Output a library call to function ORGFUN (a SYMBOL_REF rtx)
4024 for a value of mode OUTMODE,
4025 with NARGS different arguments, passed as ARGS.
4026 Store the return value if RETVAL is nonzero: store it in VALUE if
4027 VALUE is nonnull, otherwise pick a convenient location. In either
4028 case return the location of the stored value.
4030 FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for
4031 `const' calls, LCT_PURE for `pure' calls, or another LCT_ value for
4032 other types of library calls. */
4035 emit_library_call_value_1 (int retval, rtx orgfun, rtx value,
4036 enum libcall_type fn_type,
4037 machine_mode outmode, int nargs, rtx_mode_t *args)
4039 /* Total size in bytes of all the stack-parms scanned so far. */
4040 struct args_size args_size;
4041 /* Size of arguments before any adjustments (such as rounding). */
4042 struct args_size original_args_size;
4043 int argnum;
4044 rtx fun;
4045 /* Todo, choose the correct decl type of orgfun. Sadly this information
4046 isn't present here, so we default to native calling abi here. */
4047 tree fndecl ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
4048 tree fntype ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
4049 int count;
4050 rtx argblock = 0;
4051 CUMULATIVE_ARGS args_so_far_v;
4052 cumulative_args_t args_so_far;
4053 struct arg
4055 rtx value;
4056 machine_mode mode;
4057 rtx reg;
4058 int partial;
4059 struct locate_and_pad_arg_data locate;
4060 rtx save_area;
4062 struct arg *argvec;
4063 int old_inhibit_defer_pop = inhibit_defer_pop;
4064 rtx call_fusage = 0;
4065 rtx mem_value = 0;
4066 rtx valreg;
4067 int pcc_struct_value = 0;
4068 poly_int64 struct_value_size = 0;
4069 int flags;
4070 int reg_parm_stack_space = 0;
4071 poly_int64 needed;
4072 rtx_insn *before_call;
4073 bool have_push_fusage;
4074 tree tfom; /* type_for_mode (outmode, 0) */
4076 #ifdef REG_PARM_STACK_SPACE
4077 /* Define the boundary of the register parm stack space that needs to be
4078 save, if any. */
4079 int low_to_save = 0, high_to_save = 0;
4080 rtx save_area = 0; /* Place that it is saved. */
4081 #endif
4083 /* Size of the stack reserved for parameter registers. */
4084 unsigned int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
4085 char *initial_stack_usage_map = stack_usage_map;
4086 unsigned HOST_WIDE_INT initial_stack_usage_watermark = stack_usage_watermark;
4087 char *stack_usage_map_buf = NULL;
4089 rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
4091 #ifdef REG_PARM_STACK_SPACE
4092 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
4093 #endif
4095 /* By default, library functions cannot throw. */
4096 flags = ECF_NOTHROW;
4098 switch (fn_type)
4100 case LCT_NORMAL:
4101 break;
4102 case LCT_CONST:
4103 flags |= ECF_CONST;
4104 break;
4105 case LCT_PURE:
4106 flags |= ECF_PURE;
4107 break;
4108 case LCT_NORETURN:
4109 flags |= ECF_NORETURN;
4110 break;
4111 case LCT_THROW:
4112 flags &= ~ECF_NOTHROW;
4113 break;
4114 case LCT_RETURNS_TWICE:
4115 flags = ECF_RETURNS_TWICE;
4116 break;
4118 fun = orgfun;
4120 /* Ensure current function's preferred stack boundary is at least
4121 what we need. */
4122 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
4123 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
4125 /* If this kind of value comes back in memory,
4126 decide where in memory it should come back. */
4127 if (outmode != VOIDmode)
4129 tfom = lang_hooks.types.type_for_mode (outmode, 0);
4130 if (aggregate_value_p (tfom, 0))
4132 #ifdef PCC_STATIC_STRUCT_RETURN
4133 rtx pointer_reg
4134 = hard_function_value (build_pointer_type (tfom), 0, 0, 0);
4135 mem_value = gen_rtx_MEM (outmode, pointer_reg);
4136 pcc_struct_value = 1;
4137 if (value == 0)
4138 value = gen_reg_rtx (outmode);
4139 #else /* not PCC_STATIC_STRUCT_RETURN */
4140 struct_value_size = GET_MODE_SIZE (outmode);
4141 if (value != 0 && MEM_P (value))
4142 mem_value = value;
4143 else
4144 mem_value = assign_temp (tfom, 1, 1);
4145 #endif
4146 /* This call returns a big structure. */
4147 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
4150 else
4151 tfom = void_type_node;
4153 /* ??? Unfinished: must pass the memory address as an argument. */
4155 /* Copy all the libcall-arguments out of the varargs data
4156 and into a vector ARGVEC.
4158 Compute how to pass each argument. We only support a very small subset
4159 of the full argument passing conventions to limit complexity here since
4160 library functions shouldn't have many args. */
4162 argvec = XALLOCAVEC (struct arg, nargs + 1);
4163 memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
4165 #ifdef INIT_CUMULATIVE_LIBCALL_ARGS
4166 INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far_v, outmode, fun);
4167 #else
4168 INIT_CUMULATIVE_ARGS (args_so_far_v, NULL_TREE, fun, 0, nargs);
4169 #endif
4170 args_so_far = pack_cumulative_args (&args_so_far_v);
4172 args_size.constant = 0;
4173 args_size.var = 0;
4175 count = 0;
4177 push_temp_slots ();
4179 /* If there's a structure value address to be passed,
4180 either pass it in the special place, or pass it as an extra argument. */
4181 if (mem_value && struct_value == 0 && ! pcc_struct_value)
4183 rtx addr = XEXP (mem_value, 0);
4185 nargs++;
4187 /* Make sure it is a reasonable operand for a move or push insn. */
4188 if (!REG_P (addr) && !MEM_P (addr)
4189 && !(CONSTANT_P (addr)
4190 && targetm.legitimate_constant_p (Pmode, addr)))
4191 addr = force_operand (addr, NULL_RTX);
4193 argvec[count].value = addr;
4194 argvec[count].mode = Pmode;
4195 argvec[count].partial = 0;
4197 function_arg_info ptr_arg (Pmode, /*named=*/true);
4198 argvec[count].reg = targetm.calls.function_arg (args_so_far, ptr_arg);
4199 gcc_assert (targetm.calls.arg_partial_bytes (args_so_far, ptr_arg) == 0);
4201 locate_and_pad_parm (Pmode, NULL_TREE,
4202 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4204 #else
4205 argvec[count].reg != 0,
4206 #endif
4207 reg_parm_stack_space, 0,
4208 NULL_TREE, &args_size, &argvec[count].locate);
4210 if (argvec[count].reg == 0 || argvec[count].partial != 0
4211 || reg_parm_stack_space > 0)
4212 args_size.constant += argvec[count].locate.size.constant;
4214 targetm.calls.function_arg_advance (args_so_far, ptr_arg);
4216 count++;
4219 for (unsigned int i = 0; count < nargs; i++, count++)
4221 rtx val = args[i].first;
4222 function_arg_info arg (args[i].second, /*named=*/true);
4223 int unsigned_p = 0;
4225 /* We cannot convert the arg value to the mode the library wants here;
4226 must do it earlier where we know the signedness of the arg. */
4227 gcc_assert (arg.mode != BLKmode
4228 && (GET_MODE (val) == arg.mode
4229 || GET_MODE (val) == VOIDmode));
4231 /* Make sure it is a reasonable operand for a move or push insn. */
4232 if (!REG_P (val) && !MEM_P (val)
4233 && !(CONSTANT_P (val)
4234 && targetm.legitimate_constant_p (arg.mode, val)))
4235 val = force_operand (val, NULL_RTX);
4237 if (pass_by_reference (&args_so_far_v, arg))
4239 rtx slot;
4240 int must_copy = !reference_callee_copied (&args_so_far_v, arg);
4242 /* If this was a CONST function, it is now PURE since it now
4243 reads memory. */
4244 if (flags & ECF_CONST)
4246 flags &= ~ECF_CONST;
4247 flags |= ECF_PURE;
4250 if (MEM_P (val) && !must_copy)
4252 tree val_expr = MEM_EXPR (val);
4253 if (val_expr)
4254 mark_addressable (val_expr);
4255 slot = val;
4257 else
4259 slot = assign_temp (lang_hooks.types.type_for_mode (arg.mode, 0),
4260 1, 1);
4261 emit_move_insn (slot, val);
4264 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4265 gen_rtx_USE (VOIDmode, slot),
4266 call_fusage);
4267 if (must_copy)
4268 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4269 gen_rtx_CLOBBER (VOIDmode,
4270 slot),
4271 call_fusage);
4273 arg.mode = Pmode;
4274 arg.pass_by_reference = true;
4275 val = force_operand (XEXP (slot, 0), NULL_RTX);
4278 arg.mode = promote_function_mode (NULL_TREE, arg.mode, &unsigned_p,
4279 NULL_TREE, 0);
4280 argvec[count].mode = arg.mode;
4281 argvec[count].value = convert_modes (arg.mode, GET_MODE (val), val,
4282 unsigned_p);
4283 argvec[count].reg = targetm.calls.function_arg (args_so_far, arg);
4285 argvec[count].partial
4286 = targetm.calls.arg_partial_bytes (args_so_far, arg);
4288 if (argvec[count].reg == 0
4289 || argvec[count].partial != 0
4290 || reg_parm_stack_space > 0)
4292 locate_and_pad_parm (arg.mode, NULL_TREE,
4293 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4295 #else
4296 argvec[count].reg != 0,
4297 #endif
4298 reg_parm_stack_space, argvec[count].partial,
4299 NULL_TREE, &args_size, &argvec[count].locate);
4300 args_size.constant += argvec[count].locate.size.constant;
4301 gcc_assert (!argvec[count].locate.size.var);
4303 #ifdef BLOCK_REG_PADDING
4304 else
4305 /* The argument is passed entirely in registers. See at which
4306 end it should be padded. */
4307 argvec[count].locate.where_pad =
4308 BLOCK_REG_PADDING (arg.mode, NULL_TREE,
4309 known_le (GET_MODE_SIZE (arg.mode),
4310 UNITS_PER_WORD));
4311 #endif
4313 targetm.calls.function_arg_advance (args_so_far, arg);
4316 for (int i = 0; i < nargs; i++)
4317 if (reg_parm_stack_space > 0
4318 || argvec[i].reg == 0
4319 || argvec[i].partial != 0)
4320 update_stack_alignment_for_call (&argvec[i].locate);
4322 /* If this machine requires an external definition for library
4323 functions, write one out. */
4324 assemble_external_libcall (fun);
4326 original_args_size = args_size;
4327 args_size.constant = (aligned_upper_bound (args_size.constant
4328 + stack_pointer_delta,
4329 STACK_BYTES)
4330 - stack_pointer_delta);
4332 args_size.constant = upper_bound (args_size.constant,
4333 reg_parm_stack_space);
4335 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4336 args_size.constant -= reg_parm_stack_space;
4338 crtl->outgoing_args_size = upper_bound (crtl->outgoing_args_size,
4339 args_size.constant);
4341 if (flag_stack_usage_info && !ACCUMULATE_OUTGOING_ARGS)
4343 poly_int64 pushed = args_size.constant + pending_stack_adjust;
4344 current_function_pushed_stack_size
4345 = upper_bound (current_function_pushed_stack_size, pushed);
4348 if (ACCUMULATE_OUTGOING_ARGS)
4350 /* Since the stack pointer will never be pushed, it is possible for
4351 the evaluation of a parm to clobber something we have already
4352 written to the stack. Since most function calls on RISC machines
4353 do not use the stack, this is uncommon, but must work correctly.
4355 Therefore, we save any area of the stack that was already written
4356 and that we are using. Here we set up to do this by making a new
4357 stack usage map from the old one.
4359 Another approach might be to try to reorder the argument
4360 evaluations to avoid this conflicting stack usage. */
4362 needed = args_size.constant;
4364 /* Since we will be writing into the entire argument area, the
4365 map must be allocated for its entire size, not just the part that
4366 is the responsibility of the caller. */
4367 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4368 needed += reg_parm_stack_space;
4370 poly_int64 limit = needed;
4371 if (ARGS_GROW_DOWNWARD)
4372 limit += 1;
4374 /* For polynomial sizes, this is the maximum possible size needed
4375 for arguments with a constant size and offset. */
4376 HOST_WIDE_INT const_limit = constant_lower_bound (limit);
4377 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
4378 const_limit);
4380 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
4381 stack_usage_map = stack_usage_map_buf;
4383 if (initial_highest_arg_in_use)
4384 memcpy (stack_usage_map, initial_stack_usage_map,
4385 initial_highest_arg_in_use);
4387 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
4388 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
4389 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
4390 needed = 0;
4392 /* We must be careful to use virtual regs before they're instantiated,
4393 and real regs afterwards. Loop optimization, for example, can create
4394 new libcalls after we've instantiated the virtual regs, and if we
4395 use virtuals anyway, they won't match the rtl patterns. */
4397 if (virtuals_instantiated)
4398 argblock = plus_constant (Pmode, stack_pointer_rtx,
4399 STACK_POINTER_OFFSET);
4400 else
4401 argblock = virtual_outgoing_args_rtx;
4403 else
4405 if (!targetm.calls.push_argument (0))
4406 argblock = push_block (gen_int_mode (args_size.constant, Pmode), 0, 0);
4409 /* We push args individually in reverse order, perform stack alignment
4410 before the first push (the last arg). */
4411 if (argblock == 0)
4412 anti_adjust_stack (gen_int_mode (args_size.constant
4413 - original_args_size.constant,
4414 Pmode));
4416 argnum = nargs - 1;
4418 #ifdef REG_PARM_STACK_SPACE
4419 if (ACCUMULATE_OUTGOING_ARGS)
4421 /* The argument list is the property of the called routine and it
4422 may clobber it. If the fixed area has been used for previous
4423 parameters, we must save and restore it. */
4424 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
4425 &low_to_save, &high_to_save);
4427 #endif
4429 /* When expanding a normal call, args are stored in push order,
4430 which is the reverse of what we have here. */
4431 bool any_regs = false;
4432 for (int i = nargs; i-- > 0; )
4433 if (argvec[i].reg != NULL_RTX)
4435 targetm.calls.call_args (argvec[i].reg, NULL_TREE);
4436 any_regs = true;
4438 if (!any_regs)
4439 targetm.calls.call_args (pc_rtx, NULL_TREE);
4441 /* Push the args that need to be pushed. */
4443 have_push_fusage = false;
4445 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4446 are to be pushed. */
4447 for (count = 0; count < nargs; count++, argnum--)
4449 machine_mode mode = argvec[argnum].mode;
4450 rtx val = argvec[argnum].value;
4451 rtx reg = argvec[argnum].reg;
4452 int partial = argvec[argnum].partial;
4453 unsigned int parm_align = argvec[argnum].locate.boundary;
4454 poly_int64 lower_bound = 0, upper_bound = 0;
4456 if (! (reg != 0 && partial == 0))
4458 rtx use;
4460 if (ACCUMULATE_OUTGOING_ARGS)
4462 /* If this is being stored into a pre-allocated, fixed-size,
4463 stack area, save any previous data at that location. */
4465 if (ARGS_GROW_DOWNWARD)
4467 /* stack_slot is negative, but we want to index stack_usage_map
4468 with positive values. */
4469 upper_bound = -argvec[argnum].locate.slot_offset.constant + 1;
4470 lower_bound = upper_bound - argvec[argnum].locate.size.constant;
4472 else
4474 lower_bound = argvec[argnum].locate.slot_offset.constant;
4475 upper_bound = lower_bound + argvec[argnum].locate.size.constant;
4478 if (stack_region_maybe_used_p (lower_bound, upper_bound,
4479 reg_parm_stack_space))
4481 /* We need to make a save area. */
4482 poly_uint64 size
4483 = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
4484 machine_mode save_mode
4485 = int_mode_for_size (size, 1).else_blk ();
4486 rtx adr
4487 = plus_constant (Pmode, argblock,
4488 argvec[argnum].locate.offset.constant);
4489 rtx stack_area
4490 = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
4492 if (save_mode == BLKmode)
4494 argvec[argnum].save_area
4495 = assign_stack_temp (BLKmode,
4496 argvec[argnum].locate.size.constant
4499 emit_block_move (validize_mem
4500 (copy_rtx (argvec[argnum].save_area)),
4501 stack_area,
4502 (gen_int_mode
4503 (argvec[argnum].locate.size.constant,
4504 Pmode)),
4505 BLOCK_OP_CALL_PARM);
4507 else
4509 argvec[argnum].save_area = gen_reg_rtx (save_mode);
4511 emit_move_insn (argvec[argnum].save_area, stack_area);
4516 emit_push_insn (val, mode, NULL_TREE, NULL_RTX, parm_align,
4517 partial, reg, 0, argblock,
4518 (gen_int_mode
4519 (argvec[argnum].locate.offset.constant, Pmode)),
4520 reg_parm_stack_space,
4521 ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad), false);
4523 /* Now mark the segment we just used. */
4524 if (ACCUMULATE_OUTGOING_ARGS)
4525 mark_stack_region_used (lower_bound, upper_bound);
4527 NO_DEFER_POP;
4529 /* Indicate argument access so that alias.c knows that these
4530 values are live. */
4531 if (argblock)
4532 use = plus_constant (Pmode, argblock,
4533 argvec[argnum].locate.offset.constant);
4534 else if (have_push_fusage)
4535 continue;
4536 else
4538 /* When arguments are pushed, trying to tell alias.c where
4539 exactly this argument is won't work, because the
4540 auto-increment causes confusion. So we merely indicate
4541 that we access something with a known mode somewhere on
4542 the stack. */
4543 use = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4544 gen_rtx_SCRATCH (Pmode));
4545 have_push_fusage = true;
4547 use = gen_rtx_MEM (argvec[argnum].mode, use);
4548 use = gen_rtx_USE (VOIDmode, use);
4549 call_fusage = gen_rtx_EXPR_LIST (VOIDmode, use, call_fusage);
4553 argnum = nargs - 1;
4555 fun = prepare_call_address (NULL, fun, NULL, &call_fusage, 0, 0);
4557 /* Now load any reg parms into their regs. */
4559 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4560 are to be pushed. */
4561 for (count = 0; count < nargs; count++, argnum--)
4563 machine_mode mode = argvec[argnum].mode;
4564 rtx val = argvec[argnum].value;
4565 rtx reg = argvec[argnum].reg;
4566 int partial = argvec[argnum].partial;
4568 /* Handle calls that pass values in multiple non-contiguous
4569 locations. The PA64 has examples of this for library calls. */
4570 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4571 emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (mode));
4572 else if (reg != 0 && partial == 0)
4574 emit_move_insn (reg, val);
4575 #ifdef BLOCK_REG_PADDING
4576 poly_int64 size = GET_MODE_SIZE (argvec[argnum].mode);
4578 /* Copied from load_register_parameters. */
4580 /* Handle case where we have a value that needs shifting
4581 up to the msb. eg. a QImode value and we're padding
4582 upward on a BYTES_BIG_ENDIAN machine. */
4583 if (known_lt (size, UNITS_PER_WORD)
4584 && (argvec[argnum].locate.where_pad
4585 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)))
4587 rtx x;
4588 poly_int64 shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
4590 /* Assigning REG here rather than a temp makes CALL_FUSAGE
4591 report the whole reg as used. Strictly speaking, the
4592 call only uses SIZE bytes at the msb end, but it doesn't
4593 seem worth generating rtl to say that. */
4594 reg = gen_rtx_REG (word_mode, REGNO (reg));
4595 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
4596 if (x != reg)
4597 emit_move_insn (reg, x);
4599 #endif
4602 NO_DEFER_POP;
4605 /* Any regs containing parms remain in use through the call. */
4606 for (count = 0; count < nargs; count++)
4608 rtx reg = argvec[count].reg;
4609 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4610 use_group_regs (&call_fusage, reg);
4611 else if (reg != 0)
4613 int partial = argvec[count].partial;
4614 if (partial)
4616 int nregs;
4617 gcc_assert (partial % UNITS_PER_WORD == 0);
4618 nregs = partial / UNITS_PER_WORD;
4619 use_regs (&call_fusage, REGNO (reg), nregs);
4621 else
4622 use_reg (&call_fusage, reg);
4626 /* Pass the function the address in which to return a structure value. */
4627 if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
4629 emit_move_insn (struct_value,
4630 force_reg (Pmode,
4631 force_operand (XEXP (mem_value, 0),
4632 NULL_RTX)));
4633 if (REG_P (struct_value))
4634 use_reg (&call_fusage, struct_value);
4637 /* Don't allow popping to be deferred, since then
4638 cse'ing of library calls could delete a call and leave the pop. */
4639 NO_DEFER_POP;
4640 valreg = (mem_value == 0 && outmode != VOIDmode
4641 ? hard_libcall_value (outmode, orgfun) : NULL_RTX);
4643 /* Stack must be properly aligned now. */
4644 gcc_assert (multiple_p (stack_pointer_delta,
4645 PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT));
4647 before_call = get_last_insn ();
4649 if (flag_callgraph_info)
4650 record_final_call (SYMBOL_REF_DECL (orgfun), UNKNOWN_LOCATION);
4652 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
4653 will set inhibit_defer_pop to that value. */
4654 /* The return type is needed to decide how many bytes the function pops.
4655 Signedness plays no role in that, so for simplicity, we pretend it's
4656 always signed. We also assume that the list of arguments passed has
4657 no impact, so we pretend it is unknown. */
4659 emit_call_1 (fun, NULL,
4660 get_identifier (XSTR (orgfun, 0)),
4661 build_function_type (tfom, NULL_TREE),
4662 original_args_size.constant, args_size.constant,
4663 struct_value_size,
4664 targetm.calls.function_arg (args_so_far,
4665 function_arg_info::end_marker ()),
4666 valreg,
4667 old_inhibit_defer_pop + 1, call_fusage, flags, args_so_far);
4669 if (flag_ipa_ra)
4671 rtx datum = orgfun;
4672 gcc_assert (GET_CODE (datum) == SYMBOL_REF);
4673 rtx_call_insn *last = last_call_insn ();
4674 add_reg_note (last, REG_CALL_DECL, datum);
4677 /* Right-shift returned value if necessary. */
4678 if (!pcc_struct_value
4679 && TYPE_MODE (tfom) != BLKmode
4680 && targetm.calls.return_in_msb (tfom))
4682 shift_return_value (TYPE_MODE (tfom), false, valreg);
4683 valreg = gen_rtx_REG (TYPE_MODE (tfom), REGNO (valreg));
4686 targetm.calls.end_call_args ();
4688 /* For calls to `setjmp', etc., inform function.c:setjmp_warnings
4689 that it should complain if nonvolatile values are live. For
4690 functions that cannot return, inform flow that control does not
4691 fall through. */
4692 if (flags & ECF_NORETURN)
4694 /* The barrier note must be emitted
4695 immediately after the CALL_INSN. Some ports emit more than
4696 just a CALL_INSN above, so we must search for it here. */
4697 rtx_insn *last = get_last_insn ();
4698 while (!CALL_P (last))
4700 last = PREV_INSN (last);
4701 /* There was no CALL_INSN? */
4702 gcc_assert (last != before_call);
4705 emit_barrier_after (last);
4708 /* Consider that "regular" libcalls, i.e. all of them except for LCT_THROW
4709 and LCT_RETURNS_TWICE, cannot perform non-local gotos. */
4710 if (flags & ECF_NOTHROW)
4712 rtx_insn *last = get_last_insn ();
4713 while (!CALL_P (last))
4715 last = PREV_INSN (last);
4716 /* There was no CALL_INSN? */
4717 gcc_assert (last != before_call);
4720 make_reg_eh_region_note_nothrow_nononlocal (last);
4723 /* Now restore inhibit_defer_pop to its actual original value. */
4724 OK_DEFER_POP;
4726 pop_temp_slots ();
4728 /* Copy the value to the right place. */
4729 if (outmode != VOIDmode && retval)
4731 if (mem_value)
4733 if (value == 0)
4734 value = mem_value;
4735 if (value != mem_value)
4736 emit_move_insn (value, mem_value);
4738 else if (GET_CODE (valreg) == PARALLEL)
4740 if (value == 0)
4741 value = gen_reg_rtx (outmode);
4742 emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
4744 else
4746 /* Convert to the proper mode if a promotion has been active. */
4747 if (GET_MODE (valreg) != outmode)
4749 int unsignedp = TYPE_UNSIGNED (tfom);
4751 gcc_assert (promote_function_mode (tfom, outmode, &unsignedp,
4752 fndecl ? TREE_TYPE (fndecl) : fntype, 1)
4753 == GET_MODE (valreg));
4754 valreg = convert_modes (outmode, GET_MODE (valreg), valreg, 0);
4757 if (value != 0)
4758 emit_move_insn (value, valreg);
4759 else
4760 value = valreg;
4764 if (ACCUMULATE_OUTGOING_ARGS)
4766 #ifdef REG_PARM_STACK_SPACE
4767 if (save_area)
4768 restore_fixed_argument_area (save_area, argblock,
4769 high_to_save, low_to_save);
4770 #endif
4772 /* If we saved any argument areas, restore them. */
4773 for (count = 0; count < nargs; count++)
4774 if (argvec[count].save_area)
4776 machine_mode save_mode = GET_MODE (argvec[count].save_area);
4777 rtx adr = plus_constant (Pmode, argblock,
4778 argvec[count].locate.offset.constant);
4779 rtx stack_area = gen_rtx_MEM (save_mode,
4780 memory_address (save_mode, adr));
4782 if (save_mode == BLKmode)
4783 emit_block_move (stack_area,
4784 validize_mem
4785 (copy_rtx (argvec[count].save_area)),
4786 (gen_int_mode
4787 (argvec[count].locate.size.constant, Pmode)),
4788 BLOCK_OP_CALL_PARM);
4789 else
4790 emit_move_insn (stack_area, argvec[count].save_area);
4793 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
4794 stack_usage_map = initial_stack_usage_map;
4795 stack_usage_watermark = initial_stack_usage_watermark;
4798 free (stack_usage_map_buf);
4800 return value;
4805 /* Store a single argument for a function call
4806 into the register or memory area where it must be passed.
4807 *ARG describes the argument value and where to pass it.
4809 ARGBLOCK is the address of the stack-block for all the arguments,
4810 or 0 on a machine where arguments are pushed individually.
4812 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
4813 so must be careful about how the stack is used.
4815 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
4816 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
4817 that we need not worry about saving and restoring the stack.
4819 FNDECL is the declaration of the function we are calling.
4821 Return nonzero if this arg should cause sibcall failure,
4822 zero otherwise. */
4824 static int
4825 store_one_arg (struct arg_data *arg, rtx argblock, int flags,
4826 int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
4828 tree pval = arg->tree_value;
4829 rtx reg = 0;
4830 int partial = 0;
4831 poly_int64 used = 0;
4832 poly_int64 lower_bound = 0, upper_bound = 0;
4833 int sibcall_failure = 0;
4835 if (TREE_CODE (pval) == ERROR_MARK)
4836 return 1;
4838 /* Push a new temporary level for any temporaries we make for
4839 this argument. */
4840 push_temp_slots ();
4842 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
4844 /* If this is being stored into a pre-allocated, fixed-size, stack area,
4845 save any previous data at that location. */
4846 if (argblock && ! variable_size && arg->stack)
4848 if (ARGS_GROW_DOWNWARD)
4850 /* stack_slot is negative, but we want to index stack_usage_map
4851 with positive values. */
4852 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4854 rtx offset = XEXP (XEXP (arg->stack_slot, 0), 1);
4855 upper_bound = -rtx_to_poly_int64 (offset) + 1;
4857 else
4858 upper_bound = 0;
4860 lower_bound = upper_bound - arg->locate.size.constant;
4862 else
4864 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4866 rtx offset = XEXP (XEXP (arg->stack_slot, 0), 1);
4867 lower_bound = rtx_to_poly_int64 (offset);
4869 else
4870 lower_bound = 0;
4872 upper_bound = lower_bound + arg->locate.size.constant;
4875 if (stack_region_maybe_used_p (lower_bound, upper_bound,
4876 reg_parm_stack_space))
4878 /* We need to make a save area. */
4879 poly_uint64 size = arg->locate.size.constant * BITS_PER_UNIT;
4880 machine_mode save_mode
4881 = int_mode_for_size (size, 1).else_blk ();
4882 rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
4883 rtx stack_area = gen_rtx_MEM (save_mode, adr);
4885 if (save_mode == BLKmode)
4887 arg->save_area
4888 = assign_temp (TREE_TYPE (arg->tree_value), 1, 1);
4889 preserve_temp_slots (arg->save_area);
4890 emit_block_move (validize_mem (copy_rtx (arg->save_area)),
4891 stack_area,
4892 (gen_int_mode
4893 (arg->locate.size.constant, Pmode)),
4894 BLOCK_OP_CALL_PARM);
4896 else
4898 arg->save_area = gen_reg_rtx (save_mode);
4899 emit_move_insn (arg->save_area, stack_area);
4905 /* If this isn't going to be placed on both the stack and in registers,
4906 set up the register and number of words. */
4907 if (! arg->pass_on_stack)
4909 if (flags & ECF_SIBCALL)
4910 reg = arg->tail_call_reg;
4911 else
4912 reg = arg->reg;
4913 partial = arg->partial;
4916 /* Being passed entirely in a register. We shouldn't be called in
4917 this case. */
4918 gcc_assert (reg == 0 || partial != 0);
4920 /* If this arg needs special alignment, don't load the registers
4921 here. */
4922 if (arg->n_aligned_regs != 0)
4923 reg = 0;
4925 /* If this is being passed partially in a register, we can't evaluate
4926 it directly into its stack slot. Otherwise, we can. */
4927 if (arg->value == 0)
4929 /* stack_arg_under_construction is nonzero if a function argument is
4930 being evaluated directly into the outgoing argument list and
4931 expand_call must take special action to preserve the argument list
4932 if it is called recursively.
4934 For scalar function arguments stack_usage_map is sufficient to
4935 determine which stack slots must be saved and restored. Scalar
4936 arguments in general have pass_on_stack == 0.
4938 If this argument is initialized by a function which takes the
4939 address of the argument (a C++ constructor or a C function
4940 returning a BLKmode structure), then stack_usage_map is
4941 insufficient and expand_call must push the stack around the
4942 function call. Such arguments have pass_on_stack == 1.
4944 Note that it is always safe to set stack_arg_under_construction,
4945 but this generates suboptimal code if set when not needed. */
4947 if (arg->pass_on_stack)
4948 stack_arg_under_construction++;
4950 arg->value = expand_expr (pval,
4951 (partial
4952 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
4953 ? NULL_RTX : arg->stack,
4954 VOIDmode, EXPAND_STACK_PARM);
4956 /* If we are promoting object (or for any other reason) the mode
4957 doesn't agree, convert the mode. */
4959 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
4960 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
4961 arg->value, arg->unsignedp);
4963 if (arg->pass_on_stack)
4964 stack_arg_under_construction--;
4967 /* Check for overlap with already clobbered argument area. */
4968 if ((flags & ECF_SIBCALL)
4969 && MEM_P (arg->value)
4970 && mem_might_overlap_already_clobbered_arg_p (XEXP (arg->value, 0),
4971 arg->locate.size.constant))
4972 sibcall_failure = 1;
4974 /* Don't allow anything left on stack from computation
4975 of argument to alloca. */
4976 if (flags & ECF_MAY_BE_ALLOCA)
4977 do_pending_stack_adjust ();
4979 if (arg->value == arg->stack)
4980 /* If the value is already in the stack slot, we are done. */
4982 else if (arg->mode != BLKmode)
4984 unsigned int parm_align;
4986 /* Argument is a scalar, not entirely passed in registers.
4987 (If part is passed in registers, arg->partial says how much
4988 and emit_push_insn will take care of putting it there.)
4990 Push it, and if its size is less than the
4991 amount of space allocated to it,
4992 also bump stack pointer by the additional space.
4993 Note that in C the default argument promotions
4994 will prevent such mismatches. */
4996 poly_int64 size = (TYPE_EMPTY_P (TREE_TYPE (pval))
4997 ? 0 : GET_MODE_SIZE (arg->mode));
4999 /* Compute how much space the push instruction will push.
5000 On many machines, pushing a byte will advance the stack
5001 pointer by a halfword. */
5002 #ifdef PUSH_ROUNDING
5003 size = PUSH_ROUNDING (size);
5004 #endif
5005 used = size;
5007 /* Compute how much space the argument should get:
5008 round up to a multiple of the alignment for arguments. */
5009 if (targetm.calls.function_arg_padding (arg->mode, TREE_TYPE (pval))
5010 != PAD_NONE)
5011 /* At the moment we don't (need to) support ABIs for which the
5012 padding isn't known at compile time. In principle it should
5013 be easy to add though. */
5014 used = force_align_up (size, PARM_BOUNDARY / BITS_PER_UNIT);
5016 /* Compute the alignment of the pushed argument. */
5017 parm_align = arg->locate.boundary;
5018 if (targetm.calls.function_arg_padding (arg->mode, TREE_TYPE (pval))
5019 == PAD_DOWNWARD)
5021 poly_int64 pad = used - size;
5022 unsigned int pad_align = known_alignment (pad) * BITS_PER_UNIT;
5023 if (pad_align != 0)
5024 parm_align = MIN (parm_align, pad_align);
5027 /* This isn't already where we want it on the stack, so put it there.
5028 This can either be done with push or copy insns. */
5029 if (maybe_ne (used, 0)
5030 && !emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval),
5031 NULL_RTX, parm_align, partial, reg, used - size,
5032 argblock, ARGS_SIZE_RTX (arg->locate.offset),
5033 reg_parm_stack_space,
5034 ARGS_SIZE_RTX (arg->locate.alignment_pad), true))
5035 sibcall_failure = 1;
5037 /* Unless this is a partially-in-register argument, the argument is now
5038 in the stack. */
5039 if (partial == 0)
5040 arg->value = arg->stack;
5042 else
5044 /* BLKmode, at least partly to be pushed. */
5046 unsigned int parm_align;
5047 poly_int64 excess;
5048 rtx size_rtx;
5050 /* Pushing a nonscalar.
5051 If part is passed in registers, PARTIAL says how much
5052 and emit_push_insn will take care of putting it there. */
5054 /* Round its size up to a multiple
5055 of the allocation unit for arguments. */
5057 if (arg->locate.size.var != 0)
5059 excess = 0;
5060 size_rtx = ARGS_SIZE_RTX (arg->locate.size);
5062 else
5064 /* PUSH_ROUNDING has no effect on us, because emit_push_insn
5065 for BLKmode is careful to avoid it. */
5066 excess = (arg->locate.size.constant
5067 - arg_int_size_in_bytes (TREE_TYPE (pval))
5068 + partial);
5069 size_rtx = expand_expr (arg_size_in_bytes (TREE_TYPE (pval)),
5070 NULL_RTX, TYPE_MODE (sizetype),
5071 EXPAND_NORMAL);
5074 parm_align = arg->locate.boundary;
5076 /* When an argument is padded down, the block is aligned to
5077 PARM_BOUNDARY, but the actual argument isn't. */
5078 if (targetm.calls.function_arg_padding (arg->mode, TREE_TYPE (pval))
5079 == PAD_DOWNWARD)
5081 if (arg->locate.size.var)
5082 parm_align = BITS_PER_UNIT;
5083 else
5085 unsigned int excess_align
5086 = known_alignment (excess) * BITS_PER_UNIT;
5087 if (excess_align != 0)
5088 parm_align = MIN (parm_align, excess_align);
5092 if ((flags & ECF_SIBCALL) && MEM_P (arg->value))
5094 /* emit_push_insn might not work properly if arg->value and
5095 argblock + arg->locate.offset areas overlap. */
5096 rtx x = arg->value;
5097 poly_int64 i = 0;
5099 if (strip_offset (XEXP (x, 0), &i)
5100 == crtl->args.internal_arg_pointer)
5102 /* arg.locate doesn't contain the pretend_args_size offset,
5103 it's part of argblock. Ensure we don't count it in I. */
5104 if (STACK_GROWS_DOWNWARD)
5105 i -= crtl->args.pretend_args_size;
5106 else
5107 i += crtl->args.pretend_args_size;
5109 /* expand_call should ensure this. */
5110 gcc_assert (!arg->locate.offset.var
5111 && arg->locate.size.var == 0);
5112 poly_int64 size_val = rtx_to_poly_int64 (size_rtx);
5114 if (known_eq (arg->locate.offset.constant, i))
5116 /* Even though they appear to be at the same location,
5117 if part of the outgoing argument is in registers,
5118 they aren't really at the same location. Check for
5119 this by making sure that the incoming size is the
5120 same as the outgoing size. */
5121 if (maybe_ne (arg->locate.size.constant, size_val))
5122 sibcall_failure = 1;
5124 else if (maybe_in_range_p (arg->locate.offset.constant,
5125 i, size_val))
5126 sibcall_failure = 1;
5127 /* Use arg->locate.size.constant instead of size_rtx
5128 because we only care about the part of the argument
5129 on the stack. */
5130 else if (maybe_in_range_p (i, arg->locate.offset.constant,
5131 arg->locate.size.constant))
5132 sibcall_failure = 1;
5136 if (!CONST_INT_P (size_rtx) || INTVAL (size_rtx) != 0)
5137 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
5138 parm_align, partial, reg, excess, argblock,
5139 ARGS_SIZE_RTX (arg->locate.offset),
5140 reg_parm_stack_space,
5141 ARGS_SIZE_RTX (arg->locate.alignment_pad), false);
5143 /* Unless this is a partially-in-register argument, the argument is now
5144 in the stack.
5146 ??? Unlike the case above, in which we want the actual
5147 address of the data, so that we can load it directly into a
5148 register, here we want the address of the stack slot, so that
5149 it's properly aligned for word-by-word copying or something
5150 like that. It's not clear that this is always correct. */
5151 if (partial == 0)
5152 arg->value = arg->stack_slot;
5155 if (arg->reg && GET_CODE (arg->reg) == PARALLEL)
5157 tree type = TREE_TYPE (arg->tree_value);
5158 arg->parallel_value
5159 = emit_group_load_into_temps (arg->reg, arg->value, type,
5160 int_size_in_bytes (type));
5163 /* Mark all slots this store used. */
5164 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
5165 && argblock && ! variable_size && arg->stack)
5166 mark_stack_region_used (lower_bound, upper_bound);
5168 /* Once we have pushed something, pops can't safely
5169 be deferred during the rest of the arguments. */
5170 NO_DEFER_POP;
5172 /* Free any temporary slots made in processing this argument. */
5173 pop_temp_slots ();
5175 return sibcall_failure;
5178 /* Nonzero if we do not know how to pass ARG solely in registers. */
5180 bool
5181 must_pass_in_stack_var_size (const function_arg_info &arg)
5183 if (!arg.type)
5184 return false;
5186 /* If the type has variable size... */
5187 if (!poly_int_tree_p (TYPE_SIZE (arg.type)))
5188 return true;
5190 /* If the type is marked as addressable (it is required
5191 to be constructed into the stack)... */
5192 if (TREE_ADDRESSABLE (arg.type))
5193 return true;
5195 return false;
5198 /* Another version of the TARGET_MUST_PASS_IN_STACK hook. This one
5199 takes trailing padding of a structure into account. */
5200 /* ??? Should be able to merge these two by examining BLOCK_REG_PADDING. */
5202 bool
5203 must_pass_in_stack_var_size_or_pad (const function_arg_info &arg)
5205 if (!arg.type)
5206 return false;
5208 /* If the type has variable size... */
5209 if (TREE_CODE (TYPE_SIZE (arg.type)) != INTEGER_CST)
5210 return true;
5212 /* If the type is marked as addressable (it is required
5213 to be constructed into the stack)... */
5214 if (TREE_ADDRESSABLE (arg.type))
5215 return true;
5217 if (TYPE_EMPTY_P (arg.type))
5218 return false;
5220 /* If the padding and mode of the type is such that a copy into
5221 a register would put it into the wrong part of the register. */
5222 if (arg.mode == BLKmode
5223 && int_size_in_bytes (arg.type) % (PARM_BOUNDARY / BITS_PER_UNIT)
5224 && (targetm.calls.function_arg_padding (arg.mode, arg.type)
5225 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)))
5226 return true;
5228 return false;
5231 /* Return true if TYPE must be passed on the stack when passed to
5232 the "..." arguments of a function. */
5234 bool
5235 must_pass_va_arg_in_stack (tree type)
5237 function_arg_info arg (type, /*named=*/false);
5238 return targetm.calls.must_pass_in_stack (arg);
5241 /* Return true if FIELD is the C++17 empty base field that should
5242 be ignored for ABI calling convention decisions in order to
5243 maintain ABI compatibility between C++14 and earlier, which doesn't
5244 add this FIELD to classes with empty bases, and C++17 and later
5245 which does. */
5247 bool
5248 cxx17_empty_base_field_p (const_tree field)
5250 return (DECL_FIELD_ABI_IGNORED (field)
5251 && DECL_ARTIFICIAL (field)
5252 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
5253 && !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (field)));