OSDN Git Service

2002-07-24 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 /* Whether VRSAVE instructions should be generated.  */
78 int rs6000_altivec_vrsave;
79
80 /* String from -mvrsave= option.  */
81 const char *rs6000_altivec_vrsave_string;
82
83 /* Nonzero if we want SPE ABI extensions.  */
84 int rs6000_spe_abi;
85
86 /* Whether isel instructions should be generated.  */
87 int rs6000_isel;
88
89 /* Nonzero if we have FPRs.  */
90 int rs6000_fprs = 1;
91
92 /* String from -misel=.  */
93 const char *rs6000_isel_string;
94
95 /* Set to non-zero once AIX common-mode calls have been defined.  */
96 static int common_mode_defined;
97
98 /* Save information from a "cmpxx" operation until the branch or scc is
99    emitted.  */
100 rtx rs6000_compare_op0, rs6000_compare_op1;
101 int rs6000_compare_fp_p;
102
103 /* Label number of label created for -mrelocatable, to call to so we can
104    get the address of the GOT section */
105 int rs6000_pic_labelno;
106
107 #ifdef USING_ELFOS_H
108 /* Which abi to adhere to */
109 const char *rs6000_abi_name = RS6000_ABI_NAME;
110
111 /* Semantics of the small data area */
112 enum rs6000_sdata_type rs6000_sdata = SDATA_DATA;
113
114 /* Which small data model to use */
115 const char *rs6000_sdata_name = (char *)0;
116
117 /* Counter for labels which are to be placed in .fixup.  */
118 int fixuplabelno = 0;
119 #endif
120
121 /* ABI enumeration available for subtarget to use.  */
122 enum rs6000_abi rs6000_current_abi;
123
124 /* ABI string from -mabi= option.  */
125 const char *rs6000_abi_string;
126
127 /* Debug flags */
128 const char *rs6000_debug_name;
129 int rs6000_debug_stack;         /* debug stack applications */
130 int rs6000_debug_arg;           /* debug argument handling */
131
132 /* Flag to say the TOC is initialized */
133 int toc_initialized;
134 char toc_label_name[10];
135
136 /* Alias set for saves and restores from the rs6000 stack.  */
137 static int rs6000_sr_alias_set;
138
139 /* Call distance, overridden by -mlongcall and #pragma longcall(1).
140    The only place that looks at this is rs6000_set_default_type_attributes;
141    everywhere else should rely on the presence or absence of a longcall
142    attribute on the function declaration.  */
143 int rs6000_default_long_calls;
144 const char *rs6000_longcall_switch;
145
146 struct builtin_description
147 {
148   /* mask is not const because we're going to alter it below.  This
149      nonsense will go away when we rewrite the -march infrastructure
150      to give us more target flag bits.  */
151   unsigned int mask;
152   const enum insn_code icode;
153   const char *const name;
154   const enum rs6000_builtins code;
155 };
156
157 static void rs6000_add_gc_roots PARAMS ((void));
158 static int num_insns_constant_wide PARAMS ((HOST_WIDE_INT));
159 static rtx expand_block_move_mem PARAMS ((enum machine_mode, rtx, rtx));
160 static void validate_condition_mode 
161   PARAMS ((enum rtx_code, enum machine_mode));
162 static rtx rs6000_generate_compare PARAMS ((enum rtx_code));
163 static void rs6000_maybe_dead PARAMS ((rtx));
164 static void rs6000_emit_stack_tie PARAMS ((void));
165 static void rs6000_frame_related PARAMS ((rtx, rtx, HOST_WIDE_INT, rtx, rtx));
166 static void emit_frame_save PARAMS ((rtx, rtx, enum machine_mode,
167                                      unsigned int, int, int));
168 static rtx gen_frame_mem_offset PARAMS ((enum machine_mode, rtx, int));
169 static void rs6000_emit_allocate_stack PARAMS ((HOST_WIDE_INT, int));
170 static unsigned rs6000_hash_constant PARAMS ((rtx));
171 static unsigned toc_hash_function PARAMS ((const void *));
172 static int toc_hash_eq PARAMS ((const void *, const void *));
173 static int toc_hash_mark_entry PARAMS ((void **, void *));
174 static void toc_hash_mark_table PARAMS ((void *));
175 static int constant_pool_expr_1 PARAMS ((rtx, int *, int *));
176 static struct machine_function * rs6000_init_machine_status PARAMS ((void));
177 static bool rs6000_assemble_integer PARAMS ((rtx, unsigned int, int));
178 static int rs6000_ra_ever_killed PARAMS ((void));
179 static tree rs6000_handle_longcall_attribute PARAMS ((tree *, tree, tree, int, bool *));
180 const struct attribute_spec rs6000_attribute_table[];
181 static void rs6000_set_default_type_attributes PARAMS ((tree));
182 static void rs6000_output_function_prologue PARAMS ((FILE *, HOST_WIDE_INT));
183 static void rs6000_output_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
184 static rtx rs6000_emit_set_long_const PARAMS ((rtx,
185   HOST_WIDE_INT, HOST_WIDE_INT));
186 #if TARGET_ELF
187 static unsigned int rs6000_elf_section_type_flags PARAMS ((tree, const char *,
188                                                            int));
189 static void rs6000_elf_asm_out_constructor PARAMS ((rtx, int));
190 static void rs6000_elf_asm_out_destructor PARAMS ((rtx, int));
191 static void rs6000_elf_select_section PARAMS ((tree, int,
192                                                  unsigned HOST_WIDE_INT));
193 static void rs6000_elf_unique_section PARAMS ((tree, int));
194 static void rs6000_elf_select_rtx_section PARAMS ((enum machine_mode, rtx,
195                                                    unsigned HOST_WIDE_INT));
196 static void rs6000_elf_encode_section_info PARAMS ((tree, int));
197 static const char *rs6000_elf_strip_name_encoding PARAMS ((const char *));
198 #endif
199 #if TARGET_XCOFF
200 static void xcoff_asm_named_section PARAMS ((const char *, unsigned int));
201 static void rs6000_xcoff_select_section PARAMS ((tree, int,
202                                                  unsigned HOST_WIDE_INT));
203 static void rs6000_xcoff_unique_section PARAMS ((tree, int));
204 static void rs6000_xcoff_select_rtx_section PARAMS ((enum machine_mode, rtx,
205                                                      unsigned HOST_WIDE_INT));
206 static const char * rs6000_xcoff_strip_name_encoding PARAMS ((const char *));
207 #endif
208 static void rs6000_xcoff_encode_section_info PARAMS ((tree, int))
209      ATTRIBUTE_UNUSED;
210 static int rs6000_adjust_cost PARAMS ((rtx, rtx, rtx, int));
211 static int rs6000_adjust_priority PARAMS ((rtx, int));
212 static int rs6000_issue_rate PARAMS ((void));
213
214 static void rs6000_init_builtins PARAMS ((void));
215 static rtx rs6000_expand_unop_builtin PARAMS ((enum insn_code, tree, rtx));
216 static rtx rs6000_expand_binop_builtin PARAMS ((enum insn_code, tree, rtx));
217 static rtx rs6000_expand_ternop_builtin PARAMS ((enum insn_code, tree, rtx));
218 static rtx rs6000_expand_builtin PARAMS ((tree, rtx, rtx, enum machine_mode, int));
219 static void altivec_init_builtins PARAMS ((void));
220 static void rs6000_common_init_builtins PARAMS ((void));
221
222 static void enable_mask_for_builtins PARAMS ((struct builtin_description *,
223                                               int, enum rs6000_builtins,
224                                               enum rs6000_builtins));
225 static void spe_init_builtins PARAMS ((void));
226 static rtx spe_expand_builtin PARAMS ((tree, rtx, bool *));
227 static rtx spe_expand_predicate_builtin PARAMS ((enum insn_code, tree, rtx));
228 static rtx spe_expand_evsel_builtin PARAMS ((enum insn_code, tree, rtx));
229 static int rs6000_emit_int_cmove PARAMS ((rtx, rtx, rtx, rtx));
230
231 static rtx altivec_expand_builtin PARAMS ((tree, rtx, bool *));
232 static rtx altivec_expand_ld_builtin PARAMS ((tree, rtx, bool *));
233 static rtx altivec_expand_st_builtin PARAMS ((tree, rtx, bool *));
234 static rtx altivec_expand_dst_builtin PARAMS ((tree, rtx, bool *));
235 static rtx altivec_expand_abs_builtin PARAMS ((enum insn_code, tree, rtx));
236 static rtx altivec_expand_predicate_builtin PARAMS ((enum insn_code, const char *, tree, rtx));
237 static rtx altivec_expand_stv_builtin PARAMS ((enum insn_code, tree));
238 static void rs6000_parse_abi_options PARAMS ((void));
239 static void rs6000_parse_vrsave_option PARAMS ((void));
240 static void rs6000_parse_isel_option PARAMS ((void));
241 static int first_altivec_reg_to_save PARAMS ((void));
242 static unsigned int compute_vrsave_mask PARAMS ((void));
243 static void is_altivec_return_reg PARAMS ((rtx, void *));
244 static rtx generate_set_vrsave PARAMS ((rtx, rs6000_stack_t *, int));
245 static void altivec_frame_fixup PARAMS ((rtx, rtx, HOST_WIDE_INT));
246 static int easy_vector_constant PARAMS ((rtx));
247 \f
248 /* Default register names.  */
249 char rs6000_reg_names[][8] =
250 {
251       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
252       "8",  "9", "10", "11", "12", "13", "14", "15",
253      "16", "17", "18", "19", "20", "21", "22", "23",
254      "24", "25", "26", "27", "28", "29", "30", "31",
255       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
256       "8",  "9", "10", "11", "12", "13", "14", "15",
257      "16", "17", "18", "19", "20", "21", "22", "23",
258      "24", "25", "26", "27", "28", "29", "30", "31",
259      "mq", "lr", "ctr","ap",
260       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
261       "xer",
262       /* AltiVec registers.  */
263       "0",  "1",  "2",  "3",  "4",  "5",  "6", "7",
264       "8",  "9",  "10", "11", "12", "13", "14", "15",
265       "16", "17", "18", "19", "20", "21", "22", "23",
266       "24", "25", "26", "27", "28", "29", "30", "31",
267       "vrsave"
268 };
269
270 #ifdef TARGET_REGNAMES
271 static const char alt_reg_names[][8] =
272 {
273    "%r0",   "%r1",  "%r2",  "%r3",  "%r4",  "%r5",  "%r6",  "%r7",
274    "%r8",   "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
275   "%r16",  "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
276   "%r24",  "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
277    "%f0",   "%f1",  "%f2",  "%f3",  "%f4",  "%f5",  "%f6",  "%f7",
278    "%f8",   "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
279   "%f16",  "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
280   "%f24",  "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
281     "mq",    "lr",  "ctr",   "ap",
282   "%cr0",  "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
283    "xer",
284    /* AltiVec registers.  */
285    "%v0",  "%v1",  "%v2",  "%v3",  "%v4",  "%v5",  "%v6", "%v7",
286    "%v8",  "%v9",  "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
287    "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
288    "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
289    "vrsave"
290 };
291 #endif
292 \f
293 #ifndef MASK_STRICT_ALIGN
294 #define MASK_STRICT_ALIGN 0
295 #endif
296 \f
297 /* Initialize the GCC target structure.  */
298 #undef TARGET_ATTRIBUTE_TABLE
299 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
300 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
301 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
302
303 #undef TARGET_ASM_ALIGNED_DI_OP
304 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
305
306 /* Default unaligned ops are only provided for ELF.  Find the ops needed
307    for non-ELF systems.  */
308 #ifndef OBJECT_FORMAT_ELF
309 #if TARGET_XCOFF
310 /* For XCOFF.  rs6000_assemble_integer will handle unaligned DIs on
311    64-bit targets.  */
312 #undef TARGET_ASM_UNALIGNED_HI_OP
313 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
314 #undef TARGET_ASM_UNALIGNED_SI_OP
315 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
316 #undef TARGET_ASM_UNALIGNED_DI_OP
317 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
318 #else
319 /* For Darwin.  */
320 #undef TARGET_ASM_UNALIGNED_HI_OP
321 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
322 #undef TARGET_ASM_UNALIGNED_SI_OP
323 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
324 #endif
325 #endif
326
327 /* This hook deals with fixups for relocatable code and DI-mode objects
328    in 64-bit code.  */
329 #undef TARGET_ASM_INTEGER
330 #define TARGET_ASM_INTEGER rs6000_assemble_integer
331
332 #undef TARGET_ASM_FUNCTION_PROLOGUE
333 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
334 #undef TARGET_ASM_FUNCTION_EPILOGUE
335 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
336
337 #if TARGET_ELF
338 #undef TARGET_SECTION_TYPE_FLAGS
339 #define TARGET_SECTION_TYPE_FLAGS  rs6000_elf_section_type_flags
340 #endif
341
342 #undef TARGET_SCHED_ISSUE_RATE
343 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
344 #undef TARGET_SCHED_ADJUST_COST
345 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
346 #undef TARGET_SCHED_ADJUST_PRIORITY
347 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
348
349 #undef TARGET_INIT_BUILTINS
350 #define TARGET_INIT_BUILTINS rs6000_init_builtins
351
352 #undef TARGET_EXPAND_BUILTIN
353 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
354
355 /* The VRSAVE bitmask puts bit %v0 as the most significant bit.  */
356 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
357
358 struct gcc_target targetm = TARGET_INITIALIZER;
359 \f
360 /* Override command line options.  Mostly we process the processor
361    type and sometimes adjust other TARGET_ options.  */
362
363 void
364 rs6000_override_options (default_cpu)
365      const char *default_cpu;
366 {
367   size_t i, j;
368   struct rs6000_cpu_select *ptr;
369
370   /* Simplify the entries below by making a mask for any POWER
371      variant and any PowerPC variant.  */
372
373 #define POWER_MASKS (MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING)
374 #define POWERPC_MASKS (MASK_POWERPC | MASK_PPC_GPOPT \
375                        | MASK_PPC_GFXOPT | MASK_POWERPC64)
376 #define POWERPC_OPT_MASKS (MASK_PPC_GPOPT | MASK_PPC_GFXOPT)
377
378   static struct ptt
379     {
380       const char *const name;           /* Canonical processor name.  */
381       const enum processor_type processor; /* Processor type enum value.  */
382       const int target_enable;  /* Target flags to enable.  */
383       const int target_disable; /* Target flags to disable.  */
384     } const processor_target_table[]
385       = {{"common", PROCESSOR_COMMON, MASK_NEW_MNEMONICS,
386             POWER_MASKS | POWERPC_MASKS},
387          {"power", PROCESSOR_POWER,
388             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
389             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
390          {"power2", PROCESSOR_POWER,
391             MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING,
392             POWERPC_MASKS | MASK_NEW_MNEMONICS},
393          {"power3", PROCESSOR_PPC630,
394             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
395             POWER_MASKS | MASK_PPC_GPOPT},
396          {"power4", PROCESSOR_POWER4,
397             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
398             POWER_MASKS | MASK_PPC_GPOPT},
399          {"powerpc", PROCESSOR_POWERPC,
400             MASK_POWERPC | MASK_NEW_MNEMONICS,
401             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
402          {"powerpc64", PROCESSOR_POWERPC64,
403             MASK_POWERPC | MASK_POWERPC64 | MASK_NEW_MNEMONICS,
404             POWER_MASKS | POWERPC_OPT_MASKS},
405          {"rios", PROCESSOR_RIOS1,
406             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
407             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
408          {"rios1", PROCESSOR_RIOS1,
409             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
410             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
411          {"rsc", PROCESSOR_PPC601,
412             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
413             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
414          {"rsc1", PROCESSOR_PPC601,
415             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
416             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
417          {"rios2", PROCESSOR_RIOS2,
418             MASK_POWER | MASK_MULTIPLE | MASK_STRING | MASK_POWER2,
419             POWERPC_MASKS | MASK_NEW_MNEMONICS},
420          {"rs64a", PROCESSOR_RS64A,
421             MASK_POWERPC | MASK_NEW_MNEMONICS,
422             POWER_MASKS | POWERPC_OPT_MASKS},
423          {"401", PROCESSOR_PPC403,
424             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
425             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
426          {"403", PROCESSOR_PPC403,
427             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS | MASK_STRICT_ALIGN,
428             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
429          {"405", PROCESSOR_PPC405,
430             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
431             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
432          {"505", PROCESSOR_MPCCORE,
433             MASK_POWERPC | MASK_NEW_MNEMONICS,
434             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
435          {"601", PROCESSOR_PPC601,
436             MASK_POWER | MASK_POWERPC | MASK_NEW_MNEMONICS | MASK_MULTIPLE | MASK_STRING,
437             MASK_POWER2 | POWERPC_OPT_MASKS | MASK_POWERPC64},
438          {"602", PROCESSOR_PPC603,
439             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
440             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
441          {"603", PROCESSOR_PPC603,
442             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
443             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
444          {"603e", PROCESSOR_PPC603,
445             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
446             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
447          {"ec603e", PROCESSOR_PPC603,
448             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
449             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
450          {"604", PROCESSOR_PPC604,
451             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
452             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
453          {"604e", PROCESSOR_PPC604e,
454             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
455             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
456          {"620", PROCESSOR_PPC620,
457             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
458             POWER_MASKS | MASK_PPC_GPOPT},
459          {"630", PROCESSOR_PPC630,
460             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
461             POWER_MASKS | MASK_PPC_GPOPT},
462          {"740", PROCESSOR_PPC750,
463             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
464             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
465          {"750", PROCESSOR_PPC750,
466             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
467             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
468          {"7400", PROCESSOR_PPC7400,
469             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
470             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
471          {"7450", PROCESSOR_PPC7450,
472             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
473             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
474          {"8540", PROCESSOR_PPC8540,
475             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
476             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
477          {"801", PROCESSOR_MPCCORE,
478             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
479             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
480          {"821", PROCESSOR_MPCCORE,
481             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
482             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
483          {"823", PROCESSOR_MPCCORE,
484             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
485             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
486          {"860", PROCESSOR_MPCCORE,
487             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
488             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64}};
489
490   const size_t ptt_size = ARRAY_SIZE (processor_target_table);
491
492   /* Save current -mmultiple/-mno-multiple status.  */
493   int multiple = TARGET_MULTIPLE;
494   /* Save current -mstring/-mno-string status.  */
495   int string = TARGET_STRING;
496
497   /* Identify the processor type.  */
498   rs6000_select[0].string = default_cpu;
499   rs6000_cpu = TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT;
500
501   for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
502     {
503       ptr = &rs6000_select[i];
504       if (ptr->string != (char *)0 && ptr->string[0] != '\0')
505         {
506           for (j = 0; j < ptt_size; j++)
507             if (! strcmp (ptr->string, processor_target_table[j].name))
508               {
509                 if (ptr->set_tune_p)
510                   rs6000_cpu = processor_target_table[j].processor;
511
512                 if (ptr->set_arch_p)
513                   {
514                     target_flags |= processor_target_table[j].target_enable;
515                     target_flags &= ~processor_target_table[j].target_disable;
516                   }
517                 break;
518               }
519
520           if (j == ptt_size)
521             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
522         }
523     }
524
525   if (rs6000_cpu == PROCESSOR_PPC8540)
526     rs6000_isel = 1;
527
528   /* If we are optimizing big endian systems for space, use the store
529      multiple instructions.  */
530   if (BYTES_BIG_ENDIAN && optimize_size)
531     target_flags |= MASK_MULTIPLE;
532
533   /* If -mmultiple or -mno-multiple was explicitly used, don't
534      override with the processor default */
535   if (TARGET_MULTIPLE_SET)
536     target_flags = (target_flags & ~MASK_MULTIPLE) | multiple;
537
538   /* If -mstring or -mno-string was explicitly used, don't override
539      with the processor default.  */
540   if (TARGET_STRING_SET)
541     target_flags = (target_flags & ~MASK_STRING) | string;
542
543   /* Don't allow -mmultiple or -mstring on little endian systems
544      unless the cpu is a 750, because the hardware doesn't support the
545      instructions used in little endian mode, and causes an alignment
546      trap.  The 750 does not cause an alignment trap (except when the
547      target is unaligned).  */
548
549   if (! BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
550     {
551       if (TARGET_MULTIPLE)
552         {
553           target_flags &= ~MASK_MULTIPLE;
554           if (TARGET_MULTIPLE_SET)
555             warning ("-mmultiple is not supported on little endian systems");
556         }
557
558       if (TARGET_STRING)
559         {
560           target_flags &= ~MASK_STRING;
561           if (TARGET_STRING_SET)
562             warning ("-mstring is not supported on little endian systems");
563         }
564     }
565
566   if (flag_pic != 0 && DEFAULT_ABI == ABI_AIX)
567     {
568       flag_pic = 0;
569
570       if (extra_warnings)
571         warning ("-f%s ignored (all code is position independent)",
572                  (flag_pic > 1) ? "PIC" : "pic");
573     }
574
575 #ifdef XCOFF_DEBUGGING_INFO
576   if (flag_function_sections && (write_symbols != NO_DEBUG)
577       && DEFAULT_ABI == ABI_AIX)
578     {
579       warning ("-ffunction-sections disabled on AIX when debugging");
580       flag_function_sections = 0;
581     }
582
583   if (flag_data_sections && (DEFAULT_ABI == ABI_AIX))
584     {
585       warning ("-fdata-sections not supported on AIX");
586       flag_data_sections = 0;
587     }
588 #endif
589
590   /* Set debug flags */
591   if (rs6000_debug_name)
592     {
593       if (! strcmp (rs6000_debug_name, "all"))
594         rs6000_debug_stack = rs6000_debug_arg = 1;
595       else if (! strcmp (rs6000_debug_name, "stack"))
596         rs6000_debug_stack = 1;
597       else if (! strcmp (rs6000_debug_name, "arg"))
598         rs6000_debug_arg = 1;
599       else
600         error ("unknown -mdebug-%s switch", rs6000_debug_name);
601     }
602
603   /* Set size of long double */
604   rs6000_long_double_type_size = 64;
605   if (rs6000_long_double_size_string)
606     {
607       char *tail;
608       int size = strtol (rs6000_long_double_size_string, &tail, 10);
609       if (*tail != '\0' || (size != 64 && size != 128))
610         error ("Unknown switch -mlong-double-%s",
611                rs6000_long_double_size_string);
612       else
613         rs6000_long_double_type_size = size;
614     }
615
616   /* Handle -mabi= options.  */
617   rs6000_parse_abi_options ();
618
619   /* Handle -mvrsave= option.  */
620   rs6000_parse_vrsave_option ();
621
622   /* Handle -misel= option.  */
623   rs6000_parse_isel_option ();
624
625 #ifdef SUBTARGET_OVERRIDE_OPTIONS
626   SUBTARGET_OVERRIDE_OPTIONS;
627 #endif
628 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
629   SUBSUBTARGET_OVERRIDE_OPTIONS;
630 #endif
631
632   /* Handle -m(no-)longcall option.  This is a bit of a cheap hack,
633      using TARGET_OPTIONS to handle a toggle switch, but we're out of
634      bits in target_flags so TARGET_SWITCHES cannot be used.
635      Assumption here is that rs6000_longcall_switch points into the
636      text of the complete option, rather than being a copy, so we can
637      scan back for the presence or absence of the no- modifier.  */
638   if (rs6000_longcall_switch)
639     {
640       const char *base = rs6000_longcall_switch;
641       while (base[-1] != 'm') base--;
642
643       if (*rs6000_longcall_switch != '\0')
644         error ("invalid option `%s'", base);
645       rs6000_default_long_calls = (base[0] != 'n');
646     }
647
648 #ifdef TARGET_REGNAMES
649   /* If the user desires alternate register names, copy in the
650      alternate names now.  */
651   if (TARGET_REGNAMES)
652     memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
653 #endif
654
655   /* Set TARGET_AIX_STRUCT_RET last, after the ABI is determined.
656      If -maix-struct-return or -msvr4-struct-return was explicitly
657      used, don't override with the ABI default.  */
658   if (!(target_flags & MASK_AIX_STRUCT_RET_SET))
659     {
660       if (DEFAULT_ABI == ABI_V4 && !DRAFT_V4_STRUCT_RET)
661         target_flags = (target_flags & ~MASK_AIX_STRUCT_RET);
662       else
663         target_flags |= MASK_AIX_STRUCT_RET;
664     }
665
666   /* Register global variables with the garbage collector.  */
667   rs6000_add_gc_roots ();
668
669   /* Allocate an alias set for register saves & restores from stack.  */
670   rs6000_sr_alias_set = new_alias_set ();
671
672   if (TARGET_TOC) 
673     ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
674
675   /* We can only guarantee the availability of DI pseudo-ops when
676      assembling for 64-bit targets.  */
677   if (!TARGET_64BIT)
678     {
679       targetm.asm_out.aligned_op.di = NULL;
680       targetm.asm_out.unaligned_op.di = NULL;
681     }
682
683   /* Arrange to save and restore machine status around nested functions.  */
684   init_machine_status = rs6000_init_machine_status;
685 }
686
687 /* Handle -misel= option.  */
688 static void
689 rs6000_parse_isel_option ()
690 {
691   if (rs6000_isel_string == 0)
692     return;
693   else if (! strcmp (rs6000_isel_string, "yes"))
694     rs6000_isel = 1;
695   else if (! strcmp (rs6000_isel_string, "no"))
696     rs6000_isel = 0;
697   else
698     error ("unknown -misel= option specified: '%s'",
699          rs6000_isel_string);
700 }
701
702 /* Handle -mvrsave= options.  */
703 static void
704 rs6000_parse_vrsave_option ()
705 {
706   /* Generate VRSAVE instructions by default.  */
707   if (rs6000_altivec_vrsave_string == 0
708       || ! strcmp (rs6000_altivec_vrsave_string, "yes"))
709     rs6000_altivec_vrsave = 1;
710   else if (! strcmp (rs6000_altivec_vrsave_string, "no"))
711     rs6000_altivec_vrsave = 0;
712   else
713     error ("unknown -mvrsave= option specified: '%s'",
714            rs6000_altivec_vrsave_string);
715 }
716
717 /* Handle -mabi= options.  */
718 static void
719 rs6000_parse_abi_options ()
720 {
721   if (rs6000_abi_string == 0)
722     return;
723   else if (! strcmp (rs6000_abi_string, "altivec"))
724     rs6000_altivec_abi = 1;
725   else if (! strcmp (rs6000_abi_string, "no-altivec"))
726     rs6000_altivec_abi = 0;
727   else if (! strcmp (rs6000_abi_string, "spe"))
728     rs6000_spe_abi = 1;
729   else if (! strcmp (rs6000_abi_string, "no-spe"))
730     rs6000_spe_abi = 0;
731   else
732     error ("unknown ABI specified: '%s'", rs6000_abi_string);
733 }
734
735 void
736 optimization_options (level, size)
737      int level ATTRIBUTE_UNUSED;
738      int size ATTRIBUTE_UNUSED;
739 {
740 }
741 \f
742 /* Do anything needed at the start of the asm file.  */
743
744 void
745 rs6000_file_start (file, default_cpu)
746      FILE *file;
747      const char *default_cpu;
748 {
749   size_t i;
750   char buffer[80];
751   const char *start = buffer;
752   struct rs6000_cpu_select *ptr;
753
754   if (flag_verbose_asm)
755     {
756       sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
757       rs6000_select[0].string = default_cpu;
758
759       for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
760         {
761           ptr = &rs6000_select[i];
762           if (ptr->string != (char *)0 && ptr->string[0] != '\0')
763             {
764               fprintf (file, "%s %s%s", start, ptr->name, ptr->string);
765               start = "";
766             }
767         }
768
769 #ifdef USING_ELFOS_H
770       switch (rs6000_sdata)
771         {
772         case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
773         case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
774         case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
775         case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
776         }
777
778       if (rs6000_sdata && g_switch_value)
779         {
780           fprintf (file, "%s -G %d", start, g_switch_value);
781           start = "";
782         }
783 #endif
784
785       if (*start == '\0')
786         putc ('\n', file);
787     }
788 }
789 \f
790 /* Return non-zero if this function is known to have a null epilogue.  */
791
792 int
793 direct_return ()
794 {
795   if (reload_completed)
796     {
797       rs6000_stack_t *info = rs6000_stack_info ();
798
799       if (info->first_gp_reg_save == 32
800           && info->first_fp_reg_save == 64
801           && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
802           && ! info->lr_save_p
803           && ! info->cr_save_p
804           && info->vrsave_mask == 0
805           && ! info->push_p)
806         return 1;
807     }
808
809   return 0;
810 }
811
812 /* Returns 1 always.  */
813
814 int
815 any_operand (op, mode)
816      rtx op ATTRIBUTE_UNUSED;
817      enum machine_mode mode ATTRIBUTE_UNUSED;
818 {
819   return 1;
820 }
821
822 /* Returns 1 if op is the count register.  */
823 int
824 count_register_operand (op, mode)
825      rtx op;
826      enum machine_mode mode ATTRIBUTE_UNUSED;
827 {
828   if (GET_CODE (op) != REG)
829     return 0;
830
831   if (REGNO (op) == COUNT_REGISTER_REGNUM)
832     return 1;
833
834   if (REGNO (op) > FIRST_PSEUDO_REGISTER)
835     return 1;
836
837   return 0;
838 }
839
840 /* Returns 1 if op is an altivec register.  */
841 int
842 altivec_register_operand (op, mode)
843      rtx op;
844      enum machine_mode mode ATTRIBUTE_UNUSED;
845 {
846   
847   return (register_operand (op, mode)
848           && (GET_CODE (op) != REG
849               || REGNO (op) > FIRST_PSEUDO_REGISTER
850               || ALTIVEC_REGNO_P (REGNO (op))));
851 }
852
853 int
854 xer_operand (op, mode)
855      rtx op;
856      enum machine_mode mode ATTRIBUTE_UNUSED;
857 {
858   if (GET_CODE (op) != REG)
859     return 0;
860
861   if (XER_REGNO_P (REGNO (op)))
862     return 1;
863
864   return 0;
865 }
866
867 /* Return 1 if OP is a signed 8-bit constant.  Int multiplication
868    by such constants completes more quickly.  */
869
870 int
871 s8bit_cint_operand (op, mode)
872      rtx op;
873      enum machine_mode mode ATTRIBUTE_UNUSED;
874 {
875   return ( GET_CODE (op) == CONST_INT
876           && (INTVAL (op) >= -128 && INTVAL (op) <= 127));
877 }
878
879 /* Return 1 if OP is a constant that can fit in a D field.  */
880
881 int
882 short_cint_operand (op, mode)
883      rtx op;
884      enum machine_mode mode ATTRIBUTE_UNUSED;
885 {
886   return (GET_CODE (op) == CONST_INT
887           && CONST_OK_FOR_LETTER_P (INTVAL (op), 'I'));
888 }
889
890 /* Similar for an unsigned D field.  */
891
892 int
893 u_short_cint_operand (op, mode)
894      rtx op;
895      enum machine_mode mode ATTRIBUTE_UNUSED;
896 {
897   return (GET_CODE (op) == CONST_INT
898           && CONST_OK_FOR_LETTER_P (INTVAL (op) & GET_MODE_MASK (mode), 'K'));
899 }
900
901 /* Return 1 if OP is a CONST_INT that cannot fit in a signed D field.  */
902
903 int
904 non_short_cint_operand (op, mode)
905      rtx op;
906      enum machine_mode mode ATTRIBUTE_UNUSED;
907 {
908   return (GET_CODE (op) == CONST_INT
909           && (unsigned HOST_WIDE_INT) (INTVAL (op) + 0x8000) >= 0x10000);
910 }
911
912 /* Returns 1 if OP is a CONST_INT that is a positive value
913    and an exact power of 2.  */
914
915 int
916 exact_log2_cint_operand (op, mode)
917      rtx op;
918      enum machine_mode mode ATTRIBUTE_UNUSED;
919 {
920   return (GET_CODE (op) == CONST_INT
921           && INTVAL (op) > 0
922           && exact_log2 (INTVAL (op)) >= 0);
923 }
924
925 /* Returns 1 if OP is a register that is not special (i.e., not MQ,
926    ctr, or lr).  */
927
928 int
929 gpc_reg_operand (op, mode)
930      rtx op;
931      enum machine_mode mode;
932 {
933   return (register_operand (op, mode)
934           && (GET_CODE (op) != REG
935               || (REGNO (op) >= ARG_POINTER_REGNUM 
936                   && !XER_REGNO_P (REGNO (op)))
937               || REGNO (op) < MQ_REGNO));
938 }
939
940 /* Returns 1 if OP is either a pseudo-register or a register denoting a
941    CR field.  */
942
943 int
944 cc_reg_operand (op, mode)
945      rtx op;
946      enum machine_mode mode;
947 {
948   return (register_operand (op, mode)
949           && (GET_CODE (op) != REG
950               || REGNO (op) >= FIRST_PSEUDO_REGISTER
951               || CR_REGNO_P (REGNO (op))));
952 }
953
954 /* Returns 1 if OP is either a pseudo-register or a register denoting a
955    CR field that isn't CR0.  */
956
957 int
958 cc_reg_not_cr0_operand (op, mode)
959      rtx op;
960      enum machine_mode mode;
961 {
962   return (register_operand (op, mode)
963           && (GET_CODE (op) != REG
964               || REGNO (op) >= FIRST_PSEUDO_REGISTER
965               || CR_REGNO_NOT_CR0_P (REGNO (op))));
966 }
967
968 /* Returns 1 if OP is either a constant integer valid for a D-field or
969    a non-special register.  If a register, it must be in the proper
970    mode unless MODE is VOIDmode.  */
971
972 int
973 reg_or_short_operand (op, mode)
974       rtx op;
975       enum machine_mode mode;
976 {
977   return short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
978 }
979
980 /* Similar, except check if the negation of the constant would be
981    valid for a D-field.  */
982
983 int
984 reg_or_neg_short_operand (op, mode)
985       rtx op;
986       enum machine_mode mode;
987 {
988   if (GET_CODE (op) == CONST_INT)
989     return CONST_OK_FOR_LETTER_P (INTVAL (op), 'P');
990
991   return gpc_reg_operand (op, mode);
992 }
993
994 /* Returns 1 if OP is either a constant integer valid for a DS-field or
995    a non-special register.  If a register, it must be in the proper
996    mode unless MODE is VOIDmode.  */
997
998 int
999 reg_or_aligned_short_operand (op, mode)
1000       rtx op;
1001       enum machine_mode mode;
1002 {
1003   if (gpc_reg_operand (op, mode))
1004     return 1;
1005   else if (short_cint_operand (op, mode) && !(INTVAL (op) & 3))
1006     return 1;
1007
1008   return 0;
1009 }
1010
1011
1012 /* Return 1 if the operand is either a register or an integer whose
1013    high-order 16 bits are zero.  */
1014
1015 int
1016 reg_or_u_short_operand (op, mode)
1017      rtx op;
1018      enum machine_mode mode;
1019 {
1020   return u_short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
1021 }
1022
1023 /* Return 1 is the operand is either a non-special register or ANY
1024    constant integer.  */
1025
1026 int
1027 reg_or_cint_operand (op, mode)
1028     rtx op;
1029     enum machine_mode mode;
1030 {
1031   return (GET_CODE (op) == CONST_INT || gpc_reg_operand (op, mode));
1032 }
1033
1034 /* Return 1 is the operand is either a non-special register or ANY
1035    32-bit signed constant integer.  */
1036
1037 int
1038 reg_or_arith_cint_operand (op, mode)
1039     rtx op;
1040     enum machine_mode mode;
1041 {
1042   return (gpc_reg_operand (op, mode)
1043           || (GET_CODE (op) == CONST_INT
1044 #if HOST_BITS_PER_WIDE_INT != 32
1045               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80000000)
1046                   < (unsigned HOST_WIDE_INT) 0x100000000ll)
1047 #endif
1048               ));
1049 }
1050
1051 /* Return 1 is the operand is either a non-special register or a 32-bit
1052    signed constant integer valid for 64-bit addition.  */
1053
1054 int
1055 reg_or_add_cint64_operand (op, mode)
1056     rtx op;
1057     enum machine_mode mode;
1058 {
1059   return (gpc_reg_operand (op, mode)
1060           || (GET_CODE (op) == CONST_INT
1061 #if HOST_BITS_PER_WIDE_INT == 32
1062               && INTVAL (op) < 0x7fff8000
1063 #else
1064               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80008000)
1065                   < 0x100000000ll)
1066 #endif
1067               ));
1068 }
1069
1070 /* Return 1 is the operand is either a non-special register or a 32-bit
1071    signed constant integer valid for 64-bit subtraction.  */
1072
1073 int
1074 reg_or_sub_cint64_operand (op, mode)
1075     rtx op;
1076     enum machine_mode mode;
1077 {
1078   return (gpc_reg_operand (op, mode)
1079           || (GET_CODE (op) == CONST_INT
1080 #if HOST_BITS_PER_WIDE_INT == 32
1081               && (- INTVAL (op)) < 0x7fff8000
1082 #else
1083               && ((unsigned HOST_WIDE_INT) ((- INTVAL (op)) + 0x80008000)
1084                   < 0x100000000ll)
1085 #endif
1086               ));
1087 }
1088
1089 /* Return 1 is the operand is either a non-special register or ANY
1090    32-bit unsigned constant integer.  */
1091
1092 int
1093 reg_or_logical_cint_operand (op, mode)
1094     rtx op;
1095     enum machine_mode mode;
1096 {
1097   if (GET_CODE (op) == CONST_INT)
1098     {
1099       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
1100         {
1101           if (GET_MODE_BITSIZE (mode) <= 32)
1102             abort ();
1103
1104           if (INTVAL (op) < 0)
1105             return 0;
1106         }
1107
1108       return ((INTVAL (op) & GET_MODE_MASK (mode)
1109                & (~ (unsigned HOST_WIDE_INT) 0xffffffff)) == 0);
1110     }
1111   else if (GET_CODE (op) == CONST_DOUBLE)
1112     {
1113       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
1114           || mode != DImode)
1115         abort ();
1116
1117       return CONST_DOUBLE_HIGH (op) == 0;
1118     }
1119   else 
1120     return gpc_reg_operand (op, mode);
1121 }
1122
1123 /* Return 1 if the operand is an operand that can be loaded via the GOT.  */
1124
1125 int
1126 got_operand (op, mode)
1127      rtx op;
1128      enum machine_mode mode ATTRIBUTE_UNUSED;
1129 {
1130   return (GET_CODE (op) == SYMBOL_REF
1131           || GET_CODE (op) == CONST
1132           || GET_CODE (op) == LABEL_REF);
1133 }
1134
1135 /* Return 1 if the operand is a simple references that can be loaded via
1136    the GOT (labels involving addition aren't allowed).  */
1137
1138 int
1139 got_no_const_operand (op, mode)
1140      rtx op;
1141      enum machine_mode mode ATTRIBUTE_UNUSED;
1142 {
1143   return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF);
1144 }
1145
1146 /* Return the number of instructions it takes to form a constant in an
1147    integer register.  */
1148
1149 static int
1150 num_insns_constant_wide (value)
1151      HOST_WIDE_INT value;
1152 {
1153   /* signed constant loadable with {cal|addi} */
1154   if (CONST_OK_FOR_LETTER_P (value, 'I'))
1155     return 1;
1156
1157   /* constant loadable with {cau|addis} */
1158   else if (CONST_OK_FOR_LETTER_P (value, 'L'))
1159     return 1;
1160
1161 #if HOST_BITS_PER_WIDE_INT == 64
1162   else if (TARGET_POWERPC64)
1163     {
1164       HOST_WIDE_INT low  = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
1165       HOST_WIDE_INT high = value >> 31;
1166
1167       if (high == 0 || high == -1)
1168         return 2;
1169
1170       high >>= 1;
1171
1172       if (low == 0)
1173         return num_insns_constant_wide (high) + 1;
1174       else
1175         return (num_insns_constant_wide (high)
1176                 + num_insns_constant_wide (low) + 1);
1177     }
1178 #endif
1179
1180   else
1181     return 2;
1182 }
1183
1184 int
1185 num_insns_constant (op, mode)
1186      rtx op;
1187      enum machine_mode mode;
1188 {
1189   if (GET_CODE (op) == CONST_INT)
1190     {
1191 #if HOST_BITS_PER_WIDE_INT == 64
1192       if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
1193           && mask64_operand (op, mode))
1194             return 2;
1195       else
1196 #endif
1197         return num_insns_constant_wide (INTVAL (op));
1198     }
1199
1200   else if (GET_CODE (op) == CONST_DOUBLE && mode == SFmode)
1201     {
1202       long l;
1203       REAL_VALUE_TYPE rv;
1204
1205       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1206       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1207       return num_insns_constant_wide ((HOST_WIDE_INT) l);
1208     }
1209
1210   else if (GET_CODE (op) == CONST_DOUBLE)
1211     {
1212       HOST_WIDE_INT low;
1213       HOST_WIDE_INT high;
1214       long l[2];
1215       REAL_VALUE_TYPE rv;
1216       int endian = (WORDS_BIG_ENDIAN == 0);
1217
1218       if (mode == VOIDmode || mode == DImode)
1219         {
1220           high = CONST_DOUBLE_HIGH (op);
1221           low  = CONST_DOUBLE_LOW (op);
1222         }
1223       else
1224         {
1225           REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1226           REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
1227           high = l[endian];
1228           low  = l[1 - endian];
1229         }
1230
1231       if (TARGET_32BIT)
1232         return (num_insns_constant_wide (low)
1233                 + num_insns_constant_wide (high));
1234
1235       else
1236         {
1237           if (high == 0 && low >= 0)
1238             return num_insns_constant_wide (low);
1239
1240           else if (high == -1 && low < 0)
1241             return num_insns_constant_wide (low);
1242
1243           else if (mask64_operand (op, mode))
1244             return 2;
1245
1246           else if (low == 0)
1247             return num_insns_constant_wide (high) + 1;
1248
1249           else
1250             return (num_insns_constant_wide (high)
1251                     + num_insns_constant_wide (low) + 1);
1252         }
1253     }
1254
1255   else
1256     abort ();
1257 }
1258
1259 /* Return 1 if the operand is a CONST_DOUBLE and it can be put into a
1260    register with one instruction per word.  We only do this if we can
1261    safely read CONST_DOUBLE_{LOW,HIGH}.  */
1262
1263 int
1264 easy_fp_constant (op, mode)
1265      rtx op;
1266      enum machine_mode mode;
1267 {
1268   if (GET_CODE (op) != CONST_DOUBLE
1269       || GET_MODE (op) != mode
1270       || (GET_MODE_CLASS (mode) != MODE_FLOAT && mode != DImode))
1271     return 0;
1272
1273   /* Consider all constants with -msoft-float to be easy.  */
1274   if ((TARGET_SOFT_FLOAT || !TARGET_FPRS)
1275       && mode != DImode)
1276     return 1;
1277
1278   /* If we are using V.4 style PIC, consider all constants to be hard.  */
1279   if (flag_pic && DEFAULT_ABI == ABI_V4)
1280     return 0;
1281
1282 #ifdef TARGET_RELOCATABLE
1283   /* Similarly if we are using -mrelocatable, consider all constants
1284      to be hard.  */
1285   if (TARGET_RELOCATABLE)
1286     return 0;
1287 #endif
1288
1289   if (mode == DFmode)
1290     {
1291       long k[2];
1292       REAL_VALUE_TYPE rv;
1293
1294       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1295       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
1296
1297       return (num_insns_constant_wide ((HOST_WIDE_INT) k[0]) == 1
1298               && num_insns_constant_wide ((HOST_WIDE_INT) k[1]) == 1);
1299     }
1300
1301   else if (mode == SFmode)
1302     {
1303       long l;
1304       REAL_VALUE_TYPE rv;
1305
1306       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1307       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1308
1309       return num_insns_constant_wide (l) == 1;
1310     }
1311
1312   else if (mode == DImode)
1313     return ((TARGET_POWERPC64
1314              && GET_CODE (op) == CONST_DOUBLE && CONST_DOUBLE_LOW (op) == 0)
1315             || (num_insns_constant (op, DImode) <= 2));
1316
1317   else if (mode == SImode)
1318     return 1;
1319   else
1320     abort ();
1321 }
1322
1323 /* Return 1 if the operand is a CONST_INT and can be put into a
1324    register with one instruction.  */
1325
1326 static int
1327 easy_vector_constant (op)
1328      rtx op;
1329 {
1330   rtx elt;
1331   int units, i;
1332
1333   if (GET_CODE (op) != CONST_VECTOR)
1334     return 0;
1335
1336   units = CONST_VECTOR_NUNITS (op);
1337
1338   /* We can generate 0 easily.  Look for that.  */
1339   for (i = 0; i < units; ++i)
1340     {
1341       elt = CONST_VECTOR_ELT (op, i);
1342
1343       /* We could probably simplify this by just checking for equality
1344          with CONST0_RTX for the current mode, but let's be safe
1345          instead.  */
1346
1347       switch (GET_CODE (elt))
1348         {
1349         case CONST_INT:
1350           if (INTVAL (elt) != 0)
1351             return 0;
1352           break;
1353         case CONST_DOUBLE:
1354           if (CONST_DOUBLE_LOW (elt) != 0 || CONST_DOUBLE_HIGH (elt) != 0)
1355             return 0;
1356           break;
1357         default:
1358           return 0;
1359         }
1360     }
1361
1362   /* We could probably generate a few other constants trivially, but
1363      gcc doesn't generate them yet.  FIXME later.  */
1364   return 1;
1365 }
1366
1367 /* Return 1 if the operand is the constant 0.  This works for scalars
1368    as well as vectors.  */
1369 int
1370 zero_constant (op, mode)
1371      rtx op;
1372      enum machine_mode mode;
1373 {
1374   return op == CONST0_RTX (mode);
1375 }
1376
1377 /* Return 1 if the operand is 0.0.  */
1378 int
1379 zero_fp_constant (op, mode)
1380      rtx op;
1381      enum machine_mode mode;
1382 {
1383   return GET_MODE_CLASS (mode) == MODE_FLOAT && op == CONST0_RTX (mode);
1384 }
1385
1386 /* Return 1 if the operand is in volatile memory.  Note that during
1387    the RTL generation phase, memory_operand does not return TRUE for
1388    volatile memory references.  So this function allows us to
1389    recognize volatile references where its safe.  */
1390
1391 int
1392 volatile_mem_operand (op, mode)
1393      rtx op;
1394      enum machine_mode mode;
1395 {
1396   if (GET_CODE (op) != MEM)
1397     return 0;
1398
1399   if (!MEM_VOLATILE_P (op))
1400     return 0;
1401
1402   if (mode != GET_MODE (op))
1403     return 0;
1404
1405   if (reload_completed)
1406     return memory_operand (op, mode);
1407
1408   if (reload_in_progress)
1409     return strict_memory_address_p (mode, XEXP (op, 0));
1410
1411   return memory_address_p (mode, XEXP (op, 0));
1412 }
1413
1414 /* Return 1 if the operand is an offsettable memory operand.  */
1415
1416 int
1417 offsettable_mem_operand (op, mode)
1418      rtx op;
1419      enum machine_mode mode;
1420 {
1421   return ((GET_CODE (op) == MEM)
1422           && offsettable_address_p (reload_completed || reload_in_progress,
1423                                     mode, XEXP (op, 0)));
1424 }
1425
1426 /* Return 1 if the operand is either an easy FP constant (see above) or
1427    memory.  */
1428
1429 int
1430 mem_or_easy_const_operand (op, mode)
1431      rtx op;
1432      enum machine_mode mode;
1433 {
1434   return memory_operand (op, mode) || easy_fp_constant (op, mode);
1435 }
1436
1437 /* Return 1 if the operand is either a non-special register or an item
1438    that can be used as the operand of a `mode' add insn.  */
1439
1440 int
1441 add_operand (op, mode)
1442     rtx op;
1443     enum machine_mode mode;
1444 {
1445   if (GET_CODE (op) == CONST_INT)
1446     return (CONST_OK_FOR_LETTER_P (INTVAL (op), 'I')
1447             || CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
1448
1449   return gpc_reg_operand (op, mode);
1450 }
1451
1452 /* Return 1 if OP is a constant but not a valid add_operand.  */
1453
1454 int
1455 non_add_cint_operand (op, mode)
1456      rtx op;
1457      enum machine_mode mode ATTRIBUTE_UNUSED;
1458 {
1459   return (GET_CODE (op) == CONST_INT
1460           && !CONST_OK_FOR_LETTER_P (INTVAL (op), 'I')
1461           && !CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
1462 }
1463
1464 /* Return 1 if the operand is a non-special register or a constant that
1465    can be used as the operand of an OR or XOR insn on the RS/6000.  */
1466
1467 int
1468 logical_operand (op, mode)
1469      rtx op;
1470      enum machine_mode mode;
1471 {
1472   HOST_WIDE_INT opl, oph;
1473
1474   if (gpc_reg_operand (op, mode))
1475     return 1;
1476
1477   if (GET_CODE (op) == CONST_INT)
1478     {
1479       opl = INTVAL (op) & GET_MODE_MASK (mode);
1480
1481 #if HOST_BITS_PER_WIDE_INT <= 32
1482       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT && opl < 0)
1483         return 0;
1484 #endif
1485     }
1486   else if (GET_CODE (op) == CONST_DOUBLE)
1487     {
1488       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
1489         abort ();
1490
1491       opl = CONST_DOUBLE_LOW (op);
1492       oph = CONST_DOUBLE_HIGH (op);
1493       if (oph != 0)
1494         return 0;
1495     }
1496   else
1497     return 0;
1498
1499   return ((opl & ~ (unsigned HOST_WIDE_INT) 0xffff) == 0
1500           || (opl & ~ (unsigned HOST_WIDE_INT) 0xffff0000) == 0);
1501 }
1502
1503 /* Return 1 if C is a constant that is not a logical operand (as
1504    above), but could be split into one.  */
1505
1506 int
1507 non_logical_cint_operand (op, mode)
1508      rtx op;
1509      enum machine_mode mode;
1510 {
1511   return ((GET_CODE (op) == CONST_INT || GET_CODE (op) == CONST_DOUBLE)
1512           && ! logical_operand (op, mode)
1513           && reg_or_logical_cint_operand (op, mode));
1514 }
1515
1516 /* Return 1 if C is a constant that can be encoded in a 32-bit mask on the
1517    RS/6000.  It is if there are no more than two 1->0 or 0->1 transitions.
1518    Reject all ones and all zeros, since these should have been optimized
1519    away and confuse the making of MB and ME.  */
1520
1521 int
1522 mask_operand (op, mode)
1523      rtx op;
1524      enum machine_mode mode ATTRIBUTE_UNUSED;
1525 {
1526   HOST_WIDE_INT c, lsb;
1527
1528   if (GET_CODE (op) != CONST_INT)
1529     return 0;
1530
1531   c = INTVAL (op);
1532
1533   /* Fail in 64-bit mode if the mask wraps around because the upper
1534      32-bits of the mask will all be 1s, contrary to GCC's internal view.  */
1535   if (TARGET_POWERPC64 && (c & 0x80000001) == 0x80000001)
1536     return 0;
1537
1538   /* We don't change the number of transitions by inverting,
1539      so make sure we start with the LS bit zero.  */
1540   if (c & 1)
1541     c = ~c;
1542
1543   /* Reject all zeros or all ones.  */
1544   if (c == 0)
1545     return 0;
1546
1547   /* Find the first transition.  */
1548   lsb = c & -c;
1549
1550   /* Invert to look for a second transition.  */
1551   c = ~c;
1552
1553   /* Erase first transition.  */
1554   c &= -lsb;
1555
1556   /* Find the second transition (if any).  */
1557   lsb = c & -c;
1558
1559   /* Match if all the bits above are 1's (or c is zero).  */
1560   return c == -lsb;
1561 }
1562
1563 /* Return 1 for the PowerPC64 rlwinm corner case.  */
1564
1565 int
1566 mask_operand_wrap (op, mode)
1567      rtx op;
1568      enum machine_mode mode ATTRIBUTE_UNUSED;
1569 {
1570   HOST_WIDE_INT c, lsb;
1571
1572   if (GET_CODE (op) != CONST_INT)
1573     return 0;
1574
1575   c = INTVAL (op);
1576
1577   if ((c & 0x80000001) != 0x80000001)
1578     return 0;
1579
1580   c = ~c;
1581   if (c == 0)
1582     return 0;
1583
1584   lsb = c & -c;
1585   c = ~c;
1586   c &= -lsb;
1587   lsb = c & -c;
1588   return c == -lsb;
1589 }
1590
1591 /* Return 1 if the operand is a constant that is a PowerPC64 mask.
1592    It is if there are no more than one 1->0 or 0->1 transitions.
1593    Reject all zeros, since zero should have been optimized away and
1594    confuses the making of MB and ME.  */
1595
1596 int
1597 mask64_operand (op, mode)
1598      rtx op;
1599      enum machine_mode mode ATTRIBUTE_UNUSED;
1600 {
1601   if (GET_CODE (op) == CONST_INT)
1602     {
1603       HOST_WIDE_INT c, lsb;
1604
1605       c = INTVAL (op);
1606
1607       /* Reject all zeros.  */
1608       if (c == 0)
1609         return 0;
1610
1611       /* We don't change the number of transitions by inverting,
1612          so make sure we start with the LS bit zero.  */
1613       if (c & 1)
1614         c = ~c;
1615
1616       /* Find the transition, and check that all bits above are 1's.  */
1617       lsb = c & -c;
1618       return c == -lsb;
1619     }
1620   return 0;
1621 }
1622
1623 /* Like mask64_operand, but allow up to three transitions.  This
1624    predicate is used by insn patterns that generate two rldicl or
1625    rldicr machine insns.  */
1626
1627 int
1628 mask64_2_operand (op, mode)
1629      rtx op;
1630      enum machine_mode mode ATTRIBUTE_UNUSED;
1631 {
1632   if (GET_CODE (op) == CONST_INT)
1633     {
1634       HOST_WIDE_INT c, lsb;
1635
1636       c = INTVAL (op);
1637
1638       /* Disallow all zeros.  */
1639       if (c == 0)
1640         return 0;
1641
1642       /* We don't change the number of transitions by inverting,
1643          so make sure we start with the LS bit zero.  */
1644       if (c & 1)
1645         c = ~c;
1646
1647       /* Find the first transition.  */
1648       lsb = c & -c;
1649
1650       /* Invert to look for a second transition.  */
1651       c = ~c;
1652
1653       /* Erase first transition.  */
1654       c &= -lsb;
1655
1656       /* Find the second transition.  */
1657       lsb = c & -c;
1658
1659       /* Invert to look for a third transition.  */
1660       c = ~c;
1661
1662       /* Erase second transition.  */
1663       c &= -lsb;
1664
1665       /* Find the third transition (if any).  */
1666       lsb = c & -c;
1667
1668       /* Match if all the bits above are 1's (or c is zero).  */
1669       return c == -lsb;
1670     }
1671   return 0;
1672 }
1673
1674 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
1675    implement ANDing by the mask IN.  */
1676 void
1677 build_mask64_2_operands (in, out)
1678      rtx in;
1679      rtx *out;
1680 {
1681 #if HOST_BITS_PER_WIDE_INT >= 64
1682   unsigned HOST_WIDE_INT c, lsb, m1, m2;
1683   int shift;
1684
1685   if (GET_CODE (in) != CONST_INT)
1686     abort ();
1687
1688   c = INTVAL (in);
1689   if (c & 1)
1690     {
1691       /* Assume c initially something like 0x00fff000000fffff.  The idea
1692          is to rotate the word so that the middle ^^^^^^ group of zeros
1693          is at the MS end and can be cleared with an rldicl mask.  We then
1694          rotate back and clear off the MS    ^^ group of zeros with a
1695          second rldicl.  */
1696       c = ~c;                   /*   c == 0xff000ffffff00000 */
1697       lsb = c & -c;             /* lsb == 0x0000000000100000 */
1698       m1 = -lsb;                /*  m1 == 0xfffffffffff00000 */
1699       c = ~c;                   /*   c == 0x00fff000000fffff */
1700       c &= -lsb;                /*   c == 0x00fff00000000000 */
1701       lsb = c & -c;             /* lsb == 0x0000100000000000 */
1702       c = ~c;                   /*   c == 0xff000fffffffffff */
1703       c &= -lsb;                /*   c == 0xff00000000000000 */
1704       shift = 0;
1705       while ((lsb >>= 1) != 0)
1706         shift++;                /* shift == 44 on exit from loop */
1707       m1 <<= 64 - shift;        /*  m1 == 0xffffff0000000000 */
1708       m1 = ~m1;                 /*  m1 == 0x000000ffffffffff */
1709       m2 = ~c;                  /*  m2 == 0x00ffffffffffffff */
1710     }
1711   else
1712     {
1713       /* Assume c initially something like 0xff000f0000000000.  The idea
1714          is to rotate the word so that the     ^^^  middle group of zeros
1715          is at the LS end and can be cleared with an rldicr mask.  We then
1716          rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
1717          a second rldicr.  */
1718       lsb = c & -c;             /* lsb == 0x0000010000000000 */
1719       m2 = -lsb;                /*  m2 == 0xffffff0000000000 */
1720       c = ~c;                   /*   c == 0x00fff0ffffffffff */
1721       c &= -lsb;                /*   c == 0x00fff00000000000 */
1722       lsb = c & -c;             /* lsb == 0x0000100000000000 */
1723       c = ~c;                   /*   c == 0xff000fffffffffff */
1724       c &= -lsb;                /*   c == 0xff00000000000000 */
1725       shift = 0;
1726       while ((lsb >>= 1) != 0)
1727         shift++;                /* shift == 44 on exit from loop */
1728       m1 = ~c;                  /*  m1 == 0x00ffffffffffffff */
1729       m1 >>= shift;             /*  m1 == 0x0000000000000fff */
1730       m1 = ~m1;                 /*  m1 == 0xfffffffffffff000 */
1731     }
1732
1733   /* Note that when we only have two 0->1 and 1->0 transitions, one of the
1734      masks will be all 1's.  We are guaranteed more than one transition.  */
1735   out[0] = GEN_INT (64 - shift);
1736   out[1] = GEN_INT (m1);
1737   out[2] = GEN_INT (shift);
1738   out[3] = GEN_INT (m2);
1739 #else
1740   abort ();
1741 #endif
1742 }
1743
1744 /* Return 1 if the operand is either a non-special register or a constant
1745    that can be used as the operand of a PowerPC64 logical AND insn.  */
1746
1747 int
1748 and64_operand (op, mode)
1749     rtx op;
1750     enum machine_mode mode;
1751 {
1752   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
1753     return (gpc_reg_operand (op, mode) || mask64_operand (op, mode));
1754
1755   return (logical_operand (op, mode) || mask64_operand (op, mode));
1756 }
1757
1758 /* Like the above, but also match constants that can be implemented
1759    with two rldicl or rldicr insns.  */
1760
1761 int
1762 and64_2_operand (op, mode)
1763     rtx op;
1764     enum machine_mode mode;
1765 {
1766   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis. */
1767     return gpc_reg_operand (op, mode) || mask64_2_operand (op, mode);
1768
1769   return logical_operand (op, mode) || mask64_2_operand (op, mode);
1770 }
1771
1772 /* Return 1 if the operand is either a non-special register or a
1773    constant that can be used as the operand of an RS/6000 logical AND insn.  */
1774
1775 int
1776 and_operand (op, mode)
1777     rtx op;
1778     enum machine_mode mode;
1779 {
1780   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
1781     return (gpc_reg_operand (op, mode) || mask_operand (op, mode));
1782
1783   return (logical_operand (op, mode) || mask_operand (op, mode));
1784 }
1785
1786 /* Return 1 if the operand is a general register or memory operand.  */
1787
1788 int
1789 reg_or_mem_operand (op, mode)
1790      rtx op;
1791      enum machine_mode mode;
1792 {
1793   return (gpc_reg_operand (op, mode)
1794           || memory_operand (op, mode)
1795           || volatile_mem_operand (op, mode));
1796 }
1797
1798 /* Return 1 if the operand is a general register or memory operand without
1799    pre_inc or pre_dec which produces invalid form of PowerPC lwa
1800    instruction.  */
1801
1802 int
1803 lwa_operand (op, mode)
1804      rtx op;
1805      enum machine_mode mode;
1806 {
1807   rtx inner = op;
1808
1809   if (reload_completed && GET_CODE (inner) == SUBREG)
1810     inner = SUBREG_REG (inner);
1811     
1812   return gpc_reg_operand (inner, mode)
1813     || (memory_operand (inner, mode)
1814         && GET_CODE (XEXP (inner, 0)) != PRE_INC
1815         && GET_CODE (XEXP (inner, 0)) != PRE_DEC
1816         && (GET_CODE (XEXP (inner, 0)) != PLUS
1817             || GET_CODE (XEXP (XEXP (inner, 0), 1)) != CONST_INT
1818             || INTVAL (XEXP (XEXP (inner, 0), 1)) % 4 == 0));
1819 }
1820
1821 /* Return 1 if the operand, used inside a MEM, is a SYMBOL_REF.  */
1822
1823 int
1824 symbol_ref_operand (op, mode)
1825      rtx op;
1826      enum machine_mode mode;
1827 {
1828   if (mode != VOIDmode && GET_MODE (op) != mode)
1829     return 0;
1830
1831   return (GET_CODE (op) == SYMBOL_REF);
1832 }
1833
1834 /* Return 1 if the operand, used inside a MEM, is a valid first argument
1835    to CALL.  This is a SYMBOL_REF, a pseudo-register, LR or CTR.  */
1836
1837 int
1838 call_operand (op, mode)
1839      rtx op;
1840      enum machine_mode mode;
1841 {
1842   if (mode != VOIDmode && GET_MODE (op) != mode)
1843     return 0;
1844
1845   return (GET_CODE (op) == SYMBOL_REF
1846           || (GET_CODE (op) == REG
1847               && (REGNO (op) == LINK_REGISTER_REGNUM
1848                   || REGNO (op) == COUNT_REGISTER_REGNUM
1849                   || REGNO (op) >= FIRST_PSEUDO_REGISTER)));
1850 }
1851
1852 /* Return 1 if the operand is a SYMBOL_REF for a function known to be in
1853    this file and the function is not weakly defined.  */
1854
1855 int
1856 current_file_function_operand (op, mode)
1857      rtx op;
1858      enum machine_mode mode ATTRIBUTE_UNUSED;
1859 {
1860   return (GET_CODE (op) == SYMBOL_REF
1861           && (SYMBOL_REF_FLAG (op)
1862               || (op == XEXP (DECL_RTL (current_function_decl), 0)
1863                   && ! DECL_WEAK (current_function_decl))));
1864 }
1865
1866 /* Return 1 if this operand is a valid input for a move insn.  */
1867
1868 int
1869 input_operand (op, mode)
1870      rtx op;
1871      enum machine_mode mode;
1872 {
1873   /* Memory is always valid.  */
1874   if (memory_operand (op, mode))
1875     return 1;
1876
1877   /* Only a tiny bit of handling for CONSTANT_P_RTX is necessary.  */
1878   if (GET_CODE (op) == CONSTANT_P_RTX)
1879     return 1;
1880
1881   /* For floating-point, easy constants are valid.  */
1882   if (GET_MODE_CLASS (mode) == MODE_FLOAT
1883       && CONSTANT_P (op)
1884       && easy_fp_constant (op, mode))
1885     return 1;
1886
1887   /* Allow any integer constant.  */
1888   if (GET_MODE_CLASS (mode) == MODE_INT
1889       && (GET_CODE (op) == CONST_INT
1890           || GET_CODE (op) == CONST_DOUBLE))
1891     return 1;
1892
1893   /* For floating-point or multi-word mode, the only remaining valid type
1894      is a register.  */
1895   if (GET_MODE_CLASS (mode) == MODE_FLOAT
1896       || GET_MODE_SIZE (mode) > UNITS_PER_WORD)
1897     return register_operand (op, mode);
1898
1899   /* The only cases left are integral modes one word or smaller (we
1900      do not get called for MODE_CC values).  These can be in any
1901      register.  */
1902   if (register_operand (op, mode))
1903     return 1;
1904
1905   /* A SYMBOL_REF referring to the TOC is valid.  */
1906   if (LEGITIMATE_CONSTANT_POOL_ADDRESS_P (op))
1907     return 1;
1908
1909   /* A constant pool expression (relative to the TOC) is valid */
1910   if (TOC_RELATIVE_EXPR_P (op))
1911     return 1;
1912
1913   /* V.4 allows SYMBOL_REFs and CONSTs that are in the small data region
1914      to be valid.  */
1915   if (DEFAULT_ABI == ABI_V4
1916       && (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST)
1917       && small_data_operand (op, Pmode))
1918     return 1;
1919
1920   return 0;
1921 }
1922
1923 /* Return 1 for an operand in small memory on V.4/eabi.  */
1924
1925 int
1926 small_data_operand (op, mode)
1927      rtx op ATTRIBUTE_UNUSED;
1928      enum machine_mode mode ATTRIBUTE_UNUSED;
1929 {
1930 #if TARGET_ELF
1931   rtx sym_ref;
1932
1933   if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
1934     return 0;
1935
1936   if (DEFAULT_ABI != ABI_V4)
1937     return 0;
1938
1939   if (GET_CODE (op) == SYMBOL_REF)
1940     sym_ref = op;
1941
1942   else if (GET_CODE (op) != CONST
1943            || GET_CODE (XEXP (op, 0)) != PLUS
1944            || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
1945            || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
1946     return 0;
1947
1948   else
1949     {
1950       rtx sum = XEXP (op, 0);
1951       HOST_WIDE_INT summand;
1952
1953       /* We have to be careful here, because it is the referenced address
1954         that must be 32k from _SDA_BASE_, not just the symbol.  */
1955       summand = INTVAL (XEXP (sum, 1));
1956       if (summand < 0 || summand > g_switch_value)
1957        return 0;
1958
1959       sym_ref = XEXP (sum, 0);
1960     }
1961
1962   if (*XSTR (sym_ref, 0) != '@')
1963     return 0;
1964
1965   return 1;
1966
1967 #else
1968   return 0;
1969 #endif
1970 }
1971 \f
1972 static int 
1973 constant_pool_expr_1 (op, have_sym, have_toc) 
1974     rtx op;
1975     int *have_sym;
1976     int *have_toc;
1977 {
1978   switch (GET_CODE(op)) 
1979     {
1980     case SYMBOL_REF:
1981       if (CONSTANT_POOL_ADDRESS_P (op))
1982         {
1983           if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (op), Pmode))
1984             {
1985               *have_sym = 1;
1986               return 1;
1987             }
1988           else
1989             return 0;
1990         }
1991       else if (! strcmp (XSTR (op, 0), toc_label_name))
1992         {
1993           *have_toc = 1;
1994           return 1;
1995         }
1996       else
1997         return 0;
1998     case PLUS:
1999     case MINUS:
2000       return (constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc)
2001               && constant_pool_expr_1 (XEXP (op, 1), have_sym, have_toc));
2002     case CONST:
2003       return constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc);
2004     case CONST_INT:
2005       return 1;
2006     default:
2007       return 0;
2008     }
2009 }
2010
2011 int
2012 constant_pool_expr_p (op)
2013     rtx op;
2014 {
2015   int have_sym = 0;
2016   int have_toc = 0;
2017   return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_sym;
2018 }
2019
2020 int
2021 toc_relative_expr_p (op)
2022     rtx op;
2023 {
2024     int have_sym = 0;
2025     int have_toc = 0;
2026     return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_toc;
2027 }
2028
2029 /* Try machine-dependent ways of modifying an illegitimate address
2030    to be legitimate.  If we find one, return the new, valid address.
2031    This is used from only one place: `memory_address' in explow.c.
2032
2033    OLDX is the address as it was before break_out_memory_refs was
2034    called.  In some cases it is useful to look at this to decide what
2035    needs to be done.
2036
2037    MODE is passed so that this function can use GO_IF_LEGITIMATE_ADDRESS.
2038
2039    It is always safe for this function to do nothing.  It exists to
2040    recognize opportunities to optimize the output.
2041
2042    On RS/6000, first check for the sum of a register with a constant
2043    integer that is out of range.  If so, generate code to add the
2044    constant with the low-order 16 bits masked to the register and force
2045    this result into another register (this can be done with `cau').
2046    Then generate an address of REG+(CONST&0xffff), allowing for the
2047    possibility of bit 16 being a one.
2048
2049    Then check for the sum of a register and something not constant, try to
2050    load the other things into a register and return the sum.  */
2051 rtx
2052 rs6000_legitimize_address (x, oldx, mode)
2053      rtx x;
2054      rtx oldx ATTRIBUTE_UNUSED;
2055      enum machine_mode mode;
2056 {
2057   if (GET_CODE (x) == PLUS 
2058       && GET_CODE (XEXP (x, 0)) == REG
2059       && GET_CODE (XEXP (x, 1)) == CONST_INT
2060       && (unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000) >= 0x10000)
2061     { 
2062       HOST_WIDE_INT high_int, low_int;
2063       rtx sum;
2064       low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
2065       high_int = INTVAL (XEXP (x, 1)) - low_int;
2066       sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
2067                                          GEN_INT (high_int)), 0);
2068       return gen_rtx_PLUS (Pmode, sum, GEN_INT (low_int));
2069     }
2070   else if (GET_CODE (x) == PLUS 
2071            && GET_CODE (XEXP (x, 0)) == REG
2072            && GET_CODE (XEXP (x, 1)) != CONST_INT
2073            && GET_MODE_NUNITS (mode) == 1
2074            && ((TARGET_HARD_FLOAT && TARGET_FPRS)
2075                || TARGET_POWERPC64
2076                || mode != DFmode)
2077            && (TARGET_POWERPC64 || mode != DImode)
2078            && mode != TImode)
2079     {
2080       return gen_rtx_PLUS (Pmode, XEXP (x, 0),
2081                            force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
2082     }
2083   else if (ALTIVEC_VECTOR_MODE (mode))
2084     {
2085       rtx reg;
2086
2087       /* Make sure both operands are registers.  */
2088       if (GET_CODE (x) == PLUS)
2089         return gen_rtx_PLUS (Pmode, force_reg (Pmode, XEXP (x, 0)),
2090                              force_reg (Pmode, XEXP (x, 1)));
2091
2092       reg = force_reg (Pmode, x);
2093       return reg;
2094     }
2095   else if (SPE_VECTOR_MODE (mode))
2096     {
2097       /* We accept [reg + reg] and [reg + OFFSET].  */
2098
2099       if (GET_CODE (x) == PLUS)
2100       {
2101         rtx op1 = XEXP (x, 0);
2102         rtx op2 = XEXP (x, 1);
2103
2104         op1 = force_reg (Pmode, op1);
2105
2106         if (GET_CODE (op2) != REG
2107             && (GET_CODE (op2) != CONST_INT
2108                 || !SPE_CONST_OFFSET_OK (INTVAL (op2))))
2109           op2 = force_reg (Pmode, op2);
2110
2111         return gen_rtx_PLUS (Pmode, op1, op2);
2112       }
2113
2114       return force_reg (Pmode, x);
2115     }
2116   else if (TARGET_ELF && TARGET_32BIT && TARGET_NO_TOC && ! flag_pic
2117            && GET_CODE (x) != CONST_INT
2118            && GET_CODE (x) != CONST_DOUBLE 
2119            && CONSTANT_P (x)
2120            && GET_MODE_NUNITS (mode) == 1
2121            && (GET_MODE_BITSIZE (mode) <= 32
2122                || ((TARGET_HARD_FLOAT && TARGET_FPRS) && mode == DFmode)))
2123     {
2124       rtx reg = gen_reg_rtx (Pmode);
2125       emit_insn (gen_elf_high (reg, (x)));
2126       return gen_rtx_LO_SUM (Pmode, reg, (x));
2127     }
2128   else if (TARGET_MACHO && TARGET_32BIT && TARGET_NO_TOC
2129            && ! flag_pic
2130            && GET_CODE (x) != CONST_INT
2131            && GET_CODE (x) != CONST_DOUBLE 
2132            && CONSTANT_P (x)
2133            && ((TARGET_HARD_FLOAT && TARGET_FPRS) || mode != DFmode)
2134            && mode != DImode 
2135            && mode != TImode)
2136     {
2137       rtx reg = gen_reg_rtx (Pmode);
2138       emit_insn (gen_macho_high (reg, (x)));
2139       return gen_rtx_LO_SUM (Pmode, reg, (x));
2140     }
2141   else if (TARGET_TOC 
2142            && CONSTANT_POOL_EXPR_P (x)
2143            && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
2144     {
2145       return create_TOC_reference (x);
2146     }
2147   else
2148     return NULL_RTX;
2149 }
2150
2151 /* The convention appears to be to define this wherever it is used.
2152    With legitimize_reload_address now defined here, REG_MODE_OK_FOR_BASE_P
2153    is now used here.  */
2154 #ifndef REG_MODE_OK_FOR_BASE_P
2155 #define REG_MODE_OK_FOR_BASE_P(REGNO, MODE) REG_OK_FOR_BASE_P (REGNO)
2156 #endif
2157
2158 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS.  Returns a value to
2159    replace the input X, or the original X if no replacement is called for.
2160    The output parameter *WIN is 1 if the calling macro should goto WIN,
2161    0 if it should not.
2162
2163    For RS/6000, we wish to handle large displacements off a base
2164    register by splitting the addend across an addiu/addis and the mem insn.
2165    This cuts number of extra insns needed from 3 to 1.
2166
2167    On Darwin, we use this to generate code for floating point constants.
2168    A movsf_low is generated so we wind up with 2 instructions rather than 3.
2169    The Darwin code is inside #if TARGET_MACHO because only then is
2170    machopic_function_base_name() defined.  */
2171 rtx
2172 rs6000_legitimize_reload_address (x, mode, opnum, type, ind_levels, win)
2173     rtx x;
2174     enum machine_mode mode;
2175     int opnum;
2176     int type;
2177     int ind_levels ATTRIBUTE_UNUSED;
2178     int *win;
2179 {
2180   /* We must recognize output that we have already generated ourselves.  */ 
2181   if (GET_CODE (x) == PLUS
2182       && GET_CODE (XEXP (x, 0)) == PLUS
2183       && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
2184       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2185       && GET_CODE (XEXP (x, 1)) == CONST_INT)
2186     {
2187       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2188                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
2189                    opnum, (enum reload_type)type);
2190       *win = 1;
2191       return x;
2192     }
2193
2194 #if TARGET_MACHO
2195   if (DEFAULT_ABI == ABI_DARWIN && flag_pic
2196       && GET_CODE (x) == LO_SUM
2197       && GET_CODE (XEXP (x, 0)) == PLUS
2198       && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
2199       && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
2200       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 0)) == CONST
2201       && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
2202       && GET_CODE (XEXP (XEXP (x, 1), 0)) == MINUS
2203       && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 0)) == SYMBOL_REF
2204       && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 1)) == SYMBOL_REF)
2205     {
2206       /* Result of previous invocation of this function on Darwin
2207          floating point constant.  */
2208       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2209                 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
2210                 opnum, (enum reload_type)type);
2211       *win = 1;
2212       return x;
2213     }
2214 #endif
2215   if (GET_CODE (x) == PLUS
2216       && GET_CODE (XEXP (x, 0)) == REG
2217       && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2218       && REG_MODE_OK_FOR_BASE_P (XEXP (x, 0), mode)
2219       && GET_CODE (XEXP (x, 1)) == CONST_INT
2220        && !SPE_VECTOR_MODE (mode)
2221       && !ALTIVEC_VECTOR_MODE (mode))
2222     {
2223       HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
2224       HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
2225       HOST_WIDE_INT high
2226         = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
2227
2228       /* Check for 32-bit overflow.  */
2229       if (high + low != val)
2230         {
2231           *win = 0;
2232           return x;
2233         }
2234
2235       /* Reload the high part into a base reg; leave the low part
2236          in the mem directly.  */
2237
2238       x = gen_rtx_PLUS (GET_MODE (x),
2239                         gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
2240                                       GEN_INT (high)),
2241                         GEN_INT (low));
2242
2243       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2244                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
2245                    opnum, (enum reload_type)type);
2246       *win = 1;
2247       return x;
2248     }
2249 #if TARGET_MACHO
2250   if (GET_CODE (x) == SYMBOL_REF
2251       && DEFAULT_ABI == ABI_DARWIN
2252       && !ALTIVEC_VECTOR_MODE (mode)
2253       && flag_pic)
2254     {
2255       /* Darwin load of floating point constant.  */
2256       rtx offset = gen_rtx (CONST, Pmode,
2257                     gen_rtx (MINUS, Pmode, x,
2258                     gen_rtx (SYMBOL_REF, Pmode,
2259                         machopic_function_base_name ())));
2260       x = gen_rtx (LO_SUM, GET_MODE (x),
2261             gen_rtx (PLUS, Pmode, pic_offset_table_rtx,
2262                 gen_rtx (HIGH, Pmode, offset)), offset);
2263       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2264                 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
2265                 opnum, (enum reload_type)type);
2266       *win = 1;
2267       return x;
2268     }
2269 #endif
2270   if (TARGET_TOC
2271       && CONSTANT_POOL_EXPR_P (x)
2272       && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), mode))
2273     {
2274       (x) = create_TOC_reference (x);
2275       *win = 1;
2276       return x;
2277     }
2278   *win = 0;
2279   return x;
2280 }    
2281
2282 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression
2283    that is a valid memory address for an instruction.
2284    The MODE argument is the machine mode for the MEM expression
2285    that wants to use this address.
2286
2287    On the RS/6000, there are four valid address: a SYMBOL_REF that
2288    refers to a constant pool entry of an address (or the sum of it
2289    plus a constant), a short (16-bit signed) constant plus a register,
2290    the sum of two registers, or a register indirect, possibly with an
2291    auto-increment.  For DFmode and DImode with an constant plus register,
2292    we must ensure that both words are addressable or PowerPC64 with offset
2293    word aligned.
2294
2295    For modes spanning multiple registers (DFmode in 32-bit GPRs,
2296    32-bit DImode, TImode), indexed addressing cannot be used because
2297    adjacent memory cells are accessed by adding word-sized offsets
2298    during assembly output.  */
2299 int
2300 rs6000_legitimate_address (mode, x, reg_ok_strict)
2301     enum machine_mode mode;
2302     rtx x;
2303     int reg_ok_strict;
2304 {
2305   if (LEGITIMATE_INDIRECT_ADDRESS_P (x, reg_ok_strict))
2306     return 1;
2307   if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
2308       && !ALTIVEC_VECTOR_MODE (mode)
2309       && !SPE_VECTOR_MODE (mode)
2310       && TARGET_UPDATE
2311       && LEGITIMATE_INDIRECT_ADDRESS_P (XEXP (x, 0), reg_ok_strict))
2312     return 1;
2313   if (LEGITIMATE_SMALL_DATA_P (mode, x))
2314     return 1;
2315   if (LEGITIMATE_CONSTANT_POOL_ADDRESS_P (x))
2316     return 1;
2317   /* If not REG_OK_STRICT (before reload) let pass any stack offset.  */
2318   if (! reg_ok_strict
2319       && GET_CODE (x) == PLUS
2320       && GET_CODE (XEXP (x, 0)) == REG
2321       && XEXP (x, 0) == virtual_stack_vars_rtx
2322       && GET_CODE (XEXP (x, 1)) == CONST_INT)
2323     return 1;
2324   if (LEGITIMATE_OFFSET_ADDRESS_P (mode, x, reg_ok_strict))
2325     return 1;
2326   if (mode != TImode
2327       && ((TARGET_HARD_FLOAT && TARGET_FPRS)
2328           || TARGET_POWERPC64
2329           || mode != DFmode)
2330       && (TARGET_POWERPC64 || mode != DImode)
2331       && LEGITIMATE_INDEXED_ADDRESS_P (x, reg_ok_strict))
2332     return 1;
2333   if (LEGITIMATE_LO_SUM_ADDRESS_P (mode, x, reg_ok_strict))
2334     return 1;
2335   return 0;
2336 }
2337 \f
2338 /* Try to output insns to set TARGET equal to the constant C if it can
2339    be done in less than N insns.  Do all computations in MODE.
2340    Returns the place where the output has been placed if it can be
2341    done and the insns have been emitted.  If it would take more than N
2342    insns, zero is returned and no insns and emitted.  */
2343
2344 rtx
2345 rs6000_emit_set_const (dest, mode, source, n)
2346      rtx dest, source;
2347      enum machine_mode mode;
2348      int n ATTRIBUTE_UNUSED;
2349 {
2350   HOST_WIDE_INT c0, c1;
2351
2352   if (mode == QImode || mode == HImode || mode == SImode)
2353     {
2354       if (dest == NULL)
2355         dest = gen_reg_rtx (mode);
2356       emit_insn (gen_rtx_SET (VOIDmode, dest, source));
2357       return dest;
2358     }
2359
2360   if (GET_CODE (source) == CONST_INT)
2361     {
2362       c0 = INTVAL (source);
2363       c1 = -(c0 < 0);
2364     }
2365   else if (GET_CODE (source) == CONST_DOUBLE)
2366     {
2367 #if HOST_BITS_PER_WIDE_INT >= 64
2368       c0 = CONST_DOUBLE_LOW (source);
2369       c1 = -(c0 < 0);
2370 #else
2371       c0 = CONST_DOUBLE_LOW (source);
2372       c1 = CONST_DOUBLE_HIGH (source);
2373 #endif
2374     }
2375   else
2376     abort ();
2377
2378   return rs6000_emit_set_long_const (dest, c0, c1);
2379 }
2380
2381 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
2382    fall back to a straight forward decomposition.  We do this to avoid
2383    exponential run times encountered when looking for longer sequences
2384    with rs6000_emit_set_const.  */
2385 static rtx
2386 rs6000_emit_set_long_const (dest, c1, c2)
2387      rtx dest;
2388      HOST_WIDE_INT c1, c2;
2389 {
2390   if (!TARGET_POWERPC64)
2391     {
2392       rtx operand1, operand2;
2393
2394       operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
2395                                         DImode);
2396       operand2 = operand_subword_force (dest, WORDS_BIG_ENDIAN != 0,
2397                                         DImode);
2398       emit_move_insn (operand1, GEN_INT (c1));
2399       emit_move_insn (operand2, GEN_INT (c2));
2400     }
2401   else
2402     {
2403       HOST_WIDE_INT ud1, ud2, ud3, ud4;
2404
2405       ud1 = c1 & 0xffff;
2406       ud2 = (c1 & 0xffff0000) >> 16;
2407 #if HOST_BITS_PER_WIDE_INT >= 64
2408       c2 = c1 >> 32;
2409 #endif
2410       ud3 = c2 & 0xffff;
2411       ud4 = (c2 & 0xffff0000) >> 16;
2412
2413       if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000)) 
2414           || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
2415         {
2416           if (ud1 & 0x8000)
2417             emit_move_insn (dest, GEN_INT (((ud1  ^ 0x8000) -  0x8000)));
2418           else
2419             emit_move_insn (dest, GEN_INT (ud1));
2420         }
2421
2422       else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000)) 
2423                || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
2424         {
2425           if (ud2 & 0x8000)
2426             emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000) 
2427                                            - 0x80000000));
2428           else
2429             emit_move_insn (dest, GEN_INT (ud2 << 16));
2430           if (ud1 != 0)
2431             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
2432         }
2433       else if ((ud4 == 0xffff && (ud3 & 0x8000)) 
2434                || (ud4 == 0 && ! (ud3 & 0x8000)))
2435         {
2436           if (ud3 & 0x8000)
2437             emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000) 
2438                                            - 0x80000000));
2439           else
2440             emit_move_insn (dest, GEN_INT (ud3 << 16));
2441
2442           if (ud2 != 0)
2443             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud2)));
2444           emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (16)));
2445           if (ud1 != 0)
2446             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
2447         }
2448       else 
2449         {
2450           if (ud4 & 0x8000)
2451             emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000) 
2452                                            - 0x80000000));
2453           else
2454             emit_move_insn (dest, GEN_INT (ud4 << 16));
2455
2456           if (ud3 != 0)
2457             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud3)));
2458
2459           emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (32)));
2460           if (ud2 != 0)
2461             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, 
2462                                                GEN_INT (ud2 << 16)));   
2463           if (ud1 != 0)
2464             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
2465         }
2466     }
2467   return dest;
2468 }
2469
2470 /* Emit a move from SOURCE to DEST in mode MODE.  */
2471 void
2472 rs6000_emit_move (dest, source, mode)
2473      rtx dest;
2474      rtx source;
2475      enum machine_mode mode;
2476 {
2477   rtx operands[2];
2478   operands[0] = dest;
2479   operands[1] = source;
2480   
2481   /* Sanity checks.  Check that we get CONST_DOUBLE only when we should.  */
2482   if (GET_CODE (operands[1]) == CONST_DOUBLE
2483       && ! FLOAT_MODE_P (mode)
2484       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
2485     {
2486       /* FIXME.  This should never happen.  */
2487       /* Since it seems that it does, do the safe thing and convert
2488          to a CONST_INT.  */
2489       operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
2490     }
2491   if (GET_CODE (operands[1]) == CONST_DOUBLE
2492       && ! FLOAT_MODE_P (mode)
2493       && ((CONST_DOUBLE_HIGH (operands[1]) == 0
2494            && CONST_DOUBLE_LOW (operands[1]) >= 0)
2495           || (CONST_DOUBLE_HIGH (operands[1]) == -1
2496               && CONST_DOUBLE_LOW (operands[1]) < 0)))
2497     abort ();
2498
2499   /* Check if GCC is setting up a block move that will end up using FP
2500      registers as temporaries.  We must make sure this is acceptable.  */
2501   if (GET_CODE (operands[0]) == MEM
2502       && GET_CODE (operands[1]) == MEM
2503       && mode == DImode
2504       && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
2505           || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
2506       && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
2507                                             ? 32 : MEM_ALIGN (operands[0])))
2508             || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
2509                                                ? 32 
2510                                                : MEM_ALIGN (operands[1]))))
2511       && ! MEM_VOLATILE_P (operands [0])
2512       && ! MEM_VOLATILE_P (operands [1]))
2513     {
2514       emit_move_insn (adjust_address (operands[0], SImode, 0),
2515                       adjust_address (operands[1], SImode, 0));
2516       emit_move_insn (adjust_address (operands[0], SImode, 4),
2517                       adjust_address (operands[1], SImode, 4));
2518       return;
2519     }
2520   
2521   if (! no_new_pseudos && GET_CODE (operands[0]) != REG)
2522     operands[1] = force_reg (mode, operands[1]);
2523
2524   if (mode == SFmode && ! TARGET_POWERPC
2525       && TARGET_HARD_FLOAT && TARGET_FPRS
2526       && GET_CODE (operands[0]) == MEM)
2527     {
2528       int regnum;
2529
2530       if (reload_in_progress || reload_completed)
2531         regnum = true_regnum (operands[1]);
2532       else if (GET_CODE (operands[1]) == REG)
2533         regnum = REGNO (operands[1]);
2534       else
2535         regnum = -1;
2536       
2537       /* If operands[1] is a register, on POWER it may have
2538          double-precision data in it, so truncate it to single
2539          precision.  */
2540       if (FP_REGNO_P (regnum) || regnum >= FIRST_PSEUDO_REGISTER)
2541         {
2542           rtx newreg;
2543           newreg = (no_new_pseudos ? operands[1] : gen_reg_rtx (mode));
2544           emit_insn (gen_aux_truncdfsf2 (newreg, operands[1]));
2545           operands[1] = newreg;
2546         }
2547     }
2548
2549   /* Handle the case where reload calls us with an invalid address;
2550      and the case of CONSTANT_P_RTX.  */
2551   if (!ALTIVEC_VECTOR_MODE (mode)
2552       && (! general_operand (operands[1], mode)
2553           || ! nonimmediate_operand (operands[0], mode)
2554           || GET_CODE (operands[1]) == CONSTANT_P_RTX))
2555     {
2556       emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2557       return;
2558     }
2559   
2560   /* FIXME:  In the long term, this switch statement should go away
2561      and be replaced by a sequence of tests based on things like
2562      mode == Pmode.  */
2563   switch (mode)
2564     {
2565     case HImode:
2566     case QImode:
2567       if (CONSTANT_P (operands[1])
2568           && GET_CODE (operands[1]) != CONST_INT)
2569         operands[1] = force_const_mem (mode, operands[1]);
2570       break;
2571
2572     case TFmode:
2573     case DFmode:
2574     case SFmode:
2575       if (CONSTANT_P (operands[1]) 
2576           && ! easy_fp_constant (operands[1], mode))
2577         operands[1] = force_const_mem (mode, operands[1]);
2578       break;
2579       
2580     case V16QImode:
2581     case V8HImode:
2582     case V4SFmode:
2583     case V4SImode:
2584     case V4HImode:
2585     case V2SFmode:
2586     case V2SImode:
2587       if (CONSTANT_P (operands[1])
2588           && !easy_vector_constant (operands[1]))
2589         operands[1] = force_const_mem (mode, operands[1]);
2590       break;
2591       
2592     case SImode:
2593     case DImode:
2594       /* Use default pattern for address of ELF small data */
2595       if (TARGET_ELF
2596           && mode == Pmode
2597           && DEFAULT_ABI == ABI_V4
2598           && (GET_CODE (operands[1]) == SYMBOL_REF 
2599               || GET_CODE (operands[1]) == CONST)
2600           && small_data_operand (operands[1], mode))
2601         {
2602           emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2603           return;
2604         }
2605
2606       if (DEFAULT_ABI == ABI_V4
2607           && mode == Pmode && mode == SImode
2608           && flag_pic == 1 && got_operand (operands[1], mode))
2609         {
2610           emit_insn (gen_movsi_got (operands[0], operands[1]));
2611           return;
2612         }
2613
2614       if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
2615           && TARGET_NO_TOC && ! flag_pic
2616           && mode == Pmode
2617           && CONSTANT_P (operands[1])
2618           && GET_CODE (operands[1]) != HIGH
2619           && GET_CODE (operands[1]) != CONST_INT)
2620         {
2621           rtx target = (no_new_pseudos ? operands[0] : gen_reg_rtx (mode));
2622
2623           /* If this is a function address on -mcall-aixdesc,
2624              convert it to the address of the descriptor.  */
2625           if (DEFAULT_ABI == ABI_AIX
2626               && GET_CODE (operands[1]) == SYMBOL_REF
2627               && XSTR (operands[1], 0)[0] == '.')
2628             {
2629               const char *name = XSTR (operands[1], 0);
2630               rtx new_ref;
2631               while (*name == '.')
2632                 name++;
2633               new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
2634               CONSTANT_POOL_ADDRESS_P (new_ref)
2635                 = CONSTANT_POOL_ADDRESS_P (operands[1]);
2636               SYMBOL_REF_FLAG (new_ref) = SYMBOL_REF_FLAG (operands[1]);
2637               SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
2638               operands[1] = new_ref;
2639             }
2640
2641           if (DEFAULT_ABI == ABI_DARWIN)
2642             {
2643               emit_insn (gen_macho_high (target, operands[1]));
2644               emit_insn (gen_macho_low (operands[0], target, operands[1]));
2645               return;
2646             }
2647
2648           emit_insn (gen_elf_high (target, operands[1]));
2649           emit_insn (gen_elf_low (operands[0], target, operands[1]));
2650           return;
2651         }
2652
2653       /* If this is a SYMBOL_REF that refers to a constant pool entry,
2654          and we have put it in the TOC, we just need to make a TOC-relative
2655          reference to it.  */
2656       if (TARGET_TOC
2657           && GET_CODE (operands[1]) == SYMBOL_REF
2658           && CONSTANT_POOL_EXPR_P (operands[1])
2659           && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (operands[1]),
2660                                               get_pool_mode (operands[1])))
2661         {
2662           operands[1] = create_TOC_reference (operands[1]);
2663         }
2664       else if (mode == Pmode
2665                && CONSTANT_P (operands[1])
2666                && ((GET_CODE (operands[1]) != CONST_INT
2667                     && ! easy_fp_constant (operands[1], mode))
2668                    || (GET_CODE (operands[1]) == CONST_INT
2669                        && num_insns_constant (operands[1], mode) > 2)
2670                    || (GET_CODE (operands[0]) == REG
2671                        && FP_REGNO_P (REGNO (operands[0]))))
2672                && GET_CODE (operands[1]) != HIGH
2673                && ! LEGITIMATE_CONSTANT_POOL_ADDRESS_P (operands[1])
2674                && ! TOC_RELATIVE_EXPR_P (operands[1]))
2675         {
2676           /* Emit a USE operation so that the constant isn't deleted if
2677              expensive optimizations are turned on because nobody
2678              references it.  This should only be done for operands that
2679              contain SYMBOL_REFs with CONSTANT_POOL_ADDRESS_P set.
2680              This should not be done for operands that contain LABEL_REFs.
2681              For now, we just handle the obvious case.  */
2682           if (GET_CODE (operands[1]) != LABEL_REF)
2683             emit_insn (gen_rtx_USE (VOIDmode, operands[1]));
2684
2685 #if TARGET_MACHO
2686           /* Darwin uses a special PIC legitimizer.  */
2687           if (DEFAULT_ABI == ABI_DARWIN && flag_pic)
2688             {
2689               operands[1] =
2690                 rs6000_machopic_legitimize_pic_address (operands[1], mode,
2691                                                         operands[0]);
2692               if (operands[0] != operands[1])
2693                 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2694               return;
2695             }
2696 #endif
2697
2698           /* If we are to limit the number of things we put in the TOC and
2699              this is a symbol plus a constant we can add in one insn,
2700              just put the symbol in the TOC and add the constant.  Don't do
2701              this if reload is in progress.  */
2702           if (GET_CODE (operands[1]) == CONST
2703               && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
2704               && GET_CODE (XEXP (operands[1], 0)) == PLUS
2705               && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
2706               && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
2707                   || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
2708               && ! side_effects_p (operands[0]))
2709             {
2710               rtx sym =
2711                 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
2712               rtx other = XEXP (XEXP (operands[1], 0), 1);
2713
2714               sym = force_reg (mode, sym);
2715               if (mode == SImode)
2716                 emit_insn (gen_addsi3 (operands[0], sym, other));
2717               else
2718                 emit_insn (gen_adddi3 (operands[0], sym, other));
2719               return;
2720             }
2721
2722           operands[1] = force_const_mem (mode, operands[1]);
2723
2724           if (TARGET_TOC 
2725               && CONSTANT_POOL_EXPR_P (XEXP (operands[1], 0))
2726               && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
2727                         get_pool_constant (XEXP (operands[1], 0)),
2728                         get_pool_mode (XEXP (operands[1], 0))))
2729             {
2730               operands[1]
2731                 = gen_rtx_MEM (mode,
2732                                create_TOC_reference (XEXP (operands[1], 0)));
2733               set_mem_alias_set (operands[1], get_TOC_alias_set ());
2734               RTX_UNCHANGING_P (operands[1]) = 1;
2735             }
2736         }
2737       break;
2738
2739     case TImode:
2740       if (GET_CODE (operands[0]) == MEM
2741           && GET_CODE (XEXP (operands[0], 0)) != REG
2742           && ! reload_in_progress)
2743         operands[0]
2744           = replace_equiv_address (operands[0],
2745                                    copy_addr_to_reg (XEXP (operands[0], 0)));
2746
2747       if (GET_CODE (operands[1]) == MEM
2748           && GET_CODE (XEXP (operands[1], 0)) != REG
2749           && ! reload_in_progress)
2750         operands[1]
2751           = replace_equiv_address (operands[1],
2752                                    copy_addr_to_reg (XEXP (operands[1], 0)));
2753       break;
2754
2755     default:
2756       abort ();
2757     }
2758
2759   /* Above, we may have called force_const_mem which may have returned
2760      an invalid address.  If we can, fix this up; otherwise, reload will
2761      have to deal with it.  */
2762   if (GET_CODE (operands[1]) == MEM
2763       && ! memory_address_p (mode, XEXP (operands[1], 0))
2764       && ! reload_in_progress)
2765     operands[1] = adjust_address (operands[1], mode, 0);
2766
2767   emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2768   return;
2769 }
2770 \f
2771 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2772    for a call to a function whose data type is FNTYPE.
2773    For a library call, FNTYPE is 0.
2774
2775    For incoming args we set the number of arguments in the prototype large
2776    so we never return a PARALLEL.  */
2777
2778 void
2779 init_cumulative_args (cum, fntype, libname, incoming)
2780      CUMULATIVE_ARGS *cum;
2781      tree fntype;
2782      rtx libname ATTRIBUTE_UNUSED;
2783      int incoming;
2784 {
2785   static CUMULATIVE_ARGS zero_cumulative;
2786
2787   *cum = zero_cumulative;
2788   cum->words = 0;
2789   cum->fregno = FP_ARG_MIN_REG;
2790   cum->vregno = ALTIVEC_ARG_MIN_REG;
2791   cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
2792   cum->call_cookie = CALL_NORMAL;
2793   cum->sysv_gregno = GP_ARG_MIN_REG;
2794
2795   if (incoming)
2796     cum->nargs_prototype = 1000;                /* don't return a PARALLEL */
2797
2798   else if (cum->prototype)
2799     cum->nargs_prototype = (list_length (TYPE_ARG_TYPES (fntype)) - 1
2800                             + (TYPE_MODE (TREE_TYPE (fntype)) == BLKmode
2801                                || RETURN_IN_MEMORY (TREE_TYPE (fntype))));
2802
2803   else
2804     cum->nargs_prototype = 0;
2805
2806   cum->orig_nargs = cum->nargs_prototype;
2807
2808   /* Check for a longcall attribute.  */
2809   if (fntype
2810       && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
2811       && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype)))
2812     cum->call_cookie = CALL_LONG;
2813
2814   if (TARGET_DEBUG_ARG)
2815     {
2816       fprintf (stderr, "\ninit_cumulative_args:");
2817       if (fntype)
2818         {
2819           tree ret_type = TREE_TYPE (fntype);
2820           fprintf (stderr, " ret code = %s,",
2821                    tree_code_name[ (int)TREE_CODE (ret_type) ]);
2822         }
2823
2824       if (cum->call_cookie & CALL_LONG)
2825         fprintf (stderr, " longcall,");
2826
2827       fprintf (stderr, " proto = %d, nargs = %d\n",
2828                cum->prototype, cum->nargs_prototype);
2829     }
2830 }
2831 \f
2832 /* If defined, a C expression which determines whether, and in which
2833    direction, to pad out an argument with extra space.  The value
2834    should be of type `enum direction': either `upward' to pad above
2835    the argument, `downward' to pad below, or `none' to inhibit
2836    padding.
2837
2838    For the AIX ABI structs are always stored left shifted in their
2839    argument slot.  */
2840
2841 enum direction
2842 function_arg_padding (mode, type)
2843      enum machine_mode mode;
2844      tree type;
2845 {
2846   if (type != 0 && AGGREGATE_TYPE_P (type))
2847     return upward;
2848
2849   /* This is the default definition.  */
2850   return (! BYTES_BIG_ENDIAN
2851           ? upward
2852           : ((mode == BLKmode
2853               ? (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
2854                  && int_size_in_bytes (type) < (PARM_BOUNDARY / BITS_PER_UNIT))
2855               : GET_MODE_BITSIZE (mode) < PARM_BOUNDARY)
2856              ? downward : upward));
2857 }
2858
2859 /* If defined, a C expression that gives the alignment boundary, in bits,
2860    of an argument with the specified mode and type.  If it is not defined, 
2861    PARM_BOUNDARY is used for all arguments.
2862    
2863    V.4 wants long longs to be double word aligned.  */
2864
2865 int
2866 function_arg_boundary (mode, type)
2867      enum machine_mode mode;
2868      tree type ATTRIBUTE_UNUSED;
2869 {
2870   if (DEFAULT_ABI == ABI_V4 && (mode == DImode || mode == DFmode))
2871     return 64;
2872    else if (SPE_VECTOR_MODE (mode))
2873      return 64;
2874   else if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
2875     return 128;
2876   else
2877     return PARM_BOUNDARY;
2878 }
2879 \f
2880 /* Update the data in CUM to advance over an argument
2881    of mode MODE and data type TYPE.
2882    (TYPE is null for libcalls where that information may not be available.)  */
2883
2884 void
2885 function_arg_advance (cum, mode, type, named)
2886      CUMULATIVE_ARGS *cum;
2887      enum machine_mode mode;
2888      tree type;
2889      int named;
2890 {
2891   cum->nargs_prototype--;
2892
2893   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
2894     {
2895       if (cum->vregno <= ALTIVEC_ARG_MAX_REG && cum->nargs_prototype >= 0)
2896         cum->vregno++;
2897       else
2898         cum->words += RS6000_ARG_SIZE (mode, type);
2899     }
2900   else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode))
2901     {
2902       cum->words += RS6000_ARG_SIZE (mode, type);
2903       cum->sysv_gregno++;
2904     }
2905   else if (DEFAULT_ABI == ABI_V4)
2906     {
2907       if (TARGET_HARD_FLOAT && TARGET_FPRS
2908           && (mode == SFmode || mode == DFmode))
2909         {
2910           if (cum->fregno <= FP_ARG_V4_MAX_REG)
2911             cum->fregno++;
2912           else
2913             {
2914               if (mode == DFmode)
2915                 cum->words += cum->words & 1;
2916               cum->words += RS6000_ARG_SIZE (mode, type);
2917             }
2918         }
2919       else
2920         {
2921           int n_words;
2922           int gregno = cum->sysv_gregno;
2923
2924           /* Aggregates and IEEE quad get passed by reference.  */
2925           if ((type && AGGREGATE_TYPE_P (type))
2926               || mode == TFmode)
2927             n_words = 1;
2928           else 
2929             n_words = RS6000_ARG_SIZE (mode, type);
2930
2931           /* Long long is put in odd registers.  */
2932           if (n_words == 2 && (gregno & 1) == 0)
2933             gregno += 1;
2934
2935           /* Long long is not split between registers and stack.  */
2936           if (gregno + n_words - 1 > GP_ARG_MAX_REG)
2937             {
2938               /* Long long is aligned on the stack.  */
2939               if (n_words == 2)
2940                 cum->words += cum->words & 1;
2941               cum->words += n_words;
2942             }
2943
2944           /* Note: continuing to accumulate gregno past when we've started
2945              spilling to the stack indicates the fact that we've started
2946              spilling to the stack to expand_builtin_saveregs.  */
2947           cum->sysv_gregno = gregno + n_words;
2948         }
2949
2950       if (TARGET_DEBUG_ARG)
2951         {
2952           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
2953                    cum->words, cum->fregno);
2954           fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
2955                    cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
2956           fprintf (stderr, "mode = %4s, named = %d\n",
2957                    GET_MODE_NAME (mode), named);
2958         }
2959     }
2960   else
2961     {
2962       int align = (TARGET_32BIT && (cum->words & 1) != 0
2963                    && function_arg_boundary (mode, type) == 64) ? 1 : 0;
2964
2965       cum->words += align + RS6000_ARG_SIZE (mode, type);
2966
2967       if (GET_MODE_CLASS (mode) == MODE_FLOAT
2968           && TARGET_HARD_FLOAT && TARGET_FPRS)
2969         cum->fregno++;
2970
2971       if (TARGET_DEBUG_ARG)
2972         {
2973           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
2974                    cum->words, cum->fregno);
2975           fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
2976                    cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
2977           fprintf (stderr, "named = %d, align = %d\n", named, align);
2978         }
2979     }
2980 }
2981 \f
2982 /* Determine where to put an argument to a function.
2983    Value is zero to push the argument on the stack,
2984    or a hard register in which to store the argument.
2985
2986    MODE is the argument's machine mode.
2987    TYPE is the data type of the argument (as a tree).
2988     This is null for libcalls where that information may
2989     not be available.
2990    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2991     the preceding args and about the function being called.
2992    NAMED is nonzero if this argument is a named parameter
2993     (otherwise it is an extra parameter matching an ellipsis).
2994
2995    On RS/6000 the first eight words of non-FP are normally in registers
2996    and the rest are pushed.  Under AIX, the first 13 FP args are in registers.
2997    Under V.4, the first 8 FP args are in registers.
2998
2999    If this is floating-point and no prototype is specified, we use
3000    both an FP and integer register (or possibly FP reg and stack).  Library
3001    functions (when TYPE is zero) always have the proper types for args,
3002    so we can pass the FP value just in one register.  emit_library_function
3003    doesn't support PARALLEL anyway.  */
3004
3005 struct rtx_def *
3006 function_arg (cum, mode, type, named)
3007      CUMULATIVE_ARGS *cum;
3008      enum machine_mode mode;
3009      tree type;
3010      int named;
3011 {
3012   enum rs6000_abi abi = DEFAULT_ABI;
3013
3014   /* Return a marker to indicate whether CR1 needs to set or clear the
3015      bit that V.4 uses to say fp args were passed in registers.
3016      Assume that we don't need the marker for software floating point,
3017      or compiler generated library calls.  */
3018   if (mode == VOIDmode)
3019     {
3020       if (abi == ABI_V4
3021           && cum->nargs_prototype < 0
3022           && type && (cum->prototype || TARGET_NO_PROTOTYPE))
3023         {
3024           /* For the SPE, we need to crxor CR6 always.  */
3025           if (TARGET_SPE_ABI)
3026             return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
3027           else if (TARGET_HARD_FLOAT && TARGET_FPRS)
3028             return GEN_INT (cum->call_cookie
3029                             | ((cum->fregno == FP_ARG_MIN_REG)
3030                                ? CALL_V4_SET_FP_ARGS
3031                                : CALL_V4_CLEAR_FP_ARGS));
3032         }
3033
3034       return GEN_INT (cum->call_cookie);
3035     }
3036
3037   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
3038     {
3039       if (named && cum->vregno <= ALTIVEC_ARG_MAX_REG)
3040         return gen_rtx_REG (mode, cum->vregno);
3041       else
3042         return NULL;
3043     }
3044   else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode))
3045     {
3046       if (cum->sysv_gregno - 1 <= GP_ARG_MAX_REG)
3047         return gen_rtx_REG (mode, cum->sysv_gregno);
3048       else
3049         return NULL;
3050     }
3051   else if (abi == ABI_V4)
3052     {
3053       if (TARGET_HARD_FLOAT && TARGET_FPRS
3054           && (mode == SFmode || mode == DFmode))
3055         {
3056           if (cum->fregno <= FP_ARG_V4_MAX_REG)
3057             return gen_rtx_REG (mode, cum->fregno);
3058           else
3059             return NULL;
3060         }
3061       else
3062         {
3063           int n_words;
3064           int gregno = cum->sysv_gregno;
3065
3066           /* Aggregates and IEEE quad get passed by reference.  */
3067           if ((type && AGGREGATE_TYPE_P (type))
3068               || mode == TFmode)
3069             n_words = 1;
3070           else 
3071             n_words = RS6000_ARG_SIZE (mode, type);
3072
3073           /* Long long is put in odd registers.  */
3074           if (n_words == 2 && (gregno & 1) == 0)
3075             gregno += 1;
3076
3077           /* Long long is not split between registers and stack.  */
3078           if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
3079             return gen_rtx_REG (mode, gregno);
3080           else
3081             return NULL;
3082         }
3083     }
3084   else
3085     {
3086       int align = (TARGET_32BIT && (cum->words & 1) != 0
3087                    && function_arg_boundary (mode, type) == 64) ? 1 : 0;
3088       int align_words = cum->words + align;
3089
3090       if (type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
3091         return NULL_RTX;
3092
3093       if (USE_FP_FOR_ARG_P (*cum, mode, type))
3094         {
3095           if (! type
3096               || ((cum->nargs_prototype > 0)
3097                   /* IBM AIX extended its linkage convention definition always
3098                      to require FP args after register save area hole on the
3099                      stack.  */
3100                   && (DEFAULT_ABI != ABI_AIX
3101                       || ! TARGET_XL_CALL
3102                       || (align_words < GP_ARG_NUM_REG))))
3103             return gen_rtx_REG (mode, cum->fregno);
3104
3105           return gen_rtx_PARALLEL (mode,
3106             gen_rtvec (2,
3107                        gen_rtx_EXPR_LIST (VOIDmode,
3108                                 ((align_words >= GP_ARG_NUM_REG)
3109                                  ? NULL_RTX
3110                                  : (align_words
3111                                     + RS6000_ARG_SIZE (mode, type)
3112                                     > GP_ARG_NUM_REG
3113                                     /* If this is partially on the stack, then
3114                                        we only include the portion actually
3115                                        in registers here.  */
3116                                     ? gen_rtx_REG (SImode,
3117                                                GP_ARG_MIN_REG + align_words)
3118                                     : gen_rtx_REG (mode,
3119                                                GP_ARG_MIN_REG + align_words))),
3120                                 const0_rtx),
3121                        gen_rtx_EXPR_LIST (VOIDmode,
3122                                 gen_rtx_REG (mode, cum->fregno),
3123                                 const0_rtx)));
3124         }
3125       else if (align_words < GP_ARG_NUM_REG)
3126         return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
3127       else
3128         return NULL_RTX;
3129     }
3130 }
3131 \f
3132 /* For an arg passed partly in registers and partly in memory,
3133    this is the number of registers used.
3134    For args passed entirely in registers or entirely in memory, zero.  */
3135
3136 int
3137 function_arg_partial_nregs (cum, mode, type, named)
3138      CUMULATIVE_ARGS *cum;
3139      enum machine_mode mode;
3140      tree type;
3141      int named ATTRIBUTE_UNUSED;
3142 {
3143   if (DEFAULT_ABI == ABI_V4)
3144     return 0;
3145
3146   if (USE_FP_FOR_ARG_P (*cum, mode, type)
3147       || USE_ALTIVEC_FOR_ARG_P (*cum, mode, type))
3148     {
3149       if (cum->nargs_prototype >= 0)
3150         return 0;
3151     }
3152
3153   if (cum->words < GP_ARG_NUM_REG
3154       && GP_ARG_NUM_REG < (cum->words + RS6000_ARG_SIZE (mode, type)))
3155     {
3156       int ret = GP_ARG_NUM_REG - cum->words;
3157       if (ret && TARGET_DEBUG_ARG)
3158         fprintf (stderr, "function_arg_partial_nregs: %d\n", ret);
3159
3160       return ret;
3161     }
3162
3163   return 0;
3164 }
3165 \f
3166 /* A C expression that indicates when an argument must be passed by
3167    reference.  If nonzero for an argument, a copy of that argument is
3168    made in memory and a pointer to the argument is passed instead of
3169    the argument itself.  The pointer is passed in whatever way is
3170    appropriate for passing a pointer to that type.
3171
3172    Under V.4, structures and unions are passed by reference.  */
3173
3174 int
3175 function_arg_pass_by_reference (cum, mode, type, named)
3176      CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED;
3177      enum machine_mode mode ATTRIBUTE_UNUSED;
3178      tree type;
3179      int named ATTRIBUTE_UNUSED;
3180 {
3181   if (DEFAULT_ABI == ABI_V4
3182       && ((type && AGGREGATE_TYPE_P (type))
3183           || mode == TFmode))
3184     {
3185       if (TARGET_DEBUG_ARG)
3186         fprintf (stderr, "function_arg_pass_by_reference: aggregate\n");
3187
3188       return 1;
3189     }
3190
3191   return 0;
3192 }
3193 \f
3194 /* Perform any needed actions needed for a function that is receiving a
3195    variable number of arguments. 
3196
3197    CUM is as above.
3198
3199    MODE and TYPE are the mode and type of the current parameter.
3200
3201    PRETEND_SIZE is a variable that should be set to the amount of stack
3202    that must be pushed by the prolog to pretend that our caller pushed
3203    it.
3204
3205    Normally, this macro will push all remaining incoming registers on the
3206    stack and set PRETEND_SIZE to the length of the registers pushed.  */
3207
3208 void
3209 setup_incoming_varargs (cum, mode, type, pretend_size, no_rtl)
3210      CUMULATIVE_ARGS *cum;
3211      enum machine_mode mode;
3212      tree type;
3213      int *pretend_size;
3214      int no_rtl;
3215
3216 {
3217   CUMULATIVE_ARGS next_cum;
3218   int reg_size = TARGET_32BIT ? 4 : 8;
3219   rtx save_area = NULL_RTX, mem;
3220   int first_reg_offset, set;
3221   tree fntype;
3222   int stdarg_p;
3223
3224   fntype = TREE_TYPE (current_function_decl);
3225   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
3226               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
3227                   != void_type_node));
3228
3229   /* For varargs, we do not want to skip the dummy va_dcl argument.
3230      For stdargs, we do want to skip the last named argument.  */
3231   next_cum = *cum;
3232   if (stdarg_p)
3233     function_arg_advance (&next_cum, mode, type, 1);
3234
3235   if (DEFAULT_ABI == ABI_V4)
3236     {
3237       /* Indicate to allocate space on the stack for varargs save area.  */
3238       cfun->machine->sysv_varargs_p = 1;
3239       if (! no_rtl)
3240         save_area = plus_constant (virtual_stack_vars_rtx,
3241                                    - RS6000_VARARGS_SIZE);
3242
3243       first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
3244     }
3245   else
3246     {
3247       first_reg_offset = next_cum.words;
3248       save_area = virtual_incoming_args_rtx;
3249       cfun->machine->sysv_varargs_p = 0;
3250
3251       if (MUST_PASS_IN_STACK (mode, type))
3252         first_reg_offset += RS6000_ARG_SIZE (TYPE_MODE (type), type);
3253     }
3254
3255   set = get_varargs_alias_set ();
3256   if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG)
3257     {
3258       mem = gen_rtx_MEM (BLKmode,
3259                          plus_constant (save_area,
3260                                         first_reg_offset * reg_size)),
3261       set_mem_alias_set (mem, set);
3262       set_mem_align (mem, BITS_PER_WORD);
3263
3264       move_block_from_reg
3265         (GP_ARG_MIN_REG + first_reg_offset, mem,
3266          GP_ARG_NUM_REG - first_reg_offset,
3267          (GP_ARG_NUM_REG - first_reg_offset) * UNITS_PER_WORD);
3268
3269       /* ??? Does ABI_V4 need this at all?  */
3270       *pretend_size = (GP_ARG_NUM_REG - first_reg_offset) * UNITS_PER_WORD;
3271     }
3272
3273   /* Save FP registers if needed.  */
3274   if (DEFAULT_ABI == ABI_V4
3275       && TARGET_HARD_FLOAT && TARGET_FPRS
3276       && ! no_rtl
3277       && next_cum.fregno <= FP_ARG_V4_MAX_REG)
3278     {
3279       int fregno = next_cum.fregno;
3280       rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
3281       rtx lab = gen_label_rtx ();
3282       int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG) * 8);
3283
3284       emit_jump_insn (gen_rtx_SET (VOIDmode,
3285                                    pc_rtx,
3286                                    gen_rtx_IF_THEN_ELSE (VOIDmode,
3287                                             gen_rtx_NE (VOIDmode, cr1,
3288                                                         const0_rtx),
3289                                             gen_rtx_LABEL_REF (VOIDmode, lab),
3290                                             pc_rtx)));
3291
3292       while (fregno <= FP_ARG_V4_MAX_REG)
3293         {
3294           mem = gen_rtx_MEM (DFmode, plus_constant (save_area, off));
3295           set_mem_alias_set (mem, set);
3296           emit_move_insn (mem, gen_rtx_REG (DFmode, fregno));
3297           fregno++;
3298           off += 8;
3299         }
3300
3301       emit_label (lab);
3302     }
3303 }
3304
3305 /* Create the va_list data type.  */
3306
3307 tree
3308 rs6000_build_va_list ()
3309 {
3310   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
3311
3312   /* For AIX, prefer 'char *' because that's what the system
3313      header files like.  */
3314   if (DEFAULT_ABI != ABI_V4)
3315     return build_pointer_type (char_type_node);
3316
3317   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
3318   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
3319
3320   f_gpr = build_decl (FIELD_DECL, get_identifier ("gpr"), 
3321                       unsigned_char_type_node);
3322   f_fpr = build_decl (FIELD_DECL, get_identifier ("fpr"), 
3323                       unsigned_char_type_node);
3324   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
3325                       ptr_type_node);
3326   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
3327                       ptr_type_node);
3328
3329   DECL_FIELD_CONTEXT (f_gpr) = record;
3330   DECL_FIELD_CONTEXT (f_fpr) = record;
3331   DECL_FIELD_CONTEXT (f_ovf) = record;
3332   DECL_FIELD_CONTEXT (f_sav) = record;
3333
3334   TREE_CHAIN (record) = type_decl;
3335   TYPE_NAME (record) = type_decl;
3336   TYPE_FIELDS (record) = f_gpr;
3337   TREE_CHAIN (f_gpr) = f_fpr;
3338   TREE_CHAIN (f_fpr) = f_ovf;
3339   TREE_CHAIN (f_ovf) = f_sav;
3340
3341   layout_type (record);
3342
3343   /* The correct type is an array type of one element.  */
3344   return build_array_type (record, build_index_type (size_zero_node));
3345 }
3346
3347 /* Implement va_start.  */
3348
3349 void
3350 rs6000_va_start (valist, nextarg)
3351      tree valist;
3352      rtx nextarg;
3353 {
3354   HOST_WIDE_INT words, n_gpr, n_fpr;
3355   tree f_gpr, f_fpr, f_ovf, f_sav;
3356   tree gpr, fpr, ovf, sav, t;
3357
3358   /* Only SVR4 needs something special.  */
3359   if (DEFAULT_ABI != ABI_V4)
3360     {
3361       std_expand_builtin_va_start (valist, nextarg);
3362       return;
3363     }
3364
3365   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
3366   f_fpr = TREE_CHAIN (f_gpr);
3367   f_ovf = TREE_CHAIN (f_fpr);
3368   f_sav = TREE_CHAIN (f_ovf);
3369
3370   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
3371   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
3372   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
3373   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
3374   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
3375
3376   /* Count number of gp and fp argument registers used.  */
3377   words = current_function_args_info.words;
3378   n_gpr = current_function_args_info.sysv_gregno - GP_ARG_MIN_REG;
3379   n_fpr = current_function_args_info.fregno - FP_ARG_MIN_REG;
3380
3381   if (TARGET_DEBUG_ARG)
3382     {
3383       fputs ("va_start: words = ", stderr);
3384       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, words);
3385       fputs (", n_gpr = ", stderr);
3386       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, n_gpr);
3387       fputs (", n_fpr = ", stderr);
3388       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, n_fpr);
3389       putc ('\n', stderr);
3390     }
3391
3392   t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, build_int_2 (n_gpr, 0));
3393   TREE_SIDE_EFFECTS (t) = 1;
3394   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3395
3396   t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr, build_int_2 (n_fpr, 0));
3397   TREE_SIDE_EFFECTS (t) = 1;
3398   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3399
3400   /* Find the overflow area.  */
3401   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
3402   if (words != 0)
3403     t = build (PLUS_EXPR, TREE_TYPE (ovf), t,
3404                build_int_2 (words * UNITS_PER_WORD, 0));
3405   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
3406   TREE_SIDE_EFFECTS (t) = 1;
3407   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3408
3409   /* Find the register save area.  */
3410   t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
3411   t = build (PLUS_EXPR, TREE_TYPE (sav), t,
3412              build_int_2 (-RS6000_VARARGS_SIZE, -1));
3413   t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
3414   TREE_SIDE_EFFECTS (t) = 1;
3415   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3416 }
3417
3418 /* Implement va_arg.  */
3419
3420 rtx
3421 rs6000_va_arg (valist, type)
3422      tree valist, type;
3423 {
3424   tree f_gpr, f_fpr, f_ovf, f_sav;
3425   tree gpr, fpr, ovf, sav, reg, t, u;
3426   int indirect_p, size, rsize, n_reg, sav_ofs, sav_scale;
3427   rtx lab_false, lab_over, addr_rtx, r;
3428
3429   if (DEFAULT_ABI != ABI_V4)
3430     return std_expand_builtin_va_arg (valist, type);
3431
3432   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
3433   f_fpr = TREE_CHAIN (f_gpr);
3434   f_ovf = TREE_CHAIN (f_fpr);
3435   f_sav = TREE_CHAIN (f_ovf);
3436
3437   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
3438   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
3439   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
3440   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
3441   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
3442
3443   size = int_size_in_bytes (type);
3444   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3445
3446   if (AGGREGATE_TYPE_P (type) || TYPE_MODE (type) == TFmode)
3447     {
3448       /* Aggregates and long doubles are passed by reference.  */
3449       indirect_p = 1;
3450       reg = gpr;
3451       n_reg = 1;
3452       sav_ofs = 0;
3453       sav_scale = 4;
3454       size = UNITS_PER_WORD;
3455       rsize = 1;
3456     }
3457   else if (FLOAT_TYPE_P (type) && TARGET_HARD_FLOAT && TARGET_FPRS)
3458     {
3459       /* FP args go in FP registers, if present.  */
3460       indirect_p = 0;
3461       reg = fpr;
3462       n_reg = 1;
3463       sav_ofs = 8*4;
3464       sav_scale = 8;
3465     }
3466   else
3467     {
3468       /* Otherwise into GP registers.  */
3469       indirect_p = 0;
3470       reg = gpr;
3471       n_reg = rsize;
3472       sav_ofs = 0;
3473       sav_scale = 4;
3474     }
3475
3476   /* Pull the value out of the saved registers ...  */
3477
3478   lab_false = gen_label_rtx ();
3479   lab_over = gen_label_rtx ();
3480   addr_rtx = gen_reg_rtx (Pmode);
3481
3482   /*  AltiVec vectors never go in registers.  */
3483   if (!TARGET_ALTIVEC || TREE_CODE (type) != VECTOR_TYPE)
3484     {
3485       TREE_THIS_VOLATILE (reg) = 1;
3486       emit_cmp_and_jump_insns
3487         (expand_expr (reg, NULL_RTX, QImode, EXPAND_NORMAL),
3488          GEN_INT (8 - n_reg + 1), GE, const1_rtx, QImode, 1,
3489          lab_false);
3490
3491       /* Long long is aligned in the registers.  */
3492       if (n_reg > 1)
3493         {
3494           u = build (BIT_AND_EXPR, TREE_TYPE (reg), reg,
3495                      build_int_2 (n_reg - 1, 0));
3496           u = build (PLUS_EXPR, TREE_TYPE (reg), reg, u);
3497           u = build (MODIFY_EXPR, TREE_TYPE (reg), reg, u);
3498           TREE_SIDE_EFFECTS (u) = 1;
3499           expand_expr (u, const0_rtx, VOIDmode, EXPAND_NORMAL);
3500         }
3501
3502       if (sav_ofs)
3503         t = build (PLUS_EXPR, ptr_type_node, sav, build_int_2 (sav_ofs, 0));
3504       else
3505         t = sav;
3506
3507       u = build (POSTINCREMENT_EXPR, TREE_TYPE (reg), reg,
3508                  build_int_2 (n_reg, 0));
3509       TREE_SIDE_EFFECTS (u) = 1;
3510
3511       u = build1 (CONVERT_EXPR, integer_type_node, u);
3512       TREE_SIDE_EFFECTS (u) = 1;
3513
3514       u = build (MULT_EXPR, integer_type_node, u, build_int_2 (sav_scale, 0));
3515       TREE_SIDE_EFFECTS (u) = 1;
3516
3517       t = build (PLUS_EXPR, ptr_type_node, t, u);
3518       TREE_SIDE_EFFECTS (t) = 1;
3519
3520       r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
3521       if (r != addr_rtx)
3522         emit_move_insn (addr_rtx, r);
3523
3524       emit_jump_insn (gen_jump (lab_over));
3525       emit_barrier ();
3526     }
3527
3528   emit_label (lab_false);
3529
3530   /* ... otherwise out of the overflow area.  */
3531
3532   /* Make sure we don't find reg 7 for the next int arg.
3533
3534      All AltiVec vectors go in the overflow area.  So in the AltiVec
3535      case we need to get the vectors from the overflow area, but
3536      remember where the GPRs and FPRs are.  */
3537   if (n_reg > 1 && (TREE_CODE (type) != VECTOR_TYPE
3538                     || !TARGET_ALTIVEC))
3539     {
3540       t = build (MODIFY_EXPR, TREE_TYPE (reg), reg, build_int_2 (8, 0));
3541       TREE_SIDE_EFFECTS (t) = 1;
3542       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3543     }
3544
3545   /* Care for on-stack alignment if needed.  */
3546   if (rsize <= 1)
3547     t = ovf;
3548   else
3549     {
3550       int align;
3551
3552       /* AltiVec vectors are 16 byte aligned.  */
3553       if (TARGET_ALTIVEC && TREE_CODE (type) == VECTOR_TYPE)
3554         align = 15;
3555       else
3556         align = 7;
3557
3558       t = build (PLUS_EXPR, TREE_TYPE (ovf), ovf, build_int_2 (align, 0));
3559       t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-align-1, -1));
3560     }
3561   t = save_expr (t);
3562
3563   r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
3564   if (r != addr_rtx)
3565     emit_move_insn (addr_rtx, r);
3566
3567   t = build (PLUS_EXPR, TREE_TYPE (t), t, build_int_2 (size, 0));
3568   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
3569   TREE_SIDE_EFFECTS (t) = 1;
3570   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3571
3572   emit_label (lab_over);
3573
3574   if (indirect_p)
3575     {
3576       r = gen_rtx_MEM (Pmode, addr_rtx);
3577       set_mem_alias_set (r, get_varargs_alias_set ());
3578       emit_move_insn (addr_rtx, r);
3579     }
3580
3581   return addr_rtx;
3582 }
3583
3584 /* Builtins.  */
3585
3586 #define def_builtin(MASK, NAME, TYPE, CODE)                     \
3587 do {                                                            \
3588   if ((MASK) & target_flags)                                    \
3589     builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD,      \
3590                       NULL, NULL_TREE);                         \
3591 } while (0)
3592
3593 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc).  */
3594
3595 static const struct builtin_description bdesc_3arg[] =
3596 {
3597   { MASK_ALTIVEC, CODE_FOR_altivec_vmaddfp, "__builtin_altivec_vmaddfp", ALTIVEC_BUILTIN_VMADDFP },
3598   { MASK_ALTIVEC, CODE_FOR_altivec_vmhaddshs, "__builtin_altivec_vmhaddshs", ALTIVEC_BUILTIN_VMHADDSHS },
3599   { MASK_ALTIVEC, CODE_FOR_altivec_vmhraddshs, "__builtin_altivec_vmhraddshs", ALTIVEC_BUILTIN_VMHRADDSHS },
3600   { MASK_ALTIVEC, CODE_FOR_altivec_vmladduhm, "__builtin_altivec_vmladduhm", ALTIVEC_BUILTIN_VMLADDUHM},
3601   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumubm, "__builtin_altivec_vmsumubm", ALTIVEC_BUILTIN_VMSUMUBM },
3602   { MASK_ALTIVEC, CODE_FOR_altivec_vmsummbm, "__builtin_altivec_vmsummbm", ALTIVEC_BUILTIN_VMSUMMBM },
3603   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhm, "__builtin_altivec_vmsumuhm", ALTIVEC_BUILTIN_VMSUMUHM },
3604   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshm, "__builtin_altivec_vmsumshm", ALTIVEC_BUILTIN_VMSUMSHM },
3605   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhs, "__builtin_altivec_vmsumuhs", ALTIVEC_BUILTIN_VMSUMUHS },
3606   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshs, "__builtin_altivec_vmsumshs", ALTIVEC_BUILTIN_VMSUMSHS },
3607   { MASK_ALTIVEC, CODE_FOR_altivec_vnmsubfp, "__builtin_altivec_vnmsubfp", ALTIVEC_BUILTIN_VNMSUBFP }, 
3608   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4sf, "__builtin_altivec_vperm_4sf", ALTIVEC_BUILTIN_VPERM_4SF },
3609   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4si, "__builtin_altivec_vperm_4si", ALTIVEC_BUILTIN_VPERM_4SI },
3610   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_8hi, "__builtin_altivec_vperm_8hi", ALTIVEC_BUILTIN_VPERM_8HI },
3611   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_16qi, "__builtin_altivec_vperm_16qi", ALTIVEC_BUILTIN_VPERM_16QI },
3612   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4sf, "__builtin_altivec_vsel_4sf", ALTIVEC_BUILTIN_VSEL_4SF },
3613   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4si, "__builtin_altivec_vsel_4si", ALTIVEC_BUILTIN_VSEL_4SI },
3614   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_8hi, "__builtin_altivec_vsel_8hi", ALTIVEC_BUILTIN_VSEL_8HI },
3615   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_16qi, "__builtin_altivec_vsel_16qi", ALTIVEC_BUILTIN_VSEL_16QI },
3616   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_16qi, "__builtin_altivec_vsldoi_16qi", ALTIVEC_BUILTIN_VSLDOI_16QI },
3617   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_8hi, "__builtin_altivec_vsldoi_8hi", ALTIVEC_BUILTIN_VSLDOI_8HI },
3618   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4si, "__builtin_altivec_vsldoi_4si", ALTIVEC_BUILTIN_VSLDOI_4SI },
3619   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4sf, "__builtin_altivec_vsldoi_4sf", ALTIVEC_BUILTIN_VSLDOI_4SF },
3620 };
3621
3622 /* DST operations: void foo (void *, const int, const char).  */
3623
3624 static const struct builtin_description bdesc_dst[] =
3625 {
3626   { MASK_ALTIVEC, CODE_FOR_altivec_dst, "__builtin_altivec_dst", ALTIVEC_BUILTIN_DST },
3627   { MASK_ALTIVEC, CODE_FOR_altivec_dstt, "__builtin_altivec_dstt", ALTIVEC_BUILTIN_DSTT },
3628   { MASK_ALTIVEC, CODE_FOR_altivec_dstst, "__builtin_altivec_dstst", ALTIVEC_BUILTIN_DSTST },
3629   { MASK_ALTIVEC, CODE_FOR_altivec_dststt, "__builtin_altivec_dststt", ALTIVEC_BUILTIN_DSTSTT }
3630 };
3631
3632 /* Simple binary operations: VECc = foo (VECa, VECb).  */
3633
3634 static struct builtin_description bdesc_2arg[] =
3635 {
3636   { MASK_ALTIVEC, CODE_FOR_addv16qi3, "__builtin_altivec_vaddubm", ALTIVEC_BUILTIN_VADDUBM },
3637   { MASK_ALTIVEC, CODE_FOR_addv8hi3, "__builtin_altivec_vadduhm", ALTIVEC_BUILTIN_VADDUHM },
3638   { MASK_ALTIVEC, CODE_FOR_addv4si3, "__builtin_altivec_vadduwm", ALTIVEC_BUILTIN_VADDUWM },
3639   { MASK_ALTIVEC, CODE_FOR_addv4sf3, "__builtin_altivec_vaddfp", ALTIVEC_BUILTIN_VADDFP },
3640   { MASK_ALTIVEC, CODE_FOR_altivec_vaddcuw, "__builtin_altivec_vaddcuw", ALTIVEC_BUILTIN_VADDCUW },
3641   { MASK_ALTIVEC, CODE_FOR_altivec_vaddubs, "__builtin_altivec_vaddubs", ALTIVEC_BUILTIN_VADDUBS },
3642   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsbs, "__builtin_altivec_vaddsbs", ALTIVEC_BUILTIN_VADDSBS },
3643   { MASK_ALTIVEC, CODE_FOR_altivec_vadduhs, "__builtin_altivec_vadduhs", ALTIVEC_BUILTIN_VADDUHS },
3644   { MASK_ALTIVEC, CODE_FOR_altivec_vaddshs, "__builtin_altivec_vaddshs", ALTIVEC_BUILTIN_VADDSHS },
3645   { MASK_ALTIVEC, CODE_FOR_altivec_vadduws, "__builtin_altivec_vadduws", ALTIVEC_BUILTIN_VADDUWS },
3646   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsws, "__builtin_altivec_vaddsws", ALTIVEC_BUILTIN_VADDSWS },
3647   { MASK_ALTIVEC, CODE_FOR_andv4si3, "__builtin_altivec_vand", ALTIVEC_BUILTIN_VAND },
3648   { MASK_ALTIVEC, CODE_FOR_altivec_vandc, "__builtin_altivec_vandc", ALTIVEC_BUILTIN_VANDC },
3649   { MASK_ALTIVEC, CODE_FOR_altivec_vavgub, "__builtin_altivec_vavgub", ALTIVEC_BUILTIN_VAVGUB },
3650   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsb, "__builtin_altivec_vavgsb", ALTIVEC_BUILTIN_VAVGSB },
3651   { MASK_ALTIVEC, CODE_FOR_altivec_vavguh, "__builtin_altivec_vavguh", ALTIVEC_BUILTIN_VAVGUH },
3652   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsh, "__builtin_altivec_vavgsh", ALTIVEC_BUILTIN_VAVGSH },
3653   { MASK_ALTIVEC, CODE_FOR_altivec_vavguw, "__builtin_altivec_vavguw", ALTIVEC_BUILTIN_VAVGUW },
3654   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsw, "__builtin_altivec_vavgsw", ALTIVEC_BUILTIN_VAVGSW },
3655   { MASK_ALTIVEC, CODE_FOR_altivec_vcfux, "__builtin_altivec_vcfux", ALTIVEC_BUILTIN_VCFUX },
3656   { MASK_ALTIVEC, CODE_FOR_altivec_vcfsx, "__builtin_altivec_vcfsx", ALTIVEC_BUILTIN_VCFSX },
3657   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpbfp, "__builtin_altivec_vcmpbfp", ALTIVEC_BUILTIN_VCMPBFP },
3658   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequb, "__builtin_altivec_vcmpequb", ALTIVEC_BUILTIN_VCMPEQUB },
3659   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequh, "__builtin_altivec_vcmpequh", ALTIVEC_BUILTIN_VCMPEQUH },
3660   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequw, "__builtin_altivec_vcmpequw", ALTIVEC_BUILTIN_VCMPEQUW },
3661   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpeqfp, "__builtin_altivec_vcmpeqfp", ALTIVEC_BUILTIN_VCMPEQFP },
3662   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgefp, "__builtin_altivec_vcmpgefp", ALTIVEC_BUILTIN_VCMPGEFP },
3663   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtub, "__builtin_altivec_vcmpgtub", ALTIVEC_BUILTIN_VCMPGTUB },
3664   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsb, "__builtin_altivec_vcmpgtsb", ALTIVEC_BUILTIN_VCMPGTSB },
3665   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuh, "__builtin_altivec_vcmpgtuh", ALTIVEC_BUILTIN_VCMPGTUH },
3666   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsh, "__builtin_altivec_vcmpgtsh", ALTIVEC_BUILTIN_VCMPGTSH },
3667   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuw, "__builtin_altivec_vcmpgtuw", ALTIVEC_BUILTIN_VCMPGTUW },
3668   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsw, "__builtin_altivec_vcmpgtsw", ALTIVEC_BUILTIN_VCMPGTSW },
3669   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtfp, "__builtin_altivec_vcmpgtfp", ALTIVEC_BUILTIN_VCMPGTFP },
3670   { MASK_ALTIVEC, CODE_FOR_altivec_vctsxs, "__builtin_altivec_vctsxs", ALTIVEC_BUILTIN_VCTSXS },
3671   { MASK_ALTIVEC, CODE_FOR_altivec_vctuxs, "__builtin_altivec_vctuxs", ALTIVEC_BUILTIN_VCTUXS },
3672   { MASK_ALTIVEC, CODE_FOR_umaxv16qi3, "__builtin_altivec_vmaxub", ALTIVEC_BUILTIN_VMAXUB },
3673   { MASK_ALTIVEC, CODE_FOR_smaxv16qi3, "__builtin_altivec_vmaxsb", ALTIVEC_BUILTIN_VMAXSB },
3674   { MASK_ALTIVEC, CODE_FOR_umaxv8hi3, "__builtin_altivec_vmaxuh", ALTIVEC_BUILTIN_VMAXUH },
3675   { MASK_ALTIVEC, CODE_FOR_smaxv8hi3, "__builtin_altivec_vmaxsh", ALTIVEC_BUILTIN_VMAXSH },
3676   { MASK_ALTIVEC, CODE_FOR_umaxv4si3, "__builtin_altivec_vmaxuw", ALTIVEC_BUILTIN_VMAXUW },
3677   { MASK_ALTIVEC, CODE_FOR_smaxv4si3, "__builtin_altivec_vmaxsw", ALTIVEC_BUILTIN_VMAXSW },
3678   { MASK_ALTIVEC, CODE_FOR_smaxv4sf3, "__builtin_altivec_vmaxfp", ALTIVEC_BUILTIN_VMAXFP },
3679   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghb, "__builtin_altivec_vmrghb", ALTIVEC_BUILTIN_VMRGHB },
3680   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghh, "__builtin_altivec_vmrghh", ALTIVEC_BUILTIN_VMRGHH },
3681   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghw, "__builtin_altivec_vmrghw", ALTIVEC_BUILTIN_VMRGHW },
3682   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglb, "__builtin_altivec_vmrglb", ALTIVEC_BUILTIN_VMRGLB },
3683   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglh, "__builtin_altivec_vmrglh", ALTIVEC_BUILTIN_VMRGLH },
3684   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglw, "__builtin_altivec_vmrglw", ALTIVEC_BUILTIN_VMRGLW },
3685   { MASK_ALTIVEC, CODE_FOR_uminv16qi3, "__builtin_altivec_vminub", ALTIVEC_BUILTIN_VMINUB },
3686   { MASK_ALTIVEC, CODE_FOR_sminv16qi3, "__builtin_altivec_vminsb", ALTIVEC_BUILTIN_VMINSB },
3687   { MASK_ALTIVEC, CODE_FOR_uminv8hi3, "__builtin_altivec_vminuh", ALTIVEC_BUILTIN_VMINUH },
3688   { MASK_ALTIVEC, CODE_FOR_sminv8hi3, "__builtin_altivec_vminsh", ALTIVEC_BUILTIN_VMINSH },
3689   { MASK_ALTIVEC, CODE_FOR_uminv4si3, "__builtin_altivec_vminuw", ALTIVEC_BUILTIN_VMINUW },
3690   { MASK_ALTIVEC, CODE_FOR_sminv4si3, "__builtin_altivec_vminsw", ALTIVEC_BUILTIN_VMINSW },
3691   { MASK_ALTIVEC, CODE_FOR_sminv4sf3, "__builtin_altivec_vminfp", ALTIVEC_BUILTIN_VMINFP },
3692   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleub, "__builtin_altivec_vmuleub", ALTIVEC_BUILTIN_VMULEUB },
3693   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesb, "__builtin_altivec_vmulesb", ALTIVEC_BUILTIN_VMULESB },
3694   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleuh, "__builtin_altivec_vmuleuh", ALTIVEC_BUILTIN_VMULEUH },
3695   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesh, "__builtin_altivec_vmulesh", ALTIVEC_BUILTIN_VMULESH },
3696   { MASK_ALTIVEC, CODE_FOR_altivec_vmuloub, "__builtin_altivec_vmuloub", ALTIVEC_BUILTIN_VMULOUB },
3697   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosb, "__builtin_altivec_vmulosb", ALTIVEC_BUILTIN_VMULOSB },
3698   { MASK_ALTIVEC, CODE_FOR_altivec_vmulouh, "__builtin_altivec_vmulouh", ALTIVEC_BUILTIN_VMULOUH },
3699   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosh, "__builtin_altivec_vmulosh", ALTIVEC_BUILTIN_VMULOSH },
3700   { MASK_ALTIVEC, CODE_FOR_altivec_vnor, "__builtin_altivec_vnor", ALTIVEC_BUILTIN_VNOR },
3701   { MASK_ALTIVEC, CODE_FOR_iorv4si3, "__builtin_altivec_vor", ALTIVEC_BUILTIN_VOR },
3702   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum, "__builtin_altivec_vpkuhum", ALTIVEC_BUILTIN_VPKUHUM },
3703   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum, "__builtin_altivec_vpkuwum", ALTIVEC_BUILTIN_VPKUWUM },
3704   { MASK_ALTIVEC, CODE_FOR_altivec_vpkpx, "__builtin_altivec_vpkpx", ALTIVEC_BUILTIN_VPKPX },
3705   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhss, "__builtin_altivec_vpkuhss", ALTIVEC_BUILTIN_VPKUHSS },
3706   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshss, "__builtin_altivec_vpkshss", ALTIVEC_BUILTIN_VPKSHSS },
3707   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwss, "__builtin_altivec_vpkuwss", ALTIVEC_BUILTIN_VPKUWSS },
3708   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswss, "__builtin_altivec_vpkswss", ALTIVEC_BUILTIN_VPKSWSS },
3709   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhus, "__builtin_altivec_vpkuhus", ALTIVEC_BUILTIN_VPKUHUS },
3710   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshus, "__builtin_altivec_vpkshus", ALTIVEC_BUILTIN_VPKSHUS },
3711   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwus, "__builtin_altivec_vpkuwus", ALTIVEC_BUILTIN_VPKUWUS },
3712   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswus, "__builtin_altivec_vpkswus", ALTIVEC_BUILTIN_VPKSWUS },
3713   { MASK_ALTIVEC, CODE_FOR_altivec_vrlb, "__builtin_altivec_vrlb", ALTIVEC_BUILTIN_VRLB },
3714   { MASK_ALTIVEC, CODE_FOR_altivec_vrlh, "__builtin_altivec_vrlh", ALTIVEC_BUILTIN_VRLH },
3715   { MASK_ALTIVEC, CODE_FOR_altivec_vrlw, "__builtin_altivec_vrlw", ALTIVEC_BUILTIN_VRLW },
3716   { MASK_ALTIVEC, CODE_FOR_altivec_vslb, "__builtin_altivec_vslb", ALTIVEC_BUILTIN_VSLB },
3717   { MASK_ALTIVEC, CODE_FOR_altivec_vslh, "__builtin_altivec_vslh", ALTIVEC_BUILTIN_VSLH },
3718   { MASK_ALTIVEC, CODE_FOR_altivec_vslw, "__builtin_altivec_vslw", ALTIVEC_BUILTIN_VSLW },
3719   { MASK_ALTIVEC, CODE_FOR_altivec_vsl, "__builtin_altivec_vsl", ALTIVEC_BUILTIN_VSL },
3720   { MASK_ALTIVEC, CODE_FOR_altivec_vslo, "__builtin_altivec_vslo", ALTIVEC_BUILTIN_VSLO },
3721   { MASK_ALTIVEC, CODE_FOR_altivec_vspltb, "__builtin_altivec_vspltb", ALTIVEC_BUILTIN_VSPLTB },
3722   { MASK_ALTIVEC, CODE_FOR_altivec_vsplth, "__builtin_altivec_vsplth", ALTIVEC_BUILTIN_VSPLTH },
3723   { MASK_ALTIVEC, CODE_FOR_altivec_vspltw, "__builtin_altivec_vspltw", ALTIVEC_BUILTIN_VSPLTW },
3724   { MASK_ALTIVEC, CODE_FOR_altivec_vsrb, "__builtin_altivec_vsrb", ALTIVEC_BUILTIN_VSRB },
3725   { MASK_ALTIVEC, CODE_FOR_altivec_vsrh, "__builtin_altivec_vsrh", ALTIVEC_BUILTIN_VSRH },
3726   { MASK_ALTIVEC, CODE_FOR_altivec_vsrw, "__builtin_altivec_vsrw", ALTIVEC_BUILTIN_VSRW },
3727   { MASK_ALTIVEC, CODE_FOR_altivec_vsrab, "__builtin_altivec_vsrab", ALTIVEC_BUILTIN_VSRAB },
3728   { MASK_ALTIVEC, CODE_FOR_altivec_vsrah, "__builtin_altivec_vsrah", ALTIVEC_BUILTIN_VSRAH },
3729   { MASK_ALTIVEC, CODE_FOR_altivec_vsraw, "__builtin_altivec_vsraw", ALTIVEC_BUILTIN_VSRAW },
3730   { MASK_ALTIVEC, CODE_FOR_altivec_vsr, "__builtin_altivec_vsr", ALTIVEC_BUILTIN_VSR },
3731   { MASK_ALTIVEC, CODE_FOR_altivec_vsro, "__builtin_altivec_vsro", ALTIVEC_BUILTIN_VSRO },
3732   { MASK_ALTIVEC, CODE_FOR_subv16qi3, "__builtin_altivec_vsububm", ALTIVEC_BUILTIN_VSUBUBM },
3733   { MASK_ALTIVEC, CODE_FOR_subv8hi3, "__builtin_altivec_vsubuhm", ALTIVEC_BUILTIN_VSUBUHM },
3734   { MASK_ALTIVEC, CODE_FOR_subv4si3, "__builtin_altivec_vsubuwm", ALTIVEC_BUILTIN_VSUBUWM },
3735   { MASK_ALTIVEC, CODE_FOR_subv4sf3, "__builtin_altivec_vsubfp", ALTIVEC_BUILTIN_VSUBFP },
3736   { MASK_ALTIVEC, CODE_FOR_altivec_vsubcuw, "__builtin_altivec_vsubcuw", ALTIVEC_BUILTIN_VSUBCUW },
3737   { MASK_ALTIVEC, CODE_FOR_altivec_vsububs, "__builtin_altivec_vsububs", ALTIVEC_BUILTIN_VSUBUBS },
3738   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsbs, "__builtin_altivec_vsubsbs", ALTIVEC_BUILTIN_VSUBSBS },
3739   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuhs, "__builtin_altivec_vsubuhs", ALTIVEC_BUILTIN_VSUBUHS },
3740   { MASK_ALTIVEC, CODE_FOR_altivec_vsubshs, "__builtin_altivec_vsubshs", ALTIVEC_BUILTIN_VSUBSHS },
3741   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuws, "__builtin_altivec_vsubuws", ALTIVEC_BUILTIN_VSUBUWS },
3742   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsws, "__builtin_altivec_vsubsws", ALTIVEC_BUILTIN_VSUBSWS },
3743   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4ubs, "__builtin_altivec_vsum4ubs", ALTIVEC_BUILTIN_VSUM4UBS },
3744   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4sbs, "__builtin_altivec_vsum4sbs", ALTIVEC_BUILTIN_VSUM4SBS },
3745   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4shs, "__builtin_altivec_vsum4shs", ALTIVEC_BUILTIN_VSUM4SHS },
3746   { MASK_ALTIVEC, CODE_FOR_altivec_vsum2sws, "__builtin_altivec_vsum2sws", ALTIVEC_BUILTIN_VSUM2SWS },
3747   { MASK_ALTIVEC, CODE_FOR_altivec_vsumsws, "__builtin_altivec_vsumsws", ALTIVEC_BUILTIN_VSUMSWS },
3748   { MASK_ALTIVEC, CODE_FOR_xorv4si3, "__builtin_altivec_vxor", ALTIVEC_BUILTIN_VXOR },
3749
3750   /* Place holder, leave as first spe builtin.  */
3751   { 0, CODE_FOR_spe_evaddw, "__builtin_spe_evaddw", SPE_BUILTIN_EVADDW },
3752   { 0, CODE_FOR_spe_evand, "__builtin_spe_evand", SPE_BUILTIN_EVAND },
3753   { 0, CODE_FOR_spe_evandc, "__builtin_spe_evandc", SPE_BUILTIN_EVANDC },
3754   { 0, CODE_FOR_spe_evdivws, "__builtin_spe_evdivws", SPE_BUILTIN_EVDIVWS },
3755   { 0, CODE_FOR_spe_evdivwu, "__builtin_spe_evdivwu", SPE_BUILTIN_EVDIVWU },
3756   { 0, CODE_FOR_spe_eveqv, "__builtin_spe_eveqv", SPE_BUILTIN_EVEQV },
3757   { 0, CODE_FOR_spe_evfsadd, "__builtin_spe_evfsadd", SPE_BUILTIN_EVFSADD },
3758   { 0, CODE_FOR_spe_evfsdiv, "__builtin_spe_evfsdiv", SPE_BUILTIN_EVFSDIV },
3759   { 0, CODE_FOR_spe_evfsmul, "__builtin_spe_evfsmul", SPE_BUILTIN_EVFSMUL },
3760   { 0, CODE_FOR_spe_evfssub, "__builtin_spe_evfssub", SPE_BUILTIN_EVFSSUB },
3761   { 0, CODE_FOR_spe_evmergehi, "__builtin_spe_evmergehi", SPE_BUILTIN_EVMERGEHI },
3762   { 0, CODE_FOR_spe_evmergehilo, "__builtin_spe_evmergehilo", SPE_BUILTIN_EVMERGEHILO },
3763   { 0, CODE_FOR_spe_evmergelo, "__builtin_spe_evmergelo", SPE_BUILTIN_EVMERGELO },
3764   { 0, CODE_FOR_spe_evmergelohi, "__builtin_spe_evmergelohi", SPE_BUILTIN_EVMERGELOHI },
3765   { 0, CODE_FOR_spe_evmhegsmfaa, "__builtin_spe_evmhegsmfaa", SPE_BUILTIN_EVMHEGSMFAA },
3766   { 0, CODE_FOR_spe_evmhegsmfan, "__builtin_spe_evmhegsmfan", SPE_BUILTIN_EVMHEGSMFAN },
3767   { 0, CODE_FOR_spe_evmhegsmiaa, "__builtin_spe_evmhegsmiaa", SPE_BUILTIN_EVMHEGSMIAA },
3768   { 0, CODE_FOR_spe_evmhegsmian, "__builtin_spe_evmhegsmian", SPE_BUILTIN_EVMHEGSMIAN },
3769   { 0, CODE_FOR_spe_evmhegumiaa, "__builtin_spe_evmhegumiaa", SPE_BUILTIN_EVMHEGUMIAA },
3770   { 0, CODE_FOR_spe_evmhegumian, "__builtin_spe_evmhegumian", SPE_BUILTIN_EVMHEGUMIAN },
3771   { 0, CODE_FOR_spe_evmhesmf, "__builtin_spe_evmhesmf", SPE_BUILTIN_EVMHESMF },
3772   { 0, CODE_FOR_spe_evmhesmfa, "__builtin_spe_evmhesmfa", SPE_BUILTIN_EVMHESMFA },
3773   { 0, CODE_FOR_spe_evmhesmfaaw, "__builtin_spe_evmhesmfaaw", SPE_BUILTIN_EVMHESMFAAW },
3774   { 0, CODE_FOR_spe_evmhesmfanw, "__builtin_spe_evmhesmfanw", SPE_BUILTIN_EVMHESMFANW },
3775   { 0, CODE_FOR_spe_evmhesmi, "__builtin_spe_evmhesmi", SPE_BUILTIN_EVMHESMI },
3776   { 0, CODE_FOR_spe_evmhesmia, "__builtin_spe_evmhesmia", SPE_BUILTIN_EVMHESMIA },
3777   { 0, CODE_FOR_spe_evmhesmiaaw, "__builtin_spe_evmhesmiaaw", SPE_BUILTIN_EVMHESMIAAW },
3778   { 0, CODE_FOR_spe_evmhesmianw, "__builtin_spe_evmhesmianw", SPE_BUILTIN_EVMHESMIANW },
3779   { 0, CODE_FOR_spe_evmhessf, "__builtin_spe_evmhessf", SPE_BUILTIN_EVMHESSF },
3780   { 0, CODE_FOR_spe_evmhessfa, "__builtin_spe_evmhessfa", SPE_BUILTIN_EVMHESSFA },
3781   { 0, CODE_FOR_spe_evmhessfaaw, "__builtin_spe_evmhessfaaw", SPE_BUILTIN_EVMHESSFAAW },
3782   { 0, CODE_FOR_spe_evmhessfanw, "__builtin_spe_evmhessfanw", SPE_BUILTIN_EVMHESSFANW },
3783   { 0, CODE_FOR_spe_evmhessiaaw, "__builtin_spe_evmhessiaaw", SPE_BUILTIN_EVMHESSIAAW },
3784   { 0, CODE_FOR_spe_evmhessianw, "__builtin_spe_evmhessianw", SPE_BUILTIN_EVMHESSIANW },
3785   { 0, CODE_FOR_spe_evmheumi, "__builtin_spe_evmheumi", SPE_BUILTIN_EVMHEUMI },
3786   { 0, CODE_FOR_spe_evmheumia, "__builtin_spe_evmheumia", SPE_BUILTIN_EVMHEUMIA },
3787   { 0, CODE_FOR_spe_evmheumiaaw, "__builtin_spe_evmheumiaaw", SPE_BUILTIN_EVMHEUMIAAW },
3788   { 0, CODE_FOR_spe_evmheumianw, "__builtin_spe_evmheumianw", SPE_BUILTIN_EVMHEUMIANW },
3789   { 0, CODE_FOR_spe_evmheusiaaw, "__builtin_spe_evmheusiaaw", SPE_BUILTIN_EVMHEUSIAAW },
3790   { 0, CODE_FOR_spe_evmheusianw, "__builtin_spe_evmheusianw", SPE_BUILTIN_EVMHEUSIANW },
3791   { 0, CODE_FOR_spe_evmhogsmfaa, "__builtin_spe_evmhogsmfaa", SPE_BUILTIN_EVMHOGSMFAA },
3792   { 0, CODE_FOR_spe_evmhogsmfan, "__builtin_spe_evmhogsmfan", SPE_BUILTIN_EVMHOGSMFAN },
3793   { 0, CODE_FOR_spe_evmhogsmiaa, "__builtin_spe_evmhogsmiaa", SPE_BUILTIN_EVMHOGSMIAA },
3794   { 0, CODE_FOR_spe_evmhogsmian, "__builtin_spe_evmhogsmian", SPE_BUILTIN_EVMHOGSMIAN },
3795   { 0, CODE_FOR_spe_evmhogumiaa, "__builtin_spe_evmhogumiaa", SPE_BUILTIN_EVMHOGUMIAA },
3796   { 0, CODE_FOR_spe_evmhogumian, "__builtin_spe_evmhogumian", SPE_BUILTIN_EVMHOGUMIAN },
3797   { 0, CODE_FOR_spe_evmhosmf, "__builtin_spe_evmhosmf", SPE_BUILTIN_EVMHOSMF },
3798   { 0, CODE_FOR_spe_evmhosmfa, "__builtin_spe_evmhosmfa", SPE_BUILTIN_EVMHOSMFA },
3799   { 0, CODE_FOR_spe_evmhosmfaaw, "__builtin_spe_evmhosmfaaw", SPE_BUILTIN_EVMHOSMFAAW },
3800   { 0, CODE_FOR_spe_evmhosmfanw, "__builtin_spe_evmhosmfanw", SPE_BUILTIN_EVMHOSMFANW },
3801   { 0, CODE_FOR_spe_evmhosmi, "__builtin_spe_evmhosmi", SPE_BUILTIN_EVMHOSMI },
3802   { 0, CODE_FOR_spe_evmhosmia, "__builtin_spe_evmhosmia", SPE_BUILTIN_EVMHOSMIA },
3803   { 0, CODE_FOR_spe_evmhosmiaaw, "__builtin_spe_evmhosmiaaw", SPE_BUILTIN_EVMHOSMIAAW },
3804   { 0, CODE_FOR_spe_evmhosmianw, "__builtin_spe_evmhosmianw", SPE_BUILTIN_EVMHOSMIANW },
3805   { 0, CODE_FOR_spe_evmhossf, "__builtin_spe_evmhossf", SPE_BUILTIN_EVMHOSSF },
3806   { 0, CODE_FOR_spe_evmhossfa, "__builtin_spe_evmhossfa", SPE_BUILTIN_EVMHOSSFA },
3807   { 0, CODE_FOR_spe_evmhossfaaw, "__builtin_spe_evmhossfaaw", SPE_BUILTIN_EVMHOSSFAAW },
3808   { 0, CODE_FOR_spe_evmhossfanw, "__builtin_spe_evmhossfanw", SPE_BUILTIN_EVMHOSSFANW },
3809   { 0, CODE_FOR_spe_evmhossiaaw, "__builtin_spe_evmhossiaaw", SPE_BUILTIN_EVMHOSSIAAW },
3810   { 0, CODE_FOR_spe_evmhossianw, "__builtin_spe_evmhossianw", SPE_BUILTIN_EVMHOSSIANW },
3811   { 0, CODE_FOR_spe_evmhoumi, "__builtin_spe_evmhoumi", SPE_BUILTIN_EVMHOUMI },
3812   { 0, CODE_FOR_spe_evmhoumia, "__builtin_spe_evmhoumia", SPE_BUILTIN_EVMHOUMIA },
3813   { 0, CODE_FOR_spe_evmhoumiaaw, "__builtin_spe_evmhoumiaaw", SPE_BUILTIN_EVMHOUMIAAW },
3814   { 0, CODE_FOR_spe_evmhoumianw, "__builtin_spe_evmhoumianw", SPE_BUILTIN_EVMHOUMIANW },
3815   { 0, CODE_FOR_spe_evmhousiaaw, "__builtin_spe_evmhousiaaw", SPE_BUILTIN_EVMHOUSIAAW },
3816   { 0, CODE_FOR_spe_evmhousianw, "__builtin_spe_evmhousianw", SPE_BUILTIN_EVMHOUSIANW },
3817   { 0, CODE_FOR_spe_evmwhsmf, "__builtin_spe_evmwhsmf", SPE_BUILTIN_EVMWHSMF },
3818   { 0, CODE_FOR_spe_evmwhsmfa, "__builtin_spe_evmwhsmfa", SPE_BUILTIN_EVMWHSMFA },
3819   { 0, CODE_FOR_spe_evmwhsmi, "__builtin_spe_evmwhsmi", SPE_BUILTIN_EVMWHSMI },
3820   { 0, CODE_FOR_spe_evmwhsmia, "__builtin_spe_evmwhsmia", SPE_BUILTIN_EVMWHSMIA },
3821   { 0, CODE_FOR_spe_evmwhssf, "__builtin_spe_evmwhssf", SPE_BUILTIN_EVMWHSSF },
3822   { 0, CODE_FOR_spe_evmwhssfa, "__builtin_spe_evmwhssfa", SPE_BUILTIN_EVMWHSSFA },
3823   { 0, CODE_FOR_spe_evmwhumi, "__builtin_spe_evmwhumi", SPE_BUILTIN_EVMWHUMI },
3824   { 0, CODE_FOR_spe_evmwhumia, "__builtin_spe_evmwhumia", SPE_BUILTIN_EVMWHUMIA },
3825   { 0, CODE_FOR_spe_evmwlsmf, "__builtin_spe_evmwlsmf", SPE_BUILTIN_EVMWLSMF },
3826   { 0, CODE_FOR_spe_evmwlsmfa, "__builtin_spe_evmwlsmfa", SPE_BUILTIN_EVMWLSMFA },
3827   { 0, CODE_FOR_spe_evmwlsmfaaw, "__builtin_spe_evmwlsmfaaw", SPE_BUILTIN_EVMWLSMFAAW },
3828   { 0, CODE_FOR_spe_evmwlsmfanw, "__builtin_spe_evmwlsmfanw", SPE_BUILTIN_EVMWLSMFANW },
3829   { 0, CODE_FOR_spe_evmwlsmiaaw, "__builtin_spe_evmwlsmiaaw", SPE_BUILTIN_EVMWLSMIAAW },
3830   { 0, CODE_FOR_spe_evmwlsmianw, "__builtin_spe_evmwlsmianw", SPE_BUILTIN_EVMWLSMIANW },
3831   { 0, CODE_FOR_spe_evmwlssf, "__builtin_spe_evmwlssf", SPE_BUILTIN_EVMWLSSF },
3832   { 0, CODE_FOR_spe_evmwlssfa, "__builtin_spe_evmwlssfa", SPE_BUILTIN_EVMWLSSFA },
3833   { 0, CODE_FOR_spe_evmwlssfaaw, "__builtin_spe_evmwlssfaaw", SPE_BUILTIN_EVMWLSSFAAW },
3834   { 0, CODE_FOR_spe_evmwlssfanw, "__builtin_spe_evmwlssfanw", SPE_BUILTIN_EVMWLSSFANW },
3835   { 0, CODE_FOR_spe_evmwlssiaaw, "__builtin_spe_evmwlssiaaw", SPE_BUILTIN_EVMWLSSIAAW },
3836   { 0, CODE_FOR_spe_evmwlssianw, "__builtin_spe_evmwlssianw", SPE_BUILTIN_EVMWLSSIANW },
3837   { 0, CODE_FOR_spe_evmwlumi, "__builtin_spe_evmwlumi", SPE_BUILTIN_EVMWLUMI },
3838   { 0, CODE_FOR_spe_evmwlumia, "__builtin_spe_evmwlumia", SPE_BUILTIN_EVMWLUMIA },
3839   { 0, CODE_FOR_spe_evmwlumiaaw, "__builtin_spe_evmwlumiaaw", SPE_BUILTIN_EVMWLUMIAAW },
3840   { 0, CODE_FOR_spe_evmwlumianw, "__builtin_spe_evmwlumianw", SPE_BUILTIN_EVMWLUMIANW },
3841   { 0, CODE_FOR_spe_evmwlusiaaw, "__builtin_spe_evmwlusiaaw", SPE_BUILTIN_EVMWLUSIAAW },
3842   { 0, CODE_FOR_spe_evmwlusianw, "__builtin_spe_evmwlusianw", SPE_BUILTIN_EVMWLUSIANW },
3843   { 0, CODE_FOR_spe_evmwsmf, "__builtin_spe_evmwsmf", SPE_BUILTIN_EVMWSMF },
3844   { 0, CODE_FOR_spe_evmwsmfa, "__builtin_spe_evmwsmfa", SPE_BUILTIN_EVMWSMFA },
3845   { 0, CODE_FOR_spe_evmwsmfaa, "__builtin_spe_evmwsmfaa", SPE_BUILTIN_EVMWSMFAA },
3846   { 0, CODE_FOR_spe_evmwsmfan, "__builtin_spe_evmwsmfan", SPE_BUILTIN_EVMWSMFAN },
3847   { 0, CODE_FOR_spe_evmwsmi, "__builtin_spe_evmwsmi", SPE_BUILTIN_EVMWSMI },
3848   { 0, CODE_FOR_spe_evmwsmia, "__builtin_spe_evmwsmia", SPE_BUILTIN_EVMWSMIA },
3849   { 0, CODE_FOR_spe_evmwsmiaa, "__builtin_spe_evmwsmiaa", SPE_BUILTIN_EVMWSMIAA },
3850   { 0, CODE_FOR_spe_evmwsmian, "__builtin_spe_evmwsmian", SPE_BUILTIN_EVMWSMIAN },
3851   { 0, CODE_FOR_spe_evmwssf, "__builtin_spe_evmwssf", SPE_BUILTIN_EVMWSSF },
3852   { 0, CODE_FOR_spe_evmwssfa, "__builtin_spe_evmwssfa", SPE_BUILTIN_EVMWSSFA },
3853   { 0, CODE_FOR_spe_evmwssfaa, "__builtin_spe_evmwssfaa", SPE_BUILTIN_EVMWSSFAA },
3854   { 0, CODE_FOR_spe_evmwssfan, "__builtin_spe_evmwssfan", SPE_BUILTIN_EVMWSSFAN },
3855   { 0, CODE_FOR_spe_evmwumi, "__builtin_spe_evmwumi", SPE_BUILTIN_EVMWUMI },
3856   { 0, CODE_FOR_spe_evmwumia, "__builtin_spe_evmwumia", SPE_BUILTIN_EVMWUMIA },
3857   { 0, CODE_FOR_spe_evmwumiaa, "__builtin_spe_evmwumiaa", SPE_BUILTIN_EVMWUMIAA },
3858   { 0, CODE_FOR_spe_evmwumian, "__builtin_spe_evmwumian", SPE_BUILTIN_EVMWUMIAN },
3859   { 0, CODE_FOR_spe_evnand, "__builtin_spe_evnand", SPE_BUILTIN_EVNAND },
3860   { 0, CODE_FOR_spe_evnor, "__builtin_spe_evnor", SPE_BUILTIN_EVNOR },
3861   { 0, CODE_FOR_spe_evor, "__builtin_spe_evor", SPE_BUILTIN_EVOR },
3862   { 0, CODE_FOR_spe_evorc, "__builtin_spe_evorc", SPE_BUILTIN_EVORC },
3863   { 0, CODE_FOR_spe_evrlw, "__builtin_spe_evrlw", SPE_BUILTIN_EVRLW },
3864   { 0, CODE_FOR_spe_evslw, "__builtin_spe_evslw", SPE_BUILTIN_EVSLW },
3865   { 0, CODE_FOR_spe_evsrws, "__builtin_spe_evsrws", SPE_BUILTIN_EVSRWS },
3866   { 0, CODE_FOR_spe_evsrwu, "__builtin_spe_evsrwu", SPE_BUILTIN_EVSRWU },
3867   { 0, CODE_FOR_spe_evsubfw, "__builtin_spe_evsubfw", SPE_BUILTIN_EVSUBFW },
3868
3869   /* SPE binary operations expecting a 5-bit unsigned literal.  */
3870   { 0, CODE_FOR_spe_evaddiw, "__builtin_spe_evaddiw", SPE_BUILTIN_EVADDIW },
3871
3872   { 0, CODE_FOR_spe_evrlwi, "__builtin_spe_evrlwi", SPE_BUILTIN_EVRLWI },
3873   { 0, CODE_FOR_spe_evslwi, "__builtin_spe_evslwi", SPE_BUILTIN_EVSLWI },
3874   { 0, CODE_FOR_spe_evsrwis, "__builtin_spe_evsrwis", SPE_BUILTIN_EVSRWIS },
3875   { 0, CODE_FOR_spe_evsrwiu, "__builtin_spe_evsrwiu", SPE_BUILTIN_EVSRWIU },
3876   { 0, CODE_FOR_spe_evsubifw, "__builtin_spe_evsubifw", SPE_BUILTIN_EVSUBIFW },
3877   { 0, CODE_FOR_spe_evmwhssfaa, "__builtin_spe_evmwhssfaa", SPE_BUILTIN_EVMWHSSFAA },
3878   { 0, CODE_FOR_spe_evmwhssmaa, "__builtin_spe_evmwhssmaa", SPE_BUILTIN_EVMWHSSMAA },
3879   { 0, CODE_FOR_spe_evmwhsmfaa, "__builtin_spe_evmwhsmfaa", SPE_BUILTIN_EVMWHSMFAA },
3880   { 0, CODE_FOR_spe_evmwhsmiaa, "__builtin_spe_evmwhsmiaa", SPE_BUILTIN_EVMWHSMIAA },
3881   { 0, CODE_FOR_spe_evmwhusiaa, "__builtin_spe_evmwhusiaa", SPE_BUILTIN_EVMWHUSIAA },
3882   { 0, CODE_FOR_spe_evmwhumiaa, "__builtin_spe_evmwhumiaa", SPE_BUILTIN_EVMWHUMIAA },
3883   { 0, CODE_FOR_spe_evmwhssfan, "__builtin_spe_evmwhssfan", SPE_BUILTIN_EVMWHSSFAN },
3884   { 0, CODE_FOR_spe_evmwhssian, "__builtin_spe_evmwhssian", SPE_BUILTIN_EVMWHSSIAN },
3885   { 0, CODE_FOR_spe_evmwhsmfan, "__builtin_spe_evmwhsmfan", SPE_BUILTIN_EVMWHSMFAN },
3886   { 0, CODE_FOR_spe_evmwhsmian, "__builtin_spe_evmwhsmian", SPE_BUILTIN_EVMWHSMIAN },
3887   { 0, CODE_FOR_spe_evmwhusian, "__builtin_spe_evmwhusian", SPE_BUILTIN_EVMWHUSIAN },
3888   { 0, CODE_FOR_spe_evmwhumian, "__builtin_spe_evmwhumian", SPE_BUILTIN_EVMWHUMIAN },
3889   { 0, CODE_FOR_spe_evmwhgssfaa, "__builtin_spe_evmwhgssfaa", SPE_BUILTIN_EVMWHGSSFAA },
3890   { 0, CODE_FOR_spe_evmwhgsmfaa, "__builtin_spe_evmwhgsmfaa", SPE_BUILTIN_EVMWHGSMFAA },
3891   { 0, CODE_FOR_spe_evmwhgsmiaa, "__builtin_spe_evmwhgsmiaa", SPE_BUILTIN_EVMWHGSMIAA },
3892   { 0, CODE_FOR_spe_evmwhgumiaa, "__builtin_spe_evmwhgumiaa", SPE_BUILTIN_EVMWHGUMIAA },
3893   { 0, CODE_FOR_spe_evmwhgssfan, "__builtin_spe_evmwhgssfan", SPE_BUILTIN_EVMWHGSSFAN },
3894   { 0, CODE_FOR_spe_evmwhgsmfan, "__builtin_spe_evmwhgsmfan", SPE_BUILTIN_EVMWHGSMFAN },
3895   { 0, CODE_FOR_spe_evmwhgsmian, "__builtin_spe_evmwhgsmian", SPE_BUILTIN_EVMWHGSMIAN },
3896   { 0, CODE_FOR_spe_evmwhgumian, "__builtin_spe_evmwhgumian", SPE_BUILTIN_EVMWHGUMIAN },
3897   { 0, CODE_FOR_spe_brinc, "__builtin_spe_brinc", SPE_BUILTIN_BRINC },
3898
3899   /* Place-holder.  Leave as last binary SPE builtin.  */
3900   { 0, CODE_FOR_spe_evxor, "__builtin_spe_evxor", SPE_BUILTIN_EVXOR },
3901 };
3902
3903 /* AltiVec predicates.  */
3904
3905 struct builtin_description_predicates
3906 {
3907   const unsigned int mask;
3908   const enum insn_code icode;
3909   const char *opcode;
3910   const char *const name;
3911   const enum rs6000_builtins code;
3912 };
3913
3914 static const struct builtin_description_predicates bdesc_altivec_preds[] =
3915 {
3916   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpbfp.", "__builtin_altivec_vcmpbfp_p", ALTIVEC_BUILTIN_VCMPBFP_P },
3917   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpeqfp.", "__builtin_altivec_vcmpeqfp_p", ALTIVEC_BUILTIN_VCMPEQFP_P },
3918   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgefp.", "__builtin_altivec_vcmpgefp_p", ALTIVEC_BUILTIN_VCMPGEFP_P },
3919   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgtfp.", "__builtin_altivec_vcmpgtfp_p", ALTIVEC_BUILTIN_VCMPGTFP_P },
3920   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpequw.", "__builtin_altivec_vcmpequw_p", ALTIVEC_BUILTIN_VCMPEQUW_P },
3921   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtsw.", "__builtin_altivec_vcmpgtsw_p", ALTIVEC_BUILTIN_VCMPGTSW_P },
3922   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtuw.", "__builtin_altivec_vcmpgtuw_p", ALTIVEC_BUILTIN_VCMPGTUW_P },
3923   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtuh.", "__builtin_altivec_vcmpgtuh_p", ALTIVEC_BUILTIN_VCMPGTUH_P },
3924   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtsh.", "__builtin_altivec_vcmpgtsh_p", ALTIVEC_BUILTIN_VCMPGTSH_P },
3925   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpequh.", "__builtin_altivec_vcmpequh_p", ALTIVEC_BUILTIN_VCMPEQUH_P },
3926   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpequb.", "__builtin_altivec_vcmpequb_p", ALTIVEC_BUILTIN_VCMPEQUB_P },
3927   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtsb.", "__builtin_altivec_vcmpgtsb_p", ALTIVEC_BUILTIN_VCMPGTSB_P },
3928   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtub.", "__builtin_altivec_vcmpgtub_p", ALTIVEC_BUILTIN_VCMPGTUB_P }
3929 };
3930
3931 /* SPE predicates.  */
3932 static struct builtin_description bdesc_spe_predicates[] =
3933 {
3934   /* Place-holder.  Leave as first.  */
3935   { 0, CODE_FOR_spe_evcmpeq, "__builtin_spe_evcmpeq", SPE_BUILTIN_EVCMPEQ },
3936   { 0, CODE_FOR_spe_evcmpgts, "__builtin_spe_evcmpgts", SPE_BUILTIN_EVCMPGTS },
3937   { 0, CODE_FOR_spe_evcmpgtu, "__builtin_spe_evcmpgtu", SPE_BUILTIN_EVCMPGTU },
3938   { 0, CODE_FOR_spe_evcmplts, "__builtin_spe_evcmplts", SPE_BUILTIN_EVCMPLTS },
3939   { 0, CODE_FOR_spe_evcmpltu, "__builtin_spe_evcmpltu", SPE_BUILTIN_EVCMPLTU },
3940   { 0, CODE_FOR_spe_evfscmpeq, "__builtin_spe_evfscmpeq", SPE_BUILTIN_EVFSCMPEQ },
3941   { 0, CODE_FOR_spe_evfscmpgt, "__builtin_spe_evfscmpgt", SPE_BUILTIN_EVFSCMPGT },
3942   { 0, CODE_FOR_spe_evfscmplt, "__builtin_spe_evfscmplt", SPE_BUILTIN_EVFSCMPLT },
3943   { 0, CODE_FOR_spe_evfststeq, "__builtin_spe_evfststeq", SPE_BUILTIN_EVFSTSTEQ },
3944   { 0, CODE_FOR_spe_evfststgt, "__builtin_spe_evfststgt", SPE_BUILTIN_EVFSTSTGT },
3945   /* Place-holder.  Leave as last.  */
3946   { 0, CODE_FOR_spe_evfststlt, "__builtin_spe_evfststlt", SPE_BUILTIN_EVFSTSTLT },
3947 };
3948
3949 /* SPE evsel predicates.  */
3950 static struct builtin_description bdesc_spe_evsel[] =
3951 {
3952   /* Place-holder.  Leave as first.  */
3953   { 0, CODE_FOR_spe_evcmpgts, "__builtin_spe_evsel_gts", SPE_BUILTIN_EVSEL_CMPGTS },
3954   { 0, CODE_FOR_spe_evcmpgtu, "__builtin_spe_evsel_gtu", SPE_BUILTIN_EVSEL_CMPGTU },
3955   { 0, CODE_FOR_spe_evcmplts, "__builtin_spe_evsel_lts", SPE_BUILTIN_EVSEL_CMPLTS },
3956   { 0, CODE_FOR_spe_evcmpltu, "__builtin_spe_evsel_ltu", SPE_BUILTIN_EVSEL_CMPLTU },
3957   { 0, CODE_FOR_spe_evcmpeq, "__builtin_spe_evsel_eq", SPE_BUILTIN_EVSEL_CMPEQ },
3958   { 0, CODE_FOR_spe_evfscmpgt, "__builtin_spe_evsel_fsgt", SPE_BUILTIN_EVSEL_FSCMPGT },
3959   { 0, CODE_FOR_spe_evfscmplt, "__builtin_spe_evsel_fslt", SPE_BUILTIN_EVSEL_FSCMPLT },
3960   { 0, CODE_FOR_spe_evfscmpeq, "__builtin_spe_evsel_fseq", SPE_BUILTIN_EVSEL_FSCMPEQ },
3961   { 0, CODE_FOR_spe_evfststgt, "__builtin_spe_evsel_fststgt", SPE_BUILTIN_EVSEL_FSTSTGT },
3962   { 0, CODE_FOR_spe_evfststlt, "__builtin_spe_evsel_fststlt", SPE_BUILTIN_EVSEL_FSTSTLT },
3963   /* Place-holder.  Leave as last.  */
3964   { 0, CODE_FOR_spe_evfststeq, "__builtin_spe_evsel_fststeq", SPE_BUILTIN_EVSEL_FSTSTEQ },
3965 };
3966
3967 /* ABS* opreations.  */
3968
3969 static const struct builtin_description bdesc_abs[] =
3970 {
3971   { MASK_ALTIVEC, CODE_FOR_absv4si2, "__builtin_altivec_abs_v4si", ALTIVEC_BUILTIN_ABS_V4SI },
3972   { MASK_ALTIVEC, CODE_FOR_absv8hi2, "__builtin_altivec_abs_v8hi", ALTIVEC_BUILTIN_ABS_V8HI },
3973   { MASK_ALTIVEC, CODE_FOR_absv4sf2, "__builtin_altivec_abs_v4sf", ALTIVEC_BUILTIN_ABS_V4SF },
3974   { MASK_ALTIVEC, CODE_FOR_absv16qi2, "__builtin_altivec_abs_v16qi", ALTIVEC_BUILTIN_ABS_V16QI },
3975   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v4si, "__builtin_altivec_abss_v4si", ALTIVEC_BUILTIN_ABSS_V4SI },
3976   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v8hi, "__builtin_altivec_abss_v8hi", ALTIVEC_BUILTIN_ABSS_V8HI },
3977   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v16qi, "__builtin_altivec_abss_v16qi", ALTIVEC_BUILTIN_ABSS_V16QI }
3978 };
3979
3980 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
3981    foo (VECa).  */
3982
3983 static struct builtin_description bdesc_1arg[] =
3984 {
3985   { MASK_ALTIVEC, CODE_FOR_altivec_vexptefp, "__builtin_altivec_vexptefp", ALTIVEC_BUILTIN_VEXPTEFP },
3986   { MASK_ALTIVEC, CODE_FOR_altivec_vlogefp, "__builtin_altivec_vlogefp", ALTIVEC_BUILTIN_VLOGEFP },
3987   { MASK_ALTIVEC, CODE_FOR_altivec_vrefp, "__builtin_altivec_vrefp", ALTIVEC_BUILTIN_VREFP },
3988   { MASK_ALTIVEC, CODE_FOR_altivec_vrfim, "__builtin_altivec_vrfim", ALTIVEC_BUILTIN_VRFIM },
3989   { MASK_ALTIVEC, CODE_FOR_altivec_vrfin, "__builtin_altivec_vrfin", ALTIVEC_BUILTIN_VRFIN },
3990   { MASK_ALTIVEC, CODE_FOR_altivec_vrfip, "__builtin_altivec_vrfip", ALTIVEC_BUILTIN_VRFIP },
3991   { MASK_ALTIVEC, CODE_FOR_ftruncv4sf2, "__builtin_altivec_vrfiz", ALTIVEC_BUILTIN_VRFIZ },
3992   { MASK_ALTIVEC, CODE_FOR_altivec_vrsqrtefp, "__builtin_altivec_vrsqrtefp", ALTIVEC_BUILTIN_VRSQRTEFP },
3993   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisb, "__builtin_altivec_vspltisb", ALTIVEC_BUILTIN_VSPLTISB },
3994   { MASK_ALTIVEC, CODE_FOR_altivec_vspltish, "__builtin_altivec_vspltish", ALTIVEC_BUILTIN_VSPLTISH },
3995   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisw, "__builtin_altivec_vspltisw", ALTIVEC_BUILTIN_VSPLTISW },
3996   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsb, "__builtin_altivec_vupkhsb", ALTIVEC_BUILTIN_VUPKHSB },
3997   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhpx, "__builtin_altivec_vupkhpx", ALTIVEC_BUILTIN_VUPKHPX },
3998   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsh, "__builtin_altivec_vupkhsh", ALTIVEC_BUILTIN_VUPKHSH },
3999   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsb, "__builtin_altivec_vupklsb", ALTIVEC_BUILTIN_VUPKLSB },
4000   { MASK_ALTIVEC, CODE_FOR_altivec_vupklpx, "__builtin_altivec_vupklpx", ALTIVEC_BUILTIN_VUPKLPX },
4001   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsh, "__builtin_altivec_vupklsh", ALTIVEC_BUILTIN_VUPKLSH },
4002
4003   /* The SPE unary builtins must start with SPE_BUILTIN_EVABS and
4004      end with SPE_BUILTIN_EVSUBFUSIAAW.  */
4005   { 0, CODE_FOR_spe_evabs, "__builtin_spe_evabs", SPE_BUILTIN_EVABS },
4006   { 0, CODE_FOR_spe_evaddsmiaaw, "__builtin_spe_evaddsmiaaw", SPE_BUILTIN_EVADDSMIAAW },
4007   { 0, CODE_FOR_spe_evaddssiaaw, "__builtin_spe_evaddssiaaw", SPE_BUILTIN_EVADDSSIAAW },
4008   { 0, CODE_FOR_spe_evaddumiaaw, "__builtin_spe_evaddumiaaw", SPE_BUILTIN_EVADDUMIAAW },
4009   { 0, CODE_FOR_spe_evaddusiaaw, "__builtin_spe_evaddusiaaw", SPE_BUILTIN_EVADDUSIAAW },
4010   { 0, CODE_FOR_spe_evcntlsw, "__builtin_spe_evcntlsw", SPE_BUILTIN_EVCNTLSW },
4011   { 0, CODE_FOR_spe_evcntlzw, "__builtin_spe_evcntlzw", SPE_BUILTIN_EVCNTLZW },
4012   { 0, CODE_FOR_spe_evextsb, "__builtin_spe_evextsb", SPE_BUILTIN_EVEXTSB },
4013   { 0, CODE_FOR_spe_evextsh, "__builtin_spe_evextsh", SPE_BUILTIN_EVEXTSH },
4014   { 0, CODE_FOR_spe_evfsabs, "__builtin_spe_evfsabs", SPE_BUILTIN_EVFSABS },
4015   { 0, CODE_FOR_spe_evfscfsf, "__builtin_spe_evfscfsf", SPE_BUILTIN_EVFSCFSF },
4016   { 0, CODE_FOR_spe_evfscfsi, "__builtin_spe_evfscfsi", SPE_BUILTIN_EVFSCFSI },
4017   { 0, CODE_FOR_spe_evfscfuf, "__builtin_spe_evfscfuf", SPE_BUILTIN_EVFSCFUF },
4018   { 0, CODE_FOR_spe_evfscfui, "__builtin_spe_evfscfui", SPE_BUILTIN_EVFSCFUI },
4019   { 0, CODE_FOR_spe_evfsctsf, "__builtin_spe_evfsctsf", SPE_BUILTIN_EVFSCTSF },
4020   { 0, CODE_FOR_spe_evfsctsi, "__builtin_spe_evfsctsi", SPE_BUILTIN_EVFSCTSI },
4021   { 0, CODE_FOR_spe_evfsctsiz, "__builtin_spe_evfsctsiz", SPE_BUILTIN_EVFSCTSIZ },
4022   { 0, CODE_FOR_spe_evfsctuf, "__builtin_spe_evfsctuf", SPE_BUILTIN_EVFSCTUF },
4023   { 0, CODE_FOR_spe_evfsctui, "__builtin_spe_evfsctui", SPE_BUILTIN_EVFSCTUI },
4024   { 0, CODE_FOR_spe_evfsctuiz, "__builtin_spe_evfsctuiz", SPE_BUILTIN_EVFSCTUIZ },
4025   { 0, CODE_FOR_spe_evfsnabs, "__builtin_spe_evfsnabs", SPE_BUILTIN_EVFSNABS },
4026   { 0, CODE_FOR_spe_evfsneg, "__builtin_spe_evfsneg", SPE_BUILTIN_EVFSNEG },
4027   { 0, CODE_FOR_spe_evmra, "__builtin_spe_evmra", SPE_BUILTIN_EVMRA },
4028   { 0, CODE_FOR_spe_evneg, "__builtin_spe_evneg", SPE_BUILTIN_EVNEG },
4029   { 0, CODE_FOR_spe_evrndw, "__builtin_spe_evrndw", SPE_BUILTIN_EVRNDW },
4030   { 0, CODE_FOR_spe_evsubfsmiaaw, "__builtin_spe_evsubfsmiaaw", SPE_BUILTIN_EVSUBFSMIAAW },
4031   { 0, CODE_FOR_spe_evsubfssiaaw, "__builtin_spe_evsubfssiaaw", SPE_BUILTIN_EVSUBFSSIAAW },
4032   { 0, CODE_FOR_spe_evsubfumiaaw, "__builtin_spe_evsubfumiaaw", SPE_BUILTIN_EVSUBFUMIAAW },
4033   { 0, CODE_FOR_spe_evsplatfi, "__builtin_spe_evsplatfi", SPE_BUILTIN_EVSPLATFI },
4034   { 0, CODE_FOR_spe_evsplati, "__builtin_spe_evsplati", SPE_BUILTIN_EVSPLATI },
4035
4036   /* Place-holder.  Leave as last unary SPE builtin.  */
4037   { 0, CODE_FOR_spe_evsubfusiaaw, "__builtin_spe_evsubfusiaaw", SPE_BUILTIN_EVSUBFUSIAAW },
4038 };
4039
4040 static rtx
4041 rs6000_expand_unop_builtin (icode, arglist, target)
4042      enum insn_code icode;
4043      tree arglist;
4044      rtx target;
4045 {
4046   rtx pat;
4047   tree arg0 = TREE_VALUE (arglist);
4048   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4049   enum machine_mode tmode = insn_data[icode].operand[0].mode;
4050   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
4051
4052   /* If we got invalid arguments bail out before generating bad rtl.  */
4053   if (arg0 == error_mark_node)
4054     return const0_rtx;
4055
4056   switch (icode)
4057     {
4058       /* Only allow 5-bit *signed* literals.  */
4059     case CODE_FOR_altivec_vspltisb:
4060     case CODE_FOR_altivec_vspltish:
4061     case CODE_FOR_altivec_vspltisw:
4062     case CODE_FOR_spe_evsplatfi:
4063     case CODE_FOR_spe_evsplati:
4064       if (GET_CODE (op0) != CONST_INT
4065           || INTVAL (op0) > 0x1f
4066           || INTVAL (op0) < -0x1f)
4067         {
4068           error ("argument 1 must be a 5-bit signed literal");
4069           return const0_rtx;
4070         }
4071       break;
4072     default:
4073       break;
4074     }
4075
4076   if (target == 0
4077       || GET_MODE (target) != tmode
4078       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4079     target = gen_reg_rtx (tmode);
4080
4081   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
4082     op0 = copy_to_mode_reg (mode0, op0);
4083
4084   pat = GEN_FCN (icode) (target, op0);
4085   if (! pat)
4086     return 0;
4087   emit_insn (pat);
4088
4089   return target;
4090 }
4091
4092 static rtx
4093 altivec_expand_abs_builtin (icode, arglist, target)
4094      enum insn_code icode;
4095      tree arglist;
4096      rtx target;
4097 {
4098   rtx pat, scratch1, scratch2;
4099   tree arg0 = TREE_VALUE (arglist);
4100   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4101   enum machine_mode tmode = insn_data[icode].operand[0].mode;
4102   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
4103
4104   /* If we have invalid arguments, bail out before generating bad rtl.  */
4105   if (arg0 == error_mark_node)
4106     return const0_rtx;
4107
4108   if (target == 0
4109       || GET_MODE (target) != tmode
4110       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4111     target = gen_reg_rtx (tmode);
4112
4113   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
4114     op0 = copy_to_mode_reg (mode0, op0);
4115
4116   scratch1 = gen_reg_rtx (mode0);
4117   scratch2 = gen_reg_rtx (mode0);
4118
4119   pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
4120   if (! pat)
4121     return 0;
4122   emit_insn (pat);
4123
4124   return target;
4125 }
4126
4127 static rtx
4128 rs6000_expand_binop_builtin (icode, arglist, target)
4129      enum insn_code icode;
4130      tree arglist;
4131      rtx target;
4132 {
4133   rtx pat;
4134   tree arg0 = TREE_VALUE (arglist);
4135   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4136   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4137   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
4138   enum machine_mode tmode = insn_data[icode].operand[0].mode;
4139   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
4140   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
4141
4142   /* If we got invalid arguments bail out before generating bad rtl.  */
4143   if (arg0 == error_mark_node || arg1 == error_mark_node)
4144     return const0_rtx;
4145
4146   switch (icode)
4147     {
4148       /* Only allow 5-bit unsigned literals.  */
4149     case CODE_FOR_altivec_vcfux:
4150     case CODE_FOR_altivec_vcfsx:
4151     case CODE_FOR_altivec_vctsxs:
4152     case CODE_FOR_altivec_vctuxs:
4153     case CODE_FOR_altivec_vspltb:
4154     case CODE_FOR_altivec_vsplth:
4155     case CODE_FOR_altivec_vspltw:
4156     case CODE_FOR_spe_evaddiw:
4157     case CODE_FOR_spe_evldd:
4158     case CODE_FOR_spe_evldh:
4159     case CODE_FOR_spe_evldw:
4160     case CODE_FOR_spe_evlhhesplat:
4161     case CODE_FOR_spe_evlhhossplat:
4162     case CODE_FOR_spe_evlhhousplat:
4163     case CODE_FOR_spe_evlwhe:
4164     case CODE_FOR_spe_evlwhos:
4165     case CODE_FOR_spe_evlwhou:
4166     case CODE_FOR_spe_evlwhsplat:
4167     case CODE_FOR_spe_evlwwsplat:
4168     case CODE_FOR_spe_evrlwi:
4169     case CODE_FOR_spe_evslwi:
4170     case CODE_FOR_spe_evsrwis:
4171     case CODE_FOR_spe_evsrwiu:
4172       if (TREE_CODE (arg1) != INTEGER_CST
4173           || TREE_INT_CST_LOW (arg1) & ~0x1f)
4174         {
4175           error ("argument 2 must be a 5-bit unsigned literal");
4176           return const0_rtx;
4177         }
4178       break;
4179     default:
4180       break;
4181     }
4182
4183   if (target == 0
4184       || GET_MODE (target) != tmode
4185       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4186     target = gen_reg_rtx (tmode);
4187
4188   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
4189     op0 = copy_to_mode_reg (mode0, op0);
4190   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
4191     op1 = copy_to_mode_reg (mode1, op1);
4192
4193   pat = GEN_FCN (icode) (target, op0, op1);
4194   if (! pat)
4195     return 0;
4196   emit_insn (pat);
4197
4198   return target;
4199 }
4200
4201 static rtx
4202 altivec_expand_predicate_builtin (icode, opcode, arglist, target)
4203      enum insn_code icode;
4204      const char *opcode;
4205      tree arglist;
4206      rtx target;
4207 {
4208   rtx pat, scratch;
4209   tree cr6_form = TREE_VALUE (arglist);
4210   tree arg0 = TREE_VALUE (TREE_CHAIN (arglist));
4211   tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
4212   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4213   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
4214   enum machine_mode tmode = SImode;
4215   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
4216   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
4217   int cr6_form_int;
4218
4219   if (TREE_CODE (cr6_form) != INTEGER_CST)
4220     {
4221       error ("argument 1 of __builtin_altivec_predicate must be a constant");
4222       return const0_rtx;
4223     }
4224   else
4225     cr6_form_int = TREE_INT_CST_LOW (cr6_form);
4226
4227   if (mode0 != mode1)
4228     abort ();
4229
4230   /* If we have invalid arguments, bail out before generating bad rtl.  */
4231   if (arg0 == error_mark_node || arg1 == error_mark_node)
4232     return const0_rtx;
4233
4234   if (target == 0
4235       || GET_MODE (target) != tmode
4236       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4237     target = gen_reg_rtx (tmode);
4238
4239   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
4240     op0 = copy_to_mode_reg (mode0, op0);
4241   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
4242     op1 = copy_to_mode_reg (mode1, op1);
4243
4244   scratch = gen_reg_rtx (mode0);
4245
4246   pat = GEN_FCN (icode) (scratch, op0, op1,
4247                          gen_rtx (SYMBOL_REF, Pmode, opcode));
4248   if (! pat)
4249     return 0;
4250   emit_insn (pat);
4251
4252   /* The vec_any* and vec_all* predicates use the same opcodes for two
4253      different operations, but the bits in CR6 will be different
4254      depending on what information we want.  So we have to play tricks
4255      with CR6 to get the right bits out.
4256
4257      If you think this is disgusting, look at the specs for the
4258      AltiVec predicates.  */
4259
4260      switch (cr6_form_int)
4261        {
4262        case 0:
4263          emit_insn (gen_cr6_test_for_zero (target));
4264          break;
4265        case 1:
4266          emit_insn (gen_cr6_test_for_zero_reverse (target));
4267          break;
4268        case 2:
4269          emit_insn (gen_cr6_test_for_lt (target));
4270          break;
4271        case 3:
4272          emit_insn (gen_cr6_test_for_lt_reverse (target));
4273          break;
4274        default:
4275          error ("argument 1 of __builtin_altivec_predicate is out of range");
4276          break;
4277        }
4278
4279   return target;
4280 }
4281
4282 static rtx
4283 altivec_expand_stv_builtin (icode, arglist)
4284      enum insn_code icode;
4285      tree arglist;
4286 {
4287   tree arg0 = TREE_VALUE (arglist);
4288   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4289   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
4290   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4291   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
4292   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
4293   rtx pat;
4294   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
4295   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
4296   enum machine_mode mode2 = insn_data[icode].operand[2].mode;
4297
4298   /* Invalid arguments.  Bail before doing anything stoopid!  */
4299   if (arg0 == error_mark_node
4300       || arg1 == error_mark_node
4301       || arg2 == error_mark_node)
4302     return const0_rtx;
4303
4304   if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
4305     op0 = copy_to_mode_reg (mode2, op0);
4306   if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
4307     op1 = copy_to_mode_reg (mode0, op1);
4308   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
4309     op2 = copy_to_mode_reg (mode1, op2);
4310
4311   pat = GEN_FCN (icode) (op1, op2, op0);
4312   if (pat)
4313     emit_insn (pat);
4314   return NULL_RTX;
4315 }
4316
4317 static rtx
4318 rs6000_expand_ternop_builtin (icode, arglist, target)
4319      enum insn_code icode;
4320      tree arglist;
4321      rtx target;
4322 {
4323   rtx pat;
4324   tree arg0 = TREE_VALUE (arglist);
4325   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4326   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
4327   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4328   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
4329   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
4330   enum machine_mode tmode = insn_data[icode].operand[0].mode;
4331   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
4332   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
4333   enum machine_mode mode2 = insn_data[icode].operand[3].mode;
4334
4335   /* If we got invalid arguments bail out before generating bad rtl.  */
4336   if (arg0 == error_mark_node
4337       || arg1 == error_mark_node
4338       || arg2 == error_mark_node)
4339     return const0_rtx;
4340
4341   switch (icode)
4342     {
4343       /* Only allow 4-bit unsigned literals.  */
4344     case CODE_FOR_altivec_vsldoi_4sf:
4345     case CODE_FOR_altivec_vsldoi_4si:
4346     case CODE_FOR_altivec_vsldoi_8hi:
4347     case CODE_FOR_altivec_vsldoi_16qi:
4348       if (TREE_CODE (arg2) != INTEGER_CST
4349           || TREE_INT_CST_LOW (arg2) & ~0xf)
4350         {
4351           error ("argument 3 must be a 4-bit unsigned literal");
4352           return const0_rtx;
4353         }
4354       break;
4355     default:
4356       break;
4357     }
4358
4359   if (target == 0
4360       || GET_MODE (target) != tmode
4361       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4362     target = gen_reg_rtx (tmode);
4363
4364   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
4365     op0 = copy_to_mode_reg (mode0, op0);
4366   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
4367     op1 = copy_to_mode_reg (mode1, op1);
4368   if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
4369     op2 = copy_to_mode_reg (mode2, op2);
4370
4371   pat = GEN_FCN (icode) (target, op0, op1, op2);
4372   if (! pat)
4373     return 0;
4374   emit_insn (pat);
4375
4376   return target;
4377 }
4378
4379 /* Expand the lvx builtins.  */
4380 static rtx
4381 altivec_expand_ld_builtin (exp, target, expandedp)
4382      tree exp;
4383      rtx target;
4384      bool *expandedp;
4385 {
4386   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4387   tree arglist = TREE_OPERAND (exp, 1);
4388   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
4389   tree arg0;
4390   enum machine_mode tmode, mode0;
4391   rtx pat, op0;
4392   enum insn_code icode;
4393
4394   switch (fcode)
4395     {
4396     case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
4397       icode = CODE_FOR_altivec_lvx_16qi;
4398       break;
4399     case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
4400       icode = CODE_FOR_altivec_lvx_8hi;
4401       break;
4402     case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
4403       icode = CODE_FOR_altivec_lvx_4si;
4404       break;
4405     case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
4406       icode = CODE_FOR_altivec_lvx_4sf;
4407       break;
4408     default:
4409       *expandedp = false;
4410       return NULL_RTX;
4411     }
4412
4413   *expandedp = true;
4414
4415   arg0 = TREE_VALUE (arglist);
4416   op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4417   tmode = insn_data[icode].operand[0].mode;
4418   mode0 = insn_data[icode].operand[1].mode;
4419
4420   if (target == 0
4421       || GET_MODE (target) != tmode
4422       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4423     target = gen_reg_rtx (tmode);
4424
4425   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
4426     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
4427
4428   pat = GEN_FCN (icode) (target, op0);
4429   if (! pat)
4430     return 0;
4431   emit_insn (pat);
4432   return target;
4433 }
4434
4435 /* Expand the stvx builtins.  */
4436 static rtx
4437 altivec_expand_st_builtin (exp, target, expandedp)
4438      tree exp;
4439      rtx target ATTRIBUTE_UNUSED;
4440      bool *expandedp;
4441 {
4442   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4443   tree arglist = TREE_OPERAND (exp, 1);
4444   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
4445   tree arg0, arg1;
4446   enum machine_mode mode0, mode1;
4447   rtx pat, op0, op1;
4448   enum insn_code icode;
4449
4450   switch (fcode)
4451     {
4452     case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
4453       icode = CODE_FOR_altivec_stvx_16qi;
4454       break;
4455     case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
4456       icode = CODE_FOR_altivec_stvx_8hi;
4457       break;
4458     case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
4459       icode = CODE_FOR_altivec_stvx_4si;
4460       break;
4461     case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
4462       icode = CODE_FOR_altivec_stvx_4sf;
4463       break;
4464     default:
4465       *expandedp = false;
4466       return NULL_RTX;
4467     }
4468
4469   arg0 = TREE_VALUE (arglist);
4470   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4471   op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4472   op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
4473   mode0 = insn_data[icode].operand[0].mode;
4474   mode1 = insn_data[icode].operand[1].mode;
4475
4476   if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
4477     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
4478   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
4479     op1 = copy_to_mode_reg (mode1, op1);
4480
4481   pat = GEN_FCN (icode) (op0, op1);
4482   if (pat)
4483     emit_insn (pat);
4484
4485   *expandedp = true;
4486   return NULL_RTX;
4487 }
4488
4489 /* Expand the dst builtins.  */
4490 static rtx
4491 altivec_expand_dst_builtin (exp, target, expandedp)
4492      tree exp;
4493      rtx target ATTRIBUTE_UNUSED;
4494      bool *expandedp;
4495 {
4496   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4497   tree arglist = TREE_OPERAND (exp, 1);
4498   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
4499   tree arg0, arg1, arg2;
4500   enum machine_mode mode0, mode1, mode2;
4501   rtx pat, op0, op1, op2;
4502   struct builtin_description *d;
4503   size_t i;
4504
4505   *expandedp = false;
4506
4507   /* Handle DST variants.  */
4508   d = (struct builtin_description *) bdesc_dst;
4509   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
4510     if (d->code == fcode)
4511       {
4512         arg0 = TREE_VALUE (arglist);
4513         arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4514         arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
4515         op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4516         op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
4517         op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
4518         mode0 = insn_data[d->icode].operand[0].mode;
4519         mode1 = insn_data[d->icode].operand[1].mode;
4520         mode2 = insn_data[d->icode].operand[2].mode;
4521
4522         /* Invalid arguments, bail out before generating bad rtl.  */
4523         if (arg0 == error_mark_node
4524             || arg1 == error_mark_node
4525             || arg2 == error_mark_node)
4526           return const0_rtx;
4527
4528         if (TREE_CODE (arg2) != INTEGER_CST
4529             || TREE_INT_CST_LOW (arg2) & ~0x3)
4530           {
4531             error ("argument to `%s' must be a 2-bit unsigned literal", d->name);
4532             return const0_rtx;
4533           }
4534
4535         if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
4536           op0 = copy_to_mode_reg (mode0, op0);
4537         if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
4538           op1 = copy_to_mode_reg (mode1, op1);
4539
4540         pat = GEN_FCN (d->icode) (op0, op1, op2);
4541         if (pat != 0)
4542           emit_insn (pat);
4543
4544         *expandedp = true;
4545         return NULL_RTX;
4546       }
4547
4548   return NULL_RTX;
4549 }
4550
4551 /* Expand the builtin in EXP and store the result in TARGET.  Store
4552    true in *EXPANDEDP if we found a builtin to expand.  */
4553 static rtx
4554 altivec_expand_builtin (exp, target, expandedp)
4555      tree exp;
4556      rtx target;
4557      bool *expandedp;
4558 {
4559   struct builtin_description *d;
4560   struct builtin_description_predicates *dp;
4561   size_t i;
4562   enum insn_code icode;
4563   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4564   tree arglist = TREE_OPERAND (exp, 1);
4565   tree arg0;
4566   rtx op0, pat;
4567   enum machine_mode tmode, mode0;
4568   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
4569
4570   target = altivec_expand_ld_builtin (exp, target, expandedp);
4571   if (*expandedp)
4572     return target;
4573
4574   target = altivec_expand_st_builtin (exp, target, expandedp);
4575   if (*expandedp)
4576     return target;
4577
4578   target = altivec_expand_dst_builtin (exp, target, expandedp);
4579   if (*expandedp)
4580     return target;
4581
4582   *expandedp = true;
4583
4584   switch (fcode)
4585     {
4586     case ALTIVEC_BUILTIN_STVX:
4587       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx, arglist);
4588     case ALTIVEC_BUILTIN_STVEBX:
4589       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, arglist);
4590     case ALTIVEC_BUILTIN_STVEHX:
4591       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, arglist);
4592     case ALTIVEC_BUILTIN_STVEWX:
4593       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, arglist);
4594     case ALTIVEC_BUILTIN_STVXL:
4595       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl, arglist);
4596
4597     case ALTIVEC_BUILTIN_MFVSCR:
4598       icode = CODE_FOR_altivec_mfvscr;
4599       tmode = insn_data[icode].operand[0].mode;
4600
4601       if (target == 0
4602           || GET_MODE (target) != tmode
4603           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4604         target = gen_reg_rtx (tmode);
4605       
4606       pat = GEN_FCN (icode) (target);
4607       if (! pat)
4608         return 0;
4609       emit_insn (pat);
4610       return target;
4611
4612     case ALTIVEC_BUILTIN_MTVSCR:
4613       icode = CODE_FOR_altivec_mtvscr;
4614       arg0 = TREE_VALUE (arglist);
4615       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4616       mode0 = insn_data[icode].operand[0].mode;
4617
4618       /* If we got invalid arguments bail out before generating bad rtl.  */
4619       if (arg0 == error_mark_node)
4620         return const0_rtx;
4621
4622       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
4623         op0 = copy_to_mode_reg (mode0, op0);
4624
4625       pat = GEN_FCN (icode) (op0);
4626       if (pat)
4627         emit_insn (pat);
4628       return NULL_RTX;
4629
4630     case ALTIVEC_BUILTIN_DSSALL:
4631       emit_insn (gen_altivec_dssall ());
4632       return NULL_RTX;
4633
4634     case ALTIVEC_BUILTIN_DSS:
4635       icode = CODE_FOR_altivec_dss;
4636       arg0 = TREE_VALUE (arglist);
4637       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4638       mode0 = insn_data[icode].operand[0].mode;
4639
4640       /* If we got invalid arguments bail out before generating bad rtl.  */
4641       if (arg0 == error_mark_node)
4642         return const0_rtx;
4643
4644       if (TREE_CODE (arg0) != INTEGER_CST
4645           || TREE_INT_CST_LOW (arg0) & ~0x3)
4646         {
4647           error ("argument to dss must be a 2-bit unsigned literal");
4648           return const0_rtx;
4649         }
4650
4651       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
4652         op0 = copy_to_mode_reg (mode0, op0);
4653
4654       emit_insn (gen_altivec_dss (op0));
4655       return NULL_RTX;
4656     }
4657
4658   /* Expand abs* operations.  */
4659   d = (struct builtin_description *) bdesc_abs;
4660   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
4661     if (d->code == fcode)
4662       return altivec_expand_abs_builtin (d->icode, arglist, target);
4663
4664   /* Expand the AltiVec predicates.  */
4665   dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
4666   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
4667     if (dp->code == fcode)
4668       return altivec_expand_predicate_builtin (dp->icode, dp->opcode, arglist, target);
4669
4670   /* LV* are funky.  We initialized them differently.  */
4671   switch (fcode)
4672     {
4673     case ALTIVEC_BUILTIN_LVSL:
4674       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvsl,
4675                                            arglist, target);
4676     case ALTIVEC_BUILTIN_LVSR:
4677       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvsr,
4678                                           arglist, target);
4679     case ALTIVEC_BUILTIN_LVEBX:
4680       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvebx,
4681                                           arglist, target);
4682     case ALTIVEC_BUILTIN_LVEHX:
4683       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvehx,
4684                                           arglist, target);
4685     case ALTIVEC_BUILTIN_LVEWX:
4686       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvewx,
4687                                           arglist, target);
4688     case ALTIVEC_BUILTIN_LVXL:
4689       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvxl,
4690                                           arglist, target);
4691     case ALTIVEC_BUILTIN_LVX:
4692       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvx,
4693                                           arglist, target);
4694     default:
4695       break;
4696       /* Fall through.  */
4697     }
4698
4699   *expandedp = false;
4700   return NULL_RTX;
4701 }
4702
4703 /* Binops that need to be initialized manually, but can be expanded
4704    automagically by rs6000_expand_binop_builtin.  */
4705 static struct builtin_description bdesc_2arg_spe[] =
4706 {
4707   { 0, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
4708   { 0, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
4709   { 0, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
4710   { 0, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
4711   { 0, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
4712   { 0, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
4713   { 0, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
4714   { 0, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
4715   { 0, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
4716   { 0, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
4717   { 0, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
4718   { 0, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
4719   { 0, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
4720   { 0, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
4721   { 0, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
4722   { 0, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
4723   { 0, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
4724   { 0, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
4725   { 0, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
4726   { 0, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
4727   { 0, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
4728   { 0, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
4729 };
4730
4731 /* Expand the builtin in EXP and store the result in TARGET.  Store
4732    true in *EXPANDEDP if we found a builtin to expand.
4733
4734    This expands the SPE builtins that are not simple unary and binary
4735    operations.  */
4736 static rtx
4737 spe_expand_builtin (exp, target, expandedp)
4738      tree exp;
4739      rtx target;
4740      bool *expandedp;
4741 {
4742   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4743   tree arglist = TREE_OPERAND (exp, 1);
4744   tree arg1, arg0;
4745   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
4746   enum insn_code icode;
4747   enum machine_mode tmode, mode0;
4748   rtx pat, op0;
4749   struct builtin_description *d;
4750   size_t i;
4751
4752   *expandedp = true;
4753
4754   /* Syntax check for a 5-bit unsigned immediate.  */
4755   switch (fcode)
4756     {
4757     case SPE_BUILTIN_EVSTDD:
4758     case SPE_BUILTIN_EVSTDH:
4759     case SPE_BUILTIN_EVSTDW:
4760     case SPE_BUILTIN_EVSTWHE:
4761     case SPE_BUILTIN_EVSTWHO:
4762     case SPE_BUILTIN_EVSTWWE:
4763     case SPE_BUILTIN_EVSTWWO:
4764       arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
4765       if (TREE_CODE (arg1) != INTEGER_CST
4766           || TREE_INT_CST_LOW (arg1) & ~0x1f)
4767         {
4768           error ("argument 2 must be a 5-bit unsigned literal");
4769           return const0_rtx;
4770         }
4771       break;
4772     default:
4773       break;
4774     }
4775
4776   d = (struct builtin_description *) bdesc_2arg_spe;
4777   for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
4778     if (d->code == fcode)
4779       return rs6000_expand_binop_builtin (d->icode, arglist, target);
4780
4781   d = (struct builtin_description *) bdesc_spe_predicates;
4782   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
4783     if (d->code == fcode)
4784       return spe_expand_predicate_builtin (d->icode, arglist, target);
4785
4786   d = (struct builtin_description *) bdesc_spe_evsel;
4787   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
4788     if (d->code == fcode)
4789       return spe_expand_evsel_builtin (d->icode, arglist, target);
4790
4791   switch (fcode)
4792     {
4793     case SPE_BUILTIN_EVSTDDX:
4794       return altivec_expand_stv_builtin (CODE_FOR_spe_evstddx, arglist);
4795     case SPE_BUILTIN_EVSTDHX:
4796       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdhx, arglist);
4797     case SPE_BUILTIN_EVSTDWX:
4798       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdwx, arglist);
4799     case SPE_BUILTIN_EVSTWHEX:
4800       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhex, arglist);
4801     case SPE_BUILTIN_EVSTWHOX:
4802       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhox, arglist);
4803     case SPE_BUILTIN_EVSTWWEX:
4804       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwex, arglist);
4805     case SPE_BUILTIN_EVSTWWOX:
4806       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwox, arglist);
4807     case SPE_BUILTIN_EVSTDD:
4808       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdd, arglist);
4809     case SPE_BUILTIN_EVSTDH:
4810       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdh, arglist);
4811     case SPE_BUILTIN_EVSTDW:
4812       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdw, arglist);
4813     case SPE_BUILTIN_EVSTWHE:
4814       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhe, arglist);
4815     case SPE_BUILTIN_EVSTWHO:
4816       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwho, arglist);
4817     case SPE_BUILTIN_EVSTWWE:
4818       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwe, arglist);
4819     case SPE_BUILTIN_EVSTWWO:
4820       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwo, arglist);
4821     case SPE_BUILTIN_MFSPEFSCR:
4822       icode = CODE_FOR_spe_mfspefscr;
4823       tmode = insn_data[icode].operand[0].mode;
4824
4825       if (target == 0
4826           || GET_MODE (target) != tmode
4827           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4828         target = gen_reg_rtx (tmode);
4829       
4830       pat = GEN_FCN (icode) (target);
4831       if (! pat)
4832         return 0;
4833       emit_insn (pat);
4834       return target;
4835     case SPE_BUILTIN_MTSPEFSCR:
4836       icode = CODE_FOR_spe_mtspefscr;
4837       arg0 = TREE_VALUE (arglist);
4838       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4839       mode0 = insn_data[icode].operand[0].mode;
4840
4841       if (arg0 == error_mark_node)
4842         return const0_rtx;
4843
4844       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
4845         op0 = copy_to_mode_reg (mode0, op0);
4846
4847       pat = GEN_FCN (icode) (op0);
4848       if (pat)
4849         emit_insn (pat);
4850       return NULL_RTX;
4851     default:
4852       break;
4853     }
4854
4855   *expandedp = false;
4856   return NULL_RTX;
4857 }
4858
4859 static rtx
4860 spe_expand_predicate_builtin (icode, arglist, target)
4861      enum insn_code icode;
4862      tree arglist;
4863      rtx target;
4864 {
4865   rtx pat, scratch, tmp;
4866   tree form = TREE_VALUE (arglist);
4867   tree arg0 = TREE_VALUE (TREE_CHAIN (arglist));
4868   tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
4869   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4870   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
4871   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
4872   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
4873   int form_int;
4874   enum rtx_code code;
4875
4876   if (TREE_CODE (form) != INTEGER_CST)
4877     {
4878       error ("argument 1 of __builtin_spe_predicate must be a constant");
4879       return const0_rtx;
4880     }
4881   else
4882     form_int = TREE_INT_CST_LOW (form);
4883
4884   if (mode0 != mode1)
4885     abort ();
4886
4887   if (arg0 == error_mark_node || arg1 == error_mark_node)
4888     return const0_rtx;
4889
4890   if (target == 0
4891       || GET_MODE (target) != SImode
4892       || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
4893     target = gen_reg_rtx (SImode);
4894
4895   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
4896     op0 = copy_to_mode_reg (mode0, op0);
4897   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
4898     op1 = copy_to_mode_reg (mode1, op1);
4899
4900   scratch = gen_reg_rtx (CCmode);
4901
4902   pat = GEN_FCN (icode) (scratch, op0, op1);
4903   if (! pat)
4904     return const0_rtx;
4905   emit_insn (pat);
4906
4907   /* There are 4 variants for each predicate: _any_, _all_, _upper_,
4908      _lower_.  We use one compare, but look in different bits of the
4909      CR for each variant.
4910
4911      There are 2 elements in each SPE simd type (upper/lower).  The CR
4912      bits are set as follows:
4913
4914      BIT0  | BIT 1  | BIT 2   | BIT 3
4915      U     |   L    | (U | L) | (U & L)
4916
4917      So, for an "all" relationship, BIT 3 would be set.
4918      For an "any" relationship, BIT 2 would be set.  Etc.
4919
4920      Following traditional nomenclature, these bits map to:
4921
4922      BIT0  | BIT 1  | BIT 2   | BIT 3
4923      LT    | GT     | EQ      | OV
4924
4925      Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
4926   */
4927
4928   switch (form_int)
4929     {
4930       /* All variant.  OV bit.  */
4931     case 0:
4932       /* We need to get to the OV bit, which is the ORDERED bit.  We
4933          could generate (ordered:SI (reg:CC xx) (const_int 0)), but
4934          that's ugly and will trigger a validate_condition_mode abort.
4935          So let's just use another pattern.  */
4936       emit_insn (gen_move_from_CR_ov_bit (target, scratch));
4937       return target;
4938       /* Any variant.  EQ bit.  */
4939     case 1:
4940       code = EQ;
4941       break;
4942       /* Upper variant.  LT bit.  */
4943     case 2:
4944       code = LT;
4945       break;
4946       /* Lower variant.  GT bit.  */
4947     case 3:
4948       code = GT;
4949       break;
4950     default:
4951       error ("argument 1 of __builtin_spe_predicate is out of range");
4952       return const0_rtx;
4953     }
4954
4955   tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
4956   emit_move_insn (target, tmp);
4957
4958   return target;
4959 }
4960
4961 /* The evsel builtins look like this:
4962
4963      e = __builtin_spe_evsel_OP (a, b, c, d);
4964
4965    and work like this:
4966
4967      e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
4968      e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
4969 */
4970
4971 static rtx
4972 spe_expand_evsel_builtin (icode, arglist, target)
4973      enum insn_code icode;
4974      tree arglist;
4975      rtx target;
4976 {
4977   rtx pat, scratch;
4978   tree arg0 = TREE_VALUE (arglist);
4979   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4980   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
4981   tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
4982   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4983   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
4984   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
4985   rtx op3 = expand_expr (arg3, NULL_RTX, VOIDmode, 0);
4986   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
4987   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
4988
4989   if (mode0 != mode1)
4990     abort ();
4991
4992   if (arg0 == error_mark_node || arg1 == error_mark_node
4993       || arg2 == error_mark_node || arg3 == error_mark_node)
4994     return const0_rtx;
4995
4996   if (target == 0
4997       || GET_MODE (target) != mode0
4998       || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
4999     target = gen_reg_rtx (mode0);
5000
5001   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5002     op0 = copy_to_mode_reg (mode0, op0);
5003   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
5004     op1 = copy_to_mode_reg (mode0, op1);
5005   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
5006     op2 = copy_to_mode_reg (mode0, op2);
5007   if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
5008     op3 = copy_to_mode_reg (mode0, op3);
5009
5010   /* Generate the compare.  */
5011   scratch = gen_reg_rtx (CCmode);
5012   pat = GEN_FCN (icode) (scratch, op0, op1);
5013   if (! pat)
5014     return const0_rtx;
5015   emit_insn (pat);
5016
5017   if (mode0 == V2SImode)
5018     emit_insn (gen_spe_evsel (target, op2, op3, scratch));
5019   else
5020     emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
5021
5022   return target;
5023 }
5024
5025 /* Expand an expression EXP that calls a built-in function,
5026    with result going to TARGET if that's convenient
5027    (and in mode MODE if that's convenient).
5028    SUBTARGET may be used as the target for computing one of EXP's operands.
5029    IGNORE is nonzero if the value is to be ignored.  */
5030
5031 static rtx
5032 rs6000_expand_builtin (exp, target, subtarget, mode, ignore)
5033      tree exp;
5034      rtx target;
5035      rtx subtarget ATTRIBUTE_UNUSED;
5036      enum machine_mode mode ATTRIBUTE_UNUSED;
5037      int ignore ATTRIBUTE_UNUSED;
5038 {
5039   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5040   tree arglist = TREE_OPERAND (exp, 1);
5041   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5042   struct builtin_description *d;
5043   size_t i;
5044   rtx ret;
5045   bool success;
5046   
5047   if (TARGET_ALTIVEC)
5048     {
5049       ret = altivec_expand_builtin (exp, target, &success);
5050
5051       if (success)
5052         return ret;
5053     }
5054   if (TARGET_SPE)
5055     {
5056       ret = spe_expand_builtin (exp, target, &success);
5057
5058       if (success)
5059         return ret;
5060     }
5061
5062   /* Handle simple unary operations.  */
5063   d = (struct builtin_description *) bdesc_1arg;
5064   for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
5065     if (d->code == fcode)
5066       return rs6000_expand_unop_builtin (d->icode, arglist, target);
5067
5068   /* Handle simple binary operations.  */
5069   d = (struct builtin_description *) bdesc_2arg;
5070   for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
5071     if (d->code == fcode)
5072       return rs6000_expand_binop_builtin (d->icode, arglist, target);
5073
5074   /* Handle simple ternary operations.  */
5075   d = (struct builtin_description *) bdesc_3arg;
5076   for (i = 0; i < ARRAY_SIZE  (bdesc_3arg); i++, d++)
5077     if (d->code == fcode)
5078       return rs6000_expand_ternop_builtin (d->icode, arglist, target);
5079
5080   abort ();
5081   return NULL_RTX;
5082 }
5083
5084 static void
5085 rs6000_init_builtins ()
5086 {
5087   if (TARGET_SPE)
5088     spe_init_builtins ();
5089   if (TARGET_ALTIVEC)
5090     altivec_init_builtins ();
5091   rs6000_common_init_builtins ();
5092 }
5093
5094 /* Search through a set of builtins and enable the mask bits.
5095    DESC is an array of builtins.
5096    SIZE is the totaly number of builtins.
5097    START is the builtin enum at which to start.
5098    END is the builtin enum at which to end.  */
5099 static void
5100 enable_mask_for_builtins (desc, size, start, end)
5101      struct builtin_description *desc;
5102      int size;
5103      enum rs6000_builtins start, end;
5104 {
5105   int i;
5106
5107   for (i = 0; i < size; ++i)
5108     if (desc[i].code == start)
5109       break;
5110
5111   if (i == size)
5112     return;
5113
5114   for (; i < size; ++i)
5115     {
5116       /* Flip all the bits on.  */
5117       desc[i].mask = target_flags;
5118       if (desc[i].code == end)
5119         break;
5120     }
5121 }
5122
5123 static void
5124 spe_init_builtins (void)
5125 {
5126   tree endlink = void_list_node;
5127   tree puint_type_node = build_pointer_type (unsigned_type_node);
5128   tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
5129   tree pv2si_type_node = build_pointer_type (V2SI_type_node);
5130   struct builtin_description *d;
5131   size_t i;
5132
5133   tree v2si_ftype_4_v2si
5134     = build_function_type
5135     (V2SI_type_node,
5136      tree_cons (NULL_TREE, V2SI_type_node,
5137                 tree_cons (NULL_TREE, V2SI_type_node,
5138                            tree_cons (NULL_TREE, V2SI_type_node,
5139                                       tree_cons (NULL_TREE, V2SI_type_node,
5140                                                  endlink)))));
5141
5142   tree v2sf_ftype_4_v2sf
5143     = build_function_type
5144     (V2SF_type_node,
5145      tree_cons (NULL_TREE, V2SF_type_node,
5146                 tree_cons (NULL_TREE, V2SF_type_node,
5147                            tree_cons (NULL_TREE, V2SF_type_node,
5148                                       tree_cons (NULL_TREE, V2SF_type_node,
5149                                                  endlink)))));
5150
5151   tree int_ftype_int_v2si_v2si
5152     = build_function_type
5153     (integer_type_node,
5154      tree_cons (NULL_TREE, integer_type_node,
5155                 tree_cons (NULL_TREE, V2SI_type_node,
5156                            tree_cons (NULL_TREE, V2SI_type_node,
5157                                       endlink))));
5158
5159   tree int_ftype_int_v2sf_v2sf
5160     = build_function_type
5161     (integer_type_node,
5162      tree_cons (NULL_TREE, integer_type_node,
5163                 tree_cons (NULL_TREE, V2SF_type_node,
5164                            tree_cons (NULL_TREE, V2SF_type_node,
5165                                       endlink))));
5166
5167   tree void_ftype_v2si_puint_int
5168     = build_function_type (void_type_node,
5169                            tree_cons (NULL_TREE, V2SI_type_node,
5170                                       tree_cons (NULL_TREE, puint_type_node,
5171                                                  tree_cons (NULL_TREE,
5172                                                             integer_type_node,
5173                                                             endlink))));
5174
5175   tree void_ftype_v2si_puint_char
5176     = build_function_type (void_type_node,
5177                            tree_cons (NULL_TREE, V2SI_type_node,
5178                                       tree_cons (NULL_TREE, puint_type_node,
5179                                                  tree_cons (NULL_TREE,
5180                                                             char_type_node,
5181                                                             endlink))));
5182
5183   tree void_ftype_v2si_pv2si_int
5184     = build_function_type (void_type_node,
5185                            tree_cons (NULL_TREE, V2SI_type_node,
5186                                       tree_cons (NULL_TREE, pv2si_type_node,
5187                                                  tree_cons (NULL_TREE,
5188                                                             integer_type_node,
5189                                                             endlink))));
5190
5191   tree void_ftype_v2si_pv2si_char
5192     = build_function_type (void_type_node,
5193                            tree_cons (NULL_TREE, V2SI_type_node,
5194                                       tree_cons (NULL_TREE, pv2si_type_node,
5195                                                  tree_cons (NULL_TREE,
5196                                                             char_type_node,
5197                                                             endlink))));
5198
5199   tree void_ftype_int
5200     = build_function_type (void_type_node,
5201                            tree_cons (NULL_TREE, integer_type_node, endlink));
5202
5203   tree int_ftype_void
5204     = build_function_type (integer_type_node,
5205                            tree_cons (NULL_TREE, void_type_node, endlink));
5206
5207   tree v2si_ftype_pv2si_int
5208     = build_function_type (V2SI_type_node,
5209                            tree_cons (NULL_TREE, pv2si_type_node,
5210                                       tree_cons (NULL_TREE, integer_type_node,
5211                                                  endlink)));
5212
5213   tree v2si_ftype_puint_int
5214     = build_function_type (V2SI_type_node,
5215                            tree_cons (NULL_TREE, puint_type_node,
5216                                       tree_cons (NULL_TREE, integer_type_node,
5217                                                  endlink)));
5218
5219   tree v2si_ftype_pushort_int
5220     = build_function_type (V2SI_type_node,
5221                            tree_cons (NULL_TREE, pushort_type_node,
5222                                       tree_cons (NULL_TREE, integer_type_node,
5223                                                  endlink)));
5224
5225   /* The initialization of the simple binary and unary builtins is
5226      done in rs6000_common_init_builtins, but we have to enable the
5227      mask bits here manually because we have run out of `target_flags'
5228      bits.  We really need to redesign this mask business.  */
5229
5230   enable_mask_for_builtins ((struct builtin_description *) bdesc_2arg,
5231                             ARRAY_SIZE (bdesc_2arg),
5232                             SPE_BUILTIN_EVADDW,
5233                             SPE_BUILTIN_EVXOR);
5234   enable_mask_for_builtins ((struct builtin_description *) bdesc_1arg,
5235                             ARRAY_SIZE (bdesc_1arg),
5236                             SPE_BUILTIN_EVABS,
5237                             SPE_BUILTIN_EVSUBFUSIAAW);
5238   enable_mask_for_builtins ((struct builtin_description *) bdesc_spe_predicates,
5239                             ARRAY_SIZE (bdesc_spe_predicates),
5240                             SPE_BUILTIN_EVCMPEQ,
5241                             SPE_BUILTIN_EVFSTSTLT);
5242   enable_mask_for_builtins ((struct builtin_description *) bdesc_spe_evsel,
5243                             ARRAY_SIZE (bdesc_spe_evsel),
5244                             SPE_BUILTIN_EVSEL_CMPGTS,
5245                             SPE_BUILTIN_EVSEL_FSTSTEQ);
5246
5247   /* Initialize irregular SPE builtins.  */
5248   
5249   def_builtin (target_flags, "__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
5250   def_builtin (target_flags, "__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
5251   def_builtin (target_flags, "__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
5252   def_builtin (target_flags, "__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
5253   def_builtin (target_flags, "__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
5254   def_builtin (target_flags, "__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
5255   def_builtin (target_flags, "__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
5256   def_builtin (target_flags, "__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
5257   def_builtin (target_flags, "__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
5258   def_builtin (target_flags, "__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
5259   def_builtin (target_flags, "__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
5260   def_builtin (target_flags, "__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
5261   def_builtin (target_flags, "__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
5262   def_builtin (target_flags, "__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
5263   def_builtin (target_flags, "__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
5264   def_builtin (target_flags, "__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
5265
5266   /* Loads.  */
5267   def_builtin (target_flags, "__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
5268   def_builtin (target_flags, "__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
5269   def_builtin (target_flags, "__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
5270   def_builtin (target_flags, "__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
5271   def_builtin (target_flags, "__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
5272   def_builtin (target_flags, "__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
5273   def_builtin (target_flags, "__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
5274   def_builtin (target_flags, "__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
5275   def_builtin (target_flags, "__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
5276   def_builtin (target_flags, "__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
5277   def_builtin (target_flags, "__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
5278   def_builtin (target_flags, "__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
5279   def_builtin (target_flags, "__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
5280   def_builtin (target_flags, "__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
5281   def_builtin (target_flags, "__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
5282   def_builtin (target_flags, "__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
5283   def_builtin (target_flags, "__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
5284   def_builtin (target_flags, "__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
5285   def_builtin (target_flags, "__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
5286   def_builtin (target_flags, "__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
5287   def_builtin (target_flags, "__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
5288   def_builtin (target_flags, "__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
5289
5290   /* Predicates.  */
5291   d = (struct builtin_description *) bdesc_spe_predicates;
5292   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
5293     {
5294       tree type;
5295
5296       switch (insn_data[d->icode].operand[1].mode)
5297         {
5298         case V2SImode:
5299           type = int_ftype_int_v2si_v2si;
5300           break;
5301         case V2SFmode:
5302           type = int_ftype_int_v2sf_v2sf;
5303           break;
5304         default:
5305           abort ();
5306         }
5307
5308       def_builtin (d->mask, d->name, type, d->code);
5309     }
5310
5311   /* Evsel predicates.  */
5312   d = (struct builtin_description *) bdesc_spe_evsel;
5313   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
5314     {
5315       tree type;
5316
5317       switch (insn_data[d->icode].operand[1].mode)
5318         {
5319         case V2SImode:
5320           type = v2si_ftype_4_v2si;
5321           break;
5322         case V2SFmode:
5323           type = v2sf_ftype_4_v2sf;
5324           break;
5325         default:
5326           abort ();
5327         }
5328
5329       def_builtin (d->mask, d->name, type, d->code);
5330     }
5331 }
5332
5333 static void
5334 altivec_init_builtins (void)
5335 {
5336   struct builtin_description *d;
5337   struct builtin_description_predicates *dp;
5338   size_t i;
5339   tree pfloat_type_node = build_pointer_type (float_type_node);
5340   tree pint_type_node = build_pointer_type (integer_type_node);
5341   tree pshort_type_node = build_pointer_type (short_integer_type_node);
5342   tree pchar_type_node = build_pointer_type (char_type_node);
5343
5344   tree pvoid_type_node = build_pointer_type (void_type_node);
5345
5346   tree int_ftype_int_v4si_v4si
5347     = build_function_type_list (integer_type_node,
5348                                 integer_type_node, V4SI_type_node,
5349                                 V4SI_type_node, NULL_TREE);
5350   tree v4sf_ftype_pfloat
5351     = build_function_type_list (V4SF_type_node, pfloat_type_node, NULL_TREE);
5352   tree void_ftype_pfloat_v4sf
5353     = build_function_type_list (void_type_node,
5354                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
5355   tree v4si_ftype_pint
5356     = build_function_type_list (V4SI_type_node, pint_type_node, NULL_TREE);  tree void_ftype_pint_v4si
5357     = build_function_type_list (void_type_node,
5358                                 pint_type_node, V4SI_type_node, NULL_TREE);
5359   tree v8hi_ftype_pshort
5360     = build_function_type_list (V8HI_type_node, pshort_type_node, NULL_TREE);
5361   tree void_ftype_pshort_v8hi
5362     = build_function_type_list (void_type_node,
5363                                 pshort_type_node, V8HI_type_node, NULL_TREE);
5364   tree v16qi_ftype_pchar
5365     = build_function_type_list (V16QI_type_node, pchar_type_node, NULL_TREE);
5366   tree void_ftype_pchar_v16qi
5367     = build_function_type_list (void_type_node,
5368                                 pchar_type_node, V16QI_type_node, NULL_TREE);
5369   tree void_ftype_v4si
5370     = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
5371   tree v8hi_ftype_void
5372     = build_function_type (V8HI_type_node, void_list_node);
5373   tree void_ftype_void
5374     = build_function_type (void_type_node, void_list_node);
5375   tree void_ftype_qi
5376     = build_function_type_list (void_type_node, char_type_node, NULL_TREE);
5377   tree v16qi_ftype_int_pvoid
5378     = build_function_type_list (V16QI_type_node,
5379                                 integer_type_node, pvoid_type_node, NULL_TREE);
5380   tree v8hi_ftype_int_pvoid
5381     = build_function_type_list (V8HI_type_node,
5382                                 integer_type_node, pvoid_type_node, NULL_TREE);
5383   tree v4si_ftype_int_pvoid
5384     = build_function_type_list (V4SI_type_node,
5385                                 integer_type_node, pvoid_type_node, NULL_TREE);
5386   tree void_ftype_v4si_int_pvoid
5387     = build_function_type_list (void_type_node,
5388                                 V4SI_type_node, integer_type_node,
5389                                 pvoid_type_node, NULL_TREE);
5390   tree void_ftype_v16qi_int_pvoid
5391     = build_function_type_list (void_type_node,
5392                                 V16QI_type_node, integer_type_node,
5393                                 pvoid_type_node, NULL_TREE);
5394   tree void_ftype_v8hi_int_pvoid
5395     = build_function_type_list (void_type_node,
5396                                 V8HI_type_node, integer_type_node,
5397                                 pvoid_type_node, NULL_TREE);
5398   tree int_ftype_int_v8hi_v8hi
5399     = build_function_type_list (integer_type_node,
5400                                 integer_type_node, V8HI_type_node,
5401                                 V8HI_type_node, NULL_TREE);
5402   tree int_ftype_int_v16qi_v16qi
5403     = build_function_type_list (integer_type_node,
5404                                 integer_type_node, V16QI_type_node,
5405                                 V16QI_type_node, NULL_TREE);
5406   tree int_ftype_int_v4sf_v4sf
5407     = build_function_type_list (integer_type_node,
5408                                 integer_type_node, V4SF_type_node,
5409                                 V4SF_type_node, NULL_TREE);
5410   tree v4si_ftype_v4si
5411     = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
5412   tree v8hi_ftype_v8hi
5413     = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
5414   tree v16qi_ftype_v16qi
5415     = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
5416   tree v4sf_ftype_v4sf
5417     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
5418   tree void_ftype_pvoid_int_char
5419     = build_function_type_list (void_type_node,
5420                                 pvoid_type_node, integer_type_node,
5421                                 char_type_node, NULL_TREE);
5422
5423   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4sf", v4sf_ftype_pfloat, ALTIVEC_BUILTIN_LD_INTERNAL_4sf);
5424   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4sf", void_ftype_pfloat_v4sf, ALTIVEC_BUILTIN_ST_INTERNAL_4sf);
5425   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4si", v4si_ftype_pint, ALTIVEC_BUILTIN_LD_INTERNAL_4si);
5426   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4si", void_ftype_pint_v4si, ALTIVEC_BUILTIN_ST_INTERNAL_4si);
5427   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_8hi", v8hi_ftype_pshort, ALTIVEC_BUILTIN_LD_INTERNAL_8hi);
5428   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_8hi", void_ftype_pshort_v8hi, ALTIVEC_BUILTIN_ST_INTERNAL_8hi);
5429   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_16qi", v16qi_ftype_pchar, ALTIVEC_BUILTIN_LD_INTERNAL_16qi);
5430   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_16qi", void_ftype_pchar_v16qi, ALTIVEC_BUILTIN_ST_INTERNAL_16qi);
5431   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
5432   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
5433   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
5434   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dss", void_ftype_qi, ALTIVEC_BUILTIN_DSS);
5435   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsl", v16qi_ftype_int_pvoid, ALTIVEC_BUILTIN_LVSL);
5436   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsr", v16qi_ftype_int_pvoid, ALTIVEC_BUILTIN_LVSR);
5437   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvebx", v16qi_ftype_int_pvoid, ALTIVEC_BUILTIN_LVEBX);
5438   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvehx", v8hi_ftype_int_pvoid, ALTIVEC_BUILTIN_LVEHX);
5439   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvewx", v4si_ftype_int_pvoid, ALTIVEC_BUILTIN_LVEWX);
5440   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvxl", v4si_ftype_int_pvoid, ALTIVEC_BUILTIN_LVXL);
5441   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvx", v4si_ftype_int_pvoid, ALTIVEC_BUILTIN_LVX);
5442   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvx", void_ftype_v4si_int_pvoid, ALTIVEC_BUILTIN_STVX);
5443   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvewx", void_ftype_v4si_int_pvoid, ALTIVEC_BUILTIN_STVEWX);
5444   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvxl", void_ftype_v4si_int_pvoid, ALTIVEC_BUILTIN_STVXL);
5445   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvebx", void_ftype_v16qi_int_pvoid, ALTIVEC_BUILTIN_STVEBX);
5446   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvehx", void_ftype_v8hi_int_pvoid, ALTIVEC_BUILTIN_STVEHX);
5447
5448   /* Add the DST variants.  */
5449   d = (struct builtin_description *) bdesc_dst;
5450   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
5451     def_builtin (d->mask, d->name, void_ftype_pvoid_int_char, d->code);
5452
5453   /* Initialize the predicates.  */
5454   dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
5455   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
5456     {
5457       enum machine_mode mode1;
5458       tree type;
5459
5460       mode1 = insn_data[dp->icode].operand[1].mode;
5461
5462       switch (mode1)
5463         {
5464         case V4SImode:
5465           type = int_ftype_int_v4si_v4si;
5466           break;
5467         case V8HImode:
5468           type = int_ftype_int_v8hi_v8hi;
5469           break;
5470         case V16QImode:
5471           type = int_ftype_int_v16qi_v16qi;
5472           break;
5473         case V4SFmode:
5474           type = int_ftype_int_v4sf_v4sf;
5475           break;
5476         default:
5477           abort ();
5478         }
5479       
5480       def_builtin (dp->mask, dp->name, type, dp->code);
5481     }
5482
5483   /* Initialize the abs* operators.  */
5484   d = (struct builtin_description *) bdesc_abs;
5485   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
5486     {
5487       enum machine_mode mode0;
5488       tree type;
5489
5490       mode0 = insn_data[d->icode].operand[0].mode;
5491
5492       switch (mode0)
5493         {
5494         case V4SImode:
5495           type = v4si_ftype_v4si;
5496           break;
5497         case V8HImode:
5498           type = v8hi_ftype_v8hi;
5499           break;
5500         case V16QImode:
5501           type = v16qi_ftype_v16qi;
5502           break;
5503         case V4SFmode:
5504           type = v4sf_ftype_v4sf;
5505           break;
5506         default:
5507           abort ();
5508         }
5509       
5510       def_builtin (d->mask, d->name, type, d->code);
5511     }
5512 }
5513
5514 static void
5515 rs6000_common_init_builtins (void)
5516 {
5517   struct builtin_description *d;
5518   size_t i;
5519
5520   tree v4sf_ftype_v4sf_v4sf_v16qi
5521     = build_function_type_list (V4SF_type_node,
5522                                 V4SF_type_node, V4SF_type_node,
5523                                 V16QI_type_node, NULL_TREE);
5524   tree v4si_ftype_v4si_v4si_v16qi
5525     = build_function_type_list (V4SI_type_node,
5526                                 V4SI_type_node, V4SI_type_node,
5527                                 V16QI_type_node, NULL_TREE);
5528   tree v8hi_ftype_v8hi_v8hi_v16qi
5529     = build_function_type_list (V8HI_type_node,
5530                                 V8HI_type_node, V8HI_type_node,
5531                                 V16QI_type_node, NULL_TREE);
5532   tree v16qi_ftype_v16qi_v16qi_v16qi
5533     = build_function_type_list (V16QI_type_node,
5534                                 V16QI_type_node, V16QI_type_node,
5535                                 V16QI_type_node, NULL_TREE);
5536   tree v4si_ftype_char
5537     = build_function_type_list (V4SI_type_node, char_type_node, NULL_TREE);
5538   tree v8hi_ftype_char
5539     = build_function_type_list (V8HI_type_node, char_type_node, NULL_TREE);
5540   tree v16qi_ftype_char
5541     = build_function_type_list (V16QI_type_node, char_type_node, NULL_TREE);
5542   tree v8hi_ftype_v16qi
5543     = build_function_type_list (V8HI_type_node, V16QI_type_node, NULL_TREE);
5544   tree v4sf_ftype_v4sf
5545     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
5546
5547   tree v2si_ftype_v2si_v2si
5548     = build_function_type_list (V2SI_type_node,
5549                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
5550
5551   tree v2sf_ftype_v2sf_v2sf
5552     = build_function_type_list (V2SF_type_node,
5553                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
5554
5555   tree v2si_ftype_int_int
5556     = build_function_type_list (V2SI_type_node,
5557                                 integer_type_node, integer_type_node,
5558                                 NULL_TREE);
5559
5560   tree v2si_ftype_v2si
5561     = build_function_type_list (V2SI_type_node, V2SI_type_node, NULL_TREE);
5562
5563   tree v2sf_ftype_v2sf
5564     = build_function_type_list (V2SF_type_node,
5565                                 V2SF_type_node, NULL_TREE);
5566   
5567   tree v2sf_ftype_v2si
5568     = build_function_type_list (V2SF_type_node,
5569                                 V2SI_type_node, NULL_TREE);
5570
5571   tree v2si_ftype_v2sf
5572     = build_function_type_list (V2SI_type_node,
5573                                 V2SF_type_node, NULL_TREE);
5574
5575   tree v2si_ftype_v2si_char
5576     = build_function_type_list (V2SI_type_node,
5577                                 V2SI_type_node, char_type_node, NULL_TREE);
5578
5579   tree v2si_ftype_int_char
5580     = build_function_type_list (V2SI_type_node,
5581                                 integer_type_node, char_type_node, NULL_TREE);
5582
5583   tree v2si_ftype_char
5584     = build_function_type_list (V2SI_type_node, char_type_node, NULL_TREE);
5585
5586   tree int_ftype_int_int
5587     = build_function_type_list (integer_type_node,
5588                                 integer_type_node, integer_type_node,
5589                                 NULL_TREE);
5590
5591   tree v4si_ftype_v4si_v4si
5592     = build_function_type_list (V4SI_type_node,
5593                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
5594   tree v4sf_ftype_v4si_char
5595     = build_function_type_list (V4SF_type_node,
5596                                 V4SI_type_node, char_type_node, NULL_TREE);
5597   tree v4si_ftype_v4sf_char
5598     = build_function_type_list (V4SI_type_node,
5599                                 V4SF_type_node, char_type_node, NULL_TREE);
5600   tree v4si_ftype_v4si_char
5601     = build_function_type_list (V4SI_type_node,
5602                                 V4SI_type_node, char_type_node, NULL_TREE);
5603   tree v8hi_ftype_v8hi_char
5604     = build_function_type_list (V8HI_type_node,
5605                                 V8HI_type_node, char_type_node, NULL_TREE);
5606   tree v16qi_ftype_v16qi_char
5607     = build_function_type_list (V16QI_type_node,
5608                                 V16QI_type_node, char_type_node, NULL_TREE);
5609   tree v16qi_ftype_v16qi_v16qi_char
5610     = build_function_type_list (V16QI_type_node,
5611                                 V16QI_type_node, V16QI_type_node,
5612                                 char_type_node, NULL_TREE);
5613   tree v8hi_ftype_v8hi_v8hi_char
5614     = build_function_type_list (V8HI_type_node,
5615                                 V8HI_type_node, V8HI_type_node,
5616                                 char_type_node, NULL_TREE);
5617   tree v4si_ftype_v4si_v4si_char
5618     = build_function_type_list (V4SI_type_node,
5619                                 V4SI_type_node, V4SI_type_node,
5620                                 char_type_node, NULL_TREE);
5621   tree v4sf_ftype_v4sf_v4sf_char
5622     = build_function_type_list (V4SF_type_node,
5623                                 V4SF_type_node, V4SF_type_node,
5624                                 char_type_node, NULL_TREE);
5625   tree v4sf_ftype_v4sf_v4sf
5626     = build_function_type_list (V4SF_type_node,
5627                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
5628   tree v4sf_ftype_v4sf_v4sf_v4si
5629     = build_function_type_list (V4SF_type_node,
5630                                 V4SF_type_node, V4SF_type_node,
5631                                 V4SI_type_node, NULL_TREE);
5632   tree v4sf_ftype_v4sf_v4sf_v4sf
5633     = build_function_type_list (V4SF_type_node,
5634                                 V4SF_type_node, V4SF_type_node,
5635                                 V4SF_type_node, NULL_TREE);
5636   tree v4si_ftype_v4si_v4si_v4si 
5637     = build_function_type_list (V4SI_type_node,
5638                                 V4SI_type_node, V4SI_type_node,
5639                                 V4SI_type_node, NULL_TREE);
5640   tree v8hi_ftype_v8hi_v8hi
5641     = build_function_type_list (V8HI_type_node,
5642                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
5643   tree v8hi_ftype_v8hi_v8hi_v8hi
5644     = build_function_type_list (V8HI_type_node,
5645                                 V8HI_type_node, V8HI_type_node,
5646                                 V8HI_type_node, NULL_TREE);
5647  tree v4si_ftype_v8hi_v8hi_v4si
5648     = build_function_type_list (V4SI_type_node,
5649                                 V8HI_type_node, V8HI_type_node,
5650                                 V4SI_type_node, NULL_TREE);
5651  tree v4si_ftype_v16qi_v16qi_v4si
5652     = build_function_type_list (V4SI_type_node,
5653                                 V16QI_type_node, V16QI_type_node,
5654                                 V4SI_type_node, NULL_TREE);
5655   tree v16qi_ftype_v16qi_v16qi
5656     = build_function_type_list (V16QI_type_node,
5657                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
5658   tree v4si_ftype_v4sf_v4sf
5659     = build_function_type_list (V4SI_type_node,
5660                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
5661   tree v8hi_ftype_v16qi_v16qi
5662     = build_function_type_list (V8HI_type_node,
5663                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
5664   tree v4si_ftype_v8hi_v8hi
5665     = build_function_type_list (V4SI_type_node,
5666                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
5667   tree v8hi_ftype_v4si_v4si
5668     = build_function_type_list (V8HI_type_node,
5669                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
5670   tree v16qi_ftype_v8hi_v8hi
5671     = build_function_type_list (V16QI_type_node,
5672                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
5673   tree v4si_ftype_v16qi_v4si
5674     = build_function_type_list (V4SI_type_node,
5675                                 V16QI_type_node, V4SI_type_node, NULL_TREE);
5676   tree v4si_ftype_v16qi_v16qi
5677     = build_function_type_list (V4SI_type_node,
5678                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
5679   tree v4si_ftype_v8hi_v4si
5680     = build_function_type_list (V4SI_type_node,
5681                                 V8HI_type_node, V4SI_type_node, NULL_TREE);
5682   tree v4si_ftype_v8hi
5683     = build_function_type_list (V4SI_type_node, V8HI_type_node, NULL_TREE);
5684   tree int_ftype_v4si_v4si
5685     = build_function_type_list (integer_type_node,
5686                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
5687   tree int_ftype_v4sf_v4sf
5688     = build_function_type_list (integer_type_node,
5689                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
5690   tree int_ftype_v16qi_v16qi
5691     = build_function_type_list (integer_type_node,
5692                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
5693   tree int_ftype_v8hi_v8hi
5694     = build_function_type_list (integer_type_node,
5695                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
5696
5697   /* Add the simple ternary operators.  */
5698   d = (struct builtin_description *) bdesc_3arg;
5699   for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
5700     {
5701       
5702       enum machine_mode mode0, mode1, mode2, mode3;
5703       tree type;
5704
5705       if (d->name == 0)
5706         continue;
5707       
5708       mode0 = insn_data[d->icode].operand[0].mode;
5709       mode1 = insn_data[d->icode].operand[1].mode;
5710       mode2 = insn_data[d->icode].operand[2].mode;
5711       mode3 = insn_data[d->icode].operand[3].mode;
5712       
5713       /* When all four are of the same mode.  */
5714       if (mode0 == mode1 && mode1 == mode2 && mode2 == mode3)
5715         {
5716           switch (mode0)
5717             {
5718             case V4SImode:
5719               type = v4si_ftype_v4si_v4si_v4si;
5720               break;
5721             case V4SFmode:
5722               type = v4sf_ftype_v4sf_v4sf_v4sf;
5723               break;
5724             case V8HImode:
5725               type = v8hi_ftype_v8hi_v8hi_v8hi;
5726               break;          
5727             case V16QImode:
5728               type = v16qi_ftype_v16qi_v16qi_v16qi;
5729               break;          
5730             default:
5731               abort();        
5732             }
5733         }
5734       else if (mode0 == mode1 && mode1 == mode2 && mode3 == V16QImode)
5735         {
5736           switch (mode0)
5737             {
5738             case V4SImode:
5739               type = v4si_ftype_v4si_v4si_v16qi;
5740               break;
5741             case V4SFmode:
5742               type = v4sf_ftype_v4sf_v4sf_v16qi;
5743               break;
5744             case V8HImode:
5745               type = v8hi_ftype_v8hi_v8hi_v16qi;
5746               break;          
5747             case V16QImode:
5748               type = v16qi_ftype_v16qi_v16qi_v16qi;
5749               break;          
5750             default:
5751               abort();        
5752             }
5753         }
5754       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode 
5755                && mode3 == V4SImode)
5756         type = v4si_ftype_v16qi_v16qi_v4si;
5757       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode 
5758                && mode3 == V4SImode)
5759         type = v4si_ftype_v8hi_v8hi_v4si;
5760       else if (mode0 == V4SFmode && mode1 == V4SFmode && mode2 == V4SFmode 
5761                && mode3 == V4SImode)
5762         type = v4sf_ftype_v4sf_v4sf_v4si;
5763
5764       /* vchar, vchar, vchar, 4 bit literal.  */
5765       else if (mode0 == V16QImode && mode1 == mode0 && mode2 == mode0
5766                && mode3 == QImode)
5767         type = v16qi_ftype_v16qi_v16qi_char;
5768
5769       /* vshort, vshort, vshort, 4 bit literal.  */
5770       else if (mode0 == V8HImode && mode1 == mode0 && mode2 == mode0
5771                && mode3 == QImode)
5772         type = v8hi_ftype_v8hi_v8hi_char;
5773
5774       /* vint, vint, vint, 4 bit literal.  */
5775       else if (mode0 == V4SImode && mode1 == mode0 && mode2 == mode0
5776                && mode3 == QImode)
5777         type = v4si_ftype_v4si_v4si_char;
5778
5779       /* vfloat, vfloat, vfloat, 4 bit literal.  */
5780       else if (mode0 == V4SFmode && mode1 == mode0 && mode2 == mode0
5781                && mode3 == QImode)
5782         type = v4sf_ftype_v4sf_v4sf_char;
5783
5784       else
5785         abort ();
5786
5787       def_builtin (d->mask, d->name, type, d->code);
5788     }
5789
5790   /* Add the simple binary operators.  */
5791   d = (struct builtin_description *) bdesc_2arg;
5792   for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
5793     {
5794       enum machine_mode mode0, mode1, mode2;
5795       tree type;
5796
5797       if (d->name == 0)
5798         continue;
5799       
5800       mode0 = insn_data[d->icode].operand[0].mode;
5801       mode1 = insn_data[d->icode].operand[1].mode;
5802       mode2 = insn_data[d->icode].operand[2].mode;
5803
5804       /* When all three operands are of the same mode.  */
5805       if (mode0 == mode1 && mode1 == mode2)
5806         {
5807           switch (mode0)
5808             {
5809             case V4SFmode:
5810               type = v4sf_ftype_v4sf_v4sf;
5811               break;
5812             case V4SImode:
5813               type = v4si_ftype_v4si_v4si;
5814               break;
5815             case V16QImode:
5816               type = v16qi_ftype_v16qi_v16qi;
5817               break;
5818             case V8HImode:
5819               type = v8hi_ftype_v8hi_v8hi;
5820               break;
5821             case V2SImode:
5822               type = v2si_ftype_v2si_v2si;
5823               break;
5824             case V2SFmode:
5825               type = v2sf_ftype_v2sf_v2sf;
5826               break;
5827             case SImode:
5828               type = int_ftype_int_int;
5829               break;
5830             default:
5831               abort ();
5832             }
5833         }
5834
5835       /* A few other combos we really don't want to do manually.  */
5836
5837       /* vint, vfloat, vfloat.  */
5838       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == V4SFmode)
5839         type = v4si_ftype_v4sf_v4sf;
5840
5841       /* vshort, vchar, vchar.  */
5842       else if (mode0 == V8HImode && mode1 == V16QImode && mode2 == V16QImode)
5843         type = v8hi_ftype_v16qi_v16qi;
5844
5845       /* vint, vshort, vshort.  */
5846       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode)
5847         type = v4si_ftype_v8hi_v8hi;
5848
5849       /* vshort, vint, vint.  */
5850       else if (mode0 == V8HImode && mode1 == V4SImode && mode2 == V4SImode)
5851         type = v8hi_ftype_v4si_v4si;
5852
5853       /* vchar, vshort, vshort.  */
5854       else if (mode0 == V16QImode && mode1 == V8HImode && mode2 == V8HImode)
5855         type = v16qi_ftype_v8hi_v8hi;
5856
5857       /* vint, vchar, vint.  */
5858       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V4SImode)
5859         type = v4si_ftype_v16qi_v4si;
5860
5861       /* vint, vchar, vchar.  */
5862       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode)
5863         type = v4si_ftype_v16qi_v16qi;
5864
5865       /* vint, vshort, vint.  */
5866       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V4SImode)
5867         type = v4si_ftype_v8hi_v4si;
5868       
5869       /* vint, vint, 5 bit literal.  */
5870       else if (mode0 == V4SImode && mode1 == V4SImode && mode2 == QImode)
5871         type = v4si_ftype_v4si_char;
5872       
5873       /* vshort, vshort, 5 bit literal.  */
5874       else if (mode0 == V8HImode && mode1 == V8HImode && mode2 == QImode)
5875         type = v8hi_ftype_v8hi_char;
5876       
5877       /* vchar, vchar, 5 bit literal.  */
5878       else if (mode0 == V16QImode && mode1 == V16QImode && mode2 == QImode)
5879         type = v16qi_ftype_v16qi_char;
5880
5881       /* vfloat, vint, 5 bit literal.  */
5882       else if (mode0 == V4SFmode && mode1 == V4SImode && mode2 == QImode)
5883         type = v4sf_ftype_v4si_char;
5884       
5885       /* vint, vfloat, 5 bit literal.  */
5886       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == QImode)
5887         type = v4si_ftype_v4sf_char;
5888
5889       else if (mode0 == V2SImode && mode1 == SImode && mode2 == SImode)
5890         type = v2si_ftype_int_int;
5891
5892       else if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
5893         type = v2si_ftype_v2si_char;
5894
5895       else if (mode0 == V2SImode && mode1 == SImode && mode2 == QImode)
5896         type = v2si_ftype_int_char;
5897
5898       /* int, x, x.  */
5899       else if (mode0 == SImode)
5900         {
5901           switch (mode1)
5902             {
5903             case V4SImode:
5904               type = int_ftype_v4si_v4si;
5905               break;
5906             case V4SFmode:
5907               type = int_ftype_v4sf_v4sf;
5908               break;
5909             case V16QImode:
5910               type = int_ftype_v16qi_v16qi;
5911               break;
5912             case V8HImode:
5913               type = int_ftype_v8hi_v8hi;
5914               break;
5915             default:
5916               abort ();
5917             }
5918         }
5919
5920       else
5921         abort ();
5922
5923       def_builtin (d->mask, d->name, type, d->code);
5924     }
5925
5926   /* Add the simple unary operators.  */
5927   d = (struct builtin_description *) bdesc_1arg;
5928   for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
5929     {
5930       enum machine_mode mode0, mode1;
5931       tree type;
5932
5933       if (d->name == 0)
5934         continue;
5935       
5936       mode0 = insn_data[d->icode].operand[0].mode;
5937       mode1 = insn_data[d->icode].operand[1].mode;
5938
5939       if (mode0 == V4SImode && mode1 == QImode)
5940         type = v4si_ftype_char;
5941       else if (mode0 == V8HImode && mode1 == QImode)
5942         type = v8hi_ftype_char;
5943       else if (mode0 == V16QImode && mode1 == QImode)
5944         type = v16qi_ftype_char;
5945       else if (mode0 == V4SFmode && mode1 == V4SFmode)
5946         type = v4sf_ftype_v4sf;
5947       else if (mode0 == V8HImode && mode1 == V16QImode)
5948         type = v8hi_ftype_v16qi;
5949       else if (mode0 == V4SImode && mode1 == V8HImode)
5950         type = v4si_ftype_v8hi;
5951       else if (mode0 == V2SImode && mode1 == V2SImode)
5952         type = v2si_ftype_v2si;
5953       else if (mode0 == V2SFmode && mode1 == V2SFmode)
5954         type = v2sf_ftype_v2sf;
5955       else if (mode0 == V2SFmode && mode1 == V2SImode)
5956         type = v2sf_ftype_v2si;
5957       else if (mode0 == V2SImode && mode1 == V2SFmode)
5958         type = v2si_ftype_v2sf;
5959       else if (mode0 == V2SImode && mode1 == QImode)
5960         type = v2si_ftype_char;
5961       else
5962         abort ();
5963
5964       def_builtin (d->mask, d->name, type, d->code);
5965     }
5966 }
5967
5968 /* Generate a memory reference for expand_block_move, copying volatile,
5969    and other bits from an original memory reference.  */
5970
5971 static rtx
5972 expand_block_move_mem (mode, addr, orig_mem)
5973      enum machine_mode mode;
5974      rtx addr;
5975      rtx orig_mem;
5976 {
5977   rtx mem = gen_rtx_MEM (mode, addr);
5978
5979   MEM_COPY_ATTRIBUTES (mem, orig_mem);
5980   return mem;
5981 }
5982
5983 /* Expand a block move operation, and return 1 if successful.  Return 0
5984    if we should let the compiler generate normal code.
5985
5986    operands[0] is the destination
5987    operands[1] is the source
5988    operands[2] is the length
5989    operands[3] is the alignment */
5990
5991 #define MAX_MOVE_REG 4
5992
5993 int
5994 expand_block_move (operands)
5995      rtx operands[];
5996 {
5997   rtx orig_dest = operands[0];
5998   rtx orig_src  = operands[1];
5999   rtx bytes_rtx = operands[2];
6000   rtx align_rtx = operands[3];
6001   int constp    = (GET_CODE (bytes_rtx) == CONST_INT);
6002   int align;
6003   int bytes;
6004   int offset;
6005   int num_reg;
6006   int i;
6007   rtx src_reg;
6008   rtx dest_reg;
6009   rtx src_addr;
6010   rtx dest_addr;
6011   rtx tmp_reg;
6012   rtx stores[MAX_MOVE_REG];
6013   int move_bytes;
6014
6015   /* If this is not a fixed size move, just call memcpy */
6016   if (! constp)
6017     return 0;
6018
6019   /* If this is not a fixed size alignment, abort */
6020   if (GET_CODE (align_rtx) != CONST_INT)
6021     abort ();
6022   align = INTVAL (align_rtx);
6023
6024   /* Anything to move? */
6025   bytes = INTVAL (bytes_rtx);
6026   if (bytes <= 0)
6027     return 1;
6028
6029   /* store_one_arg depends on expand_block_move to handle at least the size of
6030      reg_parm_stack_space.  */  
6031   if (bytes > (TARGET_POWERPC64 ? 64 : 32))
6032     return 0;
6033
6034   /* Move the address into scratch registers.  */
6035   dest_reg = copy_addr_to_reg (XEXP (orig_dest, 0));
6036   src_reg  = copy_addr_to_reg (XEXP (orig_src,  0));
6037
6038   if (TARGET_STRING)    /* string instructions are available */
6039     {
6040       for ( ; bytes > 0; bytes -= move_bytes)
6041         {
6042           if (bytes > 24                /* move up to 32 bytes at a time */
6043               && ! fixed_regs[5]
6044               && ! fixed_regs[6]
6045               && ! fixed_regs[7]
6046               && ! fixed_regs[8]
6047               && ! fixed_regs[9]
6048               && ! fixed_regs[10]
6049               && ! fixed_regs[11]
6050               && ! fixed_regs[12])
6051             {
6052               move_bytes = (bytes > 32) ? 32 : bytes;
6053               emit_insn (gen_movstrsi_8reg (expand_block_move_mem (BLKmode,
6054                                                                    dest_reg,
6055                                                                    orig_dest),
6056                                             expand_block_move_mem (BLKmode,
6057                                                                    src_reg,
6058                                                                    orig_src),
6059                                             GEN_INT ((move_bytes == 32)
6060                                                      ? 0 : move_bytes),
6061                                             align_rtx));
6062             }
6063           else if (bytes > 16   /* move up to 24 bytes at a time */
6064                    && ! fixed_regs[5]
6065                    && ! fixed_regs[6]
6066                    && ! fixed_regs[7]
6067                    && ! fixed_regs[8]
6068                    && ! fixed_regs[9]
6069                    && ! fixed_regs[10])
6070             {
6071               move_bytes = (bytes > 24) ? 24 : bytes;
6072               emit_insn (gen_movstrsi_6reg (expand_block_move_mem (BLKmode,
6073                                                                    dest_reg,
6074                                                                    orig_dest),
6075                                             expand_block_move_mem (BLKmode,
6076                                                                    src_reg,
6077                                                                    orig_src),
6078                                             GEN_INT (move_bytes),
6079                                             align_rtx));
6080             }
6081           else if (bytes > 8    /* move up to 16 bytes at a time */
6082                    && ! fixed_regs[5]
6083                    && ! fixed_regs[6]
6084                    && ! fixed_regs[7]
6085                    && ! fixed_regs[8])
6086             {
6087               move_bytes = (bytes > 16) ? 16 : bytes;
6088               emit_insn (gen_movstrsi_4reg (expand_block_move_mem (BLKmode,
6089                                                                    dest_reg,
6090                                                                    orig_dest),
6091                                             expand_block_move_mem (BLKmode,
6092                                                                    src_reg,
6093                                                                    orig_src),
6094                                             GEN_INT (move_bytes),
6095                                             align_rtx));
6096             }
6097           else if (bytes >= 8 && TARGET_POWERPC64
6098                    /* 64-bit loads and stores require word-aligned
6099                       displacements.  */
6100                    && (align >= 8 || (! STRICT_ALIGNMENT && align >= 4)))
6101             {
6102               move_bytes = 8;
6103               tmp_reg = gen_reg_rtx (DImode);
6104               emit_move_insn (tmp_reg,
6105                               expand_block_move_mem (DImode,
6106                                                      src_reg, orig_src));
6107               emit_move_insn (expand_block_move_mem (DImode,
6108                                                      dest_reg, orig_dest),
6109                               tmp_reg);
6110             }
6111           else if (bytes > 4 && !TARGET_POWERPC64)
6112             {                   /* move up to 8 bytes at a time */
6113               move_bytes = (bytes > 8) ? 8 : bytes;
6114               emit_insn (gen_movstrsi_2reg (expand_block_move_mem (BLKmode,
6115                                                                    dest_reg,
6116                                                                    orig_dest),
6117                                             expand_block_move_mem (BLKmode,
6118                                                                    src_reg,
6119                                                                    orig_src),
6120                                             GEN_INT (move_bytes),
6121                                             align_rtx));
6122             }
6123           else if (bytes >= 4 && (align >= 4 || ! STRICT_ALIGNMENT))
6124             {                   /* move 4 bytes */
6125               move_bytes = 4;
6126               tmp_reg = gen_reg_rtx (SImode);
6127               emit_move_insn (tmp_reg,
6128                               expand_block_move_mem (SImode,
6129                                                      src_reg, orig_src));
6130               emit_move_insn (expand_block_move_mem (SImode,
6131                                                      dest_reg, orig_dest),
6132                               tmp_reg);
6133             }
6134           else if (bytes == 2 && (align >= 2 || ! STRICT_ALIGNMENT))
6135             {                   /* move 2 bytes */
6136               move_bytes = 2;
6137               tmp_reg = gen_reg_rtx (HImode);
6138               emit_move_insn (tmp_reg,
6139                               expand_block_move_mem (HImode,
6140                                                      src_reg, orig_src));
6141               emit_move_insn (expand_block_move_mem (HImode,
6142                                                      dest_reg, orig_dest),
6143                               tmp_reg);
6144             }
6145           else if (bytes == 1)  /* move 1 byte */
6146             {
6147               move_bytes = 1;
6148               tmp_reg = gen_reg_rtx (QImode);
6149               emit_move_insn (tmp_reg,
6150                               expand_block_move_mem (QImode,
6151                                                      src_reg, orig_src));
6152               emit_move_insn (expand_block_move_mem (QImode,
6153                                                      dest_reg, orig_dest),
6154                               tmp_reg);
6155             }
6156           else
6157             {                   /* move up to 4 bytes at a time */
6158               move_bytes = (bytes > 4) ? 4 : bytes;
6159               emit_insn (gen_movstrsi_1reg (expand_block_move_mem (BLKmode,
6160                                                                    dest_reg,
6161                                                                    orig_dest),
6162                                             expand_block_move_mem (BLKmode,
6163                                                                    src_reg,
6164                                                                    orig_src),
6165                                             GEN_INT (move_bytes),
6166                                             align_rtx));
6167             }
6168
6169           if (bytes > move_bytes)
6170             {
6171               if (! TARGET_POWERPC64)
6172                 {
6173                   emit_insn (gen_addsi3 (src_reg, src_reg,
6174                                          GEN_INT (move_bytes)));
6175                   emit_insn (gen_addsi3 (dest_reg, dest_reg,
6176                                          GEN_INT (move_bytes)));
6177                 }
6178               else
6179                 {
6180                   emit_insn (gen_adddi3 (src_reg, src_reg,
6181                                          GEN_INT (move_bytes)));
6182                   emit_insn (gen_adddi3 (dest_reg, dest_reg,
6183                                          GEN_INT (move_bytes)));
6184                 }
6185             }
6186         }
6187     }
6188
6189   else                  /* string instructions not available */
6190     {
6191       num_reg = offset = 0;
6192       for ( ; bytes > 0; (bytes -= move_bytes), (offset += move_bytes))
6193         {
6194           /* Calculate the correct offset for src/dest */
6195           if (offset == 0)
6196             {
6197               src_addr  = src_reg;
6198               dest_addr = dest_reg;
6199             }
6200           else
6201             {
6202               src_addr = plus_constant (src_reg, offset);
6203               dest_addr = plus_constant (dest_reg, offset);
6204             }
6205
6206           /* Generate the appropriate load and store, saving the stores
6207              for later.  */
6208           if (bytes >= 8 && TARGET_POWERPC64
6209               /* 64-bit loads and stores require word-aligned
6210                  displacements.  */
6211               && (align >= 8 || (! STRICT_ALIGNMENT && align >= 4)))
6212             {
6213               move_bytes = 8;
6214               tmp_reg = gen_reg_rtx (DImode);
6215               emit_insn (gen_movdi (tmp_reg,
6216                                     expand_block_move_mem (DImode,
6217                                                            src_addr,
6218                                                            orig_src)));
6219               stores[num_reg++] = gen_movdi (expand_block_move_mem (DImode,
6220                                                                     dest_addr,
6221                                                                     orig_dest),
6222                                              tmp_reg);
6223             }
6224           else if (bytes >= 4 && (align >= 4 || ! STRICT_ALIGNMENT))
6225             {
6226               move_bytes = 4;
6227               tmp_reg = gen_reg_rtx (SImode);
6228               emit_insn (gen_movsi (tmp_reg,
6229                                     expand_block_move_mem (SImode,
6230                                                            src_addr,
6231                                                            orig_src)));
6232               stores[num_reg++] = gen_movsi (expand_block_move_mem (SImode,
6233                                                                     dest_addr,
6234                                                                     orig_dest),
6235                                              tmp_reg);
6236             }
6237           else if (bytes >= 2 && (align >= 2 || ! STRICT_ALIGNMENT))
6238             {
6239               move_bytes = 2;
6240               tmp_reg = gen_reg_rtx (HImode);
6241               emit_insn (gen_movhi (tmp_reg,
6242                                     expand_block_move_mem (HImode,
6243                                                            src_addr,
6244                                                            orig_src)));
6245               stores[num_reg++] = gen_movhi (expand_block_move_mem (HImode,
6246                                                                     dest_addr,
6247                                                                     orig_dest),
6248                                              tmp_reg);
6249             }
6250           else
6251             {
6252               move_bytes = 1;
6253               tmp_reg = gen_reg_rtx (QImode);
6254               emit_insn (gen_movqi (tmp_reg,
6255                                     expand_block_move_mem (QImode,
6256                                                            src_addr,
6257                                                            orig_src)));
6258               stores[num_reg++] = gen_movqi (expand_block_move_mem (QImode,
6259                                                                     dest_addr,
6260                                                                     orig_dest),
6261                                              tmp_reg);
6262             }
6263
6264           if (num_reg >= MAX_MOVE_REG)
6265             {
6266               for (i = 0; i < num_reg; i++)
6267                 emit_insn (stores[i]);
6268               num_reg = 0;
6269             }
6270         }
6271
6272       for (i = 0; i < num_reg; i++)
6273         emit_insn (stores[i]);
6274     }
6275
6276   return 1;
6277 }
6278
6279 \f
6280 /* Return 1 if OP is a load multiple operation.  It is known to be a
6281    PARALLEL and the first section will be tested.  */
6282
6283 int
6284 load_multiple_operation (op, mode)
6285      rtx op;
6286      enum machine_mode mode ATTRIBUTE_UNUSED;
6287 {
6288   int count = XVECLEN (op, 0);
6289   unsigned int dest_regno;
6290   rtx src_addr;
6291   int i;
6292
6293   /* Perform a quick check so we don't blow up below.  */
6294   if (count <= 1
6295       || GET_CODE (XVECEXP (op, 0, 0)) != SET
6296       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
6297       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
6298     return 0;
6299
6300   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
6301   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
6302
6303   for (i = 1; i < count; i++)
6304     {
6305       rtx elt = XVECEXP (op, 0, i);
6306
6307       if (GET_CODE (elt) != SET
6308           || GET_CODE (SET_DEST (elt)) != REG
6309           || GET_MODE (SET_DEST (elt)) != SImode
6310           || REGNO (SET_DEST (elt)) != dest_regno + i
6311           || GET_CODE (SET_SRC (elt)) != MEM
6312           || GET_MODE (SET_SRC (elt)) != SImode
6313           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
6314           || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
6315           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
6316           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != i * 4)
6317         return 0;
6318     }
6319
6320   return 1;
6321 }
6322
6323 /* Similar, but tests for store multiple.  Here, the second vector element
6324    is a CLOBBER.  It will be tested later.  */
6325
6326 int
6327 store_multiple_operation (op, mode)
6328      rtx op;
6329      enum machine_mode mode ATTRIBUTE_UNUSED;
6330 {
6331   int count = XVECLEN (op, 0) - 1;
6332   unsigned int src_regno;
6333   rtx dest_addr;
6334   int i;
6335
6336   /* Perform a quick check so we don't blow up below.  */
6337   if (count <= 1
6338       || GET_CODE (XVECEXP (op, 0, 0)) != SET
6339       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
6340       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
6341     return 0;
6342
6343   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
6344   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
6345
6346   for (i = 1; i < count; i++)
6347     {
6348       rtx elt = XVECEXP (op, 0, i + 1);
6349
6350       if (GET_CODE (elt) != SET
6351           || GET_CODE (SET_SRC (elt)) != REG
6352           || GET_MODE (SET_SRC (elt)) != SImode
6353           || REGNO (SET_SRC (elt)) != src_regno + i
6354           || GET_CODE (SET_DEST (elt)) != MEM
6355           || GET_MODE (SET_DEST (elt)) != SImode
6356           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
6357           || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
6358           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
6359           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != i * 4)
6360         return 0;
6361     }
6362
6363   return 1;
6364 }
6365
6366 /* Return 1 for a parallel vrsave operation.  */
6367
6368 int
6369 vrsave_operation (op, mode)
6370      rtx op;
6371      enum machine_mode mode ATTRIBUTE_UNUSED;
6372 {
6373   int count = XVECLEN (op, 0);
6374   unsigned int dest_regno, src_regno;
6375   int i;
6376
6377   if (count <= 1
6378       || GET_CODE (XVECEXP (op, 0, 0)) != SET
6379       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
6380       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC_VOLATILE)
6381     return 0;
6382
6383   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
6384   src_regno  = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
6385
6386   if (dest_regno != VRSAVE_REGNO
6387       && src_regno != VRSAVE_REGNO)
6388     return 0;
6389
6390   for (i = 1; i < count; i++)
6391     {
6392       rtx elt = XVECEXP (op, 0, i);
6393
6394       if (GET_CODE (elt) != CLOBBER
6395           && GET_CODE (elt) != SET)
6396         return 0;
6397     }
6398
6399   return 1;
6400 }
6401
6402 /* Return 1 for an PARALLEL suitable for mtcrf.  */
6403
6404 int
6405 mtcrf_operation (op, mode)
6406      rtx op;
6407      enum machine_mode mode ATTRIBUTE_UNUSED;
6408 {
6409   int count = XVECLEN (op, 0);
6410   int i;
6411   rtx src_reg;
6412
6413   /* Perform a quick check so we don't blow up below.  */
6414   if (count < 1
6415       || GET_CODE (XVECEXP (op, 0, 0)) != SET
6416       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC
6417       || XVECLEN (SET_SRC (XVECEXP (op, 0, 0)), 0) != 2)
6418     return 0;
6419   src_reg = XVECEXP (SET_SRC (XVECEXP (op, 0, 0)), 0, 0);
6420   
6421   if (GET_CODE (src_reg) != REG
6422       || GET_MODE (src_reg) != SImode
6423       || ! INT_REGNO_P (REGNO (src_reg)))
6424     return 0;
6425
6426   for (i = 0; i < count; i++)
6427     {
6428       rtx exp = XVECEXP (op, 0, i);
6429       rtx unspec;
6430       int maskval;
6431       
6432       if (GET_CODE (exp) != SET
6433           || GET_CODE (SET_DEST (exp)) != REG
6434           || GET_MODE (SET_DEST (exp)) != CCmode
6435           || ! CR_REGNO_P (REGNO (SET_DEST (exp))))
6436         return 0;
6437       unspec = SET_SRC (exp);
6438       maskval = 1 << (MAX_CR_REGNO - REGNO (SET_DEST (exp)));
6439       
6440       if (GET_CODE (unspec) != UNSPEC
6441           || XINT (unspec, 1) != 20
6442           || XVECLEN (unspec, 0) != 2
6443           || XVECEXP (unspec, 0, 0) != src_reg
6444           || GET_CODE (XVECEXP (unspec, 0, 1)) != CONST_INT
6445           || INTVAL (XVECEXP (unspec, 0, 1)) != maskval)
6446         return 0;
6447     }
6448   return 1;
6449 }
6450
6451 /* Return 1 for an PARALLEL suitable for lmw.  */
6452
6453 int
6454 lmw_operation (op, mode)
6455      rtx op;
6456      enum machine_mode mode ATTRIBUTE_UNUSED;
6457 {
6458   int count = XVECLEN (op, 0);
6459   unsigned int dest_regno;
6460   rtx src_addr;
6461   unsigned int base_regno;
6462   HOST_WIDE_INT offset;
6463   int i;
6464
6465   /* Perform a quick check so we don't blow up below.  */
6466   if (count <= 1
6467       || GET_CODE (XVECEXP (op, 0, 0)) != SET
6468       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
6469       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
6470     return 0;
6471
6472   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
6473   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
6474
6475   if (dest_regno > 31
6476       || count != 32 - (int) dest_regno)
6477     return 0;
6478
6479   if (LEGITIMATE_INDIRECT_ADDRESS_P (src_addr, 0))
6480     {
6481       offset = 0;
6482       base_regno = REGNO (src_addr);
6483       if (base_regno == 0)
6484         return 0;
6485     }
6486   else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, src_addr, 0))
6487     {
6488       offset = INTVAL (XEXP (src_addr, 1));
6489       base_regno = REGNO (XEXP (src_addr, 0));
6490     }
6491   else
6492     return 0;
6493
6494   for (i = 0; i < count; i++)
6495     {
6496       rtx elt = XVECEXP (op, 0, i);
6497       rtx newaddr;
6498       rtx addr_reg;
6499       HOST_WIDE_INT newoffset;
6500
6501       if (GET_CODE (elt) != SET
6502           || GET_CODE (SET_DEST (elt)) != REG
6503           || GET_MODE (SET_DEST (elt)) != SImode
6504           || REGNO (SET_DEST (elt)) != dest_regno + i
6505           || GET_CODE (SET_SRC (elt)) != MEM
6506           || GET_MODE (SET_SRC (elt)) != SImode)
6507         return 0;
6508       newaddr = XEXP (SET_SRC (elt), 0);
6509       if (LEGITIMATE_INDIRECT_ADDRESS_P (newaddr, 0))
6510         {
6511           newoffset = 0;
6512           addr_reg = newaddr;
6513         }
6514       else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, newaddr, 0))
6515         {
6516           addr_reg = XEXP (newaddr, 0);
6517           newoffset = INTVAL (XEXP (newaddr, 1));
6518         }
6519       else
6520         return 0;
6521       if (REGNO (addr_reg) != base_regno
6522           || newoffset != offset + 4 * i)
6523         return 0;
6524     }
6525
6526   return 1;
6527 }
6528
6529 /* Return 1 for an PARALLEL suitable for stmw.  */
6530
6531 int
6532 stmw_operation (op, mode)
6533      rtx op;
6534      enum machine_mode mode ATTRIBUTE_UNUSED;
6535 {
6536   int count = XVECLEN (op, 0);
6537   unsigned int src_regno;
6538   rtx dest_addr;
6539   unsigned int base_regno;
6540   HOST_WIDE_INT offset;
6541   int i;
6542
6543   /* Perform a quick check so we don't blow up below.  */
6544   if (count <= 1
6545       || GET_CODE (XVECEXP (op, 0, 0)) != SET
6546       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
6547       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
6548     return 0;
6549
6550   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
6551   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
6552
6553   if (src_regno > 31
6554       || count != 32 - (int) src_regno)
6555     return 0;
6556
6557   if (LEGITIMATE_INDIRECT_ADDRESS_P (dest_addr, 0))
6558     {
6559       offset = 0;
6560       base_regno = REGNO (dest_addr);
6561       if (base_regno == 0)
6562         return 0;
6563     }
6564   else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, dest_addr, 0))
6565     {
6566       offset = INTVAL (XEXP (dest_addr, 1));
6567       base_regno = REGNO (XEXP (dest_addr, 0));
6568     }
6569   else
6570     return 0;
6571
6572   for (i = 0; i < count; i++)
6573     {
6574       rtx elt = XVECEXP (op, 0, i);
6575       rtx newaddr;
6576       rtx addr_reg;
6577       HOST_WIDE_INT newoffset;
6578
6579       if (GET_CODE (elt) != SET
6580           || GET_CODE (SET_SRC (elt)) != REG
6581           || GET_MODE (SET_SRC (elt)) != SImode
6582           || REGNO (SET_SRC (elt)) != src_regno + i
6583           || GET_CODE (SET_DEST (elt)) != MEM
6584           || GET_MODE (SET_DEST (elt)) != SImode)
6585         return 0;
6586       newaddr = XEXP (SET_DEST (elt), 0);
6587       if (LEGITIMATE_INDIRECT_ADDRESS_P (newaddr, 0))
6588         {
6589           newoffset = 0;
6590           addr_reg = newaddr;
6591         }
6592       else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, newaddr, 0))
6593         {
6594           addr_reg = XEXP (newaddr, 0);
6595           newoffset = INTVAL (XEXP (newaddr, 1));
6596         }
6597       else
6598         return 0;
6599       if (REGNO (addr_reg) != base_regno
6600           || newoffset != offset + 4 * i)
6601         return 0;
6602     }
6603
6604   return 1;
6605 }
6606 \f
6607 /* A validation routine: say whether CODE, a condition code, and MODE
6608    match.  The other alternatives either don't make sense or should
6609    never be generated.  */
6610
6611 static void
6612 validate_condition_mode (code, mode)
6613      enum rtx_code code;
6614      enum machine_mode mode;
6615 {
6616   if (GET_RTX_CLASS (code) != '<' 
6617       || GET_MODE_CLASS (mode) != MODE_CC)
6618     abort ();
6619
6620   /* These don't make sense.  */
6621   if ((code == GT || code == LT || code == GE || code == LE)
6622       && mode == CCUNSmode)
6623     abort ();
6624
6625   if ((code == GTU || code == LTU || code == GEU || code == LEU)
6626       && mode != CCUNSmode)
6627     abort ();
6628
6629   if (mode != CCFPmode
6630       && (code == ORDERED || code == UNORDERED
6631           || code == UNEQ || code == LTGT
6632           || code == UNGT || code == UNLT
6633           || code == UNGE || code == UNLE))
6634     abort ();
6635   
6636   /* These should never be generated except for 
6637      flag_unsafe_math_optimizations.  */
6638   if (mode == CCFPmode
6639       && ! flag_unsafe_math_optimizations
6640       && (code == LE || code == GE
6641           || code == UNEQ || code == LTGT
6642           || code == UNGT || code == UNLT))
6643     abort ();
6644
6645   /* These are invalid; the information is not there.  */
6646   if (mode == CCEQmode 
6647       && code != EQ && code != NE)
6648     abort ();
6649 }
6650
6651 /* Return 1 if OP is a comparison operation that is valid for a branch insn.
6652    We only check the opcode against the mode of the CC value here.  */
6653
6654 int
6655 branch_comparison_operator (op, mode)
6656      rtx op;
6657      enum machine_mode mode ATTRIBUTE_UNUSED;
6658 {
6659   enum rtx_code code = GET_CODE (op);
6660   enum machine_mode cc_mode;
6661
6662   if (GET_RTX_CLASS (code) != '<')
6663     return 0;
6664
6665   cc_mode = GET_MODE (XEXP (op, 0));
6666   if (GET_MODE_CLASS (cc_mode) != MODE_CC)
6667     return 0;
6668
6669   validate_condition_mode (code, cc_mode);
6670
6671   return 1;
6672 }
6673
6674 /* Return 1 if OP is a comparison operation that is valid for a branch
6675    insn and which is true if the corresponding bit in the CC register
6676    is set.  */
6677
6678 int
6679 branch_positive_comparison_operator (op, mode)
6680      rtx op;
6681      enum machine_mode mode;
6682 {
6683   enum rtx_code code;
6684
6685   if (! branch_comparison_operator (op, mode))
6686     return 0;
6687
6688   code = GET_CODE (op);
6689   return (code == EQ || code == LT || code == GT
6690           || (TARGET_SPE && TARGET_HARD_FLOAT && !TARGET_FPRS && code == NE)
6691           || code == LTU || code == GTU
6692           || code == UNORDERED);
6693 }
6694
6695 /* Return 1 if OP is a comparison operation that is valid for an scc insn.
6696    We check the opcode against the mode of the CC value and disallow EQ or
6697    NE comparisons for integers.  */
6698
6699 int
6700 scc_comparison_operator (op, mode)
6701      rtx op;
6702      enum machine_mode mode;
6703 {
6704   enum rtx_code code = GET_CODE (op);
6705   enum machine_mode cc_mode;
6706
6707   if (GET_MODE (op) != mode && mode != VOIDmode)
6708     return 0;
6709
6710   if (GET_RTX_CLASS (code) != '<')
6711     return 0;
6712
6713   cc_mode = GET_MODE (XEXP (op, 0));
6714   if (GET_MODE_CLASS (cc_mode) != MODE_CC)
6715     return 0;
6716
6717   validate_condition_mode (code, cc_mode);
6718
6719   if (code == NE && cc_mode != CCFPmode)
6720     return 0;
6721
6722   return 1;
6723 }
6724
6725 int
6726 trap_comparison_operator (op, mode)
6727     rtx op;
6728     enum machine_mode mode;
6729 {
6730   if (mode != VOIDmode && mode != GET_MODE (op))
6731     return 0;
6732   return GET_RTX_CLASS (GET_CODE (op)) == '<';
6733 }
6734
6735 int
6736 boolean_operator (op, mode)
6737     rtx op;
6738     enum machine_mode mode ATTRIBUTE_UNUSED;
6739 {
6740   enum rtx_code code = GET_CODE (op);
6741   return (code == AND || code == IOR || code == XOR);
6742 }
6743
6744 int
6745 boolean_or_operator (op, mode)
6746     rtx op;
6747     enum machine_mode mode ATTRIBUTE_UNUSED;
6748 {
6749   enum rtx_code code = GET_CODE (op);
6750   return (code == IOR || code == XOR);
6751 }
6752
6753 int
6754 min_max_operator (op, mode)
6755     rtx op;
6756     enum machine_mode mode ATTRIBUTE_UNUSED;
6757 {
6758   enum rtx_code code = GET_CODE (op);
6759   return (code == SMIN || code == SMAX || code == UMIN || code == UMAX);
6760 }
6761 \f
6762 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
6763    mask required to convert the result of a rotate insn into a shift
6764    left insn of SHIFTOP bits.  Both are known to be SImode CONST_INT.  */
6765
6766 int
6767 includes_lshift_p (shiftop, andop)
6768      rtx shiftop;
6769      rtx andop;
6770 {
6771   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
6772
6773   shift_mask <<= INTVAL (shiftop);
6774
6775   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
6776 }
6777
6778 /* Similar, but for right shift.  */
6779
6780 int
6781 includes_rshift_p (shiftop, andop)
6782      rtx shiftop;
6783      rtx andop;
6784 {
6785   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
6786
6787   shift_mask >>= INTVAL (shiftop);
6788
6789   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
6790 }
6791
6792 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
6793    to perform a left shift.  It must have exactly SHIFTOP least
6794    signifigant 0's, then one or more 1's, then zero or more 0's.  */
6795
6796 int
6797 includes_rldic_lshift_p (shiftop, andop)
6798      rtx shiftop;
6799      rtx andop;
6800 {
6801   if (GET_CODE (andop) == CONST_INT)
6802     {
6803       HOST_WIDE_INT c, lsb, shift_mask;
6804
6805       c = INTVAL (andop);
6806       if (c == 0 || c == ~0)
6807         return 0;
6808
6809       shift_mask = ~0;
6810       shift_mask <<= INTVAL (shiftop);
6811
6812       /* Find the least signifigant one bit.  */
6813       lsb = c & -c;
6814
6815       /* It must coincide with the LSB of the shift mask.  */
6816       if (-lsb != shift_mask)
6817         return 0;
6818
6819       /* Invert to look for the next transition (if any).  */
6820       c = ~c;
6821
6822       /* Remove the low group of ones (originally low group of zeros).  */
6823       c &= -lsb;
6824
6825       /* Again find the lsb, and check we have all 1's above.  */
6826       lsb = c & -c;
6827       return c == -lsb;
6828     }
6829   else if (GET_CODE (andop) == CONST_DOUBLE
6830            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
6831     {
6832       HOST_WIDE_INT low, high, lsb;
6833       HOST_WIDE_INT shift_mask_low, shift_mask_high;
6834
6835       low = CONST_DOUBLE_LOW (andop);
6836       if (HOST_BITS_PER_WIDE_INT < 64)
6837         high = CONST_DOUBLE_HIGH (andop);
6838
6839       if ((low == 0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == 0))
6840           || (low == ~0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0)))
6841         return 0;
6842
6843       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
6844         {
6845           shift_mask_high = ~0;
6846           if (INTVAL (shiftop) > 32)
6847             shift_mask_high <<= INTVAL (shiftop) - 32;
6848
6849           lsb = high & -high;
6850
6851           if (-lsb != shift_mask_high || INTVAL (shiftop) < 32)
6852             return 0;
6853
6854           high = ~high;
6855           high &= -lsb;
6856
6857           lsb = high & -high;
6858           return high == -lsb;
6859         }
6860
6861       shift_mask_low = ~0;
6862       shift_mask_low <<= INTVAL (shiftop);
6863
6864       lsb = low & -low;
6865
6866       if (-lsb != shift_mask_low)
6867         return 0;
6868
6869       if (HOST_BITS_PER_WIDE_INT < 64)
6870         high = ~high;
6871       low = ~low;
6872       low &= -lsb;
6873
6874       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
6875         {
6876           lsb = high & -high;
6877           return high == -lsb;
6878         }
6879
6880       lsb = low & -low;
6881       return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
6882     }
6883   else
6884     return 0;
6885 }
6886
6887 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
6888    to perform a left shift.  It must have SHIFTOP or more least
6889    signifigant 0's, with the remainder of the word 1's.  */
6890
6891 int
6892 includes_rldicr_lshift_p (shiftop, andop)
6893      rtx shiftop;
6894      rtx andop;
6895 {
6896   if (GET_CODE (andop) == CONST_INT)
6897     {
6898       HOST_WIDE_INT c, lsb, shift_mask;
6899
6900       shift_mask = ~0;
6901       shift_mask <<= INTVAL (shiftop);
6902       c = INTVAL (andop);
6903
6904       /* Find the least signifigant one bit.  */
6905       lsb = c & -c;
6906
6907       /* It must be covered by the shift mask.
6908          This test also rejects c == 0.  */
6909       if ((lsb & shift_mask) == 0)
6910         return 0;
6911
6912       /* Check we have all 1's above the transition, and reject all 1's.  */
6913       return c == -lsb && lsb != 1;
6914     }
6915   else if (GET_CODE (andop) == CONST_DOUBLE
6916            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
6917     {
6918       HOST_WIDE_INT low, lsb, shift_mask_low;
6919
6920       low = CONST_DOUBLE_LOW (andop);
6921
6922       if (HOST_BITS_PER_WIDE_INT < 64)
6923         {
6924           HOST_WIDE_INT high, shift_mask_high;
6925
6926           high = CONST_DOUBLE_HIGH (andop);
6927
6928           if (low == 0)
6929             {
6930               shift_mask_high = ~0;
6931               if (INTVAL (shiftop) > 32)
6932                 shift_mask_high <<= INTVAL (shiftop) - 32;
6933
6934               lsb = high & -high;
6935
6936               if ((lsb & shift_mask_high) == 0)
6937                 return 0;
6938
6939               return high == -lsb;
6940             }
6941           if (high != ~0)
6942             return 0;
6943         }
6944
6945       shift_mask_low = ~0;
6946       shift_mask_low <<= INTVAL (shiftop);
6947
6948       lsb = low & -low;
6949
6950       if ((lsb & shift_mask_low) == 0)
6951         return 0;
6952
6953       return low == -lsb && lsb != 1;
6954     }
6955   else
6956     return 0;
6957 }
6958
6959 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
6960    for lfq and stfq insns.
6961
6962    Note reg1 and reg2 *must* be hard registers.  To be sure we will
6963    abort if we are passed pseudo registers.  */
6964
6965 int
6966 registers_ok_for_quad_peep (reg1, reg2)
6967      rtx reg1, reg2;
6968 {
6969   /* We might have been passed a SUBREG.  */
6970   if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG) 
6971     return 0;
6972
6973   return (REGNO (reg1) == REGNO (reg2) - 1);
6974 }
6975
6976 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
6977    addr1 and addr2 must be in consecutive memory locations
6978    (addr2 == addr1 + 8).  */
6979
6980 int
6981 addrs_ok_for_quad_peep (addr1, addr2)
6982      rtx addr1;
6983      rtx addr2;
6984 {
6985   unsigned int reg1;
6986   int offset1;
6987
6988   /* Extract an offset (if used) from the first addr.  */
6989   if (GET_CODE (addr1) == PLUS)
6990     {
6991       /* If not a REG, return zero.  */
6992       if (GET_CODE (XEXP (addr1, 0)) != REG)
6993         return 0;
6994       else
6995         {
6996           reg1 = REGNO (XEXP (addr1, 0));
6997           /* The offset must be constant!  */
6998           if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
6999             return 0;
7000           offset1 = INTVAL (XEXP (addr1, 1));
7001         }
7002     }
7003   else if (GET_CODE (addr1) != REG)
7004     return 0;
7005   else
7006     {
7007       reg1 = REGNO (addr1);
7008       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
7009       offset1 = 0;
7010     }
7011
7012   /* Make sure the second address is a (mem (plus (reg) (const_int))).  */
7013   if (GET_CODE (addr2) != PLUS)
7014     return 0;
7015
7016   if (GET_CODE (XEXP (addr2, 0)) != REG
7017       || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
7018     return 0;
7019
7020   if (reg1 != REGNO (XEXP (addr2, 0)))
7021     return 0;
7022
7023   /* The offset for the second addr must be 8 more than the first addr.  */
7024   if (INTVAL (XEXP (addr2, 1)) != offset1 + 8)
7025     return 0;
7026
7027   /* All the tests passed.  addr1 and addr2 are valid for lfq or stfq
7028      instructions.  */
7029   return 1;
7030 }
7031 \f
7032 /* Return the register class of a scratch register needed to copy IN into
7033    or out of a register in CLASS in MODE.  If it can be done directly,
7034    NO_REGS is returned.  */
7035
7036 enum reg_class
7037 secondary_reload_class (class, mode, in)
7038      enum reg_class class;
7039      enum machine_mode mode ATTRIBUTE_UNUSED;
7040      rtx in;
7041 {
7042   int regno;
7043
7044   if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN && flag_pic))
7045     {
7046       /* We cannot copy a symbolic operand directly into anything
7047          other than BASE_REGS for TARGET_ELF.  So indicate that a
7048          register from BASE_REGS is needed as an intermediate
7049          register.
7050          
7051          On Darwin, pic addresses require a load from memory, which
7052          needs a base register.  */
7053       if (class != BASE_REGS
7054           && (GET_CODE (in) == SYMBOL_REF
7055               || GET_CODE (in) == HIGH
7056               || GET_CODE (in) == LABEL_REF
7057               || GET_CODE (in) == CONST))
7058         return BASE_REGS;
7059     }
7060
7061   if (GET_CODE (in) == REG)
7062     {
7063       regno = REGNO (in);
7064       if (regno >= FIRST_PSEUDO_REGISTER)
7065         {
7066           regno = true_regnum (in);
7067           if (regno >= FIRST_PSEUDO_REGISTER)
7068             regno = -1;
7069         }
7070     }
7071   else if (GET_CODE (in) == SUBREG)
7072     {
7073       regno = true_regnum (in);
7074       if (regno >= FIRST_PSEUDO_REGISTER)
7075         regno = -1;
7076     }
7077   else
7078     regno = -1;
7079
7080   /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
7081      into anything.  */
7082   if (class == GENERAL_REGS || class == BASE_REGS
7083       || (regno >= 0 && INT_REGNO_P (regno)))
7084     return NO_REGS;
7085
7086   /* Constants, memory, and FP registers can go into FP registers.  */
7087   if ((regno == -1 || FP_REGNO_P (regno))
7088       && (class == FLOAT_REGS || class == NON_SPECIAL_REGS))
7089     return NO_REGS;
7090
7091   /* Memory, and AltiVec registers can go into AltiVec registers.  */
7092   if ((regno == -1 || ALTIVEC_REGNO_P (regno))
7093       && class == ALTIVEC_REGS)
7094     return NO_REGS;
7095
7096   /* We can copy among the CR registers.  */
7097   if ((class == CR_REGS || class == CR0_REGS)
7098       && regno >= 0 && CR_REGNO_P (regno))
7099     return NO_REGS;
7100
7101   /* Otherwise, we need GENERAL_REGS.  */
7102   return GENERAL_REGS;
7103 }
7104 \f
7105 /* Given a comparison operation, return the bit number in CCR to test.  We
7106    know this is a valid comparison.  
7107
7108    SCC_P is 1 if this is for an scc.  That means that %D will have been
7109    used instead of %C, so the bits will be in different places.
7110
7111    Return -1 if OP isn't a valid comparison for some reason.  */
7112
7113 int
7114 ccr_bit (op, scc_p)
7115      rtx op;
7116      int scc_p;
7117 {
7118   enum rtx_code code = GET_CODE (op);
7119   enum machine_mode cc_mode;
7120   int cc_regnum;
7121   int base_bit;
7122   rtx reg;
7123
7124   if (GET_RTX_CLASS (code) != '<')
7125     return -1;
7126
7127   reg = XEXP (op, 0);
7128
7129   if (GET_CODE (reg) != REG
7130       || ! CR_REGNO_P (REGNO (reg)))
7131     abort ();
7132
7133   cc_mode = GET_MODE (reg);
7134   cc_regnum = REGNO (reg);
7135   base_bit = 4 * (cc_regnum - CR0_REGNO);
7136
7137   validate_condition_mode (code, cc_mode);
7138
7139   switch (code)
7140     {
7141     case NE:
7142       if (TARGET_SPE && TARGET_HARD_FLOAT && cc_mode == CCFPmode)
7143         return base_bit + 1;
7144       return scc_p ? base_bit + 3 : base_bit + 2;
7145     case EQ:
7146       if (TARGET_SPE && TARGET_HARD_FLOAT && cc_mode == CCFPmode)
7147         return base_bit + 1;
7148       return base_bit + 2;
7149     case GT:  case GTU:  case UNLE:
7150       return base_bit + 1;
7151     case LT:  case LTU:  case UNGE:
7152       return base_bit;
7153     case ORDERED:  case UNORDERED:
7154       return base_bit + 3;
7155
7156     case GE:  case GEU:
7157       /* If scc, we will have done a cror to put the bit in the
7158          unordered position.  So test that bit.  For integer, this is ! LT
7159          unless this is an scc insn.  */
7160       return scc_p ? base_bit + 3 : base_bit;
7161
7162     case LE:  case LEU:
7163       return scc_p ? base_bit + 3 : base_bit + 1;
7164
7165     default:
7166       abort ();
7167     }
7168 }
7169 \f
7170 /* Return the GOT register.  */
7171
7172 struct rtx_def *
7173 rs6000_got_register (value)
7174      rtx value ATTRIBUTE_UNUSED;
7175 {
7176   /* The second flow pass currently (June 1999) can't update
7177      regs_ever_live without disturbing other parts of the compiler, so
7178      update it here to make the prolog/epilogue code happy.  */
7179   if (no_new_pseudos && ! regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM])
7180     regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7181
7182   current_function_uses_pic_offset_table = 1;
7183
7184   return pic_offset_table_rtx;
7185 }
7186 \f
7187 /* Function to init struct machine_function.
7188    This will be called, via a pointer variable,
7189    from push_function_context.  */
7190
7191 static struct machine_function *
7192 rs6000_init_machine_status ()
7193 {
7194   return ggc_alloc_cleared (sizeof (machine_function));
7195 }
7196 \f
7197 /* These macros test for integers and extract the low-order bits.  */
7198 #define INT_P(X)  \
7199 ((GET_CODE (X) == CONST_INT || GET_CODE (X) == CONST_DOUBLE)    \
7200  && GET_MODE (X) == VOIDmode)
7201
7202 #define INT_LOWPART(X) \
7203   (GET_CODE (X) == CONST_INT ? INTVAL (X) : CONST_DOUBLE_LOW (X))
7204
7205 int
7206 extract_MB (op)
7207      rtx op;
7208 {
7209   int i;
7210   unsigned long val = INT_LOWPART (op);
7211
7212   /* If the high bit is zero, the value is the first 1 bit we find
7213      from the left.  */
7214   if ((val & 0x80000000) == 0)
7215     {
7216       if ((val & 0xffffffff) == 0)
7217         abort ();
7218
7219       i = 1;
7220       while (((val <<= 1) & 0x80000000) == 0)
7221         ++i;
7222       return i;
7223     }
7224
7225   /* If the high bit is set and the low bit is not, or the mask is all
7226      1's, the value is zero.  */
7227   if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
7228     return 0;
7229
7230   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
7231      from the right.  */
7232   i = 31;
7233   while (((val >>= 1) & 1) != 0)
7234     --i;
7235
7236   return i;
7237 }
7238
7239 int
7240 extract_ME (op)
7241      rtx op;
7242 {
7243   int i;
7244   unsigned long val = INT_LOWPART (op);
7245
7246   /* If the low bit is zero, the value is the first 1 bit we find from
7247      the right.  */
7248   if ((val & 1) == 0)
7249     {
7250       if ((val & 0xffffffff) == 0)
7251         abort ();
7252
7253       i = 30;
7254       while (((val >>= 1) & 1) == 0)
7255         --i;
7256
7257       return i;
7258     }
7259
7260   /* If the low bit is set and the high bit is not, or the mask is all
7261      1's, the value is 31.  */
7262   if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
7263     return 31;
7264
7265   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
7266      from the left.  */
7267   i = 0;
7268   while (((val <<= 1) & 0x80000000) != 0)
7269     ++i;
7270
7271   return i;
7272 }
7273
7274 /* Print an operand.  Recognize special options, documented below.  */
7275
7276 #if TARGET_ELF
7277 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
7278 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
7279 #else
7280 #define SMALL_DATA_RELOC "sda21"
7281 #define SMALL_DATA_REG 0
7282 #endif
7283
7284 void
7285 print_operand (file, x, code)
7286     FILE *file;
7287     rtx x;
7288     int code;
7289 {
7290   int i;
7291   HOST_WIDE_INT val;
7292   unsigned HOST_WIDE_INT uval;
7293
7294   switch (code)
7295     {
7296     case '.':
7297       /* Write out an instruction after the call which may be replaced
7298          with glue code by the loader.  This depends on the AIX version.  */
7299       asm_fprintf (file, RS6000_CALL_GLUE);
7300       return;
7301
7302       /* %a is output_address.  */
7303
7304     case 'A':
7305       /* If X is a constant integer whose low-order 5 bits are zero,
7306          write 'l'.  Otherwise, write 'r'.  This is a kludge to fix a bug
7307          in the AIX assembler where "sri" with a zero shift count
7308          writes a trash instruction.  */
7309       if (GET_CODE (x) == CONST_INT && (INTVAL (x) & 31) == 0)
7310         putc ('l', file);
7311       else
7312         putc ('r', file);
7313       return;
7314
7315     case 'b':
7316       /* If constant, low-order 16 bits of constant, unsigned.
7317          Otherwise, write normally.  */
7318       if (INT_P (x))
7319         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 0xffff);
7320       else
7321         print_operand (file, x, 0);
7322       return;
7323
7324     case 'B':
7325       /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
7326          for 64-bit mask direction.  */
7327       putc (((INT_LOWPART(x) & 1) == 0 ? 'r' : 'l'), file);
7328       return;
7329
7330       /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
7331          output_operand.  */
7332
7333     case 'D':
7334       /* There used to be a comment for 'C' reading "This is an
7335            optional cror needed for certain floating-point
7336            comparisons.  Otherwise write nothing."  */
7337
7338       /* Similar, except that this is for an scc, so we must be able to
7339          encode the test in a single bit that is one.  We do the above
7340          for any LE, GE, GEU, or LEU and invert the bit for NE.  */
7341       if (GET_CODE (x) == LE || GET_CODE (x) == GE
7342           || GET_CODE (x) == LEU || GET_CODE (x) == GEU)
7343         {
7344           int base_bit = 4 * (REGNO (XEXP (x, 0)) - CR0_REGNO);
7345
7346           fprintf (file, "cror %d,%d,%d\n\t", base_bit + 3,
7347                    base_bit + 2,
7348                    base_bit + (GET_CODE (x) == GE || GET_CODE (x) == GEU));
7349         }
7350
7351       else if (GET_CODE (x) == NE)
7352         {
7353           int base_bit = 4 * (REGNO (XEXP (x, 0)) - CR0_REGNO);
7354
7355           fprintf (file, "crnor %d,%d,%d\n\t", base_bit + 3,
7356                    base_bit + 2, base_bit + 2);
7357         }
7358       else if (TARGET_SPE && TARGET_HARD_FLOAT
7359                && GET_CODE (x) == EQ
7360                && GET_MODE (XEXP (x, 0)) == CCFPmode)
7361         {
7362           int base_bit = 4 * (REGNO (XEXP (x, 0)) - CR0_REGNO);
7363
7364           fprintf (file, "crnor %d,%d,%d\n\t", base_bit + 1,
7365                    base_bit + 1, base_bit + 1);
7366         }
7367       return;
7368
7369     case 'E':
7370       /* X is a CR register.  Print the number of the EQ bit of the CR */
7371       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
7372         output_operand_lossage ("invalid %%E value");
7373       else
7374         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
7375       return;
7376
7377     case 'f':
7378       /* X is a CR register.  Print the shift count needed to move it
7379          to the high-order four bits.  */
7380       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
7381         output_operand_lossage ("invalid %%f value");
7382       else
7383         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
7384       return;
7385
7386     case 'F':
7387       /* Similar, but print the count for the rotate in the opposite
7388          direction.  */
7389       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
7390         output_operand_lossage ("invalid %%F value");
7391       else
7392         fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
7393       return;
7394
7395     case 'G':
7396       /* X is a constant integer.  If it is negative, print "m",
7397          otherwise print "z".  This is to make a aze or ame insn.  */
7398       if (GET_CODE (x) != CONST_INT)
7399         output_operand_lossage ("invalid %%G value");
7400       else if (INTVAL (x) >= 0)
7401         putc ('z', file);
7402       else
7403         putc ('m', file);
7404       return;
7405
7406     case 'h':
7407       /* If constant, output low-order five bits.  Otherwise, write
7408          normally.  */
7409       if (INT_P (x))
7410         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 31);
7411       else
7412         print_operand (file, x, 0);
7413       return;
7414
7415     case 'H':
7416       /* If constant, output low-order six bits.  Otherwise, write
7417          normally.  */
7418       if (INT_P (x))
7419         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 63);
7420       else
7421         print_operand (file, x, 0);
7422       return;
7423
7424     case 'I':
7425       /* Print `i' if this is a constant, else nothing.  */
7426       if (INT_P (x))
7427         putc ('i', file);
7428       return;
7429
7430     case 'j':
7431       /* Write the bit number in CCR for jump.  */
7432       i = ccr_bit (x, 0);
7433       if (i == -1)
7434         output_operand_lossage ("invalid %%j code");
7435       else
7436         fprintf (file, "%d", i);
7437       return;
7438
7439     case 'J':
7440       /* Similar, but add one for shift count in rlinm for scc and pass
7441          scc flag to `ccr_bit'.  */
7442       i = ccr_bit (x, 1);
7443       if (i == -1)
7444         output_operand_lossage ("invalid %%J code");
7445       else
7446         /* If we want bit 31, write a shift count of zero, not 32.  */
7447         fprintf (file, "%d", i == 31 ? 0 : i + 1);
7448       return;
7449
7450     case 'k':
7451       /* X must be a constant.  Write the 1's complement of the
7452          constant.  */
7453       if (! INT_P (x))
7454         output_operand_lossage ("invalid %%k value");
7455       else
7456         fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INT_LOWPART (x));
7457       return;
7458
7459     case 'K':
7460       /* X must be a symbolic constant on ELF.  Write an
7461          expression suitable for an 'addi' that adds in the low 16
7462          bits of the MEM.  */
7463       if (GET_CODE (x) != CONST)
7464         {
7465           print_operand_address (file, x);
7466           fputs ("@l", file);
7467         }
7468       else
7469         {
7470           if (GET_CODE (XEXP (x, 0)) != PLUS
7471               || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
7472                   && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
7473               || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
7474             output_operand_lossage ("invalid %%K value");
7475           print_operand_address (file, XEXP (XEXP (x, 0), 0));
7476           fputs ("@l", file);
7477           /* For GNU as, there must be a non-alphanumeric character
7478              between 'l' and the number.  The '-' is added by
7479              print_operand() already.  */
7480           if (INTVAL (XEXP (XEXP (x, 0), 1)) >= 0)
7481             fputs ("+", file);
7482           print_operand (file, XEXP (XEXP (x, 0), 1), 0);
7483         }
7484       return;
7485
7486       /* %l is output_asm_label.  */
7487
7488     case 'L':
7489       /* Write second word of DImode or DFmode reference.  Works on register
7490          or non-indexed memory only.  */
7491       if (GET_CODE (x) == REG)
7492         fprintf (file, "%s", reg_names[REGNO (x) + 1]);
7493       else if (GET_CODE (x) == MEM)
7494         {
7495           /* Handle possible auto-increment.  Since it is pre-increment and
7496              we have already done it, we can just use an offset of word.  */
7497           if (GET_CODE (XEXP (x, 0)) == PRE_INC
7498               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
7499             output_address (plus_constant (XEXP (XEXP (x, 0), 0),
7500                                            UNITS_PER_WORD));
7501           else
7502             output_address (XEXP (adjust_address_nv (x, SImode,
7503                                                      UNITS_PER_WORD),
7504                                   0));
7505
7506           if (small_data_operand (x, GET_MODE (x)))
7507             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
7508                      reg_names[SMALL_DATA_REG]);
7509         }
7510       return;
7511                             
7512     case 'm':
7513       /* MB value for a mask operand.  */
7514       if (! mask_operand (x, SImode))
7515         output_operand_lossage ("invalid %%m value");
7516
7517       fprintf (file, "%d", extract_MB (x));
7518       return;
7519
7520     case 'M':
7521       /* ME value for a mask operand.  */
7522       if (! mask_operand (x, SImode))
7523         output_operand_lossage ("invalid %%M value");
7524
7525       fprintf (file, "%d", extract_ME (x));
7526       return;
7527
7528       /* %n outputs the negative of its operand.  */
7529
7530     case 'N':
7531       /* Write the number of elements in the vector times 4.  */
7532       if (GET_CODE (x) != PARALLEL)
7533         output_operand_lossage ("invalid %%N value");
7534       else
7535         fprintf (file, "%d", XVECLEN (x, 0) * 4);
7536       return;
7537
7538     case 'O':
7539       /* Similar, but subtract 1 first.  */
7540       if (GET_CODE (x) != PARALLEL)
7541         output_operand_lossage ("invalid %%O value");
7542       else
7543         fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
7544       return;
7545
7546     case 'p':
7547       /* X is a CONST_INT that is a power of two.  Output the logarithm.  */
7548       if (! INT_P (x)
7549           || INT_LOWPART (x) < 0
7550           || (i = exact_log2 (INT_LOWPART (x))) < 0)
7551         output_operand_lossage ("invalid %%p value");
7552       else
7553         fprintf (file, "%d", i);
7554       return;
7555
7556     case 'P':
7557       /* The operand must be an indirect memory reference.  The result
7558          is the register number.  */
7559       if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
7560           || REGNO (XEXP (x, 0)) >= 32)
7561         output_operand_lossage ("invalid %%P value");
7562       else
7563         fprintf (file, "%d", REGNO (XEXP (x, 0)));
7564       return;
7565
7566     case 'q':
7567       /* This outputs the logical code corresponding to a boolean
7568          expression.  The expression may have one or both operands
7569          negated (if one, only the first one).  For condition register
7570          logical operations, it will also treat the negated
7571          CR codes as NOTs, but not handle NOTs of them.  */
7572       {
7573         const char *const *t = 0;
7574         const char *s;
7575         enum rtx_code code = GET_CODE (x);
7576         static const char * const tbl[3][3] = {
7577           { "and", "andc", "nor" },
7578           { "or", "orc", "nand" },
7579           { "xor", "eqv", "xor" } };
7580
7581         if (code == AND)
7582           t = tbl[0];
7583         else if (code == IOR)
7584           t = tbl[1];
7585         else if (code == XOR)
7586           t = tbl[2];
7587         else
7588           output_operand_lossage ("invalid %%q value");
7589
7590         if (GET_CODE (XEXP (x, 0)) != NOT)
7591           s = t[0];
7592         else
7593           {
7594             if (GET_CODE (XEXP (x, 1)) == NOT)
7595               s = t[2];
7596             else
7597               s = t[1];
7598           }
7599         
7600         fputs (s, file);
7601       }
7602       return;
7603
7604     case 'R':
7605       /* X is a CR register.  Print the mask for `mtcrf'.  */
7606       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
7607         output_operand_lossage ("invalid %%R value");
7608       else
7609         fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
7610       return;
7611
7612     case 's':
7613       /* Low 5 bits of 32 - value */
7614       if (! INT_P (x))
7615         output_operand_lossage ("invalid %%s value");
7616       else
7617         fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INT_LOWPART (x)) & 31);
7618       return;
7619
7620     case 'S':
7621       /* PowerPC64 mask position.  All 0's is excluded.
7622          CONST_INT 32-bit mask is considered sign-extended so any
7623          transition must occur within the CONST_INT, not on the boundary.  */
7624       if (! mask64_operand (x, DImode))
7625         output_operand_lossage ("invalid %%S value");
7626
7627       uval = INT_LOWPART (x);
7628
7629       if (uval & 1)     /* Clear Left */
7630         {
7631           uval &= ((unsigned HOST_WIDE_INT) 1 << 63 << 1) - 1;
7632           i = 64;
7633         }
7634       else              /* Clear Right */
7635         {
7636           uval = ~uval;
7637           uval &= ((unsigned HOST_WIDE_INT) 1 << 63 << 1) - 1;
7638           i = 63;
7639         }
7640       while (uval != 0)
7641         --i, uval >>= 1;
7642       if (i < 0)
7643         abort ();
7644       fprintf (file, "%d", i);
7645       return;
7646
7647     case 't':
7648       /* Like 'J' but get to the OVERFLOW/UNORDERED bit.  */
7649       if (GET_CODE (x) != REG || GET_MODE (x) != CCmode)
7650         abort ();
7651
7652       /* Bit 3 is OV bit.  */
7653       i = 4 * (REGNO (x) - CR0_REGNO) + 3;
7654
7655       /* If we want bit 31, write a shift count of zero, not 32.  */
7656       fprintf (file, "%d", i == 31 ? 0 : i + 1);
7657       return;
7658
7659     case 'T':
7660       /* Print the symbolic name of a branch target register.  */
7661       if (GET_CODE (x) != REG || (REGNO (x) != LINK_REGISTER_REGNUM
7662                                   && REGNO (x) != COUNT_REGISTER_REGNUM))
7663         output_operand_lossage ("invalid %%T value");
7664       else if (REGNO (x) == LINK_REGISTER_REGNUM)
7665         fputs (TARGET_NEW_MNEMONICS ? "lr" : "r", file);
7666       else
7667         fputs ("ctr", file);
7668       return;
7669
7670     case 'u':
7671       /* High-order 16 bits of constant for use in unsigned operand.  */
7672       if (! INT_P (x))
7673         output_operand_lossage ("invalid %%u value");
7674       else
7675         fprintf (file, HOST_WIDE_INT_PRINT_HEX, 
7676                  (INT_LOWPART (x) >> 16) & 0xffff);
7677       return;
7678
7679     case 'v':
7680       /* High-order 16 bits of constant for use in signed operand.  */
7681       if (! INT_P (x))
7682         output_operand_lossage ("invalid %%v value");
7683       else
7684         fprintf (file, HOST_WIDE_INT_PRINT_HEX,
7685                  (INT_LOWPART (x) >> 16) & 0xffff);
7686       return;
7687
7688     case 'U':
7689       /* Print `u' if this has an auto-increment or auto-decrement.  */
7690       if (GET_CODE (x) == MEM
7691           && (GET_CODE (XEXP (x, 0)) == PRE_INC
7692               || GET_CODE (XEXP (x, 0)) == PRE_DEC))
7693         putc ('u', file);
7694       return;
7695
7696     case 'V':
7697       /* Print the trap code for this operand.  */
7698       switch (GET_CODE (x))
7699         {
7700         case EQ:
7701           fputs ("eq", file);   /* 4 */
7702           break;
7703         case NE:
7704           fputs ("ne", file);   /* 24 */
7705           break;
7706         case LT:
7707           fputs ("lt", file);   /* 16 */
7708           break;
7709         case LE:
7710           fputs ("le", file);   /* 20 */
7711           break;
7712         case GT:
7713           fputs ("gt", file);   /* 8 */
7714           break;
7715         case GE:
7716           fputs ("ge", file);   /* 12 */
7717           break;
7718         case LTU:
7719           fputs ("llt", file);  /* 2 */
7720           break;
7721         case LEU:
7722           fputs ("lle", file);  /* 6 */
7723           break;
7724         case GTU:
7725           fputs ("lgt", file);  /* 1 */
7726           break;
7727         case GEU:
7728           fputs ("lge", file);  /* 5 */
7729           break;
7730         default:
7731           abort ();
7732         }
7733       break;
7734
7735     case 'w':
7736       /* If constant, low-order 16 bits of constant, signed.  Otherwise, write
7737          normally.  */
7738       if (INT_P (x))
7739         fprintf (file, HOST_WIDE_INT_PRINT_DEC, 
7740                  ((INT_LOWPART (x) & 0xffff) ^ 0x8000) - 0x8000);
7741       else
7742         print_operand (file, x, 0);
7743       return;
7744
7745     case 'W':
7746       /* MB value for a PowerPC64 rldic operand.  */
7747       val = (GET_CODE (x) == CONST_INT
7748              ? INTVAL (x) : CONST_DOUBLE_HIGH (x));
7749
7750       if (val < 0)
7751         i = -1;
7752       else
7753         for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
7754           if ((val <<= 1) < 0)
7755             break;
7756
7757 #if HOST_BITS_PER_WIDE_INT == 32
7758       if (GET_CODE (x) == CONST_INT && i >= 0)
7759         i += 32;  /* zero-extend high-part was all 0's */
7760       else if (GET_CODE (x) == CONST_DOUBLE && i == 32)
7761         {
7762           val = CONST_DOUBLE_LOW (x);
7763
7764           if (val == 0)
7765             abort ();
7766           else if (val < 0)
7767             --i;
7768           else
7769             for ( ; i < 64; i++)
7770               if ((val <<= 1) < 0)
7771                 break;
7772         }
7773 #endif
7774
7775       fprintf (file, "%d", i + 1);
7776       return;
7777
7778     case 'X':
7779       if (GET_CODE (x) == MEM
7780           && LEGITIMATE_INDEXED_ADDRESS_P (XEXP (x, 0), 0))
7781         putc ('x', file);
7782       return;
7783
7784     case 'Y':
7785       /* Like 'L', for third word of TImode  */
7786       if (GET_CODE (x) == REG)
7787         fprintf (file, "%s", reg_names[REGNO (x) + 2]);
7788       else if (GET_CODE (x) == MEM)
7789         {
7790           if (GET_CODE (XEXP (x, 0)) == PRE_INC
7791               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
7792             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 8));
7793           else
7794             output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
7795           if (small_data_operand (x, GET_MODE (x)))
7796             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
7797                      reg_names[SMALL_DATA_REG]);
7798         }
7799       return;
7800                             
7801     case 'z':
7802       /* X is a SYMBOL_REF.  Write out the name preceded by a
7803          period and without any trailing data in brackets.  Used for function
7804          names.  If we are configured for System V (or the embedded ABI) on
7805          the PowerPC, do not emit the period, since those systems do not use
7806          TOCs and the like.  */
7807       if (GET_CODE (x) != SYMBOL_REF)
7808         abort ();
7809
7810       if (XSTR (x, 0)[0] != '.')
7811         {
7812           switch (DEFAULT_ABI)
7813             {
7814             default:
7815               abort ();
7816
7817             case ABI_AIX:
7818               putc ('.', file);
7819               break;
7820
7821             case ABI_V4:
7822             case ABI_AIX_NODESC:
7823             case ABI_DARWIN:
7824               break;
7825             }
7826         }
7827 #if TARGET_AIX
7828       RS6000_OUTPUT_BASENAME (file, XSTR (x, 0));
7829 #else
7830       assemble_name (file, XSTR (x, 0));
7831 #endif
7832       return;
7833
7834     case 'Z':
7835       /* Like 'L', for last word of TImode.  */
7836       if (GET_CODE (x) == REG)
7837         fprintf (file, "%s", reg_names[REGNO (x) + 3]);
7838       else if (GET_CODE (x) == MEM)
7839         {
7840           if (GET_CODE (XEXP (x, 0)) == PRE_INC
7841               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
7842             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 12));
7843           else
7844             output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
7845           if (small_data_operand (x, GET_MODE (x)))
7846             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
7847                      reg_names[SMALL_DATA_REG]);
7848         }
7849       return;
7850
7851       /* Print AltiVec or SPE memory operand.  */
7852     case 'y':
7853       {
7854         rtx tmp;
7855
7856         if (GET_CODE (x) != MEM)
7857           abort ();
7858
7859         tmp = XEXP (x, 0);
7860
7861         if (TARGET_SPE)
7862           {
7863             /* Handle [reg].  */
7864             if (GET_CODE (tmp) == REG)
7865               {
7866                 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
7867                 break;
7868               }
7869             /* Handle [reg+UIMM].  */
7870             else if (GET_CODE (tmp) == PLUS &&
7871                      GET_CODE (XEXP (tmp, 1)) == CONST_INT)
7872               {
7873                 int x;
7874
7875                 if (GET_CODE (XEXP (tmp, 0)) != REG)
7876                   abort ();
7877
7878                 x = INTVAL (XEXP (tmp, 1));
7879                 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
7880                 break;
7881               }
7882
7883             /* Fall through.  Must be [reg+reg].  */
7884           }
7885         if (GET_CODE (tmp) == REG)
7886           fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
7887         else if (GET_CODE (tmp) == PLUS && GET_CODE (XEXP (tmp, 1)) == REG)
7888           {
7889             if (REGNO (XEXP (tmp, 0)) == 0)
7890               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
7891                        reg_names[ REGNO (XEXP (tmp, 0)) ]);
7892             else
7893               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
7894                        reg_names[ REGNO (XEXP (tmp, 1)) ]);
7895           }
7896         else
7897           abort ();
7898         break;
7899       }
7900                             
7901     case 0:
7902       if (GET_CODE (x) == REG)
7903         fprintf (file, "%s", reg_names[REGNO (x)]);
7904       else if (GET_CODE (x) == MEM)
7905         {
7906           /* We need to handle PRE_INC and PRE_DEC here, since we need to
7907              know the width from the mode.  */
7908           if (GET_CODE (XEXP (x, 0)) == PRE_INC)
7909             fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
7910                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
7911           else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
7912             fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
7913                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
7914           else
7915             output_address (XEXP (x, 0));
7916         }
7917       else
7918         output_addr_const (file, x);
7919       return;
7920
7921     default:
7922       output_operand_lossage ("invalid %%xn code");
7923     }
7924 }
7925 \f
7926 /* Print the address of an operand.  */
7927
7928 void
7929 print_operand_address (file, x)
7930      FILE *file;
7931      rtx x;
7932 {
7933   if (GET_CODE (x) == REG)
7934     fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
7935   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
7936            || GET_CODE (x) == LABEL_REF)
7937     {
7938       output_addr_const (file, x);
7939       if (small_data_operand (x, GET_MODE (x)))
7940         fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
7941                  reg_names[SMALL_DATA_REG]);
7942       else if (TARGET_TOC)
7943         abort ();
7944     }
7945   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG)
7946     {
7947       if (REGNO (XEXP (x, 0)) == 0)
7948         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
7949                  reg_names[ REGNO (XEXP (x, 0)) ]);
7950       else
7951         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
7952                  reg_names[ REGNO (XEXP (x, 1)) ]);
7953     }
7954   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
7955     {
7956       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (XEXP (x, 1)));
7957       fprintf (file, "(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
7958     }
7959 #if TARGET_ELF
7960   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
7961            && CONSTANT_P (XEXP (x, 1)))
7962     {
7963       output_addr_const (file, XEXP (x, 1));
7964       fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
7965     }
7966 #endif
7967 #if TARGET_MACHO
7968   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
7969            && CONSTANT_P (XEXP (x, 1)))
7970     {
7971       fprintf (file, "lo16(");
7972       output_addr_const (file, XEXP (x, 1));
7973       fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
7974     }
7975 #endif
7976   else if (LEGITIMATE_CONSTANT_POOL_ADDRESS_P (x))
7977     {
7978       if (TARGET_AIX && (!TARGET_ELF || !TARGET_MINIMAL_TOC))
7979         {
7980           rtx contains_minus = XEXP (x, 1);
7981           rtx minus, symref;
7982           const char *name;
7983           
7984           /* Find the (minus (sym) (toc)) buried in X, and temporarily
7985              turn it into (sym) for output_addr_const.  */
7986           while (GET_CODE (XEXP (contains_minus, 0)) != MINUS)
7987             contains_minus = XEXP (contains_minus, 0);
7988
7989           minus = XEXP (contains_minus, 0);
7990           symref = XEXP (minus, 0);
7991           XEXP (contains_minus, 0) = symref;
7992           if (TARGET_ELF)
7993             {
7994               char *newname;
7995
7996               name = XSTR (symref, 0);
7997               newname = alloca (strlen (name) + sizeof ("@toc"));
7998               strcpy (newname, name);
7999               strcat (newname, "@toc");
8000               XSTR (symref, 0) = newname;
8001             }
8002           output_addr_const (file, XEXP (x, 1));
8003           if (TARGET_ELF)
8004             XSTR (symref, 0) = name;
8005           XEXP (contains_minus, 0) = minus;
8006         }
8007       else
8008         output_addr_const (file, XEXP (x, 1));
8009
8010       fprintf (file, "(%s)", reg_names[REGNO (XEXP (x, 0))]);
8011     }
8012   else
8013     abort ();
8014 }
8015 \f
8016 /* Target hook for assembling integer objects.  The powerpc version has
8017    to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
8018    is defined.  It also needs to handle DI-mode objects on 64-bit
8019    targets.  */
8020
8021 static bool
8022 rs6000_assemble_integer (x, size, aligned_p)
8023      rtx x;
8024      unsigned int size;
8025      int aligned_p;
8026 {
8027 #ifdef RELOCATABLE_NEEDS_FIXUP
8028   /* Special handling for SI values.  */
8029   if (size == 4 && aligned_p)
8030     {
8031       extern int in_toc_section PARAMS ((void));
8032       static int recurse = 0;
8033       
8034       /* For -mrelocatable, we mark all addresses that need to be fixed up
8035          in the .fixup section.  */
8036       if (TARGET_RELOCATABLE
8037           && !in_toc_section ()
8038           && !in_text_section ()
8039           && !recurse
8040           && GET_CODE (x) != CONST_INT
8041           && GET_CODE (x) != CONST_DOUBLE
8042           && CONSTANT_P (x))
8043         {
8044           char buf[256];
8045
8046           recurse = 1;
8047           ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
8048           fixuplabelno++;
8049           ASM_OUTPUT_LABEL (asm_out_file, buf);
8050           fprintf (asm_out_file, "\t.long\t(");
8051           output_addr_const (asm_out_file, x);
8052           fprintf (asm_out_file, ")@fixup\n");
8053           fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
8054           ASM_OUTPUT_ALIGN (asm_out_file, 2);
8055           fprintf (asm_out_file, "\t.long\t");
8056           assemble_name (asm_out_file, buf);
8057           fprintf (asm_out_file, "\n\t.previous\n");
8058           recurse = 0;
8059           return true;
8060         }
8061       /* Remove initial .'s to turn a -mcall-aixdesc function
8062          address into the address of the descriptor, not the function
8063          itself.  */
8064       else if (GET_CODE (x) == SYMBOL_REF
8065                && XSTR (x, 0)[0] == '.'
8066                && DEFAULT_ABI == ABI_AIX)
8067         {
8068           const char *name = XSTR (x, 0);
8069           while (*name == '.')
8070             name++;
8071
8072           fprintf (asm_out_file, "\t.long\t%s\n", name);
8073           return true;
8074         }
8075     }
8076 #endif /* RELOCATABLE_NEEDS_FIXUP */
8077   return default_assemble_integer (x, size, aligned_p);
8078 }
8079 \f
8080 enum rtx_code
8081 rs6000_reverse_condition (mode, code)
8082      enum machine_mode mode;
8083      enum rtx_code code;
8084 {
8085   /* Reversal of FP compares takes care -- an ordered compare
8086      becomes an unordered compare and vice versa.  */
8087   if (mode == CCFPmode)
8088     return reverse_condition_maybe_unordered (code);
8089   else
8090     return reverse_condition (code);
8091 }
8092
8093 /* Generate a compare for CODE.  Return a brand-new rtx that
8094    represents the result of the compare.  */
8095
8096 static rtx
8097 rs6000_generate_compare (code)
8098      enum rtx_code code;
8099 {
8100   enum machine_mode comp_mode;
8101   rtx compare_result;
8102
8103   if (rs6000_compare_fp_p)
8104     comp_mode = CCFPmode;
8105   else if (code == GTU || code == LTU
8106           || code == GEU || code == LEU)
8107     comp_mode = CCUNSmode;
8108   else
8109     comp_mode = CCmode;
8110
8111   /* First, the compare.  */
8112   compare_result = gen_reg_rtx (comp_mode);
8113
8114   /* SPE FP compare instructions on the GPRs.  Yuck!  */
8115   if ((TARGET_SPE && TARGET_HARD_FLOAT) && rs6000_compare_fp_p)
8116     {
8117       rtx cmp, or1, or2, or_result, compare_result2;
8118
8119       switch (code)
8120         {
8121         case EQ:
8122         case UNEQ:
8123         case NE:
8124         case LTGT:
8125           cmp = flag_unsafe_math_optimizations
8126             ? gen_tstsfeq_gpr (compare_result, rs6000_compare_op0,
8127                                rs6000_compare_op1)
8128             : gen_cmpsfeq_gpr (compare_result, rs6000_compare_op0,
8129                                rs6000_compare_op1);
8130           break;
8131         case GT:
8132         case GTU:
8133         case UNGT:
8134         case UNGE:
8135         case GE:
8136         case GEU:
8137           cmp = flag_unsafe_math_optimizations
8138             ? gen_tstsfgt_gpr (compare_result, rs6000_compare_op0,
8139                                rs6000_compare_op1)
8140             : gen_cmpsfgt_gpr (compare_result, rs6000_compare_op0,
8141                                rs6000_compare_op1);
8142           break;
8143         case LT:
8144         case LTU:
8145         case UNLT:
8146         case UNLE:
8147         case LE:
8148         case LEU:
8149           cmp = flag_unsafe_math_optimizations
8150             ? gen_tstsflt_gpr (compare_result, rs6000_compare_op0,
8151                                rs6000_compare_op1)
8152             : gen_cmpsflt_gpr (compare_result, rs6000_compare_op0,
8153                                rs6000_compare_op1);
8154           break;
8155         default:
8156           abort ();
8157         }
8158
8159       /* Synthesize LE and GE from LT/GT || EQ.  */
8160       if (code == LE || code == GE || code == LEU || code == GEU)
8161         {
8162           /* Synthesize GE/LE frome GT/LT || EQ.  */
8163
8164           emit_insn (cmp);
8165
8166           switch (code)
8167             {
8168             case LE: code = LT; break;
8169             case GE: code = GT; break;
8170             case LEU: code = LT; break;
8171             case GEU: code = GT; break;
8172             default: abort ();
8173             }
8174
8175           or1 = gen_reg_rtx (SImode);
8176           or2 = gen_reg_rtx (SImode);
8177           or_result = gen_reg_rtx (CCEQmode);
8178           compare_result2 = gen_reg_rtx (CCFPmode);
8179
8180           /* Do the EQ.  */
8181           cmp = flag_unsafe_math_optimizations
8182             ? gen_tstsfeq_gpr (compare_result2, rs6000_compare_op0,
8183                                rs6000_compare_op1)
8184             : gen_cmpsfeq_gpr (compare_result2, rs6000_compare_op0,
8185                                rs6000_compare_op1);
8186           emit_insn (cmp);
8187
8188           /* The MC8540 FP compare instructions set the CR bits
8189              differently than other PPC compare instructions.  For
8190              that matter, there is no generic test instruction, but a
8191              testgt, testlt, and testeq.  For a true condition, bit 2
8192              is set (x1xx) in the CR.  Following the traditional CR
8193              values:
8194
8195              LT    GT    EQ    OV
8196              bit3  bit2  bit1  bit0
8197
8198              ... bit 2 would be a GT CR alias, so later on we
8199              look in the GT bits for the branch instructins.
8200              However, we must be careful to emit correct RTL in
8201              the meantime, so optimizations don't get confused.  */
8202
8203           or1 = gen_rtx (NE, SImode, compare_result, const0_rtx);
8204           or2 = gen_rtx (NE, SImode, compare_result2, const0_rtx);
8205
8206           /* OR them together.  */
8207           cmp = gen_rtx_SET (VOIDmode, or_result,
8208                              gen_rtx_COMPARE (CCEQmode,
8209                                               gen_rtx_IOR (SImode, or1, or2),
8210                                               const_true_rtx));
8211           compare_result = or_result;
8212           code = EQ;
8213         }
8214       else
8215         {
8216           /* We only care about 1 bit (x1xx), so map everything to NE to
8217              maintain rtl sanity.  We'll get to the right bit (x1xx) at
8218              code output time.  */
8219           if (code == NE || code == LTGT)
8220             /* Do the inverse here because we have no cmpne
8221                instruction.  We use the cmpeq instruction and expect
8222                to get a 0 instead.  */
8223             code = EQ;
8224           else
8225             code = NE;
8226         }
8227
8228       emit_insn (cmp);
8229     }
8230   else
8231     emit_insn (gen_rtx_SET (VOIDmode, compare_result,
8232                             gen_rtx_COMPARE (comp_mode,
8233                                              rs6000_compare_op0, 
8234                                              rs6000_compare_op1)));
8235   
8236   /* Some kinds of FP comparisons need an OR operation;
8237      except for flag_unsafe_math_optimizations we don't bother.  */
8238   if (rs6000_compare_fp_p
8239       && ! flag_unsafe_math_optimizations
8240       && ! (TARGET_HARD_FLOAT && TARGET_SPE)
8241       && (code == LE || code == GE
8242           || code == UNEQ || code == LTGT
8243           || code == UNGT || code == UNLT))
8244     {
8245       enum rtx_code or1, or2;
8246       rtx or1_rtx, or2_rtx, compare2_rtx;
8247       rtx or_result = gen_reg_rtx (CCEQmode);
8248       
8249       switch (code)
8250         {
8251         case LE: or1 = LT;  or2 = EQ;  break;
8252         case GE: or1 = GT;  or2 = EQ;  break;
8253         case UNEQ: or1 = UNORDERED;  or2 = EQ;  break;
8254         case LTGT: or1 = LT;  or2 = GT;  break;
8255         case UNGT: or1 = UNORDERED;  or2 = GT;  break;
8256         case UNLT: or1 = UNORDERED;  or2 = LT;  break;
8257         default:  abort ();
8258         }
8259       validate_condition_mode (or1, comp_mode);
8260       validate_condition_mode (or2, comp_mode);
8261       or1_rtx = gen_rtx (or1, SImode, compare_result, const0_rtx);
8262       or2_rtx = gen_rtx (or2, SImode, compare_result, const0_rtx);
8263       compare2_rtx = gen_rtx_COMPARE (CCEQmode,
8264                                       gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
8265                                       const_true_rtx);
8266       emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
8267
8268       compare_result = or_result;
8269       code = EQ;
8270     }
8271
8272   validate_condition_mode (code, GET_MODE (compare_result));
8273   
8274   return gen_rtx (code, VOIDmode, compare_result, const0_rtx);
8275 }
8276
8277
8278 /* Emit the RTL for an sCOND pattern.  */
8279
8280 void
8281 rs6000_emit_sCOND (code, result)
8282      enum rtx_code code;
8283      rtx result;
8284 {
8285   rtx condition_rtx;
8286   enum machine_mode op_mode;
8287
8288   condition_rtx = rs6000_generate_compare (code);
8289
8290   op_mode = GET_MODE (rs6000_compare_op0);
8291   if (op_mode == VOIDmode)
8292     op_mode = GET_MODE (rs6000_compare_op1);
8293
8294   if (TARGET_POWERPC64 && (op_mode == DImode || rs6000_compare_fp_p))
8295     {
8296       PUT_MODE (condition_rtx, DImode);
8297       convert_move (result, condition_rtx, 0);
8298     }
8299   else
8300     {
8301       PUT_MODE (condition_rtx, SImode);
8302       emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
8303     }
8304 }
8305
8306 /* Emit a branch of kind CODE to location LOC.  */
8307
8308 void
8309 rs6000_emit_cbranch (code, loc)
8310      enum rtx_code code;
8311      rtx loc;
8312 {
8313   rtx condition_rtx, loc_ref;
8314
8315   condition_rtx = rs6000_generate_compare (code);
8316   loc_ref = gen_rtx_LABEL_REF (VOIDmode, loc);
8317   emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
8318                                gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
8319                                                      loc_ref, pc_rtx)));
8320 }
8321
8322 /* Return the string to output a conditional branch to LABEL, which is
8323    the operand number of the label, or -1 if the branch is really a
8324    conditional return.  
8325
8326    OP is the conditional expression.  XEXP (OP, 0) is assumed to be a
8327    condition code register and its mode specifies what kind of
8328    comparison we made.
8329
8330    REVERSED is non-zero if we should reverse the sense of the comparison.
8331
8332    INSN is the insn.  */
8333
8334 char *
8335 output_cbranch (op, label, reversed, insn)
8336      rtx op;
8337      const char * label;
8338      int reversed;
8339      rtx insn;
8340 {
8341   static char string[64];
8342   enum rtx_code code = GET_CODE (op);
8343   rtx cc_reg = XEXP (op, 0);
8344   enum machine_mode mode = GET_MODE (cc_reg);
8345   int cc_regno = REGNO (cc_reg) - CR0_REGNO;
8346   int need_longbranch = label != NULL && get_attr_length (insn) == 8;
8347   int really_reversed = reversed ^ need_longbranch;
8348   char *s = string;
8349   const char *ccode;
8350   const char *pred;
8351   rtx note;
8352
8353   validate_condition_mode (code, mode);
8354
8355   /* Work out which way this really branches.  We could use
8356      reverse_condition_maybe_unordered here always but this
8357      makes the resulting assembler clearer.  */
8358   if (really_reversed)
8359     code = rs6000_reverse_condition (mode, code);
8360
8361   if ((TARGET_SPE && TARGET_HARD_FLOAT) && mode == CCFPmode)
8362     {
8363       /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
8364          to the GT bit.  */
8365       if (code == EQ)
8366         /* Opposite of GT.  */
8367         code = UNLE;
8368       else if (code == NE)
8369         code = GT;
8370       else
8371         abort ();
8372     }
8373
8374   switch (code)
8375     {
8376       /* Not all of these are actually distinct opcodes, but
8377          we distinguish them for clarity of the resulting assembler.  */
8378     case NE: case LTGT:
8379       ccode = "ne"; break;
8380     case EQ: case UNEQ:
8381       ccode = "eq"; break;
8382     case GE: case GEU: 
8383       ccode = "ge"; break;
8384     case GT: case GTU: case UNGT: 
8385       ccode = "gt"; break;
8386     case LE: case LEU: 
8387       ccode = "le"; break;
8388     case LT: case LTU: case UNLT: 
8389       ccode = "lt"; break;
8390     case UNORDERED: ccode = "un"; break;
8391     case ORDERED: ccode = "nu"; break;
8392     case UNGE: ccode = "nl"; break;
8393     case UNLE: ccode = "ng"; break;
8394     default:
8395       abort ();
8396     }
8397   
8398   /* Maybe we have a guess as to how likely the branch is.  
8399      The old mnemonics don't have a way to specify this information.  */
8400   note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
8401   if (note != NULL_RTX)
8402     {
8403       /* PROB is the difference from 50%.  */
8404       int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
8405       
8406       /* For branches that are very close to 50%, assume not-taken.  */
8407       if (abs (prob) > REG_BR_PROB_BASE / 20
8408           && ((prob > 0) ^ need_longbranch))
8409         pred = "+";
8410       else
8411         pred = "-";
8412     }
8413   else
8414     pred = "";
8415
8416   if (label == NULL)
8417     s += sprintf (s, "{b%sr|b%slr%s} ", ccode, ccode, pred);
8418   else
8419     s += sprintf (s, "{b%s|b%s%s} ", ccode, ccode, pred);
8420
8421   /* We need to escape any '%' characters in the reg_names string.
8422      Assume they'd only be the first character...  */
8423   if (reg_names[cc_regno + CR0_REGNO][0] == '%')
8424     *s++ = '%';
8425   s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
8426
8427   if (label != NULL)
8428     {
8429       /* If the branch distance was too far, we may have to use an
8430          unconditional branch to go the distance.  */
8431       if (need_longbranch)
8432         s += sprintf (s, ",$+8\n\tb %s", label);
8433       else
8434         s += sprintf (s, ",%s", label);
8435     }
8436
8437   return string;
8438 }
8439
8440 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
8441    operands of the last comparison is nonzero/true, FALSE_COND if it
8442    is zero/false.  Return 0 if the hardware has no such operation.  */
8443
8444 int
8445 rs6000_emit_cmove (dest, op, true_cond, false_cond)
8446      rtx dest;
8447      rtx op;
8448      rtx true_cond;
8449      rtx false_cond;
8450 {
8451   enum rtx_code code = GET_CODE (op);
8452   rtx op0 = rs6000_compare_op0;
8453   rtx op1 = rs6000_compare_op1;
8454   REAL_VALUE_TYPE c1;
8455   enum machine_mode compare_mode = GET_MODE (op0);
8456   enum machine_mode result_mode = GET_MODE (dest);
8457   rtx temp;
8458
8459   /* These modes should always match. */
8460   if (GET_MODE (op1) != compare_mode
8461       /* In the isel case however, we can use a compare immediate, so
8462          op1 may be a small constant.  */
8463       && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
8464     return 0;
8465   if (GET_MODE (true_cond) != result_mode)
8466     return 0;
8467   if (GET_MODE (false_cond) != result_mode)
8468     return 0;
8469
8470   /* First, work out if the hardware can do this at all, or
8471      if it's too slow...  */
8472   if (! rs6000_compare_fp_p)
8473     {
8474       if (TARGET_ISEL)
8475         return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
8476       return 0;
8477     }
8478
8479   /* Eliminate half of the comparisons by switching operands, this
8480      makes the remaining code simpler.  */
8481   if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
8482       || code == LTGT || code == LT)
8483     {
8484       code = reverse_condition_maybe_unordered (code);
8485       temp = true_cond;
8486       true_cond = false_cond;
8487       false_cond = temp;
8488     }
8489
8490   /* UNEQ and LTGT take four instructions for a comparison with zero,
8491      it'll probably be faster to use a branch here too.  */
8492   if (code == UNEQ)
8493     return 0;
8494   
8495   if (GET_CODE (op1) == CONST_DOUBLE)
8496     REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
8497     
8498   /* We're going to try to implement comparions by performing
8499      a subtract, then comparing against zero.  Unfortunately,
8500      Inf - Inf is NaN which is not zero, and so if we don't
8501      know that the the operand is finite and the comparison
8502      would treat EQ different to UNORDERED, we can't do it.  */
8503   if (! flag_unsafe_math_optimizations
8504       && code != GT && code != UNGE
8505       && (GET_CODE (op1) != CONST_DOUBLE || target_isinf (c1))
8506       /* Constructs of the form (a OP b ? a : b) are safe.  */
8507       && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
8508           || (! rtx_equal_p (op0, true_cond) 
8509               && ! rtx_equal_p (op1, true_cond))))
8510     return 0;
8511   /* At this point we know we can use fsel.  */
8512
8513   /* Reduce the comparison to a comparison against zero.  */
8514   temp = gen_reg_rtx (compare_mode);
8515   emit_insn (gen_rtx_SET (VOIDmode, temp,
8516                           gen_rtx_MINUS (compare_mode, op0, op1)));
8517   op0 = temp;
8518   op1 = CONST0_RTX (compare_mode);
8519
8520   /* If we don't care about NaNs we can reduce some of the comparisons
8521      down to faster ones.  */
8522   if (flag_unsafe_math_optimizations)
8523     switch (code)
8524       {
8525       case GT:
8526         code = LE;
8527         temp = true_cond;
8528         true_cond = false_cond;
8529         false_cond = temp;
8530         break;
8531       case UNGE:
8532         code = GE;
8533         break;
8534       case UNEQ:
8535         code = EQ;
8536         break;
8537       default:
8538         break;
8539       }
8540
8541   /* Now, reduce everything down to a GE.  */
8542   switch (code)
8543     {
8544     case GE:
8545       break;
8546
8547     case LE:
8548       temp = gen_reg_rtx (compare_mode);
8549       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
8550       op0 = temp;
8551       break;
8552
8553     case ORDERED:
8554       temp = gen_reg_rtx (compare_mode);
8555       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
8556       op0 = temp;
8557       break;
8558
8559     case EQ:
8560       temp = gen_reg_rtx (compare_mode);
8561       emit_insn (gen_rtx_SET (VOIDmode, temp, 
8562                               gen_rtx_NEG (compare_mode,
8563                                            gen_rtx_ABS (compare_mode, op0))));
8564       op0 = temp;
8565       break;
8566
8567     case UNGE:
8568       temp = gen_reg_rtx (result_mode);
8569       emit_insn (gen_rtx_SET (VOIDmode, temp,
8570                               gen_rtx_IF_THEN_ELSE (result_mode,
8571                                                     gen_rtx_GE (VOIDmode,
8572                                                                 op0, op1),
8573                                                     true_cond, false_cond)));
8574       false_cond = temp;
8575       true_cond = false_cond;
8576
8577       temp = gen_reg_rtx (compare_mode);
8578       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
8579       op0 = temp;
8580       break;
8581
8582     case GT:
8583       temp = gen_reg_rtx (result_mode);
8584       emit_insn (gen_rtx_SET (VOIDmode, temp,
8585                               gen_rtx_IF_THEN_ELSE (result_mode, 
8586                                                     gen_rtx_GE (VOIDmode,
8587                                                                 op0, op1),
8588                                                     true_cond, false_cond)));
8589       true_cond = temp;
8590       false_cond = true_cond;
8591
8592       temp = gen_reg_rtx (compare_mode);
8593       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
8594       op0 = temp;
8595       break;
8596
8597     default:
8598       abort ();
8599     }
8600
8601   emit_insn (gen_rtx_SET (VOIDmode, dest,
8602                           gen_rtx_IF_THEN_ELSE (result_mode,
8603                                                 gen_rtx_GE (VOIDmode,
8604                                                             op0, op1),
8605                                                 true_cond, false_cond)));
8606   return 1;
8607 }
8608
8609 /* Same as above, but for ints (isel).  */
8610
8611 static int
8612 rs6000_emit_int_cmove (dest, op, true_cond, false_cond)
8613      rtx dest;
8614      rtx op;
8615      rtx true_cond;
8616      rtx false_cond;
8617 {
8618   rtx condition_rtx, cr;
8619
8620   /* All isel implementations thus far are 32-bits.  */
8621   if (GET_MODE (rs6000_compare_op0) != SImode)
8622     return 0;
8623
8624   /* We still have to do the compare, because isel doesn't do a
8625      compare, it just looks at the CRx bits set by a previous compare
8626      instruction.  */
8627   condition_rtx = rs6000_generate_compare (GET_CODE (op));
8628   cr = XEXP (condition_rtx, 0);
8629
8630   if (GET_MODE (cr) == CCmode)
8631     emit_insn (gen_isel_signed (dest, condition_rtx,
8632                                 true_cond, false_cond, cr));
8633   else
8634     emit_insn (gen_isel_unsigned (dest, condition_rtx,
8635                                   true_cond, false_cond, cr));
8636
8637   return 1;
8638 }
8639
8640 const char *
8641 output_isel (operands)
8642      rtx *operands;
8643 {
8644   enum rtx_code code;
8645
8646   code = GET_CODE (operands[1]);
8647   if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
8648     {
8649       PUT_CODE (operands[1], reverse_condition (code));
8650       return "isel %0,%3,%2,%j1";
8651     }
8652   else
8653     return "isel %0,%2,%3,%j1";
8654 }
8655
8656 void
8657 rs6000_emit_minmax (dest, code, op0, op1)
8658      rtx dest;
8659      enum rtx_code code;
8660      rtx op0;
8661      rtx op1;
8662 {
8663   enum machine_mode mode = GET_MODE (op0);
8664   enum rtx_code c;
8665   rtx target;
8666
8667   if (code == SMAX || code == SMIN)
8668     c = GE;
8669   else
8670     c = GEU;
8671
8672   if (code == SMAX || code == UMAX)
8673     target = emit_conditional_move (dest, c, op0, op1, mode, 
8674                                     op0, op1, mode, 0);
8675   else
8676     target = emit_conditional_move (dest, c, op0, op1, mode, 
8677                                     op1, op0, mode, 0);
8678   if (target == NULL_RTX)
8679     abort ();
8680   if (target != dest)
8681     emit_move_insn (dest, target);
8682 }
8683 \f
8684 /* This page contains routines that are used to determine what the
8685    function prologue and epilogue code will do and write them out.  */
8686
8687 /* Return the first fixed-point register that is required to be
8688    saved. 32 if none.  */
8689
8690 int
8691 first_reg_to_save ()
8692 {
8693   int first_reg;
8694
8695   /* Find lowest numbered live register.  */
8696   for (first_reg = 13; first_reg <= 31; first_reg++)
8697     if (regs_ever_live[first_reg] 
8698         && (! call_used_regs[first_reg]
8699             || (first_reg == RS6000_PIC_OFFSET_TABLE_REGNUM
8700                 && ((DEFAULT_ABI == ABI_V4 && flag_pic == 1)
8701                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic)))))
8702       break;
8703
8704 #if TARGET_MACHO
8705   if (flag_pic && current_function_uses_pic_offset_table &&
8706       (first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM))
8707     return RS6000_PIC_OFFSET_TABLE_REGNUM;
8708 #endif
8709
8710   return first_reg;
8711 }
8712
8713 /* Similar, for FP regs.  */
8714
8715 int
8716 first_fp_reg_to_save ()
8717 {
8718   int first_reg;
8719
8720   /* Find lowest numbered live register.  */
8721   for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
8722     if (regs_ever_live[first_reg])
8723       break;
8724
8725   return first_reg;
8726 }
8727
8728 /* Similar, for AltiVec regs.  */
8729
8730 static int
8731 first_altivec_reg_to_save ()
8732 {
8733   int i;
8734
8735   /* Stack frame remains as is unless we are in AltiVec ABI.  */
8736   if (! TARGET_ALTIVEC_ABI)
8737     return LAST_ALTIVEC_REGNO + 1;
8738
8739   /* Find lowest numbered live register.  */
8740   for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
8741     if (regs_ever_live[i])
8742       break;
8743
8744   return i;
8745 }
8746
8747 /* Return a 32-bit mask of the AltiVec registers we need to set in
8748    VRSAVE.  Bit n of the return value is 1 if Vn is live.  The MSB in
8749    the 32-bit word is 0.  */
8750
8751 static unsigned int
8752 compute_vrsave_mask ()
8753 {
8754   unsigned int i, mask = 0;
8755
8756   /* First, find out if we use _any_ altivec registers.  */
8757   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
8758     if (regs_ever_live[i])
8759       mask |= ALTIVEC_REG_BIT (i);
8760
8761   if (mask == 0)
8762     return mask;
8763
8764   /* Next, add all registers that are call-clobbered.  We do this
8765      because post-reload register optimizers such as regrename_optimize
8766      may choose to use them.  They never change the register class
8767      chosen by reload, so cannot create new uses of altivec registers
8768      if there were none before, so the early exit above is safe.  */
8769   /* ??? Alternately, we could define HARD_REGNO_RENAME_OK to disallow
8770      altivec registers not saved in the mask, which might well make the
8771      adjustments below more effective in eliding the save/restore of
8772      VRSAVE in small functions.  */
8773   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
8774     if (call_used_regs[i])
8775       mask |= ALTIVEC_REG_BIT (i);
8776
8777   /* Next, remove the argument registers from the set.  These must
8778      be in the VRSAVE mask set by the caller, so we don't need to add
8779      them in again.  More importantly, the mask we compute here is
8780      used to generate CLOBBERs in the set_vrsave insn, and we do not
8781      wish the argument registers to die.  */
8782   for (i = cfun->args_info.vregno; i >= ALTIVEC_ARG_MIN_REG; --i)
8783     mask &= ~ALTIVEC_REG_BIT (i);
8784
8785   /* Similarly, remove the return value from the set.  */
8786   {
8787     bool yes = false;
8788     diddle_return_value (is_altivec_return_reg, &yes);
8789     if (yes)
8790       mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
8791   }
8792
8793   return mask;
8794 }
8795
8796 static void
8797 is_altivec_return_reg (reg, xyes)
8798      rtx reg;
8799      void *xyes;
8800 {
8801   bool *yes = (bool *) xyes;
8802   if (REGNO (reg) == ALTIVEC_ARG_RETURN)
8803     *yes = true;
8804 }
8805
8806 \f
8807 /* Calculate the stack information for the current function.  This is
8808    complicated by having two separate calling sequences, the AIX calling
8809    sequence and the V.4 calling sequence.
8810
8811    AIX (and Darwin/Mac OS X) stack frames look like:
8812                                                           32-bit  64-bit
8813         SP----> +---------------------------------------+
8814                 | back chain to caller                  | 0       0
8815                 +---------------------------------------+
8816                 | saved CR                              | 4       8 (8-11)
8817                 +---------------------------------------+
8818                 | saved LR                              | 8       16
8819                 +---------------------------------------+
8820                 | reserved for compilers                | 12      24
8821                 +---------------------------------------+
8822                 | reserved for binders                  | 16      32
8823                 +---------------------------------------+
8824                 | saved TOC pointer                     | 20      40
8825                 +---------------------------------------+
8826                 | Parameter save area (P)               | 24      48
8827                 +---------------------------------------+
8828                 | Alloca space (A)                      | 24+P    etc.
8829                 +---------------------------------------+
8830                 | Local variable space (L)              | 24+P+A
8831                 +---------------------------------------+
8832                 | Float/int conversion temporary (X)    | 24+P+A+L
8833                 +---------------------------------------+
8834                 | Save area for AltiVec registers (W)   | 24+P+A+L+X
8835                 +---------------------------------------+
8836                 | AltiVec alignment padding (Y)         | 24+P+A+L+X+W
8837                 +---------------------------------------+
8838                 | Save area for VRSAVE register (Z)     | 24+P+A+L+X+W+Y
8839                 +---------------------------------------+
8840                 | Save area for GP registers (G)        | 24+P+A+X+L+X+W+Y+Z
8841                 +---------------------------------------+
8842                 | Save area for FP registers (F)        | 24+P+A+X+L+X+W+Y+Z+G
8843                 +---------------------------------------+
8844         old SP->| back chain to caller's caller         |
8845                 +---------------------------------------+
8846
8847    The required alignment for AIX configurations is two words (i.e., 8
8848    or 16 bytes).
8849
8850
8851    V.4 stack frames look like:
8852
8853         SP----> +---------------------------------------+
8854                 | back chain to caller                  | 0
8855                 +---------------------------------------+
8856                 | caller's saved LR                     | 4
8857                 +---------------------------------------+
8858                 | Parameter save area (P)               | 8
8859                 +---------------------------------------+
8860                 | Alloca space (A)                      | 8+P
8861                 +---------------------------------------+    
8862                 | Varargs save area (V)                 | 8+P+A
8863                 +---------------------------------------+    
8864                 | Local variable space (L)              | 8+P+A+V
8865                 +---------------------------------------+    
8866                 | Float/int conversion temporary (X)    | 8+P+A+V+L
8867                 +---------------------------------------+
8868                 | Save area for AltiVec registers (W)   | 8+P+A+V+L+X
8869                 +---------------------------------------+
8870                 | AltiVec alignment padding (Y)         | 8+P+A+V+L+X+W
8871                 +---------------------------------------+
8872                 | Save area for VRSAVE register (Z)     | 8+P+A+V+L+X+W+Y
8873                 +---------------------------------------+
8874                 | SPE: area for 64-bit GP registers     |
8875                 +---------------------------------------+
8876                 | SPE alignment padding                 |
8877                 +---------------------------------------+
8878                 | saved CR (C)                          | 8+P+A+V+L+X+W+Y+Z
8879                 +---------------------------------------+    
8880                 | Save area for GP registers (G)        | 8+P+A+V+L+X+W+Y+Z+C
8881                 +---------------------------------------+    
8882                 | Save area for FP registers (F)        | 8+P+A+V+L+X+W+Y+Z+C+G
8883                 +---------------------------------------+
8884         old SP->| back chain to caller's caller         |
8885                 +---------------------------------------+
8886
8887    The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
8888    given.  (But note below and in sysv4.h that we require only 8 and
8889    may round up the size of our stack frame anyways.  The historical
8890    reason is early versions of powerpc-linux which didn't properly
8891    align the stack at program startup.  A happy side-effect is that
8892    -mno-eabi libraries can be used with -meabi programs.)
8893
8894    The EABI configuration defaults to the V.4 layout, unless
8895    -mcall-aix is used, in which case the AIX layout is used.  However,
8896    the stack alignment requirements may differ.  If -mno-eabi is not
8897    given, the required stack alignment is 8 bytes; if -mno-eabi is
8898    given, the required alignment is 16 bytes.  (But see V.4 comment
8899    above.)  */
8900
8901 #ifndef ABI_STACK_BOUNDARY
8902 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
8903 #endif
8904
8905 rs6000_stack_t *
8906 rs6000_stack_info ()
8907 {
8908   static rs6000_stack_t info, zero_info;
8909   rs6000_stack_t *info_ptr = &info;
8910   int reg_size = TARGET_POWERPC64 ? 8 : 4;
8911   enum rs6000_abi abi;
8912   int ehrd_size;
8913   int total_raw_size;
8914
8915   /* Zero all fields portably.  */
8916   info = zero_info;
8917
8918   /* Select which calling sequence.  */
8919   info_ptr->abi = abi = DEFAULT_ABI;
8920
8921   /* Calculate which registers need to be saved & save area size.  */
8922   info_ptr->first_gp_reg_save = first_reg_to_save ();
8923   /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM, 
8924      even if it currently looks like we won't.  */
8925   if (((TARGET_TOC && TARGET_MINIMAL_TOC)
8926        || (flag_pic == 1 && abi == ABI_V4)
8927        || (flag_pic && abi == ABI_DARWIN))
8928       && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
8929     info_ptr->gp_size = reg_size * (32 - RS6000_PIC_OFFSET_TABLE_REGNUM);
8930   else
8931     info_ptr->gp_size = reg_size * (32 - info_ptr->first_gp_reg_save);
8932
8933   /* For the SPE, we have an additional upper 32-bits on each GPR.
8934      Ideally we should save the entire 64-bits only when the upper
8935      half is used in SIMD instructions.  Since we only record
8936      registers live (not the size they are used in), this proves
8937      difficult because we'd have to traverse the instruction chain at
8938      the right time, taking reload into account.  This is a real pain,
8939      so we opt to save the GPRs in 64-bits always.  Anyone overly
8940      concerned with frame size can fix this.  ;-).
8941
8942      So... since we save all GPRs (except the SP) in 64-bits, the
8943      traditional GP save area will be empty.  */
8944   if (TARGET_SPE_ABI)
8945     info_ptr->gp_size = 0;
8946
8947   info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
8948   info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
8949
8950   info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
8951   info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
8952                                  - info_ptr->first_altivec_reg_save);
8953
8954   /* Does this function call anything?  */
8955   info_ptr->calls_p = (! current_function_is_leaf
8956                        || cfun->machine->ra_needs_full_frame);
8957
8958   /* Determine if we need to save the link register.  */
8959   if (rs6000_ra_ever_killed ()
8960       || (DEFAULT_ABI == ABI_AIX && current_function_profile)
8961 #ifdef TARGET_RELOCATABLE
8962       || (TARGET_RELOCATABLE && (get_pool_size () != 0))
8963 #endif
8964       || (info_ptr->first_fp_reg_save != 64
8965           && !FP_SAVE_INLINE (info_ptr->first_fp_reg_save))
8966       || info_ptr->first_altivec_reg_save <= LAST_ALTIVEC_REGNO
8967       || (abi == ABI_V4 && current_function_calls_alloca)
8968       || (DEFAULT_ABI == ABI_DARWIN
8969           && flag_pic
8970           && current_function_uses_pic_offset_table)
8971       || info_ptr->calls_p)
8972     {
8973       info_ptr->lr_save_p = 1;
8974       regs_ever_live[LINK_REGISTER_REGNUM] = 1;
8975     }
8976
8977   /* Determine if we need to save the condition code registers.  */
8978   if (regs_ever_live[CR2_REGNO] 
8979       || regs_ever_live[CR3_REGNO]
8980       || regs_ever_live[CR4_REGNO])
8981     {
8982       info_ptr->cr_save_p = 1;
8983       if (abi == ABI_V4)
8984         info_ptr->cr_size = reg_size;
8985     }
8986
8987   /* If the current function calls __builtin_eh_return, then we need
8988      to allocate stack space for registers that will hold data for
8989      the exception handler.  */
8990   if (current_function_calls_eh_return)
8991     {
8992       unsigned int i;
8993       for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
8994         continue;
8995
8996       /* SPE saves EH registers in 64-bits.  */
8997       ehrd_size = i * (TARGET_SPE_ABI ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
8998     }
8999   else
9000     ehrd_size = 0;
9001
9002   /* Determine various sizes.  */
9003   info_ptr->reg_size     = reg_size;
9004   info_ptr->fixed_size   = RS6000_SAVE_AREA;
9005   info_ptr->varargs_size = RS6000_VARARGS_AREA;
9006   info_ptr->vars_size    = RS6000_ALIGN (get_frame_size (), 8);
9007   info_ptr->parm_size    = RS6000_ALIGN (current_function_outgoing_args_size,
9008                                          8);
9009
9010   if (TARGET_SPE_ABI)
9011     info_ptr->spe_gp_size = 8 * (32 - info_ptr->first_gp_reg_save);
9012   else
9013     info_ptr->spe_gp_size = 0;
9014
9015   if (TARGET_ALTIVEC_ABI && TARGET_ALTIVEC_VRSAVE)
9016     {
9017       info_ptr->vrsave_mask = compute_vrsave_mask ();
9018       info_ptr->vrsave_size  = info_ptr->vrsave_mask ? 4 : 0;
9019     }
9020   else
9021     {
9022       info_ptr->vrsave_mask = 0;
9023       info_ptr->vrsave_size = 0;
9024     }
9025
9026   /* Calculate the offsets.  */
9027   switch (abi)
9028     {
9029     case ABI_NONE:
9030     default:
9031       abort ();
9032
9033     case ABI_AIX:
9034     case ABI_AIX_NODESC:
9035     case ABI_DARWIN:
9036       info_ptr->fp_save_offset   = - info_ptr->fp_size;
9037       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
9038
9039       if (TARGET_ALTIVEC_ABI)
9040         {
9041           info_ptr->vrsave_save_offset
9042             = info_ptr->gp_save_offset - info_ptr->vrsave_size;
9043
9044           /* Align stack so vector save area is on a quadword boundary.  */
9045           if (info_ptr->altivec_size != 0)
9046             info_ptr->altivec_padding_size
9047               = 16 - (-info_ptr->vrsave_save_offset % 16);
9048           else
9049             info_ptr->altivec_padding_size = 0;
9050
9051           info_ptr->altivec_save_offset
9052             = info_ptr->vrsave_save_offset
9053             - info_ptr->altivec_padding_size
9054             - info_ptr->altivec_size;
9055
9056           /* Adjust for AltiVec case.  */
9057           info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
9058         }
9059       else
9060         info_ptr->ehrd_offset      = info_ptr->gp_save_offset - ehrd_size;
9061       info_ptr->cr_save_offset   = reg_size; /* first word when 64-bit.  */
9062       info_ptr->lr_save_offset   = 2*reg_size;
9063       break;
9064
9065     case ABI_V4:
9066       info_ptr->fp_save_offset   = - info_ptr->fp_size;
9067       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
9068       info_ptr->cr_save_offset   = info_ptr->gp_save_offset - info_ptr->cr_size;
9069
9070       if (TARGET_SPE_ABI)
9071       {
9072         /* Align stack so SPE GPR save area is aligned on a
9073            double-word boundary.  */
9074         if (info_ptr->spe_gp_size != 0)
9075           info_ptr->spe_padding_size
9076             = 8 - (-info_ptr->cr_save_offset % 8);
9077         else
9078           info_ptr->spe_padding_size = 0;
9079
9080         info_ptr->spe_gp_save_offset
9081           = info_ptr->cr_save_offset
9082           - info_ptr->spe_padding_size
9083           - info_ptr->spe_gp_size;
9084
9085         /* Adjust for SPE case.  */
9086         info_ptr->toc_save_offset
9087           = info_ptr->spe_gp_save_offset - info_ptr->toc_size;
9088       }
9089       else if (TARGET_ALTIVEC_ABI)
9090         {
9091           info_ptr->vrsave_save_offset
9092             = info_ptr->cr_save_offset - info_ptr->vrsave_size;
9093
9094           /* Align stack so vector save area is on a quadword boundary.  */
9095           if (info_ptr->altivec_size != 0)
9096             info_ptr->altivec_padding_size
9097               = 16 - (-info_ptr->vrsave_save_offset % 16);
9098           else
9099             info_ptr->altivec_padding_size = 0;
9100
9101           info_ptr->altivec_save_offset
9102             = info_ptr->vrsave_save_offset
9103             - info_ptr->altivec_padding_size
9104             - info_ptr->altivec_size;
9105
9106           /* Adjust for AltiVec case.  */
9107           info_ptr->toc_save_offset
9108             = info_ptr->altivec_save_offset - info_ptr->toc_size;
9109         }
9110       else
9111         info_ptr->toc_save_offset  = info_ptr->cr_save_offset - info_ptr->toc_size;
9112       info_ptr->ehrd_offset      = info_ptr->toc_save_offset - ehrd_size;
9113       info_ptr->lr_save_offset   = reg_size;
9114       break;
9115     }
9116
9117   info_ptr->save_size    = RS6000_ALIGN (info_ptr->fp_size
9118                                          + info_ptr->gp_size
9119                                          + info_ptr->altivec_size
9120                                          + info_ptr->altivec_padding_size
9121                                          + info_ptr->vrsave_size
9122                                          + info_ptr->spe_gp_size
9123                                          + info_ptr->spe_padding_size
9124                                          + ehrd_size
9125                                          + info_ptr->cr_size
9126                                          + info_ptr->lr_size
9127                                          + info_ptr->vrsave_size
9128                                          + info_ptr->toc_size,
9129                                          (TARGET_ALTIVEC_ABI || ABI_DARWIN)
9130                                          ? 16 : 8);
9131
9132   total_raw_size         = (info_ptr->vars_size
9133                             + info_ptr->parm_size
9134                             + info_ptr->save_size
9135                             + info_ptr->varargs_size
9136                             + info_ptr->fixed_size);
9137
9138   info_ptr->total_size =
9139     RS6000_ALIGN (total_raw_size, ABI_STACK_BOUNDARY / BITS_PER_UNIT);
9140
9141   /* Determine if we need to allocate any stack frame:
9142
9143      For AIX we need to push the stack if a frame pointer is needed
9144      (because the stack might be dynamically adjusted), if we are
9145      debugging, if we make calls, or if the sum of fp_save, gp_save,
9146      and local variables are more than the space needed to save all
9147      non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
9148      + 18*8 = 288 (GPR13 reserved).
9149
9150      For V.4 we don't have the stack cushion that AIX uses, but assume
9151      that the debugger can handle stackless frames.  */
9152
9153   if (info_ptr->calls_p)
9154     info_ptr->push_p = 1;
9155
9156   else if (abi == ABI_V4)
9157     info_ptr->push_p = total_raw_size > info_ptr->fixed_size;
9158
9159   else
9160     info_ptr->push_p = (frame_pointer_needed
9161                         || (abi != ABI_DARWIN && write_symbols != NO_DEBUG)
9162                         || ((total_raw_size - info_ptr->fixed_size)
9163                             > (TARGET_32BIT ? 220 : 288)));
9164
9165   /* Zero offsets if we're not saving those registers.  */
9166   if (info_ptr->fp_size == 0)
9167     info_ptr->fp_save_offset = 0;
9168
9169   if (info_ptr->gp_size == 0)
9170     info_ptr->gp_save_offset = 0;
9171
9172   if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
9173     info_ptr->altivec_save_offset = 0;
9174
9175   if (! TARGET_ALTIVEC_ABI || info_ptr->vrsave_mask == 0)
9176     info_ptr->vrsave_save_offset = 0;
9177
9178   if (! TARGET_SPE_ABI || info_ptr->spe_gp_size == 0)
9179     info_ptr->spe_gp_save_offset = 0;
9180
9181   if (! info_ptr->lr_save_p)
9182     info_ptr->lr_save_offset = 0;
9183
9184   if (! info_ptr->cr_save_p)
9185     info_ptr->cr_save_offset = 0;
9186
9187   if (! info_ptr->toc_save_p)
9188     info_ptr->toc_save_offset = 0;
9189
9190   return info_ptr;
9191 }
9192
9193 void
9194 debug_stack_info (info)
9195      rs6000_stack_t *info;
9196 {
9197   const char *abi_string;
9198
9199   if (! info)
9200     info = rs6000_stack_info ();
9201
9202   fprintf (stderr, "\nStack information for function %s:\n",
9203            ((current_function_decl && DECL_NAME (current_function_decl))
9204             ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
9205             : "<unknown>"));
9206
9207   switch (info->abi)
9208     {
9209     default:             abi_string = "Unknown";        break;
9210     case ABI_NONE:       abi_string = "NONE";           break;
9211     case ABI_AIX:
9212     case ABI_AIX_NODESC: abi_string = "AIX";            break;
9213     case ABI_DARWIN:     abi_string = "Darwin";         break;
9214     case ABI_V4:         abi_string = "V.4";            break;
9215     }
9216
9217   fprintf (stderr, "\tABI                 = %5s\n", abi_string);
9218
9219   if (TARGET_ALTIVEC_ABI)
9220     fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
9221
9222   if (TARGET_SPE_ABI)
9223     fprintf (stderr, "\tSPE ABI extensions enabled.\n");
9224
9225   if (info->first_gp_reg_save != 32)
9226     fprintf (stderr, "\tfirst_gp_reg_save   = %5d\n", info->first_gp_reg_save);
9227
9228   if (info->first_fp_reg_save != 64)
9229     fprintf (stderr, "\tfirst_fp_reg_save   = %5d\n", info->first_fp_reg_save);
9230
9231   if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
9232     fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
9233              info->first_altivec_reg_save);
9234
9235   if (info->lr_save_p)
9236     fprintf (stderr, "\tlr_save_p           = %5d\n", info->lr_save_p);
9237
9238   if (info->cr_save_p)
9239     fprintf (stderr, "\tcr_save_p           = %5d\n", info->cr_save_p);
9240
9241   if (info->toc_save_p)
9242     fprintf (stderr, "\ttoc_save_p          = %5d\n", info->toc_save_p);
9243
9244   if (info->vrsave_mask)
9245     fprintf (stderr, "\tvrsave_mask         = 0x%x\n", info->vrsave_mask);
9246
9247   if (info->push_p)
9248     fprintf (stderr, "\tpush_p              = %5d\n", info->push_p);
9249
9250   if (info->calls_p)
9251     fprintf (stderr, "\tcalls_p             = %5d\n", info->calls_p);
9252
9253   if (info->gp_save_offset)
9254     fprintf (stderr, "\tgp_save_offset      = %5d\n", info->gp_save_offset);
9255
9256   if (info->fp_save_offset)
9257     fprintf (stderr, "\tfp_save_offset      = %5d\n", info->fp_save_offset);
9258
9259   if (info->altivec_save_offset)
9260     fprintf (stderr, "\taltivec_save_offset = %5d\n",
9261              info->altivec_save_offset);
9262
9263   if (info->spe_gp_save_offset)
9264     fprintf (stderr, "\tspe_gp_save_offset  = %5d\n",
9265              info->spe_gp_save_offset);
9266
9267   if (info->vrsave_save_offset)
9268     fprintf (stderr, "\tvrsave_save_offset  = %5d\n",
9269              info->vrsave_save_offset);
9270
9271   if (info->lr_save_offset)
9272     fprintf (stderr, "\tlr_save_offset      = %5d\n", info->lr_save_offset);
9273
9274   if (info->cr_save_offset)
9275     fprintf (stderr, "\tcr_save_offset      = %5d\n", info->cr_save_offset);
9276
9277   if (info->toc_save_offset)
9278     fprintf (stderr, "\ttoc_save_offset     = %5d\n", info->toc_save_offset);
9279
9280   if (info->varargs_save_offset)
9281     fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
9282
9283   if (info->total_size)
9284     fprintf (stderr, "\ttotal_size          = %5d\n", info->total_size);
9285
9286   if (info->varargs_size)
9287     fprintf (stderr, "\tvarargs_size        = %5d\n", info->varargs_size);
9288
9289   if (info->vars_size)
9290     fprintf (stderr, "\tvars_size           = %5d\n", info->vars_size);
9291
9292   if (info->parm_size)
9293     fprintf (stderr, "\tparm_size           = %5d\n", info->parm_size);
9294
9295   if (info->fixed_size)
9296     fprintf (stderr, "\tfixed_size          = %5d\n", info->fixed_size);
9297
9298   if (info->gp_size)
9299     fprintf (stderr, "\tgp_size             = %5d\n", info->gp_size);
9300
9301   if (info->spe_gp_size)
9302     fprintf (stderr, "\tspe_gp_size         = %5d\n", info->spe_gp_size);
9303
9304   if (info->fp_size)
9305     fprintf (stderr, "\tfp_size             = %5d\n", info->fp_size);
9306
9307   if (info->altivec_size)
9308     fprintf (stderr, "\taltivec_size        = %5d\n", info->altivec_size);
9309
9310   if (info->vrsave_size)
9311     fprintf (stderr, "\tvrsave_size         = %5d\n", info->vrsave_size);
9312
9313   if (info->altivec_padding_size)
9314     fprintf (stderr, "\taltivec_padding_size= %5d\n",
9315              info->altivec_padding_size);
9316
9317   if (info->spe_padding_size)
9318     fprintf (stderr, "\tspe_padding_size    = %5d\n",
9319              info->spe_padding_size);
9320
9321   if (info->lr_size)
9322     fprintf (stderr, "\tlr_size             = %5d\n", info->lr_size);
9323
9324   if (info->cr_size)
9325     fprintf (stderr, "\tcr_size             = %5d\n", info->cr_size);
9326
9327   if (info->toc_size)
9328     fprintf (stderr, "\ttoc_size            = %5d\n", info->toc_size);
9329
9330   if (info->save_size)
9331     fprintf (stderr, "\tsave_size           = %5d\n", info->save_size);
9332
9333   if (info->reg_size != 4)
9334     fprintf (stderr, "\treg_size            = %5d\n", info->reg_size);
9335
9336   fprintf (stderr, "\n");
9337 }
9338
9339 rtx
9340 rs6000_return_addr (count, frame)
9341      int count;
9342      rtx frame;
9343 {
9344   /* Currently we don't optimize very well between prolog and body
9345      code and for PIC code the code can be actually quite bad, so
9346      don't try to be too clever here.  */
9347   if (count != 0 || flag_pic != 0)
9348     {
9349       cfun->machine->ra_needs_full_frame = 1;
9350
9351       return
9352         gen_rtx_MEM
9353           (Pmode,
9354            memory_address
9355            (Pmode,
9356             plus_constant (copy_to_reg
9357                            (gen_rtx_MEM (Pmode,
9358                                          memory_address (Pmode, frame))),
9359                            RETURN_ADDRESS_OFFSET)));
9360     }
9361
9362   return get_hard_reg_initial_val (Pmode, LINK_REGISTER_REGNUM);
9363 }
9364
9365 static int
9366 rs6000_ra_ever_killed ()
9367 {
9368   rtx top;
9369
9370 #ifdef ASM_OUTPUT_MI_THUNK
9371   if (current_function_is_thunk)
9372     return 0;
9373 #endif
9374   if (!has_hard_reg_initial_val (Pmode, LINK_REGISTER_REGNUM)
9375       || cfun->machine->ra_needs_full_frame)
9376     return regs_ever_live[LINK_REGISTER_REGNUM];
9377
9378   push_topmost_sequence ();
9379   top = get_insns ();
9380   pop_topmost_sequence ();
9381
9382   return reg_set_between_p (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM), 
9383                             top, NULL_RTX);
9384 }
9385 \f
9386 /* Add a REG_MAYBE_DEAD note to the insn.  */
9387 static void
9388 rs6000_maybe_dead (insn)
9389      rtx insn;
9390 {
9391   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
9392                                         const0_rtx,
9393                                         REG_NOTES (insn));
9394 }
9395
9396 /* Emit instructions needed to load the TOC register.
9397    This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
9398    a constant pool; or for SVR4 -fpic.  */
9399
9400 void
9401 rs6000_emit_load_toc_table (fromprolog)
9402      int fromprolog;
9403 {
9404   rtx dest;
9405   dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
9406
9407   if (TARGET_ELF && DEFAULT_ABI != ABI_AIX)
9408     {
9409       if (DEFAULT_ABI == ABI_V4 && flag_pic == 1)
9410         {
9411           rtx temp = (fromprolog 
9412                       ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
9413                       : gen_reg_rtx (Pmode));
9414           rs6000_maybe_dead (emit_insn (gen_load_toc_v4_pic_si (temp)));
9415           rs6000_maybe_dead (emit_move_insn (dest, temp));
9416         }
9417       else if (flag_pic == 2)
9418         {
9419           char buf[30];
9420           rtx tempLR = (fromprolog 
9421                         ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
9422                         : gen_reg_rtx (Pmode));
9423           rtx temp0 = (fromprolog
9424                         ? gen_rtx_REG (Pmode, 0)
9425                         : gen_reg_rtx (Pmode));
9426           rtx symF;
9427
9428           /* possibly create the toc section */
9429           if (! toc_initialized)
9430             {
9431               toc_section ();
9432               function_section (current_function_decl);
9433             }
9434   
9435           if (fromprolog)
9436             {
9437               rtx symL;
9438           
9439               ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
9440               symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
9441
9442               ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
9443               symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
9444
9445               rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_1 (tempLR, 
9446                                                                    symF)));
9447               rs6000_maybe_dead (emit_move_insn (dest, tempLR));
9448               rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest,
9449                                                                    symL,
9450                                                                    symF)));
9451             }
9452           else
9453             {
9454               rtx tocsym;
9455               static int reload_toc_labelno = 0;
9456
9457               tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
9458
9459               ASM_GENERATE_INTERNAL_LABEL (buf, "LCG", reload_toc_labelno++);
9460               symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
9461
9462               rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_1b (tempLR, 
9463                                                                     symF, 
9464                                                                     tocsym)));
9465               rs6000_maybe_dead (emit_move_insn (dest, tempLR));
9466               rs6000_maybe_dead (emit_move_insn (temp0, 
9467                                                  gen_rtx_MEM (Pmode, dest)));
9468             }
9469           rs6000_maybe_dead (emit_insn (gen_addsi3 (dest, temp0, dest)));
9470         }
9471       else if (flag_pic == 0 && TARGET_MINIMAL_TOC)
9472         {
9473           /* This is for AIX code running in non-PIC ELF.  */
9474           char buf[30];
9475           rtx realsym;
9476           ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
9477           realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
9478           
9479           rs6000_maybe_dead (emit_insn (gen_elf_high (dest, realsym)));
9480           rs6000_maybe_dead (emit_insn (gen_elf_low (dest, dest, realsym)));
9481         }
9482       else
9483         abort ();
9484     }
9485   else
9486     {
9487       if (TARGET_32BIT)
9488         rs6000_maybe_dead (emit_insn (gen_load_toc_aix_si (dest)));
9489       else
9490         rs6000_maybe_dead (emit_insn (gen_load_toc_aix_di (dest)));
9491     }
9492 }
9493
9494 int   
9495 get_TOC_alias_set ()
9496 {
9497     static int set = -1;
9498     if (set == -1)
9499       set = new_alias_set ();
9500     return set;
9501 }   
9502
9503 /* This retuns nonzero if the current function uses the TOC.  This is
9504    determined by the presence of (unspec ... 7), which is generated by
9505    the various load_toc_* patterns.  */
9506
9507 int
9508 uses_TOC () 
9509 {
9510     rtx insn;
9511
9512     for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9513       if (INSN_P (insn))
9514         {
9515           rtx pat = PATTERN (insn);
9516           int i;
9517
9518           if (GET_CODE (pat) == PARALLEL) 
9519             for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
9520               if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == UNSPEC 
9521                  && XINT (XVECEXP (PATTERN (insn), 0, i), 1) == 7)
9522                   return 1;
9523         }
9524     return 0;
9525 }
9526
9527 rtx
9528 create_TOC_reference (symbol) 
9529     rtx symbol;
9530 {
9531   return gen_rtx_PLUS (Pmode, 
9532            gen_rtx_REG (Pmode, TOC_REGISTER),
9533              gen_rtx_CONST (Pmode, 
9534                gen_rtx_MINUS (Pmode, symbol, 
9535                  gen_rtx_SYMBOL_REF (Pmode, toc_label_name))));
9536 }
9537
9538 #if TARGET_AIX
9539 /* __throw will restore its own return address to be the same as the
9540    return address of the function that the throw is being made to.
9541    This is unfortunate, because we want to check the original
9542    return address to see if we need to restore the TOC.
9543    So we have to squirrel it away here.  
9544    This is used only in compiling __throw and __rethrow. 
9545
9546    Most of this code should be removed by CSE.  */
9547 static rtx insn_after_throw;
9548
9549 /* This does the saving...  */
9550 void
9551 rs6000_aix_emit_builtin_unwind_init ()
9552 {
9553   rtx mem;
9554   rtx stack_top = gen_reg_rtx (Pmode);
9555   rtx opcode_addr = gen_reg_rtx (Pmode);
9556
9557   insn_after_throw = gen_reg_rtx (SImode);
9558
9559   mem = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
9560   emit_move_insn (stack_top, mem);
9561
9562   mem = gen_rtx_MEM (Pmode, 
9563                      gen_rtx_PLUS (Pmode, stack_top, 
9564                                    GEN_INT (2 * GET_MODE_SIZE (Pmode))));
9565   emit_move_insn (opcode_addr, mem);
9566   emit_move_insn (insn_after_throw, gen_rtx_MEM (SImode, opcode_addr));
9567 }
9568
9569 /* Emit insns to _restore_ the TOC register, at runtime (specifically
9570    in _eh.o).  Only used on AIX.
9571
9572    The idea is that on AIX, function calls look like this:
9573         bl  somefunction-trampoline
9574         lwz r2,20(sp)
9575
9576    and later,
9577         somefunction-trampoline:
9578         stw r2,20(sp)
9579          ... load function address in the count register ...
9580         bctr
9581    or like this, if the linker determines that this is not a cross-module call
9582    and so the TOC need not be restored:
9583         bl  somefunction
9584         nop
9585    or like this, if the compiler could determine that this is not a
9586    cross-module call:
9587         bl  somefunction
9588    now, the tricky bit here is that register 2 is saved and restored
9589    by the _linker_, so we can't readily generate debugging information
9590    for it.  So we need to go back up the call chain looking at the
9591    insns at return addresses to see which calls saved the TOC register
9592    and so see where it gets restored from.
9593
9594    Oh, and all this gets done in RTL inside the eh_epilogue pattern,
9595    just before the actual epilogue.
9596
9597    On the bright side, this incurs no space or time overhead unless an
9598    exception is thrown, except for the extra code in libgcc.a.  
9599
9600    The parameter STACKSIZE is a register containing (at runtime)
9601    the amount to be popped off the stack in addition to the stack frame
9602    of this routine (which will be __throw or __rethrow, and so is
9603    guaranteed to have a stack frame).  */
9604
9605 void
9606 rs6000_emit_eh_toc_restore (stacksize)
9607      rtx stacksize;
9608 {
9609   rtx top_of_stack;
9610   rtx bottom_of_stack = gen_reg_rtx (Pmode);
9611   rtx tocompare = gen_reg_rtx (SImode);
9612   rtx opcode = gen_reg_rtx (SImode);
9613   rtx opcode_addr = gen_reg_rtx (Pmode);
9614   rtx mem;
9615   rtx loop_start = gen_label_rtx ();
9616   rtx no_toc_restore_needed = gen_label_rtx ();
9617   rtx loop_exit = gen_label_rtx ();
9618   
9619   mem = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
9620   set_mem_alias_set (mem, rs6000_sr_alias_set);
9621   emit_move_insn (bottom_of_stack, mem);
9622
9623   top_of_stack = expand_binop (Pmode, add_optab, 
9624                                bottom_of_stack, stacksize,
9625                                NULL_RTX, 1, OPTAB_WIDEN);
9626
9627   emit_move_insn (tocompare, gen_int_mode (TARGET_32BIT ? 0x80410014 
9628                                            : 0xE8410028, SImode));
9629
9630   if (insn_after_throw == NULL_RTX)
9631     abort ();
9632   emit_move_insn (opcode, insn_after_throw);
9633   
9634   emit_note (NULL, NOTE_INSN_LOOP_BEG);
9635   emit_label (loop_start);
9636   
9637   do_compare_rtx_and_jump (opcode, tocompare, NE, 1,
9638                            SImode, NULL_RTX, NULL_RTX,
9639                            no_toc_restore_needed);
9640   
9641   mem = gen_rtx_MEM (Pmode, 
9642                      gen_rtx_PLUS (Pmode, bottom_of_stack, 
9643                                    GEN_INT (5 * GET_MODE_SIZE (Pmode))));
9644   emit_move_insn (gen_rtx_REG (Pmode, 2), mem);
9645
9646   emit_label (no_toc_restore_needed);
9647   do_compare_rtx_and_jump (top_of_stack, bottom_of_stack, EQ, 1,
9648                            Pmode, NULL_RTX, NULL_RTX,
9649                            loop_exit);
9650
9651   mem = gen_rtx_MEM (Pmode, bottom_of_stack);
9652   set_mem_alias_set (mem, rs6000_sr_alias_set);
9653   emit_move_insn (bottom_of_stack, mem);
9654   
9655   mem = gen_rtx_MEM (Pmode, 
9656                      gen_rtx_PLUS (Pmode, bottom_of_stack, 
9657                                    GEN_INT (2 * GET_MODE_SIZE (Pmode))));
9658   emit_move_insn (opcode_addr, mem);
9659   emit_move_insn (opcode, gen_rtx_MEM (SImode, opcode_addr));
9660
9661   emit_note (NULL, NOTE_INSN_LOOP_CONT);
9662   emit_jump (loop_start);
9663   emit_note (NULL, NOTE_INSN_LOOP_END);
9664   emit_label (loop_exit);
9665 }
9666 #endif /* TARGET_AIX */
9667 \f
9668 /* This ties together stack memory (MEM with an alias set of
9669    rs6000_sr_alias_set) and the change to the stack pointer.  */
9670
9671 static void
9672 rs6000_emit_stack_tie ()
9673 {
9674   rtx mem = gen_rtx_MEM (BLKmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM));
9675
9676   set_mem_alias_set (mem, rs6000_sr_alias_set);
9677   emit_insn (gen_stack_tie (mem));
9678 }
9679
9680 /* Emit the correct code for allocating stack space, as insns.
9681    If COPY_R12, make sure a copy of the old frame is left in r12.
9682    The generated code may use hard register 0 as a temporary.  */
9683
9684 static void
9685 rs6000_emit_allocate_stack (size, copy_r12)
9686      HOST_WIDE_INT size;
9687      int copy_r12;
9688 {
9689   rtx insn;
9690   rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
9691   rtx tmp_reg = gen_rtx_REG (Pmode, 0);
9692   rtx todec = GEN_INT (-size);
9693
9694   if (current_function_limit_stack)
9695     {
9696       if (REG_P (stack_limit_rtx)
9697           && REGNO (stack_limit_rtx) > 1 
9698           && REGNO (stack_limit_rtx) <= 31)
9699         {
9700           emit_insn (Pmode == SImode
9701                      ? gen_addsi3 (tmp_reg,
9702                                    stack_limit_rtx,
9703                                    GEN_INT (size))
9704                      : gen_adddi3 (tmp_reg,
9705                                    stack_limit_rtx,
9706                                    GEN_INT (size)));
9707           
9708           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
9709                                     const0_rtx));
9710         }
9711       else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
9712                && TARGET_32BIT
9713                && DEFAULT_ABI == ABI_V4)
9714         {
9715           rtx toload = gen_rtx_CONST (VOIDmode,
9716                                       gen_rtx_PLUS (Pmode, 
9717                                                     stack_limit_rtx, 
9718                                                     GEN_INT (size)));
9719           
9720           emit_insn (gen_elf_high (tmp_reg, toload));
9721           emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
9722           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
9723                                     const0_rtx));
9724         }
9725       else
9726         warning ("stack limit expression is not supported");
9727     }
9728
9729   if (copy_r12 || ! TARGET_UPDATE)
9730     emit_move_insn (gen_rtx_REG (Pmode, 12), stack_reg);
9731
9732   if (TARGET_UPDATE)
9733     {
9734       if (size > 32767)
9735         {
9736           /* Need a note here so that try_split doesn't get confused.  */
9737           if (get_last_insn() == NULL_RTX)
9738             emit_note (0, NOTE_INSN_DELETED);
9739           insn = emit_move_insn (tmp_reg, todec);
9740           try_split (PATTERN (insn), insn, 0);
9741           todec = tmp_reg;
9742         }
9743       
9744       if (Pmode == SImode)
9745         insn = emit_insn (gen_movsi_update (stack_reg, stack_reg, 
9746                                             todec, stack_reg));
9747       else
9748         insn = emit_insn (gen_movdi_update (stack_reg, stack_reg, 
9749                                             todec, stack_reg));
9750     }
9751   else
9752     {
9753       if (Pmode == SImode)
9754         insn = emit_insn (gen_addsi3 (stack_reg, stack_reg, todec));
9755       else
9756         insn = emit_insn (gen_adddi3 (stack_reg, stack_reg, todec));
9757       emit_move_insn (gen_rtx_MEM (Pmode, stack_reg),
9758                       gen_rtx_REG (Pmode, 12));
9759     }
9760   
9761   RTX_FRAME_RELATED_P (insn) = 1;
9762   REG_NOTES (insn) = 
9763     gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
9764                        gen_rtx_SET (VOIDmode, stack_reg, 
9765                                     gen_rtx_PLUS (Pmode, stack_reg,
9766                                                   GEN_INT (-size))),
9767                        REG_NOTES (insn));
9768 }
9769
9770 /* Add a RTX_FRAME_RELATED note so that dwarf2out_frame_debug_expr
9771    knows that:
9772
9773      (mem (plus (blah) (regXX)))
9774
9775    is really:
9776
9777      (mem (plus (blah) (const VALUE_OF_REGXX))).  */
9778
9779 static void
9780 altivec_frame_fixup (insn, reg, val)
9781      rtx insn, reg;
9782      HOST_WIDE_INT val;
9783 {
9784   rtx real;
9785
9786   real = copy_rtx (PATTERN (insn));
9787
9788   real = replace_rtx (real, reg, GEN_INT (val));
9789
9790   RTX_FRAME_RELATED_P (insn) = 1;
9791   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
9792                                         real,
9793                                         REG_NOTES (insn));
9794 }
9795
9796 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
9797    with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
9798    is not NULL.  It would be nice if dwarf2out_frame_debug_expr could
9799    deduce these equivalences by itself so it wasn't necessary to hold
9800    its hand so much.  */
9801
9802 static void
9803 rs6000_frame_related (insn, reg, val, reg2, rreg)
9804      rtx insn;
9805      rtx reg;
9806      HOST_WIDE_INT val;
9807      rtx reg2;
9808      rtx rreg;
9809 {
9810   rtx real, temp;
9811
9812   /* copy_rtx will not make unique copies of registers, so we need to
9813      ensure we don't have unwanted sharing here.  */
9814   if (reg == reg2)
9815     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
9816
9817   if (reg == rreg)
9818     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
9819
9820   real = copy_rtx (PATTERN (insn));
9821
9822   if (reg2 != NULL_RTX)
9823     real = replace_rtx (real, reg2, rreg);
9824   
9825   real = replace_rtx (real, reg, 
9826                       gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
9827                                                         STACK_POINTER_REGNUM),
9828                                     GEN_INT (val)));
9829   
9830   /* We expect that 'real' is either a SET or a PARALLEL containing
9831      SETs (and possibly other stuff).  In a PARALLEL, all the SETs
9832      are important so they all have to be marked RTX_FRAME_RELATED_P.  */
9833
9834   if (GET_CODE (real) == SET)
9835     {
9836       rtx set = real;
9837       
9838       temp = simplify_rtx (SET_SRC (set));
9839       if (temp)
9840         SET_SRC (set) = temp;
9841       temp = simplify_rtx (SET_DEST (set));
9842       if (temp)
9843         SET_DEST (set) = temp;
9844       if (GET_CODE (SET_DEST (set)) == MEM)
9845         {
9846           temp = simplify_rtx (XEXP (SET_DEST (set), 0));
9847           if (temp)
9848             XEXP (SET_DEST (set), 0) = temp;
9849         }
9850     }
9851   else if (GET_CODE (real) == PARALLEL)
9852     {
9853       int i;
9854       for (i = 0; i < XVECLEN (real, 0); i++)
9855         if (GET_CODE (XVECEXP (real, 0, i)) == SET)
9856           {
9857             rtx set = XVECEXP (real, 0, i);
9858             
9859             temp = simplify_rtx (SET_SRC (set));
9860             if (temp)
9861               SET_SRC (set) = temp;
9862             temp = simplify_rtx (SET_DEST (set));
9863             if (temp)
9864               SET_DEST (set) = temp;
9865             if (GET_CODE (SET_DEST (set)) == MEM)
9866               {
9867                 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
9868                 if (temp)
9869                   XEXP (SET_DEST (set), 0) = temp;
9870               }
9871             RTX_FRAME_RELATED_P (set) = 1;
9872           }
9873     }
9874   else
9875     abort ();
9876   
9877   RTX_FRAME_RELATED_P (insn) = 1;
9878   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
9879                                         real,
9880                                         REG_NOTES (insn));
9881 }
9882
9883 /* Returns an insn that has a vrsave set operation with the
9884    appropriate CLOBBERs.  */
9885
9886 static rtx
9887 generate_set_vrsave (reg, info, epiloguep)
9888      rtx reg;
9889      rs6000_stack_t *info;
9890      int epiloguep;
9891 {
9892   int nclobs, i;
9893   rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
9894   rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
9895
9896   clobs[0]
9897     = gen_rtx_SET (VOIDmode,
9898                    vrsave,
9899                    gen_rtx_UNSPEC_VOLATILE (SImode,
9900                                             gen_rtvec (2, reg, vrsave),
9901                                             30));
9902
9903   nclobs = 1;
9904
9905   /* We need to clobber the registers in the mask so the scheduler
9906      does not move sets to VRSAVE before sets of AltiVec registers.
9907
9908      However, if the function receives nonlocal gotos, reload will set
9909      all call saved registers live.  We will end up with:
9910
9911         (set (reg 999) (mem))
9912         (parallel [ (set (reg vrsave) (unspec blah))
9913                     (clobber (reg 999))])
9914
9915      The clobber will cause the store into reg 999 to be dead, and
9916      flow will attempt to delete an epilogue insn.  In this case, we
9917      need an unspec use/set of the register.  */
9918
9919   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
9920     if (info->vrsave_mask != 0 && ALTIVEC_REG_BIT (i) != 0)
9921       {
9922         if (!epiloguep || call_used_regs [i])
9923           clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
9924                                              gen_rtx_REG (V4SImode, i));
9925         else
9926           {
9927             rtx reg = gen_rtx_REG (V4SImode, i);
9928
9929             clobs[nclobs++]
9930               = gen_rtx_SET (VOIDmode,
9931                              reg,
9932                              gen_rtx_UNSPEC (V4SImode,
9933                                              gen_rtvec (1, reg), 27));
9934           }
9935       }
9936
9937   insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
9938
9939   for (i = 0; i < nclobs; ++i)
9940     XVECEXP (insn, 0, i) = clobs[i];
9941
9942   return insn;
9943 }
9944
9945 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
9946    Save REGNO into [FRAME_REG + OFFSET] in mode MODE.  */
9947
9948 static void
9949 emit_frame_save (frame_reg, frame_ptr, mode, regno, offset, total_size)
9950      rtx frame_reg;
9951      rtx frame_ptr;
9952      enum machine_mode mode;
9953      unsigned int regno;
9954      int offset;
9955      int total_size;
9956 {
9957   rtx reg, offset_rtx, insn, mem, addr, int_rtx;
9958   rtx replacea, replaceb;
9959
9960   int_rtx = GEN_INT (offset);
9961
9962   /* Some cases that need register indexed addressing.  */
9963   if ((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
9964       || (TARGET_SPE_ABI
9965           && SPE_VECTOR_MODE (mode)
9966           && !SPE_CONST_OFFSET_OK (offset)))
9967     {
9968       /* Whomever calls us must make sure r11 is available in the
9969          flow path of instructions in the prologue.  */
9970       offset_rtx = gen_rtx_REG (Pmode, 11);
9971       emit_move_insn (offset_rtx, int_rtx);
9972
9973       replacea = offset_rtx;
9974       replaceb = int_rtx;
9975     }
9976   else
9977     {
9978       offset_rtx = int_rtx;
9979       replacea = NULL_RTX;
9980       replaceb = NULL_RTX;
9981     }
9982
9983   reg = gen_rtx_REG (mode, regno);
9984   addr = gen_rtx_PLUS (Pmode, frame_reg, offset_rtx);
9985   mem = gen_rtx_MEM (mode, addr);
9986   set_mem_alias_set (mem, rs6000_sr_alias_set);
9987
9988   insn = emit_move_insn (mem, reg);
9989
9990   rs6000_frame_related (insn, frame_ptr, total_size, replacea, replaceb);
9991 }
9992
9993 /* Emit an offset memory reference suitable for a frame store, while
9994    converting to a valid addressing mode.  */
9995
9996 static rtx
9997 gen_frame_mem_offset (mode, reg, offset)
9998      enum machine_mode mode;
9999      rtx reg;
10000      int offset;
10001 {
10002   rtx int_rtx, offset_rtx;
10003
10004   int_rtx = GEN_INT (offset);
10005
10006   if (TARGET_SPE_ABI && SPE_VECTOR_MODE (mode))
10007     {
10008       offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
10009       emit_move_insn (offset_rtx, int_rtx);
10010     }
10011   else
10012     offset_rtx = int_rtx;
10013
10014   return gen_rtx_MEM (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
10015 }
10016
10017 /* Emit function prologue as insns.  */
10018
10019 void
10020 rs6000_emit_prologue ()
10021 {
10022   rs6000_stack_t *info = rs6000_stack_info ();
10023   enum machine_mode reg_mode = TARGET_POWERPC64 ? DImode : SImode;
10024   int reg_size = TARGET_POWERPC64 ? 8 : 4;
10025   rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
10026   rtx frame_ptr_rtx = gen_rtx_REG (Pmode, 12);
10027   rtx frame_reg_rtx = sp_reg_rtx;
10028   rtx cr_save_rtx = NULL;
10029   rtx insn;
10030   int saving_FPRs_inline;
10031   int using_store_multiple;
10032   HOST_WIDE_INT sp_offset = 0;
10033   
10034    if (TARGET_SPE_ABI)
10035      {
10036        reg_mode = V2SImode;
10037        reg_size = 8;
10038      }
10039
10040   using_store_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
10041                           && !TARGET_SPE_ABI
10042                           && info->first_gp_reg_save < 31);
10043   saving_FPRs_inline = (info->first_fp_reg_save == 64
10044                         || FP_SAVE_INLINE (info->first_fp_reg_save));
10045
10046   /* For V.4, update stack before we do any saving and set back pointer.  */
10047   if (info->push_p && DEFAULT_ABI == ABI_V4)
10048     {
10049       if (info->total_size < 32767)
10050         sp_offset = info->total_size;
10051       else
10052         frame_reg_rtx = frame_ptr_rtx;
10053       rs6000_emit_allocate_stack (info->total_size, 
10054                                   (frame_reg_rtx != sp_reg_rtx
10055                                    && (info->cr_save_p
10056                                        || info->lr_save_p
10057                                        || info->first_fp_reg_save < 64
10058                                        || info->first_gp_reg_save < 32
10059                                        )));
10060       if (frame_reg_rtx != sp_reg_rtx)
10061         rs6000_emit_stack_tie ();
10062     }
10063
10064   /* Save AltiVec registers if needed.  */
10065   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
10066     {
10067       int i;
10068
10069       /* There should be a non inline version of this, for when we
10070          are saving lots of vector registers.  */
10071       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
10072         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
10073           {
10074             rtx areg, savereg, mem;
10075             int offset;
10076
10077             offset = info->altivec_save_offset + sp_offset
10078               + 16 * (i - info->first_altivec_reg_save);
10079
10080             savereg = gen_rtx_REG (V4SImode, i);
10081
10082             areg = gen_rtx_REG (Pmode, 0);
10083             emit_move_insn (areg, GEN_INT (offset));
10084
10085             /* AltiVec addressing mode is [reg+reg].  */
10086             mem = gen_rtx_MEM (V4SImode,
10087                                gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
10088                                
10089             set_mem_alias_set (mem, rs6000_sr_alias_set);
10090
10091             insn = emit_move_insn (mem, savereg);
10092
10093             altivec_frame_fixup (insn, areg, offset);
10094           }
10095     }
10096
10097   /* VRSAVE is a bit vector representing which AltiVec registers
10098      are used.  The OS uses this to determine which vector
10099      registers to save on a context switch.  We need to save
10100      VRSAVE on the stack frame, add whatever AltiVec registers we
10101      used in this function, and do the corresponding magic in the
10102      epilogue.  */
10103
10104   if (TARGET_ALTIVEC && info->vrsave_mask != 0)
10105     {
10106       rtx reg, mem, vrsave;
10107       int offset;
10108
10109       /* Get VRSAVE onto a GPR.  */
10110       reg = gen_rtx_REG (SImode, 12);
10111       vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
10112       if (TARGET_MACHO)
10113         emit_insn (gen_get_vrsave_internal (reg));
10114       else
10115         emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
10116
10117       /* Save VRSAVE.  */
10118       offset = info->vrsave_save_offset + sp_offset;
10119       mem
10120         = gen_rtx_MEM (SImode,
10121                        gen_rtx_PLUS (Pmode, frame_reg_rtx, GEN_INT (offset)));
10122       set_mem_alias_set (mem, rs6000_sr_alias_set);
10123       insn = emit_move_insn (mem, reg);
10124
10125       /* Include the registers in the mask.  */
10126       emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
10127
10128       insn = emit_insn (generate_set_vrsave (reg, info, 0));
10129     }
10130
10131   /* If we use the link register, get it into r0.  */
10132   if (info->lr_save_p)
10133     emit_move_insn (gen_rtx_REG (Pmode, 0),
10134                     gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
10135
10136   /* If we need to save CR, put it into r12.  */
10137   if (info->cr_save_p && frame_reg_rtx != frame_ptr_rtx)
10138     {
10139       cr_save_rtx = gen_rtx_REG (SImode, 12);
10140       emit_insn (gen_movesi_from_cr (cr_save_rtx));
10141     }
10142
10143   /* Do any required saving of fpr's.  If only one or two to save, do
10144      it ourselves.  Otherwise, call function.  */
10145   if (saving_FPRs_inline)
10146     {
10147       int i;
10148       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
10149         if ((regs_ever_live[info->first_fp_reg_save+i] 
10150              && ! call_used_regs[info->first_fp_reg_save+i]))
10151           emit_frame_save (frame_reg_rtx, frame_ptr_rtx, DFmode,
10152                            info->first_fp_reg_save + i,
10153                            info->fp_save_offset + sp_offset + 8 * i,
10154                            info->total_size);
10155     }
10156   else if (info->first_fp_reg_save != 64)
10157     {
10158       int i;
10159       char rname[30];
10160       const char *alloc_rname;
10161       rtvec p;
10162       p = rtvec_alloc (2 + 64 - info->first_fp_reg_save);
10163       
10164       RTVEC_ELT (p, 0) = gen_rtx_CLOBBER (VOIDmode, 
10165                                           gen_rtx_REG (Pmode, 
10166                                                        LINK_REGISTER_REGNUM));
10167       sprintf (rname, "%s%d%s", SAVE_FP_PREFIX,
10168                info->first_fp_reg_save - 32, SAVE_FP_SUFFIX);
10169       alloc_rname = ggc_strdup (rname);
10170       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode,
10171                                       gen_rtx_SYMBOL_REF (Pmode,
10172                                                           alloc_rname));
10173       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
10174         {
10175           rtx addr, reg, mem;
10176           reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
10177           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
10178                                GEN_INT (info->fp_save_offset 
10179                                         + sp_offset + 8*i));
10180           mem = gen_rtx_MEM (DFmode, addr);
10181           set_mem_alias_set (mem, rs6000_sr_alias_set);
10182
10183           RTVEC_ELT (p, i + 2) = gen_rtx_SET (VOIDmode, mem, reg);
10184         }
10185       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
10186       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
10187                             NULL_RTX, NULL_RTX);
10188     }
10189
10190   /* Save GPRs.  This is done as a PARALLEL if we are using
10191      the store-multiple instructions.  */
10192   if (using_store_multiple)
10193     {
10194       rtvec p, dwarfp;
10195       int i;
10196       p = rtvec_alloc (32 - info->first_gp_reg_save);
10197       dwarfp = rtvec_alloc (32 - info->first_gp_reg_save);
10198       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
10199         {
10200           rtx addr, reg, mem;
10201           reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
10202           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
10203                                GEN_INT (info->gp_save_offset 
10204                                         + sp_offset 
10205                                         + reg_size * i));
10206           mem = gen_rtx_MEM (reg_mode, addr);
10207           set_mem_alias_set (mem, rs6000_sr_alias_set);
10208
10209           RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, reg);
10210         }
10211       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
10212       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
10213                             NULL_RTX, NULL_RTX);
10214     }
10215   else
10216     {
10217       int i;
10218       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
10219         if ((regs_ever_live[info->first_gp_reg_save+i] 
10220              && ! call_used_regs[info->first_gp_reg_save+i])
10221             || (i+info->first_gp_reg_save == RS6000_PIC_OFFSET_TABLE_REGNUM
10222                 && ((DEFAULT_ABI == ABI_V4 && flag_pic == 1)
10223                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
10224           {
10225             rtx addr, reg, mem;
10226             reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
10227
10228             if (TARGET_SPE_ABI)
10229               {
10230                 int offset = info->spe_gp_save_offset + sp_offset + 8 * i;
10231                 rtx b;
10232
10233                 if (!SPE_CONST_OFFSET_OK (offset))
10234                   {
10235                     b = gen_rtx_REG (Pmode, FIXED_SCRATCH);
10236                     emit_move_insn (b, GEN_INT (offset));
10237                   }
10238                 else
10239                   b = GEN_INT (offset);
10240
10241                 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, b);
10242                 mem = gen_rtx_MEM (V2SImode, addr);
10243                 set_mem_alias_set (mem, rs6000_sr_alias_set);
10244                 insn = emit_move_insn (mem, reg);
10245
10246                 if (GET_CODE (b) == CONST_INT)
10247                   rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
10248                                         NULL_RTX, NULL_RTX);
10249                 else
10250                   rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
10251                                         b, GEN_INT (offset));
10252               }
10253             else
10254               {
10255                 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
10256                                      GEN_INT (info->gp_save_offset 
10257                                               + sp_offset 
10258                                               + reg_size * i));
10259                 mem = gen_rtx_MEM (reg_mode, addr);
10260                 set_mem_alias_set (mem, rs6000_sr_alias_set);
10261
10262                 insn = emit_move_insn (mem, reg);
10263                 rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
10264                                       NULL_RTX, NULL_RTX);
10265               }
10266           }
10267     }
10268
10269   /* ??? There's no need to emit actual instructions here, but it's the
10270      easiest way to get the frame unwind information emitted.  */
10271   if (current_function_calls_eh_return)
10272     {
10273       unsigned int i, regno;
10274
10275       for (i = 0; ; ++i)
10276         {
10277           regno = EH_RETURN_DATA_REGNO (i);
10278           if (regno == INVALID_REGNUM)
10279             break;
10280
10281           emit_frame_save (frame_reg_rtx, frame_ptr_rtx, reg_mode, regno,
10282                            info->ehrd_offset + sp_offset
10283                            + reg_size * (int) i,
10284                            info->total_size);
10285         }
10286     }
10287
10288   /* Save lr if we used it.  */
10289   if (info->lr_save_p)
10290     {
10291       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
10292                                GEN_INT (info->lr_save_offset + sp_offset));
10293       rtx reg = gen_rtx_REG (Pmode, 0);
10294       rtx mem = gen_rtx_MEM (Pmode, addr);
10295       /* This should not be of rs6000_sr_alias_set, because of
10296          __builtin_return_address.  */
10297       
10298       insn = emit_move_insn (mem, reg);
10299       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
10300                             reg, gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
10301     }
10302
10303   /* Save CR if we use any that must be preserved.  */
10304   if (info->cr_save_p)
10305     {
10306       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
10307                                GEN_INT (info->cr_save_offset + sp_offset));
10308       rtx mem = gen_rtx_MEM (SImode, addr);
10309
10310       set_mem_alias_set (mem, rs6000_sr_alias_set);
10311
10312       /* If r12 was used to hold the original sp, copy cr into r0 now
10313          that it's free.  */
10314       if (REGNO (frame_reg_rtx) == 12)
10315         {
10316           cr_save_rtx = gen_rtx_REG (SImode, 0);
10317           emit_insn (gen_movesi_from_cr (cr_save_rtx));
10318         }
10319       insn = emit_move_insn (mem, cr_save_rtx);
10320
10321       /* Now, there's no way that dwarf2out_frame_debug_expr is going
10322          to understand '(unspec:SI [(reg:CC 68) ...] 19)'.  But that's
10323          OK.  All we have to do is specify that _one_ condition code
10324          register is saved in this stack slot.  The thrower's epilogue
10325          will then restore all the call-saved registers.
10326          We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux.  */
10327       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
10328                             cr_save_rtx, gen_rtx_REG (SImode, CR2_REGNO));
10329     }
10330
10331   /* Update stack and set back pointer unless this is V.4, 
10332      for which it was done previously.  */
10333   if (info->push_p && DEFAULT_ABI != ABI_V4)
10334     rs6000_emit_allocate_stack (info->total_size, FALSE);
10335
10336   /* Set frame pointer, if needed.  */
10337   if (frame_pointer_needed)
10338     {
10339       insn = emit_move_insn (gen_rtx_REG (Pmode, FRAME_POINTER_REGNUM), 
10340                              sp_reg_rtx);
10341       RTX_FRAME_RELATED_P (insn) = 1;
10342     }
10343
10344   /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up.  */
10345   if ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
10346       || (DEFAULT_ABI == ABI_V4 && flag_pic == 1
10347           && regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM]))
10348   {
10349     /* If emit_load_toc_table will use the link register, we need to save
10350        it.  We use R11 for this purpose because emit_load_toc_table
10351        can use register 0.  This allows us to use a plain 'blr' to return
10352        from the procedure more often.  */
10353     int save_LR_around_toc_setup = (TARGET_ELF && flag_pic != 0
10354                                     && ! info->lr_save_p
10355                                     && EXIT_BLOCK_PTR->pred != NULL);
10356     if (save_LR_around_toc_setup)
10357       emit_move_insn (gen_rtx_REG (Pmode, 11), 
10358                       gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
10359     
10360     rs6000_emit_load_toc_table (TRUE);
10361
10362     if (save_LR_around_toc_setup)
10363       emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM), 
10364                       gen_rtx_REG (Pmode, 11));
10365   }
10366
10367   if (DEFAULT_ABI == ABI_DARWIN
10368       && flag_pic && current_function_uses_pic_offset_table)
10369     {
10370       rtx dest = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
10371
10372       rs6000_maybe_dead (emit_insn (gen_load_macho_picbase (dest)));
10373
10374       rs6000_maybe_dead (
10375         emit_move_insn (gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM),
10376                         gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)));
10377     }
10378 }
10379
10380 /* Write function prologue.  */
10381
10382 static void
10383 rs6000_output_function_prologue (file, size)
10384      FILE *file;
10385      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
10386 {
10387   rs6000_stack_t *info = rs6000_stack_info ();
10388
10389   if (TARGET_DEBUG_STACK)
10390     debug_stack_info (info);
10391
10392   /* Write .extern for any function we will call to save and restore
10393      fp values.  */
10394   if (info->first_fp_reg_save < 64
10395       && !FP_SAVE_INLINE (info->first_fp_reg_save))
10396     fprintf (file, "\t.extern %s%d%s\n\t.extern %s%d%s\n",
10397              SAVE_FP_PREFIX, info->first_fp_reg_save - 32, SAVE_FP_SUFFIX,
10398              RESTORE_FP_PREFIX, info->first_fp_reg_save - 32,
10399              RESTORE_FP_SUFFIX);
10400
10401   /* Write .extern for AIX common mode routines, if needed.  */
10402   if (! TARGET_POWER && ! TARGET_POWERPC && ! common_mode_defined)
10403     {
10404       fputs ("\t.extern __mulh\n", file);
10405       fputs ("\t.extern __mull\n", file);
10406       fputs ("\t.extern __divss\n", file);
10407       fputs ("\t.extern __divus\n", file);
10408       fputs ("\t.extern __quoss\n", file);
10409       fputs ("\t.extern __quous\n", file);
10410       common_mode_defined = 1;
10411     }
10412
10413   if (! HAVE_prologue)
10414     {
10415       start_sequence ();
10416
10417       /* A NOTE_INSN_DELETED is supposed to be at the start and end of
10418          the "toplevel" insn chain.  */
10419       emit_note (0, NOTE_INSN_DELETED);
10420       rs6000_emit_prologue ();
10421       emit_note (0, NOTE_INSN_DELETED);
10422
10423       /* Expand INSN_ADDRESSES so final() doesn't crash. */
10424       {
10425         rtx insn;
10426         unsigned addr = 0;
10427         for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
10428           {
10429             INSN_ADDRESSES_NEW (insn, addr);
10430             addr += 4;
10431           }
10432       }
10433
10434       if (TARGET_DEBUG_STACK)
10435         debug_rtx_list (get_insns (), 100);
10436       final (get_insns (), file, FALSE, FALSE);
10437       end_sequence ();
10438     }
10439
10440   rs6000_pic_labelno++;
10441 }
10442   
10443 /* Emit function epilogue as insns.
10444
10445    At present, dwarf2out_frame_debug_expr doesn't understand
10446    register restores, so we don't bother setting RTX_FRAME_RELATED_P
10447    anywhere in the epilogue.  Most of the insns below would in any case
10448    need special notes to explain where r11 is in relation to the stack.  */
10449
10450 void
10451 rs6000_emit_epilogue (sibcall)
10452      int sibcall;
10453 {
10454   rs6000_stack_t *info;
10455   int restoring_FPRs_inline;
10456   int using_load_multiple;
10457   int using_mfcr_multiple;
10458   int use_backchain_to_restore_sp;
10459   int sp_offset = 0;
10460   rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
10461   rtx frame_reg_rtx = sp_reg_rtx;
10462   enum machine_mode reg_mode = TARGET_POWERPC64 ? DImode : SImode;
10463   int reg_size = TARGET_POWERPC64 ? 8 : 4;
10464   int i;
10465
10466   if (TARGET_SPE_ABI)
10467     {
10468       reg_mode = V2SImode;
10469       reg_size = 8;
10470     }
10471
10472   info = rs6000_stack_info ();
10473   using_load_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
10474                          && !TARGET_SPE_ABI
10475                          && info->first_gp_reg_save < 31);
10476   restoring_FPRs_inline = (sibcall
10477                            || current_function_calls_eh_return
10478                            || info->first_fp_reg_save == 64
10479                            || FP_SAVE_INLINE (info->first_fp_reg_save));
10480   use_backchain_to_restore_sp = (frame_pointer_needed 
10481                                  || current_function_calls_alloca
10482                                  || info->total_size > 32767);
10483   using_mfcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
10484                          || rs6000_cpu == PROCESSOR_PPC603
10485                          || rs6000_cpu == PROCESSOR_PPC750
10486                          || optimize_size);
10487
10488   /* If we have a frame pointer, a call to alloca,  or a large stack
10489      frame, restore the old stack pointer using the backchain.  Otherwise,
10490      we know what size to update it with.  */
10491   if (use_backchain_to_restore_sp)
10492     {
10493       /* Under V.4, don't reset the stack pointer until after we're done
10494          loading the saved registers.  */
10495       if (DEFAULT_ABI == ABI_V4)
10496         frame_reg_rtx = gen_rtx_REG (Pmode, 11);
10497
10498       emit_move_insn (frame_reg_rtx,
10499                       gen_rtx_MEM (Pmode, sp_reg_rtx));
10500       
10501     }
10502   else if (info->push_p)
10503     {
10504       if (DEFAULT_ABI == ABI_V4)
10505         sp_offset = info->total_size;
10506       else
10507         {
10508           emit_insn (TARGET_32BIT
10509                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
10510                                    GEN_INT (info->total_size))
10511                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
10512                                    GEN_INT (info->total_size)));
10513         }
10514     }
10515   
10516   /* Restore AltiVec registers if needed.  */
10517   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
10518     {
10519       int i;
10520
10521       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
10522         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
10523           {
10524             rtx addr, areg, mem;
10525
10526             areg = gen_rtx_REG (Pmode, 0);
10527             emit_move_insn
10528               (areg, GEN_INT (info->altivec_save_offset
10529                               + sp_offset
10530                               + 16 * (i - info->first_altivec_reg_save)));
10531
10532             /* AltiVec addressing mode is [reg+reg].  */
10533             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
10534             mem = gen_rtx_MEM (V4SImode, addr);
10535             set_mem_alias_set (mem, rs6000_sr_alias_set);
10536
10537             emit_move_insn (gen_rtx_REG (V4SImode, i), mem);
10538           }
10539     }
10540
10541   /* Restore VRSAVE if needed.  */
10542   if (TARGET_ALTIVEC_ABI && info->vrsave_mask != 0)
10543     {
10544       rtx addr, mem, reg;
10545
10546       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
10547                            GEN_INT (info->vrsave_save_offset + sp_offset));
10548       mem = gen_rtx_MEM (SImode, addr);
10549       set_mem_alias_set (mem, rs6000_sr_alias_set);
10550       reg = gen_rtx_REG (SImode, 12);
10551       emit_move_insn (reg, mem);
10552
10553       emit_insn (generate_set_vrsave (reg, info, 1));
10554     }
10555
10556   /* Get the old lr if we saved it.  */
10557   if (info->lr_save_p)
10558     {
10559       rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx,
10560                                       info->lr_save_offset + sp_offset);
10561
10562       set_mem_alias_set (mem, rs6000_sr_alias_set);
10563
10564       emit_move_insn (gen_rtx_REG (Pmode, 0), mem);
10565     }
10566   
10567   /* Get the old cr if we saved it.  */
10568   if (info->cr_save_p)
10569     {
10570       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
10571                                GEN_INT (info->cr_save_offset + sp_offset));
10572       rtx mem = gen_rtx_MEM (SImode, addr);
10573
10574       set_mem_alias_set (mem, rs6000_sr_alias_set);
10575
10576       emit_move_insn (gen_rtx_REG (SImode, 12), mem);
10577     }
10578   
10579   /* Set LR here to try to overlap restores below.  */
10580   if (info->lr_save_p)
10581     emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM),
10582                     gen_rtx_REG (Pmode, 0));
10583   
10584   /* Load exception handler data registers, if needed.  */
10585   if (current_function_calls_eh_return)
10586     {
10587       unsigned int i, regno;
10588
10589       for (i = 0; ; ++i)
10590         {
10591           rtx mem;
10592
10593           regno = EH_RETURN_DATA_REGNO (i);
10594           if (regno == INVALID_REGNUM)
10595             break;
10596
10597           mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
10598                                       info->ehrd_offset + sp_offset
10599                                       + reg_size * (int) i);
10600           set_mem_alias_set (mem, rs6000_sr_alias_set);
10601
10602           emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
10603         }
10604     }
10605   
10606   /* Restore GPRs.  This is done as a PARALLEL if we are using
10607      the load-multiple instructions.  */
10608   if (using_load_multiple)
10609     {
10610       rtvec p;
10611       p = rtvec_alloc (32 - info->first_gp_reg_save);
10612       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
10613         {
10614           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
10615                                    GEN_INT (info->gp_save_offset 
10616                                             + sp_offset 
10617                                             + reg_size * i));
10618           rtx mem = gen_rtx_MEM (reg_mode, addr);
10619
10620           set_mem_alias_set (mem, rs6000_sr_alias_set);
10621
10622           RTVEC_ELT (p, i) = 
10623             gen_rtx_SET (VOIDmode,
10624                          gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
10625                          mem);
10626         }
10627       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
10628     }
10629   else
10630     for (i = 0; i < 32 - info->first_gp_reg_save; i++)
10631       if ((regs_ever_live[info->first_gp_reg_save+i] 
10632            && ! call_used_regs[info->first_gp_reg_save+i])
10633           || (i+info->first_gp_reg_save == RS6000_PIC_OFFSET_TABLE_REGNUM
10634               && ((DEFAULT_ABI == ABI_V4 && flag_pic == 1)
10635                   || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
10636         {
10637           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
10638                                    GEN_INT (info->gp_save_offset 
10639                                             + sp_offset 
10640                                             + reg_size * i));
10641           rtx mem = gen_rtx_MEM (reg_mode, addr);
10642
10643           /* Restore 64-bit quantities for SPE.  */
10644           if (TARGET_SPE_ABI)
10645             {
10646               int offset = info->spe_gp_save_offset + sp_offset + 8 * i;
10647               rtx b;
10648
10649               if (!SPE_CONST_OFFSET_OK (offset))
10650                 {
10651                   b = gen_rtx_REG (Pmode, FIXED_SCRATCH);
10652                   emit_move_insn (b, GEN_INT (offset));
10653                 }
10654               else
10655                 b = GEN_INT (offset);
10656
10657               addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, b);
10658               mem = gen_rtx_MEM (V2SImode, addr);
10659             }
10660
10661           set_mem_alias_set (mem, rs6000_sr_alias_set);
10662
10663           emit_move_insn (gen_rtx_REG (reg_mode, 
10664                                        info->first_gp_reg_save + i), mem);
10665         }
10666
10667   /* Restore fpr's if we need to do it without calling a function.  */
10668   if (restoring_FPRs_inline)
10669     for (i = 0; i < 64 - info->first_fp_reg_save; i++)
10670       if ((regs_ever_live[info->first_fp_reg_save+i] 
10671            && ! call_used_regs[info->first_fp_reg_save+i]))
10672         {
10673           rtx addr, mem;
10674           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
10675                                GEN_INT (info->fp_save_offset 
10676                                         + sp_offset 
10677                                         + 8 * i));
10678           mem = gen_rtx_MEM (DFmode, addr);
10679           set_mem_alias_set (mem, rs6000_sr_alias_set);
10680
10681           emit_move_insn (gen_rtx_REG (DFmode, 
10682                                        info->first_fp_reg_save + i),
10683                           mem);
10684         }
10685
10686   /* If we saved cr, restore it here.  Just those that were used.  */
10687   if (info->cr_save_p)
10688     {
10689       rtx r12_rtx = gen_rtx_REG (SImode, 12);
10690       int count = 0;
10691       
10692       if (using_mfcr_multiple)
10693         {
10694           for (i = 0; i < 8; i++)
10695             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
10696               count++;
10697           if (count == 0)
10698             abort ();
10699         }
10700
10701       if (using_mfcr_multiple && count > 1)
10702         {
10703           rtvec p;
10704           int ndx;
10705           
10706           p = rtvec_alloc (count);
10707
10708           ndx = 0;
10709           for (i = 0; i < 8; i++)
10710             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
10711               {
10712                 rtvec r = rtvec_alloc (2);
10713                 RTVEC_ELT (r, 0) = r12_rtx;
10714                 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
10715                 RTVEC_ELT (p, ndx) =
10716                   gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO+i), 
10717                                gen_rtx_UNSPEC (CCmode, r, 20));
10718                 ndx++;
10719               }
10720           emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
10721           if (ndx != count)
10722             abort ();
10723         }
10724       else
10725         for (i = 0; i < 8; i++)
10726           if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
10727             {
10728               emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode, 
10729                                                            CR0_REGNO+i),
10730                                               r12_rtx));
10731             }
10732     }
10733
10734   /* If this is V.4, unwind the stack pointer after all of the loads
10735      have been done.  We need to emit a block here so that sched
10736      doesn't decide to move the sp change before the register restores
10737      (which may not have any obvious dependency on the stack).  This
10738      doesn't hurt performance, because there is no scheduling that can
10739      be done after this point.  */
10740   if (DEFAULT_ABI == ABI_V4)
10741     {
10742       if (frame_reg_rtx != sp_reg_rtx)
10743           rs6000_emit_stack_tie ();
10744
10745       if (use_backchain_to_restore_sp)
10746         {
10747           emit_move_insn (sp_reg_rtx, frame_reg_rtx);
10748         }
10749       else if (sp_offset != 0)
10750         {
10751           emit_insn (Pmode == SImode
10752                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
10753                                    GEN_INT (sp_offset))
10754                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
10755                                    GEN_INT (sp_offset)));
10756         }
10757     }
10758
10759   if (current_function_calls_eh_return)
10760     {
10761       rtx sa = EH_RETURN_STACKADJ_RTX;
10762       emit_insn (Pmode == SImode
10763                  ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx, sa)
10764                  : gen_adddi3 (sp_reg_rtx, sp_reg_rtx, sa));
10765     }
10766
10767   if (!sibcall)
10768     {
10769       rtvec p;
10770       if (! restoring_FPRs_inline)
10771         p = rtvec_alloc (3 + 64 - info->first_fp_reg_save);
10772       else
10773         p = rtvec_alloc (2);
10774
10775       RTVEC_ELT (p, 0) = gen_rtx_RETURN (VOIDmode);
10776       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode, 
10777                                       gen_rtx_REG (Pmode, 
10778                                                    LINK_REGISTER_REGNUM));
10779
10780       /* If we have to restore more than two FP registers, branch to the
10781          restore function.  It will return to our caller.  */
10782       if (! restoring_FPRs_inline)
10783         {
10784           int i;
10785           char rname[30];
10786           const char *alloc_rname;
10787
10788           sprintf (rname, "%s%d%s", RESTORE_FP_PREFIX, 
10789                    info->first_fp_reg_save - 32, RESTORE_FP_SUFFIX);
10790           alloc_rname = ggc_strdup (rname);
10791           RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode,
10792                                           gen_rtx_SYMBOL_REF (Pmode,
10793                                                               alloc_rname));
10794
10795           for (i = 0; i < 64 - info->first_fp_reg_save; i++)
10796             {
10797               rtx addr, mem;
10798               addr = gen_rtx_PLUS (Pmode, sp_reg_rtx,
10799                                    GEN_INT (info->fp_save_offset + 8*i));
10800               mem = gen_rtx_MEM (DFmode, addr);
10801               set_mem_alias_set (mem, rs6000_sr_alias_set);
10802
10803               RTVEC_ELT (p, i+3) = 
10804                 gen_rtx_SET (VOIDmode,
10805                              gen_rtx_REG (DFmode, info->first_fp_reg_save + i),
10806                              mem);
10807             }
10808         }
10809       
10810       emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
10811     }
10812 }
10813
10814 /* Write function epilogue.  */
10815
10816 static void
10817 rs6000_output_function_epilogue (file, size)
10818      FILE *file;
10819      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
10820 {
10821   rs6000_stack_t *info = rs6000_stack_info ();
10822   int optional_tbtab = (optimize_size || TARGET_ELF) ? 0 : 1;
10823
10824   if (! HAVE_epilogue)
10825     {
10826       rtx insn = get_last_insn ();
10827       /* If the last insn was a BARRIER, we don't have to write anything except
10828          the trace table.  */
10829       if (GET_CODE (insn) == NOTE)
10830         insn = prev_nonnote_insn (insn);
10831       if (insn == 0 ||  GET_CODE (insn) != BARRIER)
10832         {
10833           /* This is slightly ugly, but at least we don't have two
10834              copies of the epilogue-emitting code.  */
10835           start_sequence ();
10836
10837           /* A NOTE_INSN_DELETED is supposed to be at the start
10838              and end of the "toplevel" insn chain.  */
10839           emit_note (0, NOTE_INSN_DELETED);
10840           rs6000_emit_epilogue (FALSE);
10841           emit_note (0, NOTE_INSN_DELETED);
10842
10843           /* Expand INSN_ADDRESSES so final() doesn't crash. */
10844           {
10845             rtx insn;
10846             unsigned addr = 0;
10847             for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
10848               {
10849                 INSN_ADDRESSES_NEW (insn, addr);
10850                 addr += 4;
10851               }
10852           }
10853
10854           if (TARGET_DEBUG_STACK)
10855             debug_rtx_list (get_insns (), 100);
10856           final (get_insns (), file, FALSE, FALSE);
10857           end_sequence ();
10858         }
10859     }
10860
10861   /* Output a traceback table here.  See /usr/include/sys/debug.h for info
10862      on its format.
10863
10864      We don't output a traceback table if -finhibit-size-directive was
10865      used.  The documentation for -finhibit-size-directive reads
10866      ``don't output a @code{.size} assembler directive, or anything
10867      else that would cause trouble if the function is split in the
10868      middle, and the two halves are placed at locations far apart in
10869      memory.''  The traceback table has this property, since it
10870      includes the offset from the start of the function to the
10871      traceback table itself.
10872
10873      System V.4 Powerpc's (and the embedded ABI derived from it) use a
10874      different traceback table.  */
10875   if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive)
10876     {
10877       const char *fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
10878       const char *language_string = lang_hooks.name;
10879       int fixed_parms = 0, float_parms = 0, parm_info = 0;
10880       int i;
10881
10882       while (*fname == '.')     /* V.4 encodes . in the name */
10883         fname++;
10884
10885       /* Need label immediately before tbtab, so we can compute its offset
10886          from the function start.  */
10887       if (*fname == '*')
10888         ++fname;
10889       ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
10890       ASM_OUTPUT_LABEL (file, fname);
10891
10892       /* The .tbtab pseudo-op can only be used for the first eight
10893          expressions, since it can't handle the possibly variable
10894          length fields that follow.  However, if you omit the optional
10895          fields, the assembler outputs zeros for all optional fields
10896          anyways, giving each variable length field is minimum length
10897          (as defined in sys/debug.h).  Thus we can not use the .tbtab
10898          pseudo-op at all.  */
10899
10900       /* An all-zero word flags the start of the tbtab, for debuggers
10901          that have to find it by searching forward from the entry
10902          point or from the current pc.  */
10903       fputs ("\t.long 0\n", file);
10904
10905       /* Tbtab format type.  Use format type 0.  */
10906       fputs ("\t.byte 0,", file);
10907
10908       /* Language type.  Unfortunately, there doesn't seem to be any
10909          official way to get this info, so we use language_string.  C
10910          is 0.  C++ is 9.  No number defined for Obj-C, so use the
10911          value for C for now.  There is no official value for Java,
10912          although IBM appears to be using 13.  There is no official value
10913          for Chill, so we've chosen 44 pseudo-randomly.  */
10914       if (! strcmp (language_string, "GNU C")
10915           || ! strcmp (language_string, "GNU Objective-C"))
10916         i = 0;
10917       else if (! strcmp (language_string, "GNU F77"))
10918         i = 1;
10919       else if (! strcmp (language_string, "GNU Ada"))
10920         i = 3;
10921       else if (! strcmp (language_string, "GNU Pascal"))
10922         i = 2;
10923       else if (! strcmp (language_string, "GNU C++"))
10924         i = 9;
10925       else if (! strcmp (language_string, "GNU Java"))
10926         i = 13;
10927       else if (! strcmp (language_string, "GNU CHILL"))
10928         i = 44;
10929       else
10930         abort ();
10931       fprintf (file, "%d,", i);
10932
10933       /* 8 single bit fields: global linkage (not set for C extern linkage,
10934          apparently a PL/I convention?), out-of-line epilogue/prologue, offset
10935          from start of procedure stored in tbtab, internal function, function
10936          has controlled storage, function has no toc, function uses fp,
10937          function logs/aborts fp operations.  */
10938       /* Assume that fp operations are used if any fp reg must be saved.  */
10939       fprintf (file, "%d,",
10940                (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
10941
10942       /* 6 bitfields: function is interrupt handler, name present in
10943          proc table, function calls alloca, on condition directives
10944          (controls stack walks, 3 bits), saves condition reg, saves
10945          link reg.  */
10946       /* The `function calls alloca' bit seems to be set whenever reg 31 is
10947          set up as a frame pointer, even when there is no alloca call.  */
10948       fprintf (file, "%d,",
10949                ((optional_tbtab << 6)
10950                 | ((optional_tbtab & frame_pointer_needed) << 5)
10951                 | (info->cr_save_p << 1)
10952                 | (info->lr_save_p)));
10953
10954       /* 3 bitfields: saves backchain, fixup code, number of fpr saved
10955          (6 bits).  */
10956       fprintf (file, "%d,",
10957                (info->push_p << 7) | (64 - info->first_fp_reg_save));
10958
10959       /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits).  */
10960       fprintf (file, "%d,", (32 - first_reg_to_save ()));
10961
10962       if (optional_tbtab)
10963         {
10964           /* Compute the parameter info from the function decl argument
10965              list.  */
10966           tree decl;
10967           int next_parm_info_bit = 31;
10968
10969           for (decl = DECL_ARGUMENTS (current_function_decl);
10970                decl; decl = TREE_CHAIN (decl))
10971             {
10972               rtx parameter = DECL_INCOMING_RTL (decl);
10973               enum machine_mode mode = GET_MODE (parameter);
10974
10975               if (GET_CODE (parameter) == REG)
10976                 {
10977                   if (GET_MODE_CLASS (mode) == MODE_FLOAT)
10978                     {
10979                       int bits;
10980
10981                       float_parms++;
10982
10983                       if (mode == SFmode)
10984                         bits = 0x2;
10985                       else if (mode == DFmode)
10986                         bits = 0x3;
10987                       else
10988                         abort ();
10989
10990                       /* If only one bit will fit, don't or in this entry.  */
10991                       if (next_parm_info_bit > 0)
10992                         parm_info |= (bits << (next_parm_info_bit - 1));
10993                       next_parm_info_bit -= 2;
10994                     }
10995                   else
10996                     {
10997                       fixed_parms += ((GET_MODE_SIZE (mode)
10998                                        + (UNITS_PER_WORD - 1))
10999                                       / UNITS_PER_WORD);
11000                       next_parm_info_bit -= 1;
11001                     }
11002                 }
11003             }
11004         }
11005
11006       /* Number of fixed point parameters.  */
11007       /* This is actually the number of words of fixed point parameters; thus
11008          an 8 byte struct counts as 2; and thus the maximum value is 8.  */
11009       fprintf (file, "%d,", fixed_parms);
11010
11011       /* 2 bitfields: number of floating point parameters (7 bits), parameters
11012          all on stack.  */
11013       /* This is actually the number of fp registers that hold parameters;
11014          and thus the maximum value is 13.  */
11015       /* Set parameters on stack bit if parameters are not in their original
11016          registers, regardless of whether they are on the stack?  Xlc
11017          seems to set the bit when not optimizing.  */
11018       fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
11019
11020       if (! optional_tbtab)
11021         return;
11022
11023       /* Optional fields follow.  Some are variable length.  */
11024
11025       /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
11026          11 double float.  */
11027       /* There is an entry for each parameter in a register, in the order that
11028          they occur in the parameter list.  Any intervening arguments on the
11029          stack are ignored.  If the list overflows a long (max possible length
11030          34 bits) then completely leave off all elements that don't fit.  */
11031       /* Only emit this long if there was at least one parameter.  */
11032       if (fixed_parms || float_parms)
11033         fprintf (file, "\t.long %d\n", parm_info);
11034
11035       /* Offset from start of code to tb table.  */
11036       fputs ("\t.long ", file);
11037       ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
11038 #if TARGET_AIX
11039       RS6000_OUTPUT_BASENAME (file, fname);
11040 #else
11041       assemble_name (file, fname);
11042 #endif
11043       fputs ("-.", file);
11044 #if TARGET_AIX
11045       RS6000_OUTPUT_BASENAME (file, fname);
11046 #else
11047       assemble_name (file, fname);
11048 #endif
11049       putc ('\n', file);
11050
11051       /* Interrupt handler mask.  */
11052       /* Omit this long, since we never set the interrupt handler bit
11053          above.  */
11054
11055       /* Number of CTL (controlled storage) anchors.  */
11056       /* Omit this long, since the has_ctl bit is never set above.  */
11057
11058       /* Displacement into stack of each CTL anchor.  */
11059       /* Omit this list of longs, because there are no CTL anchors.  */
11060
11061       /* Length of function name.  */
11062       fprintf (file, "\t.short %d\n", (int) strlen (fname));
11063
11064       /* Function name.  */
11065       assemble_string (fname, strlen (fname));
11066
11067       /* Register for alloca automatic storage; this is always reg 31.
11068          Only emit this if the alloca bit was set above.  */
11069       if (frame_pointer_needed)
11070         fputs ("\t.byte 31\n", file);
11071
11072       fputs ("\t.align 2\n", file);
11073     }
11074   return;
11075 }
11076 \f
11077 /* A C compound statement that outputs the assembler code for a thunk
11078    function, used to implement C++ virtual function calls with
11079    multiple inheritance.  The thunk acts as a wrapper around a virtual
11080    function, adjusting the implicit object parameter before handing
11081    control off to the real function.
11082
11083    First, emit code to add the integer DELTA to the location that
11084    contains the incoming first argument.  Assume that this argument
11085    contains a pointer, and is the one used to pass the `this' pointer
11086    in C++.  This is the incoming argument *before* the function
11087    prologue, e.g. `%o0' on a sparc.  The addition must preserve the
11088    values of all other incoming arguments.
11089
11090    After the addition, emit code to jump to FUNCTION, which is a
11091    `FUNCTION_DECL'.  This is a direct pure jump, not a call, and does
11092    not touch the return address.  Hence returning from FUNCTION will
11093    return to whoever called the current `thunk'.
11094
11095    The effect must be as if FUNCTION had been called directly with the
11096    adjusted first argument.  This macro is responsible for emitting
11097    all of the code for a thunk function; output_function_prologue()
11098    and output_function_epilogue() are not invoked.
11099
11100    The THUNK_FNDECL is redundant.  (DELTA and FUNCTION have already
11101    been extracted from it.)  It might possibly be useful on some
11102    targets, but probably not.
11103
11104    If you do not define this macro, the target-independent code in the
11105    C++ frontend will generate a less efficient heavyweight thunk that
11106    calls FUNCTION instead of jumping to it.  The generic approach does
11107    not support varargs.  */
11108
11109 void
11110 output_mi_thunk (file, thunk_fndecl, delta, function)
11111      FILE *file;
11112      tree thunk_fndecl ATTRIBUTE_UNUSED;
11113      int delta;
11114      tree function;
11115 {
11116   const char *this_reg =
11117     reg_names[ aggregate_value_p (TREE_TYPE (TREE_TYPE (function))) ? 4 : 3 ];
11118   const char *prefix;
11119   const char *fname;
11120   const char *r0         = reg_names[0];
11121   const char *toc        = reg_names[2];
11122   const char *schain     = reg_names[11];
11123   const char *r12        = reg_names[12];
11124   char buf[512];
11125   static int labelno = 0;
11126
11127   /* Small constants that can be done by one add instruction.  */
11128   if (delta >= -32768 && delta <= 32767)
11129     {
11130       if (! TARGET_NEW_MNEMONICS)
11131         fprintf (file, "\tcal %s,%d(%s)\n", this_reg, delta, this_reg);
11132       else
11133         fprintf (file, "\taddi %s,%s,%d\n", this_reg, this_reg, delta);
11134     }
11135
11136   /* Large constants that can be done by one addis instruction.  */
11137   else if ((delta & 0xffff) == 0 && num_insns_constant_wide (delta) == 1)
11138     asm_fprintf (file, "\t{cau|addis} %s,%s,%d\n", this_reg, this_reg,
11139                  delta >> 16);
11140
11141   /* 32-bit constants that can be done by an add and addis instruction.  */
11142   else if (TARGET_32BIT || num_insns_constant_wide (delta) == 1)
11143     {
11144       /* Break into two pieces, propagating the sign bit from the low
11145          word to the upper word.  */
11146       int delta_high = delta >> 16;
11147       int delta_low  = delta & 0xffff;
11148       if ((delta_low & 0x8000) != 0)
11149         {
11150           delta_high++;
11151           delta_low = (delta_low ^ 0x8000) - 0x8000;    /* sign extend */
11152         }
11153
11154       asm_fprintf (file, "\t{cau|addis} %s,%s,%d\n", this_reg, this_reg,
11155                    delta_high);
11156
11157       if (! TARGET_NEW_MNEMONICS)
11158         fprintf (file, "\tcal %s,%d(%s)\n", this_reg, delta_low, this_reg);
11159       else
11160         fprintf (file, "\taddi %s,%s,%d\n", this_reg, this_reg, delta_low);
11161     }
11162
11163   /* 64-bit constants, fixme */
11164   else
11165     abort ();
11166
11167   /* Get the prefix in front of the names.  */
11168   switch (DEFAULT_ABI)
11169     {
11170     default:
11171       abort ();
11172
11173     case ABI_AIX:
11174       prefix = ".";
11175       break;
11176
11177     case ABI_V4:
11178     case ABI_AIX_NODESC:
11179       prefix = "";
11180       break;
11181     }
11182
11183   /* If the function is compiled in this module, jump to it directly.
11184      Otherwise, load up its address and jump to it.  */
11185
11186   fname = XSTR (XEXP (DECL_RTL (function), 0), 0);
11187
11188   if (current_file_function_operand (XEXP (DECL_RTL (function), 0), VOIDmode)
11189       && (! lookup_attribute ("longcall",
11190                               TYPE_ATTRIBUTES (TREE_TYPE (function)))
11191           || lookup_attribute ("shortcall",
11192                                TYPE_ATTRIBUTES (TREE_TYPE (function)))))
11193
11194     {
11195       fprintf (file, "\tb %s", prefix);
11196       assemble_name (file, fname);
11197       if (DEFAULT_ABI == ABI_V4 && flag_pic) fputs ("@local", file);
11198       putc ('\n', file);
11199     }
11200
11201   else
11202     {
11203       switch (DEFAULT_ABI)
11204         {
11205         default:
11206           abort ();
11207
11208         case ABI_AIX:
11209           /* Set up a TOC entry for the function.  */
11210           ASM_GENERATE_INTERNAL_LABEL (buf, "Lthunk", labelno);
11211           toc_section ();
11212           ASM_OUTPUT_INTERNAL_LABEL (file, "Lthunk", labelno);
11213           labelno++;
11214
11215           if (TARGET_MINIMAL_TOC)
11216             fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
11217           else
11218             {
11219               fputs ("\t.tc ", file);
11220               assemble_name (file, fname);
11221               fputs ("[TC],", file);
11222             }
11223           assemble_name (file, fname);
11224           putc ('\n', file);
11225           text_section ();
11226           if (TARGET_MINIMAL_TOC)
11227             asm_fprintf (file, (TARGET_32BIT)
11228                          ? "\t{l|lwz} %s,%s(%s)\n" : "\tld %s,%s(%s)\n", r12,
11229                          TARGET_ELF ? ".LCTOC0@toc" : ".LCTOC..1", toc);
11230           asm_fprintf (file, (TARGET_32BIT) ? "\t{l|lwz} %s," : "\tld %s,", r12);
11231           assemble_name (file, buf);
11232           if (TARGET_ELF && TARGET_MINIMAL_TOC)
11233             fputs ("-(.LCTOC1)", file);
11234           asm_fprintf (file, "(%s)\n", TARGET_MINIMAL_TOC ? r12 : toc);
11235           asm_fprintf (file,
11236                        (TARGET_32BIT) ? "\t{l|lwz} %s,0(%s)\n" : "\tld %s,0(%s)\n",
11237                        r0, r12);
11238
11239           asm_fprintf (file,
11240                        (TARGET_32BIT) ? "\t{l|lwz} %s,4(%s)\n" : "\tld %s,8(%s)\n",
11241                        toc, r12);
11242
11243           asm_fprintf (file, "\tmtctr %s\n", r0);
11244           asm_fprintf (file,
11245                        (TARGET_32BIT) ? "\t{l|lwz} %s,8(%s)\n" : "\tld %s,16(%s)\n",
11246                        schain, r12);
11247
11248           asm_fprintf (file, "\tbctr\n");
11249           break;
11250
11251         case ABI_AIX_NODESC:
11252         case ABI_V4:
11253           fprintf (file, "\tb %s", prefix);
11254           assemble_name (file, fname);
11255           if (flag_pic) fputs ("@plt", file);
11256           putc ('\n', file);
11257           break;
11258
11259 #if TARGET_MACHO
11260         case ABI_DARWIN:
11261           fprintf (file, "\tb %s", prefix);
11262           if (flag_pic && !machopic_name_defined_p (fname))
11263             assemble_name (file, machopic_stub_name (fname));
11264           else
11265             assemble_name (file, fname);
11266           putc ('\n', file);
11267           break;
11268 #endif
11269         }
11270     }
11271 }
11272
11273 \f
11274 /* A quick summary of the various types of 'constant-pool tables'
11275    under PowerPC:
11276
11277    Target       Flags           Name            One table per   
11278    AIX          (none)          AIX TOC         object file
11279    AIX          -mfull-toc      AIX TOC         object file
11280    AIX          -mminimal-toc   AIX minimal TOC translation unit
11281    SVR4/EABI    (none)          SVR4 SDATA      object file
11282    SVR4/EABI    -fpic           SVR4 pic        object file
11283    SVR4/EABI    -fPIC           SVR4 PIC        translation unit
11284    SVR4/EABI    -mrelocatable   EABI TOC        function
11285    SVR4/EABI    -maix           AIX TOC         object file
11286    SVR4/EABI    -maix -mminimal-toc 
11287                                 AIX minimal TOC translation unit
11288
11289    Name                 Reg.    Set by  entries       contains:
11290                                         made by  addrs? fp?     sum?
11291
11292    AIX TOC              2       crt0    as       Y      option  option
11293    AIX minimal TOC      30      prolog  gcc      Y      Y       option
11294    SVR4 SDATA           13      crt0    gcc      N      Y       N
11295    SVR4 pic             30      prolog  ld       Y      not yet N
11296    SVR4 PIC             30      prolog  gcc      Y      option  option
11297    EABI TOC             30      prolog  gcc      Y      option  option
11298
11299 */
11300
11301 /* Hash table stuff for keeping track of TOC entries.  */
11302
11303 struct toc_hash_struct 
11304 {
11305   /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
11306      ASM_OUTPUT_SPECIAL_POOL_ENTRY_P.  */
11307   rtx key;
11308   enum machine_mode key_mode;
11309   int labelno;
11310 };
11311
11312 static htab_t toc_hash_table;
11313
11314 /* Hash functions for the hash table.  */
11315
11316 static unsigned
11317 rs6000_hash_constant (k)
11318      rtx k;
11319 {
11320   unsigned result = (GET_CODE (k) << 3) ^ GET_MODE (k);
11321   const char *format = GET_RTX_FORMAT (GET_CODE (k));
11322   int flen = strlen (format);
11323   int fidx;
11324   
11325   if (GET_CODE (k) == LABEL_REF)
11326     return result * 1231 + X0INT (XEXP (k, 0), 3);
11327
11328   if (GET_CODE (k) == CODE_LABEL)
11329     fidx = 3;
11330   else
11331     fidx = 0;
11332
11333   for (; fidx < flen; fidx++)
11334     switch (format[fidx])
11335       {
11336       case 's':
11337         {
11338           unsigned i, len;
11339           const char *str = XSTR (k, fidx);
11340           len = strlen (str);
11341           result = result * 613 + len;
11342           for (i = 0; i < len; i++)
11343             result = result * 613 + (unsigned) str[i];
11344           break;
11345         }
11346       case 'u':
11347       case 'e':
11348         result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
11349         break;
11350       case 'i':
11351       case 'n':
11352         result = result * 613 + (unsigned) XINT (k, fidx);
11353         break;
11354       case 'w':
11355         if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
11356           result = result * 613 + (unsigned) XWINT (k, fidx);
11357         else
11358           {
11359             size_t i;
11360             for (i = 0; i < sizeof(HOST_WIDE_INT)/sizeof(unsigned); i++)
11361               result = result * 613 + (unsigned) (XWINT (k, fidx)
11362                                                   >> CHAR_BIT * i);
11363           }
11364         break;
11365       default:
11366         abort ();
11367       }
11368   return result;
11369 }
11370
11371 static unsigned
11372 toc_hash_function (hash_entry)
11373      const void * hash_entry;
11374 {
11375   const struct toc_hash_struct *thc = 
11376     (const struct toc_hash_struct *) hash_entry;
11377   return rs6000_hash_constant (thc->key) ^ thc->key_mode;
11378 }
11379
11380 /* Compare H1 and H2 for equivalence.  */
11381
11382 static int
11383 toc_hash_eq (h1, h2)
11384      const void * h1;
11385      const void * h2;
11386 {
11387   rtx r1 = ((const struct toc_hash_struct *) h1)->key;
11388   rtx r2 = ((const struct toc_hash_struct *) h2)->key;
11389
11390   if (((const struct toc_hash_struct *) h1)->key_mode
11391       != ((const struct toc_hash_struct *) h2)->key_mode)
11392     return 0;
11393
11394   return rtx_equal_p (r1, r2);
11395 }
11396
11397 /* Mark the hash table-entry HASH_ENTRY.  */
11398
11399 static int
11400 toc_hash_mark_entry (hash_slot, unused)
11401      void ** hash_slot;
11402      void * unused ATTRIBUTE_UNUSED;
11403 {
11404   const struct toc_hash_struct * hash_entry = 
11405     *(const struct toc_hash_struct **) hash_slot;
11406   rtx r = hash_entry->key;
11407   ggc_set_mark (hash_entry);
11408   /* For CODE_LABELS, we don't want to drag in the whole insn chain...  */
11409   if (GET_CODE (r) == LABEL_REF)
11410     {
11411       ggc_set_mark (r);
11412       ggc_set_mark (XEXP (r, 0));
11413     }
11414   else
11415     ggc_mark_rtx (r);
11416   return 1;
11417 }
11418
11419 /* Mark all the elements of the TOC hash-table *HT.  */
11420
11421 static void
11422 toc_hash_mark_table (vht)
11423      void *vht;
11424 {
11425   htab_t *ht = vht;
11426   
11427   htab_traverse (*ht, toc_hash_mark_entry, (void *)0);
11428 }
11429
11430 /* These are the names given by the C++ front-end to vtables, and
11431    vtable-like objects.  Ideally, this logic should not be here;
11432    instead, there should be some programmatic way of inquiring as
11433    to whether or not an object is a vtable.  */
11434
11435 #define VTABLE_NAME_P(NAME)                             \
11436   (strncmp ("_vt.", name, strlen("_vt.")) == 0          \
11437   || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0       \
11438   || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0       \
11439   || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0) 
11440
11441 void
11442 rs6000_output_symbol_ref (file, x)
11443      FILE *file;
11444      rtx x;
11445 {
11446   /* Currently C++ toc references to vtables can be emitted before it
11447      is decided whether the vtable is public or private.  If this is
11448      the case, then the linker will eventually complain that there is
11449      a reference to an unknown section.  Thus, for vtables only, 
11450      we emit the TOC reference to reference the symbol and not the
11451      section.  */
11452   const char *name = XSTR (x, 0);
11453
11454   if (VTABLE_NAME_P (name)) 
11455     {
11456       RS6000_OUTPUT_BASENAME (file, name);
11457     }
11458   else
11459     assemble_name (file, name);
11460 }
11461
11462 /* Output a TOC entry.  We derive the entry name from what is being
11463    written.  */
11464
11465 void
11466 output_toc (file, x, labelno, mode)
11467      FILE *file;
11468      rtx x;
11469      int labelno;
11470      enum machine_mode mode;
11471 {
11472   char buf[256];
11473   const char *name = buf;
11474   const char *real_name;
11475   rtx base = x;
11476   int offset = 0;
11477
11478   if (TARGET_NO_TOC)
11479     abort ();
11480
11481   /* When the linker won't eliminate them, don't output duplicate
11482      TOC entries (this happens on AIX if there is any kind of TOC,
11483      and on SVR4 under -fPIC or -mrelocatable).  */
11484   if (TARGET_TOC)
11485     {
11486       struct toc_hash_struct *h;
11487       void * * found;
11488       
11489       h = ggc_alloc (sizeof (*h));
11490       h->key = x;
11491       h->key_mode = mode;
11492       h->labelno = labelno;
11493       
11494       found = htab_find_slot (toc_hash_table, h, 1);
11495       if (*found == NULL)
11496         *found = h;
11497       else  /* This is indeed a duplicate.  
11498                Set this label equal to that label.  */
11499         {
11500           fputs ("\t.set ", file);
11501           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
11502           fprintf (file, "%d,", labelno);
11503           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
11504           fprintf (file, "%d\n", ((*(const struct toc_hash_struct **) 
11505                                               found)->labelno));
11506           return;
11507         }
11508     }
11509
11510   /* If we're going to put a double constant in the TOC, make sure it's
11511      aligned properly when strict alignment is on.  */
11512   if (GET_CODE (x) == CONST_DOUBLE
11513       && STRICT_ALIGNMENT
11514       && GET_MODE_BITSIZE (mode) >= 64
11515       && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
11516     ASM_OUTPUT_ALIGN (file, 3);
11517   }
11518
11519   ASM_OUTPUT_INTERNAL_LABEL (file, "LC", labelno);
11520
11521   /* Handle FP constants specially.  Note that if we have a minimal
11522      TOC, things we put here aren't actually in the TOC, so we can allow
11523      FP constants.  */
11524   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
11525     {
11526       REAL_VALUE_TYPE rv;
11527       long k[2];
11528
11529       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
11530       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
11531
11532       if (TARGET_64BIT)
11533         {
11534           if (TARGET_MINIMAL_TOC)
11535             fputs (DOUBLE_INT_ASM_OP, file);
11536           else
11537             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
11538                      k[0] & 0xffffffff, k[1] & 0xffffffff);
11539           fprintf (file, "0x%lx%08lx\n",
11540                    k[0] & 0xffffffff, k[1] & 0xffffffff);
11541           return;
11542         }
11543       else
11544         {
11545           if (TARGET_MINIMAL_TOC)
11546             fputs ("\t.long ", file);
11547           else
11548             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
11549                      k[0] & 0xffffffff, k[1] & 0xffffffff);
11550           fprintf (file, "0x%lx,0x%lx\n",
11551                    k[0] & 0xffffffff, k[1] & 0xffffffff);
11552           return;
11553         }
11554     }
11555   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
11556     {
11557       REAL_VALUE_TYPE rv;
11558       long l;
11559
11560       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
11561       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
11562
11563       if (TARGET_64BIT)
11564         {
11565           if (TARGET_MINIMAL_TOC)
11566             fputs (DOUBLE_INT_ASM_OP, file);
11567           else
11568             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
11569           fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
11570           return;
11571         }
11572       else
11573         {
11574           if (TARGET_MINIMAL_TOC)
11575             fputs ("\t.long ", file);
11576           else
11577             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
11578           fprintf (file, "0x%lx\n", l & 0xffffffff);
11579           return;
11580         }
11581     }
11582   else if (GET_MODE (x) == VOIDmode
11583            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
11584     {
11585       unsigned HOST_WIDE_INT low;
11586       HOST_WIDE_INT high;
11587
11588       if (GET_CODE (x) == CONST_DOUBLE)
11589         {
11590           low = CONST_DOUBLE_LOW (x);
11591           high = CONST_DOUBLE_HIGH (x);
11592         }
11593       else
11594 #if HOST_BITS_PER_WIDE_INT == 32
11595         {
11596           low = INTVAL (x);
11597           high = (low & 0x80000000) ? ~0 : 0;
11598         }
11599 #else
11600         {
11601           low = INTVAL (x) & 0xffffffff;
11602           high = (HOST_WIDE_INT) INTVAL (x) >> 32;
11603         }
11604 #endif
11605
11606       /* TOC entries are always Pmode-sized, but since this
11607          is a bigendian machine then if we're putting smaller
11608          integer constants in the TOC we have to pad them.
11609          (This is still a win over putting the constants in
11610          a separate constant pool, because then we'd have
11611          to have both a TOC entry _and_ the actual constant.)
11612
11613          For a 32-bit target, CONST_INT values are loaded and shifted
11614          entirely within `low' and can be stored in one TOC entry.  */
11615
11616       if (TARGET_64BIT && POINTER_SIZE < GET_MODE_BITSIZE (mode))
11617         abort ();/* It would be easy to make this work, but it doesn't now.  */
11618
11619       if (POINTER_SIZE > GET_MODE_BITSIZE (mode))
11620         lshift_double (low, high, POINTER_SIZE - GET_MODE_BITSIZE (mode),
11621                        POINTER_SIZE, &low, &high, 0);
11622
11623       if (TARGET_64BIT)
11624         {
11625           if (TARGET_MINIMAL_TOC)
11626             fputs (DOUBLE_INT_ASM_OP, file);
11627           else
11628             fprintf (file, "\t.tc ID_%lx_%lx[TC],",
11629                      (long) high & 0xffffffff, (long) low & 0xffffffff);
11630           fprintf (file, "0x%lx%08lx\n",
11631                    (long) high & 0xffffffff, (long) low & 0xffffffff);
11632           return;
11633         }
11634       else
11635         {
11636           if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
11637             {
11638               if (TARGET_MINIMAL_TOC)
11639                 fputs ("\t.long ", file);
11640               else
11641                 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
11642                          (long) high & 0xffffffff, (long) low & 0xffffffff);
11643               fprintf (file, "0x%lx,0x%lx\n",
11644                        (long) high & 0xffffffff, (long) low & 0xffffffff);
11645             }
11646           else
11647             {
11648               if (TARGET_MINIMAL_TOC)
11649                 fputs ("\t.long ", file);
11650               else
11651                 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
11652               fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
11653             }
11654           return;
11655         }
11656     }
11657
11658   if (GET_CODE (x) == CONST)
11659     {
11660       if (GET_CODE (XEXP (x, 0)) != PLUS)
11661         abort ();
11662
11663       base = XEXP (XEXP (x, 0), 0);
11664       offset = INTVAL (XEXP (XEXP (x, 0), 1));
11665     }
11666   
11667   if (GET_CODE (base) == SYMBOL_REF)
11668     name = XSTR (base, 0);
11669   else if (GET_CODE (base) == LABEL_REF)
11670     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (XEXP (base, 0)));
11671   else if (GET_CODE (base) == CODE_LABEL)
11672     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
11673   else
11674     abort ();
11675
11676   real_name = (*targetm.strip_name_encoding) (name);
11677   if (TARGET_MINIMAL_TOC)
11678     fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
11679   else
11680     {
11681       fprintf (file, "\t.tc %s", real_name);
11682
11683       if (offset < 0)
11684         fprintf (file, ".N%d", - offset);
11685       else if (offset)
11686         fprintf (file, ".P%d", offset);
11687
11688       fputs ("[TC],", file);
11689     }
11690
11691   /* Currently C++ toc references to vtables can be emitted before it
11692      is decided whether the vtable is public or private.  If this is
11693      the case, then the linker will eventually complain that there is
11694      a TOC reference to an unknown section.  Thus, for vtables only,
11695      we emit the TOC reference to reference the symbol and not the
11696      section.  */
11697   if (VTABLE_NAME_P (name))
11698     {
11699       RS6000_OUTPUT_BASENAME (file, name);
11700       if (offset < 0)
11701         fprintf (file, "%d", offset);
11702       else if (offset > 0)
11703         fprintf (file, "+%d", offset);
11704     }
11705   else
11706     output_addr_const (file, x);
11707   putc ('\n', file);
11708 }
11709 \f
11710 /* Output an assembler pseudo-op to write an ASCII string of N characters
11711    starting at P to FILE.
11712
11713    On the RS/6000, we have to do this using the .byte operation and
11714    write out special characters outside the quoted string.
11715    Also, the assembler is broken; very long strings are truncated,
11716    so we must artificially break them up early.  */
11717
11718 void
11719 output_ascii (file, p, n)
11720      FILE *file;
11721      const char *p;
11722      int n;
11723 {
11724   char c;
11725   int i, count_string;
11726   const char *for_string = "\t.byte \"";
11727   const char *for_decimal = "\t.byte ";
11728   const char *to_close = NULL;
11729
11730   count_string = 0;
11731   for (i = 0; i < n; i++)
11732     {
11733       c = *p++;
11734       if (c >= ' ' && c < 0177)
11735         {
11736           if (for_string)
11737             fputs (for_string, file);
11738           putc (c, file);
11739
11740           /* Write two quotes to get one.  */
11741           if (c == '"')
11742             {
11743               putc (c, file);
11744               ++count_string;
11745             }
11746
11747           for_string = NULL;
11748           for_decimal = "\"\n\t.byte ";
11749           to_close = "\"\n";
11750           ++count_string;
11751
11752           if (count_string >= 512)
11753             {
11754               fputs (to_close, file);
11755
11756               for_string = "\t.byte \"";
11757               for_decimal = "\t.byte ";
11758               to_close = NULL;
11759               count_string = 0;
11760             }
11761         }
11762       else
11763         {
11764           if (for_decimal)
11765             fputs (for_decimal, file);
11766           fprintf (file, "%d", c);
11767
11768           for_string = "\n\t.byte \"";
11769           for_decimal = ", ";
11770           to_close = "\n";
11771           count_string = 0;
11772         }
11773     }
11774
11775   /* Now close the string if we have written one.  Then end the line.  */
11776   if (to_close)
11777     fputs (to_close, file);
11778 }
11779 \f
11780 /* Generate a unique section name for FILENAME for a section type
11781    represented by SECTION_DESC.  Output goes into BUF.
11782
11783    SECTION_DESC can be any string, as long as it is different for each
11784    possible section type.
11785
11786    We name the section in the same manner as xlc.  The name begins with an
11787    underscore followed by the filename (after stripping any leading directory
11788    names) with the last period replaced by the string SECTION_DESC.  If
11789    FILENAME does not contain a period, SECTION_DESC is appended to the end of
11790    the name.  */
11791
11792 void
11793 rs6000_gen_section_name (buf, filename, section_desc)
11794      char **buf;
11795      const char *filename;
11796      const char *section_desc;
11797 {
11798   const char *q, *after_last_slash, *last_period = 0;
11799   char *p;
11800   int len;
11801
11802   after_last_slash = filename;
11803   for (q = filename; *q; q++)
11804     {
11805       if (*q == '/')
11806         after_last_slash = q + 1;
11807       else if (*q == '.')
11808         last_period = q;
11809     }
11810
11811   len = strlen (after_last_slash) + strlen (section_desc) + 2;
11812   *buf = (char *) permalloc (len);
11813
11814   p = *buf;
11815   *p++ = '_';
11816
11817   for (q = after_last_slash; *q; q++)
11818     {
11819       if (q == last_period)
11820         {
11821           strcpy (p, section_desc);
11822           p += strlen (section_desc);
11823         }
11824
11825       else if (ISALNUM (*q))
11826         *p++ = *q;
11827     }
11828
11829   if (last_period == 0)
11830     strcpy (p, section_desc);
11831   else
11832     *p = '\0';
11833 }
11834 \f
11835 /* Emit profile function.  */
11836
11837 void
11838 output_profile_hook (labelno)
11839      int labelno;
11840 {
11841   if (DEFAULT_ABI == ABI_AIX)
11842     {
11843       char buf[30];
11844       const char *label_name;
11845       rtx fun;
11846
11847       ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
11848       label_name = (*targetm.strip_name_encoding) (ggc_strdup (buf));
11849       fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
11850
11851       emit_library_call (init_one_libfunc (RS6000_MCOUNT), 0, VOIDmode, 1,
11852                          fun, Pmode);
11853     }
11854   else if (DEFAULT_ABI == ABI_DARWIN)
11855     {
11856       const char *mcount_name = RS6000_MCOUNT;
11857       int caller_addr_regno = LINK_REGISTER_REGNUM;
11858
11859       /* Be conservative and always set this, at least for now.  */
11860       current_function_uses_pic_offset_table = 1;
11861
11862 #if TARGET_MACHO
11863       /* For PIC code, set up a stub and collect the caller's address
11864          from r0, which is where the prologue puts it.  */
11865       if (flag_pic)
11866         {
11867           mcount_name = machopic_stub_name (mcount_name);
11868           if (current_function_uses_pic_offset_table)
11869             caller_addr_regno = 0;
11870         }
11871 #endif
11872       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
11873                          0, VOIDmode, 1,
11874                          gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
11875     }
11876 }
11877
11878 /* Write function profiler code.  */
11879
11880 void
11881 output_function_profiler (file, labelno)
11882   FILE *file;
11883   int labelno;
11884 {
11885   char buf[100];
11886   int save_lr = 8;
11887
11888   ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
11889   switch (DEFAULT_ABI)
11890     {
11891     default:
11892       abort ();
11893
11894     case ABI_V4:
11895       save_lr = 4;
11896       /* Fall through.  */
11897
11898     case ABI_AIX_NODESC:
11899       if (!TARGET_32BIT)
11900         {
11901           warning ("no profiling of 64-bit code for this ABI");
11902           return;
11903         }
11904       fprintf (file, "\tmflr %s\n", reg_names[0]);
11905       if (flag_pic == 1)
11906         {
11907           fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
11908           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
11909                        reg_names[0], save_lr, reg_names[1]);
11910           asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
11911           asm_fprintf (file, "\t{l|lwz} %s,", reg_names[0]);
11912           assemble_name (file, buf);
11913           asm_fprintf (file, "@got(%s)\n", reg_names[12]);
11914         }
11915       else if (flag_pic > 1)
11916         {
11917           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
11918                        reg_names[0], save_lr, reg_names[1]);
11919           /* Now, we need to get the address of the label.  */
11920           fputs ("\tbl 1f\n\t.long ", file);
11921           assemble_name (file, buf);
11922           fputs ("-.\n1:", file);
11923           asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
11924           asm_fprintf (file, "\t{l|lwz} %s,0(%s)\n", 
11925                        reg_names[0], reg_names[11]);
11926           asm_fprintf (file, "\t{cax|add} %s,%s,%s\n",
11927                        reg_names[0], reg_names[0], reg_names[11]);
11928         }
11929       else
11930         {
11931           asm_fprintf (file, "\t{liu|lis} %s,", reg_names[12]);
11932           assemble_name (file, buf);
11933           fputs ("@ha\n", file);
11934           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
11935                        reg_names[0], save_lr, reg_names[1]);
11936           asm_fprintf (file, "\t{cal|la} %s,", reg_names[0]);
11937           assemble_name (file, buf);
11938           asm_fprintf (file, "@l(%s)\n", reg_names[12]);
11939         }
11940
11941       if (current_function_needs_context && DEFAULT_ABI == ABI_AIX_NODESC)
11942         {
11943           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
11944                        reg_names[STATIC_CHAIN_REGNUM],
11945                        12, reg_names[1]);
11946           fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
11947           asm_fprintf (file, "\t{l|lwz} %s,%d(%s)\n",
11948                        reg_names[STATIC_CHAIN_REGNUM],
11949                        12, reg_names[1]);
11950         }
11951       else
11952         /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH.  */
11953         fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
11954       break;
11955
11956     case ABI_AIX:
11957     case ABI_DARWIN:
11958       /* Don't do anything, done in output_profile_hook ().  */
11959       break;
11960     }
11961 }
11962
11963 /* Adjust the cost of a scheduling dependency.  Return the new cost of
11964    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
11965
11966 static int
11967 rs6000_adjust_cost (insn, link, dep_insn, cost)
11968      rtx insn;
11969      rtx link;
11970      rtx dep_insn ATTRIBUTE_UNUSED;
11971      int cost;
11972 {
11973   if (! recog_memoized (insn))
11974     return 0;
11975
11976   if (REG_NOTE_KIND (link) != 0)
11977     return 0;
11978
11979   if (REG_NOTE_KIND (link) == 0)
11980     {
11981       /* Data dependency; DEP_INSN writes a register that INSN reads
11982          some cycles later.  */
11983       switch (get_attr_type (insn))
11984         {
11985         case TYPE_JMPREG:
11986           /* Tell the first scheduling pass about the latency between
11987              a mtctr and bctr (and mtlr and br/blr).  The first
11988              scheduling pass will not know about this latency since
11989              the mtctr instruction, which has the latency associated
11990              to it, will be generated by reload.  */
11991           return TARGET_POWER ? 5 : 4;
11992         case TYPE_BRANCH:
11993           /* Leave some extra cycles between a compare and its
11994              dependent branch, to inhibit expensive mispredicts.  */
11995           if ((rs6000_cpu_attr == CPU_PPC603
11996                || rs6000_cpu_attr == CPU_PPC604
11997                || rs6000_cpu_attr == CPU_PPC604E
11998                || rs6000_cpu_attr == CPU_PPC620
11999                || rs6000_cpu_attr == CPU_PPC630
12000                || rs6000_cpu_attr == CPU_PPC750
12001                || rs6000_cpu_attr == CPU_PPC7400
12002                || rs6000_cpu_attr == CPU_PPC7450
12003                || rs6000_cpu_attr == CPU_POWER4)
12004               && recog_memoized (dep_insn)
12005               && (INSN_CODE (dep_insn) >= 0)
12006               && (get_attr_type (dep_insn) == TYPE_COMPARE
12007                   || get_attr_type (dep_insn) == TYPE_DELAYED_COMPARE
12008                   || get_attr_type (dep_insn) == TYPE_FPCOMPARE
12009                   || get_attr_type (dep_insn) == TYPE_CR_LOGICAL))
12010             return cost + 2;
12011         default:
12012           break;
12013         }
12014       /* Fall out to return default cost.  */
12015     }
12016
12017   return cost;
12018 }
12019
12020 /* A C statement (sans semicolon) to update the integer scheduling
12021    priority INSN_PRIORITY (INSN).  Reduce the priority to execute the
12022    INSN earlier, increase the priority to execute INSN later.  Do not
12023    define this macro if you do not need to adjust the scheduling
12024    priorities of insns.  */
12025
12026 static int
12027 rs6000_adjust_priority (insn, priority)
12028      rtx insn ATTRIBUTE_UNUSED;
12029      int priority;
12030 {
12031   /* On machines (like the 750) which have asymmetric integer units,
12032      where one integer unit can do multiply and divides and the other
12033      can't, reduce the priority of multiply/divide so it is scheduled
12034      before other integer operations.  */
12035
12036 #if 0
12037   if (! INSN_P (insn))
12038     return priority;
12039
12040   if (GET_CODE (PATTERN (insn)) == USE)
12041     return priority;
12042
12043   switch (rs6000_cpu_attr) {
12044   case CPU_PPC750:
12045     switch (get_attr_type (insn))
12046       {
12047       default:
12048         break;
12049
12050       case TYPE_IMUL:
12051       case TYPE_IDIV:
12052         fprintf (stderr, "priority was %#x (%d) before adjustment\n",
12053                  priority, priority);
12054         if (priority >= 0 && priority < 0x01000000)
12055           priority >>= 3;
12056         break;
12057       }
12058   }
12059 #endif
12060
12061   return priority;
12062 }
12063
12064 /* Return how many instructions the machine can issue per cycle.  */
12065
12066 static int
12067 rs6000_issue_rate ()
12068 {
12069   switch (rs6000_cpu_attr) {
12070   case CPU_RIOS1:  /* ? */
12071   case CPU_RS64A:
12072   case CPU_PPC601: /* ? */
12073   case CPU_PPC7450:
12074     return 3;
12075   case CPU_PPC603:
12076   case CPU_PPC750:
12077   case CPU_PPC7400:
12078     return 2; 
12079   case CPU_RIOS2:
12080   case CPU_PPC604:
12081   case CPU_PPC604E:
12082   case CPU_PPC620:
12083   case CPU_PPC630:
12084   case CPU_POWER4:
12085     return 4;
12086   default:
12087     return 1;
12088   }
12089 }
12090
12091 \f
12092 /* Length in units of the trampoline for entering a nested function.  */
12093
12094 int
12095 rs6000_trampoline_size ()
12096 {
12097   int ret = 0;
12098
12099   switch (DEFAULT_ABI)
12100     {
12101     default:
12102       abort ();
12103
12104     case ABI_AIX:
12105       ret = (TARGET_32BIT) ? 12 : 24;
12106       break;
12107
12108     case ABI_DARWIN:
12109     case ABI_V4:
12110     case ABI_AIX_NODESC:
12111       ret = (TARGET_32BIT) ? 40 : 48;
12112       break;
12113     }
12114
12115   return ret;
12116 }
12117
12118 /* Emit RTL insns to initialize the variable parts of a trampoline.
12119    FNADDR is an RTX for the address of the function's pure code.
12120    CXT is an RTX for the static chain value for the function.  */
12121
12122 void
12123 rs6000_initialize_trampoline (addr, fnaddr, cxt)
12124      rtx addr;
12125      rtx fnaddr;
12126      rtx cxt;
12127 {
12128   enum machine_mode pmode = Pmode;
12129   int regsize = (TARGET_32BIT) ? 4 : 8;
12130   rtx ctx_reg = force_reg (pmode, cxt);
12131
12132   switch (DEFAULT_ABI)
12133     {
12134     default:
12135       abort ();
12136
12137 /* Macros to shorten the code expansions below.  */
12138 #define MEM_DEREF(addr) gen_rtx_MEM (pmode, memory_address (pmode, addr))
12139 #define MEM_PLUS(addr,offset) \
12140   gen_rtx_MEM (pmode, memory_address (pmode, plus_constant (addr, offset)))
12141
12142     /* Under AIX, just build the 3 word function descriptor */
12143     case ABI_AIX:
12144       {
12145         rtx fn_reg = gen_reg_rtx (pmode);
12146         rtx toc_reg = gen_reg_rtx (pmode);
12147         emit_move_insn (fn_reg, MEM_DEREF (fnaddr));
12148         emit_move_insn (toc_reg, MEM_PLUS (fnaddr, regsize));
12149         emit_move_insn (MEM_DEREF (addr), fn_reg);
12150         emit_move_insn (MEM_PLUS (addr, regsize), toc_reg);
12151         emit_move_insn (MEM_PLUS (addr, 2*regsize), ctx_reg);
12152       }
12153       break;
12154
12155     /* Under V.4/eabi/darwin, __trampoline_setup does the real work.  */
12156     case ABI_DARWIN:
12157     case ABI_V4:
12158     case ABI_AIX_NODESC:
12159       emit_library_call (gen_rtx_SYMBOL_REF (SImode, "__trampoline_setup"),
12160                          FALSE, VOIDmode, 4,
12161                          addr, pmode,
12162                          GEN_INT (rs6000_trampoline_size ()), SImode,
12163                          fnaddr, pmode,
12164                          ctx_reg, pmode);
12165       break;
12166     }
12167
12168   return;
12169 }
12170
12171 \f
12172 /* Table of valid machine attributes.  */
12173
12174 const struct attribute_spec rs6000_attribute_table[] =
12175 {
12176   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
12177   { "longcall",  0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
12178   { "shortcall", 0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
12179   { NULL,        0, 0, false, false, false, NULL }
12180 };
12181
12182 /* Handle a "longcall" or "shortcall" attribute; arguments as in
12183    struct attribute_spec.handler.  */
12184
12185 static tree
12186 rs6000_handle_longcall_attribute (node, name, args, flags, no_add_attrs)
12187      tree *node;
12188      tree name;
12189      tree args ATTRIBUTE_UNUSED;
12190      int flags ATTRIBUTE_UNUSED;
12191      bool *no_add_attrs;
12192 {
12193   if (TREE_CODE (*node) != FUNCTION_TYPE
12194       && TREE_CODE (*node) != FIELD_DECL
12195       && TREE_CODE (*node) != TYPE_DECL)
12196     {
12197       warning ("`%s' attribute only applies to functions",
12198                IDENTIFIER_POINTER (name));
12199       *no_add_attrs = true;
12200     }
12201
12202   return NULL_TREE;
12203 }
12204
12205 /* Set longcall attributes on all functions declared when
12206    rs6000_default_long_calls is true.  */
12207 static void
12208 rs6000_set_default_type_attributes (type)
12209      tree type;
12210 {
12211   if (rs6000_default_long_calls
12212       && (TREE_CODE (type) == FUNCTION_TYPE
12213           || TREE_CODE (type) == METHOD_TYPE))
12214     TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
12215                                         NULL_TREE,
12216                                         TYPE_ATTRIBUTES (type));
12217 }
12218
12219 /* Return a reference suitable for calling a function with the
12220    longcall attribute.  */
12221
12222 struct rtx_def *
12223 rs6000_longcall_ref (call_ref)
12224      rtx call_ref;
12225 {
12226   const char *call_name;
12227   tree node;
12228
12229   if (GET_CODE (call_ref) != SYMBOL_REF)
12230     return call_ref;
12231
12232   /* System V adds '.' to the internal name, so skip them.  */
12233   call_name = XSTR (call_ref, 0);
12234   if (*call_name == '.')
12235     {
12236       while (*call_name == '.')
12237         call_name++;
12238
12239       node = get_identifier (call_name);
12240       call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
12241     }
12242
12243   return force_reg (Pmode, call_ref);
12244 }
12245
12246 \f
12247 #ifdef USING_ELFOS_H
12248
12249 /* A C statement or statements to switch to the appropriate section
12250    for output of RTX in mode MODE.  You can assume that RTX is some
12251    kind of constant in RTL.  The argument MODE is redundant except in
12252    the case of a `const_int' rtx.  Select the section by calling
12253    `text_section' or one of the alternatives for other sections.
12254
12255    Do not define this macro if you put all constants in the read-only
12256    data section.  */
12257
12258 static void
12259 rs6000_elf_select_rtx_section (mode, x, align)
12260      enum machine_mode mode;
12261      rtx x;
12262      unsigned HOST_WIDE_INT align;
12263 {
12264   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
12265     toc_section ();
12266   else
12267     default_elf_select_rtx_section (mode, x, align);
12268 }
12269
12270 /* A C statement or statements to switch to the appropriate
12271    section for output of DECL.  DECL is either a `VAR_DECL' node
12272    or a constant of some sort.  RELOC indicates whether forming
12273    the initial value of DECL requires link-time relocations.  */
12274
12275 static void
12276 rs6000_elf_select_section (decl, reloc, align)
12277      tree decl;
12278      int reloc;
12279      unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
12280 {
12281   int size = int_size_in_bytes (TREE_TYPE (decl));
12282   int needs_sdata;
12283   int readonly;
12284   static void (* const sec_funcs[4]) PARAMS ((void)) = {
12285     &readonly_data_section,
12286     &sdata2_section,
12287     &data_section,
12288     &sdata_section
12289   };
12290   
12291   needs_sdata = (size > 0 
12292                  && size <= g_switch_value
12293                  && rs6000_sdata != SDATA_NONE
12294                  && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)));
12295
12296   if (TREE_CODE (decl) == STRING_CST)
12297     readonly = ! flag_writable_strings;
12298   else if (TREE_CODE (decl) == VAR_DECL)
12299     readonly = (! (flag_pic && reloc)
12300                 && TREE_READONLY (decl)
12301                 && ! TREE_SIDE_EFFECTS (decl)
12302                 && DECL_INITIAL (decl)
12303                 && DECL_INITIAL (decl) != error_mark_node
12304                 && TREE_CONSTANT (DECL_INITIAL (decl)));
12305   else if (TREE_CODE (decl) == CONSTRUCTOR)
12306     readonly = (! (flag_pic && reloc)
12307                 && ! TREE_SIDE_EFFECTS (decl)
12308                 && TREE_CONSTANT (decl));
12309   else
12310     readonly = 1;
12311   if (needs_sdata && rs6000_sdata != SDATA_EABI)
12312     readonly = 0;
12313   
12314   (*sec_funcs[(readonly ? 0 : 2) + (needs_sdata ? 1 : 0)])();
12315 }
12316
12317 /* A C statement to build up a unique section name, expressed as a
12318    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
12319    RELOC indicates whether the initial value of EXP requires
12320    link-time relocations.  If you do not define this macro, GCC will use
12321    the symbol name prefixed by `.' as the section name.  Note - this
12322    macro can now be called for uninitialized data items as well as
12323    initialised data and functions.  */
12324
12325 static void
12326 rs6000_elf_unique_section (decl, reloc)
12327      tree decl;
12328      int reloc;
12329 {
12330   int len;
12331   int sec;
12332   const char *name;
12333   char *string;
12334   const char *prefix;
12335
12336   static const char *const prefixes[7][2] =
12337   {
12338     { ".rodata.", ".gnu.linkonce.r." },
12339     { ".sdata2.", ".gnu.linkonce.s2." },
12340     { ".data.",   ".gnu.linkonce.d." },
12341     { ".sdata.",  ".gnu.linkonce.s." },
12342     { ".bss.",    ".gnu.linkonce.b." },
12343     { ".sbss.",   ".gnu.linkonce.sb." },
12344     { ".text.",   ".gnu.linkonce.t." }
12345   };
12346
12347   if (TREE_CODE (decl) == FUNCTION_DECL)
12348     sec = 6;
12349   else
12350     {
12351       int readonly;
12352       int needs_sdata;
12353       int size;
12354
12355       readonly = 1;
12356       if (TREE_CODE (decl) == STRING_CST)
12357         readonly = ! flag_writable_strings;
12358       else if (TREE_CODE (decl) == VAR_DECL)
12359         readonly = (! (flag_pic && reloc)
12360                     && TREE_READONLY (decl)
12361                     && ! TREE_SIDE_EFFECTS (decl)
12362                     && TREE_CONSTANT (DECL_INITIAL (decl)));
12363
12364       size = int_size_in_bytes (TREE_TYPE (decl));
12365       needs_sdata = (size > 0 
12366                      && size <= g_switch_value
12367                      && rs6000_sdata != SDATA_NONE
12368                      && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)));
12369
12370       if (DECL_INITIAL (decl) == 0
12371           || DECL_INITIAL (decl) == error_mark_node)
12372         sec = 4;
12373       else if (! readonly)
12374         sec = 2;
12375       else
12376         sec = 0;
12377
12378       if (needs_sdata)
12379         {
12380           /* .sdata2 is only for EABI.  */
12381           if (sec == 0 && rs6000_sdata != SDATA_EABI)
12382             sec = 2;
12383           sec += 1;
12384         }
12385     }
12386
12387   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
12388   name = (*targetm.strip_name_encoding) (name);
12389   prefix = prefixes[sec][DECL_ONE_ONLY (decl)];
12390   len    = strlen (name) + strlen (prefix);
12391   string = alloca (len + 1);
12392   
12393   sprintf (string, "%s%s", prefix, name);
12394   
12395   DECL_SECTION_NAME (decl) = build_string (len, string);
12396 }
12397
12398 \f
12399 /* If we are referencing a function that is static or is known to be
12400    in this file, make the SYMBOL_REF special.  We can use this to indicate
12401    that we can branch to this function without emitting a no-op after the
12402    call.  For real AIX calling sequences, we also replace the
12403    function name with the real name (1 or 2 leading .'s), rather than
12404    the function descriptor name.  This saves a lot of overriding code
12405    to read the prefixes.  */
12406
12407 static void
12408 rs6000_elf_encode_section_info (decl, first)
12409      tree decl;
12410      int first;
12411 {
12412   if (!first)
12413     return;
12414
12415   if (TREE_CODE (decl) == FUNCTION_DECL)
12416     {
12417       rtx sym_ref = XEXP (DECL_RTL (decl), 0);
12418       if ((TREE_ASM_WRITTEN (decl) || ! TREE_PUBLIC (decl))
12419           && ! DECL_WEAK (decl))
12420         SYMBOL_REF_FLAG (sym_ref) = 1;
12421
12422       if (DEFAULT_ABI == ABI_AIX)
12423         {
12424           size_t len1 = (DEFAULT_ABI == ABI_AIX) ? 1 : 2;
12425           size_t len2 = strlen (XSTR (sym_ref, 0));
12426           char *str = alloca (len1 + len2 + 1);
12427           str[0] = '.';
12428           str[1] = '.';
12429           memcpy (str + len1, XSTR (sym_ref, 0), len2 + 1);
12430
12431           XSTR (sym_ref, 0) = ggc_alloc_string (str, len1 + len2);
12432         }
12433     }
12434   else if (rs6000_sdata != SDATA_NONE
12435            && DEFAULT_ABI == ABI_V4
12436            && TREE_CODE (decl) == VAR_DECL)
12437     {
12438       int size = int_size_in_bytes (TREE_TYPE (decl));
12439       tree section_name = DECL_SECTION_NAME (decl);
12440       const char *name = (char *)0;
12441       int len = 0;
12442
12443       if (section_name)
12444         {
12445           if (TREE_CODE (section_name) == STRING_CST)
12446             {
12447               name = TREE_STRING_POINTER (section_name);
12448               len = TREE_STRING_LENGTH (section_name);
12449             }
12450           else
12451             abort ();
12452         }
12453
12454       if ((size > 0 && size <= g_switch_value)
12455           || (name
12456               && ((len == sizeof (".sdata") - 1
12457                    && strcmp (name, ".sdata") == 0)
12458                   || (len == sizeof (".sdata2") - 1
12459                       && strcmp (name, ".sdata2") == 0)
12460                   || (len == sizeof (".sbss") - 1
12461                       && strcmp (name, ".sbss") == 0)
12462                   || (len == sizeof (".sbss2") - 1
12463                       && strcmp (name, ".sbss2") == 0)
12464                   || (len == sizeof (".PPC.EMB.sdata0") - 1
12465                       && strcmp (name, ".PPC.EMB.sdata0") == 0)
12466                   || (len == sizeof (".PPC.EMB.sbss0") - 1
12467                       && strcmp (name, ".PPC.EMB.sbss0") == 0))))
12468         {
12469           rtx sym_ref = XEXP (DECL_RTL (decl), 0);
12470           size_t len = strlen (XSTR (sym_ref, 0));
12471           char *str = alloca (len + 2);
12472
12473           str[0] = '@';
12474           memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
12475           XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
12476         }
12477     }
12478 }
12479
12480 static const char *
12481 rs6000_elf_strip_name_encoding (str)
12482      const char *str;
12483 {
12484   while (*str == '*' || *str == '@')
12485     str++;
12486   return str;
12487 }
12488
12489 #endif /* USING_ELFOS_H */
12490
12491 \f
12492 /* Return a REG that occurs in ADDR with coefficient 1.
12493    ADDR can be effectively incremented by incrementing REG.
12494
12495    r0 is special and we must not select it as an address
12496    register by this routine since our caller will try to
12497    increment the returned register via an "la" instruction.  */
12498
12499 struct rtx_def *
12500 find_addr_reg (addr)
12501      rtx addr;
12502 {
12503   while (GET_CODE (addr) == PLUS)
12504     {
12505       if (GET_CODE (XEXP (addr, 0)) == REG
12506           && REGNO (XEXP (addr, 0)) != 0)
12507         addr = XEXP (addr, 0);
12508       else if (GET_CODE (XEXP (addr, 1)) == REG
12509                && REGNO (XEXP (addr, 1)) != 0)
12510         addr = XEXP (addr, 1);
12511       else if (CONSTANT_P (XEXP (addr, 0)))
12512         addr = XEXP (addr, 1);
12513       else if (CONSTANT_P (XEXP (addr, 1)))
12514         addr = XEXP (addr, 0);
12515       else
12516         abort ();
12517     }
12518   if (GET_CODE (addr) == REG && REGNO (addr) != 0)
12519     return addr;
12520   abort ();
12521 }
12522
12523 void
12524 rs6000_fatal_bad_address (op)
12525   rtx op;
12526 {
12527   fatal_insn ("bad address", op);
12528 }
12529
12530 /* Called to register all of our global variables with the garbage
12531    collector.  */
12532
12533 static void
12534 rs6000_add_gc_roots ()
12535 {
12536   toc_hash_table = htab_create (1021, toc_hash_function, toc_hash_eq, NULL);
12537   ggc_add_root (&toc_hash_table, 1, sizeof (toc_hash_table), 
12538                 toc_hash_mark_table);
12539 }
12540
12541 #if TARGET_MACHO
12542
12543 #if 0
12544 /* Returns 1 if OP is either a symbol reference or a sum of a symbol
12545    reference and a constant.  */
12546
12547 int
12548 symbolic_operand (op)
12549      rtx op;
12550 {
12551   switch (GET_CODE (op))
12552     {
12553     case SYMBOL_REF:
12554     case LABEL_REF:
12555       return 1;
12556     case CONST:
12557       op = XEXP (op, 0);
12558       return (GET_CODE (op) == SYMBOL_REF ||
12559               (GET_CODE (XEXP (op, 0)) == SYMBOL_REF
12560                || GET_CODE (XEXP (op, 0)) == LABEL_REF)
12561               && GET_CODE (XEXP (op, 1)) == CONST_INT);
12562     default:
12563       return 0;
12564     }
12565 }
12566 #endif
12567
12568 #ifdef RS6000_LONG_BRANCH
12569
12570 static tree stub_list = 0;
12571
12572 /* ADD_COMPILER_STUB adds the compiler generated stub for handling 
12573    procedure calls to the linked list.  */
12574
12575 void 
12576 add_compiler_stub (label_name, function_name, line_number)
12577      tree label_name;
12578      tree function_name;
12579      int line_number;
12580 {
12581   tree stub = build_tree_list (function_name, label_name);
12582   TREE_TYPE (stub) = build_int_2 (line_number, 0);
12583   TREE_CHAIN (stub) = stub_list;
12584   stub_list = stub;
12585 }
12586
12587 #define STUB_LABEL_NAME(STUB)     TREE_VALUE (STUB)
12588 #define STUB_FUNCTION_NAME(STUB)  TREE_PURPOSE (STUB)
12589 #define STUB_LINE_NUMBER(STUB)    TREE_INT_CST_LOW (TREE_TYPE (STUB))
12590
12591 /* OUTPUT_COMPILER_STUB outputs the compiler generated stub for
12592    handling procedure calls from the linked list and initializes the
12593    linked list.  */
12594
12595 void
12596 output_compiler_stub ()
12597 {
12598   char tmp_buf[256];
12599   char label_buf[256];
12600   char *label;
12601   tree tmp_stub, stub;
12602
12603   if (!flag_pic)
12604     for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
12605       {
12606         fprintf (asm_out_file,
12607                  "%s:\n", IDENTIFIER_POINTER(STUB_LABEL_NAME(stub)));
12608
12609 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
12610         if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
12611           fprintf (asm_out_file, "\t.stabd 68,0,%d\n", STUB_LINE_NUMBER(stub));
12612 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
12613
12614         if (IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub))[0] == '*')
12615           strcpy (label_buf,
12616                   IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub))+1);
12617         else
12618           {
12619             label_buf[0] = '_';
12620             strcpy (label_buf+1,
12621                     IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub)));
12622           }
12623
12624         strcpy (tmp_buf, "lis r12,hi16(");
12625         strcat (tmp_buf, label_buf);
12626         strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
12627         strcat (tmp_buf, label_buf);
12628         strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
12629         output_asm_insn (tmp_buf, 0);
12630
12631 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
12632         if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
12633           fprintf(asm_out_file, "\t.stabd 68,0,%d\n", STUB_LINE_NUMBER (stub));
12634 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
12635       }
12636
12637   stub_list = 0;
12638 }
12639
12640 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
12641    already there or not.  */
12642
12643 int
12644 no_previous_def (function_name)
12645      tree function_name;
12646 {
12647   tree stub;
12648   for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
12649     if (function_name == STUB_FUNCTION_NAME (stub))
12650       return 0;
12651   return 1;
12652 }
12653
12654 /* GET_PREV_LABEL gets the label name from the previous definition of
12655    the function.  */
12656
12657 tree
12658 get_prev_label (function_name)
12659      tree function_name;
12660 {
12661   tree stub;
12662   for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
12663     if (function_name == STUB_FUNCTION_NAME (stub))
12664       return STUB_LABEL_NAME (stub);
12665   return 0;
12666 }
12667
12668 /* INSN is either a function call or a millicode call.  It may have an
12669    unconditional jump in its delay slot.  
12670
12671    CALL_DEST is the routine we are calling.  */
12672
12673 char *
12674 output_call (insn, call_dest, operand_number)
12675      rtx insn;
12676      rtx call_dest;
12677      int operand_number;
12678 {
12679   static char buf[256];
12680   if (GET_CODE (call_dest) == SYMBOL_REF && TARGET_LONG_BRANCH && !flag_pic)
12681     {
12682       tree labelname;
12683       tree funname = get_identifier (XSTR (call_dest, 0));
12684       
12685       if (no_previous_def (funname))
12686         {
12687           int line_number;
12688           rtx label_rtx = gen_label_rtx ();
12689           char *label_buf, temp_buf[256];
12690           ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
12691                                        CODE_LABEL_NUMBER (label_rtx));
12692           label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
12693           labelname = get_identifier (label_buf);
12694           for (; insn && GET_CODE (insn) != NOTE; insn = PREV_INSN (insn));
12695           if (insn)
12696             line_number = NOTE_LINE_NUMBER (insn);
12697           add_compiler_stub (labelname, funname, line_number);
12698         }
12699       else
12700         labelname = get_prev_label (funname);
12701
12702       sprintf (buf, "jbsr %%z%d,%.246s",
12703                operand_number, IDENTIFIER_POINTER (labelname));
12704       return buf;
12705     }
12706   else
12707     {
12708       sprintf (buf, "bl %%z%d", operand_number);
12709       return buf;
12710     }
12711 }
12712
12713 #endif /* RS6000_LONG_BRANCH */
12714
12715 #define GEN_LOCAL_LABEL_FOR_SYMBOL(BUF,SYMBOL,LENGTH,N)         \
12716   do {                                                          \
12717     const char *const symbol_ = (SYMBOL);                       \
12718     char *buffer_ = (BUF);                                      \
12719     if (symbol_[0] == '"')                                      \
12720       {                                                         \
12721         sprintf(buffer_, "\"L%d$%s", (N), symbol_+1);           \
12722       }                                                         \
12723     else if (name_needs_quotes(symbol_))                        \
12724       {                                                         \
12725         sprintf(buffer_, "\"L%d$%s\"", (N), symbol_);           \
12726       }                                                         \
12727     else                                                        \
12728       {                                                         \
12729         sprintf(buffer_, "L%d$%s", (N), symbol_);               \
12730       }                                                         \
12731   } while (0)
12732
12733
12734 /* Generate PIC and indirect symbol stubs.  */
12735
12736 void
12737 machopic_output_stub (file, symb, stub)
12738      FILE *file;
12739      const char *symb, *stub;
12740 {
12741   unsigned int length;
12742   char *symbol_name, *lazy_ptr_name;
12743   char *local_label_0;
12744   static int label = 0;
12745
12746   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
12747   symb = (*targetm.strip_name_encoding) (symb);
12748
12749   label += 1;
12750
12751   length = strlen (symb);
12752   symbol_name = alloca (length + 32);
12753   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
12754
12755   lazy_ptr_name = alloca (length + 32);
12756   GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
12757
12758   local_label_0 = alloca (length + 32);
12759   GEN_LOCAL_LABEL_FOR_SYMBOL (local_label_0, symb, length, 0);
12760
12761   if (flag_pic == 2)
12762     machopic_picsymbol_stub_section ();
12763   else
12764     machopic_symbol_stub_section ();
12765
12766   fprintf (file, "%s:\n", stub);
12767   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
12768
12769   if (flag_pic == 2)
12770     {
12771       fprintf (file, "\tmflr r0\n");
12772       fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
12773       fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
12774       fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
12775                lazy_ptr_name, local_label_0);
12776       fprintf (file, "\tmtlr r0\n");
12777       fprintf (file, "\tlwz r12,lo16(%s-%s)(r11)\n",
12778                lazy_ptr_name, local_label_0);
12779       fprintf (file, "\tmtctr r12\n");
12780       fprintf (file, "\taddi r11,r11,lo16(%s-%s)\n",
12781                lazy_ptr_name, local_label_0);
12782       fprintf (file, "\tbctr\n");
12783     }
12784   else
12785     fprintf (file, "non-pure not supported\n");
12786   
12787   machopic_lazy_symbol_ptr_section ();
12788   fprintf (file, "%s:\n", lazy_ptr_name);
12789   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
12790   fprintf (file, "\t.long dyld_stub_binding_helper\n");
12791 }
12792
12793 /* Legitimize PIC addresses.  If the address is already
12794    position-independent, we return ORIG.  Newly generated
12795    position-independent addresses go into a reg.  This is REG if non
12796    zero, otherwise we allocate register(s) as necessary.  */
12797
12798 #define SMALL_INT(X) ((unsigned) (INTVAL(X) + 0x8000) < 0x10000)
12799
12800 rtx
12801 rs6000_machopic_legitimize_pic_address (orig, mode, reg)
12802      rtx orig;
12803      enum machine_mode mode;
12804      rtx reg;
12805 {
12806   rtx base, offset;
12807
12808   if (reg == NULL && ! reload_in_progress && ! reload_completed)
12809     reg = gen_reg_rtx (Pmode);
12810
12811   if (GET_CODE (orig) == CONST)
12812     {
12813       if (GET_CODE (XEXP (orig, 0)) == PLUS
12814           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
12815         return orig;
12816
12817       if (GET_CODE (XEXP (orig, 0)) == PLUS)
12818         {
12819           base =
12820             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
12821                                                     Pmode, reg);
12822           offset =
12823             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
12824                                                     Pmode, reg);
12825         }
12826       else
12827         abort ();
12828
12829       if (GET_CODE (offset) == CONST_INT)
12830         {
12831           if (SMALL_INT (offset))
12832             return plus_constant (base, INTVAL (offset));
12833           else if (! reload_in_progress && ! reload_completed)
12834             offset = force_reg (Pmode, offset);
12835           else
12836             {
12837               rtx mem = force_const_mem (Pmode, orig);
12838               return machopic_legitimize_pic_address (mem, Pmode, reg);
12839             }
12840         }
12841       return gen_rtx (PLUS, Pmode, base, offset);
12842     }
12843
12844   /* Fall back on generic machopic code.  */
12845   return machopic_legitimize_pic_address (orig, mode, reg);
12846 }
12847
12848 /* This is just a placeholder to make linking work without having to
12849    add this to the generic Darwin EXTRA_SECTIONS.  If -mcall-aix is
12850    ever needed for Darwin (not too likely!) this would have to get a
12851    real definition.  */
12852
12853 void
12854 toc_section ()
12855 {
12856 }
12857
12858 #endif /* TARGET_MACHO */
12859
12860 #if TARGET_ELF
12861 static unsigned int
12862 rs6000_elf_section_type_flags (decl, name, reloc)
12863      tree decl;
12864      const char *name;
12865      int reloc;
12866 {
12867   unsigned int flags = default_section_type_flags (decl, name, reloc);
12868
12869   if (TARGET_RELOCATABLE)
12870     flags |= SECTION_WRITE;
12871
12872   return flags;
12873 }
12874
12875 /* Record an element in the table of global constructors.  SYMBOL is
12876    a SYMBOL_REF of the function to be called; PRIORITY is a number
12877    between 0 and MAX_INIT_PRIORITY.
12878
12879    This differs from default_named_section_asm_out_constructor in
12880    that we have special handling for -mrelocatable.  */
12881
12882 static void
12883 rs6000_elf_asm_out_constructor (symbol, priority)
12884      rtx symbol;
12885      int priority;
12886 {
12887   const char *section = ".ctors";
12888   char buf[16];
12889
12890   if (priority != DEFAULT_INIT_PRIORITY)
12891     {
12892       sprintf (buf, ".ctors.%.5u",
12893                /* Invert the numbering so the linker puts us in the proper
12894                   order; constructors are run from right to left, and the
12895                   linker sorts in increasing order.  */
12896                MAX_INIT_PRIORITY - priority);
12897       section = buf;
12898     }
12899
12900   named_section_flags (section, SECTION_WRITE);
12901   assemble_align (POINTER_SIZE);
12902
12903   if (TARGET_RELOCATABLE)
12904     {
12905       fputs ("\t.long (", asm_out_file);
12906       output_addr_const (asm_out_file, symbol);
12907       fputs (")@fixup\n", asm_out_file);
12908     }
12909   else
12910     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
12911 }
12912
12913 static void
12914 rs6000_elf_asm_out_destructor (symbol, priority)
12915      rtx symbol;
12916      int priority;
12917 {
12918   const char *section = ".dtors";
12919   char buf[16];
12920
12921   if (priority != DEFAULT_INIT_PRIORITY)
12922     {
12923       sprintf (buf, ".dtors.%.5u",
12924                /* Invert the numbering so the linker puts us in the proper
12925                   order; constructors are run from right to left, and the
12926                   linker sorts in increasing order.  */
12927                MAX_INIT_PRIORITY - priority);
12928       section = buf;
12929     }
12930
12931   named_section_flags (section, SECTION_WRITE);
12932   assemble_align (POINTER_SIZE);
12933
12934   if (TARGET_RELOCATABLE)
12935     {
12936       fputs ("\t.long (", asm_out_file);
12937       output_addr_const (asm_out_file, symbol);
12938       fputs (")@fixup\n", asm_out_file);
12939     }
12940   else
12941     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
12942 }
12943 #endif
12944
12945 #if TARGET_XCOFF
12946 static void
12947 xcoff_asm_named_section (name, flags)
12948      const char *name;
12949      unsigned int flags ATTRIBUTE_UNUSED;
12950 {
12951   fprintf (asm_out_file, "\t.csect %s\n", name);
12952 }
12953
12954 static void
12955 rs6000_xcoff_select_section (exp, reloc, align)
12956      tree exp;
12957      int reloc;
12958      unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
12959 {
12960   if ((TREE_CODE (exp) == STRING_CST
12961        && ! flag_writable_strings)
12962       || (TREE_CODE_CLASS (TREE_CODE (exp)) == 'd'
12963           && TREE_READONLY (exp) && ! TREE_THIS_VOLATILE (exp)
12964           && DECL_INITIAL (exp)
12965           && (DECL_INITIAL (exp) == error_mark_node
12966               || TREE_CONSTANT (DECL_INITIAL (exp)))
12967           && ! (reloc)))
12968     {
12969       if (TREE_PUBLIC (exp))
12970         read_only_data_section ();
12971       else
12972         read_only_private_data_section ();
12973     }
12974   else
12975     {
12976       if (TREE_PUBLIC (exp))
12977         data_section ();
12978       else
12979         private_data_section ();
12980     }
12981 }
12982
12983 static void
12984 rs6000_xcoff_unique_section (decl, reloc)
12985      tree decl;
12986      int reloc ATTRIBUTE_UNUSED;
12987 {
12988   const char *name;
12989   char *string;
12990   size_t len;
12991
12992   if (TREE_CODE (decl) == FUNCTION_DECL)
12993     {
12994       name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
12995       len = strlen (name) + 5;
12996       string = alloca (len + 1);
12997       sprintf (string, ".%s[PR]", name);
12998       DECL_SECTION_NAME (decl) = build_string (len, string);
12999     }
13000 }
13001
13002 /* Select section for constant in constant pool.
13003
13004    On RS/6000, all constants are in the private read-only data area.
13005    However, if this is being placed in the TOC it must be output as a
13006    toc entry.  */
13007
13008 static void
13009 rs6000_xcoff_select_rtx_section (mode, x, align)
13010      enum machine_mode mode;
13011      rtx x;
13012      unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
13013 {
13014   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
13015     toc_section ();
13016   else
13017     read_only_private_data_section ();
13018 }
13019
13020 /* Remove any trailing [DS] or the like from the symbol name.  */
13021
13022 static const char *
13023 rs6000_xcoff_strip_name_encoding (name)
13024      const char *name;
13025 {
13026   size_t len;
13027   if (*name == '*')
13028     name++;
13029   len = strlen (name);
13030   if (name[len - 1] == ']')
13031     return ggc_alloc_string (name, len - 4);
13032   else
13033     return name;
13034 }
13035
13036 #endif /* TARGET_XCOFF */
13037
13038 /* Note that this is also used for ELF64.  */
13039
13040 static void
13041 rs6000_xcoff_encode_section_info (decl, first)
13042      tree decl;
13043      int first ATTRIBUTE_UNUSED;
13044 {
13045   if (TREE_CODE (decl) == FUNCTION_DECL
13046       && (TREE_ASM_WRITTEN (decl) || ! TREE_PUBLIC (decl))
13047       && ! DECL_WEAK (decl))
13048     SYMBOL_REF_FLAG (XEXP (DECL_RTL (decl), 0)) = 1;
13049 }