* gcc-interface/decl.c (gnat_to_gnu_entity) <E_Variable>: Translate
[official-gcc.git] / gcc / calls.c
blob530e7bf219dfff6813ca7119ab3adcb21f0e894c
1 /* Convert function calls to rtl insns, for GNU C compiler.
2 Copyright (C) 1989-2016 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-chkp.h"
51 #include "rtl-chkp.h"
54 /* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits. */
55 #define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
57 /* Data structure and subroutines used within expand_call. */
59 struct arg_data
61 /* Tree node for this argument. */
62 tree tree_value;
63 /* Mode for value; TYPE_MODE unless promoted. */
64 machine_mode mode;
65 /* Current RTL value for argument, or 0 if it isn't precomputed. */
66 rtx value;
67 /* Initially-compute RTL value for argument; only for const functions. */
68 rtx initial_value;
69 /* Register to pass this argument in, 0 if passed on stack, or an
70 PARALLEL if the arg is to be copied into multiple non-contiguous
71 registers. */
72 rtx reg;
73 /* Register to pass this argument in when generating tail call sequence.
74 This is not the same register as for normal calls on machines with
75 register windows. */
76 rtx tail_call_reg;
77 /* If REG is a PARALLEL, this is a copy of VALUE pulled into the correct
78 form for emit_group_move. */
79 rtx parallel_value;
80 /* If value is passed in neither reg nor stack, this field holds a number
81 of a special slot to be used. */
82 rtx special_slot;
83 /* For pointer bounds hold an index of parm bounds are bound to. -1 if
84 there is no such pointer. */
85 int pointer_arg;
86 /* If pointer_arg refers a structure, then pointer_offset holds an offset
87 of a pointer in this structure. */
88 int pointer_offset;
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 int highest_outgoing_arg_in_use;
128 /* A bitmap of virtual-incoming stack space. Bit is set if the corresponding
129 stack location's tail call argument has been already stored into the stack.
130 This bitmap is used to prevent sibling call optimization if function tries
131 to use parent's incoming argument slots when they have been already
132 overwritten with tail call arguments. */
133 static sbitmap stored_args_map;
135 /* stack_arg_under_construction is nonzero when an argument may be
136 initialized with a constructor call (including a C function that
137 returns a BLKmode struct) and expand_call must take special action
138 to make sure the object being constructed does not overlap the
139 argument list for the constructor call. */
140 static int stack_arg_under_construction;
142 static void emit_call_1 (rtx, tree, tree, tree, HOST_WIDE_INT, HOST_WIDE_INT,
143 HOST_WIDE_INT, rtx, rtx, int, rtx, int,
144 cumulative_args_t);
145 static void precompute_register_parameters (int, struct arg_data *, int *);
146 static void store_bounds (struct arg_data *, struct arg_data *);
147 static int store_one_arg (struct arg_data *, rtx, int, int, int);
148 static void store_unaligned_arguments_into_pseudos (struct arg_data *, int);
149 static int finalize_must_preallocate (int, int, struct arg_data *,
150 struct args_size *);
151 static void precompute_arguments (int, struct arg_data *);
152 static int compute_argument_block_size (int, struct args_size *, tree, tree, int);
153 static void initialize_argument_information (int, struct arg_data *,
154 struct args_size *, int,
155 tree, tree,
156 tree, tree, cumulative_args_t, int,
157 rtx *, int *, int *, int *,
158 bool *, bool);
159 static void compute_argument_addresses (struct arg_data *, rtx, int);
160 static rtx rtx_for_function_call (tree, tree);
161 static void load_register_parameters (struct arg_data *, int, rtx *, int,
162 int, int *);
163 static rtx emit_library_call_value_1 (int, rtx, rtx, enum libcall_type,
164 machine_mode, int, va_list);
165 static int special_function_p (const_tree, int);
166 static int check_sibcall_argument_overlap_1 (rtx);
167 static int check_sibcall_argument_overlap (rtx_insn *, struct arg_data *, int);
169 static int combine_pending_stack_adjustment_and_call (int, struct args_size *,
170 unsigned int);
171 static tree split_complex_types (tree);
173 #ifdef REG_PARM_STACK_SPACE
174 static rtx save_fixed_argument_area (int, rtx, int *, int *);
175 static void restore_fixed_argument_area (rtx, rtx, int, int);
176 #endif
178 /* Force FUNEXP into a form suitable for the address of a CALL,
179 and return that as an rtx. Also load the static chain register
180 if FNDECL is a nested function.
182 CALL_FUSAGE points to a variable holding the prospective
183 CALL_INSN_FUNCTION_USAGE information. */
186 prepare_call_address (tree fndecl_or_type, rtx funexp, rtx static_chain_value,
187 rtx *call_fusage, int reg_parm_seen, int flags)
189 /* Make a valid memory address and copy constants through pseudo-regs,
190 but not for a constant address if -fno-function-cse. */
191 if (GET_CODE (funexp) != SYMBOL_REF)
193 /* If it's an indirect call by descriptor, generate code to perform
194 runtime identification of the pointer and load the descriptor. */
195 if ((flags & ECF_BY_DESCRIPTOR) && !flag_trampolines)
197 const int bit_val = targetm.calls.custom_function_descriptors;
198 rtx call_lab = gen_label_rtx ();
200 gcc_assert (fndecl_or_type && TYPE_P (fndecl_or_type));
201 fndecl_or_type
202 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
203 fndecl_or_type);
204 DECL_STATIC_CHAIN (fndecl_or_type) = 1;
205 rtx chain = targetm.calls.static_chain (fndecl_or_type, false);
207 /* Avoid long live ranges around function calls. */
208 funexp = copy_to_mode_reg (Pmode, funexp);
210 if (REG_P (chain))
211 emit_insn (gen_rtx_CLOBBER (VOIDmode, chain));
213 /* Emit the runtime identification pattern. */
214 rtx mask = gen_rtx_AND (Pmode, funexp, GEN_INT (bit_val));
215 emit_cmp_and_jump_insns (mask, const0_rtx, EQ, NULL_RTX, Pmode, 1,
216 call_lab);
218 /* Statically predict the branch to very likely taken. */
219 rtx_insn *insn = get_last_insn ();
220 if (JUMP_P (insn))
221 predict_insn_def (insn, PRED_BUILTIN_EXPECT, TAKEN);
223 /* Load the descriptor. */
224 rtx mem = gen_rtx_MEM (ptr_mode,
225 plus_constant (Pmode, funexp, - bit_val));
226 MEM_NOTRAP_P (mem) = 1;
227 mem = convert_memory_address (Pmode, mem);
228 emit_move_insn (chain, mem);
230 mem = gen_rtx_MEM (ptr_mode,
231 plus_constant (Pmode, funexp,
232 POINTER_SIZE / BITS_PER_UNIT
233 - bit_val));
234 MEM_NOTRAP_P (mem) = 1;
235 mem = convert_memory_address (Pmode, mem);
236 emit_move_insn (funexp, mem);
238 emit_label (call_lab);
240 if (REG_P (chain))
242 use_reg (call_fusage, chain);
243 STATIC_CHAIN_REG_P (chain) = 1;
246 /* Make sure we're not going to be overwritten below. */
247 gcc_assert (!static_chain_value);
250 /* If we are using registers for parameters, force the
251 function address into a register now. */
252 funexp = ((reg_parm_seen
253 && targetm.small_register_classes_for_mode_p (FUNCTION_MODE))
254 ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
255 : memory_address (FUNCTION_MODE, funexp));
257 else
259 /* funexp could be a SYMBOL_REF represents a function pointer which is
260 of ptr_mode. In this case, it should be converted into address mode
261 to be a valid address for memory rtx pattern. See PR 64971. */
262 if (GET_MODE (funexp) != Pmode)
263 funexp = convert_memory_address (Pmode, funexp);
265 if (!(flags & ECF_SIBCALL))
267 if (!NO_FUNCTION_CSE && optimize && ! flag_no_function_cse)
268 funexp = force_reg (Pmode, funexp);
272 if (static_chain_value != 0
273 && (TREE_CODE (fndecl_or_type) != FUNCTION_DECL
274 || DECL_STATIC_CHAIN (fndecl_or_type)))
276 rtx chain;
278 chain = targetm.calls.static_chain (fndecl_or_type, false);
279 static_chain_value = convert_memory_address (Pmode, static_chain_value);
281 emit_move_insn (chain, static_chain_value);
282 if (REG_P (chain))
284 use_reg (call_fusage, chain);
285 STATIC_CHAIN_REG_P (chain) = 1;
289 return funexp;
292 /* Generate instructions to call function FUNEXP,
293 and optionally pop the results.
294 The CALL_INSN is the first insn generated.
296 FNDECL is the declaration node of the function. This is given to the
297 hook TARGET_RETURN_POPS_ARGS to determine whether this function pops
298 its own args.
300 FUNTYPE is the data type of the function. This is given to the hook
301 TARGET_RETURN_POPS_ARGS to determine whether this function pops its
302 own args. We used to allow an identifier for library functions, but
303 that doesn't work when the return type is an aggregate type and the
304 calling convention says that the pointer to this aggregate is to be
305 popped by the callee.
307 STACK_SIZE is the number of bytes of arguments on the stack,
308 ROUNDED_STACK_SIZE is that number rounded up to
309 PREFERRED_STACK_BOUNDARY; zero if the size is variable. This is
310 both to put into the call insn and to generate explicit popping
311 code if necessary.
313 STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
314 It is zero if this call doesn't want a structure value.
316 NEXT_ARG_REG is the rtx that results from executing
317 targetm.calls.function_arg (&args_so_far, VOIDmode, void_type_node, true)
318 just after all the args have had their registers assigned.
319 This could be whatever you like, but normally it is the first
320 arg-register beyond those used for args in this call,
321 or 0 if all the arg-registers are used in this call.
322 It is passed on to `gen_call' so you can put this info in the call insn.
324 VALREG is a hard register in which a value is returned,
325 or 0 if the call does not return a value.
327 OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
328 the args to this call were processed.
329 We restore `inhibit_defer_pop' to that value.
331 CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
332 denote registers used by the called function. */
334 static void
335 emit_call_1 (rtx funexp, tree fntree ATTRIBUTE_UNUSED, tree fndecl ATTRIBUTE_UNUSED,
336 tree funtype ATTRIBUTE_UNUSED,
337 HOST_WIDE_INT stack_size ATTRIBUTE_UNUSED,
338 HOST_WIDE_INT rounded_stack_size,
339 HOST_WIDE_INT struct_value_size ATTRIBUTE_UNUSED,
340 rtx next_arg_reg ATTRIBUTE_UNUSED, rtx valreg,
341 int old_inhibit_defer_pop, rtx call_fusage, int ecf_flags,
342 cumulative_args_t args_so_far ATTRIBUTE_UNUSED)
344 rtx rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
345 rtx call, funmem, pat;
346 int already_popped = 0;
347 HOST_WIDE_INT n_popped = 0;
349 /* Sibling call patterns never pop arguments (no sibcall(_value)_pop
350 patterns exist). Any popping that the callee does on return will
351 be from our caller's frame rather than ours. */
352 if (!(ecf_flags & ECF_SIBCALL))
354 n_popped += targetm.calls.return_pops_args (fndecl, funtype, stack_size);
356 #ifdef CALL_POPS_ARGS
357 n_popped += CALL_POPS_ARGS (*get_cumulative_args (args_so_far));
358 #endif
361 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
362 and we don't want to load it into a register as an optimization,
363 because prepare_call_address already did it if it should be done. */
364 if (GET_CODE (funexp) != SYMBOL_REF)
365 funexp = memory_address (FUNCTION_MODE, funexp);
367 funmem = gen_rtx_MEM (FUNCTION_MODE, funexp);
368 if (fndecl && TREE_CODE (fndecl) == FUNCTION_DECL)
370 tree t = fndecl;
372 /* Although a built-in FUNCTION_DECL and its non-__builtin
373 counterpart compare equal and get a shared mem_attrs, they
374 produce different dump output in compare-debug compilations,
375 if an entry gets garbage collected in one compilation, then
376 adds a different (but equivalent) entry, while the other
377 doesn't run the garbage collector at the same spot and then
378 shares the mem_attr with the equivalent entry. */
379 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
381 tree t2 = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
382 if (t2)
383 t = t2;
386 set_mem_expr (funmem, t);
388 else if (fntree)
389 set_mem_expr (funmem, build_simple_mem_ref (CALL_EXPR_FN (fntree)));
391 if (ecf_flags & ECF_SIBCALL)
393 if (valreg)
394 pat = targetm.gen_sibcall_value (valreg, funmem,
395 rounded_stack_size_rtx,
396 next_arg_reg, NULL_RTX);
397 else
398 pat = targetm.gen_sibcall (funmem, rounded_stack_size_rtx,
399 next_arg_reg, GEN_INT (struct_value_size));
401 /* If the target has "call" or "call_value" insns, then prefer them
402 if no arguments are actually popped. If the target does not have
403 "call" or "call_value" insns, then we must use the popping versions
404 even if the call has no arguments to pop. */
405 else if (n_popped > 0
406 || !(valreg
407 ? targetm.have_call_value ()
408 : targetm.have_call ()))
410 rtx n_pop = GEN_INT (n_popped);
412 /* If this subroutine pops its own args, record that in the call insn
413 if possible, for the sake of frame pointer elimination. */
415 if (valreg)
416 pat = targetm.gen_call_value_pop (valreg, funmem,
417 rounded_stack_size_rtx,
418 next_arg_reg, n_pop);
419 else
420 pat = targetm.gen_call_pop (funmem, rounded_stack_size_rtx,
421 next_arg_reg, n_pop);
423 already_popped = 1;
425 else
427 if (valreg)
428 pat = targetm.gen_call_value (valreg, funmem, rounded_stack_size_rtx,
429 next_arg_reg, NULL_RTX);
430 else
431 pat = targetm.gen_call (funmem, rounded_stack_size_rtx, next_arg_reg,
432 GEN_INT (struct_value_size));
434 emit_insn (pat);
436 /* Find the call we just emitted. */
437 rtx_call_insn *call_insn = last_call_insn ();
439 /* Some target create a fresh MEM instead of reusing the one provided
440 above. Set its MEM_EXPR. */
441 call = get_call_rtx_from (call_insn);
442 if (call
443 && MEM_EXPR (XEXP (call, 0)) == NULL_TREE
444 && MEM_EXPR (funmem) != NULL_TREE)
445 set_mem_expr (XEXP (call, 0), MEM_EXPR (funmem));
447 /* Mark instrumented calls. */
448 if (call && fntree)
449 CALL_EXPR_WITH_BOUNDS_P (call) = CALL_WITH_BOUNDS_P (fntree);
451 /* Put the register usage information there. */
452 add_function_usage_to (call_insn, call_fusage);
454 /* If this is a const call, then set the insn's unchanging bit. */
455 if (ecf_flags & ECF_CONST)
456 RTL_CONST_CALL_P (call_insn) = 1;
458 /* If this is a pure call, then set the insn's unchanging bit. */
459 if (ecf_flags & ECF_PURE)
460 RTL_PURE_CALL_P (call_insn) = 1;
462 /* If this is a const call, then set the insn's unchanging bit. */
463 if (ecf_flags & ECF_LOOPING_CONST_OR_PURE)
464 RTL_LOOPING_CONST_OR_PURE_CALL_P (call_insn) = 1;
466 /* Create a nothrow REG_EH_REGION note, if needed. */
467 make_reg_eh_region_note (call_insn, ecf_flags, 0);
469 if (ecf_flags & ECF_NORETURN)
470 add_reg_note (call_insn, REG_NORETURN, const0_rtx);
472 if (ecf_flags & ECF_RETURNS_TWICE)
474 add_reg_note (call_insn, REG_SETJMP, const0_rtx);
475 cfun->calls_setjmp = 1;
478 SIBLING_CALL_P (call_insn) = ((ecf_flags & ECF_SIBCALL) != 0);
480 /* Restore this now, so that we do defer pops for this call's args
481 if the context of the call as a whole permits. */
482 inhibit_defer_pop = old_inhibit_defer_pop;
484 if (n_popped > 0)
486 if (!already_popped)
487 CALL_INSN_FUNCTION_USAGE (call_insn)
488 = gen_rtx_EXPR_LIST (VOIDmode,
489 gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx),
490 CALL_INSN_FUNCTION_USAGE (call_insn));
491 rounded_stack_size -= n_popped;
492 rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
493 stack_pointer_delta -= n_popped;
495 add_reg_note (call_insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
497 /* If popup is needed, stack realign must use DRAP */
498 if (SUPPORTS_STACK_ALIGNMENT)
499 crtl->need_drap = true;
501 /* For noreturn calls when not accumulating outgoing args force
502 REG_ARGS_SIZE note to prevent crossjumping of calls with different
503 args sizes. */
504 else if (!ACCUMULATE_OUTGOING_ARGS && (ecf_flags & ECF_NORETURN) != 0)
505 add_reg_note (call_insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
507 if (!ACCUMULATE_OUTGOING_ARGS)
509 /* If returning from the subroutine does not automatically pop the args,
510 we need an instruction to pop them sooner or later.
511 Perhaps do it now; perhaps just record how much space to pop later.
513 If returning from the subroutine does pop the args, indicate that the
514 stack pointer will be changed. */
516 if (rounded_stack_size != 0)
518 if (ecf_flags & ECF_NORETURN)
519 /* Just pretend we did the pop. */
520 stack_pointer_delta -= rounded_stack_size;
521 else if (flag_defer_pop && inhibit_defer_pop == 0
522 && ! (ecf_flags & (ECF_CONST | ECF_PURE)))
523 pending_stack_adjust += rounded_stack_size;
524 else
525 adjust_stack (rounded_stack_size_rtx);
528 /* When we accumulate outgoing args, we must avoid any stack manipulations.
529 Restore the stack pointer to its original value now. Usually
530 ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
531 On i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
532 popping variants of functions exist as well.
534 ??? We may optimize similar to defer_pop above, but it is
535 probably not worthwhile.
537 ??? It will be worthwhile to enable combine_stack_adjustments even for
538 such machines. */
539 else if (n_popped)
540 anti_adjust_stack (GEN_INT (n_popped));
543 /* Determine if the function identified by FNDECL is one with
544 special properties we wish to know about. Modify FLAGS accordingly.
546 For example, if the function might return more than one time (setjmp), then
547 set ECF_RETURNS_TWICE.
549 Set ECF_MAY_BE_ALLOCA for any memory allocation function that might allocate
550 space from the stack such as alloca. */
552 static int
553 special_function_p (const_tree fndecl, int flags)
555 tree name_decl = DECL_NAME (fndecl);
557 /* For instrumentation clones we want to derive flags
558 from the original name. */
559 if (cgraph_node::get (fndecl)
560 && cgraph_node::get (fndecl)->instrumentation_clone)
561 name_decl = DECL_NAME (cgraph_node::get (fndecl)->orig_decl);
563 if (fndecl && name_decl
564 && IDENTIFIER_LENGTH (name_decl) <= 11
565 /* Exclude functions not at the file scope, or not `extern',
566 since they are not the magic functions we would otherwise
567 think they are.
568 FIXME: this should be handled with attributes, not with this
569 hacky imitation of DECL_ASSEMBLER_NAME. It's (also) wrong
570 because you can declare fork() inside a function if you
571 wish. */
572 && (DECL_CONTEXT (fndecl) == NULL_TREE
573 || TREE_CODE (DECL_CONTEXT (fndecl)) == TRANSLATION_UNIT_DECL)
574 && TREE_PUBLIC (fndecl))
576 const char *name = IDENTIFIER_POINTER (name_decl);
577 const char *tname = name;
579 /* We assume that alloca will always be called by name. It
580 makes no sense to pass it as a pointer-to-function to
581 anything that does not understand its behavior. */
582 if (IDENTIFIER_LENGTH (name_decl) == 6
583 && name[0] == 'a'
584 && ! strcmp (name, "alloca"))
585 flags |= ECF_MAY_BE_ALLOCA;
587 /* Disregard prefix _ or __. */
588 if (name[0] == '_')
590 if (name[1] == '_')
591 tname += 2;
592 else
593 tname += 1;
596 /* ECF_RETURNS_TWICE is safe even for -ffreestanding. */
597 if (! strcmp (tname, "setjmp")
598 || ! strcmp (tname, "sigsetjmp")
599 || ! strcmp (name, "savectx")
600 || ! strcmp (name, "vfork")
601 || ! strcmp (name, "getcontext"))
602 flags |= ECF_RETURNS_TWICE;
605 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
606 switch (DECL_FUNCTION_CODE (fndecl))
608 case BUILT_IN_ALLOCA:
609 case BUILT_IN_ALLOCA_WITH_ALIGN:
610 flags |= ECF_MAY_BE_ALLOCA;
611 break;
612 default:
613 break;
616 return flags;
619 /* Similar to special_function_p; return a set of ERF_ flags for the
620 function FNDECL. */
621 static int
622 decl_return_flags (tree fndecl)
624 tree attr;
625 tree type = TREE_TYPE (fndecl);
626 if (!type)
627 return 0;
629 attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
630 if (!attr)
631 return 0;
633 attr = TREE_VALUE (TREE_VALUE (attr));
634 if (!attr || TREE_STRING_LENGTH (attr) < 1)
635 return 0;
637 switch (TREE_STRING_POINTER (attr)[0])
639 case '1':
640 case '2':
641 case '3':
642 case '4':
643 return ERF_RETURNS_ARG | (TREE_STRING_POINTER (attr)[0] - '1');
645 case 'm':
646 return ERF_NOALIAS;
648 case '.':
649 default:
650 return 0;
654 /* Return nonzero when FNDECL represents a call to setjmp. */
657 setjmp_call_p (const_tree fndecl)
659 if (DECL_IS_RETURNS_TWICE (fndecl))
660 return ECF_RETURNS_TWICE;
661 return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
665 /* Return true if STMT may be an alloca call. */
667 bool
668 gimple_maybe_alloca_call_p (const gimple *stmt)
670 tree fndecl;
672 if (!is_gimple_call (stmt))
673 return false;
675 fndecl = gimple_call_fndecl (stmt);
676 if (fndecl && (special_function_p (fndecl, 0) & ECF_MAY_BE_ALLOCA))
677 return true;
679 return false;
682 /* Return true if STMT is a builtin alloca call. */
684 bool
685 gimple_alloca_call_p (const gimple *stmt)
687 tree fndecl;
689 if (!is_gimple_call (stmt))
690 return false;
692 fndecl = gimple_call_fndecl (stmt);
693 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
694 switch (DECL_FUNCTION_CODE (fndecl))
696 case BUILT_IN_ALLOCA:
697 case BUILT_IN_ALLOCA_WITH_ALIGN:
698 return true;
699 default:
700 break;
703 return false;
706 /* Return true when exp contains a builtin alloca call. */
708 bool
709 alloca_call_p (const_tree exp)
711 tree fndecl;
712 if (TREE_CODE (exp) == CALL_EXPR
713 && (fndecl = get_callee_fndecl (exp))
714 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
715 switch (DECL_FUNCTION_CODE (fndecl))
717 case BUILT_IN_ALLOCA:
718 case BUILT_IN_ALLOCA_WITH_ALIGN:
719 return true;
720 default:
721 break;
724 return false;
727 /* Return TRUE if FNDECL is either a TM builtin or a TM cloned
728 function. Return FALSE otherwise. */
730 static bool
731 is_tm_builtin (const_tree fndecl)
733 if (fndecl == NULL)
734 return false;
736 if (decl_is_tm_clone (fndecl))
737 return true;
739 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
741 switch (DECL_FUNCTION_CODE (fndecl))
743 case BUILT_IN_TM_COMMIT:
744 case BUILT_IN_TM_COMMIT_EH:
745 case BUILT_IN_TM_ABORT:
746 case BUILT_IN_TM_IRREVOCABLE:
747 case BUILT_IN_TM_GETTMCLONE_IRR:
748 case BUILT_IN_TM_MEMCPY:
749 case BUILT_IN_TM_MEMMOVE:
750 case BUILT_IN_TM_MEMSET:
751 CASE_BUILT_IN_TM_STORE (1):
752 CASE_BUILT_IN_TM_STORE (2):
753 CASE_BUILT_IN_TM_STORE (4):
754 CASE_BUILT_IN_TM_STORE (8):
755 CASE_BUILT_IN_TM_STORE (FLOAT):
756 CASE_BUILT_IN_TM_STORE (DOUBLE):
757 CASE_BUILT_IN_TM_STORE (LDOUBLE):
758 CASE_BUILT_IN_TM_STORE (M64):
759 CASE_BUILT_IN_TM_STORE (M128):
760 CASE_BUILT_IN_TM_STORE (M256):
761 CASE_BUILT_IN_TM_LOAD (1):
762 CASE_BUILT_IN_TM_LOAD (2):
763 CASE_BUILT_IN_TM_LOAD (4):
764 CASE_BUILT_IN_TM_LOAD (8):
765 CASE_BUILT_IN_TM_LOAD (FLOAT):
766 CASE_BUILT_IN_TM_LOAD (DOUBLE):
767 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
768 CASE_BUILT_IN_TM_LOAD (M64):
769 CASE_BUILT_IN_TM_LOAD (M128):
770 CASE_BUILT_IN_TM_LOAD (M256):
771 case BUILT_IN_TM_LOG:
772 case BUILT_IN_TM_LOG_1:
773 case BUILT_IN_TM_LOG_2:
774 case BUILT_IN_TM_LOG_4:
775 case BUILT_IN_TM_LOG_8:
776 case BUILT_IN_TM_LOG_FLOAT:
777 case BUILT_IN_TM_LOG_DOUBLE:
778 case BUILT_IN_TM_LOG_LDOUBLE:
779 case BUILT_IN_TM_LOG_M64:
780 case BUILT_IN_TM_LOG_M128:
781 case BUILT_IN_TM_LOG_M256:
782 return true;
783 default:
784 break;
787 return false;
790 /* Detect flags (function attributes) from the function decl or type node. */
793 flags_from_decl_or_type (const_tree exp)
795 int flags = 0;
797 if (DECL_P (exp))
799 /* The function exp may have the `malloc' attribute. */
800 if (DECL_IS_MALLOC (exp))
801 flags |= ECF_MALLOC;
803 /* The function exp may have the `returns_twice' attribute. */
804 if (DECL_IS_RETURNS_TWICE (exp))
805 flags |= ECF_RETURNS_TWICE;
807 /* Process the pure and const attributes. */
808 if (TREE_READONLY (exp))
809 flags |= ECF_CONST;
810 if (DECL_PURE_P (exp))
811 flags |= ECF_PURE;
812 if (DECL_LOOPING_CONST_OR_PURE_P (exp))
813 flags |= ECF_LOOPING_CONST_OR_PURE;
815 if (DECL_IS_NOVOPS (exp))
816 flags |= ECF_NOVOPS;
817 if (lookup_attribute ("leaf", DECL_ATTRIBUTES (exp)))
818 flags |= ECF_LEAF;
820 if (TREE_NOTHROW (exp))
821 flags |= ECF_NOTHROW;
823 if (flag_tm)
825 if (is_tm_builtin (exp))
826 flags |= ECF_TM_BUILTIN;
827 else if ((flags & (ECF_CONST|ECF_NOVOPS)) != 0
828 || lookup_attribute ("transaction_pure",
829 TYPE_ATTRIBUTES (TREE_TYPE (exp))))
830 flags |= ECF_TM_PURE;
833 flags = special_function_p (exp, flags);
835 else if (TYPE_P (exp))
837 if (TYPE_READONLY (exp))
838 flags |= ECF_CONST;
840 if (flag_tm
841 && ((flags & ECF_CONST) != 0
842 || lookup_attribute ("transaction_pure", TYPE_ATTRIBUTES (exp))))
843 flags |= ECF_TM_PURE;
845 else
846 gcc_unreachable ();
848 if (TREE_THIS_VOLATILE (exp))
850 flags |= ECF_NORETURN;
851 if (flags & (ECF_CONST|ECF_PURE))
852 flags |= ECF_LOOPING_CONST_OR_PURE;
855 return flags;
858 /* Detect flags from a CALL_EXPR. */
861 call_expr_flags (const_tree t)
863 int flags;
864 tree decl = get_callee_fndecl (t);
866 if (decl)
867 flags = flags_from_decl_or_type (decl);
868 else if (CALL_EXPR_FN (t) == NULL_TREE)
869 flags = internal_fn_flags (CALL_EXPR_IFN (t));
870 else
872 tree type = TREE_TYPE (CALL_EXPR_FN (t));
873 if (type && TREE_CODE (type) == POINTER_TYPE)
874 flags = flags_from_decl_or_type (TREE_TYPE (type));
875 else
876 flags = 0;
877 if (CALL_EXPR_BY_DESCRIPTOR (t))
878 flags |= ECF_BY_DESCRIPTOR;
881 return flags;
884 /* Return true if TYPE should be passed by invisible reference. */
886 bool
887 pass_by_reference (CUMULATIVE_ARGS *ca, machine_mode mode,
888 tree type, bool named_arg)
890 if (type)
892 /* If this type contains non-trivial constructors, then it is
893 forbidden for the middle-end to create any new copies. */
894 if (TREE_ADDRESSABLE (type))
895 return true;
897 /* GCC post 3.4 passes *all* variable sized types by reference. */
898 if (!TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
899 return true;
901 /* If a record type should be passed the same as its first (and only)
902 member, use the type and mode of that member. */
903 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
905 type = TREE_TYPE (first_field (type));
906 mode = TYPE_MODE (type);
910 return targetm.calls.pass_by_reference (pack_cumulative_args (ca), mode,
911 type, named_arg);
914 /* Return true if TYPE, which is passed by reference, should be callee
915 copied instead of caller copied. */
917 bool
918 reference_callee_copied (CUMULATIVE_ARGS *ca, machine_mode mode,
919 tree type, bool named_arg)
921 if (type && TREE_ADDRESSABLE (type))
922 return false;
923 return targetm.calls.callee_copies (pack_cumulative_args (ca), mode, type,
924 named_arg);
928 /* Precompute all register parameters as described by ARGS, storing values
929 into fields within the ARGS array.
931 NUM_ACTUALS indicates the total number elements in the ARGS array.
933 Set REG_PARM_SEEN if we encounter a register parameter. */
935 static void
936 precompute_register_parameters (int num_actuals, struct arg_data *args,
937 int *reg_parm_seen)
939 int i;
941 *reg_parm_seen = 0;
943 for (i = 0; i < num_actuals; i++)
944 if (args[i].reg != 0 && ! args[i].pass_on_stack)
946 *reg_parm_seen = 1;
948 if (args[i].value == 0)
950 push_temp_slots ();
951 args[i].value = expand_normal (args[i].tree_value);
952 preserve_temp_slots (args[i].value);
953 pop_temp_slots ();
956 /* If we are to promote the function arg to a wider mode,
957 do it now. */
959 if (args[i].mode != TYPE_MODE (TREE_TYPE (args[i].tree_value)))
960 args[i].value
961 = convert_modes (args[i].mode,
962 TYPE_MODE (TREE_TYPE (args[i].tree_value)),
963 args[i].value, args[i].unsignedp);
965 /* If the value is a non-legitimate constant, force it into a
966 pseudo now. TLS symbols sometimes need a call to resolve. */
967 if (CONSTANT_P (args[i].value)
968 && !targetm.legitimate_constant_p (args[i].mode, args[i].value))
969 args[i].value = force_reg (args[i].mode, args[i].value);
971 /* If we're going to have to load the value by parts, pull the
972 parts into pseudos. The part extraction process can involve
973 non-trivial computation. */
974 if (GET_CODE (args[i].reg) == PARALLEL)
976 tree type = TREE_TYPE (args[i].tree_value);
977 args[i].parallel_value
978 = emit_group_load_into_temps (args[i].reg, args[i].value,
979 type, int_size_in_bytes (type));
982 /* If the value is expensive, and we are inside an appropriately
983 short loop, put the value into a pseudo and then put the pseudo
984 into the hard reg.
986 For small register classes, also do this if this call uses
987 register parameters. This is to avoid reload conflicts while
988 loading the parameters registers. */
990 else if ((! (REG_P (args[i].value)
991 || (GET_CODE (args[i].value) == SUBREG
992 && REG_P (SUBREG_REG (args[i].value)))))
993 && args[i].mode != BLKmode
994 && (set_src_cost (args[i].value, args[i].mode,
995 optimize_insn_for_speed_p ())
996 > COSTS_N_INSNS (1))
997 && ((*reg_parm_seen
998 && targetm.small_register_classes_for_mode_p (args[i].mode))
999 || optimize))
1000 args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
1004 #ifdef REG_PARM_STACK_SPACE
1006 /* The argument list is the property of the called routine and it
1007 may clobber it. If the fixed area has been used for previous
1008 parameters, we must save and restore it. */
1010 static rtx
1011 save_fixed_argument_area (int reg_parm_stack_space, rtx argblock, int *low_to_save, int *high_to_save)
1013 int low;
1014 int high;
1016 /* Compute the boundary of the area that needs to be saved, if any. */
1017 high = reg_parm_stack_space;
1018 if (ARGS_GROW_DOWNWARD)
1019 high += 1;
1021 if (high > highest_outgoing_arg_in_use)
1022 high = highest_outgoing_arg_in_use;
1024 for (low = 0; low < high; low++)
1025 if (stack_usage_map[low] != 0)
1027 int num_to_save;
1028 machine_mode save_mode;
1029 int delta;
1030 rtx addr;
1031 rtx stack_area;
1032 rtx save_area;
1034 while (stack_usage_map[--high] == 0)
1037 *low_to_save = low;
1038 *high_to_save = high;
1040 num_to_save = high - low + 1;
1041 save_mode = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
1043 /* If we don't have the required alignment, must do this
1044 in BLKmode. */
1045 if ((low & (MIN (GET_MODE_SIZE (save_mode),
1046 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
1047 save_mode = BLKmode;
1049 if (ARGS_GROW_DOWNWARD)
1050 delta = -high;
1051 else
1052 delta = low;
1054 addr = plus_constant (Pmode, argblock, delta);
1055 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
1057 set_mem_align (stack_area, PARM_BOUNDARY);
1058 if (save_mode == BLKmode)
1060 save_area = assign_stack_temp (BLKmode, num_to_save);
1061 emit_block_move (validize_mem (save_area), stack_area,
1062 GEN_INT (num_to_save), BLOCK_OP_CALL_PARM);
1064 else
1066 save_area = gen_reg_rtx (save_mode);
1067 emit_move_insn (save_area, stack_area);
1070 return save_area;
1073 return NULL_RTX;
1076 static void
1077 restore_fixed_argument_area (rtx save_area, rtx argblock, int high_to_save, int low_to_save)
1079 machine_mode save_mode = GET_MODE (save_area);
1080 int delta;
1081 rtx addr, stack_area;
1083 if (ARGS_GROW_DOWNWARD)
1084 delta = -high_to_save;
1085 else
1086 delta = low_to_save;
1088 addr = plus_constant (Pmode, argblock, delta);
1089 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
1090 set_mem_align (stack_area, PARM_BOUNDARY);
1092 if (save_mode != BLKmode)
1093 emit_move_insn (stack_area, save_area);
1094 else
1095 emit_block_move (stack_area, validize_mem (save_area),
1096 GEN_INT (high_to_save - low_to_save + 1),
1097 BLOCK_OP_CALL_PARM);
1099 #endif /* REG_PARM_STACK_SPACE */
1101 /* If any elements in ARGS refer to parameters that are to be passed in
1102 registers, but not in memory, and whose alignment does not permit a
1103 direct copy into registers. Copy the values into a group of pseudos
1104 which we will later copy into the appropriate hard registers.
1106 Pseudos for each unaligned argument will be stored into the array
1107 args[argnum].aligned_regs. The caller is responsible for deallocating
1108 the aligned_regs array if it is nonzero. */
1110 static void
1111 store_unaligned_arguments_into_pseudos (struct arg_data *args, int num_actuals)
1113 int i, j;
1115 for (i = 0; i < num_actuals; i++)
1116 if (args[i].reg != 0 && ! args[i].pass_on_stack
1117 && GET_CODE (args[i].reg) != PARALLEL
1118 && args[i].mode == BLKmode
1119 && MEM_P (args[i].value)
1120 && (MEM_ALIGN (args[i].value)
1121 < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
1123 int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1124 int endian_correction = 0;
1126 if (args[i].partial)
1128 gcc_assert (args[i].partial % UNITS_PER_WORD == 0);
1129 args[i].n_aligned_regs = args[i].partial / UNITS_PER_WORD;
1131 else
1133 args[i].n_aligned_regs
1134 = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1137 args[i].aligned_regs = XNEWVEC (rtx, args[i].n_aligned_regs);
1139 /* Structures smaller than a word are normally aligned to the
1140 least significant byte. On a BYTES_BIG_ENDIAN machine,
1141 this means we must skip the empty high order bytes when
1142 calculating the bit offset. */
1143 if (bytes < UNITS_PER_WORD
1144 #ifdef BLOCK_REG_PADDING
1145 && (BLOCK_REG_PADDING (args[i].mode,
1146 TREE_TYPE (args[i].tree_value), 1)
1147 == downward)
1148 #else
1149 && BYTES_BIG_ENDIAN
1150 #endif
1152 endian_correction = BITS_PER_WORD - bytes * BITS_PER_UNIT;
1154 for (j = 0; j < args[i].n_aligned_regs; j++)
1156 rtx reg = gen_reg_rtx (word_mode);
1157 rtx word = operand_subword_force (args[i].value, j, BLKmode);
1158 int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
1160 args[i].aligned_regs[j] = reg;
1161 word = extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
1162 word_mode, word_mode, false);
1164 /* There is no need to restrict this code to loading items
1165 in TYPE_ALIGN sized hunks. The bitfield instructions can
1166 load up entire word sized registers efficiently.
1168 ??? This may not be needed anymore.
1169 We use to emit a clobber here but that doesn't let later
1170 passes optimize the instructions we emit. By storing 0 into
1171 the register later passes know the first AND to zero out the
1172 bitfield being set in the register is unnecessary. The store
1173 of 0 will be deleted as will at least the first AND. */
1175 emit_move_insn (reg, const0_rtx);
1177 bytes -= bitsize / BITS_PER_UNIT;
1178 store_bit_field (reg, bitsize, endian_correction, 0, 0,
1179 word_mode, word, false);
1184 /* Issue an error if CALL_EXPR was flagged as requiring
1185 tall-call optimization. */
1187 static void
1188 maybe_complain_about_tail_call (tree call_expr, const char *reason)
1190 gcc_assert (TREE_CODE (call_expr) == CALL_EXPR);
1191 if (!CALL_EXPR_MUST_TAIL_CALL (call_expr))
1192 return;
1194 error_at (EXPR_LOCATION (call_expr), "cannot tail-call: %s", reason);
1197 /* Fill in ARGS_SIZE and ARGS array based on the parameters found in
1198 CALL_EXPR EXP.
1200 NUM_ACTUALS is the total number of parameters.
1202 N_NAMED_ARGS is the total number of named arguments.
1204 STRUCT_VALUE_ADDR_VALUE is the implicit argument for a struct return
1205 value, or null.
1207 FNDECL is the tree code for the target of this call (if known)
1209 ARGS_SO_FAR holds state needed by the target to know where to place
1210 the next argument.
1212 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
1213 for arguments which are passed in registers.
1215 OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
1216 and may be modified by this routine.
1218 OLD_PENDING_ADJ, MUST_PREALLOCATE and FLAGS are pointers to integer
1219 flags which may be modified by this routine.
1221 MAY_TAILCALL is cleared if we encounter an invisible pass-by-reference
1222 that requires allocation of stack space.
1224 CALL_FROM_THUNK_P is true if this call is the jump from a thunk to
1225 the thunked-to function. */
1227 static void
1228 initialize_argument_information (int num_actuals ATTRIBUTE_UNUSED,
1229 struct arg_data *args,
1230 struct args_size *args_size,
1231 int n_named_args ATTRIBUTE_UNUSED,
1232 tree exp, tree struct_value_addr_value,
1233 tree fndecl, tree fntype,
1234 cumulative_args_t args_so_far,
1235 int reg_parm_stack_space,
1236 rtx *old_stack_level, int *old_pending_adj,
1237 int *must_preallocate, int *ecf_flags,
1238 bool *may_tailcall, bool call_from_thunk_p)
1240 CUMULATIVE_ARGS *args_so_far_pnt = get_cumulative_args (args_so_far);
1241 location_t loc = EXPR_LOCATION (exp);
1243 /* Count arg position in order args appear. */
1244 int argpos;
1246 int i;
1248 args_size->constant = 0;
1249 args_size->var = 0;
1251 bitmap_obstack_initialize (NULL);
1253 /* In this loop, we consider args in the order they are written.
1254 We fill up ARGS from the back. */
1256 i = num_actuals - 1;
1258 int j = i, ptr_arg = -1;
1259 call_expr_arg_iterator iter;
1260 tree arg;
1261 bitmap slots = NULL;
1263 if (struct_value_addr_value)
1265 args[j].tree_value = struct_value_addr_value;
1266 j--;
1268 /* If we pass structure address then we need to
1269 create bounds for it. Since created bounds is
1270 a call statement, we expand it right here to avoid
1271 fixing all other places where it may be expanded. */
1272 if (CALL_WITH_BOUNDS_P (exp))
1274 args[j].value = gen_reg_rtx (targetm.chkp_bound_mode ());
1275 args[j].tree_value
1276 = chkp_make_bounds_for_struct_addr (struct_value_addr_value);
1277 expand_expr_real (args[j].tree_value, args[j].value, VOIDmode,
1278 EXPAND_NORMAL, 0, false);
1279 args[j].pointer_arg = j + 1;
1280 j--;
1283 argpos = 0;
1284 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
1286 tree argtype = TREE_TYPE (arg);
1288 /* Remember last param with pointer and associate it
1289 with following pointer bounds. */
1290 if (CALL_WITH_BOUNDS_P (exp)
1291 && chkp_type_has_pointer (argtype))
1293 if (slots)
1294 BITMAP_FREE (slots);
1295 ptr_arg = j;
1296 if (!BOUNDED_TYPE_P (argtype))
1298 slots = BITMAP_ALLOC (NULL);
1299 chkp_find_bound_slots (argtype, slots);
1302 else if (CALL_WITH_BOUNDS_P (exp)
1303 && pass_by_reference (NULL, TYPE_MODE (argtype), argtype,
1304 argpos < n_named_args))
1306 if (slots)
1307 BITMAP_FREE (slots);
1308 ptr_arg = j;
1310 else if (POINTER_BOUNDS_TYPE_P (argtype))
1312 /* We expect bounds in instrumented calls only.
1313 Otherwise it is a sign we lost flag due to some optimization
1314 and may emit call args incorrectly. */
1315 gcc_assert (CALL_WITH_BOUNDS_P (exp));
1317 /* For structures look for the next available pointer. */
1318 if (ptr_arg != -1 && slots)
1320 unsigned bnd_no = bitmap_first_set_bit (slots);
1321 args[j].pointer_offset =
1322 bnd_no * POINTER_SIZE / BITS_PER_UNIT;
1324 bitmap_clear_bit (slots, bnd_no);
1326 /* Check we have no more pointers in the structure. */
1327 if (bitmap_empty_p (slots))
1328 BITMAP_FREE (slots);
1330 args[j].pointer_arg = ptr_arg;
1332 /* Check we covered all pointers in the previous
1333 non bounds arg. */
1334 if (!slots)
1335 ptr_arg = -1;
1337 else
1338 ptr_arg = -1;
1340 if (targetm.calls.split_complex_arg
1341 && argtype
1342 && TREE_CODE (argtype) == COMPLEX_TYPE
1343 && targetm.calls.split_complex_arg (argtype))
1345 tree subtype = TREE_TYPE (argtype);
1346 args[j].tree_value = build1 (REALPART_EXPR, subtype, arg);
1347 j--;
1348 args[j].tree_value = build1 (IMAGPART_EXPR, subtype, arg);
1350 else
1351 args[j].tree_value = arg;
1352 j--;
1353 argpos++;
1356 if (slots)
1357 BITMAP_FREE (slots);
1360 bitmap_obstack_release (NULL);
1362 /* I counts args in order (to be) pushed; ARGPOS counts in order written. */
1363 for (argpos = 0; argpos < num_actuals; i--, argpos++)
1365 tree type = TREE_TYPE (args[i].tree_value);
1366 int unsignedp;
1367 machine_mode mode;
1369 /* Replace erroneous argument with constant zero. */
1370 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1371 args[i].tree_value = integer_zero_node, type = integer_type_node;
1373 /* If TYPE is a transparent union or record, pass things the way
1374 we would pass the first field of the union or record. We have
1375 already verified that the modes are the same. */
1376 if ((TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == RECORD_TYPE)
1377 && TYPE_TRANSPARENT_AGGR (type))
1378 type = TREE_TYPE (first_field (type));
1380 /* Decide where to pass this arg.
1382 args[i].reg is nonzero if all or part is passed in registers.
1384 args[i].partial is nonzero if part but not all is passed in registers,
1385 and the exact value says how many bytes are passed in registers.
1387 args[i].pass_on_stack is nonzero if the argument must at least be
1388 computed on the stack. It may then be loaded back into registers
1389 if args[i].reg is nonzero.
1391 These decisions are driven by the FUNCTION_... macros and must agree
1392 with those made by function.c. */
1394 /* See if this argument should be passed by invisible reference. */
1395 if (pass_by_reference (args_so_far_pnt, TYPE_MODE (type),
1396 type, argpos < n_named_args))
1398 bool callee_copies;
1399 tree base = NULL_TREE;
1401 callee_copies
1402 = reference_callee_copied (args_so_far_pnt, TYPE_MODE (type),
1403 type, argpos < n_named_args);
1405 /* If we're compiling a thunk, pass through invisible references
1406 instead of making a copy. */
1407 if (call_from_thunk_p
1408 || (callee_copies
1409 && !TREE_ADDRESSABLE (type)
1410 && (base = get_base_address (args[i].tree_value))
1411 && TREE_CODE (base) != SSA_NAME
1412 && (!DECL_P (base) || MEM_P (DECL_RTL (base)))))
1414 /* We may have turned the parameter value into an SSA name.
1415 Go back to the original parameter so we can take the
1416 address. */
1417 if (TREE_CODE (args[i].tree_value) == SSA_NAME)
1419 gcc_assert (SSA_NAME_IS_DEFAULT_DEF (args[i].tree_value));
1420 args[i].tree_value = SSA_NAME_VAR (args[i].tree_value);
1421 gcc_assert (TREE_CODE (args[i].tree_value) == PARM_DECL);
1423 /* Argument setup code may have copied the value to register. We
1424 revert that optimization now because the tail call code must
1425 use the original location. */
1426 if (TREE_CODE (args[i].tree_value) == PARM_DECL
1427 && !MEM_P (DECL_RTL (args[i].tree_value))
1428 && DECL_INCOMING_RTL (args[i].tree_value)
1429 && MEM_P (DECL_INCOMING_RTL (args[i].tree_value)))
1430 set_decl_rtl (args[i].tree_value,
1431 DECL_INCOMING_RTL (args[i].tree_value));
1433 mark_addressable (args[i].tree_value);
1435 /* We can't use sibcalls if a callee-copied argument is
1436 stored in the current function's frame. */
1437 if (!call_from_thunk_p && DECL_P (base) && !TREE_STATIC (base))
1439 *may_tailcall = false;
1440 maybe_complain_about_tail_call (exp,
1441 "a callee-copied argument is"
1442 " stored in the current "
1443 " function's frame");
1446 args[i].tree_value = build_fold_addr_expr_loc (loc,
1447 args[i].tree_value);
1448 type = TREE_TYPE (args[i].tree_value);
1450 if (*ecf_flags & ECF_CONST)
1451 *ecf_flags &= ~(ECF_CONST | ECF_LOOPING_CONST_OR_PURE);
1453 else
1455 /* We make a copy of the object and pass the address to the
1456 function being called. */
1457 rtx copy;
1459 if (!COMPLETE_TYPE_P (type)
1460 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
1461 || (flag_stack_check == GENERIC_STACK_CHECK
1462 && compare_tree_int (TYPE_SIZE_UNIT (type),
1463 STACK_CHECK_MAX_VAR_SIZE) > 0))
1465 /* This is a variable-sized object. Make space on the stack
1466 for it. */
1467 rtx size_rtx = expr_size (args[i].tree_value);
1469 if (*old_stack_level == 0)
1471 emit_stack_save (SAVE_BLOCK, old_stack_level);
1472 *old_pending_adj = pending_stack_adjust;
1473 pending_stack_adjust = 0;
1476 /* We can pass TRUE as the 4th argument because we just
1477 saved the stack pointer and will restore it right after
1478 the call. */
1479 copy = allocate_dynamic_stack_space (size_rtx,
1480 TYPE_ALIGN (type),
1481 TYPE_ALIGN (type),
1482 true);
1483 copy = gen_rtx_MEM (BLKmode, copy);
1484 set_mem_attributes (copy, type, 1);
1486 else
1487 copy = assign_temp (type, 1, 0);
1489 store_expr (args[i].tree_value, copy, 0, false, false);
1491 /* Just change the const function to pure and then let
1492 the next test clear the pure based on
1493 callee_copies. */
1494 if (*ecf_flags & ECF_CONST)
1496 *ecf_flags &= ~ECF_CONST;
1497 *ecf_flags |= ECF_PURE;
1500 if (!callee_copies && *ecf_flags & ECF_PURE)
1501 *ecf_flags &= ~(ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
1503 args[i].tree_value
1504 = build_fold_addr_expr_loc (loc, make_tree (type, copy));
1505 type = TREE_TYPE (args[i].tree_value);
1506 *may_tailcall = false;
1507 maybe_complain_about_tail_call (exp,
1508 "argument must be passed"
1509 " by copying");
1513 unsignedp = TYPE_UNSIGNED (type);
1514 mode = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
1515 fndecl ? TREE_TYPE (fndecl) : fntype, 0);
1517 args[i].unsignedp = unsignedp;
1518 args[i].mode = mode;
1520 args[i].reg = targetm.calls.function_arg (args_so_far, mode, type,
1521 argpos < n_named_args);
1523 if (args[i].reg && CONST_INT_P (args[i].reg))
1525 args[i].special_slot = args[i].reg;
1526 args[i].reg = NULL;
1529 /* If this is a sibling call and the machine has register windows, the
1530 register window has to be unwinded before calling the routine, so
1531 arguments have to go into the incoming registers. */
1532 if (targetm.calls.function_incoming_arg != targetm.calls.function_arg)
1533 args[i].tail_call_reg
1534 = targetm.calls.function_incoming_arg (args_so_far, mode, type,
1535 argpos < n_named_args);
1536 else
1537 args[i].tail_call_reg = args[i].reg;
1539 if (args[i].reg)
1540 args[i].partial
1541 = targetm.calls.arg_partial_bytes (args_so_far, mode, type,
1542 argpos < n_named_args);
1544 args[i].pass_on_stack = targetm.calls.must_pass_in_stack (mode, type);
1546 /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
1547 it means that we are to pass this arg in the register(s) designated
1548 by the PARALLEL, but also to pass it in the stack. */
1549 if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
1550 && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
1551 args[i].pass_on_stack = 1;
1553 /* If this is an addressable type, we must preallocate the stack
1554 since we must evaluate the object into its final location.
1556 If this is to be passed in both registers and the stack, it is simpler
1557 to preallocate. */
1558 if (TREE_ADDRESSABLE (type)
1559 || (args[i].pass_on_stack && args[i].reg != 0))
1560 *must_preallocate = 1;
1562 /* No stack allocation and padding for bounds. */
1563 if (POINTER_BOUNDS_P (args[i].tree_value))
1565 /* Compute the stack-size of this argument. */
1566 else if (args[i].reg == 0 || args[i].partial != 0
1567 || reg_parm_stack_space > 0
1568 || args[i].pass_on_stack)
1569 locate_and_pad_parm (mode, type,
1570 #ifdef STACK_PARMS_IN_REG_PARM_AREA
1572 #else
1573 args[i].reg != 0,
1574 #endif
1575 reg_parm_stack_space,
1576 args[i].pass_on_stack ? 0 : args[i].partial,
1577 fndecl, args_size, &args[i].locate);
1578 #ifdef BLOCK_REG_PADDING
1579 else
1580 /* The argument is passed entirely in registers. See at which
1581 end it should be padded. */
1582 args[i].locate.where_pad =
1583 BLOCK_REG_PADDING (mode, type,
1584 int_size_in_bytes (type) <= UNITS_PER_WORD);
1585 #endif
1587 /* Update ARGS_SIZE, the total stack space for args so far. */
1589 args_size->constant += args[i].locate.size.constant;
1590 if (args[i].locate.size.var)
1591 ADD_PARM_SIZE (*args_size, args[i].locate.size.var);
1593 /* Increment ARGS_SO_FAR, which has info about which arg-registers
1594 have been used, etc. */
1596 targetm.calls.function_arg_advance (args_so_far, TYPE_MODE (type),
1597 type, argpos < n_named_args);
1601 /* Update ARGS_SIZE to contain the total size for the argument block.
1602 Return the original constant component of the argument block's size.
1604 REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
1605 for arguments passed in registers. */
1607 static int
1608 compute_argument_block_size (int reg_parm_stack_space,
1609 struct args_size *args_size,
1610 tree fndecl ATTRIBUTE_UNUSED,
1611 tree fntype ATTRIBUTE_UNUSED,
1612 int preferred_stack_boundary ATTRIBUTE_UNUSED)
1614 int unadjusted_args_size = args_size->constant;
1616 /* For accumulate outgoing args mode we don't need to align, since the frame
1617 will be already aligned. Align to STACK_BOUNDARY in order to prevent
1618 backends from generating misaligned frame sizes. */
1619 if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
1620 preferred_stack_boundary = STACK_BOUNDARY;
1622 /* Compute the actual size of the argument block required. The variable
1623 and constant sizes must be combined, the size may have to be rounded,
1624 and there may be a minimum required size. */
1626 if (args_size->var)
1628 args_size->var = ARGS_SIZE_TREE (*args_size);
1629 args_size->constant = 0;
1631 preferred_stack_boundary /= BITS_PER_UNIT;
1632 if (preferred_stack_boundary > 1)
1634 /* We don't handle this case yet. To handle it correctly we have
1635 to add the delta, round and subtract the delta.
1636 Currently no machine description requires this support. */
1637 gcc_assert (!(stack_pointer_delta & (preferred_stack_boundary - 1)));
1638 args_size->var = round_up (args_size->var, preferred_stack_boundary);
1641 if (reg_parm_stack_space > 0)
1643 args_size->var
1644 = size_binop (MAX_EXPR, args_size->var,
1645 ssize_int (reg_parm_stack_space));
1647 /* The area corresponding to register parameters is not to count in
1648 the size of the block we need. So make the adjustment. */
1649 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1650 args_size->var
1651 = size_binop (MINUS_EXPR, args_size->var,
1652 ssize_int (reg_parm_stack_space));
1655 else
1657 preferred_stack_boundary /= BITS_PER_UNIT;
1658 if (preferred_stack_boundary < 1)
1659 preferred_stack_boundary = 1;
1660 args_size->constant = (((args_size->constant
1661 + stack_pointer_delta
1662 + preferred_stack_boundary - 1)
1663 / preferred_stack_boundary
1664 * preferred_stack_boundary)
1665 - stack_pointer_delta);
1667 args_size->constant = MAX (args_size->constant,
1668 reg_parm_stack_space);
1670 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1671 args_size->constant -= reg_parm_stack_space;
1673 return unadjusted_args_size;
1676 /* Precompute parameters as needed for a function call.
1678 FLAGS is mask of ECF_* constants.
1680 NUM_ACTUALS is the number of arguments.
1682 ARGS is an array containing information for each argument; this
1683 routine fills in the INITIAL_VALUE and VALUE fields for each
1684 precomputed argument. */
1686 static void
1687 precompute_arguments (int num_actuals, struct arg_data *args)
1689 int i;
1691 /* If this is a libcall, then precompute all arguments so that we do not
1692 get extraneous instructions emitted as part of the libcall sequence. */
1694 /* If we preallocated the stack space, and some arguments must be passed
1695 on the stack, then we must precompute any parameter which contains a
1696 function call which will store arguments on the stack.
1697 Otherwise, evaluating the parameter may clobber previous parameters
1698 which have already been stored into the stack. (we have code to avoid
1699 such case by saving the outgoing stack arguments, but it results in
1700 worse code) */
1701 if (!ACCUMULATE_OUTGOING_ARGS)
1702 return;
1704 for (i = 0; i < num_actuals; i++)
1706 tree type;
1707 machine_mode mode;
1709 if (TREE_CODE (args[i].tree_value) != CALL_EXPR)
1710 continue;
1712 /* If this is an addressable type, we cannot pre-evaluate it. */
1713 type = TREE_TYPE (args[i].tree_value);
1714 gcc_assert (!TREE_ADDRESSABLE (type));
1716 args[i].initial_value = args[i].value
1717 = expand_normal (args[i].tree_value);
1719 mode = TYPE_MODE (type);
1720 if (mode != args[i].mode)
1722 int unsignedp = args[i].unsignedp;
1723 args[i].value
1724 = convert_modes (args[i].mode, mode,
1725 args[i].value, args[i].unsignedp);
1727 /* CSE will replace this only if it contains args[i].value
1728 pseudo, so convert it down to the declared mode using
1729 a SUBREG. */
1730 if (REG_P (args[i].value)
1731 && GET_MODE_CLASS (args[i].mode) == MODE_INT
1732 && promote_mode (type, mode, &unsignedp) != args[i].mode)
1734 args[i].initial_value
1735 = gen_lowpart_SUBREG (mode, args[i].value);
1736 SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1737 SUBREG_PROMOTED_SET (args[i].initial_value, args[i].unsignedp);
1743 /* Given the current state of MUST_PREALLOCATE and information about
1744 arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
1745 compute and return the final value for MUST_PREALLOCATE. */
1747 static int
1748 finalize_must_preallocate (int must_preallocate, int num_actuals,
1749 struct arg_data *args, struct args_size *args_size)
1751 /* See if we have or want to preallocate stack space.
1753 If we would have to push a partially-in-regs parm
1754 before other stack parms, preallocate stack space instead.
1756 If the size of some parm is not a multiple of the required stack
1757 alignment, we must preallocate.
1759 If the total size of arguments that would otherwise create a copy in
1760 a temporary (such as a CALL) is more than half the total argument list
1761 size, preallocation is faster.
1763 Another reason to preallocate is if we have a machine (like the m88k)
1764 where stack alignment is required to be maintained between every
1765 pair of insns, not just when the call is made. However, we assume here
1766 that such machines either do not have push insns (and hence preallocation
1767 would occur anyway) or the problem is taken care of with
1768 PUSH_ROUNDING. */
1770 if (! must_preallocate)
1772 int partial_seen = 0;
1773 int copy_to_evaluate_size = 0;
1774 int i;
1776 for (i = 0; i < num_actuals && ! must_preallocate; i++)
1778 if (args[i].partial > 0 && ! args[i].pass_on_stack)
1779 partial_seen = 1;
1780 else if (partial_seen && args[i].reg == 0)
1781 must_preallocate = 1;
1782 /* We preallocate in case there are bounds passed
1783 in the bounds table to have precomputed address
1784 for bounds association. */
1785 else if (POINTER_BOUNDS_P (args[i].tree_value)
1786 && !args[i].reg)
1787 must_preallocate = 1;
1789 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1790 && (TREE_CODE (args[i].tree_value) == CALL_EXPR
1791 || TREE_CODE (args[i].tree_value) == TARGET_EXPR
1792 || TREE_CODE (args[i].tree_value) == COND_EXPR
1793 || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
1794 copy_to_evaluate_size
1795 += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1798 if (copy_to_evaluate_size * 2 >= args_size->constant
1799 && args_size->constant > 0)
1800 must_preallocate = 1;
1802 return must_preallocate;
1805 /* If we preallocated stack space, compute the address of each argument
1806 and store it into the ARGS array.
1808 We need not ensure it is a valid memory address here; it will be
1809 validized when it is used.
1811 ARGBLOCK is an rtx for the address of the outgoing arguments. */
1813 static void
1814 compute_argument_addresses (struct arg_data *args, rtx argblock, int num_actuals)
1816 if (argblock)
1818 rtx arg_reg = argblock;
1819 int i, arg_offset = 0;
1821 if (GET_CODE (argblock) == PLUS)
1822 arg_reg = XEXP (argblock, 0), arg_offset = INTVAL (XEXP (argblock, 1));
1824 for (i = 0; i < num_actuals; i++)
1826 rtx offset = ARGS_SIZE_RTX (args[i].locate.offset);
1827 rtx slot_offset = ARGS_SIZE_RTX (args[i].locate.slot_offset);
1828 rtx addr;
1829 unsigned int align, boundary;
1830 unsigned int units_on_stack = 0;
1831 machine_mode partial_mode = VOIDmode;
1833 /* Skip this parm if it will not be passed on the stack. */
1834 if (! args[i].pass_on_stack
1835 && args[i].reg != 0
1836 && args[i].partial == 0)
1837 continue;
1839 /* Pointer Bounds are never passed on the stack. */
1840 if (POINTER_BOUNDS_P (args[i].tree_value))
1841 continue;
1843 if (CONST_INT_P (offset))
1844 addr = plus_constant (Pmode, arg_reg, INTVAL (offset));
1845 else
1846 addr = gen_rtx_PLUS (Pmode, arg_reg, offset);
1848 addr = plus_constant (Pmode, addr, arg_offset);
1850 if (args[i].partial != 0)
1852 /* Only part of the parameter is being passed on the stack.
1853 Generate a simple memory reference of the correct size. */
1854 units_on_stack = args[i].locate.size.constant;
1855 partial_mode = mode_for_size (units_on_stack * BITS_PER_UNIT,
1856 MODE_INT, 1);
1857 args[i].stack = gen_rtx_MEM (partial_mode, addr);
1858 set_mem_size (args[i].stack, units_on_stack);
1860 else
1862 args[i].stack = gen_rtx_MEM (args[i].mode, addr);
1863 set_mem_attributes (args[i].stack,
1864 TREE_TYPE (args[i].tree_value), 1);
1866 align = BITS_PER_UNIT;
1867 boundary = args[i].locate.boundary;
1868 if (args[i].locate.where_pad != downward)
1869 align = boundary;
1870 else if (CONST_INT_P (offset))
1872 align = INTVAL (offset) * BITS_PER_UNIT | boundary;
1873 align = least_bit_hwi (align);
1875 set_mem_align (args[i].stack, align);
1877 if (CONST_INT_P (slot_offset))
1878 addr = plus_constant (Pmode, arg_reg, INTVAL (slot_offset));
1879 else
1880 addr = gen_rtx_PLUS (Pmode, arg_reg, slot_offset);
1882 addr = plus_constant (Pmode, addr, arg_offset);
1884 if (args[i].partial != 0)
1886 /* Only part of the parameter is being passed on the stack.
1887 Generate a simple memory reference of the correct size.
1889 args[i].stack_slot = gen_rtx_MEM (partial_mode, addr);
1890 set_mem_size (args[i].stack_slot, units_on_stack);
1892 else
1894 args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
1895 set_mem_attributes (args[i].stack_slot,
1896 TREE_TYPE (args[i].tree_value), 1);
1898 set_mem_align (args[i].stack_slot, args[i].locate.boundary);
1900 /* Function incoming arguments may overlap with sibling call
1901 outgoing arguments and we cannot allow reordering of reads
1902 from function arguments with stores to outgoing arguments
1903 of sibling calls. */
1904 set_mem_alias_set (args[i].stack, 0);
1905 set_mem_alias_set (args[i].stack_slot, 0);
1910 /* Given a FNDECL and EXP, return an rtx suitable for use as a target address
1911 in a call instruction.
1913 FNDECL is the tree node for the target function. For an indirect call
1914 FNDECL will be NULL_TREE.
1916 ADDR is the operand 0 of CALL_EXPR for this call. */
1918 static rtx
1919 rtx_for_function_call (tree fndecl, tree addr)
1921 rtx funexp;
1923 /* Get the function to call, in the form of RTL. */
1924 if (fndecl)
1926 if (!TREE_USED (fndecl) && fndecl != current_function_decl)
1927 TREE_USED (fndecl) = 1;
1929 /* Get a SYMBOL_REF rtx for the function address. */
1930 funexp = XEXP (DECL_RTL (fndecl), 0);
1932 else
1933 /* Generate an rtx (probably a pseudo-register) for the address. */
1935 push_temp_slots ();
1936 funexp = expand_normal (addr);
1937 pop_temp_slots (); /* FUNEXP can't be BLKmode. */
1939 return funexp;
1942 /* Internal state for internal_arg_pointer_based_exp and its helpers. */
1943 static struct
1945 /* Last insn that has been scanned by internal_arg_pointer_based_exp_scan,
1946 or NULL_RTX if none has been scanned yet. */
1947 rtx_insn *scan_start;
1948 /* Vector indexed by REGNO - FIRST_PSEUDO_REGISTER, recording if a pseudo is
1949 based on crtl->args.internal_arg_pointer. The element is NULL_RTX if the
1950 pseudo isn't based on it, a CONST_INT offset if the pseudo is based on it
1951 with fixed offset, or PC if this is with variable or unknown offset. */
1952 vec<rtx> cache;
1953 } internal_arg_pointer_exp_state;
1955 static rtx internal_arg_pointer_based_exp (const_rtx, bool);
1957 /* Helper function for internal_arg_pointer_based_exp. Scan insns in
1958 the tail call sequence, starting with first insn that hasn't been
1959 scanned yet, and note for each pseudo on the LHS whether it is based
1960 on crtl->args.internal_arg_pointer or not, and what offset from that
1961 that pointer it has. */
1963 static void
1964 internal_arg_pointer_based_exp_scan (void)
1966 rtx_insn *insn, *scan_start = internal_arg_pointer_exp_state.scan_start;
1968 if (scan_start == NULL_RTX)
1969 insn = get_insns ();
1970 else
1971 insn = NEXT_INSN (scan_start);
1973 while (insn)
1975 rtx set = single_set (insn);
1976 if (set && REG_P (SET_DEST (set)) && !HARD_REGISTER_P (SET_DEST (set)))
1978 rtx val = NULL_RTX;
1979 unsigned int idx = REGNO (SET_DEST (set)) - FIRST_PSEUDO_REGISTER;
1980 /* Punt on pseudos set multiple times. */
1981 if (idx < internal_arg_pointer_exp_state.cache.length ()
1982 && (internal_arg_pointer_exp_state.cache[idx]
1983 != NULL_RTX))
1984 val = pc_rtx;
1985 else
1986 val = internal_arg_pointer_based_exp (SET_SRC (set), false);
1987 if (val != NULL_RTX)
1989 if (idx >= internal_arg_pointer_exp_state.cache.length ())
1990 internal_arg_pointer_exp_state.cache
1991 .safe_grow_cleared (idx + 1);
1992 internal_arg_pointer_exp_state.cache[idx] = val;
1995 if (NEXT_INSN (insn) == NULL_RTX)
1996 scan_start = insn;
1997 insn = NEXT_INSN (insn);
2000 internal_arg_pointer_exp_state.scan_start = scan_start;
2003 /* Compute whether RTL is based on crtl->args.internal_arg_pointer. Return
2004 NULL_RTX if RTL isn't based on it, a CONST_INT offset if RTL is based on
2005 it with fixed offset, or PC if this is with variable or unknown offset.
2006 TOPLEVEL is true if the function is invoked at the topmost level. */
2008 static rtx
2009 internal_arg_pointer_based_exp (const_rtx rtl, bool toplevel)
2011 if (CONSTANT_P (rtl))
2012 return NULL_RTX;
2014 if (rtl == crtl->args.internal_arg_pointer)
2015 return const0_rtx;
2017 if (REG_P (rtl) && HARD_REGISTER_P (rtl))
2018 return NULL_RTX;
2020 if (GET_CODE (rtl) == PLUS && CONST_INT_P (XEXP (rtl, 1)))
2022 rtx val = internal_arg_pointer_based_exp (XEXP (rtl, 0), toplevel);
2023 if (val == NULL_RTX || val == pc_rtx)
2024 return val;
2025 return plus_constant (Pmode, val, INTVAL (XEXP (rtl, 1)));
2028 /* When called at the topmost level, scan pseudo assignments in between the
2029 last scanned instruction in the tail call sequence and the latest insn
2030 in that sequence. */
2031 if (toplevel)
2032 internal_arg_pointer_based_exp_scan ();
2034 if (REG_P (rtl))
2036 unsigned int idx = REGNO (rtl) - FIRST_PSEUDO_REGISTER;
2037 if (idx < internal_arg_pointer_exp_state.cache.length ())
2038 return internal_arg_pointer_exp_state.cache[idx];
2040 return NULL_RTX;
2043 subrtx_iterator::array_type array;
2044 FOR_EACH_SUBRTX (iter, array, rtl, NONCONST)
2046 const_rtx x = *iter;
2047 if (REG_P (x) && internal_arg_pointer_based_exp (x, false) != NULL_RTX)
2048 return pc_rtx;
2049 if (MEM_P (x))
2050 iter.skip_subrtxes ();
2053 return NULL_RTX;
2056 /* Return true if and only if SIZE storage units (usually bytes)
2057 starting from address ADDR overlap with already clobbered argument
2058 area. This function is used to determine if we should give up a
2059 sibcall. */
2061 static bool
2062 mem_overlaps_already_clobbered_arg_p (rtx addr, unsigned HOST_WIDE_INT size)
2064 HOST_WIDE_INT i;
2065 rtx val;
2067 if (bitmap_empty_p (stored_args_map))
2068 return false;
2069 val = internal_arg_pointer_based_exp (addr, true);
2070 if (val == NULL_RTX)
2071 return false;
2072 else if (val == pc_rtx)
2073 return true;
2074 else
2075 i = INTVAL (val);
2077 if (STACK_GROWS_DOWNWARD)
2078 i -= crtl->args.pretend_args_size;
2079 else
2080 i += crtl->args.pretend_args_size;
2083 if (ARGS_GROW_DOWNWARD)
2084 i = -i - size;
2086 if (size > 0)
2088 unsigned HOST_WIDE_INT k;
2090 for (k = 0; k < size; k++)
2091 if (i + k < SBITMAP_SIZE (stored_args_map)
2092 && bitmap_bit_p (stored_args_map, i + k))
2093 return true;
2096 return false;
2099 /* Do the register loads required for any wholly-register parms or any
2100 parms which are passed both on the stack and in a register. Their
2101 expressions were already evaluated.
2103 Mark all register-parms as living through the call, putting these USE
2104 insns in the CALL_INSN_FUNCTION_USAGE field.
2106 When IS_SIBCALL, perform the check_sibcall_argument_overlap
2107 checking, setting *SIBCALL_FAILURE if appropriate. */
2109 static void
2110 load_register_parameters (struct arg_data *args, int num_actuals,
2111 rtx *call_fusage, int flags, int is_sibcall,
2112 int *sibcall_failure)
2114 int i, j;
2116 for (i = 0; i < num_actuals; i++)
2118 rtx reg = ((flags & ECF_SIBCALL)
2119 ? args[i].tail_call_reg : args[i].reg);
2120 if (reg)
2122 int partial = args[i].partial;
2123 int nregs;
2124 int size = 0;
2125 rtx_insn *before_arg = get_last_insn ();
2126 /* Set non-negative if we must move a word at a time, even if
2127 just one word (e.g, partial == 4 && mode == DFmode). Set
2128 to -1 if we just use a normal move insn. This value can be
2129 zero if the argument is a zero size structure. */
2130 nregs = -1;
2131 if (GET_CODE (reg) == PARALLEL)
2133 else if (partial)
2135 gcc_assert (partial % UNITS_PER_WORD == 0);
2136 nregs = partial / UNITS_PER_WORD;
2138 else if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode)
2140 size = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
2141 nregs = (size + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
2143 else
2144 size = GET_MODE_SIZE (args[i].mode);
2146 /* Handle calls that pass values in multiple non-contiguous
2147 locations. The Irix 6 ABI has examples of this. */
2149 if (GET_CODE (reg) == PARALLEL)
2150 emit_group_move (reg, args[i].parallel_value);
2152 /* If simple case, just do move. If normal partial, store_one_arg
2153 has already loaded the register for us. In all other cases,
2154 load the register(s) from memory. */
2156 else if (nregs == -1)
2158 emit_move_insn (reg, args[i].value);
2159 #ifdef BLOCK_REG_PADDING
2160 /* Handle case where we have a value that needs shifting
2161 up to the msb. eg. a QImode value and we're padding
2162 upward on a BYTES_BIG_ENDIAN machine. */
2163 if (size < UNITS_PER_WORD
2164 && (args[i].locate.where_pad
2165 == (BYTES_BIG_ENDIAN ? upward : downward)))
2167 rtx x;
2168 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2170 /* Assigning REG here rather than a temp makes CALL_FUSAGE
2171 report the whole reg as used. Strictly speaking, the
2172 call only uses SIZE bytes at the msb end, but it doesn't
2173 seem worth generating rtl to say that. */
2174 reg = gen_rtx_REG (word_mode, REGNO (reg));
2175 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
2176 if (x != reg)
2177 emit_move_insn (reg, x);
2179 #endif
2182 /* If we have pre-computed the values to put in the registers in
2183 the case of non-aligned structures, copy them in now. */
2185 else if (args[i].n_aligned_regs != 0)
2186 for (j = 0; j < args[i].n_aligned_regs; j++)
2187 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
2188 args[i].aligned_regs[j]);
2190 else if (partial == 0 || args[i].pass_on_stack)
2192 rtx mem = validize_mem (copy_rtx (args[i].value));
2194 /* Check for overlap with already clobbered argument area,
2195 providing that this has non-zero size. */
2196 if (is_sibcall
2197 && size != 0
2198 && (mem_overlaps_already_clobbered_arg_p
2199 (XEXP (args[i].value, 0), size)))
2200 *sibcall_failure = 1;
2202 if (size % UNITS_PER_WORD == 0
2203 || MEM_ALIGN (mem) % BITS_PER_WORD == 0)
2204 move_block_to_reg (REGNO (reg), mem, nregs, args[i].mode);
2205 else
2207 if (nregs > 1)
2208 move_block_to_reg (REGNO (reg), mem, nregs - 1,
2209 args[i].mode);
2210 rtx dest = gen_rtx_REG (word_mode, REGNO (reg) + nregs - 1);
2211 unsigned int bitoff = (nregs - 1) * BITS_PER_WORD;
2212 unsigned int bitsize = size * BITS_PER_UNIT - bitoff;
2213 rtx x = extract_bit_field (mem, bitsize, bitoff, 1, dest,
2214 word_mode, word_mode, false);
2215 if (BYTES_BIG_ENDIAN)
2216 x = expand_shift (LSHIFT_EXPR, word_mode, x,
2217 BITS_PER_WORD - bitsize, dest, 1);
2218 if (x != dest)
2219 emit_move_insn (dest, x);
2222 /* Handle a BLKmode that needs shifting. */
2223 if (nregs == 1 && size < UNITS_PER_WORD
2224 #ifdef BLOCK_REG_PADDING
2225 && args[i].locate.where_pad == downward
2226 #else
2227 && BYTES_BIG_ENDIAN
2228 #endif
2231 rtx dest = gen_rtx_REG (word_mode, REGNO (reg));
2232 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2233 enum tree_code dir = (BYTES_BIG_ENDIAN
2234 ? RSHIFT_EXPR : LSHIFT_EXPR);
2235 rtx x;
2237 x = expand_shift (dir, word_mode, dest, shift, dest, 1);
2238 if (x != dest)
2239 emit_move_insn (dest, x);
2243 /* When a parameter is a block, and perhaps in other cases, it is
2244 possible that it did a load from an argument slot that was
2245 already clobbered. */
2246 if (is_sibcall
2247 && check_sibcall_argument_overlap (before_arg, &args[i], 0))
2248 *sibcall_failure = 1;
2250 /* Handle calls that pass values in multiple non-contiguous
2251 locations. The Irix 6 ABI has examples of this. */
2252 if (GET_CODE (reg) == PARALLEL)
2253 use_group_regs (call_fusage, reg);
2254 else if (nregs == -1)
2255 use_reg_mode (call_fusage, reg,
2256 TYPE_MODE (TREE_TYPE (args[i].tree_value)));
2257 else if (nregs > 0)
2258 use_regs (call_fusage, REGNO (reg), nregs);
2263 /* We need to pop PENDING_STACK_ADJUST bytes. But, if the arguments
2264 wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
2265 bytes, then we would need to push some additional bytes to pad the
2266 arguments. So, we compute an adjust to the stack pointer for an
2267 amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
2268 bytes. Then, when the arguments are pushed the stack will be perfectly
2269 aligned. ARGS_SIZE->CONSTANT is set to the number of bytes that should
2270 be popped after the call. Returns the adjustment. */
2272 static int
2273 combine_pending_stack_adjustment_and_call (int unadjusted_args_size,
2274 struct args_size *args_size,
2275 unsigned int preferred_unit_stack_boundary)
2277 /* The number of bytes to pop so that the stack will be
2278 under-aligned by UNADJUSTED_ARGS_SIZE bytes. */
2279 HOST_WIDE_INT adjustment;
2280 /* The alignment of the stack after the arguments are pushed, if we
2281 just pushed the arguments without adjust the stack here. */
2282 unsigned HOST_WIDE_INT unadjusted_alignment;
2284 unadjusted_alignment
2285 = ((stack_pointer_delta + unadjusted_args_size)
2286 % preferred_unit_stack_boundary);
2288 /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
2289 as possible -- leaving just enough left to cancel out the
2290 UNADJUSTED_ALIGNMENT. In other words, we want to ensure that the
2291 PENDING_STACK_ADJUST is non-negative, and congruent to
2292 -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY. */
2294 /* Begin by trying to pop all the bytes. */
2295 unadjusted_alignment
2296 = (unadjusted_alignment
2297 - (pending_stack_adjust % preferred_unit_stack_boundary));
2298 adjustment = pending_stack_adjust;
2299 /* Push enough additional bytes that the stack will be aligned
2300 after the arguments are pushed. */
2301 if (preferred_unit_stack_boundary > 1)
2303 if (unadjusted_alignment > 0)
2304 adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
2305 else
2306 adjustment += unadjusted_alignment;
2309 /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
2310 bytes after the call. The right number is the entire
2311 PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
2312 by the arguments in the first place. */
2313 args_size->constant
2314 = pending_stack_adjust - adjustment + unadjusted_args_size;
2316 return adjustment;
2319 /* Scan X expression if it does not dereference any argument slots
2320 we already clobbered by tail call arguments (as noted in stored_args_map
2321 bitmap).
2322 Return nonzero if X expression dereferences such argument slots,
2323 zero otherwise. */
2325 static int
2326 check_sibcall_argument_overlap_1 (rtx x)
2328 RTX_CODE code;
2329 int i, j;
2330 const char *fmt;
2332 if (x == NULL_RTX)
2333 return 0;
2335 code = GET_CODE (x);
2337 /* We need not check the operands of the CALL expression itself. */
2338 if (code == CALL)
2339 return 0;
2341 if (code == MEM)
2342 return mem_overlaps_already_clobbered_arg_p (XEXP (x, 0),
2343 GET_MODE_SIZE (GET_MODE (x)));
2345 /* Scan all subexpressions. */
2346 fmt = GET_RTX_FORMAT (code);
2347 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2349 if (*fmt == 'e')
2351 if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
2352 return 1;
2354 else if (*fmt == 'E')
2356 for (j = 0; j < XVECLEN (x, i); j++)
2357 if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
2358 return 1;
2361 return 0;
2364 /* Scan sequence after INSN if it does not dereference any argument slots
2365 we already clobbered by tail call arguments (as noted in stored_args_map
2366 bitmap). If MARK_STORED_ARGS_MAP, add stack slots for ARG to
2367 stored_args_map bitmap afterwards (when ARG is a register MARK_STORED_ARGS_MAP
2368 should be 0). Return nonzero if sequence after INSN dereferences such argument
2369 slots, zero otherwise. */
2371 static int
2372 check_sibcall_argument_overlap (rtx_insn *insn, struct arg_data *arg,
2373 int mark_stored_args_map)
2375 int low, high;
2377 if (insn == NULL_RTX)
2378 insn = get_insns ();
2379 else
2380 insn = NEXT_INSN (insn);
2382 for (; insn; insn = NEXT_INSN (insn))
2383 if (INSN_P (insn)
2384 && check_sibcall_argument_overlap_1 (PATTERN (insn)))
2385 break;
2387 if (mark_stored_args_map)
2389 if (ARGS_GROW_DOWNWARD)
2390 low = -arg->locate.slot_offset.constant - arg->locate.size.constant;
2391 else
2392 low = arg->locate.slot_offset.constant;
2394 for (high = low + arg->locate.size.constant; low < high; low++)
2395 bitmap_set_bit (stored_args_map, low);
2397 return insn != NULL_RTX;
2400 /* Given that a function returns a value of mode MODE at the most
2401 significant end of hard register VALUE, shift VALUE left or right
2402 as specified by LEFT_P. Return true if some action was needed. */
2404 bool
2405 shift_return_value (machine_mode mode, bool left_p, rtx value)
2407 HOST_WIDE_INT shift;
2409 gcc_assert (REG_P (value) && HARD_REGISTER_P (value));
2410 shift = GET_MODE_BITSIZE (GET_MODE (value)) - GET_MODE_BITSIZE (mode);
2411 if (shift == 0)
2412 return false;
2414 /* Use ashr rather than lshr for right shifts. This is for the benefit
2415 of the MIPS port, which requires SImode values to be sign-extended
2416 when stored in 64-bit registers. */
2417 if (!force_expand_binop (GET_MODE (value), left_p ? ashl_optab : ashr_optab,
2418 value, GEN_INT (shift), value, 1, OPTAB_WIDEN))
2419 gcc_unreachable ();
2420 return true;
2423 /* If X is a likely-spilled register value, copy it to a pseudo
2424 register and return that register. Return X otherwise. */
2426 static rtx
2427 avoid_likely_spilled_reg (rtx x)
2429 rtx new_rtx;
2431 if (REG_P (x)
2432 && HARD_REGISTER_P (x)
2433 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (x))))
2435 /* Make sure that we generate a REG rather than a CONCAT.
2436 Moves into CONCATs can need nontrivial instructions,
2437 and the whole point of this function is to avoid
2438 using the hard register directly in such a situation. */
2439 generating_concat_p = 0;
2440 new_rtx = gen_reg_rtx (GET_MODE (x));
2441 generating_concat_p = 1;
2442 emit_move_insn (new_rtx, x);
2443 return new_rtx;
2445 return x;
2448 /* Helper function for expand_call.
2449 Return false is EXP is not implementable as a sibling call. */
2451 static bool
2452 can_implement_as_sibling_call_p (tree exp,
2453 rtx structure_value_addr,
2454 tree funtype,
2455 int reg_parm_stack_space ATTRIBUTE_UNUSED,
2456 tree fndecl,
2457 int flags,
2458 tree addr,
2459 const args_size &args_size)
2461 if (!targetm.have_sibcall_epilogue ())
2463 maybe_complain_about_tail_call
2464 (exp,
2465 "machine description does not have"
2466 " a sibcall_epilogue instruction pattern");
2467 return false;
2470 /* Doing sibling call optimization needs some work, since
2471 structure_value_addr can be allocated on the stack.
2472 It does not seem worth the effort since few optimizable
2473 sibling calls will return a structure. */
2474 if (structure_value_addr != NULL_RTX)
2476 maybe_complain_about_tail_call (exp, "callee returns a structure");
2477 return false;
2480 #ifdef REG_PARM_STACK_SPACE
2481 /* If outgoing reg parm stack space changes, we can not do sibcall. */
2482 if (OUTGOING_REG_PARM_STACK_SPACE (funtype)
2483 != OUTGOING_REG_PARM_STACK_SPACE (TREE_TYPE (current_function_decl))
2484 || (reg_parm_stack_space != REG_PARM_STACK_SPACE (current_function_decl)))
2486 maybe_complain_about_tail_call (exp,
2487 "inconsistent size of stack space"
2488 " allocated for arguments which are"
2489 " passed in registers");
2490 return false;
2492 #endif
2494 /* Check whether the target is able to optimize the call
2495 into a sibcall. */
2496 if (!targetm.function_ok_for_sibcall (fndecl, exp))
2498 maybe_complain_about_tail_call (exp,
2499 "target is not able to optimize the"
2500 " call into a sibling call");
2501 return false;
2504 /* Functions that do not return exactly once may not be sibcall
2505 optimized. */
2506 if (flags & ECF_RETURNS_TWICE)
2508 maybe_complain_about_tail_call (exp, "callee returns twice");
2509 return false;
2511 if (flags & ECF_NORETURN)
2513 maybe_complain_about_tail_call (exp, "callee does not return");
2514 return false;
2517 if (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (addr))))
2519 maybe_complain_about_tail_call (exp, "volatile function type");
2520 return false;
2523 /* If the called function is nested in the current one, it might access
2524 some of the caller's arguments, but could clobber them beforehand if
2525 the argument areas are shared. */
2526 if (fndecl && decl_function_context (fndecl) == current_function_decl)
2528 maybe_complain_about_tail_call (exp, "nested function");
2529 return false;
2532 /* If this function requires more stack slots than the current
2533 function, we cannot change it into a sibling call.
2534 crtl->args.pretend_args_size is not part of the
2535 stack allocated by our caller. */
2536 if (args_size.constant > (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 (targetm.calls.return_pops_args (fndecl, funtype, args_size.constant)
2547 != targetm.calls.return_pops_args (current_function_decl,
2548 TREE_TYPE (current_function_decl),
2549 crtl->args.size))
2551 maybe_complain_about_tail_call (exp,
2552 "inconsistent number of"
2553 " popped arguments");
2554 return false;
2557 if (!lang_hooks.decls.ok_for_sibcall (fndecl))
2559 maybe_complain_about_tail_call (exp, "frontend does not support"
2560 " sibling call");
2561 return false;
2564 /* All checks passed. */
2565 return true;
2568 /* Generate all the code for a CALL_EXPR exp
2569 and return an rtx for its value.
2570 Store the value in TARGET (specified as an rtx) if convenient.
2571 If the value is stored in TARGET then TARGET is returned.
2572 If IGNORE is nonzero, then we ignore the value of the function call. */
2575 expand_call (tree exp, rtx target, int ignore)
2577 /* Nonzero if we are currently expanding a call. */
2578 static int currently_expanding_call = 0;
2580 /* RTX for the function to be called. */
2581 rtx funexp;
2582 /* Sequence of insns to perform a normal "call". */
2583 rtx_insn *normal_call_insns = NULL;
2584 /* Sequence of insns to perform a tail "call". */
2585 rtx_insn *tail_call_insns = NULL;
2586 /* Data type of the function. */
2587 tree funtype;
2588 tree type_arg_types;
2589 tree rettype;
2590 /* Declaration of the function being called,
2591 or 0 if the function is computed (not known by name). */
2592 tree fndecl = 0;
2593 /* The type of the function being called. */
2594 tree fntype;
2595 bool try_tail_call = CALL_EXPR_TAILCALL (exp);
2596 bool must_tail_call = CALL_EXPR_MUST_TAIL_CALL (exp);
2597 int pass;
2599 /* Register in which non-BLKmode value will be returned,
2600 or 0 if no value or if value is BLKmode. */
2601 rtx valreg;
2602 /* Register(s) in which bounds are returned. */
2603 rtx valbnd = NULL;
2604 /* Address where we should return a BLKmode value;
2605 0 if value not BLKmode. */
2606 rtx structure_value_addr = 0;
2607 /* Nonzero if that address is being passed by treating it as
2608 an extra, implicit first parameter. Otherwise,
2609 it is passed by being copied directly into struct_value_rtx. */
2610 int structure_value_addr_parm = 0;
2611 /* Holds the value of implicit argument for the struct value. */
2612 tree structure_value_addr_value = NULL_TREE;
2613 /* Size of aggregate value wanted, or zero if none wanted
2614 or if we are using the non-reentrant PCC calling convention
2615 or expecting the value in registers. */
2616 HOST_WIDE_INT struct_value_size = 0;
2617 /* Nonzero if called function returns an aggregate in memory PCC style,
2618 by returning the address of where to find it. */
2619 int pcc_struct_value = 0;
2620 rtx struct_value = 0;
2622 /* Number of actual parameters in this call, including struct value addr. */
2623 int num_actuals;
2624 /* Number of named args. Args after this are anonymous ones
2625 and they must all go on the stack. */
2626 int n_named_args;
2627 /* Number of complex actual arguments that need to be split. */
2628 int num_complex_actuals = 0;
2630 /* Vector of information about each argument.
2631 Arguments are numbered in the order they will be pushed,
2632 not the order they are written. */
2633 struct arg_data *args;
2635 /* Total size in bytes of all the stack-parms scanned so far. */
2636 struct args_size args_size;
2637 struct args_size adjusted_args_size;
2638 /* Size of arguments before any adjustments (such as rounding). */
2639 int unadjusted_args_size;
2640 /* Data on reg parms scanned so far. */
2641 CUMULATIVE_ARGS args_so_far_v;
2642 cumulative_args_t args_so_far;
2643 /* Nonzero if a reg parm has been scanned. */
2644 int reg_parm_seen;
2645 /* Nonzero if this is an indirect function call. */
2647 /* Nonzero if we must avoid push-insns in the args for this call.
2648 If stack space is allocated for register parameters, but not by the
2649 caller, then it is preallocated in the fixed part of the stack frame.
2650 So the entire argument block must then be preallocated (i.e., we
2651 ignore PUSH_ROUNDING in that case). */
2653 int must_preallocate = !PUSH_ARGS;
2655 /* Size of the stack reserved for parameter registers. */
2656 int reg_parm_stack_space = 0;
2658 /* Address of space preallocated for stack parms
2659 (on machines that lack push insns), or 0 if space not preallocated. */
2660 rtx argblock = 0;
2662 /* Mask of ECF_ and ERF_ flags. */
2663 int flags = 0;
2664 int return_flags = 0;
2665 #ifdef REG_PARM_STACK_SPACE
2666 /* Define the boundary of the register parm stack space that needs to be
2667 saved, if any. */
2668 int low_to_save, high_to_save;
2669 rtx save_area = 0; /* Place that it is saved */
2670 #endif
2672 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
2673 char *initial_stack_usage_map = stack_usage_map;
2674 char *stack_usage_map_buf = NULL;
2676 int old_stack_allocated;
2678 /* State variables to track stack modifications. */
2679 rtx old_stack_level = 0;
2680 int old_stack_arg_under_construction = 0;
2681 int old_pending_adj = 0;
2682 int old_inhibit_defer_pop = inhibit_defer_pop;
2684 /* Some stack pointer alterations we make are performed via
2685 allocate_dynamic_stack_space. This modifies the stack_pointer_delta,
2686 which we then also need to save/restore along the way. */
2687 int old_stack_pointer_delta = 0;
2689 rtx call_fusage;
2690 tree addr = CALL_EXPR_FN (exp);
2691 int i;
2692 /* The alignment of the stack, in bits. */
2693 unsigned HOST_WIDE_INT preferred_stack_boundary;
2694 /* The alignment of the stack, in bytes. */
2695 unsigned HOST_WIDE_INT preferred_unit_stack_boundary;
2696 /* The static chain value to use for this call. */
2697 rtx static_chain_value;
2698 /* See if this is "nothrow" function call. */
2699 if (TREE_NOTHROW (exp))
2700 flags |= ECF_NOTHROW;
2702 /* See if we can find a DECL-node for the actual function, and get the
2703 function attributes (flags) from the function decl or type node. */
2704 fndecl = get_callee_fndecl (exp);
2705 if (fndecl)
2707 fntype = TREE_TYPE (fndecl);
2708 flags |= flags_from_decl_or_type (fndecl);
2709 return_flags |= decl_return_flags (fndecl);
2711 else
2713 fntype = TREE_TYPE (TREE_TYPE (addr));
2714 flags |= flags_from_decl_or_type (fntype);
2715 if (CALL_EXPR_BY_DESCRIPTOR (exp))
2716 flags |= ECF_BY_DESCRIPTOR;
2718 rettype = TREE_TYPE (exp);
2720 struct_value = targetm.calls.struct_value_rtx (fntype, 0);
2722 /* Warn if this value is an aggregate type,
2723 regardless of which calling convention we are using for it. */
2724 if (AGGREGATE_TYPE_P (rettype))
2725 warning (OPT_Waggregate_return, "function call has aggregate value");
2727 /* If the result of a non looping pure or const function call is
2728 ignored (or void), and none of its arguments are volatile, we can
2729 avoid expanding the call and just evaluate the arguments for
2730 side-effects. */
2731 if ((flags & (ECF_CONST | ECF_PURE))
2732 && (!(flags & ECF_LOOPING_CONST_OR_PURE))
2733 && (ignore || target == const0_rtx
2734 || TYPE_MODE (rettype) == VOIDmode))
2736 bool volatilep = false;
2737 tree arg;
2738 call_expr_arg_iterator iter;
2740 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2741 if (TREE_THIS_VOLATILE (arg))
2743 volatilep = true;
2744 break;
2747 if (! volatilep)
2749 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2750 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
2751 return const0_rtx;
2755 #ifdef REG_PARM_STACK_SPACE
2756 reg_parm_stack_space = REG_PARM_STACK_SPACE (!fndecl ? fntype : fndecl);
2757 #endif
2759 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
2760 && reg_parm_stack_space > 0 && PUSH_ARGS)
2761 must_preallocate = 1;
2763 /* Set up a place to return a structure. */
2765 /* Cater to broken compilers. */
2766 if (aggregate_value_p (exp, fntype))
2768 /* This call returns a big structure. */
2769 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
2771 #ifdef PCC_STATIC_STRUCT_RETURN
2773 pcc_struct_value = 1;
2775 #else /* not PCC_STATIC_STRUCT_RETURN */
2777 struct_value_size = int_size_in_bytes (rettype);
2779 /* Even if it is semantically safe to use the target as the return
2780 slot, it may be not sufficiently aligned for the return type. */
2781 if (CALL_EXPR_RETURN_SLOT_OPT (exp)
2782 && target
2783 && MEM_P (target)
2784 && !(MEM_ALIGN (target) < TYPE_ALIGN (rettype)
2785 && SLOW_UNALIGNED_ACCESS (TYPE_MODE (rettype),
2786 MEM_ALIGN (target))))
2787 structure_value_addr = XEXP (target, 0);
2788 else
2790 /* For variable-sized objects, we must be called with a target
2791 specified. If we were to allocate space on the stack here,
2792 we would have no way of knowing when to free it. */
2793 rtx d = assign_temp (rettype, 1, 1);
2794 structure_value_addr = XEXP (d, 0);
2795 target = 0;
2798 #endif /* not PCC_STATIC_STRUCT_RETURN */
2801 /* Figure out the amount to which the stack should be aligned. */
2802 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
2803 if (fndecl)
2805 struct cgraph_rtl_info *i = cgraph_node::rtl_info (fndecl);
2806 /* Without automatic stack alignment, we can't increase preferred
2807 stack boundary. With automatic stack alignment, it is
2808 unnecessary since unless we can guarantee that all callers will
2809 align the outgoing stack properly, callee has to align its
2810 stack anyway. */
2811 if (i
2812 && i->preferred_incoming_stack_boundary
2813 && i->preferred_incoming_stack_boundary < preferred_stack_boundary)
2814 preferred_stack_boundary = i->preferred_incoming_stack_boundary;
2817 /* Operand 0 is a pointer-to-function; get the type of the function. */
2818 funtype = TREE_TYPE (addr);
2819 gcc_assert (POINTER_TYPE_P (funtype));
2820 funtype = TREE_TYPE (funtype);
2822 /* Count whether there are actual complex arguments that need to be split
2823 into their real and imaginary parts. Munge the type_arg_types
2824 appropriately here as well. */
2825 if (targetm.calls.split_complex_arg)
2827 call_expr_arg_iterator iter;
2828 tree arg;
2829 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2831 tree type = TREE_TYPE (arg);
2832 if (type && TREE_CODE (type) == COMPLEX_TYPE
2833 && targetm.calls.split_complex_arg (type))
2834 num_complex_actuals++;
2836 type_arg_types = split_complex_types (TYPE_ARG_TYPES (funtype));
2838 else
2839 type_arg_types = TYPE_ARG_TYPES (funtype);
2841 if (flags & ECF_MAY_BE_ALLOCA)
2842 cfun->calls_alloca = 1;
2844 /* If struct_value_rtx is 0, it means pass the address
2845 as if it were an extra parameter. Put the argument expression
2846 in structure_value_addr_value. */
2847 if (structure_value_addr && struct_value == 0)
2849 /* If structure_value_addr is a REG other than
2850 virtual_outgoing_args_rtx, we can use always use it. If it
2851 is not a REG, we must always copy it into a register.
2852 If it is virtual_outgoing_args_rtx, we must copy it to another
2853 register in some cases. */
2854 rtx temp = (!REG_P (structure_value_addr)
2855 || (ACCUMULATE_OUTGOING_ARGS
2856 && stack_arg_under_construction
2857 && structure_value_addr == virtual_outgoing_args_rtx)
2858 ? copy_addr_to_reg (convert_memory_address
2859 (Pmode, structure_value_addr))
2860 : structure_value_addr);
2862 structure_value_addr_value =
2863 make_tree (build_pointer_type (TREE_TYPE (funtype)), temp);
2864 structure_value_addr_parm = CALL_WITH_BOUNDS_P (exp) ? 2 : 1;
2867 /* Count the arguments and set NUM_ACTUALS. */
2868 num_actuals =
2869 call_expr_nargs (exp) + num_complex_actuals + structure_value_addr_parm;
2871 /* Compute number of named args.
2872 First, do a raw count of the args for INIT_CUMULATIVE_ARGS. */
2874 if (type_arg_types != 0)
2875 n_named_args
2876 = (list_length (type_arg_types)
2877 /* Count the struct value address, if it is passed as a parm. */
2878 + structure_value_addr_parm);
2879 else
2880 /* If we know nothing, treat all args as named. */
2881 n_named_args = num_actuals;
2883 /* Start updating where the next arg would go.
2885 On some machines (such as the PA) indirect calls have a different
2886 calling convention than normal calls. The fourth argument in
2887 INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
2888 or not. */
2889 INIT_CUMULATIVE_ARGS (args_so_far_v, funtype, NULL_RTX, fndecl, n_named_args);
2890 args_so_far = pack_cumulative_args (&args_so_far_v);
2892 /* Now possibly adjust the number of named args.
2893 Normally, don't include the last named arg if anonymous args follow.
2894 We do include the last named arg if
2895 targetm.calls.strict_argument_naming() returns nonzero.
2896 (If no anonymous args follow, the result of list_length is actually
2897 one too large. This is harmless.)
2899 If targetm.calls.pretend_outgoing_varargs_named() returns
2900 nonzero, and targetm.calls.strict_argument_naming() returns zero,
2901 this machine will be able to place unnamed args that were passed
2902 in registers into the stack. So treat all args as named. This
2903 allows the insns emitting for a specific argument list to be
2904 independent of the function declaration.
2906 If targetm.calls.pretend_outgoing_varargs_named() returns zero,
2907 we do not have any reliable way to pass unnamed args in
2908 registers, so we must force them into memory. */
2910 if (type_arg_types != 0
2911 && targetm.calls.strict_argument_naming (args_so_far))
2913 else if (type_arg_types != 0
2914 && ! targetm.calls.pretend_outgoing_varargs_named (args_so_far))
2915 /* Don't include the last named arg. */
2916 --n_named_args;
2917 else
2918 /* Treat all args as named. */
2919 n_named_args = num_actuals;
2921 /* Make a vector to hold all the information about each arg. */
2922 args = XALLOCAVEC (struct arg_data, num_actuals);
2923 memset (args, 0, num_actuals * sizeof (struct arg_data));
2925 /* Build up entries in the ARGS array, compute the size of the
2926 arguments into ARGS_SIZE, etc. */
2927 initialize_argument_information (num_actuals, args, &args_size,
2928 n_named_args, exp,
2929 structure_value_addr_value, fndecl, fntype,
2930 args_so_far, reg_parm_stack_space,
2931 &old_stack_level, &old_pending_adj,
2932 &must_preallocate, &flags,
2933 &try_tail_call, CALL_FROM_THUNK_P (exp));
2935 if (args_size.var)
2936 must_preallocate = 1;
2938 /* Now make final decision about preallocating stack space. */
2939 must_preallocate = finalize_must_preallocate (must_preallocate,
2940 num_actuals, args,
2941 &args_size);
2943 /* If the structure value address will reference the stack pointer, we
2944 must stabilize it. We don't need to do this if we know that we are
2945 not going to adjust the stack pointer in processing this call. */
2947 if (structure_value_addr
2948 && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
2949 || reg_mentioned_p (virtual_outgoing_args_rtx,
2950 structure_value_addr))
2951 && (args_size.var
2952 || (!ACCUMULATE_OUTGOING_ARGS && args_size.constant)))
2953 structure_value_addr = copy_to_reg (structure_value_addr);
2955 /* Tail calls can make things harder to debug, and we've traditionally
2956 pushed these optimizations into -O2. Don't try if we're already
2957 expanding a call, as that means we're an argument. Don't try if
2958 there's cleanups, as we know there's code to follow the call. */
2960 if (currently_expanding_call++ != 0
2961 || !flag_optimize_sibling_calls
2962 || args_size.var
2963 || dbg_cnt (tail_call) == false)
2964 try_tail_call = 0;
2966 /* If the user has marked the function as requiring tail-call
2967 optimization, attempt it. */
2968 if (must_tail_call)
2969 try_tail_call = 1;
2971 /* Rest of purposes for tail call optimizations to fail. */
2972 if (try_tail_call)
2973 try_tail_call = can_implement_as_sibling_call_p (exp,
2974 structure_value_addr,
2975 funtype,
2976 reg_parm_stack_space,
2977 fndecl,
2978 flags, addr, args_size);
2980 /* Check if caller and callee disagree in promotion of function
2981 return value. */
2982 if (try_tail_call)
2984 machine_mode caller_mode, caller_promoted_mode;
2985 machine_mode callee_mode, callee_promoted_mode;
2986 int caller_unsignedp, callee_unsignedp;
2987 tree caller_res = DECL_RESULT (current_function_decl);
2989 caller_unsignedp = TYPE_UNSIGNED (TREE_TYPE (caller_res));
2990 caller_mode = DECL_MODE (caller_res);
2991 callee_unsignedp = TYPE_UNSIGNED (TREE_TYPE (funtype));
2992 callee_mode = TYPE_MODE (TREE_TYPE (funtype));
2993 caller_promoted_mode
2994 = promote_function_mode (TREE_TYPE (caller_res), caller_mode,
2995 &caller_unsignedp,
2996 TREE_TYPE (current_function_decl), 1);
2997 callee_promoted_mode
2998 = promote_function_mode (TREE_TYPE (funtype), callee_mode,
2999 &callee_unsignedp,
3000 funtype, 1);
3001 if (caller_mode != VOIDmode
3002 && (caller_promoted_mode != callee_promoted_mode
3003 || ((caller_mode != caller_promoted_mode
3004 || callee_mode != callee_promoted_mode)
3005 && (caller_unsignedp != callee_unsignedp
3006 || GET_MODE_BITSIZE (caller_mode)
3007 < GET_MODE_BITSIZE (callee_mode)))))
3009 try_tail_call = 0;
3010 maybe_complain_about_tail_call (exp,
3011 "caller and callee disagree in"
3012 " promotion of function"
3013 " return value");
3017 /* Ensure current function's preferred stack boundary is at least
3018 what we need. Stack alignment may also increase preferred stack
3019 boundary. */
3020 if (crtl->preferred_stack_boundary < preferred_stack_boundary)
3021 crtl->preferred_stack_boundary = preferred_stack_boundary;
3022 else
3023 preferred_stack_boundary = crtl->preferred_stack_boundary;
3025 preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
3027 /* We want to make two insn chains; one for a sibling call, the other
3028 for a normal call. We will select one of the two chains after
3029 initial RTL generation is complete. */
3030 for (pass = try_tail_call ? 0 : 1; pass < 2; pass++)
3032 int sibcall_failure = 0;
3033 /* We want to emit any pending stack adjustments before the tail
3034 recursion "call". That way we know any adjustment after the tail
3035 recursion call can be ignored if we indeed use the tail
3036 call expansion. */
3037 saved_pending_stack_adjust save;
3038 rtx_insn *insns, *before_call, *after_args;
3039 rtx next_arg_reg;
3041 if (pass == 0)
3043 /* State variables we need to save and restore between
3044 iterations. */
3045 save_pending_stack_adjust (&save);
3047 if (pass)
3048 flags &= ~ECF_SIBCALL;
3049 else
3050 flags |= ECF_SIBCALL;
3052 /* Other state variables that we must reinitialize each time
3053 through the loop (that are not initialized by the loop itself). */
3054 argblock = 0;
3055 call_fusage = 0;
3057 /* Start a new sequence for the normal call case.
3059 From this point on, if the sibling call fails, we want to set
3060 sibcall_failure instead of continuing the loop. */
3061 start_sequence ();
3063 /* Don't let pending stack adjusts add up to too much.
3064 Also, do all pending adjustments now if there is any chance
3065 this might be a call to alloca or if we are expanding a sibling
3066 call sequence.
3067 Also do the adjustments before a throwing call, otherwise
3068 exception handling can fail; PR 19225. */
3069 if (pending_stack_adjust >= 32
3070 || (pending_stack_adjust > 0
3071 && (flags & ECF_MAY_BE_ALLOCA))
3072 || (pending_stack_adjust > 0
3073 && flag_exceptions && !(flags & ECF_NOTHROW))
3074 || pass == 0)
3075 do_pending_stack_adjust ();
3077 /* Precompute any arguments as needed. */
3078 if (pass)
3079 precompute_arguments (num_actuals, args);
3081 /* Now we are about to start emitting insns that can be deleted
3082 if a libcall is deleted. */
3083 if (pass && (flags & ECF_MALLOC))
3084 start_sequence ();
3086 if (pass == 0
3087 && crtl->stack_protect_guard
3088 && targetm.stack_protect_runtime_enabled_p ())
3089 stack_protect_epilogue ();
3091 adjusted_args_size = args_size;
3092 /* Compute the actual size of the argument block required. The variable
3093 and constant sizes must be combined, the size may have to be rounded,
3094 and there may be a minimum required size. When generating a sibcall
3095 pattern, do not round up, since we'll be re-using whatever space our
3096 caller provided. */
3097 unadjusted_args_size
3098 = compute_argument_block_size (reg_parm_stack_space,
3099 &adjusted_args_size,
3100 fndecl, fntype,
3101 (pass == 0 ? 0
3102 : preferred_stack_boundary));
3104 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
3106 /* The argument block when performing a sibling call is the
3107 incoming argument block. */
3108 if (pass == 0)
3110 argblock = crtl->args.internal_arg_pointer;
3111 if (STACK_GROWS_DOWNWARD)
3112 argblock
3113 = plus_constant (Pmode, argblock, crtl->args.pretend_args_size);
3114 else
3115 argblock
3116 = plus_constant (Pmode, argblock, -crtl->args.pretend_args_size);
3118 stored_args_map = sbitmap_alloc (args_size.constant);
3119 bitmap_clear (stored_args_map);
3122 /* If we have no actual push instructions, or shouldn't use them,
3123 make space for all args right now. */
3124 else if (adjusted_args_size.var != 0)
3126 if (old_stack_level == 0)
3128 emit_stack_save (SAVE_BLOCK, &old_stack_level);
3129 old_stack_pointer_delta = stack_pointer_delta;
3130 old_pending_adj = pending_stack_adjust;
3131 pending_stack_adjust = 0;
3132 /* stack_arg_under_construction says whether a stack arg is
3133 being constructed at the old stack level. Pushing the stack
3134 gets a clean outgoing argument block. */
3135 old_stack_arg_under_construction = stack_arg_under_construction;
3136 stack_arg_under_construction = 0;
3138 argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
3139 if (flag_stack_usage_info)
3140 current_function_has_unbounded_dynamic_stack_size = 1;
3142 else
3144 /* Note that we must go through the motions of allocating an argument
3145 block even if the size is zero because we may be storing args
3146 in the area reserved for register arguments, which may be part of
3147 the stack frame. */
3149 int needed = adjusted_args_size.constant;
3151 /* Store the maximum argument space used. It will be pushed by
3152 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
3153 checking). */
3155 if (needed > crtl->outgoing_args_size)
3156 crtl->outgoing_args_size = needed;
3158 if (must_preallocate)
3160 if (ACCUMULATE_OUTGOING_ARGS)
3162 /* Since the stack pointer will never be pushed, it is
3163 possible for the evaluation of a parm to clobber
3164 something we have already written to the stack.
3165 Since most function calls on RISC machines do not use
3166 the stack, this is uncommon, but must work correctly.
3168 Therefore, we save any area of the stack that was already
3169 written and that we are using. Here we set up to do this
3170 by making a new stack usage map from the old one. The
3171 actual save will be done by store_one_arg.
3173 Another approach might be to try to reorder the argument
3174 evaluations to avoid this conflicting stack usage. */
3176 /* Since we will be writing into the entire argument area,
3177 the map must be allocated for its entire size, not just
3178 the part that is the responsibility of the caller. */
3179 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
3180 needed += reg_parm_stack_space;
3182 if (ARGS_GROW_DOWNWARD)
3183 highest_outgoing_arg_in_use
3184 = MAX (initial_highest_arg_in_use, needed + 1);
3185 else
3186 highest_outgoing_arg_in_use
3187 = MAX (initial_highest_arg_in_use, needed);
3189 free (stack_usage_map_buf);
3190 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
3191 stack_usage_map = stack_usage_map_buf;
3193 if (initial_highest_arg_in_use)
3194 memcpy (stack_usage_map, initial_stack_usage_map,
3195 initial_highest_arg_in_use);
3197 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
3198 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
3199 (highest_outgoing_arg_in_use
3200 - initial_highest_arg_in_use));
3201 needed = 0;
3203 /* The address of the outgoing argument list must not be
3204 copied to a register here, because argblock would be left
3205 pointing to the wrong place after the call to
3206 allocate_dynamic_stack_space below. */
3208 argblock = virtual_outgoing_args_rtx;
3210 else
3212 if (inhibit_defer_pop == 0)
3214 /* Try to reuse some or all of the pending_stack_adjust
3215 to get this space. */
3216 needed
3217 = (combine_pending_stack_adjustment_and_call
3218 (unadjusted_args_size,
3219 &adjusted_args_size,
3220 preferred_unit_stack_boundary));
3222 /* combine_pending_stack_adjustment_and_call computes
3223 an adjustment before the arguments are allocated.
3224 Account for them and see whether or not the stack
3225 needs to go up or down. */
3226 needed = unadjusted_args_size - needed;
3228 if (needed < 0)
3230 /* We're releasing stack space. */
3231 /* ??? We can avoid any adjustment at all if we're
3232 already aligned. FIXME. */
3233 pending_stack_adjust = -needed;
3234 do_pending_stack_adjust ();
3235 needed = 0;
3237 else
3238 /* We need to allocate space. We'll do that in
3239 push_block below. */
3240 pending_stack_adjust = 0;
3243 /* Special case this because overhead of `push_block' in
3244 this case is non-trivial. */
3245 if (needed == 0)
3246 argblock = virtual_outgoing_args_rtx;
3247 else
3249 argblock = push_block (GEN_INT (needed), 0, 0);
3250 if (ARGS_GROW_DOWNWARD)
3251 argblock = plus_constant (Pmode, argblock, needed);
3254 /* We only really need to call `copy_to_reg' in the case
3255 where push insns are going to be used to pass ARGBLOCK
3256 to a function call in ARGS. In that case, the stack
3257 pointer changes value from the allocation point to the
3258 call point, and hence the value of
3259 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
3260 as well always do it. */
3261 argblock = copy_to_reg (argblock);
3266 if (ACCUMULATE_OUTGOING_ARGS)
3268 /* The save/restore code in store_one_arg handles all
3269 cases except one: a constructor call (including a C
3270 function returning a BLKmode struct) to initialize
3271 an argument. */
3272 if (stack_arg_under_construction)
3274 rtx push_size
3275 = GEN_INT (adjusted_args_size.constant
3276 + (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype
3277 : TREE_TYPE (fndecl))) ? 0
3278 : reg_parm_stack_space));
3279 if (old_stack_level == 0)
3281 emit_stack_save (SAVE_BLOCK, &old_stack_level);
3282 old_stack_pointer_delta = stack_pointer_delta;
3283 old_pending_adj = pending_stack_adjust;
3284 pending_stack_adjust = 0;
3285 /* stack_arg_under_construction says whether a stack
3286 arg is being constructed at the old stack level.
3287 Pushing the stack gets a clean outgoing argument
3288 block. */
3289 old_stack_arg_under_construction
3290 = stack_arg_under_construction;
3291 stack_arg_under_construction = 0;
3292 /* Make a new map for the new argument list. */
3293 free (stack_usage_map_buf);
3294 stack_usage_map_buf = XCNEWVEC (char, highest_outgoing_arg_in_use);
3295 stack_usage_map = stack_usage_map_buf;
3296 highest_outgoing_arg_in_use = 0;
3298 /* We can pass TRUE as the 4th argument because we just
3299 saved the stack pointer and will restore it right after
3300 the call. */
3301 allocate_dynamic_stack_space (push_size, 0,
3302 BIGGEST_ALIGNMENT, true);
3305 /* If argument evaluation might modify the stack pointer,
3306 copy the address of the argument list to a register. */
3307 for (i = 0; i < num_actuals; i++)
3308 if (args[i].pass_on_stack)
3310 argblock = copy_addr_to_reg (argblock);
3311 break;
3315 compute_argument_addresses (args, argblock, num_actuals);
3317 /* Stack is properly aligned, pops can't safely be deferred during
3318 the evaluation of the arguments. */
3319 NO_DEFER_POP;
3321 /* Precompute all register parameters. It isn't safe to compute
3322 anything once we have started filling any specific hard regs.
3323 TLS symbols sometimes need a call to resolve. Precompute
3324 register parameters before any stack pointer manipulation
3325 to avoid unaligned stack in the called function. */
3326 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
3328 OK_DEFER_POP;
3330 /* Perform stack alignment before the first push (the last arg). */
3331 if (argblock == 0
3332 && adjusted_args_size.constant > reg_parm_stack_space
3333 && adjusted_args_size.constant != unadjusted_args_size)
3335 /* When the stack adjustment is pending, we get better code
3336 by combining the adjustments. */
3337 if (pending_stack_adjust
3338 && ! inhibit_defer_pop)
3340 pending_stack_adjust
3341 = (combine_pending_stack_adjustment_and_call
3342 (unadjusted_args_size,
3343 &adjusted_args_size,
3344 preferred_unit_stack_boundary));
3345 do_pending_stack_adjust ();
3347 else if (argblock == 0)
3348 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
3349 - unadjusted_args_size));
3351 /* Now that the stack is properly aligned, pops can't safely
3352 be deferred during the evaluation of the arguments. */
3353 NO_DEFER_POP;
3355 /* Record the maximum pushed stack space size. We need to delay
3356 doing it this far to take into account the optimization done
3357 by combine_pending_stack_adjustment_and_call. */
3358 if (flag_stack_usage_info
3359 && !ACCUMULATE_OUTGOING_ARGS
3360 && pass
3361 && adjusted_args_size.var == 0)
3363 int pushed = adjusted_args_size.constant + pending_stack_adjust;
3364 if (pushed > current_function_pushed_stack_size)
3365 current_function_pushed_stack_size = pushed;
3368 funexp = rtx_for_function_call (fndecl, addr);
3370 if (CALL_EXPR_STATIC_CHAIN (exp))
3371 static_chain_value = expand_normal (CALL_EXPR_STATIC_CHAIN (exp));
3372 else
3373 static_chain_value = 0;
3375 #ifdef REG_PARM_STACK_SPACE
3376 /* Save the fixed argument area if it's part of the caller's frame and
3377 is clobbered by argument setup for this call. */
3378 if (ACCUMULATE_OUTGOING_ARGS && pass)
3379 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3380 &low_to_save, &high_to_save);
3381 #endif
3383 /* Now store (and compute if necessary) all non-register parms.
3384 These come before register parms, since they can require block-moves,
3385 which could clobber the registers used for register parms.
3386 Parms which have partial registers are not stored here,
3387 but we do preallocate space here if they want that. */
3389 for (i = 0; i < num_actuals; i++)
3391 /* Delay bounds until all other args are stored. */
3392 if (POINTER_BOUNDS_P (args[i].tree_value))
3393 continue;
3394 else if (args[i].reg == 0 || args[i].pass_on_stack)
3396 rtx_insn *before_arg = get_last_insn ();
3398 /* We don't allow passing huge (> 2^30 B) arguments
3399 by value. It would cause an overflow later on. */
3400 if (adjusted_args_size.constant
3401 >= (1 << (HOST_BITS_PER_INT - 2)))
3403 sorry ("passing too large argument on stack");
3404 continue;
3407 if (store_one_arg (&args[i], argblock, flags,
3408 adjusted_args_size.var != 0,
3409 reg_parm_stack_space)
3410 || (pass == 0
3411 && check_sibcall_argument_overlap (before_arg,
3412 &args[i], 1)))
3413 sibcall_failure = 1;
3416 if (args[i].stack)
3417 call_fusage
3418 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[i].tree_value)),
3419 gen_rtx_USE (VOIDmode, args[i].stack),
3420 call_fusage);
3423 /* If we have a parm that is passed in registers but not in memory
3424 and whose alignment does not permit a direct copy into registers,
3425 make a group of pseudos that correspond to each register that we
3426 will later fill. */
3427 if (STRICT_ALIGNMENT)
3428 store_unaligned_arguments_into_pseudos (args, num_actuals);
3430 /* Now store any partially-in-registers parm.
3431 This is the last place a block-move can happen. */
3432 if (reg_parm_seen)
3433 for (i = 0; i < num_actuals; i++)
3434 if (args[i].partial != 0 && ! args[i].pass_on_stack)
3436 rtx_insn *before_arg = get_last_insn ();
3438 /* On targets with weird calling conventions (e.g. PA) it's
3439 hard to ensure that all cases of argument overlap between
3440 stack and registers work. Play it safe and bail out. */
3441 if (ARGS_GROW_DOWNWARD && !STACK_GROWS_DOWNWARD)
3443 sibcall_failure = 1;
3444 break;
3447 if (store_one_arg (&args[i], argblock, flags,
3448 adjusted_args_size.var != 0,
3449 reg_parm_stack_space)
3450 || (pass == 0
3451 && check_sibcall_argument_overlap (before_arg,
3452 &args[i], 1)))
3453 sibcall_failure = 1;
3456 bool any_regs = false;
3457 for (i = 0; i < num_actuals; i++)
3458 if (args[i].reg != NULL_RTX)
3460 any_regs = true;
3461 targetm.calls.call_args (args[i].reg, funtype);
3463 if (!any_regs)
3464 targetm.calls.call_args (pc_rtx, funtype);
3466 /* Figure out the register where the value, if any, will come back. */
3467 valreg = 0;
3468 valbnd = 0;
3469 if (TYPE_MODE (rettype) != VOIDmode
3470 && ! structure_value_addr)
3472 if (pcc_struct_value)
3474 valreg = hard_function_value (build_pointer_type (rettype),
3475 fndecl, NULL, (pass == 0));
3476 if (CALL_WITH_BOUNDS_P (exp))
3477 valbnd = targetm.calls.
3478 chkp_function_value_bounds (build_pointer_type (rettype),
3479 fndecl, (pass == 0));
3481 else
3483 valreg = hard_function_value (rettype, fndecl, fntype,
3484 (pass == 0));
3485 if (CALL_WITH_BOUNDS_P (exp))
3486 valbnd = targetm.calls.chkp_function_value_bounds (rettype,
3487 fndecl,
3488 (pass == 0));
3491 /* If VALREG is a PARALLEL whose first member has a zero
3492 offset, use that. This is for targets such as m68k that
3493 return the same value in multiple places. */
3494 if (GET_CODE (valreg) == PARALLEL)
3496 rtx elem = XVECEXP (valreg, 0, 0);
3497 rtx where = XEXP (elem, 0);
3498 rtx offset = XEXP (elem, 1);
3499 if (offset == const0_rtx
3500 && GET_MODE (where) == GET_MODE (valreg))
3501 valreg = where;
3505 /* Store all bounds not passed in registers. */
3506 for (i = 0; i < num_actuals; i++)
3508 if (POINTER_BOUNDS_P (args[i].tree_value)
3509 && !args[i].reg)
3510 store_bounds (&args[i],
3511 args[i].pointer_arg == -1
3512 ? NULL
3513 : &args[args[i].pointer_arg]);
3516 /* If register arguments require space on the stack and stack space
3517 was not preallocated, allocate stack space here for arguments
3518 passed in registers. */
3519 if (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
3520 && !ACCUMULATE_OUTGOING_ARGS
3521 && must_preallocate == 0 && reg_parm_stack_space > 0)
3522 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
3524 /* Pass the function the address in which to return a
3525 structure value. */
3526 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
3528 structure_value_addr
3529 = convert_memory_address (Pmode, structure_value_addr);
3530 emit_move_insn (struct_value,
3531 force_reg (Pmode,
3532 force_operand (structure_value_addr,
3533 NULL_RTX)));
3535 if (REG_P (struct_value))
3536 use_reg (&call_fusage, struct_value);
3539 after_args = get_last_insn ();
3540 funexp = prepare_call_address (fndecl ? fndecl : fntype, funexp,
3541 static_chain_value, &call_fusage,
3542 reg_parm_seen, flags);
3544 load_register_parameters (args, num_actuals, &call_fusage, flags,
3545 pass == 0, &sibcall_failure);
3547 /* Save a pointer to the last insn before the call, so that we can
3548 later safely search backwards to find the CALL_INSN. */
3549 before_call = get_last_insn ();
3551 /* Set up next argument register. For sibling calls on machines
3552 with register windows this should be the incoming register. */
3553 if (pass == 0)
3554 next_arg_reg = targetm.calls.function_incoming_arg (args_so_far,
3555 VOIDmode,
3556 void_type_node,
3557 true);
3558 else
3559 next_arg_reg = targetm.calls.function_arg (args_so_far,
3560 VOIDmode, void_type_node,
3561 true);
3563 if (pass == 1 && (return_flags & ERF_RETURNS_ARG))
3565 int arg_nr = return_flags & ERF_RETURN_ARG_MASK;
3566 arg_nr = num_actuals - arg_nr - 1;
3567 if (arg_nr >= 0
3568 && arg_nr < num_actuals
3569 && args[arg_nr].reg
3570 && valreg
3571 && REG_P (valreg)
3572 && GET_MODE (args[arg_nr].reg) == GET_MODE (valreg))
3573 call_fusage
3574 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[arg_nr].tree_value)),
3575 gen_rtx_SET (valreg, args[arg_nr].reg),
3576 call_fusage);
3578 /* All arguments and registers used for the call must be set up by
3579 now! */
3581 /* Stack must be properly aligned now. */
3582 gcc_assert (!pass
3583 || !(stack_pointer_delta % preferred_unit_stack_boundary));
3585 /* Generate the actual call instruction. */
3586 emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
3587 adjusted_args_size.constant, struct_value_size,
3588 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
3589 flags, args_so_far);
3591 if (flag_ipa_ra)
3593 rtx_call_insn *last;
3594 rtx datum = NULL_RTX;
3595 if (fndecl != NULL_TREE)
3597 datum = XEXP (DECL_RTL (fndecl), 0);
3598 gcc_assert (datum != NULL_RTX
3599 && GET_CODE (datum) == SYMBOL_REF);
3601 last = last_call_insn ();
3602 add_reg_note (last, REG_CALL_DECL, datum);
3605 /* If the call setup or the call itself overlaps with anything
3606 of the argument setup we probably clobbered our call address.
3607 In that case we can't do sibcalls. */
3608 if (pass == 0
3609 && check_sibcall_argument_overlap (after_args, 0, 0))
3610 sibcall_failure = 1;
3612 /* If a non-BLKmode value is returned at the most significant end
3613 of a register, shift the register right by the appropriate amount
3614 and update VALREG accordingly. BLKmode values are handled by the
3615 group load/store machinery below. */
3616 if (!structure_value_addr
3617 && !pcc_struct_value
3618 && TYPE_MODE (rettype) != VOIDmode
3619 && TYPE_MODE (rettype) != BLKmode
3620 && REG_P (valreg)
3621 && targetm.calls.return_in_msb (rettype))
3623 if (shift_return_value (TYPE_MODE (rettype), false, valreg))
3624 sibcall_failure = 1;
3625 valreg = gen_rtx_REG (TYPE_MODE (rettype), REGNO (valreg));
3628 if (pass && (flags & ECF_MALLOC))
3630 rtx temp = gen_reg_rtx (GET_MODE (valreg));
3631 rtx_insn *last, *insns;
3633 /* The return value from a malloc-like function is a pointer. */
3634 if (TREE_CODE (rettype) == POINTER_TYPE)
3635 mark_reg_pointer (temp, MALLOC_ABI_ALIGNMENT);
3637 emit_move_insn (temp, valreg);
3639 /* The return value from a malloc-like function can not alias
3640 anything else. */
3641 last = get_last_insn ();
3642 add_reg_note (last, REG_NOALIAS, temp);
3644 /* Write out the sequence. */
3645 insns = get_insns ();
3646 end_sequence ();
3647 emit_insn (insns);
3648 valreg = temp;
3651 /* For calls to `setjmp', etc., inform
3652 function.c:setjmp_warnings that it should complain if
3653 nonvolatile values are live. For functions that cannot
3654 return, inform flow that control does not fall through. */
3656 if ((flags & ECF_NORETURN) || pass == 0)
3658 /* The barrier must be emitted
3659 immediately after the CALL_INSN. Some ports emit more
3660 than just a CALL_INSN above, so we must search for it here. */
3662 rtx_insn *last = get_last_insn ();
3663 while (!CALL_P (last))
3665 last = PREV_INSN (last);
3666 /* There was no CALL_INSN? */
3667 gcc_assert (last != before_call);
3670 emit_barrier_after (last);
3672 /* Stack adjustments after a noreturn call are dead code.
3673 However when NO_DEFER_POP is in effect, we must preserve
3674 stack_pointer_delta. */
3675 if (inhibit_defer_pop == 0)
3677 stack_pointer_delta = old_stack_allocated;
3678 pending_stack_adjust = 0;
3682 /* If value type not void, return an rtx for the value. */
3684 if (TYPE_MODE (rettype) == VOIDmode
3685 || ignore)
3686 target = const0_rtx;
3687 else if (structure_value_addr)
3689 if (target == 0 || !MEM_P (target))
3691 target
3692 = gen_rtx_MEM (TYPE_MODE (rettype),
3693 memory_address (TYPE_MODE (rettype),
3694 structure_value_addr));
3695 set_mem_attributes (target, rettype, 1);
3698 else if (pcc_struct_value)
3700 /* This is the special C++ case where we need to
3701 know what the true target was. We take care to
3702 never use this value more than once in one expression. */
3703 target = gen_rtx_MEM (TYPE_MODE (rettype),
3704 copy_to_reg (valreg));
3705 set_mem_attributes (target, rettype, 1);
3707 /* Handle calls that return values in multiple non-contiguous locations.
3708 The Irix 6 ABI has examples of this. */
3709 else if (GET_CODE (valreg) == PARALLEL)
3711 if (target == 0)
3712 target = emit_group_move_into_temps (valreg);
3713 else if (rtx_equal_p (target, valreg))
3715 else if (GET_CODE (target) == PARALLEL)
3716 /* Handle the result of a emit_group_move_into_temps
3717 call in the previous pass. */
3718 emit_group_move (target, valreg);
3719 else
3720 emit_group_store (target, valreg, rettype,
3721 int_size_in_bytes (rettype));
3723 else if (target
3724 && GET_MODE (target) == TYPE_MODE (rettype)
3725 && GET_MODE (target) == GET_MODE (valreg))
3727 bool may_overlap = false;
3729 /* We have to copy a return value in a CLASS_LIKELY_SPILLED hard
3730 reg to a plain register. */
3731 if (!REG_P (target) || HARD_REGISTER_P (target))
3732 valreg = avoid_likely_spilled_reg (valreg);
3734 /* If TARGET is a MEM in the argument area, and we have
3735 saved part of the argument area, then we can't store
3736 directly into TARGET as it may get overwritten when we
3737 restore the argument save area below. Don't work too
3738 hard though and simply force TARGET to a register if it
3739 is a MEM; the optimizer is quite likely to sort it out. */
3740 if (ACCUMULATE_OUTGOING_ARGS && pass && MEM_P (target))
3741 for (i = 0; i < num_actuals; i++)
3742 if (args[i].save_area)
3744 may_overlap = true;
3745 break;
3748 if (may_overlap)
3749 target = copy_to_reg (valreg);
3750 else
3752 /* TARGET and VALREG cannot be equal at this point
3753 because the latter would not have
3754 REG_FUNCTION_VALUE_P true, while the former would if
3755 it were referring to the same register.
3757 If they refer to the same register, this move will be
3758 a no-op, except when function inlining is being
3759 done. */
3760 emit_move_insn (target, valreg);
3762 /* If we are setting a MEM, this code must be executed.
3763 Since it is emitted after the call insn, sibcall
3764 optimization cannot be performed in that case. */
3765 if (MEM_P (target))
3766 sibcall_failure = 1;
3769 else
3770 target = copy_to_reg (avoid_likely_spilled_reg (valreg));
3772 /* If we promoted this return value, make the proper SUBREG.
3773 TARGET might be const0_rtx here, so be careful. */
3774 if (REG_P (target)
3775 && TYPE_MODE (rettype) != BLKmode
3776 && GET_MODE (target) != TYPE_MODE (rettype))
3778 tree type = rettype;
3779 int unsignedp = TYPE_UNSIGNED (type);
3780 int offset = 0;
3781 machine_mode pmode;
3783 /* Ensure we promote as expected, and get the new unsignedness. */
3784 pmode = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
3785 funtype, 1);
3786 gcc_assert (GET_MODE (target) == pmode);
3788 if ((WORDS_BIG_ENDIAN || BYTES_BIG_ENDIAN)
3789 && (GET_MODE_SIZE (GET_MODE (target))
3790 > GET_MODE_SIZE (TYPE_MODE (type))))
3792 offset = GET_MODE_SIZE (GET_MODE (target))
3793 - GET_MODE_SIZE (TYPE_MODE (type));
3794 if (! BYTES_BIG_ENDIAN)
3795 offset = (offset / UNITS_PER_WORD) * UNITS_PER_WORD;
3796 else if (! WORDS_BIG_ENDIAN)
3797 offset %= UNITS_PER_WORD;
3800 target = gen_rtx_SUBREG (TYPE_MODE (type), target, offset);
3801 SUBREG_PROMOTED_VAR_P (target) = 1;
3802 SUBREG_PROMOTED_SET (target, unsignedp);
3805 /* If size of args is variable or this was a constructor call for a stack
3806 argument, restore saved stack-pointer value. */
3808 if (old_stack_level)
3810 rtx_insn *prev = get_last_insn ();
3812 emit_stack_restore (SAVE_BLOCK, old_stack_level);
3813 stack_pointer_delta = old_stack_pointer_delta;
3815 fixup_args_size_notes (prev, get_last_insn (), stack_pointer_delta);
3817 pending_stack_adjust = old_pending_adj;
3818 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
3819 stack_arg_under_construction = old_stack_arg_under_construction;
3820 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3821 stack_usage_map = initial_stack_usage_map;
3822 sibcall_failure = 1;
3824 else if (ACCUMULATE_OUTGOING_ARGS && pass)
3826 #ifdef REG_PARM_STACK_SPACE
3827 if (save_area)
3828 restore_fixed_argument_area (save_area, argblock,
3829 high_to_save, low_to_save);
3830 #endif
3832 /* If we saved any argument areas, restore them. */
3833 for (i = 0; i < num_actuals; i++)
3834 if (args[i].save_area)
3836 machine_mode save_mode = GET_MODE (args[i].save_area);
3837 rtx stack_area
3838 = gen_rtx_MEM (save_mode,
3839 memory_address (save_mode,
3840 XEXP (args[i].stack_slot, 0)));
3842 if (save_mode != BLKmode)
3843 emit_move_insn (stack_area, args[i].save_area);
3844 else
3845 emit_block_move (stack_area, args[i].save_area,
3846 GEN_INT (args[i].locate.size.constant),
3847 BLOCK_OP_CALL_PARM);
3850 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3851 stack_usage_map = initial_stack_usage_map;
3854 /* If this was alloca, record the new stack level. */
3855 if (flags & ECF_MAY_BE_ALLOCA)
3856 record_new_stack_level ();
3858 /* Free up storage we no longer need. */
3859 for (i = 0; i < num_actuals; ++i)
3860 free (args[i].aligned_regs);
3862 targetm.calls.end_call_args ();
3864 insns = get_insns ();
3865 end_sequence ();
3867 if (pass == 0)
3869 tail_call_insns = insns;
3871 /* Restore the pending stack adjustment now that we have
3872 finished generating the sibling call sequence. */
3874 restore_pending_stack_adjust (&save);
3876 /* Prepare arg structure for next iteration. */
3877 for (i = 0; i < num_actuals; i++)
3879 args[i].value = 0;
3880 args[i].aligned_regs = 0;
3881 args[i].stack = 0;
3884 sbitmap_free (stored_args_map);
3885 internal_arg_pointer_exp_state.scan_start = NULL;
3886 internal_arg_pointer_exp_state.cache.release ();
3888 else
3890 normal_call_insns = insns;
3892 /* Verify that we've deallocated all the stack we used. */
3893 gcc_assert ((flags & ECF_NORETURN)
3894 || (old_stack_allocated
3895 == stack_pointer_delta - pending_stack_adjust));
3898 /* If something prevents making this a sibling call,
3899 zero out the sequence. */
3900 if (sibcall_failure)
3901 tail_call_insns = NULL;
3902 else
3903 break;
3906 /* If tail call production succeeded, we need to remove REG_EQUIV notes on
3907 arguments too, as argument area is now clobbered by the call. */
3908 if (tail_call_insns)
3910 emit_insn (tail_call_insns);
3911 crtl->tail_call_emit = true;
3913 else
3915 emit_insn (normal_call_insns);
3916 if (try_tail_call)
3917 /* Ideally we'd emit a message for all of the ways that it could
3918 have failed. */
3919 maybe_complain_about_tail_call (exp, "tail call production failed");
3922 currently_expanding_call--;
3924 free (stack_usage_map_buf);
3926 /* Join result with returned bounds so caller may use them if needed. */
3927 target = chkp_join_splitted_slot (target, valbnd);
3929 return target;
3932 /* A sibling call sequence invalidates any REG_EQUIV notes made for
3933 this function's incoming arguments.
3935 At the start of RTL generation we know the only REG_EQUIV notes
3936 in the rtl chain are those for incoming arguments, so we can look
3937 for REG_EQUIV notes between the start of the function and the
3938 NOTE_INSN_FUNCTION_BEG.
3940 This is (slight) overkill. We could keep track of the highest
3941 argument we clobber and be more selective in removing notes, but it
3942 does not seem to be worth the effort. */
3944 void
3945 fixup_tail_calls (void)
3947 rtx_insn *insn;
3949 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3951 rtx note;
3953 /* There are never REG_EQUIV notes for the incoming arguments
3954 after the NOTE_INSN_FUNCTION_BEG note, so stop if we see it. */
3955 if (NOTE_P (insn)
3956 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3957 break;
3959 note = find_reg_note (insn, REG_EQUIV, 0);
3960 if (note)
3961 remove_note (insn, note);
3962 note = find_reg_note (insn, REG_EQUIV, 0);
3963 gcc_assert (!note);
3967 /* Traverse a list of TYPES and expand all complex types into their
3968 components. */
3969 static tree
3970 split_complex_types (tree types)
3972 tree p;
3974 /* Before allocating memory, check for the common case of no complex. */
3975 for (p = types; p; p = TREE_CHAIN (p))
3977 tree type = TREE_VALUE (p);
3978 if (TREE_CODE (type) == COMPLEX_TYPE
3979 && targetm.calls.split_complex_arg (type))
3980 goto found;
3982 return types;
3984 found:
3985 types = copy_list (types);
3987 for (p = types; p; p = TREE_CHAIN (p))
3989 tree complex_type = TREE_VALUE (p);
3991 if (TREE_CODE (complex_type) == COMPLEX_TYPE
3992 && targetm.calls.split_complex_arg (complex_type))
3994 tree next, imag;
3996 /* Rewrite complex type with component type. */
3997 TREE_VALUE (p) = TREE_TYPE (complex_type);
3998 next = TREE_CHAIN (p);
4000 /* Add another component type for the imaginary part. */
4001 imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
4002 TREE_CHAIN (p) = imag;
4003 TREE_CHAIN (imag) = next;
4005 /* Skip the newly created node. */
4006 p = TREE_CHAIN (p);
4010 return types;
4013 /* Output a library call to function FUN (a SYMBOL_REF rtx).
4014 The RETVAL parameter specifies whether return value needs to be saved, other
4015 parameters are documented in the emit_library_call function below. */
4017 static rtx
4018 emit_library_call_value_1 (int retval, rtx orgfun, rtx value,
4019 enum libcall_type fn_type,
4020 machine_mode outmode, int nargs, va_list p)
4022 /* Total size in bytes of all the stack-parms scanned so far. */
4023 struct args_size args_size;
4024 /* Size of arguments before any adjustments (such as rounding). */
4025 struct args_size original_args_size;
4026 int argnum;
4027 rtx fun;
4028 /* Todo, choose the correct decl type of orgfun. Sadly this information
4029 isn't present here, so we default to native calling abi here. */
4030 tree fndecl ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
4031 tree fntype ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
4032 int count;
4033 rtx argblock = 0;
4034 CUMULATIVE_ARGS args_so_far_v;
4035 cumulative_args_t args_so_far;
4036 struct arg
4038 rtx value;
4039 machine_mode mode;
4040 rtx reg;
4041 int partial;
4042 struct locate_and_pad_arg_data locate;
4043 rtx save_area;
4045 struct arg *argvec;
4046 int old_inhibit_defer_pop = inhibit_defer_pop;
4047 rtx call_fusage = 0;
4048 rtx mem_value = 0;
4049 rtx valreg;
4050 int pcc_struct_value = 0;
4051 int struct_value_size = 0;
4052 int flags;
4053 int reg_parm_stack_space = 0;
4054 int needed;
4055 rtx_insn *before_call;
4056 bool have_push_fusage;
4057 tree tfom; /* type_for_mode (outmode, 0) */
4059 #ifdef REG_PARM_STACK_SPACE
4060 /* Define the boundary of the register parm stack space that needs to be
4061 save, if any. */
4062 int low_to_save = 0, high_to_save = 0;
4063 rtx save_area = 0; /* Place that it is saved. */
4064 #endif
4066 /* Size of the stack reserved for parameter registers. */
4067 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
4068 char *initial_stack_usage_map = stack_usage_map;
4069 char *stack_usage_map_buf = NULL;
4071 rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
4073 #ifdef REG_PARM_STACK_SPACE
4074 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
4075 #endif
4077 /* By default, library functions cannot throw. */
4078 flags = ECF_NOTHROW;
4080 switch (fn_type)
4082 case LCT_NORMAL:
4083 break;
4084 case LCT_CONST:
4085 flags |= ECF_CONST;
4086 break;
4087 case LCT_PURE:
4088 flags |= ECF_PURE;
4089 break;
4090 case LCT_NORETURN:
4091 flags |= ECF_NORETURN;
4092 break;
4093 case LCT_THROW:
4094 flags &= ~ECF_NOTHROW;
4095 break;
4096 case LCT_RETURNS_TWICE:
4097 flags = ECF_RETURNS_TWICE;
4098 break;
4100 fun = orgfun;
4102 /* Ensure current function's preferred stack boundary is at least
4103 what we need. */
4104 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
4105 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
4107 /* If this kind of value comes back in memory,
4108 decide where in memory it should come back. */
4109 if (outmode != VOIDmode)
4111 tfom = lang_hooks.types.type_for_mode (outmode, 0);
4112 if (aggregate_value_p (tfom, 0))
4114 #ifdef PCC_STATIC_STRUCT_RETURN
4115 rtx pointer_reg
4116 = hard_function_value (build_pointer_type (tfom), 0, 0, 0);
4117 mem_value = gen_rtx_MEM (outmode, pointer_reg);
4118 pcc_struct_value = 1;
4119 if (value == 0)
4120 value = gen_reg_rtx (outmode);
4121 #else /* not PCC_STATIC_STRUCT_RETURN */
4122 struct_value_size = GET_MODE_SIZE (outmode);
4123 if (value != 0 && MEM_P (value))
4124 mem_value = value;
4125 else
4126 mem_value = assign_temp (tfom, 1, 1);
4127 #endif
4128 /* This call returns a big structure. */
4129 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
4132 else
4133 tfom = void_type_node;
4135 /* ??? Unfinished: must pass the memory address as an argument. */
4137 /* Copy all the libcall-arguments out of the varargs data
4138 and into a vector ARGVEC.
4140 Compute how to pass each argument. We only support a very small subset
4141 of the full argument passing conventions to limit complexity here since
4142 library functions shouldn't have many args. */
4144 argvec = XALLOCAVEC (struct arg, nargs + 1);
4145 memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
4147 #ifdef INIT_CUMULATIVE_LIBCALL_ARGS
4148 INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far_v, outmode, fun);
4149 #else
4150 INIT_CUMULATIVE_ARGS (args_so_far_v, NULL_TREE, fun, 0, nargs);
4151 #endif
4152 args_so_far = pack_cumulative_args (&args_so_far_v);
4154 args_size.constant = 0;
4155 args_size.var = 0;
4157 count = 0;
4159 push_temp_slots ();
4161 /* If there's a structure value address to be passed,
4162 either pass it in the special place, or pass it as an extra argument. */
4163 if (mem_value && struct_value == 0 && ! pcc_struct_value)
4165 rtx addr = XEXP (mem_value, 0);
4167 nargs++;
4169 /* Make sure it is a reasonable operand for a move or push insn. */
4170 if (!REG_P (addr) && !MEM_P (addr)
4171 && !(CONSTANT_P (addr)
4172 && targetm.legitimate_constant_p (Pmode, addr)))
4173 addr = force_operand (addr, NULL_RTX);
4175 argvec[count].value = addr;
4176 argvec[count].mode = Pmode;
4177 argvec[count].partial = 0;
4179 argvec[count].reg = targetm.calls.function_arg (args_so_far,
4180 Pmode, NULL_TREE, true);
4181 gcc_assert (targetm.calls.arg_partial_bytes (args_so_far, Pmode,
4182 NULL_TREE, 1) == 0);
4184 locate_and_pad_parm (Pmode, NULL_TREE,
4185 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4187 #else
4188 argvec[count].reg != 0,
4189 #endif
4190 reg_parm_stack_space, 0,
4191 NULL_TREE, &args_size, &argvec[count].locate);
4193 if (argvec[count].reg == 0 || argvec[count].partial != 0
4194 || reg_parm_stack_space > 0)
4195 args_size.constant += argvec[count].locate.size.constant;
4197 targetm.calls.function_arg_advance (args_so_far, Pmode, (tree) 0, true);
4199 count++;
4202 for (; count < nargs; count++)
4204 rtx val = va_arg (p, rtx);
4205 machine_mode mode = (machine_mode) va_arg (p, int);
4206 int unsigned_p = 0;
4208 /* We cannot convert the arg value to the mode the library wants here;
4209 must do it earlier where we know the signedness of the arg. */
4210 gcc_assert (mode != BLKmode
4211 && (GET_MODE (val) == mode || GET_MODE (val) == VOIDmode));
4213 /* Make sure it is a reasonable operand for a move or push insn. */
4214 if (!REG_P (val) && !MEM_P (val)
4215 && !(CONSTANT_P (val) && targetm.legitimate_constant_p (mode, val)))
4216 val = force_operand (val, NULL_RTX);
4218 if (pass_by_reference (&args_so_far_v, mode, NULL_TREE, 1))
4220 rtx slot;
4221 int must_copy
4222 = !reference_callee_copied (&args_so_far_v, mode, NULL_TREE, 1);
4224 /* If this was a CONST function, it is now PURE since it now
4225 reads memory. */
4226 if (flags & ECF_CONST)
4228 flags &= ~ECF_CONST;
4229 flags |= ECF_PURE;
4232 if (MEM_P (val) && !must_copy)
4234 tree val_expr = MEM_EXPR (val);
4235 if (val_expr)
4236 mark_addressable (val_expr);
4237 slot = val;
4239 else
4241 slot = assign_temp (lang_hooks.types.type_for_mode (mode, 0),
4242 1, 1);
4243 emit_move_insn (slot, val);
4246 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4247 gen_rtx_USE (VOIDmode, slot),
4248 call_fusage);
4249 if (must_copy)
4250 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4251 gen_rtx_CLOBBER (VOIDmode,
4252 slot),
4253 call_fusage);
4255 mode = Pmode;
4256 val = force_operand (XEXP (slot, 0), NULL_RTX);
4259 mode = promote_function_mode (NULL_TREE, mode, &unsigned_p, NULL_TREE, 0);
4260 argvec[count].mode = mode;
4261 argvec[count].value = convert_modes (mode, GET_MODE (val), val, unsigned_p);
4262 argvec[count].reg = targetm.calls.function_arg (args_so_far, mode,
4263 NULL_TREE, true);
4265 argvec[count].partial
4266 = targetm.calls.arg_partial_bytes (args_so_far, mode, NULL_TREE, 1);
4268 if (argvec[count].reg == 0
4269 || argvec[count].partial != 0
4270 || reg_parm_stack_space > 0)
4272 locate_and_pad_parm (mode, NULL_TREE,
4273 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4275 #else
4276 argvec[count].reg != 0,
4277 #endif
4278 reg_parm_stack_space, argvec[count].partial,
4279 NULL_TREE, &args_size, &argvec[count].locate);
4280 args_size.constant += argvec[count].locate.size.constant;
4281 gcc_assert (!argvec[count].locate.size.var);
4283 #ifdef BLOCK_REG_PADDING
4284 else
4285 /* The argument is passed entirely in registers. See at which
4286 end it should be padded. */
4287 argvec[count].locate.where_pad =
4288 BLOCK_REG_PADDING (mode, NULL_TREE,
4289 GET_MODE_SIZE (mode) <= UNITS_PER_WORD);
4290 #endif
4292 targetm.calls.function_arg_advance (args_so_far, mode, (tree) 0, true);
4295 /* If this machine requires an external definition for library
4296 functions, write one out. */
4297 assemble_external_libcall (fun);
4299 original_args_size = args_size;
4300 args_size.constant = (((args_size.constant
4301 + stack_pointer_delta
4302 + STACK_BYTES - 1)
4303 / STACK_BYTES
4304 * STACK_BYTES)
4305 - stack_pointer_delta);
4307 args_size.constant = MAX (args_size.constant,
4308 reg_parm_stack_space);
4310 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4311 args_size.constant -= reg_parm_stack_space;
4313 if (args_size.constant > crtl->outgoing_args_size)
4314 crtl->outgoing_args_size = args_size.constant;
4316 if (flag_stack_usage_info && !ACCUMULATE_OUTGOING_ARGS)
4318 int pushed = args_size.constant + pending_stack_adjust;
4319 if (pushed > current_function_pushed_stack_size)
4320 current_function_pushed_stack_size = pushed;
4323 if (ACCUMULATE_OUTGOING_ARGS)
4325 /* Since the stack pointer will never be pushed, it is possible for
4326 the evaluation of a parm to clobber something we have already
4327 written to the stack. Since most function calls on RISC machines
4328 do not use the stack, this is uncommon, but must work correctly.
4330 Therefore, we save any area of the stack that was already written
4331 and that we are using. Here we set up to do this by making a new
4332 stack usage map from the old one.
4334 Another approach might be to try to reorder the argument
4335 evaluations to avoid this conflicting stack usage. */
4337 needed = args_size.constant;
4339 /* Since we will be writing into the entire argument area, the
4340 map must be allocated for its entire size, not just the part that
4341 is the responsibility of the caller. */
4342 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4343 needed += reg_parm_stack_space;
4345 if (ARGS_GROW_DOWNWARD)
4346 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
4347 needed + 1);
4348 else
4349 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use, needed);
4351 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
4352 stack_usage_map = stack_usage_map_buf;
4354 if (initial_highest_arg_in_use)
4355 memcpy (stack_usage_map, initial_stack_usage_map,
4356 initial_highest_arg_in_use);
4358 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
4359 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
4360 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
4361 needed = 0;
4363 /* We must be careful to use virtual regs before they're instantiated,
4364 and real regs afterwards. Loop optimization, for example, can create
4365 new libcalls after we've instantiated the virtual regs, and if we
4366 use virtuals anyway, they won't match the rtl patterns. */
4368 if (virtuals_instantiated)
4369 argblock = plus_constant (Pmode, stack_pointer_rtx,
4370 STACK_POINTER_OFFSET);
4371 else
4372 argblock = virtual_outgoing_args_rtx;
4374 else
4376 if (!PUSH_ARGS)
4377 argblock = push_block (GEN_INT (args_size.constant), 0, 0);
4380 /* We push args individually in reverse order, perform stack alignment
4381 before the first push (the last arg). */
4382 if (argblock == 0)
4383 anti_adjust_stack (GEN_INT (args_size.constant
4384 - original_args_size.constant));
4386 argnum = nargs - 1;
4388 #ifdef REG_PARM_STACK_SPACE
4389 if (ACCUMULATE_OUTGOING_ARGS)
4391 /* The argument list is the property of the called routine and it
4392 may clobber it. If the fixed area has been used for previous
4393 parameters, we must save and restore it. */
4394 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
4395 &low_to_save, &high_to_save);
4397 #endif
4399 /* When expanding a normal call, args are stored in push order,
4400 which is the reverse of what we have here. */
4401 bool any_regs = false;
4402 for (int i = nargs; i-- > 0; )
4403 if (argvec[i].reg != NULL_RTX)
4405 targetm.calls.call_args (argvec[i].reg, NULL_TREE);
4406 any_regs = true;
4408 if (!any_regs)
4409 targetm.calls.call_args (pc_rtx, NULL_TREE);
4411 /* Push the args that need to be pushed. */
4413 have_push_fusage = false;
4415 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4416 are to be pushed. */
4417 for (count = 0; count < nargs; count++, argnum--)
4419 machine_mode mode = argvec[argnum].mode;
4420 rtx val = argvec[argnum].value;
4421 rtx reg = argvec[argnum].reg;
4422 int partial = argvec[argnum].partial;
4423 unsigned int parm_align = argvec[argnum].locate.boundary;
4424 int lower_bound = 0, upper_bound = 0, i;
4426 if (! (reg != 0 && partial == 0))
4428 rtx use;
4430 if (ACCUMULATE_OUTGOING_ARGS)
4432 /* If this is being stored into a pre-allocated, fixed-size,
4433 stack area, save any previous data at that location. */
4435 if (ARGS_GROW_DOWNWARD)
4437 /* stack_slot is negative, but we want to index stack_usage_map
4438 with positive values. */
4439 upper_bound = -argvec[argnum].locate.slot_offset.constant + 1;
4440 lower_bound = upper_bound - argvec[argnum].locate.size.constant;
4442 else
4444 lower_bound = argvec[argnum].locate.slot_offset.constant;
4445 upper_bound = lower_bound + argvec[argnum].locate.size.constant;
4448 i = lower_bound;
4449 /* Don't worry about things in the fixed argument area;
4450 it has already been saved. */
4451 if (i < reg_parm_stack_space)
4452 i = reg_parm_stack_space;
4453 while (i < upper_bound && stack_usage_map[i] == 0)
4454 i++;
4456 if (i < upper_bound)
4458 /* We need to make a save area. */
4459 unsigned int size
4460 = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
4461 machine_mode save_mode
4462 = mode_for_size (size, MODE_INT, 1);
4463 rtx adr
4464 = plus_constant (Pmode, argblock,
4465 argvec[argnum].locate.offset.constant);
4466 rtx stack_area
4467 = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
4469 if (save_mode == BLKmode)
4471 argvec[argnum].save_area
4472 = assign_stack_temp (BLKmode,
4473 argvec[argnum].locate.size.constant
4476 emit_block_move (validize_mem
4477 (copy_rtx (argvec[argnum].save_area)),
4478 stack_area,
4479 GEN_INT (argvec[argnum].locate.size.constant),
4480 BLOCK_OP_CALL_PARM);
4482 else
4484 argvec[argnum].save_area = gen_reg_rtx (save_mode);
4486 emit_move_insn (argvec[argnum].save_area, stack_area);
4491 emit_push_insn (val, mode, NULL_TREE, NULL_RTX, parm_align,
4492 partial, reg, 0, argblock,
4493 GEN_INT (argvec[argnum].locate.offset.constant),
4494 reg_parm_stack_space,
4495 ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad), false);
4497 /* Now mark the segment we just used. */
4498 if (ACCUMULATE_OUTGOING_ARGS)
4499 for (i = lower_bound; i < upper_bound; i++)
4500 stack_usage_map[i] = 1;
4502 NO_DEFER_POP;
4504 /* Indicate argument access so that alias.c knows that these
4505 values are live. */
4506 if (argblock)
4507 use = plus_constant (Pmode, argblock,
4508 argvec[argnum].locate.offset.constant);
4509 else if (have_push_fusage)
4510 continue;
4511 else
4513 /* When arguments are pushed, trying to tell alias.c where
4514 exactly this argument is won't work, because the
4515 auto-increment causes confusion. So we merely indicate
4516 that we access something with a known mode somewhere on
4517 the stack. */
4518 use = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4519 gen_rtx_SCRATCH (Pmode));
4520 have_push_fusage = true;
4522 use = gen_rtx_MEM (argvec[argnum].mode, use);
4523 use = gen_rtx_USE (VOIDmode, use);
4524 call_fusage = gen_rtx_EXPR_LIST (VOIDmode, use, call_fusage);
4528 argnum = nargs - 1;
4530 fun = prepare_call_address (NULL, fun, NULL, &call_fusage, 0, 0);
4532 /* Now load any reg parms into their regs. */
4534 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4535 are to be pushed. */
4536 for (count = 0; count < nargs; count++, argnum--)
4538 machine_mode mode = argvec[argnum].mode;
4539 rtx val = argvec[argnum].value;
4540 rtx reg = argvec[argnum].reg;
4541 int partial = argvec[argnum].partial;
4542 #ifdef BLOCK_REG_PADDING
4543 int size = 0;
4544 #endif
4546 /* Handle calls that pass values in multiple non-contiguous
4547 locations. The PA64 has examples of this for library calls. */
4548 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4549 emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (mode));
4550 else if (reg != 0 && partial == 0)
4552 emit_move_insn (reg, val);
4553 #ifdef BLOCK_REG_PADDING
4554 size = GET_MODE_SIZE (argvec[argnum].mode);
4556 /* Copied from load_register_parameters. */
4558 /* Handle case where we have a value that needs shifting
4559 up to the msb. eg. a QImode value and we're padding
4560 upward on a BYTES_BIG_ENDIAN machine. */
4561 if (size < UNITS_PER_WORD
4562 && (argvec[argnum].locate.where_pad
4563 == (BYTES_BIG_ENDIAN ? upward : downward)))
4565 rtx x;
4566 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
4568 /* Assigning REG here rather than a temp makes CALL_FUSAGE
4569 report the whole reg as used. Strictly speaking, the
4570 call only uses SIZE bytes at the msb end, but it doesn't
4571 seem worth generating rtl to say that. */
4572 reg = gen_rtx_REG (word_mode, REGNO (reg));
4573 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
4574 if (x != reg)
4575 emit_move_insn (reg, x);
4577 #endif
4580 NO_DEFER_POP;
4583 /* Any regs containing parms remain in use through the call. */
4584 for (count = 0; count < nargs; count++)
4586 rtx reg = argvec[count].reg;
4587 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4588 use_group_regs (&call_fusage, reg);
4589 else if (reg != 0)
4591 int partial = argvec[count].partial;
4592 if (partial)
4594 int nregs;
4595 gcc_assert (partial % UNITS_PER_WORD == 0);
4596 nregs = partial / UNITS_PER_WORD;
4597 use_regs (&call_fusage, REGNO (reg), nregs);
4599 else
4600 use_reg (&call_fusage, reg);
4604 /* Pass the function the address in which to return a structure value. */
4605 if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
4607 emit_move_insn (struct_value,
4608 force_reg (Pmode,
4609 force_operand (XEXP (mem_value, 0),
4610 NULL_RTX)));
4611 if (REG_P (struct_value))
4612 use_reg (&call_fusage, struct_value);
4615 /* Don't allow popping to be deferred, since then
4616 cse'ing of library calls could delete a call and leave the pop. */
4617 NO_DEFER_POP;
4618 valreg = (mem_value == 0 && outmode != VOIDmode
4619 ? hard_libcall_value (outmode, orgfun) : NULL_RTX);
4621 /* Stack must be properly aligned now. */
4622 gcc_assert (!(stack_pointer_delta
4623 & (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1)));
4625 before_call = get_last_insn ();
4627 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
4628 will set inhibit_defer_pop to that value. */
4629 /* The return type is needed to decide how many bytes the function pops.
4630 Signedness plays no role in that, so for simplicity, we pretend it's
4631 always signed. We also assume that the list of arguments passed has
4632 no impact, so we pretend it is unknown. */
4634 emit_call_1 (fun, NULL,
4635 get_identifier (XSTR (orgfun, 0)),
4636 build_function_type (tfom, NULL_TREE),
4637 original_args_size.constant, args_size.constant,
4638 struct_value_size,
4639 targetm.calls.function_arg (args_so_far,
4640 VOIDmode, void_type_node, true),
4641 valreg,
4642 old_inhibit_defer_pop + 1, call_fusage, flags, args_so_far);
4644 if (flag_ipa_ra)
4646 rtx datum = orgfun;
4647 gcc_assert (GET_CODE (datum) == SYMBOL_REF);
4648 rtx_call_insn *last = last_call_insn ();
4649 add_reg_note (last, REG_CALL_DECL, datum);
4652 /* Right-shift returned value if necessary. */
4653 if (!pcc_struct_value
4654 && TYPE_MODE (tfom) != BLKmode
4655 && targetm.calls.return_in_msb (tfom))
4657 shift_return_value (TYPE_MODE (tfom), false, valreg);
4658 valreg = gen_rtx_REG (TYPE_MODE (tfom), REGNO (valreg));
4661 targetm.calls.end_call_args ();
4663 /* For calls to `setjmp', etc., inform function.c:setjmp_warnings
4664 that it should complain if nonvolatile values are live. For
4665 functions that cannot return, inform flow that control does not
4666 fall through. */
4667 if (flags & ECF_NORETURN)
4669 /* The barrier note must be emitted
4670 immediately after the CALL_INSN. Some ports emit more than
4671 just a CALL_INSN above, so we must search for it here. */
4672 rtx_insn *last = get_last_insn ();
4673 while (!CALL_P (last))
4675 last = PREV_INSN (last);
4676 /* There was no CALL_INSN? */
4677 gcc_assert (last != before_call);
4680 emit_barrier_after (last);
4683 /* Consider that "regular" libcalls, i.e. all of them except for LCT_THROW
4684 and LCT_RETURNS_TWICE, cannot perform non-local gotos. */
4685 if (flags & ECF_NOTHROW)
4687 rtx_insn *last = get_last_insn ();
4688 while (!CALL_P (last))
4690 last = PREV_INSN (last);
4691 /* There was no CALL_INSN? */
4692 gcc_assert (last != before_call);
4695 make_reg_eh_region_note_nothrow_nononlocal (last);
4698 /* Now restore inhibit_defer_pop to its actual original value. */
4699 OK_DEFER_POP;
4701 pop_temp_slots ();
4703 /* Copy the value to the right place. */
4704 if (outmode != VOIDmode && retval)
4706 if (mem_value)
4708 if (value == 0)
4709 value = mem_value;
4710 if (value != mem_value)
4711 emit_move_insn (value, mem_value);
4713 else if (GET_CODE (valreg) == PARALLEL)
4715 if (value == 0)
4716 value = gen_reg_rtx (outmode);
4717 emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
4719 else
4721 /* Convert to the proper mode if a promotion has been active. */
4722 if (GET_MODE (valreg) != outmode)
4724 int unsignedp = TYPE_UNSIGNED (tfom);
4726 gcc_assert (promote_function_mode (tfom, outmode, &unsignedp,
4727 fndecl ? TREE_TYPE (fndecl) : fntype, 1)
4728 == GET_MODE (valreg));
4729 valreg = convert_modes (outmode, GET_MODE (valreg), valreg, 0);
4732 if (value != 0)
4733 emit_move_insn (value, valreg);
4734 else
4735 value = valreg;
4739 if (ACCUMULATE_OUTGOING_ARGS)
4741 #ifdef REG_PARM_STACK_SPACE
4742 if (save_area)
4743 restore_fixed_argument_area (save_area, argblock,
4744 high_to_save, low_to_save);
4745 #endif
4747 /* If we saved any argument areas, restore them. */
4748 for (count = 0; count < nargs; count++)
4749 if (argvec[count].save_area)
4751 machine_mode save_mode = GET_MODE (argvec[count].save_area);
4752 rtx adr = plus_constant (Pmode, argblock,
4753 argvec[count].locate.offset.constant);
4754 rtx stack_area = gen_rtx_MEM (save_mode,
4755 memory_address (save_mode, adr));
4757 if (save_mode == BLKmode)
4758 emit_block_move (stack_area,
4759 validize_mem
4760 (copy_rtx (argvec[count].save_area)),
4761 GEN_INT (argvec[count].locate.size.constant),
4762 BLOCK_OP_CALL_PARM);
4763 else
4764 emit_move_insn (stack_area, argvec[count].save_area);
4767 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
4768 stack_usage_map = initial_stack_usage_map;
4771 free (stack_usage_map_buf);
4773 return value;
4777 /* Output a library call to function FUN (a SYMBOL_REF rtx)
4778 (emitting the queue unless NO_QUEUE is nonzero),
4779 for a value of mode OUTMODE,
4780 with NARGS different arguments, passed as alternating rtx values
4781 and machine_modes to convert them to.
4783 FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for
4784 `const' calls, LCT_PURE for `pure' calls, or other LCT_ value for
4785 other types of library calls. */
4787 void
4788 emit_library_call (rtx orgfun, enum libcall_type fn_type,
4789 machine_mode outmode, int nargs, ...)
4791 va_list p;
4793 va_start (p, nargs);
4794 emit_library_call_value_1 (0, orgfun, NULL_RTX, fn_type, outmode, nargs, p);
4795 va_end (p);
4798 /* Like emit_library_call except that an extra argument, VALUE,
4799 comes second and says where to store the result.
4800 (If VALUE is zero, this function chooses a convenient way
4801 to return the value.
4803 This function returns an rtx for where the value is to be found.
4804 If VALUE is nonzero, VALUE is returned. */
4807 emit_library_call_value (rtx orgfun, rtx value,
4808 enum libcall_type fn_type,
4809 machine_mode outmode, int nargs, ...)
4811 rtx result;
4812 va_list p;
4814 va_start (p, nargs);
4815 result = emit_library_call_value_1 (1, orgfun, value, fn_type, outmode,
4816 nargs, p);
4817 va_end (p);
4819 return result;
4823 /* Store pointer bounds argument ARG into Bounds Table entry
4824 associated with PARM. */
4825 static void
4826 store_bounds (struct arg_data *arg, struct arg_data *parm)
4828 rtx slot = NULL, ptr = NULL, addr = NULL;
4830 /* We may pass bounds not associated with any pointer. */
4831 if (!parm)
4833 gcc_assert (arg->special_slot);
4834 slot = arg->special_slot;
4835 ptr = const0_rtx;
4837 /* Find pointer associated with bounds and where it is
4838 passed. */
4839 else
4841 if (!parm->reg)
4843 gcc_assert (!arg->special_slot);
4845 addr = adjust_address (parm->stack, Pmode, arg->pointer_offset);
4847 else if (REG_P (parm->reg))
4849 gcc_assert (arg->special_slot);
4850 slot = arg->special_slot;
4852 if (MEM_P (parm->value))
4853 addr = adjust_address (parm->value, Pmode, arg->pointer_offset);
4854 else if (REG_P (parm->value))
4855 ptr = gen_rtx_SUBREG (Pmode, parm->value, arg->pointer_offset);
4856 else
4858 gcc_assert (!arg->pointer_offset);
4859 ptr = parm->value;
4862 else
4864 gcc_assert (GET_CODE (parm->reg) == PARALLEL);
4866 gcc_assert (arg->special_slot);
4867 slot = arg->special_slot;
4869 if (parm->parallel_value)
4870 ptr = chkp_get_value_with_offs (parm->parallel_value,
4871 GEN_INT (arg->pointer_offset));
4872 else
4873 gcc_unreachable ();
4877 /* Expand bounds. */
4878 if (!arg->value)
4879 arg->value = expand_normal (arg->tree_value);
4881 targetm.calls.store_bounds_for_arg (ptr, addr, arg->value, slot);
4884 /* Store a single argument for a function call
4885 into the register or memory area where it must be passed.
4886 *ARG describes the argument value and where to pass it.
4888 ARGBLOCK is the address of the stack-block for all the arguments,
4889 or 0 on a machine where arguments are pushed individually.
4891 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
4892 so must be careful about how the stack is used.
4894 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
4895 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
4896 that we need not worry about saving and restoring the stack.
4898 FNDECL is the declaration of the function we are calling.
4900 Return nonzero if this arg should cause sibcall failure,
4901 zero otherwise. */
4903 static int
4904 store_one_arg (struct arg_data *arg, rtx argblock, int flags,
4905 int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
4907 tree pval = arg->tree_value;
4908 rtx reg = 0;
4909 int partial = 0;
4910 int used = 0;
4911 int i, lower_bound = 0, upper_bound = 0;
4912 int sibcall_failure = 0;
4914 if (TREE_CODE (pval) == ERROR_MARK)
4915 return 1;
4917 /* Push a new temporary level for any temporaries we make for
4918 this argument. */
4919 push_temp_slots ();
4921 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
4923 /* If this is being stored into a pre-allocated, fixed-size, stack area,
4924 save any previous data at that location. */
4925 if (argblock && ! variable_size && arg->stack)
4927 if (ARGS_GROW_DOWNWARD)
4929 /* stack_slot is negative, but we want to index stack_usage_map
4930 with positive values. */
4931 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4932 upper_bound = -INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1)) + 1;
4933 else
4934 upper_bound = 0;
4936 lower_bound = upper_bound - arg->locate.size.constant;
4938 else
4940 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4941 lower_bound = INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1));
4942 else
4943 lower_bound = 0;
4945 upper_bound = lower_bound + arg->locate.size.constant;
4948 i = lower_bound;
4949 /* Don't worry about things in the fixed argument area;
4950 it has already been saved. */
4951 if (i < reg_parm_stack_space)
4952 i = reg_parm_stack_space;
4953 while (i < upper_bound && stack_usage_map[i] == 0)
4954 i++;
4956 if (i < upper_bound)
4958 /* We need to make a save area. */
4959 unsigned int size = arg->locate.size.constant * BITS_PER_UNIT;
4960 machine_mode save_mode = mode_for_size (size, MODE_INT, 1);
4961 rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
4962 rtx stack_area = gen_rtx_MEM (save_mode, adr);
4964 if (save_mode == BLKmode)
4966 arg->save_area
4967 = assign_temp (TREE_TYPE (arg->tree_value), 1, 1);
4968 preserve_temp_slots (arg->save_area);
4969 emit_block_move (validize_mem (copy_rtx (arg->save_area)),
4970 stack_area,
4971 GEN_INT (arg->locate.size.constant),
4972 BLOCK_OP_CALL_PARM);
4974 else
4976 arg->save_area = gen_reg_rtx (save_mode);
4977 emit_move_insn (arg->save_area, stack_area);
4983 /* If this isn't going to be placed on both the stack and in registers,
4984 set up the register and number of words. */
4985 if (! arg->pass_on_stack)
4987 if (flags & ECF_SIBCALL)
4988 reg = arg->tail_call_reg;
4989 else
4990 reg = arg->reg;
4991 partial = arg->partial;
4994 /* Being passed entirely in a register. We shouldn't be called in
4995 this case. */
4996 gcc_assert (reg == 0 || partial != 0);
4998 /* If this arg needs special alignment, don't load the registers
4999 here. */
5000 if (arg->n_aligned_regs != 0)
5001 reg = 0;
5003 /* If this is being passed partially in a register, we can't evaluate
5004 it directly into its stack slot. Otherwise, we can. */
5005 if (arg->value == 0)
5007 /* stack_arg_under_construction is nonzero if a function argument is
5008 being evaluated directly into the outgoing argument list and
5009 expand_call must take special action to preserve the argument list
5010 if it is called recursively.
5012 For scalar function arguments stack_usage_map is sufficient to
5013 determine which stack slots must be saved and restored. Scalar
5014 arguments in general have pass_on_stack == 0.
5016 If this argument is initialized by a function which takes the
5017 address of the argument (a C++ constructor or a C function
5018 returning a BLKmode structure), then stack_usage_map is
5019 insufficient and expand_call must push the stack around the
5020 function call. Such arguments have pass_on_stack == 1.
5022 Note that it is always safe to set stack_arg_under_construction,
5023 but this generates suboptimal code if set when not needed. */
5025 if (arg->pass_on_stack)
5026 stack_arg_under_construction++;
5028 arg->value = expand_expr (pval,
5029 (partial
5030 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
5031 ? NULL_RTX : arg->stack,
5032 VOIDmode, EXPAND_STACK_PARM);
5034 /* If we are promoting object (or for any other reason) the mode
5035 doesn't agree, convert the mode. */
5037 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
5038 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
5039 arg->value, arg->unsignedp);
5041 if (arg->pass_on_stack)
5042 stack_arg_under_construction--;
5045 /* Check for overlap with already clobbered argument area. */
5046 if ((flags & ECF_SIBCALL)
5047 && MEM_P (arg->value)
5048 && mem_overlaps_already_clobbered_arg_p (XEXP (arg->value, 0),
5049 arg->locate.size.constant))
5050 sibcall_failure = 1;
5052 /* Don't allow anything left on stack from computation
5053 of argument to alloca. */
5054 if (flags & ECF_MAY_BE_ALLOCA)
5055 do_pending_stack_adjust ();
5057 if (arg->value == arg->stack)
5058 /* If the value is already in the stack slot, we are done. */
5060 else if (arg->mode != BLKmode)
5062 int size;
5063 unsigned int parm_align;
5065 /* Argument is a scalar, not entirely passed in registers.
5066 (If part is passed in registers, arg->partial says how much
5067 and emit_push_insn will take care of putting it there.)
5069 Push it, and if its size is less than the
5070 amount of space allocated to it,
5071 also bump stack pointer by the additional space.
5072 Note that in C the default argument promotions
5073 will prevent such mismatches. */
5075 size = GET_MODE_SIZE (arg->mode);
5076 /* Compute how much space the push instruction will push.
5077 On many machines, pushing a byte will advance the stack
5078 pointer by a halfword. */
5079 #ifdef PUSH_ROUNDING
5080 size = PUSH_ROUNDING (size);
5081 #endif
5082 used = size;
5084 /* Compute how much space the argument should get:
5085 round up to a multiple of the alignment for arguments. */
5086 if (none != FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)))
5087 used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
5088 / (PARM_BOUNDARY / BITS_PER_UNIT))
5089 * (PARM_BOUNDARY / BITS_PER_UNIT));
5091 /* Compute the alignment of the pushed argument. */
5092 parm_align = arg->locate.boundary;
5093 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
5095 int pad = used - size;
5096 if (pad)
5098 unsigned int pad_align = least_bit_hwi (pad) * BITS_PER_UNIT;
5099 parm_align = MIN (parm_align, pad_align);
5103 /* This isn't already where we want it on the stack, so put it there.
5104 This can either be done with push or copy insns. */
5105 if (!emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), NULL_RTX,
5106 parm_align, partial, reg, used - size, argblock,
5107 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
5108 ARGS_SIZE_RTX (arg->locate.alignment_pad), true))
5109 sibcall_failure = 1;
5111 /* Unless this is a partially-in-register argument, the argument is now
5112 in the stack. */
5113 if (partial == 0)
5114 arg->value = arg->stack;
5116 else
5118 /* BLKmode, at least partly to be pushed. */
5120 unsigned int parm_align;
5121 int excess;
5122 rtx size_rtx;
5124 /* Pushing a nonscalar.
5125 If part is passed in registers, PARTIAL says how much
5126 and emit_push_insn will take care of putting it there. */
5128 /* Round its size up to a multiple
5129 of the allocation unit for arguments. */
5131 if (arg->locate.size.var != 0)
5133 excess = 0;
5134 size_rtx = ARGS_SIZE_RTX (arg->locate.size);
5136 else
5138 /* PUSH_ROUNDING has no effect on us, because emit_push_insn
5139 for BLKmode is careful to avoid it. */
5140 excess = (arg->locate.size.constant
5141 - int_size_in_bytes (TREE_TYPE (pval))
5142 + partial);
5143 size_rtx = expand_expr (size_in_bytes (TREE_TYPE (pval)),
5144 NULL_RTX, TYPE_MODE (sizetype),
5145 EXPAND_NORMAL);
5148 parm_align = arg->locate.boundary;
5150 /* When an argument is padded down, the block is aligned to
5151 PARM_BOUNDARY, but the actual argument isn't. */
5152 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
5154 if (arg->locate.size.var)
5155 parm_align = BITS_PER_UNIT;
5156 else if (excess)
5158 unsigned int excess_align = least_bit_hwi (excess) * BITS_PER_UNIT;
5159 parm_align = MIN (parm_align, excess_align);
5163 if ((flags & ECF_SIBCALL) && MEM_P (arg->value))
5165 /* emit_push_insn might not work properly if arg->value and
5166 argblock + arg->locate.offset areas overlap. */
5167 rtx x = arg->value;
5168 int i = 0;
5170 if (XEXP (x, 0) == crtl->args.internal_arg_pointer
5171 || (GET_CODE (XEXP (x, 0)) == PLUS
5172 && XEXP (XEXP (x, 0), 0) ==
5173 crtl->args.internal_arg_pointer
5174 && CONST_INT_P (XEXP (XEXP (x, 0), 1))))
5176 if (XEXP (x, 0) != crtl->args.internal_arg_pointer)
5177 i = INTVAL (XEXP (XEXP (x, 0), 1));
5179 /* arg.locate doesn't contain the pretend_args_size offset,
5180 it's part of argblock. Ensure we don't count it in I. */
5181 if (STACK_GROWS_DOWNWARD)
5182 i -= crtl->args.pretend_args_size;
5183 else
5184 i += crtl->args.pretend_args_size;
5186 /* expand_call should ensure this. */
5187 gcc_assert (!arg->locate.offset.var
5188 && arg->locate.size.var == 0
5189 && CONST_INT_P (size_rtx));
5191 if (arg->locate.offset.constant > i)
5193 if (arg->locate.offset.constant < i + INTVAL (size_rtx))
5194 sibcall_failure = 1;
5196 else if (arg->locate.offset.constant < i)
5198 /* Use arg->locate.size.constant instead of size_rtx
5199 because we only care about the part of the argument
5200 on the stack. */
5201 if (i < (arg->locate.offset.constant
5202 + arg->locate.size.constant))
5203 sibcall_failure = 1;
5205 else
5207 /* Even though they appear to be at the same location,
5208 if part of the outgoing argument is in registers,
5209 they aren't really at the same location. Check for
5210 this by making sure that the incoming size is the
5211 same as the outgoing size. */
5212 if (arg->locate.size.constant != INTVAL (size_rtx))
5213 sibcall_failure = 1;
5218 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
5219 parm_align, partial, reg, excess, argblock,
5220 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
5221 ARGS_SIZE_RTX (arg->locate.alignment_pad), false);
5223 /* Unless this is a partially-in-register argument, the argument is now
5224 in the stack.
5226 ??? Unlike the case above, in which we want the actual
5227 address of the data, so that we can load it directly into a
5228 register, here we want the address of the stack slot, so that
5229 it's properly aligned for word-by-word copying or something
5230 like that. It's not clear that this is always correct. */
5231 if (partial == 0)
5232 arg->value = arg->stack_slot;
5235 if (arg->reg && GET_CODE (arg->reg) == PARALLEL)
5237 tree type = TREE_TYPE (arg->tree_value);
5238 arg->parallel_value
5239 = emit_group_load_into_temps (arg->reg, arg->value, type,
5240 int_size_in_bytes (type));
5243 /* Mark all slots this store used. */
5244 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
5245 && argblock && ! variable_size && arg->stack)
5246 for (i = lower_bound; i < upper_bound; i++)
5247 stack_usage_map[i] = 1;
5249 /* Once we have pushed something, pops can't safely
5250 be deferred during the rest of the arguments. */
5251 NO_DEFER_POP;
5253 /* Free any temporary slots made in processing this argument. */
5254 pop_temp_slots ();
5256 return sibcall_failure;
5259 /* Nonzero if we do not know how to pass TYPE solely in registers. */
5261 bool
5262 must_pass_in_stack_var_size (machine_mode mode ATTRIBUTE_UNUSED,
5263 const_tree type)
5265 if (!type)
5266 return false;
5268 /* If the type has variable size... */
5269 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5270 return true;
5272 /* If the type is marked as addressable (it is required
5273 to be constructed into the stack)... */
5274 if (TREE_ADDRESSABLE (type))
5275 return true;
5277 return false;
5280 /* Another version of the TARGET_MUST_PASS_IN_STACK hook. This one
5281 takes trailing padding of a structure into account. */
5282 /* ??? Should be able to merge these two by examining BLOCK_REG_PADDING. */
5284 bool
5285 must_pass_in_stack_var_size_or_pad (machine_mode mode, const_tree type)
5287 if (!type)
5288 return false;
5290 /* If the type has variable size... */
5291 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5292 return true;
5294 /* If the type is marked as addressable (it is required
5295 to be constructed into the stack)... */
5296 if (TREE_ADDRESSABLE (type))
5297 return true;
5299 /* If the padding and mode of the type is such that a copy into
5300 a register would put it into the wrong part of the register. */
5301 if (mode == BLKmode
5302 && int_size_in_bytes (type) % (PARM_BOUNDARY / BITS_PER_UNIT)
5303 && (FUNCTION_ARG_PADDING (mode, type)
5304 == (BYTES_BIG_ENDIAN ? upward : downward)))
5305 return true;
5307 return false;