re PR middle-end/17258 (gcc.c-torture/compile/20030224-1.c:16: internal compiler...
[gcc.git] / gcc / calls.c
1 /* Convert function calls to rtl insns, for GNU C compiler.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "flags.h"
29 #include "expr.h"
30 #include "optabs.h"
31 #include "libfuncs.h"
32 #include "function.h"
33 #include "regs.h"
34 #include "toplev.h"
35 #include "output.h"
36 #include "tm_p.h"
37 #include "timevar.h"
38 #include "sbitmap.h"
39 #include "langhooks.h"
40 #include "target.h"
41 #include "cgraph.h"
42 #include "except.h"
43
44 /* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits. */
45 #define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
46
47 /* Data structure and subroutines used within expand_call. */
48
49 struct arg_data
50 {
51 /* Tree node for this argument. */
52 tree tree_value;
53 /* Mode for value; TYPE_MODE unless promoted. */
54 enum machine_mode mode;
55 /* Current RTL value for argument, or 0 if it isn't precomputed. */
56 rtx value;
57 /* Initially-compute RTL value for argument; only for const functions. */
58 rtx initial_value;
59 /* Register to pass this argument in, 0 if passed on stack, or an
60 PARALLEL if the arg is to be copied into multiple non-contiguous
61 registers. */
62 rtx reg;
63 /* Register to pass this argument in when generating tail call sequence.
64 This is not the same register as for normal calls on machines with
65 register windows. */
66 rtx tail_call_reg;
67 /* If REG was promoted from the actual mode of the argument expression,
68 indicates whether the promotion is sign- or zero-extended. */
69 int unsignedp;
70 /* Number of registers to use. 0 means put the whole arg in registers.
71 Also 0 if not passed in registers. */
72 int partial;
73 /* Nonzero if argument must be passed on stack.
74 Note that some arguments may be passed on the stack
75 even though pass_on_stack is zero, just because FUNCTION_ARG says so.
76 pass_on_stack identifies arguments that *cannot* go in registers. */
77 int pass_on_stack;
78 /* Some fields packaged up for locate_and_pad_parm. */
79 struct locate_and_pad_arg_data locate;
80 /* Location on the stack at which parameter should be stored. The store
81 has already been done if STACK == VALUE. */
82 rtx stack;
83 /* Location on the stack of the start of this argument slot. This can
84 differ from STACK if this arg pads downward. This location is known
85 to be aligned to FUNCTION_ARG_BOUNDARY. */
86 rtx stack_slot;
87 /* Place that this stack area has been saved, if needed. */
88 rtx save_area;
89 /* If an argument's alignment does not permit direct copying into registers,
90 copy in smaller-sized pieces into pseudos. These are stored in a
91 block pointed to by this field. The next field says how many
92 word-sized pseudos we made. */
93 rtx *aligned_regs;
94 int n_aligned_regs;
95 };
96
97 /* A vector of one char per byte of stack space. A byte if nonzero if
98 the corresponding stack location has been used.
99 This vector is used to prevent a function call within an argument from
100 clobbering any stack already set up. */
101 static char *stack_usage_map;
102
103 /* Size of STACK_USAGE_MAP. */
104 static int highest_outgoing_arg_in_use;
105
106 /* A bitmap of virtual-incoming stack space. Bit is set if the corresponding
107 stack location's tail call argument has been already stored into the stack.
108 This bitmap is used to prevent sibling call optimization if function tries
109 to use parent's incoming argument slots when they have been already
110 overwritten with tail call arguments. */
111 static sbitmap stored_args_map;
112
113 /* stack_arg_under_construction is nonzero when an argument may be
114 initialized with a constructor call (including a C function that
115 returns a BLKmode struct) and expand_call must take special action
116 to make sure the object being constructed does not overlap the
117 argument list for the constructor call. */
118 int stack_arg_under_construction;
119
120 static void emit_call_1 (rtx, tree, tree, tree, HOST_WIDE_INT, HOST_WIDE_INT,
121 HOST_WIDE_INT, rtx, rtx, int, rtx, int,
122 CUMULATIVE_ARGS *);
123 static void precompute_register_parameters (int, struct arg_data *, int *);
124 static int store_one_arg (struct arg_data *, rtx, int, int, int);
125 static void store_unaligned_arguments_into_pseudos (struct arg_data *, int);
126 static int finalize_must_preallocate (int, int, struct arg_data *,
127 struct args_size *);
128 static void precompute_arguments (int, int, struct arg_data *);
129 static int compute_argument_block_size (int, struct args_size *, int);
130 static void initialize_argument_information (int, struct arg_data *,
131 struct args_size *, int, tree,
132 tree, CUMULATIVE_ARGS *, int,
133 rtx *, int *, int *, int *,
134 bool *, bool);
135 static void compute_argument_addresses (struct arg_data *, rtx, int);
136 static rtx rtx_for_function_call (tree, tree);
137 static void load_register_parameters (struct arg_data *, int, rtx *, int,
138 int, int *);
139 static rtx emit_library_call_value_1 (int, rtx, rtx, enum libcall_type,
140 enum machine_mode, int, va_list);
141 static int special_function_p (tree, int);
142 static int check_sibcall_argument_overlap_1 (rtx);
143 static int check_sibcall_argument_overlap (rtx, struct arg_data *, int);
144
145 static int combine_pending_stack_adjustment_and_call (int, struct args_size *,
146 unsigned int);
147 static bool shift_returned_value (tree, rtx *);
148
149 #ifdef REG_PARM_STACK_SPACE
150 static rtx save_fixed_argument_area (int, rtx, int *, int *);
151 static void restore_fixed_argument_area (rtx, rtx, int, int);
152 #endif
153 \f
154 /* Force FUNEXP into a form suitable for the address of a CALL,
155 and return that as an rtx. Also load the static chain register
156 if FNDECL is a nested function.
157
158 CALL_FUSAGE points to a variable holding the prospective
159 CALL_INSN_FUNCTION_USAGE information. */
160
161 rtx
162 prepare_call_address (rtx funexp, rtx static_chain_value,
163 rtx *call_fusage, int reg_parm_seen, int sibcallp)
164 {
165 /* Make a valid memory address and copy constants through pseudo-regs,
166 but not for a constant address if -fno-function-cse. */
167 if (GET_CODE (funexp) != SYMBOL_REF)
168 /* If we are using registers for parameters, force the
169 function address into a register now. */
170 funexp = ((SMALL_REGISTER_CLASSES && reg_parm_seen)
171 ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
172 : memory_address (FUNCTION_MODE, funexp));
173 else if (! sibcallp)
174 {
175 #ifndef NO_FUNCTION_CSE
176 if (optimize && ! flag_no_function_cse)
177 funexp = force_reg (Pmode, funexp);
178 #endif
179 }
180
181 if (static_chain_value != 0)
182 {
183 static_chain_value = convert_memory_address (Pmode, static_chain_value);
184 emit_move_insn (static_chain_rtx, static_chain_value);
185
186 if (REG_P (static_chain_rtx))
187 use_reg (call_fusage, static_chain_rtx);
188 }
189
190 return funexp;
191 }
192
193 /* Generate instructions to call function FUNEXP,
194 and optionally pop the results.
195 The CALL_INSN is the first insn generated.
196
197 FNDECL is the declaration node of the function. This is given to the
198 macro RETURN_POPS_ARGS to determine whether this function pops its own args.
199
200 FUNTYPE is the data type of the function. This is given to the macro
201 RETURN_POPS_ARGS to determine whether this function pops its own args.
202 We used to allow an identifier for library functions, but that doesn't
203 work when the return type is an aggregate type and the calling convention
204 says that the pointer to this aggregate is to be popped by the callee.
205
206 STACK_SIZE is the number of bytes of arguments on the stack,
207 ROUNDED_STACK_SIZE is that number rounded up to
208 PREFERRED_STACK_BOUNDARY; zero if the size is variable. This is
209 both to put into the call insn and to generate explicit popping
210 code if necessary.
211
212 STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
213 It is zero if this call doesn't want a structure value.
214
215 NEXT_ARG_REG is the rtx that results from executing
216 FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1)
217 just after all the args have had their registers assigned.
218 This could be whatever you like, but normally it is the first
219 arg-register beyond those used for args in this call,
220 or 0 if all the arg-registers are used in this call.
221 It is passed on to `gen_call' so you can put this info in the call insn.
222
223 VALREG is a hard register in which a value is returned,
224 or 0 if the call does not return a value.
225
226 OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
227 the args to this call were processed.
228 We restore `inhibit_defer_pop' to that value.
229
230 CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
231 denote registers used by the called function. */
232
233 static void
234 emit_call_1 (rtx funexp, tree fntree, tree fndecl ATTRIBUTE_UNUSED,
235 tree funtype ATTRIBUTE_UNUSED,
236 HOST_WIDE_INT stack_size ATTRIBUTE_UNUSED,
237 HOST_WIDE_INT rounded_stack_size,
238 HOST_WIDE_INT struct_value_size ATTRIBUTE_UNUSED,
239 rtx next_arg_reg ATTRIBUTE_UNUSED, rtx valreg,
240 int old_inhibit_defer_pop, rtx call_fusage, int ecf_flags,
241 CUMULATIVE_ARGS *args_so_far ATTRIBUTE_UNUSED)
242 {
243 rtx rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
244 rtx call_insn;
245 int already_popped = 0;
246 HOST_WIDE_INT n_popped = RETURN_POPS_ARGS (fndecl, funtype, stack_size);
247 #if defined (HAVE_call) && defined (HAVE_call_value)
248 rtx struct_value_size_rtx;
249 struct_value_size_rtx = GEN_INT (struct_value_size);
250 #endif
251
252 #ifdef CALL_POPS_ARGS
253 n_popped += CALL_POPS_ARGS (* args_so_far);
254 #endif
255
256 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
257 and we don't want to load it into a register as an optimization,
258 because prepare_call_address already did it if it should be done. */
259 if (GET_CODE (funexp) != SYMBOL_REF)
260 funexp = memory_address (FUNCTION_MODE, funexp);
261
262 #if defined (HAVE_sibcall_pop) && defined (HAVE_sibcall_value_pop)
263 if ((ecf_flags & ECF_SIBCALL)
264 && HAVE_sibcall_pop && HAVE_sibcall_value_pop
265 && (n_popped > 0 || stack_size == 0))
266 {
267 rtx n_pop = GEN_INT (n_popped);
268 rtx pat;
269
270 /* If this subroutine pops its own args, record that in the call insn
271 if possible, for the sake of frame pointer elimination. */
272
273 if (valreg)
274 pat = GEN_SIBCALL_VALUE_POP (valreg,
275 gen_rtx_MEM (FUNCTION_MODE, funexp),
276 rounded_stack_size_rtx, next_arg_reg,
277 n_pop);
278 else
279 pat = GEN_SIBCALL_POP (gen_rtx_MEM (FUNCTION_MODE, funexp),
280 rounded_stack_size_rtx, next_arg_reg, n_pop);
281
282 emit_call_insn (pat);
283 already_popped = 1;
284 }
285 else
286 #endif
287
288 #if defined (HAVE_call_pop) && defined (HAVE_call_value_pop)
289 /* If the target has "call" or "call_value" insns, then prefer them
290 if no arguments are actually popped. If the target does not have
291 "call" or "call_value" insns, then we must use the popping versions
292 even if the call has no arguments to pop. */
293 #if defined (HAVE_call) && defined (HAVE_call_value)
294 if (HAVE_call && HAVE_call_value && HAVE_call_pop && HAVE_call_value_pop
295 && n_popped > 0 && ! (ecf_flags & ECF_SP_DEPRESSED))
296 #else
297 if (HAVE_call_pop && HAVE_call_value_pop)
298 #endif
299 {
300 rtx n_pop = GEN_INT (n_popped);
301 rtx pat;
302
303 /* If this subroutine pops its own args, record that in the call insn
304 if possible, for the sake of frame pointer elimination. */
305
306 if (valreg)
307 pat = GEN_CALL_VALUE_POP (valreg,
308 gen_rtx_MEM (FUNCTION_MODE, funexp),
309 rounded_stack_size_rtx, next_arg_reg, n_pop);
310 else
311 pat = GEN_CALL_POP (gen_rtx_MEM (FUNCTION_MODE, funexp),
312 rounded_stack_size_rtx, next_arg_reg, n_pop);
313
314 emit_call_insn (pat);
315 already_popped = 1;
316 }
317 else
318 #endif
319
320 #if defined (HAVE_sibcall) && defined (HAVE_sibcall_value)
321 if ((ecf_flags & ECF_SIBCALL)
322 && HAVE_sibcall && HAVE_sibcall_value)
323 {
324 if (valreg)
325 emit_call_insn (GEN_SIBCALL_VALUE (valreg,
326 gen_rtx_MEM (FUNCTION_MODE, funexp),
327 rounded_stack_size_rtx,
328 next_arg_reg, NULL_RTX));
329 else
330 emit_call_insn (GEN_SIBCALL (gen_rtx_MEM (FUNCTION_MODE, funexp),
331 rounded_stack_size_rtx, next_arg_reg,
332 struct_value_size_rtx));
333 }
334 else
335 #endif
336
337 #if defined (HAVE_call) && defined (HAVE_call_value)
338 if (HAVE_call && HAVE_call_value)
339 {
340 if (valreg)
341 emit_call_insn (GEN_CALL_VALUE (valreg,
342 gen_rtx_MEM (FUNCTION_MODE, funexp),
343 rounded_stack_size_rtx, next_arg_reg,
344 NULL_RTX));
345 else
346 emit_call_insn (GEN_CALL (gen_rtx_MEM (FUNCTION_MODE, funexp),
347 rounded_stack_size_rtx, next_arg_reg,
348 struct_value_size_rtx));
349 }
350 else
351 #endif
352 abort ();
353
354 /* Find the call we just emitted. */
355 call_insn = last_call_insn ();
356
357 /* Mark memory as used for "pure" function call. */
358 if (ecf_flags & ECF_PURE)
359 call_fusage
360 = gen_rtx_EXPR_LIST
361 (VOIDmode,
362 gen_rtx_USE (VOIDmode,
363 gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode))),
364 call_fusage);
365
366 /* Put the register usage information there. */
367 add_function_usage_to (call_insn, call_fusage);
368
369 /* If this is a const call, then set the insn's unchanging bit. */
370 if (ecf_flags & (ECF_CONST | ECF_PURE))
371 CONST_OR_PURE_CALL_P (call_insn) = 1;
372
373 /* If this call can't throw, attach a REG_EH_REGION reg note to that
374 effect. */
375 if (ecf_flags & ECF_NOTHROW)
376 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, const0_rtx,
377 REG_NOTES (call_insn));
378 else
379 {
380 int rn = lookup_stmt_eh_region (fntree);
381
382 /* If rn < 0, then either (1) tree-ssa not used or (2) doesn't
383 throw, which we already took care of. */
384 if (rn > 0)
385 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, GEN_INT (rn),
386 REG_NOTES (call_insn));
387 note_current_region_may_contain_throw ();
388 }
389
390 if (ecf_flags & ECF_NORETURN)
391 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_NORETURN, const0_rtx,
392 REG_NOTES (call_insn));
393 if (ecf_flags & ECF_ALWAYS_RETURN)
394 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_ALWAYS_RETURN, const0_rtx,
395 REG_NOTES (call_insn));
396
397 if (ecf_flags & ECF_RETURNS_TWICE)
398 {
399 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_SETJMP, const0_rtx,
400 REG_NOTES (call_insn));
401 current_function_calls_setjmp = 1;
402 }
403
404 SIBLING_CALL_P (call_insn) = ((ecf_flags & ECF_SIBCALL) != 0);
405
406 /* Restore this now, so that we do defer pops for this call's args
407 if the context of the call as a whole permits. */
408 inhibit_defer_pop = old_inhibit_defer_pop;
409
410 if (n_popped > 0)
411 {
412 if (!already_popped)
413 CALL_INSN_FUNCTION_USAGE (call_insn)
414 = gen_rtx_EXPR_LIST (VOIDmode,
415 gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx),
416 CALL_INSN_FUNCTION_USAGE (call_insn));
417 rounded_stack_size -= n_popped;
418 rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
419 stack_pointer_delta -= n_popped;
420 }
421
422 if (!ACCUMULATE_OUTGOING_ARGS)
423 {
424 /* If returning from the subroutine does not automatically pop the args,
425 we need an instruction to pop them sooner or later.
426 Perhaps do it now; perhaps just record how much space to pop later.
427
428 If returning from the subroutine does pop the args, indicate that the
429 stack pointer will be changed. */
430
431 if (rounded_stack_size != 0)
432 {
433 if (ecf_flags & (ECF_SP_DEPRESSED | ECF_NORETURN | ECF_LONGJMP))
434 /* Just pretend we did the pop. */
435 stack_pointer_delta -= rounded_stack_size;
436 else if (flag_defer_pop && inhibit_defer_pop == 0
437 && ! (ecf_flags & (ECF_CONST | ECF_PURE)))
438 pending_stack_adjust += rounded_stack_size;
439 else
440 adjust_stack (rounded_stack_size_rtx);
441 }
442 }
443 /* When we accumulate outgoing args, we must avoid any stack manipulations.
444 Restore the stack pointer to its original value now. Usually
445 ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
446 On i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
447 popping variants of functions exist as well.
448
449 ??? We may optimize similar to defer_pop above, but it is
450 probably not worthwhile.
451
452 ??? It will be worthwhile to enable combine_stack_adjustments even for
453 such machines. */
454 else if (n_popped)
455 anti_adjust_stack (GEN_INT (n_popped));
456 }
457
458 /* Determine if the function identified by NAME and FNDECL is one with
459 special properties we wish to know about.
460
461 For example, if the function might return more than one time (setjmp), then
462 set RETURNS_TWICE to a nonzero value.
463
464 Similarly set LONGJMP for if the function is in the longjmp family.
465
466 Set MAY_BE_ALLOCA for any memory allocation function that might allocate
467 space from the stack such as alloca. */
468
469 static int
470 special_function_p (tree fndecl, int flags)
471 {
472 if (fndecl && DECL_NAME (fndecl)
473 && IDENTIFIER_LENGTH (DECL_NAME (fndecl)) <= 17
474 /* Exclude functions not at the file scope, or not `extern',
475 since they are not the magic functions we would otherwise
476 think they are.
477 FIXME: this should be handled with attributes, not with this
478 hacky imitation of DECL_ASSEMBLER_NAME. It's (also) wrong
479 because you can declare fork() inside a function if you
480 wish. */
481 && (DECL_CONTEXT (fndecl) == NULL_TREE
482 || TREE_CODE (DECL_CONTEXT (fndecl)) == TRANSLATION_UNIT_DECL)
483 && TREE_PUBLIC (fndecl))
484 {
485 const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
486 const char *tname = name;
487
488 /* We assume that alloca will always be called by name. It
489 makes no sense to pass it as a pointer-to-function to
490 anything that does not understand its behavior. */
491 if (((IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 6
492 && name[0] == 'a'
493 && ! strcmp (name, "alloca"))
494 || (IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 16
495 && name[0] == '_'
496 && ! strcmp (name, "__builtin_alloca"))))
497 flags |= ECF_MAY_BE_ALLOCA;
498
499 /* Disregard prefix _, __ or __x. */
500 if (name[0] == '_')
501 {
502 if (name[1] == '_' && name[2] == 'x')
503 tname += 3;
504 else if (name[1] == '_')
505 tname += 2;
506 else
507 tname += 1;
508 }
509
510 if (tname[0] == 's')
511 {
512 if ((tname[1] == 'e'
513 && (! strcmp (tname, "setjmp")
514 || ! strcmp (tname, "setjmp_syscall")))
515 || (tname[1] == 'i'
516 && ! strcmp (tname, "sigsetjmp"))
517 || (tname[1] == 'a'
518 && ! strcmp (tname, "savectx")))
519 flags |= ECF_RETURNS_TWICE;
520
521 if (tname[1] == 'i'
522 && ! strcmp (tname, "siglongjmp"))
523 flags |= ECF_LONGJMP;
524 }
525 else if ((tname[0] == 'q' && tname[1] == 's'
526 && ! strcmp (tname, "qsetjmp"))
527 || (tname[0] == 'v' && tname[1] == 'f'
528 && ! strcmp (tname, "vfork")))
529 flags |= ECF_RETURNS_TWICE;
530
531 else if (tname[0] == 'l' && tname[1] == 'o'
532 && ! strcmp (tname, "longjmp"))
533 flags |= ECF_LONGJMP;
534 }
535
536 return flags;
537 }
538
539 /* Return nonzero when tree represent call to longjmp. */
540
541 int
542 setjmp_call_p (tree fndecl)
543 {
544 return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
545 }
546
547 /* Return true when exp contains alloca call. */
548 bool
549 alloca_call_p (tree exp)
550 {
551 if (TREE_CODE (exp) == CALL_EXPR
552 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
553 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
554 == FUNCTION_DECL)
555 && (special_function_p (TREE_OPERAND (TREE_OPERAND (exp, 0), 0),
556 0) & ECF_MAY_BE_ALLOCA))
557 return true;
558 return false;
559 }
560
561 /* Detect flags (function attributes) from the function decl or type node. */
562
563 int
564 flags_from_decl_or_type (tree exp)
565 {
566 int flags = 0;
567 tree type = exp;
568
569 if (DECL_P (exp))
570 {
571 struct cgraph_rtl_info *i = cgraph_rtl_info (exp);
572 type = TREE_TYPE (exp);
573
574 if (i)
575 {
576 if (i->pure_function)
577 flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
578 if (i->const_function)
579 flags |= ECF_CONST | ECF_LIBCALL_BLOCK;
580 }
581
582 /* The function exp may have the `malloc' attribute. */
583 if (DECL_IS_MALLOC (exp))
584 flags |= ECF_MALLOC;
585
586 /* The function exp may have the `pure' attribute. */
587 if (DECL_IS_PURE (exp))
588 flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
589
590 if (TREE_NOTHROW (exp))
591 flags |= ECF_NOTHROW;
592
593 if (TREE_READONLY (exp) && ! TREE_THIS_VOLATILE (exp))
594 flags |= ECF_LIBCALL_BLOCK | ECF_CONST;
595
596 flags = special_function_p (exp, flags);
597 }
598 else if (TYPE_P (exp) && TYPE_READONLY (exp) && ! TREE_THIS_VOLATILE (exp))
599 flags |= ECF_CONST;
600
601 if (TREE_THIS_VOLATILE (exp))
602 flags |= ECF_NORETURN;
603
604 /* Mark if the function returns with the stack pointer depressed. We
605 cannot consider it pure or constant in that case. */
606 if (TREE_CODE (type) == FUNCTION_TYPE && TYPE_RETURNS_STACK_DEPRESSED (type))
607 {
608 flags |= ECF_SP_DEPRESSED;
609 flags &= ~(ECF_PURE | ECF_CONST | ECF_LIBCALL_BLOCK);
610 }
611
612 return flags;
613 }
614
615 /* Detect flags from a CALL_EXPR. */
616
617 int
618 call_expr_flags (tree t)
619 {
620 int flags;
621 tree decl = get_callee_fndecl (t);
622
623 if (decl)
624 flags = flags_from_decl_or_type (decl);
625 else
626 {
627 t = TREE_TYPE (TREE_OPERAND (t, 0));
628 if (t && TREE_CODE (t) == POINTER_TYPE)
629 flags = flags_from_decl_or_type (TREE_TYPE (t));
630 else
631 flags = 0;
632 }
633
634 return flags;
635 }
636
637 /* Precompute all register parameters as described by ARGS, storing values
638 into fields within the ARGS array.
639
640 NUM_ACTUALS indicates the total number elements in the ARGS array.
641
642 Set REG_PARM_SEEN if we encounter a register parameter. */
643
644 static void
645 precompute_register_parameters (int num_actuals, struct arg_data *args, int *reg_parm_seen)
646 {
647 int i;
648
649 *reg_parm_seen = 0;
650
651 for (i = 0; i < num_actuals; i++)
652 if (args[i].reg != 0 && ! args[i].pass_on_stack)
653 {
654 *reg_parm_seen = 1;
655
656 if (args[i].value == 0)
657 {
658 push_temp_slots ();
659 args[i].value = expand_expr (args[i].tree_value, NULL_RTX,
660 VOIDmode, 0);
661 preserve_temp_slots (args[i].value);
662 pop_temp_slots ();
663 }
664
665 /* If the value is a non-legitimate constant, force it into a
666 pseudo now. TLS symbols sometimes need a call to resolve. */
667 if (CONSTANT_P (args[i].value)
668 && !LEGITIMATE_CONSTANT_P (args[i].value))
669 args[i].value = force_reg (args[i].mode, args[i].value);
670
671 /* If we are to promote the function arg to a wider mode,
672 do it now. */
673
674 if (args[i].mode != TYPE_MODE (TREE_TYPE (args[i].tree_value)))
675 args[i].value
676 = convert_modes (args[i].mode,
677 TYPE_MODE (TREE_TYPE (args[i].tree_value)),
678 args[i].value, args[i].unsignedp);
679
680 /* If the value is expensive, and we are inside an appropriately
681 short loop, put the value into a pseudo and then put the pseudo
682 into the hard reg.
683
684 For small register classes, also do this if this call uses
685 register parameters. This is to avoid reload conflicts while
686 loading the parameters registers. */
687
688 if ((! (REG_P (args[i].value)
689 || (GET_CODE (args[i].value) == SUBREG
690 && REG_P (SUBREG_REG (args[i].value)))))
691 && args[i].mode != BLKmode
692 && rtx_cost (args[i].value, SET) > COSTS_N_INSNS (1)
693 && ((SMALL_REGISTER_CLASSES && *reg_parm_seen)
694 || optimize))
695 args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
696 }
697 }
698
699 #ifdef REG_PARM_STACK_SPACE
700
701 /* The argument list is the property of the called routine and it
702 may clobber it. If the fixed area has been used for previous
703 parameters, we must save and restore it. */
704
705 static rtx
706 save_fixed_argument_area (int reg_parm_stack_space, rtx argblock, int *low_to_save, int *high_to_save)
707 {
708 int low;
709 int high;
710
711 /* Compute the boundary of the area that needs to be saved, if any. */
712 high = reg_parm_stack_space;
713 #ifdef ARGS_GROW_DOWNWARD
714 high += 1;
715 #endif
716 if (high > highest_outgoing_arg_in_use)
717 high = highest_outgoing_arg_in_use;
718
719 for (low = 0; low < high; low++)
720 if (stack_usage_map[low] != 0)
721 {
722 int num_to_save;
723 enum machine_mode save_mode;
724 int delta;
725 rtx stack_area;
726 rtx save_area;
727
728 while (stack_usage_map[--high] == 0)
729 ;
730
731 *low_to_save = low;
732 *high_to_save = high;
733
734 num_to_save = high - low + 1;
735 save_mode = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
736
737 /* If we don't have the required alignment, must do this
738 in BLKmode. */
739 if ((low & (MIN (GET_MODE_SIZE (save_mode),
740 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
741 save_mode = BLKmode;
742
743 #ifdef ARGS_GROW_DOWNWARD
744 delta = -high;
745 #else
746 delta = low;
747 #endif
748 stack_area = gen_rtx_MEM (save_mode,
749 memory_address (save_mode,
750 plus_constant (argblock,
751 delta)));
752
753 set_mem_align (stack_area, PARM_BOUNDARY);
754 if (save_mode == BLKmode)
755 {
756 save_area = assign_stack_temp (BLKmode, num_to_save, 0);
757 emit_block_move (validize_mem (save_area), stack_area,
758 GEN_INT (num_to_save), BLOCK_OP_CALL_PARM);
759 }
760 else
761 {
762 save_area = gen_reg_rtx (save_mode);
763 emit_move_insn (save_area, stack_area);
764 }
765
766 return save_area;
767 }
768
769 return NULL_RTX;
770 }
771
772 static void
773 restore_fixed_argument_area (rtx save_area, rtx argblock, int high_to_save, int low_to_save)
774 {
775 enum machine_mode save_mode = GET_MODE (save_area);
776 int delta;
777 rtx stack_area;
778
779 #ifdef ARGS_GROW_DOWNWARD
780 delta = -high_to_save;
781 #else
782 delta = low_to_save;
783 #endif
784 stack_area = gen_rtx_MEM (save_mode,
785 memory_address (save_mode,
786 plus_constant (argblock, delta)));
787 set_mem_align (stack_area, PARM_BOUNDARY);
788
789 if (save_mode != BLKmode)
790 emit_move_insn (stack_area, save_area);
791 else
792 emit_block_move (stack_area, validize_mem (save_area),
793 GEN_INT (high_to_save - low_to_save + 1),
794 BLOCK_OP_CALL_PARM);
795 }
796 #endif /* REG_PARM_STACK_SPACE */
797
798 /* If any elements in ARGS refer to parameters that are to be passed in
799 registers, but not in memory, and whose alignment does not permit a
800 direct copy into registers. Copy the values into a group of pseudos
801 which we will later copy into the appropriate hard registers.
802
803 Pseudos for each unaligned argument will be stored into the array
804 args[argnum].aligned_regs. The caller is responsible for deallocating
805 the aligned_regs array if it is nonzero. */
806
807 static void
808 store_unaligned_arguments_into_pseudos (struct arg_data *args, int num_actuals)
809 {
810 int i, j;
811
812 for (i = 0; i < num_actuals; i++)
813 if (args[i].reg != 0 && ! args[i].pass_on_stack
814 && args[i].mode == BLKmode
815 && (TYPE_ALIGN (TREE_TYPE (args[i].tree_value))
816 < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
817 {
818 int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
819 int nregs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
820 int endian_correction = 0;
821
822 args[i].n_aligned_regs = args[i].partial ? args[i].partial : nregs;
823 args[i].aligned_regs = xmalloc (sizeof (rtx) * args[i].n_aligned_regs);
824
825 /* Structures smaller than a word are normally aligned to the
826 least significant byte. On a BYTES_BIG_ENDIAN machine,
827 this means we must skip the empty high order bytes when
828 calculating the bit offset. */
829 if (bytes < UNITS_PER_WORD
830 #ifdef BLOCK_REG_PADDING
831 && (BLOCK_REG_PADDING (args[i].mode,
832 TREE_TYPE (args[i].tree_value), 1)
833 == downward)
834 #else
835 && BYTES_BIG_ENDIAN
836 #endif
837 )
838 endian_correction = BITS_PER_WORD - bytes * BITS_PER_UNIT;
839
840 for (j = 0; j < args[i].n_aligned_regs; j++)
841 {
842 rtx reg = gen_reg_rtx (word_mode);
843 rtx word = operand_subword_force (args[i].value, j, BLKmode);
844 int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
845
846 args[i].aligned_regs[j] = reg;
847 word = extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
848 word_mode, word_mode);
849
850 /* There is no need to restrict this code to loading items
851 in TYPE_ALIGN sized hunks. The bitfield instructions can
852 load up entire word sized registers efficiently.
853
854 ??? This may not be needed anymore.
855 We use to emit a clobber here but that doesn't let later
856 passes optimize the instructions we emit. By storing 0 into
857 the register later passes know the first AND to zero out the
858 bitfield being set in the register is unnecessary. The store
859 of 0 will be deleted as will at least the first AND. */
860
861 emit_move_insn (reg, const0_rtx);
862
863 bytes -= bitsize / BITS_PER_UNIT;
864 store_bit_field (reg, bitsize, endian_correction, word_mode,
865 word);
866 }
867 }
868 }
869
870 /* Fill in ARGS_SIZE and ARGS array based on the parameters found in
871 ACTPARMS.
872
873 NUM_ACTUALS is the total number of parameters.
874
875 N_NAMED_ARGS is the total number of named arguments.
876
877 FNDECL is the tree code for the target of this call (if known)
878
879 ARGS_SO_FAR holds state needed by the target to know where to place
880 the next argument.
881
882 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
883 for arguments which are passed in registers.
884
885 OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
886 and may be modified by this routine.
887
888 OLD_PENDING_ADJ, MUST_PREALLOCATE and FLAGS are pointers to integer
889 flags which may may be modified by this routine.
890
891 MAY_TAILCALL is cleared if we encounter an invisible pass-by-reference
892 that requires allocation of stack space.
893
894 CALL_FROM_THUNK_P is true if this call is the jump from a thunk to
895 the thunked-to function. */
896
897 static void
898 initialize_argument_information (int num_actuals ATTRIBUTE_UNUSED,
899 struct arg_data *args,
900 struct args_size *args_size,
901 int n_named_args ATTRIBUTE_UNUSED,
902 tree actparms, tree fndecl,
903 CUMULATIVE_ARGS *args_so_far,
904 int reg_parm_stack_space,
905 rtx *old_stack_level, int *old_pending_adj,
906 int *must_preallocate, int *ecf_flags,
907 bool *may_tailcall, bool call_from_thunk_p)
908 {
909 /* 1 if scanning parms front to back, -1 if scanning back to front. */
910 int inc;
911
912 /* Count arg position in order args appear. */
913 int argpos;
914
915 int i;
916 tree p;
917
918 args_size->constant = 0;
919 args_size->var = 0;
920
921 /* In this loop, we consider args in the order they are written.
922 We fill up ARGS from the front or from the back if necessary
923 so that in any case the first arg to be pushed ends up at the front. */
924
925 if (PUSH_ARGS_REVERSED)
926 {
927 i = num_actuals - 1, inc = -1;
928 /* In this case, must reverse order of args
929 so that we compute and push the last arg first. */
930 }
931 else
932 {
933 i = 0, inc = 1;
934 }
935
936 /* I counts args in order (to be) pushed; ARGPOS counts in order written. */
937 for (p = actparms, argpos = 0; p; p = TREE_CHAIN (p), i += inc, argpos++)
938 {
939 tree type = TREE_TYPE (TREE_VALUE (p));
940 int unsignedp;
941 enum machine_mode mode;
942
943 args[i].tree_value = TREE_VALUE (p);
944
945 /* Replace erroneous argument with constant zero. */
946 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
947 args[i].tree_value = integer_zero_node, type = integer_type_node;
948
949 /* If TYPE is a transparent union, pass things the way we would
950 pass the first field of the union. We have already verified that
951 the modes are the same. */
952 if (TREE_CODE (type) == UNION_TYPE && TYPE_TRANSPARENT_UNION (type))
953 type = TREE_TYPE (TYPE_FIELDS (type));
954
955 /* Decide where to pass this arg.
956
957 args[i].reg is nonzero if all or part is passed in registers.
958
959 args[i].partial is nonzero if part but not all is passed in registers,
960 and the exact value says how many words are passed in registers.
961
962 args[i].pass_on_stack is nonzero if the argument must at least be
963 computed on the stack. It may then be loaded back into registers
964 if args[i].reg is nonzero.
965
966 These decisions are driven by the FUNCTION_... macros and must agree
967 with those made by function.c. */
968
969 /* See if this argument should be passed by invisible reference. */
970 if (pass_by_reference (args_so_far, TYPE_MODE (type),
971 type, argpos < n_named_args))
972 {
973 bool callee_copies;
974 tree base;
975
976 callee_copies
977 = FUNCTION_ARG_CALLEE_COPIES (*args_so_far, TYPE_MODE (type),
978 type, argpos < n_named_args);
979
980 /* If we're compiling a thunk, pass through invisible references
981 instead of making a copy. */
982 if (call_from_thunk_p
983 || (callee_copies
984 && !TREE_ADDRESSABLE (type)
985 && (base = get_base_address (args[i].tree_value))
986 && (!DECL_P (base) || MEM_P (DECL_RTL (base)))))
987 {
988 /* We can't use sibcalls if a callee-copied argument is
989 stored in the current function's frame. */
990 if (!call_from_thunk_p && DECL_P (base) && !TREE_STATIC (base))
991 *may_tailcall = false;
992
993 args[i].tree_value = build_fold_addr_expr (args[i].tree_value);
994 type = TREE_TYPE (args[i].tree_value);
995
996 *ecf_flags &= ~(ECF_CONST | ECF_LIBCALL_BLOCK);
997 }
998 else
999 {
1000 /* We make a copy of the object and pass the address to the
1001 function being called. */
1002 rtx copy;
1003
1004 if (!COMPLETE_TYPE_P (type)
1005 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
1006 || (flag_stack_check && ! STACK_CHECK_BUILTIN
1007 && (0 < compare_tree_int (TYPE_SIZE_UNIT (type),
1008 STACK_CHECK_MAX_VAR_SIZE))))
1009 {
1010 /* This is a variable-sized object. Make space on the stack
1011 for it. */
1012 rtx size_rtx = expr_size (TREE_VALUE (p));
1013
1014 if (*old_stack_level == 0)
1015 {
1016 emit_stack_save (SAVE_BLOCK, old_stack_level, NULL_RTX);
1017 *old_pending_adj = pending_stack_adjust;
1018 pending_stack_adjust = 0;
1019 }
1020
1021 copy = gen_rtx_MEM (BLKmode,
1022 allocate_dynamic_stack_space
1023 (size_rtx, NULL_RTX, TYPE_ALIGN (type)));
1024 set_mem_attributes (copy, type, 1);
1025 }
1026 else
1027 copy = assign_temp (type, 0, 1, 0);
1028
1029 store_expr (args[i].tree_value, copy, 0);
1030
1031 if (callee_copies)
1032 *ecf_flags &= ~(ECF_CONST | ECF_LIBCALL_BLOCK);
1033 else
1034 *ecf_flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
1035
1036 args[i].tree_value
1037 = build_fold_addr_expr (make_tree (type, copy));
1038 type = TREE_TYPE (args[i].tree_value);
1039 *may_tailcall = false;
1040 }
1041 }
1042
1043 mode = TYPE_MODE (type);
1044 unsignedp = TYPE_UNSIGNED (type);
1045
1046 if (targetm.calls.promote_function_args (fndecl ? TREE_TYPE (fndecl) : 0))
1047 mode = promote_mode (type, mode, &unsignedp, 1);
1048
1049 args[i].unsignedp = unsignedp;
1050 args[i].mode = mode;
1051
1052 args[i].reg = FUNCTION_ARG (*args_so_far, mode, type,
1053 argpos < n_named_args);
1054 #ifdef FUNCTION_INCOMING_ARG
1055 /* If this is a sibling call and the machine has register windows, the
1056 register window has to be unwinded before calling the routine, so
1057 arguments have to go into the incoming registers. */
1058 args[i].tail_call_reg = FUNCTION_INCOMING_ARG (*args_so_far, mode, type,
1059 argpos < n_named_args);
1060 #else
1061 args[i].tail_call_reg = args[i].reg;
1062 #endif
1063
1064 if (args[i].reg)
1065 args[i].partial
1066 = FUNCTION_ARG_PARTIAL_NREGS (*args_so_far, mode, type,
1067 argpos < n_named_args);
1068
1069 args[i].pass_on_stack = targetm.calls.must_pass_in_stack (mode, type);
1070
1071 /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
1072 it means that we are to pass this arg in the register(s) designated
1073 by the PARALLEL, but also to pass it in the stack. */
1074 if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
1075 && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
1076 args[i].pass_on_stack = 1;
1077
1078 /* If this is an addressable type, we must preallocate the stack
1079 since we must evaluate the object into its final location.
1080
1081 If this is to be passed in both registers and the stack, it is simpler
1082 to preallocate. */
1083 if (TREE_ADDRESSABLE (type)
1084 || (args[i].pass_on_stack && args[i].reg != 0))
1085 *must_preallocate = 1;
1086
1087 /* If this is an addressable type, we cannot pre-evaluate it. Thus,
1088 we cannot consider this function call constant. */
1089 if (TREE_ADDRESSABLE (type))
1090 *ecf_flags &= ~ECF_LIBCALL_BLOCK;
1091
1092 /* Compute the stack-size of this argument. */
1093 if (args[i].reg == 0 || args[i].partial != 0
1094 || reg_parm_stack_space > 0
1095 || args[i].pass_on_stack)
1096 locate_and_pad_parm (mode, type,
1097 #ifdef STACK_PARMS_IN_REG_PARM_AREA
1098 1,
1099 #else
1100 args[i].reg != 0,
1101 #endif
1102 args[i].pass_on_stack ? 0 : args[i].partial,
1103 fndecl, args_size, &args[i].locate);
1104 #ifdef BLOCK_REG_PADDING
1105 else
1106 /* The argument is passed entirely in registers. See at which
1107 end it should be padded. */
1108 args[i].locate.where_pad =
1109 BLOCK_REG_PADDING (mode, type,
1110 int_size_in_bytes (type) <= UNITS_PER_WORD);
1111 #endif
1112
1113 /* Update ARGS_SIZE, the total stack space for args so far. */
1114
1115 args_size->constant += args[i].locate.size.constant;
1116 if (args[i].locate.size.var)
1117 ADD_PARM_SIZE (*args_size, args[i].locate.size.var);
1118
1119 /* Increment ARGS_SO_FAR, which has info about which arg-registers
1120 have been used, etc. */
1121
1122 FUNCTION_ARG_ADVANCE (*args_so_far, TYPE_MODE (type), type,
1123 argpos < n_named_args);
1124 }
1125 }
1126
1127 /* Update ARGS_SIZE to contain the total size for the argument block.
1128 Return the original constant component of the argument block's size.
1129
1130 REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
1131 for arguments passed in registers. */
1132
1133 static int
1134 compute_argument_block_size (int reg_parm_stack_space,
1135 struct args_size *args_size,
1136 int preferred_stack_boundary ATTRIBUTE_UNUSED)
1137 {
1138 int unadjusted_args_size = args_size->constant;
1139
1140 /* For accumulate outgoing args mode we don't need to align, since the frame
1141 will be already aligned. Align to STACK_BOUNDARY in order to prevent
1142 backends from generating misaligned frame sizes. */
1143 if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
1144 preferred_stack_boundary = STACK_BOUNDARY;
1145
1146 /* Compute the actual size of the argument block required. The variable
1147 and constant sizes must be combined, the size may have to be rounded,
1148 and there may be a minimum required size. */
1149
1150 if (args_size->var)
1151 {
1152 args_size->var = ARGS_SIZE_TREE (*args_size);
1153 args_size->constant = 0;
1154
1155 preferred_stack_boundary /= BITS_PER_UNIT;
1156 if (preferred_stack_boundary > 1)
1157 {
1158 /* We don't handle this case yet. To handle it correctly we have
1159 to add the delta, round and subtract the delta.
1160 Currently no machine description requires this support. */
1161 if (stack_pointer_delta & (preferred_stack_boundary - 1))
1162 abort ();
1163 args_size->var = round_up (args_size->var, preferred_stack_boundary);
1164 }
1165
1166 if (reg_parm_stack_space > 0)
1167 {
1168 args_size->var
1169 = size_binop (MAX_EXPR, args_size->var,
1170 ssize_int (reg_parm_stack_space));
1171
1172 #ifndef OUTGOING_REG_PARM_STACK_SPACE
1173 /* The area corresponding to register parameters is not to count in
1174 the size of the block we need. So make the adjustment. */
1175 args_size->var
1176 = size_binop (MINUS_EXPR, args_size->var,
1177 ssize_int (reg_parm_stack_space));
1178 #endif
1179 }
1180 }
1181 else
1182 {
1183 preferred_stack_boundary /= BITS_PER_UNIT;
1184 if (preferred_stack_boundary < 1)
1185 preferred_stack_boundary = 1;
1186 args_size->constant = (((args_size->constant
1187 + stack_pointer_delta
1188 + preferred_stack_boundary - 1)
1189 / preferred_stack_boundary
1190 * preferred_stack_boundary)
1191 - stack_pointer_delta);
1192
1193 args_size->constant = MAX (args_size->constant,
1194 reg_parm_stack_space);
1195
1196 #ifndef OUTGOING_REG_PARM_STACK_SPACE
1197 args_size->constant -= reg_parm_stack_space;
1198 #endif
1199 }
1200 return unadjusted_args_size;
1201 }
1202
1203 /* Precompute parameters as needed for a function call.
1204
1205 FLAGS is mask of ECF_* constants.
1206
1207 NUM_ACTUALS is the number of arguments.
1208
1209 ARGS is an array containing information for each argument; this
1210 routine fills in the INITIAL_VALUE and VALUE fields for each
1211 precomputed argument. */
1212
1213 static void
1214 precompute_arguments (int flags, int num_actuals, struct arg_data *args)
1215 {
1216 int i;
1217
1218 /* If this is a libcall, then precompute all arguments so that we do not
1219 get extraneous instructions emitted as part of the libcall sequence. */
1220 if ((flags & ECF_LIBCALL_BLOCK) == 0)
1221 return;
1222
1223 for (i = 0; i < num_actuals; i++)
1224 {
1225 enum machine_mode mode;
1226
1227 /* If this is an addressable type, we cannot pre-evaluate it. */
1228 if (TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value)))
1229 abort ();
1230
1231 args[i].initial_value = args[i].value
1232 = expand_expr (args[i].tree_value, NULL_RTX, VOIDmode, 0);
1233
1234 mode = TYPE_MODE (TREE_TYPE (args[i].tree_value));
1235 if (mode != args[i].mode)
1236 {
1237 args[i].value
1238 = convert_modes (args[i].mode, mode,
1239 args[i].value, args[i].unsignedp);
1240 #if defined(PROMOTE_FUNCTION_MODE) && !defined(PROMOTE_MODE)
1241 /* CSE will replace this only if it contains args[i].value
1242 pseudo, so convert it down to the declared mode using
1243 a SUBREG. */
1244 if (REG_P (args[i].value)
1245 && GET_MODE_CLASS (args[i].mode) == MODE_INT)
1246 {
1247 args[i].initial_value
1248 = gen_lowpart_SUBREG (mode, args[i].value);
1249 SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1250 SUBREG_PROMOTED_UNSIGNED_SET (args[i].initial_value,
1251 args[i].unsignedp);
1252 }
1253 #endif
1254 }
1255 }
1256 }
1257
1258 /* Given the current state of MUST_PREALLOCATE and information about
1259 arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
1260 compute and return the final value for MUST_PREALLOCATE. */
1261
1262 static int
1263 finalize_must_preallocate (int must_preallocate, int num_actuals, struct arg_data *args, struct args_size *args_size)
1264 {
1265 /* See if we have or want to preallocate stack space.
1266
1267 If we would have to push a partially-in-regs parm
1268 before other stack parms, preallocate stack space instead.
1269
1270 If the size of some parm is not a multiple of the required stack
1271 alignment, we must preallocate.
1272
1273 If the total size of arguments that would otherwise create a copy in
1274 a temporary (such as a CALL) is more than half the total argument list
1275 size, preallocation is faster.
1276
1277 Another reason to preallocate is if we have a machine (like the m88k)
1278 where stack alignment is required to be maintained between every
1279 pair of insns, not just when the call is made. However, we assume here
1280 that such machines either do not have push insns (and hence preallocation
1281 would occur anyway) or the problem is taken care of with
1282 PUSH_ROUNDING. */
1283
1284 if (! must_preallocate)
1285 {
1286 int partial_seen = 0;
1287 int copy_to_evaluate_size = 0;
1288 int i;
1289
1290 for (i = 0; i < num_actuals && ! must_preallocate; i++)
1291 {
1292 if (args[i].partial > 0 && ! args[i].pass_on_stack)
1293 partial_seen = 1;
1294 else if (partial_seen && args[i].reg == 0)
1295 must_preallocate = 1;
1296
1297 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1298 && (TREE_CODE (args[i].tree_value) == CALL_EXPR
1299 || TREE_CODE (args[i].tree_value) == TARGET_EXPR
1300 || TREE_CODE (args[i].tree_value) == COND_EXPR
1301 || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
1302 copy_to_evaluate_size
1303 += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1304 }
1305
1306 if (copy_to_evaluate_size * 2 >= args_size->constant
1307 && args_size->constant > 0)
1308 must_preallocate = 1;
1309 }
1310 return must_preallocate;
1311 }
1312
1313 /* If we preallocated stack space, compute the address of each argument
1314 and store it into the ARGS array.
1315
1316 We need not ensure it is a valid memory address here; it will be
1317 validized when it is used.
1318
1319 ARGBLOCK is an rtx for the address of the outgoing arguments. */
1320
1321 static void
1322 compute_argument_addresses (struct arg_data *args, rtx argblock, int num_actuals)
1323 {
1324 if (argblock)
1325 {
1326 rtx arg_reg = argblock;
1327 int i, arg_offset = 0;
1328
1329 if (GET_CODE (argblock) == PLUS)
1330 arg_reg = XEXP (argblock, 0), arg_offset = INTVAL (XEXP (argblock, 1));
1331
1332 for (i = 0; i < num_actuals; i++)
1333 {
1334 rtx offset = ARGS_SIZE_RTX (args[i].locate.offset);
1335 rtx slot_offset = ARGS_SIZE_RTX (args[i].locate.slot_offset);
1336 rtx addr;
1337
1338 /* Skip this parm if it will not be passed on the stack. */
1339 if (! args[i].pass_on_stack && args[i].reg != 0)
1340 continue;
1341
1342 if (GET_CODE (offset) == CONST_INT)
1343 addr = plus_constant (arg_reg, INTVAL (offset));
1344 else
1345 addr = gen_rtx_PLUS (Pmode, arg_reg, offset);
1346
1347 addr = plus_constant (addr, arg_offset);
1348 args[i].stack = gen_rtx_MEM (args[i].mode, addr);
1349 set_mem_align (args[i].stack, PARM_BOUNDARY);
1350 set_mem_attributes (args[i].stack,
1351 TREE_TYPE (args[i].tree_value), 1);
1352
1353 if (GET_CODE (slot_offset) == CONST_INT)
1354 addr = plus_constant (arg_reg, INTVAL (slot_offset));
1355 else
1356 addr = gen_rtx_PLUS (Pmode, arg_reg, slot_offset);
1357
1358 addr = plus_constant (addr, arg_offset);
1359 args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
1360 set_mem_align (args[i].stack_slot, PARM_BOUNDARY);
1361 set_mem_attributes (args[i].stack_slot,
1362 TREE_TYPE (args[i].tree_value), 1);
1363
1364 /* Function incoming arguments may overlap with sibling call
1365 outgoing arguments and we cannot allow reordering of reads
1366 from function arguments with stores to outgoing arguments
1367 of sibling calls. */
1368 set_mem_alias_set (args[i].stack, 0);
1369 set_mem_alias_set (args[i].stack_slot, 0);
1370 }
1371 }
1372 }
1373
1374 /* Given a FNDECL and EXP, return an rtx suitable for use as a target address
1375 in a call instruction.
1376
1377 FNDECL is the tree node for the target function. For an indirect call
1378 FNDECL will be NULL_TREE.
1379
1380 ADDR is the operand 0 of CALL_EXPR for this call. */
1381
1382 static rtx
1383 rtx_for_function_call (tree fndecl, tree addr)
1384 {
1385 rtx funexp;
1386
1387 /* Get the function to call, in the form of RTL. */
1388 if (fndecl)
1389 {
1390 /* If this is the first use of the function, see if we need to
1391 make an external definition for it. */
1392 if (! TREE_USED (fndecl))
1393 {
1394 assemble_external (fndecl);
1395 TREE_USED (fndecl) = 1;
1396 }
1397
1398 /* Get a SYMBOL_REF rtx for the function address. */
1399 funexp = XEXP (DECL_RTL (fndecl), 0);
1400 }
1401 else
1402 /* Generate an rtx (probably a pseudo-register) for the address. */
1403 {
1404 push_temp_slots ();
1405 funexp = expand_expr (addr, NULL_RTX, VOIDmode, 0);
1406 pop_temp_slots (); /* FUNEXP can't be BLKmode. */
1407 }
1408 return funexp;
1409 }
1410
1411 /* Do the register loads required for any wholly-register parms or any
1412 parms which are passed both on the stack and in a register. Their
1413 expressions were already evaluated.
1414
1415 Mark all register-parms as living through the call, putting these USE
1416 insns in the CALL_INSN_FUNCTION_USAGE field.
1417
1418 When IS_SIBCALL, perform the check_sibcall_overlap_argument_overlap
1419 checking, setting *SIBCALL_FAILURE if appropriate. */
1420
1421 static void
1422 load_register_parameters (struct arg_data *args, int num_actuals,
1423 rtx *call_fusage, int flags, int is_sibcall,
1424 int *sibcall_failure)
1425 {
1426 int i, j;
1427
1428 for (i = 0; i < num_actuals; i++)
1429 {
1430 rtx reg = ((flags & ECF_SIBCALL)
1431 ? args[i].tail_call_reg : args[i].reg);
1432 if (reg)
1433 {
1434 int partial = args[i].partial;
1435 int nregs;
1436 int size = 0;
1437 rtx before_arg = get_last_insn ();
1438 /* Set to non-negative if must move a word at a time, even if just
1439 one word (e.g, partial == 1 && mode == DFmode). Set to -1 if
1440 we just use a normal move insn. This value can be zero if the
1441 argument is a zero size structure with no fields. */
1442 nregs = -1;
1443 if (partial)
1444 nregs = partial;
1445 else if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode)
1446 {
1447 size = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1448 nregs = (size + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
1449 }
1450 else
1451 size = GET_MODE_SIZE (args[i].mode);
1452
1453 /* Handle calls that pass values in multiple non-contiguous
1454 locations. The Irix 6 ABI has examples of this. */
1455
1456 if (GET_CODE (reg) == PARALLEL)
1457 {
1458 tree type = TREE_TYPE (args[i].tree_value);
1459 emit_group_load (reg, args[i].value, type,
1460 int_size_in_bytes (type));
1461 }
1462
1463 /* If simple case, just do move. If normal partial, store_one_arg
1464 has already loaded the register for us. In all other cases,
1465 load the register(s) from memory. */
1466
1467 else if (nregs == -1)
1468 {
1469 emit_move_insn (reg, args[i].value);
1470 #ifdef BLOCK_REG_PADDING
1471 /* Handle case where we have a value that needs shifting
1472 up to the msb. eg. a QImode value and we're padding
1473 upward on a BYTES_BIG_ENDIAN machine. */
1474 if (size < UNITS_PER_WORD
1475 && (args[i].locate.where_pad
1476 == (BYTES_BIG_ENDIAN ? upward : downward)))
1477 {
1478 rtx x;
1479 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
1480
1481 /* Assigning REG here rather than a temp makes CALL_FUSAGE
1482 report the whole reg as used. Strictly speaking, the
1483 call only uses SIZE bytes at the msb end, but it doesn't
1484 seem worth generating rtl to say that. */
1485 reg = gen_rtx_REG (word_mode, REGNO (reg));
1486 x = expand_shift (LSHIFT_EXPR, word_mode, reg,
1487 build_int_cst (NULL_TREE, shift),
1488 reg, 1);
1489 if (x != reg)
1490 emit_move_insn (reg, x);
1491 }
1492 #endif
1493 }
1494
1495 /* If we have pre-computed the values to put in the registers in
1496 the case of non-aligned structures, copy them in now. */
1497
1498 else if (args[i].n_aligned_regs != 0)
1499 for (j = 0; j < args[i].n_aligned_regs; j++)
1500 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
1501 args[i].aligned_regs[j]);
1502
1503 else if (partial == 0 || args[i].pass_on_stack)
1504 {
1505 rtx mem = validize_mem (args[i].value);
1506
1507 /* Handle a BLKmode that needs shifting. */
1508 if (nregs == 1 && size < UNITS_PER_WORD
1509 #ifdef BLOCK_REG_PADDING
1510 && args[i].locate.where_pad == downward
1511 #else
1512 && BYTES_BIG_ENDIAN
1513 #endif
1514 )
1515 {
1516 rtx tem = operand_subword_force (mem, 0, args[i].mode);
1517 rtx ri = gen_rtx_REG (word_mode, REGNO (reg));
1518 rtx x = gen_reg_rtx (word_mode);
1519 int shift = (UNITS_PER_WORD - size) * BITS_PER_UNIT;
1520 enum tree_code dir = BYTES_BIG_ENDIAN ? RSHIFT_EXPR
1521 : LSHIFT_EXPR;
1522
1523 emit_move_insn (x, tem);
1524 x = expand_shift (dir, word_mode, x,
1525 build_int_cst (NULL_TREE, shift),
1526 ri, 1);
1527 if (x != ri)
1528 emit_move_insn (ri, x);
1529 }
1530 else
1531 move_block_to_reg (REGNO (reg), mem, nregs, args[i].mode);
1532 }
1533
1534 /* When a parameter is a block, and perhaps in other cases, it is
1535 possible that it did a load from an argument slot that was
1536 already clobbered. */
1537 if (is_sibcall
1538 && check_sibcall_argument_overlap (before_arg, &args[i], 0))
1539 *sibcall_failure = 1;
1540
1541 /* Handle calls that pass values in multiple non-contiguous
1542 locations. The Irix 6 ABI has examples of this. */
1543 if (GET_CODE (reg) == PARALLEL)
1544 use_group_regs (call_fusage, reg);
1545 else if (nregs == -1)
1546 use_reg (call_fusage, reg);
1547 else
1548 use_regs (call_fusage, REGNO (reg), nregs == 0 ? 1 : nregs);
1549 }
1550 }
1551 }
1552
1553 /* We need to pop PENDING_STACK_ADJUST bytes. But, if the arguments
1554 wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
1555 bytes, then we would need to push some additional bytes to pad the
1556 arguments. So, we compute an adjust to the stack pointer for an
1557 amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
1558 bytes. Then, when the arguments are pushed the stack will be perfectly
1559 aligned. ARGS_SIZE->CONSTANT is set to the number of bytes that should
1560 be popped after the call. Returns the adjustment. */
1561
1562 static int
1563 combine_pending_stack_adjustment_and_call (int unadjusted_args_size,
1564 struct args_size *args_size,
1565 unsigned int preferred_unit_stack_boundary)
1566 {
1567 /* The number of bytes to pop so that the stack will be
1568 under-aligned by UNADJUSTED_ARGS_SIZE bytes. */
1569 HOST_WIDE_INT adjustment;
1570 /* The alignment of the stack after the arguments are pushed, if we
1571 just pushed the arguments without adjust the stack here. */
1572 unsigned HOST_WIDE_INT unadjusted_alignment;
1573
1574 unadjusted_alignment
1575 = ((stack_pointer_delta + unadjusted_args_size)
1576 % preferred_unit_stack_boundary);
1577
1578 /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
1579 as possible -- leaving just enough left to cancel out the
1580 UNADJUSTED_ALIGNMENT. In other words, we want to ensure that the
1581 PENDING_STACK_ADJUST is non-negative, and congruent to
1582 -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY. */
1583
1584 /* Begin by trying to pop all the bytes. */
1585 unadjusted_alignment
1586 = (unadjusted_alignment
1587 - (pending_stack_adjust % preferred_unit_stack_boundary));
1588 adjustment = pending_stack_adjust;
1589 /* Push enough additional bytes that the stack will be aligned
1590 after the arguments are pushed. */
1591 if (preferred_unit_stack_boundary > 1)
1592 {
1593 if (unadjusted_alignment > 0)
1594 adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
1595 else
1596 adjustment += unadjusted_alignment;
1597 }
1598
1599 /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
1600 bytes after the call. The right number is the entire
1601 PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
1602 by the arguments in the first place. */
1603 args_size->constant
1604 = pending_stack_adjust - adjustment + unadjusted_args_size;
1605
1606 return adjustment;
1607 }
1608
1609 /* Scan X expression if it does not dereference any argument slots
1610 we already clobbered by tail call arguments (as noted in stored_args_map
1611 bitmap).
1612 Return nonzero if X expression dereferences such argument slots,
1613 zero otherwise. */
1614
1615 static int
1616 check_sibcall_argument_overlap_1 (rtx x)
1617 {
1618 RTX_CODE code;
1619 int i, j;
1620 unsigned int k;
1621 const char *fmt;
1622
1623 if (x == NULL_RTX)
1624 return 0;
1625
1626 code = GET_CODE (x);
1627
1628 if (code == MEM)
1629 {
1630 if (XEXP (x, 0) == current_function_internal_arg_pointer)
1631 i = 0;
1632 else if (GET_CODE (XEXP (x, 0)) == PLUS
1633 && XEXP (XEXP (x, 0), 0) ==
1634 current_function_internal_arg_pointer
1635 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
1636 i = INTVAL (XEXP (XEXP (x, 0), 1));
1637 else
1638 return 1;
1639
1640 #ifdef ARGS_GROW_DOWNWARD
1641 i = -i - GET_MODE_SIZE (GET_MODE (x));
1642 #endif
1643
1644 for (k = 0; k < GET_MODE_SIZE (GET_MODE (x)); k++)
1645 if (i + k < stored_args_map->n_bits
1646 && TEST_BIT (stored_args_map, i + k))
1647 return 1;
1648
1649 return 0;
1650 }
1651
1652 /* Scan all subexpressions. */
1653 fmt = GET_RTX_FORMAT (code);
1654 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
1655 {
1656 if (*fmt == 'e')
1657 {
1658 if (check_sibcall_argument_overlap_1 (XEXP (x, i)))
1659 return 1;
1660 }
1661 else if (*fmt == 'E')
1662 {
1663 for (j = 0; j < XVECLEN (x, i); j++)
1664 if (check_sibcall_argument_overlap_1 (XVECEXP (x, i, j)))
1665 return 1;
1666 }
1667 }
1668 return 0;
1669 }
1670
1671 /* Scan sequence after INSN if it does not dereference any argument slots
1672 we already clobbered by tail call arguments (as noted in stored_args_map
1673 bitmap). If MARK_STORED_ARGS_MAP, add stack slots for ARG to
1674 stored_args_map bitmap afterwards (when ARG is a register MARK_STORED_ARGS_MAP
1675 should be 0). Return nonzero if sequence after INSN dereferences such argument
1676 slots, zero otherwise. */
1677
1678 static int
1679 check_sibcall_argument_overlap (rtx insn, struct arg_data *arg, int mark_stored_args_map)
1680 {
1681 int low, high;
1682
1683 if (insn == NULL_RTX)
1684 insn = get_insns ();
1685 else
1686 insn = NEXT_INSN (insn);
1687
1688 for (; insn; insn = NEXT_INSN (insn))
1689 if (INSN_P (insn)
1690 && check_sibcall_argument_overlap_1 (PATTERN (insn)))
1691 break;
1692
1693 if (mark_stored_args_map)
1694 {
1695 #ifdef ARGS_GROW_DOWNWARD
1696 low = -arg->locate.slot_offset.constant - arg->locate.size.constant;
1697 #else
1698 low = arg->locate.slot_offset.constant;
1699 #endif
1700
1701 for (high = low + arg->locate.size.constant; low < high; low++)
1702 SET_BIT (stored_args_map, low);
1703 }
1704 return insn != NULL_RTX;
1705 }
1706
1707 /* If function value *VALUE was returned at the most significant end of a
1708 register, shift it towards the least significant end and convert it to
1709 TYPE's mode. Return true and update *VALUE if some action was needed.
1710
1711 TYPE is the type of the function's return value, which is known not
1712 to have mode BLKmode. */
1713
1714 static bool
1715 shift_returned_value (tree type, rtx *value)
1716 {
1717 if (targetm.calls.return_in_msb (type))
1718 {
1719 HOST_WIDE_INT shift;
1720
1721 shift = (GET_MODE_BITSIZE (GET_MODE (*value))
1722 - BITS_PER_UNIT * int_size_in_bytes (type));
1723 if (shift > 0)
1724 {
1725 /* Shift the value into the low part of the register. */
1726 *value = expand_binop (GET_MODE (*value), lshr_optab, *value,
1727 GEN_INT (shift), 0, 1, OPTAB_WIDEN);
1728
1729 /* Truncate it to the type's mode, or its integer equivalent.
1730 This is subject to TRULY_NOOP_TRUNCATION. */
1731 *value = convert_to_mode (int_mode_for_mode (TYPE_MODE (type)),
1732 *value, 0);
1733
1734 /* Now convert it to the final form. */
1735 *value = gen_lowpart (TYPE_MODE (type), *value);
1736 return true;
1737 }
1738 }
1739 return false;
1740 }
1741
1742 /* Remove all REG_EQUIV notes found in the insn chain. */
1743
1744 static void
1745 purge_reg_equiv_notes (void)
1746 {
1747 rtx insn;
1748
1749 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1750 {
1751 while (1)
1752 {
1753 rtx note = find_reg_note (insn, REG_EQUIV, 0);
1754 if (note)
1755 {
1756 /* Remove the note and keep looking at the notes for
1757 this insn. */
1758 remove_note (insn, note);
1759 continue;
1760 }
1761 break;
1762 }
1763 }
1764 }
1765
1766 /* Generate all the code for a function call
1767 and return an rtx for its value.
1768 Store the value in TARGET (specified as an rtx) if convenient.
1769 If the value is stored in TARGET then TARGET is returned.
1770 If IGNORE is nonzero, then we ignore the value of the function call. */
1771
1772 rtx
1773 expand_call (tree exp, rtx target, int ignore)
1774 {
1775 /* Nonzero if we are currently expanding a call. */
1776 static int currently_expanding_call = 0;
1777
1778 /* List of actual parameters. */
1779 tree actparms = TREE_OPERAND (exp, 1);
1780 /* RTX for the function to be called. */
1781 rtx funexp;
1782 /* Sequence of insns to perform a normal "call". */
1783 rtx normal_call_insns = NULL_RTX;
1784 /* Sequence of insns to perform a tail "call". */
1785 rtx tail_call_insns = NULL_RTX;
1786 /* Data type of the function. */
1787 tree funtype;
1788 tree type_arg_types;
1789 /* Declaration of the function being called,
1790 or 0 if the function is computed (not known by name). */
1791 tree fndecl = 0;
1792 /* The type of the function being called. */
1793 tree fntype;
1794 bool try_tail_call = CALL_EXPR_TAILCALL (exp);
1795 int pass;
1796
1797 /* Register in which non-BLKmode value will be returned,
1798 or 0 if no value or if value is BLKmode. */
1799 rtx valreg;
1800 /* Address where we should return a BLKmode value;
1801 0 if value not BLKmode. */
1802 rtx structure_value_addr = 0;
1803 /* Nonzero if that address is being passed by treating it as
1804 an extra, implicit first parameter. Otherwise,
1805 it is passed by being copied directly into struct_value_rtx. */
1806 int structure_value_addr_parm = 0;
1807 /* Size of aggregate value wanted, or zero if none wanted
1808 or if we are using the non-reentrant PCC calling convention
1809 or expecting the value in registers. */
1810 HOST_WIDE_INT struct_value_size = 0;
1811 /* Nonzero if called function returns an aggregate in memory PCC style,
1812 by returning the address of where to find it. */
1813 int pcc_struct_value = 0;
1814 rtx struct_value = 0;
1815
1816 /* Number of actual parameters in this call, including struct value addr. */
1817 int num_actuals;
1818 /* Number of named args. Args after this are anonymous ones
1819 and they must all go on the stack. */
1820 int n_named_args;
1821
1822 /* Vector of information about each argument.
1823 Arguments are numbered in the order they will be pushed,
1824 not the order they are written. */
1825 struct arg_data *args;
1826
1827 /* Total size in bytes of all the stack-parms scanned so far. */
1828 struct args_size args_size;
1829 struct args_size adjusted_args_size;
1830 /* Size of arguments before any adjustments (such as rounding). */
1831 int unadjusted_args_size;
1832 /* Data on reg parms scanned so far. */
1833 CUMULATIVE_ARGS args_so_far;
1834 /* Nonzero if a reg parm has been scanned. */
1835 int reg_parm_seen;
1836 /* Nonzero if this is an indirect function call. */
1837
1838 /* Nonzero if we must avoid push-insns in the args for this call.
1839 If stack space is allocated for register parameters, but not by the
1840 caller, then it is preallocated in the fixed part of the stack frame.
1841 So the entire argument block must then be preallocated (i.e., we
1842 ignore PUSH_ROUNDING in that case). */
1843
1844 int must_preallocate = !PUSH_ARGS;
1845
1846 /* Size of the stack reserved for parameter registers. */
1847 int reg_parm_stack_space = 0;
1848
1849 /* Address of space preallocated for stack parms
1850 (on machines that lack push insns), or 0 if space not preallocated. */
1851 rtx argblock = 0;
1852
1853 /* Mask of ECF_ flags. */
1854 int flags = 0;
1855 #ifdef REG_PARM_STACK_SPACE
1856 /* Define the boundary of the register parm stack space that needs to be
1857 saved, if any. */
1858 int low_to_save, high_to_save;
1859 rtx save_area = 0; /* Place that it is saved */
1860 #endif
1861
1862 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
1863 char *initial_stack_usage_map = stack_usage_map;
1864
1865 int old_stack_allocated;
1866
1867 /* State variables to track stack modifications. */
1868 rtx old_stack_level = 0;
1869 int old_stack_arg_under_construction = 0;
1870 int old_pending_adj = 0;
1871 int old_inhibit_defer_pop = inhibit_defer_pop;
1872
1873 /* Some stack pointer alterations we make are performed via
1874 allocate_dynamic_stack_space. This modifies the stack_pointer_delta,
1875 which we then also need to save/restore along the way. */
1876 int old_stack_pointer_delta = 0;
1877
1878 rtx call_fusage;
1879 tree p = TREE_OPERAND (exp, 0);
1880 tree addr = TREE_OPERAND (exp, 0);
1881 int i;
1882 /* The alignment of the stack, in bits. */
1883 unsigned HOST_WIDE_INT preferred_stack_boundary;
1884 /* The alignment of the stack, in bytes. */
1885 unsigned HOST_WIDE_INT preferred_unit_stack_boundary;
1886 /* The static chain value to use for this call. */
1887 rtx static_chain_value;
1888 /* See if this is "nothrow" function call. */
1889 if (TREE_NOTHROW (exp))
1890 flags |= ECF_NOTHROW;
1891
1892 /* See if we can find a DECL-node for the actual function, and get the
1893 function attributes (flags) from the function decl or type node. */
1894 fndecl = get_callee_fndecl (exp);
1895 if (fndecl)
1896 {
1897 fntype = TREE_TYPE (fndecl);
1898 flags |= flags_from_decl_or_type (fndecl);
1899 }
1900 else
1901 {
1902 fntype = TREE_TYPE (TREE_TYPE (p));
1903 flags |= flags_from_decl_or_type (fntype);
1904 }
1905
1906 struct_value = targetm.calls.struct_value_rtx (fntype, 0);
1907
1908 /* Warn if this value is an aggregate type,
1909 regardless of which calling convention we are using for it. */
1910 if (warn_aggregate_return && AGGREGATE_TYPE_P (TREE_TYPE (exp)))
1911 warning ("function call has aggregate value");
1912
1913 /* If the result of a pure or const function call is ignored (or void),
1914 and none of its arguments are volatile, we can avoid expanding the
1915 call and just evaluate the arguments for side-effects. */
1916 if ((flags & (ECF_CONST | ECF_PURE))
1917 && (ignore || target == const0_rtx
1918 || TYPE_MODE (TREE_TYPE (exp)) == VOIDmode))
1919 {
1920 bool volatilep = false;
1921 tree arg;
1922
1923 for (arg = actparms; arg; arg = TREE_CHAIN (arg))
1924 if (TREE_THIS_VOLATILE (TREE_VALUE (arg)))
1925 {
1926 volatilep = true;
1927 break;
1928 }
1929
1930 if (! volatilep)
1931 {
1932 for (arg = actparms; arg; arg = TREE_CHAIN (arg))
1933 expand_expr (TREE_VALUE (arg), const0_rtx,
1934 VOIDmode, EXPAND_NORMAL);
1935 return const0_rtx;
1936 }
1937 }
1938
1939 #ifdef REG_PARM_STACK_SPACE
1940 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
1941 #endif
1942
1943 #ifndef OUTGOING_REG_PARM_STACK_SPACE
1944 if (reg_parm_stack_space > 0 && PUSH_ARGS)
1945 must_preallocate = 1;
1946 #endif
1947
1948 /* Set up a place to return a structure. */
1949
1950 /* Cater to broken compilers. */
1951 if (aggregate_value_p (exp, fndecl))
1952 {
1953 /* This call returns a big structure. */
1954 flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
1955
1956 #ifdef PCC_STATIC_STRUCT_RETURN
1957 {
1958 pcc_struct_value = 1;
1959 }
1960 #else /* not PCC_STATIC_STRUCT_RETURN */
1961 {
1962 struct_value_size = int_size_in_bytes (TREE_TYPE (exp));
1963
1964 if (CALL_EXPR_HAS_RETURN_SLOT_ADDR (exp))
1965 {
1966 /* The structure value address arg is already in actparms.
1967 Pull it out. It might be nice to just leave it there, but
1968 we need to set structure_value_addr. */
1969 tree return_arg = TREE_VALUE (actparms);
1970 actparms = TREE_CHAIN (actparms);
1971 structure_value_addr = expand_expr (return_arg, NULL_RTX,
1972 VOIDmode, EXPAND_NORMAL);
1973 }
1974 else if (target && MEM_P (target))
1975 structure_value_addr = XEXP (target, 0);
1976 else
1977 {
1978 /* For variable-sized objects, we must be called with a target
1979 specified. If we were to allocate space on the stack here,
1980 we would have no way of knowing when to free it. */
1981 rtx d = assign_temp (TREE_TYPE (exp), 1, 1, 1);
1982
1983 mark_temp_addr_taken (d);
1984 structure_value_addr = XEXP (d, 0);
1985 target = 0;
1986 }
1987 }
1988 #endif /* not PCC_STATIC_STRUCT_RETURN */
1989 }
1990
1991 /* Figure out the amount to which the stack should be aligned. */
1992 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
1993 if (fndecl)
1994 {
1995 struct cgraph_rtl_info *i = cgraph_rtl_info (fndecl);
1996 if (i && i->preferred_incoming_stack_boundary)
1997 preferred_stack_boundary = i->preferred_incoming_stack_boundary;
1998 }
1999
2000 /* Operand 0 is a pointer-to-function; get the type of the function. */
2001 funtype = TREE_TYPE (addr);
2002 if (! POINTER_TYPE_P (funtype))
2003 abort ();
2004 funtype = TREE_TYPE (funtype);
2005
2006 /* Munge the tree to split complex arguments into their imaginary
2007 and real parts. */
2008 if (targetm.calls.split_complex_arg)
2009 {
2010 type_arg_types = split_complex_types (TYPE_ARG_TYPES (funtype));
2011 actparms = split_complex_values (actparms);
2012 }
2013 else
2014 type_arg_types = TYPE_ARG_TYPES (funtype);
2015
2016 if (flags & ECF_MAY_BE_ALLOCA)
2017 current_function_calls_alloca = 1;
2018
2019 /* If struct_value_rtx is 0, it means pass the address
2020 as if it were an extra parameter. */
2021 if (structure_value_addr && struct_value == 0)
2022 {
2023 /* If structure_value_addr is a REG other than
2024 virtual_outgoing_args_rtx, we can use always use it. If it
2025 is not a REG, we must always copy it into a register.
2026 If it is virtual_outgoing_args_rtx, we must copy it to another
2027 register in some cases. */
2028 rtx temp = (!REG_P (structure_value_addr)
2029 || (ACCUMULATE_OUTGOING_ARGS
2030 && stack_arg_under_construction
2031 && structure_value_addr == virtual_outgoing_args_rtx)
2032 ? copy_addr_to_reg (convert_memory_address
2033 (Pmode, structure_value_addr))
2034 : structure_value_addr);
2035
2036 actparms
2037 = tree_cons (error_mark_node,
2038 make_tree (build_pointer_type (TREE_TYPE (funtype)),
2039 temp),
2040 actparms);
2041 structure_value_addr_parm = 1;
2042 }
2043
2044 /* Count the arguments and set NUM_ACTUALS. */
2045 for (p = actparms, num_actuals = 0; p; p = TREE_CHAIN (p))
2046 num_actuals++;
2047
2048 /* Compute number of named args.
2049 First, do a raw count of the args for INIT_CUMULATIVE_ARGS. */
2050
2051 if (type_arg_types != 0)
2052 n_named_args
2053 = (list_length (type_arg_types)
2054 /* Count the struct value address, if it is passed as a parm. */
2055 + structure_value_addr_parm);
2056 else
2057 /* If we know nothing, treat all args as named. */
2058 n_named_args = num_actuals;
2059
2060 /* Start updating where the next arg would go.
2061
2062 On some machines (such as the PA) indirect calls have a different
2063 calling convention than normal calls. The fourth argument in
2064 INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
2065 or not. */
2066 INIT_CUMULATIVE_ARGS (args_so_far, funtype, NULL_RTX, fndecl, n_named_args);
2067
2068 /* Now possibly adjust the number of named args.
2069 Normally, don't include the last named arg if anonymous args follow.
2070 We do include the last named arg if
2071 targetm.calls.strict_argument_naming() returns nonzero.
2072 (If no anonymous args follow, the result of list_length is actually
2073 one too large. This is harmless.)
2074
2075 If targetm.calls.pretend_outgoing_varargs_named() returns
2076 nonzero, and targetm.calls.strict_argument_naming() returns zero,
2077 this machine will be able to place unnamed args that were passed
2078 in registers into the stack. So treat all args as named. This
2079 allows the insns emitting for a specific argument list to be
2080 independent of the function declaration.
2081
2082 If targetm.calls.pretend_outgoing_varargs_named() returns zero,
2083 we do not have any reliable way to pass unnamed args in
2084 registers, so we must force them into memory. */
2085
2086 if (type_arg_types != 0
2087 && targetm.calls.strict_argument_naming (&args_so_far))
2088 ;
2089 else if (type_arg_types != 0
2090 && ! targetm.calls.pretend_outgoing_varargs_named (&args_so_far))
2091 /* Don't include the last named arg. */
2092 --n_named_args;
2093 else
2094 /* Treat all args as named. */
2095 n_named_args = num_actuals;
2096
2097 /* Make a vector to hold all the information about each arg. */
2098 args = alloca (num_actuals * sizeof (struct arg_data));
2099 memset (args, 0, num_actuals * sizeof (struct arg_data));
2100
2101 /* Build up entries in the ARGS array, compute the size of the
2102 arguments into ARGS_SIZE, etc. */
2103 initialize_argument_information (num_actuals, args, &args_size,
2104 n_named_args, actparms, fndecl,
2105 &args_so_far, reg_parm_stack_space,
2106 &old_stack_level, &old_pending_adj,
2107 &must_preallocate, &flags,
2108 &try_tail_call, CALL_FROM_THUNK_P (exp));
2109
2110 if (args_size.var)
2111 {
2112 /* If this function requires a variable-sized argument list, don't
2113 try to make a cse'able block for this call. We may be able to
2114 do this eventually, but it is too complicated to keep track of
2115 what insns go in the cse'able block and which don't. */
2116
2117 flags &= ~ECF_LIBCALL_BLOCK;
2118 must_preallocate = 1;
2119 }
2120
2121 /* Now make final decision about preallocating stack space. */
2122 must_preallocate = finalize_must_preallocate (must_preallocate,
2123 num_actuals, args,
2124 &args_size);
2125
2126 /* If the structure value address will reference the stack pointer, we
2127 must stabilize it. We don't need to do this if we know that we are
2128 not going to adjust the stack pointer in processing this call. */
2129
2130 if (structure_value_addr
2131 && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
2132 || reg_mentioned_p (virtual_outgoing_args_rtx,
2133 structure_value_addr))
2134 && (args_size.var
2135 || (!ACCUMULATE_OUTGOING_ARGS && args_size.constant)))
2136 structure_value_addr = copy_to_reg (structure_value_addr);
2137
2138 /* Tail calls can make things harder to debug, and we've traditionally
2139 pushed these optimizations into -O2. Don't try if we're already
2140 expanding a call, as that means we're an argument. Don't try if
2141 there's cleanups, as we know there's code to follow the call. */
2142
2143 if (currently_expanding_call++ != 0
2144 || !flag_optimize_sibling_calls
2145 || args_size.var
2146 || lookup_stmt_eh_region (exp) >= 0)
2147 try_tail_call = 0;
2148
2149 /* Rest of purposes for tail call optimizations to fail. */
2150 if (
2151 #ifdef HAVE_sibcall_epilogue
2152 !HAVE_sibcall_epilogue
2153 #else
2154 1
2155 #endif
2156 || !try_tail_call
2157 /* Doing sibling call optimization needs some work, since
2158 structure_value_addr can be allocated on the stack.
2159 It does not seem worth the effort since few optimizable
2160 sibling calls will return a structure. */
2161 || structure_value_addr != NULL_RTX
2162 /* Check whether the target is able to optimize the call
2163 into a sibcall. */
2164 || !targetm.function_ok_for_sibcall (fndecl, exp)
2165 /* Functions that do not return exactly once may not be sibcall
2166 optimized. */
2167 || (flags & (ECF_RETURNS_TWICE | ECF_LONGJMP | ECF_NORETURN))
2168 || TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (addr)))
2169 /* If the called function is nested in the current one, it might access
2170 some of the caller's arguments, but could clobber them beforehand if
2171 the argument areas are shared. */
2172 || (fndecl && decl_function_context (fndecl) == current_function_decl)
2173 /* If this function requires more stack slots than the current
2174 function, we cannot change it into a sibling call. */
2175 || args_size.constant > current_function_args_size
2176 /* If the callee pops its own arguments, then it must pop exactly
2177 the same number of arguments as the current function. */
2178 || (RETURN_POPS_ARGS (fndecl, funtype, args_size.constant)
2179 != RETURN_POPS_ARGS (current_function_decl,
2180 TREE_TYPE (current_function_decl),
2181 current_function_args_size))
2182 || !lang_hooks.decls.ok_for_sibcall (fndecl))
2183 try_tail_call = 0;
2184
2185 /* Ensure current function's preferred stack boundary is at least
2186 what we need. We don't have to increase alignment for recursive
2187 functions. */
2188 if (cfun->preferred_stack_boundary < preferred_stack_boundary
2189 && fndecl != current_function_decl)
2190 cfun->preferred_stack_boundary = preferred_stack_boundary;
2191 if (fndecl == current_function_decl)
2192 cfun->recursive_call_emit = true;
2193
2194 preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
2195
2196 /* We want to make two insn chains; one for a sibling call, the other
2197 for a normal call. We will select one of the two chains after
2198 initial RTL generation is complete. */
2199 for (pass = try_tail_call ? 0 : 1; pass < 2; pass++)
2200 {
2201 int sibcall_failure = 0;
2202 /* We want to emit any pending stack adjustments before the tail
2203 recursion "call". That way we know any adjustment after the tail
2204 recursion call can be ignored if we indeed use the tail
2205 call expansion. */
2206 int save_pending_stack_adjust = 0;
2207 int save_stack_pointer_delta = 0;
2208 rtx insns;
2209 rtx before_call, next_arg_reg;
2210
2211 if (pass == 0)
2212 {
2213 /* State variables we need to save and restore between
2214 iterations. */
2215 save_pending_stack_adjust = pending_stack_adjust;
2216 save_stack_pointer_delta = stack_pointer_delta;
2217 }
2218 if (pass)
2219 flags &= ~ECF_SIBCALL;
2220 else
2221 flags |= ECF_SIBCALL;
2222
2223 /* Other state variables that we must reinitialize each time
2224 through the loop (that are not initialized by the loop itself). */
2225 argblock = 0;
2226 call_fusage = 0;
2227
2228 /* Start a new sequence for the normal call case.
2229
2230 From this point on, if the sibling call fails, we want to set
2231 sibcall_failure instead of continuing the loop. */
2232 start_sequence ();
2233
2234 /* Don't let pending stack adjusts add up to too much.
2235 Also, do all pending adjustments now if there is any chance
2236 this might be a call to alloca or if we are expanding a sibling
2237 call sequence or if we are calling a function that is to return
2238 with stack pointer depressed. */
2239 if (pending_stack_adjust >= 32
2240 || (pending_stack_adjust > 0
2241 && (flags & (ECF_MAY_BE_ALLOCA | ECF_SP_DEPRESSED)))
2242 || pass == 0)
2243 do_pending_stack_adjust ();
2244
2245 /* When calling a const function, we must pop the stack args right away,
2246 so that the pop is deleted or moved with the call. */
2247 if (pass && (flags & ECF_LIBCALL_BLOCK))
2248 NO_DEFER_POP;
2249
2250 /* Precompute any arguments as needed. */
2251 if (pass)
2252 precompute_arguments (flags, num_actuals, args);
2253
2254 /* Now we are about to start emitting insns that can be deleted
2255 if a libcall is deleted. */
2256 if (pass && (flags & (ECF_LIBCALL_BLOCK | ECF_MALLOC)))
2257 start_sequence ();
2258
2259 adjusted_args_size = args_size;
2260 /* Compute the actual size of the argument block required. The variable
2261 and constant sizes must be combined, the size may have to be rounded,
2262 and there may be a minimum required size. When generating a sibcall
2263 pattern, do not round up, since we'll be re-using whatever space our
2264 caller provided. */
2265 unadjusted_args_size
2266 = compute_argument_block_size (reg_parm_stack_space,
2267 &adjusted_args_size,
2268 (pass == 0 ? 0
2269 : preferred_stack_boundary));
2270
2271 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
2272
2273 /* The argument block when performing a sibling call is the
2274 incoming argument block. */
2275 if (pass == 0)
2276 {
2277 argblock = virtual_incoming_args_rtx;
2278 argblock
2279 #ifdef STACK_GROWS_DOWNWARD
2280 = plus_constant (argblock, current_function_pretend_args_size);
2281 #else
2282 = plus_constant (argblock, -current_function_pretend_args_size);
2283 #endif
2284 stored_args_map = sbitmap_alloc (args_size.constant);
2285 sbitmap_zero (stored_args_map);
2286 }
2287
2288 /* If we have no actual push instructions, or shouldn't use them,
2289 make space for all args right now. */
2290 else if (adjusted_args_size.var != 0)
2291 {
2292 if (old_stack_level == 0)
2293 {
2294 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
2295 old_stack_pointer_delta = stack_pointer_delta;
2296 old_pending_adj = pending_stack_adjust;
2297 pending_stack_adjust = 0;
2298 /* stack_arg_under_construction says whether a stack arg is
2299 being constructed at the old stack level. Pushing the stack
2300 gets a clean outgoing argument block. */
2301 old_stack_arg_under_construction = stack_arg_under_construction;
2302 stack_arg_under_construction = 0;
2303 }
2304 argblock = push_block (ARGS_SIZE_RTX (adjusted_args_size), 0, 0);
2305 }
2306 else
2307 {
2308 /* Note that we must go through the motions of allocating an argument
2309 block even if the size is zero because we may be storing args
2310 in the area reserved for register arguments, which may be part of
2311 the stack frame. */
2312
2313 int needed = adjusted_args_size.constant;
2314
2315 /* Store the maximum argument space used. It will be pushed by
2316 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
2317 checking). */
2318
2319 if (needed > current_function_outgoing_args_size)
2320 current_function_outgoing_args_size = needed;
2321
2322 if (must_preallocate)
2323 {
2324 if (ACCUMULATE_OUTGOING_ARGS)
2325 {
2326 /* Since the stack pointer will never be pushed, it is
2327 possible for the evaluation of a parm to clobber
2328 something we have already written to the stack.
2329 Since most function calls on RISC machines do not use
2330 the stack, this is uncommon, but must work correctly.
2331
2332 Therefore, we save any area of the stack that was already
2333 written and that we are using. Here we set up to do this
2334 by making a new stack usage map from the old one. The
2335 actual save will be done by store_one_arg.
2336
2337 Another approach might be to try to reorder the argument
2338 evaluations to avoid this conflicting stack usage. */
2339
2340 #ifndef OUTGOING_REG_PARM_STACK_SPACE
2341 /* Since we will be writing into the entire argument area,
2342 the map must be allocated for its entire size, not just
2343 the part that is the responsibility of the caller. */
2344 needed += reg_parm_stack_space;
2345 #endif
2346
2347 #ifdef ARGS_GROW_DOWNWARD
2348 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2349 needed + 1);
2350 #else
2351 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2352 needed);
2353 #endif
2354 stack_usage_map = alloca (highest_outgoing_arg_in_use);
2355
2356 if (initial_highest_arg_in_use)
2357 memcpy (stack_usage_map, initial_stack_usage_map,
2358 initial_highest_arg_in_use);
2359
2360 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
2361 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
2362 (highest_outgoing_arg_in_use
2363 - initial_highest_arg_in_use));
2364 needed = 0;
2365
2366 /* The address of the outgoing argument list must not be
2367 copied to a register here, because argblock would be left
2368 pointing to the wrong place after the call to
2369 allocate_dynamic_stack_space below. */
2370
2371 argblock = virtual_outgoing_args_rtx;
2372 }
2373 else
2374 {
2375 if (inhibit_defer_pop == 0)
2376 {
2377 /* Try to reuse some or all of the pending_stack_adjust
2378 to get this space. */
2379 needed
2380 = (combine_pending_stack_adjustment_and_call
2381 (unadjusted_args_size,
2382 &adjusted_args_size,
2383 preferred_unit_stack_boundary));
2384
2385 /* combine_pending_stack_adjustment_and_call computes
2386 an adjustment before the arguments are allocated.
2387 Account for them and see whether or not the stack
2388 needs to go up or down. */
2389 needed = unadjusted_args_size - needed;
2390
2391 if (needed < 0)
2392 {
2393 /* We're releasing stack space. */
2394 /* ??? We can avoid any adjustment at all if we're
2395 already aligned. FIXME. */
2396 pending_stack_adjust = -needed;
2397 do_pending_stack_adjust ();
2398 needed = 0;
2399 }
2400 else
2401 /* We need to allocate space. We'll do that in
2402 push_block below. */
2403 pending_stack_adjust = 0;
2404 }
2405
2406 /* Special case this because overhead of `push_block' in
2407 this case is non-trivial. */
2408 if (needed == 0)
2409 argblock = virtual_outgoing_args_rtx;
2410 else
2411 {
2412 argblock = push_block (GEN_INT (needed), 0, 0);
2413 #ifdef ARGS_GROW_DOWNWARD
2414 argblock = plus_constant (argblock, needed);
2415 #endif
2416 }
2417
2418 /* We only really need to call `copy_to_reg' in the case
2419 where push insns are going to be used to pass ARGBLOCK
2420 to a function call in ARGS. In that case, the stack
2421 pointer changes value from the allocation point to the
2422 call point, and hence the value of
2423 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
2424 as well always do it. */
2425 argblock = copy_to_reg (argblock);
2426 }
2427 }
2428 }
2429
2430 if (ACCUMULATE_OUTGOING_ARGS)
2431 {
2432 /* The save/restore code in store_one_arg handles all
2433 cases except one: a constructor call (including a C
2434 function returning a BLKmode struct) to initialize
2435 an argument. */
2436 if (stack_arg_under_construction)
2437 {
2438 #ifndef OUTGOING_REG_PARM_STACK_SPACE
2439 rtx push_size = GEN_INT (reg_parm_stack_space
2440 + adjusted_args_size.constant);
2441 #else
2442 rtx push_size = GEN_INT (adjusted_args_size.constant);
2443 #endif
2444 if (old_stack_level == 0)
2445 {
2446 emit_stack_save (SAVE_BLOCK, &old_stack_level,
2447 NULL_RTX);
2448 old_stack_pointer_delta = stack_pointer_delta;
2449 old_pending_adj = pending_stack_adjust;
2450 pending_stack_adjust = 0;
2451 /* stack_arg_under_construction says whether a stack
2452 arg is being constructed at the old stack level.
2453 Pushing the stack gets a clean outgoing argument
2454 block. */
2455 old_stack_arg_under_construction
2456 = stack_arg_under_construction;
2457 stack_arg_under_construction = 0;
2458 /* Make a new map for the new argument list. */
2459 stack_usage_map = alloca (highest_outgoing_arg_in_use);
2460 memset (stack_usage_map, 0, highest_outgoing_arg_in_use);
2461 highest_outgoing_arg_in_use = 0;
2462 }
2463 allocate_dynamic_stack_space (push_size, NULL_RTX,
2464 BITS_PER_UNIT);
2465 }
2466
2467 /* If argument evaluation might modify the stack pointer,
2468 copy the address of the argument list to a register. */
2469 for (i = 0; i < num_actuals; i++)
2470 if (args[i].pass_on_stack)
2471 {
2472 argblock = copy_addr_to_reg (argblock);
2473 break;
2474 }
2475 }
2476
2477 compute_argument_addresses (args, argblock, num_actuals);
2478
2479 /* If we push args individually in reverse order, perform stack alignment
2480 before the first push (the last arg). */
2481 if (PUSH_ARGS_REVERSED && argblock == 0
2482 && adjusted_args_size.constant != unadjusted_args_size)
2483 {
2484 /* When the stack adjustment is pending, we get better code
2485 by combining the adjustments. */
2486 if (pending_stack_adjust
2487 && ! (flags & ECF_LIBCALL_BLOCK)
2488 && ! inhibit_defer_pop)
2489 {
2490 pending_stack_adjust
2491 = (combine_pending_stack_adjustment_and_call
2492 (unadjusted_args_size,
2493 &adjusted_args_size,
2494 preferred_unit_stack_boundary));
2495 do_pending_stack_adjust ();
2496 }
2497 else if (argblock == 0)
2498 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
2499 - unadjusted_args_size));
2500 }
2501 /* Now that the stack is properly aligned, pops can't safely
2502 be deferred during the evaluation of the arguments. */
2503 NO_DEFER_POP;
2504
2505 funexp = rtx_for_function_call (fndecl, addr);
2506
2507 /* Figure out the register where the value, if any, will come back. */
2508 valreg = 0;
2509 if (TYPE_MODE (TREE_TYPE (exp)) != VOIDmode
2510 && ! structure_value_addr)
2511 {
2512 if (pcc_struct_value)
2513 valreg = hard_function_value (build_pointer_type (TREE_TYPE (exp)),
2514 fndecl, (pass == 0));
2515 else
2516 valreg = hard_function_value (TREE_TYPE (exp), fndecl, (pass == 0));
2517 }
2518
2519 /* Precompute all register parameters. It isn't safe to compute anything
2520 once we have started filling any specific hard regs. */
2521 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
2522
2523 if (TREE_OPERAND (exp, 2))
2524 static_chain_value = expand_expr (TREE_OPERAND (exp, 2),
2525 NULL_RTX, VOIDmode, 0);
2526 else
2527 static_chain_value = 0;
2528
2529 #ifdef REG_PARM_STACK_SPACE
2530 /* Save the fixed argument area if it's part of the caller's frame and
2531 is clobbered by argument setup for this call. */
2532 if (ACCUMULATE_OUTGOING_ARGS && pass)
2533 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
2534 &low_to_save, &high_to_save);
2535 #endif
2536
2537 /* Now store (and compute if necessary) all non-register parms.
2538 These come before register parms, since they can require block-moves,
2539 which could clobber the registers used for register parms.
2540 Parms which have partial registers are not stored here,
2541 but we do preallocate space here if they want that. */
2542
2543 for (i = 0; i < num_actuals; i++)
2544 if (args[i].reg == 0 || args[i].pass_on_stack)
2545 {
2546 rtx before_arg = get_last_insn ();
2547
2548 if (store_one_arg (&args[i], argblock, flags,
2549 adjusted_args_size.var != 0,
2550 reg_parm_stack_space)
2551 || (pass == 0
2552 && check_sibcall_argument_overlap (before_arg,
2553 &args[i], 1)))
2554 sibcall_failure = 1;
2555
2556 if (flags & ECF_CONST
2557 && args[i].stack
2558 && args[i].value == args[i].stack)
2559 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
2560 gen_rtx_USE (VOIDmode,
2561 args[i].value),
2562 call_fusage);
2563 }
2564
2565 /* If we have a parm that is passed in registers but not in memory
2566 and whose alignment does not permit a direct copy into registers,
2567 make a group of pseudos that correspond to each register that we
2568 will later fill. */
2569 if (STRICT_ALIGNMENT)
2570 store_unaligned_arguments_into_pseudos (args, num_actuals);
2571
2572 /* Now store any partially-in-registers parm.
2573 This is the last place a block-move can happen. */
2574 if (reg_parm_seen)
2575 for (i = 0; i < num_actuals; i++)
2576 if (args[i].partial != 0 && ! args[i].pass_on_stack)
2577 {
2578 rtx before_arg = get_last_insn ();
2579
2580 if (store_one_arg (&args[i], argblock, flags,
2581 adjusted_args_size.var != 0,
2582 reg_parm_stack_space)
2583 || (pass == 0
2584 && check_sibcall_argument_overlap (before_arg,
2585 &args[i], 1)))
2586 sibcall_failure = 1;
2587 }
2588
2589 /* If we pushed args in forward order, perform stack alignment
2590 after pushing the last arg. */
2591 if (!PUSH_ARGS_REVERSED && argblock == 0)
2592 anti_adjust_stack (GEN_INT (adjusted_args_size.constant
2593 - unadjusted_args_size));
2594
2595 /* If register arguments require space on the stack and stack space
2596 was not preallocated, allocate stack space here for arguments
2597 passed in registers. */
2598 #ifdef OUTGOING_REG_PARM_STACK_SPACE
2599 if (!ACCUMULATE_OUTGOING_ARGS
2600 && must_preallocate == 0 && reg_parm_stack_space > 0)
2601 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
2602 #endif
2603
2604 /* Pass the function the address in which to return a
2605 structure value. */
2606 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
2607 {
2608 structure_value_addr
2609 = convert_memory_address (Pmode, structure_value_addr);
2610 emit_move_insn (struct_value,
2611 force_reg (Pmode,
2612 force_operand (structure_value_addr,
2613 NULL_RTX)));
2614
2615 if (REG_P (struct_value))
2616 use_reg (&call_fusage, struct_value);
2617 }
2618
2619 funexp = prepare_call_address (funexp, static_chain_value,
2620 &call_fusage, reg_parm_seen, pass == 0);
2621
2622 load_register_parameters (args, num_actuals, &call_fusage, flags,
2623 pass == 0, &sibcall_failure);
2624
2625 /* Save a pointer to the last insn before the call, so that we can
2626 later safely search backwards to find the CALL_INSN. */
2627 before_call = get_last_insn ();
2628
2629 /* Set up next argument register. For sibling calls on machines
2630 with register windows this should be the incoming register. */
2631 #ifdef FUNCTION_INCOMING_ARG
2632 if (pass == 0)
2633 next_arg_reg = FUNCTION_INCOMING_ARG (args_so_far, VOIDmode,
2634 void_type_node, 1);
2635 else
2636 #endif
2637 next_arg_reg = FUNCTION_ARG (args_so_far, VOIDmode,
2638 void_type_node, 1);
2639
2640 /* All arguments and registers used for the call must be set up by
2641 now! */
2642
2643 /* Stack must be properly aligned now. */
2644 if (pass && stack_pointer_delta % preferred_unit_stack_boundary)
2645 abort ();
2646
2647 /* Generate the actual call instruction. */
2648 emit_call_1 (funexp, exp, fndecl, funtype, unadjusted_args_size,
2649 adjusted_args_size.constant, struct_value_size,
2650 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
2651 flags, & args_so_far);
2652
2653 /* If call is cse'able, make appropriate pair of reg-notes around it.
2654 Test valreg so we don't crash; may safely ignore `const'
2655 if return type is void. Disable for PARALLEL return values, because
2656 we have no way to move such values into a pseudo register. */
2657 if (pass && (flags & ECF_LIBCALL_BLOCK))
2658 {
2659 rtx insns;
2660 rtx insn;
2661 bool failed = valreg == 0 || GET_CODE (valreg) == PARALLEL;
2662
2663 insns = get_insns ();
2664
2665 /* Expansion of block moves possibly introduced a loop that may
2666 not appear inside libcall block. */
2667 for (insn = insns; insn; insn = NEXT_INSN (insn))
2668 if (JUMP_P (insn))
2669 failed = true;
2670
2671 if (failed)
2672 {
2673 end_sequence ();
2674 emit_insn (insns);
2675 }
2676 else
2677 {
2678 rtx note = 0;
2679 rtx temp = gen_reg_rtx (GET_MODE (valreg));
2680
2681 /* Mark the return value as a pointer if needed. */
2682 if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
2683 mark_reg_pointer (temp,
2684 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp))));
2685
2686 end_sequence ();
2687 if (flag_unsafe_math_optimizations
2688 && fndecl
2689 && DECL_BUILT_IN (fndecl)
2690 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRT
2691 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRTF
2692 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_SQRTL))
2693 note = gen_rtx_fmt_e (SQRT,
2694 GET_MODE (temp),
2695 args[0].initial_value);
2696 else
2697 {
2698 /* Construct an "equal form" for the value which
2699 mentions all the arguments in order as well as
2700 the function name. */
2701 for (i = 0; i < num_actuals; i++)
2702 note = gen_rtx_EXPR_LIST (VOIDmode,
2703 args[i].initial_value, note);
2704 note = gen_rtx_EXPR_LIST (VOIDmode, funexp, note);
2705
2706 if (flags & ECF_PURE)
2707 note = gen_rtx_EXPR_LIST (VOIDmode,
2708 gen_rtx_USE (VOIDmode,
2709 gen_rtx_MEM (BLKmode,
2710 gen_rtx_SCRATCH (VOIDmode))),
2711 note);
2712 }
2713 emit_libcall_block (insns, temp, valreg, note);
2714
2715 valreg = temp;
2716 }
2717 }
2718 else if (pass && (flags & ECF_MALLOC))
2719 {
2720 rtx temp = gen_reg_rtx (GET_MODE (valreg));
2721 rtx last, insns;
2722
2723 /* The return value from a malloc-like function is a pointer. */
2724 if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
2725 mark_reg_pointer (temp, BIGGEST_ALIGNMENT);
2726
2727 emit_move_insn (temp, valreg);
2728
2729 /* The return value from a malloc-like function can not alias
2730 anything else. */
2731 last = get_last_insn ();
2732 REG_NOTES (last) =
2733 gen_rtx_EXPR_LIST (REG_NOALIAS, temp, REG_NOTES (last));
2734
2735 /* Write out the sequence. */
2736 insns = get_insns ();
2737 end_sequence ();
2738 emit_insn (insns);
2739 valreg = temp;
2740 }
2741
2742 /* For calls to `setjmp', etc., inform flow.c it should complain
2743 if nonvolatile values are live. For functions that cannot return,
2744 inform flow that control does not fall through. */
2745
2746 if ((flags & (ECF_NORETURN | ECF_LONGJMP)) || pass == 0)
2747 {
2748 /* The barrier must be emitted
2749 immediately after the CALL_INSN. Some ports emit more
2750 than just a CALL_INSN above, so we must search for it here. */
2751
2752 rtx last = get_last_insn ();
2753 while (!CALL_P (last))
2754 {
2755 last = PREV_INSN (last);
2756 /* There was no CALL_INSN? */
2757 if (last == before_call)
2758 abort ();
2759 }
2760
2761 emit_barrier_after (last);
2762
2763 /* Stack adjustments after a noreturn call are dead code.
2764 However when NO_DEFER_POP is in effect, we must preserve
2765 stack_pointer_delta. */
2766 if (inhibit_defer_pop == 0)
2767 {
2768 stack_pointer_delta = old_stack_allocated;
2769 pending_stack_adjust = 0;
2770 }
2771 }
2772
2773 if (flags & ECF_LONGJMP)
2774 current_function_calls_longjmp = 1;
2775
2776 /* If value type not void, return an rtx for the value. */
2777
2778 if (TYPE_MODE (TREE_TYPE (exp)) == VOIDmode
2779 || ignore)
2780 target = const0_rtx;
2781 else if (structure_value_addr)
2782 {
2783 if (target == 0 || !MEM_P (target))
2784 {
2785 target
2786 = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
2787 memory_address (TYPE_MODE (TREE_TYPE (exp)),
2788 structure_value_addr));
2789 set_mem_attributes (target, exp, 1);
2790 }
2791 }
2792 else if (pcc_struct_value)
2793 {
2794 /* This is the special C++ case where we need to
2795 know what the true target was. We take care to
2796 never use this value more than once in one expression. */
2797 target = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
2798 copy_to_reg (valreg));
2799 set_mem_attributes (target, exp, 1);
2800 }
2801 /* Handle calls that return values in multiple non-contiguous locations.
2802 The Irix 6 ABI has examples of this. */
2803 else if (GET_CODE (valreg) == PARALLEL)
2804 {
2805 if (target == 0)
2806 {
2807 /* This will only be assigned once, so it can be readonly. */
2808 tree nt = build_qualified_type (TREE_TYPE (exp),
2809 (TYPE_QUALS (TREE_TYPE (exp))
2810 | TYPE_QUAL_CONST));
2811
2812 target = assign_temp (nt, 0, 1, 1);
2813 preserve_temp_slots (target);
2814 }
2815
2816 if (! rtx_equal_p (target, valreg))
2817 emit_group_store (target, valreg, TREE_TYPE (exp),
2818 int_size_in_bytes (TREE_TYPE (exp)));
2819
2820 /* We can not support sibling calls for this case. */
2821 sibcall_failure = 1;
2822 }
2823 else if (target
2824 && GET_MODE (target) == TYPE_MODE (TREE_TYPE (exp))
2825 && GET_MODE (target) == GET_MODE (valreg))
2826 {
2827 /* TARGET and VALREG cannot be equal at this point because the
2828 latter would not have REG_FUNCTION_VALUE_P true, while the
2829 former would if it were referring to the same register.
2830
2831 If they refer to the same register, this move will be a no-op,
2832 except when function inlining is being done. */
2833 emit_move_insn (target, valreg);
2834
2835 /* If we are setting a MEM, this code must be executed. Since it is
2836 emitted after the call insn, sibcall optimization cannot be
2837 performed in that case. */
2838 if (MEM_P (target))
2839 sibcall_failure = 1;
2840 }
2841 else if (TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
2842 {
2843 target = copy_blkmode_from_reg (target, valreg, TREE_TYPE (exp));
2844
2845 /* We can not support sibling calls for this case. */
2846 sibcall_failure = 1;
2847 }
2848 else
2849 {
2850 if (shift_returned_value (TREE_TYPE (exp), &valreg))
2851 sibcall_failure = 1;
2852
2853 target = copy_to_reg (valreg);
2854 }
2855
2856 if (targetm.calls.promote_function_return(funtype))
2857 {
2858 /* If we promoted this return value, make the proper SUBREG. TARGET
2859 might be const0_rtx here, so be careful. */
2860 if (REG_P (target)
2861 && TYPE_MODE (TREE_TYPE (exp)) != BLKmode
2862 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
2863 {
2864 tree type = TREE_TYPE (exp);
2865 int unsignedp = TYPE_UNSIGNED (type);
2866 int offset = 0;
2867
2868 /* If we don't promote as expected, something is wrong. */
2869 if (GET_MODE (target)
2870 != promote_mode (type, TYPE_MODE (type), &unsignedp, 1))
2871 abort ();
2872
2873 if ((WORDS_BIG_ENDIAN || BYTES_BIG_ENDIAN)
2874 && GET_MODE_SIZE (GET_MODE (target))
2875 > GET_MODE_SIZE (TYPE_MODE (type)))
2876 {
2877 offset = GET_MODE_SIZE (GET_MODE (target))
2878 - GET_MODE_SIZE (TYPE_MODE (type));
2879 if (! BYTES_BIG_ENDIAN)
2880 offset = (offset / UNITS_PER_WORD) * UNITS_PER_WORD;
2881 else if (! WORDS_BIG_ENDIAN)
2882 offset %= UNITS_PER_WORD;
2883 }
2884 target = gen_rtx_SUBREG (TYPE_MODE (type), target, offset);
2885 SUBREG_PROMOTED_VAR_P (target) = 1;
2886 SUBREG_PROMOTED_UNSIGNED_SET (target, unsignedp);
2887 }
2888 }
2889
2890 /* If size of args is variable or this was a constructor call for a stack
2891 argument, restore saved stack-pointer value. */
2892
2893 if (old_stack_level && ! (flags & ECF_SP_DEPRESSED))
2894 {
2895 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
2896 stack_pointer_delta = old_stack_pointer_delta;
2897 pending_stack_adjust = old_pending_adj;
2898 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
2899 stack_arg_under_construction = old_stack_arg_under_construction;
2900 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
2901 stack_usage_map = initial_stack_usage_map;
2902 sibcall_failure = 1;
2903 }
2904 else if (ACCUMULATE_OUTGOING_ARGS && pass)
2905 {
2906 #ifdef REG_PARM_STACK_SPACE
2907 if (save_area)
2908 restore_fixed_argument_area (save_area, argblock,
2909 high_to_save, low_to_save);
2910 #endif
2911
2912 /* If we saved any argument areas, restore them. */
2913 for (i = 0; i < num_actuals; i++)
2914 if (args[i].save_area)
2915 {
2916 enum machine_mode save_mode = GET_MODE (args[i].save_area);
2917 rtx stack_area
2918 = gen_rtx_MEM (save_mode,
2919 memory_address (save_mode,
2920 XEXP (args[i].stack_slot, 0)));
2921
2922 if (save_mode != BLKmode)
2923 emit_move_insn (stack_area, args[i].save_area);
2924 else
2925 emit_block_move (stack_area, args[i].save_area,
2926 GEN_INT (args[i].locate.size.constant),
2927 BLOCK_OP_CALL_PARM);
2928 }
2929
2930 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
2931 stack_usage_map = initial_stack_usage_map;
2932 }
2933
2934 /* If this was alloca, record the new stack level for nonlocal gotos.
2935 Check for the handler slots since we might not have a save area
2936 for non-local gotos. */
2937
2938 if ((flags & ECF_MAY_BE_ALLOCA) && cfun->nonlocal_goto_save_area != 0)
2939 update_nonlocal_goto_save_area ();
2940
2941 /* Free up storage we no longer need. */
2942 for (i = 0; i < num_actuals; ++i)
2943 if (args[i].aligned_regs)
2944 free (args[i].aligned_regs);
2945
2946 insns = get_insns ();
2947 end_sequence ();
2948
2949 if (pass == 0)
2950 {
2951 tail_call_insns = insns;
2952
2953 /* Restore the pending stack adjustment now that we have
2954 finished generating the sibling call sequence. */
2955
2956 pending_stack_adjust = save_pending_stack_adjust;
2957 stack_pointer_delta = save_stack_pointer_delta;
2958
2959 /* Prepare arg structure for next iteration. */
2960 for (i = 0; i < num_actuals; i++)
2961 {
2962 args[i].value = 0;
2963 args[i].aligned_regs = 0;
2964 args[i].stack = 0;
2965 }
2966
2967 sbitmap_free (stored_args_map);
2968 }
2969 else
2970 {
2971 normal_call_insns = insns;
2972
2973 /* Verify that we've deallocated all the stack we used. */
2974 if (! (flags & (ECF_NORETURN | ECF_LONGJMP))
2975 && old_stack_allocated != stack_pointer_delta
2976 - pending_stack_adjust)
2977 abort ();
2978 }
2979
2980 /* If something prevents making this a sibling call,
2981 zero out the sequence. */
2982 if (sibcall_failure)
2983 tail_call_insns = NULL_RTX;
2984 else
2985 break;
2986 }
2987
2988 /* If tail call production succeeded, we need to remove REG_EQUIV notes on
2989 arguments too, as argument area is now clobbered by the call. */
2990 if (tail_call_insns)
2991 {
2992 emit_insn (tail_call_insns);
2993 cfun->tail_call_emit = true;
2994 }
2995 else
2996 emit_insn (normal_call_insns);
2997
2998 currently_expanding_call--;
2999
3000 /* If this function returns with the stack pointer depressed, ensure
3001 this block saves and restores the stack pointer, show it was
3002 changed, and adjust for any outgoing arg space. */
3003 if (flags & ECF_SP_DEPRESSED)
3004 {
3005 clear_pending_stack_adjust ();
3006 emit_insn (gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx));
3007 emit_move_insn (virtual_stack_dynamic_rtx, stack_pointer_rtx);
3008 }
3009
3010 return target;
3011 }
3012
3013 /* A sibling call sequence invalidates any REG_EQUIV notes made for
3014 this function's incoming arguments.
3015
3016 At the start of RTL generation we know the only REG_EQUIV notes
3017 in the rtl chain are those for incoming arguments, so we can safely
3018 flush any REG_EQUIV note.
3019
3020 This is (slight) overkill. We could keep track of the highest
3021 argument we clobber and be more selective in removing notes, but it
3022 does not seem to be worth the effort. */
3023 void
3024 fixup_tail_calls (void)
3025 {
3026 purge_reg_equiv_notes ();
3027 }
3028
3029 /* Traverse an argument list in VALUES and expand all complex
3030 arguments into their components. */
3031 tree
3032 split_complex_values (tree values)
3033 {
3034 tree p;
3035
3036 /* Before allocating memory, check for the common case of no complex. */
3037 for (p = values; p; p = TREE_CHAIN (p))
3038 {
3039 tree type = TREE_TYPE (TREE_VALUE (p));
3040 if (type && TREE_CODE (type) == COMPLEX_TYPE
3041 && targetm.calls.split_complex_arg (type))
3042 goto found;
3043 }
3044 return values;
3045
3046 found:
3047 values = copy_list (values);
3048
3049 for (p = values; p; p = TREE_CHAIN (p))
3050 {
3051 tree complex_value = TREE_VALUE (p);
3052 tree complex_type;
3053
3054 complex_type = TREE_TYPE (complex_value);
3055 if (!complex_type)
3056 continue;
3057
3058 if (TREE_CODE (complex_type) == COMPLEX_TYPE
3059 && targetm.calls.split_complex_arg (complex_type))
3060 {
3061 tree subtype;
3062 tree real, imag, next;
3063
3064 subtype = TREE_TYPE (complex_type);
3065 complex_value = save_expr (complex_value);
3066 real = build1 (REALPART_EXPR, subtype, complex_value);
3067 imag = build1 (IMAGPART_EXPR, subtype, complex_value);
3068
3069 TREE_VALUE (p) = real;
3070 next = TREE_CHAIN (p);
3071 imag = build_tree_list (NULL_TREE, imag);
3072 TREE_CHAIN (p) = imag;
3073 TREE_CHAIN (imag) = next;
3074
3075 /* Skip the newly created node. */
3076 p = TREE_CHAIN (p);
3077 }
3078 }
3079
3080 return values;
3081 }
3082
3083 /* Traverse a list of TYPES and expand all complex types into their
3084 components. */
3085 tree
3086 split_complex_types (tree types)
3087 {
3088 tree p;
3089
3090 /* Before allocating memory, check for the common case of no complex. */
3091 for (p = types; p; p = TREE_CHAIN (p))
3092 {
3093 tree type = TREE_VALUE (p);
3094 if (TREE_CODE (type) == COMPLEX_TYPE
3095 && targetm.calls.split_complex_arg (type))
3096 goto found;
3097 }
3098 return types;
3099
3100 found:
3101 types = copy_list (types);
3102
3103 for (p = types; p; p = TREE_CHAIN (p))
3104 {
3105 tree complex_type = TREE_VALUE (p);
3106
3107 if (TREE_CODE (complex_type) == COMPLEX_TYPE
3108 && targetm.calls.split_complex_arg (complex_type))
3109 {
3110 tree next, imag;
3111
3112 /* Rewrite complex type with component type. */
3113 TREE_VALUE (p) = TREE_TYPE (complex_type);
3114 next = TREE_CHAIN (p);
3115
3116 /* Add another component type for the imaginary part. */
3117 imag = build_tree_list (NULL_TREE, TREE_VALUE (p));
3118 TREE_CHAIN (p) = imag;
3119 TREE_CHAIN (imag) = next;
3120
3121 /* Skip the newly created node. */
3122 p = TREE_CHAIN (p);
3123 }
3124 }
3125
3126 return types;
3127 }
3128 \f
3129 /* Output a library call to function FUN (a SYMBOL_REF rtx).
3130 The RETVAL parameter specifies whether return value needs to be saved, other
3131 parameters are documented in the emit_library_call function below. */
3132
3133 static rtx
3134 emit_library_call_value_1 (int retval, rtx orgfun, rtx value,
3135 enum libcall_type fn_type,
3136 enum machine_mode outmode, int nargs, va_list p)
3137 {
3138 /* Total size in bytes of all the stack-parms scanned so far. */
3139 struct args_size args_size;
3140 /* Size of arguments before any adjustments (such as rounding). */
3141 struct args_size original_args_size;
3142 int argnum;
3143 rtx fun;
3144 int inc;
3145 int count;
3146 rtx argblock = 0;
3147 CUMULATIVE_ARGS args_so_far;
3148 struct arg
3149 {
3150 rtx value;
3151 enum machine_mode mode;
3152 rtx reg;
3153 int partial;
3154 struct locate_and_pad_arg_data locate;
3155 rtx save_area;
3156 };
3157 struct arg *argvec;
3158 int old_inhibit_defer_pop = inhibit_defer_pop;
3159 rtx call_fusage = 0;
3160 rtx mem_value = 0;
3161 rtx valreg;
3162 int pcc_struct_value = 0;
3163 int struct_value_size = 0;
3164 int flags;
3165 int reg_parm_stack_space = 0;
3166 int needed;
3167 rtx before_call;
3168 tree tfom; /* type_for_mode (outmode, 0) */
3169
3170 #ifdef REG_PARM_STACK_SPACE
3171 /* Define the boundary of the register parm stack space that needs to be
3172 save, if any. */
3173 int low_to_save, high_to_save;
3174 rtx save_area = 0; /* Place that it is saved. */
3175 #endif
3176
3177 /* Size of the stack reserved for parameter registers. */
3178 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
3179 char *initial_stack_usage_map = stack_usage_map;
3180
3181 rtx struct_value = targetm.calls.struct_value_rtx (0, 0);
3182
3183 #ifdef REG_PARM_STACK_SPACE
3184 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
3185 #endif
3186
3187 /* By default, library functions can not throw. */
3188 flags = ECF_NOTHROW;
3189
3190 switch (fn_type)
3191 {
3192 case LCT_NORMAL:
3193 break;
3194 case LCT_CONST:
3195 flags |= ECF_CONST;
3196 break;
3197 case LCT_PURE:
3198 flags |= ECF_PURE;
3199 break;
3200 case LCT_CONST_MAKE_BLOCK:
3201 flags |= ECF_CONST | ECF_LIBCALL_BLOCK;
3202 break;
3203 case LCT_PURE_MAKE_BLOCK:
3204 flags |= ECF_PURE | ECF_LIBCALL_BLOCK;
3205 break;
3206 case LCT_NORETURN:
3207 flags |= ECF_NORETURN;
3208 break;
3209 case LCT_THROW:
3210 flags = ECF_NORETURN;
3211 break;
3212 case LCT_ALWAYS_RETURN:
3213 flags = ECF_ALWAYS_RETURN;
3214 break;
3215 case LCT_RETURNS_TWICE:
3216 flags = ECF_RETURNS_TWICE;
3217 break;
3218 }
3219 fun = orgfun;
3220
3221 /* Ensure current function's preferred stack boundary is at least
3222 what we need. */
3223 if (cfun->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
3224 cfun->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3225
3226 /* If this kind of value comes back in memory,
3227 decide where in memory it should come back. */
3228 if (outmode != VOIDmode)
3229 {
3230 tfom = lang_hooks.types.type_for_mode (outmode, 0);
3231 if (aggregate_value_p (tfom, 0))
3232 {
3233 #ifdef PCC_STATIC_STRUCT_RETURN
3234 rtx pointer_reg
3235 = hard_function_value (build_pointer_type (tfom), 0, 0);
3236 mem_value = gen_rtx_MEM (outmode, pointer_reg);
3237 pcc_struct_value = 1;
3238 if (value == 0)
3239 value = gen_reg_rtx (outmode);
3240 #else /* not PCC_STATIC_STRUCT_RETURN */
3241 struct_value_size = GET_MODE_SIZE (outmode);
3242 if (value != 0 && MEM_P (value))
3243 mem_value = value;
3244 else
3245 mem_value = assign_temp (tfom, 0, 1, 1);
3246 #endif
3247 /* This call returns a big structure. */
3248 flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
3249 }
3250 }
3251 else
3252 tfom = void_type_node;
3253
3254 /* ??? Unfinished: must pass the memory address as an argument. */
3255
3256 /* Copy all the libcall-arguments out of the varargs data
3257 and into a vector ARGVEC.
3258
3259 Compute how to pass each argument. We only support a very small subset
3260 of the full argument passing conventions to limit complexity here since
3261 library functions shouldn't have many args. */
3262
3263 argvec = alloca ((nargs + 1) * sizeof (struct arg));
3264 memset (argvec, 0, (nargs + 1) * sizeof (struct arg));
3265
3266 #ifdef INIT_CUMULATIVE_LIBCALL_ARGS
3267 INIT_CUMULATIVE_LIBCALL_ARGS (args_so_far, outmode, fun);
3268 #else
3269 INIT_CUMULATIVE_ARGS (args_so_far, NULL_TREE, fun, 0, nargs);
3270 #endif
3271
3272 args_size.constant = 0;
3273 args_size.var = 0;
3274
3275 count = 0;
3276
3277 /* Now we are about to start emitting insns that can be deleted
3278 if a libcall is deleted. */
3279 if (flags & ECF_LIBCALL_BLOCK)
3280 start_sequence ();
3281
3282 push_temp_slots ();
3283
3284 /* If there's a structure value address to be passed,
3285 either pass it in the special place, or pass it as an extra argument. */
3286 if (mem_value && struct_value == 0 && ! pcc_struct_value)
3287 {
3288 rtx addr = XEXP (mem_value, 0);
3289 nargs++;
3290
3291 /* Make sure it is a reasonable operand for a move or push insn. */
3292 if (!REG_P (addr) && !MEM_P (addr)
3293 && ! (CONSTANT_P (addr) && LEGITIMATE_CONSTANT_P (addr)))
3294 addr = force_operand (addr, NULL_RTX);
3295
3296 argvec[count].value = addr;
3297 argvec[count].mode = Pmode;
3298 argvec[count].partial = 0;
3299
3300 argvec[count].reg = FUNCTION_ARG (args_so_far, Pmode, NULL_TREE, 1);
3301 if (FUNCTION_ARG_PARTIAL_NREGS (args_so_far, Pmode, NULL_TREE, 1))
3302 abort ();
3303
3304 locate_and_pad_parm (Pmode, NULL_TREE,
3305 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3306 1,
3307 #else
3308 argvec[count].reg != 0,
3309 #endif
3310 0, NULL_TREE, &args_size, &argvec[count].locate);
3311
3312 if (argvec[count].reg == 0 || argvec[count].partial != 0
3313 || reg_parm_stack_space > 0)
3314 args_size.constant += argvec[count].locate.size.constant;
3315
3316 FUNCTION_ARG_ADVANCE (args_so_far, Pmode, (tree) 0, 1);
3317
3318 count++;
3319 }
3320
3321 for (; count < nargs; count++)
3322 {
3323 rtx val = va_arg (p, rtx);
3324 enum machine_mode mode = va_arg (p, enum machine_mode);
3325
3326 /* We cannot convert the arg value to the mode the library wants here;
3327 must do it earlier where we know the signedness of the arg. */
3328 if (mode == BLKmode
3329 || (GET_MODE (val) != mode && GET_MODE (val) != VOIDmode))
3330 abort ();
3331
3332 /* Make sure it is a reasonable operand for a move or push insn. */
3333 if (!REG_P (val) && !MEM_P (val)
3334 && ! (CONSTANT_P (val) && LEGITIMATE_CONSTANT_P (val)))
3335 val = force_operand (val, NULL_RTX);
3336
3337 if (pass_by_reference (&args_so_far, mode, NULL_TREE, 1))
3338 {
3339 rtx slot;
3340 int must_copy = ! FUNCTION_ARG_CALLEE_COPIES (args_so_far, mode,
3341 NULL_TREE, 1);
3342
3343 /* loop.c won't look at CALL_INSN_FUNCTION_USAGE of const/pure
3344 functions, so we have to pretend this isn't such a function. */
3345 if (flags & ECF_LIBCALL_BLOCK)
3346 {
3347 rtx insns = get_insns ();
3348 end_sequence ();
3349 emit_insn (insns);
3350 }
3351 flags &= ~(ECF_CONST | ECF_PURE | ECF_LIBCALL_BLOCK);
3352
3353 /* If this was a CONST function, it is now PURE since
3354 it now reads memory. */
3355 if (flags & ECF_CONST)
3356 {
3357 flags &= ~ECF_CONST;
3358 flags |= ECF_PURE;
3359 }
3360
3361 if (GET_MODE (val) == MEM && !must_copy)
3362 slot = val;
3363 else
3364 {
3365 slot = assign_temp (lang_hooks.types.type_for_mode (mode, 0),
3366 0, 1, 1);
3367 emit_move_insn (slot, val);
3368 }
3369
3370 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
3371 gen_rtx_USE (VOIDmode, slot),
3372 call_fusage);
3373 if (must_copy)
3374 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
3375 gen_rtx_CLOBBER (VOIDmode,
3376 slot),
3377 call_fusage);
3378
3379 mode = Pmode;
3380 val = force_operand (XEXP (slot, 0), NULL_RTX);
3381 }
3382
3383 argvec[count].value = val;
3384 argvec[count].mode = mode;
3385
3386 argvec[count].reg = FUNCTION_ARG (args_so_far, mode, NULL_TREE, 1);
3387
3388 argvec[count].partial
3389 = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, mode, NULL_TREE, 1);
3390
3391 locate_and_pad_parm (mode, NULL_TREE,
3392 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3393 1,
3394 #else
3395 argvec[count].reg != 0,
3396 #endif
3397 argvec[count].partial,
3398 NULL_TREE, &args_size, &argvec[count].locate);
3399
3400 if (argvec[count].locate.size.var)
3401 abort ();
3402
3403 if (argvec[count].reg == 0 || argvec[count].partial != 0
3404 || reg_parm_stack_space > 0)
3405 args_size.constant += argvec[count].locate.size.constant;
3406
3407 FUNCTION_ARG_ADVANCE (args_so_far, mode, (tree) 0, 1);
3408 }
3409
3410 /* If this machine requires an external definition for library
3411 functions, write one out. */
3412 assemble_external_libcall (fun);
3413
3414 original_args_size = args_size;
3415 args_size.constant = (((args_size.constant
3416 + stack_pointer_delta
3417 + STACK_BYTES - 1)
3418 / STACK_BYTES
3419 * STACK_BYTES)
3420 - stack_pointer_delta);
3421
3422 args_size.constant = MAX (args_size.constant,
3423 reg_parm_stack_space);
3424
3425 #ifndef OUTGOING_REG_PARM_STACK_SPACE
3426 args_size.constant -= reg_parm_stack_space;
3427 #endif
3428
3429 if (args_size.constant > current_function_outgoing_args_size)
3430 current_function_outgoing_args_size = args_size.constant;
3431
3432 if (ACCUMULATE_OUTGOING_ARGS)
3433 {
3434 /* Since the stack pointer will never be pushed, it is possible for
3435 the evaluation of a parm to clobber something we have already
3436 written to the stack. Since most function calls on RISC machines
3437 do not use the stack, this is uncommon, but must work correctly.
3438
3439 Therefore, we save any area of the stack that was already written
3440 and that we are using. Here we set up to do this by making a new
3441 stack usage map from the old one.
3442
3443 Another approach might be to try to reorder the argument
3444 evaluations to avoid this conflicting stack usage. */
3445
3446 needed = args_size.constant;
3447
3448 #ifndef OUTGOING_REG_PARM_STACK_SPACE
3449 /* Since we will be writing into the entire argument area, the
3450 map must be allocated for its entire size, not just the part that
3451 is the responsibility of the caller. */
3452 needed += reg_parm_stack_space;
3453 #endif
3454
3455 #ifdef ARGS_GROW_DOWNWARD
3456 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3457 needed + 1);
3458 #else
3459 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3460 needed);
3461 #endif
3462 stack_usage_map = alloca (highest_outgoing_arg_in_use);
3463
3464 if (initial_highest_arg_in_use)
3465 memcpy (stack_usage_map, initial_stack_usage_map,
3466 initial_highest_arg_in_use);
3467
3468 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
3469 memset (&stack_usage_map[initial_highest_arg_in_use], 0,
3470 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
3471 needed = 0;
3472
3473 /* We must be careful to use virtual regs before they're instantiated,
3474 and real regs afterwards. Loop optimization, for example, can create
3475 new libcalls after we've instantiated the virtual regs, and if we
3476 use virtuals anyway, they won't match the rtl patterns. */
3477
3478 if (virtuals_instantiated)
3479 argblock = plus_constant (stack_pointer_rtx, STACK_POINTER_OFFSET);
3480 else
3481 argblock = virtual_outgoing_args_rtx;
3482 }
3483 else
3484 {
3485 if (!PUSH_ARGS)
3486 argblock = push_block (GEN_INT (args_size.constant), 0, 0);
3487 }
3488
3489 /* If we push args individually in reverse order, perform stack alignment
3490 before the first push (the last arg). */
3491 if (argblock == 0 && PUSH_ARGS_REVERSED)
3492 anti_adjust_stack (GEN_INT (args_size.constant
3493 - original_args_size.constant));
3494
3495 if (PUSH_ARGS_REVERSED)
3496 {
3497 inc = -1;
3498 argnum = nargs - 1;
3499 }
3500 else
3501 {
3502 inc = 1;
3503 argnum = 0;
3504 }
3505
3506 #ifdef REG_PARM_STACK_SPACE
3507 if (ACCUMULATE_OUTGOING_ARGS)
3508 {
3509 /* The argument list is the property of the called routine and it
3510 may clobber it. If the fixed area has been used for previous
3511 parameters, we must save and restore it. */
3512 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
3513 &low_to_save, &high_to_save);
3514 }
3515 #endif
3516
3517 /* Push the args that need to be pushed. */
3518
3519 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
3520 are to be pushed. */
3521 for (count = 0; count < nargs; count++, argnum += inc)
3522 {
3523 enum machine_mode mode = argvec[argnum].mode;
3524 rtx val = argvec[argnum].value;
3525 rtx reg = argvec[argnum].reg;
3526 int partial = argvec[argnum].partial;
3527 int lower_bound = 0, upper_bound = 0, i;
3528
3529 if (! (reg != 0 && partial == 0))
3530 {
3531 if (ACCUMULATE_OUTGOING_ARGS)
3532 {
3533 /* If this is being stored into a pre-allocated, fixed-size,
3534 stack area, save any previous data at that location. */
3535
3536 #ifdef ARGS_GROW_DOWNWARD
3537 /* stack_slot is negative, but we want to index stack_usage_map
3538 with positive values. */
3539 upper_bound = -argvec[argnum].locate.offset.constant + 1;
3540 lower_bound = upper_bound - argvec[argnum].locate.size.constant;
3541 #else
3542 lower_bound = argvec[argnum].locate.offset.constant;
3543 upper_bound = lower_bound + argvec[argnum].locate.size.constant;
3544 #endif
3545
3546 i = lower_bound;
3547 /* Don't worry about things in the fixed argument area;
3548 it has already been saved. */
3549 if (i < reg_parm_stack_space)
3550 i = reg_parm_stack_space;
3551 while (i < upper_bound && stack_usage_map[i] == 0)
3552 i++;
3553
3554 if (i < upper_bound)
3555 {
3556 /* We need to make a save area. */
3557 unsigned int size
3558 = argvec[argnum].locate.size.constant * BITS_PER_UNIT;
3559 enum machine_mode save_mode
3560 = mode_for_size (size, MODE_INT, 1);
3561 rtx adr
3562 = plus_constant (argblock,
3563 argvec[argnum].locate.offset.constant);
3564 rtx stack_area
3565 = gen_rtx_MEM (save_mode, memory_address (save_mode, adr));
3566
3567 if (save_mode == BLKmode)
3568 {
3569 argvec[argnum].save_area
3570 = assign_stack_temp (BLKmode,
3571 argvec[argnum].locate.size.constant,
3572 0);
3573
3574 emit_block_move (validize_mem (argvec[argnum].save_area),
3575 stack_area,
3576 GEN_INT (argvec[argnum].locate.size.constant),
3577 BLOCK_OP_CALL_PARM);
3578 }
3579 else
3580 {
3581 argvec[argnum].save_area = gen_reg_rtx (save_mode);
3582
3583 emit_move_insn (argvec[argnum].save_area, stack_area);
3584 }
3585 }
3586 }
3587
3588 emit_push_insn (val, mode, NULL_TREE, NULL_RTX, PARM_BOUNDARY,
3589 partial, reg, 0, argblock,
3590 GEN_INT (argvec[argnum].locate.offset.constant),
3591 reg_parm_stack_space,
3592 ARGS_SIZE_RTX (argvec[argnum].locate.alignment_pad));
3593
3594 /* Now mark the segment we just used. */
3595 if (ACCUMULATE_OUTGOING_ARGS)
3596 for (i = lower_bound; i < upper_bound; i++)
3597 stack_usage_map[i] = 1;
3598
3599 NO_DEFER_POP;
3600 }
3601 }
3602
3603 /* If we pushed args in forward order, perform stack alignment
3604 after pushing the last arg. */
3605 if (argblock == 0 && !PUSH_ARGS_REVERSED)
3606 anti_adjust_stack (GEN_INT (args_size.constant
3607 - original_args_size.constant));
3608
3609 if (PUSH_ARGS_REVERSED)
3610 argnum = nargs - 1;
3611 else
3612 argnum = 0;
3613
3614 fun = prepare_call_address (fun, NULL, &call_fusage, 0, 0);
3615
3616 /* Now load any reg parms into their regs. */
3617
3618 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
3619 are to be pushed. */
3620 for (count = 0; count < nargs; count++, argnum += inc)
3621 {
3622 enum machine_mode mode = argvec[argnum].mode;
3623 rtx val = argvec[argnum].value;
3624 rtx reg = argvec[argnum].reg;
3625 int partial = argvec[argnum].partial;
3626
3627 /* Handle calls that pass values in multiple non-contiguous
3628 locations. The PA64 has examples of this for library calls. */
3629 if (reg != 0 && GET_CODE (reg) == PARALLEL)
3630 emit_group_load (reg, val, NULL_TREE, GET_MODE_SIZE (mode));
3631 else if (reg != 0 && partial == 0)
3632 emit_move_insn (reg, val);
3633
3634 NO_DEFER_POP;
3635 }
3636
3637 /* Any regs containing parms remain in use through the call. */
3638 for (count = 0; count < nargs; count++)
3639 {
3640 rtx reg = argvec[count].reg;
3641 if (reg != 0 && GET_CODE (reg) == PARALLEL)
3642 use_group_regs (&call_fusage, reg);
3643 else if (reg != 0)
3644 use_reg (&call_fusage, reg);
3645 }
3646
3647 /* Pass the function the address in which to return a structure value. */
3648 if (mem_value != 0 && struct_value != 0 && ! pcc_struct_value)
3649 {
3650 emit_move_insn (struct_value,
3651 force_reg (Pmode,
3652 force_operand (XEXP (mem_value, 0),
3653 NULL_RTX)));
3654 if (REG_P (struct_value))
3655 use_reg (&call_fusage, struct_value);
3656 }
3657
3658 /* Don't allow popping to be deferred, since then
3659 cse'ing of library calls could delete a call and leave the pop. */
3660 NO_DEFER_POP;
3661 valreg = (mem_value == 0 && outmode != VOIDmode
3662 ? hard_libcall_value (outmode) : NULL_RTX);
3663
3664 /* Stack must be properly aligned now. */
3665 if (stack_pointer_delta & (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1))
3666 abort ();
3667
3668 before_call = get_last_insn ();
3669
3670 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
3671 will set inhibit_defer_pop to that value. */
3672 /* The return type is needed to decide how many bytes the function pops.
3673 Signedness plays no role in that, so for simplicity, we pretend it's
3674 always signed. We also assume that the list of arguments passed has
3675 no impact, so we pretend it is unknown. */
3676
3677 emit_call_1 (fun, NULL,
3678 get_identifier (XSTR (orgfun, 0)),
3679 build_function_type (tfom, NULL_TREE),
3680 original_args_size.constant, args_size.constant,
3681 struct_value_size,
3682 FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1),
3683 valreg,
3684 old_inhibit_defer_pop + 1, call_fusage, flags, & args_so_far);
3685
3686 /* For calls to `setjmp', etc., inform flow.c it should complain
3687 if nonvolatile values are live. For functions that cannot return,
3688 inform flow that control does not fall through. */
3689
3690 if (flags & (ECF_NORETURN | ECF_LONGJMP))
3691 {
3692 /* The barrier note must be emitted
3693 immediately after the CALL_INSN. Some ports emit more than
3694 just a CALL_INSN above, so we must search for it here. */
3695
3696 rtx last = get_last_insn ();
3697 while (!CALL_P (last))
3698 {
3699 last = PREV_INSN (last);
3700 /* There was no CALL_INSN? */
3701 if (last == before_call)
3702 abort ();
3703 }
3704
3705 emit_barrier_after (last);
3706 }
3707
3708 /* Now restore inhibit_defer_pop to its actual original value. */
3709 OK_DEFER_POP;
3710
3711 /* If call is cse'able, make appropriate pair of reg-notes around it.
3712 Test valreg so we don't crash; may safely ignore `const'
3713 if return type is void. Disable for PARALLEL return values, because
3714 we have no way to move such values into a pseudo register. */
3715 if (flags & ECF_LIBCALL_BLOCK)
3716 {
3717 rtx insns;
3718
3719 if (valreg == 0)
3720 {
3721 insns = get_insns ();
3722 end_sequence ();
3723 emit_insn (insns);
3724 }
3725 else
3726 {
3727 rtx note = 0;
3728 rtx temp;
3729 int i;
3730
3731 if (GET_CODE (valreg) == PARALLEL)
3732 {
3733 temp = gen_reg_rtx (outmode);
3734 emit_group_store (temp, valreg, NULL_TREE,
3735 GET_MODE_SIZE (outmode));
3736 valreg = temp;
3737 }
3738
3739 temp = gen_reg_rtx (GET_MODE (valreg));
3740
3741 /* Construct an "equal form" for the value which mentions all the
3742 arguments in order as well as the function name. */
3743 for (i = 0; i < nargs; i++)
3744 note = gen_rtx_EXPR_LIST (VOIDmode, argvec[i].value, note);
3745 note = gen_rtx_EXPR_LIST (VOIDmode, fun, note);
3746
3747 insns = get_insns ();
3748 end_sequence ();
3749
3750 if (flags & ECF_PURE)
3751 note = gen_rtx_EXPR_LIST (VOIDmode,
3752 gen_rtx_USE (VOIDmode,
3753 gen_rtx_MEM (BLKmode,
3754 gen_rtx_SCRATCH (VOIDmode))),
3755 note);
3756
3757 emit_libcall_block (insns, temp, valreg, note);
3758
3759 valreg = temp;
3760 }
3761 }
3762 pop_temp_slots ();
3763
3764 /* Copy the value to the right place. */
3765 if (outmode != VOIDmode && retval)
3766 {
3767 if (mem_value)
3768 {
3769 if (value == 0)
3770 value = mem_value;
3771 if (value != mem_value)
3772 emit_move_insn (value, mem_value);
3773 }
3774 else if (GET_CODE (valreg) == PARALLEL)
3775 {
3776 if (value == 0)
3777 value = gen_reg_rtx (outmode);
3778 emit_group_store (value, valreg, NULL_TREE, GET_MODE_SIZE (outmode));
3779 }
3780 else if (value != 0)
3781 emit_move_insn (value, valreg);
3782 else
3783 value = valreg;
3784 }
3785
3786 if (ACCUMULATE_OUTGOING_ARGS)
3787 {
3788 #ifdef REG_PARM_STACK_SPACE
3789 if (save_area)
3790 restore_fixed_argument_area (save_area, argblock,
3791 high_to_save, low_to_save);
3792 #endif
3793
3794 /* If we saved any argument areas, restore them. */
3795 for (count = 0; count < nargs; count++)
3796 if (argvec[count].save_area)
3797 {
3798 enum machine_mode save_mode = GET_MODE (argvec[count].save_area);
3799 rtx adr = plus_constant (argblock,
3800 argvec[count].locate.offset.constant);
3801 rtx stack_area = gen_rtx_MEM (save_mode,
3802 memory_address (save_mode, adr));
3803
3804 if (save_mode == BLKmode)
3805 emit_block_move (stack_area,
3806 validize_mem (argvec[count].save_area),
3807 GEN_INT (argvec[count].locate.size.constant),
3808 BLOCK_OP_CALL_PARM);
3809 else
3810 emit_move_insn (stack_area, argvec[count].save_area);
3811 }
3812
3813 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3814 stack_usage_map = initial_stack_usage_map;
3815 }
3816
3817 return value;
3818
3819 }
3820 \f
3821 /* Output a library call to function FUN (a SYMBOL_REF rtx)
3822 (emitting the queue unless NO_QUEUE is nonzero),
3823 for a value of mode OUTMODE,
3824 with NARGS different arguments, passed as alternating rtx values
3825 and machine_modes to convert them to.
3826
3827 FN_TYPE should be LCT_NORMAL for `normal' calls, LCT_CONST for `const'
3828 calls, LCT_PURE for `pure' calls, LCT_CONST_MAKE_BLOCK for `const' calls
3829 which should be enclosed in REG_LIBCALL/REG_RETVAL notes,
3830 LCT_PURE_MAKE_BLOCK for `purep' calls which should be enclosed in
3831 REG_LIBCALL/REG_RETVAL notes with extra (use (memory (scratch)),
3832 or other LCT_ value for other types of library calls. */
3833
3834 void
3835 emit_library_call (rtx orgfun, enum libcall_type fn_type,
3836 enum machine_mode outmode, int nargs, ...)
3837 {
3838 va_list p;
3839
3840 va_start (p, nargs);
3841 emit_library_call_value_1 (0, orgfun, NULL_RTX, fn_type, outmode, nargs, p);
3842 va_end (p);
3843 }
3844 \f
3845 /* Like emit_library_call except that an extra argument, VALUE,
3846 comes second and says where to store the result.
3847 (If VALUE is zero, this function chooses a convenient way
3848 to return the value.
3849
3850 This function returns an rtx for where the value is to be found.
3851 If VALUE is nonzero, VALUE is returned. */
3852
3853 rtx
3854 emit_library_call_value (rtx orgfun, rtx value,
3855 enum libcall_type fn_type,
3856 enum machine_mode outmode, int nargs, ...)
3857 {
3858 rtx result;
3859 va_list p;
3860
3861 va_start (p, nargs);
3862 result = emit_library_call_value_1 (1, orgfun, value, fn_type, outmode,
3863 nargs, p);
3864 va_end (p);
3865
3866 return result;
3867 }
3868 \f
3869 /* Store a single argument for a function call
3870 into the register or memory area where it must be passed.
3871 *ARG describes the argument value and where to pass it.
3872
3873 ARGBLOCK is the address of the stack-block for all the arguments,
3874 or 0 on a machine where arguments are pushed individually.
3875
3876 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
3877 so must be careful about how the stack is used.
3878
3879 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
3880 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
3881 that we need not worry about saving and restoring the stack.
3882
3883 FNDECL is the declaration of the function we are calling.
3884
3885 Return nonzero if this arg should cause sibcall failure,
3886 zero otherwise. */
3887
3888 static int
3889 store_one_arg (struct arg_data *arg, rtx argblock, int flags,
3890 int variable_size ATTRIBUTE_UNUSED, int reg_parm_stack_space)
3891 {
3892 tree pval = arg->tree_value;
3893 rtx reg = 0;
3894 int partial = 0;
3895 int used = 0;
3896 int i, lower_bound = 0, upper_bound = 0;
3897 int sibcall_failure = 0;
3898
3899 if (TREE_CODE (pval) == ERROR_MARK)
3900 return 1;
3901
3902 /* Push a new temporary level for any temporaries we make for
3903 this argument. */
3904 push_temp_slots ();
3905
3906 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
3907 {
3908 /* If this is being stored into a pre-allocated, fixed-size, stack area,
3909 save any previous data at that location. */
3910 if (argblock && ! variable_size && arg->stack)
3911 {
3912 #ifdef ARGS_GROW_DOWNWARD
3913 /* stack_slot is negative, but we want to index stack_usage_map
3914 with positive values. */
3915 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
3916 upper_bound = -INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1)) + 1;
3917 else
3918 upper_bound = 0;
3919
3920 lower_bound = upper_bound - arg->locate.size.constant;
3921 #else
3922 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
3923 lower_bound = INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1));
3924 else
3925 lower_bound = 0;
3926
3927 upper_bound = lower_bound + arg->locate.size.constant;
3928 #endif
3929
3930 i = lower_bound;
3931 /* Don't worry about things in the fixed argument area;
3932 it has already been saved. */
3933 if (i < reg_parm_stack_space)
3934 i = reg_parm_stack_space;
3935 while (i < upper_bound && stack_usage_map[i] == 0)
3936 i++;
3937
3938 if (i < upper_bound)
3939 {
3940 /* We need to make a save area. */
3941 unsigned int size = arg->locate.size.constant * BITS_PER_UNIT;
3942 enum machine_mode save_mode = mode_for_size (size, MODE_INT, 1);
3943 rtx adr = memory_address (save_mode, XEXP (arg->stack_slot, 0));
3944 rtx stack_area = gen_rtx_MEM (save_mode, adr);
3945
3946 if (save_mode == BLKmode)
3947 {
3948 tree ot = TREE_TYPE (arg->tree_value);
3949 tree nt = build_qualified_type (ot, (TYPE_QUALS (ot)
3950 | TYPE_QUAL_CONST));
3951
3952 arg->save_area = assign_temp (nt, 0, 1, 1);
3953 preserve_temp_slots (arg->save_area);
3954 emit_block_move (validize_mem (arg->save_area), stack_area,
3955 expr_size (arg->tree_value),
3956 BLOCK_OP_CALL_PARM);
3957 }
3958 else
3959 {
3960 arg->save_area = gen_reg_rtx (save_mode);
3961 emit_move_insn (arg->save_area, stack_area);
3962 }
3963 }
3964 }
3965 }
3966
3967 /* If this isn't going to be placed on both the stack and in registers,
3968 set up the register and number of words. */
3969 if (! arg->pass_on_stack)
3970 {
3971 if (flags & ECF_SIBCALL)
3972 reg = arg->tail_call_reg;
3973 else
3974 reg = arg->reg;
3975 partial = arg->partial;
3976 }
3977
3978 if (reg != 0 && partial == 0)
3979 /* Being passed entirely in a register. We shouldn't be called in
3980 this case. */
3981 abort ();
3982
3983 /* If this arg needs special alignment, don't load the registers
3984 here. */
3985 if (arg->n_aligned_regs != 0)
3986 reg = 0;
3987
3988 /* If this is being passed partially in a register, we can't evaluate
3989 it directly into its stack slot. Otherwise, we can. */
3990 if (arg->value == 0)
3991 {
3992 /* stack_arg_under_construction is nonzero if a function argument is
3993 being evaluated directly into the outgoing argument list and
3994 expand_call must take special action to preserve the argument list
3995 if it is called recursively.
3996
3997 For scalar function arguments stack_usage_map is sufficient to
3998 determine which stack slots must be saved and restored. Scalar
3999 arguments in general have pass_on_stack == 0.
4000
4001 If this argument is initialized by a function which takes the
4002 address of the argument (a C++ constructor or a C function
4003 returning a BLKmode structure), then stack_usage_map is
4004 insufficient and expand_call must push the stack around the
4005 function call. Such arguments have pass_on_stack == 1.
4006
4007 Note that it is always safe to set stack_arg_under_construction,
4008 but this generates suboptimal code if set when not needed. */
4009
4010 if (arg->pass_on_stack)
4011 stack_arg_under_construction++;
4012
4013 arg->value = expand_expr (pval,
4014 (partial
4015 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
4016 ? NULL_RTX : arg->stack,
4017 VOIDmode, EXPAND_STACK_PARM);
4018
4019 /* If we are promoting object (or for any other reason) the mode
4020 doesn't agree, convert the mode. */
4021
4022 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
4023 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
4024 arg->value, arg->unsignedp);
4025
4026 if (arg->pass_on_stack)
4027 stack_arg_under_construction--;
4028 }
4029
4030 /* Don't allow anything left on stack from computation
4031 of argument to alloca. */
4032 if (flags & ECF_MAY_BE_ALLOCA)
4033 do_pending_stack_adjust ();
4034
4035 if (arg->value == arg->stack)
4036 /* If the value is already in the stack slot, we are done. */
4037 ;
4038 else if (arg->mode != BLKmode)
4039 {
4040 int size;
4041
4042 /* Argument is a scalar, not entirely passed in registers.
4043 (If part is passed in registers, arg->partial says how much
4044 and emit_push_insn will take care of putting it there.)
4045
4046 Push it, and if its size is less than the
4047 amount of space allocated to it,
4048 also bump stack pointer by the additional space.
4049 Note that in C the default argument promotions
4050 will prevent such mismatches. */
4051
4052 size = GET_MODE_SIZE (arg->mode);
4053 /* Compute how much space the push instruction will push.
4054 On many machines, pushing a byte will advance the stack
4055 pointer by a halfword. */
4056 #ifdef PUSH_ROUNDING
4057 size = PUSH_ROUNDING (size);
4058 #endif
4059 used = size;
4060
4061 /* Compute how much space the argument should get:
4062 round up to a multiple of the alignment for arguments. */
4063 if (none != FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)))
4064 used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
4065 / (PARM_BOUNDARY / BITS_PER_UNIT))
4066 * (PARM_BOUNDARY / BITS_PER_UNIT));
4067
4068 /* This isn't already where we want it on the stack, so put it there.
4069 This can either be done with push or copy insns. */
4070 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), NULL_RTX,
4071 PARM_BOUNDARY, partial, reg, used - size, argblock,
4072 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4073 ARGS_SIZE_RTX (arg->locate.alignment_pad));
4074
4075 /* Unless this is a partially-in-register argument, the argument is now
4076 in the stack. */
4077 if (partial == 0)
4078 arg->value = arg->stack;
4079 }
4080 else
4081 {
4082 /* BLKmode, at least partly to be pushed. */
4083
4084 unsigned int parm_align;
4085 int excess;
4086 rtx size_rtx;
4087
4088 /* Pushing a nonscalar.
4089 If part is passed in registers, PARTIAL says how much
4090 and emit_push_insn will take care of putting it there. */
4091
4092 /* Round its size up to a multiple
4093 of the allocation unit for arguments. */
4094
4095 if (arg->locate.size.var != 0)
4096 {
4097 excess = 0;
4098 size_rtx = ARGS_SIZE_RTX (arg->locate.size);
4099 }
4100 else
4101 {
4102 /* PUSH_ROUNDING has no effect on us, because
4103 emit_push_insn for BLKmode is careful to avoid it. */
4104 if (reg && GET_CODE (reg) == PARALLEL)
4105 {
4106 /* Use the size of the elt to compute excess. */
4107 rtx elt = XEXP (XVECEXP (reg, 0, 0), 0);
4108 excess = (arg->locate.size.constant
4109 - int_size_in_bytes (TREE_TYPE (pval))
4110 + partial * GET_MODE_SIZE (GET_MODE (elt)));
4111 }
4112 else
4113 excess = (arg->locate.size.constant
4114 - int_size_in_bytes (TREE_TYPE (pval))
4115 + partial * UNITS_PER_WORD);
4116 size_rtx = expand_expr (size_in_bytes (TREE_TYPE (pval)),
4117 NULL_RTX, TYPE_MODE (sizetype), 0);
4118 }
4119
4120 /* Some types will require stricter alignment, which will be
4121 provided for elsewhere in argument layout. */
4122 parm_align = MAX (PARM_BOUNDARY, TYPE_ALIGN (TREE_TYPE (pval)));
4123
4124 /* When an argument is padded down, the block is aligned to
4125 PARM_BOUNDARY, but the actual argument isn't. */
4126 if (FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)) == downward)
4127 {
4128 if (arg->locate.size.var)
4129 parm_align = BITS_PER_UNIT;
4130 else if (excess)
4131 {
4132 unsigned int excess_align = (excess & -excess) * BITS_PER_UNIT;
4133 parm_align = MIN (parm_align, excess_align);
4134 }
4135 }
4136
4137 if ((flags & ECF_SIBCALL) && MEM_P (arg->value))
4138 {
4139 /* emit_push_insn might not work properly if arg->value and
4140 argblock + arg->locate.offset areas overlap. */
4141 rtx x = arg->value;
4142 int i = 0;
4143
4144 if (XEXP (x, 0) == current_function_internal_arg_pointer
4145 || (GET_CODE (XEXP (x, 0)) == PLUS
4146 && XEXP (XEXP (x, 0), 0) ==
4147 current_function_internal_arg_pointer
4148 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4149 {
4150 if (XEXP (x, 0) != current_function_internal_arg_pointer)
4151 i = INTVAL (XEXP (XEXP (x, 0), 1));
4152
4153 /* expand_call should ensure this. */
4154 if (arg->locate.offset.var || GET_CODE (size_rtx) != CONST_INT)
4155 abort ();
4156
4157 if (arg->locate.offset.constant > i)
4158 {
4159 if (arg->locate.offset.constant < i + INTVAL (size_rtx))
4160 sibcall_failure = 1;
4161 }
4162 else if (arg->locate.offset.constant < i)
4163 {
4164 if (i < arg->locate.offset.constant + INTVAL (size_rtx))
4165 sibcall_failure = 1;
4166 }
4167 }
4168 }
4169
4170 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
4171 parm_align, partial, reg, excess, argblock,
4172 ARGS_SIZE_RTX (arg->locate.offset), reg_parm_stack_space,
4173 ARGS_SIZE_RTX (arg->locate.alignment_pad));
4174
4175 /* Unless this is a partially-in-register argument, the argument is now
4176 in the stack.
4177
4178 ??? Unlike the case above, in which we want the actual
4179 address of the data, so that we can load it directly into a
4180 register, here we want the address of the stack slot, so that
4181 it's properly aligned for word-by-word copying or something
4182 like that. It's not clear that this is always correct. */
4183 if (partial == 0)
4184 arg->value = arg->stack_slot;
4185 }
4186
4187 /* Mark all slots this store used. */
4188 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL)
4189 && argblock && ! variable_size && arg->stack)
4190 for (i = lower_bound; i < upper_bound; i++)
4191 stack_usage_map[i] = 1;
4192
4193 /* Once we have pushed something, pops can't safely
4194 be deferred during the rest of the arguments. */
4195 NO_DEFER_POP;
4196
4197 /* Free any temporary slots made in processing this argument. Show
4198 that we might have taken the address of something and pushed that
4199 as an operand. */
4200 preserve_temp_slots (NULL_RTX);
4201 free_temp_slots ();
4202 pop_temp_slots ();
4203
4204 return sibcall_failure;
4205 }
4206
4207 /* Nonzero if we do not know how to pass TYPE solely in registers. */
4208
4209 bool
4210 must_pass_in_stack_var_size (enum machine_mode mode ATTRIBUTE_UNUSED,
4211 tree type)
4212 {
4213 if (!type)
4214 return false;
4215
4216 /* If the type has variable size... */
4217 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4218 return true;
4219
4220 /* If the type is marked as addressable (it is required
4221 to be constructed into the stack)... */
4222 if (TREE_ADDRESSABLE (type))
4223 return true;
4224
4225 return false;
4226 }
4227
4228 /* Another version of the TARGET_MUST_PASS_IN_STACK hook. This one
4229 takes trailing padding of a structure into account. */
4230 /* ??? Should be able to merge these two by examining BLOCK_REG_PADDING. */
4231
4232 bool
4233 must_pass_in_stack_var_size_or_pad (enum machine_mode mode, tree type)
4234 {
4235 if (!type)
4236 return false;
4237
4238 /* If the type has variable size... */
4239 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4240 return true;
4241
4242 /* If the type is marked as addressable (it is required
4243 to be constructed into the stack)... */
4244 if (TREE_ADDRESSABLE (type))
4245 return true;
4246
4247 /* If the padding and mode of the type is such that a copy into
4248 a register would put it into the wrong part of the register. */
4249 if (mode == BLKmode
4250 && int_size_in_bytes (type) % (PARM_BOUNDARY / BITS_PER_UNIT)
4251 && (FUNCTION_ARG_PADDING (mode, type)
4252 == (BYTES_BIG_ENDIAN ? upward : downward)))
4253 return true;
4254
4255 return false;
4256 }