2015-05-12 Pierre-Marie de Rodat <derodat@adacore.com>
[official-gcc.git] / gcc / calls.c
blob51b896ef25cd7983b3cdbe365dcccf99012d8f9a
1 /* Convert function calls to rtl insns, for GNU C compiler.
2 Copyright (C) 1989-2015 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 "tm.h"
24 #include "rtl.h"
25 #include "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "stor-layout.h"
37 #include "varasm.h"
38 #include "stringpool.h"
39 #include "attribs.h"
40 #include "predict.h"
41 #include "hashtab.h"
42 #include "hard-reg-set.h"
43 #include "function.h"
44 #include "basic-block.h"
45 #include "tree-ssa-alias.h"
46 #include "internal-fn.h"
47 #include "gimple-expr.h"
48 #include "is-a.h"
49 #include "gimple.h"
50 #include "flags.h"
51 #include "statistics.h"
52 #include "real.h"
53 #include "fixed-value.h"
54 #include "insn-config.h"
55 #include "expmed.h"
56 #include "dojump.h"
57 #include "explow.h"
58 #include "calls.h"
59 #include "emit-rtl.h"
60 #include "stmt.h"
61 #include "expr.h"
62 #include "insn-codes.h"
63 #include "optabs.h"
64 #include "libfuncs.h"
65 #include "regs.h"
66 #include "diagnostic-core.h"
67 #include "output.h"
68 #include "tm_p.h"
69 #include "timevar.h"
70 #include "sbitmap.h"
71 #include "bitmap.h"
72 #include "langhooks.h"
73 #include "target.h"
74 #include "hash-map.h"
75 #include "plugin-api.h"
76 #include "ipa-ref.h"
77 #include "cgraph.h"
78 #include "except.h"
79 #include "dbgcnt.h"
80 #include "rtl-iter.h"
81 #include "tree-chkp.h"
82 #include "rtl-chkp.h"
84 /* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits. */
85 #define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
87 /* Data structure and subroutines used within expand_call. */
89 struct arg_data
91 /* Tree node for this argument. */
92 tree tree_value;
93 /* Mode for value; TYPE_MODE unless promoted. */
94 machine_mode mode;
95 /* Current RTL value for argument, or 0 if it isn't precomputed. */
96 rtx value;
97 /* Initially-compute RTL value for argument; only for const functions. */
98 rtx initial_value;
99 /* Register to pass this argument in, 0 if passed on stack, or an
100 PARALLEL if the arg is to be copied into multiple non-contiguous
101 registers. */
102 rtx reg;
103 /* Register to pass this argument in when generating tail call sequence.
104 This is not the same register as for normal calls on machines with
105 register windows. */
106 rtx tail_call_reg;
107 /* If REG is a PARALLEL, this is a copy of VALUE pulled into the correct
108 form for emit_group_move. */
109 rtx parallel_value;
110 /* If value is passed in neither reg nor stack, this field holds a number
111 of a special slot to be used. */
112 rtx special_slot;
113 /* For pointer bounds hold an index of parm bounds are bound to. -1 if
114 there is no such pointer. */
115 int pointer_arg;
116 /* If pointer_arg refers a structure, then pointer_offset holds an offset
117 of a pointer in this structure. */
118 int pointer_offset;
119 /* If REG was promoted from the actual mode of the argument expression,
120 indicates whether the promotion is sign- or zero-extended. */
121 int unsignedp;
122 /* Number of bytes to put in registers. 0 means put the whole arg
123 in registers. Also 0 if not passed in registers. */
124 int partial;
125 /* Nonzero if argument must be passed on stack.
126 Note that some arguments may be passed on the stack
127 even though pass_on_stack is zero, just because FUNCTION_ARG says so.
128 pass_on_stack identifies arguments that *cannot* go in registers. */
129 int pass_on_stack;
130 /* Some fields packaged up for locate_and_pad_parm. */
131 struct locate_and_pad_arg_data locate;
132 /* Location on the stack at which parameter should be stored. The store
133 has already been done if STACK == VALUE. */
134 rtx stack;
135 /* Location on the stack of the start of this argument slot. This can
136 differ from STACK if this arg pads downward. This location is known
137 to be aligned to TARGET_FUNCTION_ARG_BOUNDARY. */
138 rtx stack_slot;
139 /* Place that this stack area has been saved, if needed. */
140 rtx save_area;
141 /* If an argument's alignment does not permit direct copying into registers,
142 copy in smaller-sized pieces into pseudos. These are stored in a
143 block pointed to by this field. The next field says how many
144 word-sized pseudos we made. */
145 rtx *aligned_regs;
146 int n_aligned_regs;
149 /* A vector of one char per byte of stack space. A byte if nonzero if
150 the corresponding stack location has been used.
151 This vector is used to prevent a function call within an argument from
152 clobbering any stack already set up. */
153 static char *stack_usage_map;
155 /* Size of STACK_USAGE_MAP. */
156 static int highest_outgoing_arg_in_use;
158 /* A bitmap of virtual-incoming stack space. Bit is set if the corresponding
159 stack location's tail call argument has been already stored into the stack.
160 This bitmap is used to prevent sibling call optimization if function tries
161 to use parent's incoming argument slots when they have been already
162 overwritten with tail call arguments. */
163 static sbitmap stored_args_map;
165 /* stack_arg_under_construction is nonzero when an argument may be
166 initialized with a constructor call (including a C function that
167 returns a BLKmode struct) and expand_call must take special action
168 to make sure the object being constructed does not overlap the
169 argument list for the constructor call. */
170 static int stack_arg_under_construction;
172 static void emit_call_1 (rtx, tree, tree, tree, HOST_WIDE_INT, HOST_WIDE_INT,
173 HOST_WIDE_INT, rtx, rtx, int, rtx, int,
174 cumulative_args_t);
175 static void precompute_register_parameters (int, struct arg_data *, int *);
176 static void store_bounds (struct arg_data *, struct arg_data *);
177 static int store_one_arg (struct arg_data *, rtx, int, int, int);
178 static void store_unaligned_arguments_into_pseudos (struct arg_data *, int);
179 static int finalize_must_preallocate (int, int, struct arg_data *,
180 struct args_size *);
181 static void precompute_arguments (int, struct arg_data *);
182 static int compute_argument_block_size (int, struct args_size *, tree, tree, int);
183 static void initialize_argument_information (int, struct arg_data *,
184 struct args_size *, int,
185 tree, tree,
186 tree, tree, cumulative_args_t, int,
187 rtx *, int *, int *, int *,
188 bool *, bool);
189 static void compute_argument_addresses (struct arg_data *, rtx, int);
190 static rtx rtx_for_function_call (tree, tree);
191 static void load_register_parameters (struct arg_data *, int, rtx *, int,
192 int, int *);
193 static rtx emit_library_call_value_1 (int, rtx, rtx, enum libcall_type,
194 machine_mode, int, va_list);
195 static int special_function_p (const_tree, int);
196 static int check_sibcall_argument_overlap_1 (rtx);
197 static int check_sibcall_argument_overlap (rtx_insn *, struct arg_data *, int);
199 static int combine_pending_stack_adjustment_and_call (int, struct args_size *,
200 unsigned int);
201 static tree split_complex_types (tree);
203 #ifdef REG_PARM_STACK_SPACE
204 static rtx save_fixed_argument_area (int, rtx, int *, int *);
205 static void restore_fixed_argument_area (rtx, rtx, int, int);
206 #endif
208 /* Force FUNEXP into a form suitable for the address of a CALL,
209 and return that as an rtx. Also load the static chain register
210 if FNDECL is a nested function.
212 CALL_FUSAGE points to a variable holding the prospective
213 CALL_INSN_FUNCTION_USAGE information. */
216 prepare_call_address (tree fndecl_or_type, rtx funexp, rtx static_chain_value,
217 rtx *call_fusage, int reg_parm_seen, int sibcallp)
219 /* Make a valid memory address and copy constants through pseudo-regs,
220 but not for a constant address if -fno-function-cse. */
221 if (GET_CODE (funexp) != SYMBOL_REF)
222 /* If we are using registers for parameters, force the
223 function address into a register now. */
224 funexp = ((reg_parm_seen
225 && targetm.small_register_classes_for_mode_p (FUNCTION_MODE))
226 ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
227 : memory_address (FUNCTION_MODE, funexp));
228 else if (flag_pic && !flag_plt && fndecl_or_type
229 && TREE_CODE (fndecl_or_type) == FUNCTION_DECL
230 && !targetm.binds_local_p (fndecl_or_type))
232 funexp = force_reg (Pmode, funexp);
234 else if (! sibcallp)
236 if (!NO_FUNCTION_CSE && optimize && ! flag_no_function_cse)
237 funexp = force_reg (Pmode, funexp);
240 if (static_chain_value != 0
241 && (TREE_CODE (fndecl_or_type) != FUNCTION_DECL
242 || DECL_STATIC_CHAIN (fndecl_or_type)))
244 rtx chain;
246 chain = targetm.calls.static_chain (fndecl_or_type, false);
247 static_chain_value = convert_memory_address (Pmode, static_chain_value);
249 emit_move_insn (chain, static_chain_value);
250 if (REG_P (chain))
251 use_reg (call_fusage, chain);
254 return funexp;
257 /* Generate instructions to call function FUNEXP,
258 and optionally pop the results.
259 The CALL_INSN is the first insn generated.
261 FNDECL is the declaration node of the function. This is given to the
262 hook TARGET_RETURN_POPS_ARGS to determine whether this function pops
263 its own args.
265 FUNTYPE is the data type of the function. This is given to the hook
266 TARGET_RETURN_POPS_ARGS to determine whether this function pops its
267 own args. We used to allow an identifier for library functions, but
268 that doesn't work when the return type is an aggregate type and the
269 calling convention says that the pointer to this aggregate is to be
270 popped by the callee.
272 STACK_SIZE is the number of bytes of arguments on the stack,
273 ROUNDED_STACK_SIZE is that number rounded up to
274 PREFERRED_STACK_BOUNDARY; zero if the size is variable. This is
275 both to put into the call insn and to generate explicit popping
276 code if necessary.
278 STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
279 It is zero if this call doesn't want a structure value.
281 NEXT_ARG_REG is the rtx that results from executing
282 targetm.calls.function_arg (&args_so_far, VOIDmode, void_type_node, true)
283 just after all the args have had their registers assigned.
284 This could be whatever you like, but normally it is the first
285 arg-register beyond those used for args in this call,
286 or 0 if all the arg-registers are used in this call.
287 It is passed on to `gen_call' so you can put this info in the call insn.
289 VALREG is a hard register in which a value is returned,
290 or 0 if the call does not return a value.
292 OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
293 the args to this call were processed.
294 We restore `inhibit_defer_pop' to that value.
296 CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
297 denote registers used by the called function. */
299 static void
300 emit_call_1 (rtx funexp, tree fntree ATTRIBUTE_UNUSED, tree fndecl ATTRIBUTE_UNUSED,
301 tree funtype ATTRIBUTE_UNUSED,
302 HOST_WIDE_INT stack_size ATTRIBUTE_UNUSED,
303 HOST_WIDE_INT rounded_stack_size,
304 HOST_WIDE_INT struct_value_size ATTRIBUTE_UNUSED,
305 rtx next_arg_reg ATTRIBUTE_UNUSED, rtx valreg,
306 int old_inhibit_defer_pop, rtx call_fusage, int ecf_flags,
307 cumulative_args_t args_so_far ATTRIBUTE_UNUSED)
309 rtx rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
310 rtx_insn *call_insn;
311 rtx call, funmem;
312 int already_popped = 0;
313 HOST_WIDE_INT n_popped
314 = targetm.calls.return_pops_args (fndecl, funtype, stack_size);
316 #ifdef CALL_POPS_ARGS
317 n_popped += CALL_POPS_ARGS (*get_cumulative_args (args_so_far));
318 #endif
320 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
321 and we don't want to load it into a register as an optimization,
322 because prepare_call_address already did it if it should be done. */
323 if (GET_CODE (funexp) != SYMBOL_REF)
324 funexp = memory_address (FUNCTION_MODE, funexp);
326 funmem = gen_rtx_MEM (FUNCTION_MODE, funexp);
327 if (fndecl && TREE_CODE (fndecl) == FUNCTION_DECL)
329 tree t = fndecl;
331 /* Although a built-in FUNCTION_DECL and its non-__builtin
332 counterpart compare equal and get a shared mem_attrs, they
333 produce different dump output in compare-debug compilations,
334 if an entry gets garbage collected in one compilation, then
335 adds a different (but equivalent) entry, while the other
336 doesn't run the garbage collector at the same spot and then
337 shares the mem_attr with the equivalent entry. */
338 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
340 tree t2 = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
341 if (t2)
342 t = t2;
345 set_mem_expr (funmem, t);
347 else if (fntree)
348 set_mem_expr (funmem, build_simple_mem_ref (CALL_EXPR_FN (fntree)));
350 #if defined (HAVE_sibcall_pop) && defined (HAVE_sibcall_value_pop)
351 if ((ecf_flags & ECF_SIBCALL)
352 && HAVE_sibcall_pop && HAVE_sibcall_value_pop
353 && (n_popped > 0 || stack_size == 0))
355 rtx n_pop = GEN_INT (n_popped);
356 rtx pat;
358 /* If this subroutine pops its own args, record that in the call insn
359 if possible, for the sake of frame pointer elimination. */
361 if (valreg)
362 pat = GEN_SIBCALL_VALUE_POP (valreg, funmem, rounded_stack_size_rtx,
363 next_arg_reg, n_pop);
364 else
365 pat = GEN_SIBCALL_POP (funmem, rounded_stack_size_rtx, next_arg_reg,
366 n_pop);
368 emit_call_insn (pat);
369 already_popped = 1;
371 else
372 #endif
374 #if defined (HAVE_call_pop) && defined (HAVE_call_value_pop)
375 /* If the target has "call" or "call_value" insns, then prefer them
376 if no arguments are actually popped. If the target does not have
377 "call" or "call_value" insns, then we must use the popping versions
378 even if the call has no arguments to pop. */
379 #if defined (HAVE_call) && defined (HAVE_call_value)
380 if (HAVE_call && HAVE_call_value && HAVE_call_pop && HAVE_call_value_pop
381 && n_popped > 0)
382 #else
383 if (HAVE_call_pop && HAVE_call_value_pop)
384 #endif
386 rtx n_pop = GEN_INT (n_popped);
387 rtx pat;
389 /* If this subroutine pops its own args, record that in the call insn
390 if possible, for the sake of frame pointer elimination. */
392 if (valreg)
393 pat = GEN_CALL_VALUE_POP (valreg, funmem, rounded_stack_size_rtx,
394 next_arg_reg, n_pop);
395 else
396 pat = GEN_CALL_POP (funmem, rounded_stack_size_rtx, next_arg_reg,
397 n_pop);
399 emit_call_insn (pat);
400 already_popped = 1;
402 else
403 #endif
405 #if defined (HAVE_sibcall) && defined (HAVE_sibcall_value)
406 if ((ecf_flags & ECF_SIBCALL)
407 && HAVE_sibcall && HAVE_sibcall_value)
409 if (valreg)
410 emit_call_insn (GEN_SIBCALL_VALUE (valreg, funmem,
411 rounded_stack_size_rtx,
412 next_arg_reg, NULL_RTX));
413 else
414 emit_call_insn (GEN_SIBCALL (funmem, rounded_stack_size_rtx,
415 next_arg_reg,
416 GEN_INT (struct_value_size)));
418 else
419 #endif
421 #if defined (HAVE_call) && defined (HAVE_call_value)
422 if (HAVE_call && HAVE_call_value)
424 if (valreg)
425 emit_call_insn (GEN_CALL_VALUE (valreg, funmem, rounded_stack_size_rtx,
426 next_arg_reg, NULL_RTX));
427 else
428 emit_call_insn (GEN_CALL (funmem, rounded_stack_size_rtx, next_arg_reg,
429 GEN_INT (struct_value_size)));
431 else
432 #endif
433 gcc_unreachable ();
435 /* Find the call we just emitted. */
436 call_insn = last_call_insn ();
438 /* Some target create a fresh MEM instead of reusing the one provided
439 above. Set its MEM_EXPR. */
440 call = get_call_rtx_from (call_insn);
441 if (call
442 && MEM_EXPR (XEXP (call, 0)) == NULL_TREE
443 && MEM_EXPR (funmem) != NULL_TREE)
444 set_mem_expr (XEXP (call, 0), MEM_EXPR (funmem));
446 /* Mark instrumented calls. */
447 if (call && fntree)
448 CALL_EXPR_WITH_BOUNDS_P (call) = CALL_WITH_BOUNDS_P (fntree);
450 /* Put the register usage information there. */
451 add_function_usage_to (call_insn, call_fusage);
453 /* If this is a const call, then set the insn's unchanging bit. */
454 if (ecf_flags & ECF_CONST)
455 RTL_CONST_CALL_P (call_insn) = 1;
457 /* If this is a pure call, then set the insn's unchanging bit. */
458 if (ecf_flags & ECF_PURE)
459 RTL_PURE_CALL_P (call_insn) = 1;
461 /* If this is a const call, then set the insn's unchanging bit. */
462 if (ecf_flags & ECF_LOOPING_CONST_OR_PURE)
463 RTL_LOOPING_CONST_OR_PURE_CALL_P (call_insn) = 1;
465 /* Create a nothrow REG_EH_REGION note, if needed. */
466 make_reg_eh_region_note (call_insn, ecf_flags, 0);
468 if (ecf_flags & ECF_NORETURN)
469 add_reg_note (call_insn, REG_NORETURN, const0_rtx);
471 if (ecf_flags & ECF_RETURNS_TWICE)
473 add_reg_note (call_insn, REG_SETJMP, const0_rtx);
474 cfun->calls_setjmp = 1;
477 SIBLING_CALL_P (call_insn) = ((ecf_flags & ECF_SIBCALL) != 0);
479 /* Restore this now, so that we do defer pops for this call's args
480 if the context of the call as a whole permits. */
481 inhibit_defer_pop = old_inhibit_defer_pop;
483 if (n_popped > 0)
485 if (!already_popped)
486 CALL_INSN_FUNCTION_USAGE (call_insn)
487 = gen_rtx_EXPR_LIST (VOIDmode,
488 gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx),
489 CALL_INSN_FUNCTION_USAGE (call_insn));
490 rounded_stack_size -= n_popped;
491 rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
492 stack_pointer_delta -= n_popped;
494 add_reg_note (call_insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
496 /* If popup is needed, stack realign must use DRAP */
497 if (SUPPORTS_STACK_ALIGNMENT)
498 crtl->need_drap = true;
500 /* For noreturn calls when not accumulating outgoing args force
501 REG_ARGS_SIZE note to prevent crossjumping of calls with different
502 args sizes. */
503 else if (!ACCUMULATE_OUTGOING_ARGS && (ecf_flags & ECF_NORETURN) != 0)
504 add_reg_note (call_insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
506 if (!ACCUMULATE_OUTGOING_ARGS)
508 /* If returning from the subroutine does not automatically pop the args,
509 we need an instruction to pop them sooner or later.
510 Perhaps do it now; perhaps just record how much space to pop later.
512 If returning from the subroutine does pop the args, indicate that the
513 stack pointer will be changed. */
515 if (rounded_stack_size != 0)
517 if (ecf_flags & ECF_NORETURN)
518 /* Just pretend we did the pop. */
519 stack_pointer_delta -= rounded_stack_size;
520 else if (flag_defer_pop && inhibit_defer_pop == 0
521 && ! (ecf_flags & (ECF_CONST | ECF_PURE)))
522 pending_stack_adjust += rounded_stack_size;
523 else
524 adjust_stack (rounded_stack_size_rtx);
527 /* When we accumulate outgoing args, we must avoid any stack manipulations.
528 Restore the stack pointer to its original value now. Usually
529 ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
530 On i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
531 popping variants of functions exist as well.
533 ??? We may optimize similar to defer_pop above, but it is
534 probably not worthwhile.
536 ??? It will be worthwhile to enable combine_stack_adjustments even for
537 such machines. */
538 else if (n_popped)
539 anti_adjust_stack (GEN_INT (n_popped));
542 /* Determine if the function identified by NAME and FNDECL is one with
543 special properties we wish to know about.
545 For example, if the function might return more than one time (setjmp), then
546 set RETURNS_TWICE to a nonzero value.
548 Similarly set NORETURN if the function is in the longjmp family.
550 Set MAY_BE_ALLOCA for any memory allocation function that might allocate
551 space from the stack such as alloca. */
553 static int
554 special_function_p (const_tree fndecl, int flags)
556 tree name_decl = DECL_NAME (fndecl);
558 /* For instrumentation clones we want to derive flags
559 from the original name. */
560 if (cgraph_node::get (fndecl)
561 && cgraph_node::get (fndecl)->instrumentation_clone)
562 name_decl = DECL_NAME (cgraph_node::get (fndecl)->orig_decl);
564 if (fndecl && name_decl
565 && IDENTIFIER_LENGTH (name_decl) <= 17
566 /* Exclude functions not at the file scope, or not `extern',
567 since they are not the magic functions we would otherwise
568 think they are.
569 FIXME: this should be handled with attributes, not with this
570 hacky imitation of DECL_ASSEMBLER_NAME. It's (also) wrong
571 because you can declare fork() inside a function if you
572 wish. */
573 && (DECL_CONTEXT (fndecl) == NULL_TREE
574 || TREE_CODE (DECL_CONTEXT (fndecl)) == TRANSLATION_UNIT_DECL)
575 && TREE_PUBLIC (fndecl))
577 const char *name = IDENTIFIER_POINTER (name_decl);
578 const char *tname = name;
580 /* We assume that alloca will always be called by name. It
581 makes no sense to pass it as a pointer-to-function to
582 anything that does not understand its behavior. */
583 if (((IDENTIFIER_LENGTH (name_decl) == 6
584 && name[0] == 'a'
585 && ! strcmp (name, "alloca"))
586 || (IDENTIFIER_LENGTH (name_decl) == 16
587 && name[0] == '_'
588 && ! strcmp (name, "__builtin_alloca"))))
589 flags |= ECF_MAY_BE_ALLOCA;
591 /* Disregard prefix _, __, __x or __builtin_. */
592 if (name[0] == '_')
594 if (name[1] == '_'
595 && name[2] == 'b'
596 && !strncmp (name + 3, "uiltin_", 7))
597 tname += 10;
598 else if (name[1] == '_' && name[2] == 'x')
599 tname += 3;
600 else if (name[1] == '_')
601 tname += 2;
602 else
603 tname += 1;
606 if (tname[0] == 's')
608 if ((tname[1] == 'e'
609 && (! strcmp (tname, "setjmp")
610 || ! strcmp (tname, "setjmp_syscall")))
611 || (tname[1] == 'i'
612 && ! strcmp (tname, "sigsetjmp"))
613 || (tname[1] == 'a'
614 && ! strcmp (tname, "savectx")))
615 flags |= ECF_RETURNS_TWICE | ECF_LEAF;
617 if (tname[1] == 'i'
618 && ! strcmp (tname, "siglongjmp"))
619 flags |= ECF_NORETURN;
621 else if ((tname[0] == 'q' && tname[1] == 's'
622 && ! strcmp (tname, "qsetjmp"))
623 || (tname[0] == 'v' && tname[1] == 'f'
624 && ! strcmp (tname, "vfork"))
625 || (tname[0] == 'g' && tname[1] == 'e'
626 && !strcmp (tname, "getcontext")))
627 flags |= ECF_RETURNS_TWICE | ECF_LEAF;
629 else if (tname[0] == 'l' && tname[1] == 'o'
630 && ! strcmp (tname, "longjmp"))
631 flags |= ECF_NORETURN;
634 return flags;
637 /* Similar to special_function_p; return a set of ERF_ flags for the
638 function FNDECL. */
639 static int
640 decl_return_flags (tree fndecl)
642 tree attr;
643 tree type = TREE_TYPE (fndecl);
644 if (!type)
645 return 0;
647 attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
648 if (!attr)
649 return 0;
651 attr = TREE_VALUE (TREE_VALUE (attr));
652 if (!attr || TREE_STRING_LENGTH (attr) < 1)
653 return 0;
655 switch (TREE_STRING_POINTER (attr)[0])
657 case '1':
658 case '2':
659 case '3':
660 case '4':
661 return ERF_RETURNS_ARG | (TREE_STRING_POINTER (attr)[0] - '1');
663 case 'm':
664 return ERF_NOALIAS;
666 case '.':
667 default:
668 return 0;
672 /* Return nonzero when FNDECL represents a call to setjmp. */
675 setjmp_call_p (const_tree fndecl)
677 if (DECL_IS_RETURNS_TWICE (fndecl))
678 return ECF_RETURNS_TWICE;
679 return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
683 /* Return true if STMT is an alloca call. */
685 bool
686 gimple_alloca_call_p (const_gimple stmt)
688 tree fndecl;
690 if (!is_gimple_call (stmt))
691 return false;
693 fndecl = gimple_call_fndecl (stmt);
694 if (fndecl && (special_function_p (fndecl, 0) & ECF_MAY_BE_ALLOCA))
695 return true;
697 return false;
700 /* Return true when exp contains alloca call. */
702 bool
703 alloca_call_p (const_tree exp)
705 tree fndecl;
706 if (TREE_CODE (exp) == CALL_EXPR
707 && (fndecl = get_callee_fndecl (exp))
708 && (special_function_p (fndecl, 0) & ECF_MAY_BE_ALLOCA))
709 return true;
710 return false;
713 /* Return TRUE if FNDECL is either a TM builtin or a TM cloned
714 function. Return FALSE otherwise. */
716 static bool
717 is_tm_builtin (const_tree fndecl)
719 if (fndecl == NULL)
720 return false;
722 if (decl_is_tm_clone (fndecl))
723 return true;
725 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
727 switch (DECL_FUNCTION_CODE (fndecl))
729 case BUILT_IN_TM_COMMIT:
730 case BUILT_IN_TM_COMMIT_EH:
731 case BUILT_IN_TM_ABORT:
732 case BUILT_IN_TM_IRREVOCABLE:
733 case BUILT_IN_TM_GETTMCLONE_IRR:
734 case BUILT_IN_TM_MEMCPY:
735 case BUILT_IN_TM_MEMMOVE:
736 case BUILT_IN_TM_MEMSET:
737 CASE_BUILT_IN_TM_STORE (1):
738 CASE_BUILT_IN_TM_STORE (2):
739 CASE_BUILT_IN_TM_STORE (4):
740 CASE_BUILT_IN_TM_STORE (8):
741 CASE_BUILT_IN_TM_STORE (FLOAT):
742 CASE_BUILT_IN_TM_STORE (DOUBLE):
743 CASE_BUILT_IN_TM_STORE (LDOUBLE):
744 CASE_BUILT_IN_TM_STORE (M64):
745 CASE_BUILT_IN_TM_STORE (M128):
746 CASE_BUILT_IN_TM_STORE (M256):
747 CASE_BUILT_IN_TM_LOAD (1):
748 CASE_BUILT_IN_TM_LOAD (2):
749 CASE_BUILT_IN_TM_LOAD (4):
750 CASE_BUILT_IN_TM_LOAD (8):
751 CASE_BUILT_IN_TM_LOAD (FLOAT):
752 CASE_BUILT_IN_TM_LOAD (DOUBLE):
753 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
754 CASE_BUILT_IN_TM_LOAD (M64):
755 CASE_BUILT_IN_TM_LOAD (M128):
756 CASE_BUILT_IN_TM_LOAD (M256):
757 case BUILT_IN_TM_LOG:
758 case BUILT_IN_TM_LOG_1:
759 case BUILT_IN_TM_LOG_2:
760 case BUILT_IN_TM_LOG_4:
761 case BUILT_IN_TM_LOG_8:
762 case BUILT_IN_TM_LOG_FLOAT:
763 case BUILT_IN_TM_LOG_DOUBLE:
764 case BUILT_IN_TM_LOG_LDOUBLE:
765 case BUILT_IN_TM_LOG_M64:
766 case BUILT_IN_TM_LOG_M128:
767 case BUILT_IN_TM_LOG_M256:
768 return true;
769 default:
770 break;
773 return false;
776 /* Detect flags (function attributes) from the function decl or type node. */
779 flags_from_decl_or_type (const_tree exp)
781 int flags = 0;
783 if (DECL_P (exp))
785 /* The function exp may have the `malloc' attribute. */
786 if (DECL_IS_MALLOC (exp))
787 flags |= ECF_MALLOC;
789 /* The function exp may have the `returns_twice' attribute. */
790 if (DECL_IS_RETURNS_TWICE (exp))
791 flags |= ECF_RETURNS_TWICE;
793 /* Process the pure and const attributes. */
794 if (TREE_READONLY (exp))
795 flags |= ECF_CONST;
796 if (DECL_PURE_P (exp))
797 flags |= ECF_PURE;
798 if (DECL_LOOPING_CONST_OR_PURE_P (exp))
799 flags |= ECF_LOOPING_CONST_OR_PURE;
801 if (DECL_IS_NOVOPS (exp))
802 flags |= ECF_NOVOPS;
803 if (lookup_attribute ("leaf", DECL_ATTRIBUTES (exp)))
804 flags |= ECF_LEAF;
806 if (TREE_NOTHROW (exp))
807 flags |= ECF_NOTHROW;
809 if (flag_tm)
811 if (is_tm_builtin (exp))
812 flags |= ECF_TM_BUILTIN;
813 else if ((flags & (ECF_CONST|ECF_NOVOPS)) != 0
814 || lookup_attribute ("transaction_pure",
815 TYPE_ATTRIBUTES (TREE_TYPE (exp))))
816 flags |= ECF_TM_PURE;
819 flags = special_function_p (exp, flags);
821 else if (TYPE_P (exp))
823 if (TYPE_READONLY (exp))
824 flags |= ECF_CONST;
826 if (flag_tm
827 && ((flags & ECF_CONST) != 0
828 || lookup_attribute ("transaction_pure", TYPE_ATTRIBUTES (exp))))
829 flags |= ECF_TM_PURE;
831 else
832 gcc_unreachable ();
834 if (TREE_THIS_VOLATILE (exp))
836 flags |= ECF_NORETURN;
837 if (flags & (ECF_CONST|ECF_PURE))
838 flags |= ECF_LOOPING_CONST_OR_PURE;
841 return flags;
844 /* Detect flags from a CALL_EXPR. */
847 call_expr_flags (const_tree t)
849 int flags;
850 tree decl = get_callee_fndecl (t);
852 if (decl)
853 flags = flags_from_decl_or_type (decl);
854 else if (CALL_EXPR_FN (t) == NULL_TREE)
855 flags = internal_fn_flags (CALL_EXPR_IFN (t));
856 else
858 t = TREE_TYPE (CALL_EXPR_FN (t));
859 if (t && TREE_CODE (t) == POINTER_TYPE)
860 flags = flags_from_decl_or_type (TREE_TYPE (t));
861 else
862 flags = 0;
865 return flags;
868 /* Precompute all register parameters as described by ARGS, storing values
869 into fields within the ARGS array.
871 NUM_ACTUALS indicates the total number elements in the ARGS array.
873 Set REG_PARM_SEEN if we encounter a register parameter. */
875 static void
876 precompute_register_parameters (int num_actuals, struct arg_data *args,
877 int *reg_parm_seen)
879 int i;
881 *reg_parm_seen = 0;
883 for (i = 0; i < num_actuals; i++)
884 if (args[i].reg != 0 && ! args[i].pass_on_stack)
886 *reg_parm_seen = 1;
888 if (args[i].value == 0)
890 push_temp_slots ();
891 args[i].value = expand_normal (args[i].tree_value);
892 preserve_temp_slots (args[i].value);
893 pop_temp_slots ();
896 /* If we are to promote the function arg to a wider mode,
897 do it now. */
899 if (args[i].mode != TYPE_MODE (TREE_TYPE (args[i].tree_value)))
900 args[i].value
901 = convert_modes (args[i].mode,
902 TYPE_MODE (TREE_TYPE (args[i].tree_value)),
903 args[i].value, args[i].unsignedp);
905 /* If the value is a non-legitimate constant, force it into a
906 pseudo now. TLS symbols sometimes need a call to resolve. */
907 if (CONSTANT_P (args[i].value)
908 && !targetm.legitimate_constant_p (args[i].mode, args[i].value))
909 args[i].value = force_reg (args[i].mode, args[i].value);
911 /* If we're going to have to load the value by parts, pull the
912 parts into pseudos. The part extraction process can involve
913 non-trivial computation. */
914 if (GET_CODE (args[i].reg) == PARALLEL)
916 tree type = TREE_TYPE (args[i].tree_value);
917 args[i].parallel_value
918 = emit_group_load_into_temps (args[i].reg, args[i].value,
919 type, int_size_in_bytes (type));
922 /* If the value is expensive, and we are inside an appropriately
923 short loop, put the value into a pseudo and then put the pseudo
924 into the hard reg.
926 For small register classes, also do this if this call uses
927 register parameters. This is to avoid reload conflicts while
928 loading the parameters registers. */
930 else if ((! (REG_P (args[i].value)
931 || (GET_CODE (args[i].value) == SUBREG
932 && REG_P (SUBREG_REG (args[i].value)))))
933 && args[i].mode != BLKmode
934 && set_src_cost (args[i].value, optimize_insn_for_speed_p ())
935 > COSTS_N_INSNS (1)
936 && ((*reg_parm_seen
937 && targetm.small_register_classes_for_mode_p (args[i].mode))
938 || optimize))
939 args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
943 #ifdef REG_PARM_STACK_SPACE
945 /* The argument list is the property of the called routine and it
946 may clobber it. If the fixed area has been used for previous
947 parameters, we must save and restore it. */
949 static rtx
950 save_fixed_argument_area (int reg_parm_stack_space, rtx argblock, int *low_to_save, int *high_to_save)
952 int low;
953 int high;
955 /* Compute the boundary of the area that needs to be saved, if any. */
956 high = reg_parm_stack_space;
957 if (ARGS_GROW_DOWNWARD)
958 high += 1;
960 if (high > highest_outgoing_arg_in_use)
961 high = highest_outgoing_arg_in_use;
963 for (low = 0; low < high; low++)
964 if (stack_usage_map[low] != 0)
966 int num_to_save;
967 machine_mode save_mode;
968 int delta;
969 rtx addr;
970 rtx stack_area;
971 rtx save_area;
973 while (stack_usage_map[--high] == 0)
976 *low_to_save = low;
977 *high_to_save = high;
979 num_to_save = high - low + 1;
980 save_mode = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
982 /* If we don't have the required alignment, must do this
983 in BLKmode. */
984 if ((low & (MIN (GET_MODE_SIZE (save_mode),
985 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
986 save_mode = BLKmode;
988 if (ARGS_GROW_DOWNWARD)
989 delta = -high;
990 else
991 delta = low;
993 addr = plus_constant (Pmode, argblock, delta);
994 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
996 set_mem_align (stack_area, PARM_BOUNDARY);
997 if (save_mode == BLKmode)
999 save_area = assign_stack_temp (BLKmode, num_to_save);
1000 emit_block_move (validize_mem (save_area), stack_area,
1001 GEN_INT (num_to_save), BLOCK_OP_CALL_PARM);
1003 else
1005 save_area = gen_reg_rtx (save_mode);
1006 emit_move_insn (save_area, stack_area);
1009 return save_area;
1012 return NULL_RTX;
1015 static void
1016 restore_fixed_argument_area (rtx save_area, rtx argblock, int high_to_save, int low_to_save)
1018 machine_mode save_mode = GET_MODE (save_area);
1019 int delta;
1020 rtx addr, stack_area;
1022 if (ARGS_GROW_DOWNWARD)
1023 delta = -high_to_save;
1024 else
1025 delta = low_to_save;
1027 addr = plus_constant (Pmode, argblock, delta);
1028 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
1029 set_mem_align (stack_area, PARM_BOUNDARY);
1031 if (save_mode != BLKmode)
1032 emit_move_insn (stack_area, save_area);
1033 else
1034 emit_block_move (stack_area, validize_mem (save_area),
1035 GEN_INT (high_to_save - low_to_save + 1),
1036 BLOCK_OP_CALL_PARM);
1038 #endif /* REG_PARM_STACK_SPACE */
1040 /* If any elements in ARGS refer to parameters that are to be passed in
1041 registers, but not in memory, and whose alignment does not permit a
1042 direct copy into registers. Copy the values into a group of pseudos
1043 which we will later copy into the appropriate hard registers.
1045 Pseudos for each unaligned argument will be stored into the array
1046 args[argnum].aligned_regs. The caller is responsible for deallocating
1047 the aligned_regs array if it is nonzero. */
1049 static void
1050 store_unaligned_arguments_into_pseudos (struct arg_data *args, int num_actuals)
1052 int i, j;
1054 for (i = 0; i < num_actuals; i++)
1055 if (args[i].reg != 0 && ! args[i].pass_on_stack
1056 && GET_CODE (args[i].reg) != PARALLEL
1057 && args[i].mode == BLKmode
1058 && MEM_P (args[i].value)
1059 && (MEM_ALIGN (args[i].value)
1060 < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
1062 int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1063 int endian_correction = 0;
1065 if (args[i].partial)
1067 gcc_assert (args[i].partial % UNITS_PER_WORD == 0);
1068 args[i].n_aligned_regs = args[i].partial / UNITS_PER_WORD;
1070 else
1072 args[i].n_aligned_regs
1073 = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1076 args[i].aligned_regs = XNEWVEC (rtx, args[i].n_aligned_regs);
1078 /* Structures smaller than a word are normally aligned to the
1079 least significant byte. On a BYTES_BIG_ENDIAN machine,
1080 this means we must skip the empty high order bytes when
1081 calculating the bit offset. */
1082 if (bytes < UNITS_PER_WORD
1083 #ifdef BLOCK_REG_PADDING
1084 && (BLOCK_REG_PADDING (args[i].mode,
1085 TREE_TYPE (args[i].tree_value), 1)
1086 == downward)
1087 #else
1088 && BYTES_BIG_ENDIAN
1089 #endif
1091 endian_correction = BITS_PER_WORD - bytes * BITS_PER_UNIT;
1093 for (j = 0; j < args[i].n_aligned_regs; j++)
1095 rtx reg = gen_reg_rtx (word_mode);
1096 rtx word = operand_subword_force (args[i].value, j, BLKmode);
1097 int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
1099 args[i].aligned_regs[j] = reg;
1100 word = extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
1101 word_mode, word_mode);
1103 /* There is no need to restrict this code to loading items
1104 in TYPE_ALIGN sized hunks. The bitfield instructions can
1105 load up entire word sized registers efficiently.
1107 ??? This may not be needed anymore.
1108 We use to emit a clobber here but that doesn't let later
1109 passes optimize the instructions we emit. By storing 0 into
1110 the register later passes know the first AND to zero out the
1111 bitfield being set in the register is unnecessary. The store
1112 of 0 will be deleted as will at least the first AND. */
1114 emit_move_insn (reg, const0_rtx);
1116 bytes -= bitsize / BITS_PER_UNIT;
1117 store_bit_field (reg, bitsize, endian_correction, 0, 0,
1118 word_mode, word);
1123 /* Fill in ARGS_SIZE and ARGS array based on the parameters found in
1124 CALL_EXPR EXP.
1126 NUM_ACTUALS is the total number of parameters.
1128 N_NAMED_ARGS is the total number of named arguments.
1130 STRUCT_VALUE_ADDR_VALUE is the implicit argument for a struct return
1131 value, or null.
1133 FNDECL is the tree code for the target of this call (if known)
1135 ARGS_SO_FAR holds state needed by the target to know where to place
1136 the next argument.
1138 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
1139 for arguments which are passed in registers.
1141 OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
1142 and may be modified by this routine.
1144 OLD_PENDING_ADJ, MUST_PREALLOCATE and FLAGS are pointers to integer
1145 flags which may may be modified by this routine.
1147 MAY_TAILCALL is cleared if we encounter an invisible pass-by-reference
1148 that requires allocation of stack space.
1150 CALL_FROM_THUNK_P is true if this call is the jump from a thunk to
1151 the thunked-to function. */
1153 static void
1154 initialize_argument_information (int num_actuals ATTRIBUTE_UNUSED,
1155 struct arg_data *args,
1156 struct args_size *args_size,
1157 int n_named_args ATTRIBUTE_UNUSED,
1158 tree exp, tree struct_value_addr_value,
1159 tree fndecl, tree fntype,
1160 cumulative_args_t args_so_far,
1161 int reg_parm_stack_space,
1162 rtx *old_stack_level, int *old_pending_adj,
1163 int *must_preallocate, int *ecf_flags,
1164 bool *may_tailcall, bool call_from_thunk_p)
1166 CUMULATIVE_ARGS *args_so_far_pnt = get_cumulative_args (args_so_far);
1167 location_t loc = EXPR_LOCATION (exp);
1169 /* Count arg position in order args appear. */
1170 int argpos;
1172 int i;
1174 args_size->constant = 0;
1175 args_size->var = 0;
1177 bitmap_obstack_initialize (NULL);
1179 /* In this loop, we consider args in the order they are written.
1180 We fill up ARGS from the back. */
1182 i = num_actuals - 1;
1184 int j = i, ptr_arg = -1;
1185 call_expr_arg_iterator iter;
1186 tree arg;
1187 bitmap slots = NULL;
1189 if (struct_value_addr_value)
1191 args[j].tree_value = struct_value_addr_value;
1192 j--;
1194 /* If we pass structure address then we need to
1195 create bounds for it. Since created bounds is
1196 a call statement, we expand it right here to avoid
1197 fixing all other places where it may be expanded. */
1198 if (CALL_WITH_BOUNDS_P (exp))
1200 args[j].value = gen_reg_rtx (targetm.chkp_bound_mode ());
1201 args[j].tree_value
1202 = chkp_make_bounds_for_struct_addr (struct_value_addr_value);
1203 expand_expr_real (args[j].tree_value, args[j].value, VOIDmode,
1204 EXPAND_NORMAL, 0, false);
1205 args[j].pointer_arg = j + 1;
1206 j--;
1209 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
1211 tree argtype = TREE_TYPE (arg);
1213 /* Remember last param with pointer and associate it
1214 with following pointer bounds. */
1215 if (CALL_WITH_BOUNDS_P (exp)
1216 && chkp_type_has_pointer (argtype))
1218 if (slots)
1219 BITMAP_FREE (slots);
1220 ptr_arg = j;
1221 if (!BOUNDED_TYPE_P (argtype))
1223 slots = BITMAP_ALLOC (NULL);
1224 chkp_find_bound_slots (argtype, slots);
1227 else if (POINTER_BOUNDS_TYPE_P (argtype))
1229 /* We expect bounds in instrumented calls only.
1230 Otherwise it is a sign we lost flag due to some optimization
1231 and may emit call args incorrectly. */
1232 gcc_assert (CALL_WITH_BOUNDS_P (exp));
1234 /* For structures look for the next available pointer. */
1235 if (ptr_arg != -1 && slots)
1237 unsigned bnd_no = bitmap_first_set_bit (slots);
1238 args[j].pointer_offset =
1239 bnd_no * POINTER_SIZE / BITS_PER_UNIT;
1241 bitmap_clear_bit (slots, bnd_no);
1243 /* Check we have no more pointers in the structure. */
1244 if (bitmap_empty_p (slots))
1245 BITMAP_FREE (slots);
1247 args[j].pointer_arg = ptr_arg;
1249 /* Check we covered all pointers in the previous
1250 non bounds arg. */
1251 if (!slots)
1252 ptr_arg = -1;
1254 else
1255 ptr_arg = -1;
1257 if (targetm.calls.split_complex_arg
1258 && argtype
1259 && TREE_CODE (argtype) == COMPLEX_TYPE
1260 && targetm.calls.split_complex_arg (argtype))
1262 tree subtype = TREE_TYPE (argtype);
1263 args[j].tree_value = build1 (REALPART_EXPR, subtype, arg);
1264 j--;
1265 args[j].tree_value = build1 (IMAGPART_EXPR, subtype, arg);
1267 else
1268 args[j].tree_value = arg;
1269 j--;
1272 if (slots)
1273 BITMAP_FREE (slots);
1276 bitmap_obstack_release (NULL);
1278 /* I counts args in order (to be) pushed; ARGPOS counts in order written. */
1279 for (argpos = 0; argpos < num_actuals; i--, argpos++)
1281 tree type = TREE_TYPE (args[i].tree_value);
1282 int unsignedp;
1283 machine_mode mode;
1285 /* Replace erroneous argument with constant zero. */
1286 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1287 args[i].tree_value = integer_zero_node, type = integer_type_node;
1289 /* If TYPE is a transparent union or record, pass things the way
1290 we would pass the first field of the union or record. We have
1291 already verified that the modes are the same. */
1292 if ((TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == RECORD_TYPE)
1293 && TYPE_TRANSPARENT_AGGR (type))
1294 type = TREE_TYPE (first_field (type));
1296 /* Decide where to pass this arg.
1298 args[i].reg is nonzero if all or part is passed in registers.
1300 args[i].partial is nonzero if part but not all is passed in registers,
1301 and the exact value says how many bytes are passed in registers.
1303 args[i].pass_on_stack is nonzero if the argument must at least be
1304 computed on the stack. It may then be loaded back into registers
1305 if args[i].reg is nonzero.
1307 These decisions are driven by the FUNCTION_... macros and must agree
1308 with those made by function.c. */
1310 /* See if this argument should be passed by invisible reference. */
1311 if (pass_by_reference (args_so_far_pnt, TYPE_MODE (type),
1312 type, argpos < n_named_args))
1314 bool callee_copies;
1315 tree base = NULL_TREE;
1317 callee_copies
1318 = reference_callee_copied (args_so_far_pnt, TYPE_MODE (type),
1319 type, argpos < n_named_args);
1321 /* If we're compiling a thunk, pass through invisible references
1322 instead of making a copy. */
1323 if (call_from_thunk_p
1324 || (callee_copies
1325 && !TREE_ADDRESSABLE (type)
1326 && (base = get_base_address (args[i].tree_value))
1327 && TREE_CODE (base) != SSA_NAME
1328 && (!DECL_P (base) || MEM_P (DECL_RTL (base)))))
1330 /* We may have turned the parameter value into an SSA name.
1331 Go back to the original parameter so we can take the
1332 address. */
1333 if (TREE_CODE (args[i].tree_value) == SSA_NAME)
1335 gcc_assert (SSA_NAME_IS_DEFAULT_DEF (args[i].tree_value));
1336 args[i].tree_value = SSA_NAME_VAR (args[i].tree_value);
1337 gcc_assert (TREE_CODE (args[i].tree_value) == PARM_DECL);
1339 /* Argument setup code may have copied the value to register. We
1340 revert that optimization now because the tail call code must
1341 use the original location. */
1342 if (TREE_CODE (args[i].tree_value) == PARM_DECL
1343 && !MEM_P (DECL_RTL (args[i].tree_value))
1344 && DECL_INCOMING_RTL (args[i].tree_value)
1345 && MEM_P (DECL_INCOMING_RTL (args[i].tree_value)))
1346 set_decl_rtl (args[i].tree_value,
1347 DECL_INCOMING_RTL (args[i].tree_value));
1349 mark_addressable (args[i].tree_value);
1351 /* We can't use sibcalls if a callee-copied argument is
1352 stored in the current function's frame. */
1353 if (!call_from_thunk_p && DECL_P (base) && !TREE_STATIC (base))
1354 *may_tailcall = false;
1356 args[i].tree_value = build_fold_addr_expr_loc (loc,
1357 args[i].tree_value);
1358 type = TREE_TYPE (args[i].tree_value);
1360 if (*ecf_flags & ECF_CONST)
1361 *ecf_flags &= ~(ECF_CONST | ECF_LOOPING_CONST_OR_PURE);
1363 else
1365 /* We make a copy of the object and pass the address to the
1366 function being called. */
1367 rtx copy;
1369 if (!COMPLETE_TYPE_P (type)
1370 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
1371 || (flag_stack_check == GENERIC_STACK_CHECK
1372 && compare_tree_int (TYPE_SIZE_UNIT (type),
1373 STACK_CHECK_MAX_VAR_SIZE) > 0))
1375 /* This is a variable-sized object. Make space on the stack
1376 for it. */
1377 rtx size_rtx = expr_size (args[i].tree_value);
1379 if (*old_stack_level == 0)
1381 emit_stack_save (SAVE_BLOCK, old_stack_level);
1382 *old_pending_adj = pending_stack_adjust;
1383 pending_stack_adjust = 0;
1386 /* We can pass TRUE as the 4th argument because we just
1387 saved the stack pointer and will restore it right after
1388 the call. */
1389 copy = allocate_dynamic_stack_space (size_rtx,
1390 TYPE_ALIGN (type),
1391 TYPE_ALIGN (type),
1392 true);
1393 copy = gen_rtx_MEM (BLKmode, copy);
1394 set_mem_attributes (copy, type, 1);
1396 else
1397 copy = assign_temp (type, 1, 0);
1399 store_expr (args[i].tree_value, copy, 0, false);
1401 /* Just change the const function to pure and then let
1402 the next test clear the pure based on
1403 callee_copies. */
1404 if (*ecf_flags & ECF_CONST)
1406 *ecf_flags &= ~ECF_CONST;
1407 *ecf_flags |= ECF_PURE;
1410 if (!callee_copies && *ecf_flags & ECF_PURE)
1411 *ecf_flags &= ~(ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
1413 args[i].tree_value
1414 = build_fold_addr_expr_loc (loc, make_tree (type, copy));
1415 type = TREE_TYPE (args[i].tree_value);
1416 *may_tailcall = false;
1420 unsignedp = TYPE_UNSIGNED (type);
1421 mode = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
1422 fndecl ? TREE_TYPE (fndecl) : fntype, 0);
1424 args[i].unsignedp = unsignedp;
1425 args[i].mode = mode;
1427 args[i].reg = targetm.calls.function_arg (args_so_far, mode, type,
1428 argpos < n_named_args);
1430 if (args[i].reg && CONST_INT_P (args[i].reg))
1432 args[i].special_slot = args[i].reg;
1433 args[i].reg = NULL;
1436 /* If this is a sibling call and the machine has register windows, the
1437 register window has to be unwinded before calling the routine, so
1438 arguments have to go into the incoming registers. */
1439 if (targetm.calls.function_incoming_arg != targetm.calls.function_arg)
1440 args[i].tail_call_reg
1441 = targetm.calls.function_incoming_arg (args_so_far, mode, type,
1442 argpos < n_named_args);
1443 else
1444 args[i].tail_call_reg = args[i].reg;
1446 if (args[i].reg)
1447 args[i].partial
1448 = targetm.calls.arg_partial_bytes (args_so_far, mode, type,
1449 argpos < n_named_args);
1451 args[i].pass_on_stack = targetm.calls.must_pass_in_stack (mode, type);
1453 /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
1454 it means that we are to pass this arg in the register(s) designated
1455 by the PARALLEL, but also to pass it in the stack. */
1456 if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
1457 && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
1458 args[i].pass_on_stack = 1;
1460 /* If this is an addressable type, we must preallocate the stack
1461 since we must evaluate the object into its final location.
1463 If this is to be passed in both registers and the stack, it is simpler
1464 to preallocate. */
1465 if (TREE_ADDRESSABLE (type)
1466 || (args[i].pass_on_stack && args[i].reg != 0))
1467 *must_preallocate = 1;
1469 /* No stack allocation and padding for bounds. */
1470 if (POINTER_BOUNDS_P (args[i].tree_value))
1472 /* Compute the stack-size of this argument. */
1473 else if (args[i].reg == 0 || args[i].partial != 0
1474 || reg_parm_stack_space > 0
1475 || args[i].pass_on_stack)
1476 locate_and_pad_parm (mode, type,
1477 #ifdef STACK_PARMS_IN_REG_PARM_AREA
1479 #else
1480 args[i].reg != 0,
1481 #endif
1482 reg_parm_stack_space,
1483 args[i].pass_on_stack ? 0 : args[i].partial,
1484 fndecl, args_size, &args[i].locate);
1485 #ifdef BLOCK_REG_PADDING
1486 else
1487 /* The argument is passed entirely in registers. See at which
1488 end it should be padded. */
1489 args[i].locate.where_pad =
1490 BLOCK_REG_PADDING (mode, type,
1491 int_size_in_bytes (type) <= UNITS_PER_WORD);
1492 #endif
1494 /* Update ARGS_SIZE, the total stack space for args so far. */
1496 args_size->constant += args[i].locate.size.constant;
1497 if (args[i].locate.size.var)
1498 ADD_PARM_SIZE (*args_size, args[i].locate.size.var);
1500 /* Increment ARGS_SO_FAR, which has info about which arg-registers
1501 have been used, etc. */
1503 targetm.calls.function_arg_advance (args_so_far, TYPE_MODE (type),
1504 type, argpos < n_named_args);
1508 /* Update ARGS_SIZE to contain the total size for the argument block.
1509 Return the original constant component of the argument block's size.
1511 REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
1512 for arguments passed in registers. */
1514 static int
1515 compute_argument_block_size (int reg_parm_stack_space,
1516 struct args_size *args_size,
1517 tree fndecl ATTRIBUTE_UNUSED,
1518 tree fntype ATTRIBUTE_UNUSED,
1519 int preferred_stack_boundary ATTRIBUTE_UNUSED)
1521 int unadjusted_args_size = args_size->constant;
1523 /* For accumulate outgoing args mode we don't need to align, since the frame
1524 will be already aligned. Align to STACK_BOUNDARY in order to prevent
1525 backends from generating misaligned frame sizes. */
1526 if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
1527 preferred_stack_boundary = STACK_BOUNDARY;
1529 /* Compute the actual size of the argument block required. The variable
1530 and constant sizes must be combined, the size may have to be rounded,
1531 and there may be a minimum required size. */
1533 if (args_size->var)
1535 args_size->var = ARGS_SIZE_TREE (*args_size);
1536 args_size->constant = 0;
1538 preferred_stack_boundary /= BITS_PER_UNIT;
1539 if (preferred_stack_boundary > 1)
1541 /* We don't handle this case yet. To handle it correctly we have
1542 to add the delta, round and subtract the delta.
1543 Currently no machine description requires this support. */
1544 gcc_assert (!(stack_pointer_delta & (preferred_stack_boundary - 1)));
1545 args_size->var = round_up (args_size->var, preferred_stack_boundary);
1548 if (reg_parm_stack_space > 0)
1550 args_size->var
1551 = size_binop (MAX_EXPR, args_size->var,
1552 ssize_int (reg_parm_stack_space));
1554 /* The area corresponding to register parameters is not to count in
1555 the size of the block we need. So make the adjustment. */
1556 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1557 args_size->var
1558 = size_binop (MINUS_EXPR, args_size->var,
1559 ssize_int (reg_parm_stack_space));
1562 else
1564 preferred_stack_boundary /= BITS_PER_UNIT;
1565 if (preferred_stack_boundary < 1)
1566 preferred_stack_boundary = 1;
1567 args_size->constant = (((args_size->constant
1568 + stack_pointer_delta
1569 + preferred_stack_boundary - 1)
1570 / preferred_stack_boundary
1571 * preferred_stack_boundary)
1572 - stack_pointer_delta);
1574 args_size->constant = MAX (args_size->constant,
1575 reg_parm_stack_space);
1577 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1578 args_size->constant -= reg_parm_stack_space;
1580 return unadjusted_args_size;
1583 /* Precompute parameters as needed for a function call.
1585 FLAGS is mask of ECF_* constants.
1587 NUM_ACTUALS is the number of arguments.
1589 ARGS is an array containing information for each argument; this
1590 routine fills in the INITIAL_VALUE and VALUE fields for each
1591 precomputed argument. */
1593 static void
1594 precompute_arguments (int num_actuals, struct arg_data *args)
1596 int i;
1598 /* If this is a libcall, then precompute all arguments so that we do not
1599 get extraneous instructions emitted as part of the libcall sequence. */
1601 /* If we preallocated the stack space, and some arguments must be passed
1602 on the stack, then we must precompute any parameter which contains a
1603 function call which will store arguments on the stack.
1604 Otherwise, evaluating the parameter may clobber previous parameters
1605 which have already been stored into the stack. (we have code to avoid
1606 such case by saving the outgoing stack arguments, but it results in
1607 worse code) */
1608 if (!ACCUMULATE_OUTGOING_ARGS)
1609 return;
1611 for (i = 0; i < num_actuals; i++)
1613 tree type;
1614 machine_mode mode;
1616 if (TREE_CODE (args[i].tree_value) != CALL_EXPR)
1617 continue;
1619 /* If this is an addressable type, we cannot pre-evaluate it. */
1620 type = TREE_TYPE (args[i].tree_value);
1621 gcc_assert (!TREE_ADDRESSABLE (type));
1623 args[i].initial_value = args[i].value
1624 = expand_normal (args[i].tree_value);
1626 mode = TYPE_MODE (type);
1627 if (mode != args[i].mode)
1629 int unsignedp = args[i].unsignedp;
1630 args[i].value
1631 = convert_modes (args[i].mode, mode,
1632 args[i].value, args[i].unsignedp);
1634 /* CSE will replace this only if it contains args[i].value
1635 pseudo, so convert it down to the declared mode using
1636 a SUBREG. */
1637 if (REG_P (args[i].value)
1638 && GET_MODE_CLASS (args[i].mode) == MODE_INT
1639 && promote_mode (type, mode, &unsignedp) != args[i].mode)
1641 args[i].initial_value
1642 = gen_lowpart_SUBREG (mode, args[i].value);
1643 SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1644 SUBREG_PROMOTED_SET (args[i].initial_value, args[i].unsignedp);
1650 /* Given the current state of MUST_PREALLOCATE and information about
1651 arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
1652 compute and return the final value for MUST_PREALLOCATE. */
1654 static int
1655 finalize_must_preallocate (int must_preallocate, int num_actuals,
1656 struct arg_data *args, struct args_size *args_size)
1658 /* See if we have or want to preallocate stack space.
1660 If we would have to push a partially-in-regs parm
1661 before other stack parms, preallocate stack space instead.
1663 If the size of some parm is not a multiple of the required stack
1664 alignment, we must preallocate.
1666 If the total size of arguments that would otherwise create a copy in
1667 a temporary (such as a CALL) is more than half the total argument list
1668 size, preallocation is faster.
1670 Another reason to preallocate is if we have a machine (like the m88k)
1671 where stack alignment is required to be maintained between every
1672 pair of insns, not just when the call is made. However, we assume here
1673 that such machines either do not have push insns (and hence preallocation
1674 would occur anyway) or the problem is taken care of with
1675 PUSH_ROUNDING. */
1677 if (! must_preallocate)
1679 int partial_seen = 0;
1680 int copy_to_evaluate_size = 0;
1681 int i;
1683 for (i = 0; i < num_actuals && ! must_preallocate; i++)
1685 if (args[i].partial > 0 && ! args[i].pass_on_stack)
1686 partial_seen = 1;
1687 else if (partial_seen && args[i].reg == 0)
1688 must_preallocate = 1;
1689 /* We preallocate in case there are bounds passed
1690 in the bounds table to have precomputed address
1691 for bounds association. */
1692 else if (POINTER_BOUNDS_P (args[i].tree_value)
1693 && !args[i].reg)
1694 must_preallocate = 1;
1696 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1697 && (TREE_CODE (args[i].tree_value) == CALL_EXPR
1698 || TREE_CODE (args[i].tree_value) == TARGET_EXPR
1699 || TREE_CODE (args[i].tree_value) == COND_EXPR
1700 || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
1701 copy_to_evaluate_size
1702 += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1705 if (copy_to_evaluate_size * 2 >= args_size->constant
1706 && args_size->constant > 0)
1707 must_preallocate = 1;
1709 return must_preallocate;
1712 /* If we preallocated stack space, compute the address of each argument
1713 and store it into the ARGS array.
1715 We need not ensure it is a valid memory address here; it will be
1716 validized when it is used.
1718 ARGBLOCK is an rtx for the address of the outgoing arguments. */
1720 static void
1721 compute_argument_addresses (struct arg_data *args, rtx argblock, int num_actuals)
1723 if (argblock)
1725 rtx arg_reg = argblock;
1726 int i, arg_offset = 0;
1728 if (GET_CODE (argblock) == PLUS)
1729 arg_reg = XEXP (argblock, 0), arg_offset = INTVAL (XEXP (argblock, 1));
1731 for (i = 0; i < num_actuals; i++)
1733 rtx offset = ARGS_SIZE_RTX (args[i].locate.offset);
1734 rtx slot_offset = ARGS_SIZE_RTX (args[i].locate.slot_offset);
1735 rtx addr;
1736 unsigned int align, boundary;
1737 unsigned int units_on_stack = 0;
1738 machine_mode partial_mode = VOIDmode;
1740 /* Skip this parm if it will not be passed on the stack. */
1741 if (! args[i].pass_on_stack
1742 && args[i].reg != 0
1743 && args[i].partial == 0)
1744 continue;
1746 /* Pointer Bounds are never passed on the stack. */
1747 if (POINTER_BOUNDS_P (args[i].tree_value))
1748 continue;
1750 if (CONST_INT_P (offset))
1751 addr = plus_constant (Pmode, arg_reg, INTVAL (offset));
1752 else
1753 addr = gen_rtx_PLUS (Pmode, arg_reg, offset);
1755 addr = plus_constant (Pmode, addr, arg_offset);
1757 if (args[i].partial != 0)
1759 /* Only part of the parameter is being passed on the stack.
1760 Generate a simple memory reference of the correct size. */
1761 units_on_stack = args[i].locate.size.constant;
1762 partial_mode = mode_for_size (units_on_stack * BITS_PER_UNIT,
1763 MODE_INT, 1);
1764 args[i].stack = gen_rtx_MEM (partial_mode, addr);
1765 set_mem_size (args[i].stack, units_on_stack);
1767 else
1769 args[i].stack = gen_rtx_MEM (args[i].mode, addr);
1770 set_mem_attributes (args[i].stack,
1771 TREE_TYPE (args[i].tree_value), 1);
1773 align = BITS_PER_UNIT;
1774 boundary = args[i].locate.boundary;
1775 if (args[i].locate.where_pad != downward)
1776 align = boundary;
1777 else if (CONST_INT_P (offset))
1779 align = INTVAL (offset) * BITS_PER_UNIT | boundary;
1780 align = align & -align;
1782 set_mem_align (args[i].stack, align);
1784 if (CONST_INT_P (slot_offset))
1785 addr = plus_constant (Pmode, arg_reg, INTVAL (slot_offset));
1786 else
1787 addr = gen_rtx_PLUS (Pmode, arg_reg, slot_offset);
1789 addr = plus_constant (Pmode, addr, arg_offset);
1791 if (args[i].partial != 0)
1793 /* Only part of the parameter is being passed on the stack.
1794 Generate a simple memory reference of the correct size.
1796 args[i].stack_slot = gen_rtx_MEM (partial_mode, addr);
1797 set_mem_size (args[i].stack_slot, units_on_stack);
1799 else
1801 args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
1802 set_mem_attributes (args[i].stack_slot,
1803 TREE_TYPE (args[i].tree_value), 1);
1805 set_mem_align (args[i].stack_slot, args[i].locate.boundary);
1807 /* Function incoming arguments may overlap with sibling call
1808 outgoing arguments and we cannot allow reordering of reads
1809 from function arguments with stores to outgoing arguments
1810 of sibling calls. */
1811 set_mem_alias_set (args[i].stack, 0);
1812 set_mem_alias_set (args[i].stack_slot, 0);
1817 /* Given a FNDECL and EXP, return an rtx suitable for use as a target address
1818 in a call instruction.
1820 FNDECL is the tree node for the target function. For an indirect call
1821 FNDECL will be NULL_TREE.
1823 ADDR is the operand 0 of CALL_EXPR for this call. */
1825 static rtx
1826 rtx_for_function_call (tree fndecl, tree addr)
1828 rtx funexp;
1830 /* Get the function to call, in the form of RTL. */
1831 if (fndecl)
1833 if (!TREE_USED (fndecl) && fndecl != current_function_decl)
1834 TREE_USED (fndecl) = 1;
1836 /* Get a SYMBOL_REF rtx for the function address. */
1837 funexp = XEXP (DECL_RTL (fndecl), 0);
1839 else
1840 /* Generate an rtx (probably a pseudo-register) for the address. */
1842 push_temp_slots ();
1843 funexp = expand_normal (addr);
1844 pop_temp_slots (); /* FUNEXP can't be BLKmode. */
1846 return funexp;
1849 /* Internal state for internal_arg_pointer_based_exp and its helpers. */
1850 static struct
1852 /* Last insn that has been scanned by internal_arg_pointer_based_exp_scan,
1853 or NULL_RTX if none has been scanned yet. */
1854 rtx_insn *scan_start;
1855 /* Vector indexed by REGNO - FIRST_PSEUDO_REGISTER, recording if a pseudo is
1856 based on crtl->args.internal_arg_pointer. The element is NULL_RTX if the
1857 pseudo isn't based on it, a CONST_INT offset if the pseudo is based on it
1858 with fixed offset, or PC if this is with variable or unknown offset. */
1859 vec<rtx> cache;
1860 } internal_arg_pointer_exp_state;
1862 static rtx internal_arg_pointer_based_exp (const_rtx, bool);
1864 /* Helper function for internal_arg_pointer_based_exp. Scan insns in
1865 the tail call sequence, starting with first insn that hasn't been
1866 scanned yet, and note for each pseudo on the LHS whether it is based
1867 on crtl->args.internal_arg_pointer or not, and what offset from that
1868 that pointer it has. */
1870 static void
1871 internal_arg_pointer_based_exp_scan (void)
1873 rtx_insn *insn, *scan_start = internal_arg_pointer_exp_state.scan_start;
1875 if (scan_start == NULL_RTX)
1876 insn = get_insns ();
1877 else
1878 insn = NEXT_INSN (scan_start);
1880 while (insn)
1882 rtx set = single_set (insn);
1883 if (set && REG_P (SET_DEST (set)) && !HARD_REGISTER_P (SET_DEST (set)))
1885 rtx val = NULL_RTX;
1886 unsigned int idx = REGNO (SET_DEST (set)) - FIRST_PSEUDO_REGISTER;
1887 /* Punt on pseudos set multiple times. */
1888 if (idx < internal_arg_pointer_exp_state.cache.length ()
1889 && (internal_arg_pointer_exp_state.cache[idx]
1890 != NULL_RTX))
1891 val = pc_rtx;
1892 else
1893 val = internal_arg_pointer_based_exp (SET_SRC (set), false);
1894 if (val != NULL_RTX)
1896 if (idx >= internal_arg_pointer_exp_state.cache.length ())
1897 internal_arg_pointer_exp_state.cache
1898 .safe_grow_cleared (idx + 1);
1899 internal_arg_pointer_exp_state.cache[idx] = val;
1902 if (NEXT_INSN (insn) == NULL_RTX)
1903 scan_start = insn;
1904 insn = NEXT_INSN (insn);
1907 internal_arg_pointer_exp_state.scan_start = scan_start;
1910 /* Compute whether RTL is based on crtl->args.internal_arg_pointer. Return
1911 NULL_RTX if RTL isn't based on it, a CONST_INT offset if RTL is based on
1912 it with fixed offset, or PC if this is with variable or unknown offset.
1913 TOPLEVEL is true if the function is invoked at the topmost level. */
1915 static rtx
1916 internal_arg_pointer_based_exp (const_rtx rtl, bool toplevel)
1918 if (CONSTANT_P (rtl))
1919 return NULL_RTX;
1921 if (rtl == crtl->args.internal_arg_pointer)
1922 return const0_rtx;
1924 if (REG_P (rtl) && HARD_REGISTER_P (rtl))
1925 return NULL_RTX;
1927 if (GET_CODE (rtl) == PLUS && CONST_INT_P (XEXP (rtl, 1)))
1929 rtx val = internal_arg_pointer_based_exp (XEXP (rtl, 0), toplevel);
1930 if (val == NULL_RTX || val == pc_rtx)
1931 return val;
1932 return plus_constant (Pmode, val, INTVAL (XEXP (rtl, 1)));
1935 /* When called at the topmost level, scan pseudo assignments in between the
1936 last scanned instruction in the tail call sequence and the latest insn
1937 in that sequence. */
1938 if (toplevel)
1939 internal_arg_pointer_based_exp_scan ();
1941 if (REG_P (rtl))
1943 unsigned int idx = REGNO (rtl) - FIRST_PSEUDO_REGISTER;
1944 if (idx < internal_arg_pointer_exp_state.cache.length ())
1945 return internal_arg_pointer_exp_state.cache[idx];
1947 return NULL_RTX;
1950 subrtx_iterator::array_type array;
1951 FOR_EACH_SUBRTX (iter, array, rtl, NONCONST)
1953 const_rtx x = *iter;
1954 if (REG_P (x) && internal_arg_pointer_based_exp (x, false) != NULL_RTX)
1955 return pc_rtx;
1956 if (MEM_P (x))
1957 iter.skip_subrtxes ();
1960 return NULL_RTX;
1963 /* Return true if and only if SIZE storage units (usually bytes)
1964 starting from address ADDR overlap with already clobbered argument
1965 area. This function is used to determine if we should give up a
1966 sibcall. */
1968 static bool
1969 mem_overlaps_already_clobbered_arg_p (rtx addr, unsigned HOST_WIDE_INT size)
1971 HOST_WIDE_INT i;
1972 rtx val;
1974 if (bitmap_empty_p (stored_args_map))
1975 return false;
1976 val = internal_arg_pointer_based_exp (addr, true);
1977 if (val == NULL_RTX)
1978 return false;
1979 else if (val == pc_rtx)
1980 return true;
1981 else
1982 i = INTVAL (val);
1983 #ifdef STACK_GROWS_DOWNWARD
1984 i -= crtl->args.pretend_args_size;
1985 #else
1986 i += crtl->args.pretend_args_size;
1987 #endif
1989 if (ARGS_GROW_DOWNWARD)
1990 i = -i - size;
1992 if (size > 0)
1994 unsigned HOST_WIDE_INT k;
1996 for (k = 0; k < size; k++)
1997 if (i + k < SBITMAP_SIZE (stored_args_map)
1998 && bitmap_bit_p (stored_args_map, i + k))
1999 return true;
2002 return false;
2005 /* Do the register loads required for any wholly-register parms or any
2006 parms which are passed both on the stack and in a register. Their
2007 expressions were already evaluated.
2009 Mark all register-parms as living through the call, putting these USE
2010 insns in the CALL_INSN_FUNCTION_USAGE field.
2012 When IS_SIBCALL, perform the check_sibcall_argument_overlap
2013 checking, setting *SIBCALL_FAILURE if appropriate. */
2015 static void
2016 load_register_parameters (struct arg_data *args, int num_actuals,
2017 rtx *call_fusage, int flags, int is_sibcall,
2018 int *sibcall_failure)
2020 int i, j;
2022 for (i = 0; i < num_actuals; i++)
2024 rtx reg = ((flags & ECF_SIBCALL)
2025 ? args[i].tail_call_reg : args[i].reg);
2026 if (reg)
2028 int partial = args[i].partial;
2029 int nregs;
2030 int size = 0;
2031 rtx_insn *before_arg = get_last_insn ();
2032 /* Set non-negative if we must move a word at a time, even if
2033 just one word (e.g, partial == 4 && mode == DFmode). Set
2034 to -1 if we just use a normal move insn. This value can be
2035 zero if the argument is a zero size structure. */
2036 nregs = -1;
2037 if (GET_CODE (reg) == PARALLEL)
2039 else if (partial)
2041 gcc_assert (partial % UNITS_PER_WORD == 0);
2042 nregs = partial / UNITS_PER_WORD;
2044 else if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode)
2046 size = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
2047 nregs = (size + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
2049 else
2050 size = GET_MODE_SIZE (args[i].mode);
2052 /* Handle calls that pass values in multiple non-contiguous
2053 locations. The Irix 6 ABI has examples of this. */
2055 if (GET_CODE (reg) == PARALLEL)
2056 emit_group_move (reg, args[i].parallel_value);
2058 /* If simple case, just do move. If normal partial, store_one_arg
2059 has already loaded the register for us. In all other cases,
2060 load the register(s) from memory. */
2062 else if (nregs == -1)
2064 emit_move_insn (reg, args[i].value);
2065 #ifdef BLOCK_REG_PADDING
2066 /* Handle case where we have a value that needs shifting
2067 up to the msb. eg. a QImode value and we're padding
2068 upward on a BYTES_BIG_ENDIAN machine. */
2069 if (size < UNITS_PER_WORD
2070 && (args[i].locate.where_pad
2071 == (BYTES_BIG_ENDIAN ? upward : downward)))
2073 rtx x;
2074 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2076 /* Assigning REG here rather than a temp makes CALL_FUSAGE
2077 report the whole reg as used. Strictly speaking, the
2078 call only uses SIZE bytes at the msb end, but it doesn't
2079 seem worth generating rtl to say that. */
2080 reg = gen_rtx_REG (word_mode, REGNO (reg));
2081 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
2082 if (x != reg)
2083 emit_move_insn (reg, x);
2085 #endif
2088 /* If we have pre-computed the values to put in the registers in
2089 the case of non-aligned structures, copy them in now. */
2091 else if (args[i].n_aligned_regs != 0)
2092 for (j = 0; j < args[i].n_aligned_regs; j++)
2093 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
2094 args[i].aligned_regs[j]);
2096 else if (partial == 0 || args[i].pass_on_stack)
2098 rtx mem = validize_mem (copy_rtx (args[i].value));
2100 /* Check for overlap with already clobbered argument area,
2101 providing that this has non-zero size. */
2102 if (is_sibcall
2103 && (size == 0
2104 || mem_overlaps_already_clobbered_arg_p
2105 (XEXP (args[i].value, 0), size)))
2106 *sibcall_failure = 1;
2108 if (size % UNITS_PER_WORD == 0
2109 || MEM_ALIGN (mem) % BITS_PER_WORD == 0)
2110 move_block_to_reg (REGNO (reg), mem, nregs, args[i].mode);
2111 else
2113 if (nregs > 1)
2114 move_block_to_reg (REGNO (reg), mem, nregs - 1,
2115 args[i].mode);
2116 rtx dest = gen_rtx_REG (word_mode, REGNO (reg) + nregs - 1);
2117 unsigned int bitoff = (nregs - 1) * BITS_PER_WORD;
2118 unsigned int bitsize = size * BITS_PER_UNIT - bitoff;
2119 rtx x = extract_bit_field (mem, bitsize, bitoff, 1,
2120 dest, word_mode, word_mode);
2121 if (BYTES_BIG_ENDIAN)
2122 x = expand_shift (LSHIFT_EXPR, word_mode, x,
2123 BITS_PER_WORD - bitsize, dest, 1);
2124 if (x != dest)
2125 emit_move_insn (dest, x);
2128 /* Handle a BLKmode that needs shifting. */
2129 if (nregs == 1 && size < UNITS_PER_WORD
2130 #ifdef BLOCK_REG_PADDING
2131 && args[i].locate.where_pad == downward
2132 #else
2133 && BYTES_BIG_ENDIAN
2134 #endif
2137 rtx dest = gen_rtx_REG (word_mode, REGNO (reg));
2138 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2139 enum tree_code dir = (BYTES_BIG_ENDIAN
2140 ? RSHIFT_EXPR : LSHIFT_EXPR);
2141 rtx x;
2143 x = expand_shift (dir, word_mode, dest, shift, dest, 1);
2144 if (x != dest)
2145 emit_move_insn (dest, x);
2149 /* When a parameter is a block, and perhaps in other cases, it is
2150 possible that it did a load from an argument slot that was
2151 already clobbered. */
2152 if (is_sibcall
2153 && check_sibcall_argument_overlap (before_arg, &args[i], 0))
2154 *sibcall_failure = 1;
2156 /* Handle calls that pass values in multiple non-contiguous
2157 locations. The Irix 6 ABI has examples of this. */
2158 if (GET_CODE (reg) == PARALLEL)
2159 use_group_regs (call_fusage, reg);
2160 else if (nregs == -1)
2161 use_reg_mode (call_fusage, reg,
2162 TYPE_MODE (TREE_TYPE (args[i].tree_value)));
2163 else if (nregs > 0)
2164 use_regs (call_fusage, REGNO (reg), nregs);
2169 /* We need to pop PENDING_STACK_ADJUST bytes. But, if the arguments
2170 wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
2171 bytes, then we would need to push some additional bytes to pad the
2172 arguments. So, we compute an adjust to the stack pointer for an
2173 amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
2174 bytes. Then, when the arguments are pushed the stack will be perfectly
2175 aligned. ARGS_SIZE->CONSTANT is set to the number of bytes that should
2176 be popped after the call. Returns the adjustment. */
2178 static int
2179 combine_pending_stack_adjustment_and_call (int unadjusted_args_size,
2180 struct args_size *args_size,
2181 unsigned int preferred_unit_stack_boundary)
2183 /* The number of bytes to pop so that the stack will be
2184 under-aligned by UNADJUSTED_ARGS_SIZE bytes. */
2185 HOST_WIDE_INT adjustment;
2186 /* The alignment of the stack after the arguments are pushed, if we
2187 just pushed the arguments without adjust the stack here. */
2188 unsigned HOST_WIDE_INT unadjusted_alignment;
2190 unadjusted_alignment
2191 = ((stack_pointer_delta + unadjusted_args_size)
2192 % preferred_unit_stack_boundary);
2194 /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
2195 as possible -- leaving just enough left to cancel out the
2196 UNADJUSTED_ALIGNMENT. In other words, we want to ensure that the
2197 PENDING_STACK_ADJUST is non-negative, and congruent to
2198 -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY. */
2200 /* Begin by trying to pop all the bytes. */
2201 unadjusted_alignment
2202 = (unadjusted_alignment
2203 - (pending_stack_adjust % preferred_unit_stack_boundary));
2204 adjustment = pending_stack_adjust;
2205 /* Push enough additional bytes that the stack will be aligned
2206 after the arguments are pushed. */
2207 if (preferred_unit_stack_boundary > 1)
2209 if (unadjusted_alignment > 0)
2210 adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
2211 else
2212 adjustment += unadjusted_alignment;
2215 /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
2216 bytes after the call. The right number is the entire
2217 PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
2218 by the arguments in the first place. */
2219 args_size->constant
2220 = pending_stack_adjust - adjustment + unadjusted_args_size;
2222 return adjustment;
2225 /* Scan X expression if it does not dereference any argument slots
2226 we already clobbered by tail call arguments (as noted in stored_args_map
2227 bitmap).
2228 Return nonzero if X expression dereferences such argument slots,
2229 zero otherwise. */
2231 static int
2232 check_sibcall_argument_overlap_1 (rtx x)
2234 RTX_CODE code;
2235 int i, j;
2236 const char *fmt;
2238 if (x == NULL_RTX)
2239 return 0;
2241 code = GET_CODE (x);
2243 /* We need not check the operands of the CALL expression itself. */
2244 if (code == CALL)
2245 return 0;
2247 if (code == MEM)
2248 return mem_overlaps_already_clobbered_arg_p (XEXP (x, 0),
2249 GET_MODE_SIZE (GET_MODE (x)));
2251 /* Scan all subexpressions. */
2252 fmt = GET_RTX_FORMAT (code);
2253 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2255 if (*fmt == 'e')
2257 if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
2258 return 1;
2260 else if (*fmt == 'E')
2262 for (j = 0; j < XVECLEN (x, i); j++)
2263 if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
2264 return 1;
2267 return 0;
2270 /* Scan sequence after INSN if it does not dereference any argument slots
2271 we already clobbered by tail call arguments (as noted in stored_args_map
2272 bitmap). If MARK_STORED_ARGS_MAP, add stack slots for ARG to
2273 stored_args_map bitmap afterwards (when ARG is a register MARK_STORED_ARGS_MAP
2274 should be 0). Return nonzero if sequence after INSN dereferences such argument
2275 slots, zero otherwise. */
2277 static int
2278 check_sibcall_argument_overlap (rtx_insn *insn, struct arg_data *arg,
2279 int mark_stored_args_map)
2281 int low, high;
2283 if (insn == NULL_RTX)
2284 insn = get_insns ();
2285 else
2286 insn = NEXT_INSN (insn);
2288 for (; insn; insn = NEXT_INSN (insn))
2289 if (INSN_P (insn)
2290 && check_sibcall_argument_overlap_1 (PATTERN (insn)))
2291 break;
2293 if (mark_stored_args_map)
2295 if (ARGS_GROW_DOWNWARD)
2296 low = -arg->locate.slot_offset.constant - arg->locate.size.constant;
2297 else
2298 low = arg->locate.slot_offset.constant;
2300 for (high = low + arg->locate.size.constant; low < high; low++)
2301 bitmap_set_bit (stored_args_map, low);
2303 return insn != NULL_RTX;
2306 /* Given that a function returns a value of mode MODE at the most
2307 significant end of hard register VALUE, shift VALUE left or right
2308 as specified by LEFT_P. Return true if some action was needed. */
2310 bool
2311 shift_return_value (machine_mode mode, bool left_p, rtx value)
2313 HOST_WIDE_INT shift;
2315 gcc_assert (REG_P (value) && HARD_REGISTER_P (value));
2316 shift = GET_MODE_BITSIZE (GET_MODE (value)) - GET_MODE_BITSIZE (mode);
2317 if (shift == 0)
2318 return false;
2320 /* Use ashr rather than lshr for right shifts. This is for the benefit
2321 of the MIPS port, which requires SImode values to be sign-extended
2322 when stored in 64-bit registers. */
2323 if (!force_expand_binop (GET_MODE (value), left_p ? ashl_optab : ashr_optab,
2324 value, GEN_INT (shift), value, 1, OPTAB_WIDEN))
2325 gcc_unreachable ();
2326 return true;
2329 /* If X is a likely-spilled register value, copy it to a pseudo
2330 register and return that register. Return X otherwise. */
2332 static rtx
2333 avoid_likely_spilled_reg (rtx x)
2335 rtx new_rtx;
2337 if (REG_P (x)
2338 && HARD_REGISTER_P (x)
2339 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (x))))
2341 /* Make sure that we generate a REG rather than a CONCAT.
2342 Moves into CONCATs can need nontrivial instructions,
2343 and the whole point of this function is to avoid
2344 using the hard register directly in such a situation. */
2345 generating_concat_p = 0;
2346 new_rtx = gen_reg_rtx (GET_MODE (x));
2347 generating_concat_p = 1;
2348 emit_move_insn (new_rtx, x);
2349 return new_rtx;
2351 return x;
2354 /* Generate all the code for a CALL_EXPR exp
2355 and return an rtx for its value.
2356 Store the value in TARGET (specified as an rtx) if convenient.
2357 If the value is stored in TARGET then TARGET is returned.
2358 If IGNORE is nonzero, then we ignore the value of the function call. */
2361 expand_call (tree exp, rtx target, int ignore)
2363 /* Nonzero if we are currently expanding a call. */
2364 static int currently_expanding_call = 0;
2366 /* RTX for the function to be called. */
2367 rtx funexp;
2368 /* Sequence of insns to perform a normal "call". */
2369 rtx_insn *normal_call_insns = NULL;
2370 /* Sequence of insns to perform a tail "call". */
2371 rtx_insn *tail_call_insns = NULL;
2372 /* Data type of the function. */
2373 tree funtype;
2374 tree type_arg_types;
2375 tree rettype;
2376 /* Declaration of the function being called,
2377 or 0 if the function is computed (not known by name). */
2378 tree fndecl = 0;
2379 /* The type of the function being called. */
2380 tree fntype;
2381 bool try_tail_call = CALL_EXPR_TAILCALL (exp);
2382 int pass;
2384 /* Register in which non-BLKmode value will be returned,
2385 or 0 if no value or if value is BLKmode. */
2386 rtx valreg;
2387 /* Register(s) in which bounds are returned. */
2388 rtx valbnd = NULL;
2389 /* Address where we should return a BLKmode value;
2390 0 if value not BLKmode. */
2391 rtx structure_value_addr = 0;
2392 /* Nonzero if that address is being passed by treating it as
2393 an extra, implicit first parameter. Otherwise,
2394 it is passed by being copied directly into struct_value_rtx. */
2395 int structure_value_addr_parm = 0;
2396 /* Holds the value of implicit argument for the struct value. */
2397 tree structure_value_addr_value = NULL_TREE;
2398 /* Size of aggregate value wanted, or zero if none wanted
2399 or if we are using the non-reentrant PCC calling convention
2400 or expecting the value in registers. */
2401 HOST_WIDE_INT struct_value_size = 0;
2402 /* Nonzero if called function returns an aggregate in memory PCC style,
2403 by returning the address of where to find it. */
2404 int pcc_struct_value = 0;
2405 rtx struct_value = 0;
2407 /* Number of actual parameters in this call, including struct value addr. */
2408 int num_actuals;
2409 /* Number of named args. Args after this are anonymous ones
2410 and they must all go on the stack. */
2411 int n_named_args;
2412 /* Number of complex actual arguments that need to be split. */
2413 int num_complex_actuals = 0;
2415 /* Vector of information about each argument.
2416 Arguments are numbered in the order they will be pushed,
2417 not the order they are written. */
2418 struct arg_data *args;
2420 /* Total size in bytes of all the stack-parms scanned so far. */
2421 struct args_size args_size;
2422 struct args_size adjusted_args_size;
2423 /* Size of arguments before any adjustments (such as rounding). */
2424 int unadjusted_args_size;
2425 /* Data on reg parms scanned so far. */
2426 CUMULATIVE_ARGS args_so_far_v;
2427 cumulative_args_t args_so_far;
2428 /* Nonzero if a reg parm has been scanned. */
2429 int reg_parm_seen;
2430 /* Nonzero if this is an indirect function call. */
2432 /* Nonzero if we must avoid push-insns in the args for this call.
2433 If stack space is allocated for register parameters, but not by the
2434 caller, then it is preallocated in the fixed part of the stack frame.
2435 So the entire argument block must then be preallocated (i.e., we
2436 ignore PUSH_ROUNDING in that case). */
2438 int must_preallocate = !PUSH_ARGS;
2440 /* Size of the stack reserved for parameter registers. */
2441 int reg_parm_stack_space = 0;
2443 /* Address of space preallocated for stack parms
2444 (on machines that lack push insns), or 0 if space not preallocated. */
2445 rtx argblock = 0;
2447 /* Mask of ECF_ and ERF_ flags. */
2448 int flags = 0;
2449 int return_flags = 0;
2450 #ifdef REG_PARM_STACK_SPACE
2451 /* Define the boundary of the register parm stack space that needs to be
2452 saved, if any. */
2453 int low_to_save, high_to_save;
2454 rtx save_area = 0; /* Place that it is saved */
2455 #endif
2457 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
2458 char *initial_stack_usage_map = stack_usage_map;
2459 char *stack_usage_map_buf = NULL;
2461 int old_stack_allocated;
2463 /* State variables to track stack modifications. */
2464 rtx old_stack_level = 0;
2465 int old_stack_arg_under_construction = 0;
2466 int old_pending_adj = 0;
2467 int old_inhibit_defer_pop = inhibit_defer_pop;
2469 /* Some stack pointer alterations we make are performed via
2470 allocate_dynamic_stack_space. This modifies the stack_pointer_delta,
2471 which we then also need to save/restore along the way. */
2472 int old_stack_pointer_delta = 0;
2474 rtx call_fusage;
2475 tree addr = CALL_EXPR_FN (exp);
2476 int i;
2477 /* The alignment of the stack, in bits. */
2478 unsigned HOST_WIDE_INT preferred_stack_boundary;
2479 /* The alignment of the stack, in bytes. */
2480 unsigned HOST_WIDE_INT preferred_unit_stack_boundary;
2481 /* The static chain value to use for this call. */
2482 rtx static_chain_value;
2483 /* See if this is "nothrow" function call. */
2484 if (TREE_NOTHROW (exp))
2485 flags |= ECF_NOTHROW;
2487 /* See if we can find a DECL-node for the actual function, and get the
2488 function attributes (flags) from the function decl or type node. */
2489 fndecl = get_callee_fndecl (exp);
2490 if (fndecl)
2492 fntype = TREE_TYPE (fndecl);
2493 flags |= flags_from_decl_or_type (fndecl);
2494 return_flags |= decl_return_flags (fndecl);
2496 else
2498 fntype = TREE_TYPE (TREE_TYPE (addr));
2499 flags |= flags_from_decl_or_type (fntype);
2501 rettype = TREE_TYPE (exp);
2503 struct_value = targetm.calls.struct_value_rtx (fntype, 0);
2505 /* Warn if this value is an aggregate type,
2506 regardless of which calling convention we are using for it. */
2507 if (AGGREGATE_TYPE_P (rettype))
2508 warning (OPT_Waggregate_return, "function call has aggregate value");
2510 /* If the result of a non looping pure or const function call is
2511 ignored (or void), and none of its arguments are volatile, we can
2512 avoid expanding the call and just evaluate the arguments for
2513 side-effects. */
2514 if ((flags & (ECF_CONST | ECF_PURE))
2515 && (!(flags & ECF_LOOPING_CONST_OR_PURE))
2516 && (ignore || target == const0_rtx
2517 || TYPE_MODE (rettype) == VOIDmode))
2519 bool volatilep = false;
2520 tree arg;
2521 call_expr_arg_iterator iter;
2523 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2524 if (TREE_THIS_VOLATILE (arg))
2526 volatilep = true;
2527 break;
2530 if (! volatilep)
2532 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2533 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
2534 return const0_rtx;
2538 #ifdef REG_PARM_STACK_SPACE
2539 reg_parm_stack_space = REG_PARM_STACK_SPACE (!fndecl ? fntype : fndecl);
2540 #endif
2542 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
2543 && reg_parm_stack_space > 0 && PUSH_ARGS)
2544 must_preallocate = 1;
2546 /* Set up a place to return a structure. */
2548 /* Cater to broken compilers. */
2549 if (aggregate_value_p (exp, fntype))
2551 /* This call returns a big structure. */
2552 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
2554 #ifdef PCC_STATIC_STRUCT_RETURN
2556 pcc_struct_value = 1;
2558 #else /* not PCC_STATIC_STRUCT_RETURN */
2560 struct_value_size = int_size_in_bytes (rettype);
2562 /* Even if it is semantically safe to use the target as the return
2563 slot, it may be not sufficiently aligned for the return type. */
2564 if (CALL_EXPR_RETURN_SLOT_OPT (exp)
2565 && target
2566 && MEM_P (target)
2567 && !(MEM_ALIGN (target) < TYPE_ALIGN (rettype)
2568 && SLOW_UNALIGNED_ACCESS (TYPE_MODE (rettype),
2569 MEM_ALIGN (target))))
2570 structure_value_addr = XEXP (target, 0);
2571 else
2573 /* For variable-sized objects, we must be called with a target
2574 specified. If we were to allocate space on the stack here,
2575 we would have no way of knowing when to free it. */
2576 rtx d = assign_temp (rettype, 1, 1);
2577 structure_value_addr = XEXP (d, 0);
2578 target = 0;
2581 #endif /* not PCC_STATIC_STRUCT_RETURN */
2584 /* Figure out the amount to which the stack should be aligned. */
2585 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
2586 if (fndecl)
2588 struct cgraph_rtl_info *i = cgraph_node::rtl_info (fndecl);
2589 /* Without automatic stack alignment, we can't increase preferred
2590 stack boundary. With automatic stack alignment, it is
2591 unnecessary since unless we can guarantee that all callers will
2592 align the outgoing stack properly, callee has to align its
2593 stack anyway. */
2594 if (i
2595 && i->preferred_incoming_stack_boundary
2596 && i->preferred_incoming_stack_boundary < preferred_stack_boundary)
2597 preferred_stack_boundary = i->preferred_incoming_stack_boundary;
2600 /* Operand 0 is a pointer-to-function; get the type of the function. */
2601 funtype = TREE_TYPE (addr);
2602 gcc_assert (POINTER_TYPE_P (funtype));
2603 funtype = TREE_TYPE (funtype);
2605 /* Count whether there are actual complex arguments that need to be split
2606 into their real and imaginary parts. Munge the type_arg_types
2607 appropriately here as well. */
2608 if (targetm.calls.split_complex_arg)
2610 call_expr_arg_iterator iter;
2611 tree arg;
2612 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2614 tree type = TREE_TYPE (arg);
2615 if (type && TREE_CODE (type) == COMPLEX_TYPE
2616 && targetm.calls.split_complex_arg (type))
2617 num_complex_actuals++;
2619 type_arg_types = split_complex_types (TYPE_ARG_TYPES (funtype));
2621 else
2622 type_arg_types = TYPE_ARG_TYPES (funtype);
2624 if (flags & ECF_MAY_BE_ALLOCA)
2625 cfun->calls_alloca = 1;
2627 /* If struct_value_rtx is 0, it means pass the address
2628 as if it were an extra parameter. Put the argument expression
2629 in structure_value_addr_value. */
2630 if (structure_value_addr && struct_value == 0)
2632 /* If structure_value_addr is a REG other than
2633 virtual_outgoing_args_rtx, we can use always use it. If it
2634 is not a REG, we must always copy it into a register.
2635 If it is virtual_outgoing_args_rtx, we must copy it to another
2636 register in some cases. */
2637 rtx temp = (!REG_P (structure_value_addr)
2638 || (ACCUMULATE_OUTGOING_ARGS
2639 && stack_arg_under_construction
2640 && structure_value_addr == virtual_outgoing_args_rtx)
2641 ? copy_addr_to_reg (convert_memory_address
2642 (Pmode, structure_value_addr))
2643 : structure_value_addr);
2645 structure_value_addr_value =
2646 make_tree (build_pointer_type (TREE_TYPE (funtype)), temp);
2647 structure_value_addr_parm = CALL_WITH_BOUNDS_P (exp) ? 2 : 1;
2650 /* Count the arguments and set NUM_ACTUALS. */
2651 num_actuals =
2652 call_expr_nargs (exp) + num_complex_actuals + structure_value_addr_parm;
2654 /* Compute number of named args.
2655 First, do a raw count of the args for INIT_CUMULATIVE_ARGS. */
2657 if (type_arg_types != 0)
2658 n_named_args
2659 = (list_length (type_arg_types)
2660 /* Count the struct value address, if it is passed as a parm. */
2661 + structure_value_addr_parm);
2662 else
2663 /* If we know nothing, treat all args as named. */
2664 n_named_args = num_actuals;
2666 /* Start updating where the next arg would go.
2668 On some machines (such as the PA) indirect calls have a different
2669 calling convention than normal calls. The fourth argument in
2670 INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
2671 or not. */
2672 INIT_CUMULATIVE_ARGS (args_so_far_v, funtype, NULL_RTX, fndecl, n_named_args);
2673 args_so_far = pack_cumulative_args (&args_so_far_v);
2675 /* Now possibly adjust the number of named args.
2676 Normally, don't include the last named arg if anonymous args follow.
2677 We do include the last named arg if
2678 targetm.calls.strict_argument_naming() returns nonzero.
2679 (If no anonymous args follow, the result of list_length is actually
2680 one too large. This is harmless.)
2682 If targetm.calls.pretend_outgoing_varargs_named() returns
2683 nonzero, and targetm.calls.strict_argument_naming() returns zero,
2684 this machine will be able to place unnamed args that were passed
2685 in registers into the stack. So treat all args as named. This
2686 allows the insns emitting for a specific argument list to be
2687 independent of the function declaration.
2689 If targetm.calls.pretend_outgoing_varargs_named() returns zero,
2690 we do not have any reliable way to pass unnamed args in
2691 registers, so we must force them into memory. */
2693 if (type_arg_types != 0
2694 && targetm.calls.strict_argument_naming (args_so_far))
2696 else if (type_arg_types != 0
2697 && ! targetm.calls.pretend_outgoing_varargs_named (args_so_far))
2698 /* Don't include the last named arg. */
2699 --n_named_args;
2700 else
2701 /* Treat all args as named. */
2702 n_named_args = num_actuals;
2704 /* Make a vector to hold all the information about each arg. */
2705 args = XALLOCAVEC (struct arg_data, num_actuals);
2706 memset (args, 0, num_actuals * sizeof (struct arg_data));
2708 /* Build up entries in the ARGS array, compute the size of the
2709 arguments into ARGS_SIZE, etc. */
2710 initialize_argument_information (num_actuals, args, &args_size,
2711 n_named_args, exp,
2712 structure_value_addr_value, fndecl, fntype,
2713 args_so_far, reg_parm_stack_space,
2714 &old_stack_level, &old_pending_adj,
2715 &must_preallocate, &flags,
2716 &try_tail_call, CALL_FROM_THUNK_P (exp));
2718 if (args_size.var)
2719 must_preallocate = 1;
2721 /* Now make final decision about preallocating stack space. */
2722 must_preallocate = finalize_must_preallocate (must_preallocate,
2723 num_actuals, args,
2724 &args_size);
2726 /* If the structure value address will reference the stack pointer, we
2727 must stabilize it. We don't need to do this if we know that we are
2728 not going to adjust the stack pointer in processing this call. */
2730 if (structure_value_addr
2731 && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
2732 || reg_mentioned_p (virtual_outgoing_args_rtx,
2733 structure_value_addr))
2734 && (args_size.var
2735 || (!ACCUMULATE_OUTGOING_ARGS && args_size.constant)))
2736 structure_value_addr = copy_to_reg (structure_value_addr);
2738 /* Tail calls can make things harder to debug, and we've traditionally
2739 pushed these optimizations into -O2. Don't try if we're already
2740 expanding a call, as that means we're an argument. Don't try if
2741 there's cleanups, as we know there's code to follow the call. */
2743 if (currently_expanding_call++ != 0
2744 || !flag_optimize_sibling_calls
2745 || args_size.var
2746 || dbg_cnt (tail_call) == false)
2747 try_tail_call = 0;
2749 /* Rest of purposes for tail call optimizations to fail. */
2750 if (
2751 #ifdef HAVE_sibcall_epilogue
2752 !HAVE_sibcall_epilogue
2753 #else
2755 #endif
2756 || !try_tail_call
2757 /* Doing sibling call optimization needs some work, since
2758 structure_value_addr can be allocated on the stack.
2759 It does not seem worth the effort since few optimizable
2760 sibling calls will return a structure. */
2761 || structure_value_addr != NULL_RTX
2762 #ifdef REG_PARM_STACK_SPACE
2763 /* If outgoing reg parm stack space changes, we can not do sibcall. */
2764 || (OUTGOING_REG_PARM_STACK_SPACE (funtype)
2765 != OUTGOING_REG_PARM_STACK_SPACE (TREE_TYPE (current_function_decl)))
2766 || (reg_parm_stack_space != REG_PARM_STACK_SPACE (current_function_decl))
2767 #endif
2768 /* Check whether the target is able to optimize the call
2769 into a sibcall. */
2770 || !targetm.function_ok_for_sibcall (fndecl, exp)
2771 /* Functions that do not return exactly once may not be sibcall
2772 optimized. */
2773 || (flags & (ECF_RETURNS_TWICE | ECF_NORETURN))
2774 || TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (addr)))
2775 /* If the called function is nested in the current one, it might access
2776 some of the caller's arguments, but could clobber them beforehand if
2777 the argument areas are shared. */
2778 || (fndecl && decl_function_context (fndecl) == current_function_decl)
2779 /* If this function requires more stack slots than the current
2780 function, we cannot change it into a sibling call.
2781 crtl->args.pretend_args_size is not part of the
2782 stack allocated by our caller. */
2783 || args_size.constant > (crtl->args.size
2784 - crtl->args.pretend_args_size)
2785 /* If the callee pops its own arguments, then it must pop exactly
2786 the same number of arguments as the current function. */
2787 || (targetm.calls.return_pops_args (fndecl, funtype, args_size.constant)
2788 != targetm.calls.return_pops_args (current_function_decl,
2789 TREE_TYPE (current_function_decl),
2790 crtl->args.size))
2791 || !lang_hooks.decls.ok_for_sibcall (fndecl))
2792 try_tail_call = 0;
2794 /* Check if caller and callee disagree in promotion of function
2795 return value. */
2796 if (try_tail_call)
2798 machine_mode caller_mode, caller_promoted_mode;
2799 machine_mode callee_mode, callee_promoted_mode;
2800 int caller_unsignedp, callee_unsignedp;
2801 tree caller_res = DECL_RESULT (current_function_decl);
2803 caller_unsignedp = TYPE_UNSIGNED (TREE_TYPE (caller_res));
2804 caller_mode = DECL_MODE (caller_res);
2805 callee_unsignedp = TYPE_UNSIGNED (TREE_TYPE (funtype));
2806 callee_mode = TYPE_MODE (TREE_TYPE (funtype));
2807 caller_promoted_mode
2808 = promote_function_mode (TREE_TYPE (caller_res), caller_mode,
2809 &caller_unsignedp,
2810 TREE_TYPE (current_function_decl), 1);
2811 callee_promoted_mode
2812 = promote_function_mode (TREE_TYPE (funtype), callee_mode,
2813 &callee_unsignedp,
2814 funtype, 1);
2815 if (caller_mode != VOIDmode
2816 && (caller_promoted_mode != callee_promoted_mode
2817 || ((caller_mode != caller_promoted_mode
2818 || callee_mode != callee_promoted_mode)
2819 && (caller_unsignedp != callee_unsignedp
2820 || GET_MODE_BITSIZE (caller_mode)
2821 < GET_MODE_BITSIZE (callee_mode)))))
2822 try_tail_call = 0;
2825 /* Ensure current function's preferred stack boundary is at least
2826 what we need. Stack alignment may also increase preferred stack
2827 boundary. */
2828 if (crtl->preferred_stack_boundary < preferred_stack_boundary)
2829 crtl->preferred_stack_boundary = preferred_stack_boundary;
2830 else
2831 preferred_stack_boundary = crtl->preferred_stack_boundary;
2833 preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
2835 /* We want to make two insn chains; one for a sibling call, the other
2836 for a normal call. We will select one of the two chains after
2837 initial RTL generation is complete. */
2838 for (pass = try_tail_call ? 0 : 1; pass < 2; pass++)
2840 int sibcall_failure = 0;
2841 /* We want to emit any pending stack adjustments before the tail
2842 recursion "call". That way we know any adjustment after the tail
2843 recursion call can be ignored if we indeed use the tail
2844 call expansion. */
2845 saved_pending_stack_adjust save;
2846 rtx_insn *insns, *before_call, *after_args;
2847 rtx next_arg_reg;
2849 if (pass == 0)
2851 /* State variables we need to save and restore between
2852 iterations. */
2853 save_pending_stack_adjust (&save);
2855 if (pass)
2856 flags &= ~ECF_SIBCALL;
2857 else
2858 flags |= ECF_SIBCALL;
2860 /* Other state variables that we must reinitialize each time
2861 through the loop (that are not initialized by the loop itself). */
2862 argblock = 0;
2863 call_fusage = 0;
2865 /* Start a new sequence for the normal call case.
2867 From this point on, if the sibling call fails, we want to set
2868 sibcall_failure instead of continuing the loop. */
2869 start_sequence ();
2871 /* Don't let pending stack adjusts add up to too much.
2872 Also, do all pending adjustments now if there is any chance
2873 this might be a call to alloca or if we are expanding a sibling
2874 call sequence.
2875 Also do the adjustments before a throwing call, otherwise
2876 exception handling can fail; PR 19225. */
2877 if (pending_stack_adjust >= 32
2878 || (pending_stack_adjust > 0
2879 && (flags & ECF_MAY_BE_ALLOCA))
2880 || (pending_stack_adjust > 0
2881 && flag_exceptions && !(flags & ECF_NOTHROW))
2882 || pass == 0)
2883 do_pending_stack_adjust ();
2885 /* Precompute any arguments as needed. */
2886 if (pass)
2887 precompute_arguments (num_actuals, args);
2889 /* Now we are about to start emitting insns that can be deleted
2890 if a libcall is deleted. */
2891 if (pass && (flags & ECF_MALLOC))
2892 start_sequence ();
2894 if (pass == 0 && crtl->stack_protect_guard)
2895 stack_protect_epilogue ();
2897 adjusted_args_size = args_size;
2898 /* Compute the actual size of the argument block required. The variable
2899 and constant sizes must be combined, the size may have to be rounded,
2900 and there may be a minimum required size. When generating a sibcall
2901 pattern, do not round up, since we'll be re-using whatever space our
2902 caller provided. */
2903 unadjusted_args_size
2904 = compute_argument_block_size (reg_parm_stack_space,
2905 &adjusted_args_size,
2906 fndecl, fntype,
2907 (pass == 0 ? 0
2908 : preferred_stack_boundary));
2910 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
2912 /* The argument block when performing a sibling call is the
2913 incoming argument block. */
2914 if (pass == 0)
2916 argblock = crtl->args.internal_arg_pointer;
2917 argblock
2918 #ifdef STACK_GROWS_DOWNWARD
2919 = plus_constant (Pmode, argblock, crtl->args.pretend_args_size);
2920 #else
2921 = plus_constant (Pmode, argblock, -crtl->args.pretend_args_size);
2922 #endif
2923 stored_args_map = sbitmap_alloc (args_size.constant);
2924 bitmap_clear (stored_args_map);
2927 /* If we have no actual push instructions, or shouldn't use them,
2928 make space for all args right now. */
2929 else if (adjusted_args_size.var != 0)
2931 if (old_stack_level == 0)
2933 emit_stack_save (SAVE_BLOCK, &old_stack_level);
2934 old_stack_pointer_delta = stack_pointer_delta;
2935 old_pending_adj = pending_stack_adjust;
2936 pending_stack_adjust = 0;
2937 /* stack_arg_under_construction says whether a stack arg is
2938 being constructed at the old stack level. Pushing the stack
2939 gets a clean outgoing argument block. */
2940 old_stack_arg_under_construction = stack_arg_under_construction;
2941 stack_arg_under_construction = 0;
2943 argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
2944 if (flag_stack_usage_info)
2945 current_function_has_unbounded_dynamic_stack_size = 1;
2947 else
2949 /* Note that we must go through the motions of allocating an argument
2950 block even if the size is zero because we may be storing args
2951 in the area reserved for register arguments, which may be part of
2952 the stack frame. */
2954 int needed = adjusted_args_size.constant;
2956 /* Store the maximum argument space used. It will be pushed by
2957 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
2958 checking). */
2960 if (needed > crtl->outgoing_args_size)
2961 crtl->outgoing_args_size = needed;
2963 if (must_preallocate)
2965 if (ACCUMULATE_OUTGOING_ARGS)
2967 /* Since the stack pointer will never be pushed, it is
2968 possible for the evaluation of a parm to clobber
2969 something we have already written to the stack.
2970 Since most function calls on RISC machines do not use
2971 the stack, this is uncommon, but must work correctly.
2973 Therefore, we save any area of the stack that was already
2974 written and that we are using. Here we set up to do this
2975 by making a new stack usage map from the old one. The
2976 actual save will be done by store_one_arg.
2978 Another approach might be to try to reorder the argument
2979 evaluations to avoid this conflicting stack usage. */
2981 /* Since we will be writing into the entire argument area,
2982 the map must be allocated for its entire size, not just
2983 the part that is the responsibility of the caller. */
2984 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
2985 needed += reg_parm_stack_space;
2987 if (ARGS_GROW_DOWNWARD)
2988 highest_outgoing_arg_in_use
2989 = MAX (initial_highest_arg_in_use, needed + 1);
2990 else
2991 highest_outgoing_arg_in_use
2992 = MAX (initial_highest_arg_in_use, needed);
2994 free (stack_usage_map_buf);
2995 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
2996 stack_usage_map = stack_usage_map_buf;
2998 if (initial_highest_arg_in_use)
2999 memcpy (stack_usage_map, initial_stack_usage_map,
3000 initial_highest_arg_in_use);
3002 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
3003 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
3004 (highest_outgoing_arg_in_use
3005 - initial_highest_arg_in_use));
3006 needed = 0;
3008 /* The address of the outgoing argument list must not be
3009 copied to a register here, because argblock would be left
3010 pointing to the wrong place after the call to
3011 allocate_dynamic_stack_space below. */
3013 argblock = virtual_outgoing_args_rtx;
3015 else
3017 if (inhibit_defer_pop == 0)
3019 /* Try to reuse some or all of the pending_stack_adjust
3020 to get this space. */
3021 needed
3022 = (combine_pending_stack_adjustment_and_call
3023 (unadjusted_args_size,
3024 &adjusted_args_size,
3025 preferred_unit_stack_boundary));
3027 /* combine_pending_stack_adjustment_and_call computes
3028 an adjustment before the arguments are allocated.
3029 Account for them and see whether or not the stack
3030 needs to go up or down. */
3031 needed = unadjusted_args_size - needed;
3033 if (needed < 0)
3035 /* We're releasing stack space. */
3036 /* ??? We can avoid any adjustment at all if we're
3037 already aligned. FIXME. */
3038 pending_stack_adjust = -needed;
3039 do_pending_stack_adjust ();
3040 needed = 0;
3042 else
3043 /* We need to allocate space. We'll do that in
3044 push_block below. */
3045 pending_stack_adjust = 0;
3048 /* Special case this because overhead of `push_block' in
3049 this case is non-trivial. */
3050 if (needed == 0)
3051 argblock = virtual_outgoing_args_rtx;
3052 else
3054 argblock = push_block (GEN_INT (needed), 0, 0);
3055 if (ARGS_GROW_DOWNWARD)
3056 argblock = plus_constant (Pmode, argblock, needed);
3059 /* We only really need to call `copy_to_reg' in the case
3060 where push insns are going to be used to pass ARGBLOCK
3061 to a function call in ARGS. In that case, the stack
3062 pointer changes value from the allocation point to the
3063 call point, and hence the value of
3064 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
3065 as well always do it. */
3066 argblock = copy_to_reg (argblock);
3071 if (ACCUMULATE_OUTGOING_ARGS)
3073 /* The save/restore code in store_one_arg handles all
3074 cases except one: a constructor call (including a C
3075 function returning a BLKmode struct) to initialize
3076 an argument. */
3077 if (stack_arg_under_construction)
3079 rtx push_size
3080 = GEN_INT (adjusted_args_size.constant
3081 + (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype
3082 : TREE_TYPE (fndecl))) ? 0
3083 : reg_parm_stack_space));
3084 if (old_stack_level == 0)
3086 emit_stack_save (SAVE_BLOCK, &old_stack_level);
3087 old_stack_pointer_delta = stack_pointer_delta;
3088 old_pending_adj = pending_stack_adjust;
3089 pending_stack_adjust = 0;
3090 /* stack_arg_under_construction says whether a stack
3091 arg is being constructed at the old stack level.
3092 Pushing the stack gets a clean outgoing argument
3093 block. */
3094 old_stack_arg_under_construction
3095 = stack_arg_under_construction;
3096 stack_arg_under_construction = 0;
3097 /* Make a new map for the new argument list. */
3098 free (stack_usage_map_buf);
3099 stack_usage_map_buf = XCNEWVEC (char, highest_outgoing_arg_in_use);
3100 stack_usage_map = stack_usage_map_buf;
3101 highest_outgoing_arg_in_use = 0;
3103 /* We can pass TRUE as the 4th argument because we just
3104 saved the stack pointer and will restore it right after
3105 the call. */
3106 allocate_dynamic_stack_space (push_size, 0,
3107 BIGGEST_ALIGNMENT, true);
3110 /* If argument evaluation might modify the stack pointer,
3111 copy the address of the argument list to a register. */
3112 for (i = 0; i < num_actuals; i++)
3113 if (args[i].pass_on_stack)
3115 argblock = copy_addr_to_reg (argblock);
3116 break;
3120 compute_argument_addresses (args, argblock, num_actuals);
3122 /* Perform stack alignment before the first push (the last arg). */
3123 if (argblock == 0
3124 && adjusted_args_size.constant > reg_parm_stack_space
3125 && adjusted_args_size.constant != unadjusted_args_size)
3127 /* When the stack adjustment is pending, we get better code
3128 by combining the adjustments. */
3129 if (pending_stack_adjust
3130 && ! inhibit_defer_pop)
3132 pending_stack_adjust
3133 = (combine_pending_stack_adjustment_and_call
3134 (unadjusted_args_size,
3135 &adjusted_args_size,
3136 preferred_unit_stack_boundary));
3137 do_pending_stack_adjust ();
3139 else if (argblock == 0)
3140 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
3141 - unadjusted_args_size));
3143 /* Now that the stack is properly aligned, pops can't safely
3144 be deferred during the evaluation of the arguments. */
3145 NO_DEFER_POP;
3147 /* Record the maximum pushed stack space size. We need to delay
3148 doing it this far to take into account the optimization done
3149 by combine_pending_stack_adjustment_and_call. */
3150 if (flag_stack_usage_info
3151 && !ACCUMULATE_OUTGOING_ARGS
3152 && pass
3153 && adjusted_args_size.var == 0)
3155 int pushed = adjusted_args_size.constant + pending_stack_adjust;
3156 if (pushed > current_function_pushed_stack_size)
3157 current_function_pushed_stack_size = pushed;
3160 funexp = rtx_for_function_call (fndecl, addr);
3162 /* Precompute all register parameters. It isn't safe to compute anything
3163 once we have started filling any specific hard regs. */
3164 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
3166 if (CALL_EXPR_STATIC_CHAIN (exp))
3167 static_chain_value = expand_normal (CALL_EXPR_STATIC_CHAIN (exp));
3168 else
3169 static_chain_value = 0;
3171 #ifdef REG_PARM_STACK_SPACE
3172 /* Save the fixed argument area if it's part of the caller's frame and
3173 is clobbered by argument setup for this call. */
3174 if (ACCUMULATE_OUTGOING_ARGS && pass)
3175 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3176 &low_to_save, &high_to_save);
3177 #endif
3179 /* Now store (and compute if necessary) all non-register parms.
3180 These come before register parms, since they can require block-moves,
3181 which could clobber the registers used for register parms.
3182 Parms which have partial registers are not stored here,
3183 but we do preallocate space here if they want that. */
3185 for (i = 0; i < num_actuals; i++)
3187 /* Delay bounds until all other args are stored. */
3188 if (POINTER_BOUNDS_P (args[i].tree_value))
3189 continue;
3190 else if (args[i].reg == 0 || args[i].pass_on_stack)
3192 rtx_insn *before_arg = get_last_insn ();
3194 /* We don't allow passing huge (> 2^30 B) arguments
3195 by value. It would cause an overflow later on. */
3196 if (adjusted_args_size.constant
3197 >= (1 << (HOST_BITS_PER_INT - 2)))
3199 sorry ("passing too large argument on stack");
3200 continue;
3203 if (store_one_arg (&args[i], argblock, flags,
3204 adjusted_args_size.var != 0,
3205 reg_parm_stack_space)
3206 || (pass == 0
3207 && check_sibcall_argument_overlap (before_arg,
3208 &args[i], 1)))
3209 sibcall_failure = 1;
3212 if (args[i].stack)
3213 call_fusage
3214 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[i].tree_value)),
3215 gen_rtx_USE (VOIDmode, args[i].stack),
3216 call_fusage);
3219 /* If we have a parm that is passed in registers but not in memory
3220 and whose alignment does not permit a direct copy into registers,
3221 make a group of pseudos that correspond to each register that we
3222 will later fill. */
3223 if (STRICT_ALIGNMENT)
3224 store_unaligned_arguments_into_pseudos (args, num_actuals);
3226 /* Now store any partially-in-registers parm.
3227 This is the last place a block-move can happen. */
3228 if (reg_parm_seen)
3229 for (i = 0; i < num_actuals; i++)
3230 if (args[i].partial != 0 && ! args[i].pass_on_stack)
3232 rtx_insn *before_arg = get_last_insn ();
3234 if (store_one_arg (&args[i], argblock, flags,
3235 adjusted_args_size.var != 0,
3236 reg_parm_stack_space)
3237 || (pass == 0
3238 && check_sibcall_argument_overlap (before_arg,
3239 &args[i], 1)))
3240 sibcall_failure = 1;
3243 bool any_regs = false;
3244 for (i = 0; i < num_actuals; i++)
3245 if (args[i].reg != NULL_RTX)
3247 any_regs = true;
3248 targetm.calls.call_args (args[i].reg, funtype);
3250 if (!any_regs)
3251 targetm.calls.call_args (pc_rtx, funtype);
3253 /* Figure out the register where the value, if any, will come back. */
3254 valreg = 0;
3255 valbnd = 0;
3256 if (TYPE_MODE (rettype) != VOIDmode
3257 && ! structure_value_addr)
3259 if (pcc_struct_value)
3261 valreg = hard_function_value (build_pointer_type (rettype),
3262 fndecl, NULL, (pass == 0));
3263 if (CALL_WITH_BOUNDS_P (exp))
3264 valbnd = targetm.calls.
3265 chkp_function_value_bounds (build_pointer_type (rettype),
3266 fndecl, (pass == 0));
3268 else
3270 valreg = hard_function_value (rettype, fndecl, fntype,
3271 (pass == 0));
3272 if (CALL_WITH_BOUNDS_P (exp))
3273 valbnd = targetm.calls.chkp_function_value_bounds (rettype,
3274 fndecl,
3275 (pass == 0));
3278 /* If VALREG is a PARALLEL whose first member has a zero
3279 offset, use that. This is for targets such as m68k that
3280 return the same value in multiple places. */
3281 if (GET_CODE (valreg) == PARALLEL)
3283 rtx elem = XVECEXP (valreg, 0, 0);
3284 rtx where = XEXP (elem, 0);
3285 rtx offset = XEXP (elem, 1);
3286 if (offset == const0_rtx
3287 && GET_MODE (where) == GET_MODE (valreg))
3288 valreg = where;
3292 /* Store all bounds not passed in registers. */
3293 for (i = 0; i < num_actuals; i++)
3295 if (POINTER_BOUNDS_P (args[i].tree_value)
3296 && !args[i].reg)
3297 store_bounds (&args[i],
3298 args[i].pointer_arg == -1
3299 ? NULL
3300 : &args[args[i].pointer_arg]);
3303 /* If register arguments require space on the stack and stack space
3304 was not preallocated, allocate stack space here for arguments
3305 passed in registers. */
3306 if (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
3307 && !ACCUMULATE_OUTGOING_ARGS
3308 && must_preallocate == 0 && reg_parm_stack_space > 0)
3309 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
3311 /* Pass the function the address in which to return a
3312 structure value. */
3313 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
3315 structure_value_addr
3316 = convert_memory_address (Pmode, structure_value_addr);
3317 emit_move_insn (struct_value,
3318 force_reg (Pmode,
3319 force_operand (structure_value_addr,
3320 NULL_RTX)));
3322 if (REG_P (struct_value))
3323 use_reg (&call_fusage, struct_value);
3326 after_args = get_last_insn ();
3327 funexp = prepare_call_address (fndecl ? fndecl : fntype, funexp,
3328 static_chain_value, &call_fusage,
3329 reg_parm_seen, pass == 0);
3331 load_register_parameters (args, num_actuals, &call_fusage, flags,
3332 pass == 0, &sibcall_failure);
3334 /* Save a pointer to the last insn before the call, so that we can
3335 later safely search backwards to find the CALL_INSN. */
3336 before_call = get_last_insn ();
3338 /* Set up next argument register. For sibling calls on machines
3339 with register windows this should be the incoming register. */
3340 if (pass == 0)
3341 next_arg_reg = targetm.calls.function_incoming_arg (args_so_far,
3342 VOIDmode,
3343 void_type_node,
3344 true);
3345 else
3346 next_arg_reg = targetm.calls.function_arg (args_so_far,
3347 VOIDmode, void_type_node,
3348 true);
3350 if (pass == 1 && (return_flags & ERF_RETURNS_ARG))
3352 int arg_nr = return_flags & ERF_RETURN_ARG_MASK;
3353 arg_nr = num_actuals - arg_nr - 1;
3354 if (arg_nr >= 0
3355 && arg_nr < num_actuals
3356 && args[arg_nr].reg
3357 && valreg
3358 && REG_P (valreg)
3359 && GET_MODE (args[arg_nr].reg) == GET_MODE (valreg))
3360 call_fusage
3361 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[arg_nr].tree_value)),
3362 gen_rtx_SET (valreg, args[arg_nr].reg),
3363 call_fusage);
3365 /* All arguments and registers used for the call must be set up by
3366 now! */
3368 /* Stack must be properly aligned now. */
3369 gcc_assert (!pass
3370 || !(stack_pointer_delta % preferred_unit_stack_boundary));
3372 /* Generate the actual call instruction. */
3373 emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
3374 adjusted_args_size.constant, struct_value_size,
3375 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
3376 flags, args_so_far);
3378 if (flag_ipa_ra)
3380 rtx_call_insn *last;
3381 rtx datum = NULL_RTX;
3382 if (fndecl != NULL_TREE)
3384 datum = XEXP (DECL_RTL (fndecl), 0);
3385 gcc_assert (datum != NULL_RTX
3386 && GET_CODE (datum) == SYMBOL_REF);
3388 last = last_call_insn ();
3389 add_reg_note (last, REG_CALL_DECL, datum);
3392 /* If the call setup or the call itself overlaps with anything
3393 of the argument setup we probably clobbered our call address.
3394 In that case we can't do sibcalls. */
3395 if (pass == 0
3396 && check_sibcall_argument_overlap (after_args, 0, 0))
3397 sibcall_failure = 1;
3399 /* If a non-BLKmode value is returned at the most significant end
3400 of a register, shift the register right by the appropriate amount
3401 and update VALREG accordingly. BLKmode values are handled by the
3402 group load/store machinery below. */
3403 if (!structure_value_addr
3404 && !pcc_struct_value
3405 && TYPE_MODE (rettype) != VOIDmode
3406 && TYPE_MODE (rettype) != BLKmode
3407 && REG_P (valreg)
3408 && targetm.calls.return_in_msb (rettype))
3410 if (shift_return_value (TYPE_MODE (rettype), false, valreg))
3411 sibcall_failure = 1;
3412 valreg = gen_rtx_REG (TYPE_MODE (rettype), REGNO (valreg));
3415 if (pass && (flags & ECF_MALLOC))
3417 rtx temp = gen_reg_rtx (GET_MODE (valreg));
3418 rtx_insn *last, *insns;
3420 /* The return value from a malloc-like function is a pointer. */
3421 if (TREE_CODE (rettype) == POINTER_TYPE)
3422 mark_reg_pointer (temp, MALLOC_ABI_ALIGNMENT);
3424 emit_move_insn (temp, valreg);
3426 /* The return value from a malloc-like function can not alias
3427 anything else. */
3428 last = get_last_insn ();
3429 add_reg_note (last, REG_NOALIAS, temp);
3431 /* Write out the sequence. */
3432 insns = get_insns ();
3433 end_sequence ();
3434 emit_insn (insns);
3435 valreg = temp;
3438 /* For calls to `setjmp', etc., inform
3439 function.c:setjmp_warnings that it should complain if
3440 nonvolatile values are live. For functions that cannot
3441 return, inform flow that control does not fall through. */
3443 if ((flags & ECF_NORETURN) || pass == 0)
3445 /* The barrier must be emitted
3446 immediately after the CALL_INSN. Some ports emit more
3447 than just a CALL_INSN above, so we must search for it here. */
3449 rtx_insn *last = get_last_insn ();
3450 while (!CALL_P (last))
3452 last = PREV_INSN (last);
3453 /* There was no CALL_INSN? */
3454 gcc_assert (last != before_call);
3457 emit_barrier_after (last);
3459 /* Stack adjustments after a noreturn call are dead code.
3460 However when NO_DEFER_POP is in effect, we must preserve
3461 stack_pointer_delta. */
3462 if (inhibit_defer_pop == 0)
3464 stack_pointer_delta = old_stack_allocated;
3465 pending_stack_adjust = 0;
3469 /* If value type not void, return an rtx for the value. */
3471 if (TYPE_MODE (rettype) == VOIDmode
3472 || ignore)
3473 target = const0_rtx;
3474 else if (structure_value_addr)
3476 if (target == 0 || !MEM_P (target))
3478 target
3479 = gen_rtx_MEM (TYPE_MODE (rettype),
3480 memory_address (TYPE_MODE (rettype),
3481 structure_value_addr));
3482 set_mem_attributes (target, rettype, 1);
3485 else if (pcc_struct_value)
3487 /* This is the special C++ case where we need to
3488 know what the true target was. We take care to
3489 never use this value more than once in one expression. */
3490 target = gen_rtx_MEM (TYPE_MODE (rettype),
3491 copy_to_reg (valreg));
3492 set_mem_attributes (target, rettype, 1);
3494 /* Handle calls that return values in multiple non-contiguous locations.
3495 The Irix 6 ABI has examples of this. */
3496 else if (GET_CODE (valreg) == PARALLEL)
3498 if (target == 0)
3499 target = emit_group_move_into_temps (valreg);
3500 else if (rtx_equal_p (target, valreg))
3502 else if (GET_CODE (target) == PARALLEL)
3503 /* Handle the result of a emit_group_move_into_temps
3504 call in the previous pass. */
3505 emit_group_move (target, valreg);
3506 else
3507 emit_group_store (target, valreg, rettype,
3508 int_size_in_bytes (rettype));
3510 else if (target
3511 && GET_MODE (target) == TYPE_MODE (rettype)
3512 && GET_MODE (target) == GET_MODE (valreg))
3514 bool may_overlap = false;
3516 /* We have to copy a return value in a CLASS_LIKELY_SPILLED hard
3517 reg to a plain register. */
3518 if (!REG_P (target) || HARD_REGISTER_P (target))
3519 valreg = avoid_likely_spilled_reg (valreg);
3521 /* If TARGET is a MEM in the argument area, and we have
3522 saved part of the argument area, then we can't store
3523 directly into TARGET as it may get overwritten when we
3524 restore the argument save area below. Don't work too
3525 hard though and simply force TARGET to a register if it
3526 is a MEM; the optimizer is quite likely to sort it out. */
3527 if (ACCUMULATE_OUTGOING_ARGS && pass && MEM_P (target))
3528 for (i = 0; i < num_actuals; i++)
3529 if (args[i].save_area)
3531 may_overlap = true;
3532 break;
3535 if (may_overlap)
3536 target = copy_to_reg (valreg);
3537 else
3539 /* TARGET and VALREG cannot be equal at this point
3540 because the latter would not have
3541 REG_FUNCTION_VALUE_P true, while the former would if
3542 it were referring to the same register.
3544 If they refer to the same register, this move will be
3545 a no-op, except when function inlining is being
3546 done. */
3547 emit_move_insn (target, valreg);
3549 /* If we are setting a MEM, this code must be executed.
3550 Since it is emitted after the call insn, sibcall
3551 optimization cannot be performed in that case. */
3552 if (MEM_P (target))
3553 sibcall_failure = 1;
3556 else
3557 target = copy_to_reg (avoid_likely_spilled_reg (valreg));
3559 /* If we promoted this return value, make the proper SUBREG.
3560 TARGET might be const0_rtx here, so be careful. */
3561 if (REG_P (target)
3562 && TYPE_MODE (rettype) != BLKmode
3563 && GET_MODE (target) != TYPE_MODE (rettype))
3565 tree type = rettype;
3566 int unsignedp = TYPE_UNSIGNED (type);
3567 int offset = 0;
3568 machine_mode pmode;
3570 /* Ensure we promote as expected, and get the new unsignedness. */
3571 pmode = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
3572 funtype, 1);
3573 gcc_assert (GET_MODE (target) == pmode);
3575 if ((WORDS_BIG_ENDIAN || BYTES_BIG_ENDIAN)
3576 && (GET_MODE_SIZE (GET_MODE (target))
3577 > GET_MODE_SIZE (TYPE_MODE (type))))
3579 offset = GET_MODE_SIZE (GET_MODE (target))
3580 - GET_MODE_SIZE (TYPE_MODE (type));
3581 if (! BYTES_BIG_ENDIAN)
3582 offset = (offset / UNITS_PER_WORD) * UNITS_PER_WORD;
3583 else if (! WORDS_BIG_ENDIAN)
3584 offset %= UNITS_PER_WORD;
3587 target = gen_rtx_SUBREG (TYPE_MODE (type), target, offset);
3588 SUBREG_PROMOTED_VAR_P (target) = 1;
3589 SUBREG_PROMOTED_SET (target, unsignedp);
3592 /* If size of args is variable or this was a constructor call for a stack
3593 argument, restore saved stack-pointer value. */
3595 if (old_stack_level)
3597 rtx_insn *prev = get_last_insn ();
3599 emit_stack_restore (SAVE_BLOCK, old_stack_level);
3600 stack_pointer_delta = old_stack_pointer_delta;
3602 fixup_args_size_notes (prev, get_last_insn (), stack_pointer_delta);
3604 pending_stack_adjust = old_pending_adj;
3605 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
3606 stack_arg_under_construction = old_stack_arg_under_construction;
3607 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3608 stack_usage_map = initial_stack_usage_map;
3609 sibcall_failure = 1;
3611 else if (ACCUMULATE_OUTGOING_ARGS && pass)
3613 #ifdef REG_PARM_STACK_SPACE
3614 if (save_area)
3615 restore_fixed_argument_area (save_area, argblock,
3616 high_to_save, low_to_save);
3617 #endif
3619 /* If we saved any argument areas, restore them. */
3620 for (i = 0; i < num_actuals; i++)
3621 if (args[i].save_area)
3623 machine_mode save_mode = GET_MODE (args[i].save_area);
3624 rtx stack_area
3625 = gen_rtx_MEM (save_mode,
3626 memory_address (save_mode,
3627 XEXP (args[i].stack_slot, 0)));
3629 if (save_mode != BLKmode)
3630 emit_move_insn (stack_area, args[i].save_area);
3631 else
3632 emit_block_move (stack_area, args[i].save_area,
3633 GEN_INT (args[i].locate.size.constant),
3634 BLOCK_OP_CALL_PARM);
3637 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3638 stack_usage_map = initial_stack_usage_map;
3641 /* If this was alloca, record the new stack level for nonlocal gotos.
3642 Check for the handler slots since we might not have a save area
3643 for non-local gotos. */
3645 if ((flags & ECF_MAY_BE_ALLOCA) && cfun->nonlocal_goto_save_area != 0)
3646 update_nonlocal_goto_save_area ();
3648 /* Free up storage we no longer need. */
3649 for (i = 0; i < num_actuals; ++i)
3650 free (args[i].aligned_regs);
3652 targetm.calls.end_call_args ();
3654 insns = get_insns ();
3655 end_sequence ();
3657 if (pass == 0)
3659 tail_call_insns = insns;
3661 /* Restore the pending stack adjustment now that we have
3662 finished generating the sibling call sequence. */
3664 restore_pending_stack_adjust (&save);
3666 /* Prepare arg structure for next iteration. */
3667 for (i = 0; i < num_actuals; i++)
3669 args[i].value = 0;
3670 args[i].aligned_regs = 0;
3671 args[i].stack = 0;
3674 sbitmap_free (stored_args_map);
3675 internal_arg_pointer_exp_state.scan_start = NULL;
3676 internal_arg_pointer_exp_state.cache.release ();
3678 else
3680 normal_call_insns = insns;
3682 /* Verify that we've deallocated all the stack we used. */
3683 gcc_assert ((flags & ECF_NORETURN)
3684 || (old_stack_allocated
3685 == stack_pointer_delta - pending_stack_adjust));
3688 /* If something prevents making this a sibling call,
3689 zero out the sequence. */
3690 if (sibcall_failure)
3691 tail_call_insns = NULL;
3692 else
3693 break;
3696 /* If tail call production succeeded, we need to remove REG_EQUIV notes on
3697 arguments too, as argument area is now clobbered by the call. */
3698 if (tail_call_insns)
3700 emit_insn (tail_call_insns);
3701 crtl->tail_call_emit = true;
3703 else
3704 emit_insn (normal_call_insns);
3706 currently_expanding_call--;
3708 free (stack_usage_map_buf);
3710 /* Join result with returned bounds so caller may use them if needed. */
3711 target = chkp_join_splitted_slot (target, valbnd);
3713 return target;
3716 /* A sibling call sequence invalidates any REG_EQUIV notes made for
3717 this function's incoming arguments.
3719 At the start of RTL generation we know the only REG_EQUIV notes
3720 in the rtl chain are those for incoming arguments, so we can look
3721 for REG_EQUIV notes between the start of the function and the
3722 NOTE_INSN_FUNCTION_BEG.
3724 This is (slight) overkill. We could keep track of the highest
3725 argument we clobber and be more selective in removing notes, but it
3726 does not seem to be worth the effort. */
3728 void
3729 fixup_tail_calls (void)
3731 rtx_insn *insn;
3733 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3735 rtx note;
3737 /* There are never REG_EQUIV notes for the incoming arguments
3738 after the NOTE_INSN_FUNCTION_BEG note, so stop if we see it. */
3739 if (NOTE_P (insn)
3740 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3741 break;
3743 note = find_reg_note (insn, REG_EQUIV, 0);
3744 if (note)
3745 remove_note (insn, note);
3746 note = find_reg_note (insn, REG_EQUIV, 0);
3747 gcc_assert (!note);
3751 /* Traverse a list of TYPES and expand all complex types into their
3752 components. */
3753 static tree
3754 split_complex_types (tree types)
3756 tree p;
3758 /* Before allocating memory, check for the common case of no complex. */
3759 for (p = types; p; p = TREE_CHAIN (p))
3761 tree type = TREE_VALUE (p);
3762 if (TREE_CODE (type) == COMPLEX_TYPE
3763 && targetm.calls.split_complex_arg (type))
3764 goto found;
3766 return types;
3768 found:
3769 types = copy_list (types);
3771 for (p = types; p; p = TREE_CHAIN (p))
3773 tree complex_type = TREE_VALUE (p);
3775 if (TREE_CODE (complex_type) == COMPLEX_TYPE
3776 && targetm.calls.split_complex_arg (complex_type))
3778 tree next, imag;
3780 /* Rewrite complex type with component type. */
3781 TREE_VALUE (p) = TREE_TYPE (complex_type);
3782 next = TREE_CHAIN (p);
3784 /* Add another component type for the imaginary part. */
3785 imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
3786 TREE_CHAIN (p) = imag;
3787 TREE_CHAIN (imag) = next;
3789 /* Skip the newly created node. */
3790 p = TREE_CHAIN (p);
3794 return types;
3797 /* Output a library call to function FUN (a SYMBOL_REF rtx).
3798 The RETVAL parameter specifies whether return value needs to be saved, other
3799 parameters are documented in the emit_library_call function below. */
3801 static rtx
3802 emit_library_call_value_1 (int retval, rtx orgfun, rtx value,
3803 enum libcall_type fn_type,
3804 machine_mode outmode, int nargs, va_list p)
3806 /* Total size in bytes of all the stack-parms scanned so far. */
3807 struct args_size args_size;
3808 /* Size of arguments before any adjustments (such as rounding). */
3809 struct args_size original_args_size;
3810 int argnum;
3811 rtx fun;
3812 /* Todo, choose the correct decl type of orgfun. Sadly this information
3813 isn't present here, so we default to native calling abi here. */
3814 tree fndecl ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
3815 tree fntype ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
3816 int count;
3817 rtx argblock = 0;
3818 CUMULATIVE_ARGS args_so_far_v;
3819 cumulative_args_t args_so_far;
3820 struct arg
3822 rtx value;
3823 machine_mode mode;
3824 rtx reg;
3825 int partial;
3826 struct locate_and_pad_arg_data locate;
3827 rtx save_area;
3829 struct arg *argvec;
3830 int old_inhibit_defer_pop = inhibit_defer_pop;
3831 rtx call_fusage = 0;
3832 rtx mem_value = 0;
3833 rtx valreg;
3834 int pcc_struct_value = 0;
3835 int struct_value_size = 0;
3836 int flags;
3837 int reg_parm_stack_space = 0;
3838 int needed;
3839 rtx_insn *before_call;
3840 bool have_push_fusage;
3841 tree tfom; /* type_for_mode (outmode, 0) */
3843 #ifdef REG_PARM_STACK_SPACE
3844 /* Define the boundary of the register parm stack space that needs to be
3845 save, if any. */
3846 int low_to_save = 0, high_to_save = 0;
3847 rtx save_area = 0; /* Place that it is saved. */
3848 #endif
3850 /* Size of the stack reserved for parameter registers. */
3851 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
3852 char *initial_stack_usage_map = stack_usage_map;
3853 char *stack_usage_map_buf = NULL;
3855 rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
3857 #ifdef REG_PARM_STACK_SPACE
3858 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
3859 #endif
3861 /* By default, library functions can not throw. */
3862 flags = ECF_NOTHROW;
3864 switch (fn_type)
3866 case LCT_NORMAL:
3867 break;
3868 case LCT_CONST:
3869 flags |= ECF_CONST;
3870 break;
3871 case LCT_PURE:
3872 flags |= ECF_PURE;
3873 break;
3874 case LCT_NORETURN:
3875 flags |= ECF_NORETURN;
3876 break;
3877 case LCT_THROW:
3878 flags = ECF_NORETURN;
3879 break;
3880 case LCT_RETURNS_TWICE:
3881 flags = ECF_RETURNS_TWICE;
3882 break;
3884 fun = orgfun;
3886 /* Ensure current function's preferred stack boundary is at least
3887 what we need. */
3888 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
3889 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3891 /* If this kind of value comes back in memory,
3892 decide where in memory it should come back. */
3893 if (outmode != VOIDmode)
3895 tfom = lang_hooks.types.type_for_mode (outmode, 0);
3896 if (aggregate_value_p (tfom, 0))
3898 #ifdef PCC_STATIC_STRUCT_RETURN
3899 rtx pointer_reg
3900 = hard_function_value (build_pointer_type (tfom), 0, 0, 0);
3901 mem_value = gen_rtx_MEM (outmode, pointer_reg);
3902 pcc_struct_value = 1;
3903 if (value == 0)
3904 value = gen_reg_rtx (outmode);
3905 #else /* not PCC_STATIC_STRUCT_RETURN */
3906 struct_value_size = GET_MODE_SIZE (outmode);
3907 if (value != 0 && MEM_P (value))
3908 mem_value = value;
3909 else
3910 mem_value = assign_temp (tfom, 1, 1);
3911 #endif
3912 /* This call returns a big structure. */
3913 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
3916 else
3917 tfom = void_type_node;
3919 /* ??? Unfinished: must pass the memory address as an argument. */
3921 /* Copy all the libcall-arguments out of the varargs data
3922 and into a vector ARGVEC.
3924 Compute how to pass each argument. We only support a very small subset
3925 of the full argument passing conventions to limit complexity here since
3926 library functions shouldn't have many args. */
3928 argvec = XALLOCAVEC (struct arg, nargs + 1);
3929 memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
3931 #ifdef INIT_CUMULATIVE_LIBCALL_ARGS
3932 INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far_v, outmode, fun);
3933 #else
3934 INIT_CUMULATIVE_ARGS (args_so_far_v, NULL_TREE, fun, 0, nargs);
3935 #endif
3936 args_so_far = pack_cumulative_args (&args_so_far_v);
3938 args_size.constant = 0;
3939 args_size.var = 0;
3941 count = 0;
3943 push_temp_slots ();
3945 /* If there's a structure value address to be passed,
3946 either pass it in the special place, or pass it as an extra argument. */
3947 if (mem_value && struct_value == 0 && ! pcc_struct_value)
3949 rtx addr = XEXP (mem_value, 0);
3951 nargs++;
3953 /* Make sure it is a reasonable operand for a move or push insn. */
3954 if (!REG_P (addr) && !MEM_P (addr)
3955 && !(CONSTANT_P (addr)
3956 && targetm.legitimate_constant_p (Pmode, addr)))
3957 addr = force_operand (addr, NULL_RTX);
3959 argvec[count].value = addr;
3960 argvec[count].mode = Pmode;
3961 argvec[count].partial = 0;
3963 argvec[count].reg = targetm.calls.function_arg (args_so_far,
3964 Pmode, NULL_TREE, true);
3965 gcc_assert (targetm.calls.arg_partial_bytes (args_so_far, Pmode,
3966 NULL_TREE, 1) == 0);
3968 locate_and_pad_parm (Pmode, NULL_TREE,
3969 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3971 #else
3972 argvec[count].reg != 0,
3973 #endif
3974 reg_parm_stack_space, 0,
3975 NULL_TREE, &args_size, &argvec[count].locate);
3977 if (argvec[count].reg == 0 || argvec[count].partial != 0
3978 || reg_parm_stack_space > 0)
3979 args_size.constant += argvec[count].locate.size.constant;
3981 targetm.calls.function_arg_advance (args_so_far, Pmode, (tree) 0, true);
3983 count++;
3986 for (; count < nargs; count++)
3988 rtx val = va_arg (p, rtx);
3989 machine_mode mode = (machine_mode) va_arg (p, int);
3990 int unsigned_p = 0;
3992 /* We cannot convert the arg value to the mode the library wants here;
3993 must do it earlier where we know the signedness of the arg. */
3994 gcc_assert (mode != BLKmode
3995 && (GET_MODE (val) == mode || GET_MODE (val) == VOIDmode));
3997 /* Make sure it is a reasonable operand for a move or push insn. */
3998 if (!REG_P (val) && !MEM_P (val)
3999 && !(CONSTANT_P (val) && targetm.legitimate_constant_p (mode, val)))
4000 val = force_operand (val, NULL_RTX);
4002 if (pass_by_reference (&args_so_far_v, mode, NULL_TREE, 1))
4004 rtx slot;
4005 int must_copy
4006 = !reference_callee_copied (&args_so_far_v, mode, NULL_TREE, 1);
4008 /* If this was a CONST function, it is now PURE since it now
4009 reads memory. */
4010 if (flags & ECF_CONST)
4012 flags &= ~ECF_CONST;
4013 flags |= ECF_PURE;
4016 if (MEM_P (val) && !must_copy)
4018 tree val_expr = MEM_EXPR (val);
4019 if (val_expr)
4020 mark_addressable (val_expr);
4021 slot = val;
4023 else
4025 slot = assign_temp (lang_hooks.types.type_for_mode (mode, 0),
4026 1, 1);
4027 emit_move_insn (slot, val);
4030 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4031 gen_rtx_USE (VOIDmode, slot),
4032 call_fusage);
4033 if (must_copy)
4034 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4035 gen_rtx_CLOBBER (VOIDmode,
4036 slot),
4037 call_fusage);
4039 mode = Pmode;
4040 val = force_operand (XEXP (slot, 0), NULL_RTX);
4043 mode = promote_function_mode (NULL_TREE, mode, &unsigned_p, NULL_TREE, 0);
4044 argvec[count].mode = mode;
4045 argvec[count].value = convert_modes (mode, GET_MODE (val), val, unsigned_p);
4046 argvec[count].reg = targetm.calls.function_arg (args_so_far, mode,
4047 NULL_TREE, true);
4049 argvec[count].partial
4050 = targetm.calls.arg_partial_bytes (args_so_far, mode, NULL_TREE, 1);
4052 if (argvec[count].reg == 0
4053 || argvec[count].partial != 0
4054 || reg_parm_stack_space > 0)
4056 locate_and_pad_parm (mode, NULL_TREE,
4057 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4059 #else
4060 argvec[count].reg != 0,
4061 #endif
4062 reg_parm_stack_space, argvec[count].partial,
4063 NULL_TREE, &args_size, &argvec[count].locate);
4064 args_size.constant += argvec[count].locate.size.constant;
4065 gcc_assert (!argvec[count].locate.size.var);
4067 #ifdef BLOCK_REG_PADDING
4068 else
4069 /* The argument is passed entirely in registers. See at which
4070 end it should be padded. */
4071 argvec[count].locate.where_pad =
4072 BLOCK_REG_PADDING (mode, NULL_TREE,
4073 GET_MODE_SIZE (mode) <= UNITS_PER_WORD);
4074 #endif
4076 targetm.calls.function_arg_advance (args_so_far, mode, (tree) 0, true);
4079 /* If this machine requires an external definition for library
4080 functions, write one out. */
4081 assemble_external_libcall (fun);
4083 original_args_size = args_size;
4084 args_size.constant = (((args_size.constant
4085 + stack_pointer_delta
4086 + STACK_BYTES - 1)
4087 / STACK_BYTES
4088 * STACK_BYTES)
4089 - stack_pointer_delta);
4091 args_size.constant = MAX (args_size.constant,
4092 reg_parm_stack_space);
4094 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4095 args_size.constant -= reg_parm_stack_space;
4097 if (args_size.constant > crtl->outgoing_args_size)
4098 crtl->outgoing_args_size = args_size.constant;
4100 if (flag_stack_usage_info && !ACCUMULATE_OUTGOING_ARGS)
4102 int pushed = args_size.constant + pending_stack_adjust;
4103 if (pushed > current_function_pushed_stack_size)
4104 current_function_pushed_stack_size = pushed;
4107 if (ACCUMULATE_OUTGOING_ARGS)
4109 /* Since the stack pointer will never be pushed, it is possible for
4110 the evaluation of a parm to clobber something we have already
4111 written to the stack. Since most function calls on RISC machines
4112 do not use the stack, this is uncommon, but must work correctly.
4114 Therefore, we save any area of the stack that was already written
4115 and that we are using. Here we set up to do this by making a new
4116 stack usage map from the old one.
4118 Another approach might be to try to reorder the argument
4119 evaluations to avoid this conflicting stack usage. */
4121 needed = args_size.constant;
4123 /* Since we will be writing into the entire argument area, the
4124 map must be allocated for its entire size, not just the part that
4125 is the responsibility of the caller. */
4126 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4127 needed += reg_parm_stack_space;
4129 if (ARGS_GROW_DOWNWARD)
4130 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
4131 needed + 1);
4132 else
4133 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use, needed);
4135 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
4136 stack_usage_map = stack_usage_map_buf;
4138 if (initial_highest_arg_in_use)
4139 memcpy (stack_usage_map, initial_stack_usage_map,
4140 initial_highest_arg_in_use);
4142 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
4143 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
4144 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
4145 needed = 0;
4147 /* We must be careful to use virtual regs before they're instantiated,
4148 and real regs afterwards. Loop optimization, for example, can create
4149 new libcalls after we've instantiated the virtual regs, and if we
4150 use virtuals anyway, they won't match the rtl patterns. */
4152 if (virtuals_instantiated)
4153 argblock = plus_constant (Pmode, stack_pointer_rtx,
4154 STACK_POINTER_OFFSET);
4155 else
4156 argblock = virtual_outgoing_args_rtx;
4158 else
4160 if (!PUSH_ARGS)
4161 argblock = push_block (GEN_INT (args_size.constant), 0, 0);
4164 /* We push args individually in reverse order, perform stack alignment
4165 before the first push (the last arg). */
4166 if (argblock == 0)
4167 anti_adjust_stack (GEN_INT (args_size.constant
4168 - original_args_size.constant));
4170 argnum = nargs - 1;
4172 #ifdef REG_PARM_STACK_SPACE
4173 if (ACCUMULATE_OUTGOING_ARGS)
4175 /* The argument list is the property of the called routine and it
4176 may clobber it. If the fixed area has been used for previous
4177 parameters, we must save and restore it. */
4178 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
4179 &low_to_save, &high_to_save);
4181 #endif
4183 /* When expanding a normal call, args are stored in push order,
4184 which is the reverse of what we have here. */
4185 bool any_regs = false;
4186 for (int i = nargs; i-- > 0; )
4187 if (argvec[i].reg != NULL_RTX)
4189 targetm.calls.call_args (argvec[i].reg, NULL_TREE);
4190 any_regs = true;
4192 if (!any_regs)
4193 targetm.calls.call_args (pc_rtx, NULL_TREE);
4195 /* Push the args that need to be pushed. */
4197 have_push_fusage = false;
4199 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4200 are to be pushed. */
4201 for (count = 0; count < nargs; count++, argnum--)
4203 machine_mode mode = argvec[argnum].mode;
4204 rtx val = argvec[argnum].value;
4205 rtx reg = argvec[argnum].reg;
4206 int partial = argvec[argnum].partial;
4207 unsigned int parm_align = argvec[argnum].locate.boundary;
4208 int lower_bound = 0, upper_bound = 0, i;
4210 if (! (reg != 0 && partial == 0))
4212 rtx use;
4214 if (ACCUMULATE_OUTGOING_ARGS)
4216 /* If this is being stored into a pre-allocated, fixed-size,
4217 stack area, save any previous data at that location. */
4219 if (ARGS_GROW_DOWNWARD)
4221 /* stack_slot is negative, but we want to index stack_usage_map
4222 with positive values. */
4223 upper_bound = -argvec[argnum].locate.slot_offset.constant + 1;
4224 lower_bound = upper_bound - argvec[argnum].locate.size.constant;
4226 else
4228 lower_bound = argvec[argnum].locate.slot_offset.constant;
4229 upper_bound = lower_bound + argvec[argnum].locate.size.constant;
4232 i = lower_bound;
4233 /* Don't worry about things in the fixed argument area;
4234 it has already been saved. */
4235 if (i < reg_parm_stack_space)
4236 i = reg_parm_stack_space;
4237 while (i < upper_bound && stack_usage_map[i] == 0)
4238 i++;
4240 if (i < upper_bound)
4242 /* We need to make a save area. */
4243 unsigned int size
4244 = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
4245 machine_mode save_mode
4246 = mode_for_size (size, MODE_INT, 1);
4247 rtx adr
4248 = plus_constant (Pmode, argblock,
4249 argvec[argnum].locate.offset.constant);
4250 rtx stack_area
4251 = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
4253 if (save_mode == BLKmode)
4255 argvec[argnum].save_area
4256 = assign_stack_temp (BLKmode,
4257 argvec[argnum].locate.size.constant
4260 emit_block_move (validize_mem
4261 (copy_rtx (argvec[argnum].save_area)),
4262 stack_area,
4263 GEN_INT (argvec[argnum].locate.size.constant),
4264 BLOCK_OP_CALL_PARM);
4266 else
4268 argvec[argnum].save_area = gen_reg_rtx (save_mode);
4270 emit_move_insn (argvec[argnum].save_area, stack_area);
4275 emit_push_insn (val, mode, NULL_TREE, NULL_RTX, parm_align,
4276 partial, reg, 0, argblock,
4277 GEN_INT (argvec[argnum].locate.offset.constant),
4278 reg_parm_stack_space,
4279 ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad));
4281 /* Now mark the segment we just used. */
4282 if (ACCUMULATE_OUTGOING_ARGS)
4283 for (i = lower_bound; i < upper_bound; i++)
4284 stack_usage_map[i] = 1;
4286 NO_DEFER_POP;
4288 /* Indicate argument access so that alias.c knows that these
4289 values are live. */
4290 if (argblock)
4291 use = plus_constant (Pmode, argblock,
4292 argvec[argnum].locate.offset.constant);
4293 else if (have_push_fusage)
4294 continue;
4295 else
4297 /* When arguments are pushed, trying to tell alias.c where
4298 exactly this argument is won't work, because the
4299 auto-increment causes confusion. So we merely indicate
4300 that we access something with a known mode somewhere on
4301 the stack. */
4302 use = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4303 gen_rtx_SCRATCH (Pmode));
4304 have_push_fusage = true;
4306 use = gen_rtx_MEM (argvec[argnum].mode, use);
4307 use = gen_rtx_USE (VOIDmode, use);
4308 call_fusage = gen_rtx_EXPR_LIST (VOIDmode, use, call_fusage);
4312 argnum = nargs - 1;
4314 fun = prepare_call_address (NULL, fun, NULL, &call_fusage, 0, 0);
4316 /* Now load any reg parms into their regs. */
4318 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4319 are to be pushed. */
4320 for (count = 0; count < nargs; count++, argnum--)
4322 machine_mode mode = argvec[argnum].mode;
4323 rtx val = argvec[argnum].value;
4324 rtx reg = argvec[argnum].reg;
4325 int partial = argvec[argnum].partial;
4326 #ifdef BLOCK_REG_PADDING
4327 int size = 0;
4328 #endif
4330 /* Handle calls that pass values in multiple non-contiguous
4331 locations. The PA64 has examples of this for library calls. */
4332 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4333 emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (mode));
4334 else if (reg != 0 && partial == 0)
4336 emit_move_insn (reg, val);
4337 #ifdef BLOCK_REG_PADDING
4338 size = GET_MODE_SIZE (argvec[argnum].mode);
4340 /* Copied from load_register_parameters. */
4342 /* Handle case where we have a value that needs shifting
4343 up to the msb. eg. a QImode value and we're padding
4344 upward on a BYTES_BIG_ENDIAN machine. */
4345 if (size < UNITS_PER_WORD
4346 && (argvec[argnum].locate.where_pad
4347 == (BYTES_BIG_ENDIAN ? upward : downward)))
4349 rtx x;
4350 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
4352 /* Assigning REG here rather than a temp makes CALL_FUSAGE
4353 report the whole reg as used. Strictly speaking, the
4354 call only uses SIZE bytes at the msb end, but it doesn't
4355 seem worth generating rtl to say that. */
4356 reg = gen_rtx_REG (word_mode, REGNO (reg));
4357 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
4358 if (x != reg)
4359 emit_move_insn (reg, x);
4361 #endif
4364 NO_DEFER_POP;
4367 /* Any regs containing parms remain in use through the call. */
4368 for (count = 0; count < nargs; count++)
4370 rtx reg = argvec[count].reg;
4371 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4372 use_group_regs (&call_fusage, reg);
4373 else if (reg != 0)
4375 int partial = argvec[count].partial;
4376 if (partial)
4378 int nregs;
4379 gcc_assert (partial % UNITS_PER_WORD == 0);
4380 nregs = partial / UNITS_PER_WORD;
4381 use_regs (&call_fusage, REGNO (reg), nregs);
4383 else
4384 use_reg (&call_fusage, reg);
4388 /* Pass the function the address in which to return a structure value. */
4389 if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
4391 emit_move_insn (struct_value,
4392 force_reg (Pmode,
4393 force_operand (XEXP (mem_value, 0),
4394 NULL_RTX)));
4395 if (REG_P (struct_value))
4396 use_reg (&call_fusage, struct_value);
4399 /* Don't allow popping to be deferred, since then
4400 cse'ing of library calls could delete a call and leave the pop. */
4401 NO_DEFER_POP;
4402 valreg = (mem_value == 0 && outmode != VOIDmode
4403 ? hard_libcall_value (outmode, orgfun) : NULL_RTX);
4405 /* Stack must be properly aligned now. */
4406 gcc_assert (!(stack_pointer_delta
4407 & (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1)));
4409 before_call = get_last_insn ();
4411 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
4412 will set inhibit_defer_pop to that value. */
4413 /* The return type is needed to decide how many bytes the function pops.
4414 Signedness plays no role in that, so for simplicity, we pretend it's
4415 always signed. We also assume that the list of arguments passed has
4416 no impact, so we pretend it is unknown. */
4418 emit_call_1 (fun, NULL,
4419 get_identifier (XSTR (orgfun, 0)),
4420 build_function_type (tfom, NULL_TREE),
4421 original_args_size.constant, args_size.constant,
4422 struct_value_size,
4423 targetm.calls.function_arg (args_so_far,
4424 VOIDmode, void_type_node, true),
4425 valreg,
4426 old_inhibit_defer_pop + 1, call_fusage, flags, args_so_far);
4428 if (flag_ipa_ra)
4430 rtx last, datum = orgfun;
4431 gcc_assert (GET_CODE (datum) == SYMBOL_REF);
4432 last = last_call_insn ();
4433 add_reg_note (last, REG_CALL_DECL, datum);
4436 /* Right-shift returned value if necessary. */
4437 if (!pcc_struct_value
4438 && TYPE_MODE (tfom) != BLKmode
4439 && targetm.calls.return_in_msb (tfom))
4441 shift_return_value (TYPE_MODE (tfom), false, valreg);
4442 valreg = gen_rtx_REG (TYPE_MODE (tfom), REGNO (valreg));
4445 targetm.calls.end_call_args ();
4447 /* For calls to `setjmp', etc., inform function.c:setjmp_warnings
4448 that it should complain if nonvolatile values are live. For
4449 functions that cannot return, inform flow that control does not
4450 fall through. */
4451 if (flags & ECF_NORETURN)
4453 /* The barrier note must be emitted
4454 immediately after the CALL_INSN. Some ports emit more than
4455 just a CALL_INSN above, so we must search for it here. */
4456 rtx_insn *last = get_last_insn ();
4457 while (!CALL_P (last))
4459 last = PREV_INSN (last);
4460 /* There was no CALL_INSN? */
4461 gcc_assert (last != before_call);
4464 emit_barrier_after (last);
4467 /* Consider that "regular" libcalls, i.e. all of them except for LCT_THROW
4468 and LCT_RETURNS_TWICE, cannot perform non-local gotos. */
4469 if (flags & ECF_NOTHROW)
4471 rtx_insn *last = get_last_insn ();
4472 while (!CALL_P (last))
4474 last = PREV_INSN (last);
4475 /* There was no CALL_INSN? */
4476 gcc_assert (last != before_call);
4479 make_reg_eh_region_note_nothrow_nononlocal (last);
4482 /* Now restore inhibit_defer_pop to its actual original value. */
4483 OK_DEFER_POP;
4485 pop_temp_slots ();
4487 /* Copy the value to the right place. */
4488 if (outmode != VOIDmode && retval)
4490 if (mem_value)
4492 if (value == 0)
4493 value = mem_value;
4494 if (value != mem_value)
4495 emit_move_insn (value, mem_value);
4497 else if (GET_CODE (valreg) == PARALLEL)
4499 if (value == 0)
4500 value = gen_reg_rtx (outmode);
4501 emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
4503 else
4505 /* Convert to the proper mode if a promotion has been active. */
4506 if (GET_MODE (valreg) != outmode)
4508 int unsignedp = TYPE_UNSIGNED (tfom);
4510 gcc_assert (promote_function_mode (tfom, outmode, &unsignedp,
4511 fndecl ? TREE_TYPE (fndecl) : fntype, 1)
4512 == GET_MODE (valreg));
4513 valreg = convert_modes (outmode, GET_MODE (valreg), valreg, 0);
4516 if (value != 0)
4517 emit_move_insn (value, valreg);
4518 else
4519 value = valreg;
4523 if (ACCUMULATE_OUTGOING_ARGS)
4525 #ifdef REG_PARM_STACK_SPACE
4526 if (save_area)
4527 restore_fixed_argument_area (save_area, argblock,
4528 high_to_save, low_to_save);
4529 #endif
4531 /* If we saved any argument areas, restore them. */
4532 for (count = 0; count < nargs; count++)
4533 if (argvec[count].save_area)
4535 machine_mode save_mode = GET_MODE (argvec[count].save_area);
4536 rtx adr = plus_constant (Pmode, argblock,
4537 argvec[count].locate.offset.constant);
4538 rtx stack_area = gen_rtx_MEM (save_mode,
4539 memory_address (save_mode, adr));
4541 if (save_mode == BLKmode)
4542 emit_block_move (stack_area,
4543 validize_mem
4544 (copy_rtx (argvec[count].save_area)),
4545 GEN_INT (argvec[count].locate.size.constant),
4546 BLOCK_OP_CALL_PARM);
4547 else
4548 emit_move_insn (stack_area, argvec[count].save_area);
4551 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
4552 stack_usage_map = initial_stack_usage_map;
4555 free (stack_usage_map_buf);
4557 return value;
4561 /* Output a library call to function FUN (a SYMBOL_REF rtx)
4562 (emitting the queue unless NO_QUEUE is nonzero),
4563 for a value of mode OUTMODE,
4564 with NARGS different arguments, passed as alternating rtx values
4565 and machine_modes to convert them to.
4567 FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for
4568 `const' calls, LCT_PURE for `pure' calls, or other LCT_ value for
4569 other types of library calls. */
4571 void
4572 emit_library_call (rtx orgfun, enum libcall_type fn_type,
4573 machine_mode outmode, int nargs, ...)
4575 va_list p;
4577 va_start (p, nargs);
4578 emit_library_call_value_1 (0, orgfun, NULL_RTX, fn_type, outmode, nargs, p);
4579 va_end (p);
4582 /* Like emit_library_call except that an extra argument, VALUE,
4583 comes second and says where to store the result.
4584 (If VALUE is zero, this function chooses a convenient way
4585 to return the value.
4587 This function returns an rtx for where the value is to be found.
4588 If VALUE is nonzero, VALUE is returned. */
4591 emit_library_call_value (rtx orgfun, rtx value,
4592 enum libcall_type fn_type,
4593 machine_mode outmode, int nargs, ...)
4595 rtx result;
4596 va_list p;
4598 va_start (p, nargs);
4599 result = emit_library_call_value_1 (1, orgfun, value, fn_type, outmode,
4600 nargs, p);
4601 va_end (p);
4603 return result;
4607 /* Store pointer bounds argument ARG into Bounds Table entry
4608 associated with PARM. */
4609 static void
4610 store_bounds (struct arg_data *arg, struct arg_data *parm)
4612 rtx slot = NULL, ptr = NULL, addr = NULL;
4614 /* We may pass bounds not associated with any pointer. */
4615 if (!parm)
4617 gcc_assert (arg->special_slot);
4618 slot = arg->special_slot;
4619 ptr = const0_rtx;
4621 /* Find pointer associated with bounds and where it is
4622 passed. */
4623 else
4625 if (!parm->reg)
4627 gcc_assert (!arg->special_slot);
4629 addr = adjust_address (parm->stack, Pmode, arg->pointer_offset);
4631 else if (REG_P (parm->reg))
4633 gcc_assert (arg->special_slot);
4634 slot = arg->special_slot;
4636 if (MEM_P (parm->value))
4637 addr = adjust_address (parm->value, Pmode, arg->pointer_offset);
4638 else if (REG_P (parm->value))
4639 ptr = gen_rtx_SUBREG (Pmode, parm->value, arg->pointer_offset);
4640 else
4642 gcc_assert (!arg->pointer_offset);
4643 ptr = parm->value;
4646 else
4648 gcc_assert (GET_CODE (parm->reg) == PARALLEL);
4650 gcc_assert (arg->special_slot);
4651 slot = arg->special_slot;
4653 if (parm->parallel_value)
4654 ptr = chkp_get_value_with_offs (parm->parallel_value,
4655 GEN_INT (arg->pointer_offset));
4656 else
4657 gcc_unreachable ();
4661 /* Expand bounds. */
4662 if (!arg->value)
4663 arg->value = expand_normal (arg->tree_value);
4665 targetm.calls.store_bounds_for_arg (ptr, addr, arg->value, slot);
4668 /* Store a single argument for a function call
4669 into the register or memory area where it must be passed.
4670 *ARG describes the argument value and where to pass it.
4672 ARGBLOCK is the address of the stack-block for all the arguments,
4673 or 0 on a machine where arguments are pushed individually.
4675 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
4676 so must be careful about how the stack is used.
4678 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
4679 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
4680 that we need not worry about saving and restoring the stack.
4682 FNDECL is the declaration of the function we are calling.
4684 Return nonzero if this arg should cause sibcall failure,
4685 zero otherwise. */
4687 static int
4688 store_one_arg (struct arg_data *arg, rtx argblock, int flags,
4689 int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
4691 tree pval = arg->tree_value;
4692 rtx reg = 0;
4693 int partial = 0;
4694 int used = 0;
4695 int i, lower_bound = 0, upper_bound = 0;
4696 int sibcall_failure = 0;
4698 if (TREE_CODE (pval) == ERROR_MARK)
4699 return 1;
4701 /* Push a new temporary level for any temporaries we make for
4702 this argument. */
4703 push_temp_slots ();
4705 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
4707 /* If this is being stored into a pre-allocated, fixed-size, stack area,
4708 save any previous data at that location. */
4709 if (argblock && ! variable_size && arg->stack)
4711 if (ARGS_GROW_DOWNWARD)
4713 /* stack_slot is negative, but we want to index stack_usage_map
4714 with positive values. */
4715 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4716 upper_bound = -INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1)) + 1;
4717 else
4718 upper_bound = 0;
4720 lower_bound = upper_bound - arg->locate.size.constant;
4722 else
4724 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4725 lower_bound = INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1));
4726 else
4727 lower_bound = 0;
4729 upper_bound = lower_bound + arg->locate.size.constant;
4732 i = lower_bound;
4733 /* Don't worry about things in the fixed argument area;
4734 it has already been saved. */
4735 if (i < reg_parm_stack_space)
4736 i = reg_parm_stack_space;
4737 while (i < upper_bound && stack_usage_map[i] == 0)
4738 i++;
4740 if (i < upper_bound)
4742 /* We need to make a save area. */
4743 unsigned int size = arg->locate.size.constant * BITS_PER_UNIT;
4744 machine_mode save_mode = mode_for_size (size, MODE_INT, 1);
4745 rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
4746 rtx stack_area = gen_rtx_MEM (save_mode, adr);
4748 if (save_mode == BLKmode)
4750 arg->save_area
4751 = assign_temp (TREE_TYPE (arg->tree_value), 1, 1);
4752 preserve_temp_slots (arg->save_area);
4753 emit_block_move (validize_mem (copy_rtx (arg->save_area)),
4754 stack_area,
4755 GEN_INT (arg->locate.size.constant),
4756 BLOCK_OP_CALL_PARM);
4758 else
4760 arg->save_area = gen_reg_rtx (save_mode);
4761 emit_move_insn (arg->save_area, stack_area);
4767 /* If this isn't going to be placed on both the stack and in registers,
4768 set up the register and number of words. */
4769 if (! arg->pass_on_stack)
4771 if (flags & ECF_SIBCALL)
4772 reg = arg->tail_call_reg;
4773 else
4774 reg = arg->reg;
4775 partial = arg->partial;
4778 /* Being passed entirely in a register. We shouldn't be called in
4779 this case. */
4780 gcc_assert (reg == 0 || partial != 0);
4782 /* If this arg needs special alignment, don't load the registers
4783 here. */
4784 if (arg->n_aligned_regs != 0)
4785 reg = 0;
4787 /* If this is being passed partially in a register, we can't evaluate
4788 it directly into its stack slot. Otherwise, we can. */
4789 if (arg->value == 0)
4791 /* stack_arg_under_construction is nonzero if a function argument is
4792 being evaluated directly into the outgoing argument list and
4793 expand_call must take special action to preserve the argument list
4794 if it is called recursively.
4796 For scalar function arguments stack_usage_map is sufficient to
4797 determine which stack slots must be saved and restored. Scalar
4798 arguments in general have pass_on_stack == 0.
4800 If this argument is initialized by a function which takes the
4801 address of the argument (a C++ constructor or a C function
4802 returning a BLKmode structure), then stack_usage_map is
4803 insufficient and expand_call must push the stack around the
4804 function call. Such arguments have pass_on_stack == 1.
4806 Note that it is always safe to set stack_arg_under_construction,
4807 but this generates suboptimal code if set when not needed. */
4809 if (arg->pass_on_stack)
4810 stack_arg_under_construction++;
4812 arg->value = expand_expr (pval,
4813 (partial
4814 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
4815 ? NULL_RTX : arg->stack,
4816 VOIDmode, EXPAND_STACK_PARM);
4818 /* If we are promoting object (or for any other reason) the mode
4819 doesn't agree, convert the mode. */
4821 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
4822 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
4823 arg->value, arg->unsignedp);
4825 if (arg->pass_on_stack)
4826 stack_arg_under_construction--;
4829 /* Check for overlap with already clobbered argument area. */
4830 if ((flags & ECF_SIBCALL)
4831 && MEM_P (arg->value)
4832 && mem_overlaps_already_clobbered_arg_p (XEXP (arg->value, 0),
4833 arg->locate.size.constant))
4834 sibcall_failure = 1;
4836 /* Don't allow anything left on stack from computation
4837 of argument to alloca. */
4838 if (flags & ECF_MAY_BE_ALLOCA)
4839 do_pending_stack_adjust ();
4841 if (arg->value == arg->stack)
4842 /* If the value is already in the stack slot, we are done. */
4844 else if (arg->mode != BLKmode)
4846 int size;
4847 unsigned int parm_align;
4849 /* Argument is a scalar, not entirely passed in registers.
4850 (If part is passed in registers, arg->partial says how much
4851 and emit_push_insn will take care of putting it there.)
4853 Push it, and if its size is less than the
4854 amount of space allocated to it,
4855 also bump stack pointer by the additional space.
4856 Note that in C the default argument promotions
4857 will prevent such mismatches. */
4859 size = GET_MODE_SIZE (arg->mode);
4860 /* Compute how much space the push instruction will push.
4861 On many machines, pushing a byte will advance the stack
4862 pointer by a halfword. */
4863 #ifdef PUSH_ROUNDING
4864 size = PUSH_ROUNDING (size);
4865 #endif
4866 used = size;
4868 /* Compute how much space the argument should get:
4869 round up to a multiple of the alignment for arguments. */
4870 if (none != FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)))
4871 used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
4872 / (PARM_BOUNDARY / BITS_PER_UNIT))
4873 * (PARM_BOUNDARY / BITS_PER_UNIT));
4875 /* Compute the alignment of the pushed argument. */
4876 parm_align = arg->locate.boundary;
4877 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
4879 int pad = used - size;
4880 if (pad)
4882 unsigned int pad_align = (pad & -pad) * BITS_PER_UNIT;
4883 parm_align = MIN (parm_align, pad_align);
4887 /* This isn't already where we want it on the stack, so put it there.
4888 This can either be done with push or copy insns. */
4889 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), NULL_RTX,
4890 parm_align, partial, reg, used - size, argblock,
4891 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4892 ARGS_SIZE_RTX (arg->locate.alignment_pad));
4894 /* Unless this is a partially-in-register argument, the argument is now
4895 in the stack. */
4896 if (partial == 0)
4897 arg->value = arg->stack;
4899 else
4901 /* BLKmode, at least partly to be pushed. */
4903 unsigned int parm_align;
4904 int excess;
4905 rtx size_rtx;
4907 /* Pushing a nonscalar.
4908 If part is passed in registers, PARTIAL says how much
4909 and emit_push_insn will take care of putting it there. */
4911 /* Round its size up to a multiple
4912 of the allocation unit for arguments. */
4914 if (arg->locate.size.var != 0)
4916 excess = 0;
4917 size_rtx = ARGS_SIZE_RTX (arg->locate.size);
4919 else
4921 /* PUSH_ROUNDING has no effect on us, because emit_push_insn
4922 for BLKmode is careful to avoid it. */
4923 excess = (arg->locate.size.constant
4924 - int_size_in_bytes (TREE_TYPE (pval))
4925 + partial);
4926 size_rtx = expand_expr (size_in_bytes (TREE_TYPE (pval)),
4927 NULL_RTX, TYPE_MODE (sizetype),
4928 EXPAND_NORMAL);
4931 parm_align = arg->locate.boundary;
4933 /* When an argument is padded down, the block is aligned to
4934 PARM_BOUNDARY, but the actual argument isn't. */
4935 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
4937 if (arg->locate.size.var)
4938 parm_align = BITS_PER_UNIT;
4939 else if (excess)
4941 unsigned int excess_align = (excess & -excess) * BITS_PER_UNIT;
4942 parm_align = MIN (parm_align, excess_align);
4946 if ((flags & ECF_SIBCALL) && MEM_P (arg->value))
4948 /* emit_push_insn might not work properly if arg->value and
4949 argblock + arg->locate.offset areas overlap. */
4950 rtx x = arg->value;
4951 int i = 0;
4953 if (XEXP (x, 0) == crtl->args.internal_arg_pointer
4954 || (GET_CODE (XEXP (x, 0)) == PLUS
4955 && XEXP (XEXP (x, 0), 0) ==
4956 crtl->args.internal_arg_pointer
4957 && CONST_INT_P (XEXP (XEXP (x, 0), 1))))
4959 if (XEXP (x, 0) != crtl->args.internal_arg_pointer)
4960 i = INTVAL (XEXP (XEXP (x, 0), 1));
4962 /* expand_call should ensure this. */
4963 gcc_assert (!arg->locate.offset.var
4964 && arg->locate.size.var == 0
4965 && CONST_INT_P (size_rtx));
4967 if (arg->locate.offset.constant > i)
4969 if (arg->locate.offset.constant < i + INTVAL (size_rtx))
4970 sibcall_failure = 1;
4972 else if (arg->locate.offset.constant < i)
4974 /* Use arg->locate.size.constant instead of size_rtx
4975 because we only care about the part of the argument
4976 on the stack. */
4977 if (i < (arg->locate.offset.constant
4978 + arg->locate.size.constant))
4979 sibcall_failure = 1;
4981 else
4983 /* Even though they appear to be at the same location,
4984 if part of the outgoing argument is in registers,
4985 they aren't really at the same location. Check for
4986 this by making sure that the incoming size is the
4987 same as the outgoing size. */
4988 if (arg->locate.size.constant != INTVAL (size_rtx))
4989 sibcall_failure = 1;
4994 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
4995 parm_align, partial, reg, excess, argblock,
4996 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4997 ARGS_SIZE_RTX (arg->locate.alignment_pad));
4999 /* Unless this is a partially-in-register argument, the argument is now
5000 in the stack.
5002 ??? Unlike the case above, in which we want the actual
5003 address of the data, so that we can load it directly into a
5004 register, here we want the address of the stack slot, so that
5005 it's properly aligned for word-by-word copying or something
5006 like that. It's not clear that this is always correct. */
5007 if (partial == 0)
5008 arg->value = arg->stack_slot;
5011 if (arg->reg && GET_CODE (arg->reg) == PARALLEL)
5013 tree type = TREE_TYPE (arg->tree_value);
5014 arg->parallel_value
5015 = emit_group_load_into_temps (arg->reg, arg->value, type,
5016 int_size_in_bytes (type));
5019 /* Mark all slots this store used. */
5020 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
5021 && argblock && ! variable_size && arg->stack)
5022 for (i = lower_bound; i < upper_bound; i++)
5023 stack_usage_map[i] = 1;
5025 /* Once we have pushed something, pops can't safely
5026 be deferred during the rest of the arguments. */
5027 NO_DEFER_POP;
5029 /* Free any temporary slots made in processing this argument. */
5030 pop_temp_slots ();
5032 return sibcall_failure;
5035 /* Nonzero if we do not know how to pass TYPE solely in registers. */
5037 bool
5038 must_pass_in_stack_var_size (machine_mode mode ATTRIBUTE_UNUSED,
5039 const_tree type)
5041 if (!type)
5042 return false;
5044 /* If the type has variable size... */
5045 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5046 return true;
5048 /* If the type is marked as addressable (it is required
5049 to be constructed into the stack)... */
5050 if (TREE_ADDRESSABLE (type))
5051 return true;
5053 return false;
5056 /* Another version of the TARGET_MUST_PASS_IN_STACK hook. This one
5057 takes trailing padding of a structure into account. */
5058 /* ??? Should be able to merge these two by examining BLOCK_REG_PADDING. */
5060 bool
5061 must_pass_in_stack_var_size_or_pad (machine_mode mode, const_tree type)
5063 if (!type)
5064 return false;
5066 /* If the type has variable size... */
5067 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5068 return true;
5070 /* If the type is marked as addressable (it is required
5071 to be constructed into the stack)... */
5072 if (TREE_ADDRESSABLE (type))
5073 return true;
5075 /* If the padding and mode of the type is such that a copy into
5076 a register would put it into the wrong part of the register. */
5077 if (mode == BLKmode
5078 && int_size_in_bytes (type) % (PARM_BOUNDARY / BITS_PER_UNIT)
5079 && (FUNCTION_ARG_PADDING (mode, type)
5080 == (BYTES_BIG_ENDIAN ? upward : downward)))
5081 return true;
5083 return false;