loop splitting
[gcc.git] / gcc / calls.c
1 /* Convert function calls to rtl insns, for GNU C compiler.
2 Copyright (C) 1989-2016 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "predict.h"
29 #include "memmodel.h"
30 #include "tm_p.h"
31 #include "stringpool.h"
32 #include "expmed.h"
33 #include "optabs.h"
34 #include "emit-rtl.h"
35 #include "cgraph.h"
36 #include "diagnostic-core.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "internal-fn.h"
41 #include "dojump.h"
42 #include "explow.h"
43 #include "calls.h"
44 #include "expr.h"
45 #include "output.h"
46 #include "langhooks.h"
47 #include "except.h"
48 #include "dbgcnt.h"
49 #include "rtl-iter.h"
50 #include "tree-chkp.h"
51 #include "rtl-chkp.h"
52
53
54 /* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits. */
55 #define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
56
57 /* Data structure and subroutines used within expand_call. */
58
59 struct arg_data
60 {
61 /* Tree node for this argument. */
62 tree tree_value;
63 /* Mode for value; TYPE_MODE unless promoted. */
64 machine_mode mode;
65 /* Current RTL value for argument, or 0 if it isn't precomputed. */
66 rtx value;
67 /* Initially-compute RTL value for argument; only for const functions. */
68 rtx initial_value;
69 /* Register to pass this argument in, 0 if passed on stack, or an
70 PARALLEL if the arg is to be copied into multiple non-contiguous
71 registers. */
72 rtx reg;
73 /* Register to pass this argument in when generating tail call sequence.
74 This is not the same register as for normal calls on machines with
75 register windows. */
76 rtx tail_call_reg;
77 /* If REG is a PARALLEL, this is a copy of VALUE pulled into the correct
78 form for emit_group_move. */
79 rtx parallel_value;
80 /* If value is passed in neither reg nor stack, this field holds a number
81 of a special slot to be used. */
82 rtx special_slot;
83 /* For pointer bounds hold an index of parm bounds are bound to. -1 if
84 there is no such pointer. */
85 int pointer_arg;
86 /* If pointer_arg refers a structure, then pointer_offset holds an offset
87 of a pointer in this structure. */
88 int pointer_offset;
89 /* If REG was promoted from the actual mode of the argument expression,
90 indicates whether the promotion is sign- or zero-extended. */
91 int unsignedp;
92 /* Number of bytes to put in registers. 0 means put the whole arg
93 in registers. Also 0 if not passed in registers. */
94 int partial;
95 /* Nonzero if argument must be passed on stack.
96 Note that some arguments may be passed on the stack
97 even though pass_on_stack is zero, just because FUNCTION_ARG says so.
98 pass_on_stack identifies arguments that *cannot* go in registers. */
99 int pass_on_stack;
100 /* Some fields packaged up for locate_and_pad_parm. */
101 struct locate_and_pad_arg_data locate;
102 /* Location on the stack at which parameter should be stored. The store
103 has already been done if STACK == VALUE. */
104 rtx stack;
105 /* Location on the stack of the start of this argument slot. This can
106 differ from STACK if this arg pads downward. This location is known
107 to be aligned to TARGET_FUNCTION_ARG_BOUNDARY. */
108 rtx stack_slot;
109 /* Place that this stack area has been saved, if needed. */
110 rtx save_area;
111 /* If an argument's alignment does not permit direct copying into registers,
112 copy in smaller-sized pieces into pseudos. These are stored in a
113 block pointed to by this field. The next field says how many
114 word-sized pseudos we made. */
115 rtx *aligned_regs;
116 int n_aligned_regs;
117 };
118
119 /* A vector of one char per byte of stack space. A byte if nonzero if
120 the corresponding stack location has been used.
121 This vector is used to prevent a function call within an argument from
122 clobbering any stack already set up. */
123 static char *stack_usage_map;
124
125 /* Size of STACK_USAGE_MAP. */
126 static int highest_outgoing_arg_in_use;
127
128 /* A bitmap of virtual-incoming stack space. Bit is set if the corresponding
129 stack location's tail call argument has been already stored into the stack.
130 This bitmap is used to prevent sibling call optimization if function tries
131 to use parent's incoming argument slots when they have been already
132 overwritten with tail call arguments. */
133 static sbitmap stored_args_map;
134
135 /* stack_arg_under_construction is nonzero when an argument may be
136 initialized with a constructor call (including a C function that
137 returns a BLKmode struct) and expand_call must take special action
138 to make sure the object being constructed does not overlap the
139 argument list for the constructor call. */
140 static int stack_arg_under_construction;
141
142 static void emit_call_1 (rtx, tree, tree, tree, HOST_WIDE_INT, HOST_WIDE_INT,
143 HOST_WIDE_INT, rtx, rtx, int, rtx, int,
144 cumulative_args_t);
145 static void precompute_register_parameters (int, struct arg_data *, int *);
146 static void store_bounds (struct arg_data *, struct arg_data *);
147 static int store_one_arg (struct arg_data *, rtx, int, int, int);
148 static void store_unaligned_arguments_into_pseudos (struct arg_data *, int);
149 static int finalize_must_preallocate (int, int, struct arg_data *,
150 struct args_size *);
151 static void precompute_arguments (int, struct arg_data *);
152 static int compute_argument_block_size (int, struct args_size *, tree, tree, int);
153 static void initialize_argument_information (int, struct arg_data *,
154 struct args_size *, int,
155 tree, tree,
156 tree, tree, cumulative_args_t, int,
157 rtx *, int *, int *, int *,
158 bool *, bool);
159 static void compute_argument_addresses (struct arg_data *, rtx, int);
160 static rtx rtx_for_function_call (tree, tree);
161 static void load_register_parameters (struct arg_data *, int, rtx *, int,
162 int, int *);
163 static rtx emit_library_call_value_1 (int, rtx, rtx, enum libcall_type,
164 machine_mode, int, va_list);
165 static int special_function_p (const_tree, int);
166 static int check_sibcall_argument_overlap_1 (rtx);
167 static int check_sibcall_argument_overlap (rtx_insn *, struct arg_data *, int);
168
169 static int combine_pending_stack_adjustment_and_call (int, struct args_size *,
170 unsigned int);
171 static tree split_complex_types (tree);
172
173 #ifdef REG_PARM_STACK_SPACE
174 static rtx save_fixed_argument_area (int, rtx, int *, int *);
175 static void restore_fixed_argument_area (rtx, rtx, int, int);
176 #endif
177 \f
178 /* Force FUNEXP into a form suitable for the address of a CALL,
179 and return that as an rtx. Also load the static chain register
180 if FNDECL is a nested function.
181
182 CALL_FUSAGE points to a variable holding the prospective
183 CALL_INSN_FUNCTION_USAGE information. */
184
185 rtx
186 prepare_call_address (tree fndecl_or_type, rtx funexp, rtx static_chain_value,
187 rtx *call_fusage, int reg_parm_seen, int flags)
188 {
189 /* Make a valid memory address and copy constants through pseudo-regs,
190 but not for a constant address if -fno-function-cse. */
191 if (GET_CODE (funexp) != SYMBOL_REF)
192 {
193 /* If it's an indirect call by descriptor, generate code to perform
194 runtime identification of the pointer and load the descriptor. */
195 if ((flags & ECF_BY_DESCRIPTOR) && !flag_trampolines)
196 {
197 const int bit_val = targetm.calls.custom_function_descriptors;
198 rtx call_lab = gen_label_rtx ();
199
200 gcc_assert (fndecl_or_type && TYPE_P (fndecl_or_type));
201 fndecl_or_type
202 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
203 fndecl_or_type);
204 DECL_STATIC_CHAIN (fndecl_or_type) = 1;
205 rtx chain = targetm.calls.static_chain (fndecl_or_type, false);
206
207 /* Avoid long live ranges around function calls. */
208 funexp = copy_to_mode_reg (Pmode, funexp);
209
210 if (REG_P (chain))
211 emit_insn (gen_rtx_CLOBBER (VOIDmode, chain));
212
213 /* Emit the runtime identification pattern. */
214 rtx mask = gen_rtx_AND (Pmode, funexp, GEN_INT (bit_val));
215 emit_cmp_and_jump_insns (mask, const0_rtx, EQ, NULL_RTX, Pmode, 1,
216 call_lab);
217
218 /* Statically predict the branch to very likely taken. */
219 rtx_insn *insn = get_last_insn ();
220 if (JUMP_P (insn))
221 predict_insn_def (insn, PRED_BUILTIN_EXPECT, TAKEN);
222
223 /* Load the descriptor. */
224 rtx mem = gen_rtx_MEM (ptr_mode,
225 plus_constant (Pmode, funexp, - bit_val));
226 MEM_NOTRAP_P (mem) = 1;
227 mem = convert_memory_address (Pmode, mem);
228 emit_move_insn (chain, mem);
229
230 mem = gen_rtx_MEM (ptr_mode,
231 plus_constant (Pmode, funexp,
232 POINTER_SIZE / BITS_PER_UNIT
233 - bit_val));
234 MEM_NOTRAP_P (mem) = 1;
235 mem = convert_memory_address (Pmode, mem);
236 emit_move_insn (funexp, mem);
237
238 emit_label (call_lab);
239
240 if (REG_P (chain))
241 {
242 use_reg (call_fusage, chain);
243 STATIC_CHAIN_REG_P (chain) = 1;
244 }
245
246 /* Make sure we're not going to be overwritten below. */
247 gcc_assert (!static_chain_value);
248 }
249
250 /* If we are using registers for parameters, force the
251 function address into a register now. */
252 funexp = ((reg_parm_seen
253 && targetm.small_register_classes_for_mode_p (FUNCTION_MODE))
254 ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
255 : memory_address (FUNCTION_MODE, funexp));
256 }
257 else
258 {
259 /* funexp could be a SYMBOL_REF represents a function pointer which is
260 of ptr_mode. In this case, it should be converted into address mode
261 to be a valid address for memory rtx pattern. See PR 64971. */
262 if (GET_MODE (funexp) != Pmode)
263 funexp = convert_memory_address (Pmode, funexp);
264
265 if (!(flags & ECF_SIBCALL))
266 {
267 if (!NO_FUNCTION_CSE && optimize && ! flag_no_function_cse)
268 funexp = force_reg (Pmode, funexp);
269 }
270 }
271
272 if (static_chain_value != 0
273 && (TREE_CODE (fndecl_or_type) != FUNCTION_DECL
274 || DECL_STATIC_CHAIN (fndecl_or_type)))
275 {
276 rtx chain;
277
278 chain = targetm.calls.static_chain (fndecl_or_type, false);
279 static_chain_value = convert_memory_address (Pmode, static_chain_value);
280
281 emit_move_insn (chain, static_chain_value);
282 if (REG_P (chain))
283 {
284 use_reg (call_fusage, chain);
285 STATIC_CHAIN_REG_P (chain) = 1;
286 }
287 }
288
289 return funexp;
290 }
291
292 /* Generate instructions to call function FUNEXP,
293 and optionally pop the results.
294 The CALL_INSN is the first insn generated.
295
296 FNDECL is the declaration node of the function. This is given to the
297 hook TARGET_RETURN_POPS_ARGS to determine whether this function pops
298 its own args.
299
300 FUNTYPE is the data type of the function. This is given to the hook
301 TARGET_RETURN_POPS_ARGS to determine whether this function pops its
302 own args. We used to allow an identifier for library functions, but
303 that doesn't work when the return type is an aggregate type and the
304 calling convention says that the pointer to this aggregate is to be
305 popped by the callee.
306
307 STACK_SIZE is the number of bytes of arguments on the stack,
308 ROUNDED_STACK_SIZE is that number rounded up to
309 PREFERRED_STACK_BOUNDARY; zero if the size is variable. This is
310 both to put into the call insn and to generate explicit popping
311 code if necessary.
312
313 STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
314 It is zero if this call doesn't want a structure value.
315
316 NEXT_ARG_REG is the rtx that results from executing
317 targetm.calls.function_arg (&args_so_far, VOIDmode, void_type_node, true)
318 just after all the args have had their registers assigned.
319 This could be whatever you like, but normally it is the first
320 arg-register beyond those used for args in this call,
321 or 0 if all the arg-registers are used in this call.
322 It is passed on to `gen_call' so you can put this info in the call insn.
323
324 VALREG is a hard register in which a value is returned,
325 or 0 if the call does not return a value.
326
327 OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
328 the args to this call were processed.
329 We restore `inhibit_defer_pop' to that value.
330
331 CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
332 denote registers used by the called function. */
333
334 static void
335 emit_call_1 (rtx funexp, tree fntree ATTRIBUTE_UNUSED, tree fndecl ATTRIBUTE_UNUSED,
336 tree funtype ATTRIBUTE_UNUSED,
337 HOST_WIDE_INT stack_size ATTRIBUTE_UNUSED,
338 HOST_WIDE_INT rounded_stack_size,
339 HOST_WIDE_INT struct_value_size ATTRIBUTE_UNUSED,
340 rtx next_arg_reg ATTRIBUTE_UNUSED, rtx valreg,
341 int old_inhibit_defer_pop, rtx call_fusage, int ecf_flags,
342 cumulative_args_t args_so_far ATTRIBUTE_UNUSED)
343 {
344 rtx rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
345 rtx call, funmem, pat;
346 int already_popped = 0;
347 HOST_WIDE_INT n_popped = 0;
348
349 /* Sibling call patterns never pop arguments (no sibcall(_value)_pop
350 patterns exist). Any popping that the callee does on return will
351 be from our caller's frame rather than ours. */
352 if (!(ecf_flags & ECF_SIBCALL))
353 {
354 n_popped += targetm.calls.return_pops_args (fndecl, funtype, stack_size);
355
356 #ifdef CALL_POPS_ARGS
357 n_popped += CALL_POPS_ARGS (*get_cumulative_args (args_so_far));
358 #endif
359 }
360
361 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
362 and we don't want to load it into a register as an optimization,
363 because prepare_call_address already did it if it should be done. */
364 if (GET_CODE (funexp) != SYMBOL_REF)
365 funexp = memory_address (FUNCTION_MODE, funexp);
366
367 funmem = gen_rtx_MEM (FUNCTION_MODE, funexp);
368 if (fndecl && TREE_CODE (fndecl) == FUNCTION_DECL)
369 {
370 tree t = fndecl;
371
372 /* Although a built-in FUNCTION_DECL and its non-__builtin
373 counterpart compare equal and get a shared mem_attrs, they
374 produce different dump output in compare-debug compilations,
375 if an entry gets garbage collected in one compilation, then
376 adds a different (but equivalent) entry, while the other
377 doesn't run the garbage collector at the same spot and then
378 shares the mem_attr with the equivalent entry. */
379 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
380 {
381 tree t2 = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
382 if (t2)
383 t = t2;
384 }
385
386 set_mem_expr (funmem, t);
387 }
388 else if (fntree)
389 set_mem_expr (funmem, build_simple_mem_ref (CALL_EXPR_FN (fntree)));
390
391 if (ecf_flags & ECF_SIBCALL)
392 {
393 if (valreg)
394 pat = targetm.gen_sibcall_value (valreg, funmem,
395 rounded_stack_size_rtx,
396 next_arg_reg, NULL_RTX);
397 else
398 pat = targetm.gen_sibcall (funmem, rounded_stack_size_rtx,
399 next_arg_reg, GEN_INT (struct_value_size));
400 }
401 /* If the target has "call" or "call_value" insns, then prefer them
402 if no arguments are actually popped. If the target does not have
403 "call" or "call_value" insns, then we must use the popping versions
404 even if the call has no arguments to pop. */
405 else if (n_popped > 0
406 || !(valreg
407 ? targetm.have_call_value ()
408 : targetm.have_call ()))
409 {
410 rtx n_pop = GEN_INT (n_popped);
411
412 /* If this subroutine pops its own args, record that in the call insn
413 if possible, for the sake of frame pointer elimination. */
414
415 if (valreg)
416 pat = targetm.gen_call_value_pop (valreg, funmem,
417 rounded_stack_size_rtx,
418 next_arg_reg, n_pop);
419 else
420 pat = targetm.gen_call_pop (funmem, rounded_stack_size_rtx,
421 next_arg_reg, n_pop);
422
423 already_popped = 1;
424 }
425 else
426 {
427 if (valreg)
428 pat = targetm.gen_call_value (valreg, funmem, rounded_stack_size_rtx,
429 next_arg_reg, NULL_RTX);
430 else
431 pat = targetm.gen_call (funmem, rounded_stack_size_rtx, next_arg_reg,
432 GEN_INT (struct_value_size));
433 }
434 emit_insn (pat);
435
436 /* Find the call we just emitted. */
437 rtx_call_insn *call_insn = last_call_insn ();
438
439 /* Some target create a fresh MEM instead of reusing the one provided
440 above. Set its MEM_EXPR. */
441 call = get_call_rtx_from (call_insn);
442 if (call
443 && MEM_EXPR (XEXP (call, 0)) == NULL_TREE
444 && MEM_EXPR (funmem) != NULL_TREE)
445 set_mem_expr (XEXP (call, 0), MEM_EXPR (funmem));
446
447 /* Mark instrumented calls. */
448 if (call && fntree)
449 CALL_EXPR_WITH_BOUNDS_P (call) = CALL_WITH_BOUNDS_P (fntree);
450
451 /* Put the register usage information there. */
452 add_function_usage_to (call_insn, call_fusage);
453
454 /* If this is a const call, then set the insn's unchanging bit. */
455 if (ecf_flags & ECF_CONST)
456 RTL_CONST_CALL_P (call_insn) = 1;
457
458 /* If this is a pure call, then set the insn's unchanging bit. */
459 if (ecf_flags & ECF_PURE)
460 RTL_PURE_CALL_P (call_insn) = 1;
461
462 /* If this is a const call, then set the insn's unchanging bit. */
463 if (ecf_flags & ECF_LOOPING_CONST_OR_PURE)
464 RTL_LOOPING_CONST_OR_PURE_CALL_P (call_insn) = 1;
465
466 /* Create a nothrow REG_EH_REGION note, if needed. */
467 make_reg_eh_region_note (call_insn, ecf_flags, 0);
468
469 if (ecf_flags & ECF_NORETURN)
470 add_reg_note (call_insn, REG_NORETURN, const0_rtx);
471
472 if (ecf_flags & ECF_RETURNS_TWICE)
473 {
474 add_reg_note (call_insn, REG_SETJMP, const0_rtx);
475 cfun->calls_setjmp = 1;
476 }
477
478 SIBLING_CALL_P (call_insn) = ((ecf_flags & ECF_SIBCALL) != 0);
479
480 /* Restore this now, so that we do defer pops for this call's args
481 if the context of the call as a whole permits. */
482 inhibit_defer_pop = old_inhibit_defer_pop;
483
484 if (n_popped > 0)
485 {
486 if (!already_popped)
487 CALL_INSN_FUNCTION_USAGE (call_insn)
488 = gen_rtx_EXPR_LIST (VOIDmode,
489 gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx),
490 CALL_INSN_FUNCTION_USAGE (call_insn));
491 rounded_stack_size -= n_popped;
492 rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
493 stack_pointer_delta -= n_popped;
494
495 add_reg_note (call_insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
496
497 /* If popup is needed, stack realign must use DRAP */
498 if (SUPPORTS_STACK_ALIGNMENT)
499 crtl->need_drap = true;
500 }
501 /* For noreturn calls when not accumulating outgoing args force
502 REG_ARGS_SIZE note to prevent crossjumping of calls with different
503 args sizes. */
504 else if (!ACCUMULATE_OUTGOING_ARGS && (ecf_flags & ECF_NORETURN) != 0)
505 add_reg_note (call_insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
506
507 if (!ACCUMULATE_OUTGOING_ARGS)
508 {
509 /* If returning from the subroutine does not automatically pop the args,
510 we need an instruction to pop them sooner or later.
511 Perhaps do it now; perhaps just record how much space to pop later.
512
513 If returning from the subroutine does pop the args, indicate that the
514 stack pointer will be changed. */
515
516 if (rounded_stack_size != 0)
517 {
518 if (ecf_flags & ECF_NORETURN)
519 /* Just pretend we did the pop. */
520 stack_pointer_delta -= rounded_stack_size;
521 else if (flag_defer_pop && inhibit_defer_pop == 0
522 && ! (ecf_flags & (ECF_CONST | ECF_PURE)))
523 pending_stack_adjust += rounded_stack_size;
524 else
525 adjust_stack (rounded_stack_size_rtx);
526 }
527 }
528 /* When we accumulate outgoing args, we must avoid any stack manipulations.
529 Restore the stack pointer to its original value now. Usually
530 ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
531 On i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
532 popping variants of functions exist as well.
533
534 ??? We may optimize similar to defer_pop above, but it is
535 probably not worthwhile.
536
537 ??? It will be worthwhile to enable combine_stack_adjustments even for
538 such machines. */
539 else if (n_popped)
540 anti_adjust_stack (GEN_INT (n_popped));
541 }
542
543 /* Determine if the function identified by FNDECL is one with
544 special properties we wish to know about. Modify FLAGS accordingly.
545
546 For example, if the function might return more than one time (setjmp), then
547 set ECF_RETURNS_TWICE.
548
549 Set ECF_MAY_BE_ALLOCA for any memory allocation function that might allocate
550 space from the stack such as alloca. */
551
552 static int
553 special_function_p (const_tree fndecl, int flags)
554 {
555 tree name_decl = DECL_NAME (fndecl);
556
557 /* For instrumentation clones we want to derive flags
558 from the original name. */
559 if (cgraph_node::get (fndecl)
560 && cgraph_node::get (fndecl)->instrumentation_clone)
561 name_decl = DECL_NAME (cgraph_node::get (fndecl)->orig_decl);
562
563 if (fndecl && name_decl
564 && IDENTIFIER_LENGTH (name_decl) <= 11
565 /* Exclude functions not at the file scope, or not `extern',
566 since they are not the magic functions we would otherwise
567 think they are.
568 FIXME: this should be handled with attributes, not with this
569 hacky imitation of DECL_ASSEMBLER_NAME. It's (also) wrong
570 because you can declare fork() inside a function if you
571 wish. */
572 && (DECL_CONTEXT (fndecl) == NULL_TREE
573 || TREE_CODE (DECL_CONTEXT (fndecl)) == TRANSLATION_UNIT_DECL)
574 && TREE_PUBLIC (fndecl))
575 {
576 const char *name = IDENTIFIER_POINTER (name_decl);
577 const char *tname = name;
578
579 /* We assume that alloca will always be called by name. It
580 makes no sense to pass it as a pointer-to-function to
581 anything that does not understand its behavior. */
582 if (IDENTIFIER_LENGTH (name_decl) == 6
583 && name[0] == 'a'
584 && ! strcmp (name, "alloca"))
585 flags |= ECF_MAY_BE_ALLOCA;
586
587 /* Disregard prefix _ or __. */
588 if (name[0] == '_')
589 {
590 if (name[1] == '_')
591 tname += 2;
592 else
593 tname += 1;
594 }
595
596 /* ECF_RETURNS_TWICE is safe even for -ffreestanding. */
597 if (! strcmp (tname, "setjmp")
598 || ! strcmp (tname, "sigsetjmp")
599 || ! strcmp (name, "savectx")
600 || ! strcmp (name, "vfork")
601 || ! strcmp (name, "getcontext"))
602 flags |= ECF_RETURNS_TWICE;
603 }
604
605 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
606 switch (DECL_FUNCTION_CODE (fndecl))
607 {
608 case BUILT_IN_ALLOCA:
609 case BUILT_IN_ALLOCA_WITH_ALIGN:
610 flags |= ECF_MAY_BE_ALLOCA;
611 break;
612 default:
613 break;
614 }
615
616 return flags;
617 }
618
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)
623 {
624 tree attr;
625 tree type = TREE_TYPE (fndecl);
626 if (!type)
627 return 0;
628
629 attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
630 if (!attr)
631 return 0;
632
633 attr = TREE_VALUE (TREE_VALUE (attr));
634 if (!attr || TREE_STRING_LENGTH (attr) < 1)
635 return 0;
636
637 switch (TREE_STRING_POINTER (attr)[0])
638 {
639 case '1':
640 case '2':
641 case '3':
642 case '4':
643 return ERF_RETURNS_ARG | (TREE_STRING_POINTER (attr)[0] - '1');
644
645 case 'm':
646 return ERF_NOALIAS;
647
648 case '.':
649 default:
650 return 0;
651 }
652 }
653
654 /* Return nonzero when FNDECL represents a call to setjmp. */
655
656 int
657 setjmp_call_p (const_tree fndecl)
658 {
659 if (DECL_IS_RETURNS_TWICE (fndecl))
660 return ECF_RETURNS_TWICE;
661 return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
662 }
663
664
665 /* Return true if STMT may be an alloca call. */
666
667 bool
668 gimple_maybe_alloca_call_p (const gimple *stmt)
669 {
670 tree fndecl;
671
672 if (!is_gimple_call (stmt))
673 return false;
674
675 fndecl = gimple_call_fndecl (stmt);
676 if (fndecl && (special_function_p (fndecl, 0) & ECF_MAY_BE_ALLOCA))
677 return true;
678
679 return false;
680 }
681
682 /* Return true if STMT is a builtin alloca call. */
683
684 bool
685 gimple_alloca_call_p (const gimple *stmt)
686 {
687 tree fndecl;
688
689 if (!is_gimple_call (stmt))
690 return false;
691
692 fndecl = gimple_call_fndecl (stmt);
693 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
694 switch (DECL_FUNCTION_CODE (fndecl))
695 {
696 case BUILT_IN_ALLOCA:
697 case BUILT_IN_ALLOCA_WITH_ALIGN:
698 return true;
699 default:
700 break;
701 }
702
703 return false;
704 }
705
706 /* Return true when exp contains a builtin alloca call. */
707
708 bool
709 alloca_call_p (const_tree exp)
710 {
711 tree fndecl;
712 if (TREE_CODE (exp) == CALL_EXPR
713 && (fndecl = get_callee_fndecl (exp))
714 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
715 switch (DECL_FUNCTION_CODE (fndecl))
716 {
717 case BUILT_IN_ALLOCA:
718 case BUILT_IN_ALLOCA_WITH_ALIGN:
719 return true;
720 default:
721 break;
722 }
723
724 return false;
725 }
726
727 /* Return TRUE if FNDECL is either a TM builtin or a TM cloned
728 function. Return FALSE otherwise. */
729
730 static bool
731 is_tm_builtin (const_tree fndecl)
732 {
733 if (fndecl == NULL)
734 return false;
735
736 if (decl_is_tm_clone (fndecl))
737 return true;
738
739 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
740 {
741 switch (DECL_FUNCTION_CODE (fndecl))
742 {
743 case BUILT_IN_TM_COMMIT:
744 case BUILT_IN_TM_COMMIT_EH:
745 case BUILT_IN_TM_ABORT:
746 case BUILT_IN_TM_IRREVOCABLE:
747 case BUILT_IN_TM_GETTMCLONE_IRR:
748 case BUILT_IN_TM_MEMCPY:
749 case BUILT_IN_TM_MEMMOVE:
750 case BUILT_IN_TM_MEMSET:
751 CASE_BUILT_IN_TM_STORE (1):
752 CASE_BUILT_IN_TM_STORE (2):
753 CASE_BUILT_IN_TM_STORE (4):
754 CASE_BUILT_IN_TM_STORE (8):
755 CASE_BUILT_IN_TM_STORE (FLOAT):
756 CASE_BUILT_IN_TM_STORE (DOUBLE):
757 CASE_BUILT_IN_TM_STORE (LDOUBLE):
758 CASE_BUILT_IN_TM_STORE (M64):
759 CASE_BUILT_IN_TM_STORE (M128):
760 CASE_BUILT_IN_TM_STORE (M256):
761 CASE_BUILT_IN_TM_LOAD (1):
762 CASE_BUILT_IN_TM_LOAD (2):
763 CASE_BUILT_IN_TM_LOAD (4):
764 CASE_BUILT_IN_TM_LOAD (8):
765 CASE_BUILT_IN_TM_LOAD (FLOAT):
766 CASE_BUILT_IN_TM_LOAD (DOUBLE):
767 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
768 CASE_BUILT_IN_TM_LOAD (M64):
769 CASE_BUILT_IN_TM_LOAD (M128):
770 CASE_BUILT_IN_TM_LOAD (M256):
771 case BUILT_IN_TM_LOG:
772 case BUILT_IN_TM_LOG_1:
773 case BUILT_IN_TM_LOG_2:
774 case BUILT_IN_TM_LOG_4:
775 case BUILT_IN_TM_LOG_8:
776 case BUILT_IN_TM_LOG_FLOAT:
777 case BUILT_IN_TM_LOG_DOUBLE:
778 case BUILT_IN_TM_LOG_LDOUBLE:
779 case BUILT_IN_TM_LOG_M64:
780 case BUILT_IN_TM_LOG_M128:
781 case BUILT_IN_TM_LOG_M256:
782 return true;
783 default:
784 break;
785 }
786 }
787 return false;
788 }
789
790 /* Detect flags (function attributes) from the function decl or type node. */
791
792 int
793 flags_from_decl_or_type (const_tree exp)
794 {
795 int flags = 0;
796
797 if (DECL_P (exp))
798 {
799 /* The function exp may have the `malloc' attribute. */
800 if (DECL_IS_MALLOC (exp))
801 flags |= ECF_MALLOC;
802
803 /* The function exp may have the `returns_twice' attribute. */
804 if (DECL_IS_RETURNS_TWICE (exp))
805 flags |= ECF_RETURNS_TWICE;
806
807 /* Process the pure and const attributes. */
808 if (TREE_READONLY (exp))
809 flags |= ECF_CONST;
810 if (DECL_PURE_P (exp))
811 flags |= ECF_PURE;
812 if (DECL_LOOPING_CONST_OR_PURE_P (exp))
813 flags |= ECF_LOOPING_CONST_OR_PURE;
814
815 if (DECL_IS_NOVOPS (exp))
816 flags |= ECF_NOVOPS;
817 if (lookup_attribute ("leaf", DECL_ATTRIBUTES (exp)))
818 flags |= ECF_LEAF;
819
820 if (TREE_NOTHROW (exp))
821 flags |= ECF_NOTHROW;
822
823 if (flag_tm)
824 {
825 if (is_tm_builtin (exp))
826 flags |= ECF_TM_BUILTIN;
827 else if ((flags & (ECF_CONST|ECF_NOVOPS)) != 0
828 || lookup_attribute ("transaction_pure",
829 TYPE_ATTRIBUTES (TREE_TYPE (exp))))
830 flags |= ECF_TM_PURE;
831 }
832
833 flags = special_function_p (exp, flags);
834 }
835 else if (TYPE_P (exp))
836 {
837 if (TYPE_READONLY (exp))
838 flags |= ECF_CONST;
839
840 if (flag_tm
841 && ((flags & ECF_CONST) != 0
842 || lookup_attribute ("transaction_pure", TYPE_ATTRIBUTES (exp))))
843 flags |= ECF_TM_PURE;
844 }
845 else
846 gcc_unreachable ();
847
848 if (TREE_THIS_VOLATILE (exp))
849 {
850 flags |= ECF_NORETURN;
851 if (flags & (ECF_CONST|ECF_PURE))
852 flags |= ECF_LOOPING_CONST_OR_PURE;
853 }
854
855 return flags;
856 }
857
858 /* Detect flags from a CALL_EXPR. */
859
860 int
861 call_expr_flags (const_tree t)
862 {
863 int flags;
864 tree decl = get_callee_fndecl (t);
865
866 if (decl)
867 flags = flags_from_decl_or_type (decl);
868 else if (CALL_EXPR_FN (t) == NULL_TREE)
869 flags = internal_fn_flags (CALL_EXPR_IFN (t));
870 else
871 {
872 tree type = TREE_TYPE (CALL_EXPR_FN (t));
873 if (type && TREE_CODE (type) == POINTER_TYPE)
874 flags = flags_from_decl_or_type (TREE_TYPE (type));
875 else
876 flags = 0;
877 if (CALL_EXPR_BY_DESCRIPTOR (t))
878 flags |= ECF_BY_DESCRIPTOR;
879 }
880
881 return flags;
882 }
883
884 /* Return true if TYPE should be passed by invisible reference. */
885
886 bool
887 pass_by_reference (CUMULATIVE_ARGS *ca, machine_mode mode,
888 tree type, bool named_arg)
889 {
890 if (type)
891 {
892 /* If this type contains non-trivial constructors, then it is
893 forbidden for the middle-end to create any new copies. */
894 if (TREE_ADDRESSABLE (type))
895 return true;
896
897 /* GCC post 3.4 passes *all* variable sized types by reference. */
898 if (!TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
899 return true;
900
901 /* If a record type should be passed the same as its first (and only)
902 member, use the type and mode of that member. */
903 if (TREE_CODE (type) == RECORD_TYPE && TYPE_TRANSPARENT_AGGR (type))
904 {
905 type = TREE_TYPE (first_field (type));
906 mode = TYPE_MODE (type);
907 }
908 }
909
910 return targetm.calls.pass_by_reference (pack_cumulative_args (ca), mode,
911 type, named_arg);
912 }
913
914 /* Return true if TYPE, which is passed by reference, should be callee
915 copied instead of caller copied. */
916
917 bool
918 reference_callee_copied (CUMULATIVE_ARGS *ca, machine_mode mode,
919 tree type, bool named_arg)
920 {
921 if (type && TREE_ADDRESSABLE (type))
922 return false;
923 return targetm.calls.callee_copies (pack_cumulative_args (ca), mode, type,
924 named_arg);
925 }
926
927
928 /* Precompute all register parameters as described by ARGS, storing values
929 into fields within the ARGS array.
930
931 NUM_ACTUALS indicates the total number elements in the ARGS array.
932
933 Set REG_PARM_SEEN if we encounter a register parameter. */
934
935 static void
936 precompute_register_parameters (int num_actuals, struct arg_data *args,
937 int *reg_parm_seen)
938 {
939 int i;
940
941 *reg_parm_seen = 0;
942
943 for (i = 0; i < num_actuals; i++)
944 if (args[i].reg != 0 && ! args[i].pass_on_stack)
945 {
946 *reg_parm_seen = 1;
947
948 if (args[i].value == 0)
949 {
950 push_temp_slots ();
951 args[i].value = expand_normal (args[i].tree_value);
952 preserve_temp_slots (args[i].value);
953 pop_temp_slots ();
954 }
955
956 /* If we are to promote the function arg to a wider mode,
957 do it now. */
958
959 if (args[i].mode != TYPE_MODE (TREE_TYPE (args[i].tree_value)))
960 args[i].value
961 = convert_modes (args[i].mode,
962 TYPE_MODE (TREE_TYPE (args[i].tree_value)),
963 args[i].value, args[i].unsignedp);
964
965 /* If the value is a non-legitimate constant, force it into a
966 pseudo now. TLS symbols sometimes need a call to resolve. */
967 if (CONSTANT_P (args[i].value)
968 && !targetm.legitimate_constant_p (args[i].mode, args[i].value))
969 args[i].value = force_reg (args[i].mode, args[i].value);
970
971 /* If we're going to have to load the value by parts, pull the
972 parts into pseudos. The part extraction process can involve
973 non-trivial computation. */
974 if (GET_CODE (args[i].reg) == PARALLEL)
975 {
976 tree type = TREE_TYPE (args[i].tree_value);
977 args[i].parallel_value
978 = emit_group_load_into_temps (args[i].reg, args[i].value,
979 type, int_size_in_bytes (type));
980 }
981
982 /* If the value is expensive, and we are inside an appropriately
983 short loop, put the value into a pseudo and then put the pseudo
984 into the hard reg.
985
986 For small register classes, also do this if this call uses
987 register parameters. This is to avoid reload conflicts while
988 loading the parameters registers. */
989
990 else if ((! (REG_P (args[i].value)
991 || (GET_CODE (args[i].value) == SUBREG
992 && REG_P (SUBREG_REG (args[i].value)))))
993 && args[i].mode != BLKmode
994 && (set_src_cost (args[i].value, args[i].mode,
995 optimize_insn_for_speed_p ())
996 > COSTS_N_INSNS (1))
997 && ((*reg_parm_seen
998 && targetm.small_register_classes_for_mode_p (args[i].mode))
999 || optimize))
1000 args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
1001 }
1002 }
1003
1004 #ifdef REG_PARM_STACK_SPACE
1005
1006 /* The argument list is the property of the called routine and it
1007 may clobber it. If the fixed area has been used for previous
1008 parameters, we must save and restore it. */
1009
1010 static rtx
1011 save_fixed_argument_area (int reg_parm_stack_space, rtx argblock, int *low_to_save, int *high_to_save)
1012 {
1013 int low;
1014 int high;
1015
1016 /* Compute the boundary of the area that needs to be saved, if any. */
1017 high = reg_parm_stack_space;
1018 if (ARGS_GROW_DOWNWARD)
1019 high += 1;
1020
1021 if (high > highest_outgoing_arg_in_use)
1022 high = highest_outgoing_arg_in_use;
1023
1024 for (low = 0; low < high; low++)
1025 if (stack_usage_map[low] != 0)
1026 {
1027 int num_to_save;
1028 machine_mode save_mode;
1029 int delta;
1030 rtx addr;
1031 rtx stack_area;
1032 rtx save_area;
1033
1034 while (stack_usage_map[--high] == 0)
1035 ;
1036
1037 *low_to_save = low;
1038 *high_to_save = high;
1039
1040 num_to_save = high - low + 1;
1041 save_mode = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
1042
1043 /* If we don't have the required alignment, must do this
1044 in BLKmode. */
1045 if ((low & (MIN (GET_MODE_SIZE (save_mode),
1046 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
1047 save_mode = BLKmode;
1048
1049 if (ARGS_GROW_DOWNWARD)
1050 delta = -high;
1051 else
1052 delta = low;
1053
1054 addr = plus_constant (Pmode, argblock, delta);
1055 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
1056
1057 set_mem_align (stack_area, PARM_BOUNDARY);
1058 if (save_mode == BLKmode)
1059 {
1060 save_area = assign_stack_temp (BLKmode, num_to_save);
1061 emit_block_move (validize_mem (save_area), stack_area,
1062 GEN_INT (num_to_save), BLOCK_OP_CALL_PARM);
1063 }
1064 else
1065 {
1066 save_area = gen_reg_rtx (save_mode);
1067 emit_move_insn (save_area, stack_area);
1068 }
1069
1070 return save_area;
1071 }
1072
1073 return NULL_RTX;
1074 }
1075
1076 static void
1077 restore_fixed_argument_area (rtx save_area, rtx argblock, int high_to_save, int low_to_save)
1078 {
1079 machine_mode save_mode = GET_MODE (save_area);
1080 int delta;
1081 rtx addr, stack_area;
1082
1083 if (ARGS_GROW_DOWNWARD)
1084 delta = -high_to_save;
1085 else
1086 delta = low_to_save;
1087
1088 addr = plus_constant (Pmode, argblock, delta);
1089 stack_area = gen_rtx_MEM (save_mode, memory_address (save_mode, addr));
1090 set_mem_align (stack_area, PARM_BOUNDARY);
1091
1092 if (save_mode != BLKmode)
1093 emit_move_insn (stack_area, save_area);
1094 else
1095 emit_block_move (stack_area, validize_mem (save_area),
1096 GEN_INT (high_to_save - low_to_save + 1),
1097 BLOCK_OP_CALL_PARM);
1098 }
1099 #endif /* REG_PARM_STACK_SPACE */
1100
1101 /* If any elements in ARGS refer to parameters that are to be passed in
1102 registers, but not in memory, and whose alignment does not permit a
1103 direct copy into registers. Copy the values into a group of pseudos
1104 which we will later copy into the appropriate hard registers.
1105
1106 Pseudos for each unaligned argument will be stored into the array
1107 args[argnum].aligned_regs. The caller is responsible for deallocating
1108 the aligned_regs array if it is nonzero. */
1109
1110 static void
1111 store_unaligned_arguments_into_pseudos (struct arg_data *args, int num_actuals)
1112 {
1113 int i, j;
1114
1115 for (i = 0; i < num_actuals; i++)
1116 if (args[i].reg != 0 && ! args[i].pass_on_stack
1117 && GET_CODE (args[i].reg) != PARALLEL
1118 && args[i].mode == BLKmode
1119 && MEM_P (args[i].value)
1120 && (MEM_ALIGN (args[i].value)
1121 < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
1122 {
1123 int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1124 int endian_correction = 0;
1125
1126 if (args[i].partial)
1127 {
1128 gcc_assert (args[i].partial % UNITS_PER_WORD == 0);
1129 args[i].n_aligned_regs = args[i].partial / UNITS_PER_WORD;
1130 }
1131 else
1132 {
1133 args[i].n_aligned_regs
1134 = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1135 }
1136
1137 args[i].aligned_regs = XNEWVEC (rtx, args[i].n_aligned_regs);
1138
1139 /* Structures smaller than a word are normally aligned to the
1140 least significant byte. On a BYTES_BIG_ENDIAN machine,
1141 this means we must skip the empty high order bytes when
1142 calculating the bit offset. */
1143 if (bytes < UNITS_PER_WORD
1144 #ifdef BLOCK_REG_PADDING
1145 && (BLOCK_REG_PADDING (args[i].mode,
1146 TREE_TYPE (args[i].tree_value), 1)
1147 == downward)
1148 #else
1149 && BYTES_BIG_ENDIAN
1150 #endif
1151 )
1152 endian_correction = BITS_PER_WORD - bytes * BITS_PER_UNIT;
1153
1154 for (j = 0; j < args[i].n_aligned_regs; j++)
1155 {
1156 rtx reg = gen_reg_rtx (word_mode);
1157 rtx word = operand_subword_force (args[i].value, j, BLKmode);
1158 int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
1159
1160 args[i].aligned_regs[j] = reg;
1161 word = extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
1162 word_mode, word_mode, false);
1163
1164 /* There is no need to restrict this code to loading items
1165 in TYPE_ALIGN sized hunks. The bitfield instructions can
1166 load up entire word sized registers efficiently.
1167
1168 ??? This may not be needed anymore.
1169 We use to emit a clobber here but that doesn't let later
1170 passes optimize the instructions we emit. By storing 0 into
1171 the register later passes know the first AND to zero out the
1172 bitfield being set in the register is unnecessary. The store
1173 of 0 will be deleted as will at least the first AND. */
1174
1175 emit_move_insn (reg, const0_rtx);
1176
1177 bytes -= bitsize / BITS_PER_UNIT;
1178 store_bit_field (reg, bitsize, endian_correction, 0, 0,
1179 word_mode, word, false);
1180 }
1181 }
1182 }
1183
1184 /* Issue an error if CALL_EXPR was flagged as requiring
1185 tall-call optimization. */
1186
1187 static void
1188 maybe_complain_about_tail_call (tree call_expr, const char *reason)
1189 {
1190 gcc_assert (TREE_CODE (call_expr) == CALL_EXPR);
1191 if (!CALL_EXPR_MUST_TAIL_CALL (call_expr))
1192 return;
1193
1194 error_at (EXPR_LOCATION (call_expr), "cannot tail-call: %s", reason);
1195 }
1196
1197 /* Fill in ARGS_SIZE and ARGS array based on the parameters found in
1198 CALL_EXPR EXP.
1199
1200 NUM_ACTUALS is the total number of parameters.
1201
1202 N_NAMED_ARGS is the total number of named arguments.
1203
1204 STRUCT_VALUE_ADDR_VALUE is the implicit argument for a struct return
1205 value, or null.
1206
1207 FNDECL is the tree code for the target of this call (if known)
1208
1209 ARGS_SO_FAR holds state needed by the target to know where to place
1210 the next argument.
1211
1212 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
1213 for arguments which are passed in registers.
1214
1215 OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
1216 and may be modified by this routine.
1217
1218 OLD_PENDING_ADJ, MUST_PREALLOCATE and FLAGS are pointers to integer
1219 flags which may be modified by this routine.
1220
1221 MAY_TAILCALL is cleared if we encounter an invisible pass-by-reference
1222 that requires allocation of stack space.
1223
1224 CALL_FROM_THUNK_P is true if this call is the jump from a thunk to
1225 the thunked-to function. */
1226
1227 static void
1228 initialize_argument_information (int num_actuals ATTRIBUTE_UNUSED,
1229 struct arg_data *args,
1230 struct args_size *args_size,
1231 int n_named_args ATTRIBUTE_UNUSED,
1232 tree exp, tree struct_value_addr_value,
1233 tree fndecl, tree fntype,
1234 cumulative_args_t args_so_far,
1235 int reg_parm_stack_space,
1236 rtx *old_stack_level, int *old_pending_adj,
1237 int *must_preallocate, int *ecf_flags,
1238 bool *may_tailcall, bool call_from_thunk_p)
1239 {
1240 CUMULATIVE_ARGS *args_so_far_pnt = get_cumulative_args (args_so_far);
1241 location_t loc = EXPR_LOCATION (exp);
1242
1243 /* Count arg position in order args appear. */
1244 int argpos;
1245
1246 int i;
1247
1248 args_size->constant = 0;
1249 args_size->var = 0;
1250
1251 bitmap_obstack_initialize (NULL);
1252
1253 /* In this loop, we consider args in the order they are written.
1254 We fill up ARGS from the back. */
1255
1256 i = num_actuals - 1;
1257 {
1258 int j = i, ptr_arg = -1;
1259 call_expr_arg_iterator iter;
1260 tree arg;
1261 bitmap slots = NULL;
1262
1263 if (struct_value_addr_value)
1264 {
1265 args[j].tree_value = struct_value_addr_value;
1266 j--;
1267
1268 /* If we pass structure address then we need to
1269 create bounds for it. Since created bounds is
1270 a call statement, we expand it right here to avoid
1271 fixing all other places where it may be expanded. */
1272 if (CALL_WITH_BOUNDS_P (exp))
1273 {
1274 args[j].value = gen_reg_rtx (targetm.chkp_bound_mode ());
1275 args[j].tree_value
1276 = chkp_make_bounds_for_struct_addr (struct_value_addr_value);
1277 expand_expr_real (args[j].tree_value, args[j].value, VOIDmode,
1278 EXPAND_NORMAL, 0, false);
1279 args[j].pointer_arg = j + 1;
1280 j--;
1281 }
1282 }
1283 argpos = 0;
1284 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
1285 {
1286 tree argtype = TREE_TYPE (arg);
1287
1288 /* Remember last param with pointer and associate it
1289 with following pointer bounds. */
1290 if (CALL_WITH_BOUNDS_P (exp)
1291 && chkp_type_has_pointer (argtype))
1292 {
1293 if (slots)
1294 BITMAP_FREE (slots);
1295 ptr_arg = j;
1296 if (!BOUNDED_TYPE_P (argtype))
1297 {
1298 slots = BITMAP_ALLOC (NULL);
1299 chkp_find_bound_slots (argtype, slots);
1300 }
1301 }
1302 else if (CALL_WITH_BOUNDS_P (exp)
1303 && pass_by_reference (NULL, TYPE_MODE (argtype), argtype,
1304 argpos < n_named_args))
1305 {
1306 if (slots)
1307 BITMAP_FREE (slots);
1308 ptr_arg = j;
1309 }
1310 else if (POINTER_BOUNDS_TYPE_P (argtype))
1311 {
1312 /* We expect bounds in instrumented calls only.
1313 Otherwise it is a sign we lost flag due to some optimization
1314 and may emit call args incorrectly. */
1315 gcc_assert (CALL_WITH_BOUNDS_P (exp));
1316
1317 /* For structures look for the next available pointer. */
1318 if (ptr_arg != -1 && slots)
1319 {
1320 unsigned bnd_no = bitmap_first_set_bit (slots);
1321 args[j].pointer_offset =
1322 bnd_no * POINTER_SIZE / BITS_PER_UNIT;
1323
1324 bitmap_clear_bit (slots, bnd_no);
1325
1326 /* Check we have no more pointers in the structure. */
1327 if (bitmap_empty_p (slots))
1328 BITMAP_FREE (slots);
1329 }
1330 args[j].pointer_arg = ptr_arg;
1331
1332 /* Check we covered all pointers in the previous
1333 non bounds arg. */
1334 if (!slots)
1335 ptr_arg = -1;
1336 }
1337 else
1338 ptr_arg = -1;
1339
1340 if (targetm.calls.split_complex_arg
1341 && argtype
1342 && TREE_CODE (argtype) == COMPLEX_TYPE
1343 && targetm.calls.split_complex_arg (argtype))
1344 {
1345 tree subtype = TREE_TYPE (argtype);
1346 args[j].tree_value = build1 (REALPART_EXPR, subtype, arg);
1347 j--;
1348 args[j].tree_value = build1 (IMAGPART_EXPR, subtype, arg);
1349 }
1350 else
1351 args[j].tree_value = arg;
1352 j--;
1353 argpos++;
1354 }
1355
1356 if (slots)
1357 BITMAP_FREE (slots);
1358 }
1359
1360 bitmap_obstack_release (NULL);
1361
1362 /* I counts args in order (to be) pushed; ARGPOS counts in order written. */
1363 for (argpos = 0; argpos < num_actuals; i--, argpos++)
1364 {
1365 tree type = TREE_TYPE (args[i].tree_value);
1366 int unsignedp;
1367 machine_mode mode;
1368
1369 /* Replace erroneous argument with constant zero. */
1370 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
1371 args[i].tree_value = integer_zero_node, type = integer_type_node;
1372
1373 /* If TYPE is a transparent union or record, pass things the way
1374 we would pass the first field of the union or record. We have
1375 already verified that the modes are the same. */
1376 if ((TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == RECORD_TYPE)
1377 && TYPE_TRANSPARENT_AGGR (type))
1378 type = TREE_TYPE (first_field (type));
1379
1380 /* Decide where to pass this arg.
1381
1382 args[i].reg is nonzero if all or part is passed in registers.
1383
1384 args[i].partial is nonzero if part but not all is passed in registers,
1385 and the exact value says how many bytes are passed in registers.
1386
1387 args[i].pass_on_stack is nonzero if the argument must at least be
1388 computed on the stack. It may then be loaded back into registers
1389 if args[i].reg is nonzero.
1390
1391 These decisions are driven by the FUNCTION_... macros and must agree
1392 with those made by function.c. */
1393
1394 /* See if this argument should be passed by invisible reference. */
1395 if (pass_by_reference (args_so_far_pnt, TYPE_MODE (type),
1396 type, argpos < n_named_args))
1397 {
1398 bool callee_copies;
1399 tree base = NULL_TREE;
1400
1401 callee_copies
1402 = reference_callee_copied (args_so_far_pnt, TYPE_MODE (type),
1403 type, argpos < n_named_args);
1404
1405 /* If we're compiling a thunk, pass through invisible references
1406 instead of making a copy. */
1407 if (call_from_thunk_p
1408 || (callee_copies
1409 && !TREE_ADDRESSABLE (type)
1410 && (base = get_base_address (args[i].tree_value))
1411 && TREE_CODE (base) != SSA_NAME
1412 && (!DECL_P (base) || MEM_P (DECL_RTL (base)))))
1413 {
1414 /* We may have turned the parameter value into an SSA name.
1415 Go back to the original parameter so we can take the
1416 address. */
1417 if (TREE_CODE (args[i].tree_value) == SSA_NAME)
1418 {
1419 gcc_assert (SSA_NAME_IS_DEFAULT_DEF (args[i].tree_value));
1420 args[i].tree_value = SSA_NAME_VAR (args[i].tree_value);
1421 gcc_assert (TREE_CODE (args[i].tree_value) == PARM_DECL);
1422 }
1423 /* Argument setup code may have copied the value to register. We
1424 revert that optimization now because the tail call code must
1425 use the original location. */
1426 if (TREE_CODE (args[i].tree_value) == PARM_DECL
1427 && !MEM_P (DECL_RTL (args[i].tree_value))
1428 && DECL_INCOMING_RTL (args[i].tree_value)
1429 && MEM_P (DECL_INCOMING_RTL (args[i].tree_value)))
1430 set_decl_rtl (args[i].tree_value,
1431 DECL_INCOMING_RTL (args[i].tree_value));
1432
1433 mark_addressable (args[i].tree_value);
1434
1435 /* We can't use sibcalls if a callee-copied argument is
1436 stored in the current function's frame. */
1437 if (!call_from_thunk_p && DECL_P (base) && !TREE_STATIC (base))
1438 {
1439 *may_tailcall = false;
1440 maybe_complain_about_tail_call (exp,
1441 "a callee-copied argument is"
1442 " stored in the current "
1443 " function's frame");
1444 }
1445
1446 args[i].tree_value = build_fold_addr_expr_loc (loc,
1447 args[i].tree_value);
1448 type = TREE_TYPE (args[i].tree_value);
1449
1450 if (*ecf_flags & ECF_CONST)
1451 *ecf_flags &= ~(ECF_CONST | ECF_LOOPING_CONST_OR_PURE);
1452 }
1453 else
1454 {
1455 /* We make a copy of the object and pass the address to the
1456 function being called. */
1457 rtx copy;
1458
1459 if (!COMPLETE_TYPE_P (type)
1460 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
1461 || (flag_stack_check == GENERIC_STACK_CHECK
1462 && compare_tree_int (TYPE_SIZE_UNIT (type),
1463 STACK_CHECK_MAX_VAR_SIZE) > 0))
1464 {
1465 /* This is a variable-sized object. Make space on the stack
1466 for it. */
1467 rtx size_rtx = expr_size (args[i].tree_value);
1468
1469 if (*old_stack_level == 0)
1470 {
1471 emit_stack_save (SAVE_BLOCK, old_stack_level);
1472 *old_pending_adj = pending_stack_adjust;
1473 pending_stack_adjust = 0;
1474 }
1475
1476 /* We can pass TRUE as the 4th argument because we just
1477 saved the stack pointer and will restore it right after
1478 the call. */
1479 copy = allocate_dynamic_stack_space (size_rtx,
1480 TYPE_ALIGN (type),
1481 TYPE_ALIGN (type),
1482 true);
1483 copy = gen_rtx_MEM (BLKmode, copy);
1484 set_mem_attributes (copy, type, 1);
1485 }
1486 else
1487 copy = assign_temp (type, 1, 0);
1488
1489 store_expr (args[i].tree_value, copy, 0, false, false);
1490
1491 /* Just change the const function to pure and then let
1492 the next test clear the pure based on
1493 callee_copies. */
1494 if (*ecf_flags & ECF_CONST)
1495 {
1496 *ecf_flags &= ~ECF_CONST;
1497 *ecf_flags |= ECF_PURE;
1498 }
1499
1500 if (!callee_copies && *ecf_flags & ECF_PURE)
1501 *ecf_flags &= ~(ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
1502
1503 args[i].tree_value
1504 = build_fold_addr_expr_loc (loc, make_tree (type, copy));
1505 type = TREE_TYPE (args[i].tree_value);
1506 *may_tailcall = false;
1507 maybe_complain_about_tail_call (exp,
1508 "argument must be passed"
1509 " by copying");
1510 }
1511 }
1512
1513 unsignedp = TYPE_UNSIGNED (type);
1514 mode = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
1515 fndecl ? TREE_TYPE (fndecl) : fntype, 0);
1516
1517 args[i].unsignedp = unsignedp;
1518 args[i].mode = mode;
1519
1520 args[i].reg = targetm.calls.function_arg (args_so_far, mode, type,
1521 argpos < n_named_args);
1522
1523 if (args[i].reg && CONST_INT_P (args[i].reg))
1524 {
1525 args[i].special_slot = args[i].reg;
1526 args[i].reg = NULL;
1527 }
1528
1529 /* If this is a sibling call and the machine has register windows, the
1530 register window has to be unwinded before calling the routine, so
1531 arguments have to go into the incoming registers. */
1532 if (targetm.calls.function_incoming_arg != targetm.calls.function_arg)
1533 args[i].tail_call_reg
1534 = targetm.calls.function_incoming_arg (args_so_far, mode, type,
1535 argpos < n_named_args);
1536 else
1537 args[i].tail_call_reg = args[i].reg;
1538
1539 if (args[i].reg)
1540 args[i].partial
1541 = targetm.calls.arg_partial_bytes (args_so_far, mode, type,
1542 argpos < n_named_args);
1543
1544 args[i].pass_on_stack = targetm.calls.must_pass_in_stack (mode, type);
1545
1546 /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
1547 it means that we are to pass this arg in the register(s) designated
1548 by the PARALLEL, but also to pass it in the stack. */
1549 if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
1550 && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
1551 args[i].pass_on_stack = 1;
1552
1553 /* If this is an addressable type, we must preallocate the stack
1554 since we must evaluate the object into its final location.
1555
1556 If this is to be passed in both registers and the stack, it is simpler
1557 to preallocate. */
1558 if (TREE_ADDRESSABLE (type)
1559 || (args[i].pass_on_stack && args[i].reg != 0))
1560 *must_preallocate = 1;
1561
1562 /* No stack allocation and padding for bounds. */
1563 if (POINTER_BOUNDS_P (args[i].tree_value))
1564 ;
1565 /* Compute the stack-size of this argument. */
1566 else if (args[i].reg == 0 || args[i].partial != 0
1567 || reg_parm_stack_space > 0
1568 || args[i].pass_on_stack)
1569 locate_and_pad_parm (mode, type,
1570 #ifdef STACK_PARMS_IN_REG_PARM_AREA
1571 1,
1572 #else
1573 args[i].reg != 0,
1574 #endif
1575 reg_parm_stack_space,
1576 args[i].pass_on_stack ? 0 : args[i].partial,
1577 fndecl, args_size, &args[i].locate);
1578 #ifdef BLOCK_REG_PADDING
1579 else
1580 /* The argument is passed entirely in registers. See at which
1581 end it should be padded. */
1582 args[i].locate.where_pad =
1583 BLOCK_REG_PADDING (mode, type,
1584 int_size_in_bytes (type) <= UNITS_PER_WORD);
1585 #endif
1586
1587 /* Update ARGS_SIZE, the total stack space for args so far. */
1588
1589 args_size->constant += args[i].locate.size.constant;
1590 if (args[i].locate.size.var)
1591 ADD_PARM_SIZE (*args_size, args[i].locate.size.var);
1592
1593 /* Increment ARGS_SO_FAR, which has info about which arg-registers
1594 have been used, etc. */
1595
1596 targetm.calls.function_arg_advance (args_so_far, TYPE_MODE (type),
1597 type, argpos < n_named_args);
1598 }
1599 }
1600
1601 /* Update ARGS_SIZE to contain the total size for the argument block.
1602 Return the original constant component of the argument block's size.
1603
1604 REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
1605 for arguments passed in registers. */
1606
1607 static int
1608 compute_argument_block_size (int reg_parm_stack_space,
1609 struct args_size *args_size,
1610 tree fndecl ATTRIBUTE_UNUSED,
1611 tree fntype ATTRIBUTE_UNUSED,
1612 int preferred_stack_boundary ATTRIBUTE_UNUSED)
1613 {
1614 int unadjusted_args_size = args_size->constant;
1615
1616 /* For accumulate outgoing args mode we don't need to align, since the frame
1617 will be already aligned. Align to STACK_BOUNDARY in order to prevent
1618 backends from generating misaligned frame sizes. */
1619 if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
1620 preferred_stack_boundary = STACK_BOUNDARY;
1621
1622 /* Compute the actual size of the argument block required. The variable
1623 and constant sizes must be combined, the size may have to be rounded,
1624 and there may be a minimum required size. */
1625
1626 if (args_size->var)
1627 {
1628 args_size->var = ARGS_SIZE_TREE (*args_size);
1629 args_size->constant = 0;
1630
1631 preferred_stack_boundary /= BITS_PER_UNIT;
1632 if (preferred_stack_boundary > 1)
1633 {
1634 /* We don't handle this case yet. To handle it correctly we have
1635 to add the delta, round and subtract the delta.
1636 Currently no machine description requires this support. */
1637 gcc_assert (!(stack_pointer_delta & (preferred_stack_boundary - 1)));
1638 args_size->var = round_up (args_size->var, preferred_stack_boundary);
1639 }
1640
1641 if (reg_parm_stack_space > 0)
1642 {
1643 args_size->var
1644 = size_binop (MAX_EXPR, args_size->var,
1645 ssize_int (reg_parm_stack_space));
1646
1647 /* The area corresponding to register parameters is not to count in
1648 the size of the block we need. So make the adjustment. */
1649 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1650 args_size->var
1651 = size_binop (MINUS_EXPR, args_size->var,
1652 ssize_int (reg_parm_stack_space));
1653 }
1654 }
1655 else
1656 {
1657 preferred_stack_boundary /= BITS_PER_UNIT;
1658 if (preferred_stack_boundary < 1)
1659 preferred_stack_boundary = 1;
1660 args_size->constant = (((args_size->constant
1661 + stack_pointer_delta
1662 + preferred_stack_boundary - 1)
1663 / preferred_stack_boundary
1664 * preferred_stack_boundary)
1665 - stack_pointer_delta);
1666
1667 args_size->constant = MAX (args_size->constant,
1668 reg_parm_stack_space);
1669
1670 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
1671 args_size->constant -= reg_parm_stack_space;
1672 }
1673 return unadjusted_args_size;
1674 }
1675
1676 /* Precompute parameters as needed for a function call.
1677
1678 FLAGS is mask of ECF_* constants.
1679
1680 NUM_ACTUALS is the number of arguments.
1681
1682 ARGS is an array containing information for each argument; this
1683 routine fills in the INITIAL_VALUE and VALUE fields for each
1684 precomputed argument. */
1685
1686 static void
1687 precompute_arguments (int num_actuals, struct arg_data *args)
1688 {
1689 int i;
1690
1691 /* If this is a libcall, then precompute all arguments so that we do not
1692 get extraneous instructions emitted as part of the libcall sequence. */
1693
1694 /* If we preallocated the stack space, and some arguments must be passed
1695 on the stack, then we must precompute any parameter which contains a
1696 function call which will store arguments on the stack.
1697 Otherwise, evaluating the parameter may clobber previous parameters
1698 which have already been stored into the stack. (we have code to avoid
1699 such case by saving the outgoing stack arguments, but it results in
1700 worse code) */
1701 if (!ACCUMULATE_OUTGOING_ARGS)
1702 return;
1703
1704 for (i = 0; i < num_actuals; i++)
1705 {
1706 tree type;
1707 machine_mode mode;
1708
1709 if (TREE_CODE (args[i].tree_value) != CALL_EXPR)
1710 continue;
1711
1712 /* If this is an addressable type, we cannot pre-evaluate it. */
1713 type = TREE_TYPE (args[i].tree_value);
1714 gcc_assert (!TREE_ADDRESSABLE (type));
1715
1716 args[i].initial_value = args[i].value
1717 = expand_normal (args[i].tree_value);
1718
1719 mode = TYPE_MODE (type);
1720 if (mode != args[i].mode)
1721 {
1722 int unsignedp = args[i].unsignedp;
1723 args[i].value
1724 = convert_modes (args[i].mode, mode,
1725 args[i].value, args[i].unsignedp);
1726
1727 /* CSE will replace this only if it contains args[i].value
1728 pseudo, so convert it down to the declared mode using
1729 a SUBREG. */
1730 if (REG_P (args[i].value)
1731 && GET_MODE_CLASS (args[i].mode) == MODE_INT
1732 && promote_mode (type, mode, &unsignedp) != args[i].mode)
1733 {
1734 args[i].initial_value
1735 = gen_lowpart_SUBREG (mode, args[i].value);
1736 SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1737 SUBREG_PROMOTED_SET (args[i].initial_value, args[i].unsignedp);
1738 }
1739 }
1740 }
1741 }
1742
1743 /* Given the current state of MUST_PREALLOCATE and information about
1744 arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
1745 compute and return the final value for MUST_PREALLOCATE. */
1746
1747 static int
1748 finalize_must_preallocate (int must_preallocate, int num_actuals,
1749 struct arg_data *args, struct args_size *args_size)
1750 {
1751 /* See if we have or want to preallocate stack space.
1752
1753 If we would have to push a partially-in-regs parm
1754 before other stack parms, preallocate stack space instead.
1755
1756 If the size of some parm is not a multiple of the required stack
1757 alignment, we must preallocate.
1758
1759 If the total size of arguments that would otherwise create a copy in
1760 a temporary (such as a CALL) is more than half the total argument list
1761 size, preallocation is faster.
1762
1763 Another reason to preallocate is if we have a machine (like the m88k)
1764 where stack alignment is required to be maintained between every
1765 pair of insns, not just when the call is made. However, we assume here
1766 that such machines either do not have push insns (and hence preallocation
1767 would occur anyway) or the problem is taken care of with
1768 PUSH_ROUNDING. */
1769
1770 if (! must_preallocate)
1771 {
1772 int partial_seen = 0;
1773 int copy_to_evaluate_size = 0;
1774 int i;
1775
1776 for (i = 0; i < num_actuals && ! must_preallocate; i++)
1777 {
1778 if (args[i].partial > 0 && ! args[i].pass_on_stack)
1779 partial_seen = 1;
1780 else if (partial_seen && args[i].reg == 0)
1781 must_preallocate = 1;
1782 /* We preallocate in case there are bounds passed
1783 in the bounds table to have precomputed address
1784 for bounds association. */
1785 else if (POINTER_BOUNDS_P (args[i].tree_value)
1786 && !args[i].reg)
1787 must_preallocate = 1;
1788
1789 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1790 && (TREE_CODE (args[i].tree_value) == CALL_EXPR
1791 || TREE_CODE (args[i].tree_value) == TARGET_EXPR
1792 || TREE_CODE (args[i].tree_value) == COND_EXPR
1793 || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
1794 copy_to_evaluate_size
1795 += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1796 }
1797
1798 if (copy_to_evaluate_size * 2 >= args_size->constant
1799 && args_size->constant > 0)
1800 must_preallocate = 1;
1801 }
1802 return must_preallocate;
1803 }
1804
1805 /* If we preallocated stack space, compute the address of each argument
1806 and store it into the ARGS array.
1807
1808 We need not ensure it is a valid memory address here; it will be
1809 validized when it is used.
1810
1811 ARGBLOCK is an rtx for the address of the outgoing arguments. */
1812
1813 static void
1814 compute_argument_addresses (struct arg_data *args, rtx argblock, int num_actuals)
1815 {
1816 if (argblock)
1817 {
1818 rtx arg_reg = argblock;
1819 int i, arg_offset = 0;
1820
1821 if (GET_CODE (argblock) == PLUS)
1822 arg_reg = XEXP (argblock, 0), arg_offset = INTVAL (XEXP (argblock, 1));
1823
1824 for (i = 0; i < num_actuals; i++)
1825 {
1826 rtx offset = ARGS_SIZE_RTX (args[i].locate.offset);
1827 rtx slot_offset = ARGS_SIZE_RTX (args[i].locate.slot_offset);
1828 rtx addr;
1829 unsigned int align, boundary;
1830 unsigned int units_on_stack = 0;
1831 machine_mode partial_mode = VOIDmode;
1832
1833 /* Skip this parm if it will not be passed on the stack. */
1834 if (! args[i].pass_on_stack
1835 && args[i].reg != 0
1836 && args[i].partial == 0)
1837 continue;
1838
1839 /* Pointer Bounds are never passed on the stack. */
1840 if (POINTER_BOUNDS_P (args[i].tree_value))
1841 continue;
1842
1843 if (CONST_INT_P (offset))
1844 addr = plus_constant (Pmode, arg_reg, INTVAL (offset));
1845 else
1846 addr = gen_rtx_PLUS (Pmode, arg_reg, offset);
1847
1848 addr = plus_constant (Pmode, addr, arg_offset);
1849
1850 if (args[i].partial != 0)
1851 {
1852 /* Only part of the parameter is being passed on the stack.
1853 Generate a simple memory reference of the correct size. */
1854 units_on_stack = args[i].locate.size.constant;
1855 partial_mode = mode_for_size (units_on_stack * BITS_PER_UNIT,
1856 MODE_INT, 1);
1857 args[i].stack = gen_rtx_MEM (partial_mode, addr);
1858 set_mem_size (args[i].stack, units_on_stack);
1859 }
1860 else
1861 {
1862 args[i].stack = gen_rtx_MEM (args[i].mode, addr);
1863 set_mem_attributes (args[i].stack,
1864 TREE_TYPE (args[i].tree_value), 1);
1865 }
1866 align = BITS_PER_UNIT;
1867 boundary = args[i].locate.boundary;
1868 if (args[i].locate.where_pad != downward)
1869 align = boundary;
1870 else if (CONST_INT_P (offset))
1871 {
1872 align = INTVAL (offset) * BITS_PER_UNIT | boundary;
1873 align = least_bit_hwi (align);
1874 }
1875 set_mem_align (args[i].stack, align);
1876
1877 if (CONST_INT_P (slot_offset))
1878 addr = plus_constant (Pmode, arg_reg, INTVAL (slot_offset));
1879 else
1880 addr = gen_rtx_PLUS (Pmode, arg_reg, slot_offset);
1881
1882 addr = plus_constant (Pmode, addr, arg_offset);
1883
1884 if (args[i].partial != 0)
1885 {
1886 /* Only part of the parameter is being passed on the stack.
1887 Generate a simple memory reference of the correct size.
1888 */
1889 args[i].stack_slot = gen_rtx_MEM (partial_mode, addr);
1890 set_mem_size (args[i].stack_slot, units_on_stack);
1891 }
1892 else
1893 {
1894 args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
1895 set_mem_attributes (args[i].stack_slot,
1896 TREE_TYPE (args[i].tree_value), 1);
1897 }
1898 set_mem_align (args[i].stack_slot, args[i].locate.boundary);
1899
1900 /* Function incoming arguments may overlap with sibling call
1901 outgoing arguments and we cannot allow reordering of reads
1902 from function arguments with stores to outgoing arguments
1903 of sibling calls. */
1904 set_mem_alias_set (args[i].stack, 0);
1905 set_mem_alias_set (args[i].stack_slot, 0);
1906 }
1907 }
1908 }
1909
1910 /* Given a FNDECL and EXP, return an rtx suitable for use as a target address
1911 in a call instruction.
1912
1913 FNDECL is the tree node for the target function. For an indirect call
1914 FNDECL will be NULL_TREE.
1915
1916 ADDR is the operand 0 of CALL_EXPR for this call. */
1917
1918 static rtx
1919 rtx_for_function_call (tree fndecl, tree addr)
1920 {
1921 rtx funexp;
1922
1923 /* Get the function to call, in the form of RTL. */
1924 if (fndecl)
1925 {
1926 if (!TREE_USED (fndecl) && fndecl != current_function_decl)
1927 TREE_USED (fndecl) = 1;
1928
1929 /* Get a SYMBOL_REF rtx for the function address. */
1930 funexp = XEXP (DECL_RTL (fndecl), 0);
1931 }
1932 else
1933 /* Generate an rtx (probably a pseudo-register) for the address. */
1934 {
1935 push_temp_slots ();
1936 funexp = expand_normal (addr);
1937 pop_temp_slots (); /* FUNEXP can't be BLKmode. */
1938 }
1939 return funexp;
1940 }
1941
1942 /* Internal state for internal_arg_pointer_based_exp and its helpers. */
1943 static struct
1944 {
1945 /* Last insn that has been scanned by internal_arg_pointer_based_exp_scan,
1946 or NULL_RTX if none has been scanned yet. */
1947 rtx_insn *scan_start;
1948 /* Vector indexed by REGNO - FIRST_PSEUDO_REGISTER, recording if a pseudo is
1949 based on crtl->args.internal_arg_pointer. The element is NULL_RTX if the
1950 pseudo isn't based on it, a CONST_INT offset if the pseudo is based on it
1951 with fixed offset, or PC if this is with variable or unknown offset. */
1952 vec<rtx> cache;
1953 } internal_arg_pointer_exp_state;
1954
1955 static rtx internal_arg_pointer_based_exp (const_rtx, bool);
1956
1957 /* Helper function for internal_arg_pointer_based_exp. Scan insns in
1958 the tail call sequence, starting with first insn that hasn't been
1959 scanned yet, and note for each pseudo on the LHS whether it is based
1960 on crtl->args.internal_arg_pointer or not, and what offset from that
1961 that pointer it has. */
1962
1963 static void
1964 internal_arg_pointer_based_exp_scan (void)
1965 {
1966 rtx_insn *insn, *scan_start = internal_arg_pointer_exp_state.scan_start;
1967
1968 if (scan_start == NULL_RTX)
1969 insn = get_insns ();
1970 else
1971 insn = NEXT_INSN (scan_start);
1972
1973 while (insn)
1974 {
1975 rtx set = single_set (insn);
1976 if (set && REG_P (SET_DEST (set)) && !HARD_REGISTER_P (SET_DEST (set)))
1977 {
1978 rtx val = NULL_RTX;
1979 unsigned int idx = REGNO (SET_DEST (set)) - FIRST_PSEUDO_REGISTER;
1980 /* Punt on pseudos set multiple times. */
1981 if (idx < internal_arg_pointer_exp_state.cache.length ()
1982 && (internal_arg_pointer_exp_state.cache[idx]
1983 != NULL_RTX))
1984 val = pc_rtx;
1985 else
1986 val = internal_arg_pointer_based_exp (SET_SRC (set), false);
1987 if (val != NULL_RTX)
1988 {
1989 if (idx >= internal_arg_pointer_exp_state.cache.length ())
1990 internal_arg_pointer_exp_state.cache
1991 .safe_grow_cleared (idx + 1);
1992 internal_arg_pointer_exp_state.cache[idx] = val;
1993 }
1994 }
1995 if (NEXT_INSN (insn) == NULL_RTX)
1996 scan_start = insn;
1997 insn = NEXT_INSN (insn);
1998 }
1999
2000 internal_arg_pointer_exp_state.scan_start = scan_start;
2001 }
2002
2003 /* Compute whether RTL is based on crtl->args.internal_arg_pointer. Return
2004 NULL_RTX if RTL isn't based on it, a CONST_INT offset if RTL is based on
2005 it with fixed offset, or PC if this is with variable or unknown offset.
2006 TOPLEVEL is true if the function is invoked at the topmost level. */
2007
2008 static rtx
2009 internal_arg_pointer_based_exp (const_rtx rtl, bool toplevel)
2010 {
2011 if (CONSTANT_P (rtl))
2012 return NULL_RTX;
2013
2014 if (rtl == crtl->args.internal_arg_pointer)
2015 return const0_rtx;
2016
2017 if (REG_P (rtl) && HARD_REGISTER_P (rtl))
2018 return NULL_RTX;
2019
2020 if (GET_CODE (rtl) == PLUS && CONST_INT_P (XEXP (rtl, 1)))
2021 {
2022 rtx val = internal_arg_pointer_based_exp (XEXP (rtl, 0), toplevel);
2023 if (val == NULL_RTX || val == pc_rtx)
2024 return val;
2025 return plus_constant (Pmode, val, INTVAL (XEXP (rtl, 1)));
2026 }
2027
2028 /* When called at the topmost level, scan pseudo assignments in between the
2029 last scanned instruction in the tail call sequence and the latest insn
2030 in that sequence. */
2031 if (toplevel)
2032 internal_arg_pointer_based_exp_scan ();
2033
2034 if (REG_P (rtl))
2035 {
2036 unsigned int idx = REGNO (rtl) - FIRST_PSEUDO_REGISTER;
2037 if (idx < internal_arg_pointer_exp_state.cache.length ())
2038 return internal_arg_pointer_exp_state.cache[idx];
2039
2040 return NULL_RTX;
2041 }
2042
2043 subrtx_iterator::array_type array;
2044 FOR_EACH_SUBRTX (iter, array, rtl, NONCONST)
2045 {
2046 const_rtx x = *iter;
2047 if (REG_P (x) && internal_arg_pointer_based_exp (x, false) != NULL_RTX)
2048 return pc_rtx;
2049 if (MEM_P (x))
2050 iter.skip_subrtxes ();
2051 }
2052
2053 return NULL_RTX;
2054 }
2055
2056 /* Return true if and only if SIZE storage units (usually bytes)
2057 starting from address ADDR overlap with already clobbered argument
2058 area. This function is used to determine if we should give up a
2059 sibcall. */
2060
2061 static bool
2062 mem_overlaps_already_clobbered_arg_p (rtx addr, unsigned HOST_WIDE_INT size)
2063 {
2064 HOST_WIDE_INT i;
2065 rtx val;
2066
2067 if (bitmap_empty_p (stored_args_map))
2068 return false;
2069 val = internal_arg_pointer_based_exp (addr, true);
2070 if (val == NULL_RTX)
2071 return false;
2072 else if (val == pc_rtx)
2073 return true;
2074 else
2075 i = INTVAL (val);
2076
2077 if (STACK_GROWS_DOWNWARD)
2078 i -= crtl->args.pretend_args_size;
2079 else
2080 i += crtl->args.pretend_args_size;
2081
2082
2083 if (ARGS_GROW_DOWNWARD)
2084 i = -i - size;
2085
2086 if (size > 0)
2087 {
2088 unsigned HOST_WIDE_INT k;
2089
2090 for (k = 0; k < size; k++)
2091 if (i + k < SBITMAP_SIZE (stored_args_map)
2092 && bitmap_bit_p (stored_args_map, i + k))
2093 return true;
2094 }
2095
2096 return false;
2097 }
2098
2099 /* Do the register loads required for any wholly-register parms or any
2100 parms which are passed both on the stack and in a register. Their
2101 expressions were already evaluated.
2102
2103 Mark all register-parms as living through the call, putting these USE
2104 insns in the CALL_INSN_FUNCTION_USAGE field.
2105
2106 When IS_SIBCALL, perform the check_sibcall_argument_overlap
2107 checking, setting *SIBCALL_FAILURE if appropriate. */
2108
2109 static void
2110 load_register_parameters (struct arg_data *args, int num_actuals,
2111 rtx *call_fusage, int flags, int is_sibcall,
2112 int *sibcall_failure)
2113 {
2114 int i, j;
2115
2116 for (i = 0; i < num_actuals; i++)
2117 {
2118 rtx reg = ((flags & ECF_SIBCALL)
2119 ? args[i].tail_call_reg : args[i].reg);
2120 if (reg)
2121 {
2122 int partial = args[i].partial;
2123 int nregs;
2124 int size = 0;
2125 rtx_insn *before_arg = get_last_insn ();
2126 /* Set non-negative if we must move a word at a time, even if
2127 just one word (e.g, partial == 4 && mode == DFmode). Set
2128 to -1 if we just use a normal move insn. This value can be
2129 zero if the argument is a zero size structure. */
2130 nregs = -1;
2131 if (GET_CODE (reg) == PARALLEL)
2132 ;
2133 else if (partial)
2134 {
2135 gcc_assert (partial % UNITS_PER_WORD == 0);
2136 nregs = partial / UNITS_PER_WORD;
2137 }
2138 else if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode)
2139 {
2140 size = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
2141 nregs = (size + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
2142 }
2143 else
2144 size = GET_MODE_SIZE (args[i].mode);
2145
2146 /* Handle calls that pass values in multiple non-contiguous
2147 locations. The Irix 6 ABI has examples of this. */
2148
2149 if (GET_CODE (reg) == PARALLEL)
2150 emit_group_move (reg, args[i].parallel_value);
2151
2152 /* If simple case, just do move. If normal partial, store_one_arg
2153 has already loaded the register for us. In all other cases,
2154 load the register(s) from memory. */
2155
2156 else if (nregs == -1)
2157 {
2158 emit_move_insn (reg, args[i].value);
2159 #ifdef BLOCK_REG_PADDING
2160 /* Handle case where we have a value that needs shifting
2161 up to the msb. eg. a QImode value and we're padding
2162 upward on a BYTES_BIG_ENDIAN machine. */
2163 if (size < UNITS_PER_WORD
2164 && (args[i].locate.where_pad
2165 == (BYTES_BIG_ENDIAN ? upward : downward)))
2166 {
2167 rtx x;
2168 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2169
2170 /* Assigning REG here rather than a temp makes CALL_FUSAGE
2171 report the whole reg as used. Strictly speaking, the
2172 call only uses SIZE bytes at the msb end, but it doesn't
2173 seem worth generating rtl to say that. */
2174 reg = gen_rtx_REG (word_mode, REGNO (reg));
2175 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
2176 if (x != reg)
2177 emit_move_insn (reg, x);
2178 }
2179 #endif
2180 }
2181
2182 /* If we have pre-computed the values to put in the registers in
2183 the case of non-aligned structures, copy them in now. */
2184
2185 else if (args[i].n_aligned_regs != 0)
2186 for (j = 0; j < args[i].n_aligned_regs; j++)
2187 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
2188 args[i].aligned_regs[j]);
2189
2190 else if (partial == 0 || args[i].pass_on_stack)
2191 {
2192 rtx mem = validize_mem (copy_rtx (args[i].value));
2193
2194 /* Check for overlap with already clobbered argument area,
2195 providing that this has non-zero size. */
2196 if (is_sibcall
2197 && size != 0
2198 && (mem_overlaps_already_clobbered_arg_p
2199 (XEXP (args[i].value, 0), size)))
2200 *sibcall_failure = 1;
2201
2202 if (size % UNITS_PER_WORD == 0
2203 || MEM_ALIGN (mem) % BITS_PER_WORD == 0)
2204 move_block_to_reg (REGNO (reg), mem, nregs, args[i].mode);
2205 else
2206 {
2207 if (nregs > 1)
2208 move_block_to_reg (REGNO (reg), mem, nregs - 1,
2209 args[i].mode);
2210 rtx dest = gen_rtx_REG (word_mode, REGNO (reg) + nregs - 1);
2211 unsigned int bitoff = (nregs - 1) * BITS_PER_WORD;
2212 unsigned int bitsize = size * BITS_PER_UNIT - bitoff;
2213 rtx x = extract_bit_field (mem, bitsize, bitoff, 1, dest,
2214 word_mode, word_mode, false);
2215 if (BYTES_BIG_ENDIAN)
2216 x = expand_shift (LSHIFT_EXPR, word_mode, x,
2217 BITS_PER_WORD - bitsize, dest, 1);
2218 if (x != dest)
2219 emit_move_insn (dest, x);
2220 }
2221
2222 /* Handle a BLKmode that needs shifting. */
2223 if (nregs == 1 && size < UNITS_PER_WORD
2224 #ifdef BLOCK_REG_PADDING
2225 && args[i].locate.where_pad == downward
2226 #else
2227 && BYTES_BIG_ENDIAN
2228 #endif
2229 )
2230 {
2231 rtx dest = gen_rtx_REG (word_mode, REGNO (reg));
2232 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
2233 enum tree_code dir = (BYTES_BIG_ENDIAN
2234 ? RSHIFT_EXPR : LSHIFT_EXPR);
2235 rtx x;
2236
2237 x = expand_shift (dir, word_mode, dest, shift, dest, 1);
2238 if (x != dest)
2239 emit_move_insn (dest, x);
2240 }
2241 }
2242
2243 /* When a parameter is a block, and perhaps in other cases, it is
2244 possible that it did a load from an argument slot that was
2245 already clobbered. */
2246 if (is_sibcall
2247 && check_sibcall_argument_overlap (before_arg, &args[i], 0))
2248 *sibcall_failure = 1;
2249
2250 /* Handle calls that pass values in multiple non-contiguous
2251 locations. The Irix 6 ABI has examples of this. */
2252 if (GET_CODE (reg) == PARALLEL)
2253 use_group_regs (call_fusage, reg);
2254 else if (nregs == -1)
2255 use_reg_mode (call_fusage, reg,
2256 TYPE_MODE (TREE_TYPE (args[i].tree_value)));
2257 else if (nregs > 0)
2258 use_regs (call_fusage, REGNO (reg), nregs);
2259 }
2260 }
2261 }
2262
2263 /* We need to pop PENDING_STACK_ADJUST bytes. But, if the arguments
2264 wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
2265 bytes, then we would need to push some additional bytes to pad the
2266 arguments. So, we compute an adjust to the stack pointer for an
2267 amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
2268 bytes. Then, when the arguments are pushed the stack will be perfectly
2269 aligned. ARGS_SIZE->CONSTANT is set to the number of bytes that should
2270 be popped after the call. Returns the adjustment. */
2271
2272 static int
2273 combine_pending_stack_adjustment_and_call (int unadjusted_args_size,
2274 struct args_size *args_size,
2275 unsigned int preferred_unit_stack_boundary)
2276 {
2277 /* The number of bytes to pop so that the stack will be
2278 under-aligned by UNADJUSTED_ARGS_SIZE bytes. */
2279 HOST_WIDE_INT adjustment;
2280 /* The alignment of the stack after the arguments are pushed, if we
2281 just pushed the arguments without adjust the stack here. */
2282 unsigned HOST_WIDE_INT unadjusted_alignment;
2283
2284 unadjusted_alignment
2285 = ((stack_pointer_delta + unadjusted_args_size)
2286 % preferred_unit_stack_boundary);
2287
2288 /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
2289 as possible -- leaving just enough left to cancel out the
2290 UNADJUSTED_ALIGNMENT. In other words, we want to ensure that the
2291 PENDING_STACK_ADJUST is non-negative, and congruent to
2292 -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY. */
2293
2294 /* Begin by trying to pop all the bytes. */
2295 unadjusted_alignment
2296 = (unadjusted_alignment
2297 - (pending_stack_adjust % preferred_unit_stack_boundary));
2298 adjustment = pending_stack_adjust;
2299 /* Push enough additional bytes that the stack will be aligned
2300 after the arguments are pushed. */
2301 if (preferred_unit_stack_boundary > 1)
2302 {
2303 if (unadjusted_alignment > 0)
2304 adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
2305 else
2306 adjustment += unadjusted_alignment;
2307 }
2308
2309 /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
2310 bytes after the call. The right number is the entire
2311 PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
2312 by the arguments in the first place. */
2313 args_size->constant
2314 = pending_stack_adjust - adjustment + unadjusted_args_size;
2315
2316 return adjustment;
2317 }
2318
2319 /* Scan X expression if it does not dereference any argument slots
2320 we already clobbered by tail call arguments (as noted in stored_args_map
2321 bitmap).
2322 Return nonzero if X expression dereferences such argument slots,
2323 zero otherwise. */
2324
2325 static int
2326 check_sibcall_argument_overlap_1 (rtx x)
2327 {
2328 RTX_CODE code;
2329 int i, j;
2330 const char *fmt;
2331
2332 if (x == NULL_RTX)
2333 return 0;
2334
2335 code = GET_CODE (x);
2336
2337 /* We need not check the operands of the CALL expression itself. */
2338 if (code == CALL)
2339 return 0;
2340
2341 if (code == MEM)
2342 return mem_overlaps_already_clobbered_arg_p (XEXP (x, 0),
2343 GET_MODE_SIZE (GET_MODE (x)));
2344
2345 /* Scan all subexpressions. */
2346 fmt = GET_RTX_FORMAT (code);
2347 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2348 {
2349 if (*fmt == 'e')
2350 {
2351 if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
2352 return 1;
2353 }
2354 else if (*fmt == 'E')
2355 {
2356 for (j = 0; j < XVECLEN (x, i); j++)
2357 if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
2358 return 1;
2359 }
2360 }
2361 return 0;
2362 }
2363
2364 /* Scan sequence after INSN if it does not dereference any argument slots
2365 we already clobbered by tail call arguments (as noted in stored_args_map
2366 bitmap). If MARK_STORED_ARGS_MAP, add stack slots for ARG to
2367 stored_args_map bitmap afterwards (when ARG is a register MARK_STORED_ARGS_MAP
2368 should be 0). Return nonzero if sequence after INSN dereferences such argument
2369 slots, zero otherwise. */
2370
2371 static int
2372 check_sibcall_argument_overlap (rtx_insn *insn, struct arg_data *arg,
2373 int mark_stored_args_map)
2374 {
2375 int low, high;
2376
2377 if (insn == NULL_RTX)
2378 insn = get_insns ();
2379 else
2380 insn = NEXT_INSN (insn);
2381
2382 for (; insn; insn = NEXT_INSN (insn))
2383 if (INSN_P (insn)
2384 && check_sibcall_argument_overlap_1 (PATTERN (insn)))
2385 break;
2386
2387 if (mark_stored_args_map)
2388 {
2389 if (ARGS_GROW_DOWNWARD)
2390 low = -arg->locate.slot_offset.constant - arg->locate.size.constant;
2391 else
2392 low = arg->locate.slot_offset.constant;
2393
2394 for (high = low + arg->locate.size.constant; low < high; low++)
2395 bitmap_set_bit (stored_args_map, low);
2396 }
2397 return insn != NULL_RTX;
2398 }
2399
2400 /* Given that a function returns a value of mode MODE at the most
2401 significant end of hard register VALUE, shift VALUE left or right
2402 as specified by LEFT_P. Return true if some action was needed. */
2403
2404 bool
2405 shift_return_value (machine_mode mode, bool left_p, rtx value)
2406 {
2407 HOST_WIDE_INT shift;
2408
2409 gcc_assert (REG_P (value) && HARD_REGISTER_P (value));
2410 shift = GET_MODE_BITSIZE (GET_MODE (value)) - GET_MODE_BITSIZE (mode);
2411 if (shift == 0)
2412 return false;
2413
2414 /* Use ashr rather than lshr for right shifts. This is for the benefit
2415 of the MIPS port, which requires SImode values to be sign-extended
2416 when stored in 64-bit registers. */
2417 if (!force_expand_binop (GET_MODE (value), left_p ? ashl_optab : ashr_optab,
2418 value, GEN_INT (shift), value, 1, OPTAB_WIDEN))
2419 gcc_unreachable ();
2420 return true;
2421 }
2422
2423 /* If X is a likely-spilled register value, copy it to a pseudo
2424 register and return that register. Return X otherwise. */
2425
2426 static rtx
2427 avoid_likely_spilled_reg (rtx x)
2428 {
2429 rtx new_rtx;
2430
2431 if (REG_P (x)
2432 && HARD_REGISTER_P (x)
2433 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (x))))
2434 {
2435 /* Make sure that we generate a REG rather than a CONCAT.
2436 Moves into CONCATs can need nontrivial instructions,
2437 and the whole point of this function is to avoid
2438 using the hard register directly in such a situation. */
2439 generating_concat_p = 0;
2440 new_rtx = gen_reg_rtx (GET_MODE (x));
2441 generating_concat_p = 1;
2442 emit_move_insn (new_rtx, x);
2443 return new_rtx;
2444 }
2445 return x;
2446 }
2447
2448 /* Helper function for expand_call.
2449 Return false is EXP is not implementable as a sibling call. */
2450
2451 static bool
2452 can_implement_as_sibling_call_p (tree exp,
2453 rtx structure_value_addr,
2454 tree funtype,
2455 int reg_parm_stack_space ATTRIBUTE_UNUSED,
2456 tree fndecl,
2457 int flags,
2458 tree addr,
2459 const args_size &args_size)
2460 {
2461 if (!targetm.have_sibcall_epilogue ())
2462 {
2463 maybe_complain_about_tail_call
2464 (exp,
2465 "machine description does not have"
2466 " a sibcall_epilogue instruction pattern");
2467 return false;
2468 }
2469
2470 /* Doing sibling call optimization needs some work, since
2471 structure_value_addr can be allocated on the stack.
2472 It does not seem worth the effort since few optimizable
2473 sibling calls will return a structure. */
2474 if (structure_value_addr != NULL_RTX)
2475 {
2476 maybe_complain_about_tail_call (exp, "callee returns a structure");
2477 return false;
2478 }
2479
2480 #ifdef REG_PARM_STACK_SPACE
2481 /* If outgoing reg parm stack space changes, we can not do sibcall. */
2482 if (OUTGOING_REG_PARM_STACK_SPACE (funtype)
2483 != OUTGOING_REG_PARM_STACK_SPACE (TREE_TYPE (current_function_decl))
2484 || (reg_parm_stack_space != REG_PARM_STACK_SPACE (current_function_decl)))
2485 {
2486 maybe_complain_about_tail_call (exp,
2487 "inconsistent size of stack space"
2488 " allocated for arguments which are"
2489 " passed in registers");
2490 return false;
2491 }
2492 #endif
2493
2494 /* Check whether the target is able to optimize the call
2495 into a sibcall. */
2496 if (!targetm.function_ok_for_sibcall (fndecl, exp))
2497 {
2498 maybe_complain_about_tail_call (exp,
2499 "target is not able to optimize the"
2500 " call into a sibling call");
2501 return false;
2502 }
2503
2504 /* Functions that do not return exactly once may not be sibcall
2505 optimized. */
2506 if (flags & ECF_RETURNS_TWICE)
2507 {
2508 maybe_complain_about_tail_call (exp, "callee returns twice");
2509 return false;
2510 }
2511 if (flags & ECF_NORETURN)
2512 {
2513 maybe_complain_about_tail_call (exp, "callee does not return");
2514 return false;
2515 }
2516
2517 if (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (addr))))
2518 {
2519 maybe_complain_about_tail_call (exp, "volatile function type");
2520 return false;
2521 }
2522
2523 /* If the called function is nested in the current one, it might access
2524 some of the caller's arguments, but could clobber them beforehand if
2525 the argument areas are shared. */
2526 if (fndecl && decl_function_context (fndecl) == current_function_decl)
2527 {
2528 maybe_complain_about_tail_call (exp, "nested function");
2529 return false;
2530 }
2531
2532 /* If this function requires more stack slots than the current
2533 function, we cannot change it into a sibling call.
2534 crtl->args.pretend_args_size is not part of the
2535 stack allocated by our caller. */
2536 if (args_size.constant > (crtl->args.size - crtl->args.pretend_args_size))
2537 {
2538 maybe_complain_about_tail_call (exp,
2539 "callee required more stack slots"
2540 " than the caller");
2541 return false;
2542 }
2543
2544 /* If the callee pops its own arguments, then it must pop exactly
2545 the same number of arguments as the current function. */
2546 if (targetm.calls.return_pops_args (fndecl, funtype, args_size.constant)
2547 != targetm.calls.return_pops_args (current_function_decl,
2548 TREE_TYPE (current_function_decl),
2549 crtl->args.size))
2550 {
2551 maybe_complain_about_tail_call (exp,
2552 "inconsistent number of"
2553 " popped arguments");
2554 return false;
2555 }
2556
2557 if (!lang_hooks.decls.ok_for_sibcall (fndecl))
2558 {
2559 maybe_complain_about_tail_call (exp, "frontend does not support"
2560 " sibling call");
2561 return false;
2562 }
2563
2564 /* All checks passed. */
2565 return true;
2566 }
2567
2568 /* Generate all the code for a CALL_EXPR exp
2569 and return an rtx for its value.
2570 Store the value in TARGET (specified as an rtx) if convenient.
2571 If the value is stored in TARGET then TARGET is returned.
2572 If IGNORE is nonzero, then we ignore the value of the function call. */
2573
2574 rtx
2575 expand_call (tree exp, rtx target, int ignore)
2576 {
2577 /* Nonzero if we are currently expanding a call. */
2578 static int currently_expanding_call = 0;
2579
2580 /* RTX for the function to be called. */
2581 rtx funexp;
2582 /* Sequence of insns to perform a normal "call". */
2583 rtx_insn *normal_call_insns = NULL;
2584 /* Sequence of insns to perform a tail "call". */
2585 rtx_insn *tail_call_insns = NULL;
2586 /* Data type of the function. */
2587 tree funtype;
2588 tree type_arg_types;
2589 tree rettype;
2590 /* Declaration of the function being called,
2591 or 0 if the function is computed (not known by name). */
2592 tree fndecl = 0;
2593 /* The type of the function being called. */
2594 tree fntype;
2595 bool try_tail_call = CALL_EXPR_TAILCALL (exp);
2596 bool must_tail_call = CALL_EXPR_MUST_TAIL_CALL (exp);
2597 int pass;
2598
2599 /* Register in which non-BLKmode value will be returned,
2600 or 0 if no value or if value is BLKmode. */
2601 rtx valreg;
2602 /* Register(s) in which bounds are returned. */
2603 rtx valbnd = NULL;
2604 /* Address where we should return a BLKmode value;
2605 0 if value not BLKmode. */
2606 rtx structure_value_addr = 0;
2607 /* Nonzero if that address is being passed by treating it as
2608 an extra, implicit first parameter. Otherwise,
2609 it is passed by being copied directly into struct_value_rtx. */
2610 int structure_value_addr_parm = 0;
2611 /* Holds the value of implicit argument for the struct value. */
2612 tree structure_value_addr_value = NULL_TREE;
2613 /* Size of aggregate value wanted, or zero if none wanted
2614 or if we are using the non-reentrant PCC calling convention
2615 or expecting the value in registers. */
2616 HOST_WIDE_INT struct_value_size = 0;
2617 /* Nonzero if called function returns an aggregate in memory PCC style,
2618 by returning the address of where to find it. */
2619 int pcc_struct_value = 0;
2620 rtx struct_value = 0;
2621
2622 /* Number of actual parameters in this call, including struct value addr. */
2623 int num_actuals;
2624 /* Number of named args. Args after this are anonymous ones
2625 and they must all go on the stack. */
2626 int n_named_args;
2627 /* Number of complex actual arguments that need to be split. */
2628 int num_complex_actuals = 0;
2629
2630 /* Vector of information about each argument.
2631 Arguments are numbered in the order they will be pushed,
2632 not the order they are written. */
2633 struct arg_data *args;
2634
2635 /* Total size in bytes of all the stack-parms scanned so far. */
2636 struct args_size args_size;
2637 struct args_size adjusted_args_size;
2638 /* Size of arguments before any adjustments (such as rounding). */
2639 int unadjusted_args_size;
2640 /* Data on reg parms scanned so far. */
2641 CUMULATIVE_ARGS args_so_far_v;
2642 cumulative_args_t args_so_far;
2643 /* Nonzero if a reg parm has been scanned. */
2644 int reg_parm_seen;
2645 /* Nonzero if this is an indirect function call. */
2646
2647 /* Nonzero if we must avoid push-insns in the args for this call.
2648 If stack space is allocated for register parameters, but not by the
2649 caller, then it is preallocated in the fixed part of the stack frame.
2650 So the entire argument block must then be preallocated (i.e., we
2651 ignore PUSH_ROUNDING in that case). */
2652
2653 int must_preallocate = !PUSH_ARGS;
2654
2655 /* Size of the stack reserved for parameter registers. */
2656 int reg_parm_stack_space = 0;
2657
2658 /* Address of space preallocated for stack parms
2659 (on machines that lack push insns), or 0 if space not preallocated. */
2660 rtx argblock = 0;
2661
2662 /* Mask of ECF_ and ERF_ flags. */
2663 int flags = 0;
2664 int return_flags = 0;
2665 #ifdef REG_PARM_STACK_SPACE
2666 /* Define the boundary of the register parm stack space that needs to be
2667 saved, if any. */
2668 int low_to_save, high_to_save;
2669 rtx save_area = 0; /* Place that it is saved */
2670 #endif
2671
2672 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
2673 char *initial_stack_usage_map = stack_usage_map;
2674 char *stack_usage_map_buf = NULL;
2675
2676 int old_stack_allocated;
2677
2678 /* State variables to track stack modifications. */
2679 rtx old_stack_level = 0;
2680 int old_stack_arg_under_construction = 0;
2681 int old_pending_adj = 0;
2682 int old_inhibit_defer_pop = inhibit_defer_pop;
2683
2684 /* Some stack pointer alterations we make are performed via
2685 allocate_dynamic_stack_space. This modifies the stack_pointer_delta,
2686 which we then also need to save/restore along the way. */
2687 int old_stack_pointer_delta = 0;
2688
2689 rtx call_fusage;
2690 tree addr = CALL_EXPR_FN (exp);
2691 int i;
2692 /* The alignment of the stack, in bits. */
2693 unsigned HOST_WIDE_INT preferred_stack_boundary;
2694 /* The alignment of the stack, in bytes. */
2695 unsigned HOST_WIDE_INT preferred_unit_stack_boundary;
2696 /* The static chain value to use for this call. */
2697 rtx static_chain_value;
2698 /* See if this is "nothrow" function call. */
2699 if (TREE_NOTHROW (exp))
2700 flags |= ECF_NOTHROW;
2701
2702 /* See if we can find a DECL-node for the actual function, and get the
2703 function attributes (flags) from the function decl or type node. */
2704 fndecl = get_callee_fndecl (exp);
2705 if (fndecl)
2706 {
2707 fntype = TREE_TYPE (fndecl);
2708 flags |= flags_from_decl_or_type (fndecl);
2709 return_flags |= decl_return_flags (fndecl);
2710 }
2711 else
2712 {
2713 fntype = TREE_TYPE (TREE_TYPE (addr));
2714 flags |= flags_from_decl_or_type (fntype);
2715 if (CALL_EXPR_BY_DESCRIPTOR (exp))
2716 flags |= ECF_BY_DESCRIPTOR;
2717 }
2718 rettype = TREE_TYPE (exp);
2719
2720 struct_value = targetm.calls.struct_value_rtx (fntype, 0);
2721
2722 /* Warn if this value is an aggregate type,
2723 regardless of which calling convention we are using for it. */
2724 if (AGGREGATE_TYPE_P (rettype))
2725 warning (OPT_Waggregate_return, "function call has aggregate value");
2726
2727 /* If the result of a non looping pure or const function call is
2728 ignored (or void), and none of its arguments are volatile, we can
2729 avoid expanding the call and just evaluate the arguments for
2730 side-effects. */
2731 if ((flags & (ECF_CONST | ECF_PURE))
2732 && (!(flags & ECF_LOOPING_CONST_OR_PURE))
2733 && (ignore || target == const0_rtx
2734 || TYPE_MODE (rettype) == VOIDmode))
2735 {
2736 bool volatilep = false;
2737 tree arg;
2738 call_expr_arg_iterator iter;
2739
2740 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2741 if (TREE_THIS_VOLATILE (arg))
2742 {
2743 volatilep = true;
2744 break;
2745 }
2746
2747 if (! volatilep)
2748 {
2749 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2750 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
2751 return const0_rtx;
2752 }
2753 }
2754
2755 #ifdef REG_PARM_STACK_SPACE
2756 reg_parm_stack_space = REG_PARM_STACK_SPACE (!fndecl ? fntype : fndecl);
2757 #endif
2758
2759 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
2760 && reg_parm_stack_space > 0 && PUSH_ARGS)
2761 must_preallocate = 1;
2762
2763 /* Set up a place to return a structure. */
2764
2765 /* Cater to broken compilers. */
2766 if (aggregate_value_p (exp, fntype))
2767 {
2768 /* This call returns a big structure. */
2769 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
2770
2771 #ifdef PCC_STATIC_STRUCT_RETURN
2772 {
2773 pcc_struct_value = 1;
2774 }
2775 #else /* not PCC_STATIC_STRUCT_RETURN */
2776 {
2777 struct_value_size = int_size_in_bytes (rettype);
2778
2779 /* Even if it is semantically safe to use the target as the return
2780 slot, it may be not sufficiently aligned for the return type. */
2781 if (CALL_EXPR_RETURN_SLOT_OPT (exp)
2782 && target
2783 && MEM_P (target)
2784 && !(MEM_ALIGN (target) < TYPE_ALIGN (rettype)
2785 && SLOW_UNALIGNED_ACCESS (TYPE_MODE (rettype),
2786 MEM_ALIGN (target))))
2787 structure_value_addr = XEXP (target, 0);
2788 else
2789 {
2790 /* For variable-sized objects, we must be called with a target
2791 specified. If we were to allocate space on the stack here,
2792 we would have no way of knowing when to free it. */
2793 rtx d = assign_temp (rettype, 1, 1);
2794 structure_value_addr = XEXP (d, 0);
2795 target = 0;
2796 }
2797 }
2798 #endif /* not PCC_STATIC_STRUCT_RETURN */
2799 }
2800
2801 /* Figure out the amount to which the stack should be aligned. */
2802 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
2803 if (fndecl)
2804 {
2805 struct cgraph_rtl_info *i = cgraph_node::rtl_info (fndecl);
2806 /* Without automatic stack alignment, we can't increase preferred
2807 stack boundary. With automatic stack alignment, it is
2808 unnecessary since unless we can guarantee that all callers will
2809 align the outgoing stack properly, callee has to align its
2810 stack anyway. */
2811 if (i
2812 && i->preferred_incoming_stack_boundary
2813 && i->preferred_incoming_stack_boundary < preferred_stack_boundary)
2814 preferred_stack_boundary = i->preferred_incoming_stack_boundary;
2815 }
2816
2817 /* Operand 0 is a pointer-to-function; get the type of the function. */
2818 funtype = TREE_TYPE (addr);
2819 gcc_assert (POINTER_TYPE_P (funtype));
2820 funtype = TREE_TYPE (funtype);
2821
2822 /* Count whether there are actual complex arguments that need to be split
2823 into their real and imaginary parts. Munge the type_arg_types
2824 appropriately here as well. */
2825 if (targetm.calls.split_complex_arg)
2826 {
2827 call_expr_arg_iterator iter;
2828 tree arg;
2829 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
2830 {
2831 tree type = TREE_TYPE (arg);
2832 if (type && TREE_CODE (type) == COMPLEX_TYPE
2833 && targetm.calls.split_complex_arg (type))
2834 num_complex_actuals++;
2835 }
2836 type_arg_types = split_complex_types (TYPE_ARG_TYPES (funtype));
2837 }
2838 else
2839 type_arg_types = TYPE_ARG_TYPES (funtype);
2840
2841 if (flags & ECF_MAY_BE_ALLOCA)
2842 cfun->calls_alloca = 1;
2843
2844 /* If struct_value_rtx is 0, it means pass the address
2845 as if it were an extra parameter. Put the argument expression
2846 in structure_value_addr_value. */
2847 if (structure_value_addr && struct_value == 0)
2848 {
2849 /* If structure_value_addr is a REG other than
2850 virtual_outgoing_args_rtx, we can use always use it. If it
2851 is not a REG, we must always copy it into a register.
2852 If it is virtual_outgoing_args_rtx, we must copy it to another
2853 register in some cases. */
2854 rtx temp = (!REG_P (structure_value_addr)
2855 || (ACCUMULATE_OUTGOING_ARGS
2856 && stack_arg_under_construction
2857 && structure_value_addr == virtual_outgoing_args_rtx)
2858 ? copy_addr_to_reg (convert_memory_address
2859 (Pmode, structure_value_addr))
2860 : structure_value_addr);
2861
2862 structure_value_addr_value =
2863 make_tree (build_pointer_type (TREE_TYPE (funtype)), temp);
2864 structure_value_addr_parm = CALL_WITH_BOUNDS_P (exp) ? 2 : 1;
2865 }
2866
2867 /* Count the arguments and set NUM_ACTUALS. */
2868 num_actuals =
2869 call_expr_nargs (exp) + num_complex_actuals + structure_value_addr_parm;
2870
2871 /* Compute number of named args.
2872 First, do a raw count of the args for INIT_CUMULATIVE_ARGS. */
2873
2874 if (type_arg_types != 0)
2875 n_named_args
2876 = (list_length (type_arg_types)
2877 /* Count the struct value address, if it is passed as a parm. */
2878 + structure_value_addr_parm);
2879 else
2880 /* If we know nothing, treat all args as named. */
2881 n_named_args = num_actuals;
2882
2883 /* Start updating where the next arg would go.
2884
2885 On some machines (such as the PA) indirect calls have a different
2886 calling convention than normal calls. The fourth argument in
2887 INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
2888 or not. */
2889 INIT_CUMULATIVE_ARGS (args_so_far_v, funtype, NULL_RTX, fndecl, n_named_args);
2890 args_so_far = pack_cumulative_args (&args_so_far_v);
2891
2892 /* Now possibly adjust the number of named args.
2893 Normally, don't include the last named arg if anonymous args follow.
2894 We do include the last named arg if
2895 targetm.calls.strict_argument_naming() returns nonzero.
2896 (If no anonymous args follow, the result of list_length is actually
2897 one too large. This is harmless.)
2898
2899 If targetm.calls.pretend_outgoing_varargs_named() returns
2900 nonzero, and targetm.calls.strict_argument_naming() returns zero,
2901 this machine will be able to place unnamed args that were passed
2902 in registers into the stack. So treat all args as named. This
2903 allows the insns emitting for a specific argument list to be
2904 independent of the function declaration.
2905
2906 If targetm.calls.pretend_outgoing_varargs_named() returns zero,
2907 we do not have any reliable way to pass unnamed args in
2908 registers, so we must force them into memory. */
2909
2910 if (type_arg_types != 0
2911 && targetm.calls.strict_argument_naming (args_so_far))
2912 ;
2913 else if (type_arg_types != 0
2914 && ! targetm.calls.pretend_outgoing_varargs_named (args_so_far))
2915 /* Don't include the last named arg. */
2916 --n_named_args;
2917 else
2918 /* Treat all args as named. */
2919 n_named_args = num_actuals;
2920
2921 /* Make a vector to hold all the information about each arg. */
2922 args = XALLOCAVEC (struct arg_data, num_actuals);
2923 memset (args, 0, num_actuals * sizeof (struct arg_data));
2924
2925 /* Build up entries in the ARGS array, compute the size of the
2926 arguments into ARGS_SIZE, etc. */
2927 initialize_argument_information (num_actuals, args, &args_size,
2928 n_named_args, exp,
2929 structure_value_addr_value, fndecl, fntype,
2930 args_so_far, reg_parm_stack_space,
2931 &old_stack_level, &old_pending_adj,
2932 &must_preallocate, &flags,
2933 &try_tail_call, CALL_FROM_THUNK_P (exp));
2934
2935 if (args_size.var)
2936 must_preallocate = 1;
2937
2938 /* Now make final decision about preallocating stack space. */
2939 must_preallocate = finalize_must_preallocate (must_preallocate,
2940 num_actuals, args,
2941 &args_size);
2942
2943 /* If the structure value address will reference the stack pointer, we
2944 must stabilize it. We don't need to do this if we know that we are
2945 not going to adjust the stack pointer in processing this call. */
2946
2947 if (structure_value_addr
2948 && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
2949 || reg_mentioned_p (virtual_outgoing_args_rtx,
2950 structure_value_addr))
2951 && (args_size.var
2952 || (!ACCUMULATE_OUTGOING_ARGS && args_size.constant)))
2953 structure_value_addr = copy_to_reg (structure_value_addr);
2954
2955 /* Tail calls can make things harder to debug, and we've traditionally
2956 pushed these optimizations into -O2. Don't try if we're already
2957 expanding a call, as that means we're an argument. Don't try if
2958 there's cleanups, as we know there's code to follow the call. */
2959
2960 if (currently_expanding_call++ != 0
2961 || !flag_optimize_sibling_calls
2962 || args_size.var
2963 || dbg_cnt (tail_call) == false)
2964 try_tail_call = 0;
2965
2966 /* If the user has marked the function as requiring tail-call
2967 optimization, attempt it. */
2968 if (must_tail_call)
2969 try_tail_call = 1;
2970
2971 /* Rest of purposes for tail call optimizations to fail. */
2972 if (try_tail_call)
2973 try_tail_call = can_implement_as_sibling_call_p (exp,
2974 structure_value_addr,
2975 funtype,
2976 reg_parm_stack_space,
2977 fndecl,
2978 flags, addr, args_size);
2979
2980 /* Check if caller and callee disagree in promotion of function
2981 return value. */
2982 if (try_tail_call)
2983 {
2984 machine_mode caller_mode, caller_promoted_mode;
2985 machine_mode callee_mode, callee_promoted_mode;
2986 int caller_unsignedp, callee_unsignedp;
2987 tree caller_res = DECL_RESULT (current_function_decl);
2988
2989 caller_unsignedp = TYPE_UNSIGNED (TREE_TYPE (caller_res));
2990 caller_mode = DECL_MODE (caller_res);
2991 callee_unsignedp = TYPE_UNSIGNED (TREE_TYPE (funtype));
2992 callee_mode = TYPE_MODE (TREE_TYPE (funtype));
2993 caller_promoted_mode
2994 = promote_function_mode (TREE_TYPE (caller_res), caller_mode,
2995 &caller_unsignedp,
2996 TREE_TYPE (current_function_decl), 1);
2997 callee_promoted_mode
2998 = promote_function_mode (TREE_TYPE (funtype), callee_mode,
2999 &callee_unsignedp,
3000 funtype, 1);
3001 if (caller_mode != VOIDmode
3002 && (caller_promoted_mode != callee_promoted_mode
3003 || ((caller_mode != caller_promoted_mode
3004 || callee_mode != callee_promoted_mode)
3005 && (caller_unsignedp != callee_unsignedp
3006 || GET_MODE_BITSIZE (caller_mode)
3007 < GET_MODE_BITSIZE (callee_mode)))))
3008 {
3009 try_tail_call = 0;
3010 maybe_complain_about_tail_call (exp,
3011 "caller and callee disagree in"
3012 " promotion of function"
3013 " return value");
3014 }
3015 }
3016
3017 /* Ensure current function's preferred stack boundary is at least
3018 what we need. Stack alignment may also increase preferred stack
3019 boundary. */
3020 if (crtl->preferred_stack_boundary < preferred_stack_boundary)
3021 crtl->preferred_stack_boundary = preferred_stack_boundary;
3022 else
3023 preferred_stack_boundary = crtl->preferred_stack_boundary;
3024
3025 preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
3026
3027 /* We want to make two insn chains; one for a sibling call, the other
3028 for a normal call. We will select one of the two chains after
3029 initial RTL generation is complete. */
3030 for (pass = try_tail_call ? 0 : 1; pass < 2; pass++)
3031 {
3032 int sibcall_failure = 0;
3033 /* We want to emit any pending stack adjustments before the tail
3034 recursion "call". That way we know any adjustment after the tail
3035 recursion call can be ignored if we indeed use the tail
3036 call expansion. */
3037 saved_pending_stack_adjust save;
3038 rtx_insn *insns, *before_call, *after_args;
3039 rtx next_arg_reg;
3040
3041 if (pass == 0)
3042 {
3043 /* State variables we need to save and restore between
3044 iterations. */
3045 save_pending_stack_adjust (&save);
3046 }
3047 if (pass)
3048 flags &= ~ECF_SIBCALL;
3049 else
3050 flags |= ECF_SIBCALL;
3051
3052 /* Other state variables that we must reinitialize each time
3053 through the loop (that are not initialized by the loop itself). */
3054 argblock = 0;
3055 call_fusage = 0;
3056
3057 /* Start a new sequence for the normal call case.
3058
3059 From this point on, if the sibling call fails, we want to set
3060 sibcall_failure instead of continuing the loop. */
3061 start_sequence ();
3062
3063 /* Don't let pending stack adjusts add up to too much.
3064 Also, do all pending adjustments now if there is any chance
3065 this might be a call to alloca or if we are expanding a sibling
3066 call sequence.
3067 Also do the adjustments before a throwing call, otherwise
3068 exception handling can fail; PR 19225. */
3069 if (pending_stack_adjust >= 32
3070 || (pending_stack_adjust > 0
3071 && (flags & ECF_MAY_BE_ALLOCA))
3072 || (pending_stack_adjust > 0
3073 && flag_exceptions && !(flags & ECF_NOTHROW))
3074 || pass == 0)
3075 do_pending_stack_adjust ();
3076
3077 /* Precompute any arguments as needed. */
3078 if (pass)
3079 precompute_arguments (num_actuals, args);
3080
3081 /* Now we are about to start emitting insns that can be deleted
3082 if a libcall is deleted. */
3083 if (pass && (flags & ECF_MALLOC))
3084 start_sequence ();
3085
3086 if (pass == 0 && crtl->stack_protect_guard)
3087 stack_protect_epilogue ();
3088
3089 adjusted_args_size = args_size;
3090 /* Compute the actual size of the argument block required. The variable
3091 and constant sizes must be combined, the size may have to be rounded,
3092 and there may be a minimum required size. When generating a sibcall
3093 pattern, do not round up, since we'll be re-using whatever space our
3094 caller provided. */
3095 unadjusted_args_size
3096 = compute_argument_block_size (reg_parm_stack_space,
3097 &adjusted_args_size,
3098 fndecl, fntype,
3099 (pass == 0 ? 0
3100 : preferred_stack_boundary));
3101
3102 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
3103
3104 /* The argument block when performing a sibling call is the
3105 incoming argument block. */
3106 if (pass == 0)
3107 {
3108 argblock = crtl->args.internal_arg_pointer;
3109 if (STACK_GROWS_DOWNWARD)
3110 argblock
3111 = plus_constant (Pmode, argblock, crtl->args.pretend_args_size);
3112 else
3113 argblock
3114 = plus_constant (Pmode, argblock, -crtl->args.pretend_args_size);
3115
3116 stored_args_map = sbitmap_alloc (args_size.constant);
3117 bitmap_clear (stored_args_map);
3118 }
3119
3120 /* If we have no actual push instructions, or shouldn't use them,
3121 make space for all args right now. */
3122 else if (adjusted_args_size.var != 0)
3123 {
3124 if (old_stack_level == 0)
3125 {
3126 emit_stack_save (SAVE_BLOCK, &old_stack_level);
3127 old_stack_pointer_delta = stack_pointer_delta;
3128 old_pending_adj = pending_stack_adjust;
3129 pending_stack_adjust = 0;
3130 /* stack_arg_under_construction says whether a stack arg is
3131 being constructed at the old stack level. Pushing the stack
3132 gets a clean outgoing argument block. */
3133 old_stack_arg_under_construction = stack_arg_under_construction;
3134 stack_arg_under_construction = 0;
3135 }
3136 argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
3137 if (flag_stack_usage_info)
3138 current_function_has_unbounded_dynamic_stack_size = 1;
3139 }
3140 else
3141 {
3142 /* Note that we must go through the motions of allocating an argument
3143 block even if the size is zero because we may be storing args
3144 in the area reserved for register arguments, which may be part of
3145 the stack frame. */
3146
3147 int needed = adjusted_args_size.constant;
3148
3149 /* Store the maximum argument space used. It will be pushed by
3150 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
3151 checking). */
3152
3153 if (needed > crtl->outgoing_args_size)
3154 crtl->outgoing_args_size = needed;
3155
3156 if (must_preallocate)
3157 {
3158 if (ACCUMULATE_OUTGOING_ARGS)
3159 {
3160 /* Since the stack pointer will never be pushed, it is
3161 possible for the evaluation of a parm to clobber
3162 something we have already written to the stack.
3163 Since most function calls on RISC machines do not use
3164 the stack, this is uncommon, but must work correctly.
3165
3166 Therefore, we save any area of the stack that was already
3167 written and that we are using. Here we set up to do this
3168 by making a new stack usage map from the old one. The
3169 actual save will be done by store_one_arg.
3170
3171 Another approach might be to try to reorder the argument
3172 evaluations to avoid this conflicting stack usage. */
3173
3174 /* Since we will be writing into the entire argument area,
3175 the map must be allocated for its entire size, not just
3176 the part that is the responsibility of the caller. */
3177 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
3178 needed += reg_parm_stack_space;
3179
3180 if (ARGS_GROW_DOWNWARD)
3181 highest_outgoing_arg_in_use
3182 = MAX (initial_highest_arg_in_use, needed + 1);
3183 else
3184 highest_outgoing_arg_in_use
3185 = MAX (initial_highest_arg_in_use, needed);
3186
3187 free (stack_usage_map_buf);
3188 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
3189 stack_usage_map = stack_usage_map_buf;
3190
3191 if (initial_highest_arg_in_use)
3192 memcpy (stack_usage_map, initial_stack_usage_map,
3193 initial_highest_arg_in_use);
3194
3195 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
3196 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
3197 (highest_outgoing_arg_in_use
3198 - initial_highest_arg_in_use));
3199 needed = 0;
3200
3201 /* The address of the outgoing argument list must not be
3202 copied to a register here, because argblock would be left
3203 pointing to the wrong place after the call to
3204 allocate_dynamic_stack_space below. */
3205
3206 argblock = virtual_outgoing_args_rtx;
3207 }
3208 else
3209 {
3210 if (inhibit_defer_pop == 0)
3211 {
3212 /* Try to reuse some or all of the pending_stack_adjust
3213 to get this space. */
3214 needed
3215 = (combine_pending_stack_adjustment_and_call
3216 (unadjusted_args_size,
3217 &adjusted_args_size,
3218 preferred_unit_stack_boundary));
3219
3220 /* combine_pending_stack_adjustment_and_call computes
3221 an adjustment before the arguments are allocated.
3222 Account for them and see whether or not the stack
3223 needs to go up or down. */
3224 needed = unadjusted_args_size - needed;
3225
3226 if (needed < 0)
3227 {
3228 /* We're releasing stack space. */
3229 /* ??? We can avoid any adjustment at all if we're
3230 already aligned. FIXME. */
3231 pending_stack_adjust = -needed;
3232 do_pending_stack_adjust ();
3233 needed = 0;
3234 }
3235 else
3236 /* We need to allocate space. We'll do that in
3237 push_block below. */
3238 pending_stack_adjust = 0;
3239 }
3240
3241 /* Special case this because overhead of `push_block' in
3242 this case is non-trivial. */
3243 if (needed == 0)
3244 argblock = virtual_outgoing_args_rtx;
3245 else
3246 {
3247 argblock = push_block (GEN_INT (needed), 0, 0);
3248 if (ARGS_GROW_DOWNWARD)
3249 argblock = plus_constant (Pmode, argblock, needed);
3250 }
3251
3252 /* We only really need to call `copy_to_reg' in the case
3253 where push insns are going to be used to pass ARGBLOCK
3254 to a function call in ARGS. In that case, the stack
3255 pointer changes value from the allocation point to the
3256 call point, and hence the value of
3257 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
3258 as well always do it. */
3259 argblock = copy_to_reg (argblock);
3260 }
3261 }
3262 }
3263
3264 if (ACCUMULATE_OUTGOING_ARGS)
3265 {
3266 /* The save/restore code in store_one_arg handles all
3267 cases except one: a constructor call (including a C
3268 function returning a BLKmode struct) to initialize
3269 an argument. */
3270 if (stack_arg_under_construction)
3271 {
3272 rtx push_size
3273 = GEN_INT (adjusted_args_size.constant
3274 + (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype
3275 : TREE_TYPE (fndecl))) ? 0
3276 : reg_parm_stack_space));
3277 if (old_stack_level == 0)
3278 {
3279 emit_stack_save (SAVE_BLOCK, &old_stack_level);
3280 old_stack_pointer_delta = stack_pointer_delta;
3281 old_pending_adj = pending_stack_adjust;
3282 pending_stack_adjust = 0;
3283 /* stack_arg_under_construction says whether a stack
3284 arg is being constructed at the old stack level.
3285 Pushing the stack gets a clean outgoing argument
3286 block. */
3287 old_stack_arg_under_construction
3288 = stack_arg_under_construction;
3289 stack_arg_under_construction = 0;
3290 /* Make a new map for the new argument list. */
3291 free (stack_usage_map_buf);
3292 stack_usage_map_buf = XCNEWVEC (char, highest_outgoing_arg_in_use);
3293 stack_usage_map = stack_usage_map_buf;
3294 highest_outgoing_arg_in_use = 0;
3295 }
3296 /* We can pass TRUE as the 4th argument because we just
3297 saved the stack pointer and will restore it right after
3298 the call. */
3299 allocate_dynamic_stack_space (push_size, 0,
3300 BIGGEST_ALIGNMENT, true);
3301 }
3302
3303 /* If argument evaluation might modify the stack pointer,
3304 copy the address of the argument list to a register. */
3305 for (i = 0; i < num_actuals; i++)
3306 if (args[i].pass_on_stack)
3307 {
3308 argblock = copy_addr_to_reg (argblock);
3309 break;
3310 }
3311 }
3312
3313 compute_argument_addresses (args, argblock, num_actuals);
3314
3315 /* Stack is properly aligned, pops can't safely be deferred during
3316 the evaluation of the arguments. */
3317 NO_DEFER_POP;
3318
3319 /* Precompute all register parameters. It isn't safe to compute
3320 anything once we have started filling any specific hard regs.
3321 TLS symbols sometimes need a call to resolve. Precompute
3322 register parameters before any stack pointer manipulation
3323 to avoid unaligned stack in the called function. */
3324 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
3325
3326 OK_DEFER_POP;
3327
3328 /* Perform stack alignment before the first push (the last arg). */
3329 if (argblock == 0
3330 && adjusted_args_size.constant > reg_parm_stack_space
3331 && adjusted_args_size.constant != unadjusted_args_size)
3332 {
3333 /* When the stack adjustment is pending, we get better code
3334 by combining the adjustments. */
3335 if (pending_stack_adjust
3336 && ! inhibit_defer_pop)
3337 {
3338 pending_stack_adjust
3339 = (combine_pending_stack_adjustment_and_call
3340 (unadjusted_args_size,
3341 &adjusted_args_size,
3342 preferred_unit_stack_boundary));
3343 do_pending_stack_adjust ();
3344 }
3345 else if (argblock == 0)
3346 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
3347 - unadjusted_args_size));
3348 }
3349 /* Now that the stack is properly aligned, pops can't safely
3350 be deferred during the evaluation of the arguments. */
3351 NO_DEFER_POP;
3352
3353 /* Record the maximum pushed stack space size. We need to delay
3354 doing it this far to take into account the optimization done
3355 by combine_pending_stack_adjustment_and_call. */
3356 if (flag_stack_usage_info
3357 && !ACCUMULATE_OUTGOING_ARGS
3358 && pass
3359 && adjusted_args_size.var == 0)
3360 {
3361 int pushed = adjusted_args_size.constant + pending_stack_adjust;
3362 if (pushed > current_function_pushed_stack_size)
3363 current_function_pushed_stack_size = pushed;
3364 }
3365
3366 funexp = rtx_for_function_call (fndecl, addr);
3367
3368 if (CALL_EXPR_STATIC_CHAIN (exp))
3369 static_chain_value = expand_normal (CALL_EXPR_STATIC_CHAIN (exp));
3370 else
3371 static_chain_value = 0;
3372
3373 #ifdef REG_PARM_STACK_SPACE
3374 /* Save the fixed argument area if it's part of the caller's frame and
3375 is clobbered by argument setup for this call. */
3376 if (ACCUMULATE_OUTGOING_ARGS && pass)
3377 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3378 &low_to_save, &high_to_save);
3379 #endif
3380
3381 /* Now store (and compute if necessary) all non-register parms.
3382 These come before register parms, since they can require block-moves,
3383 which could clobber the registers used for register parms.
3384 Parms which have partial registers are not stored here,
3385 but we do preallocate space here if they want that. */
3386
3387 for (i = 0; i < num_actuals; i++)
3388 {
3389 /* Delay bounds until all other args are stored. */
3390 if (POINTER_BOUNDS_P (args[i].tree_value))
3391 continue;
3392 else if (args[i].reg == 0 || args[i].pass_on_stack)
3393 {
3394 rtx_insn *before_arg = get_last_insn ();
3395
3396 /* We don't allow passing huge (> 2^30 B) arguments
3397 by value. It would cause an overflow later on. */
3398 if (adjusted_args_size.constant
3399 >= (1 << (HOST_BITS_PER_INT - 2)))
3400 {
3401 sorry ("passing too large argument on stack");
3402 continue;
3403 }
3404
3405 if (store_one_arg (&args[i], argblock, flags,
3406 adjusted_args_size.var != 0,
3407 reg_parm_stack_space)
3408 || (pass == 0
3409 && check_sibcall_argument_overlap (before_arg,
3410 &args[i], 1)))
3411 sibcall_failure = 1;
3412 }
3413
3414 if (args[i].stack)
3415 call_fusage
3416 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[i].tree_value)),
3417 gen_rtx_USE (VOIDmode, args[i].stack),
3418 call_fusage);
3419 }
3420
3421 /* If we have a parm that is passed in registers but not in memory
3422 and whose alignment does not permit a direct copy into registers,
3423 make a group of pseudos that correspond to each register that we
3424 will later fill. */
3425 if (STRICT_ALIGNMENT)
3426 store_unaligned_arguments_into_pseudos (args, num_actuals);
3427
3428 /* Prepare the address of the call. This must be done before any
3429 register parameters is loaded for find_first_parameter_load to
3430 work properly in the presence of descriptors. */
3431 funexp = prepare_call_address (fndecl ? fndecl : fntype, funexp,
3432 static_chain_value, &call_fusage,
3433 reg_parm_seen, flags);
3434
3435 /* Now store any partially-in-registers parm.
3436 This is the last place a block-move can happen. */
3437 if (reg_parm_seen)
3438 for (i = 0; i < num_actuals; i++)
3439 if (args[i].partial != 0 && ! args[i].pass_on_stack)
3440 {
3441 rtx_insn *before_arg = get_last_insn ();
3442
3443 /* On targets with weird calling conventions (e.g. PA) it's
3444 hard to ensure that all cases of argument overlap between
3445 stack and registers work. Play it safe and bail out. */
3446 if (ARGS_GROW_DOWNWARD && !STACK_GROWS_DOWNWARD)
3447 {
3448 sibcall_failure = 1;
3449 break;
3450 }
3451
3452 if (store_one_arg (&args[i], argblock, flags,
3453 adjusted_args_size.var != 0,
3454 reg_parm_stack_space)
3455 || (pass == 0
3456 && check_sibcall_argument_overlap (before_arg,
3457 &args[i], 1)))
3458 sibcall_failure = 1;
3459 }
3460
3461 bool any_regs = false;
3462 for (i = 0; i < num_actuals; i++)
3463 if (args[i].reg != NULL_RTX)
3464 {
3465 any_regs = true;
3466 targetm.calls.call_args (args[i].reg, funtype);
3467 }
3468 if (!any_regs)
3469 targetm.calls.call_args (pc_rtx, funtype);
3470
3471 /* Figure out the register where the value, if any, will come back. */
3472 valreg = 0;
3473 valbnd = 0;
3474 if (TYPE_MODE (rettype) != VOIDmode
3475 && ! structure_value_addr)
3476 {
3477 if (pcc_struct_value)
3478 {
3479 valreg = hard_function_value (build_pointer_type (rettype),
3480 fndecl, NULL, (pass == 0));
3481 if (CALL_WITH_BOUNDS_P (exp))
3482 valbnd = targetm.calls.
3483 chkp_function_value_bounds (build_pointer_type (rettype),
3484 fndecl, (pass == 0));
3485 }
3486 else
3487 {
3488 valreg = hard_function_value (rettype, fndecl, fntype,
3489 (pass == 0));
3490 if (CALL_WITH_BOUNDS_P (exp))
3491 valbnd = targetm.calls.chkp_function_value_bounds (rettype,
3492 fndecl,
3493 (pass == 0));
3494 }
3495
3496 /* If VALREG is a PARALLEL whose first member has a zero
3497 offset, use that. This is for targets such as m68k that
3498 return the same value in multiple places. */
3499 if (GET_CODE (valreg) == PARALLEL)
3500 {
3501 rtx elem = XVECEXP (valreg, 0, 0);
3502 rtx where = XEXP (elem, 0);
3503 rtx offset = XEXP (elem, 1);
3504 if (offset == const0_rtx
3505 && GET_MODE (where) == GET_MODE (valreg))
3506 valreg = where;
3507 }
3508 }
3509
3510 /* Store all bounds not passed in registers. */
3511 for (i = 0; i < num_actuals; i++)
3512 {
3513 if (POINTER_BOUNDS_P (args[i].tree_value)
3514 && !args[i].reg)
3515 store_bounds (&args[i],
3516 args[i].pointer_arg == -1
3517 ? NULL
3518 : &args[args[i].pointer_arg]);
3519 }
3520
3521 /* If register arguments require space on the stack and stack space
3522 was not preallocated, allocate stack space here for arguments
3523 passed in registers. */
3524 if (OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl)))
3525 && !ACCUMULATE_OUTGOING_ARGS
3526 && must_preallocate == 0 && reg_parm_stack_space > 0)
3527 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
3528
3529 /* Pass the function the address in which to return a
3530 structure value. */
3531 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
3532 {
3533 structure_value_addr
3534 = convert_memory_address (Pmode, structure_value_addr);
3535 emit_move_insn (struct_value,
3536 force_reg (Pmode,
3537 force_operand (structure_value_addr,
3538 NULL_RTX)));
3539
3540 if (REG_P (struct_value))
3541 use_reg (&call_fusage, struct_value);
3542 }
3543
3544 after_args = get_last_insn ();
3545 load_register_parameters (args, num_actuals, &call_fusage, flags,
3546 pass == 0, &sibcall_failure);
3547
3548 /* Save a pointer to the last insn before the call, so that we can
3549 later safely search backwards to find the CALL_INSN. */
3550 before_call = get_last_insn ();
3551
3552 /* Set up next argument register. For sibling calls on machines
3553 with register windows this should be the incoming register. */
3554 if (pass == 0)
3555 next_arg_reg = targetm.calls.function_incoming_arg (args_so_far,
3556 VOIDmode,
3557 void_type_node,
3558 true);
3559 else
3560 next_arg_reg = targetm.calls.function_arg (args_so_far,
3561 VOIDmode, void_type_node,
3562 true);
3563
3564 if (pass == 1 && (return_flags & ERF_RETURNS_ARG))
3565 {
3566 int arg_nr = return_flags & ERF_RETURN_ARG_MASK;
3567 arg_nr = num_actuals - arg_nr - 1;
3568 if (arg_nr >= 0
3569 && arg_nr < num_actuals
3570 && args[arg_nr].reg
3571 && valreg
3572 && REG_P (valreg)
3573 && GET_MODE (args[arg_nr].reg) == GET_MODE (valreg))
3574 call_fusage
3575 = gen_rtx_EXPR_LIST (TYPE_MODE (TREE_TYPE (args[arg_nr].tree_value)),
3576 gen_rtx_SET (valreg, args[arg_nr].reg),
3577 call_fusage);
3578 }
3579 /* All arguments and registers used for the call must be set up by
3580 now! */
3581
3582 /* Stack must be properly aligned now. */
3583 gcc_assert (!pass
3584 || !(stack_pointer_delta % preferred_unit_stack_boundary));
3585
3586 /* Generate the actual call instruction. */
3587 emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
3588 adjusted_args_size.constant, struct_value_size,
3589 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
3590 flags, args_so_far);
3591
3592 if (flag_ipa_ra)
3593 {
3594 rtx_call_insn *last;
3595 rtx datum = NULL_RTX;
3596 if (fndecl != NULL_TREE)
3597 {
3598 datum = XEXP (DECL_RTL (fndecl), 0);
3599 gcc_assert (datum != NULL_RTX
3600 && GET_CODE (datum) == SYMBOL_REF);
3601 }
3602 last = last_call_insn ();
3603 add_reg_note (last, REG_CALL_DECL, datum);
3604 }
3605
3606 /* If the call setup or the call itself overlaps with anything
3607 of the argument setup we probably clobbered our call address.
3608 In that case we can't do sibcalls. */
3609 if (pass == 0
3610 && check_sibcall_argument_overlap (after_args, 0, 0))
3611 sibcall_failure = 1;
3612
3613 /* If a non-BLKmode value is returned at the most significant end
3614 of a register, shift the register right by the appropriate amount
3615 and update VALREG accordingly. BLKmode values are handled by the
3616 group load/store machinery below. */
3617 if (!structure_value_addr
3618 && !pcc_struct_value
3619 && TYPE_MODE (rettype) != VOIDmode
3620 && TYPE_MODE (rettype) != BLKmode
3621 && REG_P (valreg)
3622 && targetm.calls.return_in_msb (rettype))
3623 {
3624 if (shift_return_value (TYPE_MODE (rettype), false, valreg))
3625 sibcall_failure = 1;
3626 valreg = gen_rtx_REG (TYPE_MODE (rettype), REGNO (valreg));
3627 }
3628
3629 if (pass && (flags & ECF_MALLOC))
3630 {
3631 rtx temp = gen_reg_rtx (GET_MODE (valreg));
3632 rtx_insn *last, *insns;
3633
3634 /* The return value from a malloc-like function is a pointer. */
3635 if (TREE_CODE (rettype) == POINTER_TYPE)
3636 mark_reg_pointer (temp, MALLOC_ABI_ALIGNMENT);
3637
3638 emit_move_insn (temp, valreg);
3639
3640 /* The return value from a malloc-like function can not alias
3641 anything else. */
3642 last = get_last_insn ();
3643 add_reg_note (last, REG_NOALIAS, temp);
3644
3645 /* Write out the sequence. */
3646 insns = get_insns ();
3647 end_sequence ();
3648 emit_insn (insns);
3649 valreg = temp;
3650 }
3651
3652 /* For calls to `setjmp', etc., inform
3653 function.c:setjmp_warnings that it should complain if
3654 nonvolatile values are live. For functions that cannot
3655 return, inform flow that control does not fall through. */
3656
3657 if ((flags & ECF_NORETURN) || pass == 0)
3658 {
3659 /* The barrier must be emitted
3660 immediately after the CALL_INSN. Some ports emit more
3661 than just a CALL_INSN above, so we must search for it here. */
3662
3663 rtx_insn *last = get_last_insn ();
3664 while (!CALL_P (last))
3665 {
3666 last = PREV_INSN (last);
3667 /* There was no CALL_INSN? */
3668 gcc_assert (last != before_call);
3669 }
3670
3671 emit_barrier_after (last);
3672
3673 /* Stack adjustments after a noreturn call are dead code.
3674 However when NO_DEFER_POP is in effect, we must preserve
3675 stack_pointer_delta. */
3676 if (inhibit_defer_pop == 0)
3677 {
3678 stack_pointer_delta = old_stack_allocated;
3679 pending_stack_adjust = 0;
3680 }
3681 }
3682
3683 /* If value type not void, return an rtx for the value. */
3684
3685 if (TYPE_MODE (rettype) == VOIDmode
3686 || ignore)
3687 target = const0_rtx;
3688 else if (structure_value_addr)
3689 {
3690 if (target == 0 || !MEM_P (target))
3691 {
3692 target
3693 = gen_rtx_MEM (TYPE_MODE (rettype),
3694 memory_address (TYPE_MODE (rettype),
3695 structure_value_addr));
3696 set_mem_attributes (target, rettype, 1);
3697 }
3698 }
3699 else if (pcc_struct_value)
3700 {
3701 /* This is the special C++ case where we need to
3702 know what the true target was. We take care to
3703 never use this value more than once in one expression. */
3704 target = gen_rtx_MEM (TYPE_MODE (rettype),
3705 copy_to_reg (valreg));
3706 set_mem_attributes (target, rettype, 1);
3707 }
3708 /* Handle calls that return values in multiple non-contiguous locations.
3709 The Irix 6 ABI has examples of this. */
3710 else if (GET_CODE (valreg) == PARALLEL)
3711 {
3712 if (target == 0)
3713 target = emit_group_move_into_temps (valreg);
3714 else if (rtx_equal_p (target, valreg))
3715 ;
3716 else if (GET_CODE (target) == PARALLEL)
3717 /* Handle the result of a emit_group_move_into_temps
3718 call in the previous pass. */
3719 emit_group_move (target, valreg);
3720 else
3721 emit_group_store (target, valreg, rettype,
3722 int_size_in_bytes (rettype));
3723 }
3724 else if (target
3725 && GET_MODE (target) == TYPE_MODE (rettype)
3726 && GET_MODE (target) == GET_MODE (valreg))
3727 {
3728 bool may_overlap = false;
3729
3730 /* We have to copy a return value in a CLASS_LIKELY_SPILLED hard
3731 reg to a plain register. */
3732 if (!REG_P (target) || HARD_REGISTER_P (target))
3733 valreg = avoid_likely_spilled_reg (valreg);
3734
3735 /* If TARGET is a MEM in the argument area, and we have
3736 saved part of the argument area, then we can't store
3737 directly into TARGET as it may get overwritten when we
3738 restore the argument save area below. Don't work too
3739 hard though and simply force TARGET to a register if it
3740 is a MEM; the optimizer is quite likely to sort it out. */
3741 if (ACCUMULATE_OUTGOING_ARGS && pass && MEM_P (target))
3742 for (i = 0; i < num_actuals; i++)
3743 if (args[i].save_area)
3744 {
3745 may_overlap = true;
3746 break;
3747 }
3748
3749 if (may_overlap)
3750 target = copy_to_reg (valreg);
3751 else
3752 {
3753 /* TARGET and VALREG cannot be equal at this point
3754 because the latter would not have
3755 REG_FUNCTION_VALUE_P true, while the former would if
3756 it were referring to the same register.
3757
3758 If they refer to the same register, this move will be
3759 a no-op, except when function inlining is being
3760 done. */
3761 emit_move_insn (target, valreg);
3762
3763 /* If we are setting a MEM, this code must be executed.
3764 Since it is emitted after the call insn, sibcall
3765 optimization cannot be performed in that case. */
3766 if (MEM_P (target))
3767 sibcall_failure = 1;
3768 }
3769 }
3770 else
3771 target = copy_to_reg (avoid_likely_spilled_reg (valreg));
3772
3773 /* If we promoted this return value, make the proper SUBREG.
3774 TARGET might be const0_rtx here, so be careful. */
3775 if (REG_P (target)
3776 && TYPE_MODE (rettype) != BLKmode
3777 && GET_MODE (target) != TYPE_MODE (rettype))
3778 {
3779 tree type = rettype;
3780 int unsignedp = TYPE_UNSIGNED (type);
3781 int offset = 0;
3782 machine_mode pmode;
3783
3784 /* Ensure we promote as expected, and get the new unsignedness. */
3785 pmode = promote_function_mode (type, TYPE_MODE (type), &unsignedp,
3786 funtype, 1);
3787 gcc_assert (GET_MODE (target) == pmode);
3788
3789 if ((WORDS_BIG_ENDIAN || BYTES_BIG_ENDIAN)
3790 && (GET_MODE_SIZE (GET_MODE (target))
3791 > GET_MODE_SIZE (TYPE_MODE (type))))
3792 {
3793 offset = GET_MODE_SIZE (GET_MODE (target))
3794 - GET_MODE_SIZE (TYPE_MODE (type));
3795 if (! BYTES_BIG_ENDIAN)
3796 offset = (offset / UNITS_PER_WORD) * UNITS_PER_WORD;
3797 else if (! WORDS_BIG_ENDIAN)
3798 offset %= UNITS_PER_WORD;
3799 }
3800
3801 target = gen_rtx_SUBREG (TYPE_MODE (type), target, offset);
3802 SUBREG_PROMOTED_VAR_P (target) = 1;
3803 SUBREG_PROMOTED_SET (target, unsignedp);
3804 }
3805
3806 /* If size of args is variable or this was a constructor call for a stack
3807 argument, restore saved stack-pointer value. */
3808
3809 if (old_stack_level)
3810 {
3811 rtx_insn *prev = get_last_insn ();
3812
3813 emit_stack_restore (SAVE_BLOCK, old_stack_level);
3814 stack_pointer_delta = old_stack_pointer_delta;
3815
3816 fixup_args_size_notes (prev, get_last_insn (), stack_pointer_delta);
3817
3818 pending_stack_adjust = old_pending_adj;
3819 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
3820 stack_arg_under_construction = old_stack_arg_under_construction;
3821 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3822 stack_usage_map = initial_stack_usage_map;
3823 sibcall_failure = 1;
3824 }
3825 else if (ACCUMULATE_OUTGOING_ARGS && pass)
3826 {
3827 #ifdef REG_PARM_STACK_SPACE
3828 if (save_area)
3829 restore_fixed_argument_area (save_area, argblock,
3830 high_to_save, low_to_save);
3831 #endif
3832
3833 /* If we saved any argument areas, restore them. */
3834 for (i = 0; i < num_actuals; i++)
3835 if (args[i].save_area)
3836 {
3837 machine_mode save_mode = GET_MODE (args[i].save_area);
3838 rtx stack_area
3839 = gen_rtx_MEM (save_mode,
3840 memory_address (save_mode,
3841 XEXP (args[i].stack_slot, 0)));
3842
3843 if (save_mode != BLKmode)
3844 emit_move_insn (stack_area, args[i].save_area);
3845 else
3846 emit_block_move (stack_area, args[i].save_area,
3847 GEN_INT (args[i].locate.size.constant),
3848 BLOCK_OP_CALL_PARM);
3849 }
3850
3851 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3852 stack_usage_map = initial_stack_usage_map;
3853 }
3854
3855 /* If this was alloca, record the new stack level. */
3856 if (flags & ECF_MAY_BE_ALLOCA)
3857 record_new_stack_level ();
3858
3859 /* Free up storage we no longer need. */
3860 for (i = 0; i < num_actuals; ++i)
3861 free (args[i].aligned_regs);
3862
3863 targetm.calls.end_call_args ();
3864
3865 insns = get_insns ();
3866 end_sequence ();
3867
3868 if (pass == 0)
3869 {
3870 tail_call_insns = insns;
3871
3872 /* Restore the pending stack adjustment now that we have
3873 finished generating the sibling call sequence. */
3874
3875 restore_pending_stack_adjust (&save);
3876
3877 /* Prepare arg structure for next iteration. */
3878 for (i = 0; i < num_actuals; i++)
3879 {
3880 args[i].value = 0;
3881 args[i].aligned_regs = 0;
3882 args[i].stack = 0;
3883 }
3884
3885 sbitmap_free (stored_args_map);
3886 internal_arg_pointer_exp_state.scan_start = NULL;
3887 internal_arg_pointer_exp_state.cache.release ();
3888 }
3889 else
3890 {
3891 normal_call_insns = insns;
3892
3893 /* Verify that we've deallocated all the stack we used. */
3894 gcc_assert ((flags & ECF_NORETURN)
3895 || (old_stack_allocated
3896 == stack_pointer_delta - pending_stack_adjust));
3897 }
3898
3899 /* If something prevents making this a sibling call,
3900 zero out the sequence. */
3901 if (sibcall_failure)
3902 tail_call_insns = NULL;
3903 else
3904 break;
3905 }
3906
3907 /* If tail call production succeeded, we need to remove REG_EQUIV notes on
3908 arguments too, as argument area is now clobbered by the call. */
3909 if (tail_call_insns)
3910 {
3911 emit_insn (tail_call_insns);
3912 crtl->tail_call_emit = true;
3913 }
3914 else
3915 {
3916 emit_insn (normal_call_insns);
3917 if (try_tail_call)
3918 /* Ideally we'd emit a message for all of the ways that it could
3919 have failed. */
3920 maybe_complain_about_tail_call (exp, "tail call production failed");
3921 }
3922
3923 currently_expanding_call--;
3924
3925 free (stack_usage_map_buf);
3926
3927 /* Join result with returned bounds so caller may use them if needed. */
3928 target = chkp_join_splitted_slot (target, valbnd);
3929
3930 return target;
3931 }
3932
3933 /* A sibling call sequence invalidates any REG_EQUIV notes made for
3934 this function's incoming arguments.
3935
3936 At the start of RTL generation we know the only REG_EQUIV notes
3937 in the rtl chain are those for incoming arguments, so we can look
3938 for REG_EQUIV notes between the start of the function and the
3939 NOTE_INSN_FUNCTION_BEG.
3940
3941 This is (slight) overkill. We could keep track of the highest
3942 argument we clobber and be more selective in removing notes, but it
3943 does not seem to be worth the effort. */
3944
3945 void
3946 fixup_tail_calls (void)
3947 {
3948 rtx_insn *insn;
3949
3950 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3951 {
3952 rtx note;
3953
3954 /* There are never REG_EQUIV notes for the incoming arguments
3955 after the NOTE_INSN_FUNCTION_BEG note, so stop if we see it. */
3956 if (NOTE_P (insn)
3957 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3958 break;
3959
3960 note = find_reg_note (insn, REG_EQUIV, 0);
3961 if (note)
3962 remove_note (insn, note);
3963 note = find_reg_note (insn, REG_EQUIV, 0);
3964 gcc_assert (!note);
3965 }
3966 }
3967
3968 /* Traverse a list of TYPES and expand all complex types into their
3969 components. */
3970 static tree
3971 split_complex_types (tree types)
3972 {
3973 tree p;
3974
3975 /* Before allocating memory, check for the common case of no complex. */
3976 for (p = types; p; p = TREE_CHAIN (p))
3977 {
3978 tree type = TREE_VALUE (p);
3979 if (TREE_CODE (type) == COMPLEX_TYPE
3980 && targetm.calls.split_complex_arg (type))
3981 goto found;
3982 }
3983 return types;
3984
3985 found:
3986 types = copy_list (types);
3987
3988 for (p = types; p; p = TREE_CHAIN (p))
3989 {
3990 tree complex_type = TREE_VALUE (p);
3991
3992 if (TREE_CODE (complex_type) == COMPLEX_TYPE
3993 && targetm.calls.split_complex_arg (complex_type))
3994 {
3995 tree next, imag;
3996
3997 /* Rewrite complex type with component type. */
3998 TREE_VALUE (p) = TREE_TYPE (complex_type);
3999 next = TREE_CHAIN (p);
4000
4001 /* Add another component type for the imaginary part. */
4002 imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
4003 TREE_CHAIN (p) = imag;
4004 TREE_CHAIN (imag) = next;
4005
4006 /* Skip the newly created node. */
4007 p = TREE_CHAIN (p);
4008 }
4009 }
4010
4011 return types;
4012 }
4013 \f
4014 /* Output a library call to function FUN (a SYMBOL_REF rtx).
4015 The RETVAL parameter specifies whether return value needs to be saved, other
4016 parameters are documented in the emit_library_call function below. */
4017
4018 static rtx
4019 emit_library_call_value_1 (int retval, rtx orgfun, rtx value,
4020 enum libcall_type fn_type,
4021 machine_mode outmode, int nargs, va_list p)
4022 {
4023 /* Total size in bytes of all the stack-parms scanned so far. */
4024 struct args_size args_size;
4025 /* Size of arguments before any adjustments (such as rounding). */
4026 struct args_size original_args_size;
4027 int argnum;
4028 rtx fun;
4029 /* Todo, choose the correct decl type of orgfun. Sadly this information
4030 isn't present here, so we default to native calling abi here. */
4031 tree fndecl ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
4032 tree fntype ATTRIBUTE_UNUSED = NULL_TREE; /* library calls default to host calling abi ? */
4033 int count;
4034 rtx argblock = 0;
4035 CUMULATIVE_ARGS args_so_far_v;
4036 cumulative_args_t args_so_far;
4037 struct arg
4038 {
4039 rtx value;
4040 machine_mode mode;
4041 rtx reg;
4042 int partial;
4043 struct locate_and_pad_arg_data locate;
4044 rtx save_area;
4045 };
4046 struct arg *argvec;
4047 int old_inhibit_defer_pop = inhibit_defer_pop;
4048 rtx call_fusage = 0;
4049 rtx mem_value = 0;
4050 rtx valreg;
4051 int pcc_struct_value = 0;
4052 int struct_value_size = 0;
4053 int flags;
4054 int reg_parm_stack_space = 0;
4055 int needed;
4056 rtx_insn *before_call;
4057 bool have_push_fusage;
4058 tree tfom; /* type_for_mode (outmode, 0) */
4059
4060 #ifdef REG_PARM_STACK_SPACE
4061 /* Define the boundary of the register parm stack space that needs to be
4062 save, if any. */
4063 int low_to_save = 0, high_to_save = 0;
4064 rtx save_area = 0; /* Place that it is saved. */
4065 #endif
4066
4067 /* Size of the stack reserved for parameter registers. */
4068 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
4069 char *initial_stack_usage_map = stack_usage_map;
4070 char *stack_usage_map_buf = NULL;
4071
4072 rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
4073
4074 #ifdef REG_PARM_STACK_SPACE
4075 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
4076 #endif
4077
4078 /* By default, library functions cannot throw. */
4079 flags = ECF_NOTHROW;
4080
4081 switch (fn_type)
4082 {
4083 case LCT_NORMAL:
4084 break;
4085 case LCT_CONST:
4086 flags |= ECF_CONST;
4087 break;
4088 case LCT_PURE:
4089 flags |= ECF_PURE;
4090 break;
4091 case LCT_NORETURN:
4092 flags |= ECF_NORETURN;
4093 break;
4094 case LCT_THROW:
4095 flags &= ~ECF_NOTHROW;
4096 break;
4097 case LCT_RETURNS_TWICE:
4098 flags = ECF_RETURNS_TWICE;
4099 break;
4100 }
4101 fun = orgfun;
4102
4103 /* Ensure current function's preferred stack boundary is at least
4104 what we need. */
4105 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
4106 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
4107
4108 /* If this kind of value comes back in memory,
4109 decide where in memory it should come back. */
4110 if (outmode != VOIDmode)
4111 {
4112 tfom = lang_hooks.types.type_for_mode (outmode, 0);
4113 if (aggregate_value_p (tfom, 0))
4114 {
4115 #ifdef PCC_STATIC_STRUCT_RETURN
4116 rtx pointer_reg
4117 = hard_function_value (build_pointer_type (tfom), 0, 0, 0);
4118 mem_value = gen_rtx_MEM (outmode, pointer_reg);
4119 pcc_struct_value = 1;
4120 if (value == 0)
4121 value = gen_reg_rtx (outmode);
4122 #else /* not PCC_STATIC_STRUCT_RETURN */
4123 struct_value_size = GET_MODE_SIZE (outmode);
4124 if (value != 0 && MEM_P (value))
4125 mem_value = value;
4126 else
4127 mem_value = assign_temp (tfom, 1, 1);
4128 #endif
4129 /* This call returns a big structure. */
4130 flags &= ~(ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE);
4131 }
4132 }
4133 else
4134 tfom = void_type_node;
4135
4136 /* ??? Unfinished: must pass the memory address as an argument. */
4137
4138 /* Copy all the libcall-arguments out of the varargs data
4139 and into a vector ARGVEC.
4140
4141 Compute how to pass each argument. We only support a very small subset
4142 of the full argument passing conventions to limit complexity here since
4143 library functions shouldn't have many args. */
4144
4145 argvec = XALLOCAVEC (struct arg, nargs + 1);
4146 memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
4147
4148 #ifdef INIT_CUMULATIVE_LIBCALL_ARGS
4149 INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far_v, outmode, fun);
4150 #else
4151 INIT_CUMULATIVE_ARGS (args_so_far_v, NULL_TREE, fun, 0, nargs);
4152 #endif
4153 args_so_far = pack_cumulative_args (&args_so_far_v);
4154
4155 args_size.constant = 0;
4156 args_size.var = 0;
4157
4158 count = 0;
4159
4160 push_temp_slots ();
4161
4162 /* If there's a structure value address to be passed,
4163 either pass it in the special place, or pass it as an extra argument. */
4164 if (mem_value && struct_value == 0 && ! pcc_struct_value)
4165 {
4166 rtx addr = XEXP (mem_value, 0);
4167
4168 nargs++;
4169
4170 /* Make sure it is a reasonable operand for a move or push insn. */
4171 if (!REG_P (addr) && !MEM_P (addr)
4172 && !(CONSTANT_P (addr)
4173 && targetm.legitimate_constant_p (Pmode, addr)))
4174 addr = force_operand (addr, NULL_RTX);
4175
4176 argvec[count].value = addr;
4177 argvec[count].mode = Pmode;
4178 argvec[count].partial = 0;
4179
4180 argvec[count].reg = targetm.calls.function_arg (args_so_far,
4181 Pmode, NULL_TREE, true);
4182 gcc_assert (targetm.calls.arg_partial_bytes (args_so_far, Pmode,
4183 NULL_TREE, 1) == 0);
4184
4185 locate_and_pad_parm (Pmode, NULL_TREE,
4186 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4187 1,
4188 #else
4189 argvec[count].reg != 0,
4190 #endif
4191 reg_parm_stack_space, 0,
4192 NULL_TREE, &args_size, &argvec[count].locate);
4193
4194 if (argvec[count].reg == 0 || argvec[count].partial != 0
4195 || reg_parm_stack_space > 0)
4196 args_size.constant += argvec[count].locate.size.constant;
4197
4198 targetm.calls.function_arg_advance (args_so_far, Pmode, (tree) 0, true);
4199
4200 count++;
4201 }
4202
4203 for (; count < nargs; count++)
4204 {
4205 rtx val = va_arg (p, rtx);
4206 machine_mode mode = (machine_mode) va_arg (p, int);
4207 int unsigned_p = 0;
4208
4209 /* We cannot convert the arg value to the mode the library wants here;
4210 must do it earlier where we know the signedness of the arg. */
4211 gcc_assert (mode != BLKmode
4212 && (GET_MODE (val) == mode || GET_MODE (val) == VOIDmode));
4213
4214 /* Make sure it is a reasonable operand for a move or push insn. */
4215 if (!REG_P (val) && !MEM_P (val)
4216 && !(CONSTANT_P (val) && targetm.legitimate_constant_p (mode, val)))
4217 val = force_operand (val, NULL_RTX);
4218
4219 if (pass_by_reference (&args_so_far_v, mode, NULL_TREE, 1))
4220 {
4221 rtx slot;
4222 int must_copy
4223 = !reference_callee_copied (&args_so_far_v, mode, NULL_TREE, 1);
4224
4225 /* If this was a CONST function, it is now PURE since it now
4226 reads memory. */
4227 if (flags & ECF_CONST)
4228 {
4229 flags &= ~ECF_CONST;
4230 flags |= ECF_PURE;
4231 }
4232
4233 if (MEM_P (val) && !must_copy)
4234 {
4235 tree val_expr = MEM_EXPR (val);
4236 if (val_expr)
4237 mark_addressable (val_expr);
4238 slot = val;
4239 }
4240 else
4241 {
4242 slot = assign_temp (lang_hooks.types.type_for_mode (mode, 0),
4243 1, 1);
4244 emit_move_insn (slot, val);
4245 }
4246
4247 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4248 gen_rtx_USE (VOIDmode, slot),
4249 call_fusage);
4250 if (must_copy)
4251 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
4252 gen_rtx_CLOBBER (VOIDmode,
4253 slot),
4254 call_fusage);
4255
4256 mode = Pmode;
4257 val = force_operand (XEXP (slot, 0), NULL_RTX);
4258 }
4259
4260 mode = promote_function_mode (NULL_TREE, mode, &unsigned_p, NULL_TREE, 0);
4261 argvec[count].mode = mode;
4262 argvec[count].value = convert_modes (mode, GET_MODE (val), val, unsigned_p);
4263 argvec[count].reg = targetm.calls.function_arg (args_so_far, mode,
4264 NULL_TREE, true);
4265
4266 argvec[count].partial
4267 = targetm.calls.arg_partial_bytes (args_so_far, mode, NULL_TREE, 1);
4268
4269 if (argvec[count].reg == 0
4270 || argvec[count].partial != 0
4271 || reg_parm_stack_space > 0)
4272 {
4273 locate_and_pad_parm (mode, NULL_TREE,
4274 #ifdef STACK_PARMS_IN_REG_PARM_AREA
4275 1,
4276 #else
4277 argvec[count].reg != 0,
4278 #endif
4279 reg_parm_stack_space, argvec[count].partial,
4280 NULL_TREE, &args_size, &argvec[count].locate);
4281 args_size.constant += argvec[count].locate.size.constant;
4282 gcc_assert (!argvec[count].locate.size.var);
4283 }
4284 #ifdef BLOCK_REG_PADDING
4285 else
4286 /* The argument is passed entirely in registers. See at which
4287 end it should be padded. */
4288 argvec[count].locate.where_pad =
4289 BLOCK_REG_PADDING (mode, NULL_TREE,
4290 GET_MODE_SIZE (mode) <= UNITS_PER_WORD);
4291 #endif
4292
4293 targetm.calls.function_arg_advance (args_so_far, mode, (tree) 0, true);
4294 }
4295
4296 /* If this machine requires an external definition for library
4297 functions, write one out. */
4298 assemble_external_libcall (fun);
4299
4300 original_args_size = args_size;
4301 args_size.constant = (((args_size.constant
4302 + stack_pointer_delta
4303 + STACK_BYTES - 1)
4304 / STACK_BYTES
4305 * STACK_BYTES)
4306 - stack_pointer_delta);
4307
4308 args_size.constant = MAX (args_size.constant,
4309 reg_parm_stack_space);
4310
4311 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4312 args_size.constant -= reg_parm_stack_space;
4313
4314 if (args_size.constant > crtl->outgoing_args_size)
4315 crtl->outgoing_args_size = args_size.constant;
4316
4317 if (flag_stack_usage_info && !ACCUMULATE_OUTGOING_ARGS)
4318 {
4319 int pushed = args_size.constant + pending_stack_adjust;
4320 if (pushed > current_function_pushed_stack_size)
4321 current_function_pushed_stack_size = pushed;
4322 }
4323
4324 if (ACCUMULATE_OUTGOING_ARGS)
4325 {
4326 /* Since the stack pointer will never be pushed, it is possible for
4327 the evaluation of a parm to clobber something we have already
4328 written to the stack. Since most function calls on RISC machines
4329 do not use the stack, this is uncommon, but must work correctly.
4330
4331 Therefore, we save any area of the stack that was already written
4332 and that we are using. Here we set up to do this by making a new
4333 stack usage map from the old one.
4334
4335 Another approach might be to try to reorder the argument
4336 evaluations to avoid this conflicting stack usage. */
4337
4338 needed = args_size.constant;
4339
4340 /* Since we will be writing into the entire argument area, the
4341 map must be allocated for its entire size, not just the part that
4342 is the responsibility of the caller. */
4343 if (! OUTGOING_REG_PARM_STACK_SPACE ((!fndecl ? fntype : TREE_TYPE (fndecl))))
4344 needed += reg_parm_stack_space;
4345
4346 if (ARGS_GROW_DOWNWARD)
4347 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
4348 needed + 1);
4349 else
4350 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use, needed);
4351
4352 stack_usage_map_buf = XNEWVEC (char, highest_outgoing_arg_in_use);
4353 stack_usage_map = stack_usage_map_buf;
4354
4355 if (initial_highest_arg_in_use)
4356 memcpy (stack_usage_map, initial_stack_usage_map,
4357 initial_highest_arg_in_use);
4358
4359 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
4360 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
4361 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
4362 needed = 0;
4363
4364 /* We must be careful to use virtual regs before they're instantiated,
4365 and real regs afterwards. Loop optimization, for example, can create
4366 new libcalls after we've instantiated the virtual regs, and if we
4367 use virtuals anyway, they won't match the rtl patterns. */
4368
4369 if (virtuals_instantiated)
4370 argblock = plus_constant (Pmode, stack_pointer_rtx,
4371 STACK_POINTER_OFFSET);
4372 else
4373 argblock = virtual_outgoing_args_rtx;
4374 }
4375 else
4376 {
4377 if (!PUSH_ARGS)
4378 argblock = push_block (GEN_INT (args_size.constant), 0, 0);
4379 }
4380
4381 /* We push args individually in reverse order, perform stack alignment
4382 before the first push (the last arg). */
4383 if (argblock == 0)
4384 anti_adjust_stack (GEN_INT (args_size.constant
4385 - original_args_size.constant));
4386
4387 argnum = nargs - 1;
4388
4389 #ifdef REG_PARM_STACK_SPACE
4390 if (ACCUMULATE_OUTGOING_ARGS)
4391 {
4392 /* The argument list is the property of the called routine and it
4393 may clobber it. If the fixed area has been used for previous
4394 parameters, we must save and restore it. */
4395 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
4396 &low_to_save, &high_to_save);
4397 }
4398 #endif
4399
4400 /* When expanding a normal call, args are stored in push order,
4401 which is the reverse of what we have here. */
4402 bool any_regs = false;
4403 for (int i = nargs; i-- > 0; )
4404 if (argvec[i].reg != NULL_RTX)
4405 {
4406 targetm.calls.call_args (argvec[i].reg, NULL_TREE);
4407 any_regs = true;
4408 }
4409 if (!any_regs)
4410 targetm.calls.call_args (pc_rtx, NULL_TREE);
4411
4412 /* Push the args that need to be pushed. */
4413
4414 have_push_fusage = false;
4415
4416 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4417 are to be pushed. */
4418 for (count = 0; count < nargs; count++, argnum--)
4419 {
4420 machine_mode mode = argvec[argnum].mode;
4421 rtx val = argvec[argnum].value;
4422 rtx reg = argvec[argnum].reg;
4423 int partial = argvec[argnum].partial;
4424 unsigned int parm_align = argvec[argnum].locate.boundary;
4425 int lower_bound = 0, upper_bound = 0, i;
4426
4427 if (! (reg != 0 && partial == 0))
4428 {
4429 rtx use;
4430
4431 if (ACCUMULATE_OUTGOING_ARGS)
4432 {
4433 /* If this is being stored into a pre-allocated, fixed-size,
4434 stack area, save any previous data at that location. */
4435
4436 if (ARGS_GROW_DOWNWARD)
4437 {
4438 /* stack_slot is negative, but we want to index stack_usage_map
4439 with positive values. */
4440 upper_bound = -argvec[argnum].locate.slot_offset.constant + 1;
4441 lower_bound = upper_bound - argvec[argnum].locate.size.constant;
4442 }
4443 else
4444 {
4445 lower_bound = argvec[argnum].locate.slot_offset.constant;
4446 upper_bound = lower_bound + argvec[argnum].locate.size.constant;
4447 }
4448
4449 i = lower_bound;
4450 /* Don't worry about things in the fixed argument area;
4451 it has already been saved. */
4452 if (i < reg_parm_stack_space)
4453 i = reg_parm_stack_space;
4454 while (i < upper_bound && stack_usage_map[i] == 0)
4455 i++;
4456
4457 if (i < upper_bound)
4458 {
4459 /* We need to make a save area. */
4460 unsigned int size
4461 = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
4462 machine_mode save_mode
4463 = mode_for_size (size, MODE_INT, 1);
4464 rtx adr
4465 = plus_constant (Pmode, argblock,
4466 argvec[argnum].locate.offset.constant);
4467 rtx stack_area
4468 = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
4469
4470 if (save_mode == BLKmode)
4471 {
4472 argvec[argnum].save_area
4473 = assign_stack_temp (BLKmode,
4474 argvec[argnum].locate.size.constant
4475 );
4476
4477 emit_block_move (validize_mem
4478 (copy_rtx (argvec[argnum].save_area)),
4479 stack_area,
4480 GEN_INT (argvec[argnum].locate.size.constant),
4481 BLOCK_OP_CALL_PARM);
4482 }
4483 else
4484 {
4485 argvec[argnum].save_area = gen_reg_rtx (save_mode);
4486
4487 emit_move_insn (argvec[argnum].save_area, stack_area);
4488 }
4489 }
4490 }
4491
4492 emit_push_insn (val, mode, NULL_TREE, NULL_RTX, parm_align,
4493 partial, reg, 0, argblock,
4494 GEN_INT (argvec[argnum].locate.offset.constant),
4495 reg_parm_stack_space,
4496 ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad), false);
4497
4498 /* Now mark the segment we just used. */
4499 if (ACCUMULATE_OUTGOING_ARGS)
4500 for (i = lower_bound; i < upper_bound; i++)
4501 stack_usage_map[i] = 1;
4502
4503 NO_DEFER_POP;
4504
4505 /* Indicate argument access so that alias.c knows that these
4506 values are live. */
4507 if (argblock)
4508 use = plus_constant (Pmode, argblock,
4509 argvec[argnum].locate.offset.constant);
4510 else if (have_push_fusage)
4511 continue;
4512 else
4513 {
4514 /* When arguments are pushed, trying to tell alias.c where
4515 exactly this argument is won't work, because the
4516 auto-increment causes confusion. So we merely indicate
4517 that we access something with a known mode somewhere on
4518 the stack. */
4519 use = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4520 gen_rtx_SCRATCH (Pmode));
4521 have_push_fusage = true;
4522 }
4523 use = gen_rtx_MEM (argvec[argnum].mode, use);
4524 use = gen_rtx_USE (VOIDmode, use);
4525 call_fusage = gen_rtx_EXPR_LIST (VOIDmode, use, call_fusage);
4526 }
4527 }
4528
4529 argnum = nargs - 1;
4530
4531 fun = prepare_call_address (NULL, fun, NULL, &call_fusage, 0, 0);
4532
4533 /* Now load any reg parms into their regs. */
4534
4535 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
4536 are to be pushed. */
4537 for (count = 0; count < nargs; count++, argnum--)
4538 {
4539 machine_mode mode = argvec[argnum].mode;
4540 rtx val = argvec[argnum].value;
4541 rtx reg = argvec[argnum].reg;
4542 int partial = argvec[argnum].partial;
4543 #ifdef BLOCK_REG_PADDING
4544 int size = 0;
4545 #endif
4546
4547 /* Handle calls that pass values in multiple non-contiguous
4548 locations. The PA64 has examples of this for library calls. */
4549 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4550 emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (mode));
4551 else if (reg != 0 && partial == 0)
4552 {
4553 emit_move_insn (reg, val);
4554 #ifdef BLOCK_REG_PADDING
4555 size = GET_MODE_SIZE (argvec[argnum].mode);
4556
4557 /* Copied from load_register_parameters. */
4558
4559 /* Handle case where we have a value that needs shifting
4560 up to the msb. eg. a QImode value and we're padding
4561 upward on a BYTES_BIG_ENDIAN machine. */
4562 if (size < UNITS_PER_WORD
4563 && (argvec[argnum].locate.where_pad
4564 == (BYTES_BIG_ENDIAN ? upward : downward)))
4565 {
4566 rtx x;
4567 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
4568
4569 /* Assigning REG here rather than a temp makes CALL_FUSAGE
4570 report the whole reg as used. Strictly speaking, the
4571 call only uses SIZE bytes at the msb end, but it doesn't
4572 seem worth generating rtl to say that. */
4573 reg = gen_rtx_REG (word_mode, REGNO (reg));
4574 x = expand_shift (LSHIFT_EXPR, word_mode, reg, shift, reg, 1);
4575 if (x != reg)
4576 emit_move_insn (reg, x);
4577 }
4578 #endif
4579 }
4580
4581 NO_DEFER_POP;
4582 }
4583
4584 /* Any regs containing parms remain in use through the call. */
4585 for (count = 0; count < nargs; count++)
4586 {
4587 rtx reg = argvec[count].reg;
4588 if (reg != 0 && GET_CODE (reg) == PARALLEL)
4589 use_group_regs (&call_fusage, reg);
4590 else if (reg != 0)
4591 {
4592 int partial = argvec[count].partial;
4593 if (partial)
4594 {
4595 int nregs;
4596 gcc_assert (partial % UNITS_PER_WORD == 0);
4597 nregs = partial / UNITS_PER_WORD;
4598 use_regs (&call_fusage, REGNO (reg), nregs);
4599 }
4600 else
4601 use_reg (&call_fusage, reg);
4602 }
4603 }
4604
4605 /* Pass the function the address in which to return a structure value. */
4606 if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
4607 {
4608 emit_move_insn (struct_value,
4609 force_reg (Pmode,
4610 force_operand (XEXP (mem_value, 0),
4611 NULL_RTX)));
4612 if (REG_P (struct_value))
4613 use_reg (&call_fusage, struct_value);
4614 }
4615
4616 /* Don't allow popping to be deferred, since then
4617 cse'ing of library calls could delete a call and leave the pop. */
4618 NO_DEFER_POP;
4619 valreg = (mem_value == 0 && outmode != VOIDmode
4620 ? hard_libcall_value (outmode, orgfun) : NULL_RTX);
4621
4622 /* Stack must be properly aligned now. */
4623 gcc_assert (!(stack_pointer_delta
4624 & (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1)));
4625
4626 before_call = get_last_insn ();
4627
4628 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
4629 will set inhibit_defer_pop to that value. */
4630 /* The return type is needed to decide how many bytes the function pops.
4631 Signedness plays no role in that, so for simplicity, we pretend it's
4632 always signed. We also assume that the list of arguments passed has
4633 no impact, so we pretend it is unknown. */
4634
4635 emit_call_1 (fun, NULL,
4636 get_identifier (XSTR (orgfun, 0)),
4637 build_function_type (tfom, NULL_TREE),
4638 original_args_size.constant, args_size.constant,
4639 struct_value_size,
4640 targetm.calls.function_arg (args_so_far,
4641 VOIDmode, void_type_node, true),
4642 valreg,
4643 old_inhibit_defer_pop + 1, call_fusage, flags, args_so_far);
4644
4645 if (flag_ipa_ra)
4646 {
4647 rtx datum = orgfun;
4648 gcc_assert (GET_CODE (datum) == SYMBOL_REF);
4649 rtx_call_insn *last = last_call_insn ();
4650 add_reg_note (last, REG_CALL_DECL, datum);
4651 }
4652
4653 /* Right-shift returned value if necessary. */
4654 if (!pcc_struct_value
4655 && TYPE_MODE (tfom) != BLKmode
4656 && targetm.calls.return_in_msb (tfom))
4657 {
4658 shift_return_value (TYPE_MODE (tfom), false, valreg);
4659 valreg = gen_rtx_REG (TYPE_MODE (tfom), REGNO (valreg));
4660 }
4661
4662 targetm.calls.end_call_args ();
4663
4664 /* For calls to `setjmp', etc., inform function.c:setjmp_warnings
4665 that it should complain if nonvolatile values are live. For
4666 functions that cannot return, inform flow that control does not
4667 fall through. */
4668 if (flags & ECF_NORETURN)
4669 {
4670 /* The barrier note must be emitted
4671 immediately after the CALL_INSN. Some ports emit more than
4672 just a CALL_INSN above, so we must search for it here. */
4673 rtx_insn *last = get_last_insn ();
4674 while (!CALL_P (last))
4675 {
4676 last = PREV_INSN (last);
4677 /* There was no CALL_INSN? */
4678 gcc_assert (last != before_call);
4679 }
4680
4681 emit_barrier_after (last);
4682 }
4683
4684 /* Consider that "regular" libcalls, i.e. all of them except for LCT_THROW
4685 and LCT_RETURNS_TWICE, cannot perform non-local gotos. */
4686 if (flags & ECF_NOTHROW)
4687 {
4688 rtx_insn *last = get_last_insn ();
4689 while (!CALL_P (last))
4690 {
4691 last = PREV_INSN (last);
4692 /* There was no CALL_INSN? */
4693 gcc_assert (last != before_call);
4694 }
4695
4696 make_reg_eh_region_note_nothrow_nononlocal (last);
4697 }
4698
4699 /* Now restore inhibit_defer_pop to its actual original value. */
4700 OK_DEFER_POP;
4701
4702 pop_temp_slots ();
4703
4704 /* Copy the value to the right place. */
4705 if (outmode != VOIDmode && retval)
4706 {
4707 if (mem_value)
4708 {
4709 if (value == 0)
4710 value = mem_value;
4711 if (value != mem_value)
4712 emit_move_insn (value, mem_value);
4713 }
4714 else if (GET_CODE (valreg) == PARALLEL)
4715 {
4716 if (value == 0)
4717 value = gen_reg_rtx (outmode);
4718 emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
4719 }
4720 else
4721 {
4722 /* Convert to the proper mode if a promotion has been active. */
4723 if (GET_MODE (valreg) != outmode)
4724 {
4725 int unsignedp = TYPE_UNSIGNED (tfom);
4726
4727 gcc_assert (promote_function_mode (tfom, outmode, &unsignedp,
4728 fndecl ? TREE_TYPE (fndecl) : fntype, 1)
4729 == GET_MODE (valreg));
4730 valreg = convert_modes (outmode, GET_MODE (valreg), valreg, 0);
4731 }
4732
4733 if (value != 0)
4734 emit_move_insn (value, valreg);
4735 else
4736 value = valreg;
4737 }
4738 }
4739
4740 if (ACCUMULATE_OUTGOING_ARGS)
4741 {
4742 #ifdef REG_PARM_STACK_SPACE
4743 if (save_area)
4744 restore_fixed_argument_area (save_area, argblock,
4745 high_to_save, low_to_save);
4746 #endif
4747
4748 /* If we saved any argument areas, restore them. */
4749 for (count = 0; count < nargs; count++)
4750 if (argvec[count].save_area)
4751 {
4752 machine_mode save_mode = GET_MODE (argvec[count].save_area);
4753 rtx adr = plus_constant (Pmode, argblock,
4754 argvec[count].locate.offset.constant);
4755 rtx stack_area = gen_rtx_MEM (save_mode,
4756 memory_address (save_mode, adr));
4757
4758 if (save_mode == BLKmode)
4759 emit_block_move (stack_area,
4760 validize_mem
4761 (copy_rtx (argvec[count].save_area)),
4762 GEN_INT (argvec[count].locate.size.constant),
4763 BLOCK_OP_CALL_PARM);
4764 else
4765 emit_move_insn (stack_area, argvec[count].save_area);
4766 }
4767
4768 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
4769 stack_usage_map = initial_stack_usage_map;
4770 }
4771
4772 free (stack_usage_map_buf);
4773
4774 return value;
4775
4776 }
4777 \f
4778 /* Output a library call to function FUN (a SYMBOL_REF rtx)
4779 (emitting the queue unless NO_QUEUE is nonzero),
4780 for a value of mode OUTMODE,
4781 with NARGS different arguments, passed as alternating rtx values
4782 and machine_modes to convert them to.
4783
4784 FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for
4785 `const' calls, LCT_PURE for `pure' calls, or other LCT_ value for
4786 other types of library calls. */
4787
4788 void
4789 emit_library_call (rtx orgfun, enum libcall_type fn_type,
4790 machine_mode outmode, int nargs, ...)
4791 {
4792 va_list p;
4793
4794 va_start (p, nargs);
4795 emit_library_call_value_1 (0, orgfun, NULL_RTX, fn_type, outmode, nargs, p);
4796 va_end (p);
4797 }
4798 \f
4799 /* Like emit_library_call except that an extra argument, VALUE,
4800 comes second and says where to store the result.
4801 (If VALUE is zero, this function chooses a convenient way
4802 to return the value.
4803
4804 This function returns an rtx for where the value is to be found.
4805 If VALUE is nonzero, VALUE is returned. */
4806
4807 rtx
4808 emit_library_call_value (rtx orgfun, rtx value,
4809 enum libcall_type fn_type,
4810 machine_mode outmode, int nargs, ...)
4811 {
4812 rtx result;
4813 va_list p;
4814
4815 va_start (p, nargs);
4816 result = emit_library_call_value_1 (1, orgfun, value, fn_type, outmode,
4817 nargs, p);
4818 va_end (p);
4819
4820 return result;
4821 }
4822 \f
4823
4824 /* Store pointer bounds argument ARG into Bounds Table entry
4825 associated with PARM. */
4826 static void
4827 store_bounds (struct arg_data *arg, struct arg_data *parm)
4828 {
4829 rtx slot = NULL, ptr = NULL, addr = NULL;
4830
4831 /* We may pass bounds not associated with any pointer. */
4832 if (!parm)
4833 {
4834 gcc_assert (arg->special_slot);
4835 slot = arg->special_slot;
4836 ptr = const0_rtx;
4837 }
4838 /* Find pointer associated with bounds and where it is
4839 passed. */
4840 else
4841 {
4842 if (!parm->reg)
4843 {
4844 gcc_assert (!arg->special_slot);
4845
4846 addr = adjust_address (parm->stack, Pmode, arg->pointer_offset);
4847 }
4848 else if (REG_P (parm->reg))
4849 {
4850 gcc_assert (arg->special_slot);
4851 slot = arg->special_slot;
4852
4853 if (MEM_P (parm->value))
4854 addr = adjust_address (parm->value, Pmode, arg->pointer_offset);
4855 else if (REG_P (parm->value))
4856 ptr = gen_rtx_SUBREG (Pmode, parm->value, arg->pointer_offset);
4857 else
4858 {
4859 gcc_assert (!arg->pointer_offset);
4860 ptr = parm->value;
4861 }
4862 }
4863 else
4864 {
4865 gcc_assert (GET_CODE (parm->reg) == PARALLEL);
4866
4867 gcc_assert (arg->special_slot);
4868 slot = arg->special_slot;
4869
4870 if (parm->parallel_value)
4871 ptr = chkp_get_value_with_offs (parm->parallel_value,
4872 GEN_INT (arg->pointer_offset));
4873 else
4874 gcc_unreachable ();
4875 }
4876 }
4877
4878 /* Expand bounds. */
4879 if (!arg->value)
4880 arg->value = expand_normal (arg->tree_value);
4881
4882 targetm.calls.store_bounds_for_arg (ptr, addr, arg->value, slot);
4883 }
4884
4885 /* Store a single argument for a function call
4886 into the register or memory area where it must be passed.
4887 *ARG describes the argument value and where to pass it.
4888
4889 ARGBLOCK is the address of the stack-block for all the arguments,
4890 or 0 on a machine where arguments are pushed individually.
4891
4892 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
4893 so must be careful about how the stack is used.
4894
4895 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
4896 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
4897 that we need not worry about saving and restoring the stack.
4898
4899 FNDECL is the declaration of the function we are calling.
4900
4901 Return nonzero if this arg should cause sibcall failure,
4902 zero otherwise. */
4903
4904 static int
4905 store_one_arg (struct arg_data *arg, rtx argblock, int flags,
4906 int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
4907 {
4908 tree pval = arg->tree_value;
4909 rtx reg = 0;
4910 int partial = 0;
4911 int used = 0;
4912 int i, lower_bound = 0, upper_bound = 0;
4913 int sibcall_failure = 0;
4914
4915 if (TREE_CODE (pval) == ERROR_MARK)
4916 return 1;
4917
4918 /* Push a new temporary level for any temporaries we make for
4919 this argument. */
4920 push_temp_slots ();
4921
4922 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
4923 {
4924 /* If this is being stored into a pre-allocated, fixed-size, stack area,
4925 save any previous data at that location. */
4926 if (argblock && ! variable_size && arg->stack)
4927 {
4928 if (ARGS_GROW_DOWNWARD)
4929 {
4930 /* stack_slot is negative, but we want to index stack_usage_map
4931 with positive values. */
4932 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4933 upper_bound = -INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1)) + 1;
4934 else
4935 upper_bound = 0;
4936
4937 lower_bound = upper_bound - arg->locate.size.constant;
4938 }
4939 else
4940 {
4941 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4942 lower_bound = INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1));
4943 else
4944 lower_bound = 0;
4945
4946 upper_bound = lower_bound + arg->locate.size.constant;
4947 }
4948
4949 i = lower_bound;
4950 /* Don't worry about things in the fixed argument area;
4951 it has already been saved. */
4952 if (i < reg_parm_stack_space)
4953 i = reg_parm_stack_space;
4954 while (i < upper_bound && stack_usage_map[i] == 0)
4955 i++;
4956
4957 if (i < upper_bound)
4958 {
4959 /* We need to make a save area. */
4960 unsigned int size = arg->locate.size.constant * BITS_PER_UNIT;
4961 machine_mode save_mode = mode_for_size (size, MODE_INT, 1);
4962 rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
4963 rtx stack_area = gen_rtx_MEM (save_mode, adr);
4964
4965 if (save_mode == BLKmode)
4966 {
4967 arg->save_area
4968 = assign_temp (TREE_TYPE (arg->tree_value), 1, 1);
4969 preserve_temp_slots (arg->save_area);
4970 emit_block_move (validize_mem (copy_rtx (arg->save_area)),
4971 stack_area,
4972 GEN_INT (arg->locate.size.constant),
4973 BLOCK_OP_CALL_PARM);
4974 }
4975 else
4976 {
4977 arg->save_area = gen_reg_rtx (save_mode);
4978 emit_move_insn (arg->save_area, stack_area);
4979 }
4980 }
4981 }
4982 }
4983
4984 /* If this isn't going to be placed on both the stack and in registers,
4985 set up the register and number of words. */
4986 if (! arg->pass_on_stack)
4987 {
4988 if (flags & ECF_SIBCALL)
4989 reg = arg->tail_call_reg;
4990 else
4991 reg = arg->reg;
4992 partial = arg->partial;
4993 }
4994
4995 /* Being passed entirely in a register. We shouldn't be called in
4996 this case. */
4997 gcc_assert (reg == 0 || partial != 0);
4998
4999 /* If this arg needs special alignment, don't load the registers
5000 here. */
5001 if (arg->n_aligned_regs != 0)
5002 reg = 0;
5003
5004 /* If this is being passed partially in a register, we can't evaluate
5005 it directly into its stack slot. Otherwise, we can. */
5006 if (arg->value == 0)
5007 {
5008 /* stack_arg_under_construction is nonzero if a function argument is
5009 being evaluated directly into the outgoing argument list and
5010 expand_call must take special action to preserve the argument list
5011 if it is called recursively.
5012
5013 For scalar function arguments stack_usage_map is sufficient to
5014 determine which stack slots must be saved and restored. Scalar
5015 arguments in general have pass_on_stack == 0.
5016
5017 If this argument is initialized by a function which takes the
5018 address of the argument (a C++ constructor or a C function
5019 returning a BLKmode structure), then stack_usage_map is
5020 insufficient and expand_call must push the stack around the
5021 function call. Such arguments have pass_on_stack == 1.
5022
5023 Note that it is always safe to set stack_arg_under_construction,
5024 but this generates suboptimal code if set when not needed. */
5025
5026 if (arg->pass_on_stack)
5027 stack_arg_under_construction++;
5028
5029 arg->value = expand_expr (pval,
5030 (partial
5031 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
5032 ? NULL_RTX : arg->stack,
5033 VOIDmode, EXPAND_STACK_PARM);
5034
5035 /* If we are promoting object (or for any other reason) the mode
5036 doesn't agree, convert the mode. */
5037
5038 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
5039 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
5040 arg->value, arg->unsignedp);
5041
5042 if (arg->pass_on_stack)
5043 stack_arg_under_construction--;
5044 }
5045
5046 /* Check for overlap with already clobbered argument area. */
5047 if ((flags & ECF_SIBCALL)
5048 && MEM_P (arg->value)
5049 && mem_overlaps_already_clobbered_arg_p (XEXP (arg->value, 0),
5050 arg->locate.size.constant))
5051 sibcall_failure = 1;
5052
5053 /* Don't allow anything left on stack from computation
5054 of argument to alloca. */
5055 if (flags & ECF_MAY_BE_ALLOCA)
5056 do_pending_stack_adjust ();
5057
5058 if (arg->value == arg->stack)
5059 /* If the value is already in the stack slot, we are done. */
5060 ;
5061 else if (arg->mode != BLKmode)
5062 {
5063 int size;
5064 unsigned int parm_align;
5065
5066 /* Argument is a scalar, not entirely passed in registers.
5067 (If part is passed in registers, arg->partial says how much
5068 and emit_push_insn will take care of putting it there.)
5069
5070 Push it, and if its size is less than the
5071 amount of space allocated to it,
5072 also bump stack pointer by the additional space.
5073 Note that in C the default argument promotions
5074 will prevent such mismatches. */
5075
5076 size = GET_MODE_SIZE (arg->mode);
5077 /* Compute how much space the push instruction will push.
5078 On many machines, pushing a byte will advance the stack
5079 pointer by a halfword. */
5080 #ifdef PUSH_ROUNDING
5081 size = PUSH_ROUNDING (size);
5082 #endif
5083 used = size;
5084
5085 /* Compute how much space the argument should get:
5086 round up to a multiple of the alignment for arguments. */
5087 if (none != FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)))
5088 used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
5089 / (PARM_BOUNDARY / BITS_PER_UNIT))
5090 * (PARM_BOUNDARY / BITS_PER_UNIT));
5091
5092 /* Compute the alignment of the pushed argument. */
5093 parm_align = arg->locate.boundary;
5094 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
5095 {
5096 int pad = used - size;
5097 if (pad)
5098 {
5099 unsigned int pad_align = least_bit_hwi (pad) * BITS_PER_UNIT;
5100 parm_align = MIN (parm_align, pad_align);
5101 }
5102 }
5103
5104 /* This isn't already where we want it on the stack, so put it there.
5105 This can either be done with push or copy insns. */
5106 if (!emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), NULL_RTX,
5107 parm_align, partial, reg, used - size, argblock,
5108 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
5109 ARGS_SIZE_RTX (arg->locate.alignment_pad), true))
5110 sibcall_failure = 1;
5111
5112 /* Unless this is a partially-in-register argument, the argument is now
5113 in the stack. */
5114 if (partial == 0)
5115 arg->value = arg->stack;
5116 }
5117 else
5118 {
5119 /* BLKmode, at least partly to be pushed. */
5120
5121 unsigned int parm_align;
5122 int excess;
5123 rtx size_rtx;
5124
5125 /* Pushing a nonscalar.
5126 If part is passed in registers, PARTIAL says how much
5127 and emit_push_insn will take care of putting it there. */
5128
5129 /* Round its size up to a multiple
5130 of the allocation unit for arguments. */
5131
5132 if (arg->locate.size.var != 0)
5133 {
5134 excess = 0;
5135 size_rtx = ARGS_SIZE_RTX (arg->locate.size);
5136 }
5137 else
5138 {
5139 /* PUSH_ROUNDING has no effect on us, because emit_push_insn
5140 for BLKmode is careful to avoid it. */
5141 excess = (arg->locate.size.constant
5142 - int_size_in_bytes (TREE_TYPE (pval))
5143 + partial);
5144 size_rtx = expand_expr (size_in_bytes (TREE_TYPE (pval)),
5145 NULL_RTX, TYPE_MODE (sizetype),
5146 EXPAND_NORMAL);
5147 }
5148
5149 parm_align = arg->locate.boundary;
5150
5151 /* When an argument is padded down, the block is aligned to
5152 PARM_BOUNDARY, but the actual argument isn't. */
5153 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
5154 {
5155 if (arg->locate.size.var)
5156 parm_align = BITS_PER_UNIT;
5157 else if (excess)
5158 {
5159 unsigned int excess_align = least_bit_hwi (excess) * BITS_PER_UNIT;
5160 parm_align = MIN (parm_align, excess_align);
5161 }
5162 }
5163
5164 if ((flags & ECF_SIBCALL) && MEM_P (arg->value))
5165 {
5166 /* emit_push_insn might not work properly if arg->value and
5167 argblock + arg->locate.offset areas overlap. */
5168 rtx x = arg->value;
5169 int i = 0;
5170
5171 if (XEXP (x, 0) == crtl->args.internal_arg_pointer
5172 || (GET_CODE (XEXP (x, 0)) == PLUS
5173 && XEXP (XEXP (x, 0), 0) ==
5174 crtl->args.internal_arg_pointer
5175 && CONST_INT_P (XEXP (XEXP (x, 0), 1))))
5176 {
5177 if (XEXP (x, 0) != crtl->args.internal_arg_pointer)
5178 i = INTVAL (XEXP (XEXP (x, 0), 1));
5179
5180 /* arg.locate doesn't contain the pretend_args_size offset,
5181 it's part of argblock. Ensure we don't count it in I. */
5182 if (STACK_GROWS_DOWNWARD)
5183 i -= crtl->args.pretend_args_size;
5184 else
5185 i += crtl->args.pretend_args_size;
5186
5187 /* expand_call should ensure this. */
5188 gcc_assert (!arg->locate.offset.var
5189 && arg->locate.size.var == 0
5190 && CONST_INT_P (size_rtx));
5191
5192 if (arg->locate.offset.constant > i)
5193 {
5194 if (arg->locate.offset.constant < i + INTVAL (size_rtx))
5195 sibcall_failure = 1;
5196 }
5197 else if (arg->locate.offset.constant < i)
5198 {
5199 /* Use arg->locate.size.constant instead of size_rtx
5200 because we only care about the part of the argument
5201 on the stack. */
5202 if (i < (arg->locate.offset.constant
5203 + arg->locate.size.constant))
5204 sibcall_failure = 1;
5205 }
5206 else
5207 {
5208 /* Even though they appear to be at the same location,
5209 if part of the outgoing argument is in registers,
5210 they aren't really at the same location. Check for
5211 this by making sure that the incoming size is the
5212 same as the outgoing size. */
5213 if (arg->locate.size.constant != INTVAL (size_rtx))
5214 sibcall_failure = 1;
5215 }
5216 }
5217 }
5218
5219 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
5220 parm_align, partial, reg, excess, argblock,
5221 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
5222 ARGS_SIZE_RTX (arg->locate.alignment_pad), false);
5223
5224 /* Unless this is a partially-in-register argument, the argument is now
5225 in the stack.
5226
5227 ??? Unlike the case above, in which we want the actual
5228 address of the data, so that we can load it directly into a
5229 register, here we want the address of the stack slot, so that
5230 it's properly aligned for word-by-word copying or something
5231 like that. It's not clear that this is always correct. */
5232 if (partial == 0)
5233 arg->value = arg->stack_slot;
5234 }
5235
5236 if (arg->reg && GET_CODE (arg->reg) == PARALLEL)
5237 {
5238 tree type = TREE_TYPE (arg->tree_value);
5239 arg->parallel_value
5240 = emit_group_load_into_temps (arg->reg, arg->value, type,
5241 int_size_in_bytes (type));
5242 }
5243
5244 /* Mark all slots this store used. */
5245 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
5246 && argblock && ! variable_size && arg->stack)
5247 for (i = lower_bound; i < upper_bound; i++)
5248 stack_usage_map[i] = 1;
5249
5250 /* Once we have pushed something, pops can't safely
5251 be deferred during the rest of the arguments. */
5252 NO_DEFER_POP;
5253
5254 /* Free any temporary slots made in processing this argument. */
5255 pop_temp_slots ();
5256
5257 return sibcall_failure;
5258 }
5259
5260 /* Nonzero if we do not know how to pass TYPE solely in registers. */
5261
5262 bool
5263 must_pass_in_stack_var_size (machine_mode mode ATTRIBUTE_UNUSED,
5264 const_tree type)
5265 {
5266 if (!type)
5267 return false;
5268
5269 /* If the type has variable size... */
5270 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5271 return true;
5272
5273 /* If the type is marked as addressable (it is required
5274 to be constructed into the stack)... */
5275 if (TREE_ADDRESSABLE (type))
5276 return true;
5277
5278 return false;
5279 }
5280
5281 /* Another version of the TARGET_MUST_PASS_IN_STACK hook. This one
5282 takes trailing padding of a structure into account. */
5283 /* ??? Should be able to merge these two by examining BLOCK_REG_PADDING. */
5284
5285 bool
5286 must_pass_in_stack_var_size_or_pad (machine_mode mode, const_tree type)
5287 {
5288 if (!type)
5289 return false;
5290
5291 /* If the type has variable size... */
5292 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5293 return true;
5294
5295 /* If the type is marked as addressable (it is required
5296 to be constructed into the stack)... */
5297 if (TREE_ADDRESSABLE (type))
5298 return true;
5299
5300 /* If the padding and mode of the type is such that a copy into
5301 a register would put it into the wrong part of the register. */
5302 if (mode == BLKmode
5303 && int_size_in_bytes (type) % (PARM_BOUNDARY / BITS_PER_UNIT)
5304 && (FUNCTION_ARG_PADDING (mode, type)
5305 == (BYTES_BIG_ENDIAN ? upward : downward)))
5306 return true;
5307
5308 return false;
5309 }