OSDN Git Service

* config/pa/fptr.c: Fix comment typos.
[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 GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published
10    by the Free Software Foundation; either version 2, or (at your
11    option) any later version.
12
13    GCC is distributed in the hope that it will be useful, but WITHOUT
14    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
16    License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GCC; see the file COPYING.  If not, write to the
20    Free Software Foundation, 59 Temple Place - Suite 330, Boston,
21    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 #include "cfglayout.h"
54 #if TARGET_XCOFF
55 #include "xcoffout.h"  /* get declarations of xcoff_*_section_name */
56 #endif
57
58 #ifndef TARGET_NO_PROTOTYPE
59 #define TARGET_NO_PROTOTYPE 0
60 #endif
61
62 #define EASY_VECTOR_15(n, x, y) ((n) >= -16 && (n) <= 15 \
63                                  && easy_vector_same (x, y))
64
65 #define EASY_VECTOR_15_ADD_SELF(n, x, y) ((n) >= 0x10 && (n) <= 0x1e \
66                                           && !((n) & 1)              \
67                                           && easy_vector_same (x, y))
68
69 #define min(A,B)        ((A) < (B) ? (A) : (B))
70 #define max(A,B)        ((A) > (B) ? (A) : (B))
71
72 /* Target cpu type */
73
74 enum processor_type rs6000_cpu;
75 struct rs6000_cpu_select rs6000_select[3] =
76 {
77   /* switch             name,                   tune    arch */
78   { (const char *)0,    "--with-cpu=",          1,      1 },
79   { (const char *)0,    "-mcpu=",               1,      1 },
80   { (const char *)0,    "-mtune=",              1,      0 },
81 };
82
83 /* Size of long double */
84 const char *rs6000_long_double_size_string;
85 int rs6000_long_double_type_size;
86
87 /* Whether -mabi=altivec has appeared */
88 int rs6000_altivec_abi;
89
90 /* Whether VRSAVE instructions should be generated.  */
91 int rs6000_altivec_vrsave;
92
93 /* String from -mvrsave= option.  */
94 const char *rs6000_altivec_vrsave_string;
95
96 /* Nonzero if we want SPE ABI extensions.  */
97 int rs6000_spe_abi;
98
99 /* Whether isel instructions should be generated.  */
100 int rs6000_isel;
101
102 /* Whether SPE simd instructions should be generated.  */
103 int rs6000_spe;
104
105 /* Nonzero if floating point operations are done in the GPRs.  */
106 int rs6000_float_gprs = 0;
107
108 /* String from -mfloat-gprs=.  */
109 const char *rs6000_float_gprs_string;
110
111 /* String from -misel=.  */
112 const char *rs6000_isel_string;
113
114 /* String from -mspe=.  */
115 const char *rs6000_spe_string;
116
117 /* Set to nonzero once AIX common-mode calls have been defined.  */
118 static GTY(()) int common_mode_defined;
119
120 /* Save information from a "cmpxx" operation until the branch or scc is
121    emitted.  */
122 rtx rs6000_compare_op0, rs6000_compare_op1;
123 int rs6000_compare_fp_p;
124
125 /* Label number of label created for -mrelocatable, to call to so we can
126    get the address of the GOT section */
127 int rs6000_pic_labelno;
128
129 #ifdef USING_ELFOS_H
130 /* Which abi to adhere to */
131 const char *rs6000_abi_name;
132
133 /* Semantics of the small data area */
134 enum rs6000_sdata_type rs6000_sdata = SDATA_DATA;
135
136 /* Which small data model to use */
137 const char *rs6000_sdata_name = (char *)0;
138
139 /* Counter for labels which are to be placed in .fixup.  */
140 int fixuplabelno = 0;
141 #endif
142
143 /* Bit size of immediate TLS offsets and string from which it is decoded.  */
144 int rs6000_tls_size = 32;
145 const char *rs6000_tls_size_string;
146
147 /* ABI enumeration available for subtarget to use.  */
148 enum rs6000_abi rs6000_current_abi;
149
150 /* ABI string from -mabi= option.  */
151 const char *rs6000_abi_string;
152
153 /* Debug flags */
154 const char *rs6000_debug_name;
155 int rs6000_debug_stack;         /* debug stack applications */
156 int rs6000_debug_arg;           /* debug argument handling */
157
158 /* Opaque types.  */
159 static GTY(()) tree opaque_V2SI_type_node;
160 static GTY(()) tree opaque_V2SF_type_node;
161 static GTY(()) tree opaque_p_V2SI_type_node;
162
163 const char *rs6000_traceback_name;
164 static enum {
165   traceback_default = 0,
166   traceback_none,
167   traceback_part,
168   traceback_full
169 } rs6000_traceback;
170
171 /* Flag to say the TOC is initialized */
172 int toc_initialized;
173 char toc_label_name[10];
174
175 /* Alias set for saves and restores from the rs6000 stack.  */
176 static int rs6000_sr_alias_set;
177
178 /* Call distance, overridden by -mlongcall and #pragma longcall(1).
179    The only place that looks at this is rs6000_set_default_type_attributes;
180    everywhere else should rely on the presence or absence of a longcall
181    attribute on the function declaration.  */
182 int rs6000_default_long_calls;
183 const char *rs6000_longcall_switch;
184
185 /* Control alignment for fields within structures. */
186 /* String from -malign-XXXXX. */
187 const char *rs6000_alignment_string;
188 int rs6000_alignment_flags;
189
190 struct builtin_description
191 {
192   /* mask is not const because we're going to alter it below.  This
193      nonsense will go away when we rewrite the -march infrastructure
194      to give us more target flag bits.  */
195   unsigned int mask;
196   const enum insn_code icode;
197   const char *const name;
198   const enum rs6000_builtins code;
199 };
200
201 static bool rs6000_function_ok_for_sibcall PARAMS ((tree, tree));
202 static int num_insns_constant_wide PARAMS ((HOST_WIDE_INT));
203 static void validate_condition_mode 
204   PARAMS ((enum rtx_code, enum machine_mode));
205 static rtx rs6000_generate_compare PARAMS ((enum rtx_code));
206 static void rs6000_maybe_dead PARAMS ((rtx));
207 static void rs6000_emit_stack_tie PARAMS ((void));
208 static void rs6000_frame_related PARAMS ((rtx, rtx, HOST_WIDE_INT, rtx, rtx));
209 static rtx spe_synthesize_frame_save PARAMS ((rtx));
210 static bool spe_func_has_64bit_regs_p PARAMS ((void));
211 static void emit_frame_save PARAMS ((rtx, rtx, enum machine_mode,
212                                      unsigned int, int, int));
213 static rtx gen_frame_mem_offset PARAMS ((enum machine_mode, rtx, int));
214 static void rs6000_emit_allocate_stack PARAMS ((HOST_WIDE_INT, int));
215 static unsigned rs6000_hash_constant PARAMS ((rtx));
216 static unsigned toc_hash_function PARAMS ((const void *));
217 static int toc_hash_eq PARAMS ((const void *, const void *));
218 static int constant_pool_expr_1 PARAMS ((rtx, int *, int *));
219 static bool constant_pool_expr_p PARAMS ((rtx));
220 static bool toc_relative_expr_p PARAMS ((rtx));
221 static bool legitimate_small_data_p PARAMS ((enum machine_mode, rtx));
222 static bool legitimate_offset_address_p PARAMS ((enum machine_mode, rtx, int));
223 static bool legitimate_indexed_address_p PARAMS ((rtx, int));
224 static bool legitimate_indirect_address_p PARAMS ((rtx, int));
225 static bool legitimate_lo_sum_address_p PARAMS ((enum machine_mode, rtx, int));
226 static struct machine_function * rs6000_init_machine_status PARAMS ((void));
227 static bool rs6000_assemble_integer PARAMS ((rtx, unsigned int, int));
228 #ifdef HAVE_GAS_HIDDEN
229 static void rs6000_assemble_visibility PARAMS ((tree, int));
230 #endif
231 static int rs6000_ra_ever_killed PARAMS ((void));
232 static tree rs6000_handle_longcall_attribute PARAMS ((tree *, tree, tree, int, bool *));
233 extern const struct attribute_spec rs6000_attribute_table[];
234 static void rs6000_set_default_type_attributes PARAMS ((tree));
235 static void rs6000_output_function_prologue PARAMS ((FILE *, HOST_WIDE_INT));
236 static void rs6000_output_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
237 static void rs6000_output_mi_thunk PARAMS ((FILE *, tree, HOST_WIDE_INT,
238                                             HOST_WIDE_INT, tree));
239 static rtx rs6000_emit_set_long_const PARAMS ((rtx,
240   HOST_WIDE_INT, HOST_WIDE_INT));
241 static void rs6000_file_start PARAMS ((void));
242 #if TARGET_ELF
243 static unsigned int rs6000_elf_section_type_flags PARAMS ((tree, const char *,
244                                                            int));
245 static void rs6000_elf_asm_out_constructor PARAMS ((rtx, int));
246 static void rs6000_elf_asm_out_destructor PARAMS ((rtx, int));
247 static void rs6000_elf_select_section PARAMS ((tree, int,
248                                                unsigned HOST_WIDE_INT));
249 static void rs6000_elf_unique_section PARAMS ((tree, int));
250 static void rs6000_elf_select_rtx_section PARAMS ((enum machine_mode, rtx,
251                                                    unsigned HOST_WIDE_INT));
252 static void rs6000_elf_encode_section_info PARAMS ((tree, rtx, int))
253      ATTRIBUTE_UNUSED;
254 static bool rs6000_elf_in_small_data_p PARAMS ((tree));
255 #endif
256 #if TARGET_XCOFF
257 static void rs6000_xcoff_asm_globalize_label PARAMS ((FILE *, const char *));
258 static void rs6000_xcoff_asm_named_section PARAMS ((const char *, unsigned int));
259 static void rs6000_xcoff_select_section PARAMS ((tree, int,
260                                                  unsigned HOST_WIDE_INT));
261 static void rs6000_xcoff_unique_section PARAMS ((tree, int));
262 static void rs6000_xcoff_select_rtx_section PARAMS ((enum machine_mode, rtx,
263                                                      unsigned HOST_WIDE_INT));
264 static const char * rs6000_xcoff_strip_name_encoding PARAMS ((const char *));
265 static unsigned int rs6000_xcoff_section_type_flags PARAMS ((tree, const char *, int));
266 static void rs6000_xcoff_file_start PARAMS ((void));
267 static void rs6000_xcoff_file_end PARAMS ((void));
268 #endif
269 #if TARGET_MACHO
270 static bool rs6000_binds_local_p PARAMS ((tree));
271 #endif
272 static int rs6000_use_dfa_pipeline_interface PARAMS ((void));
273 static int rs6000_variable_issue PARAMS ((FILE *, int, rtx, int));
274 static bool rs6000_rtx_costs PARAMS ((rtx, int, int, int *));
275 static int rs6000_adjust_cost PARAMS ((rtx, rtx, rtx, int));
276 static int rs6000_adjust_priority PARAMS ((rtx, int));
277 static int rs6000_issue_rate PARAMS ((void));
278 static int rs6000_use_sched_lookahead PARAMS ((void));
279
280 static void rs6000_init_builtins PARAMS ((void));
281 static rtx rs6000_expand_unop_builtin PARAMS ((enum insn_code, tree, rtx));
282 static rtx rs6000_expand_binop_builtin PARAMS ((enum insn_code, tree, rtx));
283 static rtx rs6000_expand_ternop_builtin PARAMS ((enum insn_code, tree, rtx));
284 static rtx rs6000_expand_builtin PARAMS ((tree, rtx, rtx, enum machine_mode, int));
285 static void altivec_init_builtins PARAMS ((void));
286 static void rs6000_common_init_builtins PARAMS ((void));
287
288 static void enable_mask_for_builtins PARAMS ((struct builtin_description *,
289                                               int, enum rs6000_builtins,
290                                               enum rs6000_builtins));
291 static void spe_init_builtins PARAMS ((void));
292 static rtx spe_expand_builtin PARAMS ((tree, rtx, bool *));
293 static rtx spe_expand_predicate_builtin PARAMS ((enum insn_code, tree, rtx));
294 static rtx spe_expand_evsel_builtin PARAMS ((enum insn_code, tree, rtx));
295 static int rs6000_emit_int_cmove PARAMS ((rtx, rtx, rtx, rtx));
296
297 static rtx altivec_expand_builtin PARAMS ((tree, rtx, bool *));
298 static rtx altivec_expand_ld_builtin PARAMS ((tree, rtx, bool *));
299 static rtx altivec_expand_st_builtin PARAMS ((tree, rtx, bool *));
300 static rtx altivec_expand_dst_builtin PARAMS ((tree, rtx, bool *));
301 static rtx altivec_expand_abs_builtin PARAMS ((enum insn_code, tree, rtx));
302 static rtx altivec_expand_predicate_builtin PARAMS ((enum insn_code, const char *, tree, rtx));
303 static rtx altivec_expand_stv_builtin PARAMS ((enum insn_code, tree));
304 static void rs6000_parse_abi_options PARAMS ((void));
305 static void rs6000_parse_alignment_option PARAMS ((void));
306 static void rs6000_parse_tls_size_option PARAMS ((void));
307 static void rs6000_parse_yes_no_option (const char *, const char *, int *);
308 static int first_altivec_reg_to_save PARAMS ((void));
309 static unsigned int compute_vrsave_mask PARAMS ((void));
310 static void is_altivec_return_reg PARAMS ((rtx, void *));
311 static rtx generate_set_vrsave PARAMS ((rtx, rs6000_stack_t *, int));
312 int easy_vector_constant PARAMS ((rtx, enum machine_mode));
313 static int easy_vector_same PARAMS ((rtx, enum machine_mode));
314 static bool is_ev64_opaque_type PARAMS ((tree));
315 static rtx rs6000_dwarf_register_span PARAMS ((rtx));
316 static rtx rs6000_legitimize_tls_address PARAMS ((rtx, enum tls_model));
317 static rtx rs6000_tls_get_addr PARAMS ((void));
318 static rtx rs6000_got_sym PARAMS ((void));
319 static inline int rs6000_tls_symbol_ref_1 PARAMS ((rtx *, void *));
320 static const char *rs6000_get_some_local_dynamic_name PARAMS ((void));
321 static int rs6000_get_some_local_dynamic_name_1 PARAMS ((rtx *, void *));
322 static rtx rs6000_complex_function_value (enum machine_mode);
323 static rtx rs6000_spe_function_arg (CUMULATIVE_ARGS *, enum machine_mode, tree);
324
325 /* Hash table stuff for keeping track of TOC entries.  */
326
327 struct toc_hash_struct GTY(())
328 {
329   /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
330      ASM_OUTPUT_SPECIAL_POOL_ENTRY_P.  */
331   rtx key;
332   enum machine_mode key_mode;
333   int labelno;
334 };
335
336 static GTY ((param_is (struct toc_hash_struct))) htab_t toc_hash_table;
337 \f
338 /* Default register names.  */
339 char rs6000_reg_names[][8] =
340 {
341       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
342       "8",  "9", "10", "11", "12", "13", "14", "15",
343      "16", "17", "18", "19", "20", "21", "22", "23",
344      "24", "25", "26", "27", "28", "29", "30", "31",
345       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
346       "8",  "9", "10", "11", "12", "13", "14", "15",
347      "16", "17", "18", "19", "20", "21", "22", "23",
348      "24", "25", "26", "27", "28", "29", "30", "31",
349      "mq", "lr", "ctr","ap",
350       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
351       "xer",
352       /* AltiVec registers.  */
353       "0",  "1",  "2",  "3",  "4",  "5",  "6", "7",
354       "8",  "9",  "10", "11", "12", "13", "14", "15",
355       "16", "17", "18", "19", "20", "21", "22", "23",
356       "24", "25", "26", "27", "28", "29", "30", "31",
357       "vrsave", "vscr",
358       /* SPE registers.  */
359       "spe_acc", "spefscr"
360 };
361
362 #ifdef TARGET_REGNAMES
363 static const char alt_reg_names[][8] =
364 {
365    "%r0",   "%r1",  "%r2",  "%r3",  "%r4",  "%r5",  "%r6",  "%r7",
366    "%r8",   "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
367   "%r16",  "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
368   "%r24",  "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
369    "%f0",   "%f1",  "%f2",  "%f3",  "%f4",  "%f5",  "%f6",  "%f7",
370    "%f8",   "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
371   "%f16",  "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
372   "%f24",  "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
373     "mq",    "lr",  "ctr",   "ap",
374   "%cr0",  "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
375    "xer",
376   /* AltiVec registers.  */
377    "%v0",  "%v1",  "%v2",  "%v3",  "%v4",  "%v5",  "%v6", "%v7",
378    "%v8",  "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
379   "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
380   "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
381   "vrsave", "vscr",
382   /* SPE registers.  */
383   "spe_acc", "spefscr"
384 };
385 #endif
386 \f
387 #ifndef MASK_STRICT_ALIGN
388 #define MASK_STRICT_ALIGN 0
389 #endif
390 #ifndef TARGET_PROFILE_KERNEL
391 #define TARGET_PROFILE_KERNEL 0
392 #endif
393
394 /* The VRSAVE bitmask puts bit %v0 as the most significant bit.  */
395 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
396
397 /* Return 1 for a symbol ref for a thread-local storage symbol.  */
398 #define RS6000_SYMBOL_REF_TLS_P(RTX) \
399   (GET_CODE (RTX) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (RTX) != 0)
400 \f
401 /* Initialize the GCC target structure.  */
402 #undef TARGET_ATTRIBUTE_TABLE
403 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
404 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
405 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
406
407 #undef TARGET_ASM_ALIGNED_DI_OP
408 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
409
410 /* Default unaligned ops are only provided for ELF.  Find the ops needed
411    for non-ELF systems.  */
412 #ifndef OBJECT_FORMAT_ELF
413 #if TARGET_XCOFF
414 /* For XCOFF.  rs6000_assemble_integer will handle unaligned DIs on
415    64-bit targets.  */
416 #undef TARGET_ASM_UNALIGNED_HI_OP
417 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
418 #undef TARGET_ASM_UNALIGNED_SI_OP
419 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
420 #undef TARGET_ASM_UNALIGNED_DI_OP
421 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
422 #else
423 /* For Darwin.  */
424 #undef TARGET_ASM_UNALIGNED_HI_OP
425 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
426 #undef TARGET_ASM_UNALIGNED_SI_OP
427 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
428 #endif
429 #endif
430
431 /* This hook deals with fixups for relocatable code and DI-mode objects
432    in 64-bit code.  */
433 #undef TARGET_ASM_INTEGER
434 #define TARGET_ASM_INTEGER rs6000_assemble_integer
435
436 #ifdef HAVE_GAS_HIDDEN
437 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
438 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
439 #endif
440
441 #undef TARGET_HAVE_TLS
442 #define TARGET_HAVE_TLS HAVE_AS_TLS
443
444 #undef TARGET_CANNOT_FORCE_CONST_MEM
445 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_tls_referenced_p
446
447 #undef TARGET_ASM_FUNCTION_PROLOGUE
448 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
449 #undef TARGET_ASM_FUNCTION_EPILOGUE
450 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
451
452 #undef  TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE 
453 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE rs6000_use_dfa_pipeline_interface
454 #undef  TARGET_SCHED_VARIABLE_ISSUE
455 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
456
457 #undef TARGET_SCHED_ISSUE_RATE
458 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
459 #undef TARGET_SCHED_ADJUST_COST
460 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
461 #undef TARGET_SCHED_ADJUST_PRIORITY
462 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
463
464 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
465 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
466
467 #undef TARGET_INIT_BUILTINS
468 #define TARGET_INIT_BUILTINS rs6000_init_builtins
469
470 #undef TARGET_EXPAND_BUILTIN
471 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
472
473 #if TARGET_MACHO
474 #undef TARGET_BINDS_LOCAL_P
475 #define TARGET_BINDS_LOCAL_P rs6000_binds_local_p
476 #endif
477
478 #undef TARGET_ASM_OUTPUT_MI_THUNK
479 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
480
481 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
482 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
483
484 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
485 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
486
487 #undef TARGET_RTX_COSTS
488 #define TARGET_RTX_COSTS rs6000_rtx_costs
489 #undef TARGET_ADDRESS_COST
490 #define TARGET_ADDRESS_COST hook_int_rtx_0
491
492 #undef TARGET_VECTOR_OPAQUE_P
493 #define TARGET_VECTOR_OPAQUE_P is_ev64_opaque_type
494
495 #undef TARGET_DWARF_REGISTER_SPAN
496 #define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
497
498 struct gcc_target targetm = TARGET_INITIALIZER;
499 \f
500 /* Override command line options.  Mostly we process the processor
501    type and sometimes adjust other TARGET_ options.  */
502
503 void
504 rs6000_override_options (default_cpu)
505      const char *default_cpu;
506 {
507   size_t i, j;
508   struct rs6000_cpu_select *ptr;
509
510   /* Simplify the entries below by making a mask for any POWER
511      variant and any PowerPC variant.  */
512
513 #define POWER_MASKS (MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING)
514 #define POWERPC_MASKS (MASK_POWERPC | MASK_PPC_GPOPT \
515                        | MASK_PPC_GFXOPT | MASK_POWERPC64)
516 #define POWERPC_OPT_MASKS (MASK_PPC_GPOPT | MASK_PPC_GFXOPT)
517
518   static struct ptt
519     {
520       const char *const name;           /* Canonical processor name.  */
521       const enum processor_type processor; /* Processor type enum value.  */
522       const int target_enable;  /* Target flags to enable.  */
523       const int target_disable; /* Target flags to disable.  */
524     } const processor_target_table[]
525       = {{"common", PROCESSOR_COMMON, MASK_NEW_MNEMONICS,
526             POWER_MASKS | POWERPC_MASKS},
527          {"power", PROCESSOR_POWER,
528             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
529             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
530          {"power2", PROCESSOR_POWER,
531             MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING,
532             POWERPC_MASKS | MASK_NEW_MNEMONICS},
533          {"power3", PROCESSOR_PPC630,
534             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
535             POWER_MASKS},
536          {"power4", PROCESSOR_POWER4,
537             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
538             POWER_MASKS},
539          {"powerpc", PROCESSOR_POWERPC,
540             MASK_POWERPC | MASK_NEW_MNEMONICS,
541             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
542          {"powerpc64", PROCESSOR_POWERPC64,
543             MASK_POWERPC | MASK_POWERPC64 | MASK_NEW_MNEMONICS,
544             POWER_MASKS | POWERPC_OPT_MASKS},
545          {"rios", PROCESSOR_RIOS1,
546             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
547             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
548          {"rios1", PROCESSOR_RIOS1,
549             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
550             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
551          {"rsc", PROCESSOR_PPC601,
552             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
553             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
554          {"rsc1", PROCESSOR_PPC601,
555             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
556             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
557          {"rios2", PROCESSOR_RIOS2,
558             MASK_POWER | MASK_MULTIPLE | MASK_STRING | MASK_POWER2,
559             POWERPC_MASKS | MASK_NEW_MNEMONICS},
560          {"rs64a", PROCESSOR_RS64A,
561             MASK_POWERPC | MASK_NEW_MNEMONICS,
562             POWER_MASKS | POWERPC_OPT_MASKS},
563          {"401", PROCESSOR_PPC403,
564             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
565             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
566          {"403", PROCESSOR_PPC403,
567             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS | MASK_STRICT_ALIGN,
568             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
569          {"405", PROCESSOR_PPC405,
570             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
571             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
572          {"405fp", PROCESSOR_PPC405,
573             MASK_POWERPC | MASK_NEW_MNEMONICS,
574             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
575          {"440", PROCESSOR_PPC440,
576             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
577             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
578          {"440fp", PROCESSOR_PPC440,
579             MASK_POWERPC | MASK_NEW_MNEMONICS,
580             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
581          {"505", PROCESSOR_MPCCORE,
582             MASK_POWERPC | MASK_NEW_MNEMONICS,
583             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
584          {"601", PROCESSOR_PPC601,
585             MASK_POWER | MASK_POWERPC | MASK_NEW_MNEMONICS | MASK_MULTIPLE | MASK_STRING,
586             MASK_POWER2 | POWERPC_OPT_MASKS | MASK_POWERPC64},
587          {"602", PROCESSOR_PPC603,
588             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
589             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
590          {"603", PROCESSOR_PPC603,
591             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
592             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
593          {"603e", PROCESSOR_PPC603,
594             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
595             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
596          {"ec603e", PROCESSOR_PPC603,
597             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
598             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
599          {"604", PROCESSOR_PPC604,
600             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
601             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
602          {"604e", PROCESSOR_PPC604e,
603             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
604             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
605          {"620", PROCESSOR_PPC620,
606             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
607             POWER_MASKS},
608          {"630", PROCESSOR_PPC630,
609             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
610             POWER_MASKS},
611          {"740", PROCESSOR_PPC750,
612             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
613             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
614          {"750", PROCESSOR_PPC750,
615             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
616             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
617          {"7400", PROCESSOR_PPC7400,
618             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
619             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
620          {"7450", PROCESSOR_PPC7450,
621             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
622             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
623          {"8540", PROCESSOR_PPC8540,
624             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
625             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
626          {"801", PROCESSOR_MPCCORE,
627             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
628             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
629          {"821", PROCESSOR_MPCCORE,
630             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
631             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
632          {"823", PROCESSOR_MPCCORE,
633             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
634             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
635          {"860", PROCESSOR_MPCCORE,
636             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
637             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
638          {"970", PROCESSOR_POWER4,
639             MASK_POWERPC | POWERPC_OPT_MASKS | MASK_NEW_MNEMONICS,
640             POWER_MASKS}};
641
642   const size_t ptt_size = ARRAY_SIZE (processor_target_table);
643
644   /* Save current -mmultiple/-mno-multiple status.  */
645   int multiple = TARGET_MULTIPLE;
646   /* Save current -mstring/-mno-string status.  */
647   int string = TARGET_STRING;
648
649   /* Identify the processor type.  */
650   rs6000_select[0].string = default_cpu;
651   rs6000_cpu = TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT;
652
653   for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
654     {
655       ptr = &rs6000_select[i];
656       if (ptr->string != (char *)0 && ptr->string[0] != '\0')
657         {
658           for (j = 0; j < ptt_size; j++)
659             if (! strcmp (ptr->string, processor_target_table[j].name))
660               {
661                 if (ptr->set_tune_p)
662                   rs6000_cpu = processor_target_table[j].processor;
663
664                 if (ptr->set_arch_p)
665                   {
666                     target_flags |= processor_target_table[j].target_enable;
667                     target_flags &= ~processor_target_table[j].target_disable;
668                   }
669                 break;
670               }
671
672           if (j == ptt_size)
673             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
674         }
675     }
676
677   if (TARGET_E500)
678     rs6000_isel = 1;
679
680   /* If we are optimizing big endian systems for space, use the load/store
681      multiple and string instructions.  */
682   if (BYTES_BIG_ENDIAN && optimize_size)
683     target_flags |= MASK_MULTIPLE | MASK_STRING;
684
685   /* If -mmultiple or -mno-multiple was explicitly used, don't
686      override with the processor default */
687   if ((target_flags_explicit & MASK_MULTIPLE) != 0)
688     target_flags = (target_flags & ~MASK_MULTIPLE) | multiple;
689
690   /* If -mstring or -mno-string was explicitly used, don't override
691      with the processor default.  */
692   if ((target_flags_explicit & MASK_STRING) != 0)
693     target_flags = (target_flags & ~MASK_STRING) | string;
694
695   /* Don't allow -mmultiple or -mstring on little endian systems
696      unless the cpu is a 750, because the hardware doesn't support the
697      instructions used in little endian mode, and causes an alignment
698      trap.  The 750 does not cause an alignment trap (except when the
699      target is unaligned).  */
700
701   if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
702     {
703       if (TARGET_MULTIPLE)
704         {
705           target_flags &= ~MASK_MULTIPLE;
706           if ((target_flags_explicit & MASK_MULTIPLE) != 0)
707             warning ("-mmultiple is not supported on little endian systems");
708         }
709
710       if (TARGET_STRING)
711         {
712           target_flags &= ~MASK_STRING;
713           if ((target_flags_explicit & MASK_STRING) != 0)
714             warning ("-mstring is not supported on little endian systems");
715         }
716     }
717
718   /* Set debug flags */
719   if (rs6000_debug_name)
720     {
721       if (! strcmp (rs6000_debug_name, "all"))
722         rs6000_debug_stack = rs6000_debug_arg = 1;
723       else if (! strcmp (rs6000_debug_name, "stack"))
724         rs6000_debug_stack = 1;
725       else if (! strcmp (rs6000_debug_name, "arg"))
726         rs6000_debug_arg = 1;
727       else
728         error ("unknown -mdebug-%s switch", rs6000_debug_name);
729     }
730
731   if (rs6000_traceback_name)
732     {
733       if (! strncmp (rs6000_traceback_name, "full", 4))
734         rs6000_traceback = traceback_full;
735       else if (! strncmp (rs6000_traceback_name, "part", 4))
736         rs6000_traceback = traceback_part;
737       else if (! strncmp (rs6000_traceback_name, "no", 2))
738         rs6000_traceback = traceback_none;
739       else
740         error ("unknown -mtraceback arg `%s'; expecting `full', `partial' or `none'",
741                rs6000_traceback_name);
742     }
743
744   /* Set size of long double */
745   rs6000_long_double_type_size = 64;
746   if (rs6000_long_double_size_string)
747     {
748       char *tail;
749       int size = strtol (rs6000_long_double_size_string, &tail, 10);
750       if (*tail != '\0' || (size != 64 && size != 128))
751         error ("Unknown switch -mlong-double-%s",
752                rs6000_long_double_size_string);
753       else
754         rs6000_long_double_type_size = size;
755     }
756
757   /* Handle -mabi= options.  */
758   rs6000_parse_abi_options ();
759
760   /* Handle -malign-XXXXX option.  */
761   rs6000_parse_alignment_option ();
762
763   /* Handle generic -mFOO=YES/NO options.  */
764   rs6000_parse_yes_no_option ("vrsave", rs6000_altivec_vrsave_string,
765                               &rs6000_altivec_vrsave);
766   rs6000_parse_yes_no_option ("isel", rs6000_isel_string,
767                               &rs6000_isel);
768   rs6000_parse_yes_no_option ("spe", rs6000_spe_string, &rs6000_spe);
769   rs6000_parse_yes_no_option ("float-gprs", rs6000_float_gprs_string,
770                               &rs6000_float_gprs);
771
772   /* Handle -mtls-size option.  */
773   rs6000_parse_tls_size_option ();
774
775 #ifdef SUBTARGET_OVERRIDE_OPTIONS
776   SUBTARGET_OVERRIDE_OPTIONS;
777 #endif
778 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
779   SUBSUBTARGET_OVERRIDE_OPTIONS;
780 #endif
781
782   if (TARGET_E500)
783     {
784       /* The e500 does not have string instructions, and we set
785          MASK_STRING above when optimizing for size.  */
786       if ((target_flags & MASK_STRING) != 0)
787         target_flags = target_flags & ~MASK_STRING;
788
789       /* No SPE means 64-bit long doubles, even if an E500.  */
790       if (rs6000_spe_string != 0
791           && !strcmp (rs6000_spe_string, "no"))
792         rs6000_long_double_type_size = 64;
793     }
794   else if (rs6000_select[1].string != NULL)
795     {
796       /* For the powerpc-eabispe configuration, we set all these by
797          default, so let's unset them if we manually set another
798          CPU that is not the E500.  */
799       if (rs6000_abi_string == 0)
800         rs6000_spe_abi = 0;
801       if (rs6000_spe_string == 0)
802         rs6000_spe = 0;
803       if (rs6000_float_gprs_string == 0)
804         rs6000_float_gprs = 0;
805       if (rs6000_isel_string == 0)
806         rs6000_isel = 0;
807       if (rs6000_long_double_size_string == 0)
808         rs6000_long_double_type_size = 64;
809     }
810
811   /* Handle -m(no-)longcall option.  This is a bit of a cheap hack,
812      using TARGET_OPTIONS to handle a toggle switch, but we're out of
813      bits in target_flags so TARGET_SWITCHES cannot be used.
814      Assumption here is that rs6000_longcall_switch points into the
815      text of the complete option, rather than being a copy, so we can
816      scan back for the presence or absence of the no- modifier.  */
817   if (rs6000_longcall_switch)
818     {
819       const char *base = rs6000_longcall_switch;
820       while (base[-1] != 'm') base--;
821
822       if (*rs6000_longcall_switch != '\0')
823         error ("invalid option `%s'", base);
824       rs6000_default_long_calls = (base[0] != 'n');
825     }
826
827 #ifdef TARGET_REGNAMES
828   /* If the user desires alternate register names, copy in the
829      alternate names now.  */
830   if (TARGET_REGNAMES)
831     memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
832 #endif
833
834   /* Set TARGET_AIX_STRUCT_RET last, after the ABI is determined.
835      If -maix-struct-return or -msvr4-struct-return was explicitly
836      used, don't override with the ABI default.  */
837   if ((target_flags_explicit & MASK_AIX_STRUCT_RET) == 0)
838     {
839       if (DEFAULT_ABI == ABI_V4 && !DRAFT_V4_STRUCT_RET)
840         target_flags = (target_flags & ~MASK_AIX_STRUCT_RET);
841       else
842         target_flags |= MASK_AIX_STRUCT_RET;
843     }
844
845   if (TARGET_LONG_DOUBLE_128
846       && (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_DARWIN))
847     real_format_for_mode[TFmode - QFmode] = &ibm_extended_format;
848
849   /* Allocate an alias set for register saves & restores from stack.  */
850   rs6000_sr_alias_set = new_alias_set ();
851
852   if (TARGET_TOC) 
853     ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
854
855   /* We can only guarantee the availability of DI pseudo-ops when
856      assembling for 64-bit targets.  */
857   if (!TARGET_64BIT)
858     {
859       targetm.asm_out.aligned_op.di = NULL;
860       targetm.asm_out.unaligned_op.di = NULL;
861     }
862
863   /* Set maximum branch target alignment at two instructions, eight bytes.  */
864   align_jumps_max_skip = 8;
865   align_loops_max_skip = 8;
866
867   /* Arrange to save and restore machine status around nested functions.  */
868   init_machine_status = rs6000_init_machine_status;
869 }
870
871 /* Handle generic options of the form -mfoo=yes/no.
872    NAME is the option name.
873    VALUE is the option value.
874    FLAG is the pointer to the flag where to store a 1 or 0, depending on
875    whether the option value is 'yes' or 'no' respectively.  */
876 static void
877 rs6000_parse_yes_no_option (const char *name, const char *value, int *flag)
878 {
879   if (value == 0)
880     return;
881   else if (!strcmp (value, "yes"))
882     *flag = 1;
883   else if (!strcmp (value, "no"))
884     *flag = 0;
885   else
886     error ("unknown -m%s= option specified: '%s'", name, value);
887 }
888
889 /* Handle -mabi= options.  */
890 static void
891 rs6000_parse_abi_options ()
892 {
893   if (rs6000_abi_string == 0)
894     return;
895   else if (! strcmp (rs6000_abi_string, "altivec"))
896     rs6000_altivec_abi = 1;
897   else if (! strcmp (rs6000_abi_string, "no-altivec"))
898     rs6000_altivec_abi = 0;
899   else if (! strcmp (rs6000_abi_string, "spe"))
900     {
901       rs6000_spe_abi = 1;
902       if (!TARGET_SPE_ABI)
903         error ("not configured for ABI: '%s'", rs6000_abi_string);
904     }
905   
906   else if (! strcmp (rs6000_abi_string, "no-spe"))
907     rs6000_spe_abi = 0;
908   else
909     error ("unknown ABI specified: '%s'", rs6000_abi_string);
910 }
911
912 /* Handle -malign-XXXXXX options.  */
913 static void
914 rs6000_parse_alignment_option ()
915 {
916   if (rs6000_alignment_string == 0
917       || ! strcmp (rs6000_alignment_string, "power"))
918     rs6000_alignment_flags = MASK_ALIGN_POWER;
919   else if (! strcmp (rs6000_alignment_string, "natural"))
920     rs6000_alignment_flags = MASK_ALIGN_NATURAL;
921   else
922     error ("unknown -malign-XXXXX option specified: '%s'",
923            rs6000_alignment_string);
924 }
925
926 /* Validate and record the size specified with the -mtls-size option.  */
927
928 static void
929 rs6000_parse_tls_size_option ()
930 {
931   if (rs6000_tls_size_string == 0)
932     return;
933   else if (strcmp (rs6000_tls_size_string, "16") == 0)
934     rs6000_tls_size = 16;
935   else if (strcmp (rs6000_tls_size_string, "32") == 0)
936     rs6000_tls_size = 32;
937   else if (strcmp (rs6000_tls_size_string, "64") == 0)
938     rs6000_tls_size = 64;
939   else
940     error ("bad value `%s' for -mtls-size switch", rs6000_tls_size_string);
941 }
942
943 void
944 optimization_options (level, size)
945      int level ATTRIBUTE_UNUSED;
946      int size ATTRIBUTE_UNUSED;
947 {
948 }
949 \f
950 /* Do anything needed at the start of the asm file.  */
951
952 static void
953 rs6000_file_start ()
954 {
955   size_t i;
956   char buffer[80];
957   const char *start = buffer;
958   struct rs6000_cpu_select *ptr;
959   const char *default_cpu = TARGET_CPU_DEFAULT;
960   FILE *file = asm_out_file;
961
962   default_file_start ();
963
964 #ifdef TARGET_BI_ARCH
965   if ((TARGET_DEFAULT ^ target_flags) & MASK_64BIT)
966     default_cpu = 0;
967 #endif
968
969   if (flag_verbose_asm)
970     {
971       sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
972       rs6000_select[0].string = default_cpu;
973
974       for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
975         {
976           ptr = &rs6000_select[i];
977           if (ptr->string != (char *)0 && ptr->string[0] != '\0')
978             {
979               fprintf (file, "%s %s%s", start, ptr->name, ptr->string);
980               start = "";
981             }
982         }
983
984 #ifdef USING_ELFOS_H
985       switch (rs6000_sdata)
986         {
987         case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
988         case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
989         case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
990         case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
991         }
992
993       if (rs6000_sdata && g_switch_value)
994         {
995           fprintf (file, "%s -G " HOST_WIDE_INT_PRINT_UNSIGNED, start,
996                    g_switch_value);
997           start = "";
998         }
999 #endif
1000
1001       if (*start == '\0')
1002         putc ('\n', file);
1003     }
1004 }
1005 \f
1006 /* Return nonzero if this function is known to have a null epilogue.  */
1007
1008 int
1009 direct_return ()
1010 {
1011   if (reload_completed)
1012     {
1013       rs6000_stack_t *info = rs6000_stack_info ();
1014
1015       if (info->first_gp_reg_save == 32
1016           && info->first_fp_reg_save == 64
1017           && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
1018           && ! info->lr_save_p
1019           && ! info->cr_save_p
1020           && info->vrsave_mask == 0
1021           && ! info->push_p)
1022         return 1;
1023     }
1024
1025   return 0;
1026 }
1027
1028 /* Returns 1 always.  */
1029
1030 int
1031 any_operand (op, mode)
1032      rtx op ATTRIBUTE_UNUSED;
1033      enum machine_mode mode ATTRIBUTE_UNUSED;
1034 {
1035   return 1;
1036 }
1037
1038 /* Returns 1 if op is the count register.  */
1039 int
1040 count_register_operand (op, mode)
1041      rtx op;
1042      enum machine_mode mode ATTRIBUTE_UNUSED;
1043 {
1044   if (GET_CODE (op) != REG)
1045     return 0;
1046
1047   if (REGNO (op) == COUNT_REGISTER_REGNUM)
1048     return 1;
1049
1050   if (REGNO (op) > FIRST_PSEUDO_REGISTER)
1051     return 1;
1052
1053   return 0;
1054 }
1055
1056 /* Returns 1 if op is an altivec register.  */
1057 int
1058 altivec_register_operand (op, mode)
1059      rtx op;
1060      enum machine_mode mode ATTRIBUTE_UNUSED;
1061 {
1062   
1063   return (register_operand (op, mode)
1064           && (GET_CODE (op) != REG
1065               || REGNO (op) > FIRST_PSEUDO_REGISTER
1066               || ALTIVEC_REGNO_P (REGNO (op))));
1067 }
1068
1069 int
1070 xer_operand (op, mode)
1071      rtx op;
1072      enum machine_mode mode ATTRIBUTE_UNUSED;
1073 {
1074   if (GET_CODE (op) != REG)
1075     return 0;
1076
1077   if (XER_REGNO_P (REGNO (op)))
1078     return 1;
1079
1080   return 0;
1081 }
1082
1083 /* Return 1 if OP is a signed 8-bit constant.  Int multiplication
1084    by such constants completes more quickly.  */
1085
1086 int
1087 s8bit_cint_operand (op, mode)
1088      rtx op;
1089      enum machine_mode mode ATTRIBUTE_UNUSED;
1090 {
1091   return ( GET_CODE (op) == CONST_INT
1092           && (INTVAL (op) >= -128 && INTVAL (op) <= 127));
1093 }
1094
1095 /* Return 1 if OP is a constant that can fit in a D field.  */
1096
1097 int
1098 short_cint_operand (op, mode)
1099      rtx op;
1100      enum machine_mode mode ATTRIBUTE_UNUSED;
1101 {
1102   return (GET_CODE (op) == CONST_INT
1103           && CONST_OK_FOR_LETTER_P (INTVAL (op), 'I'));
1104 }
1105
1106 /* Similar for an unsigned D field.  */
1107
1108 int
1109 u_short_cint_operand (op, mode)
1110      rtx op;
1111      enum machine_mode mode ATTRIBUTE_UNUSED;
1112 {
1113   return (GET_CODE (op) == CONST_INT
1114           && CONST_OK_FOR_LETTER_P (INTVAL (op) & GET_MODE_MASK (mode), 'K'));
1115 }
1116
1117 /* Return 1 if OP is a CONST_INT that cannot fit in a signed D field.  */
1118
1119 int
1120 non_short_cint_operand (op, mode)
1121      rtx op;
1122      enum machine_mode mode ATTRIBUTE_UNUSED;
1123 {
1124   return (GET_CODE (op) == CONST_INT
1125           && (unsigned HOST_WIDE_INT) (INTVAL (op) + 0x8000) >= 0x10000);
1126 }
1127
1128 /* Returns 1 if OP is a CONST_INT that is a positive value
1129    and an exact power of 2.  */
1130
1131 int
1132 exact_log2_cint_operand (op, mode)
1133      rtx op;
1134      enum machine_mode mode ATTRIBUTE_UNUSED;
1135 {
1136   return (GET_CODE (op) == CONST_INT
1137           && INTVAL (op) > 0
1138           && exact_log2 (INTVAL (op)) >= 0);
1139 }
1140
1141 /* Returns 1 if OP is a register that is not special (i.e., not MQ,
1142    ctr, or lr).  */
1143
1144 int
1145 gpc_reg_operand (op, mode)
1146      rtx op;
1147      enum machine_mode mode;
1148 {
1149   return (register_operand (op, mode)
1150           && (GET_CODE (op) != REG
1151               || (REGNO (op) >= ARG_POINTER_REGNUM 
1152                   && !XER_REGNO_P (REGNO (op)))
1153               || REGNO (op) < MQ_REGNO));
1154 }
1155
1156 /* Returns 1 if OP is either a pseudo-register or a register denoting a
1157    CR field.  */
1158
1159 int
1160 cc_reg_operand (op, mode)
1161      rtx op;
1162      enum machine_mode mode;
1163 {
1164   return (register_operand (op, mode)
1165           && (GET_CODE (op) != REG
1166               || REGNO (op) >= FIRST_PSEUDO_REGISTER
1167               || CR_REGNO_P (REGNO (op))));
1168 }
1169
1170 /* Returns 1 if OP is either a pseudo-register or a register denoting a
1171    CR field that isn't CR0.  */
1172
1173 int
1174 cc_reg_not_cr0_operand (op, mode)
1175      rtx op;
1176      enum machine_mode mode;
1177 {
1178   return (register_operand (op, mode)
1179           && (GET_CODE (op) != REG
1180               || REGNO (op) >= FIRST_PSEUDO_REGISTER
1181               || CR_REGNO_NOT_CR0_P (REGNO (op))));
1182 }
1183
1184 /* Returns 1 if OP is either a constant integer valid for a D-field or
1185    a non-special register.  If a register, it must be in the proper
1186    mode unless MODE is VOIDmode.  */
1187
1188 int
1189 reg_or_short_operand (op, mode)
1190       rtx op;
1191       enum machine_mode mode;
1192 {
1193   return short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
1194 }
1195
1196 /* Similar, except check if the negation of the constant would be
1197    valid for a D-field.  */
1198
1199 int
1200 reg_or_neg_short_operand (op, mode)
1201       rtx op;
1202       enum machine_mode mode;
1203 {
1204   if (GET_CODE (op) == CONST_INT)
1205     return CONST_OK_FOR_LETTER_P (INTVAL (op), 'P');
1206
1207   return gpc_reg_operand (op, mode);
1208 }
1209
1210 /* Returns 1 if OP is either a constant integer valid for a DS-field or
1211    a non-special register.  If a register, it must be in the proper
1212    mode unless MODE is VOIDmode.  */
1213
1214 int
1215 reg_or_aligned_short_operand (op, mode)
1216       rtx op;
1217       enum machine_mode mode;
1218 {
1219   if (gpc_reg_operand (op, mode))
1220     return 1;
1221   else if (short_cint_operand (op, mode) && !(INTVAL (op) & 3))
1222     return 1;
1223
1224   return 0;
1225 }
1226
1227
1228 /* Return 1 if the operand is either a register or an integer whose
1229    high-order 16 bits are zero.  */
1230
1231 int
1232 reg_or_u_short_operand (op, mode)
1233      rtx op;
1234      enum machine_mode mode;
1235 {
1236   return u_short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
1237 }
1238
1239 /* Return 1 is the operand is either a non-special register or ANY
1240    constant integer.  */
1241
1242 int
1243 reg_or_cint_operand (op, mode)
1244     rtx op;
1245     enum machine_mode mode;
1246 {
1247   return (GET_CODE (op) == CONST_INT || gpc_reg_operand (op, mode));
1248 }
1249
1250 /* Return 1 is the operand is either a non-special register or ANY
1251    32-bit signed constant integer.  */
1252
1253 int
1254 reg_or_arith_cint_operand (op, mode)
1255     rtx op;
1256     enum machine_mode mode;
1257 {
1258   return (gpc_reg_operand (op, mode)
1259           || (GET_CODE (op) == CONST_INT
1260 #if HOST_BITS_PER_WIDE_INT != 32
1261               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80000000)
1262                   < (unsigned HOST_WIDE_INT) 0x100000000ll)
1263 #endif
1264               ));
1265 }
1266
1267 /* Return 1 is the operand is either a non-special register or a 32-bit
1268    signed constant integer valid for 64-bit addition.  */
1269
1270 int
1271 reg_or_add_cint64_operand (op, mode)
1272     rtx op;
1273     enum machine_mode mode;
1274 {
1275   return (gpc_reg_operand (op, mode)
1276           || (GET_CODE (op) == CONST_INT
1277 #if HOST_BITS_PER_WIDE_INT == 32
1278               && INTVAL (op) < 0x7fff8000
1279 #else
1280               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80008000)
1281                   < 0x100000000ll)
1282 #endif
1283               ));
1284 }
1285
1286 /* Return 1 is the operand is either a non-special register or a 32-bit
1287    signed constant integer valid for 64-bit subtraction.  */
1288
1289 int
1290 reg_or_sub_cint64_operand (op, mode)
1291     rtx op;
1292     enum machine_mode mode;
1293 {
1294   return (gpc_reg_operand (op, mode)
1295           || (GET_CODE (op) == CONST_INT
1296 #if HOST_BITS_PER_WIDE_INT == 32
1297               && (- INTVAL (op)) < 0x7fff8000
1298 #else
1299               && ((unsigned HOST_WIDE_INT) ((- INTVAL (op)) + 0x80008000)
1300                   < 0x100000000ll)
1301 #endif
1302               ));
1303 }
1304
1305 /* Return 1 is the operand is either a non-special register or ANY
1306    32-bit unsigned constant integer.  */
1307
1308 int
1309 reg_or_logical_cint_operand (op, mode)
1310     rtx op;
1311     enum machine_mode mode;
1312 {
1313   if (GET_CODE (op) == CONST_INT)
1314     {
1315       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
1316         {
1317           if (GET_MODE_BITSIZE (mode) <= 32)
1318             abort ();
1319
1320           if (INTVAL (op) < 0)
1321             return 0;
1322         }
1323
1324       return ((INTVAL (op) & GET_MODE_MASK (mode)
1325                & (~ (unsigned HOST_WIDE_INT) 0xffffffff)) == 0);
1326     }
1327   else if (GET_CODE (op) == CONST_DOUBLE)
1328     {
1329       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
1330           || mode != DImode)
1331         abort ();
1332
1333       return CONST_DOUBLE_HIGH (op) == 0;
1334     }
1335   else 
1336     return gpc_reg_operand (op, mode);
1337 }
1338
1339 /* Return 1 if the operand is an operand that can be loaded via the GOT.  */
1340
1341 int
1342 got_operand (op, mode)
1343      rtx op;
1344      enum machine_mode mode ATTRIBUTE_UNUSED;
1345 {
1346   return (GET_CODE (op) == SYMBOL_REF
1347           || GET_CODE (op) == CONST
1348           || GET_CODE (op) == LABEL_REF);
1349 }
1350
1351 /* Return 1 if the operand is a simple references that can be loaded via
1352    the GOT (labels involving addition aren't allowed).  */
1353
1354 int
1355 got_no_const_operand (op, mode)
1356      rtx op;
1357      enum machine_mode mode ATTRIBUTE_UNUSED;
1358 {
1359   return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF);
1360 }
1361
1362 /* Return the number of instructions it takes to form a constant in an
1363    integer register.  */
1364
1365 static int
1366 num_insns_constant_wide (value)
1367      HOST_WIDE_INT value;
1368 {
1369   /* signed constant loadable with {cal|addi} */
1370   if (CONST_OK_FOR_LETTER_P (value, 'I'))
1371     return 1;
1372
1373   /* constant loadable with {cau|addis} */
1374   else if (CONST_OK_FOR_LETTER_P (value, 'L'))
1375     return 1;
1376
1377 #if HOST_BITS_PER_WIDE_INT == 64
1378   else if (TARGET_POWERPC64)
1379     {
1380       HOST_WIDE_INT low  = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
1381       HOST_WIDE_INT high = value >> 31;
1382
1383       if (high == 0 || high == -1)
1384         return 2;
1385
1386       high >>= 1;
1387
1388       if (low == 0)
1389         return num_insns_constant_wide (high) + 1;
1390       else
1391         return (num_insns_constant_wide (high)
1392                 + num_insns_constant_wide (low) + 1);
1393     }
1394 #endif
1395
1396   else
1397     return 2;
1398 }
1399
1400 int
1401 num_insns_constant (op, mode)
1402      rtx op;
1403      enum machine_mode mode;
1404 {
1405   if (GET_CODE (op) == CONST_INT)
1406     {
1407 #if HOST_BITS_PER_WIDE_INT == 64
1408       if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
1409           && mask64_operand (op, mode))
1410             return 2;
1411       else
1412 #endif
1413         return num_insns_constant_wide (INTVAL (op));
1414     }
1415
1416   else if (GET_CODE (op) == CONST_DOUBLE && mode == SFmode)
1417     {
1418       long l;
1419       REAL_VALUE_TYPE rv;
1420
1421       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1422       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1423       return num_insns_constant_wide ((HOST_WIDE_INT) l);
1424     }
1425
1426   else if (GET_CODE (op) == CONST_DOUBLE)
1427     {
1428       HOST_WIDE_INT low;
1429       HOST_WIDE_INT high;
1430       long l[2];
1431       REAL_VALUE_TYPE rv;
1432       int endian = (WORDS_BIG_ENDIAN == 0);
1433
1434       if (mode == VOIDmode || mode == DImode)
1435         {
1436           high = CONST_DOUBLE_HIGH (op);
1437           low  = CONST_DOUBLE_LOW (op);
1438         }
1439       else
1440         {
1441           REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1442           REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
1443           high = l[endian];
1444           low  = l[1 - endian];
1445         }
1446
1447       if (TARGET_32BIT)
1448         return (num_insns_constant_wide (low)
1449                 + num_insns_constant_wide (high));
1450
1451       else
1452         {
1453           if (high == 0 && low >= 0)
1454             return num_insns_constant_wide (low);
1455
1456           else if (high == -1 && low < 0)
1457             return num_insns_constant_wide (low);
1458
1459           else if (mask64_operand (op, mode))
1460             return 2;
1461
1462           else if (low == 0)
1463             return num_insns_constant_wide (high) + 1;
1464
1465           else
1466             return (num_insns_constant_wide (high)
1467                     + num_insns_constant_wide (low) + 1);
1468         }
1469     }
1470
1471   else
1472     abort ();
1473 }
1474
1475 /* Return 1 if the operand is a CONST_DOUBLE and it can be put into a
1476    register with one instruction per word.  We only do this if we can
1477    safely read CONST_DOUBLE_{LOW,HIGH}.  */
1478
1479 int
1480 easy_fp_constant (op, mode)
1481      rtx op;
1482      enum machine_mode mode;
1483 {
1484   if (GET_CODE (op) != CONST_DOUBLE
1485       || GET_MODE (op) != mode
1486       || (GET_MODE_CLASS (mode) != MODE_FLOAT && mode != DImode))
1487     return 0;
1488
1489   /* Consider all constants with -msoft-float to be easy.  */
1490   if ((TARGET_SOFT_FLOAT || !TARGET_FPRS)
1491       && mode != DImode)
1492     return 1;
1493
1494   /* If we are using V.4 style PIC, consider all constants to be hard.  */
1495   if (flag_pic && DEFAULT_ABI == ABI_V4)
1496     return 0;
1497
1498 #ifdef TARGET_RELOCATABLE
1499   /* Similarly if we are using -mrelocatable, consider all constants
1500      to be hard.  */
1501   if (TARGET_RELOCATABLE)
1502     return 0;
1503 #endif
1504
1505   if (mode == TFmode)
1506     {
1507       long k[4];
1508       REAL_VALUE_TYPE rv;
1509
1510       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1511       REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
1512
1513       return (num_insns_constant_wide ((HOST_WIDE_INT) k[0]) == 1
1514               && num_insns_constant_wide ((HOST_WIDE_INT) k[1]) == 1
1515               && num_insns_constant_wide ((HOST_WIDE_INT) k[2]) == 1
1516               && num_insns_constant_wide ((HOST_WIDE_INT) k[3]) == 1);
1517     }
1518
1519   else if (mode == DFmode)
1520     {
1521       long k[2];
1522       REAL_VALUE_TYPE rv;
1523
1524       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1525       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
1526
1527       return (num_insns_constant_wide ((HOST_WIDE_INT) k[0]) == 1
1528               && num_insns_constant_wide ((HOST_WIDE_INT) k[1]) == 1);
1529     }
1530
1531   else if (mode == SFmode)
1532     {
1533       long l;
1534       REAL_VALUE_TYPE rv;
1535
1536       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1537       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1538
1539       return num_insns_constant_wide (l) == 1;
1540     }
1541
1542   else if (mode == DImode)
1543     return ((TARGET_POWERPC64
1544              && GET_CODE (op) == CONST_DOUBLE && CONST_DOUBLE_LOW (op) == 0)
1545             || (num_insns_constant (op, DImode) <= 2));
1546
1547   else if (mode == SImode)
1548     return 1;
1549   else
1550     abort ();
1551 }
1552
1553 /* Return nonzero if all elements of a vector have the same value.  */
1554
1555 static int
1556 easy_vector_same (op, mode)
1557      rtx op;
1558      enum machine_mode mode ATTRIBUTE_UNUSED;
1559 {
1560   int units, i, cst;
1561
1562   units = CONST_VECTOR_NUNITS (op);
1563
1564   cst = INTVAL (CONST_VECTOR_ELT (op, 0));
1565   for (i = 1; i < units; ++i)
1566     if (INTVAL (CONST_VECTOR_ELT (op, i)) != cst)
1567       break;
1568   if (i == units)
1569     return 1;
1570   return 0;
1571 }
1572
1573 /* Return 1 if the operand is a CONST_INT and can be put into a
1574    register without using memory.  */
1575
1576 int
1577 easy_vector_constant (op, mode)
1578      rtx op;
1579      enum machine_mode mode;
1580 {
1581   int cst, cst2;
1582
1583   if (GET_CODE (op) != CONST_VECTOR
1584       || (!TARGET_ALTIVEC
1585           && !TARGET_SPE))
1586     return 0;
1587
1588   if (zero_constant (op, mode)
1589       && ((TARGET_ALTIVEC && ALTIVEC_VECTOR_MODE (mode))
1590           || (TARGET_SPE && SPE_VECTOR_MODE (mode))))
1591     return 1;
1592
1593   if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
1594     return 0;
1595
1596   if (TARGET_SPE && mode == V1DImode)
1597     return 0;
1598
1599   cst  = INTVAL (CONST_VECTOR_ELT (op, 0));
1600   cst2 = INTVAL (CONST_VECTOR_ELT (op, 1));
1601
1602   /* Limit SPE vectors to 15 bits signed.  These we can generate with:
1603        li r0, CONSTANT1
1604        evmergelo r0, r0, r0
1605        li r0, CONSTANT2
1606
1607      I don't know how efficient it would be to allow bigger constants,
1608      considering we'll have an extra 'ori' for every 'li'.  I doubt 5
1609      instructions is better than a 64-bit memory load, but I don't
1610      have the e500 timing specs.  */
1611   if (TARGET_SPE && mode == V2SImode
1612       && cst  >= -0x7fff && cst <= 0x7fff
1613       && cst2 >= -0x7fff && cst2 <= 0x7fff)
1614     return 1;
1615
1616   if (TARGET_ALTIVEC && EASY_VECTOR_15 (cst, op, mode))
1617     return 1;
1618
1619   if (TARGET_ALTIVEC && EASY_VECTOR_15_ADD_SELF (cst, op, mode))
1620     return 1;
1621
1622   return 0;
1623 }
1624
1625 /* Same as easy_vector_constant but only for EASY_VECTOR_15_ADD_SELF.  */
1626
1627 int
1628 easy_vector_constant_add_self (op, mode)
1629      rtx op;
1630      enum machine_mode mode;
1631 {
1632   int cst;
1633
1634   if (!easy_vector_constant (op, mode))
1635     return 0;
1636
1637   cst = INTVAL (CONST_VECTOR_ELT (op, 0));
1638
1639   return TARGET_ALTIVEC && EASY_VECTOR_15_ADD_SELF (cst, op, mode);
1640 }
1641
1642 const char *
1643 output_vec_const_move (operands)
1644      rtx *operands;
1645 {
1646   int cst, cst2;
1647   enum machine_mode mode;
1648   rtx dest, vec;
1649
1650   dest = operands[0];
1651   vec = operands[1];
1652
1653   cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
1654   cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
1655   mode = GET_MODE (dest);
1656
1657   if (TARGET_ALTIVEC)
1658     {
1659       if (zero_constant (vec, mode))
1660         return "vxor %0,%0,%0";
1661       else if (EASY_VECTOR_15 (cst, vec, mode))
1662         {
1663           operands[1] = GEN_INT (cst);
1664           switch (mode)
1665             {
1666             case V4SImode:
1667               return "vspltisw %0,%1";
1668             case V8HImode:
1669               return "vspltish %0,%1";
1670             case V16QImode:
1671               return "vspltisb %0,%1";
1672             default:
1673               abort ();
1674             }
1675         }
1676       else if (EASY_VECTOR_15_ADD_SELF (cst, vec, mode))
1677         return "#";
1678       else
1679         abort ();
1680     }
1681
1682   if (TARGET_SPE)
1683     {
1684       /* Vector constant 0 is handled as a splitter of V2SI, and in the
1685          pattern of V1DI, V4HI, and V2SF.
1686
1687          FIXME: We should probably return # and add post reload
1688          splitters for these, but this way is so easy ;-).
1689       */
1690       operands[1] = GEN_INT (cst);
1691       operands[2] = GEN_INT (cst2);
1692       if (cst == cst2)
1693         return "li %0,%1\n\tevmergelo %0,%0,%0";
1694       else
1695         return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
1696     }
1697
1698   abort ();
1699 }
1700
1701 /* Return 1 if the operand is the constant 0.  This works for scalars
1702    as well as vectors.  */
1703 int
1704 zero_constant (op, mode)
1705      rtx op;
1706      enum machine_mode mode;
1707 {
1708   return op == CONST0_RTX (mode);
1709 }
1710
1711 /* Return 1 if the operand is 0.0.  */
1712 int
1713 zero_fp_constant (op, mode)
1714      rtx op;
1715      enum machine_mode mode;
1716 {
1717   return GET_MODE_CLASS (mode) == MODE_FLOAT && op == CONST0_RTX (mode);
1718 }
1719
1720 /* Return 1 if the operand is in volatile memory.  Note that during
1721    the RTL generation phase, memory_operand does not return TRUE for
1722    volatile memory references.  So this function allows us to
1723    recognize volatile references where its safe.  */
1724
1725 int
1726 volatile_mem_operand (op, mode)
1727      rtx op;
1728      enum machine_mode mode;
1729 {
1730   if (GET_CODE (op) != MEM)
1731     return 0;
1732
1733   if (!MEM_VOLATILE_P (op))
1734     return 0;
1735
1736   if (mode != GET_MODE (op))
1737     return 0;
1738
1739   if (reload_completed)
1740     return memory_operand (op, mode);
1741
1742   if (reload_in_progress)
1743     return strict_memory_address_p (mode, XEXP (op, 0));
1744
1745   return memory_address_p (mode, XEXP (op, 0));
1746 }
1747
1748 /* Return 1 if the operand is an offsettable memory operand.  */
1749
1750 int
1751 offsettable_mem_operand (op, mode)
1752      rtx op;
1753      enum machine_mode mode;
1754 {
1755   return ((GET_CODE (op) == MEM)
1756           && offsettable_address_p (reload_completed || reload_in_progress,
1757                                     mode, XEXP (op, 0)));
1758 }
1759
1760 /* Return 1 if the operand is either an easy FP constant (see above) or
1761    memory.  */
1762
1763 int
1764 mem_or_easy_const_operand (op, mode)
1765      rtx op;
1766      enum machine_mode mode;
1767 {
1768   return memory_operand (op, mode) || easy_fp_constant (op, mode);
1769 }
1770
1771 /* Return 1 if the operand is either a non-special register or an item
1772    that can be used as the operand of a `mode' add insn.  */
1773
1774 int
1775 add_operand (op, mode)
1776     rtx op;
1777     enum machine_mode mode;
1778 {
1779   if (GET_CODE (op) == CONST_INT)
1780     return (CONST_OK_FOR_LETTER_P (INTVAL (op), 'I')
1781             || CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
1782
1783   return gpc_reg_operand (op, mode);
1784 }
1785
1786 /* Return 1 if OP is a constant but not a valid add_operand.  */
1787
1788 int
1789 non_add_cint_operand (op, mode)
1790      rtx op;
1791      enum machine_mode mode ATTRIBUTE_UNUSED;
1792 {
1793   return (GET_CODE (op) == CONST_INT
1794           && !CONST_OK_FOR_LETTER_P (INTVAL (op), 'I')
1795           && !CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
1796 }
1797
1798 /* Return 1 if the operand is a non-special register or a constant that
1799    can be used as the operand of an OR or XOR insn on the RS/6000.  */
1800
1801 int
1802 logical_operand (op, mode)
1803      rtx op;
1804      enum machine_mode mode;
1805 {
1806   HOST_WIDE_INT opl, oph;
1807
1808   if (gpc_reg_operand (op, mode))
1809     return 1;
1810
1811   if (GET_CODE (op) == CONST_INT)
1812     {
1813       opl = INTVAL (op) & GET_MODE_MASK (mode);
1814
1815 #if HOST_BITS_PER_WIDE_INT <= 32
1816       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT && opl < 0)
1817         return 0;
1818 #endif
1819     }
1820   else if (GET_CODE (op) == CONST_DOUBLE)
1821     {
1822       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
1823         abort ();
1824
1825       opl = CONST_DOUBLE_LOW (op);
1826       oph = CONST_DOUBLE_HIGH (op);
1827       if (oph != 0)
1828         return 0;
1829     }
1830   else
1831     return 0;
1832
1833   return ((opl & ~ (unsigned HOST_WIDE_INT) 0xffff) == 0
1834           || (opl & ~ (unsigned HOST_WIDE_INT) 0xffff0000) == 0);
1835 }
1836
1837 /* Return 1 if C is a constant that is not a logical operand (as
1838    above), but could be split into one.  */
1839
1840 int
1841 non_logical_cint_operand (op, mode)
1842      rtx op;
1843      enum machine_mode mode;
1844 {
1845   return ((GET_CODE (op) == CONST_INT || GET_CODE (op) == CONST_DOUBLE)
1846           && ! logical_operand (op, mode)
1847           && reg_or_logical_cint_operand (op, mode));
1848 }
1849
1850 /* Return 1 if C is a constant that can be encoded in a 32-bit mask on the
1851    RS/6000.  It is if there are no more than two 1->0 or 0->1 transitions.
1852    Reject all ones and all zeros, since these should have been optimized
1853    away and confuse the making of MB and ME.  */
1854
1855 int
1856 mask_operand (op, mode)
1857      rtx op;
1858      enum machine_mode mode ATTRIBUTE_UNUSED;
1859 {
1860   HOST_WIDE_INT c, lsb;
1861
1862   if (GET_CODE (op) != CONST_INT)
1863     return 0;
1864
1865   c = INTVAL (op);
1866
1867   /* Fail in 64-bit mode if the mask wraps around because the upper
1868      32-bits of the mask will all be 1s, contrary to GCC's internal view.  */
1869   if (TARGET_POWERPC64 && (c & 0x80000001) == 0x80000001)
1870     return 0;
1871
1872   /* We don't change the number of transitions by inverting,
1873      so make sure we start with the LS bit zero.  */
1874   if (c & 1)
1875     c = ~c;
1876
1877   /* Reject all zeros or all ones.  */
1878   if (c == 0)
1879     return 0;
1880
1881   /* Find the first transition.  */
1882   lsb = c & -c;
1883
1884   /* Invert to look for a second transition.  */
1885   c = ~c;
1886
1887   /* Erase first transition.  */
1888   c &= -lsb;
1889
1890   /* Find the second transition (if any).  */
1891   lsb = c & -c;
1892
1893   /* Match if all the bits above are 1's (or c is zero).  */
1894   return c == -lsb;
1895 }
1896
1897 /* Return 1 for the PowerPC64 rlwinm corner case.  */
1898
1899 int
1900 mask_operand_wrap (op, mode)
1901      rtx op;
1902      enum machine_mode mode ATTRIBUTE_UNUSED;
1903 {
1904   HOST_WIDE_INT c, lsb;
1905
1906   if (GET_CODE (op) != CONST_INT)
1907     return 0;
1908
1909   c = INTVAL (op);
1910
1911   if ((c & 0x80000001) != 0x80000001)
1912     return 0;
1913
1914   c = ~c;
1915   if (c == 0)
1916     return 0;
1917
1918   lsb = c & -c;
1919   c = ~c;
1920   c &= -lsb;
1921   lsb = c & -c;
1922   return c == -lsb;
1923 }
1924
1925 /* Return 1 if the operand is a constant that is a PowerPC64 mask.
1926    It is if there are no more than one 1->0 or 0->1 transitions.
1927    Reject all zeros, since zero should have been optimized away and
1928    confuses the making of MB and ME.  */
1929
1930 int
1931 mask64_operand (op, mode)
1932      rtx op;
1933      enum machine_mode mode ATTRIBUTE_UNUSED;
1934 {
1935   if (GET_CODE (op) == CONST_INT)
1936     {
1937       HOST_WIDE_INT c, lsb;
1938
1939       c = INTVAL (op);
1940
1941       /* Reject all zeros.  */
1942       if (c == 0)
1943         return 0;
1944
1945       /* We don't change the number of transitions by inverting,
1946          so make sure we start with the LS bit zero.  */
1947       if (c & 1)
1948         c = ~c;
1949
1950       /* Find the transition, and check that all bits above are 1's.  */
1951       lsb = c & -c;
1952
1953       /* Match if all the bits above are 1's (or c is zero).  */
1954       return c == -lsb;
1955     }
1956   return 0;
1957 }
1958
1959 /* Like mask64_operand, but allow up to three transitions.  This
1960    predicate is used by insn patterns that generate two rldicl or
1961    rldicr machine insns.  */
1962
1963 int
1964 mask64_2_operand (op, mode)
1965      rtx op;
1966      enum machine_mode mode ATTRIBUTE_UNUSED;
1967 {
1968   if (GET_CODE (op) == CONST_INT)
1969     {
1970       HOST_WIDE_INT c, lsb;
1971
1972       c = INTVAL (op);
1973
1974       /* Disallow all zeros.  */
1975       if (c == 0)
1976         return 0;
1977
1978       /* We don't change the number of transitions by inverting,
1979          so make sure we start with the LS bit zero.  */
1980       if (c & 1)
1981         c = ~c;
1982
1983       /* Find the first transition.  */
1984       lsb = c & -c;
1985
1986       /* Invert to look for a second transition.  */
1987       c = ~c;
1988
1989       /* Erase first transition.  */
1990       c &= -lsb;
1991
1992       /* Find the second transition.  */
1993       lsb = c & -c;
1994
1995       /* Invert to look for a third transition.  */
1996       c = ~c;
1997
1998       /* Erase second transition.  */
1999       c &= -lsb;
2000
2001       /* Find the third transition (if any).  */
2002       lsb = c & -c;
2003
2004       /* Match if all the bits above are 1's (or c is zero).  */
2005       return c == -lsb;
2006     }
2007   return 0;
2008 }
2009
2010 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
2011    implement ANDing by the mask IN.  */
2012 void
2013 build_mask64_2_operands (in, out)
2014      rtx in;
2015      rtx *out;
2016 {
2017 #if HOST_BITS_PER_WIDE_INT >= 64
2018   unsigned HOST_WIDE_INT c, lsb, m1, m2;
2019   int shift;
2020
2021   if (GET_CODE (in) != CONST_INT)
2022     abort ();
2023
2024   c = INTVAL (in);
2025   if (c & 1)
2026     {
2027       /* Assume c initially something like 0x00fff000000fffff.  The idea
2028          is to rotate the word so that the middle ^^^^^^ group of zeros
2029          is at the MS end and can be cleared with an rldicl mask.  We then
2030          rotate back and clear off the MS    ^^ group of zeros with a
2031          second rldicl.  */
2032       c = ~c;                   /*   c == 0xff000ffffff00000 */
2033       lsb = c & -c;             /* lsb == 0x0000000000100000 */
2034       m1 = -lsb;                /*  m1 == 0xfffffffffff00000 */
2035       c = ~c;                   /*   c == 0x00fff000000fffff */
2036       c &= -lsb;                /*   c == 0x00fff00000000000 */
2037       lsb = c & -c;             /* lsb == 0x0000100000000000 */
2038       c = ~c;                   /*   c == 0xff000fffffffffff */
2039       c &= -lsb;                /*   c == 0xff00000000000000 */
2040       shift = 0;
2041       while ((lsb >>= 1) != 0)
2042         shift++;                /* shift == 44 on exit from loop */
2043       m1 <<= 64 - shift;        /*  m1 == 0xffffff0000000000 */
2044       m1 = ~m1;                 /*  m1 == 0x000000ffffffffff */
2045       m2 = ~c;                  /*  m2 == 0x00ffffffffffffff */
2046     }
2047   else
2048     {
2049       /* Assume c initially something like 0xff000f0000000000.  The idea
2050          is to rotate the word so that the     ^^^  middle group of zeros
2051          is at the LS end and can be cleared with an rldicr mask.  We then
2052          rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
2053          a second rldicr.  */
2054       lsb = c & -c;             /* lsb == 0x0000010000000000 */
2055       m2 = -lsb;                /*  m2 == 0xffffff0000000000 */
2056       c = ~c;                   /*   c == 0x00fff0ffffffffff */
2057       c &= -lsb;                /*   c == 0x00fff00000000000 */
2058       lsb = c & -c;             /* lsb == 0x0000100000000000 */
2059       c = ~c;                   /*   c == 0xff000fffffffffff */
2060       c &= -lsb;                /*   c == 0xff00000000000000 */
2061       shift = 0;
2062       while ((lsb >>= 1) != 0)
2063         shift++;                /* shift == 44 on exit from loop */
2064       m1 = ~c;                  /*  m1 == 0x00ffffffffffffff */
2065       m1 >>= shift;             /*  m1 == 0x0000000000000fff */
2066       m1 = ~m1;                 /*  m1 == 0xfffffffffffff000 */
2067     }
2068
2069   /* Note that when we only have two 0->1 and 1->0 transitions, one of the
2070      masks will be all 1's.  We are guaranteed more than one transition.  */
2071   out[0] = GEN_INT (64 - shift);
2072   out[1] = GEN_INT (m1);
2073   out[2] = GEN_INT (shift);
2074   out[3] = GEN_INT (m2);
2075 #else
2076   (void)in;
2077   (void)out;
2078   abort ();
2079 #endif
2080 }
2081
2082 /* Return 1 if the operand is either a non-special register or a constant
2083    that can be used as the operand of a PowerPC64 logical AND insn.  */
2084
2085 int
2086 and64_operand (op, mode)
2087     rtx op;
2088     enum machine_mode mode;
2089 {
2090   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
2091     return (gpc_reg_operand (op, mode) || mask64_operand (op, mode));
2092
2093   return (logical_operand (op, mode) || mask64_operand (op, mode));
2094 }
2095
2096 /* Like the above, but also match constants that can be implemented
2097    with two rldicl or rldicr insns.  */
2098
2099 int
2100 and64_2_operand (op, mode)
2101     rtx op;
2102     enum machine_mode mode;
2103 {
2104   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis. */
2105     return gpc_reg_operand (op, mode) || mask64_2_operand (op, mode);
2106
2107   return logical_operand (op, mode) || mask64_2_operand (op, mode);
2108 }
2109
2110 /* Return 1 if the operand is either a non-special register or a
2111    constant that can be used as the operand of an RS/6000 logical AND insn.  */
2112
2113 int
2114 and_operand (op, mode)
2115     rtx op;
2116     enum machine_mode mode;
2117 {
2118   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
2119     return (gpc_reg_operand (op, mode) || mask_operand (op, mode));
2120
2121   return (logical_operand (op, mode) || mask_operand (op, mode));
2122 }
2123
2124 /* Return 1 if the operand is a general register or memory operand.  */
2125
2126 int
2127 reg_or_mem_operand (op, mode)
2128      rtx op;
2129      enum machine_mode mode;
2130 {
2131   return (gpc_reg_operand (op, mode)
2132           || memory_operand (op, mode)
2133           || volatile_mem_operand (op, mode));
2134 }
2135
2136 /* Return 1 if the operand is a general register or memory operand without
2137    pre_inc or pre_dec which produces invalid form of PowerPC lwa
2138    instruction.  */
2139
2140 int
2141 lwa_operand (op, mode)
2142      rtx op;
2143      enum machine_mode mode;
2144 {
2145   rtx inner = op;
2146
2147   if (reload_completed && GET_CODE (inner) == SUBREG)
2148     inner = SUBREG_REG (inner);
2149     
2150   return gpc_reg_operand (inner, mode)
2151     || (memory_operand (inner, mode)
2152         && GET_CODE (XEXP (inner, 0)) != PRE_INC
2153         && GET_CODE (XEXP (inner, 0)) != PRE_DEC
2154         && (GET_CODE (XEXP (inner, 0)) != PLUS
2155             || GET_CODE (XEXP (XEXP (inner, 0), 1)) != CONST_INT
2156             || INTVAL (XEXP (XEXP (inner, 0), 1)) % 4 == 0));
2157 }
2158
2159 /* Return 1 if the operand, used inside a MEM, is a SYMBOL_REF.  */
2160
2161 int
2162 symbol_ref_operand (op, mode)
2163      rtx op;
2164      enum machine_mode mode;
2165 {
2166   if (mode != VOIDmode && GET_MODE (op) != mode)
2167     return 0;
2168
2169   return (GET_CODE (op) == SYMBOL_REF
2170           && (DEFAULT_ABI != ABI_AIX || SYMBOL_REF_FUNCTION_P (op)));
2171 }
2172
2173 /* Return 1 if the operand, used inside a MEM, is a valid first argument
2174    to CALL.  This is a SYMBOL_REF, a pseudo-register, LR or CTR.  */
2175
2176 int
2177 call_operand (op, mode)
2178      rtx op;
2179      enum machine_mode mode;
2180 {
2181   if (mode != VOIDmode && GET_MODE (op) != mode)
2182     return 0;
2183
2184   return (GET_CODE (op) == SYMBOL_REF
2185           || (GET_CODE (op) == REG
2186               && (REGNO (op) == LINK_REGISTER_REGNUM
2187                   || REGNO (op) == COUNT_REGISTER_REGNUM
2188                   || REGNO (op) >= FIRST_PSEUDO_REGISTER)));
2189 }
2190
2191 /* Return 1 if the operand is a SYMBOL_REF for a function known to be in
2192    this file.  */
2193
2194 int
2195 current_file_function_operand (op, mode)
2196      rtx op;
2197      enum machine_mode mode ATTRIBUTE_UNUSED;
2198 {
2199   return (GET_CODE (op) == SYMBOL_REF
2200           && (DEFAULT_ABI != ABI_AIX || SYMBOL_REF_FUNCTION_P (op))
2201           && (SYMBOL_REF_LOCAL_P (op)
2202               || (op == XEXP (DECL_RTL (current_function_decl), 0))));
2203 }
2204
2205 /* Return 1 if this operand is a valid input for a move insn.  */
2206
2207 int
2208 input_operand (op, mode)
2209      rtx op;
2210      enum machine_mode mode;
2211 {
2212   /* Memory is always valid.  */
2213   if (memory_operand (op, mode))
2214     return 1;
2215
2216   /* Only a tiny bit of handling for CONSTANT_P_RTX is necessary.  */
2217   if (GET_CODE (op) == CONSTANT_P_RTX)
2218     return 1;
2219
2220   /* For floating-point, easy constants are valid.  */
2221   if (GET_MODE_CLASS (mode) == MODE_FLOAT
2222       && CONSTANT_P (op)
2223       && easy_fp_constant (op, mode))
2224     return 1;
2225
2226   /* Allow any integer constant.  */
2227   if (GET_MODE_CLASS (mode) == MODE_INT
2228       && (GET_CODE (op) == CONST_INT
2229           || GET_CODE (op) == CONST_DOUBLE))
2230     return 1;
2231
2232   /* Allow easy vector constants.  */
2233   if (GET_CODE (op) == CONST_VECTOR
2234       && easy_vector_constant (op, mode))
2235     return 1;
2236
2237   /* For floating-point or multi-word mode, the only remaining valid type
2238      is a register.  */
2239   if (GET_MODE_CLASS (mode) == MODE_FLOAT
2240       || GET_MODE_SIZE (mode) > UNITS_PER_WORD)
2241     return register_operand (op, mode);
2242
2243   /* The only cases left are integral modes one word or smaller (we
2244      do not get called for MODE_CC values).  These can be in any
2245      register.  */
2246   if (register_operand (op, mode))
2247     return 1;
2248
2249   /* A SYMBOL_REF referring to the TOC is valid.  */
2250   if (legitimate_constant_pool_address_p (op))
2251     return 1;
2252
2253   /* A constant pool expression (relative to the TOC) is valid */
2254   if (toc_relative_expr_p (op))
2255     return 1;
2256
2257   /* V.4 allows SYMBOL_REFs and CONSTs that are in the small data region
2258      to be valid.  */
2259   if (DEFAULT_ABI == ABI_V4
2260       && (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST)
2261       && small_data_operand (op, Pmode))
2262     return 1;
2263
2264   return 0;
2265 }
2266
2267 /* Return 1 for an operand in small memory on V.4/eabi.  */
2268
2269 int
2270 small_data_operand (op, mode)
2271      rtx op ATTRIBUTE_UNUSED;
2272      enum machine_mode mode ATTRIBUTE_UNUSED;
2273 {
2274 #if TARGET_ELF
2275   rtx sym_ref;
2276
2277   if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
2278     return 0;
2279
2280   if (DEFAULT_ABI != ABI_V4)
2281     return 0;
2282
2283   if (GET_CODE (op) == SYMBOL_REF)
2284     sym_ref = op;
2285
2286   else if (GET_CODE (op) != CONST
2287            || GET_CODE (XEXP (op, 0)) != PLUS
2288            || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
2289            || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
2290     return 0;
2291
2292   else
2293     {
2294       rtx sum = XEXP (op, 0);
2295       HOST_WIDE_INT summand;
2296
2297       /* We have to be careful here, because it is the referenced address
2298         that must be 32k from _SDA_BASE_, not just the symbol.  */
2299       summand = INTVAL (XEXP (sum, 1));
2300       if (summand < 0 || (unsigned HOST_WIDE_INT) summand > g_switch_value)
2301        return 0;
2302
2303       sym_ref = XEXP (sum, 0);
2304     }
2305
2306   return SYMBOL_REF_SMALL_P (sym_ref);
2307 #else
2308   return 0;
2309 #endif
2310 }
2311
2312 /* Return 1 for all valid move insn operand combination involving altivec      
2313    vectors in gprs.  */
2314
2315 int
2316 altivec_in_gprs_p (rtx op0, rtx op1)
2317 {
2318   if (REG_P (op0) && REGNO_REG_CLASS (REGNO (op0)) == GENERAL_REGS)
2319     return 1;
2320
2321   if (REG_P (op1) && REGNO_REG_CLASS (REGNO (op1)) == GENERAL_REGS) 
2322     return 1;
2323   return 0;
2324 }
2325
2326 \f
2327 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address.  */
2328
2329 static int 
2330 constant_pool_expr_1 (op, have_sym, have_toc) 
2331     rtx op;
2332     int *have_sym;
2333     int *have_toc;
2334 {
2335   switch (GET_CODE(op)) 
2336     {
2337     case SYMBOL_REF:
2338       if (RS6000_SYMBOL_REF_TLS_P (op))
2339         return 0;
2340       else if (CONSTANT_POOL_ADDRESS_P (op))
2341         {
2342           if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (op), Pmode))
2343             {
2344               *have_sym = 1;
2345               return 1;
2346             }
2347           else
2348             return 0;
2349         }
2350       else if (! strcmp (XSTR (op, 0), toc_label_name))
2351         {
2352           *have_toc = 1;
2353           return 1;
2354         }
2355       else
2356         return 0;
2357     case PLUS:
2358     case MINUS:
2359       return (constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc)
2360               && constant_pool_expr_1 (XEXP (op, 1), have_sym, have_toc));
2361     case CONST:
2362       return constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc);
2363     case CONST_INT:
2364       return 1;
2365     default:
2366       return 0;
2367     }
2368 }
2369
2370 static bool
2371 constant_pool_expr_p (op)
2372     rtx op;
2373 {
2374   int have_sym = 0;
2375   int have_toc = 0;
2376   return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_sym;
2377 }
2378
2379 static bool
2380 toc_relative_expr_p (op)
2381     rtx op;
2382 {
2383   int have_sym = 0;
2384   int have_toc = 0;
2385   return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_toc;
2386 }
2387
2388 /* SPE offset addressing is limited to 5-bits worth of double words.  */
2389 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
2390
2391 bool
2392 legitimate_constant_pool_address_p (x)
2393      rtx x;
2394 {
2395   return (TARGET_TOC
2396           && GET_CODE (x) == PLUS
2397           && GET_CODE (XEXP (x, 0)) == REG
2398           && (TARGET_MINIMAL_TOC || REGNO (XEXP (x, 0)) == TOC_REGISTER)
2399           && constant_pool_expr_p (XEXP (x, 1)));
2400 }
2401
2402 static bool
2403 legitimate_small_data_p (mode, x)
2404      enum machine_mode mode;
2405      rtx x;
2406 {
2407   return (DEFAULT_ABI == ABI_V4
2408           && !flag_pic && !TARGET_TOC
2409           && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
2410           && small_data_operand (x, mode));
2411 }
2412
2413 static bool
2414 legitimate_offset_address_p (mode, x, strict)
2415      enum machine_mode mode;
2416      rtx x;
2417      int strict;
2418 {
2419   unsigned HOST_WIDE_INT offset, extra;
2420
2421   if (GET_CODE (x) != PLUS)
2422     return false;
2423   if (GET_CODE (XEXP (x, 0)) != REG)
2424     return false;
2425   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
2426     return false;
2427   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
2428     return false;
2429
2430   offset = INTVAL (XEXP (x, 1));
2431   extra = 0;
2432   switch (mode)
2433     {
2434     case V16QImode:
2435     case V8HImode:
2436     case V4SFmode:
2437     case V4SImode:
2438       /* AltiVec vector modes.  Only reg+reg addressing is valid here,
2439          which leaves the only valid constant offset of zero, which by
2440          canonicalization rules is also invalid.  */
2441       return false;
2442
2443     case V4HImode:
2444     case V2SImode:
2445     case V1DImode:
2446     case V2SFmode:
2447       /* SPE vector modes.  */
2448       return SPE_CONST_OFFSET_OK (offset);
2449
2450     case DFmode:
2451     case DImode:
2452       if (TARGET_32BIT)
2453         extra = 4;
2454       else if (offset & 3)
2455         return false;
2456       break;
2457
2458     case TFmode:
2459     case TImode:
2460       if (TARGET_32BIT)
2461         extra = 12;
2462       else if (offset & 3)
2463         return false;
2464       else
2465         extra = 8;
2466       break;
2467
2468     default:
2469       break;
2470     }
2471
2472   return (offset + extra >= offset) && (offset + extra + 0x8000 < 0x10000);
2473 }
2474
2475 static bool
2476 legitimate_indexed_address_p (x, strict)
2477      rtx x;
2478      int strict;
2479 {
2480   rtx op0, op1;
2481
2482   if (GET_CODE (x) != PLUS)
2483     return false;
2484   op0 = XEXP (x, 0);
2485   op1 = XEXP (x, 1);
2486
2487   if (!REG_P (op0) || !REG_P (op1))
2488     return false;
2489
2490   return ((INT_REG_OK_FOR_BASE_P (op0, strict)
2491            && INT_REG_OK_FOR_INDEX_P (op1, strict))
2492           || (INT_REG_OK_FOR_BASE_P (op1, strict)
2493               && INT_REG_OK_FOR_INDEX_P (op0, strict)));
2494 }
2495
2496 static inline bool
2497 legitimate_indirect_address_p (x, strict)
2498      rtx x;
2499      int strict;
2500 {
2501   return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
2502 }
2503
2504 static bool
2505 legitimate_lo_sum_address_p (mode, x, strict)
2506      enum machine_mode mode;
2507      rtx x;
2508      int strict;
2509 {
2510   if (GET_CODE (x) != LO_SUM)
2511     return false;
2512   if (GET_CODE (XEXP (x, 0)) != REG)
2513     return false;
2514   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
2515     return false;
2516   x = XEXP (x, 1);
2517
2518   if (TARGET_ELF)
2519     {
2520       if (DEFAULT_ABI != ABI_AIX && flag_pic)
2521         return false;
2522       if (TARGET_TOC)
2523         return false;
2524       if (GET_MODE_NUNITS (mode) != 1)
2525         return false;
2526       if (GET_MODE_BITSIZE (mode) > 32
2527           && !(TARGET_HARD_FLOAT && TARGET_FPRS && mode == DFmode))
2528         return false;
2529
2530       return CONSTANT_P (x);
2531     }
2532
2533   return false;
2534 }
2535
2536
2537 /* Try machine-dependent ways of modifying an illegitimate address
2538    to be legitimate.  If we find one, return the new, valid address.
2539    This is used from only one place: `memory_address' in explow.c.
2540
2541    OLDX is the address as it was before break_out_memory_refs was
2542    called.  In some cases it is useful to look at this to decide what
2543    needs to be done.
2544
2545    MODE is passed so that this function can use GO_IF_LEGITIMATE_ADDRESS.
2546
2547    It is always safe for this function to do nothing.  It exists to
2548    recognize opportunities to optimize the output.
2549
2550    On RS/6000, first check for the sum of a register with a constant
2551    integer that is out of range.  If so, generate code to add the
2552    constant with the low-order 16 bits masked to the register and force
2553    this result into another register (this can be done with `cau').
2554    Then generate an address of REG+(CONST&0xffff), allowing for the
2555    possibility of bit 16 being a one.
2556
2557    Then check for the sum of a register and something not constant, try to
2558    load the other things into a register and return the sum.  */
2559
2560 rtx
2561 rs6000_legitimize_address (x, oldx, mode)
2562      rtx x;
2563      rtx oldx ATTRIBUTE_UNUSED;
2564      enum machine_mode mode;
2565 {
2566   if (GET_CODE (x) == SYMBOL_REF)
2567     {
2568       enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
2569       if (model != 0)
2570         return rs6000_legitimize_tls_address (x, model);
2571     }
2572
2573   if (GET_CODE (x) == PLUS 
2574       && GET_CODE (XEXP (x, 0)) == REG
2575       && GET_CODE (XEXP (x, 1)) == CONST_INT
2576       && (unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000) >= 0x10000)
2577     { 
2578       HOST_WIDE_INT high_int, low_int;
2579       rtx sum;
2580       low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
2581       high_int = INTVAL (XEXP (x, 1)) - low_int;
2582       sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
2583                                          GEN_INT (high_int)), 0);
2584       return gen_rtx_PLUS (Pmode, sum, GEN_INT (low_int));
2585     }
2586   else if (GET_CODE (x) == PLUS 
2587            && GET_CODE (XEXP (x, 0)) == REG
2588            && GET_CODE (XEXP (x, 1)) != CONST_INT
2589            && GET_MODE_NUNITS (mode) == 1
2590            && ((TARGET_HARD_FLOAT && TARGET_FPRS)
2591                || TARGET_POWERPC64
2592                || (mode != DFmode && mode != TFmode))
2593            && (TARGET_POWERPC64 || mode != DImode)
2594            && mode != TImode)
2595     {
2596       return gen_rtx_PLUS (Pmode, XEXP (x, 0),
2597                            force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
2598     }
2599   else if (ALTIVEC_VECTOR_MODE (mode))
2600     {
2601       rtx reg;
2602
2603       /* Make sure both operands are registers.  */
2604       if (GET_CODE (x) == PLUS)
2605         return gen_rtx_PLUS (Pmode, force_reg (Pmode, XEXP (x, 0)),
2606                              force_reg (Pmode, XEXP (x, 1)));
2607
2608       reg = force_reg (Pmode, x);
2609       return reg;
2610     }
2611   else if (SPE_VECTOR_MODE (mode))
2612     {
2613       /* We accept [reg + reg] and [reg + OFFSET].  */
2614
2615       if (GET_CODE (x) == PLUS)
2616       {
2617         rtx op1 = XEXP (x, 0);
2618         rtx op2 = XEXP (x, 1);
2619
2620         op1 = force_reg (Pmode, op1);
2621
2622         if (GET_CODE (op2) != REG
2623             && (GET_CODE (op2) != CONST_INT
2624                 || !SPE_CONST_OFFSET_OK (INTVAL (op2))))
2625           op2 = force_reg (Pmode, op2);
2626
2627         return gen_rtx_PLUS (Pmode, op1, op2);
2628       }
2629
2630       return force_reg (Pmode, x);
2631     }
2632   else if (TARGET_ELF
2633            && TARGET_32BIT
2634            && TARGET_NO_TOC
2635            && ! flag_pic
2636            && GET_CODE (x) != CONST_INT
2637            && GET_CODE (x) != CONST_DOUBLE 
2638            && CONSTANT_P (x)
2639            && GET_MODE_NUNITS (mode) == 1
2640            && (GET_MODE_BITSIZE (mode) <= 32
2641                || ((TARGET_HARD_FLOAT && TARGET_FPRS) && mode == DFmode)))
2642     {
2643       rtx reg = gen_reg_rtx (Pmode);
2644       emit_insn (gen_elf_high (reg, (x)));
2645       return gen_rtx_LO_SUM (Pmode, reg, (x));
2646     }
2647   else if (TARGET_MACHO && TARGET_32BIT && TARGET_NO_TOC
2648            && ! flag_pic
2649 #if TARGET_MACHO
2650            && ! MACHO_DYNAMIC_NO_PIC_P
2651 #endif
2652            && GET_CODE (x) != CONST_INT
2653            && GET_CODE (x) != CONST_DOUBLE 
2654            && CONSTANT_P (x)
2655            && ((TARGET_HARD_FLOAT && TARGET_FPRS) || mode != DFmode)
2656            && mode != DImode 
2657            && mode != TImode)
2658     {
2659       rtx reg = gen_reg_rtx (Pmode);
2660       emit_insn (gen_macho_high (reg, (x)));
2661       return gen_rtx_LO_SUM (Pmode, reg, (x));
2662     }
2663   else if (TARGET_TOC 
2664            && constant_pool_expr_p (x)
2665            && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
2666     {
2667       return create_TOC_reference (x);
2668     }
2669   else
2670     return NULL_RTX;
2671 }
2672
2673 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
2674
2675 static GTY(()) rtx rs6000_tls_symbol;
2676 static rtx
2677 rs6000_tls_get_addr ()
2678 {
2679   if (!rs6000_tls_symbol)
2680     rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
2681
2682   return rs6000_tls_symbol;
2683 }
2684
2685 /* Construct the SYMBOL_REF for TLS GOT references.  */
2686
2687 static GTY(()) rtx rs6000_got_symbol;
2688 static rtx
2689 rs6000_got_sym ()
2690 {
2691   if (!rs6000_got_symbol)
2692     {
2693       rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
2694       SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
2695       SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
2696     }     
2697
2698   return rs6000_got_symbol;
2699 }
2700
2701 /* ADDR contains a thread-local SYMBOL_REF.  Generate code to compute
2702    this (thread-local) address.  */
2703
2704 static rtx
2705 rs6000_legitimize_tls_address (addr, model)
2706      rtx addr;
2707      enum tls_model model;
2708 {
2709   rtx dest, insn;
2710
2711   dest = gen_reg_rtx (Pmode);
2712   if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
2713     {
2714       rtx tlsreg;
2715
2716       if (TARGET_64BIT)
2717         {
2718           tlsreg = gen_rtx_REG (Pmode, 13);
2719           insn = gen_tls_tprel_64 (dest, tlsreg, addr);
2720         }
2721       else
2722         {
2723           tlsreg = gen_rtx_REG (Pmode, 2);
2724           insn = gen_tls_tprel_32 (dest, tlsreg, addr);
2725         }
2726       emit_insn (insn);
2727     }
2728   else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
2729     {
2730       rtx tlsreg, tmp;
2731
2732       tmp = gen_reg_rtx (Pmode);
2733       if (TARGET_64BIT)
2734         {
2735           tlsreg = gen_rtx_REG (Pmode, 13);
2736           insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
2737         }
2738       else
2739         {
2740           tlsreg = gen_rtx_REG (Pmode, 2);
2741           insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
2742         }
2743       emit_insn (insn);
2744       if (TARGET_64BIT)
2745         insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
2746       else
2747         insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
2748       emit_insn (insn);
2749     }
2750   else
2751     {
2752       rtx r3, got, tga, tmp1, tmp2, eqv;
2753
2754       if (TARGET_64BIT)
2755         got = gen_rtx_REG (Pmode, TOC_REGISTER);
2756       else
2757         {
2758           if (flag_pic == 1)
2759             got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
2760           else
2761             {
2762               rtx gsym = rs6000_got_sym ();
2763               got = gen_reg_rtx (Pmode);
2764               if (flag_pic == 0)
2765                 rs6000_emit_move (got, gsym, Pmode);
2766               else
2767                 {
2768                   char buf[30];
2769                   static int tls_got_labelno = 0;
2770                   rtx tempLR, lab, tmp3, mem;
2771                   rtx first, last;
2772
2773                   ASM_GENERATE_INTERNAL_LABEL (buf, "LTLS", tls_got_labelno++);
2774                   lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
2775                   tempLR = gen_reg_rtx (Pmode);
2776                   tmp1 = gen_reg_rtx (Pmode);
2777                   tmp2 = gen_reg_rtx (Pmode);
2778                   tmp3 = gen_reg_rtx (Pmode);
2779                   mem = gen_rtx_MEM (Pmode, tmp1);
2780                   RTX_UNCHANGING_P (mem) = 1;
2781
2782                   first = emit_insn (gen_load_toc_v4_PIC_1b (tempLR, lab,
2783                                                              gsym));
2784                   emit_move_insn (tmp1, tempLR);
2785                   emit_move_insn (tmp2, mem);
2786                   emit_insn (gen_addsi3 (tmp3, tmp1, tmp2));
2787                   last = emit_move_insn (got, tmp3);
2788                   REG_NOTES (last) = gen_rtx_EXPR_LIST (REG_EQUAL, gsym,
2789                                                         REG_NOTES (last));
2790                   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last,
2791                                                          REG_NOTES (first));
2792                   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first,
2793                                                         REG_NOTES (last));
2794                 }
2795             }
2796         }
2797
2798       if (model == TLS_MODEL_GLOBAL_DYNAMIC)
2799         {
2800           r3 = gen_rtx_REG (Pmode, 3);
2801           if (TARGET_64BIT)
2802             insn = gen_tls_gd_64 (r3, got, addr);
2803           else
2804             insn = gen_tls_gd_32 (r3, got, addr);
2805           start_sequence ();
2806           emit_insn (insn);
2807           tga = gen_rtx_MEM (Pmode, rs6000_tls_get_addr ());
2808           insn = gen_call_value (r3, tga, const0_rtx, const0_rtx);
2809           insn = emit_call_insn (insn);
2810           CONST_OR_PURE_CALL_P (insn) = 1;
2811           use_reg (&CALL_INSN_FUNCTION_USAGE (insn), r3);
2812           insn = get_insns ();
2813           end_sequence ();
2814           emit_libcall_block (insn, dest, r3, addr);
2815         }
2816       else if (model == TLS_MODEL_LOCAL_DYNAMIC)
2817         {
2818           r3 = gen_rtx_REG (Pmode, 3);
2819           if (TARGET_64BIT)
2820             insn = gen_tls_ld_64 (r3, got);
2821           else
2822             insn = gen_tls_ld_32 (r3, got);
2823           start_sequence ();
2824           emit_insn (insn);
2825           tga = gen_rtx_MEM (Pmode, rs6000_tls_get_addr ());
2826           insn = gen_call_value (r3, tga, const0_rtx, const0_rtx);
2827           insn = emit_call_insn (insn);
2828           CONST_OR_PURE_CALL_P (insn) = 1;
2829           use_reg (&CALL_INSN_FUNCTION_USAGE (insn), r3);
2830           insn = get_insns ();
2831           end_sequence ();
2832           tmp1 = gen_reg_rtx (Pmode);
2833           eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
2834                                 UNSPEC_TLSLD);
2835           emit_libcall_block (insn, tmp1, r3, eqv);
2836           if (rs6000_tls_size == 16)
2837             {
2838               if (TARGET_64BIT)
2839                 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
2840               else
2841                 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
2842             }
2843           else if (rs6000_tls_size == 32)
2844             {
2845               tmp2 = gen_reg_rtx (Pmode);
2846               if (TARGET_64BIT)
2847                 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
2848               else
2849                 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
2850               emit_insn (insn);
2851               if (TARGET_64BIT)
2852                 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
2853               else
2854                 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
2855             }
2856           else
2857             {
2858               tmp2 = gen_reg_rtx (Pmode);
2859               if (TARGET_64BIT)
2860                 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
2861               else
2862                 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
2863               emit_insn (insn);
2864               insn = gen_rtx_SET (Pmode, dest,
2865                                   gen_rtx_PLUS (Pmode, tmp2, tmp1));
2866             }
2867           emit_insn (insn);
2868         }
2869       else
2870         {
2871           /* IE, or 64 bit offset LE.  */
2872           tmp2 = gen_reg_rtx (Pmode);
2873           if (TARGET_64BIT)
2874             insn = gen_tls_got_tprel_64 (tmp2, got, addr);
2875           else
2876             insn = gen_tls_got_tprel_32 (tmp2, got, addr);
2877           emit_insn (insn);
2878           if (TARGET_64BIT)
2879             insn = gen_tls_tls_64 (dest, tmp2, addr);
2880           else
2881             insn = gen_tls_tls_32 (dest, tmp2, addr);
2882           emit_insn (insn);
2883         }
2884     }
2885
2886   return dest;
2887 }
2888
2889 /* Return 1 if X is a SYMBOL_REF for a TLS symbol.  This is used in
2890    instruction definitions.  */
2891
2892 int
2893 rs6000_tls_symbol_ref (x, mode)
2894      rtx x;
2895      enum machine_mode mode ATTRIBUTE_UNUSED;
2896 {
2897   return RS6000_SYMBOL_REF_TLS_P (x);
2898 }
2899
2900 /* Return 1 if X contains a thread-local symbol.  */
2901
2902 bool
2903 rs6000_tls_referenced_p (x)
2904      rtx x;
2905 {
2906   return for_each_rtx (&x, &rs6000_tls_symbol_ref_1, 0);
2907 }
2908
2909 /* Return 1 if *X is a thread-local symbol.  This is the same as
2910    rs6000_tls_symbol_ref except for the type of the unused argument.  */
2911
2912 static inline int
2913 rs6000_tls_symbol_ref_1 (x, data)
2914      rtx *x;
2915      void *data ATTRIBUTE_UNUSED;
2916 {
2917   return RS6000_SYMBOL_REF_TLS_P (*x);
2918 }
2919
2920 /* The convention appears to be to define this wherever it is used.
2921    With legitimize_reload_address now defined here, REG_MODE_OK_FOR_BASE_P
2922    is now used here.  */
2923 #ifndef REG_MODE_OK_FOR_BASE_P
2924 #define REG_MODE_OK_FOR_BASE_P(REGNO, MODE) REG_OK_FOR_BASE_P (REGNO)
2925 #endif
2926
2927 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS.  Returns a value to
2928    replace the input X, or the original X if no replacement is called for.
2929    The output parameter *WIN is 1 if the calling macro should goto WIN,
2930    0 if it should not.
2931
2932    For RS/6000, we wish to handle large displacements off a base
2933    register by splitting the addend across an addiu/addis and the mem insn.
2934    This cuts number of extra insns needed from 3 to 1.
2935
2936    On Darwin, we use this to generate code for floating point constants.
2937    A movsf_low is generated so we wind up with 2 instructions rather than 3.
2938    The Darwin code is inside #if TARGET_MACHO because only then is
2939    machopic_function_base_name() defined.  */
2940 rtx
2941 rs6000_legitimize_reload_address (x, mode, opnum, type, ind_levels, win)
2942     rtx x;
2943     enum machine_mode mode;
2944     int opnum;
2945     int type;
2946     int ind_levels ATTRIBUTE_UNUSED;
2947     int *win;
2948 {
2949   /* We must recognize output that we have already generated ourselves.  */ 
2950   if (GET_CODE (x) == PLUS
2951       && GET_CODE (XEXP (x, 0)) == PLUS
2952       && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
2953       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2954       && GET_CODE (XEXP (x, 1)) == CONST_INT)
2955     {
2956       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2957                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
2958                    opnum, (enum reload_type)type);
2959       *win = 1;
2960       return x;
2961     }
2962
2963 #if TARGET_MACHO
2964   if (DEFAULT_ABI == ABI_DARWIN && flag_pic
2965       && GET_CODE (x) == LO_SUM
2966       && GET_CODE (XEXP (x, 0)) == PLUS
2967       && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
2968       && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
2969       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 0)) == CONST
2970       && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
2971       && GET_CODE (XEXP (XEXP (x, 1), 0)) == MINUS
2972       && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 0)) == SYMBOL_REF
2973       && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 1)) == SYMBOL_REF)
2974     {
2975       /* Result of previous invocation of this function on Darwin
2976          floating point constant.  */
2977       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2978                 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
2979                 opnum, (enum reload_type)type);
2980       *win = 1;
2981       return x;
2982     }
2983 #endif
2984   if (GET_CODE (x) == PLUS
2985       && GET_CODE (XEXP (x, 0)) == REG
2986       && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2987       && REG_MODE_OK_FOR_BASE_P (XEXP (x, 0), mode)
2988       && GET_CODE (XEXP (x, 1)) == CONST_INT
2989       && !SPE_VECTOR_MODE (mode)
2990       && !ALTIVEC_VECTOR_MODE (mode))
2991     {
2992       HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
2993       HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
2994       HOST_WIDE_INT high
2995         = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
2996
2997       /* Check for 32-bit overflow.  */
2998       if (high + low != val)
2999         {
3000           *win = 0;
3001           return x;
3002         }
3003
3004       /* Reload the high part into a base reg; leave the low part
3005          in the mem directly.  */
3006
3007       x = gen_rtx_PLUS (GET_MODE (x),
3008                         gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
3009                                       GEN_INT (high)),
3010                         GEN_INT (low));
3011
3012       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3013                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
3014                    opnum, (enum reload_type)type);
3015       *win = 1;
3016       return x;
3017     }
3018 #if TARGET_MACHO
3019   if (GET_CODE (x) == SYMBOL_REF
3020       && DEFAULT_ABI == ABI_DARWIN
3021       && !ALTIVEC_VECTOR_MODE (mode)
3022       && flag_pic)
3023     {
3024       /* Darwin load of floating point constant.  */
3025       rtx offset = gen_rtx (CONST, Pmode,
3026                     gen_rtx (MINUS, Pmode, x,
3027                     gen_rtx (SYMBOL_REF, Pmode,
3028                         machopic_function_base_name ())));
3029       x = gen_rtx (LO_SUM, GET_MODE (x),
3030             gen_rtx (PLUS, Pmode, pic_offset_table_rtx,
3031                 gen_rtx (HIGH, Pmode, offset)), offset);
3032       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3033                 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
3034                 opnum, (enum reload_type)type);
3035       *win = 1;
3036       return x;
3037     }
3038    if (GET_CODE (x) == SYMBOL_REF
3039        && DEFAULT_ABI == ABI_DARWIN
3040        && !ALTIVEC_VECTOR_MODE (mode)
3041        && MACHO_DYNAMIC_NO_PIC_P)
3042      {
3043        /* Darwin load of floating point constant.  */
3044        x = gen_rtx (LO_SUM, GET_MODE (x),
3045                gen_rtx (HIGH, Pmode, x), x);
3046        push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3047                BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
3048                opnum, (enum reload_type)type);
3049        *win = 1;
3050        return x;
3051      }
3052 #endif
3053   if (TARGET_TOC
3054       && constant_pool_expr_p (x)
3055       && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), mode))
3056     {
3057       (x) = create_TOC_reference (x);
3058       *win = 1;
3059       return x;
3060     }
3061   *win = 0;
3062   return x;
3063 }    
3064
3065 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression
3066    that is a valid memory address for an instruction.
3067    The MODE argument is the machine mode for the MEM expression
3068    that wants to use this address.
3069
3070    On the RS/6000, there are four valid address: a SYMBOL_REF that
3071    refers to a constant pool entry of an address (or the sum of it
3072    plus a constant), a short (16-bit signed) constant plus a register,
3073    the sum of two registers, or a register indirect, possibly with an
3074    auto-increment.  For DFmode and DImode with a constant plus register,
3075    we must ensure that both words are addressable or PowerPC64 with offset
3076    word aligned.
3077
3078    For modes spanning multiple registers (DFmode in 32-bit GPRs,
3079    32-bit DImode, TImode), indexed addressing cannot be used because
3080    adjacent memory cells are accessed by adding word-sized offsets
3081    during assembly output.  */
3082 int
3083 rs6000_legitimate_address (mode, x, reg_ok_strict)
3084     enum machine_mode mode;
3085     rtx x;
3086     int reg_ok_strict;
3087 {
3088   if (RS6000_SYMBOL_REF_TLS_P (x))
3089     return 0;
3090   if (legitimate_indirect_address_p (x, reg_ok_strict))
3091     return 1;
3092   if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
3093       && !ALTIVEC_VECTOR_MODE (mode)
3094       && !SPE_VECTOR_MODE (mode)
3095       && TARGET_UPDATE
3096       && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
3097     return 1;
3098   if (legitimate_small_data_p (mode, x))
3099     return 1;
3100   if (legitimate_constant_pool_address_p (x))
3101     return 1;
3102   /* If not REG_OK_STRICT (before reload) let pass any stack offset.  */
3103   if (! reg_ok_strict
3104       && GET_CODE (x) == PLUS
3105       && GET_CODE (XEXP (x, 0)) == REG
3106       && XEXP (x, 0) == virtual_stack_vars_rtx
3107       && GET_CODE (XEXP (x, 1)) == CONST_INT)
3108     return 1;
3109   if (legitimate_offset_address_p (mode, x, reg_ok_strict))
3110     return 1;
3111   if (mode != TImode
3112       && ((TARGET_HARD_FLOAT && TARGET_FPRS)
3113           || TARGET_POWERPC64
3114           || (mode != DFmode && mode != TFmode))
3115       && (TARGET_POWERPC64 || mode != DImode)
3116       && legitimate_indexed_address_p (x, reg_ok_strict))
3117     return 1;
3118   if (legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
3119     return 1;
3120   return 0;
3121 }
3122
3123 /* Go to LABEL if ADDR (a legitimate address expression)
3124    has an effect that depends on the machine mode it is used for.
3125
3126    On the RS/6000 this is true of all integral offsets (since AltiVec
3127    modes don't allow them) or is a pre-increment or decrement.
3128
3129    ??? Except that due to conceptual problems in offsettable_address_p
3130    we can't really report the problems of integral offsets.  So leave
3131    this assuming that the adjustable offset must be valid for the 
3132    sub-words of a TFmode operand, which is what we had before.  */
3133
3134 bool
3135 rs6000_mode_dependent_address (addr)
3136      rtx addr;
3137 {
3138   switch (GET_CODE (addr))
3139     {
3140     case PLUS:
3141       if (GET_CODE (XEXP (addr, 1)) == CONST_INT)
3142         {
3143           unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
3144           return val + 12 + 0x8000 >= 0x10000;
3145         }
3146       break;
3147
3148     case LO_SUM:
3149       return true;
3150
3151     case PRE_INC:
3152     case PRE_DEC:
3153       return TARGET_UPDATE;
3154
3155     default:
3156       break;
3157     }
3158
3159   return false;
3160 }
3161 \f
3162 /* Try to output insns to set TARGET equal to the constant C if it can
3163    be done in less than N insns.  Do all computations in MODE.
3164    Returns the place where the output has been placed if it can be
3165    done and the insns have been emitted.  If it would take more than N
3166    insns, zero is returned and no insns and emitted.  */
3167
3168 rtx
3169 rs6000_emit_set_const (dest, mode, source, n)
3170      rtx dest, source;
3171      enum machine_mode mode;
3172      int n ATTRIBUTE_UNUSED;
3173 {
3174   rtx result, insn, set;
3175   HOST_WIDE_INT c0, c1;
3176
3177   if (mode == QImode || mode == HImode)
3178     {
3179       if (dest == NULL)
3180         dest = gen_reg_rtx (mode);
3181       emit_insn (gen_rtx_SET (VOIDmode, dest, source));
3182       return dest;
3183     }
3184   else if (mode == SImode)
3185     {
3186       result = no_new_pseudos ? dest : gen_reg_rtx (SImode);
3187
3188       emit_insn (gen_rtx_SET (VOIDmode, result,
3189                               GEN_INT (INTVAL (source)
3190                                        & (~ (HOST_WIDE_INT) 0xffff))));
3191       emit_insn (gen_rtx_SET (VOIDmode, dest,
3192                               gen_rtx_IOR (SImode, result,
3193                                            GEN_INT (INTVAL (source) & 0xffff))));
3194       result = dest;
3195     }
3196   else if (mode == DImode)
3197     {
3198       if (GET_CODE (source) == CONST_INT)
3199         {
3200           c0 = INTVAL (source);
3201           c1 = -(c0 < 0);
3202         }
3203       else if (GET_CODE (source) == CONST_DOUBLE)
3204         {
3205 #if HOST_BITS_PER_WIDE_INT >= 64
3206           c0 = CONST_DOUBLE_LOW (source);
3207           c1 = -(c0 < 0);
3208 #else
3209           c0 = CONST_DOUBLE_LOW (source);
3210           c1 = CONST_DOUBLE_HIGH (source);
3211 #endif
3212         }
3213       else
3214         abort ();
3215
3216       result = rs6000_emit_set_long_const (dest, c0, c1);
3217     }
3218   else
3219     abort ();
3220
3221   insn = get_last_insn ();
3222   set = single_set (insn);
3223   if (! CONSTANT_P (SET_SRC (set)))
3224     set_unique_reg_note (insn, REG_EQUAL, source);
3225
3226   return result;
3227 }
3228
3229 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
3230    fall back to a straight forward decomposition.  We do this to avoid
3231    exponential run times encountered when looking for longer sequences
3232    with rs6000_emit_set_const.  */
3233 static rtx
3234 rs6000_emit_set_long_const (dest, c1, c2)
3235      rtx dest;
3236      HOST_WIDE_INT c1, c2;
3237 {
3238   if (!TARGET_POWERPC64)
3239     {
3240       rtx operand1, operand2;
3241
3242       operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
3243                                         DImode);
3244       operand2 = operand_subword_force (dest, WORDS_BIG_ENDIAN != 0,
3245                                         DImode);
3246       emit_move_insn (operand1, GEN_INT (c1));
3247       emit_move_insn (operand2, GEN_INT (c2));
3248     }
3249   else
3250     {
3251       HOST_WIDE_INT ud1, ud2, ud3, ud4;
3252
3253       ud1 = c1 & 0xffff;
3254       ud2 = (c1 & 0xffff0000) >> 16;
3255 #if HOST_BITS_PER_WIDE_INT >= 64
3256       c2 = c1 >> 32;
3257 #endif
3258       ud3 = c2 & 0xffff;
3259       ud4 = (c2 & 0xffff0000) >> 16;
3260
3261       if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000)) 
3262           || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
3263         {
3264           if (ud1 & 0x8000)
3265             emit_move_insn (dest, GEN_INT (((ud1  ^ 0x8000) -  0x8000)));
3266           else
3267             emit_move_insn (dest, GEN_INT (ud1));
3268         }
3269
3270       else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000)) 
3271                || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
3272         {
3273           if (ud2 & 0x8000)
3274             emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000) 
3275                                            - 0x80000000));
3276           else
3277             emit_move_insn (dest, GEN_INT (ud2 << 16));
3278           if (ud1 != 0)
3279             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
3280         }
3281       else if ((ud4 == 0xffff && (ud3 & 0x8000)) 
3282                || (ud4 == 0 && ! (ud3 & 0x8000)))
3283         {
3284           if (ud3 & 0x8000)
3285             emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000) 
3286                                            - 0x80000000));
3287           else
3288             emit_move_insn (dest, GEN_INT (ud3 << 16));
3289
3290           if (ud2 != 0)
3291             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud2)));
3292           emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (16)));
3293           if (ud1 != 0)
3294             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
3295         }
3296       else 
3297         {
3298           if (ud4 & 0x8000)
3299             emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000) 
3300                                            - 0x80000000));
3301           else
3302             emit_move_insn (dest, GEN_INT (ud4 << 16));
3303
3304           if (ud3 != 0)
3305             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud3)));
3306
3307           emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (32)));
3308           if (ud2 != 0)
3309             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, 
3310                                                GEN_INT (ud2 << 16)));   
3311           if (ud1 != 0)
3312             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
3313         }
3314     }
3315   return dest;
3316 }
3317
3318 /* Emit a move from SOURCE to DEST in mode MODE.  */
3319 void
3320 rs6000_emit_move (dest, source, mode)
3321      rtx dest;
3322      rtx source;
3323      enum machine_mode mode;
3324 {
3325   rtx operands[2];
3326   operands[0] = dest;
3327   operands[1] = source;
3328   
3329   /* Sanity checks.  Check that we get CONST_DOUBLE only when we should.  */
3330   if (GET_CODE (operands[1]) == CONST_DOUBLE
3331       && ! FLOAT_MODE_P (mode)
3332       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3333     {
3334       /* FIXME.  This should never happen.  */
3335       /* Since it seems that it does, do the safe thing and convert
3336          to a CONST_INT.  */
3337       operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
3338     }
3339   if (GET_CODE (operands[1]) == CONST_DOUBLE
3340       && ! FLOAT_MODE_P (mode)
3341       && ((CONST_DOUBLE_HIGH (operands[1]) == 0
3342            && CONST_DOUBLE_LOW (operands[1]) >= 0)
3343           || (CONST_DOUBLE_HIGH (operands[1]) == -1
3344               && CONST_DOUBLE_LOW (operands[1]) < 0)))
3345     abort ();
3346
3347   /* Check if GCC is setting up a block move that will end up using FP
3348      registers as temporaries.  We must make sure this is acceptable.  */
3349   if (GET_CODE (operands[0]) == MEM
3350       && GET_CODE (operands[1]) == MEM
3351       && mode == DImode
3352       && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
3353           || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
3354       && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
3355                                             ? 32 : MEM_ALIGN (operands[0])))
3356             || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
3357                                                ? 32 
3358                                                : MEM_ALIGN (operands[1]))))
3359       && ! MEM_VOLATILE_P (operands [0])
3360       && ! MEM_VOLATILE_P (operands [1]))
3361     {
3362       emit_move_insn (adjust_address (operands[0], SImode, 0),
3363                       adjust_address (operands[1], SImode, 0));
3364       emit_move_insn (adjust_address (operands[0], SImode, 4),
3365                       adjust_address (operands[1], SImode, 4));
3366       return;
3367     }
3368   
3369   if (!no_new_pseudos)
3370     {
3371       if (GET_CODE (operands[1]) == MEM && optimize > 0
3372           && (mode == QImode || mode == HImode || mode == SImode)
3373           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3374         {
3375           rtx reg = gen_reg_rtx (word_mode);
3376
3377           emit_insn (gen_rtx_SET (word_mode, reg,
3378                                   gen_rtx_ZERO_EXTEND (word_mode,
3379                                                        operands[1])));
3380           operands[1] = gen_lowpart (mode, reg);
3381         }
3382       if (GET_CODE (operands[0]) != REG)
3383         operands[1] = force_reg (mode, operands[1]);
3384     }
3385
3386   if (mode == SFmode && ! TARGET_POWERPC
3387       && TARGET_HARD_FLOAT && TARGET_FPRS
3388       && GET_CODE (operands[0]) == MEM)
3389     {
3390       int regnum;
3391
3392       if (reload_in_progress || reload_completed)
3393         regnum = true_regnum (operands[1]);
3394       else if (GET_CODE (operands[1]) == REG)
3395         regnum = REGNO (operands[1]);
3396       else
3397         regnum = -1;
3398       
3399       /* If operands[1] is a register, on POWER it may have
3400          double-precision data in it, so truncate it to single
3401          precision.  */
3402       if (FP_REGNO_P (regnum) || regnum >= FIRST_PSEUDO_REGISTER)
3403         {
3404           rtx newreg;
3405           newreg = (no_new_pseudos ? operands[1] : gen_reg_rtx (mode));
3406           emit_insn (gen_aux_truncdfsf2 (newreg, operands[1]));
3407           operands[1] = newreg;
3408         }
3409     }
3410
3411   /* Recognize the case where operand[1] is a reference to thread-local
3412      data and load its address to a register.  */
3413   if (GET_CODE (operands[1]) == SYMBOL_REF)
3414     {
3415       enum tls_model model = SYMBOL_REF_TLS_MODEL (operands[1]);
3416       if (model != 0)
3417         operands[1] = rs6000_legitimize_tls_address (operands[1], model);
3418     }
3419
3420   /* Handle the case where reload calls us with an invalid address.  */
3421   if (reload_in_progress && mode == Pmode
3422       && (! general_operand (operands[1], mode)
3423           || ! nonimmediate_operand (operands[0], mode)))
3424     goto emit_set;
3425
3426   /* Handle the case of CONSTANT_P_RTX.  */
3427   if (GET_CODE (operands[1]) == CONSTANT_P_RTX)
3428     goto emit_set;
3429   
3430   /* FIXME:  In the long term, this switch statement should go away
3431      and be replaced by a sequence of tests based on things like
3432      mode == Pmode.  */
3433   switch (mode)
3434     {
3435     case HImode:
3436     case QImode:
3437       if (CONSTANT_P (operands[1])
3438           && GET_CODE (operands[1]) != CONST_INT)
3439         operands[1] = force_const_mem (mode, operands[1]);
3440       break;
3441
3442     case TFmode:
3443     case DFmode:
3444     case SFmode:
3445       if (CONSTANT_P (operands[1]) 
3446           && ! easy_fp_constant (operands[1], mode))
3447         operands[1] = force_const_mem (mode, operands[1]);
3448       break;
3449       
3450     case V16QImode:
3451     case V8HImode:
3452     case V4SFmode:
3453     case V4SImode:
3454     case V4HImode:
3455     case V2SFmode:
3456     case V2SImode:
3457     case V1DImode:
3458       if (CONSTANT_P (operands[1])
3459           && !easy_vector_constant (operands[1], mode))
3460         operands[1] = force_const_mem (mode, operands[1]);
3461       break;
3462       
3463     case SImode:
3464     case DImode:
3465       /* Use default pattern for address of ELF small data */
3466       if (TARGET_ELF
3467           && mode == Pmode
3468           && DEFAULT_ABI == ABI_V4
3469           && (GET_CODE (operands[1]) == SYMBOL_REF 
3470               || GET_CODE (operands[1]) == CONST)
3471           && small_data_operand (operands[1], mode))
3472         {
3473           emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
3474           return;
3475         }
3476
3477       if (DEFAULT_ABI == ABI_V4
3478           && mode == Pmode && mode == SImode
3479           && flag_pic == 1 && got_operand (operands[1], mode))
3480         {
3481           emit_insn (gen_movsi_got (operands[0], operands[1]));
3482           return;
3483         }
3484
3485       if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
3486           && TARGET_NO_TOC
3487           && ! flag_pic
3488           && mode == Pmode
3489           && CONSTANT_P (operands[1])
3490           && GET_CODE (operands[1]) != HIGH
3491           && GET_CODE (operands[1]) != CONST_INT)
3492         {
3493           rtx target = (no_new_pseudos ? operands[0] : gen_reg_rtx (mode));
3494
3495           /* If this is a function address on -mcall-aixdesc,
3496              convert it to the address of the descriptor.  */
3497           if (DEFAULT_ABI == ABI_AIX
3498               && GET_CODE (operands[1]) == SYMBOL_REF
3499               && XSTR (operands[1], 0)[0] == '.')
3500             {
3501               const char *name = XSTR (operands[1], 0);
3502               rtx new_ref;
3503               while (*name == '.')
3504                 name++;
3505               new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
3506               CONSTANT_POOL_ADDRESS_P (new_ref)
3507                 = CONSTANT_POOL_ADDRESS_P (operands[1]);
3508               SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
3509               SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
3510               SYMBOL_REF_DECL (new_ref) = SYMBOL_REF_DECL (operands[1]);
3511               operands[1] = new_ref;
3512             }
3513
3514           if (DEFAULT_ABI == ABI_DARWIN)
3515             {
3516 #if TARGET_MACHO
3517               if (MACHO_DYNAMIC_NO_PIC_P)
3518                 {
3519                   /* Take care of any required data indirection.  */
3520                   operands[1] = rs6000_machopic_legitimize_pic_address (
3521                                   operands[1], mode, operands[0]);
3522                   if (operands[0] != operands[1])
3523                     emit_insn (gen_rtx_SET (VOIDmode,
3524                                             operands[0], operands[1]));
3525                   return;
3526                 }
3527 #endif
3528               emit_insn (gen_macho_high (target, operands[1]));
3529               emit_insn (gen_macho_low (operands[0], target, operands[1]));
3530               return;
3531             }
3532
3533           emit_insn (gen_elf_high (target, operands[1]));
3534           emit_insn (gen_elf_low (operands[0], target, operands[1]));
3535           return;
3536         }
3537
3538       /* If this is a SYMBOL_REF that refers to a constant pool entry,
3539          and we have put it in the TOC, we just need to make a TOC-relative
3540          reference to it.  */
3541       if (TARGET_TOC
3542           && GET_CODE (operands[1]) == SYMBOL_REF
3543           && constant_pool_expr_p (operands[1])
3544           && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (operands[1]),
3545                                               get_pool_mode (operands[1])))
3546         {
3547           operands[1] = create_TOC_reference (operands[1]);
3548         }
3549       else if (mode == Pmode
3550                && CONSTANT_P (operands[1])
3551                && ((GET_CODE (operands[1]) != CONST_INT
3552                     && ! easy_fp_constant (operands[1], mode))
3553                    || (GET_CODE (operands[1]) == CONST_INT
3554                        && num_insns_constant (operands[1], mode) > 2)
3555                    || (GET_CODE (operands[0]) == REG
3556                        && FP_REGNO_P (REGNO (operands[0]))))
3557                && GET_CODE (operands[1]) != HIGH
3558                && ! legitimate_constant_pool_address_p (operands[1])
3559                && ! toc_relative_expr_p (operands[1]))
3560         {
3561           /* Emit a USE operation so that the constant isn't deleted if
3562              expensive optimizations are turned on because nobody
3563              references it.  This should only be done for operands that
3564              contain SYMBOL_REFs with CONSTANT_POOL_ADDRESS_P set.
3565              This should not be done for operands that contain LABEL_REFs.
3566              For now, we just handle the obvious case.  */
3567           if (GET_CODE (operands[1]) != LABEL_REF)
3568             emit_insn (gen_rtx_USE (VOIDmode, operands[1]));
3569
3570 #if TARGET_MACHO
3571           /* Darwin uses a special PIC legitimizer.  */
3572           if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
3573             {
3574               operands[1] =
3575                 rs6000_machopic_legitimize_pic_address (operands[1], mode,
3576                                                         operands[0]);
3577               if (operands[0] != operands[1])
3578                 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
3579               return;
3580             }
3581 #endif
3582
3583           /* If we are to limit the number of things we put in the TOC and
3584              this is a symbol plus a constant we can add in one insn,
3585              just put the symbol in the TOC and add the constant.  Don't do
3586              this if reload is in progress.  */
3587           if (GET_CODE (operands[1]) == CONST
3588               && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
3589               && GET_CODE (XEXP (operands[1], 0)) == PLUS
3590               && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
3591               && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
3592                   || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
3593               && ! side_effects_p (operands[0]))
3594             {
3595               rtx sym =
3596                 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
3597               rtx other = XEXP (XEXP (operands[1], 0), 1);
3598
3599               sym = force_reg (mode, sym);
3600               if (mode == SImode)
3601                 emit_insn (gen_addsi3 (operands[0], sym, other));
3602               else
3603                 emit_insn (gen_adddi3 (operands[0], sym, other));
3604               return;
3605             }
3606
3607           operands[1] = force_const_mem (mode, operands[1]);
3608
3609           if (TARGET_TOC 
3610               && constant_pool_expr_p (XEXP (operands[1], 0))
3611               && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
3612                         get_pool_constant (XEXP (operands[1], 0)),
3613                         get_pool_mode (XEXP (operands[1], 0))))
3614             {
3615               operands[1]
3616                 = gen_rtx_MEM (mode,
3617                                create_TOC_reference (XEXP (operands[1], 0)));
3618               set_mem_alias_set (operands[1], get_TOC_alias_set ());
3619               RTX_UNCHANGING_P (operands[1]) = 1;
3620             }
3621         }
3622       break;
3623
3624     case TImode:
3625       if (GET_CODE (operands[0]) == MEM
3626           && GET_CODE (XEXP (operands[0], 0)) != REG
3627           && ! reload_in_progress)
3628         operands[0]
3629           = replace_equiv_address (operands[0],
3630                                    copy_addr_to_reg (XEXP (operands[0], 0)));
3631
3632       if (GET_CODE (operands[1]) == MEM
3633           && GET_CODE (XEXP (operands[1], 0)) != REG
3634           && ! reload_in_progress)
3635         operands[1]
3636           = replace_equiv_address (operands[1],
3637                                    copy_addr_to_reg (XEXP (operands[1], 0)));
3638       if (TARGET_POWER)
3639         {
3640           emit_insn (gen_rtx_PARALLEL (VOIDmode,
3641                        gen_rtvec (2,
3642                                   gen_rtx_SET (VOIDmode,
3643                                                operands[0], operands[1]),
3644                                   gen_rtx_CLOBBER (VOIDmode,
3645                                                    gen_rtx_SCRATCH (SImode)))));
3646           return;
3647         }
3648       break;
3649
3650     default:
3651       abort ();
3652     }
3653
3654   /* Above, we may have called force_const_mem which may have returned
3655      an invalid address.  If we can, fix this up; otherwise, reload will
3656      have to deal with it.  */
3657   if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
3658     operands[1] = validize_mem (operands[1]);
3659
3660  emit_set:
3661   emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
3662 }
3663 \f
3664 /* Initialize a variable CUM of type CUMULATIVE_ARGS
3665    for a call to a function whose data type is FNTYPE.
3666    For a library call, FNTYPE is 0.
3667
3668    For incoming args we set the number of arguments in the prototype large
3669    so we never return a PARALLEL.  */
3670
3671 void
3672 init_cumulative_args (cum, fntype, libname, incoming, libcall)
3673      CUMULATIVE_ARGS *cum;
3674      tree fntype;
3675      rtx libname ATTRIBUTE_UNUSED;
3676      int incoming;
3677      int libcall;
3678 {
3679   static CUMULATIVE_ARGS zero_cumulative;
3680
3681   *cum = zero_cumulative;
3682   cum->words = 0;
3683   cum->fregno = FP_ARG_MIN_REG;
3684   cum->vregno = ALTIVEC_ARG_MIN_REG;
3685   cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
3686   cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
3687                       ? CALL_LIBCALL : CALL_NORMAL);
3688   cum->sysv_gregno = GP_ARG_MIN_REG;
3689   cum->stdarg = fntype
3690     && (TYPE_ARG_TYPES (fntype) != 0
3691         && (TREE_VALUE (tree_last  (TYPE_ARG_TYPES (fntype)))
3692             != void_type_node));
3693
3694   if (incoming)
3695     cum->nargs_prototype = 1000;                /* don't return a PARALLEL */
3696
3697   else if (cum->prototype)
3698     cum->nargs_prototype = (list_length (TYPE_ARG_TYPES (fntype)) - 1
3699                             + (TYPE_MODE (TREE_TYPE (fntype)) == BLKmode
3700                                || RETURN_IN_MEMORY (TREE_TYPE (fntype))));
3701
3702   else
3703     cum->nargs_prototype = 0;
3704
3705   cum->orig_nargs = cum->nargs_prototype;
3706
3707   /* Check for a longcall attribute.  */
3708   if (fntype
3709       && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
3710       && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype)))
3711     cum->call_cookie = CALL_LONG;
3712
3713   if (TARGET_DEBUG_ARG)
3714     {
3715       fprintf (stderr, "\ninit_cumulative_args:");
3716       if (fntype)
3717         {
3718           tree ret_type = TREE_TYPE (fntype);
3719           fprintf (stderr, " ret code = %s,",
3720                    tree_code_name[ (int)TREE_CODE (ret_type) ]);
3721         }
3722
3723       if (cum->call_cookie & CALL_LONG)
3724         fprintf (stderr, " longcall,");
3725
3726       fprintf (stderr, " proto = %d, nargs = %d\n",
3727                cum->prototype, cum->nargs_prototype);
3728     }
3729 }
3730 \f
3731 /* If defined, a C expression which determines whether, and in which
3732    direction, to pad out an argument with extra space.  The value
3733    should be of type `enum direction': either `upward' to pad above
3734    the argument, `downward' to pad below, or `none' to inhibit
3735    padding.
3736
3737    For the AIX ABI structs are always stored left shifted in their
3738    argument slot.  */
3739
3740 enum direction
3741 function_arg_padding (mode, type)
3742      enum machine_mode mode;
3743      tree type;
3744 {
3745   if (type != 0 && AGGREGATE_TYPE_P (type))
3746     return upward;
3747
3748   /* This is the default definition.  */
3749   return (! BYTES_BIG_ENDIAN
3750           ? upward
3751           : ((mode == BLKmode
3752               ? (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3753                  && int_size_in_bytes (type) < (PARM_BOUNDARY / BITS_PER_UNIT))
3754               : GET_MODE_BITSIZE (mode) < PARM_BOUNDARY)
3755              ? downward : upward));
3756 }
3757
3758 /* If defined, a C expression that gives the alignment boundary, in bits,
3759    of an argument with the specified mode and type.  If it is not defined, 
3760    PARM_BOUNDARY is used for all arguments.
3761    
3762    V.4 wants long longs to be double word aligned.  */
3763
3764 int
3765 function_arg_boundary (mode, type)
3766      enum machine_mode mode;
3767      tree type ATTRIBUTE_UNUSED;
3768 {
3769   if (DEFAULT_ABI == ABI_V4 && (mode == DImode || mode == DFmode))
3770     return 64;
3771    else if (SPE_VECTOR_MODE (mode))
3772      return 64;
3773   else if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
3774     return 128;
3775   else
3776     return PARM_BOUNDARY;
3777 }
3778 \f
3779 /* Update the data in CUM to advance over an argument
3780    of mode MODE and data type TYPE.
3781    (TYPE is null for libcalls where that information may not be available.)  */
3782
3783 void
3784 function_arg_advance (cum, mode, type, named)
3785      CUMULATIVE_ARGS *cum;
3786      enum machine_mode mode;
3787      tree type;
3788      int named;
3789 {
3790   cum->nargs_prototype--;
3791
3792   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
3793     {
3794       if (cum->vregno <= ALTIVEC_ARG_MAX_REG && cum->nargs_prototype >= 0)
3795         cum->vregno++;
3796       else
3797         cum->words += RS6000_ARG_SIZE (mode, type);
3798     }
3799   else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
3800            && !cum->stdarg
3801            && cum->sysv_gregno <= GP_ARG_MAX_REG)
3802     cum->sysv_gregno++;
3803   else if (DEFAULT_ABI == ABI_V4)
3804     {
3805       if (TARGET_HARD_FLOAT && TARGET_FPRS
3806           && (mode == SFmode || mode == DFmode))
3807         {
3808           if (cum->fregno <= FP_ARG_V4_MAX_REG)
3809             cum->fregno++;
3810           else
3811             {
3812               if (mode == DFmode)
3813                 cum->words += cum->words & 1;
3814               cum->words += RS6000_ARG_SIZE (mode, type);
3815             }
3816         }
3817       else
3818         {
3819           int n_words;
3820           int gregno = cum->sysv_gregno;
3821
3822           /* Aggregates and IEEE quad get passed by reference.  */
3823           if ((type && AGGREGATE_TYPE_P (type))
3824               || mode == TFmode)
3825             n_words = 1;
3826           else 
3827             n_words = RS6000_ARG_SIZE (mode, type);
3828
3829           /* Long long and SPE vectors are put in odd registers.  */
3830           if (n_words == 2 && (gregno & 1) == 0)
3831             gregno += 1;
3832
3833           /* Long long and SPE vectors are not split between registers
3834              and stack.  */
3835           if (gregno + n_words - 1 > GP_ARG_MAX_REG)
3836             {
3837               /* Long long is aligned on the stack.  */
3838               if (n_words == 2)
3839                 cum->words += cum->words & 1;
3840               cum->words += n_words;
3841             }
3842
3843           /* Note: continuing to accumulate gregno past when we've started
3844              spilling to the stack indicates the fact that we've started
3845              spilling to the stack to expand_builtin_saveregs.  */
3846           cum->sysv_gregno = gregno + n_words;
3847         }
3848
3849       if (TARGET_DEBUG_ARG)
3850         {
3851           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
3852                    cum->words, cum->fregno);
3853           fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
3854                    cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
3855           fprintf (stderr, "mode = %4s, named = %d\n",
3856                    GET_MODE_NAME (mode), named);
3857         }
3858     }
3859   else
3860     {
3861       int align = (TARGET_32BIT && (cum->words & 1) != 0
3862                    && function_arg_boundary (mode, type) == 64) ? 1 : 0;
3863
3864       cum->words += align + RS6000_ARG_SIZE (mode, type);
3865
3866       if (GET_MODE_CLASS (mode) == MODE_FLOAT
3867           && TARGET_HARD_FLOAT && TARGET_FPRS)
3868         cum->fregno += (mode == TFmode ? 2 : 1);
3869
3870       if (TARGET_DEBUG_ARG)
3871         {
3872           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
3873                    cum->words, cum->fregno);
3874           fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
3875                    cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
3876           fprintf (stderr, "named = %d, align = %d\n", named, align);
3877         }
3878     }
3879 }
3880
3881 /* Determine where to put a SIMD argument on the SPE.  */
3882 static rtx
3883 rs6000_spe_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode, tree type)
3884 {
3885   if (cum->stdarg)
3886     {
3887       int gregno = cum->sysv_gregno;
3888       int n_words = RS6000_ARG_SIZE (mode, type);
3889
3890       /* SPE vectors are put in odd registers.  */
3891       if (n_words == 2 && (gregno & 1) == 0)
3892         gregno += 1;
3893
3894       if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
3895         {
3896           rtx r1, r2;
3897           enum machine_mode m = SImode;
3898
3899           r1 = gen_rtx_REG (m, gregno);
3900           r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
3901           r2 = gen_rtx_REG (m, gregno + 1);
3902           r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
3903           return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
3904         }
3905       else
3906         return NULL;
3907     }
3908   else
3909     {
3910       if (cum->sysv_gregno <= GP_ARG_MAX_REG)
3911         return gen_rtx_REG (mode, cum->sysv_gregno);
3912       else
3913         return NULL;
3914     }
3915 }
3916
3917 /* Determine where to put an argument to a function.
3918    Value is zero to push the argument on the stack,
3919    or a hard register in which to store the argument.
3920
3921    MODE is the argument's machine mode.
3922    TYPE is the data type of the argument (as a tree).
3923     This is null for libcalls where that information may
3924     not be available.
3925    CUM is a variable of type CUMULATIVE_ARGS which gives info about
3926     the preceding args and about the function being called.
3927    NAMED is nonzero if this argument is a named parameter
3928     (otherwise it is an extra parameter matching an ellipsis).
3929
3930    On RS/6000 the first eight words of non-FP are normally in registers
3931    and the rest are pushed.  Under AIX, the first 13 FP args are in registers.
3932    Under V.4, the first 8 FP args are in registers.
3933
3934    If this is floating-point and no prototype is specified, we use
3935    both an FP and integer register (or possibly FP reg and stack).  Library
3936    functions (when CALL_LIBCALL is set) always have the proper types for args,
3937    so we can pass the FP value just in one register.  emit_library_function
3938    doesn't support PARALLEL anyway.  */
3939
3940 struct rtx_def *
3941 function_arg (cum, mode, type, named)
3942      CUMULATIVE_ARGS *cum;
3943      enum machine_mode mode;
3944      tree type;
3945      int named;
3946 {
3947   enum rs6000_abi abi = DEFAULT_ABI;
3948
3949   /* Return a marker to indicate whether CR1 needs to set or clear the
3950      bit that V.4 uses to say fp args were passed in registers.
3951      Assume that we don't need the marker for software floating point,
3952      or compiler generated library calls.  */
3953   if (mode == VOIDmode)
3954     {
3955       if (abi == ABI_V4
3956           && cum->nargs_prototype < 0
3957           && (cum->call_cookie & CALL_LIBCALL) == 0
3958           && (cum->prototype || TARGET_NO_PROTOTYPE))
3959         {
3960           /* For the SPE, we need to crxor CR6 always.  */
3961           if (TARGET_SPE_ABI)
3962             return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
3963           else if (TARGET_HARD_FLOAT && TARGET_FPRS)
3964             return GEN_INT (cum->call_cookie
3965                             | ((cum->fregno == FP_ARG_MIN_REG)
3966                                ? CALL_V4_SET_FP_ARGS
3967                                : CALL_V4_CLEAR_FP_ARGS));
3968         }
3969
3970       return GEN_INT (cum->call_cookie);
3971     }
3972
3973   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
3974     {
3975       if (named && cum->vregno <= ALTIVEC_ARG_MAX_REG)
3976         return gen_rtx_REG (mode, cum->vregno);
3977       else
3978         return NULL;
3979     }
3980   else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode))
3981     return rs6000_spe_function_arg (cum, mode, type);
3982   else if (abi == ABI_V4)
3983     {
3984       if (TARGET_HARD_FLOAT && TARGET_FPRS
3985           && (mode == SFmode || mode == DFmode))
3986         {
3987           if (cum->fregno <= FP_ARG_V4_MAX_REG)
3988             return gen_rtx_REG (mode, cum->fregno);
3989           else
3990             return NULL;
3991         }
3992       else
3993         {
3994           int n_words;
3995           int gregno = cum->sysv_gregno;
3996
3997           /* Aggregates and IEEE quad get passed by reference.  */
3998           if ((type && AGGREGATE_TYPE_P (type))
3999               || mode == TFmode)
4000             n_words = 1;
4001           else 
4002             n_words = RS6000_ARG_SIZE (mode, type);
4003
4004           /* Long long and SPE vectors are put in odd registers.  */
4005           if (n_words == 2 && (gregno & 1) == 0)
4006             gregno += 1;
4007
4008           /* Long long do not split between registers and stack.  */
4009           if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
4010             return gen_rtx_REG (mode, gregno);
4011           else
4012             return NULL;
4013         }
4014     }
4015   else
4016     {
4017       int align = (TARGET_32BIT && (cum->words & 1) != 0
4018                    && function_arg_boundary (mode, type) == 64) ? 1 : 0;
4019       int align_words = cum->words + align;
4020
4021       if (type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4022         return NULL_RTX;
4023
4024       if (USE_FP_FOR_ARG_P (*cum, mode, type))
4025         {
4026           if (! type
4027               || ((cum->nargs_prototype > 0)
4028                   /* IBM AIX extended its linkage convention definition always
4029                      to require FP args after register save area hole on the
4030                      stack.  */
4031                   && (DEFAULT_ABI != ABI_AIX
4032                       || ! TARGET_XL_CALL
4033                       || (align_words < GP_ARG_NUM_REG))))
4034             return gen_rtx_REG (mode, cum->fregno);
4035
4036           return gen_rtx_PARALLEL (mode,
4037             gen_rtvec (2,
4038                        gen_rtx_EXPR_LIST (VOIDmode,
4039                                 ((align_words >= GP_ARG_NUM_REG)
4040                                  ? NULL_RTX
4041                                  : (align_words
4042                                     + RS6000_ARG_SIZE (mode, type)
4043                                     > GP_ARG_NUM_REG
4044                                     /* If this is partially on the stack, then
4045                                        we only include the portion actually
4046                                        in registers here.  */
4047                                     ? gen_rtx_REG (SImode,
4048                                                GP_ARG_MIN_REG + align_words)
4049                                     : gen_rtx_REG (mode,
4050                                                GP_ARG_MIN_REG + align_words))),
4051                                 const0_rtx),
4052                        gen_rtx_EXPR_LIST (VOIDmode,
4053                                 gen_rtx_REG (mode, cum->fregno),
4054                                 const0_rtx)));
4055         }
4056       else if (align_words < GP_ARG_NUM_REG)
4057         return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
4058       else
4059         return NULL_RTX;
4060     }
4061 }
4062 \f
4063 /* For an arg passed partly in registers and partly in memory,
4064    this is the number of registers used.
4065    For args passed entirely in registers or entirely in memory, zero.  */
4066
4067 int
4068 function_arg_partial_nregs (cum, mode, type, named)
4069      CUMULATIVE_ARGS *cum;
4070      enum machine_mode mode;
4071      tree type;
4072      int named ATTRIBUTE_UNUSED;
4073 {
4074   if (DEFAULT_ABI == ABI_V4)
4075     return 0;
4076
4077   if (USE_FP_FOR_ARG_P (*cum, mode, type)
4078       || USE_ALTIVEC_FOR_ARG_P (*cum, mode, type))
4079     {
4080       if (cum->nargs_prototype >= 0)
4081         return 0;
4082     }
4083
4084   if (cum->words < GP_ARG_NUM_REG
4085       && GP_ARG_NUM_REG < (cum->words + RS6000_ARG_SIZE (mode, type)))
4086     {
4087       int ret = GP_ARG_NUM_REG - cum->words;
4088       if (ret && TARGET_DEBUG_ARG)
4089         fprintf (stderr, "function_arg_partial_nregs: %d\n", ret);
4090
4091       return ret;
4092     }
4093
4094   return 0;
4095 }
4096 \f
4097 /* A C expression that indicates when an argument must be passed by
4098    reference.  If nonzero for an argument, a copy of that argument is
4099    made in memory and a pointer to the argument is passed instead of
4100    the argument itself.  The pointer is passed in whatever way is
4101    appropriate for passing a pointer to that type.
4102
4103    Under V.4, structures and unions are passed by reference.
4104
4105    As an extension to all ABIs, variable sized types are passed by
4106    reference.  */
4107
4108 int
4109 function_arg_pass_by_reference (cum, mode, type, named)
4110      CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED;
4111      enum machine_mode mode ATTRIBUTE_UNUSED;
4112      tree type;
4113      int named ATTRIBUTE_UNUSED;
4114 {
4115   if (DEFAULT_ABI == ABI_V4
4116       && ((type && AGGREGATE_TYPE_P (type))
4117           || mode == TFmode))
4118     {
4119       if (TARGET_DEBUG_ARG)
4120         fprintf (stderr, "function_arg_pass_by_reference: aggregate\n");
4121
4122       return 1;
4123     }
4124   return type && int_size_in_bytes (type) <= 0;
4125 }
4126 \f
4127 /* Perform any needed actions needed for a function that is receiving a
4128    variable number of arguments. 
4129
4130    CUM is as above.
4131
4132    MODE and TYPE are the mode and type of the current parameter.
4133
4134    PRETEND_SIZE is a variable that should be set to the amount of stack
4135    that must be pushed by the prolog to pretend that our caller pushed
4136    it.
4137
4138    Normally, this macro will push all remaining incoming registers on the
4139    stack and set PRETEND_SIZE to the length of the registers pushed.  */
4140
4141 void
4142 setup_incoming_varargs (cum, mode, type, pretend_size, no_rtl)
4143      CUMULATIVE_ARGS *cum;
4144      enum machine_mode mode;
4145      tree type;
4146      int *pretend_size ATTRIBUTE_UNUSED;
4147      int no_rtl;
4148
4149 {
4150   CUMULATIVE_ARGS next_cum;
4151   int reg_size = TARGET_32BIT ? 4 : 8;
4152   rtx save_area = NULL_RTX, mem;
4153   int first_reg_offset, set;
4154   tree fntype;
4155   int stdarg_p;
4156
4157   fntype = TREE_TYPE (current_function_decl);
4158   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
4159               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4160                   != void_type_node));
4161
4162   /* For varargs, we do not want to skip the dummy va_dcl argument.
4163      For stdargs, we do want to skip the last named argument.  */
4164   next_cum = *cum;
4165   if (stdarg_p)
4166     function_arg_advance (&next_cum, mode, type, 1);
4167
4168   if (DEFAULT_ABI == ABI_V4)
4169     {
4170       /* Indicate to allocate space on the stack for varargs save area.  */
4171       cfun->machine->sysv_varargs_p = 1;
4172       if (! no_rtl)
4173         save_area = plus_constant (virtual_stack_vars_rtx,
4174                                    - RS6000_VARARGS_SIZE);
4175
4176       first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
4177     }
4178   else
4179     {
4180       first_reg_offset = next_cum.words;
4181       save_area = virtual_incoming_args_rtx;
4182       cfun->machine->sysv_varargs_p = 0;
4183
4184       if (MUST_PASS_IN_STACK (mode, type))
4185         first_reg_offset += RS6000_ARG_SIZE (TYPE_MODE (type), type);
4186     }
4187
4188   set = get_varargs_alias_set ();
4189   if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG)
4190     {
4191       mem = gen_rtx_MEM (BLKmode,
4192                          plus_constant (save_area,
4193                                         first_reg_offset * reg_size)),
4194       set_mem_alias_set (mem, set);
4195       set_mem_align (mem, BITS_PER_WORD);
4196
4197       move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
4198                            GP_ARG_NUM_REG - first_reg_offset);
4199     }
4200
4201   /* Save FP registers if needed.  */
4202   if (DEFAULT_ABI == ABI_V4
4203       && TARGET_HARD_FLOAT && TARGET_FPRS
4204       && ! no_rtl
4205       && next_cum.fregno <= FP_ARG_V4_MAX_REG)
4206     {
4207       int fregno = next_cum.fregno;
4208       rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
4209       rtx lab = gen_label_rtx ();
4210       int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG) * 8);
4211
4212       emit_jump_insn (gen_rtx_SET (VOIDmode,
4213                                    pc_rtx,
4214                                    gen_rtx_IF_THEN_ELSE (VOIDmode,
4215                                             gen_rtx_NE (VOIDmode, cr1,
4216                                                         const0_rtx),
4217                                             gen_rtx_LABEL_REF (VOIDmode, lab),
4218                                             pc_rtx)));
4219
4220       while (fregno <= FP_ARG_V4_MAX_REG)
4221         {
4222           mem = gen_rtx_MEM (DFmode, plus_constant (save_area, off));
4223           set_mem_alias_set (mem, set);
4224           emit_move_insn (mem, gen_rtx_REG (DFmode, fregno));
4225           fregno++;
4226           off += 8;
4227         }
4228
4229       emit_label (lab);
4230     }
4231 }
4232
4233 /* Create the va_list data type.  */
4234
4235 tree
4236 rs6000_build_va_list ()
4237 {
4238   tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
4239
4240   /* For AIX, prefer 'char *' because that's what the system
4241      header files like.  */
4242   if (DEFAULT_ABI != ABI_V4)
4243     return build_pointer_type (char_type_node);
4244
4245   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
4246   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
4247
4248   f_gpr = build_decl (FIELD_DECL, get_identifier ("gpr"), 
4249                       unsigned_char_type_node);
4250   f_fpr = build_decl (FIELD_DECL, get_identifier ("fpr"), 
4251                       unsigned_char_type_node);
4252   /* Give the two bytes of padding a name, so that -Wpadded won't warn on
4253      every user file.  */
4254   f_res = build_decl (FIELD_DECL, get_identifier ("reserved"),
4255                       short_unsigned_type_node);
4256   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
4257                       ptr_type_node);
4258   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
4259                       ptr_type_node);
4260
4261   DECL_FIELD_CONTEXT (f_gpr) = record;
4262   DECL_FIELD_CONTEXT (f_fpr) = record;
4263   DECL_FIELD_CONTEXT (f_res) = record;
4264   DECL_FIELD_CONTEXT (f_ovf) = record;
4265   DECL_FIELD_CONTEXT (f_sav) = record;
4266
4267   TREE_CHAIN (record) = type_decl;
4268   TYPE_NAME (record) = type_decl;
4269   TYPE_FIELDS (record) = f_gpr;
4270   TREE_CHAIN (f_gpr) = f_fpr;
4271   TREE_CHAIN (f_fpr) = f_res;
4272   TREE_CHAIN (f_res) = f_ovf;
4273   TREE_CHAIN (f_ovf) = f_sav;
4274
4275   layout_type (record);
4276
4277   /* The correct type is an array type of one element.  */
4278   return build_array_type (record, build_index_type (size_zero_node));
4279 }
4280
4281 /* Implement va_start.  */
4282
4283 void
4284 rs6000_va_start (valist, nextarg)
4285      tree valist;
4286      rtx nextarg;
4287 {
4288   HOST_WIDE_INT words, n_gpr, n_fpr;
4289   tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
4290   tree gpr, fpr, ovf, sav, t;
4291
4292   /* Only SVR4 needs something special.  */
4293   if (DEFAULT_ABI != ABI_V4)
4294     {
4295       std_expand_builtin_va_start (valist, nextarg);
4296       return;
4297     }
4298
4299   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4300   f_fpr = TREE_CHAIN (f_gpr);
4301   f_res = TREE_CHAIN (f_fpr);
4302   f_ovf = TREE_CHAIN (f_res);
4303   f_sav = TREE_CHAIN (f_ovf);
4304
4305   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
4306   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
4307   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
4308   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
4309   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
4310
4311   /* Count number of gp and fp argument registers used.  */
4312   words = current_function_args_info.words;
4313   n_gpr = current_function_args_info.sysv_gregno - GP_ARG_MIN_REG;
4314   n_fpr = current_function_args_info.fregno - FP_ARG_MIN_REG;
4315
4316   if (TARGET_DEBUG_ARG)
4317     fprintf (stderr, "va_start: words = "HOST_WIDE_INT_PRINT_DEC", n_gpr = "
4318              HOST_WIDE_INT_PRINT_DEC", n_fpr = "HOST_WIDE_INT_PRINT_DEC"\n",
4319              words, n_gpr, n_fpr);
4320
4321   t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, build_int_2 (n_gpr, 0));
4322   TREE_SIDE_EFFECTS (t) = 1;
4323   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4324
4325   t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr, build_int_2 (n_fpr, 0));
4326   TREE_SIDE_EFFECTS (t) = 1;
4327   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4328
4329   /* Find the overflow area.  */
4330   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
4331   if (words != 0)
4332     t = build (PLUS_EXPR, TREE_TYPE (ovf), t,
4333                build_int_2 (words * UNITS_PER_WORD, 0));
4334   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
4335   TREE_SIDE_EFFECTS (t) = 1;
4336   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4337
4338   /* Find the register save area.  */
4339   t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
4340   t = build (PLUS_EXPR, TREE_TYPE (sav), t,
4341              build_int_2 (-RS6000_VARARGS_SIZE, -1));
4342   t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
4343   TREE_SIDE_EFFECTS (t) = 1;
4344   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4345 }
4346
4347 /* Implement va_arg.  */
4348
4349 rtx
4350 rs6000_va_arg (valist, type)
4351      tree valist, type;
4352 {
4353   tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
4354   tree gpr, fpr, ovf, sav, reg, t, u;
4355   int indirect_p, size, rsize, n_reg, sav_ofs, sav_scale;
4356   rtx lab_false, lab_over, addr_rtx, r;
4357
4358   if (DEFAULT_ABI != ABI_V4)
4359     {
4360       /* Variable sized types are passed by reference.  */
4361       if (int_size_in_bytes (type) <= 0)
4362         {
4363           u = build_pointer_type (type);
4364
4365           /* Args grow upward.  */
4366           t = build (POSTINCREMENT_EXPR, TREE_TYPE (valist), valist,
4367                      build_int_2 (POINTER_SIZE / BITS_PER_UNIT, 0));
4368           TREE_SIDE_EFFECTS (t) = 1;
4369
4370           t = build1 (NOP_EXPR, build_pointer_type (u), t);
4371           TREE_SIDE_EFFECTS (t) = 1;
4372
4373           t = build1 (INDIRECT_REF, u, t);
4374           TREE_SIDE_EFFECTS (t) = 1;
4375
4376           return expand_expr (t, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4377         }
4378       else
4379         return std_expand_builtin_va_arg (valist, type);
4380     }
4381
4382   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4383   f_fpr = TREE_CHAIN (f_gpr);
4384   f_res = TREE_CHAIN (f_fpr);
4385   f_ovf = TREE_CHAIN (f_res);
4386   f_sav = TREE_CHAIN (f_ovf);
4387
4388   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
4389   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
4390   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
4391   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
4392   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
4393
4394   size = int_size_in_bytes (type);
4395   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4396
4397   if (AGGREGATE_TYPE_P (type) || TYPE_MODE (type) == TFmode)
4398     {
4399       /* Aggregates and long doubles are passed by reference.  */
4400       indirect_p = 1;
4401       reg = gpr;
4402       n_reg = 1;
4403       sav_ofs = 0;
4404       sav_scale = 4;
4405       size = UNITS_PER_WORD;
4406       rsize = 1;
4407     }
4408   else if (FLOAT_TYPE_P (type) && TARGET_HARD_FLOAT && TARGET_FPRS)
4409     {
4410       /* FP args go in FP registers, if present.  */
4411       indirect_p = 0;
4412       reg = fpr;
4413       n_reg = 1;
4414       sav_ofs = 8*4;
4415       sav_scale = 8;
4416     }
4417   else
4418     {
4419       /* Otherwise into GP registers.  */
4420       indirect_p = 0;
4421       reg = gpr;
4422       n_reg = rsize;
4423       sav_ofs = 0;
4424       sav_scale = 4;
4425     }
4426
4427   /* Pull the value out of the saved registers ...  */
4428
4429   lab_false = gen_label_rtx ();
4430   lab_over = gen_label_rtx ();
4431   addr_rtx = gen_reg_rtx (Pmode);
4432
4433   /*  AltiVec vectors never go in registers.  */
4434   if (!TARGET_ALTIVEC || TREE_CODE (type) != VECTOR_TYPE)
4435     {
4436       TREE_THIS_VOLATILE (reg) = 1;
4437       emit_cmp_and_jump_insns
4438         (expand_expr (reg, NULL_RTX, QImode, EXPAND_NORMAL),
4439          GEN_INT (8 - n_reg + 1), GE, const1_rtx, QImode, 1,
4440          lab_false);
4441
4442       /* Long long is aligned in the registers.  */
4443       if (n_reg > 1)
4444         {
4445           u = build (BIT_AND_EXPR, TREE_TYPE (reg), reg,
4446                      build_int_2 (n_reg - 1, 0));
4447           u = build (PLUS_EXPR, TREE_TYPE (reg), reg, u);
4448           u = build (MODIFY_EXPR, TREE_TYPE (reg), reg, u);
4449           TREE_SIDE_EFFECTS (u) = 1;
4450           expand_expr (u, const0_rtx, VOIDmode, EXPAND_NORMAL);
4451         }
4452
4453       if (sav_ofs)
4454         t = build (PLUS_EXPR, ptr_type_node, sav, build_int_2 (sav_ofs, 0));
4455       else
4456         t = sav;
4457
4458       u = build (POSTINCREMENT_EXPR, TREE_TYPE (reg), reg,
4459                  build_int_2 (n_reg, 0));
4460       TREE_SIDE_EFFECTS (u) = 1;
4461
4462       u = build1 (CONVERT_EXPR, integer_type_node, u);
4463       TREE_SIDE_EFFECTS (u) = 1;
4464
4465       u = build (MULT_EXPR, integer_type_node, u, build_int_2 (sav_scale, 0));
4466       TREE_SIDE_EFFECTS (u) = 1;
4467
4468       t = build (PLUS_EXPR, ptr_type_node, t, u);
4469       TREE_SIDE_EFFECTS (t) = 1;
4470
4471       r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4472       if (r != addr_rtx)
4473         emit_move_insn (addr_rtx, r);
4474
4475       emit_jump_insn (gen_jump (lab_over));
4476       emit_barrier ();
4477     }
4478
4479   emit_label (lab_false);
4480
4481   /* ... otherwise out of the overflow area.  */
4482
4483   /* Make sure we don't find reg 7 for the next int arg.
4484
4485      All AltiVec vectors go in the overflow area.  So in the AltiVec
4486      case we need to get the vectors from the overflow area, but
4487      remember where the GPRs and FPRs are.  */
4488   if (n_reg > 1 && (TREE_CODE (type) != VECTOR_TYPE
4489                     || !TARGET_ALTIVEC))
4490     {
4491       t = build (MODIFY_EXPR, TREE_TYPE (reg), reg, build_int_2 (8, 0));
4492       TREE_SIDE_EFFECTS (t) = 1;
4493       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4494     }
4495
4496   /* Care for on-stack alignment if needed.  */
4497   if (rsize <= 1)
4498     t = ovf;
4499   else
4500     {
4501       int align;
4502
4503       /* AltiVec vectors are 16 byte aligned.  */
4504       if (TARGET_ALTIVEC && TREE_CODE (type) == VECTOR_TYPE)
4505         align = 15;
4506       else
4507         align = 7;
4508
4509       t = build (PLUS_EXPR, TREE_TYPE (ovf), ovf, build_int_2 (align, 0));
4510       t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-align-1, -1));
4511     }
4512   t = save_expr (t);
4513
4514   r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4515   if (r != addr_rtx)
4516     emit_move_insn (addr_rtx, r);
4517
4518   t = build (PLUS_EXPR, TREE_TYPE (t), t, build_int_2 (size, 0));
4519   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
4520   TREE_SIDE_EFFECTS (t) = 1;
4521   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4522
4523   emit_label (lab_over);
4524
4525   if (indirect_p)
4526     {
4527       r = gen_rtx_MEM (Pmode, addr_rtx);
4528       set_mem_alias_set (r, get_varargs_alias_set ());
4529       emit_move_insn (addr_rtx, r);
4530     }
4531
4532   return addr_rtx;
4533 }
4534
4535 /* Builtins.  */
4536
4537 #define def_builtin(MASK, NAME, TYPE, CODE)                     \
4538 do {                                                            \
4539   if ((MASK) & target_flags)                                    \
4540     builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD,      \
4541                       NULL, NULL_TREE);                         \
4542 } while (0)
4543
4544 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc).  */
4545
4546 static const struct builtin_description bdesc_3arg[] =
4547 {
4548   { MASK_ALTIVEC, CODE_FOR_altivec_vmaddfp, "__builtin_altivec_vmaddfp", ALTIVEC_BUILTIN_VMADDFP },
4549   { MASK_ALTIVEC, CODE_FOR_altivec_vmhaddshs, "__builtin_altivec_vmhaddshs", ALTIVEC_BUILTIN_VMHADDSHS },
4550   { MASK_ALTIVEC, CODE_FOR_altivec_vmhraddshs, "__builtin_altivec_vmhraddshs", ALTIVEC_BUILTIN_VMHRADDSHS },
4551   { MASK_ALTIVEC, CODE_FOR_altivec_vmladduhm, "__builtin_altivec_vmladduhm", ALTIVEC_BUILTIN_VMLADDUHM},
4552   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumubm, "__builtin_altivec_vmsumubm", ALTIVEC_BUILTIN_VMSUMUBM },
4553   { MASK_ALTIVEC, CODE_FOR_altivec_vmsummbm, "__builtin_altivec_vmsummbm", ALTIVEC_BUILTIN_VMSUMMBM },
4554   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhm, "__builtin_altivec_vmsumuhm", ALTIVEC_BUILTIN_VMSUMUHM },
4555   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshm, "__builtin_altivec_vmsumshm", ALTIVEC_BUILTIN_VMSUMSHM },
4556   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhs, "__builtin_altivec_vmsumuhs", ALTIVEC_BUILTIN_VMSUMUHS },
4557   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshs, "__builtin_altivec_vmsumshs", ALTIVEC_BUILTIN_VMSUMSHS },
4558   { MASK_ALTIVEC, CODE_FOR_altivec_vnmsubfp, "__builtin_altivec_vnmsubfp", ALTIVEC_BUILTIN_VNMSUBFP }, 
4559   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4sf, "__builtin_altivec_vperm_4sf", ALTIVEC_BUILTIN_VPERM_4SF },
4560   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4si, "__builtin_altivec_vperm_4si", ALTIVEC_BUILTIN_VPERM_4SI },
4561   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_8hi, "__builtin_altivec_vperm_8hi", ALTIVEC_BUILTIN_VPERM_8HI },
4562   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_16qi, "__builtin_altivec_vperm_16qi", ALTIVEC_BUILTIN_VPERM_16QI },
4563   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4sf, "__builtin_altivec_vsel_4sf", ALTIVEC_BUILTIN_VSEL_4SF },
4564   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4si, "__builtin_altivec_vsel_4si", ALTIVEC_BUILTIN_VSEL_4SI },
4565   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_8hi, "__builtin_altivec_vsel_8hi", ALTIVEC_BUILTIN_VSEL_8HI },
4566   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_16qi, "__builtin_altivec_vsel_16qi", ALTIVEC_BUILTIN_VSEL_16QI },
4567   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_16qi, "__builtin_altivec_vsldoi_16qi", ALTIVEC_BUILTIN_VSLDOI_16QI },
4568   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_8hi, "__builtin_altivec_vsldoi_8hi", ALTIVEC_BUILTIN_VSLDOI_8HI },
4569   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4si, "__builtin_altivec_vsldoi_4si", ALTIVEC_BUILTIN_VSLDOI_4SI },
4570   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4sf, "__builtin_altivec_vsldoi_4sf", ALTIVEC_BUILTIN_VSLDOI_4SF },
4571 };
4572
4573 /* DST operations: void foo (void *, const int, const char).  */
4574
4575 static const struct builtin_description bdesc_dst[] =
4576 {
4577   { MASK_ALTIVEC, CODE_FOR_altivec_dst, "__builtin_altivec_dst", ALTIVEC_BUILTIN_DST },
4578   { MASK_ALTIVEC, CODE_FOR_altivec_dstt, "__builtin_altivec_dstt", ALTIVEC_BUILTIN_DSTT },
4579   { MASK_ALTIVEC, CODE_FOR_altivec_dstst, "__builtin_altivec_dstst", ALTIVEC_BUILTIN_DSTST },
4580   { MASK_ALTIVEC, CODE_FOR_altivec_dststt, "__builtin_altivec_dststt", ALTIVEC_BUILTIN_DSTSTT }
4581 };
4582
4583 /* Simple binary operations: VECc = foo (VECa, VECb).  */
4584
4585 static struct builtin_description bdesc_2arg[] =
4586 {
4587   { MASK_ALTIVEC, CODE_FOR_addv16qi3, "__builtin_altivec_vaddubm", ALTIVEC_BUILTIN_VADDUBM },
4588   { MASK_ALTIVEC, CODE_FOR_addv8hi3, "__builtin_altivec_vadduhm", ALTIVEC_BUILTIN_VADDUHM },
4589   { MASK_ALTIVEC, CODE_FOR_addv4si3, "__builtin_altivec_vadduwm", ALTIVEC_BUILTIN_VADDUWM },
4590   { MASK_ALTIVEC, CODE_FOR_addv4sf3, "__builtin_altivec_vaddfp", ALTIVEC_BUILTIN_VADDFP },
4591   { MASK_ALTIVEC, CODE_FOR_altivec_vaddcuw, "__builtin_altivec_vaddcuw", ALTIVEC_BUILTIN_VADDCUW },
4592   { MASK_ALTIVEC, CODE_FOR_altivec_vaddubs, "__builtin_altivec_vaddubs", ALTIVEC_BUILTIN_VADDUBS },
4593   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsbs, "__builtin_altivec_vaddsbs", ALTIVEC_BUILTIN_VADDSBS },
4594   { MASK_ALTIVEC, CODE_FOR_altivec_vadduhs, "__builtin_altivec_vadduhs", ALTIVEC_BUILTIN_VADDUHS },
4595   { MASK_ALTIVEC, CODE_FOR_altivec_vaddshs, "__builtin_altivec_vaddshs", ALTIVEC_BUILTIN_VADDSHS },
4596   { MASK_ALTIVEC, CODE_FOR_altivec_vadduws, "__builtin_altivec_vadduws", ALTIVEC_BUILTIN_VADDUWS },
4597   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsws, "__builtin_altivec_vaddsws", ALTIVEC_BUILTIN_VADDSWS },
4598   { MASK_ALTIVEC, CODE_FOR_andv4si3, "__builtin_altivec_vand", ALTIVEC_BUILTIN_VAND },
4599   { MASK_ALTIVEC, CODE_FOR_altivec_vandc, "__builtin_altivec_vandc", ALTIVEC_BUILTIN_VANDC },
4600   { MASK_ALTIVEC, CODE_FOR_altivec_vavgub, "__builtin_altivec_vavgub", ALTIVEC_BUILTIN_VAVGUB },
4601   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsb, "__builtin_altivec_vavgsb", ALTIVEC_BUILTIN_VAVGSB },
4602   { MASK_ALTIVEC, CODE_FOR_altivec_vavguh, "__builtin_altivec_vavguh", ALTIVEC_BUILTIN_VAVGUH },
4603   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsh, "__builtin_altivec_vavgsh", ALTIVEC_BUILTIN_VAVGSH },
4604   { MASK_ALTIVEC, CODE_FOR_altivec_vavguw, "__builtin_altivec_vavguw", ALTIVEC_BUILTIN_VAVGUW },
4605   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsw, "__builtin_altivec_vavgsw", ALTIVEC_BUILTIN_VAVGSW },
4606   { MASK_ALTIVEC, CODE_FOR_altivec_vcfux, "__builtin_altivec_vcfux", ALTIVEC_BUILTIN_VCFUX },
4607   { MASK_ALTIVEC, CODE_FOR_altivec_vcfsx, "__builtin_altivec_vcfsx", ALTIVEC_BUILTIN_VCFSX },
4608   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpbfp, "__builtin_altivec_vcmpbfp", ALTIVEC_BUILTIN_VCMPBFP },
4609   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequb, "__builtin_altivec_vcmpequb", ALTIVEC_BUILTIN_VCMPEQUB },
4610   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequh, "__builtin_altivec_vcmpequh", ALTIVEC_BUILTIN_VCMPEQUH },
4611   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequw, "__builtin_altivec_vcmpequw", ALTIVEC_BUILTIN_VCMPEQUW },
4612   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpeqfp, "__builtin_altivec_vcmpeqfp", ALTIVEC_BUILTIN_VCMPEQFP },
4613   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgefp, "__builtin_altivec_vcmpgefp", ALTIVEC_BUILTIN_VCMPGEFP },
4614   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtub, "__builtin_altivec_vcmpgtub", ALTIVEC_BUILTIN_VCMPGTUB },
4615   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsb, "__builtin_altivec_vcmpgtsb", ALTIVEC_BUILTIN_VCMPGTSB },
4616   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuh, "__builtin_altivec_vcmpgtuh", ALTIVEC_BUILTIN_VCMPGTUH },
4617   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsh, "__builtin_altivec_vcmpgtsh", ALTIVEC_BUILTIN_VCMPGTSH },
4618   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuw, "__builtin_altivec_vcmpgtuw", ALTIVEC_BUILTIN_VCMPGTUW },
4619   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsw, "__builtin_altivec_vcmpgtsw", ALTIVEC_BUILTIN_VCMPGTSW },
4620   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtfp, "__builtin_altivec_vcmpgtfp", ALTIVEC_BUILTIN_VCMPGTFP },
4621   { MASK_ALTIVEC, CODE_FOR_altivec_vctsxs, "__builtin_altivec_vctsxs", ALTIVEC_BUILTIN_VCTSXS },
4622   { MASK_ALTIVEC, CODE_FOR_altivec_vctuxs, "__builtin_altivec_vctuxs", ALTIVEC_BUILTIN_VCTUXS },
4623   { MASK_ALTIVEC, CODE_FOR_umaxv16qi3, "__builtin_altivec_vmaxub", ALTIVEC_BUILTIN_VMAXUB },
4624   { MASK_ALTIVEC, CODE_FOR_smaxv16qi3, "__builtin_altivec_vmaxsb", ALTIVEC_BUILTIN_VMAXSB },
4625   { MASK_ALTIVEC, CODE_FOR_umaxv8hi3, "__builtin_altivec_vmaxuh", ALTIVEC_BUILTIN_VMAXUH },
4626   { MASK_ALTIVEC, CODE_FOR_smaxv8hi3, "__builtin_altivec_vmaxsh", ALTIVEC_BUILTIN_VMAXSH },
4627   { MASK_ALTIVEC, CODE_FOR_umaxv4si3, "__builtin_altivec_vmaxuw", ALTIVEC_BUILTIN_VMAXUW },
4628   { MASK_ALTIVEC, CODE_FOR_smaxv4si3, "__builtin_altivec_vmaxsw", ALTIVEC_BUILTIN_VMAXSW },
4629   { MASK_ALTIVEC, CODE_FOR_smaxv4sf3, "__builtin_altivec_vmaxfp", ALTIVEC_BUILTIN_VMAXFP },
4630   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghb, "__builtin_altivec_vmrghb", ALTIVEC_BUILTIN_VMRGHB },
4631   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghh, "__builtin_altivec_vmrghh", ALTIVEC_BUILTIN_VMRGHH },
4632   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghw, "__builtin_altivec_vmrghw", ALTIVEC_BUILTIN_VMRGHW },
4633   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglb, "__builtin_altivec_vmrglb", ALTIVEC_BUILTIN_VMRGLB },
4634   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglh, "__builtin_altivec_vmrglh", ALTIVEC_BUILTIN_VMRGLH },
4635   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglw, "__builtin_altivec_vmrglw", ALTIVEC_BUILTIN_VMRGLW },
4636   { MASK_ALTIVEC, CODE_FOR_uminv16qi3, "__builtin_altivec_vminub", ALTIVEC_BUILTIN_VMINUB },
4637   { MASK_ALTIVEC, CODE_FOR_sminv16qi3, "__builtin_altivec_vminsb", ALTIVEC_BUILTIN_VMINSB },
4638   { MASK_ALTIVEC, CODE_FOR_uminv8hi3, "__builtin_altivec_vminuh", ALTIVEC_BUILTIN_VMINUH },
4639   { MASK_ALTIVEC, CODE_FOR_sminv8hi3, "__builtin_altivec_vminsh", ALTIVEC_BUILTIN_VMINSH },
4640   { MASK_ALTIVEC, CODE_FOR_uminv4si3, "__builtin_altivec_vminuw", ALTIVEC_BUILTIN_VMINUW },
4641   { MASK_ALTIVEC, CODE_FOR_sminv4si3, "__builtin_altivec_vminsw", ALTIVEC_BUILTIN_VMINSW },
4642   { MASK_ALTIVEC, CODE_FOR_sminv4sf3, "__builtin_altivec_vminfp", ALTIVEC_BUILTIN_VMINFP },
4643   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleub, "__builtin_altivec_vmuleub", ALTIVEC_BUILTIN_VMULEUB },
4644   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesb, "__builtin_altivec_vmulesb", ALTIVEC_BUILTIN_VMULESB },
4645   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleuh, "__builtin_altivec_vmuleuh", ALTIVEC_BUILTIN_VMULEUH },
4646   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesh, "__builtin_altivec_vmulesh", ALTIVEC_BUILTIN_VMULESH },
4647   { MASK_ALTIVEC, CODE_FOR_altivec_vmuloub, "__builtin_altivec_vmuloub", ALTIVEC_BUILTIN_VMULOUB },
4648   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosb, "__builtin_altivec_vmulosb", ALTIVEC_BUILTIN_VMULOSB },
4649   { MASK_ALTIVEC, CODE_FOR_altivec_vmulouh, "__builtin_altivec_vmulouh", ALTIVEC_BUILTIN_VMULOUH },
4650   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosh, "__builtin_altivec_vmulosh", ALTIVEC_BUILTIN_VMULOSH },
4651   { MASK_ALTIVEC, CODE_FOR_altivec_vnor, "__builtin_altivec_vnor", ALTIVEC_BUILTIN_VNOR },
4652   { MASK_ALTIVEC, CODE_FOR_iorv4si3, "__builtin_altivec_vor", ALTIVEC_BUILTIN_VOR },
4653   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum, "__builtin_altivec_vpkuhum", ALTIVEC_BUILTIN_VPKUHUM },
4654   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum, "__builtin_altivec_vpkuwum", ALTIVEC_BUILTIN_VPKUWUM },
4655   { MASK_ALTIVEC, CODE_FOR_altivec_vpkpx, "__builtin_altivec_vpkpx", ALTIVEC_BUILTIN_VPKPX },
4656   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhss, "__builtin_altivec_vpkuhss", ALTIVEC_BUILTIN_VPKUHSS },
4657   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshss, "__builtin_altivec_vpkshss", ALTIVEC_BUILTIN_VPKSHSS },
4658   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwss, "__builtin_altivec_vpkuwss", ALTIVEC_BUILTIN_VPKUWSS },
4659   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswss, "__builtin_altivec_vpkswss", ALTIVEC_BUILTIN_VPKSWSS },
4660   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhus, "__builtin_altivec_vpkuhus", ALTIVEC_BUILTIN_VPKUHUS },
4661   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshus, "__builtin_altivec_vpkshus", ALTIVEC_BUILTIN_VPKSHUS },
4662   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwus, "__builtin_altivec_vpkuwus", ALTIVEC_BUILTIN_VPKUWUS },
4663   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswus, "__builtin_altivec_vpkswus", ALTIVEC_BUILTIN_VPKSWUS },
4664   { MASK_ALTIVEC, CODE_FOR_altivec_vrlb, "__builtin_altivec_vrlb", ALTIVEC_BUILTIN_VRLB },
4665   { MASK_ALTIVEC, CODE_FOR_altivec_vrlh, "__builtin_altivec_vrlh", ALTIVEC_BUILTIN_VRLH },
4666   { MASK_ALTIVEC, CODE_FOR_altivec_vrlw, "__builtin_altivec_vrlw", ALTIVEC_BUILTIN_VRLW },
4667   { MASK_ALTIVEC, CODE_FOR_altivec_vslb, "__builtin_altivec_vslb", ALTIVEC_BUILTIN_VSLB },
4668   { MASK_ALTIVEC, CODE_FOR_altivec_vslh, "__builtin_altivec_vslh", ALTIVEC_BUILTIN_VSLH },
4669   { MASK_ALTIVEC, CODE_FOR_altivec_vslw, "__builtin_altivec_vslw", ALTIVEC_BUILTIN_VSLW },
4670   { MASK_ALTIVEC, CODE_FOR_altivec_vsl, "__builtin_altivec_vsl", ALTIVEC_BUILTIN_VSL },
4671   { MASK_ALTIVEC, CODE_FOR_altivec_vslo, "__builtin_altivec_vslo", ALTIVEC_BUILTIN_VSLO },
4672   { MASK_ALTIVEC, CODE_FOR_altivec_vspltb, "__builtin_altivec_vspltb", ALTIVEC_BUILTIN_VSPLTB },
4673   { MASK_ALTIVEC, CODE_FOR_altivec_vsplth, "__builtin_altivec_vsplth", ALTIVEC_BUILTIN_VSPLTH },
4674   { MASK_ALTIVEC, CODE_FOR_altivec_vspltw, "__builtin_altivec_vspltw", ALTIVEC_BUILTIN_VSPLTW },
4675   { MASK_ALTIVEC, CODE_FOR_altivec_vsrb, "__builtin_altivec_vsrb", ALTIVEC_BUILTIN_VSRB },
4676   { MASK_ALTIVEC, CODE_FOR_altivec_vsrh, "__builtin_altivec_vsrh", ALTIVEC_BUILTIN_VSRH },
4677   { MASK_ALTIVEC, CODE_FOR_altivec_vsrw, "__builtin_altivec_vsrw", ALTIVEC_BUILTIN_VSRW },
4678   { MASK_ALTIVEC, CODE_FOR_altivec_vsrab, "__builtin_altivec_vsrab", ALTIVEC_BUILTIN_VSRAB },
4679   { MASK_ALTIVEC, CODE_FOR_altivec_vsrah, "__builtin_altivec_vsrah", ALTIVEC_BUILTIN_VSRAH },
4680   { MASK_ALTIVEC, CODE_FOR_altivec_vsraw, "__builtin_altivec_vsraw", ALTIVEC_BUILTIN_VSRAW },
4681   { MASK_ALTIVEC, CODE_FOR_altivec_vsr, "__builtin_altivec_vsr", ALTIVEC_BUILTIN_VSR },
4682   { MASK_ALTIVEC, CODE_FOR_altivec_vsro, "__builtin_altivec_vsro", ALTIVEC_BUILTIN_VSRO },
4683   { MASK_ALTIVEC, CODE_FOR_subv16qi3, "__builtin_altivec_vsububm", ALTIVEC_BUILTIN_VSUBUBM },
4684   { MASK_ALTIVEC, CODE_FOR_subv8hi3, "__builtin_altivec_vsubuhm", ALTIVEC_BUILTIN_VSUBUHM },
4685   { MASK_ALTIVEC, CODE_FOR_subv4si3, "__builtin_altivec_vsubuwm", ALTIVEC_BUILTIN_VSUBUWM },
4686   { MASK_ALTIVEC, CODE_FOR_subv4sf3, "__builtin_altivec_vsubfp", ALTIVEC_BUILTIN_VSUBFP },
4687   { MASK_ALTIVEC, CODE_FOR_altivec_vsubcuw, "__builtin_altivec_vsubcuw", ALTIVEC_BUILTIN_VSUBCUW },
4688   { MASK_ALTIVEC, CODE_FOR_altivec_vsububs, "__builtin_altivec_vsububs", ALTIVEC_BUILTIN_VSUBUBS },
4689   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsbs, "__builtin_altivec_vsubsbs", ALTIVEC_BUILTIN_VSUBSBS },
4690   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuhs, "__builtin_altivec_vsubuhs", ALTIVEC_BUILTIN_VSUBUHS },
4691   { MASK_ALTIVEC, CODE_FOR_altivec_vsubshs, "__builtin_altivec_vsubshs", ALTIVEC_BUILTIN_VSUBSHS },
4692   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuws, "__builtin_altivec_vsubuws", ALTIVEC_BUILTIN_VSUBUWS },
4693   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsws, "__builtin_altivec_vsubsws", ALTIVEC_BUILTIN_VSUBSWS },
4694   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4ubs, "__builtin_altivec_vsum4ubs", ALTIVEC_BUILTIN_VSUM4UBS },
4695   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4sbs, "__builtin_altivec_vsum4sbs", ALTIVEC_BUILTIN_VSUM4SBS },
4696   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4shs, "__builtin_altivec_vsum4shs", ALTIVEC_BUILTIN_VSUM4SHS },
4697   { MASK_ALTIVEC, CODE_FOR_altivec_vsum2sws, "__builtin_altivec_vsum2sws", ALTIVEC_BUILTIN_VSUM2SWS },
4698   { MASK_ALTIVEC, CODE_FOR_altivec_vsumsws, "__builtin_altivec_vsumsws", ALTIVEC_BUILTIN_VSUMSWS },
4699   { MASK_ALTIVEC, CODE_FOR_xorv4si3, "__builtin_altivec_vxor", ALTIVEC_BUILTIN_VXOR },
4700
4701   /* Place holder, leave as first spe builtin.  */
4702   { 0, CODE_FOR_spe_evaddw, "__builtin_spe_evaddw", SPE_BUILTIN_EVADDW },
4703   { 0, CODE_FOR_spe_evand, "__builtin_spe_evand", SPE_BUILTIN_EVAND },
4704   { 0, CODE_FOR_spe_evandc, "__builtin_spe_evandc", SPE_BUILTIN_EVANDC },
4705   { 0, CODE_FOR_spe_evdivws, "__builtin_spe_evdivws", SPE_BUILTIN_EVDIVWS },
4706   { 0, CODE_FOR_spe_evdivwu, "__builtin_spe_evdivwu", SPE_BUILTIN_EVDIVWU },
4707   { 0, CODE_FOR_spe_eveqv, "__builtin_spe_eveqv", SPE_BUILTIN_EVEQV },
4708   { 0, CODE_FOR_spe_evfsadd, "__builtin_spe_evfsadd", SPE_BUILTIN_EVFSADD },
4709   { 0, CODE_FOR_spe_evfsdiv, "__builtin_spe_evfsdiv", SPE_BUILTIN_EVFSDIV },
4710   { 0, CODE_FOR_spe_evfsmul, "__builtin_spe_evfsmul", SPE_BUILTIN_EVFSMUL },
4711   { 0, CODE_FOR_spe_evfssub, "__builtin_spe_evfssub", SPE_BUILTIN_EVFSSUB },
4712   { 0, CODE_FOR_spe_evmergehi, "__builtin_spe_evmergehi", SPE_BUILTIN_EVMERGEHI },
4713   { 0, CODE_FOR_spe_evmergehilo, "__builtin_spe_evmergehilo", SPE_BUILTIN_EVMERGEHILO },
4714   { 0, CODE_FOR_spe_evmergelo, "__builtin_spe_evmergelo", SPE_BUILTIN_EVMERGELO },
4715   { 0, CODE_FOR_spe_evmergelohi, "__builtin_spe_evmergelohi", SPE_BUILTIN_EVMERGELOHI },
4716   { 0, CODE_FOR_spe_evmhegsmfaa, "__builtin_spe_evmhegsmfaa", SPE_BUILTIN_EVMHEGSMFAA },
4717   { 0, CODE_FOR_spe_evmhegsmfan, "__builtin_spe_evmhegsmfan", SPE_BUILTIN_EVMHEGSMFAN },
4718   { 0, CODE_FOR_spe_evmhegsmiaa, "__builtin_spe_evmhegsmiaa", SPE_BUILTIN_EVMHEGSMIAA },
4719   { 0, CODE_FOR_spe_evmhegsmian, "__builtin_spe_evmhegsmian", SPE_BUILTIN_EVMHEGSMIAN },
4720   { 0, CODE_FOR_spe_evmhegumiaa, "__builtin_spe_evmhegumiaa", SPE_BUILTIN_EVMHEGUMIAA },
4721   { 0, CODE_FOR_spe_evmhegumian, "__builtin_spe_evmhegumian", SPE_BUILTIN_EVMHEGUMIAN },
4722   { 0, CODE_FOR_spe_evmhesmf, "__builtin_spe_evmhesmf", SPE_BUILTIN_EVMHESMF },
4723   { 0, CODE_FOR_spe_evmhesmfa, "__builtin_spe_evmhesmfa", SPE_BUILTIN_EVMHESMFA },
4724   { 0, CODE_FOR_spe_evmhesmfaaw, "__builtin_spe_evmhesmfaaw", SPE_BUILTIN_EVMHESMFAAW },
4725   { 0, CODE_FOR_spe_evmhesmfanw, "__builtin_spe_evmhesmfanw", SPE_BUILTIN_EVMHESMFANW },
4726   { 0, CODE_FOR_spe_evmhesmi, "__builtin_spe_evmhesmi", SPE_BUILTIN_EVMHESMI },
4727   { 0, CODE_FOR_spe_evmhesmia, "__builtin_spe_evmhesmia", SPE_BUILTIN_EVMHESMIA },
4728   { 0, CODE_FOR_spe_evmhesmiaaw, "__builtin_spe_evmhesmiaaw", SPE_BUILTIN_EVMHESMIAAW },
4729   { 0, CODE_FOR_spe_evmhesmianw, "__builtin_spe_evmhesmianw", SPE_BUILTIN_EVMHESMIANW },
4730   { 0, CODE_FOR_spe_evmhessf, "__builtin_spe_evmhessf", SPE_BUILTIN_EVMHESSF },
4731   { 0, CODE_FOR_spe_evmhessfa, "__builtin_spe_evmhessfa", SPE_BUILTIN_EVMHESSFA },
4732   { 0, CODE_FOR_spe_evmhessfaaw, "__builtin_spe_evmhessfaaw", SPE_BUILTIN_EVMHESSFAAW },
4733   { 0, CODE_FOR_spe_evmhessfanw, "__builtin_spe_evmhessfanw", SPE_BUILTIN_EVMHESSFANW },
4734   { 0, CODE_FOR_spe_evmhessiaaw, "__builtin_spe_evmhessiaaw", SPE_BUILTIN_EVMHESSIAAW },
4735   { 0, CODE_FOR_spe_evmhessianw, "__builtin_spe_evmhessianw", SPE_BUILTIN_EVMHESSIANW },
4736   { 0, CODE_FOR_spe_evmheumi, "__builtin_spe_evmheumi", SPE_BUILTIN_EVMHEUMI },
4737   { 0, CODE_FOR_spe_evmheumia, "__builtin_spe_evmheumia", SPE_BUILTIN_EVMHEUMIA },
4738   { 0, CODE_FOR_spe_evmheumiaaw, "__builtin_spe_evmheumiaaw", SPE_BUILTIN_EVMHEUMIAAW },
4739   { 0, CODE_FOR_spe_evmheumianw, "__builtin_spe_evmheumianw", SPE_BUILTIN_EVMHEUMIANW },
4740   { 0, CODE_FOR_spe_evmheusiaaw, "__builtin_spe_evmheusiaaw", SPE_BUILTIN_EVMHEUSIAAW },
4741   { 0, CODE_FOR_spe_evmheusianw, "__builtin_spe_evmheusianw", SPE_BUILTIN_EVMHEUSIANW },
4742   { 0, CODE_FOR_spe_evmhogsmfaa, "__builtin_spe_evmhogsmfaa", SPE_BUILTIN_EVMHOGSMFAA },
4743   { 0, CODE_FOR_spe_evmhogsmfan, "__builtin_spe_evmhogsmfan", SPE_BUILTIN_EVMHOGSMFAN },
4744   { 0, CODE_FOR_spe_evmhogsmiaa, "__builtin_spe_evmhogsmiaa", SPE_BUILTIN_EVMHOGSMIAA },
4745   { 0, CODE_FOR_spe_evmhogsmian, "__builtin_spe_evmhogsmian", SPE_BUILTIN_EVMHOGSMIAN },
4746   { 0, CODE_FOR_spe_evmhogumiaa, "__builtin_spe_evmhogumiaa", SPE_BUILTIN_EVMHOGUMIAA },
4747   { 0, CODE_FOR_spe_evmhogumian, "__builtin_spe_evmhogumian", SPE_BUILTIN_EVMHOGUMIAN },
4748   { 0, CODE_FOR_spe_evmhosmf, "__builtin_spe_evmhosmf", SPE_BUILTIN_EVMHOSMF },
4749   { 0, CODE_FOR_spe_evmhosmfa, "__builtin_spe_evmhosmfa", SPE_BUILTIN_EVMHOSMFA },
4750   { 0, CODE_FOR_spe_evmhosmfaaw, "__builtin_spe_evmhosmfaaw", SPE_BUILTIN_EVMHOSMFAAW },
4751   { 0, CODE_FOR_spe_evmhosmfanw, "__builtin_spe_evmhosmfanw", SPE_BUILTIN_EVMHOSMFANW },
4752   { 0, CODE_FOR_spe_evmhosmi, "__builtin_spe_evmhosmi", SPE_BUILTIN_EVMHOSMI },
4753   { 0, CODE_FOR_spe_evmhosmia, "__builtin_spe_evmhosmia", SPE_BUILTIN_EVMHOSMIA },
4754   { 0, CODE_FOR_spe_evmhosmiaaw, "__builtin_spe_evmhosmiaaw", SPE_BUILTIN_EVMHOSMIAAW },
4755   { 0, CODE_FOR_spe_evmhosmianw, "__builtin_spe_evmhosmianw", SPE_BUILTIN_EVMHOSMIANW },
4756   { 0, CODE_FOR_spe_evmhossf, "__builtin_spe_evmhossf", SPE_BUILTIN_EVMHOSSF },
4757   { 0, CODE_FOR_spe_evmhossfa, "__builtin_spe_evmhossfa", SPE_BUILTIN_EVMHOSSFA },
4758   { 0, CODE_FOR_spe_evmhossfaaw, "__builtin_spe_evmhossfaaw", SPE_BUILTIN_EVMHOSSFAAW },
4759   { 0, CODE_FOR_spe_evmhossfanw, "__builtin_spe_evmhossfanw", SPE_BUILTIN_EVMHOSSFANW },
4760   { 0, CODE_FOR_spe_evmhossiaaw, "__builtin_spe_evmhossiaaw", SPE_BUILTIN_EVMHOSSIAAW },
4761   { 0, CODE_FOR_spe_evmhossianw, "__builtin_spe_evmhossianw", SPE_BUILTIN_EVMHOSSIANW },
4762   { 0, CODE_FOR_spe_evmhoumi, "__builtin_spe_evmhoumi", SPE_BUILTIN_EVMHOUMI },
4763   { 0, CODE_FOR_spe_evmhoumia, "__builtin_spe_evmhoumia", SPE_BUILTIN_EVMHOUMIA },
4764   { 0, CODE_FOR_spe_evmhoumiaaw, "__builtin_spe_evmhoumiaaw", SPE_BUILTIN_EVMHOUMIAAW },
4765   { 0, CODE_FOR_spe_evmhoumianw, "__builtin_spe_evmhoumianw", SPE_BUILTIN_EVMHOUMIANW },
4766   { 0, CODE_FOR_spe_evmhousiaaw, "__builtin_spe_evmhousiaaw", SPE_BUILTIN_EVMHOUSIAAW },
4767   { 0, CODE_FOR_spe_evmhousianw, "__builtin_spe_evmhousianw", SPE_BUILTIN_EVMHOUSIANW },
4768   { 0, CODE_FOR_spe_evmwhsmf, "__builtin_spe_evmwhsmf", SPE_BUILTIN_EVMWHSMF },
4769   { 0, CODE_FOR_spe_evmwhsmfa, "__builtin_spe_evmwhsmfa", SPE_BUILTIN_EVMWHSMFA },
4770   { 0, CODE_FOR_spe_evmwhsmi, "__builtin_spe_evmwhsmi", SPE_BUILTIN_EVMWHSMI },
4771   { 0, CODE_FOR_spe_evmwhsmia, "__builtin_spe_evmwhsmia", SPE_BUILTIN_EVMWHSMIA },
4772   { 0, CODE_FOR_spe_evmwhssf, "__builtin_spe_evmwhssf", SPE_BUILTIN_EVMWHSSF },
4773   { 0, CODE_FOR_spe_evmwhssfa, "__builtin_spe_evmwhssfa", SPE_BUILTIN_EVMWHSSFA },
4774   { 0, CODE_FOR_spe_evmwhumi, "__builtin_spe_evmwhumi", SPE_BUILTIN_EVMWHUMI },
4775   { 0, CODE_FOR_spe_evmwhumia, "__builtin_spe_evmwhumia", SPE_BUILTIN_EVMWHUMIA },
4776   { 0, CODE_FOR_spe_evmwlsmiaaw, "__builtin_spe_evmwlsmiaaw", SPE_BUILTIN_EVMWLSMIAAW },
4777   { 0, CODE_FOR_spe_evmwlsmianw, "__builtin_spe_evmwlsmianw", SPE_BUILTIN_EVMWLSMIANW },
4778   { 0, CODE_FOR_spe_evmwlssiaaw, "__builtin_spe_evmwlssiaaw", SPE_BUILTIN_EVMWLSSIAAW },
4779   { 0, CODE_FOR_spe_evmwlssianw, "__builtin_spe_evmwlssianw", SPE_BUILTIN_EVMWLSSIANW },
4780   { 0, CODE_FOR_spe_evmwlumi, "__builtin_spe_evmwlumi", SPE_BUILTIN_EVMWLUMI },
4781   { 0, CODE_FOR_spe_evmwlumia, "__builtin_spe_evmwlumia", SPE_BUILTIN_EVMWLUMIA },
4782   { 0, CODE_FOR_spe_evmwlumiaaw, "__builtin_spe_evmwlumiaaw", SPE_BUILTIN_EVMWLUMIAAW },
4783   { 0, CODE_FOR_spe_evmwlumianw, "__builtin_spe_evmwlumianw", SPE_BUILTIN_EVMWLUMIANW },
4784   { 0, CODE_FOR_spe_evmwlusiaaw, "__builtin_spe_evmwlusiaaw", SPE_BUILTIN_EVMWLUSIAAW },
4785   { 0, CODE_FOR_spe_evmwlusianw, "__builtin_spe_evmwlusianw", SPE_BUILTIN_EVMWLUSIANW },
4786   { 0, CODE_FOR_spe_evmwsmf, "__builtin_spe_evmwsmf", SPE_BUILTIN_EVMWSMF },
4787   { 0, CODE_FOR_spe_evmwsmfa, "__builtin_spe_evmwsmfa", SPE_BUILTIN_EVMWSMFA },
4788   { 0, CODE_FOR_spe_evmwsmfaa, "__builtin_spe_evmwsmfaa", SPE_BUILTIN_EVMWSMFAA },
4789   { 0, CODE_FOR_spe_evmwsmfan, "__builtin_spe_evmwsmfan", SPE_BUILTIN_EVMWSMFAN },
4790   { 0, CODE_FOR_spe_evmwsmi, "__builtin_spe_evmwsmi", SPE_BUILTIN_EVMWSMI },
4791   { 0, CODE_FOR_spe_evmwsmia, "__builtin_spe_evmwsmia", SPE_BUILTIN_EVMWSMIA },
4792   { 0, CODE_FOR_spe_evmwsmiaa, "__builtin_spe_evmwsmiaa", SPE_BUILTIN_EVMWSMIAA },
4793   { 0, CODE_FOR_spe_evmwsmian, "__builtin_spe_evmwsmian", SPE_BUILTIN_EVMWSMIAN },
4794   { 0, CODE_FOR_spe_evmwssf, "__builtin_spe_evmwssf", SPE_BUILTIN_EVMWSSF },
4795   { 0, CODE_FOR_spe_evmwssfa, "__builtin_spe_evmwssfa", SPE_BUILTIN_EVMWSSFA },
4796   { 0, CODE_FOR_spe_evmwssfaa, "__builtin_spe_evmwssfaa", SPE_BUILTIN_EVMWSSFAA },
4797   { 0, CODE_FOR_spe_evmwssfan, "__builtin_spe_evmwssfan", SPE_BUILTIN_EVMWSSFAN },
4798   { 0, CODE_FOR_spe_evmwumi, "__builtin_spe_evmwumi", SPE_BUILTIN_EVMWUMI },
4799   { 0, CODE_FOR_spe_evmwumia, "__builtin_spe_evmwumia", SPE_BUILTIN_EVMWUMIA },
4800   { 0, CODE_FOR_spe_evmwumiaa, "__builtin_spe_evmwumiaa", SPE_BUILTIN_EVMWUMIAA },
4801   { 0, CODE_FOR_spe_evmwumian, "__builtin_spe_evmwumian", SPE_BUILTIN_EVMWUMIAN },
4802   { 0, CODE_FOR_spe_evnand, "__builtin_spe_evnand", SPE_BUILTIN_EVNAND },
4803   { 0, CODE_FOR_spe_evnor, "__builtin_spe_evnor", SPE_BUILTIN_EVNOR },
4804   { 0, CODE_FOR_spe_evor, "__builtin_spe_evor", SPE_BUILTIN_EVOR },
4805   { 0, CODE_FOR_spe_evorc, "__builtin_spe_evorc", SPE_BUILTIN_EVORC },
4806   { 0, CODE_FOR_spe_evrlw, "__builtin_spe_evrlw", SPE_BUILTIN_EVRLW },
4807   { 0, CODE_FOR_spe_evslw, "__builtin_spe_evslw", SPE_BUILTIN_EVSLW },
4808   { 0, CODE_FOR_spe_evsrws, "__builtin_spe_evsrws", SPE_BUILTIN_EVSRWS },
4809   { 0, CODE_FOR_spe_evsrwu, "__builtin_spe_evsrwu", SPE_BUILTIN_EVSRWU },
4810   { 0, CODE_FOR_spe_evsubfw, "__builtin_spe_evsubfw", SPE_BUILTIN_EVSUBFW },
4811
4812   /* SPE binary operations expecting a 5-bit unsigned literal.  */
4813   { 0, CODE_FOR_spe_evaddiw, "__builtin_spe_evaddiw", SPE_BUILTIN_EVADDIW },
4814
4815   { 0, CODE_FOR_spe_evrlwi, "__builtin_spe_evrlwi", SPE_BUILTIN_EVRLWI },
4816   { 0, CODE_FOR_spe_evslwi, "__builtin_spe_evslwi", SPE_BUILTIN_EVSLWI },
4817   { 0, CODE_FOR_spe_evsrwis, "__builtin_spe_evsrwis", SPE_BUILTIN_EVSRWIS },
4818   { 0, CODE_FOR_spe_evsrwiu, "__builtin_spe_evsrwiu", SPE_BUILTIN_EVSRWIU },
4819   { 0, CODE_FOR_spe_evsubifw, "__builtin_spe_evsubifw", SPE_BUILTIN_EVSUBIFW },
4820   { 0, CODE_FOR_spe_evmwhssfaa, "__builtin_spe_evmwhssfaa", SPE_BUILTIN_EVMWHSSFAA },
4821   { 0, CODE_FOR_spe_evmwhssmaa, "__builtin_spe_evmwhssmaa", SPE_BUILTIN_EVMWHSSMAA },
4822   { 0, CODE_FOR_spe_evmwhsmfaa, "__builtin_spe_evmwhsmfaa", SPE_BUILTIN_EVMWHSMFAA },
4823   { 0, CODE_FOR_spe_evmwhsmiaa, "__builtin_spe_evmwhsmiaa", SPE_BUILTIN_EVMWHSMIAA },
4824   { 0, CODE_FOR_spe_evmwhusiaa, "__builtin_spe_evmwhusiaa", SPE_BUILTIN_EVMWHUSIAA },
4825   { 0, CODE_FOR_spe_evmwhumiaa, "__builtin_spe_evmwhumiaa", SPE_BUILTIN_EVMWHUMIAA },
4826   { 0, CODE_FOR_spe_evmwhssfan, "__builtin_spe_evmwhssfan", SPE_BUILTIN_EVMWHSSFAN },
4827   { 0, CODE_FOR_spe_evmwhssian, "__builtin_spe_evmwhssian", SPE_BUILTIN_EVMWHSSIAN },
4828   { 0, CODE_FOR_spe_evmwhsmfan, "__builtin_spe_evmwhsmfan", SPE_BUILTIN_EVMWHSMFAN },
4829   { 0, CODE_FOR_spe_evmwhsmian, "__builtin_spe_evmwhsmian", SPE_BUILTIN_EVMWHSMIAN },
4830   { 0, CODE_FOR_spe_evmwhusian, "__builtin_spe_evmwhusian", SPE_BUILTIN_EVMWHUSIAN },
4831   { 0, CODE_FOR_spe_evmwhumian, "__builtin_spe_evmwhumian", SPE_BUILTIN_EVMWHUMIAN },
4832   { 0, CODE_FOR_spe_evmwhgssfaa, "__builtin_spe_evmwhgssfaa", SPE_BUILTIN_EVMWHGSSFAA },
4833   { 0, CODE_FOR_spe_evmwhgsmfaa, "__builtin_spe_evmwhgsmfaa", SPE_BUILTIN_EVMWHGSMFAA },
4834   { 0, CODE_FOR_spe_evmwhgsmiaa, "__builtin_spe_evmwhgsmiaa", SPE_BUILTIN_EVMWHGSMIAA },
4835   { 0, CODE_FOR_spe_evmwhgumiaa, "__builtin_spe_evmwhgumiaa", SPE_BUILTIN_EVMWHGUMIAA },
4836   { 0, CODE_FOR_spe_evmwhgssfan, "__builtin_spe_evmwhgssfan", SPE_BUILTIN_EVMWHGSSFAN },
4837   { 0, CODE_FOR_spe_evmwhgsmfan, "__builtin_spe_evmwhgsmfan", SPE_BUILTIN_EVMWHGSMFAN },
4838   { 0, CODE_FOR_spe_evmwhgsmian, "__builtin_spe_evmwhgsmian", SPE_BUILTIN_EVMWHGSMIAN },
4839   { 0, CODE_FOR_spe_evmwhgumian, "__builtin_spe_evmwhgumian", SPE_BUILTIN_EVMWHGUMIAN },
4840   { 0, CODE_FOR_spe_brinc, "__builtin_spe_brinc", SPE_BUILTIN_BRINC },
4841
4842   /* Place-holder.  Leave as last binary SPE builtin.  */
4843   { 0, CODE_FOR_xorv2si3, "__builtin_spe_evxor", SPE_BUILTIN_EVXOR },
4844 };
4845
4846 /* AltiVec predicates.  */
4847
4848 struct builtin_description_predicates
4849 {
4850   const unsigned int mask;
4851   const enum insn_code icode;
4852   const char *opcode;
4853   const char *const name;
4854   const enum rs6000_builtins code;
4855 };
4856
4857 static const struct builtin_description_predicates bdesc_altivec_preds[] =
4858 {
4859   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpbfp.", "__builtin_altivec_vcmpbfp_p", ALTIVEC_BUILTIN_VCMPBFP_P },
4860   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpeqfp.", "__builtin_altivec_vcmpeqfp_p", ALTIVEC_BUILTIN_VCMPEQFP_P },
4861   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgefp.", "__builtin_altivec_vcmpgefp_p", ALTIVEC_BUILTIN_VCMPGEFP_P },
4862   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgtfp.", "__builtin_altivec_vcmpgtfp_p", ALTIVEC_BUILTIN_VCMPGTFP_P },
4863   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpequw.", "__builtin_altivec_vcmpequw_p", ALTIVEC_BUILTIN_VCMPEQUW_P },
4864   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtsw.", "__builtin_altivec_vcmpgtsw_p", ALTIVEC_BUILTIN_VCMPGTSW_P },
4865   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtuw.", "__builtin_altivec_vcmpgtuw_p", ALTIVEC_BUILTIN_VCMPGTUW_P },
4866   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtuh.", "__builtin_altivec_vcmpgtuh_p", ALTIVEC_BUILTIN_VCMPGTUH_P },
4867   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtsh.", "__builtin_altivec_vcmpgtsh_p", ALTIVEC_BUILTIN_VCMPGTSH_P },
4868   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpequh.", "__builtin_altivec_vcmpequh_p", ALTIVEC_BUILTIN_VCMPEQUH_P },
4869   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpequb.", "__builtin_altivec_vcmpequb_p", ALTIVEC_BUILTIN_VCMPEQUB_P },
4870   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtsb.", "__builtin_altivec_vcmpgtsb_p", ALTIVEC_BUILTIN_VCMPGTSB_P },
4871   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtub.", "__builtin_altivec_vcmpgtub_p", ALTIVEC_BUILTIN_VCMPGTUB_P }
4872 };
4873
4874 /* SPE predicates.  */
4875 static struct builtin_description bdesc_spe_predicates[] =
4876 {
4877   /* Place-holder.  Leave as first.  */
4878   { 0, CODE_FOR_spe_evcmpeq, "__builtin_spe_evcmpeq", SPE_BUILTIN_EVCMPEQ },
4879   { 0, CODE_FOR_spe_evcmpgts, "__builtin_spe_evcmpgts", SPE_BUILTIN_EVCMPGTS },
4880   { 0, CODE_FOR_spe_evcmpgtu, "__builtin_spe_evcmpgtu", SPE_BUILTIN_EVCMPGTU },
4881   { 0, CODE_FOR_spe_evcmplts, "__builtin_spe_evcmplts", SPE_BUILTIN_EVCMPLTS },
4882   { 0, CODE_FOR_spe_evcmpltu, "__builtin_spe_evcmpltu", SPE_BUILTIN_EVCMPLTU },
4883   { 0, CODE_FOR_spe_evfscmpeq, "__builtin_spe_evfscmpeq", SPE_BUILTIN_EVFSCMPEQ },
4884   { 0, CODE_FOR_spe_evfscmpgt, "__builtin_spe_evfscmpgt", SPE_BUILTIN_EVFSCMPGT },
4885   { 0, CODE_FOR_spe_evfscmplt, "__builtin_spe_evfscmplt", SPE_BUILTIN_EVFSCMPLT },
4886   { 0, CODE_FOR_spe_evfststeq, "__builtin_spe_evfststeq", SPE_BUILTIN_EVFSTSTEQ },
4887   { 0, CODE_FOR_spe_evfststgt, "__builtin_spe_evfststgt", SPE_BUILTIN_EVFSTSTGT },
4888   /* Place-holder.  Leave as last.  */
4889   { 0, CODE_FOR_spe_evfststlt, "__builtin_spe_evfststlt", SPE_BUILTIN_EVFSTSTLT },
4890 };
4891
4892 /* SPE evsel predicates.  */
4893 static struct builtin_description bdesc_spe_evsel[] =
4894 {
4895   /* Place-holder.  Leave as first.  */
4896   { 0, CODE_FOR_spe_evcmpgts, "__builtin_spe_evsel_gts", SPE_BUILTIN_EVSEL_CMPGTS },
4897   { 0, CODE_FOR_spe_evcmpgtu, "__builtin_spe_evsel_gtu", SPE_BUILTIN_EVSEL_CMPGTU },
4898   { 0, CODE_FOR_spe_evcmplts, "__builtin_spe_evsel_lts", SPE_BUILTIN_EVSEL_CMPLTS },
4899   { 0, CODE_FOR_spe_evcmpltu, "__builtin_spe_evsel_ltu", SPE_BUILTIN_EVSEL_CMPLTU },
4900   { 0, CODE_FOR_spe_evcmpeq, "__builtin_spe_evsel_eq", SPE_BUILTIN_EVSEL_CMPEQ },
4901   { 0, CODE_FOR_spe_evfscmpgt, "__builtin_spe_evsel_fsgt", SPE_BUILTIN_EVSEL_FSCMPGT },
4902   { 0, CODE_FOR_spe_evfscmplt, "__builtin_spe_evsel_fslt", SPE_BUILTIN_EVSEL_FSCMPLT },
4903   { 0, CODE_FOR_spe_evfscmpeq, "__builtin_spe_evsel_fseq", SPE_BUILTIN_EVSEL_FSCMPEQ },
4904   { 0, CODE_FOR_spe_evfststgt, "__builtin_spe_evsel_fststgt", SPE_BUILTIN_EVSEL_FSTSTGT },
4905   { 0, CODE_FOR_spe_evfststlt, "__builtin_spe_evsel_fststlt", SPE_BUILTIN_EVSEL_FSTSTLT },
4906   /* Place-holder.  Leave as last.  */
4907   { 0, CODE_FOR_spe_evfststeq, "__builtin_spe_evsel_fststeq", SPE_BUILTIN_EVSEL_FSTSTEQ },
4908 };
4909
4910 /* ABS* operations.  */
4911
4912 static const struct builtin_description bdesc_abs[] =
4913 {
4914   { MASK_ALTIVEC, CODE_FOR_absv4si2, "__builtin_altivec_abs_v4si", ALTIVEC_BUILTIN_ABS_V4SI },
4915   { MASK_ALTIVEC, CODE_FOR_absv8hi2, "__builtin_altivec_abs_v8hi", ALTIVEC_BUILTIN_ABS_V8HI },
4916   { MASK_ALTIVEC, CODE_FOR_absv4sf2, "__builtin_altivec_abs_v4sf", ALTIVEC_BUILTIN_ABS_V4SF },
4917   { MASK_ALTIVEC, CODE_FOR_absv16qi2, "__builtin_altivec_abs_v16qi", ALTIVEC_BUILTIN_ABS_V16QI },
4918   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v4si, "__builtin_altivec_abss_v4si", ALTIVEC_BUILTIN_ABSS_V4SI },
4919   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v8hi, "__builtin_altivec_abss_v8hi", ALTIVEC_BUILTIN_ABSS_V8HI },
4920   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v16qi, "__builtin_altivec_abss_v16qi", ALTIVEC_BUILTIN_ABSS_V16QI }
4921 };
4922
4923 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
4924    foo (VECa).  */
4925
4926 static struct builtin_description bdesc_1arg[] =
4927 {
4928   { MASK_ALTIVEC, CODE_FOR_altivec_vexptefp, "__builtin_altivec_vexptefp", ALTIVEC_BUILTIN_VEXPTEFP },
4929   { MASK_ALTIVEC, CODE_FOR_altivec_vlogefp, "__builtin_altivec_vlogefp", ALTIVEC_BUILTIN_VLOGEFP },
4930   { MASK_ALTIVEC, CODE_FOR_altivec_vrefp, "__builtin_altivec_vrefp", ALTIVEC_BUILTIN_VREFP },
4931   { MASK_ALTIVEC, CODE_FOR_altivec_vrfim, "__builtin_altivec_vrfim", ALTIVEC_BUILTIN_VRFIM },
4932   { MASK_ALTIVEC, CODE_FOR_altivec_vrfin, "__builtin_altivec_vrfin", ALTIVEC_BUILTIN_VRFIN },
4933   { MASK_ALTIVEC, CODE_FOR_altivec_vrfip, "__builtin_altivec_vrfip", ALTIVEC_BUILTIN_VRFIP },
4934   { MASK_ALTIVEC, CODE_FOR_ftruncv4sf2, "__builtin_altivec_vrfiz", ALTIVEC_BUILTIN_VRFIZ },
4935   { MASK_ALTIVEC, CODE_FOR_altivec_vrsqrtefp, "__builtin_altivec_vrsqrtefp", ALTIVEC_BUILTIN_VRSQRTEFP },
4936   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisb, "__builtin_altivec_vspltisb", ALTIVEC_BUILTIN_VSPLTISB },
4937   { MASK_ALTIVEC, CODE_FOR_altivec_vspltish, "__builtin_altivec_vspltish", ALTIVEC_BUILTIN_VSPLTISH },
4938   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisw, "__builtin_altivec_vspltisw", ALTIVEC_BUILTIN_VSPLTISW },
4939   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsb, "__builtin_altivec_vupkhsb", ALTIVEC_BUILTIN_VUPKHSB },
4940   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhpx, "__builtin_altivec_vupkhpx", ALTIVEC_BUILTIN_VUPKHPX },
4941   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsh, "__builtin_altivec_vupkhsh", ALTIVEC_BUILTIN_VUPKHSH },
4942   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsb, "__builtin_altivec_vupklsb", ALTIVEC_BUILTIN_VUPKLSB },
4943   { MASK_ALTIVEC, CODE_FOR_altivec_vupklpx, "__builtin_altivec_vupklpx", ALTIVEC_BUILTIN_VUPKLPX },
4944   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsh, "__builtin_altivec_vupklsh", ALTIVEC_BUILTIN_VUPKLSH },
4945
4946   /* The SPE unary builtins must start with SPE_BUILTIN_EVABS and
4947      end with SPE_BUILTIN_EVSUBFUSIAAW.  */
4948   { 0, CODE_FOR_spe_evabs, "__builtin_spe_evabs", SPE_BUILTIN_EVABS },
4949   { 0, CODE_FOR_spe_evaddsmiaaw, "__builtin_spe_evaddsmiaaw", SPE_BUILTIN_EVADDSMIAAW },
4950   { 0, CODE_FOR_spe_evaddssiaaw, "__builtin_spe_evaddssiaaw", SPE_BUILTIN_EVADDSSIAAW },
4951   { 0, CODE_FOR_spe_evaddumiaaw, "__builtin_spe_evaddumiaaw", SPE_BUILTIN_EVADDUMIAAW },
4952   { 0, CODE_FOR_spe_evaddusiaaw, "__builtin_spe_evaddusiaaw", SPE_BUILTIN_EVADDUSIAAW },
4953   { 0, CODE_FOR_spe_evcntlsw, "__builtin_spe_evcntlsw", SPE_BUILTIN_EVCNTLSW },
4954   { 0, CODE_FOR_spe_evcntlzw, "__builtin_spe_evcntlzw", SPE_BUILTIN_EVCNTLZW },
4955   { 0, CODE_FOR_spe_evextsb, "__builtin_spe_evextsb", SPE_BUILTIN_EVEXTSB },
4956   { 0, CODE_FOR_spe_evextsh, "__builtin_spe_evextsh", SPE_BUILTIN_EVEXTSH },
4957   { 0, CODE_FOR_spe_evfsabs, "__builtin_spe_evfsabs", SPE_BUILTIN_EVFSABS },
4958   { 0, CODE_FOR_spe_evfscfsf, "__builtin_spe_evfscfsf", SPE_BUILTIN_EVFSCFSF },
4959   { 0, CODE_FOR_spe_evfscfsi, "__builtin_spe_evfscfsi", SPE_BUILTIN_EVFSCFSI },
4960   { 0, CODE_FOR_spe_evfscfuf, "__builtin_spe_evfscfuf", SPE_BUILTIN_EVFSCFUF },
4961   { 0, CODE_FOR_spe_evfscfui, "__builtin_spe_evfscfui", SPE_BUILTIN_EVFSCFUI },
4962   { 0, CODE_FOR_spe_evfsctsf, "__builtin_spe_evfsctsf", SPE_BUILTIN_EVFSCTSF },
4963   { 0, CODE_FOR_spe_evfsctsi, "__builtin_spe_evfsctsi", SPE_BUILTIN_EVFSCTSI },
4964   { 0, CODE_FOR_spe_evfsctsiz, "__builtin_spe_evfsctsiz", SPE_BUILTIN_EVFSCTSIZ },
4965   { 0, CODE_FOR_spe_evfsctuf, "__builtin_spe_evfsctuf", SPE_BUILTIN_EVFSCTUF },
4966   { 0, CODE_FOR_spe_evfsctui, "__builtin_spe_evfsctui", SPE_BUILTIN_EVFSCTUI },
4967   { 0, CODE_FOR_spe_evfsctuiz, "__builtin_spe_evfsctuiz", SPE_BUILTIN_EVFSCTUIZ },
4968   { 0, CODE_FOR_spe_evfsnabs, "__builtin_spe_evfsnabs", SPE_BUILTIN_EVFSNABS },
4969   { 0, CODE_FOR_spe_evfsneg, "__builtin_spe_evfsneg", SPE_BUILTIN_EVFSNEG },
4970   { 0, CODE_FOR_spe_evmra, "__builtin_spe_evmra", SPE_BUILTIN_EVMRA },
4971   { 0, CODE_FOR_spe_evneg, "__builtin_spe_evneg", SPE_BUILTIN_EVNEG },
4972   { 0, CODE_FOR_spe_evrndw, "__builtin_spe_evrndw", SPE_BUILTIN_EVRNDW },
4973   { 0, CODE_FOR_spe_evsubfsmiaaw, "__builtin_spe_evsubfsmiaaw", SPE_BUILTIN_EVSUBFSMIAAW },
4974   { 0, CODE_FOR_spe_evsubfssiaaw, "__builtin_spe_evsubfssiaaw", SPE_BUILTIN_EVSUBFSSIAAW },
4975   { 0, CODE_FOR_spe_evsubfumiaaw, "__builtin_spe_evsubfumiaaw", SPE_BUILTIN_EVSUBFUMIAAW },
4976   { 0, CODE_FOR_spe_evsplatfi, "__builtin_spe_evsplatfi", SPE_BUILTIN_EVSPLATFI },
4977   { 0, CODE_FOR_spe_evsplati, "__builtin_spe_evsplati", SPE_BUILTIN_EVSPLATI },
4978
4979   /* Place-holder.  Leave as last unary SPE builtin.  */
4980   { 0, CODE_FOR_spe_evsubfusiaaw, "__builtin_spe_evsubfusiaaw", SPE_BUILTIN_EVSUBFUSIAAW },
4981 };
4982
4983 static rtx
4984 rs6000_expand_unop_builtin (icode, arglist, target)
4985      enum insn_code icode;
4986      tree arglist;
4987      rtx target;
4988 {
4989   rtx pat;
4990   tree arg0 = TREE_VALUE (arglist);
4991   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
4992   enum machine_mode tmode = insn_data[icode].operand[0].mode;
4993   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
4994
4995   if (icode == CODE_FOR_nothing)
4996     /* Builtin not supported on this processor.  */
4997     return 0;
4998
4999   /* If we got invalid arguments bail out before generating bad rtl.  */
5000   if (arg0 == error_mark_node)
5001     return const0_rtx;
5002
5003   if (icode == CODE_FOR_altivec_vspltisb
5004       || icode == CODE_FOR_altivec_vspltish
5005       || icode == CODE_FOR_altivec_vspltisw
5006       || icode == CODE_FOR_spe_evsplatfi
5007       || icode == CODE_FOR_spe_evsplati)
5008     {
5009       /* Only allow 5-bit *signed* literals.  */
5010       if (GET_CODE (op0) != CONST_INT
5011           || INTVAL (op0) > 0x1f
5012           || INTVAL (op0) < -0x1f)
5013         {
5014           error ("argument 1 must be a 5-bit signed literal");
5015           return const0_rtx;
5016         }
5017     }
5018
5019   if (target == 0
5020       || GET_MODE (target) != tmode
5021       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5022     target = gen_reg_rtx (tmode);
5023
5024   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5025     op0 = copy_to_mode_reg (mode0, op0);
5026
5027   pat = GEN_FCN (icode) (target, op0);
5028   if (! pat)
5029     return 0;
5030   emit_insn (pat);
5031
5032   return target;
5033 }
5034
5035 static rtx
5036 altivec_expand_abs_builtin (icode, arglist, target)
5037      enum insn_code icode;
5038      tree arglist;
5039      rtx target;
5040 {
5041   rtx pat, scratch1, scratch2;
5042   tree arg0 = TREE_VALUE (arglist);
5043   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5044   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5045   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5046
5047   /* If we have invalid arguments, bail out before generating bad rtl.  */
5048   if (arg0 == error_mark_node)
5049     return const0_rtx;
5050
5051   if (target == 0
5052       || GET_MODE (target) != tmode
5053       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5054     target = gen_reg_rtx (tmode);
5055
5056   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5057     op0 = copy_to_mode_reg (mode0, op0);
5058
5059   scratch1 = gen_reg_rtx (mode0);
5060   scratch2 = gen_reg_rtx (mode0);
5061
5062   pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
5063   if (! pat)
5064     return 0;
5065   emit_insn (pat);
5066
5067   return target;
5068 }
5069
5070 static rtx
5071 rs6000_expand_binop_builtin (icode, arglist, target)
5072      enum insn_code icode;
5073      tree arglist;
5074      rtx target;
5075 {
5076   rtx pat;
5077   tree arg0 = TREE_VALUE (arglist);
5078   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5079   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5080   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5081   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5082   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5083   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5084
5085   if (icode == CODE_FOR_nothing)
5086     /* Builtin not supported on this processor.  */
5087     return 0;
5088
5089   /* If we got invalid arguments bail out before generating bad rtl.  */
5090   if (arg0 == error_mark_node || arg1 == error_mark_node)
5091     return const0_rtx;
5092
5093   if (icode == CODE_FOR_altivec_vcfux
5094       || icode == CODE_FOR_altivec_vcfsx
5095       || icode == CODE_FOR_altivec_vctsxs
5096       || icode == CODE_FOR_altivec_vctuxs
5097       || icode == CODE_FOR_altivec_vspltb
5098       || icode == CODE_FOR_altivec_vsplth
5099       || icode == CODE_FOR_altivec_vspltw
5100       || icode == CODE_FOR_spe_evaddiw
5101       || icode == CODE_FOR_spe_evldd
5102       || icode == CODE_FOR_spe_evldh
5103       || icode == CODE_FOR_spe_evldw
5104       || icode == CODE_FOR_spe_evlhhesplat
5105       || icode == CODE_FOR_spe_evlhhossplat
5106       || icode == CODE_FOR_spe_evlhhousplat
5107       || icode == CODE_FOR_spe_evlwhe
5108       || icode == CODE_FOR_spe_evlwhos
5109       || icode == CODE_FOR_spe_evlwhou
5110       || icode == CODE_FOR_spe_evlwhsplat
5111       || icode == CODE_FOR_spe_evlwwsplat
5112       || icode == CODE_FOR_spe_evrlwi
5113       || icode == CODE_FOR_spe_evslwi
5114       || icode == CODE_FOR_spe_evsrwis
5115       || icode == CODE_FOR_spe_evsubifw
5116       || icode == CODE_FOR_spe_evsrwiu)
5117     {
5118       /* Only allow 5-bit unsigned literals.  */
5119       if (TREE_CODE (arg1) != INTEGER_CST
5120           || TREE_INT_CST_LOW (arg1) & ~0x1f)
5121         {
5122           error ("argument 2 must be a 5-bit unsigned literal");
5123           return const0_rtx;
5124         }
5125     }
5126
5127   if (target == 0
5128       || GET_MODE (target) != tmode
5129       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5130     target = gen_reg_rtx (tmode);
5131
5132   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5133     op0 = copy_to_mode_reg (mode0, op0);
5134   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5135     op1 = copy_to_mode_reg (mode1, op1);
5136
5137   pat = GEN_FCN (icode) (target, op0, op1);
5138   if (! pat)
5139     return 0;
5140   emit_insn (pat);
5141
5142   return target;
5143 }
5144
5145 static rtx
5146 altivec_expand_predicate_builtin (icode, opcode, arglist, target)
5147      enum insn_code icode;
5148      const char *opcode;
5149      tree arglist;
5150      rtx target;
5151 {
5152   rtx pat, scratch;
5153   tree cr6_form = TREE_VALUE (arglist);
5154   tree arg0 = TREE_VALUE (TREE_CHAIN (arglist));
5155   tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5156   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5157   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5158   enum machine_mode tmode = SImode;
5159   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5160   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5161   int cr6_form_int;
5162
5163   if (TREE_CODE (cr6_form) != INTEGER_CST)
5164     {
5165       error ("argument 1 of __builtin_altivec_predicate must be a constant");
5166       return const0_rtx;
5167     }
5168   else
5169     cr6_form_int = TREE_INT_CST_LOW (cr6_form);
5170
5171   if (mode0 != mode1)
5172     abort ();
5173
5174   /* If we have invalid arguments, bail out before generating bad rtl.  */
5175   if (arg0 == error_mark_node || arg1 == error_mark_node)
5176     return const0_rtx;
5177
5178   if (target == 0
5179       || GET_MODE (target) != tmode
5180       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5181     target = gen_reg_rtx (tmode);
5182
5183   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5184     op0 = copy_to_mode_reg (mode0, op0);
5185   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5186     op1 = copy_to_mode_reg (mode1, op1);
5187
5188   scratch = gen_reg_rtx (mode0);
5189
5190   pat = GEN_FCN (icode) (scratch, op0, op1,
5191                          gen_rtx (SYMBOL_REF, Pmode, opcode));
5192   if (! pat)
5193     return 0;
5194   emit_insn (pat);
5195
5196   /* The vec_any* and vec_all* predicates use the same opcodes for two
5197      different operations, but the bits in CR6 will be different
5198      depending on what information we want.  So we have to play tricks
5199      with CR6 to get the right bits out.
5200
5201      If you think this is disgusting, look at the specs for the
5202      AltiVec predicates.  */
5203
5204      switch (cr6_form_int)
5205        {
5206        case 0:
5207          emit_insn (gen_cr6_test_for_zero (target));
5208          break;
5209        case 1:
5210          emit_insn (gen_cr6_test_for_zero_reverse (target));
5211          break;
5212        case 2:
5213          emit_insn (gen_cr6_test_for_lt (target));
5214          break;
5215        case 3:
5216          emit_insn (gen_cr6_test_for_lt_reverse (target));
5217          break;
5218        default:
5219          error ("argument 1 of __builtin_altivec_predicate is out of range");
5220          break;
5221        }
5222
5223   return target;
5224 }
5225
5226 static rtx
5227 altivec_expand_stv_builtin (icode, arglist)
5228      enum insn_code icode;
5229      tree arglist;
5230 {
5231   tree arg0 = TREE_VALUE (arglist);
5232   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5233   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5234   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5235   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5236   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
5237   rtx pat;
5238   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
5239   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
5240   enum machine_mode mode2 = insn_data[icode].operand[2].mode;
5241
5242   /* Invalid arguments.  Bail before doing anything stoopid!  */
5243   if (arg0 == error_mark_node
5244       || arg1 == error_mark_node
5245       || arg2 == error_mark_node)
5246     return const0_rtx;
5247
5248   if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
5249     op0 = copy_to_mode_reg (mode2, op0);
5250   if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
5251     op1 = copy_to_mode_reg (mode0, op1);
5252   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
5253     op2 = copy_to_mode_reg (mode1, op2);
5254
5255   pat = GEN_FCN (icode) (op1, op2, op0);
5256   if (pat)
5257     emit_insn (pat);
5258   return NULL_RTX;
5259 }
5260
5261 static rtx
5262 rs6000_expand_ternop_builtin (icode, arglist, target)
5263      enum insn_code icode;
5264      tree arglist;
5265      rtx target;
5266 {
5267   rtx pat;
5268   tree arg0 = TREE_VALUE (arglist);
5269   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5270   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5271   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5272   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5273   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
5274   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5275   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5276   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5277   enum machine_mode mode2 = insn_data[icode].operand[3].mode;
5278
5279   if (icode == CODE_FOR_nothing)
5280     /* Builtin not supported on this processor.  */
5281     return 0;
5282
5283   /* If we got invalid arguments bail out before generating bad rtl.  */
5284   if (arg0 == error_mark_node
5285       || arg1 == error_mark_node
5286       || arg2 == error_mark_node)
5287     return const0_rtx;
5288
5289   if (icode == CODE_FOR_altivec_vsldoi_4sf
5290       || icode == CODE_FOR_altivec_vsldoi_4si
5291       || icode == CODE_FOR_altivec_vsldoi_8hi
5292       || icode == CODE_FOR_altivec_vsldoi_16qi)
5293     {
5294       /* Only allow 4-bit unsigned literals.  */
5295       if (TREE_CODE (arg2) != INTEGER_CST
5296           || TREE_INT_CST_LOW (arg2) & ~0xf)
5297         {
5298           error ("argument 3 must be a 4-bit unsigned literal");
5299           return const0_rtx;
5300         }
5301     }
5302
5303   if (target == 0
5304       || GET_MODE (target) != tmode
5305       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5306     target = gen_reg_rtx (tmode);
5307
5308   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5309     op0 = copy_to_mode_reg (mode0, op0);
5310   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5311     op1 = copy_to_mode_reg (mode1, op1);
5312   if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
5313     op2 = copy_to_mode_reg (mode2, op2);
5314
5315   pat = GEN_FCN (icode) (target, op0, op1, op2);
5316   if (! pat)
5317     return 0;
5318   emit_insn (pat);
5319
5320   return target;
5321 }
5322
5323 /* Expand the lvx builtins.  */
5324 static rtx
5325 altivec_expand_ld_builtin (exp, target, expandedp)
5326      tree exp;
5327      rtx target;
5328      bool *expandedp;
5329 {
5330   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5331   tree arglist = TREE_OPERAND (exp, 1);
5332   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5333   tree arg0;
5334   enum machine_mode tmode, mode0;
5335   rtx pat, op0;
5336   enum insn_code icode;
5337
5338   switch (fcode)
5339     {
5340     case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
5341       icode = CODE_FOR_altivec_lvx_16qi;
5342       break;
5343     case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
5344       icode = CODE_FOR_altivec_lvx_8hi;
5345       break;
5346     case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
5347       icode = CODE_FOR_altivec_lvx_4si;
5348       break;
5349     case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
5350       icode = CODE_FOR_altivec_lvx_4sf;
5351       break;
5352     default:
5353       *expandedp = false;
5354       return NULL_RTX;
5355     }
5356
5357   *expandedp = true;
5358
5359   arg0 = TREE_VALUE (arglist);
5360   op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5361   tmode = insn_data[icode].operand[0].mode;
5362   mode0 = insn_data[icode].operand[1].mode;
5363
5364   if (target == 0
5365       || GET_MODE (target) != tmode
5366       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5367     target = gen_reg_rtx (tmode);
5368
5369   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5370     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
5371
5372   pat = GEN_FCN (icode) (target, op0);
5373   if (! pat)
5374     return 0;
5375   emit_insn (pat);
5376   return target;
5377 }
5378
5379 /* Expand the stvx builtins.  */
5380 static rtx
5381 altivec_expand_st_builtin (exp, target, expandedp)
5382      tree exp;
5383      rtx target ATTRIBUTE_UNUSED;
5384      bool *expandedp;
5385 {
5386   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5387   tree arglist = TREE_OPERAND (exp, 1);
5388   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5389   tree arg0, arg1;
5390   enum machine_mode mode0, mode1;
5391   rtx pat, op0, op1;
5392   enum insn_code icode;
5393
5394   switch (fcode)
5395     {
5396     case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
5397       icode = CODE_FOR_altivec_stvx_16qi;
5398       break;
5399     case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
5400       icode = CODE_FOR_altivec_stvx_8hi;
5401       break;
5402     case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
5403       icode = CODE_FOR_altivec_stvx_4si;
5404       break;
5405     case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
5406       icode = CODE_FOR_altivec_stvx_4sf;
5407       break;
5408     default:
5409       *expandedp = false;
5410       return NULL_RTX;
5411     }
5412
5413   arg0 = TREE_VALUE (arglist);
5414   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5415   op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5416   op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5417   mode0 = insn_data[icode].operand[0].mode;
5418   mode1 = insn_data[icode].operand[1].mode;
5419
5420   if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
5421     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
5422   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
5423     op1 = copy_to_mode_reg (mode1, op1);
5424
5425   pat = GEN_FCN (icode) (op0, op1);
5426   if (pat)
5427     emit_insn (pat);
5428
5429   *expandedp = true;
5430   return NULL_RTX;
5431 }
5432
5433 /* Expand the dst builtins.  */
5434 static rtx
5435 altivec_expand_dst_builtin (exp, target, expandedp)
5436      tree exp;
5437      rtx target ATTRIBUTE_UNUSED;
5438      bool *expandedp;
5439 {
5440   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5441   tree arglist = TREE_OPERAND (exp, 1);
5442   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5443   tree arg0, arg1, arg2;
5444   enum machine_mode mode0, mode1, mode2;
5445   rtx pat, op0, op1, op2;
5446   struct builtin_description *d;
5447   size_t i;
5448
5449   *expandedp = false;
5450
5451   /* Handle DST variants.  */
5452   d = (struct builtin_description *) bdesc_dst;
5453   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
5454     if (d->code == fcode)
5455       {
5456         arg0 = TREE_VALUE (arglist);
5457         arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5458         arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5459         op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5460         op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5461         op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
5462         mode0 = insn_data[d->icode].operand[0].mode;
5463         mode1 = insn_data[d->icode].operand[1].mode;
5464         mode2 = insn_data[d->icode].operand[2].mode;
5465
5466         /* Invalid arguments, bail out before generating bad rtl.  */
5467         if (arg0 == error_mark_node
5468             || arg1 == error_mark_node
5469             || arg2 == error_mark_node)
5470           return const0_rtx;
5471
5472         if (TREE_CODE (arg2) != INTEGER_CST
5473             || TREE_INT_CST_LOW (arg2) & ~0x3)
5474           {
5475             error ("argument to `%s' must be a 2-bit unsigned literal", d->name);
5476             return const0_rtx;
5477           }
5478
5479         if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
5480           op0 = copy_to_mode_reg (mode0, op0);
5481         if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
5482           op1 = copy_to_mode_reg (mode1, op1);
5483
5484         pat = GEN_FCN (d->icode) (op0, op1, op2);
5485         if (pat != 0)
5486           emit_insn (pat);
5487
5488         *expandedp = true;
5489         return NULL_RTX;
5490       }
5491
5492   return NULL_RTX;
5493 }
5494
5495 /* Expand the builtin in EXP and store the result in TARGET.  Store
5496    true in *EXPANDEDP if we found a builtin to expand.  */
5497 static rtx
5498 altivec_expand_builtin (exp, target, expandedp)
5499      tree exp;
5500      rtx target;
5501      bool *expandedp;
5502 {
5503   struct builtin_description *d;
5504   struct builtin_description_predicates *dp;
5505   size_t i;
5506   enum insn_code icode;
5507   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5508   tree arglist = TREE_OPERAND (exp, 1);
5509   tree arg0;
5510   rtx op0, pat;
5511   enum machine_mode tmode, mode0;
5512   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5513
5514   target = altivec_expand_ld_builtin (exp, target, expandedp);
5515   if (*expandedp)
5516     return target;
5517
5518   target = altivec_expand_st_builtin (exp, target, expandedp);
5519   if (*expandedp)
5520     return target;
5521
5522   target = altivec_expand_dst_builtin (exp, target, expandedp);
5523   if (*expandedp)
5524     return target;
5525
5526   *expandedp = true;
5527
5528   switch (fcode)
5529     {
5530     case ALTIVEC_BUILTIN_STVX:
5531       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx, arglist);
5532     case ALTIVEC_BUILTIN_STVEBX:
5533       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, arglist);
5534     case ALTIVEC_BUILTIN_STVEHX:
5535       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, arglist);
5536     case ALTIVEC_BUILTIN_STVEWX:
5537       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, arglist);
5538     case ALTIVEC_BUILTIN_STVXL:
5539       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl, arglist);
5540
5541     case ALTIVEC_BUILTIN_MFVSCR:
5542       icode = CODE_FOR_altivec_mfvscr;
5543       tmode = insn_data[icode].operand[0].mode;
5544
5545       if (target == 0
5546           || GET_MODE (target) != tmode
5547           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5548         target = gen_reg_rtx (tmode);
5549       
5550       pat = GEN_FCN (icode) (target);
5551       if (! pat)
5552         return 0;
5553       emit_insn (pat);
5554       return target;
5555
5556     case ALTIVEC_BUILTIN_MTVSCR:
5557       icode = CODE_FOR_altivec_mtvscr;
5558       arg0 = TREE_VALUE (arglist);
5559       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5560       mode0 = insn_data[icode].operand[0].mode;
5561
5562       /* If we got invalid arguments bail out before generating bad rtl.  */
5563       if (arg0 == error_mark_node)
5564         return const0_rtx;
5565
5566       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
5567         op0 = copy_to_mode_reg (mode0, op0);
5568
5569       pat = GEN_FCN (icode) (op0);
5570       if (pat)
5571         emit_insn (pat);
5572       return NULL_RTX;
5573
5574     case ALTIVEC_BUILTIN_DSSALL:
5575       emit_insn (gen_altivec_dssall ());
5576       return NULL_RTX;
5577
5578     case ALTIVEC_BUILTIN_DSS:
5579       icode = CODE_FOR_altivec_dss;
5580       arg0 = TREE_VALUE (arglist);
5581       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5582       mode0 = insn_data[icode].operand[0].mode;
5583
5584       /* If we got invalid arguments bail out before generating bad rtl.  */
5585       if (arg0 == error_mark_node)
5586         return const0_rtx;
5587
5588       if (TREE_CODE (arg0) != INTEGER_CST
5589           || TREE_INT_CST_LOW (arg0) & ~0x3)
5590         {
5591           error ("argument to dss must be a 2-bit unsigned literal");
5592           return const0_rtx;
5593         }
5594
5595       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
5596         op0 = copy_to_mode_reg (mode0, op0);
5597
5598       emit_insn (gen_altivec_dss (op0));
5599       return NULL_RTX;
5600     }
5601
5602   /* Expand abs* operations.  */
5603   d = (struct builtin_description *) bdesc_abs;
5604   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
5605     if (d->code == fcode)
5606       return altivec_expand_abs_builtin (d->icode, arglist, target);
5607
5608   /* Expand the AltiVec predicates.  */
5609   dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
5610   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
5611     if (dp->code == fcode)
5612       return altivec_expand_predicate_builtin (dp->icode, dp->opcode, arglist, target);
5613
5614   /* LV* are funky.  We initialized them differently.  */
5615   switch (fcode)
5616     {
5617     case ALTIVEC_BUILTIN_LVSL:
5618       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvsl,
5619                                            arglist, target);
5620     case ALTIVEC_BUILTIN_LVSR:
5621       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvsr,
5622                                           arglist, target);
5623     case ALTIVEC_BUILTIN_LVEBX:
5624       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvebx,
5625                                           arglist, target);
5626     case ALTIVEC_BUILTIN_LVEHX:
5627       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvehx,
5628                                           arglist, target);
5629     case ALTIVEC_BUILTIN_LVEWX:
5630       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvewx,
5631                                           arglist, target);
5632     case ALTIVEC_BUILTIN_LVXL:
5633       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvxl,
5634                                           arglist, target);
5635     case ALTIVEC_BUILTIN_LVX:
5636       return rs6000_expand_binop_builtin (CODE_FOR_altivec_lvx,
5637                                           arglist, target);
5638     default:
5639       break;
5640       /* Fall through.  */
5641     }
5642
5643   *expandedp = false;
5644   return NULL_RTX;
5645 }
5646
5647 /* Binops that need to be initialized manually, but can be expanded
5648    automagically by rs6000_expand_binop_builtin.  */
5649 static struct builtin_description bdesc_2arg_spe[] =
5650 {
5651   { 0, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
5652   { 0, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
5653   { 0, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
5654   { 0, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
5655   { 0, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
5656   { 0, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
5657   { 0, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
5658   { 0, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
5659   { 0, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
5660   { 0, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
5661   { 0, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
5662   { 0, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
5663   { 0, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
5664   { 0, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
5665   { 0, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
5666   { 0, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
5667   { 0, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
5668   { 0, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
5669   { 0, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
5670   { 0, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
5671   { 0, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
5672   { 0, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
5673 };
5674
5675 /* Expand the builtin in EXP and store the result in TARGET.  Store
5676    true in *EXPANDEDP if we found a builtin to expand.
5677
5678    This expands the SPE builtins that are not simple unary and binary
5679    operations.  */
5680 static rtx
5681 spe_expand_builtin (exp, target, expandedp)
5682      tree exp;
5683      rtx target;
5684      bool *expandedp;
5685 {
5686   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5687   tree arglist = TREE_OPERAND (exp, 1);
5688   tree arg1, arg0;
5689   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5690   enum insn_code icode;
5691   enum machine_mode tmode, mode0;
5692   rtx pat, op0;
5693   struct builtin_description *d;
5694   size_t i;
5695
5696   *expandedp = true;
5697
5698   /* Syntax check for a 5-bit unsigned immediate.  */
5699   switch (fcode)
5700     {
5701     case SPE_BUILTIN_EVSTDD:
5702     case SPE_BUILTIN_EVSTDH:
5703     case SPE_BUILTIN_EVSTDW:
5704     case SPE_BUILTIN_EVSTWHE:
5705     case SPE_BUILTIN_EVSTWHO:
5706     case SPE_BUILTIN_EVSTWWE:
5707     case SPE_BUILTIN_EVSTWWO:
5708       arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5709       if (TREE_CODE (arg1) != INTEGER_CST
5710           || TREE_INT_CST_LOW (arg1) & ~0x1f)
5711         {
5712           error ("argument 2 must be a 5-bit unsigned literal");
5713           return const0_rtx;
5714         }
5715       break;
5716     default:
5717       break;
5718     }
5719
5720   d = (struct builtin_description *) bdesc_2arg_spe;
5721   for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
5722     if (d->code == fcode)
5723       return rs6000_expand_binop_builtin (d->icode, arglist, target);
5724
5725   d = (struct builtin_description *) bdesc_spe_predicates;
5726   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
5727     if (d->code == fcode)
5728       return spe_expand_predicate_builtin (d->icode, arglist, target);
5729
5730   d = (struct builtin_description *) bdesc_spe_evsel;
5731   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
5732     if (d->code == fcode)
5733       return spe_expand_evsel_builtin (d->icode, arglist, target);
5734
5735   switch (fcode)
5736     {
5737     case SPE_BUILTIN_EVSTDDX:
5738       return altivec_expand_stv_builtin (CODE_FOR_spe_evstddx, arglist);
5739     case SPE_BUILTIN_EVSTDHX:
5740       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdhx, arglist);
5741     case SPE_BUILTIN_EVSTDWX:
5742       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdwx, arglist);
5743     case SPE_BUILTIN_EVSTWHEX:
5744       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhex, arglist);
5745     case SPE_BUILTIN_EVSTWHOX:
5746       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhox, arglist);
5747     case SPE_BUILTIN_EVSTWWEX:
5748       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwex, arglist);
5749     case SPE_BUILTIN_EVSTWWOX:
5750       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwox, arglist);
5751     case SPE_BUILTIN_EVSTDD:
5752       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdd, arglist);
5753     case SPE_BUILTIN_EVSTDH:
5754       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdh, arglist);
5755     case SPE_BUILTIN_EVSTDW:
5756       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdw, arglist);
5757     case SPE_BUILTIN_EVSTWHE:
5758       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhe, arglist);
5759     case SPE_BUILTIN_EVSTWHO:
5760       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwho, arglist);
5761     case SPE_BUILTIN_EVSTWWE:
5762       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwe, arglist);
5763     case SPE_BUILTIN_EVSTWWO:
5764       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwo, arglist);
5765     case SPE_BUILTIN_MFSPEFSCR:
5766       icode = CODE_FOR_spe_mfspefscr;
5767       tmode = insn_data[icode].operand[0].mode;
5768
5769       if (target == 0
5770           || GET_MODE (target) != tmode
5771           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5772         target = gen_reg_rtx (tmode);
5773       
5774       pat = GEN_FCN (icode) (target);
5775       if (! pat)
5776         return 0;
5777       emit_insn (pat);
5778       return target;
5779     case SPE_BUILTIN_MTSPEFSCR:
5780       icode = CODE_FOR_spe_mtspefscr;
5781       arg0 = TREE_VALUE (arglist);
5782       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5783       mode0 = insn_data[icode].operand[0].mode;
5784
5785       if (arg0 == error_mark_node)
5786         return const0_rtx;
5787
5788       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
5789         op0 = copy_to_mode_reg (mode0, op0);
5790
5791       pat = GEN_FCN (icode) (op0);
5792       if (pat)
5793         emit_insn (pat);
5794       return NULL_RTX;
5795     default:
5796       break;
5797     }
5798
5799   *expandedp = false;
5800   return NULL_RTX;
5801 }
5802
5803 static rtx
5804 spe_expand_predicate_builtin (icode, arglist, target)
5805      enum insn_code icode;
5806      tree arglist;
5807      rtx target;
5808 {
5809   rtx pat, scratch, tmp;
5810   tree form = TREE_VALUE (arglist);
5811   tree arg0 = TREE_VALUE (TREE_CHAIN (arglist));
5812   tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5813   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5814   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5815   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5816   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5817   int form_int;
5818   enum rtx_code code;
5819
5820   if (TREE_CODE (form) != INTEGER_CST)
5821     {
5822       error ("argument 1 of __builtin_spe_predicate must be a constant");
5823       return const0_rtx;
5824     }
5825   else
5826     form_int = TREE_INT_CST_LOW (form);
5827
5828   if (mode0 != mode1)
5829     abort ();
5830
5831   if (arg0 == error_mark_node || arg1 == error_mark_node)
5832     return const0_rtx;
5833
5834   if (target == 0
5835       || GET_MODE (target) != SImode
5836       || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
5837     target = gen_reg_rtx (SImode);
5838
5839   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5840     op0 = copy_to_mode_reg (mode0, op0);
5841   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5842     op1 = copy_to_mode_reg (mode1, op1);
5843
5844   scratch = gen_reg_rtx (CCmode);
5845
5846   pat = GEN_FCN (icode) (scratch, op0, op1);
5847   if (! pat)
5848     return const0_rtx;
5849   emit_insn (pat);
5850
5851   /* There are 4 variants for each predicate: _any_, _all_, _upper_,
5852      _lower_.  We use one compare, but look in different bits of the
5853      CR for each variant.
5854
5855      There are 2 elements in each SPE simd type (upper/lower).  The CR
5856      bits are set as follows:
5857
5858      BIT0  | BIT 1  | BIT 2   | BIT 3
5859      U     |   L    | (U | L) | (U & L)
5860
5861      So, for an "all" relationship, BIT 3 would be set.
5862      For an "any" relationship, BIT 2 would be set.  Etc.
5863
5864      Following traditional nomenclature, these bits map to:
5865
5866      BIT0  | BIT 1  | BIT 2   | BIT 3
5867      LT    | GT     | EQ      | OV
5868
5869      Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
5870   */
5871
5872   switch (form_int)
5873     {
5874       /* All variant.  OV bit.  */
5875     case 0:
5876       /* We need to get to the OV bit, which is the ORDERED bit.  We
5877          could generate (ordered:SI (reg:CC xx) (const_int 0)), but
5878          that's ugly and will trigger a validate_condition_mode abort.
5879          So let's just use another pattern.  */
5880       emit_insn (gen_move_from_CR_ov_bit (target, scratch));
5881       return target;
5882       /* Any variant.  EQ bit.  */
5883     case 1:
5884       code = EQ;
5885       break;
5886       /* Upper variant.  LT bit.  */
5887     case 2:
5888       code = LT;
5889       break;
5890       /* Lower variant.  GT bit.  */
5891     case 3:
5892       code = GT;
5893       break;
5894     default:
5895       error ("argument 1 of __builtin_spe_predicate is out of range");
5896       return const0_rtx;
5897     }
5898
5899   tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
5900   emit_move_insn (target, tmp);
5901
5902   return target;
5903 }
5904
5905 /* The evsel builtins look like this:
5906
5907      e = __builtin_spe_evsel_OP (a, b, c, d);
5908
5909    and work like this:
5910
5911      e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
5912      e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
5913 */
5914
5915 static rtx
5916 spe_expand_evsel_builtin (icode, arglist, target)
5917      enum insn_code icode;
5918      tree arglist;
5919      rtx target;
5920 {
5921   rtx pat, scratch;
5922   tree arg0 = TREE_VALUE (arglist);
5923   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5924   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5925   tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
5926   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5927   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5928   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
5929   rtx op3 = expand_expr (arg3, NULL_RTX, VOIDmode, 0);
5930   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5931   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5932
5933   if (mode0 != mode1)
5934     abort ();
5935
5936   if (arg0 == error_mark_node || arg1 == error_mark_node
5937       || arg2 == error_mark_node || arg3 == error_mark_node)
5938     return const0_rtx;
5939
5940   if (target == 0
5941       || GET_MODE (target) != mode0
5942       || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
5943     target = gen_reg_rtx (mode0);
5944
5945   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5946     op0 = copy_to_mode_reg (mode0, op0);
5947   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
5948     op1 = copy_to_mode_reg (mode0, op1);
5949   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
5950     op2 = copy_to_mode_reg (mode0, op2);
5951   if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
5952     op3 = copy_to_mode_reg (mode0, op3);
5953
5954   /* Generate the compare.  */
5955   scratch = gen_reg_rtx (CCmode);
5956   pat = GEN_FCN (icode) (scratch, op0, op1);
5957   if (! pat)
5958     return const0_rtx;
5959   emit_insn (pat);
5960
5961   if (mode0 == V2SImode)
5962     emit_insn (gen_spe_evsel (target, op2, op3, scratch));
5963   else
5964     emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
5965
5966   return target;
5967 }
5968
5969 /* Expand an expression EXP that calls a built-in function,
5970    with result going to TARGET if that's convenient
5971    (and in mode MODE if that's convenient).
5972    SUBTARGET may be used as the target for computing one of EXP's operands.
5973    IGNORE is nonzero if the value is to be ignored.  */
5974
5975 static rtx
5976 rs6000_expand_builtin (exp, target, subtarget, mode, ignore)
5977      tree exp;
5978      rtx target;
5979      rtx subtarget ATTRIBUTE_UNUSED;
5980      enum machine_mode mode ATTRIBUTE_UNUSED;
5981      int ignore ATTRIBUTE_UNUSED;
5982 {
5983   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5984   tree arglist = TREE_OPERAND (exp, 1);
5985   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5986   struct builtin_description *d;
5987   size_t i;
5988   rtx ret;
5989   bool success;
5990   
5991   if (TARGET_ALTIVEC)
5992     {
5993       ret = altivec_expand_builtin (exp, target, &success);
5994
5995       if (success)
5996         return ret;
5997     }
5998   if (TARGET_SPE)
5999     {
6000       ret = spe_expand_builtin (exp, target, &success);
6001
6002       if (success)
6003         return ret;
6004     }
6005
6006   if (TARGET_ALTIVEC || TARGET_SPE)
6007     {
6008       /* Handle simple unary operations.  */
6009       d = (struct builtin_description *) bdesc_1arg;
6010       for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
6011         if (d->code == fcode)
6012           return rs6000_expand_unop_builtin (d->icode, arglist, target);
6013
6014       /* Handle simple binary operations.  */
6015       d = (struct builtin_description *) bdesc_2arg;
6016       for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
6017         if (d->code == fcode)
6018           return rs6000_expand_binop_builtin (d->icode, arglist, target);
6019
6020       /* Handle simple ternary operations.  */
6021       d = (struct builtin_description *) bdesc_3arg;
6022       for (i = 0; i < ARRAY_SIZE  (bdesc_3arg); i++, d++)
6023         if (d->code == fcode)
6024           return rs6000_expand_ternop_builtin (d->icode, arglist, target);
6025     }
6026
6027   abort ();
6028   return NULL_RTX;
6029 }
6030
6031 static void
6032 rs6000_init_builtins ()
6033 {
6034   opaque_V2SI_type_node = copy_node (V2SI_type_node);
6035   opaque_V2SF_type_node = copy_node (V2SF_type_node);
6036   opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
6037
6038   if (TARGET_SPE)
6039     spe_init_builtins ();
6040   if (TARGET_ALTIVEC)
6041     altivec_init_builtins ();
6042   if (TARGET_ALTIVEC || TARGET_SPE)
6043     rs6000_common_init_builtins ();
6044 }
6045
6046 /* Search through a set of builtins and enable the mask bits.
6047    DESC is an array of builtins.
6048    SIZE is the total number of builtins.
6049    START is the builtin enum at which to start.
6050    END is the builtin enum at which to end.  */
6051 static void
6052 enable_mask_for_builtins (desc, size, start, end)
6053      struct builtin_description *desc;
6054      int size;
6055      enum rs6000_builtins start, end;
6056 {
6057   int i;
6058
6059   for (i = 0; i < size; ++i)
6060     if (desc[i].code == start)
6061       break;
6062
6063   if (i == size)
6064     return;
6065
6066   for (; i < size; ++i)
6067     {
6068       /* Flip all the bits on.  */
6069       desc[i].mask = target_flags;
6070       if (desc[i].code == end)
6071         break;
6072     }
6073 }
6074
6075 static void
6076 spe_init_builtins ()
6077 {
6078   tree endlink = void_list_node;
6079   tree puint_type_node = build_pointer_type (unsigned_type_node);
6080   tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
6081   struct builtin_description *d;
6082   size_t i;
6083
6084   tree v2si_ftype_4_v2si
6085     = build_function_type
6086     (opaque_V2SI_type_node,
6087      tree_cons (NULL_TREE, opaque_V2SI_type_node,
6088                 tree_cons (NULL_TREE, opaque_V2SI_type_node,
6089                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6090                                       tree_cons (NULL_TREE, opaque_V2SI_type_node,
6091                                                  endlink)))));
6092
6093   tree v2sf_ftype_4_v2sf
6094     = build_function_type
6095     (opaque_V2SF_type_node,
6096      tree_cons (NULL_TREE, opaque_V2SF_type_node,
6097                 tree_cons (NULL_TREE, opaque_V2SF_type_node,
6098                            tree_cons (NULL_TREE, opaque_V2SF_type_node,
6099                                       tree_cons (NULL_TREE, opaque_V2SF_type_node,
6100                                                  endlink)))));
6101
6102   tree int_ftype_int_v2si_v2si
6103     = build_function_type
6104     (integer_type_node,
6105      tree_cons (NULL_TREE, integer_type_node,
6106                 tree_cons (NULL_TREE, opaque_V2SI_type_node,
6107                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6108                                       endlink))));
6109
6110   tree int_ftype_int_v2sf_v2sf
6111     = build_function_type
6112     (integer_type_node,
6113      tree_cons (NULL_TREE, integer_type_node,
6114                 tree_cons (NULL_TREE, opaque_V2SF_type_node,
6115                            tree_cons (NULL_TREE, opaque_V2SF_type_node,
6116                                       endlink))));
6117
6118   tree void_ftype_v2si_puint_int
6119     = build_function_type (void_type_node,
6120                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6121                                       tree_cons (NULL_TREE, puint_type_node,
6122                                                  tree_cons (NULL_TREE,
6123                                                             integer_type_node,
6124                                                             endlink))));
6125
6126   tree void_ftype_v2si_puint_char
6127     = build_function_type (void_type_node,
6128                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6129                                       tree_cons (NULL_TREE, puint_type_node,
6130                                                  tree_cons (NULL_TREE,
6131                                                             char_type_node,
6132                                                             endlink))));
6133
6134   tree void_ftype_v2si_pv2si_int
6135     = build_function_type (void_type_node,
6136                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6137                                       tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
6138                                                  tree_cons (NULL_TREE,
6139                                                             integer_type_node,
6140                                                             endlink))));
6141
6142   tree void_ftype_v2si_pv2si_char
6143     = build_function_type (void_type_node,
6144                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6145                                       tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
6146                                                  tree_cons (NULL_TREE,
6147                                                             char_type_node,
6148                                                             endlink))));
6149
6150   tree void_ftype_int
6151     = build_function_type (void_type_node,
6152                            tree_cons (NULL_TREE, integer_type_node, endlink));
6153
6154   tree int_ftype_void
6155     = build_function_type (integer_type_node, endlink);
6156
6157   tree v2si_ftype_pv2si_int
6158     = build_function_type (opaque_V2SI_type_node,
6159                            tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
6160                                       tree_cons (NULL_TREE, integer_type_node,
6161                                                  endlink)));
6162
6163   tree v2si_ftype_puint_int
6164     = build_function_type (opaque_V2SI_type_node,
6165                            tree_cons (NULL_TREE, puint_type_node,
6166                                       tree_cons (NULL_TREE, integer_type_node,
6167                                                  endlink)));
6168
6169   tree v2si_ftype_pushort_int
6170     = build_function_type (opaque_V2SI_type_node,
6171                            tree_cons (NULL_TREE, pushort_type_node,
6172                                       tree_cons (NULL_TREE, integer_type_node,
6173                                                  endlink)));
6174
6175   /* The initialization of the simple binary and unary builtins is
6176      done in rs6000_common_init_builtins, but we have to enable the
6177      mask bits here manually because we have run out of `target_flags'
6178      bits.  We really need to redesign this mask business.  */
6179
6180   enable_mask_for_builtins ((struct builtin_description *) bdesc_2arg,
6181                             ARRAY_SIZE (bdesc_2arg),
6182                             SPE_BUILTIN_EVADDW,
6183                             SPE_BUILTIN_EVXOR);
6184   enable_mask_for_builtins ((struct builtin_description *) bdesc_1arg,
6185                             ARRAY_SIZE (bdesc_1arg),
6186                             SPE_BUILTIN_EVABS,
6187                             SPE_BUILTIN_EVSUBFUSIAAW);
6188   enable_mask_for_builtins ((struct builtin_description *) bdesc_spe_predicates,
6189                             ARRAY_SIZE (bdesc_spe_predicates),
6190                             SPE_BUILTIN_EVCMPEQ,
6191                             SPE_BUILTIN_EVFSTSTLT);
6192   enable_mask_for_builtins ((struct builtin_description *) bdesc_spe_evsel,
6193                             ARRAY_SIZE (bdesc_spe_evsel),
6194                             SPE_BUILTIN_EVSEL_CMPGTS,
6195                             SPE_BUILTIN_EVSEL_FSTSTEQ);
6196
6197   /* Initialize irregular SPE builtins.  */
6198   
6199   def_builtin (target_flags, "__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
6200   def_builtin (target_flags, "__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
6201   def_builtin (target_flags, "__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
6202   def_builtin (target_flags, "__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
6203   def_builtin (target_flags, "__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
6204   def_builtin (target_flags, "__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
6205   def_builtin (target_flags, "__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
6206   def_builtin (target_flags, "__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
6207   def_builtin (target_flags, "__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
6208   def_builtin (target_flags, "__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
6209   def_builtin (target_flags, "__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
6210   def_builtin (target_flags, "__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
6211   def_builtin (target_flags, "__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
6212   def_builtin (target_flags, "__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
6213   def_builtin (target_flags, "__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
6214   def_builtin (target_flags, "__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
6215
6216   /* Loads.  */
6217   def_builtin (target_flags, "__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
6218   def_builtin (target_flags, "__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
6219   def_builtin (target_flags, "__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
6220   def_builtin (target_flags, "__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
6221   def_builtin (target_flags, "__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
6222   def_builtin (target_flags, "__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
6223   def_builtin (target_flags, "__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
6224   def_builtin (target_flags, "__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
6225   def_builtin (target_flags, "__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
6226   def_builtin (target_flags, "__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
6227   def_builtin (target_flags, "__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
6228   def_builtin (target_flags, "__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
6229   def_builtin (target_flags, "__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
6230   def_builtin (target_flags, "__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
6231   def_builtin (target_flags, "__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
6232   def_builtin (target_flags, "__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
6233   def_builtin (target_flags, "__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
6234   def_builtin (target_flags, "__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
6235   def_builtin (target_flags, "__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
6236   def_builtin (target_flags, "__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
6237   def_builtin (target_flags, "__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
6238   def_builtin (target_flags, "__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
6239
6240   /* Predicates.  */
6241   d = (struct builtin_description *) bdesc_spe_predicates;
6242   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
6243     {
6244       tree type;
6245
6246       switch (insn_data[d->icode].operand[1].mode)
6247         {
6248         case V2SImode:
6249           type = int_ftype_int_v2si_v2si;
6250           break;
6251         case V2SFmode:
6252           type = int_ftype_int_v2sf_v2sf;
6253           break;
6254         default:
6255           abort ();
6256         }
6257
6258       def_builtin (d->mask, d->name, type, d->code);
6259     }
6260
6261   /* Evsel predicates.  */
6262   d = (struct builtin_description *) bdesc_spe_evsel;
6263   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
6264     {
6265       tree type;
6266
6267       switch (insn_data[d->icode].operand[1].mode)
6268         {
6269         case V2SImode:
6270           type = v2si_ftype_4_v2si;
6271           break;
6272         case V2SFmode:
6273           type = v2sf_ftype_4_v2sf;
6274           break;
6275         default:
6276           abort ();
6277         }
6278
6279       def_builtin (d->mask, d->name, type, d->code);
6280     }
6281 }
6282
6283 static void
6284 altivec_init_builtins ()
6285 {
6286   struct builtin_description *d;
6287   struct builtin_description_predicates *dp;
6288   size_t i;
6289   tree pfloat_type_node = build_pointer_type (float_type_node);
6290   tree pint_type_node = build_pointer_type (integer_type_node);
6291   tree pshort_type_node = build_pointer_type (short_integer_type_node);
6292   tree pchar_type_node = build_pointer_type (char_type_node);
6293
6294   tree pvoid_type_node = build_pointer_type (void_type_node);
6295
6296   tree pcfloat_type_node = build_pointer_type (build_qualified_type (float_type_node, TYPE_QUAL_CONST));
6297   tree pcint_type_node = build_pointer_type (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
6298   tree pcshort_type_node = build_pointer_type (build_qualified_type (short_integer_type_node, TYPE_QUAL_CONST));
6299   tree pcchar_type_node = build_pointer_type (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
6300
6301   tree pcvoid_type_node = build_pointer_type (build_qualified_type (void_type_node, TYPE_QUAL_CONST));
6302
6303   tree int_ftype_int_v4si_v4si
6304     = build_function_type_list (integer_type_node,
6305                                 integer_type_node, V4SI_type_node,
6306                                 V4SI_type_node, NULL_TREE);
6307   tree v4sf_ftype_pcfloat
6308     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
6309   tree void_ftype_pfloat_v4sf
6310     = build_function_type_list (void_type_node,
6311                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
6312   tree v4si_ftype_pcint
6313     = build_function_type_list (V4SI_type_node, pcint_type_node, NULL_TREE);
6314   tree void_ftype_pint_v4si
6315     = build_function_type_list (void_type_node,
6316                                 pint_type_node, V4SI_type_node, NULL_TREE);
6317   tree v8hi_ftype_pcshort
6318     = build_function_type_list (V8HI_type_node, pcshort_type_node, NULL_TREE);
6319   tree void_ftype_pshort_v8hi
6320     = build_function_type_list (void_type_node,
6321                                 pshort_type_node, V8HI_type_node, NULL_TREE);
6322   tree v16qi_ftype_pcchar
6323     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
6324   tree void_ftype_pchar_v16qi
6325     = build_function_type_list (void_type_node,
6326                                 pchar_type_node, V16QI_type_node, NULL_TREE);
6327   tree void_ftype_v4si
6328     = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
6329   tree v8hi_ftype_void
6330     = build_function_type (V8HI_type_node, void_list_node);
6331   tree void_ftype_void
6332     = build_function_type (void_type_node, void_list_node);
6333   tree void_ftype_qi
6334     = build_function_type_list (void_type_node, char_type_node, NULL_TREE);
6335
6336   tree v16qi_ftype_int_pcvoid
6337     = build_function_type_list (V16QI_type_node,
6338                                 integer_type_node, pcvoid_type_node, NULL_TREE);
6339   tree v8hi_ftype_int_pcvoid
6340     = build_function_type_list (V8HI_type_node,
6341                                 integer_type_node, pcvoid_type_node, NULL_TREE);
6342   tree v4si_ftype_int_pcvoid
6343     = build_function_type_list (V4SI_type_node,
6344                                 integer_type_node, pcvoid_type_node, NULL_TREE);
6345
6346   tree void_ftype_v4si_int_pvoid
6347     = build_function_type_list (void_type_node,
6348                                 V4SI_type_node, integer_type_node,
6349                                 pvoid_type_node, NULL_TREE);
6350   tree void_ftype_v16qi_int_pvoid
6351     = build_function_type_list (void_type_node,
6352                                 V16QI_type_node, integer_type_node,
6353                                 pvoid_type_node, NULL_TREE);
6354   tree void_ftype_v8hi_int_pvoid
6355     = build_function_type_list (void_type_node,
6356                                 V8HI_type_node, integer_type_node,
6357                                 pvoid_type_node, NULL_TREE);
6358   tree int_ftype_int_v8hi_v8hi
6359     = build_function_type_list (integer_type_node,
6360                                 integer_type_node, V8HI_type_node,
6361                                 V8HI_type_node, NULL_TREE);
6362   tree int_ftype_int_v16qi_v16qi
6363     = build_function_type_list (integer_type_node,
6364                                 integer_type_node, V16QI_type_node,
6365                                 V16QI_type_node, NULL_TREE);
6366   tree int_ftype_int_v4sf_v4sf
6367     = build_function_type_list (integer_type_node,
6368                                 integer_type_node, V4SF_type_node,
6369                                 V4SF_type_node, NULL_TREE);
6370   tree v4si_ftype_v4si
6371     = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
6372   tree v8hi_ftype_v8hi
6373     = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
6374   tree v16qi_ftype_v16qi
6375     = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
6376   tree v4sf_ftype_v4sf
6377     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
6378   tree void_ftype_pcvoid_int_char
6379     = build_function_type_list (void_type_node,
6380                                 pcvoid_type_node, integer_type_node,
6381                                 char_type_node, NULL_TREE);
6382   
6383   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4sf", v4sf_ftype_pcfloat,
6384                ALTIVEC_BUILTIN_LD_INTERNAL_4sf);
6385   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4sf", void_ftype_pfloat_v4sf,
6386                ALTIVEC_BUILTIN_ST_INTERNAL_4sf);
6387   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4si", v4si_ftype_pcint,
6388                ALTIVEC_BUILTIN_LD_INTERNAL_4si);
6389   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4si", void_ftype_pint_v4si,
6390                ALTIVEC_BUILTIN_ST_INTERNAL_4si);
6391   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_8hi", v8hi_ftype_pcshort,
6392                ALTIVEC_BUILTIN_LD_INTERNAL_8hi);
6393   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_8hi", void_ftype_pshort_v8hi,
6394                ALTIVEC_BUILTIN_ST_INTERNAL_8hi);
6395   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_16qi", v16qi_ftype_pcchar,
6396                ALTIVEC_BUILTIN_LD_INTERNAL_16qi);
6397   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_16qi", void_ftype_pchar_v16qi,
6398                ALTIVEC_BUILTIN_ST_INTERNAL_16qi);
6399   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
6400   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
6401   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
6402   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dss", void_ftype_qi, ALTIVEC_BUILTIN_DSS);
6403   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsl", v16qi_ftype_int_pcvoid, ALTIVEC_BUILTIN_LVSL);
6404   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsr", v16qi_ftype_int_pcvoid, ALTIVEC_BUILTIN_LVSR);
6405   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvebx", v16qi_ftype_int_pcvoid, ALTIVEC_BUILTIN_LVEBX);
6406   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvehx", v8hi_ftype_int_pcvoid, ALTIVEC_BUILTIN_LVEHX);
6407   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvewx", v4si_ftype_int_pcvoid, ALTIVEC_BUILTIN_LVEWX);
6408   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvxl", v4si_ftype_int_pcvoid, ALTIVEC_BUILTIN_LVXL);
6409   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvx", v4si_ftype_int_pcvoid, ALTIVEC_BUILTIN_LVX);
6410   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvx", void_ftype_v4si_int_pvoid, ALTIVEC_BUILTIN_STVX);
6411   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvewx", void_ftype_v4si_int_pvoid, ALTIVEC_BUILTIN_STVEWX);
6412   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvxl", void_ftype_v4si_int_pvoid, ALTIVEC_BUILTIN_STVXL);
6413   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvebx", void_ftype_v16qi_int_pvoid, ALTIVEC_BUILTIN_STVEBX);
6414   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvehx", void_ftype_v8hi_int_pvoid, ALTIVEC_BUILTIN_STVEHX);
6415
6416   /* Add the DST variants.  */
6417   d = (struct builtin_description *) bdesc_dst;
6418   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
6419     def_builtin (d->mask, d->name, void_ftype_pcvoid_int_char, d->code);
6420
6421   /* Initialize the predicates.  */
6422   dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
6423   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
6424     {
6425       enum machine_mode mode1;
6426       tree type;
6427
6428       mode1 = insn_data[dp->icode].operand[1].mode;
6429
6430       switch (mode1)
6431         {
6432         case V4SImode:
6433           type = int_ftype_int_v4si_v4si;
6434           break;
6435         case V8HImode:
6436           type = int_ftype_int_v8hi_v8hi;
6437           break;
6438         case V16QImode:
6439           type = int_ftype_int_v16qi_v16qi;
6440           break;
6441         case V4SFmode:
6442           type = int_ftype_int_v4sf_v4sf;
6443           break;
6444         default:
6445           abort ();
6446         }
6447       
6448       def_builtin (dp->mask, dp->name, type, dp->code);
6449     }
6450
6451   /* Initialize the abs* operators.  */
6452   d = (struct builtin_description *) bdesc_abs;
6453   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
6454     {
6455       enum machine_mode mode0;
6456       tree type;
6457
6458       mode0 = insn_data[d->icode].operand[0].mode;
6459
6460       switch (mode0)
6461         {
6462         case V4SImode:
6463           type = v4si_ftype_v4si;
6464           break;
6465         case V8HImode:
6466           type = v8hi_ftype_v8hi;
6467           break;
6468         case V16QImode:
6469           type = v16qi_ftype_v16qi;
6470           break;
6471         case V4SFmode:
6472           type = v4sf_ftype_v4sf;
6473           break;
6474         default:
6475           abort ();
6476         }
6477       
6478       def_builtin (d->mask, d->name, type, d->code);
6479     }
6480 }
6481
6482 static void
6483 rs6000_common_init_builtins ()
6484 {
6485   struct builtin_description *d;
6486   size_t i;
6487
6488   tree v4sf_ftype_v4sf_v4sf_v16qi
6489     = build_function_type_list (V4SF_type_node,
6490                                 V4SF_type_node, V4SF_type_node,
6491                                 V16QI_type_node, NULL_TREE);
6492   tree v4si_ftype_v4si_v4si_v16qi
6493     = build_function_type_list (V4SI_type_node,
6494                                 V4SI_type_node, V4SI_type_node,
6495                                 V16QI_type_node, NULL_TREE);
6496   tree v8hi_ftype_v8hi_v8hi_v16qi
6497     = build_function_type_list (V8HI_type_node,
6498                                 V8HI_type_node, V8HI_type_node,
6499                                 V16QI_type_node, NULL_TREE);
6500   tree v16qi_ftype_v16qi_v16qi_v16qi
6501     = build_function_type_list (V16QI_type_node,
6502                                 V16QI_type_node, V16QI_type_node,
6503                                 V16QI_type_node, NULL_TREE);
6504   tree v4si_ftype_char
6505     = build_function_type_list (V4SI_type_node, char_type_node, NULL_TREE);
6506   tree v8hi_ftype_char
6507     = build_function_type_list (V8HI_type_node, char_type_node, NULL_TREE);
6508   tree v16qi_ftype_char
6509     = build_function_type_list (V16QI_type_node, char_type_node, NULL_TREE);
6510   tree v8hi_ftype_v16qi
6511     = build_function_type_list (V8HI_type_node, V16QI_type_node, NULL_TREE);
6512   tree v4sf_ftype_v4sf
6513     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
6514
6515   tree v2si_ftype_v2si_v2si
6516     = build_function_type_list (opaque_V2SI_type_node,
6517                                 opaque_V2SI_type_node,
6518                                 opaque_V2SI_type_node, NULL_TREE);
6519
6520   tree v2sf_ftype_v2sf_v2sf
6521     = build_function_type_list (opaque_V2SF_type_node,
6522                                 opaque_V2SF_type_node,
6523                                 opaque_V2SF_type_node, NULL_TREE);
6524
6525   tree v2si_ftype_int_int
6526     = build_function_type_list (opaque_V2SI_type_node,
6527                                 integer_type_node, integer_type_node,
6528                                 NULL_TREE);
6529
6530   tree v2si_ftype_v2si
6531     = build_function_type_list (opaque_V2SI_type_node,
6532                                 opaque_V2SI_type_node, NULL_TREE);
6533
6534   tree v2sf_ftype_v2sf
6535     = build_function_type_list (opaque_V2SF_type_node,
6536                                 opaque_V2SF_type_node, NULL_TREE);
6537   
6538   tree v2sf_ftype_v2si
6539     = build_function_type_list (opaque_V2SF_type_node,
6540                                 opaque_V2SI_type_node, NULL_TREE);
6541
6542   tree v2si_ftype_v2sf
6543     = build_function_type_list (opaque_V2SI_type_node,
6544                                 opaque_V2SF_type_node, NULL_TREE);
6545
6546   tree v2si_ftype_v2si_char
6547     = build_function_type_list (opaque_V2SI_type_node,
6548                                 opaque_V2SI_type_node,
6549                                 char_type_node, NULL_TREE);
6550
6551   tree v2si_ftype_int_char
6552     = build_function_type_list (opaque_V2SI_type_node,
6553                                 integer_type_node, char_type_node, NULL_TREE);
6554
6555   tree v2si_ftype_char
6556     = build_function_type_list (opaque_V2SI_type_node,
6557                                 char_type_node, NULL_TREE);
6558
6559   tree int_ftype_int_int
6560     = build_function_type_list (integer_type_node,
6561                                 integer_type_node, integer_type_node,
6562                                 NULL_TREE);
6563
6564   tree v4si_ftype_v4si_v4si
6565     = build_function_type_list (V4SI_type_node,
6566                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
6567   tree v4sf_ftype_v4si_char
6568     = build_function_type_list (V4SF_type_node,
6569                                 V4SI_type_node, char_type_node, NULL_TREE);
6570   tree v4si_ftype_v4sf_char
6571     = build_function_type_list (V4SI_type_node,
6572                                 V4SF_type_node, char_type_node, NULL_TREE);
6573   tree v4si_ftype_v4si_char
6574     = build_function_type_list (V4SI_type_node,
6575                                 V4SI_type_node, char_type_node, NULL_TREE);
6576   tree v8hi_ftype_v8hi_char
6577     = build_function_type_list (V8HI_type_node,
6578                                 V8HI_type_node, char_type_node, NULL_TREE);
6579   tree v16qi_ftype_v16qi_char
6580     = build_function_type_list (V16QI_type_node,
6581                                 V16QI_type_node, char_type_node, NULL_TREE);
6582   tree v16qi_ftype_v16qi_v16qi_char
6583     = build_function_type_list (V16QI_type_node,
6584                                 V16QI_type_node, V16QI_type_node,
6585                                 char_type_node, NULL_TREE);
6586   tree v8hi_ftype_v8hi_v8hi_char
6587     = build_function_type_list (V8HI_type_node,
6588                                 V8HI_type_node, V8HI_type_node,
6589                                 char_type_node, NULL_TREE);
6590   tree v4si_ftype_v4si_v4si_char
6591     = build_function_type_list (V4SI_type_node,
6592                                 V4SI_type_node, V4SI_type_node,
6593                                 char_type_node, NULL_TREE);
6594   tree v4sf_ftype_v4sf_v4sf_char
6595     = build_function_type_list (V4SF_type_node,
6596                                 V4SF_type_node, V4SF_type_node,
6597                                 char_type_node, NULL_TREE);
6598   tree v4sf_ftype_v4sf_v4sf
6599     = build_function_type_list (V4SF_type_node,
6600                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
6601   tree v4sf_ftype_v4sf_v4sf_v4si
6602     = build_function_type_list (V4SF_type_node,
6603                                 V4SF_type_node, V4SF_type_node,
6604                                 V4SI_type_node, NULL_TREE);
6605   tree v4sf_ftype_v4sf_v4sf_v4sf
6606     = build_function_type_list (V4SF_type_node,
6607                                 V4SF_type_node, V4SF_type_node,
6608                                 V4SF_type_node, NULL_TREE);
6609   tree v4si_ftype_v4si_v4si_v4si 
6610     = build_function_type_list (V4SI_type_node,
6611                                 V4SI_type_node, V4SI_type_node,
6612                                 V4SI_type_node, NULL_TREE);
6613   tree v8hi_ftype_v8hi_v8hi
6614     = build_function_type_list (V8HI_type_node,
6615                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
6616   tree v8hi_ftype_v8hi_v8hi_v8hi
6617     = build_function_type_list (V8HI_type_node,
6618                                 V8HI_type_node, V8HI_type_node,
6619                                 V8HI_type_node, NULL_TREE);
6620  tree v4si_ftype_v8hi_v8hi_v4si
6621     = build_function_type_list (V4SI_type_node,
6622                                 V8HI_type_node, V8HI_type_node,
6623                                 V4SI_type_node, NULL_TREE);
6624  tree v4si_ftype_v16qi_v16qi_v4si
6625     = build_function_type_list (V4SI_type_node,
6626                                 V16QI_type_node, V16QI_type_node,
6627                                 V4SI_type_node, NULL_TREE);
6628   tree v16qi_ftype_v16qi_v16qi
6629     = build_function_type_list (V16QI_type_node,
6630                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
6631   tree v4si_ftype_v4sf_v4sf
6632     = build_function_type_list (V4SI_type_node,
6633                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
6634   tree v8hi_ftype_v16qi_v16qi
6635     = build_function_type_list (V8HI_type_node,
6636                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
6637   tree v4si_ftype_v8hi_v8hi
6638     = build_function_type_list (V4SI_type_node,
6639                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
6640   tree v8hi_ftype_v4si_v4si
6641     = build_function_type_list (V8HI_type_node,
6642                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
6643   tree v16qi_ftype_v8hi_v8hi
6644     = build_function_type_list (V16QI_type_node,
6645                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
6646   tree v4si_ftype_v16qi_v4si
6647     = build_function_type_list (V4SI_type_node,
6648                                 V16QI_type_node, V4SI_type_node, NULL_TREE);
6649   tree v4si_ftype_v16qi_v16qi
6650     = build_function_type_list (V4SI_type_node,
6651                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
6652   tree v4si_ftype_v8hi_v4si
6653     = build_function_type_list (V4SI_type_node,
6654                                 V8HI_type_node, V4SI_type_node, NULL_TREE);
6655   tree v4si_ftype_v8hi
6656     = build_function_type_list (V4SI_type_node, V8HI_type_node, NULL_TREE);
6657   tree int_ftype_v4si_v4si
6658     = build_function_type_list (integer_type_node,
6659                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
6660   tree int_ftype_v4sf_v4sf
6661     = build_function_type_list (integer_type_node,
6662                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
6663   tree int_ftype_v16qi_v16qi
6664     = build_function_type_list (integer_type_node,
6665                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
6666   tree int_ftype_v8hi_v8hi
6667     = build_function_type_list (integer_type_node,
6668                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
6669
6670   /* Add the simple ternary operators.  */
6671   d = (struct builtin_description *) bdesc_3arg;
6672   for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
6673     {
6674       
6675       enum machine_mode mode0, mode1, mode2, mode3;
6676       tree type;
6677
6678       if (d->name == 0 || d->icode == CODE_FOR_nothing)
6679         continue;
6680       
6681       mode0 = insn_data[d->icode].operand[0].mode;
6682       mode1 = insn_data[d->icode].operand[1].mode;
6683       mode2 = insn_data[d->icode].operand[2].mode;
6684       mode3 = insn_data[d->icode].operand[3].mode;
6685       
6686       /* When all four are of the same mode.  */
6687       if (mode0 == mode1 && mode1 == mode2 && mode2 == mode3)
6688         {
6689           switch (mode0)
6690             {
6691             case V4SImode:
6692               type = v4si_ftype_v4si_v4si_v4si;
6693               break;
6694             case V4SFmode:
6695               type = v4sf_ftype_v4sf_v4sf_v4sf;
6696               break;
6697             case V8HImode:
6698               type = v8hi_ftype_v8hi_v8hi_v8hi;
6699               break;          
6700             case V16QImode:
6701               type = v16qi_ftype_v16qi_v16qi_v16qi;
6702               break;          
6703             default:
6704               abort();        
6705             }
6706         }
6707       else if (mode0 == mode1 && mode1 == mode2 && mode3 == V16QImode)
6708         {
6709           switch (mode0)
6710             {
6711             case V4SImode:
6712               type = v4si_ftype_v4si_v4si_v16qi;
6713               break;
6714             case V4SFmode:
6715               type = v4sf_ftype_v4sf_v4sf_v16qi;
6716               break;
6717             case V8HImode:
6718               type = v8hi_ftype_v8hi_v8hi_v16qi;
6719               break;          
6720             case V16QImode:
6721               type = v16qi_ftype_v16qi_v16qi_v16qi;
6722               break;          
6723             default:
6724               abort();        
6725             }
6726         }
6727       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode 
6728                && mode3 == V4SImode)
6729         type = v4si_ftype_v16qi_v16qi_v4si;
6730       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode 
6731                && mode3 == V4SImode)
6732         type = v4si_ftype_v8hi_v8hi_v4si;
6733       else if (mode0 == V4SFmode && mode1 == V4SFmode && mode2 == V4SFmode 
6734                && mode3 == V4SImode)
6735         type = v4sf_ftype_v4sf_v4sf_v4si;
6736
6737       /* vchar, vchar, vchar, 4 bit literal.  */
6738       else if (mode0 == V16QImode && mode1 == mode0 && mode2 == mode0
6739                && mode3 == QImode)
6740         type = v16qi_ftype_v16qi_v16qi_char;
6741
6742       /* vshort, vshort, vshort, 4 bit literal.  */
6743       else if (mode0 == V8HImode && mode1 == mode0 && mode2 == mode0
6744                && mode3 == QImode)
6745         type = v8hi_ftype_v8hi_v8hi_char;
6746
6747       /* vint, vint, vint, 4 bit literal.  */
6748       else if (mode0 == V4SImode && mode1 == mode0 && mode2 == mode0
6749                && mode3 == QImode)
6750         type = v4si_ftype_v4si_v4si_char;
6751
6752       /* vfloat, vfloat, vfloat, 4 bit literal.  */
6753       else if (mode0 == V4SFmode && mode1 == mode0 && mode2 == mode0
6754                && mode3 == QImode)
6755         type = v4sf_ftype_v4sf_v4sf_char;
6756
6757       else
6758         abort ();
6759
6760       def_builtin (d->mask, d->name, type, d->code);
6761     }
6762
6763   /* Add the simple binary operators.  */
6764   d = (struct builtin_description *) bdesc_2arg;
6765   for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
6766     {
6767       enum machine_mode mode0, mode1, mode2;
6768       tree type;
6769
6770       if (d->name == 0 || d->icode == CODE_FOR_nothing)
6771         continue;
6772       
6773       mode0 = insn_data[d->icode].operand[0].mode;
6774       mode1 = insn_data[d->icode].operand[1].mode;
6775       mode2 = insn_data[d->icode].operand[2].mode;
6776
6777       /* When all three operands are of the same mode.  */
6778       if (mode0 == mode1 && mode1 == mode2)
6779         {
6780           switch (mode0)
6781             {
6782             case V4SFmode:
6783               type = v4sf_ftype_v4sf_v4sf;
6784               break;
6785             case V4SImode:
6786               type = v4si_ftype_v4si_v4si;
6787               break;
6788             case V16QImode:
6789               type = v16qi_ftype_v16qi_v16qi;
6790               break;
6791             case V8HImode:
6792               type = v8hi_ftype_v8hi_v8hi;
6793               break;
6794             case V2SImode:
6795               type = v2si_ftype_v2si_v2si;
6796               break;
6797             case V2SFmode:
6798               type = v2sf_ftype_v2sf_v2sf;
6799               break;
6800             case SImode:
6801               type = int_ftype_int_int;
6802               break;
6803             default:
6804               abort ();
6805             }
6806         }
6807
6808       /* A few other combos we really don't want to do manually.  */
6809
6810       /* vint, vfloat, vfloat.  */
6811       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == V4SFmode)
6812         type = v4si_ftype_v4sf_v4sf;
6813
6814       /* vshort, vchar, vchar.  */
6815       else if (mode0 == V8HImode && mode1 == V16QImode && mode2 == V16QImode)
6816         type = v8hi_ftype_v16qi_v16qi;
6817
6818       /* vint, vshort, vshort.  */
6819       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode)
6820         type = v4si_ftype_v8hi_v8hi;
6821
6822       /* vshort, vint, vint.  */
6823       else if (mode0 == V8HImode && mode1 == V4SImode && mode2 == V4SImode)
6824         type = v8hi_ftype_v4si_v4si;
6825
6826       /* vchar, vshort, vshort.  */
6827       else if (mode0 == V16QImode && mode1 == V8HImode && mode2 == V8HImode)
6828         type = v16qi_ftype_v8hi_v8hi;
6829
6830       /* vint, vchar, vint.  */
6831       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V4SImode)
6832         type = v4si_ftype_v16qi_v4si;
6833
6834       /* vint, vchar, vchar.  */
6835       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode)
6836         type = v4si_ftype_v16qi_v16qi;
6837
6838       /* vint, vshort, vint.  */
6839       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V4SImode)
6840         type = v4si_ftype_v8hi_v4si;
6841       
6842       /* vint, vint, 5 bit literal.  */
6843       else if (mode0 == V4SImode && mode1 == V4SImode && mode2 == QImode)
6844         type = v4si_ftype_v4si_char;
6845       
6846       /* vshort, vshort, 5 bit literal.  */
6847       else if (mode0 == V8HImode && mode1 == V8HImode && mode2 == QImode)
6848         type = v8hi_ftype_v8hi_char;
6849       
6850       /* vchar, vchar, 5 bit literal.  */
6851       else if (mode0 == V16QImode && mode1 == V16QImode && mode2 == QImode)
6852         type = v16qi_ftype_v16qi_char;
6853
6854       /* vfloat, vint, 5 bit literal.  */
6855       else if (mode0 == V4SFmode && mode1 == V4SImode && mode2 == QImode)
6856         type = v4sf_ftype_v4si_char;
6857       
6858       /* vint, vfloat, 5 bit literal.  */
6859       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == QImode)
6860         type = v4si_ftype_v4sf_char;
6861
6862       else if (mode0 == V2SImode && mode1 == SImode && mode2 == SImode)
6863         type = v2si_ftype_int_int;
6864
6865       else if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
6866         type = v2si_ftype_v2si_char;
6867
6868       else if (mode0 == V2SImode && mode1 == SImode && mode2 == QImode)
6869         type = v2si_ftype_int_char;
6870
6871       /* int, x, x.  */
6872       else if (mode0 == SImode)
6873         {
6874           switch (mode1)
6875             {
6876             case V4SImode:
6877               type = int_ftype_v4si_v4si;
6878               break;
6879             case V4SFmode:
6880               type = int_ftype_v4sf_v4sf;
6881               break;
6882             case V16QImode:
6883               type = int_ftype_v16qi_v16qi;
6884               break;
6885             case V8HImode:
6886               type = int_ftype_v8hi_v8hi;
6887               break;
6888             default:
6889               abort ();
6890             }
6891         }
6892
6893       else
6894         abort ();
6895
6896       def_builtin (d->mask, d->name, type, d->code);
6897     }
6898
6899   /* Add the simple unary operators.  */
6900   d = (struct builtin_description *) bdesc_1arg;
6901   for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
6902     {
6903       enum machine_mode mode0, mode1;
6904       tree type;
6905
6906       if (d->name == 0 || d->icode == CODE_FOR_nothing)
6907         continue;
6908       
6909       mode0 = insn_data[d->icode].operand[0].mode;
6910       mode1 = insn_data[d->icode].operand[1].mode;
6911
6912       if (mode0 == V4SImode && mode1 == QImode)
6913         type = v4si_ftype_char;
6914       else if (mode0 == V8HImode && mode1 == QImode)
6915         type = v8hi_ftype_char;
6916       else if (mode0 == V16QImode && mode1 == QImode)
6917         type = v16qi_ftype_char;
6918       else if (mode0 == V4SFmode && mode1 == V4SFmode)
6919         type = v4sf_ftype_v4sf;
6920       else if (mode0 == V8HImode && mode1 == V16QImode)
6921         type = v8hi_ftype_v16qi;
6922       else if (mode0 == V4SImode && mode1 == V8HImode)
6923         type = v4si_ftype_v8hi;
6924       else if (mode0 == V2SImode && mode1 == V2SImode)
6925         type = v2si_ftype_v2si;
6926       else if (mode0 == V2SFmode && mode1 == V2SFmode)
6927         type = v2sf_ftype_v2sf;
6928       else if (mode0 == V2SFmode && mode1 == V2SImode)
6929         type = v2sf_ftype_v2si;
6930       else if (mode0 == V2SImode && mode1 == V2SFmode)
6931         type = v2si_ftype_v2sf;
6932       else if (mode0 == V2SImode && mode1 == QImode)
6933         type = v2si_ftype_char;
6934       else
6935         abort ();
6936
6937       def_builtin (d->mask, d->name, type, d->code);
6938     }
6939 }
6940
6941 \f
6942 /* Expand a block move operation, and return 1 if successful.  Return 0
6943    if we should let the compiler generate normal code.
6944
6945    operands[0] is the destination
6946    operands[1] is the source
6947    operands[2] is the length
6948    operands[3] is the alignment */
6949
6950 #define MAX_MOVE_REG 4
6951
6952 int
6953 expand_block_move (operands)
6954      rtx operands[];
6955 {
6956   rtx orig_dest = operands[0];
6957   rtx orig_src  = operands[1];
6958   rtx bytes_rtx = operands[2];
6959   rtx align_rtx = operands[3];
6960   int constp    = (GET_CODE (bytes_rtx) == CONST_INT);
6961   int align;
6962   int bytes;
6963   int offset;
6964   int move_bytes;
6965   rtx stores[MAX_MOVE_REG];
6966   int num_reg = 0;
6967
6968   /* If this is not a fixed size move, just call memcpy */
6969   if (! constp)
6970     return 0;
6971
6972   /* If this is not a fixed size alignment, abort */
6973   if (GET_CODE (align_rtx) != CONST_INT)
6974     abort ();
6975   align = INTVAL (align_rtx);
6976
6977   /* Anything to move? */
6978   bytes = INTVAL (bytes_rtx);
6979   if (bytes <= 0)
6980     return 1;
6981
6982   /* store_one_arg depends on expand_block_move to handle at least the size of
6983      reg_parm_stack_space.  */  
6984   if (bytes > (TARGET_POWERPC64 ? 64 : 32))
6985     return 0;
6986
6987   for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
6988     {
6989       union {
6990         rtx (*movstrsi) PARAMS ((rtx, rtx, rtx, rtx));
6991         rtx (*mov) PARAMS ((rtx, rtx));
6992       } gen_func;
6993       enum machine_mode mode = BLKmode;
6994       rtx src, dest;
6995       
6996       if (TARGET_STRING
6997           && bytes > 24         /* move up to 32 bytes at a time */
6998           && ! fixed_regs[5]
6999           && ! fixed_regs[6]
7000           && ! fixed_regs[7]
7001           && ! fixed_regs[8]
7002           && ! fixed_regs[9]
7003           && ! fixed_regs[10]
7004           && ! fixed_regs[11]
7005           && ! fixed_regs[12])
7006         {
7007           move_bytes = (bytes > 32) ? 32 : bytes;
7008           gen_func.movstrsi = gen_movstrsi_8reg;
7009         }
7010       else if (TARGET_STRING
7011                && bytes > 16    /* move up to 24 bytes at a time */
7012                && ! fixed_regs[5]
7013                && ! fixed_regs[6]
7014                && ! fixed_regs[7]
7015                && ! fixed_regs[8]
7016                && ! fixed_regs[9]
7017                && ! fixed_regs[10])
7018         {
7019           move_bytes = (bytes > 24) ? 24 : bytes;
7020           gen_func.movstrsi = gen_movstrsi_6reg;
7021         }
7022       else if (TARGET_STRING
7023                && bytes > 8     /* move up to 16 bytes at a time */
7024                && ! fixed_regs[5]
7025                && ! fixed_regs[6]
7026                && ! fixed_regs[7]
7027                && ! fixed_regs[8])
7028         {
7029           move_bytes = (bytes > 16) ? 16 : bytes;
7030           gen_func.movstrsi = gen_movstrsi_4reg;
7031         }
7032       else if (bytes >= 8 && TARGET_POWERPC64
7033                /* 64-bit loads and stores require word-aligned
7034                   displacements.  */
7035                && (align >= 8 || (! STRICT_ALIGNMENT && align >= 4)))
7036         {
7037           move_bytes = 8;
7038           mode = DImode;
7039           gen_func.mov = gen_movdi;
7040         }
7041       else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
7042         {                       /* move up to 8 bytes at a time */
7043           move_bytes = (bytes > 8) ? 8 : bytes;
7044           gen_func.movstrsi = gen_movstrsi_2reg;
7045         }
7046       else if (bytes >= 4 && (align >= 4 || ! STRICT_ALIGNMENT))
7047         {                       /* move 4 bytes */
7048           move_bytes = 4;
7049           mode = SImode;
7050           gen_func.mov = gen_movsi;
7051         }
7052       else if (bytes == 2 && (align >= 2 || ! STRICT_ALIGNMENT))
7053         {                       /* move 2 bytes */
7054           move_bytes = 2;
7055           mode = HImode;
7056           gen_func.mov = gen_movhi;
7057         }
7058       else if (TARGET_STRING && bytes > 1)
7059         {                       /* move up to 4 bytes at a time */
7060           move_bytes = (bytes > 4) ? 4 : bytes;
7061           gen_func.movstrsi = gen_movstrsi_1reg;
7062         }
7063       else /* move 1 byte at a time */
7064         {
7065           move_bytes = 1;
7066           mode = QImode;
7067           gen_func.mov = gen_movqi;
7068         }
7069       
7070       src = adjust_address (orig_src, mode, offset);
7071       dest = adjust_address (orig_dest, mode, offset);
7072       
7073       if (mode != BLKmode) 
7074         {
7075           rtx tmp_reg = gen_reg_rtx (mode);
7076           
7077           emit_insn ((*gen_func.mov) (tmp_reg, src));
7078           stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
7079         }
7080
7081       if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
7082         {
7083           int i;
7084           for (i = 0; i < num_reg; i++)
7085             emit_insn (stores[i]);
7086           num_reg = 0;
7087         }
7088
7089       if (mode == BLKmode)
7090         {
7091           /* Move the address into scratch registers.  The movstrsi
7092              patterns require zero offset.  */
7093           if (!REG_P (XEXP (src, 0)))
7094             {
7095               rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
7096               src = replace_equiv_address (src, src_reg);
7097             }
7098           set_mem_size (src, GEN_INT (move_bytes));
7099           
7100           if (!REG_P (XEXP (dest, 0)))
7101             {
7102               rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
7103               dest = replace_equiv_address (dest, dest_reg);
7104             }
7105           set_mem_size (dest, GEN_INT (move_bytes));
7106           
7107           emit_insn ((*gen_func.movstrsi) (dest, src,
7108                                            GEN_INT (move_bytes & 31),
7109                                            align_rtx));
7110         }
7111     }
7112
7113   return 1;
7114 }
7115
7116 \f
7117 /* Return 1 if OP is a load multiple operation.  It is known to be a
7118    PARALLEL and the first section will be tested.  */
7119
7120 int
7121 load_multiple_operation (op, mode)
7122      rtx op;
7123      enum machine_mode mode ATTRIBUTE_UNUSED;
7124 {
7125   int count = XVECLEN (op, 0);
7126   unsigned int dest_regno;
7127   rtx src_addr;
7128   int i;
7129
7130   /* Perform a quick check so we don't blow up below.  */
7131   if (count <= 1
7132       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7133       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
7134       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
7135     return 0;
7136
7137   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
7138   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
7139
7140   for (i = 1; i < count; i++)
7141     {
7142       rtx elt = XVECEXP (op, 0, i);
7143
7144       if (GET_CODE (elt) != SET
7145           || GET_CODE (SET_DEST (elt)) != REG
7146           || GET_MODE (SET_DEST (elt)) != SImode
7147           || REGNO (SET_DEST (elt)) != dest_regno + i
7148           || GET_CODE (SET_SRC (elt)) != MEM
7149           || GET_MODE (SET_SRC (elt)) != SImode
7150           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
7151           || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
7152           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
7153           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != i * 4)
7154         return 0;
7155     }
7156
7157   return 1;
7158 }
7159
7160 /* Similar, but tests for store multiple.  Here, the second vector element
7161    is a CLOBBER.  It will be tested later.  */
7162
7163 int
7164 store_multiple_operation (op, mode)
7165      rtx op;
7166      enum machine_mode mode ATTRIBUTE_UNUSED;
7167 {
7168   int count = XVECLEN (op, 0) - 1;
7169   unsigned int src_regno;
7170   rtx dest_addr;
7171   int i;
7172
7173   /* Perform a quick check so we don't blow up below.  */
7174   if (count <= 1
7175       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7176       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
7177       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
7178     return 0;
7179
7180   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
7181   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
7182
7183   for (i = 1; i < count; i++)
7184     {
7185       rtx elt = XVECEXP (op, 0, i + 1);
7186
7187       if (GET_CODE (elt) != SET
7188           || GET_CODE (SET_SRC (elt)) != REG
7189           || GET_MODE (SET_SRC (elt)) != SImode
7190           || REGNO (SET_SRC (elt)) != src_regno + i
7191           || GET_CODE (SET_DEST (elt)) != MEM
7192           || GET_MODE (SET_DEST (elt)) != SImode
7193           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
7194           || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
7195           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
7196           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != i * 4)
7197         return 0;
7198     }
7199
7200   return 1;
7201 }
7202
7203 /* Return a string to perform a load_multiple operation.
7204    operands[0] is the vector.
7205    operands[1] is the source address.
7206    operands[2] is the first destination register.  */
7207
7208 const char *
7209 rs6000_output_load_multiple (operands)
7210      rtx operands[3];
7211 {
7212   /* We have to handle the case where the pseudo used to contain the address
7213      is assigned to one of the output registers.  */
7214   int i, j;
7215   int words = XVECLEN (operands[0], 0);
7216   rtx xop[10];
7217
7218   if (XVECLEN (operands[0], 0) == 1)
7219     return "{l|lwz} %2,0(%1)";
7220
7221   for (i = 0; i < words; i++)
7222     if (refers_to_regno_p (REGNO (operands[2]) + i,
7223                            REGNO (operands[2]) + i + 1, operands[1], 0))
7224       {
7225         if (i == words-1)
7226           {
7227             xop[0] = GEN_INT (4 * (words-1));
7228             xop[1] = operands[1];
7229             xop[2] = operands[2];
7230             output_asm_insn ("{lsi|lswi} %2,%1,%0\n\t{l|lwz} %1,%0(%1)", xop);
7231             return "";
7232           }
7233         else if (i == 0)
7234           {
7235             xop[0] = GEN_INT (4 * (words-1));
7236             xop[1] = operands[1];
7237             xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7238             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);
7239             return "";
7240           }
7241         else
7242           {
7243             for (j = 0; j < words; j++)
7244               if (j != i)
7245                 {
7246                   xop[0] = GEN_INT (j * 4);
7247                   xop[1] = operands[1];
7248                   xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
7249                   output_asm_insn ("{l|lwz} %2,%0(%1)", xop);
7250                 }
7251             xop[0] = GEN_INT (i * 4);
7252             xop[1] = operands[1];
7253             output_asm_insn ("{l|lwz} %1,%0(%1)", xop);
7254             return "";
7255           }
7256       }
7257
7258   return "{lsi|lswi} %2,%1,%N0";
7259 }
7260
7261 /* Return 1 for a parallel vrsave operation.  */
7262
7263 int
7264 vrsave_operation (op, mode)
7265      rtx op;
7266      enum machine_mode mode ATTRIBUTE_UNUSED;
7267 {
7268   int count = XVECLEN (op, 0);
7269   unsigned int dest_regno, src_regno;
7270   int i;
7271
7272   if (count <= 1
7273       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7274       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
7275       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC_VOLATILE)
7276     return 0;
7277
7278   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
7279   src_regno  = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
7280
7281   if (dest_regno != VRSAVE_REGNO
7282       && src_regno != VRSAVE_REGNO)
7283     return 0;
7284
7285   for (i = 1; i < count; i++)
7286     {
7287       rtx elt = XVECEXP (op, 0, i);
7288
7289       if (GET_CODE (elt) != CLOBBER
7290           && GET_CODE (elt) != SET)
7291         return 0;
7292     }
7293
7294   return 1;
7295 }
7296
7297 /* Return 1 for an PARALLEL suitable for mtcrf.  */
7298
7299 int
7300 mtcrf_operation (op, mode)
7301      rtx op;
7302      enum machine_mode mode ATTRIBUTE_UNUSED;
7303 {
7304   int count = XVECLEN (op, 0);
7305   int i;
7306   rtx src_reg;
7307
7308   /* Perform a quick check so we don't blow up below.  */
7309   if (count < 1
7310       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7311       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC
7312       || XVECLEN (SET_SRC (XVECEXP (op, 0, 0)), 0) != 2)
7313     return 0;
7314   src_reg = XVECEXP (SET_SRC (XVECEXP (op, 0, 0)), 0, 0);
7315   
7316   if (GET_CODE (src_reg) != REG
7317       || GET_MODE (src_reg) != SImode
7318       || ! INT_REGNO_P (REGNO (src_reg)))
7319     return 0;
7320
7321   for (i = 0; i < count; i++)
7322     {
7323       rtx exp = XVECEXP (op, 0, i);
7324       rtx unspec;
7325       int maskval;
7326       
7327       if (GET_CODE (exp) != SET
7328           || GET_CODE (SET_DEST (exp)) != REG
7329           || GET_MODE (SET_DEST (exp)) != CCmode
7330           || ! CR_REGNO_P (REGNO (SET_DEST (exp))))
7331         return 0;
7332       unspec = SET_SRC (exp);
7333       maskval = 1 << (MAX_CR_REGNO - REGNO (SET_DEST (exp)));
7334       
7335       if (GET_CODE (unspec) != UNSPEC
7336           || XINT (unspec, 1) != UNSPEC_MOVESI_TO_CR
7337           || XVECLEN (unspec, 0) != 2
7338           || XVECEXP (unspec, 0, 0) != src_reg
7339           || GET_CODE (XVECEXP (unspec, 0, 1)) != CONST_INT
7340           || INTVAL (XVECEXP (unspec, 0, 1)) != maskval)
7341         return 0;
7342     }
7343   return 1;
7344 }
7345
7346 /* Return 1 for an PARALLEL suitable for lmw.  */
7347
7348 int
7349 lmw_operation (op, mode)
7350      rtx op;
7351      enum machine_mode mode ATTRIBUTE_UNUSED;
7352 {
7353   int count = XVECLEN (op, 0);
7354   unsigned int dest_regno;
7355   rtx src_addr;
7356   unsigned int base_regno;
7357   HOST_WIDE_INT offset;
7358   int i;
7359
7360   /* Perform a quick check so we don't blow up below.  */
7361   if (count <= 1
7362       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7363       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
7364       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
7365     return 0;
7366
7367   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
7368   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
7369
7370   if (dest_regno > 31
7371       || count != 32 - (int) dest_regno)
7372     return 0;
7373
7374   if (legitimate_indirect_address_p (src_addr, 0))
7375     {
7376       offset = 0;
7377       base_regno = REGNO (src_addr);
7378       if (base_regno == 0)
7379         return 0;
7380     }
7381   else if (legitimate_offset_address_p (SImode, src_addr, 0))
7382     {
7383       offset = INTVAL (XEXP (src_addr, 1));
7384       base_regno = REGNO (XEXP (src_addr, 0));
7385     }
7386   else
7387     return 0;
7388
7389   for (i = 0; i < count; i++)
7390     {
7391       rtx elt = XVECEXP (op, 0, i);
7392       rtx newaddr;
7393       rtx addr_reg;
7394       HOST_WIDE_INT newoffset;
7395
7396       if (GET_CODE (elt) != SET
7397           || GET_CODE (SET_DEST (elt)) != REG
7398           || GET_MODE (SET_DEST (elt)) != SImode
7399           || REGNO (SET_DEST (elt)) != dest_regno + i
7400           || GET_CODE (SET_SRC (elt)) != MEM
7401           || GET_MODE (SET_SRC (elt)) != SImode)
7402         return 0;
7403       newaddr = XEXP (SET_SRC (elt), 0);
7404       if (legitimate_indirect_address_p (newaddr, 0))
7405         {
7406           newoffset = 0;
7407           addr_reg = newaddr;
7408         }
7409       else if (legitimate_offset_address_p (SImode, newaddr, 0))
7410         {
7411           addr_reg = XEXP (newaddr, 0);
7412           newoffset = INTVAL (XEXP (newaddr, 1));
7413         }
7414       else
7415         return 0;
7416       if (REGNO (addr_reg) != base_regno
7417           || newoffset != offset + 4 * i)
7418         return 0;
7419     }
7420
7421   return 1;
7422 }
7423
7424 /* Return 1 for an PARALLEL suitable for stmw.  */
7425
7426 int
7427 stmw_operation (op, mode)
7428      rtx op;
7429      enum machine_mode mode ATTRIBUTE_UNUSED;
7430 {
7431   int count = XVECLEN (op, 0);
7432   unsigned int src_regno;
7433   rtx dest_addr;
7434   unsigned int base_regno;
7435   HOST_WIDE_INT offset;
7436   int i;
7437
7438   /* Perform a quick check so we don't blow up below.  */
7439   if (count <= 1
7440       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7441       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
7442       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
7443     return 0;
7444
7445   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
7446   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
7447
7448   if (src_regno > 31
7449       || count != 32 - (int) src_regno)
7450     return 0;
7451
7452   if (legitimate_indirect_address_p (dest_addr, 0))
7453     {
7454       offset = 0;
7455       base_regno = REGNO (dest_addr);
7456       if (base_regno == 0)
7457         return 0;
7458     }
7459   else if (legitimate_offset_address_p (SImode, dest_addr, 0))
7460     {
7461       offset = INTVAL (XEXP (dest_addr, 1));
7462       base_regno = REGNO (XEXP (dest_addr, 0));
7463     }
7464   else
7465     return 0;
7466
7467   for (i = 0; i < count; i++)
7468     {
7469       rtx elt = XVECEXP (op, 0, i);
7470       rtx newaddr;
7471       rtx addr_reg;
7472       HOST_WIDE_INT newoffset;
7473
7474       if (GET_CODE (elt) != SET
7475           || GET_CODE (SET_SRC (elt)) != REG
7476           || GET_MODE (SET_SRC (elt)) != SImode
7477           || REGNO (SET_SRC (elt)) != src_regno + i
7478           || GET_CODE (SET_DEST (elt)) != MEM
7479           || GET_MODE (SET_DEST (elt)) != SImode)
7480         return 0;
7481       newaddr = XEXP (SET_DEST (elt), 0);
7482       if (legitimate_indirect_address_p (newaddr, 0))
7483         {
7484           newoffset = 0;
7485           addr_reg = newaddr;
7486         }
7487       else if (legitimate_offset_address_p (SImode, newaddr, 0))
7488         {
7489           addr_reg = XEXP (newaddr, 0);
7490           newoffset = INTVAL (XEXP (newaddr, 1));
7491         }
7492       else
7493         return 0;
7494       if (REGNO (addr_reg) != base_regno
7495           || newoffset != offset + 4 * i)
7496         return 0;
7497     }
7498
7499   return 1;
7500 }
7501 \f
7502 /* A validation routine: say whether CODE, a condition code, and MODE
7503    match.  The other alternatives either don't make sense or should
7504    never be generated.  */
7505
7506 static void
7507 validate_condition_mode (code, mode)
7508      enum rtx_code code;
7509      enum machine_mode mode;
7510 {
7511   if (GET_RTX_CLASS (code) != '<' 
7512       || GET_MODE_CLASS (mode) != MODE_CC)
7513     abort ();
7514
7515   /* These don't make sense.  */
7516   if ((code == GT || code == LT || code == GE || code == LE)
7517       && mode == CCUNSmode)
7518     abort ();
7519
7520   if ((code == GTU || code == LTU || code == GEU || code == LEU)
7521       && mode != CCUNSmode)
7522     abort ();
7523
7524   if (mode != CCFPmode
7525       && (code == ORDERED || code == UNORDERED
7526           || code == UNEQ || code == LTGT
7527           || code == UNGT || code == UNLT
7528           || code == UNGE || code == UNLE))
7529     abort ();
7530   
7531   /* These should never be generated except for 
7532      flag_finite_math_only.  */
7533   if (mode == CCFPmode
7534       && ! flag_finite_math_only
7535       && (code == LE || code == GE
7536           || code == UNEQ || code == LTGT
7537           || code == UNGT || code == UNLT))
7538     abort ();
7539
7540   /* These are invalid; the information is not there.  */
7541   if (mode == CCEQmode 
7542       && code != EQ && code != NE)
7543     abort ();
7544 }
7545
7546 /* Return 1 if OP is a comparison operation that is valid for a branch insn.
7547    We only check the opcode against the mode of the CC value here.  */
7548
7549 int
7550 branch_comparison_operator (op, mode)
7551      rtx op;
7552      enum machine_mode mode ATTRIBUTE_UNUSED;
7553 {
7554   enum rtx_code code = GET_CODE (op);
7555   enum machine_mode cc_mode;
7556
7557   if (GET_RTX_CLASS (code) != '<')
7558     return 0;
7559
7560   cc_mode = GET_MODE (XEXP (op, 0));
7561   if (GET_MODE_CLASS (cc_mode) != MODE_CC)
7562     return 0;
7563
7564   validate_condition_mode (code, cc_mode);
7565
7566   return 1;
7567 }
7568
7569 /* Return 1 if OP is a comparison operation that is valid for a branch
7570    insn and which is true if the corresponding bit in the CC register
7571    is set.  */
7572
7573 int
7574 branch_positive_comparison_operator (op, mode)
7575      rtx op;
7576      enum machine_mode mode;
7577 {
7578   enum rtx_code code;
7579
7580   if (! branch_comparison_operator (op, mode))
7581     return 0;
7582
7583   code = GET_CODE (op);
7584   return (code == EQ || code == LT || code == GT
7585           || (TARGET_E500 && TARGET_HARD_FLOAT && !TARGET_FPRS && code == NE)
7586           || code == LTU || code == GTU
7587           || code == UNORDERED);
7588 }
7589
7590 /* Return 1 if OP is a comparison operation that is valid for an scc
7591    insn: it must be a positive comparison.  */
7592
7593 int
7594 scc_comparison_operator (op, mode)
7595      rtx op;
7596      enum machine_mode mode;
7597 {
7598   return branch_positive_comparison_operator (op, mode);
7599 }
7600
7601 int
7602 trap_comparison_operator (op, mode)
7603     rtx op;
7604     enum machine_mode mode;
7605 {
7606   if (mode != VOIDmode && mode != GET_MODE (op))
7607     return 0;
7608   return GET_RTX_CLASS (GET_CODE (op)) == '<';
7609 }
7610
7611 int
7612 boolean_operator (op, mode)
7613     rtx op;
7614     enum machine_mode mode ATTRIBUTE_UNUSED;
7615 {
7616   enum rtx_code code = GET_CODE (op);
7617   return (code == AND || code == IOR || code == XOR);
7618 }
7619
7620 int
7621 boolean_or_operator (op, mode)
7622     rtx op;
7623     enum machine_mode mode ATTRIBUTE_UNUSED;
7624 {
7625   enum rtx_code code = GET_CODE (op);
7626   return (code == IOR || code == XOR);
7627 }
7628
7629 int
7630 min_max_operator (op, mode)
7631     rtx op;
7632     enum machine_mode mode ATTRIBUTE_UNUSED;
7633 {
7634   enum rtx_code code = GET_CODE (op);
7635   return (code == SMIN || code == SMAX || code == UMIN || code == UMAX);
7636 }
7637 \f
7638 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
7639    mask required to convert the result of a rotate insn into a shift
7640    left insn of SHIFTOP bits.  Both are known to be SImode CONST_INT.  */
7641
7642 int
7643 includes_lshift_p (shiftop, andop)
7644      rtx shiftop;
7645      rtx andop;
7646 {
7647   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
7648
7649   shift_mask <<= INTVAL (shiftop);
7650
7651   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
7652 }
7653
7654 /* Similar, but for right shift.  */
7655
7656 int
7657 includes_rshift_p (shiftop, andop)
7658      rtx shiftop;
7659      rtx andop;
7660 {
7661   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
7662
7663   shift_mask >>= INTVAL (shiftop);
7664
7665   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
7666 }
7667
7668 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
7669    to perform a left shift.  It must have exactly SHIFTOP least
7670    significant 0's, then one or more 1's, then zero or more 0's.  */
7671
7672 int
7673 includes_rldic_lshift_p (shiftop, andop)
7674      rtx shiftop;
7675      rtx andop;
7676 {
7677   if (GET_CODE (andop) == CONST_INT)
7678     {
7679       HOST_WIDE_INT c, lsb, shift_mask;
7680
7681       c = INTVAL (andop);
7682       if (c == 0 || c == ~0)
7683         return 0;
7684
7685       shift_mask = ~0;
7686       shift_mask <<= INTVAL (shiftop);
7687
7688       /* Find the least significant one bit.  */
7689       lsb = c & -c;
7690
7691       /* It must coincide with the LSB of the shift mask.  */
7692       if (-lsb != shift_mask)
7693         return 0;
7694
7695       /* Invert to look for the next transition (if any).  */
7696       c = ~c;
7697
7698       /* Remove the low group of ones (originally low group of zeros).  */
7699       c &= -lsb;
7700
7701       /* Again find the lsb, and check we have all 1's above.  */
7702       lsb = c & -c;
7703       return c == -lsb;
7704     }
7705   else if (GET_CODE (andop) == CONST_DOUBLE
7706            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
7707     {
7708       HOST_WIDE_INT low, high, lsb;
7709       HOST_WIDE_INT shift_mask_low, shift_mask_high;
7710
7711       low = CONST_DOUBLE_LOW (andop);
7712       if (HOST_BITS_PER_WIDE_INT < 64)
7713         high = CONST_DOUBLE_HIGH (andop);
7714
7715       if ((low == 0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == 0))
7716           || (low == ~0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0)))
7717         return 0;
7718
7719       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
7720         {
7721           shift_mask_high = ~0;
7722           if (INTVAL (shiftop) > 32)
7723             shift_mask_high <<= INTVAL (shiftop) - 32;
7724
7725           lsb = high & -high;
7726
7727           if (-lsb != shift_mask_high || INTVAL (shiftop) < 32)
7728             return 0;
7729
7730           high = ~high;
7731           high &= -lsb;
7732
7733           lsb = high & -high;
7734           return high == -lsb;
7735         }
7736
7737       shift_mask_low = ~0;
7738       shift_mask_low <<= INTVAL (shiftop);
7739
7740       lsb = low & -low;
7741
7742       if (-lsb != shift_mask_low)
7743         return 0;
7744
7745       if (HOST_BITS_PER_WIDE_INT < 64)
7746         high = ~high;
7747       low = ~low;
7748       low &= -lsb;
7749
7750       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
7751         {
7752           lsb = high & -high;
7753           return high == -lsb;
7754         }
7755
7756       lsb = low & -low;
7757       return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
7758     }
7759   else
7760     return 0;
7761 }
7762
7763 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
7764    to perform a left shift.  It must have SHIFTOP or more least
7765    significant 0's, with the remainder of the word 1's.  */
7766
7767 int
7768 includes_rldicr_lshift_p (shiftop, andop)
7769      rtx shiftop;
7770      rtx andop;
7771 {
7772   if (GET_CODE (andop) == CONST_INT)
7773     {
7774       HOST_WIDE_INT c, lsb, shift_mask;
7775
7776       shift_mask = ~0;
7777       shift_mask <<= INTVAL (shiftop);
7778       c = INTVAL (andop);
7779
7780       /* Find the least significant one bit.  */
7781       lsb = c & -c;
7782
7783       /* It must be covered by the shift mask.
7784          This test also rejects c == 0.  */
7785       if ((lsb & shift_mask) == 0)
7786         return 0;
7787
7788       /* Check we have all 1's above the transition, and reject all 1's.  */
7789       return c == -lsb && lsb != 1;
7790     }
7791   else if (GET_CODE (andop) == CONST_DOUBLE
7792            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
7793     {
7794       HOST_WIDE_INT low, lsb, shift_mask_low;
7795
7796       low = CONST_DOUBLE_LOW (andop);
7797
7798       if (HOST_BITS_PER_WIDE_INT < 64)
7799         {
7800           HOST_WIDE_INT high, shift_mask_high;
7801
7802           high = CONST_DOUBLE_HIGH (andop);
7803
7804           if (low == 0)
7805             {
7806               shift_mask_high = ~0;
7807               if (INTVAL (shiftop) > 32)
7808                 shift_mask_high <<= INTVAL (shiftop) - 32;
7809
7810               lsb = high & -high;
7811
7812               if ((lsb & shift_mask_high) == 0)
7813                 return 0;
7814
7815               return high == -lsb;
7816             }
7817           if (high != ~0)
7818             return 0;
7819         }
7820
7821       shift_mask_low = ~0;
7822       shift_mask_low <<= INTVAL (shiftop);
7823
7824       lsb = low & -low;
7825
7826       if ((lsb & shift_mask_low) == 0)
7827         return 0;
7828
7829       return low == -lsb && lsb != 1;
7830     }
7831   else
7832     return 0;
7833 }
7834
7835 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
7836    for lfq and stfq insns.
7837
7838    Note reg1 and reg2 *must* be hard registers.  To be sure we will
7839    abort if we are passed pseudo registers.  */
7840
7841 int
7842 registers_ok_for_quad_peep (reg1, reg2)
7843      rtx reg1, reg2;
7844 {
7845   /* We might have been passed a SUBREG.  */
7846   if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG) 
7847     return 0;
7848
7849   return (REGNO (reg1) == REGNO (reg2) - 1);
7850 }
7851
7852 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
7853    addr1 and addr2 must be in consecutive memory locations
7854    (addr2 == addr1 + 8).  */
7855
7856 int
7857 addrs_ok_for_quad_peep (addr1, addr2)
7858      rtx addr1;
7859      rtx addr2;
7860 {
7861   unsigned int reg1;
7862   int offset1;
7863
7864   /* Extract an offset (if used) from the first addr.  */
7865   if (GET_CODE (addr1) == PLUS)
7866     {
7867       /* If not a REG, return zero.  */
7868       if (GET_CODE (XEXP (addr1, 0)) != REG)
7869         return 0;
7870       else
7871         {
7872           reg1 = REGNO (XEXP (addr1, 0));
7873           /* The offset must be constant!  */
7874           if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
7875             return 0;
7876           offset1 = INTVAL (XEXP (addr1, 1));
7877         }
7878     }
7879   else if (GET_CODE (addr1) != REG)
7880     return 0;
7881   else
7882     {
7883       reg1 = REGNO (addr1);
7884       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
7885       offset1 = 0;
7886     }
7887
7888 /* Make sure the second address is a (mem (plus (reg) (const_int)))
7889     or if it is (mem (reg)) then make sure that offset1 is -8 and the same 
7890     register as addr1.  */
7891   if (offset1 == -8 && GET_CODE (addr2) == REG && reg1 == REGNO (addr2))
7892    return 1;
7893   if (GET_CODE (addr2) != PLUS)
7894     return 0;
7895
7896   if (GET_CODE (XEXP (addr2, 0)) != REG
7897       || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
7898     return 0;
7899
7900   if (reg1 != REGNO (XEXP (addr2, 0)))
7901     return 0;
7902
7903   /* The offset for the second addr must be 8 more than the first addr.  */
7904   if (INTVAL (XEXP (addr2, 1)) != offset1 + 8)
7905     return 0;
7906
7907   /* All the tests passed.  addr1 and addr2 are valid for lfq or stfq
7908      instructions.  */
7909   return 1;
7910 }
7911 \f
7912 /* Return the register class of a scratch register needed to copy IN into
7913    or out of a register in CLASS in MODE.  If it can be done directly,
7914    NO_REGS is returned.  */
7915
7916 enum reg_class
7917 secondary_reload_class (class, mode, in)
7918      enum reg_class class;
7919      enum machine_mode mode ATTRIBUTE_UNUSED;
7920      rtx in;
7921 {
7922   int regno;
7923
7924   if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
7925 #if TARGET_MACHO
7926                     && MACHOPIC_INDIRECT
7927 #endif
7928                     ))
7929     {
7930       /* We cannot copy a symbolic operand directly into anything
7931          other than BASE_REGS for TARGET_ELF.  So indicate that a
7932          register from BASE_REGS is needed as an intermediate
7933          register.
7934          
7935          On Darwin, pic addresses require a load from memory, which
7936          needs a base register.  */
7937       if (class != BASE_REGS
7938           && (GET_CODE (in) == SYMBOL_REF
7939               || GET_CODE (in) == HIGH
7940               || GET_CODE (in) == LABEL_REF
7941               || GET_CODE (in) == CONST))
7942         return BASE_REGS;
7943     }
7944
7945   if (GET_CODE (in) == REG)
7946     {
7947       regno = REGNO (in);
7948       if (regno >= FIRST_PSEUDO_REGISTER)
7949         {
7950           regno = true_regnum (in);
7951           if (regno >= FIRST_PSEUDO_REGISTER)
7952             regno = -1;
7953         }
7954     }
7955   else if (GET_CODE (in) == SUBREG)
7956     {
7957       regno = true_regnum (in);
7958       if (regno >= FIRST_PSEUDO_REGISTER)
7959         regno = -1;
7960     }
7961   else
7962     regno = -1;
7963
7964   /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
7965      into anything.  */
7966   if (class == GENERAL_REGS || class == BASE_REGS
7967       || (regno >= 0 && INT_REGNO_P (regno)))
7968     return NO_REGS;
7969
7970   /* Constants, memory, and FP registers can go into FP registers.  */
7971   if ((regno == -1 || FP_REGNO_P (regno))
7972       && (class == FLOAT_REGS || class == NON_SPECIAL_REGS))
7973     return NO_REGS;
7974
7975   /* Memory, and AltiVec registers can go into AltiVec registers.  */
7976   if ((regno == -1 || ALTIVEC_REGNO_P (regno))
7977       && class == ALTIVEC_REGS)
7978     return NO_REGS;
7979
7980   /* We can copy among the CR registers.  */
7981   if ((class == CR_REGS || class == CR0_REGS)
7982       && regno >= 0 && CR_REGNO_P (regno))
7983     return NO_REGS;
7984
7985   /* Otherwise, we need GENERAL_REGS.  */
7986   return GENERAL_REGS;
7987 }
7988 \f
7989 /* Given a comparison operation, return the bit number in CCR to test.  We
7990    know this is a valid comparison.  
7991
7992    SCC_P is 1 if this is for an scc.  That means that %D will have been
7993    used instead of %C, so the bits will be in different places.
7994
7995    Return -1 if OP isn't a valid comparison for some reason.  */
7996
7997 int
7998 ccr_bit (op, scc_p)
7999      rtx op;
8000      int scc_p;
8001 {
8002   enum rtx_code code = GET_CODE (op);
8003   enum machine_mode cc_mode;
8004   int cc_regnum;
8005   int base_bit;
8006   rtx reg;
8007
8008   if (GET_RTX_CLASS (code) != '<')
8009     return -1;
8010
8011   reg = XEXP (op, 0);
8012
8013   if (GET_CODE (reg) != REG
8014       || ! CR_REGNO_P (REGNO (reg)))
8015     abort ();
8016
8017   cc_mode = GET_MODE (reg);
8018   cc_regnum = REGNO (reg);
8019   base_bit = 4 * (cc_regnum - CR0_REGNO);
8020
8021   validate_condition_mode (code, cc_mode);
8022
8023   /* When generating a sCOND operation, only positive conditions are
8024      allowed.  */
8025   if (scc_p && code != EQ && code != GT && code != LT && code != UNORDERED
8026       && code != GTU && code != LTU)
8027     abort ();
8028   
8029   switch (code)
8030     {
8031     case NE:
8032       if (TARGET_E500 && !TARGET_FPRS
8033           && TARGET_HARD_FLOAT && cc_mode == CCFPmode)
8034         return base_bit + 1;
8035       return scc_p ? base_bit + 3 : base_bit + 2;
8036     case EQ:
8037       if (TARGET_E500 && !TARGET_FPRS
8038           && TARGET_HARD_FLOAT && cc_mode == CCFPmode)
8039         return base_bit + 1;
8040       return base_bit + 2;
8041     case GT:  case GTU:  case UNLE:
8042       return base_bit + 1;
8043     case LT:  case LTU:  case UNGE:
8044       return base_bit;
8045     case ORDERED:  case UNORDERED:
8046       return base_bit + 3;
8047
8048     case GE:  case GEU:
8049       /* If scc, we will have done a cror to put the bit in the
8050          unordered position.  So test that bit.  For integer, this is ! LT
8051          unless this is an scc insn.  */
8052       return scc_p ? base_bit + 3 : base_bit;
8053
8054     case LE:  case LEU:
8055       return scc_p ? base_bit + 3 : base_bit + 1;
8056
8057     default:
8058       abort ();
8059     }
8060 }
8061 \f
8062 /* Return the GOT register.  */
8063
8064 struct rtx_def *
8065 rs6000_got_register (value)
8066      rtx value ATTRIBUTE_UNUSED;
8067 {
8068   /* The second flow pass currently (June 1999) can't update
8069      regs_ever_live without disturbing other parts of the compiler, so
8070      update it here to make the prolog/epilogue code happy.  */
8071   if (no_new_pseudos && ! regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM])
8072     regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
8073
8074   current_function_uses_pic_offset_table = 1;
8075
8076   return pic_offset_table_rtx;
8077 }
8078 \f
8079 /* Function to init struct machine_function.
8080    This will be called, via a pointer variable,
8081    from push_function_context.  */
8082
8083 static struct machine_function *
8084 rs6000_init_machine_status ()
8085 {
8086   return ggc_alloc_cleared (sizeof (machine_function));
8087 }
8088 \f
8089 /* These macros test for integers and extract the low-order bits.  */
8090 #define INT_P(X)  \
8091 ((GET_CODE (X) == CONST_INT || GET_CODE (X) == CONST_DOUBLE)    \
8092  && GET_MODE (X) == VOIDmode)
8093
8094 #define INT_LOWPART(X) \
8095   (GET_CODE (X) == CONST_INT ? INTVAL (X) : CONST_DOUBLE_LOW (X))
8096
8097 int
8098 extract_MB (op)
8099      rtx op;
8100 {
8101   int i;
8102   unsigned long val = INT_LOWPART (op);
8103
8104   /* If the high bit is zero, the value is the first 1 bit we find
8105      from the left.  */
8106   if ((val & 0x80000000) == 0)
8107     {
8108       if ((val & 0xffffffff) == 0)
8109         abort ();
8110
8111       i = 1;
8112       while (((val <<= 1) & 0x80000000) == 0)
8113         ++i;
8114       return i;
8115     }
8116
8117   /* If the high bit is set and the low bit is not, or the mask is all
8118      1's, the value is zero.  */
8119   if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
8120     return 0;
8121
8122   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
8123      from the right.  */
8124   i = 31;
8125   while (((val >>= 1) & 1) != 0)
8126     --i;
8127
8128   return i;
8129 }
8130
8131 int
8132 extract_ME (op)
8133      rtx op;
8134 {
8135   int i;
8136   unsigned long val = INT_LOWPART (op);
8137
8138   /* If the low bit is zero, the value is the first 1 bit we find from
8139      the right.  */
8140   if ((val & 1) == 0)
8141     {
8142       if ((val & 0xffffffff) == 0)
8143         abort ();
8144
8145       i = 30;
8146       while (((val >>= 1) & 1) == 0)
8147         --i;
8148
8149       return i;
8150     }
8151
8152   /* If the low bit is set and the high bit is not, or the mask is all
8153      1's, the value is 31.  */
8154   if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
8155     return 31;
8156
8157   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
8158      from the left.  */
8159   i = 0;
8160   while (((val <<= 1) & 0x80000000) != 0)
8161     ++i;
8162
8163   return i;
8164 }
8165
8166 /* Locate some local-dynamic symbol still in use by this function
8167    so that we can print its name in some tls_ld pattern.  */
8168
8169 static const char *
8170 rs6000_get_some_local_dynamic_name ()
8171 {
8172   rtx insn;
8173
8174   if (cfun->machine->some_ld_name)
8175     return cfun->machine->some_ld_name;
8176
8177   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
8178     if (INSN_P (insn)
8179         && for_each_rtx (&PATTERN (insn),
8180                          rs6000_get_some_local_dynamic_name_1, 0))
8181       return cfun->machine->some_ld_name;
8182
8183   abort ();
8184 }
8185
8186 /* Helper function for rs6000_get_some_local_dynamic_name.  */
8187
8188 static int
8189 rs6000_get_some_local_dynamic_name_1 (px, data)
8190      rtx *px;
8191      void *data ATTRIBUTE_UNUSED;
8192 {
8193   rtx x = *px;
8194
8195   if (GET_CODE (x) == SYMBOL_REF)
8196     {
8197       const char *str = XSTR (x, 0);
8198       if (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
8199         {
8200           cfun->machine->some_ld_name = str;
8201           return 1;
8202         }
8203     }
8204
8205   return 0;
8206 }
8207
8208 /* Print an operand.  Recognize special options, documented below.  */
8209
8210 #if TARGET_ELF
8211 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
8212 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
8213 #else
8214 #define SMALL_DATA_RELOC "sda21"
8215 #define SMALL_DATA_REG 0
8216 #endif
8217
8218 void
8219 print_operand (file, x, code)
8220     FILE *file;
8221     rtx x;
8222     int code;
8223 {
8224   int i;
8225   HOST_WIDE_INT val;
8226   unsigned HOST_WIDE_INT uval;
8227
8228   switch (code)
8229     {
8230     case '.':
8231       /* Write out an instruction after the call which may be replaced
8232          with glue code by the loader.  This depends on the AIX version.  */
8233       asm_fprintf (file, RS6000_CALL_GLUE);
8234       return;
8235
8236       /* %a is output_address.  */
8237
8238     case 'A':
8239       /* If X is a constant integer whose low-order 5 bits are zero,
8240          write 'l'.  Otherwise, write 'r'.  This is a kludge to fix a bug
8241          in the AIX assembler where "sri" with a zero shift count
8242          writes a trash instruction.  */
8243       if (GET_CODE (x) == CONST_INT && (INTVAL (x) & 31) == 0)
8244         putc ('l', file);
8245       else
8246         putc ('r', file);
8247       return;
8248
8249     case 'b':
8250       /* If constant, low-order 16 bits of constant, unsigned.
8251          Otherwise, write normally.  */
8252       if (INT_P (x))
8253         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 0xffff);
8254       else
8255         print_operand (file, x, 0);
8256       return;
8257
8258     case 'B':
8259       /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
8260          for 64-bit mask direction.  */
8261       putc (((INT_LOWPART(x) & 1) == 0 ? 'r' : 'l'), file);
8262       return;
8263
8264       /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
8265          output_operand.  */
8266
8267     case 'E':
8268       /* X is a CR register.  Print the number of the EQ bit of the CR */
8269       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
8270         output_operand_lossage ("invalid %%E value");
8271       else
8272         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
8273       return;
8274
8275     case 'f':
8276       /* X is a CR register.  Print the shift count needed to move it
8277          to the high-order four bits.  */
8278       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
8279         output_operand_lossage ("invalid %%f value");
8280       else
8281         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
8282       return;
8283
8284     case 'F':
8285       /* Similar, but print the count for the rotate in the opposite
8286          direction.  */
8287       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
8288         output_operand_lossage ("invalid %%F value");
8289       else
8290         fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
8291       return;
8292
8293     case 'G':
8294       /* X is a constant integer.  If it is negative, print "m",
8295          otherwise print "z".  This is to make an aze or ame insn.  */
8296       if (GET_CODE (x) != CONST_INT)
8297         output_operand_lossage ("invalid %%G value");
8298       else if (INTVAL (x) >= 0)
8299         putc ('z', file);
8300       else
8301         putc ('m', file);
8302       return;
8303
8304     case 'h':
8305       /* If constant, output low-order five bits.  Otherwise, write
8306          normally.  */
8307       if (INT_P (x))
8308         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 31);
8309       else
8310         print_operand (file, x, 0);
8311       return;
8312
8313     case 'H':
8314       /* If constant, output low-order six bits.  Otherwise, write
8315          normally.  */
8316       if (INT_P (x))
8317         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 63);
8318       else
8319         print_operand (file, x, 0);
8320       return;
8321
8322     case 'I':
8323       /* Print `i' if this is a constant, else nothing.  */
8324       if (INT_P (x))
8325         putc ('i', file);
8326       return;
8327
8328     case 'j':
8329       /* Write the bit number in CCR for jump.  */
8330       i = ccr_bit (x, 0);
8331       if (i == -1)
8332         output_operand_lossage ("invalid %%j code");
8333       else
8334         fprintf (file, "%d", i);
8335       return;
8336
8337     case 'J':
8338       /* Similar, but add one for shift count in rlinm for scc and pass
8339          scc flag to `ccr_bit'.  */
8340       i = ccr_bit (x, 1);
8341       if (i == -1)
8342         output_operand_lossage ("invalid %%J code");
8343       else
8344         /* If we want bit 31, write a shift count of zero, not 32.  */
8345         fprintf (file, "%d", i == 31 ? 0 : i + 1);
8346       return;
8347
8348     case 'k':
8349       /* X must be a constant.  Write the 1's complement of the
8350          constant.  */
8351       if (! INT_P (x))
8352         output_operand_lossage ("invalid %%k value");
8353       else
8354         fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INT_LOWPART (x));
8355       return;
8356
8357     case 'K':
8358       /* X must be a symbolic constant on ELF.  Write an
8359          expression suitable for an 'addi' that adds in the low 16
8360          bits of the MEM.  */
8361       if (GET_CODE (x) != CONST)
8362         {
8363           print_operand_address (file, x);
8364           fputs ("@l", file);
8365         }
8366       else
8367         {
8368           if (GET_CODE (XEXP (x, 0)) != PLUS
8369               || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
8370                   && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
8371               || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
8372             output_operand_lossage ("invalid %%K value");
8373           print_operand_address (file, XEXP (XEXP (x, 0), 0));
8374           fputs ("@l", file);
8375           /* For GNU as, there must be a non-alphanumeric character
8376              between 'l' and the number.  The '-' is added by
8377              print_operand() already.  */
8378           if (INTVAL (XEXP (XEXP (x, 0), 1)) >= 0)
8379             fputs ("+", file);
8380           print_operand (file, XEXP (XEXP (x, 0), 1), 0);
8381         }
8382       return;
8383
8384       /* %l is output_asm_label.  */
8385
8386     case 'L':
8387       /* Write second word of DImode or DFmode reference.  Works on register
8388          or non-indexed memory only.  */
8389       if (GET_CODE (x) == REG)
8390         fprintf (file, "%s", reg_names[REGNO (x) + 1]);
8391       else if (GET_CODE (x) == MEM)
8392         {
8393           /* Handle possible auto-increment.  Since it is pre-increment and
8394              we have already done it, we can just use an offset of word.  */
8395           if (GET_CODE (XEXP (x, 0)) == PRE_INC
8396               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
8397             output_address (plus_constant (XEXP (XEXP (x, 0), 0),
8398                                            UNITS_PER_WORD));
8399           else
8400             output_address (XEXP (adjust_address_nv (x, SImode,
8401                                                      UNITS_PER_WORD),
8402                                   0));
8403
8404           if (small_data_operand (x, GET_MODE (x)))
8405             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
8406                      reg_names[SMALL_DATA_REG]);
8407         }
8408       return;
8409                             
8410     case 'm':
8411       /* MB value for a mask operand.  */
8412       if (! mask_operand (x, SImode))
8413         output_operand_lossage ("invalid %%m value");
8414
8415       fprintf (file, "%d", extract_MB (x));
8416       return;
8417
8418     case 'M':
8419       /* ME value for a mask operand.  */
8420       if (! mask_operand (x, SImode))
8421         output_operand_lossage ("invalid %%M value");
8422
8423       fprintf (file, "%d", extract_ME (x));
8424       return;
8425
8426       /* %n outputs the negative of its operand.  */
8427
8428     case 'N':
8429       /* Write the number of elements in the vector times 4.  */
8430       if (GET_CODE (x) != PARALLEL)
8431         output_operand_lossage ("invalid %%N value");
8432       else
8433         fprintf (file, "%d", XVECLEN (x, 0) * 4);
8434       return;
8435
8436     case 'O':
8437       /* Similar, but subtract 1 first.  */
8438       if (GET_CODE (x) != PARALLEL)
8439         output_operand_lossage ("invalid %%O value");
8440       else
8441         fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
8442       return;
8443
8444     case 'p':
8445       /* X is a CONST_INT that is a power of two.  Output the logarithm.  */
8446       if (! INT_P (x)
8447           || INT_LOWPART (x) < 0
8448           || (i = exact_log2 (INT_LOWPART (x))) < 0)
8449         output_operand_lossage ("invalid %%p value");
8450       else
8451         fprintf (file, "%d", i);
8452       return;
8453
8454     case 'P':
8455       /* The operand must be an indirect memory reference.  The result
8456          is the register number.  */
8457       if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
8458           || REGNO (XEXP (x, 0)) >= 32)
8459         output_operand_lossage ("invalid %%P value");
8460       else
8461         fprintf (file, "%d", REGNO (XEXP (x, 0)));
8462       return;
8463
8464     case 'q':
8465       /* This outputs the logical code corresponding to a boolean
8466          expression.  The expression may have one or both operands
8467          negated (if one, only the first one).  For condition register
8468          logical operations, it will also treat the negated
8469          CR codes as NOTs, but not handle NOTs of them.  */
8470       {
8471         const char *const *t = 0;
8472         const char *s;
8473         enum rtx_code code = GET_CODE (x);
8474         static const char * const tbl[3][3] = {
8475           { "and", "andc", "nor" },
8476           { "or", "orc", "nand" },
8477           { "xor", "eqv", "xor" } };
8478
8479         if (code == AND)
8480           t = tbl[0];
8481         else if (code == IOR)
8482           t = tbl[1];
8483         else if (code == XOR)
8484           t = tbl[2];
8485         else
8486           output_operand_lossage ("invalid %%q value");
8487
8488         if (GET_CODE (XEXP (x, 0)) != NOT)
8489           s = t[0];
8490         else
8491           {
8492             if (GET_CODE (XEXP (x, 1)) == NOT)
8493               s = t[2];
8494             else
8495               s = t[1];
8496           }
8497         
8498         fputs (s, file);
8499       }
8500       return;
8501
8502     case 'R':
8503       /* X is a CR register.  Print the mask for `mtcrf'.  */
8504       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
8505         output_operand_lossage ("invalid %%R value");
8506       else
8507         fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
8508       return;
8509
8510     case 's':
8511       /* Low 5 bits of 32 - value */
8512       if (! INT_P (x))
8513         output_operand_lossage ("invalid %%s value");
8514       else
8515         fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INT_LOWPART (x)) & 31);
8516       return;
8517
8518     case 'S':
8519       /* PowerPC64 mask position.  All 0's is excluded.
8520          CONST_INT 32-bit mask is considered sign-extended so any
8521          transition must occur within the CONST_INT, not on the boundary.  */
8522       if (! mask64_operand (x, DImode))
8523         output_operand_lossage ("invalid %%S value");
8524
8525       uval = INT_LOWPART (x);
8526
8527       if (uval & 1)     /* Clear Left */
8528         {
8529 #if HOST_BITS_PER_WIDE_INT > 64
8530           uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
8531 #endif
8532           i = 64;
8533         }
8534       else              /* Clear Right */
8535         {
8536           uval = ~uval;
8537 #if HOST_BITS_PER_WIDE_INT > 64
8538           uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
8539 #endif
8540           i = 63;
8541         }
8542       while (uval != 0)
8543         --i, uval >>= 1;
8544       if (i < 0)
8545         abort ();
8546       fprintf (file, "%d", i);
8547       return;
8548
8549     case 't':
8550       /* Like 'J' but get to the OVERFLOW/UNORDERED bit.  */
8551       if (GET_CODE (x) != REG || GET_MODE (x) != CCmode)
8552         abort ();
8553
8554       /* Bit 3 is OV bit.  */
8555       i = 4 * (REGNO (x) - CR0_REGNO) + 3;
8556
8557       /* If we want bit 31, write a shift count of zero, not 32.  */
8558       fprintf (file, "%d", i == 31 ? 0 : i + 1);
8559       return;
8560
8561     case 'T':
8562       /* Print the symbolic name of a branch target register.  */
8563       if (GET_CODE (x) != REG || (REGNO (x) != LINK_REGISTER_REGNUM
8564                                   && REGNO (x) != COUNT_REGISTER_REGNUM))
8565         output_operand_lossage ("invalid %%T value");
8566       else if (REGNO (x) == LINK_REGISTER_REGNUM)
8567         fputs (TARGET_NEW_MNEMONICS ? "lr" : "r", file);
8568       else
8569         fputs ("ctr", file);
8570       return;
8571
8572     case 'u':
8573       /* High-order 16 bits of constant for use in unsigned operand.  */
8574       if (! INT_P (x))
8575         output_operand_lossage ("invalid %%u value");
8576       else
8577         fprintf (file, HOST_WIDE_INT_PRINT_HEX, 
8578                  (INT_LOWPART (x) >> 16) & 0xffff);
8579       return;
8580
8581     case 'v':
8582       /* High-order 16 bits of constant for use in signed operand.  */
8583       if (! INT_P (x))
8584         output_operand_lossage ("invalid %%v value");
8585       else
8586         fprintf (file, HOST_WIDE_INT_PRINT_HEX,
8587                  (INT_LOWPART (x) >> 16) & 0xffff);
8588       return;
8589
8590     case 'U':
8591       /* Print `u' if this has an auto-increment or auto-decrement.  */
8592       if (GET_CODE (x) == MEM
8593           && (GET_CODE (XEXP (x, 0)) == PRE_INC
8594               || GET_CODE (XEXP (x, 0)) == PRE_DEC))
8595         putc ('u', file);
8596       return;
8597
8598     case 'V':
8599       /* Print the trap code for this operand.  */
8600       switch (GET_CODE (x))
8601         {
8602         case EQ:
8603           fputs ("eq", file);   /* 4 */
8604           break;
8605         case NE:
8606           fputs ("ne", file);   /* 24 */
8607           break;
8608         case LT:
8609           fputs ("lt", file);   /* 16 */
8610           break;
8611         case LE:
8612           fputs ("le", file);   /* 20 */
8613           break;
8614         case GT:
8615           fputs ("gt", file);   /* 8 */
8616           break;
8617         case GE:
8618           fputs ("ge", file);   /* 12 */
8619           break;
8620         case LTU:
8621           fputs ("llt", file);  /* 2 */
8622           break;
8623         case LEU:
8624           fputs ("lle", file);  /* 6 */
8625           break;
8626         case GTU:
8627           fputs ("lgt", file);  /* 1 */
8628           break;
8629         case GEU:
8630           fputs ("lge", file);  /* 5 */
8631           break;
8632         default:
8633           abort ();
8634         }
8635       break;
8636
8637     case 'w':
8638       /* If constant, low-order 16 bits of constant, signed.  Otherwise, write
8639          normally.  */
8640       if (INT_P (x))
8641         fprintf (file, HOST_WIDE_INT_PRINT_DEC, 
8642                  ((INT_LOWPART (x) & 0xffff) ^ 0x8000) - 0x8000);
8643       else
8644         print_operand (file, x, 0);
8645       return;
8646
8647     case 'W':
8648       /* MB value for a PowerPC64 rldic operand.  */
8649       val = (GET_CODE (x) == CONST_INT
8650              ? INTVAL (x) : CONST_DOUBLE_HIGH (x));
8651
8652       if (val < 0)
8653         i = -1;
8654       else
8655         for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
8656           if ((val <<= 1) < 0)
8657             break;
8658
8659 #if HOST_BITS_PER_WIDE_INT == 32
8660       if (GET_CODE (x) == CONST_INT && i >= 0)
8661         i += 32;  /* zero-extend high-part was all 0's */
8662       else if (GET_CODE (x) == CONST_DOUBLE && i == 32)
8663         {
8664           val = CONST_DOUBLE_LOW (x);
8665
8666           if (val == 0)
8667             abort ();
8668           else if (val < 0)
8669             --i;
8670           else
8671             for ( ; i < 64; i++)
8672               if ((val <<= 1) < 0)
8673                 break;
8674         }
8675 #endif
8676
8677       fprintf (file, "%d", i + 1);
8678       return;
8679
8680     case 'X':
8681       if (GET_CODE (x) == MEM
8682           && legitimate_indexed_address_p (XEXP (x, 0), 0))
8683         putc ('x', file);
8684       return;
8685
8686     case 'Y':
8687       /* Like 'L', for third word of TImode  */
8688       if (GET_CODE (x) == REG)
8689         fprintf (file, "%s", reg_names[REGNO (x) + 2]);
8690       else if (GET_CODE (x) == MEM)
8691         {
8692           if (GET_CODE (XEXP (x, 0)) == PRE_INC
8693               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
8694             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 8));
8695           else
8696             output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
8697           if (small_data_operand (x, GET_MODE (x)))
8698             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
8699                      reg_names[SMALL_DATA_REG]);
8700         }
8701       return;
8702                             
8703     case 'z':
8704       /* X is a SYMBOL_REF.  Write out the name preceded by a
8705          period and without any trailing data in brackets.  Used for function
8706          names.  If we are configured for System V (or the embedded ABI) on
8707          the PowerPC, do not emit the period, since those systems do not use
8708          TOCs and the like.  */
8709       if (GET_CODE (x) != SYMBOL_REF)
8710         abort ();
8711
8712       if (XSTR (x, 0)[0] != '.')
8713         {
8714           switch (DEFAULT_ABI)
8715             {
8716             default:
8717               abort ();
8718
8719             case ABI_AIX:
8720               putc ('.', file);
8721               break;
8722
8723             case ABI_V4:
8724             case ABI_DARWIN:
8725               break;
8726             }
8727         }
8728       if (TARGET_AIX)
8729         RS6000_OUTPUT_BASENAME (file, XSTR (x, 0));
8730       else
8731         assemble_name (file, XSTR (x, 0));
8732       return;
8733
8734     case 'Z':
8735       /* Like 'L', for last word of TImode.  */
8736       if (GET_CODE (x) == REG)
8737         fprintf (file, "%s", reg_names[REGNO (x) + 3]);
8738       else if (GET_CODE (x) == MEM)
8739         {
8740           if (GET_CODE (XEXP (x, 0)) == PRE_INC
8741               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
8742             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 12));
8743           else
8744             output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
8745           if (small_data_operand (x, GET_MODE (x)))
8746             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
8747                      reg_names[SMALL_DATA_REG]);
8748         }
8749       return;
8750
8751       /* Print AltiVec or SPE memory operand.  */
8752     case 'y':
8753       {
8754         rtx tmp;
8755
8756         if (GET_CODE (x) != MEM)
8757           abort ();
8758
8759         tmp = XEXP (x, 0);
8760
8761         if (TARGET_E500)
8762           {
8763             /* Handle [reg].  */
8764             if (GET_CODE (tmp) == REG)
8765               {
8766                 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
8767                 break;
8768               }
8769             /* Handle [reg+UIMM].  */
8770             else if (GET_CODE (tmp) == PLUS &&
8771                      GET_CODE (XEXP (tmp, 1)) == CONST_INT)
8772               {
8773                 int x;
8774
8775                 if (GET_CODE (XEXP (tmp, 0)) != REG)
8776                   abort ();
8777
8778                 x = INTVAL (XEXP (tmp, 1));
8779                 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
8780                 break;
8781               }
8782
8783             /* Fall through.  Must be [reg+reg].  */
8784           }
8785         if (GET_CODE (tmp) == REG)
8786           fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
8787         else if (GET_CODE (tmp) == PLUS && GET_CODE (XEXP (tmp, 1)) == REG)
8788           {
8789             if (REGNO (XEXP (tmp, 0)) == 0)
8790               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
8791                        reg_names[ REGNO (XEXP (tmp, 0)) ]);
8792             else
8793               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
8794                        reg_names[ REGNO (XEXP (tmp, 1)) ]);
8795           }
8796         else
8797           abort ();
8798         break;
8799       }
8800                             
8801     case 0:
8802       if (GET_CODE (x) == REG)
8803         fprintf (file, "%s", reg_names[REGNO (x)]);
8804       else if (GET_CODE (x) == MEM)
8805         {
8806           /* We need to handle PRE_INC and PRE_DEC here, since we need to
8807              know the width from the mode.  */
8808           if (GET_CODE (XEXP (x, 0)) == PRE_INC)
8809             fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
8810                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
8811           else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
8812             fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
8813                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
8814           else
8815             output_address (XEXP (x, 0));
8816         }
8817       else
8818         output_addr_const (file, x);
8819       return;
8820
8821     case '&':
8822       assemble_name (file, rs6000_get_some_local_dynamic_name ());
8823       return;
8824
8825     default:
8826       output_operand_lossage ("invalid %%xn code");
8827     }
8828 }
8829 \f
8830 /* Print the address of an operand.  */
8831
8832 void
8833 print_operand_address (file, x)
8834      FILE *file;
8835      rtx x;
8836 {
8837   if (GET_CODE (x) == REG)
8838     fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
8839   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
8840            || GET_CODE (x) == LABEL_REF)
8841     {
8842       output_addr_const (file, x);
8843       if (small_data_operand (x, GET_MODE (x)))
8844         fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
8845                  reg_names[SMALL_DATA_REG]);
8846       else if (TARGET_TOC)
8847         abort ();
8848     }
8849   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG)
8850     {
8851       if (REGNO (XEXP (x, 0)) == 0)
8852         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
8853                  reg_names[ REGNO (XEXP (x, 0)) ]);
8854       else
8855         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
8856                  reg_names[ REGNO (XEXP (x, 1)) ]);
8857     }
8858   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
8859     fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
8860              INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
8861 #if TARGET_ELF
8862   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
8863            && CONSTANT_P (XEXP (x, 1)))
8864     {
8865       output_addr_const (file, XEXP (x, 1));
8866       fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
8867     }
8868 #endif
8869 #if TARGET_MACHO
8870   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
8871            && CONSTANT_P (XEXP (x, 1)))
8872     {
8873       fprintf (file, "lo16(");
8874       output_addr_const (file, XEXP (x, 1));
8875       fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
8876     }
8877 #endif
8878   else if (legitimate_constant_pool_address_p (x))
8879     {
8880       if (TARGET_AIX && (!TARGET_ELF || !TARGET_MINIMAL_TOC))
8881         {
8882           rtx contains_minus = XEXP (x, 1);
8883           rtx minus, symref;
8884           const char *name;
8885           
8886           /* Find the (minus (sym) (toc)) buried in X, and temporarily
8887              turn it into (sym) for output_addr_const.  */
8888           while (GET_CODE (XEXP (contains_minus, 0)) != MINUS)
8889             contains_minus = XEXP (contains_minus, 0);
8890
8891           minus = XEXP (contains_minus, 0);
8892           symref = XEXP (minus, 0);
8893           XEXP (contains_minus, 0) = symref;
8894           if (TARGET_ELF)
8895             {
8896               char *newname;
8897
8898               name = XSTR (symref, 0);
8899               newname = alloca (strlen (name) + sizeof ("@toc"));
8900               strcpy (newname, name);
8901               strcat (newname, "@toc");
8902               XSTR (symref, 0) = newname;
8903             }
8904           output_addr_const (file, XEXP (x, 1));
8905           if (TARGET_ELF)
8906             XSTR (symref, 0) = name;
8907           XEXP (contains_minus, 0) = minus;
8908         }
8909       else
8910         output_addr_const (file, XEXP (x, 1));
8911
8912       fprintf (file, "(%s)", reg_names[REGNO (XEXP (x, 0))]);
8913     }
8914   else
8915     abort ();
8916 }
8917 \f
8918 /* Target hook for assembling integer objects.  The PowerPC version has
8919    to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
8920    is defined.  It also needs to handle DI-mode objects on 64-bit
8921    targets.  */
8922
8923 static bool
8924 rs6000_assemble_integer (x, size, aligned_p)
8925      rtx x;
8926      unsigned int size;
8927      int aligned_p;
8928 {
8929 #ifdef RELOCATABLE_NEEDS_FIXUP
8930   /* Special handling for SI values.  */
8931   if (size == 4 && aligned_p)
8932     {
8933       extern int in_toc_section PARAMS ((void));
8934       static int recurse = 0;
8935       
8936       /* For -mrelocatable, we mark all addresses that need to be fixed up
8937          in the .fixup section.  */
8938       if (TARGET_RELOCATABLE
8939           && !in_toc_section ()
8940           && !in_text_section ()
8941           && !recurse
8942           && GET_CODE (x) != CONST_INT
8943           && GET_CODE (x) != CONST_DOUBLE
8944           && CONSTANT_P (x))
8945         {
8946           char buf[256];
8947
8948           recurse = 1;
8949           ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
8950           fixuplabelno++;
8951           ASM_OUTPUT_LABEL (asm_out_file, buf);
8952           fprintf (asm_out_file, "\t.long\t(");
8953           output_addr_const (asm_out_file, x);
8954           fprintf (asm_out_file, ")@fixup\n");
8955           fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
8956           ASM_OUTPUT_ALIGN (asm_out_file, 2);
8957           fprintf (asm_out_file, "\t.long\t");
8958           assemble_name (asm_out_file, buf);
8959           fprintf (asm_out_file, "\n\t.previous\n");
8960           recurse = 0;
8961           return true;
8962         }
8963       /* Remove initial .'s to turn a -mcall-aixdesc function
8964          address into the address of the descriptor, not the function
8965          itself.  */
8966       else if (GET_CODE (x) == SYMBOL_REF
8967                && XSTR (x, 0)[0] == '.'
8968                && DEFAULT_ABI == ABI_AIX)
8969         {
8970           const char *name = XSTR (x, 0);
8971           while (*name == '.')
8972             name++;
8973
8974           fprintf (asm_out_file, "\t.long\t%s\n", name);
8975           return true;
8976         }
8977     }
8978 #endif /* RELOCATABLE_NEEDS_FIXUP */
8979   return default_assemble_integer (x, size, aligned_p);
8980 }
8981
8982 #ifdef HAVE_GAS_HIDDEN
8983 /* Emit an assembler directive to set symbol visibility for DECL to
8984    VISIBILITY_TYPE.  */
8985
8986 static void
8987 rs6000_assemble_visibility (decl, vis)
8988      tree decl;
8989      int vis;
8990 {
8991   /* Functions need to have their entry point symbol visibility set as
8992      well as their descriptor symbol visibility.  */
8993   if (DEFAULT_ABI == ABI_AIX && TREE_CODE (decl) == FUNCTION_DECL)
8994     {
8995       static const char * const visibility_types[] = {
8996         NULL, "internal", "hidden", "protected"
8997       };
8998
8999       const char *name, *type;
9000
9001       name = ((* targetm.strip_name_encoding)
9002               (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
9003       type = visibility_types[vis];
9004
9005       fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
9006       fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
9007     }
9008   else
9009     default_assemble_visibility (decl, vis);
9010 }
9011 #endif
9012 \f
9013 enum rtx_code
9014 rs6000_reverse_condition (mode, code)
9015      enum machine_mode mode;
9016      enum rtx_code code;
9017 {
9018   /* Reversal of FP compares takes care -- an ordered compare
9019      becomes an unordered compare and vice versa.  */
9020   if (mode == CCFPmode 
9021       && (!flag_finite_math_only
9022           || code == UNLT || code == UNLE || code == UNGT || code == UNGE
9023           || code == UNEQ || code == LTGT))
9024     return reverse_condition_maybe_unordered (code);
9025   else
9026     return reverse_condition (code);
9027 }
9028
9029 /* Generate a compare for CODE.  Return a brand-new rtx that
9030    represents the result of the compare.  */
9031
9032 static rtx
9033 rs6000_generate_compare (code)
9034      enum rtx_code code;
9035 {
9036   enum machine_mode comp_mode;
9037   rtx compare_result;
9038
9039   if (rs6000_compare_fp_p)
9040     comp_mode = CCFPmode;
9041   else if (code == GTU || code == LTU
9042           || code == GEU || code == LEU)
9043     comp_mode = CCUNSmode;
9044   else
9045     comp_mode = CCmode;
9046
9047   /* First, the compare.  */
9048   compare_result = gen_reg_rtx (comp_mode);
9049
9050   /* SPE FP compare instructions on the GPRs.  Yuck!  */
9051   if ((TARGET_E500 && !TARGET_FPRS && TARGET_HARD_FLOAT)
9052       && rs6000_compare_fp_p)
9053     {
9054       rtx cmp, or1, or2, or_result, compare_result2;
9055
9056       switch (code)
9057         {
9058         case EQ:
9059         case UNEQ:
9060         case NE:
9061         case LTGT:
9062           cmp = flag_finite_math_only
9063             ? gen_tstsfeq_gpr (compare_result, rs6000_compare_op0,
9064                                rs6000_compare_op1)
9065             : gen_cmpsfeq_gpr (compare_result, rs6000_compare_op0,
9066                                rs6000_compare_op1);
9067           break;
9068         case GT:
9069         case GTU:
9070         case UNGT:
9071         case UNGE:
9072         case GE:
9073         case GEU:
9074           cmp = flag_finite_math_only
9075             ? gen_tstsfgt_gpr (compare_result, rs6000_compare_op0,
9076                                rs6000_compare_op1)
9077             : gen_cmpsfgt_gpr (compare_result, rs6000_compare_op0,
9078                                rs6000_compare_op1);
9079           break;
9080         case LT:
9081         case LTU:
9082         case UNLT:
9083         case UNLE:
9084         case LE:
9085         case LEU:
9086           cmp = flag_finite_math_only
9087             ? gen_tstsflt_gpr (compare_result, rs6000_compare_op0,
9088                                rs6000_compare_op1)
9089             : gen_cmpsflt_gpr (compare_result, rs6000_compare_op0,
9090                                rs6000_compare_op1);
9091           break;
9092         default:
9093           abort ();
9094         }
9095
9096       /* Synthesize LE and GE from LT/GT || EQ.  */
9097       if (code == LE || code == GE || code == LEU || code == GEU)
9098         {
9099           /* Synthesize GE/LE frome GT/LT || EQ.  */
9100
9101           emit_insn (cmp);
9102
9103           switch (code)
9104             {
9105             case LE: code = LT; break;
9106             case GE: code = GT; break;
9107             case LEU: code = LT; break;
9108             case GEU: code = GT; break;
9109             default: abort ();
9110             }
9111
9112           or1 = gen_reg_rtx (SImode);
9113           or2 = gen_reg_rtx (SImode);
9114           or_result = gen_reg_rtx (CCEQmode);
9115           compare_result2 = gen_reg_rtx (CCFPmode);
9116
9117           /* Do the EQ.  */
9118           cmp = flag_finite_math_only
9119             ? gen_tstsfeq_gpr (compare_result2, rs6000_compare_op0,
9120                                rs6000_compare_op1)
9121             : gen_cmpsfeq_gpr (compare_result2, rs6000_compare_op0,
9122                                rs6000_compare_op1);
9123           emit_insn (cmp);
9124
9125           /* The MC8540 FP compare instructions set the CR bits
9126              differently than other PPC compare instructions.  For
9127              that matter, there is no generic test instruction, but a
9128              testgt, testlt, and testeq.  For a true condition, bit 2
9129              is set (x1xx) in the CR.  Following the traditional CR
9130              values:
9131
9132              LT    GT    EQ    OV
9133              bit3  bit2  bit1  bit0
9134
9135              ... bit 2 would be a GT CR alias, so later on we
9136              look in the GT bits for the branch instructions.
9137              However, we must be careful to emit correct RTL in
9138              the meantime, so optimizations don't get confused.  */
9139
9140           or1 = gen_rtx (NE, SImode, compare_result, const0_rtx);
9141           or2 = gen_rtx (NE, SImode, compare_result2, const0_rtx);
9142
9143           /* OR them together.  */
9144           cmp = gen_rtx_SET (VOIDmode, or_result,
9145                              gen_rtx_COMPARE (CCEQmode,
9146                                               gen_rtx_IOR (SImode, or1, or2),
9147                                               const_true_rtx));
9148           compare_result = or_result;
9149           code = EQ;
9150         }
9151       else
9152         {
9153           /* We only care about 1 bit (x1xx), so map everything to NE to
9154              maintain rtl sanity.  We'll get to the right bit (x1xx) at
9155              code output time.  */
9156           if (code == NE || code == LTGT)
9157             /* Do the inverse here because we have no cmpne
9158                instruction.  We use the cmpeq instruction and expect
9159                to get a 0 instead.  */
9160             code = EQ;
9161           else
9162             code = NE;
9163         }
9164
9165       emit_insn (cmp);
9166     }
9167   else
9168     emit_insn (gen_rtx_SET (VOIDmode, compare_result,
9169                             gen_rtx_COMPARE (comp_mode,
9170                                              rs6000_compare_op0, 
9171                                              rs6000_compare_op1)));
9172   
9173   /* Some kinds of FP comparisons need an OR operation;
9174      under flag_finite_math_only we don't bother.  */
9175   if (rs6000_compare_fp_p
9176       && ! flag_finite_math_only
9177       && ! (TARGET_HARD_FLOAT && TARGET_E500 && !TARGET_FPRS)
9178       && (code == LE || code == GE
9179           || code == UNEQ || code == LTGT
9180           || code == UNGT || code == UNLT))
9181     {
9182       enum rtx_code or1, or2;
9183       rtx or1_rtx, or2_rtx, compare2_rtx;
9184       rtx or_result = gen_reg_rtx (CCEQmode);
9185       
9186       switch (code)
9187         {
9188         case LE: or1 = LT;  or2 = EQ;  break;
9189         case GE: or1 = GT;  or2 = EQ;  break;
9190         case UNEQ: or1 = UNORDERED;  or2 = EQ;  break;
9191         case LTGT: or1 = LT;  or2 = GT;  break;
9192         case UNGT: or1 = UNORDERED;  or2 = GT;  break;
9193         case UNLT: or1 = UNORDERED;  or2 = LT;  break;
9194         default:  abort ();
9195         }
9196       validate_condition_mode (or1, comp_mode);
9197       validate_condition_mode (or2, comp_mode);
9198       or1_rtx = gen_rtx (or1, SImode, compare_result, const0_rtx);
9199       or2_rtx = gen_rtx (or2, SImode, compare_result, const0_rtx);
9200       compare2_rtx = gen_rtx_COMPARE (CCEQmode,
9201                                       gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
9202                                       const_true_rtx);
9203       emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
9204
9205       compare_result = or_result;
9206       code = EQ;
9207     }
9208
9209   validate_condition_mode (code, GET_MODE (compare_result));
9210   
9211   return gen_rtx (code, VOIDmode, compare_result, const0_rtx);
9212 }
9213
9214
9215 /* Emit the RTL for an sCOND pattern.  */
9216
9217 void
9218 rs6000_emit_sCOND (code, result)
9219      enum rtx_code code;
9220      rtx result;
9221 {
9222   rtx condition_rtx;
9223   enum machine_mode op_mode;
9224   enum rtx_code cond_code;
9225
9226   condition_rtx = rs6000_generate_compare (code);
9227   cond_code = GET_CODE (condition_rtx);
9228
9229   if (cond_code == NE
9230       || cond_code == GE || cond_code == LE
9231       || cond_code == GEU || cond_code == LEU
9232       || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
9233     {
9234       rtx not_result = gen_reg_rtx (CCEQmode);
9235       rtx not_op, rev_cond_rtx;
9236       enum machine_mode cc_mode;
9237       
9238       cc_mode = GET_MODE (XEXP (condition_rtx, 0));
9239
9240       rev_cond_rtx = gen_rtx (rs6000_reverse_condition (cc_mode, cond_code),
9241                               SImode, XEXP (condition_rtx, 0), const0_rtx);
9242       not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
9243       emit_insn (gen_rtx_SET (VOIDmode, not_result, not_op));
9244       condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
9245     }
9246
9247   op_mode = GET_MODE (rs6000_compare_op0);
9248   if (op_mode == VOIDmode)
9249     op_mode = GET_MODE (rs6000_compare_op1);
9250
9251   if (TARGET_POWERPC64 && (op_mode == DImode || rs6000_compare_fp_p))
9252     {
9253       PUT_MODE (condition_rtx, DImode);
9254       convert_move (result, condition_rtx, 0);
9255     }
9256   else
9257     {
9258       PUT_MODE (condition_rtx, SImode);
9259       emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
9260     }
9261 }
9262
9263 /* Emit a branch of kind CODE to location LOC.  */
9264
9265 void
9266 rs6000_emit_cbranch (code, loc)
9267      enum rtx_code code;
9268      rtx loc;
9269 {
9270   rtx condition_rtx, loc_ref;
9271
9272   condition_rtx = rs6000_generate_compare (code);
9273   loc_ref = gen_rtx_LABEL_REF (VOIDmode, loc);
9274   emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
9275                                gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
9276                                                      loc_ref, pc_rtx)));
9277 }
9278
9279 /* Return the string to output a conditional branch to LABEL, which is
9280    the operand number of the label, or -1 if the branch is really a
9281    conditional return.  
9282
9283    OP is the conditional expression.  XEXP (OP, 0) is assumed to be a
9284    condition code register and its mode specifies what kind of
9285    comparison we made.
9286
9287    REVERSED is nonzero if we should reverse the sense of the comparison.
9288
9289    INSN is the insn.  */
9290
9291 char *
9292 output_cbranch (op, label, reversed, insn)
9293      rtx op;
9294      const char * label;
9295      int reversed;
9296      rtx insn;
9297 {
9298   static char string[64];
9299   enum rtx_code code = GET_CODE (op);
9300   rtx cc_reg = XEXP (op, 0);
9301   enum machine_mode mode = GET_MODE (cc_reg);
9302   int cc_regno = REGNO (cc_reg) - CR0_REGNO;
9303   int need_longbranch = label != NULL && get_attr_length (insn) == 8;
9304   int really_reversed = reversed ^ need_longbranch;
9305   char *s = string;
9306   const char *ccode;
9307   const char *pred;
9308   rtx note;
9309
9310   validate_condition_mode (code, mode);
9311
9312   /* Work out which way this really branches.  We could use
9313      reverse_condition_maybe_unordered here always but this
9314      makes the resulting assembler clearer.  */
9315   if (really_reversed)
9316     {
9317       /* Reversal of FP compares takes care -- an ordered compare
9318          becomes an unordered compare and vice versa.  */
9319       if (mode == CCFPmode)
9320         code = reverse_condition_maybe_unordered (code);
9321       else
9322         code = reverse_condition (code);
9323     }
9324
9325   if ((TARGET_E500 && !TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
9326     {
9327       /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
9328          to the GT bit.  */
9329       if (code == EQ)
9330         /* Opposite of GT.  */
9331         code = UNLE;
9332       else if (code == NE)
9333         code = GT;
9334       else
9335         abort ();
9336     }
9337
9338   switch (code)
9339     {
9340       /* Not all of these are actually distinct opcodes, but
9341          we distinguish them for clarity of the resulting assembler.  */
9342     case NE: case LTGT:
9343       ccode = "ne"; break;
9344     case EQ: case UNEQ:
9345       ccode = "eq"; break;
9346     case GE: case GEU: 
9347       ccode = "ge"; break;
9348     case GT: case GTU: case UNGT: 
9349       ccode = "gt"; break;
9350     case LE: case LEU: 
9351       ccode = "le"; break;
9352     case LT: case LTU: case UNLT: 
9353       ccode = "lt"; break;
9354     case UNORDERED: ccode = "un"; break;
9355     case ORDERED: ccode = "nu"; break;
9356     case UNGE: ccode = "nl"; break;
9357     case UNLE: ccode = "ng"; break;
9358     default:
9359       abort ();
9360     }
9361   
9362   /* Maybe we have a guess as to how likely the branch is.  
9363      The old mnemonics don't have a way to specify this information.  */
9364   pred = "";
9365   note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
9366   if (note != NULL_RTX)
9367     {
9368       /* PROB is the difference from 50%.  */
9369       int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
9370       bool always_hint = rs6000_cpu != PROCESSOR_POWER4;
9371
9372       /* Only hint for highly probable/improbable branches on newer
9373          cpus as static prediction overrides processor dynamic
9374          prediction.  For older cpus we may as well always hint, but
9375          assume not taken for branches that are very close to 50% as a
9376          mispredicted taken branch is more expensive than a
9377          mispredicted not-taken branch.  */ 
9378       if (always_hint
9379           || abs (prob) > REG_BR_PROB_BASE / 100 * 48)
9380         {
9381           if (abs (prob) > REG_BR_PROB_BASE / 20
9382               && ((prob > 0) ^ need_longbranch))
9383               pred = "+";
9384           else
9385             pred = "-";
9386         }
9387     }
9388
9389   if (label == NULL)
9390     s += sprintf (s, "{b%sr|b%slr%s} ", ccode, ccode, pred);
9391   else
9392     s += sprintf (s, "{b%s|b%s%s} ", ccode, ccode, pred);
9393
9394   /* We need to escape any '%' characters in the reg_names string.
9395      Assume they'd only be the first character...  */
9396   if (reg_names[cc_regno + CR0_REGNO][0] == '%')
9397     *s++ = '%';
9398   s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
9399
9400   if (label != NULL)
9401     {
9402       /* If the branch distance was too far, we may have to use an
9403          unconditional branch to go the distance.  */
9404       if (need_longbranch)
9405         s += sprintf (s, ",$+8\n\tb %s", label);
9406       else
9407         s += sprintf (s, ",%s", label);
9408     }
9409
9410   return string;
9411 }
9412
9413 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
9414    operands of the last comparison is nonzero/true, FALSE_COND if it
9415    is zero/false.  Return 0 if the hardware has no such operation.  */
9416
9417 int
9418 rs6000_emit_cmove (dest, op, true_cond, false_cond)
9419      rtx dest;
9420      rtx op;
9421      rtx true_cond;
9422      rtx false_cond;
9423 {
9424   enum rtx_code code = GET_CODE (op);
9425   rtx op0 = rs6000_compare_op0;
9426   rtx op1 = rs6000_compare_op1;
9427   REAL_VALUE_TYPE c1;
9428   enum machine_mode compare_mode = GET_MODE (op0);
9429   enum machine_mode result_mode = GET_MODE (dest);
9430   rtx temp;
9431
9432   /* These modes should always match. */
9433   if (GET_MODE (op1) != compare_mode
9434       /* In the isel case however, we can use a compare immediate, so
9435          op1 may be a small constant.  */
9436       && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
9437     return 0;
9438   if (GET_MODE (true_cond) != result_mode)
9439     return 0;
9440   if (GET_MODE (false_cond) != result_mode)
9441     return 0;
9442
9443   /* First, work out if the hardware can do this at all, or
9444      if it's too slow...  */
9445   if (! rs6000_compare_fp_p)
9446     {
9447       if (TARGET_ISEL)
9448         return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
9449       return 0;
9450     }
9451
9452   /* Eliminate half of the comparisons by switching operands, this
9453      makes the remaining code simpler.  */
9454   if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
9455       || code == LTGT || code == LT || code == UNLE)
9456     {
9457       code = reverse_condition_maybe_unordered (code);
9458       temp = true_cond;
9459       true_cond = false_cond;
9460       false_cond = temp;
9461     }
9462
9463   /* UNEQ and LTGT take four instructions for a comparison with zero,
9464      it'll probably be faster to use a branch here too.  */
9465   if (code == UNEQ && HONOR_NANS (compare_mode))
9466     return 0;
9467   
9468   if (GET_CODE (op1) == CONST_DOUBLE)
9469     REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
9470     
9471   /* We're going to try to implement comparisons by performing
9472      a subtract, then comparing against zero.  Unfortunately,
9473      Inf - Inf is NaN which is not zero, and so if we don't
9474      know that the operand is finite and the comparison
9475      would treat EQ different to UNORDERED, we can't do it.  */
9476   if (HONOR_INFINITIES (compare_mode)
9477       && code != GT && code != UNGE
9478       && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
9479       /* Constructs of the form (a OP b ? a : b) are safe.  */
9480       && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
9481           || (! rtx_equal_p (op0, true_cond) 
9482               && ! rtx_equal_p (op1, true_cond))))
9483     return 0;
9484   /* At this point we know we can use fsel.  */
9485
9486   /* Reduce the comparison to a comparison against zero.  */
9487   temp = gen_reg_rtx (compare_mode);
9488   emit_insn (gen_rtx_SET (VOIDmode, temp,
9489                           gen_rtx_MINUS (compare_mode, op0, op1)));
9490   op0 = temp;
9491   op1 = CONST0_RTX (compare_mode);
9492
9493   /* If we don't care about NaNs we can reduce some of the comparisons
9494      down to faster ones.  */
9495   if (! HONOR_NANS (compare_mode))
9496     switch (code)
9497       {
9498       case GT:
9499         code = LE;
9500         temp = true_cond;
9501         true_cond = false_cond;
9502         false_cond = temp;
9503         break;
9504       case UNGE:
9505         code = GE;
9506         break;
9507       case UNEQ:
9508         code = EQ;
9509         break;
9510       default:
9511         break;
9512       }
9513
9514   /* Now, reduce everything down to a GE.  */
9515   switch (code)
9516     {
9517     case GE:
9518       break;
9519
9520     case LE:
9521       temp = gen_reg_rtx (compare_mode);
9522       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
9523       op0 = temp;
9524       break;
9525
9526     case ORDERED:
9527       temp = gen_reg_rtx (compare_mode);
9528       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
9529       op0 = temp;
9530       break;
9531
9532     case EQ:
9533       temp = gen_reg_rtx (compare_mode);
9534       emit_insn (gen_rtx_SET (VOIDmode, temp, 
9535                               gen_rtx_NEG (compare_mode,
9536                                            gen_rtx_ABS (compare_mode, op0))));
9537       op0 = temp;
9538       break;
9539
9540     case UNGE:
9541       /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
9542       temp = gen_reg_rtx (result_mode);
9543       emit_insn (gen_rtx_SET (VOIDmode, temp,
9544                               gen_rtx_IF_THEN_ELSE (result_mode,
9545                                                     gen_rtx_GE (VOIDmode,
9546                                                                 op0, op1),
9547                                                     true_cond, false_cond)));
9548       false_cond = true_cond;
9549       true_cond = temp;
9550
9551       temp = gen_reg_rtx (compare_mode);
9552       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
9553       op0 = temp;
9554       break;
9555
9556     case GT:
9557       /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
9558       temp = gen_reg_rtx (result_mode);
9559       emit_insn (gen_rtx_SET (VOIDmode, temp,
9560                               gen_rtx_IF_THEN_ELSE (result_mode, 
9561                                                     gen_rtx_GE (VOIDmode,
9562                                                                 op0, op1),
9563                                                     true_cond, false_cond)));
9564       true_cond = false_cond;
9565       false_cond = temp;
9566
9567       temp = gen_reg_rtx (compare_mode);
9568       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
9569       op0 = temp;
9570       break;
9571
9572     default:
9573       abort ();
9574     }
9575
9576   emit_insn (gen_rtx_SET (VOIDmode, dest,
9577                           gen_rtx_IF_THEN_ELSE (result_mode,
9578                                                 gen_rtx_GE (VOIDmode,
9579                                                             op0, op1),
9580                                                 true_cond, false_cond)));
9581   return 1;
9582 }
9583
9584 /* Same as above, but for ints (isel).  */
9585
9586 static int
9587 rs6000_emit_int_cmove (dest, op, true_cond, false_cond)
9588      rtx dest;
9589      rtx op;
9590      rtx true_cond;
9591      rtx false_cond;
9592 {
9593   rtx condition_rtx, cr;
9594
9595   /* All isel implementations thus far are 32-bits.  */
9596   if (GET_MODE (rs6000_compare_op0) != SImode)
9597     return 0;
9598
9599   /* We still have to do the compare, because isel doesn't do a
9600      compare, it just looks at the CRx bits set by a previous compare
9601      instruction.  */
9602   condition_rtx = rs6000_generate_compare (GET_CODE (op));
9603   cr = XEXP (condition_rtx, 0);
9604
9605   if (GET_MODE (cr) == CCmode)
9606     emit_insn (gen_isel_signed (dest, condition_rtx,
9607                                 true_cond, false_cond, cr));
9608   else
9609     emit_insn (gen_isel_unsigned (dest, condition_rtx,
9610                                   true_cond, false_cond, cr));
9611
9612   return 1;
9613 }
9614
9615 const char *
9616 output_isel (operands)
9617      rtx *operands;
9618 {
9619   enum rtx_code code;
9620
9621   code = GET_CODE (operands[1]);
9622   if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
9623     {
9624       PUT_CODE (operands[1], reverse_condition (code));
9625       return "isel %0,%3,%2,%j1";
9626     }
9627   else
9628     return "isel %0,%2,%3,%j1";
9629 }
9630
9631 void
9632 rs6000_emit_minmax (dest, code, op0, op1)
9633      rtx dest;
9634      enum rtx_code code;
9635      rtx op0;
9636      rtx op1;
9637 {
9638   enum machine_mode mode = GET_MODE (op0);
9639   enum rtx_code c;
9640   rtx target;
9641
9642   if (code == SMAX || code == SMIN)
9643     c = GE;
9644   else
9645     c = GEU;
9646
9647   if (code == SMAX || code == UMAX)
9648     target = emit_conditional_move (dest, c, op0, op1, mode, 
9649                                     op0, op1, mode, 0);
9650   else
9651     target = emit_conditional_move (dest, c, op0, op1, mode, 
9652                                     op1, op0, mode, 0);
9653   if (target == NULL_RTX)
9654     abort ();
9655   if (target != dest)
9656     emit_move_insn (dest, target);
9657 }
9658
9659 /* Called by altivec splitter.
9660    Input: 
9661           operands[0] : Destination of move
9662           operands[1] : Source of move
9663           noperands   : Size of operands vector
9664    Output:
9665           operands[2-5] ([2-3] in 64 bit) : Destination slots
9666           operands[6-9] ([4-5] in 64 bit) : Source slots
9667
9668    Splits the move of operands[1] to operands[0].
9669    This is done, if GPRs are one of the operands.  In this case
9670    a sequence of simple move insns has to be issued.  The sequence of these
9671    move insns has to be done in correct order to avoid early clobber of the
9672    base register or destructive overlap of registers. 
9673 */
9674           
9675 void
9676 rs6000_split_altivec_in_gprs (rtx *operands)
9677 {
9678     int nregs, reg, i, j;
9679   enum machine_mode mode; 
9680
9681   /* Calculate number to move (2/4 for 32/64 bit mode).  */ 
9682
9683   reg = REG_P (operands[0]) ? REGNO (operands[0]) : REGNO (operands[1]); 
9684   mode = GET_MODE (operands[0]);
9685   nregs = HARD_REGNO_NREGS (reg, mode);                                  
9686   
9687   if (REG_P (operands[1]) 
9688       && REG_P (operands[0]) 
9689       && (REGNO (operands[1]) < REGNO (operands[0])))
9690     {  
9691       /* Move register range backwards, if we have destructive overlap.  */
9692
9693       j = nregs;
9694       for (i = 0; i < nregs; i++)
9695         {
9696           j--;
9697           operands[i + 2] = operand_subword (operands[0], j, 0, mode);
9698           operands[i + 2 + nregs] = 
9699             operand_subword (operands[1], j, 0, mode);   
9700         }
9701     }     
9702   else
9703     {
9704       j = -1;
9705
9706       if (GET_CODE (operands[1]) == MEM)
9707         {
9708           rtx breg;
9709           /* We have offsettable addresses only. If we use one of the
9710              registers to address memory, we have change that register last.  */            
9711           breg = GET_CODE (XEXP (operands[1], 0)) == PLUS ?
9712               XEXP (XEXP (operands[1], 0), 0) :
9713               XEXP (operands[1], 0);
9714
9715           if (REGNO (breg) >= REGNO (operands[0]) 
9716               && REGNO (breg) < REGNO (operands[0]) + nregs)
9717               j = REGNO (breg) - REGNO (operands[0]);
9718         }
9719
9720       for (i = 0; i < nregs; i++)
9721         { 
9722           /* Calculate index to next subword.  */
9723           j++;
9724           if (j == nregs) 
9725             j = 0;
9726
9727           operands[i + 2] = operand_subword (operands[0], j, 0, mode);
9728           operands[i + 2 + nregs] = 
9729             operand_subword (operands[1], j, 0, mode);
9730
9731         }
9732     }
9733 }
9734
9735 \f
9736 /* This page contains routines that are used to determine what the
9737    function prologue and epilogue code will do and write them out.  */
9738
9739 /* Return the first fixed-point register that is required to be
9740    saved. 32 if none.  */
9741
9742 int
9743 first_reg_to_save ()
9744 {
9745   int first_reg;
9746
9747   /* Find lowest numbered live register.  */
9748   for (first_reg = 13; first_reg <= 31; first_reg++)
9749     if (regs_ever_live[first_reg] 
9750         && (! call_used_regs[first_reg]
9751             || (first_reg == RS6000_PIC_OFFSET_TABLE_REGNUM
9752                 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
9753                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic)))))
9754       break;
9755
9756 #if TARGET_MACHO
9757   if (flag_pic
9758       && current_function_uses_pic_offset_table
9759       && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
9760     return RS6000_PIC_OFFSET_TABLE_REGNUM;
9761 #endif
9762
9763   return first_reg;
9764 }
9765
9766 /* Similar, for FP regs.  */
9767
9768 int
9769 first_fp_reg_to_save ()
9770 {
9771   int first_reg;
9772
9773   /* Find lowest numbered live register.  */
9774   for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
9775     if (regs_ever_live[first_reg])
9776       break;
9777
9778   return first_reg;
9779 }
9780
9781 /* Similar, for AltiVec regs.  */
9782
9783 static int
9784 first_altivec_reg_to_save ()
9785 {
9786   int i;
9787
9788   /* Stack frame remains as is unless we are in AltiVec ABI.  */
9789   if (! TARGET_ALTIVEC_ABI)
9790     return LAST_ALTIVEC_REGNO + 1;
9791
9792   /* Find lowest numbered live register.  */
9793   for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
9794     if (regs_ever_live[i])
9795       break;
9796
9797   return i;
9798 }
9799
9800 /* Return a 32-bit mask of the AltiVec registers we need to set in
9801    VRSAVE.  Bit n of the return value is 1 if Vn is live.  The MSB in
9802    the 32-bit word is 0.  */
9803
9804 static unsigned int
9805 compute_vrsave_mask ()
9806 {
9807   unsigned int i, mask = 0;
9808
9809   /* First, find out if we use _any_ altivec registers.  */
9810   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
9811     if (regs_ever_live[i])
9812       mask |= ALTIVEC_REG_BIT (i);
9813
9814   if (mask == 0)
9815     return mask;
9816
9817   /* Next, remove the argument registers from the set.  These must
9818      be in the VRSAVE mask set by the caller, so we don't need to add
9819      them in again.  More importantly, the mask we compute here is
9820      used to generate CLOBBERs in the set_vrsave insn, and we do not
9821      wish the argument registers to die.  */
9822   for (i = cfun->args_info.vregno; i >= ALTIVEC_ARG_MIN_REG; --i)
9823     mask &= ~ALTIVEC_REG_BIT (i);
9824
9825   /* Similarly, remove the return value from the set.  */
9826   {
9827     bool yes = false;
9828     diddle_return_value (is_altivec_return_reg, &yes);
9829     if (yes)
9830       mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
9831   }
9832
9833   return mask;
9834 }
9835
9836 static void
9837 is_altivec_return_reg (reg, xyes)
9838      rtx reg;
9839      void *xyes;
9840 {
9841   bool *yes = (bool *) xyes;
9842   if (REGNO (reg) == ALTIVEC_ARG_RETURN)
9843     *yes = true;
9844 }
9845
9846 \f
9847 /* Calculate the stack information for the current function.  This is
9848    complicated by having two separate calling sequences, the AIX calling
9849    sequence and the V.4 calling sequence.
9850
9851    AIX (and Darwin/Mac OS X) stack frames look like:
9852                                                           32-bit  64-bit
9853         SP----> +---------------------------------------+
9854                 | back chain to caller                  | 0       0
9855                 +---------------------------------------+
9856                 | saved CR                              | 4       8 (8-11)
9857                 +---------------------------------------+
9858                 | saved LR                              | 8       16
9859                 +---------------------------------------+
9860                 | reserved for compilers                | 12      24
9861                 +---------------------------------------+
9862                 | reserved for binders                  | 16      32
9863                 +---------------------------------------+
9864                 | saved TOC pointer                     | 20      40
9865                 +---------------------------------------+
9866                 | Parameter save area (P)               | 24      48
9867                 +---------------------------------------+
9868                 | Alloca space (A)                      | 24+P    etc.
9869                 +---------------------------------------+
9870                 | Local variable space (L)              | 24+P+A
9871                 +---------------------------------------+
9872                 | Float/int conversion temporary (X)    | 24+P+A+L
9873                 +---------------------------------------+
9874                 | Save area for AltiVec registers (W)   | 24+P+A+L+X
9875                 +---------------------------------------+
9876                 | AltiVec alignment padding (Y)         | 24+P+A+L+X+W
9877                 +---------------------------------------+
9878                 | Save area for VRSAVE register (Z)     | 24+P+A+L+X+W+Y
9879                 +---------------------------------------+
9880                 | Save area for GP registers (G)        | 24+P+A+X+L+X+W+Y+Z
9881                 +---------------------------------------+
9882                 | Save area for FP registers (F)        | 24+P+A+X+L+X+W+Y+Z+G
9883                 +---------------------------------------+
9884         old SP->| back chain to caller's caller         |
9885                 +---------------------------------------+
9886
9887    The required alignment for AIX configurations is two words (i.e., 8
9888    or 16 bytes).
9889
9890
9891    V.4 stack frames look like:
9892
9893         SP----> +---------------------------------------+
9894                 | back chain to caller                  | 0
9895                 +---------------------------------------+
9896                 | caller's saved LR                     | 4
9897                 +---------------------------------------+
9898                 | Parameter save area (P)               | 8
9899                 +---------------------------------------+
9900                 | Alloca space (A)                      | 8+P
9901                 +---------------------------------------+    
9902                 | Varargs save area (V)                 | 8+P+A
9903                 +---------------------------------------+    
9904                 | Local variable space (L)              | 8+P+A+V
9905                 +---------------------------------------+    
9906                 | Float/int conversion temporary (X)    | 8+P+A+V+L
9907                 +---------------------------------------+
9908                 | Save area for AltiVec registers (W)   | 8+P+A+V+L+X
9909                 +---------------------------------------+
9910                 | AltiVec alignment padding (Y)         | 8+P+A+V+L+X+W
9911                 +---------------------------------------+
9912                 | Save area for VRSAVE register (Z)     | 8+P+A+V+L+X+W+Y
9913                 +---------------------------------------+
9914                 | SPE: area for 64-bit GP registers     |
9915                 +---------------------------------------+
9916                 | SPE alignment padding                 |
9917                 +---------------------------------------+
9918                 | saved CR (C)                          | 8+P+A+V+L+X+W+Y+Z
9919                 +---------------------------------------+    
9920                 | Save area for GP registers (G)        | 8+P+A+V+L+X+W+Y+Z+C
9921                 +---------------------------------------+    
9922                 | Save area for FP registers (F)        | 8+P+A+V+L+X+W+Y+Z+C+G
9923                 +---------------------------------------+
9924         old SP->| back chain to caller's caller         |
9925                 +---------------------------------------+
9926
9927    The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
9928    given.  (But note below and in sysv4.h that we require only 8 and
9929    may round up the size of our stack frame anyways.  The historical
9930    reason is early versions of powerpc-linux which didn't properly
9931    align the stack at program startup.  A happy side-effect is that
9932    -mno-eabi libraries can be used with -meabi programs.)
9933
9934    The EABI configuration defaults to the V.4 layout.  However,
9935    the stack alignment requirements may differ.  If -mno-eabi is not
9936    given, the required stack alignment is 8 bytes; if -mno-eabi is
9937    given, the required alignment is 16 bytes.  (But see V.4 comment
9938    above.)  */
9939
9940 #ifndef ABI_STACK_BOUNDARY
9941 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
9942 #endif
9943
9944 rs6000_stack_t *
9945 rs6000_stack_info ()
9946 {
9947   static rs6000_stack_t info, zero_info;
9948   rs6000_stack_t *info_ptr = &info;
9949   int reg_size = TARGET_POWERPC64 ? 8 : 4;
9950   int ehrd_size;
9951   int total_raw_size;
9952
9953   /* Zero all fields portably.  */
9954   info = zero_info;
9955
9956   if (TARGET_SPE)
9957     {
9958       /* Cache value so we don't rescan instruction chain over and over.  */
9959       if (cfun->machine->insn_chain_scanned_p == 0)
9960         {
9961           cfun->machine->insn_chain_scanned_p = 1;
9962           info_ptr->spe_64bit_regs_used = (int) spe_func_has_64bit_regs_p ();
9963         }
9964     }
9965
9966   /* Select which calling sequence.  */
9967   info_ptr->abi = DEFAULT_ABI;
9968
9969   /* Calculate which registers need to be saved & save area size.  */
9970   info_ptr->first_gp_reg_save = first_reg_to_save ();
9971   /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM, 
9972      even if it currently looks like we won't.  */
9973   if (((TARGET_TOC && TARGET_MINIMAL_TOC)
9974        || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
9975        || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
9976       && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
9977     info_ptr->gp_size = reg_size * (32 - RS6000_PIC_OFFSET_TABLE_REGNUM);
9978   else
9979     info_ptr->gp_size = reg_size * (32 - info_ptr->first_gp_reg_save);
9980
9981   /* For the SPE, we have an additional upper 32-bits on each GPR.
9982      Ideally we should save the entire 64-bits only when the upper
9983      half is used in SIMD instructions.  Since we only record
9984      registers live (not the size they are used in), this proves
9985      difficult because we'd have to traverse the instruction chain at
9986      the right time, taking reload into account.  This is a real pain,
9987      so we opt to save the GPRs in 64-bits always if but one register
9988      gets used in 64-bits.  Otherwise, all the registers in the frame
9989      get saved in 32-bits.
9990
9991      So... since when we save all GPRs (except the SP) in 64-bits, the
9992      traditional GP save area will be empty.  */
9993   if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
9994     info_ptr->gp_size = 0;
9995
9996   info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
9997   info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
9998
9999   info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
10000   info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
10001                                  - info_ptr->first_altivec_reg_save);
10002
10003   /* Does this function call anything?  */
10004   info_ptr->calls_p = (! current_function_is_leaf
10005                        || cfun->machine->ra_needs_full_frame);
10006
10007   /* Determine if we need to save the link register.  */
10008   if (rs6000_ra_ever_killed ()
10009       || (DEFAULT_ABI == ABI_AIX
10010           && current_function_profile
10011           && !TARGET_PROFILE_KERNEL)
10012 #ifdef TARGET_RELOCATABLE
10013       || (TARGET_RELOCATABLE && (get_pool_size () != 0))
10014 #endif
10015       || (info_ptr->first_fp_reg_save != 64
10016           && !FP_SAVE_INLINE (info_ptr->first_fp_reg_save))
10017       || info_ptr->first_altivec_reg_save <= LAST_ALTIVEC_REGNO
10018       || (DEFAULT_ABI == ABI_V4 && current_function_calls_alloca)
10019       || (DEFAULT_ABI == ABI_DARWIN
10020           && flag_pic
10021           && current_function_uses_pic_offset_table)
10022       || info_ptr->calls_p)
10023     {
10024       info_ptr->lr_save_p = 1;
10025       regs_ever_live[LINK_REGISTER_REGNUM] = 1;
10026     }
10027
10028   /* Determine if we need to save the condition code registers.  */
10029   if (regs_ever_live[CR2_REGNO] 
10030       || regs_ever_live[CR3_REGNO]
10031       || regs_ever_live[CR4_REGNO])
10032     {
10033       info_ptr->cr_save_p = 1;
10034       if (DEFAULT_ABI == ABI_V4)
10035         info_ptr->cr_size = reg_size;
10036     }
10037
10038   /* If the current function calls __builtin_eh_return, then we need
10039      to allocate stack space for registers that will hold data for
10040      the exception handler.  */
10041   if (current_function_calls_eh_return)
10042     {
10043       unsigned int i;
10044       for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
10045         continue;
10046
10047       /* SPE saves EH registers in 64-bits.  */
10048       ehrd_size = i * (TARGET_SPE_ABI
10049                        && info_ptr->spe_64bit_regs_used != 0
10050                        ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
10051     }
10052   else
10053     ehrd_size = 0;
10054
10055   /* Determine various sizes.  */
10056   info_ptr->reg_size     = reg_size;
10057   info_ptr->fixed_size   = RS6000_SAVE_AREA;
10058   info_ptr->varargs_size = RS6000_VARARGS_AREA;
10059   info_ptr->vars_size    = RS6000_ALIGN (get_frame_size (), 8);
10060   info_ptr->parm_size    = RS6000_ALIGN (current_function_outgoing_args_size,
10061                                          8);
10062
10063   if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
10064     info_ptr->spe_gp_size = 8 * (32 - info_ptr->first_gp_reg_save);
10065   else
10066     info_ptr->spe_gp_size = 0;
10067
10068   if (TARGET_ALTIVEC_ABI && TARGET_ALTIVEC_VRSAVE)
10069     {
10070       info_ptr->vrsave_mask = compute_vrsave_mask ();
10071       info_ptr->vrsave_size  = info_ptr->vrsave_mask ? 4 : 0;
10072     }
10073   else
10074     {
10075       info_ptr->vrsave_mask = 0;
10076       info_ptr->vrsave_size = 0;
10077     }
10078
10079   /* Calculate the offsets.  */
10080   switch (DEFAULT_ABI)
10081     {
10082     case ABI_NONE:
10083     default:
10084       abort ();
10085
10086     case ABI_AIX:
10087     case ABI_DARWIN:
10088       info_ptr->fp_save_offset   = - info_ptr->fp_size;
10089       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
10090
10091       if (TARGET_ALTIVEC_ABI)
10092         {
10093           info_ptr->vrsave_save_offset
10094             = info_ptr->gp_save_offset - info_ptr->vrsave_size;
10095
10096           /* Align stack so vector save area is on a quadword boundary.  */
10097           if (info_ptr->altivec_size != 0)
10098             info_ptr->altivec_padding_size
10099               = 16 - (-info_ptr->vrsave_save_offset % 16);
10100           else
10101             info_ptr->altivec_padding_size = 0;
10102
10103           info_ptr->altivec_save_offset
10104             = info_ptr->vrsave_save_offset
10105             - info_ptr->altivec_padding_size
10106             - info_ptr->altivec_size;
10107
10108           /* Adjust for AltiVec case.  */
10109           info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
10110         }
10111       else
10112         info_ptr->ehrd_offset      = info_ptr->gp_save_offset - ehrd_size;
10113       info_ptr->cr_save_offset   = reg_size; /* first word when 64-bit.  */
10114       info_ptr->lr_save_offset   = 2*reg_size;
10115       break;
10116
10117     case ABI_V4:
10118       info_ptr->fp_save_offset   = - info_ptr->fp_size;
10119       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
10120       info_ptr->cr_save_offset   = info_ptr->gp_save_offset - info_ptr->cr_size;
10121
10122       if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
10123       {
10124         /* Align stack so SPE GPR save area is aligned on a
10125            double-word boundary.  */
10126         if (info_ptr->spe_gp_size != 0)
10127           info_ptr->spe_padding_size
10128             = 8 - (-info_ptr->cr_save_offset % 8);
10129         else
10130           info_ptr->spe_padding_size = 0;
10131
10132         info_ptr->spe_gp_save_offset
10133           = info_ptr->cr_save_offset
10134           - info_ptr->spe_padding_size
10135           - info_ptr->spe_gp_size;
10136
10137         /* Adjust for SPE case.  */
10138         info_ptr->toc_save_offset
10139           = info_ptr->spe_gp_save_offset - info_ptr->toc_size;
10140       }
10141       else if (TARGET_ALTIVEC_ABI)
10142         {
10143           info_ptr->vrsave_save_offset
10144             = info_ptr->cr_save_offset - info_ptr->vrsave_size;
10145
10146           /* Align stack so vector save area is on a quadword boundary.  */
10147           if (info_ptr->altivec_size != 0)
10148             info_ptr->altivec_padding_size
10149               = 16 - (-info_ptr->vrsave_save_offset % 16);
10150           else
10151             info_ptr->altivec_padding_size = 0;
10152
10153           info_ptr->altivec_save_offset
10154             = info_ptr->vrsave_save_offset
10155             - info_ptr->altivec_padding_size
10156             - info_ptr->altivec_size;
10157
10158           /* Adjust for AltiVec case.  */
10159           info_ptr->toc_save_offset
10160             = info_ptr->altivec_save_offset - info_ptr->toc_size;
10161         }
10162       else
10163         info_ptr->toc_save_offset  = info_ptr->cr_save_offset - info_ptr->toc_size;
10164       info_ptr->ehrd_offset      = info_ptr->toc_save_offset - ehrd_size;
10165       info_ptr->lr_save_offset   = reg_size;
10166       break;
10167     }
10168
10169   info_ptr->save_size    = RS6000_ALIGN (info_ptr->fp_size
10170                                          + info_ptr->gp_size
10171                                          + info_ptr->altivec_size
10172                                          + info_ptr->altivec_padding_size
10173                                          + info_ptr->spe_gp_size
10174                                          + info_ptr->spe_padding_size
10175                                          + ehrd_size
10176                                          + info_ptr->cr_size
10177                                          + info_ptr->lr_size
10178                                          + info_ptr->vrsave_size
10179                                          + info_ptr->toc_size,
10180                                          (TARGET_ALTIVEC_ABI || ABI_DARWIN)
10181                                          ? 16 : 8);
10182
10183   total_raw_size         = (info_ptr->vars_size
10184                             + info_ptr->parm_size
10185                             + info_ptr->save_size
10186                             + info_ptr->varargs_size
10187                             + info_ptr->fixed_size);
10188
10189   info_ptr->total_size =
10190     RS6000_ALIGN (total_raw_size, ABI_STACK_BOUNDARY / BITS_PER_UNIT);
10191
10192   /* Determine if we need to allocate any stack frame:
10193
10194      For AIX we need to push the stack if a frame pointer is needed
10195      (because the stack might be dynamically adjusted), if we are
10196      debugging, if we make calls, or if the sum of fp_save, gp_save,
10197      and local variables are more than the space needed to save all
10198      non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
10199      + 18*8 = 288 (GPR13 reserved).
10200
10201      For V.4 we don't have the stack cushion that AIX uses, but assume
10202      that the debugger can handle stackless frames.  */
10203
10204   if (info_ptr->calls_p)
10205     info_ptr->push_p = 1;
10206
10207   else if (DEFAULT_ABI == ABI_V4)
10208     info_ptr->push_p = total_raw_size > info_ptr->fixed_size;
10209
10210   else if (frame_pointer_needed)
10211     info_ptr->push_p = 1;
10212
10213   else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
10214     info_ptr->push_p = 1;
10215
10216   else
10217     info_ptr->push_p
10218       = total_raw_size - info_ptr->fixed_size > (TARGET_32BIT ? 220 : 288);
10219
10220   /* Zero offsets if we're not saving those registers.  */
10221   if (info_ptr->fp_size == 0)
10222     info_ptr->fp_save_offset = 0;
10223
10224   if (info_ptr->gp_size == 0)
10225     info_ptr->gp_save_offset = 0;
10226
10227   if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
10228     info_ptr->altivec_save_offset = 0;
10229
10230   if (! TARGET_ALTIVEC_ABI || info_ptr->vrsave_mask == 0)
10231     info_ptr->vrsave_save_offset = 0;
10232
10233   if (! TARGET_SPE_ABI
10234       || info_ptr->spe_64bit_regs_used == 0
10235       || info_ptr->spe_gp_size == 0)
10236     info_ptr->spe_gp_save_offset = 0;
10237
10238   if (! info_ptr->lr_save_p)
10239     info_ptr->lr_save_offset = 0;
10240
10241   if (! info_ptr->cr_save_p)
10242     info_ptr->cr_save_offset = 0;
10243
10244   if (! info_ptr->toc_save_p)
10245     info_ptr->toc_save_offset = 0;
10246
10247   return info_ptr;
10248 }
10249
10250 /* Return true if the current function uses any GPRs in 64-bit SIMD
10251    mode.  */
10252
10253 static bool
10254 spe_func_has_64bit_regs_p ()
10255 {
10256   rtx insns, insn;
10257
10258   /* Functions that save and restore all the call-saved registers will
10259      need to save/restore the registers in 64-bits.  */
10260   if (current_function_calls_eh_return
10261       || current_function_calls_setjmp
10262       || current_function_has_nonlocal_goto)
10263     return true;
10264
10265   insns = get_insns ();
10266
10267   for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
10268     {
10269       if (INSN_P (insn))
10270         {
10271           rtx i;
10272
10273           i = PATTERN (insn);
10274           if (GET_CODE (i) == SET
10275               && SPE_VECTOR_MODE (GET_MODE (SET_SRC (i))))
10276             return true;
10277         }
10278     }
10279
10280   return false;
10281 }
10282
10283 void
10284 debug_stack_info (info)
10285      rs6000_stack_t *info;
10286 {
10287   const char *abi_string;
10288
10289   if (! info)
10290     info = rs6000_stack_info ();
10291
10292   fprintf (stderr, "\nStack information for function %s:\n",
10293            ((current_function_decl && DECL_NAME (current_function_decl))
10294             ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
10295             : "<unknown>"));
10296
10297   switch (info->abi)
10298     {
10299     default:             abi_string = "Unknown";        break;
10300     case ABI_NONE:       abi_string = "NONE";           break;
10301     case ABI_AIX:        abi_string = "AIX";            break;
10302     case ABI_DARWIN:     abi_string = "Darwin";         break;
10303     case ABI_V4:         abi_string = "V.4";            break;
10304     }
10305
10306   fprintf (stderr, "\tABI                 = %5s\n", abi_string);
10307
10308   if (TARGET_ALTIVEC_ABI)
10309     fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
10310
10311   if (TARGET_SPE_ABI)
10312     fprintf (stderr, "\tSPE ABI extensions enabled.\n");
10313
10314   if (info->first_gp_reg_save != 32)
10315     fprintf (stderr, "\tfirst_gp_reg_save   = %5d\n", info->first_gp_reg_save);
10316
10317   if (info->first_fp_reg_save != 64)
10318     fprintf (stderr, "\tfirst_fp_reg_save   = %5d\n", info->first_fp_reg_save);
10319
10320   if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
10321     fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
10322              info->first_altivec_reg_save);
10323
10324   if (info->lr_save_p)
10325     fprintf (stderr, "\tlr_save_p           = %5d\n", info->lr_save_p);
10326
10327   if (info->cr_save_p)
10328     fprintf (stderr, "\tcr_save_p           = %5d\n", info->cr_save_p);
10329
10330   if (info->toc_save_p)
10331     fprintf (stderr, "\ttoc_save_p          = %5d\n", info->toc_save_p);
10332
10333   if (info->vrsave_mask)
10334     fprintf (stderr, "\tvrsave_mask         = 0x%x\n", info->vrsave_mask);
10335
10336   if (info->push_p)
10337     fprintf (stderr, "\tpush_p              = %5d\n", info->push_p);
10338
10339   if (info->calls_p)
10340     fprintf (stderr, "\tcalls_p             = %5d\n", info->calls_p);
10341
10342   if (info->gp_save_offset)
10343     fprintf (stderr, "\tgp_save_offset      = %5d\n", info->gp_save_offset);
10344
10345   if (info->fp_save_offset)
10346     fprintf (stderr, "\tfp_save_offset      = %5d\n", info->fp_save_offset);
10347
10348   if (info->altivec_save_offset)
10349     fprintf (stderr, "\taltivec_save_offset = %5d\n",
10350              info->altivec_save_offset);
10351
10352   if (info->spe_gp_save_offset)
10353     fprintf (stderr, "\tspe_gp_save_offset  = %5d\n",
10354              info->spe_gp_save_offset);
10355
10356   if (info->vrsave_save_offset)
10357     fprintf (stderr, "\tvrsave_save_offset  = %5d\n",
10358              info->vrsave_save_offset);
10359
10360   if (info->lr_save_offset)
10361     fprintf (stderr, "\tlr_save_offset      = %5d\n", info->lr_save_offset);
10362
10363   if (info->cr_save_offset)
10364     fprintf (stderr, "\tcr_save_offset      = %5d\n", info->cr_save_offset);
10365
10366   if (info->toc_save_offset)
10367     fprintf (stderr, "\ttoc_save_offset     = %5d\n", info->toc_save_offset);
10368
10369   if (info->varargs_save_offset)
10370     fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
10371
10372   if (info->total_size)
10373     fprintf (stderr, "\ttotal_size          = %5d\n", info->total_size);
10374
10375   if (info->varargs_size)
10376     fprintf (stderr, "\tvarargs_size        = %5d\n", info->varargs_size);
10377
10378   if (info->vars_size)
10379     fprintf (stderr, "\tvars_size           = %5d\n", info->vars_size);
10380
10381   if (info->parm_size)
10382     fprintf (stderr, "\tparm_size           = %5d\n", info->parm_size);
10383
10384   if (info->fixed_size)
10385     fprintf (stderr, "\tfixed_size          = %5d\n", info->fixed_size);
10386
10387   if (info->gp_size)
10388     fprintf (stderr, "\tgp_size             = %5d\n", info->gp_size);
10389
10390   if (info->spe_gp_size)
10391     fprintf (stderr, "\tspe_gp_size         = %5d\n", info->spe_gp_size);
10392
10393   if (info->fp_size)
10394     fprintf (stderr, "\tfp_size             = %5d\n", info->fp_size);
10395
10396   if (info->altivec_size)
10397     fprintf (stderr, "\taltivec_size        = %5d\n", info->altivec_size);
10398
10399   if (info->vrsave_size)
10400     fprintf (stderr, "\tvrsave_size         = %5d\n", info->vrsave_size);
10401
10402   if (info->altivec_padding_size)
10403     fprintf (stderr, "\taltivec_padding_size= %5d\n",
10404              info->altivec_padding_size);
10405
10406   if (info->spe_padding_size)
10407     fprintf (stderr, "\tspe_padding_size    = %5d\n",
10408              info->spe_padding_size);
10409
10410   if (info->lr_size)
10411     fprintf (stderr, "\tlr_size             = %5d\n", info->lr_size);
10412
10413   if (info->cr_size)
10414     fprintf (stderr, "\tcr_size             = %5d\n", info->cr_size);
10415
10416   if (info->toc_size)
10417     fprintf (stderr, "\ttoc_size            = %5d\n", info->toc_size);
10418
10419   if (info->save_size)
10420     fprintf (stderr, "\tsave_size           = %5d\n", info->save_size);
10421
10422   if (info->reg_size != 4)
10423     fprintf (stderr, "\treg_size            = %5d\n", info->reg_size);
10424
10425   fprintf (stderr, "\n");
10426 }
10427
10428 rtx
10429 rs6000_return_addr (count, frame)
10430      int count;
10431      rtx frame;
10432 {
10433   /* Currently we don't optimize very well between prolog and body
10434      code and for PIC code the code can be actually quite bad, so
10435      don't try to be too clever here.  */
10436   if (count != 0 || (DEFAULT_ABI != ABI_AIX && flag_pic))
10437     {
10438       cfun->machine->ra_needs_full_frame = 1;
10439
10440       return
10441         gen_rtx_MEM
10442           (Pmode,
10443            memory_address
10444            (Pmode,
10445             plus_constant (copy_to_reg
10446                            (gen_rtx_MEM (Pmode,
10447                                          memory_address (Pmode, frame))),
10448                            RETURN_ADDRESS_OFFSET)));
10449     }
10450
10451   return get_hard_reg_initial_val (Pmode, LINK_REGISTER_REGNUM);
10452 }
10453
10454 /* Say whether a function is a candidate for sibcall handling or not.
10455    We do not allow indirect calls to be optimized into sibling calls.
10456    Also, we can't do it if there are any vector parameters; there's
10457    nowhere to put the VRsave code so it works; note that functions with
10458    vector parameters are required to have a prototype, so the argument
10459    type info must be available here.  (The tail recursion case can work
10460    with vector parameters, but there's no way to distinguish here.) */
10461 static bool
10462 rs6000_function_ok_for_sibcall (decl, exp)
10463     tree decl;
10464     tree exp ATTRIBUTE_UNUSED;
10465 {
10466   tree type;
10467   if (decl)
10468     {
10469       if (TARGET_ALTIVEC_VRSAVE)
10470         {
10471           for (type = TYPE_ARG_TYPES (TREE_TYPE (decl));
10472                type; type = TREE_CHAIN (type))
10473             {
10474               if (TREE_CODE (TREE_VALUE (type)) == VECTOR_TYPE)
10475                 return false;
10476             }
10477         }
10478       if (DEFAULT_ABI == ABI_DARWIN
10479           || (*targetm.binds_local_p) (decl))
10480         {
10481           tree attr_list = TYPE_ATTRIBUTES (TREE_TYPE (decl));
10482
10483           if (!lookup_attribute ("longcall", attr_list)
10484               || lookup_attribute ("shortcall", attr_list))
10485             return true;
10486         }
10487     }
10488   return false;
10489 }
10490
10491 static int
10492 rs6000_ra_ever_killed ()
10493 {
10494   rtx top;
10495   rtx reg;
10496   rtx insn;
10497
10498   /* Irritatingly, there are two kinds of thunks -- those created with
10499      TARGET_ASM_OUTPUT_MI_THUNK and those with DECL_THUNK_P that go
10500      through the regular part of the compiler.  This is a very hacky
10501      way to tell them apart.  */
10502   if (current_function_is_thunk && !no_new_pseudos)
10503     return 0;
10504
10505   /* regs_ever_live has LR marked as used if any sibcalls are present,
10506      but this should not force saving and restoring in the
10507      pro/epilogue.  Likewise, reg_set_between_p thinks a sibcall
10508      clobbers LR, so that is inappropriate. */
10509
10510   /* Also, the prologue can generate a store into LR that
10511      doesn't really count, like this:
10512
10513         move LR->R0
10514         bcl to set PIC register
10515         move LR->R31
10516         move R0->LR
10517
10518      When we're called from the epilogue, we need to avoid counting
10519      this as a store.  */
10520          
10521   push_topmost_sequence ();
10522   top = get_insns ();
10523   pop_topmost_sequence ();
10524   reg = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
10525
10526   for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
10527     {
10528       if (INSN_P (insn))
10529         {
10530           if (FIND_REG_INC_NOTE (insn, reg))
10531             return 1;
10532           else if (GET_CODE (insn) == CALL_INSN 
10533                    && !SIBLING_CALL_P (insn))
10534             return 1;
10535           else if (set_of (reg, insn) != NULL_RTX
10536                    && !prologue_epilogue_contains (insn))
10537             return 1;
10538         }
10539     }
10540   return 0;
10541 }
10542 \f
10543 /* Add a REG_MAYBE_DEAD note to the insn.  */
10544 static void
10545 rs6000_maybe_dead (insn)
10546      rtx insn;
10547 {
10548   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
10549                                         const0_rtx,
10550                                         REG_NOTES (insn));
10551 }
10552
10553 /* Emit instructions needed to load the TOC register.
10554    This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
10555    a constant pool; or for SVR4 -fpic.  */
10556
10557 void
10558 rs6000_emit_load_toc_table (fromprolog)
10559      int fromprolog;
10560 {
10561   rtx dest, insn;
10562   dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
10563
10564   if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
10565     {
10566       rtx temp = (fromprolog
10567                   ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
10568                   : gen_reg_rtx (Pmode));
10569       insn = emit_insn (gen_load_toc_v4_pic_si (temp));
10570       if (fromprolog)
10571         rs6000_maybe_dead (insn);
10572       insn = emit_move_insn (dest, temp);
10573       if (fromprolog)
10574         rs6000_maybe_dead (insn);
10575     }
10576   else if (TARGET_ELF && DEFAULT_ABI != ABI_AIX && flag_pic == 2)
10577     {
10578       char buf[30];
10579       rtx tempLR = (fromprolog
10580                     ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
10581                     : gen_reg_rtx (Pmode));
10582       rtx temp0 = (fromprolog
10583                    ? gen_rtx_REG (Pmode, 0)
10584                    : gen_reg_rtx (Pmode));
10585       rtx symF;
10586
10587       /* possibly create the toc section */
10588       if (! toc_initialized)
10589         {
10590           toc_section ();
10591           function_section (current_function_decl);
10592         }
10593
10594       if (fromprolog)
10595         {
10596           rtx symL;
10597
10598           ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
10599           symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
10600
10601           ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
10602           symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
10603
10604           rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_1 (tempLR,
10605                                                                symF)));
10606           rs6000_maybe_dead (emit_move_insn (dest, tempLR));
10607           rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest,
10608                                                                symL,
10609                                                                symF)));
10610         }
10611       else
10612         {
10613           rtx tocsym;
10614           static int reload_toc_labelno = 0;
10615
10616           tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
10617
10618           ASM_GENERATE_INTERNAL_LABEL (buf, "LCG", reload_toc_labelno++);
10619           symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
10620
10621           emit_insn (gen_load_toc_v4_PIC_1b (tempLR, symF, tocsym));
10622           emit_move_insn (dest, tempLR);
10623           emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
10624         }
10625       insn = emit_insn (gen_addsi3 (dest, temp0, dest));
10626       if (fromprolog)
10627         rs6000_maybe_dead (insn);
10628     }
10629   else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
10630     {
10631       /* This is for AIX code running in non-PIC ELF32.  */
10632       char buf[30];
10633       rtx realsym;
10634       ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
10635       realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
10636
10637       insn = emit_insn (gen_elf_high (dest, realsym));
10638       if (fromprolog)
10639         rs6000_maybe_dead (insn);
10640       insn = emit_insn (gen_elf_low (dest, dest, realsym));
10641       if (fromprolog)
10642         rs6000_maybe_dead (insn);
10643     }
10644   else if (DEFAULT_ABI == ABI_AIX)
10645     {
10646       if (TARGET_32BIT)
10647         insn = emit_insn (gen_load_toc_aix_si (dest));
10648       else
10649         insn = emit_insn (gen_load_toc_aix_di (dest));
10650       if (fromprolog)
10651         rs6000_maybe_dead (insn);
10652     }
10653   else
10654     abort ();
10655 }
10656
10657 int   
10658 get_TOC_alias_set ()
10659 {
10660     static int set = -1;
10661     if (set == -1)
10662       set = new_alias_set ();
10663     return set;
10664 }   
10665
10666 /* This returns nonzero if the current function uses the TOC.  This is
10667    determined by the presence of (unspec ... UNSPEC_TOC) or
10668    use (unspec ... UNSPEC_TOC), which are generated by the various
10669    load_toc_* patterns.  */
10670
10671 int
10672 uses_TOC () 
10673 {
10674   rtx insn;
10675
10676   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
10677     if (INSN_P (insn))
10678       {
10679         rtx pat = PATTERN (insn);
10680         int i;
10681
10682         if (GET_CODE (pat) == PARALLEL) 
10683           for (i = 0; i < XVECLEN (pat, 0); i++)
10684             {
10685               rtx sub = XVECEXP (pat, 0, i);
10686               if (GET_CODE (sub) == USE)
10687                 {
10688                   sub = XEXP (sub, 0);
10689                   if (GET_CODE (sub) == UNSPEC
10690                       && XINT (sub, 1) == UNSPEC_TOC)
10691                     return 1;
10692                 }
10693             }
10694       }
10695   return 0;
10696 }
10697
10698 rtx
10699 create_TOC_reference (symbol) 
10700     rtx symbol;
10701 {
10702   return gen_rtx_PLUS (Pmode, 
10703            gen_rtx_REG (Pmode, TOC_REGISTER),
10704              gen_rtx_CONST (Pmode, 
10705                gen_rtx_MINUS (Pmode, symbol, 
10706                  gen_rtx_SYMBOL_REF (Pmode, toc_label_name))));
10707 }
10708
10709 /* __throw will restore its own return address to be the same as the
10710    return address of the function that the throw is being made to.
10711    This is unfortunate, because we want to check the original
10712    return address to see if we need to restore the TOC.
10713    So we have to squirrel it away here.  
10714    This is used only in compiling __throw and __rethrow. 
10715
10716    Most of this code should be removed by CSE.  */
10717 static rtx insn_after_throw;
10718
10719 /* This does the saving...  */
10720 void
10721 rs6000_aix_emit_builtin_unwind_init ()
10722 {
10723   rtx mem;
10724   rtx stack_top = gen_reg_rtx (Pmode);
10725   rtx opcode_addr = gen_reg_rtx (Pmode);
10726
10727   insn_after_throw = gen_reg_rtx (SImode);
10728
10729   mem = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
10730   emit_move_insn (stack_top, mem);
10731
10732   mem = gen_rtx_MEM (Pmode, 
10733                      gen_rtx_PLUS (Pmode, stack_top, 
10734                                    GEN_INT (2 * GET_MODE_SIZE (Pmode))));
10735   emit_move_insn (opcode_addr, mem);
10736   emit_move_insn (insn_after_throw, gen_rtx_MEM (SImode, opcode_addr));
10737 }
10738
10739 /* Emit insns to _restore_ the TOC register, at runtime (specifically
10740    in _eh.o).  Only used on AIX.
10741
10742    The idea is that on AIX, function calls look like this:
10743         bl  somefunction-trampoline
10744         lwz r2,20(sp)
10745
10746    and later,
10747         somefunction-trampoline:
10748         stw r2,20(sp)
10749          ... load function address in the count register ...
10750         bctr
10751    or like this, if the linker determines that this is not a cross-module call
10752    and so the TOC need not be restored:
10753         bl  somefunction
10754         nop
10755    or like this, if the compiler could determine that this is not a
10756    cross-module call:
10757         bl  somefunction
10758    now, the tricky bit here is that register 2 is saved and restored
10759    by the _linker_, so we can't readily generate debugging information
10760    for it.  So we need to go back up the call chain looking at the
10761    insns at return addresses to see which calls saved the TOC register
10762    and so see where it gets restored from.
10763
10764    Oh, and all this gets done in RTL inside the eh_epilogue pattern,
10765    just before the actual epilogue.
10766
10767    On the bright side, this incurs no space or time overhead unless an
10768    exception is thrown, except for the extra code in libgcc.a.  
10769
10770    The parameter STACKSIZE is a register containing (at runtime)
10771    the amount to be popped off the stack in addition to the stack frame
10772    of this routine (which will be __throw or __rethrow, and so is
10773    guaranteed to have a stack frame).  */
10774
10775 void
10776 rs6000_emit_eh_toc_restore (stacksize)
10777      rtx stacksize;
10778 {
10779   rtx top_of_stack;
10780   rtx bottom_of_stack = gen_reg_rtx (Pmode);
10781   rtx tocompare = gen_reg_rtx (SImode);
10782   rtx opcode = gen_reg_rtx (SImode);
10783   rtx opcode_addr = gen_reg_rtx (Pmode);
10784   rtx mem;
10785   rtx loop_start = gen_label_rtx ();
10786   rtx no_toc_restore_needed = gen_label_rtx ();
10787   rtx loop_exit = gen_label_rtx ();
10788   
10789   mem = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
10790   set_mem_alias_set (mem, rs6000_sr_alias_set);
10791   emit_move_insn (bottom_of_stack, mem);
10792
10793   top_of_stack = expand_binop (Pmode, add_optab, 
10794                                bottom_of_stack, stacksize,
10795                                NULL_RTX, 1, OPTAB_WIDEN);
10796
10797   emit_move_insn (tocompare, gen_int_mode (TARGET_32BIT ? 0x80410014 
10798                                            : 0xE8410028, SImode));
10799
10800   if (insn_after_throw == NULL_RTX)
10801     abort ();
10802   emit_move_insn (opcode, insn_after_throw);
10803   
10804   emit_note (NOTE_INSN_LOOP_BEG);
10805   emit_label (loop_start);
10806   
10807   do_compare_rtx_and_jump (opcode, tocompare, NE, 1,
10808                            SImode, NULL_RTX, NULL_RTX,
10809                            no_toc_restore_needed);
10810   
10811   mem = gen_rtx_MEM (Pmode, 
10812                      gen_rtx_PLUS (Pmode, bottom_of_stack, 
10813                                    GEN_INT (5 * GET_MODE_SIZE (Pmode))));
10814   emit_move_insn (gen_rtx_REG (Pmode, 2), mem);
10815
10816   emit_label (no_toc_restore_needed);
10817   do_compare_rtx_and_jump (top_of_stack, bottom_of_stack, EQ, 1,
10818                            Pmode, NULL_RTX, NULL_RTX,
10819                            loop_exit);
10820
10821   mem = gen_rtx_MEM (Pmode, bottom_of_stack);
10822   set_mem_alias_set (mem, rs6000_sr_alias_set);
10823   emit_move_insn (bottom_of_stack, mem);
10824   
10825   mem = gen_rtx_MEM (Pmode, 
10826                      gen_rtx_PLUS (Pmode, bottom_of_stack, 
10827                                    GEN_INT (2 * GET_MODE_SIZE (Pmode))));
10828   emit_move_insn (opcode_addr, mem);
10829   emit_move_insn (opcode, gen_rtx_MEM (SImode, opcode_addr));
10830
10831   emit_note (NOTE_INSN_LOOP_CONT);
10832   emit_jump (loop_start);
10833   emit_note (NOTE_INSN_LOOP_END);
10834   emit_label (loop_exit);
10835 }
10836 \f
10837 /* This ties together stack memory (MEM with an alias set of
10838    rs6000_sr_alias_set) and the change to the stack pointer.  */
10839
10840 static void
10841 rs6000_emit_stack_tie ()
10842 {
10843   rtx mem = gen_rtx_MEM (BLKmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM));
10844
10845   set_mem_alias_set (mem, rs6000_sr_alias_set);
10846   emit_insn (gen_stack_tie (mem));
10847 }
10848
10849 /* Emit the correct code for allocating stack space, as insns.
10850    If COPY_R12, make sure a copy of the old frame is left in r12.
10851    The generated code may use hard register 0 as a temporary.  */
10852
10853 static void
10854 rs6000_emit_allocate_stack (size, copy_r12)
10855      HOST_WIDE_INT size;
10856      int copy_r12;
10857 {
10858   rtx insn;
10859   rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
10860   rtx tmp_reg = gen_rtx_REG (Pmode, 0);
10861   rtx todec = GEN_INT (-size);
10862
10863   if (current_function_limit_stack)
10864     {
10865       if (REG_P (stack_limit_rtx)
10866           && REGNO (stack_limit_rtx) > 1 
10867           && REGNO (stack_limit_rtx) <= 31)
10868         {
10869           emit_insn (TARGET_32BIT
10870                      ? gen_addsi3 (tmp_reg,
10871                                    stack_limit_rtx,
10872                                    GEN_INT (size))
10873                      : gen_adddi3 (tmp_reg,
10874                                    stack_limit_rtx,
10875                                    GEN_INT (size)));
10876
10877           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
10878                                     const0_rtx));
10879         }
10880       else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
10881                && TARGET_32BIT
10882                && DEFAULT_ABI == ABI_V4)
10883         {
10884           rtx toload = gen_rtx_CONST (VOIDmode,
10885                                       gen_rtx_PLUS (Pmode, 
10886                                                     stack_limit_rtx, 
10887                                                     GEN_INT (size)));
10888
10889           emit_insn (gen_elf_high (tmp_reg, toload));
10890           emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
10891           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
10892                                     const0_rtx));
10893         }
10894       else
10895         warning ("stack limit expression is not supported");
10896     }
10897
10898   if (copy_r12 || ! TARGET_UPDATE)
10899     emit_move_insn (gen_rtx_REG (Pmode, 12), stack_reg);
10900
10901   if (TARGET_UPDATE)
10902     {
10903       if (size > 32767)
10904         {
10905           /* Need a note here so that try_split doesn't get confused.  */
10906           if (get_last_insn() == NULL_RTX)
10907             emit_note (NOTE_INSN_DELETED);
10908           insn = emit_move_insn (tmp_reg, todec);
10909           try_split (PATTERN (insn), insn, 0);
10910           todec = tmp_reg;
10911         }
10912
10913       insn = emit_insn (TARGET_32BIT
10914                         ? gen_movsi_update (stack_reg, stack_reg,
10915                                             todec, stack_reg)
10916                         : gen_movdi_update (stack_reg, stack_reg, 
10917                                             todec, stack_reg));
10918     }
10919   else
10920     {
10921       insn = emit_insn (TARGET_32BIT
10922                         ? gen_addsi3 (stack_reg, stack_reg, todec)
10923                         : gen_adddi3 (stack_reg, stack_reg, todec));
10924       emit_move_insn (gen_rtx_MEM (Pmode, stack_reg),
10925                       gen_rtx_REG (Pmode, 12));
10926     }
10927  
10928   RTX_FRAME_RELATED_P (insn) = 1;
10929   REG_NOTES (insn) = 
10930     gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10931                        gen_rtx_SET (VOIDmode, stack_reg, 
10932                                     gen_rtx_PLUS (Pmode, stack_reg,
10933                                                   GEN_INT (-size))),
10934                        REG_NOTES (insn));
10935 }
10936
10937 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
10938    with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
10939    is not NULL.  It would be nice if dwarf2out_frame_debug_expr could
10940    deduce these equivalences by itself so it wasn't necessary to hold
10941    its hand so much.  */
10942
10943 static void
10944 rs6000_frame_related (insn, reg, val, reg2, rreg)
10945      rtx insn;
10946      rtx reg;
10947      HOST_WIDE_INT val;
10948      rtx reg2;
10949      rtx rreg;
10950 {
10951   rtx real, temp;
10952
10953   /* copy_rtx will not make unique copies of registers, so we need to
10954      ensure we don't have unwanted sharing here.  */
10955   if (reg == reg2)
10956     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
10957
10958   if (reg == rreg)
10959     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
10960
10961   real = copy_rtx (PATTERN (insn));
10962
10963   if (reg2 != NULL_RTX)
10964     real = replace_rtx (real, reg2, rreg);
10965   
10966   real = replace_rtx (real, reg, 
10967                       gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
10968                                                         STACK_POINTER_REGNUM),
10969                                     GEN_INT (val)));
10970   
10971   /* We expect that 'real' is either a SET or a PARALLEL containing
10972      SETs (and possibly other stuff).  In a PARALLEL, all the SETs
10973      are important so they all have to be marked RTX_FRAME_RELATED_P.  */
10974
10975   if (GET_CODE (real) == SET)
10976     {
10977       rtx set = real;
10978       
10979       temp = simplify_rtx (SET_SRC (set));
10980       if (temp)
10981         SET_SRC (set) = temp;
10982       temp = simplify_rtx (SET_DEST (set));
10983       if (temp)
10984         SET_DEST (set) = temp;
10985       if (GET_CODE (SET_DEST (set)) == MEM)
10986         {
10987           temp = simplify_rtx (XEXP (SET_DEST (set), 0));
10988           if (temp)
10989             XEXP (SET_DEST (set), 0) = temp;
10990         }
10991     }
10992   else if (GET_CODE (real) == PARALLEL)
10993     {
10994       int i;
10995       for (i = 0; i < XVECLEN (real, 0); i++)
10996         if (GET_CODE (XVECEXP (real, 0, i)) == SET)
10997           {
10998             rtx set = XVECEXP (real, 0, i);
10999             
11000             temp = simplify_rtx (SET_SRC (set));
11001             if (temp)
11002               SET_SRC (set) = temp;
11003             temp = simplify_rtx (SET_DEST (set));
11004             if (temp)
11005               SET_DEST (set) = temp;
11006             if (GET_CODE (SET_DEST (set)) == MEM)
11007               {
11008                 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
11009                 if (temp)
11010                   XEXP (SET_DEST (set), 0) = temp;
11011               }
11012             RTX_FRAME_RELATED_P (set) = 1;
11013           }
11014     }
11015   else
11016     abort ();
11017
11018   if (TARGET_SPE)
11019     real = spe_synthesize_frame_save (real);
11020
11021   RTX_FRAME_RELATED_P (insn) = 1;
11022   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
11023                                         real,
11024                                         REG_NOTES (insn));
11025 }
11026
11027 /* Given an SPE frame note, return a PARALLEL of SETs with the
11028    original note, plus a synthetic register save.  */
11029
11030 static rtx
11031 spe_synthesize_frame_save (real)
11032      rtx real;
11033 {
11034   rtx synth, offset, reg, real2;
11035
11036   if (GET_CODE (real) != SET
11037       || GET_MODE (SET_SRC (real)) != V2SImode)
11038     return real;
11039
11040   /* For the SPE, registers saved in 64-bits, get a PARALLEL for their
11041      frame related note.  The parallel contains a set of the register
11042      being saved, and another set to a synthetic register (n+1200).
11043      This is so we can differentiate between 64-bit and 32-bit saves.
11044      Words cannot describe this nastiness.  */
11045
11046   if (GET_CODE (SET_DEST (real)) != MEM
11047       || GET_CODE (XEXP (SET_DEST (real), 0)) != PLUS
11048       || GET_CODE (SET_SRC (real)) != REG)
11049     abort ();
11050
11051   /* Transform:
11052        (set (mem (plus (reg x) (const y)))
11053             (reg z))
11054      into:
11055        (set (mem (plus (reg x) (const y+4)))
11056             (reg z+1200))
11057   */
11058
11059   real2 = copy_rtx (real);
11060   PUT_MODE (SET_DEST (real2), SImode);
11061   reg = SET_SRC (real2);
11062   real2 = replace_rtx (real2, reg, gen_rtx_REG (SImode, REGNO (reg)));
11063   synth = copy_rtx (real2);
11064
11065   if (BYTES_BIG_ENDIAN)
11066     {
11067       offset = XEXP (XEXP (SET_DEST (real2), 0), 1);
11068       real2 = replace_rtx (real2, offset, GEN_INT (INTVAL (offset) + 4));
11069     }
11070
11071   reg = SET_SRC (synth);
11072
11073   synth = replace_rtx (synth, reg,
11074                        gen_rtx_REG (SImode, REGNO (reg) + 1200));
11075
11076   offset = XEXP (XEXP (SET_DEST (synth), 0), 1);
11077   synth = replace_rtx (synth, offset,
11078                        GEN_INT (INTVAL (offset)
11079                                 + (BYTES_BIG_ENDIAN ? 0 : 4)));
11080
11081   RTX_FRAME_RELATED_P (synth) = 1;
11082   RTX_FRAME_RELATED_P (real2) = 1;
11083   if (BYTES_BIG_ENDIAN)
11084     real = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, synth, real2));
11085   else
11086     real = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, real2, synth));
11087
11088   return real;
11089 }
11090
11091 /* Returns an insn that has a vrsave set operation with the
11092    appropriate CLOBBERs.  */
11093
11094 static rtx
11095 generate_set_vrsave (reg, info, epiloguep)
11096      rtx reg;
11097      rs6000_stack_t *info;
11098      int epiloguep;
11099 {
11100   int nclobs, i;
11101   rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
11102   rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
11103
11104   clobs[0]
11105     = gen_rtx_SET (VOIDmode,
11106                    vrsave,
11107                    gen_rtx_UNSPEC_VOLATILE (SImode,
11108                                             gen_rtvec (2, reg, vrsave),
11109                                             30));
11110
11111   nclobs = 1;
11112
11113   /* We need to clobber the registers in the mask so the scheduler
11114      does not move sets to VRSAVE before sets of AltiVec registers.
11115
11116      However, if the function receives nonlocal gotos, reload will set
11117      all call saved registers live.  We will end up with:
11118
11119         (set (reg 999) (mem))
11120         (parallel [ (set (reg vrsave) (unspec blah))
11121                     (clobber (reg 999))])
11122
11123      The clobber will cause the store into reg 999 to be dead, and
11124      flow will attempt to delete an epilogue insn.  In this case, we
11125      need an unspec use/set of the register.  */
11126
11127   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
11128     if (info->vrsave_mask != 0 && ALTIVEC_REG_BIT (i) != 0)
11129       {
11130         if (!epiloguep || call_used_regs [i])
11131           clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
11132                                              gen_rtx_REG (V4SImode, i));
11133         else
11134           {
11135             rtx reg = gen_rtx_REG (V4SImode, i);
11136
11137             clobs[nclobs++]
11138               = gen_rtx_SET (VOIDmode,
11139                              reg,
11140                              gen_rtx_UNSPEC (V4SImode,
11141                                              gen_rtvec (1, reg), 27));
11142           }
11143       }
11144
11145   insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
11146
11147   for (i = 0; i < nclobs; ++i)
11148     XVECEXP (insn, 0, i) = clobs[i];
11149
11150   return insn;
11151 }
11152
11153 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
11154    Save REGNO into [FRAME_REG + OFFSET] in mode MODE.  */
11155
11156 static void
11157 emit_frame_save (frame_reg, frame_ptr, mode, regno, offset, total_size)
11158      rtx frame_reg;
11159      rtx frame_ptr;
11160      enum machine_mode mode;
11161      unsigned int regno;
11162      int offset;
11163      int total_size;
11164 {
11165   rtx reg, offset_rtx, insn, mem, addr, int_rtx;
11166   rtx replacea, replaceb;
11167
11168   int_rtx = GEN_INT (offset);
11169
11170   /* Some cases that need register indexed addressing.  */
11171   if ((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
11172       || (TARGET_SPE_ABI
11173           && SPE_VECTOR_MODE (mode)
11174           && !SPE_CONST_OFFSET_OK (offset)))
11175     {
11176       /* Whomever calls us must make sure r11 is available in the
11177          flow path of instructions in the prologue.  */
11178       offset_rtx = gen_rtx_REG (Pmode, 11);
11179       emit_move_insn (offset_rtx, int_rtx);
11180
11181       replacea = offset_rtx;
11182       replaceb = int_rtx;
11183     }
11184   else
11185     {
11186       offset_rtx = int_rtx;
11187       replacea = NULL_RTX;
11188       replaceb = NULL_RTX;
11189     }
11190
11191   reg = gen_rtx_REG (mode, regno);
11192   addr = gen_rtx_PLUS (Pmode, frame_reg, offset_rtx);
11193   mem = gen_rtx_MEM (mode, addr);
11194   set_mem_alias_set (mem, rs6000_sr_alias_set);
11195
11196   insn = emit_move_insn (mem, reg);
11197
11198   rs6000_frame_related (insn, frame_ptr, total_size, replacea, replaceb);
11199 }
11200
11201 /* Emit an offset memory reference suitable for a frame store, while
11202    converting to a valid addressing mode.  */
11203
11204 static rtx
11205 gen_frame_mem_offset (mode, reg, offset)
11206      enum machine_mode mode;
11207      rtx reg;
11208      int offset;
11209 {
11210   rtx int_rtx, offset_rtx;
11211
11212   int_rtx = GEN_INT (offset);
11213
11214   if (TARGET_SPE_ABI && SPE_VECTOR_MODE (mode))
11215     {
11216       offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
11217       emit_move_insn (offset_rtx, int_rtx);
11218     }
11219   else
11220     offset_rtx = int_rtx;
11221
11222   return gen_rtx_MEM (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
11223 }
11224
11225 /* Emit function prologue as insns.  */
11226
11227 void
11228 rs6000_emit_prologue ()
11229 {
11230   rs6000_stack_t *info = rs6000_stack_info ();
11231   enum machine_mode reg_mode = TARGET_POWERPC64 ? DImode : SImode;
11232   int reg_size = TARGET_POWERPC64 ? 8 : 4;
11233   rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
11234   rtx frame_ptr_rtx = gen_rtx_REG (Pmode, 12);
11235   rtx frame_reg_rtx = sp_reg_rtx;
11236   rtx cr_save_rtx = NULL;
11237   rtx insn;
11238   int saving_FPRs_inline;
11239   int using_store_multiple;
11240   HOST_WIDE_INT sp_offset = 0;
11241   
11242    if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
11243      {
11244        reg_mode = V2SImode;
11245        reg_size = 8;
11246      }
11247
11248   using_store_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
11249                           && (!TARGET_SPE_ABI
11250                               || info->spe_64bit_regs_used == 0)
11251                           && info->first_gp_reg_save < 31);
11252   saving_FPRs_inline = (info->first_fp_reg_save == 64
11253                         || FP_SAVE_INLINE (info->first_fp_reg_save));
11254
11255   /* For V.4, update stack before we do any saving and set back pointer.  */
11256   if (info->push_p && DEFAULT_ABI == ABI_V4)
11257     {
11258       if (info->total_size < 32767)
11259         sp_offset = info->total_size;
11260       else
11261         frame_reg_rtx = frame_ptr_rtx;
11262       rs6000_emit_allocate_stack (info->total_size, 
11263                                   (frame_reg_rtx != sp_reg_rtx
11264                                    && (info->cr_save_p
11265                                        || info->lr_save_p
11266                                        || info->first_fp_reg_save < 64
11267                                        || info->first_gp_reg_save < 32
11268                                        )));
11269       if (frame_reg_rtx != sp_reg_rtx)
11270         rs6000_emit_stack_tie ();
11271     }
11272
11273   /* Save AltiVec registers if needed.  */
11274   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
11275     {
11276       int i;
11277
11278       /* There should be a non inline version of this, for when we
11279          are saving lots of vector registers.  */
11280       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
11281         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
11282           {
11283             rtx areg, savereg, mem;
11284             int offset;
11285
11286             offset = info->altivec_save_offset + sp_offset
11287               + 16 * (i - info->first_altivec_reg_save);
11288
11289             savereg = gen_rtx_REG (V4SImode, i);
11290
11291             areg = gen_rtx_REG (Pmode, 0);
11292             emit_move_insn (areg, GEN_INT (offset));
11293
11294             /* AltiVec addressing mode is [reg+reg].  */
11295             mem = gen_rtx_MEM (V4SImode,
11296                                gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
11297                                
11298             set_mem_alias_set (mem, rs6000_sr_alias_set);
11299
11300             insn = emit_move_insn (mem, savereg);
11301
11302             rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
11303                                   areg, GEN_INT (offset));
11304           }
11305     }
11306
11307   /* VRSAVE is a bit vector representing which AltiVec registers
11308      are used.  The OS uses this to determine which vector
11309      registers to save on a context switch.  We need to save
11310      VRSAVE on the stack frame, add whatever AltiVec registers we
11311      used in this function, and do the corresponding magic in the
11312      epilogue.  */
11313
11314   if (TARGET_ALTIVEC && info->vrsave_mask != 0)
11315     {
11316       rtx reg, mem, vrsave;
11317       int offset;
11318
11319       /* Get VRSAVE onto a GPR.  */
11320       reg = gen_rtx_REG (SImode, 12);
11321       vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
11322       if (TARGET_MACHO)
11323         emit_insn (gen_get_vrsave_internal (reg));
11324       else
11325         emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
11326
11327       /* Save VRSAVE.  */
11328       offset = info->vrsave_save_offset + sp_offset;
11329       mem
11330         = gen_rtx_MEM (SImode,
11331                        gen_rtx_PLUS (Pmode, frame_reg_rtx, GEN_INT (offset)));
11332       set_mem_alias_set (mem, rs6000_sr_alias_set);
11333       insn = emit_move_insn (mem, reg);
11334
11335       /* Include the registers in the mask.  */
11336       emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
11337
11338       insn = emit_insn (generate_set_vrsave (reg, info, 0));
11339     }
11340
11341   /* If we use the link register, get it into r0.  */
11342   if (info->lr_save_p)
11343     emit_move_insn (gen_rtx_REG (Pmode, 0),
11344                     gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
11345
11346   /* If we need to save CR, put it into r12.  */
11347   if (info->cr_save_p && frame_reg_rtx != frame_ptr_rtx)
11348     {
11349       cr_save_rtx = gen_rtx_REG (SImode, 12);
11350       emit_insn (gen_movesi_from_cr (cr_save_rtx));
11351     }
11352
11353   /* Do any required saving of fpr's.  If only one or two to save, do
11354      it ourselves.  Otherwise, call function.  */
11355   if (saving_FPRs_inline)
11356     {
11357       int i;
11358       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
11359         if ((regs_ever_live[info->first_fp_reg_save+i] 
11360              && ! call_used_regs[info->first_fp_reg_save+i]))
11361           emit_frame_save (frame_reg_rtx, frame_ptr_rtx, DFmode,
11362                            info->first_fp_reg_save + i,
11363                            info->fp_save_offset + sp_offset + 8 * i,
11364                            info->total_size);
11365     }
11366   else if (info->first_fp_reg_save != 64)
11367     {
11368       int i;
11369       char rname[30];
11370       const char *alloc_rname;
11371       rtvec p;
11372       p = rtvec_alloc (2 + 64 - info->first_fp_reg_save);
11373       
11374       RTVEC_ELT (p, 0) = gen_rtx_CLOBBER (VOIDmode, 
11375                                           gen_rtx_REG (Pmode, 
11376                                                        LINK_REGISTER_REGNUM));
11377       sprintf (rname, "%s%d%s", SAVE_FP_PREFIX,
11378                info->first_fp_reg_save - 32, SAVE_FP_SUFFIX);
11379       alloc_rname = ggc_strdup (rname);
11380       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode,
11381                                       gen_rtx_SYMBOL_REF (Pmode,
11382                                                           alloc_rname));
11383       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
11384         {
11385           rtx addr, reg, mem;
11386           reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
11387           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
11388                                GEN_INT (info->fp_save_offset 
11389                                         + sp_offset + 8*i));
11390           mem = gen_rtx_MEM (DFmode, addr);
11391           set_mem_alias_set (mem, rs6000_sr_alias_set);
11392
11393           RTVEC_ELT (p, i + 2) = gen_rtx_SET (VOIDmode, mem, reg);
11394         }
11395       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
11396       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
11397                             NULL_RTX, NULL_RTX);
11398     }
11399
11400   /* Save GPRs.  This is done as a PARALLEL if we are using
11401      the store-multiple instructions.  */
11402   if (using_store_multiple)
11403     {
11404       rtvec p;
11405       int i;
11406       p = rtvec_alloc (32 - info->first_gp_reg_save);
11407       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
11408         {
11409           rtx addr, reg, mem;
11410           reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
11411           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
11412                                GEN_INT (info->gp_save_offset 
11413                                         + sp_offset 
11414                                         + reg_size * i));
11415           mem = gen_rtx_MEM (reg_mode, addr);
11416           set_mem_alias_set (mem, rs6000_sr_alias_set);
11417
11418           RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, reg);
11419         }
11420       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
11421       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
11422                             NULL_RTX, NULL_RTX);
11423     }
11424   else
11425     {
11426       int i;
11427       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
11428         if ((regs_ever_live[info->first_gp_reg_save+i] 
11429              && ! call_used_regs[info->first_gp_reg_save+i])
11430             || (i+info->first_gp_reg_save == RS6000_PIC_OFFSET_TABLE_REGNUM
11431                 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
11432                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
11433           {
11434             rtx addr, reg, mem;
11435             reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
11436
11437             if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
11438               {
11439                 int offset = info->spe_gp_save_offset + sp_offset + 8 * i;
11440                 rtx b;
11441
11442                 if (!SPE_CONST_OFFSET_OK (offset))
11443                   {
11444                     b = gen_rtx_REG (Pmode, FIXED_SCRATCH);
11445                     emit_move_insn (b, GEN_INT (offset));
11446                   }
11447                 else
11448                   b = GEN_INT (offset);
11449
11450                 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, b);
11451                 mem = gen_rtx_MEM (V2SImode, addr);
11452                 set_mem_alias_set (mem, rs6000_sr_alias_set);
11453                 insn = emit_move_insn (mem, reg);
11454
11455                 if (GET_CODE (b) == CONST_INT)
11456                   rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
11457                                         NULL_RTX, NULL_RTX);
11458                 else
11459                   rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
11460                                         b, GEN_INT (offset));
11461               }
11462             else
11463               {
11464                 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
11465                                      GEN_INT (info->gp_save_offset 
11466                                               + sp_offset 
11467                                               + reg_size * i));
11468                 mem = gen_rtx_MEM (reg_mode, addr);
11469                 set_mem_alias_set (mem, rs6000_sr_alias_set);
11470
11471                 insn = emit_move_insn (mem, reg);
11472                 rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
11473                                       NULL_RTX, NULL_RTX);
11474               }
11475           }
11476     }
11477
11478   /* ??? There's no need to emit actual instructions here, but it's the
11479      easiest way to get the frame unwind information emitted.  */
11480   if (current_function_calls_eh_return)
11481     {
11482       unsigned int i, regno;
11483
11484       for (i = 0; ; ++i)
11485         {
11486           regno = EH_RETURN_DATA_REGNO (i);
11487           if (regno == INVALID_REGNUM)
11488             break;
11489
11490           emit_frame_save (frame_reg_rtx, frame_ptr_rtx, reg_mode, regno,
11491                            info->ehrd_offset + sp_offset
11492                            + reg_size * (int) i,
11493                            info->total_size);
11494         }
11495     }
11496
11497   /* Save lr if we used it.  */
11498   if (info->lr_save_p)
11499     {
11500       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
11501                                GEN_INT (info->lr_save_offset + sp_offset));
11502       rtx reg = gen_rtx_REG (Pmode, 0);
11503       rtx mem = gen_rtx_MEM (Pmode, addr);
11504       /* This should not be of rs6000_sr_alias_set, because of
11505          __builtin_return_address.  */
11506       
11507       insn = emit_move_insn (mem, reg);
11508       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
11509                             reg, gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
11510     }
11511
11512   /* Save CR if we use any that must be preserved.  */
11513   if (info->cr_save_p)
11514     {
11515       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
11516                                GEN_INT (info->cr_save_offset + sp_offset));
11517       rtx mem = gen_rtx_MEM (SImode, addr);
11518
11519       set_mem_alias_set (mem, rs6000_sr_alias_set);
11520
11521       /* If r12 was used to hold the original sp, copy cr into r0 now
11522          that it's free.  */
11523       if (REGNO (frame_reg_rtx) == 12)
11524         {
11525           cr_save_rtx = gen_rtx_REG (SImode, 0);
11526           emit_insn (gen_movesi_from_cr (cr_save_rtx));
11527         }
11528       insn = emit_move_insn (mem, cr_save_rtx);
11529
11530       /* Now, there's no way that dwarf2out_frame_debug_expr is going
11531          to understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)'.
11532          But that's OK.  All we have to do is specify that _one_ condition
11533          code register is saved in this stack slot.  The thrower's epilogue
11534          will then restore all the call-saved registers.
11535          We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux.  */
11536       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
11537                             cr_save_rtx, gen_rtx_REG (SImode, CR2_REGNO));
11538     }
11539
11540   /* Update stack and set back pointer unless this is V.4, 
11541      for which it was done previously.  */
11542   if (info->push_p && DEFAULT_ABI != ABI_V4)
11543     rs6000_emit_allocate_stack (info->total_size, FALSE);
11544
11545   /* Set frame pointer, if needed.  */
11546   if (frame_pointer_needed)
11547     {
11548       insn = emit_move_insn (gen_rtx_REG (Pmode, FRAME_POINTER_REGNUM), 
11549                              sp_reg_rtx);
11550       RTX_FRAME_RELATED_P (insn) = 1;
11551     }
11552
11553   /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up.  */
11554   if ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
11555       || (DEFAULT_ABI == ABI_V4 && flag_pic == 1
11556           && regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM]))
11557   {
11558     /* If emit_load_toc_table will use the link register, we need to save
11559        it.  We use R12 for this purpose because emit_load_toc_table
11560        can use register 0.  This allows us to use a plain 'blr' to return
11561        from the procedure more often.  */
11562     int save_LR_around_toc_setup = (TARGET_ELF
11563                                     && DEFAULT_ABI != ABI_AIX
11564                                     && flag_pic
11565                                     && ! info->lr_save_p
11566                                     && EXIT_BLOCK_PTR->pred != NULL);
11567     if (save_LR_around_toc_setup)
11568       {
11569         rtx lr = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
11570         rs6000_maybe_dead (emit_move_insn (frame_ptr_rtx, lr));
11571         rs6000_emit_load_toc_table (TRUE);
11572         rs6000_maybe_dead (emit_move_insn (lr, frame_ptr_rtx));
11573       }
11574     else
11575       rs6000_emit_load_toc_table (TRUE);
11576   }
11577
11578 #if TARGET_MACHO
11579   if (DEFAULT_ABI == ABI_DARWIN
11580       && flag_pic && current_function_uses_pic_offset_table)
11581     {
11582       rtx dest = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
11583       const char *picbase = machopic_function_base_name ();
11584       rtx src = gen_rtx_SYMBOL_REF (Pmode, picbase);
11585
11586       rs6000_maybe_dead (emit_insn (gen_load_macho_picbase (dest, src)));
11587
11588       rs6000_maybe_dead (
11589         emit_move_insn (gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM),
11590                         gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)));
11591     }
11592 #endif
11593 }
11594
11595 /* Write function prologue.  */
11596
11597 static void
11598 rs6000_output_function_prologue (file, size)
11599      FILE *file;
11600      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
11601 {
11602   rs6000_stack_t *info = rs6000_stack_info ();
11603
11604   if (TARGET_DEBUG_STACK)
11605     debug_stack_info (info);
11606
11607   /* Write .extern for any function we will call to save and restore
11608      fp values.  */
11609   if (info->first_fp_reg_save < 64
11610       && !FP_SAVE_INLINE (info->first_fp_reg_save))
11611     fprintf (file, "\t.extern %s%d%s\n\t.extern %s%d%s\n",
11612              SAVE_FP_PREFIX, info->first_fp_reg_save - 32, SAVE_FP_SUFFIX,
11613              RESTORE_FP_PREFIX, info->first_fp_reg_save - 32,
11614              RESTORE_FP_SUFFIX);
11615
11616   /* Write .extern for AIX common mode routines, if needed.  */
11617   if (! TARGET_POWER && ! TARGET_POWERPC && ! common_mode_defined)
11618     {
11619       fputs ("\t.extern __mulh\n", file);
11620       fputs ("\t.extern __mull\n", file);
11621       fputs ("\t.extern __divss\n", file);
11622       fputs ("\t.extern __divus\n", file);
11623       fputs ("\t.extern __quoss\n", file);
11624       fputs ("\t.extern __quous\n", file);
11625       common_mode_defined = 1;
11626     }
11627
11628   if (! HAVE_prologue)
11629     {
11630       start_sequence ();
11631
11632       /* A NOTE_INSN_DELETED is supposed to be at the start and end of
11633          the "toplevel" insn chain.  */
11634       emit_note (NOTE_INSN_DELETED);
11635       rs6000_emit_prologue ();
11636       emit_note (NOTE_INSN_DELETED);
11637
11638       /* Expand INSN_ADDRESSES so final() doesn't crash. */
11639       {
11640         rtx insn;
11641         unsigned addr = 0;
11642         for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
11643           {
11644             INSN_ADDRESSES_NEW (insn, addr);
11645             addr += 4;
11646           }
11647       }
11648
11649       if (TARGET_DEBUG_STACK)
11650         debug_rtx_list (get_insns (), 100);
11651       final (get_insns (), file, FALSE, FALSE);
11652       end_sequence ();
11653     }
11654
11655   rs6000_pic_labelno++;
11656 }
11657   
11658 /* Emit function epilogue as insns.
11659
11660    At present, dwarf2out_frame_debug_expr doesn't understand
11661    register restores, so we don't bother setting RTX_FRAME_RELATED_P
11662    anywhere in the epilogue.  Most of the insns below would in any case
11663    need special notes to explain where r11 is in relation to the stack.  */
11664
11665 void
11666 rs6000_emit_epilogue (sibcall)
11667      int sibcall;
11668 {
11669   rs6000_stack_t *info;
11670   int restoring_FPRs_inline;
11671   int using_load_multiple;
11672   int using_mfcr_multiple;
11673   int use_backchain_to_restore_sp;
11674   int sp_offset = 0;
11675   rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
11676   rtx frame_reg_rtx = sp_reg_rtx;
11677   enum machine_mode reg_mode = TARGET_POWERPC64 ? DImode : SImode;
11678   int reg_size = TARGET_POWERPC64 ? 8 : 4;
11679   int i;
11680
11681   info = rs6000_stack_info ();
11682
11683   if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
11684     {
11685       reg_mode = V2SImode;
11686       reg_size = 8;
11687     }
11688
11689   using_load_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
11690                          && (!TARGET_SPE_ABI
11691                              || info->spe_64bit_regs_used == 0)
11692                          && info->first_gp_reg_save < 31);
11693   restoring_FPRs_inline = (sibcall
11694                            || current_function_calls_eh_return
11695                            || info->first_fp_reg_save == 64
11696                            || FP_SAVE_INLINE (info->first_fp_reg_save));
11697   use_backchain_to_restore_sp = (frame_pointer_needed 
11698                                  || current_function_calls_alloca
11699                                  || info->total_size > 32767);
11700   using_mfcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
11701                          || rs6000_cpu == PROCESSOR_PPC603
11702                          || rs6000_cpu == PROCESSOR_PPC750
11703                          || optimize_size);
11704
11705   /* If we have a frame pointer, a call to alloca,  or a large stack
11706      frame, restore the old stack pointer using the backchain.  Otherwise,
11707      we know what size to update it with.  */
11708   if (use_backchain_to_restore_sp)
11709     {
11710       /* Under V.4, don't reset the stack pointer until after we're done
11711          loading the saved registers.  */
11712       if (DEFAULT_ABI == ABI_V4)
11713         frame_reg_rtx = gen_rtx_REG (Pmode, 11);
11714
11715       emit_move_insn (frame_reg_rtx,
11716                       gen_rtx_MEM (Pmode, sp_reg_rtx));
11717       
11718     }
11719   else if (info->push_p)
11720     {
11721       if (DEFAULT_ABI == ABI_V4)
11722         sp_offset = info->total_size;
11723       else
11724         {
11725           emit_insn (TARGET_32BIT
11726                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
11727                                    GEN_INT (info->total_size))
11728                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
11729                                    GEN_INT (info->total_size)));
11730         }
11731     }
11732   
11733   /* Restore AltiVec registers if needed.  */
11734   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
11735     {
11736       int i;
11737
11738       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
11739         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
11740           {
11741             rtx addr, areg, mem;
11742
11743             areg = gen_rtx_REG (Pmode, 0);
11744             emit_move_insn
11745               (areg, GEN_INT (info->altivec_save_offset
11746                               + sp_offset
11747                               + 16 * (i - info->first_altivec_reg_save)));
11748
11749             /* AltiVec addressing mode is [reg+reg].  */
11750             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
11751             mem = gen_rtx_MEM (V4SImode, addr);
11752             set_mem_alias_set (mem, rs6000_sr_alias_set);
11753
11754             emit_move_insn (gen_rtx_REG (V4SImode, i), mem);
11755           }
11756     }
11757
11758   /* Restore VRSAVE if needed.  */
11759   if (TARGET_ALTIVEC_ABI && info->vrsave_mask != 0)
11760     {
11761       rtx addr, mem, reg;
11762
11763       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
11764                            GEN_INT (info->vrsave_save_offset + sp_offset));
11765       mem = gen_rtx_MEM (SImode, addr);
11766       set_mem_alias_set (mem, rs6000_sr_alias_set);
11767       reg = gen_rtx_REG (SImode, 12);
11768       emit_move_insn (reg, mem);
11769
11770       emit_insn (generate_set_vrsave (reg, info, 1));
11771     }
11772
11773   /* Get the old lr if we saved it.  */
11774   if (info->lr_save_p)
11775     {
11776       rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx,
11777                                       info->lr_save_offset + sp_offset);
11778
11779       set_mem_alias_set (mem, rs6000_sr_alias_set);
11780
11781       emit_move_insn (gen_rtx_REG (Pmode, 0), mem);
11782     }
11783   
11784   /* Get the old cr if we saved it.  */
11785   if (info->cr_save_p)
11786     {
11787       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
11788                                GEN_INT (info->cr_save_offset + sp_offset));
11789       rtx mem = gen_rtx_MEM (SImode, addr);
11790
11791       set_mem_alias_set (mem, rs6000_sr_alias_set);
11792
11793       emit_move_insn (gen_rtx_REG (SImode, 12), mem);
11794     }
11795   
11796   /* Set LR here to try to overlap restores below.  */
11797   if (info->lr_save_p)
11798     emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM),
11799                     gen_rtx_REG (Pmode, 0));
11800   
11801   /* Load exception handler data registers, if needed.  */
11802   if (current_function_calls_eh_return)
11803     {
11804       unsigned int i, regno;
11805
11806       for (i = 0; ; ++i)
11807         {
11808           rtx mem;
11809
11810           regno = EH_RETURN_DATA_REGNO (i);
11811           if (regno == INVALID_REGNUM)
11812             break;
11813
11814           mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
11815                                       info->ehrd_offset + sp_offset
11816                                       + reg_size * (int) i);
11817           set_mem_alias_set (mem, rs6000_sr_alias_set);
11818
11819           emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
11820         }
11821     }
11822   
11823   /* Restore GPRs.  This is done as a PARALLEL if we are using
11824      the load-multiple instructions.  */
11825   if (using_load_multiple)
11826     {
11827       rtvec p;
11828       p = rtvec_alloc (32 - info->first_gp_reg_save);
11829       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
11830         {
11831           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
11832                                    GEN_INT (info->gp_save_offset 
11833                                             + sp_offset 
11834                                             + reg_size * i));
11835           rtx mem = gen_rtx_MEM (reg_mode, addr);
11836
11837           set_mem_alias_set (mem, rs6000_sr_alias_set);
11838
11839           RTVEC_ELT (p, i) = 
11840             gen_rtx_SET (VOIDmode,
11841                          gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
11842                          mem);
11843         }
11844       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
11845     }
11846   else
11847     for (i = 0; i < 32 - info->first_gp_reg_save; i++)
11848       if ((regs_ever_live[info->first_gp_reg_save+i] 
11849            && ! call_used_regs[info->first_gp_reg_save+i])
11850           || (i+info->first_gp_reg_save == RS6000_PIC_OFFSET_TABLE_REGNUM
11851               && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
11852                   || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
11853         {
11854           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
11855                                    GEN_INT (info->gp_save_offset 
11856                                             + sp_offset 
11857                                             + reg_size * i));
11858           rtx mem = gen_rtx_MEM (reg_mode, addr);
11859
11860           /* Restore 64-bit quantities for SPE.  */
11861           if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
11862             {
11863               int offset = info->spe_gp_save_offset + sp_offset + 8 * i;
11864               rtx b;
11865
11866               if (!SPE_CONST_OFFSET_OK (offset))
11867                 {
11868                   b = gen_rtx_REG (Pmode, FIXED_SCRATCH);
11869                   emit_move_insn (b, GEN_INT (offset));
11870                 }
11871               else
11872                 b = GEN_INT (offset);
11873
11874               addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, b);
11875               mem = gen_rtx_MEM (V2SImode, addr);
11876             }
11877
11878           set_mem_alias_set (mem, rs6000_sr_alias_set);
11879
11880           emit_move_insn (gen_rtx_REG (reg_mode, 
11881                                        info->first_gp_reg_save + i), mem);
11882         }
11883
11884   /* Restore fpr's if we need to do it without calling a function.  */
11885   if (restoring_FPRs_inline)
11886     for (i = 0; i < 64 - info->first_fp_reg_save; i++)
11887       if ((regs_ever_live[info->first_fp_reg_save+i] 
11888            && ! call_used_regs[info->first_fp_reg_save+i]))
11889         {
11890           rtx addr, mem;
11891           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
11892                                GEN_INT (info->fp_save_offset 
11893                                         + sp_offset 
11894                                         + 8 * i));
11895           mem = gen_rtx_MEM (DFmode, addr);
11896           set_mem_alias_set (mem, rs6000_sr_alias_set);
11897
11898           emit_move_insn (gen_rtx_REG (DFmode, 
11899                                        info->first_fp_reg_save + i),
11900                           mem);
11901         }
11902
11903   /* If we saved cr, restore it here.  Just those that were used.  */
11904   if (info->cr_save_p)
11905     {
11906       rtx r12_rtx = gen_rtx_REG (SImode, 12);
11907       int count = 0;
11908       
11909       if (using_mfcr_multiple)
11910         {
11911           for (i = 0; i < 8; i++)
11912             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
11913               count++;
11914           if (count == 0)
11915             abort ();
11916         }
11917
11918       if (using_mfcr_multiple && count > 1)
11919         {
11920           rtvec p;
11921           int ndx;
11922           
11923           p = rtvec_alloc (count);
11924
11925           ndx = 0;
11926           for (i = 0; i < 8; i++)
11927             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
11928               {
11929                 rtvec r = rtvec_alloc (2);
11930                 RTVEC_ELT (r, 0) = r12_rtx;
11931                 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
11932                 RTVEC_ELT (p, ndx) =
11933                   gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO+i), 
11934                                gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
11935                 ndx++;
11936               }
11937           emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
11938           if (ndx != count)
11939             abort ();
11940         }
11941       else
11942         for (i = 0; i < 8; i++)
11943           if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
11944             {
11945               emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode, 
11946                                                            CR0_REGNO+i),
11947                                               r12_rtx));
11948             }
11949     }
11950
11951   /* If this is V.4, unwind the stack pointer after all of the loads
11952      have been done.  We need to emit a block here so that sched
11953      doesn't decide to move the sp change before the register restores
11954      (which may not have any obvious dependency on the stack).  This
11955      doesn't hurt performance, because there is no scheduling that can
11956      be done after this point.  */
11957   if (DEFAULT_ABI == ABI_V4)
11958     {
11959       if (frame_reg_rtx != sp_reg_rtx)
11960           rs6000_emit_stack_tie ();
11961
11962       if (use_backchain_to_restore_sp)
11963         {
11964           emit_move_insn (sp_reg_rtx, frame_reg_rtx);
11965         }
11966       else if (sp_offset != 0)
11967         {
11968           emit_insn (TARGET_32BIT
11969                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
11970                                    GEN_INT (sp_offset))
11971                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
11972                                    GEN_INT (sp_offset)));
11973         }
11974     }
11975
11976   if (current_function_calls_eh_return)
11977     {
11978       rtx sa = EH_RETURN_STACKADJ_RTX;
11979       emit_insn (TARGET_32BIT
11980                  ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx, sa)
11981                  : gen_adddi3 (sp_reg_rtx, sp_reg_rtx, sa));
11982     }
11983
11984   if (!sibcall)
11985     {
11986       rtvec p;
11987       if (! restoring_FPRs_inline)
11988         p = rtvec_alloc (3 + 64 - info->first_fp_reg_save);
11989       else
11990         p = rtvec_alloc (2);
11991
11992       RTVEC_ELT (p, 0) = gen_rtx_RETURN (VOIDmode);
11993       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode, 
11994                                       gen_rtx_REG (Pmode, 
11995                                                    LINK_REGISTER_REGNUM));
11996
11997       /* If we have to restore more than two FP registers, branch to the
11998          restore function.  It will return to our caller.  */
11999       if (! restoring_FPRs_inline)
12000         {
12001           int i;
12002           char rname[30];
12003           const char *alloc_rname;
12004
12005           sprintf (rname, "%s%d%s", RESTORE_FP_PREFIX, 
12006                    info->first_fp_reg_save - 32, RESTORE_FP_SUFFIX);
12007           alloc_rname = ggc_strdup (rname);
12008           RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode,
12009                                           gen_rtx_SYMBOL_REF (Pmode,
12010                                                               alloc_rname));
12011
12012           for (i = 0; i < 64 - info->first_fp_reg_save; i++)
12013             {
12014               rtx addr, mem;
12015               addr = gen_rtx_PLUS (Pmode, sp_reg_rtx,
12016                                    GEN_INT (info->fp_save_offset + 8*i));
12017               mem = gen_rtx_MEM (DFmode, addr);
12018               set_mem_alias_set (mem, rs6000_sr_alias_set);
12019
12020               RTVEC_ELT (p, i+3) = 
12021                 gen_rtx_SET (VOIDmode,
12022                              gen_rtx_REG (DFmode, info->first_fp_reg_save + i),
12023                              mem);
12024             }
12025         }
12026       
12027       emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
12028     }
12029 }
12030
12031 /* Write function epilogue.  */
12032
12033 static void
12034 rs6000_output_function_epilogue (file, size)
12035      FILE *file;
12036      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
12037 {
12038   rs6000_stack_t *info = rs6000_stack_info ();
12039
12040   if (! HAVE_epilogue)
12041     {
12042       rtx insn = get_last_insn ();
12043       /* If the last insn was a BARRIER, we don't have to write anything except
12044          the trace table.  */
12045       if (GET_CODE (insn) == NOTE)
12046         insn = prev_nonnote_insn (insn);
12047       if (insn == 0 ||  GET_CODE (insn) != BARRIER)
12048         {
12049           /* This is slightly ugly, but at least we don't have two
12050              copies of the epilogue-emitting code.  */
12051           start_sequence ();
12052
12053           /* A NOTE_INSN_DELETED is supposed to be at the start
12054              and end of the "toplevel" insn chain.  */
12055           emit_note (NOTE_INSN_DELETED);
12056           rs6000_emit_epilogue (FALSE);
12057           emit_note (NOTE_INSN_DELETED);
12058
12059           /* Expand INSN_ADDRESSES so final() doesn't crash. */
12060           {
12061             rtx insn;
12062             unsigned addr = 0;
12063             for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
12064               {
12065                 INSN_ADDRESSES_NEW (insn, addr);
12066                 addr += 4;
12067               }
12068           }
12069
12070           if (TARGET_DEBUG_STACK)
12071             debug_rtx_list (get_insns (), 100);
12072           final (get_insns (), file, FALSE, FALSE);
12073           end_sequence ();
12074         }
12075     }
12076
12077   /* Output a traceback table here.  See /usr/include/sys/debug.h for info
12078      on its format.
12079
12080      We don't output a traceback table if -finhibit-size-directive was
12081      used.  The documentation for -finhibit-size-directive reads
12082      ``don't output a @code{.size} assembler directive, or anything
12083      else that would cause trouble if the function is split in the
12084      middle, and the two halves are placed at locations far apart in
12085      memory.''  The traceback table has this property, since it
12086      includes the offset from the start of the function to the
12087      traceback table itself.
12088
12089      System V.4 Powerpc's (and the embedded ABI derived from it) use a
12090      different traceback table.  */
12091   if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive
12092       && rs6000_traceback != traceback_none)
12093     {
12094       const char *fname = NULL;
12095       const char *language_string = lang_hooks.name;
12096       int fixed_parms = 0, float_parms = 0, parm_info = 0;
12097       int i;
12098       int optional_tbtab;
12099
12100       if (rs6000_traceback == traceback_full)
12101         optional_tbtab = 1;
12102       else if (rs6000_traceback == traceback_part)
12103         optional_tbtab = 0;
12104       else
12105         optional_tbtab = !optimize_size && !TARGET_ELF;
12106
12107       if (optional_tbtab)
12108         {
12109           fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
12110           while (*fname == '.') /* V.4 encodes . in the name */
12111             fname++;
12112
12113           /* Need label immediately before tbtab, so we can compute
12114              its offset from the function start.  */
12115           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
12116           ASM_OUTPUT_LABEL (file, fname);
12117         }
12118
12119       /* The .tbtab pseudo-op can only be used for the first eight
12120          expressions, since it can't handle the possibly variable
12121          length fields that follow.  However, if you omit the optional
12122          fields, the assembler outputs zeros for all optional fields
12123          anyways, giving each variable length field is minimum length
12124          (as defined in sys/debug.h).  Thus we can not use the .tbtab
12125          pseudo-op at all.  */
12126
12127       /* An all-zero word flags the start of the tbtab, for debuggers
12128          that have to find it by searching forward from the entry
12129          point or from the current pc.  */
12130       fputs ("\t.long 0\n", file);
12131
12132       /* Tbtab format type.  Use format type 0.  */
12133       fputs ("\t.byte 0,", file);
12134
12135       /* Language type.  Unfortunately, there doesn't seem to be any
12136          official way to get this info, so we use language_string.  C
12137          is 0.  C++ is 9.  No number defined for Obj-C, so use the
12138          value for C for now.  There is no official value for Java,
12139          although IBM appears to be using 13.  There is no official value
12140          for Chill, so we've chosen 44 pseudo-randomly.  */
12141       if (! strcmp (language_string, "GNU C")
12142           || ! strcmp (language_string, "GNU Objective-C"))
12143         i = 0;
12144       else if (! strcmp (language_string, "GNU F77"))
12145         i = 1;
12146       else if (! strcmp (language_string, "GNU Ada"))
12147         i = 3;
12148       else if (! strcmp (language_string, "GNU Pascal"))
12149         i = 2;
12150       else if (! strcmp (language_string, "GNU C++"))
12151         i = 9;
12152       else if (! strcmp (language_string, "GNU Java"))
12153         i = 13;
12154       else if (! strcmp (language_string, "GNU CHILL"))
12155         i = 44;
12156       else
12157         abort ();
12158       fprintf (file, "%d,", i);
12159
12160       /* 8 single bit fields: global linkage (not set for C extern linkage,
12161          apparently a PL/I convention?), out-of-line epilogue/prologue, offset
12162          from start of procedure stored in tbtab, internal function, function
12163          has controlled storage, function has no toc, function uses fp,
12164          function logs/aborts fp operations.  */
12165       /* Assume that fp operations are used if any fp reg must be saved.  */
12166       fprintf (file, "%d,",
12167                (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
12168
12169       /* 6 bitfields: function is interrupt handler, name present in
12170          proc table, function calls alloca, on condition directives
12171          (controls stack walks, 3 bits), saves condition reg, saves
12172          link reg.  */
12173       /* The `function calls alloca' bit seems to be set whenever reg 31 is
12174          set up as a frame pointer, even when there is no alloca call.  */
12175       fprintf (file, "%d,",
12176                ((optional_tbtab << 6)
12177                 | ((optional_tbtab & frame_pointer_needed) << 5)
12178                 | (info->cr_save_p << 1)
12179                 | (info->lr_save_p)));
12180
12181       /* 3 bitfields: saves backchain, fixup code, number of fpr saved
12182          (6 bits).  */
12183       fprintf (file, "%d,",
12184                (info->push_p << 7) | (64 - info->first_fp_reg_save));
12185
12186       /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits).  */
12187       fprintf (file, "%d,", (32 - first_reg_to_save ()));
12188
12189       if (optional_tbtab)
12190         {
12191           /* Compute the parameter info from the function decl argument
12192              list.  */
12193           tree decl;
12194           int next_parm_info_bit = 31;
12195
12196           for (decl = DECL_ARGUMENTS (current_function_decl);
12197                decl; decl = TREE_CHAIN (decl))
12198             {
12199               rtx parameter = DECL_INCOMING_RTL (decl);
12200               enum machine_mode mode = GET_MODE (parameter);
12201
12202               if (GET_CODE (parameter) == REG)
12203                 {
12204                   if (GET_MODE_CLASS (mode) == MODE_FLOAT)
12205                     {
12206                       int bits;
12207
12208                       float_parms++;
12209
12210                       if (mode == SFmode)
12211                         bits = 0x2;
12212                       else if (mode == DFmode || mode == TFmode)
12213                         bits = 0x3;
12214                       else
12215                         abort ();
12216
12217                       /* If only one bit will fit, don't or in this entry.  */
12218                       if (next_parm_info_bit > 0)
12219                         parm_info |= (bits << (next_parm_info_bit - 1));
12220                       next_parm_info_bit -= 2;
12221                     }
12222                   else
12223                     {
12224                       fixed_parms += ((GET_MODE_SIZE (mode)
12225                                        + (UNITS_PER_WORD - 1))
12226                                       / UNITS_PER_WORD);
12227                       next_parm_info_bit -= 1;
12228                     }
12229                 }
12230             }
12231         }
12232
12233       /* Number of fixed point parameters.  */
12234       /* This is actually the number of words of fixed point parameters; thus
12235          an 8 byte struct counts as 2; and thus the maximum value is 8.  */
12236       fprintf (file, "%d,", fixed_parms);
12237
12238       /* 2 bitfields: number of floating point parameters (7 bits), parameters
12239          all on stack.  */
12240       /* This is actually the number of fp registers that hold parameters;
12241          and thus the maximum value is 13.  */
12242       /* Set parameters on stack bit if parameters are not in their original
12243          registers, regardless of whether they are on the stack?  Xlc
12244          seems to set the bit when not optimizing.  */
12245       fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
12246
12247       if (! optional_tbtab)
12248         return;
12249
12250       /* Optional fields follow.  Some are variable length.  */
12251
12252       /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
12253          11 double float.  */
12254       /* There is an entry for each parameter in a register, in the order that
12255          they occur in the parameter list.  Any intervening arguments on the
12256          stack are ignored.  If the list overflows a long (max possible length
12257          34 bits) then completely leave off all elements that don't fit.  */
12258       /* Only emit this long if there was at least one parameter.  */
12259       if (fixed_parms || float_parms)
12260         fprintf (file, "\t.long %d\n", parm_info);
12261
12262       /* Offset from start of code to tb table.  */
12263       fputs ("\t.long ", file);
12264       ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
12265 #if TARGET_AIX
12266       RS6000_OUTPUT_BASENAME (file, fname);
12267 #else
12268       assemble_name (file, fname);
12269 #endif
12270       fputs ("-.", file);
12271 #if TARGET_AIX
12272       RS6000_OUTPUT_BASENAME (file, fname);
12273 #else
12274       assemble_name (file, fname);
12275 #endif
12276       putc ('\n', file);
12277
12278       /* Interrupt handler mask.  */
12279       /* Omit this long, since we never set the interrupt handler bit
12280          above.  */
12281
12282       /* Number of CTL (controlled storage) anchors.  */
12283       /* Omit this long, since the has_ctl bit is never set above.  */
12284
12285       /* Displacement into stack of each CTL anchor.  */
12286       /* Omit this list of longs, because there are no CTL anchors.  */
12287
12288       /* Length of function name.  */
12289       if (*fname == '*')
12290         ++fname;
12291       fprintf (file, "\t.short %d\n", (int) strlen (fname));
12292
12293       /* Function name.  */
12294       assemble_string (fname, strlen (fname));
12295
12296       /* Register for alloca automatic storage; this is always reg 31.
12297          Only emit this if the alloca bit was set above.  */
12298       if (frame_pointer_needed)
12299         fputs ("\t.byte 31\n", file);
12300
12301       fputs ("\t.align 2\n", file);
12302     }
12303 }
12304 \f
12305 /* A C compound statement that outputs the assembler code for a thunk
12306    function, used to implement C++ virtual function calls with
12307    multiple inheritance.  The thunk acts as a wrapper around a virtual
12308    function, adjusting the implicit object parameter before handing
12309    control off to the real function.
12310
12311    First, emit code to add the integer DELTA to the location that
12312    contains the incoming first argument.  Assume that this argument
12313    contains a pointer, and is the one used to pass the `this' pointer
12314    in C++.  This is the incoming argument *before* the function
12315    prologue, e.g. `%o0' on a sparc.  The addition must preserve the
12316    values of all other incoming arguments.
12317
12318    After the addition, emit code to jump to FUNCTION, which is a
12319    `FUNCTION_DECL'.  This is a direct pure jump, not a call, and does
12320    not touch the return address.  Hence returning from FUNCTION will
12321    return to whoever called the current `thunk'.
12322
12323    The effect must be as if FUNCTION had been called directly with the
12324    adjusted first argument.  This macro is responsible for emitting
12325    all of the code for a thunk function; output_function_prologue()
12326    and output_function_epilogue() are not invoked.
12327
12328    The THUNK_FNDECL is redundant.  (DELTA and FUNCTION have already
12329    been extracted from it.)  It might possibly be useful on some
12330    targets, but probably not.
12331
12332    If you do not define this macro, the target-independent code in the
12333    C++ frontend will generate a less efficient heavyweight thunk that
12334    calls FUNCTION instead of jumping to it.  The generic approach does
12335    not support varargs.  */
12336
12337 static void
12338 rs6000_output_mi_thunk (file, thunk_fndecl, delta, vcall_offset, function)
12339      FILE *file;
12340      tree thunk_fndecl ATTRIBUTE_UNUSED;
12341      HOST_WIDE_INT delta;
12342      HOST_WIDE_INT vcall_offset;
12343      tree function;
12344 {
12345   rtx this, insn, funexp;
12346
12347   reload_completed = 1;
12348   epilogue_completed = 1;
12349   no_new_pseudos = 1;
12350
12351   /* Mark the end of the (empty) prologue.  */
12352   emit_note (NOTE_INSN_PROLOGUE_END);
12353
12354   /* Find the "this" pointer.  If the function returns a structure,
12355      the structure return pointer is in r3.  */
12356   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function))))
12357     this = gen_rtx_REG (Pmode, 4);
12358   else
12359     this = gen_rtx_REG (Pmode, 3);
12360
12361   /* Apply the constant offset, if required.  */
12362   if (delta)
12363     {
12364       rtx delta_rtx = GEN_INT (delta);
12365       emit_insn (TARGET_32BIT
12366                  ? gen_addsi3 (this, this, delta_rtx)
12367                  : gen_adddi3 (this, this, delta_rtx));
12368     }
12369
12370   /* Apply the offset from the vtable, if required.  */
12371   if (vcall_offset)
12372     {
12373       rtx vcall_offset_rtx = GEN_INT (vcall_offset);
12374       rtx tmp = gen_rtx_REG (Pmode, 12);
12375
12376       emit_move_insn (tmp, gen_rtx_MEM (Pmode, this));
12377       emit_insn (TARGET_32BIT
12378                  ? gen_addsi3 (tmp, tmp, vcall_offset_rtx)
12379                  : gen_adddi3 (tmp, tmp, vcall_offset_rtx));
12380       emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
12381       emit_insn (TARGET_32BIT
12382                  ? gen_addsi3 (this, this, tmp)
12383                  : gen_adddi3 (this, this, tmp));
12384     }
12385
12386   /* Generate a tail call to the target function.  */
12387   if (!TREE_USED (function))
12388     {
12389       assemble_external (function);
12390       TREE_USED (function) = 1;
12391     }
12392   funexp = XEXP (DECL_RTL (function), 0);
12393   SYMBOL_REF_FLAGS (funexp) &= ~SYMBOL_FLAG_LOCAL;
12394   funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
12395
12396 #if TARGET_MACHO
12397   if (MACHOPIC_INDIRECT)
12398     funexp = machopic_indirect_call_target (funexp);
12399 #endif
12400
12401   /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
12402      generate sibcall RTL explicitly to avoid constraint abort.  */
12403   insn = emit_call_insn (
12404            gen_rtx_PARALLEL (VOIDmode,
12405              gen_rtvec (4,
12406                         gen_rtx_CALL (VOIDmode,
12407                                       funexp, const0_rtx),
12408                         gen_rtx_USE (VOIDmode, const0_rtx),
12409                         gen_rtx_USE (VOIDmode,
12410                                      gen_rtx_REG (SImode,
12411                                                   LINK_REGISTER_REGNUM)),
12412                         gen_rtx_RETURN (VOIDmode))));
12413   SIBLING_CALL_P (insn) = 1;
12414   emit_barrier ();
12415
12416   /* Run just enough of rest_of_compilation to get the insns emitted.
12417      There's not really enough bulk here to make other passes such as
12418      instruction scheduling worth while.  Note that use_thunk calls
12419      assemble_start_function and assemble_end_function.  */
12420   insn = get_insns ();
12421   insn_locators_initialize ();
12422   shorten_branches (insn);
12423   final_start_function (insn, file, 1);
12424   final (insn, file, 1, 0);
12425   final_end_function ();
12426
12427   reload_completed = 0;
12428   epilogue_completed = 0;
12429   no_new_pseudos = 0;
12430 }
12431 \f
12432 /* A quick summary of the various types of 'constant-pool tables'
12433    under PowerPC:
12434
12435    Target       Flags           Name            One table per   
12436    AIX          (none)          AIX TOC         object file
12437    AIX          -mfull-toc      AIX TOC         object file
12438    AIX          -mminimal-toc   AIX minimal TOC translation unit
12439    SVR4/EABI    (none)          SVR4 SDATA      object file
12440    SVR4/EABI    -fpic           SVR4 pic        object file
12441    SVR4/EABI    -fPIC           SVR4 PIC        translation unit
12442    SVR4/EABI    -mrelocatable   EABI TOC        function
12443    SVR4/EABI    -maix           AIX TOC         object file
12444    SVR4/EABI    -maix -mminimal-toc 
12445                                 AIX minimal TOC translation unit
12446
12447    Name                 Reg.    Set by  entries       contains:
12448                                         made by  addrs? fp?     sum?
12449
12450    AIX TOC              2       crt0    as       Y      option  option
12451    AIX minimal TOC      30      prolog  gcc      Y      Y       option
12452    SVR4 SDATA           13      crt0    gcc      N      Y       N
12453    SVR4 pic             30      prolog  ld       Y      not yet N
12454    SVR4 PIC             30      prolog  gcc      Y      option  option
12455    EABI TOC             30      prolog  gcc      Y      option  option
12456
12457 */
12458
12459 /* Hash functions for the hash table.  */
12460
12461 static unsigned
12462 rs6000_hash_constant (k)
12463      rtx k;
12464 {
12465   enum rtx_code code = GET_CODE (k);
12466   enum machine_mode mode = GET_MODE (k);
12467   unsigned result = (code << 3) ^ mode;
12468   const char *format;
12469   int flen, fidx;
12470   
12471   format = GET_RTX_FORMAT (code);
12472   flen = strlen (format);
12473   fidx = 0;
12474
12475   switch (code)
12476     {
12477     case LABEL_REF:
12478       return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
12479
12480     case CONST_DOUBLE:
12481       if (mode != VOIDmode)
12482         return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
12483       flen = 2;
12484       break;
12485
12486     case CODE_LABEL:
12487       fidx = 3;
12488       break;
12489
12490     default:
12491       break;
12492     }
12493
12494   for (; fidx < flen; fidx++)
12495     switch (format[fidx])
12496       {
12497       case 's':
12498         {
12499           unsigned i, len;
12500           const char *str = XSTR (k, fidx);
12501           len = strlen (str);
12502           result = result * 613 + len;
12503           for (i = 0; i < len; i++)
12504             result = result * 613 + (unsigned) str[i];
12505           break;
12506         }
12507       case 'u':
12508       case 'e':
12509         result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
12510         break;
12511       case 'i':
12512       case 'n':
12513         result = result * 613 + (unsigned) XINT (k, fidx);
12514         break;
12515       case 'w':
12516         if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
12517           result = result * 613 + (unsigned) XWINT (k, fidx);
12518         else
12519           {
12520             size_t i;
12521             for (i = 0; i < sizeof(HOST_WIDE_INT)/sizeof(unsigned); i++)
12522               result = result * 613 + (unsigned) (XWINT (k, fidx)
12523                                                   >> CHAR_BIT * i);
12524           }
12525         break;
12526       case '0':
12527         break;
12528       default:
12529         abort ();
12530       }
12531
12532   return result;
12533 }
12534
12535 static unsigned
12536 toc_hash_function (hash_entry)
12537      const void * hash_entry;
12538 {
12539   const struct toc_hash_struct *thc = 
12540     (const struct toc_hash_struct *) hash_entry;
12541   return rs6000_hash_constant (thc->key) ^ thc->key_mode;
12542 }
12543
12544 /* Compare H1 and H2 for equivalence.  */
12545
12546 static int
12547 toc_hash_eq (h1, h2)
12548      const void * h1;
12549      const void * h2;
12550 {
12551   rtx r1 = ((const struct toc_hash_struct *) h1)->key;
12552   rtx r2 = ((const struct toc_hash_struct *) h2)->key;
12553
12554   if (((const struct toc_hash_struct *) h1)->key_mode
12555       != ((const struct toc_hash_struct *) h2)->key_mode)
12556     return 0;
12557
12558   return rtx_equal_p (r1, r2);
12559 }
12560
12561 /* These are the names given by the C++ front-end to vtables, and
12562    vtable-like objects.  Ideally, this logic should not be here;
12563    instead, there should be some programmatic way of inquiring as
12564    to whether or not an object is a vtable.  */
12565
12566 #define VTABLE_NAME_P(NAME)                             \
12567   (strncmp ("_vt.", name, strlen("_vt.")) == 0          \
12568   || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0       \
12569   || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0       \
12570   || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0) 
12571
12572 void
12573 rs6000_output_symbol_ref (file, x)
12574      FILE *file;
12575      rtx x;
12576 {
12577   /* Currently C++ toc references to vtables can be emitted before it
12578      is decided whether the vtable is public or private.  If this is
12579      the case, then the linker will eventually complain that there is
12580      a reference to an unknown section.  Thus, for vtables only, 
12581      we emit the TOC reference to reference the symbol and not the
12582      section.  */
12583   const char *name = XSTR (x, 0);
12584
12585   if (VTABLE_NAME_P (name)) 
12586     {
12587       RS6000_OUTPUT_BASENAME (file, name);
12588     }
12589   else
12590     assemble_name (file, name);
12591 }
12592
12593 /* Output a TOC entry.  We derive the entry name from what is being
12594    written.  */
12595
12596 void
12597 output_toc (file, x, labelno, mode)
12598      FILE *file;
12599      rtx x;
12600      int labelno;
12601      enum machine_mode mode;
12602 {
12603   char buf[256];
12604   const char *name = buf;
12605   const char *real_name;
12606   rtx base = x;
12607   int offset = 0;
12608
12609   if (TARGET_NO_TOC)
12610     abort ();
12611
12612   /* When the linker won't eliminate them, don't output duplicate
12613      TOC entries (this happens on AIX if there is any kind of TOC,
12614      and on SVR4 under -fPIC or -mrelocatable).  Don't do this for
12615      CODE_LABELs.  */
12616   if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
12617     {
12618       struct toc_hash_struct *h;
12619       void * * found;
12620       
12621       /* Create toc_hash_table.  This can't be done at OVERRIDE_OPTIONS
12622          time because GGC is not initialised at that point.  */
12623       if (toc_hash_table == NULL)
12624         toc_hash_table = htab_create_ggc (1021, toc_hash_function, 
12625                                           toc_hash_eq, NULL);
12626
12627       h = ggc_alloc (sizeof (*h));
12628       h->key = x;
12629       h->key_mode = mode;
12630       h->labelno = labelno;
12631       
12632       found = htab_find_slot (toc_hash_table, h, 1);
12633       if (*found == NULL)
12634         *found = h;
12635       else  /* This is indeed a duplicate.  
12636                Set this label equal to that label.  */
12637         {
12638           fputs ("\t.set ", file);
12639           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
12640           fprintf (file, "%d,", labelno);
12641           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
12642           fprintf (file, "%d\n", ((*(const struct toc_hash_struct **) 
12643                                               found)->labelno));
12644           return;
12645         }
12646     }
12647
12648   /* If we're going to put a double constant in the TOC, make sure it's
12649      aligned properly when strict alignment is on.  */
12650   if (GET_CODE (x) == CONST_DOUBLE
12651       && STRICT_ALIGNMENT
12652       && GET_MODE_BITSIZE (mode) >= 64
12653       && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
12654     ASM_OUTPUT_ALIGN (file, 3);
12655   }
12656
12657   (*targetm.asm_out.internal_label) (file, "LC", labelno);
12658
12659   /* Handle FP constants specially.  Note that if we have a minimal
12660      TOC, things we put here aren't actually in the TOC, so we can allow
12661      FP constants.  */
12662   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == TFmode)
12663     {
12664       REAL_VALUE_TYPE rv;
12665       long k[4];
12666
12667       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
12668       REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
12669
12670       if (TARGET_64BIT)
12671         {
12672           if (TARGET_MINIMAL_TOC)
12673             fputs (DOUBLE_INT_ASM_OP, file);
12674           else
12675             fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
12676                      k[0] & 0xffffffff, k[1] & 0xffffffff,
12677                      k[2] & 0xffffffff, k[3] & 0xffffffff);
12678           fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
12679                    k[0] & 0xffffffff, k[1] & 0xffffffff,
12680                    k[2] & 0xffffffff, k[3] & 0xffffffff);
12681           return;
12682         }
12683       else
12684         {
12685           if (TARGET_MINIMAL_TOC)
12686             fputs ("\t.long ", file);
12687           else
12688             fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
12689                      k[0] & 0xffffffff, k[1] & 0xffffffff,
12690                      k[2] & 0xffffffff, k[3] & 0xffffffff);
12691           fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
12692                    k[0] & 0xffffffff, k[1] & 0xffffffff,
12693                    k[2] & 0xffffffff, k[3] & 0xffffffff);
12694           return;
12695         }
12696     }
12697   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
12698     {
12699       REAL_VALUE_TYPE rv;
12700       long k[2];
12701
12702       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
12703       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
12704
12705       if (TARGET_64BIT)
12706         {
12707           if (TARGET_MINIMAL_TOC)
12708             fputs (DOUBLE_INT_ASM_OP, file);
12709           else
12710             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
12711                      k[0] & 0xffffffff, k[1] & 0xffffffff);
12712           fprintf (file, "0x%lx%08lx\n",
12713                    k[0] & 0xffffffff, k[1] & 0xffffffff);
12714           return;
12715         }
12716       else
12717         {
12718           if (TARGET_MINIMAL_TOC)
12719             fputs ("\t.long ", file);
12720           else
12721             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
12722                      k[0] & 0xffffffff, k[1] & 0xffffffff);
12723           fprintf (file, "0x%lx,0x%lx\n",
12724                    k[0] & 0xffffffff, k[1] & 0xffffffff);
12725           return;
12726         }
12727     }
12728   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
12729     {
12730       REAL_VALUE_TYPE rv;
12731       long l;
12732
12733       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
12734       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
12735
12736       if (TARGET_64BIT)
12737         {
12738           if (TARGET_MINIMAL_TOC)
12739             fputs (DOUBLE_INT_ASM_OP, file);
12740           else
12741             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
12742           fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
12743           return;
12744         }
12745       else
12746         {
12747           if (TARGET_MINIMAL_TOC)
12748             fputs ("\t.long ", file);
12749           else
12750             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
12751           fprintf (file, "0x%lx\n", l & 0xffffffff);
12752           return;
12753         }
12754     }
12755   else if (GET_MODE (x) == VOIDmode
12756            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
12757     {
12758       unsigned HOST_WIDE_INT low;
12759       HOST_WIDE_INT high;
12760
12761       if (GET_CODE (x) == CONST_DOUBLE)
12762         {
12763           low = CONST_DOUBLE_LOW (x);
12764           high = CONST_DOUBLE_HIGH (x);
12765         }
12766       else
12767 #if HOST_BITS_PER_WIDE_INT == 32
12768         {
12769           low = INTVAL (x);
12770           high = (low & 0x80000000) ? ~0 : 0;
12771         }
12772 #else
12773         {
12774           low = INTVAL (x) & 0xffffffff;
12775           high = (HOST_WIDE_INT) INTVAL (x) >> 32;
12776         }
12777 #endif
12778
12779       /* TOC entries are always Pmode-sized, but since this
12780          is a bigendian machine then if we're putting smaller
12781          integer constants in the TOC we have to pad them.
12782          (This is still a win over putting the constants in
12783          a separate constant pool, because then we'd have
12784          to have both a TOC entry _and_ the actual constant.)
12785
12786          For a 32-bit target, CONST_INT values are loaded and shifted
12787          entirely within `low' and can be stored in one TOC entry.  */
12788
12789       if (TARGET_64BIT && POINTER_SIZE < GET_MODE_BITSIZE (mode))
12790         abort ();/* It would be easy to make this work, but it doesn't now.  */
12791
12792       if (POINTER_SIZE > GET_MODE_BITSIZE (mode))
12793         {
12794 #if HOST_BITS_PER_WIDE_INT == 32
12795           lshift_double (low, high, POINTER_SIZE - GET_MODE_BITSIZE (mode),
12796                          POINTER_SIZE, &low, &high, 0);
12797 #else
12798           low |= high << 32;
12799           low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
12800           high = (HOST_WIDE_INT) low >> 32;
12801           low &= 0xffffffff;
12802 #endif
12803         }
12804
12805       if (TARGET_64BIT)
12806         {
12807           if (TARGET_MINIMAL_TOC)
12808             fputs (DOUBLE_INT_ASM_OP, file);
12809           else
12810             fprintf (file, "\t.tc ID_%lx_%lx[TC],",
12811                      (long) high & 0xffffffff, (long) low & 0xffffffff);
12812           fprintf (file, "0x%lx%08lx\n",
12813                    (long) high & 0xffffffff, (long) low & 0xffffffff);
12814           return;
12815         }
12816       else
12817         {
12818           if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
12819             {
12820               if (TARGET_MINIMAL_TOC)
12821                 fputs ("\t.long ", file);
12822               else
12823                 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
12824                          (long) high & 0xffffffff, (long) low & 0xffffffff);
12825               fprintf (file, "0x%lx,0x%lx\n",
12826                        (long) high & 0xffffffff, (long) low & 0xffffffff);
12827             }
12828           else
12829             {
12830               if (TARGET_MINIMAL_TOC)
12831                 fputs ("\t.long ", file);
12832               else
12833                 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
12834               fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
12835             }
12836           return;
12837         }
12838     }
12839
12840   if (GET_CODE (x) == CONST)
12841     {
12842       if (GET_CODE (XEXP (x, 0)) != PLUS)
12843         abort ();
12844
12845       base = XEXP (XEXP (x, 0), 0);
12846       offset = INTVAL (XEXP (XEXP (x, 0), 1));
12847     }
12848   
12849   if (GET_CODE (base) == SYMBOL_REF)
12850     name = XSTR (base, 0);
12851   else if (GET_CODE (base) == LABEL_REF)
12852     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (XEXP (base, 0)));
12853   else if (GET_CODE (base) == CODE_LABEL)
12854     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
12855   else
12856     abort ();
12857
12858   real_name = (*targetm.strip_name_encoding) (name);
12859   if (TARGET_MINIMAL_TOC)
12860     fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
12861   else
12862     {
12863       fprintf (file, "\t.tc %s", real_name);
12864
12865       if (offset < 0)
12866         fprintf (file, ".N%d", - offset);
12867       else if (offset)
12868         fprintf (file, ".P%d", offset);
12869
12870       fputs ("[TC],", file);
12871     }
12872
12873   /* Currently C++ toc references to vtables can be emitted before it
12874      is decided whether the vtable is public or private.  If this is
12875      the case, then the linker will eventually complain that there is
12876      a TOC reference to an unknown section.  Thus, for vtables only,
12877      we emit the TOC reference to reference the symbol and not the
12878      section.  */
12879   if (VTABLE_NAME_P (name))
12880     {
12881       RS6000_OUTPUT_BASENAME (file, name);
12882       if (offset < 0)
12883         fprintf (file, "%d", offset);
12884       else if (offset > 0)
12885         fprintf (file, "+%d", offset);
12886     }
12887   else
12888     output_addr_const (file, x);
12889   putc ('\n', file);
12890 }
12891 \f
12892 /* Output an assembler pseudo-op to write an ASCII string of N characters
12893    starting at P to FILE.
12894
12895    On the RS/6000, we have to do this using the .byte operation and
12896    write out special characters outside the quoted string.
12897    Also, the assembler is broken; very long strings are truncated,
12898    so we must artificially break them up early.  */
12899
12900 void
12901 output_ascii (file, p, n)
12902      FILE *file;
12903      const char *p;
12904      int n;
12905 {
12906   char c;
12907   int i, count_string;
12908   const char *for_string = "\t.byte \"";
12909   const char *for_decimal = "\t.byte ";
12910   const char *to_close = NULL;
12911
12912   count_string = 0;
12913   for (i = 0; i < n; i++)
12914     {
12915       c = *p++;
12916       if (c >= ' ' && c < 0177)
12917         {
12918           if (for_string)
12919             fputs (for_string, file);
12920           putc (c, file);
12921
12922           /* Write two quotes to get one.  */
12923           if (c == '"')
12924             {
12925               putc (c, file);
12926               ++count_string;
12927             }
12928
12929           for_string = NULL;
12930           for_decimal = "\"\n\t.byte ";
12931           to_close = "\"\n";
12932           ++count_string;
12933
12934           if (count_string >= 512)
12935             {
12936               fputs (to_close, file);
12937
12938               for_string = "\t.byte \"";
12939               for_decimal = "\t.byte ";
12940               to_close = NULL;
12941               count_string = 0;
12942             }
12943         }
12944       else
12945         {
12946           if (for_decimal)
12947             fputs (for_decimal, file);
12948           fprintf (file, "%d", c);
12949
12950           for_string = "\n\t.byte \"";
12951           for_decimal = ", ";
12952           to_close = "\n";
12953           count_string = 0;
12954         }
12955     }
12956
12957   /* Now close the string if we have written one.  Then end the line.  */
12958   if (to_close)
12959     fputs (to_close, file);
12960 }
12961 \f
12962 /* Generate a unique section name for FILENAME for a section type
12963    represented by SECTION_DESC.  Output goes into BUF.
12964
12965    SECTION_DESC can be any string, as long as it is different for each
12966    possible section type.
12967
12968    We name the section in the same manner as xlc.  The name begins with an
12969    underscore followed by the filename (after stripping any leading directory
12970    names) with the last period replaced by the string SECTION_DESC.  If
12971    FILENAME does not contain a period, SECTION_DESC is appended to the end of
12972    the name.  */
12973
12974 void
12975 rs6000_gen_section_name (buf, filename, section_desc)
12976      char **buf;
12977      const char *filename;
12978      const char *section_desc;
12979 {
12980   const char *q, *after_last_slash, *last_period = 0;
12981   char *p;
12982   int len;
12983
12984   after_last_slash = filename;
12985   for (q = filename; *q; q++)
12986     {
12987       if (*q == '/')
12988         after_last_slash = q + 1;
12989       else if (*q == '.')
12990         last_period = q;
12991     }
12992
12993   len = strlen (after_last_slash) + strlen (section_desc) + 2;
12994   *buf = (char *) xmalloc (len);
12995
12996   p = *buf;
12997   *p++ = '_';
12998
12999   for (q = after_last_slash; *q; q++)
13000     {
13001       if (q == last_period)
13002         {
13003           strcpy (p, section_desc);
13004           p += strlen (section_desc);
13005           break;
13006         }
13007
13008       else if (ISALNUM (*q))
13009         *p++ = *q;
13010     }
13011
13012   if (last_period == 0)
13013     strcpy (p, section_desc);
13014   else
13015     *p = '\0';
13016 }
13017 \f
13018 /* Emit profile function.  */
13019
13020 void
13021 output_profile_hook (labelno)
13022      int labelno ATTRIBUTE_UNUSED;
13023 {
13024   if (TARGET_PROFILE_KERNEL)
13025     return;
13026
13027   if (DEFAULT_ABI == ABI_AIX)
13028     {
13029 #ifndef NO_PROFILE_COUNTERS
13030 # define NO_PROFILE_COUNTERS 0
13031 #endif
13032       if (NO_PROFILE_COUNTERS)  
13033         emit_library_call (init_one_libfunc (RS6000_MCOUNT), 0, VOIDmode, 0);
13034       else
13035         {
13036           char buf[30];
13037           const char *label_name;
13038           rtx fun;
13039
13040           ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
13041           label_name = (*targetm.strip_name_encoding) (ggc_strdup (buf));
13042           fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
13043
13044           emit_library_call (init_one_libfunc (RS6000_MCOUNT), 0, VOIDmode, 1,
13045                              fun, Pmode);
13046         }
13047     }
13048   else if (DEFAULT_ABI == ABI_DARWIN)
13049     {
13050       const char *mcount_name = RS6000_MCOUNT;
13051       int caller_addr_regno = LINK_REGISTER_REGNUM;
13052
13053       /* Be conservative and always set this, at least for now.  */
13054       current_function_uses_pic_offset_table = 1;
13055
13056 #if TARGET_MACHO
13057       /* For PIC code, set up a stub and collect the caller's address
13058          from r0, which is where the prologue puts it.  */
13059       if (MACHOPIC_INDIRECT)
13060         {
13061           mcount_name = machopic_stub_name (mcount_name);
13062           if (current_function_uses_pic_offset_table)
13063             caller_addr_regno = 0;
13064         }
13065 #endif
13066       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
13067                          0, VOIDmode, 1,
13068                          gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
13069     }
13070 }
13071
13072 /* Write function profiler code.  */
13073
13074 void
13075 output_function_profiler (file, labelno)
13076   FILE *file;
13077   int labelno;
13078 {
13079   char buf[100];
13080   int save_lr = 8;
13081
13082   switch (DEFAULT_ABI)
13083     {
13084     default:
13085       abort ();
13086
13087     case ABI_V4:
13088       save_lr = 4;
13089       if (!TARGET_32BIT)
13090         {
13091           warning ("no profiling of 64-bit code for this ABI");
13092           return;
13093         }
13094       ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
13095       fprintf (file, "\tmflr %s\n", reg_names[0]);
13096       if (flag_pic == 1)
13097         {
13098           fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
13099           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
13100                        reg_names[0], save_lr, reg_names[1]);
13101           asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
13102           asm_fprintf (file, "\t{l|lwz} %s,", reg_names[0]);
13103           assemble_name (file, buf);
13104           asm_fprintf (file, "@got(%s)\n", reg_names[12]);
13105         }
13106       else if (flag_pic > 1)
13107         {
13108           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
13109                        reg_names[0], save_lr, reg_names[1]);
13110           /* Now, we need to get the address of the label.  */
13111           fputs ("\tbl 1f\n\t.long ", file);
13112           assemble_name (file, buf);
13113           fputs ("-.\n1:", file);
13114           asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
13115           asm_fprintf (file, "\t{l|lwz} %s,0(%s)\n", 
13116                        reg_names[0], reg_names[11]);
13117           asm_fprintf (file, "\t{cax|add} %s,%s,%s\n",
13118                        reg_names[0], reg_names[0], reg_names[11]);
13119         }
13120       else
13121         {
13122           asm_fprintf (file, "\t{liu|lis} %s,", reg_names[12]);
13123           assemble_name (file, buf);
13124           fputs ("@ha\n", file);
13125           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
13126                        reg_names[0], save_lr, reg_names[1]);
13127           asm_fprintf (file, "\t{cal|la} %s,", reg_names[0]);
13128           assemble_name (file, buf);
13129           asm_fprintf (file, "@l(%s)\n", reg_names[12]);
13130         }
13131
13132       /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH.  */
13133       fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
13134       break;
13135
13136     case ABI_AIX:
13137     case ABI_DARWIN:
13138       if (!TARGET_PROFILE_KERNEL)
13139         {
13140           /* Don't do anything, done in output_profile_hook (). */
13141         }
13142       else
13143         {
13144           if (TARGET_32BIT)
13145             abort ();
13146
13147           asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
13148           asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
13149
13150           if (current_function_needs_context)
13151             {
13152               asm_fprintf (file, "\tstd %s,24(%s)\n",
13153                            reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
13154               fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
13155               asm_fprintf (file, "\tld %s,24(%s)\n",
13156                            reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
13157             }
13158           else
13159             fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
13160         }
13161       break;
13162     }
13163 }
13164
13165 \f
13166 static int
13167 rs6000_use_dfa_pipeline_interface ()
13168 {
13169   return 1;
13170 }
13171
13172 /* Power4 load update and store update instructions are cracked into a
13173    load or store and an integer insn which are executed in the same cycle.
13174    Branches have their own dispatch slot which does not count against the
13175    GCC issue rate, but it changes the program flow so there are no other
13176    instructions to issue in this cycle.  */
13177
13178 static int
13179 rs6000_variable_issue (stream, verbose, insn, more)
13180   FILE *stream ATTRIBUTE_UNUSED;
13181   int verbose ATTRIBUTE_UNUSED;
13182   rtx insn;
13183   int more;
13184 {
13185   if (GET_CODE (PATTERN (insn)) == USE
13186       || GET_CODE (PATTERN (insn)) == CLOBBER)
13187     return more;
13188
13189   if (rs6000_cpu == PROCESSOR_POWER4)
13190     {
13191       enum attr_type type = get_attr_type (insn);
13192       if (type == TYPE_LOAD_EXT_U || type == TYPE_LOAD_EXT_UX
13193           || type == TYPE_LOAD_UX || type == TYPE_STORE_UX)
13194         return 0;
13195       else if (type == TYPE_LOAD_U || type == TYPE_STORE_U
13196                || type == TYPE_FPLOAD_U || type == TYPE_FPSTORE_U
13197                || type == TYPE_FPLOAD_UX || type == TYPE_FPSTORE_UX
13198                || type == TYPE_LOAD_EXT || type == TYPE_DELAYED_CR
13199                || type == TYPE_COMPARE || type == TYPE_DELAYED_COMPARE
13200                || type == TYPE_IMUL_COMPARE || type == TYPE_LMUL_COMPARE
13201                || type == TYPE_IDIV || type == TYPE_LDIV
13202                || type == TYPE_INSERT_WORD)
13203         return more > 2 ? more - 2 : 0;
13204     }
13205
13206   return more - 1;
13207 }
13208
13209 /* Adjust the cost of a scheduling dependency.  Return the new cost of
13210    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
13211
13212 static int
13213 rs6000_adjust_cost (insn, link, dep_insn, cost)
13214      rtx insn;
13215      rtx link;
13216      rtx dep_insn ATTRIBUTE_UNUSED;
13217      int cost;
13218 {
13219   if (! recog_memoized (insn))
13220     return 0;
13221
13222   if (REG_NOTE_KIND (link) != 0)
13223     return 0;
13224
13225   if (REG_NOTE_KIND (link) == 0)
13226     {
13227       /* Data dependency; DEP_INSN writes a register that INSN reads
13228          some cycles later.  */
13229       switch (get_attr_type (insn))
13230         {
13231         case TYPE_JMPREG:
13232           /* Tell the first scheduling pass about the latency between
13233              a mtctr and bctr (and mtlr and br/blr).  The first
13234              scheduling pass will not know about this latency since
13235              the mtctr instruction, which has the latency associated
13236              to it, will be generated by reload.  */
13237           return TARGET_POWER ? 5 : 4;
13238         case TYPE_BRANCH:
13239           /* Leave some extra cycles between a compare and its
13240              dependent branch, to inhibit expensive mispredicts.  */
13241           if ((rs6000_cpu_attr == CPU_PPC603
13242                || rs6000_cpu_attr == CPU_PPC604
13243                || rs6000_cpu_attr == CPU_PPC604E
13244                || rs6000_cpu_attr == CPU_PPC620
13245                || rs6000_cpu_attr == CPU_PPC630
13246                || rs6000_cpu_attr == CPU_PPC750
13247                || rs6000_cpu_attr == CPU_PPC7400
13248                || rs6000_cpu_attr == CPU_PPC7450
13249                || rs6000_cpu_attr == CPU_POWER4)
13250               && recog_memoized (dep_insn)
13251               && (INSN_CODE (dep_insn) >= 0)
13252               && (get_attr_type (dep_insn) == TYPE_CMP
13253                   || get_attr_type (dep_insn) == TYPE_COMPARE
13254                   || get_attr_type (dep_insn) == TYPE_DELAYED_COMPARE
13255                   || get_attr_type (dep_insn) == TYPE_IMUL_COMPARE
13256                   || get_attr_type (dep_insn) == TYPE_LMUL_COMPARE
13257                   || get_attr_type (dep_insn) == TYPE_FPCOMPARE
13258                   || get_attr_type (dep_insn) == TYPE_CR_LOGICAL
13259                   || get_attr_type (dep_insn) == TYPE_DELAYED_CR))
13260             return cost + 2;
13261         default:
13262           break;
13263         }
13264       /* Fall out to return default cost.  */
13265     }
13266
13267   return cost;
13268 }
13269
13270 /* A C statement (sans semicolon) to update the integer scheduling
13271    priority INSN_PRIORITY (INSN).  Reduce the priority to execute the
13272    INSN earlier, increase the priority to execute INSN later.  Do not
13273    define this macro if you do not need to adjust the scheduling
13274    priorities of insns.  */
13275
13276 static int
13277 rs6000_adjust_priority (insn, priority)
13278      rtx insn ATTRIBUTE_UNUSED;
13279      int priority;
13280 {
13281   /* On machines (like the 750) which have asymmetric integer units,
13282      where one integer unit can do multiply and divides and the other
13283      can't, reduce the priority of multiply/divide so it is scheduled
13284      before other integer operations.  */
13285
13286 #if 0
13287   if (! INSN_P (insn))
13288     return priority;
13289
13290   if (GET_CODE (PATTERN (insn)) == USE)
13291     return priority;
13292
13293   switch (rs6000_cpu_attr) {
13294   case CPU_PPC750:
13295     switch (get_attr_type (insn))
13296       {
13297       default:
13298         break;
13299
13300       case TYPE_IMUL:
13301       case TYPE_IDIV:
13302         fprintf (stderr, "priority was %#x (%d) before adjustment\n",
13303                  priority, priority);
13304         if (priority >= 0 && priority < 0x01000000)
13305           priority >>= 3;
13306         break;
13307       }
13308   }
13309 #endif
13310
13311   return priority;
13312 }
13313
13314 /* Return how many instructions the machine can issue per cycle.  */
13315
13316 static int
13317 rs6000_issue_rate ()
13318 {
13319   /* Use issue rate of 1 for first scheduling pass to decrease degradation.  */
13320   if (!reload_completed)
13321     return 1;
13322
13323   switch (rs6000_cpu_attr) {
13324   case CPU_RIOS1:  /* ? */
13325   case CPU_RS64A:
13326   case CPU_PPC601: /* ? */
13327   case CPU_PPC7450:
13328     return 3;
13329   case CPU_PPC440:
13330   case CPU_PPC603:
13331   case CPU_PPC750:
13332   case CPU_PPC7400:
13333   case CPU_PPC8540:
13334     return 2; 
13335   case CPU_RIOS2:
13336   case CPU_PPC604:
13337   case CPU_PPC604E:
13338   case CPU_PPC620:
13339   case CPU_PPC630:
13340   case CPU_POWER4:
13341     return 4;
13342   default:
13343     return 1;
13344   }
13345 }
13346
13347 /* Return how many instructions to look ahead for better insn
13348    scheduling.  */
13349
13350 static int
13351 rs6000_use_sched_lookahead ()
13352 {
13353   if (rs6000_cpu_attr == CPU_PPC8540)
13354     return 4;
13355   return 0;
13356 }
13357
13358 \f
13359 /* Length in units of the trampoline for entering a nested function.  */
13360
13361 int
13362 rs6000_trampoline_size ()
13363 {
13364   int ret = 0;
13365
13366   switch (DEFAULT_ABI)
13367     {
13368     default:
13369       abort ();
13370
13371     case ABI_AIX:
13372       ret = (TARGET_32BIT) ? 12 : 24;
13373       break;
13374
13375     case ABI_DARWIN:
13376     case ABI_V4:
13377       ret = (TARGET_32BIT) ? 40 : 48;
13378       break;
13379     }
13380
13381   return ret;
13382 }
13383
13384 /* Emit RTL insns to initialize the variable parts of a trampoline.
13385    FNADDR is an RTX for the address of the function's pure code.
13386    CXT is an RTX for the static chain value for the function.  */
13387
13388 void
13389 rs6000_initialize_trampoline (addr, fnaddr, cxt)
13390      rtx addr;
13391      rtx fnaddr;
13392      rtx cxt;
13393 {
13394   enum machine_mode pmode = Pmode;
13395   int regsize = (TARGET_32BIT) ? 4 : 8;
13396   rtx ctx_reg = force_reg (pmode, cxt);
13397
13398   switch (DEFAULT_ABI)
13399     {
13400     default:
13401       abort ();
13402
13403 /* Macros to shorten the code expansions below.  */
13404 #define MEM_DEREF(addr) gen_rtx_MEM (pmode, memory_address (pmode, addr))
13405 #define MEM_PLUS(addr,offset) \
13406   gen_rtx_MEM (pmode, memory_address (pmode, plus_constant (addr, offset)))
13407
13408     /* Under AIX, just build the 3 word function descriptor */
13409     case ABI_AIX:
13410       {
13411         rtx fn_reg = gen_reg_rtx (pmode);
13412         rtx toc_reg = gen_reg_rtx (pmode);
13413         emit_move_insn (fn_reg, MEM_DEREF (fnaddr));
13414         emit_move_insn (toc_reg, MEM_PLUS (fnaddr, regsize));
13415         emit_move_insn (MEM_DEREF (addr), fn_reg);
13416         emit_move_insn (MEM_PLUS (addr, regsize), toc_reg);
13417         emit_move_insn (MEM_PLUS (addr, 2*regsize), ctx_reg);
13418       }
13419       break;
13420
13421     /* Under V.4/eabi/darwin, __trampoline_setup does the real work.  */
13422     case ABI_DARWIN:
13423     case ABI_V4:
13424       emit_library_call (gen_rtx_SYMBOL_REF (SImode, "__trampoline_setup"),
13425                          FALSE, VOIDmode, 4,
13426                          addr, pmode,
13427                          GEN_INT (rs6000_trampoline_size ()), SImode,
13428                          fnaddr, pmode,
13429                          ctx_reg, pmode);
13430       break;
13431     }
13432
13433   return;
13434 }
13435
13436 \f
13437 /* Table of valid machine attributes.  */
13438
13439 const struct attribute_spec rs6000_attribute_table[] =
13440 {
13441   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
13442   { "longcall",  0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
13443   { "shortcall", 0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
13444   { NULL,        0, 0, false, false, false, NULL }
13445 };
13446
13447 /* Handle a "longcall" or "shortcall" attribute; arguments as in
13448    struct attribute_spec.handler.  */
13449
13450 static tree
13451 rs6000_handle_longcall_attribute (node, name, args, flags, no_add_attrs)
13452      tree *node;
13453      tree name;
13454      tree args ATTRIBUTE_UNUSED;
13455      int flags ATTRIBUTE_UNUSED;
13456      bool *no_add_attrs;
13457 {
13458   if (TREE_CODE (*node) != FUNCTION_TYPE
13459       && TREE_CODE (*node) != FIELD_DECL
13460       && TREE_CODE (*node) != TYPE_DECL)
13461     {
13462       warning ("`%s' attribute only applies to functions",
13463                IDENTIFIER_POINTER (name));
13464       *no_add_attrs = true;
13465     }
13466
13467   return NULL_TREE;
13468 }
13469
13470 /* Set longcall attributes on all functions declared when
13471    rs6000_default_long_calls is true.  */
13472 static void
13473 rs6000_set_default_type_attributes (type)
13474      tree type;
13475 {
13476   if (rs6000_default_long_calls
13477       && (TREE_CODE (type) == FUNCTION_TYPE
13478           || TREE_CODE (type) == METHOD_TYPE))
13479     TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
13480                                         NULL_TREE,
13481                                         TYPE_ATTRIBUTES (type));
13482 }
13483
13484 /* Return a reference suitable for calling a function with the
13485    longcall attribute.  */
13486
13487 struct rtx_def *
13488 rs6000_longcall_ref (call_ref)
13489      rtx call_ref;
13490 {
13491   const char *call_name;
13492   tree node;
13493
13494   if (GET_CODE (call_ref) != SYMBOL_REF)
13495     return call_ref;
13496
13497   /* System V adds '.' to the internal name, so skip them.  */
13498   call_name = XSTR (call_ref, 0);
13499   if (*call_name == '.')
13500     {
13501       while (*call_name == '.')
13502         call_name++;
13503
13504       node = get_identifier (call_name);
13505       call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
13506     }
13507
13508   return force_reg (Pmode, call_ref);
13509 }
13510 \f
13511 #ifdef USING_ELFOS_H
13512
13513 /* A C statement or statements to switch to the appropriate section
13514    for output of RTX in mode MODE.  You can assume that RTX is some
13515    kind of constant in RTL.  The argument MODE is redundant except in
13516    the case of a `const_int' rtx.  Select the section by calling
13517    `text_section' or one of the alternatives for other sections.
13518
13519    Do not define this macro if you put all constants in the read-only
13520    data section.  */
13521
13522 static void
13523 rs6000_elf_select_rtx_section (mode, x, align)
13524      enum machine_mode mode;
13525      rtx x;
13526      unsigned HOST_WIDE_INT align;
13527 {
13528   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
13529     toc_section ();
13530   else
13531     default_elf_select_rtx_section (mode, x, align);
13532 }
13533
13534 /* A C statement or statements to switch to the appropriate
13535    section for output of DECL.  DECL is either a `VAR_DECL' node
13536    or a constant of some sort.  RELOC indicates whether forming
13537    the initial value of DECL requires link-time relocations.  */
13538
13539 static void
13540 rs6000_elf_select_section (decl, reloc, align)
13541      tree decl;
13542      int reloc;
13543      unsigned HOST_WIDE_INT align;
13544 {
13545   /* Pretend that we're always building for a shared library when
13546      ABI_AIX, because otherwise we end up with dynamic relocations
13547      in read-only sections.  This happens for function pointers,
13548      references to vtables in typeinfo, and probably other cases.  */
13549   default_elf_select_section_1 (decl, reloc, align,
13550                                 flag_pic || DEFAULT_ABI == ABI_AIX);
13551 }
13552
13553 /* A C statement to build up a unique section name, expressed as a
13554    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
13555    RELOC indicates whether the initial value of EXP requires
13556    link-time relocations.  If you do not define this macro, GCC will use
13557    the symbol name prefixed by `.' as the section name.  Note - this
13558    macro can now be called for uninitialized data items as well as
13559    initialized data and functions.  */
13560
13561 static void
13562 rs6000_elf_unique_section (decl, reloc)
13563      tree decl;
13564      int reloc;
13565 {
13566   /* As above, pretend that we're always building for a shared library
13567      when ABI_AIX, to avoid dynamic relocations in read-only sections.  */
13568   default_unique_section_1 (decl, reloc,
13569                             flag_pic || DEFAULT_ABI == ABI_AIX);
13570 }
13571 \f
13572 /* For a SYMBOL_REF, set generic flags and then perform some
13573    target-specific processing.
13574
13575    When the AIX ABI is requested on a non-AIX system, replace the
13576    function name with the real name (with a leading .) rather than the
13577    function descriptor name.  This saves a lot of overriding code to
13578    read the prefixes.  */
13579
13580 static void
13581 rs6000_elf_encode_section_info (decl, rtl, first)
13582      tree decl;
13583      rtx rtl;
13584      int first;
13585 {
13586   default_encode_section_info (decl, rtl, first);
13587
13588   if (first
13589       && TREE_CODE (decl) == FUNCTION_DECL
13590       && !TARGET_AIX
13591       && DEFAULT_ABI == ABI_AIX)
13592     {
13593       rtx sym_ref = XEXP (rtl, 0);
13594       size_t len = strlen (XSTR (sym_ref, 0));
13595       char *str = alloca (len + 2);
13596       str[0] = '.';
13597       memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
13598       XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
13599     }
13600 }
13601
13602 static bool
13603 rs6000_elf_in_small_data_p (decl)
13604      tree decl;
13605 {
13606   if (rs6000_sdata == SDATA_NONE)
13607     return false;
13608
13609   if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
13610     {
13611       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
13612       if (strcmp (section, ".sdata") == 0
13613           || strcmp (section, ".sdata2") == 0
13614           || strcmp (section, ".sbss") == 0
13615           || strcmp (section, ".sbss2") == 0
13616           || strcmp (section, ".PPC.EMB.sdata0") == 0
13617           || strcmp (section, ".PPC.EMB.sbss0") == 0)
13618         return true;
13619     }
13620   else
13621     {
13622       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
13623
13624       if (size > 0
13625           && (unsigned HOST_WIDE_INT) size <= g_switch_value
13626           /* If it's not public, and we're not going to reference it there,
13627              there's no need to put it in the small data section.  */
13628           && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
13629         return true;
13630     }
13631
13632   return false;
13633 }
13634
13635 #endif /* USING_ELFOS_H */
13636
13637 \f
13638 /* Return a REG that occurs in ADDR with coefficient 1.
13639    ADDR can be effectively incremented by incrementing REG.
13640
13641    r0 is special and we must not select it as an address
13642    register by this routine since our caller will try to
13643    increment the returned register via an "la" instruction.  */
13644
13645 struct rtx_def *
13646 find_addr_reg (addr)
13647      rtx addr;
13648 {
13649   while (GET_CODE (addr) == PLUS)
13650     {
13651       if (GET_CODE (XEXP (addr, 0)) == REG
13652           && REGNO (XEXP (addr, 0)) != 0)
13653         addr = XEXP (addr, 0);
13654       else if (GET_CODE (XEXP (addr, 1)) == REG
13655                && REGNO (XEXP (addr, 1)) != 0)
13656         addr = XEXP (addr, 1);
13657       else if (CONSTANT_P (XEXP (addr, 0)))
13658         addr = XEXP (addr, 1);
13659       else if (CONSTANT_P (XEXP (addr, 1)))
13660         addr = XEXP (addr, 0);
13661       else
13662         abort ();
13663     }
13664   if (GET_CODE (addr) == REG && REGNO (addr) != 0)
13665     return addr;
13666   abort ();
13667 }
13668
13669 void
13670 rs6000_fatal_bad_address (op)
13671   rtx op;
13672 {
13673   fatal_insn ("bad address", op);
13674 }
13675
13676 #if TARGET_MACHO
13677
13678 #if 0
13679 /* Returns 1 if OP is either a symbol reference or a sum of a symbol
13680    reference and a constant.  */
13681
13682 int
13683 symbolic_operand (op)
13684      rtx op;
13685 {
13686   switch (GET_CODE (op))
13687     {
13688     case SYMBOL_REF:
13689     case LABEL_REF:
13690       return 1;
13691     case CONST:
13692       op = XEXP (op, 0);
13693       return (GET_CODE (op) == SYMBOL_REF ||
13694               (GET_CODE (XEXP (op, 0)) == SYMBOL_REF
13695                || GET_CODE (XEXP (op, 0)) == LABEL_REF)
13696               && GET_CODE (XEXP (op, 1)) == CONST_INT);
13697     default:
13698       return 0;
13699     }
13700 }
13701 #endif
13702
13703 #ifdef RS6000_LONG_BRANCH
13704
13705 static tree stub_list = 0;
13706
13707 /* ADD_COMPILER_STUB adds the compiler generated stub for handling 
13708    procedure calls to the linked list.  */
13709
13710 void 
13711 add_compiler_stub (label_name, function_name, line_number)
13712      tree label_name;
13713      tree function_name;
13714      int line_number;
13715 {
13716   tree stub = build_tree_list (function_name, label_name);
13717   TREE_TYPE (stub) = build_int_2 (line_number, 0);
13718   TREE_CHAIN (stub) = stub_list;
13719   stub_list = stub;
13720 }
13721
13722 #define STUB_LABEL_NAME(STUB)     TREE_VALUE (STUB)
13723 #define STUB_FUNCTION_NAME(STUB)  TREE_PURPOSE (STUB)
13724 #define STUB_LINE_NUMBER(STUB)    TREE_INT_CST_LOW (TREE_TYPE (STUB))
13725
13726 /* OUTPUT_COMPILER_STUB outputs the compiler generated stub for
13727    handling procedure calls from the linked list and initializes the
13728    linked list.  */
13729
13730 void
13731 output_compiler_stub ()
13732 {
13733   char tmp_buf[256];
13734   char label_buf[256];
13735   tree stub;
13736
13737   if (!flag_pic)
13738     for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
13739       {
13740         fprintf (asm_out_file,
13741                  "%s:\n", IDENTIFIER_POINTER(STUB_LABEL_NAME(stub)));
13742
13743 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
13744         if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
13745           fprintf (asm_out_file, "\t.stabd 68,0,%d\n", STUB_LINE_NUMBER(stub));
13746 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
13747
13748         if (IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub))[0] == '*')
13749           strcpy (label_buf,
13750                   IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub))+1);
13751         else
13752           {
13753             label_buf[0] = '_';
13754             strcpy (label_buf+1,
13755                     IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub)));
13756           }
13757
13758         strcpy (tmp_buf, "lis r12,hi16(");
13759         strcat (tmp_buf, label_buf);
13760         strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
13761         strcat (tmp_buf, label_buf);
13762         strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
13763         output_asm_insn (tmp_buf, 0);
13764
13765 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
13766         if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
13767           fprintf(asm_out_file, "\t.stabd 68,0,%d\n", STUB_LINE_NUMBER (stub));
13768 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
13769       }
13770
13771   stub_list = 0;
13772 }
13773
13774 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
13775    already there or not.  */
13776
13777 int
13778 no_previous_def (function_name)
13779      tree function_name;
13780 {
13781   tree stub;
13782   for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
13783     if (function_name == STUB_FUNCTION_NAME (stub))
13784       return 0;
13785   return 1;
13786 }
13787
13788 /* GET_PREV_LABEL gets the label name from the previous definition of
13789    the function.  */
13790
13791 tree
13792 get_prev_label (function_name)
13793      tree function_name;
13794 {
13795   tree stub;
13796   for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
13797     if (function_name == STUB_FUNCTION_NAME (stub))
13798       return STUB_LABEL_NAME (stub);
13799   return 0;
13800 }
13801
13802 /* INSN is either a function call or a millicode call.  It may have an
13803    unconditional jump in its delay slot.  
13804
13805    CALL_DEST is the routine we are calling.  */
13806
13807 char *
13808 output_call (insn, call_dest, operand_number)
13809      rtx insn;
13810      rtx call_dest;
13811      int operand_number;
13812 {
13813   static char buf[256];
13814   if (GET_CODE (call_dest) == SYMBOL_REF && TARGET_LONG_BRANCH && !flag_pic)
13815     {
13816       tree labelname;
13817       tree funname = get_identifier (XSTR (call_dest, 0));
13818       
13819       if (no_previous_def (funname))
13820         {
13821           int line_number = 0;
13822           rtx label_rtx = gen_label_rtx ();
13823           char *label_buf, temp_buf[256];
13824           ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
13825                                        CODE_LABEL_NUMBER (label_rtx));
13826           label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
13827           labelname = get_identifier (label_buf);
13828           for (; insn && GET_CODE (insn) != NOTE; insn = PREV_INSN (insn));
13829           if (insn)
13830             line_number = NOTE_LINE_NUMBER (insn);
13831           add_compiler_stub (labelname, funname, line_number);
13832         }
13833       else
13834         labelname = get_prev_label (funname);
13835
13836       sprintf (buf, "jbsr %%z%d,%.246s",
13837                operand_number, IDENTIFIER_POINTER (labelname));
13838       return buf;
13839     }
13840   else
13841     {
13842       sprintf (buf, "bl %%z%d", operand_number);
13843       return buf;
13844     }
13845 }
13846
13847 #endif /* RS6000_LONG_BRANCH */
13848
13849 #define GEN_LOCAL_LABEL_FOR_SYMBOL(BUF,SYMBOL,LENGTH,N)         \
13850   do {                                                          \
13851     const char *const symbol_ = (SYMBOL);                       \
13852     char *buffer_ = (BUF);                                      \
13853     if (symbol_[0] == '"')                                      \
13854       {                                                         \
13855         sprintf(buffer_, "\"L%d$%s", (N), symbol_+1);           \
13856       }                                                         \
13857     else if (name_needs_quotes(symbol_))                        \
13858       {                                                         \
13859         sprintf(buffer_, "\"L%d$%s\"", (N), symbol_);           \
13860       }                                                         \
13861     else                                                        \
13862       {                                                         \
13863         sprintf(buffer_, "L%d$%s", (N), symbol_);               \
13864       }                                                         \
13865   } while (0)
13866
13867
13868 /* Generate PIC and indirect symbol stubs.  */
13869
13870 void
13871 machopic_output_stub (file, symb, stub)
13872      FILE *file;
13873      const char *symb, *stub;
13874 {
13875   unsigned int length;
13876   char *symbol_name, *lazy_ptr_name;
13877   char *local_label_0;
13878   static int label = 0;
13879
13880   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
13881   symb = (*targetm.strip_name_encoding) (symb);
13882
13883   label += 1;
13884
13885   length = strlen (symb);
13886   symbol_name = alloca (length + 32);
13887   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
13888
13889   lazy_ptr_name = alloca (length + 32);
13890   GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
13891
13892   local_label_0 = alloca (length + 32);
13893   GEN_LOCAL_LABEL_FOR_SYMBOL (local_label_0, symb, length, 0);
13894
13895   if (flag_pic == 2)
13896     machopic_picsymbol_stub1_section ();
13897   else
13898     machopic_symbol_stub1_section ();
13899   fprintf (file, "\t.align 2\n");
13900
13901   fprintf (file, "%s:\n", stub);
13902   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
13903
13904   if (flag_pic == 2)
13905     {
13906       fprintf (file, "\tmflr r0\n");
13907       fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
13908       fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
13909       fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
13910                lazy_ptr_name, local_label_0);
13911       fprintf (file, "\tmtlr r0\n");
13912       fprintf (file, "\tlwzu r12,lo16(%s-%s)(r11)\n",
13913                lazy_ptr_name, local_label_0);
13914       fprintf (file, "\tmtctr r12\n");
13915       fprintf (file, "\tbctr\n");
13916     }
13917   else
13918    {
13919      fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
13920      fprintf (file, "\tlwzu r12,lo16(%s)(r11)\n", lazy_ptr_name);
13921      fprintf (file, "\tmtctr r12\n");
13922      fprintf (file, "\tbctr\n");
13923    }
13924   
13925   machopic_lazy_symbol_ptr_section ();
13926   fprintf (file, "%s:\n", lazy_ptr_name);
13927   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
13928   fprintf (file, "\t.long dyld_stub_binding_helper\n");
13929 }
13930
13931 /* Legitimize PIC addresses.  If the address is already
13932    position-independent, we return ORIG.  Newly generated
13933    position-independent addresses go into a reg.  This is REG if non
13934    zero, otherwise we allocate register(s) as necessary.  */
13935
13936 #define SMALL_INT(X) ((unsigned) (INTVAL(X) + 0x8000) < 0x10000)
13937
13938 rtx
13939 rs6000_machopic_legitimize_pic_address (orig, mode, reg)
13940      rtx orig;
13941      enum machine_mode mode;
13942      rtx reg;
13943 {
13944   rtx base, offset;
13945
13946   if (reg == NULL && ! reload_in_progress && ! reload_completed)
13947     reg = gen_reg_rtx (Pmode);
13948
13949   if (GET_CODE (orig) == CONST)
13950     {
13951       if (GET_CODE (XEXP (orig, 0)) == PLUS
13952           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
13953         return orig;
13954
13955       if (GET_CODE (XEXP (orig, 0)) == PLUS)
13956         {
13957           base =
13958             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
13959                                                     Pmode, reg);
13960           offset =
13961             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
13962                                                     Pmode, reg);
13963         }
13964       else
13965         abort ();
13966
13967       if (GET_CODE (offset) == CONST_INT)
13968         {
13969           if (SMALL_INT (offset))
13970             return plus_constant (base, INTVAL (offset));
13971           else if (! reload_in_progress && ! reload_completed)
13972             offset = force_reg (Pmode, offset);
13973           else
13974             {
13975               rtx mem = force_const_mem (Pmode, orig);
13976               return machopic_legitimize_pic_address (mem, Pmode, reg);
13977             }
13978         }
13979       return gen_rtx (PLUS, Pmode, base, offset);
13980     }
13981
13982   /* Fall back on generic machopic code.  */
13983   return machopic_legitimize_pic_address (orig, mode, reg);
13984 }
13985
13986 /* This is just a placeholder to make linking work without having to
13987    add this to the generic Darwin EXTRA_SECTIONS.  If -mcall-aix is
13988    ever needed for Darwin (not too likely!) this would have to get a
13989    real definition.  */
13990
13991 void
13992 toc_section ()
13993 {
13994 }
13995
13996 #endif /* TARGET_MACHO */
13997
13998 #if TARGET_ELF
13999 static unsigned int
14000 rs6000_elf_section_type_flags (decl, name, reloc)
14001      tree decl;
14002      const char *name;
14003      int reloc;
14004 {
14005   unsigned int flags
14006     = default_section_type_flags_1 (decl, name, reloc,
14007                                     flag_pic || DEFAULT_ABI == ABI_AIX);
14008
14009   if (TARGET_RELOCATABLE)
14010     flags |= SECTION_WRITE;
14011
14012   return flags;
14013 }
14014
14015 /* Record an element in the table of global constructors.  SYMBOL is
14016    a SYMBOL_REF of the function to be called; PRIORITY is a number
14017    between 0 and MAX_INIT_PRIORITY.
14018
14019    This differs from default_named_section_asm_out_constructor in
14020    that we have special handling for -mrelocatable.  */
14021
14022 static void
14023 rs6000_elf_asm_out_constructor (symbol, priority)
14024      rtx symbol;
14025      int priority;
14026 {
14027   const char *section = ".ctors";
14028   char buf[16];
14029
14030   if (priority != DEFAULT_INIT_PRIORITY)
14031     {
14032       sprintf (buf, ".ctors.%.5u",
14033                /* Invert the numbering so the linker puts us in the proper
14034                   order; constructors are run from right to left, and the
14035                   linker sorts in increasing order.  */
14036                MAX_INIT_PRIORITY - priority);
14037       section = buf;
14038     }
14039
14040   named_section_flags (section, SECTION_WRITE);
14041   assemble_align (POINTER_SIZE);
14042
14043   if (TARGET_RELOCATABLE)
14044     {
14045       fputs ("\t.long (", asm_out_file);
14046       output_addr_const (asm_out_file, symbol);
14047       fputs (")@fixup\n", asm_out_file);
14048     }
14049   else
14050     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
14051 }
14052
14053 static void
14054 rs6000_elf_asm_out_destructor (symbol, priority)
14055      rtx symbol;
14056      int priority;
14057 {
14058   const char *section = ".dtors";
14059   char buf[16];
14060
14061   if (priority != DEFAULT_INIT_PRIORITY)
14062     {
14063       sprintf (buf, ".dtors.%.5u",
14064                /* Invert the numbering so the linker puts us in the proper
14065                   order; constructors are run from right to left, and the
14066                   linker sorts in increasing order.  */
14067                MAX_INIT_PRIORITY - priority);
14068       section = buf;
14069     }
14070
14071   named_section_flags (section, SECTION_WRITE);
14072   assemble_align (POINTER_SIZE);
14073
14074   if (TARGET_RELOCATABLE)
14075     {
14076       fputs ("\t.long (", asm_out_file);
14077       output_addr_const (asm_out_file, symbol);
14078       fputs (")@fixup\n", asm_out_file);
14079     }
14080   else
14081     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
14082 }
14083
14084 void
14085 rs6000_elf_declare_function_name (file, name, decl)
14086      FILE *file;
14087      const char *name;
14088      tree decl;
14089 {
14090   if (TARGET_64BIT)
14091     {
14092       fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
14093       ASM_OUTPUT_LABEL (file, name);
14094       fputs (DOUBLE_INT_ASM_OP, file);
14095       putc ('.', file);
14096       assemble_name (file, name);
14097       fputs (",.TOC.@tocbase,0\n\t.previous\n\t.size\t", file);
14098       assemble_name (file, name);
14099       fputs (",24\n\t.type\t.", file);
14100       assemble_name (file, name);
14101       fputs (",@function\n", file);
14102       if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
14103         {
14104           fputs ("\t.globl\t.", file);
14105           assemble_name (file, name);
14106           putc ('\n', file);
14107         }
14108       ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
14109       putc ('.', file);
14110       ASM_OUTPUT_LABEL (file, name);
14111       return;
14112     }
14113
14114   if (TARGET_RELOCATABLE
14115       && (get_pool_size () != 0 || current_function_profile)
14116       && uses_TOC())
14117     {
14118       char buf[256];
14119
14120       (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
14121
14122       ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
14123       fprintf (file, "\t.long ");
14124       assemble_name (file, buf);
14125       putc ('-', file);
14126       ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
14127       assemble_name (file, buf);
14128       putc ('\n', file);
14129     }
14130
14131   ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
14132   ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
14133
14134   if (DEFAULT_ABI == ABI_AIX)
14135     {
14136       const char *desc_name, *orig_name;
14137
14138       orig_name = (*targetm.strip_name_encoding) (name);
14139       desc_name = orig_name;
14140       while (*desc_name == '.')
14141         desc_name++;
14142
14143       if (TREE_PUBLIC (decl))
14144         fprintf (file, "\t.globl %s\n", desc_name);
14145
14146       fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
14147       fprintf (file, "%s:\n", desc_name);
14148       fprintf (file, "\t.long %s\n", orig_name);
14149       fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
14150       if (DEFAULT_ABI == ABI_AIX)
14151         fputs ("\t.long 0\n", file);
14152       fprintf (file, "\t.previous\n");
14153     }
14154   ASM_OUTPUT_LABEL (file, name);
14155 }
14156 #endif
14157
14158 #if TARGET_XCOFF
14159 static void
14160 rs6000_xcoff_asm_globalize_label (stream, name)
14161      FILE *stream;
14162      const char *name;
14163 {
14164   fputs (GLOBAL_ASM_OP, stream);
14165   RS6000_OUTPUT_BASENAME (stream, name);
14166   putc ('\n', stream);
14167 }
14168
14169 static void
14170 rs6000_xcoff_asm_named_section (name, flags)
14171      const char *name;
14172      unsigned int flags;
14173 {
14174   int smclass;
14175   static const char * const suffix[3] = { "PR", "RO", "RW" };
14176
14177   if (flags & SECTION_CODE)
14178     smclass = 0;
14179   else if (flags & SECTION_WRITE)
14180     smclass = 2;
14181   else
14182     smclass = 1;
14183
14184   fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
14185            (flags & SECTION_CODE) ? "." : "",
14186            name, suffix[smclass], flags & SECTION_ENTSIZE);
14187 }
14188
14189 static void
14190 rs6000_xcoff_select_section (decl, reloc, align)
14191      tree decl;
14192      int reloc;
14193      unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
14194 {
14195   if (decl_readonly_section_1 (decl, reloc, 1))
14196     {
14197       if (TREE_PUBLIC (decl))
14198         read_only_data_section ();
14199       else
14200         read_only_private_data_section ();
14201     }
14202   else
14203     {
14204       if (TREE_PUBLIC (decl))
14205         data_section ();
14206       else
14207         private_data_section ();
14208     }
14209 }
14210
14211 static void
14212 rs6000_xcoff_unique_section (decl, reloc)
14213      tree decl;
14214      int reloc ATTRIBUTE_UNUSED;
14215 {
14216   const char *name;
14217
14218   /* Use select_section for private and uninitialized data.  */
14219   if (!TREE_PUBLIC (decl)
14220       || DECL_COMMON (decl)
14221       || DECL_INITIAL (decl) == NULL_TREE
14222       || DECL_INITIAL (decl) == error_mark_node
14223       || (flag_zero_initialized_in_bss
14224           && initializer_zerop (DECL_INITIAL (decl))))
14225     return;
14226
14227   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
14228   name = (*targetm.strip_name_encoding) (name);
14229   DECL_SECTION_NAME (decl) = build_string (strlen (name), name);
14230 }
14231
14232 /* Select section for constant in constant pool.
14233
14234    On RS/6000, all constants are in the private read-only data area.
14235    However, if this is being placed in the TOC it must be output as a
14236    toc entry.  */
14237
14238 static void
14239 rs6000_xcoff_select_rtx_section (mode, x, align)
14240      enum machine_mode mode;
14241      rtx x;
14242      unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED;
14243 {
14244   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
14245     toc_section ();
14246   else
14247     read_only_private_data_section ();
14248 }
14249
14250 /* Remove any trailing [DS] or the like from the symbol name.  */
14251
14252 static const char *
14253 rs6000_xcoff_strip_name_encoding (name)
14254      const char *name;
14255 {
14256   size_t len;
14257   if (*name == '*')
14258     name++;
14259   len = strlen (name);
14260   if (name[len - 1] == ']')
14261     return ggc_alloc_string (name, len - 4);
14262   else
14263     return name;
14264 }
14265
14266 /* Section attributes.  AIX is always PIC.  */
14267
14268 static unsigned int
14269 rs6000_xcoff_section_type_flags (decl, name, reloc)
14270      tree decl;
14271      const char *name;
14272      int reloc;
14273 {
14274   unsigned int align;
14275   unsigned int flags = default_section_type_flags_1 (decl, name, reloc, 1);
14276
14277   /* Align to at least UNIT size.  */
14278   if (flags & SECTION_CODE)
14279     align = MIN_UNITS_PER_WORD;
14280   else
14281     /* Increase alignment of large objects if not already stricter.  */
14282     align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
14283                  int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
14284                  ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
14285
14286   return flags | (exact_log2 (align) & SECTION_ENTSIZE);
14287 }
14288
14289 /* Output at beginning of assembler file.
14290
14291    Initialize the section names for the RS/6000 at this point.
14292
14293    Specify filename, including full path, to assembler.
14294
14295    We want to go into the TOC section so at least one .toc will be emitted.
14296    Also, in order to output proper .bs/.es pairs, we need at least one static
14297    [RW] section emitted.
14298
14299    Finally, declare mcount when profiling to make the assembler happy.  */
14300
14301 static void
14302 rs6000_xcoff_file_start ()
14303 {
14304   rs6000_gen_section_name (&xcoff_bss_section_name,
14305                            main_input_filename, ".bss_");
14306   rs6000_gen_section_name (&xcoff_private_data_section_name,
14307                            main_input_filename, ".rw_");
14308   rs6000_gen_section_name (&xcoff_read_only_section_name,
14309                            main_input_filename, ".ro_");
14310
14311   fputs ("\t.file\t", asm_out_file);
14312   output_quoted_string (asm_out_file, main_input_filename);
14313   fputc ('\n', asm_out_file);
14314   toc_section ();
14315   if (write_symbols != NO_DEBUG)
14316     private_data_section ();
14317   text_section ();
14318   if (profile_flag)
14319     fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
14320   rs6000_file_start ();
14321 }
14322
14323 /* Output at end of assembler file.
14324    On the RS/6000, referencing data should automatically pull in text.  */
14325
14326 static void
14327 rs6000_xcoff_file_end ()
14328 {
14329   text_section ();
14330   fputs ("_section_.text:\n", asm_out_file);
14331   data_section ();
14332   fputs (TARGET_32BIT
14333          ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
14334          asm_out_file);
14335 }
14336 #endif /* TARGET_XCOFF */
14337
14338 #if TARGET_MACHO
14339 /* Cross-module name binding.  Darwin does not support overriding
14340    functions at dynamic-link time.  */
14341
14342 static bool
14343 rs6000_binds_local_p (decl)
14344      tree decl;
14345 {
14346   return default_binds_local_p_1 (decl, 0);
14347 }
14348 #endif
14349
14350 /* Compute a (partial) cost for rtx X.  Return true if the complete
14351    cost has been computed, and false if subexpressions should be
14352    scanned.  In either case, *TOTAL contains the cost result.  */
14353
14354 static bool
14355 rs6000_rtx_costs (x, code, outer_code, total)
14356      rtx x;
14357      int code, outer_code ATTRIBUTE_UNUSED;
14358      int *total;
14359 {
14360   switch (code)
14361     {
14362       /* On the RS/6000, if it is valid in the insn, it is free.
14363          So this always returns 0.  */
14364     case CONST_INT:
14365     case CONST:
14366     case LABEL_REF:
14367     case SYMBOL_REF:
14368     case CONST_DOUBLE:
14369     case HIGH:
14370       *total = 0;
14371       return true;
14372
14373     case PLUS:
14374       *total = ((GET_CODE (XEXP (x, 1)) == CONST_INT
14375                  && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1))
14376                                                + 0x8000) >= 0x10000)
14377                  && ((INTVAL (XEXP (x, 1)) & 0xffff) != 0))
14378                 ? COSTS_N_INSNS (2)
14379                 : COSTS_N_INSNS (1));
14380       return true;
14381
14382     case AND:
14383     case IOR:
14384     case XOR:
14385       *total = ((GET_CODE (XEXP (x, 1)) == CONST_INT
14386                  && (INTVAL (XEXP (x, 1)) & (~ (HOST_WIDE_INT) 0xffff)) != 0
14387                  && ((INTVAL (XEXP (x, 1)) & 0xffff) != 0))
14388                 ? COSTS_N_INSNS (2)
14389                 : COSTS_N_INSNS (1));
14390       return true;
14391
14392     case MULT:
14393       if (optimize_size)
14394         {
14395           *total = COSTS_N_INSNS (2);
14396           return true;
14397         }
14398       switch (rs6000_cpu)
14399         {
14400         case PROCESSOR_RIOS1:
14401         case PROCESSOR_PPC405:
14402           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
14403                     ? COSTS_N_INSNS (5)
14404                     : (INTVAL (XEXP (x, 1)) >= -256
14405                        && INTVAL (XEXP (x, 1)) <= 255)
14406                     ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4));
14407           return true;
14408
14409         case PROCESSOR_PPC440:
14410           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
14411                     ? COSTS_N_INSNS (3)
14412                     : COSTS_N_INSNS (2));
14413           return true;
14414
14415         case PROCESSOR_RS64A:
14416           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
14417                     ? GET_MODE (XEXP (x, 1)) != DImode
14418                     ? COSTS_N_INSNS (20) : COSTS_N_INSNS (34)
14419                     : (INTVAL (XEXP (x, 1)) >= -256
14420                        && INTVAL (XEXP (x, 1)) <= 255)
14421                     ? COSTS_N_INSNS (8) : COSTS_N_INSNS (12));
14422           return true;
14423
14424         case PROCESSOR_RIOS2:
14425         case PROCESSOR_MPCCORE:
14426         case PROCESSOR_PPC604e:
14427           *total = COSTS_N_INSNS (2);
14428           return true;
14429
14430         case PROCESSOR_PPC601:
14431           *total = COSTS_N_INSNS (5);
14432           return true;
14433
14434         case PROCESSOR_PPC603:
14435         case PROCESSOR_PPC7400:
14436         case PROCESSOR_PPC750:
14437           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
14438                     ? COSTS_N_INSNS (5)
14439                     : (INTVAL (XEXP (x, 1)) >= -256
14440                        && INTVAL (XEXP (x, 1)) <= 255)
14441                     ? COSTS_N_INSNS (2) : COSTS_N_INSNS (3));
14442           return true;
14443
14444         case PROCESSOR_PPC7450:
14445           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
14446                     ? COSTS_N_INSNS (4)
14447                     : COSTS_N_INSNS (3));
14448           return true;
14449
14450         case PROCESSOR_PPC403:
14451         case PROCESSOR_PPC604:
14452         case PROCESSOR_PPC8540:
14453           *total = COSTS_N_INSNS (4);
14454           return true;
14455
14456         case PROCESSOR_PPC620:
14457         case PROCESSOR_PPC630:
14458           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
14459                     ? GET_MODE (XEXP (x, 1)) != DImode
14460                     ? COSTS_N_INSNS (5) : COSTS_N_INSNS (7)
14461                     : (INTVAL (XEXP (x, 1)) >= -256
14462                        && INTVAL (XEXP (x, 1)) <= 255)
14463                     ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4));
14464           return true;
14465
14466         case PROCESSOR_POWER4:
14467           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
14468                     ? GET_MODE (XEXP (x, 1)) != DImode
14469                     ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4)
14470                     : COSTS_N_INSNS (2));
14471           return true;
14472
14473         default:
14474           abort ();
14475         }
14476
14477     case DIV:
14478     case MOD:
14479       if (GET_CODE (XEXP (x, 1)) == CONST_INT
14480           && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
14481         {
14482           *total = COSTS_N_INSNS (2);
14483           return true;
14484         }
14485       /* FALLTHRU */
14486
14487     case UDIV:
14488     case UMOD:
14489       switch (rs6000_cpu)
14490         {
14491         case PROCESSOR_RIOS1:
14492           *total = COSTS_N_INSNS (19);
14493           return true;
14494
14495         case PROCESSOR_RIOS2:
14496           *total = COSTS_N_INSNS (13);
14497           return true;
14498
14499         case PROCESSOR_RS64A:
14500           *total = (GET_MODE (XEXP (x, 1)) != DImode
14501                     ? COSTS_N_INSNS (65)
14502                     : COSTS_N_INSNS (67));
14503           return true;
14504
14505         case PROCESSOR_MPCCORE:
14506           *total = COSTS_N_INSNS (6);
14507           return true;
14508
14509         case PROCESSOR_PPC403:
14510           *total = COSTS_N_INSNS (33);
14511           return true;
14512
14513         case PROCESSOR_PPC405:
14514           *total = COSTS_N_INSNS (35);
14515           return true;
14516
14517         case PROCESSOR_PPC440:
14518           *total = COSTS_N_INSNS (34);
14519           return true;
14520
14521         case PROCESSOR_PPC601:
14522           *total = COSTS_N_INSNS (36);
14523           return true;
14524
14525         case PROCESSOR_PPC603:
14526           *total = COSTS_N_INSNS (37);
14527           return true;
14528
14529         case PROCESSOR_PPC604:
14530         case PROCESSOR_PPC604e:
14531           *total = COSTS_N_INSNS (20);
14532           return true;
14533
14534         case PROCESSOR_PPC620:
14535         case PROCESSOR_PPC630:
14536           *total = (GET_MODE (XEXP (x, 1)) != DImode
14537                     ? COSTS_N_INSNS (21)
14538                     : COSTS_N_INSNS (37));
14539           return true;
14540
14541         case PROCESSOR_PPC750:
14542         case PROCESSOR_PPC8540:
14543         case PROCESSOR_PPC7400:
14544           *total = COSTS_N_INSNS (19);
14545           return true;
14546
14547         case PROCESSOR_PPC7450:
14548           *total = COSTS_N_INSNS (23);
14549           return true;
14550
14551         case PROCESSOR_POWER4:
14552           *total = (GET_MODE (XEXP (x, 1)) != DImode
14553                     ? COSTS_N_INSNS (18)
14554                     : COSTS_N_INSNS (34));
14555           return true;
14556
14557         default:
14558           abort ();
14559         }
14560
14561     case FFS:
14562       *total = COSTS_N_INSNS (4);
14563       return true;
14564
14565     case MEM:
14566       /* MEM should be slightly more expensive than (plus (reg) (const)) */
14567       *total = 5;
14568       return true;
14569
14570     default:
14571       return false;
14572     }
14573 }
14574
14575 /* A C expression returning the cost of moving data from a register of class
14576    CLASS1 to one of CLASS2.  */
14577
14578 int
14579 rs6000_register_move_cost (mode, from, to)
14580      enum machine_mode mode;
14581      enum reg_class from, to;
14582 {
14583   /*  Moves from/to GENERAL_REGS.  */
14584   if (reg_classes_intersect_p (to, GENERAL_REGS)
14585       || reg_classes_intersect_p (from, GENERAL_REGS))
14586     {
14587       if (! reg_classes_intersect_p (to, GENERAL_REGS))
14588         from = to;
14589
14590       if (from == FLOAT_REGS || from == ALTIVEC_REGS)
14591         return (rs6000_memory_move_cost (mode, from, 0)
14592                 + rs6000_memory_move_cost (mode, GENERAL_REGS, 0));
14593
14594 /* It's more expensive to move CR_REGS than CR0_REGS because of the shift...*/
14595       else if (from == CR_REGS)
14596         return 4;
14597
14598       else
14599 /* A move will cost one instruction per GPR moved.  */
14600         return 2 * HARD_REGNO_NREGS (0, mode);
14601     }
14602
14603 /* Moving between two similar registers is just one instruction.  */
14604   else if (reg_classes_intersect_p (to, from))
14605     return mode == TFmode ? 4 : 2;
14606
14607 /* Everything else has to go through GENERAL_REGS.  */
14608   else
14609     return (rs6000_register_move_cost (mode, GENERAL_REGS, to) 
14610             + rs6000_register_move_cost (mode, from, GENERAL_REGS));
14611 }
14612
14613 /* A C expressions returning the cost of moving data of MODE from a register to
14614    or from memory.  */
14615
14616 int
14617 rs6000_memory_move_cost (mode, class, in)
14618   enum machine_mode mode;
14619   enum reg_class class;
14620   int in ATTRIBUTE_UNUSED;
14621 {
14622   if (reg_classes_intersect_p (class, GENERAL_REGS))
14623     return 4 * HARD_REGNO_NREGS (0, mode);
14624   else if (reg_classes_intersect_p (class, FLOAT_REGS))
14625     return 4 * HARD_REGNO_NREGS (32, mode);
14626   else if (reg_classes_intersect_p (class, ALTIVEC_REGS))
14627     return 4 * HARD_REGNO_NREGS (FIRST_ALTIVEC_REGNO, mode);
14628   else
14629     return 4 + rs6000_register_move_cost (mode, class, GENERAL_REGS);
14630 }
14631
14632 /* Return an RTX representing where to find the function value of a
14633    function returning MODE.  */
14634 static rtx
14635 rs6000_complex_function_value (enum machine_mode mode)
14636 {
14637   unsigned int regno;
14638   rtx r1, r2;
14639   enum machine_mode inner = GET_MODE_INNER (mode);
14640
14641   if (FLOAT_MODE_P (mode))
14642     regno = FP_ARG_RETURN;
14643   else
14644     {
14645       regno = GP_ARG_RETURN;
14646
14647       /* 32-bit is OK since it'll go in r3/r4.  */
14648       if (TARGET_32BIT
14649           && GET_MODE_BITSIZE (inner) >= 32)
14650         return gen_rtx_REG (mode, regno);
14651     }
14652
14653   r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
14654                           const0_rtx);
14655   r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
14656                           GEN_INT (GET_MODE_UNIT_SIZE (inner)));
14657   return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
14658 }
14659
14660 /* Define how to find the value returned by a function.
14661    VALTYPE is the data type of the value (as a tree).
14662    If the precise function being called is known, FUNC is its FUNCTION_DECL;
14663    otherwise, FUNC is 0.
14664
14665    On the SPE, both FPs and vectors are returned in r3.
14666
14667    On RS/6000 an integer value is in r3 and a floating-point value is in
14668    fp1, unless -msoft-float.  */
14669
14670 rtx
14671 rs6000_function_value (tree valtype, tree func ATTRIBUTE_UNUSED)
14672 {
14673   enum machine_mode mode;
14674   unsigned int regno;
14675
14676   if ((INTEGRAL_TYPE_P (valtype)
14677        && TYPE_PRECISION (valtype) < BITS_PER_WORD)
14678       || POINTER_TYPE_P (valtype))
14679     mode = word_mode;
14680   else
14681     mode = TYPE_MODE (valtype);
14682
14683   if (TREE_CODE (valtype) == REAL_TYPE && TARGET_HARD_FLOAT && TARGET_FPRS)
14684     regno = FP_ARG_RETURN;
14685   else if (TREE_CODE (valtype) == COMPLEX_TYPE
14686            && TARGET_HARD_FLOAT
14687            && SPLIT_COMPLEX_ARGS)
14688     return rs6000_complex_function_value (mode);
14689   else if (TREE_CODE (valtype) == VECTOR_TYPE && TARGET_ALTIVEC)
14690     regno = ALTIVEC_ARG_RETURN;
14691   else
14692     regno = GP_ARG_RETURN;
14693
14694   return gen_rtx_REG (mode, regno);
14695 }
14696
14697 /* Define how to find the value returned by a library function
14698    assuming the value has mode MODE.  */
14699 rtx
14700 rs6000_libcall_value (enum machine_mode mode)
14701 {
14702   unsigned int regno;
14703
14704   if (GET_MODE_CLASS (mode) == MODE_FLOAT
14705            && TARGET_HARD_FLOAT && TARGET_FPRS)
14706     regno = FP_ARG_RETURN;
14707   else if (ALTIVEC_VECTOR_MODE (mode))
14708     regno = ALTIVEC_ARG_RETURN;
14709   else if (COMPLEX_MODE_P (mode) && SPLIT_COMPLEX_ARGS)
14710     return rs6000_complex_function_value (mode);
14711   else
14712     regno = GP_ARG_RETURN;
14713
14714   return gen_rtx_REG (mode, regno);
14715 }
14716
14717 /* Return true if TYPE is of type __ev64_opaque__.  */
14718
14719 static bool
14720 is_ev64_opaque_type (type)
14721      tree type;
14722 {
14723   return (TARGET_SPE
14724           && (type == opaque_V2SI_type_node
14725               || type == opaque_V2SF_type_node
14726               || type == opaque_p_V2SI_type_node
14727               || (TREE_CODE (type) == VECTOR_TYPE
14728                   && TYPE_NAME (type)
14729                   && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14730                   && DECL_NAME (TYPE_NAME (type))
14731                   && strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))),
14732                              "__ev64_opaque__") == 0)));
14733 }
14734
14735 static rtx
14736 rs6000_dwarf_register_span (reg)
14737      rtx reg;
14738 {
14739   unsigned regno;
14740
14741   if (!TARGET_SPE || !SPE_VECTOR_MODE (GET_MODE (reg)))
14742     return NULL_RTX;
14743
14744   regno = REGNO (reg);
14745
14746   /* The duality of the SPE register size wreaks all kinds of havoc.
14747      This is a way of distinguishing r0 in 32-bits from r0 in
14748      64-bits.  */
14749   return
14750     gen_rtx_PARALLEL (VOIDmode,
14751                       BYTES_BIG_ENDIAN
14752                       ? gen_rtvec (2,
14753                                    gen_rtx_REG (SImode, regno + 1200),
14754                                    gen_rtx_REG (SImode, regno))
14755                       : gen_rtvec (2,
14756                                    gen_rtx_REG (SImode, regno),
14757                                    gen_rtx_REG (SImode, regno + 1200)));
14758 }
14759
14760 #include "gt-rs6000.h"