OSDN Git Service

2002-02-05 Aldy Hernandez <aldyh@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / config / rs6000 / rs6000.c
1 /* Subroutines used for code generation on IBM RS/6000.
2    Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 
3    2000, 2001, 2002 Free Software Foundation, Inc.
4    Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "real.h"
29 #include "insn-config.h"
30 #include "conditions.h"
31 #include "insn-attr.h"
32 #include "flags.h"
33 #include "recog.h"
34 #include "obstack.h"
35 #include "tree.h"
36 #include "expr.h"
37 #include "optabs.h"
38 #include "except.h"
39 #include "function.h"
40 #include "output.h"
41 #include "basic-block.h"
42 #include "integrate.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "hashtab.h"
46 #include "tm_p.h"
47 #include "target.h"
48 #include "target-def.h"
49 #include "langhooks.h"
50 #include "reload.h"
51
52 #ifndef TARGET_NO_PROTOTYPE
53 #define TARGET_NO_PROTOTYPE 0
54 #endif
55
56 #define min(A,B)        ((A) < (B) ? (A) : (B))
57 #define max(A,B)        ((A) > (B) ? (A) : (B))
58
59 /* Target cpu type */
60
61 enum processor_type rs6000_cpu;
62 struct rs6000_cpu_select rs6000_select[3] =
63 {
64   /* switch             name,                   tune    arch */
65   { (const char *)0,    "--with-cpu=",          1,      1 },
66   { (const char *)0,    "-mcpu=",               1,      1 },
67   { (const char *)0,    "-mtune=",              1,      0 },
68 };
69
70 /* Size of long double */
71 const char *rs6000_long_double_size_string;
72 int rs6000_long_double_type_size;
73
74 /* Whether -mabi=altivec has appeared */
75 int rs6000_altivec_abi;
76
77 /* Set to non-zero once AIX common-mode calls have been defined.  */
78 static int common_mode_defined;
79
80 /* Save information from a "cmpxx" operation until the branch or scc is
81    emitted.  */
82 rtx rs6000_compare_op0, rs6000_compare_op1;
83 int rs6000_compare_fp_p;
84
85 /* Label number of label created for -mrelocatable, to call to so we can
86    get the address of the GOT section */
87 int rs6000_pic_labelno;
88
89 #ifdef USING_ELFOS_H
90 /* Which abi to adhere to */
91 const char *rs6000_abi_name = RS6000_ABI_NAME;
92
93 /* Semantics of the small data area */
94 enum rs6000_sdata_type rs6000_sdata = SDATA_DATA;
95
96 /* Which small data model to use */
97 const char *rs6000_sdata_name = (char *)0;
98
99 /* Counter for labels which are to be placed in .fixup.  */
100 int fixuplabelno = 0;
101 #endif
102
103 /* ABI enumeration available for subtarget to use.  */
104 enum rs6000_abi rs6000_current_abi;
105
106 /* ABI string from -mabi= option.  */
107 const char *rs6000_abi_string;
108
109 /* Debug flags */
110 const char *rs6000_debug_name;
111 int rs6000_debug_stack;         /* debug stack applications */
112 int rs6000_debug_arg;           /* debug argument handling */
113
114 /* Flag to say the TOC is initialized */
115 int toc_initialized;
116 char toc_label_name[10];
117
118 /* Alias set for saves and restores from the rs6000 stack.  */
119 static int rs6000_sr_alias_set;
120
121 static void rs6000_add_gc_roots PARAMS ((void));
122 static int num_insns_constant_wide PARAMS ((HOST_WIDE_INT));
123 static rtx expand_block_move_mem PARAMS ((enum machine_mode, rtx, rtx));
124 static void validate_condition_mode 
125   PARAMS ((enum rtx_code, enum machine_mode));
126 static rtx rs6000_generate_compare PARAMS ((enum rtx_code));
127 static void rs6000_maybe_dead PARAMS ((rtx));
128 static void rs6000_emit_stack_tie PARAMS ((void));
129 static void rs6000_frame_related PARAMS ((rtx, rtx, HOST_WIDE_INT, rtx, rtx));
130 static void rs6000_emit_allocate_stack PARAMS ((HOST_WIDE_INT, int));
131 static unsigned rs6000_hash_constant PARAMS ((rtx));
132 static unsigned toc_hash_function PARAMS ((const void *));
133 static int toc_hash_eq PARAMS ((const void *, const void *));
134 static int toc_hash_mark_entry PARAMS ((void **, void *));
135 static void toc_hash_mark_table PARAMS ((void *));
136 static int constant_pool_expr_1 PARAMS ((rtx, int *, int *));
137 static void rs6000_free_machine_status PARAMS ((struct function *));
138 static void rs6000_init_machine_status PARAMS ((struct function *));
139 static bool rs6000_assemble_integer PARAMS ((rtx, unsigned int, int));
140 static int rs6000_ra_ever_killed PARAMS ((void));
141 static tree rs6000_handle_longcall_attribute PARAMS ((tree *, tree, tree, int, bool *));
142 const struct attribute_spec rs6000_attribute_table[];
143 static void rs6000_output_function_prologue PARAMS ((FILE *, HOST_WIDE_INT));
144 static void rs6000_output_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
145 static rtx rs6000_emit_set_long_const PARAMS ((rtx,
146   HOST_WIDE_INT, HOST_WIDE_INT));
147 #if TARGET_ELF
148 static unsigned int rs6000_elf_section_type_flags PARAMS ((tree, const char *,
149                                                            int));
150 static void rs6000_elf_asm_out_constructor PARAMS ((rtx, int));
151 static void rs6000_elf_asm_out_destructor PARAMS ((rtx, int));
152 #endif
153 #ifdef OBJECT_FORMAT_COFF
154 static void xcoff_asm_named_section PARAMS ((const char *, unsigned int));
155 #endif
156 static int rs6000_adjust_cost PARAMS ((rtx, rtx, rtx, int));
157 static int rs6000_adjust_priority PARAMS ((rtx, int));
158 static int rs6000_issue_rate PARAMS ((void));
159
160 static void rs6000_init_builtins PARAMS ((void));
161 static void altivec_init_builtins PARAMS ((void));
162 static rtx rs6000_expand_builtin PARAMS ((tree, rtx, rtx, enum machine_mode, int));
163 static rtx altivec_expand_builtin PARAMS ((tree, rtx));
164 static rtx altivec_expand_unop_builtin PARAMS ((enum insn_code, tree, rtx));
165 static rtx altivec_expand_binop_builtin PARAMS ((enum insn_code, tree, rtx));
166 static rtx altivec_expand_predicate_builtin PARAMS ((enum insn_code, const char *, tree, rtx));
167 static rtx altivec_expand_ternop_builtin PARAMS ((enum insn_code, tree, rtx));
168 static rtx altivec_expand_stv_builtin PARAMS ((enum insn_code, tree));
169 static void rs6000_parse_abi_options PARAMS ((void));
170 static int first_altivec_reg_to_save PARAMS ((void));
171 static unsigned int compute_vrsave_mask PARAMS ((void));
172 static void is_altivec_return_reg PARAMS ((rtx, void *));
173 int vrsave_operation PARAMS ((rtx, enum machine_mode));
174 static rtx generate_set_vrsave PARAMS ((rtx, rs6000_stack_t *, int));
175 static void altivec_frame_fixup PARAMS ((rtx, rtx, HOST_WIDE_INT));
176 \f
177 /* Default register names.  */
178 char rs6000_reg_names[][8] =
179 {
180       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
181       "8",  "9", "10", "11", "12", "13", "14", "15",
182      "16", "17", "18", "19", "20", "21", "22", "23",
183      "24", "25", "26", "27", "28", "29", "30", "31",
184       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
185       "8",  "9", "10", "11", "12", "13", "14", "15",
186      "16", "17", "18", "19", "20", "21", "22", "23",
187      "24", "25", "26", "27", "28", "29", "30", "31",
188      "mq", "lr", "ctr","ap",
189       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
190       "xer",
191       /* AltiVec registers.  */
192       "0",  "1",  "2",  "3",  "4",  "5",  "6", "7",
193       "8",  "9",  "10", "11", "12", "13", "14", "15",
194       "16", "17", "18", "19", "20", "21", "22", "23",
195       "24", "25", "26", "27", "28", "29", "30", "31",
196       "vrsave"
197 };
198
199 #ifdef TARGET_REGNAMES
200 static const char alt_reg_names[][8] =
201 {
202    "%r0",   "%r1",  "%r2",  "%r3",  "%r4",  "%r5",  "%r6",  "%r7",
203    "%r8",   "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
204   "%r16",  "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
205   "%r24",  "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
206    "%f0",   "%f1",  "%f2",  "%f3",  "%f4",  "%f5",  "%f6",  "%f7",
207    "%f8",   "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
208   "%f16",  "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
209   "%f24",  "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
210     "mq",    "lr",  "ctr",   "ap",
211   "%cr0",  "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
212    "xer",
213    /* AltiVec registers.  */
214    "%v0",  "%v1",  "%v2",  "%v3",  "%v4",  "%v5",  "%v6", "%v7",
215    "%v8",  "%v9",  "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
216    "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
217    "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
218    "%vrsave"
219 };
220 #endif
221 \f
222 #ifndef MASK_STRICT_ALIGN
223 #define MASK_STRICT_ALIGN 0
224 #endif
225 \f
226 /* Initialize the GCC target structure.  */
227 #undef TARGET_ATTRIBUTE_TABLE
228 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
229
230 #undef TARGET_ASM_ALIGNED_DI_OP
231 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
232
233 /* Default unaligned ops are only provided for ELF.  Find the ops needed
234    for non-ELF systems.  */
235 #ifndef OBJECT_FORMAT_ELF
236 #ifdef OBJECT_FORMAT_COFF
237 /* For XCOFF.  rs6000_assemble_integer will handle unaligned DIs on
238    64-bit targets.  */
239 #undef TARGET_ASM_UNALIGNED_HI_OP
240 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
241 #undef TARGET_ASM_UNALIGNED_SI_OP
242 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
243 #undef TARGET_ASM_UNALIGNED_DI_OP
244 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
245 #else
246 /* For Darwin.  */
247 #undef TARGET_ASM_UNALIGNED_HI_OP
248 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
249 #undef TARGET_ASM_UNALIGNED_SI_OP
250 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
251 #endif
252 #endif
253
254 /* This hook deals with fixups for relocatable code and DI-mode objects
255    in 64-bit code.  */
256 #undef TARGET_ASM_INTEGER
257 #define TARGET_ASM_INTEGER rs6000_assemble_integer
258
259 #undef TARGET_ASM_FUNCTION_PROLOGUE
260 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
261 #undef TARGET_ASM_FUNCTION_EPILOGUE
262 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
263
264 #if TARGET_ELF
265 #undef TARGET_SECTION_TYPE_FLAGS
266 #define TARGET_SECTION_TYPE_FLAGS  rs6000_elf_section_type_flags
267 #endif
268
269 #undef TARGET_SCHED_ISSUE_RATE
270 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
271 #undef TARGET_SCHED_ADJUST_COST
272 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
273 #undef TARGET_SCHED_ADJUST_PRIORITY
274 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
275
276 #undef TARGET_INIT_BUILTINS
277 #define TARGET_INIT_BUILTINS rs6000_init_builtins
278
279 #undef TARGET_EXPAND_BUILTIN
280 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
281
282 /* The VRSAVE bitmask puts bit %v0 as the most significant bit.  */
283 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
284
285 struct gcc_target targetm = TARGET_INITIALIZER;
286 \f
287 /* Override command line options.  Mostly we process the processor
288    type and sometimes adjust other TARGET_ options.  */
289
290 void
291 rs6000_override_options (default_cpu)
292      const char *default_cpu;
293 {
294   size_t i, j;
295   struct rs6000_cpu_select *ptr;
296
297   /* Simplify the entries below by making a mask for any POWER
298      variant and any PowerPC variant.  */
299
300 #define POWER_MASKS (MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING)
301 #define POWERPC_MASKS (MASK_POWERPC | MASK_PPC_GPOPT \
302                        | MASK_PPC_GFXOPT | MASK_POWERPC64)
303 #define POWERPC_OPT_MASKS (MASK_PPC_GPOPT | MASK_PPC_GFXOPT)
304
305   static struct ptt
306     {
307       const char *const name;           /* Canonical processor name.  */
308       const enum processor_type processor; /* Processor type enum value.  */
309       const int target_enable;  /* Target flags to enable.  */
310       const int target_disable; /* Target flags to disable.  */
311     } const processor_target_table[]
312       = {{"common", PROCESSOR_COMMON, MASK_NEW_MNEMONICS,
313             POWER_MASKS | POWERPC_MASKS},
314          {"power", PROCESSOR_POWER,
315             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
316             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
317          {"power2", PROCESSOR_POWER,
318             MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING,
319             POWERPC_MASKS | MASK_NEW_MNEMONICS},
320          {"power3", PROCESSOR_PPC630,
321             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
322             POWER_MASKS | MASK_PPC_GPOPT},
323          {"powerpc", PROCESSOR_POWERPC,
324             MASK_POWERPC | MASK_NEW_MNEMONICS,
325             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
326          {"powerpc64", PROCESSOR_POWERPC64,
327             MASK_POWERPC | MASK_POWERPC64 | MASK_NEW_MNEMONICS,
328             POWER_MASKS | POWERPC_OPT_MASKS},
329          {"rios", PROCESSOR_RIOS1,
330             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
331             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
332          {"rios1", PROCESSOR_RIOS1,
333             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
334             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
335          {"rsc", PROCESSOR_PPC601,
336             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
337             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
338          {"rsc1", PROCESSOR_PPC601,
339             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
340             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
341          {"rios2", PROCESSOR_RIOS2,
342             MASK_POWER | MASK_MULTIPLE | MASK_STRING | MASK_POWER2,
343             POWERPC_MASKS | MASK_NEW_MNEMONICS},
344          {"rs64a", PROCESSOR_RS64A,
345             MASK_POWERPC | MASK_NEW_MNEMONICS,
346             POWER_MASKS | POWERPC_OPT_MASKS},
347          {"401", PROCESSOR_PPC403,
348             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
349             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
350          {"403", PROCESSOR_PPC403,
351             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS | MASK_STRICT_ALIGN,
352             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
353          {"405", PROCESSOR_PPC405,
354             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
355             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
356          {"505", PROCESSOR_MPCCORE,
357             MASK_POWERPC | MASK_NEW_MNEMONICS,
358             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
359          {"601", PROCESSOR_PPC601,
360             MASK_POWER | MASK_POWERPC | MASK_NEW_MNEMONICS | MASK_MULTIPLE | MASK_STRING,
361             MASK_POWER2 | POWERPC_OPT_MASKS | MASK_POWERPC64},
362          {"602", PROCESSOR_PPC603,
363             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
364             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
365          {"603", PROCESSOR_PPC603,
366             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
367             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
368          {"603e", PROCESSOR_PPC603,
369             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
370             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
371          {"ec603e", PROCESSOR_PPC603,
372             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
373             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
374          {"604", PROCESSOR_PPC604,
375             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
376             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
377          {"604e", PROCESSOR_PPC604e,
378             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
379             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
380          {"620", PROCESSOR_PPC620,
381             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
382             POWER_MASKS | MASK_PPC_GPOPT},
383          {"630", PROCESSOR_PPC630,
384             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
385             POWER_MASKS | MASK_PPC_GPOPT},
386          {"740", PROCESSOR_PPC750,
387             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
388             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
389          {"750", PROCESSOR_PPC750,
390             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
391             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
392          {"7400", PROCESSOR_PPC7400,
393             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
394             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
395          {"7450", PROCESSOR_PPC7450,
396             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
397             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
398          {"801", PROCESSOR_MPCCORE,
399             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
400             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
401          {"821", PROCESSOR_MPCCORE,
402             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
403             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
404          {"823", PROCESSOR_MPCCORE,
405             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
406             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
407          {"860", PROCESSOR_MPCCORE,
408             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
409             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64}};
410
411   size_t ptt_size = sizeof (processor_target_table) / sizeof (struct ptt);
412
413   /* Save current -mmultiple/-mno-multiple status.  */
414   int multiple = TARGET_MULTIPLE;
415   /* Save current -mstring/-mno-string status.  */
416   int string = TARGET_STRING;
417
418   /* Identify the processor type.  */
419   rs6000_select[0].string = default_cpu;
420   rs6000_cpu = TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT;
421
422   for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
423     {
424       ptr = &rs6000_select[i];
425       if (ptr->string != (char *)0 && ptr->string[0] != '\0')
426         {
427           for (j = 0; j < ptt_size; j++)
428             if (! strcmp (ptr->string, processor_target_table[j].name))
429               {
430                 if (ptr->set_tune_p)
431                   rs6000_cpu = processor_target_table[j].processor;
432
433                 if (ptr->set_arch_p)
434                   {
435                     target_flags |= processor_target_table[j].target_enable;
436                     target_flags &= ~processor_target_table[j].target_disable;
437                   }
438                 break;
439               }
440
441           if (j == ptt_size)
442             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
443         }
444     }
445
446   /* If we are optimizing big endian systems for space, use the store
447      multiple instructions.  */
448   if (BYTES_BIG_ENDIAN && optimize_size)
449     target_flags |= MASK_MULTIPLE;
450
451   /* If -mmultiple or -mno-multiple was explicitly used, don't
452      override with the processor default */
453   if (TARGET_MULTIPLE_SET)
454     target_flags = (target_flags & ~MASK_MULTIPLE) | multiple;
455
456   /* If -mstring or -mno-string was explicitly used, don't override
457      with the processor default.  */
458   if (TARGET_STRING_SET)
459     target_flags = (target_flags & ~MASK_STRING) | string;
460
461   /* Don't allow -mmultiple or -mstring on little endian systems
462      unless the cpu is a 750, because the hardware doesn't support the
463      instructions used in little endian mode, and causes an alignment
464      trap.  The 750 does not cause an alignment trap (except when the
465      target is unaligned).  */
466
467   if (! BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
468     {
469       if (TARGET_MULTIPLE)
470         {
471           target_flags &= ~MASK_MULTIPLE;
472           if (TARGET_MULTIPLE_SET)
473             warning ("-mmultiple is not supported on little endian systems");
474         }
475
476       if (TARGET_STRING)
477         {
478           target_flags &= ~MASK_STRING;
479           if (TARGET_STRING_SET)
480             warning ("-mstring is not supported on little endian systems");
481         }
482     }
483
484   if (flag_pic && DEFAULT_ABI == ABI_AIX)
485     {
486       warning ("-f%s ignored (all code is position independent)",
487                (flag_pic > 1) ? "PIC" : "pic");
488       flag_pic = 0;
489     }
490
491 #ifdef XCOFF_DEBUGGING_INFO
492   if (flag_function_sections && (write_symbols != NO_DEBUG)
493       && DEFAULT_ABI == ABI_AIX)
494     {
495       warning ("-ffunction-sections disabled on AIX when debugging");
496       flag_function_sections = 0;
497     }
498
499   if (flag_data_sections && (DEFAULT_ABI == ABI_AIX))
500     {
501       warning ("-fdata-sections not supported on AIX");
502       flag_data_sections = 0;
503     }
504 #endif
505
506   /* Set debug flags */
507   if (rs6000_debug_name)
508     {
509       if (! strcmp (rs6000_debug_name, "all"))
510         rs6000_debug_stack = rs6000_debug_arg = 1;
511       else if (! strcmp (rs6000_debug_name, "stack"))
512         rs6000_debug_stack = 1;
513       else if (! strcmp (rs6000_debug_name, "arg"))
514         rs6000_debug_arg = 1;
515       else
516         error ("unknown -mdebug-%s switch", rs6000_debug_name);
517     }
518
519   /* Set size of long double */
520   rs6000_long_double_type_size = 64;
521   if (rs6000_long_double_size_string)
522     {
523       char *tail;
524       int size = strtol (rs6000_long_double_size_string, &tail, 10);
525       if (*tail != '\0' || (size != 64 && size != 128))
526         error ("Unknown switch -mlong-double-%s",
527                rs6000_long_double_size_string);
528       else
529         rs6000_long_double_type_size = size;
530     }
531
532   /* Handle -mabi= options.  */
533   rs6000_parse_abi_options ();
534
535 #ifdef TARGET_REGNAMES
536   /* If the user desires alternate register names, copy in the
537      alternate names now.  */
538   if (TARGET_REGNAMES)
539     memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
540 #endif
541
542 #ifdef SUBTARGET_OVERRIDE_OPTIONS
543   SUBTARGET_OVERRIDE_OPTIONS;
544 #endif
545 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
546   SUBSUBTARGET_OVERRIDE_OPTIONS;
547 #endif
548
549   /* Set TARGET_AIX_STRUCT_RET last, after the ABI is determined.
550      If -maix-struct-return or -msvr4-struct-return was explicitly
551      used, don't override with the ABI default.  */
552   if (!(target_flags & MASK_AIX_STRUCT_RET_SET))
553     {
554       if (DEFAULT_ABI == ABI_V4 && !DRAFT_V4_STRUCT_RET)
555         target_flags = (target_flags & ~MASK_AIX_STRUCT_RET);
556       else
557         target_flags |= MASK_AIX_STRUCT_RET;
558     }
559
560   /* Register global variables with the garbage collector.  */
561   rs6000_add_gc_roots ();
562
563   /* Allocate an alias set for register saves & restores from stack.  */
564   rs6000_sr_alias_set = new_alias_set ();
565
566   if (TARGET_TOC) 
567     ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
568
569   /* We can only guarantee the availability of DI pseudo-ops when
570      assembling for 64-bit targets.  */
571   if (!TARGET_64BIT)
572     {
573       targetm.asm_out.aligned_op.di = NULL;
574       targetm.asm_out.unaligned_op.di = NULL;
575     }
576
577   /* Arrange to save and restore machine status around nested functions.  */
578   init_machine_status = rs6000_init_machine_status;
579   free_machine_status = rs6000_free_machine_status;
580 }
581
582 /* Handle -mabi= options.  */
583 static void
584 rs6000_parse_abi_options ()
585 {
586   if (rs6000_abi_string == 0)
587     return;
588   else if (! strcmp (rs6000_abi_string, "altivec"))
589     rs6000_altivec_abi = 1;
590   else
591     error ("unknown ABI specified: '%s'", rs6000_abi_string);
592 }
593
594 void
595 optimization_options (level, size)
596      int level ATTRIBUTE_UNUSED;
597      int size ATTRIBUTE_UNUSED;
598 {
599 }
600 \f
601 /* Do anything needed at the start of the asm file.  */
602
603 void
604 rs6000_file_start (file, default_cpu)
605      FILE *file;
606      const char *default_cpu;
607 {
608   size_t i;
609   char buffer[80];
610   const char *start = buffer;
611   struct rs6000_cpu_select *ptr;
612
613   if (flag_verbose_asm)
614     {
615       sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
616       rs6000_select[0].string = default_cpu;
617
618       for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
619         {
620           ptr = &rs6000_select[i];
621           if (ptr->string != (char *)0 && ptr->string[0] != '\0')
622             {
623               fprintf (file, "%s %s%s", start, ptr->name, ptr->string);
624               start = "";
625             }
626         }
627
628 #ifdef USING_ELFOS_H
629       switch (rs6000_sdata)
630         {
631         case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
632         case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
633         case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
634         case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
635         }
636
637       if (rs6000_sdata && g_switch_value)
638         {
639           fprintf (file, "%s -G %d", start, g_switch_value);
640           start = "";
641         }
642 #endif
643
644       if (*start == '\0')
645         putc ('\n', file);
646     }
647 }
648
649 \f
650 /* Create a CONST_DOUBLE from a string.  */
651
652 struct rtx_def *
653 rs6000_float_const (string, mode)
654      const char *string;
655      enum machine_mode mode;
656 {
657   REAL_VALUE_TYPE value;
658   value = REAL_VALUE_ATOF (string, mode);
659   return immed_real_const_1 (value, mode);
660 }
661 \f
662 /* Return non-zero if this function is known to have a null epilogue.  */
663
664 int
665 direct_return ()
666 {
667   if (reload_completed)
668     {
669       rs6000_stack_t *info = rs6000_stack_info ();
670
671       if (info->first_gp_reg_save == 32
672           && info->first_fp_reg_save == 64
673           && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
674           && ! info->lr_save_p
675           && ! info->cr_save_p
676           && info->vrsave_mask == 0
677           && ! info->push_p)
678         return 1;
679     }
680
681   return 0;
682 }
683
684 /* Returns 1 always.  */
685
686 int
687 any_operand (op, mode)
688      rtx op ATTRIBUTE_UNUSED;
689      enum machine_mode mode ATTRIBUTE_UNUSED;
690 {
691   return 1;
692 }
693
694 /* Returns 1 if op is the count register.  */
695 int
696 count_register_operand (op, mode)
697      rtx op;
698      enum machine_mode mode ATTRIBUTE_UNUSED;
699 {
700   if (GET_CODE (op) != REG)
701     return 0;
702
703   if (REGNO (op) == COUNT_REGISTER_REGNUM)
704     return 1;
705
706   if (REGNO (op) > FIRST_PSEUDO_REGISTER)
707     return 1;
708
709   return 0;
710 }
711
712 int
713 xer_operand (op, mode)
714      rtx op;
715      enum machine_mode mode ATTRIBUTE_UNUSED;
716 {
717   if (GET_CODE (op) != REG)
718     return 0;
719
720   if (XER_REGNO_P (REGNO (op)))
721     return 1;
722
723   return 0;
724 }
725
726 /* Return 1 if OP is a signed 8-bit constant.  Int multiplication
727    by such constants completes more quickly.  */
728
729 int
730 s8bit_cint_operand (op, mode)
731      rtx op;
732      enum machine_mode mode ATTRIBUTE_UNUSED;
733 {
734   return ( GET_CODE (op) == CONST_INT
735           && (INTVAL (op) >= -128 && INTVAL (op) <= 127));
736 }
737
738 /* Return 1 if OP is a constant that can fit in a D field.  */
739
740 int
741 short_cint_operand (op, mode)
742      rtx op;
743      enum machine_mode mode ATTRIBUTE_UNUSED;
744 {
745   return (GET_CODE (op) == CONST_INT
746           && CONST_OK_FOR_LETTER_P (INTVAL (op), 'I'));
747 }
748
749 /* Similar for an unsigned D field.  */
750
751 int
752 u_short_cint_operand (op, mode)
753      rtx op;
754      enum machine_mode mode ATTRIBUTE_UNUSED;
755 {
756   return (GET_CODE (op) == CONST_INT
757           && CONST_OK_FOR_LETTER_P (INTVAL (op), 'K'));
758 }
759
760 /* Return 1 if OP is a CONST_INT that cannot fit in a signed D field.  */
761
762 int
763 non_short_cint_operand (op, mode)
764      rtx op;
765      enum machine_mode mode ATTRIBUTE_UNUSED;
766 {
767   return (GET_CODE (op) == CONST_INT
768           && (unsigned HOST_WIDE_INT) (INTVAL (op) + 0x8000) >= 0x10000);
769 }
770
771 /* Returns 1 if OP is a CONST_INT that is a positive value
772    and an exact power of 2.  */
773
774 int
775 exact_log2_cint_operand (op, mode)
776      rtx op;
777      enum machine_mode mode ATTRIBUTE_UNUSED;
778 {
779   return (GET_CODE (op) == CONST_INT
780           && INTVAL (op) > 0
781           && exact_log2 (INTVAL (op)) >= 0);
782 }
783
784 /* Returns 1 if OP is a register that is not special (i.e., not MQ,
785    ctr, or lr).  */
786
787 int
788 gpc_reg_operand (op, mode)
789      rtx op;
790      enum machine_mode mode;
791 {
792   return (register_operand (op, mode)
793           && (GET_CODE (op) != REG
794               || (REGNO (op) >= ARG_POINTER_REGNUM 
795                   && !XER_REGNO_P (REGNO (op)))
796               || REGNO (op) < MQ_REGNO));
797 }
798
799 /* Returns 1 if OP is either a pseudo-register or a register denoting a
800    CR field.  */
801
802 int
803 cc_reg_operand (op, mode)
804      rtx op;
805      enum machine_mode mode;
806 {
807   return (register_operand (op, mode)
808           && (GET_CODE (op) != REG
809               || REGNO (op) >= FIRST_PSEUDO_REGISTER
810               || CR_REGNO_P (REGNO (op))));
811 }
812
813 /* Returns 1 if OP is either a pseudo-register or a register denoting a
814    CR field that isn't CR0.  */
815
816 int
817 cc_reg_not_cr0_operand (op, mode)
818      rtx op;
819      enum machine_mode mode;
820 {
821   return (register_operand (op, mode)
822           && (GET_CODE (op) != REG
823               || REGNO (op) >= FIRST_PSEUDO_REGISTER
824               || CR_REGNO_NOT_CR0_P (REGNO (op))));
825 }
826
827 /* Returns 1 if OP is either a constant integer valid for a D-field or
828    a non-special register.  If a register, it must be in the proper
829    mode unless MODE is VOIDmode.  */
830
831 int
832 reg_or_short_operand (op, mode)
833       rtx op;
834       enum machine_mode mode;
835 {
836   return short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
837 }
838
839 /* Similar, except check if the negation of the constant would be
840    valid for a D-field.  */
841
842 int
843 reg_or_neg_short_operand (op, mode)
844       rtx op;
845       enum machine_mode mode;
846 {
847   if (GET_CODE (op) == CONST_INT)
848     return CONST_OK_FOR_LETTER_P (INTVAL (op), 'P');
849
850   return gpc_reg_operand (op, mode);
851 }
852
853 /* Return 1 if the operand is either a register or an integer whose
854    high-order 16 bits are zero.  */
855
856 int
857 reg_or_u_short_operand (op, mode)
858      rtx op;
859      enum machine_mode mode;
860 {
861   return u_short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
862 }
863
864 /* Return 1 is the operand is either a non-special register or ANY
865    constant integer.  */
866
867 int
868 reg_or_cint_operand (op, mode)
869     rtx op;
870     enum machine_mode mode;
871 {
872   return (GET_CODE (op) == CONST_INT || gpc_reg_operand (op, mode));
873 }
874
875 /* Return 1 is the operand is either a non-special register or ANY
876    32-bit signed constant integer.  */
877
878 int
879 reg_or_arith_cint_operand (op, mode)
880     rtx op;
881     enum machine_mode mode;
882 {
883   return (gpc_reg_operand (op, mode)
884           || (GET_CODE (op) == CONST_INT
885 #if HOST_BITS_PER_WIDE_INT != 32
886               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80000000)
887                   < (unsigned HOST_WIDE_INT) 0x100000000ll)
888 #endif
889               ));
890 }
891
892 /* Return 1 is the operand is either a non-special register or a 32-bit
893    signed constant integer valid for 64-bit addition.  */
894
895 int
896 reg_or_add_cint64_operand (op, mode)
897     rtx op;
898     enum machine_mode mode;
899 {
900   return (gpc_reg_operand (op, mode)
901           || (GET_CODE (op) == CONST_INT
902               && INTVAL (op) < 0x7fff8000
903 #if HOST_BITS_PER_WIDE_INT != 32
904               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80008000)
905                   < 0x100000000ll)
906 #endif
907               ));
908 }
909
910 /* Return 1 is the operand is either a non-special register or a 32-bit
911    signed constant integer valid for 64-bit subtraction.  */
912
913 int
914 reg_or_sub_cint64_operand (op, mode)
915     rtx op;
916     enum machine_mode mode;
917 {
918   return (gpc_reg_operand (op, mode)
919           || (GET_CODE (op) == CONST_INT
920               && (- INTVAL (op)) < 0x7fff8000
921 #if HOST_BITS_PER_WIDE_INT != 32
922               && ((unsigned HOST_WIDE_INT) ((- INTVAL (op)) + 0x80008000)
923                   < 0x100000000ll)
924 #endif
925               ));
926 }
927
928 /* Return 1 is the operand is either a non-special register or ANY
929    32-bit unsigned constant integer.  */
930
931 int
932 reg_or_logical_cint_operand (op, mode)
933     rtx op;
934     enum machine_mode mode;
935 {
936   if (GET_CODE (op) == CONST_INT)
937     {
938       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
939         {
940           if (GET_MODE_BITSIZE (mode) <= 32)
941             abort ();
942
943           if (INTVAL (op) < 0)
944             return 0;
945         }
946
947       return ((INTVAL (op) & GET_MODE_MASK (mode)
948                & (~ (unsigned HOST_WIDE_INT) 0xffffffff)) == 0);
949     }
950   else if (GET_CODE (op) == CONST_DOUBLE)
951     {
952       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
953           || mode != DImode)
954         abort ();
955
956       return CONST_DOUBLE_HIGH (op) == 0;
957     }
958   else 
959     return gpc_reg_operand (op, mode);
960 }
961
962 /* Return 1 if the operand is an operand that can be loaded via the GOT.  */
963
964 int
965 got_operand (op, mode)
966      rtx op;
967      enum machine_mode mode ATTRIBUTE_UNUSED;
968 {
969   return (GET_CODE (op) == SYMBOL_REF
970           || GET_CODE (op) == CONST
971           || GET_CODE (op) == LABEL_REF);
972 }
973
974 /* Return 1 if the operand is a simple references that can be loaded via
975    the GOT (labels involving addition aren't allowed).  */
976
977 int
978 got_no_const_operand (op, mode)
979      rtx op;
980      enum machine_mode mode ATTRIBUTE_UNUSED;
981 {
982   return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF);
983 }
984
985 /* Return the number of instructions it takes to form a constant in an
986    integer register.  */
987
988 static int
989 num_insns_constant_wide (value)
990      HOST_WIDE_INT value;
991 {
992   /* signed constant loadable with {cal|addi} */
993   if (CONST_OK_FOR_LETTER_P (value, 'I'))
994     return 1;
995
996   /* constant loadable with {cau|addis} */
997   else if (CONST_OK_FOR_LETTER_P (value, 'L'))
998     return 1;
999
1000 #if HOST_BITS_PER_WIDE_INT == 64
1001   else if (TARGET_POWERPC64)
1002     {
1003       HOST_WIDE_INT low  = value & 0xffffffff;
1004       HOST_WIDE_INT high = value >> 32;
1005
1006       low = (low ^ 0x80000000) - 0x80000000;  /* sign extend */
1007
1008       if (high == 0 && (low & 0x80000000) == 0)
1009         return 2;
1010
1011       else if (high == -1 && (low & 0x80000000) != 0)
1012         return 2;
1013
1014       else if (! low)
1015         return num_insns_constant_wide (high) + 1;
1016
1017       else
1018         return (num_insns_constant_wide (high)
1019                 + num_insns_constant_wide (low) + 1);
1020     }
1021 #endif
1022
1023   else
1024     return 2;
1025 }
1026
1027 int
1028 num_insns_constant (op, mode)
1029      rtx op;
1030      enum machine_mode mode;
1031 {
1032   if (GET_CODE (op) == CONST_INT)
1033     {
1034 #if HOST_BITS_PER_WIDE_INT == 64
1035       if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
1036           && mask64_operand (op, mode))
1037             return 2;
1038       else
1039 #endif
1040         return num_insns_constant_wide (INTVAL (op));
1041     }
1042
1043   else if (GET_CODE (op) == CONST_DOUBLE && mode == SFmode)
1044     {
1045       long l;
1046       REAL_VALUE_TYPE rv;
1047
1048       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1049       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1050       return num_insns_constant_wide ((HOST_WIDE_INT)l);
1051     }
1052
1053   else if (GET_CODE (op) == CONST_DOUBLE)
1054     {
1055       HOST_WIDE_INT low;
1056       HOST_WIDE_INT high;
1057       long l[2];
1058       REAL_VALUE_TYPE rv;
1059       int endian = (WORDS_BIG_ENDIAN == 0);
1060
1061       if (mode == VOIDmode || mode == DImode)
1062         {
1063           high = CONST_DOUBLE_HIGH (op);
1064           low  = CONST_DOUBLE_LOW (op);
1065         }
1066       else
1067         {
1068           REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1069           REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
1070           high = l[endian];
1071           low  = l[1 - endian];
1072         }
1073
1074       if (TARGET_32BIT)
1075         return (num_insns_constant_wide (low)
1076                 + num_insns_constant_wide (high));
1077
1078       else
1079         {
1080           if (high == 0 && (low & 0x80000000) == 0)
1081             return num_insns_constant_wide (low);
1082
1083           else if (high == -1 && (low & 0x80000000) != 0)
1084             return num_insns_constant_wide (low);
1085
1086           else if (mask64_operand (op, mode))
1087             return 2;
1088
1089           else if (low == 0)
1090             return num_insns_constant_wide (high) + 1;
1091
1092           else
1093             return (num_insns_constant_wide (high)
1094                     + num_insns_constant_wide (low) + 1);
1095         }
1096     }
1097
1098   else
1099     abort ();
1100 }
1101
1102 /* Return 1 if the operand is a CONST_DOUBLE and it can be put into a
1103    register with one instruction per word.  We only do this if we can
1104    safely read CONST_DOUBLE_{LOW,HIGH}.  */
1105
1106 int
1107 easy_fp_constant (op, mode)
1108      rtx op;
1109      enum machine_mode mode;
1110 {
1111   if (GET_CODE (op) != CONST_DOUBLE
1112       || GET_MODE (op) != mode
1113       || (GET_MODE_CLASS (mode) != MODE_FLOAT && mode != DImode))
1114     return 0;
1115
1116   /* Consider all constants with -msoft-float to be easy.  */
1117   if (TARGET_SOFT_FLOAT && mode != DImode)
1118     return 1;
1119
1120   /* If we are using V.4 style PIC, consider all constants to be hard.  */
1121   if (flag_pic && DEFAULT_ABI == ABI_V4)
1122     return 0;
1123
1124 #ifdef TARGET_RELOCATABLE
1125   /* Similarly if we are using -mrelocatable, consider all constants
1126      to be hard.  */
1127   if (TARGET_RELOCATABLE)
1128     return 0;
1129 #endif
1130
1131   if (mode == DFmode)
1132     {
1133       long k[2];
1134       REAL_VALUE_TYPE rv;
1135
1136       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1137       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
1138
1139       return (num_insns_constant_wide ((HOST_WIDE_INT)k[0]) == 1
1140               && num_insns_constant_wide ((HOST_WIDE_INT)k[1]) == 1);
1141     }
1142
1143   else if (mode == SFmode)
1144     {
1145       long l;
1146       REAL_VALUE_TYPE rv;
1147
1148       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1149       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1150
1151       return num_insns_constant_wide (l) == 1;
1152     }
1153
1154   else if (mode == DImode)
1155     return ((TARGET_POWERPC64
1156              && GET_CODE (op) == CONST_DOUBLE && CONST_DOUBLE_LOW (op) == 0)
1157             || (num_insns_constant (op, DImode) <= 2));
1158
1159   else if (mode == SImode)
1160     return 1;
1161   else
1162     abort ();
1163 }
1164
1165 /* Return 1 if the operand is 0.0.  */
1166 int
1167 zero_fp_constant (op, mode)
1168      rtx op;
1169      enum machine_mode mode;
1170 {
1171   return GET_MODE_CLASS (mode) == MODE_FLOAT && op == CONST0_RTX (mode);
1172 }
1173
1174 /* Return 1 if the operand is in volatile memory.  Note that during
1175    the RTL generation phase, memory_operand does not return TRUE for
1176    volatile memory references.  So this function allows us to
1177    recognize volatile references where its safe.  */
1178
1179 int
1180 volatile_mem_operand (op, mode)
1181      rtx op;
1182      enum machine_mode mode;
1183 {
1184   if (GET_CODE (op) != MEM)
1185     return 0;
1186
1187   if (!MEM_VOLATILE_P (op))
1188     return 0;
1189
1190   if (mode != GET_MODE (op))
1191     return 0;
1192
1193   if (reload_completed)
1194     return memory_operand (op, mode);
1195
1196   if (reload_in_progress)
1197     return strict_memory_address_p (mode, XEXP (op, 0));
1198
1199   return memory_address_p (mode, XEXP (op, 0));
1200 }
1201
1202 /* Return 1 if the operand is an offsettable memory operand.  */
1203
1204 int
1205 offsettable_mem_operand (op, mode)
1206      rtx op;
1207      enum machine_mode mode;
1208 {
1209   return ((GET_CODE (op) == MEM)
1210           && offsettable_address_p (reload_completed || reload_in_progress,
1211                                     mode, XEXP (op, 0)));
1212 }
1213
1214 /* Return 1 if the operand is either an easy FP constant (see above) or
1215    memory.  */
1216
1217 int
1218 mem_or_easy_const_operand (op, mode)
1219      rtx op;
1220      enum machine_mode mode;
1221 {
1222   return memory_operand (op, mode) || easy_fp_constant (op, mode);
1223 }
1224
1225 /* Return 1 if the operand is either a non-special register or an item
1226    that can be used as the operand of a `mode' add insn.  */
1227
1228 int
1229 add_operand (op, mode)
1230     rtx op;
1231     enum machine_mode mode;
1232 {
1233   if (GET_CODE (op) == CONST_INT)
1234     return (CONST_OK_FOR_LETTER_P (INTVAL(op), 'I')
1235             || CONST_OK_FOR_LETTER_P (INTVAL(op), 'L'));
1236
1237   return gpc_reg_operand (op, mode);
1238 }
1239
1240 /* Return 1 if OP is a constant but not a valid add_operand.  */
1241
1242 int
1243 non_add_cint_operand (op, mode)
1244      rtx op;
1245      enum machine_mode mode ATTRIBUTE_UNUSED;
1246 {
1247   return (GET_CODE (op) == CONST_INT
1248           && (unsigned HOST_WIDE_INT) (INTVAL (op) + 0x8000) >= 0x10000
1249           && ! CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
1250 }
1251
1252 /* Return 1 if the operand is a non-special register or a constant that
1253    can be used as the operand of an OR or XOR insn on the RS/6000.  */
1254
1255 int
1256 logical_operand (op, mode)
1257      rtx op;
1258      enum machine_mode mode;
1259 {
1260   HOST_WIDE_INT opl, oph;
1261
1262   if (gpc_reg_operand (op, mode))
1263     return 1;
1264
1265   if (GET_CODE (op) == CONST_INT)
1266     {
1267       opl = INTVAL (op) & GET_MODE_MASK (mode);
1268
1269 #if HOST_BITS_PER_WIDE_INT <= 32
1270       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT && opl < 0)
1271         return 0;
1272 #endif
1273     }
1274   else if (GET_CODE (op) == CONST_DOUBLE)
1275     {
1276       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
1277         abort ();
1278
1279       opl = CONST_DOUBLE_LOW (op);
1280       oph = CONST_DOUBLE_HIGH (op);
1281       if (oph != 0)
1282         return 0;
1283     }
1284   else
1285     return 0;
1286
1287   return ((opl & ~ (unsigned HOST_WIDE_INT) 0xffff) == 0
1288           || (opl & ~ (unsigned HOST_WIDE_INT) 0xffff0000) == 0);
1289 }
1290
1291 /* Return 1 if C is a constant that is not a logical operand (as
1292    above), but could be split into one.  */
1293
1294 int
1295 non_logical_cint_operand (op, mode)
1296      rtx op;
1297      enum machine_mode mode;
1298 {
1299   return ((GET_CODE (op) == CONST_INT || GET_CODE (op) == CONST_DOUBLE)
1300           && ! logical_operand (op, mode)
1301           && reg_or_logical_cint_operand (op, mode));
1302 }
1303
1304 /* Return 1 if C is a constant that can be encoded in a 32-bit mask on the
1305    RS/6000.  It is if there are no more than two 1->0 or 0->1 transitions.
1306    Reject all ones and all zeros, since these should have been optimized
1307    away and confuse the making of MB and ME.  */
1308
1309 int
1310 mask_operand (op, mode)
1311      rtx op;
1312      enum machine_mode mode ATTRIBUTE_UNUSED;
1313 {
1314   HOST_WIDE_INT c, lsb;
1315
1316   if (GET_CODE (op) != CONST_INT)
1317     return 0;
1318
1319   c = INTVAL (op);
1320
1321   /* We don't change the number of transitions by inverting,
1322      so make sure we start with the LS bit zero.  */
1323   if (c & 1)
1324     c = ~c;
1325
1326   /* Reject all zeros or all ones.  */
1327   if (c == 0)
1328     return 0;
1329
1330   /* Find the first transition.  */
1331   lsb = c & -c;
1332
1333   /* Invert to look for a second transition.  */
1334   c = ~c;
1335
1336   /* Erase first transition.  */
1337   c &= -lsb;
1338
1339   /* Find the second transition (if any).  */
1340   lsb = c & -c;
1341
1342   /* Match if all the bits above are 1's (or c is zero).  */
1343   return c == -lsb;
1344 }
1345
1346 /* Return 1 if the operand is a constant that is a PowerPC64 mask.
1347    It is if there are no more than one 1->0 or 0->1 transitions.
1348    Reject all ones and all zeros, since these should have been optimized
1349    away and confuse the making of MB and ME.  */
1350
1351 int
1352 mask64_operand (op, mode)
1353      rtx op;
1354      enum machine_mode mode;
1355 {
1356   if (GET_CODE (op) == CONST_INT)
1357     {
1358       HOST_WIDE_INT c, lsb;
1359
1360       /* We don't change the number of transitions by inverting,
1361          so make sure we start with the LS bit zero.  */
1362       c = INTVAL (op);
1363       if (c & 1)
1364         c = ~c;
1365
1366       /* Reject all zeros or all ones.  */
1367       if (c == 0)
1368         return 0;
1369
1370       /* Find the transition, and check that all bits above are 1's.  */
1371       lsb = c & -c;
1372       return c == -lsb;
1373     }
1374   else if (GET_CODE (op) == CONST_DOUBLE
1375            && (mode == VOIDmode || mode == DImode))
1376     {
1377       HOST_WIDE_INT low, high, lsb;
1378
1379       if (HOST_BITS_PER_WIDE_INT < 64)
1380         high = CONST_DOUBLE_HIGH (op);
1381
1382       low = CONST_DOUBLE_LOW (op);
1383       if (low & 1)
1384         {
1385           if (HOST_BITS_PER_WIDE_INT < 64)
1386             high = ~high;
1387           low = ~low;
1388         }
1389
1390       if (low == 0)
1391         {
1392           if (HOST_BITS_PER_WIDE_INT >= 64 || high == 0)
1393             return 0;
1394
1395           lsb = high & -high;
1396           return high == -lsb;
1397         }
1398
1399       lsb = low & -low;
1400       return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
1401     }
1402   else
1403     return 0;
1404 }
1405
1406 /* Return 1 if the operand is either a non-special register or a constant
1407    that can be used as the operand of a PowerPC64 logical AND insn.  */
1408
1409 int
1410 and64_operand (op, mode)
1411     rtx op;
1412     enum machine_mode mode;
1413 {
1414   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
1415     return (gpc_reg_operand (op, mode) || mask64_operand (op, mode));
1416
1417   return (logical_operand (op, mode) || mask64_operand (op, mode));
1418 }
1419
1420 /* Return 1 if the operand is either a non-special register or a
1421    constant that can be used as the operand of an RS/6000 logical AND insn.  */
1422
1423 int
1424 and_operand (op, mode)
1425     rtx op;
1426     enum machine_mode mode;
1427 {
1428   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
1429     return (gpc_reg_operand (op, mode) || mask_operand (op, mode));
1430
1431   return (logical_operand (op, mode) || mask_operand (op, mode));
1432 }
1433
1434 /* Return 1 if the operand is a general register or memory operand.  */
1435
1436 int
1437 reg_or_mem_operand (op, mode)
1438      rtx op;
1439      enum machine_mode mode;
1440 {
1441   return (gpc_reg_operand (op, mode)
1442           || memory_operand (op, mode)
1443           || volatile_mem_operand (op, mode));
1444 }
1445
1446 /* Return 1 if the operand is a general register or memory operand without
1447    pre_inc or pre_dec which produces invalid form of PowerPC lwa
1448    instruction.  */
1449
1450 int
1451 lwa_operand (op, mode)
1452      rtx op;
1453      enum machine_mode mode;
1454 {
1455   rtx inner = op;
1456
1457   if (reload_completed && GET_CODE (inner) == SUBREG)
1458     inner = SUBREG_REG (inner);
1459     
1460   return gpc_reg_operand (inner, mode)
1461     || (memory_operand (inner, mode)
1462         && GET_CODE (XEXP (inner, 0)) != PRE_INC
1463         && GET_CODE (XEXP (inner, 0)) != PRE_DEC
1464         && (GET_CODE (XEXP (inner, 0)) != PLUS
1465             || GET_CODE (XEXP (XEXP (inner, 0), 1)) != CONST_INT
1466             || INTVAL (XEXP (XEXP (inner, 0), 1)) % 4 == 0));
1467 }
1468
1469 /* Return 1 if the operand, used inside a MEM, is a valid first argument
1470    to CALL.  This is a SYMBOL_REF or a pseudo-register, which will be
1471    forced to lr.  */
1472
1473 int
1474 call_operand (op, mode)
1475      rtx op;
1476      enum machine_mode mode;
1477 {
1478   if (mode != VOIDmode && GET_MODE (op) != mode)
1479     return 0;
1480
1481   return (GET_CODE (op) == SYMBOL_REF
1482           || (GET_CODE (op) == REG && REGNO (op) >= FIRST_PSEUDO_REGISTER));
1483 }
1484
1485 /* Return 1 if the operand is a SYMBOL_REF for a function known to be in
1486    this file and the function is not weakly defined.  */
1487
1488 int
1489 current_file_function_operand (op, mode)
1490      rtx op;
1491      enum machine_mode mode ATTRIBUTE_UNUSED;
1492 {
1493   return (GET_CODE (op) == SYMBOL_REF
1494           && (SYMBOL_REF_FLAG (op)
1495               || (op == XEXP (DECL_RTL (current_function_decl), 0)
1496                   && ! DECL_WEAK (current_function_decl))));
1497 }
1498
1499 /* Return 1 if this operand is a valid input for a move insn.  */
1500
1501 int
1502 input_operand (op, mode)
1503      rtx op;
1504      enum machine_mode mode;
1505 {
1506   /* Memory is always valid.  */
1507   if (memory_operand (op, mode))
1508     return 1;
1509
1510   /* Only a tiny bit of handling for CONSTANT_P_RTX is necessary.  */
1511   if (GET_CODE (op) == CONSTANT_P_RTX)
1512     return 1;
1513
1514   /* For floating-point, easy constants are valid.  */
1515   if (GET_MODE_CLASS (mode) == MODE_FLOAT
1516       && CONSTANT_P (op)
1517       && easy_fp_constant (op, mode))
1518     return 1;
1519
1520   /* Allow any integer constant.  */
1521   if (GET_MODE_CLASS (mode) == MODE_INT
1522       && (GET_CODE (op) == CONST_INT
1523           || GET_CODE (op) == CONST_DOUBLE))
1524     return 1;
1525
1526   /* For floating-point or multi-word mode, the only remaining valid type
1527      is a register.  */
1528   if (GET_MODE_CLASS (mode) == MODE_FLOAT
1529       || GET_MODE_SIZE (mode) > UNITS_PER_WORD)
1530     return register_operand (op, mode);
1531
1532   /* The only cases left are integral modes one word or smaller (we
1533      do not get called for MODE_CC values).  These can be in any
1534      register.  */
1535   if (register_operand (op, mode))
1536     return 1;
1537
1538   /* A SYMBOL_REF referring to the TOC is valid.  */
1539   if (LEGITIMATE_CONSTANT_POOL_ADDRESS_P (op))
1540     return 1;
1541
1542   /* A constant pool expression (relative to the TOC) is valid */
1543   if (TOC_RELATIVE_EXPR_P (op))
1544     return 1;
1545
1546   /* V.4 allows SYMBOL_REFs and CONSTs that are in the small data region
1547      to be valid.  */
1548   if (DEFAULT_ABI == ABI_V4
1549       && (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST)
1550       && small_data_operand (op, Pmode))
1551     return 1;
1552
1553   return 0;
1554 }
1555
1556 /* Return 1 for an operand in small memory on V.4/eabi.  */
1557
1558 int
1559 small_data_operand (op, mode)
1560      rtx op ATTRIBUTE_UNUSED;
1561      enum machine_mode mode ATTRIBUTE_UNUSED;
1562 {
1563 #if TARGET_ELF
1564   rtx sym_ref;
1565
1566   if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
1567     return 0;
1568
1569   if (DEFAULT_ABI != ABI_V4)
1570     return 0;
1571
1572   if (GET_CODE (op) == SYMBOL_REF)
1573     sym_ref = op;
1574
1575   else if (GET_CODE (op) != CONST
1576            || GET_CODE (XEXP (op, 0)) != PLUS
1577            || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
1578            || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
1579     return 0;
1580
1581   else
1582     {
1583       rtx sum = XEXP (op, 0);
1584       HOST_WIDE_INT summand;
1585
1586       /* We have to be careful here, because it is the referenced address
1587         that must be 32k from _SDA_BASE_, not just the symbol.  */
1588       summand = INTVAL (XEXP (sum, 1));
1589       if (summand < 0 || summand > g_switch_value)
1590        return 0;
1591
1592       sym_ref = XEXP (sum, 0);
1593     }
1594
1595   if (*XSTR (sym_ref, 0) != '@')
1596     return 0;
1597
1598   return 1;
1599
1600 #else
1601   return 0;
1602 #endif
1603 }
1604 \f
1605 static int 
1606 constant_pool_expr_1 (op, have_sym, have_toc) 
1607     rtx op;
1608     int *have_sym;
1609     int *have_toc;
1610 {
1611   switch (GET_CODE(op)) 
1612     {
1613     case SYMBOL_REF:
1614       if (CONSTANT_POOL_ADDRESS_P (op))
1615         {
1616           if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (op), Pmode))
1617             {
1618               *have_sym = 1;
1619               return 1;
1620             }
1621           else
1622             return 0;
1623         }
1624       else if (! strcmp (XSTR (op, 0), toc_label_name))
1625         {
1626           *have_toc = 1;
1627           return 1;
1628         }
1629       else
1630         return 0;
1631     case PLUS:
1632     case MINUS:
1633       return constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc) &&
1634         constant_pool_expr_1 (XEXP (op, 1), have_sym, have_toc);
1635     case CONST:
1636       return constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc);
1637     case CONST_INT:
1638       return 1;
1639     default:
1640       return 0;
1641     }
1642 }
1643
1644 int
1645 constant_pool_expr_p (op)
1646     rtx op;
1647 {
1648   int have_sym = 0;
1649   int have_toc = 0;
1650   return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_sym;
1651 }
1652
1653 int
1654 toc_relative_expr_p (op)
1655     rtx op;
1656 {
1657     int have_sym = 0;
1658     int have_toc = 0;
1659     return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_toc;
1660 }
1661
1662 /* Try machine-dependent ways of modifying an illegitimate address
1663    to be legitimate.  If we find one, return the new, valid address.
1664    This is used from only one place: `memory_address' in explow.c.
1665
1666    OLDX is the address as it was before break_out_memory_refs was
1667    called.  In some cases it is useful to look at this to decide what
1668    needs to be done.
1669
1670    MODE is passed so that this function can use GO_IF_LEGITIMATE_ADDRESS.
1671
1672    It is always safe for this function to do nothing.  It exists to
1673    recognize opportunities to optimize the output.
1674
1675    On RS/6000, first check for the sum of a register with a constant
1676    integer that is out of range.  If so, generate code to add the
1677    constant with the low-order 16 bits masked to the register and force
1678    this result into another register (this can be done with `cau').
1679    Then generate an address of REG+(CONST&0xffff), allowing for the
1680    possibility of bit 16 being a one.
1681
1682    Then check for the sum of a register and something not constant, try to
1683    load the other things into a register and return the sum.  */
1684 rtx
1685 rs6000_legitimize_address (x, oldx, mode)
1686      rtx x;
1687      rtx oldx ATTRIBUTE_UNUSED;
1688      enum machine_mode mode;
1689 {
1690   if (GET_CODE (x) == PLUS 
1691       && GET_CODE (XEXP (x, 0)) == REG
1692       && GET_CODE (XEXP (x, 1)) == CONST_INT
1693       && (unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000) >= 0x10000)
1694     { 
1695       HOST_WIDE_INT high_int, low_int;
1696       rtx sum;
1697       high_int = INTVAL (XEXP (x, 1)) & (~ (HOST_WIDE_INT) 0xffff);
1698       low_int = INTVAL (XEXP (x, 1)) & 0xffff;
1699       if (low_int & 0x8000)
1700         high_int += 0x10000, low_int |= ((HOST_WIDE_INT) -1) << 16;
1701       sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
1702                                          GEN_INT (high_int)), 0);
1703       return gen_rtx_PLUS (Pmode, sum, GEN_INT (low_int));
1704     }
1705   else if (GET_CODE (x) == PLUS 
1706            && GET_CODE (XEXP (x, 0)) == REG
1707            && GET_CODE (XEXP (x, 1)) != CONST_INT
1708            && GET_MODE_NUNITS (mode) == 1
1709            && (TARGET_HARD_FLOAT || TARGET_POWERPC64 || mode != DFmode)
1710            && (TARGET_POWERPC64 || mode != DImode)
1711            && mode != TImode)
1712     {
1713       return gen_rtx_PLUS (Pmode, XEXP (x, 0),
1714                            force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
1715     }
1716   else if (ALTIVEC_VECTOR_MODE (mode))
1717     {
1718       rtx reg;
1719
1720       /* Make sure both operands are registers.  */
1721       if (GET_CODE (x) == PLUS)
1722         return gen_rtx_PLUS (Pmode, force_reg (Pmode, XEXP (x, 0)),
1723                              force_reg (Pmode, XEXP (x, 1)));
1724
1725       reg = force_reg (Pmode, x);
1726       return reg;
1727     }
1728   else if (TARGET_ELF && TARGET_32BIT && TARGET_NO_TOC && ! flag_pic
1729            && GET_CODE (x) != CONST_INT
1730            && GET_CODE (x) != CONST_DOUBLE 
1731            && CONSTANT_P (x)
1732            && GET_MODE_NUNITS (mode) == 1
1733            && (GET_MODE_BITSIZE (mode) <= 32
1734                || (TARGET_HARD_FLOAT && mode == DFmode)))
1735     {
1736       rtx reg = gen_reg_rtx (Pmode);
1737       emit_insn (gen_elf_high (reg, (x)));
1738       return gen_rtx_LO_SUM (Pmode, reg, (x));
1739     }
1740   else if (TARGET_MACHO && TARGET_32BIT && TARGET_NO_TOC
1741            && ! flag_pic
1742            && GET_CODE (x) != CONST_INT
1743            && GET_CODE (x) != CONST_DOUBLE 
1744            && CONSTANT_P (x)
1745            && (TARGET_HARD_FLOAT || mode != DFmode)
1746            && mode != DImode 
1747            && mode != TImode)
1748     {
1749       rtx reg = gen_reg_rtx (Pmode);
1750       emit_insn (gen_macho_high (reg, (x)));
1751       return gen_rtx_LO_SUM (Pmode, reg, (x));
1752     }
1753   else if (TARGET_TOC 
1754            && CONSTANT_POOL_EXPR_P (x)
1755            && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
1756     {
1757       return create_TOC_reference (x);
1758     }
1759   else
1760     return NULL_RTX;
1761 }
1762
1763 /* The convention appears to be to define this wherever it is used.
1764    With legitimize_reload_address now defined here, REG_MODE_OK_FOR_BASE_P
1765    is now used here.  */
1766 #ifndef REG_MODE_OK_FOR_BASE_P
1767 #define REG_MODE_OK_FOR_BASE_P(REGNO, MODE) REG_OK_FOR_BASE_P (REGNO)
1768 #endif
1769
1770 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS.  Returns a value to
1771    replace the input X, or the original X if no replacement is called for.
1772    The output parameter *WIN is 1 if the calling macro should goto WIN,
1773    0 if it should not.
1774
1775    For RS/6000, we wish to handle large displacements off a base
1776    register by splitting the addend across an addiu/addis and the mem insn.
1777    This cuts number of extra insns needed from 3 to 1.
1778
1779    On Darwin, we use this to generate code for floating point constants.
1780    A movsf_low is generated so we wind up with 2 instructions rather than 3.
1781    The Darwin code is inside #if TARGET_MACHO because only then is
1782    machopic_function_base_name() defined.  */
1783 rtx
1784 rs6000_legitimize_reload_address (x, mode, opnum, type, ind_levels, win)
1785     rtx x;
1786     enum machine_mode mode;
1787     int opnum;
1788     int type;
1789     int ind_levels ATTRIBUTE_UNUSED;
1790     int *win;
1791 {
1792   /* We must recognize output that we have already generated ourselves.  */ 
1793   if (GET_CODE (x) == PLUS
1794       && GET_CODE (XEXP (x, 0)) == PLUS
1795       && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
1796       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
1797       && GET_CODE (XEXP (x, 1)) == CONST_INT)
1798     {
1799       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
1800                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
1801                    opnum, (enum reload_type)type);
1802       *win = 1;
1803       return x;
1804     }
1805
1806 #if TARGET_MACHO
1807   if (DEFAULT_ABI == ABI_DARWIN && flag_pic
1808       && GET_CODE (x) == LO_SUM
1809       && GET_CODE (XEXP (x, 0)) == PLUS
1810       && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
1811       && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
1812       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 0)) == CONST
1813       && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
1814       && GET_CODE (XEXP (XEXP (x, 1), 0)) == MINUS
1815       && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 0)) == SYMBOL_REF
1816       && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 1)) == SYMBOL_REF)
1817     {
1818       /* Result of previous invocation of this function on Darwin
1819          floating point constant.  */
1820       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
1821                 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
1822                 opnum, (enum reload_type)type);
1823       *win = 1;
1824       return x;
1825     }
1826 #endif
1827   if (GET_CODE (x) == PLUS
1828       && GET_CODE (XEXP (x, 0)) == REG
1829       && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
1830       && REG_MODE_OK_FOR_BASE_P (XEXP (x, 0), mode)
1831       && GET_CODE (XEXP (x, 1)) == CONST_INT)
1832     {
1833       HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
1834       HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
1835       HOST_WIDE_INT high
1836         = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
1837
1838       /* Check for 32-bit overflow.  */
1839       if (high + low != val)
1840         {
1841           *win = 0;
1842           return x;
1843         }
1844
1845       /* Reload the high part into a base reg; leave the low part
1846          in the mem directly.  */
1847
1848       x = gen_rtx_PLUS (GET_MODE (x),
1849                         gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
1850                                       GEN_INT (high)),
1851                         GEN_INT (low));
1852
1853       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
1854                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
1855                    opnum, (enum reload_type)type);
1856       *win = 1;
1857       return x;
1858     }
1859 #if TARGET_MACHO
1860   if (GET_CODE (x) == SYMBOL_REF
1861       && DEFAULT_ABI == ABI_DARWIN
1862       && flag_pic)
1863     {
1864       /* Darwin load of floating point constant.  */
1865       rtx offset = gen_rtx (CONST, Pmode,
1866                     gen_rtx (MINUS, Pmode, x,
1867                     gen_rtx (SYMBOL_REF, Pmode,
1868                         machopic_function_base_name ())));
1869       x = gen_rtx (LO_SUM, GET_MODE (x),
1870             gen_rtx (PLUS, Pmode, pic_offset_table_rtx,
1871                 gen_rtx (HIGH, Pmode, offset)), offset);
1872       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
1873                 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
1874                 opnum, (enum reload_type)type);
1875       *win = 1;
1876       return x;
1877     }
1878 #endif
1879   if (TARGET_TOC
1880            && CONSTANT_POOL_EXPR_P (x)
1881            && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), mode))
1882     {
1883       (x) = create_TOC_reference (x);
1884       *win = 1;
1885       return x;
1886     }
1887   *win = 0;
1888   return x;
1889 }    
1890
1891 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression
1892    that is a valid memory address for an instruction.
1893    The MODE argument is the machine mode for the MEM expression
1894    that wants to use this address.
1895
1896    On the RS/6000, there are four valid address: a SYMBOL_REF that
1897    refers to a constant pool entry of an address (or the sum of it
1898    plus a constant), a short (16-bit signed) constant plus a register,
1899    the sum of two registers, or a register indirect, possibly with an
1900    auto-increment.  For DFmode and DImode with an constant plus register,
1901    we must ensure that both words are addressable or PowerPC64 with offset
1902    word aligned.
1903
1904    For modes spanning multiple registers (DFmode in 32-bit GPRs,
1905    32-bit DImode, TImode), indexed addressing cannot be used because
1906    adjacent memory cells are accessed by adding word-sized offsets
1907    during assembly output.  */
1908 int
1909 rs6000_legitimate_address (mode, x, reg_ok_strict)
1910     enum machine_mode mode;
1911     rtx x;
1912     int reg_ok_strict;
1913 {
1914   if (LEGITIMATE_INDIRECT_ADDRESS_P (x, reg_ok_strict))
1915     return 1;
1916   if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
1917       && TARGET_UPDATE
1918       && LEGITIMATE_INDIRECT_ADDRESS_P (XEXP (x, 0), reg_ok_strict))
1919     return 1;
1920   if (LEGITIMATE_SMALL_DATA_P (mode, x))
1921     return 1;
1922   if (LEGITIMATE_CONSTANT_POOL_ADDRESS_P (x))
1923     return 1;
1924   /* If not REG_OK_STRICT (before reload) let pass any stack offset.  */
1925   if (! reg_ok_strict
1926       && GET_CODE (x) == PLUS
1927       && GET_CODE (XEXP (x, 0)) == REG
1928       && XEXP (x, 0) == virtual_stack_vars_rtx
1929       && GET_CODE (XEXP (x, 1)) == CONST_INT)
1930     return 1;
1931   if (LEGITIMATE_OFFSET_ADDRESS_P (mode, x, reg_ok_strict))
1932     return 1;
1933   if (mode != TImode
1934       && (TARGET_HARD_FLOAT || TARGET_POWERPC64 || mode != DFmode)
1935       && (TARGET_POWERPC64 || mode != DImode)
1936       && LEGITIMATE_INDEXED_ADDRESS_P (x, reg_ok_strict))
1937     return 1;
1938   if (LEGITIMATE_LO_SUM_ADDRESS_P (mode, x, reg_ok_strict))
1939     return 1;
1940   return 0;
1941 }
1942 \f
1943 /* Try to output insns to set TARGET equal to the constant C if it can
1944    be done in less than N insns.  Do all computations in MODE.
1945    Returns the place where the output has been placed if it can be
1946    done and the insns have been emitted.  If it would take more than N
1947    insns, zero is returned and no insns and emitted.  */
1948
1949 rtx
1950 rs6000_emit_set_const (dest, mode, source, n)
1951      rtx dest, source;
1952      enum machine_mode mode;
1953      int n ATTRIBUTE_UNUSED;
1954 {
1955   HOST_WIDE_INT c0, c1;
1956
1957   if (mode == QImode || mode == HImode || mode == SImode)
1958     {
1959       if (dest == NULL)
1960         dest = gen_reg_rtx (mode);
1961       emit_insn (gen_rtx_SET (VOIDmode, dest, source));
1962       return dest;
1963     }
1964
1965   if (GET_CODE (source) == CONST_INT)
1966     {
1967       c0 = INTVAL (source);
1968       c1 = -(c0 < 0);
1969     }
1970   else if (GET_CODE (source) == CONST_DOUBLE)
1971     {
1972 #if HOST_BITS_PER_WIDE_INT >= 64
1973       c0 = CONST_DOUBLE_LOW (source);
1974       c1 = -(c0 < 0);
1975 #else
1976       c0 = CONST_DOUBLE_LOW (source);
1977       c1 = CONST_DOUBLE_HIGH (source);
1978 #endif
1979     }
1980   else
1981     abort ();
1982
1983   return rs6000_emit_set_long_const (dest, c0, c1);
1984 }
1985
1986 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
1987    fall back to a straight forward decomposition.  We do this to avoid
1988    exponential run times encountered when looking for longer sequences
1989    with rs6000_emit_set_const.  */
1990 static rtx
1991 rs6000_emit_set_long_const (dest, c1, c2)
1992      rtx dest;
1993      HOST_WIDE_INT c1, c2;
1994 {
1995   if (!TARGET_POWERPC64)
1996     {
1997       rtx operand1, operand2;
1998
1999       operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
2000                                         DImode);
2001       operand2 = operand_subword_force (dest, WORDS_BIG_ENDIAN != 0,
2002                                         DImode);
2003       emit_move_insn (operand1, GEN_INT (c1));
2004       emit_move_insn (operand2, GEN_INT (c2));
2005     }
2006   else
2007     {
2008       HOST_WIDE_INT ud1, ud2, ud3, ud4;
2009
2010       ud1 = c1 & 0xffff;
2011       ud2 = (c1 & 0xffff0000) >> 16;
2012 #if HOST_BITS_PER_WIDE_INT >= 64
2013       c2 = c1 >> 32;
2014 #endif
2015       ud3 = c2 & 0xffff;
2016       ud4 = (c2 & 0xffff0000) >> 16;
2017
2018       if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000)) 
2019           || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
2020         {
2021           if (ud1 & 0x8000)
2022             emit_move_insn (dest, GEN_INT (((ud1  ^ 0x8000) -  0x8000)));
2023           else
2024             emit_move_insn (dest, GEN_INT (ud1));
2025         }
2026
2027       else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000)) 
2028                || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
2029         {
2030           if (ud2 & 0x8000)
2031             emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000) 
2032                                            - 0x80000000));
2033           else
2034             emit_move_insn (dest, GEN_INT (ud2 << 16));
2035           if (ud1 != 0)
2036             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
2037         }
2038       else if ((ud4 == 0xffff && (ud3 & 0x8000)) 
2039                || (ud4 == 0 && ! (ud3 & 0x8000)))
2040         {
2041           if (ud3 & 0x8000)
2042             emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000) 
2043                                            - 0x80000000));
2044           else
2045             emit_move_insn (dest, GEN_INT (ud3 << 16));
2046
2047           if (ud2 != 0)
2048             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud2)));
2049           emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (16)));
2050           if (ud1 != 0)
2051             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
2052         }
2053       else 
2054         {
2055           if (ud4 & 0x8000)
2056             emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000) 
2057                                            - 0x80000000));
2058           else
2059             emit_move_insn (dest, GEN_INT (ud4 << 16));
2060
2061           if (ud3 != 0)
2062             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud3)));
2063
2064           emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (32)));
2065           if (ud2 != 0)
2066             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, 
2067                                                GEN_INT (ud2 << 16)));   
2068           if (ud1 != 0)
2069             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
2070         }
2071     }
2072   return dest;
2073 }
2074
2075 /* Emit a move from SOURCE to DEST in mode MODE.  */
2076 void
2077 rs6000_emit_move (dest, source, mode)
2078      rtx dest;
2079      rtx source;
2080      enum machine_mode mode;
2081 {
2082   rtx operands[2];
2083   operands[0] = dest;
2084   operands[1] = source;
2085   
2086   /* Sanity checks.  Check that we get CONST_DOUBLE only when we should.  */
2087   if (GET_CODE (operands[1]) == CONST_DOUBLE
2088       && ! FLOAT_MODE_P (mode)
2089       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
2090     {
2091       /* FIXME.  This should never happen.  */
2092       /* Since it seems that it does, do the safe thing and convert
2093          to a CONST_INT.  */
2094       operands[1] = 
2095         GEN_INT (trunc_int_for_mode (CONST_DOUBLE_LOW (operands[1]), mode));
2096     }
2097   if (GET_CODE (operands[1]) == CONST_DOUBLE
2098       && ! FLOAT_MODE_P (mode)
2099       && ((CONST_DOUBLE_HIGH (operands[1]) == 0
2100            && CONST_DOUBLE_LOW (operands[1]) >= 0)
2101           || (CONST_DOUBLE_HIGH (operands[1]) == -1
2102               && CONST_DOUBLE_LOW (operands[1]) < 0)))
2103     abort ();
2104
2105   /* Check if GCC is setting up a block move that will end up using FP
2106      registers as temporaries.  We must make sure this is acceptable.  */
2107   if (GET_CODE (operands[0]) == MEM
2108       && GET_CODE (operands[1]) == MEM
2109       && mode == DImode
2110       && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
2111           || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
2112       && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
2113                                             ? 32 : MEM_ALIGN (operands[0])))
2114             || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
2115                                                ? 32 
2116                                                : MEM_ALIGN (operands[1]))))
2117       && ! MEM_VOLATILE_P (operands [0])
2118       && ! MEM_VOLATILE_P (operands [1]))
2119     {
2120       emit_move_insn (adjust_address (operands[0], SImode, 0),
2121                       adjust_address (operands[1], SImode, 0));
2122       emit_move_insn (adjust_address (operands[0], SImode, 4),
2123                       adjust_address (operands[1], SImode, 4));
2124       return;
2125     }
2126   
2127   if (! no_new_pseudos && GET_CODE (operands[0]) != REG)
2128     operands[1] = force_reg (mode, operands[1]);
2129
2130   if (mode == SFmode && ! TARGET_POWERPC && TARGET_HARD_FLOAT
2131       && GET_CODE (operands[0]) == MEM)
2132     {
2133       int regnum;
2134
2135       if (reload_in_progress || reload_completed)
2136         regnum = true_regnum (operands[1]);
2137       else if (GET_CODE (operands[1]) == REG)
2138         regnum = REGNO (operands[1]);
2139       else
2140         regnum = -1;
2141       
2142       /* If operands[1] is a register, on POWER it may have
2143          double-precision data in it, so truncate it to single
2144          precision.  */
2145       if (FP_REGNO_P (regnum) || regnum >= FIRST_PSEUDO_REGISTER)
2146         {
2147           rtx newreg;
2148           newreg = (no_new_pseudos ? operands[1] : gen_reg_rtx (mode));
2149           emit_insn (gen_aux_truncdfsf2 (newreg, operands[1]));
2150           operands[1] = newreg;
2151         }
2152     }
2153
2154   /* Handle the case where reload calls us with an invalid address;
2155      and the case of CONSTANT_P_RTX.  */
2156   if (! general_operand (operands[1], mode)
2157       || ! nonimmediate_operand (operands[0], mode)
2158       || GET_CODE (operands[1]) == CONSTANT_P_RTX)
2159     {
2160       emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2161       return;
2162     }
2163   
2164   /* FIXME:  In the long term, this switch statement should go away
2165      and be replaced by a sequence of tests based on things like
2166      mode == Pmode.  */
2167   switch (mode)
2168     {
2169     case HImode:
2170     case QImode:
2171       if (CONSTANT_P (operands[1])
2172           && GET_CODE (operands[1]) != CONST_INT)
2173         operands[1] = force_const_mem (mode, operands[1]);
2174       break;
2175
2176     case TFmode:
2177     case DFmode:
2178     case SFmode:
2179       if (CONSTANT_P (operands[1]) 
2180           && ! easy_fp_constant (operands[1], mode))
2181         operands[1] = force_const_mem (mode, operands[1]);
2182       break;
2183       
2184     case V16QImode:
2185     case V8HImode:
2186     case V4SFmode:
2187     case V4SImode:
2188       /* fixme: aldyh -- allow vector constants when they are implemented.  */
2189       if (CONSTANT_P (operands[1]))
2190         operands[1] = force_const_mem (mode, operands[1]);
2191       break;
2192       
2193     case SImode:
2194     case DImode:
2195       /* Use default pattern for address of ELF small data */
2196       if (TARGET_ELF
2197           && mode == Pmode
2198           && DEFAULT_ABI == ABI_V4
2199           && (GET_CODE (operands[1]) == SYMBOL_REF 
2200               || GET_CODE (operands[1]) == CONST)
2201           && small_data_operand (operands[1], mode))
2202         {
2203           emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2204           return;
2205         }
2206
2207       if (DEFAULT_ABI == ABI_V4
2208           && mode == Pmode && mode == SImode
2209           && flag_pic == 1 && got_operand (operands[1], mode))
2210         {
2211           emit_insn (gen_movsi_got (operands[0], operands[1]));
2212           return;
2213         }
2214
2215       if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
2216           && TARGET_NO_TOC && ! flag_pic
2217           && mode == Pmode
2218           && CONSTANT_P (operands[1])
2219           && GET_CODE (operands[1]) != HIGH
2220           && GET_CODE (operands[1]) != CONST_INT)
2221         {
2222           rtx target = (no_new_pseudos ? operands[0] : gen_reg_rtx (mode));
2223
2224           /* If this is a function address on -mcall-aixdesc,
2225              convert it to the address of the descriptor.  */
2226           if (DEFAULT_ABI == ABI_AIX
2227               && GET_CODE (operands[1]) == SYMBOL_REF
2228               && XSTR (operands[1], 0)[0] == '.')
2229             {
2230               const char *name = XSTR (operands[1], 0);
2231               rtx new_ref;
2232               while (*name == '.')
2233                 name++;
2234               new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
2235               CONSTANT_POOL_ADDRESS_P (new_ref)
2236                 = CONSTANT_POOL_ADDRESS_P (operands[1]);
2237               SYMBOL_REF_FLAG (new_ref) = SYMBOL_REF_FLAG (operands[1]);
2238               SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
2239               operands[1] = new_ref;
2240             }
2241
2242           if (DEFAULT_ABI == ABI_DARWIN)
2243             {
2244               emit_insn (gen_macho_high (target, operands[1]));
2245               emit_insn (gen_macho_low (operands[0], target, operands[1]));
2246               return;
2247             }
2248
2249           emit_insn (gen_elf_high (target, operands[1]));
2250           emit_insn (gen_elf_low (operands[0], target, operands[1]));
2251           return;
2252         }
2253
2254       /* If this is a SYMBOL_REF that refers to a constant pool entry,
2255          and we have put it in the TOC, we just need to make a TOC-relative
2256          reference to it.  */
2257       if (TARGET_TOC
2258           && GET_CODE (operands[1]) == SYMBOL_REF
2259           && CONSTANT_POOL_EXPR_P (operands[1])
2260           && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (operands[1]),
2261                                               get_pool_mode (operands[1])))
2262         {
2263           operands[1] = create_TOC_reference (operands[1]);
2264         }
2265       else if (mode == Pmode
2266                && CONSTANT_P (operands[1])
2267                && ((GET_CODE (operands[1]) != CONST_INT
2268                     && ! easy_fp_constant (operands[1], mode))
2269                    || (GET_CODE (operands[1]) == CONST_INT
2270                        && num_insns_constant (operands[1], mode) > 2)
2271                    || (GET_CODE (operands[0]) == REG
2272                        && FP_REGNO_P (REGNO (operands[0]))))
2273                && GET_CODE (operands[1]) != HIGH
2274                && ! LEGITIMATE_CONSTANT_POOL_ADDRESS_P (operands[1])
2275                && ! TOC_RELATIVE_EXPR_P (operands[1]))
2276         {
2277           /* Emit a USE operation so that the constant isn't deleted if
2278              expensive optimizations are turned on because nobody
2279              references it.  This should only be done for operands that
2280              contain SYMBOL_REFs with CONSTANT_POOL_ADDRESS_P set.
2281              This should not be done for operands that contain LABEL_REFs.
2282              For now, we just handle the obvious case.  */
2283           if (GET_CODE (operands[1]) != LABEL_REF)
2284             emit_insn (gen_rtx_USE (VOIDmode, operands[1]));
2285
2286 #if TARGET_MACHO
2287           /* Darwin uses a special PIC legitimizer.  */
2288           if (DEFAULT_ABI == ABI_DARWIN && flag_pic)
2289             {
2290               operands[1] =
2291                 rs6000_machopic_legitimize_pic_address (operands[1], mode,
2292                                                         operands[0]);
2293               if (operands[0] != operands[1])
2294                 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2295               return;
2296             }
2297 #endif
2298
2299           /* If we are to limit the number of things we put in the TOC and
2300              this is a symbol plus a constant we can add in one insn,
2301              just put the symbol in the TOC and add the constant.  Don't do
2302              this if reload is in progress.  */
2303           if (GET_CODE (operands[1]) == CONST
2304               && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
2305               && GET_CODE (XEXP (operands[1], 0)) == PLUS
2306               && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
2307               && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
2308                   || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
2309               && ! side_effects_p (operands[0]))
2310             {
2311               rtx sym =
2312                 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
2313               rtx other = XEXP (XEXP (operands[1], 0), 1);
2314
2315               sym = force_reg (mode, sym);
2316               if (mode == SImode)
2317                 emit_insn (gen_addsi3 (operands[0], sym, other));
2318               else
2319                 emit_insn (gen_adddi3 (operands[0], sym, other));
2320               return;
2321             }
2322
2323           operands[1] = force_const_mem (mode, operands[1]);
2324
2325           if (TARGET_TOC 
2326               && CONSTANT_POOL_EXPR_P (XEXP (operands[1], 0))
2327               && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
2328                         get_pool_constant (XEXP (operands[1], 0)),
2329                         get_pool_mode (XEXP (operands[1], 0))))
2330             {
2331               operands[1]
2332                 = gen_rtx_MEM (mode,
2333                                create_TOC_reference (XEXP (operands[1], 0)));
2334               set_mem_alias_set (operands[1], get_TOC_alias_set ());
2335               RTX_UNCHANGING_P (operands[1]) = 1;
2336             }
2337         }
2338       break;
2339
2340     case TImode:
2341       if (GET_CODE (operands[0]) == MEM
2342           && GET_CODE (XEXP (operands[0], 0)) != REG
2343           && ! reload_in_progress)
2344         operands[0]
2345           = replace_equiv_address (operands[0],
2346                                    copy_addr_to_reg (XEXP (operands[0], 0)));
2347
2348       if (GET_CODE (operands[1]) == MEM
2349           && GET_CODE (XEXP (operands[1], 0)) != REG
2350           && ! reload_in_progress)
2351         operands[1]
2352           = replace_equiv_address (operands[1],
2353                                    copy_addr_to_reg (XEXP (operands[1], 0)));
2354       break;
2355
2356     default:
2357       abort ();
2358     }
2359
2360   /* Above, we may have called force_const_mem which may have returned
2361      an invalid address.  If we can, fix this up; otherwise, reload will
2362      have to deal with it.  */
2363   if (GET_CODE (operands[1]) == MEM
2364       && ! memory_address_p (mode, XEXP (operands[1], 0))
2365       && ! reload_in_progress)
2366     operands[1] = adjust_address (operands[1], mode, 0);
2367
2368   emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2369   return;
2370 }
2371 \f
2372 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2373    for a call to a function whose data type is FNTYPE.
2374    For a library call, FNTYPE is 0.
2375
2376    For incoming args we set the number of arguments in the prototype large
2377    so we never return a PARALLEL.  */
2378
2379 void
2380 init_cumulative_args (cum, fntype, libname, incoming)
2381      CUMULATIVE_ARGS *cum;
2382      tree fntype;
2383      rtx libname ATTRIBUTE_UNUSED;
2384      int incoming;
2385 {
2386   static CUMULATIVE_ARGS zero_cumulative;
2387
2388   *cum = zero_cumulative;
2389   cum->words = 0;
2390   cum->fregno = FP_ARG_MIN_REG;
2391   cum->vregno = ALTIVEC_ARG_MIN_REG;
2392   cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
2393   cum->call_cookie = CALL_NORMAL;
2394   cum->sysv_gregno = GP_ARG_MIN_REG;
2395
2396   if (incoming)
2397     cum->nargs_prototype = 1000;                /* don't return a PARALLEL */
2398
2399   else if (cum->prototype)
2400     cum->nargs_prototype = (list_length (TYPE_ARG_TYPES (fntype)) - 1
2401                             + (TYPE_MODE (TREE_TYPE (fntype)) == BLKmode
2402                                || RETURN_IN_MEMORY (TREE_TYPE (fntype))));
2403
2404   else
2405     cum->nargs_prototype = 0;
2406
2407   cum->orig_nargs = cum->nargs_prototype;
2408
2409   /* Check for longcall's */
2410   if (fntype && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype)))
2411     cum->call_cookie = CALL_LONG;
2412
2413   if (TARGET_DEBUG_ARG)
2414     {
2415       fprintf (stderr, "\ninit_cumulative_args:");
2416       if (fntype)
2417         {
2418           tree ret_type = TREE_TYPE (fntype);
2419           fprintf (stderr, " ret code = %s,",
2420                    tree_code_name[ (int)TREE_CODE (ret_type) ]);
2421         }
2422
2423       if (cum->call_cookie & CALL_LONG)
2424         fprintf (stderr, " longcall,");
2425
2426       fprintf (stderr, " proto = %d, nargs = %d\n",
2427                cum->prototype, cum->nargs_prototype);
2428     }
2429 }
2430 \f
2431 /* If defined, a C expression which determines whether, and in which
2432    direction, to pad out an argument with extra space.  The value
2433    should be of type `enum direction': either `upward' to pad above
2434    the argument, `downward' to pad below, or `none' to inhibit
2435    padding.
2436
2437    For the AIX ABI structs are always stored left shifted in their
2438    argument slot.  */
2439
2440 enum direction
2441 function_arg_padding (mode, type)
2442      enum machine_mode mode;
2443      tree type;
2444 {
2445   if (type != 0 && AGGREGATE_TYPE_P (type))
2446     return upward;
2447
2448   /* This is the default definition.  */
2449   return (! BYTES_BIG_ENDIAN
2450           ? upward
2451           : ((mode == BLKmode
2452               ? (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
2453                  && int_size_in_bytes (type) < (PARM_BOUNDARY / BITS_PER_UNIT))
2454               : GET_MODE_BITSIZE (mode) < PARM_BOUNDARY)
2455              ? downward : upward));
2456 }
2457
2458 /* If defined, a C expression that gives the alignment boundary, in bits,
2459    of an argument with the specified mode and type.  If it is not defined, 
2460    PARM_BOUNDARY is used for all arguments.
2461    
2462    V.4 wants long longs to be double word aligned.  */
2463
2464 int
2465 function_arg_boundary (mode, type)
2466      enum machine_mode mode;
2467      tree type ATTRIBUTE_UNUSED;
2468 {
2469   if (DEFAULT_ABI == ABI_V4 && (mode == DImode || mode == DFmode))
2470     return 64;
2471   else if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
2472     return 128;
2473   else
2474     return PARM_BOUNDARY;
2475 }
2476 \f
2477 /* Update the data in CUM to advance over an argument
2478    of mode MODE and data type TYPE.
2479    (TYPE is null for libcalls where that information may not be available.)  */
2480
2481 void
2482 function_arg_advance (cum, mode, type, named)
2483      CUMULATIVE_ARGS *cum;
2484      enum machine_mode mode;
2485      tree type;
2486      int named;
2487 {
2488   cum->nargs_prototype--;
2489
2490   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
2491     {
2492       if (cum->vregno <= ALTIVEC_ARG_MAX_REG && cum->nargs_prototype >= 0)
2493         cum->vregno++;
2494       else
2495         cum->words += RS6000_ARG_SIZE (mode, type);
2496     }
2497   else if (DEFAULT_ABI == ABI_V4)
2498     {
2499       if (TARGET_HARD_FLOAT
2500           && (mode == SFmode || mode == DFmode))
2501         {
2502           if (cum->fregno <= FP_ARG_V4_MAX_REG)
2503             cum->fregno++;
2504           else
2505             {
2506               if (mode == DFmode)
2507                 cum->words += cum->words & 1;
2508               cum->words += RS6000_ARG_SIZE (mode, type);
2509             }
2510         }
2511       else
2512         {
2513           int n_words;
2514           int gregno = cum->sysv_gregno;
2515
2516           /* Aggregates and IEEE quad get passed by reference.  */
2517           if ((type && AGGREGATE_TYPE_P (type))
2518               || mode == TFmode)
2519             n_words = 1;
2520           else 
2521             n_words = RS6000_ARG_SIZE (mode, type);
2522
2523           /* Long long is put in odd registers.  */
2524           if (n_words == 2 && (gregno & 1) == 0)
2525             gregno += 1;
2526
2527           /* Long long is not split between registers and stack.  */
2528           if (gregno + n_words - 1 > GP_ARG_MAX_REG)
2529             {
2530               /* Long long is aligned on the stack.  */
2531               if (n_words == 2)
2532                 cum->words += cum->words & 1;
2533               cum->words += n_words;
2534             }
2535
2536           /* Note: continuing to accumulate gregno past when we've started
2537              spilling to the stack indicates the fact that we've started
2538              spilling to the stack to expand_builtin_saveregs.  */
2539           cum->sysv_gregno = gregno + n_words;
2540         }
2541
2542       if (TARGET_DEBUG_ARG)
2543         {
2544           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
2545                    cum->words, cum->fregno);
2546           fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
2547                    cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
2548           fprintf (stderr, "mode = %4s, named = %d\n",
2549                    GET_MODE_NAME (mode), named);
2550         }
2551     }
2552   else
2553     {
2554       int align = (TARGET_32BIT && (cum->words & 1) != 0
2555                    && function_arg_boundary (mode, type) == 64) ? 1 : 0;
2556
2557       cum->words += align + RS6000_ARG_SIZE (mode, type);
2558
2559       if (GET_MODE_CLASS (mode) == MODE_FLOAT && TARGET_HARD_FLOAT)
2560         cum->fregno++;
2561
2562       if (TARGET_DEBUG_ARG)
2563         {
2564           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
2565                    cum->words, cum->fregno);
2566           fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
2567                    cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
2568           fprintf (stderr, "named = %d, align = %d\n", named, align);
2569         }
2570     }
2571 }
2572 \f
2573 /* Determine where to put an argument to a function.
2574    Value is zero to push the argument on the stack,
2575    or a hard register in which to store the argument.
2576
2577    MODE is the argument's machine mode.
2578    TYPE is the data type of the argument (as a tree).
2579     This is null for libcalls where that information may
2580     not be available.
2581    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2582     the preceding args and about the function being called.
2583    NAMED is nonzero if this argument is a named parameter
2584     (otherwise it is an extra parameter matching an ellipsis).
2585
2586    On RS/6000 the first eight words of non-FP are normally in registers
2587    and the rest are pushed.  Under AIX, the first 13 FP args are in registers.
2588    Under V.4, the first 8 FP args are in registers.
2589
2590    If this is floating-point and no prototype is specified, we use
2591    both an FP and integer register (or possibly FP reg and stack).  Library
2592    functions (when TYPE is zero) always have the proper types for args,
2593    so we can pass the FP value just in one register.  emit_library_function
2594    doesn't support PARALLEL anyway.  */
2595
2596 struct rtx_def *
2597 function_arg (cum, mode, type, named)
2598      CUMULATIVE_ARGS *cum;
2599      enum machine_mode mode;
2600      tree type;
2601      int named;
2602 {
2603   enum rs6000_abi abi = DEFAULT_ABI;
2604
2605   /* Return a marker to indicate whether CR1 needs to set or clear the
2606      bit that V.4 uses to say fp args were passed in registers.
2607      Assume that we don't need the marker for software floating point,
2608      or compiler generated library calls.  */
2609   if (mode == VOIDmode)
2610     {
2611       if (abi == ABI_V4
2612           && TARGET_HARD_FLOAT
2613           && cum->nargs_prototype < 0
2614           && type && (cum->prototype || TARGET_NO_PROTOTYPE))
2615         {
2616           return GEN_INT (cum->call_cookie
2617                           | ((cum->fregno == FP_ARG_MIN_REG)
2618                              ? CALL_V4_SET_FP_ARGS
2619                              : CALL_V4_CLEAR_FP_ARGS));
2620         }
2621
2622       return GEN_INT (cum->call_cookie);
2623     }
2624
2625   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
2626     {
2627       if (named && cum->vregno <= ALTIVEC_ARG_MAX_REG)
2628         return gen_rtx_REG (mode, cum->vregno);
2629       else
2630         return NULL;
2631     }
2632   else if (abi == ABI_V4)
2633     {
2634       if (TARGET_HARD_FLOAT
2635           && (mode == SFmode || mode == DFmode))
2636         {
2637           if (cum->fregno <= FP_ARG_V4_MAX_REG)
2638             return gen_rtx_REG (mode, cum->fregno);
2639           else
2640             return NULL;
2641         }
2642       else
2643         {
2644           int n_words;
2645           int gregno = cum->sysv_gregno;
2646
2647           /* Aggregates and IEEE quad get passed by reference.  */
2648           if ((type && AGGREGATE_TYPE_P (type))
2649               || mode == TFmode)
2650             n_words = 1;
2651           else 
2652             n_words = RS6000_ARG_SIZE (mode, type);
2653
2654           /* Long long is put in odd registers.  */
2655           if (n_words == 2 && (gregno & 1) == 0)
2656             gregno += 1;
2657
2658           /* Long long is not split between registers and stack.  */
2659           if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
2660             return gen_rtx_REG (mode, gregno);
2661           else
2662             return NULL;
2663         }
2664     }
2665   else
2666     {
2667       int align = (TARGET_32BIT && (cum->words & 1) != 0
2668                    && function_arg_boundary (mode, type) == 64) ? 1 : 0;
2669       int align_words = cum->words + align;
2670
2671       if (type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
2672         return NULL_RTX;
2673
2674       if (USE_FP_FOR_ARG_P (*cum, mode, type))
2675         {
2676           if (! type
2677               || ((cum->nargs_prototype > 0)
2678                   /* IBM AIX extended its linkage convention definition always
2679                      to require FP args after register save area hole on the
2680                      stack.  */
2681                   && (DEFAULT_ABI != ABI_AIX
2682                       || ! TARGET_XL_CALL
2683                       || (align_words < GP_ARG_NUM_REG))))
2684             return gen_rtx_REG (mode, cum->fregno);
2685
2686           return gen_rtx_PARALLEL (mode,
2687             gen_rtvec (2,
2688                        gen_rtx_EXPR_LIST (VOIDmode,
2689                                 ((align_words >= GP_ARG_NUM_REG)
2690                                  ? NULL_RTX
2691                                  : (align_words
2692                                     + RS6000_ARG_SIZE (mode, type)
2693                                     > GP_ARG_NUM_REG
2694                                     /* If this is partially on the stack, then
2695                                        we only include the portion actually
2696                                        in registers here.  */
2697                                     ? gen_rtx_REG (SImode,
2698                                                GP_ARG_MIN_REG + align_words)
2699                                     : gen_rtx_REG (mode,
2700                                                GP_ARG_MIN_REG + align_words))),
2701                                 const0_rtx),
2702                        gen_rtx_EXPR_LIST (VOIDmode,
2703                                 gen_rtx_REG (mode, cum->fregno),
2704                                 const0_rtx)));
2705         }
2706       else if (align_words < GP_ARG_NUM_REG)
2707         return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
2708       else
2709         return NULL_RTX;
2710     }
2711 }
2712 \f
2713 /* For an arg passed partly in registers and partly in memory,
2714    this is the number of registers used.
2715    For args passed entirely in registers or entirely in memory, zero.  */
2716
2717 int
2718 function_arg_partial_nregs (cum, mode, type, named)
2719      CUMULATIVE_ARGS *cum;
2720      enum machine_mode mode;
2721      tree type;
2722      int named ATTRIBUTE_UNUSED;
2723 {
2724   if (DEFAULT_ABI == ABI_V4)
2725     return 0;
2726
2727   if (USE_FP_FOR_ARG_P (*cum, mode, type)
2728       || USE_ALTIVEC_FOR_ARG_P (*cum, mode, type))
2729     {
2730       if (cum->nargs_prototype >= 0)
2731         return 0;
2732     }
2733
2734   if (cum->words < GP_ARG_NUM_REG
2735       && GP_ARG_NUM_REG < (cum->words + RS6000_ARG_SIZE (mode, type)))
2736     {
2737       int ret = GP_ARG_NUM_REG - cum->words;
2738       if (ret && TARGET_DEBUG_ARG)
2739         fprintf (stderr, "function_arg_partial_nregs: %d\n", ret);
2740
2741       return ret;
2742     }
2743
2744   return 0;
2745 }
2746 \f
2747 /* A C expression that indicates when an argument must be passed by
2748    reference.  If nonzero for an argument, a copy of that argument is
2749    made in memory and a pointer to the argument is passed instead of
2750    the argument itself.  The pointer is passed in whatever way is
2751    appropriate for passing a pointer to that type.
2752
2753    Under V.4, structures and unions are passed by reference.  */
2754
2755 int
2756 function_arg_pass_by_reference (cum, mode, type, named)
2757      CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED;
2758      enum machine_mode mode ATTRIBUTE_UNUSED;
2759      tree type;
2760      int named ATTRIBUTE_UNUSED;
2761 {
2762   if (DEFAULT_ABI == ABI_V4
2763       && ((type && AGGREGATE_TYPE_P (type))
2764           || mode == TFmode))
2765     {
2766       if (TARGET_DEBUG_ARG)
2767         fprintf (stderr, "function_arg_pass_by_reference: aggregate\n");
2768
2769       return 1;
2770     }
2771
2772   return 0;
2773 }
2774 \f
2775 /* Perform any needed actions needed for a function that is receiving a
2776    variable number of arguments. 
2777
2778    CUM is as above.
2779
2780    MODE and TYPE are the mode and type of the current parameter.
2781
2782    PRETEND_SIZE is a variable that should be set to the amount of stack
2783    that must be pushed by the prolog to pretend that our caller pushed
2784    it.
2785
2786    Normally, this macro will push all remaining incoming registers on the
2787    stack and set PRETEND_SIZE to the length of the registers pushed.  */
2788
2789 void
2790 setup_incoming_varargs (cum, mode, type, pretend_size, no_rtl)
2791      CUMULATIVE_ARGS *cum;
2792      enum machine_mode mode;
2793      tree type;
2794      int *pretend_size;
2795      int no_rtl;
2796
2797 {
2798   CUMULATIVE_ARGS next_cum;
2799   int reg_size = TARGET_32BIT ? 4 : 8;
2800   rtx save_area = NULL_RTX, mem;
2801   int first_reg_offset, set;
2802   tree fntype;
2803   int stdarg_p;
2804
2805   fntype = TREE_TYPE (current_function_decl);
2806   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
2807               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
2808                   != void_type_node));
2809
2810   /* For varargs, we do not want to skip the dummy va_dcl argument.
2811      For stdargs, we do want to skip the last named argument.  */
2812   next_cum = *cum;
2813   if (stdarg_p)
2814     function_arg_advance (&next_cum, mode, type, 1);
2815
2816   if (DEFAULT_ABI == ABI_V4)
2817     {
2818       /* Indicate to allocate space on the stack for varargs save area.  */
2819       /* ??? Does this really have to be located at a magic spot on the
2820          stack, or can we allocate this with assign_stack_local instead.  */
2821       cfun->machine->sysv_varargs_p = 1;
2822       if (! no_rtl)
2823         save_area = plus_constant (virtual_stack_vars_rtx,
2824                                    - RS6000_VARARGS_SIZE);
2825
2826       first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
2827     }
2828   else
2829     {
2830       first_reg_offset = next_cum.words;
2831       save_area = virtual_incoming_args_rtx;
2832       cfun->machine->sysv_varargs_p = 0;
2833
2834       if (MUST_PASS_IN_STACK (mode, type))
2835         first_reg_offset += RS6000_ARG_SIZE (TYPE_MODE (type), type);
2836     }
2837
2838   set = get_varargs_alias_set ();
2839   if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG)
2840     {
2841       mem = gen_rtx_MEM (BLKmode,
2842                          plus_constant (save_area,
2843                                         first_reg_offset * reg_size)),
2844       set_mem_alias_set (mem, set);
2845       set_mem_align (mem, BITS_PER_WORD);
2846
2847       move_block_from_reg
2848         (GP_ARG_MIN_REG + first_reg_offset, mem,
2849          GP_ARG_NUM_REG - first_reg_offset,
2850          (GP_ARG_NUM_REG - first_reg_offset) * UNITS_PER_WORD);
2851
2852       /* ??? Does ABI_V4 need this at all?  */
2853       *pretend_size = (GP_ARG_NUM_REG - first_reg_offset) * UNITS_PER_WORD;
2854     }
2855
2856   /* Save FP registers if needed.  */
2857   if (DEFAULT_ABI == ABI_V4
2858       && TARGET_HARD_FLOAT && ! no_rtl
2859       && next_cum.fregno <= FP_ARG_V4_MAX_REG)
2860     {
2861       int fregno = next_cum.fregno;
2862       rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
2863       rtx lab = gen_label_rtx ();
2864       int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG) * 8);
2865
2866       emit_jump_insn (gen_rtx_SET (VOIDmode,
2867                                    pc_rtx,
2868                                    gen_rtx_IF_THEN_ELSE (VOIDmode,
2869                                             gen_rtx_NE (VOIDmode, cr1,
2870                                                         const0_rtx),
2871                                             gen_rtx_LABEL_REF (VOIDmode, lab),
2872                                             pc_rtx)));
2873
2874       while (fregno <= FP_ARG_V4_MAX_REG)
2875         {
2876           mem = gen_rtx_MEM (DFmode, plus_constant (save_area, off));
2877           set_mem_alias_set (mem, set);
2878           emit_move_insn (mem, gen_rtx_REG (DFmode, fregno));
2879           fregno++;
2880           off += 8;
2881         }
2882
2883       emit_label (lab);
2884     }
2885 }
2886
2887 /* Create the va_list data type.  */
2888
2889 tree
2890 rs6000_build_va_list ()
2891 {
2892   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
2893
2894   /* For AIX, prefer 'char *' because that's what the system
2895      header files like.  */
2896   if (DEFAULT_ABI != ABI_V4)
2897     return build_pointer_type (char_type_node);
2898
2899   record = make_lang_type (RECORD_TYPE);
2900   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
2901
2902   f_gpr = build_decl (FIELD_DECL, get_identifier ("gpr"), 
2903                       unsigned_char_type_node);
2904   f_fpr = build_decl (FIELD_DECL, get_identifier ("fpr"), 
2905                       unsigned_char_type_node);
2906   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
2907                       ptr_type_node);
2908   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
2909                       ptr_type_node);
2910
2911   DECL_FIELD_CONTEXT (f_gpr) = record;
2912   DECL_FIELD_CONTEXT (f_fpr) = record;
2913   DECL_FIELD_CONTEXT (f_ovf) = record;
2914   DECL_FIELD_CONTEXT (f_sav) = record;
2915
2916   TREE_CHAIN (record) = type_decl;
2917   TYPE_NAME (record) = type_decl;
2918   TYPE_FIELDS (record) = f_gpr;
2919   TREE_CHAIN (f_gpr) = f_fpr;
2920   TREE_CHAIN (f_fpr) = f_ovf;
2921   TREE_CHAIN (f_ovf) = f_sav;
2922
2923   layout_type (record);
2924
2925   /* The correct type is an array type of one element.  */
2926   return build_array_type (record, build_index_type (size_zero_node));
2927 }
2928
2929 /* Implement va_start.  */
2930
2931 void
2932 rs6000_va_start (stdarg_p, valist, nextarg)
2933      int stdarg_p;
2934      tree valist;
2935      rtx nextarg;
2936 {
2937   HOST_WIDE_INT words, n_gpr, n_fpr;
2938   tree f_gpr, f_fpr, f_ovf, f_sav;
2939   tree gpr, fpr, ovf, sav, t;
2940
2941   /* Only SVR4 needs something special.  */
2942   if (DEFAULT_ABI != ABI_V4)
2943     {
2944       std_expand_builtin_va_start (stdarg_p, valist, nextarg);
2945       return;
2946     }
2947
2948   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
2949   f_fpr = TREE_CHAIN (f_gpr);
2950   f_ovf = TREE_CHAIN (f_fpr);
2951   f_sav = TREE_CHAIN (f_ovf);
2952
2953   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
2954   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
2955   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
2956   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
2957   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
2958
2959   /* Count number of gp and fp argument registers used.  */
2960   words = current_function_args_info.words;
2961   n_gpr = current_function_args_info.sysv_gregno - GP_ARG_MIN_REG;
2962   n_fpr = current_function_args_info.fregno - FP_ARG_MIN_REG;
2963
2964   if (TARGET_DEBUG_ARG)
2965     {
2966       fputs ("va_start: words = ", stderr);
2967       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, words);
2968       fputs (", n_gpr = ", stderr);
2969       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, n_gpr);
2970       fputs (", n_fpr = ", stderr);
2971       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, n_fpr);
2972       putc ('\n', stderr);
2973     }
2974
2975   t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, build_int_2 (n_gpr, 0));
2976   TREE_SIDE_EFFECTS (t) = 1;
2977   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2978
2979   t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr, build_int_2 (n_fpr, 0));
2980   TREE_SIDE_EFFECTS (t) = 1;
2981   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2982
2983   /* Find the overflow area.  */
2984   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
2985   if (words != 0)
2986     t = build (PLUS_EXPR, TREE_TYPE (ovf), t,
2987                build_int_2 (words * UNITS_PER_WORD, 0));
2988   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
2989   TREE_SIDE_EFFECTS (t) = 1;
2990   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2991
2992   /* Find the register save area.  */
2993   t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
2994   t = build (PLUS_EXPR, TREE_TYPE (sav), t,
2995              build_int_2 (-RS6000_VARARGS_SIZE, -1));
2996   t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
2997   TREE_SIDE_EFFECTS (t) = 1;
2998   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2999 }
3000
3001 /* Implement va_arg.  */
3002
3003 rtx
3004 rs6000_va_arg (valist, type)
3005      tree valist, type;
3006 {
3007   tree f_gpr, f_fpr, f_ovf, f_sav;
3008   tree gpr, fpr, ovf, sav, reg, t, u;
3009   int indirect_p, size, rsize, n_reg, sav_ofs, sav_scale;
3010   rtx lab_false, lab_over, addr_rtx, r;
3011
3012   /* For AIX, the rule is that structures are passed left-aligned in
3013      their stack slot.  However, GCC does not presently do this:
3014      structures which are the same size as integer types are passed
3015      right-aligned, as if they were in fact integers.  This only
3016      matters for structures of size 1 or 2, or 4 when TARGET_64BIT.  */
3017   if (DEFAULT_ABI != ABI_V4)
3018     {
3019       HOST_WIDE_INT align, rounded_size;
3020       enum machine_mode mode;
3021       tree addr_tree;
3022
3023       /* Compute the rounded size of the type.  */
3024       align = PARM_BOUNDARY / BITS_PER_UNIT;
3025       rounded_size = (((int_size_in_bytes (type) + align - 1) / align)
3026                       * align);
3027
3028       addr_tree = valist;
3029
3030       mode = TYPE_MODE (type);
3031       if (mode != BLKmode)
3032         {
3033           HOST_WIDE_INT adj;
3034           adj = TREE_INT_CST_LOW (TYPE_SIZE (type)) / BITS_PER_UNIT;
3035           if (rounded_size > align)
3036             adj = rounded_size;
3037           
3038           addr_tree = build (PLUS_EXPR, TREE_TYPE (addr_tree), addr_tree,
3039                              build_int_2 (rounded_size - adj, 0));
3040         }
3041
3042       addr_rtx = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
3043       addr_rtx = copy_to_reg (addr_rtx);
3044       
3045       /* Compute new value for AP.  */
3046       t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
3047                  build (PLUS_EXPR, TREE_TYPE (valist), valist,
3048                         build_int_2 (rounded_size, 0)));
3049       TREE_SIDE_EFFECTS (t) = 1;
3050       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3051       
3052       return addr_rtx;
3053     }
3054
3055   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
3056   f_fpr = TREE_CHAIN (f_gpr);
3057   f_ovf = TREE_CHAIN (f_fpr);
3058   f_sav = TREE_CHAIN (f_ovf);
3059
3060   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
3061   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
3062   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
3063   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
3064   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
3065
3066   size = int_size_in_bytes (type);
3067   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3068
3069   if (AGGREGATE_TYPE_P (type) || TYPE_MODE (type) == TFmode)
3070     {
3071       /* Aggregates and long doubles are passed by reference.  */
3072       indirect_p = 1;
3073       reg = gpr;
3074       n_reg = 1;
3075       sav_ofs = 0;
3076       sav_scale = 4;
3077       size = rsize = UNITS_PER_WORD;
3078     }
3079   else if (FLOAT_TYPE_P (type) && ! TARGET_SOFT_FLOAT)
3080     {
3081       /* FP args go in FP registers, if present.  */
3082       indirect_p = 0;
3083       reg = fpr;
3084       n_reg = 1;
3085       sav_ofs = 8*4;
3086       sav_scale = 8;
3087     }
3088   else
3089     {
3090       /* Otherwise into GP registers.  */
3091       indirect_p = 0;
3092       reg = gpr;
3093       n_reg = rsize;
3094       sav_ofs = 0;
3095       sav_scale = 4;
3096     }
3097
3098   /* Pull the value out of the saved registers ...  */
3099
3100   lab_false = gen_label_rtx ();
3101   lab_over = gen_label_rtx ();
3102   addr_rtx = gen_reg_rtx (Pmode);
3103
3104   emit_cmp_and_jump_insns (expand_expr (reg, NULL_RTX, QImode, EXPAND_NORMAL),
3105                            GEN_INT (8 - n_reg + 1), GE, const1_rtx, QImode, 1,
3106                            lab_false);
3107
3108   /* Long long is aligned in the registers.  */
3109   if (n_reg > 1)
3110     {
3111       u = build (BIT_AND_EXPR, TREE_TYPE (reg), reg,
3112                  build_int_2 (n_reg - 1, 0));
3113       u = build (PLUS_EXPR, TREE_TYPE (reg), reg, u);
3114       u = build (MODIFY_EXPR, TREE_TYPE (reg), reg, u);
3115       TREE_SIDE_EFFECTS (u) = 1;
3116       expand_expr (u, const0_rtx, VOIDmode, EXPAND_NORMAL);
3117     }
3118
3119   if (sav_ofs)
3120     t = build (PLUS_EXPR, ptr_type_node, sav, build_int_2 (sav_ofs, 0));
3121   else
3122     t = sav;
3123
3124   u = build (POSTINCREMENT_EXPR, TREE_TYPE (reg), reg, build_int_2 (n_reg, 0));
3125   TREE_SIDE_EFFECTS (u) = 1;
3126
3127   u = build1 (CONVERT_EXPR, integer_type_node, u);
3128   TREE_SIDE_EFFECTS (u) = 1;
3129
3130   u = build (MULT_EXPR, integer_type_node, u, build_int_2 (sav_scale, 0));
3131   TREE_SIDE_EFFECTS (u) = 1;
3132
3133   t = build (PLUS_EXPR, ptr_type_node, t, u);
3134   TREE_SIDE_EFFECTS (t) = 1;
3135
3136   r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
3137   if (r != addr_rtx)
3138     emit_move_insn (addr_rtx, r);
3139
3140   emit_jump_insn (gen_jump (lab_over));
3141   emit_barrier ();
3142   emit_label (lab_false);
3143
3144   /* ... otherwise out of the overflow area.  */
3145
3146   /* Make sure we don't find reg 7 for the next int arg.  */
3147   if (n_reg > 1)
3148     {
3149       t = build (MODIFY_EXPR, TREE_TYPE (reg), reg, build_int_2 (8, 0));
3150       TREE_SIDE_EFFECTS (t) = 1;
3151       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3152     }
3153
3154   /* Care for on-stack alignment if needed.  */
3155   if (rsize <= 1)
3156     t = ovf;
3157   else
3158     {
3159       t = build (PLUS_EXPR, TREE_TYPE (ovf), ovf, build_int_2 (7, 0));
3160       t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-8, -1));
3161     }
3162   t = save_expr (t);
3163
3164   r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
3165   if (r != addr_rtx)
3166     emit_move_insn (addr_rtx, r);
3167
3168   t = build (PLUS_EXPR, TREE_TYPE (t), t, build_int_2 (size, 0));
3169   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
3170   TREE_SIDE_EFFECTS (t) = 1;
3171   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3172
3173   emit_label (lab_over);
3174
3175   if (indirect_p)
3176     {
3177       r = gen_rtx_MEM (Pmode, addr_rtx);
3178       set_mem_alias_set (r, get_varargs_alias_set ());
3179       emit_move_insn (addr_rtx, r);
3180     }
3181
3182   return addr_rtx;
3183 }
3184
3185 /* Builtins.  */
3186
3187 #define def_builtin(MASK, NAME, TYPE, CODE)                             \
3188 do {                                                                    \
3189   if ((MASK) & target_flags)                                            \
3190     builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD, NULL);       \
3191 } while (0)
3192
3193 struct builtin_description
3194 {
3195   const unsigned int mask;
3196   const enum insn_code icode;
3197   const char *const name;
3198   const enum rs6000_builtins code;
3199 };
3200
3201 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc).  */
3202
3203 static const struct builtin_description bdesc_3arg[] =
3204 {
3205   { MASK_ALTIVEC, CODE_FOR_altivec_vmaddfp, "__builtin_altivec_vmaddfp", ALTIVEC_BUILTIN_VMADDFP },
3206   { MASK_ALTIVEC, CODE_FOR_altivec_vmhaddshs, "__builtin_altivec_vmhaddshs", ALTIVEC_BUILTIN_VMHADDSHS },
3207   { MASK_ALTIVEC, CODE_FOR_altivec_vmhraddshs, "__builtin_altivec_vmhraddshs", ALTIVEC_BUILTIN_VMHRADDSHS },
3208   { MASK_ALTIVEC, CODE_FOR_altivec_vmladduhm, "__builtin_altivec_vmladduhm", ALTIVEC_BUILTIN_VMLADDUHM},
3209   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumubm, "__builtin_altivec_vmsumubm", ALTIVEC_BUILTIN_VMSUMUBM },
3210   { MASK_ALTIVEC, CODE_FOR_altivec_vmsummbm, "__builtin_altivec_vmsummbm", ALTIVEC_BUILTIN_VMSUMMBM },
3211   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhm, "__builtin_altivec_vmsumuhm", ALTIVEC_BUILTIN_VMSUMUHM },
3212   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshm, "__builtin_altivec_vmsumshm", ALTIVEC_BUILTIN_VMSUMSHM },
3213   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhs, "__builtin_altivec_vmsumuhs", ALTIVEC_BUILTIN_VMSUMUHS },
3214   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshs, "__builtin_altivec_vmsumshs", ALTIVEC_BUILTIN_VMSUMSHS },
3215   { MASK_ALTIVEC, CODE_FOR_altivec_vnmsubfp, "__builtin_altivec_vnmsubfp", ALTIVEC_BUILTIN_VNMSUBFP }, 
3216   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4sf, "__builtin_altivec_vperm_4sf", ALTIVEC_BUILTIN_VPERM_4SF },
3217   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4si, "__builtin_altivec_vperm_4si", ALTIVEC_BUILTIN_VPERM_4SI },
3218   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_8hi, "__builtin_altivec_vperm_8hi", ALTIVEC_BUILTIN_VPERM_8HI },
3219   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_16qi, "__builtin_altivec_vperm_16qi", ALTIVEC_BUILTIN_VPERM_16QI },
3220   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4sf, "__builtin_altivec_vsel_4sf", ALTIVEC_BUILTIN_VSEL_4SF },
3221   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4si, "__builtin_altivec_vsel_4si", ALTIVEC_BUILTIN_VSEL_4SI },
3222   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_8hi, "__builtin_altivec_vsel_8hi", ALTIVEC_BUILTIN_VSEL_8HI },
3223   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_16qi, "__builtin_altivec_vsel_16qi", ALTIVEC_BUILTIN_VSEL_16QI },
3224   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_16qi, "__builtin_altivec_vsldoi_16qi", ALTIVEC_BUILTIN_VSLDOI_16QI },
3225   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_8hi, "__builtin_altivec_vsldoi_8hi", ALTIVEC_BUILTIN_VSLDOI_8HI },
3226   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4si, "__builtin_altivec_vsldoi_4si", ALTIVEC_BUILTIN_VSLDOI_4SI },
3227   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4sf, "__builtin_altivec_vsldoi_4sf", ALTIVEC_BUILTIN_VSLDOI_4SF },
3228 };
3229
3230 /* DST operations: void foo (void *, const int, const char).  */
3231
3232 static const struct builtin_description bdesc_dst[] =
3233 {
3234   { MASK_ALTIVEC, CODE_FOR_altivec_dst, "__builtin_altivec_dst", ALTIVEC_BUILTIN_DST },
3235   { MASK_ALTIVEC, CODE_FOR_altivec_dstt, "__builtin_altivec_dstt", ALTIVEC_BUILTIN_DSTT },
3236   { MASK_ALTIVEC, CODE_FOR_altivec_dstst, "__builtin_altivec_dstst", ALTIVEC_BUILTIN_DSTST },
3237   { MASK_ALTIVEC, CODE_FOR_altivec_dststt, "__builtin_altivec_dststt", ALTIVEC_BUILTIN_DSTSTT }
3238 };
3239
3240 /* Simple binary operations: VECc = foo (VECa, VECb).  */
3241
3242 static const struct builtin_description bdesc_2arg[] =
3243 {
3244   { MASK_ALTIVEC, CODE_FOR_addv16qi3, "__builtin_altivec_vaddubm", ALTIVEC_BUILTIN_VADDUBM },
3245   { MASK_ALTIVEC, CODE_FOR_addv8hi3, "__builtin_altivec_vadduhm", ALTIVEC_BUILTIN_VADDUHM },
3246   { MASK_ALTIVEC, CODE_FOR_addv4si3, "__builtin_altivec_vadduwm", ALTIVEC_BUILTIN_VADDUWM },
3247   { MASK_ALTIVEC, CODE_FOR_addv4sf3, "__builtin_altivec_vaddfp", ALTIVEC_BUILTIN_VADDFP },
3248   { MASK_ALTIVEC, CODE_FOR_altivec_vaddcuw, "__builtin_altivec_vaddcuw", ALTIVEC_BUILTIN_VADDCUW },
3249   { MASK_ALTIVEC, CODE_FOR_altivec_vaddubs, "__builtin_altivec_vaddubs", ALTIVEC_BUILTIN_VADDUBS },
3250   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsbs, "__builtin_altivec_vaddsbs", ALTIVEC_BUILTIN_VADDSBS },
3251   { MASK_ALTIVEC, CODE_FOR_altivec_vadduhs, "__builtin_altivec_vadduhs", ALTIVEC_BUILTIN_VADDUHS },
3252   { MASK_ALTIVEC, CODE_FOR_altivec_vaddshs, "__builtin_altivec_vaddshs", ALTIVEC_BUILTIN_VADDSHS },
3253   { MASK_ALTIVEC, CODE_FOR_altivec_vadduws, "__builtin_altivec_vadduws", ALTIVEC_BUILTIN_VADDUWS },
3254   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsws, "__builtin_altivec_vaddsws", ALTIVEC_BUILTIN_VADDSWS },
3255   { MASK_ALTIVEC, CODE_FOR_andv4si3, "__builtin_altivec_vand", ALTIVEC_BUILTIN_VAND },
3256   { MASK_ALTIVEC, CODE_FOR_altivec_vandc, "__builtin_altivec_vandc", ALTIVEC_BUILTIN_VANDC },
3257   { MASK_ALTIVEC, CODE_FOR_altivec_vavgub, "__builtin_altivec_vavgub", ALTIVEC_BUILTIN_VAVGUB },
3258   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsb, "__builtin_altivec_vavgsb", ALTIVEC_BUILTIN_VAVGSB },
3259   { MASK_ALTIVEC, CODE_FOR_altivec_vavguh, "__builtin_altivec_vavguh", ALTIVEC_BUILTIN_VAVGUH },
3260   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsh, "__builtin_altivec_vavgsh", ALTIVEC_BUILTIN_VAVGSH },
3261   { MASK_ALTIVEC, CODE_FOR_altivec_vavguw, "__builtin_altivec_vavguw", ALTIVEC_BUILTIN_VAVGUW },
3262   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsw, "__builtin_altivec_vavgsw", ALTIVEC_BUILTIN_VAVGSW },
3263   { MASK_ALTIVEC, CODE_FOR_altivec_vcfux, "__builtin_altivec_vcfux", ALTIVEC_BUILTIN_VCFUX },
3264   { MASK_ALTIVEC, CODE_FOR_altivec_vcfsx, "__builtin_altivec_vcfsx", ALTIVEC_BUILTIN_VCFSX },
3265   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpbfp, "__builtin_altivec_vcmpbfp", ALTIVEC_BUILTIN_VCMPBFP },
3266   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequb, "__builtin_altivec_vcmpequb", ALTIVEC_BUILTIN_VCMPEQUB },
3267   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequh, "__builtin_altivec_vcmpequh", ALTIVEC_BUILTIN_VCMPEQUH },
3268   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequw, "__builtin_altivec_vcmpequw", ALTIVEC_BUILTIN_VCMPEQUW },
3269   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpeqfp, "__builtin_altivec_vcmpeqfp", ALTIVEC_BUILTIN_VCMPEQFP },
3270   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgefp, "__builtin_altivec_vcmpgefp", ALTIVEC_BUILTIN_VCMPGEFP },
3271   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtub, "__builtin_altivec_vcmpgtub", ALTIVEC_BUILTIN_VCMPGTUB },
3272   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsb, "__builtin_altivec_vcmpgtsb", ALTIVEC_BUILTIN_VCMPGTSB },
3273   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuh, "__builtin_altivec_vcmpgtuh", ALTIVEC_BUILTIN_VCMPGTUH },
3274   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsh, "__builtin_altivec_vcmpgtsh", ALTIVEC_BUILTIN_VCMPGTSH },
3275   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuw, "__builtin_altivec_vcmpgtuw", ALTIVEC_BUILTIN_VCMPGTUW },
3276   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsw, "__builtin_altivec_vcmpgtsw", ALTIVEC_BUILTIN_VCMPGTSW },
3277   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtfp, "__builtin_altivec_vcmpgtfp", ALTIVEC_BUILTIN_VCMPGTFP },
3278   { MASK_ALTIVEC, CODE_FOR_altivec_vctsxs, "__builtin_altivec_vctsxs", ALTIVEC_BUILTIN_VCTSXS },
3279   { MASK_ALTIVEC, CODE_FOR_altivec_vctuxs, "__builtin_altivec_vctuxs", ALTIVEC_BUILTIN_VCTUXS },
3280   { MASK_ALTIVEC, CODE_FOR_umaxv16qi3, "__builtin_altivec_vmaxub", ALTIVEC_BUILTIN_VMAXUB },
3281   { MASK_ALTIVEC, CODE_FOR_smaxv16qi3, "__builtin_altivec_vmaxsb", ALTIVEC_BUILTIN_VMAXSB },
3282   { MASK_ALTIVEC, CODE_FOR_uminv8hi3, "__builtin_altivec_vmaxuh", ALTIVEC_BUILTIN_VMAXUH },
3283   { MASK_ALTIVEC, CODE_FOR_sminv8hi3, "__builtin_altivec_vmaxsh", ALTIVEC_BUILTIN_VMAXSH },
3284   { MASK_ALTIVEC, CODE_FOR_uminv4si3, "__builtin_altivec_vmaxuw", ALTIVEC_BUILTIN_VMAXUW },
3285   { MASK_ALTIVEC, CODE_FOR_sminv4si3, "__builtin_altivec_vmaxsw", ALTIVEC_BUILTIN_VMAXSW },
3286   { MASK_ALTIVEC, CODE_FOR_sminv4sf3, "__builtin_altivec_vmaxfp", ALTIVEC_BUILTIN_VMAXFP },
3287   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghb, "__builtin_altivec_vmrghb", ALTIVEC_BUILTIN_VMRGHB },
3288   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghh, "__builtin_altivec_vmrghh", ALTIVEC_BUILTIN_VMRGHH },
3289   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghw, "__builtin_altivec_vmrghw", ALTIVEC_BUILTIN_VMRGHW },
3290   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglb, "__builtin_altivec_vmrglb", ALTIVEC_BUILTIN_VMRGLB },
3291   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglh, "__builtin_altivec_vmrglh", ALTIVEC_BUILTIN_VMRGLH },
3292   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglw, "__builtin_altivec_vmrglw", ALTIVEC_BUILTIN_VMRGLW },
3293   { MASK_ALTIVEC, CODE_FOR_uminv16qi3, "__builtin_altivec_vminub", ALTIVEC_BUILTIN_VMINUB },
3294   { MASK_ALTIVEC, CODE_FOR_sminv16qi3, "__builtin_altivec_vminsb", ALTIVEC_BUILTIN_VMINSB },
3295   { MASK_ALTIVEC, CODE_FOR_uminv8hi3, "__builtin_altivec_vminuh", ALTIVEC_BUILTIN_VMINUH },
3296   { MASK_ALTIVEC, CODE_FOR_sminv8hi3, "__builtin_altivec_vminsh", ALTIVEC_BUILTIN_VMINSH },
3297   { MASK_ALTIVEC, CODE_FOR_uminv4si3, "__builtin_altivec_vminuw", ALTIVEC_BUILTIN_VMINUW },
3298   { MASK_ALTIVEC, CODE_FOR_sminv4si3, "__builtin_altivec_vminsw", ALTIVEC_BUILTIN_VMINSW },
3299   { MASK_ALTIVEC, CODE_FOR_sminv4sf3, "__builtin_altivec_vminfp", ALTIVEC_BUILTIN_VMINFP },
3300   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleub, "__builtin_altivec_vmuleub", ALTIVEC_BUILTIN_VMULEUB },
3301   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesb, "__builtin_altivec_vmulesb", ALTIVEC_BUILTIN_VMULESB },
3302   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleuh, "__builtin_altivec_vmuleuh", ALTIVEC_BUILTIN_VMULEUH },
3303   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesh, "__builtin_altivec_vmulesh", ALTIVEC_BUILTIN_VMULESH },
3304   { MASK_ALTIVEC, CODE_FOR_altivec_vmuloub, "__builtin_altivec_vmuloub", ALTIVEC_BUILTIN_VMULOUB },
3305   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosb, "__builtin_altivec_vmulosb", ALTIVEC_BUILTIN_VMULOSB },
3306   { MASK_ALTIVEC, CODE_FOR_altivec_vmulouh, "__builtin_altivec_vmulouh", ALTIVEC_BUILTIN_VMULOUH },
3307   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosh, "__builtin_altivec_vmulosh", ALTIVEC_BUILTIN_VMULOSH },
3308   { MASK_ALTIVEC, CODE_FOR_altivec_vnor, "__builtin_altivec_vnor", ALTIVEC_BUILTIN_VNOR },
3309   { MASK_ALTIVEC, CODE_FOR_iorv4si3, "__builtin_altivec_vor", ALTIVEC_BUILTIN_VOR },
3310   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum, "__builtin_altivec_vpkuhum", ALTIVEC_BUILTIN_VPKUHUM },
3311   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum, "__builtin_altivec_vpkuwum", ALTIVEC_BUILTIN_VPKUWUM },
3312   { MASK_ALTIVEC, CODE_FOR_altivec_vpkpx, "__builtin_altivec_vpkpx", ALTIVEC_BUILTIN_VPKPX },
3313   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhss, "__builtin_altivec_vpkuhss", ALTIVEC_BUILTIN_VPKUHSS },
3314   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshss, "__builtin_altivec_vpkshss", ALTIVEC_BUILTIN_VPKSHSS },
3315   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwss, "__builtin_altivec_vpkuwss", ALTIVEC_BUILTIN_VPKUWSS },
3316   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswss, "__builtin_altivec_vpkswss", ALTIVEC_BUILTIN_VPKSWSS },
3317   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhus, "__builtin_altivec_vpkuhus", ALTIVEC_BUILTIN_VPKUHUS },
3318   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshus, "__builtin_altivec_vpkshus", ALTIVEC_BUILTIN_VPKSHUS },
3319   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwus, "__builtin_altivec_vpkuwus", ALTIVEC_BUILTIN_VPKUWUS },
3320   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswus, "__builtin_altivec_vpkswus", ALTIVEC_BUILTIN_VPKSWUS },
3321   { MASK_ALTIVEC, CODE_FOR_altivec_vrlb, "__builtin_altivec_vrlb", ALTIVEC_BUILTIN_VRLB },
3322   { MASK_ALTIVEC, CODE_FOR_altivec_vrlh, "__builtin_altivec_vrlh", ALTIVEC_BUILTIN_VRLH },
3323   { MASK_ALTIVEC, CODE_FOR_altivec_vrlw, "__builtin_altivec_vrlw", ALTIVEC_BUILTIN_VRLW },
3324   { MASK_ALTIVEC, CODE_FOR_altivec_vslb, "__builtin_altivec_vslb", ALTIVEC_BUILTIN_VSLB },
3325   { MASK_ALTIVEC, CODE_FOR_altivec_vslh, "__builtin_altivec_vslh", ALTIVEC_BUILTIN_VSLH },
3326   { MASK_ALTIVEC, CODE_FOR_altivec_vslw, "__builtin_altivec_vslw", ALTIVEC_BUILTIN_VSLW },
3327   { MASK_ALTIVEC, CODE_FOR_altivec_vsl, "__builtin_altivec_vsl", ALTIVEC_BUILTIN_VSL },
3328   { MASK_ALTIVEC, CODE_FOR_altivec_vslo, "__builtin_altivec_vslo", ALTIVEC_BUILTIN_VSLO },
3329   { MASK_ALTIVEC, CODE_FOR_altivec_vspltb, "__builtin_altivec_vspltb", ALTIVEC_BUILTIN_VSPLTB },
3330   { MASK_ALTIVEC, CODE_FOR_altivec_vsplth, "__builtin_altivec_vsplth", ALTIVEC_BUILTIN_VSPLTH },
3331   { MASK_ALTIVEC, CODE_FOR_altivec_vspltw, "__builtin_altivec_vspltw", ALTIVEC_BUILTIN_VSPLTW },
3332   { MASK_ALTIVEC, CODE_FOR_altivec_vsrb, "__builtin_altivec_vsrb", ALTIVEC_BUILTIN_VSRB },
3333   { MASK_ALTIVEC, CODE_FOR_altivec_vsrh, "__builtin_altivec_vsrh", ALTIVEC_BUILTIN_VSRH },
3334   { MASK_ALTIVEC, CODE_FOR_altivec_vsrw, "__builtin_altivec_vsrw", ALTIVEC_BUILTIN_VSRW },
3335   { MASK_ALTIVEC, CODE_FOR_altivec_vsrab, "__builtin_altivec_vsrab", ALTIVEC_BUILTIN_VSRAB },
3336   { MASK_ALTIVEC, CODE_FOR_altivec_vsrah, "__builtin_altivec_vsrah", ALTIVEC_BUILTIN_VSRAH },
3337   { MASK_ALTIVEC, CODE_FOR_altivec_vsraw, "__builtin_altivec_vsraw", ALTIVEC_BUILTIN_VSRAW },
3338   { MASK_ALTIVEC, CODE_FOR_altivec_vsr, "__builtin_altivec_vsr", ALTIVEC_BUILTIN_VSR },
3339   { MASK_ALTIVEC, CODE_FOR_altivec_vsro, "__builtin_altivec_vsro", ALTIVEC_BUILTIN_VSRO },
3340   { MASK_ALTIVEC, CODE_FOR_subv16qi3, "__builtin_altivec_vsububm", ALTIVEC_BUILTIN_VSUBUBM },
3341   { MASK_ALTIVEC, CODE_FOR_subv8hi3, "__builtin_altivec_vsubuhm", ALTIVEC_BUILTIN_VSUBUHM },
3342   { MASK_ALTIVEC, CODE_FOR_subv4si3, "__builtin_altivec_vsubuwm", ALTIVEC_BUILTIN_VSUBUWM },
3343   { MASK_ALTIVEC, CODE_FOR_subv4sf3, "__builtin_altivec_vsubfp", ALTIVEC_BUILTIN_VSUBFP },
3344   { MASK_ALTIVEC, CODE_FOR_altivec_vsubcuw, "__builtin_altivec_vsubcuw", ALTIVEC_BUILTIN_VSUBCUW },
3345   { MASK_ALTIVEC, CODE_FOR_altivec_vsububs, "__builtin_altivec_vsububs", ALTIVEC_BUILTIN_VSUBUBS },
3346   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsbs, "__builtin_altivec_vsubsbs", ALTIVEC_BUILTIN_VSUBSBS },
3347   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuhs, "__builtin_altivec_vsubuhs", ALTIVEC_BUILTIN_VSUBUHS },
3348   { MASK_ALTIVEC, CODE_FOR_altivec_vsubshs, "__builtin_altivec_vsubshs", ALTIVEC_BUILTIN_VSUBSHS },
3349   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuws, "__builtin_altivec_vsubuws", ALTIVEC_BUILTIN_VSUBUWS },
3350   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsws, "__builtin_altivec_vsubsws", ALTIVEC_BUILTIN_VSUBSWS },
3351   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4ubs, "__builtin_altivec_vsum4ubs", ALTIVEC_BUILTIN_VSUM4UBS },
3352   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4sbs, "__builtin_altivec_vsum4sbs", ALTIVEC_BUILTIN_VSUM4SBS },
3353   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4shs, "__builtin_altivec_vsum4shs", ALTIVEC_BUILTIN_VSUM4SHS },
3354   { MASK_ALTIVEC, CODE_FOR_altivec_vsum2sws, "__builtin_altivec_vsum2sws", ALTIVEC_BUILTIN_VSUM2SWS },
3355   { MASK_ALTIVEC, CODE_FOR_altivec_vsumsws, "__builtin_altivec_vsumsws", ALTIVEC_BUILTIN_VSUMSWS },
3356   { MASK_ALTIVEC, CODE_FOR_xorv4si3, "__builtin_altivec_vxor", ALTIVEC_BUILTIN_VXOR },
3357 };
3358
3359 /* AltiVec predicates.  */
3360
3361 struct builtin_description_predicates
3362 {
3363   const unsigned int mask;
3364   const enum insn_code icode;
3365   const char *opcode;
3366   const char *const name;
3367   const enum rs6000_builtins code;
3368 };
3369
3370 static const struct builtin_description_predicates bdesc_altivec_preds[] =
3371 {
3372   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpbfp.", "__builtin_altivec_vcmpbfp_p", ALTIVEC_BUILTIN_VCMPBFP_P },
3373   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpeqfp.", "__builtin_altivec_vcmpeqfp_p", ALTIVEC_BUILTIN_VCMPEQFP_P },
3374   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgefp.", "__builtin_altivec_vcmpgefp_p", ALTIVEC_BUILTIN_VCMPGEFP_P },
3375   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgtfp.", "__builtin_altivec_vcmpgtfp_p", ALTIVEC_BUILTIN_VCMPGTFP_P },
3376   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpequw.", "__builtin_altivec_vcmpequw_p", ALTIVEC_BUILTIN_VCMPEQUW_P },
3377   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtsw.", "__builtin_altivec_vcmpgtsw_p", ALTIVEC_BUILTIN_VCMPGTSW_P },
3378   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtuw.", "__builtin_altivec_vcmpgtuw_p", ALTIVEC_BUILTIN_VCMPGTUW_P },
3379   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtuh.", "__builtin_altivec_vcmpgtuh_p", ALTIVEC_BUILTIN_VCMPGTUH_P },
3380   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtsh.", "__builtin_altivec_vcmpgtsh_p", ALTIVEC_BUILTIN_VCMPGTSH_P },
3381   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpequh.", "__builtin_altivec_vcmpequh_p", ALTIVEC_BUILTIN_VCMPEQUH_P },
3382   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpequb.", "__builtin_altivec_vcmpequb_p", ALTIVEC_BUILTIN_VCMPEQUB_P },
3383   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtsb.", "__builtin_altivec_vcmpgtsb_p", ALTIVEC_BUILTIN_VCMPGTSB_P },
3384   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtub.", "__builtin_altivec_vcmpgtub_p", ALTIVEC_BUILTIN_VCMPGTUB_P }
3385 };
3386
3387 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
3388    foo (VECa).  */
3389
3390 static const struct builtin_description bdesc_1arg[] =
3391 {
3392   { MASK_ALTIVEC, CODE_FOR_altivec_vexptefp, "__builtin_altivec_vexptefp", ALTIVEC_BUILTIN_VEXPTEFP },
3393   { MASK_ALTIVEC, CODE_FOR_altivec_vlogefp, "__builtin_altivec_vlogefp", ALTIVEC_BUILTIN_VLOGEFP },
3394   { MASK_ALTIVEC, CODE_FOR_altivec_vrefp, "__builtin_altivec_vrefp", ALTIVEC_BUILTIN_VREFP },
3395   { MASK_ALTIVEC, CODE_FOR_altivec_vrfim, "__builtin_altivec_vrfim", ALTIVEC_BUILTIN_VRFIM },
3396   { MASK_ALTIVEC, CODE_FOR_altivec_vrfin, "__builtin_altivec_vrfin", ALTIVEC_BUILTIN_VRFIN },
3397   { MASK_ALTIVEC, CODE_FOR_altivec_vrfip, "__builtin_altivec_vrfip", ALTIVEC_BUILTIN_VRFIP },
3398   { MASK_ALTIVEC, CODE_FOR_ftruncv4sf2, "__builtin_altivec_vrfiz", ALTIVEC_BUILTIN_VRFIZ },
3399   { MASK_ALTIVEC, CODE_FOR_altivec_vrsqrtefp, "__builtin_altivec_vrsqrtefp", ALTIVEC_BUILTIN_VRSQRTEFP },
3400   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisb, "__builtin_altivec_vspltisb", ALTIVEC_BUILTIN_VSPLTISB },
3401   { MASK_ALTIVEC, CODE_FOR_altivec_vspltish, "__builtin_altivec_vspltish", ALTIVEC_BUILTIN_VSPLTISH },
3402   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisw, "__builtin_altivec_vspltisw", ALTIVEC_BUILTIN_VSPLTISW },
3403   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsb, "__builtin_altivec_vupkhsb", ALTIVEC_BUILTIN_VUPKHSB },
3404   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhpx, "__builtin_altivec_vupkhpx", ALTIVEC_BUILTIN_VUPKHPX },
3405   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsh, "__builtin_altivec_vupkhsh", ALTIVEC_BUILTIN_VUPKHSH },
3406   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsb, "__builtin_altivec_vupklsb", ALTIVEC_BUILTIN_VUPKLSB },
3407   { MASK_ALTIVEC, CODE_FOR_altivec_vupklpx, "__builtin_altivec_vupklpx", ALTIVEC_BUILTIN_VUPKLPX },
3408   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsh, "__builtin_altivec_vupklsh", ALTIVEC_BUILTIN_VUPKLSH },
3409 };
3410
3411 static rtx
3412 altivec_expand_unop_builtin (icode, arglist, target)
3413      enum insn_code icode;
3414      tree arglist;
3415      rtx target;
3416 {
3417   rtx pat;
3418   tree arg0 = TREE_VALUE (arglist);
3419   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3420   enum machine_mode tmode = insn_data[icode].operand[0].mode;
3421   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3422
3423   /* If we got invalid arguments bail out before generating bad rtl.  */
3424   if (arg0 == error_mark_node)
3425     return NULL_RTX;
3426
3427   if (target == 0
3428       || GET_MODE (target) != tmode
3429       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3430     target = gen_reg_rtx (tmode);
3431
3432   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3433     op0 = copy_to_mode_reg (mode0, op0);
3434
3435   pat = GEN_FCN (icode) (target, op0);
3436   if (! pat)
3437     return 0;
3438   emit_insn (pat);
3439
3440   return target;
3441 }
3442
3443 static rtx
3444 altivec_expand_binop_builtin (icode, arglist, target)
3445      enum insn_code icode;
3446      tree arglist;
3447      rtx target;
3448 {
3449   rtx pat;
3450   tree arg0 = TREE_VALUE (arglist);
3451   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3452   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3453   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3454   enum machine_mode tmode = insn_data[icode].operand[0].mode;
3455   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3456   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
3457
3458   /* If we got invalid arguments bail out before generating bad rtl.  */
3459   if (arg0 == error_mark_node || arg1 == error_mark_node)
3460     return NULL_RTX;
3461
3462   if (target == 0
3463       || GET_MODE (target) != tmode
3464       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3465     target = gen_reg_rtx (tmode);
3466
3467   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3468     op0 = copy_to_mode_reg (mode0, op0);
3469   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
3470     op1 = copy_to_mode_reg (mode1, op1);
3471
3472   pat = GEN_FCN (icode) (target, op0, op1);
3473   if (! pat)
3474     return 0;
3475   emit_insn (pat);
3476
3477   return target;
3478 }
3479
3480 static rtx
3481 altivec_expand_predicate_builtin (icode, opcode, arglist, target)
3482      enum insn_code icode;
3483      const char *opcode;
3484      tree arglist;
3485      rtx target;
3486 {
3487   rtx pat, scratch;
3488   tree cr6_form = TREE_VALUE (arglist);
3489   tree arg0 = TREE_VALUE (TREE_CHAIN (arglist));
3490   tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3491   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3492   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3493   enum machine_mode tmode = SImode;
3494   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3495   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
3496   int cr6_form_int;
3497
3498   if (TREE_CODE (cr6_form) != INTEGER_CST)
3499     {
3500       error ("argument 1 of __builtin_altivec_predicate must be a constant");
3501       return NULL_RTX;
3502     }
3503   else
3504     cr6_form_int = TREE_INT_CST_LOW (cr6_form);
3505
3506   if (mode0 != mode1)
3507     abort ();
3508
3509   /* If we have invalid arguments, bail out before generating bad rtl.  */
3510   if (arg0 == error_mark_node || arg1 == error_mark_node)
3511     return NULL_RTX;
3512
3513   if (target == 0
3514       || GET_MODE (target) != tmode
3515       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3516     target = gen_reg_rtx (tmode);
3517
3518   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3519     op0 = copy_to_mode_reg (mode0, op0);
3520   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
3521     op1 = copy_to_mode_reg (mode1, op1);
3522
3523   scratch = gen_reg_rtx (mode0);
3524
3525   pat = GEN_FCN (icode) (scratch, op0, op1,
3526                          gen_rtx (SYMBOL_REF, Pmode, opcode));
3527   if (! pat)
3528     return 0;
3529   emit_insn (pat);
3530
3531   /* The vec_any* and vec_all* predicates use the same opcodes for two
3532      different operations, but the bits in CR6 will be different
3533      depending on what information we want.  So we have to play tricks
3534      with CR6 to get the right bits out.
3535
3536      If you think this is disgusting, look at the specs for the
3537      AltiVec predicates.  */
3538
3539      switch (cr6_form_int)
3540        {
3541        case 0:
3542          emit_insn (gen_cr6_test_for_zero (target));
3543          break;
3544        case 1:
3545          emit_insn (gen_cr6_test_for_zero_reverse (target));
3546          break;
3547        case 2:
3548          emit_insn (gen_cr6_test_for_lt (target));
3549          break;
3550        case 3:
3551          emit_insn (gen_cr6_test_for_lt_reverse (target));
3552          break;
3553        default:
3554          error ("argument 1 of __builtin_altivec_predicate is out of range");
3555          break;
3556        }
3557
3558   return target;
3559 }
3560
3561 static rtx
3562 altivec_expand_stv_builtin (icode, arglist)
3563      enum insn_code icode;
3564      tree arglist;
3565 {
3566   tree arg0 = TREE_VALUE (arglist);
3567   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3568   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3569   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3570   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3571   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
3572   rtx pat;
3573   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
3574   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
3575   enum machine_mode mode2 = insn_data[icode].operand[2].mode;
3576
3577   /* Invalid arguments.  Bail before doing anything stoopid!  */
3578   if (arg0 == error_mark_node
3579       || arg1 == error_mark_node
3580       || arg2 == error_mark_node)
3581     return NULL_RTX;
3582
3583   if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
3584     op0 = copy_to_mode_reg (mode2, op0);
3585   if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
3586     op1 = copy_to_mode_reg (mode0, op1);
3587   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
3588     op2 = copy_to_mode_reg (mode1, op2);
3589
3590   pat = GEN_FCN (icode) (op1, op2, op0);
3591   if (pat)
3592     emit_insn (pat);
3593   return NULL_RTX;
3594 }
3595
3596 static rtx
3597 altivec_expand_ternop_builtin (icode, arglist, target)
3598      enum insn_code icode;
3599      tree arglist;
3600      rtx target;
3601 {
3602   rtx pat;
3603   tree arg0 = TREE_VALUE (arglist);
3604   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3605   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3606   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3607   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3608   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
3609   enum machine_mode tmode = insn_data[icode].operand[0].mode;
3610   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3611   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
3612   enum machine_mode mode2 = insn_data[icode].operand[3].mode;
3613
3614   /* If we got invalid arguments bail out before generating bad rtl.  */
3615   if (arg0 == error_mark_node
3616       || arg1 == error_mark_node
3617       || arg2 == error_mark_node)
3618     return NULL_RTX;
3619
3620   if (target == 0
3621       || GET_MODE (target) != tmode
3622       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3623     target = gen_reg_rtx (tmode);
3624
3625   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3626     op0 = copy_to_mode_reg (mode0, op0);
3627   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
3628     op1 = copy_to_mode_reg (mode1, op1);
3629   if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
3630     op2 = copy_to_mode_reg (mode2, op2);
3631
3632   pat = GEN_FCN (icode) (target, op0, op1, op2);
3633   if (! pat)
3634     return 0;
3635   emit_insn (pat);
3636
3637   return target;
3638 }
3639 static rtx
3640 altivec_expand_builtin (exp, target)
3641      tree exp;
3642      rtx target;
3643 {
3644   struct builtin_description *d;
3645   struct builtin_description_predicates *dp;
3646   size_t i;
3647   enum insn_code icode;
3648   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
3649   tree arglist = TREE_OPERAND (exp, 1);
3650   tree arg0, arg1, arg2;
3651   rtx op0, op1, op2, pat;
3652   enum machine_mode tmode, mode0, mode1, mode2;
3653   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3654   
3655   switch (fcode)
3656     {
3657     case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
3658       icode = CODE_FOR_altivec_lvx_16qi;
3659       arg0 = TREE_VALUE (arglist);
3660       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3661       tmode = insn_data[icode].operand[0].mode;
3662       mode0 = insn_data[icode].operand[1].mode;
3663
3664       if (target == 0
3665           || GET_MODE (target) != tmode
3666           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3667         target = gen_reg_rtx (tmode);
3668
3669       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3670         op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3671
3672       pat = GEN_FCN (icode) (target, op0);
3673       if (! pat)
3674         return 0;
3675       emit_insn (pat);
3676       return target;
3677
3678     case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
3679       icode = CODE_FOR_altivec_lvx_8hi;
3680       arg0 = TREE_VALUE (arglist);
3681       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3682       tmode = insn_data[icode].operand[0].mode;
3683       mode0 = insn_data[icode].operand[1].mode;
3684
3685       if (target == 0
3686           || GET_MODE (target) != tmode
3687           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3688         target = gen_reg_rtx (tmode);
3689
3690       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3691         op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3692
3693       pat = GEN_FCN (icode) (target, op0);
3694       if (! pat)
3695         return 0;
3696       emit_insn (pat);
3697       return target;
3698
3699     case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
3700       icode = CODE_FOR_altivec_lvx_4si;
3701       arg0 = TREE_VALUE (arglist);
3702       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3703       tmode = insn_data[icode].operand[0].mode;
3704       mode0 = insn_data[icode].operand[1].mode;
3705
3706       if (target == 0
3707           || GET_MODE (target) != tmode
3708           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3709         target = gen_reg_rtx (tmode);
3710
3711       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3712         op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3713
3714       pat = GEN_FCN (icode) (target, op0);
3715       if (! pat)
3716         return 0;
3717       emit_insn (pat);
3718       return target;
3719
3720     case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
3721       icode = CODE_FOR_altivec_lvx_4sf;
3722       arg0 = TREE_VALUE (arglist);
3723       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3724       tmode = insn_data[icode].operand[0].mode;
3725       mode0 = insn_data[icode].operand[1].mode;
3726
3727       if (target == 0
3728           || GET_MODE (target) != tmode
3729           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3730         target = gen_reg_rtx (tmode);
3731
3732       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3733         op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3734
3735       pat = GEN_FCN (icode) (target, op0);
3736       if (! pat)
3737         return 0;
3738       emit_insn (pat);
3739       return target;
3740
3741     case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
3742       icode = CODE_FOR_altivec_stvx_16qi;
3743       arg0 = TREE_VALUE (arglist);
3744       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3745       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3746       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3747       mode0 = insn_data[icode].operand[0].mode;
3748       mode1 = insn_data[icode].operand[1].mode;
3749
3750       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
3751         op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3752       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
3753         op1 = copy_to_mode_reg (mode1, op1);
3754
3755       pat = GEN_FCN (icode) (op0, op1);
3756       if (pat)
3757         emit_insn (pat);
3758       return NULL_RTX;
3759
3760     case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
3761       icode = CODE_FOR_altivec_stvx_8hi;
3762       arg0 = TREE_VALUE (arglist);
3763       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3764       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3765       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3766       mode0 = insn_data[icode].operand[0].mode;
3767       mode1 = insn_data[icode].operand[1].mode;
3768
3769       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
3770         op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3771       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
3772         op1 = copy_to_mode_reg (mode1, op1);
3773
3774       pat = GEN_FCN (icode) (op0, op1);
3775       if (pat)
3776         emit_insn (pat);
3777       return NULL_RTX;
3778
3779     case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
3780       icode = CODE_FOR_altivec_stvx_4si;
3781       arg0 = TREE_VALUE (arglist);
3782       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3783       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3784       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3785       mode0 = insn_data[icode].operand[0].mode;
3786       mode1 = insn_data[icode].operand[1].mode;
3787
3788       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
3789         op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3790       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
3791         op1 = copy_to_mode_reg (mode1, op1);
3792
3793       pat = GEN_FCN (icode) (op0, op1);
3794       if (pat)
3795         emit_insn (pat);
3796       return NULL_RTX;
3797
3798     case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
3799       icode = CODE_FOR_altivec_stvx_4sf;
3800       arg0 = TREE_VALUE (arglist);
3801       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3802       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3803       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3804       mode0 = insn_data[icode].operand[0].mode;
3805       mode1 = insn_data[icode].operand[1].mode;
3806
3807       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
3808         op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3809       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
3810         op1 = copy_to_mode_reg (mode1, op1);
3811
3812       pat = GEN_FCN (icode) (op0, op1);
3813       if (pat)
3814         emit_insn (pat);
3815       return NULL_RTX;
3816
3817     case ALTIVEC_BUILTIN_STVX:
3818       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx, arglist);
3819     case ALTIVEC_BUILTIN_STVEBX:
3820       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, arglist);
3821     case ALTIVEC_BUILTIN_STVEHX:
3822       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, arglist);
3823     case ALTIVEC_BUILTIN_STVEWX:
3824       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, arglist);
3825     case ALTIVEC_BUILTIN_STVXL:
3826       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl, arglist);
3827   
3828     case ALTIVEC_BUILTIN_MFVSCR:
3829       icode = CODE_FOR_altivec_mfvscr;
3830       tmode = insn_data[icode].operand[0].mode;
3831
3832       if (target == 0
3833           || GET_MODE (target) != tmode
3834           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
3835         target = gen_reg_rtx (tmode);
3836       
3837       pat = GEN_FCN (icode) (target);
3838       if (! pat)
3839         return 0;
3840       emit_insn (pat);
3841       return target;
3842
3843     case ALTIVEC_BUILTIN_MTVSCR:
3844       icode = CODE_FOR_altivec_mtvscr;
3845       arg0 = TREE_VALUE (arglist);
3846       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3847       mode0 = insn_data[icode].operand[0].mode;
3848
3849       /* If we got invalid arguments bail out before generating bad rtl.  */
3850       if (arg0 == error_mark_node)
3851         return NULL_RTX;
3852
3853       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
3854         op0 = copy_to_mode_reg (mode0, op0);
3855
3856       pat = GEN_FCN (icode) (op0);
3857       if (pat)
3858         emit_insn (pat);
3859       return NULL_RTX;
3860       
3861     case ALTIVEC_BUILTIN_DSSALL:
3862       emit_insn (gen_altivec_dssall ());
3863       return NULL_RTX;
3864
3865     case ALTIVEC_BUILTIN_DSS:
3866       icode = CODE_FOR_altivec_dss;
3867       arg0 = TREE_VALUE (arglist);
3868       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3869       mode0 = insn_data[icode].operand[0].mode;
3870
3871       /* If we got invalid arguments bail out before generating bad rtl.  */
3872       if (arg0 == error_mark_node)
3873         return NULL_RTX;
3874
3875       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
3876         op0 = copy_to_mode_reg (mode0, op0);
3877
3878       emit_insn (gen_altivec_dss (op0));
3879       return NULL_RTX;
3880     }
3881
3882   /* Handle DST variants.  */
3883   d = (struct builtin_description *) bdesc_dst;
3884   for (i = 0; i < sizeof (bdesc_dst) / sizeof *d; i++, d++)
3885     if (d->code == fcode)
3886       {
3887         arg0 = TREE_VALUE (arglist);
3888         arg1 = TREE_VALUE (TREE_CHAIN (arglist));
3889         arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
3890         op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
3891         op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
3892         op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
3893         mode0 = insn_data[d->icode].operand[0].mode;
3894         mode1 = insn_data[d->icode].operand[1].mode;
3895         mode2 = insn_data[d->icode].operand[2].mode;
3896
3897         /* Invalid arguments, bail out before generating bad rtl.  */
3898         if (arg0 == error_mark_node
3899             || arg1 == error_mark_node
3900             || arg2 == error_mark_node)
3901           return NULL_RTX;
3902
3903         if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
3904           op0 = copy_to_mode_reg (mode0, op0);
3905         if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
3906           op1 = copy_to_mode_reg (mode1, op1);
3907
3908         if (GET_CODE (op2) != CONST_INT || INTVAL (op2) > 3)
3909           {
3910             error ("argument 3 of `%s' must be a 2-bit literal", d->name);
3911             return NULL_RTX;
3912           }
3913
3914         pat = GEN_FCN (d->icode) (op0, op1, op2);
3915         if (pat != 0)
3916           emit_insn (pat);
3917
3918         return NULL_RTX;
3919       }
3920
3921   /* Handle simple unary operations.  */
3922   d = (struct builtin_description *) bdesc_1arg;
3923   for (i = 0; i < sizeof (bdesc_1arg) / sizeof *d; i++, d++)
3924     if (d->code == fcode)
3925       return altivec_expand_unop_builtin (d->icode, arglist, target);
3926
3927   /* Handle simple binary operations.  */
3928   d = (struct builtin_description *) bdesc_2arg;
3929   for (i = 0; i < sizeof (bdesc_2arg) / sizeof *d; i++, d++)
3930     if (d->code == fcode)
3931       return altivec_expand_binop_builtin (d->icode, arglist, target);
3932
3933   /* Expand the AltiVec predicates.  */
3934   dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
3935   for (i = 0; i < sizeof (bdesc_altivec_preds) / sizeof *dp; i++, dp++)
3936     if (dp->code == fcode)
3937       return altivec_expand_predicate_builtin (dp->icode, dp->opcode, arglist, target);
3938
3939   /* LV* are funky.  We initialized them differently.  */
3940   switch (fcode)
3941     {
3942     case ALTIVEC_BUILTIN_LVSL:
3943       return altivec_expand_binop_builtin (CODE_FOR_altivec_lvsl,
3944                                            arglist, target);
3945     case ALTIVEC_BUILTIN_LVSR:
3946       return altivec_expand_binop_builtin (CODE_FOR_altivec_lvsr,
3947                                            arglist, target);
3948     case ALTIVEC_BUILTIN_LVEBX:
3949       return altivec_expand_binop_builtin (CODE_FOR_altivec_lvebx,
3950                                            arglist, target);
3951     case ALTIVEC_BUILTIN_LVEHX:
3952       return altivec_expand_binop_builtin (CODE_FOR_altivec_lvehx,
3953                                            arglist, target);
3954     case ALTIVEC_BUILTIN_LVEWX:
3955       return altivec_expand_binop_builtin (CODE_FOR_altivec_lvewx,
3956                                            arglist, target);
3957     case ALTIVEC_BUILTIN_LVXL:
3958       return altivec_expand_binop_builtin (CODE_FOR_altivec_lvxl,
3959                                            arglist, target);
3960     case ALTIVEC_BUILTIN_LVX:
3961       return altivec_expand_binop_builtin (CODE_FOR_altivec_lvx,
3962                                            arglist, target);
3963     default:
3964       break;
3965       /* Fall through.  */
3966     }
3967
3968   /* Handle simple ternary operations.  */
3969   d = (struct builtin_description *) bdesc_3arg;
3970   for (i = 0; i < sizeof  (bdesc_3arg) / sizeof *d; i++, d++)
3971     if (d->code == fcode)
3972       return altivec_expand_ternop_builtin (d->icode, arglist, target);
3973
3974   abort ();
3975   return NULL_RTX;
3976 }
3977
3978 /* Expand an expression EXP that calls a built-in function,
3979    with result going to TARGET if that's convenient
3980    (and in mode MODE if that's convenient).
3981    SUBTARGET may be used as the target for computing one of EXP's operands.
3982    IGNORE is nonzero if the value is to be ignored.  */
3983
3984 static rtx
3985 rs6000_expand_builtin (exp, target, subtarget, mode, ignore)
3986      tree exp;
3987      rtx target;
3988      rtx subtarget ATTRIBUTE_UNUSED;
3989      enum machine_mode mode ATTRIBUTE_UNUSED;
3990      int ignore ATTRIBUTE_UNUSED;
3991 {
3992   if (TARGET_ALTIVEC)
3993     return altivec_expand_builtin (exp, target);
3994
3995   abort ();
3996 }
3997
3998 static void
3999 rs6000_init_builtins ()
4000 {
4001   if (TARGET_ALTIVEC)
4002     altivec_init_builtins ();
4003 }
4004
4005 static void
4006 altivec_init_builtins (void)
4007 {
4008   struct builtin_description *d;
4009   struct builtin_description_predicates *dp;
4010   size_t i;
4011
4012   tree endlink = void_list_node;
4013
4014   tree pint_type_node = build_pointer_type (integer_type_node);
4015   tree pvoid_type_node = build_pointer_type (void_type_node);
4016   tree pshort_type_node = build_pointer_type (short_integer_type_node);
4017   tree pchar_type_node = build_pointer_type (char_type_node);
4018   tree pfloat_type_node = build_pointer_type (float_type_node);
4019
4020   tree v4sf_ftype_v4sf_v4sf_v16qi
4021     = build_function_type (V4SF_type_node,
4022                            tree_cons (NULL_TREE, V4SF_type_node,
4023                                       tree_cons (NULL_TREE, V4SF_type_node,
4024                                                  tree_cons (NULL_TREE, 
4025                                                             V16QI_type_node,
4026                                                             endlink))));
4027   tree v4si_ftype_v4si_v4si_v16qi
4028     = build_function_type (V4SI_type_node,
4029                            tree_cons (NULL_TREE, V4SI_type_node,
4030                                       tree_cons (NULL_TREE, V4SI_type_node,
4031                                                  tree_cons (NULL_TREE, 
4032                                                             V16QI_type_node,
4033                                                             endlink))));
4034   tree v8hi_ftype_v8hi_v8hi_v16qi
4035     = build_function_type (V8HI_type_node,
4036                            tree_cons (NULL_TREE, V8HI_type_node,
4037                                       tree_cons (NULL_TREE, V8HI_type_node,
4038                                                  tree_cons (NULL_TREE, 
4039                                                             V16QI_type_node,
4040                                                             endlink))));
4041   tree v16qi_ftype_v16qi_v16qi_v16qi
4042     = build_function_type (V16QI_type_node,
4043                            tree_cons (NULL_TREE, V16QI_type_node,
4044                                       tree_cons (NULL_TREE, V16QI_type_node,
4045                                                  tree_cons (NULL_TREE, 
4046                                                             V16QI_type_node,
4047                                                             endlink))));
4048
4049   /* V4SI foo (char).  */
4050   tree v4si_ftype_char
4051     = build_function_type (V4SI_type_node,
4052                            tree_cons (NULL_TREE, char_type_node, endlink));
4053
4054   /* V8HI foo (char).  */
4055   tree v8hi_ftype_char
4056     = build_function_type (V8HI_type_node,
4057                            tree_cons (NULL_TREE, char_type_node, endlink));
4058
4059   /* V16QI foo (char).  */
4060   tree v16qi_ftype_char
4061     = build_function_type (V16QI_type_node,
4062                            tree_cons (NULL_TREE, char_type_node, endlink));
4063   /* V4SF foo (V4SF).  */
4064   tree v4sf_ftype_v4sf
4065     = build_function_type (V4SF_type_node,
4066                            tree_cons (NULL_TREE, V4SF_type_node, endlink));
4067
4068   /* V4SI foo (int *).  */
4069   tree v4si_ftype_pint
4070     = build_function_type (V4SI_type_node,
4071                            tree_cons (NULL_TREE, pint_type_node, endlink));
4072   /* V8HI foo (short *).  */
4073   tree v8hi_ftype_pshort
4074     = build_function_type (V8HI_type_node,
4075                            tree_cons (NULL_TREE, pshort_type_node, endlink));
4076   /* V16QI foo (char *).  */
4077   tree v16qi_ftype_pchar
4078     = build_function_type (V16QI_type_node,
4079                            tree_cons (NULL_TREE, pchar_type_node, endlink));
4080   /* V4SF foo (float *).  */
4081   tree v4sf_ftype_pfloat
4082     = build_function_type (V4SF_type_node,
4083                            tree_cons (NULL_TREE, pfloat_type_node, endlink));
4084
4085   /* V8HI foo (V16QI).  */
4086   tree v8hi_ftype_v16qi
4087     = build_function_type (V8HI_type_node,
4088                            tree_cons (NULL_TREE, V16QI_type_node, endlink));
4089
4090   /* void foo (void *, int, char/literal).  */
4091   tree void_ftype_pvoid_int_char
4092     = build_function_type (void_type_node,
4093                            tree_cons (NULL_TREE, pvoid_type_node,
4094                                       tree_cons (NULL_TREE, integer_type_node,
4095                                                  tree_cons (NULL_TREE,
4096                                                             char_type_node,
4097                                                             endlink))));
4098
4099   /* void foo (int *, V4SI).  */
4100   tree void_ftype_pint_v4si
4101     = build_function_type (void_type_node,
4102                            tree_cons (NULL_TREE, pint_type_node,
4103                                       tree_cons (NULL_TREE, V4SI_type_node,
4104                                                  endlink)));
4105   /* void foo (short *, V8HI).  */
4106   tree void_ftype_pshort_v8hi
4107     = build_function_type (void_type_node,
4108                            tree_cons (NULL_TREE, pshort_type_node,
4109                                       tree_cons (NULL_TREE, V8HI_type_node,
4110                                                  endlink)));
4111   /* void foo (char *, V16QI).  */
4112   tree void_ftype_pchar_v16qi
4113     = build_function_type (void_type_node,
4114                            tree_cons (NULL_TREE, pchar_type_node,
4115                                       tree_cons (NULL_TREE, V16QI_type_node,
4116                                                  endlink)));
4117   /* void foo (float *, V4SF).  */
4118   tree void_ftype_pfloat_v4sf
4119     = build_function_type (void_type_node,
4120                            tree_cons (NULL_TREE, pfloat_type_node,
4121                                       tree_cons (NULL_TREE, V4SF_type_node,
4122                                                  endlink)));
4123
4124   /* void foo (V4SI).  */
4125   tree void_ftype_v4si
4126     = build_function_type (void_type_node,
4127                            tree_cons (NULL_TREE, V4SI_type_node,
4128                                       endlink));
4129
4130   /* void foo (vint, int, void *).  */
4131   tree void_ftype_v4si_int_pvoid
4132     = build_function_type (void_type_node,
4133                            tree_cons (NULL_TREE, V4SI_type_node,
4134                                       tree_cons (NULL_TREE, integer_type_node,
4135                                                  tree_cons (NULL_TREE,
4136                                                             pvoid_type_node,
4137                                                             endlink))));
4138
4139   /* void foo (vchar, int, void *).  */
4140   tree void_ftype_v16qi_int_pvoid
4141     = build_function_type (void_type_node,
4142                            tree_cons (NULL_TREE, V16QI_type_node,
4143                                       tree_cons (NULL_TREE, integer_type_node,
4144                                                  tree_cons (NULL_TREE,
4145                                                             pvoid_type_node,
4146                                                             endlink))));
4147
4148   /* void foo (vshort, int, void *).  */
4149   tree void_ftype_v8hi_int_pvoid
4150     = build_function_type (void_type_node,
4151                            tree_cons (NULL_TREE, V8HI_type_node,
4152                                       tree_cons (NULL_TREE, integer_type_node,
4153                                                  tree_cons (NULL_TREE,
4154                                                             pvoid_type_node,
4155                                                             endlink))));
4156
4157   /* void foo (char).  */
4158   tree void_ftype_qi
4159     = build_function_type (void_type_node,
4160                            tree_cons (NULL_TREE, char_type_node,
4161                                       endlink));
4162
4163   /* void foo (void).  */
4164   tree void_ftype_void
4165     = build_function_type (void_type_node, void_list_node);
4166
4167   /* vshort foo (void).  */
4168   tree v8hi_ftype_void
4169     = build_function_type (V8HI_type_node, void_list_node);
4170
4171   tree v4si_ftype_v4si_v4si
4172     = build_function_type (V4SI_type_node,
4173                            tree_cons (NULL_TREE, V4SI_type_node,
4174                                       tree_cons (NULL_TREE, V4SI_type_node,
4175                                                  endlink)));
4176
4177   /* These are for the unsigned 5 bit literals.  */
4178
4179   tree v4sf_ftype_v4si_char
4180     = build_function_type (V4SF_type_node,
4181                            tree_cons (NULL_TREE, V4SI_type_node,
4182                                       tree_cons (NULL_TREE, char_type_node,
4183                                                  endlink)));
4184   tree v4si_ftype_v4sf_char
4185     = build_function_type (V4SI_type_node,
4186                            tree_cons (NULL_TREE, V4SF_type_node,
4187                                       tree_cons (NULL_TREE, char_type_node,
4188                                                  endlink)));
4189   tree v4si_ftype_v4si_char
4190     = build_function_type (V4SI_type_node,
4191                            tree_cons (NULL_TREE, V4SI_type_node,
4192                                       tree_cons (NULL_TREE, char_type_node,
4193                                                  endlink)));
4194   tree v8hi_ftype_v8hi_char
4195     = build_function_type (V8HI_type_node,
4196                            tree_cons (NULL_TREE, V8HI_type_node,
4197                                       tree_cons (NULL_TREE, char_type_node,
4198                                                  endlink)));
4199   tree v16qi_ftype_v16qi_char
4200     = build_function_type (V16QI_type_node,
4201                            tree_cons (NULL_TREE, V16QI_type_node,
4202                                       tree_cons (NULL_TREE, char_type_node,
4203                                                  endlink)));
4204
4205   /* These are for the unsigned 4 bit literals.  */
4206
4207   tree v16qi_ftype_v16qi_v16qi_char
4208     = build_function_type (V16QI_type_node,
4209                            tree_cons (NULL_TREE, V16QI_type_node,
4210                                       tree_cons (NULL_TREE, V16QI_type_node,
4211                                                  tree_cons (NULL_TREE,
4212                                                             char_type_node,
4213                                                             endlink))));
4214
4215   tree v8hi_ftype_v8hi_v8hi_char
4216     = build_function_type (V8HI_type_node,
4217                            tree_cons (NULL_TREE, V8HI_type_node,
4218                                       tree_cons (NULL_TREE, V8HI_type_node,
4219                                                  tree_cons (NULL_TREE,
4220                                                             char_type_node,
4221                                                             endlink))));
4222
4223   tree v4si_ftype_v4si_v4si_char
4224     = build_function_type (V4SI_type_node,
4225                            tree_cons (NULL_TREE, V4SI_type_node,
4226                                       tree_cons (NULL_TREE, V4SI_type_node,
4227                                                  tree_cons (NULL_TREE,
4228                                                             char_type_node,
4229                                                             endlink))));
4230
4231   tree v4sf_ftype_v4sf_v4sf_char
4232     = build_function_type (V4SF_type_node,
4233                            tree_cons (NULL_TREE, V4SF_type_node,
4234                                       tree_cons (NULL_TREE, V4SF_type_node,
4235                                                  tree_cons (NULL_TREE,
4236                                                             char_type_node,
4237                                                             endlink))));
4238
4239   /* End of 4 bit literals.  */
4240
4241   tree v4sf_ftype_v4sf_v4sf
4242     = build_function_type (V4SF_type_node,
4243                            tree_cons (NULL_TREE, V4SF_type_node,
4244                                       tree_cons (NULL_TREE, V4SF_type_node,
4245                                                  endlink)));
4246   tree v4sf_ftype_v4sf_v4sf_v4si
4247     = build_function_type (V4SF_type_node,
4248                            tree_cons (NULL_TREE, V4SF_type_node,
4249                                       tree_cons (NULL_TREE, V4SF_type_node,
4250                                                  tree_cons (NULL_TREE,
4251                                                             V4SI_type_node,
4252                                                             endlink))));
4253   tree v4sf_ftype_v4sf_v4sf_v4sf
4254     = build_function_type (V4SF_type_node,
4255                            tree_cons (NULL_TREE, V4SF_type_node,
4256                                       tree_cons (NULL_TREE, V4SF_type_node,
4257                                                  tree_cons (NULL_TREE, 
4258                                                             V4SF_type_node,
4259                                                             endlink))));
4260   tree v4si_ftype_v4si_v4si_v4si 
4261     = build_function_type (V4SI_type_node,
4262                            tree_cons (NULL_TREE, V4SI_type_node,
4263                                       tree_cons (NULL_TREE, V4SI_type_node,
4264                                                  tree_cons (NULL_TREE,
4265                                                             V4SI_type_node,
4266                                                             endlink))));
4267
4268   tree v8hi_ftype_v8hi_v8hi
4269     = build_function_type (V8HI_type_node,
4270                            tree_cons (NULL_TREE, V8HI_type_node,
4271                                       tree_cons (NULL_TREE, V8HI_type_node,
4272                                                  endlink)));
4273   tree v8hi_ftype_v8hi_v8hi_v8hi
4274     = build_function_type (V8HI_type_node,
4275                            tree_cons (NULL_TREE, V8HI_type_node,
4276                                       tree_cons (NULL_TREE, V8HI_type_node,
4277                                                  tree_cons (NULL_TREE, 
4278                                                             V8HI_type_node,
4279                                                             endlink))));
4280  tree v4si_ftype_v8hi_v8hi_v4si
4281     = build_function_type (V4SI_type_node,
4282                            tree_cons (NULL_TREE, V8HI_type_node,
4283                                       tree_cons (NULL_TREE, V8HI_type_node,
4284                                                  tree_cons (NULL_TREE,
4285                                                             V4SI_type_node,
4286                                                             endlink))));
4287  tree v4si_ftype_v16qi_v16qi_v4si
4288     = build_function_type (V4SI_type_node,
4289                            tree_cons (NULL_TREE, V16QI_type_node,
4290                                       tree_cons (NULL_TREE, V16QI_type_node,
4291                                                  tree_cons (NULL_TREE,
4292                                                             V4SI_type_node,
4293                                                             endlink))));
4294   
4295   tree v16qi_ftype_v16qi_v16qi
4296     = build_function_type (V16QI_type_node,
4297                            tree_cons (NULL_TREE, V16QI_type_node,
4298                                       tree_cons (NULL_TREE, V16QI_type_node,
4299                                                  endlink)));
4300   
4301   tree v4si_ftype_v4sf_v4sf
4302     = build_function_type (V4SI_type_node,
4303                            tree_cons (NULL_TREE, V4SF_type_node,
4304                                       tree_cons (NULL_TREE, V4SF_type_node,
4305                                                  endlink)));
4306
4307   tree v8hi_ftype_v16qi_v16qi
4308     = build_function_type (V8HI_type_node,
4309                            tree_cons (NULL_TREE, V16QI_type_node,
4310                                       tree_cons (NULL_TREE, V16QI_type_node,
4311                                                  endlink)));
4312
4313   tree v4si_ftype_v8hi_v8hi
4314     = build_function_type (V4SI_type_node,
4315                            tree_cons (NULL_TREE, V8HI_type_node,
4316                                       tree_cons (NULL_TREE, V8HI_type_node,
4317                                                  endlink)));
4318
4319   tree v8hi_ftype_v4si_v4si
4320     = build_function_type (V8HI_type_node,
4321                            tree_cons (NULL_TREE, V4SI_type_node,
4322                                       tree_cons (NULL_TREE, V4SI_type_node,
4323                                                  endlink)));
4324
4325   tree v16qi_ftype_v8hi_v8hi
4326     = build_function_type (V16QI_type_node,
4327                            tree_cons (NULL_TREE, V8HI_type_node,
4328                                       tree_cons (NULL_TREE, V8HI_type_node,
4329                                                  endlink)));
4330
4331   tree v4si_ftype_v16qi_v4si
4332     = build_function_type (V4SI_type_node,
4333                            tree_cons (NULL_TREE, V16QI_type_node,
4334                                       tree_cons (NULL_TREE, V4SI_type_node,
4335                                                  endlink)));
4336
4337   tree v4si_ftype_v16qi_v16qi
4338     = build_function_type (V4SI_type_node,
4339                            tree_cons (NULL_TREE, V16QI_type_node,
4340                                       tree_cons (NULL_TREE, V16QI_type_node,
4341                                                  endlink)));
4342
4343   tree v4si_ftype_v8hi_v4si
4344     = build_function_type (V4SI_type_node,
4345                            tree_cons (NULL_TREE, V8HI_type_node,
4346                                       tree_cons (NULL_TREE, V4SI_type_node,
4347                                                  endlink)));
4348
4349   tree v4si_ftype_v8hi
4350     = build_function_type (V4SI_type_node,
4351                            tree_cons (NULL_TREE, V8HI_type_node, endlink));
4352
4353   tree int_ftype_v4si_v4si
4354     = build_function_type (integer_type_node,
4355                            tree_cons (NULL_TREE, V4SI_type_node,
4356                                       tree_cons (NULL_TREE, V4SI_type_node,
4357                                                  endlink)));
4358
4359   tree int_ftype_v4sf_v4sf
4360     = build_function_type (integer_type_node,
4361                            tree_cons (NULL_TREE, V4SF_type_node,
4362                                       tree_cons (NULL_TREE, V4SF_type_node,
4363                                                  endlink)));
4364
4365   tree int_ftype_v16qi_v16qi
4366     = build_function_type (integer_type_node,
4367                            tree_cons (NULL_TREE, V16QI_type_node,
4368                                       tree_cons (NULL_TREE, V16QI_type_node,
4369                                                  endlink)));
4370
4371   tree int_ftype_int_v4si_v4si
4372     = build_function_type
4373     (integer_type_node,
4374      tree_cons (NULL_TREE, integer_type_node,
4375                 tree_cons (NULL_TREE, V4SI_type_node,
4376                            tree_cons (NULL_TREE, V4SI_type_node,
4377                                       endlink))));
4378
4379   tree int_ftype_int_v4sf_v4sf
4380     = build_function_type
4381     (integer_type_node,
4382      tree_cons (NULL_TREE, integer_type_node,
4383                 tree_cons (NULL_TREE, V4SF_type_node,
4384                            tree_cons (NULL_TREE, V4SF_type_node,
4385                                       endlink))));
4386
4387   tree int_ftype_int_v8hi_v8hi
4388     = build_function_type
4389     (integer_type_node,
4390      tree_cons (NULL_TREE, integer_type_node,
4391                  tree_cons (NULL_TREE, V8HI_type_node,
4392                             tree_cons (NULL_TREE, V8HI_type_node,
4393                                        endlink))));
4394
4395   tree int_ftype_int_v16qi_v16qi
4396     = build_function_type
4397     (integer_type_node,
4398      tree_cons (NULL_TREE, integer_type_node,
4399                 tree_cons (NULL_TREE, V16QI_type_node,
4400                            tree_cons (NULL_TREE, V16QI_type_node,
4401                                       endlink))));
4402
4403   tree v16qi_ftype_int_pvoid
4404     = build_function_type (V16QI_type_node,
4405                            tree_cons (NULL_TREE, integer_type_node,
4406                                       tree_cons (NULL_TREE, pvoid_type_node,
4407                                                  endlink)));
4408
4409   tree v4si_ftype_int_pvoid
4410     = build_function_type (V4SI_type_node,
4411                            tree_cons (NULL_TREE, integer_type_node,
4412                                       tree_cons (NULL_TREE, pvoid_type_node,
4413                                                  endlink)));
4414
4415   tree v8hi_ftype_int_pvoid
4416     = build_function_type (V8HI_type_node,
4417                            tree_cons (NULL_TREE, integer_type_node,
4418                                       tree_cons (NULL_TREE, pvoid_type_node,
4419                                                  endlink)));
4420
4421   tree int_ftype_v8hi_v8hi
4422     = build_function_type (integer_type_node,
4423                            tree_cons (NULL_TREE, V8HI_type_node,
4424                                       tree_cons (NULL_TREE, V8HI_type_node,
4425                                                  endlink)));
4426
4427   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4sf", v4sf_ftype_pfloat, ALTIVEC_BUILTIN_LD_INTERNAL_4sf);
4428   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4sf", void_ftype_pfloat_v4sf, ALTIVEC_BUILTIN_ST_INTERNAL_4sf);
4429   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4si", v4si_ftype_pint, ALTIVEC_BUILTIN_LD_INTERNAL_4si);
4430   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4si", void_ftype_pint_v4si, ALTIVEC_BUILTIN_ST_INTERNAL_4si);
4431   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_8hi", v8hi_ftype_pshort, ALTIVEC_BUILTIN_LD_INTERNAL_8hi);
4432   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_8hi", void_ftype_pshort_v8hi, ALTIVEC_BUILTIN_ST_INTERNAL_8hi);
4433   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_16qi", v16qi_ftype_pchar, ALTIVEC_BUILTIN_LD_INTERNAL_16qi);
4434   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_16qi", void_ftype_pchar_v16qi, ALTIVEC_BUILTIN_ST_INTERNAL_16qi);
4435   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
4436   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
4437   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
4438   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dss", void_ftype_qi, ALTIVEC_BUILTIN_DSS);
4439   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsl", v16qi_ftype_int_pvoid, ALTIVEC_BUILTIN_LVSL);
4440   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsr", v16qi_ftype_int_pvoid, ALTIVEC_BUILTIN_LVSR);
4441   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvebx", v16qi_ftype_int_pvoid, ALTIVEC_BUILTIN_LVEBX);
4442   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvehx", v8hi_ftype_int_pvoid, ALTIVEC_BUILTIN_LVEHX);
4443   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvewx", v4si_ftype_int_pvoid, ALTIVEC_BUILTIN_LVEWX);
4444   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvxl", v4si_ftype_int_pvoid, ALTIVEC_BUILTIN_LVXL);
4445   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvx", v4si_ftype_int_pvoid, ALTIVEC_BUILTIN_LVX);
4446   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvx", void_ftype_v4si_int_pvoid, ALTIVEC_BUILTIN_STVX);
4447   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvebx", void_ftype_v16qi_int_pvoid, ALTIVEC_BUILTIN_STVEBX);
4448   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvehx", void_ftype_v8hi_int_pvoid, ALTIVEC_BUILTIN_STVEHX);
4449   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvewx", void_ftype_v4si_int_pvoid, ALTIVEC_BUILTIN_STVEWX);
4450   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvxl", void_ftype_v4si_int_pvoid, ALTIVEC_BUILTIN_STVXL);
4451   
4452   /* Add the simple ternary operators.  */
4453   d = (struct builtin_description *) bdesc_3arg;
4454   for (i = 0; i < sizeof (bdesc_3arg) / sizeof *d; i++, d++)
4455     {
4456       
4457       enum machine_mode mode0, mode1, mode2, mode3;
4458       tree type;
4459
4460       if (d->name == 0)
4461         continue;
4462       
4463       mode0 = insn_data[d->icode].operand[0].mode;
4464       mode1 = insn_data[d->icode].operand[1].mode;
4465       mode2 = insn_data[d->icode].operand[2].mode;
4466       mode3 = insn_data[d->icode].operand[3].mode;
4467       
4468       /* When all four are of the same mode.  */
4469       if (mode0 == mode1 && mode1 == mode2 && mode2 == mode3)
4470         {
4471           switch (mode0)
4472             {
4473             case V4SImode:
4474               type = v4si_ftype_v4si_v4si_v4si;
4475               break;
4476             case V4SFmode:
4477               type = v4sf_ftype_v4sf_v4sf_v4sf;
4478               break;
4479             case V8HImode:
4480               type = v8hi_ftype_v8hi_v8hi_v8hi;
4481               break;          
4482             case V16QImode:
4483               type = v16qi_ftype_v16qi_v16qi_v16qi;
4484               break;          
4485             default:
4486               abort();        
4487             }
4488         }
4489       else if (mode0 == mode1 && mode1 == mode2 && mode3 == V16QImode)
4490         {
4491           switch (mode0)
4492             {
4493             case V4SImode:
4494               type = v4si_ftype_v4si_v4si_v16qi;
4495               break;
4496             case V4SFmode:
4497               type = v4sf_ftype_v4sf_v4sf_v16qi;
4498               break;
4499             case V8HImode:
4500               type = v8hi_ftype_v8hi_v8hi_v16qi;
4501               break;          
4502             case V16QImode:
4503               type = v16qi_ftype_v16qi_v16qi_v16qi;
4504               break;          
4505             default:
4506               abort();        
4507             }
4508         }
4509       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode 
4510                && mode3 == V4SImode)
4511         type = v4si_ftype_v16qi_v16qi_v4si;
4512       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode 
4513                && mode3 == V4SImode)
4514         type = v4si_ftype_v8hi_v8hi_v4si;
4515       else if (mode0 == V4SFmode && mode1 == V4SFmode && mode2 == V4SFmode 
4516                && mode3 == V4SImode)
4517         type = v4sf_ftype_v4sf_v4sf_v4si;
4518
4519       /* vchar, vchar, vchar, 4 bit literal.  */
4520       else if (mode0 == V16QImode && mode1 == mode0 && mode2 == mode0
4521                && mode3 == QImode)
4522         type = v16qi_ftype_v16qi_v16qi_char;
4523
4524       /* vshort, vshort, vshort, 4 bit literal.  */
4525       else if (mode0 == V8HImode && mode1 == mode0 && mode2 == mode0
4526                && mode3 == QImode)
4527         type = v8hi_ftype_v8hi_v8hi_char;
4528
4529       /* vint, vint, vint, 4 bit literal.  */
4530       else if (mode0 == V4SImode && mode1 == mode0 && mode2 == mode0
4531                && mode3 == QImode)
4532         type = v4si_ftype_v4si_v4si_char;
4533
4534       /* vfloat, vfloat, vfloat, 4 bit literal.  */
4535       else if (mode0 == V4SFmode && mode1 == mode0 && mode2 == mode0
4536                && mode3 == QImode)
4537         type = v4sf_ftype_v4sf_v4sf_char;
4538
4539       else
4540         abort ();
4541
4542       def_builtin (d->mask, d->name, type, d->code);
4543     }
4544
4545   /* Add the DST variants.  */
4546   d = (struct builtin_description *) bdesc_dst;
4547   for (i = 0; i < sizeof (bdesc_dst) / sizeof *d; i++, d++)
4548     def_builtin (d->mask, d->name, void_ftype_pvoid_int_char, d->code);
4549
4550   /* Initialize the predicates.  */
4551   dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
4552   for (i = 0; i < sizeof (bdesc_altivec_preds) / sizeof *dp; i++, dp++)
4553     {
4554       enum machine_mode mode1;
4555       tree type;
4556
4557       mode1 = insn_data[dp->icode].operand[1].mode;
4558
4559       switch (mode1)
4560         {
4561         case V4SImode:
4562           type = int_ftype_int_v4si_v4si;
4563           break;
4564         case V8HImode:
4565           type = int_ftype_int_v8hi_v8hi;
4566           break;
4567         case V16QImode:
4568           type = int_ftype_int_v16qi_v16qi;
4569           break;
4570         case V4SFmode:
4571           type = int_ftype_int_v4sf_v4sf;
4572           break;
4573         default:
4574           abort ();
4575         }
4576       
4577       def_builtin (dp->mask, dp->name, type, dp->code);
4578     }
4579
4580   /* Add the simple binary operators.  */
4581   d = (struct builtin_description *) bdesc_2arg;
4582   for (i = 0; i < sizeof (bdesc_2arg) / sizeof *d; i++, d++)
4583     {
4584       enum machine_mode mode0, mode1, mode2;
4585       tree type;
4586
4587       if (d->name == 0)
4588         continue;
4589       
4590       mode0 = insn_data[d->icode].operand[0].mode;
4591       mode1 = insn_data[d->icode].operand[1].mode;
4592       mode2 = insn_data[d->icode].operand[2].mode;
4593
4594       /* When all three operands are of the same mode.  */
4595       if (mode0 == mode1 && mode1 == mode2)
4596         {
4597           switch (mode0)
4598             {
4599             case V4SFmode:
4600               type = v4sf_ftype_v4sf_v4sf;
4601               break;
4602             case V4SImode:
4603               type = v4si_ftype_v4si_v4si;
4604               break;
4605             case V16QImode:
4606               type = v16qi_ftype_v16qi_v16qi;
4607               break;
4608             case V8HImode:
4609               type = v8hi_ftype_v8hi_v8hi;
4610               break;
4611             default:
4612               abort ();
4613             }
4614         }
4615
4616       /* A few other combos we really don't want to do manually.  */
4617
4618       /* vint, vfloat, vfloat.  */
4619       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == V4SFmode)
4620         type = v4si_ftype_v4sf_v4sf;
4621
4622       /* vshort, vchar, vchar.  */
4623       else if (mode0 == V8HImode && mode1 == V16QImode && mode2 == V16QImode)
4624         type = v8hi_ftype_v16qi_v16qi;
4625
4626       /* vint, vshort, vshort.  */
4627       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode)
4628         type = v4si_ftype_v8hi_v8hi;
4629
4630       /* vshort, vint, vint.  */
4631       else if (mode0 == V8HImode && mode1 == V4SImode && mode2 == V4SImode)
4632         type = v8hi_ftype_v4si_v4si;
4633
4634       /* vchar, vshort, vshort.  */
4635       else if (mode0 == V16QImode && mode1 == V8HImode && mode2 == V8HImode)
4636         type = v16qi_ftype_v8hi_v8hi;
4637
4638       /* vint, vchar, vint.  */
4639       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V4SImode)
4640         type = v4si_ftype_v16qi_v4si;
4641
4642       /* vint, vchar, vchar.  */
4643       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode)
4644         type = v4si_ftype_v16qi_v16qi;
4645
4646       /* vint, vshort, vint.  */
4647       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V4SImode)
4648         type = v4si_ftype_v8hi_v4si;
4649       
4650       /* vint, vint, 5 bit literal.  */
4651       else if (mode0 == V4SImode && mode1 == V4SImode && mode2 == QImode)
4652         type = v4si_ftype_v4si_char;
4653       
4654       /* vshort, vshort, 5 bit literal.  */
4655       else if (mode0 == V8HImode && mode1 == V8HImode && mode2 == QImode)
4656         type = v8hi_ftype_v8hi_char;
4657       
4658       /* vchar, vchar, 5 bit literal.  */
4659       else if (mode0 == V16QImode && mode1 == V16QImode && mode2 == QImode)
4660         type = v16qi_ftype_v16qi_char;
4661
4662       /* vfloat, vint, 5 bit literal.  */
4663       else if (mode0 == V4SFmode && mode1 == V4SImode && mode2 == QImode)
4664         type = v4sf_ftype_v4si_char;
4665       
4666       /* vint, vfloat, 5 bit literal.  */
4667       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == QImode)
4668         type = v4si_ftype_v4sf_char;
4669
4670       /* int, x, x.  */
4671       else if (mode0 == SImode)
4672         {
4673           switch (mode1)
4674             {
4675             case V4SImode:
4676               type = int_ftype_v4si_v4si;
4677               break;
4678             case V4SFmode:
4679               type = int_ftype_v4sf_v4sf;
4680               break;
4681             case V16QImode:
4682               type = int_ftype_v16qi_v16qi;
4683               break;
4684             case V8HImode:
4685               type = int_ftype_v8hi_v8hi;
4686               break;
4687             default:
4688               abort ();
4689             }
4690         }
4691
4692       else
4693         abort ();
4694
4695       def_builtin (d->mask, d->name, type, d->code);
4696     }
4697
4698   /* Add the simple unary operators.  */
4699   d = (struct builtin_description *) bdesc_1arg;
4700   for (i = 0; i < sizeof (bdesc_1arg) / sizeof *d; i++, d++)
4701     {
4702       enum machine_mode mode0, mode1;
4703       tree type;
4704
4705       if (d->name == 0)
4706         continue;
4707       
4708       mode0 = insn_data[d->icode].operand[0].mode;
4709       mode1 = insn_data[d->icode].operand[1].mode;
4710
4711       if (mode0 == V4SImode && mode1 == QImode)
4712         type = v4si_ftype_char;
4713       else if (mode0 == V8HImode && mode1 == QImode)
4714         type = v8hi_ftype_char;
4715       else if (mode0 == V16QImode && mode1 == QImode)
4716         type = v16qi_ftype_char;
4717       else if (mode0 == V4SFmode && mode1 == V4SFmode)
4718         type = v4sf_ftype_v4sf;
4719       else if (mode0 == V8HImode && mode1 == V16QImode)
4720         type = v8hi_ftype_v16qi;
4721       else if (mode0 == V4SImode && mode1 == V8HImode)
4722         type = v4si_ftype_v8hi;
4723       else
4724         abort ();
4725
4726       def_builtin (d->mask, d->name, type, d->code);
4727     }
4728 }
4729
4730 \f
4731 /* Generate a memory reference for expand_block_move, copying volatile,
4732    and other bits from an original memory reference.  */
4733
4734 static rtx
4735 expand_block_move_mem (mode, addr, orig_mem)
4736      enum machine_mode mode;
4737      rtx addr;
4738      rtx orig_mem;
4739 {
4740   rtx mem = gen_rtx_MEM (mode, addr);
4741
4742   MEM_COPY_ATTRIBUTES (mem, orig_mem);
4743   return mem;
4744 }
4745
4746 /* Expand a block move operation, and return 1 if successful.  Return 0
4747    if we should let the compiler generate normal code.
4748
4749    operands[0] is the destination
4750    operands[1] is the source
4751    operands[2] is the length
4752    operands[3] is the alignment */
4753
4754 #define MAX_MOVE_REG 4
4755
4756 int
4757 expand_block_move (operands)
4758      rtx operands[];
4759 {
4760   rtx orig_dest = operands[0];
4761   rtx orig_src  = operands[1];
4762   rtx bytes_rtx = operands[2];
4763   rtx align_rtx = operands[3];
4764   int constp    = (GET_CODE (bytes_rtx) == CONST_INT);
4765   int align;
4766   int bytes;
4767   int offset;
4768   int num_reg;
4769   int i;
4770   rtx src_reg;
4771   rtx dest_reg;
4772   rtx src_addr;
4773   rtx dest_addr;
4774   rtx tmp_reg;
4775   rtx stores[MAX_MOVE_REG];
4776   int move_bytes;
4777
4778   /* If this is not a fixed size move, just call memcpy */
4779   if (! constp)
4780     return 0;
4781
4782   /* If this is not a fixed size alignment, abort */
4783   if (GET_CODE (align_rtx) != CONST_INT)
4784     abort ();
4785   align = INTVAL (align_rtx);
4786
4787   /* Anything to move? */
4788   bytes = INTVAL (bytes_rtx);
4789   if (bytes <= 0)
4790     return 1;
4791
4792   /* store_one_arg depends on expand_block_move to handle at least the size of
4793      reg_parm_stack_space.  */  
4794   if (bytes > (TARGET_POWERPC64 ? 64 : 32))
4795     return 0;
4796
4797   /* Move the address into scratch registers.  */
4798   dest_reg = copy_addr_to_reg (XEXP (orig_dest, 0));
4799   src_reg  = copy_addr_to_reg (XEXP (orig_src,  0));
4800
4801   if (TARGET_STRING)    /* string instructions are available */
4802     {
4803       for ( ; bytes > 0; bytes -= move_bytes)
4804         {
4805           if (bytes > 24                /* move up to 32 bytes at a time */
4806               && ! fixed_regs[5]
4807               && ! fixed_regs[6]
4808               && ! fixed_regs[7]
4809               && ! fixed_regs[8]
4810               && ! fixed_regs[9]
4811               && ! fixed_regs[10]
4812               && ! fixed_regs[11]
4813               && ! fixed_regs[12])
4814             {
4815               move_bytes = (bytes > 32) ? 32 : bytes;
4816               emit_insn (gen_movstrsi_8reg (expand_block_move_mem (BLKmode,
4817                                                                    dest_reg,
4818                                                                    orig_dest),
4819                                             expand_block_move_mem (BLKmode,
4820                                                                    src_reg,
4821                                                                    orig_src),
4822                                             GEN_INT ((move_bytes == 32)
4823                                                      ? 0 : move_bytes),
4824                                             align_rtx));
4825             }
4826           else if (bytes > 16   /* move up to 24 bytes at a time */
4827                    && ! fixed_regs[5]
4828                    && ! fixed_regs[6]
4829                    && ! fixed_regs[7]
4830                    && ! fixed_regs[8]
4831                    && ! fixed_regs[9]
4832                    && ! fixed_regs[10])
4833             {
4834               move_bytes = (bytes > 24) ? 24 : bytes;
4835               emit_insn (gen_movstrsi_6reg (expand_block_move_mem (BLKmode,
4836                                                                    dest_reg,
4837                                                                    orig_dest),
4838                                             expand_block_move_mem (BLKmode,
4839                                                                    src_reg,
4840                                                                    orig_src),
4841                                             GEN_INT (move_bytes),
4842                                             align_rtx));
4843             }
4844           else if (bytes > 8    /* move up to 16 bytes at a time */
4845                    && ! fixed_regs[5]
4846                    && ! fixed_regs[6]
4847                    && ! fixed_regs[7]
4848                    && ! fixed_regs[8])
4849             {
4850               move_bytes = (bytes > 16) ? 16 : bytes;
4851               emit_insn (gen_movstrsi_4reg (expand_block_move_mem (BLKmode,
4852                                                                    dest_reg,
4853                                                                    orig_dest),
4854                                             expand_block_move_mem (BLKmode,
4855                                                                    src_reg,
4856                                                                    orig_src),
4857                                             GEN_INT (move_bytes),
4858                                             align_rtx));
4859             }
4860           else if (bytes >= 8 && TARGET_POWERPC64
4861                    /* 64-bit loads and stores require word-aligned
4862                       displacements.  */
4863                    && (align >= 8 || (! STRICT_ALIGNMENT && align >= 4)))
4864             {
4865               move_bytes = 8;
4866               tmp_reg = gen_reg_rtx (DImode);
4867               emit_move_insn (tmp_reg,
4868                               expand_block_move_mem (DImode,
4869                                                      src_reg, orig_src));
4870               emit_move_insn (expand_block_move_mem (DImode,
4871                                                      dest_reg, orig_dest),
4872                               tmp_reg);
4873             }
4874           else if (bytes > 4 && !TARGET_POWERPC64)
4875             {                   /* move up to 8 bytes at a time */
4876               move_bytes = (bytes > 8) ? 8 : bytes;
4877               emit_insn (gen_movstrsi_2reg (expand_block_move_mem (BLKmode,
4878                                                                    dest_reg,
4879                                                                    orig_dest),
4880                                             expand_block_move_mem (BLKmode,
4881                                                                    src_reg,
4882                                                                    orig_src),
4883                                             GEN_INT (move_bytes),
4884                                             align_rtx));
4885             }
4886           else if (bytes >= 4 && (align >= 4 || ! STRICT_ALIGNMENT))
4887             {                   /* move 4 bytes */
4888               move_bytes = 4;
4889               tmp_reg = gen_reg_rtx (SImode);
4890               emit_move_insn (tmp_reg,
4891                               expand_block_move_mem (SImode,
4892                                                      src_reg, orig_src));
4893               emit_move_insn (expand_block_move_mem (SImode,
4894                                                      dest_reg, orig_dest),
4895                               tmp_reg);
4896             }
4897           else if (bytes == 2 && (align >= 2 || ! STRICT_ALIGNMENT))
4898             {                   /* move 2 bytes */
4899               move_bytes = 2;
4900               tmp_reg = gen_reg_rtx (HImode);
4901               emit_move_insn (tmp_reg,
4902                               expand_block_move_mem (HImode,
4903                                                      src_reg, orig_src));
4904               emit_move_insn (expand_block_move_mem (HImode,
4905                                                      dest_reg, orig_dest),
4906                               tmp_reg);
4907             }
4908           else if (bytes == 1)  /* move 1 byte */
4909             {
4910               move_bytes = 1;
4911               tmp_reg = gen_reg_rtx (QImode);
4912               emit_move_insn (tmp_reg,
4913                               expand_block_move_mem (QImode,
4914                                                      src_reg, orig_src));
4915               emit_move_insn (expand_block_move_mem (QImode,
4916                                                      dest_reg, orig_dest),
4917                               tmp_reg);
4918             }
4919           else
4920             {                   /* move up to 4 bytes at a time */
4921               move_bytes = (bytes > 4) ? 4 : bytes;
4922               emit_insn (gen_movstrsi_1reg (expand_block_move_mem (BLKmode,
4923                                                                    dest_reg,
4924                                                                    orig_dest),
4925                                             expand_block_move_mem (BLKmode,
4926                                                                    src_reg,
4927                                                                    orig_src),
4928                                             GEN_INT (move_bytes),
4929                                             align_rtx));
4930             }
4931
4932           if (bytes > move_bytes)
4933             {
4934               if (! TARGET_POWERPC64)
4935                 {
4936                   emit_insn (gen_addsi3 (src_reg, src_reg,
4937                                          GEN_INT (move_bytes)));
4938                   emit_insn (gen_addsi3 (dest_reg, dest_reg,
4939                                          GEN_INT (move_bytes)));
4940                 }
4941               else
4942                 {
4943                   emit_insn (gen_adddi3 (src_reg, src_reg,
4944                                          GEN_INT (move_bytes)));
4945                   emit_insn (gen_adddi3 (dest_reg, dest_reg,
4946                                          GEN_INT (move_bytes)));
4947                 }
4948             }
4949         }
4950     }
4951
4952   else                  /* string instructions not available */
4953     {
4954       num_reg = offset = 0;
4955       for ( ; bytes > 0; (bytes -= move_bytes), (offset += move_bytes))
4956         {
4957           /* Calculate the correct offset for src/dest */
4958           if (offset == 0)
4959             {
4960               src_addr  = src_reg;
4961               dest_addr = dest_reg;
4962             }
4963           else
4964             {
4965               src_addr = plus_constant (src_reg, offset);
4966               dest_addr = plus_constant (dest_reg, offset);
4967             }
4968
4969           /* Generate the appropriate load and store, saving the stores
4970              for later.  */
4971           if (bytes >= 8 && TARGET_POWERPC64
4972               /* 64-bit loads and stores require word-aligned
4973                  displacements.  */
4974               && (align >= 8 || (! STRICT_ALIGNMENT && align >= 4)))
4975             {
4976               move_bytes = 8;
4977               tmp_reg = gen_reg_rtx (DImode);
4978               emit_insn (gen_movdi (tmp_reg,
4979                                     expand_block_move_mem (DImode,
4980                                                            src_addr,
4981                                                            orig_src)));
4982               stores[num_reg++] = gen_movdi (expand_block_move_mem (DImode,
4983                                                                     dest_addr,
4984                                                                     orig_dest),
4985                                              tmp_reg);
4986             }
4987           else if (bytes >= 4 && (align >= 4 || ! STRICT_ALIGNMENT))
4988             {
4989               move_bytes = 4;
4990               tmp_reg = gen_reg_rtx (SImode);
4991               emit_insn (gen_movsi (tmp_reg,
4992                                     expand_block_move_mem (SImode,
4993                                                            src_addr,
4994                                                            orig_src)));
4995               stores[num_reg++] = gen_movsi (expand_block_move_mem (SImode,
4996                                                                     dest_addr,
4997                                                                     orig_dest),
4998                                              tmp_reg);
4999             }
5000           else if (bytes >= 2 && (align >= 2 || ! STRICT_ALIGNMENT))
5001             {
5002               move_bytes = 2;
5003               tmp_reg = gen_reg_rtx (HImode);
5004               emit_insn (gen_movhi (tmp_reg,
5005                                     expand_block_move_mem (HImode,
5006                                                            src_addr,
5007                                                            orig_src)));
5008               stores[num_reg++] = gen_movhi (expand_block_move_mem (HImode,
5009                                                                     dest_addr,
5010                                                                     orig_dest),
5011                                              tmp_reg);
5012             }
5013           else
5014             {
5015               move_bytes = 1;
5016               tmp_reg = gen_reg_rtx (QImode);
5017               emit_insn (gen_movqi (tmp_reg,
5018                                     expand_block_move_mem (QImode,
5019                                                            src_addr,
5020                                                            orig_src)));
5021               stores[num_reg++] = gen_movqi (expand_block_move_mem (QImode,
5022                                                                     dest_addr,
5023                                                                     orig_dest),
5024                                              tmp_reg);
5025             }
5026
5027           if (num_reg >= MAX_MOVE_REG)
5028             {
5029               for (i = 0; i < num_reg; i++)
5030                 emit_insn (stores[i]);
5031               num_reg = 0;
5032             }
5033         }
5034
5035       for (i = 0; i < num_reg; i++)
5036         emit_insn (stores[i]);
5037     }
5038
5039   return 1;
5040 }
5041
5042 \f
5043 /* Return 1 if OP is a load multiple operation.  It is known to be a
5044    PARALLEL and the first section will be tested.  */
5045
5046 int
5047 load_multiple_operation (op, mode)
5048      rtx op;
5049      enum machine_mode mode ATTRIBUTE_UNUSED;
5050 {
5051   int count = XVECLEN (op, 0);
5052   unsigned int dest_regno;
5053   rtx src_addr;
5054   int i;
5055
5056   /* Perform a quick check so we don't blow up below.  */
5057   if (count <= 1
5058       || GET_CODE (XVECEXP (op, 0, 0)) != SET
5059       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
5060       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
5061     return 0;
5062
5063   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
5064   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
5065
5066   for (i = 1; i < count; i++)
5067     {
5068       rtx elt = XVECEXP (op, 0, i);
5069
5070       if (GET_CODE (elt) != SET
5071           || GET_CODE (SET_DEST (elt)) != REG
5072           || GET_MODE (SET_DEST (elt)) != SImode
5073           || REGNO (SET_DEST (elt)) != dest_regno + i
5074           || GET_CODE (SET_SRC (elt)) != MEM
5075           || GET_MODE (SET_SRC (elt)) != SImode
5076           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
5077           || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
5078           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
5079           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != i * 4)
5080         return 0;
5081     }
5082
5083   return 1;
5084 }
5085
5086 /* Similar, but tests for store multiple.  Here, the second vector element
5087    is a CLOBBER.  It will be tested later.  */
5088
5089 int
5090 store_multiple_operation (op, mode)
5091      rtx op;
5092      enum machine_mode mode ATTRIBUTE_UNUSED;
5093 {
5094   int count = XVECLEN (op, 0) - 1;
5095   unsigned int src_regno;
5096   rtx dest_addr;
5097   int i;
5098
5099   /* Perform a quick check so we don't blow up below.  */
5100   if (count <= 1
5101       || GET_CODE (XVECEXP (op, 0, 0)) != SET
5102       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
5103       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
5104     return 0;
5105
5106   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
5107   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
5108
5109   for (i = 1; i < count; i++)
5110     {
5111       rtx elt = XVECEXP (op, 0, i + 1);
5112
5113       if (GET_CODE (elt) != SET
5114           || GET_CODE (SET_SRC (elt)) != REG
5115           || GET_MODE (SET_SRC (elt)) != SImode
5116           || REGNO (SET_SRC (elt)) != src_regno + i
5117           || GET_CODE (SET_DEST (elt)) != MEM
5118           || GET_MODE (SET_DEST (elt)) != SImode
5119           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
5120           || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
5121           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
5122           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != i * 4)
5123         return 0;
5124     }
5125
5126   return 1;
5127 }
5128
5129 /* Return 1 for a parallel vrsave operation.  */
5130
5131 int
5132 vrsave_operation (op, mode)
5133      rtx op;
5134      enum machine_mode mode ATTRIBUTE_UNUSED;
5135 {
5136   int count = XVECLEN (op, 0);
5137   unsigned int dest_regno, src_regno;
5138   int i;
5139
5140   if (count <= 1
5141       || GET_CODE (XVECEXP (op, 0, 0)) != SET
5142       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
5143       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC_VOLATILE)
5144     return 0;
5145
5146   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
5147   src_regno  = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
5148
5149   if (dest_regno != VRSAVE_REGNO
5150       && src_regno != VRSAVE_REGNO)
5151     return 0;
5152
5153   for (i = 1; i < count; i++)
5154     {
5155       rtx elt = XVECEXP (op, 0, i);
5156
5157       if (GET_CODE (elt) != CLOBBER
5158           && GET_CODE (elt) != SET)
5159         return 0;
5160     }
5161
5162   return 1;
5163 }
5164
5165 /* Return 1 for an PARALLEL suitable for mtcrf.  */
5166
5167 int
5168 mtcrf_operation (op, mode)
5169      rtx op;
5170      enum machine_mode mode ATTRIBUTE_UNUSED;
5171 {
5172   int count = XVECLEN (op, 0);
5173   int i;
5174   rtx src_reg;
5175
5176   /* Perform a quick check so we don't blow up below.  */
5177   if (count < 1
5178       || GET_CODE (XVECEXP (op, 0, 0)) != SET
5179       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC
5180       || XVECLEN (SET_SRC (XVECEXP (op, 0, 0)), 0) != 2)
5181     return 0;
5182   src_reg = XVECEXP (SET_SRC (XVECEXP (op, 0, 0)), 0, 0);
5183   
5184   if (GET_CODE (src_reg) != REG
5185       || GET_MODE (src_reg) != SImode
5186       || ! INT_REGNO_P (REGNO (src_reg)))
5187     return 0;
5188
5189   for (i = 0; i < count; i++)
5190     {
5191       rtx exp = XVECEXP (op, 0, i);
5192       rtx unspec;
5193       int maskval;
5194       
5195       if (GET_CODE (exp) != SET
5196           || GET_CODE (SET_DEST (exp)) != REG
5197           || GET_MODE (SET_DEST (exp)) != CCmode
5198           || ! CR_REGNO_P (REGNO (SET_DEST (exp))))
5199         return 0;
5200       unspec = SET_SRC (exp);
5201       maskval = 1 << (MAX_CR_REGNO - REGNO (SET_DEST (exp)));
5202       
5203       if (GET_CODE (unspec) != UNSPEC
5204           || XINT (unspec, 1) != 20
5205           || XVECLEN (unspec, 0) != 2
5206           || XVECEXP (unspec, 0, 0) != src_reg
5207           || GET_CODE (XVECEXP (unspec, 0, 1)) != CONST_INT
5208           || INTVAL (XVECEXP (unspec, 0, 1)) != maskval)
5209         return 0;
5210     }
5211   return 1;
5212 }
5213
5214 /* Return 1 for an PARALLEL suitable for lmw.  */
5215
5216 int
5217 lmw_operation (op, mode)
5218      rtx op;
5219      enum machine_mode mode ATTRIBUTE_UNUSED;
5220 {
5221   int count = XVECLEN (op, 0);
5222   unsigned int dest_regno;
5223   rtx src_addr;
5224   unsigned int base_regno;
5225   HOST_WIDE_INT offset;
5226   int i;
5227
5228   /* Perform a quick check so we don't blow up below.  */
5229   if (count <= 1
5230       || GET_CODE (XVECEXP (op, 0, 0)) != SET
5231       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
5232       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
5233     return 0;
5234
5235   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
5236   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
5237
5238   if (dest_regno > 31
5239       || count != 32 - (int) dest_regno)
5240     return 0;
5241
5242   if (LEGITIMATE_INDIRECT_ADDRESS_P (src_addr, 0))
5243     {
5244       offset = 0;
5245       base_regno = REGNO (src_addr);
5246       if (base_regno == 0)
5247         return 0;
5248     }
5249   else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, src_addr, 0))
5250     {
5251       offset = INTVAL (XEXP (src_addr, 1));
5252       base_regno = REGNO (XEXP (src_addr, 0));
5253     }
5254   else
5255     return 0;
5256
5257   for (i = 0; i < count; i++)
5258     {
5259       rtx elt = XVECEXP (op, 0, i);
5260       rtx newaddr;
5261       rtx addr_reg;
5262       HOST_WIDE_INT newoffset;
5263
5264       if (GET_CODE (elt) != SET
5265           || GET_CODE (SET_DEST (elt)) != REG
5266           || GET_MODE (SET_DEST (elt)) != SImode
5267           || REGNO (SET_DEST (elt)) != dest_regno + i
5268           || GET_CODE (SET_SRC (elt)) != MEM
5269           || GET_MODE (SET_SRC (elt)) != SImode)
5270         return 0;
5271       newaddr = XEXP (SET_SRC (elt), 0);
5272       if (LEGITIMATE_INDIRECT_ADDRESS_P (newaddr, 0))
5273         {
5274           newoffset = 0;
5275           addr_reg = newaddr;
5276         }
5277       else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, newaddr, 0))
5278         {
5279           addr_reg = XEXP (newaddr, 0);
5280           newoffset = INTVAL (XEXP (newaddr, 1));
5281         }
5282       else
5283         return 0;
5284       if (REGNO (addr_reg) != base_regno
5285           || newoffset != offset + 4 * i)
5286         return 0;
5287     }
5288
5289   return 1;
5290 }
5291
5292 /* Return 1 for an PARALLEL suitable for stmw.  */
5293
5294 int
5295 stmw_operation (op, mode)
5296      rtx op;
5297      enum machine_mode mode ATTRIBUTE_UNUSED;
5298 {
5299   int count = XVECLEN (op, 0);
5300   unsigned int src_regno;
5301   rtx dest_addr;
5302   unsigned int base_regno;
5303   HOST_WIDE_INT offset;
5304   int i;
5305
5306   /* Perform a quick check so we don't blow up below.  */
5307   if (count <= 1
5308       || GET_CODE (XVECEXP (op, 0, 0)) != SET
5309       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
5310       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
5311     return 0;
5312
5313   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
5314   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
5315
5316   if (src_regno > 31
5317       || count != 32 - (int) src_regno)
5318     return 0;
5319
5320   if (LEGITIMATE_INDIRECT_ADDRESS_P (dest_addr, 0))
5321     {
5322       offset = 0;
5323       base_regno = REGNO (dest_addr);
5324       if (base_regno == 0)
5325         return 0;
5326     }
5327   else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, dest_addr, 0))
5328     {
5329       offset = INTVAL (XEXP (dest_addr, 1));
5330       base_regno = REGNO (XEXP (dest_addr, 0));
5331     }
5332   else
5333     return 0;
5334
5335   for (i = 0; i < count; i++)
5336     {
5337       rtx elt = XVECEXP (op, 0, i);
5338       rtx newaddr;
5339       rtx addr_reg;
5340       HOST_WIDE_INT newoffset;
5341
5342       if (GET_CODE (elt) != SET
5343           || GET_CODE (SET_SRC (elt)) != REG
5344           || GET_MODE (SET_SRC (elt)) != SImode
5345           || REGNO (SET_SRC (elt)) != src_regno + i
5346           || GET_CODE (SET_DEST (elt)) != MEM
5347           || GET_MODE (SET_DEST (elt)) != SImode)
5348         return 0;
5349       newaddr = XEXP (SET_DEST (elt), 0);
5350       if (LEGITIMATE_INDIRECT_ADDRESS_P (newaddr, 0))
5351         {
5352           newoffset = 0;
5353           addr_reg = newaddr;
5354         }
5355       else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, newaddr, 0))
5356         {
5357           addr_reg = XEXP (newaddr, 0);
5358           newoffset = INTVAL (XEXP (newaddr, 1));
5359         }
5360       else
5361         return 0;
5362       if (REGNO (addr_reg) != base_regno
5363           || newoffset != offset + 4 * i)
5364         return 0;
5365     }
5366
5367   return 1;
5368 }
5369 \f
5370 /* A validation routine: say whether CODE, a condition code, and MODE
5371    match.  The other alternatives either don't make sense or should
5372    never be generated.  */
5373
5374 static void
5375 validate_condition_mode (code, mode)
5376      enum rtx_code code;
5377      enum machine_mode mode;
5378 {
5379   if (GET_RTX_CLASS (code) != '<' 
5380       || GET_MODE_CLASS (mode) != MODE_CC)
5381     abort ();
5382
5383   /* These don't make sense.  */
5384   if ((code == GT || code == LT || code == GE || code == LE)
5385       && mode == CCUNSmode)
5386     abort ();
5387
5388   if ((code == GTU || code == LTU || code == GEU || code == LEU)
5389       && mode != CCUNSmode)
5390     abort ();
5391
5392   if (mode != CCFPmode
5393       && (code == ORDERED || code == UNORDERED
5394           || code == UNEQ || code == LTGT
5395           || code == UNGT || code == UNLT
5396           || code == UNGE || code == UNLE))
5397     abort ();
5398   
5399   /* These should never be generated except for 
5400      flag_unsafe_math_optimizations.  */
5401   if (mode == CCFPmode
5402       && ! flag_unsafe_math_optimizations
5403       && (code == LE || code == GE
5404           || code == UNEQ || code == LTGT
5405           || code == UNGT || code == UNLT))
5406     abort ();
5407
5408   /* These are invalid; the information is not there.  */
5409   if (mode == CCEQmode 
5410       && code != EQ && code != NE)
5411     abort ();
5412 }
5413
5414 /* Return 1 if OP is a comparison operation that is valid for a branch insn.
5415    We only check the opcode against the mode of the CC value here.  */
5416
5417 int
5418 branch_comparison_operator (op, mode)
5419      rtx op;
5420      enum machine_mode mode ATTRIBUTE_UNUSED;
5421 {
5422   enum rtx_code code = GET_CODE (op);
5423   enum machine_mode cc_mode;
5424
5425   if (GET_RTX_CLASS (code) != '<')
5426     return 0;
5427
5428   cc_mode = GET_MODE (XEXP (op, 0));
5429   if (GET_MODE_CLASS (cc_mode) != MODE_CC)
5430     return 0;
5431
5432   validate_condition_mode (code, cc_mode);
5433
5434   return 1;
5435 }
5436
5437 /* Return 1 if OP is a comparison operation that is valid for a branch
5438    insn and which is true if the corresponding bit in the CC register
5439    is set.  */
5440
5441 int
5442 branch_positive_comparison_operator (op, mode)
5443      rtx op;
5444      enum machine_mode mode;
5445 {
5446   enum rtx_code code;
5447
5448   if (! branch_comparison_operator (op, mode))
5449     return 0;
5450
5451   code = GET_CODE (op);
5452   return (code == EQ || code == LT || code == GT
5453           || code == LTU || code == GTU
5454           || code == UNORDERED);
5455 }
5456
5457 /* Return 1 if OP is a comparison operation that is valid for an scc insn.
5458    We check the opcode against the mode of the CC value and disallow EQ or
5459    NE comparisons for integers.  */
5460
5461 int
5462 scc_comparison_operator (op, mode)
5463      rtx op;
5464      enum machine_mode mode;
5465 {
5466   enum rtx_code code = GET_CODE (op);
5467   enum machine_mode cc_mode;
5468
5469   if (GET_MODE (op) != mode && mode != VOIDmode)
5470     return 0;
5471
5472   if (GET_RTX_CLASS (code) != '<')
5473     return 0;
5474
5475   cc_mode = GET_MODE (XEXP (op, 0));
5476   if (GET_MODE_CLASS (cc_mode) != MODE_CC)
5477     return 0;
5478
5479   validate_condition_mode (code, cc_mode);
5480
5481   if (code == NE && cc_mode != CCFPmode)
5482     return 0;
5483
5484   return 1;
5485 }
5486
5487 int
5488 trap_comparison_operator (op, mode)
5489     rtx op;
5490     enum machine_mode mode;
5491 {
5492   if (mode != VOIDmode && mode != GET_MODE (op))
5493     return 0;
5494   return GET_RTX_CLASS (GET_CODE (op)) == '<';
5495 }
5496
5497 int
5498 boolean_operator (op, mode)
5499     rtx op;
5500     enum machine_mode mode ATTRIBUTE_UNUSED;
5501 {
5502   enum rtx_code code = GET_CODE (op);
5503   return (code == AND || code == IOR || code == XOR);
5504 }
5505
5506 int
5507 boolean_or_operator (op, mode)
5508     rtx op;
5509     enum machine_mode mode ATTRIBUTE_UNUSED;
5510 {
5511   enum rtx_code code = GET_CODE (op);
5512   return (code == IOR || code == XOR);
5513 }
5514
5515 int
5516 min_max_operator (op, mode)
5517     rtx op;
5518     enum machine_mode mode ATTRIBUTE_UNUSED;
5519 {
5520   enum rtx_code code = GET_CODE (op);
5521   return (code == SMIN || code == SMAX || code == UMIN || code == UMAX);
5522 }
5523 \f
5524 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
5525    mask required to convert the result of a rotate insn into a shift
5526    left insn of SHIFTOP bits.  Both are known to be CONST_INT.  */
5527
5528 int
5529 includes_lshift_p (shiftop, andop)
5530      rtx shiftop;
5531      rtx andop;
5532 {
5533   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
5534
5535   shift_mask <<= INTVAL (shiftop);
5536
5537   return (INTVAL (andop) & ~shift_mask) == 0;
5538 }
5539
5540 /* Similar, but for right shift.  */
5541
5542 int
5543 includes_rshift_p (shiftop, andop)
5544      rtx shiftop;
5545      rtx andop;
5546 {
5547   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
5548
5549   shift_mask >>= INTVAL (shiftop);
5550
5551   return (INTVAL (andop) & ~shift_mask) == 0;
5552 }
5553
5554 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
5555    to perform a left shift.  It must have exactly SHIFTOP least
5556    signifigant 0's, then one or more 1's, then zero or more 0's.  */
5557
5558 int
5559 includes_rldic_lshift_p (shiftop, andop)
5560      rtx shiftop;
5561      rtx andop;
5562 {
5563   if (GET_CODE (andop) == CONST_INT)
5564     {
5565       HOST_WIDE_INT c, lsb, shift_mask;
5566
5567       c = INTVAL (andop);
5568       if (c == 0 || c == ~0)
5569         return 0;
5570
5571       shift_mask = ~0;
5572       shift_mask <<= INTVAL (shiftop);
5573
5574       /* Find the least signifigant one bit.  */
5575       lsb = c & -c;
5576
5577       /* It must coincide with the LSB of the shift mask.  */
5578       if (-lsb != shift_mask)
5579         return 0;
5580
5581       /* Invert to look for the next transition (if any).  */
5582       c = ~c;
5583
5584       /* Remove the low group of ones (originally low group of zeros).  */
5585       c &= -lsb;
5586
5587       /* Again find the lsb, and check we have all 1's above.  */
5588       lsb = c & -c;
5589       return c == -lsb;
5590     }
5591   else if (GET_CODE (andop) == CONST_DOUBLE
5592            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
5593     {
5594       HOST_WIDE_INT low, high, lsb;
5595       HOST_WIDE_INT shift_mask_low, shift_mask_high;
5596
5597       low = CONST_DOUBLE_LOW (andop);
5598       if (HOST_BITS_PER_WIDE_INT < 64)
5599         high = CONST_DOUBLE_HIGH (andop);
5600
5601       if ((low == 0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == 0))
5602           || (low == ~0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0)))
5603         return 0;
5604
5605       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
5606         {
5607           shift_mask_high = ~0;
5608           if (INTVAL (shiftop) > 32)
5609             shift_mask_high <<= INTVAL (shiftop) - 32;
5610
5611           lsb = high & -high;
5612
5613           if (-lsb != shift_mask_high || INTVAL (shiftop) < 32)
5614             return 0;
5615
5616           high = ~high;
5617           high &= -lsb;
5618
5619           lsb = high & -high;
5620           return high == -lsb;
5621         }
5622
5623       shift_mask_low = ~0;
5624       shift_mask_low <<= INTVAL (shiftop);
5625
5626       lsb = low & -low;
5627
5628       if (-lsb != shift_mask_low)
5629         return 0;
5630
5631       if (HOST_BITS_PER_WIDE_INT < 64)
5632         high = ~high;
5633       low = ~low;
5634       low &= -lsb;
5635
5636       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
5637         {
5638           lsb = high & -high;
5639           return high == -lsb;
5640         }
5641
5642       lsb = low & -low;
5643       return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
5644     }
5645   else
5646     return 0;
5647 }
5648
5649 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
5650    to perform a left shift.  It must have SHIFTOP or more least
5651    signifigant 0's, with the remainder of the word 1's.  */
5652
5653 int
5654 includes_rldicr_lshift_p (shiftop, andop)
5655      rtx shiftop;
5656      rtx andop;
5657 {
5658   if (GET_CODE (andop) == CONST_INT)
5659     {
5660       HOST_WIDE_INT c, lsb, shift_mask;
5661
5662       shift_mask = ~0;
5663       shift_mask <<= INTVAL (shiftop);
5664       c = INTVAL (andop);
5665
5666       /* Find the least signifigant one bit.  */
5667       lsb = c & -c;
5668
5669       /* It must be covered by the shift mask.
5670          This test also rejects c == 0.  */
5671       if ((lsb & shift_mask) == 0)
5672         return 0;
5673
5674       /* Check we have all 1's above the transition, and reject all 1's.  */
5675       return c == -lsb && lsb != 1;
5676     }
5677   else if (GET_CODE (andop) == CONST_DOUBLE
5678            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
5679     {
5680       HOST_WIDE_INT low, lsb, shift_mask_low;
5681
5682       low = CONST_DOUBLE_LOW (andop);
5683
5684       if (HOST_BITS_PER_WIDE_INT < 64)
5685         {
5686           HOST_WIDE_INT high, shift_mask_high;
5687
5688           high = CONST_DOUBLE_HIGH (andop);
5689
5690           if (low == 0)
5691             {
5692               shift_mask_high = ~0;
5693               if (INTVAL (shiftop) > 32)
5694                 shift_mask_high <<= INTVAL (shiftop) - 32;
5695
5696               lsb = high & -high;
5697
5698               if ((lsb & shift_mask_high) == 0)
5699                 return 0;
5700
5701               return high == -lsb;
5702             }
5703           if (high != ~0)
5704             return 0;
5705         }
5706
5707       shift_mask_low = ~0;
5708       shift_mask_low <<= INTVAL (shiftop);
5709
5710       lsb = low & -low;
5711
5712       if ((lsb & shift_mask_low) == 0)
5713         return 0;
5714
5715       return low == -lsb && lsb != 1;
5716     }
5717   else
5718     return 0;
5719 }
5720
5721 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
5722    for lfq and stfq insns.
5723
5724    Note reg1 and reg2 *must* be hard registers.  To be sure we will
5725    abort if we are passed pseudo registers.  */
5726
5727 int
5728 registers_ok_for_quad_peep (reg1, reg2)
5729      rtx reg1, reg2;
5730 {
5731   /* We might have been passed a SUBREG.  */
5732   if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG) 
5733     return 0;
5734
5735   return (REGNO (reg1) == REGNO (reg2) - 1);
5736 }
5737
5738 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
5739    addr1 and addr2 must be in consecutive memory locations
5740    (addr2 == addr1 + 8).  */
5741
5742 int
5743 addrs_ok_for_quad_peep (addr1, addr2)
5744      rtx addr1;
5745      rtx addr2;
5746 {
5747   unsigned int reg1;
5748   int offset1;
5749
5750   /* Extract an offset (if used) from the first addr.  */
5751   if (GET_CODE (addr1) == PLUS)
5752     {
5753       /* If not a REG, return zero.  */
5754       if (GET_CODE (XEXP (addr1, 0)) != REG)
5755         return 0;
5756       else
5757         {
5758           reg1 = REGNO (XEXP (addr1, 0));
5759           /* The offset must be constant!  */
5760           if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
5761             return 0;
5762           offset1 = INTVAL (XEXP (addr1, 1));
5763         }
5764     }
5765   else if (GET_CODE (addr1) != REG)
5766     return 0;
5767   else
5768     {
5769       reg1 = REGNO (addr1);
5770       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
5771       offset1 = 0;
5772     }
5773
5774   /* Make sure the second address is a (mem (plus (reg) (const_int))).  */
5775   if (GET_CODE (addr2) != PLUS)
5776     return 0;
5777
5778   if (GET_CODE (XEXP (addr2, 0)) != REG
5779       || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
5780     return 0;
5781
5782   if (reg1 != REGNO (XEXP (addr2, 0)))
5783     return 0;
5784
5785   /* The offset for the second addr must be 8 more than the first addr.  */
5786   if (INTVAL (XEXP (addr2, 1)) != offset1 + 8)
5787     return 0;
5788
5789   /* All the tests passed.  addr1 and addr2 are valid for lfq or stfq
5790      instructions.  */
5791   return 1;
5792 }
5793 \f
5794 /* Return the register class of a scratch register needed to copy IN into
5795    or out of a register in CLASS in MODE.  If it can be done directly,
5796    NO_REGS is returned.  */
5797
5798 enum reg_class
5799 secondary_reload_class (class, mode, in)
5800      enum reg_class class;
5801      enum machine_mode mode ATTRIBUTE_UNUSED;
5802      rtx in;
5803 {
5804   int regno;
5805
5806   if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN && flag_pic))
5807     {
5808       /* We cannot copy a symbolic operand directly into anything
5809          other than BASE_REGS for TARGET_ELF.  So indicate that a
5810          register from BASE_REGS is needed as an intermediate
5811          register.
5812          
5813          On Darwin, pic addresses require a load from memory, which
5814          needs a base register.  */
5815       if (class != BASE_REGS
5816           && (GET_CODE (in) == SYMBOL_REF
5817               || GET_CODE (in) == HIGH
5818               || GET_CODE (in) == LABEL_REF
5819               || GET_CODE (in) == CONST))
5820         return BASE_REGS;
5821     }
5822
5823   if (GET_CODE (in) == REG)
5824     {
5825       regno = REGNO (in);
5826       if (regno >= FIRST_PSEUDO_REGISTER)
5827         {
5828           regno = true_regnum (in);
5829           if (regno >= FIRST_PSEUDO_REGISTER)
5830             regno = -1;
5831         }
5832     }
5833   else if (GET_CODE (in) == SUBREG)
5834     {
5835       regno = true_regnum (in);
5836       if (regno >= FIRST_PSEUDO_REGISTER)
5837         regno = -1;
5838     }
5839   else
5840     regno = -1;
5841
5842   /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
5843      into anything.  */
5844   if (class == GENERAL_REGS || class == BASE_REGS
5845       || (regno >= 0 && INT_REGNO_P (regno)))
5846     return NO_REGS;
5847
5848   /* Constants, memory, and FP registers can go into FP registers.  */
5849   if ((regno == -1 || FP_REGNO_P (regno))
5850       && (class == FLOAT_REGS || class == NON_SPECIAL_REGS))
5851     return NO_REGS;
5852
5853   /* Memory, and AltiVec registers can go into AltiVec registers.  */
5854   if ((regno == -1 || ALTIVEC_REGNO_P (regno))
5855       && class == ALTIVEC_REGS)
5856     return NO_REGS;
5857
5858   /* We can copy among the CR registers.  */
5859   if ((class == CR_REGS || class == CR0_REGS)
5860       && regno >= 0 && CR_REGNO_P (regno))
5861     return NO_REGS;
5862
5863   /* Otherwise, we need GENERAL_REGS.  */
5864   return GENERAL_REGS;
5865 }
5866 \f
5867 /* Given a comparison operation, return the bit number in CCR to test.  We
5868    know this is a valid comparison.  
5869
5870    SCC_P is 1 if this is for an scc.  That means that %D will have been
5871    used instead of %C, so the bits will be in different places.
5872
5873    Return -1 if OP isn't a valid comparison for some reason.  */
5874
5875 int
5876 ccr_bit (op, scc_p)
5877      rtx op;
5878      int scc_p;
5879 {
5880   enum rtx_code code = GET_CODE (op);
5881   enum machine_mode cc_mode;
5882   int cc_regnum;
5883   int base_bit;
5884   rtx reg;
5885
5886   if (GET_RTX_CLASS (code) != '<')
5887     return -1;
5888
5889   reg = XEXP (op, 0);
5890
5891   if (GET_CODE (reg) != REG
5892       || ! CR_REGNO_P (REGNO (reg)))
5893     abort ();
5894
5895   cc_mode = GET_MODE (reg);
5896   cc_regnum = REGNO (reg);
5897   base_bit = 4 * (cc_regnum - CR0_REGNO);
5898
5899   validate_condition_mode (code, cc_mode);
5900
5901   switch (code)
5902     {
5903     case NE:
5904       return scc_p ? base_bit + 3 : base_bit + 2;
5905     case EQ:
5906       return base_bit + 2;
5907     case GT:  case GTU:  case UNLE:
5908       return base_bit + 1;
5909     case LT:  case LTU:  case UNGE:
5910       return base_bit;
5911     case ORDERED:  case UNORDERED:
5912       return base_bit + 3;
5913
5914     case GE:  case GEU:
5915       /* If scc, we will have done a cror to put the bit in the
5916          unordered position.  So test that bit.  For integer, this is ! LT
5917          unless this is an scc insn.  */
5918       return scc_p ? base_bit + 3 : base_bit;
5919
5920     case LE:  case LEU:
5921       return scc_p ? base_bit + 3 : base_bit + 1;
5922
5923     default:
5924       abort ();
5925     }
5926 }
5927 \f
5928 /* Return the GOT register.  */
5929
5930 struct rtx_def *
5931 rs6000_got_register (value)
5932      rtx value ATTRIBUTE_UNUSED;
5933 {
5934   /* The second flow pass currently (June 1999) can't update
5935      regs_ever_live without disturbing other parts of the compiler, so
5936      update it here to make the prolog/epilogue code happy.  */
5937   if (no_new_pseudos && ! regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
5938     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
5939
5940   current_function_uses_pic_offset_table = 1;
5941
5942   return pic_offset_table_rtx;
5943 }
5944 \f
5945 /* Functions to init, mark and free struct machine_function.
5946    These will be called, via pointer variables,
5947    from push_function_context and pop_function_context.  */
5948
5949 static void
5950 rs6000_init_machine_status (p)
5951      struct function *p;
5952 {
5953   p->machine = (machine_function *) xcalloc (1, sizeof (machine_function));
5954 }
5955
5956 static void
5957 rs6000_free_machine_status (p)
5958      struct function *p;
5959 {
5960   if (p->machine == NULL)
5961     return;
5962
5963   free (p->machine);
5964   p->machine = NULL;
5965 }
5966
5967 \f
5968 /* Print an operand.  Recognize special options, documented below.  */
5969
5970 #if TARGET_ELF
5971 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
5972 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
5973 #else
5974 #define SMALL_DATA_RELOC "sda21"
5975 #define SMALL_DATA_REG 0
5976 #endif
5977
5978 void
5979 print_operand (file, x, code)
5980     FILE *file;
5981     rtx x;
5982     int code;
5983 {
5984   int i;
5985   HOST_WIDE_INT val;
5986
5987   /* These macros test for integers and extract the low-order bits.  */
5988 #define INT_P(X)  \
5989 ((GET_CODE (X) == CONST_INT || GET_CODE (X) == CONST_DOUBLE)    \
5990  && GET_MODE (X) == VOIDmode)
5991
5992 #define INT_LOWPART(X) \
5993   (GET_CODE (X) == CONST_INT ? INTVAL (X) : CONST_DOUBLE_LOW (X))
5994
5995   switch (code)
5996     {
5997     case '.':
5998       /* Write out an instruction after the call which may be replaced
5999          with glue code by the loader.  This depends on the AIX version.  */
6000       asm_fprintf (file, RS6000_CALL_GLUE);
6001       return;
6002
6003       /* %a is output_address.  */
6004
6005     case 'A':
6006       /* If X is a constant integer whose low-order 5 bits are zero,
6007          write 'l'.  Otherwise, write 'r'.  This is a kludge to fix a bug
6008          in the AIX assembler where "sri" with a zero shift count
6009          writes a trash instruction.  */
6010       if (GET_CODE (x) == CONST_INT && (INTVAL (x) & 31) == 0)
6011         putc ('l', file);
6012       else
6013         putc ('r', file);
6014       return;
6015
6016     case 'b':
6017       /* If constant, low-order 16 bits of constant, unsigned.
6018          Otherwise, write normally.  */
6019       if (INT_P (x))
6020         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 0xffff);
6021       else
6022         print_operand (file, x, 0);
6023       return;
6024
6025     case 'B':
6026       /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
6027          for 64-bit mask direction.  */
6028       putc (((INT_LOWPART(x) & 1) == 0 ? 'r' : 'l'), file);
6029       return;
6030
6031       /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
6032          output_operand.  */
6033
6034     case 'D':
6035       /* There used to be a comment for 'C' reading "This is an
6036            optional cror needed for certain floating-point
6037            comparisons.  Otherwise write nothing."  */
6038
6039       /* Similar, except that this is for an scc, so we must be able to
6040          encode the test in a single bit that is one.  We do the above
6041          for any LE, GE, GEU, or LEU and invert the bit for NE.  */
6042       if (GET_CODE (x) == LE || GET_CODE (x) == GE
6043           || GET_CODE (x) == LEU || GET_CODE (x) == GEU)
6044         {
6045           int base_bit = 4 * (REGNO (XEXP (x, 0)) - CR0_REGNO);
6046
6047           fprintf (file, "cror %d,%d,%d\n\t", base_bit + 3,
6048                    base_bit + 2,
6049                    base_bit + (GET_CODE (x) == GE || GET_CODE (x) == GEU));
6050         }
6051
6052       else if (GET_CODE (x) == NE)
6053         {
6054           int base_bit = 4 * (REGNO (XEXP (x, 0)) - CR0_REGNO);
6055
6056           fprintf (file, "crnor %d,%d,%d\n\t", base_bit + 3,
6057                    base_bit + 2, base_bit + 2);
6058         }
6059       return;
6060
6061     case 'E':
6062       /* X is a CR register.  Print the number of the EQ bit of the CR */
6063       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
6064         output_operand_lossage ("invalid %%E value");
6065       else
6066         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
6067       return;
6068
6069     case 'f':
6070       /* X is a CR register.  Print the shift count needed to move it
6071          to the high-order four bits.  */
6072       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
6073         output_operand_lossage ("invalid %%f value");
6074       else
6075         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
6076       return;
6077
6078     case 'F':
6079       /* Similar, but print the count for the rotate in the opposite
6080          direction.  */
6081       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
6082         output_operand_lossage ("invalid %%F value");
6083       else
6084         fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
6085       return;
6086
6087     case 'G':
6088       /* X is a constant integer.  If it is negative, print "m",
6089          otherwise print "z".  This is to make a aze or ame insn.  */
6090       if (GET_CODE (x) != CONST_INT)
6091         output_operand_lossage ("invalid %%G value");
6092       else if (INTVAL (x) >= 0)
6093         putc ('z', file);
6094       else
6095         putc ('m', file);
6096       return;
6097
6098     case 'h':
6099       /* If constant, output low-order five bits.  Otherwise, write
6100          normally.  */
6101       if (INT_P (x))
6102         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 31);
6103       else
6104         print_operand (file, x, 0);
6105       return;
6106
6107     case 'H':
6108       /* If constant, output low-order six bits.  Otherwise, write
6109          normally.  */
6110       if (INT_P (x))
6111         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 63);
6112       else
6113         print_operand (file, x, 0);
6114       return;
6115
6116     case 'I':
6117       /* Print `i' if this is a constant, else nothing.  */
6118       if (INT_P (x))
6119         putc ('i', file);
6120       return;
6121
6122     case 'j':
6123       /* Write the bit number in CCR for jump.  */
6124       i = ccr_bit (x, 0);
6125       if (i == -1)
6126         output_operand_lossage ("invalid %%j code");
6127       else
6128         fprintf (file, "%d", i);
6129       return;
6130
6131     case 'J':
6132       /* Similar, but add one for shift count in rlinm for scc and pass
6133          scc flag to `ccr_bit'.  */
6134       i = ccr_bit (x, 1);
6135       if (i == -1)
6136         output_operand_lossage ("invalid %%J code");
6137       else
6138         /* If we want bit 31, write a shift count of zero, not 32.  */
6139         fprintf (file, "%d", i == 31 ? 0 : i + 1);
6140       return;
6141
6142     case 'k':
6143       /* X must be a constant.  Write the 1's complement of the
6144          constant.  */
6145       if (! INT_P (x))
6146         output_operand_lossage ("invalid %%k value");
6147       else
6148         fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INT_LOWPART (x));
6149       return;
6150
6151     case 'K':
6152       /* X must be a symbolic constant on ELF.  Write an
6153          expression suitable for an 'addi' that adds in the low 16
6154          bits of the MEM.  */
6155       if (GET_CODE (x) != CONST)
6156         {
6157           print_operand_address (file, x);
6158           fputs ("@l", file);
6159         }
6160       else
6161         {
6162           if (GET_CODE (XEXP (x, 0)) != PLUS
6163               || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
6164                   && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
6165               || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
6166             output_operand_lossage ("invalid %%K value");
6167           print_operand_address (file, XEXP (XEXP (x, 0), 0));
6168           fputs ("@l", file);
6169           print_operand (file, XEXP (XEXP (x, 0), 1), 0);
6170         }
6171       return;
6172
6173       /* %l is output_asm_label.  */
6174
6175     case 'L':
6176       /* Write second word of DImode or DFmode reference.  Works on register
6177          or non-indexed memory only.  */
6178       if (GET_CODE (x) == REG)
6179         fprintf (file, "%s", reg_names[REGNO (x) + 1]);
6180       else if (GET_CODE (x) == MEM)
6181         {
6182           /* Handle possible auto-increment.  Since it is pre-increment and
6183              we have already done it, we can just use an offset of word.  */
6184           if (GET_CODE (XEXP (x, 0)) == PRE_INC
6185               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
6186             output_address (plus_constant (XEXP (XEXP (x, 0), 0),
6187                                            UNITS_PER_WORD));
6188           else
6189             output_address (XEXP (adjust_address_nv (x, SImode,
6190                                                      UNITS_PER_WORD),
6191                                   0));
6192
6193           if (small_data_operand (x, GET_MODE (x)))
6194             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
6195                      reg_names[SMALL_DATA_REG]);
6196         }
6197       return;
6198                             
6199     case 'm':
6200       /* MB value for a mask operand.  */
6201       if (! mask_operand (x, VOIDmode))
6202         output_operand_lossage ("invalid %%m value");
6203
6204       val = INT_LOWPART (x);
6205
6206       /* If the high bit is set and the low bit is not, the value is zero.
6207          If the high bit is zero, the value is the first 1 bit we find from
6208          the left.  */
6209       if ((val & 0x80000000) && ((val & 1) == 0))
6210         {
6211           putc ('0', file);
6212           return;
6213         }
6214       else if ((val & 0x80000000) == 0)
6215         {
6216           for (i = 1; i < 32; i++)
6217             if ((val <<= 1) & 0x80000000)
6218               break;
6219           fprintf (file, "%d", i);
6220           return;
6221         }
6222           
6223       /* Otherwise, look for the first 0 bit from the right.  The result is its
6224          number plus 1. We know the low-order bit is one.  */
6225       for (i = 0; i < 32; i++)
6226         if (((val >>= 1) & 1) == 0)
6227           break;
6228
6229       /* If we ended in ...01, i would be 0.  The correct value is 31, so
6230          we want 31 - i.  */
6231       fprintf (file, "%d", 31 - i);
6232       return;
6233
6234     case 'M':
6235       /* ME value for a mask operand.  */
6236       if (! mask_operand (x, VOIDmode))
6237         output_operand_lossage ("invalid %%M value");
6238
6239       val = INT_LOWPART (x);
6240
6241       /* If the low bit is set and the high bit is not, the value is 31.
6242          If the low bit is zero, the value is the first 1 bit we find from
6243          the right.  */
6244       if ((val & 1) && ((val & 0x80000000) == 0))
6245         {
6246           fputs ("31", file);
6247           return;
6248         }
6249       else if ((val & 1) == 0)
6250         {
6251           for (i = 0; i < 32; i++)
6252             if ((val >>= 1) & 1)
6253               break;
6254
6255           /* If we had ....10, i would be 0.  The result should be
6256              30, so we need 30 - i.  */
6257           fprintf (file, "%d", 30 - i);
6258           return;
6259         }
6260           
6261       /* Otherwise, look for the first 0 bit from the left.  The result is its
6262          number minus 1. We know the high-order bit is one.  */
6263       for (i = 0; i < 32; i++)
6264         if (((val <<= 1) & 0x80000000) == 0)
6265           break;
6266
6267       fprintf (file, "%d", i);
6268       return;
6269
6270       /* %n outputs the negative of its operand.  */
6271
6272     case 'N':
6273       /* Write the number of elements in the vector times 4.  */
6274       if (GET_CODE (x) != PARALLEL)
6275         output_operand_lossage ("invalid %%N value");
6276       else
6277         fprintf (file, "%d", XVECLEN (x, 0) * 4);
6278       return;
6279
6280     case 'O':
6281       /* Similar, but subtract 1 first.  */
6282       if (GET_CODE (x) != PARALLEL)
6283         output_operand_lossage ("invalid %%O value");
6284       else
6285         fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
6286       return;
6287
6288     case 'p':
6289       /* X is a CONST_INT that is a power of two.  Output the logarithm.  */
6290       if (! INT_P (x)
6291           || INT_LOWPART (x) < 0
6292           || (i = exact_log2 (INT_LOWPART (x))) < 0)
6293         output_operand_lossage ("invalid %%p value");
6294       else
6295         fprintf (file, "%d", i);
6296       return;
6297
6298     case 'P':
6299       /* The operand must be an indirect memory reference.  The result
6300          is the register number.  */
6301       if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
6302           || REGNO (XEXP (x, 0)) >= 32)
6303         output_operand_lossage ("invalid %%P value");
6304       else
6305         fprintf (file, "%d", REGNO (XEXP (x, 0)));
6306       return;
6307
6308     case 'q':
6309       /* This outputs the logical code corresponding to a boolean
6310          expression.  The expression may have one or both operands
6311          negated (if one, only the first one).  For condition register
6312          logical operations, it will also treat the negated
6313          CR codes as NOTs, but not handle NOTs of them.  */
6314       {
6315         const char *const *t = 0;
6316         const char *s;
6317         enum rtx_code code = GET_CODE (x);
6318         static const char * const tbl[3][3] = {
6319           { "and", "andc", "nor" },
6320           { "or", "orc", "nand" },
6321           { "xor", "eqv", "xor" } };
6322
6323         if (code == AND)
6324           t = tbl[0];
6325         else if (code == IOR)
6326           t = tbl[1];
6327         else if (code == XOR)
6328           t = tbl[2];
6329         else
6330           output_operand_lossage ("invalid %%q value");
6331
6332         if (GET_CODE (XEXP (x, 0)) != NOT)
6333           s = t[0];
6334         else
6335           {
6336             if (GET_CODE (XEXP (x, 1)) == NOT)
6337               s = t[2];
6338             else
6339               s = t[1];
6340           }
6341         
6342         fputs (s, file);
6343       }
6344       return;
6345
6346     case 'R':
6347       /* X is a CR register.  Print the mask for `mtcrf'.  */
6348       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
6349         output_operand_lossage ("invalid %%R value");
6350       else
6351         fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
6352       return;
6353
6354     case 's':
6355       /* Low 5 bits of 32 - value */
6356       if (! INT_P (x))
6357         output_operand_lossage ("invalid %%s value");
6358       else
6359         fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INT_LOWPART (x)) & 31);
6360       return;
6361
6362     case 'S':
6363       /* PowerPC64 mask position.  All 0's and all 1's are excluded.
6364          CONST_INT 32-bit mask is considered sign-extended so any
6365          transition must occur within the CONST_INT, not on the boundary.  */
6366       if (! mask64_operand (x, VOIDmode))
6367         output_operand_lossage ("invalid %%S value");
6368
6369       val = INT_LOWPART (x);
6370
6371       if (val & 1)      /* Clear Left */
6372         {
6373           for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
6374             if (!((val >>= 1) & 1))
6375               break;
6376
6377 #if HOST_BITS_PER_WIDE_INT == 32
6378           if (GET_CODE (x) == CONST_DOUBLE && i == 32)
6379             {
6380               val = CONST_DOUBLE_HIGH (x);
6381
6382               if (val == 0)
6383                 --i;
6384               else
6385                 for (i = 32; i < 64; i++)
6386                   if (!((val >>= 1) & 1))
6387                     break;
6388             }
6389 #endif
6390         /* i = index of last set bit from right
6391            mask begins at 63 - i from left */
6392           if (i > 63)
6393             output_operand_lossage ("%%S computed all 1's mask");
6394
6395           fprintf (file, "%d", 63 - i);
6396           return;
6397         }
6398       else      /* Clear Right */
6399         {
6400           for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
6401             if ((val >>= 1) & 1)
6402               break;
6403
6404 #if HOST_BITS_PER_WIDE_INT == 32
6405         if (GET_CODE (x) == CONST_DOUBLE && i == 32)
6406           {
6407             val = CONST_DOUBLE_HIGH (x);
6408
6409             if (val == (HOST_WIDE_INT) -1)
6410               --i;
6411             else
6412               for (i = 32; i < 64; i++)
6413                 if ((val >>= 1) & 1)
6414                   break;
6415           }
6416 #endif
6417         /* i = index of last clear bit from right
6418            mask ends at 62 - i from left */
6419           if (i > 62)
6420             output_operand_lossage ("%%S computed all 0's mask");
6421
6422           fprintf (file, "%d", 62 - i);
6423           return;
6424         }
6425
6426     case 'T':
6427       /* Print the symbolic name of a branch target register.  */
6428       if (GET_CODE (x) != REG || (REGNO (x) != LINK_REGISTER_REGNUM
6429                                   && REGNO (x) != COUNT_REGISTER_REGNUM))
6430         output_operand_lossage ("invalid %%T value");
6431       else if (REGNO (x) == LINK_REGISTER_REGNUM)
6432         fputs (TARGET_NEW_MNEMONICS ? "lr" : "r", file);
6433       else
6434         fputs ("ctr", file);
6435       return;
6436
6437     case 'u':
6438       /* High-order 16 bits of constant for use in unsigned operand.  */
6439       if (! INT_P (x))
6440         output_operand_lossage ("invalid %%u value");
6441       else
6442         fprintf (file, HOST_WIDE_INT_PRINT_HEX, 
6443                  (INT_LOWPART (x) >> 16) & 0xffff);
6444       return;
6445
6446     case 'v':
6447       /* High-order 16 bits of constant for use in signed operand.  */
6448       if (! INT_P (x))
6449         output_operand_lossage ("invalid %%v value");
6450       else
6451         fprintf (file, HOST_WIDE_INT_PRINT_HEX,
6452                  (INT_LOWPART (x) >> 16) & 0xffff);
6453       return;
6454
6455     case 'U':
6456       /* Print `u' if this has an auto-increment or auto-decrement.  */
6457       if (GET_CODE (x) == MEM
6458           && (GET_CODE (XEXP (x, 0)) == PRE_INC
6459               || GET_CODE (XEXP (x, 0)) == PRE_DEC))
6460         putc ('u', file);
6461       return;
6462
6463     case 'V':
6464       /* Print the trap code for this operand.  */
6465       switch (GET_CODE (x))
6466         {
6467         case EQ:
6468           fputs ("eq", file);   /* 4 */
6469           break;
6470         case NE:
6471           fputs ("ne", file);   /* 24 */
6472           break;
6473         case LT:
6474           fputs ("lt", file);   /* 16 */
6475           break;
6476         case LE:
6477           fputs ("le", file);   /* 20 */
6478           break;
6479         case GT:
6480           fputs ("gt", file);   /* 8 */
6481           break;
6482         case GE:
6483           fputs ("ge", file);   /* 12 */
6484           break;
6485         case LTU:
6486           fputs ("llt", file);  /* 2 */
6487           break;
6488         case LEU:
6489           fputs ("lle", file);  /* 6 */
6490           break;
6491         case GTU:
6492           fputs ("lgt", file);  /* 1 */
6493           break;
6494         case GEU:
6495           fputs ("lge", file);  /* 5 */
6496           break;
6497         default:
6498           abort ();
6499         }
6500       break;
6501
6502     case 'w':
6503       /* If constant, low-order 16 bits of constant, signed.  Otherwise, write
6504          normally.  */
6505       if (INT_P (x))
6506         fprintf (file, HOST_WIDE_INT_PRINT_DEC, 
6507                  ((INT_LOWPART (x) & 0xffff) ^ 0x8000) - 0x8000);
6508       else
6509         print_operand (file, x, 0);
6510       return;
6511
6512     case 'W':
6513       /* MB value for a PowerPC64 rldic operand.  */
6514       val = (GET_CODE (x) == CONST_INT
6515              ? INTVAL (x) : CONST_DOUBLE_HIGH (x));
6516
6517       if (val < 0)
6518         i = -1;
6519       else
6520         for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
6521           if ((val <<= 1) < 0)
6522             break;
6523
6524 #if HOST_BITS_PER_WIDE_INT == 32
6525       if (GET_CODE (x) == CONST_INT && i >= 0)
6526         i += 32;  /* zero-extend high-part was all 0's */
6527       else if (GET_CODE (x) == CONST_DOUBLE && i == 32)
6528         {
6529           val = CONST_DOUBLE_LOW (x);
6530
6531           if (val == 0)
6532             abort ();
6533           else if (val < 0)
6534             --i;
6535           else
6536             for ( ; i < 64; i++)
6537               if ((val <<= 1) < 0)
6538                 break;
6539         }
6540 #endif
6541
6542       fprintf (file, "%d", i + 1);
6543       return;
6544
6545     case 'X':
6546       if (GET_CODE (x) == MEM
6547           && LEGITIMATE_INDEXED_ADDRESS_P (XEXP (x, 0), 0))
6548         putc ('x', file);
6549       return;
6550
6551     case 'Y':
6552       /* Like 'L', for third word of TImode  */
6553       if (GET_CODE (x) == REG)
6554         fprintf (file, "%s", reg_names[REGNO (x) + 2]);
6555       else if (GET_CODE (x) == MEM)
6556         {
6557           if (GET_CODE (XEXP (x, 0)) == PRE_INC
6558               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
6559             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 8));
6560           else
6561             output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
6562           if (small_data_operand (x, GET_MODE (x)))
6563             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
6564                      reg_names[SMALL_DATA_REG]);
6565         }
6566       return;
6567                             
6568     case 'z':
6569       /* X is a SYMBOL_REF.  Write out the name preceded by a
6570          period and without any trailing data in brackets.  Used for function
6571          names.  If we are configured for System V (or the embedded ABI) on
6572          the PowerPC, do not emit the period, since those systems do not use
6573          TOCs and the like.  */
6574       if (GET_CODE (x) != SYMBOL_REF)
6575         abort ();
6576
6577       if (XSTR (x, 0)[0] != '.')
6578         {
6579           switch (DEFAULT_ABI)
6580             {
6581             default:
6582               abort ();
6583
6584             case ABI_AIX:
6585               putc ('.', file);
6586               break;
6587
6588             case ABI_V4:
6589             case ABI_AIX_NODESC:
6590             case ABI_DARWIN:
6591               break;
6592             }
6593         }
6594 #if TARGET_AIX
6595       RS6000_OUTPUT_BASENAME (file, XSTR (x, 0));
6596 #else
6597       assemble_name (file, XSTR (x, 0));
6598 #endif
6599       return;
6600
6601     case 'Z':
6602       /* Like 'L', for last word of TImode.  */
6603       if (GET_CODE (x) == REG)
6604         fprintf (file, "%s", reg_names[REGNO (x) + 3]);
6605       else if (GET_CODE (x) == MEM)
6606         {
6607           if (GET_CODE (XEXP (x, 0)) == PRE_INC
6608               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
6609             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 12));
6610           else
6611             output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
6612           if (small_data_operand (x, GET_MODE (x)))
6613             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
6614                      reg_names[SMALL_DATA_REG]);
6615         }
6616       return;
6617
6618       /* Print AltiVec memory operand.  */
6619     case 'y':
6620       {
6621         rtx tmp;
6622
6623         if (GET_CODE (x) != MEM)
6624           abort ();
6625
6626         tmp = XEXP (x, 0);
6627
6628         if (GET_CODE (tmp) == REG)
6629           fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
6630         else if (GET_CODE (tmp) == PLUS && GET_CODE (XEXP (tmp, 1)) == REG)
6631           {
6632             if (REGNO (XEXP (tmp, 0)) == 0)
6633               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
6634                        reg_names[ REGNO (XEXP (tmp, 0)) ]);
6635             else
6636               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
6637                        reg_names[ REGNO (XEXP (tmp, 1)) ]);
6638           }
6639         else
6640           abort ();
6641         break;
6642       }
6643                             
6644     case 0:
6645       if (GET_CODE (x) == REG)
6646         fprintf (file, "%s", reg_names[REGNO (x)]);
6647       else if (GET_CODE (x) == MEM)
6648         {
6649           /* We need to handle PRE_INC and PRE_DEC here, since we need to
6650              know the width from the mode.  */
6651           if (GET_CODE (XEXP (x, 0)) == PRE_INC)
6652             fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
6653                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
6654           else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
6655             fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
6656                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
6657           else
6658             output_address (XEXP (x, 0));
6659         }
6660       else
6661         output_addr_const (file, x);
6662       return;
6663
6664     default:
6665       output_operand_lossage ("invalid %%xn code");
6666     }
6667 }
6668 \f
6669 /* Print the address of an operand.  */
6670
6671 void
6672 print_operand_address (file, x)
6673      FILE *file;
6674      rtx x;
6675 {
6676   if (GET_CODE (x) == REG)
6677     fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
6678   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
6679            || GET_CODE (x) == LABEL_REF)
6680     {
6681       output_addr_const (file, x);
6682       if (small_data_operand (x, GET_MODE (x)))
6683         fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
6684                  reg_names[SMALL_DATA_REG]);
6685       else if (TARGET_TOC)
6686         abort ();
6687     }
6688   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG)
6689     {
6690       if (REGNO (XEXP (x, 0)) == 0)
6691         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
6692                  reg_names[ REGNO (XEXP (x, 0)) ]);
6693       else
6694         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
6695                  reg_names[ REGNO (XEXP (x, 1)) ]);
6696     }
6697   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
6698     {
6699       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (XEXP (x, 1)));
6700       fprintf (file, "(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
6701     }
6702 #if TARGET_ELF
6703   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
6704            && CONSTANT_P (XEXP (x, 1)))
6705     {
6706       output_addr_const (file, XEXP (x, 1));
6707       fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
6708     }
6709 #endif
6710 #if TARGET_MACHO
6711   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
6712            && CONSTANT_P (XEXP (x, 1)))
6713     {
6714       fprintf (file, "lo16(");
6715       output_addr_const (file, XEXP (x, 1));
6716       fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
6717     }
6718 #endif
6719   else if (LEGITIMATE_CONSTANT_POOL_ADDRESS_P (x))
6720     {
6721       if (TARGET_AIX && (!TARGET_ELF || !TARGET_MINIMAL_TOC))
6722         {
6723           rtx contains_minus = XEXP (x, 1);
6724           rtx minus, symref;
6725           const char *name;
6726           
6727           /* Find the (minus (sym) (toc)) buried in X, and temporarily
6728              turn it into (sym) for output_addr_const.  */
6729           while (GET_CODE (XEXP (contains_minus, 0)) != MINUS)
6730             contains_minus = XEXP (contains_minus, 0);
6731
6732           minus = XEXP (contains_minus, 0);
6733           symref = XEXP (minus, 0);
6734           XEXP (contains_minus, 0) = symref;
6735           if (TARGET_ELF)
6736             {
6737               char *newname;
6738
6739               name = XSTR (symref, 0);
6740               newname = alloca (strlen (name) + sizeof ("@toc"));
6741               strcpy (newname, name);
6742               strcat (newname, "@toc");
6743               XSTR (symref, 0) = newname;
6744             }
6745           output_addr_const (file, XEXP (x, 1));
6746           if (TARGET_ELF)
6747             XSTR (symref, 0) = name;
6748           XEXP (contains_minus, 0) = minus;
6749         }
6750       else
6751         output_addr_const (file, XEXP (x, 1));
6752
6753       fprintf (file, "(%s)", reg_names[REGNO (XEXP (x, 0))]);
6754     }
6755   else
6756     abort ();
6757 }
6758 \f
6759 /* Target hook for assembling integer objects.  The powerpc version has
6760    to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
6761    is defined.  It also needs to handle DI-mode objects on 64-bit
6762    targets.  */
6763
6764 static bool
6765 rs6000_assemble_integer (x, size, aligned_p)
6766      rtx x;
6767      unsigned int size;
6768      int aligned_p;
6769 {
6770 #ifdef RELOCATABLE_NEEDS_FIXUP
6771   /* Special handling for SI values.  */
6772   if (size == 4 && aligned_p)
6773     {
6774       extern int in_toc_section PARAMS ((void));
6775       static int recurse = 0;
6776       
6777       /* For -mrelocatable, we mark all addresses that need to be fixed up
6778          in the .fixup section.  */
6779       if (TARGET_RELOCATABLE
6780           && !in_toc_section ()
6781           && !in_text_section ()
6782           && !recurse
6783           && GET_CODE (x) != CONST_INT
6784           && GET_CODE (x) != CONST_DOUBLE
6785           && CONSTANT_P (x))
6786         {
6787           char buf[256];
6788
6789           recurse = 1;
6790           ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
6791           fixuplabelno++;
6792           ASM_OUTPUT_LABEL (asm_out_file, buf);
6793           fprintf (asm_out_file, "\t.long\t(");
6794           output_addr_const (asm_out_file, x);
6795           fprintf (asm_out_file, ")@fixup\n");
6796           fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
6797           ASM_OUTPUT_ALIGN (asm_out_file, 2);
6798           fprintf (asm_out_file, "\t.long\t");
6799           assemble_name (asm_out_file, buf);
6800           fprintf (asm_out_file, "\n\t.previous\n");
6801           recurse = 0;
6802           return true;
6803         }
6804       /* Remove initial .'s to turn a -mcall-aixdesc function
6805          address into the address of the descriptor, not the function
6806          itself.  */
6807       else if (GET_CODE (x) == SYMBOL_REF
6808                && XSTR (x, 0)[0] == '.'
6809                && DEFAULT_ABI == ABI_AIX)
6810         {
6811           const char *name = XSTR (x, 0);
6812           while (*name == '.')
6813             name++;
6814
6815           fprintf (asm_out_file, "\t.long\t%s\n", name);
6816           return true;
6817         }
6818     }
6819 #endif /* RELOCATABLE_NEEDS_FIXUP */
6820   return default_assemble_integer (x, size, aligned_p);
6821 }
6822 \f
6823 enum rtx_code
6824 rs6000_reverse_condition (mode, code)
6825      enum machine_mode mode;
6826      enum rtx_code code;
6827 {
6828   /* Reversal of FP compares takes care -- an ordered compare
6829      becomes an unordered compare and vice versa.  */
6830   if (mode == CCFPmode)
6831     return reverse_condition_maybe_unordered (code);
6832   else
6833     return reverse_condition (code);
6834 }
6835
6836 /* Generate a compare for CODE.  Return a brand-new rtx that
6837    represents the result of the compare.  */
6838
6839 static rtx
6840 rs6000_generate_compare (code)
6841      enum rtx_code code;
6842 {
6843   enum machine_mode comp_mode;
6844   rtx compare_result;
6845
6846   if (rs6000_compare_fp_p)
6847     comp_mode = CCFPmode;
6848   else if (code == GTU || code == LTU
6849           || code == GEU || code == LEU)
6850     comp_mode = CCUNSmode;
6851   else
6852     comp_mode = CCmode;
6853
6854   /* First, the compare.  */
6855   compare_result = gen_reg_rtx (comp_mode);
6856   emit_insn (gen_rtx_SET (VOIDmode, compare_result,
6857                           gen_rtx_COMPARE (comp_mode,
6858                                            rs6000_compare_op0, 
6859                                            rs6000_compare_op1)));
6860   
6861   /* Some kinds of FP comparisons need an OR operation;
6862      except for flag_unsafe_math_optimizations we don't bother.  */
6863   if (rs6000_compare_fp_p
6864       && ! flag_unsafe_math_optimizations
6865       && (code == LE || code == GE
6866           || code == UNEQ || code == LTGT
6867           || code == UNGT || code == UNLT))
6868     {
6869       enum rtx_code or1, or2;
6870       rtx or1_rtx, or2_rtx, compare2_rtx;
6871       rtx or_result = gen_reg_rtx (CCEQmode);
6872       
6873       switch (code)
6874         {
6875         case LE: or1 = LT;  or2 = EQ;  break;
6876         case GE: or1 = GT;  or2 = EQ;  break;
6877         case UNEQ: or1 = UNORDERED;  or2 = EQ;  break;
6878         case LTGT: or1 = LT;  or2 = GT;  break;
6879         case UNGT: or1 = UNORDERED;  or2 = GT;  break;
6880         case UNLT: or1 = UNORDERED;  or2 = LT;  break;
6881         default:  abort ();
6882         }
6883       validate_condition_mode (or1, comp_mode);
6884       validate_condition_mode (or2, comp_mode);
6885       or1_rtx = gen_rtx (or1, SImode, compare_result, const0_rtx);
6886       or2_rtx = gen_rtx (or2, SImode, compare_result, const0_rtx);
6887       compare2_rtx = gen_rtx_COMPARE (CCEQmode,
6888                                       gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
6889                                       const_true_rtx);
6890       emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
6891
6892       compare_result = or_result;
6893       code = EQ;
6894     }
6895
6896   validate_condition_mode (code, GET_MODE (compare_result));
6897   
6898   return gen_rtx (code, VOIDmode, compare_result, const0_rtx);
6899 }
6900
6901
6902 /* Emit the RTL for an sCOND pattern.  */
6903
6904 void
6905 rs6000_emit_sCOND (code, result)
6906      enum rtx_code code;
6907      rtx result;
6908 {
6909   rtx condition_rtx;
6910   enum machine_mode op_mode;
6911
6912   condition_rtx = rs6000_generate_compare (code);
6913
6914   op_mode = GET_MODE (rs6000_compare_op0);
6915   if (op_mode == VOIDmode)
6916     op_mode = GET_MODE (rs6000_compare_op1);
6917
6918   if (TARGET_POWERPC64 && (op_mode == DImode || rs6000_compare_fp_p))
6919     {
6920       PUT_MODE (condition_rtx, DImode);
6921       convert_move (result, condition_rtx, 0);
6922     }
6923   else
6924     {
6925       PUT_MODE (condition_rtx, SImode);
6926       emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
6927     }
6928 }
6929
6930 /* Emit a branch of kind CODE to location LOC.  */
6931
6932 void
6933 rs6000_emit_cbranch (code, loc)
6934      enum rtx_code code;
6935      rtx loc;
6936 {
6937   rtx condition_rtx, loc_ref;
6938
6939   condition_rtx = rs6000_generate_compare (code);
6940   loc_ref = gen_rtx_LABEL_REF (VOIDmode, loc);
6941   emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
6942                                gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
6943                                                      loc_ref, pc_rtx)));
6944 }
6945
6946 /* Return the string to output a conditional branch to LABEL, which is
6947    the operand number of the label, or -1 if the branch is really a
6948    conditional return.  
6949
6950    OP is the conditional expression.  XEXP (OP, 0) is assumed to be a
6951    condition code register and its mode specifies what kind of
6952    comparison we made.
6953
6954    REVERSED is non-zero if we should reverse the sense of the comparison.
6955
6956    INSN is the insn.  */
6957
6958 char *
6959 output_cbranch (op, label, reversed, insn)
6960      rtx op;
6961      const char * label;
6962      int reversed;
6963      rtx insn;
6964 {
6965   static char string[64];
6966   enum rtx_code code = GET_CODE (op);
6967   rtx cc_reg = XEXP (op, 0);
6968   enum machine_mode mode = GET_MODE (cc_reg);
6969   int cc_regno = REGNO (cc_reg) - CR0_REGNO;
6970   int need_longbranch = label != NULL && get_attr_length (insn) == 8;
6971   int really_reversed = reversed ^ need_longbranch;
6972   char *s = string;
6973   const char *ccode;
6974   const char *pred;
6975   rtx note;
6976
6977   validate_condition_mode (code, mode);
6978
6979   /* Work out which way this really branches.  We could use
6980      reverse_condition_maybe_unordered here always but this
6981      makes the resulting assembler clearer.  */
6982   if (really_reversed)
6983     code = rs6000_reverse_condition (mode, code);
6984
6985   switch (code)
6986     {
6987       /* Not all of these are actually distinct opcodes, but
6988          we distinguish them for clarity of the resulting assembler.  */
6989     case NE: case LTGT:
6990       ccode = "ne"; break;
6991     case EQ: case UNEQ:
6992       ccode = "eq"; break;
6993     case GE: case GEU: 
6994       ccode = "ge"; break;
6995     case GT: case GTU: case UNGT: 
6996       ccode = "gt"; break;
6997     case LE: case LEU: 
6998       ccode = "le"; break;
6999     case LT: case LTU: case UNLT: 
7000       ccode = "lt"; break;
7001     case UNORDERED: ccode = "un"; break;
7002     case ORDERED: ccode = "nu"; break;
7003     case UNGE: ccode = "nl"; break;
7004     case UNLE: ccode = "ng"; break;
7005     default:
7006       abort ();
7007     }
7008   
7009   /* Maybe we have a guess as to how likely the branch is.  
7010      The old mnemonics don't have a way to specify this information.  */
7011   note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
7012   if (note != NULL_RTX)
7013     {
7014       /* PROB is the difference from 50%.  */
7015       int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
7016       
7017       /* For branches that are very close to 50%, assume not-taken.  */
7018       if (abs (prob) > REG_BR_PROB_BASE / 20
7019           && ((prob > 0) ^ need_longbranch))
7020         pred = "+";
7021       else
7022         pred = "-";
7023     }
7024   else
7025     pred = "";
7026
7027   if (label == NULL)
7028     s += sprintf (s, "{b%sr|b%slr%s} ", ccode, ccode, pred);
7029   else
7030     s += sprintf (s, "{b%s|b%s%s} ", ccode, ccode, pred);
7031
7032   /* We need to escape any '%' characters in the reg_names string.
7033      Assume they'd only be the first character...  */
7034   if (reg_names[cc_regno + CR0_REGNO][0] == '%')
7035     *s++ = '%';
7036   s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
7037
7038   if (label != NULL)
7039     {
7040       /* If the branch distance was too far, we may have to use an
7041          unconditional branch to go the distance.  */
7042       if (need_longbranch)
7043         s += sprintf (s, ",$+8\n\tb %s", label);
7044       else
7045         s += sprintf (s, ",%s", label);
7046     }
7047
7048   return string;
7049 }
7050
7051 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
7052    operands of the last comparison is nonzero/true, FALSE_COND if it
7053    is zero/false.  Return 0 if the hardware has no such operation.  */
7054
7055 int
7056 rs6000_emit_cmove (dest, op, true_cond, false_cond)
7057      rtx dest;
7058      rtx op;
7059      rtx true_cond;
7060      rtx false_cond;
7061 {
7062   enum rtx_code code = GET_CODE (op);
7063   rtx op0 = rs6000_compare_op0;
7064   rtx op1 = rs6000_compare_op1;
7065   REAL_VALUE_TYPE c1;
7066   enum machine_mode mode = GET_MODE (op0);
7067   rtx temp;
7068
7069   /* First, work out if the hardware can do this at all, or
7070      if it's too slow...  */
7071   /* If the comparison is an integer one, since we only have fsel
7072      it'll be cheaper to use a branch.  */
7073   if (! rs6000_compare_fp_p)
7074     return 0;
7075
7076   /* Eliminate half of the comparisons by switching operands, this
7077      makes the remaining code simpler.  */
7078   if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
7079       || code == LTGT || code == LT)
7080     {
7081       code = reverse_condition_maybe_unordered (code);
7082       temp = true_cond;
7083       true_cond = false_cond;
7084       false_cond = temp;
7085     }
7086
7087   /* UNEQ and LTGT take four instructions for a comparison with zero,
7088      it'll probably be faster to use a branch here too.  */
7089   if (code == UNEQ)
7090     return 0;
7091   
7092   if (GET_CODE (op1) == CONST_DOUBLE)
7093     REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
7094     
7095   /* We're going to try to implement comparions by performing
7096      a subtract, then comparing against zero.  Unfortunately,
7097      Inf - Inf is NaN which is not zero, and so if we don't
7098      know that the the operand is finite and the comparison
7099      would treat EQ different to UNORDERED, we can't do it.  */
7100   if (! flag_unsafe_math_optimizations
7101       && code != GT && code != UNGE
7102       && (GET_CODE (op1) != CONST_DOUBLE || target_isinf (c1))
7103       /* Constructs of the form (a OP b ? a : b) are safe.  */
7104       && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
7105           || (! rtx_equal_p (op0, true_cond) 
7106               && ! rtx_equal_p (op1, true_cond))))
7107     return 0;
7108   /* At this point we know we can use fsel.  */
7109
7110   /* Reduce the comparison to a comparison against zero.  */
7111   temp = gen_reg_rtx (mode);
7112   emit_insn (gen_rtx_SET (VOIDmode, temp,
7113                           gen_rtx_MINUS (mode, op0, op1)));
7114   op0 = temp;
7115   op1 = CONST0_RTX (mode);
7116
7117   /* If we don't care about NaNs we can reduce some of the comparisons
7118      down to faster ones.  */
7119   if (flag_unsafe_math_optimizations)
7120     switch (code)
7121       {
7122       case GT:
7123         code = LE;
7124         temp = true_cond;
7125         true_cond = false_cond;
7126         false_cond = temp;
7127         break;
7128       case UNGE:
7129         code = GE;
7130         break;
7131       case UNEQ:
7132         code = EQ;
7133         break;
7134       default:
7135         break;
7136       }
7137
7138   /* Now, reduce everything down to a GE.  */
7139   switch (code)
7140     {
7141     case GE:
7142       break;
7143
7144     case LE:
7145       temp = gen_reg_rtx (mode);
7146       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (mode, op0)));
7147       op0 = temp;
7148       break;
7149
7150     case ORDERED:
7151       temp = gen_reg_rtx (mode);
7152       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (mode, op0)));
7153       op0 = temp;
7154       break;
7155
7156     case EQ:
7157       temp = gen_reg_rtx (mode);
7158       emit_insn (gen_rtx_SET (VOIDmode, temp, 
7159                               gen_rtx_NEG (mode,
7160                                            gen_rtx_ABS (mode, op0))));
7161       op0 = temp;
7162       break;
7163
7164     case UNGE:
7165       temp = gen_reg_rtx (mode);
7166       emit_insn (gen_rtx_SET (VOIDmode, temp,
7167                               gen_rtx_IF_THEN_ELSE (mode, 
7168                                                     gen_rtx_GE (VOIDmode,
7169                                                                 op0, op1),
7170                                                     true_cond, false_cond)));
7171       false_cond = temp;
7172       true_cond = false_cond;
7173
7174       temp = gen_reg_rtx (mode);
7175       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (mode, op0)));
7176       op0 = temp;
7177       break;
7178
7179     case GT:
7180       temp = gen_reg_rtx (mode);
7181       emit_insn (gen_rtx_SET (VOIDmode, temp,
7182                               gen_rtx_IF_THEN_ELSE (mode, 
7183                                                     gen_rtx_GE (VOIDmode,
7184                                                                 op0, op1),
7185                                                     true_cond, false_cond)));
7186       true_cond = temp;
7187       false_cond = true_cond;
7188
7189       temp = gen_reg_rtx (mode);
7190       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (mode, op0)));
7191       op0 = temp;
7192       break;
7193
7194     default:
7195       abort ();
7196     }
7197
7198   emit_insn (gen_rtx_SET (VOIDmode, dest,
7199                           gen_rtx_IF_THEN_ELSE (GET_MODE (dest),
7200                                                 gen_rtx_GE (VOIDmode,
7201                                                             op0, op1),
7202                                                 true_cond, false_cond)));
7203   return 1;
7204 }
7205
7206 void
7207 rs6000_emit_minmax (dest, code, op0, op1)
7208      rtx dest;
7209      enum rtx_code code;
7210      rtx op0;
7211      rtx op1;
7212 {
7213   enum machine_mode mode = GET_MODE (op0);
7214   rtx target;
7215   if (code == SMAX || code == UMAX)
7216     target = emit_conditional_move (dest, GE, op0, op1, mode, 
7217                                     op0, op1, mode, 0);
7218   else
7219     target = emit_conditional_move (dest, GE, op0, op1, mode, 
7220                                     op1, op0, mode, 0);
7221   if (target == NULL_RTX)
7222     abort ();
7223   if (target != dest)
7224     emit_move_insn (dest, target);
7225 }
7226 \f
7227 /* This page contains routines that are used to determine what the
7228    function prologue and epilogue code will do and write them out.  */
7229
7230 /* Return the first fixed-point register that is required to be
7231    saved. 32 if none.  */
7232
7233 int
7234 first_reg_to_save ()
7235 {
7236   int first_reg;
7237
7238   /* Find lowest numbered live register.  */
7239   for (first_reg = 13; first_reg <= 31; first_reg++)
7240     if (regs_ever_live[first_reg] 
7241         && (! call_used_regs[first_reg]
7242             || (first_reg == PIC_OFFSET_TABLE_REGNUM
7243                 && ((DEFAULT_ABI == ABI_V4 && flag_pic == 1)
7244                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic)))))
7245       break;
7246
7247   if (current_function_profile)
7248     {
7249       /* AIX must save/restore every register that contains a parameter
7250          before/after the .__mcount call plus an additional register
7251          for the static chain, if needed; use registers from 30 down to 22
7252          to do this.  */
7253       if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_DARWIN)
7254         {
7255           int last_parm_reg, profile_first_reg;
7256
7257           /* Figure out last used parameter register.  The proper thing
7258              to do is to walk incoming args of the function.  A function
7259              might have live parameter registers even if it has no
7260              incoming args.  */
7261           for (last_parm_reg = 10;
7262                last_parm_reg > 2 && ! regs_ever_live [last_parm_reg];
7263                last_parm_reg--)
7264             ;
7265
7266           /* Calculate first reg for saving parameter registers
7267              and static chain.
7268              Skip reg 31 which may contain the frame pointer.  */
7269           profile_first_reg = (33 - last_parm_reg
7270                                - (current_function_needs_context ? 1 : 0));
7271 #if TARGET_MACHO
7272           /* Need to skip another reg to account for R31 being PICBASE
7273              (when flag_pic is set) or R30 being used as the frame
7274              pointer (when flag_pic is not set).  */
7275           --profile_first_reg;
7276 #endif
7277           /* Do not save frame pointer if no parameters needs to be saved.  */
7278           if (profile_first_reg == 31)
7279             profile_first_reg = 32;
7280
7281           if (first_reg > profile_first_reg)
7282             first_reg = profile_first_reg;
7283         }
7284
7285       /* SVR4 may need one register to preserve the static chain.  */
7286       else if (current_function_needs_context)
7287         {
7288           /* Skip reg 31 which may contain the frame pointer.  */
7289           if (first_reg > 30)
7290             first_reg = 30;
7291         }
7292     }
7293
7294 #if TARGET_MACHO
7295   if (flag_pic && current_function_uses_pic_offset_table &&
7296       (first_reg > PIC_OFFSET_TABLE_REGNUM))
7297     return PIC_OFFSET_TABLE_REGNUM;
7298 #endif
7299
7300   return first_reg;
7301 }
7302
7303 /* Similar, for FP regs.  */
7304
7305 int
7306 first_fp_reg_to_save ()
7307 {
7308   int first_reg;
7309
7310   /* Find lowest numbered live register.  */
7311   for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
7312     if (regs_ever_live[first_reg])
7313       break;
7314
7315   return first_reg;
7316 }
7317
7318 /* Similar, for AltiVec regs.  */
7319
7320 static int
7321 first_altivec_reg_to_save ()
7322 {
7323   int i;
7324
7325   /* Stack frame remains as is unless we are in AltiVec ABI.  */
7326   if (! TARGET_ALTIVEC_ABI)
7327     return LAST_ALTIVEC_REGNO + 1;
7328
7329   /* Find lowest numbered live register.  */
7330   for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
7331     if (regs_ever_live[i])
7332       break;
7333
7334   return i;
7335 }
7336
7337 /* Return a 32-bit mask of the AltiVec registers we need to set in
7338    VRSAVE.  Bit n of the return value is 1 if Vn is live.  The MSB in
7339    the 32-bit word is 0.  */
7340
7341 static unsigned int
7342 compute_vrsave_mask ()
7343 {
7344   unsigned int i, mask = 0;
7345
7346   /* First, find out if we use _any_ altivec registers.  */
7347   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
7348     if (regs_ever_live[i])
7349       mask |= ALTIVEC_REG_BIT (i);
7350
7351   if (mask == 0)
7352     return mask;
7353
7354   /* Next, add all registers that are call-clobbered.  We do this
7355      because post-reload register optimizers such as regrename_optimize
7356      may choose to use them.  They never change the register class
7357      chosen by reload, so cannot create new uses of altivec registers
7358      if there were none before, so the early exit above is safe.  */
7359   /* ??? Alternately, we could define HARD_REGNO_RENAME_OK to disallow
7360      altivec registers not saved in the mask, which might well make the
7361      adjustments below more effective in eliding the save/restore of
7362      VRSAVE in small functions.  */
7363   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
7364     if (call_used_regs[i])
7365       mask |= ALTIVEC_REG_BIT (i);
7366
7367   /* Next, remove the argument registers from the set.  These must
7368      be in the VRSAVE mask set by the caller, so we don't need to add
7369      them in again.  More importantly, the mask we compute here is
7370      used to generate CLOBBERs in the set_vrsave insn, and we do not
7371      wish the argument registers to die.  */
7372   for (i = cfun->args_info.vregno; i >= ALTIVEC_ARG_MIN_REG; --i)
7373     mask &= ~ALTIVEC_REG_BIT (i);
7374
7375   /* Similarly, remove the return value from the set.  */
7376   {
7377     bool yes = false;
7378     diddle_return_value (is_altivec_return_reg, &yes);
7379     if (yes)
7380       mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
7381   }
7382
7383   return mask;
7384 }
7385
7386 static void
7387 is_altivec_return_reg (reg, xyes)
7388      rtx reg;
7389      void *xyes;
7390 {
7391   bool *yes = (bool *) xyes;
7392   if (REGNO (reg) == ALTIVEC_ARG_RETURN)
7393     *yes = true;
7394 }
7395
7396 \f
7397 /* Calculate the stack information for the current function.  This is
7398    complicated by having two separate calling sequences, the AIX calling
7399    sequence and the V.4 calling sequence.
7400
7401    AIX (and Darwin/Mac OS X) stack frames look like:
7402                                                           32-bit  64-bit
7403         SP----> +---------------------------------------+
7404                 | back chain to caller                  | 0       0
7405                 +---------------------------------------+
7406                 | saved CR                              | 4       8 (8-11)
7407                 +---------------------------------------+
7408                 | saved LR                              | 8       16
7409                 +---------------------------------------+
7410                 | reserved for compilers                | 12      24
7411                 +---------------------------------------+
7412                 | reserved for binders                  | 16      32
7413                 +---------------------------------------+
7414                 | saved TOC pointer                     | 20      40
7415                 +---------------------------------------+
7416                 | Parameter save area (P)               | 24      48
7417                 +---------------------------------------+
7418                 | Alloca space (A)                      | 24+P    etc.
7419                 +---------------------------------------+
7420                 | Local variable space (L)              | 24+P+A
7421                 +---------------------------------------+
7422                 | Float/int conversion temporary (X)    | 24+P+A+L
7423                 +---------------------------------------+
7424                 | Save area for AltiVec registers (W)   | 24+P+A+L+X
7425                 +---------------------------------------+
7426                 | AltiVec alignment padding (Y)         | 24+P+A+L+X+W
7427                 +---------------------------------------+
7428                 | Save area for VRSAVE register (Z)     | 24+P+A+L+X+W+Y
7429                 +---------------------------------------+
7430                 | Save area for GP registers (G)        | 24+P+A+X+L+X+W+Y+Z
7431                 +---------------------------------------+
7432                 | Save area for FP registers (F)        | 24+P+A+X+L+X+W+Y+Z+G
7433                 +---------------------------------------+
7434         old SP->| back chain to caller's caller         |
7435                 +---------------------------------------+
7436
7437    The required alignment for AIX configurations is two words (i.e., 8
7438    or 16 bytes).
7439
7440
7441    V.4 stack frames look like:
7442
7443         SP----> +---------------------------------------+
7444                 | back chain to caller                  | 0
7445                 +---------------------------------------+
7446                 | caller's saved LR                     | 4
7447                 +---------------------------------------+
7448                 | Parameter save area (P)               | 8
7449                 +---------------------------------------+
7450                 | Alloca space (A)                      | 8+P
7451                 +---------------------------------------+    
7452                 | Varargs save area (V)                 | 8+P+A
7453                 +---------------------------------------+    
7454                 | Local variable space (L)              | 8+P+A+V
7455                 +---------------------------------------+    
7456                 | Float/int conversion temporary (X)    | 8+P+A+V+L
7457                 +---------------------------------------+
7458                 | Save area for AltiVec registers (W)   | 8+P+A+V+L+X
7459                 +---------------------------------------+
7460                 | AltiVec alignment padding (Y)         | 8+P+A+V+L+X+W
7461                 +---------------------------------------+
7462                 | Save area for VRSAVE register (Z)     | 8+P+A+V+L+X+W+Y
7463                 +---------------------------------------+
7464                 | saved CR (C)                          | 8+P+A+V+L+X+W+Y+Z
7465                 +---------------------------------------+    
7466                 | Save area for GP registers (G)        | 8+P+A+V+L+X+W+Y+Z+C
7467                 +---------------------------------------+    
7468                 | Save area for FP registers (F)        | 8+P+A+V+L+X+W+Y+Z+C+G
7469                 +---------------------------------------+
7470         old SP->| back chain to caller's caller         |
7471                 +---------------------------------------+
7472
7473    The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
7474    given.  (But note below and in sysv4.h that we require only 8 and
7475    may round up the size of our stack frame anyways.  The historical
7476    reason is early versions of powerpc-linux which didn't properly
7477    align the stack at program startup.  A happy side-effect is that
7478    -mno-eabi libraries can be used with -meabi programs.)
7479
7480    The EABI configuration defaults to the V.4 layout, unless
7481    -mcall-aix is used, in which case the AIX layout is used.  However,
7482    the stack alignment requirements may differ.  If -mno-eabi is not
7483    given, the required stack alignment is 8 bytes; if -mno-eabi is
7484    given, the required alignment is 16 bytes.  (But see V.4 comment
7485    above.)  */
7486
7487 #ifndef ABI_STACK_BOUNDARY
7488 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
7489 #endif
7490
7491 rs6000_stack_t *
7492 rs6000_stack_info ()
7493 {
7494   static rs6000_stack_t info, zero_info;
7495   rs6000_stack_t *info_ptr = &info;
7496   int reg_size = TARGET_POWERPC64 ? 8 : 4;
7497   enum rs6000_abi abi;
7498   int ehrd_size;
7499   int total_raw_size;
7500
7501   /* Zero all fields portably.  */
7502   info = zero_info;
7503
7504   /* Select which calling sequence.  */
7505   info_ptr->abi = abi = DEFAULT_ABI;
7506
7507   /* Calculate which registers need to be saved & save area size.  */
7508   info_ptr->first_gp_reg_save = first_reg_to_save ();
7509   /* Assume that we will have to save PIC_OFFSET_TABLE_REGNUM, 
7510      even if it currently looks like we won't.  */
7511   if (((TARGET_TOC && TARGET_MINIMAL_TOC)
7512        || (flag_pic == 1 && abi == ABI_V4)
7513        || (flag_pic && abi == ABI_DARWIN))
7514       && info_ptr->first_gp_reg_save > PIC_OFFSET_TABLE_REGNUM)
7515     info_ptr->gp_size = reg_size * (32 - PIC_OFFSET_TABLE_REGNUM);
7516   else
7517     info_ptr->gp_size = reg_size * (32 - info_ptr->first_gp_reg_save);
7518
7519   info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
7520   info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
7521
7522   info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
7523   info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
7524                                  - info_ptr->first_altivec_reg_save);
7525
7526   /* Does this function call anything?  */
7527   info_ptr->calls_p = (! current_function_is_leaf
7528                        || cfun->machine->ra_needs_full_frame);
7529
7530   /* Determine if we need to save the link register.  */
7531   if (rs6000_ra_ever_killed ()
7532       || (DEFAULT_ABI == ABI_AIX && current_function_profile)
7533 #ifdef TARGET_RELOCATABLE
7534       || (TARGET_RELOCATABLE && (get_pool_size () != 0))
7535 #endif
7536       || (info_ptr->first_fp_reg_save != 64
7537           && !FP_SAVE_INLINE (info_ptr->first_fp_reg_save))
7538       || info_ptr->first_altivec_reg_save <= LAST_ALTIVEC_REGNO
7539       || (abi == ABI_V4 && current_function_calls_alloca)
7540       || (DEFAULT_ABI == ABI_DARWIN
7541           && flag_pic
7542           && current_function_uses_pic_offset_table)
7543       || info_ptr->calls_p)
7544     {
7545       info_ptr->lr_save_p = 1;
7546       regs_ever_live[LINK_REGISTER_REGNUM] = 1;
7547     }
7548
7549   /* Determine if we need to save the condition code registers.  */
7550   if (regs_ever_live[CR2_REGNO] 
7551       || regs_ever_live[CR3_REGNO]
7552       || regs_ever_live[CR4_REGNO])
7553     {
7554       info_ptr->cr_save_p = 1;
7555       if (abi == ABI_V4)
7556         info_ptr->cr_size = reg_size;
7557     }
7558
7559   /* If the current function calls __builtin_eh_return, then we need
7560      to allocate stack space for registers that will hold data for
7561      the exception handler.  */
7562   if (current_function_calls_eh_return)
7563     {
7564       unsigned int i;
7565       for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
7566         continue;
7567       ehrd_size = i * UNITS_PER_WORD;
7568     }
7569   else
7570     ehrd_size = 0;
7571
7572   /* Determine various sizes.  */
7573   info_ptr->reg_size     = reg_size;
7574   info_ptr->fixed_size   = RS6000_SAVE_AREA;
7575   info_ptr->varargs_size = RS6000_VARARGS_AREA;
7576   info_ptr->vars_size    = RS6000_ALIGN (get_frame_size (), 8);
7577   info_ptr->parm_size    = RS6000_ALIGN (current_function_outgoing_args_size,
7578                                          8);
7579
7580   if (TARGET_ALTIVEC_ABI)
7581     {
7582       info_ptr->vrsave_mask = compute_vrsave_mask ();
7583       info_ptr->vrsave_size  = info_ptr->vrsave_mask ? 4 : 0;
7584     }
7585   else
7586     {
7587       info_ptr->vrsave_mask = 0;
7588       info_ptr->vrsave_size = 0;
7589     }
7590
7591   /* Calculate the offsets.  */
7592   switch (abi)
7593     {
7594     case ABI_NONE:
7595     default:
7596       abort ();
7597
7598     case ABI_AIX:
7599     case ABI_AIX_NODESC:
7600     case ABI_DARWIN:
7601       info_ptr->fp_save_offset   = - info_ptr->fp_size;
7602       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
7603
7604       if (TARGET_ALTIVEC_ABI)
7605         {
7606           info_ptr->vrsave_save_offset
7607             = info_ptr->gp_save_offset - info_ptr->vrsave_size;
7608
7609           /* Align stack so vector save area is on a quadword boundary.  */
7610           if (info_ptr->altivec_size != 0)
7611             info_ptr->altivec_padding_size
7612               = 16 - (-info_ptr->vrsave_save_offset % 16);
7613           else
7614             info_ptr->altivec_padding_size = 0;
7615
7616           info_ptr->altivec_save_offset
7617             = info_ptr->vrsave_save_offset
7618             - info_ptr->altivec_padding_size
7619             - info_ptr->altivec_size;
7620
7621           /* Adjust for AltiVec case.  */
7622           info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
7623         }
7624       else
7625         info_ptr->ehrd_offset      = info_ptr->gp_save_offset - ehrd_size;
7626       info_ptr->cr_save_offset   = reg_size; /* first word when 64-bit.  */
7627       info_ptr->lr_save_offset   = 2*reg_size;
7628       break;
7629
7630     case ABI_V4:
7631       info_ptr->fp_save_offset   = - info_ptr->fp_size;
7632       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
7633       info_ptr->cr_save_offset   = info_ptr->gp_save_offset - info_ptr->cr_size;
7634
7635       if (TARGET_ALTIVEC_ABI)
7636         {
7637           info_ptr->vrsave_save_offset
7638             = info_ptr->cr_save_offset - info_ptr->vrsave_size;
7639
7640           /* Align stack so vector save area is on a quadword boundary.  */
7641           if (info_ptr->altivec_size != 0)
7642             info_ptr->altivec_padding_size
7643               = 16 - (-info_ptr->vrsave_save_offset % 16);
7644           else
7645             info_ptr->altivec_padding_size = 0;
7646
7647           info_ptr->altivec_save_offset
7648             = info_ptr->vrsave_save_offset
7649             - info_ptr->altivec_padding_size
7650             - info_ptr->altivec_size;
7651
7652           /* Adjust for AltiVec case.  */
7653           info_ptr->toc_save_offset
7654             = info_ptr->altivec_save_offset - info_ptr->toc_size;
7655         }
7656       else
7657         info_ptr->toc_save_offset  = info_ptr->cr_save_offset - info_ptr->toc_size;
7658       info_ptr->ehrd_offset      = info_ptr->toc_save_offset - ehrd_size;
7659       info_ptr->lr_save_offset   = reg_size;
7660       break;
7661     }
7662
7663   info_ptr->save_size    = RS6000_ALIGN (info_ptr->fp_size
7664                                          + info_ptr->gp_size
7665                                          + info_ptr->altivec_size
7666                                          + info_ptr->altivec_padding_size
7667                                          + info_ptr->vrsave_size
7668                                          + ehrd_size
7669                                          + info_ptr->cr_size
7670                                          + info_ptr->lr_size
7671                                          + info_ptr->vrsave_size
7672                                          + info_ptr->toc_size,
7673                                          (TARGET_ALTIVEC_ABI || ABI_DARWIN)
7674                                          ? 16 : 8);
7675
7676   total_raw_size         = (info_ptr->vars_size
7677                             + info_ptr->parm_size
7678                             + info_ptr->save_size
7679                             + info_ptr->varargs_size
7680                             + info_ptr->fixed_size);
7681
7682   info_ptr->total_size =
7683     RS6000_ALIGN (total_raw_size, ABI_STACK_BOUNDARY / BITS_PER_UNIT);
7684
7685   /* Determine if we need to allocate any stack frame:
7686
7687      For AIX we need to push the stack if a frame pointer is needed
7688      (because the stack might be dynamically adjusted), if we are
7689      debugging, if we make calls, or if the sum of fp_save, gp_save,
7690      and local variables are more than the space needed to save all
7691      non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
7692      + 18*8 = 288 (GPR13 reserved).
7693
7694      For V.4 we don't have the stack cushion that AIX uses, but assume
7695      that the debugger can handle stackless frames.  */
7696
7697   if (info_ptr->calls_p)
7698     info_ptr->push_p = 1;
7699
7700   else if (abi == ABI_V4)
7701     info_ptr->push_p = (total_raw_size > info_ptr->fixed_size
7702                         || info_ptr->calls_p);
7703
7704   else
7705     info_ptr->push_p = (frame_pointer_needed
7706                         || (abi != ABI_DARWIN && write_symbols != NO_DEBUG)
7707                         || ((total_raw_size - info_ptr->fixed_size)
7708                             > (TARGET_32BIT ? 220 : 288)));
7709
7710   /* Zero offsets if we're not saving those registers.  */
7711   if (info_ptr->fp_size == 0)
7712     info_ptr->fp_save_offset = 0;
7713
7714   if (info_ptr->gp_size == 0)
7715     info_ptr->gp_save_offset = 0;
7716
7717   if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
7718     info_ptr->altivec_save_offset = 0;
7719
7720   if (! TARGET_ALTIVEC_ABI || info_ptr->vrsave_mask == 0)
7721     info_ptr->vrsave_save_offset = 0;
7722
7723   if (! info_ptr->lr_save_p)
7724     info_ptr->lr_save_offset = 0;
7725
7726   if (! info_ptr->cr_save_p)
7727     info_ptr->cr_save_offset = 0;
7728
7729   if (! info_ptr->toc_save_p)
7730     info_ptr->toc_save_offset = 0;
7731
7732   return info_ptr;
7733 }
7734
7735 void
7736 debug_stack_info (info)
7737      rs6000_stack_t *info;
7738 {
7739   const char *abi_string;
7740
7741   if (! info)
7742     info = rs6000_stack_info ();
7743
7744   fprintf (stderr, "\nStack information for function %s:\n",
7745            ((current_function_decl && DECL_NAME (current_function_decl))
7746             ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
7747             : "<unknown>"));
7748
7749   switch (info->abi)
7750     {
7751     default:             abi_string = "Unknown";        break;
7752     case ABI_NONE:       abi_string = "NONE";           break;
7753     case ABI_AIX:
7754     case ABI_AIX_NODESC: abi_string = "AIX";            break;
7755     case ABI_DARWIN:     abi_string = "Darwin";         break;
7756     case ABI_V4:         abi_string = "V.4";            break;
7757     }
7758
7759   fprintf (stderr, "\tABI                 = %5s\n", abi_string);
7760
7761   if (TARGET_ALTIVEC_ABI)
7762     fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
7763
7764   if (info->first_gp_reg_save != 32)
7765     fprintf (stderr, "\tfirst_gp_reg_save   = %5d\n", info->first_gp_reg_save);
7766
7767   if (info->first_fp_reg_save != 64)
7768     fprintf (stderr, "\tfirst_fp_reg_save   = %5d\n", info->first_fp_reg_save);
7769
7770   if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
7771     fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
7772              info->first_altivec_reg_save);
7773
7774   if (info->lr_save_p)
7775     fprintf (stderr, "\tlr_save_p           = %5d\n", info->lr_save_p);
7776
7777   if (info->cr_save_p)
7778     fprintf (stderr, "\tcr_save_p           = %5d\n", info->cr_save_p);
7779
7780   if (info->toc_save_p)
7781     fprintf (stderr, "\ttoc_save_p          = %5d\n", info->toc_save_p);
7782
7783   if (info->vrsave_mask)
7784     fprintf (stderr, "\tvrsave_mask         = 0x%x\n", info->vrsave_mask);
7785
7786   if (info->push_p)
7787     fprintf (stderr, "\tpush_p              = %5d\n", info->push_p);
7788
7789   if (info->calls_p)
7790     fprintf (stderr, "\tcalls_p             = %5d\n", info->calls_p);
7791
7792   if (info->gp_save_offset)
7793     fprintf (stderr, "\tgp_save_offset      = %5d\n", info->gp_save_offset);
7794
7795   if (info->fp_save_offset)
7796     fprintf (stderr, "\tfp_save_offset      = %5d\n", info->fp_save_offset);
7797
7798   if (info->altivec_save_offset)
7799     fprintf (stderr, "\taltivec_save_offset = %5d\n",
7800              info->altivec_save_offset);
7801
7802   if (info->vrsave_save_offset)
7803     fprintf (stderr, "\tvrsave_save_offset  = %5d\n",
7804              info->vrsave_save_offset);
7805
7806   if (info->lr_save_offset)
7807     fprintf (stderr, "\tlr_save_offset      = %5d\n", info->lr_save_offset);
7808
7809   if (info->cr_save_offset)
7810     fprintf (stderr, "\tcr_save_offset      = %5d\n", info->cr_save_offset);
7811
7812   if (info->toc_save_offset)
7813     fprintf (stderr, "\ttoc_save_offset     = %5d\n", info->toc_save_offset);
7814
7815   if (info->varargs_save_offset)
7816     fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
7817
7818   if (info->total_size)
7819     fprintf (stderr, "\ttotal_size          = %5d\n", info->total_size);
7820
7821   if (info->varargs_size)
7822     fprintf (stderr, "\tvarargs_size        = %5d\n", info->varargs_size);
7823
7824   if (info->vars_size)
7825     fprintf (stderr, "\tvars_size           = %5d\n", info->vars_size);
7826
7827   if (info->parm_size)
7828     fprintf (stderr, "\tparm_size           = %5d\n", info->parm_size);
7829
7830   if (info->fixed_size)
7831     fprintf (stderr, "\tfixed_size          = %5d\n", info->fixed_size);
7832
7833   if (info->gp_size)
7834     fprintf (stderr, "\tgp_size             = %5d\n", info->gp_size);
7835
7836   if (info->fp_size)
7837     fprintf (stderr, "\tfp_size             = %5d\n", info->fp_size);
7838
7839   if (info->altivec_size)
7840     fprintf (stderr, "\taltivec_size        = %5d\n", info->altivec_size);
7841
7842   if (info->vrsave_size)
7843     fprintf (stderr, "\tvrsave_size         = %5d\n", info->vrsave_size);
7844
7845   if (info->altivec_padding_size)
7846     fprintf (stderr, "\taltivec_padding_size= %5d\n",
7847              info->altivec_padding_size);
7848
7849   if (info->lr_size)
7850     fprintf (stderr, "\tlr_size             = %5d\n", info->lr_size);
7851
7852   if (info->cr_size)
7853     fprintf (stderr, "\tcr_size             = %5d\n", info->cr_size);
7854
7855   if (info->toc_size)
7856     fprintf (stderr, "\ttoc_size            = %5d\n", info->toc_size);
7857
7858   if (info->save_size)
7859     fprintf (stderr, "\tsave_size           = %5d\n", info->save_size);
7860
7861   if (info->reg_size != 4)
7862     fprintf (stderr, "\treg_size            = %5d\n", info->reg_size);
7863
7864   fprintf (stderr, "\n");
7865 }
7866
7867 rtx
7868 rs6000_return_addr (count, frame)
7869      int count;
7870      rtx frame;
7871 {
7872   /* Currently we don't optimize very well between prolog and body
7873      code and for PIC code the code can be actually quite bad, so
7874      don't try to be too clever here.  */
7875   if (count != 0
7876       || flag_pic != 0
7877       || DEFAULT_ABI == ABI_AIX
7878       || DEFAULT_ABI == ABI_AIX_NODESC)
7879     {
7880       cfun->machine->ra_needs_full_frame = 1;
7881
7882       return
7883         gen_rtx_MEM
7884           (Pmode,
7885            memory_address
7886            (Pmode,
7887             plus_constant (copy_to_reg
7888                            (gen_rtx_MEM (Pmode,
7889                                          memory_address (Pmode, frame))),
7890                            RETURN_ADDRESS_OFFSET)));
7891     }
7892
7893   return get_hard_reg_initial_val (Pmode, LINK_REGISTER_REGNUM);
7894 }
7895
7896 static int
7897 rs6000_ra_ever_killed ()
7898 {
7899   rtx top;
7900
7901 #ifdef ASM_OUTPUT_MI_THUNK
7902   if (current_function_is_thunk)
7903     return 0;
7904 #endif
7905   if (!has_hard_reg_initial_val (Pmode, LINK_REGISTER_REGNUM)
7906       || cfun->machine->ra_needs_full_frame)
7907     return regs_ever_live[LINK_REGISTER_REGNUM];
7908
7909   push_topmost_sequence ();
7910   top = get_insns ();
7911   pop_topmost_sequence ();
7912
7913   return reg_set_between_p (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM), 
7914                             top, NULL_RTX);
7915 }
7916 \f
7917 /* Add a REG_MAYBE_DEAD note to the insn.  */
7918 static void
7919 rs6000_maybe_dead (insn)
7920      rtx insn;
7921 {
7922   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
7923                                         const0_rtx,
7924                                         REG_NOTES (insn));
7925 }
7926
7927 /* Emit instructions needed to load the TOC register.
7928    This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
7929    a constant pool; or for SVR4 -fpic.  */
7930
7931 void
7932 rs6000_emit_load_toc_table (fromprolog)
7933      int fromprolog;
7934 {
7935   rtx dest;
7936   dest = gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM);
7937
7938   if (TARGET_ELF && DEFAULT_ABI != ABI_AIX)
7939     {
7940       if (DEFAULT_ABI == ABI_V4 && flag_pic == 1)
7941         {
7942           rtx temp = (fromprolog 
7943                       ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
7944                       : gen_reg_rtx (Pmode));
7945           rs6000_maybe_dead (emit_insn (gen_load_toc_v4_pic_si (temp)));
7946           rs6000_maybe_dead (emit_move_insn (dest, temp));
7947         }
7948       else if (flag_pic == 2)
7949         {
7950           char buf[30];
7951           rtx tempLR = (fromprolog 
7952                         ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
7953                         : gen_reg_rtx (Pmode));
7954           rtx temp0 = (fromprolog
7955                         ? gen_rtx_REG (Pmode, 0)
7956                         : gen_reg_rtx (Pmode));
7957           rtx symF;
7958
7959           /* possibly create the toc section */
7960           if (! toc_initialized)
7961             {
7962               toc_section ();
7963               function_section (current_function_decl);
7964             }
7965   
7966           if (fromprolog)
7967             {
7968               rtx symL;
7969           
7970               ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
7971               symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
7972
7973               ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
7974               symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
7975
7976               rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_1 (tempLR, 
7977                                                                    symF)));
7978               rs6000_maybe_dead (emit_move_insn (dest, tempLR));
7979               rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest,
7980                                                                    symL,
7981                                                                    symF)));
7982             }
7983           else
7984             {
7985               rtx tocsym;
7986               static int reload_toc_labelno = 0;
7987
7988               tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
7989
7990               ASM_GENERATE_INTERNAL_LABEL (buf, "LCG", reload_toc_labelno++);
7991               symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
7992
7993               rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_1b (tempLR, 
7994                                                                     symF, 
7995                                                                     tocsym)));
7996               rs6000_maybe_dead (emit_move_insn (dest, tempLR));
7997               rs6000_maybe_dead (emit_move_insn (temp0, 
7998                                                  gen_rtx_MEM (Pmode, dest)));
7999             }
8000           rs6000_maybe_dead (emit_insn (gen_addsi3 (dest, temp0, dest)));
8001         }
8002       else if (flag_pic == 0 && TARGET_MINIMAL_TOC)
8003         {
8004           /* This is for AIX code running in non-PIC ELF.  */
8005           char buf[30];
8006           rtx realsym;
8007           ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
8008           realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
8009           
8010           rs6000_maybe_dead (emit_insn (gen_elf_high (dest, realsym)));
8011           rs6000_maybe_dead (emit_insn (gen_elf_low (dest, dest, realsym)));
8012         }
8013       else
8014         abort ();
8015     }
8016   else
8017     {
8018       if (TARGET_32BIT)
8019         rs6000_maybe_dead (emit_insn (gen_load_toc_aix_si (dest)));
8020       else
8021         rs6000_maybe_dead (emit_insn (gen_load_toc_aix_di (dest)));
8022     }
8023 }
8024
8025 int   
8026 get_TOC_alias_set ()
8027 {
8028     static int set = -1;
8029     if (set == -1)
8030       set = new_alias_set ();
8031     return set;
8032 }   
8033
8034 /* This retuns nonzero if the current function uses the TOC.  This is
8035    determined by the presence of (unspec ... 7), which is generated by
8036    the various load_toc_* patterns.  */
8037
8038 int
8039 uses_TOC () 
8040 {
8041     rtx insn;
8042
8043     for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
8044       if (INSN_P (insn))
8045         {
8046           rtx pat = PATTERN (insn);
8047           int i;
8048
8049           if (GET_CODE (pat) == PARALLEL) 
8050             for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
8051               if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == UNSPEC 
8052                  && XINT (XVECEXP (PATTERN (insn), 0, i), 1) == 7)
8053                   return 1;
8054         }
8055     return 0;
8056 }
8057
8058 rtx
8059 create_TOC_reference (symbol) 
8060     rtx symbol;
8061 {
8062   return gen_rtx_PLUS (Pmode, 
8063            gen_rtx_REG (Pmode, TOC_REGISTER),
8064              gen_rtx_CONST (Pmode, 
8065                gen_rtx_MINUS (Pmode, symbol, 
8066                  gen_rtx_SYMBOL_REF (Pmode, toc_label_name))));
8067 }
8068
8069 #if TARGET_AIX
8070 /* __throw will restore its own return address to be the same as the
8071    return address of the function that the throw is being made to.
8072    This is unfortunate, because we want to check the original
8073    return address to see if we need to restore the TOC.
8074    So we have to squirrel it away here.  
8075    This is used only in compiling __throw and __rethrow. 
8076
8077    Most of this code should be removed by CSE.  */
8078 static rtx insn_after_throw;
8079
8080 /* This does the saving...  */
8081 void
8082 rs6000_aix_emit_builtin_unwind_init ()
8083 {
8084   rtx mem;
8085   rtx stack_top = gen_reg_rtx (Pmode);
8086   rtx opcode_addr = gen_reg_rtx (Pmode);
8087
8088   insn_after_throw = gen_reg_rtx (SImode);
8089
8090   mem = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
8091   emit_move_insn (stack_top, mem);
8092
8093   mem = gen_rtx_MEM (Pmode, 
8094                      gen_rtx_PLUS (Pmode, stack_top, 
8095                                    GEN_INT (2 * GET_MODE_SIZE (Pmode))));
8096   emit_move_insn (opcode_addr, mem);
8097   emit_move_insn (insn_after_throw, gen_rtx_MEM (SImode, opcode_addr));
8098 }
8099
8100 /* Emit insns to _restore_ the TOC register, at runtime (specifically
8101    in _eh.o).  Only used on AIX.
8102
8103    The idea is that on AIX, function calls look like this:
8104         bl  somefunction-trampoline
8105         lwz r2,20(sp)
8106
8107    and later,
8108         somefunction-trampoline:
8109         stw r2,20(sp)
8110          ... load function address in the count register ...
8111         bctr
8112    or like this, if the linker determines that this is not a cross-module call
8113    and so the TOC need not be restored:
8114         bl  somefunction
8115         nop
8116    or like this, if the compiler could determine that this is not a
8117    cross-module call:
8118         bl  somefunction
8119    now, the tricky bit here is that register 2 is saved and restored
8120    by the _linker_, so we can't readily generate debugging information
8121    for it.  So we need to go back up the call chain looking at the
8122    insns at return addresses to see which calls saved the TOC register
8123    and so see where it gets restored from.
8124
8125    Oh, and all this gets done in RTL inside the eh_epilogue pattern,
8126    just before the actual epilogue.
8127
8128    On the bright side, this incurs no space or time overhead unless an
8129    exception is thrown, except for the extra code in libgcc.a.  
8130
8131    The parameter STACKSIZE is a register containing (at runtime)
8132    the amount to be popped off the stack in addition to the stack frame
8133    of this routine (which will be __throw or __rethrow, and so is
8134    guaranteed to have a stack frame).  */
8135
8136 void
8137 rs6000_emit_eh_toc_restore (stacksize)
8138      rtx stacksize;
8139 {
8140   rtx top_of_stack;
8141   rtx bottom_of_stack = gen_reg_rtx (Pmode);
8142   rtx tocompare = gen_reg_rtx (SImode);
8143   rtx opcode = gen_reg_rtx (SImode);
8144   rtx opcode_addr = gen_reg_rtx (Pmode);
8145   rtx mem;
8146   rtx loop_start = gen_label_rtx ();
8147   rtx no_toc_restore_needed = gen_label_rtx ();
8148   rtx loop_exit = gen_label_rtx ();
8149   
8150   mem = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
8151   set_mem_alias_set (mem, rs6000_sr_alias_set);
8152   emit_move_insn (bottom_of_stack, mem);
8153
8154   top_of_stack = expand_binop (Pmode, add_optab, 
8155                                bottom_of_stack, stacksize,
8156                                NULL_RTX, 1, OPTAB_WIDEN);
8157
8158   emit_move_insn (tocompare, 
8159                   GEN_INT (trunc_int_for_mode (TARGET_32BIT 
8160                                                ? 0x80410014 
8161                                                : 0xE8410028, SImode)));
8162
8163   if (insn_after_throw == NULL_RTX)
8164     abort ();
8165   emit_move_insn (opcode, insn_after_throw);
8166   
8167   emit_note (NULL, NOTE_INSN_LOOP_BEG);
8168   emit_label (loop_start);
8169   
8170   do_compare_rtx_and_jump (opcode, tocompare, NE, 1,
8171                            SImode, NULL_RTX, NULL_RTX,
8172                            no_toc_restore_needed);
8173   
8174   mem = gen_rtx_MEM (Pmode, 
8175                      gen_rtx_PLUS (Pmode, bottom_of_stack, 
8176                                    GEN_INT (5 * GET_MODE_SIZE (Pmode))));
8177   emit_move_insn (gen_rtx_REG (Pmode, 2), mem);
8178
8179   emit_label (no_toc_restore_needed);
8180   do_compare_rtx_and_jump (top_of_stack, bottom_of_stack, EQ, 1,
8181                            Pmode, NULL_RTX, NULL_RTX,
8182                            loop_exit);
8183
8184   mem = gen_rtx_MEM (Pmode, bottom_of_stack);
8185   set_mem_alias_set (mem, rs6000_sr_alias_set);
8186   emit_move_insn (bottom_of_stack, mem);
8187   
8188   mem = gen_rtx_MEM (Pmode, 
8189                      gen_rtx_PLUS (Pmode, bottom_of_stack, 
8190                                    GEN_INT (2 * GET_MODE_SIZE (Pmode))));
8191   emit_move_insn (opcode_addr, mem);
8192   emit_move_insn (opcode, gen_rtx_MEM (SImode, opcode_addr));
8193
8194   emit_note (NULL, NOTE_INSN_LOOP_CONT);
8195   emit_jump (loop_start);
8196   emit_note (NULL, NOTE_INSN_LOOP_END);
8197   emit_label (loop_exit);
8198 }
8199 #endif /* TARGET_AIX */
8200 \f
8201 /* This ties together stack memory (MEM with an alias set of
8202    rs6000_sr_alias_set) and the change to the stack pointer.  */
8203
8204 static void
8205 rs6000_emit_stack_tie ()
8206 {
8207   rtx mem = gen_rtx_MEM (BLKmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM));
8208
8209   set_mem_alias_set (mem, rs6000_sr_alias_set);
8210   emit_insn (gen_stack_tie (mem));
8211 }
8212
8213 /* Emit the correct code for allocating stack space, as insns.
8214    If COPY_R12, make sure a copy of the old frame is left in r12.
8215    The generated code may use hard register 0 as a temporary.  */
8216
8217 static void
8218 rs6000_emit_allocate_stack (size, copy_r12)
8219      HOST_WIDE_INT size;
8220      int copy_r12;
8221 {
8222   rtx insn;
8223   rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
8224   rtx tmp_reg = gen_rtx_REG (Pmode, 0);
8225   rtx todec = GEN_INT (-size);
8226
8227   if (current_function_limit_stack)
8228     {
8229       if (REG_P (stack_limit_rtx)
8230           && REGNO (stack_limit_rtx) > 1 
8231           && REGNO (stack_limit_rtx) <= 31)
8232         {
8233           emit_insn (Pmode == SImode
8234                      ? gen_addsi3 (tmp_reg,
8235                                    stack_limit_rtx,
8236                                    GEN_INT (size))
8237                      : gen_adddi3 (tmp_reg,
8238                                    stack_limit_rtx,
8239                                    GEN_INT (size)));
8240           
8241           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
8242                                     const0_rtx));
8243         }
8244       else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
8245                && TARGET_32BIT
8246                && DEFAULT_ABI == ABI_V4)
8247         {
8248           rtx toload = gen_rtx_CONST (VOIDmode,
8249                                       gen_rtx_PLUS (Pmode, 
8250                                                     stack_limit_rtx, 
8251                                                     GEN_INT (size)));
8252           
8253           emit_insn (gen_elf_high (tmp_reg, toload));
8254           emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
8255           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
8256                                     const0_rtx));
8257         }
8258       else
8259         warning ("stack limit expression is not supported");
8260     }
8261
8262   if (copy_r12 || ! TARGET_UPDATE)
8263     emit_move_insn (gen_rtx_REG (Pmode, 12), stack_reg);
8264
8265   if (TARGET_UPDATE)
8266     {
8267       if (size > 32767)
8268         {
8269           /* Need a note here so that try_split doesn't get confused.  */
8270           if (get_last_insn() == NULL_RTX)
8271             emit_note (0, NOTE_INSN_DELETED);
8272           insn = emit_move_insn (tmp_reg, todec);
8273           try_split (PATTERN (insn), insn, 0);
8274           todec = tmp_reg;
8275         }
8276       
8277       if (Pmode == SImode)
8278         insn = emit_insn (gen_movsi_update (stack_reg, stack_reg, 
8279                                             todec, stack_reg));
8280       else
8281         insn = emit_insn (gen_movdi_update (stack_reg, stack_reg, 
8282                                             todec, stack_reg));
8283     }
8284   else
8285     {
8286       if (Pmode == SImode)
8287         insn = emit_insn (gen_addsi3 (stack_reg, stack_reg, todec));
8288       else
8289         insn = emit_insn (gen_adddi3 (stack_reg, stack_reg, todec));
8290       emit_move_insn (gen_rtx_MEM (Pmode, stack_reg),
8291                       gen_rtx_REG (Pmode, 12));
8292     }
8293   
8294   RTX_FRAME_RELATED_P (insn) = 1;
8295   REG_NOTES (insn) = 
8296     gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
8297                        gen_rtx_SET (VOIDmode, stack_reg, 
8298                                     gen_rtx_PLUS (Pmode, stack_reg,
8299                                                   GEN_INT (-size))),
8300                        REG_NOTES (insn));
8301 }
8302
8303 /* Add a RTX_FRAME_RELATED note so that dwarf2out_frame_debug_expr
8304    knows that:
8305
8306      (mem (plus (blah) (regXX)))
8307
8308    is really:
8309
8310      (mem (plus (blah) (const VALUE_OF_REGXX))).  */
8311
8312 static void
8313 altivec_frame_fixup (insn, reg, val)
8314      rtx insn, reg;
8315      HOST_WIDE_INT val;
8316 {
8317   rtx real;
8318
8319   real = copy_rtx (PATTERN (insn));
8320
8321   real = replace_rtx (real, reg, GEN_INT (val));
8322
8323   RTX_FRAME_RELATED_P (insn) = 1;
8324   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
8325                                         real,
8326                                         REG_NOTES (insn));
8327 }
8328
8329 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
8330    with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
8331    is not NULL.  It would be nice if dwarf2out_frame_debug_expr could
8332    deduce these equivalences by itself so it wasn't necessary to hold
8333    its hand so much.  */
8334
8335 static void
8336 rs6000_frame_related (insn, reg, val, reg2, rreg)
8337      rtx insn;
8338      rtx reg;
8339      HOST_WIDE_INT val;
8340      rtx reg2;
8341      rtx rreg;
8342 {
8343   rtx real, temp;
8344
8345   real = copy_rtx (PATTERN (insn));
8346
8347   real = replace_rtx (real, reg, 
8348                       gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
8349                                                         STACK_POINTER_REGNUM),
8350                                     GEN_INT (val)));
8351   
8352   /* We expect that 'real' is either a SET or a PARALLEL containing
8353      SETs (and possibly other stuff).  In a PARALLEL, all the SETs
8354      are important so they all have to be marked RTX_FRAME_RELATED_P.  */
8355
8356   if (GET_CODE (real) == SET)
8357     {
8358       rtx set = real;
8359       
8360       temp = simplify_rtx (SET_SRC (set));
8361       if (temp)
8362         SET_SRC (set) = temp;
8363       temp = simplify_rtx (SET_DEST (set));
8364       if (temp)
8365         SET_DEST (set) = temp;
8366       if (GET_CODE (SET_DEST (set)) == MEM)
8367         {
8368           temp = simplify_rtx (XEXP (SET_DEST (set), 0));
8369           if (temp)
8370             XEXP (SET_DEST (set), 0) = temp;
8371         }
8372     }
8373   else if (GET_CODE (real) == PARALLEL)
8374     {
8375       int i;
8376       for (i = 0; i < XVECLEN (real, 0); i++)
8377         if (GET_CODE (XVECEXP (real, 0, i)) == SET)
8378           {
8379             rtx set = XVECEXP (real, 0, i);
8380             
8381             temp = simplify_rtx (SET_SRC (set));
8382             if (temp)
8383               SET_SRC (set) = temp;
8384             temp = simplify_rtx (SET_DEST (set));
8385             if (temp)
8386               SET_DEST (set) = temp;
8387             if (GET_CODE (SET_DEST (set)) == MEM)
8388               {
8389                 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
8390                 if (temp)
8391                   XEXP (SET_DEST (set), 0) = temp;
8392               }
8393             RTX_FRAME_RELATED_P (set) = 1;
8394           }
8395     }
8396   else
8397     abort ();
8398   
8399   if (reg2 != NULL_RTX)
8400     real = replace_rtx (real, reg2, rreg);
8401   
8402   RTX_FRAME_RELATED_P (insn) = 1;
8403   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
8404                                         real,
8405                                         REG_NOTES (insn));
8406 }
8407
8408 /* Returns an insn that has a vrsave set operation with the
8409    appropriate CLOBBERs.  */
8410
8411 static rtx
8412 generate_set_vrsave (reg, info, epiloguep)
8413      rtx reg;
8414      rs6000_stack_t *info;
8415      int epiloguep;
8416 {
8417   int nclobs, i;
8418   rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
8419   rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
8420
8421   clobs[0]
8422     = gen_rtx_SET (VOIDmode,
8423                    vrsave,
8424                    gen_rtx_UNSPEC_VOLATILE (SImode,
8425                                             gen_rtvec (2, reg, vrsave),
8426                                             30));
8427
8428   nclobs = 1;
8429
8430   /* We need to clobber the registers in the mask so the scheduler
8431      does not move sets to VRSAVE before sets of AltiVec registers.
8432
8433      However, if the function receives nonlocal gotos, reload will set
8434      all call saved registers live.  We will end up with:
8435
8436         (set (reg 999) (mem))
8437         (parallel [ (set (reg vrsave) (unspec blah))
8438                     (clobber (reg 999))])
8439
8440      The clobber will cause the store into reg 999 to be dead, and
8441      flow will attempt to delete an epilogue insn.  In this case, we
8442      need an unspec use/set of the register.  */
8443
8444   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
8445     if (info->vrsave_mask != 0 && ALTIVEC_REG_BIT (i) != 0)
8446       {
8447         if (!epiloguep || call_used_regs [i])
8448           clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
8449                                              gen_rtx_REG (V4SImode, i));
8450         else
8451           {
8452             rtx reg = gen_rtx_REG (V4SImode, i);
8453
8454             clobs[nclobs++]
8455               = gen_rtx_SET (VOIDmode,
8456                              reg,
8457                              gen_rtx_UNSPEC (V4SImode,
8458                                              gen_rtvec (1, reg), 27));
8459           }
8460       }
8461
8462   insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
8463
8464   for (i = 0; i < nclobs; ++i)
8465     XVECEXP (insn, 0, i) = clobs[i];
8466
8467   return insn;
8468 }
8469
8470 /* Emit function prologue as insns.  */
8471
8472 void
8473 rs6000_emit_prologue ()
8474 {
8475   rs6000_stack_t *info = rs6000_stack_info ();
8476   enum machine_mode reg_mode = TARGET_POWERPC64 ? DImode : SImode;
8477   int reg_size = TARGET_POWERPC64 ? 8 : 4;
8478   rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
8479   rtx frame_ptr_rtx = gen_rtx_REG (Pmode, 12);
8480   rtx frame_reg_rtx = sp_reg_rtx;
8481   rtx cr_save_rtx = NULL;
8482   rtx insn;
8483   int saving_FPRs_inline;
8484   int using_store_multiple;
8485   HOST_WIDE_INT sp_offset = 0;
8486   
8487   using_store_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
8488                           && info->first_gp_reg_save < 31);
8489   saving_FPRs_inline = (info->first_fp_reg_save == 64
8490                         || FP_SAVE_INLINE (info->first_fp_reg_save));
8491
8492   /* For V.4, update stack before we do any saving and set back pointer.  */
8493   if (info->push_p && DEFAULT_ABI == ABI_V4)
8494     {
8495       if (info->total_size < 32767)
8496         sp_offset = info->total_size;
8497       else
8498         frame_reg_rtx = frame_ptr_rtx;
8499       rs6000_emit_allocate_stack (info->total_size, 
8500                                   (frame_reg_rtx != sp_reg_rtx
8501                                    && (info->cr_save_p
8502                                        || info->lr_save_p
8503                                        || info->first_fp_reg_save < 64
8504                                        || info->first_gp_reg_save < 32
8505                                        )));
8506       if (frame_reg_rtx != sp_reg_rtx)
8507         rs6000_emit_stack_tie ();
8508     }
8509
8510   /* Save AltiVec registers if needed.  */
8511   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
8512     {
8513       int i;
8514
8515       /* There should be a non inline version of this, for when we
8516          are saving lots of vector registers.  */
8517       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
8518         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
8519           {
8520             rtx areg, savereg, mem;
8521             int offset;
8522
8523             offset = info->altivec_save_offset + sp_offset
8524               + 16 * (i - info->first_altivec_reg_save);
8525
8526             savereg = gen_rtx_REG (V4SImode, i);
8527
8528             areg = gen_rtx_REG (Pmode, 0);
8529             emit_move_insn (areg, GEN_INT (offset));
8530
8531             /* AltiVec addressing mode is [reg+reg].  */
8532             mem = gen_rtx_MEM (V4SImode,
8533                                gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
8534                                
8535             set_mem_alias_set (mem, rs6000_sr_alias_set);
8536
8537             insn = emit_move_insn (mem, savereg);
8538
8539             altivec_frame_fixup (insn, areg, offset);
8540           }
8541     }
8542
8543   /* VRSAVE is a bit vector representing which AltiVec registers
8544      are used.  The OS uses this to determine which vector
8545      registers to save on a context switch.  We need to save
8546      VRSAVE on the stack frame, add whatever AltiVec registers we
8547      used in this function, and do the corresponding magic in the
8548      epilogue.  */
8549
8550   if (TARGET_ALTIVEC && info->vrsave_mask != 0)
8551     {
8552       rtx reg, mem, vrsave;
8553       int offset;
8554
8555       /* Get VRSAVE onto a GPR.  */
8556       reg = gen_rtx_REG (SImode, 12);
8557       vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
8558       emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
8559
8560       /* Save VRSAVE.  */
8561       offset = info->vrsave_save_offset + sp_offset;
8562       mem
8563         = gen_rtx_MEM (SImode,
8564                        gen_rtx_PLUS (Pmode, frame_reg_rtx, GEN_INT (offset)));
8565       set_mem_alias_set (mem, rs6000_sr_alias_set);
8566       insn = emit_move_insn (mem, reg);
8567
8568       /* Include the registers in the mask.  */
8569       emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
8570
8571       insn = emit_insn (generate_set_vrsave (reg, info, 0));
8572     }
8573
8574   /* If we use the link register, get it into r0.  */
8575   if (info->lr_save_p)
8576     emit_move_insn (gen_rtx_REG (Pmode, 0),
8577                     gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
8578
8579   /* If we need to save CR, put it into r12.  */
8580   if (info->cr_save_p && frame_reg_rtx != frame_ptr_rtx)
8581     {
8582       cr_save_rtx = gen_rtx_REG (SImode, 12);
8583       emit_insn (gen_movesi_from_cr (cr_save_rtx));
8584     }
8585
8586   /* Do any required saving of fpr's.  If only one or two to save, do
8587      it ourselves.  Otherwise, call function.  */
8588   if (saving_FPRs_inline)
8589     {
8590       int i;
8591       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
8592         if ((regs_ever_live[info->first_fp_reg_save+i] 
8593              && ! call_used_regs[info->first_fp_reg_save+i]))
8594           {
8595             rtx addr, reg, mem;
8596             reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
8597             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
8598                                  GEN_INT (info->fp_save_offset 
8599                                           + sp_offset 
8600                                           + 8 * i));
8601             mem = gen_rtx_MEM (DFmode, addr);
8602             set_mem_alias_set (mem, rs6000_sr_alias_set);
8603
8604             insn = emit_move_insn (mem, reg);
8605             rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
8606                                   NULL_RTX, NULL_RTX);
8607           }
8608     }
8609   else if (info->first_fp_reg_save != 64)
8610     {
8611       int i;
8612       char rname[30];
8613       const char *alloc_rname;
8614       rtvec p;
8615       p = rtvec_alloc (2 + 64 - info->first_fp_reg_save);
8616       
8617       RTVEC_ELT (p, 0) = gen_rtx_CLOBBER (VOIDmode, 
8618                                           gen_rtx_REG (Pmode, 
8619                                                        LINK_REGISTER_REGNUM));
8620       sprintf (rname, "%s%d%s", SAVE_FP_PREFIX,
8621                info->first_fp_reg_save - 32, SAVE_FP_SUFFIX);
8622       alloc_rname = ggc_strdup (rname);
8623       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode,
8624                                       gen_rtx_SYMBOL_REF (Pmode,
8625                                                           alloc_rname));
8626       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
8627         {
8628           rtx addr, reg, mem;
8629           reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
8630           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
8631                                GEN_INT (info->fp_save_offset 
8632                                         + sp_offset + 8*i));
8633           mem = gen_rtx_MEM (DFmode, addr);
8634           set_mem_alias_set (mem, rs6000_sr_alias_set);
8635
8636           RTVEC_ELT (p, i + 2) = gen_rtx_SET (VOIDmode, mem, reg);
8637         }
8638       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
8639       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
8640                             NULL_RTX, NULL_RTX);
8641     }
8642
8643   /* Save GPRs.  This is done as a PARALLEL if we are using
8644      the store-multiple instructions.  */
8645   if (using_store_multiple)
8646     {
8647       rtvec p, dwarfp;
8648       int i;
8649       p = rtvec_alloc (32 - info->first_gp_reg_save);
8650       dwarfp = rtvec_alloc (32 - info->first_gp_reg_save);
8651       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
8652         {
8653           rtx addr, reg, mem;
8654           reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
8655           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
8656                                GEN_INT (info->gp_save_offset 
8657                                         + sp_offset 
8658                                         + reg_size * i));
8659           mem = gen_rtx_MEM (reg_mode, addr);
8660           set_mem_alias_set (mem, rs6000_sr_alias_set);
8661
8662           RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, reg);
8663         }
8664       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
8665       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
8666                             NULL_RTX, NULL_RTX);
8667     }
8668   else
8669     {
8670       int i;
8671       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
8672         if ((regs_ever_live[info->first_gp_reg_save+i] 
8673              && ! call_used_regs[info->first_gp_reg_save+i])
8674             || (i+info->first_gp_reg_save == PIC_OFFSET_TABLE_REGNUM
8675                 && ((DEFAULT_ABI == ABI_V4 && flag_pic == 1)
8676                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
8677           {
8678             rtx addr, reg, mem;
8679             reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
8680             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
8681                                  GEN_INT (info->gp_save_offset 
8682                                           + sp_offset 
8683                                           + reg_size * i));
8684             mem = gen_rtx_MEM (reg_mode, addr);
8685             set_mem_alias_set (mem, rs6000_sr_alias_set);
8686
8687             insn = emit_move_insn (mem, reg);
8688             rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
8689                                   NULL_RTX, NULL_RTX);
8690           }
8691     }
8692
8693   /* ??? There's no need to emit actual instructions here, but it's the
8694      easiest way to get the frame unwind information emitted.  */
8695   if (current_function_calls_eh_return)
8696     {
8697       unsigned int i, regno;
8698
8699       for (i = 0; ; ++i)
8700         {
8701           rtx addr, reg, mem;
8702
8703           regno = EH_RETURN_DATA_REGNO (i);
8704           if (regno == INVALID_REGNUM)
8705             break;
8706
8707           reg = gen_rtx_REG (reg_mode, regno);
8708           addr = plus_constant (frame_reg_rtx,
8709                                 info->ehrd_offset + sp_offset
8710                                 + reg_size * (int) i);
8711           mem = gen_rtx_MEM (reg_mode, addr);
8712           set_mem_alias_set (mem, rs6000_sr_alias_set);
8713
8714           insn = emit_move_insn (mem, reg);
8715           rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
8716                                 NULL_RTX, NULL_RTX);
8717         }
8718     }
8719
8720   /* Save lr if we used it.  */
8721   if (info->lr_save_p)
8722     {
8723       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
8724                                GEN_INT (info->lr_save_offset + sp_offset));
8725       rtx reg = gen_rtx_REG (Pmode, 0);
8726       rtx mem = gen_rtx_MEM (Pmode, addr);
8727       /* This should not be of rs6000_sr_alias_set, because of
8728          __builtin_return_address.  */
8729       
8730       insn = emit_move_insn (mem, reg);
8731       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
8732                             reg, gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
8733     }
8734
8735   /* Save CR if we use any that must be preserved.  */
8736   if (info->cr_save_p)
8737     {
8738       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
8739                                GEN_INT (info->cr_save_offset + sp_offset));
8740       rtx mem = gen_rtx_MEM (SImode, addr);
8741
8742       set_mem_alias_set (mem, rs6000_sr_alias_set);
8743
8744       /* If r12 was used to hold the original sp, copy cr into r0 now
8745          that it's free.  */
8746       if (REGNO (frame_reg_rtx) == 12)
8747         {
8748           cr_save_rtx = gen_rtx_REG (SImode, 0);
8749           emit_insn (gen_movesi_from_cr (cr_save_rtx));
8750         }
8751       insn = emit_move_insn (mem, cr_save_rtx);
8752
8753       /* Now, there's no way that dwarf2out_frame_debug_expr is going
8754          to understand '(unspec:SI [(reg:CC 68) ...] 19)'.  But that's
8755          OK.  All we have to do is specify that _one_ condition code
8756          register is saved in this stack slot.  The thrower's epilogue
8757          will then restore all the call-saved registers.
8758          We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux.  */
8759       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
8760                             cr_save_rtx, gen_rtx_REG (SImode, CR2_REGNO));
8761     }
8762
8763   /* Update stack and set back pointer unless this is V.4, 
8764      for which it was done previously.  */
8765   if (info->push_p && DEFAULT_ABI != ABI_V4)
8766     rs6000_emit_allocate_stack (info->total_size, FALSE);
8767
8768   /* Set frame pointer, if needed.  */
8769   if (frame_pointer_needed)
8770     {
8771       insn = emit_move_insn (gen_rtx_REG (reg_mode, FRAME_POINTER_REGNUM), 
8772                              sp_reg_rtx);
8773       RTX_FRAME_RELATED_P (insn) = 1;
8774     }
8775
8776   /* If we are using PIC_OFFSET_TABLE_REGNUM, we need to set it up.  */
8777   if ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
8778       || (DEFAULT_ABI == ABI_V4 && flag_pic == 1
8779           && regs_ever_live[PIC_OFFSET_TABLE_REGNUM]))
8780   {
8781     /* If emit_load_toc_table will use the link register, we need to save
8782        it.  We use R11 for this purpose because emit_load_toc_table
8783        can use register 0.  This allows us to use a plain 'blr' to return
8784        from the procedure more often.  */
8785     int save_LR_around_toc_setup = (TARGET_ELF && flag_pic != 0
8786                                     && ! info->lr_save_p
8787                                     && EXIT_BLOCK_PTR->pred != NULL);
8788     if (save_LR_around_toc_setup)
8789       emit_move_insn (gen_rtx_REG (Pmode, 11), 
8790                       gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
8791     
8792     rs6000_emit_load_toc_table (TRUE);
8793
8794     if (save_LR_around_toc_setup)
8795       emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM), 
8796                       gen_rtx_REG (Pmode, 11));
8797   }
8798
8799   if (DEFAULT_ABI == ABI_DARWIN
8800       && flag_pic && current_function_uses_pic_offset_table)
8801     {
8802       rtx dest = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
8803
8804       rs6000_maybe_dead (emit_insn (gen_load_macho_picbase (dest)));
8805
8806       rs6000_maybe_dead (
8807         emit_move_insn (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM),
8808                         gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)));
8809     }
8810 }
8811
8812 /* Write function prologue.  */
8813
8814 static void
8815 rs6000_output_function_prologue (file, size)
8816      FILE *file;
8817      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
8818 {
8819   rs6000_stack_t *info = rs6000_stack_info ();
8820
8821   if (TARGET_DEBUG_STACK)
8822     debug_stack_info (info);
8823
8824   /* Write .extern for any function we will call to save and restore
8825      fp values.  */
8826   if (info->first_fp_reg_save < 64
8827       && !FP_SAVE_INLINE (info->first_fp_reg_save))
8828     fprintf (file, "\t.extern %s%d%s\n\t.extern %s%d%s\n",
8829              SAVE_FP_PREFIX, info->first_fp_reg_save - 32, SAVE_FP_SUFFIX,
8830              RESTORE_FP_PREFIX, info->first_fp_reg_save - 32,
8831              RESTORE_FP_SUFFIX);
8832
8833   /* Write .extern for AIX common mode routines, if needed.  */
8834   if (! TARGET_POWER && ! TARGET_POWERPC && ! common_mode_defined)
8835     {
8836       fputs ("\t.extern __mulh\n", file);
8837       fputs ("\t.extern __mull\n", file);
8838       fputs ("\t.extern __divss\n", file);
8839       fputs ("\t.extern __divus\n", file);
8840       fputs ("\t.extern __quoss\n", file);
8841       fputs ("\t.extern __quous\n", file);
8842       common_mode_defined = 1;
8843     }
8844
8845   if (! HAVE_prologue)
8846     {
8847       start_sequence ();
8848       
8849       /* A NOTE_INSN_DELETED is supposed to be at the start and end of
8850          the "toplevel" insn chain.  */
8851       emit_note (0, NOTE_INSN_DELETED);
8852       rs6000_emit_prologue ();
8853       emit_note (0, NOTE_INSN_DELETED);
8854       
8855       if (TARGET_DEBUG_STACK)
8856         debug_rtx_list (get_insns (), 100);
8857       final (get_insns (), file, FALSE, FALSE);
8858       end_sequence ();
8859     }
8860
8861   rs6000_pic_labelno++;
8862 }
8863   
8864 /* Emit function epilogue as insns.
8865
8866    At present, dwarf2out_frame_debug_expr doesn't understand
8867    register restores, so we don't bother setting RTX_FRAME_RELATED_P
8868    anywhere in the epilogue.  Most of the insns below would in any case
8869    need special notes to explain where r11 is in relation to the stack.  */
8870
8871 void
8872 rs6000_emit_epilogue (sibcall)
8873      int sibcall;
8874 {
8875   rs6000_stack_t *info;
8876   int restoring_FPRs_inline;
8877   int using_load_multiple;
8878   int using_mfcr_multiple;
8879   int use_backchain_to_restore_sp;
8880   int sp_offset = 0;
8881   rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
8882   rtx frame_reg_rtx = sp_reg_rtx;
8883   enum machine_mode reg_mode = TARGET_POWERPC64 ? DImode : SImode;
8884   int reg_size = TARGET_POWERPC64 ? 8 : 4;
8885   int i;
8886
8887   info = rs6000_stack_info ();
8888   using_load_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
8889                          && info->first_gp_reg_save < 31);
8890   restoring_FPRs_inline = (sibcall
8891                            || current_function_calls_eh_return
8892                            || info->first_fp_reg_save == 64
8893                            || FP_SAVE_INLINE (info->first_fp_reg_save));
8894   use_backchain_to_restore_sp = (frame_pointer_needed 
8895                                  || current_function_calls_alloca
8896                                  || info->total_size > 32767);
8897   using_mfcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
8898                          || rs6000_cpu == PROCESSOR_PPC603
8899                          || rs6000_cpu == PROCESSOR_PPC750
8900                          || optimize_size);
8901
8902   /* If we have a frame pointer, a call to alloca,  or a large stack
8903      frame, restore the old stack pointer using the backchain.  Otherwise,
8904      we know what size to update it with.  */
8905   if (use_backchain_to_restore_sp)
8906     {
8907       /* Under V.4, don't reset the stack pointer until after we're done
8908          loading the saved registers.  */
8909       if (DEFAULT_ABI == ABI_V4)
8910         frame_reg_rtx = gen_rtx_REG (Pmode, 11);
8911
8912       emit_move_insn (frame_reg_rtx,
8913                       gen_rtx_MEM (Pmode, sp_reg_rtx));
8914       
8915     }
8916   else if (info->push_p)
8917     {
8918       if (DEFAULT_ABI == ABI_V4)
8919         sp_offset = info->total_size;
8920       else
8921         {
8922           emit_insn (TARGET_32BIT
8923                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
8924                                    GEN_INT (info->total_size))
8925                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
8926                                    GEN_INT (info->total_size)));
8927         }
8928     }
8929   
8930   /* Restore AltiVec registers if needed.  */
8931   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
8932     {
8933       int i;
8934
8935       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
8936         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
8937           {
8938             rtx addr, areg, mem;
8939
8940             areg = gen_rtx_REG (Pmode, 0);
8941             emit_move_insn
8942               (areg, GEN_INT (info->altivec_save_offset
8943                               + sp_offset
8944                               + 16 * (i - info->first_altivec_reg_save)));
8945
8946             /* AltiVec addressing mode is [reg+reg].  */
8947             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
8948             mem = gen_rtx_MEM (V4SImode, addr);
8949             set_mem_alias_set (mem, rs6000_sr_alias_set);
8950
8951             emit_move_insn (gen_rtx_REG (V4SImode, i), mem);
8952           }
8953     }
8954
8955   /* Restore VRSAVE if needed.  */
8956   if (TARGET_ALTIVEC_ABI && info->vrsave_mask != 0)
8957     {
8958       rtx addr, mem, reg;
8959
8960       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
8961                            GEN_INT (info->vrsave_save_offset + sp_offset));
8962       mem = gen_rtx_MEM (SImode, addr);
8963       set_mem_alias_set (mem, rs6000_sr_alias_set);
8964       reg = gen_rtx_REG (SImode, 12);
8965       emit_move_insn (reg, mem);
8966
8967       emit_insn (generate_set_vrsave (reg, info, 1));
8968     }
8969
8970   /* Get the old lr if we saved it.  */
8971   if (info->lr_save_p)
8972     {
8973       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
8974                                GEN_INT (info->lr_save_offset + sp_offset));
8975       rtx mem = gen_rtx_MEM (Pmode, addr);
8976
8977       set_mem_alias_set (mem, rs6000_sr_alias_set);
8978
8979       emit_move_insn (gen_rtx_REG (Pmode, 0), mem);
8980     }
8981   
8982   /* Get the old cr if we saved it.  */
8983   if (info->cr_save_p)
8984     {
8985       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
8986                                GEN_INT (info->cr_save_offset + sp_offset));
8987       rtx mem = gen_rtx_MEM (SImode, addr);
8988
8989       set_mem_alias_set (mem, rs6000_sr_alias_set);
8990
8991       emit_move_insn (gen_rtx_REG (SImode, 12), mem);
8992     }
8993   
8994   /* Set LR here to try to overlap restores below.  */
8995   if (info->lr_save_p)
8996     emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM),
8997                     gen_rtx_REG (Pmode, 0));
8998   
8999   /* Load exception handler data registers, if needed.  */
9000   if (current_function_calls_eh_return)
9001     {
9002       unsigned int i, regno;
9003
9004       for (i = 0; ; ++i)
9005         {
9006           rtx addr, mem;
9007
9008           regno = EH_RETURN_DATA_REGNO (i);
9009           if (regno == INVALID_REGNUM)
9010             break;
9011
9012           addr = plus_constant (frame_reg_rtx,
9013                                 info->ehrd_offset + sp_offset
9014                                 + reg_size * (int) i);
9015           mem = gen_rtx_MEM (reg_mode, addr);
9016           set_mem_alias_set (mem, rs6000_sr_alias_set);
9017
9018           emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
9019         }
9020     }
9021   
9022   /* Restore GPRs.  This is done as a PARALLEL if we are using
9023      the load-multiple instructions.  */
9024   if (using_load_multiple)
9025     {
9026       rtvec p;
9027       p = rtvec_alloc (32 - info->first_gp_reg_save);
9028       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
9029         {
9030           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
9031                                    GEN_INT (info->gp_save_offset 
9032                                             + sp_offset 
9033                                             + reg_size * i));
9034           rtx mem = gen_rtx_MEM (reg_mode, addr);
9035
9036           set_mem_alias_set (mem, rs6000_sr_alias_set);
9037
9038           RTVEC_ELT (p, i) = 
9039             gen_rtx_SET (VOIDmode,
9040                          gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
9041                          mem);
9042         }
9043       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
9044     }
9045   else
9046     for (i = 0; i < 32 - info->first_gp_reg_save; i++)
9047       if ((regs_ever_live[info->first_gp_reg_save+i] 
9048            && ! call_used_regs[info->first_gp_reg_save+i])
9049           || (i+info->first_gp_reg_save == PIC_OFFSET_TABLE_REGNUM
9050               && ((DEFAULT_ABI == ABI_V4 && flag_pic == 1)
9051                   || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
9052         {
9053           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
9054                                    GEN_INT (info->gp_save_offset 
9055                                             + sp_offset 
9056                                             + reg_size * i));
9057           rtx mem = gen_rtx_MEM (reg_mode, addr);
9058
9059           set_mem_alias_set (mem, rs6000_sr_alias_set);
9060
9061           emit_move_insn (gen_rtx_REG (reg_mode, 
9062                                        info->first_gp_reg_save + i),
9063                           mem);
9064         }
9065
9066   /* Restore fpr's if we need to do it without calling a function.  */
9067   if (restoring_FPRs_inline)
9068     for (i = 0; i < 64 - info->first_fp_reg_save; i++)
9069       if ((regs_ever_live[info->first_fp_reg_save+i] 
9070            && ! call_used_regs[info->first_fp_reg_save+i]))
9071         {
9072           rtx addr, mem;
9073           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
9074                                GEN_INT (info->fp_save_offset 
9075                                         + sp_offset 
9076                                         + 8 * i));
9077           mem = gen_rtx_MEM (DFmode, addr);
9078           set_mem_alias_set (mem, rs6000_sr_alias_set);
9079
9080           emit_move_insn (gen_rtx_REG (DFmode, 
9081                                        info->first_fp_reg_save + i),
9082                           mem);
9083         }
9084
9085   /* If we saved cr, restore it here.  Just those that were used.  */
9086   if (info->cr_save_p)
9087     {
9088       rtx r12_rtx = gen_rtx_REG (SImode, 12);
9089       int count = 0;
9090       
9091       if (using_mfcr_multiple)
9092         {
9093           for (i = 0; i < 8; i++)
9094             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
9095               count++;
9096           if (count == 0)
9097             abort ();
9098         }
9099
9100       if (using_mfcr_multiple && count > 1)
9101         {
9102           rtvec p;
9103           int ndx;
9104           
9105           p = rtvec_alloc (count);
9106
9107           ndx = 0;
9108           for (i = 0; i < 8; i++)
9109             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
9110               {
9111                 rtvec r = rtvec_alloc (2);
9112                 RTVEC_ELT (r, 0) = r12_rtx;
9113                 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
9114                 RTVEC_ELT (p, ndx) =
9115                   gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO+i), 
9116                                gen_rtx_UNSPEC (CCmode, r, 20));
9117                 ndx++;
9118               }
9119           emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
9120           if (ndx != count)
9121             abort ();
9122         }
9123       else
9124         for (i = 0; i < 8; i++)
9125           if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
9126             {
9127               emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode, 
9128                                                            CR0_REGNO+i),
9129                                               r12_rtx));
9130             }
9131     }
9132
9133   /* If this is V.4, unwind the stack pointer after all of the loads
9134      have been done.  We need to emit a block here so that sched
9135      doesn't decide to move the sp change before the register restores
9136      (which may not have any obvious dependency on the stack).  This
9137      doesn't hurt performance, because there is no scheduling that can
9138      be done after this point.  */
9139   if (DEFAULT_ABI == ABI_V4)
9140     {
9141       if (frame_reg_rtx != sp_reg_rtx)
9142           rs6000_emit_stack_tie ();
9143
9144       if (use_backchain_to_restore_sp)
9145         {
9146           emit_move_insn (sp_reg_rtx, frame_reg_rtx);
9147         }
9148       else if (sp_offset != 0)
9149         {
9150           emit_insn (Pmode == SImode
9151                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
9152                                    GEN_INT (sp_offset))
9153                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
9154                                    GEN_INT (sp_offset)));
9155         }
9156     }
9157
9158   if (current_function_calls_eh_return)
9159     {
9160       rtx sa = EH_RETURN_STACKADJ_RTX;
9161       emit_insn (Pmode == SImode
9162                  ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx, sa)
9163                  : gen_adddi3 (sp_reg_rtx, sp_reg_rtx, sa));
9164     }
9165
9166   if (!sibcall)
9167     {
9168       rtvec p;
9169       if (! restoring_FPRs_inline)
9170         p = rtvec_alloc (3 + 64 - info->first_fp_reg_save);
9171       else
9172         p = rtvec_alloc (2);
9173
9174       RTVEC_ELT (p, 0) = gen_rtx_RETURN (VOIDmode);
9175       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode, 
9176                                       gen_rtx_REG (Pmode, 
9177                                                    LINK_REGISTER_REGNUM));
9178
9179       /* If we have to restore more than two FP registers, branch to the
9180          restore function.  It will return to our caller.  */
9181       if (! restoring_FPRs_inline)
9182         {
9183           int i;
9184           char rname[30];
9185           const char *alloc_rname;
9186
9187           sprintf (rname, "%s%d%s", RESTORE_FP_PREFIX, 
9188                    info->first_fp_reg_save - 32, RESTORE_FP_SUFFIX);
9189           alloc_rname = ggc_strdup (rname);
9190           RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode,
9191                                           gen_rtx_SYMBOL_REF (Pmode,
9192                                                               alloc_rname));
9193
9194           for (i = 0; i < 64 - info->first_fp_reg_save; i++)
9195             {
9196               rtx addr, mem;
9197               addr = gen_rtx_PLUS (Pmode, sp_reg_rtx,
9198                                    GEN_INT (info->fp_save_offset + 8*i));
9199               mem = gen_rtx_MEM (DFmode, addr);
9200               set_mem_alias_set (mem, rs6000_sr_alias_set);
9201
9202               RTVEC_ELT (p, i+3) = 
9203                 gen_rtx_SET (VOIDmode,
9204                              gen_rtx_REG (DFmode, info->first_fp_reg_save + i),
9205                              mem);
9206             }
9207         }
9208       
9209       emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
9210     }
9211 }
9212
9213 /* Write function epilogue.  */
9214
9215 static void
9216 rs6000_output_function_epilogue (file, size)
9217      FILE *file;
9218      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
9219 {
9220   rs6000_stack_t *info = rs6000_stack_info ();
9221   int optional_tbtab = (optimize_size || TARGET_ELF) ? 0 : 1;
9222
9223   if (! HAVE_epilogue)
9224     {
9225       rtx insn = get_last_insn ();
9226       /* If the last insn was a BARRIER, we don't have to write anything except
9227          the trace table.  */
9228       if (GET_CODE (insn) == NOTE)
9229         insn = prev_nonnote_insn (insn);
9230       if (insn == 0 ||  GET_CODE (insn) != BARRIER)
9231         {
9232           /* This is slightly ugly, but at least we don't have two
9233              copies of the epilogue-emitting code.  */
9234           start_sequence ();
9235
9236           /* A NOTE_INSN_DELETED is supposed to be at the start
9237              and end of the "toplevel" insn chain.  */
9238           emit_note (0, NOTE_INSN_DELETED);
9239           rs6000_emit_epilogue (FALSE);
9240           emit_note (0, NOTE_INSN_DELETED);
9241
9242           if (TARGET_DEBUG_STACK)
9243             debug_rtx_list (get_insns (), 100);
9244           final (get_insns (), file, FALSE, FALSE);
9245           end_sequence ();
9246         }
9247     }
9248
9249   /* Output a traceback table here.  See /usr/include/sys/debug.h for info
9250      on its format.
9251
9252      We don't output a traceback table if -finhibit-size-directive was
9253      used.  The documentation for -finhibit-size-directive reads
9254      ``don't output a @code{.size} assembler directive, or anything
9255      else that would cause trouble if the function is split in the
9256      middle, and the two halves are placed at locations far apart in
9257      memory.''  The traceback table has this property, since it
9258      includes the offset from the start of the function to the
9259      traceback table itself.
9260
9261      System V.4 Powerpc's (and the embedded ABI derived from it) use a
9262      different traceback table.  */
9263   if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive)
9264     {
9265       const char *fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
9266       const char *language_string = lang_hooks.name;
9267       int fixed_parms = 0, float_parms = 0, parm_info = 0;
9268       int i;
9269
9270       while (*fname == '.')     /* V.4 encodes . in the name */
9271         fname++;
9272
9273       /* Need label immediately before tbtab, so we can compute its offset
9274          from the function start.  */
9275       if (*fname == '*')
9276         ++fname;
9277       ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
9278       ASM_OUTPUT_LABEL (file, fname);
9279
9280       /* The .tbtab pseudo-op can only be used for the first eight
9281          expressions, since it can't handle the possibly variable
9282          length fields that follow.  However, if you omit the optional
9283          fields, the assembler outputs zeros for all optional fields
9284          anyways, giving each variable length field is minimum length
9285          (as defined in sys/debug.h).  Thus we can not use the .tbtab
9286          pseudo-op at all.  */
9287
9288       /* An all-zero word flags the start of the tbtab, for debuggers
9289          that have to find it by searching forward from the entry
9290          point or from the current pc.  */
9291       fputs ("\t.long 0\n", file);
9292
9293       /* Tbtab format type.  Use format type 0.  */
9294       fputs ("\t.byte 0,", file);
9295
9296       /* Language type.  Unfortunately, there doesn't seem to be any
9297          official way to get this info, so we use language_string.  C
9298          is 0.  C++ is 9.  No number defined for Obj-C, so use the
9299          value for C for now.  There is no official value for Java,
9300          although IBM appears to be using 13.  There is no official value
9301          for Chill, so we've chosen 44 pseudo-randomly.  */
9302       if (! strcmp (language_string, "GNU C")
9303           || ! strcmp (language_string, "GNU Objective-C"))
9304         i = 0;
9305       else if (! strcmp (language_string, "GNU F77"))
9306         i = 1;
9307       else if (! strcmp (language_string, "GNU Ada"))
9308         i = 3;
9309       else if (! strcmp (language_string, "GNU Pascal"))
9310         i = 2;
9311       else if (! strcmp (language_string, "GNU C++"))
9312         i = 9;
9313       else if (! strcmp (language_string, "GNU Java"))
9314         i = 13;
9315       else if (! strcmp (language_string, "GNU CHILL"))
9316         i = 44;
9317       else
9318         abort ();
9319       fprintf (file, "%d,", i);
9320
9321       /* 8 single bit fields: global linkage (not set for C extern linkage,
9322          apparently a PL/I convention?), out-of-line epilogue/prologue, offset
9323          from start of procedure stored in tbtab, internal function, function
9324          has controlled storage, function has no toc, function uses fp,
9325          function logs/aborts fp operations.  */
9326       /* Assume that fp operations are used if any fp reg must be saved.  */
9327       fprintf (file, "%d,",
9328                (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
9329
9330       /* 6 bitfields: function is interrupt handler, name present in
9331          proc table, function calls alloca, on condition directives
9332          (controls stack walks, 3 bits), saves condition reg, saves
9333          link reg.  */
9334       /* The `function calls alloca' bit seems to be set whenever reg 31 is
9335          set up as a frame pointer, even when there is no alloca call.  */
9336       fprintf (file, "%d,",
9337                ((optional_tbtab << 6)
9338                 | ((optional_tbtab & frame_pointer_needed) << 5)
9339                 | (info->cr_save_p << 1)
9340                 | (info->lr_save_p)));
9341
9342       /* 3 bitfields: saves backchain, fixup code, number of fpr saved
9343          (6 bits).  */
9344       fprintf (file, "%d,",
9345                (info->push_p << 7) | (64 - info->first_fp_reg_save));
9346
9347       /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits).  */
9348       fprintf (file, "%d,", (32 - first_reg_to_save ()));
9349
9350       if (optional_tbtab)
9351         {
9352           /* Compute the parameter info from the function decl argument
9353              list.  */
9354           tree decl;
9355           int next_parm_info_bit = 31;
9356
9357           for (decl = DECL_ARGUMENTS (current_function_decl);
9358                decl; decl = TREE_CHAIN (decl))
9359             {
9360               rtx parameter = DECL_INCOMING_RTL (decl);
9361               enum machine_mode mode = GET_MODE (parameter);
9362
9363               if (GET_CODE (parameter) == REG)
9364                 {
9365                   if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9366                     {
9367                       int bits;
9368
9369                       float_parms++;
9370
9371                       if (mode == SFmode)
9372                         bits = 0x2;
9373                       else if (mode == DFmode)
9374                         bits = 0x3;
9375                       else
9376                         abort ();
9377
9378                       /* If only one bit will fit, don't or in this entry.  */
9379                       if (next_parm_info_bit > 0)
9380                         parm_info |= (bits << (next_parm_info_bit - 1));
9381                       next_parm_info_bit -= 2;
9382                     }
9383                   else
9384                     {
9385                       fixed_parms += ((GET_MODE_SIZE (mode)
9386                                        + (UNITS_PER_WORD - 1))
9387                                       / UNITS_PER_WORD);
9388                       next_parm_info_bit -= 1;
9389                     }
9390                 }
9391             }
9392         }
9393
9394       /* Number of fixed point parameters.  */
9395       /* This is actually the number of words of fixed point parameters; thus
9396          an 8 byte struct counts as 2; and thus the maximum value is 8.  */
9397       fprintf (file, "%d,", fixed_parms);
9398
9399       /* 2 bitfields: number of floating point parameters (7 bits), parameters
9400          all on stack.  */
9401       /* This is actually the number of fp registers that hold parameters;
9402          and thus the maximum value is 13.  */
9403       /* Set parameters on stack bit if parameters are not in their original
9404          registers, regardless of whether they are on the stack?  Xlc
9405          seems to set the bit when not optimizing.  */
9406       fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
9407
9408       if (! optional_tbtab)
9409         return;
9410
9411       /* Optional fields follow.  Some are variable length.  */
9412
9413       /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
9414          11 double float.  */
9415       /* There is an entry for each parameter in a register, in the order that
9416          they occur in the parameter list.  Any intervening arguments on the
9417          stack are ignored.  If the list overflows a long (max possible length
9418          34 bits) then completely leave off all elements that don't fit.  */
9419       /* Only emit this long if there was at least one parameter.  */
9420       if (fixed_parms || float_parms)
9421         fprintf (file, "\t.long %d\n", parm_info);
9422
9423       /* Offset from start of code to tb table.  */
9424       fputs ("\t.long ", file);
9425       ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
9426 #if TARGET_AIX
9427       RS6000_OUTPUT_BASENAME (file, fname);
9428 #else
9429       assemble_name (file, fname);
9430 #endif
9431       fputs ("-.", file);
9432 #if TARGET_AIX
9433       RS6000_OUTPUT_BASENAME (file, fname);
9434 #else
9435       assemble_name (file, fname);
9436 #endif
9437       putc ('\n', file);
9438
9439       /* Interrupt handler mask.  */
9440       /* Omit this long, since we never set the interrupt handler bit
9441          above.  */
9442
9443       /* Number of CTL (controlled storage) anchors.  */
9444       /* Omit this long, since the has_ctl bit is never set above.  */
9445
9446       /* Displacement into stack of each CTL anchor.  */
9447       /* Omit this list of longs, because there are no CTL anchors.  */
9448
9449       /* Length of function name.  */
9450       fprintf (file, "\t.short %d\n", (int) strlen (fname));
9451
9452       /* Function name.  */
9453       assemble_string (fname, strlen (fname));
9454
9455       /* Register for alloca automatic storage; this is always reg 31.
9456          Only emit this if the alloca bit was set above.  */
9457       if (frame_pointer_needed)
9458         fputs ("\t.byte 31\n", file);
9459     }
9460   return;
9461 }
9462 \f
9463 /* A C compound statement that outputs the assembler code for a thunk
9464    function, used to implement C++ virtual function calls with
9465    multiple inheritance.  The thunk acts as a wrapper around a virtual
9466    function, adjusting the implicit object parameter before handing
9467    control off to the real function.
9468
9469    First, emit code to add the integer DELTA to the location that
9470    contains the incoming first argument.  Assume that this argument
9471    contains a pointer, and is the one used to pass the `this' pointer
9472    in C++.  This is the incoming argument *before* the function
9473    prologue, e.g. `%o0' on a sparc.  The addition must preserve the
9474    values of all other incoming arguments.
9475
9476    After the addition, emit code to jump to FUNCTION, which is a
9477    `FUNCTION_DECL'.  This is a direct pure jump, not a call, and does
9478    not touch the return address.  Hence returning from FUNCTION will
9479    return to whoever called the current `thunk'.
9480
9481    The effect must be as if FUNCTION had been called directly with the
9482    adjusted first argument.  This macro is responsible for emitting
9483    all of the code for a thunk function; output_function_prologue()
9484    and output_function_epilogue() are not invoked.
9485
9486    The THUNK_FNDECL is redundant.  (DELTA and FUNCTION have already
9487    been extracted from it.)  It might possibly be useful on some
9488    targets, but probably not.
9489
9490    If you do not define this macro, the target-independent code in the
9491    C++ frontend will generate a less efficient heavyweight thunk that
9492    calls FUNCTION instead of jumping to it.  The generic approach does
9493    not support varargs.  */
9494
9495 void
9496 output_mi_thunk (file, thunk_fndecl, delta, function)
9497      FILE *file;
9498      tree thunk_fndecl ATTRIBUTE_UNUSED;
9499      int delta;
9500      tree function;
9501 {
9502   const char *this_reg =
9503     reg_names[ aggregate_value_p (TREE_TYPE (TREE_TYPE (function))) ? 4 : 3 ];
9504   const char *prefix;
9505   const char *fname;
9506   const char *r0         = reg_names[0];
9507   const char *toc        = reg_names[2];
9508   const char *schain     = reg_names[11];
9509   const char *r12        = reg_names[12];
9510   char buf[512];
9511   static int labelno = 0;
9512
9513   /* Small constants that can be done by one add instruction.  */
9514   if (delta >= -32768 && delta <= 32767)
9515     {
9516       if (! TARGET_NEW_MNEMONICS)
9517         fprintf (file, "\tcal %s,%d(%s)\n", this_reg, delta, this_reg);
9518       else
9519         fprintf (file, "\taddi %s,%s,%d\n", this_reg, this_reg, delta);
9520     }
9521
9522   /* Large constants that can be done by one addis instruction.  */
9523   else if ((delta & 0xffff) == 0 && num_insns_constant_wide (delta) == 1)
9524     asm_fprintf (file, "\t{cau|addis} %s,%s,%d\n", this_reg, this_reg,
9525                  delta >> 16);
9526
9527   /* 32-bit constants that can be done by an add and addis instruction.  */
9528   else if (TARGET_32BIT || num_insns_constant_wide (delta) == 1)
9529     {
9530       /* Break into two pieces, propagating the sign bit from the low
9531          word to the upper word.  */
9532       int delta_high = delta >> 16;
9533       int delta_low  = delta & 0xffff;
9534       if ((delta_low & 0x8000) != 0)
9535         {
9536           delta_high++;
9537           delta_low = (delta_low ^ 0x8000) - 0x8000;    /* sign extend */
9538         }
9539
9540       asm_fprintf (file, "\t{cau|addis} %s,%s,%d\n", this_reg, this_reg,
9541                    delta_high);
9542
9543       if (! TARGET_NEW_MNEMONICS)
9544         fprintf (file, "\tcal %s,%d(%s)\n", this_reg, delta_low, this_reg);
9545       else
9546         fprintf (file, "\taddi %s,%s,%d\n", this_reg, this_reg, delta_low);
9547     }
9548
9549   /* 64-bit constants, fixme */
9550   else
9551     abort ();
9552
9553   /* Get the prefix in front of the names.  */
9554   switch (DEFAULT_ABI)
9555     {
9556     default:
9557       abort ();
9558
9559     case ABI_AIX:
9560       prefix = ".";
9561       break;
9562
9563     case ABI_V4:
9564     case ABI_AIX_NODESC:
9565       prefix = "";
9566       break;
9567     }
9568
9569   /* If the function is compiled in this module, jump to it directly.
9570      Otherwise, load up its address and jump to it.  */
9571
9572   fname = XSTR (XEXP (DECL_RTL (function), 0), 0);
9573
9574   if (current_file_function_operand (XEXP (DECL_RTL (function), 0), VOIDmode)
9575       && ! lookup_attribute ("longcall",
9576                              TYPE_ATTRIBUTES (TREE_TYPE (function))))
9577     {
9578       fprintf (file, "\tb %s", prefix);
9579       assemble_name (file, fname);
9580       if (DEFAULT_ABI == ABI_V4 && flag_pic) fputs ("@local", file);
9581       putc ('\n', file);
9582     }
9583
9584   else
9585     {
9586       switch (DEFAULT_ABI)
9587         {
9588         default:
9589           abort ();
9590
9591         case ABI_AIX:
9592           /* Set up a TOC entry for the function.  */
9593           ASM_GENERATE_INTERNAL_LABEL (buf, "Lthunk", labelno);
9594           toc_section ();
9595           ASM_OUTPUT_INTERNAL_LABEL (file, "Lthunk", labelno);
9596           labelno++;
9597
9598           if (TARGET_MINIMAL_TOC)
9599             fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
9600           else
9601             {
9602               fputs ("\t.tc ", file);
9603               assemble_name (file, fname);
9604               fputs ("[TC],", file);
9605             }
9606           assemble_name (file, fname);
9607           putc ('\n', file);
9608           text_section ();
9609           if (TARGET_MINIMAL_TOC)
9610             asm_fprintf (file, (TARGET_32BIT)
9611                          ? "\t{l|lwz} %s,%s(%s)\n" : "\tld %s,%s(%s)\n", r12,
9612                          TARGET_ELF ? ".LCTOC0@toc" : ".LCTOC..1", toc);
9613           asm_fprintf (file, (TARGET_32BIT) ? "\t{l|lwz} %s," : "\tld %s,", r12);
9614           assemble_name (file, buf);
9615           if (TARGET_ELF && TARGET_MINIMAL_TOC)
9616             fputs ("-(.LCTOC1)", file);
9617           asm_fprintf (file, "(%s)\n", TARGET_MINIMAL_TOC ? r12 : toc);
9618           asm_fprintf (file,
9619                        (TARGET_32BIT) ? "\t{l|lwz} %s,0(%s)\n" : "\tld %s,0(%s)\n",
9620                        r0, r12);
9621
9622           asm_fprintf (file,
9623                        (TARGET_32BIT) ? "\t{l|lwz} %s,4(%s)\n" : "\tld %s,8(%s)\n",
9624                        toc, r12);
9625
9626           asm_fprintf (file, "\tmtctr %s\n", r0);
9627           asm_fprintf (file,
9628                        (TARGET_32BIT) ? "\t{l|lwz} %s,8(%s)\n" : "\tld %s,16(%s)\n",
9629                        schain, r12);
9630
9631           asm_fprintf (file, "\tbctr\n");
9632           break;
9633
9634         case ABI_AIX_NODESC:
9635         case ABI_V4:
9636           fprintf (file, "\tb %s", prefix);
9637           assemble_name (file, fname);
9638           if (flag_pic) fputs ("@plt", file);
9639           putc ('\n', file);
9640           break;
9641
9642 #if TARGET_MACHO
9643         case ABI_DARWIN:
9644           fprintf (file, "\tb %s", prefix);
9645           if (flag_pic && !machopic_name_defined_p (fname))
9646             assemble_name (file, machopic_stub_name (fname));
9647           else
9648             assemble_name (file, fname);
9649           putc ('\n', file);
9650           break;
9651 #endif
9652         }
9653     }
9654 }
9655
9656 \f
9657 /* A quick summary of the various types of 'constant-pool tables'
9658    under PowerPC:
9659
9660    Target       Flags           Name            One table per   
9661    AIX          (none)          AIX TOC         object file
9662    AIX          -mfull-toc      AIX TOC         object file
9663    AIX          -mminimal-toc   AIX minimal TOC translation unit
9664    SVR4/EABI    (none)          SVR4 SDATA      object file
9665    SVR4/EABI    -fpic           SVR4 pic        object file
9666    SVR4/EABI    -fPIC           SVR4 PIC        translation unit
9667    SVR4/EABI    -mrelocatable   EABI TOC        function
9668    SVR4/EABI    -maix           AIX TOC         object file
9669    SVR4/EABI    -maix -mminimal-toc 
9670                                 AIX minimal TOC translation unit
9671
9672    Name                 Reg.    Set by  entries       contains:
9673                                         made by  addrs? fp?     sum?
9674
9675    AIX TOC              2       crt0    as       Y      option  option
9676    AIX minimal TOC      30      prolog  gcc      Y      Y       option
9677    SVR4 SDATA           13      crt0    gcc      N      Y       N
9678    SVR4 pic             30      prolog  ld       Y      not yet N
9679    SVR4 PIC             30      prolog  gcc      Y      option  option
9680    EABI TOC             30      prolog  gcc      Y      option  option
9681
9682 */
9683
9684 /* Hash table stuff for keeping track of TOC entries.  */
9685
9686 struct toc_hash_struct 
9687 {
9688   /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
9689      ASM_OUTPUT_SPECIAL_POOL_ENTRY_P.  */
9690   rtx key;
9691   enum machine_mode key_mode;
9692   int labelno;
9693 };
9694
9695 static htab_t toc_hash_table;
9696
9697 /* Hash functions for the hash table.  */
9698
9699 static unsigned
9700 rs6000_hash_constant (k)
9701      rtx k;
9702 {
9703   unsigned result = (GET_CODE (k) << 3) ^ GET_MODE (k);
9704   const char *format = GET_RTX_FORMAT (GET_CODE (k));
9705   int flen = strlen (format);
9706   int fidx;
9707   
9708   if (GET_CODE (k) == LABEL_REF)
9709     return result * 1231 + X0INT (XEXP (k, 0), 3);
9710
9711   if (GET_CODE (k) == CONST_DOUBLE)
9712     fidx = 1;
9713   else if (GET_CODE (k) == CODE_LABEL)
9714     fidx = 3;
9715   else
9716     fidx = 0;
9717
9718   for (; fidx < flen; fidx++)
9719     switch (format[fidx])
9720       {
9721       case 's':
9722         {
9723           unsigned i, len;
9724           const char *str = XSTR (k, fidx);
9725           len = strlen (str);
9726           result = result * 613 + len;
9727           for (i = 0; i < len; i++)
9728             result = result * 613 + (unsigned) str[i];
9729           break;
9730         }
9731       case 'u':
9732       case 'e':
9733         result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
9734         break;
9735       case 'i':
9736       case 'n':
9737         result = result * 613 + (unsigned) XINT (k, fidx);
9738         break;
9739       case 'w':
9740         if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
9741           result = result * 613 + (unsigned) XWINT (k, fidx);
9742         else
9743           {
9744             size_t i;
9745             for (i = 0; i < sizeof(HOST_WIDE_INT)/sizeof(unsigned); i++)
9746               result = result * 613 + (unsigned) (XWINT (k, fidx)
9747                                                   >> CHAR_BIT * i);
9748           }
9749         break;
9750       default:
9751         abort ();
9752       }
9753   return result;
9754 }
9755
9756 static unsigned
9757 toc_hash_function (hash_entry)
9758      const void * hash_entry;
9759 {
9760   const struct toc_hash_struct *thc = 
9761     (const struct toc_hash_struct *) hash_entry;
9762   return rs6000_hash_constant (thc->key) ^ thc->key_mode;
9763 }
9764
9765 /* Compare H1 and H2 for equivalence.  */
9766
9767 static int
9768 toc_hash_eq (h1, h2)
9769      const void * h1;
9770      const void * h2;
9771 {
9772   rtx r1 = ((const struct toc_hash_struct *) h1)->key;
9773   rtx r2 = ((const struct toc_hash_struct *) h2)->key;
9774
9775   if (((const struct toc_hash_struct *) h1)->key_mode
9776       != ((const struct toc_hash_struct *) h2)->key_mode)
9777     return 0;
9778
9779   /* Gotcha:  One of these const_doubles will be in memory.
9780      The other may be on the constant-pool chain.
9781      So rtx_equal_p will think they are different...  */
9782   if (r1 == r2)
9783     return 1;
9784   if (GET_CODE (r1) != GET_CODE (r2)
9785       || GET_MODE (r1) != GET_MODE (r2))
9786     return 0;
9787   if (GET_CODE (r1) == CONST_DOUBLE)
9788     {
9789       int format_len = strlen (GET_RTX_FORMAT (CONST_DOUBLE));
9790       int i;
9791       for (i = 1; i < format_len; i++)
9792         if (XWINT (r1, i) != XWINT (r2, i))
9793           return 0;
9794       
9795       return 1;
9796     }
9797   else if (GET_CODE (r1) == LABEL_REF)
9798     return (CODE_LABEL_NUMBER (XEXP (r1, 0)) 
9799             == CODE_LABEL_NUMBER (XEXP (r2, 0)));
9800   else
9801     return rtx_equal_p (r1, r2);
9802 }
9803
9804 /* Mark the hash table-entry HASH_ENTRY.  */
9805
9806 static int
9807 toc_hash_mark_entry (hash_slot, unused)
9808      void ** hash_slot;
9809      void * unused ATTRIBUTE_UNUSED;
9810 {
9811   const struct toc_hash_struct * hash_entry = 
9812     *(const struct toc_hash_struct **) hash_slot;
9813   rtx r = hash_entry->key;
9814   ggc_set_mark (hash_entry);
9815   /* For CODE_LABELS, we don't want to drag in the whole insn chain...  */
9816   if (GET_CODE (r) == LABEL_REF)
9817     {
9818       ggc_set_mark (r);
9819       ggc_set_mark (XEXP (r, 0));
9820     }
9821   else
9822     ggc_mark_rtx (r);
9823   return 1;
9824 }
9825
9826 /* Mark all the elements of the TOC hash-table *HT.  */
9827
9828 static void
9829 toc_hash_mark_table (vht)
9830      void *vht;
9831 {
9832   htab_t *ht = vht;
9833   
9834   htab_traverse (*ht, toc_hash_mark_entry, (void *)0);
9835 }
9836
9837 /* These are the names given by the C++ front-end to vtables, and
9838    vtable-like objects.  Ideally, this logic should not be here;
9839    instead, there should be some programmatic way of inquiring as
9840    to whether or not an object is a vtable.  */
9841
9842 #define VTABLE_NAME_P(NAME)                             \
9843   (strncmp ("_vt.", name, strlen("_vt.")) == 0          \
9844   || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0       \
9845   || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0       \
9846   || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0) 
9847
9848 void
9849 rs6000_output_symbol_ref (file, x)
9850      FILE *file;
9851      rtx x;
9852 {
9853   /* Currently C++ toc references to vtables can be emitted before it
9854      is decided whether the vtable is public or private.  If this is
9855      the case, then the linker will eventually complain that there is
9856      a reference to an unknown section.  Thus, for vtables only, 
9857      we emit the TOC reference to reference the symbol and not the
9858      section.  */
9859   const char *name = XSTR (x, 0);
9860
9861   if (VTABLE_NAME_P (name)) 
9862     {
9863       RS6000_OUTPUT_BASENAME (file, name);
9864     }
9865   else
9866     assemble_name (file, name);
9867 }
9868
9869 /* Output a TOC entry.  We derive the entry name from what is being
9870    written.  */
9871
9872 void
9873 output_toc (file, x, labelno, mode)
9874      FILE *file;
9875      rtx x;
9876      int labelno;
9877      enum machine_mode mode;
9878 {
9879   char buf[256];
9880   const char *name = buf;
9881   const char *real_name;
9882   rtx base = x;
9883   int offset = 0;
9884
9885   if (TARGET_NO_TOC)
9886     abort ();
9887
9888   /* When the linker won't eliminate them, don't output duplicate
9889      TOC entries (this happens on AIX if there is any kind of TOC,
9890      and on SVR4 under -fPIC or -mrelocatable).  */
9891   if (TARGET_TOC)
9892     {
9893       struct toc_hash_struct *h;
9894       void * * found;
9895       
9896       h = ggc_alloc (sizeof (*h));
9897       h->key = x;
9898       h->key_mode = mode;
9899       h->labelno = labelno;
9900       
9901       found = htab_find_slot (toc_hash_table, h, 1);
9902       if (*found == NULL)
9903         *found = h;
9904       else  /* This is indeed a duplicate.  
9905                Set this label equal to that label.  */
9906         {
9907           fputs ("\t.set ", file);
9908           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
9909           fprintf (file, "%d,", labelno);
9910           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
9911           fprintf (file, "%d\n", ((*(const struct toc_hash_struct **) 
9912                                               found)->labelno));
9913           return;
9914         }
9915     }
9916
9917   /* If we're going to put a double constant in the TOC, make sure it's
9918      aligned properly when strict alignment is on.  */
9919   if (GET_CODE (x) == CONST_DOUBLE
9920       && STRICT_ALIGNMENT
9921       && GET_MODE_BITSIZE (mode) >= 64
9922       && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
9923     ASM_OUTPUT_ALIGN (file, 3);
9924   }
9925
9926   ASM_OUTPUT_INTERNAL_LABEL (file, "LC", labelno);
9927
9928   /* Handle FP constants specially.  Note that if we have a minimal
9929      TOC, things we put here aren't actually in the TOC, so we can allow
9930      FP constants.  */
9931   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
9932     {
9933       REAL_VALUE_TYPE rv;
9934       long k[2];
9935
9936       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
9937       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
9938
9939       if (TARGET_64BIT)
9940         {
9941           if (TARGET_MINIMAL_TOC)
9942             fputs (DOUBLE_INT_ASM_OP, file);
9943           else
9944             fprintf (file, "\t.tc FD_%lx_%lx[TC],", k[0], k[1]);
9945           fprintf (file, "0x%lx%08lx\n", k[0], k[1]);
9946           return;
9947         }
9948       else
9949         {
9950           if (TARGET_MINIMAL_TOC)
9951             fputs ("\t.long ", file);
9952           else
9953             fprintf (file, "\t.tc FD_%lx_%lx[TC],", k[0], k[1]);
9954           fprintf (file, "0x%lx,0x%lx\n", k[0], k[1]);
9955           return;
9956         }
9957     }
9958   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
9959     {
9960       REAL_VALUE_TYPE rv;
9961       long l;
9962
9963       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
9964       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
9965
9966       if (TARGET_64BIT)
9967         {
9968           if (TARGET_MINIMAL_TOC)
9969             fputs (DOUBLE_INT_ASM_OP, file);
9970           else
9971             fprintf (file, "\t.tc FS_%lx[TC],", l);
9972           fprintf (file, "0x%lx00000000\n", l);
9973           return;
9974         }
9975       else
9976         {
9977           if (TARGET_MINIMAL_TOC)
9978             fputs ("\t.long ", file);
9979           else
9980             fprintf (file, "\t.tc FS_%lx[TC],", l);
9981           fprintf (file, "0x%lx\n", l);
9982           return;
9983         }
9984     }
9985   else if (GET_MODE (x) == VOIDmode
9986            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
9987     {
9988       unsigned HOST_WIDE_INT low;
9989       HOST_WIDE_INT high;
9990
9991       if (GET_CODE (x) == CONST_DOUBLE)
9992         {
9993           low = CONST_DOUBLE_LOW (x);
9994           high = CONST_DOUBLE_HIGH (x);
9995         }
9996       else
9997 #if HOST_BITS_PER_WIDE_INT == 32
9998         {
9999           low = INTVAL (x);
10000           high = (low & 0x80000000) ? ~0 : 0;
10001         }
10002 #else
10003         {
10004           low = INTVAL (x) & 0xffffffff;
10005           high = (HOST_WIDE_INT) INTVAL (x) >> 32;
10006         }
10007 #endif
10008
10009       /* TOC entries are always Pmode-sized, but since this
10010          is a bigendian machine then if we're putting smaller
10011          integer constants in the TOC we have to pad them.
10012          (This is still a win over putting the constants in
10013          a separate constant pool, because then we'd have
10014          to have both a TOC entry _and_ the actual constant.)
10015
10016          For a 32-bit target, CONST_INT values are loaded and shifted
10017          entirely within `low' and can be stored in one TOC entry.  */
10018
10019       if (TARGET_64BIT && POINTER_SIZE < GET_MODE_BITSIZE (mode))
10020         abort ();/* It would be easy to make this work, but it doesn't now.  */
10021
10022       if (POINTER_SIZE > GET_MODE_BITSIZE (mode))
10023         lshift_double (low, high, POINTER_SIZE - GET_MODE_BITSIZE (mode),
10024                        POINTER_SIZE, &low, &high, 0);
10025
10026       if (TARGET_64BIT)
10027         {
10028           if (TARGET_MINIMAL_TOC)
10029             fputs (DOUBLE_INT_ASM_OP, file);
10030           else
10031             fprintf (file, "\t.tc ID_%lx_%lx[TC],", (long)high, (long)low);
10032           fprintf (file, "0x%lx%08lx\n", (long) high, (long) low);
10033           return;
10034         }
10035       else
10036         {
10037           if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
10038             {
10039               if (TARGET_MINIMAL_TOC)
10040                 fputs ("\t.long ", file);
10041               else
10042                 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
10043                          (long)high, (long)low);
10044               fprintf (file, "0x%lx,0x%lx\n", (long) high, (long) low);
10045             }
10046           else
10047             {
10048               if (TARGET_MINIMAL_TOC)
10049                 fputs ("\t.long ", file);
10050               else
10051                 fprintf (file, "\t.tc IS_%lx[TC],", (long) low);
10052               fprintf (file, "0x%lx\n", (long) low);
10053             }
10054           return;
10055         }
10056     }
10057
10058   if (GET_CODE (x) == CONST)
10059     {
10060       if (GET_CODE (XEXP (x, 0)) != PLUS)
10061         abort ();
10062
10063       base = XEXP (XEXP (x, 0), 0);
10064       offset = INTVAL (XEXP (XEXP (x, 0), 1));
10065     }
10066   
10067   if (GET_CODE (base) == SYMBOL_REF)
10068     name = XSTR (base, 0);
10069   else if (GET_CODE (base) == LABEL_REF)
10070     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (XEXP (base, 0)));
10071   else if (GET_CODE (base) == CODE_LABEL)
10072     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
10073   else
10074     abort ();
10075
10076   STRIP_NAME_ENCODING (real_name, name);
10077   if (TARGET_MINIMAL_TOC)
10078     fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
10079   else
10080     {
10081       fprintf (file, "\t.tc %s", real_name);
10082
10083       if (offset < 0)
10084         fprintf (file, ".N%d", - offset);
10085       else if (offset)
10086         fprintf (file, ".P%d", offset);
10087
10088       fputs ("[TC],", file);
10089     }
10090
10091   /* Currently C++ toc references to vtables can be emitted before it
10092      is decided whether the vtable is public or private.  If this is
10093      the case, then the linker will eventually complain that there is
10094      a TOC reference to an unknown section.  Thus, for vtables only,
10095      we emit the TOC reference to reference the symbol and not the
10096      section.  */
10097   if (VTABLE_NAME_P (name))
10098     {
10099       RS6000_OUTPUT_BASENAME (file, name);
10100       if (offset < 0)
10101         fprintf (file, "%d", offset);
10102       else if (offset > 0)
10103         fprintf (file, "+%d", offset);
10104     }
10105   else
10106     output_addr_const (file, x);
10107   putc ('\n', file);
10108 }
10109 \f
10110 /* Output an assembler pseudo-op to write an ASCII string of N characters
10111    starting at P to FILE.
10112
10113    On the RS/6000, we have to do this using the .byte operation and
10114    write out special characters outside the quoted string.
10115    Also, the assembler is broken; very long strings are truncated,
10116    so we must artificially break them up early.  */
10117
10118 void
10119 output_ascii (file, p, n)
10120      FILE *file;
10121      const char *p;
10122      int n;
10123 {
10124   char c;
10125   int i, count_string;
10126   const char *for_string = "\t.byte \"";
10127   const char *for_decimal = "\t.byte ";
10128   const char *to_close = NULL;
10129
10130   count_string = 0;
10131   for (i = 0; i < n; i++)
10132     {
10133       c = *p++;
10134       if (c >= ' ' && c < 0177)
10135         {
10136           if (for_string)
10137             fputs (for_string, file);
10138           putc (c, file);
10139
10140           /* Write two quotes to get one.  */
10141           if (c == '"')
10142             {
10143               putc (c, file);
10144               ++count_string;
10145             }
10146
10147           for_string = NULL;
10148           for_decimal = "\"\n\t.byte ";
10149           to_close = "\"\n";
10150           ++count_string;
10151
10152           if (count_string >= 512)
10153             {
10154               fputs (to_close, file);
10155
10156               for_string = "\t.byte \"";
10157               for_decimal = "\t.byte ";
10158               to_close = NULL;
10159               count_string = 0;
10160             }
10161         }
10162       else
10163         {
10164           if (for_decimal)
10165             fputs (for_decimal, file);
10166           fprintf (file, "%d", c);
10167
10168           for_string = "\n\t.byte \"";
10169           for_decimal = ", ";
10170           to_close = "\n";
10171           count_string = 0;
10172         }
10173     }
10174
10175   /* Now close the string if we have written one.  Then end the line.  */
10176   if (to_close)
10177     fputs (to_close, file);
10178 }
10179 \f
10180 /* Generate a unique section name for FILENAME for a section type
10181    represented by SECTION_DESC.  Output goes into BUF.
10182
10183    SECTION_DESC can be any string, as long as it is different for each
10184    possible section type.
10185
10186    We name the section in the same manner as xlc.  The name begins with an
10187    underscore followed by the filename (after stripping any leading directory
10188    names) with the last period replaced by the string SECTION_DESC.  If
10189    FILENAME does not contain a period, SECTION_DESC is appended to the end of
10190    the name.  */
10191
10192 void
10193 rs6000_gen_section_name (buf, filename, section_desc)
10194      char **buf;
10195      const char *filename;
10196      const char *section_desc;
10197 {
10198   const char *q, *after_last_slash, *last_period = 0;
10199   char *p;
10200   int len;
10201
10202   after_last_slash = filename;
10203   for (q = filename; *q; q++)
10204     {
10205       if (*q == '/')
10206         after_last_slash = q + 1;
10207       else if (*q == '.')
10208         last_period = q;
10209     }
10210
10211   len = strlen (after_last_slash) + strlen (section_desc) + 2;
10212   *buf = (char *) permalloc (len);
10213
10214   p = *buf;
10215   *p++ = '_';
10216
10217   for (q = after_last_slash; *q; q++)
10218     {
10219       if (q == last_period)
10220         {
10221           strcpy (p, section_desc);
10222           p += strlen (section_desc);
10223         }
10224
10225       else if (ISALNUM (*q))
10226         *p++ = *q;
10227     }
10228
10229   if (last_period == 0)
10230     strcpy (p, section_desc);
10231   else
10232     *p = '\0';
10233 }
10234 \f
10235 /* Emit profile function.  */
10236
10237 void
10238 output_profile_hook (labelno)
10239      int labelno;
10240 {
10241   if (DEFAULT_ABI == ABI_AIX)
10242     {
10243       char buf[30];
10244       const char *label_name;
10245       rtx fun;
10246
10247       labelno += 1;
10248
10249       ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
10250       STRIP_NAME_ENCODING (label_name, ggc_strdup (buf));
10251       fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
10252
10253       emit_library_call (init_one_libfunc (RS6000_MCOUNT), 0, VOIDmode, 1,
10254                          fun, Pmode);
10255     }
10256   else if (DEFAULT_ABI == ABI_DARWIN)
10257     {
10258       const char *mcount_name = RS6000_MCOUNT;
10259       int caller_addr_regno = LINK_REGISTER_REGNUM;
10260
10261       /* Be conservative and always set this, at least for now.  */
10262       current_function_uses_pic_offset_table = 1;
10263
10264 #if TARGET_MACHO
10265       /* For PIC code, set up a stub and collect the caller's address
10266          from r0, which is where the prologue puts it.  */
10267       if (flag_pic)
10268         {
10269           mcount_name = machopic_stub_name (mcount_name);
10270           if (current_function_uses_pic_offset_table)
10271             caller_addr_regno = 0;
10272         }
10273 #endif
10274       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
10275                          0, VOIDmode, 1,
10276                          gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
10277     }
10278 }
10279
10280 /* Write function profiler code.  */
10281
10282 void
10283 output_function_profiler (file, labelno)
10284   FILE *file;
10285   int labelno;
10286 {
10287   char buf[100];
10288
10289   ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
10290   switch (DEFAULT_ABI)
10291     {
10292     default:
10293       abort ();
10294
10295     case ABI_V4:
10296     case ABI_AIX_NODESC:
10297       fprintf (file, "\tmflr %s\n", reg_names[0]);
10298       if (flag_pic == 1)
10299         {
10300           fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
10301           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
10302                        reg_names[0], reg_names[1]);
10303           asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
10304           asm_fprintf (file, "\t{l|lwz} %s,", reg_names[0]);
10305           assemble_name (file, buf);
10306           asm_fprintf (file, "@got(%s)\n", reg_names[12]);
10307         }
10308       else if (flag_pic > 1)
10309         {
10310           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
10311                        reg_names[0], reg_names[1]);
10312           /* Now, we need to get the address of the label.  */
10313           fputs ("\tbl 1f\n\t.long ", file);
10314           assemble_name (file, buf);
10315           fputs ("-.\n1:", file);
10316           asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
10317           asm_fprintf (file, "\t{l|lwz} %s,0(%s)\n", 
10318                        reg_names[0], reg_names[11]);
10319           asm_fprintf (file, "\t{cax|add} %s,%s,%s\n",
10320                        reg_names[0], reg_names[0], reg_names[11]);
10321         }
10322       else
10323         {
10324           asm_fprintf (file, "\t{liu|lis} %s,", reg_names[12]);
10325           assemble_name (file, buf);
10326           fputs ("@ha\n", file);
10327           asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
10328                        reg_names[0], reg_names[1]);
10329           asm_fprintf (file, "\t{cal|la} %s,", reg_names[0]);
10330           assemble_name (file, buf);
10331           asm_fprintf (file, "@l(%s)\n", reg_names[12]);
10332         }
10333
10334       if (current_function_needs_context)
10335         asm_fprintf (file, "\tmr %s,%s\n",
10336                      reg_names[30], reg_names[STATIC_CHAIN_REGNUM]);
10337       fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
10338       if (current_function_needs_context)
10339         asm_fprintf (file, "\tmr %s,%s\n",
10340                      reg_names[STATIC_CHAIN_REGNUM], reg_names[30]);
10341       break;
10342
10343     case ABI_AIX:
10344     case ABI_DARWIN:
10345       /* Don't do anything, done in output_profile_hook ().  */
10346       break;
10347
10348     }
10349 }
10350
10351 /* Adjust the cost of a scheduling dependency.  Return the new cost of
10352    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
10353
10354 static int
10355 rs6000_adjust_cost (insn, link, dep_insn, cost)
10356      rtx insn;
10357      rtx link;
10358      rtx dep_insn ATTRIBUTE_UNUSED;
10359      int cost;
10360 {
10361   if (! recog_memoized (insn))
10362     return 0;
10363
10364   if (REG_NOTE_KIND (link) != 0)
10365     return 0;
10366
10367   if (REG_NOTE_KIND (link) == 0)
10368     {
10369       /* Data dependency; DEP_INSN writes a register that INSN reads
10370          some cycles later.  */
10371       switch (get_attr_type (insn))
10372         {
10373         case TYPE_JMPREG:
10374           /* Tell the first scheduling pass about the latency between
10375              a mtctr and bctr (and mtlr and br/blr).  The first
10376              scheduling pass will not know about this latency since
10377              the mtctr instruction, which has the latency associated
10378              to it, will be generated by reload.  */
10379           return TARGET_POWER ? 5 : 4;
10380         case TYPE_BRANCH:
10381           /* Leave some extra cycles between a compare and its
10382              dependent branch, to inhibit expensive mispredicts.  */
10383           if ((rs6000_cpu_attr == CPU_PPC750
10384                || rs6000_cpu_attr == CPU_PPC7400
10385                || rs6000_cpu_attr == CPU_PPC7450)
10386               && recog_memoized (dep_insn)
10387               && (INSN_CODE (dep_insn) >= 0)
10388               && (get_attr_type (dep_insn) == TYPE_COMPARE
10389                   || get_attr_type (dep_insn) == TYPE_DELAYED_COMPARE
10390                   || get_attr_type (dep_insn) == TYPE_FPCOMPARE
10391                   || get_attr_type (dep_insn) == TYPE_CR_LOGICAL))
10392             return cost + 2;
10393         default:
10394           break;
10395         }
10396       /* Fall out to return default cost.  */
10397     }
10398
10399   return cost;
10400 }
10401
10402 /* A C statement (sans semicolon) to update the integer scheduling
10403    priority INSN_PRIORITY (INSN).  Reduce the priority to execute the
10404    INSN earlier, increase the priority to execute INSN later.  Do not
10405    define this macro if you do not need to adjust the scheduling
10406    priorities of insns.  */
10407
10408 static int
10409 rs6000_adjust_priority (insn, priority)
10410      rtx insn ATTRIBUTE_UNUSED;
10411      int priority;
10412 {
10413   /* On machines (like the 750) which have asymmetric integer units,
10414      where one integer unit can do multiply and divides and the other
10415      can't, reduce the priority of multiply/divide so it is scheduled
10416      before other integer operations.  */
10417
10418 #if 0
10419   if (! INSN_P (insn))
10420     return priority;
10421
10422   if (GET_CODE (PATTERN (insn)) == USE)
10423     return priority;
10424
10425   switch (rs6000_cpu_attr) {
10426   case CPU_PPC750:
10427     switch (get_attr_type (insn))
10428       {
10429       default:
10430         break;
10431
10432       case TYPE_IMUL:
10433       case TYPE_IDIV:
10434         fprintf (stderr, "priority was %#x (%d) before adjustment\n",
10435                  priority, priority);
10436         if (priority >= 0 && priority < 0x01000000)
10437           priority >>= 3;
10438         break;
10439       }
10440   }
10441 #endif
10442
10443   return priority;
10444 }
10445
10446 /* Return how many instructions the machine can issue per cycle.  */
10447
10448 static int
10449 rs6000_issue_rate ()
10450 {
10451   switch (rs6000_cpu_attr) {
10452   case CPU_RIOS1:  /* ? */
10453   case CPU_RS64A:
10454   case CPU_PPC601: /* ? */
10455   case CPU_PPC7450:
10456     return 3;
10457   case CPU_PPC603:
10458   case CPU_PPC750:
10459   case CPU_PPC7400:
10460     return 2; 
10461   case CPU_RIOS2:
10462   case CPU_PPC604:
10463   case CPU_PPC604E:
10464   case CPU_PPC620:
10465   case CPU_PPC630:
10466     return 4;
10467   default:
10468     return 1;
10469   }
10470 }
10471
10472 \f
10473 /* Length in units of the trampoline for entering a nested function.  */
10474
10475 int
10476 rs6000_trampoline_size ()
10477 {
10478   int ret = 0;
10479
10480   switch (DEFAULT_ABI)
10481     {
10482     default:
10483       abort ();
10484
10485     case ABI_AIX:
10486       ret = (TARGET_32BIT) ? 12 : 24;
10487       break;
10488
10489     case ABI_DARWIN:
10490     case ABI_V4:
10491     case ABI_AIX_NODESC:
10492       ret = (TARGET_32BIT) ? 40 : 48;
10493       break;
10494     }
10495
10496   return ret;
10497 }
10498
10499 /* Emit RTL insns to initialize the variable parts of a trampoline.
10500    FNADDR is an RTX for the address of the function's pure code.
10501    CXT is an RTX for the static chain value for the function.  */
10502
10503 void
10504 rs6000_initialize_trampoline (addr, fnaddr, cxt)
10505      rtx addr;
10506      rtx fnaddr;
10507      rtx cxt;
10508 {
10509   enum machine_mode pmode = Pmode;
10510   int regsize = (TARGET_32BIT) ? 4 : 8;
10511   rtx ctx_reg = force_reg (pmode, cxt);
10512
10513   switch (DEFAULT_ABI)
10514     {
10515     default:
10516       abort ();
10517
10518 /* Macros to shorten the code expansions below.  */
10519 #define MEM_DEREF(addr) gen_rtx_MEM (pmode, memory_address (pmode, addr))
10520 #define MEM_PLUS(addr,offset) \
10521   gen_rtx_MEM (pmode, memory_address (pmode, plus_constant (addr, offset)))
10522
10523     /* Under AIX, just build the 3 word function descriptor */
10524     case ABI_AIX:
10525       {
10526         rtx fn_reg = gen_reg_rtx (pmode);
10527         rtx toc_reg = gen_reg_rtx (pmode);
10528         emit_move_insn (fn_reg, MEM_DEREF (fnaddr));
10529         emit_move_insn (toc_reg, MEM_PLUS (fnaddr, regsize));
10530         emit_move_insn (MEM_DEREF (addr), fn_reg);
10531         emit_move_insn (MEM_PLUS (addr, regsize), toc_reg);
10532         emit_move_insn (MEM_PLUS (addr, 2*regsize), ctx_reg);
10533       }
10534       break;
10535
10536     /* Under V.4/eabi/darwin, __trampoline_setup does the real work.  */
10537     case ABI_DARWIN:
10538     case ABI_V4:
10539     case ABI_AIX_NODESC:
10540       emit_library_call (gen_rtx_SYMBOL_REF (SImode, "__trampoline_setup"),
10541                          FALSE, VOIDmode, 4,
10542                          addr, pmode,
10543                          GEN_INT (rs6000_trampoline_size ()), SImode,
10544                          fnaddr, pmode,
10545                          ctx_reg, pmode);
10546       break;
10547     }
10548
10549   return;
10550 }
10551
10552 \f
10553 /* Table of valid machine attributes.  */
10554
10555 const struct attribute_spec rs6000_attribute_table[] =
10556 {
10557   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
10558   { "longcall", 0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
10559   { NULL,       0, 0, false, false, false, NULL }
10560 };
10561
10562 /* Handle a "longcall" attribute; arguments as in struct
10563    attribute_spec.handler.  */
10564
10565 static tree
10566 rs6000_handle_longcall_attribute (node, name, args, flags, no_add_attrs)
10567      tree *node;
10568      tree name;
10569      tree args ATTRIBUTE_UNUSED;
10570      int flags ATTRIBUTE_UNUSED;
10571      bool *no_add_attrs;
10572 {
10573   if (TREE_CODE (*node) != FUNCTION_TYPE
10574       && TREE_CODE (*node) != FIELD_DECL
10575       && TREE_CODE (*node) != TYPE_DECL)
10576     {
10577       warning ("`%s' attribute only applies to functions",
10578                IDENTIFIER_POINTER (name));
10579       *no_add_attrs = true;
10580     }
10581
10582   return NULL_TREE;
10583 }
10584
10585 /* Return a reference suitable for calling a function with the
10586    longcall attribute.  */
10587
10588 struct rtx_def *
10589 rs6000_longcall_ref (call_ref)
10590      rtx call_ref;
10591 {
10592   const char *call_name;
10593   tree node;
10594
10595   if (GET_CODE (call_ref) != SYMBOL_REF)
10596     return call_ref;
10597
10598   /* System V adds '.' to the internal name, so skip them.  */
10599   call_name = XSTR (call_ref, 0);
10600   if (*call_name == '.')
10601     {
10602       while (*call_name == '.')
10603         call_name++;
10604
10605       node = get_identifier (call_name);
10606       call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
10607     }
10608
10609   return force_reg (Pmode, call_ref);
10610 }
10611
10612 \f
10613 /* A C statement or statements to switch to the appropriate section
10614    for output of RTX in mode MODE.  You can assume that RTX is some
10615    kind of constant in RTL.  The argument MODE is redundant except in
10616    the case of a `const_int' rtx.  Select the section by calling
10617    `text_section' or one of the alternatives for other sections.
10618
10619    Do not define this macro if you put all constants in the read-only
10620    data section.  */
10621
10622 #ifdef USING_ELFOS_H
10623
10624 void
10625 rs6000_select_rtx_section (mode, x)
10626      enum machine_mode mode;
10627      rtx x;
10628 {
10629   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
10630     toc_section ();
10631   else
10632     const_section ();
10633 }
10634
10635 /* A C statement or statements to switch to the appropriate
10636    section for output of DECL.  DECL is either a `VAR_DECL' node
10637    or a constant of some sort.  RELOC indicates whether forming
10638    the initial value of DECL requires link-time relocations.  */
10639
10640 void
10641 rs6000_select_section (decl, reloc)
10642      tree decl;
10643      int reloc;
10644 {
10645   int size = int_size_in_bytes (TREE_TYPE (decl));
10646   int needs_sdata;
10647   int readonly;
10648   static void (* const sec_funcs[4]) PARAMS ((void)) = {
10649     &const_section,
10650     &sdata2_section,
10651     &data_section,
10652     &sdata_section
10653   };
10654   
10655   needs_sdata = (size > 0 
10656                  && size <= g_switch_value
10657                  && rs6000_sdata != SDATA_NONE
10658                  && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)));
10659
10660   if (TREE_CODE (decl) == STRING_CST)
10661     readonly = ! flag_writable_strings;
10662   else if (TREE_CODE (decl) == VAR_DECL)
10663     readonly = (! (flag_pic && reloc)
10664                 && TREE_READONLY (decl)
10665                 && ! TREE_SIDE_EFFECTS (decl)
10666                 && DECL_INITIAL (decl)
10667                 && DECL_INITIAL (decl) != error_mark_node
10668                 && TREE_CONSTANT (DECL_INITIAL (decl)));
10669   else if (TREE_CODE (decl) == CONSTRUCTOR)
10670     readonly = (! (flag_pic && reloc)
10671                 && ! TREE_SIDE_EFFECTS (decl)
10672                 && TREE_CONSTANT (decl));
10673   else
10674     readonly = 1;
10675   if (needs_sdata && rs6000_sdata != SDATA_EABI)
10676     readonly = 0;
10677   
10678   (*sec_funcs[(readonly ? 0 : 2) + (needs_sdata ? 1 : 0)])();
10679 }
10680
10681 /* A C statement to build up a unique section name, expressed as a
10682    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
10683    RELOC indicates whether the initial value of EXP requires
10684    link-time relocations.  If you do not define this macro, GCC will use
10685    the symbol name prefixed by `.' as the section name.  Note - this
10686    macro can now be called for uninitialized data items as well as
10687    initialised data and functions.  */
10688
10689 void
10690 rs6000_unique_section (decl, reloc)
10691      tree decl;
10692      int reloc;
10693 {
10694   int len;
10695   int sec;
10696   const char *name;
10697   char *string;
10698   const char *prefix;
10699
10700   static const char *const prefixes[7][2] =
10701   {
10702     { ".rodata.", ".gnu.linkonce.r." },
10703     { ".sdata2.", ".gnu.linkonce.s2." },
10704     { ".data.",   ".gnu.linkonce.d." },
10705     { ".sdata.",  ".gnu.linkonce.s." },
10706     { ".bss.",    ".gnu.linkonce.b." },
10707     { ".sbss.",   ".gnu.linkonce.sb." },
10708     { ".text.",   ".gnu.linkonce.t." }
10709   };
10710
10711   if (TREE_CODE (decl) == FUNCTION_DECL)
10712     sec = 6;
10713   else
10714     {
10715       int readonly;
10716       int needs_sdata;
10717       int size;
10718
10719       readonly = 1;
10720       if (TREE_CODE (decl) == STRING_CST)
10721         readonly = ! flag_writable_strings;
10722       else if (TREE_CODE (decl) == VAR_DECL)
10723         readonly = (! (flag_pic && reloc)
10724                     && TREE_READONLY (decl)
10725                     && ! TREE_SIDE_EFFECTS (decl)
10726                     && TREE_CONSTANT (DECL_INITIAL (decl)));
10727
10728       size = int_size_in_bytes (TREE_TYPE (decl));
10729       needs_sdata = (size > 0 
10730                      && size <= g_switch_value
10731                      && rs6000_sdata != SDATA_NONE
10732                      && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)));
10733
10734       if (DECL_INITIAL (decl) == 0
10735           || DECL_INITIAL (decl) == error_mark_node)
10736         sec = 4;
10737       else if (! readonly)
10738         sec = 2;
10739       else
10740         sec = 0;
10741
10742       if (needs_sdata)
10743         {
10744           /* .sdata2 is only for EABI.  */
10745           if (sec == 0 && rs6000_sdata != SDATA_EABI)
10746             sec = 2;
10747           sec += 1;
10748         }
10749     }
10750
10751   STRIP_NAME_ENCODING (name, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10752   prefix = prefixes[sec][DECL_ONE_ONLY (decl)];
10753   len    = strlen (name) + strlen (prefix);
10754   string = alloca (len + 1);
10755   
10756   sprintf (string, "%s%s", prefix, name);
10757   
10758   DECL_SECTION_NAME (decl) = build_string (len, string);
10759 }
10760
10761 \f
10762 /* If we are referencing a function that is static or is known to be
10763    in this file, make the SYMBOL_REF special.  We can use this to indicate
10764    that we can branch to this function without emitting a no-op after the
10765    call.  For real AIX calling sequences, we also replace the
10766    function name with the real name (1 or 2 leading .'s), rather than
10767    the function descriptor name.  This saves a lot of overriding code
10768    to read the prefixes.  */
10769
10770 void
10771 rs6000_encode_section_info (decl)
10772      tree decl;
10773 {
10774   if (TREE_CODE (decl) == FUNCTION_DECL)
10775     {
10776       rtx sym_ref = XEXP (DECL_RTL (decl), 0);
10777       if ((TREE_ASM_WRITTEN (decl) || ! TREE_PUBLIC (decl))
10778           && ! DECL_WEAK (decl))
10779         SYMBOL_REF_FLAG (sym_ref) = 1;
10780
10781       if (DEFAULT_ABI == ABI_AIX)
10782         {
10783           size_t len1 = (DEFAULT_ABI == ABI_AIX) ? 1 : 2;
10784           size_t len2 = strlen (XSTR (sym_ref, 0));
10785           char *str = alloca (len1 + len2 + 1);
10786           str[0] = '.';
10787           str[1] = '.';
10788           memcpy (str + len1, XSTR (sym_ref, 0), len2 + 1);
10789
10790           XSTR (sym_ref, 0) = ggc_alloc_string (str, len1 + len2);
10791         }
10792     }
10793   else if (rs6000_sdata != SDATA_NONE
10794            && DEFAULT_ABI == ABI_V4
10795            && TREE_CODE (decl) == VAR_DECL)
10796     {
10797       int size = int_size_in_bytes (TREE_TYPE (decl));
10798       tree section_name = DECL_SECTION_NAME (decl);
10799       const char *name = (char *)0;
10800       int len = 0;
10801
10802       if (section_name)
10803         {
10804           if (TREE_CODE (section_name) == STRING_CST)
10805             {
10806               name = TREE_STRING_POINTER (section_name);
10807               len = TREE_STRING_LENGTH (section_name);
10808             }
10809           else
10810             abort ();
10811         }
10812
10813       if ((size > 0 && size <= g_switch_value)
10814           || (name
10815               && ((len == sizeof (".sdata") - 1
10816                    && strcmp (name, ".sdata") == 0)
10817                   || (len == sizeof (".sdata2") - 1
10818                       && strcmp (name, ".sdata2") == 0)
10819                   || (len == sizeof (".sbss") - 1
10820                       && strcmp (name, ".sbss") == 0)
10821                   || (len == sizeof (".sbss2") - 1
10822                       && strcmp (name, ".sbss2") == 0)
10823                   || (len == sizeof (".PPC.EMB.sdata0") - 1
10824                       && strcmp (name, ".PPC.EMB.sdata0") == 0)
10825                   || (len == sizeof (".PPC.EMB.sbss0") - 1
10826                       && strcmp (name, ".PPC.EMB.sbss0") == 0))))
10827         {
10828           rtx sym_ref = XEXP (DECL_RTL (decl), 0);
10829           size_t len = strlen (XSTR (sym_ref, 0));
10830           char *str = alloca (len + 2);
10831
10832           str[0] = '@';
10833           memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
10834           XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
10835         }
10836     }
10837 }
10838
10839 #endif /* USING_ELFOS_H */
10840
10841 \f
10842 /* Return a REG that occurs in ADDR with coefficient 1.
10843    ADDR can be effectively incremented by incrementing REG.
10844
10845    r0 is special and we must not select it as an address
10846    register by this routine since our caller will try to
10847    increment the returned register via an "la" instruction.  */
10848
10849 struct rtx_def *
10850 find_addr_reg (addr)
10851      rtx addr;
10852 {
10853   while (GET_CODE (addr) == PLUS)
10854     {
10855       if (GET_CODE (XEXP (addr, 0)) == REG
10856           && REGNO (XEXP (addr, 0)) != 0)
10857         addr = XEXP (addr, 0);
10858       else if (GET_CODE (XEXP (addr, 1)) == REG
10859                && REGNO (XEXP (addr, 1)) != 0)
10860         addr = XEXP (addr, 1);
10861       else if (CONSTANT_P (XEXP (addr, 0)))
10862         addr = XEXP (addr, 1);
10863       else if (CONSTANT_P (XEXP (addr, 1)))
10864         addr = XEXP (addr, 0);
10865       else
10866         abort ();
10867     }
10868   if (GET_CODE (addr) == REG && REGNO (addr) != 0)
10869     return addr;
10870   abort ();
10871 }
10872
10873 void
10874 rs6000_fatal_bad_address (op)
10875   rtx op;
10876 {
10877   fatal_insn ("bad address", op);
10878 }
10879
10880 /* Called to register all of our global variables with the garbage
10881    collector.  */
10882
10883 static void
10884 rs6000_add_gc_roots ()
10885 {
10886   ggc_add_rtx_root (&rs6000_compare_op0, 1);
10887   ggc_add_rtx_root (&rs6000_compare_op1, 1);
10888
10889   toc_hash_table = htab_create (1021, toc_hash_function, toc_hash_eq, NULL);
10890   ggc_add_root (&toc_hash_table, 1, sizeof (toc_hash_table), 
10891                 toc_hash_mark_table);
10892
10893 #if TARGET_MACHO
10894   machopic_add_gc_roots ();
10895 #endif
10896 }
10897
10898 #if TARGET_MACHO
10899
10900 #if 0
10901 /* Returns 1 if OP is either a symbol reference or a sum of a symbol
10902    reference and a constant.  */
10903
10904 int
10905 symbolic_operand (op)
10906      rtx op;
10907 {
10908   switch (GET_CODE (op))
10909     {
10910     case SYMBOL_REF:
10911     case LABEL_REF:
10912       return 1;
10913     case CONST:
10914       op = XEXP (op, 0);
10915       return (GET_CODE (op) == SYMBOL_REF ||
10916               (GET_CODE (XEXP (op, 0)) == SYMBOL_REF
10917                || GET_CODE (XEXP (op, 0)) == LABEL_REF)
10918               && GET_CODE (XEXP (op, 1)) == CONST_INT);
10919     default:
10920       return 0;
10921     }
10922 }
10923 #endif
10924
10925 #ifdef RS6000_LONG_BRANCH
10926
10927 static tree stub_list = 0;
10928
10929 /* ADD_COMPILER_STUB adds the compiler generated stub for handling 
10930    procedure calls to the linked list.  */
10931
10932 void 
10933 add_compiler_stub (label_name, function_name, line_number)
10934      tree label_name;
10935      tree function_name;
10936      int line_number;
10937 {
10938   tree stub = build_tree_list (function_name, label_name);
10939   TREE_TYPE (stub) = build_int_2 (line_number, 0);
10940   TREE_CHAIN (stub) = stub_list;
10941   stub_list = stub;
10942 }
10943
10944 #define STUB_LABEL_NAME(STUB)     TREE_VALUE (STUB)
10945 #define STUB_FUNCTION_NAME(STUB)  TREE_PURPOSE (STUB)
10946 #define STUB_LINE_NUMBER(STUB)    TREE_INT_CST_LOW (TREE_TYPE (STUB))
10947
10948 /* OUTPUT_COMPILER_STUB outputs the compiler generated stub for
10949    handling procedure calls from the linked list and initializes the
10950    linked list.  */
10951
10952 void
10953 output_compiler_stub ()
10954 {
10955   char tmp_buf[256];
10956   char label_buf[256];
10957   char *label;
10958   tree tmp_stub, stub;
10959
10960   if (!flag_pic)
10961     for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
10962       {
10963         fprintf (asm_out_file,
10964                  "%s:\n", IDENTIFIER_POINTER(STUB_LABEL_NAME(stub)));
10965
10966 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
10967         if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
10968           fprintf (asm_out_file, "\t.stabd 68,0,%d\n", STUB_LINE_NUMBER(stub));
10969 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
10970
10971         if (IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub))[0] == '*')
10972           strcpy (label_buf,
10973                   IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub))+1);
10974         else
10975           {
10976             label_buf[0] = '_';
10977             strcpy (label_buf+1,
10978                     IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub)));
10979           }
10980
10981         strcpy (tmp_buf, "lis r12,hi16(");
10982         strcat (tmp_buf, label_buf);
10983         strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
10984         strcat (tmp_buf, label_buf);
10985         strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
10986         output_asm_insn (tmp_buf, 0);
10987
10988 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
10989         if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
10990           fprintf(asm_out_file, "\t.stabd 68,0,%d\n", STUB_LINE_NUMBER (stub));
10991 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
10992       }
10993
10994   stub_list = 0;
10995 }
10996
10997 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
10998    already there or not.  */
10999
11000 int
11001 no_previous_def (function_name)
11002      tree function_name;
11003 {
11004   tree stub;
11005   for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
11006     if (function_name == STUB_FUNCTION_NAME (stub))
11007       return 0;
11008   return 1;
11009 }
11010
11011 /* GET_PREV_LABEL gets the label name from the previous definition of
11012    the function.  */
11013
11014 tree
11015 get_prev_label (function_name)
11016      tree function_name;
11017 {
11018   tree stub;
11019   for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
11020     if (function_name == STUB_FUNCTION_NAME (stub))
11021       return STUB_LABEL_NAME (stub);
11022   return 0;
11023 }
11024
11025 /* INSN is either a function call or a millicode call.  It may have an
11026    unconditional jump in its delay slot.  
11027
11028    CALL_DEST is the routine we are calling.  */
11029
11030 char *
11031 output_call (insn, call_dest, operand_number)
11032      rtx insn;
11033      rtx call_dest;
11034      int operand_number;
11035 {
11036   static char buf[256];
11037   if (GET_CODE (call_dest) == SYMBOL_REF && TARGET_LONG_BRANCH && !flag_pic)
11038     {
11039       tree labelname;
11040       tree funname = get_identifier (XSTR (call_dest, 0));
11041       
11042       if (no_previous_def (funname))
11043         {
11044           int line_number;
11045           rtx label_rtx = gen_label_rtx ();
11046           char *label_buf, temp_buf[256];
11047           ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
11048                                        CODE_LABEL_NUMBER (label_rtx));
11049           label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
11050           labelname = get_identifier (label_buf);
11051           for (; insn && GET_CODE (insn) != NOTE; insn = PREV_INSN (insn));
11052           if (insn)
11053             line_number = NOTE_LINE_NUMBER (insn);
11054           add_compiler_stub (labelname, funname, line_number);
11055         }
11056       else
11057         labelname = get_prev_label (funname);
11058
11059       sprintf (buf, "jbsr %%z%d,%.246s",
11060                operand_number, IDENTIFIER_POINTER (labelname));
11061       return buf;
11062     }
11063   else
11064     {
11065       sprintf (buf, "bl %%z%d", operand_number);
11066       return buf;
11067     }
11068 }
11069
11070 #endif /* RS6000_LONG_BRANCH */
11071
11072 #define GEN_LOCAL_LABEL_FOR_SYMBOL(BUF,SYMBOL,LENGTH,N)         \
11073   do {                                                          \
11074     const char *const symbol_ = (SYMBOL);                       \
11075     char *buffer_ = (BUF);                                      \
11076     if (symbol_[0] == '"')                                      \
11077       {                                                         \
11078         sprintf(buffer_, "\"L%d$%s", (N), symbol_+1);           \
11079       }                                                         \
11080     else if (name_needs_quotes(symbol_))                        \
11081       {                                                         \
11082         sprintf(buffer_, "\"L%d$%s\"", (N), symbol_);           \
11083       }                                                         \
11084     else                                                        \
11085       {                                                         \
11086         sprintf(buffer_, "L%d$%s", (N), symbol_);               \
11087       }                                                         \
11088   } while (0)
11089
11090
11091 /* Generate PIC and indirect symbol stubs.  */
11092
11093 void
11094 machopic_output_stub (file, symb, stub)
11095      FILE *file;
11096      const char *symb, *stub;
11097 {
11098   unsigned int length;
11099   char *symbol_name, *lazy_ptr_name;
11100   char *local_label_0;
11101   static int label = 0;
11102
11103   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
11104   STRIP_NAME_ENCODING (symb, symb);
11105
11106   label += 1;
11107
11108   length = strlen (symb);
11109   symbol_name = alloca (length + 32);
11110   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
11111
11112   lazy_ptr_name = alloca (length + 32);
11113   GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
11114
11115   local_label_0 = alloca (length + 32);
11116   GEN_LOCAL_LABEL_FOR_SYMBOL (local_label_0, symb, length, 0);
11117
11118   if (flag_pic == 2)
11119     machopic_picsymbol_stub_section ();
11120   else
11121     machopic_symbol_stub_section ();
11122
11123   fprintf (file, "%s:\n", stub);
11124   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
11125
11126   if (flag_pic == 2)
11127     {
11128       fprintf (file, "\tmflr r0\n");
11129       fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
11130       fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
11131       fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
11132                lazy_ptr_name, local_label_0);
11133       fprintf (file, "\tmtlr r0\n");
11134       fprintf (file, "\tlwz r12,lo16(%s-%s)(r11)\n",
11135                lazy_ptr_name, local_label_0);
11136       fprintf (file, "\tmtctr r12\n");
11137       fprintf (file, "\taddi r11,r11,lo16(%s-%s)\n",
11138                lazy_ptr_name, local_label_0);
11139       fprintf (file, "\tbctr\n");
11140     }
11141   else
11142     fprintf (file, "non-pure not supported\n");
11143   
11144   machopic_lazy_symbol_ptr_section ();
11145   fprintf (file, "%s:\n", lazy_ptr_name);
11146   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
11147   fprintf (file, "\t.long dyld_stub_binding_helper\n");
11148 }
11149
11150 /* Legitimize PIC addresses.  If the address is already
11151    position-independent, we return ORIG.  Newly generated
11152    position-independent addresses go into a reg.  This is REG if non
11153    zero, otherwise we allocate register(s) as necessary.  */
11154
11155 #define SMALL_INT(X) ((unsigned) (INTVAL(X) + 0x8000) < 0x10000)
11156
11157 rtx
11158 rs6000_machopic_legitimize_pic_address (orig, mode, reg)
11159      rtx orig;
11160      enum machine_mode mode;
11161      rtx reg;
11162 {
11163   rtx base, offset;
11164
11165   if (reg == NULL && ! reload_in_progress && ! reload_completed)
11166     reg = gen_reg_rtx (Pmode);
11167
11168   if (GET_CODE (orig) == CONST)
11169     {
11170       if (GET_CODE (XEXP (orig, 0)) == PLUS
11171           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
11172         return orig;
11173
11174       if (GET_CODE (XEXP (orig, 0)) == PLUS)
11175         {
11176           base =
11177             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
11178                                                     Pmode, reg);
11179           offset =
11180             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
11181                                                     Pmode, reg);
11182         }
11183       else
11184         abort ();
11185
11186       if (GET_CODE (offset) == CONST_INT)
11187         {
11188           if (SMALL_INT (offset))
11189             return plus_constant (base, INTVAL (offset));
11190           else if (! reload_in_progress && ! reload_completed)
11191             offset = force_reg (Pmode, offset);
11192           else
11193             {
11194               rtx mem = force_const_mem (Pmode, orig);
11195               return machopic_legitimize_pic_address (mem, Pmode, reg);
11196             }
11197         }
11198       return gen_rtx (PLUS, Pmode, base, offset);
11199     }
11200
11201   /* Fall back on generic machopic code.  */
11202   return machopic_legitimize_pic_address (orig, mode, reg);
11203 }
11204
11205 /* This is just a placeholder to make linking work without having to
11206    add this to the generic Darwin EXTRA_SECTIONS.  If -mcall-aix is
11207    ever needed for Darwin (not too likely!) this would have to get a
11208    real definition.  */
11209
11210 void
11211 toc_section ()
11212 {
11213 }
11214
11215 #endif /* TARGET_MACHO */
11216
11217 #if TARGET_ELF
11218 static unsigned int
11219 rs6000_elf_section_type_flags (decl, name, reloc)
11220      tree decl;
11221      const char *name;
11222      int reloc;
11223 {
11224   unsigned int flags = default_section_type_flags (decl, name, reloc);
11225
11226   if (TARGET_RELOCATABLE)
11227     flags |= SECTION_WRITE;
11228
11229   return flags;
11230 }
11231
11232 /* Record an element in the table of global constructors.  SYMBOL is
11233    a SYMBOL_REF of the function to be called; PRIORITY is a number
11234    between 0 and MAX_INIT_PRIORITY.
11235
11236    This differs from default_named_section_asm_out_constructor in
11237    that we have special handling for -mrelocatable.  */
11238
11239 static void
11240 rs6000_elf_asm_out_constructor (symbol, priority)
11241      rtx symbol;
11242      int priority;
11243 {
11244   const char *section = ".ctors";
11245   char buf[16];
11246
11247   if (priority != DEFAULT_INIT_PRIORITY)
11248     {
11249       sprintf (buf, ".ctors.%.5u",
11250                /* Invert the numbering so the linker puts us in the proper
11251                   order; constructors are run from right to left, and the
11252                   linker sorts in increasing order.  */
11253                MAX_INIT_PRIORITY - priority);
11254       section = buf;
11255     }
11256
11257   named_section_flags (section, SECTION_WRITE);
11258   assemble_align (POINTER_SIZE);
11259
11260   if (TARGET_RELOCATABLE)
11261     {
11262       fputs ("\t.long (", asm_out_file);
11263       output_addr_const (asm_out_file, symbol);
11264       fputs (")@fixup\n", asm_out_file);
11265     }
11266   else
11267     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
11268 }
11269
11270 static void
11271 rs6000_elf_asm_out_destructor (symbol, priority)
11272      rtx symbol;
11273      int priority;
11274 {
11275   const char *section = ".dtors";
11276   char buf[16];
11277
11278   if (priority != DEFAULT_INIT_PRIORITY)
11279     {
11280       sprintf (buf, ".dtors.%.5u",
11281                /* Invert the numbering so the linker puts us in the proper
11282                   order; constructors are run from right to left, and the
11283                   linker sorts in increasing order.  */
11284                MAX_INIT_PRIORITY - priority);
11285       section = buf;
11286     }
11287
11288   named_section_flags (section, SECTION_WRITE);
11289   assemble_align (POINTER_SIZE);
11290
11291   if (TARGET_RELOCATABLE)
11292     {
11293       fputs ("\t.long (", asm_out_file);
11294       output_addr_const (asm_out_file, symbol);
11295       fputs (")@fixup\n", asm_out_file);
11296     }
11297   else
11298     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
11299 }
11300 #endif
11301
11302 #ifdef OBJECT_FORMAT_COFF
11303 static void
11304 xcoff_asm_named_section (name, flags)
11305      const char *name;
11306      unsigned int flags ATTRIBUTE_UNUSED;
11307 {
11308   fprintf (asm_out_file, "\t.csect %s\n", name);
11309 }
11310 #endif