rs6000-call.cc revision 1.1.1.1 1 /* Subroutines used to generate function calls and handle built-in
2 instructions on IBM RS/6000.
3 Copyright (C) 1991-2022 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #define IN_TARGET_CODE 1
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "memmodel.h"
30 #include "gimple.h"
31 #include "cfghooks.h"
32 #include "cfgloop.h"
33 #include "df.h"
34 #include "tm_p.h"
35 #include "stringpool.h"
36 #include "expmed.h"
37 #include "optabs.h"
38 #include "regs.h"
39 #include "ira.h"
40 #include "recog.h"
41 #include "cgraph.h"
42 #include "diagnostic-core.h"
43 #include "insn-attr.h"
44 #include "flags.h"
45 #include "alias.h"
46 #include "fold-const.h"
47 #include "attribs.h"
48 #include "stor-layout.h"
49 #include "calls.h"
50 #include "print-tree.h"
51 #include "varasm.h"
52 #include "explow.h"
53 #include "expr.h"
54 #include "output.h"
55 #include "common/common-target.h"
56 #include "langhooks.h"
57 #include "gimplify.h"
58 #include "gimple-fold.h"
59 #include "gimple-iterator.h"
60 #include "ssa.h"
61 #include "tree-ssa-propagate.h"
62 #include "builtins.h"
63 #include "tree-vector-builder.h"
64 #if TARGET_XCOFF
65 #include "xcoffout.h" /* get declarations of xcoff_*_section_name */
66 #endif
67 #include "ppc-auxv.h"
68 #include "targhooks.h"
69 #include "opts.h"
70
71 #include "rs6000-internal.h"
72
73 #if TARGET_MACHO
74 #include "gstab.h" /* for N_SLINE */
75 #include "dbxout.h" /* dbxout_ */
76 #endif
77
78 #ifndef TARGET_PROFILE_KERNEL
79 #define TARGET_PROFILE_KERNEL 0
80 #endif
81
82 #ifdef HAVE_AS_GNU_ATTRIBUTE
83 # ifndef HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE
84 # define HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE 0
85 # endif
86 #endif
87
88 #ifndef TARGET_NO_PROTOTYPE
89 #define TARGET_NO_PROTOTYPE 0
90 #endif
91
92 /* Nonzero if we can use a floating-point register to pass this arg. */
93 #define USE_FP_FOR_ARG_P(CUM,MODE) \
94 (SCALAR_FLOAT_MODE_NOT_VECTOR_P (MODE) \
95 && (CUM)->fregno <= FP_ARG_MAX_REG \
96 && TARGET_HARD_FLOAT)
97
98 /* Nonzero if we can use an AltiVec register to pass this arg. */
99 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,NAMED) \
100 (ALTIVEC_OR_VSX_VECTOR_MODE (MODE) \
101 && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG \
102 && TARGET_ALTIVEC_ABI \
103 && (NAMED))
104
105 /* Walk down the type tree of TYPE counting consecutive base elements.
106 If *MODEP is VOIDmode, then set it to the first valid floating point
107 or vector type. If a non-floating point or vector type is found, or
108 if a floating point or vector type that doesn't match a non-VOIDmode
109 *MODEP is found, then return -1, otherwise return the count in the
110 sub-tree.
111
112 There have been some ABI snafus along the way with C++. Modify
113 EMPTY_BASE_SEEN to a nonzero value iff a C++ empty base class makes
114 an appearance; separate flag bits indicate whether or not such a
115 field is marked "no unique address". Modify ZERO_WIDTH_BF_SEEN
116 to 1 iff a C++ zero-length bitfield makes an appearance, but
117 in this case otherwise treat this as still being a homogeneous
118 aggregate. */
119
120 static int
121 rs6000_aggregate_candidate (const_tree type, machine_mode *modep,
122 int *empty_base_seen, int *zero_width_bf_seen)
123 {
124 machine_mode mode;
125 HOST_WIDE_INT size;
126
127 switch (TREE_CODE (type))
128 {
129 case REAL_TYPE:
130 mode = TYPE_MODE (type);
131 if (!SCALAR_FLOAT_MODE_P (mode))
132 return -1;
133
134 if (*modep == VOIDmode)
135 *modep = mode;
136
137 if (*modep == mode)
138 return 1;
139
140 break;
141
142 case COMPLEX_TYPE:
143 mode = TYPE_MODE (TREE_TYPE (type));
144 if (!SCALAR_FLOAT_MODE_P (mode))
145 return -1;
146
147 if (*modep == VOIDmode)
148 *modep = mode;
149
150 if (*modep == mode)
151 return 2;
152
153 break;
154
155 case VECTOR_TYPE:
156 if (!TARGET_ALTIVEC_ABI || !TARGET_ALTIVEC)
157 return -1;
158
159 /* Use V4SImode as representative of all 128-bit vector types. */
160 size = int_size_in_bytes (type);
161 switch (size)
162 {
163 case 16:
164 mode = V4SImode;
165 break;
166 default:
167 return -1;
168 }
169
170 if (*modep == VOIDmode)
171 *modep = mode;
172
173 /* Vector modes are considered to be opaque: two vectors are
174 equivalent for the purposes of being homogeneous aggregates
175 if they are the same size. */
176 if (*modep == mode)
177 return 1;
178
179 break;
180
181 case ARRAY_TYPE:
182 {
183 int count;
184 tree index = TYPE_DOMAIN (type);
185
186 /* Can't handle incomplete types nor sizes that are not
187 fixed. */
188 if (!COMPLETE_TYPE_P (type)
189 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
190 return -1;
191
192 count = rs6000_aggregate_candidate (TREE_TYPE (type), modep,
193 empty_base_seen,
194 zero_width_bf_seen);
195 if (count == -1
196 || !index
197 || !TYPE_MAX_VALUE (index)
198 || !tree_fits_uhwi_p (TYPE_MAX_VALUE (index))
199 || !TYPE_MIN_VALUE (index)
200 || !tree_fits_uhwi_p (TYPE_MIN_VALUE (index))
201 || count < 0)
202 return -1;
203
204 count *= (1 + tree_to_uhwi (TYPE_MAX_VALUE (index))
205 - tree_to_uhwi (TYPE_MIN_VALUE (index)));
206
207 /* There must be no padding. */
208 if (wi::to_wide (TYPE_SIZE (type))
209 != count * GET_MODE_BITSIZE (*modep))
210 return -1;
211
212 return count;
213 }
214
215 case RECORD_TYPE:
216 {
217 int count = 0;
218 int sub_count;
219 tree field;
220
221 /* Can't handle incomplete types nor sizes that are not
222 fixed. */
223 if (!COMPLETE_TYPE_P (type)
224 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
225 return -1;
226
227 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
228 {
229 if (TREE_CODE (field) != FIELD_DECL)
230 continue;
231
232 if (DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD (field))
233 {
234 /* GCC 11 and earlier generated incorrect code in a rare
235 corner case for C++. When a RECORD_TYPE looks like a
236 homogeneous aggregate, except that it also contains
237 one or more zero-width bit fields, these earlier
238 compilers would incorrectly pass the fields in FPRs
239 or VSRs. This occurred because the front end wrongly
240 removed these bitfields from the RECORD_TYPE. In
241 GCC 12 and later, the front end flaw was corrected.
242 We want to diagnose this case. To do this, we pretend
243 that we don't see the zero-width bit fields (hence
244 the continue statement here), but pass back a flag
245 indicating what happened. The caller then diagnoses
246 the issue and rejects the RECORD_TYPE as a homogeneous
247 aggregate. */
248 *zero_width_bf_seen = 1;
249 continue;
250 }
251
252 if (DECL_FIELD_ABI_IGNORED (field))
253 {
254 if (lookup_attribute ("no_unique_address",
255 DECL_ATTRIBUTES (field)))
256 *empty_base_seen |= 2;
257 else
258 *empty_base_seen |= 1;
259 continue;
260 }
261
262 sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep,
263 empty_base_seen,
264 zero_width_bf_seen);
265 if (sub_count < 0)
266 return -1;
267 count += sub_count;
268 }
269
270 /* There must be no padding. */
271 if (wi::to_wide (TYPE_SIZE (type))
272 != count * GET_MODE_BITSIZE (*modep))
273 return -1;
274
275 return count;
276 }
277
278 case UNION_TYPE:
279 case QUAL_UNION_TYPE:
280 {
281 /* These aren't very interesting except in a degenerate case. */
282 int count = 0;
283 int sub_count;
284 tree field;
285
286 /* Can't handle incomplete types nor sizes that are not
287 fixed. */
288 if (!COMPLETE_TYPE_P (type)
289 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
290 return -1;
291
292 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
293 {
294 if (TREE_CODE (field) != FIELD_DECL)
295 continue;
296
297 sub_count = rs6000_aggregate_candidate (TREE_TYPE (field), modep,
298 empty_base_seen,
299 zero_width_bf_seen);
300 if (sub_count < 0)
301 return -1;
302 count = count > sub_count ? count : sub_count;
303 }
304
305 /* There must be no padding. */
306 if (wi::to_wide (TYPE_SIZE (type))
307 != count * GET_MODE_BITSIZE (*modep))
308 return -1;
309
310 return count;
311 }
312
313 default:
314 break;
315 }
316
317 return -1;
318 }
319
320 /* If an argument, whose type is described by TYPE and MODE, is a homogeneous
321 float or vector aggregate that shall be passed in FP/vector registers
322 according to the ELFv2 ABI, return the homogeneous element mode in
323 *ELT_MODE and the number of elements in *N_ELTS, and return TRUE.
324
325 Otherwise, set *ELT_MODE to MODE and *N_ELTS to 1, and return FALSE. */
326
327 bool
328 rs6000_discover_homogeneous_aggregate (machine_mode mode, const_tree type,
329 machine_mode *elt_mode,
330 int *n_elts)
331 {
332 /* Note that we do not accept complex types at the top level as
333 homogeneous aggregates; these types are handled via the
334 targetm.calls.split_complex_arg mechanism. Complex types
335 can be elements of homogeneous aggregates, however. */
336 if (TARGET_HARD_FLOAT && DEFAULT_ABI == ABI_ELFv2 && type
337 && AGGREGATE_TYPE_P (type))
338 {
339 machine_mode field_mode = VOIDmode;
340 int empty_base_seen = 0;
341 int zero_width_bf_seen = 0;
342 int field_count = rs6000_aggregate_candidate (type, &field_mode,
343 &empty_base_seen,
344 &zero_width_bf_seen);
345
346 if (field_count > 0)
347 {
348 int reg_size = ALTIVEC_OR_VSX_VECTOR_MODE (field_mode) ? 16 : 8;
349 int field_size = ROUND_UP (GET_MODE_SIZE (field_mode), reg_size);
350
351 /* The ELFv2 ABI allows homogeneous aggregates to occupy
352 up to AGGR_ARG_NUM_REG registers. */
353 if (field_count * field_size <= AGGR_ARG_NUM_REG * reg_size)
354 {
355 if (elt_mode)
356 *elt_mode = field_mode;
357 if (n_elts)
358 *n_elts = field_count;
359 if (empty_base_seen && warn_psabi)
360 {
361 static unsigned last_reported_type_uid;
362 unsigned uid = TYPE_UID (TYPE_MAIN_VARIANT (type));
363 if (uid != last_reported_type_uid)
364 {
365 const char *url
366 = CHANGES_ROOT_URL "gcc-10/changes.html#empty_base";
367 if (empty_base_seen & 1)
368 inform (input_location,
369 "parameter passing for argument of type %qT "
370 "when C++17 is enabled changed to match C++14 "
371 "%{in GCC 10.1%}", type, url);
372 else
373 inform (input_location,
374 "parameter passing for argument of type %qT "
375 "with %<[[no_unique_address]]%> members "
376 "changed %{in GCC 10.1%}", type, url);
377 last_reported_type_uid = uid;
378 }
379 }
380 if (zero_width_bf_seen && warn_psabi)
381 {
382 static unsigned last_reported_type_uid;
383 unsigned uid = TYPE_UID (TYPE_MAIN_VARIANT (type));
384 if (uid != last_reported_type_uid)
385 {
386 inform (input_location,
387 "ELFv2 parameter passing for an argument "
388 "containing zero-width bit fields but that is "
389 "otherwise a homogeneous aggregate was "
390 "corrected in GCC 12");
391 last_reported_type_uid = uid;
392 }
393 if (elt_mode)
394 *elt_mode = mode;
395 if (n_elts)
396 *n_elts = 1;
397 return false;
398 }
399 return true;
400 }
401 }
402 }
403
404 if (elt_mode)
405 *elt_mode = mode;
406 if (n_elts)
407 *n_elts = 1;
408 return false;
409 }
410
411 /* Return a nonzero value to say to return the function value in
412 memory, just as large structures are always returned. TYPE will be
413 the data type of the value, and FNTYPE will be the type of the
414 function doing the returning, or @code{NULL} for libcalls.
415
416 The AIX ABI for the RS/6000 specifies that all structures are
417 returned in memory. The Darwin ABI does the same.
418
419 For the Darwin 64 Bit ABI, a function result can be returned in
420 registers or in memory, depending on the size of the return data
421 type. If it is returned in registers, the value occupies the same
422 registers as it would if it were the first and only function
423 argument. Otherwise, the function places its result in memory at
424 the location pointed to by GPR3.
425
426 The SVR4 ABI specifies that structures <= 8 bytes are returned in r3/r4,
427 but a draft put them in memory, and GCC used to implement the draft
428 instead of the final standard. Therefore, aix_struct_return
429 controls this instead of DEFAULT_ABI; V.4 targets needing backward
430 compatibility can change DRAFT_V4_STRUCT_RET to override the
431 default, and -m switches get the final word. See
432 rs6000_option_override_internal for more details.
433
434 The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
435 long double support is enabled. These values are returned in memory.
436
437 int_size_in_bytes returns -1 for variable size objects, which go in
438 memory always. The cast to unsigned makes -1 > 8. */
439
440 bool
441 rs6000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
442 {
443 /* We do not allow MMA types being used as return values. Only report
444 the invalid return value usage the first time we encounter it. */
445 if (cfun
446 && !cfun->machine->mma_return_type_error
447 && TREE_TYPE (cfun->decl) == fntype
448 && (TYPE_MODE (type) == OOmode || TYPE_MODE (type) == XOmode))
449 {
450 /* Record we have now handled function CFUN, so the next time we
451 are called, we do not re-report the same error. */
452 cfun->machine->mma_return_type_error = true;
453 if (TYPE_CANONICAL (type) != NULL_TREE)
454 type = TYPE_CANONICAL (type);
455 error ("invalid use of MMA type %qs as a function return value",
456 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
457 }
458
459 /* For the Darwin64 ABI, test if we can fit the return value in regs. */
460 if (TARGET_MACHO
461 && rs6000_darwin64_abi
462 && TREE_CODE (type) == RECORD_TYPE
463 && int_size_in_bytes (type) > 0)
464 {
465 CUMULATIVE_ARGS valcum;
466 rtx valret;
467
468 valcum.words = 0;
469 valcum.fregno = FP_ARG_MIN_REG;
470 valcum.vregno = ALTIVEC_ARG_MIN_REG;
471 /* Do a trial code generation as if this were going to be passed
472 as an argument; if any part goes in memory, we return NULL. */
473 valret = rs6000_darwin64_record_arg (&valcum, type, true, true);
474 if (valret)
475 return false;
476 /* Otherwise fall through to more conventional ABI rules. */
477 }
478
479 /* The ELFv2 ABI returns homogeneous VFP aggregates in registers */
480 if (rs6000_discover_homogeneous_aggregate (TYPE_MODE (type), type,
481 NULL, NULL))
482 return false;
483
484 /* The ELFv2 ABI returns aggregates up to 16B in registers */
485 if (DEFAULT_ABI == ABI_ELFv2 && AGGREGATE_TYPE_P (type)
486 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) <= 16)
487 return false;
488
489 if (AGGREGATE_TYPE_P (type)
490 && (aix_struct_return
491 || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
492 return true;
493
494 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
495 modes only exist for GCC vector types if -maltivec. */
496 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI
497 && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
498 return false;
499
500 /* Return synthetic vectors in memory. */
501 if (TREE_CODE (type) == VECTOR_TYPE
502 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
503 {
504 static bool warned_for_return_big_vectors = false;
505 if (!warned_for_return_big_vectors)
506 {
507 warning (OPT_Wpsabi, "GCC vector returned by reference: "
508 "non-standard ABI extension with no compatibility "
509 "guarantee");
510 warned_for_return_big_vectors = true;
511 }
512 return true;
513 }
514
515 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD
516 && FLOAT128_IEEE_P (TYPE_MODE (type)))
517 return true;
518
519 return false;
520 }
521
522 /* Specify whether values returned in registers should be at the most
523 significant end of a register. We want aggregates returned by
524 value to match the way aggregates are passed to functions. */
525
526 bool
527 rs6000_return_in_msb (const_tree valtype)
528 {
529 return (DEFAULT_ABI == ABI_ELFv2
530 && BYTES_BIG_ENDIAN
531 && AGGREGATE_TYPE_P (valtype)
532 && (rs6000_function_arg_padding (TYPE_MODE (valtype), valtype)
533 == PAD_UPWARD));
534 }
535
536 #ifdef HAVE_AS_GNU_ATTRIBUTE
537 /* Return TRUE if a call to function FNDECL may be one that
538 potentially affects the function calling ABI of the object file. */
539
540 static bool
541 call_ABI_of_interest (tree fndecl)
542 {
543 if (rs6000_gnu_attr && symtab->state == EXPANSION)
544 {
545 struct cgraph_node *c_node;
546
547 /* Libcalls are always interesting. */
548 if (fndecl == NULL_TREE)
549 return true;
550
551 /* Any call to an external function is interesting. */
552 if (DECL_EXTERNAL (fndecl))
553 return true;
554
555 /* Interesting functions that we are emitting in this object file. */
556 c_node = cgraph_node::get (fndecl);
557 c_node = c_node->ultimate_alias_target ();
558 return !c_node->only_called_directly_p ();
559 }
560 return false;
561 }
562 #endif
563
564 /* Initialize a variable CUM of type CUMULATIVE_ARGS
565 for a call to a function whose data type is FNTYPE.
566 For a library call, FNTYPE is 0 and RETURN_MODE the return value mode.
567
568 For incoming args we set the number of arguments in the prototype large
569 so we never return a PARALLEL. */
570
571 void
572 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
573 rtx libname ATTRIBUTE_UNUSED, int incoming,
574 int libcall, int n_named_args,
575 tree fndecl,
576 machine_mode return_mode ATTRIBUTE_UNUSED)
577 {
578 static CUMULATIVE_ARGS zero_cumulative;
579
580 *cum = zero_cumulative;
581 cum->words = 0;
582 cum->fregno = FP_ARG_MIN_REG;
583 cum->vregno = ALTIVEC_ARG_MIN_REG;
584 cum->prototype = (fntype && prototype_p (fntype));
585 cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
586 ? CALL_LIBCALL : CALL_NORMAL);
587 cum->sysv_gregno = GP_ARG_MIN_REG;
588 cum->stdarg = stdarg_p (fntype);
589 cum->libcall = libcall;
590
591 cum->nargs_prototype = 0;
592 if (incoming || cum->prototype)
593 cum->nargs_prototype = n_named_args;
594
595 /* Check for a longcall attribute. */
596 if ((!fntype && rs6000_default_long_calls)
597 || (fntype
598 && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
599 && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype))))
600 cum->call_cookie |= CALL_LONG;
601 else if (DEFAULT_ABI != ABI_DARWIN)
602 {
603 bool is_local = (fndecl
604 && !DECL_EXTERNAL (fndecl)
605 && !DECL_WEAK (fndecl)
606 && (*targetm.binds_local_p) (fndecl));
607 if (is_local)
608 ;
609 else if (flag_plt)
610 {
611 if (fntype
612 && lookup_attribute ("noplt", TYPE_ATTRIBUTES (fntype)))
613 cum->call_cookie |= CALL_LONG;
614 }
615 else
616 {
617 if (!(fntype
618 && lookup_attribute ("plt", TYPE_ATTRIBUTES (fntype))))
619 cum->call_cookie |= CALL_LONG;
620 }
621 }
622
623 if (TARGET_DEBUG_ARG)
624 {
625 fprintf (stderr, "\ninit_cumulative_args:");
626 if (fntype)
627 {
628 tree ret_type = TREE_TYPE (fntype);
629 fprintf (stderr, " ret code = %s,",
630 get_tree_code_name (TREE_CODE (ret_type)));
631 }
632
633 if (cum->call_cookie & CALL_LONG)
634 fprintf (stderr, " longcall,");
635
636 fprintf (stderr, " proto = %d, nargs = %d\n",
637 cum->prototype, cum->nargs_prototype);
638 }
639
640 #ifdef HAVE_AS_GNU_ATTRIBUTE
641 if (TARGET_ELF && (TARGET_64BIT || DEFAULT_ABI == ABI_V4))
642 {
643 cum->escapes = call_ABI_of_interest (fndecl);
644 if (cum->escapes)
645 {
646 tree return_type;
647
648 if (fntype)
649 {
650 return_type = TREE_TYPE (fntype);
651 return_mode = TYPE_MODE (return_type);
652 }
653 else
654 return_type = lang_hooks.types.type_for_mode (return_mode, 0);
655
656 if (return_type != NULL)
657 {
658 if (TREE_CODE (return_type) == RECORD_TYPE
659 && TYPE_TRANSPARENT_AGGR (return_type))
660 {
661 return_type = TREE_TYPE (first_field (return_type));
662 return_mode = TYPE_MODE (return_type);
663 }
664 if (AGGREGATE_TYPE_P (return_type)
665 && ((unsigned HOST_WIDE_INT) int_size_in_bytes (return_type)
666 <= 8))
667 rs6000_returns_struct = true;
668 }
669 if (SCALAR_FLOAT_MODE_P (return_mode))
670 {
671 rs6000_passes_float = true;
672 if ((HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE || TARGET_64BIT)
673 && (FLOAT128_IBM_P (return_mode)
674 || FLOAT128_IEEE_P (return_mode)
675 || (return_type != NULL
676 && (TYPE_MAIN_VARIANT (return_type)
677 == long_double_type_node))))
678 rs6000_passes_long_double = true;
679 }
680 if (ALTIVEC_OR_VSX_VECTOR_MODE (return_mode))
681 rs6000_passes_vector = true;
682 }
683 }
684 #endif
685
686 if (fntype
687 && !TARGET_ALTIVEC
688 && TARGET_ALTIVEC_ABI
689 && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
690 {
691 error ("cannot return value in vector register because"
692 " altivec instructions are disabled, use %qs"
693 " to enable them", "-maltivec");
694 }
695 }
696
697
699 /* On rs6000, function arguments are promoted, as are function return
700 values. */
701
702 machine_mode
703 rs6000_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
704 machine_mode mode,
705 int *punsignedp ATTRIBUTE_UNUSED,
706 const_tree, int for_return ATTRIBUTE_UNUSED)
707 {
708 if (GET_MODE_CLASS (mode) == MODE_INT
709 && GET_MODE_SIZE (mode) < (TARGET_32BIT ? 4 : 8))
710 mode = TARGET_32BIT ? SImode : DImode;
711
712 return mode;
713 }
714
715 /* Return true if TYPE must be passed on the stack and not in registers. */
716
717 bool
718 rs6000_must_pass_in_stack (const function_arg_info &arg)
719 {
720 if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2 || TARGET_64BIT)
721 return must_pass_in_stack_var_size (arg);
722 else
723 return must_pass_in_stack_var_size_or_pad (arg);
724 }
725
726 static inline bool
727 is_complex_IBM_long_double (machine_mode mode)
728 {
729 return mode == ICmode || (mode == TCmode && FLOAT128_IBM_P (TCmode));
730 }
731
732 /* Whether ABI_V4 passes MODE args to a function in floating point
733 registers. */
734
735 static bool
736 abi_v4_pass_in_fpr (machine_mode mode, bool named)
737 {
738 if (!TARGET_HARD_FLOAT)
739 return false;
740 if (mode == DFmode)
741 return true;
742 if (mode == SFmode && named)
743 return true;
744 /* ABI_V4 passes complex IBM long double in 8 gprs.
745 Stupid, but we can't change the ABI now. */
746 if (is_complex_IBM_long_double (mode))
747 return false;
748 if (FLOAT128_2REG_P (mode))
749 return true;
750 if (DECIMAL_FLOAT_MODE_P (mode))
751 return true;
752 return false;
753 }
754
755 /* Implement TARGET_FUNCTION_ARG_PADDING.
756
757 For the AIX ABI structs are always stored left shifted in their
758 argument slot. */
759
760 pad_direction
761 rs6000_function_arg_padding (machine_mode mode, const_tree type)
762 {
763 #ifndef AGGREGATE_PADDING_FIXED
764 #define AGGREGATE_PADDING_FIXED 0
765 #endif
766 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
767 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
768 #endif
769
770 if (!AGGREGATE_PADDING_FIXED)
771 {
772 /* GCC used to pass structures of the same size as integer types as
773 if they were in fact integers, ignoring TARGET_FUNCTION_ARG_PADDING.
774 i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
775 passed padded downward, except that -mstrict-align further
776 muddied the water in that multi-component structures of 2 and 4
777 bytes in size were passed padded upward.
778
779 The following arranges for best compatibility with previous
780 versions of gcc, but removes the -mstrict-align dependency. */
781 if (BYTES_BIG_ENDIAN)
782 {
783 HOST_WIDE_INT size = 0;
784
785 if (mode == BLKmode)
786 {
787 if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
788 size = int_size_in_bytes (type);
789 }
790 else
791 size = GET_MODE_SIZE (mode);
792
793 if (size == 1 || size == 2 || size == 4)
794 return PAD_DOWNWARD;
795 }
796 return PAD_UPWARD;
797 }
798
799 if (AGGREGATES_PAD_UPWARD_ALWAYS)
800 {
801 if (type != 0 && AGGREGATE_TYPE_P (type))
802 return PAD_UPWARD;
803 }
804
805 /* Fall back to the default. */
806 return default_function_arg_padding (mode, type);
807 }
808
809 /* If defined, a C expression that gives the alignment boundary, in bits,
810 of an argument with the specified mode and type. If it is not defined,
811 PARM_BOUNDARY is used for all arguments.
812
813 V.4 wants long longs and doubles to be double word aligned. Just
814 testing the mode size is a boneheaded way to do this as it means
815 that other types such as complex int are also double word aligned.
816 However, we're stuck with this because changing the ABI might break
817 existing library interfaces.
818
819 Quadword align Altivec/VSX vectors.
820 Quadword align large synthetic vector types. */
821
822 unsigned int
823 rs6000_function_arg_boundary (machine_mode mode, const_tree type)
824 {
825 machine_mode elt_mode;
826 int n_elts;
827
828 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
829
830 if (DEFAULT_ABI == ABI_V4
831 && (GET_MODE_SIZE (mode) == 8
832 || (TARGET_HARD_FLOAT
833 && !is_complex_IBM_long_double (mode)
834 && FLOAT128_2REG_P (mode))))
835 return 64;
836 else if (FLOAT128_VECTOR_P (mode))
837 return 128;
838 else if (type && TREE_CODE (type) == VECTOR_TYPE
839 && int_size_in_bytes (type) >= 8
840 && int_size_in_bytes (type) < 16)
841 return 64;
842 else if (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
843 || (type && TREE_CODE (type) == VECTOR_TYPE
844 && int_size_in_bytes (type) >= 16))
845 return 128;
846
847 /* Aggregate types that need > 8 byte alignment are quadword-aligned
848 in the parameter area in the ELFv2 ABI, and in the AIX ABI unless
849 -mcompat-align-parm is used. */
850 if (((DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm)
851 || DEFAULT_ABI == ABI_ELFv2)
852 && type && TYPE_ALIGN (type) > 64)
853 {
854 /* "Aggregate" means any AGGREGATE_TYPE except for single-element
855 or homogeneous float/vector aggregates here. We already handled
856 vector aggregates above, but still need to check for float here. */
857 if (AGGREGATE_TYPE_P (type)
858 && !SCALAR_FLOAT_MODE_P (elt_mode))
859 return 128;
860 }
861
862 /* Similar for the Darwin64 ABI. Note that for historical reasons we
863 implement the "aggregate type" check as a BLKmode check here; this
864 means certain aggregate types are in fact not aligned. */
865 if (TARGET_MACHO && rs6000_darwin64_abi
866 && mode == BLKmode
867 && type && TYPE_ALIGN (type) > 64)
868 return 128;
869
870 return PARM_BOUNDARY;
871 }
872
873 /* The offset in words to the start of the parameter save area. */
874
875 static unsigned int
876 rs6000_parm_offset (void)
877 {
878 return (DEFAULT_ABI == ABI_V4 ? 2
879 : DEFAULT_ABI == ABI_ELFv2 ? 4
880 : 6);
881 }
882
883 /* For a function parm of MODE and TYPE, return the starting word in
884 the parameter area. NWORDS of the parameter area are already used. */
885
886 static unsigned int
887 rs6000_parm_start (machine_mode mode, const_tree type,
888 unsigned int nwords)
889 {
890 unsigned int align;
891
892 align = rs6000_function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
893 return nwords + (-(rs6000_parm_offset () + nwords) & align);
894 }
895
896 /* Compute the size (in words) of a function argument. */
897
898 static unsigned long
899 rs6000_arg_size (machine_mode mode, const_tree type)
900 {
901 unsigned long size;
902
903 if (mode != BLKmode)
904 size = GET_MODE_SIZE (mode);
905 else
906 size = int_size_in_bytes (type);
907
908 if (TARGET_32BIT)
909 return (size + 3) >> 2;
910 else
911 return (size + 7) >> 3;
912 }
913
914 /* Use this to flush pending int fields. */
916
917 static void
918 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
919 HOST_WIDE_INT bitpos, int final)
920 {
921 unsigned int startbit, endbit;
922 int intregs, intoffset;
923
924 /* Handle the situations where a float is taking up the first half
925 of the GPR, and the other half is empty (typically due to
926 alignment restrictions). We can detect this by a 8-byte-aligned
927 int field, or by seeing that this is the final flush for this
928 argument. Count the word and continue on. */
929 if (cum->floats_in_gpr == 1
930 && (cum->intoffset % 64 == 0
931 || (cum->intoffset == -1 && final)))
932 {
933 cum->words++;
934 cum->floats_in_gpr = 0;
935 }
936
937 if (cum->intoffset == -1)
938 return;
939
940 intoffset = cum->intoffset;
941 cum->intoffset = -1;
942 cum->floats_in_gpr = 0;
943
944 if (intoffset % BITS_PER_WORD != 0)
945 {
946 unsigned int bits = BITS_PER_WORD - intoffset % BITS_PER_WORD;
947 if (!int_mode_for_size (bits, 0).exists ())
948 {
949 /* We couldn't find an appropriate mode, which happens,
950 e.g., in packed structs when there are 3 bytes to load.
951 Back intoffset back to the beginning of the word in this
952 case. */
953 intoffset = ROUND_DOWN (intoffset, BITS_PER_WORD);
954 }
955 }
956
957 startbit = ROUND_DOWN (intoffset, BITS_PER_WORD);
958 endbit = ROUND_UP (bitpos, BITS_PER_WORD);
959 intregs = (endbit - startbit) / BITS_PER_WORD;
960 cum->words += intregs;
961 /* words should be unsigned. */
962 if ((unsigned)cum->words < (endbit/BITS_PER_WORD))
963 {
964 int pad = (endbit/BITS_PER_WORD) - cum->words;
965 cum->words += pad;
966 }
967 }
968
969 /* The darwin64 ABI calls for us to recurse down through structs,
970 looking for elements passed in registers. Unfortunately, we have
971 to track int register count here also because of misalignments
972 in powerpc alignment mode. */
973
974 static void
975 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
976 const_tree type,
977 HOST_WIDE_INT startbitpos)
978 {
979 tree f;
980
981 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
982 if (TREE_CODE (f) == FIELD_DECL)
983 {
984 HOST_WIDE_INT bitpos = startbitpos;
985 tree ftype = TREE_TYPE (f);
986 machine_mode mode;
987 if (ftype == error_mark_node)
988 continue;
989 mode = TYPE_MODE (ftype);
990
991 if (DECL_SIZE (f) != 0
992 && tree_fits_uhwi_p (bit_position (f)))
993 bitpos += int_bit_position (f);
994
995 /* ??? FIXME: else assume zero offset. */
996
997 if (TREE_CODE (ftype) == RECORD_TYPE)
998 rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
999 else if (USE_FP_FOR_ARG_P (cum, mode))
1000 {
1001 unsigned n_fpregs = (GET_MODE_SIZE (mode) + 7) >> 3;
1002 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
1003 cum->fregno += n_fpregs;
1004 /* Single-precision floats present a special problem for
1005 us, because they are smaller than an 8-byte GPR, and so
1006 the structure-packing rules combined with the standard
1007 varargs behavior mean that we want to pack float/float
1008 and float/int combinations into a single register's
1009 space. This is complicated by the arg advance flushing,
1010 which works on arbitrarily large groups of int-type
1011 fields. */
1012 if (mode == SFmode)
1013 {
1014 if (cum->floats_in_gpr == 1)
1015 {
1016 /* Two floats in a word; count the word and reset
1017 the float count. */
1018 cum->words++;
1019 cum->floats_in_gpr = 0;
1020 }
1021 else if (bitpos % 64 == 0)
1022 {
1023 /* A float at the beginning of an 8-byte word;
1024 count it and put off adjusting cum->words until
1025 we see if a arg advance flush is going to do it
1026 for us. */
1027 cum->floats_in_gpr++;
1028 }
1029 else
1030 {
1031 /* The float is at the end of a word, preceded
1032 by integer fields, so the arg advance flush
1033 just above has already set cum->words and
1034 everything is taken care of. */
1035 }
1036 }
1037 else
1038 cum->words += n_fpregs;
1039 }
1040 else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
1041 {
1042 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
1043 cum->vregno++;
1044 cum->words += 2;
1045 }
1046 else if (cum->intoffset == -1)
1047 cum->intoffset = bitpos;
1048 }
1049 }
1050
1051 /* Check for an item that needs to be considered specially under the darwin 64
1052 bit ABI. These are record types where the mode is BLK or the structure is
1053 8 bytes in size. */
1054 int
1055 rs6000_darwin64_struct_check_p (machine_mode mode, const_tree type)
1056 {
1057 return rs6000_darwin64_abi
1058 && ((mode == BLKmode
1059 && TREE_CODE (type) == RECORD_TYPE
1060 && int_size_in_bytes (type) > 0)
1061 || (type && TREE_CODE (type) == RECORD_TYPE
1062 && int_size_in_bytes (type) == 8)) ? 1 : 0;
1063 }
1064
1065 /* Update the data in CUM to advance over an argument
1066 of mode MODE and data type TYPE.
1067 (TYPE is null for libcalls where that information may not be available.)
1068
1069 Note that for args passed by reference, function_arg will be called
1070 with MODE and TYPE set to that of the pointer to the arg, not the arg
1071 itself. */
1072
1073 static void
1074 rs6000_function_arg_advance_1 (CUMULATIVE_ARGS *cum, machine_mode mode,
1075 const_tree type, bool named, int depth)
1076 {
1077 machine_mode elt_mode;
1078 int n_elts;
1079
1080 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
1081
1082 /* Only tick off an argument if we're not recursing. */
1083 if (depth == 0)
1084 cum->nargs_prototype--;
1085
1086 #ifdef HAVE_AS_GNU_ATTRIBUTE
1087 if (TARGET_ELF && (TARGET_64BIT || DEFAULT_ABI == ABI_V4)
1088 && cum->escapes)
1089 {
1090 if (SCALAR_FLOAT_MODE_P (mode))
1091 {
1092 rs6000_passes_float = true;
1093 if ((HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE || TARGET_64BIT)
1094 && (FLOAT128_IBM_P (mode)
1095 || FLOAT128_IEEE_P (mode)
1096 || (type != NULL
1097 && TYPE_MAIN_VARIANT (type) == long_double_type_node)))
1098 rs6000_passes_long_double = true;
1099 }
1100 if (named && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
1101 rs6000_passes_vector = true;
1102 }
1103 #endif
1104
1105 if (TARGET_ALTIVEC_ABI
1106 && (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
1107 || (type && TREE_CODE (type) == VECTOR_TYPE
1108 && int_size_in_bytes (type) == 16)))
1109 {
1110 bool stack = false;
1111
1112 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
1113 {
1114 cum->vregno += n_elts;
1115
1116 /* If we are not splitting Complex IEEE128 args then account for the
1117 fact that they are passed in 2 VSX regs. */
1118 if (!targetm.calls.split_complex_arg && type
1119 && TREE_CODE (type) == COMPLEX_TYPE && elt_mode == KCmode)
1120 cum->vregno++;
1121
1122 if (!TARGET_ALTIVEC)
1123 error ("cannot pass argument in vector register because"
1124 " altivec instructions are disabled, use %qs"
1125 " to enable them", "-maltivec");
1126
1127 /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
1128 even if it is going to be passed in a vector register.
1129 Darwin does the same for variable-argument functions. */
1130 if (((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
1131 && TARGET_64BIT)
1132 || (cum->stdarg && DEFAULT_ABI != ABI_V4))
1133 stack = true;
1134 }
1135 else
1136 stack = true;
1137
1138 if (stack)
1139 {
1140 int align;
1141
1142 /* Vector parameters must be 16-byte aligned. In 32-bit
1143 mode this means we need to take into account the offset
1144 to the parameter save area. In 64-bit mode, they just
1145 have to start on an even word, since the parameter save
1146 area is 16-byte aligned. */
1147 if (TARGET_32BIT)
1148 align = -(rs6000_parm_offset () + cum->words) & 3;
1149 else
1150 align = cum->words & 1;
1151 cum->words += align + rs6000_arg_size (mode, type);
1152
1153 if (TARGET_DEBUG_ARG)
1154 {
1155 fprintf (stderr, "function_adv: words = %2d, align=%d, ",
1156 cum->words, align);
1157 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
1158 cum->nargs_prototype, cum->prototype,
1159 GET_MODE_NAME (mode));
1160 }
1161 }
1162 }
1163 else if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
1164 {
1165 int size = int_size_in_bytes (type);
1166 /* Variable sized types have size == -1 and are
1167 treated as if consisting entirely of ints.
1168 Pad to 16 byte boundary if needed. */
1169 if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
1170 && (cum->words % 2) != 0)
1171 cum->words++;
1172 /* For varargs, we can just go up by the size of the struct. */
1173 if (!named)
1174 cum->words += (size + 7) / 8;
1175 else
1176 {
1177 /* It is tempting to say int register count just goes up by
1178 sizeof(type)/8, but this is wrong in a case such as
1179 { int; double; int; } [powerpc alignment]. We have to
1180 grovel through the fields for these too. */
1181 cum->intoffset = 0;
1182 cum->floats_in_gpr = 0;
1183 rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
1184 rs6000_darwin64_record_arg_advance_flush (cum,
1185 size * BITS_PER_UNIT, 1);
1186 }
1187 if (TARGET_DEBUG_ARG)
1188 {
1189 fprintf (stderr, "function_adv: words = %2d, align=%d, size=%d",
1190 cum->words, TYPE_ALIGN (type), size);
1191 fprintf (stderr,
1192 "nargs = %4d, proto = %d, mode = %4s (darwin64 abi)\n",
1193 cum->nargs_prototype, cum->prototype,
1194 GET_MODE_NAME (mode));
1195 }
1196 }
1197 else if (DEFAULT_ABI == ABI_V4)
1198 {
1199 if (abi_v4_pass_in_fpr (mode, named))
1200 {
1201 /* _Decimal128 must use an even/odd register pair. This assumes
1202 that the register number is odd when fregno is odd. */
1203 if (mode == TDmode && (cum->fregno % 2) == 1)
1204 cum->fregno++;
1205
1206 if (cum->fregno + (FLOAT128_2REG_P (mode) ? 1 : 0)
1207 <= FP_ARG_V4_MAX_REG)
1208 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
1209 else
1210 {
1211 cum->fregno = FP_ARG_V4_MAX_REG + 1;
1212 if (mode == DFmode || FLOAT128_IBM_P (mode)
1213 || mode == DDmode || mode == TDmode)
1214 cum->words += cum->words & 1;
1215 cum->words += rs6000_arg_size (mode, type);
1216 }
1217 }
1218 else
1219 {
1220 int n_words = rs6000_arg_size (mode, type);
1221 int gregno = cum->sysv_gregno;
1222
1223 /* Long long is put in (r3,r4), (r5,r6), (r7,r8) or (r9,r10).
1224 As does any other 2 word item such as complex int due to a
1225 historical mistake. */
1226 if (n_words == 2)
1227 gregno += (1 - gregno) & 1;
1228
1229 /* Multi-reg args are not split between registers and stack. */
1230 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
1231 {
1232 /* Long long is aligned on the stack. So are other 2 word
1233 items such as complex int due to a historical mistake. */
1234 if (n_words == 2)
1235 cum->words += cum->words & 1;
1236 cum->words += n_words;
1237 }
1238
1239 /* Note: continuing to accumulate gregno past when we've started
1240 spilling to the stack indicates the fact that we've started
1241 spilling to the stack to expand_builtin_saveregs. */
1242 cum->sysv_gregno = gregno + n_words;
1243 }
1244
1245 if (TARGET_DEBUG_ARG)
1246 {
1247 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
1248 cum->words, cum->fregno);
1249 fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
1250 cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
1251 fprintf (stderr, "mode = %4s, named = %d\n",
1252 GET_MODE_NAME (mode), named);
1253 }
1254 }
1255 else
1256 {
1257 int n_words = rs6000_arg_size (mode, type);
1258 int start_words = cum->words;
1259 int align_words = rs6000_parm_start (mode, type, start_words);
1260
1261 cum->words = align_words + n_words;
1262
1263 if (SCALAR_FLOAT_MODE_P (elt_mode) && TARGET_HARD_FLOAT)
1264 {
1265 /* _Decimal128 must be passed in an even/odd float register pair.
1266 This assumes that the register number is odd when fregno is
1267 odd. */
1268 if (elt_mode == TDmode && (cum->fregno % 2) == 1)
1269 cum->fregno++;
1270 cum->fregno += n_elts * ((GET_MODE_SIZE (elt_mode) + 7) >> 3);
1271 }
1272
1273 if (TARGET_DEBUG_ARG)
1274 {
1275 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
1276 cum->words, cum->fregno);
1277 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
1278 cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
1279 fprintf (stderr, "named = %d, align = %d, depth = %d\n",
1280 named, align_words - start_words, depth);
1281 }
1282 }
1283 }
1284
1285 void
1286 rs6000_function_arg_advance (cumulative_args_t cum,
1287 const function_arg_info &arg)
1288 {
1289 rs6000_function_arg_advance_1 (get_cumulative_args (cum),
1290 arg.mode, arg.type, arg.named, 0);
1291 }
1292
1293 /* A subroutine of rs6000_darwin64_record_arg. Assign the bits of the
1294 structure between cum->intoffset and bitpos to integer registers. */
1295
1296 static void
1297 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
1298 HOST_WIDE_INT bitpos, rtx rvec[], int *k)
1299 {
1300 machine_mode mode;
1301 unsigned int regno;
1302 unsigned int startbit, endbit;
1303 int this_regno, intregs, intoffset;
1304 rtx reg;
1305
1306 if (cum->intoffset == -1)
1307 return;
1308
1309 intoffset = cum->intoffset;
1310 cum->intoffset = -1;
1311
1312 /* If this is the trailing part of a word, try to only load that
1313 much into the register. Otherwise load the whole register. Note
1314 that in the latter case we may pick up unwanted bits. It's not a
1315 problem at the moment but may wish to revisit. */
1316
1317 if (intoffset % BITS_PER_WORD != 0)
1318 {
1319 unsigned int bits = BITS_PER_WORD - intoffset % BITS_PER_WORD;
1320 if (!int_mode_for_size (bits, 0).exists (&mode))
1321 {
1322 /* We couldn't find an appropriate mode, which happens,
1323 e.g., in packed structs when there are 3 bytes to load.
1324 Back intoffset back to the beginning of the word in this
1325 case. */
1326 intoffset = ROUND_DOWN (intoffset, BITS_PER_WORD);
1327 mode = word_mode;
1328 }
1329 }
1330 else
1331 mode = word_mode;
1332
1333 startbit = ROUND_DOWN (intoffset, BITS_PER_WORD);
1334 endbit = ROUND_UP (bitpos, BITS_PER_WORD);
1335 intregs = (endbit - startbit) / BITS_PER_WORD;
1336 this_regno = cum->words + intoffset / BITS_PER_WORD;
1337
1338 if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
1339 cum->use_stack = 1;
1340
1341 intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
1342 if (intregs <= 0)
1343 return;
1344
1345 intoffset /= BITS_PER_UNIT;
1346 do
1347 {
1348 regno = GP_ARG_MIN_REG + this_regno;
1349 reg = gen_rtx_REG (mode, regno);
1350 rvec[(*k)++] =
1351 gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
1352
1353 this_regno += 1;
1354 intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
1355 mode = word_mode;
1356 intregs -= 1;
1357 }
1358 while (intregs > 0);
1359 }
1360
1361 /* Recursive workhorse for the following. */
1362
1363 static void
1364 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
1365 HOST_WIDE_INT startbitpos, rtx rvec[],
1366 int *k)
1367 {
1368 tree f;
1369
1370 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
1371 if (TREE_CODE (f) == FIELD_DECL)
1372 {
1373 HOST_WIDE_INT bitpos = startbitpos;
1374 tree ftype = TREE_TYPE (f);
1375 machine_mode mode;
1376 if (ftype == error_mark_node)
1377 continue;
1378 mode = TYPE_MODE (ftype);
1379
1380 if (DECL_SIZE (f) != 0
1381 && tree_fits_uhwi_p (bit_position (f)))
1382 bitpos += int_bit_position (f);
1383
1384 /* ??? FIXME: else assume zero offset. */
1385
1386 if (TREE_CODE (ftype) == RECORD_TYPE)
1387 rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
1388 else if (cum->named && USE_FP_FOR_ARG_P (cum, mode))
1389 {
1390 unsigned n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
1391 #if 0
1392 switch (mode)
1393 {
1394 case E_SCmode: mode = SFmode; break;
1395 case E_DCmode: mode = DFmode; break;
1396 case E_TCmode: mode = TFmode; break;
1397 default: break;
1398 }
1399 #endif
1400 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
1401 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
1402 {
1403 gcc_assert (cum->fregno == FP_ARG_MAX_REG
1404 && (mode == TFmode || mode == TDmode));
1405 /* Long double or _Decimal128 split over regs and memory. */
1406 mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : DFmode;
1407 cum->use_stack=1;
1408 }
1409 rvec[(*k)++]
1410 = gen_rtx_EXPR_LIST (VOIDmode,
1411 gen_rtx_REG (mode, cum->fregno++),
1412 GEN_INT (bitpos / BITS_PER_UNIT));
1413 if (FLOAT128_2REG_P (mode))
1414 cum->fregno++;
1415 }
1416 else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
1417 {
1418 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
1419 rvec[(*k)++]
1420 = gen_rtx_EXPR_LIST (VOIDmode,
1421 gen_rtx_REG (mode, cum->vregno++),
1422 GEN_INT (bitpos / BITS_PER_UNIT));
1423 }
1424 else if (cum->intoffset == -1)
1425 cum->intoffset = bitpos;
1426 }
1427 }
1428
1429 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
1430 the register(s) to be used for each field and subfield of a struct
1431 being passed by value, along with the offset of where the
1432 register's value may be found in the block. FP fields go in FP
1433 register, vector fields go in vector registers, and everything
1434 else goes in int registers, packed as in memory.
1435
1436 This code is also used for function return values. RETVAL indicates
1437 whether this is the case.
1438
1439 Much of this is taken from the SPARC V9 port, which has a similar
1440 calling convention. */
1441
1442 rtx
1443 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
1444 bool named, bool retval)
1445 {
1446 rtx rvec[FIRST_PSEUDO_REGISTER];
1447 int k = 1, kbase = 1;
1448 HOST_WIDE_INT typesize = int_size_in_bytes (type);
1449 /* This is a copy; modifications are not visible to our caller. */
1450 CUMULATIVE_ARGS copy_cum = *orig_cum;
1451 CUMULATIVE_ARGS *cum = ©_cum;
1452
1453 /* Pad to 16 byte boundary if needed. */
1454 if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
1455 && (cum->words % 2) != 0)
1456 cum->words++;
1457
1458 cum->intoffset = 0;
1459 cum->use_stack = 0;
1460 cum->named = named;
1461
1462 /* Put entries into rvec[] for individual FP and vector fields, and
1463 for the chunks of memory that go in int regs. Note we start at
1464 element 1; 0 is reserved for an indication of using memory, and
1465 may or may not be filled in below. */
1466 rs6000_darwin64_record_arg_recurse (cum, type, /* startbit pos= */ 0, rvec, &k);
1467 rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
1468
1469 /* If any part of the struct went on the stack put all of it there.
1470 This hack is because the generic code for
1471 FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
1472 parts of the struct are not at the beginning. */
1473 if (cum->use_stack)
1474 {
1475 if (retval)
1476 return NULL_RTX; /* doesn't go in registers at all */
1477 kbase = 0;
1478 rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
1479 }
1480 if (k > 1 || cum->use_stack)
1481 return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
1482 else
1483 return NULL_RTX;
1484 }
1485
1486 /* Determine where to place an argument in 64-bit mode with 32-bit ABI. */
1487
1488 static rtx
1489 rs6000_mixed_function_arg (machine_mode mode, const_tree type,
1490 int align_words)
1491 {
1492 int n_units;
1493 int i, k;
1494 rtx rvec[GP_ARG_NUM_REG + 1];
1495
1496 if (align_words >= GP_ARG_NUM_REG)
1497 return NULL_RTX;
1498
1499 n_units = rs6000_arg_size (mode, type);
1500
1501 /* Optimize the simple case where the arg fits in one gpr, except in
1502 the case of BLKmode due to assign_parms assuming that registers are
1503 BITS_PER_WORD wide. */
1504 if (n_units == 0
1505 || (n_units == 1 && mode != BLKmode))
1506 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
1507
1508 k = 0;
1509 if (align_words + n_units > GP_ARG_NUM_REG)
1510 /* Not all of the arg fits in gprs. Say that it goes in memory too,
1511 using a magic NULL_RTX component.
1512 This is not strictly correct. Only some of the arg belongs in
1513 memory, not all of it. However, the normal scheme using
1514 function_arg_partial_nregs can result in unusual subregs, eg.
1515 (subreg:SI (reg:DF) 4), which are not handled well. The code to
1516 store the whole arg to memory is often more efficient than code
1517 to store pieces, and we know that space is available in the right
1518 place for the whole arg. */
1519 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
1520
1521 i = 0;
1522 do
1523 {
1524 rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
1525 rtx off = GEN_INT (i++ * 4);
1526 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
1527 }
1528 while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
1529
1530 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
1531 }
1532
1533 /* We have an argument of MODE and TYPE that goes into FPRs or VRs,
1534 but must also be copied into the parameter save area starting at
1535 offset ALIGN_WORDS. Fill in RVEC with the elements corresponding
1536 to the GPRs and/or memory. Return the number of elements used. */
1537
1538 static int
1539 rs6000_psave_function_arg (machine_mode mode, const_tree type,
1540 int align_words, rtx *rvec)
1541 {
1542 int k = 0;
1543
1544 if (align_words < GP_ARG_NUM_REG)
1545 {
1546 int n_words = rs6000_arg_size (mode, type);
1547
1548 if (align_words + n_words > GP_ARG_NUM_REG
1549 || mode == BLKmode
1550 || (TARGET_32BIT && TARGET_POWERPC64))
1551 {
1552 /* If this is partially on the stack, then we only
1553 include the portion actually in registers here. */
1554 machine_mode rmode = TARGET_32BIT ? SImode : DImode;
1555 int i = 0;
1556
1557 if (align_words + n_words > GP_ARG_NUM_REG)
1558 {
1559 /* Not all of the arg fits in gprs. Say that it goes in memory
1560 too, using a magic NULL_RTX component. Also see comment in
1561 rs6000_mixed_function_arg for why the normal
1562 function_arg_partial_nregs scheme doesn't work in this case. */
1563 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
1564 }
1565
1566 do
1567 {
1568 rtx r = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
1569 rtx off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
1570 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
1571 }
1572 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
1573 }
1574 else
1575 {
1576 /* The whole arg fits in gprs. */
1577 rtx r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
1578 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
1579 }
1580 }
1581 else
1582 {
1583 /* It's entirely in memory. */
1584 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
1585 }
1586
1587 return k;
1588 }
1589
1590 /* RVEC is a vector of K components of an argument of mode MODE.
1591 Construct the final function_arg return value from it. */
1592
1593 static rtx
1594 rs6000_finish_function_arg (machine_mode mode, rtx *rvec, int k)
1595 {
1596 gcc_assert (k >= 1);
1597
1598 /* Avoid returning a PARALLEL in the trivial cases. */
1599 if (k == 1)
1600 {
1601 if (XEXP (rvec[0], 0) == NULL_RTX)
1602 return NULL_RTX;
1603
1604 if (GET_MODE (XEXP (rvec[0], 0)) == mode)
1605 return XEXP (rvec[0], 0);
1606 }
1607
1608 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
1609 }
1610
1611 /* Determine where to put an argument to a function.
1612 Value is zero to push the argument on the stack,
1613 or a hard register in which to store the argument.
1614
1615 CUM is a variable of type CUMULATIVE_ARGS which gives info about
1616 the preceding args and about the function being called. It is
1617 not modified in this routine.
1618 ARG is a description of the argument.
1619
1620 On RS/6000 the first eight words of non-FP are normally in registers
1621 and the rest are pushed. Under AIX, the first 13 FP args are in registers.
1622 Under V.4, the first 8 FP args are in registers.
1623
1624 If this is floating-point and no prototype is specified, we use
1625 both an FP and integer register (or possibly FP reg and stack). Library
1626 functions (when CALL_LIBCALL is set) always have the proper types for args,
1627 so we can pass the FP value just in one register. emit_library_function
1628 doesn't support PARALLEL anyway.
1629
1630 Note that for args passed by reference, function_arg will be called
1631 with ARG describing the pointer to the arg, not the arg itself. */
1632
1633 rtx
1634 rs6000_function_arg (cumulative_args_t cum_v, const function_arg_info &arg)
1635 {
1636 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1637 tree type = arg.type;
1638 machine_mode mode = arg.mode;
1639 bool named = arg.named;
1640 enum rs6000_abi abi = DEFAULT_ABI;
1641 machine_mode elt_mode;
1642 int n_elts;
1643
1644 /* We do not allow MMA types being used as function arguments. */
1645 if (mode == OOmode || mode == XOmode)
1646 {
1647 if (TYPE_CANONICAL (type) != NULL_TREE)
1648 type = TYPE_CANONICAL (type);
1649 error ("invalid use of MMA operand of type %qs as a function parameter",
1650 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
1651 return NULL_RTX;
1652 }
1653
1654 /* Return a marker to indicate whether CR1 needs to set or clear the
1655 bit that V.4 uses to say fp args were passed in registers.
1656 Assume that we don't need the marker for software floating point,
1657 or compiler generated library calls. */
1658 if (arg.end_marker_p ())
1659 {
1660 if (abi == ABI_V4
1661 && (cum->call_cookie & CALL_LIBCALL) == 0
1662 && (cum->stdarg
1663 || (cum->nargs_prototype < 0
1664 && (cum->prototype || TARGET_NO_PROTOTYPE)))
1665 && TARGET_HARD_FLOAT)
1666 return GEN_INT (cum->call_cookie
1667 | ((cum->fregno == FP_ARG_MIN_REG)
1668 ? CALL_V4_SET_FP_ARGS
1669 : CALL_V4_CLEAR_FP_ARGS));
1670
1671 return GEN_INT (cum->call_cookie & ~CALL_LIBCALL);
1672 }
1673
1674 rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
1675
1676 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
1677 {
1678 rtx rslt = rs6000_darwin64_record_arg (cum, type, named, /*retval= */false);
1679 if (rslt != NULL_RTX)
1680 return rslt;
1681 /* Else fall through to usual handling. */
1682 }
1683
1684 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
1685 {
1686 rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
1687 rtx r, off;
1688 int i, k = 0;
1689
1690 /* Do we also need to pass this argument in the parameter save area?
1691 Library support functions for IEEE 128-bit are assumed to not need the
1692 value passed both in GPRs and in vector registers. */
1693 if (TARGET_64BIT && !cum->prototype
1694 && (!cum->libcall || !FLOAT128_VECTOR_P (elt_mode)))
1695 {
1696 int align_words = ROUND_UP (cum->words, 2);
1697 k = rs6000_psave_function_arg (mode, type, align_words, rvec);
1698 }
1699
1700 /* Describe where this argument goes in the vector registers. */
1701 for (i = 0; i < n_elts && cum->vregno + i <= ALTIVEC_ARG_MAX_REG; i++)
1702 {
1703 r = gen_rtx_REG (elt_mode, cum->vregno + i);
1704 off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
1705 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
1706 }
1707
1708 return rs6000_finish_function_arg (mode, rvec, k);
1709 }
1710 else if (TARGET_ALTIVEC_ABI
1711 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
1712 || (type && TREE_CODE (type) == VECTOR_TYPE
1713 && int_size_in_bytes (type) == 16)))
1714 {
1715 if (named || abi == ABI_V4)
1716 return NULL_RTX;
1717 else
1718 {
1719 /* Vector parameters to varargs functions under AIX or Darwin
1720 get passed in memory and possibly also in GPRs. */
1721 int align, align_words, n_words;
1722 machine_mode part_mode;
1723
1724 /* Vector parameters must be 16-byte aligned. In 32-bit
1725 mode this means we need to take into account the offset
1726 to the parameter save area. In 64-bit mode, they just
1727 have to start on an even word, since the parameter save
1728 area is 16-byte aligned. */
1729 if (TARGET_32BIT)
1730 align = -(rs6000_parm_offset () + cum->words) & 3;
1731 else
1732 align = cum->words & 1;
1733 align_words = cum->words + align;
1734
1735 /* Out of registers? Memory, then. */
1736 if (align_words >= GP_ARG_NUM_REG)
1737 return NULL_RTX;
1738
1739 if (TARGET_32BIT && TARGET_POWERPC64)
1740 return rs6000_mixed_function_arg (mode, type, align_words);
1741
1742 /* The vector value goes in GPRs. Only the part of the
1743 value in GPRs is reported here. */
1744 part_mode = mode;
1745 n_words = rs6000_arg_size (mode, type);
1746 if (align_words + n_words > GP_ARG_NUM_REG)
1747 /* Fortunately, there are only two possibilities, the value
1748 is either wholly in GPRs or half in GPRs and half not. */
1749 part_mode = DImode;
1750
1751 return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
1752 }
1753 }
1754
1755 else if (abi == ABI_V4)
1756 {
1757 if (abi_v4_pass_in_fpr (mode, named))
1758 {
1759 /* _Decimal128 must use an even/odd register pair. This assumes
1760 that the register number is odd when fregno is odd. */
1761 if (mode == TDmode && (cum->fregno % 2) == 1)
1762 cum->fregno++;
1763
1764 if (cum->fregno + (FLOAT128_2REG_P (mode) ? 1 : 0)
1765 <= FP_ARG_V4_MAX_REG)
1766 return gen_rtx_REG (mode, cum->fregno);
1767 else
1768 return NULL_RTX;
1769 }
1770 else
1771 {
1772 int n_words = rs6000_arg_size (mode, type);
1773 int gregno = cum->sysv_gregno;
1774
1775 /* Long long is put in (r3,r4), (r5,r6), (r7,r8) or (r9,r10).
1776 As does any other 2 word item such as complex int due to a
1777 historical mistake. */
1778 if (n_words == 2)
1779 gregno += (1 - gregno) & 1;
1780
1781 /* Multi-reg args are not split between registers and stack. */
1782 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
1783 return NULL_RTX;
1784
1785 if (TARGET_32BIT && TARGET_POWERPC64)
1786 return rs6000_mixed_function_arg (mode, type,
1787 gregno - GP_ARG_MIN_REG);
1788 return gen_rtx_REG (mode, gregno);
1789 }
1790 }
1791 else
1792 {
1793 int align_words = rs6000_parm_start (mode, type, cum->words);
1794
1795 /* _Decimal128 must be passed in an even/odd float register pair.
1796 This assumes that the register number is odd when fregno is odd. */
1797 if (elt_mode == TDmode && (cum->fregno % 2) == 1)
1798 cum->fregno++;
1799
1800 if (USE_FP_FOR_ARG_P (cum, elt_mode)
1801 && !(TARGET_AIX && !TARGET_ELF
1802 && type != NULL && AGGREGATE_TYPE_P (type)))
1803 {
1804 rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
1805 rtx r, off;
1806 int i, k = 0;
1807 unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
1808 int fpr_words;
1809
1810 /* Do we also need to pass this argument in the parameter
1811 save area? */
1812 if (type && (cum->nargs_prototype <= 0
1813 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
1814 && TARGET_XL_COMPAT
1815 && align_words >= GP_ARG_NUM_REG)))
1816 k = rs6000_psave_function_arg (mode, type, align_words, rvec);
1817
1818 /* Describe where this argument goes in the fprs. */
1819 for (i = 0; i < n_elts
1820 && cum->fregno + i * n_fpreg <= FP_ARG_MAX_REG; i++)
1821 {
1822 /* Check if the argument is split over registers and memory.
1823 This can only ever happen for long double or _Decimal128;
1824 complex types are handled via split_complex_arg. */
1825 machine_mode fmode = elt_mode;
1826 if (cum->fregno + (i + 1) * n_fpreg > FP_ARG_MAX_REG + 1)
1827 {
1828 gcc_assert (FLOAT128_2REG_P (fmode));
1829 fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
1830 }
1831
1832 r = gen_rtx_REG (fmode, cum->fregno + i * n_fpreg);
1833 off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
1834 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
1835 }
1836
1837 /* If there were not enough FPRs to hold the argument, the rest
1838 usually goes into memory. However, if the current position
1839 is still within the register parameter area, a portion may
1840 actually have to go into GPRs.
1841
1842 Note that it may happen that the portion of the argument
1843 passed in the first "half" of the first GPR was already
1844 passed in the last FPR as well.
1845
1846 For unnamed arguments, we already set up GPRs to cover the
1847 whole argument in rs6000_psave_function_arg, so there is
1848 nothing further to do at this point. */
1849 fpr_words = (i * GET_MODE_SIZE (elt_mode)) / (TARGET_32BIT ? 4 : 8);
1850 if (i < n_elts && align_words + fpr_words < GP_ARG_NUM_REG
1851 && cum->nargs_prototype > 0)
1852 {
1853 machine_mode rmode = TARGET_32BIT ? SImode : DImode;
1854 int n_words = rs6000_arg_size (mode, type);
1855
1856 align_words += fpr_words;
1857 n_words -= fpr_words;
1858
1859 do
1860 {
1861 r = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
1862 off = GEN_INT (fpr_words++ * GET_MODE_SIZE (rmode));
1863 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
1864 }
1865 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
1866 }
1867
1868 return rs6000_finish_function_arg (mode, rvec, k);
1869 }
1870 else if (align_words < GP_ARG_NUM_REG)
1871 {
1872 if (TARGET_32BIT && TARGET_POWERPC64)
1873 return rs6000_mixed_function_arg (mode, type, align_words);
1874
1875 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
1876 }
1877 else
1878 return NULL_RTX;
1879 }
1880 }
1881
1882 /* For an arg passed partly in registers and partly in memory, this is
1884 the number of bytes passed in registers. For args passed entirely in
1885 registers or entirely in memory, zero. When an arg is described by a
1886 PARALLEL, perhaps using more than one register type, this function
1887 returns the number of bytes used by the first element of the PARALLEL. */
1888
1889 int
1890 rs6000_arg_partial_bytes (cumulative_args_t cum_v,
1891 const function_arg_info &arg)
1892 {
1893 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1894 bool passed_in_gprs = true;
1895 int ret = 0;
1896 int align_words;
1897 machine_mode elt_mode;
1898 int n_elts;
1899
1900 rs6000_discover_homogeneous_aggregate (arg.mode, arg.type,
1901 &elt_mode, &n_elts);
1902
1903 if (DEFAULT_ABI == ABI_V4)
1904 return 0;
1905
1906 if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, arg.named))
1907 {
1908 /* If we are passing this arg in the fixed parameter save area (gprs or
1909 memory) as well as VRs, we do not use the partial bytes mechanism;
1910 instead, rs6000_function_arg will return a PARALLEL including a memory
1911 element as necessary. Library support functions for IEEE 128-bit are
1912 assumed to not need the value passed both in GPRs and in vector
1913 registers. */
1914 if (TARGET_64BIT && !cum->prototype
1915 && (!cum->libcall || !FLOAT128_VECTOR_P (elt_mode)))
1916 return 0;
1917
1918 /* Otherwise, we pass in VRs only. Check for partial copies. */
1919 passed_in_gprs = false;
1920 if (cum->vregno + n_elts > ALTIVEC_ARG_MAX_REG + 1)
1921 ret = (ALTIVEC_ARG_MAX_REG + 1 - cum->vregno) * 16;
1922 }
1923
1924 /* In this complicated case we just disable the partial_nregs code. */
1925 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (arg.mode, arg.type))
1926 return 0;
1927
1928 align_words = rs6000_parm_start (arg.mode, arg.type, cum->words);
1929
1930 if (USE_FP_FOR_ARG_P (cum, elt_mode)
1931 && !(TARGET_AIX && !TARGET_ELF && arg.aggregate_type_p ()))
1932 {
1933 unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
1934
1935 /* If we are passing this arg in the fixed parameter save area
1936 (gprs or memory) as well as FPRs, we do not use the partial
1937 bytes mechanism; instead, rs6000_function_arg will return a
1938 PARALLEL including a memory element as necessary. */
1939 if (arg.type
1940 && (cum->nargs_prototype <= 0
1941 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
1942 && TARGET_XL_COMPAT
1943 && align_words >= GP_ARG_NUM_REG)))
1944 return 0;
1945
1946 /* Otherwise, we pass in FPRs only. Check for partial copies. */
1947 passed_in_gprs = false;
1948 if (cum->fregno + n_elts * n_fpreg > FP_ARG_MAX_REG + 1)
1949 {
1950 /* Compute number of bytes / words passed in FPRs. If there
1951 is still space available in the register parameter area
1952 *after* that amount, a part of the argument will be passed
1953 in GPRs. In that case, the total amount passed in any
1954 registers is equal to the amount that would have been passed
1955 in GPRs if everything were passed there, so we fall back to
1956 the GPR code below to compute the appropriate value. */
1957 int fpr = ((FP_ARG_MAX_REG + 1 - cum->fregno)
1958 * MIN (8, GET_MODE_SIZE (elt_mode)));
1959 int fpr_words = fpr / (TARGET_32BIT ? 4 : 8);
1960
1961 if (align_words + fpr_words < GP_ARG_NUM_REG)
1962 passed_in_gprs = true;
1963 else
1964 ret = fpr;
1965 }
1966 }
1967
1968 if (passed_in_gprs
1969 && align_words < GP_ARG_NUM_REG
1970 && GP_ARG_NUM_REG < align_words + rs6000_arg_size (arg.mode, arg.type))
1971 ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
1972
1973 if (ret != 0 && TARGET_DEBUG_ARG)
1974 fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
1975
1976 return ret;
1977 }
1978
1979 /* A C expression that indicates when an argument must be passed by
1981 reference. If nonzero for an argument, a copy of that argument is
1982 made in memory and a pointer to the argument is passed instead of
1983 the argument itself. The pointer is passed in whatever way is
1984 appropriate for passing a pointer to that type.
1985
1986 Under V.4, aggregates and long double are passed by reference.
1987
1988 As an extension to all 32-bit ABIs, AltiVec vectors are passed by
1989 reference unless the AltiVec vector extension ABI is in force.
1990
1991 As an extension to all ABIs, variable sized types are passed by
1992 reference. */
1993
1994 bool
1995 rs6000_pass_by_reference (cumulative_args_t, const function_arg_info &arg)
1996 {
1997 if (!arg.type)
1998 return 0;
1999
2000 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD
2001 && FLOAT128_IEEE_P (TYPE_MODE (arg.type)))
2002 {
2003 if (TARGET_DEBUG_ARG)
2004 fprintf (stderr, "function_arg_pass_by_reference: V4 IEEE 128-bit\n");
2005 return 1;
2006 }
2007
2008 if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (arg.type))
2009 {
2010 if (TARGET_DEBUG_ARG)
2011 fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
2012 return 1;
2013 }
2014
2015 if (int_size_in_bytes (arg.type) < 0)
2016 {
2017 if (TARGET_DEBUG_ARG)
2018 fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
2019 return 1;
2020 }
2021
2022 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
2023 modes only exist for GCC vector types if -maltivec. */
2024 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (arg.mode))
2025 {
2026 if (TARGET_DEBUG_ARG)
2027 fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
2028 return 1;
2029 }
2030
2031 /* Pass synthetic vectors in memory. */
2032 if (TREE_CODE (arg.type) == VECTOR_TYPE
2033 && int_size_in_bytes (arg.type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
2034 {
2035 static bool warned_for_pass_big_vectors = false;
2036 if (TARGET_DEBUG_ARG)
2037 fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
2038 if (!warned_for_pass_big_vectors)
2039 {
2040 warning (OPT_Wpsabi, "GCC vector passed by reference: "
2041 "non-standard ABI extension with no compatibility "
2042 "guarantee");
2043 warned_for_pass_big_vectors = true;
2044 }
2045 return 1;
2046 }
2047
2048 return 0;
2049 }
2050
2051 /* Process parameter of type TYPE after ARGS_SO_FAR parameters were
2052 already processes. Return true if the parameter must be passed
2053 (fully or partially) on the stack. */
2054
2055 static bool
2056 rs6000_parm_needs_stack (cumulative_args_t args_so_far, tree type)
2057 {
2058 int unsignedp;
2059 rtx entry_parm;
2060
2061 /* Catch errors. */
2062 if (type == NULL || type == error_mark_node)
2063 return true;
2064
2065 /* Handle types with no storage requirement. */
2066 if (TYPE_MODE (type) == VOIDmode)
2067 return false;
2068
2069 /* Handle complex types. */
2070 if (TREE_CODE (type) == COMPLEX_TYPE)
2071 return (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type))
2072 || rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type)));
2073
2074 /* Handle transparent aggregates. */
2075 if ((TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == RECORD_TYPE)
2076 && TYPE_TRANSPARENT_AGGR (type))
2077 type = TREE_TYPE (first_field (type));
2078
2079 /* See if this arg was passed by invisible reference. */
2080 function_arg_info arg (type, /*named=*/true);
2081 apply_pass_by_reference_rules (get_cumulative_args (args_so_far), arg);
2082
2083 /* Find mode as it is passed by the ABI. */
2084 unsignedp = TYPE_UNSIGNED (type);
2085 arg.mode = promote_mode (arg.type, arg.mode, &unsignedp);
2086
2087 /* If we must pass in stack, we need a stack. */
2088 if (rs6000_must_pass_in_stack (arg))
2089 return true;
2090
2091 /* If there is no incoming register, we need a stack. */
2092 entry_parm = rs6000_function_arg (args_so_far, arg);
2093 if (entry_parm == NULL)
2094 return true;
2095
2096 /* Likewise if we need to pass both in registers and on the stack. */
2097 if (GET_CODE (entry_parm) == PARALLEL
2098 && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
2099 return true;
2100
2101 /* Also true if we're partially in registers and partially not. */
2102 if (rs6000_arg_partial_bytes (args_so_far, arg) != 0)
2103 return true;
2104
2105 /* Update info on where next arg arrives in registers. */
2106 rs6000_function_arg_advance (args_so_far, arg);
2107 return false;
2108 }
2109
2110 /* Return true if FUN has no prototype, has a variable argument
2111 list, or passes any parameter in memory. */
2112
2113 static bool
2114 rs6000_function_parms_need_stack (tree fun, bool incoming)
2115 {
2116 tree fntype, result;
2117 CUMULATIVE_ARGS args_so_far_v;
2118 cumulative_args_t args_so_far;
2119
2120 if (!fun)
2121 /* Must be a libcall, all of which only use reg parms. */
2122 return false;
2123
2124 fntype = fun;
2125 if (!TYPE_P (fun))
2126 fntype = TREE_TYPE (fun);
2127
2128 /* Varargs functions need the parameter save area. */
2129 if ((!incoming && !prototype_p (fntype)) || stdarg_p (fntype))
2130 return true;
2131
2132 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far_v, fntype, NULL_RTX);
2133 args_so_far = pack_cumulative_args (&args_so_far_v);
2134
2135 /* When incoming, we will have been passed the function decl.
2136 It is necessary to use the decl to handle K&R style functions,
2137 where TYPE_ARG_TYPES may not be available. */
2138 if (incoming)
2139 {
2140 gcc_assert (DECL_P (fun));
2141 result = DECL_RESULT (fun);
2142 }
2143 else
2144 result = TREE_TYPE (fntype);
2145
2146 if (result && aggregate_value_p (result, fntype))
2147 {
2148 if (!TYPE_P (result))
2149 result = TREE_TYPE (result);
2150 result = build_pointer_type (result);
2151 rs6000_parm_needs_stack (args_so_far, result);
2152 }
2153
2154 if (incoming)
2155 {
2156 tree parm;
2157
2158 for (parm = DECL_ARGUMENTS (fun);
2159 parm && parm != void_list_node;
2160 parm = TREE_CHAIN (parm))
2161 if (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (parm)))
2162 return true;
2163 }
2164 else
2165 {
2166 function_args_iterator args_iter;
2167 tree arg_type;
2168
2169 FOREACH_FUNCTION_ARGS (fntype, arg_type, args_iter)
2170 if (rs6000_parm_needs_stack (args_so_far, arg_type))
2171 return true;
2172 }
2173
2174 return false;
2175 }
2176
2177 /* Return the size of the REG_PARM_STACK_SPACE are for FUN. This is
2178 usually a constant depending on the ABI. However, in the ELFv2 ABI
2179 the register parameter area is optional when calling a function that
2180 has a prototype is scope, has no variable argument list, and passes
2181 all parameters in registers. */
2182
2183 int
2184 rs6000_reg_parm_stack_space (tree fun, bool incoming)
2185 {
2186 int reg_parm_stack_space;
2187
2188 switch (DEFAULT_ABI)
2189 {
2190 default:
2191 reg_parm_stack_space = 0;
2192 break;
2193
2194 case ABI_AIX:
2195 case ABI_DARWIN:
2196 reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
2197 break;
2198
2199 case ABI_ELFv2:
2200 /* ??? Recomputing this every time is a bit expensive. Is there
2201 a place to cache this information? */
2202 if (rs6000_function_parms_need_stack (fun, incoming))
2203 reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
2204 else
2205 reg_parm_stack_space = 0;
2206 break;
2207 }
2208
2209 return reg_parm_stack_space;
2210 }
2211
2212 static void
2213 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
2214 {
2215 int i;
2216 machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
2217
2218 if (nregs == 0)
2219 return;
2220
2221 for (i = 0; i < nregs; i++)
2222 {
2223 rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
2224 if (reload_completed)
2225 {
2226 if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
2227 tem = NULL_RTX;
2228 else
2229 tem = simplify_gen_subreg (reg_mode, x, BLKmode,
2230 i * GET_MODE_SIZE (reg_mode));
2231 }
2232 else
2233 tem = replace_equiv_address (tem, XEXP (tem, 0));
2234
2235 gcc_assert (tem);
2236
2237 emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
2238 }
2239 }
2240
2241 /* Perform any needed actions needed for a function that is receiving a
2243 variable number of arguments.
2244
2245 CUM is as above.
2246
2247 ARG is the last named argument.
2248
2249 PRETEND_SIZE is a variable that should be set to the amount of stack
2250 that must be pushed by the prolog to pretend that our caller pushed
2251 it.
2252
2253 Normally, this macro will push all remaining incoming registers on the
2254 stack and set PRETEND_SIZE to the length of the registers pushed. */
2255
2256 void
2257 setup_incoming_varargs (cumulative_args_t cum,
2258 const function_arg_info &arg,
2259 int *pretend_size ATTRIBUTE_UNUSED, int no_rtl)
2260 {
2261 CUMULATIVE_ARGS next_cum;
2262 int reg_size = TARGET_32BIT ? 4 : 8;
2263 rtx save_area = NULL_RTX, mem;
2264 int first_reg_offset;
2265 alias_set_type set;
2266
2267 /* Skip the last named argument. */
2268 next_cum = *get_cumulative_args (cum);
2269 rs6000_function_arg_advance_1 (&next_cum, arg.mode, arg.type, arg.named, 0);
2270
2271 if (DEFAULT_ABI == ABI_V4)
2272 {
2273 first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
2274
2275 if (! no_rtl)
2276 {
2277 int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
2278 HOST_WIDE_INT offset = 0;
2279
2280 /* Try to optimize the size of the varargs save area.
2281 The ABI requires that ap.reg_save_area is doubleword
2282 aligned, but we don't need to allocate space for all
2283 the bytes, only those to which we actually will save
2284 anything. */
2285 if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
2286 gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
2287 if (TARGET_HARD_FLOAT
2288 && next_cum.fregno <= FP_ARG_V4_MAX_REG
2289 && cfun->va_list_fpr_size)
2290 {
2291 if (gpr_reg_num)
2292 fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
2293 * UNITS_PER_FP_WORD;
2294 if (cfun->va_list_fpr_size
2295 < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
2296 fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
2297 else
2298 fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
2299 * UNITS_PER_FP_WORD;
2300 }
2301 if (gpr_reg_num)
2302 {
2303 offset = -((first_reg_offset * reg_size) & ~7);
2304 if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
2305 {
2306 gpr_reg_num = cfun->va_list_gpr_size;
2307 if (reg_size == 4 && (first_reg_offset & 1))
2308 gpr_reg_num++;
2309 }
2310 gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
2311 }
2312 else if (fpr_size)
2313 offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
2314 * UNITS_PER_FP_WORD
2315 - (int) (GP_ARG_NUM_REG * reg_size);
2316
2317 if (gpr_size + fpr_size)
2318 {
2319 rtx reg_save_area
2320 = assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
2321 gcc_assert (MEM_P (reg_save_area));
2322 reg_save_area = XEXP (reg_save_area, 0);
2323 if (GET_CODE (reg_save_area) == PLUS)
2324 {
2325 gcc_assert (XEXP (reg_save_area, 0)
2326 == virtual_stack_vars_rtx);
2327 gcc_assert (CONST_INT_P (XEXP (reg_save_area, 1)));
2328 offset += INTVAL (XEXP (reg_save_area, 1));
2329 }
2330 else
2331 gcc_assert (reg_save_area == virtual_stack_vars_rtx);
2332 }
2333
2334 cfun->machine->varargs_save_offset = offset;
2335 save_area = plus_constant (Pmode, virtual_stack_vars_rtx, offset);
2336 }
2337 }
2338 else
2339 {
2340 first_reg_offset = next_cum.words;
2341 save_area = crtl->args.internal_arg_pointer;
2342
2343 if (targetm.calls.must_pass_in_stack (arg))
2344 first_reg_offset += rs6000_arg_size (TYPE_MODE (arg.type), arg.type);
2345 }
2346
2347 set = get_varargs_alias_set ();
2348 if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
2349 && cfun->va_list_gpr_size)
2350 {
2351 int n_gpr, nregs = GP_ARG_NUM_REG - first_reg_offset;
2352
2353 if (va_list_gpr_counter_field)
2354 /* V4 va_list_gpr_size counts number of registers needed. */
2355 n_gpr = cfun->va_list_gpr_size;
2356 else
2357 /* char * va_list instead counts number of bytes needed. */
2358 n_gpr = (cfun->va_list_gpr_size + reg_size - 1) / reg_size;
2359
2360 if (nregs > n_gpr)
2361 nregs = n_gpr;
2362
2363 mem = gen_rtx_MEM (BLKmode,
2364 plus_constant (Pmode, save_area,
2365 first_reg_offset * reg_size));
2366 MEM_NOTRAP_P (mem) = 1;
2367 set_mem_alias_set (mem, set);
2368 set_mem_align (mem, BITS_PER_WORD);
2369
2370 rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
2371 nregs);
2372 }
2373
2374 /* Save FP registers if needed. */
2375 if (DEFAULT_ABI == ABI_V4
2376 && TARGET_HARD_FLOAT
2377 && ! no_rtl
2378 && next_cum.fregno <= FP_ARG_V4_MAX_REG
2379 && cfun->va_list_fpr_size)
2380 {
2381 int fregno = next_cum.fregno, nregs;
2382 rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
2383 rtx lab = gen_label_rtx ();
2384 int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
2385 * UNITS_PER_FP_WORD);
2386
2387 emit_jump_insn
2388 (gen_rtx_SET (pc_rtx,
2389 gen_rtx_IF_THEN_ELSE (VOIDmode,
2390 gen_rtx_NE (VOIDmode, cr1,
2391 const0_rtx),
2392 gen_rtx_LABEL_REF (VOIDmode, lab),
2393 pc_rtx)));
2394
2395 for (nregs = 0;
2396 fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
2397 fregno++, off += UNITS_PER_FP_WORD, nregs++)
2398 {
2399 mem = gen_rtx_MEM (TARGET_HARD_FLOAT ? DFmode : SFmode,
2400 plus_constant (Pmode, save_area, off));
2401 MEM_NOTRAP_P (mem) = 1;
2402 set_mem_alias_set (mem, set);
2403 set_mem_align (mem, GET_MODE_ALIGNMENT (
2404 TARGET_HARD_FLOAT ? DFmode : SFmode));
2405 emit_move_insn (mem, gen_rtx_REG (
2406 TARGET_HARD_FLOAT ? DFmode : SFmode, fregno));
2407 }
2408
2409 emit_label (lab);
2410 }
2411 }
2412
2413 /* Create the va_list data type. */
2414
2415 tree
2416 rs6000_build_builtin_va_list (void)
2417 {
2418 tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
2419
2420 /* For AIX, prefer 'char *' because that's what the system
2421 header files like. */
2422 if (DEFAULT_ABI != ABI_V4)
2423 return build_pointer_type (char_type_node);
2424
2425 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
2426 type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
2427 get_identifier ("__va_list_tag"), record);
2428
2429 f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
2430 unsigned_char_type_node);
2431 f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
2432 unsigned_char_type_node);
2433 /* Give the two bytes of padding a name, so that -Wpadded won't warn on
2434 every user file. */
2435 f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2436 get_identifier ("reserved"), short_unsigned_type_node);
2437 f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2438 get_identifier ("overflow_arg_area"),
2439 ptr_type_node);
2440 f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2441 get_identifier ("reg_save_area"),
2442 ptr_type_node);
2443
2444 va_list_gpr_counter_field = f_gpr;
2445 va_list_fpr_counter_field = f_fpr;
2446
2447 DECL_FIELD_CONTEXT (f_gpr) = record;
2448 DECL_FIELD_CONTEXT (f_fpr) = record;
2449 DECL_FIELD_CONTEXT (f_res) = record;
2450 DECL_FIELD_CONTEXT (f_ovf) = record;
2451 DECL_FIELD_CONTEXT (f_sav) = record;
2452
2453 TYPE_STUB_DECL (record) = type_decl;
2454 TYPE_NAME (record) = type_decl;
2455 TYPE_FIELDS (record) = f_gpr;
2456 DECL_CHAIN (f_gpr) = f_fpr;
2457 DECL_CHAIN (f_fpr) = f_res;
2458 DECL_CHAIN (f_res) = f_ovf;
2459 DECL_CHAIN (f_ovf) = f_sav;
2460
2461 layout_type (record);
2462
2463 /* The correct type is an array type of one element. */
2464 return build_array_type (record, build_index_type (size_zero_node));
2465 }
2466
2467 /* Implement va_start. */
2468
2469 void
2470 rs6000_va_start (tree valist, rtx nextarg)
2471 {
2472 HOST_WIDE_INT words, n_gpr, n_fpr;
2473 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
2474 tree gpr, fpr, ovf, sav, t;
2475
2476 /* Only SVR4 needs something special. */
2477 if (DEFAULT_ABI != ABI_V4)
2478 {
2479 std_expand_builtin_va_start (valist, nextarg);
2480 return;
2481 }
2482
2483 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
2484 f_fpr = DECL_CHAIN (f_gpr);
2485 f_res = DECL_CHAIN (f_fpr);
2486 f_ovf = DECL_CHAIN (f_res);
2487 f_sav = DECL_CHAIN (f_ovf);
2488
2489 valist = build_simple_mem_ref (valist);
2490 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
2491 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
2492 f_fpr, NULL_TREE);
2493 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
2494 f_ovf, NULL_TREE);
2495 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
2496 f_sav, NULL_TREE);
2497
2498 /* Count number of gp and fp argument registers used. */
2499 words = crtl->args.info.words;
2500 n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
2501 GP_ARG_NUM_REG);
2502 n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
2503 FP_ARG_NUM_REG);
2504
2505 if (TARGET_DEBUG_ARG)
2506 fprintf (stderr, "va_start: words = " HOST_WIDE_INT_PRINT_DEC", n_gpr = "
2507 HOST_WIDE_INT_PRINT_DEC", n_fpr = " HOST_WIDE_INT_PRINT_DEC"\n",
2508 words, n_gpr, n_fpr);
2509
2510 if (cfun->va_list_gpr_size)
2511 {
2512 t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
2513 build_int_cst (NULL_TREE, n_gpr));
2514 TREE_SIDE_EFFECTS (t) = 1;
2515 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2516 }
2517
2518 if (cfun->va_list_fpr_size)
2519 {
2520 t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
2521 build_int_cst (NULL_TREE, n_fpr));
2522 TREE_SIDE_EFFECTS (t) = 1;
2523 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2524
2525 #ifdef HAVE_AS_GNU_ATTRIBUTE
2526 if (call_ABI_of_interest (cfun->decl))
2527 rs6000_passes_float = true;
2528 #endif
2529 }
2530
2531 /* Find the overflow area. */
2532 t = make_tree (TREE_TYPE (ovf), crtl->args.internal_arg_pointer);
2533 if (words != 0)
2534 t = fold_build_pointer_plus_hwi (t, words * MIN_UNITS_PER_WORD);
2535 t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
2536 TREE_SIDE_EFFECTS (t) = 1;
2537 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2538
2539 /* If there were no va_arg invocations, don't set up the register
2540 save area. */
2541 if (!cfun->va_list_gpr_size
2542 && !cfun->va_list_fpr_size
2543 && n_gpr < GP_ARG_NUM_REG
2544 && n_fpr < FP_ARG_V4_MAX_REG)
2545 return;
2546
2547 /* Find the register save area. */
2548 t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
2549 if (cfun->machine->varargs_save_offset)
2550 t = fold_build_pointer_plus_hwi (t, cfun->machine->varargs_save_offset);
2551 t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
2552 TREE_SIDE_EFFECTS (t) = 1;
2553 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2554 }
2555
2556 /* Implement va_arg. */
2557
2558 tree
2559 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
2560 gimple_seq *post_p)
2561 {
2562 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
2563 tree gpr, fpr, ovf, sav, reg, t, u;
2564 int size, rsize, n_reg, sav_ofs, sav_scale;
2565 tree lab_false, lab_over, addr;
2566 int align;
2567 tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
2568 int regalign = 0;
2569 gimple *stmt;
2570
2571 if (pass_va_arg_by_reference (type))
2572 {
2573 t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
2574 return build_va_arg_indirect_ref (t);
2575 }
2576
2577 /* We need to deal with the fact that the darwin ppc64 ABI is defined by an
2578 earlier version of gcc, with the property that it always applied alignment
2579 adjustments to the va-args (even for zero-sized types). The cheapest way
2580 to deal with this is to replicate the effect of the part of
2581 std_gimplify_va_arg_expr that carries out the align adjust, for the case
2582 of relevance.
2583 We don't need to check for pass-by-reference because of the test above.
2584 We can return a simplifed answer, since we know there's no offset to add. */
2585
2586 if (((TARGET_MACHO
2587 && rs6000_darwin64_abi)
2588 || DEFAULT_ABI == ABI_ELFv2
2589 || (DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm))
2590 && integer_zerop (TYPE_SIZE (type)))
2591 {
2592 unsigned HOST_WIDE_INT align, boundary;
2593 tree valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
2594 align = PARM_BOUNDARY / BITS_PER_UNIT;
2595 boundary = rs6000_function_arg_boundary (TYPE_MODE (type), type);
2596 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
2597 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
2598 boundary /= BITS_PER_UNIT;
2599 if (boundary > align)
2600 {
2601 tree t ;
2602 /* This updates arg ptr by the amount that would be necessary
2603 to align the zero-sized (but not zero-alignment) item. */
2604 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
2605 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
2606 gimplify_and_add (t, pre_p);
2607
2608 t = fold_convert (sizetype, valist_tmp);
2609 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
2610 fold_convert (TREE_TYPE (valist),
2611 fold_build2 (BIT_AND_EXPR, sizetype, t,
2612 size_int (-boundary))));
2613 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
2614 gimplify_and_add (t, pre_p);
2615 }
2616 /* Since it is zero-sized there's no increment for the item itself. */
2617 valist_tmp = fold_convert (build_pointer_type (type), valist_tmp);
2618 return build_va_arg_indirect_ref (valist_tmp);
2619 }
2620
2621 if (DEFAULT_ABI != ABI_V4)
2622 {
2623 if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
2624 {
2625 tree elem_type = TREE_TYPE (type);
2626 machine_mode elem_mode = TYPE_MODE (elem_type);
2627 int elem_size = GET_MODE_SIZE (elem_mode);
2628
2629 if (elem_size < UNITS_PER_WORD)
2630 {
2631 tree real_part, imag_part;
2632 gimple_seq post = NULL;
2633
2634 real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
2635 &post);
2636 /* Copy the value into a temporary, lest the formal temporary
2637 be reused out from under us. */
2638 real_part = get_initialized_tmp_var (real_part, pre_p, &post);
2639 gimple_seq_add_seq (pre_p, post);
2640
2641 imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
2642 post_p);
2643
2644 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
2645 }
2646 }
2647
2648 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
2649 }
2650
2651 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
2652 f_fpr = DECL_CHAIN (f_gpr);
2653 f_res = DECL_CHAIN (f_fpr);
2654 f_ovf = DECL_CHAIN (f_res);
2655 f_sav = DECL_CHAIN (f_ovf);
2656
2657 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
2658 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
2659 f_fpr, NULL_TREE);
2660 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
2661 f_ovf, NULL_TREE);
2662 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
2663 f_sav, NULL_TREE);
2664
2665 size = int_size_in_bytes (type);
2666 rsize = (size + 3) / 4;
2667 int pad = 4 * rsize - size;
2668 align = 1;
2669
2670 machine_mode mode = TYPE_MODE (type);
2671 if (abi_v4_pass_in_fpr (mode, false))
2672 {
2673 /* FP args go in FP registers, if present. */
2674 reg = fpr;
2675 n_reg = (size + 7) / 8;
2676 sav_ofs = (TARGET_HARD_FLOAT ? 8 : 4) * 4;
2677 sav_scale = (TARGET_HARD_FLOAT ? 8 : 4);
2678 if (mode != SFmode && mode != SDmode)
2679 align = 8;
2680 }
2681 else
2682 {
2683 /* Otherwise into GP registers. */
2684 reg = gpr;
2685 n_reg = rsize;
2686 sav_ofs = 0;
2687 sav_scale = 4;
2688 if (n_reg == 2)
2689 align = 8;
2690 }
2691
2692 /* Pull the value out of the saved registers.... */
2693
2694 lab_over = NULL;
2695 addr = create_tmp_var (ptr_type_node, "addr");
2696
2697 /* AltiVec vectors never go in registers when -mabi=altivec. */
2698 if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
2699 align = 16;
2700 else
2701 {
2702 lab_false = create_artificial_label (input_location);
2703 lab_over = create_artificial_label (input_location);
2704
2705 /* Long long is aligned in the registers. As are any other 2 gpr
2706 item such as complex int due to a historical mistake. */
2707 u = reg;
2708 if (n_reg == 2 && reg == gpr)
2709 {
2710 regalign = 1;
2711 u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
2712 build_int_cst (TREE_TYPE (reg), n_reg - 1));
2713 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
2714 unshare_expr (reg), u);
2715 }
2716 /* _Decimal128 is passed in even/odd fpr pairs; the stored
2717 reg number is 0 for f1, so we want to make it odd. */
2718 else if (reg == fpr && mode == TDmode)
2719 {
2720 t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
2721 build_int_cst (TREE_TYPE (reg), 1));
2722 u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
2723 }
2724
2725 t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
2726 t = build2 (GE_EXPR, boolean_type_node, u, t);
2727 u = build1 (GOTO_EXPR, void_type_node, lab_false);
2728 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
2729 gimplify_and_add (t, pre_p);
2730
2731 t = sav;
2732 if (sav_ofs)
2733 t = fold_build_pointer_plus_hwi (sav, sav_ofs);
2734
2735 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
2736 build_int_cst (TREE_TYPE (reg), n_reg));
2737 u = fold_convert (sizetype, u);
2738 u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
2739 t = fold_build_pointer_plus (t, u);
2740
2741 /* _Decimal32 varargs are located in the second word of the 64-bit
2742 FP register for 32-bit binaries. */
2743 if (TARGET_32BIT && TARGET_HARD_FLOAT && mode == SDmode)
2744 t = fold_build_pointer_plus_hwi (t, size);
2745
2746 /* Args are passed right-aligned. */
2747 if (BYTES_BIG_ENDIAN)
2748 t = fold_build_pointer_plus_hwi (t, pad);
2749
2750 gimplify_assign (addr, t, pre_p);
2751
2752 gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
2753
2754 stmt = gimple_build_label (lab_false);
2755 gimple_seq_add_stmt (pre_p, stmt);
2756
2757 if ((n_reg == 2 && !regalign) || n_reg > 2)
2758 {
2759 /* Ensure that we don't find any more args in regs.
2760 Alignment has taken care of for special cases. */
2761 gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
2762 }
2763 }
2764
2765 /* ... otherwise out of the overflow area. */
2766
2767 /* Care for on-stack alignment if needed. */
2768 t = ovf;
2769 if (align != 1)
2770 {
2771 t = fold_build_pointer_plus_hwi (t, align - 1);
2772 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
2773 build_int_cst (TREE_TYPE (t), -align));
2774 }
2775
2776 /* Args are passed right-aligned. */
2777 if (BYTES_BIG_ENDIAN)
2778 t = fold_build_pointer_plus_hwi (t, pad);
2779
2780 gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
2781
2782 gimplify_assign (unshare_expr (addr), t, pre_p);
2783
2784 t = fold_build_pointer_plus_hwi (t, size);
2785 gimplify_assign (unshare_expr (ovf), t, pre_p);
2786
2787 if (lab_over)
2788 {
2789 stmt = gimple_build_label (lab_over);
2790 gimple_seq_add_stmt (pre_p, stmt);
2791 }
2792
2793 if (STRICT_ALIGNMENT
2794 && (TYPE_ALIGN (type)
2795 > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
2796 {
2797 /* The value (of type complex double, for example) may not be
2798 aligned in memory in the saved registers, so copy via a
2799 temporary. (This is the same code as used for SPARC.) */
2800 tree tmp = create_tmp_var (type, "va_arg_tmp");
2801 tree dest_addr = build_fold_addr_expr (tmp);
2802
2803 tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
2804 3, dest_addr, addr, size_int (rsize * 4));
2805 TREE_ADDRESSABLE (tmp) = 1;
2806
2807 gimplify_and_add (copy, pre_p);
2808 addr = dest_addr;
2809 }
2810
2811 addr = fold_convert (ptrtype, addr);
2812 return build_va_arg_indirect_ref (addr);
2813 }
2814
2815 rtx
2816 swap_endian_selector_for_mode (machine_mode mode)
2817 {
2818 unsigned int swap1[16] = {15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
2819 unsigned int swap2[16] = {7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8};
2820 unsigned int swap4[16] = {3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12};
2821 unsigned int swap8[16] = {1,0,3,2,5,4,7,6,9,8,11,10,13,12,15,14};
2822
2823 unsigned int *swaparray, i;
2824 rtx perm[16];
2825
2826 switch (mode)
2827 {
2828 case E_V1TImode:
2829 swaparray = swap1;
2830 break;
2831 case E_V2DFmode:
2832 case E_V2DImode:
2833 swaparray = swap2;
2834 break;
2835 case E_V4SFmode:
2836 case E_V4SImode:
2837 swaparray = swap4;
2838 break;
2839 case E_V8HImode:
2840 swaparray = swap8;
2841 break;
2842 default:
2843 gcc_unreachable ();
2844 }
2845
2846 for (i = 0; i < 16; ++i)
2847 perm[i] = GEN_INT (swaparray[i]);
2848
2849 return force_reg (V16QImode, gen_rtx_CONST_VECTOR (V16QImode,
2850 gen_rtvec_v (16, perm)));
2851 }
2852
2853 /* Return the internal arg pointer used for function incoming
2854 arguments. When -fsplit-stack, the arg pointer is r12 so we need
2855 to copy it to a pseudo in order for it to be preserved over calls
2856 and suchlike. We'd really like to use a pseudo here for the
2857 internal arg pointer but data-flow analysis is not prepared to
2858 accept pseudos as live at the beginning of a function. */
2859
2860 rtx
2861 rs6000_internal_arg_pointer (void)
2862 {
2863 if (flag_split_stack
2864 && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
2865 == NULL))
2866
2867 {
2868 if (cfun->machine->split_stack_arg_pointer == NULL_RTX)
2869 {
2870 rtx pat;
2871
2872 cfun->machine->split_stack_arg_pointer = gen_reg_rtx (Pmode);
2873 REG_POINTER (cfun->machine->split_stack_arg_pointer) = 1;
2874
2875 /* Put the pseudo initialization right after the note at the
2876 beginning of the function. */
2877 pat = gen_rtx_SET (cfun->machine->split_stack_arg_pointer,
2878 gen_rtx_REG (Pmode, 12));
2879 push_topmost_sequence ();
2880 emit_insn_after (pat, get_insns ());
2881 pop_topmost_sequence ();
2882 }
2883 rtx ret = plus_constant (Pmode, cfun->machine->split_stack_arg_pointer,
2884 FIRST_PARM_OFFSET (current_function_decl));
2885 return copy_to_reg (ret);
2886 }
2887 return virtual_incoming_args_rtx;
2888 }
2889
2890
2891 /* A C compound statement that outputs the assembler code for a thunk
2893 function, used to implement C++ virtual function calls with
2894 multiple inheritance. The thunk acts as a wrapper around a virtual
2895 function, adjusting the implicit object parameter before handing
2896 control off to the real function.
2897
2898 First, emit code to add the integer DELTA to the location that
2899 contains the incoming first argument. Assume that this argument
2900 contains a pointer, and is the one used to pass the `this' pointer
2901 in C++. This is the incoming argument *before* the function
2902 prologue, e.g. `%o0' on a sparc. The addition must preserve the
2903 values of all other incoming arguments.
2904
2905 After the addition, emit code to jump to FUNCTION, which is a
2906 `FUNCTION_DECL'. This is a direct pure jump, not a call, and does
2907 not touch the return address. Hence returning from FUNCTION will
2908 return to whoever called the current `thunk'.
2909
2910 The effect must be as if FUNCTION had been called directly with the
2911 adjusted first argument. This macro is responsible for emitting
2912 all of the code for a thunk function; output_function_prologue()
2913 and output_function_epilogue() are not invoked.
2914
2915 The THUNK_FNDECL is redundant. (DELTA and FUNCTION have already
2916 been extracted from it.) It might possibly be useful on some
2917 targets, but probably not.
2918
2919 If you do not define this macro, the target-independent code in the
2920 C++ frontend will generate a less efficient heavyweight thunk that
2921 calls FUNCTION instead of jumping to it. The generic approach does
2922 not support varargs. */
2923
2924 void
2925 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
2926 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
2927 tree function)
2928 {
2929 const char *fnname = get_fnname_from_decl (thunk_fndecl);
2930 rtx this_rtx, funexp;
2931 rtx_insn *insn;
2932
2933 reload_completed = 1;
2934 epilogue_completed = 1;
2935
2936 /* Mark the end of the (empty) prologue. */
2937 emit_note (NOTE_INSN_PROLOGUE_END);
2938
2939 /* Find the "this" pointer. If the function returns a structure,
2940 the structure return pointer is in r3. */
2941 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
2942 this_rtx = gen_rtx_REG (Pmode, 4);
2943 else
2944 this_rtx = gen_rtx_REG (Pmode, 3);
2945
2946 /* Apply the constant offset, if required. */
2947 if (delta)
2948 emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
2949
2950 /* Apply the offset from the vtable, if required. */
2951 if (vcall_offset)
2952 {
2953 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
2954 rtx tmp = gen_rtx_REG (Pmode, 12);
2955
2956 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
2957 if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
2958 {
2959 emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
2960 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
2961 }
2962 else
2963 {
2964 rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
2965
2966 emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
2967 }
2968 emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
2969 }
2970
2971 /* Generate a tail call to the target function. */
2972 if (!TREE_USED (function))
2973 {
2974 assemble_external (function);
2975 TREE_USED (function) = 1;
2976 }
2977 funexp = XEXP (DECL_RTL (function), 0);
2978 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
2979
2980 insn = emit_call_insn (gen_sibcall (funexp, const0_rtx, const0_rtx));
2981 SIBLING_CALL_P (insn) = 1;
2982 emit_barrier ();
2983
2984 /* Run just enough of rest_of_compilation to get the insns emitted.
2985 There's not really enough bulk here to make other passes such as
2986 instruction scheduling worth while. */
2987 insn = get_insns ();
2988 shorten_branches (insn);
2989 assemble_start_function (thunk_fndecl, fnname);
2990 final_start_function (insn, file, 1);
2991 final (insn, file, 1);
2992 final_end_function ();
2993 assemble_end_function (thunk_fndecl, fnname);
2994
2995 reload_completed = 0;
2996 epilogue_completed = 0;
2997 }
2998