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