runtime: GOARCH values for ppc64 BE & LE
[official-gcc.git] / gcc / calls.c
blobc64c0eb6bf7cc01a8274a9dcd38566cf98a12a2b
1 /* Convert function calls to rtl insns, for GNU C compiler.
2 Copyright (C) 1989-2014 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 "tree.h"
26 #include "stor-layout.h"
27 #include "varasm.h"
28 #include "stringpool.h"
29 #include "attribs.h"
30 #include "predict.h"
31 #include "vec.h"
32 #include "hashtab.h"
33 #include "hash-set.h"
34 #include "machmode.h"
35 #include "hard-reg-set.h"
36 #include "input.h"
37 #include "function.h"
38 #include "basic-block.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-expr.h"
42 #include "is-a.h"
43 #include "gimple.h"
44 #include "flags.h"
45 #include "expr.h"
46 #include "insn-codes.h"
47 #include "optabs.h"
48 #include "libfuncs.h"
49 #include "regs.h"
50 #include "diagnostic-core.h"
51 #include "output.h"
52 #include "tm_p.h"
53 #include "timevar.h"
54 #include "sbitmap.h"
55 #include "bitmap.h"
56 #include "langhooks.h"
57 #include "target.h"
58 #include "hash-map.h"
59 #include "plugin-api.h"
60 #include "ipa-ref.h"
61 #include "cgraph.h"
62 #include "except.h"
63 #include "dbgcnt.h"
64 #include "rtl-iter.h"
65 #include "tree-chkp.h"
66 #include "rtl-chkp.h"
68 /* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits. */
69 #define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
71 /* Data structure and subroutines used within expand_call. */
73 struct arg_data
75 /* Tree node for this argument. */
76 tree tree_value;
77 /* Mode for value; TYPE_MODE unless promoted. */
78 machine_mode mode;
79 /* Current RTL value for argument, or 0 if it isn't precomputed. */
80 rtx value;
81 /* Initially-compute RTL value for argument; only for const functions. */
82 rtx initial_value;
83 /* Register to pass this argument in, 0 if passed on stack, or an
84 PARALLEL if the arg is to be copied into multiple non-contiguous
85 registers. */
86 rtx reg;
87 /* Register to pass this argument in when generating tail call sequence.
88 This is not the same register as for normal calls on machines with
89 register windows. */
90 rtx tail_call_reg;
91 /* If REG is a PARALLEL, this is a copy of VALUE pulled into the correct
92 form for emit_group_move. */
93 rtx parallel_value;
94 /* If value is passed in neither reg nor stack, this field holds a number
95 of a special slot to be used. */
96 rtx special_slot;
97 /* For pointer bounds hold an index of parm bounds are bound to. -1 if
98 there is no such pointer. */
99 int pointer_arg;
100 /* If pointer_arg refers a structure, then pointer_offset holds an offset
101 of a pointer in this structure. */
102 int pointer_offset;
103 /* If REG was promoted from the actual mode of the argument expression,
104 indicates whether the promotion is sign- or zero-extended. */
105 int unsignedp;
106 /* Number of bytes to put in registers. 0 means put the whole arg
107 in registers. Also 0 if not passed in registers. */
108 int partial;
109 /* Nonzero if argument must be passed on stack.
110 Note that some arguments may be passed on the stack
111 even though pass_on_stack is zero, just because FUNCTION_ARG says so.
112 pass_on_stack identifies arguments that *cannot* go in registers. */
113 int pass_on_stack;
114 /* Some fields packaged up for locate_and_pad_parm. */
115 struct locate_and_pad_arg_data locate;
116 /* Location on the stack at which parameter should be stored. The store
117 has already been done if STACK == VALUE. */
118 rtx stack;
119 /* Location on the stack of the start of this argument slot. This can
120 differ from STACK if this arg pads downward. This location is known
121 to be aligned to TARGET_FUNCTION_ARG_BOUNDARY. */
122 rtx stack_slot;
123 /* Place that this stack area has been saved, if needed. */
124 rtx save_area;
125 /* If an argument's alignment does not permit direct copying into registers,
126 copy in smaller-sized pieces into pseudos. These are stored in a
127 block pointed to by this field. The next field says how many
128 word-sized pseudos we made. */
129 rtx *aligned_regs;
130 int n_aligned_regs;
133 /* A vector of one char per byte of stack space. A byte if nonzero if
134 the corresponding stack location has been used.
135 This vector is used to prevent a function call within an argument from
136 clobbering any stack already set up. */
137 static char *stack_usage_map;
139 /* Size of STACK_USAGE_MAP. */
140 static int highest_outgoing_arg_in_use;
142 /* A bitmap of virtual-incoming stack space. Bit is set if the corresponding
143 stack location's tail call argument has been already stored into the stack.
144 This bitmap is used to prevent sibling call optimization if function tries
145 to use parent's incoming argument slots when they have been already
146 overwritten with tail call arguments. */
147 static sbitmap stored_args_map;
149 /* stack_arg_under_construction is nonzero when an argument may be
150 initialized with a constructor call (including a C function that
151 returns a BLKmode struct) and expand_call must take special action
152 to make sure the object being constructed does not overlap the
153 argument list for the constructor call. */
154 static int stack_arg_under_construction;
156 static void emit_call_1 (rtx, tree, tree, tree, HOST_WIDE_INT, HOST_WIDE_INT,
157 HOST_WIDE_INT, rtx, rtx, int, rtx, int,
158 cumulative_args_t);
159 static void precompute_register_parameters (int, struct arg_data *, int *);
160 static void store_bounds (struct arg_data *, struct arg_data *);
161 static int store_one_arg (struct arg_data *, rtx, int, int, int);
162 static void store_unaligned_arguments_into_pseudos (struct arg_data *, int);
163 static int finalize_must_preallocate (int, int, struct arg_data *,
164 struct args_size *);
165 static void precompute_arguments (int, struct arg_data *);
166 static int compute_argument_block_size (int, struct args_size *, tree, tree, int);
167 static void initialize_argument_information (int, struct arg_data *,
168 struct args_size *, int,
169 tree, tree,
170 tree, tree, cumulative_args_t, int,
171 rtx *, int *, int *, int *,
172 bool *, bool);
173 static void compute_argument_addresses (struct arg_data *, rtx, int);
174 static rtx rtx_for_function_call (tree, tree);
175 static void load_register_parameters (struct arg_data *, int, rtx *, int,
176 int, int *);
177 static rtx emit_library_call_value_1 (int, rtx, rtx, enum libcall_type,
178 machine_mode, int, va_list);
179 static int special_function_p (const_tree, int);
180 static int check_sibcall_argument_overlap_1 (rtx);
181 static int check_sibcall_argument_overlap (rtx_insn *, struct arg_data *, int);
183 static int combine_pending_stack_adjustment_and_call (int, struct args_size *,
184 unsigned int);
185 static tree split_complex_types (tree);
187 #ifdef REG_PARM_STACK_SPACE
188 static rtx save_fixed_argument_area (int, rtx, int *, int *);
189 static void restore_fixed_argument_area (rtx, rtx, int, int);
190 #endif
192 /* Force FUNEXP into a form suitable for the address of a CALL,
193 and return that as an rtx. Also load the static chain register
194 if FNDECL is a nested function.
196 CALL_FUSAGE points to a variable holding the prospective
197 CALL_INSN_FUNCTION_USAGE information. */
200 prepare_call_address (tree fndecl_or_type, rtx funexp, rtx static_chain_value,
201 rtx *call_fusage, int reg_parm_seen, int sibcallp)
203 /* Make a valid memory address and copy constants through pseudo-regs,
204 but not for a constant address if -fno-function-cse. */
205 if (GET_CODE (funexp) != SYMBOL_REF)
206 /* If we are using registers for parameters, force the
207 function address into a register now. */
208 funexp = ((reg_parm_seen
209 && targetm.small_register_classes_for_mode_p (FUNCTION_MODE))
210 ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
211 : memory_address (FUNCTION_MODE, funexp));
212 else if (! sibcallp)
214 #ifndef NO_FUNCTION_CSE
215 if (optimize && ! flag_no_function_cse)
216 funexp = force_reg (Pmode, funexp);
217 #endif
220 if (static_chain_value != 0
221 && (TREE_CODE (fndecl_or_type) != FUNCTION_DECL
222 || DECL_STATIC_CHAIN (fndecl_or_type)))
224 rtx chain;
226 chain = targetm.calls.static_chain (fndecl_or_type, false);
227 static_chain_value = convert_memory_address (Pmode, static_chain_value);
229 emit_move_insn (chain, static_chain_value);
230 if (REG_P (chain))
231 use_reg (call_fusage, chain);
234 return funexp;
237 /* Generate instructions to call function FUNEXP,
238 and optionally pop the results.
239 The CALL_INSN is the first insn generated.
241 FNDECL is the declaration node of the function. This is given to the
242 hook TARGET_RETURN_POPS_ARGS to determine whether this function pops
243 its own args.
245 FUNTYPE is the data type of the function. This is given to the hook
246 TARGET_RETURN_POPS_ARGS to determine whether this function pops its
247 own args. We used to allow an identifier for library functions, but
248 that doesn't work when the return type is an aggregate type and the
249 calling convention says that the pointer to this aggregate is to be
250 popped by the callee.
252 STACK_SIZE is the number of bytes of arguments on the stack,
253 ROUNDED_STACK_SIZE is that number rounded up to
254 PREFERRED_STACK_BOUNDARY; zero if the size is variable. This is
255 both to put into the call insn and to generate explicit popping
256 code if necessary.
258 STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
259 It is zero if this call doesn't want a structure value.
261 NEXT_ARG_REG is the rtx that results from executing
262 targetm.calls.function_arg (&args_so_far, VOIDmode, void_type_node, true)
263 just after all the args have had their registers assigned.
264 This could be whatever you like, but normally it is the first
265 arg-register beyond those used for args in this call,
266 or 0 if all the arg-registers are used in this call.
267 It is passed on to `gen_call' so you can put this info in the call insn.
269 VALREG is a hard register in which a value is returned,
270 or 0 if the call does not return a value.
272 OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
273 the args to this call were processed.
274 We restore `inhibit_defer_pop' to that value.
276 CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
277 denote registers used by the called function. */
279 static void
280 emit_call_1 (rtx funexp, tree fntree ATTRIBUTE_UNUSED, tree fndecl ATTRIBUTE_UNUSED,
281 tree funtype ATTRIBUTE_UNUSED,
282 HOST_WIDE_INT stack_size ATTRIBUTE_UNUSED,
283 HOST_WIDE_INT rounded_stack_size,
284 HOST_WIDE_INT struct_value_size ATTRIBUTE_UNUSED,
285 rtx next_arg_reg ATTRIBUTE_UNUSED, rtx valreg,
286 int old_inhibit_defer_pop, rtx call_fusage, int ecf_flags,
287 cumulative_args_t args_so_far ATTRIBUTE_UNUSED)
289 rtx rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
290 rtx_insn *call_insn;
291 rtx call, funmem;
292 int already_popped = 0;
293 HOST_WIDE_INT n_popped
294 = targetm.calls.return_pops_args (fndecl, funtype, stack_size);
296 #ifdef CALL_POPS_ARGS
297 n_popped += CALL_POPS_ARGS (*get_cumulative_args (args_so_far));
298 #endif
300 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
301 and we don't want to load it into a register as an optimization,
302 because prepare_call_address already did it if it should be done. */
303 if (GET_CODE (funexp) != SYMBOL_REF)
304 funexp = memory_address (FUNCTION_MODE, funexp);
306 funmem = gen_rtx_MEM (FUNCTION_MODE, funexp);
307 if (fndecl && TREE_CODE (fndecl) == FUNCTION_DECL)
309 tree t = fndecl;
311 /* Although a built-in FUNCTION_DECL and its non-__builtin
312 counterpart compare equal and get a shared mem_attrs, they
313 produce different dump output in compare-debug compilations,
314 if an entry gets garbage collected in one compilation, then
315 adds a different (but equivalent) entry, while the other
316 doesn't run the garbage collector at the same spot and then
317 shares the mem_attr with the equivalent entry. */
318 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
320 tree t2 = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
321 if (t2)
322 t = t2;
325 set_mem_expr (funmem, t);
327 else if (fntree)
328 set_mem_expr (funmem, build_simple_mem_ref (CALL_EXPR_FN (fntree)));
330 #if defined (HAVE_sibcall_pop) && defined (HAVE_sibcall_value_pop)
331 if ((ecf_flags & ECF_SIBCALL)
332 && HAVE_sibcall_pop && HAVE_sibcall_value_pop
333 && (n_popped > 0 || stack_size == 0))
335 rtx n_pop = GEN_INT (n_popped);
336 rtx pat;
338 /* If this subroutine pops its own args, record that in the call insn
339 if possible, for the sake of frame pointer elimination. */
341 if (valreg)
342 pat = GEN_SIBCALL_VALUE_POP (valreg, funmem, rounded_stack_size_rtx,
343 next_arg_reg, n_pop);
344 else
345 pat = GEN_SIBCALL_POP (funmem, rounded_stack_size_rtx, next_arg_reg,
346 n_pop);
348 emit_call_insn (pat);
349 already_popped = 1;
351 else
352 #endif
354 #if defined (HAVE_call_pop) && defined (HAVE_call_value_pop)
355 /* If the target has "call" or "call_value" insns, then prefer them
356 if no arguments are actually popped. If the target does not have
357 "call" or "call_value" insns, then we must use the popping versions
358 even if the call has no arguments to pop. */
359 #if defined (HAVE_call) && defined (HAVE_call_value)
360 if (HAVE_call && HAVE_call_value && HAVE_call_pop && HAVE_call_value_pop
361 && n_popped > 0)
362 #else
363 if (HAVE_call_pop && HAVE_call_value_pop)
364 #endif
366 rtx n_pop = GEN_INT (n_popped);
367 rtx pat;
369 /* If this subroutine pops its own args, record that in the call insn
370 if possible, for the sake of frame pointer elimination. */
372 if (valreg)
373 pat = GEN_CALL_VALUE_POP (valreg, funmem, rounded_stack_size_rtx,
374 next_arg_reg, n_pop);
375 else
376 pat = GEN_CALL_POP (funmem, rounded_stack_size_rtx, next_arg_reg,
377 n_pop);
379 emit_call_insn (pat);
380 already_popped = 1;
382 else
383 #endif
385 #if defined (HAVE_sibcall) && defined (HAVE_sibcall_value)
386 if ((ecf_flags & ECF_SIBCALL)
387 && HAVE_sibcall && HAVE_sibcall_value)
389 if (valreg)
390 emit_call_insn (GEN_SIBCALL_VALUE (valreg, funmem,
391 rounded_stack_size_rtx,
392 next_arg_reg, NULL_RTX));
393 else
394 emit_call_insn (GEN_SIBCALL (funmem, rounded_stack_size_rtx,
395 next_arg_reg,
396 GEN_INT (struct_value_size)));
398 else
399 #endif
401 #if defined (HAVE_call) && defined (HAVE_call_value)
402 if (HAVE_call && HAVE_call_value)
404 if (valreg)
405 emit_call_insn (GEN_CALL_VALUE (valreg, funmem, rounded_stack_size_rtx,
406 next_arg_reg, NULL_RTX));
407 else
408 emit_call_insn (GEN_CALL (funmem, rounded_stack_size_rtx, next_arg_reg,
409 GEN_INT (struct_value_size)));
411 else
412 #endif
413 gcc_unreachable ();
415 /* Find the call we just emitted. */
416 call_insn = last_call_insn ();
418 /* Some target create a fresh MEM instead of reusing the one provided
419 above. Set its MEM_EXPR. */
420 call = get_call_rtx_from (call_insn);
421 if (call
422 && MEM_EXPR (XEXP (call, 0)) == NULL_TREE
423 && MEM_EXPR (funmem) != NULL_TREE)
424 set_mem_expr (XEXP (call, 0), MEM_EXPR (funmem));
426 /* Mark instrumented calls. */
427 if (call && fntree)
428 CALL_EXPR_WITH_BOUNDS_P (call) = CALL_WITH_BOUNDS_P (fntree);
430 /* Put the register usage information there. */
431 add_function_usage_to (call_insn, call_fusage);
433 /* If this is a const call, then set the insn's unchanging bit. */
434 if (ecf_flags & ECF_CONST)
435 RTL_CONST_CALL_P (call_insn) = 1;
437 /* If this is a pure call, then set the insn's unchanging bit. */
438 if (ecf_flags & ECF_PURE)
439 RTL_PURE_CALL_P (call_insn) = 1;
441 /* If this is a const call, then set the insn's unchanging bit. */
442 if (ecf_flags & ECF_LOOPING_CONST_OR_PURE)
443 RTL_LOOPING_CONST_OR_PURE_CALL_P (call_insn) = 1;
445 /* Create a nothrow REG_EH_REGION note, if needed. */
446 make_reg_eh_region_note (call_insn, ecf_flags, 0);
448 if (ecf_flags & ECF_NORETURN)
449 add_reg_note (call_insn, REG_NORETURN, const0_rtx);
451 if (ecf_flags & ECF_RETURNS_TWICE)
453 add_reg_note (call_insn, REG_SETJMP, const0_rtx);
454 cfun->calls_setjmp = 1;
457 SIBLING_CALL_P (call_insn) = ((ecf_flags & ECF_SIBCALL) != 0);
459 /* Restore this now, so that we do defer pops for this call's args
460 if the context of the call as a whole permits. */
461 inhibit_defer_pop = old_inhibit_defer_pop;
463 if (n_popped > 0)
465 if (!already_popped)
466 CALL_INSN_FUNCTION_USAGE (call_insn)
467 = gen_rtx_EXPR_LIST (VOIDmode,
468 gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx),
469 CALL_INSN_FUNCTION_USAGE (call_insn));
470 rounded_stack_size -= n_popped;
471 rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
472 stack_pointer_delta -= n_popped;
474 add_reg_note (call_insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
476 /* If popup is needed, stack realign must use DRAP */
477 if (SUPPORTS_STACK_ALIGNMENT)
478 crtl->need_drap = true;
480 /* For noreturn calls when not accumulating outgoing args force
481 REG_ARGS_SIZE note to prevent crossjumping of calls with different
482 args sizes. */
483 else if (!ACCUMULATE_OUTGOING_ARGS && (ecf_flags & ECF_NORETURN) != 0)
484 add_reg_note (call_insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
486 if (!ACCUMULATE_OUTGOING_ARGS)
488 /* If returning from the subroutine does not automatically pop the args,
489 we need an instruction to pop them sooner or later.
490 Perhaps do it now; perhaps just record how much space to pop later.
492 If returning from the subroutine does pop the args, indicate that the
493 stack pointer will be changed. */
495 if (rounded_stack_size != 0)
497 if (ecf_flags & ECF_NORETURN)
498 /* Just pretend we did the pop. */
499 stack_pointer_delta -= rounded_stack_size;
500 else if (flag_defer_pop && inhibit_defer_pop == 0
501 && ! (ecf_flags & (ECF_CONST | ECF_PURE)))
502 pending_stack_adjust += rounded_stack_size;
503 else
504 adjust_stack (rounded_stack_size_rtx);
507 /* When we accumulate outgoing args, we must avoid any stack manipulations.
508 Restore the stack pointer to its original value now. Usually
509 ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
510 On i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
511 popping variants of functions exist as well.
513 ??? We may optimize similar to defer_pop above, but it is
514 probably not worthwhile.
516 ??? It will be worthwhile to enable combine_stack_adjustments even for
517 such machines. */
518 else if (n_popped)
519 anti_adjust_stack (GEN_INT (n_popped));
522 /* Determine if the function identified by NAME and FNDECL is one with
523 special properties we wish to know about.
525 For example, if the function might return more than one time (setjmp), then
526 set RETURNS_TWICE to a nonzero value.
528 Similarly set NORETURN if the function is in the longjmp family.
530 Set MAY_BE_ALLOCA for any memory allocation function that might allocate
531 space from the stack such as alloca. */
533 static int
534 special_function_p (const_tree fndecl, int flags)
536 tree name_decl = DECL_NAME (fndecl);
538 /* For instrumentation clones we want to derive flags
539 from the original name. */
540 if (cgraph_node::get (fndecl)
541 && cgraph_node::get (fndecl)->instrumentation_clone)
542 name_decl = DECL_NAME (cgraph_node::get (fndecl)->orig_decl);
544 if (fndecl && name_decl
545 && IDENTIFIER_LENGTH (name_decl) <= 17
546 /* Exclude functions not at the file scope, or not `extern',
547 since they are not the magic functions we would otherwise
548 think they are.
549 FIXME: this should be handled with attributes, not with this
550 hacky imitation of DECL_ASSEMBLER_NAME. It's (also) wrong
551 because you can declare fork() inside a function if you
552 wish. */
553 && (DECL_CONTEXT (fndecl) == NULL_TREE
554 || TREE_CODE (DECL_CONTEXT (fndecl)) == TRANSLATION_UNIT_DECL)
555 && TREE_PUBLIC (fndecl))
557 const char *name = IDENTIFIER_POINTER (name_decl);
558 const char *tname = name;
560 /* We assume that alloca will always be called by name. It
561 makes no sense to pass it as a pointer-to-function to
562 anything that does not understand its behavior. */
563 if (((IDENTIFIER_LENGTH (name_decl) == 6
564 && name[0] == 'a'
565 && ! strcmp (name, "alloca"))
566 || (IDENTIFIER_LENGTH (name_decl) == 16
567 && name[0] == '_'
568 && ! strcmp (name, "__builtin_alloca"))))
569 flags |= ECF_MAY_BE_ALLOCA;
571 /* Disregard prefix _, __, __x or __builtin_. */
572 if (name[0] == '_')
574 if (name[1] == '_'
575 && name[2] == 'b'
576 && !strncmp (name + 3, "uiltin_", 7))
577 tname += 10;
578 else if (name[1] == '_' && name[2] == 'x')
579 tname += 3;
580 else if (name[1] == '_')
581 tname += 2;
582 else
583 tname += 1;
586 if (tname[0] == 's')
588 if ((tname[1] == 'e'
589 && (! strcmp (tname, "setjmp")
590 || ! strcmp (tname, "setjmp_syscall")))
591 || (tname[1] == 'i'
592 && ! strcmp (tname, "sigsetjmp"))
593 || (tname[1] == 'a'
594 && ! strcmp (tname, "savectx")))
595 flags |= ECF_RETURNS_TWICE | ECF_LEAF;
597 if (tname[1] == 'i'
598 && ! strcmp (tname, "siglongjmp"))
599 flags |= ECF_NORETURN;
601 else if ((tname[0] == 'q' && tname[1] == 's'
602 && ! strcmp (tname, "qsetjmp"))
603 || (tname[0] == 'v' && tname[1] == 'f'
604 && ! strcmp (tname, "vfork"))
605 || (tname[0] == 'g' && tname[1] == 'e'
606 && !strcmp (tname, "getcontext")))
607 flags |= ECF_RETURNS_TWICE | ECF_LEAF;
609 else if (tname[0] == 'l' && tname[1] == 'o'
610 && ! strcmp (tname, "longjmp"))
611 flags |= ECF_NORETURN;
614 return flags;
617 /* Similar to special_function_p; return a set of ERF_ flags for the
618 function FNDECL. */
619 static int
620 decl_return_flags (tree fndecl)
622 tree attr;
623 tree type = TREE_TYPE (fndecl);
624 if (!type)
625 return 0;
627 attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
628 if (!attr)
629 return 0;
631 attr = TREE_VALUE (TREE_VALUE (attr));
632 if (!attr || TREE_STRING_LENGTH (attr) < 1)
633 return 0;
635 switch (TREE_STRING_POINTER (attr)[0])
637 case '1':
638 case '2':
639 case '3':
640 case '4':
641 return ERF_RETURNS_ARG | (TREE_STRING_POINTER (attr)[0] - '1');
643 case 'm':
644 return ERF_NOALIAS;
646 case '.':
647 default:
648 return 0;
652 /* Return nonzero when FNDECL represents a call to setjmp. */
655 setjmp_call_p (const_tree fndecl)
657 if (DECL_IS_RETURNS_TWICE (fndecl))
658 return ECF_RETURNS_TWICE;
659 return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
663 /* Return true if STMT is an alloca call. */
665 bool
666 gimple_alloca_call_p (const_gimple stmt)
668 tree fndecl;
670 if (!is_gimple_call (stmt))
671 return false;
673 fndecl = gimple_call_fndecl (stmt);
674 if (fndecl && (special_function_p (fndecl, 0) & ECF_MAY_BE_ALLOCA))
675 return true;
677 return false;
680 /* Return true when exp contains alloca call. */
682 bool
683 alloca_call_p (const_tree exp)
685 tree fndecl;
686 if (TREE_CODE (exp) == CALL_EXPR
687 && (fndecl = get_callee_fndecl (exp))
688 && (special_function_p (fndecl, 0) & ECF_MAY_BE_ALLOCA))
689 return true;
690 return false;
693 /* Return TRUE if FNDECL is either a TM builtin or a TM cloned
694 function. Return FALSE otherwise. */
696 static bool
697 is_tm_builtin (const_tree fndecl)
699 if (fndecl == NULL)
700 return false;
702 if (decl_is_tm_clone (fndecl))
703 return true;
705 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
707 switch (DECL_FUNCTION_CODE (fndecl))
709 case BUILT_IN_TM_COMMIT:
710 case BUILT_IN_TM_COMMIT_EH:
711 case BUILT_IN_TM_ABORT:
712 case BUILT_IN_TM_IRREVOCABLE:
713 case BUILT_IN_TM_GETTMCLONE_IRR:
714 case BUILT_IN_TM_MEMCPY:
715 case BUILT_IN_TM_MEMMOVE:
716 case BUILT_IN_TM_MEMSET:
717 CASE_BUILT_IN_TM_STORE (1):
718 CASE_BUILT_IN_TM_STORE (2):
719 CASE_BUILT_IN_TM_STORE (4):
720 CASE_BUILT_IN_TM_STORE (8):
721 CASE_BUILT_IN_TM_STORE (FLOAT):
722 CASE_BUILT_IN_TM_STORE (DOUBLE):
723 CASE_BUILT_IN_TM_STORE (LDOUBLE):
724 CASE_BUILT_IN_TM_STORE (M64):
725 CASE_BUILT_IN_TM_STORE (M128):
726 CASE_BUILT_IN_TM_STORE (M256):
727 CASE_BUILT_IN_TM_LOAD (1):
728 CASE_BUILT_IN_TM_LOAD (2):
729 CASE_BUILT_IN_TM_LOAD (4):
730 CASE_BUILT_IN_TM_LOAD (8):
731 CASE_BUILT_IN_TM_LOAD (FLOAT):
732 CASE_BUILT_IN_TM_LOAD (DOUBLE):
733 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
734 CASE_BUILT_IN_TM_LOAD (M64):
735 CASE_BUILT_IN_TM_LOAD (M128):
736 CASE_BUILT_IN_TM_LOAD (M256):
737 case BUILT_IN_TM_LOG:
738 case BUILT_IN_TM_LOG_1:
739 case BUILT_IN_TM_LOG_2:
740 case BUILT_IN_TM_LOG_4:
741 case BUILT_IN_TM_LOG_8:
742 case BUILT_IN_TM_LOG_FLOAT:
743 case BUILT_IN_TM_LOG_DOUBLE:
744 case BUILT_IN_TM_LOG_LDOUBLE:
745 case BUILT_IN_TM_LOG_M64:
746 case BUILT_IN_TM_LOG_M128:
747 case BUILT_IN_TM_LOG_M256:
748 return true;
749 default:
750 break;
753 return false;
756 /* Detect flags (function attributes) from the function decl or type node. */
759 flags_from_decl_or_type (const_tree exp)
761 int flags = 0;
763 if (DECL_P (exp))
765 /* The function exp may have the `malloc' attribute. */
766 if (DECL_IS_MALLOC (exp))
767 flags |= ECF_MALLOC;
769 /* The function exp may have the `returns_twice' attribute. */
770 if (DECL_IS_RETURNS_TWICE (exp))
771 flags |= ECF_RETURNS_TWICE;
773 /* Process the pure and const attributes. */
774 if (TREE_READONLY (exp))
775 flags |= ECF_CONST;
776 if (DECL_PURE_P (exp))
777 flags |= ECF_PURE;
778 if (DECL_LOOPING_CONST_OR_PURE_P (exp))
779 flags |= ECF_LOOPING_CONST_OR_PURE;
781 if (DECL_IS_NOVOPS (exp))
782 flags |= ECF_NOVOPS;
783 if (lookup_attribute ("leaf", DECL_ATTRIBUTES (exp)))
784 flags |= ECF_LEAF;
786 if (TREE_NOTHROW (exp))
787 flags |= ECF_NOTHROW;
789 if (flag_tm)
791 if (is_tm_builtin (exp))
792 flags |= ECF_TM_BUILTIN;
793 else if ((flags & (ECF_CONST|ECF_NOVOPS)) != 0
794 || lookup_attribute ("transaction_pure",
795 TYPE_ATTRIBUTES (TREE_TYPE (exp))))
796 flags |= ECF_TM_PURE;
799 flags = special_function_p (exp, flags);
801 else if (TYPE_P (exp))
803 if (TYPE_READONLY (exp))
804 flags |= ECF_CONST;
806 if (flag_tm
807 && ((flags & ECF_CONST) != 0
808 || lookup_attribute ("transaction_pure", TYPE_ATTRIBUTES (exp))))
809 flags |= ECF_TM_PURE;
811 else
812 gcc_unreachable ();
814 if (TREE_THIS_VOLATILE (exp))
816 flags |= ECF_NORETURN;
817 if (flags & (ECF_CONST|ECF_PURE))
818 flags |= ECF_LOOPING_CONST_OR_PURE;
821 return flags;
824 /* Detect flags from a CALL_EXPR. */
827 call_expr_flags (const_tree t)
829 int flags;
830 tree decl = get_callee_fndecl (t);
832 if (decl)
833 flags = flags_from_decl_or_type (decl);
834 else
836 t = TREE_TYPE (CALL_EXPR_FN (t));
837 if (t && TREE_CODE (t) == POINTER_TYPE)
838 flags = flags_from_decl_or_type (TREE_TYPE (t));
839 else
840 flags = 0;
843 return flags;
846 /* Precompute all register parameters as described by ARGS, storing values
847 into fields within the ARGS array.
849 NUM_ACTUALS indicates the total number elements in the ARGS array.
851 Set REG_PARM_SEEN if we encounter a register parameter. */
853 static void
854 precompute_register_parameters (int num_actuals, struct arg_data *args,
855 int *reg_parm_seen)
857 int i;
859 *reg_parm_seen = 0;
861 for (i = 0; i < num_actuals; i++)
862 if (args[i].reg != 0 && ! args[i].pass_on_stack)
864 *reg_parm_seen = 1;
866 if (args[i].value == 0)
868 push_temp_slots ();
869 args[i].value = expand_normal (args[i].tree_value);
870 preserve_temp_slots (args[i].value);
871 pop_temp_slots ();
874 /* If we are to promote the function arg to a wider mode,
875 do it now. */
877 if (args[i].mode != TYPE_MODE (TREE_TYPE (args[i].tree_value)))
878 args[i].value
879 = convert_modes (args[i].mode,
880 TYPE_MODE (TREE_TYPE (args[i].tree_value)),
881 args[i].value, args[i].unsignedp);
883 /* If the value is a non-legitimate constant, force it into a
884 pseudo now. TLS symbols sometimes need a call to resolve. */
885 if (CONSTANT_P (args[i].value)
886 && !targetm.legitimate_constant_p (args[i].mode, args[i].value))
887 args[i].value = force_reg (args[i].mode, args[i].value);
889 /* If we're going to have to load the value by parts, pull the
890 parts into pseudos. The part extraction process can involve
891 non-trivial computation. */
892 if (GET_CODE (args[i].reg) == PARALLEL)
894 tree type = TREE_TYPE (args[i].tree_value);
895 args[i].parallel_value
896 = emit_group_load_into_temps (args[i].reg, args[i].value,
897 type, int_size_in_bytes (type));
900 /* If the value is expensive, and we are inside an appropriately
901 short loop, put the value into a pseudo and then put the pseudo
902 into the hard reg.
904 For small register classes, also do this if this call uses
905 register parameters. This is to avoid reload conflicts while
906 loading the parameters registers. */
908 else if ((! (REG_P (args[i].value)
909 || (GET_CODE (args[i].value) == SUBREG
910 && REG_P (SUBREG_REG (args[i].value)))))
911 && args[i].mode != BLKmode
912 && set_src_cost (args[i].value, optimize_insn_for_speed_p ())
913 > COSTS_N_INSNS (1)
914 && ((*reg_parm_seen
915 && targetm.small_register_classes_for_mode_p (args[i].mode))
916 || optimize))
917 args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
921 #ifdef REG_PARM_STACK_SPACE
923 /* The argument list is the property of the called routine and it
924 may clobber it. If the fixed area has been used for previous
925 parameters, we must save and restore it. */
927 static rtx
928 save_fixed_argument_area (int reg_parm_stack_space, rtx argblock, int *low_to_save, int *high_to_save)
930 int low;
931 int high;
933 /* Compute the boundary of the area that needs to be saved, if any. */
934 high = reg_parm_stack_space;
935 #ifdef ARGS_GROW_DOWNWARD
936 high += 1;
937 #endif
938 if (high > highest_outgoing_arg_in_use)
939 high = highest_outgoing_arg_in_use;
941 for (low = 0; low < high; low++)
942 if (stack_usage_map[low] != 0)
944 int num_to_save;
945 machine_mode save_mode;
946 int delta;
947 rtx addr;
948 rtx stack_area;
949 rtx save_area;
951 while (stack_usage_map[--high] == 0)
954 *low_to_save = low;
955 *high_to_save = high;
957 num_to_save = high - low + 1;
958 save_mode = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
960 /* If we don't have the required alignment, must do this
961 in BLKmode. */
962 if ((low & (MIN (GET_MODE_SIZE (save_mode),
963 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
964 save_mode = BLKmode;
966 #ifdef ARGS_GROW_DOWNWARD
967 delta = -high;
968 #else
969 delta = low;
970 #endif
971 addr = plus_constant (Pmode, argblock, delta);
972 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
974 set_mem_align (stack_area, PARM_BOUNDARY);
975 if (save_mode == BLKmode)
977 save_area = assign_stack_temp (BLKmode, num_to_save);
978 emit_block_move (validize_mem (save_area), stack_area,
979 GEN_INT (num_to_save), BLOCK_OP_CALL_PARM);
981 else
983 save_area = gen_reg_rtx (save_mode);
984 emit_move_insn (save_area, stack_area);
987 return save_area;
990 return NULL_RTX;
993 static void
994 restore_fixed_argument_area (rtx save_area, rtx argblock, int high_to_save, int low_to_save)
996 machine_mode save_mode = GET_MODE (save_area);
997 int delta;
998 rtx addr, stack_area;
1000 #ifdef ARGS_GROW_DOWNWARD
1001 delta = -high_to_save;
1002 #else
1003 delta = low_to_save;
1004 #endif
1005 addr = plus_constant (Pmode, argblock, delta);
1006 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
1007 set_mem_align (stack_area, PARM_BOUNDARY);
1009 if (save_mode != BLKmode)
1010 emit_move_insn (stack_area, save_area);
1011 else
1012 emit_block_move (stack_area, validize_mem (save_area),
1013 GEN_INT (high_to_save - low_to_save + 1),
1014 BLOCK_OP_CALL_PARM);
1016 #endif /* REG_PARM_STACK_SPACE */
1018 /* If any elements in ARGS refer to parameters that are to be passed in
1019 registers, but not in memory, and whose alignment does not permit a
1020 direct copy into registers. Copy the values into a group of pseudos
1021 which we will later copy into the appropriate hard registers.
1023 Pseudos for each unaligned argument will be stored into the array
1024 args[argnum].aligned_regs. The caller is responsible for deallocating
1025 the aligned_regs array if it is nonzero. */
1027 static void
1028 store_unaligned_arguments_into_pseudos (struct arg_data *args, int num_actuals)
1030 int i, j;
1032 for (i = 0; i < num_actuals; i++)
1033 if (args[i].reg != 0 && ! args[i].pass_on_stack
1034 && GET_CODE (args[i].reg) != PARALLEL
1035 && args[i].mode == BLKmode
1036 && MEM_P (args[i].value)
1037 && (MEM_ALIGN (args[i].value)
1038 < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
1040 int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1041 int endian_correction = 0;
1043 if (args[i].partial)
1045 gcc_assert (args[i].partial % UNITS_PER_WORD == 0);
1046 args[i].n_aligned_regs = args[i].partial / UNITS_PER_WORD;
1048 else
1050 args[i].n_aligned_regs
1051 = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1054 args[i].aligned_regs = XNEWVEC (rtx, args[i].n_aligned_regs);
1056 /* Structures smaller than a word are normally aligned to the
1057 least significant byte. On a BYTES_BIG_ENDIAN machine,
1058 this means we must skip the empty high order bytes when
1059 calculating the bit offset. */
1060 if (bytes < UNITS_PER_WORD
1061 #ifdef BLOCK_REG_PADDING
1062 && (BLOCK_REG_PADDING (args[i].mode,
1063 TREE_TYPE (args[i].tree_value), 1)
1064 == downward)
1065 #else
1066 && BYTES_BIG_ENDIAN
1067 #endif
1069 endian_correction = BITS_PER_WORD - bytes * BITS_PER_UNIT;
1071 for (j = 0; j < args[i].n_aligned_regs; j++)
1073 rtx reg = gen_reg_rtx (word_mode);
1074 rtx word = operand_subword_force (args[i].value, j, BLKmode);
1075 int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
1077 args[i].aligned_regs[j] = reg;
1078 word = extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
1079 word_mode, word_mode);
1081 /* There is no need to restrict this code to loading items
1082 in TYPE_ALIGN sized hunks. The bitfield instructions can
1083 load up entire word sized registers efficiently.
1085 ??? This may not be needed anymore.
1086 We use to emit a clobber here but that doesn't let later
1087 passes optimize the instructions we emit. By storing 0 into
1088 the register later passes know the first AND to zero out the
1089 bitfield being set in the register is unnecessary. The store
1090 of 0 will be deleted as will at least the first AND. */
1092 emit_move_insn (reg, const0_rtx);
1094 bytes -= bitsize / BITS_PER_UNIT;
1095 store_bit_field (reg, bitsize, endian_correction, 0, 0,
1096 word_mode, word);
1101 /* Fill in ARGS_SIZE and ARGS array based on the parameters found in
1102 CALL_EXPR EXP.
1104 NUM_ACTUALS is the total number of parameters.
1106 N_NAMED_ARGS is the total number of named arguments.
1108 STRUCT_VALUE_ADDR_VALUE is the implicit argument for a struct return
1109 value, or null.
1111 FNDECL is the tree code for the target of this call (if known)
1113 ARGS_SO_FAR holds state needed by the target to know where to place
1114 the next argument.
1116 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
1117 for arguments which are passed in registers.
1119 OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
1120 and may be modified by this routine.
1122 OLD_PENDING_ADJ, MUST_PREALLOCATE and FLAGS are pointers to integer
1123 flags which may may be modified by this routine.
1125 MAY_TAILCALL is cleared if we encounter an invisible pass-by-reference
1126 that requires allocation of stack space.
1128 CALL_FROM_THUNK_P is true if this call is the jump from a thunk to
1129 the thunked-to function. */
1131 static void
1132 initialize_argument_information (int num_actuals ATTRIBUTE_UNUSED,
1133 struct arg_data *args,
1134 struct args_size *args_size,
1135 int n_named_args ATTRIBUTE_UNUSED,
1136 tree exp, tree struct_value_addr_value,
1137 tree fndecl, tree fntype,
1138 cumulative_args_t args_so_far,
1139 int reg_parm_stack_space,
1140 rtx *old_stack_level, int *old_pending_adj,
1141 int *must_preallocate, int *ecf_flags,
1142 bool *may_tailcall, bool call_from_thunk_p)
1144 CUMULATIVE_ARGS *args_so_far_pnt = get_cumulative_args (args_so_far);
1145 location_t loc = EXPR_LOCATION (exp);
1147 /* Count arg position in order args appear. */
1148 int argpos;
1150 int i;
1152 args_size->constant = 0;
1153 args_size->var = 0;
1155 bitmap_obstack_initialize (NULL);
1157 /* In this loop, we consider args in the order they are written.
1158 We fill up ARGS from the back. */
1160 i = num_actuals - 1;
1162 int j = i, ptr_arg = -1;
1163 call_expr_arg_iterator iter;
1164 tree arg;
1165 bitmap slots = NULL;
1167 if (struct_value_addr_value)
1169 args[j].tree_value = struct_value_addr_value;
1170 j--;
1172 /* If we pass structure address then we need to
1173 create bounds for it. Since created bounds is
1174 a call statement, we expand it right here to avoid
1175 fixing all other places where it may be expanded. */
1176 if (CALL_WITH_BOUNDS_P (exp))
1178 args[j].value = gen_reg_rtx (targetm.chkp_bound_mode ());
1179 args[j].tree_value
1180 = chkp_make_bounds_for_struct_addr (struct_value_addr_value);
1181 expand_expr_real (args[j].tree_value, args[j].value, VOIDmode,
1182 EXPAND_NORMAL, 0, false);
1183 args[j].pointer_arg = j + 1;
1184 j--;
1187 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
1189 tree argtype = TREE_TYPE (arg);
1191 /* Remember last param with pointer and associate it
1192 with following pointer bounds. */
1193 if (CALL_WITH_BOUNDS_P (exp)
1194 && chkp_type_has_pointer (argtype))
1196 if (slots)
1197 BITMAP_FREE (slots);
1198 ptr_arg = j;
1199 if (!BOUNDED_TYPE_P (argtype))
1201 slots = BITMAP_ALLOC (NULL);
1202 chkp_find_bound_slots (argtype, slots);
1205 else if (POINTER_BOUNDS_TYPE_P (argtype))
1207 /* We expect bounds in instrumented calls only.
1208 Otherwise it is a sign we lost flag due to some optimization
1209 and may emit call args incorrectly. */
1210 gcc_assert (CALL_WITH_BOUNDS_P (exp));
1212 /* For structures look for the next available pointer. */
1213 if (ptr_arg != -1 && slots)
1215 unsigned bnd_no = bitmap_first_set_bit (slots);
1216 args[j].pointer_offset =
1217 bnd_no * POINTER_SIZE / BITS_PER_UNIT;
1219 bitmap_clear_bit (slots, bnd_no);
1221 /* Check we have no more pointers in the structure. */
1222 if (bitmap_empty_p (slots))
1223 BITMAP_FREE (slots);
1225 args[j].pointer_arg = ptr_arg;
1227 /* Check we covered all pointers in the previous
1228 non bounds arg. */
1229 if (!slots)
1230 ptr_arg = -1;
1232 else
1233 ptr_arg = -1;
1235 if (targetm.calls.split_complex_arg
1236 && argtype
1237 && TREE_CODE (argtype) == COMPLEX_TYPE
1238 && targetm.calls.split_complex_arg (argtype))
1240 tree subtype = TREE_TYPE (argtype);
1241 args[j].tree_value = build1 (REALPART_EXPR, subtype, arg);
1242 j--;
1243 args[j].tree_value = build1 (IMAGPART_EXPR, subtype, arg);
1245 else
1246 args[j].tree_value = arg;
1247 j--;
1250 if (slots)
1251 BITMAP_FREE (slots);
1254 bitmap_obstack_release (NULL);
1256 /* I counts args in order (to be) pushed; ARGPOS counts in order written. */
1257 for (argpos = 0; argpos < num_actuals; i--, argpos++)
1259 tree type = TREE_TYPE (args[i].tree_value);
1260 int unsignedp;
1261 machine_mode mode;
1263 /* Replace erroneous argument with constant zero. */
1264 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1265 args[i].tree_value = integer_zero_node, type = integer_type_node;
1267 /* If TYPE is a transparent union or record, pass things the way
1268 we would pass the first field of the union or record. We have
1269 already verified that the modes are the same. */
1270 if ((TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == RECORD_TYPE)
1271 && TYPE_TRANSPARENT_AGGR (type))
1272 type = TREE_TYPE (first_field (type));
1274 /* Decide where to pass this arg.
1276 args[i].reg is nonzero if all or part is passed in registers.
1278 args[i].partial is nonzero if part but not all is passed in registers,
1279 and the exact value says how many bytes are passed in registers.
1281 args[i].pass_on_stack is nonzero if the argument must at least be
1282 computed on the stack. It may then be loaded back into registers
1283 if args[i].reg is nonzero.
1285 These decisions are driven by the FUNCTION_... macros and must agree
1286 with those made by function.c. */
1288 /* See if this argument should be passed by invisible reference. */
1289 if (pass_by_reference (args_so_far_pnt, TYPE_MODE (type),
1290 type, argpos < n_named_args))
1292 bool callee_copies;
1293 tree base = NULL_TREE;
1295 callee_copies
1296 = reference_callee_copied (args_so_far_pnt, TYPE_MODE (type),
1297 type, argpos < n_named_args);
1299 /* If we're compiling a thunk, pass through invisible references
1300 instead of making a copy. */
1301 if (call_from_thunk_p
1302 || (callee_copies
1303 && !TREE_ADDRESSABLE (type)
1304 && (base = get_base_address (args[i].tree_value))
1305 && TREE_CODE (base) != SSA_NAME
1306 && (!DECL_P (base) || MEM_P (DECL_RTL (base)))))
1308 /* Argument setup code may have copied the value to register. We
1309 revert that optimization now because the tail call code must
1310 use the original location. */
1311 if (TREE_CODE (args[i].tree_value) == PARM_DECL
1312 && !MEM_P (DECL_RTL (args[i].tree_value))
1313 && DECL_INCOMING_RTL (args[i].tree_value)
1314 && MEM_P (DECL_INCOMING_RTL (args[i].tree_value)))
1315 set_decl_rtl (args[i].tree_value,
1316 DECL_INCOMING_RTL (args[i].tree_value));
1318 mark_addressable (args[i].tree_value);
1320 /* We can't use sibcalls if a callee-copied argument is
1321 stored in the current function's frame. */
1322 if (!call_from_thunk_p && DECL_P (base) && !TREE_STATIC (base))
1323 *may_tailcall = false;
1325 args[i].tree_value = build_fold_addr_expr_loc (loc,
1326 args[i].tree_value);
1327 type = TREE_TYPE (args[i].tree_value);
1329 if (*ecf_flags & ECF_CONST)
1330 *ecf_flags &= ~(ECF_CONST | ECF_LOOPING_CONST_OR_PURE);
1332 else
1334 /* We make a copy of the object and pass the address to the
1335 function being called. */
1336 rtx copy;
1338 if (!COMPLETE_TYPE_P (type)
1339 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
1340 || (flag_stack_check == GENERIC_STACK_CHECK
1341 && compare_tree_int (TYPE_SIZE_UNIT (type),
1342 STACK_CHECK_MAX_VAR_SIZE) > 0))
1344 /* This is a variable-sized object. Make space on the stack
1345 for it. */
1346 rtx size_rtx = expr_size (args[i].tree_value);
1348 if (*old_stack_level == 0)
1350 emit_stack_save (SAVE_BLOCK, old_stack_level);
1351 *old_pending_adj = pending_stack_adjust;
1352 pending_stack_adjust = 0;
1355 /* We can pass TRUE as the 4th argument because we just
1356 saved the stack pointer and will restore it right after
1357 the call. */
1358 copy = allocate_dynamic_stack_space (size_rtx,
1359 TYPE_ALIGN (type),
1360 TYPE_ALIGN (type),
1361 true);
1362 copy = gen_rtx_MEM (BLKmode, copy);
1363 set_mem_attributes (copy, type, 1);
1365 else
1366 copy = assign_temp (type, 1, 0);
1368 store_expr (args[i].tree_value, copy, 0, false);
1370 /* Just change the const function to pure and then let
1371 the next test clear the pure based on
1372 callee_copies. */
1373 if (*ecf_flags & ECF_CONST)
1375 *ecf_flags &= ~ECF_CONST;
1376 *ecf_flags |= ECF_PURE;
1379 if (!callee_copies && *ecf_flags & ECF_PURE)
1380 *ecf_flags &= ~(ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
1382 args[i].tree_value
1383 = build_fold_addr_expr_loc (loc, make_tree (type, copy));
1384 type = TREE_TYPE (args[i].tree_value);
1385 *may_tailcall = false;
1389 unsignedp = TYPE_UNSIGNED (type);
1390 mode = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
1391 fndecl ? TREE_TYPE (fndecl) : fntype, 0);
1393 args[i].unsignedp = unsignedp;
1394 args[i].mode = mode;
1396 args[i].reg = targetm.calls.function_arg (args_so_far, mode, type,
1397 argpos < n_named_args);
1399 if (args[i].reg && CONST_INT_P (args[i].reg))
1401 args[i].special_slot = args[i].reg;
1402 args[i].reg = NULL;
1405 /* If this is a sibling call and the machine has register windows, the
1406 register window has to be unwinded before calling the routine, so
1407 arguments have to go into the incoming registers. */
1408 if (targetm.calls.function_incoming_arg != targetm.calls.function_arg)
1409 args[i].tail_call_reg
1410 = targetm.calls.function_incoming_arg (args_so_far, mode, type,
1411 argpos < n_named_args);
1412 else
1413 args[i].tail_call_reg = args[i].reg;
1415 if (args[i].reg)
1416 args[i].partial
1417 = targetm.calls.arg_partial_bytes (args_so_far, mode, type,
1418 argpos < n_named_args);
1420 args[i].pass_on_stack = targetm.calls.must_pass_in_stack (mode, type);
1422 /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
1423 it means that we are to pass this arg in the register(s) designated
1424 by the PARALLEL, but also to pass it in the stack. */
1425 if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
1426 && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
1427 args[i].pass_on_stack = 1;
1429 /* If this is an addressable type, we must preallocate the stack
1430 since we must evaluate the object into its final location.
1432 If this is to be passed in both registers and the stack, it is simpler
1433 to preallocate. */
1434 if (TREE_ADDRESSABLE (type)
1435 || (args[i].pass_on_stack && args[i].reg != 0))
1436 *must_preallocate = 1;
1438 /* No stack allocation and padding for bounds. */
1439 if (POINTER_BOUNDS_P (args[i].tree_value))
1441 /* Compute the stack-size of this argument. */
1442 else if (args[i].reg == 0 || args[i].partial != 0
1443 || reg_parm_stack_space > 0
1444 || args[i].pass_on_stack)
1445 locate_and_pad_parm (mode, type,
1446 #ifdef STACK_PARMS_IN_REG_PARM_AREA
1448 #else
1449 args[i].reg != 0,
1450 #endif
1451 reg_parm_stack_space,
1452 args[i].pass_on_stack ? 0 : args[i].partial,
1453 fndecl, args_size, &args[i].locate);
1454 #ifdef BLOCK_REG_PADDING
1455 else
1456 /* The argument is passed entirely in registers. See at which
1457 end it should be padded. */
1458 args[i].locate.where_pad =
1459 BLOCK_REG_PADDING (mode, type,
1460 int_size_in_bytes (type) <= UNITS_PER_WORD);
1461 #endif
1463 /* Update ARGS_SIZE, the total stack space for args so far. */
1465 args_size->constant += args[i].locate.size.constant;
1466 if (args[i].locate.size.var)
1467 ADD_PARM_SIZE (*args_size, args[i].locate.size.var);
1469 /* Increment ARGS_SO_FAR, which has info about which arg-registers
1470 have been used, etc. */
1472 targetm.calls.function_arg_advance (args_so_far, TYPE_MODE (type),
1473 type, argpos < n_named_args);
1477 /* Update ARGS_SIZE to contain the total size for the argument block.
1478 Return the original constant component of the argument block's size.
1480 REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
1481 for arguments passed in registers. */
1483 static int
1484 compute_argument_block_size (int reg_parm_stack_space,
1485 struct args_size *args_size,
1486 tree fndecl ATTRIBUTE_UNUSED,
1487 tree fntype ATTRIBUTE_UNUSED,
1488 int preferred_stack_boundary ATTRIBUTE_UNUSED)
1490 int unadjusted_args_size = args_size->constant;
1492 /* For accumulate outgoing args mode we don't need to align, since the frame
1493 will be already aligned. Align to STACK_BOUNDARY in order to prevent
1494 backends from generating misaligned frame sizes. */
1495 if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
1496 preferred_stack_boundary = STACK_BOUNDARY;
1498 /* Compute the actual size of the argument block required. The variable
1499 and constant sizes must be combined, the size may have to be rounded,
1500 and there may be a minimum required size. */
1502 if (args_size->var)
1504 args_size->var = ARGS_SIZE_TREE (*args_size);
1505 args_size->constant = 0;
1507 preferred_stack_boundary /= BITS_PER_UNIT;
1508 if (preferred_stack_boundary > 1)
1510 /* We don't handle this case yet. To handle it correctly we have
1511 to add the delta, round and subtract the delta.
1512 Currently no machine description requires this support. */
1513 gcc_assert (!(stack_pointer_delta & (preferred_stack_boundary - 1)));
1514 args_size->var = round_up (args_size->var, preferred_stack_boundary);
1517 if (reg_parm_stack_space > 0)
1519 args_size->var
1520 = size_binop (MAX_EXPR, args_size->var,
1521 ssize_int (reg_parm_stack_space));
1523 /* The area corresponding to register parameters is not to count in
1524 the size of the block we need. So make the adjustment. */
1525 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1526 args_size->var
1527 = size_binop (MINUS_EXPR, args_size->var,
1528 ssize_int (reg_parm_stack_space));
1531 else
1533 preferred_stack_boundary /= BITS_PER_UNIT;
1534 if (preferred_stack_boundary < 1)
1535 preferred_stack_boundary = 1;
1536 args_size->constant = (((args_size->constant
1537 + stack_pointer_delta
1538 + preferred_stack_boundary - 1)
1539 / preferred_stack_boundary
1540 * preferred_stack_boundary)
1541 - stack_pointer_delta);
1543 args_size->constant = MAX (args_size->constant,
1544 reg_parm_stack_space);
1546 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1547 args_size->constant -= reg_parm_stack_space;
1549 return unadjusted_args_size;
1552 /* Precompute parameters as needed for a function call.
1554 FLAGS is mask of ECF_* constants.
1556 NUM_ACTUALS is the number of arguments.
1558 ARGS is an array containing information for each argument; this
1559 routine fills in the INITIAL_VALUE and VALUE fields for each
1560 precomputed argument. */
1562 static void
1563 precompute_arguments (int num_actuals, struct arg_data *args)
1565 int i;
1567 /* If this is a libcall, then precompute all arguments so that we do not
1568 get extraneous instructions emitted as part of the libcall sequence. */
1570 /* If we preallocated the stack space, and some arguments must be passed
1571 on the stack, then we must precompute any parameter which contains a
1572 function call which will store arguments on the stack.
1573 Otherwise, evaluating the parameter may clobber previous parameters
1574 which have already been stored into the stack. (we have code to avoid
1575 such case by saving the outgoing stack arguments, but it results in
1576 worse code) */
1577 if (!ACCUMULATE_OUTGOING_ARGS)
1578 return;
1580 for (i = 0; i < num_actuals; i++)
1582 tree type;
1583 machine_mode mode;
1585 if (TREE_CODE (args[i].tree_value) != CALL_EXPR)
1586 continue;
1588 /* If this is an addressable type, we cannot pre-evaluate it. */
1589 type = TREE_TYPE (args[i].tree_value);
1590 gcc_assert (!TREE_ADDRESSABLE (type));
1592 args[i].initial_value = args[i].value
1593 = expand_normal (args[i].tree_value);
1595 mode = TYPE_MODE (type);
1596 if (mode != args[i].mode)
1598 int unsignedp = args[i].unsignedp;
1599 args[i].value
1600 = convert_modes (args[i].mode, mode,
1601 args[i].value, args[i].unsignedp);
1603 /* CSE will replace this only if it contains args[i].value
1604 pseudo, so convert it down to the declared mode using
1605 a SUBREG. */
1606 if (REG_P (args[i].value)
1607 && GET_MODE_CLASS (args[i].mode) == MODE_INT
1608 && promote_mode (type, mode, &unsignedp) != args[i].mode)
1610 args[i].initial_value
1611 = gen_lowpart_SUBREG (mode, args[i].value);
1612 SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1613 SUBREG_PROMOTED_SET (args[i].initial_value, args[i].unsignedp);
1619 /* Given the current state of MUST_PREALLOCATE and information about
1620 arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
1621 compute and return the final value for MUST_PREALLOCATE. */
1623 static int
1624 finalize_must_preallocate (int must_preallocate, int num_actuals,
1625 struct arg_data *args, struct args_size *args_size)
1627 /* See if we have or want to preallocate stack space.
1629 If we would have to push a partially-in-regs parm
1630 before other stack parms, preallocate stack space instead.
1632 If the size of some parm is not a multiple of the required stack
1633 alignment, we must preallocate.
1635 If the total size of arguments that would otherwise create a copy in
1636 a temporary (such as a CALL) is more than half the total argument list
1637 size, preallocation is faster.
1639 Another reason to preallocate is if we have a machine (like the m88k)
1640 where stack alignment is required to be maintained between every
1641 pair of insns, not just when the call is made. However, we assume here
1642 that such machines either do not have push insns (and hence preallocation
1643 would occur anyway) or the problem is taken care of with
1644 PUSH_ROUNDING. */
1646 if (! must_preallocate)
1648 int partial_seen = 0;
1649 int copy_to_evaluate_size = 0;
1650 int i;
1652 for (i = 0; i < num_actuals && ! must_preallocate; i++)
1654 if (args[i].partial > 0 && ! args[i].pass_on_stack)
1655 partial_seen = 1;
1656 else if (partial_seen && args[i].reg == 0)
1657 must_preallocate = 1;
1658 /* We preallocate in case there are bounds passed
1659 in the bounds table to have precomputed address
1660 for bounds association. */
1661 else if (POINTER_BOUNDS_P (args[i].tree_value)
1662 && !args[i].reg)
1663 must_preallocate = 1;
1665 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1666 && (TREE_CODE (args[i].tree_value) == CALL_EXPR
1667 || TREE_CODE (args[i].tree_value) == TARGET_EXPR
1668 || TREE_CODE (args[i].tree_value) == COND_EXPR
1669 || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
1670 copy_to_evaluate_size
1671 += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1674 if (copy_to_evaluate_size * 2 >= args_size->constant
1675 && args_size->constant > 0)
1676 must_preallocate = 1;
1678 return must_preallocate;
1681 /* If we preallocated stack space, compute the address of each argument
1682 and store it into the ARGS array.
1684 We need not ensure it is a valid memory address here; it will be
1685 validized when it is used.
1687 ARGBLOCK is an rtx for the address of the outgoing arguments. */
1689 static void
1690 compute_argument_addresses (struct arg_data *args, rtx argblock, int num_actuals)
1692 if (argblock)
1694 rtx arg_reg = argblock;
1695 int i, arg_offset = 0;
1697 if (GET_CODE (argblock) == PLUS)
1698 arg_reg = XEXP (argblock, 0), arg_offset = INTVAL (XEXP (argblock, 1));
1700 for (i = 0; i < num_actuals; i++)
1702 rtx offset = ARGS_SIZE_RTX (args[i].locate.offset);
1703 rtx slot_offset = ARGS_SIZE_RTX (args[i].locate.slot_offset);
1704 rtx addr;
1705 unsigned int align, boundary;
1706 unsigned int units_on_stack = 0;
1707 machine_mode partial_mode = VOIDmode;
1709 /* Skip this parm if it will not be passed on the stack. */
1710 if (! args[i].pass_on_stack
1711 && args[i].reg != 0
1712 && args[i].partial == 0)
1713 continue;
1715 /* Pointer Bounds are never passed on the stack. */
1716 if (POINTER_BOUNDS_P (args[i].tree_value))
1717 continue;
1719 if (CONST_INT_P (offset))
1720 addr = plus_constant (Pmode, arg_reg, INTVAL (offset));
1721 else
1722 addr = gen_rtx_PLUS (Pmode, arg_reg, offset);
1724 addr = plus_constant (Pmode, addr, arg_offset);
1726 if (args[i].partial != 0)
1728 /* Only part of the parameter is being passed on the stack.
1729 Generate a simple memory reference of the correct size. */
1730 units_on_stack = args[i].locate.size.constant;
1731 partial_mode = mode_for_size (units_on_stack * BITS_PER_UNIT,
1732 MODE_INT, 1);
1733 args[i].stack = gen_rtx_MEM (partial_mode, addr);
1734 set_mem_size (args[i].stack, units_on_stack);
1736 else
1738 args[i].stack = gen_rtx_MEM (args[i].mode, addr);
1739 set_mem_attributes (args[i].stack,
1740 TREE_TYPE (args[i].tree_value), 1);
1742 align = BITS_PER_UNIT;
1743 boundary = args[i].locate.boundary;
1744 if (args[i].locate.where_pad != downward)
1745 align = boundary;
1746 else if (CONST_INT_P (offset))
1748 align = INTVAL (offset) * BITS_PER_UNIT | boundary;
1749 align = align & -align;
1751 set_mem_align (args[i].stack, align);
1753 if (CONST_INT_P (slot_offset))
1754 addr = plus_constant (Pmode, arg_reg, INTVAL (slot_offset));
1755 else
1756 addr = gen_rtx_PLUS (Pmode, arg_reg, slot_offset);
1758 addr = plus_constant (Pmode, addr, arg_offset);
1760 if (args[i].partial != 0)
1762 /* Only part of the parameter is being passed on the stack.
1763 Generate a simple memory reference of the correct size.
1765 args[i].stack_slot = gen_rtx_MEM (partial_mode, addr);
1766 set_mem_size (args[i].stack_slot, units_on_stack);
1768 else
1770 args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
1771 set_mem_attributes (args[i].stack_slot,
1772 TREE_TYPE (args[i].tree_value), 1);
1774 set_mem_align (args[i].stack_slot, args[i].locate.boundary);
1776 /* Function incoming arguments may overlap with sibling call
1777 outgoing arguments and we cannot allow reordering of reads
1778 from function arguments with stores to outgoing arguments
1779 of sibling calls. */
1780 set_mem_alias_set (args[i].stack, 0);
1781 set_mem_alias_set (args[i].stack_slot, 0);
1786 /* Given a FNDECL and EXP, return an rtx suitable for use as a target address
1787 in a call instruction.
1789 FNDECL is the tree node for the target function. For an indirect call
1790 FNDECL will be NULL_TREE.
1792 ADDR is the operand 0 of CALL_EXPR for this call. */
1794 static rtx
1795 rtx_for_function_call (tree fndecl, tree addr)
1797 rtx funexp;
1799 /* Get the function to call, in the form of RTL. */
1800 if (fndecl)
1802 if (!TREE_USED (fndecl) && fndecl != current_function_decl)
1803 TREE_USED (fndecl) = 1;
1805 /* Get a SYMBOL_REF rtx for the function address. */
1806 funexp = XEXP (DECL_RTL (fndecl), 0);
1808 else
1809 /* Generate an rtx (probably a pseudo-register) for the address. */
1811 push_temp_slots ();
1812 funexp = expand_normal (addr);
1813 pop_temp_slots (); /* FUNEXP can't be BLKmode. */
1815 return funexp;
1818 /* Internal state for internal_arg_pointer_based_exp and its helpers. */
1819 static struct
1821 /* Last insn that has been scanned by internal_arg_pointer_based_exp_scan,
1822 or NULL_RTX if none has been scanned yet. */
1823 rtx_insn *scan_start;
1824 /* Vector indexed by REGNO - FIRST_PSEUDO_REGISTER, recording if a pseudo is
1825 based on crtl->args.internal_arg_pointer. The element is NULL_RTX if the
1826 pseudo isn't based on it, a CONST_INT offset if the pseudo is based on it
1827 with fixed offset, or PC if this is with variable or unknown offset. */
1828 vec<rtx> cache;
1829 } internal_arg_pointer_exp_state;
1831 static rtx internal_arg_pointer_based_exp (const_rtx, bool);
1833 /* Helper function for internal_arg_pointer_based_exp. Scan insns in
1834 the tail call sequence, starting with first insn that hasn't been
1835 scanned yet, and note for each pseudo on the LHS whether it is based
1836 on crtl->args.internal_arg_pointer or not, and what offset from that
1837 that pointer it has. */
1839 static void
1840 internal_arg_pointer_based_exp_scan (void)
1842 rtx_insn *insn, *scan_start = internal_arg_pointer_exp_state.scan_start;
1844 if (scan_start == NULL_RTX)
1845 insn = get_insns ();
1846 else
1847 insn = NEXT_INSN (scan_start);
1849 while (insn)
1851 rtx set = single_set (insn);
1852 if (set && REG_P (SET_DEST (set)) && !HARD_REGISTER_P (SET_DEST (set)))
1854 rtx val = NULL_RTX;
1855 unsigned int idx = REGNO (SET_DEST (set)) - FIRST_PSEUDO_REGISTER;
1856 /* Punt on pseudos set multiple times. */
1857 if (idx < internal_arg_pointer_exp_state.cache.length ()
1858 && (internal_arg_pointer_exp_state.cache[idx]
1859 != NULL_RTX))
1860 val = pc_rtx;
1861 else
1862 val = internal_arg_pointer_based_exp (SET_SRC (set), false);
1863 if (val != NULL_RTX)
1865 if (idx >= internal_arg_pointer_exp_state.cache.length ())
1866 internal_arg_pointer_exp_state.cache
1867 .safe_grow_cleared (idx + 1);
1868 internal_arg_pointer_exp_state.cache[idx] = val;
1871 if (NEXT_INSN (insn) == NULL_RTX)
1872 scan_start = insn;
1873 insn = NEXT_INSN (insn);
1876 internal_arg_pointer_exp_state.scan_start = scan_start;
1879 /* Compute whether RTL is based on crtl->args.internal_arg_pointer. Return
1880 NULL_RTX if RTL isn't based on it, a CONST_INT offset if RTL is based on
1881 it with fixed offset, or PC if this is with variable or unknown offset.
1882 TOPLEVEL is true if the function is invoked at the topmost level. */
1884 static rtx
1885 internal_arg_pointer_based_exp (const_rtx rtl, bool toplevel)
1887 if (CONSTANT_P (rtl))
1888 return NULL_RTX;
1890 if (rtl == crtl->args.internal_arg_pointer)
1891 return const0_rtx;
1893 if (REG_P (rtl) && HARD_REGISTER_P (rtl))
1894 return NULL_RTX;
1896 if (GET_CODE (rtl) == PLUS && CONST_INT_P (XEXP (rtl, 1)))
1898 rtx val = internal_arg_pointer_based_exp (XEXP (rtl, 0), toplevel);
1899 if (val == NULL_RTX || val == pc_rtx)
1900 return val;
1901 return plus_constant (Pmode, val, INTVAL (XEXP (rtl, 1)));
1904 /* When called at the topmost level, scan pseudo assignments in between the
1905 last scanned instruction in the tail call sequence and the latest insn
1906 in that sequence. */
1907 if (toplevel)
1908 internal_arg_pointer_based_exp_scan ();
1910 if (REG_P (rtl))
1912 unsigned int idx = REGNO (rtl) - FIRST_PSEUDO_REGISTER;
1913 if (idx < internal_arg_pointer_exp_state.cache.length ())
1914 return internal_arg_pointer_exp_state.cache[idx];
1916 return NULL_RTX;
1919 subrtx_iterator::array_type array;
1920 FOR_EACH_SUBRTX (iter, array, rtl, NONCONST)
1922 const_rtx x = *iter;
1923 if (REG_P (x) && internal_arg_pointer_based_exp (x, false) != NULL_RTX)
1924 return pc_rtx;
1925 if (MEM_P (x))
1926 iter.skip_subrtxes ();
1929 return NULL_RTX;
1932 /* Return true if and only if SIZE storage units (usually bytes)
1933 starting from address ADDR overlap with already clobbered argument
1934 area. This function is used to determine if we should give up a
1935 sibcall. */
1937 static bool
1938 mem_overlaps_already_clobbered_arg_p (rtx addr, unsigned HOST_WIDE_INT size)
1940 HOST_WIDE_INT i;
1941 rtx val;
1943 if (bitmap_empty_p (stored_args_map))
1944 return false;
1945 val = internal_arg_pointer_based_exp (addr, true);
1946 if (val == NULL_RTX)
1947 return false;
1948 else if (val == pc_rtx)
1949 return true;
1950 else
1951 i = INTVAL (val);
1952 #ifdef STACK_GROWS_DOWNWARD
1953 i -= crtl->args.pretend_args_size;
1954 #else
1955 i += crtl->args.pretend_args_size;
1956 #endif
1958 #ifdef ARGS_GROW_DOWNWARD
1959 i = -i - size;
1960 #endif
1961 if (size > 0)
1963 unsigned HOST_WIDE_INT k;
1965 for (k = 0; k < size; k++)
1966 if (i + k < SBITMAP_SIZE (stored_args_map)
1967 && bitmap_bit_p (stored_args_map, i + k))
1968 return true;
1971 return false;
1974 /* Do the register loads required for any wholly-register parms or any
1975 parms which are passed both on the stack and in a register. Their
1976 expressions were already evaluated.
1978 Mark all register-parms as living through the call, putting these USE
1979 insns in the CALL_INSN_FUNCTION_USAGE field.
1981 When IS_SIBCALL, perform the check_sibcall_argument_overlap
1982 checking, setting *SIBCALL_FAILURE if appropriate. */
1984 static void
1985 load_register_parameters (struct arg_data *args, int num_actuals,
1986 rtx *call_fusage, int flags, int is_sibcall,
1987 int *sibcall_failure)
1989 int i, j;
1991 for (i = 0; i < num_actuals; i++)
1993 rtx reg = ((flags & ECF_SIBCALL)
1994 ? args[i].tail_call_reg : args[i].reg);
1995 if (reg)
1997 int partial = args[i].partial;
1998 int nregs;
1999 int size = 0;
2000 rtx_insn *before_arg = get_last_insn ();
2001 /* Set non-negative if we must move a word at a time, even if
2002 just one word (e.g, partial == 4 && mode == DFmode). Set
2003 to -1 if we just use a normal move insn. This value can be
2004 zero if the argument is a zero size structure. */
2005 nregs = -1;
2006 if (GET_CODE (reg) == PARALLEL)
2008 else if (partial)
2010 gcc_assert (partial % UNITS_PER_WORD == 0);
2011 nregs = partial / UNITS_PER_WORD;
2013 else if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode)
2015 size = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
2016 nregs = (size + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
2018 else
2019 size = GET_MODE_SIZE (args[i].mode);
2021 /* Handle calls that pass values in multiple non-contiguous
2022 locations. The Irix 6 ABI has examples of this. */
2024 if (GET_CODE (reg) == PARALLEL)
2025 emit_group_move (reg, args[i].parallel_value);
2027 /* If simple case, just do move. If normal partial, store_one_arg
2028 has already loaded the register for us. In all other cases,
2029 load the register(s) from memory. */
2031 else if (nregs == -1)
2033 emit_move_insn (reg, args[i].value);
2034 #ifdef BLOCK_REG_PADDING
2035 /* Handle case where we have a value that needs shifting
2036 up to the msb. eg. a QImode value and we're padding
2037 upward on a BYTES_BIG_ENDIAN machine. */
2038 if (size < UNITS_PER_WORD
2039 && (args[i].locate.where_pad
2040 == (BYTES_BIG_ENDIAN ? upward : downward)))
2042 rtx x;
2043 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2045 /* Assigning REG here rather than a temp makes CALL_FUSAGE
2046 report the whole reg as used. Strictly speaking, the
2047 call only uses SIZE bytes at the msb end, but it doesn't
2048 seem worth generating rtl to say that. */
2049 reg = gen_rtx_REG (word_mode, REGNO (reg));
2050 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
2051 if (x != reg)
2052 emit_move_insn (reg, x);
2054 #endif
2057 /* If we have pre-computed the values to put in the registers in
2058 the case of non-aligned structures, copy them in now. */
2060 else if (args[i].n_aligned_regs != 0)
2061 for (j = 0; j < args[i].n_aligned_regs; j++)
2062 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
2063 args[i].aligned_regs[j]);
2065 else if (partial == 0 || args[i].pass_on_stack)
2067 rtx mem = validize_mem (copy_rtx (args[i].value));
2069 /* Check for overlap with already clobbered argument area,
2070 providing that this has non-zero size. */
2071 if (is_sibcall
2072 && (size == 0
2073 || mem_overlaps_already_clobbered_arg_p
2074 (XEXP (args[i].value, 0), size)))
2075 *sibcall_failure = 1;
2077 /* Handle a BLKmode that needs shifting. */
2078 if (nregs == 1 && size < UNITS_PER_WORD
2079 #ifdef BLOCK_REG_PADDING
2080 && args[i].locate.where_pad == downward
2081 #else
2082 && BYTES_BIG_ENDIAN
2083 #endif
2086 rtx tem = operand_subword_force (mem, 0, args[i].mode);
2087 rtx ri = gen_rtx_REG (word_mode, REGNO (reg));
2088 rtx x = gen_reg_rtx (word_mode);
2089 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2090 enum tree_code dir = BYTES_BIG_ENDIAN ? RSHIFT_EXPR
2091 : LSHIFT_EXPR;
2093 emit_move_insn (x, tem);
2094 x = expand_shift (dir, word_mode, x, shift, ri, 1);
2095 if (x != ri)
2096 emit_move_insn (ri, x);
2098 else
2099 move_block_to_reg (REGNO (reg), mem, nregs, args[i].mode);
2102 /* When a parameter is a block, and perhaps in other cases, it is
2103 possible that it did a load from an argument slot that was
2104 already clobbered. */
2105 if (is_sibcall
2106 && check_sibcall_argument_overlap (before_arg, &args[i], 0))
2107 *sibcall_failure = 1;
2109 /* Handle calls that pass values in multiple non-contiguous
2110 locations. The Irix 6 ABI has examples of this. */
2111 if (GET_CODE (reg) == PARALLEL)
2112 use_group_regs (call_fusage, reg);
2113 else if (nregs == -1)
2114 use_reg_mode (call_fusage, reg,
2115 TYPE_MODE (TREE_TYPE (args[i].tree_value)));
2116 else if (nregs > 0)
2117 use_regs (call_fusage, REGNO (reg), nregs);
2122 /* We need to pop PENDING_STACK_ADJUST bytes. But, if the arguments
2123 wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
2124 bytes, then we would need to push some additional bytes to pad the
2125 arguments. So, we compute an adjust to the stack pointer for an
2126 amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
2127 bytes. Then, when the arguments are pushed the stack will be perfectly
2128 aligned. ARGS_SIZE->CONSTANT is set to the number of bytes that should
2129 be popped after the call. Returns the adjustment. */
2131 static int
2132 combine_pending_stack_adjustment_and_call (int unadjusted_args_size,
2133 struct args_size *args_size,
2134 unsigned int preferred_unit_stack_boundary)
2136 /* The number of bytes to pop so that the stack will be
2137 under-aligned by UNADJUSTED_ARGS_SIZE bytes. */
2138 HOST_WIDE_INT adjustment;
2139 /* The alignment of the stack after the arguments are pushed, if we
2140 just pushed the arguments without adjust the stack here. */
2141 unsigned HOST_WIDE_INT unadjusted_alignment;
2143 unadjusted_alignment
2144 = ((stack_pointer_delta + unadjusted_args_size)
2145 % preferred_unit_stack_boundary);
2147 /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
2148 as possible -- leaving just enough left to cancel out the
2149 UNADJUSTED_ALIGNMENT. In other words, we want to ensure that the
2150 PENDING_STACK_ADJUST is non-negative, and congruent to
2151 -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY. */
2153 /* Begin by trying to pop all the bytes. */
2154 unadjusted_alignment
2155 = (unadjusted_alignment
2156 - (pending_stack_adjust % preferred_unit_stack_boundary));
2157 adjustment = pending_stack_adjust;
2158 /* Push enough additional bytes that the stack will be aligned
2159 after the arguments are pushed. */
2160 if (preferred_unit_stack_boundary > 1)
2162 if (unadjusted_alignment > 0)
2163 adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
2164 else
2165 adjustment += unadjusted_alignment;
2168 /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
2169 bytes after the call. The right number is the entire
2170 PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
2171 by the arguments in the first place. */
2172 args_size->constant
2173 = pending_stack_adjust - adjustment + unadjusted_args_size;
2175 return adjustment;
2178 /* Scan X expression if it does not dereference any argument slots
2179 we already clobbered by tail call arguments (as noted in stored_args_map
2180 bitmap).
2181 Return nonzero if X expression dereferences such argument slots,
2182 zero otherwise. */
2184 static int
2185 check_sibcall_argument_overlap_1 (rtx x)
2187 RTX_CODE code;
2188 int i, j;
2189 const char *fmt;
2191 if (x == NULL_RTX)
2192 return 0;
2194 code = GET_CODE (x);
2196 /* We need not check the operands of the CALL expression itself. */
2197 if (code == CALL)
2198 return 0;
2200 if (code == MEM)
2201 return mem_overlaps_already_clobbered_arg_p (XEXP (x, 0),
2202 GET_MODE_SIZE (GET_MODE (x)));
2204 /* Scan all subexpressions. */
2205 fmt = GET_RTX_FORMAT (code);
2206 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2208 if (*fmt == 'e')
2210 if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
2211 return 1;
2213 else if (*fmt == 'E')
2215 for (j = 0; j < XVECLEN (x, i); j++)
2216 if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
2217 return 1;
2220 return 0;
2223 /* Scan sequence after INSN if it does not dereference any argument slots
2224 we already clobbered by tail call arguments (as noted in stored_args_map
2225 bitmap). If MARK_STORED_ARGS_MAP, add stack slots for ARG to
2226 stored_args_map bitmap afterwards (when ARG is a register MARK_STORED_ARGS_MAP
2227 should be 0). Return nonzero if sequence after INSN dereferences such argument
2228 slots, zero otherwise. */
2230 static int
2231 check_sibcall_argument_overlap (rtx_insn *insn, struct arg_data *arg,
2232 int mark_stored_args_map)
2234 int low, high;
2236 if (insn == NULL_RTX)
2237 insn = get_insns ();
2238 else
2239 insn = NEXT_INSN (insn);
2241 for (; insn; insn = NEXT_INSN (insn))
2242 if (INSN_P (insn)
2243 && check_sibcall_argument_overlap_1 (PATTERN (insn)))
2244 break;
2246 if (mark_stored_args_map)
2248 #ifdef ARGS_GROW_DOWNWARD
2249 low = -arg->locate.slot_offset.constant - arg->locate.size.constant;
2250 #else
2251 low = arg->locate.slot_offset.constant;
2252 #endif
2254 for (high = low + arg->locate.size.constant; low < high; low++)
2255 bitmap_set_bit (stored_args_map, low);
2257 return insn != NULL_RTX;
2260 /* Given that a function returns a value of mode MODE at the most
2261 significant end of hard register VALUE, shift VALUE left or right
2262 as specified by LEFT_P. Return true if some action was needed. */
2264 bool
2265 shift_return_value (machine_mode mode, bool left_p, rtx value)
2267 HOST_WIDE_INT shift;
2269 gcc_assert (REG_P (value) && HARD_REGISTER_P (value));
2270 shift = GET_MODE_BITSIZE (GET_MODE (value)) - GET_MODE_BITSIZE (mode);
2271 if (shift == 0)
2272 return false;
2274 /* Use ashr rather than lshr for right shifts. This is for the benefit
2275 of the MIPS port, which requires SImode values to be sign-extended
2276 when stored in 64-bit registers. */
2277 if (!force_expand_binop (GET_MODE (value), left_p ? ashl_optab : ashr_optab,
2278 value, GEN_INT (shift), value, 1, OPTAB_WIDEN))
2279 gcc_unreachable ();
2280 return true;
2283 /* If X is a likely-spilled register value, copy it to a pseudo
2284 register and return that register. Return X otherwise. */
2286 static rtx
2287 avoid_likely_spilled_reg (rtx x)
2289 rtx new_rtx;
2291 if (REG_P (x)
2292 && HARD_REGISTER_P (x)
2293 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (x))))
2295 /* Make sure that we generate a REG rather than a CONCAT.
2296 Moves into CONCATs can need nontrivial instructions,
2297 and the whole point of this function is to avoid
2298 using the hard register directly in such a situation. */
2299 generating_concat_p = 0;
2300 new_rtx = gen_reg_rtx (GET_MODE (x));
2301 generating_concat_p = 1;
2302 emit_move_insn (new_rtx, x);
2303 return new_rtx;
2305 return x;
2308 /* Generate all the code for a CALL_EXPR exp
2309 and return an rtx for its value.
2310 Store the value in TARGET (specified as an rtx) if convenient.
2311 If the value is stored in TARGET then TARGET is returned.
2312 If IGNORE is nonzero, then we ignore the value of the function call. */
2315 expand_call (tree exp, rtx target, int ignore)
2317 /* Nonzero if we are currently expanding a call. */
2318 static int currently_expanding_call = 0;
2320 /* RTX for the function to be called. */
2321 rtx funexp;
2322 /* Sequence of insns to perform a normal "call". */
2323 rtx_insn *normal_call_insns = NULL;
2324 /* Sequence of insns to perform a tail "call". */
2325 rtx_insn *tail_call_insns = NULL;
2326 /* Data type of the function. */
2327 tree funtype;
2328 tree type_arg_types;
2329 tree rettype;
2330 /* Declaration of the function being called,
2331 or 0 if the function is computed (not known by name). */
2332 tree fndecl = 0;
2333 /* The type of the function being called. */
2334 tree fntype;
2335 bool try_tail_call = CALL_EXPR_TAILCALL (exp);
2336 int pass;
2338 /* Register in which non-BLKmode value will be returned,
2339 or 0 if no value or if value is BLKmode. */
2340 rtx valreg;
2341 /* Register(s) in which bounds are returned. */
2342 rtx valbnd = NULL;
2343 /* Address where we should return a BLKmode value;
2344 0 if value not BLKmode. */
2345 rtx structure_value_addr = 0;
2346 /* Nonzero if that address is being passed by treating it as
2347 an extra, implicit first parameter. Otherwise,
2348 it is passed by being copied directly into struct_value_rtx. */
2349 int structure_value_addr_parm = 0;
2350 /* Holds the value of implicit argument for the struct value. */
2351 tree structure_value_addr_value = NULL_TREE;
2352 /* Size of aggregate value wanted, or zero if none wanted
2353 or if we are using the non-reentrant PCC calling convention
2354 or expecting the value in registers. */
2355 HOST_WIDE_INT struct_value_size = 0;
2356 /* Nonzero if called function returns an aggregate in memory PCC style,
2357 by returning the address of where to find it. */
2358 int pcc_struct_value = 0;
2359 rtx struct_value = 0;
2361 /* Number of actual parameters in this call, including struct value addr. */
2362 int num_actuals;
2363 /* Number of named args. Args after this are anonymous ones
2364 and they must all go on the stack. */
2365 int n_named_args;
2366 /* Number of complex actual arguments that need to be split. */
2367 int num_complex_actuals = 0;
2369 /* Vector of information about each argument.
2370 Arguments are numbered in the order they will be pushed,
2371 not the order they are written. */
2372 struct arg_data *args;
2374 /* Total size in bytes of all the stack-parms scanned so far. */
2375 struct args_size args_size;
2376 struct args_size adjusted_args_size;
2377 /* Size of arguments before any adjustments (such as rounding). */
2378 int unadjusted_args_size;
2379 /* Data on reg parms scanned so far. */
2380 CUMULATIVE_ARGS args_so_far_v;
2381 cumulative_args_t args_so_far;
2382 /* Nonzero if a reg parm has been scanned. */
2383 int reg_parm_seen;
2384 /* Nonzero if this is an indirect function call. */
2386 /* Nonzero if we must avoid push-insns in the args for this call.
2387 If stack space is allocated for register parameters, but not by the
2388 caller, then it is preallocated in the fixed part of the stack frame.
2389 So the entire argument block must then be preallocated (i.e., we
2390 ignore PUSH_ROUNDING in that case). */
2392 int must_preallocate = !PUSH_ARGS;
2394 /* Size of the stack reserved for parameter registers. */
2395 int reg_parm_stack_space = 0;
2397 /* Address of space preallocated for stack parms
2398 (on machines that lack push insns), or 0 if space not preallocated. */
2399 rtx argblock = 0;
2401 /* Mask of ECF_ and ERF_ flags. */
2402 int flags = 0;
2403 int return_flags = 0;
2404 #ifdef REG_PARM_STACK_SPACE
2405 /* Define the boundary of the register parm stack space that needs to be
2406 saved, if any. */
2407 int low_to_save, high_to_save;
2408 rtx save_area = 0; /* Place that it is saved */
2409 #endif
2411 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
2412 char *initial_stack_usage_map = stack_usage_map;
2413 char *stack_usage_map_buf = NULL;
2415 int old_stack_allocated;
2417 /* State variables to track stack modifications. */
2418 rtx old_stack_level = 0;
2419 int old_stack_arg_under_construction = 0;
2420 int old_pending_adj = 0;
2421 int old_inhibit_defer_pop = inhibit_defer_pop;
2423 /* Some stack pointer alterations we make are performed via
2424 allocate_dynamic_stack_space. This modifies the stack_pointer_delta,
2425 which we then also need to save/restore along the way. */
2426 int old_stack_pointer_delta = 0;
2428 rtx call_fusage;
2429 tree addr = CALL_EXPR_FN (exp);
2430 int i;
2431 /* The alignment of the stack, in bits. */
2432 unsigned HOST_WIDE_INT preferred_stack_boundary;
2433 /* The alignment of the stack, in bytes. */
2434 unsigned HOST_WIDE_INT preferred_unit_stack_boundary;
2435 /* The static chain value to use for this call. */
2436 rtx static_chain_value;
2437 /* See if this is "nothrow" function call. */
2438 if (TREE_NOTHROW (exp))
2439 flags |= ECF_NOTHROW;
2441 /* See if we can find a DECL-node for the actual function, and get the
2442 function attributes (flags) from the function decl or type node. */
2443 fndecl = get_callee_fndecl (exp);
2444 if (fndecl)
2446 fntype = TREE_TYPE (fndecl);
2447 flags |= flags_from_decl_or_type (fndecl);
2448 return_flags |= decl_return_flags (fndecl);
2450 else
2452 fntype = TREE_TYPE (TREE_TYPE (addr));
2453 flags |= flags_from_decl_or_type (fntype);
2455 rettype = TREE_TYPE (exp);
2457 struct_value = targetm.calls.struct_value_rtx (fntype, 0);
2459 /* Warn if this value is an aggregate type,
2460 regardless of which calling convention we are using for it. */
2461 if (AGGREGATE_TYPE_P (rettype))
2462 warning (OPT_Waggregate_return, "function call has aggregate value");
2464 /* If the result of a non looping pure or const function call is
2465 ignored (or void), and none of its arguments are volatile, we can
2466 avoid expanding the call and just evaluate the arguments for
2467 side-effects. */
2468 if ((flags & (ECF_CONST | ECF_PURE))
2469 && (!(flags & ECF_LOOPING_CONST_OR_PURE))
2470 && (ignore || target == const0_rtx
2471 || TYPE_MODE (rettype) == VOIDmode))
2473 bool volatilep = false;
2474 tree arg;
2475 call_expr_arg_iterator iter;
2477 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2478 if (TREE_THIS_VOLATILE (arg))
2480 volatilep = true;
2481 break;
2484 if (! volatilep)
2486 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2487 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
2488 return const0_rtx;
2492 #ifdef REG_PARM_STACK_SPACE
2493 reg_parm_stack_space = REG_PARM_STACK_SPACE (!fndecl ? fntype : fndecl);
2494 #endif
2496 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
2497 && reg_parm_stack_space > 0 && PUSH_ARGS)
2498 must_preallocate = 1;
2500 /* Set up a place to return a structure. */
2502 /* Cater to broken compilers. */
2503 if (aggregate_value_p (exp, fntype))
2505 /* This call returns a big structure. */
2506 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
2508 #ifdef PCC_STATIC_STRUCT_RETURN
2510 pcc_struct_value = 1;
2512 #else /* not PCC_STATIC_STRUCT_RETURN */
2514 struct_value_size = int_size_in_bytes (rettype);
2516 /* Even if it is semantically safe to use the target as the return
2517 slot, it may be not sufficiently aligned for the return type. */
2518 if (CALL_EXPR_RETURN_SLOT_OPT (exp)
2519 && target
2520 && MEM_P (target)
2521 && !(MEM_ALIGN (target) < TYPE_ALIGN (rettype)
2522 && SLOW_UNALIGNED_ACCESS (TYPE_MODE (rettype),
2523 MEM_ALIGN (target))))
2524 structure_value_addr = XEXP (target, 0);
2525 else
2527 /* For variable-sized objects, we must be called with a target
2528 specified. If we were to allocate space on the stack here,
2529 we would have no way of knowing when to free it. */
2530 rtx d = assign_temp (rettype, 1, 1);
2531 structure_value_addr = XEXP (d, 0);
2532 target = 0;
2535 #endif /* not PCC_STATIC_STRUCT_RETURN */
2538 /* Figure out the amount to which the stack should be aligned. */
2539 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
2540 if (fndecl)
2542 struct cgraph_rtl_info *i = cgraph_node::rtl_info (fndecl);
2543 /* Without automatic stack alignment, we can't increase preferred
2544 stack boundary. With automatic stack alignment, it is
2545 unnecessary since unless we can guarantee that all callers will
2546 align the outgoing stack properly, callee has to align its
2547 stack anyway. */
2548 if (i
2549 && i->preferred_incoming_stack_boundary
2550 && i->preferred_incoming_stack_boundary < preferred_stack_boundary)
2551 preferred_stack_boundary = i->preferred_incoming_stack_boundary;
2554 /* Operand 0 is a pointer-to-function; get the type of the function. */
2555 funtype = TREE_TYPE (addr);
2556 gcc_assert (POINTER_TYPE_P (funtype));
2557 funtype = TREE_TYPE (funtype);
2559 /* Count whether there are actual complex arguments that need to be split
2560 into their real and imaginary parts. Munge the type_arg_types
2561 appropriately here as well. */
2562 if (targetm.calls.split_complex_arg)
2564 call_expr_arg_iterator iter;
2565 tree arg;
2566 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2568 tree type = TREE_TYPE (arg);
2569 if (type && TREE_CODE (type) == COMPLEX_TYPE
2570 && targetm.calls.split_complex_arg (type))
2571 num_complex_actuals++;
2573 type_arg_types = split_complex_types (TYPE_ARG_TYPES (funtype));
2575 else
2576 type_arg_types = TYPE_ARG_TYPES (funtype);
2578 if (flags & ECF_MAY_BE_ALLOCA)
2579 cfun->calls_alloca = 1;
2581 /* If struct_value_rtx is 0, it means pass the address
2582 as if it were an extra parameter. Put the argument expression
2583 in structure_value_addr_value. */
2584 if (structure_value_addr && struct_value == 0)
2586 /* If structure_value_addr is a REG other than
2587 virtual_outgoing_args_rtx, we can use always use it. If it
2588 is not a REG, we must always copy it into a register.
2589 If it is virtual_outgoing_args_rtx, we must copy it to another
2590 register in some cases. */
2591 rtx temp = (!REG_P (structure_value_addr)
2592 || (ACCUMULATE_OUTGOING_ARGS
2593 && stack_arg_under_construction
2594 && structure_value_addr == virtual_outgoing_args_rtx)
2595 ? copy_addr_to_reg (convert_memory_address
2596 (Pmode, structure_value_addr))
2597 : structure_value_addr);
2599 structure_value_addr_value =
2600 make_tree (build_pointer_type (TREE_TYPE (funtype)), temp);
2601 structure_value_addr_parm = CALL_WITH_BOUNDS_P (exp) ? 2 : 1;
2604 /* Count the arguments and set NUM_ACTUALS. */
2605 num_actuals =
2606 call_expr_nargs (exp) + num_complex_actuals + structure_value_addr_parm;
2608 /* Compute number of named args.
2609 First, do a raw count of the args for INIT_CUMULATIVE_ARGS. */
2611 if (type_arg_types != 0)
2612 n_named_args
2613 = (list_length (type_arg_types)
2614 /* Count the struct value address, if it is passed as a parm. */
2615 + structure_value_addr_parm);
2616 else
2617 /* If we know nothing, treat all args as named. */
2618 n_named_args = num_actuals;
2620 /* Start updating where the next arg would go.
2622 On some machines (such as the PA) indirect calls have a different
2623 calling convention than normal calls. The fourth argument in
2624 INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
2625 or not. */
2626 INIT_CUMULATIVE_ARGS (args_so_far_v, funtype, NULL_RTX, fndecl, n_named_args);
2627 args_so_far = pack_cumulative_args (&args_so_far_v);
2629 /* Now possibly adjust the number of named args.
2630 Normally, don't include the last named arg if anonymous args follow.
2631 We do include the last named arg if
2632 targetm.calls.strict_argument_naming() returns nonzero.
2633 (If no anonymous args follow, the result of list_length is actually
2634 one too large. This is harmless.)
2636 If targetm.calls.pretend_outgoing_varargs_named() returns
2637 nonzero, and targetm.calls.strict_argument_naming() returns zero,
2638 this machine will be able to place unnamed args that were passed
2639 in registers into the stack. So treat all args as named. This
2640 allows the insns emitting for a specific argument list to be
2641 independent of the function declaration.
2643 If targetm.calls.pretend_outgoing_varargs_named() returns zero,
2644 we do not have any reliable way to pass unnamed args in
2645 registers, so we must force them into memory. */
2647 if (type_arg_types != 0
2648 && targetm.calls.strict_argument_naming (args_so_far))
2650 else if (type_arg_types != 0
2651 && ! targetm.calls.pretend_outgoing_varargs_named (args_so_far))
2652 /* Don't include the last named arg. */
2653 --n_named_args;
2654 else
2655 /* Treat all args as named. */
2656 n_named_args = num_actuals;
2658 /* Make a vector to hold all the information about each arg. */
2659 args = XALLOCAVEC (struct arg_data, num_actuals);
2660 memset (args, 0, num_actuals * sizeof (struct arg_data));
2662 /* Build up entries in the ARGS array, compute the size of the
2663 arguments into ARGS_SIZE, etc. */
2664 initialize_argument_information (num_actuals, args, &args_size,
2665 n_named_args, exp,
2666 structure_value_addr_value, fndecl, fntype,
2667 args_so_far, reg_parm_stack_space,
2668 &old_stack_level, &old_pending_adj,
2669 &must_preallocate, &flags,
2670 &try_tail_call, CALL_FROM_THUNK_P (exp));
2672 if (args_size.var)
2673 must_preallocate = 1;
2675 /* Now make final decision about preallocating stack space. */
2676 must_preallocate = finalize_must_preallocate (must_preallocate,
2677 num_actuals, args,
2678 &args_size);
2680 /* If the structure value address will reference the stack pointer, we
2681 must stabilize it. We don't need to do this if we know that we are
2682 not going to adjust the stack pointer in processing this call. */
2684 if (structure_value_addr
2685 && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
2686 || reg_mentioned_p (virtual_outgoing_args_rtx,
2687 structure_value_addr))
2688 && (args_size.var
2689 || (!ACCUMULATE_OUTGOING_ARGS && args_size.constant)))
2690 structure_value_addr = copy_to_reg (structure_value_addr);
2692 /* Tail calls can make things harder to debug, and we've traditionally
2693 pushed these optimizations into -O2. Don't try if we're already
2694 expanding a call, as that means we're an argument. Don't try if
2695 there's cleanups, as we know there's code to follow the call. */
2697 if (currently_expanding_call++ != 0
2698 || !flag_optimize_sibling_calls
2699 || args_size.var
2700 || dbg_cnt (tail_call) == false)
2701 try_tail_call = 0;
2703 /* Rest of purposes for tail call optimizations to fail. */
2704 if (
2705 #ifdef HAVE_sibcall_epilogue
2706 !HAVE_sibcall_epilogue
2707 #else
2709 #endif
2710 || !try_tail_call
2711 /* Doing sibling call optimization needs some work, since
2712 structure_value_addr can be allocated on the stack.
2713 It does not seem worth the effort since few optimizable
2714 sibling calls will return a structure. */
2715 || structure_value_addr != NULL_RTX
2716 #ifdef REG_PARM_STACK_SPACE
2717 /* If outgoing reg parm stack space changes, we can not do sibcall. */
2718 || (OUTGOING_REG_PARM_STACK_SPACE (funtype)
2719 != OUTGOING_REG_PARM_STACK_SPACE (TREE_TYPE (current_function_decl)))
2720 || (reg_parm_stack_space != REG_PARM_STACK_SPACE (current_function_decl))
2721 #endif
2722 /* Check whether the target is able to optimize the call
2723 into a sibcall. */
2724 || !targetm.function_ok_for_sibcall (fndecl, exp)
2725 /* Functions that do not return exactly once may not be sibcall
2726 optimized. */
2727 || (flags & (ECF_RETURNS_TWICE | ECF_NORETURN))
2728 || TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (addr)))
2729 /* If the called function is nested in the current one, it might access
2730 some of the caller's arguments, but could clobber them beforehand if
2731 the argument areas are shared. */
2732 || (fndecl && decl_function_context (fndecl) == current_function_decl)
2733 /* If this function requires more stack slots than the current
2734 function, we cannot change it into a sibling call.
2735 crtl->args.pretend_args_size is not part of the
2736 stack allocated by our caller. */
2737 || args_size.constant > (crtl->args.size
2738 - crtl->args.pretend_args_size)
2739 /* If the callee pops its own arguments, then it must pop exactly
2740 the same number of arguments as the current function. */
2741 || (targetm.calls.return_pops_args (fndecl, funtype, args_size.constant)
2742 != targetm.calls.return_pops_args (current_function_decl,
2743 TREE_TYPE (current_function_decl),
2744 crtl->args.size))
2745 || !lang_hooks.decls.ok_for_sibcall (fndecl))
2746 try_tail_call = 0;
2748 /* Check if caller and callee disagree in promotion of function
2749 return value. */
2750 if (try_tail_call)
2752 machine_mode caller_mode, caller_promoted_mode;
2753 machine_mode callee_mode, callee_promoted_mode;
2754 int caller_unsignedp, callee_unsignedp;
2755 tree caller_res = DECL_RESULT (current_function_decl);
2757 caller_unsignedp = TYPE_UNSIGNED (TREE_TYPE (caller_res));
2758 caller_mode = DECL_MODE (caller_res);
2759 callee_unsignedp = TYPE_UNSIGNED (TREE_TYPE (funtype));
2760 callee_mode = TYPE_MODE (TREE_TYPE (funtype));
2761 caller_promoted_mode
2762 = promote_function_mode (TREE_TYPE (caller_res), caller_mode,
2763 &caller_unsignedp,
2764 TREE_TYPE (current_function_decl), 1);
2765 callee_promoted_mode
2766 = promote_function_mode (TREE_TYPE (funtype), callee_mode,
2767 &callee_unsignedp,
2768 funtype, 1);
2769 if (caller_mode != VOIDmode
2770 && (caller_promoted_mode != callee_promoted_mode
2771 || ((caller_mode != caller_promoted_mode
2772 || callee_mode != callee_promoted_mode)
2773 && (caller_unsignedp != callee_unsignedp
2774 || GET_MODE_BITSIZE (caller_mode)
2775 < GET_MODE_BITSIZE (callee_mode)))))
2776 try_tail_call = 0;
2779 /* Ensure current function's preferred stack boundary is at least
2780 what we need. Stack alignment may also increase preferred stack
2781 boundary. */
2782 if (crtl->preferred_stack_boundary < preferred_stack_boundary)
2783 crtl->preferred_stack_boundary = preferred_stack_boundary;
2784 else
2785 preferred_stack_boundary = crtl->preferred_stack_boundary;
2787 preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
2789 /* We want to make two insn chains; one for a sibling call, the other
2790 for a normal call. We will select one of the two chains after
2791 initial RTL generation is complete. */
2792 for (pass = try_tail_call ? 0 : 1; pass < 2; pass++)
2794 int sibcall_failure = 0;
2795 /* We want to emit any pending stack adjustments before the tail
2796 recursion "call". That way we know any adjustment after the tail
2797 recursion call can be ignored if we indeed use the tail
2798 call expansion. */
2799 saved_pending_stack_adjust save;
2800 rtx_insn *insns, *before_call, *after_args;
2801 rtx next_arg_reg;
2803 if (pass == 0)
2805 /* State variables we need to save and restore between
2806 iterations. */
2807 save_pending_stack_adjust (&save);
2809 if (pass)
2810 flags &= ~ECF_SIBCALL;
2811 else
2812 flags |= ECF_SIBCALL;
2814 /* Other state variables that we must reinitialize each time
2815 through the loop (that are not initialized by the loop itself). */
2816 argblock = 0;
2817 call_fusage = 0;
2819 /* Start a new sequence for the normal call case.
2821 From this point on, if the sibling call fails, we want to set
2822 sibcall_failure instead of continuing the loop. */
2823 start_sequence ();
2825 /* Don't let pending stack adjusts add up to too much.
2826 Also, do all pending adjustments now if there is any chance
2827 this might be a call to alloca or if we are expanding a sibling
2828 call sequence.
2829 Also do the adjustments before a throwing call, otherwise
2830 exception handling can fail; PR 19225. */
2831 if (pending_stack_adjust >= 32
2832 || (pending_stack_adjust > 0
2833 && (flags & ECF_MAY_BE_ALLOCA))
2834 || (pending_stack_adjust > 0
2835 && flag_exceptions && !(flags & ECF_NOTHROW))
2836 || pass == 0)
2837 do_pending_stack_adjust ();
2839 /* Precompute any arguments as needed. */
2840 if (pass)
2841 precompute_arguments (num_actuals, args);
2843 /* Now we are about to start emitting insns that can be deleted
2844 if a libcall is deleted. */
2845 if (pass && (flags & ECF_MALLOC))
2846 start_sequence ();
2848 if (pass == 0 && crtl->stack_protect_guard)
2849 stack_protect_epilogue ();
2851 adjusted_args_size = args_size;
2852 /* Compute the actual size of the argument block required. The variable
2853 and constant sizes must be combined, the size may have to be rounded,
2854 and there may be a minimum required size. When generating a sibcall
2855 pattern, do not round up, since we'll be re-using whatever space our
2856 caller provided. */
2857 unadjusted_args_size
2858 = compute_argument_block_size (reg_parm_stack_space,
2859 &adjusted_args_size,
2860 fndecl, fntype,
2861 (pass == 0 ? 0
2862 : preferred_stack_boundary));
2864 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
2866 /* The argument block when performing a sibling call is the
2867 incoming argument block. */
2868 if (pass == 0)
2870 argblock = crtl->args.internal_arg_pointer;
2871 argblock
2872 #ifdef STACK_GROWS_DOWNWARD
2873 = plus_constant (Pmode, argblock, crtl->args.pretend_args_size);
2874 #else
2875 = plus_constant (Pmode, argblock, -crtl->args.pretend_args_size);
2876 #endif
2877 stored_args_map = sbitmap_alloc (args_size.constant);
2878 bitmap_clear (stored_args_map);
2881 /* If we have no actual push instructions, or shouldn't use them,
2882 make space for all args right now. */
2883 else if (adjusted_args_size.var != 0)
2885 if (old_stack_level == 0)
2887 emit_stack_save (SAVE_BLOCK, &old_stack_level);
2888 old_stack_pointer_delta = stack_pointer_delta;
2889 old_pending_adj = pending_stack_adjust;
2890 pending_stack_adjust = 0;
2891 /* stack_arg_under_construction says whether a stack arg is
2892 being constructed at the old stack level. Pushing the stack
2893 gets a clean outgoing argument block. */
2894 old_stack_arg_under_construction = stack_arg_under_construction;
2895 stack_arg_under_construction = 0;
2897 argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
2898 if (flag_stack_usage_info)
2899 current_function_has_unbounded_dynamic_stack_size = 1;
2901 else
2903 /* Note that we must go through the motions of allocating an argument
2904 block even if the size is zero because we may be storing args
2905 in the area reserved for register arguments, which may be part of
2906 the stack frame. */
2908 int needed = adjusted_args_size.constant;
2910 /* Store the maximum argument space used. It will be pushed by
2911 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
2912 checking). */
2914 if (needed > crtl->outgoing_args_size)
2915 crtl->outgoing_args_size = needed;
2917 if (must_preallocate)
2919 if (ACCUMULATE_OUTGOING_ARGS)
2921 /* Since the stack pointer will never be pushed, it is
2922 possible for the evaluation of a parm to clobber
2923 something we have already written to the stack.
2924 Since most function calls on RISC machines do not use
2925 the stack, this is uncommon, but must work correctly.
2927 Therefore, we save any area of the stack that was already
2928 written and that we are using. Here we set up to do this
2929 by making a new stack usage map from the old one. The
2930 actual save will be done by store_one_arg.
2932 Another approach might be to try to reorder the argument
2933 evaluations to avoid this conflicting stack usage. */
2935 /* Since we will be writing into the entire argument area,
2936 the map must be allocated for its entire size, not just
2937 the part that is the responsibility of the caller. */
2938 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
2939 needed += reg_parm_stack_space;
2941 #ifdef ARGS_GROW_DOWNWARD
2942 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2943 needed + 1);
2944 #else
2945 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2946 needed);
2947 #endif
2948 free (stack_usage_map_buf);
2949 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
2950 stack_usage_map = stack_usage_map_buf;
2952 if (initial_highest_arg_in_use)
2953 memcpy (stack_usage_map, initial_stack_usage_map,
2954 initial_highest_arg_in_use);
2956 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
2957 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
2958 (highest_outgoing_arg_in_use
2959 - initial_highest_arg_in_use));
2960 needed = 0;
2962 /* The address of the outgoing argument list must not be
2963 copied to a register here, because argblock would be left
2964 pointing to the wrong place after the call to
2965 allocate_dynamic_stack_space below. */
2967 argblock = virtual_outgoing_args_rtx;
2969 else
2971 if (inhibit_defer_pop == 0)
2973 /* Try to reuse some or all of the pending_stack_adjust
2974 to get this space. */
2975 needed
2976 = (combine_pending_stack_adjustment_and_call
2977 (unadjusted_args_size,
2978 &adjusted_args_size,
2979 preferred_unit_stack_boundary));
2981 /* combine_pending_stack_adjustment_and_call computes
2982 an adjustment before the arguments are allocated.
2983 Account for them and see whether or not the stack
2984 needs to go up or down. */
2985 needed = unadjusted_args_size - needed;
2987 if (needed < 0)
2989 /* We're releasing stack space. */
2990 /* ??? We can avoid any adjustment at all if we're
2991 already aligned. FIXME. */
2992 pending_stack_adjust = -needed;
2993 do_pending_stack_adjust ();
2994 needed = 0;
2996 else
2997 /* We need to allocate space. We'll do that in
2998 push_block below. */
2999 pending_stack_adjust = 0;
3002 /* Special case this because overhead of `push_block' in
3003 this case is non-trivial. */
3004 if (needed == 0)
3005 argblock = virtual_outgoing_args_rtx;
3006 else
3008 argblock = push_block (GEN_INT (needed), 0, 0);
3009 #ifdef ARGS_GROW_DOWNWARD
3010 argblock = plus_constant (Pmode, argblock, needed);
3011 #endif
3014 /* We only really need to call `copy_to_reg' in the case
3015 where push insns are going to be used to pass ARGBLOCK
3016 to a function call in ARGS. In that case, the stack
3017 pointer changes value from the allocation point to the
3018 call point, and hence the value of
3019 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
3020 as well always do it. */
3021 argblock = copy_to_reg (argblock);
3026 if (ACCUMULATE_OUTGOING_ARGS)
3028 /* The save/restore code in store_one_arg handles all
3029 cases except one: a constructor call (including a C
3030 function returning a BLKmode struct) to initialize
3031 an argument. */
3032 if (stack_arg_under_construction)
3034 rtx push_size
3035 = GEN_INT (adjusted_args_size.constant
3036 + (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype
3037 : TREE_TYPE (fndecl))) ? 0
3038 : reg_parm_stack_space));
3039 if (old_stack_level == 0)
3041 emit_stack_save (SAVE_BLOCK, &old_stack_level);
3042 old_stack_pointer_delta = stack_pointer_delta;
3043 old_pending_adj = pending_stack_adjust;
3044 pending_stack_adjust = 0;
3045 /* stack_arg_under_construction says whether a stack
3046 arg is being constructed at the old stack level.
3047 Pushing the stack gets a clean outgoing argument
3048 block. */
3049 old_stack_arg_under_construction
3050 = stack_arg_under_construction;
3051 stack_arg_under_construction = 0;
3052 /* Make a new map for the new argument list. */
3053 free (stack_usage_map_buf);
3054 stack_usage_map_buf = XCNEWVEC (char, highest_outgoing_arg_in_use);
3055 stack_usage_map = stack_usage_map_buf;
3056 highest_outgoing_arg_in_use = 0;
3058 /* We can pass TRUE as the 4th argument because we just
3059 saved the stack pointer and will restore it right after
3060 the call. */
3061 allocate_dynamic_stack_space (push_size, 0,
3062 BIGGEST_ALIGNMENT, true);
3065 /* If argument evaluation might modify the stack pointer,
3066 copy the address of the argument list to a register. */
3067 for (i = 0; i < num_actuals; i++)
3068 if (args[i].pass_on_stack)
3070 argblock = copy_addr_to_reg (argblock);
3071 break;
3075 compute_argument_addresses (args, argblock, num_actuals);
3077 /* Perform stack alignment before the first push (the last arg). */
3078 if (argblock == 0
3079 && adjusted_args_size.constant > reg_parm_stack_space
3080 && adjusted_args_size.constant != unadjusted_args_size)
3082 /* When the stack adjustment is pending, we get better code
3083 by combining the adjustments. */
3084 if (pending_stack_adjust
3085 && ! inhibit_defer_pop)
3087 pending_stack_adjust
3088 = (combine_pending_stack_adjustment_and_call
3089 (unadjusted_args_size,
3090 &adjusted_args_size,
3091 preferred_unit_stack_boundary));
3092 do_pending_stack_adjust ();
3094 else if (argblock == 0)
3095 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
3096 - unadjusted_args_size));
3098 /* Now that the stack is properly aligned, pops can't safely
3099 be deferred during the evaluation of the arguments. */
3100 NO_DEFER_POP;
3102 /* Record the maximum pushed stack space size. We need to delay
3103 doing it this far to take into account the optimization done
3104 by combine_pending_stack_adjustment_and_call. */
3105 if (flag_stack_usage_info
3106 && !ACCUMULATE_OUTGOING_ARGS
3107 && pass
3108 && adjusted_args_size.var == 0)
3110 int pushed = adjusted_args_size.constant + pending_stack_adjust;
3111 if (pushed > current_function_pushed_stack_size)
3112 current_function_pushed_stack_size = pushed;
3115 funexp = rtx_for_function_call (fndecl, addr);
3117 /* Precompute all register parameters. It isn't safe to compute anything
3118 once we have started filling any specific hard regs. */
3119 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
3121 if (CALL_EXPR_STATIC_CHAIN (exp))
3122 static_chain_value = expand_normal (CALL_EXPR_STATIC_CHAIN (exp));
3123 else
3124 static_chain_value = 0;
3126 #ifdef REG_PARM_STACK_SPACE
3127 /* Save the fixed argument area if it's part of the caller's frame and
3128 is clobbered by argument setup for this call. */
3129 if (ACCUMULATE_OUTGOING_ARGS && pass)
3130 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3131 &low_to_save, &high_to_save);
3132 #endif
3134 /* Now store (and compute if necessary) all non-register parms.
3135 These come before register parms, since they can require block-moves,
3136 which could clobber the registers used for register parms.
3137 Parms which have partial registers are not stored here,
3138 but we do preallocate space here if they want that. */
3140 for (i = 0; i < num_actuals; i++)
3142 /* Delay bounds until all other args are stored. */
3143 if (POINTER_BOUNDS_P (args[i].tree_value))
3144 continue;
3145 else if (args[i].reg == 0 || args[i].pass_on_stack)
3147 rtx_insn *before_arg = get_last_insn ();
3149 /* We don't allow passing huge (> 2^30 B) arguments
3150 by value. It would cause an overflow later on. */
3151 if (adjusted_args_size.constant
3152 >= (1 << (HOST_BITS_PER_INT - 2)))
3154 sorry ("passing too large argument on stack");
3155 continue;
3158 if (store_one_arg (&args[i], argblock, flags,
3159 adjusted_args_size.var != 0,
3160 reg_parm_stack_space)
3161 || (pass == 0
3162 && check_sibcall_argument_overlap (before_arg,
3163 &args[i], 1)))
3164 sibcall_failure = 1;
3167 if (args[i].stack)
3168 call_fusage
3169 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[i].tree_value)),
3170 gen_rtx_USE (VOIDmode, args[i].stack),
3171 call_fusage);
3174 /* If we have a parm that is passed in registers but not in memory
3175 and whose alignment does not permit a direct copy into registers,
3176 make a group of pseudos that correspond to each register that we
3177 will later fill. */
3178 if (STRICT_ALIGNMENT)
3179 store_unaligned_arguments_into_pseudos (args, num_actuals);
3181 /* Now store any partially-in-registers parm.
3182 This is the last place a block-move can happen. */
3183 if (reg_parm_seen)
3184 for (i = 0; i < num_actuals; i++)
3185 if (args[i].partial != 0 && ! args[i].pass_on_stack)
3187 rtx_insn *before_arg = get_last_insn ();
3189 if (store_one_arg (&args[i], argblock, flags,
3190 adjusted_args_size.var != 0,
3191 reg_parm_stack_space)
3192 || (pass == 0
3193 && check_sibcall_argument_overlap (before_arg,
3194 &args[i], 1)))
3195 sibcall_failure = 1;
3198 bool any_regs = false;
3199 for (i = 0; i < num_actuals; i++)
3200 if (args[i].reg != NULL_RTX)
3202 any_regs = true;
3203 targetm.calls.call_args (args[i].reg, funtype);
3205 if (!any_regs)
3206 targetm.calls.call_args (pc_rtx, funtype);
3208 /* Figure out the register where the value, if any, will come back. */
3209 valreg = 0;
3210 valbnd = 0;
3211 if (TYPE_MODE (rettype) != VOIDmode
3212 && ! structure_value_addr)
3214 if (pcc_struct_value)
3216 valreg = hard_function_value (build_pointer_type (rettype),
3217 fndecl, NULL, (pass == 0));
3218 if (CALL_WITH_BOUNDS_P (exp))
3219 valbnd = targetm.calls.
3220 chkp_function_value_bounds (build_pointer_type (rettype),
3221 fndecl, (pass == 0));
3223 else
3225 valreg = hard_function_value (rettype, fndecl, fntype,
3226 (pass == 0));
3227 if (CALL_WITH_BOUNDS_P (exp))
3228 valbnd = targetm.calls.chkp_function_value_bounds (rettype,
3229 fndecl,
3230 (pass == 0));
3233 /* If VALREG is a PARALLEL whose first member has a zero
3234 offset, use that. This is for targets such as m68k that
3235 return the same value in multiple places. */
3236 if (GET_CODE (valreg) == PARALLEL)
3238 rtx elem = XVECEXP (valreg, 0, 0);
3239 rtx where = XEXP (elem, 0);
3240 rtx offset = XEXP (elem, 1);
3241 if (offset == const0_rtx
3242 && GET_MODE (where) == GET_MODE (valreg))
3243 valreg = where;
3247 /* Store all bounds not passed in registers. */
3248 for (i = 0; i < num_actuals; i++)
3250 if (POINTER_BOUNDS_P (args[i].tree_value)
3251 && !args[i].reg)
3252 store_bounds (&args[i],
3253 args[i].pointer_arg == -1
3254 ? NULL
3255 : &args[args[i].pointer_arg]);
3258 /* If register arguments require space on the stack and stack space
3259 was not preallocated, allocate stack space here for arguments
3260 passed in registers. */
3261 if (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
3262 && !ACCUMULATE_OUTGOING_ARGS
3263 && must_preallocate == 0 && reg_parm_stack_space > 0)
3264 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
3266 /* Pass the function the address in which to return a
3267 structure value. */
3268 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
3270 structure_value_addr
3271 = convert_memory_address (Pmode, structure_value_addr);
3272 emit_move_insn (struct_value,
3273 force_reg (Pmode,
3274 force_operand (structure_value_addr,
3275 NULL_RTX)));
3277 if (REG_P (struct_value))
3278 use_reg (&call_fusage, struct_value);
3281 after_args = get_last_insn ();
3282 funexp = prepare_call_address (fndecl ? fndecl : fntype, funexp,
3283 static_chain_value, &call_fusage,
3284 reg_parm_seen, pass == 0);
3286 load_register_parameters (args, num_actuals, &call_fusage, flags,
3287 pass == 0, &sibcall_failure);
3289 /* Save a pointer to the last insn before the call, so that we can
3290 later safely search backwards to find the CALL_INSN. */
3291 before_call = get_last_insn ();
3293 /* Set up next argument register. For sibling calls on machines
3294 with register windows this should be the incoming register. */
3295 if (pass == 0)
3296 next_arg_reg = targetm.calls.function_incoming_arg (args_so_far,
3297 VOIDmode,
3298 void_type_node,
3299 true);
3300 else
3301 next_arg_reg = targetm.calls.function_arg (args_so_far,
3302 VOIDmode, void_type_node,
3303 true);
3305 if (pass == 1 && (return_flags & ERF_RETURNS_ARG))
3307 int arg_nr = return_flags & ERF_RETURN_ARG_MASK;
3308 arg_nr = num_actuals - arg_nr - 1;
3309 if (arg_nr >= 0
3310 && arg_nr < num_actuals
3311 && args[arg_nr].reg
3312 && valreg
3313 && REG_P (valreg)
3314 && GET_MODE (args[arg_nr].reg) == GET_MODE (valreg))
3315 call_fusage
3316 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[arg_nr].tree_value)),
3317 gen_rtx_SET (VOIDmode, valreg, args[arg_nr].reg),
3318 call_fusage);
3320 /* All arguments and registers used for the call must be set up by
3321 now! */
3323 /* Stack must be properly aligned now. */
3324 gcc_assert (!pass
3325 || !(stack_pointer_delta % preferred_unit_stack_boundary));
3327 /* Generate the actual call instruction. */
3328 emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
3329 adjusted_args_size.constant, struct_value_size,
3330 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
3331 flags, args_so_far);
3333 if (flag_use_caller_save)
3335 rtx_call_insn *last;
3336 rtx datum = NULL_RTX;
3337 if (fndecl != NULL_TREE)
3339 datum = XEXP (DECL_RTL (fndecl), 0);
3340 gcc_assert (datum != NULL_RTX
3341 && GET_CODE (datum) == SYMBOL_REF);
3343 last = last_call_insn ();
3344 add_reg_note (last, REG_CALL_DECL, datum);
3347 /* If the call setup or the call itself overlaps with anything
3348 of the argument setup we probably clobbered our call address.
3349 In that case we can't do sibcalls. */
3350 if (pass == 0
3351 && check_sibcall_argument_overlap (after_args, 0, 0))
3352 sibcall_failure = 1;
3354 /* If a non-BLKmode value is returned at the most significant end
3355 of a register, shift the register right by the appropriate amount
3356 and update VALREG accordingly. BLKmode values are handled by the
3357 group load/store machinery below. */
3358 if (!structure_value_addr
3359 && !pcc_struct_value
3360 && TYPE_MODE (rettype) != VOIDmode
3361 && TYPE_MODE (rettype) != BLKmode
3362 && REG_P (valreg)
3363 && targetm.calls.return_in_msb (rettype))
3365 if (shift_return_value (TYPE_MODE (rettype), false, valreg))
3366 sibcall_failure = 1;
3367 valreg = gen_rtx_REG (TYPE_MODE (rettype), REGNO (valreg));
3370 if (pass && (flags & ECF_MALLOC))
3372 rtx temp = gen_reg_rtx (GET_MODE (valreg));
3373 rtx_insn *last, *insns;
3375 /* The return value from a malloc-like function is a pointer. */
3376 if (TREE_CODE (rettype) == POINTER_TYPE)
3377 mark_reg_pointer (temp, MALLOC_ABI_ALIGNMENT);
3379 emit_move_insn (temp, valreg);
3381 /* The return value from a malloc-like function can not alias
3382 anything else. */
3383 last = get_last_insn ();
3384 add_reg_note (last, REG_NOALIAS, temp);
3386 /* Write out the sequence. */
3387 insns = get_insns ();
3388 end_sequence ();
3389 emit_insn (insns);
3390 valreg = temp;
3393 /* For calls to `setjmp', etc., inform
3394 function.c:setjmp_warnings that it should complain if
3395 nonvolatile values are live. For functions that cannot
3396 return, inform flow that control does not fall through. */
3398 if ((flags & ECF_NORETURN) || pass == 0)
3400 /* The barrier must be emitted
3401 immediately after the CALL_INSN. Some ports emit more
3402 than just a CALL_INSN above, so we must search for it here. */
3404 rtx_insn *last = get_last_insn ();
3405 while (!CALL_P (last))
3407 last = PREV_INSN (last);
3408 /* There was no CALL_INSN? */
3409 gcc_assert (last != before_call);
3412 emit_barrier_after (last);
3414 /* Stack adjustments after a noreturn call are dead code.
3415 However when NO_DEFER_POP is in effect, we must preserve
3416 stack_pointer_delta. */
3417 if (inhibit_defer_pop == 0)
3419 stack_pointer_delta = old_stack_allocated;
3420 pending_stack_adjust = 0;
3424 /* If value type not void, return an rtx for the value. */
3426 if (TYPE_MODE (rettype) == VOIDmode
3427 || ignore)
3428 target = const0_rtx;
3429 else if (structure_value_addr)
3431 if (target == 0 || !MEM_P (target))
3433 target
3434 = gen_rtx_MEM (TYPE_MODE (rettype),
3435 memory_address (TYPE_MODE (rettype),
3436 structure_value_addr));
3437 set_mem_attributes (target, rettype, 1);
3440 else if (pcc_struct_value)
3442 /* This is the special C++ case where we need to
3443 know what the true target was. We take care to
3444 never use this value more than once in one expression. */
3445 target = gen_rtx_MEM (TYPE_MODE (rettype),
3446 copy_to_reg (valreg));
3447 set_mem_attributes (target, rettype, 1);
3449 /* Handle calls that return values in multiple non-contiguous locations.
3450 The Irix 6 ABI has examples of this. */
3451 else if (GET_CODE (valreg) == PARALLEL)
3453 if (target == 0)
3454 target = emit_group_move_into_temps (valreg);
3455 else if (rtx_equal_p (target, valreg))
3457 else if (GET_CODE (target) == PARALLEL)
3458 /* Handle the result of a emit_group_move_into_temps
3459 call in the previous pass. */
3460 emit_group_move (target, valreg);
3461 else
3462 emit_group_store (target, valreg, rettype,
3463 int_size_in_bytes (rettype));
3465 else if (target
3466 && GET_MODE (target) == TYPE_MODE (rettype)
3467 && GET_MODE (target) == GET_MODE (valreg))
3469 bool may_overlap = false;
3471 /* We have to copy a return value in a CLASS_LIKELY_SPILLED hard
3472 reg to a plain register. */
3473 if (!REG_P (target) || HARD_REGISTER_P (target))
3474 valreg = avoid_likely_spilled_reg (valreg);
3476 /* If TARGET is a MEM in the argument area, and we have
3477 saved part of the argument area, then we can't store
3478 directly into TARGET as it may get overwritten when we
3479 restore the argument save area below. Don't work too
3480 hard though and simply force TARGET to a register if it
3481 is a MEM; the optimizer is quite likely to sort it out. */
3482 if (ACCUMULATE_OUTGOING_ARGS && pass && MEM_P (target))
3483 for (i = 0; i < num_actuals; i++)
3484 if (args[i].save_area)
3486 may_overlap = true;
3487 break;
3490 if (may_overlap)
3491 target = copy_to_reg (valreg);
3492 else
3494 /* TARGET and VALREG cannot be equal at this point
3495 because the latter would not have
3496 REG_FUNCTION_VALUE_P true, while the former would if
3497 it were referring to the same register.
3499 If they refer to the same register, this move will be
3500 a no-op, except when function inlining is being
3501 done. */
3502 emit_move_insn (target, valreg);
3504 /* If we are setting a MEM, this code must be executed.
3505 Since it is emitted after the call insn, sibcall
3506 optimization cannot be performed in that case. */
3507 if (MEM_P (target))
3508 sibcall_failure = 1;
3511 else
3512 target = copy_to_reg (avoid_likely_spilled_reg (valreg));
3514 /* If we promoted this return value, make the proper SUBREG.
3515 TARGET might be const0_rtx here, so be careful. */
3516 if (REG_P (target)
3517 && TYPE_MODE (rettype) != BLKmode
3518 && GET_MODE (target) != TYPE_MODE (rettype))
3520 tree type = rettype;
3521 int unsignedp = TYPE_UNSIGNED (type);
3522 int offset = 0;
3523 machine_mode pmode;
3525 /* Ensure we promote as expected, and get the new unsignedness. */
3526 pmode = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
3527 funtype, 1);
3528 gcc_assert (GET_MODE (target) == pmode);
3530 if ((WORDS_BIG_ENDIAN || BYTES_BIG_ENDIAN)
3531 && (GET_MODE_SIZE (GET_MODE (target))
3532 > GET_MODE_SIZE (TYPE_MODE (type))))
3534 offset = GET_MODE_SIZE (GET_MODE (target))
3535 - GET_MODE_SIZE (TYPE_MODE (type));
3536 if (! BYTES_BIG_ENDIAN)
3537 offset = (offset / UNITS_PER_WORD) * UNITS_PER_WORD;
3538 else if (! WORDS_BIG_ENDIAN)
3539 offset %= UNITS_PER_WORD;
3542 target = gen_rtx_SUBREG (TYPE_MODE (type), target, offset);
3543 SUBREG_PROMOTED_VAR_P (target) = 1;
3544 SUBREG_PROMOTED_SET (target, unsignedp);
3547 /* If size of args is variable or this was a constructor call for a stack
3548 argument, restore saved stack-pointer value. */
3550 if (old_stack_level)
3552 rtx_insn *prev = get_last_insn ();
3554 emit_stack_restore (SAVE_BLOCK, old_stack_level);
3555 stack_pointer_delta = old_stack_pointer_delta;
3557 fixup_args_size_notes (prev, get_last_insn (), stack_pointer_delta);
3559 pending_stack_adjust = old_pending_adj;
3560 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
3561 stack_arg_under_construction = old_stack_arg_under_construction;
3562 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3563 stack_usage_map = initial_stack_usage_map;
3564 sibcall_failure = 1;
3566 else if (ACCUMULATE_OUTGOING_ARGS && pass)
3568 #ifdef REG_PARM_STACK_SPACE
3569 if (save_area)
3570 restore_fixed_argument_area (save_area, argblock,
3571 high_to_save, low_to_save);
3572 #endif
3574 /* If we saved any argument areas, restore them. */
3575 for (i = 0; i < num_actuals; i++)
3576 if (args[i].save_area)
3578 machine_mode save_mode = GET_MODE (args[i].save_area);
3579 rtx stack_area
3580 = gen_rtx_MEM (save_mode,
3581 memory_address (save_mode,
3582 XEXP (args[i].stack_slot, 0)));
3584 if (save_mode != BLKmode)
3585 emit_move_insn (stack_area, args[i].save_area);
3586 else
3587 emit_block_move (stack_area, args[i].save_area,
3588 GEN_INT (args[i].locate.size.constant),
3589 BLOCK_OP_CALL_PARM);
3592 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3593 stack_usage_map = initial_stack_usage_map;
3596 /* If this was alloca, record the new stack level for nonlocal gotos.
3597 Check for the handler slots since we might not have a save area
3598 for non-local gotos. */
3600 if ((flags & ECF_MAY_BE_ALLOCA) && cfun->nonlocal_goto_save_area != 0)
3601 update_nonlocal_goto_save_area ();
3603 /* Free up storage we no longer need. */
3604 for (i = 0; i < num_actuals; ++i)
3605 free (args[i].aligned_regs);
3607 targetm.calls.end_call_args ();
3609 insns = get_insns ();
3610 end_sequence ();
3612 if (pass == 0)
3614 tail_call_insns = insns;
3616 /* Restore the pending stack adjustment now that we have
3617 finished generating the sibling call sequence. */
3619 restore_pending_stack_adjust (&save);
3621 /* Prepare arg structure for next iteration. */
3622 for (i = 0; i < num_actuals; i++)
3624 args[i].value = 0;
3625 args[i].aligned_regs = 0;
3626 args[i].stack = 0;
3629 sbitmap_free (stored_args_map);
3630 internal_arg_pointer_exp_state.scan_start = NULL;
3631 internal_arg_pointer_exp_state.cache.release ();
3633 else
3635 normal_call_insns = insns;
3637 /* Verify that we've deallocated all the stack we used. */
3638 gcc_assert ((flags & ECF_NORETURN)
3639 || (old_stack_allocated
3640 == stack_pointer_delta - pending_stack_adjust));
3643 /* If something prevents making this a sibling call,
3644 zero out the sequence. */
3645 if (sibcall_failure)
3646 tail_call_insns = NULL;
3647 else
3648 break;
3651 /* If tail call production succeeded, we need to remove REG_EQUIV notes on
3652 arguments too, as argument area is now clobbered by the call. */
3653 if (tail_call_insns)
3655 emit_insn (tail_call_insns);
3656 crtl->tail_call_emit = true;
3658 else
3659 emit_insn (normal_call_insns);
3661 currently_expanding_call--;
3663 free (stack_usage_map_buf);
3665 /* Join result with returned bounds so caller may use them if needed. */
3666 target = chkp_join_splitted_slot (target, valbnd);
3668 return target;
3671 /* A sibling call sequence invalidates any REG_EQUIV notes made for
3672 this function's incoming arguments.
3674 At the start of RTL generation we know the only REG_EQUIV notes
3675 in the rtl chain are those for incoming arguments, so we can look
3676 for REG_EQUIV notes between the start of the function and the
3677 NOTE_INSN_FUNCTION_BEG.
3679 This is (slight) overkill. We could keep track of the highest
3680 argument we clobber and be more selective in removing notes, but it
3681 does not seem to be worth the effort. */
3683 void
3684 fixup_tail_calls (void)
3686 rtx_insn *insn;
3688 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3690 rtx note;
3692 /* There are never REG_EQUIV notes for the incoming arguments
3693 after the NOTE_INSN_FUNCTION_BEG note, so stop if we see it. */
3694 if (NOTE_P (insn)
3695 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3696 break;
3698 note = find_reg_note (insn, REG_EQUIV, 0);
3699 if (note)
3700 remove_note (insn, note);
3701 note = find_reg_note (insn, REG_EQUIV, 0);
3702 gcc_assert (!note);
3706 /* Traverse a list of TYPES and expand all complex types into their
3707 components. */
3708 static tree
3709 split_complex_types (tree types)
3711 tree p;
3713 /* Before allocating memory, check for the common case of no complex. */
3714 for (p = types; p; p = TREE_CHAIN (p))
3716 tree type = TREE_VALUE (p);
3717 if (TREE_CODE (type) == COMPLEX_TYPE
3718 && targetm.calls.split_complex_arg (type))
3719 goto found;
3721 return types;
3723 found:
3724 types = copy_list (types);
3726 for (p = types; p; p = TREE_CHAIN (p))
3728 tree complex_type = TREE_VALUE (p);
3730 if (TREE_CODE (complex_type) == COMPLEX_TYPE
3731 && targetm.calls.split_complex_arg (complex_type))
3733 tree next, imag;
3735 /* Rewrite complex type with component type. */
3736 TREE_VALUE (p) = TREE_TYPE (complex_type);
3737 next = TREE_CHAIN (p);
3739 /* Add another component type for the imaginary part. */
3740 imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
3741 TREE_CHAIN (p) = imag;
3742 TREE_CHAIN (imag) = next;
3744 /* Skip the newly created node. */
3745 p = TREE_CHAIN (p);
3749 return types;
3752 /* Output a library call to function FUN (a SYMBOL_REF rtx).
3753 The RETVAL parameter specifies whether return value needs to be saved, other
3754 parameters are documented in the emit_library_call function below. */
3756 static rtx
3757 emit_library_call_value_1 (int retval, rtx orgfun, rtx value,
3758 enum libcall_type fn_type,
3759 machine_mode outmode, int nargs, va_list p)
3761 /* Total size in bytes of all the stack-parms scanned so far. */
3762 struct args_size args_size;
3763 /* Size of arguments before any adjustments (such as rounding). */
3764 struct args_size original_args_size;
3765 int argnum;
3766 rtx fun;
3767 /* Todo, choose the correct decl type of orgfun. Sadly this information
3768 isn't present here, so we default to native calling abi here. */
3769 tree fndecl ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
3770 tree fntype ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
3771 int count;
3772 rtx argblock = 0;
3773 CUMULATIVE_ARGS args_so_far_v;
3774 cumulative_args_t args_so_far;
3775 struct arg
3777 rtx value;
3778 machine_mode mode;
3779 rtx reg;
3780 int partial;
3781 struct locate_and_pad_arg_data locate;
3782 rtx save_area;
3784 struct arg *argvec;
3785 int old_inhibit_defer_pop = inhibit_defer_pop;
3786 rtx call_fusage = 0;
3787 rtx mem_value = 0;
3788 rtx valreg;
3789 int pcc_struct_value = 0;
3790 int struct_value_size = 0;
3791 int flags;
3792 int reg_parm_stack_space = 0;
3793 int needed;
3794 rtx_insn *before_call;
3795 tree tfom; /* type_for_mode (outmode, 0) */
3797 #ifdef REG_PARM_STACK_SPACE
3798 /* Define the boundary of the register parm stack space that needs to be
3799 save, if any. */
3800 int low_to_save = 0, high_to_save = 0;
3801 rtx save_area = 0; /* Place that it is saved. */
3802 #endif
3804 /* Size of the stack reserved for parameter registers. */
3805 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
3806 char *initial_stack_usage_map = stack_usage_map;
3807 char *stack_usage_map_buf = NULL;
3809 rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
3811 #ifdef REG_PARM_STACK_SPACE
3812 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
3813 #endif
3815 /* By default, library functions can not throw. */
3816 flags = ECF_NOTHROW;
3818 switch (fn_type)
3820 case LCT_NORMAL:
3821 break;
3822 case LCT_CONST:
3823 flags |= ECF_CONST;
3824 break;
3825 case LCT_PURE:
3826 flags |= ECF_PURE;
3827 break;
3828 case LCT_NORETURN:
3829 flags |= ECF_NORETURN;
3830 break;
3831 case LCT_THROW:
3832 flags = ECF_NORETURN;
3833 break;
3834 case LCT_RETURNS_TWICE:
3835 flags = ECF_RETURNS_TWICE;
3836 break;
3838 fun = orgfun;
3840 /* Ensure current function's preferred stack boundary is at least
3841 what we need. */
3842 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
3843 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3845 /* If this kind of value comes back in memory,
3846 decide where in memory it should come back. */
3847 if (outmode != VOIDmode)
3849 tfom = lang_hooks.types.type_for_mode (outmode, 0);
3850 if (aggregate_value_p (tfom, 0))
3852 #ifdef PCC_STATIC_STRUCT_RETURN
3853 rtx pointer_reg
3854 = hard_function_value (build_pointer_type (tfom), 0, 0, 0);
3855 mem_value = gen_rtx_MEM (outmode, pointer_reg);
3856 pcc_struct_value = 1;
3857 if (value == 0)
3858 value = gen_reg_rtx (outmode);
3859 #else /* not PCC_STATIC_STRUCT_RETURN */
3860 struct_value_size = GET_MODE_SIZE (outmode);
3861 if (value != 0 && MEM_P (value))
3862 mem_value = value;
3863 else
3864 mem_value = assign_temp (tfom, 1, 1);
3865 #endif
3866 /* This call returns a big structure. */
3867 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
3870 else
3871 tfom = void_type_node;
3873 /* ??? Unfinished: must pass the memory address as an argument. */
3875 /* Copy all the libcall-arguments out of the varargs data
3876 and into a vector ARGVEC.
3878 Compute how to pass each argument. We only support a very small subset
3879 of the full argument passing conventions to limit complexity here since
3880 library functions shouldn't have many args. */
3882 argvec = XALLOCAVEC (struct arg, nargs + 1);
3883 memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
3885 #ifdef INIT_CUMULATIVE_LIBCALL_ARGS
3886 INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far_v, outmode, fun);
3887 #else
3888 INIT_CUMULATIVE_ARGS (args_so_far_v, NULL_TREE, fun, 0, nargs);
3889 #endif
3890 args_so_far = pack_cumulative_args (&args_so_far_v);
3892 args_size.constant = 0;
3893 args_size.var = 0;
3895 count = 0;
3897 push_temp_slots ();
3899 /* If there's a structure value address to be passed,
3900 either pass it in the special place, or pass it as an extra argument. */
3901 if (mem_value && struct_value == 0 && ! pcc_struct_value)
3903 rtx addr = XEXP (mem_value, 0);
3905 nargs++;
3907 /* Make sure it is a reasonable operand for a move or push insn. */
3908 if (!REG_P (addr) && !MEM_P (addr)
3909 && !(CONSTANT_P (addr)
3910 && targetm.legitimate_constant_p (Pmode, addr)))
3911 addr = force_operand (addr, NULL_RTX);
3913 argvec[count].value = addr;
3914 argvec[count].mode = Pmode;
3915 argvec[count].partial = 0;
3917 argvec[count].reg = targetm.calls.function_arg (args_so_far,
3918 Pmode, NULL_TREE, true);
3919 gcc_assert (targetm.calls.arg_partial_bytes (args_so_far, Pmode,
3920 NULL_TREE, 1) == 0);
3922 locate_and_pad_parm (Pmode, NULL_TREE,
3923 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3925 #else
3926 argvec[count].reg != 0,
3927 #endif
3928 reg_parm_stack_space, 0,
3929 NULL_TREE, &args_size, &argvec[count].locate);
3931 if (argvec[count].reg == 0 || argvec[count].partial != 0
3932 || reg_parm_stack_space > 0)
3933 args_size.constant += argvec[count].locate.size.constant;
3935 targetm.calls.function_arg_advance (args_so_far, Pmode, (tree) 0, true);
3937 count++;
3940 for (; count < nargs; count++)
3942 rtx val = va_arg (p, rtx);
3943 machine_mode mode = (machine_mode) va_arg (p, int);
3944 int unsigned_p = 0;
3946 /* We cannot convert the arg value to the mode the library wants here;
3947 must do it earlier where we know the signedness of the arg. */
3948 gcc_assert (mode != BLKmode
3949 && (GET_MODE (val) == mode || GET_MODE (val) == VOIDmode));
3951 /* Make sure it is a reasonable operand for a move or push insn. */
3952 if (!REG_P (val) && !MEM_P (val)
3953 && !(CONSTANT_P (val) && targetm.legitimate_constant_p (mode, val)))
3954 val = force_operand (val, NULL_RTX);
3956 if (pass_by_reference (&args_so_far_v, mode, NULL_TREE, 1))
3958 rtx slot;
3959 int must_copy
3960 = !reference_callee_copied (&args_so_far_v, mode, NULL_TREE, 1);
3962 /* If this was a CONST function, it is now PURE since it now
3963 reads memory. */
3964 if (flags & ECF_CONST)
3966 flags &= ~ECF_CONST;
3967 flags |= ECF_PURE;
3970 if (MEM_P (val) && !must_copy)
3972 tree val_expr = MEM_EXPR (val);
3973 if (val_expr)
3974 mark_addressable (val_expr);
3975 slot = val;
3977 else
3979 slot = assign_temp (lang_hooks.types.type_for_mode (mode, 0),
3980 1, 1);
3981 emit_move_insn (slot, val);
3984 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
3985 gen_rtx_USE (VOIDmode, slot),
3986 call_fusage);
3987 if (must_copy)
3988 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
3989 gen_rtx_CLOBBER (VOIDmode,
3990 slot),
3991 call_fusage);
3993 mode = Pmode;
3994 val = force_operand (XEXP (slot, 0), NULL_RTX);
3997 mode = promote_function_mode (NULL_TREE, mode, &unsigned_p, NULL_TREE, 0);
3998 argvec[count].mode = mode;
3999 argvec[count].value = convert_modes (mode, GET_MODE (val), val, unsigned_p);
4000 argvec[count].reg = targetm.calls.function_arg (args_so_far, mode,
4001 NULL_TREE, true);
4003 argvec[count].partial
4004 = targetm.calls.arg_partial_bytes (args_so_far, mode, NULL_TREE, 1);
4006 if (argvec[count].reg == 0
4007 || argvec[count].partial != 0
4008 || reg_parm_stack_space > 0)
4010 locate_and_pad_parm (mode, NULL_TREE,
4011 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4013 #else
4014 argvec[count].reg != 0,
4015 #endif
4016 reg_parm_stack_space, argvec[count].partial,
4017 NULL_TREE, &args_size, &argvec[count].locate);
4018 args_size.constant += argvec[count].locate.size.constant;
4019 gcc_assert (!argvec[count].locate.size.var);
4021 #ifdef BLOCK_REG_PADDING
4022 else
4023 /* The argument is passed entirely in registers. See at which
4024 end it should be padded. */
4025 argvec[count].locate.where_pad =
4026 BLOCK_REG_PADDING (mode, NULL_TREE,
4027 GET_MODE_SIZE (mode) <= UNITS_PER_WORD);
4028 #endif
4030 targetm.calls.function_arg_advance (args_so_far, mode, (tree) 0, true);
4033 /* If this machine requires an external definition for library
4034 functions, write one out. */
4035 assemble_external_libcall (fun);
4037 original_args_size = args_size;
4038 args_size.constant = (((args_size.constant
4039 + stack_pointer_delta
4040 + STACK_BYTES - 1)
4041 / STACK_BYTES
4042 * STACK_BYTES)
4043 - stack_pointer_delta);
4045 args_size.constant = MAX (args_size.constant,
4046 reg_parm_stack_space);
4048 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4049 args_size.constant -= reg_parm_stack_space;
4051 if (args_size.constant > crtl->outgoing_args_size)
4052 crtl->outgoing_args_size = args_size.constant;
4054 if (flag_stack_usage_info && !ACCUMULATE_OUTGOING_ARGS)
4056 int pushed = args_size.constant + pending_stack_adjust;
4057 if (pushed > current_function_pushed_stack_size)
4058 current_function_pushed_stack_size = pushed;
4061 if (ACCUMULATE_OUTGOING_ARGS)
4063 /* Since the stack pointer will never be pushed, it is possible for
4064 the evaluation of a parm to clobber something we have already
4065 written to the stack. Since most function calls on RISC machines
4066 do not use the stack, this is uncommon, but must work correctly.
4068 Therefore, we save any area of the stack that was already written
4069 and that we are using. Here we set up to do this by making a new
4070 stack usage map from the old one.
4072 Another approach might be to try to reorder the argument
4073 evaluations to avoid this conflicting stack usage. */
4075 needed = args_size.constant;
4077 /* Since we will be writing into the entire argument area, the
4078 map must be allocated for its entire size, not just the part that
4079 is the responsibility of the caller. */
4080 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4081 needed += reg_parm_stack_space;
4083 #ifdef ARGS_GROW_DOWNWARD
4084 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
4085 needed + 1);
4086 #else
4087 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
4088 needed);
4089 #endif
4090 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
4091 stack_usage_map = stack_usage_map_buf;
4093 if (initial_highest_arg_in_use)
4094 memcpy (stack_usage_map, initial_stack_usage_map,
4095 initial_highest_arg_in_use);
4097 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
4098 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
4099 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
4100 needed = 0;
4102 /* We must be careful to use virtual regs before they're instantiated,
4103 and real regs afterwards. Loop optimization, for example, can create
4104 new libcalls after we've instantiated the virtual regs, and if we
4105 use virtuals anyway, they won't match the rtl patterns. */
4107 if (virtuals_instantiated)
4108 argblock = plus_constant (Pmode, stack_pointer_rtx,
4109 STACK_POINTER_OFFSET);
4110 else
4111 argblock = virtual_outgoing_args_rtx;
4113 else
4115 if (!PUSH_ARGS)
4116 argblock = push_block (GEN_INT (args_size.constant), 0, 0);
4119 /* We push args individually in reverse order, perform stack alignment
4120 before the first push (the last arg). */
4121 if (argblock == 0)
4122 anti_adjust_stack (GEN_INT (args_size.constant
4123 - original_args_size.constant));
4125 argnum = nargs - 1;
4127 #ifdef REG_PARM_STACK_SPACE
4128 if (ACCUMULATE_OUTGOING_ARGS)
4130 /* The argument list is the property of the called routine and it
4131 may clobber it. If the fixed area has been used for previous
4132 parameters, we must save and restore it. */
4133 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
4134 &low_to_save, &high_to_save);
4136 #endif
4138 /* When expanding a normal call, args are stored in push order,
4139 which is the reverse of what we have here. */
4140 bool any_regs = false;
4141 for (int i = nargs; i-- > 0; )
4142 if (argvec[i].reg != NULL_RTX)
4144 targetm.calls.call_args (argvec[i].reg, NULL_TREE);
4145 any_regs = true;
4147 if (!any_regs)
4148 targetm.calls.call_args (pc_rtx, NULL_TREE);
4150 /* Push the args that need to be pushed. */
4152 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4153 are to be pushed. */
4154 for (count = 0; count < nargs; count++, argnum--)
4156 machine_mode mode = argvec[argnum].mode;
4157 rtx val = argvec[argnum].value;
4158 rtx reg = argvec[argnum].reg;
4159 int partial = argvec[argnum].partial;
4160 unsigned int parm_align = argvec[argnum].locate.boundary;
4161 int lower_bound = 0, upper_bound = 0, i;
4163 if (! (reg != 0 && partial == 0))
4165 rtx use;
4167 if (ACCUMULATE_OUTGOING_ARGS)
4169 /* If this is being stored into a pre-allocated, fixed-size,
4170 stack area, save any previous data at that location. */
4172 #ifdef ARGS_GROW_DOWNWARD
4173 /* stack_slot is negative, but we want to index stack_usage_map
4174 with positive values. */
4175 upper_bound = -argvec[argnum].locate.slot_offset.constant + 1;
4176 lower_bound = upper_bound - argvec[argnum].locate.size.constant;
4177 #else
4178 lower_bound = argvec[argnum].locate.slot_offset.constant;
4179 upper_bound = lower_bound + argvec[argnum].locate.size.constant;
4180 #endif
4182 i = lower_bound;
4183 /* Don't worry about things in the fixed argument area;
4184 it has already been saved. */
4185 if (i < reg_parm_stack_space)
4186 i = reg_parm_stack_space;
4187 while (i < upper_bound && stack_usage_map[i] == 0)
4188 i++;
4190 if (i < upper_bound)
4192 /* We need to make a save area. */
4193 unsigned int size
4194 = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
4195 machine_mode save_mode
4196 = mode_for_size (size, MODE_INT, 1);
4197 rtx adr
4198 = plus_constant (Pmode, argblock,
4199 argvec[argnum].locate.offset.constant);
4200 rtx stack_area
4201 = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
4203 if (save_mode == BLKmode)
4205 argvec[argnum].save_area
4206 = assign_stack_temp (BLKmode,
4207 argvec[argnum].locate.size.constant
4210 emit_block_move (validize_mem
4211 (copy_rtx (argvec[argnum].save_area)),
4212 stack_area,
4213 GEN_INT (argvec[argnum].locate.size.constant),
4214 BLOCK_OP_CALL_PARM);
4216 else
4218 argvec[argnum].save_area = gen_reg_rtx (save_mode);
4220 emit_move_insn (argvec[argnum].save_area, stack_area);
4225 emit_push_insn (val, mode, NULL_TREE, NULL_RTX, parm_align,
4226 partial, reg, 0, argblock,
4227 GEN_INT (argvec[argnum].locate.offset.constant),
4228 reg_parm_stack_space,
4229 ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad));
4231 /* Now mark the segment we just used. */
4232 if (ACCUMULATE_OUTGOING_ARGS)
4233 for (i = lower_bound; i < upper_bound; i++)
4234 stack_usage_map[i] = 1;
4236 NO_DEFER_POP;
4238 /* Indicate argument access so that alias.c knows that these
4239 values are live. */
4240 if (argblock)
4241 use = plus_constant (Pmode, argblock,
4242 argvec[argnum].locate.offset.constant);
4243 else
4244 /* When arguments are pushed, trying to tell alias.c where
4245 exactly this argument is won't work, because the
4246 auto-increment causes confusion. So we merely indicate
4247 that we access something with a known mode somewhere on
4248 the stack. */
4249 use = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4250 gen_rtx_SCRATCH (Pmode));
4251 use = gen_rtx_MEM (argvec[argnum].mode, use);
4252 use = gen_rtx_USE (VOIDmode, use);
4253 call_fusage = gen_rtx_EXPR_LIST (VOIDmode, use, call_fusage);
4257 argnum = nargs - 1;
4259 fun = prepare_call_address (NULL, fun, NULL, &call_fusage, 0, 0);
4261 /* Now load any reg parms into their regs. */
4263 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4264 are to be pushed. */
4265 for (count = 0; count < nargs; count++, argnum--)
4267 machine_mode mode = argvec[argnum].mode;
4268 rtx val = argvec[argnum].value;
4269 rtx reg = argvec[argnum].reg;
4270 int partial = argvec[argnum].partial;
4271 #ifdef BLOCK_REG_PADDING
4272 int size = 0;
4273 #endif
4275 /* Handle calls that pass values in multiple non-contiguous
4276 locations. The PA64 has examples of this for library calls. */
4277 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4278 emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (mode));
4279 else if (reg != 0 && partial == 0)
4281 emit_move_insn (reg, val);
4282 #ifdef BLOCK_REG_PADDING
4283 size = GET_MODE_SIZE (argvec[argnum].mode);
4285 /* Copied from load_register_parameters. */
4287 /* Handle case where we have a value that needs shifting
4288 up to the msb. eg. a QImode value and we're padding
4289 upward on a BYTES_BIG_ENDIAN machine. */
4290 if (size < UNITS_PER_WORD
4291 && (argvec[argnum].locate.where_pad
4292 == (BYTES_BIG_ENDIAN ? upward : downward)))
4294 rtx x;
4295 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
4297 /* Assigning REG here rather than a temp makes CALL_FUSAGE
4298 report the whole reg as used. Strictly speaking, the
4299 call only uses SIZE bytes at the msb end, but it doesn't
4300 seem worth generating rtl to say that. */
4301 reg = gen_rtx_REG (word_mode, REGNO (reg));
4302 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
4303 if (x != reg)
4304 emit_move_insn (reg, x);
4306 #endif
4309 NO_DEFER_POP;
4312 /* Any regs containing parms remain in use through the call. */
4313 for (count = 0; count < nargs; count++)
4315 rtx reg = argvec[count].reg;
4316 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4317 use_group_regs (&call_fusage, reg);
4318 else if (reg != 0)
4320 int partial = argvec[count].partial;
4321 if (partial)
4323 int nregs;
4324 gcc_assert (partial % UNITS_PER_WORD == 0);
4325 nregs = partial / UNITS_PER_WORD;
4326 use_regs (&call_fusage, REGNO (reg), nregs);
4328 else
4329 use_reg (&call_fusage, reg);
4333 /* Pass the function the address in which to return a structure value. */
4334 if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
4336 emit_move_insn (struct_value,
4337 force_reg (Pmode,
4338 force_operand (XEXP (mem_value, 0),
4339 NULL_RTX)));
4340 if (REG_P (struct_value))
4341 use_reg (&call_fusage, struct_value);
4344 /* Don't allow popping to be deferred, since then
4345 cse'ing of library calls could delete a call and leave the pop. */
4346 NO_DEFER_POP;
4347 valreg = (mem_value == 0 && outmode != VOIDmode
4348 ? hard_libcall_value (outmode, orgfun) : NULL_RTX);
4350 /* Stack must be properly aligned now. */
4351 gcc_assert (!(stack_pointer_delta
4352 & (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1)));
4354 before_call = get_last_insn ();
4356 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
4357 will set inhibit_defer_pop to that value. */
4358 /* The return type is needed to decide how many bytes the function pops.
4359 Signedness plays no role in that, so for simplicity, we pretend it's
4360 always signed. We also assume that the list of arguments passed has
4361 no impact, so we pretend it is unknown. */
4363 emit_call_1 (fun, NULL,
4364 get_identifier (XSTR (orgfun, 0)),
4365 build_function_type (tfom, NULL_TREE),
4366 original_args_size.constant, args_size.constant,
4367 struct_value_size,
4368 targetm.calls.function_arg (args_so_far,
4369 VOIDmode, void_type_node, true),
4370 valreg,
4371 old_inhibit_defer_pop + 1, call_fusage, flags, args_so_far);
4373 if (flag_use_caller_save)
4375 rtx last, datum = orgfun;
4376 gcc_assert (GET_CODE (datum) == SYMBOL_REF);
4377 last = last_call_insn ();
4378 add_reg_note (last, REG_CALL_DECL, datum);
4381 /* Right-shift returned value if necessary. */
4382 if (!pcc_struct_value
4383 && TYPE_MODE (tfom) != BLKmode
4384 && targetm.calls.return_in_msb (tfom))
4386 shift_return_value (TYPE_MODE (tfom), false, valreg);
4387 valreg = gen_rtx_REG (TYPE_MODE (tfom), REGNO (valreg));
4390 targetm.calls.end_call_args ();
4392 /* For calls to `setjmp', etc., inform function.c:setjmp_warnings
4393 that it should complain if nonvolatile values are live. For
4394 functions that cannot return, inform flow that control does not
4395 fall through. */
4396 if (flags & ECF_NORETURN)
4398 /* The barrier note must be emitted
4399 immediately after the CALL_INSN. Some ports emit more than
4400 just a CALL_INSN above, so we must search for it here. */
4401 rtx_insn *last = get_last_insn ();
4402 while (!CALL_P (last))
4404 last = PREV_INSN (last);
4405 /* There was no CALL_INSN? */
4406 gcc_assert (last != before_call);
4409 emit_barrier_after (last);
4412 /* Consider that "regular" libcalls, i.e. all of them except for LCT_THROW
4413 and LCT_RETURNS_TWICE, cannot perform non-local gotos. */
4414 if (flags & ECF_NOTHROW)
4416 rtx_insn *last = get_last_insn ();
4417 while (!CALL_P (last))
4419 last = PREV_INSN (last);
4420 /* There was no CALL_INSN? */
4421 gcc_assert (last != before_call);
4424 make_reg_eh_region_note_nothrow_nononlocal (last);
4427 /* Now restore inhibit_defer_pop to its actual original value. */
4428 OK_DEFER_POP;
4430 pop_temp_slots ();
4432 /* Copy the value to the right place. */
4433 if (outmode != VOIDmode && retval)
4435 if (mem_value)
4437 if (value == 0)
4438 value = mem_value;
4439 if (value != mem_value)
4440 emit_move_insn (value, mem_value);
4442 else if (GET_CODE (valreg) == PARALLEL)
4444 if (value == 0)
4445 value = gen_reg_rtx (outmode);
4446 emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
4448 else
4450 /* Convert to the proper mode if a promotion has been active. */
4451 if (GET_MODE (valreg) != outmode)
4453 int unsignedp = TYPE_UNSIGNED (tfom);
4455 gcc_assert (promote_function_mode (tfom, outmode, &unsignedp,
4456 fndecl ? TREE_TYPE (fndecl) : fntype, 1)
4457 == GET_MODE (valreg));
4458 valreg = convert_modes (outmode, GET_MODE (valreg), valreg, 0);
4461 if (value != 0)
4462 emit_move_insn (value, valreg);
4463 else
4464 value = valreg;
4468 if (ACCUMULATE_OUTGOING_ARGS)
4470 #ifdef REG_PARM_STACK_SPACE
4471 if (save_area)
4472 restore_fixed_argument_area (save_area, argblock,
4473 high_to_save, low_to_save);
4474 #endif
4476 /* If we saved any argument areas, restore them. */
4477 for (count = 0; count < nargs; count++)
4478 if (argvec[count].save_area)
4480 machine_mode save_mode = GET_MODE (argvec[count].save_area);
4481 rtx adr = plus_constant (Pmode, argblock,
4482 argvec[count].locate.offset.constant);
4483 rtx stack_area = gen_rtx_MEM (save_mode,
4484 memory_address (save_mode, adr));
4486 if (save_mode == BLKmode)
4487 emit_block_move (stack_area,
4488 validize_mem
4489 (copy_rtx (argvec[count].save_area)),
4490 GEN_INT (argvec[count].locate.size.constant),
4491 BLOCK_OP_CALL_PARM);
4492 else
4493 emit_move_insn (stack_area, argvec[count].save_area);
4496 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
4497 stack_usage_map = initial_stack_usage_map;
4500 free (stack_usage_map_buf);
4502 return value;
4506 /* Output a library call to function FUN (a SYMBOL_REF rtx)
4507 (emitting the queue unless NO_QUEUE is nonzero),
4508 for a value of mode OUTMODE,
4509 with NARGS different arguments, passed as alternating rtx values
4510 and machine_modes to convert them to.
4512 FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for
4513 `const' calls, LCT_PURE for `pure' calls, or other LCT_ value for
4514 other types of library calls. */
4516 void
4517 emit_library_call (rtx orgfun, enum libcall_type fn_type,
4518 machine_mode outmode, int nargs, ...)
4520 va_list p;
4522 va_start (p, nargs);
4523 emit_library_call_value_1 (0, orgfun, NULL_RTX, fn_type, outmode, nargs, p);
4524 va_end (p);
4527 /* Like emit_library_call except that an extra argument, VALUE,
4528 comes second and says where to store the result.
4529 (If VALUE is zero, this function chooses a convenient way
4530 to return the value.
4532 This function returns an rtx for where the value is to be found.
4533 If VALUE is nonzero, VALUE is returned. */
4536 emit_library_call_value (rtx orgfun, rtx value,
4537 enum libcall_type fn_type,
4538 machine_mode outmode, int nargs, ...)
4540 rtx result;
4541 va_list p;
4543 va_start (p, nargs);
4544 result = emit_library_call_value_1 (1, orgfun, value, fn_type, outmode,
4545 nargs, p);
4546 va_end (p);
4548 return result;
4552 /* Store pointer bounds argument ARG into Bounds Table entry
4553 associated with PARM. */
4554 static void
4555 store_bounds (struct arg_data *arg, struct arg_data *parm)
4557 rtx slot = NULL, ptr = NULL, addr = NULL;
4559 /* We may pass bounds not associated with any pointer. */
4560 if (!parm)
4562 gcc_assert (arg->special_slot);
4563 slot = arg->special_slot;
4564 ptr = const0_rtx;
4566 /* Find pointer associated with bounds and where it is
4567 passed. */
4568 else
4570 if (!parm->reg)
4572 gcc_assert (!arg->special_slot);
4574 addr = adjust_address (parm->stack, Pmode, arg->pointer_offset);
4576 else if (REG_P (parm->reg))
4578 gcc_assert (arg->special_slot);
4579 slot = arg->special_slot;
4581 if (MEM_P (parm->value))
4582 addr = adjust_address (parm->value, Pmode, arg->pointer_offset);
4583 else if (REG_P (parm->value))
4584 ptr = gen_rtx_SUBREG (Pmode, parm->value, arg->pointer_offset);
4585 else
4587 gcc_assert (!arg->pointer_offset);
4588 ptr = parm->value;
4591 else
4593 gcc_assert (GET_CODE (parm->reg) == PARALLEL);
4595 gcc_assert (arg->special_slot);
4596 slot = arg->special_slot;
4598 if (parm->parallel_value)
4599 ptr = chkp_get_value_with_offs (parm->parallel_value,
4600 GEN_INT (arg->pointer_offset));
4601 else
4602 gcc_unreachable ();
4606 /* Expand bounds. */
4607 if (!arg->value)
4608 arg->value = expand_normal (arg->tree_value);
4610 targetm.calls.store_bounds_for_arg (ptr, addr, arg->value, slot);
4613 /* Store a single argument for a function call
4614 into the register or memory area where it must be passed.
4615 *ARG describes the argument value and where to pass it.
4617 ARGBLOCK is the address of the stack-block for all the arguments,
4618 or 0 on a machine where arguments are pushed individually.
4620 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
4621 so must be careful about how the stack is used.
4623 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
4624 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
4625 that we need not worry about saving and restoring the stack.
4627 FNDECL is the declaration of the function we are calling.
4629 Return nonzero if this arg should cause sibcall failure,
4630 zero otherwise. */
4632 static int
4633 store_one_arg (struct arg_data *arg, rtx argblock, int flags,
4634 int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
4636 tree pval = arg->tree_value;
4637 rtx reg = 0;
4638 int partial = 0;
4639 int used = 0;
4640 int i, lower_bound = 0, upper_bound = 0;
4641 int sibcall_failure = 0;
4643 if (TREE_CODE (pval) == ERROR_MARK)
4644 return 1;
4646 /* Push a new temporary level for any temporaries we make for
4647 this argument. */
4648 push_temp_slots ();
4650 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
4652 /* If this is being stored into a pre-allocated, fixed-size, stack area,
4653 save any previous data at that location. */
4654 if (argblock && ! variable_size && arg->stack)
4656 #ifdef ARGS_GROW_DOWNWARD
4657 /* stack_slot is negative, but we want to index stack_usage_map
4658 with positive values. */
4659 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4660 upper_bound = -INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1)) + 1;
4661 else
4662 upper_bound = 0;
4664 lower_bound = upper_bound - arg->locate.size.constant;
4665 #else
4666 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4667 lower_bound = INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1));
4668 else
4669 lower_bound = 0;
4671 upper_bound = lower_bound + arg->locate.size.constant;
4672 #endif
4674 i = lower_bound;
4675 /* Don't worry about things in the fixed argument area;
4676 it has already been saved. */
4677 if (i < reg_parm_stack_space)
4678 i = reg_parm_stack_space;
4679 while (i < upper_bound && stack_usage_map[i] == 0)
4680 i++;
4682 if (i < upper_bound)
4684 /* We need to make a save area. */
4685 unsigned int size = arg->locate.size.constant * BITS_PER_UNIT;
4686 machine_mode save_mode = mode_for_size (size, MODE_INT, 1);
4687 rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
4688 rtx stack_area = gen_rtx_MEM (save_mode, adr);
4690 if (save_mode == BLKmode)
4692 arg->save_area
4693 = assign_temp (TREE_TYPE (arg->tree_value), 1, 1);
4694 preserve_temp_slots (arg->save_area);
4695 emit_block_move (validize_mem (copy_rtx (arg->save_area)),
4696 stack_area,
4697 GEN_INT (arg->locate.size.constant),
4698 BLOCK_OP_CALL_PARM);
4700 else
4702 arg->save_area = gen_reg_rtx (save_mode);
4703 emit_move_insn (arg->save_area, stack_area);
4709 /* If this isn't going to be placed on both the stack and in registers,
4710 set up the register and number of words. */
4711 if (! arg->pass_on_stack)
4713 if (flags & ECF_SIBCALL)
4714 reg = arg->tail_call_reg;
4715 else
4716 reg = arg->reg;
4717 partial = arg->partial;
4720 /* Being passed entirely in a register. We shouldn't be called in
4721 this case. */
4722 gcc_assert (reg == 0 || partial != 0);
4724 /* If this arg needs special alignment, don't load the registers
4725 here. */
4726 if (arg->n_aligned_regs != 0)
4727 reg = 0;
4729 /* If this is being passed partially in a register, we can't evaluate
4730 it directly into its stack slot. Otherwise, we can. */
4731 if (arg->value == 0)
4733 /* stack_arg_under_construction is nonzero if a function argument is
4734 being evaluated directly into the outgoing argument list and
4735 expand_call must take special action to preserve the argument list
4736 if it is called recursively.
4738 For scalar function arguments stack_usage_map is sufficient to
4739 determine which stack slots must be saved and restored. Scalar
4740 arguments in general have pass_on_stack == 0.
4742 If this argument is initialized by a function which takes the
4743 address of the argument (a C++ constructor or a C function
4744 returning a BLKmode structure), then stack_usage_map is
4745 insufficient and expand_call must push the stack around the
4746 function call. Such arguments have pass_on_stack == 1.
4748 Note that it is always safe to set stack_arg_under_construction,
4749 but this generates suboptimal code if set when not needed. */
4751 if (arg->pass_on_stack)
4752 stack_arg_under_construction++;
4754 arg->value = expand_expr (pval,
4755 (partial
4756 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
4757 ? NULL_RTX : arg->stack,
4758 VOIDmode, EXPAND_STACK_PARM);
4760 /* If we are promoting object (or for any other reason) the mode
4761 doesn't agree, convert the mode. */
4763 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
4764 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
4765 arg->value, arg->unsignedp);
4767 if (arg->pass_on_stack)
4768 stack_arg_under_construction--;
4771 /* Check for overlap with already clobbered argument area. */
4772 if ((flags & ECF_SIBCALL)
4773 && MEM_P (arg->value)
4774 && mem_overlaps_already_clobbered_arg_p (XEXP (arg->value, 0),
4775 arg->locate.size.constant))
4776 sibcall_failure = 1;
4778 /* Don't allow anything left on stack from computation
4779 of argument to alloca. */
4780 if (flags & ECF_MAY_BE_ALLOCA)
4781 do_pending_stack_adjust ();
4783 if (arg->value == arg->stack)
4784 /* If the value is already in the stack slot, we are done. */
4786 else if (arg->mode != BLKmode)
4788 int size;
4789 unsigned int parm_align;
4791 /* Argument is a scalar, not entirely passed in registers.
4792 (If part is passed in registers, arg->partial says how much
4793 and emit_push_insn will take care of putting it there.)
4795 Push it, and if its size is less than the
4796 amount of space allocated to it,
4797 also bump stack pointer by the additional space.
4798 Note that in C the default argument promotions
4799 will prevent such mismatches. */
4801 size = GET_MODE_SIZE (arg->mode);
4802 /* Compute how much space the push instruction will push.
4803 On many machines, pushing a byte will advance the stack
4804 pointer by a halfword. */
4805 #ifdef PUSH_ROUNDING
4806 size = PUSH_ROUNDING (size);
4807 #endif
4808 used = size;
4810 /* Compute how much space the argument should get:
4811 round up to a multiple of the alignment for arguments. */
4812 if (none != FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)))
4813 used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
4814 / (PARM_BOUNDARY / BITS_PER_UNIT))
4815 * (PARM_BOUNDARY / BITS_PER_UNIT));
4817 /* Compute the alignment of the pushed argument. */
4818 parm_align = arg->locate.boundary;
4819 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
4821 int pad = used - size;
4822 if (pad)
4824 unsigned int pad_align = (pad & -pad) * BITS_PER_UNIT;
4825 parm_align = MIN (parm_align, pad_align);
4829 /* This isn't already where we want it on the stack, so put it there.
4830 This can either be done with push or copy insns. */
4831 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), NULL_RTX,
4832 parm_align, partial, reg, used - size, argblock,
4833 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4834 ARGS_SIZE_RTX (arg->locate.alignment_pad));
4836 /* Unless this is a partially-in-register argument, the argument is now
4837 in the stack. */
4838 if (partial == 0)
4839 arg->value = arg->stack;
4841 else
4843 /* BLKmode, at least partly to be pushed. */
4845 unsigned int parm_align;
4846 int excess;
4847 rtx size_rtx;
4849 /* Pushing a nonscalar.
4850 If part is passed in registers, PARTIAL says how much
4851 and emit_push_insn will take care of putting it there. */
4853 /* Round its size up to a multiple
4854 of the allocation unit for arguments. */
4856 if (arg->locate.size.var != 0)
4858 excess = 0;
4859 size_rtx = ARGS_SIZE_RTX (arg->locate.size);
4861 else
4863 /* PUSH_ROUNDING has no effect on us, because emit_push_insn
4864 for BLKmode is careful to avoid it. */
4865 excess = (arg->locate.size.constant
4866 - int_size_in_bytes (TREE_TYPE (pval))
4867 + partial);
4868 size_rtx = expand_expr (size_in_bytes (TREE_TYPE (pval)),
4869 NULL_RTX, TYPE_MODE (sizetype),
4870 EXPAND_NORMAL);
4873 parm_align = arg->locate.boundary;
4875 /* When an argument is padded down, the block is aligned to
4876 PARM_BOUNDARY, but the actual argument isn't. */
4877 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
4879 if (arg->locate.size.var)
4880 parm_align = BITS_PER_UNIT;
4881 else if (excess)
4883 unsigned int excess_align = (excess & -excess) * BITS_PER_UNIT;
4884 parm_align = MIN (parm_align, excess_align);
4888 if ((flags & ECF_SIBCALL) && MEM_P (arg->value))
4890 /* emit_push_insn might not work properly if arg->value and
4891 argblock + arg->locate.offset areas overlap. */
4892 rtx x = arg->value;
4893 int i = 0;
4895 if (XEXP (x, 0) == crtl->args.internal_arg_pointer
4896 || (GET_CODE (XEXP (x, 0)) == PLUS
4897 && XEXP (XEXP (x, 0), 0) ==
4898 crtl->args.internal_arg_pointer
4899 && CONST_INT_P (XEXP (XEXP (x, 0), 1))))
4901 if (XEXP (x, 0) != crtl->args.internal_arg_pointer)
4902 i = INTVAL (XEXP (XEXP (x, 0), 1));
4904 /* expand_call should ensure this. */
4905 gcc_assert (!arg->locate.offset.var
4906 && arg->locate.size.var == 0
4907 && CONST_INT_P (size_rtx));
4909 if (arg->locate.offset.constant > i)
4911 if (arg->locate.offset.constant < i + INTVAL (size_rtx))
4912 sibcall_failure = 1;
4914 else if (arg->locate.offset.constant < i)
4916 /* Use arg->locate.size.constant instead of size_rtx
4917 because we only care about the part of the argument
4918 on the stack. */
4919 if (i < (arg->locate.offset.constant
4920 + arg->locate.size.constant))
4921 sibcall_failure = 1;
4923 else
4925 /* Even though they appear to be at the same location,
4926 if part of the outgoing argument is in registers,
4927 they aren't really at the same location. Check for
4928 this by making sure that the incoming size is the
4929 same as the outgoing size. */
4930 if (arg->locate.size.constant != INTVAL (size_rtx))
4931 sibcall_failure = 1;
4936 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
4937 parm_align, partial, reg, excess, argblock,
4938 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4939 ARGS_SIZE_RTX (arg->locate.alignment_pad));
4941 /* Unless this is a partially-in-register argument, the argument is now
4942 in the stack.
4944 ??? Unlike the case above, in which we want the actual
4945 address of the data, so that we can load it directly into a
4946 register, here we want the address of the stack slot, so that
4947 it's properly aligned for word-by-word copying or something
4948 like that. It's not clear that this is always correct. */
4949 if (partial == 0)
4950 arg->value = arg->stack_slot;
4953 if (arg->reg && GET_CODE (arg->reg) == PARALLEL)
4955 tree type = TREE_TYPE (arg->tree_value);
4956 arg->parallel_value
4957 = emit_group_load_into_temps (arg->reg, arg->value, type,
4958 int_size_in_bytes (type));
4961 /* Mark all slots this store used. */
4962 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
4963 && argblock && ! variable_size && arg->stack)
4964 for (i = lower_bound; i < upper_bound; i++)
4965 stack_usage_map[i] = 1;
4967 /* Once we have pushed something, pops can't safely
4968 be deferred during the rest of the arguments. */
4969 NO_DEFER_POP;
4971 /* Free any temporary slots made in processing this argument. */
4972 pop_temp_slots ();
4974 return sibcall_failure;
4977 /* Nonzero if we do not know how to pass TYPE solely in registers. */
4979 bool
4980 must_pass_in_stack_var_size (machine_mode mode ATTRIBUTE_UNUSED,
4981 const_tree type)
4983 if (!type)
4984 return false;
4986 /* If the type has variable size... */
4987 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4988 return true;
4990 /* If the type is marked as addressable (it is required
4991 to be constructed into the stack)... */
4992 if (TREE_ADDRESSABLE (type))
4993 return true;
4995 return false;
4998 /* Another version of the TARGET_MUST_PASS_IN_STACK hook. This one
4999 takes trailing padding of a structure into account. */
5000 /* ??? Should be able to merge these two by examining BLOCK_REG_PADDING. */
5002 bool
5003 must_pass_in_stack_var_size_or_pad (machine_mode mode, const_tree type)
5005 if (!type)
5006 return false;
5008 /* If the type has variable size... */
5009 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5010 return true;
5012 /* If the type is marked as addressable (it is required
5013 to be constructed into the stack)... */
5014 if (TREE_ADDRESSABLE (type))
5015 return true;
5017 /* If the padding and mode of the type is such that a copy into
5018 a register would put it into the wrong part of the register. */
5019 if (mode == BLKmode
5020 && int_size_in_bytes (type) % (PARM_BOUNDARY / BITS_PER_UNIT)
5021 && (FUNCTION_ARG_PADDING (mode, type)
5022 == (BYTES_BIG_ENDIAN ? upward : downward)))
5023 return true;
5025 return false;