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