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
rs6000_aggregate_candidate(const_tree type,machine_mode * modep,int * empty_base_seen,int * zero_width_bf_seen)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
rs6000_discover_homogeneous_aggregate(machine_mode mode,const_tree type,machine_mode * elt_mode,int * n_elts)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
rs6000_return_in_memory(const_tree type,const_tree fntype ATTRIBUTE_UNUSED)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
rs6000_return_in_msb(const_tree valtype)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
call_ABI_of_interest(tree fndecl)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
init_cumulative_args(CUMULATIVE_ARGS * cum,tree fntype,rtx libname ATTRIBUTE_UNUSED,int incoming,int libcall,int n_named_args,tree fndecl,machine_mode return_mode ATTRIBUTE_UNUSED)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 
698 /* On rs6000, function arguments are promoted, as are function return
699    values.  */
700 
701 machine_mode
rs6000_promote_function_mode(const_tree type ATTRIBUTE_UNUSED,machine_mode mode,int * punsignedp ATTRIBUTE_UNUSED,const_tree,int for_return ATTRIBUTE_UNUSED)702 rs6000_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
703                                     machine_mode mode,
704                                     int *punsignedp ATTRIBUTE_UNUSED,
705                                     const_tree, int for_return ATTRIBUTE_UNUSED)
706 {
707   if (GET_MODE_CLASS (mode) == MODE_INT
708       && GET_MODE_SIZE (mode) < (TARGET_32BIT ? 4 : 8))
709     mode = TARGET_32BIT ? SImode : DImode;
710 
711   return mode;
712 }
713 
714 /* Return true if TYPE must be passed on the stack and not in registers.  */
715 
716 bool
rs6000_must_pass_in_stack(const function_arg_info & arg)717 rs6000_must_pass_in_stack (const function_arg_info &arg)
718 {
719   if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2 || TARGET_64BIT)
720     return must_pass_in_stack_var_size (arg);
721   else
722     return must_pass_in_stack_var_size_or_pad (arg);
723 }
724 
725 static inline bool
is_complex_IBM_long_double(machine_mode mode)726 is_complex_IBM_long_double (machine_mode mode)
727 {
728   return mode == ICmode || (mode == TCmode && FLOAT128_IBM_P (TCmode));
729 }
730 
731 /* Whether ABI_V4 passes MODE args to a function in floating point
732    registers.  */
733 
734 static bool
abi_v4_pass_in_fpr(machine_mode mode,bool named)735 abi_v4_pass_in_fpr (machine_mode mode, bool named)
736 {
737   if (!TARGET_HARD_FLOAT)
738     return false;
739   if (mode == DFmode)
740     return true;
741   if (mode == SFmode && named)
742     return true;
743   /* ABI_V4 passes complex IBM long double in 8 gprs.
744      Stupid, but we can't change the ABI now.  */
745   if (is_complex_IBM_long_double (mode))
746     return false;
747   if (FLOAT128_2REG_P (mode))
748     return true;
749   if (DECIMAL_FLOAT_MODE_P (mode))
750     return true;
751   return false;
752 }
753 
754 /* Implement TARGET_FUNCTION_ARG_PADDING.
755 
756    For the AIX ABI structs are always stored left shifted in their
757    argument slot.  */
758 
759 pad_direction
rs6000_function_arg_padding(machine_mode mode,const_tree type)760 rs6000_function_arg_padding (machine_mode mode, const_tree type)
761 {
762 #ifndef AGGREGATE_PADDING_FIXED
763 #define AGGREGATE_PADDING_FIXED 0
764 #endif
765 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
766 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
767 #endif
768 
769   if (!AGGREGATE_PADDING_FIXED)
770     {
771       /* GCC used to pass structures of the same size as integer types as
772            if they were in fact integers, ignoring TARGET_FUNCTION_ARG_PADDING.
773            i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
774            passed padded downward, except that -mstrict-align further
775            muddied the water in that multi-component structures of 2 and 4
776            bytes in size were passed padded upward.
777 
778            The following arranges for best compatibility with previous
779            versions of gcc, but removes the -mstrict-align dependency.  */
780       if (BYTES_BIG_ENDIAN)
781           {
782             HOST_WIDE_INT size = 0;
783 
784             if (mode == BLKmode)
785               {
786                 if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
787                     size = int_size_in_bytes (type);
788               }
789             else
790               size = GET_MODE_SIZE (mode);
791 
792             if (size == 1 || size == 2 || size == 4)
793               return PAD_DOWNWARD;
794           }
795       return PAD_UPWARD;
796     }
797 
798   if (AGGREGATES_PAD_UPWARD_ALWAYS)
799     {
800       if (type != 0 && AGGREGATE_TYPE_P (type))
801           return PAD_UPWARD;
802     }
803 
804   /* Fall back to the default.  */
805   return default_function_arg_padding (mode, type);
806 }
807 
808 /* If defined, a C expression that gives the alignment boundary, in bits,
809    of an argument with the specified mode and type.  If it is not defined,
810    PARM_BOUNDARY is used for all arguments.
811 
812    V.4 wants long longs and doubles to be double word aligned.  Just
813    testing the mode size is a boneheaded way to do this as it means
814    that other types such as complex int are also double word aligned.
815    However, we're stuck with this because changing the ABI might break
816    existing library interfaces.
817 
818    Quadword align Altivec/VSX vectors.
819    Quadword align large synthetic vector types.   */
820 
821 unsigned int
rs6000_function_arg_boundary(machine_mode mode,const_tree type)822 rs6000_function_arg_boundary (machine_mode mode, const_tree type)
823 {
824   machine_mode elt_mode;
825   int n_elts;
826 
827   rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
828 
829   if (DEFAULT_ABI == ABI_V4
830       && (GET_MODE_SIZE (mode) == 8
831             || (TARGET_HARD_FLOAT
832                 && !is_complex_IBM_long_double (mode)
833                 && FLOAT128_2REG_P (mode))))
834     return 64;
835   else if (FLOAT128_VECTOR_P (mode))
836     return 128;
837   else if (type && TREE_CODE (type) == VECTOR_TYPE
838              && int_size_in_bytes (type) >= 8
839              && int_size_in_bytes (type) < 16)
840     return 64;
841   else if (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
842              || (type && TREE_CODE (type) == VECTOR_TYPE
843                  && int_size_in_bytes (type) >= 16))
844     return 128;
845 
846   /* Aggregate types that need > 8 byte alignment are quadword-aligned
847      in the parameter area in the ELFv2 ABI, and in the AIX ABI unless
848      -mcompat-align-parm is used.  */
849   if (((DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm)
850        || DEFAULT_ABI == ABI_ELFv2)
851       && type && TYPE_ALIGN (type) > 64)
852     {
853       /* "Aggregate" means any AGGREGATE_TYPE except for single-element
854          or homogeneous float/vector aggregates here.  We already handled
855          vector aggregates above, but still need to check for float here. */
856       if (AGGREGATE_TYPE_P (type)
857             && !SCALAR_FLOAT_MODE_P (elt_mode))
858           return 128;
859     }
860 
861   /* Similar for the Darwin64 ABI.  Note that for historical reasons we
862      implement the "aggregate type" check as a BLKmode check here; this
863      means certain aggregate types are in fact not aligned.  */
864   if (TARGET_MACHO && rs6000_darwin64_abi
865       && mode == BLKmode
866       && type && TYPE_ALIGN (type) > 64)
867     return 128;
868 
869   return PARM_BOUNDARY;
870 }
871 
872 /* The offset in words to the start of the parameter save area.  */
873 
874 static unsigned int
rs6000_parm_offset(void)875 rs6000_parm_offset (void)
876 {
877   return (DEFAULT_ABI == ABI_V4 ? 2
878             : DEFAULT_ABI == ABI_ELFv2 ? 4
879             : 6);
880 }
881 
882 /* For a function parm of MODE and TYPE, return the starting word in
883    the parameter area.  NWORDS of the parameter area are already used.  */
884 
885 static unsigned int
rs6000_parm_start(machine_mode mode,const_tree type,unsigned int nwords)886 rs6000_parm_start (machine_mode mode, const_tree type,
887                        unsigned int nwords)
888 {
889   unsigned int align;
890 
891   align = rs6000_function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
892   return nwords + (-(rs6000_parm_offset () + nwords) & align);
893 }
894 
895 /* Compute the size (in words) of a function argument.  */
896 
897 static unsigned long
rs6000_arg_size(machine_mode mode,const_tree type)898 rs6000_arg_size (machine_mode mode, const_tree type)
899 {
900   unsigned long size;
901 
902   if (mode != BLKmode)
903     size = GET_MODE_SIZE (mode);
904   else
905     size = int_size_in_bytes (type);
906 
907   if (TARGET_32BIT)
908     return (size + 3) >> 2;
909   else
910     return (size + 7) >> 3;
911 }
912 
913 /* Use this to flush pending int fields.  */
914 
915 static void
rs6000_darwin64_record_arg_advance_flush(CUMULATIVE_ARGS * cum,HOST_WIDE_INT bitpos,int final)916 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
917                                                     HOST_WIDE_INT bitpos, int final)
918 {
919   unsigned int startbit, endbit;
920   int intregs, intoffset;
921 
922   /* Handle the situations where a float is taking up the first half
923      of the GPR, and the other half is empty (typically due to
924      alignment restrictions). We can detect this by a 8-byte-aligned
925      int field, or by seeing that this is the final flush for this
926      argument. Count the word and continue on.  */
927   if (cum->floats_in_gpr == 1
928       && (cum->intoffset % 64 == 0
929             || (cum->intoffset == -1 && final)))
930     {
931       cum->words++;
932       cum->floats_in_gpr = 0;
933     }
934 
935   if (cum->intoffset == -1)
936     return;
937 
938   intoffset = cum->intoffset;
939   cum->intoffset = -1;
940   cum->floats_in_gpr = 0;
941 
942   if (intoffset % BITS_PER_WORD != 0)
943     {
944       unsigned int bits = BITS_PER_WORD - intoffset % BITS_PER_WORD;
945       if (!int_mode_for_size (bits, 0).exists ())
946           {
947             /* We couldn't find an appropriate mode, which happens,
948                e.g., in packed structs when there are 3 bytes to load.
949                Back intoffset back to the beginning of the word in this
950                case.  */
951             intoffset = ROUND_DOWN (intoffset, BITS_PER_WORD);
952           }
953     }
954 
955   startbit = ROUND_DOWN (intoffset, BITS_PER_WORD);
956   endbit = ROUND_UP (bitpos, BITS_PER_WORD);
957   intregs = (endbit - startbit) / BITS_PER_WORD;
958   cum->words += intregs;
959   /* words should be unsigned. */
960   if ((unsigned)cum->words < (endbit/BITS_PER_WORD))
961     {
962       int pad = (endbit/BITS_PER_WORD) - cum->words;
963       cum->words += pad;
964     }
965 }
966 
967 /* The darwin64 ABI calls for us to recurse down through structs,
968    looking for elements passed in registers.  Unfortunately, we have
969    to track int register count here also because of misalignments
970    in powerpc alignment mode.  */
971 
972 static void
rs6000_darwin64_record_arg_advance_recurse(CUMULATIVE_ARGS * cum,const_tree type,HOST_WIDE_INT startbitpos)973 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
974                                                       const_tree type,
975                                                       HOST_WIDE_INT startbitpos)
976 {
977   tree f;
978 
979   for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
980     if (TREE_CODE (f) == FIELD_DECL)
981       {
982           HOST_WIDE_INT bitpos = startbitpos;
983           tree ftype = TREE_TYPE (f);
984           machine_mode mode;
985           if (ftype == error_mark_node)
986             continue;
987           mode = TYPE_MODE (ftype);
988 
989           if (DECL_SIZE (f) != 0
990               && tree_fits_uhwi_p (bit_position (f)))
991             bitpos += int_bit_position (f);
992 
993           /* ??? FIXME: else assume zero offset.  */
994 
995           if (TREE_CODE (ftype) == RECORD_TYPE)
996             rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
997           else if (USE_FP_FOR_ARG_P (cum, mode))
998             {
999               unsigned n_fpregs = (GET_MODE_SIZE (mode) + 7) >> 3;
1000               rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
1001               cum->fregno += n_fpregs;
1002               /* Single-precision floats present a special problem for
1003                  us, because they are smaller than an 8-byte GPR, and so
1004                  the structure-packing rules combined with the standard
1005                  varargs behavior mean that we want to pack float/float
1006                  and float/int combinations into a single register's
1007                  space. This is complicated by the arg advance flushing,
1008                  which works on arbitrarily large groups of int-type
1009                  fields.  */
1010               if (mode == SFmode)
1011                 {
1012                     if (cum->floats_in_gpr == 1)
1013                       {
1014                         /* Two floats in a word; count the word and reset
1015                            the float count.  */
1016                         cum->words++;
1017                         cum->floats_in_gpr = 0;
1018                       }
1019                     else if (bitpos % 64 == 0)
1020                       {
1021                         /* A float at the beginning of an 8-byte word;
1022                            count it and put off adjusting cum->words until
1023                            we see if a arg advance flush is going to do it
1024                            for us.  */
1025                         cum->floats_in_gpr++;
1026                       }
1027                     else
1028                       {
1029                         /* The float is at the end of a word, preceded
1030                            by integer fields, so the arg advance flush
1031                            just above has already set cum->words and
1032                            everything is taken care of.  */
1033                       }
1034                 }
1035               else
1036                 cum->words += n_fpregs;
1037             }
1038           else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
1039             {
1040               rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
1041               cum->vregno++;
1042               cum->words += 2;
1043             }
1044           else if (cum->intoffset == -1)
1045             cum->intoffset = bitpos;
1046       }
1047 }
1048 
1049 /* Check for an item that needs to be considered specially under the darwin 64
1050    bit ABI.  These are record types where the mode is BLK or the structure is
1051    8 bytes in size.  */
1052 int
rs6000_darwin64_struct_check_p(machine_mode mode,const_tree type)1053 rs6000_darwin64_struct_check_p (machine_mode mode, const_tree type)
1054 {
1055   return rs6000_darwin64_abi
1056            && ((mode == BLKmode
1057                 && TREE_CODE (type) == RECORD_TYPE
1058                 && int_size_in_bytes (type) > 0)
1059             || (type && TREE_CODE (type) == RECORD_TYPE
1060                 && int_size_in_bytes (type) == 8)) ? 1 : 0;
1061 }
1062 
1063 /* Update the data in CUM to advance over an argument
1064    of mode MODE and data type TYPE.
1065    (TYPE is null for libcalls where that information may not be available.)
1066 
1067    Note that for args passed by reference, function_arg will be called
1068    with MODE and TYPE set to that of the pointer to the arg, not the arg
1069    itself.  */
1070 
1071 static void
rs6000_function_arg_advance_1(CUMULATIVE_ARGS * cum,machine_mode mode,const_tree type,bool named,int depth)1072 rs6000_function_arg_advance_1 (CUMULATIVE_ARGS *cum, machine_mode mode,
1073                                      const_tree type, bool named, int depth)
1074 {
1075   machine_mode elt_mode;
1076   int n_elts;
1077 
1078   rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
1079 
1080   /* Only tick off an argument if we're not recursing.  */
1081   if (depth == 0)
1082     cum->nargs_prototype--;
1083 
1084 #ifdef HAVE_AS_GNU_ATTRIBUTE
1085   if (TARGET_ELF && (TARGET_64BIT || DEFAULT_ABI == ABI_V4)
1086       && cum->escapes)
1087     {
1088       if (SCALAR_FLOAT_MODE_P (mode))
1089           {
1090             rs6000_passes_float = true;
1091             if ((HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE || TARGET_64BIT)
1092                 && (FLOAT128_IBM_P (mode)
1093                       || FLOAT128_IEEE_P (mode)
1094                       || (type != NULL
1095                           && TYPE_MAIN_VARIANT (type) == long_double_type_node)))
1096               rs6000_passes_long_double = true;
1097           }
1098       if (named && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
1099           rs6000_passes_vector = true;
1100     }
1101 #endif
1102 
1103   if (TARGET_ALTIVEC_ABI
1104       && (ALTIVEC_OR_VSX_VECTOR_MODE (elt_mode)
1105             || (type && TREE_CODE (type) == VECTOR_TYPE
1106                 && int_size_in_bytes (type) == 16)))
1107     {
1108       bool stack = false;
1109 
1110       if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
1111           {
1112             cum->vregno += n_elts;
1113 
1114             /* If we are not splitting Complex IEEE128 args then account for the
1115                fact that they are passed in 2 VSX regs. */
1116             if (!targetm.calls.split_complex_arg && type
1117                 && TREE_CODE (type) == COMPLEX_TYPE && elt_mode == KCmode)
1118               cum->vregno++;
1119 
1120             if (!TARGET_ALTIVEC)
1121               error ("cannot pass argument in vector register because"
1122                        " altivec instructions are disabled, use %qs"
1123                        " to enable them", "-maltivec");
1124 
1125             /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
1126                even if it is going to be passed in a vector register.
1127                Darwin does the same for variable-argument functions.  */
1128             if (((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
1129                  && TARGET_64BIT)
1130                 || (cum->stdarg && DEFAULT_ABI != ABI_V4))
1131               stack = true;
1132           }
1133       else
1134           stack = true;
1135 
1136       if (stack)
1137           {
1138             int align;
1139 
1140             /* Vector parameters must be 16-byte aligned.  In 32-bit
1141                mode this means we need to take into account the offset
1142                to the parameter save area.  In 64-bit mode, they just
1143                have to start on an even word, since the parameter save
1144                area is 16-byte aligned.  */
1145             if (TARGET_32BIT)
1146               align = -(rs6000_parm_offset () + cum->words) & 3;
1147             else
1148               align = cum->words & 1;
1149             cum->words += align + rs6000_arg_size (mode, type);
1150 
1151             if (TARGET_DEBUG_ARG)
1152               {
1153                 fprintf (stderr, "function_adv: words = %2d, align=%d, ",
1154                            cum->words, align);
1155                 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
1156                            cum->nargs_prototype, cum->prototype,
1157                            GET_MODE_NAME (mode));
1158               }
1159           }
1160     }
1161   else if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
1162     {
1163       int size = int_size_in_bytes (type);
1164       /* Variable sized types have size == -1 and are
1165            treated as if consisting entirely of ints.
1166            Pad to 16 byte boundary if needed.  */
1167       if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
1168             && (cum->words % 2) != 0)
1169           cum->words++;
1170       /* For varargs, we can just go up by the size of the struct. */
1171       if (!named)
1172           cum->words += (size + 7) / 8;
1173       else
1174           {
1175             /* It is tempting to say int register count just goes up by
1176                sizeof(type)/8, but this is wrong in a case such as
1177                { int; double; int; } [powerpc alignment].  We have to
1178                grovel through the fields for these too.  */
1179             cum->intoffset = 0;
1180             cum->floats_in_gpr = 0;
1181             rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
1182             rs6000_darwin64_record_arg_advance_flush (cum,
1183                                                                 size * BITS_PER_UNIT, 1);
1184           }
1185             if (TARGET_DEBUG_ARG)
1186               {
1187                 fprintf (stderr, "function_adv: words = %2d, align=%d, size=%d",
1188                            cum->words, TYPE_ALIGN (type), size);
1189                 fprintf (stderr,
1190                      "nargs = %4d, proto = %d, mode = %4s (darwin64 abi)\n",
1191                            cum->nargs_prototype, cum->prototype,
1192                            GET_MODE_NAME (mode));
1193               }
1194     }
1195   else if (DEFAULT_ABI == ABI_V4)
1196     {
1197       if (abi_v4_pass_in_fpr (mode, named))
1198           {
1199             /* _Decimal128 must use an even/odd register pair.  This assumes
1200                that the register number is odd when fregno is odd.  */
1201             if (mode == TDmode && (cum->fregno % 2) == 1)
1202               cum->fregno++;
1203 
1204             if (cum->fregno + (FLOAT128_2REG_P (mode) ? 1 : 0)
1205                 <= FP_ARG_V4_MAX_REG)
1206               cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
1207             else
1208               {
1209                 cum->fregno = FP_ARG_V4_MAX_REG + 1;
1210                 if (mode == DFmode || FLOAT128_IBM_P (mode)
1211                       || mode == DDmode || mode == TDmode)
1212                     cum->words += cum->words & 1;
1213                 cum->words += rs6000_arg_size (mode, type);
1214               }
1215           }
1216       else
1217           {
1218             int n_words = rs6000_arg_size (mode, type);
1219             int gregno = cum->sysv_gregno;
1220 
1221             /* Long long is put in (r3,r4), (r5,r6), (r7,r8) or (r9,r10).
1222                As does any other 2 word item such as complex int due to a
1223                historical mistake.  */
1224             if (n_words == 2)
1225               gregno += (1 - gregno) & 1;
1226 
1227             /* Multi-reg args are not split between registers and stack.  */
1228             if (gregno + n_words - 1 > GP_ARG_MAX_REG)
1229               {
1230                 /* Long long is aligned on the stack.  So are other 2 word
1231                      items such as complex int due to a historical mistake.  */
1232                 if (n_words == 2)
1233                     cum->words += cum->words & 1;
1234                 cum->words += n_words;
1235               }
1236 
1237             /* Note: continuing to accumulate gregno past when we've started
1238                spilling to the stack indicates the fact that we've started
1239                spilling to the stack to expand_builtin_saveregs.  */
1240             cum->sysv_gregno = gregno + n_words;
1241           }
1242 
1243       if (TARGET_DEBUG_ARG)
1244           {
1245             fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
1246                        cum->words, cum->fregno);
1247             fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
1248                        cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
1249             fprintf (stderr, "mode = %4s, named = %d\n",
1250                        GET_MODE_NAME (mode), named);
1251           }
1252     }
1253   else
1254     {
1255       int n_words = rs6000_arg_size (mode, type);
1256       int start_words = cum->words;
1257       int align_words = rs6000_parm_start (mode, type, start_words);
1258 
1259       cum->words = align_words + n_words;
1260 
1261       if (SCALAR_FLOAT_MODE_P (elt_mode) && TARGET_HARD_FLOAT)
1262           {
1263             /* _Decimal128 must be passed in an even/odd float register pair.
1264                This assumes that the register number is odd when fregno is
1265                odd.  */
1266             if (elt_mode == TDmode && (cum->fregno % 2) == 1)
1267               cum->fregno++;
1268             cum->fregno += n_elts * ((GET_MODE_SIZE (elt_mode) + 7) >> 3);
1269           }
1270 
1271       if (TARGET_DEBUG_ARG)
1272           {
1273             fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
1274                        cum->words, cum->fregno);
1275             fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
1276                        cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
1277             fprintf (stderr, "named = %d, align = %d, depth = %d\n",
1278                        named, align_words - start_words, depth);
1279           }
1280     }
1281 }
1282 
1283 void
rs6000_function_arg_advance(cumulative_args_t cum,const function_arg_info & arg)1284 rs6000_function_arg_advance (cumulative_args_t cum,
1285                                    const function_arg_info &arg)
1286 {
1287   rs6000_function_arg_advance_1 (get_cumulative_args (cum),
1288                                          arg.mode, arg.type, arg.named, 0);
1289 }
1290 
1291 /* A subroutine of rs6000_darwin64_record_arg.  Assign the bits of the
1292    structure between cum->intoffset and bitpos to integer registers.  */
1293 
1294 static void
rs6000_darwin64_record_arg_flush(CUMULATIVE_ARGS * cum,HOST_WIDE_INT bitpos,rtx rvec[],int * k)1295 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
1296                                           HOST_WIDE_INT bitpos, rtx rvec[], int *k)
1297 {
1298   machine_mode mode;
1299   unsigned int regno;
1300   unsigned int startbit, endbit;
1301   int this_regno, intregs, intoffset;
1302   rtx reg;
1303 
1304   if (cum->intoffset == -1)
1305     return;
1306 
1307   intoffset = cum->intoffset;
1308   cum->intoffset = -1;
1309 
1310   /* If this is the trailing part of a word, try to only load that
1311      much into the register.  Otherwise load the whole register.  Note
1312      that in the latter case we may pick up unwanted bits.  It's not a
1313      problem at the moment but may wish to revisit.  */
1314 
1315   if (intoffset % BITS_PER_WORD != 0)
1316     {
1317       unsigned int bits = BITS_PER_WORD - intoffset % BITS_PER_WORD;
1318       if (!int_mode_for_size (bits, 0).exists (&mode))
1319           {
1320             /* We couldn't find an appropriate mode, which happens,
1321                e.g., in packed structs when there are 3 bytes to load.
1322                Back intoffset back to the beginning of the word in this
1323                case.  */
1324             intoffset = ROUND_DOWN (intoffset, BITS_PER_WORD);
1325             mode = word_mode;
1326           }
1327     }
1328   else
1329     mode = word_mode;
1330 
1331   startbit = ROUND_DOWN (intoffset, BITS_PER_WORD);
1332   endbit = ROUND_UP (bitpos, BITS_PER_WORD);
1333   intregs = (endbit - startbit) / BITS_PER_WORD;
1334   this_regno = cum->words + intoffset / BITS_PER_WORD;
1335 
1336   if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
1337     cum->use_stack = 1;
1338 
1339   intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
1340   if (intregs <= 0)
1341     return;
1342 
1343   intoffset /= BITS_PER_UNIT;
1344   do
1345     {
1346       regno = GP_ARG_MIN_REG + this_regno;
1347       reg = gen_rtx_REG (mode, regno);
1348       rvec[(*k)++] =
1349           gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
1350 
1351       this_regno += 1;
1352       intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
1353       mode = word_mode;
1354       intregs -= 1;
1355     }
1356   while (intregs > 0);
1357 }
1358 
1359 /* Recursive workhorse for the following.  */
1360 
1361 static void
rs6000_darwin64_record_arg_recurse(CUMULATIVE_ARGS * cum,const_tree type,HOST_WIDE_INT startbitpos,rtx rvec[],int * k)1362 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
1363                                             HOST_WIDE_INT startbitpos, rtx rvec[],
1364                                             int *k)
1365 {
1366   tree f;
1367 
1368   for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
1369     if (TREE_CODE (f) == FIELD_DECL)
1370       {
1371           HOST_WIDE_INT bitpos = startbitpos;
1372           tree ftype = TREE_TYPE (f);
1373           machine_mode mode;
1374           if (ftype == error_mark_node)
1375             continue;
1376           mode = TYPE_MODE (ftype);
1377 
1378           if (DECL_SIZE (f) != 0
1379               && tree_fits_uhwi_p (bit_position (f)))
1380             bitpos += int_bit_position (f);
1381 
1382           /* ??? FIXME: else assume zero offset.  */
1383 
1384           if (TREE_CODE (ftype) == RECORD_TYPE)
1385             rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
1386           else if (cum->named && USE_FP_FOR_ARG_P (cum, mode))
1387             {
1388               unsigned n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
1389 #if 0
1390               switch (mode)
1391                 {
1392                 case E_SCmode: mode = SFmode; break;
1393                 case E_DCmode: mode = DFmode; break;
1394                 case E_TCmode: mode = TFmode; break;
1395                 default: break;
1396                 }
1397 #endif
1398               rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
1399               if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
1400                 {
1401                     gcc_assert (cum->fregno == FP_ARG_MAX_REG
1402                                   && (mode == TFmode || mode == TDmode));
1403                     /* Long double or _Decimal128 split over regs and memory.  */
1404                     mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : DFmode;
1405                     cum->use_stack=1;
1406                 }
1407               rvec[(*k)++]
1408                 = gen_rtx_EXPR_LIST (VOIDmode,
1409                                            gen_rtx_REG (mode, cum->fregno++),
1410                                            GEN_INT (bitpos / BITS_PER_UNIT));
1411               if (FLOAT128_2REG_P (mode))
1412                 cum->fregno++;
1413             }
1414           else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, 1))
1415             {
1416               rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
1417               rvec[(*k)++]
1418                 = gen_rtx_EXPR_LIST (VOIDmode,
1419                                            gen_rtx_REG (mode, cum->vregno++),
1420                                            GEN_INT (bitpos / BITS_PER_UNIT));
1421             }
1422           else if (cum->intoffset == -1)
1423             cum->intoffset = bitpos;
1424       }
1425 }
1426 
1427 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
1428    the register(s) to be used for each field and subfield of a struct
1429    being passed by value, along with the offset of where the
1430    register's value may be found in the block.  FP fields go in FP
1431    register, vector fields go in vector registers, and everything
1432    else goes in int registers, packed as in memory.
1433 
1434    This code is also used for function return values.  RETVAL indicates
1435    whether this is the case.
1436 
1437    Much of this is taken from the SPARC V9 port, which has a similar
1438    calling convention.  */
1439 
1440 rtx
rs6000_darwin64_record_arg(CUMULATIVE_ARGS * orig_cum,const_tree type,bool named,bool retval)1441 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
1442                                   bool named, bool retval)
1443 {
1444   rtx rvec[FIRST_PSEUDO_REGISTER];
1445   int k = 1, kbase = 1;
1446   HOST_WIDE_INT typesize = int_size_in_bytes (type);
1447   /* This is a copy; modifications are not visible to our caller.  */
1448   CUMULATIVE_ARGS copy_cum = *orig_cum;
1449   CUMULATIVE_ARGS *cum = &copy_cum;
1450 
1451   /* Pad to 16 byte boundary if needed.  */
1452   if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
1453       && (cum->words % 2) != 0)
1454     cum->words++;
1455 
1456   cum->intoffset = 0;
1457   cum->use_stack = 0;
1458   cum->named = named;
1459 
1460   /* Put entries into rvec[] for individual FP and vector fields, and
1461      for the chunks of memory that go in int regs.  Note we start at
1462      element 1; 0 is reserved for an indication of using memory, and
1463      may or may not be filled in below. */
1464   rs6000_darwin64_record_arg_recurse (cum, type, /* startbit pos= */ 0, rvec, &k);
1465   rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
1466 
1467   /* If any part of the struct went on the stack put all of it there.
1468      This hack is because the generic code for
1469      FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
1470      parts of the struct are not at the beginning.  */
1471   if (cum->use_stack)
1472     {
1473       if (retval)
1474           return NULL_RTX;    /* doesn't go in registers at all */
1475       kbase = 0;
1476       rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
1477     }
1478   if (k > 1 || cum->use_stack)
1479     return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
1480   else
1481     return NULL_RTX;
1482 }
1483 
1484 /* Determine where to place an argument in 64-bit mode with 32-bit ABI.  */
1485 
1486 static rtx
rs6000_mixed_function_arg(machine_mode mode,const_tree type,int align_words)1487 rs6000_mixed_function_arg (machine_mode mode, const_tree type,
1488                                  int align_words)
1489 {
1490   int n_units;
1491   int i, k;
1492   rtx rvec[GP_ARG_NUM_REG + 1];
1493 
1494   if (align_words >= GP_ARG_NUM_REG)
1495     return NULL_RTX;
1496 
1497   n_units = rs6000_arg_size (mode, type);
1498 
1499   /* Optimize the simple case where the arg fits in one gpr, except in
1500      the case of BLKmode due to assign_parms assuming that registers are
1501      BITS_PER_WORD wide.  */
1502   if (n_units == 0
1503       || (n_units == 1 && mode != BLKmode))
1504     return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
1505 
1506   k = 0;
1507   if (align_words + n_units > GP_ARG_NUM_REG)
1508     /* Not all of the arg fits in gprs.  Say that it goes in memory too,
1509        using a magic NULL_RTX component.
1510        This is not strictly correct.  Only some of the arg belongs in
1511        memory, not all of it.  However, the normal scheme using
1512        function_arg_partial_nregs can result in unusual subregs, eg.
1513        (subreg:SI (reg:DF) 4), which are not handled well.  The code to
1514        store the whole arg to memory is often more efficient than code
1515        to store pieces, and we know that space is available in the right
1516        place for the whole arg.  */
1517     rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
1518 
1519   i = 0;
1520   do
1521     {
1522       rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
1523       rtx off = GEN_INT (i++ * 4);
1524       rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
1525     }
1526   while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
1527 
1528   return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
1529 }
1530 
1531 /* We have an argument of MODE and TYPE that goes into FPRs or VRs,
1532    but must also be copied into the parameter save area starting at
1533    offset ALIGN_WORDS.  Fill in RVEC with the elements corresponding
1534    to the GPRs and/or memory.  Return the number of elements used.  */
1535 
1536 static int
rs6000_psave_function_arg(machine_mode mode,const_tree type,int align_words,rtx * rvec)1537 rs6000_psave_function_arg (machine_mode mode, const_tree type,
1538                                  int align_words, rtx *rvec)
1539 {
1540   int k = 0;
1541 
1542   if (align_words < GP_ARG_NUM_REG)
1543     {
1544       int n_words = rs6000_arg_size (mode, type);
1545 
1546       if (align_words + n_words > GP_ARG_NUM_REG
1547             || mode == BLKmode
1548             || (TARGET_32BIT && TARGET_POWERPC64))
1549           {
1550             /* If this is partially on the stack, then we only
1551                include the portion actually in registers here.  */
1552             machine_mode rmode = TARGET_32BIT ? SImode : DImode;
1553             int i = 0;
1554 
1555             if (align_words + n_words > GP_ARG_NUM_REG)
1556               {
1557                 /* Not all of the arg fits in gprs.  Say that it goes in memory
1558                      too, using a magic NULL_RTX component.  Also see comment in
1559                      rs6000_mixed_function_arg for why the normal
1560                      function_arg_partial_nregs scheme doesn't work in this case. */
1561                 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
1562               }
1563 
1564             do
1565               {
1566                 rtx r = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
1567                 rtx off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
1568                 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
1569               }
1570             while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
1571           }
1572       else
1573           {
1574             /* The whole arg fits in gprs.  */
1575             rtx r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
1576             rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
1577           }
1578     }
1579   else
1580     {
1581       /* It's entirely in memory.  */
1582       rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
1583     }
1584 
1585   return k;
1586 }
1587 
1588 /* RVEC is a vector of K components of an argument of mode MODE.
1589    Construct the final function_arg return value from it.  */
1590 
1591 static rtx
rs6000_finish_function_arg(machine_mode mode,rtx * rvec,int k)1592 rs6000_finish_function_arg (machine_mode mode, rtx *rvec, int k)
1593 {
1594   gcc_assert (k >= 1);
1595 
1596   /* Avoid returning a PARALLEL in the trivial cases.  */
1597   if (k == 1)
1598     {
1599       if (XEXP (rvec[0], 0) == NULL_RTX)
1600           return NULL_RTX;
1601 
1602       if (GET_MODE (XEXP (rvec[0], 0)) == mode)
1603           return XEXP (rvec[0], 0);
1604     }
1605 
1606   return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
1607 }
1608 
1609 /* Determine where to put an argument to a function.
1610    Value is zero to push the argument on the stack,
1611    or a hard register in which to store the argument.
1612 
1613    CUM is a variable of type CUMULATIVE_ARGS which gives info about
1614     the preceding args and about the function being called.  It is
1615     not modified in this routine.
1616    ARG is a description of the argument.
1617 
1618    On RS/6000 the first eight words of non-FP are normally in registers
1619    and the rest are pushed.  Under AIX, the first 13 FP args are in registers.
1620    Under V.4, the first 8 FP args are in registers.
1621 
1622    If this is floating-point and no prototype is specified, we use
1623    both an FP and integer register (or possibly FP reg and stack).  Library
1624    functions (when CALL_LIBCALL is set) always have the proper types for args,
1625    so we can pass the FP value just in one register.  emit_library_function
1626    doesn't support PARALLEL anyway.
1627 
1628    Note that for args passed by reference, function_arg will be called
1629    with ARG describing the pointer to the arg, not the arg itself.  */
1630 
1631 rtx
rs6000_function_arg(cumulative_args_t cum_v,const function_arg_info & arg)1632 rs6000_function_arg (cumulative_args_t cum_v, const function_arg_info &arg)
1633 {
1634   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1635   tree type = arg.type;
1636   machine_mode mode = arg.mode;
1637   bool named = arg.named;
1638   enum rs6000_abi abi = DEFAULT_ABI;
1639   machine_mode elt_mode;
1640   int n_elts;
1641 
1642   /* We do not allow MMA types being used as function arguments.  */
1643   if (mode == OOmode || mode == XOmode)
1644     {
1645       if (TYPE_CANONICAL (type) != NULL_TREE)
1646           type = TYPE_CANONICAL (type);
1647       error ("invalid use of MMA operand of type %qs as a function parameter",
1648                IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
1649       return NULL_RTX;
1650     }
1651 
1652   /* Return a marker to indicate whether CR1 needs to set or clear the
1653      bit that V.4 uses to say fp args were passed in registers.
1654      Assume that we don't need the marker for software floating point,
1655      or compiler generated library calls.  */
1656   if (arg.end_marker_p ())
1657     {
1658       if (abi == ABI_V4
1659             && (cum->call_cookie & CALL_LIBCALL) == 0
1660             && (cum->stdarg
1661                 || (cum->nargs_prototype < 0
1662                       && (cum->prototype || TARGET_NO_PROTOTYPE)))
1663             && TARGET_HARD_FLOAT)
1664           return GEN_INT (cum->call_cookie
1665                               | ((cum->fregno == FP_ARG_MIN_REG)
1666                                  ? CALL_V4_SET_FP_ARGS
1667                                  : CALL_V4_CLEAR_FP_ARGS));
1668 
1669       return GEN_INT (cum->call_cookie & ~CALL_LIBCALL);
1670     }
1671 
1672   rs6000_discover_homogeneous_aggregate (mode, type, &elt_mode, &n_elts);
1673 
1674   if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
1675     {
1676       rtx rslt = rs6000_darwin64_record_arg (cum, type, named, /*retval= */false);
1677       if (rslt != NULL_RTX)
1678           return rslt;
1679       /* Else fall through to usual handling.  */
1680     }
1681 
1682   if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, named))
1683     {
1684       rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
1685       rtx r, off;
1686       int i, k = 0;
1687 
1688       /* Do we also need to pass this argument in the parameter save area?
1689            Library support functions for IEEE 128-bit are assumed to not need the
1690            value passed both in GPRs and in vector registers.  */
1691       if (TARGET_64BIT && !cum->prototype
1692             && (!cum->libcall || !FLOAT128_VECTOR_P (elt_mode)))
1693           {
1694             int align_words = ROUND_UP (cum->words, 2);
1695             k = rs6000_psave_function_arg (mode, type, align_words, rvec);
1696           }
1697 
1698       /* Describe where this argument goes in the vector registers.  */
1699       for (i = 0; i < n_elts && cum->vregno + i <= ALTIVEC_ARG_MAX_REG; i++)
1700           {
1701             r = gen_rtx_REG (elt_mode, cum->vregno + i);
1702             off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
1703             rvec[k++] =  gen_rtx_EXPR_LIST (VOIDmode, r, off);
1704           }
1705 
1706       return rs6000_finish_function_arg (mode, rvec, k);
1707     }
1708   else if (TARGET_ALTIVEC_ABI
1709              && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
1710                  || (type && TREE_CODE (type) == VECTOR_TYPE
1711                        && int_size_in_bytes (type) == 16)))
1712     {
1713       if (named || abi == ABI_V4)
1714           return NULL_RTX;
1715       else
1716           {
1717             /* Vector parameters to varargs functions under AIX or Darwin
1718                get passed in memory and possibly also in GPRs.  */
1719             int align, align_words, n_words;
1720             machine_mode part_mode;
1721 
1722             /* Vector parameters must be 16-byte aligned.  In 32-bit
1723                mode this means we need to take into account the offset
1724                to the parameter save area.  In 64-bit mode, they just
1725                have to start on an even word, since the parameter save
1726                area is 16-byte aligned.  */
1727             if (TARGET_32BIT)
1728               align = -(rs6000_parm_offset () + cum->words) & 3;
1729             else
1730               align = cum->words & 1;
1731             align_words = cum->words + align;
1732 
1733             /* Out of registers?  Memory, then.  */
1734             if (align_words >= GP_ARG_NUM_REG)
1735               return NULL_RTX;
1736 
1737             if (TARGET_32BIT && TARGET_POWERPC64)
1738               return rs6000_mixed_function_arg (mode, type, align_words);
1739 
1740             /* The vector value goes in GPRs.  Only the part of the
1741                value in GPRs is reported here.  */
1742             part_mode = mode;
1743             n_words = rs6000_arg_size (mode, type);
1744             if (align_words + n_words > GP_ARG_NUM_REG)
1745               /* Fortunately, there are only two possibilities, the value
1746                  is either wholly in GPRs or half in GPRs and half not.  */
1747               part_mode = DImode;
1748 
1749             return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
1750           }
1751     }
1752 
1753   else if (abi == ABI_V4)
1754     {
1755       if (abi_v4_pass_in_fpr (mode, named))
1756           {
1757             /* _Decimal128 must use an even/odd register pair.  This assumes
1758                that the register number is odd when fregno is odd.  */
1759             if (mode == TDmode && (cum->fregno % 2) == 1)
1760               cum->fregno++;
1761 
1762             if (cum->fregno + (FLOAT128_2REG_P (mode) ? 1 : 0)
1763                 <= FP_ARG_V4_MAX_REG)
1764               return gen_rtx_REG (mode, cum->fregno);
1765             else
1766               return NULL_RTX;
1767           }
1768       else
1769           {
1770             int n_words = rs6000_arg_size (mode, type);
1771             int gregno = cum->sysv_gregno;
1772 
1773             /* Long long is put in (r3,r4), (r5,r6), (r7,r8) or (r9,r10).
1774                As does any other 2 word item such as complex int due to a
1775                historical mistake.  */
1776             if (n_words == 2)
1777               gregno += (1 - gregno) & 1;
1778 
1779             /* Multi-reg args are not split between registers and stack.  */
1780             if (gregno + n_words - 1 > GP_ARG_MAX_REG)
1781               return NULL_RTX;
1782 
1783             if (TARGET_32BIT && TARGET_POWERPC64)
1784               return rs6000_mixed_function_arg (mode, type,
1785                                                         gregno - GP_ARG_MIN_REG);
1786             return gen_rtx_REG (mode, gregno);
1787           }
1788     }
1789   else
1790     {
1791       int align_words = rs6000_parm_start (mode, type, cum->words);
1792 
1793       /* _Decimal128 must be passed in an even/odd float register pair.
1794            This assumes that the register number is odd when fregno is odd.  */
1795       if (elt_mode == TDmode && (cum->fregno % 2) == 1)
1796           cum->fregno++;
1797 
1798       if (USE_FP_FOR_ARG_P (cum, elt_mode)
1799             && !(TARGET_AIX && !TARGET_ELF
1800                  && type != NULL && AGGREGATE_TYPE_P (type)))
1801           {
1802             rtx rvec[GP_ARG_NUM_REG + AGGR_ARG_NUM_REG + 1];
1803             rtx r, off;
1804             int i, k = 0;
1805             unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
1806             int fpr_words;
1807 
1808             /* Do we also need to pass this argument in the parameter
1809                save area?  */
1810             if (type && (cum->nargs_prototype <= 0
1811                            || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
1812                                  && TARGET_XL_COMPAT
1813                                  && align_words >= GP_ARG_NUM_REG)))
1814               k = rs6000_psave_function_arg (mode, type, align_words, rvec);
1815 
1816             /* Describe where this argument goes in the fprs.  */
1817             for (i = 0; i < n_elts
1818                           && cum->fregno + i * n_fpreg <= FP_ARG_MAX_REG; i++)
1819               {
1820                 /* Check if the argument is split over registers and memory.
1821                      This can only ever happen for long double or _Decimal128;
1822                      complex types are handled via split_complex_arg.  */
1823                 machine_mode fmode = elt_mode;
1824                 if (cum->fregno + (i + 1) * n_fpreg > FP_ARG_MAX_REG + 1)
1825                     {
1826                       gcc_assert (FLOAT128_2REG_P (fmode));
1827                       fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
1828                     }
1829 
1830                 r = gen_rtx_REG (fmode, cum->fregno + i * n_fpreg);
1831                 off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
1832                 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
1833               }
1834 
1835             /* If there were not enough FPRs to hold the argument, the rest
1836                usually goes into memory.  However, if the current position
1837                is still within the register parameter area, a portion may
1838                actually have to go into GPRs.
1839 
1840                Note that it may happen that the portion of the argument
1841                passed in the first "half" of the first GPR was already
1842                passed in the last FPR as well.
1843 
1844                For unnamed arguments, we already set up GPRs to cover the
1845                whole argument in rs6000_psave_function_arg, so there is
1846                nothing further to do at this point.  */
1847             fpr_words = (i * GET_MODE_SIZE (elt_mode)) / (TARGET_32BIT ? 4 : 8);
1848             if (i < n_elts && align_words + fpr_words < GP_ARG_NUM_REG
1849                 && cum->nargs_prototype > 0)
1850             {
1851                 machine_mode rmode = TARGET_32BIT ? SImode : DImode;
1852                 int n_words = rs6000_arg_size (mode, type);
1853 
1854                 align_words += fpr_words;
1855                 n_words -= fpr_words;
1856 
1857                 do
1858                     {
1859                       r = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
1860                       off = GEN_INT (fpr_words++ * GET_MODE_SIZE (rmode));
1861                       rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
1862                     }
1863                 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
1864               }
1865 
1866             return rs6000_finish_function_arg (mode, rvec, k);
1867           }
1868       else if (align_words < GP_ARG_NUM_REG)
1869           {
1870             if (TARGET_32BIT && TARGET_POWERPC64)
1871               return rs6000_mixed_function_arg (mode, type, align_words);
1872 
1873             return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
1874           }
1875       else
1876           return NULL_RTX;
1877     }
1878 }
1879 
1880 /* For an arg passed partly in registers and partly in memory, this is
1881    the number of bytes passed in registers.  For args passed entirely in
1882    registers or entirely in memory, zero.  When an arg is described by a
1883    PARALLEL, perhaps using more than one register type, this function
1884    returns the number of bytes used by the first element of the PARALLEL.  */
1885 
1886 int
rs6000_arg_partial_bytes(cumulative_args_t cum_v,const function_arg_info & arg)1887 rs6000_arg_partial_bytes (cumulative_args_t cum_v,
1888                                 const function_arg_info &arg)
1889 {
1890   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1891   bool passed_in_gprs = true;
1892   int ret = 0;
1893   int align_words;
1894   machine_mode elt_mode;
1895   int n_elts;
1896 
1897   rs6000_discover_homogeneous_aggregate (arg.mode, arg.type,
1898                                                    &elt_mode, &n_elts);
1899 
1900   if (DEFAULT_ABI == ABI_V4)
1901     return 0;
1902 
1903   if (USE_ALTIVEC_FOR_ARG_P (cum, elt_mode, arg.named))
1904     {
1905       /* If we are passing this arg in the fixed parameter save area (gprs or
1906          memory) as well as VRs, we do not use the partial bytes mechanism;
1907          instead, rs6000_function_arg will return a PARALLEL including a memory
1908          element as necessary.  Library support functions for IEEE 128-bit are
1909          assumed to not need the value passed both in GPRs and in vector
1910          registers.  */
1911       if (TARGET_64BIT && !cum->prototype
1912             && (!cum->libcall || !FLOAT128_VECTOR_P (elt_mode)))
1913           return 0;
1914 
1915       /* Otherwise, we pass in VRs only.  Check for partial copies.  */
1916       passed_in_gprs = false;
1917       if (cum->vregno + n_elts > ALTIVEC_ARG_MAX_REG + 1)
1918           ret = (ALTIVEC_ARG_MAX_REG + 1 - cum->vregno) * 16;
1919     }
1920 
1921   /* In this complicated case we just disable the partial_nregs code.  */
1922   if (TARGET_MACHO && rs6000_darwin64_struct_check_p (arg.mode, arg.type))
1923     return 0;
1924 
1925   align_words = rs6000_parm_start (arg.mode, arg.type, cum->words);
1926 
1927   if (USE_FP_FOR_ARG_P (cum, elt_mode)
1928       && !(TARGET_AIX && !TARGET_ELF && arg.aggregate_type_p ()))
1929     {
1930       unsigned long n_fpreg = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
1931 
1932       /* If we are passing this arg in the fixed parameter save area
1933          (gprs or memory) as well as FPRs, we do not use the partial
1934            bytes mechanism; instead, rs6000_function_arg will return a
1935            PARALLEL including a memory element as necessary.  */
1936       if (arg.type
1937             && (cum->nargs_prototype <= 0
1938                 || ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
1939                       && TARGET_XL_COMPAT
1940                       && align_words >= GP_ARG_NUM_REG)))
1941           return 0;
1942 
1943       /* Otherwise, we pass in FPRs only.  Check for partial copies.  */
1944       passed_in_gprs = false;
1945       if (cum->fregno + n_elts * n_fpreg > FP_ARG_MAX_REG + 1)
1946           {
1947             /* Compute number of bytes / words passed in FPRs.  If there
1948                is still space available in the register parameter area
1949                *after* that amount, a part of the argument will be passed
1950                in GPRs.  In that case, the total amount passed in any
1951                registers is equal to the amount that would have been passed
1952                in GPRs if everything were passed there, so we fall back to
1953                the GPR code below to compute the appropriate value.  */
1954             int fpr = ((FP_ARG_MAX_REG + 1 - cum->fregno)
1955                          * MIN (8, GET_MODE_SIZE (elt_mode)));
1956             int fpr_words = fpr / (TARGET_32BIT ? 4 : 8);
1957 
1958             if (align_words + fpr_words < GP_ARG_NUM_REG)
1959               passed_in_gprs = true;
1960             else
1961               ret = fpr;
1962           }
1963     }
1964 
1965   if (passed_in_gprs
1966       && align_words < GP_ARG_NUM_REG
1967       && GP_ARG_NUM_REG < align_words + rs6000_arg_size (arg.mode, arg.type))
1968     ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
1969 
1970   if (ret != 0 && TARGET_DEBUG_ARG)
1971     fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
1972 
1973   return ret;
1974 }
1975 
1976 /* A C expression that indicates when an argument must be passed by
1977    reference.  If nonzero for an argument, a copy of that argument is
1978    made in memory and a pointer to the argument is passed instead of
1979    the argument itself.  The pointer is passed in whatever way is
1980    appropriate for passing a pointer to that type.
1981 
1982    Under V.4, aggregates and long double are passed by reference.
1983 
1984    As an extension to all 32-bit ABIs, AltiVec vectors are passed by
1985    reference unless the AltiVec vector extension ABI is in force.
1986 
1987    As an extension to all ABIs, variable sized types are passed by
1988    reference.  */
1989 
1990 bool
rs6000_pass_by_reference(cumulative_args_t,const function_arg_info & arg)1991 rs6000_pass_by_reference (cumulative_args_t, const function_arg_info &arg)
1992 {
1993   if (!arg.type)
1994     return 0;
1995 
1996   if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD
1997       && FLOAT128_IEEE_P (TYPE_MODE (arg.type)))
1998     {
1999       if (TARGET_DEBUG_ARG)
2000           fprintf (stderr, "function_arg_pass_by_reference: V4 IEEE 128-bit\n");
2001       return 1;
2002     }
2003 
2004   if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (arg.type))
2005     {
2006       if (TARGET_DEBUG_ARG)
2007           fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
2008       return 1;
2009     }
2010 
2011   if (int_size_in_bytes (arg.type) < 0)
2012     {
2013       if (TARGET_DEBUG_ARG)
2014           fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
2015       return 1;
2016     }
2017 
2018   /* Allow -maltivec -mabi=no-altivec without warning.  Altivec vector
2019      modes only exist for GCC vector types if -maltivec.  */
2020   if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (arg.mode))
2021     {
2022       if (TARGET_DEBUG_ARG)
2023           fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
2024       return 1;
2025     }
2026 
2027   /* Pass synthetic vectors in memory.  */
2028   if (TREE_CODE (arg.type) == VECTOR_TYPE
2029       && int_size_in_bytes (arg.type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
2030     {
2031       static bool warned_for_pass_big_vectors = false;
2032       if (TARGET_DEBUG_ARG)
2033           fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
2034       if (!warned_for_pass_big_vectors)
2035           {
2036             warning (OPT_Wpsabi, "GCC vector passed by reference: "
2037                        "non-standard ABI extension with no compatibility "
2038                        "guarantee");
2039             warned_for_pass_big_vectors = true;
2040           }
2041       return 1;
2042     }
2043 
2044   return 0;
2045 }
2046 
2047 /* Process parameter of type TYPE after ARGS_SO_FAR parameters were
2048    already processes.  Return true if the parameter must be passed
2049    (fully or partially) on the stack.  */
2050 
2051 static bool
rs6000_parm_needs_stack(cumulative_args_t args_so_far,tree type)2052 rs6000_parm_needs_stack (cumulative_args_t args_so_far, tree type)
2053 {
2054   int unsignedp;
2055   rtx entry_parm;
2056 
2057   /* Catch errors.  */
2058   if (type == NULL || type == error_mark_node)
2059     return true;
2060 
2061   /* Handle types with no storage requirement.  */
2062   if (TYPE_MODE (type) == VOIDmode)
2063     return false;
2064 
2065   /* Handle complex types.  */
2066   if (TREE_CODE (type) == COMPLEX_TYPE)
2067     return (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type))
2068               || rs6000_parm_needs_stack (args_so_far, TREE_TYPE (type)));
2069 
2070   /* Handle transparent aggregates.  */
2071   if ((TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == RECORD_TYPE)
2072       && TYPE_TRANSPARENT_AGGR (type))
2073     type = TREE_TYPE (first_field (type));
2074 
2075   /* See if this arg was passed by invisible reference.  */
2076   function_arg_info arg (type, /*named=*/true);
2077   apply_pass_by_reference_rules (get_cumulative_args (args_so_far), arg);
2078 
2079   /* Find mode as it is passed by the ABI.  */
2080   unsignedp = TYPE_UNSIGNED (type);
2081   arg.mode = promote_mode (arg.type, arg.mode, &unsignedp);
2082 
2083   /* If we must pass in stack, we need a stack.  */
2084   if (rs6000_must_pass_in_stack (arg))
2085     return true;
2086 
2087   /* If there is no incoming register, we need a stack.  */
2088   entry_parm = rs6000_function_arg (args_so_far, arg);
2089   if (entry_parm == NULL)
2090     return true;
2091 
2092   /* Likewise if we need to pass both in registers and on the stack.  */
2093   if (GET_CODE (entry_parm) == PARALLEL
2094       && XEXP (XVECEXP (entry_parm, 0, 0), 0) == NULL_RTX)
2095     return true;
2096 
2097   /* Also true if we're partially in registers and partially not.  */
2098   if (rs6000_arg_partial_bytes (args_so_far, arg) != 0)
2099     return true;
2100 
2101   /* Update info on where next arg arrives in registers.  */
2102   rs6000_function_arg_advance (args_so_far, arg);
2103   return false;
2104 }
2105 
2106 /* Return true if FUN has no prototype, has a variable argument
2107    list, or passes any parameter in memory.  */
2108 
2109 static bool
rs6000_function_parms_need_stack(tree fun,bool incoming)2110 rs6000_function_parms_need_stack (tree fun, bool incoming)
2111 {
2112   tree fntype, result;
2113   CUMULATIVE_ARGS args_so_far_v;
2114   cumulative_args_t args_so_far;
2115 
2116   if (!fun)
2117     /* Must be a libcall, all of which only use reg parms.  */
2118     return false;
2119 
2120   fntype = fun;
2121   if (!TYPE_P (fun))
2122     fntype = TREE_TYPE (fun);
2123 
2124   /* Varargs functions need the parameter save area.  */
2125   if ((!incoming && !prototype_p (fntype)) || stdarg_p (fntype))
2126     return true;
2127 
2128   INIT_CUMULATIVE_INCOMING_ARGS (args_so_far_v, fntype, NULL_RTX);
2129   args_so_far = pack_cumulative_args (&args_so_far_v);
2130 
2131   /* When incoming, we will have been passed the function decl.
2132      It is necessary to use the decl to handle K&R style functions,
2133      where TYPE_ARG_TYPES may not be available.  */
2134   if (incoming)
2135     {
2136       gcc_assert (DECL_P (fun));
2137       result = DECL_RESULT (fun);
2138     }
2139   else
2140     result = TREE_TYPE (fntype);
2141 
2142   if (result && aggregate_value_p (result, fntype))
2143     {
2144       if (!TYPE_P (result))
2145           result = TREE_TYPE (result);
2146       result = build_pointer_type (result);
2147       rs6000_parm_needs_stack (args_so_far, result);
2148     }
2149 
2150   if (incoming)
2151     {
2152       tree parm;
2153 
2154       for (parm = DECL_ARGUMENTS (fun);
2155              parm && parm != void_list_node;
2156              parm = TREE_CHAIN (parm))
2157           if (rs6000_parm_needs_stack (args_so_far, TREE_TYPE (parm)))
2158             return true;
2159     }
2160   else
2161     {
2162       function_args_iterator args_iter;
2163       tree arg_type;
2164 
2165       FOREACH_FUNCTION_ARGS (fntype, arg_type, args_iter)
2166           if (rs6000_parm_needs_stack (args_so_far, arg_type))
2167             return true;
2168     }
2169 
2170   return false;
2171 }
2172 
2173 /* Return the size of the REG_PARM_STACK_SPACE are for FUN.  This is
2174    usually a constant depending on the ABI.  However, in the ELFv2 ABI
2175    the register parameter area is optional when calling a function that
2176    has a prototype is scope, has no variable argument list, and passes
2177    all parameters in registers.  */
2178 
2179 int
rs6000_reg_parm_stack_space(tree fun,bool incoming)2180 rs6000_reg_parm_stack_space (tree fun, bool incoming)
2181 {
2182   int reg_parm_stack_space;
2183 
2184   switch (DEFAULT_ABI)
2185     {
2186     default:
2187       reg_parm_stack_space = 0;
2188       break;
2189 
2190     case ABI_AIX:
2191     case ABI_DARWIN:
2192       reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
2193       break;
2194 
2195     case ABI_ELFv2:
2196       /* ??? Recomputing this every time is a bit expensive.  Is there
2197            a place to cache this information?  */
2198       if (rs6000_function_parms_need_stack (fun, incoming))
2199           reg_parm_stack_space = TARGET_64BIT ? 64 : 32;
2200       else
2201           reg_parm_stack_space = 0;
2202       break;
2203     }
2204 
2205   return reg_parm_stack_space;
2206 }
2207 
2208 static void
rs6000_move_block_from_reg(int regno,rtx x,int nregs)2209 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
2210 {
2211   int i;
2212   machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
2213 
2214   if (nregs == 0)
2215     return;
2216 
2217   for (i = 0; i < nregs; i++)
2218     {
2219       rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
2220       if (reload_completed)
2221           {
2222             if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
2223               tem = NULL_RTX;
2224             else
2225               tem = simplify_gen_subreg (reg_mode, x, BLKmode,
2226                                                i * GET_MODE_SIZE (reg_mode));
2227           }
2228       else
2229           tem = replace_equiv_address (tem, XEXP (tem, 0));
2230 
2231       gcc_assert (tem);
2232 
2233       emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
2234     }
2235 }
2236 
2237 /* Perform any needed actions needed for a function that is receiving a
2238    variable number of arguments.
2239 
2240    CUM is as above.
2241 
2242    ARG is the last named argument.
2243 
2244    PRETEND_SIZE is a variable that should be set to the amount of stack
2245    that must be pushed by the prolog to pretend that our caller pushed
2246    it.
2247 
2248    Normally, this macro will push all remaining incoming registers on the
2249    stack and set PRETEND_SIZE to the length of the registers pushed.  */
2250 
2251 void
setup_incoming_varargs(cumulative_args_t cum,const function_arg_info & arg,int * pretend_size ATTRIBUTE_UNUSED,int no_rtl)2252 setup_incoming_varargs (cumulative_args_t cum,
2253                               const function_arg_info &arg,
2254                               int *pretend_size ATTRIBUTE_UNUSED, int no_rtl)
2255 {
2256   CUMULATIVE_ARGS next_cum;
2257   int reg_size = TARGET_32BIT ? 4 : 8;
2258   rtx save_area = NULL_RTX, mem;
2259   int first_reg_offset;
2260   alias_set_type set;
2261 
2262   /* Skip the last named argument.  */
2263   next_cum = *get_cumulative_args (cum);
2264   rs6000_function_arg_advance_1 (&next_cum, arg.mode, arg.type, arg.named, 0);
2265 
2266   if (DEFAULT_ABI == ABI_V4)
2267     {
2268       first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
2269 
2270       if (! no_rtl)
2271           {
2272             int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
2273             HOST_WIDE_INT offset = 0;
2274 
2275             /* Try to optimize the size of the varargs save area.
2276                The ABI requires that ap.reg_save_area is doubleword
2277                aligned, but we don't need to allocate space for all
2278                the bytes, only those to which we actually will save
2279                anything.  */
2280             if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
2281               gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
2282             if (TARGET_HARD_FLOAT
2283                 && next_cum.fregno <= FP_ARG_V4_MAX_REG
2284                 && cfun->va_list_fpr_size)
2285               {
2286                 if (gpr_reg_num)
2287                     fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
2288                                  * UNITS_PER_FP_WORD;
2289                 if (cfun->va_list_fpr_size
2290                       < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
2291                     fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
2292                 else
2293                     fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
2294                                   * UNITS_PER_FP_WORD;
2295               }
2296             if (gpr_reg_num)
2297               {
2298                 offset = -((first_reg_offset * reg_size) & ~7);
2299                 if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
2300                     {
2301                       gpr_reg_num = cfun->va_list_gpr_size;
2302                       if (reg_size == 4 && (first_reg_offset & 1))
2303                         gpr_reg_num++;
2304                     }
2305                 gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
2306               }
2307             else if (fpr_size)
2308               offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
2309                            * UNITS_PER_FP_WORD
2310                          - (int) (GP_ARG_NUM_REG * reg_size);
2311 
2312             if (gpr_size + fpr_size)
2313               {
2314                 rtx reg_save_area
2315                     = assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
2316                 gcc_assert (MEM_P (reg_save_area));
2317                 reg_save_area = XEXP (reg_save_area, 0);
2318                 if (GET_CODE (reg_save_area) == PLUS)
2319                     {
2320                       gcc_assert (XEXP (reg_save_area, 0)
2321                                     == virtual_stack_vars_rtx);
2322                       gcc_assert (CONST_INT_P (XEXP (reg_save_area, 1)));
2323                       offset += INTVAL (XEXP (reg_save_area, 1));
2324                     }
2325                 else
2326                     gcc_assert (reg_save_area == virtual_stack_vars_rtx);
2327               }
2328 
2329             cfun->machine->varargs_save_offset = offset;
2330             save_area = plus_constant (Pmode, virtual_stack_vars_rtx, offset);
2331           }
2332     }
2333   else
2334     {
2335       first_reg_offset = next_cum.words;
2336       save_area = crtl->args.internal_arg_pointer;
2337 
2338       if (targetm.calls.must_pass_in_stack (arg))
2339           first_reg_offset += rs6000_arg_size (TYPE_MODE (arg.type), arg.type);
2340     }
2341 
2342   set = get_varargs_alias_set ();
2343   if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
2344       && cfun->va_list_gpr_size)
2345     {
2346       int n_gpr, nregs = GP_ARG_NUM_REG - first_reg_offset;
2347 
2348       if (va_list_gpr_counter_field)
2349           /* V4 va_list_gpr_size counts number of registers needed.  */
2350           n_gpr = cfun->va_list_gpr_size;
2351       else
2352           /* char * va_list instead counts number of bytes needed.  */
2353           n_gpr = (cfun->va_list_gpr_size + reg_size - 1) / reg_size;
2354 
2355       if (nregs > n_gpr)
2356           nregs = n_gpr;
2357 
2358       mem = gen_rtx_MEM (BLKmode,
2359                                plus_constant (Pmode, save_area,
2360                                                   first_reg_offset * reg_size));
2361       MEM_NOTRAP_P (mem) = 1;
2362       set_mem_alias_set (mem, set);
2363       set_mem_align (mem, BITS_PER_WORD);
2364 
2365       rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
2366                                           nregs);
2367     }
2368 
2369   /* Save FP registers if needed.  */
2370   if (DEFAULT_ABI == ABI_V4
2371       && TARGET_HARD_FLOAT
2372       && ! no_rtl
2373       && next_cum.fregno <= FP_ARG_V4_MAX_REG
2374       && cfun->va_list_fpr_size)
2375     {
2376       int fregno = next_cum.fregno, nregs;
2377       rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
2378       rtx lab = gen_label_rtx ();
2379       int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
2380                                                          * UNITS_PER_FP_WORD);
2381 
2382       emit_jump_insn
2383           (gen_rtx_SET (pc_rtx,
2384                           gen_rtx_IF_THEN_ELSE (VOIDmode,
2385                                                       gen_rtx_NE (VOIDmode, cr1,
2386                                                                       const0_rtx),
2387                                                       gen_rtx_LABEL_REF (VOIDmode, lab),
2388                                                       pc_rtx)));
2389 
2390       for (nregs = 0;
2391              fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
2392              fregno++, off += UNITS_PER_FP_WORD, nregs++)
2393           {
2394             mem = gen_rtx_MEM (TARGET_HARD_FLOAT ? DFmode : SFmode,
2395                              plus_constant (Pmode, save_area, off));
2396             MEM_NOTRAP_P (mem) = 1;
2397             set_mem_alias_set (mem, set);
2398             set_mem_align (mem, GET_MODE_ALIGNMENT (
2399                                TARGET_HARD_FLOAT ? DFmode : SFmode));
2400             emit_move_insn (mem, gen_rtx_REG (
2401                           TARGET_HARD_FLOAT ? DFmode : SFmode, fregno));
2402           }
2403 
2404       emit_label (lab);
2405     }
2406 }
2407 
2408 /* Create the va_list data type.  */
2409 
2410 tree
rs6000_build_builtin_va_list(void)2411 rs6000_build_builtin_va_list (void)
2412 {
2413   tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
2414 
2415   /* For AIX, prefer 'char *' because that's what the system
2416      header files like.  */
2417   if (DEFAULT_ABI != ABI_V4)
2418     return build_pointer_type (char_type_node);
2419 
2420   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
2421   type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
2422                                 get_identifier ("__va_list_tag"), record);
2423 
2424   f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
2425                           unsigned_char_type_node);
2426   f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
2427                           unsigned_char_type_node);
2428   /* Give the two bytes of padding a name, so that -Wpadded won't warn on
2429      every user file.  */
2430   f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2431                           get_identifier ("reserved"), short_unsigned_type_node);
2432   f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2433                           get_identifier ("overflow_arg_area"),
2434                           ptr_type_node);
2435   f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2436                           get_identifier ("reg_save_area"),
2437                           ptr_type_node);
2438 
2439   va_list_gpr_counter_field = f_gpr;
2440   va_list_fpr_counter_field = f_fpr;
2441 
2442   DECL_FIELD_CONTEXT (f_gpr) = record;
2443   DECL_FIELD_CONTEXT (f_fpr) = record;
2444   DECL_FIELD_CONTEXT (f_res) = record;
2445   DECL_FIELD_CONTEXT (f_ovf) = record;
2446   DECL_FIELD_CONTEXT (f_sav) = record;
2447 
2448   TYPE_STUB_DECL (record) = type_decl;
2449   TYPE_NAME (record) = type_decl;
2450   TYPE_FIELDS (record) = f_gpr;
2451   DECL_CHAIN (f_gpr) = f_fpr;
2452   DECL_CHAIN (f_fpr) = f_res;
2453   DECL_CHAIN (f_res) = f_ovf;
2454   DECL_CHAIN (f_ovf) = f_sav;
2455 
2456   layout_type (record);
2457 
2458   /* The correct type is an array type of one element.  */
2459   return build_array_type (record, build_index_type (size_zero_node));
2460 }
2461 
2462 /* Implement va_start.  */
2463 
2464 void
rs6000_va_start(tree valist,rtx nextarg)2465 rs6000_va_start (tree valist, rtx nextarg)
2466 {
2467   HOST_WIDE_INT words, n_gpr, n_fpr;
2468   tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
2469   tree gpr, fpr, ovf, sav, t;
2470 
2471   /* Only SVR4 needs something special.  */
2472   if (DEFAULT_ABI != ABI_V4)
2473     {
2474       std_expand_builtin_va_start (valist, nextarg);
2475       return;
2476     }
2477 
2478   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
2479   f_fpr = DECL_CHAIN (f_gpr);
2480   f_res = DECL_CHAIN (f_fpr);
2481   f_ovf = DECL_CHAIN (f_res);
2482   f_sav = DECL_CHAIN (f_ovf);
2483 
2484   valist = build_simple_mem_ref (valist);
2485   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
2486   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
2487                     f_fpr, NULL_TREE);
2488   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
2489                     f_ovf, NULL_TREE);
2490   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
2491                     f_sav, NULL_TREE);
2492 
2493   /* Count number of gp and fp argument registers used.  */
2494   words = crtl->args.info.words;
2495   n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
2496                  GP_ARG_NUM_REG);
2497   n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
2498                  FP_ARG_NUM_REG);
2499 
2500   if (TARGET_DEBUG_ARG)
2501     fprintf (stderr, "va_start: words = " HOST_WIDE_INT_PRINT_DEC", n_gpr = "
2502                HOST_WIDE_INT_PRINT_DEC", n_fpr = " HOST_WIDE_INT_PRINT_DEC"\n",
2503                words, n_gpr, n_fpr);
2504 
2505   if (cfun->va_list_gpr_size)
2506     {
2507       t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
2508                       build_int_cst (NULL_TREE, n_gpr));
2509       TREE_SIDE_EFFECTS (t) = 1;
2510       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2511     }
2512 
2513   if (cfun->va_list_fpr_size)
2514     {
2515       t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
2516                       build_int_cst (NULL_TREE, n_fpr));
2517       TREE_SIDE_EFFECTS (t) = 1;
2518       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2519 
2520 #ifdef HAVE_AS_GNU_ATTRIBUTE
2521       if (call_ABI_of_interest (cfun->decl))
2522           rs6000_passes_float = true;
2523 #endif
2524     }
2525 
2526   /* Find the overflow area.  */
2527   t = make_tree (TREE_TYPE (ovf), crtl->args.internal_arg_pointer);
2528   if (words != 0)
2529     t = fold_build_pointer_plus_hwi (t, words * MIN_UNITS_PER_WORD);
2530   t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
2531   TREE_SIDE_EFFECTS (t) = 1;
2532   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2533 
2534   /* If there were no va_arg invocations, don't set up the register
2535      save area.  */
2536   if (!cfun->va_list_gpr_size
2537       && !cfun->va_list_fpr_size
2538       && n_gpr < GP_ARG_NUM_REG
2539       && n_fpr < FP_ARG_V4_MAX_REG)
2540     return;
2541 
2542   /* Find the register save area.  */
2543   t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
2544   if (cfun->machine->varargs_save_offset)
2545     t = fold_build_pointer_plus_hwi (t, cfun->machine->varargs_save_offset);
2546   t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
2547   TREE_SIDE_EFFECTS (t) = 1;
2548   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2549 }
2550 
2551 /* Implement va_arg.  */
2552 
2553 tree
rs6000_gimplify_va_arg(tree valist,tree type,gimple_seq * pre_p,gimple_seq * post_p)2554 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
2555                               gimple_seq *post_p)
2556 {
2557   tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
2558   tree gpr, fpr, ovf, sav, reg, t, u;
2559   int size, rsize, n_reg, sav_ofs, sav_scale;
2560   tree lab_false, lab_over, addr;
2561   int align;
2562   tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
2563   int regalign = 0;
2564   gimple *stmt;
2565 
2566   if (pass_va_arg_by_reference (type))
2567     {
2568       t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
2569       return build_va_arg_indirect_ref (t);
2570     }
2571 
2572   /* We need to deal with the fact that the darwin ppc64 ABI is defined by an
2573      earlier version of gcc, with the property that it always applied alignment
2574      adjustments to the va-args (even for zero-sized types).  The cheapest way
2575      to deal with this is to replicate the effect of the part of
2576      std_gimplify_va_arg_expr that carries out the align adjust, for the case
2577      of relevance.
2578      We don't need to check for pass-by-reference because of the test above.
2579      We can return a simplifed answer, since we know there's no offset to add.  */
2580 
2581   if (((TARGET_MACHO
2582         && rs6000_darwin64_abi)
2583        || DEFAULT_ABI == ABI_ELFv2
2584        || (DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm))
2585       && integer_zerop (TYPE_SIZE (type)))
2586     {
2587       unsigned HOST_WIDE_INT align, boundary;
2588       tree valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
2589       align = PARM_BOUNDARY / BITS_PER_UNIT;
2590       boundary = rs6000_function_arg_boundary (TYPE_MODE (type), type);
2591       if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
2592           boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
2593       boundary /= BITS_PER_UNIT;
2594       if (boundary > align)
2595           {
2596             tree t ;
2597             /* This updates arg ptr by the amount that would be necessary
2598                to align the zero-sized (but not zero-alignment) item.  */
2599             t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
2600                           fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
2601             gimplify_and_add (t, pre_p);
2602 
2603             t = fold_convert (sizetype, valist_tmp);
2604             t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
2605                       fold_convert (TREE_TYPE (valist),
2606                                         fold_build2 (BIT_AND_EXPR, sizetype, t,
2607                                                        size_int (-boundary))));
2608             t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
2609             gimplify_and_add (t, pre_p);
2610           }
2611       /* Since it is zero-sized there's no increment for the item itself. */
2612       valist_tmp = fold_convert (build_pointer_type (type), valist_tmp);
2613       return build_va_arg_indirect_ref (valist_tmp);
2614     }
2615 
2616   if (DEFAULT_ABI != ABI_V4)
2617     {
2618       if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
2619           {
2620             tree elem_type = TREE_TYPE (type);
2621             machine_mode elem_mode = TYPE_MODE (elem_type);
2622             int elem_size = GET_MODE_SIZE (elem_mode);
2623 
2624             if (elem_size < UNITS_PER_WORD)
2625               {
2626                 tree real_part, imag_part;
2627                 gimple_seq post = NULL;
2628 
2629                 real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
2630                                                               &post);
2631                 /* Copy the value into a temporary, lest the formal temporary
2632                      be reused out from under us.  */
2633                 real_part = get_initialized_tmp_var (real_part, pre_p, &post);
2634                 gimple_seq_add_seq (pre_p, post);
2635 
2636                 imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
2637                                                               post_p);
2638 
2639                 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
2640               }
2641           }
2642 
2643       return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
2644     }
2645 
2646   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
2647   f_fpr = DECL_CHAIN (f_gpr);
2648   f_res = DECL_CHAIN (f_fpr);
2649   f_ovf = DECL_CHAIN (f_res);
2650   f_sav = DECL_CHAIN (f_ovf);
2651 
2652   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
2653   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
2654                     f_fpr, NULL_TREE);
2655   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
2656                     f_ovf, NULL_TREE);
2657   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
2658                     f_sav, NULL_TREE);
2659 
2660   size = int_size_in_bytes (type);
2661   rsize = (size + 3) / 4;
2662   int pad = 4 * rsize - size;
2663   align = 1;
2664 
2665   machine_mode mode = TYPE_MODE (type);
2666   if (abi_v4_pass_in_fpr (mode, false))
2667     {
2668       /* FP args go in FP registers, if present.  */
2669       reg = fpr;
2670       n_reg = (size + 7) / 8;
2671       sav_ofs = (TARGET_HARD_FLOAT ? 8 : 4) * 4;
2672       sav_scale = (TARGET_HARD_FLOAT ? 8 : 4);
2673       if (mode != SFmode && mode != SDmode)
2674           align = 8;
2675     }
2676   else
2677     {
2678       /* Otherwise into GP registers.  */
2679       reg = gpr;
2680       n_reg = rsize;
2681       sav_ofs = 0;
2682       sav_scale = 4;
2683       if (n_reg == 2)
2684           align = 8;
2685     }
2686 
2687   /* Pull the value out of the saved registers....  */
2688 
2689   lab_over = NULL;
2690   addr = create_tmp_var (ptr_type_node, "addr");
2691 
2692   /*  AltiVec vectors never go in registers when -mabi=altivec.  */
2693   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
2694     align = 16;
2695   else
2696     {
2697       lab_false = create_artificial_label (input_location);
2698       lab_over = create_artificial_label (input_location);
2699 
2700       /* Long long is aligned in the registers.  As are any other 2 gpr
2701            item such as complex int due to a historical mistake.  */
2702       u = reg;
2703       if (n_reg == 2 && reg == gpr)
2704           {
2705             regalign = 1;
2706             u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
2707                          build_int_cst (TREE_TYPE (reg), n_reg - 1));
2708             u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
2709                           unshare_expr (reg), u);
2710           }
2711       /* _Decimal128 is passed in even/odd fpr pairs; the stored
2712            reg number is 0 for f1, so we want to make it odd.  */
2713       else if (reg == fpr && mode == TDmode)
2714           {
2715             t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
2716                           build_int_cst (TREE_TYPE (reg), 1));
2717             u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
2718           }
2719 
2720       t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
2721       t = build2 (GE_EXPR, boolean_type_node, u, t);
2722       u = build1 (GOTO_EXPR, void_type_node, lab_false);
2723       t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
2724       gimplify_and_add (t, pre_p);
2725 
2726       t = sav;
2727       if (sav_ofs)
2728           t = fold_build_pointer_plus_hwi (sav, sav_ofs);
2729 
2730       u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
2731                       build_int_cst (TREE_TYPE (reg), n_reg));
2732       u = fold_convert (sizetype, u);
2733       u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
2734       t = fold_build_pointer_plus (t, u);
2735 
2736       /* _Decimal32 varargs are located in the second word of the 64-bit
2737            FP register for 32-bit binaries.  */
2738       if (TARGET_32BIT && TARGET_HARD_FLOAT && mode == SDmode)
2739           t = fold_build_pointer_plus_hwi (t, size);
2740 
2741       /* Args are passed right-aligned.  */
2742       if (BYTES_BIG_ENDIAN)
2743           t = fold_build_pointer_plus_hwi (t, pad);
2744 
2745       gimplify_assign (addr, t, pre_p);
2746 
2747       gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
2748 
2749       stmt = gimple_build_label (lab_false);
2750       gimple_seq_add_stmt (pre_p, stmt);
2751 
2752       if ((n_reg == 2 && !regalign) || n_reg > 2)
2753           {
2754             /* Ensure that we don't find any more args in regs.
2755                Alignment has taken care of for special cases.  */
2756             gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
2757           }
2758     }
2759 
2760   /* ... otherwise out of the overflow area.  */
2761 
2762   /* Care for on-stack alignment if needed.  */
2763   t = ovf;
2764   if (align != 1)
2765     {
2766       t = fold_build_pointer_plus_hwi (t, align - 1);
2767       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
2768                       build_int_cst (TREE_TYPE (t), -align));
2769     }
2770 
2771   /* Args are passed right-aligned.  */
2772   if (BYTES_BIG_ENDIAN)
2773     t = fold_build_pointer_plus_hwi (t, pad);
2774 
2775   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
2776 
2777   gimplify_assign (unshare_expr (addr), t, pre_p);
2778 
2779   t = fold_build_pointer_plus_hwi (t, size);
2780   gimplify_assign (unshare_expr (ovf), t, pre_p);
2781 
2782   if (lab_over)
2783     {
2784       stmt = gimple_build_label (lab_over);
2785       gimple_seq_add_stmt (pre_p, stmt);
2786     }
2787 
2788   if (STRICT_ALIGNMENT
2789       && (TYPE_ALIGN (type)
2790             > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
2791     {
2792       /* The value (of type complex double, for example) may not be
2793            aligned in memory in the saved registers, so copy via a
2794            temporary.  (This is the same code as used for SPARC.)  */
2795       tree tmp = create_tmp_var (type, "va_arg_tmp");
2796       tree dest_addr = build_fold_addr_expr (tmp);
2797 
2798       tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
2799                                            3, dest_addr, addr, size_int (rsize * 4));
2800       TREE_ADDRESSABLE (tmp) = 1;
2801 
2802       gimplify_and_add (copy, pre_p);
2803       addr = dest_addr;
2804     }
2805 
2806   addr = fold_convert (ptrtype, addr);
2807   return build_va_arg_indirect_ref (addr);
2808 }
2809 
2810 rtx
swap_endian_selector_for_mode(machine_mode mode)2811 swap_endian_selector_for_mode (machine_mode mode)
2812 {
2813   unsigned int swap1[16] = {15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
2814   unsigned int swap2[16] = {7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8};
2815   unsigned int swap4[16] = {3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12};
2816   unsigned int swap8[16] = {1,0,3,2,5,4,7,6,9,8,11,10,13,12,15,14};
2817 
2818   unsigned int *swaparray, i;
2819   rtx perm[16];
2820 
2821   switch (mode)
2822     {
2823     case E_V1TImode:
2824       swaparray = swap1;
2825       break;
2826     case E_V2DFmode:
2827     case E_V2DImode:
2828       swaparray = swap2;
2829       break;
2830     case E_V4SFmode:
2831     case E_V4SImode:
2832       swaparray = swap4;
2833       break;
2834     case E_V8HImode:
2835       swaparray = swap8;
2836       break;
2837     default:
2838       gcc_unreachable ();
2839     }
2840 
2841   for (i = 0; i < 16; ++i)
2842     perm[i] = GEN_INT (swaparray[i]);
2843 
2844   return force_reg (V16QImode, gen_rtx_CONST_VECTOR (V16QImode,
2845                                                                  gen_rtvec_v (16, perm)));
2846 }
2847 
2848 /* Return the internal arg pointer used for function incoming
2849    arguments.  When -fsplit-stack, the arg pointer is r12 so we need
2850    to copy it to a pseudo in order for it to be preserved over calls
2851    and suchlike.  We'd really like to use a pseudo here for the
2852    internal arg pointer but data-flow analysis is not prepared to
2853    accept pseudos as live at the beginning of a function.  */
2854 
2855 rtx
rs6000_internal_arg_pointer(void)2856 rs6000_internal_arg_pointer (void)
2857 {
2858   if (flag_split_stack
2859      && (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (cfun->decl))
2860          == NULL))
2861 
2862     {
2863       if (cfun->machine->split_stack_arg_pointer == NULL_RTX)
2864           {
2865             rtx pat;
2866 
2867             cfun->machine->split_stack_arg_pointer = gen_reg_rtx (Pmode);
2868             REG_POINTER (cfun->machine->split_stack_arg_pointer) = 1;
2869 
2870             /* Put the pseudo initialization right after the note at the
2871                beginning of the function.  */
2872             pat = gen_rtx_SET (cfun->machine->split_stack_arg_pointer,
2873                                    gen_rtx_REG (Pmode, 12));
2874             push_topmost_sequence ();
2875             emit_insn_after (pat, get_insns ());
2876             pop_topmost_sequence ();
2877           }
2878       rtx ret = plus_constant (Pmode, cfun->machine->split_stack_arg_pointer,
2879                                      FIRST_PARM_OFFSET (current_function_decl));
2880       return copy_to_reg (ret);
2881     }
2882   return virtual_incoming_args_rtx;
2883 }
2884 
2885 
2886 /* A C compound statement that outputs the assembler code for a thunk
2887    function, used to implement C++ virtual function calls with
2888    multiple inheritance.  The thunk acts as a wrapper around a virtual
2889    function, adjusting the implicit object parameter before handing
2890    control off to the real function.
2891 
2892    First, emit code to add the integer DELTA to the location that
2893    contains the incoming first argument.  Assume that this argument
2894    contains a pointer, and is the one used to pass the `this' pointer
2895    in C++.  This is the incoming argument *before* the function
2896    prologue, e.g. `%o0' on a sparc.  The addition must preserve the
2897    values of all other incoming arguments.
2898 
2899    After the addition, emit code to jump to FUNCTION, which is a
2900    `FUNCTION_DECL'.  This is a direct pure jump, not a call, and does
2901    not touch the return address.  Hence returning from FUNCTION will
2902    return to whoever called the current `thunk'.
2903 
2904    The effect must be as if FUNCTION had been called directly with the
2905    adjusted first argument.  This macro is responsible for emitting
2906    all of the code for a thunk function; output_function_prologue()
2907    and output_function_epilogue() are not invoked.
2908 
2909    The THUNK_FNDECL is redundant.  (DELTA and FUNCTION have already
2910    been extracted from it.)  It might possibly be useful on some
2911    targets, but probably not.
2912 
2913    If you do not define this macro, the target-independent code in the
2914    C++ frontend will generate a less efficient heavyweight thunk that
2915    calls FUNCTION instead of jumping to it.  The generic approach does
2916    not support varargs.  */
2917 
2918 void
rs6000_output_mi_thunk(FILE * file,tree thunk_fndecl ATTRIBUTE_UNUSED,HOST_WIDE_INT delta,HOST_WIDE_INT vcall_offset,tree function)2919 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
2920                               HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
2921                               tree function)
2922 {
2923   const char *fnname = get_fnname_from_decl (thunk_fndecl);
2924   rtx this_rtx, funexp;
2925   rtx_insn *insn;
2926 
2927   reload_completed = 1;
2928   epilogue_completed = 1;
2929 
2930   /* Mark the end of the (empty) prologue.  */
2931   emit_note (NOTE_INSN_PROLOGUE_END);
2932 
2933   /* Find the "this" pointer.  If the function returns a structure,
2934      the structure return pointer is in r3.  */
2935   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
2936     this_rtx = gen_rtx_REG (Pmode, 4);
2937   else
2938     this_rtx = gen_rtx_REG (Pmode, 3);
2939 
2940   /* Apply the constant offset, if required.  */
2941   if (delta)
2942     emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
2943 
2944   /* Apply the offset from the vtable, if required.  */
2945   if (vcall_offset)
2946     {
2947       rtx vcall_offset_rtx = GEN_INT (vcall_offset);
2948       rtx tmp = gen_rtx_REG (Pmode, 12);
2949 
2950       emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
2951       if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
2952           {
2953             emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
2954             emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
2955           }
2956       else
2957           {
2958             rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
2959 
2960             emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
2961           }
2962       emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
2963     }
2964 
2965   /* Generate a tail call to the target function.  */
2966   if (!TREE_USED (function))
2967     {
2968       assemble_external (function);
2969       TREE_USED (function) = 1;
2970     }
2971   funexp = XEXP (DECL_RTL (function), 0);
2972   funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
2973 
2974   insn = emit_call_insn (gen_sibcall (funexp, const0_rtx, const0_rtx));
2975   SIBLING_CALL_P (insn) = 1;
2976   emit_barrier ();
2977 
2978   /* Run just enough of rest_of_compilation to get the insns emitted.
2979      There's not really enough bulk here to make other passes such as
2980      instruction scheduling worth while.  */
2981   insn = get_insns ();
2982   shorten_branches (insn);
2983   assemble_start_function (thunk_fndecl, fnname);
2984   final_start_function (insn, file, 1);
2985   final (insn, file, 1);
2986   final_end_function ();
2987   assemble_end_function (thunk_fndecl, fnname);
2988 
2989   reload_completed = 0;
2990   epilogue_completed = 0;
2991 }
2992