Fix issue for pointers to anonymous types with -fdump-ada-spec
[official-gcc.git] / gcc / calls.cc
blobe13469cfd43b5bdd4ca0d2b8458a9e4f996e36e9
1 /* Convert function calls to rtl insns, for GNU C compiler.
2 Copyright (C) 1989-2022 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.cc. */
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.cc
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 bool normal_failure = false;
3072 /* We want to emit any pending stack adjustments before the tail
3073 recursion "call". That way we know any adjustment after the tail
3074 recursion call can be ignored if we indeed use the tail
3075 call expansion. */
3076 saved_pending_stack_adjust save;
3077 rtx_insn *insns, *before_call, *after_args;
3078 rtx next_arg_reg;
3080 if (pass == 0)
3082 /* State variables we need to save and restore between
3083 iterations. */
3084 save_pending_stack_adjust (&save);
3086 if (pass)
3087 flags &= ~ECF_SIBCALL;
3088 else
3089 flags |= ECF_SIBCALL;
3091 /* Other state variables that we must reinitialize each time
3092 through the loop (that are not initialized by the loop itself). */
3093 argblock = 0;
3094 call_fusage = 0;
3096 /* Start a new sequence for the normal call case.
3098 From this point on, if the sibling call fails, we want to set
3099 sibcall_failure instead of continuing the loop. */
3100 start_sequence ();
3102 /* Don't let pending stack adjusts add up to too much.
3103 Also, do all pending adjustments now if there is any chance
3104 this might be a call to alloca or if we are expanding a sibling
3105 call sequence.
3106 Also do the adjustments before a throwing call, otherwise
3107 exception handling can fail; PR 19225. */
3108 if (maybe_ge (pending_stack_adjust, 32)
3109 || (maybe_ne (pending_stack_adjust, 0)
3110 && (flags & ECF_MAY_BE_ALLOCA))
3111 || (maybe_ne (pending_stack_adjust, 0)
3112 && flag_exceptions && !(flags & ECF_NOTHROW))
3113 || pass == 0)
3114 do_pending_stack_adjust ();
3116 /* Precompute any arguments as needed. */
3117 if (pass)
3118 precompute_arguments (num_actuals, args);
3120 /* Now we are about to start emitting insns that can be deleted
3121 if a libcall is deleted. */
3122 if (pass && (flags & ECF_MALLOC))
3123 start_sequence ();
3125 if (pass == 0
3126 && crtl->stack_protect_guard
3127 && targetm.stack_protect_runtime_enabled_p ())
3128 stack_protect_epilogue ();
3130 adjusted_args_size = args_size;
3131 /* Compute the actual size of the argument block required. The variable
3132 and constant sizes must be combined, the size may have to be rounded,
3133 and there may be a minimum required size. When generating a sibcall
3134 pattern, do not round up, since we'll be re-using whatever space our
3135 caller provided. */
3136 unadjusted_args_size
3137 = compute_argument_block_size (reg_parm_stack_space,
3138 &adjusted_args_size,
3139 fndecl, fntype,
3140 (pass == 0 ? 0
3141 : preferred_stack_boundary));
3143 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
3145 /* The argument block when performing a sibling call is the
3146 incoming argument block. */
3147 if (pass == 0)
3149 argblock = crtl->args.internal_arg_pointer;
3150 if (STACK_GROWS_DOWNWARD)
3151 argblock
3152 = plus_constant (Pmode, argblock, crtl->args.pretend_args_size);
3153 else
3154 argblock
3155 = plus_constant (Pmode, argblock, -crtl->args.pretend_args_size);
3157 HOST_WIDE_INT map_size = constant_lower_bound (args_size.constant);
3158 stored_args_map = sbitmap_alloc (map_size);
3159 bitmap_clear (stored_args_map);
3160 stored_args_watermark = HOST_WIDE_INT_M1U;
3163 /* If we have no actual push instructions, or shouldn't use them,
3164 make space for all args right now. */
3165 else if (adjusted_args_size.var != 0)
3167 if (old_stack_level == 0)
3169 emit_stack_save (SAVE_BLOCK, &old_stack_level);
3170 old_stack_pointer_delta = stack_pointer_delta;
3171 old_pending_adj = pending_stack_adjust;
3172 pending_stack_adjust = 0;
3173 /* stack_arg_under_construction says whether a stack arg is
3174 being constructed at the old stack level. Pushing the stack
3175 gets a clean outgoing argument block. */
3176 old_stack_arg_under_construction = stack_arg_under_construction;
3177 stack_arg_under_construction = 0;
3179 argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
3180 if (flag_stack_usage_info)
3181 current_function_has_unbounded_dynamic_stack_size = 1;
3183 else
3185 /* Note that we must go through the motions of allocating an argument
3186 block even if the size is zero because we may be storing args
3187 in the area reserved for register arguments, which may be part of
3188 the stack frame. */
3190 poly_int64 needed = adjusted_args_size.constant;
3192 /* Store the maximum argument space used. It will be pushed by
3193 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
3194 checking). */
3196 crtl->outgoing_args_size = upper_bound (crtl->outgoing_args_size,
3197 needed);
3199 if (must_preallocate)
3201 if (ACCUMULATE_OUTGOING_ARGS)
3203 /* Since the stack pointer will never be pushed, it is
3204 possible for the evaluation of a parm to clobber
3205 something we have already written to the stack.
3206 Since most function calls on RISC machines do not use
3207 the stack, this is uncommon, but must work correctly.
3209 Therefore, we save any area of the stack that was already
3210 written and that we are using. Here we set up to do this
3211 by making a new stack usage map from the old one. The
3212 actual save will be done by store_one_arg.
3214 Another approach might be to try to reorder the argument
3215 evaluations to avoid this conflicting stack usage. */
3217 /* Since we will be writing into the entire argument area,
3218 the map must be allocated for its entire size, not just
3219 the part that is the responsibility of the caller. */
3220 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
3221 needed += reg_parm_stack_space;
3223 poly_int64 limit = needed;
3224 if (ARGS_GROW_DOWNWARD)
3225 limit += 1;
3227 /* For polynomial sizes, this is the maximum possible
3228 size needed for arguments with a constant size
3229 and offset. */
3230 HOST_WIDE_INT const_limit = constant_lower_bound (limit);
3231 highest_outgoing_arg_in_use
3232 = MAX (initial_highest_arg_in_use, const_limit);
3234 free (stack_usage_map_buf);
3235 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
3236 stack_usage_map = stack_usage_map_buf;
3238 if (initial_highest_arg_in_use)
3239 memcpy (stack_usage_map, initial_stack_usage_map,
3240 initial_highest_arg_in_use);
3242 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
3243 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
3244 (highest_outgoing_arg_in_use
3245 - initial_highest_arg_in_use));
3246 needed = 0;
3248 /* The address of the outgoing argument list must not be
3249 copied to a register here, because argblock would be left
3250 pointing to the wrong place after the call to
3251 allocate_dynamic_stack_space below. */
3253 argblock = virtual_outgoing_args_rtx;
3255 else
3257 /* Try to reuse some or all of the pending_stack_adjust
3258 to get this space. */
3259 if (inhibit_defer_pop == 0
3260 && (combine_pending_stack_adjustment_and_call
3261 (&needed,
3262 unadjusted_args_size,
3263 &adjusted_args_size,
3264 preferred_unit_stack_boundary)))
3266 /* combine_pending_stack_adjustment_and_call computes
3267 an adjustment before the arguments are allocated.
3268 Account for them and see whether or not the stack
3269 needs to go up or down. */
3270 needed = unadjusted_args_size - needed;
3272 /* Checked by
3273 combine_pending_stack_adjustment_and_call. */
3274 gcc_checking_assert (ordered_p (needed, 0));
3275 if (maybe_lt (needed, 0))
3277 /* We're releasing stack space. */
3278 /* ??? We can avoid any adjustment at all if we're
3279 already aligned. FIXME. */
3280 pending_stack_adjust = -needed;
3281 do_pending_stack_adjust ();
3282 needed = 0;
3284 else
3285 /* We need to allocate space. We'll do that in
3286 push_block below. */
3287 pending_stack_adjust = 0;
3290 /* Special case this because overhead of `push_block' in
3291 this case is non-trivial. */
3292 if (known_eq (needed, 0))
3293 argblock = virtual_outgoing_args_rtx;
3294 else
3296 rtx needed_rtx = gen_int_mode (needed, Pmode);
3297 argblock = push_block (needed_rtx, 0, 0);
3298 if (ARGS_GROW_DOWNWARD)
3299 argblock = plus_constant (Pmode, argblock, needed);
3302 /* We only really need to call `copy_to_reg' in the case
3303 where push insns are going to be used to pass ARGBLOCK
3304 to a function call in ARGS. In that case, the stack
3305 pointer changes value from the allocation point to the
3306 call point, and hence the value of
3307 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
3308 as well always do it. */
3309 argblock = copy_to_reg (argblock);
3314 if (ACCUMULATE_OUTGOING_ARGS)
3316 /* The save/restore code in store_one_arg handles all
3317 cases except one: a constructor call (including a C
3318 function returning a BLKmode struct) to initialize
3319 an argument. */
3320 if (stack_arg_under_construction)
3322 rtx push_size
3323 = (gen_int_mode
3324 (adjusted_args_size.constant
3325 + (OUTGOING_REG_PARM_STACK_SPACE (!fndecl ? fntype
3326 : TREE_TYPE (fndecl))
3327 ? 0 : reg_parm_stack_space), Pmode));
3328 if (old_stack_level == 0)
3330 emit_stack_save (SAVE_BLOCK, &old_stack_level);
3331 old_stack_pointer_delta = stack_pointer_delta;
3332 old_pending_adj = pending_stack_adjust;
3333 pending_stack_adjust = 0;
3334 /* stack_arg_under_construction says whether a stack
3335 arg is being constructed at the old stack level.
3336 Pushing the stack gets a clean outgoing argument
3337 block. */
3338 old_stack_arg_under_construction
3339 = stack_arg_under_construction;
3340 stack_arg_under_construction = 0;
3341 /* Make a new map for the new argument list. */
3342 free (stack_usage_map_buf);
3343 stack_usage_map_buf = XCNEWVEC (char, highest_outgoing_arg_in_use);
3344 stack_usage_map = stack_usage_map_buf;
3345 highest_outgoing_arg_in_use = 0;
3346 stack_usage_watermark = HOST_WIDE_INT_M1U;
3348 /* We can pass TRUE as the 4th argument because we just
3349 saved the stack pointer and will restore it right after
3350 the call. */
3351 allocate_dynamic_stack_space (push_size, 0, BIGGEST_ALIGNMENT,
3352 -1, true);
3355 /* If argument evaluation might modify the stack pointer,
3356 copy the address of the argument list to a register. */
3357 for (i = 0; i < num_actuals; i++)
3358 if (args[i].pass_on_stack)
3360 argblock = copy_addr_to_reg (argblock);
3361 break;
3365 compute_argument_addresses (args, argblock, num_actuals);
3367 /* Stack is properly aligned, pops can't safely be deferred during
3368 the evaluation of the arguments. */
3369 NO_DEFER_POP;
3371 /* Precompute all register parameters. It isn't safe to compute
3372 anything once we have started filling any specific hard regs.
3373 TLS symbols sometimes need a call to resolve. Precompute
3374 register parameters before any stack pointer manipulation
3375 to avoid unaligned stack in the called function. */
3376 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
3378 OK_DEFER_POP;
3380 /* Perform stack alignment before the first push (the last arg). */
3381 if (argblock == 0
3382 && maybe_gt (adjusted_args_size.constant, reg_parm_stack_space)
3383 && maybe_ne (adjusted_args_size.constant, unadjusted_args_size))
3385 /* When the stack adjustment is pending, we get better code
3386 by combining the adjustments. */
3387 if (maybe_ne (pending_stack_adjust, 0)
3388 && ! inhibit_defer_pop
3389 && (combine_pending_stack_adjustment_and_call
3390 (&pending_stack_adjust,
3391 unadjusted_args_size,
3392 &adjusted_args_size,
3393 preferred_unit_stack_boundary)))
3394 do_pending_stack_adjust ();
3395 else if (argblock == 0)
3396 anti_adjust_stack (gen_int_mode (adjusted_args_size.constant
3397 - unadjusted_args_size,
3398 Pmode));
3400 /* Now that the stack is properly aligned, pops can't safely
3401 be deferred during the evaluation of the arguments. */
3402 NO_DEFER_POP;
3404 /* Record the maximum pushed stack space size. We need to delay
3405 doing it this far to take into account the optimization done
3406 by combine_pending_stack_adjustment_and_call. */
3407 if (flag_stack_usage_info
3408 && !ACCUMULATE_OUTGOING_ARGS
3409 && pass
3410 && adjusted_args_size.var == 0)
3412 poly_int64 pushed = (adjusted_args_size.constant
3413 + pending_stack_adjust);
3414 current_function_pushed_stack_size
3415 = upper_bound (current_function_pushed_stack_size, pushed);
3418 funexp = rtx_for_function_call (fndecl, addr);
3420 if (CALL_EXPR_STATIC_CHAIN (exp))
3421 static_chain_value = expand_normal (CALL_EXPR_STATIC_CHAIN (exp));
3422 else
3423 static_chain_value = 0;
3425 #ifdef REG_PARM_STACK_SPACE
3426 /* Save the fixed argument area if it's part of the caller's frame and
3427 is clobbered by argument setup for this call. */
3428 if (ACCUMULATE_OUTGOING_ARGS && pass)
3429 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3430 &low_to_save, &high_to_save);
3431 #endif
3433 /* Now store (and compute if necessary) all non-register parms.
3434 These come before register parms, since they can require block-moves,
3435 which could clobber the registers used for register parms.
3436 Parms which have partial registers are not stored here,
3437 but we do preallocate space here if they want that. */
3439 for (i = 0; i < num_actuals; i++)
3441 if (args[i].reg == 0 || args[i].pass_on_stack)
3443 rtx_insn *before_arg = get_last_insn ();
3445 /* We don't allow passing huge (> 2^30 B) arguments
3446 by value. It would cause an overflow later on. */
3447 if (constant_lower_bound (adjusted_args_size.constant)
3448 >= (1 << (HOST_BITS_PER_INT - 2)))
3450 sorry ("passing too large argument on stack");
3451 /* Don't worry about stack clean-up. */
3452 if (pass == 0)
3453 sibcall_failure = 1;
3454 else
3455 normal_failure = true;
3456 continue;
3459 if (store_one_arg (&args[i], argblock, flags,
3460 adjusted_args_size.var != 0,
3461 reg_parm_stack_space)
3462 || (pass == 0
3463 && check_sibcall_argument_overlap (before_arg,
3464 &args[i], 1)))
3465 sibcall_failure = 1;
3468 if (args[i].stack)
3469 call_fusage
3470 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[i].tree_value)),
3471 gen_rtx_USE (VOIDmode, args[i].stack),
3472 call_fusage);
3475 /* If we have a parm that is passed in registers but not in memory
3476 and whose alignment does not permit a direct copy into registers,
3477 make a group of pseudos that correspond to each register that we
3478 will later fill. */
3479 if (STRICT_ALIGNMENT)
3480 store_unaligned_arguments_into_pseudos (args, num_actuals);
3482 /* Now store any partially-in-registers parm.
3483 This is the last place a block-move can happen. */
3484 if (reg_parm_seen)
3485 for (i = 0; i < num_actuals; i++)
3486 if (args[i].partial != 0 && ! args[i].pass_on_stack)
3488 rtx_insn *before_arg = get_last_insn ();
3490 /* On targets with weird calling conventions (e.g. PA) it's
3491 hard to ensure that all cases of argument overlap between
3492 stack and registers work. Play it safe and bail out. */
3493 if (ARGS_GROW_DOWNWARD && !STACK_GROWS_DOWNWARD)
3495 sibcall_failure = 1;
3496 break;
3499 if (store_one_arg (&args[i], argblock, flags,
3500 adjusted_args_size.var != 0,
3501 reg_parm_stack_space)
3502 || (pass == 0
3503 && check_sibcall_argument_overlap (before_arg,
3504 &args[i], 1)))
3505 sibcall_failure = 1;
3508 bool any_regs = false;
3509 for (i = 0; i < num_actuals; i++)
3510 if (args[i].reg != NULL_RTX)
3512 any_regs = true;
3513 targetm.calls.call_args (args[i].reg, funtype);
3515 if (!any_regs)
3516 targetm.calls.call_args (pc_rtx, funtype);
3518 /* Figure out the register where the value, if any, will come back. */
3519 valreg = 0;
3520 if (TYPE_MODE (rettype) != VOIDmode
3521 && ! structure_value_addr)
3523 if (pcc_struct_value)
3524 valreg = hard_function_value (build_pointer_type (rettype),
3525 fndecl, NULL, (pass == 0));
3526 else
3527 valreg = hard_function_value (rettype, fndecl, fntype,
3528 (pass == 0));
3530 /* If VALREG is a PARALLEL whose first member has a zero
3531 offset, use that. This is for targets such as m68k that
3532 return the same value in multiple places. */
3533 if (GET_CODE (valreg) == PARALLEL)
3535 rtx elem = XVECEXP (valreg, 0, 0);
3536 rtx where = XEXP (elem, 0);
3537 rtx offset = XEXP (elem, 1);
3538 if (offset == const0_rtx
3539 && GET_MODE (where) == GET_MODE (valreg))
3540 valreg = where;
3544 /* If register arguments require space on the stack and stack space
3545 was not preallocated, allocate stack space here for arguments
3546 passed in registers. */
3547 if (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
3548 && !ACCUMULATE_OUTGOING_ARGS
3549 && must_preallocate == 0 && reg_parm_stack_space > 0)
3550 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
3552 /* Pass the function the address in which to return a
3553 structure value. */
3554 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
3556 structure_value_addr
3557 = convert_memory_address (Pmode, structure_value_addr);
3558 emit_move_insn (struct_value,
3559 force_reg (Pmode,
3560 force_operand (structure_value_addr,
3561 NULL_RTX)));
3563 if (REG_P (struct_value))
3564 use_reg (&call_fusage, struct_value);
3567 after_args = get_last_insn ();
3568 funexp = prepare_call_address (fndecl ? fndecl : fntype, funexp,
3569 static_chain_value, &call_fusage,
3570 reg_parm_seen, flags);
3572 load_register_parameters (args, num_actuals, &call_fusage, flags,
3573 pass == 0, &sibcall_failure);
3575 /* Save a pointer to the last insn before the call, so that we can
3576 later safely search backwards to find the CALL_INSN. */
3577 before_call = get_last_insn ();
3579 /* Set up next argument register. For sibling calls on machines
3580 with register windows this should be the incoming register. */
3581 if (pass == 0)
3582 next_arg_reg = targetm.calls.function_incoming_arg
3583 (args_so_far, function_arg_info::end_marker ());
3584 else
3585 next_arg_reg = targetm.calls.function_arg
3586 (args_so_far, function_arg_info::end_marker ());
3588 if (pass == 1 && (return_flags & ERF_RETURNS_ARG))
3590 int arg_nr = return_flags & ERF_RETURN_ARG_MASK;
3591 arg_nr = num_actuals - arg_nr - 1;
3592 if (arg_nr >= 0
3593 && arg_nr < num_actuals
3594 && args[arg_nr].reg
3595 && valreg
3596 && REG_P (valreg)
3597 && GET_MODE (args[arg_nr].reg) == GET_MODE (valreg))
3598 call_fusage
3599 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[arg_nr].tree_value)),
3600 gen_rtx_SET (valreg, args[arg_nr].reg),
3601 call_fusage);
3603 /* All arguments and registers used for the call must be set up by
3604 now! */
3606 /* Stack must be properly aligned now. */
3607 gcc_assert (!pass
3608 || multiple_p (stack_pointer_delta,
3609 preferred_unit_stack_boundary));
3611 /* Generate the actual call instruction. */
3612 emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
3613 adjusted_args_size.constant, struct_value_size,
3614 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
3615 flags, args_so_far);
3617 if (flag_ipa_ra)
3619 rtx_call_insn *last;
3620 rtx datum = NULL_RTX;
3621 if (fndecl != NULL_TREE)
3623 datum = XEXP (DECL_RTL (fndecl), 0);
3624 gcc_assert (datum != NULL_RTX
3625 && GET_CODE (datum) == SYMBOL_REF);
3627 last = last_call_insn ();
3628 add_reg_note (last, REG_CALL_DECL, datum);
3631 /* If the call setup or the call itself overlaps with anything
3632 of the argument setup we probably clobbered our call address.
3633 In that case we can't do sibcalls. */
3634 if (pass == 0
3635 && check_sibcall_argument_overlap (after_args, 0, 0))
3636 sibcall_failure = 1;
3638 /* If a non-BLKmode value is returned at the most significant end
3639 of a register, shift the register right by the appropriate amount
3640 and update VALREG accordingly. BLKmode values are handled by the
3641 group load/store machinery below. */
3642 if (!structure_value_addr
3643 && !pcc_struct_value
3644 && TYPE_MODE (rettype) != VOIDmode
3645 && TYPE_MODE (rettype) != BLKmode
3646 && REG_P (valreg)
3647 && targetm.calls.return_in_msb (rettype))
3649 if (shift_return_value (TYPE_MODE (rettype), false, valreg))
3650 sibcall_failure = 1;
3651 valreg = gen_rtx_REG (TYPE_MODE (rettype), REGNO (valreg));
3654 if (pass && (flags & ECF_MALLOC))
3656 rtx temp = gen_reg_rtx (GET_MODE (valreg));
3657 rtx_insn *last, *insns;
3659 /* The return value from a malloc-like function is a pointer. */
3660 if (TREE_CODE (rettype) == POINTER_TYPE)
3661 mark_reg_pointer (temp, MALLOC_ABI_ALIGNMENT);
3663 emit_move_insn (temp, valreg);
3665 /* The return value from a malloc-like function cannot alias
3666 anything else. */
3667 last = get_last_insn ();
3668 add_reg_note (last, REG_NOALIAS, temp);
3670 /* Write out the sequence. */
3671 insns = get_insns ();
3672 end_sequence ();
3673 emit_insn (insns);
3674 valreg = temp;
3677 /* For calls to `setjmp', etc., inform
3678 function.cc:setjmp_warnings that it should complain if
3679 nonvolatile values are live. For functions that cannot
3680 return, inform flow that control does not fall through. */
3682 if ((flags & ECF_NORETURN) || pass == 0)
3684 /* The barrier must be emitted
3685 immediately after the CALL_INSN. Some ports emit more
3686 than just a CALL_INSN above, so we must search for it here. */
3688 rtx_insn *last = get_last_insn ();
3689 while (!CALL_P (last))
3691 last = PREV_INSN (last);
3692 /* There was no CALL_INSN? */
3693 gcc_assert (last != before_call);
3696 emit_barrier_after (last);
3698 /* Stack adjustments after a noreturn call are dead code.
3699 However when NO_DEFER_POP is in effect, we must preserve
3700 stack_pointer_delta. */
3701 if (inhibit_defer_pop == 0)
3703 stack_pointer_delta = old_stack_allocated;
3704 pending_stack_adjust = 0;
3708 /* If value type not void, return an rtx for the value. */
3710 if (TYPE_MODE (rettype) == VOIDmode
3711 || ignore)
3712 target = const0_rtx;
3713 else if (structure_value_addr)
3715 if (target == 0 || !MEM_P (target))
3717 target
3718 = gen_rtx_MEM (TYPE_MODE (rettype),
3719 memory_address (TYPE_MODE (rettype),
3720 structure_value_addr));
3721 set_mem_attributes (target, rettype, 1);
3724 else if (pcc_struct_value)
3726 /* This is the special C++ case where we need to
3727 know what the true target was. We take care to
3728 never use this value more than once in one expression. */
3729 target = gen_rtx_MEM (TYPE_MODE (rettype),
3730 copy_to_reg (valreg));
3731 set_mem_attributes (target, rettype, 1);
3733 /* Handle calls that return values in multiple non-contiguous locations.
3734 The Irix 6 ABI has examples of this. */
3735 else if (GET_CODE (valreg) == PARALLEL)
3737 if (target == 0)
3738 target = emit_group_move_into_temps (valreg);
3739 else if (rtx_equal_p (target, valreg))
3741 else if (GET_CODE (target) == PARALLEL)
3742 /* Handle the result of a emit_group_move_into_temps
3743 call in the previous pass. */
3744 emit_group_move (target, valreg);
3745 else
3746 emit_group_store (target, valreg, rettype,
3747 int_size_in_bytes (rettype));
3749 else if (target
3750 && GET_MODE (target) == TYPE_MODE (rettype)
3751 && GET_MODE (target) == GET_MODE (valreg))
3753 bool may_overlap = false;
3755 /* We have to copy a return value in a CLASS_LIKELY_SPILLED hard
3756 reg to a plain register. */
3757 if (!REG_P (target) || HARD_REGISTER_P (target))
3758 valreg = avoid_likely_spilled_reg (valreg);
3760 /* If TARGET is a MEM in the argument area, and we have
3761 saved part of the argument area, then we can't store
3762 directly into TARGET as it may get overwritten when we
3763 restore the argument save area below. Don't work too
3764 hard though and simply force TARGET to a register if it
3765 is a MEM; the optimizer is quite likely to sort it out. */
3766 if (ACCUMULATE_OUTGOING_ARGS && pass && MEM_P (target))
3767 for (i = 0; i < num_actuals; i++)
3768 if (args[i].save_area)
3770 may_overlap = true;
3771 break;
3774 if (may_overlap)
3775 target = copy_to_reg (valreg);
3776 else
3778 /* TARGET and VALREG cannot be equal at this point
3779 because the latter would not have
3780 REG_FUNCTION_VALUE_P true, while the former would if
3781 it were referring to the same register.
3783 If they refer to the same register, this move will be
3784 a no-op, except when function inlining is being
3785 done. */
3786 emit_move_insn (target, valreg);
3788 /* If we are setting a MEM, this code must be executed.
3789 Since it is emitted after the call insn, sibcall
3790 optimization cannot be performed in that case. */
3791 if (MEM_P (target))
3792 sibcall_failure = 1;
3795 else
3796 target = copy_to_reg (avoid_likely_spilled_reg (valreg));
3798 /* If we promoted this return value, make the proper SUBREG.
3799 TARGET might be const0_rtx here, so be careful. */
3800 if (REG_P (target)
3801 && TYPE_MODE (rettype) != BLKmode
3802 && GET_MODE (target) != TYPE_MODE (rettype))
3804 tree type = rettype;
3805 int unsignedp = TYPE_UNSIGNED (type);
3806 machine_mode pmode;
3808 /* Ensure we promote as expected, and get the new unsignedness. */
3809 pmode = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
3810 funtype, 1);
3811 gcc_assert (GET_MODE (target) == pmode);
3813 poly_uint64 offset = subreg_lowpart_offset (TYPE_MODE (type),
3814 GET_MODE (target));
3815 target = gen_rtx_SUBREG (TYPE_MODE (type), target, offset);
3816 SUBREG_PROMOTED_VAR_P (target) = 1;
3817 SUBREG_PROMOTED_SET (target, unsignedp);
3820 /* If size of args is variable or this was a constructor call for a stack
3821 argument, restore saved stack-pointer value. */
3823 if (old_stack_level)
3825 rtx_insn *prev = get_last_insn ();
3827 emit_stack_restore (SAVE_BLOCK, old_stack_level);
3828 stack_pointer_delta = old_stack_pointer_delta;
3830 fixup_args_size_notes (prev, get_last_insn (), stack_pointer_delta);
3832 pending_stack_adjust = old_pending_adj;
3833 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
3834 stack_arg_under_construction = old_stack_arg_under_construction;
3835 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3836 stack_usage_map = initial_stack_usage_map;
3837 stack_usage_watermark = initial_stack_usage_watermark;
3838 sibcall_failure = 1;
3840 else if (ACCUMULATE_OUTGOING_ARGS && pass)
3842 #ifdef REG_PARM_STACK_SPACE
3843 if (save_area)
3844 restore_fixed_argument_area (save_area, argblock,
3845 high_to_save, low_to_save);
3846 #endif
3848 /* If we saved any argument areas, restore them. */
3849 for (i = 0; i < num_actuals; i++)
3850 if (args[i].save_area)
3852 machine_mode save_mode = GET_MODE (args[i].save_area);
3853 rtx stack_area
3854 = gen_rtx_MEM (save_mode,
3855 memory_address (save_mode,
3856 XEXP (args[i].stack_slot, 0)));
3858 if (save_mode != BLKmode)
3859 emit_move_insn (stack_area, args[i].save_area);
3860 else
3861 emit_block_move (stack_area, args[i].save_area,
3862 (gen_int_mode
3863 (args[i].locate.size.constant, Pmode)),
3864 BLOCK_OP_CALL_PARM);
3867 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3868 stack_usage_map = initial_stack_usage_map;
3869 stack_usage_watermark = initial_stack_usage_watermark;
3872 /* If this was alloca, record the new stack level. */
3873 if (flags & ECF_MAY_BE_ALLOCA)
3874 record_new_stack_level ();
3876 /* Free up storage we no longer need. */
3877 for (i = 0; i < num_actuals; ++i)
3878 free (args[i].aligned_regs);
3880 targetm.calls.end_call_args ();
3882 insns = get_insns ();
3883 end_sequence ();
3885 if (pass == 0)
3887 tail_call_insns = insns;
3889 /* Restore the pending stack adjustment now that we have
3890 finished generating the sibling call sequence. */
3892 restore_pending_stack_adjust (&save);
3894 /* Prepare arg structure for next iteration. */
3895 for (i = 0; i < num_actuals; i++)
3897 args[i].value = 0;
3898 args[i].aligned_regs = 0;
3899 args[i].stack = 0;
3902 sbitmap_free (stored_args_map);
3903 internal_arg_pointer_exp_state.scan_start = NULL;
3904 internal_arg_pointer_exp_state.cache.release ();
3906 else
3908 normal_call_insns = insns;
3910 /* Verify that we've deallocated all the stack we used. */
3911 gcc_assert ((flags & ECF_NORETURN)
3912 || normal_failure
3913 || known_eq (old_stack_allocated,
3914 stack_pointer_delta
3915 - pending_stack_adjust));
3916 if (normal_failure)
3917 normal_call_insns = NULL;
3920 /* If something prevents making this a sibling call,
3921 zero out the sequence. */
3922 if (sibcall_failure)
3923 tail_call_insns = NULL;
3924 else
3925 break;
3928 /* If tail call production succeeded, we need to remove REG_EQUIV notes on
3929 arguments too, as argument area is now clobbered by the call. */
3930 if (tail_call_insns)
3932 emit_insn (tail_call_insns);
3933 crtl->tail_call_emit = true;
3935 else
3937 emit_insn (normal_call_insns);
3938 if (try_tail_call)
3939 /* Ideally we'd emit a message for all of the ways that it could
3940 have failed. */
3941 maybe_complain_about_tail_call (exp, "tail call production failed");
3944 currently_expanding_call--;
3946 free (stack_usage_map_buf);
3947 free (args);
3948 return target;
3951 /* A sibling call sequence invalidates any REG_EQUIV notes made for
3952 this function's incoming arguments.
3954 At the start of RTL generation we know the only REG_EQUIV notes
3955 in the rtl chain are those for incoming arguments, so we can look
3956 for REG_EQUIV notes between the start of the function and the
3957 NOTE_INSN_FUNCTION_BEG.
3959 This is (slight) overkill. We could keep track of the highest
3960 argument we clobber and be more selective in removing notes, but it
3961 does not seem to be worth the effort. */
3963 void
3964 fixup_tail_calls (void)
3966 rtx_insn *insn;
3968 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3970 rtx note;
3972 /* There are never REG_EQUIV notes for the incoming arguments
3973 after the NOTE_INSN_FUNCTION_BEG note, so stop if we see it. */
3974 if (NOTE_P (insn)
3975 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3976 break;
3978 note = find_reg_note (insn, REG_EQUIV, 0);
3979 if (note)
3980 remove_note (insn, note);
3981 note = find_reg_note (insn, REG_EQUIV, 0);
3982 gcc_assert (!note);
3986 /* Traverse a list of TYPES and expand all complex types into their
3987 components. */
3988 static tree
3989 split_complex_types (tree types)
3991 tree p;
3993 /* Before allocating memory, check for the common case of no complex. */
3994 for (p = types; p; p = TREE_CHAIN (p))
3996 tree type = TREE_VALUE (p);
3997 if (TREE_CODE (type) == COMPLEX_TYPE
3998 && targetm.calls.split_complex_arg (type))
3999 goto found;
4001 return types;
4003 found:
4004 types = copy_list (types);
4006 for (p = types; p; p = TREE_CHAIN (p))
4008 tree complex_type = TREE_VALUE (p);
4010 if (TREE_CODE (complex_type) == COMPLEX_TYPE
4011 && targetm.calls.split_complex_arg (complex_type))
4013 tree next, imag;
4015 /* Rewrite complex type with component type. */
4016 TREE_VALUE (p) = TREE_TYPE (complex_type);
4017 next = TREE_CHAIN (p);
4019 /* Add another component type for the imaginary part. */
4020 imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
4021 TREE_CHAIN (p) = imag;
4022 TREE_CHAIN (imag) = next;
4024 /* Skip the newly created node. */
4025 p = TREE_CHAIN (p);
4029 return types;
4032 /* Output a library call to function ORGFUN (a SYMBOL_REF rtx)
4033 for a value of mode OUTMODE,
4034 with NARGS different arguments, passed as ARGS.
4035 Store the return value if RETVAL is nonzero: store it in VALUE if
4036 VALUE is nonnull, otherwise pick a convenient location. In either
4037 case return the location of the stored value.
4039 FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for
4040 `const' calls, LCT_PURE for `pure' calls, or another LCT_ value for
4041 other types of library calls. */
4044 emit_library_call_value_1 (int retval, rtx orgfun, rtx value,
4045 enum libcall_type fn_type,
4046 machine_mode outmode, int nargs, rtx_mode_t *args)
4048 /* Total size in bytes of all the stack-parms scanned so far. */
4049 struct args_size args_size;
4050 /* Size of arguments before any adjustments (such as rounding). */
4051 struct args_size original_args_size;
4052 int argnum;
4053 rtx fun;
4054 /* Todo, choose the correct decl type of orgfun. Sadly this information
4055 isn't present here, so we default to native calling abi here. */
4056 tree fndecl ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
4057 tree fntype ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
4058 int count;
4059 rtx argblock = 0;
4060 CUMULATIVE_ARGS args_so_far_v;
4061 cumulative_args_t args_so_far;
4062 struct arg
4064 rtx value;
4065 machine_mode mode;
4066 rtx reg;
4067 int partial;
4068 struct locate_and_pad_arg_data locate;
4069 rtx save_area;
4071 struct arg *argvec;
4072 int old_inhibit_defer_pop = inhibit_defer_pop;
4073 rtx call_fusage = 0;
4074 rtx mem_value = 0;
4075 rtx valreg;
4076 int pcc_struct_value = 0;
4077 poly_int64 struct_value_size = 0;
4078 int flags;
4079 int reg_parm_stack_space = 0;
4080 poly_int64 needed;
4081 rtx_insn *before_call;
4082 bool have_push_fusage;
4083 tree tfom; /* type_for_mode (outmode, 0) */
4085 #ifdef REG_PARM_STACK_SPACE
4086 /* Define the boundary of the register parm stack space that needs to be
4087 save, if any. */
4088 int low_to_save = 0, high_to_save = 0;
4089 rtx save_area = 0; /* Place that it is saved. */
4090 #endif
4092 /* Size of the stack reserved for parameter registers. */
4093 unsigned int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
4094 char *initial_stack_usage_map = stack_usage_map;
4095 unsigned HOST_WIDE_INT initial_stack_usage_watermark = stack_usage_watermark;
4096 char *stack_usage_map_buf = NULL;
4098 rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
4100 #ifdef REG_PARM_STACK_SPACE
4101 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
4102 #endif
4104 /* By default, library functions cannot throw. */
4105 flags = ECF_NOTHROW;
4107 switch (fn_type)
4109 case LCT_NORMAL:
4110 break;
4111 case LCT_CONST:
4112 flags |= ECF_CONST;
4113 break;
4114 case LCT_PURE:
4115 flags |= ECF_PURE;
4116 break;
4117 case LCT_NORETURN:
4118 flags |= ECF_NORETURN;
4119 break;
4120 case LCT_THROW:
4121 flags &= ~ECF_NOTHROW;
4122 break;
4123 case LCT_RETURNS_TWICE:
4124 flags = ECF_RETURNS_TWICE;
4125 break;
4127 fun = orgfun;
4129 /* Ensure current function's preferred stack boundary is at least
4130 what we need. */
4131 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
4132 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
4134 /* If this kind of value comes back in memory,
4135 decide where in memory it should come back. */
4136 if (outmode != VOIDmode)
4138 tfom = lang_hooks.types.type_for_mode (outmode, 0);
4139 if (aggregate_value_p (tfom, 0))
4141 #ifdef PCC_STATIC_STRUCT_RETURN
4142 rtx pointer_reg
4143 = hard_function_value (build_pointer_type (tfom), 0, 0, 0);
4144 mem_value = gen_rtx_MEM (outmode, pointer_reg);
4145 pcc_struct_value = 1;
4146 if (value == 0)
4147 value = gen_reg_rtx (outmode);
4148 #else /* not PCC_STATIC_STRUCT_RETURN */
4149 struct_value_size = GET_MODE_SIZE (outmode);
4150 if (value != 0 && MEM_P (value))
4151 mem_value = value;
4152 else
4153 mem_value = assign_temp (tfom, 1, 1);
4154 #endif
4155 /* This call returns a big structure. */
4156 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
4159 else
4160 tfom = void_type_node;
4162 /* ??? Unfinished: must pass the memory address as an argument. */
4164 /* Copy all the libcall-arguments out of the varargs data
4165 and into a vector ARGVEC.
4167 Compute how to pass each argument. We only support a very small subset
4168 of the full argument passing conventions to limit complexity here since
4169 library functions shouldn't have many args. */
4171 argvec = XALLOCAVEC (struct arg, nargs + 1);
4172 memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
4174 #ifdef INIT_CUMULATIVE_LIBCALL_ARGS
4175 INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far_v, outmode, fun);
4176 #else
4177 INIT_CUMULATIVE_ARGS (args_so_far_v, NULL_TREE, fun, 0, nargs);
4178 #endif
4179 args_so_far = pack_cumulative_args (&args_so_far_v);
4181 args_size.constant = 0;
4182 args_size.var = 0;
4184 count = 0;
4186 push_temp_slots ();
4188 /* If there's a structure value address to be passed,
4189 either pass it in the special place, or pass it as an extra argument. */
4190 if (mem_value && struct_value == 0 && ! pcc_struct_value)
4192 rtx addr = XEXP (mem_value, 0);
4194 nargs++;
4196 /* Make sure it is a reasonable operand for a move or push insn. */
4197 if (!REG_P (addr) && !MEM_P (addr)
4198 && !(CONSTANT_P (addr)
4199 && targetm.legitimate_constant_p (Pmode, addr)))
4200 addr = force_operand (addr, NULL_RTX);
4202 argvec[count].value = addr;
4203 argvec[count].mode = Pmode;
4204 argvec[count].partial = 0;
4206 function_arg_info ptr_arg (Pmode, /*named=*/true);
4207 argvec[count].reg = targetm.calls.function_arg (args_so_far, ptr_arg);
4208 gcc_assert (targetm.calls.arg_partial_bytes (args_so_far, ptr_arg) == 0);
4210 locate_and_pad_parm (Pmode, NULL_TREE,
4211 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4213 #else
4214 argvec[count].reg != 0,
4215 #endif
4216 reg_parm_stack_space, 0,
4217 NULL_TREE, &args_size, &argvec[count].locate);
4219 if (argvec[count].reg == 0 || argvec[count].partial != 0
4220 || reg_parm_stack_space > 0)
4221 args_size.constant += argvec[count].locate.size.constant;
4223 targetm.calls.function_arg_advance (args_so_far, ptr_arg);
4225 count++;
4228 for (unsigned int i = 0; count < nargs; i++, count++)
4230 rtx val = args[i].first;
4231 function_arg_info arg (args[i].second, /*named=*/true);
4232 int unsigned_p = 0;
4234 /* We cannot convert the arg value to the mode the library wants here;
4235 must do it earlier where we know the signedness of the arg. */
4236 gcc_assert (arg.mode != BLKmode
4237 && (GET_MODE (val) == arg.mode
4238 || GET_MODE (val) == VOIDmode));
4240 /* Make sure it is a reasonable operand for a move or push insn. */
4241 if (!REG_P (val) && !MEM_P (val)
4242 && !(CONSTANT_P (val)
4243 && targetm.legitimate_constant_p (arg.mode, val)))
4244 val = force_operand (val, NULL_RTX);
4246 if (pass_by_reference (&args_so_far_v, arg))
4248 rtx slot;
4249 int must_copy = !reference_callee_copied (&args_so_far_v, arg);
4251 /* If this was a CONST function, it is now PURE since it now
4252 reads memory. */
4253 if (flags & ECF_CONST)
4255 flags &= ~ECF_CONST;
4256 flags |= ECF_PURE;
4259 if (MEM_P (val) && !must_copy)
4261 tree val_expr = MEM_EXPR (val);
4262 if (val_expr)
4263 mark_addressable (val_expr);
4264 slot = val;
4266 else
4268 slot = assign_temp (lang_hooks.types.type_for_mode (arg.mode, 0),
4269 1, 1);
4270 emit_move_insn (slot, val);
4273 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4274 gen_rtx_USE (VOIDmode, slot),
4275 call_fusage);
4276 if (must_copy)
4277 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4278 gen_rtx_CLOBBER (VOIDmode,
4279 slot),
4280 call_fusage);
4282 arg.mode = Pmode;
4283 arg.pass_by_reference = true;
4284 val = force_operand (XEXP (slot, 0), NULL_RTX);
4287 arg.mode = promote_function_mode (NULL_TREE, arg.mode, &unsigned_p,
4288 NULL_TREE, 0);
4289 argvec[count].mode = arg.mode;
4290 argvec[count].value = convert_modes (arg.mode, GET_MODE (val), val,
4291 unsigned_p);
4292 argvec[count].reg = targetm.calls.function_arg (args_so_far, arg);
4294 argvec[count].partial
4295 = targetm.calls.arg_partial_bytes (args_so_far, arg);
4297 if (argvec[count].reg == 0
4298 || argvec[count].partial != 0
4299 || reg_parm_stack_space > 0)
4301 locate_and_pad_parm (arg.mode, NULL_TREE,
4302 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4304 #else
4305 argvec[count].reg != 0,
4306 #endif
4307 reg_parm_stack_space, argvec[count].partial,
4308 NULL_TREE, &args_size, &argvec[count].locate);
4309 args_size.constant += argvec[count].locate.size.constant;
4310 gcc_assert (!argvec[count].locate.size.var);
4312 #ifdef BLOCK_REG_PADDING
4313 else
4314 /* The argument is passed entirely in registers. See at which
4315 end it should be padded. */
4316 argvec[count].locate.where_pad =
4317 BLOCK_REG_PADDING (arg.mode, NULL_TREE,
4318 known_le (GET_MODE_SIZE (arg.mode),
4319 UNITS_PER_WORD));
4320 #endif
4322 targetm.calls.function_arg_advance (args_so_far, arg);
4325 for (int i = 0; i < nargs; i++)
4326 if (reg_parm_stack_space > 0
4327 || argvec[i].reg == 0
4328 || argvec[i].partial != 0)
4329 update_stack_alignment_for_call (&argvec[i].locate);
4331 /* If this machine requires an external definition for library
4332 functions, write one out. */
4333 assemble_external_libcall (fun);
4335 original_args_size = args_size;
4336 args_size.constant = (aligned_upper_bound (args_size.constant
4337 + stack_pointer_delta,
4338 STACK_BYTES)
4339 - stack_pointer_delta);
4341 args_size.constant = upper_bound (args_size.constant,
4342 reg_parm_stack_space);
4344 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4345 args_size.constant -= reg_parm_stack_space;
4347 crtl->outgoing_args_size = upper_bound (crtl->outgoing_args_size,
4348 args_size.constant);
4350 if (flag_stack_usage_info && !ACCUMULATE_OUTGOING_ARGS)
4352 poly_int64 pushed = args_size.constant + pending_stack_adjust;
4353 current_function_pushed_stack_size
4354 = upper_bound (current_function_pushed_stack_size, pushed);
4357 if (ACCUMULATE_OUTGOING_ARGS)
4359 /* Since the stack pointer will never be pushed, it is possible for
4360 the evaluation of a parm to clobber something we have already
4361 written to the stack. Since most function calls on RISC machines
4362 do not use the stack, this is uncommon, but must work correctly.
4364 Therefore, we save any area of the stack that was already written
4365 and that we are using. Here we set up to do this by making a new
4366 stack usage map from the old one.
4368 Another approach might be to try to reorder the argument
4369 evaluations to avoid this conflicting stack usage. */
4371 needed = args_size.constant;
4373 /* Since we will be writing into the entire argument area, the
4374 map must be allocated for its entire size, not just the part that
4375 is the responsibility of the caller. */
4376 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4377 needed += reg_parm_stack_space;
4379 poly_int64 limit = needed;
4380 if (ARGS_GROW_DOWNWARD)
4381 limit += 1;
4383 /* For polynomial sizes, this is the maximum possible size needed
4384 for arguments with a constant size and offset. */
4385 HOST_WIDE_INT const_limit = constant_lower_bound (limit);
4386 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
4387 const_limit);
4389 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
4390 stack_usage_map = stack_usage_map_buf;
4392 if (initial_highest_arg_in_use)
4393 memcpy (stack_usage_map, initial_stack_usage_map,
4394 initial_highest_arg_in_use);
4396 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
4397 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
4398 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
4399 needed = 0;
4401 /* We must be careful to use virtual regs before they're instantiated,
4402 and real regs afterwards. Loop optimization, for example, can create
4403 new libcalls after we've instantiated the virtual regs, and if we
4404 use virtuals anyway, they won't match the rtl patterns. */
4406 if (virtuals_instantiated)
4407 argblock = plus_constant (Pmode, stack_pointer_rtx,
4408 STACK_POINTER_OFFSET);
4409 else
4410 argblock = virtual_outgoing_args_rtx;
4412 else
4414 if (!targetm.calls.push_argument (0))
4415 argblock = push_block (gen_int_mode (args_size.constant, Pmode), 0, 0);
4418 /* We push args individually in reverse order, perform stack alignment
4419 before the first push (the last arg). */
4420 if (argblock == 0)
4421 anti_adjust_stack (gen_int_mode (args_size.constant
4422 - original_args_size.constant,
4423 Pmode));
4425 argnum = nargs - 1;
4427 #ifdef REG_PARM_STACK_SPACE
4428 if (ACCUMULATE_OUTGOING_ARGS)
4430 /* The argument list is the property of the called routine and it
4431 may clobber it. If the fixed area has been used for previous
4432 parameters, we must save and restore it. */
4433 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
4434 &low_to_save, &high_to_save);
4436 #endif
4438 /* When expanding a normal call, args are stored in push order,
4439 which is the reverse of what we have here. */
4440 bool any_regs = false;
4441 for (int i = nargs; i-- > 0; )
4442 if (argvec[i].reg != NULL_RTX)
4444 targetm.calls.call_args (argvec[i].reg, NULL_TREE);
4445 any_regs = true;
4447 if (!any_regs)
4448 targetm.calls.call_args (pc_rtx, NULL_TREE);
4450 /* Push the args that need to be pushed. */
4452 have_push_fusage = false;
4454 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4455 are to be pushed. */
4456 for (count = 0; count < nargs; count++, argnum--)
4458 machine_mode mode = argvec[argnum].mode;
4459 rtx val = argvec[argnum].value;
4460 rtx reg = argvec[argnum].reg;
4461 int partial = argvec[argnum].partial;
4462 unsigned int parm_align = argvec[argnum].locate.boundary;
4463 poly_int64 lower_bound = 0, upper_bound = 0;
4465 if (! (reg != 0 && partial == 0))
4467 rtx use;
4469 if (ACCUMULATE_OUTGOING_ARGS)
4471 /* If this is being stored into a pre-allocated, fixed-size,
4472 stack area, save any previous data at that location. */
4474 if (ARGS_GROW_DOWNWARD)
4476 /* stack_slot is negative, but we want to index stack_usage_map
4477 with positive values. */
4478 upper_bound = -argvec[argnum].locate.slot_offset.constant + 1;
4479 lower_bound = upper_bound - argvec[argnum].locate.size.constant;
4481 else
4483 lower_bound = argvec[argnum].locate.slot_offset.constant;
4484 upper_bound = lower_bound + argvec[argnum].locate.size.constant;
4487 if (stack_region_maybe_used_p (lower_bound, upper_bound,
4488 reg_parm_stack_space))
4490 /* We need to make a save area. */
4491 poly_uint64 size
4492 = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
4493 machine_mode save_mode
4494 = int_mode_for_size (size, 1).else_blk ();
4495 rtx adr
4496 = plus_constant (Pmode, argblock,
4497 argvec[argnum].locate.offset.constant);
4498 rtx stack_area
4499 = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
4501 if (save_mode == BLKmode)
4503 argvec[argnum].save_area
4504 = assign_stack_temp (BLKmode,
4505 argvec[argnum].locate.size.constant
4508 emit_block_move (validize_mem
4509 (copy_rtx (argvec[argnum].save_area)),
4510 stack_area,
4511 (gen_int_mode
4512 (argvec[argnum].locate.size.constant,
4513 Pmode)),
4514 BLOCK_OP_CALL_PARM);
4516 else
4518 argvec[argnum].save_area = gen_reg_rtx (save_mode);
4520 emit_move_insn (argvec[argnum].save_area, stack_area);
4525 emit_push_insn (val, mode, NULL_TREE, NULL_RTX, parm_align,
4526 partial, reg, 0, argblock,
4527 (gen_int_mode
4528 (argvec[argnum].locate.offset.constant, Pmode)),
4529 reg_parm_stack_space,
4530 ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad), false);
4532 /* Now mark the segment we just used. */
4533 if (ACCUMULATE_OUTGOING_ARGS)
4534 mark_stack_region_used (lower_bound, upper_bound);
4536 NO_DEFER_POP;
4538 /* Indicate argument access so that alias.cc knows that these
4539 values are live. */
4540 if (argblock)
4541 use = plus_constant (Pmode, argblock,
4542 argvec[argnum].locate.offset.constant);
4543 else if (have_push_fusage)
4544 continue;
4545 else
4547 /* When arguments are pushed, trying to tell alias.cc where
4548 exactly this argument is won't work, because the
4549 auto-increment causes confusion. So we merely indicate
4550 that we access something with a known mode somewhere on
4551 the stack. */
4552 use = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4553 gen_rtx_SCRATCH (Pmode));
4554 have_push_fusage = true;
4556 use = gen_rtx_MEM (argvec[argnum].mode, use);
4557 use = gen_rtx_USE (VOIDmode, use);
4558 call_fusage = gen_rtx_EXPR_LIST (VOIDmode, use, call_fusage);
4562 argnum = nargs - 1;
4564 fun = prepare_call_address (NULL, fun, NULL, &call_fusage, 0, 0);
4566 /* Now load any reg parms into their regs. */
4568 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4569 are to be pushed. */
4570 for (count = 0; count < nargs; count++, argnum--)
4572 machine_mode mode = argvec[argnum].mode;
4573 rtx val = argvec[argnum].value;
4574 rtx reg = argvec[argnum].reg;
4575 int partial = argvec[argnum].partial;
4577 /* Handle calls that pass values in multiple non-contiguous
4578 locations. The PA64 has examples of this for library calls. */
4579 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4580 emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (mode));
4581 else if (reg != 0 && partial == 0)
4583 emit_move_insn (reg, val);
4584 #ifdef BLOCK_REG_PADDING
4585 poly_int64 size = GET_MODE_SIZE (argvec[argnum].mode);
4587 /* Copied from load_register_parameters. */
4589 /* Handle case where we have a value that needs shifting
4590 up to the msb. eg. a QImode value and we're padding
4591 upward on a BYTES_BIG_ENDIAN machine. */
4592 if (known_lt (size, UNITS_PER_WORD)
4593 && (argvec[argnum].locate.where_pad
4594 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)))
4596 rtx x;
4597 poly_int64 shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
4599 /* Assigning REG here rather than a temp makes CALL_FUSAGE
4600 report the whole reg as used. Strictly speaking, the
4601 call only uses SIZE bytes at the msb end, but it doesn't
4602 seem worth generating rtl to say that. */
4603 reg = gen_rtx_REG (word_mode, REGNO (reg));
4604 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
4605 if (x != reg)
4606 emit_move_insn (reg, x);
4608 #endif
4611 NO_DEFER_POP;
4614 /* Any regs containing parms remain in use through the call. */
4615 for (count = 0; count < nargs; count++)
4617 rtx reg = argvec[count].reg;
4618 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4619 use_group_regs (&call_fusage, reg);
4620 else if (reg != 0)
4622 int partial = argvec[count].partial;
4623 if (partial)
4625 int nregs;
4626 gcc_assert (partial % UNITS_PER_WORD == 0);
4627 nregs = partial / UNITS_PER_WORD;
4628 use_regs (&call_fusage, REGNO (reg), nregs);
4630 else
4631 use_reg (&call_fusage, reg);
4635 /* Pass the function the address in which to return a structure value. */
4636 if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
4638 emit_move_insn (struct_value,
4639 force_reg (Pmode,
4640 force_operand (XEXP (mem_value, 0),
4641 NULL_RTX)));
4642 if (REG_P (struct_value))
4643 use_reg (&call_fusage, struct_value);
4646 /* Don't allow popping to be deferred, since then
4647 cse'ing of library calls could delete a call and leave the pop. */
4648 NO_DEFER_POP;
4649 valreg = (mem_value == 0 && outmode != VOIDmode
4650 ? hard_libcall_value (outmode, orgfun) : NULL_RTX);
4652 /* Stack must be properly aligned now. */
4653 gcc_assert (multiple_p (stack_pointer_delta,
4654 PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT));
4656 before_call = get_last_insn ();
4658 if (flag_callgraph_info)
4659 record_final_call (SYMBOL_REF_DECL (orgfun), UNKNOWN_LOCATION);
4661 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
4662 will set inhibit_defer_pop to that value. */
4663 /* The return type is needed to decide how many bytes the function pops.
4664 Signedness plays no role in that, so for simplicity, we pretend it's
4665 always signed. We also assume that the list of arguments passed has
4666 no impact, so we pretend it is unknown. */
4668 emit_call_1 (fun, NULL,
4669 get_identifier (XSTR (orgfun, 0)),
4670 build_function_type (tfom, NULL_TREE),
4671 original_args_size.constant, args_size.constant,
4672 struct_value_size,
4673 targetm.calls.function_arg (args_so_far,
4674 function_arg_info::end_marker ()),
4675 valreg,
4676 old_inhibit_defer_pop + 1, call_fusage, flags, args_so_far);
4678 if (flag_ipa_ra)
4680 rtx datum = orgfun;
4681 gcc_assert (GET_CODE (datum) == SYMBOL_REF);
4682 rtx_call_insn *last = last_call_insn ();
4683 add_reg_note (last, REG_CALL_DECL, datum);
4686 /* Right-shift returned value if necessary. */
4687 if (!pcc_struct_value
4688 && TYPE_MODE (tfom) != BLKmode
4689 && targetm.calls.return_in_msb (tfom))
4691 shift_return_value (TYPE_MODE (tfom), false, valreg);
4692 valreg = gen_rtx_REG (TYPE_MODE (tfom), REGNO (valreg));
4695 targetm.calls.end_call_args ();
4697 /* For calls to `setjmp', etc., inform function.cc:setjmp_warnings
4698 that it should complain if nonvolatile values are live. For
4699 functions that cannot return, inform flow that control does not
4700 fall through. */
4701 if (flags & ECF_NORETURN)
4703 /* The barrier note must be emitted
4704 immediately after the CALL_INSN. Some ports emit more than
4705 just a CALL_INSN above, so we must search for it here. */
4706 rtx_insn *last = get_last_insn ();
4707 while (!CALL_P (last))
4709 last = PREV_INSN (last);
4710 /* There was no CALL_INSN? */
4711 gcc_assert (last != before_call);
4714 emit_barrier_after (last);
4717 /* Consider that "regular" libcalls, i.e. all of them except for LCT_THROW
4718 and LCT_RETURNS_TWICE, cannot perform non-local gotos. */
4719 if (flags & ECF_NOTHROW)
4721 rtx_insn *last = get_last_insn ();
4722 while (!CALL_P (last))
4724 last = PREV_INSN (last);
4725 /* There was no CALL_INSN? */
4726 gcc_assert (last != before_call);
4729 make_reg_eh_region_note_nothrow_nononlocal (last);
4732 /* Now restore inhibit_defer_pop to its actual original value. */
4733 OK_DEFER_POP;
4735 pop_temp_slots ();
4737 /* Copy the value to the right place. */
4738 if (outmode != VOIDmode && retval)
4740 if (mem_value)
4742 if (value == 0)
4743 value = mem_value;
4744 if (value != mem_value)
4745 emit_move_insn (value, mem_value);
4747 else if (GET_CODE (valreg) == PARALLEL)
4749 if (value == 0)
4750 value = gen_reg_rtx (outmode);
4751 emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
4753 else
4755 /* Convert to the proper mode if a promotion has been active. */
4756 if (GET_MODE (valreg) != outmode)
4758 int unsignedp = TYPE_UNSIGNED (tfom);
4760 gcc_assert (promote_function_mode (tfom, outmode, &unsignedp,
4761 fndecl ? TREE_TYPE (fndecl) : fntype, 1)
4762 == GET_MODE (valreg));
4763 valreg = convert_modes (outmode, GET_MODE (valreg), valreg, 0);
4766 if (value != 0)
4767 emit_move_insn (value, valreg);
4768 else
4769 value = valreg;
4773 if (ACCUMULATE_OUTGOING_ARGS)
4775 #ifdef REG_PARM_STACK_SPACE
4776 if (save_area)
4777 restore_fixed_argument_area (save_area, argblock,
4778 high_to_save, low_to_save);
4779 #endif
4781 /* If we saved any argument areas, restore them. */
4782 for (count = 0; count < nargs; count++)
4783 if (argvec[count].save_area)
4785 machine_mode save_mode = GET_MODE (argvec[count].save_area);
4786 rtx adr = plus_constant (Pmode, argblock,
4787 argvec[count].locate.offset.constant);
4788 rtx stack_area = gen_rtx_MEM (save_mode,
4789 memory_address (save_mode, adr));
4791 if (save_mode == BLKmode)
4792 emit_block_move (stack_area,
4793 validize_mem
4794 (copy_rtx (argvec[count].save_area)),
4795 (gen_int_mode
4796 (argvec[count].locate.size.constant, Pmode)),
4797 BLOCK_OP_CALL_PARM);
4798 else
4799 emit_move_insn (stack_area, argvec[count].save_area);
4802 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
4803 stack_usage_map = initial_stack_usage_map;
4804 stack_usage_watermark = initial_stack_usage_watermark;
4807 free (stack_usage_map_buf);
4809 return value;
4814 /* Store a single argument for a function call
4815 into the register or memory area where it must be passed.
4816 *ARG describes the argument value and where to pass it.
4818 ARGBLOCK is the address of the stack-block for all the arguments,
4819 or 0 on a machine where arguments are pushed individually.
4821 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
4822 so must be careful about how the stack is used.
4824 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
4825 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
4826 that we need not worry about saving and restoring the stack.
4828 FNDECL is the declaration of the function we are calling.
4830 Return nonzero if this arg should cause sibcall failure,
4831 zero otherwise. */
4833 static int
4834 store_one_arg (struct arg_data *arg, rtx argblock, int flags,
4835 int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
4837 tree pval = arg->tree_value;
4838 rtx reg = 0;
4839 int partial = 0;
4840 poly_int64 used = 0;
4841 poly_int64 lower_bound = 0, upper_bound = 0;
4842 int sibcall_failure = 0;
4844 if (TREE_CODE (pval) == ERROR_MARK)
4845 return 1;
4847 /* Push a new temporary level for any temporaries we make for
4848 this argument. */
4849 push_temp_slots ();
4851 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
4853 /* If this is being stored into a pre-allocated, fixed-size, stack area,
4854 save any previous data at that location. */
4855 if (argblock && ! variable_size && arg->stack)
4857 if (ARGS_GROW_DOWNWARD)
4859 /* stack_slot is negative, but we want to index stack_usage_map
4860 with positive values. */
4861 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4863 rtx offset = XEXP (XEXP (arg->stack_slot, 0), 1);
4864 upper_bound = -rtx_to_poly_int64 (offset) + 1;
4866 else
4867 upper_bound = 0;
4869 lower_bound = upper_bound - arg->locate.size.constant;
4871 else
4873 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4875 rtx offset = XEXP (XEXP (arg->stack_slot, 0), 1);
4876 lower_bound = rtx_to_poly_int64 (offset);
4878 else
4879 lower_bound = 0;
4881 upper_bound = lower_bound + arg->locate.size.constant;
4884 if (stack_region_maybe_used_p (lower_bound, upper_bound,
4885 reg_parm_stack_space))
4887 /* We need to make a save area. */
4888 poly_uint64 size = arg->locate.size.constant * BITS_PER_UNIT;
4889 machine_mode save_mode
4890 = int_mode_for_size (size, 1).else_blk ();
4891 rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
4892 rtx stack_area = gen_rtx_MEM (save_mode, adr);
4894 if (save_mode == BLKmode)
4896 arg->save_area
4897 = assign_temp (TREE_TYPE (arg->tree_value), 1, 1);
4898 preserve_temp_slots (arg->save_area);
4899 emit_block_move (validize_mem (copy_rtx (arg->save_area)),
4900 stack_area,
4901 (gen_int_mode
4902 (arg->locate.size.constant, Pmode)),
4903 BLOCK_OP_CALL_PARM);
4905 else
4907 arg->save_area = gen_reg_rtx (save_mode);
4908 emit_move_insn (arg->save_area, stack_area);
4914 /* If this isn't going to be placed on both the stack and in registers,
4915 set up the register and number of words. */
4916 if (! arg->pass_on_stack)
4918 if (flags & ECF_SIBCALL)
4919 reg = arg->tail_call_reg;
4920 else
4921 reg = arg->reg;
4922 partial = arg->partial;
4925 /* Being passed entirely in a register. We shouldn't be called in
4926 this case. */
4927 gcc_assert (reg == 0 || partial != 0);
4929 /* If this arg needs special alignment, don't load the registers
4930 here. */
4931 if (arg->n_aligned_regs != 0)
4932 reg = 0;
4934 /* If this is being passed partially in a register, we can't evaluate
4935 it directly into its stack slot. Otherwise, we can. */
4936 if (arg->value == 0)
4938 /* stack_arg_under_construction is nonzero if a function argument is
4939 being evaluated directly into the outgoing argument list and
4940 expand_call must take special action to preserve the argument list
4941 if it is called recursively.
4943 For scalar function arguments stack_usage_map is sufficient to
4944 determine which stack slots must be saved and restored. Scalar
4945 arguments in general have pass_on_stack == 0.
4947 If this argument is initialized by a function which takes the
4948 address of the argument (a C++ constructor or a C function
4949 returning a BLKmode structure), then stack_usage_map is
4950 insufficient and expand_call must push the stack around the
4951 function call. Such arguments have pass_on_stack == 1.
4953 Note that it is always safe to set stack_arg_under_construction,
4954 but this generates suboptimal code if set when not needed. */
4956 if (arg->pass_on_stack)
4957 stack_arg_under_construction++;
4959 arg->value = expand_expr (pval,
4960 (partial
4961 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
4962 ? NULL_RTX : arg->stack,
4963 VOIDmode, EXPAND_STACK_PARM);
4965 /* If we are promoting object (or for any other reason) the mode
4966 doesn't agree, convert the mode. */
4968 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
4969 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
4970 arg->value, arg->unsignedp);
4972 if (arg->pass_on_stack)
4973 stack_arg_under_construction--;
4976 /* Check for overlap with already clobbered argument area. */
4977 if ((flags & ECF_SIBCALL)
4978 && MEM_P (arg->value)
4979 && mem_might_overlap_already_clobbered_arg_p (XEXP (arg->value, 0),
4980 arg->locate.size.constant))
4981 sibcall_failure = 1;
4983 /* Don't allow anything left on stack from computation
4984 of argument to alloca. */
4985 if (flags & ECF_MAY_BE_ALLOCA)
4986 do_pending_stack_adjust ();
4988 if (arg->value == arg->stack)
4989 /* If the value is already in the stack slot, we are done. */
4991 else if (arg->mode != BLKmode)
4993 unsigned int parm_align;
4995 /* Argument is a scalar, not entirely passed in registers.
4996 (If part is passed in registers, arg->partial says how much
4997 and emit_push_insn will take care of putting it there.)
4999 Push it, and if its size is less than the
5000 amount of space allocated to it,
5001 also bump stack pointer by the additional space.
5002 Note that in C the default argument promotions
5003 will prevent such mismatches. */
5005 poly_int64 size = (TYPE_EMPTY_P (TREE_TYPE (pval))
5006 ? 0 : GET_MODE_SIZE (arg->mode));
5008 /* Compute how much space the push instruction will push.
5009 On many machines, pushing a byte will advance the stack
5010 pointer by a halfword. */
5011 #ifdef PUSH_ROUNDING
5012 size = PUSH_ROUNDING (size);
5013 #endif
5014 used = size;
5016 /* Compute how much space the argument should get:
5017 round up to a multiple of the alignment for arguments. */
5018 if (targetm.calls.function_arg_padding (arg->mode, TREE_TYPE (pval))
5019 != PAD_NONE)
5020 /* At the moment we don't (need to) support ABIs for which the
5021 padding isn't known at compile time. In principle it should
5022 be easy to add though. */
5023 used = force_align_up (size, PARM_BOUNDARY / BITS_PER_UNIT);
5025 /* Compute the alignment of the pushed argument. */
5026 parm_align = arg->locate.boundary;
5027 if (targetm.calls.function_arg_padding (arg->mode, TREE_TYPE (pval))
5028 == PAD_DOWNWARD)
5030 poly_int64 pad = used - size;
5031 unsigned int pad_align = known_alignment (pad) * BITS_PER_UNIT;
5032 if (pad_align != 0)
5033 parm_align = MIN (parm_align, pad_align);
5036 /* This isn't already where we want it on the stack, so put it there.
5037 This can either be done with push or copy insns. */
5038 if (maybe_ne (used, 0)
5039 && !emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval),
5040 NULL_RTX, parm_align, partial, reg, used - size,
5041 argblock, ARGS_SIZE_RTX (arg->locate.offset),
5042 reg_parm_stack_space,
5043 ARGS_SIZE_RTX (arg->locate.alignment_pad), true))
5044 sibcall_failure = 1;
5046 /* Unless this is a partially-in-register argument, the argument is now
5047 in the stack. */
5048 if (partial == 0)
5049 arg->value = arg->stack;
5051 else
5053 /* BLKmode, at least partly to be pushed. */
5055 unsigned int parm_align;
5056 poly_int64 excess;
5057 rtx size_rtx;
5059 /* Pushing a nonscalar.
5060 If part is passed in registers, PARTIAL says how much
5061 and emit_push_insn will take care of putting it there. */
5063 /* Round its size up to a multiple
5064 of the allocation unit for arguments. */
5066 if (arg->locate.size.var != 0)
5068 excess = 0;
5069 size_rtx = ARGS_SIZE_RTX (arg->locate.size);
5071 else
5073 /* PUSH_ROUNDING has no effect on us, because emit_push_insn
5074 for BLKmode is careful to avoid it. */
5075 excess = (arg->locate.size.constant
5076 - arg_int_size_in_bytes (TREE_TYPE (pval))
5077 + partial);
5078 size_rtx = expand_expr (arg_size_in_bytes (TREE_TYPE (pval)),
5079 NULL_RTX, TYPE_MODE (sizetype),
5080 EXPAND_NORMAL);
5083 parm_align = arg->locate.boundary;
5085 /* When an argument is padded down, the block is aligned to
5086 PARM_BOUNDARY, but the actual argument isn't. */
5087 if (targetm.calls.function_arg_padding (arg->mode, TREE_TYPE (pval))
5088 == PAD_DOWNWARD)
5090 if (arg->locate.size.var)
5091 parm_align = BITS_PER_UNIT;
5092 else
5094 unsigned int excess_align
5095 = known_alignment (excess) * BITS_PER_UNIT;
5096 if (excess_align != 0)
5097 parm_align = MIN (parm_align, excess_align);
5101 if ((flags & ECF_SIBCALL) && MEM_P (arg->value))
5103 /* emit_push_insn might not work properly if arg->value and
5104 argblock + arg->locate.offset areas overlap. */
5105 rtx x = arg->value;
5106 poly_int64 i = 0;
5108 if (strip_offset (XEXP (x, 0), &i)
5109 == crtl->args.internal_arg_pointer)
5111 /* arg.locate doesn't contain the pretend_args_size offset,
5112 it's part of argblock. Ensure we don't count it in I. */
5113 if (STACK_GROWS_DOWNWARD)
5114 i -= crtl->args.pretend_args_size;
5115 else
5116 i += crtl->args.pretend_args_size;
5118 /* expand_call should ensure this. */
5119 gcc_assert (!arg->locate.offset.var
5120 && arg->locate.size.var == 0);
5121 poly_int64 size_val = rtx_to_poly_int64 (size_rtx);
5123 if (known_eq (arg->locate.offset.constant, i))
5125 /* Even though they appear to be at the same location,
5126 if part of the outgoing argument is in registers,
5127 they aren't really at the same location. Check for
5128 this by making sure that the incoming size is the
5129 same as the outgoing size. */
5130 if (maybe_ne (arg->locate.size.constant, size_val))
5131 sibcall_failure = 1;
5133 else if (maybe_in_range_p (arg->locate.offset.constant,
5134 i, size_val))
5135 sibcall_failure = 1;
5136 /* Use arg->locate.size.constant instead of size_rtx
5137 because we only care about the part of the argument
5138 on the stack. */
5139 else if (maybe_in_range_p (i, arg->locate.offset.constant,
5140 arg->locate.size.constant))
5141 sibcall_failure = 1;
5145 if (!CONST_INT_P (size_rtx) || INTVAL (size_rtx) != 0)
5146 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
5147 parm_align, partial, reg, excess, argblock,
5148 ARGS_SIZE_RTX (arg->locate.offset),
5149 reg_parm_stack_space,
5150 ARGS_SIZE_RTX (arg->locate.alignment_pad), false);
5151 /* If we bypass emit_push_insn because it is a zero sized argument,
5152 we still might need to adjust stack if such argument requires
5153 extra alignment. See PR104558. */
5154 else if ((arg->locate.alignment_pad.var
5155 || maybe_ne (arg->locate.alignment_pad.constant, 0))
5156 && !argblock)
5157 anti_adjust_stack (ARGS_SIZE_RTX (arg->locate.alignment_pad));
5159 /* Unless this is a partially-in-register argument, the argument is now
5160 in the stack.
5162 ??? Unlike the case above, in which we want the actual
5163 address of the data, so that we can load it directly into a
5164 register, here we want the address of the stack slot, so that
5165 it's properly aligned for word-by-word copying or something
5166 like that. It's not clear that this is always correct. */
5167 if (partial == 0)
5168 arg->value = arg->stack_slot;
5171 if (arg->reg && GET_CODE (arg->reg) == PARALLEL)
5173 tree type = TREE_TYPE (arg->tree_value);
5174 arg->parallel_value
5175 = emit_group_load_into_temps (arg->reg, arg->value, type,
5176 int_size_in_bytes (type));
5179 /* Mark all slots this store used. */
5180 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
5181 && argblock && ! variable_size && arg->stack)
5182 mark_stack_region_used (lower_bound, upper_bound);
5184 /* Once we have pushed something, pops can't safely
5185 be deferred during the rest of the arguments. */
5186 NO_DEFER_POP;
5188 /* Free any temporary slots made in processing this argument. */
5189 pop_temp_slots ();
5191 return sibcall_failure;
5194 /* Nonzero if we do not know how to pass ARG solely in registers. */
5196 bool
5197 must_pass_in_stack_var_size (const function_arg_info &arg)
5199 if (!arg.type)
5200 return false;
5202 /* If the type has variable size... */
5203 if (!poly_int_tree_p (TYPE_SIZE (arg.type)))
5204 return true;
5206 /* If the type is marked as addressable (it is required
5207 to be constructed into the stack)... */
5208 if (TREE_ADDRESSABLE (arg.type))
5209 return true;
5211 return false;
5214 /* Another version of the TARGET_MUST_PASS_IN_STACK hook. This one
5215 takes trailing padding of a structure into account. */
5216 /* ??? Should be able to merge these two by examining BLOCK_REG_PADDING. */
5218 bool
5219 must_pass_in_stack_var_size_or_pad (const function_arg_info &arg)
5221 if (!arg.type)
5222 return false;
5224 /* If the type has variable size... */
5225 if (TREE_CODE (TYPE_SIZE (arg.type)) != INTEGER_CST)
5226 return true;
5228 /* If the type is marked as addressable (it is required
5229 to be constructed into the stack)... */
5230 if (TREE_ADDRESSABLE (arg.type))
5231 return true;
5233 if (TYPE_EMPTY_P (arg.type))
5234 return false;
5236 /* If the padding and mode of the type is such that a copy into
5237 a register would put it into the wrong part of the register. */
5238 if (arg.mode == BLKmode
5239 && int_size_in_bytes (arg.type) % (PARM_BOUNDARY / BITS_PER_UNIT)
5240 && (targetm.calls.function_arg_padding (arg.mode, arg.type)
5241 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)))
5242 return true;
5244 return false;
5247 /* Return true if TYPE must be passed on the stack when passed to
5248 the "..." arguments of a function. */
5250 bool
5251 must_pass_va_arg_in_stack (tree type)
5253 function_arg_info arg (type, /*named=*/false);
5254 return targetm.calls.must_pass_in_stack (arg);
5257 /* Return true if FIELD is the C++17 empty base field that should
5258 be ignored for ABI calling convention decisions in order to
5259 maintain ABI compatibility between C++14 and earlier, which doesn't
5260 add this FIELD to classes with empty bases, and C++17 and later
5261 which does. */
5263 bool
5264 cxx17_empty_base_field_p (const_tree field)
5266 return (DECL_FIELD_ABI_IGNORED (field)
5267 && DECL_ARTIFICIAL (field)
5268 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
5269 && !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (field)));