[obvious] Fix typos above expand_cond_expr_using_cmove
[official-gcc.git] / gcc / calls.c
bloba79825a44e8a3ba7ff246badc2dfde7c5ef51005
1 /* Convert function calls to rtl insns, for GNU C compiler.
2 Copyright (C) 1989-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "tree.h"
25 #include "gimple.h"
26 #include "rtl.h"
27 #include "alias.h"
28 #include "fold-const.h"
29 #include "stor-layout.h"
30 #include "varasm.h"
31 #include "stringpool.h"
32 #include "attribs.h"
33 #include "internal-fn.h"
34 #include "flags.h"
35 #include "insn-config.h"
36 #include "expmed.h"
37 #include "dojump.h"
38 #include "explow.h"
39 #include "calls.h"
40 #include "emit-rtl.h"
41 #include "stmt.h"
42 #include "expr.h"
43 #include "insn-codes.h"
44 #include "optabs.h"
45 #include "libfuncs.h"
46 #include "regs.h"
47 #include "diagnostic-core.h"
48 #include "output.h"
49 #include "tm_p.h"
50 #include "timevar.h"
51 #include "langhooks.h"
52 #include "target.h"
53 #include "cgraph.h"
54 #include "except.h"
55 #include "dbgcnt.h"
56 #include "rtl-iter.h"
57 #include "tree-chkp.h"
58 #include "rtl-chkp.h"
61 /* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits. */
62 #define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
64 /* Data structure and subroutines used within expand_call. */
66 struct arg_data
68 /* Tree node for this argument. */
69 tree tree_value;
70 /* Mode for value; TYPE_MODE unless promoted. */
71 machine_mode mode;
72 /* Current RTL value for argument, or 0 if it isn't precomputed. */
73 rtx value;
74 /* Initially-compute RTL value for argument; only for const functions. */
75 rtx initial_value;
76 /* Register to pass this argument in, 0 if passed on stack, or an
77 PARALLEL if the arg is to be copied into multiple non-contiguous
78 registers. */
79 rtx reg;
80 /* Register to pass this argument in when generating tail call sequence.
81 This is not the same register as for normal calls on machines with
82 register windows. */
83 rtx tail_call_reg;
84 /* If REG is a PARALLEL, this is a copy of VALUE pulled into the correct
85 form for emit_group_move. */
86 rtx parallel_value;
87 /* If value is passed in neither reg nor stack, this field holds a number
88 of a special slot to be used. */
89 rtx special_slot;
90 /* For pointer bounds hold an index of parm bounds are bound to. -1 if
91 there is no such pointer. */
92 int pointer_arg;
93 /* If pointer_arg refers a structure, then pointer_offset holds an offset
94 of a pointer in this structure. */
95 int pointer_offset;
96 /* If REG was promoted from the actual mode of the argument expression,
97 indicates whether the promotion is sign- or zero-extended. */
98 int unsignedp;
99 /* Number of bytes to put in registers. 0 means put the whole arg
100 in registers. Also 0 if not passed in registers. */
101 int partial;
102 /* Nonzero if argument must be passed on stack.
103 Note that some arguments may be passed on the stack
104 even though pass_on_stack is zero, just because FUNCTION_ARG says so.
105 pass_on_stack identifies arguments that *cannot* go in registers. */
106 int pass_on_stack;
107 /* Some fields packaged up for locate_and_pad_parm. */
108 struct locate_and_pad_arg_data locate;
109 /* Location on the stack at which parameter should be stored. The store
110 has already been done if STACK == VALUE. */
111 rtx stack;
112 /* Location on the stack of the start of this argument slot. This can
113 differ from STACK if this arg pads downward. This location is known
114 to be aligned to TARGET_FUNCTION_ARG_BOUNDARY. */
115 rtx stack_slot;
116 /* Place that this stack area has been saved, if needed. */
117 rtx save_area;
118 /* If an argument's alignment does not permit direct copying into registers,
119 copy in smaller-sized pieces into pseudos. These are stored in a
120 block pointed to by this field. The next field says how many
121 word-sized pseudos we made. */
122 rtx *aligned_regs;
123 int n_aligned_regs;
126 /* A vector of one char per byte of stack space. A byte if nonzero if
127 the corresponding stack location has been used.
128 This vector is used to prevent a function call within an argument from
129 clobbering any stack already set up. */
130 static char *stack_usage_map;
132 /* Size of STACK_USAGE_MAP. */
133 static int highest_outgoing_arg_in_use;
135 /* A bitmap of virtual-incoming stack space. Bit is set if the corresponding
136 stack location's tail call argument has been already stored into the stack.
137 This bitmap is used to prevent sibling call optimization if function tries
138 to use parent's incoming argument slots when they have been already
139 overwritten with tail call arguments. */
140 static sbitmap stored_args_map;
142 /* stack_arg_under_construction is nonzero when an argument may be
143 initialized with a constructor call (including a C function that
144 returns a BLKmode struct) and expand_call must take special action
145 to make sure the object being constructed does not overlap the
146 argument list for the constructor call. */
147 static int stack_arg_under_construction;
149 static void emit_call_1 (rtx, tree, tree, tree, HOST_WIDE_INT, HOST_WIDE_INT,
150 HOST_WIDE_INT, rtx, rtx, int, rtx, int,
151 cumulative_args_t);
152 static void precompute_register_parameters (int, struct arg_data *, int *);
153 static void store_bounds (struct arg_data *, struct arg_data *);
154 static int store_one_arg (struct arg_data *, rtx, int, int, int);
155 static void store_unaligned_arguments_into_pseudos (struct arg_data *, int);
156 static int finalize_must_preallocate (int, int, struct arg_data *,
157 struct args_size *);
158 static void precompute_arguments (int, struct arg_data *);
159 static int compute_argument_block_size (int, struct args_size *, tree, tree, int);
160 static void initialize_argument_information (int, struct arg_data *,
161 struct args_size *, int,
162 tree, tree,
163 tree, tree, cumulative_args_t, int,
164 rtx *, int *, int *, int *,
165 bool *, bool);
166 static void compute_argument_addresses (struct arg_data *, rtx, int);
167 static rtx rtx_for_function_call (tree, tree);
168 static void load_register_parameters (struct arg_data *, int, rtx *, int,
169 int, int *);
170 static rtx emit_library_call_value_1 (int, rtx, rtx, enum libcall_type,
171 machine_mode, int, va_list);
172 static int special_function_p (const_tree, int);
173 static int check_sibcall_argument_overlap_1 (rtx);
174 static int check_sibcall_argument_overlap (rtx_insn *, struct arg_data *, int);
176 static int combine_pending_stack_adjustment_and_call (int, struct args_size *,
177 unsigned int);
178 static tree split_complex_types (tree);
180 #ifdef REG_PARM_STACK_SPACE
181 static rtx save_fixed_argument_area (int, rtx, int *, int *);
182 static void restore_fixed_argument_area (rtx, rtx, int, int);
183 #endif
185 /* Force FUNEXP into a form suitable for the address of a CALL,
186 and return that as an rtx. Also load the static chain register
187 if FNDECL is a nested function.
189 CALL_FUSAGE points to a variable holding the prospective
190 CALL_INSN_FUNCTION_USAGE information. */
193 prepare_call_address (tree fndecl_or_type, rtx funexp, rtx static_chain_value,
194 rtx *call_fusage, int reg_parm_seen, int sibcallp)
196 /* Make a valid memory address and copy constants through pseudo-regs,
197 but not for a constant address if -fno-function-cse. */
198 if (GET_CODE (funexp) != SYMBOL_REF)
199 /* If we are using registers for parameters, force the
200 function address into a register now. */
201 funexp = ((reg_parm_seen
202 && targetm.small_register_classes_for_mode_p (FUNCTION_MODE))
203 ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
204 : memory_address (FUNCTION_MODE, funexp));
205 else if (flag_pic
206 && fndecl_or_type
207 && TREE_CODE (fndecl_or_type) == FUNCTION_DECL
208 && (!flag_plt
209 || lookup_attribute ("noplt", DECL_ATTRIBUTES (fndecl_or_type)))
210 && !targetm.binds_local_p (fndecl_or_type))
212 /* This is done only for PIC code. There is no easy interface to force the
213 function address into GOT for non-PIC case. non-PIC case needs to be
214 handled specially by the backend. */
215 funexp = force_reg (Pmode, funexp);
217 else if (! sibcallp)
219 if (!NO_FUNCTION_CSE && optimize && ! flag_no_function_cse)
220 funexp = force_reg (Pmode, funexp);
223 if (static_chain_value != 0
224 && (TREE_CODE (fndecl_or_type) != FUNCTION_DECL
225 || DECL_STATIC_CHAIN (fndecl_or_type)))
227 rtx chain;
229 chain = targetm.calls.static_chain (fndecl_or_type, false);
230 static_chain_value = convert_memory_address (Pmode, static_chain_value);
232 emit_move_insn (chain, static_chain_value);
233 if (REG_P (chain))
234 use_reg (call_fusage, chain);
237 return funexp;
240 /* Generate instructions to call function FUNEXP,
241 and optionally pop the results.
242 The CALL_INSN is the first insn generated.
244 FNDECL is the declaration node of the function. This is given to the
245 hook TARGET_RETURN_POPS_ARGS to determine whether this function pops
246 its own args.
248 FUNTYPE is the data type of the function. This is given to the hook
249 TARGET_RETURN_POPS_ARGS to determine whether this function pops its
250 own args. We used to allow an identifier for library functions, but
251 that doesn't work when the return type is an aggregate type and the
252 calling convention says that the pointer to this aggregate is to be
253 popped by the callee.
255 STACK_SIZE is the number of bytes of arguments on the stack,
256 ROUNDED_STACK_SIZE is that number rounded up to
257 PREFERRED_STACK_BOUNDARY; zero if the size is variable. This is
258 both to put into the call insn and to generate explicit popping
259 code if necessary.
261 STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
262 It is zero if this call doesn't want a structure value.
264 NEXT_ARG_REG is the rtx that results from executing
265 targetm.calls.function_arg (&args_so_far, VOIDmode, void_type_node, true)
266 just after all the args have had their registers assigned.
267 This could be whatever you like, but normally it is the first
268 arg-register beyond those used for args in this call,
269 or 0 if all the arg-registers are used in this call.
270 It is passed on to `gen_call' so you can put this info in the call insn.
272 VALREG is a hard register in which a value is returned,
273 or 0 if the call does not return a value.
275 OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
276 the args to this call were processed.
277 We restore `inhibit_defer_pop' to that value.
279 CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
280 denote registers used by the called function. */
282 static void
283 emit_call_1 (rtx funexp, tree fntree ATTRIBUTE_UNUSED, tree fndecl ATTRIBUTE_UNUSED,
284 tree funtype ATTRIBUTE_UNUSED,
285 HOST_WIDE_INT stack_size ATTRIBUTE_UNUSED,
286 HOST_WIDE_INT rounded_stack_size,
287 HOST_WIDE_INT struct_value_size ATTRIBUTE_UNUSED,
288 rtx next_arg_reg ATTRIBUTE_UNUSED, rtx valreg,
289 int old_inhibit_defer_pop, rtx call_fusage, int ecf_flags,
290 cumulative_args_t args_so_far ATTRIBUTE_UNUSED)
292 rtx rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
293 rtx call, funmem;
294 int already_popped = 0;
295 HOST_WIDE_INT n_popped
296 = targetm.calls.return_pops_args (fndecl, funtype, stack_size);
298 #ifdef CALL_POPS_ARGS
299 n_popped += CALL_POPS_ARGS (*get_cumulative_args (args_so_far));
300 #endif
302 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
303 and we don't want to load it into a register as an optimization,
304 because prepare_call_address already did it if it should be done. */
305 if (GET_CODE (funexp) != SYMBOL_REF)
306 funexp = memory_address (FUNCTION_MODE, funexp);
308 funmem = gen_rtx_MEM (FUNCTION_MODE, funexp);
309 if (fndecl && TREE_CODE (fndecl) == FUNCTION_DECL)
311 tree t = fndecl;
313 /* Although a built-in FUNCTION_DECL and its non-__builtin
314 counterpart compare equal and get a shared mem_attrs, they
315 produce different dump output in compare-debug compilations,
316 if an entry gets garbage collected in one compilation, then
317 adds a different (but equivalent) entry, while the other
318 doesn't run the garbage collector at the same spot and then
319 shares the mem_attr with the equivalent entry. */
320 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
322 tree t2 = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
323 if (t2)
324 t = t2;
327 set_mem_expr (funmem, t);
329 else if (fntree)
330 set_mem_expr (funmem, build_simple_mem_ref (CALL_EXPR_FN (fntree)));
332 #if defined (HAVE_sibcall_pop) && defined (HAVE_sibcall_value_pop)
333 if ((ecf_flags & ECF_SIBCALL)
334 && HAVE_sibcall_pop && HAVE_sibcall_value_pop
335 && (n_popped > 0 || stack_size == 0))
337 rtx n_pop = GEN_INT (n_popped);
338 rtx pat;
340 /* If this subroutine pops its own args, record that in the call insn
341 if possible, for the sake of frame pointer elimination. */
343 if (valreg)
344 pat = GEN_SIBCALL_VALUE_POP (valreg, funmem, rounded_stack_size_rtx,
345 next_arg_reg, n_pop);
346 else
347 pat = GEN_SIBCALL_POP (funmem, rounded_stack_size_rtx, next_arg_reg,
348 n_pop);
350 emit_call_insn (pat);
351 already_popped = 1;
353 else
354 #endif
356 #if defined (HAVE_call_pop) && defined (HAVE_call_value_pop)
357 /* If the target has "call" or "call_value" insns, then prefer them
358 if no arguments are actually popped. If the target does not have
359 "call" or "call_value" insns, then we must use the popping versions
360 even if the call has no arguments to pop. */
361 #if defined (HAVE_call) && defined (HAVE_call_value)
362 if (HAVE_call && HAVE_call_value && HAVE_call_pop && HAVE_call_value_pop
363 && n_popped > 0)
364 #else
365 if (HAVE_call_pop && HAVE_call_value_pop)
366 #endif
368 rtx n_pop = GEN_INT (n_popped);
369 rtx pat;
371 /* If this subroutine pops its own args, record that in the call insn
372 if possible, for the sake of frame pointer elimination. */
374 if (valreg)
375 pat = GEN_CALL_VALUE_POP (valreg, funmem, rounded_stack_size_rtx,
376 next_arg_reg, n_pop);
377 else
378 pat = GEN_CALL_POP (funmem, rounded_stack_size_rtx, next_arg_reg,
379 n_pop);
381 emit_call_insn (pat);
382 already_popped = 1;
384 else
385 #endif
387 #if defined (HAVE_sibcall) && defined (HAVE_sibcall_value)
388 if ((ecf_flags & ECF_SIBCALL)
389 && HAVE_sibcall && HAVE_sibcall_value)
391 if (valreg)
392 emit_call_insn (GEN_SIBCALL_VALUE (valreg, funmem,
393 rounded_stack_size_rtx,
394 next_arg_reg, NULL_RTX));
395 else
396 emit_call_insn (GEN_SIBCALL (funmem, rounded_stack_size_rtx,
397 next_arg_reg,
398 GEN_INT (struct_value_size)));
400 else
401 #endif
403 #if defined (HAVE_call) && defined (HAVE_call_value)
404 if (HAVE_call && HAVE_call_value)
406 if (valreg)
407 emit_call_insn (GEN_CALL_VALUE (valreg, funmem, rounded_stack_size_rtx,
408 next_arg_reg, NULL_RTX));
409 else
410 emit_call_insn (GEN_CALL (funmem, rounded_stack_size_rtx, next_arg_reg,
411 GEN_INT (struct_value_size)));
413 else
414 #endif
415 gcc_unreachable ();
417 /* Find the call we just emitted. */
418 rtx_call_insn *call_insn = last_call_insn ();
420 /* Some target create a fresh MEM instead of reusing the one provided
421 above. Set its MEM_EXPR. */
422 call = get_call_rtx_from (call_insn);
423 if (call
424 && MEM_EXPR (XEXP (call, 0)) == NULL_TREE
425 && MEM_EXPR (funmem) != NULL_TREE)
426 set_mem_expr (XEXP (call, 0), MEM_EXPR (funmem));
428 /* Mark instrumented calls. */
429 if (call && fntree)
430 CALL_EXPR_WITH_BOUNDS_P (call) = CALL_WITH_BOUNDS_P (fntree);
432 /* Put the register usage information there. */
433 add_function_usage_to (call_insn, call_fusage);
435 /* If this is a const call, then set the insn's unchanging bit. */
436 if (ecf_flags & ECF_CONST)
437 RTL_CONST_CALL_P (call_insn) = 1;
439 /* If this is a pure call, then set the insn's unchanging bit. */
440 if (ecf_flags & ECF_PURE)
441 RTL_PURE_CALL_P (call_insn) = 1;
443 /* If this is a const call, then set the insn's unchanging bit. */
444 if (ecf_flags & ECF_LOOPING_CONST_OR_PURE)
445 RTL_LOOPING_CONST_OR_PURE_CALL_P (call_insn) = 1;
447 /* Create a nothrow REG_EH_REGION note, if needed. */
448 make_reg_eh_region_note (call_insn, ecf_flags, 0);
450 if (ecf_flags & ECF_NORETURN)
451 add_reg_note (call_insn, REG_NORETURN, const0_rtx);
453 if (ecf_flags & ECF_RETURNS_TWICE)
455 add_reg_note (call_insn, REG_SETJMP, const0_rtx);
456 cfun->calls_setjmp = 1;
459 SIBLING_CALL_P (call_insn) = ((ecf_flags & ECF_SIBCALL) != 0);
461 /* Restore this now, so that we do defer pops for this call's args
462 if the context of the call as a whole permits. */
463 inhibit_defer_pop = old_inhibit_defer_pop;
465 if (n_popped > 0)
467 if (!already_popped)
468 CALL_INSN_FUNCTION_USAGE (call_insn)
469 = gen_rtx_EXPR_LIST (VOIDmode,
470 gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx),
471 CALL_INSN_FUNCTION_USAGE (call_insn));
472 rounded_stack_size -= n_popped;
473 rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
474 stack_pointer_delta -= n_popped;
476 add_reg_note (call_insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
478 /* If popup is needed, stack realign must use DRAP */
479 if (SUPPORTS_STACK_ALIGNMENT)
480 crtl->need_drap = true;
482 /* For noreturn calls when not accumulating outgoing args force
483 REG_ARGS_SIZE note to prevent crossjumping of calls with different
484 args sizes. */
485 else if (!ACCUMULATE_OUTGOING_ARGS && (ecf_flags & ECF_NORETURN) != 0)
486 add_reg_note (call_insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
488 if (!ACCUMULATE_OUTGOING_ARGS)
490 /* If returning from the subroutine does not automatically pop the args,
491 we need an instruction to pop them sooner or later.
492 Perhaps do it now; perhaps just record how much space to pop later.
494 If returning from the subroutine does pop the args, indicate that the
495 stack pointer will be changed. */
497 if (rounded_stack_size != 0)
499 if (ecf_flags & ECF_NORETURN)
500 /* Just pretend we did the pop. */
501 stack_pointer_delta -= rounded_stack_size;
502 else if (flag_defer_pop && inhibit_defer_pop == 0
503 && ! (ecf_flags & (ECF_CONST | ECF_PURE)))
504 pending_stack_adjust += rounded_stack_size;
505 else
506 adjust_stack (rounded_stack_size_rtx);
509 /* When we accumulate outgoing args, we must avoid any stack manipulations.
510 Restore the stack pointer to its original value now. Usually
511 ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
512 On i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
513 popping variants of functions exist as well.
515 ??? We may optimize similar to defer_pop above, but it is
516 probably not worthwhile.
518 ??? It will be worthwhile to enable combine_stack_adjustments even for
519 such machines. */
520 else if (n_popped)
521 anti_adjust_stack (GEN_INT (n_popped));
524 /* Determine if the function identified by NAME and FNDECL is one with
525 special properties we wish to know about.
527 For example, if the function might return more than one time (setjmp), then
528 set RETURNS_TWICE to a nonzero value.
530 Similarly set NORETURN if the function is in the longjmp family.
532 Set MAY_BE_ALLOCA for any memory allocation function that might allocate
533 space from the stack such as alloca. */
535 static int
536 special_function_p (const_tree fndecl, int flags)
538 tree name_decl = DECL_NAME (fndecl);
540 /* For instrumentation clones we want to derive flags
541 from the original name. */
542 if (cgraph_node::get (fndecl)
543 && cgraph_node::get (fndecl)->instrumentation_clone)
544 name_decl = DECL_NAME (cgraph_node::get (fndecl)->orig_decl);
546 if (fndecl && name_decl
547 && IDENTIFIER_LENGTH (name_decl) <= 17
548 /* Exclude functions not at the file scope, or not `extern',
549 since they are not the magic functions we would otherwise
550 think they are.
551 FIXME: this should be handled with attributes, not with this
552 hacky imitation of DECL_ASSEMBLER_NAME. It's (also) wrong
553 because you can declare fork() inside a function if you
554 wish. */
555 && (DECL_CONTEXT (fndecl) == NULL_TREE
556 || TREE_CODE (DECL_CONTEXT (fndecl)) == TRANSLATION_UNIT_DECL)
557 && TREE_PUBLIC (fndecl))
559 const char *name = IDENTIFIER_POINTER (name_decl);
560 const char *tname = name;
562 /* We assume that alloca will always be called by name. It
563 makes no sense to pass it as a pointer-to-function to
564 anything that does not understand its behavior. */
565 if (((IDENTIFIER_LENGTH (name_decl) == 6
566 && name[0] == 'a'
567 && ! strcmp (name, "alloca"))
568 || (IDENTIFIER_LENGTH (name_decl) == 16
569 && name[0] == '_'
570 && ! strcmp (name, "__builtin_alloca"))))
571 flags |= ECF_MAY_BE_ALLOCA;
573 /* Disregard prefix _, __, __x or __builtin_. */
574 if (name[0] == '_')
576 if (name[1] == '_'
577 && name[2] == 'b'
578 && !strncmp (name + 3, "uiltin_", 7))
579 tname += 10;
580 else if (name[1] == '_' && name[2] == 'x')
581 tname += 3;
582 else if (name[1] == '_')
583 tname += 2;
584 else
585 tname += 1;
588 if (tname[0] == 's')
590 if ((tname[1] == 'e'
591 && (! strcmp (tname, "setjmp")
592 || ! strcmp (tname, "setjmp_syscall")))
593 || (tname[1] == 'i'
594 && ! strcmp (tname, "sigsetjmp"))
595 || (tname[1] == 'a'
596 && ! strcmp (tname, "savectx")))
597 flags |= ECF_RETURNS_TWICE | ECF_LEAF;
599 if (tname[1] == 'i'
600 && ! strcmp (tname, "siglongjmp"))
601 flags |= ECF_NORETURN;
603 else if ((tname[0] == 'q' && tname[1] == 's'
604 && ! strcmp (tname, "qsetjmp"))
605 || (tname[0] == 'v' && tname[1] == 'f'
606 && ! strcmp (tname, "vfork"))
607 || (tname[0] == 'g' && tname[1] == 'e'
608 && !strcmp (tname, "getcontext")))
609 flags |= ECF_RETURNS_TWICE | ECF_LEAF;
611 else if (tname[0] == 'l' && tname[1] == 'o'
612 && ! strcmp (tname, "longjmp"))
613 flags |= ECF_NORETURN;
616 return flags;
619 /* Similar to special_function_p; return a set of ERF_ flags for the
620 function FNDECL. */
621 static int
622 decl_return_flags (tree fndecl)
624 tree attr;
625 tree type = TREE_TYPE (fndecl);
626 if (!type)
627 return 0;
629 attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
630 if (!attr)
631 return 0;
633 attr = TREE_VALUE (TREE_VALUE (attr));
634 if (!attr || TREE_STRING_LENGTH (attr) < 1)
635 return 0;
637 switch (TREE_STRING_POINTER (attr)[0])
639 case '1':
640 case '2':
641 case '3':
642 case '4':
643 return ERF_RETURNS_ARG | (TREE_STRING_POINTER (attr)[0] - '1');
645 case 'm':
646 return ERF_NOALIAS;
648 case '.':
649 default:
650 return 0;
654 /* Return nonzero when FNDECL represents a call to setjmp. */
657 setjmp_call_p (const_tree fndecl)
659 if (DECL_IS_RETURNS_TWICE (fndecl))
660 return ECF_RETURNS_TWICE;
661 return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
665 /* Return true if STMT is an alloca call. */
667 bool
668 gimple_alloca_call_p (const_gimple stmt)
670 tree fndecl;
672 if (!is_gimple_call (stmt))
673 return false;
675 fndecl = gimple_call_fndecl (stmt);
676 if (fndecl && (special_function_p (fndecl, 0) & ECF_MAY_BE_ALLOCA))
677 return true;
679 return false;
682 /* Return true when exp contains alloca call. */
684 bool
685 alloca_call_p (const_tree exp)
687 tree fndecl;
688 if (TREE_CODE (exp) == CALL_EXPR
689 && (fndecl = get_callee_fndecl (exp))
690 && (special_function_p (fndecl, 0) & ECF_MAY_BE_ALLOCA))
691 return true;
692 return false;
695 /* Return TRUE if FNDECL is either a TM builtin or a TM cloned
696 function. Return FALSE otherwise. */
698 static bool
699 is_tm_builtin (const_tree fndecl)
701 if (fndecl == NULL)
702 return false;
704 if (decl_is_tm_clone (fndecl))
705 return true;
707 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
709 switch (DECL_FUNCTION_CODE (fndecl))
711 case BUILT_IN_TM_COMMIT:
712 case BUILT_IN_TM_COMMIT_EH:
713 case BUILT_IN_TM_ABORT:
714 case BUILT_IN_TM_IRREVOCABLE:
715 case BUILT_IN_TM_GETTMCLONE_IRR:
716 case BUILT_IN_TM_MEMCPY:
717 case BUILT_IN_TM_MEMMOVE:
718 case BUILT_IN_TM_MEMSET:
719 CASE_BUILT_IN_TM_STORE (1):
720 CASE_BUILT_IN_TM_STORE (2):
721 CASE_BUILT_IN_TM_STORE (4):
722 CASE_BUILT_IN_TM_STORE (8):
723 CASE_BUILT_IN_TM_STORE (FLOAT):
724 CASE_BUILT_IN_TM_STORE (DOUBLE):
725 CASE_BUILT_IN_TM_STORE (LDOUBLE):
726 CASE_BUILT_IN_TM_STORE (M64):
727 CASE_BUILT_IN_TM_STORE (M128):
728 CASE_BUILT_IN_TM_STORE (M256):
729 CASE_BUILT_IN_TM_LOAD (1):
730 CASE_BUILT_IN_TM_LOAD (2):
731 CASE_BUILT_IN_TM_LOAD (4):
732 CASE_BUILT_IN_TM_LOAD (8):
733 CASE_BUILT_IN_TM_LOAD (FLOAT):
734 CASE_BUILT_IN_TM_LOAD (DOUBLE):
735 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
736 CASE_BUILT_IN_TM_LOAD (M64):
737 CASE_BUILT_IN_TM_LOAD (M128):
738 CASE_BUILT_IN_TM_LOAD (M256):
739 case BUILT_IN_TM_LOG:
740 case BUILT_IN_TM_LOG_1:
741 case BUILT_IN_TM_LOG_2:
742 case BUILT_IN_TM_LOG_4:
743 case BUILT_IN_TM_LOG_8:
744 case BUILT_IN_TM_LOG_FLOAT:
745 case BUILT_IN_TM_LOG_DOUBLE:
746 case BUILT_IN_TM_LOG_LDOUBLE:
747 case BUILT_IN_TM_LOG_M64:
748 case BUILT_IN_TM_LOG_M128:
749 case BUILT_IN_TM_LOG_M256:
750 return true;
751 default:
752 break;
755 return false;
758 /* Detect flags (function attributes) from the function decl or type node. */
761 flags_from_decl_or_type (const_tree exp)
763 int flags = 0;
765 if (DECL_P (exp))
767 /* The function exp may have the `malloc' attribute. */
768 if (DECL_IS_MALLOC (exp))
769 flags |= ECF_MALLOC;
771 /* The function exp may have the `returns_twice' attribute. */
772 if (DECL_IS_RETURNS_TWICE (exp))
773 flags |= ECF_RETURNS_TWICE;
775 /* Process the pure and const attributes. */
776 if (TREE_READONLY (exp))
777 flags |= ECF_CONST;
778 if (DECL_PURE_P (exp))
779 flags |= ECF_PURE;
780 if (DECL_LOOPING_CONST_OR_PURE_P (exp))
781 flags |= ECF_LOOPING_CONST_OR_PURE;
783 if (DECL_IS_NOVOPS (exp))
784 flags |= ECF_NOVOPS;
785 if (lookup_attribute ("leaf", DECL_ATTRIBUTES (exp)))
786 flags |= ECF_LEAF;
788 if (TREE_NOTHROW (exp))
789 flags |= ECF_NOTHROW;
791 if (flag_tm)
793 if (is_tm_builtin (exp))
794 flags |= ECF_TM_BUILTIN;
795 else if ((flags & (ECF_CONST|ECF_NOVOPS)) != 0
796 || lookup_attribute ("transaction_pure",
797 TYPE_ATTRIBUTES (TREE_TYPE (exp))))
798 flags |= ECF_TM_PURE;
801 flags = special_function_p (exp, flags);
803 else if (TYPE_P (exp))
805 if (TYPE_READONLY (exp))
806 flags |= ECF_CONST;
808 if (flag_tm
809 && ((flags & ECF_CONST) != 0
810 || lookup_attribute ("transaction_pure", TYPE_ATTRIBUTES (exp))))
811 flags |= ECF_TM_PURE;
813 else
814 gcc_unreachable ();
816 if (TREE_THIS_VOLATILE (exp))
818 flags |= ECF_NORETURN;
819 if (flags & (ECF_CONST|ECF_PURE))
820 flags |= ECF_LOOPING_CONST_OR_PURE;
823 return flags;
826 /* Detect flags from a CALL_EXPR. */
829 call_expr_flags (const_tree t)
831 int flags;
832 tree decl = get_callee_fndecl (t);
834 if (decl)
835 flags = flags_from_decl_or_type (decl);
836 else if (CALL_EXPR_FN (t) == NULL_TREE)
837 flags = internal_fn_flags (CALL_EXPR_IFN (t));
838 else
840 t = TREE_TYPE (CALL_EXPR_FN (t));
841 if (t && TREE_CODE (t) == POINTER_TYPE)
842 flags = flags_from_decl_or_type (TREE_TYPE (t));
843 else
844 flags = 0;
847 return flags;
850 /* Return true if TYPE should be passed by invisible reference. */
852 bool
853 pass_by_reference (CUMULATIVE_ARGS *ca, machine_mode mode,
854 tree type, bool named_arg)
856 if (type)
858 /* If this type contains non-trivial constructors, then it is
859 forbidden for the middle-end to create any new copies. */
860 if (TREE_ADDRESSABLE (type))
861 return true;
863 /* GCC post 3.4 passes *all* variable sized types by reference. */
864 if (!TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
865 return true;
867 /* If a record type should be passed the same as its first (and only)
868 member, use the type and mode of that member. */
869 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
871 type = TREE_TYPE (first_field (type));
872 mode = TYPE_MODE (type);
876 return targetm.calls.pass_by_reference (pack_cumulative_args (ca), mode,
877 type, named_arg);
880 /* Return true if TYPE, which is passed by reference, should be callee
881 copied instead of caller copied. */
883 bool
884 reference_callee_copied (CUMULATIVE_ARGS *ca, machine_mode mode,
885 tree type, bool named_arg)
887 if (type && TREE_ADDRESSABLE (type))
888 return false;
889 return targetm.calls.callee_copies (pack_cumulative_args (ca), mode, type,
890 named_arg);
894 /* Precompute all register parameters as described by ARGS, storing values
895 into fields within the ARGS array.
897 NUM_ACTUALS indicates the total number elements in the ARGS array.
899 Set REG_PARM_SEEN if we encounter a register parameter. */
901 static void
902 precompute_register_parameters (int num_actuals, struct arg_data *args,
903 int *reg_parm_seen)
905 int i;
907 *reg_parm_seen = 0;
909 for (i = 0; i < num_actuals; i++)
910 if (args[i].reg != 0 && ! args[i].pass_on_stack)
912 *reg_parm_seen = 1;
914 if (args[i].value == 0)
916 push_temp_slots ();
917 args[i].value = expand_normal (args[i].tree_value);
918 preserve_temp_slots (args[i].value);
919 pop_temp_slots ();
922 /* If we are to promote the function arg to a wider mode,
923 do it now. */
925 if (args[i].mode != TYPE_MODE (TREE_TYPE (args[i].tree_value)))
926 args[i].value
927 = convert_modes (args[i].mode,
928 TYPE_MODE (TREE_TYPE (args[i].tree_value)),
929 args[i].value, args[i].unsignedp);
931 /* If the value is a non-legitimate constant, force it into a
932 pseudo now. TLS symbols sometimes need a call to resolve. */
933 if (CONSTANT_P (args[i].value)
934 && !targetm.legitimate_constant_p (args[i].mode, args[i].value))
935 args[i].value = force_reg (args[i].mode, args[i].value);
937 /* If we're going to have to load the value by parts, pull the
938 parts into pseudos. The part extraction process can involve
939 non-trivial computation. */
940 if (GET_CODE (args[i].reg) == PARALLEL)
942 tree type = TREE_TYPE (args[i].tree_value);
943 args[i].parallel_value
944 = emit_group_load_into_temps (args[i].reg, args[i].value,
945 type, int_size_in_bytes (type));
948 /* If the value is expensive, and we are inside an appropriately
949 short loop, put the value into a pseudo and then put the pseudo
950 into the hard reg.
952 For small register classes, also do this if this call uses
953 register parameters. This is to avoid reload conflicts while
954 loading the parameters registers. */
956 else if ((! (REG_P (args[i].value)
957 || (GET_CODE (args[i].value) == SUBREG
958 && REG_P (SUBREG_REG (args[i].value)))))
959 && args[i].mode != BLKmode
960 && (set_src_cost (args[i].value, args[i].mode,
961 optimize_insn_for_speed_p ())
962 > COSTS_N_INSNS (1))
963 && ((*reg_parm_seen
964 && targetm.small_register_classes_for_mode_p (args[i].mode))
965 || optimize))
966 args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
970 #ifdef REG_PARM_STACK_SPACE
972 /* The argument list is the property of the called routine and it
973 may clobber it. If the fixed area has been used for previous
974 parameters, we must save and restore it. */
976 static rtx
977 save_fixed_argument_area (int reg_parm_stack_space, rtx argblock, int *low_to_save, int *high_to_save)
979 int low;
980 int high;
982 /* Compute the boundary of the area that needs to be saved, if any. */
983 high = reg_parm_stack_space;
984 if (ARGS_GROW_DOWNWARD)
985 high += 1;
987 if (high > highest_outgoing_arg_in_use)
988 high = highest_outgoing_arg_in_use;
990 for (low = 0; low < high; low++)
991 if (stack_usage_map[low] != 0)
993 int num_to_save;
994 machine_mode save_mode;
995 int delta;
996 rtx addr;
997 rtx stack_area;
998 rtx save_area;
1000 while (stack_usage_map[--high] == 0)
1003 *low_to_save = low;
1004 *high_to_save = high;
1006 num_to_save = high - low + 1;
1007 save_mode = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
1009 /* If we don't have the required alignment, must do this
1010 in BLKmode. */
1011 if ((low & (MIN (GET_MODE_SIZE (save_mode),
1012 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
1013 save_mode = BLKmode;
1015 if (ARGS_GROW_DOWNWARD)
1016 delta = -high;
1017 else
1018 delta = low;
1020 addr = plus_constant (Pmode, argblock, delta);
1021 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
1023 set_mem_align (stack_area, PARM_BOUNDARY);
1024 if (save_mode == BLKmode)
1026 save_area = assign_stack_temp (BLKmode, num_to_save);
1027 emit_block_move (validize_mem (save_area), stack_area,
1028 GEN_INT (num_to_save), BLOCK_OP_CALL_PARM);
1030 else
1032 save_area = gen_reg_rtx (save_mode);
1033 emit_move_insn (save_area, stack_area);
1036 return save_area;
1039 return NULL_RTX;
1042 static void
1043 restore_fixed_argument_area (rtx save_area, rtx argblock, int high_to_save, int low_to_save)
1045 machine_mode save_mode = GET_MODE (save_area);
1046 int delta;
1047 rtx addr, stack_area;
1049 if (ARGS_GROW_DOWNWARD)
1050 delta = -high_to_save;
1051 else
1052 delta = low_to_save;
1054 addr = plus_constant (Pmode, argblock, delta);
1055 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
1056 set_mem_align (stack_area, PARM_BOUNDARY);
1058 if (save_mode != BLKmode)
1059 emit_move_insn (stack_area, save_area);
1060 else
1061 emit_block_move (stack_area, validize_mem (save_area),
1062 GEN_INT (high_to_save - low_to_save + 1),
1063 BLOCK_OP_CALL_PARM);
1065 #endif /* REG_PARM_STACK_SPACE */
1067 /* If any elements in ARGS refer to parameters that are to be passed in
1068 registers, but not in memory, and whose alignment does not permit a
1069 direct copy into registers. Copy the values into a group of pseudos
1070 which we will later copy into the appropriate hard registers.
1072 Pseudos for each unaligned argument will be stored into the array
1073 args[argnum].aligned_regs. The caller is responsible for deallocating
1074 the aligned_regs array if it is nonzero. */
1076 static void
1077 store_unaligned_arguments_into_pseudos (struct arg_data *args, int num_actuals)
1079 int i, j;
1081 for (i = 0; i < num_actuals; i++)
1082 if (args[i].reg != 0 && ! args[i].pass_on_stack
1083 && GET_CODE (args[i].reg) != PARALLEL
1084 && args[i].mode == BLKmode
1085 && MEM_P (args[i].value)
1086 && (MEM_ALIGN (args[i].value)
1087 < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
1089 int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1090 int endian_correction = 0;
1092 if (args[i].partial)
1094 gcc_assert (args[i].partial % UNITS_PER_WORD == 0);
1095 args[i].n_aligned_regs = args[i].partial / UNITS_PER_WORD;
1097 else
1099 args[i].n_aligned_regs
1100 = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1103 args[i].aligned_regs = XNEWVEC (rtx, args[i].n_aligned_regs);
1105 /* Structures smaller than a word are normally aligned to the
1106 least significant byte. On a BYTES_BIG_ENDIAN machine,
1107 this means we must skip the empty high order bytes when
1108 calculating the bit offset. */
1109 if (bytes < UNITS_PER_WORD
1110 #ifdef BLOCK_REG_PADDING
1111 && (BLOCK_REG_PADDING (args[i].mode,
1112 TREE_TYPE (args[i].tree_value), 1)
1113 == downward)
1114 #else
1115 && BYTES_BIG_ENDIAN
1116 #endif
1118 endian_correction = BITS_PER_WORD - bytes * BITS_PER_UNIT;
1120 for (j = 0; j < args[i].n_aligned_regs; j++)
1122 rtx reg = gen_reg_rtx (word_mode);
1123 rtx word = operand_subword_force (args[i].value, j, BLKmode);
1124 int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
1126 args[i].aligned_regs[j] = reg;
1127 word = extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
1128 word_mode, word_mode);
1130 /* There is no need to restrict this code to loading items
1131 in TYPE_ALIGN sized hunks. The bitfield instructions can
1132 load up entire word sized registers efficiently.
1134 ??? This may not be needed anymore.
1135 We use to emit a clobber here but that doesn't let later
1136 passes optimize the instructions we emit. By storing 0 into
1137 the register later passes know the first AND to zero out the
1138 bitfield being set in the register is unnecessary. The store
1139 of 0 will be deleted as will at least the first AND. */
1141 emit_move_insn (reg, const0_rtx);
1143 bytes -= bitsize / BITS_PER_UNIT;
1144 store_bit_field (reg, bitsize, endian_correction, 0, 0,
1145 word_mode, word);
1150 /* Fill in ARGS_SIZE and ARGS array based on the parameters found in
1151 CALL_EXPR EXP.
1153 NUM_ACTUALS is the total number of parameters.
1155 N_NAMED_ARGS is the total number of named arguments.
1157 STRUCT_VALUE_ADDR_VALUE is the implicit argument for a struct return
1158 value, or null.
1160 FNDECL is the tree code for the target of this call (if known)
1162 ARGS_SO_FAR holds state needed by the target to know where to place
1163 the next argument.
1165 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
1166 for arguments which are passed in registers.
1168 OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
1169 and may be modified by this routine.
1171 OLD_PENDING_ADJ, MUST_PREALLOCATE and FLAGS are pointers to integer
1172 flags which may may be modified by this routine.
1174 MAY_TAILCALL is cleared if we encounter an invisible pass-by-reference
1175 that requires allocation of stack space.
1177 CALL_FROM_THUNK_P is true if this call is the jump from a thunk to
1178 the thunked-to function. */
1180 static void
1181 initialize_argument_information (int num_actuals ATTRIBUTE_UNUSED,
1182 struct arg_data *args,
1183 struct args_size *args_size,
1184 int n_named_args ATTRIBUTE_UNUSED,
1185 tree exp, tree struct_value_addr_value,
1186 tree fndecl, tree fntype,
1187 cumulative_args_t args_so_far,
1188 int reg_parm_stack_space,
1189 rtx *old_stack_level, int *old_pending_adj,
1190 int *must_preallocate, int *ecf_flags,
1191 bool *may_tailcall, bool call_from_thunk_p)
1193 CUMULATIVE_ARGS *args_so_far_pnt = get_cumulative_args (args_so_far);
1194 location_t loc = EXPR_LOCATION (exp);
1196 /* Count arg position in order args appear. */
1197 int argpos;
1199 int i;
1201 args_size->constant = 0;
1202 args_size->var = 0;
1204 bitmap_obstack_initialize (NULL);
1206 /* In this loop, we consider args in the order they are written.
1207 We fill up ARGS from the back. */
1209 i = num_actuals - 1;
1211 int j = i, ptr_arg = -1;
1212 call_expr_arg_iterator iter;
1213 tree arg;
1214 bitmap slots = NULL;
1216 if (struct_value_addr_value)
1218 args[j].tree_value = struct_value_addr_value;
1219 j--;
1221 /* If we pass structure address then we need to
1222 create bounds for it. Since created bounds is
1223 a call statement, we expand it right here to avoid
1224 fixing all other places where it may be expanded. */
1225 if (CALL_WITH_BOUNDS_P (exp))
1227 args[j].value = gen_reg_rtx (targetm.chkp_bound_mode ());
1228 args[j].tree_value
1229 = chkp_make_bounds_for_struct_addr (struct_value_addr_value);
1230 expand_expr_real (args[j].tree_value, args[j].value, VOIDmode,
1231 EXPAND_NORMAL, 0, false);
1232 args[j].pointer_arg = j + 1;
1233 j--;
1236 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
1238 tree argtype = TREE_TYPE (arg);
1240 /* Remember last param with pointer and associate it
1241 with following pointer bounds. */
1242 if (CALL_WITH_BOUNDS_P (exp)
1243 && chkp_type_has_pointer (argtype))
1245 if (slots)
1246 BITMAP_FREE (slots);
1247 ptr_arg = j;
1248 if (!BOUNDED_TYPE_P (argtype))
1250 slots = BITMAP_ALLOC (NULL);
1251 chkp_find_bound_slots (argtype, slots);
1254 else if (POINTER_BOUNDS_TYPE_P (argtype))
1256 /* We expect bounds in instrumented calls only.
1257 Otherwise it is a sign we lost flag due to some optimization
1258 and may emit call args incorrectly. */
1259 gcc_assert (CALL_WITH_BOUNDS_P (exp));
1261 /* For structures look for the next available pointer. */
1262 if (ptr_arg != -1 && slots)
1264 unsigned bnd_no = bitmap_first_set_bit (slots);
1265 args[j].pointer_offset =
1266 bnd_no * POINTER_SIZE / BITS_PER_UNIT;
1268 bitmap_clear_bit (slots, bnd_no);
1270 /* Check we have no more pointers in the structure. */
1271 if (bitmap_empty_p (slots))
1272 BITMAP_FREE (slots);
1274 args[j].pointer_arg = ptr_arg;
1276 /* Check we covered all pointers in the previous
1277 non bounds arg. */
1278 if (!slots)
1279 ptr_arg = -1;
1281 else
1282 ptr_arg = -1;
1284 if (targetm.calls.split_complex_arg
1285 && argtype
1286 && TREE_CODE (argtype) == COMPLEX_TYPE
1287 && targetm.calls.split_complex_arg (argtype))
1289 tree subtype = TREE_TYPE (argtype);
1290 args[j].tree_value = build1 (REALPART_EXPR, subtype, arg);
1291 j--;
1292 args[j].tree_value = build1 (IMAGPART_EXPR, subtype, arg);
1294 else
1295 args[j].tree_value = arg;
1296 j--;
1299 if (slots)
1300 BITMAP_FREE (slots);
1303 bitmap_obstack_release (NULL);
1305 /* I counts args in order (to be) pushed; ARGPOS counts in order written. */
1306 for (argpos = 0; argpos < num_actuals; i--, argpos++)
1308 tree type = TREE_TYPE (args[i].tree_value);
1309 int unsignedp;
1310 machine_mode mode;
1312 /* Replace erroneous argument with constant zero. */
1313 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1314 args[i].tree_value = integer_zero_node, type = integer_type_node;
1316 /* If TYPE is a transparent union or record, pass things the way
1317 we would pass the first field of the union or record. We have
1318 already verified that the modes are the same. */
1319 if ((TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == RECORD_TYPE)
1320 && TYPE_TRANSPARENT_AGGR (type))
1321 type = TREE_TYPE (first_field (type));
1323 /* Decide where to pass this arg.
1325 args[i].reg is nonzero if all or part is passed in registers.
1327 args[i].partial is nonzero if part but not all is passed in registers,
1328 and the exact value says how many bytes are passed in registers.
1330 args[i].pass_on_stack is nonzero if the argument must at least be
1331 computed on the stack. It may then be loaded back into registers
1332 if args[i].reg is nonzero.
1334 These decisions are driven by the FUNCTION_... macros and must agree
1335 with those made by function.c. */
1337 /* See if this argument should be passed by invisible reference. */
1338 if (pass_by_reference (args_so_far_pnt, TYPE_MODE (type),
1339 type, argpos < n_named_args))
1341 bool callee_copies;
1342 tree base = NULL_TREE;
1344 callee_copies
1345 = reference_callee_copied (args_so_far_pnt, TYPE_MODE (type),
1346 type, argpos < n_named_args);
1348 /* If we're compiling a thunk, pass through invisible references
1349 instead of making a copy. */
1350 if (call_from_thunk_p
1351 || (callee_copies
1352 && !TREE_ADDRESSABLE (type)
1353 && (base = get_base_address (args[i].tree_value))
1354 && TREE_CODE (base) != SSA_NAME
1355 && (!DECL_P (base) || MEM_P (DECL_RTL (base)))))
1357 /* We may have turned the parameter value into an SSA name.
1358 Go back to the original parameter so we can take the
1359 address. */
1360 if (TREE_CODE (args[i].tree_value) == SSA_NAME)
1362 gcc_assert (SSA_NAME_IS_DEFAULT_DEF (args[i].tree_value));
1363 args[i].tree_value = SSA_NAME_VAR (args[i].tree_value);
1364 gcc_assert (TREE_CODE (args[i].tree_value) == PARM_DECL);
1366 /* Argument setup code may have copied the value to register. We
1367 revert that optimization now because the tail call code must
1368 use the original location. */
1369 if (TREE_CODE (args[i].tree_value) == PARM_DECL
1370 && !MEM_P (DECL_RTL (args[i].tree_value))
1371 && DECL_INCOMING_RTL (args[i].tree_value)
1372 && MEM_P (DECL_INCOMING_RTL (args[i].tree_value)))
1373 set_decl_rtl (args[i].tree_value,
1374 DECL_INCOMING_RTL (args[i].tree_value));
1376 mark_addressable (args[i].tree_value);
1378 /* We can't use sibcalls if a callee-copied argument is
1379 stored in the current function's frame. */
1380 if (!call_from_thunk_p && DECL_P (base) && !TREE_STATIC (base))
1381 *may_tailcall = false;
1383 args[i].tree_value = build_fold_addr_expr_loc (loc,
1384 args[i].tree_value);
1385 type = TREE_TYPE (args[i].tree_value);
1387 if (*ecf_flags & ECF_CONST)
1388 *ecf_flags &= ~(ECF_CONST | ECF_LOOPING_CONST_OR_PURE);
1390 else
1392 /* We make a copy of the object and pass the address to the
1393 function being called. */
1394 rtx copy;
1396 if (!COMPLETE_TYPE_P (type)
1397 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
1398 || (flag_stack_check == GENERIC_STACK_CHECK
1399 && compare_tree_int (TYPE_SIZE_UNIT (type),
1400 STACK_CHECK_MAX_VAR_SIZE) > 0))
1402 /* This is a variable-sized object. Make space on the stack
1403 for it. */
1404 rtx size_rtx = expr_size (args[i].tree_value);
1406 if (*old_stack_level == 0)
1408 emit_stack_save (SAVE_BLOCK, old_stack_level);
1409 *old_pending_adj = pending_stack_adjust;
1410 pending_stack_adjust = 0;
1413 /* We can pass TRUE as the 4th argument because we just
1414 saved the stack pointer and will restore it right after
1415 the call. */
1416 copy = allocate_dynamic_stack_space (size_rtx,
1417 TYPE_ALIGN (type),
1418 TYPE_ALIGN (type),
1419 true);
1420 copy = gen_rtx_MEM (BLKmode, copy);
1421 set_mem_attributes (copy, type, 1);
1423 else
1424 copy = assign_temp (type, 1, 0);
1426 store_expr (args[i].tree_value, copy, 0, false);
1428 /* Just change the const function to pure and then let
1429 the next test clear the pure based on
1430 callee_copies. */
1431 if (*ecf_flags & ECF_CONST)
1433 *ecf_flags &= ~ECF_CONST;
1434 *ecf_flags |= ECF_PURE;
1437 if (!callee_copies && *ecf_flags & ECF_PURE)
1438 *ecf_flags &= ~(ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
1440 args[i].tree_value
1441 = build_fold_addr_expr_loc (loc, make_tree (type, copy));
1442 type = TREE_TYPE (args[i].tree_value);
1443 *may_tailcall = false;
1447 unsignedp = TYPE_UNSIGNED (type);
1448 mode = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
1449 fndecl ? TREE_TYPE (fndecl) : fntype, 0);
1451 args[i].unsignedp = unsignedp;
1452 args[i].mode = mode;
1454 args[i].reg = targetm.calls.function_arg (args_so_far, mode, type,
1455 argpos < n_named_args);
1457 if (args[i].reg && CONST_INT_P (args[i].reg))
1459 args[i].special_slot = args[i].reg;
1460 args[i].reg = NULL;
1463 /* If this is a sibling call and the machine has register windows, the
1464 register window has to be unwinded before calling the routine, so
1465 arguments have to go into the incoming registers. */
1466 if (targetm.calls.function_incoming_arg != targetm.calls.function_arg)
1467 args[i].tail_call_reg
1468 = targetm.calls.function_incoming_arg (args_so_far, mode, type,
1469 argpos < n_named_args);
1470 else
1471 args[i].tail_call_reg = args[i].reg;
1473 if (args[i].reg)
1474 args[i].partial
1475 = targetm.calls.arg_partial_bytes (args_so_far, mode, type,
1476 argpos < n_named_args);
1478 args[i].pass_on_stack = targetm.calls.must_pass_in_stack (mode, type);
1480 /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
1481 it means that we are to pass this arg in the register(s) designated
1482 by the PARALLEL, but also to pass it in the stack. */
1483 if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
1484 && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
1485 args[i].pass_on_stack = 1;
1487 /* If this is an addressable type, we must preallocate the stack
1488 since we must evaluate the object into its final location.
1490 If this is to be passed in both registers and the stack, it is simpler
1491 to preallocate. */
1492 if (TREE_ADDRESSABLE (type)
1493 || (args[i].pass_on_stack && args[i].reg != 0))
1494 *must_preallocate = 1;
1496 /* No stack allocation and padding for bounds. */
1497 if (POINTER_BOUNDS_P (args[i].tree_value))
1499 /* Compute the stack-size of this argument. */
1500 else if (args[i].reg == 0 || args[i].partial != 0
1501 || reg_parm_stack_space > 0
1502 || args[i].pass_on_stack)
1503 locate_and_pad_parm (mode, type,
1504 #ifdef STACK_PARMS_IN_REG_PARM_AREA
1506 #else
1507 args[i].reg != 0,
1508 #endif
1509 reg_parm_stack_space,
1510 args[i].pass_on_stack ? 0 : args[i].partial,
1511 fndecl, args_size, &args[i].locate);
1512 #ifdef BLOCK_REG_PADDING
1513 else
1514 /* The argument is passed entirely in registers. See at which
1515 end it should be padded. */
1516 args[i].locate.where_pad =
1517 BLOCK_REG_PADDING (mode, type,
1518 int_size_in_bytes (type) <= UNITS_PER_WORD);
1519 #endif
1521 /* Update ARGS_SIZE, the total stack space for args so far. */
1523 args_size->constant += args[i].locate.size.constant;
1524 if (args[i].locate.size.var)
1525 ADD_PARM_SIZE (*args_size, args[i].locate.size.var);
1527 /* Increment ARGS_SO_FAR, which has info about which arg-registers
1528 have been used, etc. */
1530 targetm.calls.function_arg_advance (args_so_far, TYPE_MODE (type),
1531 type, argpos < n_named_args);
1535 /* Update ARGS_SIZE to contain the total size for the argument block.
1536 Return the original constant component of the argument block's size.
1538 REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
1539 for arguments passed in registers. */
1541 static int
1542 compute_argument_block_size (int reg_parm_stack_space,
1543 struct args_size *args_size,
1544 tree fndecl ATTRIBUTE_UNUSED,
1545 tree fntype ATTRIBUTE_UNUSED,
1546 int preferred_stack_boundary ATTRIBUTE_UNUSED)
1548 int unadjusted_args_size = args_size->constant;
1550 /* For accumulate outgoing args mode we don't need to align, since the frame
1551 will be already aligned. Align to STACK_BOUNDARY in order to prevent
1552 backends from generating misaligned frame sizes. */
1553 if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
1554 preferred_stack_boundary = STACK_BOUNDARY;
1556 /* Compute the actual size of the argument block required. The variable
1557 and constant sizes must be combined, the size may have to be rounded,
1558 and there may be a minimum required size. */
1560 if (args_size->var)
1562 args_size->var = ARGS_SIZE_TREE (*args_size);
1563 args_size->constant = 0;
1565 preferred_stack_boundary /= BITS_PER_UNIT;
1566 if (preferred_stack_boundary > 1)
1568 /* We don't handle this case yet. To handle it correctly we have
1569 to add the delta, round and subtract the delta.
1570 Currently no machine description requires this support. */
1571 gcc_assert (!(stack_pointer_delta & (preferred_stack_boundary - 1)));
1572 args_size->var = round_up (args_size->var, preferred_stack_boundary);
1575 if (reg_parm_stack_space > 0)
1577 args_size->var
1578 = size_binop (MAX_EXPR, args_size->var,
1579 ssize_int (reg_parm_stack_space));
1581 /* The area corresponding to register parameters is not to count in
1582 the size of the block we need. So make the adjustment. */
1583 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1584 args_size->var
1585 = size_binop (MINUS_EXPR, args_size->var,
1586 ssize_int (reg_parm_stack_space));
1589 else
1591 preferred_stack_boundary /= BITS_PER_UNIT;
1592 if (preferred_stack_boundary < 1)
1593 preferred_stack_boundary = 1;
1594 args_size->constant = (((args_size->constant
1595 + stack_pointer_delta
1596 + preferred_stack_boundary - 1)
1597 / preferred_stack_boundary
1598 * preferred_stack_boundary)
1599 - stack_pointer_delta);
1601 args_size->constant = MAX (args_size->constant,
1602 reg_parm_stack_space);
1604 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1605 args_size->constant -= reg_parm_stack_space;
1607 return unadjusted_args_size;
1610 /* Precompute parameters as needed for a function call.
1612 FLAGS is mask of ECF_* constants.
1614 NUM_ACTUALS is the number of arguments.
1616 ARGS is an array containing information for each argument; this
1617 routine fills in the INITIAL_VALUE and VALUE fields for each
1618 precomputed argument. */
1620 static void
1621 precompute_arguments (int num_actuals, struct arg_data *args)
1623 int i;
1625 /* If this is a libcall, then precompute all arguments so that we do not
1626 get extraneous instructions emitted as part of the libcall sequence. */
1628 /* If we preallocated the stack space, and some arguments must be passed
1629 on the stack, then we must precompute any parameter which contains a
1630 function call which will store arguments on the stack.
1631 Otherwise, evaluating the parameter may clobber previous parameters
1632 which have already been stored into the stack. (we have code to avoid
1633 such case by saving the outgoing stack arguments, but it results in
1634 worse code) */
1635 if (!ACCUMULATE_OUTGOING_ARGS)
1636 return;
1638 for (i = 0; i < num_actuals; i++)
1640 tree type;
1641 machine_mode mode;
1643 if (TREE_CODE (args[i].tree_value) != CALL_EXPR)
1644 continue;
1646 /* If this is an addressable type, we cannot pre-evaluate it. */
1647 type = TREE_TYPE (args[i].tree_value);
1648 gcc_assert (!TREE_ADDRESSABLE (type));
1650 args[i].initial_value = args[i].value
1651 = expand_normal (args[i].tree_value);
1653 mode = TYPE_MODE (type);
1654 if (mode != args[i].mode)
1656 int unsignedp = args[i].unsignedp;
1657 args[i].value
1658 = convert_modes (args[i].mode, mode,
1659 args[i].value, args[i].unsignedp);
1661 /* CSE will replace this only if it contains args[i].value
1662 pseudo, so convert it down to the declared mode using
1663 a SUBREG. */
1664 if (REG_P (args[i].value)
1665 && GET_MODE_CLASS (args[i].mode) == MODE_INT
1666 && promote_mode (type, mode, &unsignedp) != args[i].mode)
1668 args[i].initial_value
1669 = gen_lowpart_SUBREG (mode, args[i].value);
1670 SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1671 SUBREG_PROMOTED_SET (args[i].initial_value, args[i].unsignedp);
1677 /* Given the current state of MUST_PREALLOCATE and information about
1678 arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
1679 compute and return the final value for MUST_PREALLOCATE. */
1681 static int
1682 finalize_must_preallocate (int must_preallocate, int num_actuals,
1683 struct arg_data *args, struct args_size *args_size)
1685 /* See if we have or want to preallocate stack space.
1687 If we would have to push a partially-in-regs parm
1688 before other stack parms, preallocate stack space instead.
1690 If the size of some parm is not a multiple of the required stack
1691 alignment, we must preallocate.
1693 If the total size of arguments that would otherwise create a copy in
1694 a temporary (such as a CALL) is more than half the total argument list
1695 size, preallocation is faster.
1697 Another reason to preallocate is if we have a machine (like the m88k)
1698 where stack alignment is required to be maintained between every
1699 pair of insns, not just when the call is made. However, we assume here
1700 that such machines either do not have push insns (and hence preallocation
1701 would occur anyway) or the problem is taken care of with
1702 PUSH_ROUNDING. */
1704 if (! must_preallocate)
1706 int partial_seen = 0;
1707 int copy_to_evaluate_size = 0;
1708 int i;
1710 for (i = 0; i < num_actuals && ! must_preallocate; i++)
1712 if (args[i].partial > 0 && ! args[i].pass_on_stack)
1713 partial_seen = 1;
1714 else if (partial_seen && args[i].reg == 0)
1715 must_preallocate = 1;
1716 /* We preallocate in case there are bounds passed
1717 in the bounds table to have precomputed address
1718 for bounds association. */
1719 else if (POINTER_BOUNDS_P (args[i].tree_value)
1720 && !args[i].reg)
1721 must_preallocate = 1;
1723 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1724 && (TREE_CODE (args[i].tree_value) == CALL_EXPR
1725 || TREE_CODE (args[i].tree_value) == TARGET_EXPR
1726 || TREE_CODE (args[i].tree_value) == COND_EXPR
1727 || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
1728 copy_to_evaluate_size
1729 += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1732 if (copy_to_evaluate_size * 2 >= args_size->constant
1733 && args_size->constant > 0)
1734 must_preallocate = 1;
1736 return must_preallocate;
1739 /* If we preallocated stack space, compute the address of each argument
1740 and store it into the ARGS array.
1742 We need not ensure it is a valid memory address here; it will be
1743 validized when it is used.
1745 ARGBLOCK is an rtx for the address of the outgoing arguments. */
1747 static void
1748 compute_argument_addresses (struct arg_data *args, rtx argblock, int num_actuals)
1750 if (argblock)
1752 rtx arg_reg = argblock;
1753 int i, arg_offset = 0;
1755 if (GET_CODE (argblock) == PLUS)
1756 arg_reg = XEXP (argblock, 0), arg_offset = INTVAL (XEXP (argblock, 1));
1758 for (i = 0; i < num_actuals; i++)
1760 rtx offset = ARGS_SIZE_RTX (args[i].locate.offset);
1761 rtx slot_offset = ARGS_SIZE_RTX (args[i].locate.slot_offset);
1762 rtx addr;
1763 unsigned int align, boundary;
1764 unsigned int units_on_stack = 0;
1765 machine_mode partial_mode = VOIDmode;
1767 /* Skip this parm if it will not be passed on the stack. */
1768 if (! args[i].pass_on_stack
1769 && args[i].reg != 0
1770 && args[i].partial == 0)
1771 continue;
1773 /* Pointer Bounds are never passed on the stack. */
1774 if (POINTER_BOUNDS_P (args[i].tree_value))
1775 continue;
1777 if (CONST_INT_P (offset))
1778 addr = plus_constant (Pmode, arg_reg, INTVAL (offset));
1779 else
1780 addr = gen_rtx_PLUS (Pmode, arg_reg, offset);
1782 addr = plus_constant (Pmode, addr, arg_offset);
1784 if (args[i].partial != 0)
1786 /* Only part of the parameter is being passed on the stack.
1787 Generate a simple memory reference of the correct size. */
1788 units_on_stack = args[i].locate.size.constant;
1789 partial_mode = mode_for_size (units_on_stack * BITS_PER_UNIT,
1790 MODE_INT, 1);
1791 args[i].stack = gen_rtx_MEM (partial_mode, addr);
1792 set_mem_size (args[i].stack, units_on_stack);
1794 else
1796 args[i].stack = gen_rtx_MEM (args[i].mode, addr);
1797 set_mem_attributes (args[i].stack,
1798 TREE_TYPE (args[i].tree_value), 1);
1800 align = BITS_PER_UNIT;
1801 boundary = args[i].locate.boundary;
1802 if (args[i].locate.where_pad != downward)
1803 align = boundary;
1804 else if (CONST_INT_P (offset))
1806 align = INTVAL (offset) * BITS_PER_UNIT | boundary;
1807 align = align & -align;
1809 set_mem_align (args[i].stack, align);
1811 if (CONST_INT_P (slot_offset))
1812 addr = plus_constant (Pmode, arg_reg, INTVAL (slot_offset));
1813 else
1814 addr = gen_rtx_PLUS (Pmode, arg_reg, slot_offset);
1816 addr = plus_constant (Pmode, addr, arg_offset);
1818 if (args[i].partial != 0)
1820 /* Only part of the parameter is being passed on the stack.
1821 Generate a simple memory reference of the correct size.
1823 args[i].stack_slot = gen_rtx_MEM (partial_mode, addr);
1824 set_mem_size (args[i].stack_slot, units_on_stack);
1826 else
1828 args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
1829 set_mem_attributes (args[i].stack_slot,
1830 TREE_TYPE (args[i].tree_value), 1);
1832 set_mem_align (args[i].stack_slot, args[i].locate.boundary);
1834 /* Function incoming arguments may overlap with sibling call
1835 outgoing arguments and we cannot allow reordering of reads
1836 from function arguments with stores to outgoing arguments
1837 of sibling calls. */
1838 set_mem_alias_set (args[i].stack, 0);
1839 set_mem_alias_set (args[i].stack_slot, 0);
1844 /* Given a FNDECL and EXP, return an rtx suitable for use as a target address
1845 in a call instruction.
1847 FNDECL is the tree node for the target function. For an indirect call
1848 FNDECL will be NULL_TREE.
1850 ADDR is the operand 0 of CALL_EXPR for this call. */
1852 static rtx
1853 rtx_for_function_call (tree fndecl, tree addr)
1855 rtx funexp;
1857 /* Get the function to call, in the form of RTL. */
1858 if (fndecl)
1860 if (!TREE_USED (fndecl) && fndecl != current_function_decl)
1861 TREE_USED (fndecl) = 1;
1863 /* Get a SYMBOL_REF rtx for the function address. */
1864 funexp = XEXP (DECL_RTL (fndecl), 0);
1866 else
1867 /* Generate an rtx (probably a pseudo-register) for the address. */
1869 push_temp_slots ();
1870 funexp = expand_normal (addr);
1871 pop_temp_slots (); /* FUNEXP can't be BLKmode. */
1873 return funexp;
1876 /* Internal state for internal_arg_pointer_based_exp and its helpers. */
1877 static struct
1879 /* Last insn that has been scanned by internal_arg_pointer_based_exp_scan,
1880 or NULL_RTX if none has been scanned yet. */
1881 rtx_insn *scan_start;
1882 /* Vector indexed by REGNO - FIRST_PSEUDO_REGISTER, recording if a pseudo is
1883 based on crtl->args.internal_arg_pointer. The element is NULL_RTX if the
1884 pseudo isn't based on it, a CONST_INT offset if the pseudo is based on it
1885 with fixed offset, or PC if this is with variable or unknown offset. */
1886 vec<rtx> cache;
1887 } internal_arg_pointer_exp_state;
1889 static rtx internal_arg_pointer_based_exp (const_rtx, bool);
1891 /* Helper function for internal_arg_pointer_based_exp. Scan insns in
1892 the tail call sequence, starting with first insn that hasn't been
1893 scanned yet, and note for each pseudo on the LHS whether it is based
1894 on crtl->args.internal_arg_pointer or not, and what offset from that
1895 that pointer it has. */
1897 static void
1898 internal_arg_pointer_based_exp_scan (void)
1900 rtx_insn *insn, *scan_start = internal_arg_pointer_exp_state.scan_start;
1902 if (scan_start == NULL_RTX)
1903 insn = get_insns ();
1904 else
1905 insn = NEXT_INSN (scan_start);
1907 while (insn)
1909 rtx set = single_set (insn);
1910 if (set && REG_P (SET_DEST (set)) && !HARD_REGISTER_P (SET_DEST (set)))
1912 rtx val = NULL_RTX;
1913 unsigned int idx = REGNO (SET_DEST (set)) - FIRST_PSEUDO_REGISTER;
1914 /* Punt on pseudos set multiple times. */
1915 if (idx < internal_arg_pointer_exp_state.cache.length ()
1916 && (internal_arg_pointer_exp_state.cache[idx]
1917 != NULL_RTX))
1918 val = pc_rtx;
1919 else
1920 val = internal_arg_pointer_based_exp (SET_SRC (set), false);
1921 if (val != NULL_RTX)
1923 if (idx >= internal_arg_pointer_exp_state.cache.length ())
1924 internal_arg_pointer_exp_state.cache
1925 .safe_grow_cleared (idx + 1);
1926 internal_arg_pointer_exp_state.cache[idx] = val;
1929 if (NEXT_INSN (insn) == NULL_RTX)
1930 scan_start = insn;
1931 insn = NEXT_INSN (insn);
1934 internal_arg_pointer_exp_state.scan_start = scan_start;
1937 /* Compute whether RTL is based on crtl->args.internal_arg_pointer. Return
1938 NULL_RTX if RTL isn't based on it, a CONST_INT offset if RTL is based on
1939 it with fixed offset, or PC if this is with variable or unknown offset.
1940 TOPLEVEL is true if the function is invoked at the topmost level. */
1942 static rtx
1943 internal_arg_pointer_based_exp (const_rtx rtl, bool toplevel)
1945 if (CONSTANT_P (rtl))
1946 return NULL_RTX;
1948 if (rtl == crtl->args.internal_arg_pointer)
1949 return const0_rtx;
1951 if (REG_P (rtl) && HARD_REGISTER_P (rtl))
1952 return NULL_RTX;
1954 if (GET_CODE (rtl) == PLUS && CONST_INT_P (XEXP (rtl, 1)))
1956 rtx val = internal_arg_pointer_based_exp (XEXP (rtl, 0), toplevel);
1957 if (val == NULL_RTX || val == pc_rtx)
1958 return val;
1959 return plus_constant (Pmode, val, INTVAL (XEXP (rtl, 1)));
1962 /* When called at the topmost level, scan pseudo assignments in between the
1963 last scanned instruction in the tail call sequence and the latest insn
1964 in that sequence. */
1965 if (toplevel)
1966 internal_arg_pointer_based_exp_scan ();
1968 if (REG_P (rtl))
1970 unsigned int idx = REGNO (rtl) - FIRST_PSEUDO_REGISTER;
1971 if (idx < internal_arg_pointer_exp_state.cache.length ())
1972 return internal_arg_pointer_exp_state.cache[idx];
1974 return NULL_RTX;
1977 subrtx_iterator::array_type array;
1978 FOR_EACH_SUBRTX (iter, array, rtl, NONCONST)
1980 const_rtx x = *iter;
1981 if (REG_P (x) && internal_arg_pointer_based_exp (x, false) != NULL_RTX)
1982 return pc_rtx;
1983 if (MEM_P (x))
1984 iter.skip_subrtxes ();
1987 return NULL_RTX;
1990 /* Return true if and only if SIZE storage units (usually bytes)
1991 starting from address ADDR overlap with already clobbered argument
1992 area. This function is used to determine if we should give up a
1993 sibcall. */
1995 static bool
1996 mem_overlaps_already_clobbered_arg_p (rtx addr, unsigned HOST_WIDE_INT size)
1998 HOST_WIDE_INT i;
1999 rtx val;
2001 if (bitmap_empty_p (stored_args_map))
2002 return false;
2003 val = internal_arg_pointer_based_exp (addr, true);
2004 if (val == NULL_RTX)
2005 return false;
2006 else if (val == pc_rtx)
2007 return true;
2008 else
2009 i = INTVAL (val);
2011 if (STACK_GROWS_DOWNWARD)
2012 i -= crtl->args.pretend_args_size;
2013 else
2014 i += crtl->args.pretend_args_size;
2017 if (ARGS_GROW_DOWNWARD)
2018 i = -i - size;
2020 if (size > 0)
2022 unsigned HOST_WIDE_INT k;
2024 for (k = 0; k < size; k++)
2025 if (i + k < SBITMAP_SIZE (stored_args_map)
2026 && bitmap_bit_p (stored_args_map, i + k))
2027 return true;
2030 return false;
2033 /* Do the register loads required for any wholly-register parms or any
2034 parms which are passed both on the stack and in a register. Their
2035 expressions were already evaluated.
2037 Mark all register-parms as living through the call, putting these USE
2038 insns in the CALL_INSN_FUNCTION_USAGE field.
2040 When IS_SIBCALL, perform the check_sibcall_argument_overlap
2041 checking, setting *SIBCALL_FAILURE if appropriate. */
2043 static void
2044 load_register_parameters (struct arg_data *args, int num_actuals,
2045 rtx *call_fusage, int flags, int is_sibcall,
2046 int *sibcall_failure)
2048 int i, j;
2050 for (i = 0; i < num_actuals; i++)
2052 rtx reg = ((flags & ECF_SIBCALL)
2053 ? args[i].tail_call_reg : args[i].reg);
2054 if (reg)
2056 int partial = args[i].partial;
2057 int nregs;
2058 int size = 0;
2059 rtx_insn *before_arg = get_last_insn ();
2060 /* Set non-negative if we must move a word at a time, even if
2061 just one word (e.g, partial == 4 && mode == DFmode). Set
2062 to -1 if we just use a normal move insn. This value can be
2063 zero if the argument is a zero size structure. */
2064 nregs = -1;
2065 if (GET_CODE (reg) == PARALLEL)
2067 else if (partial)
2069 gcc_assert (partial % UNITS_PER_WORD == 0);
2070 nregs = partial / UNITS_PER_WORD;
2072 else if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode)
2074 size = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
2075 nregs = (size + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
2077 else
2078 size = GET_MODE_SIZE (args[i].mode);
2080 /* Handle calls that pass values in multiple non-contiguous
2081 locations. The Irix 6 ABI has examples of this. */
2083 if (GET_CODE (reg) == PARALLEL)
2084 emit_group_move (reg, args[i].parallel_value);
2086 /* If simple case, just do move. If normal partial, store_one_arg
2087 has already loaded the register for us. In all other cases,
2088 load the register(s) from memory. */
2090 else if (nregs == -1)
2092 emit_move_insn (reg, args[i].value);
2093 #ifdef BLOCK_REG_PADDING
2094 /* Handle case where we have a value that needs shifting
2095 up to the msb. eg. a QImode value and we're padding
2096 upward on a BYTES_BIG_ENDIAN machine. */
2097 if (size < UNITS_PER_WORD
2098 && (args[i].locate.where_pad
2099 == (BYTES_BIG_ENDIAN ? upward : downward)))
2101 rtx x;
2102 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2104 /* Assigning REG here rather than a temp makes CALL_FUSAGE
2105 report the whole reg as used. Strictly speaking, the
2106 call only uses SIZE bytes at the msb end, but it doesn't
2107 seem worth generating rtl to say that. */
2108 reg = gen_rtx_REG (word_mode, REGNO (reg));
2109 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
2110 if (x != reg)
2111 emit_move_insn (reg, x);
2113 #endif
2116 /* If we have pre-computed the values to put in the registers in
2117 the case of non-aligned structures, copy them in now. */
2119 else if (args[i].n_aligned_regs != 0)
2120 for (j = 0; j < args[i].n_aligned_regs; j++)
2121 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
2122 args[i].aligned_regs[j]);
2124 else if (partial == 0 || args[i].pass_on_stack)
2126 rtx mem = validize_mem (copy_rtx (args[i].value));
2128 /* Check for overlap with already clobbered argument area,
2129 providing that this has non-zero size. */
2130 if (is_sibcall
2131 && (size == 0
2132 || mem_overlaps_already_clobbered_arg_p
2133 (XEXP (args[i].value, 0), size)))
2134 *sibcall_failure = 1;
2136 if (size % UNITS_PER_WORD == 0
2137 || MEM_ALIGN (mem) % BITS_PER_WORD == 0)
2138 move_block_to_reg (REGNO (reg), mem, nregs, args[i].mode);
2139 else
2141 if (nregs > 1)
2142 move_block_to_reg (REGNO (reg), mem, nregs - 1,
2143 args[i].mode);
2144 rtx dest = gen_rtx_REG (word_mode, REGNO (reg) + nregs - 1);
2145 unsigned int bitoff = (nregs - 1) * BITS_PER_WORD;
2146 unsigned int bitsize = size * BITS_PER_UNIT - bitoff;
2147 rtx x = extract_bit_field (mem, bitsize, bitoff, 1,
2148 dest, word_mode, word_mode);
2149 if (BYTES_BIG_ENDIAN)
2150 x = expand_shift (LSHIFT_EXPR, word_mode, x,
2151 BITS_PER_WORD - bitsize, dest, 1);
2152 if (x != dest)
2153 emit_move_insn (dest, x);
2156 /* Handle a BLKmode that needs shifting. */
2157 if (nregs == 1 && size < UNITS_PER_WORD
2158 #ifdef BLOCK_REG_PADDING
2159 && args[i].locate.where_pad == downward
2160 #else
2161 && BYTES_BIG_ENDIAN
2162 #endif
2165 rtx dest = gen_rtx_REG (word_mode, REGNO (reg));
2166 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2167 enum tree_code dir = (BYTES_BIG_ENDIAN
2168 ? RSHIFT_EXPR : LSHIFT_EXPR);
2169 rtx x;
2171 x = expand_shift (dir, word_mode, dest, shift, dest, 1);
2172 if (x != dest)
2173 emit_move_insn (dest, x);
2177 /* When a parameter is a block, and perhaps in other cases, it is
2178 possible that it did a load from an argument slot that was
2179 already clobbered. */
2180 if (is_sibcall
2181 && check_sibcall_argument_overlap (before_arg, &args[i], 0))
2182 *sibcall_failure = 1;
2184 /* Handle calls that pass values in multiple non-contiguous
2185 locations. The Irix 6 ABI has examples of this. */
2186 if (GET_CODE (reg) == PARALLEL)
2187 use_group_regs (call_fusage, reg);
2188 else if (nregs == -1)
2189 use_reg_mode (call_fusage, reg,
2190 TYPE_MODE (TREE_TYPE (args[i].tree_value)));
2191 else if (nregs > 0)
2192 use_regs (call_fusage, REGNO (reg), nregs);
2197 /* We need to pop PENDING_STACK_ADJUST bytes. But, if the arguments
2198 wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
2199 bytes, then we would need to push some additional bytes to pad the
2200 arguments. So, we compute an adjust to the stack pointer for an
2201 amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
2202 bytes. Then, when the arguments are pushed the stack will be perfectly
2203 aligned. ARGS_SIZE->CONSTANT is set to the number of bytes that should
2204 be popped after the call. Returns the adjustment. */
2206 static int
2207 combine_pending_stack_adjustment_and_call (int unadjusted_args_size,
2208 struct args_size *args_size,
2209 unsigned int preferred_unit_stack_boundary)
2211 /* The number of bytes to pop so that the stack will be
2212 under-aligned by UNADJUSTED_ARGS_SIZE bytes. */
2213 HOST_WIDE_INT adjustment;
2214 /* The alignment of the stack after the arguments are pushed, if we
2215 just pushed the arguments without adjust the stack here. */
2216 unsigned HOST_WIDE_INT unadjusted_alignment;
2218 unadjusted_alignment
2219 = ((stack_pointer_delta + unadjusted_args_size)
2220 % preferred_unit_stack_boundary);
2222 /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
2223 as possible -- leaving just enough left to cancel out the
2224 UNADJUSTED_ALIGNMENT. In other words, we want to ensure that the
2225 PENDING_STACK_ADJUST is non-negative, and congruent to
2226 -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY. */
2228 /* Begin by trying to pop all the bytes. */
2229 unadjusted_alignment
2230 = (unadjusted_alignment
2231 - (pending_stack_adjust % preferred_unit_stack_boundary));
2232 adjustment = pending_stack_adjust;
2233 /* Push enough additional bytes that the stack will be aligned
2234 after the arguments are pushed. */
2235 if (preferred_unit_stack_boundary > 1)
2237 if (unadjusted_alignment > 0)
2238 adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
2239 else
2240 adjustment += unadjusted_alignment;
2243 /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
2244 bytes after the call. The right number is the entire
2245 PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
2246 by the arguments in the first place. */
2247 args_size->constant
2248 = pending_stack_adjust - adjustment + unadjusted_args_size;
2250 return adjustment;
2253 /* Scan X expression if it does not dereference any argument slots
2254 we already clobbered by tail call arguments (as noted in stored_args_map
2255 bitmap).
2256 Return nonzero if X expression dereferences such argument slots,
2257 zero otherwise. */
2259 static int
2260 check_sibcall_argument_overlap_1 (rtx x)
2262 RTX_CODE code;
2263 int i, j;
2264 const char *fmt;
2266 if (x == NULL_RTX)
2267 return 0;
2269 code = GET_CODE (x);
2271 /* We need not check the operands of the CALL expression itself. */
2272 if (code == CALL)
2273 return 0;
2275 if (code == MEM)
2276 return mem_overlaps_already_clobbered_arg_p (XEXP (x, 0),
2277 GET_MODE_SIZE (GET_MODE (x)));
2279 /* Scan all subexpressions. */
2280 fmt = GET_RTX_FORMAT (code);
2281 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2283 if (*fmt == 'e')
2285 if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
2286 return 1;
2288 else if (*fmt == 'E')
2290 for (j = 0; j < XVECLEN (x, i); j++)
2291 if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
2292 return 1;
2295 return 0;
2298 /* Scan sequence after INSN if it does not dereference any argument slots
2299 we already clobbered by tail call arguments (as noted in stored_args_map
2300 bitmap). If MARK_STORED_ARGS_MAP, add stack slots for ARG to
2301 stored_args_map bitmap afterwards (when ARG is a register MARK_STORED_ARGS_MAP
2302 should be 0). Return nonzero if sequence after INSN dereferences such argument
2303 slots, zero otherwise. */
2305 static int
2306 check_sibcall_argument_overlap (rtx_insn *insn, struct arg_data *arg,
2307 int mark_stored_args_map)
2309 int low, high;
2311 if (insn == NULL_RTX)
2312 insn = get_insns ();
2313 else
2314 insn = NEXT_INSN (insn);
2316 for (; insn; insn = NEXT_INSN (insn))
2317 if (INSN_P (insn)
2318 && check_sibcall_argument_overlap_1 (PATTERN (insn)))
2319 break;
2321 if (mark_stored_args_map)
2323 if (ARGS_GROW_DOWNWARD)
2324 low = -arg->locate.slot_offset.constant - arg->locate.size.constant;
2325 else
2326 low = arg->locate.slot_offset.constant;
2328 for (high = low + arg->locate.size.constant; low < high; low++)
2329 bitmap_set_bit (stored_args_map, low);
2331 return insn != NULL_RTX;
2334 /* Given that a function returns a value of mode MODE at the most
2335 significant end of hard register VALUE, shift VALUE left or right
2336 as specified by LEFT_P. Return true if some action was needed. */
2338 bool
2339 shift_return_value (machine_mode mode, bool left_p, rtx value)
2341 HOST_WIDE_INT shift;
2343 gcc_assert (REG_P (value) && HARD_REGISTER_P (value));
2344 shift = GET_MODE_BITSIZE (GET_MODE (value)) - GET_MODE_BITSIZE (mode);
2345 if (shift == 0)
2346 return false;
2348 /* Use ashr rather than lshr for right shifts. This is for the benefit
2349 of the MIPS port, which requires SImode values to be sign-extended
2350 when stored in 64-bit registers. */
2351 if (!force_expand_binop (GET_MODE (value), left_p ? ashl_optab : ashr_optab,
2352 value, GEN_INT (shift), value, 1, OPTAB_WIDEN))
2353 gcc_unreachable ();
2354 return true;
2357 /* If X is a likely-spilled register value, copy it to a pseudo
2358 register and return that register. Return X otherwise. */
2360 static rtx
2361 avoid_likely_spilled_reg (rtx x)
2363 rtx new_rtx;
2365 if (REG_P (x)
2366 && HARD_REGISTER_P (x)
2367 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (x))))
2369 /* Make sure that we generate a REG rather than a CONCAT.
2370 Moves into CONCATs can need nontrivial instructions,
2371 and the whole point of this function is to avoid
2372 using the hard register directly in such a situation. */
2373 generating_concat_p = 0;
2374 new_rtx = gen_reg_rtx (GET_MODE (x));
2375 generating_concat_p = 1;
2376 emit_move_insn (new_rtx, x);
2377 return new_rtx;
2379 return x;
2382 /* Generate all the code for a CALL_EXPR exp
2383 and return an rtx for its value.
2384 Store the value in TARGET (specified as an rtx) if convenient.
2385 If the value is stored in TARGET then TARGET is returned.
2386 If IGNORE is nonzero, then we ignore the value of the function call. */
2389 expand_call (tree exp, rtx target, int ignore)
2391 /* Nonzero if we are currently expanding a call. */
2392 static int currently_expanding_call = 0;
2394 /* RTX for the function to be called. */
2395 rtx funexp;
2396 /* Sequence of insns to perform a normal "call". */
2397 rtx_insn *normal_call_insns = NULL;
2398 /* Sequence of insns to perform a tail "call". */
2399 rtx_insn *tail_call_insns = NULL;
2400 /* Data type of the function. */
2401 tree funtype;
2402 tree type_arg_types;
2403 tree rettype;
2404 /* Declaration of the function being called,
2405 or 0 if the function is computed (not known by name). */
2406 tree fndecl = 0;
2407 /* The type of the function being called. */
2408 tree fntype;
2409 bool try_tail_call = CALL_EXPR_TAILCALL (exp);
2410 int pass;
2412 /* Register in which non-BLKmode value will be returned,
2413 or 0 if no value or if value is BLKmode. */
2414 rtx valreg;
2415 /* Register(s) in which bounds are returned. */
2416 rtx valbnd = NULL;
2417 /* Address where we should return a BLKmode value;
2418 0 if value not BLKmode. */
2419 rtx structure_value_addr = 0;
2420 /* Nonzero if that address is being passed by treating it as
2421 an extra, implicit first parameter. Otherwise,
2422 it is passed by being copied directly into struct_value_rtx. */
2423 int structure_value_addr_parm = 0;
2424 /* Holds the value of implicit argument for the struct value. */
2425 tree structure_value_addr_value = NULL_TREE;
2426 /* Size of aggregate value wanted, or zero if none wanted
2427 or if we are using the non-reentrant PCC calling convention
2428 or expecting the value in registers. */
2429 HOST_WIDE_INT struct_value_size = 0;
2430 /* Nonzero if called function returns an aggregate in memory PCC style,
2431 by returning the address of where to find it. */
2432 int pcc_struct_value = 0;
2433 rtx struct_value = 0;
2435 /* Number of actual parameters in this call, including struct value addr. */
2436 int num_actuals;
2437 /* Number of named args. Args after this are anonymous ones
2438 and they must all go on the stack. */
2439 int n_named_args;
2440 /* Number of complex actual arguments that need to be split. */
2441 int num_complex_actuals = 0;
2443 /* Vector of information about each argument.
2444 Arguments are numbered in the order they will be pushed,
2445 not the order they are written. */
2446 struct arg_data *args;
2448 /* Total size in bytes of all the stack-parms scanned so far. */
2449 struct args_size args_size;
2450 struct args_size adjusted_args_size;
2451 /* Size of arguments before any adjustments (such as rounding). */
2452 int unadjusted_args_size;
2453 /* Data on reg parms scanned so far. */
2454 CUMULATIVE_ARGS args_so_far_v;
2455 cumulative_args_t args_so_far;
2456 /* Nonzero if a reg parm has been scanned. */
2457 int reg_parm_seen;
2458 /* Nonzero if this is an indirect function call. */
2460 /* Nonzero if we must avoid push-insns in the args for this call.
2461 If stack space is allocated for register parameters, but not by the
2462 caller, then it is preallocated in the fixed part of the stack frame.
2463 So the entire argument block must then be preallocated (i.e., we
2464 ignore PUSH_ROUNDING in that case). */
2466 int must_preallocate = !PUSH_ARGS;
2468 /* Size of the stack reserved for parameter registers. */
2469 int reg_parm_stack_space = 0;
2471 /* Address of space preallocated for stack parms
2472 (on machines that lack push insns), or 0 if space not preallocated. */
2473 rtx argblock = 0;
2475 /* Mask of ECF_ and ERF_ flags. */
2476 int flags = 0;
2477 int return_flags = 0;
2478 #ifdef REG_PARM_STACK_SPACE
2479 /* Define the boundary of the register parm stack space that needs to be
2480 saved, if any. */
2481 int low_to_save, high_to_save;
2482 rtx save_area = 0; /* Place that it is saved */
2483 #endif
2485 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
2486 char *initial_stack_usage_map = stack_usage_map;
2487 char *stack_usage_map_buf = NULL;
2489 int old_stack_allocated;
2491 /* State variables to track stack modifications. */
2492 rtx old_stack_level = 0;
2493 int old_stack_arg_under_construction = 0;
2494 int old_pending_adj = 0;
2495 int old_inhibit_defer_pop = inhibit_defer_pop;
2497 /* Some stack pointer alterations we make are performed via
2498 allocate_dynamic_stack_space. This modifies the stack_pointer_delta,
2499 which we then also need to save/restore along the way. */
2500 int old_stack_pointer_delta = 0;
2502 rtx call_fusage;
2503 tree addr = CALL_EXPR_FN (exp);
2504 int i;
2505 /* The alignment of the stack, in bits. */
2506 unsigned HOST_WIDE_INT preferred_stack_boundary;
2507 /* The alignment of the stack, in bytes. */
2508 unsigned HOST_WIDE_INT preferred_unit_stack_boundary;
2509 /* The static chain value to use for this call. */
2510 rtx static_chain_value;
2511 /* See if this is "nothrow" function call. */
2512 if (TREE_NOTHROW (exp))
2513 flags |= ECF_NOTHROW;
2515 /* See if we can find a DECL-node for the actual function, and get the
2516 function attributes (flags) from the function decl or type node. */
2517 fndecl = get_callee_fndecl (exp);
2518 if (fndecl)
2520 fntype = TREE_TYPE (fndecl);
2521 flags |= flags_from_decl_or_type (fndecl);
2522 return_flags |= decl_return_flags (fndecl);
2524 else
2526 fntype = TREE_TYPE (TREE_TYPE (addr));
2527 flags |= flags_from_decl_or_type (fntype);
2529 rettype = TREE_TYPE (exp);
2531 struct_value = targetm.calls.struct_value_rtx (fntype, 0);
2533 /* Warn if this value is an aggregate type,
2534 regardless of which calling convention we are using for it. */
2535 if (AGGREGATE_TYPE_P (rettype))
2536 warning (OPT_Waggregate_return, "function call has aggregate value");
2538 /* If the result of a non looping pure or const function call is
2539 ignored (or void), and none of its arguments are volatile, we can
2540 avoid expanding the call and just evaluate the arguments for
2541 side-effects. */
2542 if ((flags & (ECF_CONST | ECF_PURE))
2543 && (!(flags & ECF_LOOPING_CONST_OR_PURE))
2544 && (ignore || target == const0_rtx
2545 || TYPE_MODE (rettype) == VOIDmode))
2547 bool volatilep = false;
2548 tree arg;
2549 call_expr_arg_iterator iter;
2551 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2552 if (TREE_THIS_VOLATILE (arg))
2554 volatilep = true;
2555 break;
2558 if (! volatilep)
2560 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2561 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
2562 return const0_rtx;
2566 #ifdef REG_PARM_STACK_SPACE
2567 reg_parm_stack_space = REG_PARM_STACK_SPACE (!fndecl ? fntype : fndecl);
2568 #endif
2570 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
2571 && reg_parm_stack_space > 0 && PUSH_ARGS)
2572 must_preallocate = 1;
2574 /* Set up a place to return a structure. */
2576 /* Cater to broken compilers. */
2577 if (aggregate_value_p (exp, fntype))
2579 /* This call returns a big structure. */
2580 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
2582 #ifdef PCC_STATIC_STRUCT_RETURN
2584 pcc_struct_value = 1;
2586 #else /* not PCC_STATIC_STRUCT_RETURN */
2588 struct_value_size = int_size_in_bytes (rettype);
2590 /* Even if it is semantically safe to use the target as the return
2591 slot, it may be not sufficiently aligned for the return type. */
2592 if (CALL_EXPR_RETURN_SLOT_OPT (exp)
2593 && target
2594 && MEM_P (target)
2595 && !(MEM_ALIGN (target) < TYPE_ALIGN (rettype)
2596 && SLOW_UNALIGNED_ACCESS (TYPE_MODE (rettype),
2597 MEM_ALIGN (target))))
2598 structure_value_addr = XEXP (target, 0);
2599 else
2601 /* For variable-sized objects, we must be called with a target
2602 specified. If we were to allocate space on the stack here,
2603 we would have no way of knowing when to free it. */
2604 rtx d = assign_temp (rettype, 1, 1);
2605 structure_value_addr = XEXP (d, 0);
2606 target = 0;
2609 #endif /* not PCC_STATIC_STRUCT_RETURN */
2612 /* Figure out the amount to which the stack should be aligned. */
2613 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
2614 if (fndecl)
2616 struct cgraph_rtl_info *i = cgraph_node::rtl_info (fndecl);
2617 /* Without automatic stack alignment, we can't increase preferred
2618 stack boundary. With automatic stack alignment, it is
2619 unnecessary since unless we can guarantee that all callers will
2620 align the outgoing stack properly, callee has to align its
2621 stack anyway. */
2622 if (i
2623 && i->preferred_incoming_stack_boundary
2624 && i->preferred_incoming_stack_boundary < preferred_stack_boundary)
2625 preferred_stack_boundary = i->preferred_incoming_stack_boundary;
2628 /* Operand 0 is a pointer-to-function; get the type of the function. */
2629 funtype = TREE_TYPE (addr);
2630 gcc_assert (POINTER_TYPE_P (funtype));
2631 funtype = TREE_TYPE (funtype);
2633 /* Count whether there are actual complex arguments that need to be split
2634 into their real and imaginary parts. Munge the type_arg_types
2635 appropriately here as well. */
2636 if (targetm.calls.split_complex_arg)
2638 call_expr_arg_iterator iter;
2639 tree arg;
2640 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2642 tree type = TREE_TYPE (arg);
2643 if (type && TREE_CODE (type) == COMPLEX_TYPE
2644 && targetm.calls.split_complex_arg (type))
2645 num_complex_actuals++;
2647 type_arg_types = split_complex_types (TYPE_ARG_TYPES (funtype));
2649 else
2650 type_arg_types = TYPE_ARG_TYPES (funtype);
2652 if (flags & ECF_MAY_BE_ALLOCA)
2653 cfun->calls_alloca = 1;
2655 /* If struct_value_rtx is 0, it means pass the address
2656 as if it were an extra parameter. Put the argument expression
2657 in structure_value_addr_value. */
2658 if (structure_value_addr && struct_value == 0)
2660 /* If structure_value_addr is a REG other than
2661 virtual_outgoing_args_rtx, we can use always use it. If it
2662 is not a REG, we must always copy it into a register.
2663 If it is virtual_outgoing_args_rtx, we must copy it to another
2664 register in some cases. */
2665 rtx temp = (!REG_P (structure_value_addr)
2666 || (ACCUMULATE_OUTGOING_ARGS
2667 && stack_arg_under_construction
2668 && structure_value_addr == virtual_outgoing_args_rtx)
2669 ? copy_addr_to_reg (convert_memory_address
2670 (Pmode, structure_value_addr))
2671 : structure_value_addr);
2673 structure_value_addr_value =
2674 make_tree (build_pointer_type (TREE_TYPE (funtype)), temp);
2675 structure_value_addr_parm = CALL_WITH_BOUNDS_P (exp) ? 2 : 1;
2678 /* Count the arguments and set NUM_ACTUALS. */
2679 num_actuals =
2680 call_expr_nargs (exp) + num_complex_actuals + structure_value_addr_parm;
2682 /* Compute number of named args.
2683 First, do a raw count of the args for INIT_CUMULATIVE_ARGS. */
2685 if (type_arg_types != 0)
2686 n_named_args
2687 = (list_length (type_arg_types)
2688 /* Count the struct value address, if it is passed as a parm. */
2689 + structure_value_addr_parm);
2690 else
2691 /* If we know nothing, treat all args as named. */
2692 n_named_args = num_actuals;
2694 /* Start updating where the next arg would go.
2696 On some machines (such as the PA) indirect calls have a different
2697 calling convention than normal calls. The fourth argument in
2698 INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
2699 or not. */
2700 INIT_CUMULATIVE_ARGS (args_so_far_v, funtype, NULL_RTX, fndecl, n_named_args);
2701 args_so_far = pack_cumulative_args (&args_so_far_v);
2703 /* Now possibly adjust the number of named args.
2704 Normally, don't include the last named arg if anonymous args follow.
2705 We do include the last named arg if
2706 targetm.calls.strict_argument_naming() returns nonzero.
2707 (If no anonymous args follow, the result of list_length is actually
2708 one too large. This is harmless.)
2710 If targetm.calls.pretend_outgoing_varargs_named() returns
2711 nonzero, and targetm.calls.strict_argument_naming() returns zero,
2712 this machine will be able to place unnamed args that were passed
2713 in registers into the stack. So treat all args as named. This
2714 allows the insns emitting for a specific argument list to be
2715 independent of the function declaration.
2717 If targetm.calls.pretend_outgoing_varargs_named() returns zero,
2718 we do not have any reliable way to pass unnamed args in
2719 registers, so we must force them into memory. */
2721 if (type_arg_types != 0
2722 && targetm.calls.strict_argument_naming (args_so_far))
2724 else if (type_arg_types != 0
2725 && ! targetm.calls.pretend_outgoing_varargs_named (args_so_far))
2726 /* Don't include the last named arg. */
2727 --n_named_args;
2728 else
2729 /* Treat all args as named. */
2730 n_named_args = num_actuals;
2732 /* Make a vector to hold all the information about each arg. */
2733 args = XALLOCAVEC (struct arg_data, num_actuals);
2734 memset (args, 0, num_actuals * sizeof (struct arg_data));
2736 /* Build up entries in the ARGS array, compute the size of the
2737 arguments into ARGS_SIZE, etc. */
2738 initialize_argument_information (num_actuals, args, &args_size,
2739 n_named_args, exp,
2740 structure_value_addr_value, fndecl, fntype,
2741 args_so_far, reg_parm_stack_space,
2742 &old_stack_level, &old_pending_adj,
2743 &must_preallocate, &flags,
2744 &try_tail_call, CALL_FROM_THUNK_P (exp));
2746 if (args_size.var)
2747 must_preallocate = 1;
2749 /* Now make final decision about preallocating stack space. */
2750 must_preallocate = finalize_must_preallocate (must_preallocate,
2751 num_actuals, args,
2752 &args_size);
2754 /* If the structure value address will reference the stack pointer, we
2755 must stabilize it. We don't need to do this if we know that we are
2756 not going to adjust the stack pointer in processing this call. */
2758 if (structure_value_addr
2759 && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
2760 || reg_mentioned_p (virtual_outgoing_args_rtx,
2761 structure_value_addr))
2762 && (args_size.var
2763 || (!ACCUMULATE_OUTGOING_ARGS && args_size.constant)))
2764 structure_value_addr = copy_to_reg (structure_value_addr);
2766 /* Tail calls can make things harder to debug, and we've traditionally
2767 pushed these optimizations into -O2. Don't try if we're already
2768 expanding a call, as that means we're an argument. Don't try if
2769 there's cleanups, as we know there's code to follow the call. */
2771 if (currently_expanding_call++ != 0
2772 || !flag_optimize_sibling_calls
2773 || args_size.var
2774 || dbg_cnt (tail_call) == false)
2775 try_tail_call = 0;
2777 /* Rest of purposes for tail call optimizations to fail. */
2778 if (!try_tail_call
2779 || !targetm.have_sibcall_epilogue ()
2780 /* Doing sibling call optimization needs some work, since
2781 structure_value_addr can be allocated on the stack.
2782 It does not seem worth the effort since few optimizable
2783 sibling calls will return a structure. */
2784 || structure_value_addr != NULL_RTX
2785 #ifdef REG_PARM_STACK_SPACE
2786 /* If outgoing reg parm stack space changes, we can not do sibcall. */
2787 || (OUTGOING_REG_PARM_STACK_SPACE (funtype)
2788 != OUTGOING_REG_PARM_STACK_SPACE (TREE_TYPE (current_function_decl)))
2789 || (reg_parm_stack_space != REG_PARM_STACK_SPACE (current_function_decl))
2790 #endif
2791 /* Check whether the target is able to optimize the call
2792 into a sibcall. */
2793 || !targetm.function_ok_for_sibcall (fndecl, exp)
2794 /* Functions that do not return exactly once may not be sibcall
2795 optimized. */
2796 || (flags & (ECF_RETURNS_TWICE | ECF_NORETURN))
2797 || TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (addr)))
2798 /* If the called function is nested in the current one, it might access
2799 some of the caller's arguments, but could clobber them beforehand if
2800 the argument areas are shared. */
2801 || (fndecl && decl_function_context (fndecl) == current_function_decl)
2802 /* If this function requires more stack slots than the current
2803 function, we cannot change it into a sibling call.
2804 crtl->args.pretend_args_size is not part of the
2805 stack allocated by our caller. */
2806 || args_size.constant > (crtl->args.size
2807 - crtl->args.pretend_args_size)
2808 /* If the callee pops its own arguments, then it must pop exactly
2809 the same number of arguments as the current function. */
2810 || (targetm.calls.return_pops_args (fndecl, funtype, args_size.constant)
2811 != targetm.calls.return_pops_args (current_function_decl,
2812 TREE_TYPE (current_function_decl),
2813 crtl->args.size))
2814 || !lang_hooks.decls.ok_for_sibcall (fndecl))
2815 try_tail_call = 0;
2817 /* Check if caller and callee disagree in promotion of function
2818 return value. */
2819 if (try_tail_call)
2821 machine_mode caller_mode, caller_promoted_mode;
2822 machine_mode callee_mode, callee_promoted_mode;
2823 int caller_unsignedp, callee_unsignedp;
2824 tree caller_res = DECL_RESULT (current_function_decl);
2826 caller_unsignedp = TYPE_UNSIGNED (TREE_TYPE (caller_res));
2827 caller_mode = DECL_MODE (caller_res);
2828 callee_unsignedp = TYPE_UNSIGNED (TREE_TYPE (funtype));
2829 callee_mode = TYPE_MODE (TREE_TYPE (funtype));
2830 caller_promoted_mode
2831 = promote_function_mode (TREE_TYPE (caller_res), caller_mode,
2832 &caller_unsignedp,
2833 TREE_TYPE (current_function_decl), 1);
2834 callee_promoted_mode
2835 = promote_function_mode (TREE_TYPE (funtype), callee_mode,
2836 &callee_unsignedp,
2837 funtype, 1);
2838 if (caller_mode != VOIDmode
2839 && (caller_promoted_mode != callee_promoted_mode
2840 || ((caller_mode != caller_promoted_mode
2841 || callee_mode != callee_promoted_mode)
2842 && (caller_unsignedp != callee_unsignedp
2843 || GET_MODE_BITSIZE (caller_mode)
2844 < GET_MODE_BITSIZE (callee_mode)))))
2845 try_tail_call = 0;
2848 /* Ensure current function's preferred stack boundary is at least
2849 what we need. Stack alignment may also increase preferred stack
2850 boundary. */
2851 if (crtl->preferred_stack_boundary < preferred_stack_boundary)
2852 crtl->preferred_stack_boundary = preferred_stack_boundary;
2853 else
2854 preferred_stack_boundary = crtl->preferred_stack_boundary;
2856 preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
2858 /* We want to make two insn chains; one for a sibling call, the other
2859 for a normal call. We will select one of the two chains after
2860 initial RTL generation is complete. */
2861 for (pass = try_tail_call ? 0 : 1; pass < 2; pass++)
2863 int sibcall_failure = 0;
2864 /* We want to emit any pending stack adjustments before the tail
2865 recursion "call". That way we know any adjustment after the tail
2866 recursion call can be ignored if we indeed use the tail
2867 call expansion. */
2868 saved_pending_stack_adjust save;
2869 rtx_insn *insns, *before_call, *after_args;
2870 rtx next_arg_reg;
2872 if (pass == 0)
2874 /* State variables we need to save and restore between
2875 iterations. */
2876 save_pending_stack_adjust (&save);
2878 if (pass)
2879 flags &= ~ECF_SIBCALL;
2880 else
2881 flags |= ECF_SIBCALL;
2883 /* Other state variables that we must reinitialize each time
2884 through the loop (that are not initialized by the loop itself). */
2885 argblock = 0;
2886 call_fusage = 0;
2888 /* Start a new sequence for the normal call case.
2890 From this point on, if the sibling call fails, we want to set
2891 sibcall_failure instead of continuing the loop. */
2892 start_sequence ();
2894 /* Don't let pending stack adjusts add up to too much.
2895 Also, do all pending adjustments now if there is any chance
2896 this might be a call to alloca or if we are expanding a sibling
2897 call sequence.
2898 Also do the adjustments before a throwing call, otherwise
2899 exception handling can fail; PR 19225. */
2900 if (pending_stack_adjust >= 32
2901 || (pending_stack_adjust > 0
2902 && (flags & ECF_MAY_BE_ALLOCA))
2903 || (pending_stack_adjust > 0
2904 && flag_exceptions && !(flags & ECF_NOTHROW))
2905 || pass == 0)
2906 do_pending_stack_adjust ();
2908 /* Precompute any arguments as needed. */
2909 if (pass)
2910 precompute_arguments (num_actuals, args);
2912 /* Now we are about to start emitting insns that can be deleted
2913 if a libcall is deleted. */
2914 if (pass && (flags & ECF_MALLOC))
2915 start_sequence ();
2917 if (pass == 0 && crtl->stack_protect_guard)
2918 stack_protect_epilogue ();
2920 adjusted_args_size = args_size;
2921 /* Compute the actual size of the argument block required. The variable
2922 and constant sizes must be combined, the size may have to be rounded,
2923 and there may be a minimum required size. When generating a sibcall
2924 pattern, do not round up, since we'll be re-using whatever space our
2925 caller provided. */
2926 unadjusted_args_size
2927 = compute_argument_block_size (reg_parm_stack_space,
2928 &adjusted_args_size,
2929 fndecl, fntype,
2930 (pass == 0 ? 0
2931 : preferred_stack_boundary));
2933 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
2935 /* The argument block when performing a sibling call is the
2936 incoming argument block. */
2937 if (pass == 0)
2939 argblock = crtl->args.internal_arg_pointer;
2940 if (STACK_GROWS_DOWNWARD)
2941 argblock
2942 = plus_constant (Pmode, argblock, crtl->args.pretend_args_size);
2943 else
2944 argblock
2945 = plus_constant (Pmode, argblock, -crtl->args.pretend_args_size);
2947 stored_args_map = sbitmap_alloc (args_size.constant);
2948 bitmap_clear (stored_args_map);
2951 /* If we have no actual push instructions, or shouldn't use them,
2952 make space for all args right now. */
2953 else if (adjusted_args_size.var != 0)
2955 if (old_stack_level == 0)
2957 emit_stack_save (SAVE_BLOCK, &old_stack_level);
2958 old_stack_pointer_delta = stack_pointer_delta;
2959 old_pending_adj = pending_stack_adjust;
2960 pending_stack_adjust = 0;
2961 /* stack_arg_under_construction says whether a stack arg is
2962 being constructed at the old stack level. Pushing the stack
2963 gets a clean outgoing argument block. */
2964 old_stack_arg_under_construction = stack_arg_under_construction;
2965 stack_arg_under_construction = 0;
2967 argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
2968 if (flag_stack_usage_info)
2969 current_function_has_unbounded_dynamic_stack_size = 1;
2971 else
2973 /* Note that we must go through the motions of allocating an argument
2974 block even if the size is zero because we may be storing args
2975 in the area reserved for register arguments, which may be part of
2976 the stack frame. */
2978 int needed = adjusted_args_size.constant;
2980 /* Store the maximum argument space used. It will be pushed by
2981 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
2982 checking). */
2984 if (needed > crtl->outgoing_args_size)
2985 crtl->outgoing_args_size = needed;
2987 if (must_preallocate)
2989 if (ACCUMULATE_OUTGOING_ARGS)
2991 /* Since the stack pointer will never be pushed, it is
2992 possible for the evaluation of a parm to clobber
2993 something we have already written to the stack.
2994 Since most function calls on RISC machines do not use
2995 the stack, this is uncommon, but must work correctly.
2997 Therefore, we save any area of the stack that was already
2998 written and that we are using. Here we set up to do this
2999 by making a new stack usage map from the old one. The
3000 actual save will be done by store_one_arg.
3002 Another approach might be to try to reorder the argument
3003 evaluations to avoid this conflicting stack usage. */
3005 /* Since we will be writing into the entire argument area,
3006 the map must be allocated for its entire size, not just
3007 the part that is the responsibility of the caller. */
3008 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
3009 needed += reg_parm_stack_space;
3011 if (ARGS_GROW_DOWNWARD)
3012 highest_outgoing_arg_in_use
3013 = MAX (initial_highest_arg_in_use, needed + 1);
3014 else
3015 highest_outgoing_arg_in_use
3016 = MAX (initial_highest_arg_in_use, needed);
3018 free (stack_usage_map_buf);
3019 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
3020 stack_usage_map = stack_usage_map_buf;
3022 if (initial_highest_arg_in_use)
3023 memcpy (stack_usage_map, initial_stack_usage_map,
3024 initial_highest_arg_in_use);
3026 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
3027 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
3028 (highest_outgoing_arg_in_use
3029 - initial_highest_arg_in_use));
3030 needed = 0;
3032 /* The address of the outgoing argument list must not be
3033 copied to a register here, because argblock would be left
3034 pointing to the wrong place after the call to
3035 allocate_dynamic_stack_space below. */
3037 argblock = virtual_outgoing_args_rtx;
3039 else
3041 if (inhibit_defer_pop == 0)
3043 /* Try to reuse some or all of the pending_stack_adjust
3044 to get this space. */
3045 needed
3046 = (combine_pending_stack_adjustment_and_call
3047 (unadjusted_args_size,
3048 &adjusted_args_size,
3049 preferred_unit_stack_boundary));
3051 /* combine_pending_stack_adjustment_and_call computes
3052 an adjustment before the arguments are allocated.
3053 Account for them and see whether or not the stack
3054 needs to go up or down. */
3055 needed = unadjusted_args_size - needed;
3057 if (needed < 0)
3059 /* We're releasing stack space. */
3060 /* ??? We can avoid any adjustment at all if we're
3061 already aligned. FIXME. */
3062 pending_stack_adjust = -needed;
3063 do_pending_stack_adjust ();
3064 needed = 0;
3066 else
3067 /* We need to allocate space. We'll do that in
3068 push_block below. */
3069 pending_stack_adjust = 0;
3072 /* Special case this because overhead of `push_block' in
3073 this case is non-trivial. */
3074 if (needed == 0)
3075 argblock = virtual_outgoing_args_rtx;
3076 else
3078 argblock = push_block (GEN_INT (needed), 0, 0);
3079 if (ARGS_GROW_DOWNWARD)
3080 argblock = plus_constant (Pmode, argblock, needed);
3083 /* We only really need to call `copy_to_reg' in the case
3084 where push insns are going to be used to pass ARGBLOCK
3085 to a function call in ARGS. In that case, the stack
3086 pointer changes value from the allocation point to the
3087 call point, and hence the value of
3088 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
3089 as well always do it. */
3090 argblock = copy_to_reg (argblock);
3095 if (ACCUMULATE_OUTGOING_ARGS)
3097 /* The save/restore code in store_one_arg handles all
3098 cases except one: a constructor call (including a C
3099 function returning a BLKmode struct) to initialize
3100 an argument. */
3101 if (stack_arg_under_construction)
3103 rtx push_size
3104 = GEN_INT (adjusted_args_size.constant
3105 + (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype
3106 : TREE_TYPE (fndecl))) ? 0
3107 : reg_parm_stack_space));
3108 if (old_stack_level == 0)
3110 emit_stack_save (SAVE_BLOCK, &old_stack_level);
3111 old_stack_pointer_delta = stack_pointer_delta;
3112 old_pending_adj = pending_stack_adjust;
3113 pending_stack_adjust = 0;
3114 /* stack_arg_under_construction says whether a stack
3115 arg is being constructed at the old stack level.
3116 Pushing the stack gets a clean outgoing argument
3117 block. */
3118 old_stack_arg_under_construction
3119 = stack_arg_under_construction;
3120 stack_arg_under_construction = 0;
3121 /* Make a new map for the new argument list. */
3122 free (stack_usage_map_buf);
3123 stack_usage_map_buf = XCNEWVEC (char, highest_outgoing_arg_in_use);
3124 stack_usage_map = stack_usage_map_buf;
3125 highest_outgoing_arg_in_use = 0;
3127 /* We can pass TRUE as the 4th argument because we just
3128 saved the stack pointer and will restore it right after
3129 the call. */
3130 allocate_dynamic_stack_space (push_size, 0,
3131 BIGGEST_ALIGNMENT, true);
3134 /* If argument evaluation might modify the stack pointer,
3135 copy the address of the argument list to a register. */
3136 for (i = 0; i < num_actuals; i++)
3137 if (args[i].pass_on_stack)
3139 argblock = copy_addr_to_reg (argblock);
3140 break;
3144 compute_argument_addresses (args, argblock, num_actuals);
3146 /* Perform stack alignment before the first push (the last arg). */
3147 if (argblock == 0
3148 && adjusted_args_size.constant > reg_parm_stack_space
3149 && adjusted_args_size.constant != unadjusted_args_size)
3151 /* When the stack adjustment is pending, we get better code
3152 by combining the adjustments. */
3153 if (pending_stack_adjust
3154 && ! inhibit_defer_pop)
3156 pending_stack_adjust
3157 = (combine_pending_stack_adjustment_and_call
3158 (unadjusted_args_size,
3159 &adjusted_args_size,
3160 preferred_unit_stack_boundary));
3161 do_pending_stack_adjust ();
3163 else if (argblock == 0)
3164 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
3165 - unadjusted_args_size));
3167 /* Now that the stack is properly aligned, pops can't safely
3168 be deferred during the evaluation of the arguments. */
3169 NO_DEFER_POP;
3171 /* Record the maximum pushed stack space size. We need to delay
3172 doing it this far to take into account the optimization done
3173 by combine_pending_stack_adjustment_and_call. */
3174 if (flag_stack_usage_info
3175 && !ACCUMULATE_OUTGOING_ARGS
3176 && pass
3177 && adjusted_args_size.var == 0)
3179 int pushed = adjusted_args_size.constant + pending_stack_adjust;
3180 if (pushed > current_function_pushed_stack_size)
3181 current_function_pushed_stack_size = pushed;
3184 funexp = rtx_for_function_call (fndecl, addr);
3186 /* Precompute all register parameters. It isn't safe to compute anything
3187 once we have started filling any specific hard regs. */
3188 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
3190 if (CALL_EXPR_STATIC_CHAIN (exp))
3191 static_chain_value = expand_normal (CALL_EXPR_STATIC_CHAIN (exp));
3192 else
3193 static_chain_value = 0;
3195 #ifdef REG_PARM_STACK_SPACE
3196 /* Save the fixed argument area if it's part of the caller's frame and
3197 is clobbered by argument setup for this call. */
3198 if (ACCUMULATE_OUTGOING_ARGS && pass)
3199 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3200 &low_to_save, &high_to_save);
3201 #endif
3203 /* Now store (and compute if necessary) all non-register parms.
3204 These come before register parms, since they can require block-moves,
3205 which could clobber the registers used for register parms.
3206 Parms which have partial registers are not stored here,
3207 but we do preallocate space here if they want that. */
3209 for (i = 0; i < num_actuals; i++)
3211 /* Delay bounds until all other args are stored. */
3212 if (POINTER_BOUNDS_P (args[i].tree_value))
3213 continue;
3214 else if (args[i].reg == 0 || args[i].pass_on_stack)
3216 rtx_insn *before_arg = get_last_insn ();
3218 /* We don't allow passing huge (> 2^30 B) arguments
3219 by value. It would cause an overflow later on. */
3220 if (adjusted_args_size.constant
3221 >= (1 << (HOST_BITS_PER_INT - 2)))
3223 sorry ("passing too large argument on stack");
3224 continue;
3227 if (store_one_arg (&args[i], argblock, flags,
3228 adjusted_args_size.var != 0,
3229 reg_parm_stack_space)
3230 || (pass == 0
3231 && check_sibcall_argument_overlap (before_arg,
3232 &args[i], 1)))
3233 sibcall_failure = 1;
3236 if (args[i].stack)
3237 call_fusage
3238 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[i].tree_value)),
3239 gen_rtx_USE (VOIDmode, args[i].stack),
3240 call_fusage);
3243 /* If we have a parm that is passed in registers but not in memory
3244 and whose alignment does not permit a direct copy into registers,
3245 make a group of pseudos that correspond to each register that we
3246 will later fill. */
3247 if (STRICT_ALIGNMENT)
3248 store_unaligned_arguments_into_pseudos (args, num_actuals);
3250 /* Now store any partially-in-registers parm.
3251 This is the last place a block-move can happen. */
3252 if (reg_parm_seen)
3253 for (i = 0; i < num_actuals; i++)
3254 if (args[i].partial != 0 && ! args[i].pass_on_stack)
3256 rtx_insn *before_arg = get_last_insn ();
3258 /* On targets with weird calling conventions (e.g. PA) it's
3259 hard to ensure that all cases of argument overlap between
3260 stack and registers work. Play it safe and bail out. */
3261 if (ARGS_GROW_DOWNWARD && !STACK_GROWS_DOWNWARD)
3263 sibcall_failure = 1;
3264 break;
3267 if (store_one_arg (&args[i], argblock, flags,
3268 adjusted_args_size.var != 0,
3269 reg_parm_stack_space)
3270 || (pass == 0
3271 && check_sibcall_argument_overlap (before_arg,
3272 &args[i], 1)))
3273 sibcall_failure = 1;
3276 bool any_regs = false;
3277 for (i = 0; i < num_actuals; i++)
3278 if (args[i].reg != NULL_RTX)
3280 any_regs = true;
3281 targetm.calls.call_args (args[i].reg, funtype);
3283 if (!any_regs)
3284 targetm.calls.call_args (pc_rtx, funtype);
3286 /* Figure out the register where the value, if any, will come back. */
3287 valreg = 0;
3288 valbnd = 0;
3289 if (TYPE_MODE (rettype) != VOIDmode
3290 && ! structure_value_addr)
3292 if (pcc_struct_value)
3294 valreg = hard_function_value (build_pointer_type (rettype),
3295 fndecl, NULL, (pass == 0));
3296 if (CALL_WITH_BOUNDS_P (exp))
3297 valbnd = targetm.calls.
3298 chkp_function_value_bounds (build_pointer_type (rettype),
3299 fndecl, (pass == 0));
3301 else
3303 valreg = hard_function_value (rettype, fndecl, fntype,
3304 (pass == 0));
3305 if (CALL_WITH_BOUNDS_P (exp))
3306 valbnd = targetm.calls.chkp_function_value_bounds (rettype,
3307 fndecl,
3308 (pass == 0));
3311 /* If VALREG is a PARALLEL whose first member has a zero
3312 offset, use that. This is for targets such as m68k that
3313 return the same value in multiple places. */
3314 if (GET_CODE (valreg) == PARALLEL)
3316 rtx elem = XVECEXP (valreg, 0, 0);
3317 rtx where = XEXP (elem, 0);
3318 rtx offset = XEXP (elem, 1);
3319 if (offset == const0_rtx
3320 && GET_MODE (where) == GET_MODE (valreg))
3321 valreg = where;
3325 /* Store all bounds not passed in registers. */
3326 for (i = 0; i < num_actuals; i++)
3328 if (POINTER_BOUNDS_P (args[i].tree_value)
3329 && !args[i].reg)
3330 store_bounds (&args[i],
3331 args[i].pointer_arg == -1
3332 ? NULL
3333 : &args[args[i].pointer_arg]);
3336 /* If register arguments require space on the stack and stack space
3337 was not preallocated, allocate stack space here for arguments
3338 passed in registers. */
3339 if (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
3340 && !ACCUMULATE_OUTGOING_ARGS
3341 && must_preallocate == 0 && reg_parm_stack_space > 0)
3342 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
3344 /* Pass the function the address in which to return a
3345 structure value. */
3346 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
3348 structure_value_addr
3349 = convert_memory_address (Pmode, structure_value_addr);
3350 emit_move_insn (struct_value,
3351 force_reg (Pmode,
3352 force_operand (structure_value_addr,
3353 NULL_RTX)));
3355 if (REG_P (struct_value))
3356 use_reg (&call_fusage, struct_value);
3359 after_args = get_last_insn ();
3360 funexp = prepare_call_address (fndecl ? fndecl : fntype, funexp,
3361 static_chain_value, &call_fusage,
3362 reg_parm_seen, pass == 0);
3364 load_register_parameters (args, num_actuals, &call_fusage, flags,
3365 pass == 0, &sibcall_failure);
3367 /* Save a pointer to the last insn before the call, so that we can
3368 later safely search backwards to find the CALL_INSN. */
3369 before_call = get_last_insn ();
3371 /* Set up next argument register. For sibling calls on machines
3372 with register windows this should be the incoming register. */
3373 if (pass == 0)
3374 next_arg_reg = targetm.calls.function_incoming_arg (args_so_far,
3375 VOIDmode,
3376 void_type_node,
3377 true);
3378 else
3379 next_arg_reg = targetm.calls.function_arg (args_so_far,
3380 VOIDmode, void_type_node,
3381 true);
3383 if (pass == 1 && (return_flags & ERF_RETURNS_ARG))
3385 int arg_nr = return_flags & ERF_RETURN_ARG_MASK;
3386 arg_nr = num_actuals - arg_nr - 1;
3387 if (arg_nr >= 0
3388 && arg_nr < num_actuals
3389 && args[arg_nr].reg
3390 && valreg
3391 && REG_P (valreg)
3392 && GET_MODE (args[arg_nr].reg) == GET_MODE (valreg))
3393 call_fusage
3394 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[arg_nr].tree_value)),
3395 gen_rtx_SET (valreg, args[arg_nr].reg),
3396 call_fusage);
3398 /* All arguments and registers used for the call must be set up by
3399 now! */
3401 /* Stack must be properly aligned now. */
3402 gcc_assert (!pass
3403 || !(stack_pointer_delta % preferred_unit_stack_boundary));
3405 /* Generate the actual call instruction. */
3406 emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
3407 adjusted_args_size.constant, struct_value_size,
3408 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
3409 flags, args_so_far);
3411 if (flag_ipa_ra)
3413 rtx_call_insn *last;
3414 rtx datum = NULL_RTX;
3415 if (fndecl != NULL_TREE)
3417 datum = XEXP (DECL_RTL (fndecl), 0);
3418 gcc_assert (datum != NULL_RTX
3419 && GET_CODE (datum) == SYMBOL_REF);
3421 last = last_call_insn ();
3422 add_reg_note (last, REG_CALL_DECL, datum);
3425 /* If the call setup or the call itself overlaps with anything
3426 of the argument setup we probably clobbered our call address.
3427 In that case we can't do sibcalls. */
3428 if (pass == 0
3429 && check_sibcall_argument_overlap (after_args, 0, 0))
3430 sibcall_failure = 1;
3432 /* If a non-BLKmode value is returned at the most significant end
3433 of a register, shift the register right by the appropriate amount
3434 and update VALREG accordingly. BLKmode values are handled by the
3435 group load/store machinery below. */
3436 if (!structure_value_addr
3437 && !pcc_struct_value
3438 && TYPE_MODE (rettype) != VOIDmode
3439 && TYPE_MODE (rettype) != BLKmode
3440 && REG_P (valreg)
3441 && targetm.calls.return_in_msb (rettype))
3443 if (shift_return_value (TYPE_MODE (rettype), false, valreg))
3444 sibcall_failure = 1;
3445 valreg = gen_rtx_REG (TYPE_MODE (rettype), REGNO (valreg));
3448 if (pass && (flags & ECF_MALLOC))
3450 rtx temp = gen_reg_rtx (GET_MODE (valreg));
3451 rtx_insn *last, *insns;
3453 /* The return value from a malloc-like function is a pointer. */
3454 if (TREE_CODE (rettype) == POINTER_TYPE)
3455 mark_reg_pointer (temp, MALLOC_ABI_ALIGNMENT);
3457 emit_move_insn (temp, valreg);
3459 /* The return value from a malloc-like function can not alias
3460 anything else. */
3461 last = get_last_insn ();
3462 add_reg_note (last, REG_NOALIAS, temp);
3464 /* Write out the sequence. */
3465 insns = get_insns ();
3466 end_sequence ();
3467 emit_insn (insns);
3468 valreg = temp;
3471 /* For calls to `setjmp', etc., inform
3472 function.c:setjmp_warnings that it should complain if
3473 nonvolatile values are live. For functions that cannot
3474 return, inform flow that control does not fall through. */
3476 if ((flags & ECF_NORETURN) || pass == 0)
3478 /* The barrier must be emitted
3479 immediately after the CALL_INSN. Some ports emit more
3480 than just a CALL_INSN above, so we must search for it here. */
3482 rtx_insn *last = get_last_insn ();
3483 while (!CALL_P (last))
3485 last = PREV_INSN (last);
3486 /* There was no CALL_INSN? */
3487 gcc_assert (last != before_call);
3490 emit_barrier_after (last);
3492 /* Stack adjustments after a noreturn call are dead code.
3493 However when NO_DEFER_POP is in effect, we must preserve
3494 stack_pointer_delta. */
3495 if (inhibit_defer_pop == 0)
3497 stack_pointer_delta = old_stack_allocated;
3498 pending_stack_adjust = 0;
3502 /* If value type not void, return an rtx for the value. */
3504 if (TYPE_MODE (rettype) == VOIDmode
3505 || ignore)
3506 target = const0_rtx;
3507 else if (structure_value_addr)
3509 if (target == 0 || !MEM_P (target))
3511 target
3512 = gen_rtx_MEM (TYPE_MODE (rettype),
3513 memory_address (TYPE_MODE (rettype),
3514 structure_value_addr));
3515 set_mem_attributes (target, rettype, 1);
3518 else if (pcc_struct_value)
3520 /* This is the special C++ case where we need to
3521 know what the true target was. We take care to
3522 never use this value more than once in one expression. */
3523 target = gen_rtx_MEM (TYPE_MODE (rettype),
3524 copy_to_reg (valreg));
3525 set_mem_attributes (target, rettype, 1);
3527 /* Handle calls that return values in multiple non-contiguous locations.
3528 The Irix 6 ABI has examples of this. */
3529 else if (GET_CODE (valreg) == PARALLEL)
3531 if (target == 0)
3532 target = emit_group_move_into_temps (valreg);
3533 else if (rtx_equal_p (target, valreg))
3535 else if (GET_CODE (target) == PARALLEL)
3536 /* Handle the result of a emit_group_move_into_temps
3537 call in the previous pass. */
3538 emit_group_move (target, valreg);
3539 else
3540 emit_group_store (target, valreg, rettype,
3541 int_size_in_bytes (rettype));
3543 else if (target
3544 && GET_MODE (target) == TYPE_MODE (rettype)
3545 && GET_MODE (target) == GET_MODE (valreg))
3547 bool may_overlap = false;
3549 /* We have to copy a return value in a CLASS_LIKELY_SPILLED hard
3550 reg to a plain register. */
3551 if (!REG_P (target) || HARD_REGISTER_P (target))
3552 valreg = avoid_likely_spilled_reg (valreg);
3554 /* If TARGET is a MEM in the argument area, and we have
3555 saved part of the argument area, then we can't store
3556 directly into TARGET as it may get overwritten when we
3557 restore the argument save area below. Don't work too
3558 hard though and simply force TARGET to a register if it
3559 is a MEM; the optimizer is quite likely to sort it out. */
3560 if (ACCUMULATE_OUTGOING_ARGS && pass && MEM_P (target))
3561 for (i = 0; i < num_actuals; i++)
3562 if (args[i].save_area)
3564 may_overlap = true;
3565 break;
3568 if (may_overlap)
3569 target = copy_to_reg (valreg);
3570 else
3572 /* TARGET and VALREG cannot be equal at this point
3573 because the latter would not have
3574 REG_FUNCTION_VALUE_P true, while the former would if
3575 it were referring to the same register.
3577 If they refer to the same register, this move will be
3578 a no-op, except when function inlining is being
3579 done. */
3580 emit_move_insn (target, valreg);
3582 /* If we are setting a MEM, this code must be executed.
3583 Since it is emitted after the call insn, sibcall
3584 optimization cannot be performed in that case. */
3585 if (MEM_P (target))
3586 sibcall_failure = 1;
3589 else
3590 target = copy_to_reg (avoid_likely_spilled_reg (valreg));
3592 /* If we promoted this return value, make the proper SUBREG.
3593 TARGET might be const0_rtx here, so be careful. */
3594 if (REG_P (target)
3595 && TYPE_MODE (rettype) != BLKmode
3596 && GET_MODE (target) != TYPE_MODE (rettype))
3598 tree type = rettype;
3599 int unsignedp = TYPE_UNSIGNED (type);
3600 int offset = 0;
3601 machine_mode pmode;
3603 /* Ensure we promote as expected, and get the new unsignedness. */
3604 pmode = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
3605 funtype, 1);
3606 gcc_assert (GET_MODE (target) == pmode);
3608 if ((WORDS_BIG_ENDIAN || BYTES_BIG_ENDIAN)
3609 && (GET_MODE_SIZE (GET_MODE (target))
3610 > GET_MODE_SIZE (TYPE_MODE (type))))
3612 offset = GET_MODE_SIZE (GET_MODE (target))
3613 - GET_MODE_SIZE (TYPE_MODE (type));
3614 if (! BYTES_BIG_ENDIAN)
3615 offset = (offset / UNITS_PER_WORD) * UNITS_PER_WORD;
3616 else if (! WORDS_BIG_ENDIAN)
3617 offset %= UNITS_PER_WORD;
3620 target = gen_rtx_SUBREG (TYPE_MODE (type), target, offset);
3621 SUBREG_PROMOTED_VAR_P (target) = 1;
3622 SUBREG_PROMOTED_SET (target, unsignedp);
3625 /* If size of args is variable or this was a constructor call for a stack
3626 argument, restore saved stack-pointer value. */
3628 if (old_stack_level)
3630 rtx_insn *prev = get_last_insn ();
3632 emit_stack_restore (SAVE_BLOCK, old_stack_level);
3633 stack_pointer_delta = old_stack_pointer_delta;
3635 fixup_args_size_notes (prev, get_last_insn (), stack_pointer_delta);
3637 pending_stack_adjust = old_pending_adj;
3638 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
3639 stack_arg_under_construction = old_stack_arg_under_construction;
3640 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3641 stack_usage_map = initial_stack_usage_map;
3642 sibcall_failure = 1;
3644 else if (ACCUMULATE_OUTGOING_ARGS && pass)
3646 #ifdef REG_PARM_STACK_SPACE
3647 if (save_area)
3648 restore_fixed_argument_area (save_area, argblock,
3649 high_to_save, low_to_save);
3650 #endif
3652 /* If we saved any argument areas, restore them. */
3653 for (i = 0; i < num_actuals; i++)
3654 if (args[i].save_area)
3656 machine_mode save_mode = GET_MODE (args[i].save_area);
3657 rtx stack_area
3658 = gen_rtx_MEM (save_mode,
3659 memory_address (save_mode,
3660 XEXP (args[i].stack_slot, 0)));
3662 if (save_mode != BLKmode)
3663 emit_move_insn (stack_area, args[i].save_area);
3664 else
3665 emit_block_move (stack_area, args[i].save_area,
3666 GEN_INT (args[i].locate.size.constant),
3667 BLOCK_OP_CALL_PARM);
3670 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3671 stack_usage_map = initial_stack_usage_map;
3674 /* If this was alloca, record the new stack level. */
3675 if (flags & ECF_MAY_BE_ALLOCA)
3676 record_new_stack_level ();
3678 /* Free up storage we no longer need. */
3679 for (i = 0; i < num_actuals; ++i)
3680 free (args[i].aligned_regs);
3682 targetm.calls.end_call_args ();
3684 insns = get_insns ();
3685 end_sequence ();
3687 if (pass == 0)
3689 tail_call_insns = insns;
3691 /* Restore the pending stack adjustment now that we have
3692 finished generating the sibling call sequence. */
3694 restore_pending_stack_adjust (&save);
3696 /* Prepare arg structure for next iteration. */
3697 for (i = 0; i < num_actuals; i++)
3699 args[i].value = 0;
3700 args[i].aligned_regs = 0;
3701 args[i].stack = 0;
3704 sbitmap_free (stored_args_map);
3705 internal_arg_pointer_exp_state.scan_start = NULL;
3706 internal_arg_pointer_exp_state.cache.release ();
3708 else
3710 normal_call_insns = insns;
3712 /* Verify that we've deallocated all the stack we used. */
3713 gcc_assert ((flags & ECF_NORETURN)
3714 || (old_stack_allocated
3715 == stack_pointer_delta - pending_stack_adjust));
3718 /* If something prevents making this a sibling call,
3719 zero out the sequence. */
3720 if (sibcall_failure)
3721 tail_call_insns = NULL;
3722 else
3723 break;
3726 /* If tail call production succeeded, we need to remove REG_EQUIV notes on
3727 arguments too, as argument area is now clobbered by the call. */
3728 if (tail_call_insns)
3730 emit_insn (tail_call_insns);
3731 crtl->tail_call_emit = true;
3733 else
3734 emit_insn (normal_call_insns);
3736 currently_expanding_call--;
3738 free (stack_usage_map_buf);
3740 /* Join result with returned bounds so caller may use them if needed. */
3741 target = chkp_join_splitted_slot (target, valbnd);
3743 return target;
3746 /* A sibling call sequence invalidates any REG_EQUIV notes made for
3747 this function's incoming arguments.
3749 At the start of RTL generation we know the only REG_EQUIV notes
3750 in the rtl chain are those for incoming arguments, so we can look
3751 for REG_EQUIV notes between the start of the function and the
3752 NOTE_INSN_FUNCTION_BEG.
3754 This is (slight) overkill. We could keep track of the highest
3755 argument we clobber and be more selective in removing notes, but it
3756 does not seem to be worth the effort. */
3758 void
3759 fixup_tail_calls (void)
3761 rtx_insn *insn;
3763 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3765 rtx note;
3767 /* There are never REG_EQUIV notes for the incoming arguments
3768 after the NOTE_INSN_FUNCTION_BEG note, so stop if we see it. */
3769 if (NOTE_P (insn)
3770 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3771 break;
3773 note = find_reg_note (insn, REG_EQUIV, 0);
3774 if (note)
3775 remove_note (insn, note);
3776 note = find_reg_note (insn, REG_EQUIV, 0);
3777 gcc_assert (!note);
3781 /* Traverse a list of TYPES and expand all complex types into their
3782 components. */
3783 static tree
3784 split_complex_types (tree types)
3786 tree p;
3788 /* Before allocating memory, check for the common case of no complex. */
3789 for (p = types; p; p = TREE_CHAIN (p))
3791 tree type = TREE_VALUE (p);
3792 if (TREE_CODE (type) == COMPLEX_TYPE
3793 && targetm.calls.split_complex_arg (type))
3794 goto found;
3796 return types;
3798 found:
3799 types = copy_list (types);
3801 for (p = types; p; p = TREE_CHAIN (p))
3803 tree complex_type = TREE_VALUE (p);
3805 if (TREE_CODE (complex_type) == COMPLEX_TYPE
3806 && targetm.calls.split_complex_arg (complex_type))
3808 tree next, imag;
3810 /* Rewrite complex type with component type. */
3811 TREE_VALUE (p) = TREE_TYPE (complex_type);
3812 next = TREE_CHAIN (p);
3814 /* Add another component type for the imaginary part. */
3815 imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
3816 TREE_CHAIN (p) = imag;
3817 TREE_CHAIN (imag) = next;
3819 /* Skip the newly created node. */
3820 p = TREE_CHAIN (p);
3824 return types;
3827 /* Output a library call to function FUN (a SYMBOL_REF rtx).
3828 The RETVAL parameter specifies whether return value needs to be saved, other
3829 parameters are documented in the emit_library_call function below. */
3831 static rtx
3832 emit_library_call_value_1 (int retval, rtx orgfun, rtx value,
3833 enum libcall_type fn_type,
3834 machine_mode outmode, int nargs, va_list p)
3836 /* Total size in bytes of all the stack-parms scanned so far. */
3837 struct args_size args_size;
3838 /* Size of arguments before any adjustments (such as rounding). */
3839 struct args_size original_args_size;
3840 int argnum;
3841 rtx fun;
3842 /* Todo, choose the correct decl type of orgfun. Sadly this information
3843 isn't present here, so we default to native calling abi here. */
3844 tree fndecl ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
3845 tree fntype ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
3846 int count;
3847 rtx argblock = 0;
3848 CUMULATIVE_ARGS args_so_far_v;
3849 cumulative_args_t args_so_far;
3850 struct arg
3852 rtx value;
3853 machine_mode mode;
3854 rtx reg;
3855 int partial;
3856 struct locate_and_pad_arg_data locate;
3857 rtx save_area;
3859 struct arg *argvec;
3860 int old_inhibit_defer_pop = inhibit_defer_pop;
3861 rtx call_fusage = 0;
3862 rtx mem_value = 0;
3863 rtx valreg;
3864 int pcc_struct_value = 0;
3865 int struct_value_size = 0;
3866 int flags;
3867 int reg_parm_stack_space = 0;
3868 int needed;
3869 rtx_insn *before_call;
3870 bool have_push_fusage;
3871 tree tfom; /* type_for_mode (outmode, 0) */
3873 #ifdef REG_PARM_STACK_SPACE
3874 /* Define the boundary of the register parm stack space that needs to be
3875 save, if any. */
3876 int low_to_save = 0, high_to_save = 0;
3877 rtx save_area = 0; /* Place that it is saved. */
3878 #endif
3880 /* Size of the stack reserved for parameter registers. */
3881 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
3882 char *initial_stack_usage_map = stack_usage_map;
3883 char *stack_usage_map_buf = NULL;
3885 rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
3887 #ifdef REG_PARM_STACK_SPACE
3888 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
3889 #endif
3891 /* By default, library functions can not throw. */
3892 flags = ECF_NOTHROW;
3894 switch (fn_type)
3896 case LCT_NORMAL:
3897 break;
3898 case LCT_CONST:
3899 flags |= ECF_CONST;
3900 break;
3901 case LCT_PURE:
3902 flags |= ECF_PURE;
3903 break;
3904 case LCT_NORETURN:
3905 flags |= ECF_NORETURN;
3906 break;
3907 case LCT_THROW:
3908 flags = ECF_NORETURN;
3909 break;
3910 case LCT_RETURNS_TWICE:
3911 flags = ECF_RETURNS_TWICE;
3912 break;
3914 fun = orgfun;
3916 /* Ensure current function's preferred stack boundary is at least
3917 what we need. */
3918 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
3919 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3921 /* If this kind of value comes back in memory,
3922 decide where in memory it should come back. */
3923 if (outmode != VOIDmode)
3925 tfom = lang_hooks.types.type_for_mode (outmode, 0);
3926 if (aggregate_value_p (tfom, 0))
3928 #ifdef PCC_STATIC_STRUCT_RETURN
3929 rtx pointer_reg
3930 = hard_function_value (build_pointer_type (tfom), 0, 0, 0);
3931 mem_value = gen_rtx_MEM (outmode, pointer_reg);
3932 pcc_struct_value = 1;
3933 if (value == 0)
3934 value = gen_reg_rtx (outmode);
3935 #else /* not PCC_STATIC_STRUCT_RETURN */
3936 struct_value_size = GET_MODE_SIZE (outmode);
3937 if (value != 0 && MEM_P (value))
3938 mem_value = value;
3939 else
3940 mem_value = assign_temp (tfom, 1, 1);
3941 #endif
3942 /* This call returns a big structure. */
3943 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
3946 else
3947 tfom = void_type_node;
3949 /* ??? Unfinished: must pass the memory address as an argument. */
3951 /* Copy all the libcall-arguments out of the varargs data
3952 and into a vector ARGVEC.
3954 Compute how to pass each argument. We only support a very small subset
3955 of the full argument passing conventions to limit complexity here since
3956 library functions shouldn't have many args. */
3958 argvec = XALLOCAVEC (struct arg, nargs + 1);
3959 memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
3961 #ifdef INIT_CUMULATIVE_LIBCALL_ARGS
3962 INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far_v, outmode, fun);
3963 #else
3964 INIT_CUMULATIVE_ARGS (args_so_far_v, NULL_TREE, fun, 0, nargs);
3965 #endif
3966 args_so_far = pack_cumulative_args (&args_so_far_v);
3968 args_size.constant = 0;
3969 args_size.var = 0;
3971 count = 0;
3973 push_temp_slots ();
3975 /* If there's a structure value address to be passed,
3976 either pass it in the special place, or pass it as an extra argument. */
3977 if (mem_value && struct_value == 0 && ! pcc_struct_value)
3979 rtx addr = XEXP (mem_value, 0);
3981 nargs++;
3983 /* Make sure it is a reasonable operand for a move or push insn. */
3984 if (!REG_P (addr) && !MEM_P (addr)
3985 && !(CONSTANT_P (addr)
3986 && targetm.legitimate_constant_p (Pmode, addr)))
3987 addr = force_operand (addr, NULL_RTX);
3989 argvec[count].value = addr;
3990 argvec[count].mode = Pmode;
3991 argvec[count].partial = 0;
3993 argvec[count].reg = targetm.calls.function_arg (args_so_far,
3994 Pmode, NULL_TREE, true);
3995 gcc_assert (targetm.calls.arg_partial_bytes (args_so_far, Pmode,
3996 NULL_TREE, 1) == 0);
3998 locate_and_pad_parm (Pmode, NULL_TREE,
3999 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4001 #else
4002 argvec[count].reg != 0,
4003 #endif
4004 reg_parm_stack_space, 0,
4005 NULL_TREE, &args_size, &argvec[count].locate);
4007 if (argvec[count].reg == 0 || argvec[count].partial != 0
4008 || reg_parm_stack_space > 0)
4009 args_size.constant += argvec[count].locate.size.constant;
4011 targetm.calls.function_arg_advance (args_so_far, Pmode, (tree) 0, true);
4013 count++;
4016 for (; count < nargs; count++)
4018 rtx val = va_arg (p, rtx);
4019 machine_mode mode = (machine_mode) va_arg (p, int);
4020 int unsigned_p = 0;
4022 /* We cannot convert the arg value to the mode the library wants here;
4023 must do it earlier where we know the signedness of the arg. */
4024 gcc_assert (mode != BLKmode
4025 && (GET_MODE (val) == mode || GET_MODE (val) == VOIDmode));
4027 /* Make sure it is a reasonable operand for a move or push insn. */
4028 if (!REG_P (val) && !MEM_P (val)
4029 && !(CONSTANT_P (val) && targetm.legitimate_constant_p (mode, val)))
4030 val = force_operand (val, NULL_RTX);
4032 if (pass_by_reference (&args_so_far_v, mode, NULL_TREE, 1))
4034 rtx slot;
4035 int must_copy
4036 = !reference_callee_copied (&args_so_far_v, mode, NULL_TREE, 1);
4038 /* If this was a CONST function, it is now PURE since it now
4039 reads memory. */
4040 if (flags & ECF_CONST)
4042 flags &= ~ECF_CONST;
4043 flags |= ECF_PURE;
4046 if (MEM_P (val) && !must_copy)
4048 tree val_expr = MEM_EXPR (val);
4049 if (val_expr)
4050 mark_addressable (val_expr);
4051 slot = val;
4053 else
4055 slot = assign_temp (lang_hooks.types.type_for_mode (mode, 0),
4056 1, 1);
4057 emit_move_insn (slot, val);
4060 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4061 gen_rtx_USE (VOIDmode, slot),
4062 call_fusage);
4063 if (must_copy)
4064 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4065 gen_rtx_CLOBBER (VOIDmode,
4066 slot),
4067 call_fusage);
4069 mode = Pmode;
4070 val = force_operand (XEXP (slot, 0), NULL_RTX);
4073 mode = promote_function_mode (NULL_TREE, mode, &unsigned_p, NULL_TREE, 0);
4074 argvec[count].mode = mode;
4075 argvec[count].value = convert_modes (mode, GET_MODE (val), val, unsigned_p);
4076 argvec[count].reg = targetm.calls.function_arg (args_so_far, mode,
4077 NULL_TREE, true);
4079 argvec[count].partial
4080 = targetm.calls.arg_partial_bytes (args_so_far, mode, NULL_TREE, 1);
4082 if (argvec[count].reg == 0
4083 || argvec[count].partial != 0
4084 || reg_parm_stack_space > 0)
4086 locate_and_pad_parm (mode, NULL_TREE,
4087 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4089 #else
4090 argvec[count].reg != 0,
4091 #endif
4092 reg_parm_stack_space, argvec[count].partial,
4093 NULL_TREE, &args_size, &argvec[count].locate);
4094 args_size.constant += argvec[count].locate.size.constant;
4095 gcc_assert (!argvec[count].locate.size.var);
4097 #ifdef BLOCK_REG_PADDING
4098 else
4099 /* The argument is passed entirely in registers. See at which
4100 end it should be padded. */
4101 argvec[count].locate.where_pad =
4102 BLOCK_REG_PADDING (mode, NULL_TREE,
4103 GET_MODE_SIZE (mode) <= UNITS_PER_WORD);
4104 #endif
4106 targetm.calls.function_arg_advance (args_so_far, mode, (tree) 0, true);
4109 /* If this machine requires an external definition for library
4110 functions, write one out. */
4111 assemble_external_libcall (fun);
4113 original_args_size = args_size;
4114 args_size.constant = (((args_size.constant
4115 + stack_pointer_delta
4116 + STACK_BYTES - 1)
4117 / STACK_BYTES
4118 * STACK_BYTES)
4119 - stack_pointer_delta);
4121 args_size.constant = MAX (args_size.constant,
4122 reg_parm_stack_space);
4124 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4125 args_size.constant -= reg_parm_stack_space;
4127 if (args_size.constant > crtl->outgoing_args_size)
4128 crtl->outgoing_args_size = args_size.constant;
4130 if (flag_stack_usage_info && !ACCUMULATE_OUTGOING_ARGS)
4132 int pushed = args_size.constant + pending_stack_adjust;
4133 if (pushed > current_function_pushed_stack_size)
4134 current_function_pushed_stack_size = pushed;
4137 if (ACCUMULATE_OUTGOING_ARGS)
4139 /* Since the stack pointer will never be pushed, it is possible for
4140 the evaluation of a parm to clobber something we have already
4141 written to the stack. Since most function calls on RISC machines
4142 do not use the stack, this is uncommon, but must work correctly.
4144 Therefore, we save any area of the stack that was already written
4145 and that we are using. Here we set up to do this by making a new
4146 stack usage map from the old one.
4148 Another approach might be to try to reorder the argument
4149 evaluations to avoid this conflicting stack usage. */
4151 needed = args_size.constant;
4153 /* Since we will be writing into the entire argument area, the
4154 map must be allocated for its entire size, not just the part that
4155 is the responsibility of the caller. */
4156 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4157 needed += reg_parm_stack_space;
4159 if (ARGS_GROW_DOWNWARD)
4160 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
4161 needed + 1);
4162 else
4163 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use, needed);
4165 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
4166 stack_usage_map = stack_usage_map_buf;
4168 if (initial_highest_arg_in_use)
4169 memcpy (stack_usage_map, initial_stack_usage_map,
4170 initial_highest_arg_in_use);
4172 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
4173 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
4174 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
4175 needed = 0;
4177 /* We must be careful to use virtual regs before they're instantiated,
4178 and real regs afterwards. Loop optimization, for example, can create
4179 new libcalls after we've instantiated the virtual regs, and if we
4180 use virtuals anyway, they won't match the rtl patterns. */
4182 if (virtuals_instantiated)
4183 argblock = plus_constant (Pmode, stack_pointer_rtx,
4184 STACK_POINTER_OFFSET);
4185 else
4186 argblock = virtual_outgoing_args_rtx;
4188 else
4190 if (!PUSH_ARGS)
4191 argblock = push_block (GEN_INT (args_size.constant), 0, 0);
4194 /* We push args individually in reverse order, perform stack alignment
4195 before the first push (the last arg). */
4196 if (argblock == 0)
4197 anti_adjust_stack (GEN_INT (args_size.constant
4198 - original_args_size.constant));
4200 argnum = nargs - 1;
4202 #ifdef REG_PARM_STACK_SPACE
4203 if (ACCUMULATE_OUTGOING_ARGS)
4205 /* The argument list is the property of the called routine and it
4206 may clobber it. If the fixed area has been used for previous
4207 parameters, we must save and restore it. */
4208 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
4209 &low_to_save, &high_to_save);
4211 #endif
4213 /* When expanding a normal call, args are stored in push order,
4214 which is the reverse of what we have here. */
4215 bool any_regs = false;
4216 for (int i = nargs; i-- > 0; )
4217 if (argvec[i].reg != NULL_RTX)
4219 targetm.calls.call_args (argvec[i].reg, NULL_TREE);
4220 any_regs = true;
4222 if (!any_regs)
4223 targetm.calls.call_args (pc_rtx, NULL_TREE);
4225 /* Push the args that need to be pushed. */
4227 have_push_fusage = false;
4229 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4230 are to be pushed. */
4231 for (count = 0; count < nargs; count++, argnum--)
4233 machine_mode mode = argvec[argnum].mode;
4234 rtx val = argvec[argnum].value;
4235 rtx reg = argvec[argnum].reg;
4236 int partial = argvec[argnum].partial;
4237 unsigned int parm_align = argvec[argnum].locate.boundary;
4238 int lower_bound = 0, upper_bound = 0, i;
4240 if (! (reg != 0 && partial == 0))
4242 rtx use;
4244 if (ACCUMULATE_OUTGOING_ARGS)
4246 /* If this is being stored into a pre-allocated, fixed-size,
4247 stack area, save any previous data at that location. */
4249 if (ARGS_GROW_DOWNWARD)
4251 /* stack_slot is negative, but we want to index stack_usage_map
4252 with positive values. */
4253 upper_bound = -argvec[argnum].locate.slot_offset.constant + 1;
4254 lower_bound = upper_bound - argvec[argnum].locate.size.constant;
4256 else
4258 lower_bound = argvec[argnum].locate.slot_offset.constant;
4259 upper_bound = lower_bound + argvec[argnum].locate.size.constant;
4262 i = lower_bound;
4263 /* Don't worry about things in the fixed argument area;
4264 it has already been saved. */
4265 if (i < reg_parm_stack_space)
4266 i = reg_parm_stack_space;
4267 while (i < upper_bound && stack_usage_map[i] == 0)
4268 i++;
4270 if (i < upper_bound)
4272 /* We need to make a save area. */
4273 unsigned int size
4274 = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
4275 machine_mode save_mode
4276 = mode_for_size (size, MODE_INT, 1);
4277 rtx adr
4278 = plus_constant (Pmode, argblock,
4279 argvec[argnum].locate.offset.constant);
4280 rtx stack_area
4281 = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
4283 if (save_mode == BLKmode)
4285 argvec[argnum].save_area
4286 = assign_stack_temp (BLKmode,
4287 argvec[argnum].locate.size.constant
4290 emit_block_move (validize_mem
4291 (copy_rtx (argvec[argnum].save_area)),
4292 stack_area,
4293 GEN_INT (argvec[argnum].locate.size.constant),
4294 BLOCK_OP_CALL_PARM);
4296 else
4298 argvec[argnum].save_area = gen_reg_rtx (save_mode);
4300 emit_move_insn (argvec[argnum].save_area, stack_area);
4305 emit_push_insn (val, mode, NULL_TREE, NULL_RTX, parm_align,
4306 partial, reg, 0, argblock,
4307 GEN_INT (argvec[argnum].locate.offset.constant),
4308 reg_parm_stack_space,
4309 ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad), false);
4311 /* Now mark the segment we just used. */
4312 if (ACCUMULATE_OUTGOING_ARGS)
4313 for (i = lower_bound; i < upper_bound; i++)
4314 stack_usage_map[i] = 1;
4316 NO_DEFER_POP;
4318 /* Indicate argument access so that alias.c knows that these
4319 values are live. */
4320 if (argblock)
4321 use = plus_constant (Pmode, argblock,
4322 argvec[argnum].locate.offset.constant);
4323 else if (have_push_fusage)
4324 continue;
4325 else
4327 /* When arguments are pushed, trying to tell alias.c where
4328 exactly this argument is won't work, because the
4329 auto-increment causes confusion. So we merely indicate
4330 that we access something with a known mode somewhere on
4331 the stack. */
4332 use = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4333 gen_rtx_SCRATCH (Pmode));
4334 have_push_fusage = true;
4336 use = gen_rtx_MEM (argvec[argnum].mode, use);
4337 use = gen_rtx_USE (VOIDmode, use);
4338 call_fusage = gen_rtx_EXPR_LIST (VOIDmode, use, call_fusage);
4342 argnum = nargs - 1;
4344 fun = prepare_call_address (NULL, fun, NULL, &call_fusage, 0, 0);
4346 /* Now load any reg parms into their regs. */
4348 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4349 are to be pushed. */
4350 for (count = 0; count < nargs; count++, argnum--)
4352 machine_mode mode = argvec[argnum].mode;
4353 rtx val = argvec[argnum].value;
4354 rtx reg = argvec[argnum].reg;
4355 int partial = argvec[argnum].partial;
4356 #ifdef BLOCK_REG_PADDING
4357 int size = 0;
4358 #endif
4360 /* Handle calls that pass values in multiple non-contiguous
4361 locations. The PA64 has examples of this for library calls. */
4362 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4363 emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (mode));
4364 else if (reg != 0 && partial == 0)
4366 emit_move_insn (reg, val);
4367 #ifdef BLOCK_REG_PADDING
4368 size = GET_MODE_SIZE (argvec[argnum].mode);
4370 /* Copied from load_register_parameters. */
4372 /* Handle case where we have a value that needs shifting
4373 up to the msb. eg. a QImode value and we're padding
4374 upward on a BYTES_BIG_ENDIAN machine. */
4375 if (size < UNITS_PER_WORD
4376 && (argvec[argnum].locate.where_pad
4377 == (BYTES_BIG_ENDIAN ? upward : downward)))
4379 rtx x;
4380 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
4382 /* Assigning REG here rather than a temp makes CALL_FUSAGE
4383 report the whole reg as used. Strictly speaking, the
4384 call only uses SIZE bytes at the msb end, but it doesn't
4385 seem worth generating rtl to say that. */
4386 reg = gen_rtx_REG (word_mode, REGNO (reg));
4387 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
4388 if (x != reg)
4389 emit_move_insn (reg, x);
4391 #endif
4394 NO_DEFER_POP;
4397 /* Any regs containing parms remain in use through the call. */
4398 for (count = 0; count < nargs; count++)
4400 rtx reg = argvec[count].reg;
4401 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4402 use_group_regs (&call_fusage, reg);
4403 else if (reg != 0)
4405 int partial = argvec[count].partial;
4406 if (partial)
4408 int nregs;
4409 gcc_assert (partial % UNITS_PER_WORD == 0);
4410 nregs = partial / UNITS_PER_WORD;
4411 use_regs (&call_fusage, REGNO (reg), nregs);
4413 else
4414 use_reg (&call_fusage, reg);
4418 /* Pass the function the address in which to return a structure value. */
4419 if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
4421 emit_move_insn (struct_value,
4422 force_reg (Pmode,
4423 force_operand (XEXP (mem_value, 0),
4424 NULL_RTX)));
4425 if (REG_P (struct_value))
4426 use_reg (&call_fusage, struct_value);
4429 /* Don't allow popping to be deferred, since then
4430 cse'ing of library calls could delete a call and leave the pop. */
4431 NO_DEFER_POP;
4432 valreg = (mem_value == 0 && outmode != VOIDmode
4433 ? hard_libcall_value (outmode, orgfun) : NULL_RTX);
4435 /* Stack must be properly aligned now. */
4436 gcc_assert (!(stack_pointer_delta
4437 & (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1)));
4439 before_call = get_last_insn ();
4441 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
4442 will set inhibit_defer_pop to that value. */
4443 /* The return type is needed to decide how many bytes the function pops.
4444 Signedness plays no role in that, so for simplicity, we pretend it's
4445 always signed. We also assume that the list of arguments passed has
4446 no impact, so we pretend it is unknown. */
4448 emit_call_1 (fun, NULL,
4449 get_identifier (XSTR (orgfun, 0)),
4450 build_function_type (tfom, NULL_TREE),
4451 original_args_size.constant, args_size.constant,
4452 struct_value_size,
4453 targetm.calls.function_arg (args_so_far,
4454 VOIDmode, void_type_node, true),
4455 valreg,
4456 old_inhibit_defer_pop + 1, call_fusage, flags, args_so_far);
4458 if (flag_ipa_ra)
4460 rtx datum = orgfun;
4461 gcc_assert (GET_CODE (datum) == SYMBOL_REF);
4462 rtx_call_insn *last = last_call_insn ();
4463 add_reg_note (last, REG_CALL_DECL, datum);
4466 /* Right-shift returned value if necessary. */
4467 if (!pcc_struct_value
4468 && TYPE_MODE (tfom) != BLKmode
4469 && targetm.calls.return_in_msb (tfom))
4471 shift_return_value (TYPE_MODE (tfom), false, valreg);
4472 valreg = gen_rtx_REG (TYPE_MODE (tfom), REGNO (valreg));
4475 targetm.calls.end_call_args ();
4477 /* For calls to `setjmp', etc., inform function.c:setjmp_warnings
4478 that it should complain if nonvolatile values are live. For
4479 functions that cannot return, inform flow that control does not
4480 fall through. */
4481 if (flags & ECF_NORETURN)
4483 /* The barrier note must be emitted
4484 immediately after the CALL_INSN. Some ports emit more than
4485 just a CALL_INSN above, so we must search for it here. */
4486 rtx_insn *last = get_last_insn ();
4487 while (!CALL_P (last))
4489 last = PREV_INSN (last);
4490 /* There was no CALL_INSN? */
4491 gcc_assert (last != before_call);
4494 emit_barrier_after (last);
4497 /* Consider that "regular" libcalls, i.e. all of them except for LCT_THROW
4498 and LCT_RETURNS_TWICE, cannot perform non-local gotos. */
4499 if (flags & ECF_NOTHROW)
4501 rtx_insn *last = get_last_insn ();
4502 while (!CALL_P (last))
4504 last = PREV_INSN (last);
4505 /* There was no CALL_INSN? */
4506 gcc_assert (last != before_call);
4509 make_reg_eh_region_note_nothrow_nononlocal (last);
4512 /* Now restore inhibit_defer_pop to its actual original value. */
4513 OK_DEFER_POP;
4515 pop_temp_slots ();
4517 /* Copy the value to the right place. */
4518 if (outmode != VOIDmode && retval)
4520 if (mem_value)
4522 if (value == 0)
4523 value = mem_value;
4524 if (value != mem_value)
4525 emit_move_insn (value, mem_value);
4527 else if (GET_CODE (valreg) == PARALLEL)
4529 if (value == 0)
4530 value = gen_reg_rtx (outmode);
4531 emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
4533 else
4535 /* Convert to the proper mode if a promotion has been active. */
4536 if (GET_MODE (valreg) != outmode)
4538 int unsignedp = TYPE_UNSIGNED (tfom);
4540 gcc_assert (promote_function_mode (tfom, outmode, &unsignedp,
4541 fndecl ? TREE_TYPE (fndecl) : fntype, 1)
4542 == GET_MODE (valreg));
4543 valreg = convert_modes (outmode, GET_MODE (valreg), valreg, 0);
4546 if (value != 0)
4547 emit_move_insn (value, valreg);
4548 else
4549 value = valreg;
4553 if (ACCUMULATE_OUTGOING_ARGS)
4555 #ifdef REG_PARM_STACK_SPACE
4556 if (save_area)
4557 restore_fixed_argument_area (save_area, argblock,
4558 high_to_save, low_to_save);
4559 #endif
4561 /* If we saved any argument areas, restore them. */
4562 for (count = 0; count < nargs; count++)
4563 if (argvec[count].save_area)
4565 machine_mode save_mode = GET_MODE (argvec[count].save_area);
4566 rtx adr = plus_constant (Pmode, argblock,
4567 argvec[count].locate.offset.constant);
4568 rtx stack_area = gen_rtx_MEM (save_mode,
4569 memory_address (save_mode, adr));
4571 if (save_mode == BLKmode)
4572 emit_block_move (stack_area,
4573 validize_mem
4574 (copy_rtx (argvec[count].save_area)),
4575 GEN_INT (argvec[count].locate.size.constant),
4576 BLOCK_OP_CALL_PARM);
4577 else
4578 emit_move_insn (stack_area, argvec[count].save_area);
4581 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
4582 stack_usage_map = initial_stack_usage_map;
4585 free (stack_usage_map_buf);
4587 return value;
4591 /* Output a library call to function FUN (a SYMBOL_REF rtx)
4592 (emitting the queue unless NO_QUEUE is nonzero),
4593 for a value of mode OUTMODE,
4594 with NARGS different arguments, passed as alternating rtx values
4595 and machine_modes to convert them to.
4597 FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for
4598 `const' calls, LCT_PURE for `pure' calls, or other LCT_ value for
4599 other types of library calls. */
4601 void
4602 emit_library_call (rtx orgfun, enum libcall_type fn_type,
4603 machine_mode outmode, int nargs, ...)
4605 va_list p;
4607 va_start (p, nargs);
4608 emit_library_call_value_1 (0, orgfun, NULL_RTX, fn_type, outmode, nargs, p);
4609 va_end (p);
4612 /* Like emit_library_call except that an extra argument, VALUE,
4613 comes second and says where to store the result.
4614 (If VALUE is zero, this function chooses a convenient way
4615 to return the value.
4617 This function returns an rtx for where the value is to be found.
4618 If VALUE is nonzero, VALUE is returned. */
4621 emit_library_call_value (rtx orgfun, rtx value,
4622 enum libcall_type fn_type,
4623 machine_mode outmode, int nargs, ...)
4625 rtx result;
4626 va_list p;
4628 va_start (p, nargs);
4629 result = emit_library_call_value_1 (1, orgfun, value, fn_type, outmode,
4630 nargs, p);
4631 va_end (p);
4633 return result;
4637 /* Store pointer bounds argument ARG into Bounds Table entry
4638 associated with PARM. */
4639 static void
4640 store_bounds (struct arg_data *arg, struct arg_data *parm)
4642 rtx slot = NULL, ptr = NULL, addr = NULL;
4644 /* We may pass bounds not associated with any pointer. */
4645 if (!parm)
4647 gcc_assert (arg->special_slot);
4648 slot = arg->special_slot;
4649 ptr = const0_rtx;
4651 /* Find pointer associated with bounds and where it is
4652 passed. */
4653 else
4655 if (!parm->reg)
4657 gcc_assert (!arg->special_slot);
4659 addr = adjust_address (parm->stack, Pmode, arg->pointer_offset);
4661 else if (REG_P (parm->reg))
4663 gcc_assert (arg->special_slot);
4664 slot = arg->special_slot;
4666 if (MEM_P (parm->value))
4667 addr = adjust_address (parm->value, Pmode, arg->pointer_offset);
4668 else if (REG_P (parm->value))
4669 ptr = gen_rtx_SUBREG (Pmode, parm->value, arg->pointer_offset);
4670 else
4672 gcc_assert (!arg->pointer_offset);
4673 ptr = parm->value;
4676 else
4678 gcc_assert (GET_CODE (parm->reg) == PARALLEL);
4680 gcc_assert (arg->special_slot);
4681 slot = arg->special_slot;
4683 if (parm->parallel_value)
4684 ptr = chkp_get_value_with_offs (parm->parallel_value,
4685 GEN_INT (arg->pointer_offset));
4686 else
4687 gcc_unreachable ();
4691 /* Expand bounds. */
4692 if (!arg->value)
4693 arg->value = expand_normal (arg->tree_value);
4695 targetm.calls.store_bounds_for_arg (ptr, addr, arg->value, slot);
4698 /* Store a single argument for a function call
4699 into the register or memory area where it must be passed.
4700 *ARG describes the argument value and where to pass it.
4702 ARGBLOCK is the address of the stack-block for all the arguments,
4703 or 0 on a machine where arguments are pushed individually.
4705 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
4706 so must be careful about how the stack is used.
4708 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
4709 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
4710 that we need not worry about saving and restoring the stack.
4712 FNDECL is the declaration of the function we are calling.
4714 Return nonzero if this arg should cause sibcall failure,
4715 zero otherwise. */
4717 static int
4718 store_one_arg (struct arg_data *arg, rtx argblock, int flags,
4719 int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
4721 tree pval = arg->tree_value;
4722 rtx reg = 0;
4723 int partial = 0;
4724 int used = 0;
4725 int i, lower_bound = 0, upper_bound = 0;
4726 int sibcall_failure = 0;
4728 if (TREE_CODE (pval) == ERROR_MARK)
4729 return 1;
4731 /* Push a new temporary level for any temporaries we make for
4732 this argument. */
4733 push_temp_slots ();
4735 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
4737 /* If this is being stored into a pre-allocated, fixed-size, stack area,
4738 save any previous data at that location. */
4739 if (argblock && ! variable_size && arg->stack)
4741 if (ARGS_GROW_DOWNWARD)
4743 /* stack_slot is negative, but we want to index stack_usage_map
4744 with positive values. */
4745 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4746 upper_bound = -INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1)) + 1;
4747 else
4748 upper_bound = 0;
4750 lower_bound = upper_bound - arg->locate.size.constant;
4752 else
4754 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4755 lower_bound = INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1));
4756 else
4757 lower_bound = 0;
4759 upper_bound = lower_bound + arg->locate.size.constant;
4762 i = lower_bound;
4763 /* Don't worry about things in the fixed argument area;
4764 it has already been saved. */
4765 if (i < reg_parm_stack_space)
4766 i = reg_parm_stack_space;
4767 while (i < upper_bound && stack_usage_map[i] == 0)
4768 i++;
4770 if (i < upper_bound)
4772 /* We need to make a save area. */
4773 unsigned int size = arg->locate.size.constant * BITS_PER_UNIT;
4774 machine_mode save_mode = mode_for_size (size, MODE_INT, 1);
4775 rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
4776 rtx stack_area = gen_rtx_MEM (save_mode, adr);
4778 if (save_mode == BLKmode)
4780 arg->save_area
4781 = assign_temp (TREE_TYPE (arg->tree_value), 1, 1);
4782 preserve_temp_slots (arg->save_area);
4783 emit_block_move (validize_mem (copy_rtx (arg->save_area)),
4784 stack_area,
4785 GEN_INT (arg->locate.size.constant),
4786 BLOCK_OP_CALL_PARM);
4788 else
4790 arg->save_area = gen_reg_rtx (save_mode);
4791 emit_move_insn (arg->save_area, stack_area);
4797 /* If this isn't going to be placed on both the stack and in registers,
4798 set up the register and number of words. */
4799 if (! arg->pass_on_stack)
4801 if (flags & ECF_SIBCALL)
4802 reg = arg->tail_call_reg;
4803 else
4804 reg = arg->reg;
4805 partial = arg->partial;
4808 /* Being passed entirely in a register. We shouldn't be called in
4809 this case. */
4810 gcc_assert (reg == 0 || partial != 0);
4812 /* If this arg needs special alignment, don't load the registers
4813 here. */
4814 if (arg->n_aligned_regs != 0)
4815 reg = 0;
4817 /* If this is being passed partially in a register, we can't evaluate
4818 it directly into its stack slot. Otherwise, we can. */
4819 if (arg->value == 0)
4821 /* stack_arg_under_construction is nonzero if a function argument is
4822 being evaluated directly into the outgoing argument list and
4823 expand_call must take special action to preserve the argument list
4824 if it is called recursively.
4826 For scalar function arguments stack_usage_map is sufficient to
4827 determine which stack slots must be saved and restored. Scalar
4828 arguments in general have pass_on_stack == 0.
4830 If this argument is initialized by a function which takes the
4831 address of the argument (a C++ constructor or a C function
4832 returning a BLKmode structure), then stack_usage_map is
4833 insufficient and expand_call must push the stack around the
4834 function call. Such arguments have pass_on_stack == 1.
4836 Note that it is always safe to set stack_arg_under_construction,
4837 but this generates suboptimal code if set when not needed. */
4839 if (arg->pass_on_stack)
4840 stack_arg_under_construction++;
4842 arg->value = expand_expr (pval,
4843 (partial
4844 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
4845 ? NULL_RTX : arg->stack,
4846 VOIDmode, EXPAND_STACK_PARM);
4848 /* If we are promoting object (or for any other reason) the mode
4849 doesn't agree, convert the mode. */
4851 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
4852 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
4853 arg->value, arg->unsignedp);
4855 if (arg->pass_on_stack)
4856 stack_arg_under_construction--;
4859 /* Check for overlap with already clobbered argument area. */
4860 if ((flags & ECF_SIBCALL)
4861 && MEM_P (arg->value)
4862 && mem_overlaps_already_clobbered_arg_p (XEXP (arg->value, 0),
4863 arg->locate.size.constant))
4864 sibcall_failure = 1;
4866 /* Don't allow anything left on stack from computation
4867 of argument to alloca. */
4868 if (flags & ECF_MAY_BE_ALLOCA)
4869 do_pending_stack_adjust ();
4871 if (arg->value == arg->stack)
4872 /* If the value is already in the stack slot, we are done. */
4874 else if (arg->mode != BLKmode)
4876 int size;
4877 unsigned int parm_align;
4879 /* Argument is a scalar, not entirely passed in registers.
4880 (If part is passed in registers, arg->partial says how much
4881 and emit_push_insn will take care of putting it there.)
4883 Push it, and if its size is less than the
4884 amount of space allocated to it,
4885 also bump stack pointer by the additional space.
4886 Note that in C the default argument promotions
4887 will prevent such mismatches. */
4889 size = GET_MODE_SIZE (arg->mode);
4890 /* Compute how much space the push instruction will push.
4891 On many machines, pushing a byte will advance the stack
4892 pointer by a halfword. */
4893 #ifdef PUSH_ROUNDING
4894 size = PUSH_ROUNDING (size);
4895 #endif
4896 used = size;
4898 /* Compute how much space the argument should get:
4899 round up to a multiple of the alignment for arguments. */
4900 if (none != FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)))
4901 used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
4902 / (PARM_BOUNDARY / BITS_PER_UNIT))
4903 * (PARM_BOUNDARY / BITS_PER_UNIT));
4905 /* Compute the alignment of the pushed argument. */
4906 parm_align = arg->locate.boundary;
4907 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
4909 int pad = used - size;
4910 if (pad)
4912 unsigned int pad_align = (pad & -pad) * BITS_PER_UNIT;
4913 parm_align = MIN (parm_align, pad_align);
4917 /* This isn't already where we want it on the stack, so put it there.
4918 This can either be done with push or copy insns. */
4919 if (!emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), NULL_RTX,
4920 parm_align, partial, reg, used - size, argblock,
4921 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4922 ARGS_SIZE_RTX (arg->locate.alignment_pad), true))
4923 sibcall_failure = 1;
4925 /* Unless this is a partially-in-register argument, the argument is now
4926 in the stack. */
4927 if (partial == 0)
4928 arg->value = arg->stack;
4930 else
4932 /* BLKmode, at least partly to be pushed. */
4934 unsigned int parm_align;
4935 int excess;
4936 rtx size_rtx;
4938 /* Pushing a nonscalar.
4939 If part is passed in registers, PARTIAL says how much
4940 and emit_push_insn will take care of putting it there. */
4942 /* Round its size up to a multiple
4943 of the allocation unit for arguments. */
4945 if (arg->locate.size.var != 0)
4947 excess = 0;
4948 size_rtx = ARGS_SIZE_RTX (arg->locate.size);
4950 else
4952 /* PUSH_ROUNDING has no effect on us, because emit_push_insn
4953 for BLKmode is careful to avoid it. */
4954 excess = (arg->locate.size.constant
4955 - int_size_in_bytes (TREE_TYPE (pval))
4956 + partial);
4957 size_rtx = expand_expr (size_in_bytes (TREE_TYPE (pval)),
4958 NULL_RTX, TYPE_MODE (sizetype),
4959 EXPAND_NORMAL);
4962 parm_align = arg->locate.boundary;
4964 /* When an argument is padded down, the block is aligned to
4965 PARM_BOUNDARY, but the actual argument isn't. */
4966 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
4968 if (arg->locate.size.var)
4969 parm_align = BITS_PER_UNIT;
4970 else if (excess)
4972 unsigned int excess_align = (excess & -excess) * BITS_PER_UNIT;
4973 parm_align = MIN (parm_align, excess_align);
4977 if ((flags & ECF_SIBCALL) && MEM_P (arg->value))
4979 /* emit_push_insn might not work properly if arg->value and
4980 argblock + arg->locate.offset areas overlap. */
4981 rtx x = arg->value;
4982 int i = 0;
4984 if (XEXP (x, 0) == crtl->args.internal_arg_pointer
4985 || (GET_CODE (XEXP (x, 0)) == PLUS
4986 && XEXP (XEXP (x, 0), 0) ==
4987 crtl->args.internal_arg_pointer
4988 && CONST_INT_P (XEXP (XEXP (x, 0), 1))))
4990 if (XEXP (x, 0) != crtl->args.internal_arg_pointer)
4991 i = INTVAL (XEXP (XEXP (x, 0), 1));
4993 /* expand_call should ensure this. */
4994 gcc_assert (!arg->locate.offset.var
4995 && arg->locate.size.var == 0
4996 && CONST_INT_P (size_rtx));
4998 if (arg->locate.offset.constant > i)
5000 if (arg->locate.offset.constant < i + INTVAL (size_rtx))
5001 sibcall_failure = 1;
5003 else if (arg->locate.offset.constant < i)
5005 /* Use arg->locate.size.constant instead of size_rtx
5006 because we only care about the part of the argument
5007 on the stack. */
5008 if (i < (arg->locate.offset.constant
5009 + arg->locate.size.constant))
5010 sibcall_failure = 1;
5012 else
5014 /* Even though they appear to be at the same location,
5015 if part of the outgoing argument is in registers,
5016 they aren't really at the same location. Check for
5017 this by making sure that the incoming size is the
5018 same as the outgoing size. */
5019 if (arg->locate.size.constant != INTVAL (size_rtx))
5020 sibcall_failure = 1;
5025 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
5026 parm_align, partial, reg, excess, argblock,
5027 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
5028 ARGS_SIZE_RTX (arg->locate.alignment_pad), false);
5030 /* Unless this is a partially-in-register argument, the argument is now
5031 in the stack.
5033 ??? Unlike the case above, in which we want the actual
5034 address of the data, so that we can load it directly into a
5035 register, here we want the address of the stack slot, so that
5036 it's properly aligned for word-by-word copying or something
5037 like that. It's not clear that this is always correct. */
5038 if (partial == 0)
5039 arg->value = arg->stack_slot;
5042 if (arg->reg && GET_CODE (arg->reg) == PARALLEL)
5044 tree type = TREE_TYPE (arg->tree_value);
5045 arg->parallel_value
5046 = emit_group_load_into_temps (arg->reg, arg->value, type,
5047 int_size_in_bytes (type));
5050 /* Mark all slots this store used. */
5051 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
5052 && argblock && ! variable_size && arg->stack)
5053 for (i = lower_bound; i < upper_bound; i++)
5054 stack_usage_map[i] = 1;
5056 /* Once we have pushed something, pops can't safely
5057 be deferred during the rest of the arguments. */
5058 NO_DEFER_POP;
5060 /* Free any temporary slots made in processing this argument. */
5061 pop_temp_slots ();
5063 return sibcall_failure;
5066 /* Nonzero if we do not know how to pass TYPE solely in registers. */
5068 bool
5069 must_pass_in_stack_var_size (machine_mode mode ATTRIBUTE_UNUSED,
5070 const_tree type)
5072 if (!type)
5073 return false;
5075 /* If the type has variable size... */
5076 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5077 return true;
5079 /* If the type is marked as addressable (it is required
5080 to be constructed into the stack)... */
5081 if (TREE_ADDRESSABLE (type))
5082 return true;
5084 return false;
5087 /* Another version of the TARGET_MUST_PASS_IN_STACK hook. This one
5088 takes trailing padding of a structure into account. */
5089 /* ??? Should be able to merge these two by examining BLOCK_REG_PADDING. */
5091 bool
5092 must_pass_in_stack_var_size_or_pad (machine_mode mode, const_tree type)
5094 if (!type)
5095 return false;
5097 /* If the type has variable size... */
5098 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5099 return true;
5101 /* If the type is marked as addressable (it is required
5102 to be constructed into the stack)... */
5103 if (TREE_ADDRESSABLE (type))
5104 return true;
5106 /* If the padding and mode of the type is such that a copy into
5107 a register would put it into the wrong part of the register. */
5108 if (mode == BLKmode
5109 && int_size_in_bytes (type) % (PARM_BOUNDARY / BITS_PER_UNIT)
5110 && (FUNCTION_ARG_PADDING (mode, type)
5111 == (BYTES_BIG_ENDIAN ? upward : downward)))
5112 return true;
5114 return false;