OSDN Git Service

* target.h (targetm.address_cost): New.
[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, 2003 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 "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "real.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "insn-attr.h"
34 #include "flags.h"
35 #include "recog.h"
36 #include "obstack.h"
37 #include "tree.h"
38 #include "expr.h"
39 #include "optabs.h"
40 #include "except.h"
41 #include "function.h"
42 #include "output.h"
43 #include "basic-block.h"
44 #include "integrate.h"
45 #include "toplev.h"
46 #include "ggc.h"
47 #include "hashtab.h"
48 #include "tm_p.h"
49 #include "target.h"
50 #include "target-def.h"
51 #include "langhooks.h"
52 #include "reload.h"
53
54 #ifndef TARGET_NO_PROTOTYPE
55 #define TARGET_NO_PROTOTYPE 0
56 #endif
57
58 #define min(A,B)        ((A) < (B) ? (A) : (B))
59 #define max(A,B)        ((A) > (B) ? (A) : (B))
60
61 /* Target cpu type */
62
63 enum processor_type rs6000_cpu;
64 struct rs6000_cpu_select rs6000_select[3] =
65 {
66   /* switch             name,                   tune    arch */
67   { (const char *)0,    "--with-cpu=",          1,      1 },
68   { (const char *)0,    "-mcpu=",               1,      1 },
69   { (const char *)0,    "-mtune=",              1,      0 },
70 };
71
72 /* Size of long double */
73 const char *rs6000_long_double_size_string;
74 int rs6000_long_double_type_size;
75
76 /* Whether -mabi=altivec has appeared */
77 int rs6000_altivec_abi;
78
79 /* Whether VRSAVE instructions should be generated.  */
80 int rs6000_altivec_vrsave;
81
82 /* String from -mvrsave= option.  */
83 const char *rs6000_altivec_vrsave_string;
84
85 /* Nonzero if we want SPE ABI extensions.  */
86 int rs6000_spe_abi;
87
88 /* Whether isel instructions should be generated.  */
89 int rs6000_isel;
90
91 /* Nonzero if we have FPRs.  */
92 int rs6000_fprs = 1;
93
94 /* String from -misel=.  */
95 const char *rs6000_isel_string;
96
97 /* Set to nonzero once AIX common-mode calls have been defined.  */
98 static GTY(()) int common_mode_defined;
99
100 /* Private copy of original value of flag_pic for ABI_AIX.  */
101 static int rs6000_flag_pic;
102
103 /* Save information from a "cmpxx" operation until the branch or scc is
104    emitted.  */
105 rtx rs6000_compare_op0, rs6000_compare_op1;
106 int rs6000_compare_fp_p;
107
108 /* Label number of label created for -mrelocatable, to call to so we can
109    get the address of the GOT section */
110 int rs6000_pic_labelno;
111
112 #ifdef USING_ELFOS_H
113 /* Which abi to adhere to */
114 const char *rs6000_abi_name = RS6000_ABI_NAME;
115
116 /* Semantics of the small data area */
117 enum rs6000_sdata_type rs6000_sdata = SDATA_DATA;
118
119 /* Which small data model to use */
120 const char *rs6000_sdata_name = (char *)0;
121
122 /* Counter for labels which are to be placed in .fixup.  */
123 int fixuplabelno = 0;
124 #endif
125
126 /* ABI enumeration available for subtarget to use.  */
127 enum rs6000_abi rs6000_current_abi;
128
129 /* ABI string from -mabi= option.  */
130 const char *rs6000_abi_string;
131
132 /* Debug flags */
133 const char *rs6000_debug_name;
134 int rs6000_debug_stack;         /* debug stack applications */
135 int rs6000_debug_arg;           /* debug argument handling */
136
137 const char *rs6000_traceback_name;
138 static enum {
139   traceback_default = 0,
140   traceback_none,
141   traceback_part,
142   traceback_full
143 } rs6000_traceback;
144
145 /* Flag to say the TOC is initialized */
146 int toc_initialized;
147 char toc_label_name[10];
148
149 /* Alias set for saves and restores from the rs6000 stack.  */
150 static int rs6000_sr_alias_set;
151
152 /* Call distance, overridden by -mlongcall and #pragma longcall(1).
153    The only place that looks at this is rs6000_set_default_type_attributes;
154    everywhere else should rely on the presence or absence of a longcall
155    attribute on the function declaration.  */
156 int rs6000_default_long_calls;
157 const char *rs6000_longcall_switch;
158
159 struct builtin_description
160 {
161   /* mask is not const because we're going to alter it below.  This
162      nonsense will go away when we rewrite the -march infrastructure
163      to give us more target flag bits.  */
164   unsigned int mask;
165   const enum insn_code icode;
166   const char *const name;
167   const enum rs6000_builtins code;
168 };
169
170 static bool rs6000_function_ok_for_sibcall PARAMS ((tree, tree));
171 static int num_insns_constant_wide PARAMS ((HOST_WIDE_INT));
172 static void validate_condition_mode 
173   PARAMS ((enum rtx_code, enum machine_mode));
174 static rtx rs6000_generate_compare PARAMS ((enum rtx_code));
175 static void rs6000_maybe_dead PARAMS ((rtx));
176 static void rs6000_emit_stack_tie PARAMS ((void));
177 static void rs6000_frame_related PARAMS ((rtx, rtx, HOST_WIDE_INT, rtx, rtx));
178 static void emit_frame_save PARAMS ((rtx, rtx, enum machine_mode,
179                                      unsigned int, int, int));
180 static rtx gen_frame_mem_offset PARAMS ((enum machine_mode, rtx, int));
181 static void rs6000_emit_allocate_stack PARAMS ((HOST_WIDE_INT, int));
182 static unsigned rs6000_hash_constant PARAMS ((rtx));
183 static unsigned toc_hash_function PARAMS ((const void *));
184 static int toc_hash_eq PARAMS ((const void *, const void *));
185 static int constant_pool_expr_1 PARAMS ((rtx, int *, int *));
186 static struct machine_function * rs6000_init_machine_status PARAMS ((void));
187 static bool rs6000_assemble_integer PARAMS ((rtx, unsigned int, int));
188 #ifdef HAVE_GAS_HIDDEN
189 static void rs6000_assemble_visibility PARAMS ((tree, int));
190 #endif
191 static int rs6000_ra_ever_killed PARAMS ((void));
192 static tree rs6000_handle_longcall_attribute PARAMS ((tree *, tree, tree, int, bool *));
193 const struct attribute_spec rs6000_attribute_table[];
194 static void rs6000_set_default_type_attributes PARAMS ((tree));
195 static void rs6000_output_function_prologue PARAMS ((FILE *, HOST_WIDE_INT));
196 static void rs6000_output_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
197 static void rs6000_output_mi_thunk PARAMS ((FILE *, tree, HOST_WIDE_INT,
198                                             HOST_WIDE_INT, tree));
199 static rtx rs6000_emit_set_long_const PARAMS ((rtx,
200   HOST_WIDE_INT, HOST_WIDE_INT));
201 #if TARGET_ELF
202 static unsigned int rs6000_elf_section_type_flags PARAMS ((tree, const char *,
203                                                            int));
204 static void rs6000_elf_asm_out_constructor PARAMS ((rtx, int));
205 static void rs6000_elf_asm_out_destructor PARAMS ((rtx, int));
206 static void rs6000_elf_select_section PARAMS ((tree, int,
207                                                unsigned HOST_WIDE_INT));
208 static void rs6000_elf_unique_section PARAMS ((tree, int));
209 static void rs6000_elf_select_rtx_section PARAMS ((enum machine_mode, rtx,
210                                                    unsigned HOST_WIDE_INT));
211 static void rs6000_elf_encode_section_info PARAMS ((tree, int))
212      ATTRIBUTE_UNUSED;
213 static const char *rs6000_elf_strip_name_encoding PARAMS ((const char *));
214 static bool rs6000_elf_in_small_data_p PARAMS ((tree));
215 #endif
216 #if TARGET_XCOFF
217 static void rs6000_xcoff_asm_globalize_label PARAMS ((FILE *, const char *));
218 static void rs6000_xcoff_asm_named_section PARAMS ((const char *, unsigned int));
219 static void rs6000_xcoff_select_section PARAMS ((tree, int,
220                                                  unsigned HOST_WIDE_INT));
221 static void rs6000_xcoff_unique_section PARAMS ((tree, int));
222 static void rs6000_xcoff_select_rtx_section PARAMS ((enum machine_mode, rtx,
223                                                      unsigned HOST_WIDE_INT));
224 static const char * rs6000_xcoff_strip_name_encoding PARAMS ((const char *));
225 static unsigned int rs6000_xcoff_section_type_flags PARAMS ((tree, const char *, int));
226 #endif
227 static void rs6000_xcoff_encode_section_info PARAMS ((tree, int))
228      ATTRIBUTE_UNUSED;
229 static bool rs6000_binds_local_p PARAMS ((tree));
230 static bool rs6000_rtx_costs PARAMS ((rtx, int, int, int *));
231 static int rs6000_adjust_cost PARAMS ((rtx, rtx, rtx, int));
232 static int rs6000_adjust_priority PARAMS ((rtx, int));
233 static int rs6000_issue_rate PARAMS ((void));
234
235 static void rs6000_init_builtins PARAMS ((void));
236 static rtx rs6000_expand_unop_builtin PARAMS ((enum insn_code, tree, rtx));
237 static rtx rs6000_expand_binop_builtin PARAMS ((enum insn_code, tree, rtx));
238 static rtx rs6000_expand_ternop_builtin PARAMS ((enum insn_code, tree, rtx));
239 static rtx rs6000_expand_builtin PARAMS ((tree, rtx, rtx, enum machine_mode, int));
240 static void altivec_init_builtins PARAMS ((void));
241 static void rs6000_common_init_builtins PARAMS ((void));
242
243 static void enable_mask_for_builtins PARAMS ((struct builtin_description *,
244                                               int, enum rs6000_builtins,
245                                               enum rs6000_builtins));
246 static void spe_init_builtins PARAMS ((void));
247 static rtx spe_expand_builtin PARAMS ((tree, rtx, bool *));
248 static rtx spe_expand_predicate_builtin PARAMS ((enum insn_code, tree, rtx));
249 static rtx spe_expand_evsel_builtin PARAMS ((enum insn_code, tree, rtx));
250 static int rs6000_emit_int_cmove PARAMS ((rtx, rtx, rtx, rtx));
251
252 static rtx altivec_expand_builtin PARAMS ((tree, rtx, bool *));
253 static rtx altivec_expand_ld_builtin PARAMS ((tree, rtx, bool *));
254 static rtx altivec_expand_st_builtin PARAMS ((tree, rtx, bool *));
255 static rtx altivec_expand_dst_builtin PARAMS ((tree, rtx, bool *));
256 static rtx altivec_expand_abs_builtin PARAMS ((enum insn_code, tree, rtx));
257 static rtx altivec_expand_predicate_builtin PARAMS ((enum insn_code, const char *, tree, rtx));
258 static rtx altivec_expand_stv_builtin PARAMS ((enum insn_code, tree));
259 static void rs6000_parse_abi_options PARAMS ((void));
260 static void rs6000_parse_vrsave_option PARAMS ((void));
261 static void rs6000_parse_isel_option PARAMS ((void));
262 static int first_altivec_reg_to_save PARAMS ((void));
263 static unsigned int compute_vrsave_mask PARAMS ((void));
264 static void is_altivec_return_reg PARAMS ((rtx, void *));
265 static rtx generate_set_vrsave PARAMS ((rtx, rs6000_stack_t *, int));
266 static void altivec_frame_fixup PARAMS ((rtx, rtx, HOST_WIDE_INT));
267 static int easy_vector_constant PARAMS ((rtx));
268
269 /* Hash table stuff for keeping track of TOC entries.  */
270
271 struct toc_hash_struct GTY(())
272 {
273   /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
274      ASM_OUTPUT_SPECIAL_POOL_ENTRY_P.  */
275   rtx key;
276   enum machine_mode key_mode;
277   int labelno;
278 };
279
280 static GTY ((param_is (struct toc_hash_struct))) htab_t toc_hash_table;
281 \f
282 /* Default register names.  */
283 char rs6000_reg_names[][8] =
284 {
285       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
286       "8",  "9", "10", "11", "12", "13", "14", "15",
287      "16", "17", "18", "19", "20", "21", "22", "23",
288      "24", "25", "26", "27", "28", "29", "30", "31",
289       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
290       "8",  "9", "10", "11", "12", "13", "14", "15",
291      "16", "17", "18", "19", "20", "21", "22", "23",
292      "24", "25", "26", "27", "28", "29", "30", "31",
293      "mq", "lr", "ctr","ap",
294       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
295       "xer",
296       /* AltiVec registers.  */
297       "0",  "1",  "2",  "3",  "4",  "5",  "6", "7",
298       "8",  "9",  "10", "11", "12", "13", "14", "15",
299       "16", "17", "18", "19", "20", "21", "22", "23",
300       "24", "25", "26", "27", "28", "29", "30", "31",
301       "vrsave", "vscr",
302       /* SPE registers.  */
303       "spe_acc", "spefscr"
304 };
305
306 #ifdef TARGET_REGNAMES
307 static const char alt_reg_names[][8] =
308 {
309    "%r0",   "%r1",  "%r2",  "%r3",  "%r4",  "%r5",  "%r6",  "%r7",
310    "%r8",   "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
311   "%r16",  "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
312   "%r24",  "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
313    "%f0",   "%f1",  "%f2",  "%f3",  "%f4",  "%f5",  "%f6",  "%f7",
314    "%f8",   "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
315   "%f16",  "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
316   "%f24",  "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
317     "mq",    "lr",  "ctr",   "ap",
318   "%cr0",  "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
319    "xer",
320   /* AltiVec registers.  */
321    "%v0",  "%v1",  "%v2",  "%v3",  "%v4",  "%v5",  "%v6", "%v7",
322    "%v8",  "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
323   "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
324   "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
325   "vrsave", "vscr",
326   /* SPE registers.  */
327   "spe_acc", "spefscr"
328 };
329 #endif
330 \f
331 #ifndef MASK_STRICT_ALIGN
332 #define MASK_STRICT_ALIGN 0
333 #endif
334
335 /* The VRSAVE bitmask puts bit %v0 as the most significant bit.  */
336 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
337 \f
338 /* Initialize the GCC target structure.  */
339 #undef TARGET_ATTRIBUTE_TABLE
340 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
341 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
342 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
343
344 #undef TARGET_ASM_ALIGNED_DI_OP
345 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
346
347 /* Default unaligned ops are only provided for ELF.  Find the ops needed
348    for non-ELF systems.  */
349 #ifndef OBJECT_FORMAT_ELF
350 #if TARGET_XCOFF
351 /* For XCOFF.  rs6000_assemble_integer will handle unaligned DIs on
352    64-bit targets.  */
353 #undef TARGET_ASM_UNALIGNED_HI_OP
354 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
355 #undef TARGET_ASM_UNALIGNED_SI_OP
356 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
357 #undef TARGET_ASM_UNALIGNED_DI_OP
358 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
359 #else
360 /* For Darwin.  */
361 #undef TARGET_ASM_UNALIGNED_HI_OP
362 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
363 #undef TARGET_ASM_UNALIGNED_SI_OP
364 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
365 #endif
366 #endif
367
368 /* This hook deals with fixups for relocatable code and DI-mode objects
369    in 64-bit code.  */
370 #undef TARGET_ASM_INTEGER
371 #define TARGET_ASM_INTEGER rs6000_assemble_integer
372
373 #ifdef HAVE_GAS_HIDDEN
374 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
375 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
376 #endif
377
378 #undef TARGET_ASM_FUNCTION_PROLOGUE
379 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
380 #undef TARGET_ASM_FUNCTION_EPILOGUE
381 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
382
383 #undef TARGET_SCHED_ISSUE_RATE
384 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
385 #undef TARGET_SCHED_ADJUST_COST
386 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
387 #undef TARGET_SCHED_ADJUST_PRIORITY
388 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
389
390 #undef TARGET_INIT_BUILTINS
391 #define TARGET_INIT_BUILTINS rs6000_init_builtins
392
393 #undef TARGET_EXPAND_BUILTIN
394 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
395
396 #undef TARGET_BINDS_LOCAL_P
397 #define TARGET_BINDS_LOCAL_P rs6000_binds_local_p
398
399 #undef TARGET_ASM_OUTPUT_MI_THUNK
400 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
401
402 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
403 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
404
405 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
406 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
407
408 #undef TARGET_RTX_COSTS
409 #define TARGET_RTX_COSTS rs6000_rtx_costs
410 #undef TARGET_ADDRESS_COST
411 #define TARGET_ADDRESS_COST hook_int_rtx_0
412
413 struct gcc_target targetm = TARGET_INITIALIZER;
414 \f
415 /* Override command line options.  Mostly we process the processor
416    type and sometimes adjust other TARGET_ options.  */
417
418 void
419 rs6000_override_options (default_cpu)
420      const char *default_cpu;
421 {
422   size_t i, j;
423   struct rs6000_cpu_select *ptr;
424
425   /* Simplify the entries below by making a mask for any POWER
426      variant and any PowerPC variant.  */
427
428 #define POWER_MASKS (MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING)
429 #define POWERPC_MASKS (MASK_POWERPC | MASK_PPC_GPOPT \
430                        | MASK_PPC_GFXOPT | MASK_POWERPC64)
431 #define POWERPC_OPT_MASKS (MASK_PPC_GPOPT | MASK_PPC_GFXOPT)
432
433   static struct ptt
434     {
435       const char *const name;           /* Canonical processor name.  */
436       const enum processor_type processor; /* Processor type enum value.  */
437       const int target_enable;  /* Target flags to enable.  */
438       const int target_disable; /* Target flags to disable.  */
439     } const processor_target_table[]
440       = {{"common", PROCESSOR_COMMON, MASK_NEW_MNEMONICS,
441             POWER_MASKS | POWERPC_MASKS},
442          {"power", PROCESSOR_POWER,
443             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
444             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
445          {"power2", PROCESSOR_POWER,
446             MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING,
447             POWERPC_MASKS | MASK_NEW_MNEMONICS},
448          {"power3", PROCESSOR_PPC630,
449             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
450             POWER_MASKS | MASK_PPC_GPOPT},
451          {"power4", PROCESSOR_POWER4,
452             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
453             POWER_MASKS | MASK_PPC_GPOPT},
454          {"powerpc", PROCESSOR_POWERPC,
455             MASK_POWERPC | MASK_NEW_MNEMONICS,
456             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
457          {"powerpc64", PROCESSOR_POWERPC64,
458             MASK_POWERPC | MASK_POWERPC64 | MASK_NEW_MNEMONICS,
459             POWER_MASKS | POWERPC_OPT_MASKS},
460          {"rios", PROCESSOR_RIOS1,
461             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
462             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
463          {"rios1", PROCESSOR_RIOS1,
464             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
465             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
466          {"rsc", PROCESSOR_PPC601,
467             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
468             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
469          {"rsc1", PROCESSOR_PPC601,
470             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
471             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
472          {"rios2", PROCESSOR_RIOS2,
473             MASK_POWER | MASK_MULTIPLE | MASK_STRING | MASK_POWER2,
474             POWERPC_MASKS | MASK_NEW_MNEMONICS},
475          {"rs64a", PROCESSOR_RS64A,
476             MASK_POWERPC | MASK_NEW_MNEMONICS,
477             POWER_MASKS | POWERPC_OPT_MASKS},
478          {"401", PROCESSOR_PPC403,
479             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
480             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
481          {"403", PROCESSOR_PPC403,
482             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS | MASK_STRICT_ALIGN,
483             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
484          {"405", PROCESSOR_PPC405,
485             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
486             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
487          {"405f", PROCESSOR_PPC405,
488             MASK_POWERPC | MASK_NEW_MNEMONICS,
489             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
490          {"505", PROCESSOR_MPCCORE,
491             MASK_POWERPC | MASK_NEW_MNEMONICS,
492             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
493          {"601", PROCESSOR_PPC601,
494             MASK_POWER | MASK_POWERPC | MASK_NEW_MNEMONICS | MASK_MULTIPLE | MASK_STRING,
495             MASK_POWER2 | POWERPC_OPT_MASKS | MASK_POWERPC64},
496          {"602", PROCESSOR_PPC603,
497             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
498             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
499          {"603", PROCESSOR_PPC603,
500             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
501             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
502          {"603e", PROCESSOR_PPC603,
503             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
504             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
505          {"ec603e", PROCESSOR_PPC603,
506             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
507             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
508          {"604", PROCESSOR_PPC604,
509             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
510             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
511          {"604e", PROCESSOR_PPC604e,
512             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
513             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
514          {"620", PROCESSOR_PPC620,
515             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
516             POWER_MASKS | MASK_PPC_GPOPT},
517          {"630", PROCESSOR_PPC630,
518             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
519             POWER_MASKS | MASK_PPC_GPOPT},
520          {"740", PROCESSOR_PPC750,
521             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
522             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
523          {"750", PROCESSOR_PPC750,
524             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
525             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
526          {"7400", PROCESSOR_PPC7400,
527             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
528             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
529          {"7450", PROCESSOR_PPC7450,
530             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
531             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
532          {"8540", PROCESSOR_PPC8540,
533             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
534             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
535          {"801", PROCESSOR_MPCCORE,
536             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
537             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
538          {"821", PROCESSOR_MPCCORE,
539             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
540             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
541          {"823", PROCESSOR_MPCCORE,
542             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
543             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
544          {"860", PROCESSOR_MPCCORE,
545             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
546             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64}};
547
548   const size_t ptt_size = ARRAY_SIZE (processor_target_table);
549
550   /* Save current -mmultiple/-mno-multiple status.  */
551   int multiple = TARGET_MULTIPLE;
552   /* Save current -mstring/-mno-string status.  */
553   int string = TARGET_STRING;
554
555   /* Identify the processor type.  */
556   rs6000_select[0].string = default_cpu;
557   rs6000_cpu = TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT;
558
559   for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
560     {
561       ptr = &rs6000_select[i];
562       if (ptr->string != (char *)0 && ptr->string[0] != '\0')
563         {
564           for (j = 0; j < ptt_size; j++)
565             if (! strcmp (ptr->string, processor_target_table[j].name))
566               {
567                 if (ptr->set_tune_p)
568                   rs6000_cpu = processor_target_table[j].processor;
569
570                 if (ptr->set_arch_p)
571                   {
572                     target_flags |= processor_target_table[j].target_enable;
573                     target_flags &= ~processor_target_table[j].target_disable;
574                   }
575                 break;
576               }
577
578           if (j == ptt_size)
579             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
580         }
581     }
582
583   if (rs6000_cpu == PROCESSOR_PPC8540)
584     rs6000_isel = 1;
585
586   /* If we are optimizing big endian systems for space, use the load/store
587      multiple and string instructions.  */
588   if (BYTES_BIG_ENDIAN && optimize_size)
589     target_flags |= MASK_MULTIPLE | MASK_STRING;
590
591   /* If -mmultiple or -mno-multiple was explicitly used, don't
592      override with the processor default */
593   if ((target_flags_explicit & MASK_MULTIPLE) != 0)
594     target_flags = (target_flags & ~MASK_MULTIPLE) | multiple;
595
596   /* If -mstring or -mno-string was explicitly used, don't override
597      with the processor default.  */
598   if ((target_flags_explicit & MASK_STRING) != 0)
599     target_flags = (target_flags & ~MASK_STRING) | string;
600
601   /* Don't allow -mmultiple or -mstring on little endian systems
602      unless the cpu is a 750, because the hardware doesn't support the
603      instructions used in little endian mode, and causes an alignment
604      trap.  The 750 does not cause an alignment trap (except when the
605      target is unaligned).  */
606
607   if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
608     {
609       if (TARGET_MULTIPLE)
610         {
611           target_flags &= ~MASK_MULTIPLE;
612           if ((target_flags_explicit & MASK_MULTIPLE) != 0)
613             warning ("-mmultiple is not supported on little endian systems");
614         }
615
616       if (TARGET_STRING)
617         {
618           target_flags &= ~MASK_STRING;
619           if ((target_flags_explicit & MASK_STRING) != 0)
620             warning ("-mstring is not supported on little endian systems");
621         }
622     }
623
624   if (flag_pic != 0 && DEFAULT_ABI == ABI_AIX)
625     {
626       rs6000_flag_pic = flag_pic;
627       flag_pic = 0;
628     }
629
630   /* For Darwin, always silently make -fpic and -fPIC identical.  */
631   if (flag_pic == 1 && DEFAULT_ABI == ABI_DARWIN)
632     flag_pic = 2;
633
634   /* Set debug flags */
635   if (rs6000_debug_name)
636     {
637       if (! strcmp (rs6000_debug_name, "all"))
638         rs6000_debug_stack = rs6000_debug_arg = 1;
639       else if (! strcmp (rs6000_debug_name, "stack"))
640         rs6000_debug_stack = 1;
641       else if (! strcmp (rs6000_debug_name, "arg"))
642         rs6000_debug_arg = 1;
643       else
644         error ("unknown -mdebug-%s switch", rs6000_debug_name);
645     }
646
647   if (rs6000_traceback_name)
648     {
649       if (! strncmp (rs6000_traceback_name, "full", 4))
650         rs6000_traceback = traceback_full;
651       else if (! strncmp (rs6000_traceback_name, "part", 4))
652         rs6000_traceback = traceback_part;
653       else if (! strncmp (rs6000_traceback_name, "no", 2))
654         rs6000_traceback = traceback_none;
655       else
656         error ("unknown -mtraceback arg `%s'; expecting `full', `partial' or `none'",
657                rs6000_traceback_name);
658     }
659
660   /* Set size of long double */
661   rs6000_long_double_type_size = 64;
662   if (rs6000_long_double_size_string)
663     {
664       char *tail;
665       int size = strtol (rs6000_long_double_size_string, &tail, 10);
666       if (*tail != '\0' || (size != 64 && size != 128))
667         error ("Unknown switch -mlong-double-%s",
668                rs6000_long_double_size_string);
669       else
670         rs6000_long_double_type_size = size;
671     }
672
673   /* Handle -mabi= options.  */
674   rs6000_parse_abi_options ();
675
676   /* Handle -mvrsave= option.  */
677   rs6000_parse_vrsave_option ();
678
679   /* Handle -misel= option.  */
680   rs6000_parse_isel_option ();
681
682 #ifdef SUBTARGET_OVERRIDE_OPTIONS
683   SUBTARGET_OVERRIDE_OPTIONS;
684 #endif
685 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
686   SUBSUBTARGET_OVERRIDE_OPTIONS;
687 #endif
688
689   /* Handle -m(no-)longcall option.  This is a bit of a cheap hack,
690      using TARGET_OPTIONS to handle a toggle switch, but we're out of
691      bits in target_flags so TARGET_SWITCHES cannot be used.
692      Assumption here is that rs6000_longcall_switch points into the
693      text of the complete option, rather than being a copy, so we can
694      scan back for the presence or absence of the no- modifier.  */
695   if (rs6000_longcall_switch)
696     {
697       const char *base = rs6000_longcall_switch;
698       while (base[-1] != 'm') base--;
699
700       if (*rs6000_longcall_switch != '\0')
701         error ("invalid option `%s'", base);
702       rs6000_default_long_calls = (base[0] != 'n');
703     }
704
705 #ifdef TARGET_REGNAMES
706   /* If the user desires alternate register names, copy in the
707      alternate names now.  */
708   if (TARGET_REGNAMES)
709     memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
710 #endif
711
712   /* Set TARGET_AIX_STRUCT_RET last, after the ABI is determined.
713      If -maix-struct-return or -msvr4-struct-return was explicitly
714      used, don't override with the ABI default.  */
715   if ((target_flags_explicit & MASK_AIX_STRUCT_RET) == 0)
716     {
717       if (DEFAULT_ABI == ABI_V4 && !DRAFT_V4_STRUCT_RET)
718         target_flags = (target_flags & ~MASK_AIX_STRUCT_RET);
719       else
720         target_flags |= MASK_AIX_STRUCT_RET;
721     }
722
723   if (TARGET_LONG_DOUBLE_128
724       && (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_DARWIN))
725     real_format_for_mode[TFmode - QFmode] = &ibm_extended_format;
726
727   /* Allocate an alias set for register saves & restores from stack.  */
728   rs6000_sr_alias_set = new_alias_set ();
729
730   if (TARGET_TOC) 
731     ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
732
733   /* We can only guarantee the availability of DI pseudo-ops when
734      assembling for 64-bit targets.  */
735   if (!TARGET_64BIT)
736     {
737       targetm.asm_out.aligned_op.di = NULL;
738       targetm.asm_out.unaligned_op.di = NULL;
739     }
740
741   /* Arrange to save and restore machine status around nested functions.  */
742   init_machine_status = rs6000_init_machine_status;
743 }
744
745 /* Handle -misel= option.  */
746 static void
747 rs6000_parse_isel_option ()
748 {
749   if (rs6000_isel_string == 0)
750     return;
751   else if (! strcmp (rs6000_isel_string, "yes"))
752     rs6000_isel = 1;
753   else if (! strcmp (rs6000_isel_string, "no"))
754     rs6000_isel = 0;
755   else
756     error ("unknown -misel= option specified: '%s'",
757          rs6000_isel_string);
758 }
759
760 /* Handle -mvrsave= options.  */
761 static void
762 rs6000_parse_vrsave_option ()
763 {
764   /* Generate VRSAVE instructions by default.  */
765   if (rs6000_altivec_vrsave_string == 0
766       || ! strcmp (rs6000_altivec_vrsave_string, "yes"))
767     rs6000_altivec_vrsave = 1;
768   else if (! strcmp (rs6000_altivec_vrsave_string, "no"))
769     rs6000_altivec_vrsave = 0;
770   else
771     error ("unknown -mvrsave= option specified: '%s'",
772            rs6000_altivec_vrsave_string);
773 }
774
775 /* Handle -mabi= options.  */
776 static void
777 rs6000_parse_abi_options ()
778 {
779   if (rs6000_abi_string == 0)
780     return;
781   else if (! strcmp (rs6000_abi_string, "altivec"))
782     rs6000_altivec_abi = 1;
783   else if (! strcmp (rs6000_abi_string, "no-altivec"))
784     rs6000_altivec_abi = 0;
785   else if (! strcmp (rs6000_abi_string, "spe"))
786     {
787       rs6000_spe_abi = 1;
788       if (!TARGET_SPE_ABI)
789         error ("not configured for ABI: '%s'", rs6000_abi_string);
790     }
791   
792   else if (! strcmp (rs6000_abi_string, "no-spe"))
793     rs6000_spe_abi = 0;
794   else
795     error ("unknown ABI specified: '%s'", rs6000_abi_string);
796 }
797
798 void
799 optimization_options (level, size)
800      int level ATTRIBUTE_UNUSED;
801      int size ATTRIBUTE_UNUSED;
802 {
803 }
804 \f
805 /* Do anything needed at the start of the asm file.  */
806
807 void
808 rs6000_file_start (file, default_cpu)
809      FILE *file;
810      const char *default_cpu;
811 {
812   size_t i;
813   char buffer[80];
814   const char *start = buffer;
815   struct rs6000_cpu_select *ptr;
816
817   if (flag_verbose_asm)
818     {
819       sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
820       rs6000_select[0].string = default_cpu;
821
822       for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
823         {
824           ptr = &rs6000_select[i];
825           if (ptr->string != (char *)0 && ptr->string[0] != '\0')
826             {
827               fprintf (file, "%s %s%s", start, ptr->name, ptr->string);
828               start = "";
829             }
830         }
831
832 #ifdef USING_ELFOS_H
833       switch (rs6000_sdata)
834         {
835         case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
836         case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
837         case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
838         case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
839         }
840
841       if (rs6000_sdata && g_switch_value)
842         {
843           fprintf (file, "%s -G %d", start, g_switch_value);
844           start = "";
845         }
846 #endif
847
848       if (*start == '\0')
849         putc ('\n', file);
850     }
851 }
852 \f
853 /* Return nonzero if this function is known to have a null epilogue.  */
854
855 int
856 direct_return ()
857 {
858   if (reload_completed)
859     {
860       rs6000_stack_t *info = rs6000_stack_info ();
861
862       if (info->first_gp_reg_save == 32
863           && info->first_fp_reg_save == 64
864           && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
865           && ! info->lr_save_p
866           && ! info->cr_save_p
867           && info->vrsave_mask == 0
868           && ! info->push_p)
869         return 1;
870     }
871
872   return 0;
873 }
874
875 /* Returns 1 always.  */
876
877 int
878 any_operand (op, mode)
879      rtx op ATTRIBUTE_UNUSED;
880      enum machine_mode mode ATTRIBUTE_UNUSED;
881 {
882   return 1;
883 }
884
885 /* Returns 1 if op is the count register.  */
886 int
887 count_register_operand (op, mode)
888      rtx op;
889      enum machine_mode mode ATTRIBUTE_UNUSED;
890 {
891   if (GET_CODE (op) != REG)
892     return 0;
893
894   if (REGNO (op) == COUNT_REGISTER_REGNUM)
895     return 1;
896
897   if (REGNO (op) > FIRST_PSEUDO_REGISTER)
898     return 1;
899
900   return 0;
901 }
902
903 /* Returns 1 if op is an altivec register.  */
904 int
905 altivec_register_operand (op, mode)
906      rtx op;
907      enum machine_mode mode ATTRIBUTE_UNUSED;
908 {
909   
910   return (register_operand (op, mode)
911           && (GET_CODE (op) != REG
912               || REGNO (op) > FIRST_PSEUDO_REGISTER
913               || ALTIVEC_REGNO_P (REGNO (op))));
914 }
915
916 int
917 xer_operand (op, mode)
918      rtx op;
919      enum machine_mode mode ATTRIBUTE_UNUSED;
920 {
921   if (GET_CODE (op) != REG)
922     return 0;
923
924   if (XER_REGNO_P (REGNO (op)))
925     return 1;
926
927   return 0;
928 }
929
930 /* Return 1 if OP is a signed 8-bit constant.  Int multiplication
931    by such constants completes more quickly.  */
932
933 int
934 s8bit_cint_operand (op, mode)
935      rtx op;
936      enum machine_mode mode ATTRIBUTE_UNUSED;
937 {
938   return ( GET_CODE (op) == CONST_INT
939           && (INTVAL (op) >= -128 && INTVAL (op) <= 127));
940 }
941
942 /* Return 1 if OP is a constant that can fit in a D field.  */
943
944 int
945 short_cint_operand (op, mode)
946      rtx op;
947      enum machine_mode mode ATTRIBUTE_UNUSED;
948 {
949   return (GET_CODE (op) == CONST_INT
950           && CONST_OK_FOR_LETTER_P (INTVAL (op), 'I'));
951 }
952
953 /* Similar for an unsigned D field.  */
954
955 int
956 u_short_cint_operand (op, mode)
957      rtx op;
958      enum machine_mode mode ATTRIBUTE_UNUSED;
959 {
960   return (GET_CODE (op) == CONST_INT
961           && CONST_OK_FOR_LETTER_P (INTVAL (op) & GET_MODE_MASK (mode), 'K'));
962 }
963
964 /* Return 1 if OP is a CONST_INT that cannot fit in a signed D field.  */
965
966 int
967 non_short_cint_operand (op, mode)
968      rtx op;
969      enum machine_mode mode ATTRIBUTE_UNUSED;
970 {
971   return (GET_CODE (op) == CONST_INT
972           && (unsigned HOST_WIDE_INT) (INTVAL (op) + 0x8000) >= 0x10000);
973 }
974
975 /* Returns 1 if OP is a CONST_INT that is a positive value
976    and an exact power of 2.  */
977
978 int
979 exact_log2_cint_operand (op, mode)
980      rtx op;
981      enum machine_mode mode ATTRIBUTE_UNUSED;
982 {
983   return (GET_CODE (op) == CONST_INT
984           && INTVAL (op) > 0
985           && exact_log2 (INTVAL (op)) >= 0);
986 }
987
988 /* Returns 1 if OP is a register that is not special (i.e., not MQ,
989    ctr, or lr).  */
990
991 int
992 gpc_reg_operand (op, mode)
993      rtx op;
994      enum machine_mode mode;
995 {
996   return (register_operand (op, mode)
997           && (GET_CODE (op) != REG
998               || (REGNO (op) >= ARG_POINTER_REGNUM 
999                   && !XER_REGNO_P (REGNO (op)))
1000               || REGNO (op) < MQ_REGNO));
1001 }
1002
1003 /* Returns 1 if OP is either a pseudo-register or a register denoting a
1004    CR field.  */
1005
1006 int
1007 cc_reg_operand (op, mode)
1008      rtx op;
1009      enum machine_mode mode;
1010 {
1011   return (register_operand (op, mode)
1012           && (GET_CODE (op) != REG
1013               || REGNO (op) >= FIRST_PSEUDO_REGISTER
1014               || CR_REGNO_P (REGNO (op))));
1015 }
1016
1017 /* Returns 1 if OP is either a pseudo-register or a register denoting a
1018    CR field that isn't CR0.  */
1019
1020 int
1021 cc_reg_not_cr0_operand (op, mode)
1022      rtx op;
1023      enum machine_mode mode;
1024 {
1025   return (register_operand (op, mode)
1026           && (GET_CODE (op) != REG
1027               || REGNO (op) >= FIRST_PSEUDO_REGISTER
1028               || CR_REGNO_NOT_CR0_P (REGNO (op))));
1029 }
1030
1031 /* Returns 1 if OP is either a constant integer valid for a D-field or
1032    a non-special register.  If a register, it must be in the proper
1033    mode unless MODE is VOIDmode.  */
1034
1035 int
1036 reg_or_short_operand (op, mode)
1037       rtx op;
1038       enum machine_mode mode;
1039 {
1040   return short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
1041 }
1042
1043 /* Similar, except check if the negation of the constant would be
1044    valid for a D-field.  */
1045
1046 int
1047 reg_or_neg_short_operand (op, mode)
1048       rtx op;
1049       enum machine_mode mode;
1050 {
1051   if (GET_CODE (op) == CONST_INT)
1052     return CONST_OK_FOR_LETTER_P (INTVAL (op), 'P');
1053
1054   return gpc_reg_operand (op, mode);
1055 }
1056
1057 /* Returns 1 if OP is either a constant integer valid for a DS-field or
1058    a non-special register.  If a register, it must be in the proper
1059    mode unless MODE is VOIDmode.  */
1060
1061 int
1062 reg_or_aligned_short_operand (op, mode)
1063       rtx op;
1064       enum machine_mode mode;
1065 {
1066   if (gpc_reg_operand (op, mode))
1067     return 1;
1068   else if (short_cint_operand (op, mode) && !(INTVAL (op) & 3))
1069     return 1;
1070
1071   return 0;
1072 }
1073
1074
1075 /* Return 1 if the operand is either a register or an integer whose
1076    high-order 16 bits are zero.  */
1077
1078 int
1079 reg_or_u_short_operand (op, mode)
1080      rtx op;
1081      enum machine_mode mode;
1082 {
1083   return u_short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
1084 }
1085
1086 /* Return 1 is the operand is either a non-special register or ANY
1087    constant integer.  */
1088
1089 int
1090 reg_or_cint_operand (op, mode)
1091     rtx op;
1092     enum machine_mode mode;
1093 {
1094   return (GET_CODE (op) == CONST_INT || gpc_reg_operand (op, mode));
1095 }
1096
1097 /* Return 1 is the operand is either a non-special register or ANY
1098    32-bit signed constant integer.  */
1099
1100 int
1101 reg_or_arith_cint_operand (op, mode)
1102     rtx op;
1103     enum machine_mode mode;
1104 {
1105   return (gpc_reg_operand (op, mode)
1106           || (GET_CODE (op) == CONST_INT
1107 #if HOST_BITS_PER_WIDE_INT != 32
1108               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80000000)
1109                   < (unsigned HOST_WIDE_INT) 0x100000000ll)
1110 #endif
1111               ));
1112 }
1113
1114 /* Return 1 is the operand is either a non-special register or a 32-bit
1115    signed constant integer valid for 64-bit addition.  */
1116
1117 int
1118 reg_or_add_cint64_operand (op, mode)
1119     rtx op;
1120     enum machine_mode mode;
1121 {
1122   return (gpc_reg_operand (op, mode)
1123           || (GET_CODE (op) == CONST_INT
1124 #if HOST_BITS_PER_WIDE_INT == 32
1125               && INTVAL (op) < 0x7fff8000
1126 #else
1127               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80008000)
1128                   < 0x100000000ll)
1129 #endif
1130               ));
1131 }
1132
1133 /* Return 1 is the operand is either a non-special register or a 32-bit
1134    signed constant integer valid for 64-bit subtraction.  */
1135
1136 int
1137 reg_or_sub_cint64_operand (op, mode)
1138     rtx op;
1139     enum machine_mode mode;
1140 {
1141   return (gpc_reg_operand (op, mode)
1142           || (GET_CODE (op) == CONST_INT
1143 #if HOST_BITS_PER_WIDE_INT == 32
1144               && (- INTVAL (op)) < 0x7fff8000
1145 #else
1146               && ((unsigned HOST_WIDE_INT) ((- INTVAL (op)) + 0x80008000)
1147                   < 0x100000000ll)
1148 #endif
1149               ));
1150 }
1151
1152 /* Return 1 is the operand is either a non-special register or ANY
1153    32-bit unsigned constant integer.  */
1154
1155 int
1156 reg_or_logical_cint_operand (op, mode)
1157     rtx op;
1158     enum machine_mode mode;
1159 {
1160   if (GET_CODE (op) == CONST_INT)
1161     {
1162       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
1163         {
1164           if (GET_MODE_BITSIZE (mode) <= 32)
1165             abort ();
1166
1167           if (INTVAL (op) < 0)
1168             return 0;
1169         }
1170
1171       return ((INTVAL (op) & GET_MODE_MASK (mode)
1172                & (~ (unsigned HOST_WIDE_INT) 0xffffffff)) == 0);
1173     }
1174   else if (GET_CODE (op) == CONST_DOUBLE)
1175     {
1176       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
1177           || mode != DImode)
1178         abort ();
1179
1180       return CONST_DOUBLE_HIGH (op) == 0;
1181     }
1182   else 
1183     return gpc_reg_operand (op, mode);
1184 }
1185
1186 /* Return 1 if the operand is an operand that can be loaded via the GOT.  */
1187
1188 int
1189 got_operand (op, mode)
1190      rtx op;
1191      enum machine_mode mode ATTRIBUTE_UNUSED;
1192 {
1193   return (GET_CODE (op) == SYMBOL_REF
1194           || GET_CODE (op) == CONST
1195           || GET_CODE (op) == LABEL_REF);
1196 }
1197
1198 /* Return 1 if the operand is a simple references that can be loaded via
1199    the GOT (labels involving addition aren't allowed).  */
1200
1201 int
1202 got_no_const_operand (op, mode)
1203      rtx op;
1204      enum machine_mode mode ATTRIBUTE_UNUSED;
1205 {
1206   return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF);
1207 }
1208
1209 /* Return the number of instructions it takes to form a constant in an
1210    integer register.  */
1211
1212 static int
1213 num_insns_constant_wide (value)
1214      HOST_WIDE_INT value;
1215 {
1216   /* signed constant loadable with {cal|addi} */
1217   if (CONST_OK_FOR_LETTER_P (value, 'I'))
1218     return 1;
1219
1220   /* constant loadable with {cau|addis} */
1221   else if (CONST_OK_FOR_LETTER_P (value, 'L'))
1222     return 1;
1223
1224 #if HOST_BITS_PER_WIDE_INT == 64
1225   else if (TARGET_POWERPC64)
1226     {
1227       HOST_WIDE_INT low  = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
1228       HOST_WIDE_INT high = value >> 31;
1229
1230       if (high == 0 || high == -1)
1231         return 2;
1232
1233       high >>= 1;
1234
1235       if (low == 0)
1236         return num_insns_constant_wide (high) + 1;
1237       else
1238         return (num_insns_constant_wide (high)
1239                 + num_insns_constant_wide (low) + 1);
1240     }
1241 #endif
1242
1243   else
1244     return 2;
1245 }
1246
1247 int
1248 num_insns_constant (op, mode)
1249      rtx op;
1250      enum machine_mode mode;
1251 {
1252   if (GET_CODE (op) == CONST_INT)
1253     {
1254 #if HOST_BITS_PER_WIDE_INT == 64
1255       if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
1256           && mask64_operand (op, mode))
1257             return 2;
1258       else
1259 #endif
1260         return num_insns_constant_wide (INTVAL (op));
1261     }
1262
1263   else if (GET_CODE (op) == CONST_DOUBLE && mode == SFmode)
1264     {
1265       long l;
1266       REAL_VALUE_TYPE rv;
1267
1268       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1269       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1270       return num_insns_constant_wide ((HOST_WIDE_INT) l);
1271     }
1272
1273   else if (GET_CODE (op) == CONST_DOUBLE)
1274     {
1275       HOST_WIDE_INT low;
1276       HOST_WIDE_INT high;
1277       long l[2];
1278       REAL_VALUE_TYPE rv;
1279       int endian = (WORDS_BIG_ENDIAN == 0);
1280
1281       if (mode == VOIDmode || mode == DImode)
1282         {
1283           high = CONST_DOUBLE_HIGH (op);
1284           low  = CONST_DOUBLE_LOW (op);
1285         }
1286       else
1287         {
1288           REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1289           REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
1290           high = l[endian];
1291           low  = l[1 - endian];
1292         }
1293
1294       if (TARGET_32BIT)
1295         return (num_insns_constant_wide (low)
1296                 + num_insns_constant_wide (high));
1297
1298       else
1299         {
1300           if (high == 0 && low >= 0)
1301             return num_insns_constant_wide (low);
1302
1303           else if (high == -1 && low < 0)
1304             return num_insns_constant_wide (low);
1305
1306           else if (mask64_operand (op, mode))
1307             return 2;
1308
1309           else if (low == 0)
1310             return num_insns_constant_wide (high) + 1;
1311
1312           else
1313             return (num_insns_constant_wide (high)
1314                     + num_insns_constant_wide (low) + 1);
1315         }
1316     }
1317
1318   else
1319     abort ();
1320 }
1321
1322 /* Return 1 if the operand is a CONST_DOUBLE and it can be put into a
1323    register with one instruction per word.  We only do this if we can
1324    safely read CONST_DOUBLE_{LOW,HIGH}.  */
1325
1326 int
1327 easy_fp_constant (op, mode)
1328      rtx op;
1329      enum machine_mode mode;
1330 {
1331   if (GET_CODE (op) != CONST_DOUBLE
1332       || GET_MODE (op) != mode
1333       || (GET_MODE_CLASS (mode) != MODE_FLOAT && mode != DImode))
1334     return 0;
1335
1336   /* Consider all constants with -msoft-float to be easy.  */
1337   if ((TARGET_SOFT_FLOAT || !TARGET_FPRS)
1338       && mode != DImode)
1339     return 1;
1340
1341   /* If we are using V.4 style PIC, consider all constants to be hard.  */
1342   if (flag_pic && DEFAULT_ABI == ABI_V4)
1343     return 0;
1344
1345 #ifdef TARGET_RELOCATABLE
1346   /* Similarly if we are using -mrelocatable, consider all constants
1347      to be hard.  */
1348   if (TARGET_RELOCATABLE)
1349     return 0;
1350 #endif
1351
1352   if (mode == TFmode)
1353     {
1354       long k[4];
1355       REAL_VALUE_TYPE rv;
1356
1357       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1358       REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
1359
1360       return (num_insns_constant_wide ((HOST_WIDE_INT) k[0]) == 1
1361               && num_insns_constant_wide ((HOST_WIDE_INT) k[1]) == 1
1362               && num_insns_constant_wide ((HOST_WIDE_INT) k[2]) == 1
1363               && num_insns_constant_wide ((HOST_WIDE_INT) k[3]) == 1);
1364     }
1365
1366   else if (mode == DFmode)
1367     {
1368       long k[2];
1369       REAL_VALUE_TYPE rv;
1370
1371       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1372       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
1373
1374       return (num_insns_constant_wide ((HOST_WIDE_INT) k[0]) == 1
1375               && num_insns_constant_wide ((HOST_WIDE_INT) k[1]) == 1);
1376     }
1377
1378   else if (mode == SFmode)
1379     {
1380       long l;
1381       REAL_VALUE_TYPE rv;
1382
1383       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1384       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1385
1386       return num_insns_constant_wide (l) == 1;
1387     }
1388
1389   else if (mode == DImode)
1390     return ((TARGET_POWERPC64
1391              && GET_CODE (op) == CONST_DOUBLE && CONST_DOUBLE_LOW (op) == 0)
1392             || (num_insns_constant (op, DImode) <= 2));
1393
1394   else if (mode == SImode)
1395     return 1;
1396   else
1397     abort ();
1398 }
1399
1400 /* Return 1 if the operand is a CONST_INT and can be put into a
1401    register with one instruction.  */
1402
1403 static int
1404 easy_vector_constant (op)
1405      rtx op;
1406 {
1407   rtx elt;
1408   int units, i;
1409
1410   if (GET_CODE (op) != CONST_VECTOR)
1411     return 0;
1412
1413   units = CONST_VECTOR_NUNITS (op);
1414
1415   /* We can generate 0 easily.  Look for that.  */
1416   for (i = 0; i < units; ++i)
1417     {
1418       elt = CONST_VECTOR_ELT (op, i);
1419
1420       /* We could probably simplify this by just checking for equality
1421          with CONST0_RTX for the current mode, but let's be safe
1422          instead.  */
1423
1424       switch (GET_CODE (elt))
1425         {
1426         case CONST_INT:
1427           if (INTVAL (elt) != 0)
1428             return 0;
1429           break;
1430         case CONST_DOUBLE:
1431           if (CONST_DOUBLE_LOW (elt) != 0 || CONST_DOUBLE_HIGH (elt) != 0)
1432             return 0;
1433           break;
1434         default:
1435           return 0;
1436         }
1437     }
1438
1439   /* We could probably generate a few other constants trivially, but
1440      gcc doesn't generate them yet.  FIXME later.  */
1441   return 1;
1442 }
1443
1444 /* Return 1 if the operand is the constant 0.  This works for scalars
1445    as well as vectors.  */
1446 int
1447 zero_constant (op, mode)
1448      rtx op;
1449      enum machine_mode mode;
1450 {
1451   return op == CONST0_RTX (mode);
1452 }
1453
1454 /* Return 1 if the operand is 0.0.  */
1455 int
1456 zero_fp_constant (op, mode)
1457      rtx op;
1458      enum machine_mode mode;
1459 {
1460   return GET_MODE_CLASS (mode) == MODE_FLOAT && op == CONST0_RTX (mode);
1461 }
1462
1463 /* Return 1 if the operand is in volatile memory.  Note that during
1464    the RTL generation phase, memory_operand does not return TRUE for
1465    volatile memory references.  So this function allows us to
1466    recognize volatile references where its safe.  */
1467
1468 int
1469 volatile_mem_operand (op, mode)
1470      rtx op;
1471      enum machine_mode mode;
1472 {
1473   if (GET_CODE (op) != MEM)
1474     return 0;
1475
1476   if (!MEM_VOLATILE_P (op))
1477     return 0;
1478
1479   if (mode != GET_MODE (op))
1480     return 0;
1481
1482   if (reload_completed)
1483     return memory_operand (op, mode);
1484
1485   if (reload_in_progress)
1486     return strict_memory_address_p (mode, XEXP (op, 0));
1487
1488   return memory_address_p (mode, XEXP (op, 0));
1489 }
1490
1491 /* Return 1 if the operand is an offsettable memory operand.  */
1492
1493 int
1494 offsettable_mem_operand (op, mode)
1495      rtx op;
1496      enum machine_mode mode;
1497 {
1498   return ((GET_CODE (op) == MEM)
1499           && offsettable_address_p (reload_completed || reload_in_progress,
1500                                     mode, XEXP (op, 0)));
1501 }
1502
1503 /* Return 1 if the operand is either an easy FP constant (see above) or
1504    memory.  */
1505
1506 int
1507 mem_or_easy_const_operand (op, mode)
1508      rtx op;
1509      enum machine_mode mode;
1510 {
1511   return memory_operand (op, mode) || easy_fp_constant (op, mode);
1512 }
1513
1514 /* Return 1 if the operand is either a non-special register or an item
1515    that can be used as the operand of a `mode' add insn.  */
1516
1517 int
1518 add_operand (op, mode)
1519     rtx op;
1520     enum machine_mode mode;
1521 {
1522   if (GET_CODE (op) == CONST_INT)
1523     return (CONST_OK_FOR_LETTER_P (INTVAL (op), 'I')
1524             || CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
1525
1526   return gpc_reg_operand (op, mode);
1527 }
1528
1529 /* Return 1 if OP is a constant but not a valid add_operand.  */
1530
1531 int
1532 non_add_cint_operand (op, mode)
1533      rtx op;
1534      enum machine_mode mode ATTRIBUTE_UNUSED;
1535 {
1536   return (GET_CODE (op) == CONST_INT
1537           && !CONST_OK_FOR_LETTER_P (INTVAL (op), 'I')
1538           && !CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
1539 }
1540
1541 /* Return 1 if the operand is a non-special register or a constant that
1542    can be used as the operand of an OR or XOR insn on the RS/6000.  */
1543
1544 int
1545 logical_operand (op, mode)
1546      rtx op;
1547      enum machine_mode mode;
1548 {
1549   HOST_WIDE_INT opl, oph;
1550
1551   if (gpc_reg_operand (op, mode))
1552     return 1;
1553
1554   if (GET_CODE (op) == CONST_INT)
1555     {
1556       opl = INTVAL (op) & GET_MODE_MASK (mode);
1557
1558 #if HOST_BITS_PER_WIDE_INT <= 32
1559       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT && opl < 0)
1560         return 0;
1561 #endif
1562     }
1563   else if (GET_CODE (op) == CONST_DOUBLE)
1564     {
1565       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
1566         abort ();
1567
1568       opl = CONST_DOUBLE_LOW (op);
1569       oph = CONST_DOUBLE_HIGH (op);
1570       if (oph != 0)
1571         return 0;
1572     }
1573   else
1574     return 0;
1575
1576   return ((opl & ~ (unsigned HOST_WIDE_INT) 0xffff) == 0
1577           || (opl & ~ (unsigned HOST_WIDE_INT) 0xffff0000) == 0);
1578 }
1579
1580 /* Return 1 if C is a constant that is not a logical operand (as
1581    above), but could be split into one.  */
1582
1583 int
1584 non_logical_cint_operand (op, mode)
1585      rtx op;
1586      enum machine_mode mode;
1587 {
1588   return ((GET_CODE (op) == CONST_INT || GET_CODE (op) == CONST_DOUBLE)
1589           && ! logical_operand (op, mode)
1590           && reg_or_logical_cint_operand (op, mode));
1591 }
1592
1593 /* Return 1 if C is a constant that can be encoded in a 32-bit mask on the
1594    RS/6000.  It is if there are no more than two 1->0 or 0->1 transitions.
1595    Reject all ones and all zeros, since these should have been optimized
1596    away and confuse the making of MB and ME.  */
1597
1598 int
1599 mask_operand (op, mode)
1600      rtx op;
1601      enum machine_mode mode ATTRIBUTE_UNUSED;
1602 {
1603   HOST_WIDE_INT c, lsb;
1604
1605   if (GET_CODE (op) != CONST_INT)
1606     return 0;
1607
1608   c = INTVAL (op);
1609
1610   /* Fail in 64-bit mode if the mask wraps around because the upper
1611      32-bits of the mask will all be 1s, contrary to GCC's internal view.  */
1612   if (TARGET_POWERPC64 && (c & 0x80000001) == 0x80000001)
1613     return 0;
1614
1615   /* We don't change the number of transitions by inverting,
1616      so make sure we start with the LS bit zero.  */
1617   if (c & 1)
1618     c = ~c;
1619
1620   /* Reject all zeros or all ones.  */
1621   if (c == 0)
1622     return 0;
1623
1624   /* Find the first transition.  */
1625   lsb = c & -c;
1626
1627   /* Invert to look for a second transition.  */
1628   c = ~c;
1629
1630   /* Erase first transition.  */
1631   c &= -lsb;
1632
1633   /* Find the second transition (if any).  */
1634   lsb = c & -c;
1635
1636   /* Match if all the bits above are 1's (or c is zero).  */
1637   return c == -lsb;
1638 }
1639
1640 /* Return 1 for the PowerPC64 rlwinm corner case.  */
1641
1642 int
1643 mask_operand_wrap (op, mode)
1644      rtx op;
1645      enum machine_mode mode ATTRIBUTE_UNUSED;
1646 {
1647   HOST_WIDE_INT c, lsb;
1648
1649   if (GET_CODE (op) != CONST_INT)
1650     return 0;
1651
1652   c = INTVAL (op);
1653
1654   if ((c & 0x80000001) != 0x80000001)
1655     return 0;
1656
1657   c = ~c;
1658   if (c == 0)
1659     return 0;
1660
1661   lsb = c & -c;
1662   c = ~c;
1663   c &= -lsb;
1664   lsb = c & -c;
1665   return c == -lsb;
1666 }
1667
1668 /* Return 1 if the operand is a constant that is a PowerPC64 mask.
1669    It is if there are no more than one 1->0 or 0->1 transitions.
1670    Reject all zeros, since zero should have been optimized away and
1671    confuses the making of MB and ME.  */
1672
1673 int
1674 mask64_operand (op, mode)
1675      rtx op;
1676      enum machine_mode mode ATTRIBUTE_UNUSED;
1677 {
1678   if (GET_CODE (op) == CONST_INT)
1679     {
1680       HOST_WIDE_INT c, lsb;
1681
1682       c = INTVAL (op);
1683
1684       /* Reject all zeros.  */
1685       if (c == 0)
1686         return 0;
1687
1688       /* We don't change the number of transitions by inverting,
1689          so make sure we start with the LS bit zero.  */
1690       if (c & 1)
1691         c = ~c;
1692
1693       /* Find the transition, and check that all bits above are 1's.  */
1694       lsb = c & -c;
1695
1696       /* Match if all the bits above are 1's (or c is zero).  */
1697       return c == -lsb;
1698     }
1699   return 0;
1700 }
1701
1702 /* Like mask64_operand, but allow up to three transitions.  This
1703    predicate is used by insn patterns that generate two rldicl or
1704    rldicr machine insns.  */
1705
1706 int
1707 mask64_2_operand (op, mode)
1708      rtx op;
1709      enum machine_mode mode ATTRIBUTE_UNUSED;
1710 {
1711   if (GET_CODE (op) == CONST_INT)
1712     {
1713       HOST_WIDE_INT c, lsb;
1714
1715       c = INTVAL (op);
1716
1717       /* Disallow all zeros.  */
1718       if (c == 0)
1719         return 0;
1720
1721       /* We don't change the number of transitions by inverting,
1722          so make sure we start with the LS bit zero.  */
1723       if (c & 1)
1724         c = ~c;
1725
1726       /* Find the first transition.  */
1727       lsb = c & -c;
1728
1729       /* Invert to look for a second transition.  */
1730       c = ~c;
1731
1732       /* Erase first transition.  */
1733       c &= -lsb;
1734
1735       /* Find the second transition.  */
1736       lsb = c & -c;
1737
1738       /* Invert to look for a third transition.  */
1739       c = ~c;
1740
1741       /* Erase second transition.  */
1742       c &= -lsb;
1743
1744       /* Find the third transition (if any).  */
1745       lsb = c & -c;
1746
1747       /* Match if all the bits above are 1's (or c is zero).  */
1748       return c == -lsb;
1749     }
1750   return 0;
1751 }
1752
1753 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
1754    implement ANDing by the mask IN.  */
1755 void
1756 build_mask64_2_operands (in, out)
1757      rtx in;
1758      rtx *out;
1759 {
1760 #if HOST_BITS_PER_WIDE_INT >= 64
1761   unsigned HOST_WIDE_INT c, lsb, m1, m2;
1762   int shift;
1763
1764   if (GET_CODE (in) != CONST_INT)
1765     abort ();
1766
1767   c = INTVAL (in);
1768   if (c & 1)
1769     {
1770       /* Assume c initially something like 0x00fff000000fffff.  The idea
1771          is to rotate the word so that the middle ^^^^^^ group of zeros
1772          is at the MS end and can be cleared with an rldicl mask.  We then
1773          rotate back and clear off the MS    ^^ group of zeros with a
1774          second rldicl.  */
1775       c = ~c;                   /*   c == 0xff000ffffff00000 */
1776       lsb = c & -c;             /* lsb == 0x0000000000100000 */
1777       m1 = -lsb;                /*  m1 == 0xfffffffffff00000 */
1778       c = ~c;                   /*   c == 0x00fff000000fffff */
1779       c &= -lsb;                /*   c == 0x00fff00000000000 */
1780       lsb = c & -c;             /* lsb == 0x0000100000000000 */
1781       c = ~c;                   /*   c == 0xff000fffffffffff */
1782       c &= -lsb;                /*   c == 0xff00000000000000 */
1783       shift = 0;
1784       while ((lsb >>= 1) != 0)
1785         shift++;                /* shift == 44 on exit from loop */
1786       m1 <<= 64 - shift;        /*  m1 == 0xffffff0000000000 */
1787       m1 = ~m1;                 /*  m1 == 0x000000ffffffffff */
1788       m2 = ~c;                  /*  m2 == 0x00ffffffffffffff */
1789     }
1790   else
1791     {
1792       /* Assume c initially something like 0xff000f0000000000.  The idea
1793          is to rotate the word so that the     ^^^  middle group of zeros
1794          is at the LS end and can be cleared with an rldicr mask.  We then
1795          rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
1796          a second rldicr.  */
1797       lsb = c & -c;             /* lsb == 0x0000010000000000 */
1798       m2 = -lsb;                /*  m2 == 0xffffff0000000000 */
1799       c = ~c;                   /*   c == 0x00fff0ffffffffff */
1800       c &= -lsb;                /*   c == 0x00fff00000000000 */
1801       lsb = c & -c;             /* lsb == 0x0000100000000000 */
1802       c = ~c;                   /*   c == 0xff000fffffffffff */
1803       c &= -lsb;                /*   c == 0xff00000000000000 */
1804       shift = 0;
1805       while ((lsb >>= 1) != 0)
1806         shift++;                /* shift == 44 on exit from loop */
1807       m1 = ~c;                  /*  m1 == 0x00ffffffffffffff */
1808       m1 >>= shift;             /*  m1 == 0x0000000000000fff */
1809       m1 = ~m1;                 /*  m1 == 0xfffffffffffff000 */
1810     }
1811
1812   /* Note that when we only have two 0->1 and 1->0 transitions, one of the
1813      masks will be all 1's.  We are guaranteed more than one transition.  */
1814   out[0] = GEN_INT (64 - shift);
1815   out[1] = GEN_INT (m1);
1816   out[2] = GEN_INT (shift);
1817   out[3] = GEN_INT (m2);
1818 #else
1819   (void)in;
1820   (void)out;
1821   abort ();
1822 #endif
1823 }
1824
1825 /* Return 1 if the operand is either a non-special register or a constant
1826    that can be used as the operand of a PowerPC64 logical AND insn.  */
1827
1828 int
1829 and64_operand (op, mode)
1830     rtx op;
1831     enum machine_mode mode;
1832 {
1833   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
1834     return (gpc_reg_operand (op, mode) || mask64_operand (op, mode));
1835
1836   return (logical_operand (op, mode) || mask64_operand (op, mode));
1837 }
1838
1839 /* Like the above, but also match constants that can be implemented
1840    with two rldicl or rldicr insns.  */
1841
1842 int
1843 and64_2_operand (op, mode)
1844     rtx op;
1845     enum machine_mode mode;
1846 {
1847   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis. */
1848     return gpc_reg_operand (op, mode) || mask64_2_operand (op, mode);
1849
1850   return logical_operand (op, mode) || mask64_2_operand (op, mode);
1851 }
1852
1853 /* Return 1 if the operand is either a non-special register or a
1854    constant that can be used as the operand of an RS/6000 logical AND insn.  */
1855
1856 int
1857 and_operand (op, mode)
1858     rtx op;
1859     enum machine_mode mode;
1860 {
1861   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
1862     return (gpc_reg_operand (op, mode) || mask_operand (op, mode));
1863
1864   return (logical_operand (op, mode) || mask_operand (op, mode));
1865 }
1866
1867 /* Return 1 if the operand is a general register or memory operand.  */
1868
1869 int
1870 reg_or_mem_operand (op, mode)
1871      rtx op;
1872      enum machine_mode mode;
1873 {
1874   return (gpc_reg_operand (op, mode)
1875           || memory_operand (op, mode)
1876           || volatile_mem_operand (op, mode));
1877 }
1878
1879 /* Return 1 if the operand is a general register or memory operand without
1880    pre_inc or pre_dec which produces invalid form of PowerPC lwa
1881    instruction.  */
1882
1883 int
1884 lwa_operand (op, mode)
1885      rtx op;
1886      enum machine_mode mode;
1887 {
1888   rtx inner = op;
1889
1890   if (reload_completed && GET_CODE (inner) == SUBREG)
1891     inner = SUBREG_REG (inner);
1892     
1893   return gpc_reg_operand (inner, mode)
1894     || (memory_operand (inner, mode)
1895         && GET_CODE (XEXP (inner, 0)) != PRE_INC
1896         && GET_CODE (XEXP (inner, 0)) != PRE_DEC
1897         && (GET_CODE (XEXP (inner, 0)) != PLUS
1898             || GET_CODE (XEXP (XEXP (inner, 0), 1)) != CONST_INT
1899             || INTVAL (XEXP (XEXP (inner, 0), 1)) % 4 == 0));
1900 }
1901
1902 /* Return 1 if the operand, used inside a MEM, is a SYMBOL_REF.  */
1903
1904 int
1905 symbol_ref_operand (op, mode)
1906      rtx op;
1907      enum machine_mode mode;
1908 {
1909   if (mode != VOIDmode && GET_MODE (op) != mode)
1910     return 0;
1911
1912   return (GET_CODE (op) == SYMBOL_REF);
1913 }
1914
1915 /* Return 1 if the operand, used inside a MEM, is a valid first argument
1916    to CALL.  This is a SYMBOL_REF, a pseudo-register, LR or CTR.  */
1917
1918 int
1919 call_operand (op, mode)
1920      rtx op;
1921      enum machine_mode mode;
1922 {
1923   if (mode != VOIDmode && GET_MODE (op) != mode)
1924     return 0;
1925
1926   return (GET_CODE (op) == SYMBOL_REF
1927           || (GET_CODE (op) == REG
1928               && (REGNO (op) == LINK_REGISTER_REGNUM
1929                   || REGNO (op) == COUNT_REGISTER_REGNUM
1930                   || REGNO (op) >= FIRST_PSEUDO_REGISTER)));
1931 }
1932
1933 /* Return 1 if the operand is a SYMBOL_REF for a function known to be in
1934    this file and the function is not weakly defined.  */
1935
1936 int
1937 current_file_function_operand (op, mode)
1938      rtx op;
1939      enum machine_mode mode ATTRIBUTE_UNUSED;
1940 {
1941   return (GET_CODE (op) == SYMBOL_REF
1942           && (SYMBOL_REF_FLAG (op)
1943               || (op == XEXP (DECL_RTL (current_function_decl), 0)
1944                   && ! DECL_WEAK (current_function_decl))));
1945 }
1946
1947 /* Return 1 if this operand is a valid input for a move insn.  */
1948
1949 int
1950 input_operand (op, mode)
1951      rtx op;
1952      enum machine_mode mode;
1953 {
1954   /* Memory is always valid.  */
1955   if (memory_operand (op, mode))
1956     return 1;
1957
1958   /* Only a tiny bit of handling for CONSTANT_P_RTX is necessary.  */
1959   if (GET_CODE (op) == CONSTANT_P_RTX)
1960     return 1;
1961
1962   /* For floating-point, easy constants are valid.  */
1963   if (GET_MODE_CLASS (mode) == MODE_FLOAT
1964       && CONSTANT_P (op)
1965       && easy_fp_constant (op, mode))
1966     return 1;
1967
1968   /* Allow any integer constant.  */
1969   if (GET_MODE_CLASS (mode) == MODE_INT
1970       && (GET_CODE (op) == CONST_INT
1971           || GET_CODE (op) == CONST_DOUBLE))
1972     return 1;
1973
1974   /* For floating-point or multi-word mode, the only remaining valid type
1975      is a register.  */
1976   if (GET_MODE_CLASS (mode) == MODE_FLOAT
1977       || GET_MODE_SIZE (mode) > UNITS_PER_WORD)
1978     return register_operand (op, mode);
1979
1980   /* The only cases left are integral modes one word or smaller (we
1981      do not get called for MODE_CC values).  These can be in any
1982      register.  */
1983   if (register_operand (op, mode))
1984     return 1;
1985
1986   /* A SYMBOL_REF referring to the TOC is valid.  */
1987   if (LEGITIMATE_CONSTANT_POOL_ADDRESS_P (op))
1988     return 1;
1989
1990   /* A constant pool expression (relative to the TOC) is valid */
1991   if (TOC_RELATIVE_EXPR_P (op))
1992     return 1;
1993
1994   /* V.4 allows SYMBOL_REFs and CONSTs that are in the small data region
1995      to be valid.  */
1996   if (DEFAULT_ABI == ABI_V4
1997       && (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST)
1998       && small_data_operand (op, Pmode))
1999     return 1;
2000
2001   return 0;
2002 }
2003
2004 /* Return 1 for an operand in small memory on V.4/eabi.  */
2005
2006 int
2007 small_data_operand (op, mode)
2008      rtx op ATTRIBUTE_UNUSED;
2009      enum machine_mode mode ATTRIBUTE_UNUSED;
2010 {
2011 #if TARGET_ELF
2012   rtx sym_ref;
2013
2014   if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
2015     return 0;
2016
2017   if (DEFAULT_ABI != ABI_V4)
2018     return 0;
2019
2020   if (GET_CODE (op) == SYMBOL_REF)
2021     sym_ref = op;
2022
2023   else if (GET_CODE (op) != CONST
2024            || GET_CODE (XEXP (op, 0)) != PLUS
2025            || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
2026            || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
2027     return 0;
2028
2029   else
2030     {
2031       rtx sum = XEXP (op, 0);
2032       HOST_WIDE_INT summand;
2033
2034       /* We have to be careful here, because it is the referenced address
2035         that must be 32k from _SDA_BASE_, not just the symbol.  */
2036       summand = INTVAL (XEXP (sum, 1));
2037       if (summand < 0 || summand > g_switch_value)
2038        return 0;
2039
2040       sym_ref = XEXP (sum, 0);
2041     }
2042
2043   if (*XSTR (sym_ref, 0) != '@')
2044     return 0;
2045
2046   return 1;
2047
2048 #else
2049   return 0;
2050 #endif
2051 }
2052 \f
2053 static int 
2054 constant_pool_expr_1 (op, have_sym, have_toc) 
2055     rtx op;
2056     int *have_sym;
2057     int *have_toc;
2058 {
2059   switch (GET_CODE(op)) 
2060     {
2061     case SYMBOL_REF:
2062       if (CONSTANT_POOL_ADDRESS_P (op))
2063         {
2064           if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (op), Pmode))
2065             {
2066               *have_sym = 1;
2067               return 1;
2068             }
2069           else
2070             return 0;
2071         }
2072       else if (! strcmp (XSTR (op, 0), toc_label_name))
2073         {
2074           *have_toc = 1;
2075           return 1;
2076         }
2077       else
2078         return 0;
2079     case PLUS:
2080     case MINUS:
2081       return (constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc)
2082               && constant_pool_expr_1 (XEXP (op, 1), have_sym, have_toc));
2083     case CONST:
2084       return constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc);
2085     case CONST_INT:
2086       return 1;
2087     default:
2088       return 0;
2089     }
2090 }
2091
2092 int
2093 constant_pool_expr_p (op)
2094     rtx op;
2095 {
2096   int have_sym = 0;
2097   int have_toc = 0;
2098   return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_sym;
2099 }
2100
2101 int
2102 toc_relative_expr_p (op)
2103     rtx op;
2104 {
2105     int have_sym = 0;
2106     int have_toc = 0;
2107     return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_toc;
2108 }
2109
2110 /* Try machine-dependent ways of modifying an illegitimate address
2111    to be legitimate.  If we find one, return the new, valid address.
2112    This is used from only one place: `memory_address' in explow.c.
2113
2114    OLDX is the address as it was before break_out_memory_refs was
2115    called.  In some cases it is useful to look at this to decide what
2116    needs to be done.
2117
2118    MODE is passed so that this function can use GO_IF_LEGITIMATE_ADDRESS.
2119
2120    It is always safe for this function to do nothing.  It exists to
2121    recognize opportunities to optimize the output.
2122
2123    On RS/6000, first check for the sum of a register with a constant
2124    integer that is out of range.  If so, generate code to add the
2125    constant with the low-order 16 bits masked to the register and force
2126    this result into another register (this can be done with `cau').
2127    Then generate an address of REG+(CONST&0xffff), allowing for the
2128    possibility of bit 16 being a one.
2129
2130    Then check for the sum of a register and something not constant, try to
2131    load the other things into a register and return the sum.  */
2132 rtx
2133 rs6000_legitimize_address (x, oldx, mode)
2134      rtx x;
2135      rtx oldx ATTRIBUTE_UNUSED;
2136      enum machine_mode mode;
2137 {
2138   if (GET_CODE (x) == PLUS 
2139       && GET_CODE (XEXP (x, 0)) == REG
2140       && GET_CODE (XEXP (x, 1)) == CONST_INT
2141       && (unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000) >= 0x10000)
2142     { 
2143       HOST_WIDE_INT high_int, low_int;
2144       rtx sum;
2145       low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
2146       high_int = INTVAL (XEXP (x, 1)) - low_int;
2147       sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
2148                                          GEN_INT (high_int)), 0);
2149       return gen_rtx_PLUS (Pmode, sum, GEN_INT (low_int));
2150     }
2151   else if (GET_CODE (x) == PLUS 
2152            && GET_CODE (XEXP (x, 0)) == REG
2153            && GET_CODE (XEXP (x, 1)) != CONST_INT
2154            && GET_MODE_NUNITS (mode) == 1
2155            && ((TARGET_HARD_FLOAT && TARGET_FPRS)
2156                || TARGET_POWERPC64
2157                || (mode != DFmode && mode != TFmode))
2158            && (TARGET_POWERPC64 || mode != DImode)
2159            && mode != TImode)
2160     {
2161       return gen_rtx_PLUS (Pmode, XEXP (x, 0),
2162                            force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
2163     }
2164   else if (ALTIVEC_VECTOR_MODE (mode))
2165     {
2166       rtx reg;
2167
2168       /* Make sure both operands are registers.  */
2169       if (GET_CODE (x) == PLUS)
2170         return gen_rtx_PLUS (Pmode, force_reg (Pmode, XEXP (x, 0)),
2171                              force_reg (Pmode, XEXP (x, 1)));
2172
2173       reg = force_reg (Pmode, x);
2174       return reg;
2175     }
2176   else if (SPE_VECTOR_MODE (mode))
2177     {
2178       /* We accept [reg + reg] and [reg + OFFSET].  */
2179
2180       if (GET_CODE (x) == PLUS)
2181       {
2182         rtx op1 = XEXP (x, 0);
2183         rtx op2 = XEXP (x, 1);
2184
2185         op1 = force_reg (Pmode, op1);
2186
2187         if (GET_CODE (op2) != REG
2188             && (GET_CODE (op2) != CONST_INT
2189                 || !SPE_CONST_OFFSET_OK (INTVAL (op2))))
2190           op2 = force_reg (Pmode, op2);
2191
2192         return gen_rtx_PLUS (Pmode, op1, op2);
2193       }
2194
2195       return force_reg (Pmode, x);
2196     }
2197   else if (TARGET_ELF && TARGET_32BIT && TARGET_NO_TOC && ! flag_pic
2198            && GET_CODE (x) != CONST_INT
2199            && GET_CODE (x) != CONST_DOUBLE 
2200            && CONSTANT_P (x)
2201            && GET_MODE_NUNITS (mode) == 1
2202            && (GET_MODE_BITSIZE (mode) <= 32
2203                || ((TARGET_HARD_FLOAT && TARGET_FPRS) && mode == DFmode)))
2204     {
2205       rtx reg = gen_reg_rtx (Pmode);
2206       emit_insn (gen_elf_high (reg, (x)));
2207       return gen_rtx_LO_SUM (Pmode, reg, (x));
2208     }
2209   else if (TARGET_MACHO && TARGET_32BIT && TARGET_NO_TOC
2210            && ! flag_pic
2211            && GET_CODE (x) != CONST_INT
2212            && GET_CODE (x) != CONST_DOUBLE 
2213            && CONSTANT_P (x)
2214            && ((TARGET_HARD_FLOAT && TARGET_FPRS) || mode != DFmode)
2215            && mode != DImode 
2216            && mode != TImode)
2217     {
2218       rtx reg = gen_reg_rtx (Pmode);
2219       emit_insn (gen_macho_high (reg, (x)));
2220       return gen_rtx_LO_SUM (Pmode, reg, (x));
2221     }
2222   else if (TARGET_TOC 
2223            && CONSTANT_POOL_EXPR_P (x)
2224            && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
2225     {
2226       return create_TOC_reference (x);
2227     }
2228   else
2229     return NULL_RTX;
2230 }
2231
2232 /* The convention appears to be to define this wherever it is used.
2233    With legitimize_reload_address now defined here, REG_MODE_OK_FOR_BASE_P
2234    is now used here.  */
2235 #ifndef REG_MODE_OK_FOR_BASE_P
2236 #define REG_MODE_OK_FOR_BASE_P(REGNO, MODE) REG_OK_FOR_BASE_P (REGNO)
2237 #endif
2238
2239 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS.  Returns a value to
2240    replace the input X, or the original X if no replacement is called for.
2241    The output parameter *WIN is 1 if the calling macro should goto WIN,
2242    0 if it should not.
2243
2244    For RS/6000, we wish to handle large displacements off a base
2245    register by splitting the addend across an addiu/addis and the mem insn.
2246    This cuts number of extra insns needed from 3 to 1.
2247
2248    On Darwin, we use this to generate code for floating point constants.
2249    A movsf_low is generated so we wind up with 2 instructions rather than 3.
2250    The Darwin code is inside #if TARGET_MACHO because only then is
2251    machopic_function_base_name() defined.  */
2252 rtx
2253 rs6000_legitimize_reload_address (x, mode, opnum, type, ind_levels, win)
2254     rtx x;
2255     enum machine_mode mode;
2256     int opnum;
2257     int type;
2258     int ind_levels ATTRIBUTE_UNUSED;
2259     int *win;
2260 {
2261   /* We must recognize output that we have already generated ourselves.  */ 
2262   if (GET_CODE (x) == PLUS
2263       && GET_CODE (XEXP (x, 0)) == PLUS
2264       && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
2265       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2266       && GET_CODE (XEXP (x, 1)) == CONST_INT)
2267     {
2268       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2269                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
2270                    opnum, (enum reload_type)type);
2271       *win = 1;
2272       return x;
2273     }
2274
2275 #if TARGET_MACHO
2276   if (DEFAULT_ABI == ABI_DARWIN && flag_pic
2277       && GET_CODE (x) == LO_SUM
2278       && GET_CODE (XEXP (x, 0)) == PLUS
2279       && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
2280       && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
2281       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 0)) == CONST
2282       && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
2283       && GET_CODE (XEXP (XEXP (x, 1), 0)) == MINUS
2284       && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 0)) == SYMBOL_REF
2285       && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 1)) == SYMBOL_REF)
2286     {
2287       /* Result of previous invocation of this function on Darwin
2288          floating point constant.  */
2289       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2290                 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
2291                 opnum, (enum reload_type)type);
2292       *win = 1;
2293       return x;
2294     }
2295 #endif
2296   if (GET_CODE (x) == PLUS
2297       && GET_CODE (XEXP (x, 0)) == REG
2298       && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2299       && REG_MODE_OK_FOR_BASE_P (XEXP (x, 0), mode)
2300       && GET_CODE (XEXP (x, 1)) == CONST_INT
2301       && !SPE_VECTOR_MODE (mode)
2302       && !ALTIVEC_VECTOR_MODE (mode))
2303     {
2304       HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
2305       HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
2306       HOST_WIDE_INT high
2307         = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
2308
2309       /* Check for 32-bit overflow.  */
2310       if (high + low != val)
2311         {
2312           *win = 0;
2313           return x;
2314         }
2315
2316       /* Reload the high part into a base reg; leave the low part
2317          in the mem directly.  */
2318
2319       x = gen_rtx_PLUS (GET_MODE (x),
2320                         gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
2321                                       GEN_INT (high)),
2322                         GEN_INT (low));
2323
2324       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2325                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
2326                    opnum, (enum reload_type)type);
2327       *win = 1;
2328       return x;
2329     }
2330 #if TARGET_MACHO
2331   if (GET_CODE (x) == SYMBOL_REF
2332       && DEFAULT_ABI == ABI_DARWIN
2333       && !ALTIVEC_VECTOR_MODE (mode)
2334       && flag_pic)
2335     {
2336       /* Darwin load of floating point constant.  */
2337       rtx offset = gen_rtx (CONST, Pmode,
2338                     gen_rtx (MINUS, Pmode, x,
2339                     gen_rtx (SYMBOL_REF, Pmode,
2340                         machopic_function_base_name ())));
2341       x = gen_rtx (LO_SUM, GET_MODE (x),
2342             gen_rtx (PLUS, Pmode, pic_offset_table_rtx,
2343                 gen_rtx (HIGH, Pmode, offset)), offset);
2344       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2345                 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
2346                 opnum, (enum reload_type)type);
2347       *win = 1;
2348       return x;
2349     }
2350 #endif
2351   if (TARGET_TOC
2352       && CONSTANT_POOL_EXPR_P (x)
2353       && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), mode))
2354     {
2355       (x) = create_TOC_reference (x);
2356       *win = 1;
2357       return x;
2358     }
2359   *win = 0;
2360   return x;
2361 }    
2362
2363 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression
2364    that is a valid memory address for an instruction.
2365    The MODE argument is the machine mode for the MEM expression
2366    that wants to use this address.
2367
2368    On the RS/6000, there are four valid address: a SYMBOL_REF that
2369    refers to a constant pool entry of an address (or the sum of it
2370    plus a constant), a short (16-bit signed) constant plus a register,
2371    the sum of two registers, or a register indirect, possibly with an
2372    auto-increment.  For DFmode and DImode with a constant plus register,
2373    we must ensure that both words are addressable or PowerPC64 with offset
2374    word aligned.
2375
2376    For modes spanning multiple registers (DFmode in 32-bit GPRs,
2377    32-bit DImode, TImode), indexed addressing cannot be used because
2378    adjacent memory cells are accessed by adding word-sized offsets
2379    during assembly output.  */
2380 int
2381 rs6000_legitimate_address (mode, x, reg_ok_strict)
2382     enum machine_mode mode;
2383     rtx x;
2384     int reg_ok_strict;
2385 {
2386   if (LEGITIMATE_INDIRECT_ADDRESS_P (x, reg_ok_strict))
2387     return 1;
2388   if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
2389       && !ALTIVEC_VECTOR_MODE (mode)
2390       && !SPE_VECTOR_MODE (mode)
2391       && TARGET_UPDATE
2392       && LEGITIMATE_INDIRECT_ADDRESS_P (XEXP (x, 0), reg_ok_strict))
2393     return 1;
2394   if (LEGITIMATE_SMALL_DATA_P (mode, x))
2395     return 1;
2396   if (LEGITIMATE_CONSTANT_POOL_ADDRESS_P (x))
2397     return 1;
2398   /* If not REG_OK_STRICT (before reload) let pass any stack offset.  */
2399   if (! reg_ok_strict
2400       && GET_CODE (x) == PLUS
2401       && GET_CODE (XEXP (x, 0)) == REG
2402       && XEXP (x, 0) == virtual_stack_vars_rtx
2403       && GET_CODE (XEXP (x, 1)) == CONST_INT)
2404     return 1;
2405   if (LEGITIMATE_OFFSET_ADDRESS_P (mode, x, reg_ok_strict))
2406     return 1;
2407   if (mode != TImode
2408       && ((TARGET_HARD_FLOAT && TARGET_FPRS)
2409           || TARGET_POWERPC64
2410           || (mode != DFmode && mode != TFmode))
2411       && (TARGET_POWERPC64 || mode != DImode)
2412       && LEGITIMATE_INDEXED_ADDRESS_P (x, reg_ok_strict))
2413     return 1;
2414   if (LEGITIMATE_LO_SUM_ADDRESS_P (mode, x, reg_ok_strict))
2415     return 1;
2416   return 0;
2417 }
2418 \f
2419 /* Try to output insns to set TARGET equal to the constant C if it can
2420    be done in less than N insns.  Do all computations in MODE.
2421    Returns the place where the output has been placed if it can be
2422    done and the insns have been emitted.  If it would take more than N
2423    insns, zero is returned and no insns and emitted.  */
2424
2425 rtx
2426 rs6000_emit_set_const (dest, mode, source, n)
2427      rtx dest, source;
2428      enum machine_mode mode;
2429      int n ATTRIBUTE_UNUSED;
2430 {
2431   rtx result, insn, set;
2432   HOST_WIDE_INT c0, c1;
2433
2434   if (mode == QImode || mode == HImode)
2435     {
2436       if (dest == NULL)
2437         dest = gen_reg_rtx (mode);
2438       emit_insn (gen_rtx_SET (VOIDmode, dest, source));
2439       return dest;
2440     }
2441   else if (mode == SImode)
2442     {
2443       result = no_new_pseudos ? dest : gen_reg_rtx (SImode);
2444
2445       emit_insn (gen_rtx_SET (VOIDmode, result,
2446                               GEN_INT (INTVAL (source)
2447                                        & (~ (HOST_WIDE_INT) 0xffff))));
2448       emit_insn (gen_rtx_SET (VOIDmode, dest,
2449                               gen_rtx_IOR (SImode, result,
2450                                            GEN_INT (INTVAL (source) & 0xffff))));
2451       result = dest;
2452     }
2453   else if (mode == DImode)
2454     {
2455       if (GET_CODE (source) == CONST_INT)
2456         {
2457           c0 = INTVAL (source);
2458           c1 = -(c0 < 0);
2459         }
2460       else if (GET_CODE (source) == CONST_DOUBLE)
2461         {
2462 #if HOST_BITS_PER_WIDE_INT >= 64
2463           c0 = CONST_DOUBLE_LOW (source);
2464           c1 = -(c0 < 0);
2465 #else
2466           c0 = CONST_DOUBLE_LOW (source);
2467           c1 = CONST_DOUBLE_HIGH (source);
2468 #endif
2469         }
2470       else
2471         abort ();
2472
2473       result = rs6000_emit_set_long_const (dest, c0, c1);
2474     }
2475   else
2476     abort ();
2477
2478   insn = get_last_insn ();
2479   set = single_set (insn);
2480   if (! CONSTANT_P (SET_SRC (set)))
2481     set_unique_reg_note (insn, REG_EQUAL, source);
2482
2483   return result;
2484 }
2485
2486 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
2487    fall back to a straight forward decomposition.  We do this to avoid
2488    exponential run times encountered when looking for longer sequences
2489    with rs6000_emit_set_const.  */
2490 static rtx
2491 rs6000_emit_set_long_const (dest, c1, c2)
2492      rtx dest;
2493      HOST_WIDE_INT c1, c2;
2494 {
2495   if (!TARGET_POWERPC64)
2496     {
2497       rtx operand1, operand2;
2498
2499       operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
2500                                         DImode);
2501       operand2 = operand_subword_force (dest, WORDS_BIG_ENDIAN != 0,
2502                                         DImode);
2503       emit_move_insn (operand1, GEN_INT (c1));
2504       emit_move_insn (operand2, GEN_INT (c2));
2505     }
2506   else
2507     {
2508       HOST_WIDE_INT ud1, ud2, ud3, ud4;
2509
2510       ud1 = c1 & 0xffff;
2511       ud2 = (c1 & 0xffff0000) >> 16;
2512 #if HOST_BITS_PER_WIDE_INT >= 64
2513       c2 = c1 >> 32;
2514 #endif
2515       ud3 = c2 & 0xffff;
2516       ud4 = (c2 & 0xffff0000) >> 16;
2517
2518       if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000)) 
2519           || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
2520         {
2521           if (ud1 & 0x8000)
2522             emit_move_insn (dest, GEN_INT (((ud1  ^ 0x8000) -  0x8000)));
2523           else
2524             emit_move_insn (dest, GEN_INT (ud1));
2525         }
2526
2527       else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000)) 
2528                || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
2529         {
2530           if (ud2 & 0x8000)
2531             emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000) 
2532                                            - 0x80000000));
2533           else
2534             emit_move_insn (dest, GEN_INT (ud2 << 16));
2535           if (ud1 != 0)
2536             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
2537         }
2538       else if ((ud4 == 0xffff && (ud3 & 0x8000)) 
2539                || (ud4 == 0 && ! (ud3 & 0x8000)))
2540         {
2541           if (ud3 & 0x8000)
2542             emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000) 
2543                                            - 0x80000000));
2544           else
2545             emit_move_insn (dest, GEN_INT (ud3 << 16));
2546
2547           if (ud2 != 0)
2548             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud2)));
2549           emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (16)));
2550           if (ud1 != 0)
2551             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
2552         }
2553       else 
2554         {
2555           if (ud4 & 0x8000)
2556             emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000) 
2557                                            - 0x80000000));
2558           else
2559             emit_move_insn (dest, GEN_INT (ud4 << 16));
2560
2561           if (ud3 != 0)
2562             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud3)));
2563
2564           emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (32)));
2565           if (ud2 != 0)
2566             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, 
2567                                                GEN_INT (ud2 << 16)));   
2568           if (ud1 != 0)
2569             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
2570         }
2571     }
2572   return dest;
2573 }
2574
2575 /* Emit a move from SOURCE to DEST in mode MODE.  */
2576 void
2577 rs6000_emit_move (dest, source, mode)
2578      rtx dest;
2579      rtx source;
2580      enum machine_mode mode;
2581 {
2582   rtx operands[2];
2583   operands[0] = dest;
2584   operands[1] = source;
2585   
2586   /* Sanity checks.  Check that we get CONST_DOUBLE only when we should.  */
2587   if (GET_CODE (operands[1]) == CONST_DOUBLE
2588       && ! FLOAT_MODE_P (mode)
2589       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
2590     {
2591       /* FIXME.  This should never happen.  */
2592       /* Since it seems that it does, do the safe thing and convert
2593          to a CONST_INT.  */
2594       operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
2595     }
2596   if (GET_CODE (operands[1]) == CONST_DOUBLE
2597       && ! FLOAT_MODE_P (mode)
2598       && ((CONST_DOUBLE_HIGH (operands[1]) == 0
2599            && CONST_DOUBLE_LOW (operands[1]) >= 0)
2600           || (CONST_DOUBLE_HIGH (operands[1]) == -1
2601               && CONST_DOUBLE_LOW (operands[1]) < 0)))
2602     abort ();
2603
2604   /* Check if GCC is setting up a block move that will end up using FP
2605      registers as temporaries.  We must make sure this is acceptable.  */
2606   if (GET_CODE (operands[0]) == MEM
2607       && GET_CODE (operands[1]) == MEM
2608       && mode == DImode
2609       && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
2610           || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
2611       && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
2612                                             ? 32 : MEM_ALIGN (operands[0])))
2613             || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
2614                                                ? 32 
2615                                                : MEM_ALIGN (operands[1]))))
2616       && ! MEM_VOLATILE_P (operands [0])
2617       && ! MEM_VOLATILE_P (operands [1]))
2618     {
2619       emit_move_insn (adjust_address (operands[0], SImode, 0),
2620                       adjust_address (operands[1], SImode, 0));
2621       emit_move_insn (adjust_address (operands[0], SImode, 4),
2622                       adjust_address (operands[1], SImode, 4));
2623       return;
2624     }
2625   
2626   if (!no_new_pseudos)
2627     {
2628       if (GET_CODE (operands[1]) == MEM && optimize > 0
2629           && (mode == QImode || mode == HImode || mode == SImode)
2630           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2631         {
2632           rtx reg = gen_reg_rtx (word_mode);
2633
2634           emit_insn (gen_rtx_SET (word_mode, reg,
2635                                   gen_rtx_ZERO_EXTEND (word_mode,
2636                                                        operands[1])));
2637           operands[1] = gen_lowpart (mode, reg);
2638         }
2639       if (GET_CODE (operands[0]) != REG)
2640         operands[1] = force_reg (mode, operands[1]);
2641     }
2642
2643   if (mode == SFmode && ! TARGET_POWERPC
2644       && TARGET_HARD_FLOAT && TARGET_FPRS
2645       && GET_CODE (operands[0]) == MEM)
2646     {
2647       int regnum;
2648
2649       if (reload_in_progress || reload_completed)
2650         regnum = true_regnum (operands[1]);
2651       else if (GET_CODE (operands[1]) == REG)
2652         regnum = REGNO (operands[1]);
2653       else
2654         regnum = -1;
2655       
2656       /* If operands[1] is a register, on POWER it may have
2657          double-precision data in it, so truncate it to single
2658          precision.  */
2659       if (FP_REGNO_P (regnum) || regnum >= FIRST_PSEUDO_REGISTER)
2660         {
2661           rtx newreg;
2662           newreg = (no_new_pseudos ? operands[1] : gen_reg_rtx (mode));
2663           emit_insn (gen_aux_truncdfsf2 (newreg, operands[1]));
2664           operands[1] = newreg;
2665         }
2666     }
2667
2668   /* Handle the case where reload calls us with an invalid address;
2669      and the case of CONSTANT_P_RTX.  */
2670   if (!ALTIVEC_VECTOR_MODE (mode)
2671       && (! general_operand (operands[1], mode)
2672           || ! nonimmediate_operand (operands[0], mode)
2673           || GET_CODE (operands[1]) == CONSTANT_P_RTX))
2674     {
2675       emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2676       return;
2677     }
2678   
2679   /* FIXME:  In the long term, this switch statement should go away
2680      and be replaced by a sequence of tests based on things like
2681      mode == Pmode.  */
2682   switch (mode)
2683     {
2684     case HImode:
2685     case QImode:
2686       if (CONSTANT_P (operands[1])
2687           && GET_CODE (operands[1]) != CONST_INT)
2688         operands[1] = force_const_mem (mode, operands[1]);
2689       break;
2690
2691     case TFmode:
2692     case DFmode:
2693     case SFmode:
2694       if (CONSTANT_P (operands[1]) 
2695           && ! easy_fp_constant (operands[1], mode))
2696         operands[1] = force_const_mem (mode, operands[1]);
2697       break;
2698       
2699     case V16QImode:
2700     case V8HImode:
2701     case V4SFmode:
2702     case V4SImode:
2703     case V4HImode:
2704     case V2SFmode:
2705     case V2SImode:
2706     case V1DImode:
2707       if (CONSTANT_P (operands[1])
2708           && !easy_vector_constant (operands[1]))
2709         operands[1] = force_const_mem (mode, operands[1]);
2710       break;
2711       
2712     case SImode:
2713     case DImode:
2714       /* Use default pattern for address of ELF small data */
2715       if (TARGET_ELF
2716           && mode == Pmode
2717           && DEFAULT_ABI == ABI_V4
2718           && (GET_CODE (operands[1]) == SYMBOL_REF 
2719               || GET_CODE (operands[1]) == CONST)
2720           && small_data_operand (operands[1], mode))
2721         {
2722           emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2723           return;
2724         }
2725
2726       if (DEFAULT_ABI == ABI_V4
2727           && mode == Pmode && mode == SImode
2728           && flag_pic == 1 && got_operand (operands[1], mode))
2729         {
2730           emit_insn (gen_movsi_got (operands[0], operands[1]));
2731           return;
2732         }
2733
2734       if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
2735           && TARGET_NO_TOC && ! flag_pic
2736           && mode == Pmode
2737           && CONSTANT_P (operands[1])
2738           && GET_CODE (operands[1]) != HIGH
2739           && GET_CODE (operands[1]) != CONST_INT)
2740         {
2741           rtx target = (no_new_pseudos ? operands[0] : gen_reg_rtx (mode));
2742
2743           /* If this is a function address on -mcall-aixdesc,
2744              convert it to the address of the descriptor.  */
2745           if (DEFAULT_ABI == ABI_AIX
2746               && GET_CODE (operands[1]) == SYMBOL_REF
2747               && XSTR (operands[1], 0)[0] == '.')
2748             {
2749               const char *name = XSTR (operands[1], 0);
2750               rtx new_ref;
2751               while (*name == '.')
2752                 name++;
2753               new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
2754               CONSTANT_POOL_ADDRESS_P (new_ref)
2755                 = CONSTANT_POOL_ADDRESS_P (operands[1]);
2756               SYMBOL_REF_FLAG (new_ref) = SYMBOL_REF_FLAG (operands[1]);
2757               SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
2758               operands[1] = new_ref;
2759             }
2760
2761           if (DEFAULT_ABI == ABI_DARWIN)
2762             {
2763               emit_insn (gen_macho_high (target, operands[1]));
2764               emit_insn (gen_macho_low (operands[0], target, operands[1]));
2765               return;
2766             }
2767
2768           emit_insn (gen_elf_high (target, operands[1]));
2769           emit_insn (gen_elf_low (operands[0], target, operands[1]));
2770           return;
2771         }
2772
2773       /* If this is a SYMBOL_REF that refers to a constant pool entry,
2774          and we have put it in the TOC, we just need to make a TOC-relative
2775          reference to it.  */
2776       if (TARGET_TOC
2777           && GET_CODE (operands[1]) == SYMBOL_REF
2778           && CONSTANT_POOL_EXPR_P (operands[1])
2779           && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (operands[1]),
2780                                               get_pool_mode (operands[1])))
2781         {
2782           operands[1] = create_TOC_reference (operands[1]);
2783         }
2784       else if (mode == Pmode
2785                && CONSTANT_P (operands[1])
2786                && ((GET_CODE (operands[1]) != CONST_INT
2787                     && ! easy_fp_constant (operands[1], mode))
2788                    || (GET_CODE (operands[1]) == CONST_INT
2789                        && num_insns_constant (operands[1], mode) > 2)
2790                    || (GET_CODE (operands[0]) == REG
2791                        && FP_REGNO_P (REGNO (operands[0]))))
2792                && GET_CODE (operands[1]) != HIGH
2793                && ! LEGITIMATE_CONSTANT_POOL_ADDRESS_P (operands[1])
2794                && ! TOC_RELATIVE_EXPR_P (operands[1]))
2795         {
2796           /* Emit a USE operation so that the constant isn't deleted if
2797              expensive optimizations are turned on because nobody
2798              references it.  This should only be done for operands that
2799              contain SYMBOL_REFs with CONSTANT_POOL_ADDRESS_P set.
2800              This should not be done for operands that contain LABEL_REFs.
2801              For now, we just handle the obvious case.  */
2802           if (GET_CODE (operands[1]) != LABEL_REF)
2803             emit_insn (gen_rtx_USE (VOIDmode, operands[1]));
2804
2805 #if TARGET_MACHO
2806           /* Darwin uses a special PIC legitimizer.  */
2807           if (DEFAULT_ABI == ABI_DARWIN && flag_pic)
2808             {
2809               operands[1] =
2810                 rs6000_machopic_legitimize_pic_address (operands[1], mode,
2811                                                         operands[0]);
2812               if (operands[0] != operands[1])
2813                 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2814               return;
2815             }
2816 #endif
2817
2818           /* If we are to limit the number of things we put in the TOC and
2819              this is a symbol plus a constant we can add in one insn,
2820              just put the symbol in the TOC and add the constant.  Don't do
2821              this if reload is in progress.  */
2822           if (GET_CODE (operands[1]) == CONST
2823               && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
2824               && GET_CODE (XEXP (operands[1], 0)) == PLUS
2825               && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
2826               && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
2827                   || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
2828               && ! side_effects_p (operands[0]))
2829             {
2830               rtx sym =
2831                 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
2832               rtx other = XEXP (XEXP (operands[1], 0), 1);
2833
2834               sym = force_reg (mode, sym);
2835               if (mode == SImode)
2836                 emit_insn (gen_addsi3 (operands[0], sym, other));
2837               else
2838                 emit_insn (gen_adddi3 (operands[0], sym, other));
2839               return;
2840             }
2841
2842           operands[1] = force_const_mem (mode, operands[1]);
2843
2844           if (TARGET_TOC 
2845               && CONSTANT_POOL_EXPR_P (XEXP (operands[1], 0))
2846               && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
2847                         get_pool_constant (XEXP (operands[1], 0)),
2848                         get_pool_mode (XEXP (operands[1], 0))))
2849             {
2850               operands[1]
2851                 = gen_rtx_MEM (mode,
2852                                create_TOC_reference (XEXP (operands[1], 0)));
2853               set_mem_alias_set (operands[1], get_TOC_alias_set ());
2854               RTX_UNCHANGING_P (operands[1]) = 1;
2855             }
2856         }
2857       break;
2858
2859     case TImode:
2860       if (GET_CODE (operands[0]) == MEM
2861           && GET_CODE (XEXP (operands[0], 0)) != REG
2862           && ! reload_in_progress)
2863         operands[0]
2864           = replace_equiv_address (operands[0],
2865                                    copy_addr_to_reg (XEXP (operands[0], 0)));
2866
2867       if (GET_CODE (operands[1]) == MEM
2868           && GET_CODE (XEXP (operands[1], 0)) != REG
2869           && ! reload_in_progress)
2870         operands[1]
2871           = replace_equiv_address (operands[1],
2872                                    copy_addr_to_reg (XEXP (operands[1], 0)));
2873       if (TARGET_POWER)
2874         {
2875           emit_insn (gen_rtx_PARALLEL (VOIDmode,
2876                        gen_rtvec (2,
2877                                   gen_rtx_SET (VOIDmode,
2878                                                operands[0], operands[1]),
2879                                   gen_rtx_CLOBBER (VOIDmode,
2880                                                    gen_rtx_SCRATCH (SImode)))));
2881           return;
2882         }
2883       break;
2884
2885     default:
2886       abort ();
2887     }
2888
2889   /* Above, we may have called force_const_mem which may have returned
2890      an invalid address.  If we can, fix this up; otherwise, reload will
2891      have to deal with it.  */
2892   if (GET_CODE (operands[1]) == MEM
2893       && ! memory_address_p (mode, XEXP (operands[1], 0))
2894       && ! reload_in_progress)
2895     operands[1] = adjust_address (operands[1], mode, 0);
2896
2897   emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
2898   return;
2899 }
2900 \f
2901 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2902    for a call to a function whose data type is FNTYPE.
2903    For a library call, FNTYPE is 0.
2904
2905    For incoming args we set the number of arguments in the prototype large
2906    so we never return a PARALLEL.  */
2907
2908 void
2909 init_cumulative_args (cum, fntype, libname, incoming)
2910      CUMULATIVE_ARGS *cum;
2911      tree fntype;
2912      rtx libname ATTRIBUTE_UNUSED;
2913      int incoming;
2914 {
2915   static CUMULATIVE_ARGS zero_cumulative;
2916
2917   *cum = zero_cumulative;
2918   cum->words = 0;
2919   cum->fregno = FP_ARG_MIN_REG;
2920   cum->vregno = ALTIVEC_ARG_MIN_REG;
2921   cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
2922   cum->call_cookie = CALL_NORMAL;
2923   cum->sysv_gregno = GP_ARG_MIN_REG;
2924
2925   if (incoming)
2926     cum->nargs_prototype = 1000;                /* don't return a PARALLEL */
2927
2928   else if (cum->prototype)
2929     cum->nargs_prototype = (list_length (TYPE_ARG_TYPES (fntype)) - 1
2930                             + (TYPE_MODE (TREE_TYPE (fntype)) == BLKmode
2931                                || RETURN_IN_MEMORY (TREE_TYPE (fntype))));
2932
2933   else
2934     cum->nargs_prototype = 0;
2935
2936   cum->orig_nargs = cum->nargs_prototype;
2937
2938   /* Check for a longcall attribute.  */
2939   if (fntype
2940       && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
2941       && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype)))
2942     cum->call_cookie = CALL_LONG;
2943
2944   if (TARGET_DEBUG_ARG)
2945     {
2946       fprintf (stderr, "\ninit_cumulative_args:");
2947       if (fntype)
2948         {
2949           tree ret_type = TREE_TYPE (fntype);
2950           fprintf (stderr, " ret code = %s,",
2951                    tree_code_name[ (int)TREE_CODE (ret_type) ]);
2952         }
2953
2954       if (cum->call_cookie & CALL_LONG)
2955         fprintf (stderr, " longcall,");
2956
2957       fprintf (stderr, " proto = %d, nargs = %d\n",
2958                cum->prototype, cum->nargs_prototype);
2959     }
2960 }
2961 \f
2962 /* If defined, a C expression which determines whether, and in which
2963    direction, to pad out an argument with extra space.  The value
2964    should be of type `enum direction': either `upward' to pad above
2965    the argument, `downward' to pad below, or `none' to inhibit
2966    padding.
2967
2968    For the AIX ABI structs are always stored left shifted in their
2969    argument slot.  */
2970
2971 enum direction
2972 function_arg_padding (mode, type)
2973      enum machine_mode mode;
2974      tree type;
2975 {
2976   if (type != 0 && AGGREGATE_TYPE_P (type))
2977     return upward;
2978
2979   /* This is the default definition.  */
2980   return (! BYTES_BIG_ENDIAN
2981           ? upward
2982           : ((mode == BLKmode
2983               ? (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
2984                  && int_size_in_bytes (type) < (PARM_BOUNDARY / BITS_PER_UNIT))
2985               : GET_MODE_BITSIZE (mode) < PARM_BOUNDARY)
2986              ? downward : upward));
2987 }
2988
2989 /* If defined, a C expression that gives the alignment boundary, in bits,
2990    of an argument with the specified mode and type.  If it is not defined, 
2991    PARM_BOUNDARY is used for all arguments.
2992    
2993    V.4 wants long longs to be double word aligned.  */
2994
2995 int
2996 function_arg_boundary (mode, type)
2997      enum machine_mode mode;
2998      tree type ATTRIBUTE_UNUSED;
2999 {
3000   if (DEFAULT_ABI == ABI_V4 && (mode == DImode || mode == DFmode))
3001     return 64;
3002    else if (SPE_VECTOR_MODE (mode))
3003      return 64;
3004   else if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
3005     return 128;
3006   else
3007     return PARM_BOUNDARY;
3008 }
3009 \f
3010 /* Update the data in CUM to advance over an argument
3011    of mode MODE and data type TYPE.
3012    (TYPE is null for libcalls where that information may not be available.)  */
3013
3014 void
3015 function_arg_advance (cum, mode, type, named)
3016      CUMULATIVE_ARGS *cum;
3017      enum machine_mode mode;
3018      tree type;
3019      int named;
3020 {
3021   cum->nargs_prototype--;
3022
3023   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
3024     {
3025       if (cum->vregno <= ALTIVEC_ARG_MAX_REG && cum->nargs_prototype >= 0)
3026         cum->vregno++;
3027       else
3028         cum->words += RS6000_ARG_SIZE (mode, type);
3029     }
3030   else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
3031            && named && cum->sysv_gregno <= GP_ARG_MAX_REG)
3032     cum->sysv_gregno++;
3033   else if (DEFAULT_ABI == ABI_V4)
3034     {
3035       if (TARGET_HARD_FLOAT && TARGET_FPRS
3036           && (mode == SFmode || mode == DFmode))
3037         {
3038           if (cum->fregno <= FP_ARG_V4_MAX_REG)
3039             cum->fregno++;
3040           else
3041             {
3042               if (mode == DFmode)
3043                 cum->words += cum->words & 1;
3044               cum->words += RS6000_ARG_SIZE (mode, type);
3045             }
3046         }
3047       else
3048         {
3049           int n_words;
3050           int gregno = cum->sysv_gregno;
3051
3052           /* Aggregates and IEEE quad get passed by reference.  */
3053           if ((type && AGGREGATE_TYPE_P (type))
3054               || mode == TFmode)
3055             n_words = 1;
3056           else 
3057             n_words = RS6000_ARG_SIZE (mode, type);
3058
3059           /* Long long and SPE vectors are put in odd registers.  */
3060           if (n_words == 2 && (gregno & 1) == 0)
3061             gregno += 1;
3062
3063           /* Long long and SPE vectors are not split between registers
3064              and stack.  */
3065           if (gregno + n_words - 1 > GP_ARG_MAX_REG)
3066             {
3067               /* Long long is aligned on the stack.  */
3068               if (n_words == 2)
3069                 cum->words += cum->words & 1;
3070               cum->words += n_words;
3071             }
3072
3073           /* Note: continuing to accumulate gregno past when we've started
3074              spilling to the stack indicates the fact that we've started
3075              spilling to the stack to expand_builtin_saveregs.  */
3076           cum->sysv_gregno = gregno + n_words;
3077         }
3078
3079       if (TARGET_DEBUG_ARG)
3080         {
3081           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
3082                    cum->words, cum->fregno);
3083           fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
3084                    cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
3085           fprintf (stderr, "mode = %4s, named = %d\n",
3086                    GET_MODE_NAME (mode), named);
3087         }
3088     }
3089   else
3090     {
3091       int align = (TARGET_32BIT && (cum->words & 1) != 0
3092                    && function_arg_boundary (mode, type) == 64) ? 1 : 0;
3093
3094       cum->words += align + RS6000_ARG_SIZE (mode, type);
3095
3096       if (GET_MODE_CLASS (mode) == MODE_FLOAT
3097           && TARGET_HARD_FLOAT && TARGET_FPRS)
3098         cum->fregno += (mode == TFmode ? 2 : 1);
3099
3100       if (TARGET_DEBUG_ARG)
3101         {
3102           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
3103                    cum->words, cum->fregno);
3104           fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
3105                    cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
3106           fprintf (stderr, "named = %d, align = %d\n", named, align);
3107         }
3108     }
3109 }
3110 \f
3111 /* Determine where to put an argument to a function.
3112    Value is zero to push the argument on the stack,
3113    or a hard register in which to store the argument.
3114
3115    MODE is the argument's machine mode.
3116    TYPE is the data type of the argument (as a tree).
3117     This is null for libcalls where that information may
3118     not be available.
3119    CUM is a variable of type CUMULATIVE_ARGS which gives info about
3120     the preceding args and about the function being called.
3121    NAMED is nonzero if this argument is a named parameter
3122     (otherwise it is an extra parameter matching an ellipsis).
3123
3124    On RS/6000 the first eight words of non-FP are normally in registers
3125    and the rest are pushed.  Under AIX, the first 13 FP args are in registers.
3126    Under V.4, the first 8 FP args are in registers.
3127
3128    If this is floating-point and no prototype is specified, we use
3129    both an FP and integer register (or possibly FP reg and stack).  Library
3130    functions (when TYPE is zero) always have the proper types for args,
3131    so we can pass the FP value just in one register.  emit_library_function
3132    doesn't support PARALLEL anyway.  */
3133
3134 struct rtx_def *
3135 function_arg (cum, mode, type, named)
3136      CUMULATIVE_ARGS *cum;
3137      enum machine_mode mode;
3138      tree type;
3139      int named;
3140 {
3141   enum rs6000_abi abi = DEFAULT_ABI;
3142
3143   /* Return a marker to indicate whether CR1 needs to set or clear the
3144      bit that V.4 uses to say fp args were passed in registers.
3145      Assume that we don't need the marker for software floating point,
3146      or compiler generated library calls.  */
3147   if (mode == VOIDmode)
3148     {
3149       if (abi == ABI_V4
3150           && cum->nargs_prototype < 0
3151           && type && (cum->prototype || TARGET_NO_PROTOTYPE))
3152         {
3153           /* For the SPE, we need to crxor CR6 always.  */
3154           if (TARGET_SPE_ABI)
3155             return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
3156           else if (TARGET_HARD_FLOAT && TARGET_FPRS)
3157             return GEN_INT (cum->call_cookie
3158                             | ((cum->fregno == FP_ARG_MIN_REG)
3159                                ? CALL_V4_SET_FP_ARGS
3160                                : CALL_V4_CLEAR_FP_ARGS));
3161         }
3162
3163       return GEN_INT (cum->call_cookie);
3164     }
3165
3166   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
3167     {
3168       if (named && cum->vregno <= ALTIVEC_ARG_MAX_REG)
3169         return gen_rtx_REG (mode, cum->vregno);
3170       else
3171         return NULL;
3172     }
3173   else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode) && named)
3174     {
3175       if (cum->sysv_gregno <= GP_ARG_MAX_REG)
3176         return gen_rtx_REG (mode, cum->sysv_gregno);
3177       else
3178         return NULL;
3179     }
3180   else if (abi == ABI_V4)
3181     {
3182       if (TARGET_HARD_FLOAT && TARGET_FPRS
3183           && (mode == SFmode || mode == DFmode))
3184         {
3185           if (cum->fregno <= FP_ARG_V4_MAX_REG)
3186             return gen_rtx_REG (mode, cum->fregno);
3187           else
3188             return NULL;
3189         }
3190       else
3191         {
3192           int n_words;
3193           int gregno = cum->sysv_gregno;
3194
3195           /* Aggregates and IEEE quad get passed by reference.  */
3196           if ((type && AGGREGATE_TYPE_P (type))
3197               || mode == TFmode)
3198             n_words = 1;
3199           else 
3200             n_words = RS6000_ARG_SIZE (mode, type);
3201
3202           /* Long long and SPE vectors are put in odd registers.  */
3203           if (n_words == 2 && (gregno & 1) == 0)
3204             gregno += 1;
3205
3206           /* Long long and SPE vectors are not split between registers
3207              and stack.  */
3208           if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
3209             {
3210               /* SPE vectors in ... get split into 2 registers.  */
3211               if (TARGET_SPE && TARGET_SPE_ABI
3212                   && SPE_VECTOR_MODE (mode) && !named)
3213                 {
3214                   rtx r1, r2;
3215                   enum machine_mode m = SImode;
3216
3217                   r1 = gen_rtx_REG (m, gregno);
3218                   r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
3219                   r2 = gen_rtx_REG (m, gregno + 1);
3220                   r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
3221                   return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
3222                 }
3223               return gen_rtx_REG (mode, gregno);
3224             }
3225           else
3226             return NULL;
3227         }
3228     }
3229   else
3230     {
3231       int align = (TARGET_32BIT && (cum->words & 1) != 0
3232                    && function_arg_boundary (mode, type) == 64) ? 1 : 0;
3233       int align_words = cum->words + align;
3234
3235       if (type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
3236         return NULL_RTX;
3237
3238       if (USE_FP_FOR_ARG_P (*cum, mode, type))
3239         {
3240           if (! type
3241               || ((cum->nargs_prototype > 0)
3242                   /* IBM AIX extended its linkage convention definition always
3243                      to require FP args after register save area hole on the
3244                      stack.  */
3245                   && (DEFAULT_ABI != ABI_AIX
3246                       || ! TARGET_XL_CALL
3247                       || (align_words < GP_ARG_NUM_REG))))
3248             return gen_rtx_REG (mode, cum->fregno);
3249
3250           return gen_rtx_PARALLEL (mode,
3251             gen_rtvec (2,
3252                        gen_rtx_EXPR_LIST (VOIDmode,
3253                                 ((align_words >= GP_ARG_NUM_REG)
3254                                  ? NULL_RTX
3255                                  : (align_words
3256                                     + RS6000_ARG_SIZE (mode, type)
3257                                     > GP_ARG_NUM_REG
3258                                     /* If this is partially on the stack, then
3259                                        we only include the portion actually
3260                                        in registers here.  */
3261                                     ? gen_rtx_REG (SImode,
3262                                                GP_ARG_MIN_REG + align_words)
3263                                     : gen_rtx_REG (mode,
3264                                                GP_ARG_MIN_REG + align_words))),
3265                                 const0_rtx),
3266                        gen_rtx_EXPR_LIST (VOIDmode,
3267                                 gen_rtx_REG (mode, cum->fregno),
3268                                 const0_rtx)));
3269         }
3270       else if (align_words < GP_ARG_NUM_REG)
3271         return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
3272       else
3273         return NULL_RTX;
3274     }
3275 }
3276 \f
3277 /* For an arg passed partly in registers and partly in memory,
3278    this is the number of registers used.
3279    For args passed entirely in registers or entirely in memory, zero.  */
3280
3281 int
3282 function_arg_partial_nregs (cum, mode, type, named)
3283      CUMULATIVE_ARGS *cum;
3284      enum machine_mode mode;
3285      tree type;
3286      int named ATTRIBUTE_UNUSED;
3287 {
3288   if (DEFAULT_ABI == ABI_V4)
3289     return 0;
3290
3291   if (USE_FP_FOR_ARG_P (*cum, mode, type)
3292       || USE_ALTIVEC_FOR_ARG_P (*cum, mode, type))
3293     {
3294       if (cum->nargs_prototype >= 0)
3295         return 0;
3296     }
3297
3298   if (cum->words < GP_ARG_NUM_REG
3299       && GP_ARG_NUM_REG < (cum->words + RS6000_ARG_SIZE (mode, type)))
3300     {
3301       int ret = GP_ARG_NUM_REG - cum->words;
3302       if (ret && TARGET_DEBUG_ARG)
3303         fprintf (stderr, "function_arg_partial_nregs: %d\n", ret);
3304
3305       return ret;
3306     }
3307
3308   return 0;
3309 }
3310 \f
3311 /* A C expression that indicates when an argument must be passed by
3312    reference.  If nonzero for an argument, a copy of that argument is
3313    made in memory and a pointer to the argument is passed instead of
3314    the argument itself.  The pointer is passed in whatever way is
3315    appropriate for passing a pointer to that type.
3316
3317    Under V.4, structures and unions are passed by reference.  */
3318
3319 int
3320 function_arg_pass_by_reference (cum, mode, type, named)
3321      CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED;
3322      enum machine_mode mode ATTRIBUTE_UNUSED;
3323      tree type;
3324      int named ATTRIBUTE_UNUSED;
3325 {
3326   if (DEFAULT_ABI == ABI_V4
3327       && ((type && AGGREGATE_TYPE_P (type))
3328           || mode == TFmode))
3329     {
3330       if (TARGET_DEBUG_ARG)
3331         fprintf (stderr, "function_arg_pass_by_reference: aggregate\n");
3332
3333       return 1;
3334     }
3335
3336   return 0;
3337 }
3338 \f
3339 /* Perform any needed actions needed for a function that is receiving a
3340    variable number of arguments. 
3341
3342    CUM is as above.
3343
3344    MODE and TYPE are the mode and type of the current parameter.
3345
3346    PRETEND_SIZE is a variable that should be set to the amount of stack
3347    that must be pushed by the prolog to pretend that our caller pushed
3348    it.
3349
3350    Normally, this macro will push all remaining incoming registers on the
3351    stack and set PRETEND_SIZE to the length of the registers pushed.  */
3352
3353 void
3354 setup_incoming_varargs (cum, mode, type, pretend_size, no_rtl)
3355      CUMULATIVE_ARGS *cum;
3356      enum machine_mode mode;
3357      tree type;
3358      int *pretend_size ATTRIBUTE_UNUSED;
3359      int no_rtl;
3360
3361 {
3362   CUMULATIVE_ARGS next_cum;
3363   int reg_size = TARGET_32BIT ? 4 : 8;
3364   rtx save_area = NULL_RTX, mem;
3365   int first_reg_offset, set;
3366   tree fntype;
3367   int stdarg_p;
3368
3369   fntype = TREE_TYPE (current_function_decl);
3370   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
3371               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
3372                   != void_type_node));
3373
3374   /* For varargs, we do not want to skip the dummy va_dcl argument.
3375      For stdargs, we do want to skip the last named argument.  */
3376   next_cum = *cum;
3377   if (stdarg_p)
3378     function_arg_advance (&next_cum, mode, type, 1);
3379
3380   if (DEFAULT_ABI == ABI_V4)
3381     {
3382       /* Indicate to allocate space on the stack for varargs save area.  */
3383       cfun->machine->sysv_varargs_p = 1;
3384       if (! no_rtl)
3385         save_area = plus_constant (virtual_stack_vars_rtx,
3386                                    - RS6000_VARARGS_SIZE);
3387
3388       first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
3389     }
3390   else
3391     {
3392       first_reg_offset = next_cum.words;
3393       save_area = virtual_incoming_args_rtx;
3394       cfun->machine->sysv_varargs_p = 0;
3395
3396       if (MUST_PASS_IN_STACK (mode, type))
3397         first_reg_offset += RS6000_ARG_SIZE (TYPE_MODE (type), type);
3398     }
3399
3400   set = get_varargs_alias_set ();
3401   if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG)
3402     {
3403       mem = gen_rtx_MEM (BLKmode,
3404                          plus_constant (save_area,
3405                                         first_reg_offset * reg_size)),
3406       set_mem_alias_set (mem, set);
3407       set_mem_align (mem, BITS_PER_WORD);
3408
3409       move_block_from_reg
3410         (GP_ARG_MIN_REG + first_reg_offset, mem,
3411          GP_ARG_NUM_REG - first_reg_offset,
3412          (GP_ARG_NUM_REG - first_reg_offset) * UNITS_PER_WORD);
3413     }
3414
3415   /* Save FP registers if needed.  */
3416   if (DEFAULT_ABI == ABI_V4
3417       && TARGET_HARD_FLOAT && TARGET_FPRS
3418       && ! no_rtl
3419       && next_cum.fregno <= FP_ARG_V4_MAX_REG)
3420     {
3421       int fregno = next_cum.fregno;
3422       rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
3423       rtx lab = gen_label_rtx ();
3424       int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG) * 8);
3425
3426       emit_jump_insn (gen_rtx_SET (VOIDmode,
3427                                    pc_rtx,
3428                                    gen_rtx_IF_THEN_ELSE (VOIDmode,
3429                                             gen_rtx_NE (VOIDmode, cr1,
3430                                                         const0_rtx),
3431                                             gen_rtx_LABEL_REF (VOIDmode, lab),
3432                                             pc_rtx)));
3433
3434       while (fregno <= FP_ARG_V4_MAX_REG)
3435         {
3436           mem = gen_rtx_MEM (DFmode, plus_constant (save_area, off));
3437           set_mem_alias_set (mem, set);
3438           emit_move_insn (mem, gen_rtx_REG (DFmode, fregno));
3439           fregno++;
3440           off += 8;
3441         }
3442
3443       emit_label (lab);
3444     }
3445 }
3446
3447 /* Create the va_list data type.  */
3448
3449 tree
3450 rs6000_build_va_list ()
3451 {
3452   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
3453
3454   /* For AIX, prefer 'char *' because that's what the system
3455      header files like.  */
3456   if (DEFAULT_ABI != ABI_V4)
3457     return build_pointer_type (char_type_node);
3458
3459   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
3460   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
3461
3462   f_gpr = build_decl (FIELD_DECL, get_identifier ("gpr"), 
3463                       unsigned_char_type_node);
3464   f_fpr = build_decl (FIELD_DECL, get_identifier ("fpr"), 
3465                       unsigned_char_type_node);
3466   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
3467                       ptr_type_node);
3468   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
3469                       ptr_type_node);
3470
3471   DECL_FIELD_CONTEXT (f_gpr) = record;
3472   DECL_FIELD_CONTEXT (f_fpr) = record;
3473   DECL_FIELD_CONTEXT (f_ovf) = record;
3474   DECL_FIELD_CONTEXT (f_sav) = record;
3475
3476   TREE_CHAIN (record) = type_decl;
3477   TYPE_NAME (record) = type_decl;
3478   TYPE_FIELDS (record) = f_gpr;
3479   TREE_CHAIN (f_gpr) = f_fpr;
3480   TREE_CHAIN (f_fpr) = f_ovf;
3481   TREE_CHAIN (f_ovf) = f_sav;
3482
3483   layout_type (record);
3484
3485   /* The correct type is an array type of one element.  */
3486   return build_array_type (record, build_index_type (size_zero_node));
3487 }
3488
3489 /* Implement va_start.  */
3490
3491 void
3492 rs6000_va_start (valist, nextarg)
3493      tree valist;
3494      rtx nextarg;
3495 {
3496   HOST_WIDE_INT words, n_gpr, n_fpr;
3497   tree f_gpr, f_fpr, f_ovf, f_sav;
3498   tree gpr, fpr, ovf, sav, t;
3499
3500   /* Only SVR4 needs something special.  */
3501   if (DEFAULT_ABI != ABI_V4)
3502     {
3503       std_expand_builtin_va_start (valist, nextarg);
3504       return;
3505     }
3506
3507   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
3508   f_fpr = TREE_CHAIN (f_gpr);
3509   f_ovf = TREE_CHAIN (f_fpr);
3510   f_sav = TREE_CHAIN (f_ovf);
3511
3512   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
3513   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
3514   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
3515   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
3516   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
3517
3518   /* Count number of gp and fp argument registers used.  */
3519   words = current_function_args_info.words;
3520   n_gpr = current_function_args_info.sysv_gregno - GP_ARG_MIN_REG;
3521   n_fpr = current_function_args_info.fregno - FP_ARG_MIN_REG;
3522
3523   if (TARGET_DEBUG_ARG)
3524     {
3525       fputs ("va_start: words = ", stderr);
3526       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, words);
3527       fputs (", n_gpr = ", stderr);
3528       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, n_gpr);
3529       fputs (", n_fpr = ", stderr);
3530       fprintf (stderr, HOST_WIDE_INT_PRINT_DEC, n_fpr);
3531       putc ('\n', stderr);
3532     }
3533
3534   t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, build_int_2 (n_gpr, 0));
3535   TREE_SIDE_EFFECTS (t) = 1;
3536   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3537
3538   t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr, build_int_2 (n_fpr, 0));
3539   TREE_SIDE_EFFECTS (t) = 1;
3540   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3541
3542   /* Find the overflow area.  */
3543   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
3544   if (words != 0)
3545     t = build (PLUS_EXPR, TREE_TYPE (ovf), t,
3546                build_int_2 (words * UNITS_PER_WORD, 0));
3547   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
3548   TREE_SIDE_EFFECTS (t) = 1;
3549   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3550
3551   /* Find the register save area.  */
3552   t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
3553   t = build (PLUS_EXPR, TREE_TYPE (sav), t,
3554              build_int_2 (-RS6000_VARARGS_SIZE, -1));
3555   t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
3556   TREE_SIDE_EFFECTS (t) = 1;
3557   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3558 }
3559
3560 /* Implement va_arg.  */
3561
3562 rtx
3563 rs6000_va_arg (valist, type)
3564      tree valist, type;
3565 {
3566   tree f_gpr, f_fpr, f_ovf, f_sav;
3567   tree gpr, fpr, ovf, sav, reg, t, u;
3568   int indirect_p, size, rsize, n_reg, sav_ofs, sav_scale;
3569   rtx lab_false, lab_over, addr_rtx, r;
3570
3571   if (DEFAULT_ABI != ABI_V4)
3572     return std_expand_builtin_va_arg (valist, type);
3573
3574   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
3575   f_fpr = TREE_CHAIN (f_gpr);
3576   f_ovf = TREE_CHAIN (f_fpr);
3577   f_sav = TREE_CHAIN (f_ovf);
3578
3579   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
3580   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
3581   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
3582   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
3583   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
3584
3585   size = int_size_in_bytes (type);
3586   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3587
3588   if (AGGREGATE_TYPE_P (type) || TYPE_MODE (type) == TFmode)
3589     {
3590       /* Aggregates and long doubles are passed by reference.  */
3591       indirect_p = 1;
3592       reg = gpr;
3593       n_reg = 1;
3594       sav_ofs = 0;
3595       sav_scale = 4;
3596       size = UNITS_PER_WORD;
3597       rsize = 1;
3598     }
3599   else if (FLOAT_TYPE_P (type) && TARGET_HARD_FLOAT && TARGET_FPRS)
3600     {
3601       /* FP args go in FP registers, if present.  */
3602       indirect_p = 0;
3603       reg = fpr;
3604       n_reg = 1;
3605       sav_ofs = 8*4;
3606       sav_scale = 8;
3607     }
3608   else
3609     {
3610       /* Otherwise into GP registers.  */
3611       indirect_p = 0;
3612       reg = gpr;
3613       n_reg = rsize;
3614       sav_ofs = 0;
3615       sav_scale = 4;
3616     }
3617
3618   /* Pull the value out of the saved registers ...  */
3619
3620   lab_false = gen_label_rtx ();
3621   lab_over = gen_label_rtx ();
3622   addr_rtx = gen_reg_rtx (Pmode);
3623
3624   /*  AltiVec vectors never go in registers.  */
3625   if (!TARGET_ALTIVEC || TREE_CODE (type) != VECTOR_TYPE)
3626     {
3627       TREE_THIS_VOLATILE (reg) = 1;
3628       emit_cmp_and_jump_insns
3629         (expand_expr (reg, NULL_RTX, QImode, EXPAND_NORMAL),
3630          GEN_INT (8 - n_reg + 1), GE, const1_rtx, QImode, 1,
3631          lab_false);
3632
3633       /* Long long is aligned in the registers.  */
3634       if (n_reg > 1)
3635         {
3636           u = build (BIT_AND_EXPR, TREE_TYPE (reg), reg,
3637                      build_int_2 (n_reg - 1, 0));
3638           u = build (PLUS_EXPR, TREE_TYPE (reg), reg, u);
3639           u = build (MODIFY_EXPR, TREE_TYPE (reg), reg, u);
3640           TREE_SIDE_EFFECTS (u) = 1;
3641           expand_expr (u, const0_rtx, VOIDmode, EXPAND_NORMAL);
3642         }
3643
3644       if (sav_ofs)
3645         t = build (PLUS_EXPR, ptr_type_node, sav, build_int_2 (sav_ofs, 0));
3646       else
3647         t = sav;
3648
3649       u = build (POSTINCREMENT_EXPR, TREE_TYPE (reg), reg,
3650                  build_int_2 (n_reg, 0));
3651       TREE_SIDE_EFFECTS (u) = 1;
3652
3653       u = build1 (CONVERT_EXPR, integer_type_node, u);
3654       TREE_SIDE_EFFECTS (u) = 1;
3655
3656       u = build (MULT_EXPR, integer_type_node, u, build_int_2 (sav_scale, 0));
3657       TREE_SIDE_EFFECTS (u) = 1;
3658
3659       t = build (PLUS_EXPR, ptr_type_node, t, u);
3660       TREE_SIDE_EFFECTS (t) = 1;
3661
3662       r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
3663       if (r != addr_rtx)
3664         emit_move_insn (addr_rtx, r);
3665
3666       emit_jump_insn (gen_jump (lab_over));
3667       emit_barrier ();
3668     }
3669
3670   emit_label (lab_false);
3671
3672   /* ... otherwise out of the overflow area.  */
3673
3674   /* Make sure we don't find reg 7 for the next int arg.
3675
3676      All AltiVec vectors go in the overflow area.  So in the AltiVec
3677      case we need to get the vectors from the overflow area, but
3678      remember where the GPRs and FPRs are.  */
3679   if (n_reg > 1 && (TREE_CODE (type) != VECTOR_TYPE
3680                     || !TARGET_ALTIVEC))
3681     {
3682       t = build (MODIFY_EXPR, TREE_TYPE (reg), reg, build_int_2 (8, 0));
3683       TREE_SIDE_EFFECTS (t) = 1;
3684       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3685     }
3686
3687   /* Care for on-stack alignment if needed.  */
3688   if (rsize <= 1)
3689     t = ovf;
3690   else
3691     {
3692       int align;
3693
3694       /* AltiVec vectors are 16 byte aligned.  */
3695       if (TARGET_ALTIVEC && TREE_CODE (type) == VECTOR_TYPE)
3696         align = 15;
3697       else
3698         align = 7;
3699
3700       t = build (PLUS_EXPR, TREE_TYPE (ovf), ovf, build_int_2 (align, 0));
3701       t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-align-1, -1));
3702     }
3703   t = save_expr (t);
3704
3705   r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
3706   if (r != addr_rtx)
3707     emit_move_insn (addr_rtx, r);
3708
3709   t = build (PLUS_EXPR, TREE_TYPE (t), t, build_int_2 (size, 0));
3710   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
3711   TREE_SIDE_EFFECTS (t) = 1;
3712   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3713
3714   emit_label (lab_over);
3715
3716   if (indirect_p)
3717     {
3718       r = gen_rtx_MEM (Pmode, addr_rtx);
3719       set_mem_alias_set (r, get_varargs_alias_set ());
3720       emit_move_insn (addr_rtx, r);
3721     }
3722
3723   return addr_rtx;
3724 }
3725
3726 /* Builtins.  */
3727
3728 #define def_builtin(MASK, NAME, TYPE, CODE)                     \
3729 do {                                                            \
3730   if ((MASK) & target_flags)                                    \
3731     builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD,      \
3732                       NULL, NULL_TREE);                         \
3733 } while (0)
3734
3735 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc).  */
3736
3737 static const struct builtin_description bdesc_3arg[] =
3738 {
3739   { MASK_ALTIVEC, CODE_FOR_altivec_vmaddfp, "__builtin_altivec_vmaddfp", ALTIVEC_BUILTIN_VMADDFP },
3740   { MASK_ALTIVEC, CODE_FOR_altivec_vmhaddshs, "__builtin_altivec_vmhaddshs", ALTIVEC_BUILTIN_VMHADDSHS },
3741   { MASK_ALTIVEC, CODE_FOR_altivec_vmhraddshs, "__builtin_altivec_vmhraddshs", ALTIVEC_BUILTIN_VMHRADDSHS },
3742   { MASK_ALTIVEC, CODE_FOR_altivec_vmladduhm, "__builtin_altivec_vmladduhm", ALTIVEC_BUILTIN_VMLADDUHM},
3743   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumubm, "__builtin_altivec_vmsumubm", ALTIVEC_BUILTIN_VMSUMUBM },
3744   { MASK_ALTIVEC, CODE_FOR_altivec_vmsummbm, "__builtin_altivec_vmsummbm", ALTIVEC_BUILTIN_VMSUMMBM },
3745   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhm, "__builtin_altivec_vmsumuhm", ALTIVEC_BUILTIN_VMSUMUHM },
3746   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshm, "__builtin_altivec_vmsumshm", ALTIVEC_BUILTIN_VMSUMSHM },
3747   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhs, "__builtin_altivec_vmsumuhs", ALTIVEC_BUILTIN_VMSUMUHS },
3748   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshs, "__builtin_altivec_vmsumshs", ALTIVEC_BUILTIN_VMSUMSHS },
3749   { MASK_ALTIVEC, CODE_FOR_altivec_vnmsubfp, "__builtin_altivec_vnmsubfp", ALTIVEC_BUILTIN_VNMSUBFP }, 
3750   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4sf, "__builtin_altivec_vperm_4sf", ALTIVEC_BUILTIN_VPERM_4SF },
3751   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4si, "__builtin_altivec_vperm_4si", ALTIVEC_BUILTIN_VPERM_4SI },
3752   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_8hi, "__builtin_altivec_vperm_8hi", ALTIVEC_BUILTIN_VPERM_8HI },
3753   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_16qi, "__builtin_altivec_vperm_16qi", ALTIVEC_BUILTIN_VPERM_16QI },
3754   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4sf, "__builtin_altivec_vsel_4sf", ALTIVEC_BUILTIN_VSEL_4SF },
3755   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4si, "__builtin_altivec_vsel_4si", ALTIVEC_BUILTIN_VSEL_4SI },
3756   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_8hi, "__builtin_altivec_vsel_8hi", ALTIVEC_BUILTIN_VSEL_8HI },
3757   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_16qi, "__builtin_altivec_vsel_16qi", ALTIVEC_BUILTIN_VSEL_16QI },
3758   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_16qi, "__builtin_altivec_vsldoi_16qi", ALTIVEC_BUILTIN_VSLDOI_16QI },
3759   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_8hi, "__builtin_altivec_vsldoi_8hi", ALTIVEC_BUILTIN_VSLDOI_8HI },
3760   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4si, "__builtin_altivec_vsldoi_4si", ALTIVEC_BUILTIN_VSLDOI_4SI },
3761   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4sf, "__builtin_altivec_vsldoi_4sf", ALTIVEC_BUILTIN_VSLDOI_4SF },
3762 };
3763
3764 /* DST operations: void foo (void *, const int, const char).  */
3765
3766 static const struct builtin_description bdesc_dst[] =
3767 {
3768   { MASK_ALTIVEC, CODE_FOR_altivec_dst, "__builtin_altivec_dst", ALTIVEC_BUILTIN_DST },
3769   { MASK_ALTIVEC, CODE_FOR_altivec_dstt, "__builtin_altivec_dstt", ALTIVEC_BUILTIN_DSTT },
3770   { MASK_ALTIVEC, CODE_FOR_altivec_dstst, "__builtin_altivec_dstst", ALTIVEC_BUILTIN_DSTST },
3771   { MASK_ALTIVEC, CODE_FOR_altivec_dststt, "__builtin_altivec_dststt", ALTIVEC_BUILTIN_DSTSTT }
3772 };
3773
3774 /* Simple binary operations: VECc = foo (VECa, VECb).  */
3775
3776 static struct builtin_description bdesc_2arg[] =
3777 {
3778   { MASK_ALTIVEC, CODE_FOR_addv16qi3, "__builtin_altivec_vaddubm", ALTIVEC_BUILTIN_VADDUBM },
3779   { MASK_ALTIVEC, CODE_FOR_addv8hi3, "__builtin_altivec_vadduhm", ALTIVEC_BUILTIN_VADDUHM },
3780   { MASK_ALTIVEC, CODE_FOR_addv4si3, "__builtin_altivec_vadduwm", ALTIVEC_BUILTIN_VADDUWM },
3781   { MASK_ALTIVEC, CODE_FOR_addv4sf3, "__builtin_altivec_vaddfp", ALTIVEC_BUILTIN_VADDFP },
3782   { MASK_ALTIVEC, CODE_FOR_altivec_vaddcuw, "__builtin_altivec_vaddcuw", ALTIVEC_BUILTIN_VADDCUW },
3783   { MASK_ALTIVEC, CODE_FOR_altivec_vaddubs, "__builtin_altivec_vaddubs", ALTIVEC_BUILTIN_VADDUBS },
3784   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsbs, "__builtin_altivec_vaddsbs", ALTIVEC_BUILTIN_VADDSBS },
3785   { MASK_ALTIVEC, CODE_FOR_altivec_vadduhs, "__builtin_altivec_vadduhs", ALTIVEC_BUILTIN_VADDUHS },
3786   { MASK_ALTIVEC, CODE_FOR_altivec_vaddshs, "__builtin_altivec_vaddshs", ALTIVEC_BUILTIN_VADDSHS },
3787   { MASK_ALTIVEC, CODE_FOR_altivec_vadduws, "__builtin_altivec_vadduws", ALTIVEC_BUILTIN_VADDUWS },
3788   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsws, "__builtin_altivec_vaddsws", ALTIVEC_BUILTIN_VADDSWS },
3789   { MASK_ALTIVEC, CODE_FOR_andv4si3, "__builtin_altivec_vand", ALTIVEC_BUILTIN_VAND },
3790   { MASK_ALTIVEC, CODE_FOR_altivec_vandc, "__builtin_altivec_vandc", ALTIVEC_BUILTIN_VANDC },
3791   { MASK_ALTIVEC, CODE_FOR_altivec_vavgub, "__builtin_altivec_vavgub", ALTIVEC_BUILTIN_VAVGUB },
3792   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsb, "__builtin_altivec_vavgsb", ALTIVEC_BUILTIN_VAVGSB },
3793   { MASK_ALTIVEC, CODE_FOR_altivec_vavguh, "__builtin_altivec_vavguh", ALTIVEC_BUILTIN_VAVGUH },
3794   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsh, "__builtin_altivec_vavgsh", ALTIVEC_BUILTIN_VAVGSH },
3795   { MASK_ALTIVEC, CODE_FOR_altivec_vavguw, "__builtin_altivec_vavguw", ALTIVEC_BUILTIN_VAVGUW },
3796   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsw, "__builtin_altivec_vavgsw", ALTIVEC_BUILTIN_VAVGSW },
3797   { MASK_ALTIVEC, CODE_FOR_altivec_vcfux, "__builtin_altivec_vcfux", ALTIVEC_BUILTIN_VCFUX },
3798   { MASK_ALTIVEC, CODE_FOR_altivec_vcfsx, "__builtin_altivec_vcfsx", ALTIVEC_BUILTIN_VCFSX },
3799   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpbfp, "__builtin_altivec_vcmpbfp", ALTIVEC_BUILTIN_VCMPBFP },
3800   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequb, "__builtin_altivec_vcmpequb", ALTIVEC_BUILTIN_VCMPEQUB },
3801   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequh, "__builtin_altivec_vcmpequh", ALTIVEC_BUILTIN_VCMPEQUH },
3802   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequw, "__builtin_altivec_vcmpequw", ALTIVEC_BUILTIN_VCMPEQUW },
3803   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpeqfp, "__builtin_altivec_vcmpeqfp", ALTIVEC_BUILTIN_VCMPEQFP },
3804   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgefp, "__builtin_altivec_vcmpgefp", ALTIVEC_BUILTIN_VCMPGEFP },
3805   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtub, "__builtin_altivec_vcmpgtub", ALTIVEC_BUILTIN_VCMPGTUB },
3806   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsb, "__builtin_altivec_vcmpgtsb", ALTIVEC_BUILTIN_VCMPGTSB },
3807   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuh, "__builtin_altivec_vcmpgtuh", ALTIVEC_BUILTIN_VCMPGTUH },
3808   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsh, "__builtin_altivec_vcmpgtsh", ALTIVEC_BUILTIN_VCMPGTSH },
3809   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuw, "__builtin_altivec_vcmpgtuw", ALTIVEC_BUILTIN_VCMPGTUW },
3810   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsw, "__builtin_altivec_vcmpgtsw", ALTIVEC_BUILTIN_VCMPGTSW },
3811   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtfp, "__builtin_altivec_vcmpgtfp", ALTIVEC_BUILTIN_VCMPGTFP },
3812   { MASK_ALTIVEC, CODE_FOR_altivec_vctsxs, "__builtin_altivec_vctsxs", ALTIVEC_BUILTIN_VCTSXS },
3813   { MASK_ALTIVEC, CODE_FOR_altivec_vctuxs, "__builtin_altivec_vctuxs", ALTIVEC_BUILTIN_VCTUXS },
3814   { MASK_ALTIVEC, CODE_FOR_umaxv16qi3, "__builtin_altivec_vmaxub", ALTIVEC_BUILTIN_VMAXUB },
3815   { MASK_ALTIVEC, CODE_FOR_smaxv16qi3, "__builtin_altivec_vmaxsb", ALTIVEC_BUILTIN_VMAXSB },
3816   { MASK_ALTIVEC, CODE_FOR_umaxv8hi3, "__builtin_altivec_vmaxuh", ALTIVEC_BUILTIN_VMAXUH },
3817   { MASK_ALTIVEC, CODE_FOR_smaxv8hi3, "__builtin_altivec_vmaxsh", ALTIVEC_BUILTIN_VMAXSH },
3818   { MASK_ALTIVEC, CODE_FOR_umaxv4si3, "__builtin_altivec_vmaxuw", ALTIVEC_BUILTIN_VMAXUW },
3819   { MASK_ALTIVEC, CODE_FOR_smaxv4si3, "__builtin_altivec_vmaxsw", ALTIVEC_BUILTIN_VMAXSW },
3820   { MASK_ALTIVEC, CODE_FOR_smaxv4sf3, "__builtin_altivec_vmaxfp", ALTIVEC_BUILTIN_VMAXFP },
3821   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghb, "__builtin_altivec_vmrghb", ALTIVEC_BUILTIN_VMRGHB },
3822   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghh, "__builtin_altivec_vmrghh", ALTIVEC_BUILTIN_VMRGHH },
3823   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghw, "__builtin_altivec_vmrghw", ALTIVEC_BUILTIN_VMRGHW },
3824   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglb, "__builtin_altivec_vmrglb", ALTIVEC_BUILTIN_VMRGLB },
3825   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglh, "__builtin_altivec_vmrglh", ALTIVEC_BUILTIN_VMRGLH },
3826   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglw, "__builtin_altivec_vmrglw", ALTIVEC_BUILTIN_VMRGLW },
3827   { MASK_ALTIVEC, CODE_FOR_uminv16qi3, "__builtin_altivec_vminub", ALTIVEC_BUILTIN_VMINUB },
3828   { MASK_ALTIVEC, CODE_FOR_sminv16qi3, "__builtin_altivec_vminsb", ALTIVEC_BUILTIN_VMINSB },
3829   { MASK_ALTIVEC, CODE_FOR_uminv8hi3, "__builtin_altivec_vminuh", ALTIVEC_BUILTIN_VMINUH },
3830   { MASK_ALTIVEC, CODE_FOR_sminv8hi3, "__builtin_altivec_vminsh", ALTIVEC_BUILTIN_VMINSH },
3831   { MASK_ALTIVEC, CODE_FOR_uminv4si3, "__builtin_altivec_vminuw", ALTIVEC_BUILTIN_VMINUW },
3832   { MASK_ALTIVEC, CODE_FOR_sminv4si3, "__builtin_altivec_vminsw", ALTIVEC_BUILTIN_VMINSW },
3833   { MASK_ALTIVEC, CODE_FOR_sminv4sf3, "__builtin_altivec_vminfp", ALTIVEC_BUILTIN_VMINFP },
3834   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleub, "__builtin_altivec_vmuleub", ALTIVEC_BUILTIN_VMULEUB },
3835   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesb, "__builtin_altivec_vmulesb", ALTIVEC_BUILTIN_VMULESB },
3836   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleuh, "__builtin_altivec_vmuleuh", ALTIVEC_BUILTIN_VMULEUH },
3837   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesh, "__builtin_altivec_vmulesh", ALTIVEC_BUILTIN_VMULESH },
3838   { MASK_ALTIVEC, CODE_FOR_altivec_vmuloub, "__builtin_altivec_vmuloub", ALTIVEC_BUILTIN_VMULOUB },
3839   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosb, "__builtin_altivec_vmulosb", ALTIVEC_BUILTIN_VMULOSB },
3840   { MASK_ALTIVEC, CODE_FOR_altivec_vmulouh, "__builtin_altivec_vmulouh", ALTIVEC_BUILTIN_VMULOUH },
3841   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosh, "__builtin_altivec_vmulosh", ALTIVEC_BUILTIN_VMULOSH },
3842   { MASK_ALTIVEC, CODE_FOR_altivec_vnor, "__builtin_altivec_vnor", ALTIVEC_BUILTIN_VNOR },
3843   { MASK_ALTIVEC, CODE_FOR_iorv4si3, "__builtin_altivec_vor", ALTIVEC_BUILTIN_VOR },
3844   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum, "__builtin_altivec_vpkuhum", ALTIVEC_BUILTIN_VPKUHUM },
3845   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum, "__builtin_altivec_vpkuwum", ALTIVEC_BUILTIN_VPKUWUM },
3846   { MASK_ALTIVEC, CODE_FOR_altivec_vpkpx, "__builtin_altivec_vpkpx", ALTIVEC_BUILTIN_VPKPX },
3847   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhss, "__builtin_altivec_vpkuhss", ALTIVEC_BUILTIN_VPKUHSS },
3848   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshss, "__builtin_altivec_vpkshss", ALTIVEC_BUILTIN_VPKSHSS },
3849   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwss, "__builtin_altivec_vpkuwss", ALTIVEC_BUILTIN_VPKUWSS },
3850   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswss, "__builtin_altivec_vpkswss", ALTIVEC_BUILTIN_VPKSWSS },
3851   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhus, "__builtin_altivec_vpkuhus", ALTIVEC_BUILTIN_VPKUHUS },
3852   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshus, "__builtin_altivec_vpkshus", ALTIVEC_BUILTIN_VPKSHUS },
3853   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwus, "__builtin_altivec_vpkuwus", ALTIVEC_BUILTIN_VPKUWUS },
3854   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswus, "__builtin_altivec_vpkswus", ALTIVEC_BUILTIN_VPKSWUS },
3855   { MASK_ALTIVEC, CODE_FOR_altivec_vrlb, "__builtin_altivec_vrlb", ALTIVEC_BUILTIN_VRLB },
3856   { MASK_ALTIVEC, CODE_FOR_altivec_vrlh, "__builtin_altivec_vrlh", ALTIVEC_BUILTIN_VRLH },
3857   { MASK_ALTIVEC, CODE_FOR_altivec_vrlw, "__builtin_altivec_vrlw", ALTIVEC_BUILTIN_VRLW },
3858   { MASK_ALTIVEC, CODE_FOR_altivec_vslb, "__builtin_altivec_vslb", ALTIVEC_BUILTIN_VSLB },
3859   { MASK_ALTIVEC, CODE_FOR_altivec_vslh, "__builtin_altivec_vslh", ALTIVEC_BUILTIN_VSLH },
3860   { MASK_ALTIVEC, CODE_FOR_altivec_vslw, "__builtin_altivec_vslw", ALTIVEC_BUILTIN_VSLW },
3861   { MASK_ALTIVEC, CODE_FOR_altivec_vsl, "__builtin_altivec_vsl", ALTIVEC_BUILTIN_VSL },
3862   { MASK_ALTIVEC, CODE_FOR_altivec_vslo, "__builtin_altivec_vslo", ALTIVEC_BUILTIN_VSLO },
3863   { MASK_ALTIVEC, CODE_FOR_altivec_vspltb, "__builtin_altivec_vspltb", ALTIVEC_BUILTIN_VSPLTB },
3864   { MASK_ALTIVEC, CODE_FOR_altivec_vsplth, "__builtin_altivec_vsplth", ALTIVEC_BUILTIN_VSPLTH },
3865   { MASK_ALTIVEC, CODE_FOR_altivec_vspltw, "__builtin_altivec_vspltw", ALTIVEC_BUILTIN_VSPLTW },
3866   { MASK_ALTIVEC, CODE_FOR_altivec_vsrb, "__builtin_altivec_vsrb", ALTIVEC_BUILTIN_VSRB },
3867   { MASK_ALTIVEC, CODE_FOR_altivec_vsrh, "__builtin_altivec_vsrh", ALTIVEC_BUILTIN_VSRH },
3868   { MASK_ALTIVEC, CODE_FOR_altivec_vsrw, "__builtin_altivec_vsrw", ALTIVEC_BUILTIN_VSRW },
3869   { MASK_ALTIVEC, CODE_FOR_altivec_vsrab, "__builtin_altivec_vsrab", ALTIVEC_BUILTIN_VSRAB },
3870   { MASK_ALTIVEC, CODE_FOR_altivec_vsrah, "__builtin_altivec_vsrah", ALTIVEC_BUILTIN_VSRAH },
3871   { MASK_ALTIVEC, CODE_FOR_altivec_vsraw, "__builtin_altivec_vsraw", ALTIVEC_BUILTIN_VSRAW },
3872   { MASK_ALTIVEC, CODE_FOR_altivec_vsr, "__builtin_altivec_vsr", ALTIVEC_BUILTIN_VSR },
3873   { MASK_ALTIVEC, CODE_FOR_altivec_vsro, "__builtin_altivec_vsro", ALTIVEC_BUILTIN_VSRO },
3874   { MASK_ALTIVEC, CODE_FOR_subv16qi3, "__builtin_altivec_vsububm", ALTIVEC_BUILTIN_VSUBUBM },
3875   { MASK_ALTIVEC, CODE_FOR_subv8hi3, "__builtin_altivec_vsubuhm", ALTIVEC_BUILTIN_VSUBUHM },
3876   { MASK_ALTIVEC, CODE_FOR_subv4si3, "__builtin_altivec_vsubuwm", ALTIVEC_BUILTIN_VSUBUWM },
3877   { MASK_ALTIVEC, CODE_FOR_subv4sf3, "__builtin_altivec_vsubfp", ALTIVEC_BUILTIN_VSUBFP },
3878   { MASK_ALTIVEC, CODE_FOR_altivec_vsubcuw, "__builtin_altivec_vsubcuw", ALTIVEC_BUILTIN_VSUBCUW },
3879   { MASK_ALTIVEC, CODE_FOR_altivec_vsububs, "__builtin_altivec_vsububs", ALTIVEC_BUILTIN_VSUBUBS },
3880   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsbs, "__builtin_altivec_vsubsbs", ALTIVEC_BUILTIN_VSUBSBS },
3881   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuhs, "__builtin_altivec_vsubuhs", ALTIVEC_BUILTIN_VSUBUHS },
3882   { MASK_ALTIVEC, CODE_FOR_altivec_vsubshs, "__builtin_altivec_vsubshs", ALTIVEC_BUILTIN_VSUBSHS },
3883   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuws, "__builtin_altivec_vsubuws", ALTIVEC_BUILTIN_VSUBUWS },
3884   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsws, "__builtin_altivec_vsubsws", ALTIVEC_BUILTIN_VSUBSWS },
3885   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4ubs, "__builtin_altivec_vsum4ubs", ALTIVEC_BUILTIN_VSUM4UBS },
3886   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4sbs, "__builtin_altivec_vsum4sbs", ALTIVEC_BUILTIN_VSUM4SBS },
3887   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4shs, "__builtin_altivec_vsum4shs", ALTIVEC_BUILTIN_VSUM4SHS },
3888   { MASK_ALTIVEC, CODE_FOR_altivec_vsum2sws, "__builtin_altivec_vsum2sws", ALTIVEC_BUILTIN_VSUM2SWS },
3889   { MASK_ALTIVEC, CODE_FOR_altivec_vsumsws, "__builtin_altivec_vsumsws", ALTIVEC_BUILTIN_VSUMSWS },
3890   { MASK_ALTIVEC, CODE_FOR_xorv4si3, "__builtin_altivec_vxor", ALTIVEC_BUILTIN_VXOR },
3891
3892   /* Place holder, leave as first spe builtin.  */
3893   { 0, CODE_FOR_spe_evaddw, "__builtin_spe_evaddw", SPE_BUILTIN_EVADDW },
3894   { 0, CODE_FOR_spe_evand, "__builtin_spe_evand", SPE_BUILTIN_EVAND },
3895   { 0, CODE_FOR_spe_evandc, "__builtin_spe_evandc", SPE_BUILTIN_EVANDC },
3896   { 0, CODE_FOR_spe_evdivws, "__builtin_spe_evdivws", SPE_BUILTIN_EVDIVWS },
3897   { 0, CODE_FOR_spe_evdivwu, "__builtin_spe_evdivwu", SPE_BUILTIN_EVDIVWU },
3898   { 0, CODE_FOR_spe_eveqv, "__builtin_spe_eveqv", SPE_BUILTIN_EVEQV },
3899   { 0, CODE_FOR_spe_evfsadd, "__builtin_spe_evfsadd", SPE_BUILTIN_EVFSADD },
3900   { 0, CODE_FOR_spe_evfsdiv, "__builtin_spe_evfsdiv", SPE_BUILTIN_EVFSDIV },
3901   { 0, CODE_FOR_spe_evfsmul, "__builtin_spe_evfsmul", SPE_BUILTIN_EVFSMUL },
3902   { 0, CODE_FOR_spe_evfssub, "__builtin_spe_evfssub", SPE_BUILTIN_EVFSSUB },
3903   { 0, CODE_FOR_spe_evmergehi, "__builtin_spe_evmergehi", SPE_BUILTIN_EVMERGEHI },
3904   { 0, CODE_FOR_spe_evmergehilo, "__builtin_spe_evmergehilo", SPE_BUILTIN_EVMERGEHILO },
3905   { 0, CODE_FOR_spe_evmergelo, "__builtin_spe_evmergelo", SPE_BUILTIN_EVMERGELO },
3906   { 0, CODE_FOR_spe_evmergelohi, "__builtin_spe_evmergelohi", SPE_BUILTIN_EVMERGELOHI },
3907   { 0, CODE_FOR_spe_evmhegsmfaa, "__builtin_spe_evmhegsmfaa", SPE_BUILTIN_EVMHEGSMFAA },
3908   { 0, CODE_FOR_spe_evmhegsmfan, "__builtin_spe_evmhegsmfan", SPE_BUILTIN_EVMHEGSMFAN },
3909   { 0, CODE_FOR_spe_evmhegsmiaa, "__builtin_spe_evmhegsmiaa", SPE_BUILTIN_EVMHEGSMIAA },
3910   { 0, CODE_FOR_spe_evmhegsmian, "__builtin_spe_evmhegsmian", SPE_BUILTIN_EVMHEGSMIAN },
3911   { 0, CODE_FOR_spe_evmhegumiaa, "__builtin_spe_evmhegumiaa", SPE_BUILTIN_EVMHEGUMIAA },
3912   { 0, CODE_FOR_spe_evmhegumian, "__builtin_spe_evmhegumian", SPE_BUILTIN_EVMHEGUMIAN },
3913   { 0, CODE_FOR_spe_evmhesmf, "__builtin_spe_evmhesmf", SPE_BUILTIN_EVMHESMF },
3914   { 0, CODE_FOR_spe_evmhesmfa, "__builtin_spe_evmhesmfa", SPE_BUILTIN_EVMHESMFA },
3915   { 0, CODE_FOR_spe_evmhesmfaaw, "__builtin_spe_evmhesmfaaw", SPE_BUILTIN_EVMHESMFAAW },
3916   { 0, CODE_FOR_spe_evmhesmfanw, "__builtin_spe_evmhesmfanw", SPE_BUILTIN_EVMHESMFANW },
3917   { 0, CODE_FOR_spe_evmhesmi, "__builtin_spe_evmhesmi", SPE_BUILTIN_EVMHESMI },
3918   { 0, CODE_FOR_spe_evmhesmia, "__builtin_spe_evmhesmia", SPE_BUILTIN_EVMHESMIA },
3919   { 0, CODE_FOR_spe_evmhesmiaaw, "__builtin_spe_evmhesmiaaw", SPE_BUILTIN_EVMHESMIAAW },
3920   { 0, CODE_FOR_spe_evmhesmianw, "__builtin_spe_evmhesmianw", SPE_BUILTIN_EVMHESMIANW },
3921   { 0, CODE_FOR_spe_evmhessf, "__builtin_spe_evmhessf", SPE_BUILTIN_EVMHESSF },
3922   { 0, CODE_FOR_spe_evmhessfa, "__builtin_spe_evmhessfa", SPE_BUILTIN_EVMHESSFA },
3923   { 0, CODE_FOR_spe_evmhessfaaw, "__builtin_spe_evmhessfaaw", SPE_BUILTIN_EVMHESSFAAW },
3924   { 0, CODE_FOR_spe_evmhessfanw, "__builtin_spe_evmhessfanw", SPE_BUILTIN_EVMHESSFANW },
3925   { 0, CODE_FOR_spe_evmhessiaaw, "__builtin_spe_evmhessiaaw", SPE_BUILTIN_EVMHESSIAAW },
3926   { 0, CODE_FOR_spe_evmhessianw, "__builtin_spe_evmhessianw", SPE_BUILTIN_EVMHESSIANW },
3927   { 0, CODE_FOR_spe_evmheumi, "__builtin_spe_evmheumi", SPE_BUILTIN_EVMHEUMI },
3928   { 0, CODE_FOR_spe_evmheumia, "__builtin_spe_evmheumia", SPE_BUILTIN_EVMHEUMIA },
3929   { 0, CODE_FOR_spe_evmheumiaaw, "__builtin_spe_evmheumiaaw", SPE_BUILTIN_EVMHEUMIAAW },
3930   { 0, CODE_FOR_spe_evmheumianw, "__builtin_spe_evmheumianw", SPE_BUILTIN_EVMHEUMIANW },
3931   { 0, CODE_FOR_spe_evmheusiaaw, "__builtin_spe_evmheusiaaw", SPE_BUILTIN_EVMHEUSIAAW },
3932   { 0, CODE_FOR_spe_evmheusianw, "__builtin_spe_evmheusianw", SPE_BUILTIN_EVMHEUSIANW },
3933   { 0, CODE_FOR_spe_evmhogsmfaa, "__builtin_spe_evmhogsmfaa", SPE_BUILTIN_EVMHOGSMFAA },
3934   { 0, CODE_FOR_spe_evmhogsmfan, "__builtin_spe_evmhogsmfan", SPE_BUILTIN_EVMHOGSMFAN },
3935   { 0, CODE_FOR_spe_evmhogsmiaa, "__builtin_spe_evmhogsmiaa", SPE_BUILTIN_EVMHOGSMIAA },
3936   { 0, CODE_FOR_spe_evmhogsmian, "__builtin_spe_evmhogsmian", SPE_BUILTIN_EVMHOGSMIAN },
3937   { 0, CODE_FOR_spe_evmhogumiaa, "__builtin_spe_evmhogumiaa", SPE_BUILTIN_EVMHOGUMIAA },
3938   { 0, CODE_FOR_spe_evmhogumian, "__builtin_spe_evmhogumian", SPE_BUILTIN_EVMHOGUMIAN },
3939   { 0, CODE_FOR_spe_evmhosmf, "__builtin_spe_evmhosmf", SPE_BUILTIN_EVMHOSMF },
3940   { 0, CODE_FOR_spe_evmhosmfa, "__builtin_spe_evmhosmfa", SPE_BUILTIN_EVMHOSMFA },
3941   { 0, CODE_FOR_spe_evmhosmfaaw, "__builtin_spe_evmhosmfaaw", SPE_BUILTIN_EVMHOSMFAAW },
3942   { 0, CODE_FOR_spe_evmhosmfanw, "__builtin_spe_evmhosmfanw", SPE_BUILTIN_EVMHOSMFANW },
3943   { 0, CODE_FOR_spe_evmhosmi, "__builtin_spe_evmhosmi", SPE_BUILTIN_EVMHOSMI },
3944   { 0, CODE_FOR_spe_evmhosmia, "__builtin_spe_evmhosmia", SPE_BUILTIN_EVMHOSMIA },
3945   { 0, CODE_FOR_spe_evmhosmiaaw, "__builtin_spe_evmhosmiaaw", SPE_BUILTIN_EVMHOSMIAAW },
3946   { 0, CODE_FOR_spe_evmhosmianw, "__builtin_spe_evmhosmianw", SPE_BUILTIN_EVMHOSMIANW },
3947   { 0, CODE_FOR_spe_evmhossf, "__builtin_spe_evmhossf", SPE_BUILTIN_EVMHOSSF },
3948   { 0, CODE_FOR_spe_evmhossfa, "__builtin_spe_evmhossfa", SPE_BUILTIN_EVMHOSSFA },
3949   { 0, CODE_FOR_spe_evmhossfaaw, "__builtin_spe_evmhossfaaw", SPE_BUILTIN_EVMHOSSFAAW },
3950   { 0, CODE_FOR_spe_evmhossfanw, "__builtin_spe_evmhossfanw", SPE_BUILTIN_EVMHOSSFANW },
3951   { 0, CODE_FOR_spe_evmhossiaaw, "__builtin_spe_evmhossiaaw", SPE_BUILTIN_EVMHOSSIAAW },
3952   { 0, CODE_FOR_spe_evmhossianw, "__builtin_spe_evmhossianw", SPE_BUILTIN_EVMHOSSIANW },
3953   { 0, CODE_FOR_spe_evmhoumi, "__builtin_spe_evmhoumi", SPE_BUILTIN_EVMHOUMI },
3954   { 0, CODE_FOR_spe_evmhoumia, "__builtin_spe_evmhoumia", SPE_BUILTIN_EVMHOUMIA },
3955   { 0, CODE_FOR_spe_evmhoumiaaw, "__builtin_spe_evmhoumiaaw", SPE_BUILTIN_EVMHOUMIAAW },
3956   { 0, CODE_FOR_spe_evmhoumianw, "__builtin_spe_evmhoumianw", SPE_BUILTIN_EVMHOUMIANW },
3957   { 0, CODE_FOR_spe_evmhousiaaw, "__builtin_spe_evmhousiaaw", SPE_BUILTIN_EVMHOUSIAAW },
3958   { 0, CODE_FOR_spe_evmhousianw, "__builtin_spe_evmhousianw", SPE_BUILTIN_EVMHOUSIANW },
3959   { 0, CODE_FOR_spe_evmwhsmf, "__builtin_spe_evmwhsmf", SPE_BUILTIN_EVMWHSMF },
3960   { 0, CODE_FOR_spe_evmwhsmfa, "__builtin_spe_evmwhsmfa", SPE_BUILTIN_EVMWHSMFA },
3961   { 0, CODE_FOR_spe_evmwhsmi, "__builtin_spe_evmwhsmi", SPE_BUILTIN_EVMWHSMI },
3962   { 0, CODE_FOR_spe_evmwhsmia, "__builtin_spe_evmwhsmia", SPE_BUILTIN_EVMWHSMIA },
3963   { 0, CODE_FOR_spe_evmwhssf, "__builtin_spe_evmwhssf", SPE_BUILTIN_EVMWHSSF },
3964   { 0, CODE_FOR_spe_evmwhssfa, "__builtin_spe_evmwhssfa", SPE_BUILTIN_EVMWHSSFA },
3965   { 0, CODE_FOR_spe_evmwhumi, "__builtin_spe_evmwhumi", SPE_BUILTIN_EVMWHUMI },
3966   { 0, CODE_FOR_spe_evmwhumia, "__builtin_spe_evmwhumia", SPE_BUILTIN_EVMWHUMIA },
3967   { 0, CODE_FOR_spe_evmwlsmiaaw, "__builtin_spe_evmwlsmiaaw", SPE_BUILTIN_EVMWLSMIAAW },
3968   { 0, CODE_FOR_spe_evmwlsmianw, "__builtin_spe_evmwlsmianw", SPE_BUILTIN_EVMWLSMIANW },
3969   { 0, CODE_FOR_spe_evmwlssiaaw, "__builtin_spe_evmwlssiaaw", SPE_BUILTIN_EVMWLSSIAAW },
3970   { 0, CODE_FOR_spe_evmwlssianw, "__builtin_spe_evmwlssianw", SPE_BUILTIN_EVMWLSSIANW },
3971   { 0, CODE_FOR_spe_evmwlumi, "__builtin_spe_evmwlumi", SPE_BUILTIN_EVMWLUMI },
3972   { 0, CODE_FOR_spe_evmwlumia, "__builtin_spe_evmwlumia", SPE_BUILTIN_EVMWLUMIA },
3973   { 0, CODE_FOR_spe_evmwlumiaaw, "__builtin_spe_evmwlumiaaw", SPE_BUILTIN_EVMWLUMIAAW },
3974   { 0, CODE_FOR_spe_evmwlumianw, "__builtin_spe_evmwlumianw", SPE_BUILTIN_EVMWLUMIANW },
3975   { 0, CODE_FOR_spe_evmwlusiaaw, "__builtin_spe_evmwlusiaaw", SPE_BUILTIN_EVMWLUSIAAW },
3976   { 0, CODE_FOR_spe_evmwlusianw, "__builtin_spe_evmwlusianw", SPE_BUILTIN_EVMWLUSIANW },
3977   { 0, CODE_FOR_spe_evmwsmf, "__builtin_spe_evmwsmf", SPE_BUILTIN_EVMWSMF },
3978   { 0, CODE_FOR_spe_evmwsmfa, "__builtin_spe_evmwsmfa", SPE_BUILTIN_EVMWSMFA },
3979   { 0, CODE_FOR_spe_evmwsmfaa, "__builtin_spe_evmwsmfaa", SPE_BUILTIN_EVMWSMFAA },
3980   { 0, CODE_FOR_spe_evmwsmfan, "__builtin_spe_evmwsmfan", SPE_BUILTIN_EVMWSMFAN },
3981   { 0, CODE_FOR_spe_evmwsmi, "__builtin_spe_evmwsmi", SPE_BUILTIN_EVMWSMI },
3982   { 0, CODE_FOR_spe_evmwsmia, "__builtin_spe_evmwsmia", SPE_BUILTIN_EVMWSMIA },
3983   { 0, CODE_FOR_spe_evmwsmiaa, "__builtin_spe_evmwsmiaa", SPE_BUILTIN_EVMWSMIAA },
3984   { 0, CODE_FOR_spe_evmwsmian, "__builtin_spe_evmwsmian", SPE_BUILTIN_EVMWSMIAN },
3985   { 0, CODE_FOR_spe_evmwssf, "__builtin_spe_evmwssf", SPE_BUILTIN_EVMWSSF },
3986   { 0, CODE_FOR_spe_evmwssfa, "__builtin_spe_evmwssfa", SPE_BUILTIN_EVMWSSFA },
3987   { 0, CODE_FOR_spe_evmwssfaa, "__builtin_spe_evmwssfaa", SPE_BUILTIN_EVMWSSFAA },
3988   { 0, CODE_FOR_spe_evmwssfan, "__builtin_spe_evmwssfan", SPE_BUILTIN_EVMWSSFAN },
3989   { 0, CODE_FOR_spe_evmwumi, "__builtin_spe_evmwumi", SPE_BUILTIN_EVMWUMI },
3990   { 0, CODE_FOR_spe_evmwumia, "__builtin_spe_evmwumia", SPE_BUILTIN_EVMWUMIA },
3991   { 0, CODE_FOR_spe_evmwumiaa, "__builtin_spe_evmwumiaa", SPE_BUILTIN_EVMWUMIAA },
3992   { 0, CODE_FOR_spe_evmwumian, "__builtin_spe_evmwumian", SPE_BUILTIN_EVMWUMIAN },
3993   { 0, CODE_FOR_spe_evnand, "__builtin_spe_evnand", SPE_BUILTIN_EVNAND },
3994   { 0, CODE_FOR_spe_evnor, "__builtin_spe_evnor", SPE_BUILTIN_EVNOR },
3995   { 0, CODE_FOR_spe_evor, "__builtin_spe_evor", SPE_BUILTIN_EVOR },
3996   { 0, CODE_FOR_spe_evorc, "__builtin_spe_evorc", SPE_BUILTIN_EVORC },
3997   { 0, CODE_FOR_spe_evrlw, "__builtin_spe_evrlw", SPE_BUILTIN_EVRLW },
3998   { 0, CODE_FOR_spe_evslw, "__builtin_spe_evslw", SPE_BUILTIN_EVSLW },
3999   { 0, CODE_FOR_spe_evsrws, "__builtin_spe_evsrws", SPE_BUILTIN_EVSRWS },
4000   { 0, CODE_FOR_spe_evsrwu, "__builtin_spe_evsrwu", SPE_BUILTIN_EVSRWU },
4001   { 0, CODE_FOR_spe_evsubfw, "__builtin_spe_evsubfw", SPE_BUILTIN_EVSUBFW },
4002
4003   /* SPE binary operations expecting a 5-bit unsigned literal.  */
4004   { 0, CODE_FOR_spe_evaddiw, "__builtin_spe_evaddiw", SPE_BUILTIN_EVADDIW },
4005
4006   { 0, CODE_FOR_spe_evrlwi, "__builtin_spe_evrlwi", SPE_BUILTIN_EVRLWI },
4007   { 0, CODE_FOR_spe_evslwi, "__builtin_spe_evslwi", SPE_BUILTIN_EVSLWI },
4008   { 0, CODE_FOR_spe_evsrwis, "__builtin_spe_evsrwis", SPE_BUILTIN_EVSRWIS },
4009   { 0, CODE_FOR_spe_evsrwiu, "__builtin_spe_evsrwiu", SPE_BUILTIN_EVSRWIU },
4010   { 0, CODE_FOR_spe_evsubifw, "__builtin_spe_evsubifw", SPE_BUILTIN_EVSUBIFW },
4011   { 0, CODE_FOR_spe_evmwhssfaa, "__builtin_spe_evmwhssfaa", SPE_BUILTIN_EVMWHSSFAA },
4012   { 0, CODE_FOR_spe_evmwhssmaa, "__builtin_spe_evmwhssmaa", SPE_BUILTIN_EVMWHSSMAA },
4013   { 0, CODE_FOR_spe_evmwhsmfaa, "__builtin_spe_evmwhsmfaa", SPE_BUILTIN_EVMWHSMFAA },
4014   { 0, CODE_FOR_spe_evmwhsmiaa, "__builtin_spe_evmwhsmiaa", SPE_BUILTIN_EVMWHSMIAA },
4015   { 0, CODE_FOR_spe_evmwhusiaa, "__builtin_spe_evmwhusiaa", SPE_BUILTIN_EVMWHUSIAA },
4016   { 0, CODE_FOR_spe_evmwhumiaa, "__builtin_spe_evmwhumiaa", SPE_BUILTIN_EVMWHUMIAA },
4017   { 0, CODE_FOR_spe_evmwhssfan, "__builtin_spe_evmwhssfan", SPE_BUILTIN_EVMWHSSFAN },
4018   { 0, CODE_FOR_spe_evmwhssian, "__builtin_spe_evmwhssian", SPE_BUILTIN_EVMWHSSIAN },
4019   { 0, CODE_FOR_spe_evmwhsmfan, "__builtin_spe_evmwhsmfan", SPE_BUILTIN_EVMWHSMFAN },
4020   { 0, CODE_FOR_spe_evmwhsmian, "__builtin_spe_evmwhsmian", SPE_BUILTIN_EVMWHSMIAN },
4021   { 0, CODE_FOR_spe_evmwhusian, "__builtin_spe_evmwhusian", SPE_BUILTIN_EVMWHUSIAN },
4022   { 0, CODE_FOR_spe_evmwhumian, "__builtin_spe_evmwhumian", SPE_BUILTIN_EVMWHUMIAN },
4023   { 0, CODE_FOR_spe_evmwhgssfaa, "__builtin_spe_evmwhgssfaa", SPE_BUILTIN_EVMWHGSSFAA },
4024   { 0, CODE_FOR_spe_evmwhgsmfaa, "__builtin_spe_evmwhgsmfaa", SPE_BUILTIN_EVMWHGSMFAA },
4025   { 0, CODE_FOR_spe_evmwhgsmiaa, "__builtin_spe_evmwhgsmiaa", SPE_BUILTIN_EVMWHGSMIAA },
4026   { 0, CODE_FOR_spe_evmwhgumiaa, "__builtin_spe_evmwhgumiaa", SPE_BUILTIN_EVMWHGUMIAA },
4027   { 0, CODE_FOR_spe_evmwhgssfan, "__builtin_spe_evmwhgssfan", SPE_BUILTIN_EVMWHGSSFAN },
4028   { 0, CODE_FOR_spe_evmwhgsmfan, "__builtin_spe_evmwhgsmfan", SPE_BUILTIN_EVMWHGSMFAN },
4029   { 0, CODE_FOR_spe_evmwhgsmian, "__builtin_spe_evmwhgsmian", SPE_BUILTIN_EVMWHGSMIAN },
4030   { 0, CODE_FOR_spe_evmwhgumian, "__builtin_spe_evmwhgumian", SPE_BUILTIN_EVMWHGUMIAN },
4031   { 0, CODE_FOR_spe_brinc, "__builtin_spe_brinc", SPE_BUILTIN_BRINC },
4032
4033   /* Place-holder.  Leave as last binary SPE builtin.  */
4034   { 0, CODE_FOR_spe_evxor, "__builtin_spe_evxor", SPE_BUILTIN_EVXOR },
4035 };
4036
4037 /* AltiVec predicates.  */
4038
4039 struct builtin_description_predicates
4040 {
4041   const unsigned int mask;
4042   const enum insn_code icode;
4043   const char *opcode;
4044   const char *const name;
4045   const enum rs6000_builtins code;
4046 };
4047
4048 static const struct builtin_description_predicates bdesc_altivec_preds[] =
4049 {
4050   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpbfp.", "__builtin_altivec_vcmpbfp_p", ALTIVEC_BUILTIN_VCMPBFP_P },
4051   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpeqfp.", "__builtin_altivec_vcmpeqfp_p", ALTIVEC_BUILTIN_VCMPEQFP_P },
4052   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgefp.", "__builtin_altivec_vcmpgefp_p", ALTIVEC_BUILTIN_VCMPGEFP_P },
4053   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgtfp.", "__builtin_altivec_vcmpgtfp_p", ALTIVEC_BUILTIN_VCMPGTFP_P },
4054   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpequw.", "__builtin_altivec_vcmpequw_p", ALTIVEC_BUILTIN_VCMPEQUW_P },
4055   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtsw.", "__builtin_altivec_vcmpgtsw_p", ALTIVEC_BUILTIN_VCMPGTSW_P },
4056   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtuw.", "__builtin_altivec_vcmpgtuw_p", ALTIVEC_BUILTIN_VCMPGTUW_P },
4057   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtuh.", "__builtin_altivec_vcmpgtuh_p", ALTIVEC_BUILTIN_VCMPGTUH_P },
4058   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtsh.", "__builtin_altivec_vcmpgtsh_p", ALTIVEC_BUILTIN_VCMPGTSH_P },
4059   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpequh.", "__builtin_altivec_vcmpequh_p", ALTIVEC_BUILTIN_VCMPEQUH_P },
4060   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpequb.", "__builtin_altivec_vcmpequb_p", ALTIVEC_BUILTIN_VCMPEQUB_P },
4061   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtsb.", "__builtin_altivec_vcmpgtsb_p", ALTIVEC_BUILTIN_VCMPGTSB_P },
4062   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtub.", "__builtin_altivec_vcmpgtub_p", ALTIVEC_BUILTIN_VCMPGTUB_P }
4063 };
4064
4065 /* SPE predicates.  */
4066 static struct builtin_description bdesc_spe_predicates[] =
4067 {
4068   /* Place-holder.  Leave as first.  */
4069   { 0, CODE_FOR_spe_evcmpeq, "__builtin_spe_evcmpeq", SPE_BUILTIN_EVCMPEQ },
4070   { 0, CODE_FOR_spe_evcmpgts, "__builtin_spe_evcmpgts", SPE_BUILTIN_EVCMPGTS },
4071   { 0, CODE_FOR_spe_evcmpgtu, "__builtin_spe_evcmpgtu", SPE_BUILTIN_EVCMPGTU },
4072   { 0, CODE_FOR_spe_evcmplts, "__builtin_spe_evcmplts", SPE_BUILTIN_EVCMPLTS },
4073   { 0, CODE_FOR_spe_evcmpltu, "__builtin_spe_evcmpltu", SPE_BUILTIN_EVCMPLTU },
4074   { 0, CODE_FOR_spe_evfscmpeq, "__builtin_spe_evfscmpeq", SPE_BUILTIN_EVFSCMPEQ },
4075   { 0, CODE_FOR_spe_evfscmpgt, "__builtin_spe_evfscmpgt", SPE_BUILTIN_EVFSCMPGT },
4076   { 0, CODE_FOR_spe_evfscmplt, "__builtin_spe_evfscmplt", SPE_BUILTIN_EVFSCMPLT },
4077   { 0, CODE_FOR_spe_evfststeq, "__builtin_spe_evfststeq", SPE_BUILTIN_EVFSTSTEQ },
4078   { 0, CODE_FOR_spe_evfststgt, "__builtin_spe_evfststgt", SPE_BUILTIN_EVFSTSTGT },
4079   /* Place-holder.  Leave as last.  */
4080   { 0, CODE_FOR_spe_evfststlt, "__builtin_spe_evfststlt", SPE_BUILTIN_EVFSTSTLT },
4081 };
4082
4083 /* SPE evsel predicates.  */
4084 static struct builtin_description bdesc_spe_evsel[] =
4085 {
4086   /* Place-holder.  Leave as first.  */
4087   { 0, CODE_FOR_spe_evcmpgts, "__builtin_spe_evsel_gts", SPE_BUILTIN_EVSEL_CMPGTS },
4088   { 0, CODE_FOR_spe_evcmpgtu, "__builtin_spe_evsel_gtu", SPE_BUILTIN_EVSEL_CMPGTU },
4089   { 0, CODE_FOR_spe_evcmplts, "__builtin_spe_evsel_lts", SPE_BUILTIN_EVSEL_CMPLTS },
4090   { 0, CODE_FOR_spe_evcmpltu, "__builtin_spe_evsel_ltu", SPE_BUILTIN_EVSEL_CMPLTU },
4091   { 0, CODE_FOR_spe_evcmpeq, "__builtin_spe_evsel_eq", SPE_BUILTIN_EVSEL_CMPEQ },
4092   { 0, CODE_FOR_spe_evfscmpgt, "__builtin_spe_evsel_fsgt", SPE_BUILTIN_EVSEL_FSCMPGT },
4093   { 0, CODE_FOR_spe_evfscmplt, "__builtin_spe_evsel_fslt", SPE_BUILTIN_EVSEL_FSCMPLT },
4094   { 0, CODE_FOR_spe_evfscmpeq, "__builtin_spe_evsel_fseq", SPE_BUILTIN_EVSEL_FSCMPEQ },
4095   { 0, CODE_FOR_spe_evfststgt, "__builtin_spe_evsel_fststgt", SPE_BUILTIN_EVSEL_FSTSTGT },
4096   { 0, CODE_FOR_spe_evfststlt, "__builtin_spe_evsel_fststlt", SPE_BUILTIN_EVSEL_FSTSTLT },
4097   /* Place-holder.  Leave as last.  */
4098   { 0, CODE_FOR_spe_evfststeq, "__builtin_spe_evsel_fststeq", SPE_BUILTIN_EVSEL_FSTSTEQ },
4099 };
4100
4101 /* ABS* opreations.  */
4102
4103 static const struct builtin_description bdesc_abs[] =
4104 {
4105   { MASK_ALTIVEC, CODE_FOR_absv4si2, "__builtin_altivec_abs_v4si", ALTIVEC_BUILTIN_ABS_V4SI },
4106   { MASK_ALTIVEC, CODE_FOR_absv8hi2, "__builtin_altivec_abs_v8hi", ALTIVEC_BUILTIN_ABS_V8HI },
4107   { MASK_ALTIVEC, CODE_FOR_absv4sf2, "__builtin_altivec_abs_v4sf", ALTIVEC_BUILTIN_ABS_V4SF },
4108   { MASK_ALTIVEC, CODE_FOR_absv16qi2, "__builtin_altivec_abs_v16qi", ALTIVEC_BUILTIN_ABS_V16QI },
4109   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v4si, "__builtin_altivec_abss_v4si", ALTIVEC_BUILTIN_ABSS_V4SI },
4110   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v8hi, "__builtin_altivec_abss_v8hi", ALTIVEC_BUILTIN_ABSS_V8HI },
4111   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v16qi, "__builtin_altivec_abss_v16qi", ALTIVEC_BUILTIN_ABSS_V16QI }
4112 };
4113
4114 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
4115    foo (VECa).  */
4116
4117 static struct builtin_description bdesc_1arg[] =
4118 {
4119   { MASK_ALTIVEC, CODE_FOR_altivec_vexptefp, "__builtin_altivec_vexptefp", ALTIVEC_BUILTIN_VEXPTEFP },
4120   { MASK_ALTIVEC, CODE_FOR_altivec_vlogefp, "__builtin_altivec_vlogefp", ALTIVEC_BUILTIN_VLOGEFP },
4121   { MASK_ALTIVEC, CODE_FOR_altivec_vrefp, "__builtin_altivec_vrefp", ALTIVEC_BUILTIN_VREFP },
4122   { MASK_ALTIVEC, CODE_FOR_altivec_vrfim, "__builtin_altivec_vrfim", ALTIVEC_BUILTIN_VRFIM },
4123   { MASK_ALTIVEC, CODE_FOR_altivec_vrfin, "__builtin_altivec_vrfin", ALTIVEC_BUILTIN_VRFIN },
4124   { MASK_ALTIVEC, CODE_FOR_altivec_vrfip, "__builtin_altivec_vrfip", ALTIVEC_BUILTIN_VRFIP },
4125   { MASK_ALTIVEC, CODE_FOR_ftruncv4sf2, "__builtin_altivec_vrfiz", ALTIVEC_BUILTIN_VRFIZ },
4126   { MASK_ALTIVEC, CODE_FOR_altivec_vrsqrtefp, "__builtin_altivec_vrsqrtefp", ALTIVEC_BUILTIN_VRSQRTEFP },
4127   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisb, "__builtin_altivec_vspltisb", ALTIVEC_BUILTIN_VSPLTISB },
4128   { MASK_ALTIVEC, CODE_FOR_altivec_vspltish, "__builtin_altivec_vspltish", ALTIVEC_BUILTIN_VSPLTISH },
4129   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisw, "__builtin_altivec_vspltisw", ALTIVEC_BUILTIN_VSPLTISW },
4130   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsb, "__builtin_altivec_vupkhsb", ALTIVEC_BUILTIN_VUPKHSB },
4131   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhpx, "__builtin_altivec_vupkhpx", ALTIVEC_BUILTIN_VUPKHPX },
4132   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsh, "__builtin_altivec_vupkhsh", ALTIVEC_BUILTIN_VUPKHSH },
4133   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsb, "__builtin_altivec_vupklsb", ALTIVEC_BUILTIN_VUPKLSB },
4134   { MASK_ALTIVEC, CODE_FOR_altivec_vupklpx, "__builtin_altivec_vupklpx", ALTIVEC_BUILTIN_VUPKLPX },
4135   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsh, "__builtin_altivec_vupklsh", ALTIVEC_BUILTIN_VUPKLSH },
4136
4137   /* The SPE unary builtins must start with SPE_BUILTIN_EVABS and
4138      end with SPE_BUILTIN_EVSUBFUSIAAW.  */
4139   { 0, CODE_FOR_spe_evabs, "__builtin_spe_evabs", SPE_BUILTIN_EVABS },
4140   { 0, CODE_FOR_spe_evaddsmiaaw, "__builtin_spe_evaddsmiaaw", SPE_BUILTIN_EVADDSMIAAW },
4141   { 0, CODE_FOR_spe_evaddssiaaw, "__builtin_spe_evaddssiaaw", SPE_BUILTIN_EVADDSSIAAW },
4142   { 0, CODE_FOR_spe_evaddumiaaw, "__builtin_spe_evaddumiaaw", SPE_BUILTIN_EVADDUMIAAW },
4143   { 0, CODE_FOR_spe_evaddusiaaw, "__builtin_spe_evaddusiaaw", SPE_BUILTIN_EVADDUSIAAW },
4144   { 0, CODE_FOR_spe_evcntlsw, "__builtin_spe_evcntlsw", SPE_BUILTIN_EVCNTLSW },
4145   { 0, CODE_FOR_spe_evcntlzw, "__builtin_spe_evcntlzw", SPE_BUILTIN_EVCNTLZW },
4146   { 0, CODE_FOR_spe_evextsb, "__builtin_spe_evextsb", SPE_BUILTIN_EVEXTSB },
4147   { 0, CODE_FOR_spe_evextsh, "__builtin_spe_evextsh", SPE_BUILTIN_EVEXTSH },
4148   { 0, CODE_FOR_spe_evfsabs, "__builtin_spe_evfsabs", SPE_BUILTIN_EVFSABS },
4149   { 0, CODE_FOR_spe_evfscfsf, "__builtin_spe_evfscfsf", SPE_BUILTIN_EVFSCFSF },
4150   { 0, CODE_FOR_spe_evfscfsi, "__builtin_spe_evfscfsi", SPE_BUILTIN_EVFSCFSI },
4151   { 0, CODE_FOR_spe_evfscfuf, "__builtin_spe_evfscfuf", SPE_BUILTIN_EVFSCFUF },
4152   { 0, CODE_FOR_spe_evfscfui, "__builtin_spe_evfscfui", SPE_BUILTIN_EVFSCFUI },
4153   { 0, CODE_FOR_spe_evfsctsf, "__builtin_spe_evfsctsf", SPE_BUILTIN_EVFSCTSF },
4154   { 0, CODE_FOR_spe_evfsctsi, "__builtin_spe_evfsctsi", SPE_BUILTIN_EVFSCTSI },
4155   { 0, CODE_FOR_spe_evfsctsiz, "__builtin_spe_evfsctsiz", SPE_BUILTIN_EVFSCTSIZ },
4156   { 0, CODE_FOR_spe_evfsctuf, "__builtin_spe_evfsctuf", SPE_BUILTIN_EVFSCTUF },
4157   { 0, CODE_FOR_spe_evfsctui, "__builtin_spe_evfsctui", SPE_BUILTIN_EVFSCTUI },
4158   { 0, CODE_FOR_spe_evfsctuiz, "__builtin_spe_evfsctuiz", SPE_BUILTIN_EVFSCTUIZ },
4159   { 0, CODE_FOR_spe_evfsnabs, "__builtin_spe_evfsnabs", SPE_BUILTIN_EVFSNABS },
4160   { 0, CODE_FOR_spe_evfsneg, "__builtin_spe_evfsneg", SPE_BUILTIN_EVFSNEG },
4161   { 0, CODE_FOR_spe_evmra, "__builtin_spe_evmra", SPE_BUILTIN_EVMRA },
4162   { 0, CODE_FOR_spe_evneg, "__builtin_spe_evneg", SPE_BUILTIN_EVNEG },
4163   { 0, CODE_FOR_spe_evrndw, "__builtin_spe_evrndw", SPE_BUILTIN_EVRNDW },
4164   { 0, CODE_FOR_spe_evsubfsmiaaw, "__builtin_spe_evsubfsmiaaw", SPE_BUILTIN_EVSUBFSMIAAW },
4165   { 0, CODE_FOR_spe_evsubfssiaaw, "__builtin_spe_evsubfssiaaw", SPE_BUILTIN_EVSUBFSSIAAW },
4166   { 0, CODE_FOR_spe_evsubfumiaaw, "__builtin_spe_evsubfumiaaw", SPE_BUILTIN_EVSUBFUMIAAW },
4167   { 0, CODE_FOR_spe_evsplatfi, "__builtin_spe_evsplatfi", SPE_BUILTIN_EVSPLATFI },
4168   { 0, CODE_FOR_spe_evsplati, "__builtin_spe_evsplati", SPE_BUILTIN_EVSPLATI },
4169
4170   /* Place-holder.  Leave as last unary SPE builtin.  */
4171   { 0, CODE_FOR_spe_evsubfusiaaw, "__builtin_spe_evsubfusiaaw", SPE_BUILTIN_EVSUBFUSIAAW },
4172 };
4173
4174 static rtx
4175 rs6000_expand_unop_builtin (icode, arglist, target)
4176      enum insn_code icode;
4177      tree arglist;
4178      rtx target;
4179 {
4180   rtx pat;
4181   tree arg0 = TREE_VALUE (arglist);
4182   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4183   enum machine_mode tmode = insn_data[icode].operand[0].mode;
4184   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
4185
4186   if (icode == CODE_FOR_nothing)
4187     /* Builtin not supported on this processor.  */
4188     return 0;
4189
4190   /* If we got invalid arguments bail out before generating bad rtl.  */
4191   if (arg0 == error_mark_node)
4192     return const0_rtx;
4193
4194   if (icode == CODE_FOR_altivec_vspltisb
4195       || icode == CODE_FOR_altivec_vspltish
4196       || icode == CODE_FOR_altivec_vspltisw
4197       || icode == CODE_FOR_spe_evsplatfi
4198       || icode == CODE_FOR_spe_evsplati)
4199     {
4200       /* Only allow 5-bit *signed* literals.  */
4201       if (GET_CODE (op0) != CONST_INT
4202           || INTVAL (op0) > 0x1f
4203           || INTVAL (op0) < -0x1f)
4204         {
4205           error ("argument 1 must be a 5-bit signed literal");
4206           return const0_rtx;
4207         }
4208     }
4209
4210   if (target == 0
4211       || GET_MODE (target) != tmode
4212       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4213     target = gen_reg_rtx (tmode);
4214
4215   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
4216     op0 = copy_to_mode_reg (mode0, op0);
4217
4218   pat = GEN_FCN (icode) (target, op0);
4219   if (! pat)
4220     return 0;
4221   emit_insn (pat);
4222
4223   return target;
4224 }
4225
4226 static rtx
4227 altivec_expand_abs_builtin (icode, arglist, target)
4228      enum insn_code icode;
4229      tree arglist;
4230      rtx target;
4231 {
4232   rtx pat, scratch1, scratch2;
4233   tree arg0 = TREE_VALUE (arglist);
4234   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4235   enum machine_mode tmode = insn_data[icode].operand[0].mode;
4236   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
4237
4238   /* If we have invalid arguments, bail out before generating bad rtl.  */
4239   if (arg0 == error_mark_node)
4240     return const0_rtx;
4241
4242   if (target == 0
4243       || GET_MODE (target) != tmode
4244       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4245     target = gen_reg_rtx (tmode);
4246
4247   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
4248     op0 = copy_to_mode_reg (mode0, op0);
4249
4250   scratch1 = gen_reg_rtx (mode0);
4251   scratch2 = gen_reg_rtx (mode0);
4252
4253   pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
4254   if (! pat)
4255     return 0;
4256   emit_insn (pat);
4257
4258   return target;
4259 }
4260
4261 static rtx
4262 rs6000_expand_binop_builtin (icode, arglist, target)
4263      enum insn_code icode;
4264      tree arglist;
4265      rtx target;
4266 {
4267   rtx pat;
4268   tree arg0 = TREE_VALUE (arglist);
4269   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4270   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4271   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
4272   enum machine_mode tmode = insn_data[icode].operand[0].mode;
4273   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
4274   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
4275
4276   if (icode == CODE_FOR_nothing)
4277     /* Builtin not supported on this processor.  */
4278     return 0;
4279
4280   /* If we got invalid arguments bail out before generating bad rtl.  */
4281   if (arg0 == error_mark_node || arg1 == error_mark_node)
4282     return const0_rtx;
4283
4284   if (icode == CODE_FOR_altivec_vcfux
4285       || icode == CODE_FOR_altivec_vcfsx
4286       || icode == CODE_FOR_altivec_vctsxs
4287       || icode == CODE_FOR_altivec_vctuxs
4288       || icode == CODE_FOR_altivec_vspltb
4289       || icode == CODE_FOR_altivec_vsplth
4290       || icode == CODE_FOR_altivec_vspltw
4291       || icode == CODE_FOR_spe_evaddiw
4292       || icode == CODE_FOR_spe_evldd
4293       || icode == CODE_FOR_spe_evldh
4294       || icode == CODE_FOR_spe_evldw
4295       || icode == CODE_FOR_spe_evlhhesplat
4296       || icode == CODE_FOR_spe_evlhhossplat
4297       || icode == CODE_FOR_spe_evlhhousplat
4298       || icode == CODE_FOR_spe_evlwhe
4299       || icode == CODE_FOR_spe_evlwhos
4300       || icode == CODE_FOR_spe_evlwhou
4301       || icode == CODE_FOR_spe_evlwhsplat
4302       || icode == CODE_FOR_spe_evlwwsplat
4303       || icode == CODE_FOR_spe_evrlwi
4304       || icode == CODE_FOR_spe_evslwi
4305       || icode == CODE_FOR_spe_evsrwis
4306       || icode == CODE_FOR_spe_evsrwiu)
4307     {
4308       /* Only allow 5-bit unsigned literals.  */
4309       if (TREE_CODE (arg1) != INTEGER_CST
4310           || TREE_INT_CST_LOW (arg1) & ~0x1f)
4311         {
4312           error ("argument 2 must be a 5-bit unsigned literal");
4313           return const0_rtx;
4314         }
4315     }
4316
4317   if (target == 0
4318       || GET_MODE (target) != tmode
4319       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4320     target = gen_reg_rtx (tmode);
4321
4322   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
4323     op0 = copy_to_mode_reg (mode0, op0);
4324   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
4325     op1 = copy_to_mode_reg (mode1, op1);
4326
4327   pat = GEN_FCN (icode) (target, op0, op1);
4328   if (! pat)
4329     return 0;
4330   emit_insn (pat);
4331
4332   return target;
4333 }
4334
4335 static rtx
4336 altivec_expand_predicate_builtin (icode, opcode, arglist, target)
4337      enum insn_code icode;
4338      const char *opcode;
4339      tree arglist;
4340      rtx target;
4341 {
4342   rtx pat, scratch;
4343   tree cr6_form = TREE_VALUE (arglist);
4344   tree arg0 = TREE_VALUE (TREE_CHAIN (arglist));
4345   tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
4346   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4347   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
4348   enum machine_mode tmode = SImode;
4349   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
4350   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
4351   int cr6_form_int;
4352
4353   if (TREE_CODE (cr6_form) != INTEGER_CST)
4354     {
4355       error ("argument 1 of __builtin_altivec_predicate must be a constant");
4356       return const0_rtx;
4357     }
4358   else
4359     cr6_form_int = TREE_INT_CST_LOW (cr6_form);
4360
4361   if (mode0 != mode1)
4362     abort ();
4363
4364   /* If we have invalid arguments, bail out before generating bad rtl.  */
4365   if (arg0 == error_mark_node || arg1 == error_mark_node)
4366     return const0_rtx;
4367
4368   if (target == 0
4369       || GET_MODE (target) != tmode
4370       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4371     target = gen_reg_rtx (tmode);
4372
4373   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
4374     op0 = copy_to_mode_reg (mode0, op0);
4375   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
4376     op1 = copy_to_mode_reg (mode1, op1);
4377
4378   scratch = gen_reg_rtx (mode0);
4379
4380   pat = GEN_FCN (icode) (scratch, op0, op1,
4381                          gen_rtx (SYMBOL_REF, Pmode, opcode));
4382   if (! pat)
4383     return 0;
4384   emit_insn (pat);
4385
4386   /* The vec_any* and vec_all* predicates use the same opcodes for two
4387      different operations, but the bits in CR6 will be different
4388      depending on what information we want.  So we have to play tricks
4389      with CR6 to get the right bits out.
4390
4391      If you think this is disgusting, look at the specs for the
4392      AltiVec predicates.  */
4393
4394      switch (cr6_form_int)
4395        {
4396        case 0:
4397          emit_insn (gen_cr6_test_for_zero (target));
4398          break;
4399        case 1:
4400          emit_insn (gen_cr6_test_for_zero_reverse (target));
4401          break;
4402        case 2:
4403          emit_insn (gen_cr6_test_for_lt (target));
4404          break;
4405        case 3:
4406          emit_insn (gen_cr6_test_for_lt_reverse (target));
4407          break;
4408        default:
4409          error ("argument 1 of __builtin_altivec_predicate is out of range");
4410          break;
4411        }
4412
4413   return target;
4414 }
4415
4416 static rtx
4417 altivec_expand_stv_builtin (icode, arglist)
4418      enum insn_code icode;
4419      tree arglist;
4420 {
4421   tree arg0 = TREE_VALUE (arglist);
4422   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4423   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
4424   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4425   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
4426   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
4427   rtx pat;
4428   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
4429   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
4430   enum machine_mode mode2 = insn_data[icode].operand[2].mode;
4431
4432   /* Invalid arguments.  Bail before doing anything stoopid!  */
4433   if (arg0 == error_mark_node
4434       || arg1 == error_mark_node
4435       || arg2 == error_mark_node)
4436     return const0_rtx;
4437
4438   if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
4439     op0 = copy_to_mode_reg (mode2, op0);
4440   if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
4441     op1 = copy_to_mode_reg (mode0, op1);
4442   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
4443     op2 = copy_to_mode_reg (mode1, op2);
4444
4445   pat = GEN_FCN (icode) (op1, op2, op0);
4446   if (pat)
4447     emit_insn (pat);
4448   return NULL_RTX;
4449 }
4450
4451 static rtx
4452 rs6000_expand_ternop_builtin (icode, arglist, target)
4453      enum insn_code icode;
4454      tree arglist;
4455      rtx target;
4456 {
4457   rtx pat;
4458   tree arg0 = TREE_VALUE (arglist);
4459   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4460   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
4461   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4462   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
4463   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
4464   enum machine_mode tmode = insn_data[icode].operand[0].mode;
4465   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
4466   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
4467   enum machine_mode mode2 = insn_data[icode].operand[3].mode;
4468
4469   if (icode == CODE_FOR_nothing)
4470     /* Builtin not supported on this processor.  */
4471     return 0;
4472
4473   /* If we got invalid arguments bail out before generating bad rtl.  */
4474   if (arg0 == error_mark_node
4475       || arg1 == error_mark_node
4476       || arg2 == error_mark_node)
4477     return const0_rtx;
4478
4479   if (icode == CODE_FOR_altivec_vsldoi_4sf
4480       || icode == CODE_FOR_altivec_vsldoi_4si
4481       || icode == CODE_FOR_altivec_vsldoi_8hi
4482       || icode == CODE_FOR_altivec_vsldoi_16qi)
4483     {
4484       /* Only allow 4-bit unsigned literals.  */
4485       if (TREE_CODE (arg2) != INTEGER_CST
4486           || TREE_INT_CST_LOW (arg2) & ~0xf)
4487         {
4488           error ("argument 3 must be a 4-bit unsigned literal");
4489           return const0_rtx;
4490         }
4491     }
4492
4493   if (target == 0
4494       || GET_MODE (target) != tmode
4495       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4496     target = gen_reg_rtx (tmode);
4497
4498   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
4499     op0 = copy_to_mode_reg (mode0, op0);
4500   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
4501     op1 = copy_to_mode_reg (mode1, op1);
4502   if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
4503     op2 = copy_to_mode_reg (mode2, op2);
4504
4505   pat = GEN_FCN (icode) (target, op0, op1, op2);
4506   if (! pat)
4507     return 0;
4508   emit_insn (pat);
4509
4510   return target;
4511 }
4512
4513 /* Expand the lvx builtins.  */
4514 static rtx
4515 altivec_expand_ld_builtin (exp, target, expandedp)
4516      tree exp;
4517      rtx target;
4518      bool *expandedp;
4519 {
4520   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4521   tree arglist = TREE_OPERAND (exp, 1);
4522   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
4523   tree arg0;
4524   enum machine_mode tmode, mode0;
4525   rtx pat, op0;
4526   enum insn_code icode;
4527
4528   switch (fcode)
4529     {
4530     case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
4531       icode = CODE_FOR_altivec_lvx_16qi;
4532       break;
4533     case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
4534       icode = CODE_FOR_altivec_lvx_8hi;
4535       break;
4536     case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
4537       icode = CODE_FOR_altivec_lvx_4si;
4538       break;
4539     case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
4540       icode = CODE_FOR_altivec_lvx_4sf;
4541       break;
4542     default:
4543       *expandedp = false;
4544       return NULL_RTX;
4545     }
4546
4547   *expandedp = true;
4548
4549   arg0 = TREE_VALUE (arglist);
4550   op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4551   tmode = insn_data[icode].operand[0].mode;
4552   mode0 = insn_data[icode].operand[1].mode;
4553
4554   if (target == 0
4555       || GET_MODE (target) != tmode
4556       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4557     target = gen_reg_rtx (tmode);
4558
4559   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
4560     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
4561
4562   pat = GEN_FCN (icode) (target, op0);
4563   if (! pat)
4564     return 0;
4565   emit_insn (pat);
4566   return target;
4567 }
4568
4569 /* Expand the stvx builtins.  */
4570 static rtx
4571 altivec_expand_st_builtin (exp, target, expandedp)
4572      tree exp;
4573      rtx target ATTRIBUTE_UNUSED;
4574      bool *expandedp;
4575 {
4576   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4577   tree arglist = TREE_OPERAND (exp, 1);
4578   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
4579   tree arg0, arg1;
4580   enum machine_mode mode0, mode1;
4581   rtx pat, op0, op1;
4582   enum insn_code icode;
4583
4584   switch (fcode)
4585     {
4586     case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
4587       icode = CODE_FOR_altivec_stvx_16qi;
4588       break;
4589     case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
4590       icode = CODE_FOR_altivec_stvx_8hi;
4591       break;
4592     case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
4593       icode = CODE_FOR_altivec_stvx_4si;
4594       break;
4595     case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
4596       icode = CODE_FOR_altivec_stvx_4sf;
4597       break;
4598     default:
4599       *expandedp = false;
4600       return NULL_RTX;
4601     }
4602
4603   arg0 = TREE_VALUE (arglist);
4604   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4605   op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4606   op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
4607   mode0 = insn_data[icode].operand[0].mode;
4608   mode1 = insn_data[icode].operand[1].mode;
4609
4610   if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
4611     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
4612   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
4613     op1 = copy_to_mode_reg (mode1, op1);
4614
4615   pat = GEN_FCN (icode) (op0, op1);
4616   if (pat)
4617     emit_insn (pat);
4618
4619   *expandedp = true;
4620   return NULL_RTX;
4621 }
4622
4623 /* Expand the dst builtins.  */
4624 static rtx
4625 altivec_expand_dst_builtin (exp, target, expandedp)
4626      tree exp;
4627      rtx target ATTRIBUTE_UNUSED;
4628      bool *expandedp;
4629 {
4630   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4631   tree arglist = TREE_OPERAND (exp, 1);
4632   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
4633   tree arg0, arg1, arg2;
4634   enum machine_mode mode0, mode1, mode2;
4635   rtx pat, op0, op1, op2;
4636   struct builtin_description *d;
4637   size_t i;
4638
4639   *expandedp = false;
4640
4641   /* Handle DST variants.  */
4642   d = (struct builtin_description *) bdesc_dst;
4643   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
4644     if (d->code == fcode)
4645       {
4646         arg0 = TREE_VALUE (arglist);
4647         arg1 = TREE_VALUE (TREE_CHAIN (arglist));
4648         arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
4649         op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4650         op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
4651         op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
4652         mode0 = insn_data[d->icode].operand[0].mode;
4653         mode1 = insn_data[d->icode].operand[1].mode;
4654         mode2 = insn_data[d->icode].operand[2].mode;
4655
4656         /* Invalid arguments, bail out before generating bad rtl.  */
4657         if (arg0 == error_mark_node
4658             || arg1 == error_mark_node
4659             || arg2 == error_mark_node)
4660           return const0_rtx;
4661
4662         if (TREE_CODE (arg2) != INTEGER_CST
4663             || TREE_INT_CST_LOW (arg2) & ~0x3)
4664           {
4665             error ("argument to `%s' must be a 2-bit unsigned literal", d->name);
4666             return const0_rtx;
4667           }
4668
4669         if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
4670           op0 = copy_to_mode_reg (mode0, op0);
4671         if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
4672           op1 = copy_to_mode_reg (mode1, op1);
4673
4674         pat = GEN_FCN (d->icode) (op0, op1, op2);
4675         if (pat != 0)
4676           emit_insn (pat);
4677
4678         *expandedp = true;
4679         return NULL_RTX;
4680       }
4681
4682   return NULL_RTX;
4683 }
4684
4685 /* Expand the builtin in EXP and store the result in TARGET.  Store
4686    true in *EXPANDEDP if we found a builtin to expand.  */
4687 static rtx
4688 altivec_expand_builtin (exp, target, expandedp)
4689      tree exp;
4690      rtx target;
4691      bool *expandedp;
4692 {
4693   struct builtin_description *d;
4694   struct builtin_description_predicates *dp;
4695   size_t i;
4696   enum insn_code icode;
4697   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4698   tree arglist = TREE_OPERAND (exp, 1);
4699   tree arg0;
4700   rtx op0, pat;
4701   enum machine_mode tmode, mode0;
4702   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
4703
4704   target = altivec_expand_ld_builtin (exp, target, expandedp);
4705   if (*expandedp)
4706     return target;
4707
4708   target = altivec_expand_st_builtin (exp, target, expandedp);
4709   if (*expandedp)
4710     return target;
4711
4712   target = altivec_expand_dst_builtin (exp, target, expandedp);
4713   if (*expandedp)
4714     return target;
4715
4716   *expandedp = true;
4717
4718   switch (fcode)
4719     {
4720     case ALTIVEC_BUILTIN_STVX:
4721       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx, arglist);
4722     case ALTIVEC_BUILTIN_STVEBX:
4723       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, arglist);
4724     case ALTIVEC_BUILTIN_STVEHX:
4725       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, arglist);
4726     case ALTIVEC_BUILTIN_STVEWX:
4727       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, arglist);
4728     case ALTIVEC_BUILTIN_STVXL:
4729       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl, arglist);
4730
4731     case ALTIVEC_BUILTIN_MFVSCR:
4732       icode = CODE_FOR_altivec_mfvscr;
4733       tmode = insn_data[icode].operand[0].mode;
4734
4735       if (target == 0
4736           || GET_MODE (target) != tmode
4737           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4738         target = gen_reg_rtx (tmode);
4739       
4740       pat = GEN_FCN (icode) (target);
4741       if (! pat)
4742         return 0;
4743       emit_insn (pat);
4744       return target;
4745
4746     case ALTIVEC_BUILTIN_MTVSCR:
4747       icode = CODE_FOR_altivec_mtvscr;
4748       arg0 = TREE_VALUE (arglist);
4749       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4750       mode0 = insn_data[icode].operand[0].mode;
4751
4752       /* If we got invalid arguments bail out before generating bad rtl.  */
4753       if (arg0 == error_mark_node)
4754         return const0_rtx;
4755
4756       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
4757         op0 = copy_to_mode_reg (mode0, op0);
4758
4759       pat = GEN_FCN (icode) (op0);
4760       if (pat)
4761         emit_insn (pat);
4762       return NULL_RTX;
4763
4764     case ALTIVEC_BUILTIN_DSSALL:
4765       emit_insn (gen_altivec_dssall ());
4766       return NULL_RTX;
4767
4768     case ALTIVEC_BUILTIN_DSS:
4769       icode = CODE_FOR_altivec_dss;
4770       arg0 = TREE_VALUE (arglist);
4771       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4772       mode0 = insn_data[icode].operand[0].mode;
4773
4774       /* If we got invalid arguments bail out before generating bad rtl.  */
4775       if (arg0 == error_mark_node)
4776         return const0_rtx;
4777
4778       if (TREE_CODE (arg0) != INTEGER_CST
4779           || TREE_INT_CST_LOW (arg0) & ~0x3)
4780         {
4781           error ("argument to dss must be a 2-bit unsigned literal");
4782           return const0_rtx;
4783         }
4784
4785       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
4786         op0 = copy_to_mode_reg (mode0, op0);
4787
4788       emit_insn (gen_altivec_dss (op0));
4789       return NULL_RTX;
4790     }
4791
4792   /* Expand abs* operations.  */
4793   d = (struct builtin_description *) bdesc_abs;
4794   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
4795     if (d->code == fcode)
4796       return altivec_expand_abs_builtin (d->icode, arglist, target);
4797
4798   /* Expand the AltiVec predicates.  */
4799   dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
4800   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
4801     if (dp->code == fcode)
4802       return altivec_expand_predicate_builtin (dp->icode, dp->opcode, arglist, target);
4803
4804   /* LV* are funky.  We initialized them differently.  */
4805   switch (fcode)
4806     {
4807     case ALTIVEC_BUILTIN_LVSL:
4808       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvsl,
4809                                            arglist, target);
4810     case ALTIVEC_BUILTIN_LVSR:
4811       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvsr,
4812                                           arglist, target);
4813     case ALTIVEC_BUILTIN_LVEBX:
4814       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvebx,
4815                                           arglist, target);
4816     case ALTIVEC_BUILTIN_LVEHX:
4817       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvehx,
4818                                           arglist, target);
4819     case ALTIVEC_BUILTIN_LVEWX:
4820       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvewx,
4821                                           arglist, target);
4822     case ALTIVEC_BUILTIN_LVXL:
4823       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvxl,
4824                                           arglist, target);
4825     case ALTIVEC_BUILTIN_LVX:
4826       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvx,
4827                                           arglist, target);
4828     default:
4829       break;
4830       /* Fall through.  */
4831     }
4832
4833   *expandedp = false;
4834   return NULL_RTX;
4835 }
4836
4837 /* Binops that need to be initialized manually, but can be expanded
4838    automagically by rs6000_expand_binop_builtin.  */
4839 static struct builtin_description bdesc_2arg_spe[] =
4840 {
4841   { 0, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
4842   { 0, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
4843   { 0, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
4844   { 0, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
4845   { 0, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
4846   { 0, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
4847   { 0, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
4848   { 0, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
4849   { 0, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
4850   { 0, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
4851   { 0, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
4852   { 0, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
4853   { 0, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
4854   { 0, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
4855   { 0, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
4856   { 0, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
4857   { 0, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
4858   { 0, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
4859   { 0, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
4860   { 0, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
4861   { 0, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
4862   { 0, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
4863 };
4864
4865 /* Expand the builtin in EXP and store the result in TARGET.  Store
4866    true in *EXPANDEDP if we found a builtin to expand.
4867
4868    This expands the SPE builtins that are not simple unary and binary
4869    operations.  */
4870 static rtx
4871 spe_expand_builtin (exp, target, expandedp)
4872      tree exp;
4873      rtx target;
4874      bool *expandedp;
4875 {
4876   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4877   tree arglist = TREE_OPERAND (exp, 1);
4878   tree arg1, arg0;
4879   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
4880   enum insn_code icode;
4881   enum machine_mode tmode, mode0;
4882   rtx pat, op0;
4883   struct builtin_description *d;
4884   size_t i;
4885
4886   *expandedp = true;
4887
4888   /* Syntax check for a 5-bit unsigned immediate.  */
4889   switch (fcode)
4890     {
4891     case SPE_BUILTIN_EVSTDD:
4892     case SPE_BUILTIN_EVSTDH:
4893     case SPE_BUILTIN_EVSTDW:
4894     case SPE_BUILTIN_EVSTWHE:
4895     case SPE_BUILTIN_EVSTWHO:
4896     case SPE_BUILTIN_EVSTWWE:
4897     case SPE_BUILTIN_EVSTWWO:
4898       arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
4899       if (TREE_CODE (arg1) != INTEGER_CST
4900           || TREE_INT_CST_LOW (arg1) & ~0x1f)
4901         {
4902           error ("argument 2 must be a 5-bit unsigned literal");
4903           return const0_rtx;
4904         }
4905       break;
4906     default:
4907       break;
4908     }
4909
4910   d = (struct builtin_description *) bdesc_2arg_spe;
4911   for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
4912     if (d->code == fcode)
4913       return rs6000_expand_binop_builtin (d->icode, arglist, target);
4914
4915   d = (struct builtin_description *) bdesc_spe_predicates;
4916   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
4917     if (d->code == fcode)
4918       return spe_expand_predicate_builtin (d->icode, arglist, target);
4919
4920   d = (struct builtin_description *) bdesc_spe_evsel;
4921   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
4922     if (d->code == fcode)
4923       return spe_expand_evsel_builtin (d->icode, arglist, target);
4924
4925   switch (fcode)
4926     {
4927     case SPE_BUILTIN_EVSTDDX:
4928       return altivec_expand_stv_builtin (CODE_FOR_spe_evstddx, arglist);
4929     case SPE_BUILTIN_EVSTDHX:
4930       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdhx, arglist);
4931     case SPE_BUILTIN_EVSTDWX:
4932       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdwx, arglist);
4933     case SPE_BUILTIN_EVSTWHEX:
4934       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhex, arglist);
4935     case SPE_BUILTIN_EVSTWHOX:
4936       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhox, arglist);
4937     case SPE_BUILTIN_EVSTWWEX:
4938       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwex, arglist);
4939     case SPE_BUILTIN_EVSTWWOX:
4940       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwox, arglist);
4941     case SPE_BUILTIN_EVSTDD:
4942       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdd, arglist);
4943     case SPE_BUILTIN_EVSTDH:
4944       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdh, arglist);
4945     case SPE_BUILTIN_EVSTDW:
4946       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdw, arglist);
4947     case SPE_BUILTIN_EVSTWHE:
4948       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhe, arglist);
4949     case SPE_BUILTIN_EVSTWHO:
4950       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwho, arglist);
4951     case SPE_BUILTIN_EVSTWWE:
4952       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwe, arglist);
4953     case SPE_BUILTIN_EVSTWWO:
4954       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwo, arglist);
4955     case SPE_BUILTIN_MFSPEFSCR:
4956       icode = CODE_FOR_spe_mfspefscr;
4957       tmode = insn_data[icode].operand[0].mode;
4958
4959       if (target == 0
4960           || GET_MODE (target) != tmode
4961           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
4962         target = gen_reg_rtx (tmode);
4963       
4964       pat = GEN_FCN (icode) (target);
4965       if (! pat)
4966         return 0;
4967       emit_insn (pat);
4968       return target;
4969     case SPE_BUILTIN_MTSPEFSCR:
4970       icode = CODE_FOR_spe_mtspefscr;
4971       arg0 = TREE_VALUE (arglist);
4972       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4973       mode0 = insn_data[icode].operand[0].mode;
4974
4975       if (arg0 == error_mark_node)
4976         return const0_rtx;
4977
4978       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
4979         op0 = copy_to_mode_reg (mode0, op0);
4980
4981       pat = GEN_FCN (icode) (op0);
4982       if (pat)
4983         emit_insn (pat);
4984       return NULL_RTX;
4985     default:
4986       break;
4987     }
4988
4989   *expandedp = false;
4990   return NULL_RTX;
4991 }
4992
4993 static rtx
4994 spe_expand_predicate_builtin (icode, arglist, target)
4995      enum insn_code icode;
4996      tree arglist;
4997      rtx target;
4998 {
4999   rtx pat, scratch, tmp;
5000   tree form = TREE_VALUE (arglist);
5001   tree arg0 = TREE_VALUE (TREE_CHAIN (arglist));
5002   tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5003   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5004   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5005   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5006   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5007   int form_int;
5008   enum rtx_code code;
5009
5010   if (TREE_CODE (form) != INTEGER_CST)
5011     {
5012       error ("argument 1 of __builtin_spe_predicate must be a constant");
5013       return const0_rtx;
5014     }
5015   else
5016     form_int = TREE_INT_CST_LOW (form);
5017
5018   if (mode0 != mode1)
5019     abort ();
5020
5021   if (arg0 == error_mark_node || arg1 == error_mark_node)
5022     return const0_rtx;
5023
5024   if (target == 0
5025       || GET_MODE (target) != SImode
5026       || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
5027     target = gen_reg_rtx (SImode);
5028
5029   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5030     op0 = copy_to_mode_reg (mode0, op0);
5031   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5032     op1 = copy_to_mode_reg (mode1, op1);
5033
5034   scratch = gen_reg_rtx (CCmode);
5035
5036   pat = GEN_FCN (icode) (scratch, op0, op1);
5037   if (! pat)
5038     return const0_rtx;
5039   emit_insn (pat);
5040
5041   /* There are 4 variants for each predicate: _any_, _all_, _upper_,
5042      _lower_.  We use one compare, but look in different bits of the
5043      CR for each variant.
5044
5045      There are 2 elements in each SPE simd type (upper/lower).  The CR
5046      bits are set as follows:
5047
5048      BIT0  | BIT 1  | BIT 2   | BIT 3
5049      U     |   L    | (U | L) | (U & L)
5050
5051      So, for an "all" relationship, BIT 3 would be set.
5052      For an "any" relationship, BIT 2 would be set.  Etc.
5053
5054      Following traditional nomenclature, these bits map to:
5055
5056      BIT0  | BIT 1  | BIT 2   | BIT 3
5057      LT    | GT     | EQ      | OV
5058
5059      Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
5060   */
5061
5062   switch (form_int)
5063     {
5064       /* All variant.  OV bit.  */
5065     case 0:
5066       /* We need to get to the OV bit, which is the ORDERED bit.  We
5067          could generate (ordered:SI (reg:CC xx) (const_int 0)), but
5068          that's ugly and will trigger a validate_condition_mode abort.
5069          So let's just use another pattern.  */
5070       emit_insn (gen_move_from_CR_ov_bit (target, scratch));
5071       return target;
5072       /* Any variant.  EQ bit.  */
5073     case 1:
5074       code = EQ;
5075       break;
5076       /* Upper variant.  LT bit.  */
5077     case 2:
5078       code = LT;
5079       break;
5080       /* Lower variant.  GT bit.  */
5081     case 3:
5082       code = GT;
5083       break;
5084     default:
5085       error ("argument 1 of __builtin_spe_predicate is out of range");
5086       return const0_rtx;
5087     }
5088
5089   tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
5090   emit_move_insn (target, tmp);
5091
5092   return target;
5093 }
5094
5095 /* The evsel builtins look like this:
5096
5097      e = __builtin_spe_evsel_OP (a, b, c, d);
5098
5099    and work like this:
5100
5101      e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
5102      e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
5103 */
5104
5105 static rtx
5106 spe_expand_evsel_builtin (icode, arglist, target)
5107      enum insn_code icode;
5108      tree arglist;
5109      rtx target;
5110 {
5111   rtx pat, scratch;
5112   tree arg0 = TREE_VALUE (arglist);
5113   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5114   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5115   tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
5116   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5117   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5118   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
5119   rtx op3 = expand_expr (arg3, NULL_RTX, VOIDmode, 0);
5120   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5121   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5122
5123   if (mode0 != mode1)
5124     abort ();
5125
5126   if (arg0 == error_mark_node || arg1 == error_mark_node
5127       || arg2 == error_mark_node || arg3 == error_mark_node)
5128     return const0_rtx;
5129
5130   if (target == 0
5131       || GET_MODE (target) != mode0
5132       || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
5133     target = gen_reg_rtx (mode0);
5134
5135   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5136     op0 = copy_to_mode_reg (mode0, op0);
5137   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
5138     op1 = copy_to_mode_reg (mode0, op1);
5139   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
5140     op2 = copy_to_mode_reg (mode0, op2);
5141   if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
5142     op3 = copy_to_mode_reg (mode0, op3);
5143
5144   /* Generate the compare.  */
5145   scratch = gen_reg_rtx (CCmode);
5146   pat = GEN_FCN (icode) (scratch, op0, op1);
5147   if (! pat)
5148     return const0_rtx;
5149   emit_insn (pat);
5150
5151   if (mode0 == V2SImode)
5152     emit_insn (gen_spe_evsel (target, op2, op3, scratch));
5153   else
5154     emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
5155
5156   return target;
5157 }
5158
5159 /* Expand an expression EXP that calls a built-in function,
5160    with result going to TARGET if that's convenient
5161    (and in mode MODE if that's convenient).
5162    SUBTARGET may be used as the target for computing one of EXP's operands.
5163    IGNORE is nonzero if the value is to be ignored.  */
5164
5165 static rtx
5166 rs6000_expand_builtin (exp, target, subtarget, mode, ignore)
5167      tree exp;
5168      rtx target;
5169      rtx subtarget ATTRIBUTE_UNUSED;
5170      enum machine_mode mode ATTRIBUTE_UNUSED;
5171      int ignore ATTRIBUTE_UNUSED;
5172 {
5173   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5174   tree arglist = TREE_OPERAND (exp, 1);
5175   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5176   struct builtin_description *d;
5177   size_t i;
5178   rtx ret;
5179   bool success;
5180   
5181   if (TARGET_ALTIVEC)
5182     {
5183       ret = altivec_expand_builtin (exp, target, &success);
5184
5185       if (success)
5186         return ret;
5187     }
5188   if (TARGET_SPE)
5189     {
5190       ret = spe_expand_builtin (exp, target, &success);
5191
5192       if (success)
5193         return ret;
5194     }
5195
5196   if (TARGET_ALTIVEC || TARGET_SPE)
5197     {
5198       /* Handle simple unary operations.  */
5199       d = (struct builtin_description *) bdesc_1arg;
5200       for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
5201         if (d->code == fcode)
5202           return rs6000_expand_unop_builtin (d->icode, arglist, target);
5203
5204       /* Handle simple binary operations.  */
5205       d = (struct builtin_description *) bdesc_2arg;
5206       for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
5207         if (d->code == fcode)
5208           return rs6000_expand_binop_builtin (d->icode, arglist, target);
5209
5210       /* Handle simple ternary operations.  */
5211       d = (struct builtin_description *) bdesc_3arg;
5212       for (i = 0; i < ARRAY_SIZE  (bdesc_3arg); i++, d++)
5213         if (d->code == fcode)
5214           return rs6000_expand_ternop_builtin (d->icode, arglist, target);
5215     }
5216
5217   abort ();
5218   return NULL_RTX;
5219 }
5220
5221 static void
5222 rs6000_init_builtins ()
5223 {
5224   if (TARGET_SPE)
5225     spe_init_builtins ();
5226   if (TARGET_ALTIVEC)
5227     altivec_init_builtins ();
5228   if (TARGET_ALTIVEC || TARGET_SPE)
5229     rs6000_common_init_builtins ();
5230 }
5231
5232 /* Search through a set of builtins and enable the mask bits.
5233    DESC is an array of builtins.
5234    SIZE is the totaly number of builtins.
5235    START is the builtin enum at which to start.
5236    END is the builtin enum at which to end.  */
5237 static void
5238 enable_mask_for_builtins (desc, size, start, end)
5239      struct builtin_description *desc;
5240      int size;
5241      enum rs6000_builtins start, end;
5242 {
5243   int i;
5244
5245   for (i = 0; i < size; ++i)
5246     if (desc[i].code == start)
5247       break;
5248
5249   if (i == size)
5250     return;
5251
5252   for (; i < size; ++i)
5253     {
5254       /* Flip all the bits on.  */
5255       desc[i].mask = target_flags;
5256       if (desc[i].code == end)
5257         break;
5258     }
5259 }
5260
5261 static void
5262 spe_init_builtins ()
5263 {
5264   tree endlink = void_list_node;
5265   tree puint_type_node = build_pointer_type (unsigned_type_node);
5266   tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
5267   tree pv2si_type_node = build_pointer_type (V2SI_type_node);
5268   struct builtin_description *d;
5269   size_t i;
5270
5271   tree v2si_ftype_4_v2si
5272     = build_function_type
5273     (V2SI_type_node,
5274      tree_cons (NULL_TREE, V2SI_type_node,
5275                 tree_cons (NULL_TREE, V2SI_type_node,
5276                            tree_cons (NULL_TREE, V2SI_type_node,
5277                                       tree_cons (NULL_TREE, V2SI_type_node,
5278                                                  endlink)))));
5279
5280   tree v2sf_ftype_4_v2sf
5281     = build_function_type
5282     (V2SF_type_node,
5283      tree_cons (NULL_TREE, V2SF_type_node,
5284                 tree_cons (NULL_TREE, V2SF_type_node,
5285                            tree_cons (NULL_TREE, V2SF_type_node,
5286                                       tree_cons (NULL_TREE, V2SF_type_node,
5287                                                  endlink)))));
5288
5289   tree int_ftype_int_v2si_v2si
5290     = build_function_type
5291     (integer_type_node,
5292      tree_cons (NULL_TREE, integer_type_node,
5293                 tree_cons (NULL_TREE, V2SI_type_node,
5294                            tree_cons (NULL_TREE, V2SI_type_node,
5295                                       endlink))));
5296
5297   tree int_ftype_int_v2sf_v2sf
5298     = build_function_type
5299     (integer_type_node,
5300      tree_cons (NULL_TREE, integer_type_node,
5301                 tree_cons (NULL_TREE, V2SF_type_node,
5302                            tree_cons (NULL_TREE, V2SF_type_node,
5303                                       endlink))));
5304
5305   tree void_ftype_v2si_puint_int
5306     = build_function_type (void_type_node,
5307                            tree_cons (NULL_TREE, V2SI_type_node,
5308                                       tree_cons (NULL_TREE, puint_type_node,
5309                                                  tree_cons (NULL_TREE,
5310                                                             integer_type_node,
5311                                                             endlink))));
5312
5313   tree void_ftype_v2si_puint_char
5314     = build_function_type (void_type_node,
5315                            tree_cons (NULL_TREE, V2SI_type_node,
5316                                       tree_cons (NULL_TREE, puint_type_node,
5317                                                  tree_cons (NULL_TREE,
5318                                                             char_type_node,
5319                                                             endlink))));
5320
5321   tree void_ftype_v2si_pv2si_int
5322     = build_function_type (void_type_node,
5323                            tree_cons (NULL_TREE, V2SI_type_node,
5324                                       tree_cons (NULL_TREE, pv2si_type_node,
5325                                                  tree_cons (NULL_TREE,
5326                                                             integer_type_node,
5327                                                             endlink))));
5328
5329   tree void_ftype_v2si_pv2si_char
5330     = build_function_type (void_type_node,
5331                            tree_cons (NULL_TREE, V2SI_type_node,
5332                                       tree_cons (NULL_TREE, pv2si_type_node,
5333                                                  tree_cons (NULL_TREE,
5334                                                             char_type_node,
5335                                                             endlink))));
5336
5337   tree void_ftype_int
5338     = build_function_type (void_type_node,
5339                            tree_cons (NULL_TREE, integer_type_node, endlink));
5340
5341   tree int_ftype_void
5342     = build_function_type (integer_type_node,
5343                            tree_cons (NULL_TREE, void_type_node, endlink));
5344
5345   tree v2si_ftype_pv2si_int
5346     = build_function_type (V2SI_type_node,
5347                            tree_cons (NULL_TREE, pv2si_type_node,
5348                                       tree_cons (NULL_TREE, integer_type_node,
5349                                                  endlink)));
5350
5351   tree v2si_ftype_puint_int
5352     = build_function_type (V2SI_type_node,
5353                            tree_cons (NULL_TREE, puint_type_node,
5354                                       tree_cons (NULL_TREE, integer_type_node,
5355                                                  endlink)));
5356
5357   tree v2si_ftype_pushort_int
5358     = build_function_type (V2SI_type_node,
5359                            tree_cons (NULL_TREE, pushort_type_node,
5360                                       tree_cons (NULL_TREE, integer_type_node,
5361                                                  endlink)));
5362
5363   /* The initialization of the simple binary and unary builtins is
5364      done in rs6000_common_init_builtins, but we have to enable the
5365      mask bits here manually because we have run out of `target_flags'
5366      bits.  We really need to redesign this mask business.  */
5367
5368   enable_mask_for_builtins ((struct builtin_description *) bdesc_2arg,
5369                             ARRAY_SIZE (bdesc_2arg),
5370                             SPE_BUILTIN_EVADDW,
5371                             SPE_BUILTIN_EVXOR);
5372   enable_mask_for_builtins ((struct builtin_description *) bdesc_1arg,
5373                             ARRAY_SIZE (bdesc_1arg),
5374                             SPE_BUILTIN_EVABS,
5375                             SPE_BUILTIN_EVSUBFUSIAAW);
5376   enable_mask_for_builtins ((struct builtin_description *) bdesc_spe_predicates,
5377                             ARRAY_SIZE (bdesc_spe_predicates),
5378                             SPE_BUILTIN_EVCMPEQ,
5379                             SPE_BUILTIN_EVFSTSTLT);
5380   enable_mask_for_builtins ((struct builtin_description *) bdesc_spe_evsel,
5381                             ARRAY_SIZE (bdesc_spe_evsel),
5382                             SPE_BUILTIN_EVSEL_CMPGTS,
5383                             SPE_BUILTIN_EVSEL_FSTSTEQ);
5384
5385   /* Initialize irregular SPE builtins.  */
5386   
5387   def_builtin (target_flags, "__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
5388   def_builtin (target_flags, "__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
5389   def_builtin (target_flags, "__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
5390   def_builtin (target_flags, "__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
5391   def_builtin (target_flags, "__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
5392   def_builtin (target_flags, "__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
5393   def_builtin (target_flags, "__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
5394   def_builtin (target_flags, "__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
5395   def_builtin (target_flags, "__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
5396   def_builtin (target_flags, "__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
5397   def_builtin (target_flags, "__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
5398   def_builtin (target_flags, "__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
5399   def_builtin (target_flags, "__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
5400   def_builtin (target_flags, "__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
5401   def_builtin (target_flags, "__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
5402   def_builtin (target_flags, "__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
5403
5404   /* Loads.  */
5405   def_builtin (target_flags, "__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
5406   def_builtin (target_flags, "__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
5407   def_builtin (target_flags, "__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
5408   def_builtin (target_flags, "__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
5409   def_builtin (target_flags, "__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
5410   def_builtin (target_flags, "__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
5411   def_builtin (target_flags, "__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
5412   def_builtin (target_flags, "__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
5413   def_builtin (target_flags, "__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
5414   def_builtin (target_flags, "__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
5415   def_builtin (target_flags, "__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
5416   def_builtin (target_flags, "__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
5417   def_builtin (target_flags, "__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
5418   def_builtin (target_flags, "__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
5419   def_builtin (target_flags, "__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
5420   def_builtin (target_flags, "__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
5421   def_builtin (target_flags, "__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
5422   def_builtin (target_flags, "__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
5423   def_builtin (target_flags, "__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
5424   def_builtin (target_flags, "__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
5425   def_builtin (target_flags, "__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
5426   def_builtin (target_flags, "__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
5427
5428   /* Predicates.  */
5429   d = (struct builtin_description *) bdesc_spe_predicates;
5430   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
5431     {
5432       tree type;
5433
5434       switch (insn_data[d->icode].operand[1].mode)
5435         {
5436         case V2SImode:
5437           type = int_ftype_int_v2si_v2si;
5438           break;
5439         case V2SFmode:
5440           type = int_ftype_int_v2sf_v2sf;
5441           break;
5442         default:
5443           abort ();
5444         }
5445
5446       def_builtin (d->mask, d->name, type, d->code);
5447     }
5448
5449   /* Evsel predicates.  */
5450   d = (struct builtin_description *) bdesc_spe_evsel;
5451   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
5452     {
5453       tree type;
5454
5455       switch (insn_data[d->icode].operand[1].mode)
5456         {
5457         case V2SImode:
5458           type = v2si_ftype_4_v2si;
5459           break;
5460         case V2SFmode:
5461           type = v2sf_ftype_4_v2sf;
5462           break;
5463         default:
5464           abort ();
5465         }
5466
5467       def_builtin (d->mask, d->name, type, d->code);
5468     }
5469 }
5470
5471 static void
5472 altivec_init_builtins ()
5473 {
5474   struct builtin_description *d;
5475   struct builtin_description_predicates *dp;
5476   size_t i;
5477   tree pfloat_type_node = build_pointer_type (float_type_node);
5478   tree pint_type_node = build_pointer_type (integer_type_node);
5479   tree pshort_type_node = build_pointer_type (short_integer_type_node);
5480   tree pchar_type_node = build_pointer_type (char_type_node);
5481
5482   tree pvoid_type_node = build_pointer_type (void_type_node);
5483
5484   tree pcfloat_type_node = build_pointer_type (build_qualified_type (float_type_node, TYPE_QUAL_CONST));
5485   tree pcint_type_node = build_pointer_type (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
5486   tree pcshort_type_node = build_pointer_type (build_qualified_type (short_integer_type_node, TYPE_QUAL_CONST));
5487   tree pcchar_type_node = build_pointer_type (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
5488
5489   tree pcvoid_type_node = build_pointer_type (build_qualified_type (void_type_node, TYPE_QUAL_CONST));
5490
5491   tree int_ftype_int_v4si_v4si
5492     = build_function_type_list (integer_type_node,
5493                                 integer_type_node, V4SI_type_node,
5494                                 V4SI_type_node, NULL_TREE);
5495   tree v4sf_ftype_pcfloat
5496     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
5497   tree void_ftype_pfloat_v4sf
5498     = build_function_type_list (void_type_node,
5499                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
5500   tree v4si_ftype_pcint
5501     = build_function_type_list (V4SI_type_node, pcint_type_node, NULL_TREE);
5502   tree void_ftype_pint_v4si
5503     = build_function_type_list (void_type_node,
5504                                 pint_type_node, V4SI_type_node, NULL_TREE);
5505   tree v8hi_ftype_pcshort
5506     = build_function_type_list (V8HI_type_node, pcshort_type_node, NULL_TREE);
5507   tree void_ftype_pshort_v8hi
5508     = build_function_type_list (void_type_node,
5509                                 pshort_type_node, V8HI_type_node, NULL_TREE);
5510   tree v16qi_ftype_pcchar
5511     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
5512   tree void_ftype_pchar_v16qi
5513     = build_function_type_list (void_type_node,
5514                                 pchar_type_node, V16QI_type_node, NULL_TREE);
5515   tree void_ftype_v4si
5516     = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
5517   tree v8hi_ftype_void
5518     = build_function_type (V8HI_type_node, void_list_node);
5519   tree void_ftype_void
5520     = build_function_type (void_type_node, void_list_node);
5521   tree void_ftype_qi
5522     = build_function_type_list (void_type_node, char_type_node, NULL_TREE);
5523
5524   tree v16qi_ftype_int_pcvoid
5525     = build_function_type_list (V16QI_type_node,
5526                                 integer_type_node, pcvoid_type_node, NULL_TREE);
5527   tree v8hi_ftype_int_pcvoid
5528     = build_function_type_list (V8HI_type_node,
5529                                 integer_type_node, pcvoid_type_node, NULL_TREE);
5530   tree v4si_ftype_int_pcvoid
5531     = build_function_type_list (V4SI_type_node,
5532                                 integer_type_node, pcvoid_type_node, NULL_TREE);
5533
5534   tree void_ftype_v4si_int_pvoid
5535     = build_function_type_list (void_type_node,
5536                                 V4SI_type_node, integer_type_node,
5537                                 pvoid_type_node, NULL_TREE);
5538   tree void_ftype_v16qi_int_pvoid
5539     = build_function_type_list (void_type_node,
5540                                 V16QI_type_node, integer_type_node,
5541                                 pvoid_type_node, NULL_TREE);
5542   tree void_ftype_v8hi_int_pvoid
5543     = build_function_type_list (void_type_node,
5544                                 V8HI_type_node, integer_type_node,
5545                                 pvoid_type_node, NULL_TREE);
5546   tree int_ftype_int_v8hi_v8hi
5547     = build_function_type_list (integer_type_node,
5548                                 integer_type_node, V8HI_type_node,
5549                                 V8HI_type_node, NULL_TREE);
5550   tree int_ftype_int_v16qi_v16qi
5551     = build_function_type_list (integer_type_node,
5552                                 integer_type_node, V16QI_type_node,
5553                                 V16QI_type_node, NULL_TREE);
5554   tree int_ftype_int_v4sf_v4sf
5555     = build_function_type_list (integer_type_node,
5556                                 integer_type_node, V4SF_type_node,
5557                                 V4SF_type_node, NULL_TREE);
5558   tree v4si_ftype_v4si
5559     = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
5560   tree v8hi_ftype_v8hi
5561     = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
5562   tree v16qi_ftype_v16qi
5563     = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
5564   tree v4sf_ftype_v4sf
5565     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
5566   tree void_ftype_pcvoid_int_char
5567     = build_function_type_list (void_type_node,
5568                                 pcvoid_type_node, integer_type_node,
5569                                 char_type_node, NULL_TREE);
5570   
5571   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4sf", v4sf_ftype_pcfloat,
5572                ALTIVEC_BUILTIN_LD_INTERNAL_4sf);
5573   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4sf", void_ftype_pfloat_v4sf,
5574                ALTIVEC_BUILTIN_ST_INTERNAL_4sf);
5575   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4si", v4si_ftype_pcint,
5576                ALTIVEC_BUILTIN_LD_INTERNAL_4si);
5577   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4si", void_ftype_pint_v4si,
5578                ALTIVEC_BUILTIN_ST_INTERNAL_4si);
5579   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_8hi", v8hi_ftype_pcshort,
5580                ALTIVEC_BUILTIN_LD_INTERNAL_8hi);
5581   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_8hi", void_ftype_pshort_v8hi,
5582                ALTIVEC_BUILTIN_ST_INTERNAL_8hi);
5583   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_16qi", v16qi_ftype_pcchar,
5584                ALTIVEC_BUILTIN_LD_INTERNAL_16qi);
5585   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_16qi", void_ftype_pchar_v16qi,
5586                ALTIVEC_BUILTIN_ST_INTERNAL_16qi);
5587   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
5588   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
5589   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
5590   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dss", void_ftype_qi, ALTIVEC_BUILTIN_DSS);
5591   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsl", v16qi_ftype_int_pcvoid, ALTIVEC_BUILTIN_LVSL);
5592   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsr", v16qi_ftype_int_pcvoid, ALTIVEC_BUILTIN_LVSR);
5593   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvebx", v16qi_ftype_int_pcvoid, ALTIVEC_BUILTIN_LVEBX);
5594   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvehx", v8hi_ftype_int_pcvoid, ALTIVEC_BUILTIN_LVEHX);
5595   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvewx", v4si_ftype_int_pcvoid, ALTIVEC_BUILTIN_LVEWX);
5596   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvxl", v4si_ftype_int_pcvoid, ALTIVEC_BUILTIN_LVXL);
5597   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvx", v4si_ftype_int_pcvoid, ALTIVEC_BUILTIN_LVX);
5598   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvx", void_ftype_v4si_int_pvoid, ALTIVEC_BUILTIN_STVX);
5599   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvewx", void_ftype_v4si_int_pvoid, ALTIVEC_BUILTIN_STVEWX);
5600   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvxl", void_ftype_v4si_int_pvoid, ALTIVEC_BUILTIN_STVXL);
5601   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvebx", void_ftype_v16qi_int_pvoid, ALTIVEC_BUILTIN_STVEBX);
5602   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvehx", void_ftype_v8hi_int_pvoid, ALTIVEC_BUILTIN_STVEHX);
5603
5604   /* Add the DST variants.  */
5605   d = (struct builtin_description *) bdesc_dst;
5606   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
5607     def_builtin (d->mask, d->name, void_ftype_pcvoid_int_char, d->code);
5608
5609   /* Initialize the predicates.  */
5610   dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
5611   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
5612     {
5613       enum machine_mode mode1;
5614       tree type;
5615
5616       mode1 = insn_data[dp->icode].operand[1].mode;
5617
5618       switch (mode1)
5619         {
5620         case V4SImode:
5621           type = int_ftype_int_v4si_v4si;
5622           break;
5623         case V8HImode:
5624           type = int_ftype_int_v8hi_v8hi;
5625           break;
5626         case V16QImode:
5627           type = int_ftype_int_v16qi_v16qi;
5628           break;
5629         case V4SFmode:
5630           type = int_ftype_int_v4sf_v4sf;
5631           break;
5632         default:
5633           abort ();
5634         }
5635       
5636       def_builtin (dp->mask, dp->name, type, dp->code);
5637     }
5638
5639   /* Initialize the abs* operators.  */
5640   d = (struct builtin_description *) bdesc_abs;
5641   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
5642     {
5643       enum machine_mode mode0;
5644       tree type;
5645
5646       mode0 = insn_data[d->icode].operand[0].mode;
5647
5648       switch (mode0)
5649         {
5650         case V4SImode:
5651           type = v4si_ftype_v4si;
5652           break;
5653         case V8HImode:
5654           type = v8hi_ftype_v8hi;
5655           break;
5656         case V16QImode:
5657           type = v16qi_ftype_v16qi;
5658           break;
5659         case V4SFmode:
5660           type = v4sf_ftype_v4sf;
5661           break;
5662         default:
5663           abort ();
5664         }
5665       
5666       def_builtin (d->mask, d->name, type, d->code);
5667     }
5668 }
5669
5670 static void
5671 rs6000_common_init_builtins ()
5672 {
5673   struct builtin_description *d;
5674   size_t i;
5675
5676   tree v4sf_ftype_v4sf_v4sf_v16qi
5677     = build_function_type_list (V4SF_type_node,
5678                                 V4SF_type_node, V4SF_type_node,
5679                                 V16QI_type_node, NULL_TREE);
5680   tree v4si_ftype_v4si_v4si_v16qi
5681     = build_function_type_list (V4SI_type_node,
5682                                 V4SI_type_node, V4SI_type_node,
5683                                 V16QI_type_node, NULL_TREE);
5684   tree v8hi_ftype_v8hi_v8hi_v16qi
5685     = build_function_type_list (V8HI_type_node,
5686                                 V8HI_type_node, V8HI_type_node,
5687                                 V16QI_type_node, NULL_TREE);
5688   tree v16qi_ftype_v16qi_v16qi_v16qi
5689     = build_function_type_list (V16QI_type_node,
5690                                 V16QI_type_node, V16QI_type_node,
5691                                 V16QI_type_node, NULL_TREE);
5692   tree v4si_ftype_char
5693     = build_function_type_list (V4SI_type_node, char_type_node, NULL_TREE);
5694   tree v8hi_ftype_char
5695     = build_function_type_list (V8HI_type_node, char_type_node, NULL_TREE);
5696   tree v16qi_ftype_char
5697     = build_function_type_list (V16QI_type_node, char_type_node, NULL_TREE);
5698   tree v8hi_ftype_v16qi
5699     = build_function_type_list (V8HI_type_node, V16QI_type_node, NULL_TREE);
5700   tree v4sf_ftype_v4sf
5701     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
5702
5703   tree v2si_ftype_v2si_v2si
5704     = build_function_type_list (V2SI_type_node,
5705                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
5706
5707   tree v2sf_ftype_v2sf_v2sf
5708     = build_function_type_list (V2SF_type_node,
5709                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
5710
5711   tree v2si_ftype_int_int
5712     = build_function_type_list (V2SI_type_node,
5713                                 integer_type_node, integer_type_node,
5714                                 NULL_TREE);
5715
5716   tree v2si_ftype_v2si
5717     = build_function_type_list (V2SI_type_node, V2SI_type_node, NULL_TREE);
5718
5719   tree v2sf_ftype_v2sf
5720     = build_function_type_list (V2SF_type_node,
5721                                 V2SF_type_node, NULL_TREE);
5722   
5723   tree v2sf_ftype_v2si
5724     = build_function_type_list (V2SF_type_node,
5725                                 V2SI_type_node, NULL_TREE);
5726
5727   tree v2si_ftype_v2sf
5728     = build_function_type_list (V2SI_type_node,
5729                                 V2SF_type_node, NULL_TREE);
5730
5731   tree v2si_ftype_v2si_char
5732     = build_function_type_list (V2SI_type_node,
5733                                 V2SI_type_node, char_type_node, NULL_TREE);
5734
5735   tree v2si_ftype_int_char
5736     = build_function_type_list (V2SI_type_node,
5737                                 integer_type_node, char_type_node, NULL_TREE);
5738
5739   tree v2si_ftype_char
5740     = build_function_type_list (V2SI_type_node, char_type_node, NULL_TREE);
5741
5742   tree int_ftype_int_int
5743     = build_function_type_list (integer_type_node,
5744                                 integer_type_node, integer_type_node,
5745                                 NULL_TREE);
5746
5747   tree v4si_ftype_v4si_v4si
5748     = build_function_type_list (V4SI_type_node,
5749                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
5750   tree v4sf_ftype_v4si_char
5751     = build_function_type_list (V4SF_type_node,
5752                                 V4SI_type_node, char_type_node, NULL_TREE);
5753   tree v4si_ftype_v4sf_char
5754     = build_function_type_list (V4SI_type_node,
5755                                 V4SF_type_node, char_type_node, NULL_TREE);
5756   tree v4si_ftype_v4si_char
5757     = build_function_type_list (V4SI_type_node,
5758                                 V4SI_type_node, char_type_node, NULL_TREE);
5759   tree v8hi_ftype_v8hi_char
5760     = build_function_type_list (V8HI_type_node,
5761                                 V8HI_type_node, char_type_node, NULL_TREE);
5762   tree v16qi_ftype_v16qi_char
5763     = build_function_type_list (V16QI_type_node,
5764                                 V16QI_type_node, char_type_node, NULL_TREE);
5765   tree v16qi_ftype_v16qi_v16qi_char
5766     = build_function_type_list (V16QI_type_node,
5767                                 V16QI_type_node, V16QI_type_node,
5768                                 char_type_node, NULL_TREE);
5769   tree v8hi_ftype_v8hi_v8hi_char
5770     = build_function_type_list (V8HI_type_node,
5771                                 V8HI_type_node, V8HI_type_node,
5772                                 char_type_node, NULL_TREE);
5773   tree v4si_ftype_v4si_v4si_char
5774     = build_function_type_list (V4SI_type_node,
5775                                 V4SI_type_node, V4SI_type_node,
5776                                 char_type_node, NULL_TREE);
5777   tree v4sf_ftype_v4sf_v4sf_char
5778     = build_function_type_list (V4SF_type_node,
5779                                 V4SF_type_node, V4SF_type_node,
5780                                 char_type_node, NULL_TREE);
5781   tree v4sf_ftype_v4sf_v4sf
5782     = build_function_type_list (V4SF_type_node,
5783                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
5784   tree v4sf_ftype_v4sf_v4sf_v4si
5785     = build_function_type_list (V4SF_type_node,
5786                                 V4SF_type_node, V4SF_type_node,
5787                                 V4SI_type_node, NULL_TREE);
5788   tree v4sf_ftype_v4sf_v4sf_v4sf
5789     = build_function_type_list (V4SF_type_node,
5790                                 V4SF_type_node, V4SF_type_node,
5791                                 V4SF_type_node, NULL_TREE);
5792   tree v4si_ftype_v4si_v4si_v4si 
5793     = build_function_type_list (V4SI_type_node,
5794                                 V4SI_type_node, V4SI_type_node,
5795                                 V4SI_type_node, NULL_TREE);
5796   tree v8hi_ftype_v8hi_v8hi
5797     = build_function_type_list (V8HI_type_node,
5798                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
5799   tree v8hi_ftype_v8hi_v8hi_v8hi
5800     = build_function_type_list (V8HI_type_node,
5801                                 V8HI_type_node, V8HI_type_node,
5802                                 V8HI_type_node, NULL_TREE);
5803  tree v4si_ftype_v8hi_v8hi_v4si
5804     = build_function_type_list (V4SI_type_node,
5805                                 V8HI_type_node, V8HI_type_node,
5806                                 V4SI_type_node, NULL_TREE);
5807  tree v4si_ftype_v16qi_v16qi_v4si
5808     = build_function_type_list (V4SI_type_node,
5809                                 V16QI_type_node, V16QI_type_node,
5810                                 V4SI_type_node, NULL_TREE);
5811   tree v16qi_ftype_v16qi_v16qi
5812     = build_function_type_list (V16QI_type_node,
5813                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
5814   tree v4si_ftype_v4sf_v4sf
5815     = build_function_type_list (V4SI_type_node,
5816                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
5817   tree v8hi_ftype_v16qi_v16qi
5818     = build_function_type_list (V8HI_type_node,
5819                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
5820   tree v4si_ftype_v8hi_v8hi
5821     = build_function_type_list (V4SI_type_node,
5822                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
5823   tree v8hi_ftype_v4si_v4si
5824     = build_function_type_list (V8HI_type_node,
5825                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
5826   tree v16qi_ftype_v8hi_v8hi
5827     = build_function_type_list (V16QI_type_node,
5828                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
5829   tree v4si_ftype_v16qi_v4si
5830     = build_function_type_list (V4SI_type_node,
5831                                 V16QI_type_node, V4SI_type_node, NULL_TREE);
5832   tree v4si_ftype_v16qi_v16qi
5833     = build_function_type_list (V4SI_type_node,
5834                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
5835   tree v4si_ftype_v8hi_v4si
5836     = build_function_type_list (V4SI_type_node,
5837                                 V8HI_type_node, V4SI_type_node, NULL_TREE);
5838   tree v4si_ftype_v8hi
5839     = build_function_type_list (V4SI_type_node, V8HI_type_node, NULL_TREE);
5840   tree int_ftype_v4si_v4si
5841     = build_function_type_list (integer_type_node,
5842                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
5843   tree int_ftype_v4sf_v4sf
5844     = build_function_type_list (integer_type_node,
5845                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
5846   tree int_ftype_v16qi_v16qi
5847     = build_function_type_list (integer_type_node,
5848                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
5849   tree int_ftype_v8hi_v8hi
5850     = build_function_type_list (integer_type_node,
5851                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
5852
5853   /* Add the simple ternary operators.  */
5854   d = (struct builtin_description *) bdesc_3arg;
5855   for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
5856     {
5857       
5858       enum machine_mode mode0, mode1, mode2, mode3;
5859       tree type;
5860
5861       if (d->name == 0 || d->icode == CODE_FOR_nothing)
5862         continue;
5863       
5864       mode0 = insn_data[d->icode].operand[0].mode;
5865       mode1 = insn_data[d->icode].operand[1].mode;
5866       mode2 = insn_data[d->icode].operand[2].mode;
5867       mode3 = insn_data[d->icode].operand[3].mode;
5868       
5869       /* When all four are of the same mode.  */
5870       if (mode0 == mode1 && mode1 == mode2 && mode2 == mode3)
5871         {
5872           switch (mode0)
5873             {
5874             case V4SImode:
5875               type = v4si_ftype_v4si_v4si_v4si;
5876               break;
5877             case V4SFmode:
5878               type = v4sf_ftype_v4sf_v4sf_v4sf;
5879               break;
5880             case V8HImode:
5881               type = v8hi_ftype_v8hi_v8hi_v8hi;
5882               break;          
5883             case V16QImode:
5884               type = v16qi_ftype_v16qi_v16qi_v16qi;
5885               break;          
5886             default:
5887               abort();        
5888             }
5889         }
5890       else if (mode0 == mode1 && mode1 == mode2 && mode3 == V16QImode)
5891         {
5892           switch (mode0)
5893             {
5894             case V4SImode:
5895               type = v4si_ftype_v4si_v4si_v16qi;
5896               break;
5897             case V4SFmode:
5898               type = v4sf_ftype_v4sf_v4sf_v16qi;
5899               break;
5900             case V8HImode:
5901               type = v8hi_ftype_v8hi_v8hi_v16qi;
5902               break;          
5903             case V16QImode:
5904               type = v16qi_ftype_v16qi_v16qi_v16qi;
5905               break;          
5906             default:
5907               abort();        
5908             }
5909         }
5910       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode 
5911                && mode3 == V4SImode)
5912         type = v4si_ftype_v16qi_v16qi_v4si;
5913       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode 
5914                && mode3 == V4SImode)
5915         type = v4si_ftype_v8hi_v8hi_v4si;
5916       else if (mode0 == V4SFmode && mode1 == V4SFmode && mode2 == V4SFmode 
5917                && mode3 == V4SImode)
5918         type = v4sf_ftype_v4sf_v4sf_v4si;
5919
5920       /* vchar, vchar, vchar, 4 bit literal.  */
5921       else if (mode0 == V16QImode && mode1 == mode0 && mode2 == mode0
5922                && mode3 == QImode)
5923         type = v16qi_ftype_v16qi_v16qi_char;
5924
5925       /* vshort, vshort, vshort, 4 bit literal.  */
5926       else if (mode0 == V8HImode && mode1 == mode0 && mode2 == mode0
5927                && mode3 == QImode)
5928         type = v8hi_ftype_v8hi_v8hi_char;
5929
5930       /* vint, vint, vint, 4 bit literal.  */
5931       else if (mode0 == V4SImode && mode1 == mode0 && mode2 == mode0
5932                && mode3 == QImode)
5933         type = v4si_ftype_v4si_v4si_char;
5934
5935       /* vfloat, vfloat, vfloat, 4 bit literal.  */
5936       else if (mode0 == V4SFmode && mode1 == mode0 && mode2 == mode0
5937                && mode3 == QImode)
5938         type = v4sf_ftype_v4sf_v4sf_char;
5939
5940       else
5941         abort ();
5942
5943       def_builtin (d->mask, d->name, type, d->code);
5944     }
5945
5946   /* Add the simple binary operators.  */
5947   d = (struct builtin_description *) bdesc_2arg;
5948   for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
5949     {
5950       enum machine_mode mode0, mode1, mode2;
5951       tree type;
5952
5953       if (d->name == 0 || d->icode == CODE_FOR_nothing)
5954         continue;
5955       
5956       mode0 = insn_data[d->icode].operand[0].mode;
5957       mode1 = insn_data[d->icode].operand[1].mode;
5958       mode2 = insn_data[d->icode].operand[2].mode;
5959
5960       /* When all three operands are of the same mode.  */
5961       if (mode0 == mode1 && mode1 == mode2)
5962         {
5963           switch (mode0)
5964             {
5965             case V4SFmode:
5966               type = v4sf_ftype_v4sf_v4sf;
5967               break;
5968             case V4SImode:
5969               type = v4si_ftype_v4si_v4si;
5970               break;
5971             case V16QImode:
5972               type = v16qi_ftype_v16qi_v16qi;
5973               break;
5974             case V8HImode:
5975               type = v8hi_ftype_v8hi_v8hi;
5976               break;
5977             case V2SImode:
5978               type = v2si_ftype_v2si_v2si;
5979               break;
5980             case V2SFmode:
5981               type = v2sf_ftype_v2sf_v2sf;
5982               break;
5983             case SImode:
5984               type = int_ftype_int_int;
5985               break;
5986             default:
5987               abort ();
5988             }
5989         }
5990
5991       /* A few other combos we really don't want to do manually.  */
5992
5993       /* vint, vfloat, vfloat.  */
5994       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == V4SFmode)
5995         type = v4si_ftype_v4sf_v4sf;
5996
5997       /* vshort, vchar, vchar.  */
5998       else if (mode0 == V8HImode && mode1 == V16QImode && mode2 == V16QImode)
5999         type = v8hi_ftype_v16qi_v16qi;
6000
6001       /* vint, vshort, vshort.  */
6002       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode)
6003         type = v4si_ftype_v8hi_v8hi;
6004
6005       /* vshort, vint, vint.  */
6006       else if (mode0 == V8HImode && mode1 == V4SImode && mode2 == V4SImode)
6007         type = v8hi_ftype_v4si_v4si;
6008
6009       /* vchar, vshort, vshort.  */
6010       else if (mode0 == V16QImode && mode1 == V8HImode && mode2 == V8HImode)
6011         type = v16qi_ftype_v8hi_v8hi;
6012
6013       /* vint, vchar, vint.  */
6014       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V4SImode)
6015         type = v4si_ftype_v16qi_v4si;
6016
6017       /* vint, vchar, vchar.  */
6018       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode)
6019         type = v4si_ftype_v16qi_v16qi;
6020
6021       /* vint, vshort, vint.  */
6022       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V4SImode)
6023         type = v4si_ftype_v8hi_v4si;
6024       
6025       /* vint, vint, 5 bit literal.  */
6026       else if (mode0 == V4SImode && mode1 == V4SImode && mode2 == QImode)
6027         type = v4si_ftype_v4si_char;
6028       
6029       /* vshort, vshort, 5 bit literal.  */
6030       else if (mode0 == V8HImode && mode1 == V8HImode && mode2 == QImode)
6031         type = v8hi_ftype_v8hi_char;
6032       
6033       /* vchar, vchar, 5 bit literal.  */
6034       else if (mode0 == V16QImode && mode1 == V16QImode && mode2 == QImode)
6035         type = v16qi_ftype_v16qi_char;
6036
6037       /* vfloat, vint, 5 bit literal.  */
6038       else if (mode0 == V4SFmode && mode1 == V4SImode && mode2 == QImode)
6039         type = v4sf_ftype_v4si_char;
6040       
6041       /* vint, vfloat, 5 bit literal.  */
6042       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == QImode)
6043         type = v4si_ftype_v4sf_char;
6044
6045       else if (mode0 == V2SImode && mode1 == SImode && mode2 == SImode)
6046         type = v2si_ftype_int_int;
6047
6048       else if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
6049         type = v2si_ftype_v2si_char;
6050
6051       else if (mode0 == V2SImode && mode1 == SImode && mode2 == QImode)
6052         type = v2si_ftype_int_char;
6053
6054       /* int, x, x.  */
6055       else if (mode0 == SImode)
6056         {
6057           switch (mode1)
6058             {
6059             case V4SImode:
6060               type = int_ftype_v4si_v4si;
6061               break;
6062             case V4SFmode:
6063               type = int_ftype_v4sf_v4sf;
6064               break;
6065             case V16QImode:
6066               type = int_ftype_v16qi_v16qi;
6067               break;
6068             case V8HImode:
6069               type = int_ftype_v8hi_v8hi;
6070               break;
6071             default:
6072               abort ();
6073             }
6074         }
6075
6076       else
6077         abort ();
6078
6079       def_builtin (d->mask, d->name, type, d->code);
6080     }
6081
6082   /* Add the simple unary operators.  */
6083   d = (struct builtin_description *) bdesc_1arg;
6084   for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
6085     {
6086       enum machine_mode mode0, mode1;
6087       tree type;
6088
6089       if (d->name == 0 || d->icode == CODE_FOR_nothing)
6090         continue;
6091       
6092       mode0 = insn_data[d->icode].operand[0].mode;
6093       mode1 = insn_data[d->icode].operand[1].mode;
6094
6095       if (mode0 == V4SImode && mode1 == QImode)
6096         type = v4si_ftype_char;
6097       else if (mode0 == V8HImode && mode1 == QImode)
6098         type = v8hi_ftype_char;
6099       else if (mode0 == V16QImode && mode1 == QImode)
6100         type = v16qi_ftype_char;
6101       else if (mode0 == V4SFmode && mode1 == V4SFmode)
6102         type = v4sf_ftype_v4sf;
6103       else if (mode0 == V8HImode && mode1 == V16QImode)
6104         type = v8hi_ftype_v16qi;
6105       else if (mode0 == V4SImode && mode1 == V8HImode)
6106         type = v4si_ftype_v8hi;
6107       else if (mode0 == V2SImode && mode1 == V2SImode)
6108         type = v2si_ftype_v2si;
6109       else if (mode0 == V2SFmode && mode1 == V2SFmode)
6110         type = v2sf_ftype_v2sf;
6111       else if (mode0 == V2SFmode && mode1 == V2SImode)
6112         type = v2sf_ftype_v2si;
6113       else if (mode0 == V2SImode && mode1 == V2SFmode)
6114         type = v2si_ftype_v2sf;
6115       else if (mode0 == V2SImode && mode1 == QImode)
6116         type = v2si_ftype_char;
6117       else
6118         abort ();
6119
6120       def_builtin (d->mask, d->name, type, d->code);
6121     }
6122 }
6123
6124 \f
6125 /* Expand a block move operation, and return 1 if successful.  Return 0
6126    if we should let the compiler generate normal code.
6127
6128    operands[0] is the destination
6129    operands[1] is the source
6130    operands[2] is the length
6131    operands[3] is the alignment */
6132
6133 #define MAX_MOVE_REG 4
6134
6135 int
6136 expand_block_move (operands)
6137      rtx operands[];
6138 {
6139   rtx orig_dest = operands[0];
6140   rtx orig_src  = operands[1];
6141   rtx bytes_rtx = operands[2];
6142   rtx align_rtx = operands[3];
6143   int constp    = (GET_CODE (bytes_rtx) == CONST_INT);
6144   int align;
6145   int bytes;
6146   int offset;
6147   int move_bytes;
6148
6149   /* If this is not a fixed size move, just call memcpy */
6150   if (! constp)
6151     return 0;
6152
6153   /* If this is not a fixed size alignment, abort */
6154   if (GET_CODE (align_rtx) != CONST_INT)
6155     abort ();
6156   align = INTVAL (align_rtx);
6157
6158   /* Anything to move? */
6159   bytes = INTVAL (bytes_rtx);
6160   if (bytes <= 0)
6161     return 1;
6162
6163   /* store_one_arg depends on expand_block_move to handle at least the size of
6164      reg_parm_stack_space.  */  
6165   if (bytes > (TARGET_POWERPC64 ? 64 : 32))
6166     return 0;
6167
6168   if (TARGET_STRING)    /* string instructions are available */
6169     {
6170       for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
6171         {
6172           union {
6173             rtx (*movstrsi) PARAMS ((rtx, rtx, rtx, rtx));
6174             rtx (*mov) PARAMS ((rtx, rtx));
6175           } gen_func;
6176           enum machine_mode mode = BLKmode;
6177           rtx src, dest;
6178
6179           if (bytes > 24                /* move up to 32 bytes at a time */
6180               && ! fixed_regs[5]
6181               && ! fixed_regs[6]
6182               && ! fixed_regs[7]
6183               && ! fixed_regs[8]
6184               && ! fixed_regs[9]
6185               && ! fixed_regs[10]
6186               && ! fixed_regs[11]
6187               && ! fixed_regs[12])
6188             {
6189               move_bytes = (bytes > 32) ? 32 : bytes;
6190               gen_func.movstrsi = gen_movstrsi_8reg;
6191             }
6192           else if (bytes > 16   /* move up to 24 bytes at a time */
6193                    && ! fixed_regs[5]
6194                    && ! fixed_regs[6]
6195                    && ! fixed_regs[7]
6196                    && ! fixed_regs[8]
6197                    && ! fixed_regs[9]
6198                    && ! fixed_regs[10])
6199             {
6200               move_bytes = (bytes > 24) ? 24 : bytes;
6201               gen_func.movstrsi = gen_movstrsi_6reg;
6202             }
6203           else if (bytes > 8    /* move up to 16 bytes at a time */
6204                    && ! fixed_regs[5]
6205                    && ! fixed_regs[6]
6206                    && ! fixed_regs[7]
6207                    && ! fixed_regs[8])
6208             {
6209               move_bytes = (bytes > 16) ? 16 : bytes;
6210               gen_func.movstrsi = gen_movstrsi_4reg;
6211             }
6212           else if (bytes >= 8 && TARGET_POWERPC64
6213                    /* 64-bit loads and stores require word-aligned
6214                       displacements.  */
6215                    && (align >= 8 || (! STRICT_ALIGNMENT && align >= 4)))
6216             {
6217               move_bytes = 8;
6218               mode = DImode;
6219               gen_func.mov = gen_movdi;
6220             }
6221           else if (bytes > 4 && !TARGET_POWERPC64)
6222             {                   /* move up to 8 bytes at a time */
6223               move_bytes = (bytes > 8) ? 8 : bytes;
6224               gen_func.movstrsi = gen_movstrsi_2reg;
6225             }
6226           else if (bytes >= 4 && (align >= 4 || ! STRICT_ALIGNMENT))
6227             {                   /* move 4 bytes */
6228               move_bytes = 4;
6229               mode = SImode;
6230               gen_func.mov = gen_movsi;
6231             }
6232           else if (bytes == 2 && (align >= 2 || ! STRICT_ALIGNMENT))
6233             {                   /* move 2 bytes */
6234               move_bytes = 2;
6235               mode = HImode;
6236               gen_func.mov = gen_movhi;
6237             }
6238           else if (bytes == 1)  /* move 1 byte */
6239             {
6240               move_bytes = 1;
6241               mode = QImode;
6242               gen_func.mov = gen_movqi;
6243             }
6244           else
6245             {                   /* move up to 4 bytes at a time */
6246               move_bytes = (bytes > 4) ? 4 : bytes;
6247               gen_func.movstrsi = gen_movstrsi_1reg;
6248             }
6249
6250           src = adjust_address (orig_src, mode, offset);
6251           dest = adjust_address (orig_dest, mode, offset);
6252
6253           if (mode == BLKmode)
6254             {
6255               /* Move the address into scratch registers.  The movstrsi
6256                  patterns require zero offset.  */
6257               if (!REG_P (XEXP (src, 0)))
6258                 {
6259                   rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
6260                   src = replace_equiv_address (src, src_reg);
6261                 }
6262               set_mem_size (src, GEN_INT (move_bytes));
6263
6264               if (!REG_P (XEXP (dest, 0)))
6265                 {
6266                   rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
6267                   dest = replace_equiv_address (dest, dest_reg);
6268                 }
6269               set_mem_size (dest, GEN_INT (move_bytes));
6270
6271               emit_insn ((*gen_func.movstrsi) (dest, src,
6272                                                GEN_INT (move_bytes & 31),
6273                                                align_rtx));
6274             }
6275           else
6276             {
6277               rtx tmp_reg = gen_reg_rtx (mode);
6278
6279               emit_insn ((*gen_func.mov) (tmp_reg, src));
6280               emit_insn ((*gen_func.mov) (dest, tmp_reg));
6281             }
6282         }
6283     }
6284
6285   else                  /* string instructions not available */
6286     {
6287       rtx stores[MAX_MOVE_REG];
6288       int num_reg = 0;
6289       int i;
6290
6291       for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
6292         {
6293           rtx (*gen_mov_func) PARAMS ((rtx, rtx));
6294           enum machine_mode mode;
6295           rtx src, dest, tmp_reg;
6296
6297           /* Generate the appropriate load and store, saving the stores
6298              for later.  */
6299           if (bytes >= 8 && TARGET_POWERPC64
6300               /* 64-bit loads and stores require word-aligned
6301                  displacements.  */
6302               && (align >= 8 || (! STRICT_ALIGNMENT && align >= 4)))
6303             {
6304               move_bytes = 8;
6305               mode = DImode;
6306               gen_mov_func = gen_movdi;
6307             }
6308           else if (bytes >= 4 && (align >= 4 || ! STRICT_ALIGNMENT))
6309             {
6310               move_bytes = 4;
6311               mode = SImode;
6312               gen_mov_func = gen_movsi;
6313             }
6314           else if (bytes >= 2 && (align >= 2 || ! STRICT_ALIGNMENT))
6315             {
6316               move_bytes = 2;
6317               mode = HImode;
6318               gen_mov_func = gen_movhi;
6319             }
6320           else
6321             {
6322               move_bytes = 1;
6323               mode = QImode;
6324               gen_mov_func = gen_movqi;
6325             }
6326
6327           src = adjust_address (orig_src, mode, offset);
6328           dest = adjust_address (orig_dest, mode, offset);
6329           tmp_reg = gen_reg_rtx (mode);
6330
6331           emit_insn ((*gen_mov_func) (tmp_reg, src));
6332           stores[num_reg++] = (*gen_mov_func) (dest, tmp_reg);
6333
6334           if (num_reg >= MAX_MOVE_REG)
6335             {
6336               for (i = 0; i < num_reg; i++)
6337                 emit_insn (stores[i]);
6338               num_reg = 0;
6339             }
6340         }
6341
6342       for (i = 0; i < num_reg; i++)
6343         emit_insn (stores[i]);
6344     }
6345
6346   return 1;
6347 }
6348
6349 \f
6350 /* Return 1 if OP is a load multiple operation.  It is known to be a
6351    PARALLEL and the first section will be tested.  */
6352
6353 int
6354 load_multiple_operation (op, mode)
6355      rtx op;
6356      enum machine_mode mode ATTRIBUTE_UNUSED;
6357 {
6358   int count = XVECLEN (op, 0);
6359   unsigned int dest_regno;
6360   rtx src_addr;
6361   int i;
6362
6363   /* Perform a quick check so we don't blow up below.  */
6364   if (count <= 1
6365       || GET_CODE (XVECEXP (op, 0, 0)) != SET
6366       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
6367       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
6368     return 0;
6369
6370   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
6371   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
6372
6373   for (i = 1; i < count; i++)
6374     {
6375       rtx elt = XVECEXP (op, 0, i);
6376
6377       if (GET_CODE (elt) != SET
6378           || GET_CODE (SET_DEST (elt)) != REG
6379           || GET_MODE (SET_DEST (elt)) != SImode
6380           || REGNO (SET_DEST (elt)) != dest_regno + i
6381           || GET_CODE (SET_SRC (elt)) != MEM
6382           || GET_MODE (SET_SRC (elt)) != SImode
6383           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
6384           || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
6385           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
6386           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != i * 4)
6387         return 0;
6388     }
6389
6390   return 1;
6391 }
6392
6393 /* Similar, but tests for store multiple.  Here, the second vector element
6394    is a CLOBBER.  It will be tested later.  */
6395
6396 int
6397 store_multiple_operation (op, mode)
6398      rtx op;
6399      enum machine_mode mode ATTRIBUTE_UNUSED;
6400 {
6401   int count = XVECLEN (op, 0) - 1;
6402   unsigned int src_regno;
6403   rtx dest_addr;
6404   int i;
6405
6406   /* Perform a quick check so we don't blow up below.  */
6407   if (count <= 1
6408       || GET_CODE (XVECEXP (op, 0, 0)) != SET
6409       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
6410       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
6411     return 0;
6412
6413   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
6414   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
6415
6416   for (i = 1; i < count; i++)
6417     {
6418       rtx elt = XVECEXP (op, 0, i + 1);
6419
6420       if (GET_CODE (elt) != SET
6421           || GET_CODE (SET_SRC (elt)) != REG
6422           || GET_MODE (SET_SRC (elt)) != SImode
6423           || REGNO (SET_SRC (elt)) != src_regno + i
6424           || GET_CODE (SET_DEST (elt)) != MEM
6425           || GET_MODE (SET_DEST (elt)) != SImode
6426           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
6427           || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
6428           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
6429           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != i * 4)
6430         return 0;
6431     }
6432
6433   return 1;
6434 }
6435
6436 /* Return a string to perform a load_multiple operation.
6437    operands[0] is the vector.
6438    operands[1] is the source address.
6439    operands[2] is the first destination register.  */
6440
6441 const char *
6442 rs6000_output_load_multiple (operands)
6443      rtx operands[3];
6444 {
6445   /* We have to handle the case where the pseudo used to contain the address
6446      is assigned to one of the output registers.  */
6447   int i, j;
6448   int words = XVECLEN (operands[0], 0);
6449   rtx xop[10];
6450
6451   if (XVECLEN (operands[0], 0) == 1)
6452     return "{l|lwz} %2,0(%1)";
6453
6454   for (i = 0; i < words; i++)
6455     if (refers_to_regno_p (REGNO (operands[2]) + i,
6456                            REGNO (operands[2]) + i + 1, operands[1], 0))
6457       {
6458         if (i == words-1)
6459           {
6460             xop[0] = GEN_INT (4 * (words-1));
6461             xop[1] = operands[1];
6462             xop[2] = operands[2];
6463             output_asm_insn ("{lsi|lswi} %2,%1,%0\n\t{l|lwz} %1,%0(%1)", xop);
6464             return "";
6465           }
6466         else if (i == 0)
6467           {
6468             xop[0] = GEN_INT (4 * (words-1));
6469             xop[1] = operands[1];
6470             xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6471             output_asm_insn ("{cal %1,4(%1)|addi %1,%1,4}\n\t{lsi|lswi} %2,%1,%0\n\t{l|lwz} %1,-4(%1)", xop);
6472             return "";
6473           }
6474         else
6475           {
6476             for (j = 0; j < words; j++)
6477               if (j != i)
6478                 {
6479                   xop[0] = GEN_INT (j * 4);
6480                   xop[1] = operands[1];
6481                   xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
6482                   output_asm_insn ("{l|lwz} %2,%0(%1)", xop);
6483                 }
6484             xop[0] = GEN_INT (i * 4);
6485             xop[1] = operands[1];
6486             output_asm_insn ("{l|lwz} %1,%0(%1)", xop);
6487             return "";
6488           }
6489       }
6490
6491   return "{lsi|lswi} %2,%1,%N0";
6492 }
6493
6494 /* Return 1 for a parallel vrsave operation.  */
6495
6496 int
6497 vrsave_operation (op, mode)
6498      rtx op;
6499      enum machine_mode mode ATTRIBUTE_UNUSED;
6500 {
6501   int count = XVECLEN (op, 0);
6502   unsigned int dest_regno, src_regno;
6503   int i;
6504
6505   if (count <= 1
6506       || GET_CODE (XVECEXP (op, 0, 0)) != SET
6507       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
6508       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC_VOLATILE)
6509     return 0;
6510
6511   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
6512   src_regno  = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
6513
6514   if (dest_regno != VRSAVE_REGNO
6515       && src_regno != VRSAVE_REGNO)
6516     return 0;
6517
6518   for (i = 1; i < count; i++)
6519     {
6520       rtx elt = XVECEXP (op, 0, i);
6521
6522       if (GET_CODE (elt) != CLOBBER
6523           && GET_CODE (elt) != SET)
6524         return 0;
6525     }
6526
6527   return 1;
6528 }
6529
6530 /* Return 1 for an PARALLEL suitable for mtcrf.  */
6531
6532 int
6533 mtcrf_operation (op, mode)
6534      rtx op;
6535      enum machine_mode mode ATTRIBUTE_UNUSED;
6536 {
6537   int count = XVECLEN (op, 0);
6538   int i;
6539   rtx src_reg;
6540
6541   /* Perform a quick check so we don't blow up below.  */
6542   if (count < 1
6543       || GET_CODE (XVECEXP (op, 0, 0)) != SET
6544       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC
6545       || XVECLEN (SET_SRC (XVECEXP (op, 0, 0)), 0) != 2)
6546     return 0;
6547   src_reg = XVECEXP (SET_SRC (XVECEXP (op, 0, 0)), 0, 0);
6548   
6549   if (GET_CODE (src_reg) != REG
6550       || GET_MODE (src_reg) != SImode
6551       || ! INT_REGNO_P (REGNO (src_reg)))
6552     return 0;
6553
6554   for (i = 0; i < count; i++)
6555     {
6556       rtx exp = XVECEXP (op, 0, i);
6557       rtx unspec;
6558       int maskval;
6559       
6560       if (GET_CODE (exp) != SET
6561           || GET_CODE (SET_DEST (exp)) != REG
6562           || GET_MODE (SET_DEST (exp)) != CCmode
6563           || ! CR_REGNO_P (REGNO (SET_DEST (exp))))
6564         return 0;
6565       unspec = SET_SRC (exp);
6566       maskval = 1 << (MAX_CR_REGNO - REGNO (SET_DEST (exp)));
6567       
6568       if (GET_CODE (unspec) != UNSPEC
6569           || XINT (unspec, 1) != 20
6570           || XVECLEN (unspec, 0) != 2
6571           || XVECEXP (unspec, 0, 0) != src_reg
6572           || GET_CODE (XVECEXP (unspec, 0, 1)) != CONST_INT
6573           || INTVAL (XVECEXP (unspec, 0, 1)) != maskval)
6574         return 0;
6575     }
6576   return 1;
6577 }
6578
6579 /* Return 1 for an PARALLEL suitable for lmw.  */
6580
6581 int
6582 lmw_operation (op, mode)
6583      rtx op;
6584      enum machine_mode mode ATTRIBUTE_UNUSED;
6585 {
6586   int count = XVECLEN (op, 0);
6587   unsigned int dest_regno;
6588   rtx src_addr;
6589   unsigned int base_regno;
6590   HOST_WIDE_INT offset;
6591   int i;
6592
6593   /* Perform a quick check so we don't blow up below.  */
6594   if (count <= 1
6595       || GET_CODE (XVECEXP (op, 0, 0)) != SET
6596       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
6597       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
6598     return 0;
6599
6600   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
6601   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
6602
6603   if (dest_regno > 31
6604       || count != 32 - (int) dest_regno)
6605     return 0;
6606
6607   if (LEGITIMATE_INDIRECT_ADDRESS_P (src_addr, 0))
6608     {
6609       offset = 0;
6610       base_regno = REGNO (src_addr);
6611       if (base_regno == 0)
6612         return 0;
6613     }
6614   else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, src_addr, 0))
6615     {
6616       offset = INTVAL (XEXP (src_addr, 1));
6617       base_regno = REGNO (XEXP (src_addr, 0));
6618     }
6619   else
6620     return 0;
6621
6622   for (i = 0; i < count; i++)
6623     {
6624       rtx elt = XVECEXP (op, 0, i);
6625       rtx newaddr;
6626       rtx addr_reg;
6627       HOST_WIDE_INT newoffset;
6628
6629       if (GET_CODE (elt) != SET
6630           || GET_CODE (SET_DEST (elt)) != REG
6631           || GET_MODE (SET_DEST (elt)) != SImode
6632           || REGNO (SET_DEST (elt)) != dest_regno + i
6633           || GET_CODE (SET_SRC (elt)) != MEM
6634           || GET_MODE (SET_SRC (elt)) != SImode)
6635         return 0;
6636       newaddr = XEXP (SET_SRC (elt), 0);
6637       if (LEGITIMATE_INDIRECT_ADDRESS_P (newaddr, 0))
6638         {
6639           newoffset = 0;
6640           addr_reg = newaddr;
6641         }
6642       else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, newaddr, 0))
6643         {
6644           addr_reg = XEXP (newaddr, 0);
6645           newoffset = INTVAL (XEXP (newaddr, 1));
6646         }
6647       else
6648         return 0;
6649       if (REGNO (addr_reg) != base_regno
6650           || newoffset != offset + 4 * i)
6651         return 0;
6652     }
6653
6654   return 1;
6655 }
6656
6657 /* Return 1 for an PARALLEL suitable for stmw.  */
6658
6659 int
6660 stmw_operation (op, mode)
6661      rtx op;
6662      enum machine_mode mode ATTRIBUTE_UNUSED;
6663 {
6664   int count = XVECLEN (op, 0);
6665   unsigned int src_regno;
6666   rtx dest_addr;
6667   unsigned int base_regno;
6668   HOST_WIDE_INT offset;
6669   int i;
6670
6671   /* Perform a quick check so we don't blow up below.  */
6672   if (count <= 1
6673       || GET_CODE (XVECEXP (op, 0, 0)) != SET
6674       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
6675       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
6676     return 0;
6677
6678   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
6679   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
6680
6681   if (src_regno > 31
6682       || count != 32 - (int) src_regno)
6683     return 0;
6684
6685   if (LEGITIMATE_INDIRECT_ADDRESS_P (dest_addr, 0))
6686     {
6687       offset = 0;
6688       base_regno = REGNO (dest_addr);
6689       if (base_regno == 0)
6690         return 0;
6691     }
6692   else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, dest_addr, 0))
6693     {
6694       offset = INTVAL (XEXP (dest_addr, 1));
6695       base_regno = REGNO (XEXP (dest_addr, 0));
6696     }
6697   else
6698     return 0;
6699
6700   for (i = 0; i < count; i++)
6701     {
6702       rtx elt = XVECEXP (op, 0, i);
6703       rtx newaddr;
6704       rtx addr_reg;
6705       HOST_WIDE_INT newoffset;
6706
6707       if (GET_CODE (elt) != SET
6708           || GET_CODE (SET_SRC (elt)) != REG
6709           || GET_MODE (SET_SRC (elt)) != SImode
6710           || REGNO (SET_SRC (elt)) != src_regno + i
6711           || GET_CODE (SET_DEST (elt)) != MEM
6712           || GET_MODE (SET_DEST (elt)) != SImode)
6713         return 0;
6714       newaddr = XEXP (SET_DEST (elt), 0);
6715       if (LEGITIMATE_INDIRECT_ADDRESS_P (newaddr, 0))
6716         {
6717           newoffset = 0;
6718           addr_reg = newaddr;
6719         }
6720       else if (LEGITIMATE_OFFSET_ADDRESS_P (SImode, newaddr, 0))
6721         {
6722           addr_reg = XEXP (newaddr, 0);
6723           newoffset = INTVAL (XEXP (newaddr, 1));
6724         }
6725       else
6726         return 0;
6727       if (REGNO (addr_reg) != base_regno
6728           || newoffset != offset + 4 * i)
6729         return 0;
6730     }
6731
6732   return 1;
6733 }
6734 \f
6735 /* A validation routine: say whether CODE, a condition code, and MODE
6736    match.  The other alternatives either don't make sense or should
6737    never be generated.  */
6738
6739 static void
6740 validate_condition_mode (code, mode)
6741      enum rtx_code code;
6742      enum machine_mode mode;
6743 {
6744   if (GET_RTX_CLASS (code) != '<' 
6745       || GET_MODE_CLASS (mode) != MODE_CC)
6746     abort ();
6747
6748   /* These don't make sense.  */
6749   if ((code == GT || code == LT || code == GE || code == LE)
6750       && mode == CCUNSmode)
6751     abort ();
6752
6753   if ((code == GTU || code == LTU || code == GEU || code == LEU)
6754       && mode != CCUNSmode)
6755     abort ();
6756
6757   if (mode != CCFPmode
6758       && (code == ORDERED || code == UNORDERED
6759           || code == UNEQ || code == LTGT
6760           || code == UNGT || code == UNLT
6761           || code == UNGE || code == UNLE))
6762     abort ();
6763   
6764   /* These should never be generated except for 
6765      flag_unsafe_math_optimizations and flag_finite_math_only.  */
6766   if (mode == CCFPmode
6767       && ! flag_unsafe_math_optimizations
6768       && ! flag_finite_math_only
6769       && (code == LE || code == GE
6770           || code == UNEQ || code == LTGT
6771           || code == UNGT || code == UNLT))
6772     abort ();
6773
6774   /* These are invalid; the information is not there.  */
6775   if (mode == CCEQmode 
6776       && code != EQ && code != NE)
6777     abort ();
6778 }
6779
6780 /* Return 1 if OP is a comparison operation that is valid for a branch insn.
6781    We only check the opcode against the mode of the CC value here.  */
6782
6783 int
6784 branch_comparison_operator (op, mode)
6785      rtx op;
6786      enum machine_mode mode ATTRIBUTE_UNUSED;
6787 {
6788   enum rtx_code code = GET_CODE (op);
6789   enum machine_mode cc_mode;
6790
6791   if (GET_RTX_CLASS (code) != '<')
6792     return 0;
6793
6794   cc_mode = GET_MODE (XEXP (op, 0));
6795   if (GET_MODE_CLASS (cc_mode) != MODE_CC)
6796     return 0;
6797
6798   validate_condition_mode (code, cc_mode);
6799
6800   return 1;
6801 }
6802
6803 /* Return 1 if OP is a comparison operation that is valid for a branch
6804    insn and which is true if the corresponding bit in the CC register
6805    is set.  */
6806
6807 int
6808 branch_positive_comparison_operator (op, mode)
6809      rtx op;
6810      enum machine_mode mode;
6811 {
6812   enum rtx_code code;
6813
6814   if (! branch_comparison_operator (op, mode))
6815     return 0;
6816
6817   code = GET_CODE (op);
6818   return (code == EQ || code == LT || code == GT
6819           || (TARGET_SPE && TARGET_HARD_FLOAT && !TARGET_FPRS && code == NE)
6820           || code == LTU || code == GTU
6821           || code == UNORDERED);
6822 }
6823
6824 /* Return 1 if OP is a comparison operation that is valid for an scc insn.
6825    We check the opcode against the mode of the CC value and disallow EQ or
6826    NE comparisons for integers.  */
6827
6828 int
6829 scc_comparison_operator (op, mode)
6830      rtx op;
6831      enum machine_mode mode;
6832 {
6833   enum rtx_code code = GET_CODE (op);
6834   enum machine_mode cc_mode;
6835
6836   if (GET_MODE (op) != mode && mode != VOIDmode)
6837     return 0;
6838
6839   if (GET_RTX_CLASS (code) != '<')
6840     return 0;
6841
6842   cc_mode = GET_MODE (XEXP (op, 0));
6843   if (GET_MODE_CLASS (cc_mode) != MODE_CC)
6844     return 0;
6845
6846   validate_condition_mode (code, cc_mode);
6847
6848   if (code == NE && cc_mode != CCFPmode)
6849     return 0;
6850
6851   return 1;
6852 }
6853
6854 int
6855 trap_comparison_operator (op, mode)
6856     rtx op;
6857     enum machine_mode mode;
6858 {
6859   if (mode != VOIDmode && mode != GET_MODE (op))
6860     return 0;
6861   return GET_RTX_CLASS (GET_CODE (op)) == '<';
6862 }
6863
6864 int
6865 boolean_operator (op, mode)
6866     rtx op;
6867     enum machine_mode mode ATTRIBUTE_UNUSED;
6868 {
6869   enum rtx_code code = GET_CODE (op);
6870   return (code == AND || code == IOR || code == XOR);
6871 }
6872
6873 int
6874 boolean_or_operator (op, mode)
6875     rtx op;
6876     enum machine_mode mode ATTRIBUTE_UNUSED;
6877 {
6878   enum rtx_code code = GET_CODE (op);
6879   return (code == IOR || code == XOR);
6880 }
6881
6882 int
6883 min_max_operator (op, mode)
6884     rtx op;
6885     enum machine_mode mode ATTRIBUTE_UNUSED;
6886 {
6887   enum rtx_code code = GET_CODE (op);
6888   return (code == SMIN || code == SMAX || code == UMIN || code == UMAX);
6889 }
6890 \f
6891 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
6892    mask required to convert the result of a rotate insn into a shift
6893    left insn of SHIFTOP bits.  Both are known to be SImode CONST_INT.  */
6894
6895 int
6896 includes_lshift_p (shiftop, andop)
6897      rtx shiftop;
6898      rtx andop;
6899 {
6900   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
6901
6902   shift_mask <<= INTVAL (shiftop);
6903
6904   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
6905 }
6906
6907 /* Similar, but for right shift.  */
6908
6909 int
6910 includes_rshift_p (shiftop, andop)
6911      rtx shiftop;
6912      rtx andop;
6913 {
6914   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
6915
6916   shift_mask >>= INTVAL (shiftop);
6917
6918   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
6919 }
6920
6921 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
6922    to perform a left shift.  It must have exactly SHIFTOP least
6923    signifigant 0's, then one or more 1's, then zero or more 0's.  */
6924
6925 int
6926 includes_rldic_lshift_p (shiftop, andop)
6927      rtx shiftop;
6928      rtx andop;
6929 {
6930   if (GET_CODE (andop) == CONST_INT)
6931     {
6932       HOST_WIDE_INT c, lsb, shift_mask;
6933
6934       c = INTVAL (andop);
6935       if (c == 0 || c == ~0)
6936         return 0;
6937
6938       shift_mask = ~0;
6939       shift_mask <<= INTVAL (shiftop);
6940
6941       /* Find the least signifigant one bit.  */
6942       lsb = c & -c;
6943
6944       /* It must coincide with the LSB of the shift mask.  */
6945       if (-lsb != shift_mask)
6946         return 0;
6947
6948       /* Invert to look for the next transition (if any).  */
6949       c = ~c;
6950
6951       /* Remove the low group of ones (originally low group of zeros).  */
6952       c &= -lsb;
6953
6954       /* Again find the lsb, and check we have all 1's above.  */
6955       lsb = c & -c;
6956       return c == -lsb;
6957     }
6958   else if (GET_CODE (andop) == CONST_DOUBLE
6959            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
6960     {
6961       HOST_WIDE_INT low, high, lsb;
6962       HOST_WIDE_INT shift_mask_low, shift_mask_high;
6963
6964       low = CONST_DOUBLE_LOW (andop);
6965       if (HOST_BITS_PER_WIDE_INT < 64)
6966         high = CONST_DOUBLE_HIGH (andop);
6967
6968       if ((low == 0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == 0))
6969           || (low == ~0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0)))
6970         return 0;
6971
6972       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
6973         {
6974           shift_mask_high = ~0;
6975           if (INTVAL (shiftop) > 32)
6976             shift_mask_high <<= INTVAL (shiftop) - 32;
6977
6978           lsb = high & -high;
6979
6980           if (-lsb != shift_mask_high || INTVAL (shiftop) < 32)
6981             return 0;
6982
6983           high = ~high;
6984           high &= -lsb;
6985
6986           lsb = high & -high;
6987           return high == -lsb;
6988         }
6989
6990       shift_mask_low = ~0;
6991       shift_mask_low <<= INTVAL (shiftop);
6992
6993       lsb = low & -low;
6994
6995       if (-lsb != shift_mask_low)
6996         return 0;
6997
6998       if (HOST_BITS_PER_WIDE_INT < 64)
6999         high = ~high;
7000       low = ~low;
7001       low &= -lsb;
7002
7003       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
7004         {
7005           lsb = high & -high;
7006           return high == -lsb;
7007         }
7008
7009       lsb = low & -low;
7010       return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
7011     }
7012   else
7013     return 0;
7014 }
7015
7016 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
7017    to perform a left shift.  It must have SHIFTOP or more least
7018    signifigant 0's, with the remainder of the word 1's.  */
7019
7020 int
7021 includes_rldicr_lshift_p (shiftop, andop)
7022      rtx shiftop;
7023      rtx andop;
7024 {
7025   if (GET_CODE (andop) == CONST_INT)
7026     {
7027       HOST_WIDE_INT c, lsb, shift_mask;
7028
7029       shift_mask = ~0;
7030       shift_mask <<= INTVAL (shiftop);
7031       c = INTVAL (andop);
7032
7033       /* Find the least signifigant one bit.  */
7034       lsb = c & -c;
7035
7036       /* It must be covered by the shift mask.
7037          This test also rejects c == 0.  */
7038       if ((lsb & shift_mask) == 0)
7039         return 0;
7040
7041       /* Check we have all 1's above the transition, and reject all 1's.  */
7042       return c == -lsb && lsb != 1;
7043     }
7044   else if (GET_CODE (andop) == CONST_DOUBLE
7045            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
7046     {
7047       HOST_WIDE_INT low, lsb, shift_mask_low;
7048
7049       low = CONST_DOUBLE_LOW (andop);
7050
7051       if (HOST_BITS_PER_WIDE_INT < 64)
7052         {
7053           HOST_WIDE_INT high, shift_mask_high;
7054
7055           high = CONST_DOUBLE_HIGH (andop);
7056
7057           if (low == 0)
7058             {
7059               shift_mask_high = ~0;
7060               if (INTVAL (shiftop) > 32)
7061                 shift_mask_high <<= INTVAL (shiftop) - 32;
7062
7063               lsb = high & -high;
7064
7065               if ((lsb & shift_mask_high) == 0)
7066                 return 0;
7067
7068               return high == -lsb;
7069             }
7070           if (high != ~0)
7071             return 0;
7072         }
7073
7074       shift_mask_low = ~0;
7075       shift_mask_low <<= INTVAL (shiftop);
7076
7077       lsb = low & -low;
7078
7079       if ((lsb & shift_mask_low) == 0)
7080         return 0;
7081
7082       return low == -lsb && lsb != 1;
7083     }
7084   else
7085     return 0;
7086 }
7087
7088 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
7089    for lfq and stfq insns.
7090
7091    Note reg1 and reg2 *must* be hard registers.  To be sure we will
7092    abort if we are passed pseudo registers.  */
7093
7094 int
7095 registers_ok_for_quad_peep (reg1, reg2)
7096      rtx reg1, reg2;
7097 {
7098   /* We might have been passed a SUBREG.  */
7099   if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG) 
7100     return 0;
7101
7102   return (REGNO (reg1) == REGNO (reg2) - 1);
7103 }
7104
7105 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
7106    addr1 and addr2 must be in consecutive memory locations
7107    (addr2 == addr1 + 8).  */
7108
7109 int
7110 addrs_ok_for_quad_peep (addr1, addr2)
7111      rtx addr1;
7112      rtx addr2;
7113 {
7114   unsigned int reg1;
7115   int offset1;
7116
7117   /* Extract an offset (if used) from the first addr.  */
7118   if (GET_CODE (addr1) == PLUS)
7119     {
7120       /* If not a REG, return zero.  */
7121       if (GET_CODE (XEXP (addr1, 0)) != REG)
7122         return 0;
7123       else
7124         {
7125           reg1 = REGNO (XEXP (addr1, 0));
7126           /* The offset must be constant!  */
7127           if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
7128             return 0;
7129           offset1 = INTVAL (XEXP (addr1, 1));
7130         }
7131     }
7132   else if (GET_CODE (addr1) != REG)
7133     return 0;
7134   else
7135     {
7136       reg1 = REGNO (addr1);
7137       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
7138       offset1 = 0;
7139     }
7140
7141   /* Make sure the second address is a (mem (plus (reg) (const_int))).  */
7142   if (GET_CODE (addr2) != PLUS)
7143     return 0;
7144
7145   if (GET_CODE (XEXP (addr2, 0)) != REG
7146       || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
7147     return 0;
7148
7149   if (reg1 != REGNO (XEXP (addr2, 0)))
7150     return 0;
7151
7152   /* The offset for the second addr must be 8 more than the first addr.  */
7153   if (INTVAL (XEXP (addr2, 1)) != offset1 + 8)
7154     return 0;
7155
7156   /* All the tests passed.  addr1 and addr2 are valid for lfq or stfq
7157      instructions.  */
7158   return 1;
7159 }
7160 \f
7161 /* Return the register class of a scratch register needed to copy IN into
7162    or out of a register in CLASS in MODE.  If it can be done directly,
7163    NO_REGS is returned.  */
7164
7165 enum reg_class
7166 secondary_reload_class (class, mode, in)
7167      enum reg_class class;
7168      enum machine_mode mode ATTRIBUTE_UNUSED;
7169      rtx in;
7170 {
7171   int regno;
7172
7173   if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN && flag_pic))
7174     {
7175       /* We cannot copy a symbolic operand directly into anything
7176          other than BASE_REGS for TARGET_ELF.  So indicate that a
7177          register from BASE_REGS is needed as an intermediate
7178          register.
7179          
7180          On Darwin, pic addresses require a load from memory, which
7181          needs a base register.  */
7182       if (class != BASE_REGS
7183           && (GET_CODE (in) == SYMBOL_REF
7184               || GET_CODE (in) == HIGH
7185               || GET_CODE (in) == LABEL_REF
7186               || GET_CODE (in) == CONST))
7187         return BASE_REGS;
7188     }
7189
7190   if (GET_CODE (in) == REG)
7191     {
7192       regno = REGNO (in);
7193       if (regno >= FIRST_PSEUDO_REGISTER)
7194         {
7195           regno = true_regnum (in);
7196           if (regno >= FIRST_PSEUDO_REGISTER)
7197             regno = -1;
7198         }
7199     }
7200   else if (GET_CODE (in) == SUBREG)
7201     {
7202       regno = true_regnum (in);
7203       if (regno >= FIRST_PSEUDO_REGISTER)
7204         regno = -1;
7205     }
7206   else
7207     regno = -1;
7208
7209   /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
7210      into anything.  */
7211   if (class == GENERAL_REGS || class == BASE_REGS
7212       || (regno >= 0 && INT_REGNO_P (regno)))
7213     return NO_REGS;
7214
7215   /* Constants, memory, and FP registers can go into FP registers.  */
7216   if ((regno == -1 || FP_REGNO_P (regno))
7217       && (class == FLOAT_REGS || class == NON_SPECIAL_REGS))
7218     return NO_REGS;
7219
7220   /* Memory, and AltiVec registers can go into AltiVec registers.  */
7221   if ((regno == -1 || ALTIVEC_REGNO_P (regno))
7222       && class == ALTIVEC_REGS)
7223     return NO_REGS;
7224
7225   /* We can copy among the CR registers.  */
7226   if ((class == CR_REGS || class == CR0_REGS)
7227       && regno >= 0 && CR_REGNO_P (regno))
7228     return NO_REGS;
7229
7230   /* Otherwise, we need GENERAL_REGS.  */
7231   return GENERAL_REGS;
7232 }
7233 \f
7234 /* Given a comparison operation, return the bit number in CCR to test.  We
7235    know this is a valid comparison.  
7236
7237    SCC_P is 1 if this is for an scc.  That means that %D will have been
7238    used instead of %C, so the bits will be in different places.
7239
7240    Return -1 if OP isn't a valid comparison for some reason.  */
7241
7242 int
7243 ccr_bit (op, scc_p)
7244      rtx op;
7245      int scc_p;
7246 {
7247   enum rtx_code code = GET_CODE (op);
7248   enum machine_mode cc_mode;
7249   int cc_regnum;
7250   int base_bit;
7251   rtx reg;
7252
7253   if (GET_RTX_CLASS (code) != '<')
7254     return -1;
7255
7256   reg = XEXP (op, 0);
7257
7258   if (GET_CODE (reg) != REG
7259       || ! CR_REGNO_P (REGNO (reg)))
7260     abort ();
7261
7262   cc_mode = GET_MODE (reg);
7263   cc_regnum = REGNO (reg);
7264   base_bit = 4 * (cc_regnum - CR0_REGNO);
7265
7266   validate_condition_mode (code, cc_mode);
7267
7268   switch (code)
7269     {
7270     case NE:
7271       if (TARGET_SPE && TARGET_HARD_FLOAT && cc_mode == CCFPmode)
7272         return base_bit + 1;
7273       return scc_p ? base_bit + 3 : base_bit + 2;
7274     case EQ:
7275       if (TARGET_SPE && TARGET_HARD_FLOAT && cc_mode == CCFPmode)
7276         return base_bit + 1;
7277       return base_bit + 2;
7278     case GT:  case GTU:  case UNLE:
7279       return base_bit + 1;
7280     case LT:  case LTU:  case UNGE:
7281       return base_bit;
7282     case ORDERED:  case UNORDERED:
7283       return base_bit + 3;
7284
7285     case GE:  case GEU:
7286       /* If scc, we will have done a cror to put the bit in the
7287          unordered position.  So test that bit.  For integer, this is ! LT
7288          unless this is an scc insn.  */
7289       return scc_p ? base_bit + 3 : base_bit;
7290
7291     case LE:  case LEU:
7292       return scc_p ? base_bit + 3 : base_bit + 1;
7293
7294     default:
7295       abort ();
7296     }
7297 }
7298 \f
7299 /* Return the GOT register.  */
7300
7301 struct rtx_def *
7302 rs6000_got_register (value)
7303      rtx value ATTRIBUTE_UNUSED;
7304 {
7305   /* The second flow pass currently (June 1999) can't update
7306      regs_ever_live without disturbing other parts of the compiler, so
7307      update it here to make the prolog/epilogue code happy.  */
7308   if (no_new_pseudos && ! regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM])
7309     regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7310
7311   current_function_uses_pic_offset_table = 1;
7312
7313   return pic_offset_table_rtx;
7314 }
7315 \f
7316 /* Function to init struct machine_function.
7317    This will be called, via a pointer variable,
7318    from push_function_context.  */
7319
7320 static struct machine_function *
7321 rs6000_init_machine_status ()
7322 {
7323   return ggc_alloc_cleared (sizeof (machine_function));
7324 }
7325 \f
7326 /* These macros test for integers and extract the low-order bits.  */
7327 #define INT_P(X)  \
7328 ((GET_CODE (X) == CONST_INT || GET_CODE (X) == CONST_DOUBLE)    \
7329  && GET_MODE (X) == VOIDmode)
7330
7331 #define INT_LOWPART(X) \
7332   (GET_CODE (X) == CONST_INT ? INTVAL (X) : CONST_DOUBLE_LOW (X))
7333
7334 int
7335 extract_MB (op)
7336      rtx op;
7337 {
7338   int i;
7339   unsigned long val = INT_LOWPART (op);
7340
7341   /* If the high bit is zero, the value is the first 1 bit we find
7342      from the left.  */
7343   if ((val & 0x80000000) == 0)
7344     {
7345       if ((val & 0xffffffff) == 0)
7346         abort ();
7347
7348       i = 1;
7349       while (((val <<= 1) & 0x80000000) == 0)
7350         ++i;
7351       return i;
7352     }
7353
7354   /* If the high bit is set and the low bit is not, or the mask is all
7355      1's, the value is zero.  */
7356   if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
7357     return 0;
7358
7359   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
7360      from the right.  */
7361   i = 31;
7362   while (((val >>= 1) & 1) != 0)
7363     --i;
7364
7365   return i;
7366 }
7367
7368 int
7369 extract_ME (op)
7370      rtx op;
7371 {
7372   int i;
7373   unsigned long val = INT_LOWPART (op);
7374
7375   /* If the low bit is zero, the value is the first 1 bit we find from
7376      the right.  */
7377   if ((val & 1) == 0)
7378     {
7379       if ((val & 0xffffffff) == 0)
7380         abort ();
7381
7382       i = 30;
7383       while (((val >>= 1) & 1) == 0)
7384         --i;
7385
7386       return i;
7387     }
7388
7389   /* If the low bit is set and the high bit is not, or the mask is all
7390      1's, the value is 31.  */
7391   if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
7392     return 31;
7393
7394   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
7395      from the left.  */
7396   i = 0;
7397   while (((val <<= 1) & 0x80000000) != 0)
7398     ++i;
7399
7400   return i;
7401 }
7402
7403 /* Print an operand.  Recognize special options, documented below.  */
7404
7405 #if TARGET_ELF
7406 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
7407 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
7408 #else
7409 #define SMALL_DATA_RELOC "sda21"
7410 #define SMALL_DATA_REG 0
7411 #endif
7412
7413 void
7414 print_operand (file, x, code)
7415     FILE *file;
7416     rtx x;
7417     int code;
7418 {
7419   int i;
7420   HOST_WIDE_INT val;
7421   unsigned HOST_WIDE_INT uval;
7422
7423   switch (code)
7424     {
7425     case '.':
7426       /* Write out an instruction after the call which may be replaced
7427          with glue code by the loader.  This depends on the AIX version.  */
7428       asm_fprintf (file, RS6000_CALL_GLUE);
7429       return;
7430
7431       /* %a is output_address.  */
7432
7433     case 'A':
7434       /* If X is a constant integer whose low-order 5 bits are zero,
7435          write 'l'.  Otherwise, write 'r'.  This is a kludge to fix a bug
7436          in the AIX assembler where "sri" with a zero shift count
7437          writes a trash instruction.  */
7438       if (GET_CODE (x) == CONST_INT && (INTVAL (x) & 31) == 0)
7439         putc ('l', file);
7440       else
7441         putc ('r', file);
7442       return;
7443
7444     case 'b':
7445       /* If constant, low-order 16 bits of constant, unsigned.
7446          Otherwise, write normally.  */
7447       if (INT_P (x))
7448         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 0xffff);
7449       else
7450         print_operand (file, x, 0);
7451       return;
7452
7453     case 'B':
7454       /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
7455          for 64-bit mask direction.  */
7456       putc (((INT_LOWPART(x) & 1) == 0 ? 'r' : 'l'), file);
7457       return;
7458
7459       /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
7460          output_operand.  */
7461
7462     case 'D':
7463       /* There used to be a comment for 'C' reading "This is an
7464            optional cror needed for certain floating-point
7465            comparisons.  Otherwise write nothing."  */
7466
7467       /* Similar, except that this is for an scc, so we must be able to
7468          encode the test in a single bit that is one.  We do the above
7469          for any LE, GE, GEU, or LEU and invert the bit for NE.  */
7470       if (GET_CODE (x) == LE || GET_CODE (x) == GE
7471           || GET_CODE (x) == LEU || GET_CODE (x) == GEU)
7472         {
7473           int base_bit = 4 * (REGNO (XEXP (x, 0)) - CR0_REGNO);
7474
7475           fprintf (file, "cror %d,%d,%d\n\t", base_bit + 3,
7476                    base_bit + 2,
7477                    base_bit + (GET_CODE (x) == GE || GET_CODE (x) == GEU));
7478         }
7479
7480       else if (GET_CODE (x) == NE)
7481         {
7482           int base_bit = 4 * (REGNO (XEXP (x, 0)) - CR0_REGNO);
7483
7484           fprintf (file, "crnor %d,%d,%d\n\t", base_bit + 3,
7485                    base_bit + 2, base_bit + 2);
7486         }
7487       else if (TARGET_SPE && TARGET_HARD_FLOAT
7488                && GET_CODE (x) == EQ
7489                && GET_MODE (XEXP (x, 0)) == CCFPmode)
7490         {
7491           int base_bit = 4 * (REGNO (XEXP (x, 0)) - CR0_REGNO);
7492
7493           fprintf (file, "crnor %d,%d,%d\n\t", base_bit + 1,
7494                    base_bit + 1, base_bit + 1);
7495         }
7496       return;
7497
7498     case 'E':
7499       /* X is a CR register.  Print the number of the EQ bit of the CR */
7500       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
7501         output_operand_lossage ("invalid %%E value");
7502       else
7503         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
7504       return;
7505
7506     case 'f':
7507       /* X is a CR register.  Print the shift count needed to move it
7508          to the high-order four bits.  */
7509       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
7510         output_operand_lossage ("invalid %%f value");
7511       else
7512         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
7513       return;
7514
7515     case 'F':
7516       /* Similar, but print the count for the rotate in the opposite
7517          direction.  */
7518       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
7519         output_operand_lossage ("invalid %%F value");
7520       else
7521         fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
7522       return;
7523
7524     case 'G':
7525       /* X is a constant integer.  If it is negative, print "m",
7526          otherwise print "z".  This is to make an aze or ame insn.  */
7527       if (GET_CODE (x) != CONST_INT)
7528         output_operand_lossage ("invalid %%G value");
7529       else if (INTVAL (x) >= 0)
7530         putc ('z', file);
7531       else
7532         putc ('m', file);
7533       return;
7534
7535     case 'h':
7536       /* If constant, output low-order five bits.  Otherwise, write
7537          normally.  */
7538       if (INT_P (x))
7539         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 31);
7540       else
7541         print_operand (file, x, 0);
7542       return;
7543
7544     case 'H':
7545       /* If constant, output low-order six bits.  Otherwise, write
7546          normally.  */
7547       if (INT_P (x))
7548         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 63);
7549       else
7550         print_operand (file, x, 0);
7551       return;
7552
7553     case 'I':
7554       /* Print `i' if this is a constant, else nothing.  */
7555       if (INT_P (x))
7556         putc ('i', file);
7557       return;
7558
7559     case 'j':
7560       /* Write the bit number in CCR for jump.  */
7561       i = ccr_bit (x, 0);
7562       if (i == -1)
7563         output_operand_lossage ("invalid %%j code");
7564       else
7565         fprintf (file, "%d", i);
7566       return;
7567
7568     case 'J':
7569       /* Similar, but add one for shift count in rlinm for scc and pass
7570          scc flag to `ccr_bit'.  */
7571       i = ccr_bit (x, 1);
7572       if (i == -1)
7573         output_operand_lossage ("invalid %%J code");
7574       else
7575         /* If we want bit 31, write a shift count of zero, not 32.  */
7576         fprintf (file, "%d", i == 31 ? 0 : i + 1);
7577       return;
7578
7579     case 'k':
7580       /* X must be a constant.  Write the 1's complement of the
7581          constant.  */
7582       if (! INT_P (x))
7583         output_operand_lossage ("invalid %%k value");
7584       else
7585         fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INT_LOWPART (x));
7586       return;
7587
7588     case 'K':
7589       /* X must be a symbolic constant on ELF.  Write an
7590          expression suitable for an 'addi' that adds in the low 16
7591          bits of the MEM.  */
7592       if (GET_CODE (x) != CONST)
7593         {
7594           print_operand_address (file, x);
7595           fputs ("@l", file);
7596         }
7597       else
7598         {
7599           if (GET_CODE (XEXP (x, 0)) != PLUS
7600               || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
7601                   && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
7602               || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
7603             output_operand_lossage ("invalid %%K value");
7604           print_operand_address (file, XEXP (XEXP (x, 0), 0));
7605           fputs ("@l", file);
7606           /* For GNU as, there must be a non-alphanumeric character
7607              between 'l' and the number.  The '-' is added by
7608              print_operand() already.  */
7609           if (INTVAL (XEXP (XEXP (x, 0), 1)) >= 0)
7610             fputs ("+", file);
7611           print_operand (file, XEXP (XEXP (x, 0), 1), 0);
7612         }
7613       return;
7614
7615       /* %l is output_asm_label.  */
7616
7617     case 'L':
7618       /* Write second word of DImode or DFmode reference.  Works on register
7619          or non-indexed memory only.  */
7620       if (GET_CODE (x) == REG)
7621         fprintf (file, "%s", reg_names[REGNO (x) + 1]);
7622       else if (GET_CODE (x) == MEM)
7623         {
7624           /* Handle possible auto-increment.  Since it is pre-increment and
7625              we have already done it, we can just use an offset of word.  */
7626           if (GET_CODE (XEXP (x, 0)) == PRE_INC
7627               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
7628             output_address (plus_constant (XEXP (XEXP (x, 0), 0),
7629                                            UNITS_PER_WORD));
7630           else
7631             output_address (XEXP (adjust_address_nv (x, SImode,
7632                                                      UNITS_PER_WORD),
7633                                   0));
7634
7635           if (small_data_operand (x, GET_MODE (x)))
7636             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
7637                      reg_names[SMALL_DATA_REG]);
7638         }
7639       return;
7640                             
7641     case 'm':
7642       /* MB value for a mask operand.  */
7643       if (! mask_operand (x, SImode))
7644         output_operand_lossage ("invalid %%m value");
7645
7646       fprintf (file, "%d", extract_MB (x));
7647       return;
7648
7649     case 'M':
7650       /* ME value for a mask operand.  */
7651       if (! mask_operand (x, SImode))
7652         output_operand_lossage ("invalid %%M value");
7653
7654       fprintf (file, "%d", extract_ME (x));
7655       return;
7656
7657       /* %n outputs the negative of its operand.  */
7658
7659     case 'N':
7660       /* Write the number of elements in the vector times 4.  */
7661       if (GET_CODE (x) != PARALLEL)
7662         output_operand_lossage ("invalid %%N value");
7663       else
7664         fprintf (file, "%d", XVECLEN (x, 0) * 4);
7665       return;
7666
7667     case 'O':
7668       /* Similar, but subtract 1 first.  */
7669       if (GET_CODE (x) != PARALLEL)
7670         output_operand_lossage ("invalid %%O value");
7671       else
7672         fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
7673       return;
7674
7675     case 'p':
7676       /* X is a CONST_INT that is a power of two.  Output the logarithm.  */
7677       if (! INT_P (x)
7678           || INT_LOWPART (x) < 0
7679           || (i = exact_log2 (INT_LOWPART (x))) < 0)
7680         output_operand_lossage ("invalid %%p value");
7681       else
7682         fprintf (file, "%d", i);
7683       return;
7684
7685     case 'P':
7686       /* The operand must be an indirect memory reference.  The result
7687          is the register number.  */
7688       if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
7689           || REGNO (XEXP (x, 0)) >= 32)
7690         output_operand_lossage ("invalid %%P value");
7691       else
7692         fprintf (file, "%d", REGNO (XEXP (x, 0)));
7693       return;
7694
7695     case 'q':
7696       /* This outputs the logical code corresponding to a boolean
7697          expression.  The expression may have one or both operands
7698          negated (if one, only the first one).  For condition register
7699          logical operations, it will also treat the negated
7700          CR codes as NOTs, but not handle NOTs of them.  */
7701       {
7702         const char *const *t = 0;
7703         const char *s;
7704         enum rtx_code code = GET_CODE (x);
7705         static const char * const tbl[3][3] = {
7706           { "and", "andc", "nor" },
7707           { "or", "orc", "nand" },
7708           { "xor", "eqv", "xor" } };
7709
7710         if (code == AND)
7711           t = tbl[0];
7712         else if (code == IOR)
7713           t = tbl[1];
7714         else if (code == XOR)
7715           t = tbl[2];
7716         else
7717           output_operand_lossage ("invalid %%q value");
7718
7719         if (GET_CODE (XEXP (x, 0)) != NOT)
7720           s = t[0];
7721         else
7722           {
7723             if (GET_CODE (XEXP (x, 1)) == NOT)
7724               s = t[2];
7725             else
7726               s = t[1];
7727           }
7728         
7729         fputs (s, file);
7730       }
7731       return;
7732
7733     case 'R':
7734       /* X is a CR register.  Print the mask for `mtcrf'.  */
7735       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
7736         output_operand_lossage ("invalid %%R value");
7737       else
7738         fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
7739       return;
7740
7741     case 's':
7742       /* Low 5 bits of 32 - value */
7743       if (! INT_P (x))
7744         output_operand_lossage ("invalid %%s value");
7745       else
7746         fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INT_LOWPART (x)) & 31);
7747       return;
7748
7749     case 'S':
7750       /* PowerPC64 mask position.  All 0's is excluded.
7751          CONST_INT 32-bit mask is considered sign-extended so any
7752          transition must occur within the CONST_INT, not on the boundary.  */
7753       if (! mask64_operand (x, DImode))
7754         output_operand_lossage ("invalid %%S value");
7755
7756       uval = INT_LOWPART (x);
7757
7758       if (uval & 1)     /* Clear Left */
7759         {
7760           uval &= ((unsigned HOST_WIDE_INT) 1 << 63 << 1) - 1;
7761           i = 64;
7762         }
7763       else              /* Clear Right */
7764         {
7765           uval = ~uval;
7766           uval &= ((unsigned HOST_WIDE_INT) 1 << 63 << 1) - 1;
7767           i = 63;
7768         }
7769       while (uval != 0)
7770         --i, uval >>= 1;
7771       if (i < 0)
7772         abort ();
7773       fprintf (file, "%d", i);
7774       return;
7775
7776     case 't':
7777       /* Like 'J' but get to the OVERFLOW/UNORDERED bit.  */
7778       if (GET_CODE (x) != REG || GET_MODE (x) != CCmode)
7779         abort ();
7780
7781       /* Bit 3 is OV bit.  */
7782       i = 4 * (REGNO (x) - CR0_REGNO) + 3;
7783
7784       /* If we want bit 31, write a shift count of zero, not 32.  */
7785       fprintf (file, "%d", i == 31 ? 0 : i + 1);
7786       return;
7787
7788     case 'T':
7789       /* Print the symbolic name of a branch target register.  */
7790       if (GET_CODE (x) != REG || (REGNO (x) != LINK_REGISTER_REGNUM
7791                                   && REGNO (x) != COUNT_REGISTER_REGNUM))
7792         output_operand_lossage ("invalid %%T value");
7793       else if (REGNO (x) == LINK_REGISTER_REGNUM)
7794         fputs (TARGET_NEW_MNEMONICS ? "lr" : "r", file);
7795       else
7796         fputs ("ctr", file);
7797       return;
7798
7799     case 'u':
7800       /* High-order 16 bits of constant for use in unsigned operand.  */
7801       if (! INT_P (x))
7802         output_operand_lossage ("invalid %%u value");
7803       else
7804         fprintf (file, HOST_WIDE_INT_PRINT_HEX, 
7805                  (INT_LOWPART (x) >> 16) & 0xffff);
7806       return;
7807
7808     case 'v':
7809       /* High-order 16 bits of constant for use in signed operand.  */
7810       if (! INT_P (x))
7811         output_operand_lossage ("invalid %%v value");
7812       else
7813         fprintf (file, HOST_WIDE_INT_PRINT_HEX,
7814                  (INT_LOWPART (x) >> 16) & 0xffff);
7815       return;
7816
7817     case 'U':
7818       /* Print `u' if this has an auto-increment or auto-decrement.  */
7819       if (GET_CODE (x) == MEM
7820           && (GET_CODE (XEXP (x, 0)) == PRE_INC
7821               || GET_CODE (XEXP (x, 0)) == PRE_DEC))
7822         putc ('u', file);
7823       return;
7824
7825     case 'V':
7826       /* Print the trap code for this operand.  */
7827       switch (GET_CODE (x))
7828         {
7829         case EQ:
7830           fputs ("eq", file);   /* 4 */
7831           break;
7832         case NE:
7833           fputs ("ne", file);   /* 24 */
7834           break;
7835         case LT:
7836           fputs ("lt", file);   /* 16 */
7837           break;
7838         case LE:
7839           fputs ("le", file);   /* 20 */
7840           break;
7841         case GT:
7842           fputs ("gt", file);   /* 8 */
7843           break;
7844         case GE:
7845           fputs ("ge", file);   /* 12 */
7846           break;
7847         case LTU:
7848           fputs ("llt", file);  /* 2 */
7849           break;
7850         case LEU:
7851           fputs ("lle", file);  /* 6 */
7852           break;
7853         case GTU:
7854           fputs ("lgt", file);  /* 1 */
7855           break;
7856         case GEU:
7857           fputs ("lge", file);  /* 5 */
7858           break;
7859         default:
7860           abort ();
7861         }
7862       break;
7863
7864     case 'w':
7865       /* If constant, low-order 16 bits of constant, signed.  Otherwise, write
7866          normally.  */
7867       if (INT_P (x))
7868         fprintf (file, HOST_WIDE_INT_PRINT_DEC, 
7869                  ((INT_LOWPART (x) & 0xffff) ^ 0x8000) - 0x8000);
7870       else
7871         print_operand (file, x, 0);
7872       return;
7873
7874     case 'W':
7875       /* MB value for a PowerPC64 rldic operand.  */
7876       val = (GET_CODE (x) == CONST_INT
7877              ? INTVAL (x) : CONST_DOUBLE_HIGH (x));
7878
7879       if (val < 0)
7880         i = -1;
7881       else
7882         for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
7883           if ((val <<= 1) < 0)
7884             break;
7885
7886 #if HOST_BITS_PER_WIDE_INT == 32
7887       if (GET_CODE (x) == CONST_INT && i >= 0)
7888         i += 32;  /* zero-extend high-part was all 0's */
7889       else if (GET_CODE (x) == CONST_DOUBLE && i == 32)
7890         {
7891           val = CONST_DOUBLE_LOW (x);
7892
7893           if (val == 0)
7894             abort ();
7895           else if (val < 0)
7896             --i;
7897           else
7898             for ( ; i < 64; i++)
7899               if ((val <<= 1) < 0)
7900                 break;
7901         }
7902 #endif
7903
7904       fprintf (file, "%d", i + 1);
7905       return;
7906
7907     case 'X':
7908       if (GET_CODE (x) == MEM
7909           && LEGITIMATE_INDEXED_ADDRESS_P (XEXP (x, 0), 0))
7910         putc ('x', file);
7911       return;
7912
7913     case 'Y':
7914       /* Like 'L', for third word of TImode  */
7915       if (GET_CODE (x) == REG)
7916         fprintf (file, "%s", reg_names[REGNO (x) + 2]);
7917       else if (GET_CODE (x) == MEM)
7918         {
7919           if (GET_CODE (XEXP (x, 0)) == PRE_INC
7920               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
7921             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 8));
7922           else
7923             output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
7924           if (small_data_operand (x, GET_MODE (x)))
7925             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
7926                      reg_names[SMALL_DATA_REG]);
7927         }
7928       return;
7929                             
7930     case 'z':
7931       /* X is a SYMBOL_REF.  Write out the name preceded by a
7932          period and without any trailing data in brackets.  Used for function
7933          names.  If we are configured for System V (or the embedded ABI) on
7934          the PowerPC, do not emit the period, since those systems do not use
7935          TOCs and the like.  */
7936       if (GET_CODE (x) != SYMBOL_REF)
7937         abort ();
7938
7939       if (XSTR (x, 0)[0] != '.')
7940         {
7941           switch (DEFAULT_ABI)
7942             {
7943             default:
7944               abort ();
7945
7946             case ABI_AIX:
7947               putc ('.', file);
7948               break;
7949
7950             case ABI_V4:
7951             case ABI_AIX_NODESC:
7952             case ABI_DARWIN:
7953               break;
7954             }
7955         }
7956 #if TARGET_AIX
7957       RS6000_OUTPUT_BASENAME (file, XSTR (x, 0));
7958 #else
7959       assemble_name (file, XSTR (x, 0));
7960 #endif
7961       return;
7962
7963     case 'Z':
7964       /* Like 'L', for last word of TImode.  */
7965       if (GET_CODE (x) == REG)
7966         fprintf (file, "%s", reg_names[REGNO (x) + 3]);
7967       else if (GET_CODE (x) == MEM)
7968         {
7969           if (GET_CODE (XEXP (x, 0)) == PRE_INC
7970               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
7971             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 12));
7972           else
7973             output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
7974           if (small_data_operand (x, GET_MODE (x)))
7975             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
7976                      reg_names[SMALL_DATA_REG]);
7977         }
7978       return;
7979
7980       /* Print AltiVec or SPE memory operand.  */
7981     case 'y':
7982       {
7983         rtx tmp;
7984
7985         if (GET_CODE (x) != MEM)
7986           abort ();
7987
7988         tmp = XEXP (x, 0);
7989
7990         if (TARGET_SPE)
7991           {
7992             /* Handle [reg].  */
7993             if (GET_CODE (tmp) == REG)
7994               {
7995                 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
7996                 break;
7997               }
7998             /* Handle [reg+UIMM].  */
7999             else if (GET_CODE (tmp) == PLUS &&
8000                      GET_CODE (XEXP (tmp, 1)) == CONST_INT)
8001               {
8002                 int x;
8003
8004                 if (GET_CODE (XEXP (tmp, 0)) != REG)
8005                   abort ();
8006
8007                 x = INTVAL (XEXP (tmp, 1));
8008                 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
8009                 break;
8010               }
8011
8012             /* Fall through.  Must be [reg+reg].  */
8013           }
8014         if (GET_CODE (tmp) == REG)
8015           fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
8016         else if (GET_CODE (tmp) == PLUS && GET_CODE (XEXP (tmp, 1)) == REG)
8017           {
8018             if (REGNO (XEXP (tmp, 0)) == 0)
8019               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
8020                        reg_names[ REGNO (XEXP (tmp, 0)) ]);
8021             else
8022               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
8023                        reg_names[ REGNO (XEXP (tmp, 1)) ]);
8024           }
8025         else
8026           abort ();
8027         break;
8028       }
8029                             
8030     case 0:
8031       if (GET_CODE (x) == REG)
8032         fprintf (file, "%s", reg_names[REGNO (x)]);
8033       else if (GET_CODE (x) == MEM)
8034         {
8035           /* We need to handle PRE_INC and PRE_DEC here, since we need to
8036              know the width from the mode.  */
8037           if (GET_CODE (XEXP (x, 0)) == PRE_INC)
8038             fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
8039                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
8040           else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
8041             fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
8042                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
8043           else
8044             output_address (XEXP (x, 0));
8045         }
8046       else
8047         output_addr_const (file, x);
8048       return;
8049
8050     default:
8051       output_operand_lossage ("invalid %%xn code");
8052     }
8053 }
8054 \f
8055 /* Print the address of an operand.  */
8056
8057 void
8058 print_operand_address (file, x)
8059      FILE *file;
8060      rtx x;
8061 {
8062   if (GET_CODE (x) == REG)
8063     fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
8064   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
8065            || GET_CODE (x) == LABEL_REF)
8066     {
8067       output_addr_const (file, x);
8068       if (small_data_operand (x, GET_MODE (x)))
8069         fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
8070                  reg_names[SMALL_DATA_REG]);
8071       else if (TARGET_TOC)
8072         abort ();
8073     }
8074   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG)
8075     {
8076       if (REGNO (XEXP (x, 0)) == 0)
8077         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
8078                  reg_names[ REGNO (XEXP (x, 0)) ]);
8079       else
8080         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
8081                  reg_names[ REGNO (XEXP (x, 1)) ]);
8082     }
8083   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
8084     {
8085       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (XEXP (x, 1)));
8086       fprintf (file, "(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
8087     }
8088 #if TARGET_ELF
8089   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
8090            && CONSTANT_P (XEXP (x, 1)))
8091     {
8092       output_addr_const (file, XEXP (x, 1));
8093       fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
8094     }
8095 #endif
8096 #if TARGET_MACHO
8097   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
8098            && CONSTANT_P (XEXP (x, 1)))
8099     {
8100       fprintf (file, "lo16(");
8101       output_addr_const (file, XEXP (x, 1));
8102       fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
8103     }
8104 #endif
8105   else if (LEGITIMATE_CONSTANT_POOL_ADDRESS_P (x))
8106     {
8107       if (TARGET_AIX && (!TARGET_ELF || !TARGET_MINIMAL_TOC))
8108         {
8109           rtx contains_minus = XEXP (x, 1);
8110           rtx minus, symref;
8111           const char *name;
8112           
8113           /* Find the (minus (sym) (toc)) buried in X, and temporarily
8114              turn it into (sym) for output_addr_const.  */
8115           while (GET_CODE (XEXP (contains_minus, 0)) != MINUS)
8116             contains_minus = XEXP (contains_minus, 0);
8117
8118           minus = XEXP (contains_minus, 0);
8119           symref = XEXP (minus, 0);
8120           XEXP (contains_minus, 0) = symref;
8121           if (TARGET_ELF)
8122             {
8123               char *newname;
8124
8125               name = XSTR (symref, 0);
8126               newname = alloca (strlen (name) + sizeof ("@toc"));
8127               strcpy (newname, name);
8128               strcat (newname, "@toc");
8129               XSTR (symref, 0) = newname;
8130             }
8131           output_addr_const (file, XEXP (x, 1));
8132           if (TARGET_ELF)
8133             XSTR (symref, 0) = name;
8134           XEXP (contains_minus, 0) = minus;
8135         }
8136       else
8137         output_addr_const (file, XEXP (x, 1));
8138
8139       fprintf (file, "(%s)", reg_names[REGNO (XEXP (x, 0))]);
8140     }
8141   else
8142     abort ();
8143 }
8144 \f
8145 /* Target hook for assembling integer objects.  The PowerPC version has
8146    to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
8147    is defined.  It also needs to handle DI-mode objects on 64-bit
8148    targets.  */
8149
8150 static bool
8151 rs6000_assemble_integer (x, size, aligned_p)
8152      rtx x;
8153      unsigned int size;
8154      int aligned_p;
8155 {
8156 #ifdef RELOCATABLE_NEEDS_FIXUP
8157   /* Special handling for SI values.  */
8158   if (size == 4 && aligned_p)
8159     {
8160       extern int in_toc_section PARAMS ((void));
8161       static int recurse = 0;
8162       
8163       /* For -mrelocatable, we mark all addresses that need to be fixed up
8164          in the .fixup section.  */
8165       if (TARGET_RELOCATABLE
8166           && !in_toc_section ()
8167           && !in_text_section ()
8168           && !recurse
8169           && GET_CODE (x) != CONST_INT
8170           && GET_CODE (x) != CONST_DOUBLE
8171           && CONSTANT_P (x))
8172         {
8173           char buf[256];
8174
8175           recurse = 1;
8176           ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
8177           fixuplabelno++;
8178           ASM_OUTPUT_LABEL (asm_out_file, buf);
8179           fprintf (asm_out_file, "\t.long\t(");
8180           output_addr_const (asm_out_file, x);
8181           fprintf (asm_out_file, ")@fixup\n");
8182           fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
8183           ASM_OUTPUT_ALIGN (asm_out_file, 2);
8184           fprintf (asm_out_file, "\t.long\t");
8185           assemble_name (asm_out_file, buf);
8186           fprintf (asm_out_file, "\n\t.previous\n");
8187           recurse = 0;
8188           return true;
8189         }
8190       /* Remove initial .'s to turn a -mcall-aixdesc function
8191          address into the address of the descriptor, not the function
8192          itself.  */
8193       else if (GET_CODE (x) == SYMBOL_REF
8194                && XSTR (x, 0)[0] == '.'
8195                && DEFAULT_ABI == ABI_AIX)
8196         {
8197           const char *name = XSTR (x, 0);
8198           while (*name == '.')
8199             name++;
8200
8201           fprintf (asm_out_file, "\t.long\t%s\n", name);
8202           return true;
8203         }
8204     }
8205 #endif /* RELOCATABLE_NEEDS_FIXUP */
8206   return default_assemble_integer (x, size, aligned_p);
8207 }
8208
8209 #ifdef HAVE_GAS_HIDDEN
8210 /* Emit an assembler directive to set symbol visibility for DECL to
8211    VISIBILITY_TYPE.  */
8212
8213 static void
8214 rs6000_assemble_visibility (decl, vis)
8215      tree decl;
8216      int vis;
8217 {
8218   /* Functions need to have their entry point symbol visibility set as
8219      well as their descriptor symbol visibility.  */
8220   if (DEFAULT_ABI == ABI_AIX && TREE_CODE (decl) == FUNCTION_DECL)
8221     {
8222       static const char * const visibility_types[] = {
8223         NULL, "internal", "hidden", "protected"
8224       };
8225
8226       const char *name, *type;
8227
8228       name = ((* targetm.strip_name_encoding)
8229               (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
8230       type = visibility_types[vis];
8231
8232       fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
8233       fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
8234     }
8235   else
8236     default_assemble_visibility (decl, vis);
8237 }
8238 #endif
8239 \f
8240 enum rtx_code
8241 rs6000_reverse_condition (mode, code)
8242      enum machine_mode mode;
8243      enum rtx_code code;
8244 {
8245   /* Reversal of FP compares takes care -- an ordered compare
8246      becomes an unordered compare and vice versa.  */
8247   if (mode == CCFPmode && !flag_unsafe_math_optimizations)
8248     return reverse_condition_maybe_unordered (code);
8249   else
8250     return reverse_condition (code);
8251 }
8252
8253 /* Generate a compare for CODE.  Return a brand-new rtx that
8254    represents the result of the compare.  */
8255
8256 static rtx
8257 rs6000_generate_compare (code)
8258      enum rtx_code code;
8259 {
8260   enum machine_mode comp_mode;
8261   rtx compare_result;
8262
8263   if (rs6000_compare_fp_p)
8264     comp_mode = CCFPmode;
8265   else if (code == GTU || code == LTU
8266           || code == GEU || code == LEU)
8267     comp_mode = CCUNSmode;
8268   else
8269     comp_mode = CCmode;
8270
8271   /* First, the compare.  */
8272   compare_result = gen_reg_rtx (comp_mode);
8273
8274   /* SPE FP compare instructions on the GPRs.  Yuck!  */
8275   if ((TARGET_SPE && TARGET_HARD_FLOAT) && rs6000_compare_fp_p)
8276     {
8277       rtx cmp, or1, or2, or_result, compare_result2;
8278
8279       switch (code)
8280         {
8281         case EQ:
8282         case UNEQ:
8283         case NE:
8284         case LTGT:
8285           cmp = flag_unsafe_math_optimizations
8286             ? gen_tstsfeq_gpr (compare_result, rs6000_compare_op0,
8287                                rs6000_compare_op1)
8288             : gen_cmpsfeq_gpr (compare_result, rs6000_compare_op0,
8289                                rs6000_compare_op1);
8290           break;
8291         case GT:
8292         case GTU:
8293         case UNGT:
8294         case UNGE:
8295         case GE:
8296         case GEU:
8297           cmp = flag_unsafe_math_optimizations
8298             ? gen_tstsfgt_gpr (compare_result, rs6000_compare_op0,
8299                                rs6000_compare_op1)
8300             : gen_cmpsfgt_gpr (compare_result, rs6000_compare_op0,
8301                                rs6000_compare_op1);
8302           break;
8303         case LT:
8304         case LTU:
8305         case UNLT:
8306         case UNLE:
8307         case LE:
8308         case LEU:
8309           cmp = flag_unsafe_math_optimizations
8310             ? gen_tstsflt_gpr (compare_result, rs6000_compare_op0,
8311                                rs6000_compare_op1)
8312             : gen_cmpsflt_gpr (compare_result, rs6000_compare_op0,
8313                                rs6000_compare_op1);
8314           break;
8315         default:
8316           abort ();
8317         }
8318
8319       /* Synthesize LE and GE from LT/GT || EQ.  */
8320       if (code == LE || code == GE || code == LEU || code == GEU)
8321         {
8322           /* Synthesize GE/LE frome GT/LT || EQ.  */
8323
8324           emit_insn (cmp);
8325
8326           switch (code)
8327             {
8328             case LE: code = LT; break;
8329             case GE: code = GT; break;
8330             case LEU: code = LT; break;
8331             case GEU: code = GT; break;
8332             default: abort ();
8333             }
8334
8335           or1 = gen_reg_rtx (SImode);
8336           or2 = gen_reg_rtx (SImode);
8337           or_result = gen_reg_rtx (CCEQmode);
8338           compare_result2 = gen_reg_rtx (CCFPmode);
8339
8340           /* Do the EQ.  */
8341           cmp = flag_unsafe_math_optimizations
8342             ? gen_tstsfeq_gpr (compare_result2, rs6000_compare_op0,
8343                                rs6000_compare_op1)
8344             : gen_cmpsfeq_gpr (compare_result2, rs6000_compare_op0,
8345                                rs6000_compare_op1);
8346           emit_insn (cmp);
8347
8348           /* The MC8540 FP compare instructions set the CR bits
8349              differently than other PPC compare instructions.  For
8350              that matter, there is no generic test instruction, but a
8351              testgt, testlt, and testeq.  For a true condition, bit 2
8352              is set (x1xx) in the CR.  Following the traditional CR
8353              values:
8354
8355              LT    GT    EQ    OV
8356              bit3  bit2  bit1  bit0
8357
8358              ... bit 2 would be a GT CR alias, so later on we
8359              look in the GT bits for the branch instructins.
8360              However, we must be careful to emit correct RTL in
8361              the meantime, so optimizations don't get confused.  */
8362
8363           or1 = gen_rtx (NE, SImode, compare_result, const0_rtx);
8364           or2 = gen_rtx (NE, SImode, compare_result2, const0_rtx);
8365
8366           /* OR them together.  */
8367           cmp = gen_rtx_SET (VOIDmode, or_result,
8368                              gen_rtx_COMPARE (CCEQmode,
8369                                               gen_rtx_IOR (SImode, or1, or2),
8370                                               const_true_rtx));
8371           compare_result = or_result;
8372           code = EQ;
8373         }
8374       else
8375         {
8376           /* We only care about 1 bit (x1xx), so map everything to NE to
8377              maintain rtl sanity.  We'll get to the right bit (x1xx) at
8378              code output time.  */
8379           if (code == NE || code == LTGT)
8380             /* Do the inverse here because we have no cmpne
8381                instruction.  We use the cmpeq instruction and expect
8382                to get a 0 instead.  */
8383             code = EQ;
8384           else
8385             code = NE;
8386         }
8387
8388       emit_insn (cmp);
8389     }
8390   else
8391     emit_insn (gen_rtx_SET (VOIDmode, compare_result,
8392                             gen_rtx_COMPARE (comp_mode,
8393                                              rs6000_compare_op0, 
8394                                              rs6000_compare_op1)));
8395   
8396   /* Some kinds of FP comparisons need an OR operation;
8397      except for flag_unsafe_math_optimizations we don't bother.  */
8398   if (rs6000_compare_fp_p
8399       && ! flag_unsafe_math_optimizations
8400       && ! (TARGET_HARD_FLOAT && TARGET_SPE)
8401       && (code == LE || code == GE
8402           || code == UNEQ || code == LTGT
8403           || code == UNGT || code == UNLT))
8404     {
8405       enum rtx_code or1, or2;
8406       rtx or1_rtx, or2_rtx, compare2_rtx;
8407       rtx or_result = gen_reg_rtx (CCEQmode);
8408       
8409       switch (code)
8410         {
8411         case LE: or1 = LT;  or2 = EQ;  break;
8412         case GE: or1 = GT;  or2 = EQ;  break;
8413         case UNEQ: or1 = UNORDERED;  or2 = EQ;  break;
8414         case LTGT: or1 = LT;  or2 = GT;  break;
8415         case UNGT: or1 = UNORDERED;  or2 = GT;  break;
8416         case UNLT: or1 = UNORDERED;  or2 = LT;  break;
8417         default:  abort ();
8418         }
8419       validate_condition_mode (or1, comp_mode);
8420       validate_condition_mode (or2, comp_mode);
8421       or1_rtx = gen_rtx (or1, SImode, compare_result, const0_rtx);
8422       or2_rtx = gen_rtx (or2, SImode, compare_result, const0_rtx);
8423       compare2_rtx = gen_rtx_COMPARE (CCEQmode,
8424                                       gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
8425                                       const_true_rtx);
8426       emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
8427
8428       compare_result = or_result;
8429       code = EQ;
8430     }
8431
8432   validate_condition_mode (code, GET_MODE (compare_result));
8433   
8434   return gen_rtx (code, VOIDmode, compare_result, const0_rtx);
8435 }
8436
8437
8438 /* Emit the RTL for an sCOND pattern.  */
8439
8440 void
8441 rs6000_emit_sCOND (code, result)
8442      enum rtx_code code;
8443      rtx result;
8444 {
8445   rtx condition_rtx;
8446   enum machine_mode op_mode;
8447
8448   condition_rtx = rs6000_generate_compare (code);
8449
8450   op_mode = GET_MODE (rs6000_compare_op0);
8451   if (op_mode == VOIDmode)
8452     op_mode = GET_MODE (rs6000_compare_op1);
8453
8454   if (TARGET_POWERPC64 && (op_mode == DImode || rs6000_compare_fp_p))
8455     {
8456       PUT_MODE (condition_rtx, DImode);
8457       convert_move (result, condition_rtx, 0);
8458     }
8459   else
8460     {
8461       PUT_MODE (condition_rtx, SImode);
8462       emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
8463     }
8464 }
8465
8466 /* Emit a branch of kind CODE to location LOC.  */
8467
8468 void
8469 rs6000_emit_cbranch (code, loc)
8470      enum rtx_code code;
8471      rtx loc;
8472 {
8473   rtx condition_rtx, loc_ref;
8474
8475   condition_rtx = rs6000_generate_compare (code);
8476   loc_ref = gen_rtx_LABEL_REF (VOIDmode, loc);
8477   emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
8478                                gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
8479                                                      loc_ref, pc_rtx)));
8480 }
8481
8482 /* Return the string to output a conditional branch to LABEL, which is
8483    the operand number of the label, or -1 if the branch is really a
8484    conditional return.  
8485
8486    OP is the conditional expression.  XEXP (OP, 0) is assumed to be a
8487    condition code register and its mode specifies what kind of
8488    comparison we made.
8489
8490    REVERSED is nonzero if we should reverse the sense of the comparison.
8491
8492    INSN is the insn.  */
8493
8494 char *
8495 output_cbranch (op, label, reversed, insn)
8496      rtx op;
8497      const char * label;
8498      int reversed;
8499      rtx insn;
8500 {
8501   static char string[64];
8502   enum rtx_code code = GET_CODE (op);
8503   rtx cc_reg = XEXP (op, 0);
8504   enum machine_mode mode = GET_MODE (cc_reg);
8505   int cc_regno = REGNO (cc_reg) - CR0_REGNO;
8506   int need_longbranch = label != NULL && get_attr_length (insn) == 8;
8507   int really_reversed = reversed ^ need_longbranch;
8508   char *s = string;
8509   const char *ccode;
8510   const char *pred;
8511   rtx note;
8512
8513   validate_condition_mode (code, mode);
8514
8515   /* Work out which way this really branches.  We could use
8516      reverse_condition_maybe_unordered here always but this
8517      makes the resulting assembler clearer.  */
8518   if (really_reversed)
8519     {
8520       /* Reversal of FP compares takes care -- an ordered compare
8521          becomes an unordered compare and vice versa.  */
8522       if (mode == CCFPmode)
8523         code = reverse_condition_maybe_unordered (code);
8524       else
8525         code = reverse_condition (code);
8526     }
8527
8528   if ((TARGET_SPE && TARGET_HARD_FLOAT) && mode == CCFPmode)
8529     {
8530       /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
8531          to the GT bit.  */
8532       if (code == EQ)
8533         /* Opposite of GT.  */
8534         code = UNLE;
8535       else if (code == NE)
8536         code = GT;
8537       else
8538         abort ();
8539     }
8540
8541   switch (code)
8542     {
8543       /* Not all of these are actually distinct opcodes, but
8544          we distinguish them for clarity of the resulting assembler.  */
8545     case NE: case LTGT:
8546       ccode = "ne"; break;
8547     case EQ: case UNEQ:
8548       ccode = "eq"; break;
8549     case GE: case GEU: 
8550       ccode = "ge"; break;
8551     case GT: case GTU: case UNGT: 
8552       ccode = "gt"; break;
8553     case LE: case LEU: 
8554       ccode = "le"; break;
8555     case LT: case LTU: case UNLT: 
8556       ccode = "lt"; break;
8557     case UNORDERED: ccode = "un"; break;
8558     case ORDERED: ccode = "nu"; break;
8559     case UNGE: ccode = "nl"; break;
8560     case UNLE: ccode = "ng"; break;
8561     default:
8562       abort ();
8563     }
8564   
8565   /* Maybe we have a guess as to how likely the branch is.  
8566      The old mnemonics don't have a way to specify this information.  */
8567   pred = "";
8568   note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
8569   if (note != NULL_RTX)
8570     {
8571       /* PROB is the difference from 50%.  */
8572       int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
8573       bool always_hint = rs6000_cpu != PROCESSOR_POWER4;
8574
8575       /* Only hint for highly probable/improbable branches on newer
8576          cpus as static prediction overrides processor dynamic
8577          prediction.  For older cpus we may as well always hint, but
8578          assume not taken for branches that are very close to 50% as a
8579          mispredicted taken branch is more expensive than a
8580          mispredicted not-taken branch.  */ 
8581       if (always_hint
8582           || abs (prob) > REG_BR_PROB_BASE / 100 * 48)
8583         {
8584           if (abs (prob) > REG_BR_PROB_BASE / 20
8585               && ((prob > 0) ^ need_longbranch))
8586             pred = "+";
8587           else
8588             pred = "-";
8589         }
8590     }
8591
8592   if (label == NULL)
8593     s += sprintf (s, "{b%sr|b%slr%s} ", ccode, ccode, pred);
8594   else
8595     s += sprintf (s, "{b%s|b%s%s} ", ccode, ccode, pred);
8596
8597   /* We need to escape any '%' characters in the reg_names string.
8598      Assume they'd only be the first character...  */
8599   if (reg_names[cc_regno + CR0_REGNO][0] == '%')
8600     *s++ = '%';
8601   s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
8602
8603   if (label != NULL)
8604     {
8605       /* If the branch distance was too far, we may have to use an
8606          unconditional branch to go the distance.  */
8607       if (need_longbranch)
8608         s += sprintf (s, ",$+8\n\tb %s", label);
8609       else
8610         s += sprintf (s, ",%s", label);
8611     }
8612
8613   return string;
8614 }
8615
8616 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
8617    operands of the last comparison is nonzero/true, FALSE_COND if it
8618    is zero/false.  Return 0 if the hardware has no such operation.  */
8619
8620 int
8621 rs6000_emit_cmove (dest, op, true_cond, false_cond)
8622      rtx dest;
8623      rtx op;
8624      rtx true_cond;
8625      rtx false_cond;
8626 {
8627   enum rtx_code code = GET_CODE (op);
8628   rtx op0 = rs6000_compare_op0;
8629   rtx op1 = rs6000_compare_op1;
8630   REAL_VALUE_TYPE c1;
8631   enum machine_mode compare_mode = GET_MODE (op0);
8632   enum machine_mode result_mode = GET_MODE (dest);
8633   rtx temp;
8634
8635   /* These modes should always match. */
8636   if (GET_MODE (op1) != compare_mode
8637       /* In the isel case however, we can use a compare immediate, so
8638          op1 may be a small constant.  */
8639       && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
8640     return 0;
8641   if (GET_MODE (true_cond) != result_mode)
8642     return 0;
8643   if (GET_MODE (false_cond) != result_mode)
8644     return 0;
8645
8646   /* First, work out if the hardware can do this at all, or
8647      if it's too slow...  */
8648   if (! rs6000_compare_fp_p)
8649     {
8650       if (TARGET_ISEL)
8651         return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
8652       return 0;
8653     }
8654
8655   /* Eliminate half of the comparisons by switching operands, this
8656      makes the remaining code simpler.  */
8657   if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
8658       || code == LTGT || code == LT)
8659     {
8660       code = reverse_condition_maybe_unordered (code);
8661       temp = true_cond;
8662       true_cond = false_cond;
8663       false_cond = temp;
8664     }
8665
8666   /* UNEQ and LTGT take four instructions for a comparison with zero,
8667      it'll probably be faster to use a branch here too.  */
8668   if (code == UNEQ)
8669     return 0;
8670   
8671   if (GET_CODE (op1) == CONST_DOUBLE)
8672     REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
8673     
8674   /* We're going to try to implement comparions by performing
8675      a subtract, then comparing against zero.  Unfortunately,
8676      Inf - Inf is NaN which is not zero, and so if we don't
8677      know that the operand is finite and the comparison
8678      would treat EQ different to UNORDERED, we can't do it.  */
8679   if (! flag_unsafe_math_optimizations
8680       && code != GT && code != UNGE
8681       && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
8682       /* Constructs of the form (a OP b ? a : b) are safe.  */
8683       && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
8684           || (! rtx_equal_p (op0, true_cond) 
8685               && ! rtx_equal_p (op1, true_cond))))
8686     return 0;
8687   /* At this point we know we can use fsel.  */
8688
8689   /* Reduce the comparison to a comparison against zero.  */
8690   temp = gen_reg_rtx (compare_mode);
8691   emit_insn (gen_rtx_SET (VOIDmode, temp,
8692                           gen_rtx_MINUS (compare_mode, op0, op1)));
8693   op0 = temp;
8694   op1 = CONST0_RTX (compare_mode);
8695
8696   /* If we don't care about NaNs we can reduce some of the comparisons
8697      down to faster ones.  */
8698   if (flag_unsafe_math_optimizations)
8699     switch (code)
8700       {
8701       case GT:
8702         code = LE;
8703         temp = true_cond;
8704         true_cond = false_cond;
8705         false_cond = temp;
8706         break;
8707       case UNGE:
8708         code = GE;
8709         break;
8710       case UNEQ:
8711         code = EQ;
8712         break;
8713       default:
8714         break;
8715       }
8716
8717   /* Now, reduce everything down to a GE.  */
8718   switch (code)
8719     {
8720     case GE:
8721       break;
8722
8723     case LE:
8724       temp = gen_reg_rtx (compare_mode);
8725       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
8726       op0 = temp;
8727       break;
8728
8729     case ORDERED:
8730       temp = gen_reg_rtx (compare_mode);
8731       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
8732       op0 = temp;
8733       break;
8734
8735     case EQ:
8736       temp = gen_reg_rtx (compare_mode);
8737       emit_insn (gen_rtx_SET (VOIDmode, temp, 
8738                               gen_rtx_NEG (compare_mode,
8739                                            gen_rtx_ABS (compare_mode, op0))));
8740       op0 = temp;
8741       break;
8742
8743     case UNGE:
8744       temp = gen_reg_rtx (result_mode);
8745       emit_insn (gen_rtx_SET (VOIDmode, temp,
8746                               gen_rtx_IF_THEN_ELSE (result_mode,
8747                                                     gen_rtx_GE (VOIDmode,
8748                                                                 op0, op1),
8749                                                     true_cond, false_cond)));
8750       false_cond = temp;
8751       true_cond = false_cond;
8752
8753       temp = gen_reg_rtx (compare_mode);
8754       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
8755       op0 = temp;
8756       break;
8757
8758     case GT:
8759       temp = gen_reg_rtx (result_mode);
8760       emit_insn (gen_rtx_SET (VOIDmode, temp,
8761                               gen_rtx_IF_THEN_ELSE (result_mode, 
8762                                                     gen_rtx_GE (VOIDmode,
8763                                                                 op0, op1),
8764                                                     true_cond, false_cond)));
8765       true_cond = temp;
8766       false_cond = true_cond;
8767
8768       temp = gen_reg_rtx (compare_mode);
8769       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
8770       op0 = temp;
8771       break;
8772
8773     default:
8774       abort ();
8775     }
8776
8777   emit_insn (gen_rtx_SET (VOIDmode, dest,
8778                           gen_rtx_IF_THEN_ELSE (result_mode,
8779                                                 gen_rtx_GE (VOIDmode,
8780                                                             op0, op1),
8781                                                 true_cond, false_cond)));
8782   return 1;
8783 }
8784
8785 /* Same as above, but for ints (isel).  */
8786
8787 static int
8788 rs6000_emit_int_cmove (dest, op, true_cond, false_cond)
8789      rtx dest;
8790      rtx op;
8791      rtx true_cond;
8792      rtx false_cond;
8793 {
8794   rtx condition_rtx, cr;
8795
8796   /* All isel implementations thus far are 32-bits.  */
8797   if (GET_MODE (rs6000_compare_op0) != SImode)
8798     return 0;
8799
8800   /* We still have to do the compare, because isel doesn't do a
8801      compare, it just looks at the CRx bits set by a previous compare
8802      instruction.  */
8803   condition_rtx = rs6000_generate_compare (GET_CODE (op));
8804   cr = XEXP (condition_rtx, 0);
8805
8806   if (GET_MODE (cr) == CCmode)
8807     emit_insn (gen_isel_signed (dest, condition_rtx,
8808                                 true_cond, false_cond, cr));
8809   else
8810     emit_insn (gen_isel_unsigned (dest, condition_rtx,
8811                                   true_cond, false_cond, cr));
8812
8813   return 1;
8814 }
8815
8816 const char *
8817 output_isel (operands)
8818      rtx *operands;
8819 {
8820   enum rtx_code code;
8821
8822   code = GET_CODE (operands[1]);
8823   if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
8824     {
8825       PUT_CODE (operands[1], reverse_condition (code));
8826       return "isel %0,%3,%2,%j1";
8827     }
8828   else
8829     return "isel %0,%2,%3,%j1";
8830 }
8831
8832 void
8833 rs6000_emit_minmax (dest, code, op0, op1)
8834      rtx dest;
8835      enum rtx_code code;
8836      rtx op0;
8837      rtx op1;
8838 {
8839   enum machine_mode mode = GET_MODE (op0);
8840   enum rtx_code c;
8841   rtx target;
8842
8843   if (code == SMAX || code == SMIN)
8844     c = GE;
8845   else
8846     c = GEU;
8847
8848   if (code == SMAX || code == UMAX)
8849     target = emit_conditional_move (dest, c, op0, op1, mode, 
8850                                     op0, op1, mode, 0);
8851   else
8852     target = emit_conditional_move (dest, c, op0, op1, mode, 
8853                                     op1, op0, mode, 0);
8854   if (target == NULL_RTX)
8855     abort ();
8856   if (target != dest)
8857     emit_move_insn (dest, target);
8858 }
8859 \f
8860 /* This page contains routines that are used to determine what the
8861    function prologue and epilogue code will do and write them out.  */
8862
8863 /* Return the first fixed-point register that is required to be
8864    saved. 32 if none.  */
8865
8866 int
8867 first_reg_to_save ()
8868 {
8869   int first_reg;
8870
8871   /* Find lowest numbered live register.  */
8872   for (first_reg = 13; first_reg <= 31; first_reg++)
8873     if (regs_ever_live[first_reg] 
8874         && (! call_used_regs[first_reg]
8875             || (first_reg == RS6000_PIC_OFFSET_TABLE_REGNUM
8876                 && ((DEFAULT_ABI == ABI_V4 && flag_pic == 1)
8877                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic)))))
8878       break;
8879
8880 #if TARGET_MACHO
8881   if (flag_pic
8882       && current_function_uses_pic_offset_table
8883       && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
8884     return RS6000_PIC_OFFSET_TABLE_REGNUM;
8885 #endif
8886
8887   return first_reg;
8888 }
8889
8890 /* Similar, for FP regs.  */
8891
8892 int
8893 first_fp_reg_to_save ()
8894 {
8895   int first_reg;
8896
8897   /* Find lowest numbered live register.  */
8898   for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
8899     if (regs_ever_live[first_reg])
8900       break;
8901
8902   return first_reg;
8903 }
8904
8905 /* Similar, for AltiVec regs.  */
8906
8907 static int
8908 first_altivec_reg_to_save ()
8909 {
8910   int i;
8911
8912   /* Stack frame remains as is unless we are in AltiVec ABI.  */
8913   if (! TARGET_ALTIVEC_ABI)
8914     return LAST_ALTIVEC_REGNO + 1;
8915
8916   /* Find lowest numbered live register.  */
8917   for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
8918     if (regs_ever_live[i])
8919       break;
8920
8921   return i;
8922 }
8923
8924 /* Return a 32-bit mask of the AltiVec registers we need to set in
8925    VRSAVE.  Bit n of the return value is 1 if Vn is live.  The MSB in
8926    the 32-bit word is 0.  */
8927
8928 static unsigned int
8929 compute_vrsave_mask ()
8930 {
8931   unsigned int i, mask = 0;
8932
8933   /* First, find out if we use _any_ altivec registers.  */
8934   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
8935     if (regs_ever_live[i])
8936       mask |= ALTIVEC_REG_BIT (i);
8937
8938   if (mask == 0)
8939     return mask;
8940
8941   /* Next, add all registers that are call-clobbered.  We do this
8942      because post-reload register optimizers such as regrename_optimize
8943      may choose to use them.  They never change the register class
8944      chosen by reload, so cannot create new uses of altivec registers
8945      if there were none before, so the early exit above is safe.  */
8946   /* ??? Alternately, we could define HARD_REGNO_RENAME_OK to disallow
8947      altivec registers not saved in the mask, which might well make the
8948      adjustments below more effective in eliding the save/restore of
8949      VRSAVE in small functions.  */
8950   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
8951     if (call_used_regs[i])
8952       mask |= ALTIVEC_REG_BIT (i);
8953
8954   /* Next, remove the argument registers from the set.  These must
8955      be in the VRSAVE mask set by the caller, so we don't need to add
8956      them in again.  More importantly, the mask we compute here is
8957      used to generate CLOBBERs in the set_vrsave insn, and we do not
8958      wish the argument registers to die.  */
8959   for (i = cfun->args_info.vregno; i >= ALTIVEC_ARG_MIN_REG; --i)
8960     mask &= ~ALTIVEC_REG_BIT (i);
8961
8962   /* Similarly, remove the return value from the set.  */
8963   {
8964     bool yes = false;
8965     diddle_return_value (is_altivec_return_reg, &yes);
8966     if (yes)
8967       mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
8968   }
8969
8970   return mask;
8971 }
8972
8973 static void
8974 is_altivec_return_reg (reg, xyes)
8975      rtx reg;
8976      void *xyes;
8977 {
8978   bool *yes = (bool *) xyes;
8979   if (REGNO (reg) == ALTIVEC_ARG_RETURN)
8980     *yes = true;
8981 }
8982
8983 \f
8984 /* Calculate the stack information for the current function.  This is
8985    complicated by having two separate calling sequences, the AIX calling
8986    sequence and the V.4 calling sequence.
8987
8988    AIX (and Darwin/Mac OS X) stack frames look like:
8989                                                           32-bit  64-bit
8990         SP----> +---------------------------------------+
8991                 | back chain to caller                  | 0       0
8992                 +---------------------------------------+
8993                 | saved CR                              | 4       8 (8-11)
8994                 +---------------------------------------+
8995                 | saved LR                              | 8       16
8996                 +---------------------------------------+
8997                 | reserved for compilers                | 12      24
8998                 +---------------------------------------+
8999                 | reserved for binders                  | 16      32
9000                 +---------------------------------------+
9001                 | saved TOC pointer                     | 20      40
9002                 +---------------------------------------+
9003                 | Parameter save area (P)               | 24      48
9004                 +---------------------------------------+
9005                 | Alloca space (A)                      | 24+P    etc.
9006                 +---------------------------------------+
9007                 | Local variable space (L)              | 24+P+A
9008                 +---------------------------------------+
9009                 | Float/int conversion temporary (X)    | 24+P+A+L
9010                 +---------------------------------------+
9011                 | Save area for AltiVec registers (W)   | 24+P+A+L+X
9012                 +---------------------------------------+
9013                 | AltiVec alignment padding (Y)         | 24+P+A+L+X+W
9014                 +---------------------------------------+
9015                 | Save area for VRSAVE register (Z)     | 24+P+A+L+X+W+Y
9016                 +---------------------------------------+
9017                 | Save area for GP registers (G)        | 24+P+A+X+L+X+W+Y+Z
9018                 +---------------------------------------+
9019                 | Save area for FP registers (F)        | 24+P+A+X+L+X+W+Y+Z+G
9020                 +---------------------------------------+
9021         old SP->| back chain to caller's caller         |
9022                 +---------------------------------------+
9023
9024    The required alignment for AIX configurations is two words (i.e., 8
9025    or 16 bytes).
9026
9027
9028    V.4 stack frames look like:
9029
9030         SP----> +---------------------------------------+
9031                 | back chain to caller                  | 0
9032                 +---------------------------------------+
9033                 | caller's saved LR                     | 4
9034                 +---------------------------------------+
9035                 | Parameter save area (P)               | 8
9036                 +---------------------------------------+
9037                 | Alloca space (A)                      | 8+P
9038                 +---------------------------------------+    
9039                 | Varargs save area (V)                 | 8+P+A
9040                 +---------------------------------------+    
9041                 | Local variable space (L)              | 8+P+A+V
9042                 +---------------------------------------+    
9043                 | Float/int conversion temporary (X)    | 8+P+A+V+L
9044                 +---------------------------------------+
9045                 | Save area for AltiVec registers (W)   | 8+P+A+V+L+X
9046                 +---------------------------------------+
9047                 | AltiVec alignment padding (Y)         | 8+P+A+V+L+X+W
9048                 +---------------------------------------+
9049                 | Save area for VRSAVE register (Z)     | 8+P+A+V+L+X+W+Y
9050                 +---------------------------------------+
9051                 | SPE: area for 64-bit GP registers     |
9052                 +---------------------------------------+
9053                 | SPE alignment padding                 |
9054                 +---------------------------------------+
9055                 | saved CR (C)                          | 8+P+A+V+L+X+W+Y+Z
9056                 +---------------------------------------+    
9057                 | Save area for GP registers (G)        | 8+P+A+V+L+X+W+Y+Z+C
9058                 +---------------------------------------+    
9059                 | Save area for FP registers (F)        | 8+P+A+V+L+X+W+Y+Z+C+G
9060                 +---------------------------------------+
9061         old SP->| back chain to caller's caller         |
9062                 +---------------------------------------+
9063
9064    The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
9065    given.  (But note below and in sysv4.h that we require only 8 and
9066    may round up the size of our stack frame anyways.  The historical
9067    reason is early versions of powerpc-linux which didn't properly
9068    align the stack at program startup.  A happy side-effect is that
9069    -mno-eabi libraries can be used with -meabi programs.)
9070
9071    The EABI configuration defaults to the V.4 layout, unless
9072    -mcall-aix is used, in which case the AIX layout is used.  However,
9073    the stack alignment requirements may differ.  If -mno-eabi is not
9074    given, the required stack alignment is 8 bytes; if -mno-eabi is
9075    given, the required alignment is 16 bytes.  (But see V.4 comment
9076    above.)  */
9077
9078 #ifndef ABI_STACK_BOUNDARY
9079 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
9080 #endif
9081
9082 rs6000_stack_t *
9083 rs6000_stack_info ()
9084 {
9085   static rs6000_stack_t info, zero_info;
9086   rs6000_stack_t *info_ptr = &info;
9087   int reg_size = TARGET_POWERPC64 ? 8 : 4;
9088   enum rs6000_abi abi;
9089   int ehrd_size;
9090   int total_raw_size;
9091
9092   /* Zero all fields portably.  */
9093   info = zero_info;
9094
9095   /* Select which calling sequence.  */
9096   info_ptr->abi = abi = DEFAULT_ABI;
9097
9098   /* Calculate which registers need to be saved & save area size.  */
9099   info_ptr->first_gp_reg_save = first_reg_to_save ();
9100   /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM, 
9101      even if it currently looks like we won't.  */
9102   if (((TARGET_TOC && TARGET_MINIMAL_TOC)
9103        || (flag_pic == 1 && abi == ABI_V4)
9104        || (flag_pic && abi == ABI_DARWIN))
9105       && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
9106     info_ptr->gp_size = reg_size * (32 - RS6000_PIC_OFFSET_TABLE_REGNUM);
9107   else
9108     info_ptr->gp_size = reg_size * (32 - info_ptr->first_gp_reg_save);
9109
9110   /* For the SPE, we have an additional upper 32-bits on each GPR.
9111      Ideally we should save the entire 64-bits only when the upper
9112      half is used in SIMD instructions.  Since we only record
9113      registers live (not the size they are used in), this proves
9114      difficult because we'd have to traverse the instruction chain at
9115      the right time, taking reload into account.  This is a real pain,
9116      so we opt to save the GPRs in 64-bits always.  Anyone overly
9117      concerned with frame size can fix this.  ;-).
9118
9119      So... since we save all GPRs (except the SP) in 64-bits, the
9120      traditional GP save area will be empty.  */
9121   if (TARGET_SPE_ABI)
9122     info_ptr->gp_size = 0;
9123
9124   info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
9125   info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
9126
9127   info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
9128   info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
9129                                  - info_ptr->first_altivec_reg_save);
9130
9131   /* Does this function call anything?  */
9132   info_ptr->calls_p = (! current_function_is_leaf
9133                        || cfun->machine->ra_needs_full_frame);
9134
9135   /* Determine if we need to save the link register.  */
9136   if (rs6000_ra_ever_killed ()
9137       || (DEFAULT_ABI == ABI_AIX && current_function_profile)
9138 #ifdef TARGET_RELOCATABLE
9139       || (TARGET_RELOCATABLE && (get_pool_size () != 0))
9140 #endif
9141       || (info_ptr->first_fp_reg_save != 64
9142           && !FP_SAVE_INLINE (info_ptr->first_fp_reg_save))
9143       || info_ptr->first_altivec_reg_save <= LAST_ALTIVEC_REGNO
9144       || (abi == ABI_V4 && current_function_calls_alloca)
9145       || (DEFAULT_ABI == ABI_DARWIN
9146           && flag_pic
9147           && current_function_uses_pic_offset_table)
9148       || info_ptr->calls_p)
9149     {
9150       info_ptr->lr_save_p = 1;
9151       regs_ever_live[LINK_REGISTER_REGNUM] = 1;
9152     }
9153
9154   /* Determine if we need to save the condition code registers.  */
9155   if (regs_ever_live[CR2_REGNO] 
9156       || regs_ever_live[CR3_REGNO]
9157       || regs_ever_live[CR4_REGNO])
9158     {
9159       info_ptr->cr_save_p = 1;
9160       if (abi == ABI_V4)
9161         info_ptr->cr_size = reg_size;
9162     }
9163
9164   /* If the current function calls __builtin_eh_return, then we need
9165      to allocate stack space for registers that will hold data for
9166      the exception handler.  */
9167   if (current_function_calls_eh_return)
9168     {
9169       unsigned int i;
9170       for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
9171         continue;
9172
9173       /* SPE saves EH registers in 64-bits.  */
9174       ehrd_size = i * (TARGET_SPE_ABI ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
9175     }
9176   else
9177     ehrd_size = 0;
9178
9179   /* Determine various sizes.  */
9180   info_ptr->reg_size     = reg_size;
9181   info_ptr->fixed_size   = RS6000_SAVE_AREA;
9182   info_ptr->varargs_size = RS6000_VARARGS_AREA;
9183   info_ptr->vars_size    = RS6000_ALIGN (get_frame_size (), 8);
9184   info_ptr->parm_size    = RS6000_ALIGN (current_function_outgoing_args_size,
9185                                          8);
9186
9187   if (TARGET_SPE_ABI)
9188     info_ptr->spe_gp_size = 8 * (32 - info_ptr->first_gp_reg_save);
9189   else
9190     info_ptr->spe_gp_size = 0;
9191
9192   if (TARGET_ALTIVEC_ABI && TARGET_ALTIVEC_VRSAVE)
9193     {
9194       info_ptr->vrsave_mask = compute_vrsave_mask ();
9195       info_ptr->vrsave_size  = info_ptr->vrsave_mask ? 4 : 0;
9196     }
9197   else
9198     {
9199       info_ptr->vrsave_mask = 0;
9200       info_ptr->vrsave_size = 0;
9201     }
9202
9203   /* Calculate the offsets.  */
9204   switch (abi)
9205     {
9206     case ABI_NONE:
9207     default:
9208       abort ();
9209
9210     case ABI_AIX:
9211     case ABI_AIX_NODESC:
9212     case ABI_DARWIN:
9213       info_ptr->fp_save_offset   = - info_ptr->fp_size;
9214       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
9215
9216       if (TARGET_ALTIVEC_ABI)
9217         {
9218           info_ptr->vrsave_save_offset
9219             = info_ptr->gp_save_offset - info_ptr->vrsave_size;
9220
9221           /* Align stack so vector save area is on a quadword boundary.  */
9222           if (info_ptr->altivec_size != 0)
9223             info_ptr->altivec_padding_size
9224               = 16 - (-info_ptr->vrsave_save_offset % 16);
9225           else
9226             info_ptr->altivec_padding_size = 0;
9227
9228           info_ptr->altivec_save_offset
9229             = info_ptr->vrsave_save_offset
9230             - info_ptr->altivec_padding_size
9231             - info_ptr->altivec_size;
9232
9233           /* Adjust for AltiVec case.  */
9234           info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
9235         }
9236       else
9237         info_ptr->ehrd_offset      = info_ptr->gp_save_offset - ehrd_size;
9238       info_ptr->cr_save_offset   = reg_size; /* first word when 64-bit.  */
9239       info_ptr->lr_save_offset   = 2*reg_size;
9240       break;
9241
9242     case ABI_V4:
9243       info_ptr->fp_save_offset   = - info_ptr->fp_size;
9244       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
9245       info_ptr->cr_save_offset   = info_ptr->gp_save_offset - info_ptr->cr_size;
9246
9247       if (TARGET_SPE_ABI)
9248       {
9249         /* Align stack so SPE GPR save area is aligned on a
9250            double-word boundary.  */
9251         if (info_ptr->spe_gp_size != 0)
9252           info_ptr->spe_padding_size
9253             = 8 - (-info_ptr->cr_save_offset % 8);
9254         else
9255           info_ptr->spe_padding_size = 0;
9256
9257         info_ptr->spe_gp_save_offset
9258           = info_ptr->cr_save_offset
9259           - info_ptr->spe_padding_size
9260           - info_ptr->spe_gp_size;
9261
9262         /* Adjust for SPE case.  */
9263         info_ptr->toc_save_offset
9264           = info_ptr->spe_gp_save_offset - info_ptr->toc_size;
9265       }
9266       else if (TARGET_ALTIVEC_ABI)
9267         {
9268           info_ptr->vrsave_save_offset
9269             = info_ptr->cr_save_offset - info_ptr->vrsave_size;
9270
9271           /* Align stack so vector save area is on a quadword boundary.  */
9272           if (info_ptr->altivec_size != 0)
9273             info_ptr->altivec_padding_size
9274               = 16 - (-info_ptr->vrsave_save_offset % 16);
9275           else
9276             info_ptr->altivec_padding_size = 0;
9277
9278           info_ptr->altivec_save_offset
9279             = info_ptr->vrsave_save_offset
9280             - info_ptr->altivec_padding_size
9281             - info_ptr->altivec_size;
9282
9283           /* Adjust for AltiVec case.  */
9284           info_ptr->toc_save_offset
9285             = info_ptr->altivec_save_offset - info_ptr->toc_size;
9286         }
9287       else
9288         info_ptr->toc_save_offset  = info_ptr->cr_save_offset - info_ptr->toc_size;
9289       info_ptr->ehrd_offset      = info_ptr->toc_save_offset - ehrd_size;
9290       info_ptr->lr_save_offset   = reg_size;
9291       break;
9292     }
9293
9294   info_ptr->save_size    = RS6000_ALIGN (info_ptr->fp_size
9295                                          + info_ptr->gp_size
9296                                          + info_ptr->altivec_size
9297                                          + info_ptr->altivec_padding_size
9298                                          + info_ptr->vrsave_size
9299                                          + info_ptr->spe_gp_size
9300                                          + info_ptr->spe_padding_size
9301                                          + ehrd_size
9302                                          + info_ptr->cr_size
9303                                          + info_ptr->lr_size
9304                                          + info_ptr->vrsave_size
9305                                          + info_ptr->toc_size,
9306                                          (TARGET_ALTIVEC_ABI || ABI_DARWIN)
9307                                          ? 16 : 8);
9308
9309   total_raw_size         = (info_ptr->vars_size
9310                             + info_ptr->parm_size
9311                             + info_ptr->save_size
9312                             + info_ptr->varargs_size
9313                             + info_ptr->fixed_size);
9314
9315   info_ptr->total_size =
9316     RS6000_ALIGN (total_raw_size, ABI_STACK_BOUNDARY / BITS_PER_UNIT);
9317
9318   /* Determine if we need to allocate any stack frame:
9319
9320      For AIX we need to push the stack if a frame pointer is needed
9321      (because the stack might be dynamically adjusted), if we are
9322      debugging, if we make calls, or if the sum of fp_save, gp_save,
9323      and local variables are more than the space needed to save all
9324      non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
9325      + 18*8 = 288 (GPR13 reserved).
9326
9327      For V.4 we don't have the stack cushion that AIX uses, but assume
9328      that the debugger can handle stackless frames.  */
9329
9330   if (info_ptr->calls_p)
9331     info_ptr->push_p = 1;
9332
9333   else if (abi == ABI_V4)
9334     info_ptr->push_p = total_raw_size > info_ptr->fixed_size;
9335
9336   else
9337     info_ptr->push_p = (frame_pointer_needed
9338                         || (abi != ABI_DARWIN && write_symbols != NO_DEBUG)
9339                         || ((total_raw_size - info_ptr->fixed_size)
9340                             > (TARGET_32BIT ? 220 : 288)));
9341
9342   /* Zero offsets if we're not saving those registers.  */
9343   if (info_ptr->fp_size == 0)
9344     info_ptr->fp_save_offset = 0;
9345
9346   if (info_ptr->gp_size == 0)
9347     info_ptr->gp_save_offset = 0;
9348
9349   if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
9350     info_ptr->altivec_save_offset = 0;
9351
9352   if (! TARGET_ALTIVEC_ABI || info_ptr->vrsave_mask == 0)
9353     info_ptr->vrsave_save_offset = 0;
9354
9355   if (! TARGET_SPE_ABI || info_ptr->spe_gp_size == 0)
9356     info_ptr->spe_gp_save_offset = 0;
9357
9358   if (! info_ptr->lr_save_p)
9359     info_ptr->lr_save_offset = 0;
9360
9361   if (! info_ptr->cr_save_p)
9362     info_ptr->cr_save_offset = 0;
9363
9364   if (! info_ptr->toc_save_p)
9365     info_ptr->toc_save_offset = 0;
9366
9367   return info_ptr;
9368 }
9369
9370 void
9371 debug_stack_info (info)
9372      rs6000_stack_t *info;
9373 {
9374   const char *abi_string;
9375
9376   if (! info)
9377     info = rs6000_stack_info ();
9378
9379   fprintf (stderr, "\nStack information for function %s:\n",
9380            ((current_function_decl && DECL_NAME (current_function_decl))
9381             ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
9382             : "<unknown>"));
9383
9384   switch (info->abi)
9385     {
9386     default:             abi_string = "Unknown";        break;
9387     case ABI_NONE:       abi_string = "NONE";           break;
9388     case ABI_AIX:
9389     case ABI_AIX_NODESC: abi_string = "AIX";            break;
9390     case ABI_DARWIN:     abi_string = "Darwin";         break;
9391     case ABI_V4:         abi_string = "V.4";            break;
9392     }
9393
9394   fprintf (stderr, "\tABI                 = %5s\n", abi_string);
9395
9396   if (TARGET_ALTIVEC_ABI)
9397     fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
9398
9399   if (TARGET_SPE_ABI)
9400     fprintf (stderr, "\tSPE ABI extensions enabled.\n");
9401
9402   if (info->first_gp_reg_save != 32)
9403     fprintf (stderr, "\tfirst_gp_reg_save   = %5d\n", info->first_gp_reg_save);
9404
9405   if (info->first_fp_reg_save != 64)
9406     fprintf (stderr, "\tfirst_fp_reg_save   = %5d\n", info->first_fp_reg_save);
9407
9408   if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
9409     fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
9410              info->first_altivec_reg_save);
9411
9412   if (info->lr_save_p)
9413     fprintf (stderr, "\tlr_save_p           = %5d\n", info->lr_save_p);
9414
9415   if (info->cr_save_p)
9416     fprintf (stderr, "\tcr_save_p           = %5d\n", info->cr_save_p);
9417
9418   if (info->toc_save_p)
9419     fprintf (stderr, "\ttoc_save_p          = %5d\n", info->toc_save_p);
9420
9421   if (info->vrsave_mask)
9422     fprintf (stderr, "\tvrsave_mask         = 0x%x\n", info->vrsave_mask);
9423
9424   if (info->push_p)
9425     fprintf (stderr, "\tpush_p              = %5d\n", info->push_p);
9426
9427   if (info->calls_p)
9428     fprintf (stderr, "\tcalls_p             = %5d\n", info->calls_p);
9429
9430   if (info->gp_save_offset)
9431     fprintf (stderr, "\tgp_save_offset      = %5d\n", info->gp_save_offset);
9432
9433   if (info->fp_save_offset)
9434     fprintf (stderr, "\tfp_save_offset      = %5d\n", info->fp_save_offset);
9435
9436   if (info->altivec_save_offset)
9437     fprintf (stderr, "\taltivec_save_offset = %5d\n",
9438              info->altivec_save_offset);
9439
9440   if (info->spe_gp_save_offset)
9441     fprintf (stderr, "\tspe_gp_save_offset  = %5d\n",
9442              info->spe_gp_save_offset);
9443
9444   if (info->vrsave_save_offset)
9445     fprintf (stderr, "\tvrsave_save_offset  = %5d\n",
9446              info->vrsave_save_offset);
9447
9448   if (info->lr_save_offset)
9449     fprintf (stderr, "\tlr_save_offset      = %5d\n", info->lr_save_offset);
9450
9451   if (info->cr_save_offset)
9452     fprintf (stderr, "\tcr_save_offset      = %5d\n", info->cr_save_offset);
9453
9454   if (info->toc_save_offset)
9455     fprintf (stderr, "\ttoc_save_offset     = %5d\n", info->toc_save_offset);
9456
9457   if (info->varargs_save_offset)
9458     fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
9459
9460   if (info->total_size)
9461     fprintf (stderr, "\ttotal_size          = %5d\n", info->total_size);
9462
9463   if (info->varargs_size)
9464     fprintf (stderr, "\tvarargs_size        = %5d\n", info->varargs_size);
9465
9466   if (info->vars_size)
9467     fprintf (stderr, "\tvars_size           = %5d\n", info->vars_size);
9468
9469   if (info->parm_size)
9470     fprintf (stderr, "\tparm_size           = %5d\n", info->parm_size);
9471
9472   if (info->fixed_size)
9473     fprintf (stderr, "\tfixed_size          = %5d\n", info->fixed_size);
9474
9475   if (info->gp_size)
9476     fprintf (stderr, "\tgp_size             = %5d\n", info->gp_size);
9477
9478   if (info->spe_gp_size)
9479     fprintf (stderr, "\tspe_gp_size         = %5d\n", info->spe_gp_size);
9480
9481   if (info->fp_size)
9482     fprintf (stderr, "\tfp_size             = %5d\n", info->fp_size);
9483
9484   if (info->altivec_size)
9485     fprintf (stderr, "\taltivec_size        = %5d\n", info->altivec_size);
9486
9487   if (info->vrsave_size)
9488     fprintf (stderr, "\tvrsave_size         = %5d\n", info->vrsave_size);
9489
9490   if (info->altivec_padding_size)
9491     fprintf (stderr, "\taltivec_padding_size= %5d\n",
9492              info->altivec_padding_size);
9493
9494   if (info->spe_padding_size)
9495     fprintf (stderr, "\tspe_padding_size    = %5d\n",
9496              info->spe_padding_size);
9497
9498   if (info->lr_size)
9499     fprintf (stderr, "\tlr_size             = %5d\n", info->lr_size);
9500
9501   if (info->cr_size)
9502     fprintf (stderr, "\tcr_size             = %5d\n", info->cr_size);
9503
9504   if (info->toc_size)
9505     fprintf (stderr, "\ttoc_size            = %5d\n", info->toc_size);
9506
9507   if (info->save_size)
9508     fprintf (stderr, "\tsave_size           = %5d\n", info->save_size);
9509
9510   if (info->reg_size != 4)
9511     fprintf (stderr, "\treg_size            = %5d\n", info->reg_size);
9512
9513   fprintf (stderr, "\n");
9514 }
9515
9516 rtx
9517 rs6000_return_addr (count, frame)
9518      int count;
9519      rtx frame;
9520 {
9521   /* Currently we don't optimize very well between prolog and body
9522      code and for PIC code the code can be actually quite bad, so
9523      don't try to be too clever here.  */
9524   if (count != 0 || flag_pic != 0)
9525     {
9526       cfun->machine->ra_needs_full_frame = 1;
9527
9528       return
9529         gen_rtx_MEM
9530           (Pmode,
9531            memory_address
9532            (Pmode,
9533             plus_constant (copy_to_reg
9534                            (gen_rtx_MEM (Pmode,
9535                                          memory_address (Pmode, frame))),
9536                            RETURN_ADDRESS_OFFSET)));
9537     }
9538
9539   return get_hard_reg_initial_val (Pmode, LINK_REGISTER_REGNUM);
9540 }
9541
9542 /* Say whether a function is a candidate for sibcall handling or not.
9543    We do not allow indirect calls to be optimized into sibling calls.
9544    Also, we can't do it if there are any vector parameters; there's
9545    nowhere to put the VRsave code so it works; note that functions with
9546    vector parameters are required to have a prototype, so the argument
9547    type info must be available here.  (The tail recursion case can work
9548    with vector parameters, but there's no way to distinguish here.) */
9549 static bool
9550 rs6000_function_ok_for_sibcall (decl, exp)
9551     tree decl;
9552     tree exp ATTRIBUTE_UNUSED;
9553 {
9554   tree type;
9555   if (decl)
9556     {
9557       if (TARGET_ALTIVEC_VRSAVE)
9558         {
9559           for (type = TYPE_ARG_TYPES (TREE_TYPE (decl));
9560                type; type = TREE_CHAIN (type))
9561             {
9562               if (TREE_CODE (TREE_VALUE (type)) == VECTOR_TYPE)
9563                 return false;
9564             }
9565         }
9566       if (DEFAULT_ABI == ABI_DARWIN
9567           || (*targetm.binds_local_p) (decl))
9568         {
9569           tree attr_list = TYPE_ATTRIBUTES (TREE_TYPE (decl));
9570
9571           if (!lookup_attribute ("longcall", attr_list)
9572               || lookup_attribute ("shortcall", attr_list))
9573             return true;
9574         }
9575     }
9576   return false;
9577 }
9578
9579 static int
9580 rs6000_ra_ever_killed ()
9581 {
9582   rtx top;
9583   rtx reg;
9584   rtx insn;
9585
9586   /* Irritatingly, there are two kinds of thunks -- those created with
9587      TARGET_ASM_OUTPUT_MI_THUNK and those with DECL_THUNK_P that go
9588      through the regular part of the compiler.  This is a very hacky
9589      way to tell them apart.  */
9590   if (current_function_is_thunk && !no_new_pseudos)
9591     return 0;
9592
9593   /* regs_ever_live has LR marked as used if any sibcalls are present,
9594      but this should not force saving and restoring in the
9595      pro/epilogue.  Likewise, reg_set_between_p thinks a sibcall
9596      clobbers LR, so that is inappropriate. */
9597
9598   /* Also, the prologue can generate a store into LR that
9599      doesn't really count, like this:
9600
9601         move LR->R0
9602         bcl to set PIC register
9603         move LR->R31
9604         move R0->LR
9605
9606      When we're called from the epilogue, we need to avoid counting
9607      this as a store.  */
9608          
9609   push_topmost_sequence ();
9610   top = get_insns ();
9611   pop_topmost_sequence ();
9612   reg = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
9613
9614   for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
9615     {
9616       if (INSN_P (insn))
9617         {
9618           if (FIND_REG_INC_NOTE (insn, reg))
9619             return 1;
9620           else if (GET_CODE (insn) == CALL_INSN 
9621                    && !SIBLING_CALL_P (insn))
9622             return 1;
9623           else if (set_of (reg, insn) != NULL_RTX
9624                    && !prologue_epilogue_contains (insn))
9625             return 1;
9626         }
9627     }
9628   return 0;
9629 }
9630 \f
9631 /* Add a REG_MAYBE_DEAD note to the insn.  */
9632 static void
9633 rs6000_maybe_dead (insn)
9634      rtx insn;
9635 {
9636   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
9637                                         const0_rtx,
9638                                         REG_NOTES (insn));
9639 }
9640
9641 /* Emit instructions needed to load the TOC register.
9642    This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
9643    a constant pool; or for SVR4 -fpic.  */
9644
9645 void
9646 rs6000_emit_load_toc_table (fromprolog)
9647      int fromprolog;
9648 {
9649   rtx dest;
9650   dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
9651
9652   if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
9653     {
9654       rtx temp = (fromprolog
9655                   ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
9656                   : gen_reg_rtx (Pmode));
9657       rs6000_maybe_dead (emit_insn (gen_load_toc_v4_pic_si (temp)));
9658       rs6000_maybe_dead (emit_move_insn (dest, temp));
9659     }
9660   else if (TARGET_ELF && DEFAULT_ABI != ABI_AIX && flag_pic == 2)
9661     {
9662       char buf[30];
9663       rtx tempLR = (fromprolog
9664                     ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
9665                     : gen_reg_rtx (Pmode));
9666       rtx temp0 = (fromprolog
9667                    ? gen_rtx_REG (Pmode, 0)
9668                    : gen_reg_rtx (Pmode));
9669       rtx symF;
9670
9671       /* possibly create the toc section */
9672       if (! toc_initialized)
9673         {
9674           toc_section ();
9675           function_section (current_function_decl);
9676         }
9677
9678       if (fromprolog)
9679         {
9680           rtx symL;
9681
9682           ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
9683           symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
9684
9685           ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
9686           symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
9687
9688           rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_1 (tempLR,
9689                                                                symF)));
9690           rs6000_maybe_dead (emit_move_insn (dest, tempLR));
9691           rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest,
9692                                                                symL,
9693                                                                symF)));
9694         }
9695       else
9696         {
9697           rtx tocsym;
9698           static int reload_toc_labelno = 0;
9699
9700           tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
9701
9702           ASM_GENERATE_INTERNAL_LABEL (buf, "LCG", reload_toc_labelno++);
9703           symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
9704
9705           rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_1b (tempLR,
9706                                                                 symF,
9707                                                                 tocsym)));
9708           rs6000_maybe_dead (emit_move_insn (dest, tempLR));
9709           rs6000_maybe_dead (emit_move_insn (temp0,
9710                                              gen_rtx_MEM (Pmode, dest)));
9711         }
9712       rs6000_maybe_dead (emit_insn (gen_addsi3 (dest, temp0, dest)));
9713     }
9714   else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
9715     {
9716       /* This is for AIX code running in non-PIC ELF32.  */
9717       char buf[30];
9718       rtx realsym;
9719       ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
9720       realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
9721
9722       rs6000_maybe_dead (emit_insn (gen_elf_high (dest, realsym)));
9723       rs6000_maybe_dead (emit_insn (gen_elf_low (dest, dest, realsym)));
9724     }
9725   else if (DEFAULT_ABI == ABI_AIX)
9726     {
9727       if (TARGET_32BIT)
9728         rs6000_maybe_dead (emit_insn (gen_load_toc_aix_si (dest)));
9729       else
9730         rs6000_maybe_dead (emit_insn (gen_load_toc_aix_di (dest)));
9731     }
9732   else
9733     abort ();
9734 }
9735
9736 int   
9737 get_TOC_alias_set ()
9738 {
9739     static int set = -1;
9740     if (set == -1)
9741       set = new_alias_set ();
9742     return set;
9743 }   
9744
9745 /* This retuns nonzero if the current function uses the TOC.  This is
9746    determined by the presence of (unspec ... 7), which is generated by
9747    the various load_toc_* patterns.  */
9748
9749 int
9750 uses_TOC () 
9751 {
9752     rtx insn;
9753
9754     for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9755       if (INSN_P (insn))
9756         {
9757           rtx pat = PATTERN (insn);
9758           int i;
9759
9760           if (GET_CODE (pat) == PARALLEL) 
9761             for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
9762               if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == UNSPEC 
9763                  && XINT (XVECEXP (PATTERN (insn), 0, i), 1) == 7)
9764                   return 1;
9765         }
9766     return 0;
9767 }
9768
9769 rtx
9770 create_TOC_reference (symbol) 
9771     rtx symbol;
9772 {
9773   return gen_rtx_PLUS (Pmode, 
9774            gen_rtx_REG (Pmode, TOC_REGISTER),
9775              gen_rtx_CONST (Pmode, 
9776                gen_rtx_MINUS (Pmode, symbol, 
9777                  gen_rtx_SYMBOL_REF (Pmode, toc_label_name))));
9778 }
9779
9780 #if TARGET_AIX
9781 /* __throw will restore its own return address to be the same as the
9782    return address of the function that the throw is being made to.
9783    This is unfortunate, because we want to check the original
9784    return address to see if we need to restore the TOC.
9785    So we have to squirrel it away here.  
9786    This is used only in compiling __throw and __rethrow. 
9787
9788    Most of this code should be removed by CSE.  */
9789 static rtx insn_after_throw;
9790
9791 /* This does the saving...  */
9792 void
9793 rs6000_aix_emit_builtin_unwind_init ()
9794 {
9795   rtx mem;
9796   rtx stack_top = gen_reg_rtx (Pmode);
9797   rtx opcode_addr = gen_reg_rtx (Pmode);
9798
9799   insn_after_throw = gen_reg_rtx (SImode);
9800
9801   mem = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
9802   emit_move_insn (stack_top, mem);
9803
9804   mem = gen_rtx_MEM (Pmode, 
9805                      gen_rtx_PLUS (Pmode, stack_top, 
9806                                    GEN_INT (2 * GET_MODE_SIZE (Pmode))));
9807   emit_move_insn (opcode_addr, mem);
9808   emit_move_insn (insn_after_throw, gen_rtx_MEM (SImode, opcode_addr));
9809 }
9810
9811 /* Emit insns to _restore_ the TOC register, at runtime (specifically
9812    in _eh.o).  Only used on AIX.
9813
9814    The idea is that on AIX, function calls look like this:
9815         bl  somefunction-trampoline
9816         lwz r2,20(sp)
9817
9818    and later,
9819         somefunction-trampoline:
9820         stw r2,20(sp)
9821          ... load function address in the count register ...
9822         bctr
9823    or like this, if the linker determines that this is not a cross-module call
9824    and so the TOC need not be restored:
9825         bl  somefunction
9826         nop
9827    or like this, if the compiler could determine that this is not a
9828    cross-module call:
9829         bl  somefunction
9830    now, the tricky bit here is that register 2 is saved and restored
9831    by the _linker_, so we can't readily generate debugging information
9832    for it.  So we need to go back up the call chain looking at the
9833    insns at return addresses to see which calls saved the TOC register
9834    and so see where it gets restored from.
9835
9836    Oh, and all this gets done in RTL inside the eh_epilogue pattern,
9837    just before the actual epilogue.
9838
9839    On the bright side, this incurs no space or time overhead unless an
9840    exception is thrown, except for the extra code in libgcc.a.  
9841
9842    The parameter STACKSIZE is a register containing (at runtime)
9843    the amount to be popped off the stack in addition to the stack frame
9844    of this routine (which will be __throw or __rethrow, and so is
9845    guaranteed to have a stack frame).  */
9846
9847 void
9848 rs6000_emit_eh_toc_restore (stacksize)
9849      rtx stacksize;
9850 {
9851   rtx top_of_stack;
9852   rtx bottom_of_stack = gen_reg_rtx (Pmode);
9853   rtx tocompare = gen_reg_rtx (SImode);
9854   rtx opcode = gen_reg_rtx (SImode);
9855   rtx opcode_addr = gen_reg_rtx (Pmode);
9856   rtx mem;
9857   rtx loop_start = gen_label_rtx ();
9858   rtx no_toc_restore_needed = gen_label_rtx ();
9859   rtx loop_exit = gen_label_rtx ();
9860   
9861   mem = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
9862   set_mem_alias_set (mem, rs6000_sr_alias_set);
9863   emit_move_insn (bottom_of_stack, mem);
9864
9865   top_of_stack = expand_binop (Pmode, add_optab, 
9866                                bottom_of_stack, stacksize,
9867                                NULL_RTX, 1, OPTAB_WIDEN);
9868
9869   emit_move_insn (tocompare, gen_int_mode (TARGET_32BIT ? 0x80410014 
9870                                            : 0xE8410028, SImode));
9871
9872   if (insn_after_throw == NULL_RTX)
9873     abort ();
9874   emit_move_insn (opcode, insn_after_throw);
9875   
9876   emit_note (NULL, NOTE_INSN_LOOP_BEG);
9877   emit_label (loop_start);
9878   
9879   do_compare_rtx_and_jump (opcode, tocompare, NE, 1,
9880                            SImode, NULL_RTX, NULL_RTX,
9881                            no_toc_restore_needed);
9882   
9883   mem = gen_rtx_MEM (Pmode, 
9884                      gen_rtx_PLUS (Pmode, bottom_of_stack, 
9885                                    GEN_INT (5 * GET_MODE_SIZE (Pmode))));
9886   emit_move_insn (gen_rtx_REG (Pmode, 2), mem);
9887
9888   emit_label (no_toc_restore_needed);
9889   do_compare_rtx_and_jump (top_of_stack, bottom_of_stack, EQ, 1,
9890                            Pmode, NULL_RTX, NULL_RTX,
9891                            loop_exit);
9892
9893   mem = gen_rtx_MEM (Pmode, bottom_of_stack);
9894   set_mem_alias_set (mem, rs6000_sr_alias_set);
9895   emit_move_insn (bottom_of_stack, mem);
9896   
9897   mem = gen_rtx_MEM (Pmode, 
9898                      gen_rtx_PLUS (Pmode, bottom_of_stack, 
9899                                    GEN_INT (2 * GET_MODE_SIZE (Pmode))));
9900   emit_move_insn (opcode_addr, mem);
9901   emit_move_insn (opcode, gen_rtx_MEM (SImode, opcode_addr));
9902
9903   emit_note (NULL, NOTE_INSN_LOOP_CONT);
9904   emit_jump (loop_start);
9905   emit_note (NULL, NOTE_INSN_LOOP_END);
9906   emit_label (loop_exit);
9907 }
9908 #endif /* TARGET_AIX */
9909 \f
9910 /* This ties together stack memory (MEM with an alias set of
9911    rs6000_sr_alias_set) and the change to the stack pointer.  */
9912
9913 static void
9914 rs6000_emit_stack_tie ()
9915 {
9916   rtx mem = gen_rtx_MEM (BLKmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM));
9917
9918   set_mem_alias_set (mem, rs6000_sr_alias_set);
9919   emit_insn (gen_stack_tie (mem));
9920 }
9921
9922 /* Emit the correct code for allocating stack space, as insns.
9923    If COPY_R12, make sure a copy of the old frame is left in r12.
9924    The generated code may use hard register 0 as a temporary.  */
9925
9926 static void
9927 rs6000_emit_allocate_stack (size, copy_r12)
9928      HOST_WIDE_INT size;
9929      int copy_r12;
9930 {
9931   rtx insn;
9932   rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
9933   rtx tmp_reg = gen_rtx_REG (Pmode, 0);
9934   rtx todec = GEN_INT (-size);
9935
9936   if (current_function_limit_stack)
9937     {
9938       if (REG_P (stack_limit_rtx)
9939           && REGNO (stack_limit_rtx) > 1 
9940           && REGNO (stack_limit_rtx) <= 31)
9941         {
9942           emit_insn (TARGET_32BIT
9943                      ? gen_addsi3 (tmp_reg,
9944                                    stack_limit_rtx,
9945                                    GEN_INT (size))
9946                      : gen_adddi3 (tmp_reg,
9947                                    stack_limit_rtx,
9948                                    GEN_INT (size)));
9949
9950           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
9951                                     const0_rtx));
9952         }
9953       else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
9954                && TARGET_32BIT
9955                && DEFAULT_ABI == ABI_V4)
9956         {
9957           rtx toload = gen_rtx_CONST (VOIDmode,
9958                                       gen_rtx_PLUS (Pmode, 
9959                                                     stack_limit_rtx, 
9960                                                     GEN_INT (size)));
9961
9962           emit_insn (gen_elf_high (tmp_reg, toload));
9963           emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
9964           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
9965                                     const0_rtx));
9966         }
9967       else
9968         warning ("stack limit expression is not supported");
9969     }
9970
9971   if (copy_r12 || ! TARGET_UPDATE)
9972     emit_move_insn (gen_rtx_REG (Pmode, 12), stack_reg);
9973
9974   if (TARGET_UPDATE)
9975     {
9976       if (size > 32767)
9977         {
9978           /* Need a note here so that try_split doesn't get confused.  */
9979           if (get_last_insn() == NULL_RTX)
9980             emit_note (0, NOTE_INSN_DELETED);
9981           insn = emit_move_insn (tmp_reg, todec);
9982           try_split (PATTERN (insn), insn, 0);
9983           todec = tmp_reg;
9984         }
9985
9986       insn = emit_insn (TARGET_32BIT
9987                         ? gen_movsi_update (stack_reg, stack_reg,
9988                                             todec, stack_reg)
9989                         : gen_movdi_update (stack_reg, stack_reg, 
9990                                             todec, stack_reg));
9991     }
9992   else
9993     {
9994       insn = emit_insn (TARGET_32BIT
9995                         ? gen_addsi3 (stack_reg, stack_reg, todec)
9996                         : gen_adddi3 (stack_reg, stack_reg, todec));
9997       emit_move_insn (gen_rtx_MEM (Pmode, stack_reg),
9998                       gen_rtx_REG (Pmode, 12));
9999     }
10000  
10001   RTX_FRAME_RELATED_P (insn) = 1;
10002   REG_NOTES (insn) = 
10003     gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10004                        gen_rtx_SET (VOIDmode, stack_reg, 
10005                                     gen_rtx_PLUS (Pmode, stack_reg,
10006                                                   GEN_INT (-size))),
10007                        REG_NOTES (insn));
10008 }
10009
10010 /* Add a RTX_FRAME_RELATED note so that dwarf2out_frame_debug_expr
10011    knows that:
10012
10013      (mem (plus (blah) (regXX)))
10014
10015    is really:
10016
10017      (mem (plus (blah) (const VALUE_OF_REGXX))).  */
10018
10019 static void
10020 altivec_frame_fixup (insn, reg, val)
10021      rtx insn, reg;
10022      HOST_WIDE_INT val;
10023 {
10024   rtx real;
10025
10026   real = copy_rtx (PATTERN (insn));
10027
10028   real = replace_rtx (real, reg, GEN_INT (val));
10029
10030   RTX_FRAME_RELATED_P (insn) = 1;
10031   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10032                                         real,
10033                                         REG_NOTES (insn));
10034 }
10035
10036 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
10037    with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
10038    is not NULL.  It would be nice if dwarf2out_frame_debug_expr could
10039    deduce these equivalences by itself so it wasn't necessary to hold
10040    its hand so much.  */
10041
10042 static void
10043 rs6000_frame_related (insn, reg, val, reg2, rreg)
10044      rtx insn;
10045      rtx reg;
10046      HOST_WIDE_INT val;
10047      rtx reg2;
10048      rtx rreg;
10049 {
10050   rtx real, temp;
10051
10052   /* copy_rtx will not make unique copies of registers, so we need to
10053      ensure we don't have unwanted sharing here.  */
10054   if (reg == reg2)
10055     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
10056
10057   if (reg == rreg)
10058     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
10059
10060   real = copy_rtx (PATTERN (insn));
10061
10062   if (reg2 != NULL_RTX)
10063     real = replace_rtx (real, reg2, rreg);
10064   
10065   real = replace_rtx (real, reg, 
10066                       gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
10067                                                         STACK_POINTER_REGNUM),
10068                                     GEN_INT (val)));
10069   
10070   /* We expect that 'real' is either a SET or a PARALLEL containing
10071      SETs (and possibly other stuff).  In a PARALLEL, all the SETs
10072      are important so they all have to be marked RTX_FRAME_RELATED_P.  */
10073
10074   if (GET_CODE (real) == SET)
10075     {
10076       rtx set = real;
10077       
10078       temp = simplify_rtx (SET_SRC (set));
10079       if (temp)
10080         SET_SRC (set) = temp;
10081       temp = simplify_rtx (SET_DEST (set));
10082       if (temp)
10083         SET_DEST (set) = temp;
10084       if (GET_CODE (SET_DEST (set)) == MEM)
10085         {
10086           temp = simplify_rtx (XEXP (SET_DEST (set), 0));
10087           if (temp)
10088             XEXP (SET_DEST (set), 0) = temp;
10089         }
10090     }
10091   else if (GET_CODE (real) == PARALLEL)
10092     {
10093       int i;
10094       for (i = 0; i < XVECLEN (real, 0); i++)
10095         if (GET_CODE (XVECEXP (real, 0, i)) == SET)
10096           {
10097             rtx set = XVECEXP (real, 0, i);
10098             
10099             temp = simplify_rtx (SET_SRC (set));
10100             if (temp)
10101               SET_SRC (set) = temp;
10102             temp = simplify_rtx (SET_DEST (set));
10103             if (temp)
10104               SET_DEST (set) = temp;
10105             if (GET_CODE (SET_DEST (set)) == MEM)
10106               {
10107                 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
10108                 if (temp)
10109                   XEXP (SET_DEST (set), 0) = temp;
10110               }
10111             RTX_FRAME_RELATED_P (set) = 1;
10112           }
10113     }
10114   else
10115     abort ();
10116   
10117   RTX_FRAME_RELATED_P (insn) = 1;
10118   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10119                                         real,
10120                                         REG_NOTES (insn));
10121 }
10122
10123 /* Returns an insn that has a vrsave set operation with the
10124    appropriate CLOBBERs.  */
10125
10126 static rtx
10127 generate_set_vrsave (reg, info, epiloguep)
10128      rtx reg;
10129      rs6000_stack_t *info;
10130      int epiloguep;
10131 {
10132   int nclobs, i;
10133   rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
10134   rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
10135
10136   clobs[0]
10137     = gen_rtx_SET (VOIDmode,
10138                    vrsave,
10139                    gen_rtx_UNSPEC_VOLATILE (SImode,
10140                                             gen_rtvec (2, reg, vrsave),
10141                                             30));
10142
10143   nclobs = 1;
10144
10145   /* We need to clobber the registers in the mask so the scheduler
10146      does not move sets to VRSAVE before sets of AltiVec registers.
10147
10148      However, if the function receives nonlocal gotos, reload will set
10149      all call saved registers live.  We will end up with:
10150
10151         (set (reg 999) (mem))
10152         (parallel [ (set (reg vrsave) (unspec blah))
10153                     (clobber (reg 999))])
10154
10155      The clobber will cause the store into reg 999 to be dead, and
10156      flow will attempt to delete an epilogue insn.  In this case, we
10157      need an unspec use/set of the register.  */
10158
10159   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
10160     if (info->vrsave_mask != 0 && ALTIVEC_REG_BIT (i) != 0)
10161       {
10162         if (!epiloguep || call_used_regs [i])
10163           clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
10164                                              gen_rtx_REG (V4SImode, i));
10165         else
10166           {
10167             rtx reg = gen_rtx_REG (V4SImode, i);
10168
10169             clobs[nclobs++]
10170               = gen_rtx_SET (VOIDmode,
10171                              reg,
10172                              gen_rtx_UNSPEC (V4SImode,
10173                                              gen_rtvec (1, reg), 27));
10174           }
10175       }
10176
10177   insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
10178
10179   for (i = 0; i < nclobs; ++i)
10180     XVECEXP (insn, 0, i) = clobs[i];
10181
10182   return insn;
10183 }
10184
10185 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
10186    Save REGNO into [FRAME_REG + OFFSET] in mode MODE.  */
10187
10188 static void
10189 emit_frame_save (frame_reg, frame_ptr, mode, regno, offset, total_size)
10190      rtx frame_reg;
10191      rtx frame_ptr;
10192      enum machine_mode mode;
10193      unsigned int regno;
10194      int offset;
10195      int total_size;
10196 {
10197   rtx reg, offset_rtx, insn, mem, addr, int_rtx;
10198   rtx replacea, replaceb;
10199
10200   int_rtx = GEN_INT (offset);
10201
10202   /* Some cases that need register indexed addressing.  */
10203   if ((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
10204       || (TARGET_SPE_ABI
10205           && SPE_VECTOR_MODE (mode)
10206           && !SPE_CONST_OFFSET_OK (offset)))
10207     {
10208       /* Whomever calls us must make sure r11 is available in the
10209          flow path of instructions in the prologue.  */
10210       offset_rtx = gen_rtx_REG (Pmode, 11);
10211       emit_move_insn (offset_rtx, int_rtx);
10212
10213       replacea = offset_rtx;
10214       replaceb = int_rtx;
10215     }
10216   else
10217     {
10218       offset_rtx = int_rtx;
10219       replacea = NULL_RTX;
10220       replaceb = NULL_RTX;
10221     }
10222
10223   reg = gen_rtx_REG (mode, regno);
10224   addr = gen_rtx_PLUS (Pmode, frame_reg, offset_rtx);
10225   mem = gen_rtx_MEM (mode, addr);
10226   set_mem_alias_set (mem, rs6000_sr_alias_set);
10227
10228   insn = emit_move_insn (mem, reg);
10229
10230   rs6000_frame_related (insn, frame_ptr, total_size, replacea, replaceb);
10231 }
10232
10233 /* Emit an offset memory reference suitable for a frame store, while
10234    converting to a valid addressing mode.  */
10235
10236 static rtx
10237 gen_frame_mem_offset (mode, reg, offset)
10238      enum machine_mode mode;
10239      rtx reg;
10240      int offset;
10241 {
10242   rtx int_rtx, offset_rtx;
10243
10244   int_rtx = GEN_INT (offset);
10245
10246   if (TARGET_SPE_ABI && SPE_VECTOR_MODE (mode))
10247     {
10248       offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
10249       emit_move_insn (offset_rtx, int_rtx);
10250     }
10251   else
10252     offset_rtx = int_rtx;
10253
10254   return gen_rtx_MEM (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
10255 }
10256
10257 /* Emit function prologue as insns.  */
10258
10259 void
10260 rs6000_emit_prologue ()
10261 {
10262   rs6000_stack_t *info = rs6000_stack_info ();
10263   enum machine_mode reg_mode = TARGET_POWERPC64 ? DImode : SImode;
10264   int reg_size = TARGET_POWERPC64 ? 8 : 4;
10265   rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
10266   rtx frame_ptr_rtx = gen_rtx_REG (Pmode, 12);
10267   rtx frame_reg_rtx = sp_reg_rtx;
10268   rtx cr_save_rtx = NULL;
10269   rtx insn;
10270   int saving_FPRs_inline;
10271   int using_store_multiple;
10272   HOST_WIDE_INT sp_offset = 0;
10273   
10274    if (TARGET_SPE_ABI)
10275      {
10276        reg_mode = V2SImode;
10277        reg_size = 8;
10278      }
10279
10280   using_store_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
10281                           && !TARGET_SPE_ABI
10282                           && info->first_gp_reg_save < 31);
10283   saving_FPRs_inline = (info->first_fp_reg_save == 64
10284                         || FP_SAVE_INLINE (info->first_fp_reg_save));
10285
10286   /* For V.4, update stack before we do any saving and set back pointer.  */
10287   if (info->push_p && DEFAULT_ABI == ABI_V4)
10288     {
10289       if (info->total_size < 32767)
10290         sp_offset = info->total_size;
10291       else
10292         frame_reg_rtx = frame_ptr_rtx;
10293       rs6000_emit_allocate_stack (info->total_size, 
10294                                   (frame_reg_rtx != sp_reg_rtx
10295                                    && (info->cr_save_p
10296                                        || info->lr_save_p
10297                                        || info->first_fp_reg_save < 64
10298                                        || info->first_gp_reg_save < 32
10299                                        )));
10300       if (frame_reg_rtx != sp_reg_rtx)
10301         rs6000_emit_stack_tie ();
10302     }
10303
10304   /* Save AltiVec registers if needed.  */
10305   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
10306     {
10307       int i;
10308
10309       /* There should be a non inline version of this, for when we
10310          are saving lots of vector registers.  */
10311       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
10312         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
10313           {
10314             rtx areg, savereg, mem;
10315             int offset;
10316
10317             offset = info->altivec_save_offset + sp_offset
10318               + 16 * (i - info->first_altivec_reg_save);
10319
10320             savereg = gen_rtx_REG (V4SImode, i);
10321
10322             areg = gen_rtx_REG (Pmode, 0);
10323             emit_move_insn (areg, GEN_INT (offset));
10324
10325             /* AltiVec addressing mode is [reg+reg].  */
10326             mem = gen_rtx_MEM (V4SImode,
10327                                gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
10328                                
10329             set_mem_alias_set (mem, rs6000_sr_alias_set);
10330
10331             insn = emit_move_insn (mem, savereg);
10332
10333             altivec_frame_fixup (insn, areg, offset);
10334           }
10335     }
10336
10337   /* VRSAVE is a bit vector representing which AltiVec registers
10338      are used.  The OS uses this to determine which vector
10339      registers to save on a context switch.  We need to save
10340      VRSAVE on the stack frame, add whatever AltiVec registers we
10341      used in this function, and do the corresponding magic in the
10342      epilogue.  */
10343
10344   if (TARGET_ALTIVEC && info->vrsave_mask != 0)
10345     {
10346       rtx reg, mem, vrsave;
10347       int offset;
10348
10349       /* Get VRSAVE onto a GPR.  */
10350       reg = gen_rtx_REG (SImode, 12);
10351       vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
10352       if (TARGET_MACHO)
10353         emit_insn (gen_get_vrsave_internal (reg));
10354       else
10355         emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
10356
10357       /* Save VRSAVE.  */
10358       offset = info->vrsave_save_offset + sp_offset;
10359       mem
10360         = gen_rtx_MEM (SImode,
10361                        gen_rtx_PLUS (Pmode, frame_reg_rtx, GEN_INT (offset)));
10362       set_mem_alias_set (mem, rs6000_sr_alias_set);
10363       insn = emit_move_insn (mem, reg);
10364
10365       /* Include the registers in the mask.  */
10366       emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
10367
10368       insn = emit_insn (generate_set_vrsave (reg, info, 0));
10369     }
10370
10371   /* If we use the link register, get it into r0.  */
10372   if (info->lr_save_p)
10373     emit_move_insn (gen_rtx_REG (Pmode, 0),
10374                     gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
10375
10376   /* If we need to save CR, put it into r12.  */
10377   if (info->cr_save_p && frame_reg_rtx != frame_ptr_rtx)
10378     {
10379       cr_save_rtx = gen_rtx_REG (SImode, 12);
10380       emit_insn (gen_movesi_from_cr (cr_save_rtx));
10381     }
10382
10383   /* Do any required saving of fpr's.  If only one or two to save, do
10384      it ourselves.  Otherwise, call function.  */
10385   if (saving_FPRs_inline)
10386     {
10387       int i;
10388       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
10389         if ((regs_ever_live[info->first_fp_reg_save+i] 
10390              && ! call_used_regs[info->first_fp_reg_save+i]))
10391           emit_frame_save (frame_reg_rtx, frame_ptr_rtx, DFmode,
10392                            info->first_fp_reg_save + i,
10393                            info->fp_save_offset + sp_offset + 8 * i,
10394                            info->total_size);
10395     }
10396   else if (info->first_fp_reg_save != 64)
10397     {
10398       int i;
10399       char rname[30];
10400       const char *alloc_rname;
10401       rtvec p;
10402       p = rtvec_alloc (2 + 64 - info->first_fp_reg_save);
10403       
10404       RTVEC_ELT (p, 0) = gen_rtx_CLOBBER (VOIDmode, 
10405                                           gen_rtx_REG (Pmode, 
10406                                                        LINK_REGISTER_REGNUM));
10407       sprintf (rname, "%s%d%s", SAVE_FP_PREFIX,
10408                info->first_fp_reg_save - 32, SAVE_FP_SUFFIX);
10409       alloc_rname = ggc_strdup (rname);
10410       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode,
10411                                       gen_rtx_SYMBOL_REF (Pmode,
10412                                                           alloc_rname));
10413       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
10414         {
10415           rtx addr, reg, mem;
10416           reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
10417           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
10418                                GEN_INT (info->fp_save_offset 
10419                                         + sp_offset + 8*i));
10420           mem = gen_rtx_MEM (DFmode, addr);
10421           set_mem_alias_set (mem, rs6000_sr_alias_set);
10422
10423           RTVEC_ELT (p, i + 2) = gen_rtx_SET (VOIDmode, mem, reg);
10424         }
10425       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
10426       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
10427                             NULL_RTX, NULL_RTX);
10428     }
10429
10430   /* Save GPRs.  This is done as a PARALLEL if we are using
10431      the store-multiple instructions.  */
10432   if (using_store_multiple)
10433     {
10434       rtvec p;
10435       int i;
10436       p = rtvec_alloc (32 - info->first_gp_reg_save);
10437       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
10438         {
10439           rtx addr, reg, mem;
10440           reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
10441           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
10442                                GEN_INT (info->gp_save_offset 
10443                                         + sp_offset 
10444                                         + reg_size * i));
10445           mem = gen_rtx_MEM (reg_mode, addr);
10446           set_mem_alias_set (mem, rs6000_sr_alias_set);
10447
10448           RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, reg);
10449         }
10450       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
10451       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
10452                             NULL_RTX, NULL_RTX);
10453     }
10454   else
10455     {
10456       int i;
10457       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
10458         if ((regs_ever_live[info->first_gp_reg_save+i] 
10459              && ! call_used_regs[info->first_gp_reg_save+i])
10460             || (i+info->first_gp_reg_save == RS6000_PIC_OFFSET_TABLE_REGNUM
10461                 && ((DEFAULT_ABI == ABI_V4 && flag_pic == 1)
10462                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
10463           {
10464             rtx addr, reg, mem;
10465             reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
10466
10467             if (TARGET_SPE_ABI)
10468               {
10469                 int offset = info->spe_gp_save_offset + sp_offset + 8 * i;
10470                 rtx b;
10471
10472                 if (!SPE_CONST_OFFSET_OK (offset))
10473                   {
10474                     b = gen_rtx_REG (Pmode, FIXED_SCRATCH);
10475                     emit_move_insn (b, GEN_INT (offset));
10476                   }
10477                 else
10478                   b = GEN_INT (offset);
10479
10480                 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, b);
10481                 mem = gen_rtx_MEM (V2SImode, addr);
10482                 set_mem_alias_set (mem, rs6000_sr_alias_set);
10483                 insn = emit_move_insn (mem, reg);
10484
10485                 if (GET_CODE (b) == CONST_INT)
10486                   rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
10487                                         NULL_RTX, NULL_RTX);
10488                 else
10489                   rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
10490                                         b, GEN_INT (offset));
10491               }
10492             else
10493               {
10494                 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
10495                                      GEN_INT (info->gp_save_offset 
10496                                               + sp_offset 
10497                                               + reg_size * i));
10498                 mem = gen_rtx_MEM (reg_mode, addr);
10499                 set_mem_alias_set (mem, rs6000_sr_alias_set);
10500
10501                 insn = emit_move_insn (mem, reg);
10502                 rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
10503                                       NULL_RTX, NULL_RTX);
10504               }
10505           }
10506     }
10507
10508   /* ??? There's no need to emit actual instructions here, but it's the
10509      easiest way to get the frame unwind information emitted.  */
10510   if (current_function_calls_eh_return)
10511     {
10512       unsigned int i, regno;
10513
10514       for (i = 0; ; ++i)
10515         {
10516           regno = EH_RETURN_DATA_REGNO (i);
10517           if (regno == INVALID_REGNUM)
10518             break;
10519
10520           emit_frame_save (frame_reg_rtx, frame_ptr_rtx, reg_mode, regno,
10521                            info->ehrd_offset + sp_offset
10522                            + reg_size * (int) i,
10523                            info->total_size);
10524         }
10525     }
10526
10527   /* Save lr if we used it.  */
10528   if (info->lr_save_p)
10529     {
10530       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
10531                                GEN_INT (info->lr_save_offset + sp_offset));
10532       rtx reg = gen_rtx_REG (Pmode, 0);
10533       rtx mem = gen_rtx_MEM (Pmode, addr);
10534       /* This should not be of rs6000_sr_alias_set, because of
10535          __builtin_return_address.  */
10536       
10537       insn = emit_move_insn (mem, reg);
10538       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
10539                             reg, gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
10540     }
10541
10542   /* Save CR if we use any that must be preserved.  */
10543   if (info->cr_save_p)
10544     {
10545       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
10546                                GEN_INT (info->cr_save_offset + sp_offset));
10547       rtx mem = gen_rtx_MEM (SImode, addr);
10548
10549       set_mem_alias_set (mem, rs6000_sr_alias_set);
10550
10551       /* If r12 was used to hold the original sp, copy cr into r0 now
10552          that it's free.  */
10553       if (REGNO (frame_reg_rtx) == 12)
10554         {
10555           cr_save_rtx = gen_rtx_REG (SImode, 0);
10556           emit_insn (gen_movesi_from_cr (cr_save_rtx));
10557         }
10558       insn = emit_move_insn (mem, cr_save_rtx);
10559
10560       /* Now, there's no way that dwarf2out_frame_debug_expr is going
10561          to understand '(unspec:SI [(reg:CC 68) ...] 19)'.  But that's
10562          OK.  All we have to do is specify that _one_ condition code
10563          register is saved in this stack slot.  The thrower's epilogue
10564          will then restore all the call-saved registers.
10565          We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux.  */
10566       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
10567                             cr_save_rtx, gen_rtx_REG (SImode, CR2_REGNO));
10568     }
10569
10570   /* Update stack and set back pointer unless this is V.4, 
10571      for which it was done previously.  */
10572   if (info->push_p && DEFAULT_ABI != ABI_V4)
10573     rs6000_emit_allocate_stack (info->total_size, FALSE);
10574
10575   /* Set frame pointer, if needed.  */
10576   if (frame_pointer_needed)
10577     {
10578       insn = emit_move_insn (gen_rtx_REG (Pmode, FRAME_POINTER_REGNUM), 
10579                              sp_reg_rtx);
10580       RTX_FRAME_RELATED_P (insn) = 1;
10581     }
10582
10583   /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up.  */
10584   if ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
10585       || (DEFAULT_ABI == ABI_V4 && flag_pic == 1
10586           && regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM]))
10587   {
10588     /* If emit_load_toc_table will use the link register, we need to save
10589        it.  We use R11 for this purpose because emit_load_toc_table
10590        can use register 0.  This allows us to use a plain 'blr' to return
10591        from the procedure more often.  */
10592     int save_LR_around_toc_setup = (TARGET_ELF && flag_pic != 0
10593                                     && ! info->lr_save_p
10594                                     && EXIT_BLOCK_PTR->pred != NULL);
10595     if (save_LR_around_toc_setup)
10596       emit_move_insn (gen_rtx_REG (Pmode, 11), 
10597                       gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
10598     
10599     rs6000_emit_load_toc_table (TRUE);
10600
10601     if (save_LR_around_toc_setup)
10602       emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM), 
10603                       gen_rtx_REG (Pmode, 11));
10604   }
10605
10606 #if TARGET_MACHO
10607   if (DEFAULT_ABI == ABI_DARWIN
10608       && flag_pic && current_function_uses_pic_offset_table)
10609     {
10610       rtx dest = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
10611       char *picbase = machopic_function_base_name ();
10612       rtx src = gen_rtx_SYMBOL_REF (Pmode, ggc_alloc_string (picbase, -1));
10613
10614       rs6000_maybe_dead (emit_insn (gen_load_macho_picbase (dest, src)));
10615
10616       rs6000_maybe_dead (
10617         emit_move_insn (gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM),
10618                         gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)));
10619     }
10620 #endif
10621 }
10622
10623 /* Write function prologue.  */
10624
10625 static void
10626 rs6000_output_function_prologue (file, size)
10627      FILE *file;
10628      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
10629 {
10630   rs6000_stack_t *info = rs6000_stack_info ();
10631
10632   if (TARGET_DEBUG_STACK)
10633     debug_stack_info (info);
10634
10635   /* Write .extern for any function we will call to save and restore
10636      fp values.  */
10637   if (info->first_fp_reg_save < 64
10638       && !FP_SAVE_INLINE (info->first_fp_reg_save))
10639     fprintf (file, "\t.extern %s%d%s\n\t.extern %s%d%s\n",
10640              SAVE_FP_PREFIX, info->first_fp_reg_save - 32, SAVE_FP_SUFFIX,
10641              RESTORE_FP_PREFIX, info->first_fp_reg_save - 32,
10642              RESTORE_FP_SUFFIX);
10643
10644   /* Write .extern for AIX common mode routines, if needed.  */
10645   if (! TARGET_POWER && ! TARGET_POWERPC && ! common_mode_defined)
10646     {
10647       fputs ("\t.extern __mulh\n", file);
10648       fputs ("\t.extern __mull\n", file);
10649       fputs ("\t.extern __divss\n", file);
10650       fputs ("\t.extern __divus\n", file);
10651       fputs ("\t.extern __quoss\n", file);
10652       fputs ("\t.extern __quous\n", file);
10653       common_mode_defined = 1;
10654     }
10655
10656   if (! HAVE_prologue)
10657     {
10658       start_sequence ();
10659
10660       /* A NOTE_INSN_DELETED is supposed to be at the start and end of
10661          the "toplevel" insn chain.  */
10662       emit_note (0, NOTE_INSN_DELETED);
10663       rs6000_emit_prologue ();
10664       emit_note (0, NOTE_INSN_DELETED);
10665
10666       /* Expand INSN_ADDRESSES so final() doesn't crash. */
10667       {
10668         rtx insn;
10669         unsigned addr = 0;
10670         for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
10671           {
10672             INSN_ADDRESSES_NEW (insn, addr);
10673             addr += 4;
10674           }
10675       }
10676
10677       if (TARGET_DEBUG_STACK)
10678         debug_rtx_list (get_insns (), 100);
10679       final (get_insns (), file, FALSE, FALSE);
10680       end_sequence ();
10681     }
10682
10683   rs6000_pic_labelno++;
10684 }
10685   
10686 /* Emit function epilogue as insns.
10687
10688    At present, dwarf2out_frame_debug_expr doesn't understand
10689    register restores, so we don't bother setting RTX_FRAME_RELATED_P
10690    anywhere in the epilogue.  Most of the insns below would in any case
10691    need special notes to explain where r11 is in relation to the stack.  */
10692
10693 void
10694 rs6000_emit_epilogue (sibcall)
10695      int sibcall;
10696 {
10697   rs6000_stack_t *info;
10698   int restoring_FPRs_inline;
10699   int using_load_multiple;
10700   int using_mfcr_multiple;
10701   int use_backchain_to_restore_sp;
10702   int sp_offset = 0;
10703   rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
10704   rtx frame_reg_rtx = sp_reg_rtx;
10705   enum machine_mode reg_mode = TARGET_POWERPC64 ? DImode : SImode;
10706   int reg_size = TARGET_POWERPC64 ? 8 : 4;
10707   int i;
10708
10709   if (TARGET_SPE_ABI)
10710     {
10711       reg_mode = V2SImode;
10712       reg_size = 8;
10713     }
10714
10715   info = rs6000_stack_info ();
10716   using_load_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
10717                          && !TARGET_SPE_ABI
10718                          && info->first_gp_reg_save < 31);
10719   restoring_FPRs_inline = (sibcall
10720                            || current_function_calls_eh_return
10721                            || info->first_fp_reg_save == 64
10722                            || FP_SAVE_INLINE (info->first_fp_reg_save));
10723   use_backchain_to_restore_sp = (frame_pointer_needed 
10724                                  || current_function_calls_alloca
10725                                  || info->total_size > 32767);
10726   using_mfcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
10727                          || rs6000_cpu == PROCESSOR_PPC603
10728                          || rs6000_cpu == PROCESSOR_PPC750
10729                          || optimize_size);
10730
10731   /* If we have a frame pointer, a call to alloca,  or a large stack
10732      frame, restore the old stack pointer using the backchain.  Otherwise,
10733      we know what size to update it with.  */
10734   if (use_backchain_to_restore_sp)
10735     {
10736       /* Under V.4, don't reset the stack pointer until after we're done
10737          loading the saved registers.  */
10738       if (DEFAULT_ABI == ABI_V4)
10739         frame_reg_rtx = gen_rtx_REG (Pmode, 11);
10740
10741       emit_move_insn (frame_reg_rtx,
10742                       gen_rtx_MEM (Pmode, sp_reg_rtx));
10743       
10744     }
10745   else if (info->push_p)
10746     {
10747       if (DEFAULT_ABI == ABI_V4)
10748         sp_offset = info->total_size;
10749       else
10750         {
10751           emit_insn (TARGET_32BIT
10752                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
10753                                    GEN_INT (info->total_size))
10754                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
10755                                    GEN_INT (info->total_size)));
10756         }
10757     }
10758   
10759   /* Restore AltiVec registers if needed.  */
10760   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
10761     {
10762       int i;
10763
10764       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
10765         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
10766           {
10767             rtx addr, areg, mem;
10768
10769             areg = gen_rtx_REG (Pmode, 0);
10770             emit_move_insn
10771               (areg, GEN_INT (info->altivec_save_offset
10772                               + sp_offset
10773                               + 16 * (i - info->first_altivec_reg_save)));
10774
10775             /* AltiVec addressing mode is [reg+reg].  */
10776             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
10777             mem = gen_rtx_MEM (V4SImode, addr);
10778             set_mem_alias_set (mem, rs6000_sr_alias_set);
10779
10780             emit_move_insn (gen_rtx_REG (V4SImode, i), mem);
10781           }
10782     }
10783
10784   /* Restore VRSAVE if needed.  */
10785   if (TARGET_ALTIVEC_ABI && info->vrsave_mask != 0)
10786     {
10787       rtx addr, mem, reg;
10788
10789       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
10790                            GEN_INT (info->vrsave_save_offset + sp_offset));
10791       mem = gen_rtx_MEM (SImode, addr);
10792       set_mem_alias_set (mem, rs6000_sr_alias_set);
10793       reg = gen_rtx_REG (SImode, 12);
10794       emit_move_insn (reg, mem);
10795
10796       emit_insn (generate_set_vrsave (reg, info, 1));
10797     }
10798
10799   /* Get the old lr if we saved it.  */
10800   if (info->lr_save_p)
10801     {
10802       rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx,
10803                                       info->lr_save_offset + sp_offset);
10804
10805       set_mem_alias_set (mem, rs6000_sr_alias_set);
10806
10807       emit_move_insn (gen_rtx_REG (Pmode, 0), mem);
10808     }
10809   
10810   /* Get the old cr if we saved it.  */
10811   if (info->cr_save_p)
10812     {
10813       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
10814                                GEN_INT (info->cr_save_offset + sp_offset));
10815       rtx mem = gen_rtx_MEM (SImode, addr);
10816
10817       set_mem_alias_set (mem, rs6000_sr_alias_set);
10818
10819       emit_move_insn (gen_rtx_REG (SImode, 12), mem);
10820     }
10821   
10822   /* Set LR here to try to overlap restores below.  */
10823   if (info->lr_save_p)
10824     emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM),
10825                     gen_rtx_REG (Pmode, 0));
10826   
10827   /* Load exception handler data registers, if needed.  */
10828   if (current_function_calls_eh_return)
10829     {
10830       unsigned int i, regno;
10831
10832       for (i = 0; ; ++i)
10833         {
10834           rtx mem;
10835
10836           regno = EH_RETURN_DATA_REGNO (i);
10837           if (regno == INVALID_REGNUM)
10838             break;
10839
10840           mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
10841                                       info->ehrd_offset + sp_offset
10842                                       + reg_size * (int) i);
10843           set_mem_alias_set (mem, rs6000_sr_alias_set);
10844
10845           emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
10846         }
10847     }
10848   
10849   /* Restore GPRs.  This is done as a PARALLEL if we are using
10850      the load-multiple instructions.  */
10851   if (using_load_multiple)
10852     {
10853       rtvec p;
10854       p = rtvec_alloc (32 - info->first_gp_reg_save);
10855       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
10856         {
10857           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
10858                                    GEN_INT (info->gp_save_offset 
10859                                             + sp_offset 
10860                                             + reg_size * i));
10861           rtx mem = gen_rtx_MEM (reg_mode, addr);
10862
10863           set_mem_alias_set (mem, rs6000_sr_alias_set);
10864
10865           RTVEC_ELT (p, i) = 
10866             gen_rtx_SET (VOIDmode,
10867                          gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
10868                          mem);
10869         }
10870       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
10871     }
10872   else
10873     for (i = 0; i < 32 - info->first_gp_reg_save; i++)
10874       if ((regs_ever_live[info->first_gp_reg_save+i] 
10875            && ! call_used_regs[info->first_gp_reg_save+i])
10876           || (i+info->first_gp_reg_save == RS6000_PIC_OFFSET_TABLE_REGNUM
10877               && ((DEFAULT_ABI == ABI_V4 && flag_pic == 1)
10878                   || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
10879         {
10880           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
10881                                    GEN_INT (info->gp_save_offset 
10882                                             + sp_offset 
10883                                             + reg_size * i));
10884           rtx mem = gen_rtx_MEM (reg_mode, addr);
10885
10886           /* Restore 64-bit quantities for SPE.  */
10887           if (TARGET_SPE_ABI)
10888             {
10889               int offset = info->spe_gp_save_offset + sp_offset + 8 * i;
10890               rtx b;
10891
10892               if (!SPE_CONST_OFFSET_OK (offset))
10893                 {
10894                   b = gen_rtx_REG (Pmode, FIXED_SCRATCH);
10895                   emit_move_insn (b, GEN_INT (offset));
10896                 }
10897               else
10898                 b = GEN_INT (offset);
10899
10900               addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, b);
10901               mem = gen_rtx_MEM (V2SImode, addr);
10902             }
10903
10904           set_mem_alias_set (mem, rs6000_sr_alias_set);
10905
10906           emit_move_insn (gen_rtx_REG (reg_mode, 
10907                                        info->first_gp_reg_save + i), mem);
10908         }
10909
10910   /* Restore fpr's if we need to do it without calling a function.  */
10911   if (restoring_FPRs_inline)
10912     for (i = 0; i < 64 - info->first_fp_reg_save; i++)
10913       if ((regs_ever_live[info->first_fp_reg_save+i] 
10914            && ! call_used_regs[info->first_fp_reg_save+i]))
10915         {
10916           rtx addr, mem;
10917           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
10918                                GEN_INT (info->fp_save_offset 
10919                                         + sp_offset 
10920                                         + 8 * i));
10921           mem = gen_rtx_MEM (DFmode, addr);
10922           set_mem_alias_set (mem, rs6000_sr_alias_set);
10923
10924           emit_move_insn (gen_rtx_REG (DFmode, 
10925                                        info->first_fp_reg_save + i),
10926                           mem);
10927         }
10928
10929   /* If we saved cr, restore it here.  Just those that were used.  */
10930   if (info->cr_save_p)
10931     {
10932       rtx r12_rtx = gen_rtx_REG (SImode, 12);
10933       int count = 0;
10934       
10935       if (using_mfcr_multiple)
10936         {
10937           for (i = 0; i < 8; i++)
10938             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
10939               count++;
10940           if (count == 0)
10941             abort ();
10942         }
10943
10944       if (using_mfcr_multiple && count > 1)
10945         {
10946           rtvec p;
10947           int ndx;
10948           
10949           p = rtvec_alloc (count);
10950
10951           ndx = 0;
10952           for (i = 0; i < 8; i++)
10953             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
10954               {
10955                 rtvec r = rtvec_alloc (2);
10956                 RTVEC_ELT (r, 0) = r12_rtx;
10957                 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
10958                 RTVEC_ELT (p, ndx) =
10959                   gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO+i), 
10960                                gen_rtx_UNSPEC (CCmode, r, 20));
10961                 ndx++;
10962               }
10963           emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
10964           if (ndx != count)
10965             abort ();
10966         }
10967       else
10968         for (i = 0; i < 8; i++)
10969           if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
10970             {
10971               emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode, 
10972                                                            CR0_REGNO+i),
10973                                               r12_rtx));
10974             }
10975     }
10976
10977   /* If this is V.4, unwind the stack pointer after all of the loads
10978      have been done.  We need to emit a block here so that sched
10979      doesn't decide to move the sp change before the register restores
10980      (which may not have any obvious dependency on the stack).  This
10981      doesn't hurt performance, because there is no scheduling that can
10982      be done after this point.  */
10983   if (DEFAULT_ABI == ABI_V4)
10984     {
10985       if (frame_reg_rtx != sp_reg_rtx)
10986           rs6000_emit_stack_tie ();
10987
10988       if (use_backchain_to_restore_sp)
10989         {
10990           emit_move_insn (sp_reg_rtx, frame_reg_rtx);
10991         }
10992       else if (sp_offset != 0)
10993         {
10994           emit_insn (TARGET_32BIT
10995                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
10996                                    GEN_INT (sp_offset))
10997                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
10998                                    GEN_INT (sp_offset)));
10999         }
11000     }
11001
11002   if (current_function_calls_eh_return)
11003     {
11004       rtx sa = EH_RETURN_STACKADJ_RTX;
11005       emit_insn (TARGET_32BIT
11006                  ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx, sa)
11007                  : gen_adddi3 (sp_reg_rtx, sp_reg_rtx, sa));
11008     }
11009
11010   if (!sibcall)
11011     {
11012       rtvec p;
11013       if (! restoring_FPRs_inline)
11014         p = rtvec_alloc (3 + 64 - info->first_fp_reg_save);
11015       else
11016         p = rtvec_alloc (2);
11017
11018       RTVEC_ELT (p, 0) = gen_rtx_RETURN (VOIDmode);
11019       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode, 
11020                                       gen_rtx_REG (Pmode, 
11021                                                    LINK_REGISTER_REGNUM));
11022
11023       /* If we have to restore more than two FP registers, branch to the
11024          restore function.  It will return to our caller.  */
11025       if (! restoring_FPRs_inline)
11026         {
11027           int i;
11028           char rname[30];
11029           const char *alloc_rname;
11030
11031           sprintf (rname, "%s%d%s", RESTORE_FP_PREFIX, 
11032                    info->first_fp_reg_save - 32, RESTORE_FP_SUFFIX);
11033           alloc_rname = ggc_strdup (rname);
11034           RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode,
11035                                           gen_rtx_SYMBOL_REF (Pmode,
11036                                                               alloc_rname));
11037
11038           for (i = 0; i < 64 - info->first_fp_reg_save; i++)
11039             {
11040               rtx addr, mem;
11041               addr = gen_rtx_PLUS (Pmode, sp_reg_rtx,
11042                                    GEN_INT (info->fp_save_offset + 8*i));
11043               mem = gen_rtx_MEM (DFmode, addr);
11044               set_mem_alias_set (mem, rs6000_sr_alias_set);
11045
11046               RTVEC_ELT (p, i+3) = 
11047                 gen_rtx_SET (VOIDmode,
11048                              gen_rtx_REG (DFmode, info->first_fp_reg_save + i),
11049                              mem);
11050             }
11051         }
11052       
11053       emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
11054     }
11055 }
11056
11057 /* Write function epilogue.  */
11058
11059 static void
11060 rs6000_output_function_epilogue (file, size)
11061      FILE *file;
11062      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
11063 {
11064   rs6000_stack_t *info = rs6000_stack_info ();
11065
11066   if (! HAVE_epilogue)
11067     {
11068       rtx insn = get_last_insn ();
11069       /* If the last insn was a BARRIER, we don't have to write anything except
11070          the trace table.  */
11071       if (GET_CODE (insn) == NOTE)
11072         insn = prev_nonnote_insn (insn);
11073       if (insn == 0 ||  GET_CODE (insn) != BARRIER)
11074         {
11075           /* This is slightly ugly, but at least we don't have two
11076              copies of the epilogue-emitting code.  */
11077           start_sequence ();
11078
11079           /* A NOTE_INSN_DELETED is supposed to be at the start
11080              and end of the "toplevel" insn chain.  */
11081           emit_note (0, NOTE_INSN_DELETED);
11082           rs6000_emit_epilogue (FALSE);
11083           emit_note (0, NOTE_INSN_DELETED);
11084
11085           /* Expand INSN_ADDRESSES so final() doesn't crash. */
11086           {
11087             rtx insn;
11088             unsigned addr = 0;
11089             for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
11090               {
11091                 INSN_ADDRESSES_NEW (insn, addr);
11092                 addr += 4;
11093               }
11094           }
11095
11096           if (TARGET_DEBUG_STACK)
11097             debug_rtx_list (get_insns (), 100);
11098           final (get_insns (), file, FALSE, FALSE);
11099           end_sequence ();
11100         }
11101     }
11102
11103   /* Output a traceback table here.  See /usr/include/sys/debug.h for info
11104      on its format.
11105
11106      We don't output a traceback table if -finhibit-size-directive was
11107      used.  The documentation for -finhibit-size-directive reads
11108      ``don't output a @code{.size} assembler directive, or anything
11109      else that would cause trouble if the function is split in the
11110      middle, and the two halves are placed at locations far apart in
11111      memory.''  The traceback table has this property, since it
11112      includes the offset from the start of the function to the
11113      traceback table itself.
11114
11115      System V.4 Powerpc's (and the embedded ABI derived from it) use a
11116      different traceback table.  */
11117   if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive
11118       && rs6000_traceback != traceback_none)
11119     {
11120       const char *fname = NULL;
11121       const char *language_string = lang_hooks.name;
11122       int fixed_parms = 0, float_parms = 0, parm_info = 0;
11123       int i;
11124       int optional_tbtab;
11125
11126       if (rs6000_traceback == traceback_full)
11127         optional_tbtab = 1;
11128       else if (rs6000_traceback == traceback_part)
11129         optional_tbtab = 0;
11130       else
11131         optional_tbtab = !optimize_size && !TARGET_ELF;
11132
11133       if (optional_tbtab)
11134         {
11135           fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
11136           while (*fname == '.') /* V.4 encodes . in the name */
11137             fname++;
11138
11139           /* Need label immediately before tbtab, so we can compute
11140              its offset from the function start.  */
11141           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
11142           ASM_OUTPUT_LABEL (file, fname);
11143         }
11144
11145       /* The .tbtab pseudo-op can only be used for the first eight
11146          expressions, since it can't handle the possibly variable
11147          length fields that follow.  However, if you omit the optional
11148          fields, the assembler outputs zeros for all optional fields
11149          anyways, giving each variable length field is minimum length
11150          (as defined in sys/debug.h).  Thus we can not use the .tbtab
11151          pseudo-op at all.  */
11152
11153       /* An all-zero word flags the start of the tbtab, for debuggers
11154          that have to find it by searching forward from the entry
11155          point or from the current pc.  */
11156       fputs ("\t.long 0\n", file);
11157
11158       /* Tbtab format type.  Use format type 0.  */
11159       fputs ("\t.byte 0,", file);
11160
11161       /* Language type.  Unfortunately, there doesn't seem to be any
11162          official way to get this info, so we use language_string.  C
11163          is 0.  C++ is 9.  No number defined for Obj-C, so use the
11164          value for C for now.  There is no official value for Java,
11165          although IBM appears to be using 13.  There is no official value
11166          for Chill, so we've chosen 44 pseudo-randomly.  */
11167       if (! strcmp (language_string, "GNU C")
11168           || ! strcmp (language_string, "GNU Objective-C"))
11169         i = 0;
11170       else if (! strcmp (language_string, "GNU F77"))
11171         i = 1;
11172       else if (! strcmp (language_string, "GNU Ada"))
11173         i = 3;
11174       else if (! strcmp (language_string, "GNU Pascal"))
11175         i = 2;
11176       else if (! strcmp (language_string, "GNU C++"))
11177         i = 9;
11178       else if (! strcmp (language_string, "GNU Java"))
11179         i = 13;
11180       else if (! strcmp (language_string, "GNU CHILL"))
11181         i = 44;
11182       else
11183         abort ();
11184       fprintf (file, "%d,", i);
11185
11186       /* 8 single bit fields: global linkage (not set for C extern linkage,
11187          apparently a PL/I convention?), out-of-line epilogue/prologue, offset
11188          from start of procedure stored in tbtab, internal function, function
11189          has controlled storage, function has no toc, function uses fp,
11190          function logs/aborts fp operations.  */
11191       /* Assume that fp operations are used if any fp reg must be saved.  */
11192       fprintf (file, "%d,",
11193                (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
11194
11195       /* 6 bitfields: function is interrupt handler, name present in
11196          proc table, function calls alloca, on condition directives
11197          (controls stack walks, 3 bits), saves condition reg, saves
11198          link reg.  */
11199       /* The `function calls alloca' bit seems to be set whenever reg 31 is
11200          set up as a frame pointer, even when there is no alloca call.  */
11201       fprintf (file, "%d,",
11202                ((optional_tbtab << 6)
11203                 | ((optional_tbtab & frame_pointer_needed) << 5)
11204                 | (info->cr_save_p << 1)
11205                 | (info->lr_save_p)));
11206
11207       /* 3 bitfields: saves backchain, fixup code, number of fpr saved
11208          (6 bits).  */
11209       fprintf (file, "%d,",
11210                (info->push_p << 7) | (64 - info->first_fp_reg_save));
11211
11212       /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits).  */
11213       fprintf (file, "%d,", (32 - first_reg_to_save ()));
11214
11215       if (optional_tbtab)
11216         {
11217           /* Compute the parameter info from the function decl argument
11218              list.  */
11219           tree decl;
11220           int next_parm_info_bit = 31;
11221
11222           for (decl = DECL_ARGUMENTS (current_function_decl);
11223                decl; decl = TREE_CHAIN (decl))
11224             {
11225               rtx parameter = DECL_INCOMING_RTL (decl);
11226               enum machine_mode mode = GET_MODE (parameter);
11227
11228               if (GET_CODE (parameter) == REG)
11229                 {
11230                   if (GET_MODE_CLASS (mode) == MODE_FLOAT)
11231                     {
11232                       int bits;
11233
11234                       float_parms++;
11235
11236                       if (mode == SFmode)
11237                         bits = 0x2;
11238                       else if (mode == DFmode || mode == TFmode)
11239                         bits = 0x3;
11240                       else
11241                         abort ();
11242
11243                       /* If only one bit will fit, don't or in this entry.  */
11244                       if (next_parm_info_bit > 0)
11245                         parm_info |= (bits << (next_parm_info_bit - 1));
11246                       next_parm_info_bit -= 2;
11247                     }
11248                   else
11249                     {
11250                       fixed_parms += ((GET_MODE_SIZE (mode)
11251                                        + (UNITS_PER_WORD - 1))
11252                                       / UNITS_PER_WORD);
11253                       next_parm_info_bit -= 1;
11254                     }
11255                 }
11256             }
11257         }
11258
11259       /* Number of fixed point parameters.  */
11260       /* This is actually the number of words of fixed point parameters; thus
11261          an 8 byte struct counts as 2; and thus the maximum value is 8.  */
11262       fprintf (file, "%d,", fixed_parms);
11263
11264       /* 2 bitfields: number of floating point parameters (7 bits), parameters
11265          all on stack.  */
11266       /* This is actually the number of fp registers that hold parameters;
11267          and thus the maximum value is 13.  */
11268       /* Set parameters on stack bit if parameters are not in their original
11269          registers, regardless of whether they are on the stack?  Xlc
11270          seems to set the bit when not optimizing.  */
11271       fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
11272
11273       if (! optional_tbtab)
11274         return;
11275
11276       /* Optional fields follow.  Some are variable length.  */
11277
11278       /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
11279          11 double float.  */
11280       /* There is an entry for each parameter in a register, in the order that
11281          they occur in the parameter list.  Any intervening arguments on the
11282          stack are ignored.  If the list overflows a long (max possible length
11283          34 bits) then completely leave off all elements that don't fit.  */
11284       /* Only emit this long if there was at least one parameter.  */
11285       if (fixed_parms || float_parms)
11286         fprintf (file, "\t.long %d\n", parm_info);
11287
11288       /* Offset from start of code to tb table.  */
11289       fputs ("\t.long ", file);
11290       ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
11291 #if TARGET_AIX
11292       RS6000_OUTPUT_BASENAME (file, fname);
11293 #else
11294       assemble_name (file, fname);
11295 #endif
11296       fputs ("-.", file);
11297 #if TARGET_AIX
11298       RS6000_OUTPUT_BASENAME (file, fname);
11299 #else
11300       assemble_name (file, fname);
11301 #endif
11302       putc ('\n', file);
11303
11304       /* Interrupt handler mask.  */
11305       /* Omit this long, since we never set the interrupt handler bit
11306          above.  */
11307
11308       /* Number of CTL (controlled storage) anchors.  */
11309       /* Omit this long, since the has_ctl bit is never set above.  */
11310
11311       /* Displacement into stack of each CTL anchor.  */
11312       /* Omit this list of longs, because there are no CTL anchors.  */
11313
11314       /* Length of function name.  */
11315       if (*fname == '*')
11316         ++fname;
11317       fprintf (file, "\t.short %d\n", (int) strlen (fname));
11318
11319       /* Function name.  */
11320       assemble_string (fname, strlen (fname));
11321
11322       /* Register for alloca automatic storage; this is always reg 31.
11323          Only emit this if the alloca bit was set above.  */
11324       if (frame_pointer_needed)
11325         fputs ("\t.byte 31\n", file);
11326
11327       fputs ("\t.align 2\n", file);
11328     }
11329 }
11330 \f
11331 /* A C compound statement that outputs the assembler code for a thunk
11332    function, used to implement C++ virtual function calls with
11333    multiple inheritance.  The thunk acts as a wrapper around a virtual
11334    function, adjusting the implicit object parameter before handing
11335    control off to the real function.
11336
11337    First, emit code to add the integer DELTA to the location that
11338    contains the incoming first argument.  Assume that this argument
11339    contains a pointer, and is the one used to pass the `this' pointer
11340    in C++.  This is the incoming argument *before* the function
11341    prologue, e.g. `%o0' on a sparc.  The addition must preserve the
11342    values of all other incoming arguments.
11343
11344    After the addition, emit code to jump to FUNCTION, which is a
11345    `FUNCTION_DECL'.  This is a direct pure jump, not a call, and does
11346    not touch the return address.  Hence returning from FUNCTION will
11347    return to whoever called the current `thunk'.
11348
11349    The effect must be as if FUNCTION had been called directly with the
11350    adjusted first argument.  This macro is responsible for emitting
11351    all of the code for a thunk function; output_function_prologue()
11352    and output_function_epilogue() are not invoked.
11353
11354    The THUNK_FNDECL is redundant.  (DELTA and FUNCTION have already
11355    been extracted from it.)  It might possibly be useful on some
11356    targets, but probably not.
11357
11358    If you do not define this macro, the target-independent code in the
11359    C++ frontend will generate a less efficient heavyweight thunk that
11360    calls FUNCTION instead of jumping to it.  The generic approach does
11361    not support varargs.  */
11362
11363 static void
11364 rs6000_output_mi_thunk (file, thunk_fndecl, delta, vcall_offset, function)
11365      FILE *file;
11366      tree thunk_fndecl ATTRIBUTE_UNUSED;
11367      HOST_WIDE_INT delta;
11368      HOST_WIDE_INT vcall_offset;
11369      tree function;
11370 {
11371   rtx this, insn, funexp;
11372
11373   reload_completed = 1;
11374   no_new_pseudos = 1;
11375
11376   /* Mark the end of the (empty) prologue.  */
11377   emit_note (NULL, NOTE_INSN_PROLOGUE_END);
11378
11379   /* Find the "this" pointer.  If the function returns a structure,
11380      the structure return pointer is in r3.  */
11381   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function))))
11382     this = gen_rtx_REG (Pmode, 4);
11383   else
11384     this = gen_rtx_REG (Pmode, 3);
11385
11386   /* Apply the constant offset, if required.  */
11387   if (delta)
11388     {
11389       rtx delta_rtx = GEN_INT (delta);
11390       emit_insn (TARGET_32BIT
11391                  ? gen_addsi3 (this, this, delta_rtx)
11392                  : gen_adddi3 (this, this, delta_rtx));
11393     }
11394
11395   /* Apply the offset from the vtable, if required.  */
11396   if (vcall_offset)
11397     {
11398       rtx vcall_offset_rtx = GEN_INT (vcall_offset);
11399       rtx tmp = gen_rtx_REG (Pmode, 12);
11400
11401       emit_move_insn (tmp, gen_rtx_MEM (Pmode, this));
11402       emit_insn (TARGET_32BIT
11403                  ? gen_addsi3 (tmp, tmp, vcall_offset_rtx)
11404                  : gen_adddi3 (tmp, tmp, vcall_offset_rtx));
11405       emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
11406       emit_insn (TARGET_32BIT
11407                  ? gen_addsi3 (this, this, tmp)
11408                  : gen_adddi3 (this, this, tmp));
11409     }
11410
11411   /* Generate a tail call to the target function.  */
11412   if (!TREE_USED (function))
11413     {
11414       assemble_external (function);
11415       TREE_USED (function) = 1;
11416     }
11417   funexp = XEXP (DECL_RTL (function), 0);
11418
11419   SYMBOL_REF_FLAG (funexp) = 0;
11420   if (current_file_function_operand (funexp, VOIDmode)
11421       && (! lookup_attribute ("longcall",
11422                               TYPE_ATTRIBUTES (TREE_TYPE (function)))
11423           || lookup_attribute ("shortcall",
11424                                TYPE_ATTRIBUTES (TREE_TYPE (function)))))
11425     SYMBOL_REF_FLAG (funexp) = 1;
11426
11427   funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
11428
11429 #if TARGET_MACHO
11430   if (flag_pic)
11431     funexp = machopic_indirect_call_target (funexp);
11432 #endif
11433
11434   /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
11435      generate sibcall RTL explicitly to avoid constraint abort.  */
11436   insn = emit_call_insn (
11437            gen_rtx_PARALLEL (VOIDmode,
11438              gen_rtvec (4,
11439                         gen_rtx_CALL (VOIDmode,
11440                                       funexp, const0_rtx),
11441                         gen_rtx_USE (VOIDmode, const0_rtx),
11442                         gen_rtx_USE (VOIDmode,
11443                                      gen_rtx_REG (SImode,
11444                                                   LINK_REGISTER_REGNUM)),
11445                         gen_rtx_RETURN (VOIDmode))));
11446   SIBLING_CALL_P (insn) = 1;
11447   emit_barrier ();
11448
11449   /* Run just enough of rest_of_compilation to get the insns emitted.
11450      There's not really enough bulk here to make other passes such as
11451      instruction scheduling worth while.  Note that use_thunk calls
11452      assemble_start_function and assemble_end_function.  */
11453   insn = get_insns ();
11454   shorten_branches (insn);
11455   final_start_function (insn, file, 1);
11456   final (insn, file, 1, 0);
11457   final_end_function ();
11458
11459   reload_completed = 0;
11460   no_new_pseudos = 0;
11461 }
11462 \f
11463 /* A quick summary of the various types of 'constant-pool tables'
11464    under PowerPC:
11465
11466    Target       Flags           Name            One table per   
11467    AIX          (none)          AIX TOC         object file
11468    AIX          -mfull-toc      AIX TOC         object file
11469    AIX          -mminimal-toc   AIX minimal TOC translation unit
11470    SVR4/EABI    (none)          SVR4 SDATA      object file
11471    SVR4/EABI    -fpic           SVR4 pic        object file
11472    SVR4/EABI    -fPIC           SVR4 PIC        translation unit
11473    SVR4/EABI    -mrelocatable   EABI TOC        function
11474    SVR4/EABI    -maix           AIX TOC         object file
11475    SVR4/EABI    -maix -mminimal-toc 
11476                                 AIX minimal TOC translation unit
11477
11478    Name                 Reg.    Set by  entries       contains:
11479                                         made by  addrs? fp?     sum?
11480
11481    AIX TOC              2       crt0    as       Y      option  option
11482    AIX minimal TOC      30      prolog  gcc      Y      Y       option
11483    SVR4 SDATA           13      crt0    gcc      N      Y       N
11484    SVR4 pic             30      prolog  ld       Y      not yet N
11485    SVR4 PIC             30      prolog  gcc      Y      option  option
11486    EABI TOC             30      prolog  gcc      Y      option  option
11487
11488 */
11489
11490 /* Hash functions for the hash table.  */
11491
11492 static unsigned
11493 rs6000_hash_constant (k)
11494      rtx k;
11495 {
11496   enum rtx_code code = GET_CODE (k);
11497   enum machine_mode mode = GET_MODE (k);
11498   unsigned result = (code << 3) ^ mode;
11499   const char *format;
11500   int flen, fidx;
11501   
11502   format = GET_RTX_FORMAT (code);
11503   flen = strlen (format);
11504   fidx = 0;
11505
11506   switch (code)
11507     {
11508     case LABEL_REF:
11509       return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
11510
11511     case CONST_DOUBLE:
11512       if (mode != VOIDmode)
11513         return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
11514       flen = 2;
11515       break;
11516
11517     case CODE_LABEL:
11518       fidx = 3;
11519       break;
11520
11521     default:
11522       break;
11523     }
11524
11525   for (; fidx < flen; fidx++)
11526     switch (format[fidx])
11527       {
11528       case 's':
11529         {
11530           unsigned i, len;
11531           const char *str = XSTR (k, fidx);
11532           len = strlen (str);
11533           result = result * 613 + len;
11534           for (i = 0; i < len; i++)
11535             result = result * 613 + (unsigned) str[i];
11536           break;
11537         }
11538       case 'u':
11539       case 'e':
11540         result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
11541         break;
11542       case 'i':
11543       case 'n':
11544         result = result * 613 + (unsigned) XINT (k, fidx);
11545         break;
11546       case 'w':
11547         if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
11548           result = result * 613 + (unsigned) XWINT (k, fidx);
11549         else
11550           {
11551             size_t i;
11552             for (i = 0; i < sizeof(HOST_WIDE_INT)/sizeof(unsigned); i++)
11553               result = result * 613 + (unsigned) (XWINT (k, fidx)
11554                                                   >> CHAR_BIT * i);
11555           }
11556         break;
11557       default:
11558         abort ();
11559       }
11560
11561   return result;
11562 }
11563
11564 static unsigned
11565 toc_hash_function (hash_entry)
11566      const void * hash_entry;
11567 {
11568   const struct toc_hash_struct *thc = 
11569     (const struct toc_hash_struct *) hash_entry;
11570   return rs6000_hash_constant (thc->key) ^ thc->key_mode;
11571 }
11572
11573 /* Compare H1 and H2 for equivalence.  */
11574
11575 static int
11576 toc_hash_eq (h1, h2)
11577      const void * h1;
11578      const void * h2;
11579 {
11580   rtx r1 = ((const struct toc_hash_struct *) h1)->key;
11581   rtx r2 = ((const struct toc_hash_struct *) h2)->key;
11582
11583   if (((const struct toc_hash_struct *) h1)->key_mode
11584       != ((const struct toc_hash_struct *) h2)->key_mode)
11585     return 0;
11586
11587   return rtx_equal_p (r1, r2);
11588 }
11589
11590 /* These are the names given by the C++ front-end to vtables, and
11591    vtable-like objects.  Ideally, this logic should not be here;
11592    instead, there should be some programmatic way of inquiring as
11593    to whether or not an object is a vtable.  */
11594
11595 #define VTABLE_NAME_P(NAME)                             \
11596   (strncmp ("_vt.", name, strlen("_vt.")) == 0          \
11597   || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0       \
11598   || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0       \
11599   || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0) 
11600
11601 void
11602 rs6000_output_symbol_ref (file, x)
11603      FILE *file;
11604      rtx x;
11605 {
11606   /* Currently C++ toc references to vtables can be emitted before it
11607      is decided whether the vtable is public or private.  If this is
11608      the case, then the linker will eventually complain that there is
11609      a reference to an unknown section.  Thus, for vtables only, 
11610      we emit the TOC reference to reference the symbol and not the
11611      section.  */
11612   const char *name = XSTR (x, 0);
11613
11614   if (VTABLE_NAME_P (name)) 
11615     {
11616       RS6000_OUTPUT_BASENAME (file, name);
11617     }
11618   else
11619     assemble_name (file, name);
11620 }
11621
11622 /* Output a TOC entry.  We derive the entry name from what is being
11623    written.  */
11624
11625 void
11626 output_toc (file, x, labelno, mode)
11627      FILE *file;
11628      rtx x;
11629      int labelno;
11630      enum machine_mode mode;
11631 {
11632   char buf[256];
11633   const char *name = buf;
11634   const char *real_name;
11635   rtx base = x;
11636   int offset = 0;
11637
11638   if (TARGET_NO_TOC)
11639     abort ();
11640
11641   /* When the linker won't eliminate them, don't output duplicate
11642      TOC entries (this happens on AIX if there is any kind of TOC,
11643      and on SVR4 under -fPIC or -mrelocatable).  Don't do this for
11644      CODE_LABELs.  */
11645   if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
11646     {
11647       struct toc_hash_struct *h;
11648       void * * found;
11649       
11650       /* Create toc_hash_table.  This can't be done at OVERRIDE_OPTIONS
11651          time because GGC is not initialised at that point.  */
11652       if (toc_hash_table == NULL)
11653         toc_hash_table = htab_create_ggc (1021, toc_hash_function, 
11654                                           toc_hash_eq, NULL);
11655
11656       h = ggc_alloc (sizeof (*h));
11657       h->key = x;
11658       h->key_mode = mode;
11659       h->labelno = labelno;
11660       
11661       found = htab_find_slot (toc_hash_table, h, 1);
11662       if (*found == NULL)
11663         *found = h;
11664       else  /* This is indeed a duplicate.  
11665                Set this label equal to that label.  */
11666         {
11667           fputs ("\t.set ", file);
11668           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
11669           fprintf (file, "%d,", labelno);
11670           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
11671           fprintf (file, "%d\n", ((*(const struct toc_hash_struct **) 
11672                                               found)->labelno));
11673           return;
11674         }
11675     }
11676
11677   /* If we're going to put a double constant in the TOC, make sure it's
11678      aligned properly when strict alignment is on.  */
11679   if (GET_CODE (x) == CONST_DOUBLE
11680       && STRICT_ALIGNMENT
11681       && GET_MODE_BITSIZE (mode) >= 64
11682       && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
11683     ASM_OUTPUT_ALIGN (file, 3);
11684   }
11685
11686   (*targetm.asm_out.internal_label) (file, "LC", labelno);
11687
11688   /* Handle FP constants specially.  Note that if we have a minimal
11689      TOC, things we put here aren't actually in the TOC, so we can allow
11690      FP constants.  */
11691   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == TFmode)
11692     {
11693       REAL_VALUE_TYPE rv;
11694       long k[4];
11695
11696       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
11697       REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
11698
11699       if (TARGET_64BIT)
11700         {
11701           if (TARGET_MINIMAL_TOC)
11702             fputs (DOUBLE_INT_ASM_OP, file);
11703           else
11704             fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
11705                      k[0] & 0xffffffff, k[1] & 0xffffffff,
11706                      k[2] & 0xffffffff, k[3] & 0xffffffff);
11707           fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
11708                    k[0] & 0xffffffff, k[1] & 0xffffffff,
11709                    k[2] & 0xffffffff, k[3] & 0xffffffff);
11710           return;
11711         }
11712       else
11713         {
11714           if (TARGET_MINIMAL_TOC)
11715             fputs ("\t.long ", file);
11716           else
11717             fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
11718                      k[0] & 0xffffffff, k[1] & 0xffffffff,
11719                      k[2] & 0xffffffff, k[3] & 0xffffffff);
11720           fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
11721                    k[0] & 0xffffffff, k[1] & 0xffffffff,
11722                    k[2] & 0xffffffff, k[3] & 0xffffffff);
11723           return;
11724         }
11725     }
11726   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
11727     {
11728       REAL_VALUE_TYPE rv;
11729       long k[2];
11730
11731       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
11732       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
11733
11734       if (TARGET_64BIT)
11735         {
11736           if (TARGET_MINIMAL_TOC)
11737             fputs (DOUBLE_INT_ASM_OP, file);
11738           else
11739             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
11740                      k[0] & 0xffffffff, k[1] & 0xffffffff);
11741           fprintf (file, "0x%lx%08lx\n",
11742                    k[0] & 0xffffffff, k[1] & 0xffffffff);
11743           return;
11744         }
11745       else
11746         {
11747           if (TARGET_MINIMAL_TOC)
11748             fputs ("\t.long ", file);
11749           else
11750             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
11751                      k[0] & 0xffffffff, k[1] & 0xffffffff);
11752           fprintf (file, "0x%lx,0x%lx\n",
11753                    k[0] & 0xffffffff, k[1] & 0xffffffff);
11754           return;
11755         }
11756     }
11757   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
11758     {
11759       REAL_VALUE_TYPE rv;
11760       long l;
11761
11762       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
11763       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
11764
11765       if (TARGET_64BIT)
11766         {
11767           if (TARGET_MINIMAL_TOC)
11768             fputs (DOUBLE_INT_ASM_OP, file);
11769           else
11770             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
11771           fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
11772           return;
11773         }
11774       else
11775         {
11776           if (TARGET_MINIMAL_TOC)
11777             fputs ("\t.long ", file);
11778           else
11779             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
11780           fprintf (file, "0x%lx\n", l & 0xffffffff);
11781           return;
11782         }
11783     }
11784   else if (GET_MODE (x) == VOIDmode
11785            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
11786     {
11787       unsigned HOST_WIDE_INT low;
11788       HOST_WIDE_INT high;
11789
11790       if (GET_CODE (x) == CONST_DOUBLE)
11791         {
11792           low = CONST_DOUBLE_LOW (x);
11793           high = CONST_DOUBLE_HIGH (x);
11794         }
11795       else
11796 #if HOST_BITS_PER_WIDE_INT == 32
11797         {
11798           low = INTVAL (x);
11799           high = (low & 0x80000000) ? ~0 : 0;
11800         }
11801 #else
11802         {
11803           low = INTVAL (x) & 0xffffffff;
11804           high = (HOST_WIDE_INT) INTVAL (x) >> 32;
11805         }
11806 #endif
11807
11808       /* TOC entries are always Pmode-sized, but since this
11809          is a bigendian machine then if we're putting smaller
11810          integer constants in the TOC we have to pad them.
11811          (This is still a win over putting the constants in
11812          a separate constant pool, because then we'd have
11813          to have both a TOC entry _and_ the actual constant.)
11814
11815          For a 32-bit target, CONST_INT values are loaded and shifted
11816          entirely within `low' and can be stored in one TOC entry.  */
11817
11818       if (TARGET_64BIT && POINTER_SIZE < GET_MODE_BITSIZE (mode))
11819         abort ();/* It would be easy to make this work, but it doesn't now.  */
11820
11821       if (POINTER_SIZE > GET_MODE_BITSIZE (mode))
11822         {
11823 #if HOST_BITS_PER_WIDE_INT == 32
11824           lshift_double (low, high, POINTER_SIZE - GET_MODE_BITSIZE (mode),
11825                          POINTER_SIZE, &low, &high, 0);
11826 #else
11827           low |= high << 32;
11828           low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
11829           high = (HOST_WIDE_INT) low >> 32;
11830           low &= 0xffffffff;
11831 #endif
11832         }
11833
11834       if (TARGET_64BIT)
11835         {
11836           if (TARGET_MINIMAL_TOC)
11837             fputs (DOUBLE_INT_ASM_OP, file);
11838           else
11839             fprintf (file, "\t.tc ID_%lx_%lx[TC],",
11840                      (long) high & 0xffffffff, (long) low & 0xffffffff);
11841           fprintf (file, "0x%lx%08lx\n",
11842                    (long) high & 0xffffffff, (long) low & 0xffffffff);
11843           return;
11844         }
11845       else
11846         {
11847           if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
11848             {
11849               if (TARGET_MINIMAL_TOC)
11850                 fputs ("\t.long ", file);
11851               else
11852                 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
11853                          (long) high & 0xffffffff, (long) low & 0xffffffff);
11854               fprintf (file, "0x%lx,0x%lx\n",
11855                        (long) high & 0xffffffff, (long) low & 0xffffffff);
11856             }
11857           else
11858             {
11859               if (TARGET_MINIMAL_TOC)
11860                 fputs ("\t.long ", file);
11861               else
11862                 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
11863               fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
11864             }
11865           return;
11866         }
11867     }
11868
11869   if (GET_CODE (x) == CONST)
11870     {
11871       if (GET_CODE (XEXP (x, 0)) != PLUS)
11872         abort ();
11873
11874       base = XEXP (XEXP (x, 0), 0);
11875       offset = INTVAL (XEXP (XEXP (x, 0), 1));
11876     }
11877   
11878   if (GET_CODE (base) == SYMBOL_REF)
11879     name = XSTR (base, 0);
11880   else if (GET_CODE (base) == LABEL_REF)
11881     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (XEXP (base, 0)));
11882   else if (GET_CODE (base) == CODE_LABEL)
11883     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
11884   else
11885     abort ();
11886
11887   real_name = (*targetm.strip_name_encoding) (name);
11888   if (TARGET_MINIMAL_TOC)
11889     fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
11890   else
11891     {
11892       fprintf (file, "\t.tc %s", real_name);
11893
11894       if (offset < 0)
11895         fprintf (file, ".N%d", - offset);
11896       else if (offset)
11897         fprintf (file, ".P%d", offset);
11898
11899       fputs ("[TC],", file);
11900     }
11901
11902   /* Currently C++ toc references to vtables can be emitted before it
11903      is decided whether the vtable is public or private.  If this is
11904      the case, then the linker will eventually complain that there is
11905      a TOC reference to an unknown section.  Thus, for vtables only,
11906      we emit the TOC reference to reference the symbol and not the
11907      section.  */
11908   if (VTABLE_NAME_P (name))
11909     {
11910       RS6000_OUTPUT_BASENAME (file, name);
11911       if (offset < 0)
11912         fprintf (file, "%d", offset);
11913       else if (offset > 0)
11914         fprintf (file, "+%d", offset);
11915     }
11916   else
11917     output_addr_const (file, x);
11918   putc ('\n', file);
11919 }
11920 \f
11921 /* Output an assembler pseudo-op to write an ASCII string of N characters
11922    starting at P to FILE.
11923
11924    On the RS/6000, we have to do this using the .byte operation and
11925    write out special characters outside the quoted string.
11926    Also, the assembler is broken; very long strings are truncated,
11927    so we must artificially break them up early.  */
11928
11929 void
11930 output_ascii (file, p, n)
11931      FILE *file;
11932      const char *p;
11933      int n;
11934 {
11935   char c;
11936   int i, count_string;
11937   const char *for_string = "\t.byte \"";
11938   const char *for_decimal = "\t.byte ";
11939   const char *to_close = NULL;
11940
11941   count_string = 0;
11942   for (i = 0; i < n; i++)
11943     {
11944       c = *p++;
11945       if (c >= ' ' && c < 0177)
11946         {
11947           if (for_string)
11948             fputs (for_string, file);
11949           putc (c, file);
11950
11951           /* Write two quotes to get one.  */
11952           if (c == '"')
11953             {
11954               putc (c, file);
11955               ++count_string;
11956             }
11957
11958           for_string = NULL;
11959           for_decimal = "\"\n\t.byte ";
11960           to_close = "\"\n";
11961           ++count_string;
11962
11963           if (count_string >= 512)
11964             {
11965               fputs (to_close, file);
11966
11967               for_string = "\t.byte \"";
11968               for_decimal = "\t.byte ";
11969               to_close = NULL;
11970               count_string = 0;
11971             }
11972         }
11973       else
11974         {
11975           if (for_decimal)
11976             fputs (for_decimal, file);
11977           fprintf (file, "%d", c);
11978
11979           for_string = "\n\t.byte \"";
11980           for_decimal = ", ";
11981           to_close = "\n";
11982           count_string = 0;
11983         }
11984     }
11985
11986   /* Now close the string if we have written one.  Then end the line.  */
11987   if (to_close)
11988     fputs (to_close, file);
11989 }
11990 \f
11991 /* Generate a unique section name for FILENAME for a section type
11992    represented by SECTION_DESC.  Output goes into BUF.
11993
11994    SECTION_DESC can be any string, as long as it is different for each
11995    possible section type.
11996
11997    We name the section in the same manner as xlc.  The name begins with an
11998    underscore followed by the filename (after stripping any leading directory
11999    names) with the last period replaced by the string SECTION_DESC.  If
12000    FILENAME does not contain a period, SECTION_DESC is appended to the end of
12001    the name.  */
12002
12003 void
12004 rs6000_gen_section_name (buf, filename, section_desc)
12005      char **buf;
12006      const char *filename;
12007      const char *section_desc;
12008 {
12009   const char *q, *after_last_slash, *last_period = 0;
12010   char *p;
12011   int len;
12012
12013   after_last_slash = filename;
12014   for (q = filename; *q; q++)
12015     {
12016       if (*q == '/')
12017         after_last_slash = q + 1;
12018       else if (*q == '.')
12019         last_period = q;
12020     }
12021
12022   len = strlen (after_last_slash) + strlen (section_desc) + 2;
12023   *buf = (char *) xmalloc (len);
12024
12025   p = *buf;
12026   *p++ = '_';
12027
12028   for (q = after_last_slash; *q; q++)
12029     {
12030       if (q == last_period)
12031         {
12032           strcpy (p, section_desc);
12033           p += strlen (section_desc);
12034           break;
12035         }
12036
12037       else if (ISALNUM (*q))
12038         *p++ = *q;
12039     }
12040
12041   if (last_period == 0)
12042     strcpy (p, section_desc);
12043   else
12044     *p = '\0';
12045 }
12046 \f
12047 /* Emit profile function.  */
12048
12049 void
12050 output_profile_hook (labelno)
12051      int labelno ATTRIBUTE_UNUSED;
12052 {
12053   if (DEFAULT_ABI == ABI_AIX)
12054     {
12055 #ifdef NO_PROFILE_COUNTERS
12056       emit_library_call (init_one_libfunc (RS6000_MCOUNT), 0, VOIDmode, 0);
12057 #else
12058       char buf[30];
12059       const char *label_name;
12060       rtx fun;
12061
12062       ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
12063       label_name = (*targetm.strip_name_encoding) (ggc_strdup (buf));
12064       fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
12065
12066       emit_library_call (init_one_libfunc (RS6000_MCOUNT), 0, VOIDmode, 1,
12067                          fun, Pmode);
12068 #endif
12069     }
12070   else if (DEFAULT_ABI == ABI_DARWIN)
12071     {
12072       const char *mcount_name = RS6000_MCOUNT;
12073       int caller_addr_regno = LINK_REGISTER_REGNUM;
12074
12075       /* Be conservative and always set this, at least for now.  */
12076       current_function_uses_pic_offset_table = 1;
12077
12078 #if TARGET_MACHO
12079       /* For PIC code, set up a stub and collect the caller's address
12080          from r0, which is where the prologue puts it.  */
12081       if (flag_pic)
12082         {
12083           mcount_name = machopic_stub_name (mcount_name);
12084           if (current_function_uses_pic_offset_table)
12085             caller_addr_regno = 0;
12086         }
12087 #endif
12088       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
12089                          0, VOIDmode, 1,
12090                          gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
12091     }
12092 }
12093
12094 /* Write function profiler code.  */
12095
12096 void
12097 output_function_profiler (file, labelno)
12098   FILE *file;
12099   int labelno;
12100 {
12101   char buf[100];
12102   int save_lr = 8;
12103
12104   ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
12105   switch (DEFAULT_ABI)
12106     {
12107     default:
12108       abort ();
12109
12110     case ABI_V4:
12111       save_lr = 4;
12112       /* Fall through.  */
12113
12114     case ABI_AIX_NODESC:
12115       if (!TARGET_32BIT)
12116         {
12117           warning ("no profiling of 64-bit code for this ABI");
12118           return;
12119         }
12120       fprintf (file, "\tmflr %s\n", reg_names[0]);
12121       if (flag_pic == 1)
12122         {
12123           fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
12124           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
12125                        reg_names[0], save_lr, reg_names[1]);
12126           asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
12127           asm_fprintf (file, "\t{l|lwz} %s,", reg_names[0]);
12128           assemble_name (file, buf);
12129           asm_fprintf (file, "@got(%s)\n", reg_names[12]);
12130         }
12131       else if (flag_pic > 1)
12132         {
12133           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
12134                        reg_names[0], save_lr, reg_names[1]);
12135           /* Now, we need to get the address of the label.  */
12136           fputs ("\tbl 1f\n\t.long ", file);
12137           assemble_name (file, buf);
12138           fputs ("-.\n1:", file);
12139           asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
12140           asm_fprintf (file, "\t{l|lwz} %s,0(%s)\n", 
12141                        reg_names[0], reg_names[11]);
12142           asm_fprintf (file, "\t{cax|add} %s,%s,%s\n",
12143                        reg_names[0], reg_names[0], reg_names[11]);
12144         }
12145       else
12146         {
12147           asm_fprintf (file, "\t{liu|lis} %s,", reg_names[12]);
12148           assemble_name (file, buf);
12149           fputs ("@ha\n", file);
12150           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
12151                        reg_names[0], save_lr, reg_names[1]);
12152           asm_fprintf (file, "\t{cal|la} %s,", reg_names[0]);
12153           assemble_name (file, buf);
12154           asm_fprintf (file, "@l(%s)\n", reg_names[12]);
12155         }
12156
12157       if (current_function_needs_context && DEFAULT_ABI == ABI_AIX_NODESC)
12158         {
12159           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
12160                        reg_names[STATIC_CHAIN_REGNUM],
12161                        12, reg_names[1]);
12162           fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
12163           asm_fprintf (file, "\t{l|lwz} %s,%d(%s)\n",
12164                        reg_names[STATIC_CHAIN_REGNUM],
12165                        12, reg_names[1]);
12166         }
12167       else
12168         /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH.  */
12169         fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
12170       break;
12171
12172     case ABI_AIX:
12173     case ABI_DARWIN:
12174       /* Don't do anything, done in output_profile_hook ().  */
12175       break;
12176     }
12177 }
12178
12179 /* Adjust the cost of a scheduling dependency.  Return the new cost of
12180    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
12181
12182 static int
12183 rs6000_adjust_cost (insn, link, dep_insn, cost)
12184      rtx insn;
12185      rtx link;
12186      rtx dep_insn ATTRIBUTE_UNUSED;
12187      int cost;
12188 {
12189   if (! recog_memoized (insn))
12190     return 0;
12191
12192   if (REG_NOTE_KIND (link) != 0)
12193     return 0;
12194
12195   if (REG_NOTE_KIND (link) == 0)
12196     {
12197       /* Data dependency; DEP_INSN writes a register that INSN reads
12198          some cycles later.  */
12199       switch (get_attr_type (insn))
12200         {
12201         case TYPE_JMPREG:
12202           /* Tell the first scheduling pass about the latency between
12203              a mtctr and bctr (and mtlr and br/blr).  The first
12204              scheduling pass will not know about this latency since
12205              the mtctr instruction, which has the latency associated
12206              to it, will be generated by reload.  */
12207           return TARGET_POWER ? 5 : 4;
12208         case TYPE_BRANCH:
12209           /* Leave some extra cycles between a compare and its
12210              dependent branch, to inhibit expensive mispredicts.  */
12211           if ((rs6000_cpu_attr == CPU_PPC603
12212                || rs6000_cpu_attr == CPU_PPC604
12213                || rs6000_cpu_attr == CPU_PPC604E
12214                || rs6000_cpu_attr == CPU_PPC620
12215                || rs6000_cpu_attr == CPU_PPC630
12216                || rs6000_cpu_attr == CPU_PPC750
12217                || rs6000_cpu_attr == CPU_PPC7400
12218                || rs6000_cpu_attr == CPU_PPC7450
12219                || rs6000_cpu_attr == CPU_POWER4)
12220               && recog_memoized (dep_insn)
12221               && (INSN_CODE (dep_insn) >= 0)
12222               && (get_attr_type (dep_insn) == TYPE_COMPARE
12223                   || get_attr_type (dep_insn) == TYPE_DELAYED_COMPARE
12224                   || get_attr_type (dep_insn) == TYPE_FPCOMPARE
12225                   || get_attr_type (dep_insn) == TYPE_CR_LOGICAL))
12226             return cost + 2;
12227         default:
12228           break;
12229         }
12230       /* Fall out to return default cost.  */
12231     }
12232
12233   return cost;
12234 }
12235
12236 /* A C statement (sans semicolon) to update the integer scheduling
12237    priority INSN_PRIORITY (INSN).  Reduce the priority to execute the
12238    INSN earlier, increase the priority to execute INSN later.  Do not
12239    define this macro if you do not need to adjust the scheduling
12240    priorities of insns.  */
12241
12242 static int
12243 rs6000_adjust_priority (insn, priority)
12244      rtx insn ATTRIBUTE_UNUSED;
12245      int priority;
12246 {
12247   /* On machines (like the 750) which have asymmetric integer units,
12248      where one integer unit can do multiply and divides and the other
12249      can't, reduce the priority of multiply/divide so it is scheduled
12250      before other integer operations.  */
12251
12252 #if 0
12253   if (! INSN_P (insn))
12254     return priority;
12255
12256   if (GET_CODE (PATTERN (insn)) == USE)
12257     return priority;
12258
12259   switch (rs6000_cpu_attr) {
12260   case CPU_PPC750:
12261     switch (get_attr_type (insn))
12262       {
12263       default:
12264         break;
12265
12266       case TYPE_IMUL:
12267       case TYPE_IDIV:
12268         fprintf (stderr, "priority was %#x (%d) before adjustment\n",
12269                  priority, priority);
12270         if (priority >= 0 && priority < 0x01000000)
12271           priority >>= 3;
12272         break;
12273       }
12274   }
12275 #endif
12276
12277   return priority;
12278 }
12279
12280 /* Return how many instructions the machine can issue per cycle.  */
12281
12282 static int
12283 rs6000_issue_rate ()
12284 {
12285   switch (rs6000_cpu_attr) {
12286   case CPU_RIOS1:  /* ? */
12287   case CPU_RS64A:
12288   case CPU_PPC601: /* ? */
12289   case CPU_PPC7450:
12290     return 3;
12291   case CPU_PPC603:
12292   case CPU_PPC750:
12293   case CPU_PPC7400:
12294     return 2; 
12295   case CPU_RIOS2:
12296   case CPU_PPC604:
12297   case CPU_PPC604E:
12298   case CPU_PPC620:
12299   case CPU_PPC630:
12300   case CPU_POWER4:
12301     return 4;
12302   default:
12303     return 1;
12304   }
12305 }
12306
12307 \f
12308 /* Length in units of the trampoline for entering a nested function.  */
12309
12310 int
12311 rs6000_trampoline_size ()
12312 {
12313   int ret = 0;
12314
12315   switch (DEFAULT_ABI)
12316     {
12317     default:
12318       abort ();
12319
12320     case ABI_AIX:
12321       ret = (TARGET_32BIT) ? 12 : 24;
12322       break;
12323
12324     case ABI_DARWIN:
12325     case ABI_V4:
12326     case ABI_AIX_NODESC:
12327       ret = (TARGET_32BIT) ? 40 : 48;
12328       break;
12329     }
12330
12331   return ret;
12332 }
12333
12334 /* Emit RTL insns to initialize the variable parts of a trampoline.
12335    FNADDR is an RTX for the address of the function's pure code.
12336    CXT is an RTX for the static chain value for the function.  */
12337
12338 void
12339 rs6000_initialize_trampoline (addr, fnaddr, cxt)
12340      rtx addr;
12341      rtx fnaddr;
12342      rtx cxt;
12343 {
12344   enum machine_mode pmode = Pmode;
12345   int regsize = (TARGET_32BIT) ? 4 : 8;
12346   rtx ctx_reg = force_reg (pmode, cxt);
12347
12348   switch (DEFAULT_ABI)
12349     {
12350     default:
12351       abort ();
12352
12353 /* Macros to shorten the code expansions below.  */
12354 #define MEM_DEREF(addr) gen_rtx_MEM (pmode, memory_address (pmode, addr))
12355 #define MEM_PLUS(addr,offset) \
12356   gen_rtx_MEM (pmode, memory_address (pmode, plus_constant (addr, offset)))
12357
12358     /* Under AIX, just build the 3 word function descriptor */
12359     case ABI_AIX:
12360       {
12361         rtx fn_reg = gen_reg_rtx (pmode);
12362         rtx toc_reg = gen_reg_rtx (pmode);
12363         emit_move_insn (fn_reg, MEM_DEREF (fnaddr));
12364         emit_move_insn (toc_reg, MEM_PLUS (fnaddr, regsize));
12365         emit_move_insn (MEM_DEREF (addr), fn_reg);
12366         emit_move_insn (MEM_PLUS (addr, regsize), toc_reg);
12367         emit_move_insn (MEM_PLUS (addr, 2*regsize), ctx_reg);
12368       }
12369       break;
12370
12371     /* Under V.4/eabi/darwin, __trampoline_setup does the real work.  */
12372     case ABI_DARWIN:
12373     case ABI_V4:
12374     case ABI_AIX_NODESC:
12375       emit_library_call (gen_rtx_SYMBOL_REF (SImode, "__trampoline_setup"),
12376                          FALSE, VOIDmode, 4,
12377                          addr, pmode,
12378                          GEN_INT (rs6000_trampoline_size ()), SImode,
12379                          fnaddr, pmode,
12380                          ctx_reg, pmode);
12381       break;
12382     }
12383
12384   return;
12385 }
12386
12387 \f
12388 /* Table of valid machine attributes.  */
12389
12390 const struct attribute_spec rs6000_attribute_table[] =
12391 {
12392   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
12393   { "longcall",  0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
12394   { "shortcall", 0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
12395   { NULL,        0, 0, false, false, false, NULL }
12396 };
12397
12398 /* Handle a "longcall" or "shortcall" attribute; arguments as in
12399    struct attribute_spec.handler.  */
12400
12401 static tree
12402 rs6000_handle_longcall_attribute (node, name, args, flags, no_add_attrs)
12403      tree *node;
12404      tree name;
12405      tree args ATTRIBUTE_UNUSED;
12406      int flags ATTRIBUTE_UNUSED;
12407      bool *no_add_attrs;
12408 {
12409   if (TREE_CODE (*node) != FUNCTION_TYPE
12410       && TREE_CODE (*node) != FIELD_DECL
12411       && TREE_CODE (*node) != TYPE_DECL)
12412     {
12413       warning ("`%s' attribute only applies to functions",
12414                IDENTIFIER_POINTER (name));
12415       *no_add_attrs = true;
12416     }
12417
12418   return NULL_TREE;
12419 }
12420
12421 /* Set longcall attributes on all functions declared when
12422    rs6000_default_long_calls is true.  */
12423 static void
12424 rs6000_set_default_type_attributes (type)
12425      tree type;
12426 {
12427   if (rs6000_default_long_calls
12428       && (TREE_CODE (type) == FUNCTION_TYPE
12429           || TREE_CODE (type) == METHOD_TYPE))
12430     TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
12431                                         NULL_TREE,
12432                                         TYPE_ATTRIBUTES (type));
12433 }
12434
12435 /* Return a reference suitable for calling a function with the
12436    longcall attribute.  */
12437
12438 struct rtx_def *
12439 rs6000_longcall_ref (call_ref)
12440      rtx call_ref;
12441 {
12442   const char *call_name;
12443   tree node;
12444
12445   if (GET_CODE (call_ref) != SYMBOL_REF)
12446     return call_ref;
12447
12448   /* System V adds '.' to the internal name, so skip them.  */
12449   call_name = XSTR (call_ref, 0);
12450   if (*call_name == '.')
12451     {
12452       while (*call_name == '.')
12453         call_name++;
12454
12455       node = get_identifier (call_name);
12456       call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
12457     }
12458
12459   return force_reg (Pmode, call_ref);
12460 }
12461
12462 \f
12463 #ifdef USING_ELFOS_H
12464
12465 /* A C statement or statements to switch to the appropriate section
12466    for output of RTX in mode MODE.  You can assume that RTX is some
12467    kind of constant in RTL.  The argument MODE is redundant except in
12468    the case of a `const_int' rtx.  Select the section by calling
12469    `text_section' or one of the alternatives for other sections.
12470
12471    Do not define this macro if you put all constants in the read-only
12472    data section.  */
12473
12474 static void
12475 rs6000_elf_select_rtx_section (mode, x, align)
12476      enum machine_mode mode;
12477      rtx x;
12478      unsigned HOST_WIDE_INT align;
12479 {
12480   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
12481     toc_section ();
12482   else
12483     default_elf_select_rtx_section (mode, x, align);
12484 }
12485
12486 /* A C statement or statements to switch to the appropriate
12487    section for output of DECL.  DECL is either a `VAR_DECL' node
12488    or a constant of some sort.  RELOC indicates whether forming
12489    the initial value of DECL requires link-time relocations.  */
12490
12491 static void
12492 rs6000_elf_select_section (decl, reloc, align)
12493      tree decl;
12494      int reloc;
12495      unsigned HOST_WIDE_INT align;
12496 {
12497   default_elf_select_section_1 (decl, reloc, align,
12498                                 flag_pic || DEFAULT_ABI == ABI_AIX);
12499 }
12500
12501 /* A C statement to build up a unique section name, expressed as a
12502    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
12503    RELOC indicates whether the initial value of EXP requires
12504    link-time relocations.  If you do not define this macro, GCC will use
12505    the symbol name prefixed by `.' as the section name.  Note - this
12506    macro can now be called for uninitialized data items as well as
12507    initialized data and functions.  */
12508
12509 static void
12510 rs6000_elf_unique_section (decl, reloc)
12511      tree decl;
12512      int reloc;
12513 {
12514   default_unique_section_1 (decl, reloc,
12515                             flag_pic || DEFAULT_ABI == ABI_AIX);
12516 }
12517
12518 \f
12519 /* If we are referencing a function that is static or is known to be
12520    in this file, make the SYMBOL_REF special.  We can use this to indicate
12521    that we can branch to this function without emitting a no-op after the
12522    call.  For real AIX calling sequences, we also replace the
12523    function name with the real name (1 or 2 leading .'s), rather than
12524    the function descriptor name.  This saves a lot of overriding code
12525    to read the prefixes.  */
12526
12527 static void
12528 rs6000_elf_encode_section_info (decl, first)
12529      tree decl;
12530      int first;
12531 {
12532   if (!first)
12533     return;
12534
12535   if (TREE_CODE (decl) == FUNCTION_DECL)
12536     {
12537       rtx sym_ref = XEXP (DECL_RTL (decl), 0);
12538       if ((*targetm.binds_local_p) (decl))
12539         SYMBOL_REF_FLAG (sym_ref) = 1;
12540
12541       if (DEFAULT_ABI == ABI_AIX)
12542         {
12543           size_t len1 = (DEFAULT_ABI == ABI_AIX) ? 1 : 2;
12544           size_t len2 = strlen (XSTR (sym_ref, 0));
12545           char *str = alloca (len1 + len2 + 1);
12546           str[0] = '.';
12547           str[1] = '.';
12548           memcpy (str + len1, XSTR (sym_ref, 0), len2 + 1);
12549
12550           XSTR (sym_ref, 0) = ggc_alloc_string (str, len1 + len2);
12551         }
12552     }
12553   else if (rs6000_sdata != SDATA_NONE
12554            && DEFAULT_ABI == ABI_V4
12555            && TREE_CODE (decl) == VAR_DECL)
12556     {
12557       rtx sym_ref = XEXP (DECL_RTL (decl), 0);
12558       int size = int_size_in_bytes (TREE_TYPE (decl));
12559       tree section_name = DECL_SECTION_NAME (decl);
12560       const char *name = (char *)0;
12561       int len = 0;
12562
12563       if ((*targetm.binds_local_p) (decl))
12564         SYMBOL_REF_FLAG (sym_ref) = 1;
12565
12566       if (section_name)
12567         {
12568           if (TREE_CODE (section_name) == STRING_CST)
12569             {
12570               name = TREE_STRING_POINTER (section_name);
12571               len = TREE_STRING_LENGTH (section_name);
12572             }
12573           else
12574             abort ();
12575         }
12576
12577       if ((size > 0 && size <= g_switch_value)
12578           || (name
12579               && ((len == sizeof (".sdata") - 1
12580                    && strcmp (name, ".sdata") == 0)
12581                   || (len == sizeof (".sdata2") - 1
12582                       && strcmp (name, ".sdata2") == 0)
12583                   || (len == sizeof (".sbss") - 1
12584                       && strcmp (name, ".sbss") == 0)
12585                   || (len == sizeof (".sbss2") - 1
12586                       && strcmp (name, ".sbss2") == 0)
12587                   || (len == sizeof (".PPC.EMB.sdata0") - 1
12588                       && strcmp (name, ".PPC.EMB.sdata0") == 0)
12589                   || (len == sizeof (".PPC.EMB.sbss0") - 1
12590                       && strcmp (name, ".PPC.EMB.sbss0") == 0))))
12591         {
12592           size_t len = strlen (XSTR (sym_ref, 0));
12593           char *str = alloca (len + 2);
12594
12595           str[0] = '@';
12596           memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
12597           XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
12598         }
12599     }
12600 }
12601
12602 static const char *
12603 rs6000_elf_strip_name_encoding (str)
12604      const char *str;
12605 {
12606   while (*str == '*' || *str == '@')
12607     str++;
12608   return str;
12609 }
12610
12611 static bool
12612 rs6000_elf_in_small_data_p (decl)
12613      tree decl;
12614 {
12615   if (rs6000_sdata == SDATA_NONE)
12616     return false;
12617
12618   if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
12619     {
12620       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
12621       if (strcmp (section, ".sdata") == 0
12622           || strcmp (section, ".sdata2") == 0
12623           || strcmp (section, ".sbss") == 0)
12624         return true;
12625     }
12626   else
12627     {
12628       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
12629
12630       if (size > 0
12631           && size <= g_switch_value
12632           && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
12633         return true;
12634     }
12635
12636   return false;
12637 }
12638
12639 #endif /* USING_ELFOS_H */
12640
12641 \f
12642 /* Return a REG that occurs in ADDR with coefficient 1.
12643    ADDR can be effectively incremented by incrementing REG.
12644
12645    r0 is special and we must not select it as an address
12646    register by this routine since our caller will try to
12647    increment the returned register via an "la" instruction.  */
12648
12649 struct rtx_def *
12650 find_addr_reg (addr)
12651      rtx addr;
12652 {
12653   while (GET_CODE (addr) == PLUS)
12654     {
12655       if (GET_CODE (XEXP (addr, 0)) == REG
12656           && REGNO (XEXP (addr, 0)) != 0)
12657         addr = XEXP (addr, 0);
12658       else if (GET_CODE (XEXP (addr, 1)) == REG
12659                && REGNO (XEXP (addr, 1)) != 0)
12660         addr = XEXP (addr, 1);
12661       else if (CONSTANT_P (XEXP (addr, 0)))
12662         addr = XEXP (addr, 1);
12663       else if (CONSTANT_P (XEXP (addr, 1)))
12664         addr = XEXP (addr, 0);
12665       else
12666         abort ();
12667     }
12668   if (GET_CODE (addr) == REG && REGNO (addr) != 0)
12669     return addr;
12670   abort ();
12671 }
12672
12673 void
12674 rs6000_fatal_bad_address (op)
12675   rtx op;
12676 {
12677   fatal_insn ("bad address", op);
12678 }
12679
12680 #if TARGET_MACHO
12681
12682 #if 0
12683 /* Returns 1 if OP is either a symbol reference or a sum of a symbol
12684    reference and a constant.  */
12685
12686 int
12687 symbolic_operand (op)
12688      rtx op;
12689 {
12690   switch (GET_CODE (op))
12691     {
12692     case SYMBOL_REF:
12693     case LABEL_REF:
12694       return 1;
12695     case CONST:
12696       op = XEXP (op, 0);
12697       return (GET_CODE (op) == SYMBOL_REF ||
12698               (GET_CODE (XEXP (op, 0)) == SYMBOL_REF
12699                || GET_CODE (XEXP (op, 0)) == LABEL_REF)
12700               && GET_CODE (XEXP (op, 1)) == CONST_INT);
12701     default:
12702       return 0;
12703     }
12704 }
12705 #endif
12706
12707 #ifdef RS6000_LONG_BRANCH
12708
12709 static tree stub_list = 0;
12710
12711 /* ADD_COMPILER_STUB adds the compiler generated stub for handling 
12712    procedure calls to the linked list.  */
12713
12714 void 
12715 add_compiler_stub (label_name, function_name, line_number)
12716      tree label_name;
12717      tree function_name;
12718      int line_number;
12719 {
12720   tree stub = build_tree_list (function_name, label_name);
12721   TREE_TYPE (stub) = build_int_2 (line_number, 0);
12722   TREE_CHAIN (stub) = stub_list;
12723   stub_list = stub;
12724 }
12725
12726 #define STUB_LABEL_NAME(STUB)     TREE_VALUE (STUB)
12727 #define STUB_FUNCTION_NAME(STUB)  TREE_PURPOSE (STUB)
12728 #define STUB_LINE_NUMBER(STUB)    TREE_INT_CST_LOW (TREE_TYPE (STUB))
12729
12730 /* OUTPUT_COMPILER_STUB outputs the compiler generated stub for
12731    handling procedure calls from the linked list and initializes the
12732    linked list.  */
12733
12734 void
12735 output_compiler_stub ()
12736 {
12737   char tmp_buf[256];
12738   char label_buf[256];
12739   tree stub;
12740
12741   if (!flag_pic)
12742     for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
12743       {
12744         fprintf (asm_out_file,
12745                  "%s:\n", IDENTIFIER_POINTER(STUB_LABEL_NAME(stub)));
12746
12747 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
12748         if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
12749           fprintf (asm_out_file, "\t.stabd 68,0,%d\n", STUB_LINE_NUMBER(stub));
12750 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
12751
12752         if (IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub))[0] == '*')
12753           strcpy (label_buf,
12754                   IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub))+1);
12755         else
12756           {
12757             label_buf[0] = '_';
12758             strcpy (label_buf+1,
12759                     IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub)));
12760           }
12761
12762         strcpy (tmp_buf, "lis r12,hi16(");
12763         strcat (tmp_buf, label_buf);
12764         strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
12765         strcat (tmp_buf, label_buf);
12766         strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
12767         output_asm_insn (tmp_buf, 0);
12768
12769 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
12770         if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
12771           fprintf(asm_out_file, "\t.stabd 68,0,%d\n", STUB_LINE_NUMBER (stub));
12772 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
12773       }
12774
12775   stub_list = 0;
12776 }
12777
12778 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
12779    already there or not.  */
12780
12781 int
12782 no_previous_def (function_name)
12783      tree function_name;
12784 {
12785   tree stub;
12786   for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
12787     if (function_name == STUB_FUNCTION_NAME (stub))
12788       return 0;
12789   return 1;
12790 }
12791
12792 /* GET_PREV_LABEL gets the label name from the previous definition of
12793    the function.  */
12794
12795 tree
12796 get_prev_label (function_name)
12797      tree function_name;
12798 {
12799   tree stub;
12800   for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
12801     if (function_name == STUB_FUNCTION_NAME (stub))
12802       return STUB_LABEL_NAME (stub);
12803   return 0;
12804 }
12805
12806 /* INSN is either a function call or a millicode call.  It may have an
12807    unconditional jump in its delay slot.  
12808
12809    CALL_DEST is the routine we are calling.  */
12810
12811 char *
12812 output_call (insn, call_dest, operand_number)
12813      rtx insn;
12814      rtx call_dest;
12815      int operand_number;
12816 {
12817   static char buf[256];
12818   if (GET_CODE (call_dest) == SYMBOL_REF && TARGET_LONG_BRANCH && !flag_pic)
12819     {
12820       tree labelname;
12821       tree funname = get_identifier (XSTR (call_dest, 0));
12822       
12823       if (no_previous_def (funname))
12824         {
12825           int line_number = 0;
12826           rtx label_rtx = gen_label_rtx ();
12827           char *label_buf, temp_buf[256];
12828           ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
12829                                        CODE_LABEL_NUMBER (label_rtx));
12830           label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
12831           labelname = get_identifier (label_buf);
12832           for (; insn && GET_CODE (insn) != NOTE; insn = PREV_INSN (insn));
12833           if (insn)
12834             line_number = NOTE_LINE_NUMBER (insn);
12835           add_compiler_stub (labelname, funname, line_number);
12836         }
12837       else
12838         labelname = get_prev_label (funname);
12839
12840       sprintf (buf, "jbsr %%z%d,%.246s",
12841                operand_number, IDENTIFIER_POINTER (labelname));
12842       return buf;
12843     }
12844   else
12845     {
12846       sprintf (buf, "bl %%z%d", operand_number);
12847       return buf;
12848     }
12849 }
12850
12851 #endif /* RS6000_LONG_BRANCH */
12852
12853 #define GEN_LOCAL_LABEL_FOR_SYMBOL(BUF,SYMBOL,LENGTH,N)         \
12854   do {                                                          \
12855     const char *const symbol_ = (SYMBOL);                       \
12856     char *buffer_ = (BUF);                                      \
12857     if (symbol_[0] == '"')                                      \
12858       {                                                         \
12859         sprintf(buffer_, "\"L%d$%s", (N), symbol_+1);           \
12860       }                                                         \
12861     else if (name_needs_quotes(symbol_))                        \
12862       {                                                         \
12863         sprintf(buffer_, "\"L%d$%s\"", (N), symbol_);           \
12864       }                                                         \
12865     else                                                        \
12866       {                                                         \
12867         sprintf(buffer_, "L%d$%s", (N), symbol_);               \
12868       }                                                         \
12869   } while (0)
12870
12871
12872 /* Generate PIC and indirect symbol stubs.  */
12873
12874 void
12875 machopic_output_stub (file, symb, stub)
12876      FILE *file;
12877      const char *symb, *stub;
12878 {
12879   unsigned int length;
12880   char *symbol_name, *lazy_ptr_name;
12881   char *local_label_0;
12882   static int label = 0;
12883
12884   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
12885   symb = (*targetm.strip_name_encoding) (symb);
12886
12887   label += 1;
12888
12889   length = strlen (symb);
12890   symbol_name = alloca (length + 32);
12891   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
12892
12893   lazy_ptr_name = alloca (length + 32);
12894   GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
12895
12896   local_label_0 = alloca (length + 32);
12897   GEN_LOCAL_LABEL_FOR_SYMBOL (local_label_0, symb, length, 0);
12898
12899   if (flag_pic == 2)
12900     machopic_picsymbol_stub1_section ();
12901   else
12902     machopic_symbol_stub1_section ();
12903   fprintf (file, "\t.align 2\n");
12904
12905   fprintf (file, "%s:\n", stub);
12906   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
12907
12908   if (flag_pic == 2)
12909     {
12910       fprintf (file, "\tmflr r0\n");
12911       fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
12912       fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
12913       fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
12914                lazy_ptr_name, local_label_0);
12915       fprintf (file, "\tmtlr r0\n");
12916       fprintf (file, "\tlwzu r12,lo16(%s-%s)(r11)\n",
12917                lazy_ptr_name, local_label_0);
12918       fprintf (file, "\tmtctr r12\n");
12919       fprintf (file, "\tbctr\n");
12920     }
12921   else
12922     fprintf (file, "non-pure not supported\n");
12923   
12924   machopic_lazy_symbol_ptr_section ();
12925   fprintf (file, "%s:\n", lazy_ptr_name);
12926   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
12927   fprintf (file, "\t.long dyld_stub_binding_helper\n");
12928 }
12929
12930 /* Legitimize PIC addresses.  If the address is already
12931    position-independent, we return ORIG.  Newly generated
12932    position-independent addresses go into a reg.  This is REG if non
12933    zero, otherwise we allocate register(s) as necessary.  */
12934
12935 #define SMALL_INT(X) ((unsigned) (INTVAL(X) + 0x8000) < 0x10000)
12936
12937 rtx
12938 rs6000_machopic_legitimize_pic_address (orig, mode, reg)
12939      rtx orig;
12940      enum machine_mode mode;
12941      rtx reg;
12942 {
12943   rtx base, offset;
12944
12945   if (reg == NULL && ! reload_in_progress && ! reload_completed)
12946     reg = gen_reg_rtx (Pmode);
12947
12948   if (GET_CODE (orig) == CONST)
12949     {
12950       if (GET_CODE (XEXP (orig, 0)) == PLUS
12951           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
12952         return orig;
12953
12954       if (GET_CODE (XEXP (orig, 0)) == PLUS)
12955         {
12956           base =
12957             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
12958                                                     Pmode, reg);
12959           offset =
12960             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
12961                                                     Pmode, reg);
12962         }
12963       else
12964         abort ();
12965
12966       if (GET_CODE (offset) == CONST_INT)
12967         {
12968           if (SMALL_INT (offset))
12969             return plus_constant (base, INTVAL (offset));
12970           else if (! reload_in_progress && ! reload_completed)
12971             offset = force_reg (Pmode, offset);
12972           else
12973             {
12974               rtx mem = force_const_mem (Pmode, orig);
12975               return machopic_legitimize_pic_address (mem, Pmode, reg);
12976             }
12977         }
12978       return gen_rtx (PLUS, Pmode, base, offset);
12979     }
12980
12981   /* Fall back on generic machopic code.  */
12982   return machopic_legitimize_pic_address (orig, mode, reg);
12983 }
12984
12985 /* This is just a placeholder to make linking work without having to
12986    add this to the generic Darwin EXTRA_SECTIONS.  If -mcall-aix is
12987    ever needed for Darwin (not too likely!) this would have to get a
12988    real definition.  */
12989
12990 void
12991 toc_section ()
12992 {
12993 }
12994
12995 #endif /* TARGET_MACHO */
12996
12997 #if TARGET_ELF
12998 static unsigned int
12999 rs6000_elf_section_type_flags (decl, name, reloc)
13000      tree decl;
13001      const char *name;
13002      int reloc;
13003 {
13004   unsigned int flags
13005     = default_section_type_flags_1 (decl, name, reloc,
13006                                     flag_pic || DEFAULT_ABI == ABI_AIX);
13007
13008   if (TARGET_RELOCATABLE)
13009     flags |= SECTION_WRITE;
13010
13011   return flags;
13012 }
13013
13014 /* Record an element in the table of global constructors.  SYMBOL is
13015    a SYMBOL_REF of the function to be called; PRIORITY is a number
13016    between 0 and MAX_INIT_PRIORITY.
13017
13018    This differs from default_named_section_asm_out_constructor in
13019    that we have special handling for -mrelocatable.  */
13020
13021 static void
13022 rs6000_elf_asm_out_constructor (symbol, priority)
13023      rtx symbol;
13024      int priority;
13025 {
13026   const char *section = ".ctors";
13027   char buf[16];
13028
13029   if (priority != DEFAULT_INIT_PRIORITY)
13030     {
13031       sprintf (buf, ".ctors.%.5u",
13032                /* Invert the numbering so the linker puts us in the proper
13033                   order; constructors are run from right to left, and the
13034                   linker sorts in increasing order.  */
13035                MAX_INIT_PRIORITY - priority);
13036       section = buf;
13037     }
13038
13039   named_section_flags (section, SECTION_WRITE);
13040   assemble_align (POINTER_SIZE);
13041
13042   if (TARGET_RELOCATABLE)
13043     {
13044       fputs ("\t.long (", asm_out_file);
13045       output_addr_const (asm_out_file, symbol);
13046       fputs (")@fixup\n", asm_out_file);
13047     }
13048   else
13049     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
13050 }
13051
13052 static void
13053 rs6000_elf_asm_out_destructor (symbol, priority)
13054      rtx symbol;
13055      int priority;
13056 {
13057   const char *section = ".dtors";
13058   char buf[16];
13059
13060   if (priority != DEFAULT_INIT_PRIORITY)
13061     {
13062       sprintf (buf, ".dtors.%.5u",
13063                /* Invert the numbering so the linker puts us in the proper
13064                   order; constructors are run from right to left, and the
13065                   linker sorts in increasing order.  */
13066                MAX_INIT_PRIORITY - priority);
13067       section = buf;
13068     }
13069
13070   named_section_flags (section, SECTION_WRITE);
13071   assemble_align (POINTER_SIZE);
13072
13073   if (TARGET_RELOCATABLE)
13074     {
13075       fputs ("\t.long (", asm_out_file);
13076       output_addr_const (asm_out_file, symbol);
13077       fputs (")@fixup\n", asm_out_file);
13078     }
13079   else
13080     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
13081 }
13082 #endif
13083
13084 #if TARGET_XCOFF
13085 static void
13086 rs6000_xcoff_asm_globalize_label (stream, name)
13087      FILE *stream;
13088      const char *name;
13089 {
13090   fputs (GLOBAL_ASM_OP, stream);
13091   RS6000_OUTPUT_BASENAME (stream, name);
13092   putc ('\n', stream);
13093 }
13094
13095 static void
13096 rs6000_xcoff_asm_named_section (name, flags)
13097      const char *name;
13098      unsigned int flags;
13099 {
13100   int smclass;
13101   static const char * const suffix[3] = { "PR", "RO", "RW" };
13102
13103   if (flags & SECTION_CODE)
13104     smclass = 0;
13105   else if (flags & SECTION_WRITE)
13106     smclass = 2;
13107   else
13108     smclass = 1;
13109
13110   fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
13111            (flags & SECTION_CODE) ? "." : "",
13112            name, suffix[smclass], flags & SECTION_ENTSIZE);
13113 }
13114
13115 static void
13116 rs6000_xcoff_select_section (decl, reloc, align)
13117      tree decl;
13118      int reloc;
13119      unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
13120 {
13121   if (decl_readonly_section_1 (decl, reloc, 1))
13122     {
13123       if (TREE_PUBLIC (decl))
13124         read_only_data_section ();
13125       else
13126         read_only_private_data_section ();
13127     }
13128   else
13129     {
13130       if (TREE_PUBLIC (decl))
13131         data_section ();
13132       else
13133         private_data_section ();
13134     }
13135 }
13136
13137 static void
13138 rs6000_xcoff_unique_section (decl, reloc)
13139      tree decl;
13140      int reloc ATTRIBUTE_UNUSED;
13141 {
13142   const char *name;
13143
13144   /* Use select_section for private and uninitialized data.  */
13145   if (!TREE_PUBLIC (decl)
13146       || DECL_COMMON (decl)
13147       || DECL_INITIAL (decl) == NULL_TREE
13148       || DECL_INITIAL (decl) == error_mark_node
13149       || (flag_zero_initialized_in_bss
13150           && initializer_zerop (DECL_INITIAL (decl))))
13151     return;
13152
13153   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
13154   name = (*targetm.strip_name_encoding) (name);
13155   DECL_SECTION_NAME (decl) = build_string (strlen (name), name);
13156 }
13157
13158 /* Select section for constant in constant pool.
13159
13160    On RS/6000, all constants are in the private read-only data area.
13161    However, if this is being placed in the TOC it must be output as a
13162    toc entry.  */
13163
13164 static void
13165 rs6000_xcoff_select_rtx_section (mode, x, align)
13166      enum machine_mode mode;
13167      rtx x;
13168      unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
13169 {
13170   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
13171     toc_section ();
13172   else
13173     read_only_private_data_section ();
13174 }
13175
13176 /* Remove any trailing [DS] or the like from the symbol name.  */
13177
13178 static const char *
13179 rs6000_xcoff_strip_name_encoding (name)
13180      const char *name;
13181 {
13182   size_t len;
13183   if (*name == '*')
13184     name++;
13185   len = strlen (name);
13186   if (name[len - 1] == ']')
13187     return ggc_alloc_string (name, len - 4);
13188   else
13189     return name;
13190 }
13191
13192 /* Section attributes.  AIX is always PIC.  */
13193
13194 static unsigned int
13195 rs6000_xcoff_section_type_flags (decl, name, reloc)
13196      tree decl;
13197      const char *name;
13198      int reloc;
13199 {
13200   unsigned int align;
13201   unsigned int flags = default_section_type_flags_1 (decl, name, reloc, 1);
13202
13203   /* Align to at least UNIT size.  */
13204   if (flags & SECTION_CODE)
13205     align = MIN_UNITS_PER_WORD;
13206   else
13207     /* Increase alignment of large objects if not already stricter.  */
13208     align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
13209                  int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
13210                  ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
13211
13212   return flags | (exact_log2 (align) & SECTION_ENTSIZE);
13213 }
13214
13215 #endif /* TARGET_XCOFF */
13216
13217 /* Note that this is also used for PPC64 Linux.  */
13218
13219 static void
13220 rs6000_xcoff_encode_section_info (decl, first)
13221      tree decl;
13222      int first ATTRIBUTE_UNUSED;
13223 {
13224   if (TREE_CODE (decl) == FUNCTION_DECL
13225       && (*targetm.binds_local_p) (decl))
13226     SYMBOL_REF_FLAG (XEXP (DECL_RTL (decl), 0)) = 1;
13227 }
13228
13229 /* Cross-module name binding.  For AIX and PPC64 Linux, which always are
13230    PIC, use private copy of flag_pic.  */
13231
13232 static bool
13233 rs6000_binds_local_p (decl)
13234      tree decl;
13235 {
13236   return default_binds_local_p_1 (decl, flag_pic || rs6000_flag_pic);
13237 }
13238
13239 /* Compute a (partial) cost for rtx X.  Return true if the complete
13240    cost has been computed, and false if subexpressions should be
13241    scanned.  In either case, *TOTAL contains the cost result.  */
13242
13243 static bool
13244 rs6000_rtx_costs (x, code, outer_code, total)
13245      rtx x;
13246      int code, outer_code ATTRIBUTE_UNUSED;
13247      int *total;
13248 {
13249   switch (code)
13250     {
13251       /* On the RS/6000, if it is valid in the insn, it is free.
13252          So this always returns 0.  */
13253     case CONST_INT:
13254     case CONST:
13255     case LABEL_REF:
13256     case SYMBOL_REF:
13257     case CONST_DOUBLE:
13258     case HIGH:
13259       *total = 0;
13260       return true;
13261
13262     case PLUS:
13263       *total = ((GET_CODE (XEXP (x, 1)) == CONST_INT
13264                  && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1))
13265                                                + 0x8000) >= 0x10000)
13266                  && ((INTVAL (XEXP (x, 1)) & 0xffff) != 0))
13267                 ? COSTS_N_INSNS (2)
13268                 : COSTS_N_INSNS (1));
13269       return true;
13270
13271     case AND:
13272     case IOR:
13273     case XOR:
13274       *total = ((GET_CODE (XEXP (x, 1)) == CONST_INT
13275                  && (INTVAL (XEXP (x, 1)) & (~ (HOST_WIDE_INT) 0xffff)) != 0
13276                  && ((INTVAL (XEXP (x, 1)) & 0xffff) != 0))
13277                 ? COSTS_N_INSNS (2)
13278                 : COSTS_N_INSNS (1));
13279       return true;
13280
13281     case MULT:
13282       if (optimize_size)
13283         {
13284           *total = COSTS_N_INSNS (2);
13285           return true;
13286         }
13287       switch (rs6000_cpu)
13288         {
13289         case PROCESSOR_RIOS1:
13290         case PROCESSOR_PPC405:
13291           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
13292                     ? COSTS_N_INSNS (5)
13293                     : (INTVAL (XEXP (x, 1)) >= -256
13294                        && INTVAL (XEXP (x, 1)) <= 255)
13295                     ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4));
13296           return true;
13297
13298         case PROCESSOR_RS64A:
13299           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
13300                     ? GET_MODE (XEXP (x, 1)) != DImode
13301                     ? COSTS_N_INSNS (20) : COSTS_N_INSNS (34)
13302                     : (INTVAL (XEXP (x, 1)) >= -256
13303                        && INTVAL (XEXP (x, 1)) <= 255)
13304                     ? COSTS_N_INSNS (8) : COSTS_N_INSNS (12));
13305           return true;
13306
13307         case PROCESSOR_RIOS2:
13308         case PROCESSOR_MPCCORE:
13309         case PROCESSOR_PPC604e:
13310           *total = COSTS_N_INSNS (2);
13311           return true;
13312
13313         case PROCESSOR_PPC601:
13314           *total = COSTS_N_INSNS (5);
13315           return true;
13316
13317         case PROCESSOR_PPC603:
13318         case PROCESSOR_PPC7400:
13319         case PROCESSOR_PPC750:
13320           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
13321                     ? COSTS_N_INSNS (5)
13322                     : (INTVAL (XEXP (x, 1)) >= -256
13323                        && INTVAL (XEXP (x, 1)) <= 255)
13324                     ? COSTS_N_INSNS (2) : COSTS_N_INSNS (3));
13325           return true;
13326
13327         case PROCESSOR_PPC7450:
13328           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
13329                     ? COSTS_N_INSNS (4)
13330                     : COSTS_N_INSNS (3));
13331           return true;
13332
13333         case PROCESSOR_PPC403:
13334         case PROCESSOR_PPC604:
13335         case PROCESSOR_PPC8540:
13336           *total = COSTS_N_INSNS (4);
13337           return true;
13338
13339         case PROCESSOR_PPC620:
13340         case PROCESSOR_PPC630:
13341         case PROCESSOR_POWER4:
13342           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
13343                     ? GET_MODE (XEXP (x, 1)) != DImode
13344                     ? COSTS_N_INSNS (5) : COSTS_N_INSNS (7)
13345                     : (INTVAL (XEXP (x, 1)) >= -256
13346                        && INTVAL (XEXP (x, 1)) <= 255)
13347                     ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4));
13348           return true;
13349
13350         default:
13351           abort ();
13352         }
13353
13354     case DIV:
13355     case MOD:
13356       if (GET_CODE (XEXP (x, 1)) == CONST_INT
13357           && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
13358         {
13359           *total = COSTS_N_INSNS (2);
13360           return true;
13361         }
13362       /* FALLTHRU */
13363
13364     case UDIV:
13365     case UMOD:
13366       switch (rs6000_cpu)
13367         {
13368         case PROCESSOR_RIOS1:
13369           *total = COSTS_N_INSNS (19);
13370           return true;
13371
13372         case PROCESSOR_RIOS2:
13373           *total = COSTS_N_INSNS (13);
13374           return true;
13375
13376         case PROCESSOR_RS64A:
13377           *total = (GET_MODE (XEXP (x, 1)) != DImode
13378                     ? COSTS_N_INSNS (65)
13379                     : COSTS_N_INSNS (67));
13380           return true;
13381
13382         case PROCESSOR_MPCCORE:
13383           *total = COSTS_N_INSNS (6);
13384           return true;
13385
13386         case PROCESSOR_PPC403:
13387           *total = COSTS_N_INSNS (33);
13388           return true;
13389
13390         case PROCESSOR_PPC405:
13391           *total = COSTS_N_INSNS (35);
13392           return true;
13393
13394         case PROCESSOR_PPC601:
13395           *total = COSTS_N_INSNS (36);
13396           return true;
13397
13398         case PROCESSOR_PPC603:
13399           *total = COSTS_N_INSNS (37);
13400           return true;
13401
13402         case PROCESSOR_PPC604:
13403         case PROCESSOR_PPC604e:
13404           *total = COSTS_N_INSNS (20);
13405           return true;
13406
13407         case PROCESSOR_PPC620:
13408         case PROCESSOR_PPC630:
13409         case PROCESSOR_POWER4:
13410           *total = (GET_MODE (XEXP (x, 1)) != DImode
13411                     ? COSTS_N_INSNS (21)
13412                     : COSTS_N_INSNS (37));
13413           return true;
13414
13415         case PROCESSOR_PPC750:
13416         case PROCESSOR_PPC8540:
13417         case PROCESSOR_PPC7400:
13418           *total = COSTS_N_INSNS (19);
13419           return true;
13420
13421         case PROCESSOR_PPC7450:
13422           *total = COSTS_N_INSNS (23);
13423           return true;
13424
13425         default:
13426           abort ();
13427         }
13428
13429     case FFS:
13430       *total = COSTS_N_INSNS (4);
13431       return true;
13432
13433     case MEM:
13434       /* MEM should be slightly more expensive than (plus (reg) (const)) */
13435       *total = 5;
13436       return true;
13437
13438     default:
13439       return false;
13440     }
13441 }
13442
13443 /* A C expression returning the cost of moving data from a register of class
13444    CLASS1 to one of CLASS2.  */
13445
13446 int
13447 rs6000_register_move_cost (mode, from, to)
13448      enum machine_mode mode;
13449      enum reg_class from, to;
13450 {
13451   /*  Moves from/to GENERAL_REGS.  */
13452   if (reg_classes_intersect_p (to, GENERAL_REGS)
13453       || reg_classes_intersect_p (from, GENERAL_REGS))
13454     {
13455       if (! reg_classes_intersect_p (to, GENERAL_REGS))
13456         from = to;
13457
13458       if (from == FLOAT_REGS || from == ALTIVEC_REGS)
13459         return (rs6000_memory_move_cost (mode, from, 0)
13460                 + rs6000_memory_move_cost (mode, GENERAL_REGS, 0));
13461
13462 /* It's more expensive to move CR_REGS than CR0_REGS because of the shift...*/
13463       else if (from == CR_REGS)
13464         return 4;
13465
13466       else
13467 /* A move will cost one instruction per GPR moved.  */
13468         return 2 * HARD_REGNO_NREGS (0, mode);
13469     }
13470
13471 /* Moving between two similar registers is just one instruction.  */
13472   else if (reg_classes_intersect_p (to, from))
13473     return mode == TFmode ? 4 : 2;
13474
13475 /* Everything else has to go through GENERAL_REGS.  */
13476   else
13477     return (rs6000_register_move_cost (mode, GENERAL_REGS, to) 
13478             + rs6000_register_move_cost (mode, from, GENERAL_REGS));
13479 }
13480
13481 /* A C expressions returning the cost of moving data of MODE from a register to
13482    or from memory.  */
13483
13484 int
13485 rs6000_memory_move_cost (mode, class, in)
13486   enum machine_mode mode;
13487   enum reg_class class;
13488   int in ATTRIBUTE_UNUSED;
13489 {
13490   if (reg_classes_intersect_p (class, GENERAL_REGS))
13491     return 4 * HARD_REGNO_NREGS (0, mode);
13492   else if (reg_classes_intersect_p (class, FLOAT_REGS))
13493     return 4 * HARD_REGNO_NREGS (32, mode);
13494   else if (reg_classes_intersect_p (class, ALTIVEC_REGS))
13495     return 4 * HARD_REGNO_NREGS (FIRST_ALTIVEC_REGNO, mode);
13496   else
13497     return 4 + rs6000_register_move_cost (mode, class, GENERAL_REGS);
13498 }
13499
13500 #include "gt-rs6000.h"