2016-07-28 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / calls.c
blobc04d00f7f445fc363d144aa27eee15fd3abcc4fb
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 "tm_p.h"
30 #include "stringpool.h"
31 #include "expmed.h"
32 #include "optabs.h"
33 #include "emit-rtl.h"
34 #include "cgraph.h"
35 #include "diagnostic-core.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "varasm.h"
39 #include "internal-fn.h"
40 #include "dojump.h"
41 #include "explow.h"
42 #include "calls.h"
43 #include "expr.h"
44 #include "output.h"
45 #include "langhooks.h"
46 #include "except.h"
47 #include "dbgcnt.h"
48 #include "rtl-iter.h"
49 #include "tree-chkp.h"
50 #include "rtl-chkp.h"
53 /* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits. */
54 #define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
56 /* Data structure and subroutines used within expand_call. */
58 struct arg_data
60 /* Tree node for this argument. */
61 tree tree_value;
62 /* Mode for value; TYPE_MODE unless promoted. */
63 machine_mode mode;
64 /* Current RTL value for argument, or 0 if it isn't precomputed. */
65 rtx value;
66 /* Initially-compute RTL value for argument; only for const functions. */
67 rtx initial_value;
68 /* Register to pass this argument in, 0 if passed on stack, or an
69 PARALLEL if the arg is to be copied into multiple non-contiguous
70 registers. */
71 rtx reg;
72 /* Register to pass this argument in when generating tail call sequence.
73 This is not the same register as for normal calls on machines with
74 register windows. */
75 rtx tail_call_reg;
76 /* If REG is a PARALLEL, this is a copy of VALUE pulled into the correct
77 form for emit_group_move. */
78 rtx parallel_value;
79 /* If value is passed in neither reg nor stack, this field holds a number
80 of a special slot to be used. */
81 rtx special_slot;
82 /* For pointer bounds hold an index of parm bounds are bound to. -1 if
83 there is no such pointer. */
84 int pointer_arg;
85 /* If pointer_arg refers a structure, then pointer_offset holds an offset
86 of a pointer in this structure. */
87 int pointer_offset;
88 /* If REG was promoted from the actual mode of the argument expression,
89 indicates whether the promotion is sign- or zero-extended. */
90 int unsignedp;
91 /* Number of bytes to put in registers. 0 means put the whole arg
92 in registers. Also 0 if not passed in registers. */
93 int partial;
94 /* Nonzero if argument must be passed on stack.
95 Note that some arguments may be passed on the stack
96 even though pass_on_stack is zero, just because FUNCTION_ARG says so.
97 pass_on_stack identifies arguments that *cannot* go in registers. */
98 int pass_on_stack;
99 /* Some fields packaged up for locate_and_pad_parm. */
100 struct locate_and_pad_arg_data locate;
101 /* Location on the stack at which parameter should be stored. The store
102 has already been done if STACK == VALUE. */
103 rtx stack;
104 /* Location on the stack of the start of this argument slot. This can
105 differ from STACK if this arg pads downward. This location is known
106 to be aligned to TARGET_FUNCTION_ARG_BOUNDARY. */
107 rtx stack_slot;
108 /* Place that this stack area has been saved, if needed. */
109 rtx save_area;
110 /* If an argument's alignment does not permit direct copying into registers,
111 copy in smaller-sized pieces into pseudos. These are stored in a
112 block pointed to by this field. The next field says how many
113 word-sized pseudos we made. */
114 rtx *aligned_regs;
115 int n_aligned_regs;
118 /* A vector of one char per byte of stack space. A byte if nonzero if
119 the corresponding stack location has been used.
120 This vector is used to prevent a function call within an argument from
121 clobbering any stack already set up. */
122 static char *stack_usage_map;
124 /* Size of STACK_USAGE_MAP. */
125 static int highest_outgoing_arg_in_use;
127 /* A bitmap of virtual-incoming stack space. Bit is set if the corresponding
128 stack location's tail call argument has been already stored into the stack.
129 This bitmap is used to prevent sibling call optimization if function tries
130 to use parent's incoming argument slots when they have been already
131 overwritten with tail call arguments. */
132 static sbitmap stored_args_map;
134 /* stack_arg_under_construction is nonzero when an argument may be
135 initialized with a constructor call (including a C function that
136 returns a BLKmode struct) and expand_call must take special action
137 to make sure the object being constructed does not overlap the
138 argument list for the constructor call. */
139 static int stack_arg_under_construction;
141 static void emit_call_1 (rtx, tree, tree, tree, HOST_WIDE_INT, HOST_WIDE_INT,
142 HOST_WIDE_INT, rtx, rtx, int, rtx, int,
143 cumulative_args_t);
144 static void precompute_register_parameters (int, struct arg_data *, int *);
145 static void store_bounds (struct arg_data *, struct arg_data *);
146 static int store_one_arg (struct arg_data *, rtx, int, int, int);
147 static void store_unaligned_arguments_into_pseudos (struct arg_data *, int);
148 static int finalize_must_preallocate (int, int, struct arg_data *,
149 struct args_size *);
150 static void precompute_arguments (int, struct arg_data *);
151 static int compute_argument_block_size (int, struct args_size *, tree, tree, int);
152 static void initialize_argument_information (int, struct arg_data *,
153 struct args_size *, int,
154 tree, tree,
155 tree, tree, cumulative_args_t, int,
156 rtx *, int *, int *, int *,
157 bool *, bool);
158 static void compute_argument_addresses (struct arg_data *, rtx, int);
159 static rtx rtx_for_function_call (tree, tree);
160 static void load_register_parameters (struct arg_data *, int, rtx *, int,
161 int, int *);
162 static rtx emit_library_call_value_1 (int, rtx, rtx, enum libcall_type,
163 machine_mode, int, va_list);
164 static int special_function_p (const_tree, int);
165 static int check_sibcall_argument_overlap_1 (rtx);
166 static int check_sibcall_argument_overlap (rtx_insn *, struct arg_data *, int);
168 static int combine_pending_stack_adjustment_and_call (int, struct args_size *,
169 unsigned int);
170 static tree split_complex_types (tree);
172 #ifdef REG_PARM_STACK_SPACE
173 static rtx save_fixed_argument_area (int, rtx, int *, int *);
174 static void restore_fixed_argument_area (rtx, rtx, int, int);
175 #endif
177 /* Force FUNEXP into a form suitable for the address of a CALL,
178 and return that as an rtx. Also load the static chain register
179 if FNDECL is a nested function.
181 CALL_FUSAGE points to a variable holding the prospective
182 CALL_INSN_FUNCTION_USAGE information. */
185 prepare_call_address (tree fndecl_or_type, rtx funexp, rtx static_chain_value,
186 rtx *call_fusage, int reg_parm_seen, int sibcallp)
188 /* Make a valid memory address and copy constants through pseudo-regs,
189 but not for a constant address if -fno-function-cse. */
190 if (GET_CODE (funexp) != SYMBOL_REF)
191 /* If we are using registers for parameters, force the
192 function address into a register now. */
193 funexp = ((reg_parm_seen
194 && targetm.small_register_classes_for_mode_p (FUNCTION_MODE))
195 ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
196 : memory_address (FUNCTION_MODE, funexp));
197 else if (! sibcallp)
199 if (!NO_FUNCTION_CSE && optimize && ! flag_no_function_cse)
200 funexp = force_reg (Pmode, funexp);
203 if (static_chain_value != 0
204 && (TREE_CODE (fndecl_or_type) != FUNCTION_DECL
205 || DECL_STATIC_CHAIN (fndecl_or_type)))
207 rtx chain;
209 chain = targetm.calls.static_chain (fndecl_or_type, false);
210 static_chain_value = convert_memory_address (Pmode, static_chain_value);
212 emit_move_insn (chain, static_chain_value);
213 if (REG_P (chain))
214 use_reg (call_fusage, chain);
217 return funexp;
220 /* Generate instructions to call function FUNEXP,
221 and optionally pop the results.
222 The CALL_INSN is the first insn generated.
224 FNDECL is the declaration node of the function. This is given to the
225 hook TARGET_RETURN_POPS_ARGS to determine whether this function pops
226 its own args.
228 FUNTYPE is the data type of the function. This is given to the hook
229 TARGET_RETURN_POPS_ARGS to determine whether this function pops its
230 own args. We used to allow an identifier for library functions, but
231 that doesn't work when the return type is an aggregate type and the
232 calling convention says that the pointer to this aggregate is to be
233 popped by the callee.
235 STACK_SIZE is the number of bytes of arguments on the stack,
236 ROUNDED_STACK_SIZE is that number rounded up to
237 PREFERRED_STACK_BOUNDARY; zero if the size is variable. This is
238 both to put into the call insn and to generate explicit popping
239 code if necessary.
241 STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
242 It is zero if this call doesn't want a structure value.
244 NEXT_ARG_REG is the rtx that results from executing
245 targetm.calls.function_arg (&args_so_far, VOIDmode, void_type_node, true)
246 just after all the args have had their registers assigned.
247 This could be whatever you like, but normally it is the first
248 arg-register beyond those used for args in this call,
249 or 0 if all the arg-registers are used in this call.
250 It is passed on to `gen_call' so you can put this info in the call insn.
252 VALREG is a hard register in which a value is returned,
253 or 0 if the call does not return a value.
255 OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
256 the args to this call were processed.
257 We restore `inhibit_defer_pop' to that value.
259 CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
260 denote registers used by the called function. */
262 static void
263 emit_call_1 (rtx funexp, tree fntree ATTRIBUTE_UNUSED, tree fndecl ATTRIBUTE_UNUSED,
264 tree funtype ATTRIBUTE_UNUSED,
265 HOST_WIDE_INT stack_size ATTRIBUTE_UNUSED,
266 HOST_WIDE_INT rounded_stack_size,
267 HOST_WIDE_INT struct_value_size ATTRIBUTE_UNUSED,
268 rtx next_arg_reg ATTRIBUTE_UNUSED, rtx valreg,
269 int old_inhibit_defer_pop, rtx call_fusage, int ecf_flags,
270 cumulative_args_t args_so_far ATTRIBUTE_UNUSED)
272 rtx rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
273 rtx call, funmem, pat;
274 int already_popped = 0;
275 HOST_WIDE_INT n_popped = 0;
277 /* Sibling call patterns never pop arguments (no sibcall(_value)_pop
278 patterns exist). Any popping that the callee does on return will
279 be from our caller's frame rather than ours. */
280 if (!(ecf_flags & ECF_SIBCALL))
282 n_popped += targetm.calls.return_pops_args (fndecl, funtype, stack_size);
284 #ifdef CALL_POPS_ARGS
285 n_popped += CALL_POPS_ARGS (*get_cumulative_args (args_so_far));
286 #endif
289 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
290 and we don't want to load it into a register as an optimization,
291 because prepare_call_address already did it if it should be done. */
292 if (GET_CODE (funexp) != SYMBOL_REF)
293 funexp = memory_address (FUNCTION_MODE, funexp);
295 funmem = gen_rtx_MEM (FUNCTION_MODE, funexp);
296 if (fndecl && TREE_CODE (fndecl) == FUNCTION_DECL)
298 tree t = fndecl;
300 /* Although a built-in FUNCTION_DECL and its non-__builtin
301 counterpart compare equal and get a shared mem_attrs, they
302 produce different dump output in compare-debug compilations,
303 if an entry gets garbage collected in one compilation, then
304 adds a different (but equivalent) entry, while the other
305 doesn't run the garbage collector at the same spot and then
306 shares the mem_attr with the equivalent entry. */
307 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
309 tree t2 = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
310 if (t2)
311 t = t2;
314 set_mem_expr (funmem, t);
316 else if (fntree)
317 set_mem_expr (funmem, build_simple_mem_ref (CALL_EXPR_FN (fntree)));
319 if (ecf_flags & ECF_SIBCALL)
321 if (valreg)
322 pat = targetm.gen_sibcall_value (valreg, funmem,
323 rounded_stack_size_rtx,
324 next_arg_reg, NULL_RTX);
325 else
326 pat = targetm.gen_sibcall (funmem, rounded_stack_size_rtx,
327 next_arg_reg, GEN_INT (struct_value_size));
329 /* If the target has "call" or "call_value" insns, then prefer them
330 if no arguments are actually popped. If the target does not have
331 "call" or "call_value" insns, then we must use the popping versions
332 even if the call has no arguments to pop. */
333 else if (n_popped > 0
334 || !(valreg
335 ? targetm.have_call_value ()
336 : targetm.have_call ()))
338 rtx n_pop = GEN_INT (n_popped);
340 /* If this subroutine pops its own args, record that in the call insn
341 if possible, for the sake of frame pointer elimination. */
343 if (valreg)
344 pat = targetm.gen_call_value_pop (valreg, funmem,
345 rounded_stack_size_rtx,
346 next_arg_reg, n_pop);
347 else
348 pat = targetm.gen_call_pop (funmem, rounded_stack_size_rtx,
349 next_arg_reg, n_pop);
351 already_popped = 1;
353 else
355 if (valreg)
356 pat = targetm.gen_call_value (valreg, funmem, rounded_stack_size_rtx,
357 next_arg_reg, NULL_RTX);
358 else
359 pat = targetm.gen_call (funmem, rounded_stack_size_rtx, next_arg_reg,
360 GEN_INT (struct_value_size));
362 emit_insn (pat);
364 /* Find the call we just emitted. */
365 rtx_call_insn *call_insn = last_call_insn ();
367 /* Some target create a fresh MEM instead of reusing the one provided
368 above. Set its MEM_EXPR. */
369 call = get_call_rtx_from (call_insn);
370 if (call
371 && MEM_EXPR (XEXP (call, 0)) == NULL_TREE
372 && MEM_EXPR (funmem) != NULL_TREE)
373 set_mem_expr (XEXP (call, 0), MEM_EXPR (funmem));
375 /* Mark instrumented calls. */
376 if (call && fntree)
377 CALL_EXPR_WITH_BOUNDS_P (call) = CALL_WITH_BOUNDS_P (fntree);
379 /* Put the register usage information there. */
380 add_function_usage_to (call_insn, call_fusage);
382 /* If this is a const call, then set the insn's unchanging bit. */
383 if (ecf_flags & ECF_CONST)
384 RTL_CONST_CALL_P (call_insn) = 1;
386 /* If this is a pure call, then set the insn's unchanging bit. */
387 if (ecf_flags & ECF_PURE)
388 RTL_PURE_CALL_P (call_insn) = 1;
390 /* If this is a const call, then set the insn's unchanging bit. */
391 if (ecf_flags & ECF_LOOPING_CONST_OR_PURE)
392 RTL_LOOPING_CONST_OR_PURE_CALL_P (call_insn) = 1;
394 /* Create a nothrow REG_EH_REGION note, if needed. */
395 make_reg_eh_region_note (call_insn, ecf_flags, 0);
397 if (ecf_flags & ECF_NORETURN)
398 add_reg_note (call_insn, REG_NORETURN, const0_rtx);
400 if (ecf_flags & ECF_RETURNS_TWICE)
402 add_reg_note (call_insn, REG_SETJMP, const0_rtx);
403 cfun->calls_setjmp = 1;
406 SIBLING_CALL_P (call_insn) = ((ecf_flags & ECF_SIBCALL) != 0);
408 /* Restore this now, so that we do defer pops for this call's args
409 if the context of the call as a whole permits. */
410 inhibit_defer_pop = old_inhibit_defer_pop;
412 if (n_popped > 0)
414 if (!already_popped)
415 CALL_INSN_FUNCTION_USAGE (call_insn)
416 = gen_rtx_EXPR_LIST (VOIDmode,
417 gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx),
418 CALL_INSN_FUNCTION_USAGE (call_insn));
419 rounded_stack_size -= n_popped;
420 rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
421 stack_pointer_delta -= n_popped;
423 add_reg_note (call_insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
425 /* If popup is needed, stack realign must use DRAP */
426 if (SUPPORTS_STACK_ALIGNMENT)
427 crtl->need_drap = true;
429 /* For noreturn calls when not accumulating outgoing args force
430 REG_ARGS_SIZE note to prevent crossjumping of calls with different
431 args sizes. */
432 else if (!ACCUMULATE_OUTGOING_ARGS && (ecf_flags & ECF_NORETURN) != 0)
433 add_reg_note (call_insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
435 if (!ACCUMULATE_OUTGOING_ARGS)
437 /* If returning from the subroutine does not automatically pop the args,
438 we need an instruction to pop them sooner or later.
439 Perhaps do it now; perhaps just record how much space to pop later.
441 If returning from the subroutine does pop the args, indicate that the
442 stack pointer will be changed. */
444 if (rounded_stack_size != 0)
446 if (ecf_flags & ECF_NORETURN)
447 /* Just pretend we did the pop. */
448 stack_pointer_delta -= rounded_stack_size;
449 else if (flag_defer_pop && inhibit_defer_pop == 0
450 && ! (ecf_flags & (ECF_CONST | ECF_PURE)))
451 pending_stack_adjust += rounded_stack_size;
452 else
453 adjust_stack (rounded_stack_size_rtx);
456 /* When we accumulate outgoing args, we must avoid any stack manipulations.
457 Restore the stack pointer to its original value now. Usually
458 ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
459 On i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
460 popping variants of functions exist as well.
462 ??? We may optimize similar to defer_pop above, but it is
463 probably not worthwhile.
465 ??? It will be worthwhile to enable combine_stack_adjustments even for
466 such machines. */
467 else if (n_popped)
468 anti_adjust_stack (GEN_INT (n_popped));
471 /* Determine if the function identified by NAME and FNDECL is one with
472 special properties we wish to know about.
474 For example, if the function might return more than one time (setjmp), then
475 set RETURNS_TWICE to a nonzero value.
477 Similarly set NORETURN if the function is in the longjmp family.
479 Set MAY_BE_ALLOCA for any memory allocation function that might allocate
480 space from the stack such as alloca. */
482 static int
483 special_function_p (const_tree fndecl, int flags)
485 tree name_decl = DECL_NAME (fndecl);
487 /* For instrumentation clones we want to derive flags
488 from the original name. */
489 if (cgraph_node::get (fndecl)
490 && cgraph_node::get (fndecl)->instrumentation_clone)
491 name_decl = DECL_NAME (cgraph_node::get (fndecl)->orig_decl);
493 if (fndecl && name_decl
494 && IDENTIFIER_LENGTH (name_decl) <= 17
495 /* Exclude functions not at the file scope, or not `extern',
496 since they are not the magic functions we would otherwise
497 think they are.
498 FIXME: this should be handled with attributes, not with this
499 hacky imitation of DECL_ASSEMBLER_NAME. It's (also) wrong
500 because you can declare fork() inside a function if you
501 wish. */
502 && (DECL_CONTEXT (fndecl) == NULL_TREE
503 || TREE_CODE (DECL_CONTEXT (fndecl)) == TRANSLATION_UNIT_DECL)
504 && TREE_PUBLIC (fndecl))
506 const char *name = IDENTIFIER_POINTER (name_decl);
507 const char *tname = name;
509 /* We assume that alloca will always be called by name. It
510 makes no sense to pass it as a pointer-to-function to
511 anything that does not understand its behavior. */
512 if (IDENTIFIER_LENGTH (name_decl) == 6
513 && name[0] == 'a'
514 && ! strcmp (name, "alloca"))
515 flags |= ECF_MAY_BE_ALLOCA;
517 /* Disregard prefix _, __ or __x. */
518 if (name[0] == '_')
520 if (name[1] == '_' && name[2] == 'x')
521 tname += 3;
522 else if (name[1] == '_')
523 tname += 2;
524 else
525 tname += 1;
528 if (tname[0] == 's')
530 if ((tname[1] == 'e'
531 && (! strcmp (tname, "setjmp")
532 || ! strcmp (tname, "setjmp_syscall")))
533 || (tname[1] == 'i'
534 && ! strcmp (tname, "sigsetjmp"))
535 || (tname[1] == 'a'
536 && ! strcmp (tname, "savectx")))
537 flags |= ECF_RETURNS_TWICE | ECF_LEAF;
539 if (tname[1] == 'i'
540 && ! strcmp (tname, "siglongjmp"))
541 flags |= ECF_NORETURN;
543 else if ((tname[0] == 'q' && tname[1] == 's'
544 && ! strcmp (tname, "qsetjmp"))
545 || (tname[0] == 'v' && tname[1] == 'f'
546 && ! strcmp (tname, "vfork"))
547 || (tname[0] == 'g' && tname[1] == 'e'
548 && !strcmp (tname, "getcontext")))
549 flags |= ECF_RETURNS_TWICE | ECF_LEAF;
551 else if (tname[0] == 'l' && tname[1] == 'o'
552 && ! strcmp (tname, "longjmp"))
553 flags |= ECF_NORETURN;
556 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
557 switch (DECL_FUNCTION_CODE (fndecl))
559 case BUILT_IN_ALLOCA:
560 case BUILT_IN_ALLOCA_WITH_ALIGN:
561 flags |= ECF_MAY_BE_ALLOCA;
562 break;
563 default:
564 break;
567 return flags;
570 /* Similar to special_function_p; return a set of ERF_ flags for the
571 function FNDECL. */
572 static int
573 decl_return_flags (tree fndecl)
575 tree attr;
576 tree type = TREE_TYPE (fndecl);
577 if (!type)
578 return 0;
580 attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
581 if (!attr)
582 return 0;
584 attr = TREE_VALUE (TREE_VALUE (attr));
585 if (!attr || TREE_STRING_LENGTH (attr) < 1)
586 return 0;
588 switch (TREE_STRING_POINTER (attr)[0])
590 case '1':
591 case '2':
592 case '3':
593 case '4':
594 return ERF_RETURNS_ARG | (TREE_STRING_POINTER (attr)[0] - '1');
596 case 'm':
597 return ERF_NOALIAS;
599 case '.':
600 default:
601 return 0;
605 /* Return nonzero when FNDECL represents a call to setjmp. */
608 setjmp_call_p (const_tree fndecl)
610 if (DECL_IS_RETURNS_TWICE (fndecl))
611 return ECF_RETURNS_TWICE;
612 return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
616 /* Return true if STMT may be an alloca call. */
618 bool
619 gimple_maybe_alloca_call_p (const gimple *stmt)
621 tree fndecl;
623 if (!is_gimple_call (stmt))
624 return false;
626 fndecl = gimple_call_fndecl (stmt);
627 if (fndecl && (special_function_p (fndecl, 0) & ECF_MAY_BE_ALLOCA))
628 return true;
630 return false;
633 /* Return true if STMT is a builtin alloca call. */
635 bool
636 gimple_alloca_call_p (const gimple *stmt)
638 tree fndecl;
640 if (!is_gimple_call (stmt))
641 return false;
643 fndecl = gimple_call_fndecl (stmt);
644 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
645 switch (DECL_FUNCTION_CODE (fndecl))
647 case BUILT_IN_ALLOCA:
648 case BUILT_IN_ALLOCA_WITH_ALIGN:
649 return true;
650 default:
651 break;
654 return false;
657 /* Return true when exp contains a builtin alloca call. */
659 bool
660 alloca_call_p (const_tree exp)
662 tree fndecl;
663 if (TREE_CODE (exp) == CALL_EXPR
664 && (fndecl = get_callee_fndecl (exp))
665 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
666 switch (DECL_FUNCTION_CODE (fndecl))
668 case BUILT_IN_ALLOCA:
669 case BUILT_IN_ALLOCA_WITH_ALIGN:
670 return true;
671 default:
672 break;
675 return false;
678 /* Return TRUE if FNDECL is either a TM builtin or a TM cloned
679 function. Return FALSE otherwise. */
681 static bool
682 is_tm_builtin (const_tree fndecl)
684 if (fndecl == NULL)
685 return false;
687 if (decl_is_tm_clone (fndecl))
688 return true;
690 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
692 switch (DECL_FUNCTION_CODE (fndecl))
694 case BUILT_IN_TM_COMMIT:
695 case BUILT_IN_TM_COMMIT_EH:
696 case BUILT_IN_TM_ABORT:
697 case BUILT_IN_TM_IRREVOCABLE:
698 case BUILT_IN_TM_GETTMCLONE_IRR:
699 case BUILT_IN_TM_MEMCPY:
700 case BUILT_IN_TM_MEMMOVE:
701 case BUILT_IN_TM_MEMSET:
702 CASE_BUILT_IN_TM_STORE (1):
703 CASE_BUILT_IN_TM_STORE (2):
704 CASE_BUILT_IN_TM_STORE (4):
705 CASE_BUILT_IN_TM_STORE (8):
706 CASE_BUILT_IN_TM_STORE (FLOAT):
707 CASE_BUILT_IN_TM_STORE (DOUBLE):
708 CASE_BUILT_IN_TM_STORE (LDOUBLE):
709 CASE_BUILT_IN_TM_STORE (M64):
710 CASE_BUILT_IN_TM_STORE (M128):
711 CASE_BUILT_IN_TM_STORE (M256):
712 CASE_BUILT_IN_TM_LOAD (1):
713 CASE_BUILT_IN_TM_LOAD (2):
714 CASE_BUILT_IN_TM_LOAD (4):
715 CASE_BUILT_IN_TM_LOAD (8):
716 CASE_BUILT_IN_TM_LOAD (FLOAT):
717 CASE_BUILT_IN_TM_LOAD (DOUBLE):
718 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
719 CASE_BUILT_IN_TM_LOAD (M64):
720 CASE_BUILT_IN_TM_LOAD (M128):
721 CASE_BUILT_IN_TM_LOAD (M256):
722 case BUILT_IN_TM_LOG:
723 case BUILT_IN_TM_LOG_1:
724 case BUILT_IN_TM_LOG_2:
725 case BUILT_IN_TM_LOG_4:
726 case BUILT_IN_TM_LOG_8:
727 case BUILT_IN_TM_LOG_FLOAT:
728 case BUILT_IN_TM_LOG_DOUBLE:
729 case BUILT_IN_TM_LOG_LDOUBLE:
730 case BUILT_IN_TM_LOG_M64:
731 case BUILT_IN_TM_LOG_M128:
732 case BUILT_IN_TM_LOG_M256:
733 return true;
734 default:
735 break;
738 return false;
741 /* Detect flags (function attributes) from the function decl or type node. */
744 flags_from_decl_or_type (const_tree exp)
746 int flags = 0;
748 if (DECL_P (exp))
750 /* The function exp may have the `malloc' attribute. */
751 if (DECL_IS_MALLOC (exp))
752 flags |= ECF_MALLOC;
754 /* The function exp may have the `returns_twice' attribute. */
755 if (DECL_IS_RETURNS_TWICE (exp))
756 flags |= ECF_RETURNS_TWICE;
758 /* Process the pure and const attributes. */
759 if (TREE_READONLY (exp))
760 flags |= ECF_CONST;
761 if (DECL_PURE_P (exp))
762 flags |= ECF_PURE;
763 if (DECL_LOOPING_CONST_OR_PURE_P (exp))
764 flags |= ECF_LOOPING_CONST_OR_PURE;
766 if (DECL_IS_NOVOPS (exp))
767 flags |= ECF_NOVOPS;
768 if (lookup_attribute ("leaf", DECL_ATTRIBUTES (exp)))
769 flags |= ECF_LEAF;
771 if (TREE_NOTHROW (exp))
772 flags |= ECF_NOTHROW;
774 if (flag_tm)
776 if (is_tm_builtin (exp))
777 flags |= ECF_TM_BUILTIN;
778 else if ((flags & (ECF_CONST|ECF_NOVOPS)) != 0
779 || lookup_attribute ("transaction_pure",
780 TYPE_ATTRIBUTES (TREE_TYPE (exp))))
781 flags |= ECF_TM_PURE;
784 flags = special_function_p (exp, flags);
786 else if (TYPE_P (exp))
788 if (TYPE_READONLY (exp))
789 flags |= ECF_CONST;
791 if (flag_tm
792 && ((flags & ECF_CONST) != 0
793 || lookup_attribute ("transaction_pure", TYPE_ATTRIBUTES (exp))))
794 flags |= ECF_TM_PURE;
796 else
797 gcc_unreachable ();
799 if (TREE_THIS_VOLATILE (exp))
801 flags |= ECF_NORETURN;
802 if (flags & (ECF_CONST|ECF_PURE))
803 flags |= ECF_LOOPING_CONST_OR_PURE;
806 return flags;
809 /* Detect flags from a CALL_EXPR. */
812 call_expr_flags (const_tree t)
814 int flags;
815 tree decl = get_callee_fndecl (t);
817 if (decl)
818 flags = flags_from_decl_or_type (decl);
819 else if (CALL_EXPR_FN (t) == NULL_TREE)
820 flags = internal_fn_flags (CALL_EXPR_IFN (t));
821 else
823 t = TREE_TYPE (CALL_EXPR_FN (t));
824 if (t && TREE_CODE (t) == POINTER_TYPE)
825 flags = flags_from_decl_or_type (TREE_TYPE (t));
826 else
827 flags = 0;
830 return flags;
833 /* Return true if TYPE should be passed by invisible reference. */
835 bool
836 pass_by_reference (CUMULATIVE_ARGS *ca, machine_mode mode,
837 tree type, bool named_arg)
839 if (type)
841 /* If this type contains non-trivial constructors, then it is
842 forbidden for the middle-end to create any new copies. */
843 if (TREE_ADDRESSABLE (type))
844 return true;
846 /* GCC post 3.4 passes *all* variable sized types by reference. */
847 if (!TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
848 return true;
850 /* If a record type should be passed the same as its first (and only)
851 member, use the type and mode of that member. */
852 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
854 type = TREE_TYPE (first_field (type));
855 mode = TYPE_MODE (type);
859 return targetm.calls.pass_by_reference (pack_cumulative_args (ca), mode,
860 type, named_arg);
863 /* Return true if TYPE, which is passed by reference, should be callee
864 copied instead of caller copied. */
866 bool
867 reference_callee_copied (CUMULATIVE_ARGS *ca, machine_mode mode,
868 tree type, bool named_arg)
870 if (type && TREE_ADDRESSABLE (type))
871 return false;
872 return targetm.calls.callee_copies (pack_cumulative_args (ca), mode, type,
873 named_arg);
877 /* Precompute all register parameters as described by ARGS, storing values
878 into fields within the ARGS array.
880 NUM_ACTUALS indicates the total number elements in the ARGS array.
882 Set REG_PARM_SEEN if we encounter a register parameter. */
884 static void
885 precompute_register_parameters (int num_actuals, struct arg_data *args,
886 int *reg_parm_seen)
888 int i;
890 *reg_parm_seen = 0;
892 for (i = 0; i < num_actuals; i++)
893 if (args[i].reg != 0 && ! args[i].pass_on_stack)
895 *reg_parm_seen = 1;
897 if (args[i].value == 0)
899 push_temp_slots ();
900 args[i].value = expand_normal (args[i].tree_value);
901 preserve_temp_slots (args[i].value);
902 pop_temp_slots ();
905 /* If we are to promote the function arg to a wider mode,
906 do it now. */
908 if (args[i].mode != TYPE_MODE (TREE_TYPE (args[i].tree_value)))
909 args[i].value
910 = convert_modes (args[i].mode,
911 TYPE_MODE (TREE_TYPE (args[i].tree_value)),
912 args[i].value, args[i].unsignedp);
914 /* If the value is a non-legitimate constant, force it into a
915 pseudo now. TLS symbols sometimes need a call to resolve. */
916 if (CONSTANT_P (args[i].value)
917 && !targetm.legitimate_constant_p (args[i].mode, args[i].value))
918 args[i].value = force_reg (args[i].mode, args[i].value);
920 /* If we're going to have to load the value by parts, pull the
921 parts into pseudos. The part extraction process can involve
922 non-trivial computation. */
923 if (GET_CODE (args[i].reg) == PARALLEL)
925 tree type = TREE_TYPE (args[i].tree_value);
926 args[i].parallel_value
927 = emit_group_load_into_temps (args[i].reg, args[i].value,
928 type, int_size_in_bytes (type));
931 /* If the value is expensive, and we are inside an appropriately
932 short loop, put the value into a pseudo and then put the pseudo
933 into the hard reg.
935 For small register classes, also do this if this call uses
936 register parameters. This is to avoid reload conflicts while
937 loading the parameters registers. */
939 else if ((! (REG_P (args[i].value)
940 || (GET_CODE (args[i].value) == SUBREG
941 && REG_P (SUBREG_REG (args[i].value)))))
942 && args[i].mode != BLKmode
943 && (set_src_cost (args[i].value, args[i].mode,
944 optimize_insn_for_speed_p ())
945 > COSTS_N_INSNS (1))
946 && ((*reg_parm_seen
947 && targetm.small_register_classes_for_mode_p (args[i].mode))
948 || optimize))
949 args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
953 #ifdef REG_PARM_STACK_SPACE
955 /* The argument list is the property of the called routine and it
956 may clobber it. If the fixed area has been used for previous
957 parameters, we must save and restore it. */
959 static rtx
960 save_fixed_argument_area (int reg_parm_stack_space, rtx argblock, int *low_to_save, int *high_to_save)
962 int low;
963 int high;
965 /* Compute the boundary of the area that needs to be saved, if any. */
966 high = reg_parm_stack_space;
967 if (ARGS_GROW_DOWNWARD)
968 high += 1;
970 if (high > highest_outgoing_arg_in_use)
971 high = highest_outgoing_arg_in_use;
973 for (low = 0; low < high; low++)
974 if (stack_usage_map[low] != 0)
976 int num_to_save;
977 machine_mode save_mode;
978 int delta;
979 rtx addr;
980 rtx stack_area;
981 rtx save_area;
983 while (stack_usage_map[--high] == 0)
986 *low_to_save = low;
987 *high_to_save = high;
989 num_to_save = high - low + 1;
990 save_mode = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
992 /* If we don't have the required alignment, must do this
993 in BLKmode. */
994 if ((low & (MIN (GET_MODE_SIZE (save_mode),
995 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
996 save_mode = BLKmode;
998 if (ARGS_GROW_DOWNWARD)
999 delta = -high;
1000 else
1001 delta = low;
1003 addr = plus_constant (Pmode, argblock, delta);
1004 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
1006 set_mem_align (stack_area, PARM_BOUNDARY);
1007 if (save_mode == BLKmode)
1009 save_area = assign_stack_temp (BLKmode, num_to_save);
1010 emit_block_move (validize_mem (save_area), stack_area,
1011 GEN_INT (num_to_save), BLOCK_OP_CALL_PARM);
1013 else
1015 save_area = gen_reg_rtx (save_mode);
1016 emit_move_insn (save_area, stack_area);
1019 return save_area;
1022 return NULL_RTX;
1025 static void
1026 restore_fixed_argument_area (rtx save_area, rtx argblock, int high_to_save, int low_to_save)
1028 machine_mode save_mode = GET_MODE (save_area);
1029 int delta;
1030 rtx addr, stack_area;
1032 if (ARGS_GROW_DOWNWARD)
1033 delta = -high_to_save;
1034 else
1035 delta = low_to_save;
1037 addr = plus_constant (Pmode, argblock, delta);
1038 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
1039 set_mem_align (stack_area, PARM_BOUNDARY);
1041 if (save_mode != BLKmode)
1042 emit_move_insn (stack_area, save_area);
1043 else
1044 emit_block_move (stack_area, validize_mem (save_area),
1045 GEN_INT (high_to_save - low_to_save + 1),
1046 BLOCK_OP_CALL_PARM);
1048 #endif /* REG_PARM_STACK_SPACE */
1050 /* If any elements in ARGS refer to parameters that are to be passed in
1051 registers, but not in memory, and whose alignment does not permit a
1052 direct copy into registers. Copy the values into a group of pseudos
1053 which we will later copy into the appropriate hard registers.
1055 Pseudos for each unaligned argument will be stored into the array
1056 args[argnum].aligned_regs. The caller is responsible for deallocating
1057 the aligned_regs array if it is nonzero. */
1059 static void
1060 store_unaligned_arguments_into_pseudos (struct arg_data *args, int num_actuals)
1062 int i, j;
1064 for (i = 0; i < num_actuals; i++)
1065 if (args[i].reg != 0 && ! args[i].pass_on_stack
1066 && GET_CODE (args[i].reg) != PARALLEL
1067 && args[i].mode == BLKmode
1068 && MEM_P (args[i].value)
1069 && (MEM_ALIGN (args[i].value)
1070 < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
1072 int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1073 int endian_correction = 0;
1075 if (args[i].partial)
1077 gcc_assert (args[i].partial % UNITS_PER_WORD == 0);
1078 args[i].n_aligned_regs = args[i].partial / UNITS_PER_WORD;
1080 else
1082 args[i].n_aligned_regs
1083 = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1086 args[i].aligned_regs = XNEWVEC (rtx, args[i].n_aligned_regs);
1088 /* Structures smaller than a word are normally aligned to the
1089 least significant byte. On a BYTES_BIG_ENDIAN machine,
1090 this means we must skip the empty high order bytes when
1091 calculating the bit offset. */
1092 if (bytes < UNITS_PER_WORD
1093 #ifdef BLOCK_REG_PADDING
1094 && (BLOCK_REG_PADDING (args[i].mode,
1095 TREE_TYPE (args[i].tree_value), 1)
1096 == downward)
1097 #else
1098 && BYTES_BIG_ENDIAN
1099 #endif
1101 endian_correction = BITS_PER_WORD - bytes * BITS_PER_UNIT;
1103 for (j = 0; j < args[i].n_aligned_regs; j++)
1105 rtx reg = gen_reg_rtx (word_mode);
1106 rtx word = operand_subword_force (args[i].value, j, BLKmode);
1107 int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
1109 args[i].aligned_regs[j] = reg;
1110 word = extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
1111 word_mode, word_mode, false);
1113 /* There is no need to restrict this code to loading items
1114 in TYPE_ALIGN sized hunks. The bitfield instructions can
1115 load up entire word sized registers efficiently.
1117 ??? This may not be needed anymore.
1118 We use to emit a clobber here but that doesn't let later
1119 passes optimize the instructions we emit. By storing 0 into
1120 the register later passes know the first AND to zero out the
1121 bitfield being set in the register is unnecessary. The store
1122 of 0 will be deleted as will at least the first AND. */
1124 emit_move_insn (reg, const0_rtx);
1126 bytes -= bitsize / BITS_PER_UNIT;
1127 store_bit_field (reg, bitsize, endian_correction, 0, 0,
1128 word_mode, word, false);
1133 /* Issue an error if CALL_EXPR was flagged as requiring
1134 tall-call optimization. */
1136 static void
1137 maybe_complain_about_tail_call (tree call_expr, const char *reason)
1139 gcc_assert (TREE_CODE (call_expr) == CALL_EXPR);
1140 if (!CALL_EXPR_MUST_TAIL_CALL (call_expr))
1141 return;
1143 error_at (EXPR_LOCATION (call_expr), "cannot tail-call: %s", reason);
1146 /* Fill in ARGS_SIZE and ARGS array based on the parameters found in
1147 CALL_EXPR EXP.
1149 NUM_ACTUALS is the total number of parameters.
1151 N_NAMED_ARGS is the total number of named arguments.
1153 STRUCT_VALUE_ADDR_VALUE is the implicit argument for a struct return
1154 value, or null.
1156 FNDECL is the tree code for the target of this call (if known)
1158 ARGS_SO_FAR holds state needed by the target to know where to place
1159 the next argument.
1161 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
1162 for arguments which are passed in registers.
1164 OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
1165 and may be modified by this routine.
1167 OLD_PENDING_ADJ, MUST_PREALLOCATE and FLAGS are pointers to integer
1168 flags which may be modified by this routine.
1170 MAY_TAILCALL is cleared if we encounter an invisible pass-by-reference
1171 that requires allocation of stack space.
1173 CALL_FROM_THUNK_P is true if this call is the jump from a thunk to
1174 the thunked-to function. */
1176 static void
1177 initialize_argument_information (int num_actuals ATTRIBUTE_UNUSED,
1178 struct arg_data *args,
1179 struct args_size *args_size,
1180 int n_named_args ATTRIBUTE_UNUSED,
1181 tree exp, tree struct_value_addr_value,
1182 tree fndecl, tree fntype,
1183 cumulative_args_t args_so_far,
1184 int reg_parm_stack_space,
1185 rtx *old_stack_level, int *old_pending_adj,
1186 int *must_preallocate, int *ecf_flags,
1187 bool *may_tailcall, bool call_from_thunk_p)
1189 CUMULATIVE_ARGS *args_so_far_pnt = get_cumulative_args (args_so_far);
1190 location_t loc = EXPR_LOCATION (exp);
1192 /* Count arg position in order args appear. */
1193 int argpos;
1195 int i;
1197 args_size->constant = 0;
1198 args_size->var = 0;
1200 bitmap_obstack_initialize (NULL);
1202 /* In this loop, we consider args in the order they are written.
1203 We fill up ARGS from the back. */
1205 i = num_actuals - 1;
1207 int j = i, ptr_arg = -1;
1208 call_expr_arg_iterator iter;
1209 tree arg;
1210 bitmap slots = NULL;
1212 if (struct_value_addr_value)
1214 args[j].tree_value = struct_value_addr_value;
1215 j--;
1217 /* If we pass structure address then we need to
1218 create bounds for it. Since created bounds is
1219 a call statement, we expand it right here to avoid
1220 fixing all other places where it may be expanded. */
1221 if (CALL_WITH_BOUNDS_P (exp))
1223 args[j].value = gen_reg_rtx (targetm.chkp_bound_mode ());
1224 args[j].tree_value
1225 = chkp_make_bounds_for_struct_addr (struct_value_addr_value);
1226 expand_expr_real (args[j].tree_value, args[j].value, VOIDmode,
1227 EXPAND_NORMAL, 0, false);
1228 args[j].pointer_arg = j + 1;
1229 j--;
1232 argpos = 0;
1233 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
1235 tree argtype = TREE_TYPE (arg);
1237 /* Remember last param with pointer and associate it
1238 with following pointer bounds. */
1239 if (CALL_WITH_BOUNDS_P (exp)
1240 && chkp_type_has_pointer (argtype))
1242 if (slots)
1243 BITMAP_FREE (slots);
1244 ptr_arg = j;
1245 if (!BOUNDED_TYPE_P (argtype))
1247 slots = BITMAP_ALLOC (NULL);
1248 chkp_find_bound_slots (argtype, slots);
1251 else if (CALL_WITH_BOUNDS_P (exp)
1252 && pass_by_reference (NULL, TYPE_MODE (argtype), argtype,
1253 argpos < n_named_args))
1255 if (slots)
1256 BITMAP_FREE (slots);
1257 ptr_arg = j;
1259 else if (POINTER_BOUNDS_TYPE_P (argtype))
1261 /* We expect bounds in instrumented calls only.
1262 Otherwise it is a sign we lost flag due to some optimization
1263 and may emit call args incorrectly. */
1264 gcc_assert (CALL_WITH_BOUNDS_P (exp));
1266 /* For structures look for the next available pointer. */
1267 if (ptr_arg != -1 && slots)
1269 unsigned bnd_no = bitmap_first_set_bit (slots);
1270 args[j].pointer_offset =
1271 bnd_no * POINTER_SIZE / BITS_PER_UNIT;
1273 bitmap_clear_bit (slots, bnd_no);
1275 /* Check we have no more pointers in the structure. */
1276 if (bitmap_empty_p (slots))
1277 BITMAP_FREE (slots);
1279 args[j].pointer_arg = ptr_arg;
1281 /* Check we covered all pointers in the previous
1282 non bounds arg. */
1283 if (!slots)
1284 ptr_arg = -1;
1286 else
1287 ptr_arg = -1;
1289 if (targetm.calls.split_complex_arg
1290 && argtype
1291 && TREE_CODE (argtype) == COMPLEX_TYPE
1292 && targetm.calls.split_complex_arg (argtype))
1294 tree subtype = TREE_TYPE (argtype);
1295 args[j].tree_value = build1 (REALPART_EXPR, subtype, arg);
1296 j--;
1297 args[j].tree_value = build1 (IMAGPART_EXPR, subtype, arg);
1299 else
1300 args[j].tree_value = arg;
1301 j--;
1302 argpos++;
1305 if (slots)
1306 BITMAP_FREE (slots);
1309 bitmap_obstack_release (NULL);
1311 /* I counts args in order (to be) pushed; ARGPOS counts in order written. */
1312 for (argpos = 0; argpos < num_actuals; i--, argpos++)
1314 tree type = TREE_TYPE (args[i].tree_value);
1315 int unsignedp;
1316 machine_mode mode;
1318 /* Replace erroneous argument with constant zero. */
1319 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1320 args[i].tree_value = integer_zero_node, type = integer_type_node;
1322 /* If TYPE is a transparent union or record, pass things the way
1323 we would pass the first field of the union or record. We have
1324 already verified that the modes are the same. */
1325 if ((TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == RECORD_TYPE)
1326 && TYPE_TRANSPARENT_AGGR (type))
1327 type = TREE_TYPE (first_field (type));
1329 /* Decide where to pass this arg.
1331 args[i].reg is nonzero if all or part is passed in registers.
1333 args[i].partial is nonzero if part but not all is passed in registers,
1334 and the exact value says how many bytes are passed in registers.
1336 args[i].pass_on_stack is nonzero if the argument must at least be
1337 computed on the stack. It may then be loaded back into registers
1338 if args[i].reg is nonzero.
1340 These decisions are driven by the FUNCTION_... macros and must agree
1341 with those made by function.c. */
1343 /* See if this argument should be passed by invisible reference. */
1344 if (pass_by_reference (args_so_far_pnt, TYPE_MODE (type),
1345 type, argpos < n_named_args))
1347 bool callee_copies;
1348 tree base = NULL_TREE;
1350 callee_copies
1351 = reference_callee_copied (args_so_far_pnt, TYPE_MODE (type),
1352 type, argpos < n_named_args);
1354 /* If we're compiling a thunk, pass through invisible references
1355 instead of making a copy. */
1356 if (call_from_thunk_p
1357 || (callee_copies
1358 && !TREE_ADDRESSABLE (type)
1359 && (base = get_base_address (args[i].tree_value))
1360 && TREE_CODE (base) != SSA_NAME
1361 && (!DECL_P (base) || MEM_P (DECL_RTL (base)))))
1363 /* We may have turned the parameter value into an SSA name.
1364 Go back to the original parameter so we can take the
1365 address. */
1366 if (TREE_CODE (args[i].tree_value) == SSA_NAME)
1368 gcc_assert (SSA_NAME_IS_DEFAULT_DEF (args[i].tree_value));
1369 args[i].tree_value = SSA_NAME_VAR (args[i].tree_value);
1370 gcc_assert (TREE_CODE (args[i].tree_value) == PARM_DECL);
1372 /* Argument setup code may have copied the value to register. We
1373 revert that optimization now because the tail call code must
1374 use the original location. */
1375 if (TREE_CODE (args[i].tree_value) == PARM_DECL
1376 && !MEM_P (DECL_RTL (args[i].tree_value))
1377 && DECL_INCOMING_RTL (args[i].tree_value)
1378 && MEM_P (DECL_INCOMING_RTL (args[i].tree_value)))
1379 set_decl_rtl (args[i].tree_value,
1380 DECL_INCOMING_RTL (args[i].tree_value));
1382 mark_addressable (args[i].tree_value);
1384 /* We can't use sibcalls if a callee-copied argument is
1385 stored in the current function's frame. */
1386 if (!call_from_thunk_p && DECL_P (base) && !TREE_STATIC (base))
1388 *may_tailcall = false;
1389 maybe_complain_about_tail_call (exp,
1390 "a callee-copied argument is"
1391 " stored in the current "
1392 " function's frame");
1395 args[i].tree_value = build_fold_addr_expr_loc (loc,
1396 args[i].tree_value);
1397 type = TREE_TYPE (args[i].tree_value);
1399 if (*ecf_flags & ECF_CONST)
1400 *ecf_flags &= ~(ECF_CONST | ECF_LOOPING_CONST_OR_PURE);
1402 else
1404 /* We make a copy of the object and pass the address to the
1405 function being called. */
1406 rtx copy;
1408 if (!COMPLETE_TYPE_P (type)
1409 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
1410 || (flag_stack_check == GENERIC_STACK_CHECK
1411 && compare_tree_int (TYPE_SIZE_UNIT (type),
1412 STACK_CHECK_MAX_VAR_SIZE) > 0))
1414 /* This is a variable-sized object. Make space on the stack
1415 for it. */
1416 rtx size_rtx = expr_size (args[i].tree_value);
1418 if (*old_stack_level == 0)
1420 emit_stack_save (SAVE_BLOCK, old_stack_level);
1421 *old_pending_adj = pending_stack_adjust;
1422 pending_stack_adjust = 0;
1425 /* We can pass TRUE as the 4th argument because we just
1426 saved the stack pointer and will restore it right after
1427 the call. */
1428 copy = allocate_dynamic_stack_space (size_rtx,
1429 TYPE_ALIGN (type),
1430 TYPE_ALIGN (type),
1431 true);
1432 copy = gen_rtx_MEM (BLKmode, copy);
1433 set_mem_attributes (copy, type, 1);
1435 else
1436 copy = assign_temp (type, 1, 0);
1438 store_expr (args[i].tree_value, copy, 0, false, false);
1440 /* Just change the const function to pure and then let
1441 the next test clear the pure based on
1442 callee_copies. */
1443 if (*ecf_flags & ECF_CONST)
1445 *ecf_flags &= ~ECF_CONST;
1446 *ecf_flags |= ECF_PURE;
1449 if (!callee_copies && *ecf_flags & ECF_PURE)
1450 *ecf_flags &= ~(ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
1452 args[i].tree_value
1453 = build_fold_addr_expr_loc (loc, make_tree (type, copy));
1454 type = TREE_TYPE (args[i].tree_value);
1455 *may_tailcall = false;
1456 maybe_complain_about_tail_call (exp,
1457 "argument must be passed"
1458 " by copying");
1462 unsignedp = TYPE_UNSIGNED (type);
1463 mode = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
1464 fndecl ? TREE_TYPE (fndecl) : fntype, 0);
1466 args[i].unsignedp = unsignedp;
1467 args[i].mode = mode;
1469 args[i].reg = targetm.calls.function_arg (args_so_far, mode, type,
1470 argpos < n_named_args);
1472 if (args[i].reg && CONST_INT_P (args[i].reg))
1474 args[i].special_slot = args[i].reg;
1475 args[i].reg = NULL;
1478 /* If this is a sibling call and the machine has register windows, the
1479 register window has to be unwinded before calling the routine, so
1480 arguments have to go into the incoming registers. */
1481 if (targetm.calls.function_incoming_arg != targetm.calls.function_arg)
1482 args[i].tail_call_reg
1483 = targetm.calls.function_incoming_arg (args_so_far, mode, type,
1484 argpos < n_named_args);
1485 else
1486 args[i].tail_call_reg = args[i].reg;
1488 if (args[i].reg)
1489 args[i].partial
1490 = targetm.calls.arg_partial_bytes (args_so_far, mode, type,
1491 argpos < n_named_args);
1493 args[i].pass_on_stack = targetm.calls.must_pass_in_stack (mode, type);
1495 /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
1496 it means that we are to pass this arg in the register(s) designated
1497 by the PARALLEL, but also to pass it in the stack. */
1498 if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
1499 && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
1500 args[i].pass_on_stack = 1;
1502 /* If this is an addressable type, we must preallocate the stack
1503 since we must evaluate the object into its final location.
1505 If this is to be passed in both registers and the stack, it is simpler
1506 to preallocate. */
1507 if (TREE_ADDRESSABLE (type)
1508 || (args[i].pass_on_stack && args[i].reg != 0))
1509 *must_preallocate = 1;
1511 /* No stack allocation and padding for bounds. */
1512 if (POINTER_BOUNDS_P (args[i].tree_value))
1514 /* Compute the stack-size of this argument. */
1515 else if (args[i].reg == 0 || args[i].partial != 0
1516 || reg_parm_stack_space > 0
1517 || args[i].pass_on_stack)
1518 locate_and_pad_parm (mode, type,
1519 #ifdef STACK_PARMS_IN_REG_PARM_AREA
1521 #else
1522 args[i].reg != 0,
1523 #endif
1524 reg_parm_stack_space,
1525 args[i].pass_on_stack ? 0 : args[i].partial,
1526 fndecl, args_size, &args[i].locate);
1527 #ifdef BLOCK_REG_PADDING
1528 else
1529 /* The argument is passed entirely in registers. See at which
1530 end it should be padded. */
1531 args[i].locate.where_pad =
1532 BLOCK_REG_PADDING (mode, type,
1533 int_size_in_bytes (type) <= UNITS_PER_WORD);
1534 #endif
1536 /* Update ARGS_SIZE, the total stack space for args so far. */
1538 args_size->constant += args[i].locate.size.constant;
1539 if (args[i].locate.size.var)
1540 ADD_PARM_SIZE (*args_size, args[i].locate.size.var);
1542 /* Increment ARGS_SO_FAR, which has info about which arg-registers
1543 have been used, etc. */
1545 targetm.calls.function_arg_advance (args_so_far, TYPE_MODE (type),
1546 type, argpos < n_named_args);
1550 /* Update ARGS_SIZE to contain the total size for the argument block.
1551 Return the original constant component of the argument block's size.
1553 REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
1554 for arguments passed in registers. */
1556 static int
1557 compute_argument_block_size (int reg_parm_stack_space,
1558 struct args_size *args_size,
1559 tree fndecl ATTRIBUTE_UNUSED,
1560 tree fntype ATTRIBUTE_UNUSED,
1561 int preferred_stack_boundary ATTRIBUTE_UNUSED)
1563 int unadjusted_args_size = args_size->constant;
1565 /* For accumulate outgoing args mode we don't need to align, since the frame
1566 will be already aligned. Align to STACK_BOUNDARY in order to prevent
1567 backends from generating misaligned frame sizes. */
1568 if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
1569 preferred_stack_boundary = STACK_BOUNDARY;
1571 /* Compute the actual size of the argument block required. The variable
1572 and constant sizes must be combined, the size may have to be rounded,
1573 and there may be a minimum required size. */
1575 if (args_size->var)
1577 args_size->var = ARGS_SIZE_TREE (*args_size);
1578 args_size->constant = 0;
1580 preferred_stack_boundary /= BITS_PER_UNIT;
1581 if (preferred_stack_boundary > 1)
1583 /* We don't handle this case yet. To handle it correctly we have
1584 to add the delta, round and subtract the delta.
1585 Currently no machine description requires this support. */
1586 gcc_assert (!(stack_pointer_delta & (preferred_stack_boundary - 1)));
1587 args_size->var = round_up (args_size->var, preferred_stack_boundary);
1590 if (reg_parm_stack_space > 0)
1592 args_size->var
1593 = size_binop (MAX_EXPR, args_size->var,
1594 ssize_int (reg_parm_stack_space));
1596 /* The area corresponding to register parameters is not to count in
1597 the size of the block we need. So make the adjustment. */
1598 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1599 args_size->var
1600 = size_binop (MINUS_EXPR, args_size->var,
1601 ssize_int (reg_parm_stack_space));
1604 else
1606 preferred_stack_boundary /= BITS_PER_UNIT;
1607 if (preferred_stack_boundary < 1)
1608 preferred_stack_boundary = 1;
1609 args_size->constant = (((args_size->constant
1610 + stack_pointer_delta
1611 + preferred_stack_boundary - 1)
1612 / preferred_stack_boundary
1613 * preferred_stack_boundary)
1614 - stack_pointer_delta);
1616 args_size->constant = MAX (args_size->constant,
1617 reg_parm_stack_space);
1619 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1620 args_size->constant -= reg_parm_stack_space;
1622 return unadjusted_args_size;
1625 /* Precompute parameters as needed for a function call.
1627 FLAGS is mask of ECF_* constants.
1629 NUM_ACTUALS is the number of arguments.
1631 ARGS is an array containing information for each argument; this
1632 routine fills in the INITIAL_VALUE and VALUE fields for each
1633 precomputed argument. */
1635 static void
1636 precompute_arguments (int num_actuals, struct arg_data *args)
1638 int i;
1640 /* If this is a libcall, then precompute all arguments so that we do not
1641 get extraneous instructions emitted as part of the libcall sequence. */
1643 /* If we preallocated the stack space, and some arguments must be passed
1644 on the stack, then we must precompute any parameter which contains a
1645 function call which will store arguments on the stack.
1646 Otherwise, evaluating the parameter may clobber previous parameters
1647 which have already been stored into the stack. (we have code to avoid
1648 such case by saving the outgoing stack arguments, but it results in
1649 worse code) */
1650 if (!ACCUMULATE_OUTGOING_ARGS)
1651 return;
1653 for (i = 0; i < num_actuals; i++)
1655 tree type;
1656 machine_mode mode;
1658 if (TREE_CODE (args[i].tree_value) != CALL_EXPR)
1659 continue;
1661 /* If this is an addressable type, we cannot pre-evaluate it. */
1662 type = TREE_TYPE (args[i].tree_value);
1663 gcc_assert (!TREE_ADDRESSABLE (type));
1665 args[i].initial_value = args[i].value
1666 = expand_normal (args[i].tree_value);
1668 mode = TYPE_MODE (type);
1669 if (mode != args[i].mode)
1671 int unsignedp = args[i].unsignedp;
1672 args[i].value
1673 = convert_modes (args[i].mode, mode,
1674 args[i].value, args[i].unsignedp);
1676 /* CSE will replace this only if it contains args[i].value
1677 pseudo, so convert it down to the declared mode using
1678 a SUBREG. */
1679 if (REG_P (args[i].value)
1680 && GET_MODE_CLASS (args[i].mode) == MODE_INT
1681 && promote_mode (type, mode, &unsignedp) != args[i].mode)
1683 args[i].initial_value
1684 = gen_lowpart_SUBREG (mode, args[i].value);
1685 SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1686 SUBREG_PROMOTED_SET (args[i].initial_value, args[i].unsignedp);
1692 /* Given the current state of MUST_PREALLOCATE and information about
1693 arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
1694 compute and return the final value for MUST_PREALLOCATE. */
1696 static int
1697 finalize_must_preallocate (int must_preallocate, int num_actuals,
1698 struct arg_data *args, struct args_size *args_size)
1700 /* See if we have or want to preallocate stack space.
1702 If we would have to push a partially-in-regs parm
1703 before other stack parms, preallocate stack space instead.
1705 If the size of some parm is not a multiple of the required stack
1706 alignment, we must preallocate.
1708 If the total size of arguments that would otherwise create a copy in
1709 a temporary (such as a CALL) is more than half the total argument list
1710 size, preallocation is faster.
1712 Another reason to preallocate is if we have a machine (like the m88k)
1713 where stack alignment is required to be maintained between every
1714 pair of insns, not just when the call is made. However, we assume here
1715 that such machines either do not have push insns (and hence preallocation
1716 would occur anyway) or the problem is taken care of with
1717 PUSH_ROUNDING. */
1719 if (! must_preallocate)
1721 int partial_seen = 0;
1722 int copy_to_evaluate_size = 0;
1723 int i;
1725 for (i = 0; i < num_actuals && ! must_preallocate; i++)
1727 if (args[i].partial > 0 && ! args[i].pass_on_stack)
1728 partial_seen = 1;
1729 else if (partial_seen && args[i].reg == 0)
1730 must_preallocate = 1;
1731 /* We preallocate in case there are bounds passed
1732 in the bounds table to have precomputed address
1733 for bounds association. */
1734 else if (POINTER_BOUNDS_P (args[i].tree_value)
1735 && !args[i].reg)
1736 must_preallocate = 1;
1738 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1739 && (TREE_CODE (args[i].tree_value) == CALL_EXPR
1740 || TREE_CODE (args[i].tree_value) == TARGET_EXPR
1741 || TREE_CODE (args[i].tree_value) == COND_EXPR
1742 || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
1743 copy_to_evaluate_size
1744 += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1747 if (copy_to_evaluate_size * 2 >= args_size->constant
1748 && args_size->constant > 0)
1749 must_preallocate = 1;
1751 return must_preallocate;
1754 /* If we preallocated stack space, compute the address of each argument
1755 and store it into the ARGS array.
1757 We need not ensure it is a valid memory address here; it will be
1758 validized when it is used.
1760 ARGBLOCK is an rtx for the address of the outgoing arguments. */
1762 static void
1763 compute_argument_addresses (struct arg_data *args, rtx argblock, int num_actuals)
1765 if (argblock)
1767 rtx arg_reg = argblock;
1768 int i, arg_offset = 0;
1770 if (GET_CODE (argblock) == PLUS)
1771 arg_reg = XEXP (argblock, 0), arg_offset = INTVAL (XEXP (argblock, 1));
1773 for (i = 0; i < num_actuals; i++)
1775 rtx offset = ARGS_SIZE_RTX (args[i].locate.offset);
1776 rtx slot_offset = ARGS_SIZE_RTX (args[i].locate.slot_offset);
1777 rtx addr;
1778 unsigned int align, boundary;
1779 unsigned int units_on_stack = 0;
1780 machine_mode partial_mode = VOIDmode;
1782 /* Skip this parm if it will not be passed on the stack. */
1783 if (! args[i].pass_on_stack
1784 && args[i].reg != 0
1785 && args[i].partial == 0)
1786 continue;
1788 /* Pointer Bounds are never passed on the stack. */
1789 if (POINTER_BOUNDS_P (args[i].tree_value))
1790 continue;
1792 if (CONST_INT_P (offset))
1793 addr = plus_constant (Pmode, arg_reg, INTVAL (offset));
1794 else
1795 addr = gen_rtx_PLUS (Pmode, arg_reg, offset);
1797 addr = plus_constant (Pmode, addr, arg_offset);
1799 if (args[i].partial != 0)
1801 /* Only part of the parameter is being passed on the stack.
1802 Generate a simple memory reference of the correct size. */
1803 units_on_stack = args[i].locate.size.constant;
1804 partial_mode = mode_for_size (units_on_stack * BITS_PER_UNIT,
1805 MODE_INT, 1);
1806 args[i].stack = gen_rtx_MEM (partial_mode, addr);
1807 set_mem_size (args[i].stack, units_on_stack);
1809 else
1811 args[i].stack = gen_rtx_MEM (args[i].mode, addr);
1812 set_mem_attributes (args[i].stack,
1813 TREE_TYPE (args[i].tree_value), 1);
1815 align = BITS_PER_UNIT;
1816 boundary = args[i].locate.boundary;
1817 if (args[i].locate.where_pad != downward)
1818 align = boundary;
1819 else if (CONST_INT_P (offset))
1821 align = INTVAL (offset) * BITS_PER_UNIT | boundary;
1822 align = align & -align;
1824 set_mem_align (args[i].stack, align);
1826 if (CONST_INT_P (slot_offset))
1827 addr = plus_constant (Pmode, arg_reg, INTVAL (slot_offset));
1828 else
1829 addr = gen_rtx_PLUS (Pmode, arg_reg, slot_offset);
1831 addr = plus_constant (Pmode, addr, arg_offset);
1833 if (args[i].partial != 0)
1835 /* Only part of the parameter is being passed on the stack.
1836 Generate a simple memory reference of the correct size.
1838 args[i].stack_slot = gen_rtx_MEM (partial_mode, addr);
1839 set_mem_size (args[i].stack_slot, units_on_stack);
1841 else
1843 args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
1844 set_mem_attributes (args[i].stack_slot,
1845 TREE_TYPE (args[i].tree_value), 1);
1847 set_mem_align (args[i].stack_slot, args[i].locate.boundary);
1849 /* Function incoming arguments may overlap with sibling call
1850 outgoing arguments and we cannot allow reordering of reads
1851 from function arguments with stores to outgoing arguments
1852 of sibling calls. */
1853 set_mem_alias_set (args[i].stack, 0);
1854 set_mem_alias_set (args[i].stack_slot, 0);
1859 /* Given a FNDECL and EXP, return an rtx suitable for use as a target address
1860 in a call instruction.
1862 FNDECL is the tree node for the target function. For an indirect call
1863 FNDECL will be NULL_TREE.
1865 ADDR is the operand 0 of CALL_EXPR for this call. */
1867 static rtx
1868 rtx_for_function_call (tree fndecl, tree addr)
1870 rtx funexp;
1872 /* Get the function to call, in the form of RTL. */
1873 if (fndecl)
1875 if (!TREE_USED (fndecl) && fndecl != current_function_decl)
1876 TREE_USED (fndecl) = 1;
1878 /* Get a SYMBOL_REF rtx for the function address. */
1879 funexp = XEXP (DECL_RTL (fndecl), 0);
1881 else
1882 /* Generate an rtx (probably a pseudo-register) for the address. */
1884 push_temp_slots ();
1885 funexp = expand_normal (addr);
1886 pop_temp_slots (); /* FUNEXP can't be BLKmode. */
1888 return funexp;
1891 /* Internal state for internal_arg_pointer_based_exp and its helpers. */
1892 static struct
1894 /* Last insn that has been scanned by internal_arg_pointer_based_exp_scan,
1895 or NULL_RTX if none has been scanned yet. */
1896 rtx_insn *scan_start;
1897 /* Vector indexed by REGNO - FIRST_PSEUDO_REGISTER, recording if a pseudo is
1898 based on crtl->args.internal_arg_pointer. The element is NULL_RTX if the
1899 pseudo isn't based on it, a CONST_INT offset if the pseudo is based on it
1900 with fixed offset, or PC if this is with variable or unknown offset. */
1901 vec<rtx> cache;
1902 } internal_arg_pointer_exp_state;
1904 static rtx internal_arg_pointer_based_exp (const_rtx, bool);
1906 /* Helper function for internal_arg_pointer_based_exp. Scan insns in
1907 the tail call sequence, starting with first insn that hasn't been
1908 scanned yet, and note for each pseudo on the LHS whether it is based
1909 on crtl->args.internal_arg_pointer or not, and what offset from that
1910 that pointer it has. */
1912 static void
1913 internal_arg_pointer_based_exp_scan (void)
1915 rtx_insn *insn, *scan_start = internal_arg_pointer_exp_state.scan_start;
1917 if (scan_start == NULL_RTX)
1918 insn = get_insns ();
1919 else
1920 insn = NEXT_INSN (scan_start);
1922 while (insn)
1924 rtx set = single_set (insn);
1925 if (set && REG_P (SET_DEST (set)) && !HARD_REGISTER_P (SET_DEST (set)))
1927 rtx val = NULL_RTX;
1928 unsigned int idx = REGNO (SET_DEST (set)) - FIRST_PSEUDO_REGISTER;
1929 /* Punt on pseudos set multiple times. */
1930 if (idx < internal_arg_pointer_exp_state.cache.length ()
1931 && (internal_arg_pointer_exp_state.cache[idx]
1932 != NULL_RTX))
1933 val = pc_rtx;
1934 else
1935 val = internal_arg_pointer_based_exp (SET_SRC (set), false);
1936 if (val != NULL_RTX)
1938 if (idx >= internal_arg_pointer_exp_state.cache.length ())
1939 internal_arg_pointer_exp_state.cache
1940 .safe_grow_cleared (idx + 1);
1941 internal_arg_pointer_exp_state.cache[idx] = val;
1944 if (NEXT_INSN (insn) == NULL_RTX)
1945 scan_start = insn;
1946 insn = NEXT_INSN (insn);
1949 internal_arg_pointer_exp_state.scan_start = scan_start;
1952 /* Compute whether RTL is based on crtl->args.internal_arg_pointer. Return
1953 NULL_RTX if RTL isn't based on it, a CONST_INT offset if RTL is based on
1954 it with fixed offset, or PC if this is with variable or unknown offset.
1955 TOPLEVEL is true if the function is invoked at the topmost level. */
1957 static rtx
1958 internal_arg_pointer_based_exp (const_rtx rtl, bool toplevel)
1960 if (CONSTANT_P (rtl))
1961 return NULL_RTX;
1963 if (rtl == crtl->args.internal_arg_pointer)
1964 return const0_rtx;
1966 if (REG_P (rtl) && HARD_REGISTER_P (rtl))
1967 return NULL_RTX;
1969 if (GET_CODE (rtl) == PLUS && CONST_INT_P (XEXP (rtl, 1)))
1971 rtx val = internal_arg_pointer_based_exp (XEXP (rtl, 0), toplevel);
1972 if (val == NULL_RTX || val == pc_rtx)
1973 return val;
1974 return plus_constant (Pmode, val, INTVAL (XEXP (rtl, 1)));
1977 /* When called at the topmost level, scan pseudo assignments in between the
1978 last scanned instruction in the tail call sequence and the latest insn
1979 in that sequence. */
1980 if (toplevel)
1981 internal_arg_pointer_based_exp_scan ();
1983 if (REG_P (rtl))
1985 unsigned int idx = REGNO (rtl) - FIRST_PSEUDO_REGISTER;
1986 if (idx < internal_arg_pointer_exp_state.cache.length ())
1987 return internal_arg_pointer_exp_state.cache[idx];
1989 return NULL_RTX;
1992 subrtx_iterator::array_type array;
1993 FOR_EACH_SUBRTX (iter, array, rtl, NONCONST)
1995 const_rtx x = *iter;
1996 if (REG_P (x) && internal_arg_pointer_based_exp (x, false) != NULL_RTX)
1997 return pc_rtx;
1998 if (MEM_P (x))
1999 iter.skip_subrtxes ();
2002 return NULL_RTX;
2005 /* Return true if and only if SIZE storage units (usually bytes)
2006 starting from address ADDR overlap with already clobbered argument
2007 area. This function is used to determine if we should give up a
2008 sibcall. */
2010 static bool
2011 mem_overlaps_already_clobbered_arg_p (rtx addr, unsigned HOST_WIDE_INT size)
2013 HOST_WIDE_INT i;
2014 rtx val;
2016 if (bitmap_empty_p (stored_args_map))
2017 return false;
2018 val = internal_arg_pointer_based_exp (addr, true);
2019 if (val == NULL_RTX)
2020 return false;
2021 else if (val == pc_rtx)
2022 return true;
2023 else
2024 i = INTVAL (val);
2026 if (STACK_GROWS_DOWNWARD)
2027 i -= crtl->args.pretend_args_size;
2028 else
2029 i += crtl->args.pretend_args_size;
2032 if (ARGS_GROW_DOWNWARD)
2033 i = -i - size;
2035 if (size > 0)
2037 unsigned HOST_WIDE_INT k;
2039 for (k = 0; k < size; k++)
2040 if (i + k < SBITMAP_SIZE (stored_args_map)
2041 && bitmap_bit_p (stored_args_map, i + k))
2042 return true;
2045 return false;
2048 /* Do the register loads required for any wholly-register parms or any
2049 parms which are passed both on the stack and in a register. Their
2050 expressions were already evaluated.
2052 Mark all register-parms as living through the call, putting these USE
2053 insns in the CALL_INSN_FUNCTION_USAGE field.
2055 When IS_SIBCALL, perform the check_sibcall_argument_overlap
2056 checking, setting *SIBCALL_FAILURE if appropriate. */
2058 static void
2059 load_register_parameters (struct arg_data *args, int num_actuals,
2060 rtx *call_fusage, int flags, int is_sibcall,
2061 int *sibcall_failure)
2063 int i, j;
2065 for (i = 0; i < num_actuals; i++)
2067 rtx reg = ((flags & ECF_SIBCALL)
2068 ? args[i].tail_call_reg : args[i].reg);
2069 if (reg)
2071 int partial = args[i].partial;
2072 int nregs;
2073 int size = 0;
2074 rtx_insn *before_arg = get_last_insn ();
2075 /* Set non-negative if we must move a word at a time, even if
2076 just one word (e.g, partial == 4 && mode == DFmode). Set
2077 to -1 if we just use a normal move insn. This value can be
2078 zero if the argument is a zero size structure. */
2079 nregs = -1;
2080 if (GET_CODE (reg) == PARALLEL)
2082 else if (partial)
2084 gcc_assert (partial % UNITS_PER_WORD == 0);
2085 nregs = partial / UNITS_PER_WORD;
2087 else if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode)
2089 size = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
2090 nregs = (size + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
2092 else
2093 size = GET_MODE_SIZE (args[i].mode);
2095 /* Handle calls that pass values in multiple non-contiguous
2096 locations. The Irix 6 ABI has examples of this. */
2098 if (GET_CODE (reg) == PARALLEL)
2099 emit_group_move (reg, args[i].parallel_value);
2101 /* If simple case, just do move. If normal partial, store_one_arg
2102 has already loaded the register for us. In all other cases,
2103 load the register(s) from memory. */
2105 else if (nregs == -1)
2107 emit_move_insn (reg, args[i].value);
2108 #ifdef BLOCK_REG_PADDING
2109 /* Handle case where we have a value that needs shifting
2110 up to the msb. eg. a QImode value and we're padding
2111 upward on a BYTES_BIG_ENDIAN machine. */
2112 if (size < UNITS_PER_WORD
2113 && (args[i].locate.where_pad
2114 == (BYTES_BIG_ENDIAN ? upward : downward)))
2116 rtx x;
2117 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2119 /* Assigning REG here rather than a temp makes CALL_FUSAGE
2120 report the whole reg as used. Strictly speaking, the
2121 call only uses SIZE bytes at the msb end, but it doesn't
2122 seem worth generating rtl to say that. */
2123 reg = gen_rtx_REG (word_mode, REGNO (reg));
2124 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
2125 if (x != reg)
2126 emit_move_insn (reg, x);
2128 #endif
2131 /* If we have pre-computed the values to put in the registers in
2132 the case of non-aligned structures, copy them in now. */
2134 else if (args[i].n_aligned_regs != 0)
2135 for (j = 0; j < args[i].n_aligned_regs; j++)
2136 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
2137 args[i].aligned_regs[j]);
2139 else if (partial == 0 || args[i].pass_on_stack)
2141 rtx mem = validize_mem (copy_rtx (args[i].value));
2143 /* Check for overlap with already clobbered argument area,
2144 providing that this has non-zero size. */
2145 if (is_sibcall
2146 && size != 0
2147 && (mem_overlaps_already_clobbered_arg_p
2148 (XEXP (args[i].value, 0), size)))
2149 *sibcall_failure = 1;
2151 if (size % UNITS_PER_WORD == 0
2152 || MEM_ALIGN (mem) % BITS_PER_WORD == 0)
2153 move_block_to_reg (REGNO (reg), mem, nregs, args[i].mode);
2154 else
2156 if (nregs > 1)
2157 move_block_to_reg (REGNO (reg), mem, nregs - 1,
2158 args[i].mode);
2159 rtx dest = gen_rtx_REG (word_mode, REGNO (reg) + nregs - 1);
2160 unsigned int bitoff = (nregs - 1) * BITS_PER_WORD;
2161 unsigned int bitsize = size * BITS_PER_UNIT - bitoff;
2162 rtx x = extract_bit_field (mem, bitsize, bitoff, 1, dest,
2163 word_mode, word_mode, false);
2164 if (BYTES_BIG_ENDIAN)
2165 x = expand_shift (LSHIFT_EXPR, word_mode, x,
2166 BITS_PER_WORD - bitsize, dest, 1);
2167 if (x != dest)
2168 emit_move_insn (dest, x);
2171 /* Handle a BLKmode that needs shifting. */
2172 if (nregs == 1 && size < UNITS_PER_WORD
2173 #ifdef BLOCK_REG_PADDING
2174 && args[i].locate.where_pad == downward
2175 #else
2176 && BYTES_BIG_ENDIAN
2177 #endif
2180 rtx dest = gen_rtx_REG (word_mode, REGNO (reg));
2181 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2182 enum tree_code dir = (BYTES_BIG_ENDIAN
2183 ? RSHIFT_EXPR : LSHIFT_EXPR);
2184 rtx x;
2186 x = expand_shift (dir, word_mode, dest, shift, dest, 1);
2187 if (x != dest)
2188 emit_move_insn (dest, x);
2192 /* When a parameter is a block, and perhaps in other cases, it is
2193 possible that it did a load from an argument slot that was
2194 already clobbered. */
2195 if (is_sibcall
2196 && check_sibcall_argument_overlap (before_arg, &args[i], 0))
2197 *sibcall_failure = 1;
2199 /* Handle calls that pass values in multiple non-contiguous
2200 locations. The Irix 6 ABI has examples of this. */
2201 if (GET_CODE (reg) == PARALLEL)
2202 use_group_regs (call_fusage, reg);
2203 else if (nregs == -1)
2204 use_reg_mode (call_fusage, reg,
2205 TYPE_MODE (TREE_TYPE (args[i].tree_value)));
2206 else if (nregs > 0)
2207 use_regs (call_fusage, REGNO (reg), nregs);
2212 /* We need to pop PENDING_STACK_ADJUST bytes. But, if the arguments
2213 wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
2214 bytes, then we would need to push some additional bytes to pad the
2215 arguments. So, we compute an adjust to the stack pointer for an
2216 amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
2217 bytes. Then, when the arguments are pushed the stack will be perfectly
2218 aligned. ARGS_SIZE->CONSTANT is set to the number of bytes that should
2219 be popped after the call. Returns the adjustment. */
2221 static int
2222 combine_pending_stack_adjustment_and_call (int unadjusted_args_size,
2223 struct args_size *args_size,
2224 unsigned int preferred_unit_stack_boundary)
2226 /* The number of bytes to pop so that the stack will be
2227 under-aligned by UNADJUSTED_ARGS_SIZE bytes. */
2228 HOST_WIDE_INT adjustment;
2229 /* The alignment of the stack after the arguments are pushed, if we
2230 just pushed the arguments without adjust the stack here. */
2231 unsigned HOST_WIDE_INT unadjusted_alignment;
2233 unadjusted_alignment
2234 = ((stack_pointer_delta + unadjusted_args_size)
2235 % preferred_unit_stack_boundary);
2237 /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
2238 as possible -- leaving just enough left to cancel out the
2239 UNADJUSTED_ALIGNMENT. In other words, we want to ensure that the
2240 PENDING_STACK_ADJUST is non-negative, and congruent to
2241 -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY. */
2243 /* Begin by trying to pop all the bytes. */
2244 unadjusted_alignment
2245 = (unadjusted_alignment
2246 - (pending_stack_adjust % preferred_unit_stack_boundary));
2247 adjustment = pending_stack_adjust;
2248 /* Push enough additional bytes that the stack will be aligned
2249 after the arguments are pushed. */
2250 if (preferred_unit_stack_boundary > 1)
2252 if (unadjusted_alignment > 0)
2253 adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
2254 else
2255 adjustment += unadjusted_alignment;
2258 /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
2259 bytes after the call. The right number is the entire
2260 PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
2261 by the arguments in the first place. */
2262 args_size->constant
2263 = pending_stack_adjust - adjustment + unadjusted_args_size;
2265 return adjustment;
2268 /* Scan X expression if it does not dereference any argument slots
2269 we already clobbered by tail call arguments (as noted in stored_args_map
2270 bitmap).
2271 Return nonzero if X expression dereferences such argument slots,
2272 zero otherwise. */
2274 static int
2275 check_sibcall_argument_overlap_1 (rtx x)
2277 RTX_CODE code;
2278 int i, j;
2279 const char *fmt;
2281 if (x == NULL_RTX)
2282 return 0;
2284 code = GET_CODE (x);
2286 /* We need not check the operands of the CALL expression itself. */
2287 if (code == CALL)
2288 return 0;
2290 if (code == MEM)
2291 return mem_overlaps_already_clobbered_arg_p (XEXP (x, 0),
2292 GET_MODE_SIZE (GET_MODE (x)));
2294 /* Scan all subexpressions. */
2295 fmt = GET_RTX_FORMAT (code);
2296 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2298 if (*fmt == 'e')
2300 if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
2301 return 1;
2303 else if (*fmt == 'E')
2305 for (j = 0; j < XVECLEN (x, i); j++)
2306 if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
2307 return 1;
2310 return 0;
2313 /* Scan sequence after INSN if it does not dereference any argument slots
2314 we already clobbered by tail call arguments (as noted in stored_args_map
2315 bitmap). If MARK_STORED_ARGS_MAP, add stack slots for ARG to
2316 stored_args_map bitmap afterwards (when ARG is a register MARK_STORED_ARGS_MAP
2317 should be 0). Return nonzero if sequence after INSN dereferences such argument
2318 slots, zero otherwise. */
2320 static int
2321 check_sibcall_argument_overlap (rtx_insn *insn, struct arg_data *arg,
2322 int mark_stored_args_map)
2324 int low, high;
2326 if (insn == NULL_RTX)
2327 insn = get_insns ();
2328 else
2329 insn = NEXT_INSN (insn);
2331 for (; insn; insn = NEXT_INSN (insn))
2332 if (INSN_P (insn)
2333 && check_sibcall_argument_overlap_1 (PATTERN (insn)))
2334 break;
2336 if (mark_stored_args_map)
2338 if (ARGS_GROW_DOWNWARD)
2339 low = -arg->locate.slot_offset.constant - arg->locate.size.constant;
2340 else
2341 low = arg->locate.slot_offset.constant;
2343 for (high = low + arg->locate.size.constant; low < high; low++)
2344 bitmap_set_bit (stored_args_map, low);
2346 return insn != NULL_RTX;
2349 /* Given that a function returns a value of mode MODE at the most
2350 significant end of hard register VALUE, shift VALUE left or right
2351 as specified by LEFT_P. Return true if some action was needed. */
2353 bool
2354 shift_return_value (machine_mode mode, bool left_p, rtx value)
2356 HOST_WIDE_INT shift;
2358 gcc_assert (REG_P (value) && HARD_REGISTER_P (value));
2359 shift = GET_MODE_BITSIZE (GET_MODE (value)) - GET_MODE_BITSIZE (mode);
2360 if (shift == 0)
2361 return false;
2363 /* Use ashr rather than lshr for right shifts. This is for the benefit
2364 of the MIPS port, which requires SImode values to be sign-extended
2365 when stored in 64-bit registers. */
2366 if (!force_expand_binop (GET_MODE (value), left_p ? ashl_optab : ashr_optab,
2367 value, GEN_INT (shift), value, 1, OPTAB_WIDEN))
2368 gcc_unreachable ();
2369 return true;
2372 /* If X is a likely-spilled register value, copy it to a pseudo
2373 register and return that register. Return X otherwise. */
2375 static rtx
2376 avoid_likely_spilled_reg (rtx x)
2378 rtx new_rtx;
2380 if (REG_P (x)
2381 && HARD_REGISTER_P (x)
2382 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (x))))
2384 /* Make sure that we generate a REG rather than a CONCAT.
2385 Moves into CONCATs can need nontrivial instructions,
2386 and the whole point of this function is to avoid
2387 using the hard register directly in such a situation. */
2388 generating_concat_p = 0;
2389 new_rtx = gen_reg_rtx (GET_MODE (x));
2390 generating_concat_p = 1;
2391 emit_move_insn (new_rtx, x);
2392 return new_rtx;
2394 return x;
2397 /* Helper function for expand_call.
2398 Return false is EXP is not implementable as a sibling call. */
2400 static bool
2401 can_implement_as_sibling_call_p (tree exp,
2402 rtx structure_value_addr,
2403 tree funtype,
2404 int reg_parm_stack_space ATTRIBUTE_UNUSED,
2405 tree fndecl,
2406 int flags,
2407 tree addr,
2408 const args_size &args_size)
2410 if (!targetm.have_sibcall_epilogue ())
2412 maybe_complain_about_tail_call
2413 (exp,
2414 "machine description does not have"
2415 " a sibcall_epilogue instruction pattern");
2416 return false;
2419 /* Doing sibling call optimization needs some work, since
2420 structure_value_addr can be allocated on the stack.
2421 It does not seem worth the effort since few optimizable
2422 sibling calls will return a structure. */
2423 if (structure_value_addr != NULL_RTX)
2425 maybe_complain_about_tail_call (exp, "callee returns a structure");
2426 return false;
2429 #ifdef REG_PARM_STACK_SPACE
2430 /* If outgoing reg parm stack space changes, we can not do sibcall. */
2431 if (OUTGOING_REG_PARM_STACK_SPACE (funtype)
2432 != OUTGOING_REG_PARM_STACK_SPACE (TREE_TYPE (current_function_decl))
2433 || (reg_parm_stack_space != REG_PARM_STACK_SPACE (current_function_decl)))
2435 maybe_complain_about_tail_call (exp,
2436 "inconsistent size of stack space"
2437 " allocated for arguments which are"
2438 " passed in registers");
2439 return false;
2441 #endif
2443 /* Check whether the target is able to optimize the call
2444 into a sibcall. */
2445 if (!targetm.function_ok_for_sibcall (fndecl, exp))
2447 maybe_complain_about_tail_call (exp,
2448 "target is not able to optimize the"
2449 " call into a sibling call");
2450 return false;
2453 /* Functions that do not return exactly once may not be sibcall
2454 optimized. */
2455 if (flags & ECF_RETURNS_TWICE)
2457 maybe_complain_about_tail_call (exp, "callee returns twice");
2458 return false;
2460 if (flags & ECF_NORETURN)
2462 maybe_complain_about_tail_call (exp, "callee does not return");
2463 return false;
2466 if (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (addr))))
2468 maybe_complain_about_tail_call (exp, "volatile function type");
2469 return false;
2472 /* If the called function is nested in the current one, it might access
2473 some of the caller's arguments, but could clobber them beforehand if
2474 the argument areas are shared. */
2475 if (fndecl && decl_function_context (fndecl) == current_function_decl)
2477 maybe_complain_about_tail_call (exp, "nested function");
2478 return false;
2481 /* If this function requires more stack slots than the current
2482 function, we cannot change it into a sibling call.
2483 crtl->args.pretend_args_size is not part of the
2484 stack allocated by our caller. */
2485 if (args_size.constant > (crtl->args.size - crtl->args.pretend_args_size))
2487 maybe_complain_about_tail_call (exp,
2488 "callee required more stack slots"
2489 " than the caller");
2490 return false;
2493 /* If the callee pops its own arguments, then it must pop exactly
2494 the same number of arguments as the current function. */
2495 if (targetm.calls.return_pops_args (fndecl, funtype, args_size.constant)
2496 != targetm.calls.return_pops_args (current_function_decl,
2497 TREE_TYPE (current_function_decl),
2498 crtl->args.size))
2500 maybe_complain_about_tail_call (exp,
2501 "inconsistent number of"
2502 " popped arguments");
2503 return false;
2506 if (!lang_hooks.decls.ok_for_sibcall (fndecl))
2508 maybe_complain_about_tail_call (exp, "frontend does not support"
2509 " sibling call");
2510 return false;
2513 /* All checks passed. */
2514 return true;
2517 /* Generate all the code for a CALL_EXPR exp
2518 and return an rtx for its value.
2519 Store the value in TARGET (specified as an rtx) if convenient.
2520 If the value is stored in TARGET then TARGET is returned.
2521 If IGNORE is nonzero, then we ignore the value of the function call. */
2524 expand_call (tree exp, rtx target, int ignore)
2526 /* Nonzero if we are currently expanding a call. */
2527 static int currently_expanding_call = 0;
2529 /* RTX for the function to be called. */
2530 rtx funexp;
2531 /* Sequence of insns to perform a normal "call". */
2532 rtx_insn *normal_call_insns = NULL;
2533 /* Sequence of insns to perform a tail "call". */
2534 rtx_insn *tail_call_insns = NULL;
2535 /* Data type of the function. */
2536 tree funtype;
2537 tree type_arg_types;
2538 tree rettype;
2539 /* Declaration of the function being called,
2540 or 0 if the function is computed (not known by name). */
2541 tree fndecl = 0;
2542 /* The type of the function being called. */
2543 tree fntype;
2544 bool try_tail_call = CALL_EXPR_TAILCALL (exp);
2545 bool must_tail_call = CALL_EXPR_MUST_TAIL_CALL (exp);
2546 int pass;
2548 /* Register in which non-BLKmode value will be returned,
2549 or 0 if no value or if value is BLKmode. */
2550 rtx valreg;
2551 /* Register(s) in which bounds are returned. */
2552 rtx valbnd = NULL;
2553 /* Address where we should return a BLKmode value;
2554 0 if value not BLKmode. */
2555 rtx structure_value_addr = 0;
2556 /* Nonzero if that address is being passed by treating it as
2557 an extra, implicit first parameter. Otherwise,
2558 it is passed by being copied directly into struct_value_rtx. */
2559 int structure_value_addr_parm = 0;
2560 /* Holds the value of implicit argument for the struct value. */
2561 tree structure_value_addr_value = NULL_TREE;
2562 /* Size of aggregate value wanted, or zero if none wanted
2563 or if we are using the non-reentrant PCC calling convention
2564 or expecting the value in registers. */
2565 HOST_WIDE_INT struct_value_size = 0;
2566 /* Nonzero if called function returns an aggregate in memory PCC style,
2567 by returning the address of where to find it. */
2568 int pcc_struct_value = 0;
2569 rtx struct_value = 0;
2571 /* Number of actual parameters in this call, including struct value addr. */
2572 int num_actuals;
2573 /* Number of named args. Args after this are anonymous ones
2574 and they must all go on the stack. */
2575 int n_named_args;
2576 /* Number of complex actual arguments that need to be split. */
2577 int num_complex_actuals = 0;
2579 /* Vector of information about each argument.
2580 Arguments are numbered in the order they will be pushed,
2581 not the order they are written. */
2582 struct arg_data *args;
2584 /* Total size in bytes of all the stack-parms scanned so far. */
2585 struct args_size args_size;
2586 struct args_size adjusted_args_size;
2587 /* Size of arguments before any adjustments (such as rounding). */
2588 int unadjusted_args_size;
2589 /* Data on reg parms scanned so far. */
2590 CUMULATIVE_ARGS args_so_far_v;
2591 cumulative_args_t args_so_far;
2592 /* Nonzero if a reg parm has been scanned. */
2593 int reg_parm_seen;
2594 /* Nonzero if this is an indirect function call. */
2596 /* Nonzero if we must avoid push-insns in the args for this call.
2597 If stack space is allocated for register parameters, but not by the
2598 caller, then it is preallocated in the fixed part of the stack frame.
2599 So the entire argument block must then be preallocated (i.e., we
2600 ignore PUSH_ROUNDING in that case). */
2602 int must_preallocate = !PUSH_ARGS;
2604 /* Size of the stack reserved for parameter registers. */
2605 int reg_parm_stack_space = 0;
2607 /* Address of space preallocated for stack parms
2608 (on machines that lack push insns), or 0 if space not preallocated. */
2609 rtx argblock = 0;
2611 /* Mask of ECF_ and ERF_ flags. */
2612 int flags = 0;
2613 int return_flags = 0;
2614 #ifdef REG_PARM_STACK_SPACE
2615 /* Define the boundary of the register parm stack space that needs to be
2616 saved, if any. */
2617 int low_to_save, high_to_save;
2618 rtx save_area = 0; /* Place that it is saved */
2619 #endif
2621 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
2622 char *initial_stack_usage_map = stack_usage_map;
2623 char *stack_usage_map_buf = NULL;
2625 int old_stack_allocated;
2627 /* State variables to track stack modifications. */
2628 rtx old_stack_level = 0;
2629 int old_stack_arg_under_construction = 0;
2630 int old_pending_adj = 0;
2631 int old_inhibit_defer_pop = inhibit_defer_pop;
2633 /* Some stack pointer alterations we make are performed via
2634 allocate_dynamic_stack_space. This modifies the stack_pointer_delta,
2635 which we then also need to save/restore along the way. */
2636 int old_stack_pointer_delta = 0;
2638 rtx call_fusage;
2639 tree addr = CALL_EXPR_FN (exp);
2640 int i;
2641 /* The alignment of the stack, in bits. */
2642 unsigned HOST_WIDE_INT preferred_stack_boundary;
2643 /* The alignment of the stack, in bytes. */
2644 unsigned HOST_WIDE_INT preferred_unit_stack_boundary;
2645 /* The static chain value to use for this call. */
2646 rtx static_chain_value;
2647 /* See if this is "nothrow" function call. */
2648 if (TREE_NOTHROW (exp))
2649 flags |= ECF_NOTHROW;
2651 /* See if we can find a DECL-node for the actual function, and get the
2652 function attributes (flags) from the function decl or type node. */
2653 fndecl = get_callee_fndecl (exp);
2654 if (fndecl)
2656 fntype = TREE_TYPE (fndecl);
2657 flags |= flags_from_decl_or_type (fndecl);
2658 return_flags |= decl_return_flags (fndecl);
2660 else
2662 fntype = TREE_TYPE (TREE_TYPE (addr));
2663 flags |= flags_from_decl_or_type (fntype);
2665 rettype = TREE_TYPE (exp);
2667 struct_value = targetm.calls.struct_value_rtx (fntype, 0);
2669 /* Warn if this value is an aggregate type,
2670 regardless of which calling convention we are using for it. */
2671 if (AGGREGATE_TYPE_P (rettype))
2672 warning (OPT_Waggregate_return, "function call has aggregate value");
2674 /* If the result of a non looping pure or const function call is
2675 ignored (or void), and none of its arguments are volatile, we can
2676 avoid expanding the call and just evaluate the arguments for
2677 side-effects. */
2678 if ((flags & (ECF_CONST | ECF_PURE))
2679 && (!(flags & ECF_LOOPING_CONST_OR_PURE))
2680 && (ignore || target == const0_rtx
2681 || TYPE_MODE (rettype) == VOIDmode))
2683 bool volatilep = false;
2684 tree arg;
2685 call_expr_arg_iterator iter;
2687 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2688 if (TREE_THIS_VOLATILE (arg))
2690 volatilep = true;
2691 break;
2694 if (! volatilep)
2696 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2697 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
2698 return const0_rtx;
2702 #ifdef REG_PARM_STACK_SPACE
2703 reg_parm_stack_space = REG_PARM_STACK_SPACE (!fndecl ? fntype : fndecl);
2704 #endif
2706 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
2707 && reg_parm_stack_space > 0 && PUSH_ARGS)
2708 must_preallocate = 1;
2710 /* Set up a place to return a structure. */
2712 /* Cater to broken compilers. */
2713 if (aggregate_value_p (exp, fntype))
2715 /* This call returns a big structure. */
2716 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
2718 #ifdef PCC_STATIC_STRUCT_RETURN
2720 pcc_struct_value = 1;
2722 #else /* not PCC_STATIC_STRUCT_RETURN */
2724 struct_value_size = int_size_in_bytes (rettype);
2726 /* Even if it is semantically safe to use the target as the return
2727 slot, it may be not sufficiently aligned for the return type. */
2728 if (CALL_EXPR_RETURN_SLOT_OPT (exp)
2729 && target
2730 && MEM_P (target)
2731 && !(MEM_ALIGN (target) < TYPE_ALIGN (rettype)
2732 && SLOW_UNALIGNED_ACCESS (TYPE_MODE (rettype),
2733 MEM_ALIGN (target))))
2734 structure_value_addr = XEXP (target, 0);
2735 else
2737 /* For variable-sized objects, we must be called with a target
2738 specified. If we were to allocate space on the stack here,
2739 we would have no way of knowing when to free it. */
2740 rtx d = assign_temp (rettype, 1, 1);
2741 structure_value_addr = XEXP (d, 0);
2742 target = 0;
2745 #endif /* not PCC_STATIC_STRUCT_RETURN */
2748 /* Figure out the amount to which the stack should be aligned. */
2749 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
2750 if (fndecl)
2752 struct cgraph_rtl_info *i = cgraph_node::rtl_info (fndecl);
2753 /* Without automatic stack alignment, we can't increase preferred
2754 stack boundary. With automatic stack alignment, it is
2755 unnecessary since unless we can guarantee that all callers will
2756 align the outgoing stack properly, callee has to align its
2757 stack anyway. */
2758 if (i
2759 && i->preferred_incoming_stack_boundary
2760 && i->preferred_incoming_stack_boundary < preferred_stack_boundary)
2761 preferred_stack_boundary = i->preferred_incoming_stack_boundary;
2764 /* Operand 0 is a pointer-to-function; get the type of the function. */
2765 funtype = TREE_TYPE (addr);
2766 gcc_assert (POINTER_TYPE_P (funtype));
2767 funtype = TREE_TYPE (funtype);
2769 /* Count whether there are actual complex arguments that need to be split
2770 into their real and imaginary parts. Munge the type_arg_types
2771 appropriately here as well. */
2772 if (targetm.calls.split_complex_arg)
2774 call_expr_arg_iterator iter;
2775 tree arg;
2776 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2778 tree type = TREE_TYPE (arg);
2779 if (type && TREE_CODE (type) == COMPLEX_TYPE
2780 && targetm.calls.split_complex_arg (type))
2781 num_complex_actuals++;
2783 type_arg_types = split_complex_types (TYPE_ARG_TYPES (funtype));
2785 else
2786 type_arg_types = TYPE_ARG_TYPES (funtype);
2788 if (flags & ECF_MAY_BE_ALLOCA)
2789 cfun->calls_alloca = 1;
2791 /* If struct_value_rtx is 0, it means pass the address
2792 as if it were an extra parameter. Put the argument expression
2793 in structure_value_addr_value. */
2794 if (structure_value_addr && struct_value == 0)
2796 /* If structure_value_addr is a REG other than
2797 virtual_outgoing_args_rtx, we can use always use it. If it
2798 is not a REG, we must always copy it into a register.
2799 If it is virtual_outgoing_args_rtx, we must copy it to another
2800 register in some cases. */
2801 rtx temp = (!REG_P (structure_value_addr)
2802 || (ACCUMULATE_OUTGOING_ARGS
2803 && stack_arg_under_construction
2804 && structure_value_addr == virtual_outgoing_args_rtx)
2805 ? copy_addr_to_reg (convert_memory_address
2806 (Pmode, structure_value_addr))
2807 : structure_value_addr);
2809 structure_value_addr_value =
2810 make_tree (build_pointer_type (TREE_TYPE (funtype)), temp);
2811 structure_value_addr_parm = CALL_WITH_BOUNDS_P (exp) ? 2 : 1;
2814 /* Count the arguments and set NUM_ACTUALS. */
2815 num_actuals =
2816 call_expr_nargs (exp) + num_complex_actuals + structure_value_addr_parm;
2818 /* Compute number of named args.
2819 First, do a raw count of the args for INIT_CUMULATIVE_ARGS. */
2821 if (type_arg_types != 0)
2822 n_named_args
2823 = (list_length (type_arg_types)
2824 /* Count the struct value address, if it is passed as a parm. */
2825 + structure_value_addr_parm);
2826 else
2827 /* If we know nothing, treat all args as named. */
2828 n_named_args = num_actuals;
2830 /* Start updating where the next arg would go.
2832 On some machines (such as the PA) indirect calls have a different
2833 calling convention than normal calls. The fourth argument in
2834 INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
2835 or not. */
2836 INIT_CUMULATIVE_ARGS (args_so_far_v, funtype, NULL_RTX, fndecl, n_named_args);
2837 args_so_far = pack_cumulative_args (&args_so_far_v);
2839 /* Now possibly adjust the number of named args.
2840 Normally, don't include the last named arg if anonymous args follow.
2841 We do include the last named arg if
2842 targetm.calls.strict_argument_naming() returns nonzero.
2843 (If no anonymous args follow, the result of list_length is actually
2844 one too large. This is harmless.)
2846 If targetm.calls.pretend_outgoing_varargs_named() returns
2847 nonzero, and targetm.calls.strict_argument_naming() returns zero,
2848 this machine will be able to place unnamed args that were passed
2849 in registers into the stack. So treat all args as named. This
2850 allows the insns emitting for a specific argument list to be
2851 independent of the function declaration.
2853 If targetm.calls.pretend_outgoing_varargs_named() returns zero,
2854 we do not have any reliable way to pass unnamed args in
2855 registers, so we must force them into memory. */
2857 if (type_arg_types != 0
2858 && targetm.calls.strict_argument_naming (args_so_far))
2860 else if (type_arg_types != 0
2861 && ! targetm.calls.pretend_outgoing_varargs_named (args_so_far))
2862 /* Don't include the last named arg. */
2863 --n_named_args;
2864 else
2865 /* Treat all args as named. */
2866 n_named_args = num_actuals;
2868 /* Make a vector to hold all the information about each arg. */
2869 args = XALLOCAVEC (struct arg_data, num_actuals);
2870 memset (args, 0, num_actuals * sizeof (struct arg_data));
2872 /* Build up entries in the ARGS array, compute the size of the
2873 arguments into ARGS_SIZE, etc. */
2874 initialize_argument_information (num_actuals, args, &args_size,
2875 n_named_args, exp,
2876 structure_value_addr_value, fndecl, fntype,
2877 args_so_far, reg_parm_stack_space,
2878 &old_stack_level, &old_pending_adj,
2879 &must_preallocate, &flags,
2880 &try_tail_call, CALL_FROM_THUNK_P (exp));
2882 if (args_size.var)
2883 must_preallocate = 1;
2885 /* Now make final decision about preallocating stack space. */
2886 must_preallocate = finalize_must_preallocate (must_preallocate,
2887 num_actuals, args,
2888 &args_size);
2890 /* If the structure value address will reference the stack pointer, we
2891 must stabilize it. We don't need to do this if we know that we are
2892 not going to adjust the stack pointer in processing this call. */
2894 if (structure_value_addr
2895 && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
2896 || reg_mentioned_p (virtual_outgoing_args_rtx,
2897 structure_value_addr))
2898 && (args_size.var
2899 || (!ACCUMULATE_OUTGOING_ARGS && args_size.constant)))
2900 structure_value_addr = copy_to_reg (structure_value_addr);
2902 /* Tail calls can make things harder to debug, and we've traditionally
2903 pushed these optimizations into -O2. Don't try if we're already
2904 expanding a call, as that means we're an argument. Don't try if
2905 there's cleanups, as we know there's code to follow the call. */
2907 if (currently_expanding_call++ != 0
2908 || !flag_optimize_sibling_calls
2909 || args_size.var
2910 || dbg_cnt (tail_call) == false)
2911 try_tail_call = 0;
2913 /* If the user has marked the function as requiring tail-call
2914 optimization, attempt it. */
2915 if (must_tail_call)
2916 try_tail_call = 1;
2918 /* Rest of purposes for tail call optimizations to fail. */
2919 if (try_tail_call)
2920 try_tail_call = can_implement_as_sibling_call_p (exp,
2921 structure_value_addr,
2922 funtype,
2923 reg_parm_stack_space,
2924 fndecl,
2925 flags, addr, args_size);
2927 /* Check if caller and callee disagree in promotion of function
2928 return value. */
2929 if (try_tail_call)
2931 machine_mode caller_mode, caller_promoted_mode;
2932 machine_mode callee_mode, callee_promoted_mode;
2933 int caller_unsignedp, callee_unsignedp;
2934 tree caller_res = DECL_RESULT (current_function_decl);
2936 caller_unsignedp = TYPE_UNSIGNED (TREE_TYPE (caller_res));
2937 caller_mode = DECL_MODE (caller_res);
2938 callee_unsignedp = TYPE_UNSIGNED (TREE_TYPE (funtype));
2939 callee_mode = TYPE_MODE (TREE_TYPE (funtype));
2940 caller_promoted_mode
2941 = promote_function_mode (TREE_TYPE (caller_res), caller_mode,
2942 &caller_unsignedp,
2943 TREE_TYPE (current_function_decl), 1);
2944 callee_promoted_mode
2945 = promote_function_mode (TREE_TYPE (funtype), callee_mode,
2946 &callee_unsignedp,
2947 funtype, 1);
2948 if (caller_mode != VOIDmode
2949 && (caller_promoted_mode != callee_promoted_mode
2950 || ((caller_mode != caller_promoted_mode
2951 || callee_mode != callee_promoted_mode)
2952 && (caller_unsignedp != callee_unsignedp
2953 || GET_MODE_BITSIZE (caller_mode)
2954 < GET_MODE_BITSIZE (callee_mode)))))
2956 try_tail_call = 0;
2957 maybe_complain_about_tail_call (exp,
2958 "caller and callee disagree in"
2959 " promotion of function"
2960 " return value");
2964 /* Ensure current function's preferred stack boundary is at least
2965 what we need. Stack alignment may also increase preferred stack
2966 boundary. */
2967 if (crtl->preferred_stack_boundary < preferred_stack_boundary)
2968 crtl->preferred_stack_boundary = preferred_stack_boundary;
2969 else
2970 preferred_stack_boundary = crtl->preferred_stack_boundary;
2972 preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
2974 /* We want to make two insn chains; one for a sibling call, the other
2975 for a normal call. We will select one of the two chains after
2976 initial RTL generation is complete. */
2977 for (pass = try_tail_call ? 0 : 1; pass < 2; pass++)
2979 int sibcall_failure = 0;
2980 /* We want to emit any pending stack adjustments before the tail
2981 recursion "call". That way we know any adjustment after the tail
2982 recursion call can be ignored if we indeed use the tail
2983 call expansion. */
2984 saved_pending_stack_adjust save;
2985 rtx_insn *insns, *before_call, *after_args;
2986 rtx next_arg_reg;
2988 if (pass == 0)
2990 /* State variables we need to save and restore between
2991 iterations. */
2992 save_pending_stack_adjust (&save);
2994 if (pass)
2995 flags &= ~ECF_SIBCALL;
2996 else
2997 flags |= ECF_SIBCALL;
2999 /* Other state variables that we must reinitialize each time
3000 through the loop (that are not initialized by the loop itself). */
3001 argblock = 0;
3002 call_fusage = 0;
3004 /* Start a new sequence for the normal call case.
3006 From this point on, if the sibling call fails, we want to set
3007 sibcall_failure instead of continuing the loop. */
3008 start_sequence ();
3010 /* Don't let pending stack adjusts add up to too much.
3011 Also, do all pending adjustments now if there is any chance
3012 this might be a call to alloca or if we are expanding a sibling
3013 call sequence.
3014 Also do the adjustments before a throwing call, otherwise
3015 exception handling can fail; PR 19225. */
3016 if (pending_stack_adjust >= 32
3017 || (pending_stack_adjust > 0
3018 && (flags & ECF_MAY_BE_ALLOCA))
3019 || (pending_stack_adjust > 0
3020 && flag_exceptions && !(flags & ECF_NOTHROW))
3021 || pass == 0)
3022 do_pending_stack_adjust ();
3024 /* Precompute any arguments as needed. */
3025 if (pass)
3026 precompute_arguments (num_actuals, args);
3028 /* Now we are about to start emitting insns that can be deleted
3029 if a libcall is deleted. */
3030 if (pass && (flags & ECF_MALLOC))
3031 start_sequence ();
3033 if (pass == 0 && crtl->stack_protect_guard)
3034 stack_protect_epilogue ();
3036 adjusted_args_size = args_size;
3037 /* Compute the actual size of the argument block required. The variable
3038 and constant sizes must be combined, the size may have to be rounded,
3039 and there may be a minimum required size. When generating a sibcall
3040 pattern, do not round up, since we'll be re-using whatever space our
3041 caller provided. */
3042 unadjusted_args_size
3043 = compute_argument_block_size (reg_parm_stack_space,
3044 &adjusted_args_size,
3045 fndecl, fntype,
3046 (pass == 0 ? 0
3047 : preferred_stack_boundary));
3049 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
3051 /* The argument block when performing a sibling call is the
3052 incoming argument block. */
3053 if (pass == 0)
3055 argblock = crtl->args.internal_arg_pointer;
3056 if (STACK_GROWS_DOWNWARD)
3057 argblock
3058 = plus_constant (Pmode, argblock, crtl->args.pretend_args_size);
3059 else
3060 argblock
3061 = plus_constant (Pmode, argblock, -crtl->args.pretend_args_size);
3063 stored_args_map = sbitmap_alloc (args_size.constant);
3064 bitmap_clear (stored_args_map);
3067 /* If we have no actual push instructions, or shouldn't use them,
3068 make space for all args right now. */
3069 else if (adjusted_args_size.var != 0)
3071 if (old_stack_level == 0)
3073 emit_stack_save (SAVE_BLOCK, &old_stack_level);
3074 old_stack_pointer_delta = stack_pointer_delta;
3075 old_pending_adj = pending_stack_adjust;
3076 pending_stack_adjust = 0;
3077 /* stack_arg_under_construction says whether a stack arg is
3078 being constructed at the old stack level. Pushing the stack
3079 gets a clean outgoing argument block. */
3080 old_stack_arg_under_construction = stack_arg_under_construction;
3081 stack_arg_under_construction = 0;
3083 argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
3084 if (flag_stack_usage_info)
3085 current_function_has_unbounded_dynamic_stack_size = 1;
3087 else
3089 /* Note that we must go through the motions of allocating an argument
3090 block even if the size is zero because we may be storing args
3091 in the area reserved for register arguments, which may be part of
3092 the stack frame. */
3094 int needed = adjusted_args_size.constant;
3096 /* Store the maximum argument space used. It will be pushed by
3097 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
3098 checking). */
3100 if (needed > crtl->outgoing_args_size)
3101 crtl->outgoing_args_size = needed;
3103 if (must_preallocate)
3105 if (ACCUMULATE_OUTGOING_ARGS)
3107 /* Since the stack pointer will never be pushed, it is
3108 possible for the evaluation of a parm to clobber
3109 something we have already written to the stack.
3110 Since most function calls on RISC machines do not use
3111 the stack, this is uncommon, but must work correctly.
3113 Therefore, we save any area of the stack that was already
3114 written and that we are using. Here we set up to do this
3115 by making a new stack usage map from the old one. The
3116 actual save will be done by store_one_arg.
3118 Another approach might be to try to reorder the argument
3119 evaluations to avoid this conflicting stack usage. */
3121 /* Since we will be writing into the entire argument area,
3122 the map must be allocated for its entire size, not just
3123 the part that is the responsibility of the caller. */
3124 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
3125 needed += reg_parm_stack_space;
3127 if (ARGS_GROW_DOWNWARD)
3128 highest_outgoing_arg_in_use
3129 = MAX (initial_highest_arg_in_use, needed + 1);
3130 else
3131 highest_outgoing_arg_in_use
3132 = MAX (initial_highest_arg_in_use, needed);
3134 free (stack_usage_map_buf);
3135 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
3136 stack_usage_map = stack_usage_map_buf;
3138 if (initial_highest_arg_in_use)
3139 memcpy (stack_usage_map, initial_stack_usage_map,
3140 initial_highest_arg_in_use);
3142 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
3143 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
3144 (highest_outgoing_arg_in_use
3145 - initial_highest_arg_in_use));
3146 needed = 0;
3148 /* The address of the outgoing argument list must not be
3149 copied to a register here, because argblock would be left
3150 pointing to the wrong place after the call to
3151 allocate_dynamic_stack_space below. */
3153 argblock = virtual_outgoing_args_rtx;
3155 else
3157 if (inhibit_defer_pop == 0)
3159 /* Try to reuse some or all of the pending_stack_adjust
3160 to get this space. */
3161 needed
3162 = (combine_pending_stack_adjustment_and_call
3163 (unadjusted_args_size,
3164 &adjusted_args_size,
3165 preferred_unit_stack_boundary));
3167 /* combine_pending_stack_adjustment_and_call computes
3168 an adjustment before the arguments are allocated.
3169 Account for them and see whether or not the stack
3170 needs to go up or down. */
3171 needed = unadjusted_args_size - needed;
3173 if (needed < 0)
3175 /* We're releasing stack space. */
3176 /* ??? We can avoid any adjustment at all if we're
3177 already aligned. FIXME. */
3178 pending_stack_adjust = -needed;
3179 do_pending_stack_adjust ();
3180 needed = 0;
3182 else
3183 /* We need to allocate space. We'll do that in
3184 push_block below. */
3185 pending_stack_adjust = 0;
3188 /* Special case this because overhead of `push_block' in
3189 this case is non-trivial. */
3190 if (needed == 0)
3191 argblock = virtual_outgoing_args_rtx;
3192 else
3194 argblock = push_block (GEN_INT (needed), 0, 0);
3195 if (ARGS_GROW_DOWNWARD)
3196 argblock = plus_constant (Pmode, argblock, needed);
3199 /* We only really need to call `copy_to_reg' in the case
3200 where push insns are going to be used to pass ARGBLOCK
3201 to a function call in ARGS. In that case, the stack
3202 pointer changes value from the allocation point to the
3203 call point, and hence the value of
3204 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
3205 as well always do it. */
3206 argblock = copy_to_reg (argblock);
3211 if (ACCUMULATE_OUTGOING_ARGS)
3213 /* The save/restore code in store_one_arg handles all
3214 cases except one: a constructor call (including a C
3215 function returning a BLKmode struct) to initialize
3216 an argument. */
3217 if (stack_arg_under_construction)
3219 rtx push_size
3220 = GEN_INT (adjusted_args_size.constant
3221 + (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype
3222 : TREE_TYPE (fndecl))) ? 0
3223 : reg_parm_stack_space));
3224 if (old_stack_level == 0)
3226 emit_stack_save (SAVE_BLOCK, &old_stack_level);
3227 old_stack_pointer_delta = stack_pointer_delta;
3228 old_pending_adj = pending_stack_adjust;
3229 pending_stack_adjust = 0;
3230 /* stack_arg_under_construction says whether a stack
3231 arg is being constructed at the old stack level.
3232 Pushing the stack gets a clean outgoing argument
3233 block. */
3234 old_stack_arg_under_construction
3235 = stack_arg_under_construction;
3236 stack_arg_under_construction = 0;
3237 /* Make a new map for the new argument list. */
3238 free (stack_usage_map_buf);
3239 stack_usage_map_buf = XCNEWVEC (char, highest_outgoing_arg_in_use);
3240 stack_usage_map = stack_usage_map_buf;
3241 highest_outgoing_arg_in_use = 0;
3243 /* We can pass TRUE as the 4th argument because we just
3244 saved the stack pointer and will restore it right after
3245 the call. */
3246 allocate_dynamic_stack_space (push_size, 0,
3247 BIGGEST_ALIGNMENT, true);
3250 /* If argument evaluation might modify the stack pointer,
3251 copy the address of the argument list to a register. */
3252 for (i = 0; i < num_actuals; i++)
3253 if (args[i].pass_on_stack)
3255 argblock = copy_addr_to_reg (argblock);
3256 break;
3260 compute_argument_addresses (args, argblock, num_actuals);
3262 /* Stack is properly aligned, pops can't safely be deferred during
3263 the evaluation of the arguments. */
3264 NO_DEFER_POP;
3266 /* Precompute all register parameters. It isn't safe to compute
3267 anything once we have started filling any specific hard regs.
3268 TLS symbols sometimes need a call to resolve. Precompute
3269 register parameters before any stack pointer manipulation
3270 to avoid unaligned stack in the called function. */
3271 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
3273 OK_DEFER_POP;
3275 /* Perform stack alignment before the first push (the last arg). */
3276 if (argblock == 0
3277 && adjusted_args_size.constant > reg_parm_stack_space
3278 && adjusted_args_size.constant != unadjusted_args_size)
3280 /* When the stack adjustment is pending, we get better code
3281 by combining the adjustments. */
3282 if (pending_stack_adjust
3283 && ! inhibit_defer_pop)
3285 pending_stack_adjust
3286 = (combine_pending_stack_adjustment_and_call
3287 (unadjusted_args_size,
3288 &adjusted_args_size,
3289 preferred_unit_stack_boundary));
3290 do_pending_stack_adjust ();
3292 else if (argblock == 0)
3293 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
3294 - unadjusted_args_size));
3296 /* Now that the stack is properly aligned, pops can't safely
3297 be deferred during the evaluation of the arguments. */
3298 NO_DEFER_POP;
3300 /* Record the maximum pushed stack space size. We need to delay
3301 doing it this far to take into account the optimization done
3302 by combine_pending_stack_adjustment_and_call. */
3303 if (flag_stack_usage_info
3304 && !ACCUMULATE_OUTGOING_ARGS
3305 && pass
3306 && adjusted_args_size.var == 0)
3308 int pushed = adjusted_args_size.constant + pending_stack_adjust;
3309 if (pushed > current_function_pushed_stack_size)
3310 current_function_pushed_stack_size = pushed;
3313 funexp = rtx_for_function_call (fndecl, addr);
3315 if (CALL_EXPR_STATIC_CHAIN (exp))
3316 static_chain_value = expand_normal (CALL_EXPR_STATIC_CHAIN (exp));
3317 else
3318 static_chain_value = 0;
3320 #ifdef REG_PARM_STACK_SPACE
3321 /* Save the fixed argument area if it's part of the caller's frame and
3322 is clobbered by argument setup for this call. */
3323 if (ACCUMULATE_OUTGOING_ARGS && pass)
3324 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3325 &low_to_save, &high_to_save);
3326 #endif
3328 /* Now store (and compute if necessary) all non-register parms.
3329 These come before register parms, since they can require block-moves,
3330 which could clobber the registers used for register parms.
3331 Parms which have partial registers are not stored here,
3332 but we do preallocate space here if they want that. */
3334 for (i = 0; i < num_actuals; i++)
3336 /* Delay bounds until all other args are stored. */
3337 if (POINTER_BOUNDS_P (args[i].tree_value))
3338 continue;
3339 else if (args[i].reg == 0 || args[i].pass_on_stack)
3341 rtx_insn *before_arg = get_last_insn ();
3343 /* We don't allow passing huge (> 2^30 B) arguments
3344 by value. It would cause an overflow later on. */
3345 if (adjusted_args_size.constant
3346 >= (1 << (HOST_BITS_PER_INT - 2)))
3348 sorry ("passing too large argument on stack");
3349 continue;
3352 if (store_one_arg (&args[i], argblock, flags,
3353 adjusted_args_size.var != 0,
3354 reg_parm_stack_space)
3355 || (pass == 0
3356 && check_sibcall_argument_overlap (before_arg,
3357 &args[i], 1)))
3358 sibcall_failure = 1;
3361 if (args[i].stack)
3362 call_fusage
3363 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[i].tree_value)),
3364 gen_rtx_USE (VOIDmode, args[i].stack),
3365 call_fusage);
3368 /* If we have a parm that is passed in registers but not in memory
3369 and whose alignment does not permit a direct copy into registers,
3370 make a group of pseudos that correspond to each register that we
3371 will later fill. */
3372 if (STRICT_ALIGNMENT)
3373 store_unaligned_arguments_into_pseudos (args, num_actuals);
3375 /* Now store any partially-in-registers parm.
3376 This is the last place a block-move can happen. */
3377 if (reg_parm_seen)
3378 for (i = 0; i < num_actuals; i++)
3379 if (args[i].partial != 0 && ! args[i].pass_on_stack)
3381 rtx_insn *before_arg = get_last_insn ();
3383 /* On targets with weird calling conventions (e.g. PA) it's
3384 hard to ensure that all cases of argument overlap between
3385 stack and registers work. Play it safe and bail out. */
3386 if (ARGS_GROW_DOWNWARD && !STACK_GROWS_DOWNWARD)
3388 sibcall_failure = 1;
3389 break;
3392 if (store_one_arg (&args[i], argblock, flags,
3393 adjusted_args_size.var != 0,
3394 reg_parm_stack_space)
3395 || (pass == 0
3396 && check_sibcall_argument_overlap (before_arg,
3397 &args[i], 1)))
3398 sibcall_failure = 1;
3401 bool any_regs = false;
3402 for (i = 0; i < num_actuals; i++)
3403 if (args[i].reg != NULL_RTX)
3405 any_regs = true;
3406 targetm.calls.call_args (args[i].reg, funtype);
3408 if (!any_regs)
3409 targetm.calls.call_args (pc_rtx, funtype);
3411 /* Figure out the register where the value, if any, will come back. */
3412 valreg = 0;
3413 valbnd = 0;
3414 if (TYPE_MODE (rettype) != VOIDmode
3415 && ! structure_value_addr)
3417 if (pcc_struct_value)
3419 valreg = hard_function_value (build_pointer_type (rettype),
3420 fndecl, NULL, (pass == 0));
3421 if (CALL_WITH_BOUNDS_P (exp))
3422 valbnd = targetm.calls.
3423 chkp_function_value_bounds (build_pointer_type (rettype),
3424 fndecl, (pass == 0));
3426 else
3428 valreg = hard_function_value (rettype, fndecl, fntype,
3429 (pass == 0));
3430 if (CALL_WITH_BOUNDS_P (exp))
3431 valbnd = targetm.calls.chkp_function_value_bounds (rettype,
3432 fndecl,
3433 (pass == 0));
3436 /* If VALREG is a PARALLEL whose first member has a zero
3437 offset, use that. This is for targets such as m68k that
3438 return the same value in multiple places. */
3439 if (GET_CODE (valreg) == PARALLEL)
3441 rtx elem = XVECEXP (valreg, 0, 0);
3442 rtx where = XEXP (elem, 0);
3443 rtx offset = XEXP (elem, 1);
3444 if (offset == const0_rtx
3445 && GET_MODE (where) == GET_MODE (valreg))
3446 valreg = where;
3450 /* Store all bounds not passed in registers. */
3451 for (i = 0; i < num_actuals; i++)
3453 if (POINTER_BOUNDS_P (args[i].tree_value)
3454 && !args[i].reg)
3455 store_bounds (&args[i],
3456 args[i].pointer_arg == -1
3457 ? NULL
3458 : &args[args[i].pointer_arg]);
3461 /* If register arguments require space on the stack and stack space
3462 was not preallocated, allocate stack space here for arguments
3463 passed in registers. */
3464 if (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
3465 && !ACCUMULATE_OUTGOING_ARGS
3466 && must_preallocate == 0 && reg_parm_stack_space > 0)
3467 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
3469 /* Pass the function the address in which to return a
3470 structure value. */
3471 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
3473 structure_value_addr
3474 = convert_memory_address (Pmode, structure_value_addr);
3475 emit_move_insn (struct_value,
3476 force_reg (Pmode,
3477 force_operand (structure_value_addr,
3478 NULL_RTX)));
3480 if (REG_P (struct_value))
3481 use_reg (&call_fusage, struct_value);
3484 after_args = get_last_insn ();
3485 funexp = prepare_call_address (fndecl ? fndecl : fntype, funexp,
3486 static_chain_value, &call_fusage,
3487 reg_parm_seen, pass == 0);
3489 load_register_parameters (args, num_actuals, &call_fusage, flags,
3490 pass == 0, &sibcall_failure);
3492 /* Save a pointer to the last insn before the call, so that we can
3493 later safely search backwards to find the CALL_INSN. */
3494 before_call = get_last_insn ();
3496 /* Set up next argument register. For sibling calls on machines
3497 with register windows this should be the incoming register. */
3498 if (pass == 0)
3499 next_arg_reg = targetm.calls.function_incoming_arg (args_so_far,
3500 VOIDmode,
3501 void_type_node,
3502 true);
3503 else
3504 next_arg_reg = targetm.calls.function_arg (args_so_far,
3505 VOIDmode, void_type_node,
3506 true);
3508 if (pass == 1 && (return_flags & ERF_RETURNS_ARG))
3510 int arg_nr = return_flags & ERF_RETURN_ARG_MASK;
3511 arg_nr = num_actuals - arg_nr - 1;
3512 if (arg_nr >= 0
3513 && arg_nr < num_actuals
3514 && args[arg_nr].reg
3515 && valreg
3516 && REG_P (valreg)
3517 && GET_MODE (args[arg_nr].reg) == GET_MODE (valreg))
3518 call_fusage
3519 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[arg_nr].tree_value)),
3520 gen_rtx_SET (valreg, args[arg_nr].reg),
3521 call_fusage);
3523 /* All arguments and registers used for the call must be set up by
3524 now! */
3526 /* Stack must be properly aligned now. */
3527 gcc_assert (!pass
3528 || !(stack_pointer_delta % preferred_unit_stack_boundary));
3530 /* Generate the actual call instruction. */
3531 emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
3532 adjusted_args_size.constant, struct_value_size,
3533 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
3534 flags, args_so_far);
3536 if (flag_ipa_ra)
3538 rtx_call_insn *last;
3539 rtx datum = NULL_RTX;
3540 if (fndecl != NULL_TREE)
3542 datum = XEXP (DECL_RTL (fndecl), 0);
3543 gcc_assert (datum != NULL_RTX
3544 && GET_CODE (datum) == SYMBOL_REF);
3546 last = last_call_insn ();
3547 add_reg_note (last, REG_CALL_DECL, datum);
3550 /* If the call setup or the call itself overlaps with anything
3551 of the argument setup we probably clobbered our call address.
3552 In that case we can't do sibcalls. */
3553 if (pass == 0
3554 && check_sibcall_argument_overlap (after_args, 0, 0))
3555 sibcall_failure = 1;
3557 /* If a non-BLKmode value is returned at the most significant end
3558 of a register, shift the register right by the appropriate amount
3559 and update VALREG accordingly. BLKmode values are handled by the
3560 group load/store machinery below. */
3561 if (!structure_value_addr
3562 && !pcc_struct_value
3563 && TYPE_MODE (rettype) != VOIDmode
3564 && TYPE_MODE (rettype) != BLKmode
3565 && REG_P (valreg)
3566 && targetm.calls.return_in_msb (rettype))
3568 if (shift_return_value (TYPE_MODE (rettype), false, valreg))
3569 sibcall_failure = 1;
3570 valreg = gen_rtx_REG (TYPE_MODE (rettype), REGNO (valreg));
3573 if (pass && (flags & ECF_MALLOC))
3575 rtx temp = gen_reg_rtx (GET_MODE (valreg));
3576 rtx_insn *last, *insns;
3578 /* The return value from a malloc-like function is a pointer. */
3579 if (TREE_CODE (rettype) == POINTER_TYPE)
3580 mark_reg_pointer (temp, MALLOC_ABI_ALIGNMENT);
3582 emit_move_insn (temp, valreg);
3584 /* The return value from a malloc-like function can not alias
3585 anything else. */
3586 last = get_last_insn ();
3587 add_reg_note (last, REG_NOALIAS, temp);
3589 /* Write out the sequence. */
3590 insns = get_insns ();
3591 end_sequence ();
3592 emit_insn (insns);
3593 valreg = temp;
3596 /* For calls to `setjmp', etc., inform
3597 function.c:setjmp_warnings that it should complain if
3598 nonvolatile values are live. For functions that cannot
3599 return, inform flow that control does not fall through. */
3601 if ((flags & ECF_NORETURN) || pass == 0)
3603 /* The barrier must be emitted
3604 immediately after the CALL_INSN. Some ports emit more
3605 than just a CALL_INSN above, so we must search for it here. */
3607 rtx_insn *last = get_last_insn ();
3608 while (!CALL_P (last))
3610 last = PREV_INSN (last);
3611 /* There was no CALL_INSN? */
3612 gcc_assert (last != before_call);
3615 emit_barrier_after (last);
3617 /* Stack adjustments after a noreturn call are dead code.
3618 However when NO_DEFER_POP is in effect, we must preserve
3619 stack_pointer_delta. */
3620 if (inhibit_defer_pop == 0)
3622 stack_pointer_delta = old_stack_allocated;
3623 pending_stack_adjust = 0;
3627 /* If value type not void, return an rtx for the value. */
3629 if (TYPE_MODE (rettype) == VOIDmode
3630 || ignore)
3631 target = const0_rtx;
3632 else if (structure_value_addr)
3634 if (target == 0 || !MEM_P (target))
3636 target
3637 = gen_rtx_MEM (TYPE_MODE (rettype),
3638 memory_address (TYPE_MODE (rettype),
3639 structure_value_addr));
3640 set_mem_attributes (target, rettype, 1);
3643 else if (pcc_struct_value)
3645 /* This is the special C++ case where we need to
3646 know what the true target was. We take care to
3647 never use this value more than once in one expression. */
3648 target = gen_rtx_MEM (TYPE_MODE (rettype),
3649 copy_to_reg (valreg));
3650 set_mem_attributes (target, rettype, 1);
3652 /* Handle calls that return values in multiple non-contiguous locations.
3653 The Irix 6 ABI has examples of this. */
3654 else if (GET_CODE (valreg) == PARALLEL)
3656 if (target == 0)
3657 target = emit_group_move_into_temps (valreg);
3658 else if (rtx_equal_p (target, valreg))
3660 else if (GET_CODE (target) == PARALLEL)
3661 /* Handle the result of a emit_group_move_into_temps
3662 call in the previous pass. */
3663 emit_group_move (target, valreg);
3664 else
3665 emit_group_store (target, valreg, rettype,
3666 int_size_in_bytes (rettype));
3668 else if (target
3669 && GET_MODE (target) == TYPE_MODE (rettype)
3670 && GET_MODE (target) == GET_MODE (valreg))
3672 bool may_overlap = false;
3674 /* We have to copy a return value in a CLASS_LIKELY_SPILLED hard
3675 reg to a plain register. */
3676 if (!REG_P (target) || HARD_REGISTER_P (target))
3677 valreg = avoid_likely_spilled_reg (valreg);
3679 /* If TARGET is a MEM in the argument area, and we have
3680 saved part of the argument area, then we can't store
3681 directly into TARGET as it may get overwritten when we
3682 restore the argument save area below. Don't work too
3683 hard though and simply force TARGET to a register if it
3684 is a MEM; the optimizer is quite likely to sort it out. */
3685 if (ACCUMULATE_OUTGOING_ARGS && pass && MEM_P (target))
3686 for (i = 0; i < num_actuals; i++)
3687 if (args[i].save_area)
3689 may_overlap = true;
3690 break;
3693 if (may_overlap)
3694 target = copy_to_reg (valreg);
3695 else
3697 /* TARGET and VALREG cannot be equal at this point
3698 because the latter would not have
3699 REG_FUNCTION_VALUE_P true, while the former would if
3700 it were referring to the same register.
3702 If they refer to the same register, this move will be
3703 a no-op, except when function inlining is being
3704 done. */
3705 emit_move_insn (target, valreg);
3707 /* If we are setting a MEM, this code must be executed.
3708 Since it is emitted after the call insn, sibcall
3709 optimization cannot be performed in that case. */
3710 if (MEM_P (target))
3711 sibcall_failure = 1;
3714 else
3715 target = copy_to_reg (avoid_likely_spilled_reg (valreg));
3717 /* If we promoted this return value, make the proper SUBREG.
3718 TARGET might be const0_rtx here, so be careful. */
3719 if (REG_P (target)
3720 && TYPE_MODE (rettype) != BLKmode
3721 && GET_MODE (target) != TYPE_MODE (rettype))
3723 tree type = rettype;
3724 int unsignedp = TYPE_UNSIGNED (type);
3725 int offset = 0;
3726 machine_mode pmode;
3728 /* Ensure we promote as expected, and get the new unsignedness. */
3729 pmode = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
3730 funtype, 1);
3731 gcc_assert (GET_MODE (target) == pmode);
3733 if ((WORDS_BIG_ENDIAN || BYTES_BIG_ENDIAN)
3734 && (GET_MODE_SIZE (GET_MODE (target))
3735 > GET_MODE_SIZE (TYPE_MODE (type))))
3737 offset = GET_MODE_SIZE (GET_MODE (target))
3738 - GET_MODE_SIZE (TYPE_MODE (type));
3739 if (! BYTES_BIG_ENDIAN)
3740 offset = (offset / UNITS_PER_WORD) * UNITS_PER_WORD;
3741 else if (! WORDS_BIG_ENDIAN)
3742 offset %= UNITS_PER_WORD;
3745 target = gen_rtx_SUBREG (TYPE_MODE (type), target, offset);
3746 SUBREG_PROMOTED_VAR_P (target) = 1;
3747 SUBREG_PROMOTED_SET (target, unsignedp);
3750 /* If size of args is variable or this was a constructor call for a stack
3751 argument, restore saved stack-pointer value. */
3753 if (old_stack_level)
3755 rtx_insn *prev = get_last_insn ();
3757 emit_stack_restore (SAVE_BLOCK, old_stack_level);
3758 stack_pointer_delta = old_stack_pointer_delta;
3760 fixup_args_size_notes (prev, get_last_insn (), stack_pointer_delta);
3762 pending_stack_adjust = old_pending_adj;
3763 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
3764 stack_arg_under_construction = old_stack_arg_under_construction;
3765 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3766 stack_usage_map = initial_stack_usage_map;
3767 sibcall_failure = 1;
3769 else if (ACCUMULATE_OUTGOING_ARGS && pass)
3771 #ifdef REG_PARM_STACK_SPACE
3772 if (save_area)
3773 restore_fixed_argument_area (save_area, argblock,
3774 high_to_save, low_to_save);
3775 #endif
3777 /* If we saved any argument areas, restore them. */
3778 for (i = 0; i < num_actuals; i++)
3779 if (args[i].save_area)
3781 machine_mode save_mode = GET_MODE (args[i].save_area);
3782 rtx stack_area
3783 = gen_rtx_MEM (save_mode,
3784 memory_address (save_mode,
3785 XEXP (args[i].stack_slot, 0)));
3787 if (save_mode != BLKmode)
3788 emit_move_insn (stack_area, args[i].save_area);
3789 else
3790 emit_block_move (stack_area, args[i].save_area,
3791 GEN_INT (args[i].locate.size.constant),
3792 BLOCK_OP_CALL_PARM);
3795 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3796 stack_usage_map = initial_stack_usage_map;
3799 /* If this was alloca, record the new stack level. */
3800 if (flags & ECF_MAY_BE_ALLOCA)
3801 record_new_stack_level ();
3803 /* Free up storage we no longer need. */
3804 for (i = 0; i < num_actuals; ++i)
3805 free (args[i].aligned_regs);
3807 targetm.calls.end_call_args ();
3809 insns = get_insns ();
3810 end_sequence ();
3812 if (pass == 0)
3814 tail_call_insns = insns;
3816 /* Restore the pending stack adjustment now that we have
3817 finished generating the sibling call sequence. */
3819 restore_pending_stack_adjust (&save);
3821 /* Prepare arg structure for next iteration. */
3822 for (i = 0; i < num_actuals; i++)
3824 args[i].value = 0;
3825 args[i].aligned_regs = 0;
3826 args[i].stack = 0;
3829 sbitmap_free (stored_args_map);
3830 internal_arg_pointer_exp_state.scan_start = NULL;
3831 internal_arg_pointer_exp_state.cache.release ();
3833 else
3835 normal_call_insns = insns;
3837 /* Verify that we've deallocated all the stack we used. */
3838 gcc_assert ((flags & ECF_NORETURN)
3839 || (old_stack_allocated
3840 == stack_pointer_delta - pending_stack_adjust));
3843 /* If something prevents making this a sibling call,
3844 zero out the sequence. */
3845 if (sibcall_failure)
3846 tail_call_insns = NULL;
3847 else
3848 break;
3851 /* If tail call production succeeded, we need to remove REG_EQUIV notes on
3852 arguments too, as argument area is now clobbered by the call. */
3853 if (tail_call_insns)
3855 emit_insn (tail_call_insns);
3856 crtl->tail_call_emit = true;
3858 else
3860 emit_insn (normal_call_insns);
3861 if (try_tail_call)
3862 /* Ideally we'd emit a message for all of the ways that it could
3863 have failed. */
3864 maybe_complain_about_tail_call (exp, "tail call production failed");
3867 currently_expanding_call--;
3869 free (stack_usage_map_buf);
3871 /* Join result with returned bounds so caller may use them if needed. */
3872 target = chkp_join_splitted_slot (target, valbnd);
3874 return target;
3877 /* A sibling call sequence invalidates any REG_EQUIV notes made for
3878 this function's incoming arguments.
3880 At the start of RTL generation we know the only REG_EQUIV notes
3881 in the rtl chain are those for incoming arguments, so we can look
3882 for REG_EQUIV notes between the start of the function and the
3883 NOTE_INSN_FUNCTION_BEG.
3885 This is (slight) overkill. We could keep track of the highest
3886 argument we clobber and be more selective in removing notes, but it
3887 does not seem to be worth the effort. */
3889 void
3890 fixup_tail_calls (void)
3892 rtx_insn *insn;
3894 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3896 rtx note;
3898 /* There are never REG_EQUIV notes for the incoming arguments
3899 after the NOTE_INSN_FUNCTION_BEG note, so stop if we see it. */
3900 if (NOTE_P (insn)
3901 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3902 break;
3904 note = find_reg_note (insn, REG_EQUIV, 0);
3905 if (note)
3906 remove_note (insn, note);
3907 note = find_reg_note (insn, REG_EQUIV, 0);
3908 gcc_assert (!note);
3912 /* Traverse a list of TYPES and expand all complex types into their
3913 components. */
3914 static tree
3915 split_complex_types (tree types)
3917 tree p;
3919 /* Before allocating memory, check for the common case of no complex. */
3920 for (p = types; p; p = TREE_CHAIN (p))
3922 tree type = TREE_VALUE (p);
3923 if (TREE_CODE (type) == COMPLEX_TYPE
3924 && targetm.calls.split_complex_arg (type))
3925 goto found;
3927 return types;
3929 found:
3930 types = copy_list (types);
3932 for (p = types; p; p = TREE_CHAIN (p))
3934 tree complex_type = TREE_VALUE (p);
3936 if (TREE_CODE (complex_type) == COMPLEX_TYPE
3937 && targetm.calls.split_complex_arg (complex_type))
3939 tree next, imag;
3941 /* Rewrite complex type with component type. */
3942 TREE_VALUE (p) = TREE_TYPE (complex_type);
3943 next = TREE_CHAIN (p);
3945 /* Add another component type for the imaginary part. */
3946 imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
3947 TREE_CHAIN (p) = imag;
3948 TREE_CHAIN (imag) = next;
3950 /* Skip the newly created node. */
3951 p = TREE_CHAIN (p);
3955 return types;
3958 /* Output a library call to function FUN (a SYMBOL_REF rtx).
3959 The RETVAL parameter specifies whether return value needs to be saved, other
3960 parameters are documented in the emit_library_call function below. */
3962 static rtx
3963 emit_library_call_value_1 (int retval, rtx orgfun, rtx value,
3964 enum libcall_type fn_type,
3965 machine_mode outmode, int nargs, va_list p)
3967 /* Total size in bytes of all the stack-parms scanned so far. */
3968 struct args_size args_size;
3969 /* Size of arguments before any adjustments (such as rounding). */
3970 struct args_size original_args_size;
3971 int argnum;
3972 rtx fun;
3973 /* Todo, choose the correct decl type of orgfun. Sadly this information
3974 isn't present here, so we default to native calling abi here. */
3975 tree fndecl ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
3976 tree fntype ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
3977 int count;
3978 rtx argblock = 0;
3979 CUMULATIVE_ARGS args_so_far_v;
3980 cumulative_args_t args_so_far;
3981 struct arg
3983 rtx value;
3984 machine_mode mode;
3985 rtx reg;
3986 int partial;
3987 struct locate_and_pad_arg_data locate;
3988 rtx save_area;
3990 struct arg *argvec;
3991 int old_inhibit_defer_pop = inhibit_defer_pop;
3992 rtx call_fusage = 0;
3993 rtx mem_value = 0;
3994 rtx valreg;
3995 int pcc_struct_value = 0;
3996 int struct_value_size = 0;
3997 int flags;
3998 int reg_parm_stack_space = 0;
3999 int needed;
4000 rtx_insn *before_call;
4001 bool have_push_fusage;
4002 tree tfom; /* type_for_mode (outmode, 0) */
4004 #ifdef REG_PARM_STACK_SPACE
4005 /* Define the boundary of the register parm stack space that needs to be
4006 save, if any. */
4007 int low_to_save = 0, high_to_save = 0;
4008 rtx save_area = 0; /* Place that it is saved. */
4009 #endif
4011 /* Size of the stack reserved for parameter registers. */
4012 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
4013 char *initial_stack_usage_map = stack_usage_map;
4014 char *stack_usage_map_buf = NULL;
4016 rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
4018 #ifdef REG_PARM_STACK_SPACE
4019 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
4020 #endif
4022 /* By default, library functions cannot throw. */
4023 flags = ECF_NOTHROW;
4025 switch (fn_type)
4027 case LCT_NORMAL:
4028 break;
4029 case LCT_CONST:
4030 flags |= ECF_CONST;
4031 break;
4032 case LCT_PURE:
4033 flags |= ECF_PURE;
4034 break;
4035 case LCT_NORETURN:
4036 flags |= ECF_NORETURN;
4037 break;
4038 case LCT_THROW:
4039 flags &= ~ECF_NOTHROW;
4040 break;
4041 case LCT_RETURNS_TWICE:
4042 flags = ECF_RETURNS_TWICE;
4043 break;
4045 fun = orgfun;
4047 /* Ensure current function's preferred stack boundary is at least
4048 what we need. */
4049 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
4050 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
4052 /* If this kind of value comes back in memory,
4053 decide where in memory it should come back. */
4054 if (outmode != VOIDmode)
4056 tfom = lang_hooks.types.type_for_mode (outmode, 0);
4057 if (aggregate_value_p (tfom, 0))
4059 #ifdef PCC_STATIC_STRUCT_RETURN
4060 rtx pointer_reg
4061 = hard_function_value (build_pointer_type (tfom), 0, 0, 0);
4062 mem_value = gen_rtx_MEM (outmode, pointer_reg);
4063 pcc_struct_value = 1;
4064 if (value == 0)
4065 value = gen_reg_rtx (outmode);
4066 #else /* not PCC_STATIC_STRUCT_RETURN */
4067 struct_value_size = GET_MODE_SIZE (outmode);
4068 if (value != 0 && MEM_P (value))
4069 mem_value = value;
4070 else
4071 mem_value = assign_temp (tfom, 1, 1);
4072 #endif
4073 /* This call returns a big structure. */
4074 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
4077 else
4078 tfom = void_type_node;
4080 /* ??? Unfinished: must pass the memory address as an argument. */
4082 /* Copy all the libcall-arguments out of the varargs data
4083 and into a vector ARGVEC.
4085 Compute how to pass each argument. We only support a very small subset
4086 of the full argument passing conventions to limit complexity here since
4087 library functions shouldn't have many args. */
4089 argvec = XALLOCAVEC (struct arg, nargs + 1);
4090 memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
4092 #ifdef INIT_CUMULATIVE_LIBCALL_ARGS
4093 INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far_v, outmode, fun);
4094 #else
4095 INIT_CUMULATIVE_ARGS (args_so_far_v, NULL_TREE, fun, 0, nargs);
4096 #endif
4097 args_so_far = pack_cumulative_args (&args_so_far_v);
4099 args_size.constant = 0;
4100 args_size.var = 0;
4102 count = 0;
4104 push_temp_slots ();
4106 /* If there's a structure value address to be passed,
4107 either pass it in the special place, or pass it as an extra argument. */
4108 if (mem_value && struct_value == 0 && ! pcc_struct_value)
4110 rtx addr = XEXP (mem_value, 0);
4112 nargs++;
4114 /* Make sure it is a reasonable operand for a move or push insn. */
4115 if (!REG_P (addr) && !MEM_P (addr)
4116 && !(CONSTANT_P (addr)
4117 && targetm.legitimate_constant_p (Pmode, addr)))
4118 addr = force_operand (addr, NULL_RTX);
4120 argvec[count].value = addr;
4121 argvec[count].mode = Pmode;
4122 argvec[count].partial = 0;
4124 argvec[count].reg = targetm.calls.function_arg (args_so_far,
4125 Pmode, NULL_TREE, true);
4126 gcc_assert (targetm.calls.arg_partial_bytes (args_so_far, Pmode,
4127 NULL_TREE, 1) == 0);
4129 locate_and_pad_parm (Pmode, NULL_TREE,
4130 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4132 #else
4133 argvec[count].reg != 0,
4134 #endif
4135 reg_parm_stack_space, 0,
4136 NULL_TREE, &args_size, &argvec[count].locate);
4138 if (argvec[count].reg == 0 || argvec[count].partial != 0
4139 || reg_parm_stack_space > 0)
4140 args_size.constant += argvec[count].locate.size.constant;
4142 targetm.calls.function_arg_advance (args_so_far, Pmode, (tree) 0, true);
4144 count++;
4147 for (; count < nargs; count++)
4149 rtx val = va_arg (p, rtx);
4150 machine_mode mode = (machine_mode) va_arg (p, int);
4151 int unsigned_p = 0;
4153 /* We cannot convert the arg value to the mode the library wants here;
4154 must do it earlier where we know the signedness of the arg. */
4155 gcc_assert (mode != BLKmode
4156 && (GET_MODE (val) == mode || GET_MODE (val) == VOIDmode));
4158 /* Make sure it is a reasonable operand for a move or push insn. */
4159 if (!REG_P (val) && !MEM_P (val)
4160 && !(CONSTANT_P (val) && targetm.legitimate_constant_p (mode, val)))
4161 val = force_operand (val, NULL_RTX);
4163 if (pass_by_reference (&args_so_far_v, mode, NULL_TREE, 1))
4165 rtx slot;
4166 int must_copy
4167 = !reference_callee_copied (&args_so_far_v, mode, NULL_TREE, 1);
4169 /* If this was a CONST function, it is now PURE since it now
4170 reads memory. */
4171 if (flags & ECF_CONST)
4173 flags &= ~ECF_CONST;
4174 flags |= ECF_PURE;
4177 if (MEM_P (val) && !must_copy)
4179 tree val_expr = MEM_EXPR (val);
4180 if (val_expr)
4181 mark_addressable (val_expr);
4182 slot = val;
4184 else
4186 slot = assign_temp (lang_hooks.types.type_for_mode (mode, 0),
4187 1, 1);
4188 emit_move_insn (slot, val);
4191 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4192 gen_rtx_USE (VOIDmode, slot),
4193 call_fusage);
4194 if (must_copy)
4195 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4196 gen_rtx_CLOBBER (VOIDmode,
4197 slot),
4198 call_fusage);
4200 mode = Pmode;
4201 val = force_operand (XEXP (slot, 0), NULL_RTX);
4204 mode = promote_function_mode (NULL_TREE, mode, &unsigned_p, NULL_TREE, 0);
4205 argvec[count].mode = mode;
4206 argvec[count].value = convert_modes (mode, GET_MODE (val), val, unsigned_p);
4207 argvec[count].reg = targetm.calls.function_arg (args_so_far, mode,
4208 NULL_TREE, true);
4210 argvec[count].partial
4211 = targetm.calls.arg_partial_bytes (args_so_far, mode, NULL_TREE, 1);
4213 if (argvec[count].reg == 0
4214 || argvec[count].partial != 0
4215 || reg_parm_stack_space > 0)
4217 locate_and_pad_parm (mode, NULL_TREE,
4218 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4220 #else
4221 argvec[count].reg != 0,
4222 #endif
4223 reg_parm_stack_space, argvec[count].partial,
4224 NULL_TREE, &args_size, &argvec[count].locate);
4225 args_size.constant += argvec[count].locate.size.constant;
4226 gcc_assert (!argvec[count].locate.size.var);
4228 #ifdef BLOCK_REG_PADDING
4229 else
4230 /* The argument is passed entirely in registers. See at which
4231 end it should be padded. */
4232 argvec[count].locate.where_pad =
4233 BLOCK_REG_PADDING (mode, NULL_TREE,
4234 GET_MODE_SIZE (mode) <= UNITS_PER_WORD);
4235 #endif
4237 targetm.calls.function_arg_advance (args_so_far, mode, (tree) 0, true);
4240 /* If this machine requires an external definition for library
4241 functions, write one out. */
4242 assemble_external_libcall (fun);
4244 original_args_size = args_size;
4245 args_size.constant = (((args_size.constant
4246 + stack_pointer_delta
4247 + STACK_BYTES - 1)
4248 / STACK_BYTES
4249 * STACK_BYTES)
4250 - stack_pointer_delta);
4252 args_size.constant = MAX (args_size.constant,
4253 reg_parm_stack_space);
4255 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4256 args_size.constant -= reg_parm_stack_space;
4258 if (args_size.constant > crtl->outgoing_args_size)
4259 crtl->outgoing_args_size = args_size.constant;
4261 if (flag_stack_usage_info && !ACCUMULATE_OUTGOING_ARGS)
4263 int pushed = args_size.constant + pending_stack_adjust;
4264 if (pushed > current_function_pushed_stack_size)
4265 current_function_pushed_stack_size = pushed;
4268 if (ACCUMULATE_OUTGOING_ARGS)
4270 /* Since the stack pointer will never be pushed, it is possible for
4271 the evaluation of a parm to clobber something we have already
4272 written to the stack. Since most function calls on RISC machines
4273 do not use the stack, this is uncommon, but must work correctly.
4275 Therefore, we save any area of the stack that was already written
4276 and that we are using. Here we set up to do this by making a new
4277 stack usage map from the old one.
4279 Another approach might be to try to reorder the argument
4280 evaluations to avoid this conflicting stack usage. */
4282 needed = args_size.constant;
4284 /* Since we will be writing into the entire argument area, the
4285 map must be allocated for its entire size, not just the part that
4286 is the responsibility of the caller. */
4287 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4288 needed += reg_parm_stack_space;
4290 if (ARGS_GROW_DOWNWARD)
4291 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
4292 needed + 1);
4293 else
4294 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use, needed);
4296 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
4297 stack_usage_map = stack_usage_map_buf;
4299 if (initial_highest_arg_in_use)
4300 memcpy (stack_usage_map, initial_stack_usage_map,
4301 initial_highest_arg_in_use);
4303 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
4304 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
4305 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
4306 needed = 0;
4308 /* We must be careful to use virtual regs before they're instantiated,
4309 and real regs afterwards. Loop optimization, for example, can create
4310 new libcalls after we've instantiated the virtual regs, and if we
4311 use virtuals anyway, they won't match the rtl patterns. */
4313 if (virtuals_instantiated)
4314 argblock = plus_constant (Pmode, stack_pointer_rtx,
4315 STACK_POINTER_OFFSET);
4316 else
4317 argblock = virtual_outgoing_args_rtx;
4319 else
4321 if (!PUSH_ARGS)
4322 argblock = push_block (GEN_INT (args_size.constant), 0, 0);
4325 /* We push args individually in reverse order, perform stack alignment
4326 before the first push (the last arg). */
4327 if (argblock == 0)
4328 anti_adjust_stack (GEN_INT (args_size.constant
4329 - original_args_size.constant));
4331 argnum = nargs - 1;
4333 #ifdef REG_PARM_STACK_SPACE
4334 if (ACCUMULATE_OUTGOING_ARGS)
4336 /* The argument list is the property of the called routine and it
4337 may clobber it. If the fixed area has been used for previous
4338 parameters, we must save and restore it. */
4339 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
4340 &low_to_save, &high_to_save);
4342 #endif
4344 /* When expanding a normal call, args are stored in push order,
4345 which is the reverse of what we have here. */
4346 bool any_regs = false;
4347 for (int i = nargs; i-- > 0; )
4348 if (argvec[i].reg != NULL_RTX)
4350 targetm.calls.call_args (argvec[i].reg, NULL_TREE);
4351 any_regs = true;
4353 if (!any_regs)
4354 targetm.calls.call_args (pc_rtx, NULL_TREE);
4356 /* Push the args that need to be pushed. */
4358 have_push_fusage = false;
4360 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4361 are to be pushed. */
4362 for (count = 0; count < nargs; count++, argnum--)
4364 machine_mode mode = argvec[argnum].mode;
4365 rtx val = argvec[argnum].value;
4366 rtx reg = argvec[argnum].reg;
4367 int partial = argvec[argnum].partial;
4368 unsigned int parm_align = argvec[argnum].locate.boundary;
4369 int lower_bound = 0, upper_bound = 0, i;
4371 if (! (reg != 0 && partial == 0))
4373 rtx use;
4375 if (ACCUMULATE_OUTGOING_ARGS)
4377 /* If this is being stored into a pre-allocated, fixed-size,
4378 stack area, save any previous data at that location. */
4380 if (ARGS_GROW_DOWNWARD)
4382 /* stack_slot is negative, but we want to index stack_usage_map
4383 with positive values. */
4384 upper_bound = -argvec[argnum].locate.slot_offset.constant + 1;
4385 lower_bound = upper_bound - argvec[argnum].locate.size.constant;
4387 else
4389 lower_bound = argvec[argnum].locate.slot_offset.constant;
4390 upper_bound = lower_bound + argvec[argnum].locate.size.constant;
4393 i = lower_bound;
4394 /* Don't worry about things in the fixed argument area;
4395 it has already been saved. */
4396 if (i < reg_parm_stack_space)
4397 i = reg_parm_stack_space;
4398 while (i < upper_bound && stack_usage_map[i] == 0)
4399 i++;
4401 if (i < upper_bound)
4403 /* We need to make a save area. */
4404 unsigned int size
4405 = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
4406 machine_mode save_mode
4407 = mode_for_size (size, MODE_INT, 1);
4408 rtx adr
4409 = plus_constant (Pmode, argblock,
4410 argvec[argnum].locate.offset.constant);
4411 rtx stack_area
4412 = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
4414 if (save_mode == BLKmode)
4416 argvec[argnum].save_area
4417 = assign_stack_temp (BLKmode,
4418 argvec[argnum].locate.size.constant
4421 emit_block_move (validize_mem
4422 (copy_rtx (argvec[argnum].save_area)),
4423 stack_area,
4424 GEN_INT (argvec[argnum].locate.size.constant),
4425 BLOCK_OP_CALL_PARM);
4427 else
4429 argvec[argnum].save_area = gen_reg_rtx (save_mode);
4431 emit_move_insn (argvec[argnum].save_area, stack_area);
4436 emit_push_insn (val, mode, NULL_TREE, NULL_RTX, parm_align,
4437 partial, reg, 0, argblock,
4438 GEN_INT (argvec[argnum].locate.offset.constant),
4439 reg_parm_stack_space,
4440 ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad), false);
4442 /* Now mark the segment we just used. */
4443 if (ACCUMULATE_OUTGOING_ARGS)
4444 for (i = lower_bound; i < upper_bound; i++)
4445 stack_usage_map[i] = 1;
4447 NO_DEFER_POP;
4449 /* Indicate argument access so that alias.c knows that these
4450 values are live. */
4451 if (argblock)
4452 use = plus_constant (Pmode, argblock,
4453 argvec[argnum].locate.offset.constant);
4454 else if (have_push_fusage)
4455 continue;
4456 else
4458 /* When arguments are pushed, trying to tell alias.c where
4459 exactly this argument is won't work, because the
4460 auto-increment causes confusion. So we merely indicate
4461 that we access something with a known mode somewhere on
4462 the stack. */
4463 use = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4464 gen_rtx_SCRATCH (Pmode));
4465 have_push_fusage = true;
4467 use = gen_rtx_MEM (argvec[argnum].mode, use);
4468 use = gen_rtx_USE (VOIDmode, use);
4469 call_fusage = gen_rtx_EXPR_LIST (VOIDmode, use, call_fusage);
4473 argnum = nargs - 1;
4475 fun = prepare_call_address (NULL, fun, NULL, &call_fusage, 0, 0);
4477 /* Now load any reg parms into their regs. */
4479 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4480 are to be pushed. */
4481 for (count = 0; count < nargs; count++, argnum--)
4483 machine_mode mode = argvec[argnum].mode;
4484 rtx val = argvec[argnum].value;
4485 rtx reg = argvec[argnum].reg;
4486 int partial = argvec[argnum].partial;
4487 #ifdef BLOCK_REG_PADDING
4488 int size = 0;
4489 #endif
4491 /* Handle calls that pass values in multiple non-contiguous
4492 locations. The PA64 has examples of this for library calls. */
4493 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4494 emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (mode));
4495 else if (reg != 0 && partial == 0)
4497 emit_move_insn (reg, val);
4498 #ifdef BLOCK_REG_PADDING
4499 size = GET_MODE_SIZE (argvec[argnum].mode);
4501 /* Copied from load_register_parameters. */
4503 /* Handle case where we have a value that needs shifting
4504 up to the msb. eg. a QImode value and we're padding
4505 upward on a BYTES_BIG_ENDIAN machine. */
4506 if (size < UNITS_PER_WORD
4507 && (argvec[argnum].locate.where_pad
4508 == (BYTES_BIG_ENDIAN ? upward : downward)))
4510 rtx x;
4511 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
4513 /* Assigning REG here rather than a temp makes CALL_FUSAGE
4514 report the whole reg as used. Strictly speaking, the
4515 call only uses SIZE bytes at the msb end, but it doesn't
4516 seem worth generating rtl to say that. */
4517 reg = gen_rtx_REG (word_mode, REGNO (reg));
4518 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
4519 if (x != reg)
4520 emit_move_insn (reg, x);
4522 #endif
4525 NO_DEFER_POP;
4528 /* Any regs containing parms remain in use through the call. */
4529 for (count = 0; count < nargs; count++)
4531 rtx reg = argvec[count].reg;
4532 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4533 use_group_regs (&call_fusage, reg);
4534 else if (reg != 0)
4536 int partial = argvec[count].partial;
4537 if (partial)
4539 int nregs;
4540 gcc_assert (partial % UNITS_PER_WORD == 0);
4541 nregs = partial / UNITS_PER_WORD;
4542 use_regs (&call_fusage, REGNO (reg), nregs);
4544 else
4545 use_reg (&call_fusage, reg);
4549 /* Pass the function the address in which to return a structure value. */
4550 if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
4552 emit_move_insn (struct_value,
4553 force_reg (Pmode,
4554 force_operand (XEXP (mem_value, 0),
4555 NULL_RTX)));
4556 if (REG_P (struct_value))
4557 use_reg (&call_fusage, struct_value);
4560 /* Don't allow popping to be deferred, since then
4561 cse'ing of library calls could delete a call and leave the pop. */
4562 NO_DEFER_POP;
4563 valreg = (mem_value == 0 && outmode != VOIDmode
4564 ? hard_libcall_value (outmode, orgfun) : NULL_RTX);
4566 /* Stack must be properly aligned now. */
4567 gcc_assert (!(stack_pointer_delta
4568 & (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1)));
4570 before_call = get_last_insn ();
4572 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
4573 will set inhibit_defer_pop to that value. */
4574 /* The return type is needed to decide how many bytes the function pops.
4575 Signedness plays no role in that, so for simplicity, we pretend it's
4576 always signed. We also assume that the list of arguments passed has
4577 no impact, so we pretend it is unknown. */
4579 emit_call_1 (fun, NULL,
4580 get_identifier (XSTR (orgfun, 0)),
4581 build_function_type (tfom, NULL_TREE),
4582 original_args_size.constant, args_size.constant,
4583 struct_value_size,
4584 targetm.calls.function_arg (args_so_far,
4585 VOIDmode, void_type_node, true),
4586 valreg,
4587 old_inhibit_defer_pop + 1, call_fusage, flags, args_so_far);
4589 if (flag_ipa_ra)
4591 rtx datum = orgfun;
4592 gcc_assert (GET_CODE (datum) == SYMBOL_REF);
4593 rtx_call_insn *last = last_call_insn ();
4594 add_reg_note (last, REG_CALL_DECL, datum);
4597 /* Right-shift returned value if necessary. */
4598 if (!pcc_struct_value
4599 && TYPE_MODE (tfom) != BLKmode
4600 && targetm.calls.return_in_msb (tfom))
4602 shift_return_value (TYPE_MODE (tfom), false, valreg);
4603 valreg = gen_rtx_REG (TYPE_MODE (tfom), REGNO (valreg));
4606 targetm.calls.end_call_args ();
4608 /* For calls to `setjmp', etc., inform function.c:setjmp_warnings
4609 that it should complain if nonvolatile values are live. For
4610 functions that cannot return, inform flow that control does not
4611 fall through. */
4612 if (flags & ECF_NORETURN)
4614 /* The barrier note must be emitted
4615 immediately after the CALL_INSN. Some ports emit more than
4616 just a CALL_INSN above, so we must search for it here. */
4617 rtx_insn *last = get_last_insn ();
4618 while (!CALL_P (last))
4620 last = PREV_INSN (last);
4621 /* There was no CALL_INSN? */
4622 gcc_assert (last != before_call);
4625 emit_barrier_after (last);
4628 /* Consider that "regular" libcalls, i.e. all of them except for LCT_THROW
4629 and LCT_RETURNS_TWICE, cannot perform non-local gotos. */
4630 if (flags & ECF_NOTHROW)
4632 rtx_insn *last = get_last_insn ();
4633 while (!CALL_P (last))
4635 last = PREV_INSN (last);
4636 /* There was no CALL_INSN? */
4637 gcc_assert (last != before_call);
4640 make_reg_eh_region_note_nothrow_nononlocal (last);
4643 /* Now restore inhibit_defer_pop to its actual original value. */
4644 OK_DEFER_POP;
4646 pop_temp_slots ();
4648 /* Copy the value to the right place. */
4649 if (outmode != VOIDmode && retval)
4651 if (mem_value)
4653 if (value == 0)
4654 value = mem_value;
4655 if (value != mem_value)
4656 emit_move_insn (value, mem_value);
4658 else if (GET_CODE (valreg) == PARALLEL)
4660 if (value == 0)
4661 value = gen_reg_rtx (outmode);
4662 emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
4664 else
4666 /* Convert to the proper mode if a promotion has been active. */
4667 if (GET_MODE (valreg) != outmode)
4669 int unsignedp = TYPE_UNSIGNED (tfom);
4671 gcc_assert (promote_function_mode (tfom, outmode, &unsignedp,
4672 fndecl ? TREE_TYPE (fndecl) : fntype, 1)
4673 == GET_MODE (valreg));
4674 valreg = convert_modes (outmode, GET_MODE (valreg), valreg, 0);
4677 if (value != 0)
4678 emit_move_insn (value, valreg);
4679 else
4680 value = valreg;
4684 if (ACCUMULATE_OUTGOING_ARGS)
4686 #ifdef REG_PARM_STACK_SPACE
4687 if (save_area)
4688 restore_fixed_argument_area (save_area, argblock,
4689 high_to_save, low_to_save);
4690 #endif
4692 /* If we saved any argument areas, restore them. */
4693 for (count = 0; count < nargs; count++)
4694 if (argvec[count].save_area)
4696 machine_mode save_mode = GET_MODE (argvec[count].save_area);
4697 rtx adr = plus_constant (Pmode, argblock,
4698 argvec[count].locate.offset.constant);
4699 rtx stack_area = gen_rtx_MEM (save_mode,
4700 memory_address (save_mode, adr));
4702 if (save_mode == BLKmode)
4703 emit_block_move (stack_area,
4704 validize_mem
4705 (copy_rtx (argvec[count].save_area)),
4706 GEN_INT (argvec[count].locate.size.constant),
4707 BLOCK_OP_CALL_PARM);
4708 else
4709 emit_move_insn (stack_area, argvec[count].save_area);
4712 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
4713 stack_usage_map = initial_stack_usage_map;
4716 free (stack_usage_map_buf);
4718 return value;
4722 /* Output a library call to function FUN (a SYMBOL_REF rtx)
4723 (emitting the queue unless NO_QUEUE is nonzero),
4724 for a value of mode OUTMODE,
4725 with NARGS different arguments, passed as alternating rtx values
4726 and machine_modes to convert them to.
4728 FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for
4729 `const' calls, LCT_PURE for `pure' calls, or other LCT_ value for
4730 other types of library calls. */
4732 void
4733 emit_library_call (rtx orgfun, enum libcall_type fn_type,
4734 machine_mode outmode, int nargs, ...)
4736 va_list p;
4738 va_start (p, nargs);
4739 emit_library_call_value_1 (0, orgfun, NULL_RTX, fn_type, outmode, nargs, p);
4740 va_end (p);
4743 /* Like emit_library_call except that an extra argument, VALUE,
4744 comes second and says where to store the result.
4745 (If VALUE is zero, this function chooses a convenient way
4746 to return the value.
4748 This function returns an rtx for where the value is to be found.
4749 If VALUE is nonzero, VALUE is returned. */
4752 emit_library_call_value (rtx orgfun, rtx value,
4753 enum libcall_type fn_type,
4754 machine_mode outmode, int nargs, ...)
4756 rtx result;
4757 va_list p;
4759 va_start (p, nargs);
4760 result = emit_library_call_value_1 (1, orgfun, value, fn_type, outmode,
4761 nargs, p);
4762 va_end (p);
4764 return result;
4768 /* Store pointer bounds argument ARG into Bounds Table entry
4769 associated with PARM. */
4770 static void
4771 store_bounds (struct arg_data *arg, struct arg_data *parm)
4773 rtx slot = NULL, ptr = NULL, addr = NULL;
4775 /* We may pass bounds not associated with any pointer. */
4776 if (!parm)
4778 gcc_assert (arg->special_slot);
4779 slot = arg->special_slot;
4780 ptr = const0_rtx;
4782 /* Find pointer associated with bounds and where it is
4783 passed. */
4784 else
4786 if (!parm->reg)
4788 gcc_assert (!arg->special_slot);
4790 addr = adjust_address (parm->stack, Pmode, arg->pointer_offset);
4792 else if (REG_P (parm->reg))
4794 gcc_assert (arg->special_slot);
4795 slot = arg->special_slot;
4797 if (MEM_P (parm->value))
4798 addr = adjust_address (parm->value, Pmode, arg->pointer_offset);
4799 else if (REG_P (parm->value))
4800 ptr = gen_rtx_SUBREG (Pmode, parm->value, arg->pointer_offset);
4801 else
4803 gcc_assert (!arg->pointer_offset);
4804 ptr = parm->value;
4807 else
4809 gcc_assert (GET_CODE (parm->reg) == PARALLEL);
4811 gcc_assert (arg->special_slot);
4812 slot = arg->special_slot;
4814 if (parm->parallel_value)
4815 ptr = chkp_get_value_with_offs (parm->parallel_value,
4816 GEN_INT (arg->pointer_offset));
4817 else
4818 gcc_unreachable ();
4822 /* Expand bounds. */
4823 if (!arg->value)
4824 arg->value = expand_normal (arg->tree_value);
4826 targetm.calls.store_bounds_for_arg (ptr, addr, arg->value, slot);
4829 /* Store a single argument for a function call
4830 into the register or memory area where it must be passed.
4831 *ARG describes the argument value and where to pass it.
4833 ARGBLOCK is the address of the stack-block for all the arguments,
4834 or 0 on a machine where arguments are pushed individually.
4836 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
4837 so must be careful about how the stack is used.
4839 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
4840 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
4841 that we need not worry about saving and restoring the stack.
4843 FNDECL is the declaration of the function we are calling.
4845 Return nonzero if this arg should cause sibcall failure,
4846 zero otherwise. */
4848 static int
4849 store_one_arg (struct arg_data *arg, rtx argblock, int flags,
4850 int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
4852 tree pval = arg->tree_value;
4853 rtx reg = 0;
4854 int partial = 0;
4855 int used = 0;
4856 int i, lower_bound = 0, upper_bound = 0;
4857 int sibcall_failure = 0;
4859 if (TREE_CODE (pval) == ERROR_MARK)
4860 return 1;
4862 /* Push a new temporary level for any temporaries we make for
4863 this argument. */
4864 push_temp_slots ();
4866 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
4868 /* If this is being stored into a pre-allocated, fixed-size, stack area,
4869 save any previous data at that location. */
4870 if (argblock && ! variable_size && arg->stack)
4872 if (ARGS_GROW_DOWNWARD)
4874 /* stack_slot is negative, but we want to index stack_usage_map
4875 with positive values. */
4876 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4877 upper_bound = -INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1)) + 1;
4878 else
4879 upper_bound = 0;
4881 lower_bound = upper_bound - arg->locate.size.constant;
4883 else
4885 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4886 lower_bound = INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1));
4887 else
4888 lower_bound = 0;
4890 upper_bound = lower_bound + arg->locate.size.constant;
4893 i = lower_bound;
4894 /* Don't worry about things in the fixed argument area;
4895 it has already been saved. */
4896 if (i < reg_parm_stack_space)
4897 i = reg_parm_stack_space;
4898 while (i < upper_bound && stack_usage_map[i] == 0)
4899 i++;
4901 if (i < upper_bound)
4903 /* We need to make a save area. */
4904 unsigned int size = arg->locate.size.constant * BITS_PER_UNIT;
4905 machine_mode save_mode = mode_for_size (size, MODE_INT, 1);
4906 rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
4907 rtx stack_area = gen_rtx_MEM (save_mode, adr);
4909 if (save_mode == BLKmode)
4911 arg->save_area
4912 = assign_temp (TREE_TYPE (arg->tree_value), 1, 1);
4913 preserve_temp_slots (arg->save_area);
4914 emit_block_move (validize_mem (copy_rtx (arg->save_area)),
4915 stack_area,
4916 GEN_INT (arg->locate.size.constant),
4917 BLOCK_OP_CALL_PARM);
4919 else
4921 arg->save_area = gen_reg_rtx (save_mode);
4922 emit_move_insn (arg->save_area, stack_area);
4928 /* If this isn't going to be placed on both the stack and in registers,
4929 set up the register and number of words. */
4930 if (! arg->pass_on_stack)
4932 if (flags & ECF_SIBCALL)
4933 reg = arg->tail_call_reg;
4934 else
4935 reg = arg->reg;
4936 partial = arg->partial;
4939 /* Being passed entirely in a register. We shouldn't be called in
4940 this case. */
4941 gcc_assert (reg == 0 || partial != 0);
4943 /* If this arg needs special alignment, don't load the registers
4944 here. */
4945 if (arg->n_aligned_regs != 0)
4946 reg = 0;
4948 /* If this is being passed partially in a register, we can't evaluate
4949 it directly into its stack slot. Otherwise, we can. */
4950 if (arg->value == 0)
4952 /* stack_arg_under_construction is nonzero if a function argument is
4953 being evaluated directly into the outgoing argument list and
4954 expand_call must take special action to preserve the argument list
4955 if it is called recursively.
4957 For scalar function arguments stack_usage_map is sufficient to
4958 determine which stack slots must be saved and restored. Scalar
4959 arguments in general have pass_on_stack == 0.
4961 If this argument is initialized by a function which takes the
4962 address of the argument (a C++ constructor or a C function
4963 returning a BLKmode structure), then stack_usage_map is
4964 insufficient and expand_call must push the stack around the
4965 function call. Such arguments have pass_on_stack == 1.
4967 Note that it is always safe to set stack_arg_under_construction,
4968 but this generates suboptimal code if set when not needed. */
4970 if (arg->pass_on_stack)
4971 stack_arg_under_construction++;
4973 arg->value = expand_expr (pval,
4974 (partial
4975 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
4976 ? NULL_RTX : arg->stack,
4977 VOIDmode, EXPAND_STACK_PARM);
4979 /* If we are promoting object (or for any other reason) the mode
4980 doesn't agree, convert the mode. */
4982 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
4983 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
4984 arg->value, arg->unsignedp);
4986 if (arg->pass_on_stack)
4987 stack_arg_under_construction--;
4990 /* Check for overlap with already clobbered argument area. */
4991 if ((flags & ECF_SIBCALL)
4992 && MEM_P (arg->value)
4993 && mem_overlaps_already_clobbered_arg_p (XEXP (arg->value, 0),
4994 arg->locate.size.constant))
4995 sibcall_failure = 1;
4997 /* Don't allow anything left on stack from computation
4998 of argument to alloca. */
4999 if (flags & ECF_MAY_BE_ALLOCA)
5000 do_pending_stack_adjust ();
5002 if (arg->value == arg->stack)
5003 /* If the value is already in the stack slot, we are done. */
5005 else if (arg->mode != BLKmode)
5007 int size;
5008 unsigned int parm_align;
5010 /* Argument is a scalar, not entirely passed in registers.
5011 (If part is passed in registers, arg->partial says how much
5012 and emit_push_insn will take care of putting it there.)
5014 Push it, and if its size is less than the
5015 amount of space allocated to it,
5016 also bump stack pointer by the additional space.
5017 Note that in C the default argument promotions
5018 will prevent such mismatches. */
5020 size = GET_MODE_SIZE (arg->mode);
5021 /* Compute how much space the push instruction will push.
5022 On many machines, pushing a byte will advance the stack
5023 pointer by a halfword. */
5024 #ifdef PUSH_ROUNDING
5025 size = PUSH_ROUNDING (size);
5026 #endif
5027 used = size;
5029 /* Compute how much space the argument should get:
5030 round up to a multiple of the alignment for arguments. */
5031 if (none != FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)))
5032 used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
5033 / (PARM_BOUNDARY / BITS_PER_UNIT))
5034 * (PARM_BOUNDARY / BITS_PER_UNIT));
5036 /* Compute the alignment of the pushed argument. */
5037 parm_align = arg->locate.boundary;
5038 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
5040 int pad = used - size;
5041 if (pad)
5043 unsigned int pad_align = (pad & -pad) * BITS_PER_UNIT;
5044 parm_align = MIN (parm_align, pad_align);
5048 /* This isn't already where we want it on the stack, so put it there.
5049 This can either be done with push or copy insns. */
5050 if (!emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), NULL_RTX,
5051 parm_align, partial, reg, used - size, argblock,
5052 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
5053 ARGS_SIZE_RTX (arg->locate.alignment_pad), true))
5054 sibcall_failure = 1;
5056 /* Unless this is a partially-in-register argument, the argument is now
5057 in the stack. */
5058 if (partial == 0)
5059 arg->value = arg->stack;
5061 else
5063 /* BLKmode, at least partly to be pushed. */
5065 unsigned int parm_align;
5066 int excess;
5067 rtx size_rtx;
5069 /* Pushing a nonscalar.
5070 If part is passed in registers, PARTIAL says how much
5071 and emit_push_insn will take care of putting it there. */
5073 /* Round its size up to a multiple
5074 of the allocation unit for arguments. */
5076 if (arg->locate.size.var != 0)
5078 excess = 0;
5079 size_rtx = ARGS_SIZE_RTX (arg->locate.size);
5081 else
5083 /* PUSH_ROUNDING has no effect on us, because emit_push_insn
5084 for BLKmode is careful to avoid it. */
5085 excess = (arg->locate.size.constant
5086 - int_size_in_bytes (TREE_TYPE (pval))
5087 + partial);
5088 size_rtx = expand_expr (size_in_bytes (TREE_TYPE (pval)),
5089 NULL_RTX, TYPE_MODE (sizetype),
5090 EXPAND_NORMAL);
5093 parm_align = arg->locate.boundary;
5095 /* When an argument is padded down, the block is aligned to
5096 PARM_BOUNDARY, but the actual argument isn't. */
5097 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
5099 if (arg->locate.size.var)
5100 parm_align = BITS_PER_UNIT;
5101 else if (excess)
5103 unsigned int excess_align = (excess & -excess) * BITS_PER_UNIT;
5104 parm_align = MIN (parm_align, excess_align);
5108 if ((flags & ECF_SIBCALL) && MEM_P (arg->value))
5110 /* emit_push_insn might not work properly if arg->value and
5111 argblock + arg->locate.offset areas overlap. */
5112 rtx x = arg->value;
5113 int i = 0;
5115 if (XEXP (x, 0) == crtl->args.internal_arg_pointer
5116 || (GET_CODE (XEXP (x, 0)) == PLUS
5117 && XEXP (XEXP (x, 0), 0) ==
5118 crtl->args.internal_arg_pointer
5119 && CONST_INT_P (XEXP (XEXP (x, 0), 1))))
5121 if (XEXP (x, 0) != crtl->args.internal_arg_pointer)
5122 i = INTVAL (XEXP (XEXP (x, 0), 1));
5124 /* arg.locate doesn't contain the pretend_args_size offset,
5125 it's part of argblock. Ensure we don't count it in I. */
5126 if (STACK_GROWS_DOWNWARD)
5127 i -= crtl->args.pretend_args_size;
5128 else
5129 i += crtl->args.pretend_args_size;
5131 /* expand_call should ensure this. */
5132 gcc_assert (!arg->locate.offset.var
5133 && arg->locate.size.var == 0
5134 && CONST_INT_P (size_rtx));
5136 if (arg->locate.offset.constant > i)
5138 if (arg->locate.offset.constant < i + INTVAL (size_rtx))
5139 sibcall_failure = 1;
5141 else if (arg->locate.offset.constant < i)
5143 /* Use arg->locate.size.constant instead of size_rtx
5144 because we only care about the part of the argument
5145 on the stack. */
5146 if (i < (arg->locate.offset.constant
5147 + arg->locate.size.constant))
5148 sibcall_failure = 1;
5150 else
5152 /* Even though they appear to be at the same location,
5153 if part of the outgoing argument is in registers,
5154 they aren't really at the same location. Check for
5155 this by making sure that the incoming size is the
5156 same as the outgoing size. */
5157 if (arg->locate.size.constant != INTVAL (size_rtx))
5158 sibcall_failure = 1;
5163 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
5164 parm_align, partial, reg, excess, argblock,
5165 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
5166 ARGS_SIZE_RTX (arg->locate.alignment_pad), false);
5168 /* Unless this is a partially-in-register argument, the argument is now
5169 in the stack.
5171 ??? Unlike the case above, in which we want the actual
5172 address of the data, so that we can load it directly into a
5173 register, here we want the address of the stack slot, so that
5174 it's properly aligned for word-by-word copying or something
5175 like that. It's not clear that this is always correct. */
5176 if (partial == 0)
5177 arg->value = arg->stack_slot;
5180 if (arg->reg && GET_CODE (arg->reg) == PARALLEL)
5182 tree type = TREE_TYPE (arg->tree_value);
5183 arg->parallel_value
5184 = emit_group_load_into_temps (arg->reg, arg->value, type,
5185 int_size_in_bytes (type));
5188 /* Mark all slots this store used. */
5189 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
5190 && argblock && ! variable_size && arg->stack)
5191 for (i = lower_bound; i < upper_bound; i++)
5192 stack_usage_map[i] = 1;
5194 /* Once we have pushed something, pops can't safely
5195 be deferred during the rest of the arguments. */
5196 NO_DEFER_POP;
5198 /* Free any temporary slots made in processing this argument. */
5199 pop_temp_slots ();
5201 return sibcall_failure;
5204 /* Nonzero if we do not know how to pass TYPE solely in registers. */
5206 bool
5207 must_pass_in_stack_var_size (machine_mode mode ATTRIBUTE_UNUSED,
5208 const_tree type)
5210 if (!type)
5211 return false;
5213 /* If the type has variable size... */
5214 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5215 return true;
5217 /* If the type is marked as addressable (it is required
5218 to be constructed into the stack)... */
5219 if (TREE_ADDRESSABLE (type))
5220 return true;
5222 return false;
5225 /* Another version of the TARGET_MUST_PASS_IN_STACK hook. This one
5226 takes trailing padding of a structure into account. */
5227 /* ??? Should be able to merge these two by examining BLOCK_REG_PADDING. */
5229 bool
5230 must_pass_in_stack_var_size_or_pad (machine_mode mode, const_tree type)
5232 if (!type)
5233 return false;
5235 /* If the type has variable size... */
5236 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5237 return true;
5239 /* If the type is marked as addressable (it is required
5240 to be constructed into the stack)... */
5241 if (TREE_ADDRESSABLE (type))
5242 return true;
5244 /* If the padding and mode of the type is such that a copy into
5245 a register would put it into the wrong part of the register. */
5246 if (mode == BLKmode
5247 && int_size_in_bytes (type) % (PARM_BOUNDARY / BITS_PER_UNIT)
5248 && (FUNCTION_ARG_PADDING (mode, type)
5249 == (BYTES_BIG_ENDIAN ? upward : downward)))
5250 return true;
5252 return false;