OSDN Git Service

* config/rs6000/rs6000.h (USE_FP_FOR_ARG_P): Move to rs6000.c.
[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 #include "sched-int.h"
55 #if TARGET_XCOFF
56 #include "xcoffout.h"  /* get declarations of xcoff_*_section_name */
57 #endif
58
59 #ifndef TARGET_NO_PROTOTYPE
60 #define TARGET_NO_PROTOTYPE 0
61 #endif
62
63 #define EASY_VECTOR_15(n, x, y) ((n) >= -16 && (n) <= 15 \
64                                  && easy_vector_same (x, y))
65
66 #define EASY_VECTOR_15_ADD_SELF(n, x, y) ((n) >= 0x10 && (n) <= 0x1e \
67                                           && !((n) & 1)              \
68                                           && easy_vector_same (x, y))
69
70 #define min(A,B)        ((A) < (B) ? (A) : (B))
71 #define max(A,B)        ((A) > (B) ? (A) : (B))
72
73 /* Target cpu type */
74
75 enum processor_type rs6000_cpu;
76 struct rs6000_cpu_select rs6000_select[3] =
77 {
78   /* switch             name,                   tune    arch */
79   { (const char *)0,    "--with-cpu=",          1,      1 },
80   { (const char *)0,    "-mcpu=",               1,      1 },
81   { (const char *)0,    "-mtune=",              1,      0 },
82 };
83
84 /* Support adjust_priority scheduler hook 
85    and -mprioritize-restricted-insns= option.  */
86 const char *rs6000_sched_restricted_insns_priority_str;
87 int rs6000_sched_restricted_insns_priority;
88
89 /* Support for -msched-costly-dep option.  */
90 const char *rs6000_sched_costly_dep_str;
91 enum rs6000_dependence_cost rs6000_sched_costly_dep;
92
93 /* Support for -minsert-sched-nops option.  */
94 const char *rs6000_sched_insert_nops_str;
95 enum rs6000_nop_insertion rs6000_sched_insert_nops;
96
97 /* Size of long double */
98 const char *rs6000_long_double_size_string;
99 int rs6000_long_double_type_size;
100
101 /* Whether -mabi=altivec has appeared */
102 int rs6000_altivec_abi;
103
104 /* Whether VRSAVE instructions should be generated.  */
105 int rs6000_altivec_vrsave;
106
107 /* String from -mvrsave= option.  */
108 const char *rs6000_altivec_vrsave_string;
109
110 /* Nonzero if we want SPE ABI extensions.  */
111 int rs6000_spe_abi;
112
113 /* Whether isel instructions should be generated.  */
114 int rs6000_isel;
115
116 /* Whether SPE simd instructions should be generated.  */
117 int rs6000_spe;
118
119 /* Nonzero if floating point operations are done in the GPRs.  */
120 int rs6000_float_gprs = 0;
121
122 /* String from -mfloat-gprs=.  */
123 const char *rs6000_float_gprs_string;
124
125 /* String from -misel=.  */
126 const char *rs6000_isel_string;
127
128 /* String from -mspe=.  */
129 const char *rs6000_spe_string;
130
131 /* Set to nonzero once AIX common-mode calls have been defined.  */
132 static GTY(()) int common_mode_defined;
133
134 /* Save information from a "cmpxx" operation until the branch or scc is
135    emitted.  */
136 rtx rs6000_compare_op0, rs6000_compare_op1;
137 int rs6000_compare_fp_p;
138
139 /* Label number of label created for -mrelocatable, to call to so we can
140    get the address of the GOT section */
141 int rs6000_pic_labelno;
142
143 #ifdef USING_ELFOS_H
144 /* Which abi to adhere to */
145 const char *rs6000_abi_name;
146
147 /* Semantics of the small data area */
148 enum rs6000_sdata_type rs6000_sdata = SDATA_DATA;
149
150 /* Which small data model to use */
151 const char *rs6000_sdata_name = (char *)0;
152
153 /* Counter for labels which are to be placed in .fixup.  */
154 int fixuplabelno = 0;
155 #endif
156
157 /* Bit size of immediate TLS offsets and string from which it is decoded.  */
158 int rs6000_tls_size = 32;
159 const char *rs6000_tls_size_string;
160
161 /* ABI enumeration available for subtarget to use.  */
162 enum rs6000_abi rs6000_current_abi;
163
164 /* ABI string from -mabi= option.  */
165 const char *rs6000_abi_string;
166
167 /* Debug flags */
168 const char *rs6000_debug_name;
169 int rs6000_debug_stack;         /* debug stack applications */
170 int rs6000_debug_arg;           /* debug argument handling */
171
172 /* Opaque types.  */
173 static GTY(()) tree opaque_V2SI_type_node;
174 static GTY(()) tree opaque_V2SF_type_node;
175 static GTY(()) tree opaque_p_V2SI_type_node;
176
177 const char *rs6000_traceback_name;
178 static enum {
179   traceback_default = 0,
180   traceback_none,
181   traceback_part,
182   traceback_full
183 } rs6000_traceback;
184
185 /* Flag to say the TOC is initialized */
186 int toc_initialized;
187 char toc_label_name[10];
188
189 /* Alias set for saves and restores from the rs6000 stack.  */
190 static int rs6000_sr_alias_set;
191
192 /* Call distance, overridden by -mlongcall and #pragma longcall(1).
193    The only place that looks at this is rs6000_set_default_type_attributes;
194    everywhere else should rely on the presence or absence of a longcall
195    attribute on the function declaration.  */
196 int rs6000_default_long_calls;
197 const char *rs6000_longcall_switch;
198
199 /* Control alignment for fields within structures. */
200 /* String from -malign-XXXXX. */
201 const char *rs6000_alignment_string;
202 int rs6000_alignment_flags;
203
204 struct builtin_description
205 {
206   /* mask is not const because we're going to alter it below.  This
207      nonsense will go away when we rewrite the -march infrastructure
208      to give us more target flag bits.  */
209   unsigned int mask;
210   const enum insn_code icode;
211   const char *const name;
212   const enum rs6000_builtins code;
213 };
214
215 static bool rs6000_function_ok_for_sibcall (tree, tree);
216 static int num_insns_constant_wide (HOST_WIDE_INT);
217 static void validate_condition_mode (enum rtx_code, enum machine_mode);
218 static rtx rs6000_generate_compare (enum rtx_code);
219 static void rs6000_maybe_dead (rtx);
220 static void rs6000_emit_stack_tie (void);
221 static void rs6000_frame_related (rtx, rtx, HOST_WIDE_INT, rtx, rtx);
222 static rtx spe_synthesize_frame_save (rtx);
223 static bool spe_func_has_64bit_regs_p (void);
224 static void emit_frame_save (rtx, rtx, enum machine_mode, unsigned int,
225                              int, int);
226 static rtx gen_frame_mem_offset (enum machine_mode, rtx, int);
227 static void rs6000_emit_allocate_stack (HOST_WIDE_INT, int);
228 static unsigned rs6000_hash_constant (rtx);
229 static unsigned toc_hash_function (const void *);
230 static int toc_hash_eq (const void *, const void *);
231 static int constant_pool_expr_1 (rtx, int *, int *);
232 static bool constant_pool_expr_p (rtx);
233 static bool toc_relative_expr_p (rtx);
234 static bool legitimate_small_data_p (enum machine_mode, rtx);
235 static bool legitimate_offset_address_p (enum machine_mode, rtx, int);
236 static bool legitimate_indexed_address_p (rtx, int);
237 static bool legitimate_indirect_address_p (rtx, int);
238 static bool legitimate_lo_sum_address_p (enum machine_mode, rtx, int);
239 static struct machine_function * rs6000_init_machine_status (void);
240 static bool rs6000_assemble_integer (rtx, unsigned int, int);
241 #ifdef HAVE_GAS_HIDDEN
242 static void rs6000_assemble_visibility (tree, int);
243 #endif
244 static int rs6000_ra_ever_killed (void);
245 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
246 extern const struct attribute_spec rs6000_attribute_table[];
247 static void rs6000_set_default_type_attributes (tree);
248 static void rs6000_output_function_prologue (FILE *, HOST_WIDE_INT);
249 static void rs6000_output_function_epilogue (FILE *, HOST_WIDE_INT);
250 static void rs6000_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
251                                     tree);
252 static rtx rs6000_emit_set_long_const (rtx, HOST_WIDE_INT, HOST_WIDE_INT);
253 static bool rs6000_return_in_memory (tree, tree);
254 static void rs6000_file_start (void);
255 #if TARGET_ELF
256 static unsigned int rs6000_elf_section_type_flags (tree, const char *, int);
257 static void rs6000_elf_asm_out_constructor (rtx, int);
258 static void rs6000_elf_asm_out_destructor (rtx, int);
259 static void rs6000_elf_select_section (tree, int, unsigned HOST_WIDE_INT);
260 static void rs6000_elf_unique_section (tree, int);
261 static void rs6000_elf_select_rtx_section (enum machine_mode, rtx,
262                                            unsigned HOST_WIDE_INT);
263 static void rs6000_elf_encode_section_info (tree, rtx, int)
264      ATTRIBUTE_UNUSED;
265 static bool rs6000_elf_in_small_data_p (tree);
266 #endif
267 #if TARGET_XCOFF
268 static void rs6000_xcoff_asm_globalize_label (FILE *, const char *);
269 static void rs6000_xcoff_asm_named_section (const char *, unsigned int);
270 static void rs6000_xcoff_select_section (tree, int, unsigned HOST_WIDE_INT);
271 static void rs6000_xcoff_unique_section (tree, int);
272 static void rs6000_xcoff_select_rtx_section (enum machine_mode, rtx,
273                                              unsigned HOST_WIDE_INT);
274 static const char * rs6000_xcoff_strip_name_encoding (const char *);
275 static unsigned int rs6000_xcoff_section_type_flags (tree, const char *, int);
276 static void rs6000_xcoff_file_start (void);
277 static void rs6000_xcoff_file_end (void);
278 #endif
279 #if TARGET_MACHO
280 static bool rs6000_binds_local_p (tree);
281 #endif
282 static int rs6000_use_dfa_pipeline_interface (void);
283 static int rs6000_variable_issue (FILE *, int, rtx, int);
284 static bool rs6000_rtx_costs (rtx, int, int, int *);
285 static int rs6000_adjust_cost (rtx, rtx, rtx, int);
286 static bool is_microcoded_insn (rtx);
287 static int is_dispatch_slot_restricted (rtx);
288 static bool is_cracked_insn (rtx);
289 static bool is_branch_slot_insn (rtx);
290 static int rs6000_adjust_priority (rtx, int);
291 static int rs6000_issue_rate (void);
292 static bool rs6000_is_costly_dependence (rtx, rtx, rtx, int, int);
293 static rtx get_next_active_insn (rtx, rtx);
294 static bool insn_terminates_group_p (rtx , enum group_termination);
295 static bool is_costly_group (rtx *, rtx);
296 static int force_new_group (int, FILE *, rtx *, rtx, bool *, int, int *);
297 static int redefine_groups (FILE *, int, rtx, rtx);
298 static int pad_groups (FILE *, int, rtx, rtx);
299 static void rs6000_sched_finish (FILE *, int);
300 static int rs6000_use_sched_lookahead (void);
301
302 static void rs6000_init_builtins (void);
303 static rtx rs6000_expand_unop_builtin (enum insn_code, tree, rtx);
304 static rtx rs6000_expand_binop_builtin (enum insn_code, tree, rtx);
305 static rtx rs6000_expand_ternop_builtin (enum insn_code, tree, rtx);
306 static rtx rs6000_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
307 static void altivec_init_builtins (void);
308 static void rs6000_common_init_builtins (void);
309 static void rs6000_init_libfuncs (void);
310
311 static void enable_mask_for_builtins (struct builtin_description *, int,
312                                       enum rs6000_builtins,
313                                       enum rs6000_builtins);
314 static void spe_init_builtins (void);
315 static rtx spe_expand_builtin (tree, rtx, bool *);
316 static rtx spe_expand_predicate_builtin (enum insn_code, tree, rtx);
317 static rtx spe_expand_evsel_builtin (enum insn_code, tree, rtx);
318 static int rs6000_emit_int_cmove (rtx, rtx, rtx, rtx);
319
320 static rtx altivec_expand_builtin (tree, rtx, bool *);
321 static rtx altivec_expand_ld_builtin (tree, rtx, bool *);
322 static rtx altivec_expand_st_builtin (tree, rtx, bool *);
323 static rtx altivec_expand_dst_builtin (tree, rtx, bool *);
324 static rtx altivec_expand_abs_builtin (enum insn_code, tree, rtx);
325 static rtx altivec_expand_predicate_builtin (enum insn_code, 
326                                             const char *, tree, rtx);
327 static rtx altivec_expand_lv_builtin (enum insn_code, tree, rtx);
328 static rtx altivec_expand_stv_builtin (enum insn_code, tree);
329 static void rs6000_parse_abi_options (void);
330 static void rs6000_parse_alignment_option (void);
331 static void rs6000_parse_tls_size_option (void);
332 static void rs6000_parse_yes_no_option (const char *, const char *, int *);
333 static int first_altivec_reg_to_save (void);
334 static unsigned int compute_vrsave_mask (void);
335 static void is_altivec_return_reg (rtx, void *);
336 static rtx generate_set_vrsave (rtx, rs6000_stack_t *, int);
337 int easy_vector_constant (rtx, enum machine_mode);
338 static int easy_vector_same (rtx, enum machine_mode);
339 static bool is_ev64_opaque_type (tree);
340 static rtx rs6000_dwarf_register_span (rtx);
341 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
342 static rtx rs6000_tls_get_addr (void);
343 static rtx rs6000_got_sym (void);
344 static inline int rs6000_tls_symbol_ref_1 (rtx *, void *);
345 static const char *rs6000_get_some_local_dynamic_name (void);
346 static int rs6000_get_some_local_dynamic_name_1 (rtx *, void *);
347 static rtx rs6000_complex_function_value (enum machine_mode);
348 static rtx rs6000_spe_function_arg (CUMULATIVE_ARGS *,
349                                     enum machine_mode, tree);
350 static rtx rs6000_mixed_function_arg (CUMULATIVE_ARGS *,
351                                       enum machine_mode, tree, int);
352 static void setup_incoming_varargs (CUMULATIVE_ARGS *,
353                                     enum machine_mode, tree,
354                                     int *, int);
355 static tree rs6000_build_builtin_va_list (void);
356
357 /* Hash table stuff for keeping track of TOC entries.  */
358
359 struct toc_hash_struct GTY(())
360 {
361   /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
362      ASM_OUTPUT_SPECIAL_POOL_ENTRY_P.  */
363   rtx key;
364   enum machine_mode key_mode;
365   int labelno;
366 };
367
368 static GTY ((param_is (struct toc_hash_struct))) htab_t toc_hash_table;
369 \f
370 /* Default register names.  */
371 char rs6000_reg_names[][8] =
372 {
373       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
374       "8",  "9", "10", "11", "12", "13", "14", "15",
375      "16", "17", "18", "19", "20", "21", "22", "23",
376      "24", "25", "26", "27", "28", "29", "30", "31",
377       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
378       "8",  "9", "10", "11", "12", "13", "14", "15",
379      "16", "17", "18", "19", "20", "21", "22", "23",
380      "24", "25", "26", "27", "28", "29", "30", "31",
381      "mq", "lr", "ctr","ap",
382       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
383       "xer",
384       /* AltiVec registers.  */
385       "0",  "1",  "2",  "3",  "4",  "5",  "6", "7",
386       "8",  "9",  "10", "11", "12", "13", "14", "15",
387       "16", "17", "18", "19", "20", "21", "22", "23",
388       "24", "25", "26", "27", "28", "29", "30", "31",
389       "vrsave", "vscr",
390       /* SPE registers.  */
391       "spe_acc", "spefscr"
392 };
393
394 #ifdef TARGET_REGNAMES
395 static const char alt_reg_names[][8] =
396 {
397    "%r0",   "%r1",  "%r2",  "%r3",  "%r4",  "%r5",  "%r6",  "%r7",
398    "%r8",   "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
399   "%r16",  "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
400   "%r24",  "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
401    "%f0",   "%f1",  "%f2",  "%f3",  "%f4",  "%f5",  "%f6",  "%f7",
402    "%f8",   "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
403   "%f16",  "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
404   "%f24",  "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
405     "mq",    "lr",  "ctr",   "ap",
406   "%cr0",  "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
407    "xer",
408   /* AltiVec registers.  */
409    "%v0",  "%v1",  "%v2",  "%v3",  "%v4",  "%v5",  "%v6", "%v7",
410    "%v8",  "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
411   "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
412   "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
413   "vrsave", "vscr",
414   /* SPE registers.  */
415   "spe_acc", "spefscr"
416 };
417 #endif
418 \f
419 #ifndef MASK_STRICT_ALIGN
420 #define MASK_STRICT_ALIGN 0
421 #endif
422 #ifndef TARGET_PROFILE_KERNEL
423 #define TARGET_PROFILE_KERNEL 0
424 #endif
425
426 /* The VRSAVE bitmask puts bit %v0 as the most significant bit.  */
427 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
428
429 /* Return 1 for a symbol ref for a thread-local storage symbol.  */
430 #define RS6000_SYMBOL_REF_TLS_P(RTX) \
431   (GET_CODE (RTX) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (RTX) != 0)
432 \f
433 /* Initialize the GCC target structure.  */
434 #undef TARGET_ATTRIBUTE_TABLE
435 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
436 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
437 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
438
439 #undef TARGET_ASM_ALIGNED_DI_OP
440 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
441
442 /* Default unaligned ops are only provided for ELF.  Find the ops needed
443    for non-ELF systems.  */
444 #ifndef OBJECT_FORMAT_ELF
445 #if TARGET_XCOFF
446 /* For XCOFF.  rs6000_assemble_integer will handle unaligned DIs on
447    64-bit targets.  */
448 #undef TARGET_ASM_UNALIGNED_HI_OP
449 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
450 #undef TARGET_ASM_UNALIGNED_SI_OP
451 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
452 #undef TARGET_ASM_UNALIGNED_DI_OP
453 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
454 #else
455 /* For Darwin.  */
456 #undef TARGET_ASM_UNALIGNED_HI_OP
457 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
458 #undef TARGET_ASM_UNALIGNED_SI_OP
459 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
460 #endif
461 #endif
462
463 /* This hook deals with fixups for relocatable code and DI-mode objects
464    in 64-bit code.  */
465 #undef TARGET_ASM_INTEGER
466 #define TARGET_ASM_INTEGER rs6000_assemble_integer
467
468 #ifdef HAVE_GAS_HIDDEN
469 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
470 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
471 #endif
472
473 #undef TARGET_HAVE_TLS
474 #define TARGET_HAVE_TLS HAVE_AS_TLS
475
476 #undef TARGET_CANNOT_FORCE_CONST_MEM
477 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_tls_referenced_p
478
479 #undef TARGET_ASM_FUNCTION_PROLOGUE
480 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
481 #undef TARGET_ASM_FUNCTION_EPILOGUE
482 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
483
484 #undef  TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE 
485 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE rs6000_use_dfa_pipeline_interface
486 #undef  TARGET_SCHED_VARIABLE_ISSUE
487 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
488
489 #undef TARGET_SCHED_ISSUE_RATE
490 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
491 #undef TARGET_SCHED_ADJUST_COST
492 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
493 #undef TARGET_SCHED_ADJUST_PRIORITY
494 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
495 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE      
496 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
497 #undef TARGET_SCHED_FINISH
498 #define TARGET_SCHED_FINISH rs6000_sched_finish
499
500 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
501 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
502
503 #undef TARGET_INIT_BUILTINS
504 #define TARGET_INIT_BUILTINS rs6000_init_builtins
505
506 #undef TARGET_EXPAND_BUILTIN
507 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
508
509 #undef TARGET_INIT_LIBFUNCS
510 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
511
512 #if TARGET_MACHO
513 #undef TARGET_BINDS_LOCAL_P
514 #define TARGET_BINDS_LOCAL_P rs6000_binds_local_p
515 #endif
516
517 #undef TARGET_ASM_OUTPUT_MI_THUNK
518 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
519
520 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
521 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
522
523 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
524 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
525
526 #undef TARGET_RTX_COSTS
527 #define TARGET_RTX_COSTS rs6000_rtx_costs
528 #undef TARGET_ADDRESS_COST
529 #define TARGET_ADDRESS_COST hook_int_rtx_0
530
531 #undef TARGET_VECTOR_OPAQUE_P
532 #define TARGET_VECTOR_OPAQUE_P is_ev64_opaque_type
533
534 #undef TARGET_DWARF_REGISTER_SPAN
535 #define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
536
537 /* On rs6000, function arguments are promoted, as are function return
538    values.  */
539 #undef TARGET_PROMOTE_FUNCTION_ARGS
540 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
541 #undef TARGET_PROMOTE_FUNCTION_RETURN
542 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
543
544 /* Structure return values are passed as an extra parameter.  */
545 #undef TARGET_STRUCT_VALUE_RTX
546 #define TARGET_STRUCT_VALUE_RTX hook_rtx_tree_int_null
547
548 #undef TARGET_RETURN_IN_MEMORY
549 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
550
551 #undef TARGET_SETUP_INCOMING_VARARGS
552 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
553
554 /* Always strict argument naming on rs6000.  */
555 #undef TARGET_STRICT_ARGUMENT_NAMING
556 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
557 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
558 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
559
560 #undef TARGET_BUILD_BUILTIN_VA_LIST
561 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
562
563 struct gcc_target targetm = TARGET_INITIALIZER;
564 \f
565 /* Override command line options.  Mostly we process the processor
566    type and sometimes adjust other TARGET_ options.  */
567
568 void
569 rs6000_override_options (const char *default_cpu)
570 {
571   size_t i, j;
572   struct rs6000_cpu_select *ptr;
573
574   /* Simplify the entries below by making a mask for any POWER
575      variant and any PowerPC variant.  */
576
577 #define POWER_MASKS (MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING)
578 #define POWERPC_MASKS (MASK_POWERPC | MASK_PPC_GPOPT \
579                        | MASK_PPC_GFXOPT | MASK_POWERPC64)
580 #define POWERPC_OPT_MASKS (MASK_PPC_GPOPT | MASK_PPC_GFXOPT)
581
582   static struct ptt
583     {
584       const char *const name;           /* Canonical processor name.  */
585       const enum processor_type processor; /* Processor type enum value.  */
586       const int target_enable;  /* Target flags to enable.  */
587       const int target_disable; /* Target flags to disable.  */
588     } const processor_target_table[]
589       = {{"common", PROCESSOR_COMMON, MASK_NEW_MNEMONICS,
590             POWER_MASKS | POWERPC_MASKS},
591          {"power", PROCESSOR_POWER,
592             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
593             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
594          {"power2", PROCESSOR_POWER,
595             MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING,
596             POWERPC_MASKS | MASK_NEW_MNEMONICS},
597          {"power3", PROCESSOR_PPC630,
598             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
599             POWER_MASKS},
600          {"power4", PROCESSOR_POWER4,
601             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
602             POWER_MASKS},
603          {"powerpc", PROCESSOR_POWERPC,
604             MASK_POWERPC | MASK_NEW_MNEMONICS,
605             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
606          {"powerpc64", PROCESSOR_POWERPC64,
607             MASK_POWERPC | MASK_POWERPC64 | MASK_NEW_MNEMONICS,
608             POWER_MASKS | POWERPC_OPT_MASKS},
609          {"rios", PROCESSOR_RIOS1,
610             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
611             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
612          {"rios1", PROCESSOR_RIOS1,
613             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
614             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
615          {"rsc", PROCESSOR_PPC601,
616             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
617             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
618          {"rsc1", PROCESSOR_PPC601,
619             MASK_POWER | MASK_MULTIPLE | MASK_STRING,
620             MASK_POWER2 | POWERPC_MASKS | MASK_NEW_MNEMONICS},
621          {"rios2", PROCESSOR_RIOS2,
622             MASK_POWER | MASK_MULTIPLE | MASK_STRING | MASK_POWER2,
623             POWERPC_MASKS | MASK_NEW_MNEMONICS},
624          {"rs64a", PROCESSOR_RS64A,
625             MASK_POWERPC | MASK_NEW_MNEMONICS,
626             POWER_MASKS | POWERPC_OPT_MASKS},
627          {"401", PROCESSOR_PPC403,
628             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
629             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
630          {"403", PROCESSOR_PPC403,
631             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS | MASK_STRICT_ALIGN,
632             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
633          {"405", PROCESSOR_PPC405,
634             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
635             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
636          {"405fp", PROCESSOR_PPC405,
637             MASK_POWERPC | MASK_NEW_MNEMONICS,
638             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
639          {"440", PROCESSOR_PPC440,
640             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
641             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
642          {"440fp", PROCESSOR_PPC440,
643             MASK_POWERPC | MASK_NEW_MNEMONICS,
644             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
645          {"505", PROCESSOR_MPCCORE,
646             MASK_POWERPC | MASK_NEW_MNEMONICS,
647             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
648          {"601", PROCESSOR_PPC601,
649             MASK_POWER | MASK_POWERPC | MASK_NEW_MNEMONICS | MASK_MULTIPLE | MASK_STRING,
650             MASK_POWER2 | POWERPC_OPT_MASKS | MASK_POWERPC64},
651          {"602", PROCESSOR_PPC603,
652             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
653             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
654          {"603", PROCESSOR_PPC603,
655             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
656             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
657          {"603e", PROCESSOR_PPC603,
658             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
659             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
660          {"ec603e", PROCESSOR_PPC603,
661             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
662             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
663          {"604", PROCESSOR_PPC604,
664             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
665             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
666          {"604e", PROCESSOR_PPC604e,
667             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
668             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
669          {"620", PROCESSOR_PPC620,
670             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
671             POWER_MASKS},
672          {"630", PROCESSOR_PPC630,
673             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
674             POWER_MASKS},
675          {"740", PROCESSOR_PPC750,
676             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
677             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
678          {"750", PROCESSOR_PPC750,
679             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
680             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
681          {"G3", PROCESSOR_PPC750,
682             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
683             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
684          {"7400", PROCESSOR_PPC7400,
685             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
686             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
687          {"7450", PROCESSOR_PPC7450,
688             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
689             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
690          {"G4", PROCESSOR_PPC7450,
691             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
692             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
693          {"8540", PROCESSOR_PPC8540,
694             MASK_POWERPC | MASK_PPC_GFXOPT | MASK_NEW_MNEMONICS,
695             POWER_MASKS | MASK_PPC_GPOPT | MASK_POWERPC64},
696          {"801", PROCESSOR_MPCCORE,
697             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
698             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
699          {"821", PROCESSOR_MPCCORE,
700             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
701             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
702          {"823", PROCESSOR_MPCCORE,
703             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
704             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
705          {"860", PROCESSOR_MPCCORE,
706             MASK_POWERPC | MASK_SOFT_FLOAT | MASK_NEW_MNEMONICS,
707             POWER_MASKS | POWERPC_OPT_MASKS | MASK_POWERPC64},
708          {"970", PROCESSOR_POWER4,
709             MASK_POWERPC | POWERPC_OPT_MASKS | MASK_NEW_MNEMONICS,
710             POWER_MASKS},
711          {"G5", PROCESSOR_POWER4,
712             MASK_POWERPC | POWERPC_OPT_MASKS | MASK_NEW_MNEMONICS,
713             POWER_MASKS}};
714
715   const size_t ptt_size = ARRAY_SIZE (processor_target_table);
716
717   /* Save current -mmultiple/-mno-multiple status.  */
718   int multiple = TARGET_MULTIPLE;
719   /* Save current -mstring/-mno-string status.  */
720   int string = TARGET_STRING;
721
722   /* Identify the processor type.  */
723   rs6000_select[0].string = default_cpu;
724   rs6000_cpu = TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT;
725
726   for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
727     {
728       ptr = &rs6000_select[i];
729       if (ptr->string != (char *)0 && ptr->string[0] != '\0')
730         {
731           for (j = 0; j < ptt_size; j++)
732             if (! strcmp (ptr->string, processor_target_table[j].name))
733               {
734                 if (ptr->set_tune_p)
735                   rs6000_cpu = processor_target_table[j].processor;
736
737                 if (ptr->set_arch_p)
738                   {
739                     target_flags |= processor_target_table[j].target_enable;
740                     target_flags &= ~processor_target_table[j].target_disable;
741                   }
742                 break;
743               }
744
745           if (j == ptt_size)
746             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
747         }
748     }
749
750   if (TARGET_E500)
751     rs6000_isel = 1;
752
753   /* If we are optimizing big endian systems for space, use the load/store
754      multiple and string instructions.  */
755   if (BYTES_BIG_ENDIAN && optimize_size)
756     target_flags |= MASK_MULTIPLE | MASK_STRING;
757
758   /* If -mmultiple or -mno-multiple was explicitly used, don't
759      override with the processor default */
760   if ((target_flags_explicit & MASK_MULTIPLE) != 0)
761     target_flags = (target_flags & ~MASK_MULTIPLE) | multiple;
762
763   /* If -mstring or -mno-string was explicitly used, don't override
764      with the processor default.  */
765   if ((target_flags_explicit & MASK_STRING) != 0)
766     target_flags = (target_flags & ~MASK_STRING) | string;
767
768   /* Don't allow -mmultiple or -mstring on little endian systems
769      unless the cpu is a 750, because the hardware doesn't support the
770      instructions used in little endian mode, and causes an alignment
771      trap.  The 750 does not cause an alignment trap (except when the
772      target is unaligned).  */
773
774   if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
775     {
776       if (TARGET_MULTIPLE)
777         {
778           target_flags &= ~MASK_MULTIPLE;
779           if ((target_flags_explicit & MASK_MULTIPLE) != 0)
780             warning ("-mmultiple is not supported on little endian systems");
781         }
782
783       if (TARGET_STRING)
784         {
785           target_flags &= ~MASK_STRING;
786           if ((target_flags_explicit & MASK_STRING) != 0)
787             warning ("-mstring is not supported on little endian systems");
788         }
789     }
790
791   /* Set debug flags */
792   if (rs6000_debug_name)
793     {
794       if (! strcmp (rs6000_debug_name, "all"))
795         rs6000_debug_stack = rs6000_debug_arg = 1;
796       else if (! strcmp (rs6000_debug_name, "stack"))
797         rs6000_debug_stack = 1;
798       else if (! strcmp (rs6000_debug_name, "arg"))
799         rs6000_debug_arg = 1;
800       else
801         error ("unknown -mdebug-%s switch", rs6000_debug_name);
802     }
803
804   if (rs6000_traceback_name)
805     {
806       if (! strncmp (rs6000_traceback_name, "full", 4))
807         rs6000_traceback = traceback_full;
808       else if (! strncmp (rs6000_traceback_name, "part", 4))
809         rs6000_traceback = traceback_part;
810       else if (! strncmp (rs6000_traceback_name, "no", 2))
811         rs6000_traceback = traceback_none;
812       else
813         error ("unknown -mtraceback arg `%s'; expecting `full', `partial' or `none'",
814                rs6000_traceback_name);
815     }
816
817   /* Set size of long double */
818   rs6000_long_double_type_size = 64;
819   if (rs6000_long_double_size_string)
820     {
821       char *tail;
822       int size = strtol (rs6000_long_double_size_string, &tail, 10);
823       if (*tail != '\0' || (size != 64 && size != 128))
824         error ("Unknown switch -mlong-double-%s",
825                rs6000_long_double_size_string);
826       else
827         rs6000_long_double_type_size = size;
828     }
829
830   /* Handle -mabi= options.  */
831   rs6000_parse_abi_options ();
832
833   /* Handle -malign-XXXXX option.  */
834   rs6000_parse_alignment_option ();
835
836   /* Handle generic -mFOO=YES/NO options.  */
837   rs6000_parse_yes_no_option ("vrsave", rs6000_altivec_vrsave_string,
838                               &rs6000_altivec_vrsave);
839   rs6000_parse_yes_no_option ("isel", rs6000_isel_string,
840                               &rs6000_isel);
841   rs6000_parse_yes_no_option ("spe", rs6000_spe_string, &rs6000_spe);
842   rs6000_parse_yes_no_option ("float-gprs", rs6000_float_gprs_string,
843                               &rs6000_float_gprs);
844
845   /* Handle -mtls-size option.  */
846   rs6000_parse_tls_size_option ();
847
848 #ifdef SUBTARGET_OVERRIDE_OPTIONS
849   SUBTARGET_OVERRIDE_OPTIONS;
850 #endif
851 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
852   SUBSUBTARGET_OVERRIDE_OPTIONS;
853 #endif
854
855   if (TARGET_E500)
856     {
857       /* The e500 does not have string instructions, and we set
858          MASK_STRING above when optimizing for size.  */
859       if ((target_flags & MASK_STRING) != 0)
860         target_flags = target_flags & ~MASK_STRING;
861
862       /* No SPE means 64-bit long doubles, even if an E500.  */
863       if (rs6000_spe_string != 0
864           && !strcmp (rs6000_spe_string, "no"))
865         rs6000_long_double_type_size = 64;
866     }
867   else if (rs6000_select[1].string != NULL)
868     {
869       /* For the powerpc-eabispe configuration, we set all these by
870          default, so let's unset them if we manually set another
871          CPU that is not the E500.  */
872       if (rs6000_abi_string == 0)
873         rs6000_spe_abi = 0;
874       if (rs6000_spe_string == 0)
875         rs6000_spe = 0;
876       if (rs6000_float_gprs_string == 0)
877         rs6000_float_gprs = 0;
878       if (rs6000_isel_string == 0)
879         rs6000_isel = 0;
880       if (rs6000_long_double_size_string == 0)
881         rs6000_long_double_type_size = 64;
882     }
883
884   /* Handle -m(no-)longcall option.  This is a bit of a cheap hack,
885      using TARGET_OPTIONS to handle a toggle switch, but we're out of
886      bits in target_flags so TARGET_SWITCHES cannot be used.
887      Assumption here is that rs6000_longcall_switch points into the
888      text of the complete option, rather than being a copy, so we can
889      scan back for the presence or absence of the no- modifier.  */
890   if (rs6000_longcall_switch)
891     {
892       const char *base = rs6000_longcall_switch;
893       while (base[-1] != 'm') base--;
894
895       if (*rs6000_longcall_switch != '\0')
896         error ("invalid option `%s'", base);
897       rs6000_default_long_calls = (base[0] != 'n');
898     }
899
900   /* Handle -mprioritize-restricted-insns option.  */
901   rs6000_sched_restricted_insns_priority = DEFAULT_RESTRICTED_INSNS_PRIORITY;
902   if (rs6000_sched_restricted_insns_priority_str)
903     rs6000_sched_restricted_insns_priority =
904       atoi (rs6000_sched_restricted_insns_priority_str);
905
906   /* Handle -msched-costly-dep option.  */
907   rs6000_sched_costly_dep = DEFAULT_SCHED_COSTLY_DEP;
908   if (rs6000_sched_costly_dep_str)
909     {
910       if (! strcmp (rs6000_sched_costly_dep_str, "no"))  
911         rs6000_sched_costly_dep = no_dep_costly;
912       else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
913         rs6000_sched_costly_dep = all_deps_costly;
914       else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
915         rs6000_sched_costly_dep = true_store_to_load_dep_costly;
916       else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
917         rs6000_sched_costly_dep = store_to_load_dep_costly;
918       else 
919         rs6000_sched_costly_dep = atoi (rs6000_sched_costly_dep_str);
920     }
921
922   /* Handle -minsert-sched-nops option.  */
923   rs6000_sched_insert_nops = DEFAULT_SCHED_FINISH_NOP_INSERTION_SCHEME;
924   if (rs6000_sched_insert_nops_str)
925     {
926       if (! strcmp (rs6000_sched_insert_nops_str, "no"))
927         rs6000_sched_insert_nops = sched_finish_none;
928       else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
929         rs6000_sched_insert_nops = sched_finish_pad_groups;
930       else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
931         rs6000_sched_insert_nops = sched_finish_regroup_exact;
932       else
933         rs6000_sched_insert_nops = atoi (rs6000_sched_insert_nops_str);
934     }
935
936 #ifdef TARGET_REGNAMES
937   /* If the user desires alternate register names, copy in the
938      alternate names now.  */
939   if (TARGET_REGNAMES)
940     memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
941 #endif
942
943   /* Set TARGET_AIX_STRUCT_RET last, after the ABI is determined.
944      If -maix-struct-return or -msvr4-struct-return was explicitly
945      used, don't override with the ABI default.  */
946   if ((target_flags_explicit & MASK_AIX_STRUCT_RET) == 0)
947     {
948       if (DEFAULT_ABI == ABI_V4 && !DRAFT_V4_STRUCT_RET)
949         target_flags = (target_flags & ~MASK_AIX_STRUCT_RET);
950       else
951         target_flags |= MASK_AIX_STRUCT_RET;
952     }
953
954   if (TARGET_LONG_DOUBLE_128
955       && (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_DARWIN))
956     REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
957
958   /* Allocate an alias set for register saves & restores from stack.  */
959   rs6000_sr_alias_set = new_alias_set ();
960
961   if (TARGET_TOC) 
962     ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
963
964   /* We can only guarantee the availability of DI pseudo-ops when
965      assembling for 64-bit targets.  */
966   if (!TARGET_64BIT)
967     {
968       targetm.asm_out.aligned_op.di = NULL;
969       targetm.asm_out.unaligned_op.di = NULL;
970     }
971
972   /* Set maximum branch target alignment at two instructions, eight bytes.  */
973   align_jumps_max_skip = 8;
974   align_loops_max_skip = 8;
975
976   /* Arrange to save and restore machine status around nested functions.  */
977   init_machine_status = rs6000_init_machine_status;
978 }
979
980 /* Handle generic options of the form -mfoo=yes/no.
981    NAME is the option name.
982    VALUE is the option value.
983    FLAG is the pointer to the flag where to store a 1 or 0, depending on
984    whether the option value is 'yes' or 'no' respectively.  */
985 static void
986 rs6000_parse_yes_no_option (const char *name, const char *value, int *flag)
987 {
988   if (value == 0)
989     return;
990   else if (!strcmp (value, "yes"))
991     *flag = 1;
992   else if (!strcmp (value, "no"))
993     *flag = 0;
994   else
995     error ("unknown -m%s= option specified: '%s'", name, value);
996 }
997
998 /* Handle -mabi= options.  */
999 static void
1000 rs6000_parse_abi_options (void)
1001 {
1002   if (rs6000_abi_string == 0)
1003     return;
1004   else if (! strcmp (rs6000_abi_string, "altivec"))
1005     rs6000_altivec_abi = 1;
1006   else if (! strcmp (rs6000_abi_string, "no-altivec"))
1007     rs6000_altivec_abi = 0;
1008   else if (! strcmp (rs6000_abi_string, "spe"))
1009     {
1010       rs6000_spe_abi = 1;
1011       if (!TARGET_SPE_ABI)
1012         error ("not configured for ABI: '%s'", rs6000_abi_string);
1013     }
1014   
1015   else if (! strcmp (rs6000_abi_string, "no-spe"))
1016     rs6000_spe_abi = 0;
1017   else
1018     error ("unknown ABI specified: '%s'", rs6000_abi_string);
1019 }
1020
1021 /* Handle -malign-XXXXXX options.  */
1022 static void
1023 rs6000_parse_alignment_option (void)
1024 {
1025   if (rs6000_alignment_string == 0)
1026     return;
1027   else if (! strcmp (rs6000_alignment_string, "power"))
1028     rs6000_alignment_flags = MASK_ALIGN_POWER;
1029   else if (! strcmp (rs6000_alignment_string, "natural"))
1030     rs6000_alignment_flags = MASK_ALIGN_NATURAL;
1031   else
1032     error ("unknown -malign-XXXXX option specified: '%s'",
1033            rs6000_alignment_string);
1034 }
1035
1036 /* Validate and record the size specified with the -mtls-size option.  */
1037
1038 static void
1039 rs6000_parse_tls_size_option (void)
1040 {
1041   if (rs6000_tls_size_string == 0)
1042     return;
1043   else if (strcmp (rs6000_tls_size_string, "16") == 0)
1044     rs6000_tls_size = 16;
1045   else if (strcmp (rs6000_tls_size_string, "32") == 0)
1046     rs6000_tls_size = 32;
1047   else if (strcmp (rs6000_tls_size_string, "64") == 0)
1048     rs6000_tls_size = 64;
1049   else
1050     error ("bad value `%s' for -mtls-size switch", rs6000_tls_size_string);
1051 }
1052
1053 void
1054 optimization_options (int level ATTRIBUTE_UNUSED, int size ATTRIBUTE_UNUSED)
1055 {
1056 }
1057 \f
1058 /* Do anything needed at the start of the asm file.  */
1059
1060 static void
1061 rs6000_file_start (void)
1062 {
1063   size_t i;
1064   char buffer[80];
1065   const char *start = buffer;
1066   struct rs6000_cpu_select *ptr;
1067   const char *default_cpu = TARGET_CPU_DEFAULT;
1068   FILE *file = asm_out_file;
1069
1070   default_file_start ();
1071
1072 #ifdef TARGET_BI_ARCH
1073   if ((TARGET_DEFAULT ^ target_flags) & MASK_64BIT)
1074     default_cpu = 0;
1075 #endif
1076
1077   if (flag_verbose_asm)
1078     {
1079       sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
1080       rs6000_select[0].string = default_cpu;
1081
1082       for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
1083         {
1084           ptr = &rs6000_select[i];
1085           if (ptr->string != (char *)0 && ptr->string[0] != '\0')
1086             {
1087               fprintf (file, "%s %s%s", start, ptr->name, ptr->string);
1088               start = "";
1089             }
1090         }
1091
1092 #ifdef USING_ELFOS_H
1093       switch (rs6000_sdata)
1094         {
1095         case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
1096         case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
1097         case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
1098         case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
1099         }
1100
1101       if (rs6000_sdata && g_switch_value)
1102         {
1103           fprintf (file, "%s -G " HOST_WIDE_INT_PRINT_UNSIGNED, start,
1104                    g_switch_value);
1105           start = "";
1106         }
1107 #endif
1108
1109       if (*start == '\0')
1110         putc ('\n', file);
1111     }
1112 }
1113 \f
1114 /* Return nonzero if this function is known to have a null epilogue.  */
1115
1116 int
1117 direct_return (void)
1118 {
1119   if (reload_completed)
1120     {
1121       rs6000_stack_t *info = rs6000_stack_info ();
1122
1123       if (info->first_gp_reg_save == 32
1124           && info->first_fp_reg_save == 64
1125           && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
1126           && ! info->lr_save_p
1127           && ! info->cr_save_p
1128           && info->vrsave_mask == 0
1129           && ! info->push_p)
1130         return 1;
1131     }
1132
1133   return 0;
1134 }
1135
1136 /* Returns 1 always.  */
1137
1138 int
1139 any_operand (rtx op ATTRIBUTE_UNUSED, 
1140              enum machine_mode mode ATTRIBUTE_UNUSED)
1141 {
1142   return 1;
1143 }
1144
1145 /* Returns 1 if op is the count register.  */
1146 int
1147 count_register_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1148 {
1149   if (GET_CODE (op) != REG)
1150     return 0;
1151
1152   if (REGNO (op) == COUNT_REGISTER_REGNUM)
1153     return 1;
1154
1155   if (REGNO (op) > FIRST_PSEUDO_REGISTER)
1156     return 1;
1157
1158   return 0;
1159 }
1160
1161 /* Returns 1 if op is an altivec register.  */
1162 int
1163 altivec_register_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1164 {
1165   
1166   return (register_operand (op, mode)
1167           && (GET_CODE (op) != REG
1168               || REGNO (op) > FIRST_PSEUDO_REGISTER
1169               || ALTIVEC_REGNO_P (REGNO (op))));
1170 }
1171
1172 int
1173 xer_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1174 {
1175   if (GET_CODE (op) != REG)
1176     return 0;
1177
1178   if (XER_REGNO_P (REGNO (op)))
1179     return 1;
1180
1181   return 0;
1182 }
1183
1184 /* Return 1 if OP is a signed 8-bit constant.  Int multiplication
1185    by such constants completes more quickly.  */
1186
1187 int
1188 s8bit_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1189 {
1190   return ( GET_CODE (op) == CONST_INT
1191           && (INTVAL (op) >= -128 && INTVAL (op) <= 127));
1192 }
1193
1194 /* Return 1 if OP is a constant that can fit in a D field.  */
1195
1196 int
1197 short_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1198 {
1199   return (GET_CODE (op) == CONST_INT
1200           && CONST_OK_FOR_LETTER_P (INTVAL (op), 'I'));
1201 }
1202
1203 /* Similar for an unsigned D field.  */
1204
1205 int
1206 u_short_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1207 {
1208   return (GET_CODE (op) == CONST_INT
1209           && CONST_OK_FOR_LETTER_P (INTVAL (op) & GET_MODE_MASK (mode), 'K'));
1210 }
1211
1212 /* Return 1 if OP is a CONST_INT that cannot fit in a signed D field.  */
1213
1214 int
1215 non_short_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1216 {
1217   return (GET_CODE (op) == CONST_INT
1218           && (unsigned HOST_WIDE_INT) (INTVAL (op) + 0x8000) >= 0x10000);
1219 }
1220
1221 /* Returns 1 if OP is a CONST_INT that is a positive value
1222    and an exact power of 2.  */
1223
1224 int
1225 exact_log2_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1226 {
1227   return (GET_CODE (op) == CONST_INT
1228           && INTVAL (op) > 0
1229           && exact_log2 (INTVAL (op)) >= 0);
1230 }
1231
1232 /* Returns 1 if OP is a register that is not special (i.e., not MQ,
1233    ctr, or lr).  */
1234
1235 int
1236 gpc_reg_operand (rtx op, enum machine_mode mode)
1237 {
1238   return (register_operand (op, mode)
1239           && (GET_CODE (op) != REG
1240               || (REGNO (op) >= ARG_POINTER_REGNUM 
1241                   && !XER_REGNO_P (REGNO (op)))
1242               || REGNO (op) < MQ_REGNO));
1243 }
1244
1245 /* Returns 1 if OP is either a pseudo-register or a register denoting a
1246    CR field.  */
1247
1248 int
1249 cc_reg_operand (rtx op, enum machine_mode mode)
1250 {
1251   return (register_operand (op, mode)
1252           && (GET_CODE (op) != REG
1253               || REGNO (op) >= FIRST_PSEUDO_REGISTER
1254               || CR_REGNO_P (REGNO (op))));
1255 }
1256
1257 /* Returns 1 if OP is either a pseudo-register or a register denoting a
1258    CR field that isn't CR0.  */
1259
1260 int
1261 cc_reg_not_cr0_operand (rtx op, enum machine_mode mode)
1262 {
1263   return (register_operand (op, mode)
1264           && (GET_CODE (op) != REG
1265               || REGNO (op) >= FIRST_PSEUDO_REGISTER
1266               || CR_REGNO_NOT_CR0_P (REGNO (op))));
1267 }
1268
1269 /* Returns 1 if OP is either a constant integer valid for a D-field or
1270    a non-special register.  If a register, it must be in the proper
1271    mode unless MODE is VOIDmode.  */
1272
1273 int
1274 reg_or_short_operand (rtx op, enum machine_mode mode)
1275 {
1276   return short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
1277 }
1278
1279 /* Similar, except check if the negation of the constant would be
1280    valid for a D-field.  */
1281
1282 int
1283 reg_or_neg_short_operand (rtx op, enum machine_mode mode)
1284 {
1285   if (GET_CODE (op) == CONST_INT)
1286     return CONST_OK_FOR_LETTER_P (INTVAL (op), 'P');
1287
1288   return gpc_reg_operand (op, mode);
1289 }
1290
1291 /* Returns 1 if OP is either a constant integer valid for a DS-field or
1292    a non-special register.  If a register, it must be in the proper
1293    mode unless MODE is VOIDmode.  */
1294
1295 int
1296 reg_or_aligned_short_operand (rtx op, enum machine_mode mode)
1297 {
1298   if (gpc_reg_operand (op, mode))
1299     return 1;
1300   else if (short_cint_operand (op, mode) && !(INTVAL (op) & 3))
1301     return 1;
1302
1303   return 0;
1304 }
1305
1306
1307 /* Return 1 if the operand is either a register or an integer whose
1308    high-order 16 bits are zero.  */
1309
1310 int
1311 reg_or_u_short_operand (rtx op, enum machine_mode mode)
1312 {
1313   return u_short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
1314 }
1315
1316 /* Return 1 is the operand is either a non-special register or ANY
1317    constant integer.  */
1318
1319 int
1320 reg_or_cint_operand (rtx op, enum machine_mode mode)
1321 {
1322   return (GET_CODE (op) == CONST_INT || gpc_reg_operand (op, mode));
1323 }
1324
1325 /* Return 1 is the operand is either a non-special register or ANY
1326    32-bit signed constant integer.  */
1327
1328 int
1329 reg_or_arith_cint_operand (rtx op, enum machine_mode mode)
1330 {
1331   return (gpc_reg_operand (op, mode)
1332           || (GET_CODE (op) == CONST_INT
1333 #if HOST_BITS_PER_WIDE_INT != 32
1334               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80000000)
1335                   < (unsigned HOST_WIDE_INT) 0x100000000ll)
1336 #endif
1337               ));
1338 }
1339
1340 /* Return 1 is the operand is either a non-special register or a 32-bit
1341    signed constant integer valid for 64-bit addition.  */
1342
1343 int
1344 reg_or_add_cint64_operand (rtx op, enum machine_mode mode)
1345 {
1346   return (gpc_reg_operand (op, mode)
1347           || (GET_CODE (op) == CONST_INT
1348 #if HOST_BITS_PER_WIDE_INT == 32
1349               && INTVAL (op) < 0x7fff8000
1350 #else
1351               && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80008000)
1352                   < 0x100000000ll)
1353 #endif
1354               ));
1355 }
1356
1357 /* Return 1 is the operand is either a non-special register or a 32-bit
1358    signed constant integer valid for 64-bit subtraction.  */
1359
1360 int
1361 reg_or_sub_cint64_operand (rtx op, enum machine_mode mode)
1362 {
1363   return (gpc_reg_operand (op, mode)
1364           || (GET_CODE (op) == CONST_INT
1365 #if HOST_BITS_PER_WIDE_INT == 32
1366               && (- INTVAL (op)) < 0x7fff8000
1367 #else
1368               && ((unsigned HOST_WIDE_INT) ((- INTVAL (op)) + 0x80008000)
1369                   < 0x100000000ll)
1370 #endif
1371               ));
1372 }
1373
1374 /* Return 1 is the operand is either a non-special register or ANY
1375    32-bit unsigned constant integer.  */
1376
1377 int
1378 reg_or_logical_cint_operand (rtx op, enum machine_mode mode)
1379 {
1380   if (GET_CODE (op) == CONST_INT)
1381     {
1382       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
1383         {
1384           if (GET_MODE_BITSIZE (mode) <= 32)
1385             abort ();
1386
1387           if (INTVAL (op) < 0)
1388             return 0;
1389         }
1390
1391       return ((INTVAL (op) & GET_MODE_MASK (mode)
1392                & (~ (unsigned HOST_WIDE_INT) 0xffffffff)) == 0);
1393     }
1394   else if (GET_CODE (op) == CONST_DOUBLE)
1395     {
1396       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
1397           || mode != DImode)
1398         abort ();
1399
1400       return CONST_DOUBLE_HIGH (op) == 0;
1401     }
1402   else 
1403     return gpc_reg_operand (op, mode);
1404 }
1405
1406 /* Return 1 if the operand is an operand that can be loaded via the GOT.  */
1407
1408 int
1409 got_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1410 {
1411   return (GET_CODE (op) == SYMBOL_REF
1412           || GET_CODE (op) == CONST
1413           || GET_CODE (op) == LABEL_REF);
1414 }
1415
1416 /* Return 1 if the operand is a simple references that can be loaded via
1417    the GOT (labels involving addition aren't allowed).  */
1418
1419 int
1420 got_no_const_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1421 {
1422   return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF);
1423 }
1424
1425 /* Return the number of instructions it takes to form a constant in an
1426    integer register.  */
1427
1428 static int
1429 num_insns_constant_wide (HOST_WIDE_INT value)
1430 {
1431   /* signed constant loadable with {cal|addi} */
1432   if (CONST_OK_FOR_LETTER_P (value, 'I'))
1433     return 1;
1434
1435   /* constant loadable with {cau|addis} */
1436   else if (CONST_OK_FOR_LETTER_P (value, 'L'))
1437     return 1;
1438
1439 #if HOST_BITS_PER_WIDE_INT == 64
1440   else if (TARGET_POWERPC64)
1441     {
1442       HOST_WIDE_INT low  = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
1443       HOST_WIDE_INT high = value >> 31;
1444
1445       if (high == 0 || high == -1)
1446         return 2;
1447
1448       high >>= 1;
1449
1450       if (low == 0)
1451         return num_insns_constant_wide (high) + 1;
1452       else
1453         return (num_insns_constant_wide (high)
1454                 + num_insns_constant_wide (low) + 1);
1455     }
1456 #endif
1457
1458   else
1459     return 2;
1460 }
1461
1462 int
1463 num_insns_constant (rtx op, enum machine_mode mode)
1464 {
1465   if (GET_CODE (op) == CONST_INT)
1466     {
1467 #if HOST_BITS_PER_WIDE_INT == 64
1468       if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
1469           && mask64_operand (op, mode))
1470             return 2;
1471       else
1472 #endif
1473         return num_insns_constant_wide (INTVAL (op));
1474     }
1475
1476   else if (GET_CODE (op) == CONST_DOUBLE && mode == SFmode)
1477     {
1478       long l;
1479       REAL_VALUE_TYPE rv;
1480
1481       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1482       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1483       return num_insns_constant_wide ((HOST_WIDE_INT) l);
1484     }
1485
1486   else if (GET_CODE (op) == CONST_DOUBLE)
1487     {
1488       HOST_WIDE_INT low;
1489       HOST_WIDE_INT high;
1490       long l[2];
1491       REAL_VALUE_TYPE rv;
1492       int endian = (WORDS_BIG_ENDIAN == 0);
1493
1494       if (mode == VOIDmode || mode == DImode)
1495         {
1496           high = CONST_DOUBLE_HIGH (op);
1497           low  = CONST_DOUBLE_LOW (op);
1498         }
1499       else
1500         {
1501           REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1502           REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
1503           high = l[endian];
1504           low  = l[1 - endian];
1505         }
1506
1507       if (TARGET_32BIT)
1508         return (num_insns_constant_wide (low)
1509                 + num_insns_constant_wide (high));
1510
1511       else
1512         {
1513           if (high == 0 && low >= 0)
1514             return num_insns_constant_wide (low);
1515
1516           else if (high == -1 && low < 0)
1517             return num_insns_constant_wide (low);
1518
1519           else if (mask64_operand (op, mode))
1520             return 2;
1521
1522           else if (low == 0)
1523             return num_insns_constant_wide (high) + 1;
1524
1525           else
1526             return (num_insns_constant_wide (high)
1527                     + num_insns_constant_wide (low) + 1);
1528         }
1529     }
1530
1531   else
1532     abort ();
1533 }
1534
1535 /* Return 1 if the operand is a CONST_DOUBLE and it can be put into a
1536    register with one instruction per word.  We only do this if we can
1537    safely read CONST_DOUBLE_{LOW,HIGH}.  */
1538
1539 int
1540 easy_fp_constant (rtx op, enum machine_mode mode)
1541 {
1542   if (GET_CODE (op) != CONST_DOUBLE
1543       || GET_MODE (op) != mode
1544       || (GET_MODE_CLASS (mode) != MODE_FLOAT && mode != DImode))
1545     return 0;
1546
1547   /* Consider all constants with -msoft-float to be easy.  */
1548   if ((TARGET_SOFT_FLOAT || !TARGET_FPRS)
1549       && mode != DImode)
1550     return 1;
1551
1552   /* If we are using V.4 style PIC, consider all constants to be hard.  */
1553   if (flag_pic && DEFAULT_ABI == ABI_V4)
1554     return 0;
1555
1556 #ifdef TARGET_RELOCATABLE
1557   /* Similarly if we are using -mrelocatable, consider all constants
1558      to be hard.  */
1559   if (TARGET_RELOCATABLE)
1560     return 0;
1561 #endif
1562
1563   if (mode == TFmode)
1564     {
1565       long k[4];
1566       REAL_VALUE_TYPE rv;
1567
1568       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1569       REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
1570
1571       return (num_insns_constant_wide ((HOST_WIDE_INT) k[0]) == 1
1572               && num_insns_constant_wide ((HOST_WIDE_INT) k[1]) == 1
1573               && num_insns_constant_wide ((HOST_WIDE_INT) k[2]) == 1
1574               && num_insns_constant_wide ((HOST_WIDE_INT) k[3]) == 1);
1575     }
1576
1577   else if (mode == DFmode)
1578     {
1579       long k[2];
1580       REAL_VALUE_TYPE rv;
1581
1582       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1583       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
1584
1585       return (num_insns_constant_wide ((HOST_WIDE_INT) k[0]) == 1
1586               && num_insns_constant_wide ((HOST_WIDE_INT) k[1]) == 1);
1587     }
1588
1589   else if (mode == SFmode)
1590     {
1591       long l;
1592       REAL_VALUE_TYPE rv;
1593
1594       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1595       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1596
1597       return num_insns_constant_wide (l) == 1;
1598     }
1599
1600   else if (mode == DImode)
1601     return ((TARGET_POWERPC64
1602              && GET_CODE (op) == CONST_DOUBLE && CONST_DOUBLE_LOW (op) == 0)
1603             || (num_insns_constant (op, DImode) <= 2));
1604
1605   else if (mode == SImode)
1606     return 1;
1607   else
1608     abort ();
1609 }
1610
1611 /* Return nonzero if all elements of a vector have the same value.  */
1612
1613 static int
1614 easy_vector_same (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1615 {
1616   int units, i, cst;
1617
1618   units = CONST_VECTOR_NUNITS (op);
1619
1620   cst = INTVAL (CONST_VECTOR_ELT (op, 0));
1621   for (i = 1; i < units; ++i)
1622     if (INTVAL (CONST_VECTOR_ELT (op, i)) != cst)
1623       break;
1624   if (i == units)
1625     return 1;
1626   return 0;
1627 }
1628
1629 /* Return 1 if the operand is a CONST_INT and can be put into a
1630    register without using memory.  */
1631
1632 int
1633 easy_vector_constant (rtx op, enum machine_mode mode)
1634 {
1635   int cst, cst2;
1636
1637   if (GET_CODE (op) != CONST_VECTOR
1638       || (!TARGET_ALTIVEC
1639           && !TARGET_SPE))
1640     return 0;
1641
1642   if (zero_constant (op, mode)
1643       && ((TARGET_ALTIVEC && ALTIVEC_VECTOR_MODE (mode))
1644           || (TARGET_SPE && SPE_VECTOR_MODE (mode))))
1645     return 1;
1646
1647   if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
1648     return 0;
1649
1650   if (TARGET_SPE && mode == V1DImode)
1651     return 0;
1652
1653   cst  = INTVAL (CONST_VECTOR_ELT (op, 0));
1654   cst2 = INTVAL (CONST_VECTOR_ELT (op, 1));
1655
1656   /* Limit SPE vectors to 15 bits signed.  These we can generate with:
1657        li r0, CONSTANT1
1658        evmergelo r0, r0, r0
1659        li r0, CONSTANT2
1660
1661      I don't know how efficient it would be to allow bigger constants,
1662      considering we'll have an extra 'ori' for every 'li'.  I doubt 5
1663      instructions is better than a 64-bit memory load, but I don't
1664      have the e500 timing specs.  */
1665   if (TARGET_SPE && mode == V2SImode
1666       && cst  >= -0x7fff && cst <= 0x7fff
1667       && cst2 >= -0x7fff && cst2 <= 0x7fff)
1668     return 1;
1669
1670   if (TARGET_ALTIVEC && EASY_VECTOR_15 (cst, op, mode))
1671     return 1;
1672
1673   if (TARGET_ALTIVEC && EASY_VECTOR_15_ADD_SELF (cst, op, mode))
1674     return 1;
1675
1676   return 0;
1677 }
1678
1679 /* Same as easy_vector_constant but only for EASY_VECTOR_15_ADD_SELF.  */
1680
1681 int
1682 easy_vector_constant_add_self (rtx op, enum machine_mode mode)
1683 {
1684   int cst;
1685
1686   if (!easy_vector_constant (op, mode))
1687     return 0;
1688
1689   cst = INTVAL (CONST_VECTOR_ELT (op, 0));
1690
1691   return TARGET_ALTIVEC && EASY_VECTOR_15_ADD_SELF (cst, op, mode);
1692 }
1693
1694 const char *
1695 output_vec_const_move (rtx *operands)
1696 {
1697   int cst, cst2;
1698   enum machine_mode mode;
1699   rtx dest, vec;
1700
1701   dest = operands[0];
1702   vec = operands[1];
1703
1704   cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
1705   cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
1706   mode = GET_MODE (dest);
1707
1708   if (TARGET_ALTIVEC)
1709     {
1710       if (zero_constant (vec, mode))
1711         return "vxor %0,%0,%0";
1712       else if (EASY_VECTOR_15 (cst, vec, mode))
1713         {
1714           operands[1] = GEN_INT (cst);
1715           switch (mode)
1716             {
1717             case V4SImode:
1718               return "vspltisw %0,%1";
1719             case V8HImode:
1720               return "vspltish %0,%1";
1721             case V16QImode:
1722               return "vspltisb %0,%1";
1723             default:
1724               abort ();
1725             }
1726         }
1727       else if (EASY_VECTOR_15_ADD_SELF (cst, vec, mode))
1728         return "#";
1729       else
1730         abort ();
1731     }
1732
1733   if (TARGET_SPE)
1734     {
1735       /* Vector constant 0 is handled as a splitter of V2SI, and in the
1736          pattern of V1DI, V4HI, and V2SF.
1737
1738          FIXME: We should probably return # and add post reload
1739          splitters for these, but this way is so easy ;-).
1740       */
1741       operands[1] = GEN_INT (cst);
1742       operands[2] = GEN_INT (cst2);
1743       if (cst == cst2)
1744         return "li %0,%1\n\tevmergelo %0,%0,%0";
1745       else
1746         return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
1747     }
1748
1749   abort ();
1750 }
1751
1752 /* Return 1 if the operand is the constant 0.  This works for scalars
1753    as well as vectors.  */
1754 int
1755 zero_constant (rtx op, enum machine_mode mode)
1756 {
1757   return op == CONST0_RTX (mode);
1758 }
1759
1760 /* Return 1 if the operand is 0.0.  */
1761 int
1762 zero_fp_constant (rtx op, enum machine_mode mode)
1763 {
1764   return GET_MODE_CLASS (mode) == MODE_FLOAT && op == CONST0_RTX (mode);
1765 }
1766
1767 /* Return 1 if the operand is in volatile memory.  Note that during
1768    the RTL generation phase, memory_operand does not return TRUE for
1769    volatile memory references.  So this function allows us to
1770    recognize volatile references where its safe.  */
1771
1772 int
1773 volatile_mem_operand (rtx op, enum machine_mode mode)
1774 {
1775   if (GET_CODE (op) != MEM)
1776     return 0;
1777
1778   if (!MEM_VOLATILE_P (op))
1779     return 0;
1780
1781   if (mode != GET_MODE (op))
1782     return 0;
1783
1784   if (reload_completed)
1785     return memory_operand (op, mode);
1786
1787   if (reload_in_progress)
1788     return strict_memory_address_p (mode, XEXP (op, 0));
1789
1790   return memory_address_p (mode, XEXP (op, 0));
1791 }
1792
1793 /* Return 1 if the operand is an offsettable memory operand.  */
1794
1795 int
1796 offsettable_mem_operand (rtx op, enum machine_mode mode)
1797 {
1798   return ((GET_CODE (op) == MEM)
1799           && offsettable_address_p (reload_completed || reload_in_progress,
1800                                     mode, XEXP (op, 0)));
1801 }
1802
1803 /* Return 1 if the operand is either an easy FP constant (see above) or
1804    memory.  */
1805
1806 int
1807 mem_or_easy_const_operand (rtx op, enum machine_mode mode)
1808 {
1809   return memory_operand (op, mode) || easy_fp_constant (op, mode);
1810 }
1811
1812 /* Return 1 if the operand is either a non-special register or an item
1813    that can be used as the operand of a `mode' add insn.  */
1814
1815 int
1816 add_operand (rtx op, enum machine_mode mode)
1817 {
1818   if (GET_CODE (op) == CONST_INT)
1819     return (CONST_OK_FOR_LETTER_P (INTVAL (op), 'I')
1820             || CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
1821
1822   return gpc_reg_operand (op, mode);
1823 }
1824
1825 /* Return 1 if OP is a constant but not a valid add_operand.  */
1826
1827 int
1828 non_add_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1829 {
1830   return (GET_CODE (op) == CONST_INT
1831           && !CONST_OK_FOR_LETTER_P (INTVAL (op), 'I')
1832           && !CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
1833 }
1834
1835 /* Return 1 if the operand is a non-special register or a constant that
1836    can be used as the operand of an OR or XOR insn on the RS/6000.  */
1837
1838 int
1839 logical_operand (rtx op, enum machine_mode mode)
1840 {
1841   HOST_WIDE_INT opl, oph;
1842
1843   if (gpc_reg_operand (op, mode))
1844     return 1;
1845
1846   if (GET_CODE (op) == CONST_INT)
1847     {
1848       opl = INTVAL (op) & GET_MODE_MASK (mode);
1849
1850 #if HOST_BITS_PER_WIDE_INT <= 32
1851       if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT && opl < 0)
1852         return 0;
1853 #endif
1854     }
1855   else if (GET_CODE (op) == CONST_DOUBLE)
1856     {
1857       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
1858         abort ();
1859
1860       opl = CONST_DOUBLE_LOW (op);
1861       oph = CONST_DOUBLE_HIGH (op);
1862       if (oph != 0)
1863         return 0;
1864     }
1865   else
1866     return 0;
1867
1868   return ((opl & ~ (unsigned HOST_WIDE_INT) 0xffff) == 0
1869           || (opl & ~ (unsigned HOST_WIDE_INT) 0xffff0000) == 0);
1870 }
1871
1872 /* Return 1 if C is a constant that is not a logical operand (as
1873    above), but could be split into one.  */
1874
1875 int
1876 non_logical_cint_operand (rtx op, enum machine_mode mode)
1877 {
1878   return ((GET_CODE (op) == CONST_INT || GET_CODE (op) == CONST_DOUBLE)
1879           && ! logical_operand (op, mode)
1880           && reg_or_logical_cint_operand (op, mode));
1881 }
1882
1883 /* Return 1 if C is a constant that can be encoded in a 32-bit mask on the
1884    RS/6000.  It is if there are no more than two 1->0 or 0->1 transitions.
1885    Reject all ones and all zeros, since these should have been optimized
1886    away and confuse the making of MB and ME.  */
1887
1888 int
1889 mask_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1890 {
1891   HOST_WIDE_INT c, lsb;
1892
1893   if (GET_CODE (op) != CONST_INT)
1894     return 0;
1895
1896   c = INTVAL (op);
1897
1898   /* Fail in 64-bit mode if the mask wraps around because the upper
1899      32-bits of the mask will all be 1s, contrary to GCC's internal view.  */
1900   if (TARGET_POWERPC64 && (c & 0x80000001) == 0x80000001)
1901     return 0;
1902
1903   /* We don't change the number of transitions by inverting,
1904      so make sure we start with the LS bit zero.  */
1905   if (c & 1)
1906     c = ~c;
1907
1908   /* Reject all zeros or all ones.  */
1909   if (c == 0)
1910     return 0;
1911
1912   /* Find the first transition.  */
1913   lsb = c & -c;
1914
1915   /* Invert to look for a second transition.  */
1916   c = ~c;
1917
1918   /* Erase first transition.  */
1919   c &= -lsb;
1920
1921   /* Find the second transition (if any).  */
1922   lsb = c & -c;
1923
1924   /* Match if all the bits above are 1's (or c is zero).  */
1925   return c == -lsb;
1926 }
1927
1928 /* Return 1 for the PowerPC64 rlwinm corner case.  */
1929
1930 int
1931 mask_operand_wrap (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1932 {
1933   HOST_WIDE_INT c, lsb;
1934
1935   if (GET_CODE (op) != CONST_INT)
1936     return 0;
1937
1938   c = INTVAL (op);
1939
1940   if ((c & 0x80000001) != 0x80000001)
1941     return 0;
1942
1943   c = ~c;
1944   if (c == 0)
1945     return 0;
1946
1947   lsb = c & -c;
1948   c = ~c;
1949   c &= -lsb;
1950   lsb = c & -c;
1951   return c == -lsb;
1952 }
1953
1954 /* Return 1 if the operand is a constant that is a PowerPC64 mask.
1955    It is if there are no more than one 1->0 or 0->1 transitions.
1956    Reject all zeros, since zero should have been optimized away and
1957    confuses the making of MB and ME.  */
1958
1959 int
1960 mask64_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1961 {
1962   if (GET_CODE (op) == CONST_INT)
1963     {
1964       HOST_WIDE_INT c, lsb;
1965
1966       c = INTVAL (op);
1967
1968       /* Reject all zeros.  */
1969       if (c == 0)
1970         return 0;
1971
1972       /* We don't change the number of transitions by inverting,
1973          so make sure we start with the LS bit zero.  */
1974       if (c & 1)
1975         c = ~c;
1976
1977       /* Find the transition, and check that all bits above are 1's.  */
1978       lsb = c & -c;
1979
1980       /* Match if all the bits above are 1's (or c is zero).  */
1981       return c == -lsb;
1982     }
1983   return 0;
1984 }
1985
1986 /* Like mask64_operand, but allow up to three transitions.  This
1987    predicate is used by insn patterns that generate two rldicl or
1988    rldicr machine insns.  */
1989
1990 int
1991 mask64_2_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1992 {
1993   if (GET_CODE (op) == CONST_INT)
1994     {
1995       HOST_WIDE_INT c, lsb;
1996
1997       c = INTVAL (op);
1998
1999       /* Disallow all zeros.  */
2000       if (c == 0)
2001         return 0;
2002
2003       /* We don't change the number of transitions by inverting,
2004          so make sure we start with the LS bit zero.  */
2005       if (c & 1)
2006         c = ~c;
2007
2008       /* Find the first transition.  */
2009       lsb = c & -c;
2010
2011       /* Invert to look for a second transition.  */
2012       c = ~c;
2013
2014       /* Erase first transition.  */
2015       c &= -lsb;
2016
2017       /* Find the second transition.  */
2018       lsb = c & -c;
2019
2020       /* Invert to look for a third transition.  */
2021       c = ~c;
2022
2023       /* Erase second transition.  */
2024       c &= -lsb;
2025
2026       /* Find the third transition (if any).  */
2027       lsb = c & -c;
2028
2029       /* Match if all the bits above are 1's (or c is zero).  */
2030       return c == -lsb;
2031     }
2032   return 0;
2033 }
2034
2035 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
2036    implement ANDing by the mask IN.  */
2037 void
2038 build_mask64_2_operands (rtx in, rtx *out)
2039 {
2040 #if HOST_BITS_PER_WIDE_INT >= 64
2041   unsigned HOST_WIDE_INT c, lsb, m1, m2;
2042   int shift;
2043
2044   if (GET_CODE (in) != CONST_INT)
2045     abort ();
2046
2047   c = INTVAL (in);
2048   if (c & 1)
2049     {
2050       /* Assume c initially something like 0x00fff000000fffff.  The idea
2051          is to rotate the word so that the middle ^^^^^^ group of zeros
2052          is at the MS end and can be cleared with an rldicl mask.  We then
2053          rotate back and clear off the MS    ^^ group of zeros with a
2054          second rldicl.  */
2055       c = ~c;                   /*   c == 0xff000ffffff00000 */
2056       lsb = c & -c;             /* lsb == 0x0000000000100000 */
2057       m1 = -lsb;                /*  m1 == 0xfffffffffff00000 */
2058       c = ~c;                   /*   c == 0x00fff000000fffff */
2059       c &= -lsb;                /*   c == 0x00fff00000000000 */
2060       lsb = c & -c;             /* lsb == 0x0000100000000000 */
2061       c = ~c;                   /*   c == 0xff000fffffffffff */
2062       c &= -lsb;                /*   c == 0xff00000000000000 */
2063       shift = 0;
2064       while ((lsb >>= 1) != 0)
2065         shift++;                /* shift == 44 on exit from loop */
2066       m1 <<= 64 - shift;        /*  m1 == 0xffffff0000000000 */
2067       m1 = ~m1;                 /*  m1 == 0x000000ffffffffff */
2068       m2 = ~c;                  /*  m2 == 0x00ffffffffffffff */
2069     }
2070   else
2071     {
2072       /* Assume c initially something like 0xff000f0000000000.  The idea
2073          is to rotate the word so that the     ^^^  middle group of zeros
2074          is at the LS end and can be cleared with an rldicr mask.  We then
2075          rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
2076          a second rldicr.  */
2077       lsb = c & -c;             /* lsb == 0x0000010000000000 */
2078       m2 = -lsb;                /*  m2 == 0xffffff0000000000 */
2079       c = ~c;                   /*   c == 0x00fff0ffffffffff */
2080       c &= -lsb;                /*   c == 0x00fff00000000000 */
2081       lsb = c & -c;             /* lsb == 0x0000100000000000 */
2082       c = ~c;                   /*   c == 0xff000fffffffffff */
2083       c &= -lsb;                /*   c == 0xff00000000000000 */
2084       shift = 0;
2085       while ((lsb >>= 1) != 0)
2086         shift++;                /* shift == 44 on exit from loop */
2087       m1 = ~c;                  /*  m1 == 0x00ffffffffffffff */
2088       m1 >>= shift;             /*  m1 == 0x0000000000000fff */
2089       m1 = ~m1;                 /*  m1 == 0xfffffffffffff000 */
2090     }
2091
2092   /* Note that when we only have two 0->1 and 1->0 transitions, one of the
2093      masks will be all 1's.  We are guaranteed more than one transition.  */
2094   out[0] = GEN_INT (64 - shift);
2095   out[1] = GEN_INT (m1);
2096   out[2] = GEN_INT (shift);
2097   out[3] = GEN_INT (m2);
2098 #else
2099   (void)in;
2100   (void)out;
2101   abort ();
2102 #endif
2103 }
2104
2105 /* Return 1 if the operand is either a non-special register or a constant
2106    that can be used as the operand of a PowerPC64 logical AND insn.  */
2107
2108 int
2109 and64_operand (rtx op, enum machine_mode mode)
2110 {
2111   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
2112     return (gpc_reg_operand (op, mode) || mask64_operand (op, mode));
2113
2114   return (logical_operand (op, mode) || mask64_operand (op, mode));
2115 }
2116
2117 /* Like the above, but also match constants that can be implemented
2118    with two rldicl or rldicr insns.  */
2119
2120 int
2121 and64_2_operand (rtx op, enum machine_mode mode)
2122 {
2123   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis. */
2124     return gpc_reg_operand (op, mode) || mask64_2_operand (op, mode);
2125
2126   return logical_operand (op, mode) || mask64_2_operand (op, mode);
2127 }
2128
2129 /* Return 1 if the operand is either a non-special register or a
2130    constant that can be used as the operand of an RS/6000 logical AND insn.  */
2131
2132 int
2133 and_operand (rtx op, enum machine_mode mode)
2134 {
2135   if (fixed_regs[CR0_REGNO])    /* CR0 not available, don't do andi./andis.  */
2136     return (gpc_reg_operand (op, mode) || mask_operand (op, mode));
2137
2138   return (logical_operand (op, mode) || mask_operand (op, mode));
2139 }
2140
2141 /* Return 1 if the operand is a general register or memory operand.  */
2142
2143 int
2144 reg_or_mem_operand (rtx op, enum machine_mode mode)
2145 {
2146   return (gpc_reg_operand (op, mode)
2147           || memory_operand (op, mode)
2148           || volatile_mem_operand (op, mode));
2149 }
2150
2151 /* Return 1 if the operand is a general register or memory operand without
2152    pre_inc or pre_dec which produces invalid form of PowerPC lwa
2153    instruction.  */
2154
2155 int
2156 lwa_operand (rtx op, enum machine_mode mode)
2157 {
2158   rtx inner = op;
2159
2160   if (reload_completed && GET_CODE (inner) == SUBREG)
2161     inner = SUBREG_REG (inner);
2162     
2163   return gpc_reg_operand (inner, mode)
2164     || (memory_operand (inner, mode)
2165         && GET_CODE (XEXP (inner, 0)) != PRE_INC
2166         && GET_CODE (XEXP (inner, 0)) != PRE_DEC
2167         && (GET_CODE (XEXP (inner, 0)) != PLUS
2168             || GET_CODE (XEXP (XEXP (inner, 0), 1)) != CONST_INT
2169             || INTVAL (XEXP (XEXP (inner, 0), 1)) % 4 == 0));
2170 }
2171
2172 /* Return 1 if the operand, used inside a MEM, is a SYMBOL_REF.  */
2173
2174 int
2175 symbol_ref_operand (rtx op, enum machine_mode mode)
2176 {
2177   if (mode != VOIDmode && GET_MODE (op) != mode)
2178     return 0;
2179
2180   return (GET_CODE (op) == SYMBOL_REF
2181           && (DEFAULT_ABI != ABI_AIX || SYMBOL_REF_FUNCTION_P (op)));
2182 }
2183
2184 /* Return 1 if the operand, used inside a MEM, is a valid first argument
2185    to CALL.  This is a SYMBOL_REF, a pseudo-register, LR or CTR.  */
2186
2187 int
2188 call_operand (rtx op, enum machine_mode mode)
2189 {
2190   if (mode != VOIDmode && GET_MODE (op) != mode)
2191     return 0;
2192
2193   return (GET_CODE (op) == SYMBOL_REF
2194           || (GET_CODE (op) == REG
2195               && (REGNO (op) == LINK_REGISTER_REGNUM
2196                   || REGNO (op) == COUNT_REGISTER_REGNUM
2197                   || REGNO (op) >= FIRST_PSEUDO_REGISTER)));
2198 }
2199
2200 /* Return 1 if the operand is a SYMBOL_REF for a function known to be in
2201    this file.  */
2202
2203 int
2204 current_file_function_operand (rtx op, 
2205                               enum machine_mode mode ATTRIBUTE_UNUSED)
2206 {
2207   return (GET_CODE (op) == SYMBOL_REF
2208           && (DEFAULT_ABI != ABI_AIX || SYMBOL_REF_FUNCTION_P (op))
2209           && (SYMBOL_REF_LOCAL_P (op)
2210               || (op == XEXP (DECL_RTL (current_function_decl), 0))));
2211 }
2212
2213 /* Return 1 if this operand is a valid input for a move insn.  */
2214
2215 int
2216 input_operand (rtx op, enum machine_mode mode)
2217 {
2218   /* Memory is always valid.  */
2219   if (memory_operand (op, mode))
2220     return 1;
2221
2222   /* Only a tiny bit of handling for CONSTANT_P_RTX is necessary.  */
2223   if (GET_CODE (op) == CONSTANT_P_RTX)
2224     return 1;
2225
2226   /* For floating-point, easy constants are valid.  */
2227   if (GET_MODE_CLASS (mode) == MODE_FLOAT
2228       && CONSTANT_P (op)
2229       && easy_fp_constant (op, mode))
2230     return 1;
2231
2232   /* Allow any integer constant.  */
2233   if (GET_MODE_CLASS (mode) == MODE_INT
2234       && (GET_CODE (op) == CONST_INT
2235           || GET_CODE (op) == CONST_DOUBLE))
2236     return 1;
2237
2238   /* Allow easy vector constants.  */
2239   if (GET_CODE (op) == CONST_VECTOR
2240       && easy_vector_constant (op, mode))
2241     return 1;
2242
2243   /* For floating-point or multi-word mode, the only remaining valid type
2244      is a register.  */
2245   if (GET_MODE_CLASS (mode) == MODE_FLOAT
2246       || GET_MODE_SIZE (mode) > UNITS_PER_WORD)
2247     return register_operand (op, mode);
2248
2249   /* The only cases left are integral modes one word or smaller (we
2250      do not get called for MODE_CC values).  These can be in any
2251      register.  */
2252   if (register_operand (op, mode))
2253     return 1;
2254
2255   /* A SYMBOL_REF referring to the TOC is valid.  */
2256   if (legitimate_constant_pool_address_p (op))
2257     return 1;
2258
2259   /* A constant pool expression (relative to the TOC) is valid */
2260   if (toc_relative_expr_p (op))
2261     return 1;
2262
2263   /* V.4 allows SYMBOL_REFs and CONSTs that are in the small data region
2264      to be valid.  */
2265   if (DEFAULT_ABI == ABI_V4
2266       && (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST)
2267       && small_data_operand (op, Pmode))
2268     return 1;
2269
2270   return 0;
2271 }
2272
2273 /* Return 1 for an operand in small memory on V.4/eabi.  */
2274
2275 int
2276 small_data_operand (rtx op ATTRIBUTE_UNUSED, 
2277                     enum machine_mode mode ATTRIBUTE_UNUSED)
2278 {
2279 #if TARGET_ELF
2280   rtx sym_ref;
2281
2282   if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
2283     return 0;
2284
2285   if (DEFAULT_ABI != ABI_V4)
2286     return 0;
2287
2288   if (GET_CODE (op) == SYMBOL_REF)
2289     sym_ref = op;
2290
2291   else if (GET_CODE (op) != CONST
2292            || GET_CODE (XEXP (op, 0)) != PLUS
2293            || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
2294            || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
2295     return 0;
2296
2297   else
2298     {
2299       rtx sum = XEXP (op, 0);
2300       HOST_WIDE_INT summand;
2301
2302       /* We have to be careful here, because it is the referenced address
2303         that must be 32k from _SDA_BASE_, not just the symbol.  */
2304       summand = INTVAL (XEXP (sum, 1));
2305       if (summand < 0 || (unsigned HOST_WIDE_INT) summand > g_switch_value)
2306        return 0;
2307
2308       sym_ref = XEXP (sum, 0);
2309     }
2310
2311   return SYMBOL_REF_SMALL_P (sym_ref);
2312 #else
2313   return 0;
2314 #endif
2315 }
2316
2317 /* Return true if either operand is a general purpose register.  */
2318
2319 bool
2320 gpr_or_gpr_p (rtx op0, rtx op1)
2321 {
2322   return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
2323           || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
2324 }
2325
2326 \f
2327 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address.  */
2328
2329 static int 
2330 constant_pool_expr_1 (rtx op, int *have_sym, int *have_toc) 
2331 {
2332   switch (GET_CODE(op)) 
2333     {
2334     case SYMBOL_REF:
2335       if (RS6000_SYMBOL_REF_TLS_P (op))
2336         return 0;
2337       else if (CONSTANT_POOL_ADDRESS_P (op))
2338         {
2339           if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (op), Pmode))
2340             {
2341               *have_sym = 1;
2342               return 1;
2343             }
2344           else
2345             return 0;
2346         }
2347       else if (! strcmp (XSTR (op, 0), toc_label_name))
2348         {
2349           *have_toc = 1;
2350           return 1;
2351         }
2352       else
2353         return 0;
2354     case PLUS:
2355     case MINUS:
2356       return (constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc)
2357               && constant_pool_expr_1 (XEXP (op, 1), have_sym, have_toc));
2358     case CONST:
2359       return constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc);
2360     case CONST_INT:
2361       return 1;
2362     default:
2363       return 0;
2364     }
2365 }
2366
2367 static bool
2368 constant_pool_expr_p (rtx op)
2369 {
2370   int have_sym = 0;
2371   int have_toc = 0;
2372   return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_sym;
2373 }
2374
2375 static bool
2376 toc_relative_expr_p (rtx op)
2377 {
2378   int have_sym = 0;
2379   int have_toc = 0;
2380   return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_toc;
2381 }
2382
2383 /* SPE offset addressing is limited to 5-bits worth of double words.  */
2384 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
2385
2386 bool
2387 legitimate_constant_pool_address_p (rtx x)
2388 {
2389   return (TARGET_TOC
2390           && GET_CODE (x) == PLUS
2391           && GET_CODE (XEXP (x, 0)) == REG
2392           && (TARGET_MINIMAL_TOC || REGNO (XEXP (x, 0)) == TOC_REGISTER)
2393           && constant_pool_expr_p (XEXP (x, 1)));
2394 }
2395
2396 static bool
2397 legitimate_small_data_p (enum machine_mode mode, rtx x)
2398 {
2399   return (DEFAULT_ABI == ABI_V4
2400           && !flag_pic && !TARGET_TOC
2401           && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
2402           && small_data_operand (x, mode));
2403 }
2404
2405 static bool
2406 legitimate_offset_address_p (enum machine_mode mode, rtx x, int strict)
2407 {
2408   unsigned HOST_WIDE_INT offset, extra;
2409
2410   if (GET_CODE (x) != PLUS)
2411     return false;
2412   if (GET_CODE (XEXP (x, 0)) != REG)
2413     return false;
2414   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
2415     return false;
2416   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
2417     return false;
2418
2419   offset = INTVAL (XEXP (x, 1));
2420   extra = 0;
2421   switch (mode)
2422     {
2423     case V16QImode:
2424     case V8HImode:
2425     case V4SFmode:
2426     case V4SImode:
2427       /* AltiVec vector modes.  Only reg+reg addressing is valid here,
2428          which leaves the only valid constant offset of zero, which by
2429          canonicalization rules is also invalid.  */
2430       return false;
2431
2432     case V4HImode:
2433     case V2SImode:
2434     case V1DImode:
2435     case V2SFmode:
2436       /* SPE vector modes.  */
2437       return SPE_CONST_OFFSET_OK (offset);
2438
2439     case DFmode:
2440     case DImode:
2441       if (TARGET_32BIT)
2442         extra = 4;
2443       else if (offset & 3)
2444         return false;
2445       break;
2446
2447     case TFmode:
2448     case TImode:
2449       if (TARGET_32BIT)
2450         extra = 12;
2451       else if (offset & 3)
2452         return false;
2453       else
2454         extra = 8;
2455       break;
2456
2457     default:
2458       break;
2459     }
2460
2461   return (offset + extra >= offset) && (offset + extra + 0x8000 < 0x10000);
2462 }
2463
2464 static bool
2465 legitimate_indexed_address_p (rtx x, int strict)
2466 {
2467   rtx op0, op1;
2468
2469   if (GET_CODE (x) != PLUS)
2470     return false;
2471   op0 = XEXP (x, 0);
2472   op1 = XEXP (x, 1);
2473
2474   if (!REG_P (op0) || !REG_P (op1))
2475     return false;
2476
2477   return ((INT_REG_OK_FOR_BASE_P (op0, strict)
2478            && INT_REG_OK_FOR_INDEX_P (op1, strict))
2479           || (INT_REG_OK_FOR_BASE_P (op1, strict)
2480               && INT_REG_OK_FOR_INDEX_P (op0, strict)));
2481 }
2482
2483 static inline bool
2484 legitimate_indirect_address_p (rtx x, int strict)
2485 {
2486   return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
2487 }
2488
2489 static bool
2490 legitimate_lo_sum_address_p (enum machine_mode mode, rtx x, int strict)
2491 {
2492   if (GET_CODE (x) != LO_SUM)
2493     return false;
2494   if (GET_CODE (XEXP (x, 0)) != REG)
2495     return false;
2496   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
2497     return false;
2498   x = XEXP (x, 1);
2499
2500   if (TARGET_ELF)
2501     {
2502       if (DEFAULT_ABI != ABI_AIX && flag_pic)
2503         return false;
2504       if (TARGET_TOC)
2505         return false;
2506       if (GET_MODE_NUNITS (mode) != 1)
2507         return false;
2508       if (GET_MODE_BITSIZE (mode) > 32
2509           && !(TARGET_HARD_FLOAT && TARGET_FPRS && mode == DFmode))
2510         return false;
2511
2512       return CONSTANT_P (x);
2513     }
2514
2515   return false;
2516 }
2517
2518
2519 /* Try machine-dependent ways of modifying an illegitimate address
2520    to be legitimate.  If we find one, return the new, valid address.
2521    This is used from only one place: `memory_address' in explow.c.
2522
2523    OLDX is the address as it was before break_out_memory_refs was
2524    called.  In some cases it is useful to look at this to decide what
2525    needs to be done.
2526
2527    MODE is passed so that this function can use GO_IF_LEGITIMATE_ADDRESS.
2528
2529    It is always safe for this function to do nothing.  It exists to
2530    recognize opportunities to optimize the output.
2531
2532    On RS/6000, first check for the sum of a register with a constant
2533    integer that is out of range.  If so, generate code to add the
2534    constant with the low-order 16 bits masked to the register and force
2535    this result into another register (this can be done with `cau').
2536    Then generate an address of REG+(CONST&0xffff), allowing for the
2537    possibility of bit 16 being a one.
2538
2539    Then check for the sum of a register and something not constant, try to
2540    load the other things into a register and return the sum.  */
2541
2542 rtx
2543 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
2544                            enum machine_mode mode)
2545 {
2546   if (GET_CODE (x) == SYMBOL_REF)
2547     {
2548       enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
2549       if (model != 0)
2550         return rs6000_legitimize_tls_address (x, model);
2551     }
2552
2553   if (GET_CODE (x) == PLUS 
2554       && GET_CODE (XEXP (x, 0)) == REG
2555       && GET_CODE (XEXP (x, 1)) == CONST_INT
2556       && (unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000) >= 0x10000)
2557     { 
2558       HOST_WIDE_INT high_int, low_int;
2559       rtx sum;
2560       low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
2561       high_int = INTVAL (XEXP (x, 1)) - low_int;
2562       sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
2563                                          GEN_INT (high_int)), 0);
2564       return gen_rtx_PLUS (Pmode, sum, GEN_INT (low_int));
2565     }
2566   else if (GET_CODE (x) == PLUS 
2567            && GET_CODE (XEXP (x, 0)) == REG
2568            && GET_CODE (XEXP (x, 1)) != CONST_INT
2569            && GET_MODE_NUNITS (mode) == 1
2570            && ((TARGET_HARD_FLOAT && TARGET_FPRS)
2571                || TARGET_POWERPC64
2572                || (mode != DFmode && mode != TFmode))
2573            && (TARGET_POWERPC64 || mode != DImode)
2574            && mode != TImode)
2575     {
2576       return gen_rtx_PLUS (Pmode, XEXP (x, 0),
2577                            force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
2578     }
2579   else if (ALTIVEC_VECTOR_MODE (mode))
2580     {
2581       rtx reg;
2582
2583       /* Make sure both operands are registers.  */
2584       if (GET_CODE (x) == PLUS)
2585         return gen_rtx_PLUS (Pmode, force_reg (Pmode, XEXP (x, 0)),
2586                              force_reg (Pmode, XEXP (x, 1)));
2587
2588       reg = force_reg (Pmode, x);
2589       return reg;
2590     }
2591   else if (SPE_VECTOR_MODE (mode))
2592     {
2593       /* We accept [reg + reg] and [reg + OFFSET].  */
2594
2595       if (GET_CODE (x) == PLUS)
2596       {
2597         rtx op1 = XEXP (x, 0);
2598         rtx op2 = XEXP (x, 1);
2599
2600         op1 = force_reg (Pmode, op1);
2601
2602         if (GET_CODE (op2) != REG
2603             && (GET_CODE (op2) != CONST_INT
2604                 || !SPE_CONST_OFFSET_OK (INTVAL (op2))))
2605           op2 = force_reg (Pmode, op2);
2606
2607         return gen_rtx_PLUS (Pmode, op1, op2);
2608       }
2609
2610       return force_reg (Pmode, x);
2611     }
2612   else if (TARGET_ELF
2613            && TARGET_32BIT
2614            && TARGET_NO_TOC
2615            && ! flag_pic
2616            && GET_CODE (x) != CONST_INT
2617            && GET_CODE (x) != CONST_DOUBLE 
2618            && CONSTANT_P (x)
2619            && GET_MODE_NUNITS (mode) == 1
2620            && (GET_MODE_BITSIZE (mode) <= 32
2621                || ((TARGET_HARD_FLOAT && TARGET_FPRS) && mode == DFmode)))
2622     {
2623       rtx reg = gen_reg_rtx (Pmode);
2624       emit_insn (gen_elf_high (reg, (x)));
2625       return gen_rtx_LO_SUM (Pmode, reg, (x));
2626     }
2627   else if (TARGET_MACHO && TARGET_32BIT && TARGET_NO_TOC
2628            && ! flag_pic
2629 #if TARGET_MACHO
2630            && ! MACHO_DYNAMIC_NO_PIC_P
2631 #endif
2632            && GET_CODE (x) != CONST_INT
2633            && GET_CODE (x) != CONST_DOUBLE 
2634            && CONSTANT_P (x)
2635            && ((TARGET_HARD_FLOAT && TARGET_FPRS) || mode != DFmode)
2636            && mode != DImode 
2637            && mode != TImode)
2638     {
2639       rtx reg = gen_reg_rtx (Pmode);
2640       emit_insn (gen_macho_high (reg, (x)));
2641       return gen_rtx_LO_SUM (Pmode, reg, (x));
2642     }
2643   else if (TARGET_TOC 
2644            && constant_pool_expr_p (x)
2645            && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
2646     {
2647       return create_TOC_reference (x);
2648     }
2649   else
2650     return NULL_RTX;
2651 }
2652
2653 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
2654
2655 static GTY(()) rtx rs6000_tls_symbol;
2656 static rtx
2657 rs6000_tls_get_addr (void)
2658 {
2659   if (!rs6000_tls_symbol)
2660     rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
2661
2662   return rs6000_tls_symbol;
2663 }
2664
2665 /* Construct the SYMBOL_REF for TLS GOT references.  */
2666
2667 static GTY(()) rtx rs6000_got_symbol;
2668 static rtx
2669 rs6000_got_sym (void)
2670 {
2671   if (!rs6000_got_symbol)
2672     {
2673       rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
2674       SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
2675       SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
2676     }     
2677
2678   return rs6000_got_symbol;
2679 }
2680
2681 /* ADDR contains a thread-local SYMBOL_REF.  Generate code to compute
2682    this (thread-local) address.  */
2683
2684 static rtx
2685 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
2686 {
2687   rtx dest, insn;
2688
2689   dest = gen_reg_rtx (Pmode);
2690   if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
2691     {
2692       rtx tlsreg;
2693
2694       if (TARGET_64BIT)
2695         {
2696           tlsreg = gen_rtx_REG (Pmode, 13);
2697           insn = gen_tls_tprel_64 (dest, tlsreg, addr);
2698         }
2699       else
2700         {
2701           tlsreg = gen_rtx_REG (Pmode, 2);
2702           insn = gen_tls_tprel_32 (dest, tlsreg, addr);
2703         }
2704       emit_insn (insn);
2705     }
2706   else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
2707     {
2708       rtx tlsreg, tmp;
2709
2710       tmp = gen_reg_rtx (Pmode);
2711       if (TARGET_64BIT)
2712         {
2713           tlsreg = gen_rtx_REG (Pmode, 13);
2714           insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
2715         }
2716       else
2717         {
2718           tlsreg = gen_rtx_REG (Pmode, 2);
2719           insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
2720         }
2721       emit_insn (insn);
2722       if (TARGET_64BIT)
2723         insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
2724       else
2725         insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
2726       emit_insn (insn);
2727     }
2728   else
2729     {
2730       rtx r3, got, tga, tmp1, tmp2, eqv;
2731
2732       if (TARGET_64BIT)
2733         got = gen_rtx_REG (Pmode, TOC_REGISTER);
2734       else
2735         {
2736           if (flag_pic == 1)
2737             got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
2738           else
2739             {
2740               rtx gsym = rs6000_got_sym ();
2741               got = gen_reg_rtx (Pmode);
2742               if (flag_pic == 0)
2743                 rs6000_emit_move (got, gsym, Pmode);
2744               else
2745                 {
2746                   char buf[30];
2747                   static int tls_got_labelno = 0;
2748                   rtx tempLR, lab, tmp3, mem;
2749                   rtx first, last;
2750
2751                   ASM_GENERATE_INTERNAL_LABEL (buf, "LTLS", tls_got_labelno++);
2752                   lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
2753                   tempLR = gen_reg_rtx (Pmode);
2754                   tmp1 = gen_reg_rtx (Pmode);
2755                   tmp2 = gen_reg_rtx (Pmode);
2756                   tmp3 = gen_reg_rtx (Pmode);
2757                   mem = gen_rtx_MEM (Pmode, tmp1);
2758                   RTX_UNCHANGING_P (mem) = 1;
2759
2760                   first = emit_insn (gen_load_toc_v4_PIC_1b (tempLR, lab,
2761                                                              gsym));
2762                   emit_move_insn (tmp1, tempLR);
2763                   emit_move_insn (tmp2, mem);
2764                   emit_insn (gen_addsi3 (tmp3, tmp1, tmp2));
2765                   last = emit_move_insn (got, tmp3);
2766                   REG_NOTES (last) = gen_rtx_EXPR_LIST (REG_EQUAL, gsym,
2767                                                         REG_NOTES (last));
2768                   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last,
2769                                                          REG_NOTES (first));
2770                   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first,
2771                                                         REG_NOTES (last));
2772                 }
2773             }
2774         }
2775
2776       if (model == TLS_MODEL_GLOBAL_DYNAMIC)
2777         {
2778           r3 = gen_rtx_REG (Pmode, 3);
2779           if (TARGET_64BIT)
2780             insn = gen_tls_gd_64 (r3, got, addr);
2781           else
2782             insn = gen_tls_gd_32 (r3, got, addr);
2783           start_sequence ();
2784           emit_insn (insn);
2785           tga = gen_rtx_MEM (Pmode, rs6000_tls_get_addr ());
2786           insn = gen_call_value (r3, tga, const0_rtx, const0_rtx);
2787           insn = emit_call_insn (insn);
2788           CONST_OR_PURE_CALL_P (insn) = 1;
2789           use_reg (&CALL_INSN_FUNCTION_USAGE (insn), r3);
2790           insn = get_insns ();
2791           end_sequence ();
2792           emit_libcall_block (insn, dest, r3, addr);
2793         }
2794       else if (model == TLS_MODEL_LOCAL_DYNAMIC)
2795         {
2796           r3 = gen_rtx_REG (Pmode, 3);
2797           if (TARGET_64BIT)
2798             insn = gen_tls_ld_64 (r3, got);
2799           else
2800             insn = gen_tls_ld_32 (r3, got);
2801           start_sequence ();
2802           emit_insn (insn);
2803           tga = gen_rtx_MEM (Pmode, rs6000_tls_get_addr ());
2804           insn = gen_call_value (r3, tga, const0_rtx, const0_rtx);
2805           insn = emit_call_insn (insn);
2806           CONST_OR_PURE_CALL_P (insn) = 1;
2807           use_reg (&CALL_INSN_FUNCTION_USAGE (insn), r3);
2808           insn = get_insns ();
2809           end_sequence ();
2810           tmp1 = gen_reg_rtx (Pmode);
2811           eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
2812                                 UNSPEC_TLSLD);
2813           emit_libcall_block (insn, tmp1, r3, eqv);
2814           if (rs6000_tls_size == 16)
2815             {
2816               if (TARGET_64BIT)
2817                 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
2818               else
2819                 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
2820             }
2821           else if (rs6000_tls_size == 32)
2822             {
2823               tmp2 = gen_reg_rtx (Pmode);
2824               if (TARGET_64BIT)
2825                 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
2826               else
2827                 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
2828               emit_insn (insn);
2829               if (TARGET_64BIT)
2830                 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
2831               else
2832                 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
2833             }
2834           else
2835             {
2836               tmp2 = gen_reg_rtx (Pmode);
2837               if (TARGET_64BIT)
2838                 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
2839               else
2840                 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
2841               emit_insn (insn);
2842               insn = gen_rtx_SET (Pmode, dest,
2843                                   gen_rtx_PLUS (Pmode, tmp2, tmp1));
2844             }
2845           emit_insn (insn);
2846         }
2847       else
2848         {
2849           /* IE, or 64 bit offset LE.  */
2850           tmp2 = gen_reg_rtx (Pmode);
2851           if (TARGET_64BIT)
2852             insn = gen_tls_got_tprel_64 (tmp2, got, addr);
2853           else
2854             insn = gen_tls_got_tprel_32 (tmp2, got, addr);
2855           emit_insn (insn);
2856           if (TARGET_64BIT)
2857             insn = gen_tls_tls_64 (dest, tmp2, addr);
2858           else
2859             insn = gen_tls_tls_32 (dest, tmp2, addr);
2860           emit_insn (insn);
2861         }
2862     }
2863
2864   return dest;
2865 }
2866
2867 /* Return 1 if X is a SYMBOL_REF for a TLS symbol.  This is used in
2868    instruction definitions.  */
2869
2870 int
2871 rs6000_tls_symbol_ref (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
2872 {
2873   return RS6000_SYMBOL_REF_TLS_P (x);
2874 }
2875
2876 /* Return 1 if X contains a thread-local symbol.  */
2877
2878 bool
2879 rs6000_tls_referenced_p (rtx x)
2880 {
2881   return for_each_rtx (&x, &rs6000_tls_symbol_ref_1, 0);
2882 }
2883
2884 /* Return 1 if *X is a thread-local symbol.  This is the same as
2885    rs6000_tls_symbol_ref except for the type of the unused argument.  */
2886
2887 static inline int
2888 rs6000_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
2889 {
2890   return RS6000_SYMBOL_REF_TLS_P (*x);
2891 }
2892
2893 /* The convention appears to be to define this wherever it is used.
2894    With legitimize_reload_address now defined here, REG_MODE_OK_FOR_BASE_P
2895    is now used here.  */
2896 #ifndef REG_MODE_OK_FOR_BASE_P
2897 #define REG_MODE_OK_FOR_BASE_P(REGNO, MODE) REG_OK_FOR_BASE_P (REGNO)
2898 #endif
2899
2900 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS.  Returns a value to
2901    replace the input X, or the original X if no replacement is called for.
2902    The output parameter *WIN is 1 if the calling macro should goto WIN,
2903    0 if it should not.
2904
2905    For RS/6000, we wish to handle large displacements off a base
2906    register by splitting the addend across an addiu/addis and the mem insn.
2907    This cuts number of extra insns needed from 3 to 1.
2908
2909    On Darwin, we use this to generate code for floating point constants.
2910    A movsf_low is generated so we wind up with 2 instructions rather than 3.
2911    The Darwin code is inside #if TARGET_MACHO because only then is
2912    machopic_function_base_name() defined.  */
2913 rtx
2914 rs6000_legitimize_reload_address (rtx x, enum machine_mode mode, 
2915         int opnum, int type, int ind_levels ATTRIBUTE_UNUSED, int *win)
2916 {
2917   /* We must recognize output that we have already generated ourselves.  */ 
2918   if (GET_CODE (x) == PLUS
2919       && GET_CODE (XEXP (x, 0)) == PLUS
2920       && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
2921       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2922       && GET_CODE (XEXP (x, 1)) == CONST_INT)
2923     {
2924       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2925                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
2926                    opnum, (enum reload_type)type);
2927       *win = 1;
2928       return x;
2929     }
2930
2931 #if TARGET_MACHO
2932   if (DEFAULT_ABI == ABI_DARWIN && flag_pic
2933       && GET_CODE (x) == LO_SUM
2934       && GET_CODE (XEXP (x, 0)) == PLUS
2935       && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
2936       && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
2937       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 0)) == CONST
2938       && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
2939       && GET_CODE (XEXP (XEXP (x, 1), 0)) == MINUS
2940       && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 0)) == SYMBOL_REF
2941       && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 1)) == SYMBOL_REF)
2942     {
2943       /* Result of previous invocation of this function on Darwin
2944          floating point constant.  */
2945       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2946                 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
2947                 opnum, (enum reload_type)type);
2948       *win = 1;
2949       return x;
2950     }
2951 #endif
2952   if (GET_CODE (x) == PLUS
2953       && GET_CODE (XEXP (x, 0)) == REG
2954       && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2955       && REG_MODE_OK_FOR_BASE_P (XEXP (x, 0), mode)
2956       && GET_CODE (XEXP (x, 1)) == CONST_INT
2957       && !SPE_VECTOR_MODE (mode)
2958       && !ALTIVEC_VECTOR_MODE (mode))
2959     {
2960       HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
2961       HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
2962       HOST_WIDE_INT high
2963         = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
2964
2965       /* Check for 32-bit overflow.  */
2966       if (high + low != val)
2967         {
2968           *win = 0;
2969           return x;
2970         }
2971
2972       /* Reload the high part into a base reg; leave the low part
2973          in the mem directly.  */
2974
2975       x = gen_rtx_PLUS (GET_MODE (x),
2976                         gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
2977                                       GEN_INT (high)),
2978                         GEN_INT (low));
2979
2980       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
2981                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
2982                    opnum, (enum reload_type)type);
2983       *win = 1;
2984       return x;
2985     }
2986 #if TARGET_MACHO
2987   if (GET_CODE (x) == SYMBOL_REF
2988       && DEFAULT_ABI == ABI_DARWIN
2989       && !ALTIVEC_VECTOR_MODE (mode)
2990       && flag_pic)
2991     {
2992       /* Darwin load of floating point constant.  */
2993       rtx offset = gen_rtx (CONST, Pmode,
2994                     gen_rtx (MINUS, Pmode, x,
2995                     gen_rtx (SYMBOL_REF, Pmode,
2996                         machopic_function_base_name ())));
2997       x = gen_rtx (LO_SUM, GET_MODE (x),
2998             gen_rtx (PLUS, Pmode, pic_offset_table_rtx,
2999                 gen_rtx (HIGH, Pmode, offset)), offset);
3000       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3001                 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
3002                 opnum, (enum reload_type)type);
3003       *win = 1;
3004       return x;
3005     }
3006    if (GET_CODE (x) == SYMBOL_REF
3007        && DEFAULT_ABI == ABI_DARWIN
3008        && !ALTIVEC_VECTOR_MODE (mode)
3009        && MACHO_DYNAMIC_NO_PIC_P)
3010      {
3011        /* Darwin load of floating point constant.  */
3012        x = gen_rtx (LO_SUM, GET_MODE (x),
3013                gen_rtx (HIGH, Pmode, x), x);
3014        push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3015                BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
3016                opnum, (enum reload_type)type);
3017        *win = 1;
3018        return x;
3019      }
3020 #endif
3021   if (TARGET_TOC
3022       && constant_pool_expr_p (x)
3023       && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), mode))
3024     {
3025       (x) = create_TOC_reference (x);
3026       *win = 1;
3027       return x;
3028     }
3029   *win = 0;
3030   return x;
3031 }    
3032
3033 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression
3034    that is a valid memory address for an instruction.
3035    The MODE argument is the machine mode for the MEM expression
3036    that wants to use this address.
3037
3038    On the RS/6000, there are four valid address: a SYMBOL_REF that
3039    refers to a constant pool entry of an address (or the sum of it
3040    plus a constant), a short (16-bit signed) constant plus a register,
3041    the sum of two registers, or a register indirect, possibly with an
3042    auto-increment.  For DFmode and DImode with a constant plus register,
3043    we must ensure that both words are addressable or PowerPC64 with offset
3044    word aligned.
3045
3046    For modes spanning multiple registers (DFmode in 32-bit GPRs,
3047    32-bit DImode, TImode), indexed addressing cannot be used because
3048    adjacent memory cells are accessed by adding word-sized offsets
3049    during assembly output.  */
3050 int
3051 rs6000_legitimate_address (enum machine_mode mode, rtx x, int reg_ok_strict)
3052 {
3053   if (RS6000_SYMBOL_REF_TLS_P (x))
3054     return 0;
3055   if (legitimate_indirect_address_p (x, reg_ok_strict))
3056     return 1;
3057   if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
3058       && !ALTIVEC_VECTOR_MODE (mode)
3059       && !SPE_VECTOR_MODE (mode)
3060       && TARGET_UPDATE
3061       && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
3062     return 1;
3063   if (legitimate_small_data_p (mode, x))
3064     return 1;
3065   if (legitimate_constant_pool_address_p (x))
3066     return 1;
3067   /* If not REG_OK_STRICT (before reload) let pass any stack offset.  */
3068   if (! reg_ok_strict
3069       && GET_CODE (x) == PLUS
3070       && GET_CODE (XEXP (x, 0)) == REG
3071       && XEXP (x, 0) == virtual_stack_vars_rtx
3072       && GET_CODE (XEXP (x, 1)) == CONST_INT)
3073     return 1;
3074   if (legitimate_offset_address_p (mode, x, reg_ok_strict))
3075     return 1;
3076   if (mode != TImode
3077       && ((TARGET_HARD_FLOAT && TARGET_FPRS)
3078           || TARGET_POWERPC64
3079           || (mode != DFmode && mode != TFmode))
3080       && (TARGET_POWERPC64 || mode != DImode)
3081       && legitimate_indexed_address_p (x, reg_ok_strict))
3082     return 1;
3083   if (legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
3084     return 1;
3085   return 0;
3086 }
3087
3088 /* Go to LABEL if ADDR (a legitimate address expression)
3089    has an effect that depends on the machine mode it is used for.
3090
3091    On the RS/6000 this is true of all integral offsets (since AltiVec
3092    modes don't allow them) or is a pre-increment or decrement.
3093
3094    ??? Except that due to conceptual problems in offsettable_address_p
3095    we can't really report the problems of integral offsets.  So leave
3096    this assuming that the adjustable offset must be valid for the 
3097    sub-words of a TFmode operand, which is what we had before.  */
3098
3099 bool
3100 rs6000_mode_dependent_address (rtx addr)
3101 {
3102   switch (GET_CODE (addr))
3103     {
3104     case PLUS:
3105       if (GET_CODE (XEXP (addr, 1)) == CONST_INT)
3106         {
3107           unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
3108           return val + 12 + 0x8000 >= 0x10000;
3109         }
3110       break;
3111
3112     case LO_SUM:
3113       return true;
3114
3115     case PRE_INC:
3116     case PRE_DEC:
3117       return TARGET_UPDATE;
3118
3119     default:
3120       break;
3121     }
3122
3123   return false;
3124 }
3125 \f
3126 /* Try to output insns to set TARGET equal to the constant C if it can
3127    be done in less than N insns.  Do all computations in MODE.
3128    Returns the place where the output has been placed if it can be
3129    done and the insns have been emitted.  If it would take more than N
3130    insns, zero is returned and no insns and emitted.  */
3131
3132 rtx
3133 rs6000_emit_set_const (rtx dest, enum machine_mode mode, 
3134                        rtx source, int n ATTRIBUTE_UNUSED)
3135 {
3136   rtx result, insn, set;
3137   HOST_WIDE_INT c0, c1;
3138
3139   if (mode == QImode || mode == HImode)
3140     {
3141       if (dest == NULL)
3142         dest = gen_reg_rtx (mode);
3143       emit_insn (gen_rtx_SET (VOIDmode, dest, source));
3144       return dest;
3145     }
3146   else if (mode == SImode)
3147     {
3148       result = no_new_pseudos ? dest : gen_reg_rtx (SImode);
3149
3150       emit_insn (gen_rtx_SET (VOIDmode, result,
3151                               GEN_INT (INTVAL (source)
3152                                        & (~ (HOST_WIDE_INT) 0xffff))));
3153       emit_insn (gen_rtx_SET (VOIDmode, dest,
3154                               gen_rtx_IOR (SImode, result,
3155                                            GEN_INT (INTVAL (source) & 0xffff))));
3156       result = dest;
3157     }
3158   else if (mode == DImode)
3159     {
3160       if (GET_CODE (source) == CONST_INT)
3161         {
3162           c0 = INTVAL (source);
3163           c1 = -(c0 < 0);
3164         }
3165       else if (GET_CODE (source) == CONST_DOUBLE)
3166         {
3167 #if HOST_BITS_PER_WIDE_INT >= 64
3168           c0 = CONST_DOUBLE_LOW (source);
3169           c1 = -(c0 < 0);
3170 #else
3171           c0 = CONST_DOUBLE_LOW (source);
3172           c1 = CONST_DOUBLE_HIGH (source);
3173 #endif
3174         }
3175       else
3176         abort ();
3177
3178       result = rs6000_emit_set_long_const (dest, c0, c1);
3179     }
3180   else
3181     abort ();
3182
3183   insn = get_last_insn ();
3184   set = single_set (insn);
3185   if (! CONSTANT_P (SET_SRC (set)))
3186     set_unique_reg_note (insn, REG_EQUAL, source);
3187
3188   return result;
3189 }
3190
3191 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
3192    fall back to a straight forward decomposition.  We do this to avoid
3193    exponential run times encountered when looking for longer sequences
3194    with rs6000_emit_set_const.  */
3195 static rtx
3196 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c1, HOST_WIDE_INT c2)
3197 {
3198   if (!TARGET_POWERPC64)
3199     {
3200       rtx operand1, operand2;
3201
3202       operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
3203                                         DImode);
3204       operand2 = operand_subword_force (dest, WORDS_BIG_ENDIAN != 0,
3205                                         DImode);
3206       emit_move_insn (operand1, GEN_INT (c1));
3207       emit_move_insn (operand2, GEN_INT (c2));
3208     }
3209   else
3210     {
3211       HOST_WIDE_INT ud1, ud2, ud3, ud4;
3212
3213       ud1 = c1 & 0xffff;
3214       ud2 = (c1 & 0xffff0000) >> 16;
3215 #if HOST_BITS_PER_WIDE_INT >= 64
3216       c2 = c1 >> 32;
3217 #endif
3218       ud3 = c2 & 0xffff;
3219       ud4 = (c2 & 0xffff0000) >> 16;
3220
3221       if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000)) 
3222           || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
3223         {
3224           if (ud1 & 0x8000)
3225             emit_move_insn (dest, GEN_INT (((ud1 ^ 0x8000) -  0x8000)));
3226           else
3227             emit_move_insn (dest, GEN_INT (ud1));
3228         }
3229
3230       else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000)) 
3231                || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
3232         {
3233           if (ud2 & 0x8000)
3234             emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000) 
3235                                            - 0x80000000));
3236           else
3237             emit_move_insn (dest, GEN_INT (ud2 << 16));
3238           if (ud1 != 0)
3239             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
3240         }
3241       else if ((ud4 == 0xffff && (ud3 & 0x8000)) 
3242                || (ud4 == 0 && ! (ud3 & 0x8000)))
3243         {
3244           if (ud3 & 0x8000)
3245             emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000) 
3246                                            - 0x80000000));
3247           else
3248             emit_move_insn (dest, GEN_INT (ud3 << 16));
3249
3250           if (ud2 != 0)
3251             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud2)));
3252           emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (16)));
3253           if (ud1 != 0)
3254             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
3255         }
3256       else 
3257         {
3258           if (ud4 & 0x8000)
3259             emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000) 
3260                                            - 0x80000000));
3261           else
3262             emit_move_insn (dest, GEN_INT (ud4 << 16));
3263
3264           if (ud3 != 0)
3265             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud3)));
3266
3267           emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (32)));
3268           if (ud2 != 0)
3269             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, 
3270                                                GEN_INT (ud2 << 16)));   
3271           if (ud1 != 0)
3272             emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
3273         }
3274     }
3275   return dest;
3276 }
3277
3278 /* Emit a move from SOURCE to DEST in mode MODE.  */
3279 void
3280 rs6000_emit_move (rtx dest, rtx source, enum machine_mode mode)
3281 {
3282   rtx operands[2];
3283   operands[0] = dest;
3284   operands[1] = source;
3285   
3286   /* Sanity checks.  Check that we get CONST_DOUBLE only when we should.  */
3287   if (GET_CODE (operands[1]) == CONST_DOUBLE
3288       && ! FLOAT_MODE_P (mode)
3289       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3290     {
3291       /* FIXME.  This should never happen.  */
3292       /* Since it seems that it does, do the safe thing and convert
3293          to a CONST_INT.  */
3294       operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
3295     }
3296   if (GET_CODE (operands[1]) == CONST_DOUBLE
3297       && ! FLOAT_MODE_P (mode)
3298       && ((CONST_DOUBLE_HIGH (operands[1]) == 0
3299            && CONST_DOUBLE_LOW (operands[1]) >= 0)
3300           || (CONST_DOUBLE_HIGH (operands[1]) == -1
3301               && CONST_DOUBLE_LOW (operands[1]) < 0)))
3302     abort ();
3303
3304   /* Check if GCC is setting up a block move that will end up using FP
3305      registers as temporaries.  We must make sure this is acceptable.  */
3306   if (GET_CODE (operands[0]) == MEM
3307       && GET_CODE (operands[1]) == MEM
3308       && mode == DImode
3309       && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
3310           || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
3311       && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
3312                                             ? 32 : MEM_ALIGN (operands[0])))
3313             || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
3314                                                ? 32 
3315                                                : MEM_ALIGN (operands[1]))))
3316       && ! MEM_VOLATILE_P (operands [0])
3317       && ! MEM_VOLATILE_P (operands [1]))
3318     {
3319       emit_move_insn (adjust_address (operands[0], SImode, 0),
3320                       adjust_address (operands[1], SImode, 0));
3321       emit_move_insn (adjust_address (operands[0], SImode, 4),
3322                       adjust_address (operands[1], SImode, 4));
3323       return;
3324     }
3325   
3326   if (!no_new_pseudos)
3327     {
3328       if (GET_CODE (operands[1]) == MEM && optimize > 0
3329           && (mode == QImode || mode == HImode || mode == SImode)
3330           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3331         {
3332           rtx reg = gen_reg_rtx (word_mode);
3333
3334           emit_insn (gen_rtx_SET (word_mode, reg,
3335                                   gen_rtx_ZERO_EXTEND (word_mode,
3336                                                        operands[1])));
3337           operands[1] = gen_lowpart (mode, reg);
3338         }
3339       if (GET_CODE (operands[0]) != REG)
3340         operands[1] = force_reg (mode, operands[1]);
3341     }
3342
3343   if (mode == SFmode && ! TARGET_POWERPC
3344       && TARGET_HARD_FLOAT && TARGET_FPRS
3345       && GET_CODE (operands[0]) == MEM)
3346     {
3347       int regnum;
3348
3349       if (reload_in_progress || reload_completed)
3350         regnum = true_regnum (operands[1]);
3351       else if (GET_CODE (operands[1]) == REG)
3352         regnum = REGNO (operands[1]);
3353       else
3354         regnum = -1;
3355       
3356       /* If operands[1] is a register, on POWER it may have
3357          double-precision data in it, so truncate it to single
3358          precision.  */
3359       if (FP_REGNO_P (regnum) || regnum >= FIRST_PSEUDO_REGISTER)
3360         {
3361           rtx newreg;
3362           newreg = (no_new_pseudos ? operands[1] : gen_reg_rtx (mode));
3363           emit_insn (gen_aux_truncdfsf2 (newreg, operands[1]));
3364           operands[1] = newreg;
3365         }
3366     }
3367
3368   /* Recognize the case where operand[1] is a reference to thread-local
3369      data and load its address to a register.  */
3370   if (GET_CODE (operands[1]) == SYMBOL_REF)
3371     {
3372       enum tls_model model = SYMBOL_REF_TLS_MODEL (operands[1]);
3373       if (model != 0)
3374         operands[1] = rs6000_legitimize_tls_address (operands[1], model);
3375     }
3376
3377   /* Handle the case where reload calls us with an invalid address.  */
3378   if (reload_in_progress && mode == Pmode
3379       && (! general_operand (operands[1], mode)
3380           || ! nonimmediate_operand (operands[0], mode)))
3381     goto emit_set;
3382
3383   /* Handle the case of CONSTANT_P_RTX.  */
3384   if (GET_CODE (operands[1]) == CONSTANT_P_RTX)
3385     goto emit_set;
3386   
3387   /* FIXME:  In the long term, this switch statement should go away
3388      and be replaced by a sequence of tests based on things like
3389      mode == Pmode.  */
3390   switch (mode)
3391     {
3392     case HImode:
3393     case QImode:
3394       if (CONSTANT_P (operands[1])
3395           && GET_CODE (operands[1]) != CONST_INT)
3396         operands[1] = force_const_mem (mode, operands[1]);
3397       break;
3398
3399     case TFmode:
3400     case DFmode:
3401     case SFmode:
3402       if (CONSTANT_P (operands[1]) 
3403           && ! easy_fp_constant (operands[1], mode))
3404         operands[1] = force_const_mem (mode, operands[1]);
3405       break;
3406       
3407     case V16QImode:
3408     case V8HImode:
3409     case V4SFmode:
3410     case V4SImode:
3411     case V4HImode:
3412     case V2SFmode:
3413     case V2SImode:
3414     case V1DImode:
3415       if (CONSTANT_P (operands[1])
3416           && !easy_vector_constant (operands[1], mode))
3417         operands[1] = force_const_mem (mode, operands[1]);
3418       break;
3419       
3420     case SImode:
3421     case DImode:
3422       /* Use default pattern for address of ELF small data */
3423       if (TARGET_ELF
3424           && mode == Pmode
3425           && DEFAULT_ABI == ABI_V4
3426           && (GET_CODE (operands[1]) == SYMBOL_REF 
3427               || GET_CODE (operands[1]) == CONST)
3428           && small_data_operand (operands[1], mode))
3429         {
3430           emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
3431           return;
3432         }
3433
3434       if (DEFAULT_ABI == ABI_V4
3435           && mode == Pmode && mode == SImode
3436           && flag_pic == 1 && got_operand (operands[1], mode))
3437         {
3438           emit_insn (gen_movsi_got (operands[0], operands[1]));
3439           return;
3440         }
3441
3442       if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
3443           && TARGET_NO_TOC
3444           && ! flag_pic
3445           && mode == Pmode
3446           && CONSTANT_P (operands[1])
3447           && GET_CODE (operands[1]) != HIGH
3448           && GET_CODE (operands[1]) != CONST_INT)
3449         {
3450           rtx target = (no_new_pseudos ? operands[0] : gen_reg_rtx (mode));
3451
3452           /* If this is a function address on -mcall-aixdesc,
3453              convert it to the address of the descriptor.  */
3454           if (DEFAULT_ABI == ABI_AIX
3455               && GET_CODE (operands[1]) == SYMBOL_REF
3456               && XSTR (operands[1], 0)[0] == '.')
3457             {
3458               const char *name = XSTR (operands[1], 0);
3459               rtx new_ref;
3460               while (*name == '.')
3461                 name++;
3462               new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
3463               CONSTANT_POOL_ADDRESS_P (new_ref)
3464                 = CONSTANT_POOL_ADDRESS_P (operands[1]);
3465               SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
3466               SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
3467               SYMBOL_REF_DECL (new_ref) = SYMBOL_REF_DECL (operands[1]);
3468               operands[1] = new_ref;
3469             }
3470
3471           if (DEFAULT_ABI == ABI_DARWIN)
3472             {
3473 #if TARGET_MACHO
3474               if (MACHO_DYNAMIC_NO_PIC_P)
3475                 {
3476                   /* Take care of any required data indirection.  */
3477                   operands[1] = rs6000_machopic_legitimize_pic_address (
3478                                   operands[1], mode, operands[0]);
3479                   if (operands[0] != operands[1])
3480                     emit_insn (gen_rtx_SET (VOIDmode,
3481                                             operands[0], operands[1]));
3482                   return;
3483                 }
3484 #endif
3485               emit_insn (gen_macho_high (target, operands[1]));
3486               emit_insn (gen_macho_low (operands[0], target, operands[1]));
3487               return;
3488             }
3489
3490           emit_insn (gen_elf_high (target, operands[1]));
3491           emit_insn (gen_elf_low (operands[0], target, operands[1]));
3492           return;
3493         }
3494
3495       /* If this is a SYMBOL_REF that refers to a constant pool entry,
3496          and we have put it in the TOC, we just need to make a TOC-relative
3497          reference to it.  */
3498       if (TARGET_TOC
3499           && GET_CODE (operands[1]) == SYMBOL_REF
3500           && constant_pool_expr_p (operands[1])
3501           && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (operands[1]),
3502                                               get_pool_mode (operands[1])))
3503         {
3504           operands[1] = create_TOC_reference (operands[1]);
3505         }
3506       else if (mode == Pmode
3507                && CONSTANT_P (operands[1])
3508                && ((GET_CODE (operands[1]) != CONST_INT
3509                     && ! easy_fp_constant (operands[1], mode))
3510                    || (GET_CODE (operands[1]) == CONST_INT
3511                        && num_insns_constant (operands[1], mode) > 2)
3512                    || (GET_CODE (operands[0]) == REG
3513                        && FP_REGNO_P (REGNO (operands[0]))))
3514                && GET_CODE (operands[1]) != HIGH
3515                && ! legitimate_constant_pool_address_p (operands[1])
3516                && ! toc_relative_expr_p (operands[1]))
3517         {
3518           /* Emit a USE operation so that the constant isn't deleted if
3519              expensive optimizations are turned on because nobody
3520              references it.  This should only be done for operands that
3521              contain SYMBOL_REFs with CONSTANT_POOL_ADDRESS_P set.
3522              This should not be done for operands that contain LABEL_REFs.
3523              For now, we just handle the obvious case.  */
3524           if (GET_CODE (operands[1]) != LABEL_REF)
3525             emit_insn (gen_rtx_USE (VOIDmode, operands[1]));
3526
3527 #if TARGET_MACHO
3528           /* Darwin uses a special PIC legitimizer.  */
3529           if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
3530             {
3531               operands[1] =
3532                 rs6000_machopic_legitimize_pic_address (operands[1], mode,
3533                                                         operands[0]);
3534               if (operands[0] != operands[1])
3535                 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
3536               return;
3537             }
3538 #endif
3539
3540           /* If we are to limit the number of things we put in the TOC and
3541              this is a symbol plus a constant we can add in one insn,
3542              just put the symbol in the TOC and add the constant.  Don't do
3543              this if reload is in progress.  */
3544           if (GET_CODE (operands[1]) == CONST
3545               && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
3546               && GET_CODE (XEXP (operands[1], 0)) == PLUS
3547               && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
3548               && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
3549                   || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
3550               && ! side_effects_p (operands[0]))
3551             {
3552               rtx sym =
3553                 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
3554               rtx other = XEXP (XEXP (operands[1], 0), 1);
3555
3556               sym = force_reg (mode, sym);
3557               if (mode == SImode)
3558                 emit_insn (gen_addsi3 (operands[0], sym, other));
3559               else
3560                 emit_insn (gen_adddi3 (operands[0], sym, other));
3561               return;
3562             }
3563
3564           operands[1] = force_const_mem (mode, operands[1]);
3565
3566           if (TARGET_TOC 
3567               && constant_pool_expr_p (XEXP (operands[1], 0))
3568               && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
3569                         get_pool_constant (XEXP (operands[1], 0)),
3570                         get_pool_mode (XEXP (operands[1], 0))))
3571             {
3572               operands[1]
3573                 = gen_rtx_MEM (mode,
3574                                create_TOC_reference (XEXP (operands[1], 0)));
3575               set_mem_alias_set (operands[1], get_TOC_alias_set ());
3576               RTX_UNCHANGING_P (operands[1]) = 1;
3577             }
3578         }
3579       break;
3580
3581     case TImode:
3582       if (GET_CODE (operands[0]) == MEM
3583           && GET_CODE (XEXP (operands[0], 0)) != REG
3584           && ! reload_in_progress)
3585         operands[0]
3586           = replace_equiv_address (operands[0],
3587                                    copy_addr_to_reg (XEXP (operands[0], 0)));
3588
3589       if (GET_CODE (operands[1]) == MEM
3590           && GET_CODE (XEXP (operands[1], 0)) != REG
3591           && ! reload_in_progress)
3592         operands[1]
3593           = replace_equiv_address (operands[1],
3594                                    copy_addr_to_reg (XEXP (operands[1], 0)));
3595       if (TARGET_POWER)
3596         {
3597           emit_insn (gen_rtx_PARALLEL (VOIDmode,
3598                        gen_rtvec (2,
3599                                   gen_rtx_SET (VOIDmode,
3600                                                operands[0], operands[1]),
3601                                   gen_rtx_CLOBBER (VOIDmode,
3602                                                    gen_rtx_SCRATCH (SImode)))));
3603           return;
3604         }
3605       break;
3606
3607     default:
3608       abort ();
3609     }
3610
3611   /* Above, we may have called force_const_mem which may have returned
3612      an invalid address.  If we can, fix this up; otherwise, reload will
3613      have to deal with it.  */
3614   if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
3615     operands[1] = validize_mem (operands[1]);
3616
3617  emit_set:
3618   emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
3619 }
3620 \f
3621 /* Nonzero if we can use a floating-point register to pass this arg.  */
3622 #define USE_FP_FOR_ARG_P(CUM,MODE,TYPE)         \
3623   (GET_MODE_CLASS (MODE) == MODE_FLOAT          \
3624    && (CUM)->fregno <= FP_ARG_MAX_REG           \
3625    && TARGET_HARD_FLOAT && TARGET_FPRS)
3626
3627 /* Nonzero if we can use an AltiVec register to pass this arg.  */
3628 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,TYPE,NAMED)      \
3629   (ALTIVEC_VECTOR_MODE (MODE)                           \
3630    && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG              \
3631    && TARGET_ALTIVEC_ABI                                \
3632    && (DEFAULT_ABI == ABI_V4 || (NAMED)))
3633
3634 /* Return a nonzero value to say to return the function value in
3635    memory, just as large structures are always returned.  TYPE will be
3636    the data type of the value, and FNTYPE will be the type of the
3637    function doing the returning, or @code{NULL} for libcalls.
3638
3639    The AIX ABI for the RS/6000 specifies that all structures are
3640    returned in memory.  The Darwin ABI does the same.  The SVR4 ABI
3641    specifies that structures <= 8 bytes are returned in r3/r4, but a
3642    draft put them in memory, and GCC used to implement the draft
3643    instead of the final standard.  Therefore, TARGET_AIX_STRUCT_RET
3644    controls this instead of DEFAULT_ABI; V.4 targets needing backward
3645    compatibility can change DRAFT_V4_STRUCT_RET to override the
3646    default, and -m switches get the final word.  See
3647    rs6000_override_options for more details.
3648
3649    The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
3650    long double support is enabled.  These values are returned in memory.
3651
3652    int_size_in_bytes returns -1 for variable size objects, which go in
3653    memory always.  The cast to unsigned makes -1 > 8.  */
3654
3655 static bool
3656 rs6000_return_in_memory (tree type, tree fntype ATTRIBUTE_UNUSED)
3657 {
3658   if (AGGREGATE_TYPE_P (type)
3659       && (TARGET_AIX_STRUCT_RET
3660           || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
3661     return true;
3662   if (DEFAULT_ABI == ABI_V4 && TYPE_MODE (type) == TFmode)
3663     return true;
3664   return false;
3665 }
3666
3667 /* Initialize a variable CUM of type CUMULATIVE_ARGS
3668    for a call to a function whose data type is FNTYPE.
3669    For a library call, FNTYPE is 0.
3670
3671    For incoming args we set the number of arguments in the prototype large
3672    so we never return a PARALLEL.  */
3673
3674 void
3675 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype, 
3676         rtx libname ATTRIBUTE_UNUSED, int incoming, int libcall)
3677 {
3678   static CUMULATIVE_ARGS zero_cumulative;
3679
3680   *cum = zero_cumulative;
3681   cum->words = 0;
3682   cum->fregno = FP_ARG_MIN_REG;
3683   cum->vregno = ALTIVEC_ARG_MIN_REG;
3684   cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
3685   cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
3686                       ? CALL_LIBCALL : CALL_NORMAL);
3687   cum->sysv_gregno = GP_ARG_MIN_REG;
3688   cum->stdarg = fntype
3689     && (TYPE_ARG_TYPES (fntype) != 0
3690         && (TREE_VALUE (tree_last  (TYPE_ARG_TYPES (fntype)))
3691             != void_type_node));
3692
3693   if (incoming)
3694     cum->nargs_prototype = 1000;                /* don't return a PARALLEL */
3695
3696   else if (cum->prototype)
3697     cum->nargs_prototype = (list_length (TYPE_ARG_TYPES (fntype)) - 1
3698                             + (TYPE_MODE (TREE_TYPE (fntype)) == BLKmode
3699                                || rs6000_return_in_memory (TREE_TYPE (fntype),
3700                                                            fntype)));
3701
3702   else
3703     cum->nargs_prototype = 0;
3704
3705   /* Check for a longcall attribute.  */
3706   if (fntype
3707       && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
3708       && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype)))
3709     cum->call_cookie = CALL_LONG;
3710
3711   if (TARGET_DEBUG_ARG)
3712     {
3713       fprintf (stderr, "\ninit_cumulative_args:");
3714       if (fntype)
3715         {
3716           tree ret_type = TREE_TYPE (fntype);
3717           fprintf (stderr, " ret code = %s,",
3718                    tree_code_name[ (int)TREE_CODE (ret_type) ]);
3719         }
3720
3721       if (cum->call_cookie & CALL_LONG)
3722         fprintf (stderr, " longcall,");
3723
3724       fprintf (stderr, " proto = %d, nargs = %d\n",
3725                cum->prototype, cum->nargs_prototype);
3726     }
3727 }
3728 \f
3729 /* If defined, a C expression which determines whether, and in which
3730    direction, to pad out an argument with extra space.  The value
3731    should be of type `enum direction': either `upward' to pad above
3732    the argument, `downward' to pad below, or `none' to inhibit
3733    padding.
3734
3735    For the AIX ABI structs are always stored left shifted in their
3736    argument slot.  */
3737
3738 enum direction
3739 function_arg_padding (enum machine_mode mode, tree type)
3740 {
3741 #ifndef AGGREGATE_PADDING_FIXED
3742 #define AGGREGATE_PADDING_FIXED 0
3743 #endif
3744 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
3745 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
3746 #endif
3747
3748   if (!AGGREGATE_PADDING_FIXED)
3749     {
3750       /* GCC used to pass structures of the same size as integer types as
3751          if they were in fact integers, ignoring FUNCTION_ARG_PADDING.
3752          ie. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
3753          passed padded downward, except that -mstrict-align further
3754          muddied the water in that multi-component structures of 2 and 4
3755          bytes in size were passed padded upward.
3756
3757          The following arranges for best compatibility with previous
3758          versions of gcc, but removes the -mstrict-align dependency.  */
3759       if (BYTES_BIG_ENDIAN)
3760         {
3761           HOST_WIDE_INT size = 0;
3762
3763           if (mode == BLKmode)
3764             {
3765               if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
3766                 size = int_size_in_bytes (type);
3767             }
3768           else
3769             size = GET_MODE_SIZE (mode);
3770
3771           if (size == 1 || size == 2 || size == 4)
3772             return downward;
3773         }
3774       return upward;
3775     }
3776
3777   if (AGGREGATES_PAD_UPWARD_ALWAYS)
3778     {
3779       if (type != 0 && AGGREGATE_TYPE_P (type))
3780         return upward;
3781     }
3782
3783   /* Fall back to the default.  */
3784   return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
3785 }
3786
3787 /* If defined, a C expression that gives the alignment boundary, in bits,
3788    of an argument with the specified mode and type.  If it is not defined, 
3789    PARM_BOUNDARY is used for all arguments.
3790    
3791    V.4 wants long longs to be double word aligned.  */
3792
3793 int
3794 function_arg_boundary (enum machine_mode mode, tree type ATTRIBUTE_UNUSED)
3795 {
3796   if (DEFAULT_ABI == ABI_V4 && (mode == DImode || mode == DFmode))
3797     return 64;
3798    else if (SPE_VECTOR_MODE (mode))
3799      return 64;
3800   else if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
3801     return 128;
3802   else
3803     return PARM_BOUNDARY;
3804 }
3805 \f
3806 /* Update the data in CUM to advance over an argument
3807    of mode MODE and data type TYPE.
3808    (TYPE is null for libcalls where that information may not be available.)  */
3809
3810 void
3811 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
3812                       tree type, int named)
3813 {
3814   cum->nargs_prototype--;
3815
3816   if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
3817     {
3818       if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
3819         cum->vregno++;
3820       
3821       /* In variable-argument functions, vector arguments get GPRs allocated
3822          even if they are going to be passed in a vector register.  */
3823       if (cum->stdarg && DEFAULT_ABI != ABI_V4)
3824         {
3825           int align;
3826           
3827           /* Vector parameters must be 16-byte aligned.  This places
3828              them at 2 mod 4 in terms of words in 32-bit mode, since
3829              the parameter save area starts at offset 24 from the
3830              stack.  In 64-bit mode, they just have to start on an
3831              even word, since the parameter save area is 16-byte
3832              aligned.  Space for GPRs is reserved even if the argument
3833              will be passed in memory.  */
3834           if (TARGET_32BIT)
3835             align = ((6 - (cum->words & 3)) & 3);
3836           else
3837             align = cum->words & 1;
3838           cum->words += align + RS6000_ARG_SIZE (mode, type);
3839           
3840           if (TARGET_DEBUG_ARG)
3841             {
3842               fprintf (stderr, "function_adv: words = %2d, align=%d, ", 
3843                        cum->words, align);
3844               fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
3845                        cum->nargs_prototype, cum->prototype, 
3846                        GET_MODE_NAME (mode));
3847             }
3848         }
3849     }
3850   else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
3851            && !cum->stdarg
3852            && cum->sysv_gregno <= GP_ARG_MAX_REG)
3853     cum->sysv_gregno++;
3854   else if (DEFAULT_ABI == ABI_V4)
3855     {
3856       if (TARGET_HARD_FLOAT && TARGET_FPRS
3857           && (mode == SFmode || mode == DFmode))
3858         {
3859           if (cum->fregno <= FP_ARG_V4_MAX_REG)
3860             cum->fregno++;
3861           else
3862             {
3863               if (mode == DFmode)
3864                 cum->words += cum->words & 1;
3865               cum->words += RS6000_ARG_SIZE (mode, type);
3866             }
3867         }
3868       else
3869         {
3870           int n_words;
3871           int gregno = cum->sysv_gregno;
3872
3873           /* Aggregates and IEEE quad get passed by reference.  */
3874           if ((type && AGGREGATE_TYPE_P (type))
3875               || mode == TFmode)
3876             n_words = 1;
3877           else 
3878             n_words = RS6000_ARG_SIZE (mode, type);
3879
3880           /* Long long and SPE vectors are put in odd registers.  */
3881           if (n_words == 2 && (gregno & 1) == 0)
3882             gregno += 1;
3883
3884           /* Long long and SPE vectors are not split between registers
3885              and stack.  */
3886           if (gregno + n_words - 1 > GP_ARG_MAX_REG)
3887             {
3888               /* Long long is aligned on the stack.  */
3889               if (n_words == 2)
3890                 cum->words += cum->words & 1;
3891               cum->words += n_words;
3892             }
3893
3894           /* Note: continuing to accumulate gregno past when we've started
3895              spilling to the stack indicates the fact that we've started
3896              spilling to the stack to expand_builtin_saveregs.  */
3897           cum->sysv_gregno = gregno + n_words;
3898         }
3899
3900       if (TARGET_DEBUG_ARG)
3901         {
3902           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
3903                    cum->words, cum->fregno);
3904           fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
3905                    cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
3906           fprintf (stderr, "mode = %4s, named = %d\n",
3907                    GET_MODE_NAME (mode), named);
3908         }
3909     }
3910   else
3911     {
3912       int align = (TARGET_32BIT && (cum->words & 1) != 0
3913                    && function_arg_boundary (mode, type) == 64) ? 1 : 0;
3914
3915       cum->words += align + RS6000_ARG_SIZE (mode, type);
3916
3917       if (GET_MODE_CLASS (mode) == MODE_FLOAT
3918           && TARGET_HARD_FLOAT && TARGET_FPRS)
3919         cum->fregno += (mode == TFmode ? 2 : 1);
3920
3921       if (TARGET_DEBUG_ARG)
3922         {
3923           fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
3924                    cum->words, cum->fregno);
3925           fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
3926                    cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
3927           fprintf (stderr, "named = %d, align = %d\n", named, align);
3928         }
3929     }
3930 }
3931
3932 /* Determine where to put a SIMD argument on the SPE.  */
3933
3934 static rtx
3935 rs6000_spe_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
3936                          tree type)
3937 {
3938   if (cum->stdarg)
3939     {
3940       int gregno = cum->sysv_gregno;
3941       int n_words = RS6000_ARG_SIZE (mode, type);
3942
3943       /* SPE vectors are put in odd registers.  */
3944       if (n_words == 2 && (gregno & 1) == 0)
3945         gregno += 1;
3946
3947       if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
3948         {
3949           rtx r1, r2;
3950           enum machine_mode m = SImode;
3951
3952           r1 = gen_rtx_REG (m, gregno);
3953           r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
3954           r2 = gen_rtx_REG (m, gregno + 1);
3955           r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
3956           return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
3957         }
3958       else
3959         return NULL_RTX;
3960     }
3961   else
3962     {
3963       if (cum->sysv_gregno <= GP_ARG_MAX_REG)
3964         return gen_rtx_REG (mode, cum->sysv_gregno);
3965       else
3966         return NULL_RTX;
3967     }
3968 }
3969
3970 /* Determine where to place an argument in 64-bit mode with 32-bit ABI.  */
3971
3972 static rtx
3973 rs6000_mixed_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
3974                            tree type, int align_words)
3975 {
3976   if (mode == DFmode)
3977     {
3978       /* -mpowerpc64 with 32bit ABI splits up a DFmode argument
3979          in vararg list into zero, one or two GPRs */
3980       if (align_words >= GP_ARG_NUM_REG)
3981         return gen_rtx_PARALLEL (DFmode,
3982                  gen_rtvec (2,
3983                             gen_rtx_EXPR_LIST (VOIDmode,
3984                                                NULL_RTX, const0_rtx), 
3985                             gen_rtx_EXPR_LIST (VOIDmode,
3986                                                gen_rtx_REG (mode,
3987                                                             cum->fregno),
3988                                                const0_rtx)));
3989       else if (align_words + RS6000_ARG_SIZE (mode, type)
3990                > GP_ARG_NUM_REG)
3991         /* If this is partially on the stack, then we only
3992            include the portion actually in registers here. */
3993         return gen_rtx_PARALLEL (DFmode,
3994                  gen_rtvec (2,   
3995                             gen_rtx_EXPR_LIST (VOIDmode,
3996                                                gen_rtx_REG (SImode,
3997                                                             GP_ARG_MIN_REG
3998                                                             + align_words),
3999                                                const0_rtx),
4000                             gen_rtx_EXPR_LIST (VOIDmode,
4001                                                gen_rtx_REG (mode,
4002                                                             cum->fregno),
4003                                                const0_rtx)));
4004
4005       /* split a DFmode arg into two GPRs */
4006       return gen_rtx_PARALLEL (DFmode,
4007                gen_rtvec (3,
4008                           gen_rtx_EXPR_LIST (VOIDmode,       
4009                                              gen_rtx_REG (SImode,
4010                                                           GP_ARG_MIN_REG
4011                                                           + align_words),
4012                                              const0_rtx),
4013                           gen_rtx_EXPR_LIST (VOIDmode,
4014                                              gen_rtx_REG (SImode,
4015                                                           GP_ARG_MIN_REG
4016                                                           + align_words + 1),
4017                                              GEN_INT (4)),
4018                           gen_rtx_EXPR_LIST (VOIDmode,
4019                                              gen_rtx_REG (mode, cum->fregno),
4020                                              const0_rtx)));
4021     }
4022   /* -mpowerpc64 with 32bit ABI splits up a DImode argument into one
4023      or two GPRs */
4024   else if (mode == DImode)
4025     {
4026       if (align_words < GP_ARG_NUM_REG - 1)
4027         return gen_rtx_PARALLEL (DImode,
4028                  gen_rtvec (2,
4029                             gen_rtx_EXPR_LIST (VOIDmode,
4030                                                gen_rtx_REG (SImode,
4031                                                             GP_ARG_MIN_REG
4032                                                             + align_words),
4033                                                const0_rtx),
4034                             gen_rtx_EXPR_LIST (VOIDmode,
4035                                                gen_rtx_REG (SImode,
4036                                                             GP_ARG_MIN_REG
4037                                                             + align_words + 1),
4038                                                GEN_INT (4))));
4039       else if (align_words == GP_ARG_NUM_REG - 1)
4040           return gen_rtx_PARALLEL (DImode,
4041                    gen_rtvec (2,
4042                               gen_rtx_EXPR_LIST (VOIDmode,
4043                                                  NULL_RTX, const0_rtx),
4044                               gen_rtx_EXPR_LIST (VOIDmode,
4045                                                  gen_rtx_REG (SImode,
4046                                                               GP_ARG_MIN_REG
4047                                                               + align_words),
4048                                                  const0_rtx)));
4049     }
4050   else if (mode == BLKmode && align_words <= (GP_ARG_NUM_REG - 1))
4051     {
4052       int k;
4053       int size = int_size_in_bytes (type);
4054       int no_units = ((size - 1) / 4) + 1;
4055       int max_no_words = GP_ARG_NUM_REG - align_words;
4056       int rtlvec_len = no_units < max_no_words ? no_units : max_no_words;
4057       rtx *rtlvec = (rtx *) alloca (rtlvec_len * sizeof (rtx));
4058
4059       memset ((char *) rtlvec, 0, rtlvec_len * sizeof (rtx));
4060
4061       for (k=0; k < rtlvec_len; k++)
4062         rtlvec[k] = gen_rtx_EXPR_LIST (VOIDmode,
4063                                        gen_rtx_REG (SImode,
4064                                                     GP_ARG_MIN_REG
4065                                                     + align_words + k),
4066                                        k == 0 ? const0_rtx : GEN_INT (k*4));
4067
4068       return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k, rtlvec));
4069   }
4070
4071   return NULL_RTX;
4072 }
4073
4074 /* Determine where to put an argument to a function.
4075    Value is zero to push the argument on the stack,
4076    or a hard register in which to store the argument.
4077
4078    MODE is the argument's machine mode.
4079    TYPE is the data type of the argument (as a tree).
4080     This is null for libcalls where that information may
4081     not be available.
4082    CUM is a variable of type CUMULATIVE_ARGS which gives info about
4083     the preceding args and about the function being called.
4084    NAMED is nonzero if this argument is a named parameter
4085     (otherwise it is an extra parameter matching an ellipsis).
4086
4087    On RS/6000 the first eight words of non-FP are normally in registers
4088    and the rest are pushed.  Under AIX, the first 13 FP args are in registers.
4089    Under V.4, the first 8 FP args are in registers.
4090
4091    If this is floating-point and no prototype is specified, we use
4092    both an FP and integer register (or possibly FP reg and stack).  Library
4093    functions (when CALL_LIBCALL is set) always have the proper types for args,
4094    so we can pass the FP value just in one register.  emit_library_function
4095    doesn't support PARALLEL anyway.  */
4096
4097 struct rtx_def *
4098 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
4099               tree type, int named)
4100 {
4101   enum rs6000_abi abi = DEFAULT_ABI;
4102
4103   /* Return a marker to indicate whether CR1 needs to set or clear the
4104      bit that V.4 uses to say fp args were passed in registers.
4105      Assume that we don't need the marker for software floating point,
4106      or compiler generated library calls.  */
4107   if (mode == VOIDmode)
4108     {
4109       if (abi == ABI_V4
4110           && cum->nargs_prototype < 0
4111           && (cum->call_cookie & CALL_LIBCALL) == 0
4112           && (cum->prototype || TARGET_NO_PROTOTYPE))
4113         {
4114           /* For the SPE, we need to crxor CR6 always.  */
4115           if (TARGET_SPE_ABI)
4116             return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
4117           else if (TARGET_HARD_FLOAT && TARGET_FPRS)
4118             return GEN_INT (cum->call_cookie
4119                             | ((cum->fregno == FP_ARG_MIN_REG)
4120                                ? CALL_V4_SET_FP_ARGS
4121                                : CALL_V4_CLEAR_FP_ARGS));
4122         }
4123
4124       return GEN_INT (cum->call_cookie);
4125     }
4126
4127   if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
4128     return gen_rtx_REG (mode, cum->vregno);
4129   else if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
4130     {
4131       if (named || abi == ABI_V4)
4132         return NULL_RTX;
4133       else
4134         {
4135           /* Vector parameters to varargs functions under AIX or Darwin
4136              get passed in memory and possibly also in GPRs.  */
4137           int align, align_words;
4138           enum machine_mode part_mode = mode;
4139
4140           /* Vector parameters must be 16-byte aligned.  This places them at
4141              2 mod 4 in terms of words in 32-bit mode, since the parameter
4142              save area starts at offset 24 from the stack.  In 64-bit mode,
4143              they just have to start on an even word, since the parameter
4144              save area is 16-byte aligned.  */
4145           if (TARGET_32BIT)
4146             align = ((6 - (cum->words & 3)) & 3);
4147           else
4148             align = cum->words & 1;
4149           align_words = cum->words + align;
4150
4151           /* Out of registers?  Memory, then.  */
4152           if (align_words >= GP_ARG_NUM_REG)
4153             return NULL_RTX;
4154           
4155           /* The vector value goes in GPRs.  Only the part of the
4156              value in GPRs is reported here.  */
4157           if (align_words + CLASS_MAX_NREGS (mode, GENERAL_REGS)
4158               > GP_ARG_NUM_REG)
4159             /* Fortunately, there are only two possibilites, the value
4160                is either wholly in GPRs or half in GPRs and half not.  */
4161             part_mode = DImode;
4162           
4163           return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
4164         }
4165     }
4166   else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode))
4167     return rs6000_spe_function_arg (cum, mode, type);
4168   else if (abi == ABI_V4)
4169     {
4170       if (TARGET_HARD_FLOAT && TARGET_FPRS
4171           && (mode == SFmode || mode == DFmode))
4172         {
4173           if (cum->fregno <= FP_ARG_V4_MAX_REG)
4174             return gen_rtx_REG (mode, cum->fregno);
4175           else
4176             return NULL_RTX;
4177         }
4178       else
4179         {
4180           int n_words;
4181           int gregno = cum->sysv_gregno;
4182
4183           /* Aggregates and IEEE quad get passed by reference.  */
4184           if ((type && AGGREGATE_TYPE_P (type))
4185               || mode == TFmode)
4186             n_words = 1;
4187           else 
4188             n_words = RS6000_ARG_SIZE (mode, type);
4189
4190           /* Long long and SPE vectors are put in odd registers.  */
4191           if (n_words == 2 && (gregno & 1) == 0)
4192             gregno += 1;
4193
4194           /* Long long do not split between registers and stack.  */
4195           if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
4196             return gen_rtx_REG (mode, gregno);
4197           else
4198             return NULL_RTX;
4199         }
4200     }
4201   else
4202     {
4203       int align = (TARGET_32BIT && (cum->words & 1) != 0
4204                    && function_arg_boundary (mode, type) == 64) ? 1 : 0;
4205       int align_words = cum->words + align;
4206
4207       if (type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4208         return NULL_RTX;
4209
4210       if (TARGET_32BIT && TARGET_POWERPC64
4211           && (mode == DFmode || mode == DImode || mode == BLKmode))
4212         return rs6000_mixed_function_arg (cum, mode, type, align_words);
4213
4214       if (USE_FP_FOR_ARG_P (cum, mode, type))
4215         {
4216           if (! type
4217               || ((cum->nargs_prototype > 0)
4218                   /* IBM AIX extended its linkage convention definition always
4219                      to require FP args after register save area hole on the
4220                      stack.  */
4221                   && (DEFAULT_ABI != ABI_AIX
4222                       || ! TARGET_XL_CALL
4223                       || (align_words < GP_ARG_NUM_REG))))
4224             return gen_rtx_REG (mode, cum->fregno);
4225
4226           return gen_rtx_PARALLEL (mode,
4227             gen_rtvec (2,
4228                        gen_rtx_EXPR_LIST (VOIDmode,
4229                                 ((align_words >= GP_ARG_NUM_REG)
4230                                  ? NULL_RTX
4231                                  : (align_words
4232                                     + RS6000_ARG_SIZE (mode, type)
4233                                     > GP_ARG_NUM_REG
4234                                     /* If this is partially on the stack, then
4235                                        we only include the portion actually
4236                                        in registers here.  */
4237                                     ? gen_rtx_REG (SImode,
4238                                                GP_ARG_MIN_REG + align_words)
4239                                     : gen_rtx_REG (mode,
4240                                                GP_ARG_MIN_REG + align_words))),
4241                                 const0_rtx),
4242                        gen_rtx_EXPR_LIST (VOIDmode,
4243                                 gen_rtx_REG (mode, cum->fregno),
4244                                 const0_rtx)));
4245         }
4246       else if (align_words < GP_ARG_NUM_REG)
4247         return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
4248       else
4249         return NULL_RTX;
4250     }
4251 }
4252 \f
4253 /* For an arg passed partly in registers and partly in memory,
4254    this is the number of registers used.
4255    For args passed entirely in registers or entirely in memory, zero.  */
4256
4257 int
4258 function_arg_partial_nregs (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
4259                             tree type, int named)
4260 {
4261   if (DEFAULT_ABI == ABI_V4)
4262     return 0;
4263
4264   if (USE_FP_FOR_ARG_P (cum, mode, type)
4265       || USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
4266     {
4267       if (cum->nargs_prototype >= 0)
4268         return 0;
4269     }
4270
4271   if (cum->words < GP_ARG_NUM_REG
4272       && GP_ARG_NUM_REG < (cum->words + RS6000_ARG_SIZE (mode, type)))
4273     {
4274       int ret = GP_ARG_NUM_REG - cum->words;
4275       if (ret && TARGET_DEBUG_ARG)
4276         fprintf (stderr, "function_arg_partial_nregs: %d\n", ret);
4277
4278       return ret;
4279     }
4280
4281   return 0;
4282 }
4283 \f
4284 /* A C expression that indicates when an argument must be passed by
4285    reference.  If nonzero for an argument, a copy of that argument is
4286    made in memory and a pointer to the argument is passed instead of
4287    the argument itself.  The pointer is passed in whatever way is
4288    appropriate for passing a pointer to that type.
4289
4290    Under V.4, structures and unions are passed by reference.
4291
4292    As an extension to all ABIs, variable sized types are passed by
4293    reference.  */
4294
4295 int
4296 function_arg_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED, 
4297                                 enum machine_mode mode ATTRIBUTE_UNUSED, 
4298                                 tree type, int named ATTRIBUTE_UNUSED)
4299 {
4300   if (DEFAULT_ABI == ABI_V4
4301       && ((type && AGGREGATE_TYPE_P (type))
4302           || mode == TFmode))
4303     {
4304       if (TARGET_DEBUG_ARG)
4305         fprintf (stderr, "function_arg_pass_by_reference: aggregate\n");
4306
4307       return 1;
4308     }
4309   return type && int_size_in_bytes (type) < 0;
4310 }
4311 \f
4312 /* Perform any needed actions needed for a function that is receiving a
4313    variable number of arguments. 
4314
4315    CUM is as above.
4316
4317    MODE and TYPE are the mode and type of the current parameter.
4318
4319    PRETEND_SIZE is a variable that should be set to the amount of stack
4320    that must be pushed by the prolog to pretend that our caller pushed
4321    it.
4322
4323    Normally, this macro will push all remaining incoming registers on the
4324    stack and set PRETEND_SIZE to the length of the registers pushed.  */
4325
4326 static void
4327 setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode, 
4328                 tree type, int *pretend_size ATTRIBUTE_UNUSED, int no_rtl)
4329 {
4330   CUMULATIVE_ARGS next_cum;
4331   int reg_size = TARGET_32BIT ? 4 : 8;
4332   rtx save_area = NULL_RTX, mem;
4333   int first_reg_offset, set;
4334
4335   /* Skip the last named argument.  */
4336   next_cum = *cum;
4337   function_arg_advance (&next_cum, mode, type, 1);
4338
4339   if (DEFAULT_ABI == ABI_V4)
4340     {
4341       /* Indicate to allocate space on the stack for varargs save area.  */
4342       cfun->machine->sysv_varargs_p = 1;
4343       if (! no_rtl)
4344         save_area = plus_constant (virtual_stack_vars_rtx,
4345                                    - RS6000_VARARGS_SIZE);
4346
4347       first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
4348     }
4349   else
4350     {
4351       first_reg_offset = next_cum.words;
4352       save_area = virtual_incoming_args_rtx;
4353       cfun->machine->sysv_varargs_p = 0;
4354
4355       if (MUST_PASS_IN_STACK (mode, type))
4356         first_reg_offset += RS6000_ARG_SIZE (TYPE_MODE (type), type);
4357     }
4358
4359   set = get_varargs_alias_set ();
4360   if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG)
4361     {
4362       mem = gen_rtx_MEM (BLKmode,
4363                          plus_constant (save_area,
4364                                         first_reg_offset * reg_size)),
4365       set_mem_alias_set (mem, set);
4366       set_mem_align (mem, BITS_PER_WORD);
4367
4368       move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
4369                            GP_ARG_NUM_REG - first_reg_offset);
4370     }
4371
4372   /* Save FP registers if needed.  */
4373   if (DEFAULT_ABI == ABI_V4
4374       && TARGET_HARD_FLOAT && TARGET_FPRS
4375       && ! no_rtl
4376       && next_cum.fregno <= FP_ARG_V4_MAX_REG)
4377     {
4378       int fregno = next_cum.fregno;
4379       rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
4380       rtx lab = gen_label_rtx ();
4381       int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG) * 8);
4382
4383       emit_jump_insn (gen_rtx_SET (VOIDmode,
4384                                    pc_rtx,
4385                                    gen_rtx_IF_THEN_ELSE (VOIDmode,
4386                                             gen_rtx_NE (VOIDmode, cr1,
4387                                                         const0_rtx),
4388                                             gen_rtx_LABEL_REF (VOIDmode, lab),
4389                                             pc_rtx)));
4390
4391       while (fregno <= FP_ARG_V4_MAX_REG)
4392         {
4393           mem = gen_rtx_MEM (DFmode, plus_constant (save_area, off));
4394           set_mem_alias_set (mem, set);
4395           emit_move_insn (mem, gen_rtx_REG (DFmode, fregno));
4396           fregno++;
4397           off += 8;
4398         }
4399
4400       emit_label (lab);
4401     }
4402 }
4403
4404 /* Create the va_list data type.  */
4405
4406 static tree
4407 rs6000_build_builtin_va_list (void)
4408 {
4409   tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
4410
4411   /* For AIX, prefer 'char *' because that's what the system
4412      header files like.  */
4413   if (DEFAULT_ABI != ABI_V4)
4414     return build_pointer_type (char_type_node);
4415
4416   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
4417   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
4418
4419   f_gpr = build_decl (FIELD_DECL, get_identifier ("gpr"), 
4420                       unsigned_char_type_node);
4421   f_fpr = build_decl (FIELD_DECL, get_identifier ("fpr"), 
4422                       unsigned_char_type_node);
4423   /* Give the two bytes of padding a name, so that -Wpadded won't warn on
4424      every user file.  */
4425   f_res = build_decl (FIELD_DECL, get_identifier ("reserved"),
4426                       short_unsigned_type_node);
4427   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
4428                       ptr_type_node);
4429   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
4430                       ptr_type_node);
4431
4432   DECL_FIELD_CONTEXT (f_gpr) = record;
4433   DECL_FIELD_CONTEXT (f_fpr) = record;
4434   DECL_FIELD_CONTEXT (f_res) = record;
4435   DECL_FIELD_CONTEXT (f_ovf) = record;
4436   DECL_FIELD_CONTEXT (f_sav) = record;
4437
4438   TREE_CHAIN (record) = type_decl;
4439   TYPE_NAME (record) = type_decl;
4440   TYPE_FIELDS (record) = f_gpr;
4441   TREE_CHAIN (f_gpr) = f_fpr;
4442   TREE_CHAIN (f_fpr) = f_res;
4443   TREE_CHAIN (f_res) = f_ovf;
4444   TREE_CHAIN (f_ovf) = f_sav;
4445
4446   layout_type (record);
4447
4448   /* The correct type is an array type of one element.  */
4449   return build_array_type (record, build_index_type (size_zero_node));
4450 }
4451
4452 /* Implement va_start.  */
4453
4454 void
4455 rs6000_va_start (tree valist, rtx nextarg)
4456 {
4457   HOST_WIDE_INT words, n_gpr, n_fpr;
4458   tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
4459   tree gpr, fpr, ovf, sav, t;
4460
4461   /* Only SVR4 needs something special.  */
4462   if (DEFAULT_ABI != ABI_V4)
4463     {
4464       std_expand_builtin_va_start (valist, nextarg);
4465       return;
4466     }
4467
4468   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4469   f_fpr = TREE_CHAIN (f_gpr);
4470   f_res = TREE_CHAIN (f_fpr);
4471   f_ovf = TREE_CHAIN (f_res);
4472   f_sav = TREE_CHAIN (f_ovf);
4473
4474   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
4475   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
4476   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
4477   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
4478   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
4479
4480   /* Count number of gp and fp argument registers used.  */
4481   words = current_function_args_info.words;
4482   n_gpr = current_function_args_info.sysv_gregno - GP_ARG_MIN_REG;
4483   n_fpr = current_function_args_info.fregno - FP_ARG_MIN_REG;
4484
4485   if (TARGET_DEBUG_ARG)
4486     fprintf (stderr, "va_start: words = "HOST_WIDE_INT_PRINT_DEC", n_gpr = "
4487              HOST_WIDE_INT_PRINT_DEC", n_fpr = "HOST_WIDE_INT_PRINT_DEC"\n",
4488              words, n_gpr, n_fpr);
4489
4490   t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, build_int_2 (n_gpr, 0));
4491   TREE_SIDE_EFFECTS (t) = 1;
4492   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4493
4494   t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr, build_int_2 (n_fpr, 0));
4495   TREE_SIDE_EFFECTS (t) = 1;
4496   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4497
4498   /* Find the overflow area.  */
4499   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
4500   if (words != 0)
4501     t = build (PLUS_EXPR, TREE_TYPE (ovf), t,
4502                build_int_2 (words * UNITS_PER_WORD, 0));
4503   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
4504   TREE_SIDE_EFFECTS (t) = 1;
4505   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4506
4507   /* Find the register save area.  */
4508   t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
4509   t = build (PLUS_EXPR, TREE_TYPE (sav), t,
4510              build_int_2 (-RS6000_VARARGS_SIZE, -1));
4511   t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
4512   TREE_SIDE_EFFECTS (t) = 1;
4513   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4514 }
4515
4516 /* Implement va_arg.  */
4517
4518 rtx
4519 rs6000_va_arg (tree valist, tree type)
4520 {
4521   tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
4522   tree gpr, fpr, ovf, sav, reg, t, u;
4523   int indirect_p, size, rsize, n_reg, sav_ofs, sav_scale;
4524   rtx lab_false, lab_over, addr_rtx, r;
4525
4526   if (DEFAULT_ABI != ABI_V4)
4527     {
4528       /* Variable sized types are passed by reference.  */
4529       if (int_size_in_bytes (type) < 0)
4530         {
4531           u = build_pointer_type (type);
4532
4533           /* Args grow upward.  */
4534           t = build (POSTINCREMENT_EXPR, TREE_TYPE (valist), valist,
4535                      build_int_2 (POINTER_SIZE / BITS_PER_UNIT, 0));
4536           TREE_SIDE_EFFECTS (t) = 1;
4537
4538           t = build1 (NOP_EXPR, build_pointer_type (u), t);
4539           TREE_SIDE_EFFECTS (t) = 1;
4540
4541           t = build1 (INDIRECT_REF, u, t);
4542           TREE_SIDE_EFFECTS (t) = 1;
4543
4544           return expand_expr (t, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4545         }
4546       else
4547         {
4548           /* Altivec arguments must be aligned to a 128-bit boundary.  */
4549           if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
4550             {
4551               tree vtype = TREE_TYPE (valist);
4552               tree new_valist, modify;
4553               
4554               /* Round address up to multiple of 16.  Computes
4555                  (addr+15)&~0xf.  */
4556               new_valist = fold (build (BIT_AND_EXPR, vtype,
4557                                         fold (build (PLUS_EXPR, vtype, valist,
4558                                                      build_int_2 (15, 0))),
4559                                         build_int_2 (~15, -1)));
4560
4561               /* Update valist.  */
4562               modify = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
4563                               new_valist);
4564               TREE_SIDE_EFFECTS (modify) = 1;
4565               expand_expr (modify, const0_rtx, VOIDmode, EXPAND_NORMAL);
4566             }
4567           
4568           return std_expand_builtin_va_arg (valist, type);
4569         }
4570     }
4571
4572   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4573   f_fpr = TREE_CHAIN (f_gpr);
4574   f_res = TREE_CHAIN (f_fpr);
4575   f_ovf = TREE_CHAIN (f_res);
4576   f_sav = TREE_CHAIN (f_ovf);
4577
4578   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
4579   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
4580   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
4581   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
4582   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
4583
4584   size = int_size_in_bytes (type);
4585   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4586
4587   if (AGGREGATE_TYPE_P (type) || TYPE_MODE (type) == TFmode)
4588     {
4589       /* Aggregates and long doubles are passed by reference.  */
4590       indirect_p = 1;
4591       reg = gpr;
4592       n_reg = 1;
4593       sav_ofs = 0;
4594       sav_scale = 4;
4595       size = UNITS_PER_WORD;
4596       rsize = 1;
4597     }
4598   else if (FLOAT_TYPE_P (type) && TARGET_HARD_FLOAT && TARGET_FPRS)
4599     {
4600       /* FP args go in FP registers, if present.  */
4601       indirect_p = 0;
4602       reg = fpr;
4603       n_reg = 1;
4604       sav_ofs = 8*4;
4605       sav_scale = 8;
4606     }
4607   else
4608     {
4609       /* Otherwise into GP registers.  */
4610       indirect_p = 0;
4611       reg = gpr;
4612       n_reg = rsize;
4613       sav_ofs = 0;
4614       sav_scale = 4;
4615     }
4616
4617   /* Pull the value out of the saved registers ...  */
4618
4619   lab_false = gen_label_rtx ();
4620   lab_over = gen_label_rtx ();
4621   addr_rtx = gen_reg_rtx (Pmode);
4622
4623   /*  AltiVec vectors never go in registers.  */
4624   if (!TARGET_ALTIVEC || TREE_CODE (type) != VECTOR_TYPE)
4625     {
4626       TREE_THIS_VOLATILE (reg) = 1;
4627       emit_cmp_and_jump_insns
4628         (expand_expr (reg, NULL_RTX, QImode, EXPAND_NORMAL),
4629          GEN_INT (8 - n_reg + 1), GE, const1_rtx, QImode, 1,
4630          lab_false);
4631
4632       /* Long long is aligned in the registers.  */
4633       if (n_reg > 1)
4634         {
4635           u = build (BIT_AND_EXPR, TREE_TYPE (reg), reg,
4636                      build_int_2 (n_reg - 1, 0));
4637           u = build (PLUS_EXPR, TREE_TYPE (reg), reg, u);
4638           u = build (MODIFY_EXPR, TREE_TYPE (reg), reg, u);
4639           TREE_SIDE_EFFECTS (u) = 1;
4640           expand_expr (u, const0_rtx, VOIDmode, EXPAND_NORMAL);
4641         }
4642
4643       if (sav_ofs)
4644         t = build (PLUS_EXPR, ptr_type_node, sav, build_int_2 (sav_ofs, 0));
4645       else
4646         t = sav;
4647
4648       u = build (POSTINCREMENT_EXPR, TREE_TYPE (reg), reg,
4649                  build_int_2 (n_reg, 0));
4650       TREE_SIDE_EFFECTS (u) = 1;
4651
4652       u = build1 (CONVERT_EXPR, integer_type_node, u);
4653       TREE_SIDE_EFFECTS (u) = 1;
4654
4655       u = build (MULT_EXPR, integer_type_node, u, build_int_2 (sav_scale, 0));
4656       TREE_SIDE_EFFECTS (u) = 1;
4657
4658       t = build (PLUS_EXPR, ptr_type_node, t, u);
4659       TREE_SIDE_EFFECTS (t) = 1;
4660
4661       r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4662       if (r != addr_rtx)
4663         emit_move_insn (addr_rtx, r);
4664
4665       emit_jump_insn (gen_jump (lab_over));
4666       emit_barrier ();
4667     }
4668
4669   emit_label (lab_false);
4670
4671   /* ... otherwise out of the overflow area.  */
4672
4673   /* Make sure we don't find reg 7 for the next int arg.
4674
4675      All AltiVec vectors go in the overflow area.  So in the AltiVec
4676      case we need to get the vectors from the overflow area, but
4677      remember where the GPRs and FPRs are.  */
4678   if (n_reg > 1 && (TREE_CODE (type) != VECTOR_TYPE
4679                     || !TARGET_ALTIVEC))
4680     {
4681       t = build (MODIFY_EXPR, TREE_TYPE (reg), reg, build_int_2 (8, 0));
4682       TREE_SIDE_EFFECTS (t) = 1;
4683       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4684     }
4685
4686   /* Care for on-stack alignment if needed.  */
4687   if (rsize <= 1)
4688     t = ovf;
4689   else
4690     {
4691       int align;
4692
4693       /* AltiVec vectors are 16 byte aligned.  */
4694       if (TARGET_ALTIVEC && TREE_CODE (type) == VECTOR_TYPE)
4695         align = 15;
4696       else
4697         align = 7;
4698
4699       t = build (PLUS_EXPR, TREE_TYPE (ovf), ovf, build_int_2 (align, 0));
4700       t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-align-1, -1));
4701     }
4702   t = save_expr (t);
4703
4704   r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
4705   if (r != addr_rtx)
4706     emit_move_insn (addr_rtx, r);
4707
4708   t = build (PLUS_EXPR, TREE_TYPE (t), t, build_int_2 (size, 0));
4709   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
4710   TREE_SIDE_EFFECTS (t) = 1;
4711   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4712
4713   emit_label (lab_over);
4714
4715   if (indirect_p)
4716     {
4717       r = gen_rtx_MEM (Pmode, addr_rtx);
4718       set_mem_alias_set (r, get_varargs_alias_set ());
4719       emit_move_insn (addr_rtx, r);
4720     }
4721
4722   return addr_rtx;
4723 }
4724
4725 /* Builtins.  */
4726
4727 #define def_builtin(MASK, NAME, TYPE, CODE)                     \
4728 do {                                                            \
4729   if ((MASK) & target_flags)                                    \
4730     builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD,      \
4731                       NULL, NULL_TREE);                         \
4732 } while (0)
4733
4734 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc).  */
4735
4736 static const struct builtin_description bdesc_3arg[] =
4737 {
4738   { MASK_ALTIVEC, CODE_FOR_altivec_vmaddfp, "__builtin_altivec_vmaddfp", ALTIVEC_BUILTIN_VMADDFP },
4739   { MASK_ALTIVEC, CODE_FOR_altivec_vmhaddshs, "__builtin_altivec_vmhaddshs", ALTIVEC_BUILTIN_VMHADDSHS },
4740   { MASK_ALTIVEC, CODE_FOR_altivec_vmhraddshs, "__builtin_altivec_vmhraddshs", ALTIVEC_BUILTIN_VMHRADDSHS },
4741   { MASK_ALTIVEC, CODE_FOR_altivec_vmladduhm, "__builtin_altivec_vmladduhm", ALTIVEC_BUILTIN_VMLADDUHM},
4742   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumubm, "__builtin_altivec_vmsumubm", ALTIVEC_BUILTIN_VMSUMUBM },
4743   { MASK_ALTIVEC, CODE_FOR_altivec_vmsummbm, "__builtin_altivec_vmsummbm", ALTIVEC_BUILTIN_VMSUMMBM },
4744   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhm, "__builtin_altivec_vmsumuhm", ALTIVEC_BUILTIN_VMSUMUHM },
4745   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshm, "__builtin_altivec_vmsumshm", ALTIVEC_BUILTIN_VMSUMSHM },
4746   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhs, "__builtin_altivec_vmsumuhs", ALTIVEC_BUILTIN_VMSUMUHS },
4747   { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshs, "__builtin_altivec_vmsumshs", ALTIVEC_BUILTIN_VMSUMSHS },
4748   { MASK_ALTIVEC, CODE_FOR_altivec_vnmsubfp, "__builtin_altivec_vnmsubfp", ALTIVEC_BUILTIN_VNMSUBFP }, 
4749   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4sf, "__builtin_altivec_vperm_4sf", ALTIVEC_BUILTIN_VPERM_4SF },
4750   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4si, "__builtin_altivec_vperm_4si", ALTIVEC_BUILTIN_VPERM_4SI },
4751   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_8hi, "__builtin_altivec_vperm_8hi", ALTIVEC_BUILTIN_VPERM_8HI },
4752   { MASK_ALTIVEC, CODE_FOR_altivec_vperm_16qi, "__builtin_altivec_vperm_16qi", ALTIVEC_BUILTIN_VPERM_16QI },
4753   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4sf, "__builtin_altivec_vsel_4sf", ALTIVEC_BUILTIN_VSEL_4SF },
4754   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4si, "__builtin_altivec_vsel_4si", ALTIVEC_BUILTIN_VSEL_4SI },
4755   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_8hi, "__builtin_altivec_vsel_8hi", ALTIVEC_BUILTIN_VSEL_8HI },
4756   { MASK_ALTIVEC, CODE_FOR_altivec_vsel_16qi, "__builtin_altivec_vsel_16qi", ALTIVEC_BUILTIN_VSEL_16QI },
4757   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_16qi, "__builtin_altivec_vsldoi_16qi", ALTIVEC_BUILTIN_VSLDOI_16QI },
4758   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_8hi, "__builtin_altivec_vsldoi_8hi", ALTIVEC_BUILTIN_VSLDOI_8HI },
4759   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4si, "__builtin_altivec_vsldoi_4si", ALTIVEC_BUILTIN_VSLDOI_4SI },
4760   { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4sf, "__builtin_altivec_vsldoi_4sf", ALTIVEC_BUILTIN_VSLDOI_4SF },
4761 };
4762
4763 /* DST operations: void foo (void *, const int, const char).  */
4764
4765 static const struct builtin_description bdesc_dst[] =
4766 {
4767   { MASK_ALTIVEC, CODE_FOR_altivec_dst, "__builtin_altivec_dst", ALTIVEC_BUILTIN_DST },
4768   { MASK_ALTIVEC, CODE_FOR_altivec_dstt, "__builtin_altivec_dstt", ALTIVEC_BUILTIN_DSTT },
4769   { MASK_ALTIVEC, CODE_FOR_altivec_dstst, "__builtin_altivec_dstst", ALTIVEC_BUILTIN_DSTST },
4770   { MASK_ALTIVEC, CODE_FOR_altivec_dststt, "__builtin_altivec_dststt", ALTIVEC_BUILTIN_DSTSTT }
4771 };
4772
4773 /* Simple binary operations: VECc = foo (VECa, VECb).  */
4774
4775 static struct builtin_description bdesc_2arg[] =
4776 {
4777   { MASK_ALTIVEC, CODE_FOR_addv16qi3, "__builtin_altivec_vaddubm", ALTIVEC_BUILTIN_VADDUBM },
4778   { MASK_ALTIVEC, CODE_FOR_addv8hi3, "__builtin_altivec_vadduhm", ALTIVEC_BUILTIN_VADDUHM },
4779   { MASK_ALTIVEC, CODE_FOR_addv4si3, "__builtin_altivec_vadduwm", ALTIVEC_BUILTIN_VADDUWM },
4780   { MASK_ALTIVEC, CODE_FOR_addv4sf3, "__builtin_altivec_vaddfp", ALTIVEC_BUILTIN_VADDFP },
4781   { MASK_ALTIVEC, CODE_FOR_altivec_vaddcuw, "__builtin_altivec_vaddcuw", ALTIVEC_BUILTIN_VADDCUW },
4782   { MASK_ALTIVEC, CODE_FOR_altivec_vaddubs, "__builtin_altivec_vaddubs", ALTIVEC_BUILTIN_VADDUBS },
4783   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsbs, "__builtin_altivec_vaddsbs", ALTIVEC_BUILTIN_VADDSBS },
4784   { MASK_ALTIVEC, CODE_FOR_altivec_vadduhs, "__builtin_altivec_vadduhs", ALTIVEC_BUILTIN_VADDUHS },
4785   { MASK_ALTIVEC, CODE_FOR_altivec_vaddshs, "__builtin_altivec_vaddshs", ALTIVEC_BUILTIN_VADDSHS },
4786   { MASK_ALTIVEC, CODE_FOR_altivec_vadduws, "__builtin_altivec_vadduws", ALTIVEC_BUILTIN_VADDUWS },
4787   { MASK_ALTIVEC, CODE_FOR_altivec_vaddsws, "__builtin_altivec_vaddsws", ALTIVEC_BUILTIN_VADDSWS },
4788   { MASK_ALTIVEC, CODE_FOR_andv4si3, "__builtin_altivec_vand", ALTIVEC_BUILTIN_VAND },
4789   { MASK_ALTIVEC, CODE_FOR_altivec_vandc, "__builtin_altivec_vandc", ALTIVEC_BUILTIN_VANDC },
4790   { MASK_ALTIVEC, CODE_FOR_altivec_vavgub, "__builtin_altivec_vavgub", ALTIVEC_BUILTIN_VAVGUB },
4791   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsb, "__builtin_altivec_vavgsb", ALTIVEC_BUILTIN_VAVGSB },
4792   { MASK_ALTIVEC, CODE_FOR_altivec_vavguh, "__builtin_altivec_vavguh", ALTIVEC_BUILTIN_VAVGUH },
4793   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsh, "__builtin_altivec_vavgsh", ALTIVEC_BUILTIN_VAVGSH },
4794   { MASK_ALTIVEC, CODE_FOR_altivec_vavguw, "__builtin_altivec_vavguw", ALTIVEC_BUILTIN_VAVGUW },
4795   { MASK_ALTIVEC, CODE_FOR_altivec_vavgsw, "__builtin_altivec_vavgsw", ALTIVEC_BUILTIN_VAVGSW },
4796   { MASK_ALTIVEC, CODE_FOR_altivec_vcfux, "__builtin_altivec_vcfux", ALTIVEC_BUILTIN_VCFUX },
4797   { MASK_ALTIVEC, CODE_FOR_altivec_vcfsx, "__builtin_altivec_vcfsx", ALTIVEC_BUILTIN_VCFSX },
4798   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpbfp, "__builtin_altivec_vcmpbfp", ALTIVEC_BUILTIN_VCMPBFP },
4799   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequb, "__builtin_altivec_vcmpequb", ALTIVEC_BUILTIN_VCMPEQUB },
4800   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequh, "__builtin_altivec_vcmpequh", ALTIVEC_BUILTIN_VCMPEQUH },
4801   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequw, "__builtin_altivec_vcmpequw", ALTIVEC_BUILTIN_VCMPEQUW },
4802   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpeqfp, "__builtin_altivec_vcmpeqfp", ALTIVEC_BUILTIN_VCMPEQFP },
4803   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgefp, "__builtin_altivec_vcmpgefp", ALTIVEC_BUILTIN_VCMPGEFP },
4804   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtub, "__builtin_altivec_vcmpgtub", ALTIVEC_BUILTIN_VCMPGTUB },
4805   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsb, "__builtin_altivec_vcmpgtsb", ALTIVEC_BUILTIN_VCMPGTSB },
4806   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuh, "__builtin_altivec_vcmpgtuh", ALTIVEC_BUILTIN_VCMPGTUH },
4807   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsh, "__builtin_altivec_vcmpgtsh", ALTIVEC_BUILTIN_VCMPGTSH },
4808   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuw, "__builtin_altivec_vcmpgtuw", ALTIVEC_BUILTIN_VCMPGTUW },
4809   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsw, "__builtin_altivec_vcmpgtsw", ALTIVEC_BUILTIN_VCMPGTSW },
4810   { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtfp, "__builtin_altivec_vcmpgtfp", ALTIVEC_BUILTIN_VCMPGTFP },
4811   { MASK_ALTIVEC, CODE_FOR_altivec_vctsxs, "__builtin_altivec_vctsxs", ALTIVEC_BUILTIN_VCTSXS },
4812   { MASK_ALTIVEC, CODE_FOR_altivec_vctuxs, "__builtin_altivec_vctuxs", ALTIVEC_BUILTIN_VCTUXS },
4813   { MASK_ALTIVEC, CODE_FOR_umaxv16qi3, "__builtin_altivec_vmaxub", ALTIVEC_BUILTIN_VMAXUB },
4814   { MASK_ALTIVEC, CODE_FOR_smaxv16qi3, "__builtin_altivec_vmaxsb", ALTIVEC_BUILTIN_VMAXSB },
4815   { MASK_ALTIVEC, CODE_FOR_umaxv8hi3, "__builtin_altivec_vmaxuh", ALTIVEC_BUILTIN_VMAXUH },
4816   { MASK_ALTIVEC, CODE_FOR_smaxv8hi3, "__builtin_altivec_vmaxsh", ALTIVEC_BUILTIN_VMAXSH },
4817   { MASK_ALTIVEC, CODE_FOR_umaxv4si3, "__builtin_altivec_vmaxuw", ALTIVEC_BUILTIN_VMAXUW },
4818   { MASK_ALTIVEC, CODE_FOR_smaxv4si3, "__builtin_altivec_vmaxsw", ALTIVEC_BUILTIN_VMAXSW },
4819   { MASK_ALTIVEC, CODE_FOR_smaxv4sf3, "__builtin_altivec_vmaxfp", ALTIVEC_BUILTIN_VMAXFP },
4820   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghb, "__builtin_altivec_vmrghb", ALTIVEC_BUILTIN_VMRGHB },
4821   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghh, "__builtin_altivec_vmrghh", ALTIVEC_BUILTIN_VMRGHH },
4822   { MASK_ALTIVEC, CODE_FOR_altivec_vmrghw, "__builtin_altivec_vmrghw", ALTIVEC_BUILTIN_VMRGHW },
4823   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglb, "__builtin_altivec_vmrglb", ALTIVEC_BUILTIN_VMRGLB },
4824   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglh, "__builtin_altivec_vmrglh", ALTIVEC_BUILTIN_VMRGLH },
4825   { MASK_ALTIVEC, CODE_FOR_altivec_vmrglw, "__builtin_altivec_vmrglw", ALTIVEC_BUILTIN_VMRGLW },
4826   { MASK_ALTIVEC, CODE_FOR_uminv16qi3, "__builtin_altivec_vminub", ALTIVEC_BUILTIN_VMINUB },
4827   { MASK_ALTIVEC, CODE_FOR_sminv16qi3, "__builtin_altivec_vminsb", ALTIVEC_BUILTIN_VMINSB },
4828   { MASK_ALTIVEC, CODE_FOR_uminv8hi3, "__builtin_altivec_vminuh", ALTIVEC_BUILTIN_VMINUH },
4829   { MASK_ALTIVEC, CODE_FOR_sminv8hi3, "__builtin_altivec_vminsh", ALTIVEC_BUILTIN_VMINSH },
4830   { MASK_ALTIVEC, CODE_FOR_uminv4si3, "__builtin_altivec_vminuw", ALTIVEC_BUILTIN_VMINUW },
4831   { MASK_ALTIVEC, CODE_FOR_sminv4si3, "__builtin_altivec_vminsw", ALTIVEC_BUILTIN_VMINSW },
4832   { MASK_ALTIVEC, CODE_FOR_sminv4sf3, "__builtin_altivec_vminfp", ALTIVEC_BUILTIN_VMINFP },
4833   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleub, "__builtin_altivec_vmuleub", ALTIVEC_BUILTIN_VMULEUB },
4834   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesb, "__builtin_altivec_vmulesb", ALTIVEC_BUILTIN_VMULESB },
4835   { MASK_ALTIVEC, CODE_FOR_altivec_vmuleuh, "__builtin_altivec_vmuleuh", ALTIVEC_BUILTIN_VMULEUH },
4836   { MASK_ALTIVEC, CODE_FOR_altivec_vmulesh, "__builtin_altivec_vmulesh", ALTIVEC_BUILTIN_VMULESH },
4837   { MASK_ALTIVEC, CODE_FOR_altivec_vmuloub, "__builtin_altivec_vmuloub", ALTIVEC_BUILTIN_VMULOUB },
4838   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosb, "__builtin_altivec_vmulosb", ALTIVEC_BUILTIN_VMULOSB },
4839   { MASK_ALTIVEC, CODE_FOR_altivec_vmulouh, "__builtin_altivec_vmulouh", ALTIVEC_BUILTIN_VMULOUH },
4840   { MASK_ALTIVEC, CODE_FOR_altivec_vmulosh, "__builtin_altivec_vmulosh", ALTIVEC_BUILTIN_VMULOSH },
4841   { MASK_ALTIVEC, CODE_FOR_altivec_vnor, "__builtin_altivec_vnor", ALTIVEC_BUILTIN_VNOR },
4842   { MASK_ALTIVEC, CODE_FOR_iorv4si3, "__builtin_altivec_vor", ALTIVEC_BUILTIN_VOR },
4843   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum, "__builtin_altivec_vpkuhum", ALTIVEC_BUILTIN_VPKUHUM },
4844   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum, "__builtin_altivec_vpkuwum", ALTIVEC_BUILTIN_VPKUWUM },
4845   { MASK_ALTIVEC, CODE_FOR_altivec_vpkpx, "__builtin_altivec_vpkpx", ALTIVEC_BUILTIN_VPKPX },
4846   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhss, "__builtin_altivec_vpkuhss", ALTIVEC_BUILTIN_VPKUHSS },
4847   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshss, "__builtin_altivec_vpkshss", ALTIVEC_BUILTIN_VPKSHSS },
4848   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwss, "__builtin_altivec_vpkuwss", ALTIVEC_BUILTIN_VPKUWSS },
4849   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswss, "__builtin_altivec_vpkswss", ALTIVEC_BUILTIN_VPKSWSS },
4850   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhus, "__builtin_altivec_vpkuhus", ALTIVEC_BUILTIN_VPKUHUS },
4851   { MASK_ALTIVEC, CODE_FOR_altivec_vpkshus, "__builtin_altivec_vpkshus", ALTIVEC_BUILTIN_VPKSHUS },
4852   { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwus, "__builtin_altivec_vpkuwus", ALTIVEC_BUILTIN_VPKUWUS },
4853   { MASK_ALTIVEC, CODE_FOR_altivec_vpkswus, "__builtin_altivec_vpkswus", ALTIVEC_BUILTIN_VPKSWUS },
4854   { MASK_ALTIVEC, CODE_FOR_altivec_vrlb, "__builtin_altivec_vrlb", ALTIVEC_BUILTIN_VRLB },
4855   { MASK_ALTIVEC, CODE_FOR_altivec_vrlh, "__builtin_altivec_vrlh", ALTIVEC_BUILTIN_VRLH },
4856   { MASK_ALTIVEC, CODE_FOR_altivec_vrlw, "__builtin_altivec_vrlw", ALTIVEC_BUILTIN_VRLW },
4857   { MASK_ALTIVEC, CODE_FOR_altivec_vslb, "__builtin_altivec_vslb", ALTIVEC_BUILTIN_VSLB },
4858   { MASK_ALTIVEC, CODE_FOR_altivec_vslh, "__builtin_altivec_vslh", ALTIVEC_BUILTIN_VSLH },
4859   { MASK_ALTIVEC, CODE_FOR_altivec_vslw, "__builtin_altivec_vslw", ALTIVEC_BUILTIN_VSLW },
4860   { MASK_ALTIVEC, CODE_FOR_altivec_vsl, "__builtin_altivec_vsl", ALTIVEC_BUILTIN_VSL },
4861   { MASK_ALTIVEC, CODE_FOR_altivec_vslo, "__builtin_altivec_vslo", ALTIVEC_BUILTIN_VSLO },
4862   { MASK_ALTIVEC, CODE_FOR_altivec_vspltb, "__builtin_altivec_vspltb", ALTIVEC_BUILTIN_VSPLTB },
4863   { MASK_ALTIVEC, CODE_FOR_altivec_vsplth, "__builtin_altivec_vsplth", ALTIVEC_BUILTIN_VSPLTH },
4864   { MASK_ALTIVEC, CODE_FOR_altivec_vspltw, "__builtin_altivec_vspltw", ALTIVEC_BUILTIN_VSPLTW },
4865   { MASK_ALTIVEC, CODE_FOR_altivec_vsrb, "__builtin_altivec_vsrb", ALTIVEC_BUILTIN_VSRB },
4866   { MASK_ALTIVEC, CODE_FOR_altivec_vsrh, "__builtin_altivec_vsrh", ALTIVEC_BUILTIN_VSRH },
4867   { MASK_ALTIVEC, CODE_FOR_altivec_vsrw, "__builtin_altivec_vsrw", ALTIVEC_BUILTIN_VSRW },
4868   { MASK_ALTIVEC, CODE_FOR_altivec_vsrab, "__builtin_altivec_vsrab", ALTIVEC_BUILTIN_VSRAB },
4869   { MASK_ALTIVEC, CODE_FOR_altivec_vsrah, "__builtin_altivec_vsrah", ALTIVEC_BUILTIN_VSRAH },
4870   { MASK_ALTIVEC, CODE_FOR_altivec_vsraw, "__builtin_altivec_vsraw", ALTIVEC_BUILTIN_VSRAW },
4871   { MASK_ALTIVEC, CODE_FOR_altivec_vsr, "__builtin_altivec_vsr", ALTIVEC_BUILTIN_VSR },
4872   { MASK_ALTIVEC, CODE_FOR_altivec_vsro, "__builtin_altivec_vsro", ALTIVEC_BUILTIN_VSRO },
4873   { MASK_ALTIVEC, CODE_FOR_subv16qi3, "__builtin_altivec_vsububm", ALTIVEC_BUILTIN_VSUBUBM },
4874   { MASK_ALTIVEC, CODE_FOR_subv8hi3, "__builtin_altivec_vsubuhm", ALTIVEC_BUILTIN_VSUBUHM },
4875   { MASK_ALTIVEC, CODE_FOR_subv4si3, "__builtin_altivec_vsubuwm", ALTIVEC_BUILTIN_VSUBUWM },
4876   { MASK_ALTIVEC, CODE_FOR_subv4sf3, "__builtin_altivec_vsubfp", ALTIVEC_BUILTIN_VSUBFP },
4877   { MASK_ALTIVEC, CODE_FOR_altivec_vsubcuw, "__builtin_altivec_vsubcuw", ALTIVEC_BUILTIN_VSUBCUW },
4878   { MASK_ALTIVEC, CODE_FOR_altivec_vsububs, "__builtin_altivec_vsububs", ALTIVEC_BUILTIN_VSUBUBS },
4879   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsbs, "__builtin_altivec_vsubsbs", ALTIVEC_BUILTIN_VSUBSBS },
4880   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuhs, "__builtin_altivec_vsubuhs", ALTIVEC_BUILTIN_VSUBUHS },
4881   { MASK_ALTIVEC, CODE_FOR_altivec_vsubshs, "__builtin_altivec_vsubshs", ALTIVEC_BUILTIN_VSUBSHS },
4882   { MASK_ALTIVEC, CODE_FOR_altivec_vsubuws, "__builtin_altivec_vsubuws", ALTIVEC_BUILTIN_VSUBUWS },
4883   { MASK_ALTIVEC, CODE_FOR_altivec_vsubsws, "__builtin_altivec_vsubsws", ALTIVEC_BUILTIN_VSUBSWS },
4884   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4ubs, "__builtin_altivec_vsum4ubs", ALTIVEC_BUILTIN_VSUM4UBS },
4885   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4sbs, "__builtin_altivec_vsum4sbs", ALTIVEC_BUILTIN_VSUM4SBS },
4886   { MASK_ALTIVEC, CODE_FOR_altivec_vsum4shs, "__builtin_altivec_vsum4shs", ALTIVEC_BUILTIN_VSUM4SHS },
4887   { MASK_ALTIVEC, CODE_FOR_altivec_vsum2sws, "__builtin_altivec_vsum2sws", ALTIVEC_BUILTIN_VSUM2SWS },
4888   { MASK_ALTIVEC, CODE_FOR_altivec_vsumsws, "__builtin_altivec_vsumsws", ALTIVEC_BUILTIN_VSUMSWS },
4889   { MASK_ALTIVEC, CODE_FOR_xorv4si3, "__builtin_altivec_vxor", ALTIVEC_BUILTIN_VXOR },
4890
4891   /* Place holder, leave as first spe builtin.  */
4892   { 0, CODE_FOR_spe_evaddw, "__builtin_spe_evaddw", SPE_BUILTIN_EVADDW },
4893   { 0, CODE_FOR_spe_evand, "__builtin_spe_evand", SPE_BUILTIN_EVAND },
4894   { 0, CODE_FOR_spe_evandc, "__builtin_spe_evandc", SPE_BUILTIN_EVANDC },
4895   { 0, CODE_FOR_spe_evdivws, "__builtin_spe_evdivws", SPE_BUILTIN_EVDIVWS },
4896   { 0, CODE_FOR_spe_evdivwu, "__builtin_spe_evdivwu", SPE_BUILTIN_EVDIVWU },
4897   { 0, CODE_FOR_spe_eveqv, "__builtin_spe_eveqv", SPE_BUILTIN_EVEQV },
4898   { 0, CODE_FOR_spe_evfsadd, "__builtin_spe_evfsadd", SPE_BUILTIN_EVFSADD },
4899   { 0, CODE_FOR_spe_evfsdiv, "__builtin_spe_evfsdiv", SPE_BUILTIN_EVFSDIV },
4900   { 0, CODE_FOR_spe_evfsmul, "__builtin_spe_evfsmul", SPE_BUILTIN_EVFSMUL },
4901   { 0, CODE_FOR_spe_evfssub, "__builtin_spe_evfssub", SPE_BUILTIN_EVFSSUB },
4902   { 0, CODE_FOR_spe_evmergehi, "__builtin_spe_evmergehi", SPE_BUILTIN_EVMERGEHI },
4903   { 0, CODE_FOR_spe_evmergehilo, "__builtin_spe_evmergehilo", SPE_BUILTIN_EVMERGEHILO },
4904   { 0, CODE_FOR_spe_evmergelo, "__builtin_spe_evmergelo", SPE_BUILTIN_EVMERGELO },
4905   { 0, CODE_FOR_spe_evmergelohi, "__builtin_spe_evmergelohi", SPE_BUILTIN_EVMERGELOHI },
4906   { 0, CODE_FOR_spe_evmhegsmfaa, "__builtin_spe_evmhegsmfaa", SPE_BUILTIN_EVMHEGSMFAA },
4907   { 0, CODE_FOR_spe_evmhegsmfan, "__builtin_spe_evmhegsmfan", SPE_BUILTIN_EVMHEGSMFAN },
4908   { 0, CODE_FOR_spe_evmhegsmiaa, "__builtin_spe_evmhegsmiaa", SPE_BUILTIN_EVMHEGSMIAA },
4909   { 0, CODE_FOR_spe_evmhegsmian, "__builtin_spe_evmhegsmian", SPE_BUILTIN_EVMHEGSMIAN },
4910   { 0, CODE_FOR_spe_evmhegumiaa, "__builtin_spe_evmhegumiaa", SPE_BUILTIN_EVMHEGUMIAA },
4911   { 0, CODE_FOR_spe_evmhegumian, "__builtin_spe_evmhegumian", SPE_BUILTIN_EVMHEGUMIAN },
4912   { 0, CODE_FOR_spe_evmhesmf, "__builtin_spe_evmhesmf", SPE_BUILTIN_EVMHESMF },
4913   { 0, CODE_FOR_spe_evmhesmfa, "__builtin_spe_evmhesmfa", SPE_BUILTIN_EVMHESMFA },
4914   { 0, CODE_FOR_spe_evmhesmfaaw, "__builtin_spe_evmhesmfaaw", SPE_BUILTIN_EVMHESMFAAW },
4915   { 0, CODE_FOR_spe_evmhesmfanw, "__builtin_spe_evmhesmfanw", SPE_BUILTIN_EVMHESMFANW },
4916   { 0, CODE_FOR_spe_evmhesmi, "__builtin_spe_evmhesmi", SPE_BUILTIN_EVMHESMI },
4917   { 0, CODE_FOR_spe_evmhesmia, "__builtin_spe_evmhesmia", SPE_BUILTIN_EVMHESMIA },
4918   { 0, CODE_FOR_spe_evmhesmiaaw, "__builtin_spe_evmhesmiaaw", SPE_BUILTIN_EVMHESMIAAW },
4919   { 0, CODE_FOR_spe_evmhesmianw, "__builtin_spe_evmhesmianw", SPE_BUILTIN_EVMHESMIANW },
4920   { 0, CODE_FOR_spe_evmhessf, "__builtin_spe_evmhessf", SPE_BUILTIN_EVMHESSF },
4921   { 0, CODE_FOR_spe_evmhessfa, "__builtin_spe_evmhessfa", SPE_BUILTIN_EVMHESSFA },
4922   { 0, CODE_FOR_spe_evmhessfaaw, "__builtin_spe_evmhessfaaw", SPE_BUILTIN_EVMHESSFAAW },
4923   { 0, CODE_FOR_spe_evmhessfanw, "__builtin_spe_evmhessfanw", SPE_BUILTIN_EVMHESSFANW },
4924   { 0, CODE_FOR_spe_evmhessiaaw, "__builtin_spe_evmhessiaaw", SPE_BUILTIN_EVMHESSIAAW },
4925   { 0, CODE_FOR_spe_evmhessianw, "__builtin_spe_evmhessianw", SPE_BUILTIN_EVMHESSIANW },
4926   { 0, CODE_FOR_spe_evmheumi, "__builtin_spe_evmheumi", SPE_BUILTIN_EVMHEUMI },
4927   { 0, CODE_FOR_spe_evmheumia, "__builtin_spe_evmheumia", SPE_BUILTIN_EVMHEUMIA },
4928   { 0, CODE_FOR_spe_evmheumiaaw, "__builtin_spe_evmheumiaaw", SPE_BUILTIN_EVMHEUMIAAW },
4929   { 0, CODE_FOR_spe_evmheumianw, "__builtin_spe_evmheumianw", SPE_BUILTIN_EVMHEUMIANW },
4930   { 0, CODE_FOR_spe_evmheusiaaw, "__builtin_spe_evmheusiaaw", SPE_BUILTIN_EVMHEUSIAAW },
4931   { 0, CODE_FOR_spe_evmheusianw, "__builtin_spe_evmheusianw", SPE_BUILTIN_EVMHEUSIANW },
4932   { 0, CODE_FOR_spe_evmhogsmfaa, "__builtin_spe_evmhogsmfaa", SPE_BUILTIN_EVMHOGSMFAA },
4933   { 0, CODE_FOR_spe_evmhogsmfan, "__builtin_spe_evmhogsmfan", SPE_BUILTIN_EVMHOGSMFAN },
4934   { 0, CODE_FOR_spe_evmhogsmiaa, "__builtin_spe_evmhogsmiaa", SPE_BUILTIN_EVMHOGSMIAA },
4935   { 0, CODE_FOR_spe_evmhogsmian, "__builtin_spe_evmhogsmian", SPE_BUILTIN_EVMHOGSMIAN },
4936   { 0, CODE_FOR_spe_evmhogumiaa, "__builtin_spe_evmhogumiaa", SPE_BUILTIN_EVMHOGUMIAA },
4937   { 0, CODE_FOR_spe_evmhogumian, "__builtin_spe_evmhogumian", SPE_BUILTIN_EVMHOGUMIAN },
4938   { 0, CODE_FOR_spe_evmhosmf, "__builtin_spe_evmhosmf", SPE_BUILTIN_EVMHOSMF },
4939   { 0, CODE_FOR_spe_evmhosmfa, "__builtin_spe_evmhosmfa", SPE_BUILTIN_EVMHOSMFA },
4940   { 0, CODE_FOR_spe_evmhosmfaaw, "__builtin_spe_evmhosmfaaw", SPE_BUILTIN_EVMHOSMFAAW },
4941   { 0, CODE_FOR_spe_evmhosmfanw, "__builtin_spe_evmhosmfanw", SPE_BUILTIN_EVMHOSMFANW },
4942   { 0, CODE_FOR_spe_evmhosmi, "__builtin_spe_evmhosmi", SPE_BUILTIN_EVMHOSMI },
4943   { 0, CODE_FOR_spe_evmhosmia, "__builtin_spe_evmhosmia", SPE_BUILTIN_EVMHOSMIA },
4944   { 0, CODE_FOR_spe_evmhosmiaaw, "__builtin_spe_evmhosmiaaw", SPE_BUILTIN_EVMHOSMIAAW },
4945   { 0, CODE_FOR_spe_evmhosmianw, "__builtin_spe_evmhosmianw", SPE_BUILTIN_EVMHOSMIANW },
4946   { 0, CODE_FOR_spe_evmhossf, "__builtin_spe_evmhossf", SPE_BUILTIN_EVMHOSSF },
4947   { 0, CODE_FOR_spe_evmhossfa, "__builtin_spe_evmhossfa", SPE_BUILTIN_EVMHOSSFA },
4948   { 0, CODE_FOR_spe_evmhossfaaw, "__builtin_spe_evmhossfaaw", SPE_BUILTIN_EVMHOSSFAAW },
4949   { 0, CODE_FOR_spe_evmhossfanw, "__builtin_spe_evmhossfanw", SPE_BUILTIN_EVMHOSSFANW },
4950   { 0, CODE_FOR_spe_evmhossiaaw, "__builtin_spe_evmhossiaaw", SPE_BUILTIN_EVMHOSSIAAW },
4951   { 0, CODE_FOR_spe_evmhossianw, "__builtin_spe_evmhossianw", SPE_BUILTIN_EVMHOSSIANW },
4952   { 0, CODE_FOR_spe_evmhoumi, "__builtin_spe_evmhoumi", SPE_BUILTIN_EVMHOUMI },
4953   { 0, CODE_FOR_spe_evmhoumia, "__builtin_spe_evmhoumia", SPE_BUILTIN_EVMHOUMIA },
4954   { 0, CODE_FOR_spe_evmhoumiaaw, "__builtin_spe_evmhoumiaaw", SPE_BUILTIN_EVMHOUMIAAW },
4955   { 0, CODE_FOR_spe_evmhoumianw, "__builtin_spe_evmhoumianw", SPE_BUILTIN_EVMHOUMIANW },
4956   { 0, CODE_FOR_spe_evmhousiaaw, "__builtin_spe_evmhousiaaw", SPE_BUILTIN_EVMHOUSIAAW },
4957   { 0, CODE_FOR_spe_evmhousianw, "__builtin_spe_evmhousianw", SPE_BUILTIN_EVMHOUSIANW },
4958   { 0, CODE_FOR_spe_evmwhsmf, "__builtin_spe_evmwhsmf", SPE_BUILTIN_EVMWHSMF },
4959   { 0, CODE_FOR_spe_evmwhsmfa, "__builtin_spe_evmwhsmfa", SPE_BUILTIN_EVMWHSMFA },
4960   { 0, CODE_FOR_spe_evmwhsmi, "__builtin_spe_evmwhsmi", SPE_BUILTIN_EVMWHSMI },
4961   { 0, CODE_FOR_spe_evmwhsmia, "__builtin_spe_evmwhsmia", SPE_BUILTIN_EVMWHSMIA },
4962   { 0, CODE_FOR_spe_evmwhssf, "__builtin_spe_evmwhssf", SPE_BUILTIN_EVMWHSSF },
4963   { 0, CODE_FOR_spe_evmwhssfa, "__builtin_spe_evmwhssfa", SPE_BUILTIN_EVMWHSSFA },
4964   { 0, CODE_FOR_spe_evmwhumi, "__builtin_spe_evmwhumi", SPE_BUILTIN_EVMWHUMI },
4965   { 0, CODE_FOR_spe_evmwhumia, "__builtin_spe_evmwhumia", SPE_BUILTIN_EVMWHUMIA },
4966   { 0, CODE_FOR_spe_evmwlsmiaaw, "__builtin_spe_evmwlsmiaaw", SPE_BUILTIN_EVMWLSMIAAW },
4967   { 0, CODE_FOR_spe_evmwlsmianw, "__builtin_spe_evmwlsmianw", SPE_BUILTIN_EVMWLSMIANW },
4968   { 0, CODE_FOR_spe_evmwlssiaaw, "__builtin_spe_evmwlssiaaw", SPE_BUILTIN_EVMWLSSIAAW },
4969   { 0, CODE_FOR_spe_evmwlssianw, "__builtin_spe_evmwlssianw", SPE_BUILTIN_EVMWLSSIANW },
4970   { 0, CODE_FOR_spe_evmwlumi, "__builtin_spe_evmwlumi", SPE_BUILTIN_EVMWLUMI },
4971   { 0, CODE_FOR_spe_evmwlumia, "__builtin_spe_evmwlumia", SPE_BUILTIN_EVMWLUMIA },
4972   { 0, CODE_FOR_spe_evmwlumiaaw, "__builtin_spe_evmwlumiaaw", SPE_BUILTIN_EVMWLUMIAAW },
4973   { 0, CODE_FOR_spe_evmwlumianw, "__builtin_spe_evmwlumianw", SPE_BUILTIN_EVMWLUMIANW },
4974   { 0, CODE_FOR_spe_evmwlusiaaw, "__builtin_spe_evmwlusiaaw", SPE_BUILTIN_EVMWLUSIAAW },
4975   { 0, CODE_FOR_spe_evmwlusianw, "__builtin_spe_evmwlusianw", SPE_BUILTIN_EVMWLUSIANW },
4976   { 0, CODE_FOR_spe_evmwsmf, "__builtin_spe_evmwsmf", SPE_BUILTIN_EVMWSMF },
4977   { 0, CODE_FOR_spe_evmwsmfa, "__builtin_spe_evmwsmfa", SPE_BUILTIN_EVMWSMFA },
4978   { 0, CODE_FOR_spe_evmwsmfaa, "__builtin_spe_evmwsmfaa", SPE_BUILTIN_EVMWSMFAA },
4979   { 0, CODE_FOR_spe_evmwsmfan, "__builtin_spe_evmwsmfan", SPE_BUILTIN_EVMWSMFAN },
4980   { 0, CODE_FOR_spe_evmwsmi, "__builtin_spe_evmwsmi", SPE_BUILTIN_EVMWSMI },
4981   { 0, CODE_FOR_spe_evmwsmia, "__builtin_spe_evmwsmia", SPE_BUILTIN_EVMWSMIA },
4982   { 0, CODE_FOR_spe_evmwsmiaa, "__builtin_spe_evmwsmiaa", SPE_BUILTIN_EVMWSMIAA },
4983   { 0, CODE_FOR_spe_evmwsmian, "__builtin_spe_evmwsmian", SPE_BUILTIN_EVMWSMIAN },
4984   { 0, CODE_FOR_spe_evmwssf, "__builtin_spe_evmwssf", SPE_BUILTIN_EVMWSSF },
4985   { 0, CODE_FOR_spe_evmwssfa, "__builtin_spe_evmwssfa", SPE_BUILTIN_EVMWSSFA },
4986   { 0, CODE_FOR_spe_evmwssfaa, "__builtin_spe_evmwssfaa", SPE_BUILTIN_EVMWSSFAA },
4987   { 0, CODE_FOR_spe_evmwssfan, "__builtin_spe_evmwssfan", SPE_BUILTIN_EVMWSSFAN },
4988   { 0, CODE_FOR_spe_evmwumi, "__builtin_spe_evmwumi", SPE_BUILTIN_EVMWUMI },
4989   { 0, CODE_FOR_spe_evmwumia, "__builtin_spe_evmwumia", SPE_BUILTIN_EVMWUMIA },
4990   { 0, CODE_FOR_spe_evmwumiaa, "__builtin_spe_evmwumiaa", SPE_BUILTIN_EVMWUMIAA },
4991   { 0, CODE_FOR_spe_evmwumian, "__builtin_spe_evmwumian", SPE_BUILTIN_EVMWUMIAN },
4992   { 0, CODE_FOR_spe_evnand, "__builtin_spe_evnand", SPE_BUILTIN_EVNAND },
4993   { 0, CODE_FOR_spe_evnor, "__builtin_spe_evnor", SPE_BUILTIN_EVNOR },
4994   { 0, CODE_FOR_spe_evor, "__builtin_spe_evor", SPE_BUILTIN_EVOR },
4995   { 0, CODE_FOR_spe_evorc, "__builtin_spe_evorc", SPE_BUILTIN_EVORC },
4996   { 0, CODE_FOR_spe_evrlw, "__builtin_spe_evrlw", SPE_BUILTIN_EVRLW },
4997   { 0, CODE_FOR_spe_evslw, "__builtin_spe_evslw", SPE_BUILTIN_EVSLW },
4998   { 0, CODE_FOR_spe_evsrws, "__builtin_spe_evsrws", SPE_BUILTIN_EVSRWS },
4999   { 0, CODE_FOR_spe_evsrwu, "__builtin_spe_evsrwu", SPE_BUILTIN_EVSRWU },
5000   { 0, CODE_FOR_spe_evsubfw, "__builtin_spe_evsubfw", SPE_BUILTIN_EVSUBFW },
5001
5002   /* SPE binary operations expecting a 5-bit unsigned literal.  */
5003   { 0, CODE_FOR_spe_evaddiw, "__builtin_spe_evaddiw", SPE_BUILTIN_EVADDIW },
5004
5005   { 0, CODE_FOR_spe_evrlwi, "__builtin_spe_evrlwi", SPE_BUILTIN_EVRLWI },
5006   { 0, CODE_FOR_spe_evslwi, "__builtin_spe_evslwi", SPE_BUILTIN_EVSLWI },
5007   { 0, CODE_FOR_spe_evsrwis, "__builtin_spe_evsrwis", SPE_BUILTIN_EVSRWIS },
5008   { 0, CODE_FOR_spe_evsrwiu, "__builtin_spe_evsrwiu", SPE_BUILTIN_EVSRWIU },
5009   { 0, CODE_FOR_spe_evsubifw, "__builtin_spe_evsubifw", SPE_BUILTIN_EVSUBIFW },
5010   { 0, CODE_FOR_spe_evmwhssfaa, "__builtin_spe_evmwhssfaa", SPE_BUILTIN_EVMWHSSFAA },
5011   { 0, CODE_FOR_spe_evmwhssmaa, "__builtin_spe_evmwhssmaa", SPE_BUILTIN_EVMWHSSMAA },
5012   { 0, CODE_FOR_spe_evmwhsmfaa, "__builtin_spe_evmwhsmfaa", SPE_BUILTIN_EVMWHSMFAA },
5013   { 0, CODE_FOR_spe_evmwhsmiaa, "__builtin_spe_evmwhsmiaa", SPE_BUILTIN_EVMWHSMIAA },
5014   { 0, CODE_FOR_spe_evmwhusiaa, "__builtin_spe_evmwhusiaa", SPE_BUILTIN_EVMWHUSIAA },
5015   { 0, CODE_FOR_spe_evmwhumiaa, "__builtin_spe_evmwhumiaa", SPE_BUILTIN_EVMWHUMIAA },
5016   { 0, CODE_FOR_spe_evmwhssfan, "__builtin_spe_evmwhssfan", SPE_BUILTIN_EVMWHSSFAN },
5017   { 0, CODE_FOR_spe_evmwhssian, "__builtin_spe_evmwhssian", SPE_BUILTIN_EVMWHSSIAN },
5018   { 0, CODE_FOR_spe_evmwhsmfan, "__builtin_spe_evmwhsmfan", SPE_BUILTIN_EVMWHSMFAN },
5019   { 0, CODE_FOR_spe_evmwhsmian, "__builtin_spe_evmwhsmian", SPE_BUILTIN_EVMWHSMIAN },
5020   { 0, CODE_FOR_spe_evmwhusian, "__builtin_spe_evmwhusian", SPE_BUILTIN_EVMWHUSIAN },
5021   { 0, CODE_FOR_spe_evmwhumian, "__builtin_spe_evmwhumian", SPE_BUILTIN_EVMWHUMIAN },
5022   { 0, CODE_FOR_spe_evmwhgssfaa, "__builtin_spe_evmwhgssfaa", SPE_BUILTIN_EVMWHGSSFAA },
5023   { 0, CODE_FOR_spe_evmwhgsmfaa, "__builtin_spe_evmwhgsmfaa", SPE_BUILTIN_EVMWHGSMFAA },
5024   { 0, CODE_FOR_spe_evmwhgsmiaa, "__builtin_spe_evmwhgsmiaa", SPE_BUILTIN_EVMWHGSMIAA },
5025   { 0, CODE_FOR_spe_evmwhgumiaa, "__builtin_spe_evmwhgumiaa", SPE_BUILTIN_EVMWHGUMIAA },
5026   { 0, CODE_FOR_spe_evmwhgssfan, "__builtin_spe_evmwhgssfan", SPE_BUILTIN_EVMWHGSSFAN },
5027   { 0, CODE_FOR_spe_evmwhgsmfan, "__builtin_spe_evmwhgsmfan", SPE_BUILTIN_EVMWHGSMFAN },
5028   { 0, CODE_FOR_spe_evmwhgsmian, "__builtin_spe_evmwhgsmian", SPE_BUILTIN_EVMWHGSMIAN },
5029   { 0, CODE_FOR_spe_evmwhgumian, "__builtin_spe_evmwhgumian", SPE_BUILTIN_EVMWHGUMIAN },
5030   { 0, CODE_FOR_spe_brinc, "__builtin_spe_brinc", SPE_BUILTIN_BRINC },
5031
5032   /* Place-holder.  Leave as last binary SPE builtin.  */
5033   { 0, CODE_FOR_xorv2si3, "__builtin_spe_evxor", SPE_BUILTIN_EVXOR },
5034 };
5035
5036 /* AltiVec predicates.  */
5037
5038 struct builtin_description_predicates
5039 {
5040   const unsigned int mask;
5041   const enum insn_code icode;
5042   const char *opcode;
5043   const char *const name;
5044   const enum rs6000_builtins code;
5045 };
5046
5047 static const struct builtin_description_predicates bdesc_altivec_preds[] =
5048 {
5049   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpbfp.", "__builtin_altivec_vcmpbfp_p", ALTIVEC_BUILTIN_VCMPBFP_P },
5050   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpeqfp.", "__builtin_altivec_vcmpeqfp_p", ALTIVEC_BUILTIN_VCMPEQFP_P },
5051   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgefp.", "__builtin_altivec_vcmpgefp_p", ALTIVEC_BUILTIN_VCMPGEFP_P },
5052   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgtfp.", "__builtin_altivec_vcmpgtfp_p", ALTIVEC_BUILTIN_VCMPGTFP_P },
5053   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpequw.", "__builtin_altivec_vcmpequw_p", ALTIVEC_BUILTIN_VCMPEQUW_P },
5054   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtsw.", "__builtin_altivec_vcmpgtsw_p", ALTIVEC_BUILTIN_VCMPGTSW_P },
5055   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtuw.", "__builtin_altivec_vcmpgtuw_p", ALTIVEC_BUILTIN_VCMPGTUW_P },
5056   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtuh.", "__builtin_altivec_vcmpgtuh_p", ALTIVEC_BUILTIN_VCMPGTUH_P },
5057   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtsh.", "__builtin_altivec_vcmpgtsh_p", ALTIVEC_BUILTIN_VCMPGTSH_P },
5058   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpequh.", "__builtin_altivec_vcmpequh_p", ALTIVEC_BUILTIN_VCMPEQUH_P },
5059   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpequb.", "__builtin_altivec_vcmpequb_p", ALTIVEC_BUILTIN_VCMPEQUB_P },
5060   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtsb.", "__builtin_altivec_vcmpgtsb_p", ALTIVEC_BUILTIN_VCMPGTSB_P },
5061   { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtub.", "__builtin_altivec_vcmpgtub_p", ALTIVEC_BUILTIN_VCMPGTUB_P }
5062 };
5063
5064 /* SPE predicates.  */
5065 static struct builtin_description bdesc_spe_predicates[] =
5066 {
5067   /* Place-holder.  Leave as first.  */
5068   { 0, CODE_FOR_spe_evcmpeq, "__builtin_spe_evcmpeq", SPE_BUILTIN_EVCMPEQ },
5069   { 0, CODE_FOR_spe_evcmpgts, "__builtin_spe_evcmpgts", SPE_BUILTIN_EVCMPGTS },
5070   { 0, CODE_FOR_spe_evcmpgtu, "__builtin_spe_evcmpgtu", SPE_BUILTIN_EVCMPGTU },
5071   { 0, CODE_FOR_spe_evcmplts, "__builtin_spe_evcmplts", SPE_BUILTIN_EVCMPLTS },
5072   { 0, CODE_FOR_spe_evcmpltu, "__builtin_spe_evcmpltu", SPE_BUILTIN_EVCMPLTU },
5073   { 0, CODE_FOR_spe_evfscmpeq, "__builtin_spe_evfscmpeq", SPE_BUILTIN_EVFSCMPEQ },
5074   { 0, CODE_FOR_spe_evfscmpgt, "__builtin_spe_evfscmpgt", SPE_BUILTIN_EVFSCMPGT },
5075   { 0, CODE_FOR_spe_evfscmplt, "__builtin_spe_evfscmplt", SPE_BUILTIN_EVFSCMPLT },
5076   { 0, CODE_FOR_spe_evfststeq, "__builtin_spe_evfststeq", SPE_BUILTIN_EVFSTSTEQ },
5077   { 0, CODE_FOR_spe_evfststgt, "__builtin_spe_evfststgt", SPE_BUILTIN_EVFSTSTGT },
5078   /* Place-holder.  Leave as last.  */
5079   { 0, CODE_FOR_spe_evfststlt, "__builtin_spe_evfststlt", SPE_BUILTIN_EVFSTSTLT },
5080 };
5081
5082 /* SPE evsel predicates.  */
5083 static struct builtin_description bdesc_spe_evsel[] =
5084 {
5085   /* Place-holder.  Leave as first.  */
5086   { 0, CODE_FOR_spe_evcmpgts, "__builtin_spe_evsel_gts", SPE_BUILTIN_EVSEL_CMPGTS },
5087   { 0, CODE_FOR_spe_evcmpgtu, "__builtin_spe_evsel_gtu", SPE_BUILTIN_EVSEL_CMPGTU },
5088   { 0, CODE_FOR_spe_evcmplts, "__builtin_spe_evsel_lts", SPE_BUILTIN_EVSEL_CMPLTS },
5089   { 0, CODE_FOR_spe_evcmpltu, "__builtin_spe_evsel_ltu", SPE_BUILTIN_EVSEL_CMPLTU },
5090   { 0, CODE_FOR_spe_evcmpeq, "__builtin_spe_evsel_eq", SPE_BUILTIN_EVSEL_CMPEQ },
5091   { 0, CODE_FOR_spe_evfscmpgt, "__builtin_spe_evsel_fsgt", SPE_BUILTIN_EVSEL_FSCMPGT },
5092   { 0, CODE_FOR_spe_evfscmplt, "__builtin_spe_evsel_fslt", SPE_BUILTIN_EVSEL_FSCMPLT },
5093   { 0, CODE_FOR_spe_evfscmpeq, "__builtin_spe_evsel_fseq", SPE_BUILTIN_EVSEL_FSCMPEQ },
5094   { 0, CODE_FOR_spe_evfststgt, "__builtin_spe_evsel_fststgt", SPE_BUILTIN_EVSEL_FSTSTGT },
5095   { 0, CODE_FOR_spe_evfststlt, "__builtin_spe_evsel_fststlt", SPE_BUILTIN_EVSEL_FSTSTLT },
5096   /* Place-holder.  Leave as last.  */
5097   { 0, CODE_FOR_spe_evfststeq, "__builtin_spe_evsel_fststeq", SPE_BUILTIN_EVSEL_FSTSTEQ },
5098 };
5099
5100 /* ABS* operations.  */
5101
5102 static const struct builtin_description bdesc_abs[] =
5103 {
5104   { MASK_ALTIVEC, CODE_FOR_absv4si2, "__builtin_altivec_abs_v4si", ALTIVEC_BUILTIN_ABS_V4SI },
5105   { MASK_ALTIVEC, CODE_FOR_absv8hi2, "__builtin_altivec_abs_v8hi", ALTIVEC_BUILTIN_ABS_V8HI },
5106   { MASK_ALTIVEC, CODE_FOR_absv4sf2, "__builtin_altivec_abs_v4sf", ALTIVEC_BUILTIN_ABS_V4SF },
5107   { MASK_ALTIVEC, CODE_FOR_absv16qi2, "__builtin_altivec_abs_v16qi", ALTIVEC_BUILTIN_ABS_V16QI },
5108   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v4si, "__builtin_altivec_abss_v4si", ALTIVEC_BUILTIN_ABSS_V4SI },
5109   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v8hi, "__builtin_altivec_abss_v8hi", ALTIVEC_BUILTIN_ABSS_V8HI },
5110   { MASK_ALTIVEC, CODE_FOR_altivec_abss_v16qi, "__builtin_altivec_abss_v16qi", ALTIVEC_BUILTIN_ABSS_V16QI }
5111 };
5112
5113 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
5114    foo (VECa).  */
5115
5116 static struct builtin_description bdesc_1arg[] =
5117 {
5118   { MASK_ALTIVEC, CODE_FOR_altivec_vexptefp, "__builtin_altivec_vexptefp", ALTIVEC_BUILTIN_VEXPTEFP },
5119   { MASK_ALTIVEC, CODE_FOR_altivec_vlogefp, "__builtin_altivec_vlogefp", ALTIVEC_BUILTIN_VLOGEFP },
5120   { MASK_ALTIVEC, CODE_FOR_altivec_vrefp, "__builtin_altivec_vrefp", ALTIVEC_BUILTIN_VREFP },
5121   { MASK_ALTIVEC, CODE_FOR_altivec_vrfim, "__builtin_altivec_vrfim", ALTIVEC_BUILTIN_VRFIM },
5122   { MASK_ALTIVEC, CODE_FOR_altivec_vrfin, "__builtin_altivec_vrfin", ALTIVEC_BUILTIN_VRFIN },
5123   { MASK_ALTIVEC, CODE_FOR_altivec_vrfip, "__builtin_altivec_vrfip", ALTIVEC_BUILTIN_VRFIP },
5124   { MASK_ALTIVEC, CODE_FOR_ftruncv4sf2, "__builtin_altivec_vrfiz", ALTIVEC_BUILTIN_VRFIZ },
5125   { MASK_ALTIVEC, CODE_FOR_altivec_vrsqrtefp, "__builtin_altivec_vrsqrtefp", ALTIVEC_BUILTIN_VRSQRTEFP },
5126   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisb, "__builtin_altivec_vspltisb", ALTIVEC_BUILTIN_VSPLTISB },
5127   { MASK_ALTIVEC, CODE_FOR_altivec_vspltish, "__builtin_altivec_vspltish", ALTIVEC_BUILTIN_VSPLTISH },
5128   { MASK_ALTIVEC, CODE_FOR_altivec_vspltisw, "__builtin_altivec_vspltisw", ALTIVEC_BUILTIN_VSPLTISW },
5129   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsb, "__builtin_altivec_vupkhsb", ALTIVEC_BUILTIN_VUPKHSB },
5130   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhpx, "__builtin_altivec_vupkhpx", ALTIVEC_BUILTIN_VUPKHPX },
5131   { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsh, "__builtin_altivec_vupkhsh", ALTIVEC_BUILTIN_VUPKHSH },
5132   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsb, "__builtin_altivec_vupklsb", ALTIVEC_BUILTIN_VUPKLSB },
5133   { MASK_ALTIVEC, CODE_FOR_altivec_vupklpx, "__builtin_altivec_vupklpx", ALTIVEC_BUILTIN_VUPKLPX },
5134   { MASK_ALTIVEC, CODE_FOR_altivec_vupklsh, "__builtin_altivec_vupklsh", ALTIVEC_BUILTIN_VUPKLSH },
5135
5136   /* The SPE unary builtins must start with SPE_BUILTIN_EVABS and
5137      end with SPE_BUILTIN_EVSUBFUSIAAW.  */
5138   { 0, CODE_FOR_spe_evabs, "__builtin_spe_evabs", SPE_BUILTIN_EVABS },
5139   { 0, CODE_FOR_spe_evaddsmiaaw, "__builtin_spe_evaddsmiaaw", SPE_BUILTIN_EVADDSMIAAW },
5140   { 0, CODE_FOR_spe_evaddssiaaw, "__builtin_spe_evaddssiaaw", SPE_BUILTIN_EVADDSSIAAW },
5141   { 0, CODE_FOR_spe_evaddumiaaw, "__builtin_spe_evaddumiaaw", SPE_BUILTIN_EVADDUMIAAW },
5142   { 0, CODE_FOR_spe_evaddusiaaw, "__builtin_spe_evaddusiaaw", SPE_BUILTIN_EVADDUSIAAW },
5143   { 0, CODE_FOR_spe_evcntlsw, "__builtin_spe_evcntlsw", SPE_BUILTIN_EVCNTLSW },
5144   { 0, CODE_FOR_spe_evcntlzw, "__builtin_spe_evcntlzw", SPE_BUILTIN_EVCNTLZW },
5145   { 0, CODE_FOR_spe_evextsb, "__builtin_spe_evextsb", SPE_BUILTIN_EVEXTSB },
5146   { 0, CODE_FOR_spe_evextsh, "__builtin_spe_evextsh", SPE_BUILTIN_EVEXTSH },
5147   { 0, CODE_FOR_spe_evfsabs, "__builtin_spe_evfsabs", SPE_BUILTIN_EVFSABS },
5148   { 0, CODE_FOR_spe_evfscfsf, "__builtin_spe_evfscfsf", SPE_BUILTIN_EVFSCFSF },
5149   { 0, CODE_FOR_spe_evfscfsi, "__builtin_spe_evfscfsi", SPE_BUILTIN_EVFSCFSI },
5150   { 0, CODE_FOR_spe_evfscfuf, "__builtin_spe_evfscfuf", SPE_BUILTIN_EVFSCFUF },
5151   { 0, CODE_FOR_spe_evfscfui, "__builtin_spe_evfscfui", SPE_BUILTIN_EVFSCFUI },
5152   { 0, CODE_FOR_spe_evfsctsf, "__builtin_spe_evfsctsf", SPE_BUILTIN_EVFSCTSF },
5153   { 0, CODE_FOR_spe_evfsctsi, "__builtin_spe_evfsctsi", SPE_BUILTIN_EVFSCTSI },
5154   { 0, CODE_FOR_spe_evfsctsiz, "__builtin_spe_evfsctsiz", SPE_BUILTIN_EVFSCTSIZ },
5155   { 0, CODE_FOR_spe_evfsctuf, "__builtin_spe_evfsctuf", SPE_BUILTIN_EVFSCTUF },
5156   { 0, CODE_FOR_spe_evfsctui, "__builtin_spe_evfsctui", SPE_BUILTIN_EVFSCTUI },
5157   { 0, CODE_FOR_spe_evfsctuiz, "__builtin_spe_evfsctuiz", SPE_BUILTIN_EVFSCTUIZ },
5158   { 0, CODE_FOR_spe_evfsnabs, "__builtin_spe_evfsnabs", SPE_BUILTIN_EVFSNABS },
5159   { 0, CODE_FOR_spe_evfsneg, "__builtin_spe_evfsneg", SPE_BUILTIN_EVFSNEG },
5160   { 0, CODE_FOR_spe_evmra, "__builtin_spe_evmra", SPE_BUILTIN_EVMRA },
5161   { 0, CODE_FOR_spe_evneg, "__builtin_spe_evneg", SPE_BUILTIN_EVNEG },
5162   { 0, CODE_FOR_spe_evrndw, "__builtin_spe_evrndw", SPE_BUILTIN_EVRNDW },
5163   { 0, CODE_FOR_spe_evsubfsmiaaw, "__builtin_spe_evsubfsmiaaw", SPE_BUILTIN_EVSUBFSMIAAW },
5164   { 0, CODE_FOR_spe_evsubfssiaaw, "__builtin_spe_evsubfssiaaw", SPE_BUILTIN_EVSUBFSSIAAW },
5165   { 0, CODE_FOR_spe_evsubfumiaaw, "__builtin_spe_evsubfumiaaw", SPE_BUILTIN_EVSUBFUMIAAW },
5166
5167   /* Place-holder.  Leave as last unary SPE builtin.  */
5168   { 0, CODE_FOR_spe_evsubfusiaaw, "__builtin_spe_evsubfusiaaw", SPE_BUILTIN_EVSUBFUSIAAW },
5169 };
5170
5171 static rtx
5172 rs6000_expand_unop_builtin (enum insn_code icode, tree arglist, rtx target)
5173 {
5174   rtx pat;
5175   tree arg0 = TREE_VALUE (arglist);
5176   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5177   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5178   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5179
5180   if (icode == CODE_FOR_nothing)
5181     /* Builtin not supported on this processor.  */
5182     return 0;
5183
5184   /* If we got invalid arguments bail out before generating bad rtl.  */
5185   if (arg0 == error_mark_node)
5186     return const0_rtx;
5187
5188   if (icode == CODE_FOR_altivec_vspltisb
5189       || icode == CODE_FOR_altivec_vspltish
5190       || icode == CODE_FOR_altivec_vspltisw
5191       || icode == CODE_FOR_spe_evsplatfi
5192       || icode == CODE_FOR_spe_evsplati)
5193     {
5194       /* Only allow 5-bit *signed* literals.  */
5195       if (GET_CODE (op0) != CONST_INT
5196           || INTVAL (op0) > 0x1f
5197           || INTVAL (op0) < -0x1f)
5198         {
5199           error ("argument 1 must be a 5-bit signed literal");
5200           return const0_rtx;
5201         }
5202     }
5203
5204   if (target == 0
5205       || GET_MODE (target) != tmode
5206       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5207     target = gen_reg_rtx (tmode);
5208
5209   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5210     op0 = copy_to_mode_reg (mode0, op0);
5211
5212   pat = GEN_FCN (icode) (target, op0);
5213   if (! pat)
5214     return 0;
5215   emit_insn (pat);
5216
5217   return target;
5218 }
5219
5220 static rtx
5221 altivec_expand_abs_builtin (enum insn_code icode, tree arglist, rtx target)
5222 {
5223   rtx pat, scratch1, scratch2;
5224   tree arg0 = TREE_VALUE (arglist);
5225   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5226   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5227   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5228
5229   /* If we have invalid arguments, bail out before generating bad rtl.  */
5230   if (arg0 == error_mark_node)
5231     return const0_rtx;
5232
5233   if (target == 0
5234       || GET_MODE (target) != tmode
5235       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5236     target = gen_reg_rtx (tmode);
5237
5238   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5239     op0 = copy_to_mode_reg (mode0, op0);
5240
5241   scratch1 = gen_reg_rtx (mode0);
5242   scratch2 = gen_reg_rtx (mode0);
5243
5244   pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
5245   if (! pat)
5246     return 0;
5247   emit_insn (pat);
5248
5249   return target;
5250 }
5251
5252 static rtx
5253 rs6000_expand_binop_builtin (enum insn_code icode, tree arglist, rtx target)
5254 {
5255   rtx pat;
5256   tree arg0 = TREE_VALUE (arglist);
5257   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5258   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5259   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5260   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5261   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5262   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5263
5264   if (icode == CODE_FOR_nothing)
5265     /* Builtin not supported on this processor.  */
5266     return 0;
5267
5268   /* If we got invalid arguments bail out before generating bad rtl.  */
5269   if (arg0 == error_mark_node || arg1 == error_mark_node)
5270     return const0_rtx;
5271
5272   if (icode == CODE_FOR_altivec_vcfux
5273       || icode == CODE_FOR_altivec_vcfsx
5274       || icode == CODE_FOR_altivec_vctsxs
5275       || icode == CODE_FOR_altivec_vctuxs
5276       || icode == CODE_FOR_altivec_vspltb
5277       || icode == CODE_FOR_altivec_vsplth
5278       || icode == CODE_FOR_altivec_vspltw
5279       || icode == CODE_FOR_spe_evaddiw
5280       || icode == CODE_FOR_spe_evldd
5281       || icode == CODE_FOR_spe_evldh
5282       || icode == CODE_FOR_spe_evldw
5283       || icode == CODE_FOR_spe_evlhhesplat
5284       || icode == CODE_FOR_spe_evlhhossplat
5285       || icode == CODE_FOR_spe_evlhhousplat
5286       || icode == CODE_FOR_spe_evlwhe
5287       || icode == CODE_FOR_spe_evlwhos
5288       || icode == CODE_FOR_spe_evlwhou
5289       || icode == CODE_FOR_spe_evlwhsplat
5290       || icode == CODE_FOR_spe_evlwwsplat
5291       || icode == CODE_FOR_spe_evrlwi
5292       || icode == CODE_FOR_spe_evslwi
5293       || icode == CODE_FOR_spe_evsrwis
5294       || icode == CODE_FOR_spe_evsubifw
5295       || icode == CODE_FOR_spe_evsrwiu)
5296     {
5297       /* Only allow 5-bit unsigned literals.  */
5298       if (TREE_CODE (arg1) != INTEGER_CST
5299           || TREE_INT_CST_LOW (arg1) & ~0x1f)
5300         {
5301           error ("argument 2 must be a 5-bit unsigned literal");
5302           return const0_rtx;
5303         }
5304     }
5305
5306   if (target == 0
5307       || GET_MODE (target) != tmode
5308       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5309     target = gen_reg_rtx (tmode);
5310
5311   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5312     op0 = copy_to_mode_reg (mode0, op0);
5313   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5314     op1 = copy_to_mode_reg (mode1, op1);
5315
5316   pat = GEN_FCN (icode) (target, op0, op1);
5317   if (! pat)
5318     return 0;
5319   emit_insn (pat);
5320
5321   return target;
5322 }
5323
5324 static rtx
5325 altivec_expand_predicate_builtin (enum insn_code icode, const char *opcode, 
5326                                   tree arglist, rtx target)
5327 {
5328   rtx pat, scratch;
5329   tree cr6_form = TREE_VALUE (arglist);
5330   tree arg0 = TREE_VALUE (TREE_CHAIN (arglist));
5331   tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5332   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5333   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5334   enum machine_mode tmode = SImode;
5335   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5336   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5337   int cr6_form_int;
5338
5339   if (TREE_CODE (cr6_form) != INTEGER_CST)
5340     {
5341       error ("argument 1 of __builtin_altivec_predicate must be a constant");
5342       return const0_rtx;
5343     }
5344   else
5345     cr6_form_int = TREE_INT_CST_LOW (cr6_form);
5346
5347   if (mode0 != mode1)
5348     abort ();
5349
5350   /* If we have invalid arguments, bail out before generating bad rtl.  */
5351   if (arg0 == error_mark_node || arg1 == error_mark_node)
5352     return const0_rtx;
5353
5354   if (target == 0
5355       || GET_MODE (target) != tmode
5356       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5357     target = gen_reg_rtx (tmode);
5358
5359   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5360     op0 = copy_to_mode_reg (mode0, op0);
5361   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5362     op1 = copy_to_mode_reg (mode1, op1);
5363
5364   scratch = gen_reg_rtx (mode0);
5365
5366   pat = GEN_FCN (icode) (scratch, op0, op1,
5367                          gen_rtx (SYMBOL_REF, Pmode, opcode));
5368   if (! pat)
5369     return 0;
5370   emit_insn (pat);
5371
5372   /* The vec_any* and vec_all* predicates use the same opcodes for two
5373      different operations, but the bits in CR6 will be different
5374      depending on what information we want.  So we have to play tricks
5375      with CR6 to get the right bits out.
5376
5377      If you think this is disgusting, look at the specs for the
5378      AltiVec predicates.  */
5379
5380      switch (cr6_form_int)
5381        {
5382        case 0:
5383          emit_insn (gen_cr6_test_for_zero (target));
5384          break;
5385        case 1:
5386          emit_insn (gen_cr6_test_for_zero_reverse (target));
5387          break;
5388        case 2:
5389          emit_insn (gen_cr6_test_for_lt (target));
5390          break;
5391        case 3:
5392          emit_insn (gen_cr6_test_for_lt_reverse (target));
5393          break;
5394        default:
5395          error ("argument 1 of __builtin_altivec_predicate is out of range");
5396          break;
5397        }
5398
5399   return target;
5400 }
5401
5402 static rtx
5403 altivec_expand_lv_builtin (enum insn_code icode, tree arglist, rtx target)
5404 {
5405   rtx pat, addr;
5406   tree arg0 = TREE_VALUE (arglist);
5407   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5408   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5409   enum machine_mode mode0 = Pmode;
5410   enum machine_mode mode1 = Pmode;
5411   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5412   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5413
5414   if (icode == CODE_FOR_nothing)
5415     /* Builtin not supported on this processor.  */
5416     return 0;
5417
5418   /* If we got invalid arguments bail out before generating bad rtl.  */
5419   if (arg0 == error_mark_node || arg1 == error_mark_node)
5420     return const0_rtx;
5421
5422   if (target == 0
5423       || GET_MODE (target) != tmode
5424       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5425     target = gen_reg_rtx (tmode);
5426
5427   op1 = copy_to_mode_reg (mode1, op1); 
5428
5429   if (op0 == const0_rtx)
5430     {
5431       addr = gen_rtx_MEM (tmode, op1);
5432     }
5433   else
5434     {
5435       op0 = copy_to_mode_reg (mode0, op0);
5436       addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
5437     }
5438
5439   pat = GEN_FCN (icode) (target, addr);
5440
5441   if (! pat)
5442     return 0;
5443   emit_insn (pat);
5444
5445   return target;
5446 }
5447
5448 static rtx
5449 altivec_expand_stv_builtin (enum insn_code icode, tree arglist)
5450 {
5451   tree arg0 = TREE_VALUE (arglist);
5452   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5453   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5454   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5455   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5456   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
5457   rtx pat, addr;
5458   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5459   enum machine_mode mode1 = Pmode;
5460   enum machine_mode mode2 = Pmode;
5461
5462   /* Invalid arguments.  Bail before doing anything stoopid!  */
5463   if (arg0 == error_mark_node
5464       || arg1 == error_mark_node
5465       || arg2 == error_mark_node)
5466     return const0_rtx;
5467
5468   if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
5469     op0 = copy_to_mode_reg (tmode, op0);
5470
5471   op2 = copy_to_mode_reg (mode2, op2); 
5472
5473   if (op1 == const0_rtx)
5474     {
5475       addr = gen_rtx_MEM (tmode, op2);
5476     }
5477   else
5478     {
5479       op1 = copy_to_mode_reg (mode1, op1);
5480       addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
5481     }
5482
5483   pat = GEN_FCN (icode) (addr, op0);
5484   if (pat)
5485     emit_insn (pat);
5486   return NULL_RTX;
5487 }
5488
5489 static rtx
5490 rs6000_expand_ternop_builtin (enum insn_code icode, tree arglist, rtx target)
5491 {
5492   rtx pat;
5493   tree arg0 = TREE_VALUE (arglist);
5494   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5495   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5496   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5497   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5498   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
5499   enum machine_mode tmode = insn_data[icode].operand[0].mode;
5500   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5501   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5502   enum machine_mode mode2 = insn_data[icode].operand[3].mode;
5503
5504   if (icode == CODE_FOR_nothing)
5505     /* Builtin not supported on this processor.  */
5506     return 0;
5507
5508   /* If we got invalid arguments bail out before generating bad rtl.  */
5509   if (arg0 == error_mark_node
5510       || arg1 == error_mark_node
5511       || arg2 == error_mark_node)
5512     return const0_rtx;
5513
5514   if (icode == CODE_FOR_altivec_vsldoi_4sf
5515       || icode == CODE_FOR_altivec_vsldoi_4si
5516       || icode == CODE_FOR_altivec_vsldoi_8hi
5517       || icode == CODE_FOR_altivec_vsldoi_16qi)
5518     {
5519       /* Only allow 4-bit unsigned literals.  */
5520       if (TREE_CODE (arg2) != INTEGER_CST
5521           || TREE_INT_CST_LOW (arg2) & ~0xf)
5522         {
5523           error ("argument 3 must be a 4-bit unsigned literal");
5524           return const0_rtx;
5525         }
5526     }
5527
5528   if (target == 0
5529       || GET_MODE (target) != tmode
5530       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5531     target = gen_reg_rtx (tmode);
5532
5533   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5534     op0 = copy_to_mode_reg (mode0, op0);
5535   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5536     op1 = copy_to_mode_reg (mode1, op1);
5537   if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
5538     op2 = copy_to_mode_reg (mode2, op2);
5539
5540   pat = GEN_FCN (icode) (target, op0, op1, op2);
5541   if (! pat)
5542     return 0;
5543   emit_insn (pat);
5544
5545   return target;
5546 }
5547
5548 /* Expand the lvx builtins.  */
5549 static rtx
5550 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
5551 {
5552   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5553   tree arglist = TREE_OPERAND (exp, 1);
5554   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5555   tree arg0;
5556   enum machine_mode tmode, mode0;
5557   rtx pat, op0;
5558   enum insn_code icode;
5559
5560   switch (fcode)
5561     {
5562     case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
5563       icode = CODE_FOR_altivec_lvx_16qi;
5564       break;
5565     case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
5566       icode = CODE_FOR_altivec_lvx_8hi;
5567       break;
5568     case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
5569       icode = CODE_FOR_altivec_lvx_4si;
5570       break;
5571     case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
5572       icode = CODE_FOR_altivec_lvx_4sf;
5573       break;
5574     default:
5575       *expandedp = false;
5576       return NULL_RTX;
5577     }
5578
5579   *expandedp = true;
5580
5581   arg0 = TREE_VALUE (arglist);
5582   op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5583   tmode = insn_data[icode].operand[0].mode;
5584   mode0 = insn_data[icode].operand[1].mode;
5585
5586   if (target == 0
5587       || GET_MODE (target) != tmode
5588       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5589     target = gen_reg_rtx (tmode);
5590
5591   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5592     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
5593
5594   pat = GEN_FCN (icode) (target, op0);
5595   if (! pat)
5596     return 0;
5597   emit_insn (pat);
5598   return target;
5599 }
5600
5601 /* Expand the stvx builtins.  */
5602 static rtx
5603 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED, 
5604                            bool *expandedp)
5605 {
5606   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5607   tree arglist = TREE_OPERAND (exp, 1);
5608   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5609   tree arg0, arg1;
5610   enum machine_mode mode0, mode1;
5611   rtx pat, op0, op1;
5612   enum insn_code icode;
5613
5614   switch (fcode)
5615     {
5616     case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
5617       icode = CODE_FOR_altivec_stvx_16qi;
5618       break;
5619     case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
5620       icode = CODE_FOR_altivec_stvx_8hi;
5621       break;
5622     case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
5623       icode = CODE_FOR_altivec_stvx_4si;
5624       break;
5625     case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
5626       icode = CODE_FOR_altivec_stvx_4sf;
5627       break;
5628     default:
5629       *expandedp = false;
5630       return NULL_RTX;
5631     }
5632
5633   arg0 = TREE_VALUE (arglist);
5634   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5635   op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5636   op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5637   mode0 = insn_data[icode].operand[0].mode;
5638   mode1 = insn_data[icode].operand[1].mode;
5639
5640   if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
5641     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
5642   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
5643     op1 = copy_to_mode_reg (mode1, op1);
5644
5645   pat = GEN_FCN (icode) (op0, op1);
5646   if (pat)
5647     emit_insn (pat);
5648
5649   *expandedp = true;
5650   return NULL_RTX;
5651 }
5652
5653 /* Expand the dst builtins.  */
5654 static rtx
5655 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED, 
5656                             bool *expandedp)
5657 {
5658   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5659   tree arglist = TREE_OPERAND (exp, 1);
5660   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5661   tree arg0, arg1, arg2;
5662   enum machine_mode mode0, mode1, mode2;
5663   rtx pat, op0, op1, op2;
5664   struct builtin_description *d;
5665   size_t i;
5666
5667   *expandedp = false;
5668
5669   /* Handle DST variants.  */
5670   d = (struct builtin_description *) bdesc_dst;
5671   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
5672     if (d->code == fcode)
5673       {
5674         arg0 = TREE_VALUE (arglist);
5675         arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5676         arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5677         op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5678         op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5679         op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
5680         mode0 = insn_data[d->icode].operand[0].mode;
5681         mode1 = insn_data[d->icode].operand[1].mode;
5682         mode2 = insn_data[d->icode].operand[2].mode;
5683
5684         /* Invalid arguments, bail out before generating bad rtl.  */
5685         if (arg0 == error_mark_node
5686             || arg1 == error_mark_node
5687             || arg2 == error_mark_node)
5688           return const0_rtx;
5689
5690         if (TREE_CODE (arg2) != INTEGER_CST
5691             || TREE_INT_CST_LOW (arg2) & ~0x3)
5692           {
5693             error ("argument to `%s' must be a 2-bit unsigned literal", d->name);
5694             return const0_rtx;
5695           }
5696
5697         if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
5698           op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
5699         if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
5700           op1 = copy_to_mode_reg (mode1, op1);
5701
5702         pat = GEN_FCN (d->icode) (op0, op1, op2);
5703         if (pat != 0)
5704           emit_insn (pat);
5705
5706         *expandedp = true;
5707         return NULL_RTX;
5708       }
5709
5710   return NULL_RTX;
5711 }
5712
5713 /* Expand the builtin in EXP and store the result in TARGET.  Store
5714    true in *EXPANDEDP if we found a builtin to expand.  */
5715 static rtx
5716 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
5717 {
5718   struct builtin_description *d;
5719   struct builtin_description_predicates *dp;
5720   size_t i;
5721   enum insn_code icode;
5722   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5723   tree arglist = TREE_OPERAND (exp, 1);
5724   tree arg0;
5725   rtx op0, pat;
5726   enum machine_mode tmode, mode0;
5727   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5728
5729   target = altivec_expand_ld_builtin (exp, target, expandedp);
5730   if (*expandedp)
5731     return target;
5732
5733   target = altivec_expand_st_builtin (exp, target, expandedp);
5734   if (*expandedp)
5735     return target;
5736
5737   target = altivec_expand_dst_builtin (exp, target, expandedp);
5738   if (*expandedp)
5739     return target;
5740
5741   *expandedp = true;
5742
5743   switch (fcode)
5744     {
5745     case ALTIVEC_BUILTIN_STVX:
5746       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx, arglist);
5747     case ALTIVEC_BUILTIN_STVEBX:
5748       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, arglist);
5749     case ALTIVEC_BUILTIN_STVEHX:
5750       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, arglist);
5751     case ALTIVEC_BUILTIN_STVEWX:
5752       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, arglist);
5753     case ALTIVEC_BUILTIN_STVXL:
5754       return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl, arglist);
5755
5756     case ALTIVEC_BUILTIN_MFVSCR:
5757       icode = CODE_FOR_altivec_mfvscr;
5758       tmode = insn_data[icode].operand[0].mode;
5759
5760       if (target == 0
5761           || GET_MODE (target) != tmode
5762           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5763         target = gen_reg_rtx (tmode);
5764       
5765       pat = GEN_FCN (icode) (target);
5766       if (! pat)
5767         return 0;
5768       emit_insn (pat);
5769       return target;
5770
5771     case ALTIVEC_BUILTIN_MTVSCR:
5772       icode = CODE_FOR_altivec_mtvscr;
5773       arg0 = TREE_VALUE (arglist);
5774       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5775       mode0 = insn_data[icode].operand[0].mode;
5776
5777       /* If we got invalid arguments bail out before generating bad rtl.  */
5778       if (arg0 == error_mark_node)
5779         return const0_rtx;
5780
5781       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
5782         op0 = copy_to_mode_reg (mode0, op0);
5783
5784       pat = GEN_FCN (icode) (op0);
5785       if (pat)
5786         emit_insn (pat);
5787       return NULL_RTX;
5788
5789     case ALTIVEC_BUILTIN_DSSALL:
5790       emit_insn (gen_altivec_dssall ());
5791       return NULL_RTX;
5792
5793     case ALTIVEC_BUILTIN_DSS:
5794       icode = CODE_FOR_altivec_dss;
5795       arg0 = TREE_VALUE (arglist);
5796       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5797       mode0 = insn_data[icode].operand[0].mode;
5798
5799       /* If we got invalid arguments bail out before generating bad rtl.  */
5800       if (arg0 == error_mark_node)
5801         return const0_rtx;
5802
5803       if (TREE_CODE (arg0) != INTEGER_CST
5804           || TREE_INT_CST_LOW (arg0) & ~0x3)
5805         {
5806           error ("argument to dss must be a 2-bit unsigned literal");
5807           return const0_rtx;
5808         }
5809
5810       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
5811         op0 = copy_to_mode_reg (mode0, op0);
5812
5813       emit_insn (gen_altivec_dss (op0));
5814       return NULL_RTX;
5815     }
5816
5817   /* Expand abs* operations.  */
5818   d = (struct builtin_description *) bdesc_abs;
5819   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
5820     if (d->code == fcode)
5821       return altivec_expand_abs_builtin (d->icode, arglist, target);
5822
5823   /* Expand the AltiVec predicates.  */
5824   dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
5825   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
5826     if (dp->code == fcode)
5827       return altivec_expand_predicate_builtin (dp->icode, dp->opcode, arglist, target);
5828
5829   /* LV* are funky.  We initialized them differently.  */
5830   switch (fcode)
5831     {
5832     case ALTIVEC_BUILTIN_LVSL:
5833       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
5834                                            arglist, target);
5835     case ALTIVEC_BUILTIN_LVSR:
5836       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
5837                                           arglist, target);
5838     case ALTIVEC_BUILTIN_LVEBX:
5839       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
5840                                           arglist, target);
5841     case ALTIVEC_BUILTIN_LVEHX:
5842       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
5843                                           arglist, target);
5844     case ALTIVEC_BUILTIN_LVEWX:
5845       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
5846                                           arglist, target);
5847     case ALTIVEC_BUILTIN_LVXL:
5848       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl,
5849                                           arglist, target);
5850     case ALTIVEC_BUILTIN_LVX:
5851       return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx,
5852                                           arglist, target);
5853     default:
5854       break;
5855       /* Fall through.  */
5856     }
5857
5858   *expandedp = false;
5859   return NULL_RTX;
5860 }
5861
5862 /* Binops that need to be initialized manually, but can be expanded
5863    automagically by rs6000_expand_binop_builtin.  */
5864 static struct builtin_description bdesc_2arg_spe[] =
5865 {
5866   { 0, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
5867   { 0, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
5868   { 0, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
5869   { 0, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
5870   { 0, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
5871   { 0, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
5872   { 0, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
5873   { 0, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
5874   { 0, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
5875   { 0, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
5876   { 0, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
5877   { 0, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
5878   { 0, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
5879   { 0, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
5880   { 0, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
5881   { 0, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
5882   { 0, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
5883   { 0, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
5884   { 0, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
5885   { 0, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
5886   { 0, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
5887   { 0, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
5888 };
5889
5890 /* Expand the builtin in EXP and store the result in TARGET.  Store
5891    true in *EXPANDEDP if we found a builtin to expand.
5892
5893    This expands the SPE builtins that are not simple unary and binary
5894    operations.  */
5895 static rtx
5896 spe_expand_builtin (tree exp, rtx target, bool *expandedp)
5897 {
5898   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5899   tree arglist = TREE_OPERAND (exp, 1);
5900   tree arg1, arg0;
5901   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5902   enum insn_code icode;
5903   enum machine_mode tmode, mode0;
5904   rtx pat, op0;
5905   struct builtin_description *d;
5906   size_t i;
5907
5908   *expandedp = true;
5909
5910   /* Syntax check for a 5-bit unsigned immediate.  */
5911   switch (fcode)
5912     {
5913     case SPE_BUILTIN_EVSTDD:
5914     case SPE_BUILTIN_EVSTDH:
5915     case SPE_BUILTIN_EVSTDW:
5916     case SPE_BUILTIN_EVSTWHE:
5917     case SPE_BUILTIN_EVSTWHO:
5918     case SPE_BUILTIN_EVSTWWE:
5919     case SPE_BUILTIN_EVSTWWO:
5920       arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5921       if (TREE_CODE (arg1) != INTEGER_CST
5922           || TREE_INT_CST_LOW (arg1) & ~0x1f)
5923         {
5924           error ("argument 2 must be a 5-bit unsigned literal");
5925           return const0_rtx;
5926         }
5927       break;
5928     default:
5929       break;
5930     }
5931
5932   /* The evsplat*i instructions are not quite generic.  */
5933   switch (fcode)
5934     {
5935     case SPE_BUILTIN_EVSPLATFI:
5936       return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
5937                                          arglist, target);
5938     case SPE_BUILTIN_EVSPLATI:
5939       return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
5940                                          arglist, target);
5941     default:
5942       break;
5943     }
5944
5945   d = (struct builtin_description *) bdesc_2arg_spe;
5946   for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
5947     if (d->code == fcode)
5948       return rs6000_expand_binop_builtin (d->icode, arglist, target);
5949
5950   d = (struct builtin_description *) bdesc_spe_predicates;
5951   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
5952     if (d->code == fcode)
5953       return spe_expand_predicate_builtin (d->icode, arglist, target);
5954
5955   d = (struct builtin_description *) bdesc_spe_evsel;
5956   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
5957     if (d->code == fcode)
5958       return spe_expand_evsel_builtin (d->icode, arglist, target);
5959
5960   switch (fcode)
5961     {
5962     case SPE_BUILTIN_EVSTDDX:
5963       return altivec_expand_stv_builtin (CODE_FOR_spe_evstddx, arglist);
5964     case SPE_BUILTIN_EVSTDHX:
5965       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdhx, arglist);
5966     case SPE_BUILTIN_EVSTDWX:
5967       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdwx, arglist);
5968     case SPE_BUILTIN_EVSTWHEX:
5969       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhex, arglist);
5970     case SPE_BUILTIN_EVSTWHOX:
5971       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhox, arglist);
5972     case SPE_BUILTIN_EVSTWWEX:
5973       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwex, arglist);
5974     case SPE_BUILTIN_EVSTWWOX:
5975       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwox, arglist);
5976     case SPE_BUILTIN_EVSTDD:
5977       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdd, arglist);
5978     case SPE_BUILTIN_EVSTDH:
5979       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdh, arglist);
5980     case SPE_BUILTIN_EVSTDW:
5981       return altivec_expand_stv_builtin (CODE_FOR_spe_evstdw, arglist);
5982     case SPE_BUILTIN_EVSTWHE:
5983       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhe, arglist);
5984     case SPE_BUILTIN_EVSTWHO:
5985       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwho, arglist);
5986     case SPE_BUILTIN_EVSTWWE:
5987       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwe, arglist);
5988     case SPE_BUILTIN_EVSTWWO:
5989       return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwo, arglist);
5990     case SPE_BUILTIN_MFSPEFSCR:
5991       icode = CODE_FOR_spe_mfspefscr;
5992       tmode = insn_data[icode].operand[0].mode;
5993
5994       if (target == 0
5995           || GET_MODE (target) != tmode
5996           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5997         target = gen_reg_rtx (tmode);
5998       
5999       pat = GEN_FCN (icode) (target);
6000       if (! pat)
6001         return 0;
6002       emit_insn (pat);
6003       return target;
6004     case SPE_BUILTIN_MTSPEFSCR:
6005       icode = CODE_FOR_spe_mtspefscr;
6006       arg0 = TREE_VALUE (arglist);
6007       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6008       mode0 = insn_data[icode].operand[0].mode;
6009
6010       if (arg0 == error_mark_node)
6011         return const0_rtx;
6012
6013       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
6014         op0 = copy_to_mode_reg (mode0, op0);
6015
6016       pat = GEN_FCN (icode) (op0);
6017       if (pat)
6018         emit_insn (pat);
6019       return NULL_RTX;
6020     default:
6021       break;
6022     }
6023
6024   *expandedp = false;
6025   return NULL_RTX;
6026 }
6027
6028 static rtx
6029 spe_expand_predicate_builtin (enum insn_code icode, tree arglist, rtx target)
6030 {
6031   rtx pat, scratch, tmp;
6032   tree form = TREE_VALUE (arglist);
6033   tree arg0 = TREE_VALUE (TREE_CHAIN (arglist));
6034   tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6035   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6036   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
6037   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
6038   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
6039   int form_int;
6040   enum rtx_code code;
6041
6042   if (TREE_CODE (form) != INTEGER_CST)
6043     {
6044       error ("argument 1 of __builtin_spe_predicate must be a constant");
6045       return const0_rtx;
6046     }
6047   else
6048     form_int = TREE_INT_CST_LOW (form);
6049
6050   if (mode0 != mode1)
6051     abort ();
6052
6053   if (arg0 == error_mark_node || arg1 == error_mark_node)
6054     return const0_rtx;
6055
6056   if (target == 0
6057       || GET_MODE (target) != SImode
6058       || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
6059     target = gen_reg_rtx (SImode);
6060
6061   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
6062     op0 = copy_to_mode_reg (mode0, op0);
6063   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
6064     op1 = copy_to_mode_reg (mode1, op1);
6065
6066   scratch = gen_reg_rtx (CCmode);
6067
6068   pat = GEN_FCN (icode) (scratch, op0, op1);
6069   if (! pat)
6070     return const0_rtx;
6071   emit_insn (pat);
6072
6073   /* There are 4 variants for each predicate: _any_, _all_, _upper_,
6074      _lower_.  We use one compare, but look in different bits of the
6075      CR for each variant.
6076
6077      There are 2 elements in each SPE simd type (upper/lower).  The CR
6078      bits are set as follows:
6079
6080      BIT0  | BIT 1  | BIT 2   | BIT 3
6081      U     |   L    | (U | L) | (U & L)
6082
6083      So, for an "all" relationship, BIT 3 would be set.
6084      For an "any" relationship, BIT 2 would be set.  Etc.
6085
6086      Following traditional nomenclature, these bits map to:
6087
6088      BIT0  | BIT 1  | BIT 2   | BIT 3
6089      LT    | GT     | EQ      | OV
6090
6091      Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
6092   */
6093
6094   switch (form_int)
6095     {
6096       /* All variant.  OV bit.  */
6097     case 0:
6098       /* We need to get to the OV bit, which is the ORDERED bit.  We
6099          could generate (ordered:SI (reg:CC xx) (const_int 0)), but
6100          that's ugly and will trigger a validate_condition_mode abort.
6101          So let's just use another pattern.  */
6102       emit_insn (gen_move_from_CR_ov_bit (target, scratch));
6103       return target;
6104       /* Any variant.  EQ bit.  */
6105     case 1:
6106       code = EQ;
6107       break;
6108       /* Upper variant.  LT bit.  */
6109     case 2:
6110       code = LT;
6111       break;
6112       /* Lower variant.  GT bit.  */
6113     case 3:
6114       code = GT;
6115       break;
6116     default:
6117       error ("argument 1 of __builtin_spe_predicate is out of range");
6118       return const0_rtx;
6119     }
6120
6121   tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
6122   emit_move_insn (target, tmp);
6123
6124   return target;
6125 }
6126
6127 /* The evsel builtins look like this:
6128
6129      e = __builtin_spe_evsel_OP (a, b, c, d);
6130
6131    and work like this:
6132
6133      e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
6134      e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
6135 */
6136
6137 static rtx
6138 spe_expand_evsel_builtin (enum insn_code icode, tree arglist, rtx target)
6139 {
6140   rtx pat, scratch;
6141   tree arg0 = TREE_VALUE (arglist);
6142   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6143   tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6144   tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
6145   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6146   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
6147   rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
6148   rtx op3 = expand_expr (arg3, NULL_RTX, VOIDmode, 0);
6149   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
6150   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
6151
6152   if (mode0 != mode1)
6153     abort ();
6154
6155   if (arg0 == error_mark_node || arg1 == error_mark_node
6156       || arg2 == error_mark_node || arg3 == error_mark_node)
6157     return const0_rtx;
6158
6159   if (target == 0
6160       || GET_MODE (target) != mode0
6161       || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
6162     target = gen_reg_rtx (mode0);
6163
6164   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
6165     op0 = copy_to_mode_reg (mode0, op0);
6166   if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
6167     op1 = copy_to_mode_reg (mode0, op1);
6168   if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
6169     op2 = copy_to_mode_reg (mode0, op2);
6170   if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
6171     op3 = copy_to_mode_reg (mode0, op3);
6172
6173   /* Generate the compare.  */
6174   scratch = gen_reg_rtx (CCmode);
6175   pat = GEN_FCN (icode) (scratch, op0, op1);
6176   if (! pat)
6177     return const0_rtx;
6178   emit_insn (pat);
6179
6180   if (mode0 == V2SImode)
6181     emit_insn (gen_spe_evsel (target, op2, op3, scratch));
6182   else
6183     emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
6184
6185   return target;
6186 }
6187
6188 /* Expand an expression EXP that calls a built-in function,
6189    with result going to TARGET if that's convenient
6190    (and in mode MODE if that's convenient).
6191    SUBTARGET may be used as the target for computing one of EXP's operands.
6192    IGNORE is nonzero if the value is to be ignored.  */
6193
6194 static rtx
6195 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
6196                       enum machine_mode mode ATTRIBUTE_UNUSED, 
6197                       int ignore ATTRIBUTE_UNUSED)
6198 {
6199   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6200   tree arglist = TREE_OPERAND (exp, 1);
6201   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
6202   struct builtin_description *d;
6203   size_t i;
6204   rtx ret;
6205   bool success;
6206   
6207   if (TARGET_ALTIVEC)
6208     {
6209       ret = altivec_expand_builtin (exp, target, &success);
6210
6211       if (success)
6212         return ret;
6213     }
6214   if (TARGET_SPE)
6215     {
6216       ret = spe_expand_builtin (exp, target, &success);
6217
6218       if (success)
6219         return ret;
6220     }
6221
6222   if (TARGET_ALTIVEC || TARGET_SPE)
6223     {
6224       /* Handle simple unary operations.  */
6225       d = (struct builtin_description *) bdesc_1arg;
6226       for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
6227         if (d->code == fcode)
6228           return rs6000_expand_unop_builtin (d->icode, arglist, target);
6229
6230       /* Handle simple binary operations.  */
6231       d = (struct builtin_description *) bdesc_2arg;
6232       for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
6233         if (d->code == fcode)
6234           return rs6000_expand_binop_builtin (d->icode, arglist, target);
6235
6236       /* Handle simple ternary operations.  */
6237       d = (struct builtin_description *) bdesc_3arg;
6238       for (i = 0; i < ARRAY_SIZE  (bdesc_3arg); i++, d++)
6239         if (d->code == fcode)
6240           return rs6000_expand_ternop_builtin (d->icode, arglist, target);
6241     }
6242
6243   abort ();
6244   return NULL_RTX;
6245 }
6246
6247 static void
6248 rs6000_init_builtins (void)
6249 {
6250   opaque_V2SI_type_node = copy_node (V2SI_type_node);
6251   opaque_V2SF_type_node = copy_node (V2SF_type_node);
6252   opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
6253
6254   if (TARGET_SPE)
6255     spe_init_builtins ();
6256   if (TARGET_ALTIVEC)
6257     altivec_init_builtins ();
6258   if (TARGET_ALTIVEC || TARGET_SPE)
6259     rs6000_common_init_builtins ();
6260 }
6261
6262 /* Search through a set of builtins and enable the mask bits.
6263    DESC is an array of builtins.
6264    SIZE is the total number of builtins.
6265    START is the builtin enum at which to start.
6266    END is the builtin enum at which to end.  */
6267 static void
6268 enable_mask_for_builtins (struct builtin_description *desc, int size,
6269                           enum rs6000_builtins start, 
6270                           enum rs6000_builtins end)
6271 {
6272   int i;
6273
6274   for (i = 0; i < size; ++i)
6275     if (desc[i].code == start)
6276       break;
6277
6278   if (i == size)
6279     return;
6280
6281   for (; i < size; ++i)
6282     {
6283       /* Flip all the bits on.  */
6284       desc[i].mask = target_flags;
6285       if (desc[i].code == end)
6286         break;
6287     }
6288 }
6289
6290 static void
6291 spe_init_builtins (void)
6292 {
6293   tree endlink = void_list_node;
6294   tree puint_type_node = build_pointer_type (unsigned_type_node);
6295   tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
6296   struct builtin_description *d;
6297   size_t i;
6298
6299   tree v2si_ftype_4_v2si
6300     = build_function_type
6301     (opaque_V2SI_type_node,
6302      tree_cons (NULL_TREE, opaque_V2SI_type_node,
6303                 tree_cons (NULL_TREE, opaque_V2SI_type_node,
6304                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6305                                       tree_cons (NULL_TREE, opaque_V2SI_type_node,
6306                                                  endlink)))));
6307
6308   tree v2sf_ftype_4_v2sf
6309     = build_function_type
6310     (opaque_V2SF_type_node,
6311      tree_cons (NULL_TREE, opaque_V2SF_type_node,
6312                 tree_cons (NULL_TREE, opaque_V2SF_type_node,
6313                            tree_cons (NULL_TREE, opaque_V2SF_type_node,
6314                                       tree_cons (NULL_TREE, opaque_V2SF_type_node,
6315                                                  endlink)))));
6316
6317   tree int_ftype_int_v2si_v2si
6318     = build_function_type
6319     (integer_type_node,
6320      tree_cons (NULL_TREE, integer_type_node,
6321                 tree_cons (NULL_TREE, opaque_V2SI_type_node,
6322                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6323                                       endlink))));
6324
6325   tree int_ftype_int_v2sf_v2sf
6326     = build_function_type
6327     (integer_type_node,
6328      tree_cons (NULL_TREE, integer_type_node,
6329                 tree_cons (NULL_TREE, opaque_V2SF_type_node,
6330                            tree_cons (NULL_TREE, opaque_V2SF_type_node,
6331                                       endlink))));
6332
6333   tree void_ftype_v2si_puint_int
6334     = build_function_type (void_type_node,
6335                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6336                                       tree_cons (NULL_TREE, puint_type_node,
6337                                                  tree_cons (NULL_TREE,
6338                                                             integer_type_node,
6339                                                             endlink))));
6340
6341   tree void_ftype_v2si_puint_char
6342     = build_function_type (void_type_node,
6343                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6344                                       tree_cons (NULL_TREE, puint_type_node,
6345                                                  tree_cons (NULL_TREE,
6346                                                             char_type_node,
6347                                                             endlink))));
6348
6349   tree void_ftype_v2si_pv2si_int
6350     = build_function_type (void_type_node,
6351                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6352                                       tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
6353                                                  tree_cons (NULL_TREE,
6354                                                             integer_type_node,
6355                                                             endlink))));
6356
6357   tree void_ftype_v2si_pv2si_char
6358     = build_function_type (void_type_node,
6359                            tree_cons (NULL_TREE, opaque_V2SI_type_node,
6360                                       tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
6361                                                  tree_cons (NULL_TREE,
6362                                                             char_type_node,
6363                                                             endlink))));
6364
6365   tree void_ftype_int
6366     = build_function_type (void_type_node,
6367                            tree_cons (NULL_TREE, integer_type_node, endlink));
6368
6369   tree int_ftype_void
6370     = build_function_type (integer_type_node, endlink);
6371
6372   tree v2si_ftype_pv2si_int
6373     = build_function_type (opaque_V2SI_type_node,
6374                            tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
6375                                       tree_cons (NULL_TREE, integer_type_node,
6376                                                  endlink)));
6377
6378   tree v2si_ftype_puint_int
6379     = build_function_type (opaque_V2SI_type_node,
6380                            tree_cons (NULL_TREE, puint_type_node,
6381                                       tree_cons (NULL_TREE, integer_type_node,
6382                                                  endlink)));
6383
6384   tree v2si_ftype_pushort_int
6385     = build_function_type (opaque_V2SI_type_node,
6386                            tree_cons (NULL_TREE, pushort_type_node,
6387                                       tree_cons (NULL_TREE, integer_type_node,
6388                                                  endlink)));
6389
6390   tree v2si_ftype_signed_char
6391     = build_function_type (opaque_V2SI_type_node,
6392                            tree_cons (NULL_TREE, signed_char_type_node,
6393                                       endlink));
6394
6395   /* The initialization of the simple binary and unary builtins is
6396      done in rs6000_common_init_builtins, but we have to enable the
6397      mask bits here manually because we have run out of `target_flags'
6398      bits.  We really need to redesign this mask business.  */
6399
6400   enable_mask_for_builtins ((struct builtin_description *) bdesc_2arg,
6401                             ARRAY_SIZE (bdesc_2arg),
6402                             SPE_BUILTIN_EVADDW,
6403                             SPE_BUILTIN_EVXOR);
6404   enable_mask_for_builtins ((struct builtin_description *) bdesc_1arg,
6405                             ARRAY_SIZE (bdesc_1arg),
6406                             SPE_BUILTIN_EVABS,
6407                             SPE_BUILTIN_EVSUBFUSIAAW);
6408   enable_mask_for_builtins ((struct builtin_description *) bdesc_spe_predicates,
6409                             ARRAY_SIZE (bdesc_spe_predicates),
6410                             SPE_BUILTIN_EVCMPEQ,
6411                             SPE_BUILTIN_EVFSTSTLT);
6412   enable_mask_for_builtins ((struct builtin_description *) bdesc_spe_evsel,
6413                             ARRAY_SIZE (bdesc_spe_evsel),
6414                             SPE_BUILTIN_EVSEL_CMPGTS,
6415                             SPE_BUILTIN_EVSEL_FSTSTEQ);
6416
6417   (*lang_hooks.decls.pushdecl)
6418     (build_decl (TYPE_DECL, get_identifier ("__ev64_opaque__"),
6419                  opaque_V2SI_type_node));
6420
6421   /* Initialize irregular SPE builtins.  */
6422   
6423   def_builtin (target_flags, "__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
6424   def_builtin (target_flags, "__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
6425   def_builtin (target_flags, "__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
6426   def_builtin (target_flags, "__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
6427   def_builtin (target_flags, "__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
6428   def_builtin (target_flags, "__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
6429   def_builtin (target_flags, "__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
6430   def_builtin (target_flags, "__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
6431   def_builtin (target_flags, "__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
6432   def_builtin (target_flags, "__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
6433   def_builtin (target_flags, "__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
6434   def_builtin (target_flags, "__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
6435   def_builtin (target_flags, "__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
6436   def_builtin (target_flags, "__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
6437   def_builtin (target_flags, "__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
6438   def_builtin (target_flags, "__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
6439   def_builtin (target_flags, "__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
6440   def_builtin (target_flags, "__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
6441
6442   /* Loads.  */
6443   def_builtin (target_flags, "__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
6444   def_builtin (target_flags, "__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
6445   def_builtin (target_flags, "__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
6446   def_builtin (target_flags, "__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
6447   def_builtin (target_flags, "__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
6448   def_builtin (target_flags, "__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
6449   def_builtin (target_flags, "__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
6450   def_builtin (target_flags, "__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
6451   def_builtin (target_flags, "__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
6452   def_builtin (target_flags, "__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
6453   def_builtin (target_flags, "__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
6454   def_builtin (target_flags, "__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
6455   def_builtin (target_flags, "__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
6456   def_builtin (target_flags, "__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
6457   def_builtin (target_flags, "__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
6458   def_builtin (target_flags, "__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
6459   def_builtin (target_flags, "__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
6460   def_builtin (target_flags, "__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
6461   def_builtin (target_flags, "__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
6462   def_builtin (target_flags, "__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
6463   def_builtin (target_flags, "__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
6464   def_builtin (target_flags, "__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
6465
6466   /* Predicates.  */
6467   d = (struct builtin_description *) bdesc_spe_predicates;
6468   for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
6469     {
6470       tree type;
6471
6472       switch (insn_data[d->icode].operand[1].mode)
6473         {
6474         case V2SImode:
6475           type = int_ftype_int_v2si_v2si;
6476           break;
6477         case V2SFmode:
6478           type = int_ftype_int_v2sf_v2sf;
6479           break;
6480         default:
6481           abort ();
6482         }
6483
6484       def_builtin (d->mask, d->name, type, d->code);
6485     }
6486
6487   /* Evsel predicates.  */
6488   d = (struct builtin_description *) bdesc_spe_evsel;
6489   for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
6490     {
6491       tree type;
6492
6493       switch (insn_data[d->icode].operand[1].mode)
6494         {
6495         case V2SImode:
6496           type = v2si_ftype_4_v2si;
6497           break;
6498         case V2SFmode:
6499           type = v2sf_ftype_4_v2sf;
6500           break;
6501         default:
6502           abort ();
6503         }
6504
6505       def_builtin (d->mask, d->name, type, d->code);
6506     }
6507 }
6508
6509 static void
6510 altivec_init_builtins (void)
6511 {
6512   struct builtin_description *d;
6513   struct builtin_description_predicates *dp;
6514   size_t i;
6515   tree pfloat_type_node = build_pointer_type (float_type_node);
6516   tree pint_type_node = build_pointer_type (integer_type_node);
6517   tree pshort_type_node = build_pointer_type (short_integer_type_node);
6518   tree pchar_type_node = build_pointer_type (char_type_node);
6519
6520   tree pvoid_type_node = build_pointer_type (void_type_node);
6521
6522   tree pcfloat_type_node = build_pointer_type (build_qualified_type (float_type_node, TYPE_QUAL_CONST));
6523   tree pcint_type_node = build_pointer_type (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
6524   tree pcshort_type_node = build_pointer_type (build_qualified_type (short_integer_type_node, TYPE_QUAL_CONST));
6525   tree pcchar_type_node = build_pointer_type (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
6526
6527   tree pcvoid_type_node = build_pointer_type (build_qualified_type (void_type_node, TYPE_QUAL_CONST));
6528
6529   tree int_ftype_int_v4si_v4si
6530     = build_function_type_list (integer_type_node,
6531                                 integer_type_node, V4SI_type_node,
6532                                 V4SI_type_node, NULL_TREE);
6533   tree v4sf_ftype_pcfloat
6534     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
6535   tree void_ftype_pfloat_v4sf
6536     = build_function_type_list (void_type_node,
6537                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
6538   tree v4si_ftype_pcint
6539     = build_function_type_list (V4SI_type_node, pcint_type_node, NULL_TREE);
6540   tree void_ftype_pint_v4si
6541     = build_function_type_list (void_type_node,
6542                                 pint_type_node, V4SI_type_node, NULL_TREE);
6543   tree v8hi_ftype_pcshort
6544     = build_function_type_list (V8HI_type_node, pcshort_type_node, NULL_TREE);
6545   tree void_ftype_pshort_v8hi
6546     = build_function_type_list (void_type_node,
6547                                 pshort_type_node, V8HI_type_node, NULL_TREE);
6548   tree v16qi_ftype_pcchar
6549     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
6550   tree void_ftype_pchar_v16qi
6551     = build_function_type_list (void_type_node,
6552                                 pchar_type_node, V16QI_type_node, NULL_TREE);
6553   tree void_ftype_v4si
6554     = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
6555   tree v8hi_ftype_void
6556     = build_function_type (V8HI_type_node, void_list_node);
6557   tree void_ftype_void
6558     = build_function_type (void_type_node, void_list_node);
6559   tree void_ftype_qi
6560     = build_function_type_list (void_type_node, char_type_node, NULL_TREE);
6561
6562   tree v16qi_ftype_long_pcvoid
6563     = build_function_type_list (V16QI_type_node,
6564                                 long_integer_type_node, pcvoid_type_node, NULL_TREE);
6565   tree v8hi_ftype_long_pcvoid
6566     = build_function_type_list (V8HI_type_node,
6567                                 long_integer_type_node, pcvoid_type_node, NULL_TREE);
6568   tree v4si_ftype_long_pcvoid
6569     = build_function_type_list (V4SI_type_node,
6570                                 long_integer_type_node, pcvoid_type_node, NULL_TREE);
6571
6572   tree void_ftype_v4si_long_pvoid
6573     = build_function_type_list (void_type_node,
6574                                 V4SI_type_node, long_integer_type_node,
6575                                 pvoid_type_node, NULL_TREE);
6576   tree void_ftype_v16qi_long_pvoid
6577     = build_function_type_list (void_type_node,
6578                                 V16QI_type_node, long_integer_type_node,
6579                                 pvoid_type_node, NULL_TREE);
6580   tree void_ftype_v8hi_long_pvoid
6581     = build_function_type_list (void_type_node,
6582                                 V8HI_type_node, long_integer_type_node,
6583                                 pvoid_type_node, NULL_TREE);
6584   tree int_ftype_int_v8hi_v8hi
6585     = build_function_type_list (integer_type_node,
6586                                 integer_type_node, V8HI_type_node,
6587                                 V8HI_type_node, NULL_TREE);
6588   tree int_ftype_int_v16qi_v16qi
6589     = build_function_type_list (integer_type_node,
6590                                 integer_type_node, V16QI_type_node,
6591                                 V16QI_type_node, NULL_TREE);
6592   tree int_ftype_int_v4sf_v4sf
6593     = build_function_type_list (integer_type_node,
6594                                 integer_type_node, V4SF_type_node,
6595                                 V4SF_type_node, NULL_TREE);
6596   tree v4si_ftype_v4si
6597     = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
6598   tree v8hi_ftype_v8hi
6599     = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
6600   tree v16qi_ftype_v16qi
6601     = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
6602   tree v4sf_ftype_v4sf
6603     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
6604   tree void_ftype_pcvoid_int_char
6605     = build_function_type_list (void_type_node,
6606                                 pcvoid_type_node, integer_type_node,
6607                                 char_type_node, NULL_TREE);
6608   
6609   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4sf", v4sf_ftype_pcfloat,
6610                ALTIVEC_BUILTIN_LD_INTERNAL_4sf);
6611   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4sf", void_ftype_pfloat_v4sf,
6612                ALTIVEC_BUILTIN_ST_INTERNAL_4sf);
6613   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4si", v4si_ftype_pcint,
6614                ALTIVEC_BUILTIN_LD_INTERNAL_4si);
6615   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4si", void_ftype_pint_v4si,
6616                ALTIVEC_BUILTIN_ST_INTERNAL_4si);
6617   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_8hi", v8hi_ftype_pcshort,
6618                ALTIVEC_BUILTIN_LD_INTERNAL_8hi);
6619   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_8hi", void_ftype_pshort_v8hi,
6620                ALTIVEC_BUILTIN_ST_INTERNAL_8hi);
6621   def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_16qi", v16qi_ftype_pcchar,
6622                ALTIVEC_BUILTIN_LD_INTERNAL_16qi);
6623   def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_16qi", void_ftype_pchar_v16qi,
6624                ALTIVEC_BUILTIN_ST_INTERNAL_16qi);
6625   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
6626   def_builtin (MASK_ALTIVEC, "__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
6627   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
6628   def_builtin (MASK_ALTIVEC, "__builtin_altivec_dss", void_ftype_qi, ALTIVEC_BUILTIN_DSS);
6629   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
6630   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
6631   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
6632   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
6633   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
6634   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
6635   def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
6636   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
6637   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
6638   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
6639   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
6640   def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
6641
6642   /* Add the DST variants.  */
6643   d = (struct builtin_description *) bdesc_dst;
6644   for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
6645     def_builtin (d->mask, d->name, void_ftype_pcvoid_int_char, d->code);
6646
6647   /* Initialize the predicates.  */
6648   dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
6649   for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
6650     {
6651       enum machine_mode mode1;
6652       tree type;
6653
6654       mode1 = insn_data[dp->icode].operand[1].mode;
6655
6656       switch (mode1)
6657         {
6658         case V4SImode:
6659           type = int_ftype_int_v4si_v4si;
6660           break;
6661         case V8HImode:
6662           type = int_ftype_int_v8hi_v8hi;
6663           break;
6664         case V16QImode:
6665           type = int_ftype_int_v16qi_v16qi;
6666           break;
6667         case V4SFmode:
6668           type = int_ftype_int_v4sf_v4sf;
6669           break;
6670         default:
6671           abort ();
6672         }
6673       
6674       def_builtin (dp->mask, dp->name, type, dp->code);
6675     }
6676
6677   /* Initialize the abs* operators.  */
6678   d = (struct builtin_description *) bdesc_abs;
6679   for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
6680     {
6681       enum machine_mode mode0;
6682       tree type;
6683
6684       mode0 = insn_data[d->icode].operand[0].mode;
6685
6686       switch (mode0)
6687         {
6688         case V4SImode:
6689           type = v4si_ftype_v4si;
6690           break;
6691         case V8HImode:
6692           type = v8hi_ftype_v8hi;
6693           break;
6694         case V16QImode:
6695           type = v16qi_ftype_v16qi;
6696           break;
6697         case V4SFmode:
6698           type = v4sf_ftype_v4sf;
6699           break;
6700         default:
6701           abort ();
6702         }
6703       
6704       def_builtin (d->mask, d->name, type, d->code);
6705     }
6706 }
6707
6708 static void
6709 rs6000_common_init_builtins (void)
6710 {
6711   struct builtin_description *d;
6712   size_t i;
6713
6714   tree v4sf_ftype_v4sf_v4sf_v16qi
6715     = build_function_type_list (V4SF_type_node,
6716                                 V4SF_type_node, V4SF_type_node,
6717                                 V16QI_type_node, NULL_TREE);
6718   tree v4si_ftype_v4si_v4si_v16qi
6719     = build_function_type_list (V4SI_type_node,
6720                                 V4SI_type_node, V4SI_type_node,
6721                                 V16QI_type_node, NULL_TREE);
6722   tree v8hi_ftype_v8hi_v8hi_v16qi
6723     = build_function_type_list (V8HI_type_node,
6724                                 V8HI_type_node, V8HI_type_node,
6725                                 V16QI_type_node, NULL_TREE);
6726   tree v16qi_ftype_v16qi_v16qi_v16qi
6727     = build_function_type_list (V16QI_type_node,
6728                                 V16QI_type_node, V16QI_type_node,
6729                                 V16QI_type_node, NULL_TREE);
6730   tree v4si_ftype_char
6731     = build_function_type_list (V4SI_type_node, char_type_node, NULL_TREE);
6732   tree v8hi_ftype_char
6733     = build_function_type_list (V8HI_type_node, char_type_node, NULL_TREE);
6734   tree v16qi_ftype_char
6735     = build_function_type_list (V16QI_type_node, char_type_node, NULL_TREE);
6736   tree v8hi_ftype_v16qi
6737     = build_function_type_list (V8HI_type_node, V16QI_type_node, NULL_TREE);
6738   tree v4sf_ftype_v4sf
6739     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
6740
6741   tree v2si_ftype_v2si_v2si
6742     = build_function_type_list (opaque_V2SI_type_node,
6743                                 opaque_V2SI_type_node,
6744                                 opaque_V2SI_type_node, NULL_TREE);
6745
6746   tree v2sf_ftype_v2sf_v2sf
6747     = build_function_type_list (opaque_V2SF_type_node,
6748                                 opaque_V2SF_type_node,
6749                                 opaque_V2SF_type_node, NULL_TREE);
6750
6751   tree v2si_ftype_int_int
6752     = build_function_type_list (opaque_V2SI_type_node,
6753                                 integer_type_node, integer_type_node,
6754                                 NULL_TREE);
6755
6756   tree v2si_ftype_v2si
6757     = build_function_type_list (opaque_V2SI_type_node,
6758                                 opaque_V2SI_type_node, NULL_TREE);
6759
6760   tree v2sf_ftype_v2sf
6761     = build_function_type_list (opaque_V2SF_type_node,
6762                                 opaque_V2SF_type_node, NULL_TREE);
6763   
6764   tree v2sf_ftype_v2si
6765     = build_function_type_list (opaque_V2SF_type_node,
6766                                 opaque_V2SI_type_node, NULL_TREE);
6767
6768   tree v2si_ftype_v2sf
6769     = build_function_type_list (opaque_V2SI_type_node,
6770                                 opaque_V2SF_type_node, NULL_TREE);
6771
6772   tree v2si_ftype_v2si_char
6773     = build_function_type_list (opaque_V2SI_type_node,
6774                                 opaque_V2SI_type_node,
6775                                 char_type_node, NULL_TREE);
6776
6777   tree v2si_ftype_int_char
6778     = build_function_type_list (opaque_V2SI_type_node,
6779                                 integer_type_node, char_type_node, NULL_TREE);
6780
6781   tree v2si_ftype_char
6782     = build_function_type_list (opaque_V2SI_type_node,
6783                                 char_type_node, NULL_TREE);
6784
6785   tree int_ftype_int_int
6786     = build_function_type_list (integer_type_node,
6787                                 integer_type_node, integer_type_node,
6788                                 NULL_TREE);
6789
6790   tree v4si_ftype_v4si_v4si
6791     = build_function_type_list (V4SI_type_node,
6792                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
6793   tree v4sf_ftype_v4si_char
6794     = build_function_type_list (V4SF_type_node,
6795                                 V4SI_type_node, char_type_node, NULL_TREE);
6796   tree v4si_ftype_v4sf_char
6797     = build_function_type_list (V4SI_type_node,
6798                                 V4SF_type_node, char_type_node, NULL_TREE);
6799   tree v4si_ftype_v4si_char
6800     = build_function_type_list (V4SI_type_node,
6801                                 V4SI_type_node, char_type_node, NULL_TREE);
6802   tree v8hi_ftype_v8hi_char
6803     = build_function_type_list (V8HI_type_node,
6804                                 V8HI_type_node, char_type_node, NULL_TREE);
6805   tree v16qi_ftype_v16qi_char
6806     = build_function_type_list (V16QI_type_node,
6807                                 V16QI_type_node, char_type_node, NULL_TREE);
6808   tree v16qi_ftype_v16qi_v16qi_char
6809     = build_function_type_list (V16QI_type_node,
6810                                 V16QI_type_node, V16QI_type_node,
6811                                 char_type_node, NULL_TREE);
6812   tree v8hi_ftype_v8hi_v8hi_char
6813     = build_function_type_list (V8HI_type_node,
6814                                 V8HI_type_node, V8HI_type_node,
6815                                 char_type_node, NULL_TREE);
6816   tree v4si_ftype_v4si_v4si_char
6817     = build_function_type_list (V4SI_type_node,
6818                                 V4SI_type_node, V4SI_type_node,
6819                                 char_type_node, NULL_TREE);
6820   tree v4sf_ftype_v4sf_v4sf_char
6821     = build_function_type_list (V4SF_type_node,
6822                                 V4SF_type_node, V4SF_type_node,
6823                                 char_type_node, NULL_TREE);
6824   tree v4sf_ftype_v4sf_v4sf
6825     = build_function_type_list (V4SF_type_node,
6826                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
6827   tree v4sf_ftype_v4sf_v4sf_v4si
6828     = build_function_type_list (V4SF_type_node,
6829                                 V4SF_type_node, V4SF_type_node,
6830                                 V4SI_type_node, NULL_TREE);
6831   tree v4sf_ftype_v4sf_v4sf_v4sf
6832     = build_function_type_list (V4SF_type_node,
6833                                 V4SF_type_node, V4SF_type_node,
6834                                 V4SF_type_node, NULL_TREE);
6835   tree v4si_ftype_v4si_v4si_v4si 
6836     = build_function_type_list (V4SI_type_node,
6837                                 V4SI_type_node, V4SI_type_node,
6838                                 V4SI_type_node, NULL_TREE);
6839   tree v8hi_ftype_v8hi_v8hi
6840     = build_function_type_list (V8HI_type_node,
6841                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
6842   tree v8hi_ftype_v8hi_v8hi_v8hi
6843     = build_function_type_list (V8HI_type_node,
6844                                 V8HI_type_node, V8HI_type_node,
6845                                 V8HI_type_node, NULL_TREE);
6846  tree v4si_ftype_v8hi_v8hi_v4si
6847     = build_function_type_list (V4SI_type_node,
6848                                 V8HI_type_node, V8HI_type_node,
6849                                 V4SI_type_node, NULL_TREE);
6850  tree v4si_ftype_v16qi_v16qi_v4si
6851     = build_function_type_list (V4SI_type_node,
6852                                 V16QI_type_node, V16QI_type_node,
6853                                 V4SI_type_node, NULL_TREE);
6854   tree v16qi_ftype_v16qi_v16qi
6855     = build_function_type_list (V16QI_type_node,
6856                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
6857   tree v4si_ftype_v4sf_v4sf
6858     = build_function_type_list (V4SI_type_node,
6859                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
6860   tree v8hi_ftype_v16qi_v16qi
6861     = build_function_type_list (V8HI_type_node,
6862                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
6863   tree v4si_ftype_v8hi_v8hi
6864     = build_function_type_list (V4SI_type_node,
6865                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
6866   tree v8hi_ftype_v4si_v4si
6867     = build_function_type_list (V8HI_type_node,
6868                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
6869   tree v16qi_ftype_v8hi_v8hi
6870     = build_function_type_list (V16QI_type_node,
6871                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
6872   tree v4si_ftype_v16qi_v4si
6873     = build_function_type_list (V4SI_type_node,
6874                                 V16QI_type_node, V4SI_type_node, NULL_TREE);
6875   tree v4si_ftype_v16qi_v16qi
6876     = build_function_type_list (V4SI_type_node,
6877                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
6878   tree v4si_ftype_v8hi_v4si
6879     = build_function_type_list (V4SI_type_node,
6880                                 V8HI_type_node, V4SI_type_node, NULL_TREE);
6881   tree v4si_ftype_v8hi
6882     = build_function_type_list (V4SI_type_node, V8HI_type_node, NULL_TREE);
6883   tree int_ftype_v4si_v4si
6884     = build_function_type_list (integer_type_node,
6885                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
6886   tree int_ftype_v4sf_v4sf
6887     = build_function_type_list (integer_type_node,
6888                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
6889   tree int_ftype_v16qi_v16qi
6890     = build_function_type_list (integer_type_node,
6891                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
6892   tree int_ftype_v8hi_v8hi
6893     = build_function_type_list (integer_type_node,
6894                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
6895
6896   /* Add the simple ternary operators.  */
6897   d = (struct builtin_description *) bdesc_3arg;
6898   for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
6899     {
6900       
6901       enum machine_mode mode0, mode1, mode2, mode3;
6902       tree type;
6903
6904       if (d->name == 0 || d->icode == CODE_FOR_nothing)
6905         continue;
6906       
6907       mode0 = insn_data[d->icode].operand[0].mode;
6908       mode1 = insn_data[d->icode].operand[1].mode;
6909       mode2 = insn_data[d->icode].operand[2].mode;
6910       mode3 = insn_data[d->icode].operand[3].mode;
6911       
6912       /* When all four are of the same mode.  */
6913       if (mode0 == mode1 && mode1 == mode2 && mode2 == mode3)
6914         {
6915           switch (mode0)
6916             {
6917             case V4SImode:
6918               type = v4si_ftype_v4si_v4si_v4si;
6919               break;
6920             case V4SFmode:
6921               type = v4sf_ftype_v4sf_v4sf_v4sf;
6922               break;
6923             case V8HImode:
6924               type = v8hi_ftype_v8hi_v8hi_v8hi;
6925               break;          
6926             case V16QImode:
6927               type = v16qi_ftype_v16qi_v16qi_v16qi;
6928               break;          
6929             default:
6930               abort();        
6931             }
6932         }
6933       else if (mode0 == mode1 && mode1 == mode2 && mode3 == V16QImode)
6934         {
6935           switch (mode0)
6936             {
6937             case V4SImode:
6938               type = v4si_ftype_v4si_v4si_v16qi;
6939               break;
6940             case V4SFmode:
6941               type = v4sf_ftype_v4sf_v4sf_v16qi;
6942               break;
6943             case V8HImode:
6944               type = v8hi_ftype_v8hi_v8hi_v16qi;
6945               break;          
6946             case V16QImode:
6947               type = v16qi_ftype_v16qi_v16qi_v16qi;
6948               break;          
6949             default:
6950               abort();        
6951             }
6952         }
6953       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode 
6954                && mode3 == V4SImode)
6955         type = v4si_ftype_v16qi_v16qi_v4si;
6956       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode 
6957                && mode3 == V4SImode)
6958         type = v4si_ftype_v8hi_v8hi_v4si;
6959       else if (mode0 == V4SFmode && mode1 == V4SFmode && mode2 == V4SFmode 
6960                && mode3 == V4SImode)
6961         type = v4sf_ftype_v4sf_v4sf_v4si;
6962
6963       /* vchar, vchar, vchar, 4 bit literal.  */
6964       else if (mode0 == V16QImode && mode1 == mode0 && mode2 == mode0
6965                && mode3 == QImode)
6966         type = v16qi_ftype_v16qi_v16qi_char;
6967
6968       /* vshort, vshort, vshort, 4 bit literal.  */
6969       else if (mode0 == V8HImode && mode1 == mode0 && mode2 == mode0
6970                && mode3 == QImode)
6971         type = v8hi_ftype_v8hi_v8hi_char;
6972
6973       /* vint, vint, vint, 4 bit literal.  */
6974       else if (mode0 == V4SImode && mode1 == mode0 && mode2 == mode0
6975                && mode3 == QImode)
6976         type = v4si_ftype_v4si_v4si_char;
6977
6978       /* vfloat, vfloat, vfloat, 4 bit literal.  */
6979       else if (mode0 == V4SFmode && mode1 == mode0 && mode2 == mode0
6980                && mode3 == QImode)
6981         type = v4sf_ftype_v4sf_v4sf_char;
6982
6983       else
6984         abort ();
6985
6986       def_builtin (d->mask, d->name, type, d->code);
6987     }
6988
6989   /* Add the simple binary operators.  */
6990   d = (struct builtin_description *) bdesc_2arg;
6991   for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
6992     {
6993       enum machine_mode mode0, mode1, mode2;
6994       tree type;
6995
6996       if (d->name == 0 || d->icode == CODE_FOR_nothing)
6997         continue;
6998       
6999       mode0 = insn_data[d->icode].operand[0].mode;
7000       mode1 = insn_data[d->icode].operand[1].mode;
7001       mode2 = insn_data[d->icode].operand[2].mode;
7002
7003       /* When all three operands are of the same mode.  */
7004       if (mode0 == mode1 && mode1 == mode2)
7005         {
7006           switch (mode0)
7007             {
7008             case V4SFmode:
7009               type = v4sf_ftype_v4sf_v4sf;
7010               break;
7011             case V4SImode:
7012               type = v4si_ftype_v4si_v4si;
7013               break;
7014             case V16QImode:
7015               type = v16qi_ftype_v16qi_v16qi;
7016               break;
7017             case V8HImode:
7018               type = v8hi_ftype_v8hi_v8hi;
7019               break;
7020             case V2SImode:
7021               type = v2si_ftype_v2si_v2si;
7022               break;
7023             case V2SFmode:
7024               type = v2sf_ftype_v2sf_v2sf;
7025               break;
7026             case SImode:
7027               type = int_ftype_int_int;
7028               break;
7029             default:
7030               abort ();
7031             }
7032         }
7033
7034       /* A few other combos we really don't want to do manually.  */
7035
7036       /* vint, vfloat, vfloat.  */
7037       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == V4SFmode)
7038         type = v4si_ftype_v4sf_v4sf;
7039
7040       /* vshort, vchar, vchar.  */
7041       else if (mode0 == V8HImode && mode1 == V16QImode && mode2 == V16QImode)
7042         type = v8hi_ftype_v16qi_v16qi;
7043
7044       /* vint, vshort, vshort.  */
7045       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode)
7046         type = v4si_ftype_v8hi_v8hi;
7047
7048       /* vshort, vint, vint.  */
7049       else if (mode0 == V8HImode && mode1 == V4SImode && mode2 == V4SImode)
7050         type = v8hi_ftype_v4si_v4si;
7051
7052       /* vchar, vshort, vshort.  */
7053       else if (mode0 == V16QImode && mode1 == V8HImode && mode2 == V8HImode)
7054         type = v16qi_ftype_v8hi_v8hi;
7055
7056       /* vint, vchar, vint.  */
7057       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V4SImode)
7058         type = v4si_ftype_v16qi_v4si;
7059
7060       /* vint, vchar, vchar.  */
7061       else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode)
7062         type = v4si_ftype_v16qi_v16qi;
7063
7064       /* vint, vshort, vint.  */
7065       else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V4SImode)
7066         type = v4si_ftype_v8hi_v4si;
7067       
7068       /* vint, vint, 5 bit literal.  */
7069       else if (mode0 == V4SImode && mode1 == V4SImode && mode2 == QImode)
7070         type = v4si_ftype_v4si_char;
7071       
7072       /* vshort, vshort, 5 bit literal.  */
7073       else if (mode0 == V8HImode && mode1 == V8HImode && mode2 == QImode)
7074         type = v8hi_ftype_v8hi_char;
7075       
7076       /* vchar, vchar, 5 bit literal.  */
7077       else if (mode0 == V16QImode && mode1 == V16QImode && mode2 == QImode)
7078         type = v16qi_ftype_v16qi_char;
7079
7080       /* vfloat, vint, 5 bit literal.  */
7081       else if (mode0 == V4SFmode && mode1 == V4SImode && mode2 == QImode)
7082         type = v4sf_ftype_v4si_char;
7083       
7084       /* vint, vfloat, 5 bit literal.  */
7085       else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == QImode)
7086         type = v4si_ftype_v4sf_char;
7087
7088       else if (mode0 == V2SImode && mode1 == SImode && mode2 == SImode)
7089         type = v2si_ftype_int_int;
7090
7091       else if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
7092         type = v2si_ftype_v2si_char;
7093
7094       else if (mode0 == V2SImode && mode1 == SImode && mode2 == QImode)
7095         type = v2si_ftype_int_char;
7096
7097       /* int, x, x.  */
7098       else if (mode0 == SImode)
7099         {
7100           switch (mode1)
7101             {
7102             case V4SImode:
7103               type = int_ftype_v4si_v4si;
7104               break;
7105             case V4SFmode:
7106               type = int_ftype_v4sf_v4sf;
7107               break;
7108             case V16QImode:
7109               type = int_ftype_v16qi_v16qi;
7110               break;
7111             case V8HImode:
7112               type = int_ftype_v8hi_v8hi;
7113               break;
7114             default:
7115               abort ();
7116             }
7117         }
7118
7119       else
7120         abort ();
7121
7122       def_builtin (d->mask, d->name, type, d->code);
7123     }
7124
7125   /* Add the simple unary operators.  */
7126   d = (struct builtin_description *) bdesc_1arg;
7127   for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
7128     {
7129       enum machine_mode mode0, mode1;
7130       tree type;
7131
7132       if (d->name == 0 || d->icode == CODE_FOR_nothing)
7133         continue;
7134       
7135       mode0 = insn_data[d->icode].operand[0].mode;
7136       mode1 = insn_data[d->icode].operand[1].mode;
7137
7138       if (mode0 == V4SImode && mode1 == QImode)
7139         type = v4si_ftype_char;
7140       else if (mode0 == V8HImode && mode1 == QImode)
7141         type = v8hi_ftype_char;
7142       else if (mode0 == V16QImode && mode1 == QImode)
7143         type = v16qi_ftype_char;
7144       else if (mode0 == V4SFmode && mode1 == V4SFmode)
7145         type = v4sf_ftype_v4sf;
7146       else if (mode0 == V8HImode && mode1 == V16QImode)
7147         type = v8hi_ftype_v16qi;
7148       else if (mode0 == V4SImode && mode1 == V8HImode)
7149         type = v4si_ftype_v8hi;
7150       else if (mode0 == V2SImode && mode1 == V2SImode)
7151         type = v2si_ftype_v2si;
7152       else if (mode0 == V2SFmode && mode1 == V2SFmode)
7153         type = v2sf_ftype_v2sf;
7154       else if (mode0 == V2SFmode && mode1 == V2SImode)
7155         type = v2sf_ftype_v2si;
7156       else if (mode0 == V2SImode && mode1 == V2SFmode)
7157         type = v2si_ftype_v2sf;
7158       else if (mode0 == V2SImode && mode1 == QImode)
7159         type = v2si_ftype_char;
7160       else
7161         abort ();
7162
7163       def_builtin (d->mask, d->name, type, d->code);
7164     }
7165 }
7166
7167 static void
7168 rs6000_init_libfuncs (void)
7169 {
7170   if (!TARGET_HARD_FLOAT)
7171     return;
7172
7173   if (DEFAULT_ABI != ABI_V4)
7174     {
7175       if (TARGET_XCOFF && ! TARGET_POWER2 && ! TARGET_POWERPC)
7176         {
7177           /* AIX library routines for float->int conversion.  */
7178           set_conv_libfunc (sfix_optab, SImode, DFmode, "__itrunc");
7179           set_conv_libfunc (ufix_optab, SImode, DFmode, "__uitrunc");
7180         }
7181
7182       /* Standard AIX/Darwin/64-bit SVR4 quad floating point routines.  */
7183       set_optab_libfunc (add_optab, TFmode, "_xlqadd");
7184       set_optab_libfunc (sub_optab, TFmode, "_xlqsub");
7185       set_optab_libfunc (smul_optab, TFmode, "_xlqmul");
7186       set_optab_libfunc (sdiv_optab, TFmode, "_xlqdiv");
7187     }
7188   else
7189     {
7190       /* 32-bit SVR4 quad floating point routines.  */
7191
7192       set_optab_libfunc (add_optab, TFmode, "_q_add");
7193       set_optab_libfunc (sub_optab, TFmode, "_q_sub");
7194       set_optab_libfunc (neg_optab, TFmode, "_q_neg");
7195       set_optab_libfunc (smul_optab, TFmode, "_q_mul");
7196       set_optab_libfunc (sdiv_optab, TFmode, "_q_div");
7197       if (TARGET_PPC_GPOPT || TARGET_POWER2)
7198         set_optab_libfunc (sqrt_optab, TFmode, "_q_sqrt");
7199
7200       set_optab_libfunc (eq_optab, TFmode, "_q_feq");
7201       set_optab_libfunc (ne_optab, TFmode, "_q_fne");
7202       set_optab_libfunc (gt_optab, TFmode, "_q_fgt");
7203       set_optab_libfunc (ge_optab, TFmode, "_q_fge");
7204       set_optab_libfunc (lt_optab, TFmode, "_q_flt");
7205       set_optab_libfunc (le_optab, TFmode, "_q_fle");
7206
7207       set_conv_libfunc (sext_optab, TFmode, SFmode, "_q_stoq");
7208       set_conv_libfunc (sext_optab, TFmode, DFmode, "_q_dtoq");
7209       set_conv_libfunc (trunc_optab, SFmode, TFmode, "_q_qtos");
7210       set_conv_libfunc (trunc_optab, DFmode, TFmode, "_q_qtod");
7211       set_conv_libfunc (sfix_optab, SImode, TFmode, "_q_qtoi");
7212       set_conv_libfunc (ufix_optab, SImode, TFmode, "_q_qtou");
7213       set_conv_libfunc (sfloat_optab, TFmode, SImode, "_q_itoq");
7214     }
7215 }
7216 \f
7217 /* Expand a block move operation, and return 1 if successful.  Return 0
7218    if we should let the compiler generate normal code.
7219
7220    operands[0] is the destination
7221    operands[1] is the source
7222    operands[2] is the length
7223    operands[3] is the alignment */
7224
7225 #define MAX_MOVE_REG 4
7226
7227 int
7228 expand_block_move (rtx operands[])
7229 {
7230   rtx orig_dest = operands[0];
7231   rtx orig_src  = operands[1];
7232   rtx bytes_rtx = operands[2];
7233   rtx align_rtx = operands[3];
7234   int constp    = (GET_CODE (bytes_rtx) == CONST_INT);
7235   int align;
7236   int bytes;
7237   int offset;
7238   int move_bytes;
7239   rtx stores[MAX_MOVE_REG];
7240   int num_reg = 0;
7241
7242   /* If this is not a fixed size move, just call memcpy */
7243   if (! constp)
7244     return 0;
7245
7246   /* If this is not a fixed size alignment, abort */
7247   if (GET_CODE (align_rtx) != CONST_INT)
7248     abort ();
7249   align = INTVAL (align_rtx);
7250
7251   /* Anything to move? */
7252   bytes = INTVAL (bytes_rtx);
7253   if (bytes <= 0)
7254     return 1;
7255
7256   /* store_one_arg depends on expand_block_move to handle at least the size of
7257      reg_parm_stack_space.  */  
7258   if (bytes > (TARGET_POWERPC64 ? 64 : 32))
7259     return 0;
7260
7261   for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
7262     {
7263       union {
7264         rtx (*movstrsi) (rtx, rtx, rtx, rtx);
7265         rtx (*mov) (rtx, rtx);
7266       } gen_func;
7267       enum machine_mode mode = BLKmode;
7268       rtx src, dest;
7269       
7270       if (TARGET_STRING
7271           && bytes > 24         /* move up to 32 bytes at a time */
7272           && ! fixed_regs[5]
7273           && ! fixed_regs[6]
7274           && ! fixed_regs[7]
7275           && ! fixed_regs[8]
7276           && ! fixed_regs[9]
7277           && ! fixed_regs[10]
7278           && ! fixed_regs[11]
7279           && ! fixed_regs[12])
7280         {
7281           move_bytes = (bytes > 32) ? 32 : bytes;
7282           gen_func.movstrsi = gen_movstrsi_8reg;
7283         }
7284       else if (TARGET_STRING
7285                && bytes > 16    /* move up to 24 bytes at a time */
7286                && ! fixed_regs[5]
7287                && ! fixed_regs[6]
7288                && ! fixed_regs[7]
7289                && ! fixed_regs[8]
7290                && ! fixed_regs[9]
7291                && ! fixed_regs[10])
7292         {
7293           move_bytes = (bytes > 24) ? 24 : bytes;
7294           gen_func.movstrsi = gen_movstrsi_6reg;
7295         }
7296       else if (TARGET_STRING
7297                && bytes > 8     /* move up to 16 bytes at a time */
7298                && ! fixed_regs[5]
7299                && ! fixed_regs[6]
7300                && ! fixed_regs[7]
7301                && ! fixed_regs[8])
7302         {
7303           move_bytes = (bytes > 16) ? 16 : bytes;
7304           gen_func.movstrsi = gen_movstrsi_4reg;
7305         }
7306       else if (bytes >= 8 && TARGET_POWERPC64
7307                /* 64-bit loads and stores require word-aligned
7308                   displacements.  */
7309                && (align >= 8 || (! STRICT_ALIGNMENT && align >= 4)))
7310         {
7311           move_bytes = 8;
7312           mode = DImode;
7313           gen_func.mov = gen_movdi;
7314         }
7315       else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
7316         {                       /* move up to 8 bytes at a time */
7317           move_bytes = (bytes > 8) ? 8 : bytes;
7318           gen_func.movstrsi = gen_movstrsi_2reg;
7319         }
7320       else if (bytes >= 4 && (align >= 4 || ! STRICT_ALIGNMENT))
7321         {                       /* move 4 bytes */
7322           move_bytes = 4;
7323           mode = SImode;
7324           gen_func.mov = gen_movsi;
7325         }
7326       else if (bytes == 2 && (align >= 2 || ! STRICT_ALIGNMENT))
7327         {                       /* move 2 bytes */
7328           move_bytes = 2;
7329           mode = HImode;
7330           gen_func.mov = gen_movhi;
7331         }
7332       else if (TARGET_STRING && bytes > 1)
7333         {                       /* move up to 4 bytes at a time */
7334           move_bytes = (bytes > 4) ? 4 : bytes;
7335           gen_func.movstrsi = gen_movstrsi_1reg;
7336         }
7337       else /* move 1 byte at a time */
7338         {
7339           move_bytes = 1;
7340           mode = QImode;
7341           gen_func.mov = gen_movqi;
7342         }
7343       
7344       src = adjust_address (orig_src, mode, offset);
7345       dest = adjust_address (orig_dest, mode, offset);
7346       
7347       if (mode != BLKmode) 
7348         {
7349           rtx tmp_reg = gen_reg_rtx (mode);
7350           
7351           emit_insn ((*gen_func.mov) (tmp_reg, src));
7352           stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
7353         }
7354
7355       if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
7356         {
7357           int i;
7358           for (i = 0; i < num_reg; i++)
7359             emit_insn (stores[i]);
7360           num_reg = 0;
7361         }
7362
7363       if (mode == BLKmode)
7364         {
7365           /* Move the address into scratch registers.  The movstrsi
7366              patterns require zero offset.  */
7367           if (!REG_P (XEXP (src, 0)))
7368             {
7369               rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
7370               src = replace_equiv_address (src, src_reg);
7371             }
7372           set_mem_size (src, GEN_INT (move_bytes));
7373           
7374           if (!REG_P (XEXP (dest, 0)))
7375             {
7376               rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
7377               dest = replace_equiv_address (dest, dest_reg);
7378             }
7379           set_mem_size (dest, GEN_INT (move_bytes));
7380           
7381           emit_insn ((*gen_func.movstrsi) (dest, src,
7382                                            GEN_INT (move_bytes & 31),
7383                                            align_rtx));
7384         }
7385     }
7386
7387   return 1;
7388 }
7389
7390 \f
7391 /* Return 1 if OP is a load multiple operation.  It is known to be a
7392    PARALLEL and the first section will be tested.  */
7393
7394 int
7395 load_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7396 {
7397   int count = XVECLEN (op, 0);
7398   unsigned int dest_regno;
7399   rtx src_addr;
7400   int i;
7401
7402   /* Perform a quick check so we don't blow up below.  */
7403   if (count <= 1
7404       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7405       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
7406       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
7407     return 0;
7408
7409   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
7410   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
7411
7412   for (i = 1; i < count; i++)
7413     {
7414       rtx elt = XVECEXP (op, 0, i);
7415
7416       if (GET_CODE (elt) != SET
7417           || GET_CODE (SET_DEST (elt)) != REG
7418           || GET_MODE (SET_DEST (elt)) != SImode
7419           || REGNO (SET_DEST (elt)) != dest_regno + i
7420           || GET_CODE (SET_SRC (elt)) != MEM
7421           || GET_MODE (SET_SRC (elt)) != SImode
7422           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
7423           || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
7424           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
7425           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != i * 4)
7426         return 0;
7427     }
7428
7429   return 1;
7430 }
7431
7432 /* Similar, but tests for store multiple.  Here, the second vector element
7433    is a CLOBBER.  It will be tested later.  */
7434
7435 int
7436 store_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7437 {
7438   int count = XVECLEN (op, 0) - 1;
7439   unsigned int src_regno;
7440   rtx dest_addr;
7441   int i;
7442
7443   /* Perform a quick check so we don't blow up below.  */
7444   if (count <= 1
7445       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7446       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
7447       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
7448     return 0;
7449
7450   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
7451   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
7452
7453   for (i = 1; i < count; i++)
7454     {
7455       rtx elt = XVECEXP (op, 0, i + 1);
7456
7457       if (GET_CODE (elt) != SET
7458           || GET_CODE (SET_SRC (elt)) != REG
7459           || GET_MODE (SET_SRC (elt)) != SImode
7460           || REGNO (SET_SRC (elt)) != src_regno + i
7461           || GET_CODE (SET_DEST (elt)) != MEM
7462           || GET_MODE (SET_DEST (elt)) != SImode
7463           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
7464           || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
7465           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
7466           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != i * 4)
7467         return 0;
7468     }
7469
7470   return 1;
7471 }
7472
7473 /* Return a string to perform a load_multiple operation.
7474    operands[0] is the vector.
7475    operands[1] is the source address.
7476    operands[2] is the first destination register.  */
7477
7478 const char *
7479 rs6000_output_load_multiple (rtx operands[3])
7480 {
7481   /* We have to handle the case where the pseudo used to contain the address
7482      is assigned to one of the output registers.  */
7483   int i, j;
7484   int words = XVECLEN (operands[0], 0);
7485   rtx xop[10];
7486
7487   if (XVECLEN (operands[0], 0) == 1)
7488     return "{l|lwz} %2,0(%1)";
7489
7490   for (i = 0; i < words; i++)
7491     if (refers_to_regno_p (REGNO (operands[2]) + i,
7492                            REGNO (operands[2]) + i + 1, operands[1], 0))
7493       {
7494         if (i == words-1)
7495           {
7496             xop[0] = GEN_INT (4 * (words-1));
7497             xop[1] = operands[1];
7498             xop[2] = operands[2];
7499             output_asm_insn ("{lsi|lswi} %2,%1,%0\n\t{l|lwz} %1,%0(%1)", xop);
7500             return "";
7501           }
7502         else if (i == 0)
7503           {
7504             xop[0] = GEN_INT (4 * (words-1));
7505             xop[1] = operands[1];
7506             xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7507             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);
7508             return "";
7509           }
7510         else
7511           {
7512             for (j = 0; j < words; j++)
7513               if (j != i)
7514                 {
7515                   xop[0] = GEN_INT (j * 4);
7516                   xop[1] = operands[1];
7517                   xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
7518                   output_asm_insn ("{l|lwz} %2,%0(%1)", xop);
7519                 }
7520             xop[0] = GEN_INT (i * 4);
7521             xop[1] = operands[1];
7522             output_asm_insn ("{l|lwz} %1,%0(%1)", xop);
7523             return "";
7524           }
7525       }
7526
7527   return "{lsi|lswi} %2,%1,%N0";
7528 }
7529
7530 /* Return 1 for a parallel vrsave operation.  */
7531
7532 int
7533 vrsave_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7534 {
7535   int count = XVECLEN (op, 0);
7536   unsigned int dest_regno, src_regno;
7537   int i;
7538
7539   if (count <= 1
7540       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7541       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
7542       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC_VOLATILE)
7543     return 0;
7544
7545   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
7546   src_regno  = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
7547
7548   if (dest_regno != VRSAVE_REGNO
7549       && src_regno != VRSAVE_REGNO)
7550     return 0;
7551
7552   for (i = 1; i < count; i++)
7553     {
7554       rtx elt = XVECEXP (op, 0, i);
7555
7556       if (GET_CODE (elt) != CLOBBER
7557           && GET_CODE (elt) != SET)
7558         return 0;
7559     }
7560
7561   return 1;
7562 }
7563
7564 /* Return 1 for an PARALLEL suitable for mfcr.  */
7565
7566 int
7567 mfcr_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7568 {
7569   int count = XVECLEN (op, 0);
7570   int i;
7571
7572   /* Perform a quick check so we don't blow up below.  */
7573   if (count < 1
7574       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7575       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC
7576       || XVECLEN (SET_SRC (XVECEXP (op, 0, 0)), 0) != 2)
7577     return 0;
7578
7579   for (i = 0; i < count; i++)
7580     {
7581       rtx exp = XVECEXP (op, 0, i);
7582       rtx unspec;
7583       int maskval;
7584       rtx src_reg;
7585
7586       src_reg = XVECEXP (SET_SRC (exp), 0, 0);
7587
7588       if (GET_CODE (src_reg) != REG
7589           || GET_MODE (src_reg) != CCmode
7590           || ! CR_REGNO_P (REGNO (src_reg)))
7591         return 0;
7592
7593       if (GET_CODE (exp) != SET
7594           || GET_CODE (SET_DEST (exp)) != REG
7595           || GET_MODE (SET_DEST (exp)) != SImode
7596           || ! INT_REGNO_P (REGNO (SET_DEST (exp))))
7597         return 0;
7598       unspec = SET_SRC (exp);
7599       maskval = 1 << (MAX_CR_REGNO - REGNO (src_reg));
7600
7601       if (GET_CODE (unspec) != UNSPEC
7602           || XINT (unspec, 1) != UNSPEC_MOVESI_FROM_CR
7603           || XVECLEN (unspec, 0) != 2
7604           || XVECEXP (unspec, 0, 0) != src_reg
7605           || GET_CODE (XVECEXP (unspec, 0, 1)) != CONST_INT
7606           || INTVAL (XVECEXP (unspec, 0, 1)) != maskval)
7607         return 0;
7608     }
7609   return 1;
7610 }
7611
7612 /* Return 1 for an PARALLEL suitable for mtcrf.  */
7613
7614 int
7615 mtcrf_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7616 {
7617   int count = XVECLEN (op, 0);
7618   int i;
7619   rtx src_reg;
7620
7621   /* Perform a quick check so we don't blow up below.  */
7622   if (count < 1
7623       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7624       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC
7625       || XVECLEN (SET_SRC (XVECEXP (op, 0, 0)), 0) != 2)
7626     return 0;
7627   src_reg = XVECEXP (SET_SRC (XVECEXP (op, 0, 0)), 0, 0);
7628   
7629   if (GET_CODE (src_reg) != REG
7630       || GET_MODE (src_reg) != SImode
7631       || ! INT_REGNO_P (REGNO (src_reg)))
7632     return 0;
7633
7634   for (i = 0; i < count; i++)
7635     {
7636       rtx exp = XVECEXP (op, 0, i);
7637       rtx unspec;
7638       int maskval;
7639       
7640       if (GET_CODE (exp) != SET
7641           || GET_CODE (SET_DEST (exp)) != REG
7642           || GET_MODE (SET_DEST (exp)) != CCmode
7643           || ! CR_REGNO_P (REGNO (SET_DEST (exp))))
7644         return 0;
7645       unspec = SET_SRC (exp);
7646       maskval = 1 << (MAX_CR_REGNO - REGNO (SET_DEST (exp)));
7647       
7648       if (GET_CODE (unspec) != UNSPEC
7649           || XINT (unspec, 1) != UNSPEC_MOVESI_TO_CR
7650           || XVECLEN (unspec, 0) != 2
7651           || XVECEXP (unspec, 0, 0) != src_reg
7652           || GET_CODE (XVECEXP (unspec, 0, 1)) != CONST_INT
7653           || INTVAL (XVECEXP (unspec, 0, 1)) != maskval)
7654         return 0;
7655     }
7656   return 1;
7657 }
7658
7659 /* Return 1 for an PARALLEL suitable for lmw.  */
7660
7661 int
7662 lmw_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7663 {
7664   int count = XVECLEN (op, 0);
7665   unsigned int dest_regno;
7666   rtx src_addr;
7667   unsigned int base_regno;
7668   HOST_WIDE_INT offset;
7669   int i;
7670
7671   /* Perform a quick check so we don't blow up below.  */
7672   if (count <= 1
7673       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7674       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
7675       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
7676     return 0;
7677
7678   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
7679   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
7680
7681   if (dest_regno > 31
7682       || count != 32 - (int) dest_regno)
7683     return 0;
7684
7685   if (legitimate_indirect_address_p (src_addr, 0))
7686     {
7687       offset = 0;
7688       base_regno = REGNO (src_addr);
7689       if (base_regno == 0)
7690         return 0;
7691     }
7692   else if (legitimate_offset_address_p (SImode, src_addr, 0))
7693     {
7694       offset = INTVAL (XEXP (src_addr, 1));
7695       base_regno = REGNO (XEXP (src_addr, 0));
7696     }
7697   else
7698     return 0;
7699
7700   for (i = 0; i < count; i++)
7701     {
7702       rtx elt = XVECEXP (op, 0, i);
7703       rtx newaddr;
7704       rtx addr_reg;
7705       HOST_WIDE_INT newoffset;
7706
7707       if (GET_CODE (elt) != SET
7708           || GET_CODE (SET_DEST (elt)) != REG
7709           || GET_MODE (SET_DEST (elt)) != SImode
7710           || REGNO (SET_DEST (elt)) != dest_regno + i
7711           || GET_CODE (SET_SRC (elt)) != MEM
7712           || GET_MODE (SET_SRC (elt)) != SImode)
7713         return 0;
7714       newaddr = XEXP (SET_SRC (elt), 0);
7715       if (legitimate_indirect_address_p (newaddr, 0))
7716         {
7717           newoffset = 0;
7718           addr_reg = newaddr;
7719         }
7720       else if (legitimate_offset_address_p (SImode, newaddr, 0))
7721         {
7722           addr_reg = XEXP (newaddr, 0);
7723           newoffset = INTVAL (XEXP (newaddr, 1));
7724         }
7725       else
7726         return 0;
7727       if (REGNO (addr_reg) != base_regno
7728           || newoffset != offset + 4 * i)
7729         return 0;
7730     }
7731
7732   return 1;
7733 }
7734
7735 /* Return 1 for an PARALLEL suitable for stmw.  */
7736
7737 int
7738 stmw_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7739 {
7740   int count = XVECLEN (op, 0);
7741   unsigned int src_regno;
7742   rtx dest_addr;
7743   unsigned int base_regno;
7744   HOST_WIDE_INT offset;
7745   int i;
7746
7747   /* Perform a quick check so we don't blow up below.  */
7748   if (count <= 1
7749       || GET_CODE (XVECEXP (op, 0, 0)) != SET
7750       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
7751       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
7752     return 0;
7753
7754   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
7755   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
7756
7757   if (src_regno > 31
7758       || count != 32 - (int) src_regno)
7759     return 0;
7760
7761   if (legitimate_indirect_address_p (dest_addr, 0))
7762     {
7763       offset = 0;
7764       base_regno = REGNO (dest_addr);
7765       if (base_regno == 0)
7766         return 0;
7767     }
7768   else if (legitimate_offset_address_p (SImode, dest_addr, 0))
7769     {
7770       offset = INTVAL (XEXP (dest_addr, 1));
7771       base_regno = REGNO (XEXP (dest_addr, 0));
7772     }
7773   else
7774     return 0;
7775
7776   for (i = 0; i < count; i++)
7777     {
7778       rtx elt = XVECEXP (op, 0, i);
7779       rtx newaddr;
7780       rtx addr_reg;
7781       HOST_WIDE_INT newoffset;
7782
7783       if (GET_CODE (elt) != SET
7784           || GET_CODE (SET_SRC (elt)) != REG
7785           || GET_MODE (SET_SRC (elt)) != SImode
7786           || REGNO (SET_SRC (elt)) != src_regno + i
7787           || GET_CODE (SET_DEST (elt)) != MEM
7788           || GET_MODE (SET_DEST (elt)) != SImode)
7789         return 0;
7790       newaddr = XEXP (SET_DEST (elt), 0);
7791       if (legitimate_indirect_address_p (newaddr, 0))
7792         {
7793           newoffset = 0;
7794           addr_reg = newaddr;
7795         }
7796       else if (legitimate_offset_address_p (SImode, newaddr, 0))
7797         {
7798           addr_reg = XEXP (newaddr, 0);
7799           newoffset = INTVAL (XEXP (newaddr, 1));
7800         }
7801       else
7802         return 0;
7803       if (REGNO (addr_reg) != base_regno
7804           || newoffset != offset + 4 * i)
7805         return 0;
7806     }
7807
7808   return 1;
7809 }
7810 \f
7811 /* A validation routine: say whether CODE, a condition code, and MODE
7812    match.  The other alternatives either don't make sense or should
7813    never be generated.  */
7814
7815 static void
7816 validate_condition_mode (enum rtx_code code, enum machine_mode mode)
7817 {
7818   if (GET_RTX_CLASS (code) != '<' 
7819       || GET_MODE_CLASS (mode) != MODE_CC)
7820     abort ();
7821
7822   /* These don't make sense.  */
7823   if ((code == GT || code == LT || code == GE || code == LE)
7824       && mode == CCUNSmode)
7825     abort ();
7826
7827   if ((code == GTU || code == LTU || code == GEU || code == LEU)
7828       && mode != CCUNSmode)
7829     abort ();
7830
7831   if (mode != CCFPmode
7832       && (code == ORDERED || code == UNORDERED
7833           || code == UNEQ || code == LTGT
7834           || code == UNGT || code == UNLT
7835           || code == UNGE || code == UNLE))
7836     abort ();
7837   
7838   /* These should never be generated except for 
7839      flag_finite_math_only.  */
7840   if (mode == CCFPmode
7841       && ! flag_finite_math_only
7842       && (code == LE || code == GE
7843           || code == UNEQ || code == LTGT
7844           || code == UNGT || code == UNLT))
7845     abort ();
7846
7847   /* These are invalid; the information is not there.  */
7848   if (mode == CCEQmode 
7849       && code != EQ && code != NE)
7850     abort ();
7851 }
7852
7853 /* Return 1 if OP is a comparison operation that is valid for a branch insn.
7854    We only check the opcode against the mode of the CC value here.  */
7855
7856 int
7857 branch_comparison_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7858 {
7859   enum rtx_code code = GET_CODE (op);
7860   enum machine_mode cc_mode;
7861
7862   if (GET_RTX_CLASS (code) != '<')
7863     return 0;
7864
7865   cc_mode = GET_MODE (XEXP (op, 0));
7866   if (GET_MODE_CLASS (cc_mode) != MODE_CC)
7867     return 0;
7868
7869   validate_condition_mode (code, cc_mode);
7870
7871   return 1;
7872 }
7873
7874 /* Return 1 if OP is a comparison operation that is valid for a branch
7875    insn and which is true if the corresponding bit in the CC register
7876    is set.  */
7877
7878 int
7879 branch_positive_comparison_operator (rtx op, enum machine_mode mode)
7880 {
7881   enum rtx_code code;
7882
7883   if (! branch_comparison_operator (op, mode))
7884     return 0;
7885
7886   code = GET_CODE (op);
7887   return (code == EQ || code == LT || code == GT
7888           || (TARGET_E500 && TARGET_HARD_FLOAT && !TARGET_FPRS && code == NE)
7889           || code == LTU || code == GTU
7890           || code == UNORDERED);
7891 }
7892
7893 /* Return 1 if OP is a comparison operation that is valid for an scc
7894    insn: it must be a positive comparison.  */
7895
7896 int
7897 scc_comparison_operator (rtx op, enum machine_mode mode)
7898 {
7899   return branch_positive_comparison_operator (op, mode);
7900 }
7901
7902 int
7903 trap_comparison_operator (rtx op, enum machine_mode mode)
7904 {
7905   if (mode != VOIDmode && mode != GET_MODE (op))
7906     return 0;
7907   return GET_RTX_CLASS (GET_CODE (op)) == '<';
7908 }
7909
7910 int
7911 boolean_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7912 {
7913   enum rtx_code code = GET_CODE (op);
7914   return (code == AND || code == IOR || code == XOR);
7915 }
7916
7917 int
7918 boolean_or_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7919 {
7920   enum rtx_code code = GET_CODE (op);
7921   return (code == IOR || code == XOR);
7922 }
7923
7924 int
7925 min_max_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
7926 {
7927   enum rtx_code code = GET_CODE (op);
7928   return (code == SMIN || code == SMAX || code == UMIN || code == UMAX);
7929 }
7930 \f
7931 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
7932    mask required to convert the result of a rotate insn into a shift
7933    left insn of SHIFTOP bits.  Both are known to be SImode CONST_INT.  */
7934
7935 int
7936 includes_lshift_p (rtx shiftop, rtx andop)
7937 {
7938   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
7939
7940   shift_mask <<= INTVAL (shiftop);
7941
7942   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
7943 }
7944
7945 /* Similar, but for right shift.  */
7946
7947 int
7948 includes_rshift_p (rtx shiftop, rtx andop)
7949 {
7950   unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
7951
7952   shift_mask >>= INTVAL (shiftop);
7953
7954   return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
7955 }
7956
7957 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
7958    to perform a left shift.  It must have exactly SHIFTOP least
7959    significant 0's, then one or more 1's, then zero or more 0's.  */
7960
7961 int
7962 includes_rldic_lshift_p (rtx shiftop, rtx andop)
7963 {
7964   if (GET_CODE (andop) == CONST_INT)
7965     {
7966       HOST_WIDE_INT c, lsb, shift_mask;
7967
7968       c = INTVAL (andop);
7969       if (c == 0 || c == ~0)
7970         return 0;
7971
7972       shift_mask = ~0;
7973       shift_mask <<= INTVAL (shiftop);
7974
7975       /* Find the least significant one bit.  */
7976       lsb = c & -c;
7977
7978       /* It must coincide with the LSB of the shift mask.  */
7979       if (-lsb != shift_mask)
7980         return 0;
7981
7982       /* Invert to look for the next transition (if any).  */
7983       c = ~c;
7984
7985       /* Remove the low group of ones (originally low group of zeros).  */
7986       c &= -lsb;
7987
7988       /* Again find the lsb, and check we have all 1's above.  */
7989       lsb = c & -c;
7990       return c == -lsb;
7991     }
7992   else if (GET_CODE (andop) == CONST_DOUBLE
7993            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
7994     {
7995       HOST_WIDE_INT low, high, lsb;
7996       HOST_WIDE_INT shift_mask_low, shift_mask_high;
7997
7998       low = CONST_DOUBLE_LOW (andop);
7999       if (HOST_BITS_PER_WIDE_INT < 64)
8000         high = CONST_DOUBLE_HIGH (andop);
8001
8002       if ((low == 0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == 0))
8003           || (low == ~0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0)))
8004         return 0;
8005
8006       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
8007         {
8008           shift_mask_high = ~0;
8009           if (INTVAL (shiftop) > 32)
8010             shift_mask_high <<= INTVAL (shiftop) - 32;
8011
8012           lsb = high & -high;
8013
8014           if (-lsb != shift_mask_high || INTVAL (shiftop) < 32)
8015             return 0;
8016
8017           high = ~high;
8018           high &= -lsb;
8019
8020           lsb = high & -high;
8021           return high == -lsb;
8022         }
8023
8024       shift_mask_low = ~0;
8025       shift_mask_low <<= INTVAL (shiftop);
8026
8027       lsb = low & -low;
8028
8029       if (-lsb != shift_mask_low)
8030         return 0;
8031
8032       if (HOST_BITS_PER_WIDE_INT < 64)
8033         high = ~high;
8034       low = ~low;
8035       low &= -lsb;
8036
8037       if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
8038         {
8039           lsb = high & -high;
8040           return high == -lsb;
8041         }
8042
8043       lsb = low & -low;
8044       return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
8045     }
8046   else
8047     return 0;
8048 }
8049
8050 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
8051    to perform a left shift.  It must have SHIFTOP or more least
8052    significant 0's, with the remainder of the word 1's.  */
8053
8054 int
8055 includes_rldicr_lshift_p (rtx shiftop, rtx andop)
8056 {
8057   if (GET_CODE (andop) == CONST_INT)
8058     {
8059       HOST_WIDE_INT c, lsb, shift_mask;
8060
8061       shift_mask = ~0;
8062       shift_mask <<= INTVAL (shiftop);
8063       c = INTVAL (andop);
8064
8065       /* Find the least significant one bit.  */
8066       lsb = c & -c;
8067
8068       /* It must be covered by the shift mask.
8069          This test also rejects c == 0.  */
8070       if ((lsb & shift_mask) == 0)
8071         return 0;
8072
8073       /* Check we have all 1's above the transition, and reject all 1's.  */
8074       return c == -lsb && lsb != 1;
8075     }
8076   else if (GET_CODE (andop) == CONST_DOUBLE
8077            && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
8078     {
8079       HOST_WIDE_INT low, lsb, shift_mask_low;
8080
8081       low = CONST_DOUBLE_LOW (andop);
8082
8083       if (HOST_BITS_PER_WIDE_INT < 64)
8084         {
8085           HOST_WIDE_INT high, shift_mask_high;
8086
8087           high = CONST_DOUBLE_HIGH (andop);
8088
8089           if (low == 0)
8090             {
8091               shift_mask_high = ~0;
8092               if (INTVAL (shiftop) > 32)
8093                 shift_mask_high <<= INTVAL (shiftop) - 32;
8094
8095               lsb = high & -high;
8096
8097               if ((lsb & shift_mask_high) == 0)
8098                 return 0;
8099
8100               return high == -lsb;
8101             }
8102           if (high != ~0)
8103             return 0;
8104         }
8105
8106       shift_mask_low = ~0;
8107       shift_mask_low <<= INTVAL (shiftop);
8108
8109       lsb = low & -low;
8110
8111       if ((lsb & shift_mask_low) == 0)
8112         return 0;
8113
8114       return low == -lsb && lsb != 1;
8115     }
8116   else
8117     return 0;
8118 }
8119
8120 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
8121    for lfq and stfq insns.
8122
8123    Note reg1 and reg2 *must* be hard registers.  To be sure we will
8124    abort if we are passed pseudo registers.  */
8125
8126 int
8127 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
8128 {
8129   /* We might have been passed a SUBREG.  */
8130   if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG) 
8131     return 0;
8132
8133   return (REGNO (reg1) == REGNO (reg2) - 1);
8134 }
8135
8136 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
8137    addr1 and addr2 must be in consecutive memory locations
8138    (addr2 == addr1 + 8).  */
8139
8140 int
8141 addrs_ok_for_quad_peep (rtx addr1, rtx addr2)
8142 {
8143   unsigned int reg1;
8144   int offset1;
8145
8146   /* Extract an offset (if used) from the first addr.  */
8147   if (GET_CODE (addr1) == PLUS)
8148     {
8149       /* If not a REG, return zero.  */
8150       if (GET_CODE (XEXP (addr1, 0)) != REG)
8151         return 0;
8152       else
8153         {
8154           reg1 = REGNO (XEXP (addr1, 0));
8155           /* The offset must be constant!  */
8156           if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
8157             return 0;
8158           offset1 = INTVAL (XEXP (addr1, 1));
8159         }
8160     }
8161   else if (GET_CODE (addr1) != REG)
8162     return 0;
8163   else
8164     {
8165       reg1 = REGNO (addr1);
8166       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
8167       offset1 = 0;
8168     }
8169
8170   /* Make sure the second address is a (mem (plus (reg) (const_int)))
8171       or if it is (mem (reg)) then make sure that offset1 is -8 and the same 
8172       register as addr1.  */
8173   if (offset1 == -8 && GET_CODE (addr2) == REG && reg1 == REGNO (addr2))
8174    return 1;
8175   if (GET_CODE (addr2) != PLUS)
8176     return 0;
8177
8178   if (GET_CODE (XEXP (addr2, 0)) != REG
8179       || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
8180     return 0;
8181
8182   if (reg1 != REGNO (XEXP (addr2, 0)))
8183     return 0;
8184
8185   /* The offset for the second addr must be 8 more than the first addr.  */
8186   if (INTVAL (XEXP (addr2, 1)) != offset1 + 8)
8187     return 0;
8188
8189   /* All the tests passed.  addr1 and addr2 are valid for lfq or stfq
8190      instructions.  */
8191   return 1;
8192 }
8193 \f
8194 /* Return the register class of a scratch register needed to copy IN into
8195    or out of a register in CLASS in MODE.  If it can be done directly,
8196    NO_REGS is returned.  */
8197
8198 enum reg_class
8199 secondary_reload_class (enum reg_class class, 
8200                 enum machine_mode mode ATTRIBUTE_UNUSED, rtx in)
8201 {
8202   int regno;
8203
8204   if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
8205 #if TARGET_MACHO
8206                     && MACHOPIC_INDIRECT
8207 #endif
8208                     ))
8209     {
8210       /* We cannot copy a symbolic operand directly into anything
8211          other than BASE_REGS for TARGET_ELF.  So indicate that a
8212          register from BASE_REGS is needed as an intermediate
8213          register.
8214          
8215          On Darwin, pic addresses require a load from memory, which
8216          needs a base register.  */
8217       if (class != BASE_REGS
8218           && (GET_CODE (in) == SYMBOL_REF
8219               || GET_CODE (in) == HIGH
8220               || GET_CODE (in) == LABEL_REF
8221               || GET_CODE (in) == CONST))
8222         return BASE_REGS;
8223     }
8224
8225   if (GET_CODE (in) == REG)
8226     {
8227       regno = REGNO (in);
8228       if (regno >= FIRST_PSEUDO_REGISTER)
8229         {
8230           regno = true_regnum (in);
8231           if (regno >= FIRST_PSEUDO_REGISTER)
8232             regno = -1;
8233         }
8234     }
8235   else if (GET_CODE (in) == SUBREG)
8236     {
8237       regno = true_regnum (in);
8238       if (regno >= FIRST_PSEUDO_REGISTER)
8239         regno = -1;
8240     }
8241   else
8242     regno = -1;
8243
8244   /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
8245      into anything.  */
8246   if (class == GENERAL_REGS || class == BASE_REGS
8247       || (regno >= 0 && INT_REGNO_P (regno)))
8248     return NO_REGS;
8249
8250   /* Constants, memory, and FP registers can go into FP registers.  */
8251   if ((regno == -1 || FP_REGNO_P (regno))
8252       && (class == FLOAT_REGS || class == NON_SPECIAL_REGS))
8253     return NO_REGS;
8254
8255   /* Memory, and AltiVec registers can go into AltiVec registers.  */
8256   if ((regno == -1 || ALTIVEC_REGNO_P (regno))
8257       && class == ALTIVEC_REGS)
8258     return NO_REGS;
8259
8260   /* We can copy among the CR registers.  */
8261   if ((class == CR_REGS || class == CR0_REGS)
8262       && regno >= 0 && CR_REGNO_P (regno))
8263     return NO_REGS;
8264
8265   /* Otherwise, we need GENERAL_REGS.  */
8266   return GENERAL_REGS;
8267 }
8268 \f
8269 /* Given a comparison operation, return the bit number in CCR to test.  We
8270    know this is a valid comparison.  
8271
8272    SCC_P is 1 if this is for an scc.  That means that %D will have been
8273    used instead of %C, so the bits will be in different places.
8274
8275    Return -1 if OP isn't a valid comparison for some reason.  */
8276
8277 int
8278 ccr_bit (rtx op, int scc_p)
8279 {
8280   enum rtx_code code = GET_CODE (op);
8281   enum machine_mode cc_mode;
8282   int cc_regnum;
8283   int base_bit;
8284   rtx reg;
8285
8286   if (GET_RTX_CLASS (code) != '<')
8287     return -1;
8288
8289   reg = XEXP (op, 0);
8290
8291   if (GET_CODE (reg) != REG
8292       || ! CR_REGNO_P (REGNO (reg)))
8293     abort ();
8294
8295   cc_mode = GET_MODE (reg);
8296   cc_regnum = REGNO (reg);
8297   base_bit = 4 * (cc_regnum - CR0_REGNO);
8298
8299   validate_condition_mode (code, cc_mode);
8300
8301   /* When generating a sCOND operation, only positive conditions are
8302      allowed.  */
8303   if (scc_p && code != EQ && code != GT && code != LT && code != UNORDERED
8304       && code != GTU && code != LTU)
8305     abort ();
8306   
8307   switch (code)
8308     {
8309     case NE:
8310       if (TARGET_E500 && !TARGET_FPRS
8311           && TARGET_HARD_FLOAT && cc_mode == CCFPmode)
8312         return base_bit + 1;
8313       return scc_p ? base_bit + 3 : base_bit + 2;
8314     case EQ:
8315       if (TARGET_E500 && !TARGET_FPRS
8316           && TARGET_HARD_FLOAT && cc_mode == CCFPmode)
8317         return base_bit + 1;
8318       return base_bit + 2;
8319     case GT:  case GTU:  case UNLE:
8320       return base_bit + 1;
8321     case LT:  case LTU:  case UNGE:
8322       return base_bit;
8323     case ORDERED:  case UNORDERED:
8324       return base_bit + 3;
8325
8326     case GE:  case GEU:
8327       /* If scc, we will have done a cror to put the bit in the
8328          unordered position.  So test that bit.  For integer, this is ! LT
8329          unless this is an scc insn.  */
8330       return scc_p ? base_bit + 3 : base_bit;
8331
8332     case LE:  case LEU:
8333       return scc_p ? base_bit + 3 : base_bit + 1;
8334
8335     default:
8336       abort ();
8337     }
8338 }
8339 \f
8340 /* Return the GOT register.  */
8341
8342 struct rtx_def *
8343 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
8344 {
8345   /* The second flow pass currently (June 1999) can't update
8346      regs_ever_live without disturbing other parts of the compiler, so
8347      update it here to make the prolog/epilogue code happy.  */
8348   if (no_new_pseudos && ! regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM])
8349     regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
8350
8351   current_function_uses_pic_offset_table = 1;
8352
8353   return pic_offset_table_rtx;
8354 }
8355 \f
8356 /* Function to init struct machine_function.
8357    This will be called, via a pointer variable,
8358    from push_function_context.  */
8359
8360 static struct machine_function *
8361 rs6000_init_machine_status (void)
8362 {
8363   return ggc_alloc_cleared (sizeof (machine_function));
8364 }
8365 \f
8366 /* These macros test for integers and extract the low-order bits.  */
8367 #define INT_P(X)  \
8368 ((GET_CODE (X) == CONST_INT || GET_CODE (X) == CONST_DOUBLE)    \
8369  && GET_MODE (X) == VOIDmode)
8370
8371 #define INT_LOWPART(X) \
8372   (GET_CODE (X) == CONST_INT ? INTVAL (X) : CONST_DOUBLE_LOW (X))
8373
8374 int
8375 extract_MB (rtx op)
8376 {
8377   int i;
8378   unsigned long val = INT_LOWPART (op);
8379
8380   /* If the high bit is zero, the value is the first 1 bit we find
8381      from the left.  */
8382   if ((val & 0x80000000) == 0)
8383     {
8384       if ((val & 0xffffffff) == 0)
8385         abort ();
8386
8387       i = 1;
8388       while (((val <<= 1) & 0x80000000) == 0)
8389         ++i;
8390       return i;
8391     }
8392
8393   /* If the high bit is set and the low bit is not, or the mask is all
8394      1's, the value is zero.  */
8395   if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
8396     return 0;
8397
8398   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
8399      from the right.  */
8400   i = 31;
8401   while (((val >>= 1) & 1) != 0)
8402     --i;
8403
8404   return i;
8405 }
8406
8407 int
8408 extract_ME (rtx op)
8409 {
8410   int i;
8411   unsigned long val = INT_LOWPART (op);
8412
8413   /* If the low bit is zero, the value is the first 1 bit we find from
8414      the right.  */
8415   if ((val & 1) == 0)
8416     {
8417       if ((val & 0xffffffff) == 0)
8418         abort ();
8419
8420       i = 30;
8421       while (((val >>= 1) & 1) == 0)
8422         --i;
8423
8424       return i;
8425     }
8426
8427   /* If the low bit is set and the high bit is not, or the mask is all
8428      1's, the value is 31.  */
8429   if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
8430     return 31;
8431
8432   /* Otherwise we have a wrap-around mask.  Look for the first 0 bit
8433      from the left.  */
8434   i = 0;
8435   while (((val <<= 1) & 0x80000000) != 0)
8436     ++i;
8437
8438   return i;
8439 }
8440
8441 /* Locate some local-dynamic symbol still in use by this function
8442    so that we can print its name in some tls_ld pattern.  */
8443
8444 static const char *
8445 rs6000_get_some_local_dynamic_name (void)
8446 {
8447   rtx insn;
8448
8449   if (cfun->machine->some_ld_name)
8450     return cfun->machine->some_ld_name;
8451
8452   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
8453     if (INSN_P (insn)
8454         && for_each_rtx (&PATTERN (insn),
8455                          rs6000_get_some_local_dynamic_name_1, 0))
8456       return cfun->machine->some_ld_name;
8457
8458   abort ();
8459 }
8460
8461 /* Helper function for rs6000_get_some_local_dynamic_name.  */
8462
8463 static int
8464 rs6000_get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
8465 {
8466   rtx x = *px;
8467
8468   if (GET_CODE (x) == SYMBOL_REF)
8469     {
8470       const char *str = XSTR (x, 0);
8471       if (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
8472         {
8473           cfun->machine->some_ld_name = str;
8474           return 1;
8475         }
8476     }
8477
8478   return 0;
8479 }
8480
8481 /* Print an operand.  Recognize special options, documented below.  */
8482
8483 #if TARGET_ELF
8484 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
8485 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
8486 #else
8487 #define SMALL_DATA_RELOC "sda21"
8488 #define SMALL_DATA_REG 0
8489 #endif
8490
8491 void
8492 print_operand (FILE *file, rtx x, int code)
8493 {
8494   int i;
8495   HOST_WIDE_INT val;
8496   unsigned HOST_WIDE_INT uval;
8497
8498   switch (code)
8499     {
8500     case '.':
8501       /* Write out an instruction after the call which may be replaced
8502          with glue code by the loader.  This depends on the AIX version.  */
8503       asm_fprintf (file, RS6000_CALL_GLUE);
8504       return;
8505
8506       /* %a is output_address.  */
8507
8508     case 'A':
8509       /* If X is a constant integer whose low-order 5 bits are zero,
8510          write 'l'.  Otherwise, write 'r'.  This is a kludge to fix a bug
8511          in the AIX assembler where "sri" with a zero shift count
8512          writes a trash instruction.  */
8513       if (GET_CODE (x) == CONST_INT && (INTVAL (x) & 31) == 0)
8514         putc ('l', file);
8515       else
8516         putc ('r', file);
8517       return;
8518
8519     case 'b':
8520       /* If constant, low-order 16 bits of constant, unsigned.
8521          Otherwise, write normally.  */
8522       if (INT_P (x))
8523         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 0xffff);
8524       else
8525         print_operand (file, x, 0);
8526       return;
8527
8528     case 'B':
8529       /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
8530          for 64-bit mask direction.  */
8531       putc (((INT_LOWPART(x) & 1) == 0 ? 'r' : 'l'), file);
8532       return;
8533
8534       /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
8535          output_operand.  */
8536
8537     case 'E':
8538       /* X is a CR register.  Print the number of the EQ bit of the CR */
8539       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
8540         output_operand_lossage ("invalid %%E value");
8541       else
8542         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
8543       return;
8544
8545     case 'f':
8546       /* X is a CR register.  Print the shift count needed to move it
8547          to the high-order four bits.  */
8548       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
8549         output_operand_lossage ("invalid %%f value");
8550       else
8551         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
8552       return;
8553
8554     case 'F':
8555       /* Similar, but print the count for the rotate in the opposite
8556          direction.  */
8557       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
8558         output_operand_lossage ("invalid %%F value");
8559       else
8560         fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
8561       return;
8562
8563     case 'G':
8564       /* X is a constant integer.  If it is negative, print "m",
8565          otherwise print "z".  This is to make an aze or ame insn.  */
8566       if (GET_CODE (x) != CONST_INT)
8567         output_operand_lossage ("invalid %%G value");
8568       else if (INTVAL (x) >= 0)
8569         putc ('z', file);
8570       else
8571         putc ('m', file);
8572       return;
8573
8574     case 'h':
8575       /* If constant, output low-order five bits.  Otherwise, write
8576          normally.  */
8577       if (INT_P (x))
8578         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 31);
8579       else
8580         print_operand (file, x, 0);
8581       return;
8582
8583     case 'H':
8584       /* If constant, output low-order six bits.  Otherwise, write
8585          normally.  */
8586       if (INT_P (x))
8587         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 63);
8588       else
8589         print_operand (file, x, 0);
8590       return;
8591
8592     case 'I':
8593       /* Print `i' if this is a constant, else nothing.  */
8594       if (INT_P (x))
8595         putc ('i', file);
8596       return;
8597
8598     case 'j':
8599       /* Write the bit number in CCR for jump.  */
8600       i = ccr_bit (x, 0);
8601       if (i == -1)
8602         output_operand_lossage ("invalid %%j code");
8603       else
8604         fprintf (file, "%d", i);
8605       return;
8606
8607     case 'J':
8608       /* Similar, but add one for shift count in rlinm for scc and pass
8609          scc flag to `ccr_bit'.  */
8610       i = ccr_bit (x, 1);
8611       if (i == -1)
8612         output_operand_lossage ("invalid %%J code");
8613       else
8614         /* If we want bit 31, write a shift count of zero, not 32.  */
8615         fprintf (file, "%d", i == 31 ? 0 : i + 1);
8616       return;
8617
8618     case 'k':
8619       /* X must be a constant.  Write the 1's complement of the
8620          constant.  */
8621       if (! INT_P (x))
8622         output_operand_lossage ("invalid %%k value");
8623       else
8624         fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INT_LOWPART (x));
8625       return;
8626
8627     case 'K':
8628       /* X must be a symbolic constant on ELF.  Write an
8629          expression suitable for an 'addi' that adds in the low 16
8630          bits of the MEM.  */
8631       if (GET_CODE (x) != CONST)
8632         {
8633           print_operand_address (file, x);
8634           fputs ("@l", file);
8635         }
8636       else
8637         {
8638           if (GET_CODE (XEXP (x, 0)) != PLUS
8639               || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
8640                   && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
8641               || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
8642             output_operand_lossage ("invalid %%K value");
8643           print_operand_address (file, XEXP (XEXP (x, 0), 0));
8644           fputs ("@l", file);
8645           /* For GNU as, there must be a non-alphanumeric character
8646              between 'l' and the number.  The '-' is added by
8647              print_operand() already.  */
8648           if (INTVAL (XEXP (XEXP (x, 0), 1)) >= 0)
8649             fputs ("+", file);
8650           print_operand (file, XEXP (XEXP (x, 0), 1), 0);
8651         }
8652       return;
8653
8654       /* %l is output_asm_label.  */
8655
8656     case 'L':
8657       /* Write second word of DImode or DFmode reference.  Works on register
8658          or non-indexed memory only.  */
8659       if (GET_CODE (x) == REG)
8660         fprintf (file, "%s", reg_names[REGNO (x) + 1]);
8661       else if (GET_CODE (x) == MEM)
8662         {
8663           /* Handle possible auto-increment.  Since it is pre-increment and
8664              we have already done it, we can just use an offset of word.  */
8665           if (GET_CODE (XEXP (x, 0)) == PRE_INC
8666               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
8667             output_address (plus_constant (XEXP (XEXP (x, 0), 0),
8668                                            UNITS_PER_WORD));
8669           else
8670             output_address (XEXP (adjust_address_nv (x, SImode,
8671                                                      UNITS_PER_WORD),
8672                                   0));
8673
8674           if (small_data_operand (x, GET_MODE (x)))
8675             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
8676                      reg_names[SMALL_DATA_REG]);
8677         }
8678       return;
8679                             
8680     case 'm':
8681       /* MB value for a mask operand.  */
8682       if (! mask_operand (x, SImode))
8683         output_operand_lossage ("invalid %%m value");
8684
8685       fprintf (file, "%d", extract_MB (x));
8686       return;
8687
8688     case 'M':
8689       /* ME value for a mask operand.  */
8690       if (! mask_operand (x, SImode))
8691         output_operand_lossage ("invalid %%M value");
8692
8693       fprintf (file, "%d", extract_ME (x));
8694       return;
8695
8696       /* %n outputs the negative of its operand.  */
8697
8698     case 'N':
8699       /* Write the number of elements in the vector times 4.  */
8700       if (GET_CODE (x) != PARALLEL)
8701         output_operand_lossage ("invalid %%N value");
8702       else
8703         fprintf (file, "%d", XVECLEN (x, 0) * 4);
8704       return;
8705
8706     case 'O':
8707       /* Similar, but subtract 1 first.  */
8708       if (GET_CODE (x) != PARALLEL)
8709         output_operand_lossage ("invalid %%O value");
8710       else
8711         fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
8712       return;
8713
8714     case 'p':
8715       /* X is a CONST_INT that is a power of two.  Output the logarithm.  */
8716       if (! INT_P (x)
8717           || INT_LOWPART (x) < 0
8718           || (i = exact_log2 (INT_LOWPART (x))) < 0)
8719         output_operand_lossage ("invalid %%p value");
8720       else
8721         fprintf (file, "%d", i);
8722       return;
8723
8724     case 'P':
8725       /* The operand must be an indirect memory reference.  The result
8726          is the register number.  */
8727       if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
8728           || REGNO (XEXP (x, 0)) >= 32)
8729         output_operand_lossage ("invalid %%P value");
8730       else
8731         fprintf (file, "%d", REGNO (XEXP (x, 0)));
8732       return;
8733
8734     case 'q':
8735       /* This outputs the logical code corresponding to a boolean
8736          expression.  The expression may have one or both operands
8737          negated (if one, only the first one).  For condition register
8738          logical operations, it will also treat the negated
8739          CR codes as NOTs, but not handle NOTs of them.  */
8740       {
8741         const char *const *t = 0;
8742         const char *s;
8743         enum rtx_code code = GET_CODE (x);
8744         static const char * const tbl[3][3] = {
8745           { "and", "andc", "nor" },
8746           { "or", "orc", "nand" },
8747           { "xor", "eqv", "xor" } };
8748
8749         if (code == AND)
8750           t = tbl[0];
8751         else if (code == IOR)
8752           t = tbl[1];
8753         else if (code == XOR)
8754           t = tbl[2];
8755         else
8756           output_operand_lossage ("invalid %%q value");
8757
8758         if (GET_CODE (XEXP (x, 0)) != NOT)
8759           s = t[0];
8760         else
8761           {
8762             if (GET_CODE (XEXP (x, 1)) == NOT)
8763               s = t[2];
8764             else
8765               s = t[1];
8766           }
8767         
8768         fputs (s, file);
8769       }
8770       return;
8771
8772     case 'Q':
8773       if (TARGET_MFCRF)
8774         fputc (',',file);
8775         /* FALLTHRU */
8776       else
8777         return;
8778
8779     case 'R':
8780       /* X is a CR register.  Print the mask for `mtcrf'.  */
8781       if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
8782         output_operand_lossage ("invalid %%R value");
8783       else
8784         fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
8785       return;
8786
8787     case 's':
8788       /* Low 5 bits of 32 - value */
8789       if (! INT_P (x))
8790         output_operand_lossage ("invalid %%s value");
8791       else
8792         fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INT_LOWPART (x)) & 31);
8793       return;
8794
8795     case 'S':
8796       /* PowerPC64 mask position.  All 0's is excluded.
8797          CONST_INT 32-bit mask is considered sign-extended so any
8798          transition must occur within the CONST_INT, not on the boundary.  */
8799       if (! mask64_operand (x, DImode))
8800         output_operand_lossage ("invalid %%S value");
8801
8802       uval = INT_LOWPART (x);
8803
8804       if (uval & 1)     /* Clear Left */
8805         {
8806 #if HOST_BITS_PER_WIDE_INT > 64
8807           uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
8808 #endif
8809           i = 64;
8810         }
8811       else              /* Clear Right */
8812         {
8813           uval = ~uval;
8814 #if HOST_BITS_PER_WIDE_INT > 64
8815           uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
8816 #endif
8817           i = 63;
8818         }
8819       while (uval != 0)
8820         --i, uval >>= 1;
8821       if (i < 0)
8822         abort ();
8823       fprintf (file, "%d", i);
8824       return;
8825
8826     case 't':
8827       /* Like 'J' but get to the OVERFLOW/UNORDERED bit.  */
8828       if (GET_CODE (x) != REG || GET_MODE (x) != CCmode)
8829         abort ();
8830
8831       /* Bit 3 is OV bit.  */
8832       i = 4 * (REGNO (x) - CR0_REGNO) + 3;
8833
8834       /* If we want bit 31, write a shift count of zero, not 32.  */
8835       fprintf (file, "%d", i == 31 ? 0 : i + 1);
8836       return;
8837
8838     case 'T':
8839       /* Print the symbolic name of a branch target register.  */
8840       if (GET_CODE (x) != REG || (REGNO (x) != LINK_REGISTER_REGNUM
8841                                   && REGNO (x) != COUNT_REGISTER_REGNUM))
8842         output_operand_lossage ("invalid %%T value");
8843       else if (REGNO (x) == LINK_REGISTER_REGNUM)
8844         fputs (TARGET_NEW_MNEMONICS ? "lr" : "r", file);
8845       else
8846         fputs ("ctr", file);
8847       return;
8848
8849     case 'u':
8850       /* High-order 16 bits of constant for use in unsigned operand.  */
8851       if (! INT_P (x))
8852         output_operand_lossage ("invalid %%u value");
8853       else
8854         fprintf (file, HOST_WIDE_INT_PRINT_HEX, 
8855                  (INT_LOWPART (x) >> 16) & 0xffff);
8856       return;
8857
8858     case 'v':
8859       /* High-order 16 bits of constant for use in signed operand.  */
8860       if (! INT_P (x))
8861         output_operand_lossage ("invalid %%v value");
8862       else
8863         fprintf (file, HOST_WIDE_INT_PRINT_HEX,
8864                  (INT_LOWPART (x) >> 16) & 0xffff);
8865       return;
8866
8867     case 'U':
8868       /* Print `u' if this has an auto-increment or auto-decrement.  */
8869       if (GET_CODE (x) == MEM
8870           && (GET_CODE (XEXP (x, 0)) == PRE_INC
8871               || GET_CODE (XEXP (x, 0)) == PRE_DEC))
8872         putc ('u', file);
8873       return;
8874
8875     case 'V':
8876       /* Print the trap code for this operand.  */
8877       switch (GET_CODE (x))
8878         {
8879         case EQ:
8880           fputs ("eq", file);   /* 4 */
8881           break;
8882         case NE:
8883           fputs ("ne", file);   /* 24 */
8884           break;
8885         case LT:
8886           fputs ("lt", file);   /* 16 */
8887           break;
8888         case LE:
8889           fputs ("le", file);   /* 20 */
8890           break;
8891         case GT:
8892           fputs ("gt", file);   /* 8 */
8893           break;
8894         case GE:
8895           fputs ("ge", file);   /* 12 */
8896           break;
8897         case LTU:
8898           fputs ("llt", file);  /* 2 */
8899           break;
8900         case LEU:
8901           fputs ("lle", file);  /* 6 */
8902           break;
8903         case GTU:
8904           fputs ("lgt", file);  /* 1 */
8905           break;
8906         case GEU:
8907           fputs ("lge", file);  /* 5 */
8908           break;
8909         default:
8910           abort ();
8911         }
8912       break;
8913
8914     case 'w':
8915       /* If constant, low-order 16 bits of constant, signed.  Otherwise, write
8916          normally.  */
8917       if (INT_P (x))
8918         fprintf (file, HOST_WIDE_INT_PRINT_DEC, 
8919                  ((INT_LOWPART (x) & 0xffff) ^ 0x8000) - 0x8000);
8920       else
8921         print_operand (file, x, 0);
8922       return;
8923
8924     case 'W':
8925       /* MB value for a PowerPC64 rldic operand.  */
8926       val = (GET_CODE (x) == CONST_INT
8927              ? INTVAL (x) : CONST_DOUBLE_HIGH (x));
8928
8929       if (val < 0)
8930         i = -1;
8931       else
8932         for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
8933           if ((val <<= 1) < 0)
8934             break;
8935
8936 #if HOST_BITS_PER_WIDE_INT == 32
8937       if (GET_CODE (x) == CONST_INT && i >= 0)
8938         i += 32;  /* zero-extend high-part was all 0's */
8939       else if (GET_CODE (x) == CONST_DOUBLE && i == 32)
8940         {
8941           val = CONST_DOUBLE_LOW (x);
8942
8943           if (val == 0)
8944             abort ();
8945           else if (val < 0)
8946             --i;
8947           else
8948             for ( ; i < 64; i++)
8949               if ((val <<= 1) < 0)
8950                 break;
8951         }
8952 #endif
8953
8954       fprintf (file, "%d", i + 1);
8955       return;
8956
8957     case 'X':
8958       if (GET_CODE (x) == MEM
8959           && legitimate_indexed_address_p (XEXP (x, 0), 0))
8960         putc ('x', file);
8961       return;
8962
8963     case 'Y':
8964       /* Like 'L', for third word of TImode  */
8965       if (GET_CODE (x) == REG)
8966         fprintf (file, "%s", reg_names[REGNO (x) + 2]);
8967       else if (GET_CODE (x) == MEM)
8968         {
8969           if (GET_CODE (XEXP (x, 0)) == PRE_INC
8970               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
8971             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 8));
8972           else
8973             output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
8974           if (small_data_operand (x, GET_MODE (x)))
8975             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
8976                      reg_names[SMALL_DATA_REG]);
8977         }
8978       return;
8979                             
8980     case 'z':
8981       /* X is a SYMBOL_REF.  Write out the name preceded by a
8982          period and without any trailing data in brackets.  Used for function
8983          names.  If we are configured for System V (or the embedded ABI) on
8984          the PowerPC, do not emit the period, since those systems do not use
8985          TOCs and the like.  */
8986       if (GET_CODE (x) != SYMBOL_REF)
8987         abort ();
8988
8989       if (XSTR (x, 0)[0] != '.')
8990         {
8991           switch (DEFAULT_ABI)
8992             {
8993             default:
8994               abort ();
8995
8996             case ABI_AIX:
8997               putc ('.', file);
8998               break;
8999
9000             case ABI_V4:
9001             case ABI_DARWIN:
9002               break;
9003             }
9004         }
9005       if (TARGET_AIX)
9006         RS6000_OUTPUT_BASENAME (file, XSTR (x, 0));
9007       else
9008         assemble_name (file, XSTR (x, 0));
9009       return;
9010
9011     case 'Z':
9012       /* Like 'L', for last word of TImode.  */
9013       if (GET_CODE (x) == REG)
9014         fprintf (file, "%s", reg_names[REGNO (x) + 3]);
9015       else if (GET_CODE (x) == MEM)
9016         {
9017           if (GET_CODE (XEXP (x, 0)) == PRE_INC
9018               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
9019             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 12));
9020           else
9021             output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
9022           if (small_data_operand (x, GET_MODE (x)))
9023             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
9024                      reg_names[SMALL_DATA_REG]);
9025         }
9026       return;
9027
9028       /* Print AltiVec or SPE memory operand.  */
9029     case 'y':
9030       {
9031         rtx tmp;
9032
9033         if (GET_CODE (x) != MEM)
9034           abort ();
9035
9036         tmp = XEXP (x, 0);
9037
9038         if (TARGET_E500)
9039           {
9040             /* Handle [reg].  */
9041             if (GET_CODE (tmp) == REG)
9042               {
9043                 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
9044                 break;
9045               }
9046             /* Handle [reg+UIMM].  */
9047             else if (GET_CODE (tmp) == PLUS &&
9048                      GET_CODE (XEXP (tmp, 1)) == CONST_INT)
9049               {
9050                 int x;
9051
9052                 if (GET_CODE (XEXP (tmp, 0)) != REG)
9053                   abort ();
9054
9055                 x = INTVAL (XEXP (tmp, 1));
9056                 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
9057                 break;
9058               }
9059
9060             /* Fall through.  Must be [reg+reg].  */
9061           }
9062         if (GET_CODE (tmp) == REG)
9063           fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
9064         else if (GET_CODE (tmp) == PLUS && GET_CODE (XEXP (tmp, 1)) == REG)
9065           {
9066             if (REGNO (XEXP (tmp, 0)) == 0)
9067               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
9068                        reg_names[ REGNO (XEXP (tmp, 0)) ]);
9069             else
9070               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
9071                        reg_names[ REGNO (XEXP (tmp, 1)) ]);
9072           }
9073         else
9074           abort ();
9075         break;
9076       }
9077                             
9078     case 0:
9079       if (GET_CODE (x) == REG)
9080         fprintf (file, "%s", reg_names[REGNO (x)]);
9081       else if (GET_CODE (x) == MEM)
9082         {
9083           /* We need to handle PRE_INC and PRE_DEC here, since we need to
9084              know the width from the mode.  */
9085           if (GET_CODE (XEXP (x, 0)) == PRE_INC)
9086             fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
9087                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
9088           else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
9089             fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
9090                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
9091           else
9092             output_address (XEXP (x, 0));
9093         }
9094       else
9095         output_addr_const (file, x);
9096       return;
9097
9098     case '&':
9099       assemble_name (file, rs6000_get_some_local_dynamic_name ());
9100       return;
9101
9102     default:
9103       output_operand_lossage ("invalid %%xn code");
9104     }
9105 }
9106 \f
9107 /* Print the address of an operand.  */
9108
9109 void
9110 print_operand_address (FILE *file, rtx x)
9111 {
9112   if (GET_CODE (x) == REG)
9113     fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
9114   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
9115            || GET_CODE (x) == LABEL_REF)
9116     {
9117       output_addr_const (file, x);
9118       if (small_data_operand (x, GET_MODE (x)))
9119         fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
9120                  reg_names[SMALL_DATA_REG]);
9121       else if (TARGET_TOC)
9122         abort ();
9123     }
9124   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG)
9125     {
9126       if (REGNO (XEXP (x, 0)) == 0)
9127         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
9128                  reg_names[ REGNO (XEXP (x, 0)) ]);
9129       else
9130         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
9131                  reg_names[ REGNO (XEXP (x, 1)) ]);
9132     }
9133   else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
9134     fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
9135              INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
9136 #if TARGET_ELF
9137   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
9138            && CONSTANT_P (XEXP (x, 1)))
9139     {
9140       output_addr_const (file, XEXP (x, 1));
9141       fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
9142     }
9143 #endif
9144 #if TARGET_MACHO
9145   else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
9146            && CONSTANT_P (XEXP (x, 1)))
9147     {
9148       fprintf (file, "lo16(");
9149       output_addr_const (file, XEXP (x, 1));
9150       fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
9151     }
9152 #endif
9153   else if (legitimate_constant_pool_address_p (x))
9154     {
9155       if (TARGET_AIX && (!TARGET_ELF || !TARGET_MINIMAL_TOC))
9156         {
9157           rtx contains_minus = XEXP (x, 1);
9158           rtx minus, symref;
9159           const char *name;
9160           
9161           /* Find the (minus (sym) (toc)) buried in X, and temporarily
9162              turn it into (sym) for output_addr_const.  */
9163           while (GET_CODE (XEXP (contains_minus, 0)) != MINUS)
9164             contains_minus = XEXP (contains_minus, 0);
9165
9166           minus = XEXP (contains_minus, 0);
9167           symref = XEXP (minus, 0);
9168           XEXP (contains_minus, 0) = symref;
9169           if (TARGET_ELF)
9170             {
9171               char *newname;
9172
9173               name = XSTR (symref, 0);
9174               newname = alloca (strlen (name) + sizeof ("@toc"));
9175               strcpy (newname, name);
9176               strcat (newname, "@toc");
9177               XSTR (symref, 0) = newname;
9178             }
9179           output_addr_const (file, XEXP (x, 1));
9180           if (TARGET_ELF)
9181             XSTR (symref, 0) = name;
9182           XEXP (contains_minus, 0) = minus;
9183         }
9184       else
9185         output_addr_const (file, XEXP (x, 1));
9186
9187       fprintf (file, "(%s)", reg_names[REGNO (XEXP (x, 0))]);
9188     }
9189   else
9190     abort ();
9191 }
9192 \f
9193 /* Target hook for assembling integer objects.  The PowerPC version has
9194    to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
9195    is defined.  It also needs to handle DI-mode objects on 64-bit
9196    targets.  */
9197
9198 static bool
9199 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
9200 {
9201 #ifdef RELOCATABLE_NEEDS_FIXUP
9202   /* Special handling for SI values.  */
9203   if (size == 4 && aligned_p)
9204     {
9205       extern int in_toc_section (void);
9206       static int recurse = 0;
9207       
9208       /* For -mrelocatable, we mark all addresses that need to be fixed up
9209          in the .fixup section.  */
9210       if (TARGET_RELOCATABLE
9211           && !in_toc_section ()
9212           && !in_text_section ()
9213           && !recurse
9214           && GET_CODE (x) != CONST_INT
9215           && GET_CODE (x) != CONST_DOUBLE
9216           && CONSTANT_P (x))
9217         {
9218           char buf[256];
9219
9220           recurse = 1;
9221           ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
9222           fixuplabelno++;
9223           ASM_OUTPUT_LABEL (asm_out_file, buf);
9224           fprintf (asm_out_file, "\t.long\t(");
9225           output_addr_const (asm_out_file, x);
9226           fprintf (asm_out_file, ")@fixup\n");
9227           fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
9228           ASM_OUTPUT_ALIGN (asm_out_file, 2);
9229           fprintf (asm_out_file, "\t.long\t");
9230           assemble_name (asm_out_file, buf);
9231           fprintf (asm_out_file, "\n\t.previous\n");
9232           recurse = 0;
9233           return true;
9234         }
9235       /* Remove initial .'s to turn a -mcall-aixdesc function
9236          address into the address of the descriptor, not the function
9237          itself.  */
9238       else if (GET_CODE (x) == SYMBOL_REF
9239                && XSTR (x, 0)[0] == '.'
9240                && DEFAULT_ABI == ABI_AIX)
9241         {
9242           const char *name = XSTR (x, 0);
9243           while (*name == '.')
9244             name++;
9245
9246           fprintf (asm_out_file, "\t.long\t%s\n", name);
9247           return true;
9248         }
9249     }
9250 #endif /* RELOCATABLE_NEEDS_FIXUP */
9251   return default_assemble_integer (x, size, aligned_p);
9252 }
9253
9254 #ifdef HAVE_GAS_HIDDEN
9255 /* Emit an assembler directive to set symbol visibility for DECL to
9256    VISIBILITY_TYPE.  */
9257
9258 static void
9259 rs6000_assemble_visibility (tree decl, int vis)
9260 {
9261   /* Functions need to have their entry point symbol visibility set as
9262      well as their descriptor symbol visibility.  */
9263   if (DEFAULT_ABI == ABI_AIX && TREE_CODE (decl) == FUNCTION_DECL)
9264     {
9265       static const char * const visibility_types[] = {
9266         NULL, "internal", "hidden", "protected"
9267       };
9268
9269       const char *name, *type;
9270
9271       name = ((* targetm.strip_name_encoding)
9272               (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
9273       type = visibility_types[vis];
9274
9275       fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
9276       fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
9277     }
9278   else
9279     default_assemble_visibility (decl, vis);
9280 }
9281 #endif
9282 \f
9283 enum rtx_code
9284 rs6000_reverse_condition (enum machine_mode mode, enum rtx_code code)
9285 {
9286   /* Reversal of FP compares takes care -- an ordered compare
9287      becomes an unordered compare and vice versa.  */
9288   if (mode == CCFPmode 
9289       && (!flag_finite_math_only
9290           || code == UNLT || code == UNLE || code == UNGT || code == UNGE
9291           || code == UNEQ || code == LTGT))
9292     return reverse_condition_maybe_unordered (code);
9293   else
9294     return reverse_condition (code);
9295 }
9296
9297 /* Generate a compare for CODE.  Return a brand-new rtx that
9298    represents the result of the compare.  */
9299
9300 static rtx
9301 rs6000_generate_compare (enum rtx_code code)
9302 {
9303   enum machine_mode comp_mode;
9304   rtx compare_result;
9305
9306   if (rs6000_compare_fp_p)
9307     comp_mode = CCFPmode;
9308   else if (code == GTU || code == LTU
9309           || code == GEU || code == LEU)
9310     comp_mode = CCUNSmode;
9311   else
9312     comp_mode = CCmode;
9313
9314   /* First, the compare.  */
9315   compare_result = gen_reg_rtx (comp_mode);
9316
9317   /* SPE FP compare instructions on the GPRs.  Yuck!  */
9318   if ((TARGET_E500 && !TARGET_FPRS && TARGET_HARD_FLOAT)
9319       && rs6000_compare_fp_p)
9320     {
9321       rtx cmp, or1, or2, or_result, compare_result2;
9322
9323       switch (code)
9324         {
9325         case EQ:
9326         case UNEQ:
9327         case NE:
9328         case LTGT:
9329           cmp = flag_finite_math_only
9330             ? gen_tstsfeq_gpr (compare_result, rs6000_compare_op0,
9331                                rs6000_compare_op1)
9332             : gen_cmpsfeq_gpr (compare_result, rs6000_compare_op0,
9333                                rs6000_compare_op1);
9334           break;
9335         case GT:
9336         case GTU:
9337         case UNGT:
9338         case UNGE:
9339         case GE:
9340         case GEU:
9341           cmp = flag_finite_math_only
9342             ? gen_tstsfgt_gpr (compare_result, rs6000_compare_op0,
9343                                rs6000_compare_op1)
9344             : gen_cmpsfgt_gpr (compare_result, rs6000_compare_op0,
9345                                rs6000_compare_op1);
9346           break;
9347         case LT:
9348         case LTU:
9349         case UNLT:
9350         case UNLE:
9351         case LE:
9352         case LEU:
9353           cmp = flag_finite_math_only
9354             ? gen_tstsflt_gpr (compare_result, rs6000_compare_op0,
9355                                rs6000_compare_op1)
9356             : gen_cmpsflt_gpr (compare_result, rs6000_compare_op0,
9357                                rs6000_compare_op1);
9358           break;
9359         default:
9360           abort ();
9361         }
9362
9363       /* Synthesize LE and GE from LT/GT || EQ.  */
9364       if (code == LE || code == GE || code == LEU || code == GEU)
9365         {
9366           /* Synthesize GE/LE frome GT/LT || EQ.  */
9367
9368           emit_insn (cmp);
9369
9370           switch (code)
9371             {
9372             case LE: code = LT; break;
9373             case GE: code = GT; break;
9374             case LEU: code = LT; break;
9375             case GEU: code = GT; break;
9376             default: abort ();
9377             }
9378
9379           or1 = gen_reg_rtx (SImode);
9380           or2 = gen_reg_rtx (SImode);
9381           or_result = gen_reg_rtx (CCEQmode);
9382           compare_result2 = gen_reg_rtx (CCFPmode);
9383
9384           /* Do the EQ.  */
9385           cmp = flag_finite_math_only
9386             ? gen_tstsfeq_gpr (compare_result2, rs6000_compare_op0,
9387                                rs6000_compare_op1)
9388             : gen_cmpsfeq_gpr (compare_result2, rs6000_compare_op0,
9389                                rs6000_compare_op1);
9390           emit_insn (cmp);
9391
9392           /* The MC8540 FP compare instructions set the CR bits
9393              differently than other PPC compare instructions.  For
9394              that matter, there is no generic test instruction, but a
9395              testgt, testlt, and testeq.  For a true condition, bit 2
9396              is set (x1xx) in the CR.  Following the traditional CR
9397              values:
9398
9399              LT    GT    EQ    OV
9400              bit3  bit2  bit1  bit0
9401
9402              ... bit 2 would be a GT CR alias, so later on we
9403              look in the GT bits for the branch instructions.
9404              However, we must be careful to emit correct RTL in
9405              the meantime, so optimizations don't get confused.  */
9406
9407           or1 = gen_rtx (NE, SImode, compare_result, const0_rtx);
9408           or2 = gen_rtx (NE, SImode, compare_result2, const0_rtx);
9409
9410           /* OR them together.  */
9411           cmp = gen_rtx_SET (VOIDmode, or_result,
9412                              gen_rtx_COMPARE (CCEQmode,
9413                                               gen_rtx_IOR (SImode, or1, or2),
9414                                               const_true_rtx));
9415           compare_result = or_result;
9416           code = EQ;
9417         }
9418       else
9419         {
9420           /* We only care about 1 bit (x1xx), so map everything to NE to
9421              maintain rtl sanity.  We'll get to the right bit (x1xx) at
9422              code output time.  */
9423           if (code == NE || code == LTGT)
9424             /* Do the inverse here because we have no cmpne
9425                instruction.  We use the cmpeq instruction and expect
9426                to get a 0 instead.  */
9427             code = EQ;
9428           else
9429             code = NE;
9430         }
9431
9432       emit_insn (cmp);
9433     }
9434   else
9435     emit_insn (gen_rtx_SET (VOIDmode, compare_result,
9436                             gen_rtx_COMPARE (comp_mode,
9437                                              rs6000_compare_op0, 
9438                                              rs6000_compare_op1)));
9439   
9440   /* Some kinds of FP comparisons need an OR operation;
9441      under flag_finite_math_only we don't bother.  */
9442   if (rs6000_compare_fp_p
9443       && ! flag_finite_math_only
9444       && ! (TARGET_HARD_FLOAT && TARGET_E500 && !TARGET_FPRS)
9445       && (code == LE || code == GE
9446           || code == UNEQ || code == LTGT
9447           || code == UNGT || code == UNLT))
9448     {
9449       enum rtx_code or1, or2;
9450       rtx or1_rtx, or2_rtx, compare2_rtx;
9451       rtx or_result = gen_reg_rtx (CCEQmode);
9452       
9453       switch (code)
9454         {
9455         case LE: or1 = LT;  or2 = EQ;  break;
9456         case GE: or1 = GT;  or2 = EQ;  break;
9457         case UNEQ: or1 = UNORDERED;  or2 = EQ;  break;
9458         case LTGT: or1 = LT;  or2 = GT;  break;
9459         case UNGT: or1 = UNORDERED;  or2 = GT;  break;
9460         case UNLT: or1 = UNORDERED;  or2 = LT;  break;
9461         default:  abort ();
9462         }
9463       validate_condition_mode (or1, comp_mode);
9464       validate_condition_mode (or2, comp_mode);
9465       or1_rtx = gen_rtx (or1, SImode, compare_result, const0_rtx);
9466       or2_rtx = gen_rtx (or2, SImode, compare_result, const0_rtx);
9467       compare2_rtx = gen_rtx_COMPARE (CCEQmode,
9468                                       gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
9469                                       const_true_rtx);
9470       emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
9471
9472       compare_result = or_result;
9473       code = EQ;
9474     }
9475
9476   validate_condition_mode (code, GET_MODE (compare_result));
9477   
9478   return gen_rtx (code, VOIDmode, compare_result, const0_rtx);
9479 }
9480
9481
9482 /* Emit the RTL for an sCOND pattern.  */
9483
9484 void
9485 rs6000_emit_sCOND (enum rtx_code code, rtx result)
9486 {
9487   rtx condition_rtx;
9488   enum machine_mode op_mode;
9489   enum rtx_code cond_code;
9490
9491   condition_rtx = rs6000_generate_compare (code);
9492   cond_code = GET_CODE (condition_rtx);
9493
9494   if (cond_code == NE
9495       || cond_code == GE || cond_code == LE
9496       || cond_code == GEU || cond_code == LEU
9497       || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
9498     {
9499       rtx not_result = gen_reg_rtx (CCEQmode);
9500       rtx not_op, rev_cond_rtx;
9501       enum machine_mode cc_mode;
9502       
9503       cc_mode = GET_MODE (XEXP (condition_rtx, 0));
9504
9505       rev_cond_rtx = gen_rtx (rs6000_reverse_condition (cc_mode, cond_code),
9506                               SImode, XEXP (condition_rtx, 0), const0_rtx);
9507       not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
9508       emit_insn (gen_rtx_SET (VOIDmode, not_result, not_op));
9509       condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
9510     }
9511
9512   op_mode = GET_MODE (rs6000_compare_op0);
9513   if (op_mode == VOIDmode)
9514     op_mode = GET_MODE (rs6000_compare_op1);
9515
9516   if (TARGET_POWERPC64 && (op_mode == DImode || rs6000_compare_fp_p))
9517     {
9518       PUT_MODE (condition_rtx, DImode);
9519       convert_move (result, condition_rtx, 0);
9520     }
9521   else
9522     {
9523       PUT_MODE (condition_rtx, SImode);
9524       emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
9525     }
9526 }
9527
9528 /* Emit a branch of kind CODE to location LOC.  */
9529
9530 void
9531 rs6000_emit_cbranch (enum rtx_code code, rtx loc)
9532 {
9533   rtx condition_rtx, loc_ref;
9534
9535   condition_rtx = rs6000_generate_compare (code);
9536   loc_ref = gen_rtx_LABEL_REF (VOIDmode, loc);
9537   emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
9538                                gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
9539                                                      loc_ref, pc_rtx)));
9540 }
9541
9542 /* Return the string to output a conditional branch to LABEL, which is
9543    the operand number of the label, or -1 if the branch is really a
9544    conditional return.  
9545
9546    OP is the conditional expression.  XEXP (OP, 0) is assumed to be a
9547    condition code register and its mode specifies what kind of
9548    comparison we made.
9549
9550    REVERSED is nonzero if we should reverse the sense of the comparison.
9551
9552    INSN is the insn.  */
9553
9554 char *
9555 output_cbranch (rtx op, const char *label, int reversed, rtx insn)
9556 {
9557   static char string[64];
9558   enum rtx_code code = GET_CODE (op);
9559   rtx cc_reg = XEXP (op, 0);
9560   enum machine_mode mode = GET_MODE (cc_reg);
9561   int cc_regno = REGNO (cc_reg) - CR0_REGNO;
9562   int need_longbranch = label != NULL && get_attr_length (insn) == 8;
9563   int really_reversed = reversed ^ need_longbranch;
9564   char *s = string;
9565   const char *ccode;
9566   const char *pred;
9567   rtx note;
9568
9569   validate_condition_mode (code, mode);
9570
9571   /* Work out which way this really branches.  We could use
9572      reverse_condition_maybe_unordered here always but this
9573      makes the resulting assembler clearer.  */
9574   if (really_reversed)
9575     {
9576       /* Reversal of FP compares takes care -- an ordered compare
9577          becomes an unordered compare and vice versa.  */
9578       if (mode == CCFPmode)
9579         code = reverse_condition_maybe_unordered (code);
9580       else
9581         code = reverse_condition (code);
9582     }
9583
9584   if ((TARGET_E500 && !TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
9585     {
9586       /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
9587          to the GT bit.  */
9588       if (code == EQ)
9589         /* Opposite of GT.  */
9590         code = UNLE;
9591       else if (code == NE)
9592         code = GT;
9593       else
9594         abort ();
9595     }
9596
9597   switch (code)
9598     {
9599       /* Not all of these are actually distinct opcodes, but
9600          we distinguish them for clarity of the resulting assembler.  */
9601     case NE: case LTGT:
9602       ccode = "ne"; break;
9603     case EQ: case UNEQ:
9604       ccode = "eq"; break;
9605     case GE: case GEU: 
9606       ccode = "ge"; break;
9607     case GT: case GTU: case UNGT: 
9608       ccode = "gt"; break;
9609     case LE: case LEU: 
9610       ccode = "le"; break;
9611     case LT: case LTU: case UNLT: 
9612       ccode = "lt"; break;
9613     case UNORDERED: ccode = "un"; break;
9614     case ORDERED: ccode = "nu"; break;
9615     case UNGE: ccode = "nl"; break;
9616     case UNLE: ccode = "ng"; break;
9617     default:
9618       abort ();
9619     }
9620   
9621   /* Maybe we have a guess as to how likely the branch is.  
9622      The old mnemonics don't have a way to specify this information.  */
9623   pred = "";
9624   note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
9625   if (note != NULL_RTX)
9626     {
9627       /* PROB is the difference from 50%.  */
9628       int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
9629       bool always_hint = rs6000_cpu != PROCESSOR_POWER4;
9630
9631       /* Only hint for highly probable/improbable branches on newer
9632          cpus as static prediction overrides processor dynamic
9633          prediction.  For older cpus we may as well always hint, but
9634          assume not taken for branches that are very close to 50% as a
9635          mispredicted taken branch is more expensive than a
9636          mispredicted not-taken branch.  */ 
9637       if (always_hint
9638           || abs (prob) > REG_BR_PROB_BASE / 100 * 48)
9639         {
9640           if (abs (prob) > REG_BR_PROB_BASE / 20
9641               && ((prob > 0) ^ need_longbranch))
9642               pred = "+";
9643           else
9644             pred = "-";
9645         }
9646     }
9647
9648   if (label == NULL)
9649     s += sprintf (s, "{b%sr|b%slr%s} ", ccode, ccode, pred);
9650   else
9651     s += sprintf (s, "{b%s|b%s%s} ", ccode, ccode, pred);
9652
9653   /* We need to escape any '%' characters in the reg_names string.
9654      Assume they'd only be the first character...  */
9655   if (reg_names[cc_regno + CR0_REGNO][0] == '%')
9656     *s++ = '%';
9657   s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
9658
9659   if (label != NULL)
9660     {
9661       /* If the branch distance was too far, we may have to use an
9662          unconditional branch to go the distance.  */
9663       if (need_longbranch)
9664         s += sprintf (s, ",$+8\n\tb %s", label);
9665       else
9666         s += sprintf (s, ",%s", label);
9667     }
9668
9669   return string;
9670 }
9671
9672 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
9673    operands of the last comparison is nonzero/true, FALSE_COND if it
9674    is zero/false.  Return 0 if the hardware has no such operation.  */
9675
9676 int
9677 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
9678 {
9679   enum rtx_code code = GET_CODE (op);
9680   rtx op0 = rs6000_compare_op0;
9681   rtx op1 = rs6000_compare_op1;
9682   REAL_VALUE_TYPE c1;
9683   enum machine_mode compare_mode = GET_MODE (op0);
9684   enum machine_mode result_mode = GET_MODE (dest);
9685   rtx temp;
9686
9687   /* These modes should always match. */
9688   if (GET_MODE (op1) != compare_mode
9689       /* In the isel case however, we can use a compare immediate, so
9690          op1 may be a small constant.  */
9691       && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
9692     return 0;
9693   if (GET_MODE (true_cond) != result_mode)
9694     return 0;
9695   if (GET_MODE (false_cond) != result_mode)
9696     return 0;
9697
9698   /* First, work out if the hardware can do this at all, or
9699      if it's too slow...  */
9700   if (! rs6000_compare_fp_p)
9701     {
9702       if (TARGET_ISEL)
9703         return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
9704       return 0;
9705     }
9706
9707   /* Eliminate half of the comparisons by switching operands, this
9708      makes the remaining code simpler.  */
9709   if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
9710       || code == LTGT || code == LT || code == UNLE)
9711     {
9712       code = reverse_condition_maybe_unordered (code);
9713       temp = true_cond;
9714       true_cond = false_cond;
9715       false_cond = temp;
9716     }
9717
9718   /* UNEQ and LTGT take four instructions for a comparison with zero,
9719      it'll probably be faster to use a branch here too.  */
9720   if (code == UNEQ && HONOR_NANS (compare_mode))
9721     return 0;
9722   
9723   if (GET_CODE (op1) == CONST_DOUBLE)
9724     REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
9725     
9726   /* We're going to try to implement comparisons by performing
9727      a subtract, then comparing against zero.  Unfortunately,
9728      Inf - Inf is NaN which is not zero, and so if we don't
9729      know that the operand is finite and the comparison
9730      would treat EQ different to UNORDERED, we can't do it.  */
9731   if (HONOR_INFINITIES (compare_mode)
9732       && code != GT && code != UNGE
9733       && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
9734       /* Constructs of the form (a OP b ? a : b) are safe.  */
9735       && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
9736           || (! rtx_equal_p (op0, true_cond) 
9737               && ! rtx_equal_p (op1, true_cond))))
9738     return 0;
9739   /* At this point we know we can use fsel.  */
9740
9741   /* Reduce the comparison to a comparison against zero.  */
9742   temp = gen_reg_rtx (compare_mode);
9743   emit_insn (gen_rtx_SET (VOIDmode, temp,
9744                           gen_rtx_MINUS (compare_mode, op0, op1)));
9745   op0 = temp;
9746   op1 = CONST0_RTX (compare_mode);
9747
9748   /* If we don't care about NaNs we can reduce some of the comparisons
9749      down to faster ones.  */
9750   if (! HONOR_NANS (compare_mode))
9751     switch (code)
9752       {
9753       case GT:
9754         code = LE;
9755         temp = true_cond;
9756         true_cond = false_cond;
9757         false_cond = temp;
9758         break;
9759       case UNGE:
9760         code = GE;
9761         break;
9762       case UNEQ:
9763         code = EQ;
9764         break;
9765       default:
9766         break;
9767       }
9768
9769   /* Now, reduce everything down to a GE.  */
9770   switch (code)
9771     {
9772     case GE:
9773       break;
9774
9775     case LE:
9776       temp = gen_reg_rtx (compare_mode);
9777       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
9778       op0 = temp;
9779       break;
9780
9781     case ORDERED:
9782       temp = gen_reg_rtx (compare_mode);
9783       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
9784       op0 = temp;
9785       break;
9786
9787     case EQ:
9788       temp = gen_reg_rtx (compare_mode);
9789       emit_insn (gen_rtx_SET (VOIDmode, temp, 
9790                               gen_rtx_NEG (compare_mode,
9791                                            gen_rtx_ABS (compare_mode, op0))));
9792       op0 = temp;
9793       break;
9794
9795     case UNGE:
9796       /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
9797       temp = gen_reg_rtx (result_mode);
9798       emit_insn (gen_rtx_SET (VOIDmode, temp,
9799                               gen_rtx_IF_THEN_ELSE (result_mode,
9800                                                     gen_rtx_GE (VOIDmode,
9801                                                                 op0, op1),
9802                                                     true_cond, false_cond)));
9803       false_cond = true_cond;
9804       true_cond = temp;
9805
9806       temp = gen_reg_rtx (compare_mode);
9807       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
9808       op0 = temp;
9809       break;
9810
9811     case GT:
9812       /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
9813       temp = gen_reg_rtx (result_mode);
9814       emit_insn (gen_rtx_SET (VOIDmode, temp,
9815                               gen_rtx_IF_THEN_ELSE (result_mode, 
9816                                                     gen_rtx_GE (VOIDmode,
9817                                                                 op0, op1),
9818                                                     true_cond, false_cond)));
9819       true_cond = false_cond;
9820       false_cond = temp;
9821
9822       temp = gen_reg_rtx (compare_mode);
9823       emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
9824       op0 = temp;
9825       break;
9826
9827     default:
9828       abort ();
9829     }
9830
9831   emit_insn (gen_rtx_SET (VOIDmode, dest,
9832                           gen_rtx_IF_THEN_ELSE (result_mode,
9833                                                 gen_rtx_GE (VOIDmode,
9834                                                             op0, op1),
9835                                                 true_cond, false_cond)));
9836   return 1;
9837 }
9838
9839 /* Same as above, but for ints (isel).  */
9840
9841 static int
9842 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
9843 {
9844   rtx condition_rtx, cr;
9845
9846   /* All isel implementations thus far are 32-bits.  */
9847   if (GET_MODE (rs6000_compare_op0) != SImode)
9848     return 0;
9849
9850   /* We still have to do the compare, because isel doesn't do a
9851      compare, it just looks at the CRx bits set by a previous compare
9852      instruction.  */
9853   condition_rtx = rs6000_generate_compare (GET_CODE (op));
9854   cr = XEXP (condition_rtx, 0);
9855
9856   if (GET_MODE (cr) == CCmode)
9857     emit_insn (gen_isel_signed (dest, condition_rtx,
9858                                 true_cond, false_cond, cr));
9859   else
9860     emit_insn (gen_isel_unsigned (dest, condition_rtx,
9861                                   true_cond, false_cond, cr));
9862
9863   return 1;
9864 }
9865
9866 const char *
9867 output_isel (rtx *operands)
9868 {
9869   enum rtx_code code;
9870
9871   code = GET_CODE (operands[1]);
9872   if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
9873     {
9874       PUT_CODE (operands[1], reverse_condition (code));
9875       return "isel %0,%3,%2,%j1";
9876     }
9877   else
9878     return "isel %0,%2,%3,%j1";
9879 }
9880
9881 void
9882 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
9883 {
9884   enum machine_mode mode = GET_MODE (op0);
9885   enum rtx_code c;
9886   rtx target;
9887
9888   if (code == SMAX || code == SMIN)
9889     c = GE;
9890   else
9891     c = GEU;
9892
9893   if (code == SMAX || code == UMAX)
9894     target = emit_conditional_move (dest, c, op0, op1, mode, 
9895                                     op0, op1, mode, 0);
9896   else
9897     target = emit_conditional_move (dest, c, op0, op1, mode, 
9898                                     op1, op0, mode, 0);
9899   if (target == NULL_RTX)
9900     abort ();
9901   if (target != dest)
9902     emit_move_insn (dest, target);
9903 }
9904
9905 /* Called by splitter for multireg moves.
9906    Input: 
9907           operands[0] : Destination of move
9908           operands[1] : Source of move
9909
9910    Output:
9911           operands[2-n] : Destination slots
9912           operands[n-m] : Source slots
9913    where n = 2 + HARD_REGNO_NREGS (reg, GET_MODE (operands[0]))
9914          m = 2 + 2 * HARD_REGNO_NREGS (reg, GET_MODE (operands[0])) - 1
9915
9916    Splits the move of operands[1] to operands[0].
9917    This is done, if GPRs are one of the operands.  In this case
9918    a sequence of simple move insns has to be issued.  The sequence of these
9919    move insns has to be done in correct order to avoid early clobber of the
9920    base register or destructive overlap of registers. 
9921 */
9922           
9923 void
9924 rs6000_split_multireg_move (rtx *operands)
9925 {
9926   int nregs, reg, i, j, used_update = 0;
9927   enum machine_mode mode; 
9928   rtx dst = operands[0];
9929   rtx src = operands[1];
9930   rtx insn = 0;
9931
9932   /* Calculate number to move (2/4 for 32/64 bit mode).  */ 
9933
9934   reg = REG_P (operands[0]) ? REGNO (operands[0]) : REGNO (operands[1]); 
9935   mode = GET_MODE (operands[0]);
9936   nregs = HARD_REGNO_NREGS (reg, mode);                                  
9937   
9938   if (REG_P (operands[1]) 
9939       && REG_P (operands[0]) 
9940       && (REGNO (operands[1]) < REGNO (operands[0])))
9941     {  
9942       /* Move register range backwards, if we have destructive overlap.  */
9943
9944       j = nregs;
9945       for (i = 0; i < nregs; i++)
9946         {
9947           j--;
9948           operands[i+2] = operand_subword (operands[0], j, 0, mode);
9949           operands[i+2+nregs] = 
9950             operand_subword (operands[1], j, 0, mode);   
9951         }
9952     }     
9953   else
9954     {
9955       j = -1;
9956
9957       if (GET_CODE (operands[1]) == MEM)
9958         {
9959           rtx breg;
9960
9961           if (GET_CODE (XEXP (operands[1], 0)) == PRE_INC
9962               || GET_CODE (XEXP (operands[1], 0)) == PRE_DEC)
9963             {
9964               rtx delta_rtx;
9965               breg = XEXP (XEXP (operands[1], 0), 0);
9966               delta_rtx =  GET_CODE (XEXP (operands[1], 0)) == PRE_INC 
9967                   ? GEN_INT (GET_MODE_SIZE (GET_MODE (operands[1]))) 
9968                   : GEN_INT (-GET_MODE_SIZE (GET_MODE (operands[1]))); 
9969               insn = emit_insn (TARGET_32BIT
9970                                 ? gen_addsi3 (breg, breg, delta_rtx)
9971                                 : gen_adddi3 (breg, breg, delta_rtx));
9972               src = gen_rtx_MEM (mode, breg);
9973             }
9974
9975           /* We have now address involving an base register only.
9976              If we use one of the registers to address memory, 
9977              we have change that register last.  */
9978
9979           breg = (GET_CODE (XEXP (src, 0)) == PLUS
9980                   ? XEXP (XEXP (src, 0), 0)
9981                   : XEXP (src, 0));
9982
9983           if (!REG_P (breg))
9984               abort();
9985
9986           if (REGNO (breg) >= REGNO (dst) 
9987               && REGNO (breg) < REGNO (dst) + nregs)
9988             j = REGNO (breg) - REGNO (dst);
9989         }
9990
9991       if (GET_CODE (operands[0]) == MEM)
9992         {
9993           rtx breg;
9994
9995           if (GET_CODE (XEXP (operands[0], 0)) == PRE_INC
9996               || GET_CODE (XEXP (operands[0], 0)) == PRE_DEC)
9997             {
9998               rtx delta_rtx;
9999               breg = XEXP (XEXP (operands[0], 0), 0);
10000               delta_rtx = GET_CODE (XEXP (operands[0], 0)) == PRE_INC 
10001                 ? GEN_INT (GET_MODE_SIZE (GET_MODE (operands[0]))) 
10002                 : GEN_INT (-GET_MODE_SIZE (GET_MODE (operands[0]))); 
10003
10004               /* We have to update the breg before doing the store.
10005                  Use store with update, if available.  */
10006
10007               if (TARGET_UPDATE)
10008                 {
10009                   insn = emit_insn (TARGET_32BIT
10010                                     ? gen_movsi_update (breg, breg, delta_rtx, 
10011                                         operand_subword (src, 0, 0, mode))
10012                                     : gen_movdi_update (breg, breg, delta_rtx,
10013                                         operand_subword (src, 0, 0, mode)));
10014                   used_update = 1;
10015                 }
10016               else
10017                   insn = emit_insn (TARGET_32BIT
10018                                     ? gen_addsi3 (breg, breg, delta_rtx)
10019                                     : gen_adddi3 (breg, breg, delta_rtx));
10020               dst = gen_rtx_MEM (mode, breg);
10021             }
10022         }
10023
10024       for (i = 0; i < nregs; i++)
10025         {  
10026           /* Calculate index to next subword.  */
10027           ++j;
10028           if (j == nregs) 
10029             j = 0;
10030
10031           operands[i+2] = operand_subword (dst, j, 0, mode);
10032           operands[i+2+nregs] = operand_subword (src, j, 0, mode);
10033
10034           if (j == 0 && used_update)
10035             {
10036               /* Already emited move of first word by 
10037                  store with update -> emit dead insn instead (r := r).  */
10038               operands[i+2] = operands[i+2+nregs];
10039             }
10040         }
10041     }
10042 }
10043
10044 \f
10045 /* This page contains routines that are used to determine what the
10046    function prologue and epilogue code will do and write them out.  */
10047
10048 /* Return the first fixed-point register that is required to be
10049    saved. 32 if none.  */
10050
10051 int
10052 first_reg_to_save (void)
10053 {
10054   int first_reg;
10055
10056   /* Find lowest numbered live register.  */
10057   for (first_reg = 13; first_reg <= 31; first_reg++)
10058     if (regs_ever_live[first_reg] 
10059         && (! call_used_regs[first_reg]
10060             || (first_reg == RS6000_PIC_OFFSET_TABLE_REGNUM
10061                 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
10062                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic)))))
10063       break;
10064
10065 #if TARGET_MACHO
10066   if (flag_pic
10067       && current_function_uses_pic_offset_table
10068       && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
10069     return RS6000_PIC_OFFSET_TABLE_REGNUM;
10070 #endif
10071
10072   return first_reg;
10073 }
10074
10075 /* Similar, for FP regs.  */
10076
10077 int
10078 first_fp_reg_to_save (void)
10079 {
10080   int first_reg;
10081
10082   /* Find lowest numbered live register.  */
10083   for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
10084     if (regs_ever_live[first_reg])
10085       break;
10086
10087   return first_reg;
10088 }
10089
10090 /* Similar, for AltiVec regs.  */
10091
10092 static int
10093 first_altivec_reg_to_save (void)
10094 {
10095   int i;
10096
10097   /* Stack frame remains as is unless we are in AltiVec ABI.  */
10098   if (! TARGET_ALTIVEC_ABI)
10099     return LAST_ALTIVEC_REGNO + 1;
10100
10101   /* Find lowest numbered live register.  */
10102   for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
10103     if (regs_ever_live[i])
10104       break;
10105
10106   return i;
10107 }
10108
10109 /* Return a 32-bit mask of the AltiVec registers we need to set in
10110    VRSAVE.  Bit n of the return value is 1 if Vn is live.  The MSB in
10111    the 32-bit word is 0.  */
10112
10113 static unsigned int
10114 compute_vrsave_mask (void)
10115 {
10116   unsigned int i, mask = 0;
10117
10118   /* First, find out if we use _any_ altivec registers.  */
10119   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
10120     if (regs_ever_live[i])
10121       mask |= ALTIVEC_REG_BIT (i);
10122
10123   if (mask == 0)
10124     return mask;
10125
10126   /* Next, remove the argument registers from the set.  These must
10127      be in the VRSAVE mask set by the caller, so we don't need to add
10128      them in again.  More importantly, the mask we compute here is
10129      used to generate CLOBBERs in the set_vrsave insn, and we do not
10130      wish the argument registers to die.  */
10131   for (i = cfun->args_info.vregno - 1; i >= ALTIVEC_ARG_MIN_REG; --i)
10132     mask &= ~ALTIVEC_REG_BIT (i);
10133
10134   /* Similarly, remove the return value from the set.  */
10135   {
10136     bool yes = false;
10137     diddle_return_value (is_altivec_return_reg, &yes);
10138     if (yes)
10139       mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
10140   }
10141
10142   return mask;
10143 }
10144
10145 static void
10146 is_altivec_return_reg (rtx reg, void *xyes)
10147 {
10148   bool *yes = (bool *) xyes;
10149   if (REGNO (reg) == ALTIVEC_ARG_RETURN)
10150     *yes = true;
10151 }
10152
10153 \f
10154 /* Calculate the stack information for the current function.  This is
10155    complicated by having two separate calling sequences, the AIX calling
10156    sequence and the V.4 calling sequence.
10157
10158    AIX (and Darwin/Mac OS X) stack frames look like:
10159                                                           32-bit  64-bit
10160         SP----> +---------------------------------------+
10161                 | back chain to caller                  | 0       0
10162                 +---------------------------------------+
10163                 | saved CR                              | 4       8 (8-11)
10164                 +---------------------------------------+
10165                 | saved LR                              | 8       16
10166                 +---------------------------------------+
10167                 | reserved for compilers                | 12      24
10168                 +---------------------------------------+
10169                 | reserved for binders                  | 16      32
10170                 +---------------------------------------+
10171                 | saved TOC pointer                     | 20      40
10172                 +---------------------------------------+
10173                 | Parameter save area (P)               | 24      48
10174                 +---------------------------------------+
10175                 | Alloca space (A)                      | 24+P    etc.
10176                 +---------------------------------------+
10177                 | Local variable space (L)              | 24+P+A
10178                 +---------------------------------------+
10179                 | Float/int conversion temporary (X)    | 24+P+A+L
10180                 +---------------------------------------+
10181                 | Save area for AltiVec registers (W)   | 24+P+A+L+X
10182                 +---------------------------------------+
10183                 | AltiVec alignment padding (Y)         | 24+P+A+L+X+W
10184                 +---------------------------------------+
10185                 | Save area for VRSAVE register (Z)     | 24+P+A+L+X+W+Y
10186                 +---------------------------------------+
10187                 | Save area for GP registers (G)        | 24+P+A+X+L+X+W+Y+Z
10188                 +---------------------------------------+
10189                 | Save area for FP registers (F)        | 24+P+A+X+L+X+W+Y+Z+G
10190                 +---------------------------------------+
10191         old SP->| back chain to caller's caller         |
10192                 +---------------------------------------+
10193
10194    The required alignment for AIX configurations is two words (i.e., 8
10195    or 16 bytes).
10196
10197
10198    V.4 stack frames look like:
10199
10200         SP----> +---------------------------------------+
10201                 | back chain to caller                  | 0
10202                 +---------------------------------------+
10203                 | caller's saved LR                     | 4
10204                 +---------------------------------------+
10205                 | Parameter save area (P)               | 8
10206                 +---------------------------------------+
10207                 | Alloca space (A)                      | 8+P
10208                 +---------------------------------------+    
10209                 | Varargs save area (V)                 | 8+P+A
10210                 +---------------------------------------+    
10211                 | Local variable space (L)              | 8+P+A+V
10212                 +---------------------------------------+    
10213                 | Float/int conversion temporary (X)    | 8+P+A+V+L
10214                 +---------------------------------------+
10215                 | Save area for AltiVec registers (W)   | 8+P+A+V+L+X
10216                 +---------------------------------------+
10217                 | AltiVec alignment padding (Y)         | 8+P+A+V+L+X+W
10218                 +---------------------------------------+
10219                 | Save area for VRSAVE register (Z)     | 8+P+A+V+L+X+W+Y
10220                 +---------------------------------------+
10221                 | SPE: area for 64-bit GP registers     |
10222                 +---------------------------------------+
10223                 | SPE alignment padding                 |
10224                 +---------------------------------------+
10225                 | saved CR (C)                          | 8+P+A+V+L+X+W+Y+Z
10226                 +---------------------------------------+    
10227                 | Save area for GP registers (G)        | 8+P+A+V+L+X+W+Y+Z+C
10228                 +---------------------------------------+    
10229                 | Save area for FP registers (F)        | 8+P+A+V+L+X+W+Y+Z+C+G
10230                 +---------------------------------------+
10231         old SP->| back chain to caller's caller         |
10232                 +---------------------------------------+
10233
10234    The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
10235    given.  (But note below and in sysv4.h that we require only 8 and
10236    may round up the size of our stack frame anyways.  The historical
10237    reason is early versions of powerpc-linux which didn't properly
10238    align the stack at program startup.  A happy side-effect is that
10239    -mno-eabi libraries can be used with -meabi programs.)
10240
10241    The EABI configuration defaults to the V.4 layout.  However,
10242    the stack alignment requirements may differ.  If -mno-eabi is not
10243    given, the required stack alignment is 8 bytes; if -mno-eabi is
10244    given, the required alignment is 16 bytes.  (But see V.4 comment
10245    above.)  */
10246
10247 #ifndef ABI_STACK_BOUNDARY
10248 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
10249 #endif
10250
10251 rs6000_stack_t *
10252 rs6000_stack_info (void)
10253 {
10254   static rs6000_stack_t info, zero_info;
10255   rs6000_stack_t *info_ptr = &info;
10256   int reg_size = TARGET_POWERPC64 ? 8 : 4;
10257   int ehrd_size;
10258   int total_raw_size;
10259
10260   /* Zero all fields portably.  */
10261   info = zero_info;
10262
10263   if (TARGET_SPE)
10264     {
10265       /* Cache value so we don't rescan instruction chain over and over.  */
10266       if (cfun->machine->insn_chain_scanned_p == 0)
10267         {
10268           cfun->machine->insn_chain_scanned_p = 1;
10269           info_ptr->spe_64bit_regs_used = (int) spe_func_has_64bit_regs_p ();
10270         }
10271     }
10272
10273   /* Select which calling sequence.  */
10274   info_ptr->abi = DEFAULT_ABI;
10275
10276   /* Calculate which registers need to be saved & save area size.  */
10277   info_ptr->first_gp_reg_save = first_reg_to_save ();
10278   /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM, 
10279      even if it currently looks like we won't.  */
10280   if (((TARGET_TOC && TARGET_MINIMAL_TOC)
10281        || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
10282        || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
10283       && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
10284     info_ptr->gp_size = reg_size * (32 - RS6000_PIC_OFFSET_TABLE_REGNUM);
10285   else
10286     info_ptr->gp_size = reg_size * (32 - info_ptr->first_gp_reg_save);
10287
10288   /* For the SPE, we have an additional upper 32-bits on each GPR.
10289      Ideally we should save the entire 64-bits only when the upper
10290      half is used in SIMD instructions.  Since we only record
10291      registers live (not the size they are used in), this proves
10292      difficult because we'd have to traverse the instruction chain at
10293      the right time, taking reload into account.  This is a real pain,
10294      so we opt to save the GPRs in 64-bits always if but one register
10295      gets used in 64-bits.  Otherwise, all the registers in the frame
10296      get saved in 32-bits.
10297
10298      So... since when we save all GPRs (except the SP) in 64-bits, the
10299      traditional GP save area will be empty.  */
10300   if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
10301     info_ptr->gp_size = 0;
10302
10303   info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
10304   info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
10305
10306   info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
10307   info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
10308                                  - info_ptr->first_altivec_reg_save);
10309
10310   /* Does this function call anything?  */
10311   info_ptr->calls_p = (! current_function_is_leaf
10312                        || cfun->machine->ra_needs_full_frame);
10313
10314   /* Determine if we need to save the link register.  */
10315   if (rs6000_ra_ever_killed ()
10316       || (DEFAULT_ABI == ABI_AIX
10317           && current_function_profile
10318           && !TARGET_PROFILE_KERNEL)
10319 #ifdef TARGET_RELOCATABLE
10320       || (TARGET_RELOCATABLE && (get_pool_size () != 0))
10321 #endif
10322       || (info_ptr->first_fp_reg_save != 64
10323           && !FP_SAVE_INLINE (info_ptr->first_fp_reg_save))
10324       || info_ptr->first_altivec_reg_save <= LAST_ALTIVEC_REGNO
10325       || (DEFAULT_ABI == ABI_V4 && current_function_calls_alloca)
10326       || (DEFAULT_ABI == ABI_DARWIN
10327           && flag_pic
10328           && current_function_uses_pic_offset_table)
10329       || info_ptr->calls_p)
10330     {
10331       info_ptr->lr_save_p = 1;
10332       regs_ever_live[LINK_REGISTER_REGNUM] = 1;
10333     }
10334
10335   /* Determine if we need to save the condition code registers.  */
10336   if (regs_ever_live[CR2_REGNO] 
10337       || regs_ever_live[CR3_REGNO]
10338       || regs_ever_live[CR4_REGNO])
10339     {
10340       info_ptr->cr_save_p = 1;
10341       if (DEFAULT_ABI == ABI_V4)
10342         info_ptr->cr_size = reg_size;
10343     }
10344
10345   /* If the current function calls __builtin_eh_return, then we need
10346      to allocate stack space for registers that will hold data for
10347      the exception handler.  */
10348   if (current_function_calls_eh_return)
10349     {
10350       unsigned int i;
10351       for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
10352         continue;
10353
10354       /* SPE saves EH registers in 64-bits.  */
10355       ehrd_size = i * (TARGET_SPE_ABI
10356                        && info_ptr->spe_64bit_regs_used != 0
10357                        ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
10358     }
10359   else
10360     ehrd_size = 0;
10361
10362   /* Determine various sizes.  */
10363   info_ptr->reg_size     = reg_size;
10364   info_ptr->fixed_size   = RS6000_SAVE_AREA;
10365   info_ptr->varargs_size = RS6000_VARARGS_AREA;
10366   info_ptr->vars_size    = RS6000_ALIGN (get_frame_size (), 8);
10367   info_ptr->parm_size    = RS6000_ALIGN (current_function_outgoing_args_size,
10368                                          8);
10369
10370   if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
10371     info_ptr->spe_gp_size = 8 * (32 - info_ptr->first_gp_reg_save);
10372   else
10373     info_ptr->spe_gp_size = 0;
10374
10375   if (TARGET_ALTIVEC_ABI && TARGET_ALTIVEC_VRSAVE)
10376     {
10377       info_ptr->vrsave_mask = compute_vrsave_mask ();
10378       info_ptr->vrsave_size  = info_ptr->vrsave_mask ? 4 : 0;
10379     }
10380   else
10381     {
10382       info_ptr->vrsave_mask = 0;
10383       info_ptr->vrsave_size = 0;
10384     }
10385
10386   /* Calculate the offsets.  */
10387   switch (DEFAULT_ABI)
10388     {
10389     case ABI_NONE:
10390     default:
10391       abort ();
10392
10393     case ABI_AIX:
10394     case ABI_DARWIN:
10395       info_ptr->fp_save_offset   = - info_ptr->fp_size;
10396       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
10397
10398       if (TARGET_ALTIVEC_ABI)
10399         {
10400           info_ptr->vrsave_save_offset
10401             = info_ptr->gp_save_offset - info_ptr->vrsave_size;
10402
10403           /* Align stack so vector save area is on a quadword boundary.  */
10404           if (info_ptr->altivec_size != 0)
10405             info_ptr->altivec_padding_size
10406               = 16 - (-info_ptr->vrsave_save_offset % 16);
10407           else
10408             info_ptr->altivec_padding_size = 0;
10409
10410           info_ptr->altivec_save_offset
10411             = info_ptr->vrsave_save_offset
10412             - info_ptr->altivec_padding_size
10413             - info_ptr->altivec_size;
10414
10415           /* Adjust for AltiVec case.  */
10416           info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
10417         }
10418       else
10419         info_ptr->ehrd_offset      = info_ptr->gp_save_offset - ehrd_size;
10420       info_ptr->cr_save_offset   = reg_size; /* first word when 64-bit.  */
10421       info_ptr->lr_save_offset   = 2*reg_size;
10422       break;
10423
10424     case ABI_V4:
10425       info_ptr->fp_save_offset   = - info_ptr->fp_size;
10426       info_ptr->gp_save_offset   = info_ptr->fp_save_offset - info_ptr->gp_size;
10427       info_ptr->cr_save_offset   = info_ptr->gp_save_offset - info_ptr->cr_size;
10428
10429       if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
10430       {
10431         /* Align stack so SPE GPR save area is aligned on a
10432            double-word boundary.  */
10433         if (info_ptr->spe_gp_size != 0)
10434           info_ptr->spe_padding_size
10435             = 8 - (-info_ptr->cr_save_offset % 8);
10436         else
10437           info_ptr->spe_padding_size = 0;
10438
10439         info_ptr->spe_gp_save_offset
10440           = info_ptr->cr_save_offset
10441           - info_ptr->spe_padding_size
10442           - info_ptr->spe_gp_size;
10443
10444         /* Adjust for SPE case.  */
10445         info_ptr->toc_save_offset
10446           = info_ptr->spe_gp_save_offset - info_ptr->toc_size;
10447       }
10448       else if (TARGET_ALTIVEC_ABI)
10449         {
10450           info_ptr->vrsave_save_offset
10451             = info_ptr->cr_save_offset - info_ptr->vrsave_size;
10452
10453           /* Align stack so vector save area is on a quadword boundary.  */
10454           if (info_ptr->altivec_size != 0)
10455             info_ptr->altivec_padding_size
10456               = 16 - (-info_ptr->vrsave_save_offset % 16);
10457           else
10458             info_ptr->altivec_padding_size = 0;
10459
10460           info_ptr->altivec_save_offset
10461             = info_ptr->vrsave_save_offset
10462             - info_ptr->altivec_padding_size
10463             - info_ptr->altivec_size;
10464
10465           /* Adjust for AltiVec case.  */
10466           info_ptr->toc_save_offset
10467             = info_ptr->altivec_save_offset - info_ptr->toc_size;
10468         }
10469       else
10470         info_ptr->toc_save_offset  = info_ptr->cr_save_offset - info_ptr->toc_size;
10471       info_ptr->ehrd_offset      = info_ptr->toc_save_offset - ehrd_size;
10472       info_ptr->lr_save_offset   = reg_size;
10473       break;
10474     }
10475
10476   info_ptr->save_size    = RS6000_ALIGN (info_ptr->fp_size
10477                                          + info_ptr->gp_size
10478                                          + info_ptr->altivec_size
10479                                          + info_ptr->altivec_padding_size
10480                                          + info_ptr->spe_gp_size
10481                                          + info_ptr->spe_padding_size
10482                                          + ehrd_size
10483                                          + info_ptr->cr_size
10484                                          + info_ptr->lr_size
10485                                          + info_ptr->vrsave_size
10486                                          + info_ptr->toc_size,
10487                                          (TARGET_ALTIVEC_ABI || ABI_DARWIN)
10488                                          ? 16 : 8);
10489
10490   total_raw_size         = (info_ptr->vars_size
10491                             + info_ptr->parm_size
10492                             + info_ptr->save_size
10493                             + info_ptr->varargs_size
10494                             + info_ptr->fixed_size);
10495
10496   info_ptr->total_size =
10497     RS6000_ALIGN (total_raw_size, ABI_STACK_BOUNDARY / BITS_PER_UNIT);
10498
10499   /* Determine if we need to allocate any stack frame:
10500
10501      For AIX we need to push the stack if a frame pointer is needed
10502      (because the stack might be dynamically adjusted), if we are
10503      debugging, if we make calls, or if the sum of fp_save, gp_save,
10504      and local variables are more than the space needed to save all
10505      non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
10506      + 18*8 = 288 (GPR13 reserved).
10507
10508      For V.4 we don't have the stack cushion that AIX uses, but assume
10509      that the debugger can handle stackless frames.  */
10510
10511   if (info_ptr->calls_p)
10512     info_ptr->push_p = 1;
10513
10514   else if (DEFAULT_ABI == ABI_V4)
10515     info_ptr->push_p = total_raw_size > info_ptr->fixed_size;
10516
10517   else if (frame_pointer_needed)
10518     info_ptr->push_p = 1;
10519
10520   else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
10521     info_ptr->push_p = 1;
10522
10523   else
10524     info_ptr->push_p
10525       = total_raw_size - info_ptr->fixed_size > (TARGET_32BIT ? 220 : 288);
10526
10527   /* Zero offsets if we're not saving those registers.  */
10528   if (info_ptr->fp_size == 0)
10529     info_ptr->fp_save_offset = 0;
10530
10531   if (info_ptr->gp_size == 0)
10532     info_ptr->gp_save_offset = 0;
10533
10534   if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
10535     info_ptr->altivec_save_offset = 0;
10536
10537   if (! TARGET_ALTIVEC_ABI || info_ptr->vrsave_mask == 0)
10538     info_ptr->vrsave_save_offset = 0;
10539
10540   if (! TARGET_SPE_ABI
10541       || info_ptr->spe_64bit_regs_used == 0
10542       || info_ptr->spe_gp_size == 0)
10543     info_ptr->spe_gp_save_offset = 0;
10544
10545   if (! info_ptr->lr_save_p)
10546     info_ptr->lr_save_offset = 0;
10547
10548   if (! info_ptr->cr_save_p)
10549     info_ptr->cr_save_offset = 0;
10550
10551   if (! info_ptr->toc_save_p)
10552     info_ptr->toc_save_offset = 0;
10553
10554   return info_ptr;
10555 }
10556
10557 /* Return true if the current function uses any GPRs in 64-bit SIMD
10558    mode.  */
10559
10560 static bool
10561 spe_func_has_64bit_regs_p (void)
10562 {
10563   rtx insns, insn;
10564
10565   /* Functions that save and restore all the call-saved registers will
10566      need to save/restore the registers in 64-bits.  */
10567   if (current_function_calls_eh_return
10568       || current_function_calls_setjmp
10569       || current_function_has_nonlocal_goto)
10570     return true;
10571
10572   insns = get_insns ();
10573
10574   for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
10575     {
10576       if (INSN_P (insn))
10577         {
10578           rtx i;
10579
10580           i = PATTERN (insn);
10581           if (GET_CODE (i) == SET
10582               && SPE_VECTOR_MODE (GET_MODE (SET_SRC (i))))
10583             return true;
10584         }
10585     }
10586
10587   return false;
10588 }
10589
10590 void
10591 debug_stack_info (rs6000_stack_t *info)
10592 {
10593   const char *abi_string;
10594
10595   if (! info)
10596     info = rs6000_stack_info ();
10597
10598   fprintf (stderr, "\nStack information for function %s:\n",
10599            ((current_function_decl && DECL_NAME (current_function_decl))
10600             ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
10601             : "<unknown>"));
10602
10603   switch (info->abi)
10604     {
10605     default:             abi_string = "Unknown";        break;
10606     case ABI_NONE:       abi_string = "NONE";           break;
10607     case ABI_AIX:        abi_string = "AIX";            break;
10608     case ABI_DARWIN:     abi_string = "Darwin";         break;
10609     case ABI_V4:         abi_string = "V.4";            break;
10610     }
10611
10612   fprintf (stderr, "\tABI                 = %5s\n", abi_string);
10613
10614   if (TARGET_ALTIVEC_ABI)
10615     fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
10616
10617   if (TARGET_SPE_ABI)
10618     fprintf (stderr, "\tSPE ABI extensions enabled.\n");
10619
10620   if (info->first_gp_reg_save != 32)
10621     fprintf (stderr, "\tfirst_gp_reg_save   = %5d\n", info->first_gp_reg_save);
10622
10623   if (info->first_fp_reg_save != 64)
10624     fprintf (stderr, "\tfirst_fp_reg_save   = %5d\n", info->first_fp_reg_save);
10625
10626   if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
10627     fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
10628              info->first_altivec_reg_save);
10629
10630   if (info->lr_save_p)
10631     fprintf (stderr, "\tlr_save_p           = %5d\n", info->lr_save_p);
10632
10633   if (info->cr_save_p)
10634     fprintf (stderr, "\tcr_save_p           = %5d\n", info->cr_save_p);
10635
10636   if (info->toc_save_p)
10637     fprintf (stderr, "\ttoc_save_p          = %5d\n", info->toc_save_p);
10638
10639   if (info->vrsave_mask)
10640     fprintf (stderr, "\tvrsave_mask         = 0x%x\n", info->vrsave_mask);
10641
10642   if (info->push_p)
10643     fprintf (stderr, "\tpush_p              = %5d\n", info->push_p);
10644
10645   if (info->calls_p)
10646     fprintf (stderr, "\tcalls_p             = %5d\n", info->calls_p);
10647
10648   if (info->gp_save_offset)
10649     fprintf (stderr, "\tgp_save_offset      = %5d\n", info->gp_save_offset);
10650
10651   if (info->fp_save_offset)
10652     fprintf (stderr, "\tfp_save_offset      = %5d\n", info->fp_save_offset);
10653
10654   if (info->altivec_save_offset)
10655     fprintf (stderr, "\taltivec_save_offset = %5d\n",
10656              info->altivec_save_offset);
10657
10658   if (info->spe_gp_save_offset)
10659     fprintf (stderr, "\tspe_gp_save_offset  = %5d\n",
10660              info->spe_gp_save_offset);
10661
10662   if (info->vrsave_save_offset)
10663     fprintf (stderr, "\tvrsave_save_offset  = %5d\n",
10664              info->vrsave_save_offset);
10665
10666   if (info->lr_save_offset)
10667     fprintf (stderr, "\tlr_save_offset      = %5d\n", info->lr_save_offset);
10668
10669   if (info->cr_save_offset)
10670     fprintf (stderr, "\tcr_save_offset      = %5d\n", info->cr_save_offset);
10671
10672   if (info->toc_save_offset)
10673     fprintf (stderr, "\ttoc_save_offset     = %5d\n", info->toc_save_offset);
10674
10675   if (info->varargs_save_offset)
10676     fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
10677
10678   if (info->total_size)
10679     fprintf (stderr, "\ttotal_size          = %5d\n", info->total_size);
10680
10681   if (info->varargs_size)
10682     fprintf (stderr, "\tvarargs_size        = %5d\n", info->varargs_size);
10683
10684   if (info->vars_size)
10685     fprintf (stderr, "\tvars_size           = %5d\n", info->vars_size);
10686
10687   if (info->parm_size)
10688     fprintf (stderr, "\tparm_size           = %5d\n", info->parm_size);
10689
10690   if (info->fixed_size)
10691     fprintf (stderr, "\tfixed_size          = %5d\n", info->fixed_size);
10692
10693   if (info->gp_size)
10694     fprintf (stderr, "\tgp_size             = %5d\n", info->gp_size);
10695
10696   if (info->spe_gp_size)
10697     fprintf (stderr, "\tspe_gp_size         = %5d\n", info->spe_gp_size);
10698
10699   if (info->fp_size)
10700     fprintf (stderr, "\tfp_size             = %5d\n", info->fp_size);
10701
10702   if (info->altivec_size)
10703     fprintf (stderr, "\taltivec_size        = %5d\n", info->altivec_size);
10704
10705   if (info->vrsave_size)
10706     fprintf (stderr, "\tvrsave_size         = %5d\n", info->vrsave_size);
10707
10708   if (info->altivec_padding_size)
10709     fprintf (stderr, "\taltivec_padding_size= %5d\n",
10710              info->altivec_padding_size);
10711
10712   if (info->spe_padding_size)
10713     fprintf (stderr, "\tspe_padding_size    = %5d\n",
10714              info->spe_padding_size);
10715
10716   if (info->lr_size)
10717     fprintf (stderr, "\tlr_size             = %5d\n", info->lr_size);
10718
10719   if (info->cr_size)
10720     fprintf (stderr, "\tcr_size             = %5d\n", info->cr_size);
10721
10722   if (info->toc_size)
10723     fprintf (stderr, "\ttoc_size            = %5d\n", info->toc_size);
10724
10725   if (info->save_size)
10726     fprintf (stderr, "\tsave_size           = %5d\n", info->save_size);
10727
10728   if (info->reg_size != 4)
10729     fprintf (stderr, "\treg_size            = %5d\n", info->reg_size);
10730
10731   fprintf (stderr, "\n");
10732 }
10733
10734 rtx
10735 rs6000_return_addr (int count, rtx frame)
10736 {
10737   /* Currently we don't optimize very well between prolog and body
10738      code and for PIC code the code can be actually quite bad, so
10739      don't try to be too clever here.  */
10740   if (count != 0 || (DEFAULT_ABI != ABI_AIX && flag_pic))
10741     {
10742       cfun->machine->ra_needs_full_frame = 1;
10743
10744       return
10745         gen_rtx_MEM
10746           (Pmode,
10747            memory_address
10748            (Pmode,
10749             plus_constant (copy_to_reg
10750                            (gen_rtx_MEM (Pmode,
10751                                          memory_address (Pmode, frame))),
10752                            RETURN_ADDRESS_OFFSET)));
10753     }
10754
10755   cfun->machine->ra_need_lr = 1;
10756   return get_hard_reg_initial_val (Pmode, LINK_REGISTER_REGNUM);
10757 }
10758
10759 /* Say whether a function is a candidate for sibcall handling or not.
10760    We do not allow indirect calls to be optimized into sibling calls.
10761    Also, we can't do it if there are any vector parameters; there's
10762    nowhere to put the VRsave code so it works; note that functions with
10763    vector parameters are required to have a prototype, so the argument
10764    type info must be available here.  (The tail recursion case can work
10765    with vector parameters, but there's no way to distinguish here.) */
10766 static bool
10767 rs6000_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
10768 {
10769   tree type;
10770   if (decl)
10771     {
10772       if (TARGET_ALTIVEC_VRSAVE)
10773         {
10774           for (type = TYPE_ARG_TYPES (TREE_TYPE (decl));
10775                type; type = TREE_CHAIN (type))
10776             {
10777               if (TREE_CODE (TREE_VALUE (type)) == VECTOR_TYPE)
10778                 return false;
10779             }
10780         }
10781       if (DEFAULT_ABI == ABI_DARWIN
10782           || (*targetm.binds_local_p) (decl))
10783         {
10784           tree attr_list = TYPE_ATTRIBUTES (TREE_TYPE (decl));
10785
10786           if (!lookup_attribute ("longcall", attr_list)
10787               || lookup_attribute ("shortcall", attr_list))
10788             return true;
10789         }
10790     }
10791   return false;
10792 }
10793
10794 static int
10795 rs6000_ra_ever_killed (void)
10796 {
10797   rtx top;
10798   rtx reg;
10799   rtx insn;
10800
10801   /* Irritatingly, there are two kinds of thunks -- those created with
10802      TARGET_ASM_OUTPUT_MI_THUNK and those with DECL_THUNK_P that go
10803      through the regular part of the compiler.  This is a very hacky
10804      way to tell them apart.  */
10805   if (current_function_is_thunk && !no_new_pseudos)
10806     return 0;
10807
10808   /* regs_ever_live has LR marked as used if any sibcalls are present,
10809      but this should not force saving and restoring in the
10810      pro/epilogue.  Likewise, reg_set_between_p thinks a sibcall
10811      clobbers LR, so that is inappropriate. */
10812
10813   /* Also, the prologue can generate a store into LR that
10814      doesn't really count, like this:
10815
10816         move LR->R0
10817         bcl to set PIC register
10818         move LR->R31
10819         move R0->LR
10820
10821      When we're called from the epilogue, we need to avoid counting
10822      this as a store.  */
10823          
10824   push_topmost_sequence ();
10825   top = get_insns ();
10826   pop_topmost_sequence ();
10827   reg = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
10828
10829   for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
10830     {
10831       if (INSN_P (insn))
10832         {
10833           if (FIND_REG_INC_NOTE (insn, reg))
10834             return 1;
10835           else if (GET_CODE (insn) == CALL_INSN 
10836                    && !SIBLING_CALL_P (insn))
10837             return 1;
10838           else if (set_of (reg, insn) != NULL_RTX
10839                    && !prologue_epilogue_contains (insn))
10840             return 1;
10841         }
10842     }
10843   return 0;
10844 }
10845 \f
10846 /* Add a REG_MAYBE_DEAD note to the insn.  */
10847 static void
10848 rs6000_maybe_dead (rtx insn)
10849 {
10850   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
10851                                         const0_rtx,
10852                                         REG_NOTES (insn));
10853 }
10854
10855 /* Emit instructions needed to load the TOC register.
10856    This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
10857    a constant pool; or for SVR4 -fpic.  */
10858
10859 void
10860 rs6000_emit_load_toc_table (int fromprolog)
10861 {
10862   rtx dest, insn;
10863   dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
10864
10865   if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
10866     {
10867       rtx temp = (fromprolog
10868                   ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
10869                   : gen_reg_rtx (Pmode));
10870       insn = emit_insn (gen_load_toc_v4_pic_si (temp));
10871       if (fromprolog)
10872         rs6000_maybe_dead (insn);
10873       insn = emit_move_insn (dest, temp);
10874       if (fromprolog)
10875         rs6000_maybe_dead (insn);
10876     }
10877   else if (TARGET_ELF && DEFAULT_ABI != ABI_AIX && flag_pic == 2)
10878     {
10879       char buf[30];
10880       rtx tempLR = (fromprolog
10881                     ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
10882                     : gen_reg_rtx (Pmode));
10883       rtx temp0 = (fromprolog
10884                    ? gen_rtx_REG (Pmode, 0)
10885                    : gen_reg_rtx (Pmode));
10886       rtx symF;
10887
10888       /* possibly create the toc section */
10889       if (! toc_initialized)
10890         {
10891           toc_section ();
10892           function_section (current_function_decl);
10893         }
10894
10895       if (fromprolog)
10896         {
10897           rtx symL;
10898
10899           ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
10900           symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
10901
10902           ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
10903           symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
10904
10905           rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_1 (tempLR,
10906                                                                symF)));
10907           rs6000_maybe_dead (emit_move_insn (dest, tempLR));
10908           rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest,
10909                                                                symL,
10910                                                                symF)));
10911         }
10912       else
10913         {
10914           rtx tocsym;
10915           static int reload_toc_labelno = 0;
10916
10917           tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
10918
10919           ASM_GENERATE_INTERNAL_LABEL (buf, "LCG", reload_toc_labelno++);
10920           symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
10921
10922           emit_insn (gen_load_toc_v4_PIC_1b (tempLR, symF, tocsym));
10923           emit_move_insn (dest, tempLR);
10924           emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
10925         }
10926       insn = emit_insn (gen_addsi3 (dest, temp0, dest));
10927       if (fromprolog)
10928         rs6000_maybe_dead (insn);
10929     }
10930   else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
10931     {
10932       /* This is for AIX code running in non-PIC ELF32.  */
10933       char buf[30];
10934       rtx realsym;
10935       ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
10936       realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
10937
10938       insn = emit_insn (gen_elf_high (dest, realsym));
10939       if (fromprolog)
10940         rs6000_maybe_dead (insn);
10941       insn = emit_insn (gen_elf_low (dest, dest, realsym));
10942       if (fromprolog)
10943         rs6000_maybe_dead (insn);
10944     }
10945   else if (DEFAULT_ABI == ABI_AIX)
10946     {
10947       if (TARGET_32BIT)
10948         insn = emit_insn (gen_load_toc_aix_si (dest));
10949       else
10950         insn = emit_insn (gen_load_toc_aix_di (dest));
10951       if (fromprolog)
10952         rs6000_maybe_dead (insn);
10953     }
10954   else
10955     abort ();
10956 }
10957
10958 int   
10959 get_TOC_alias_set (void)
10960 {
10961     static int set = -1;
10962     if (set == -1)
10963       set = new_alias_set ();
10964     return set;
10965 }   
10966
10967 /* This returns nonzero if the current function uses the TOC.  This is
10968    determined by the presence of (unspec ... UNSPEC_TOC) or
10969    use (unspec ... UNSPEC_TOC), which are generated by the various
10970    load_toc_* patterns.  */
10971
10972 int
10973 uses_TOC (void) 
10974 {
10975   rtx insn;
10976
10977   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
10978     if (INSN_P (insn))
10979       {
10980         rtx pat = PATTERN (insn);
10981         int i;
10982
10983         if (GET_CODE (pat) == PARALLEL) 
10984           for (i = 0; i < XVECLEN (pat, 0); i++)
10985             {
10986               rtx sub = XVECEXP (pat, 0, i);
10987               if (GET_CODE (sub) == USE)
10988                 {
10989                   sub = XEXP (sub, 0);
10990                   if (GET_CODE (sub) == UNSPEC
10991                       && XINT (sub, 1) == UNSPEC_TOC)
10992                     return 1;
10993                 }
10994             }
10995       }
10996   return 0;
10997 }
10998
10999 rtx
11000 create_TOC_reference (rtx symbol) 
11001 {
11002   return gen_rtx_PLUS (Pmode, 
11003            gen_rtx_REG (Pmode, TOC_REGISTER),
11004              gen_rtx_CONST (Pmode, 
11005                gen_rtx_MINUS (Pmode, symbol, 
11006                  gen_rtx_SYMBOL_REF (Pmode, toc_label_name))));
11007 }
11008
11009 /* If _Unwind_* has been called from within the same module,
11010    toc register is not guaranteed to be saved to 40(1) on function
11011    entry.  Save it there in that case.  */
11012
11013 void
11014 rs6000_aix_emit_builtin_unwind_init (void)
11015 {
11016   rtx mem;
11017   rtx stack_top = gen_reg_rtx (Pmode);
11018   rtx opcode_addr = gen_reg_rtx (Pmode);
11019   rtx opcode = gen_reg_rtx (SImode);
11020   rtx tocompare = gen_reg_rtx (SImode);
11021   rtx no_toc_save_needed = gen_label_rtx ();
11022
11023   mem = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
11024   emit_move_insn (stack_top, mem);
11025
11026   mem = gen_rtx_MEM (Pmode,
11027                      gen_rtx_PLUS (Pmode, stack_top,
11028                                    GEN_INT (2 * GET_MODE_SIZE (Pmode))));
11029   emit_move_insn (opcode_addr, mem);
11030   emit_move_insn (opcode, gen_rtx_MEM (SImode, opcode_addr));
11031   emit_move_insn (tocompare, gen_int_mode (TARGET_32BIT ? 0x80410014
11032                                            : 0xE8410028, SImode));
11033
11034   do_compare_rtx_and_jump (opcode, tocompare, EQ, 1,
11035                            SImode, NULL_RTX, NULL_RTX,
11036                            no_toc_save_needed);
11037
11038   mem = gen_rtx_MEM (Pmode,
11039                      gen_rtx_PLUS (Pmode, stack_top,
11040                                    GEN_INT (5 * GET_MODE_SIZE (Pmode))));
11041   emit_move_insn (mem, gen_rtx_REG (Pmode, 2));
11042   emit_label (no_toc_save_needed);
11043 }
11044 \f
11045 /* This ties together stack memory (MEM with an alias set of
11046    rs6000_sr_alias_set) and the change to the stack pointer.  */
11047
11048 static void
11049 rs6000_emit_stack_tie (void)
11050 {
11051   rtx mem = gen_rtx_MEM (BLKmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM));
11052
11053   set_mem_alias_set (mem, rs6000_sr_alias_set);
11054   emit_insn (gen_stack_tie (mem));
11055 }
11056
11057 /* Emit the correct code for allocating stack space, as insns.
11058    If COPY_R12, make sure a copy of the old frame is left in r12.
11059    The generated code may use hard register 0 as a temporary.  */
11060
11061 static void
11062 rs6000_emit_allocate_stack (HOST_WIDE_INT size, int copy_r12)
11063 {
11064   rtx insn;
11065   rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
11066   rtx tmp_reg = gen_rtx_REG (Pmode, 0);
11067   rtx todec = GEN_INT (-size);
11068
11069   if (current_function_limit_stack)
11070     {
11071       if (REG_P (stack_limit_rtx)
11072           && REGNO (stack_limit_rtx) > 1 
11073           && REGNO (stack_limit_rtx) <= 31)
11074         {
11075           emit_insn (TARGET_32BIT
11076                      ? gen_addsi3 (tmp_reg,
11077                                    stack_limit_rtx,
11078                                    GEN_INT (size))
11079                      : gen_adddi3 (tmp_reg,
11080                                    stack_limit_rtx,
11081                                    GEN_INT (size)));
11082
11083           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
11084                                     const0_rtx));
11085         }
11086       else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
11087                && TARGET_32BIT
11088                && DEFAULT_ABI == ABI_V4)
11089         {
11090           rtx toload = gen_rtx_CONST (VOIDmode,
11091                                       gen_rtx_PLUS (Pmode, 
11092                                                     stack_limit_rtx, 
11093                                                     GEN_INT (size)));
11094
11095           emit_insn (gen_elf_high (tmp_reg, toload));
11096           emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
11097           emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
11098                                     const0_rtx));
11099         }
11100       else
11101         warning ("stack limit expression is not supported");
11102     }
11103
11104   if (copy_r12 || ! TARGET_UPDATE)
11105     emit_move_insn (gen_rtx_REG (Pmode, 12), stack_reg);
11106
11107   if (TARGET_UPDATE)
11108     {
11109       if (size > 32767)
11110         {
11111           /* Need a note here so that try_split doesn't get confused.  */
11112           if (get_last_insn() == NULL_RTX)
11113             emit_note (NOTE_INSN_DELETED);
11114           insn = emit_move_insn (tmp_reg, todec);
11115           try_split (PATTERN (insn), insn, 0);
11116           todec = tmp_reg;
11117         }
11118
11119       insn = emit_insn (TARGET_32BIT
11120                         ? gen_movsi_update (stack_reg, stack_reg,
11121                                             todec, stack_reg)
11122                         : gen_movdi_update (stack_reg, stack_reg, 
11123                                             todec, stack_reg));
11124     }
11125   else
11126     {
11127       insn = emit_insn (TARGET_32BIT
11128                         ? gen_addsi3 (stack_reg, stack_reg, todec)
11129                         : gen_adddi3 (stack_reg, stack_reg, todec));
11130       emit_move_insn (gen_rtx_MEM (Pmode, stack_reg),
11131                       gen_rtx_REG (Pmode, 12));
11132     }
11133  
11134   RTX_FRAME_RELATED_P (insn) = 1;
11135   REG_NOTES (insn) = 
11136     gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
11137                        gen_rtx_SET (VOIDmode, stack_reg, 
11138                                     gen_rtx_PLUS (Pmode, stack_reg,
11139                                                   GEN_INT (-size))),
11140                        REG_NOTES (insn));
11141 }
11142
11143 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
11144    with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
11145    is not NULL.  It would be nice if dwarf2out_frame_debug_expr could
11146    deduce these equivalences by itself so it wasn't necessary to hold
11147    its hand so much.  */
11148
11149 static void
11150 rs6000_frame_related (rtx insn, rtx reg, HOST_WIDE_INT val, 
11151                       rtx reg2, rtx rreg)
11152 {
11153   rtx real, temp;
11154
11155   /* copy_rtx will not make unique copies of registers, so we need to
11156      ensure we don't have unwanted sharing here.  */
11157   if (reg == reg2)
11158     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
11159
11160   if (reg == rreg)
11161     reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
11162
11163   real = copy_rtx (PATTERN (insn));
11164
11165   if (reg2 != NULL_RTX)
11166     real = replace_rtx (real, reg2, rreg);
11167   
11168   real = replace_rtx (real, reg, 
11169                       gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
11170                                                         STACK_POINTER_REGNUM),
11171                                     GEN_INT (val)));
11172   
11173   /* We expect that 'real' is either a SET or a PARALLEL containing
11174      SETs (and possibly other stuff).  In a PARALLEL, all the SETs
11175      are important so they all have to be marked RTX_FRAME_RELATED_P.  */
11176
11177   if (GET_CODE (real) == SET)
11178     {
11179       rtx set = real;
11180       
11181       temp = simplify_rtx (SET_SRC (set));
11182       if (temp)
11183         SET_SRC (set) = temp;
11184       temp = simplify_rtx (SET_DEST (set));
11185       if (temp)
11186         SET_DEST (set) = temp;
11187       if (GET_CODE (SET_DEST (set)) == MEM)
11188         {
11189           temp = simplify_rtx (XEXP (SET_DEST (set), 0));
11190           if (temp)
11191             XEXP (SET_DEST (set), 0) = temp;
11192         }
11193     }
11194   else if (GET_CODE (real) == PARALLEL)
11195     {
11196       int i;
11197       for (i = 0; i < XVECLEN (real, 0); i++)
11198         if (GET_CODE (XVECEXP (real, 0, i)) == SET)
11199           {
11200             rtx set = XVECEXP (real, 0, i);
11201             
11202             temp = simplify_rtx (SET_SRC (set));
11203             if (temp)
11204               SET_SRC (set) = temp;
11205             temp = simplify_rtx (SET_DEST (set));
11206             if (temp)
11207               SET_DEST (set) = temp;
11208             if (GET_CODE (SET_DEST (set)) == MEM)
11209               {
11210                 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
11211                 if (temp)
11212                   XEXP (SET_DEST (set), 0) = temp;
11213               }
11214             RTX_FRAME_RELATED_P (set) = 1;
11215           }
11216     }
11217   else
11218     abort ();
11219
11220   if (TARGET_SPE)
11221     real = spe_synthesize_frame_save (real);
11222
11223   RTX_FRAME_RELATED_P (insn) = 1;
11224   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
11225                                         real,
11226                                         REG_NOTES (insn));
11227 }
11228
11229 /* Given an SPE frame note, return a PARALLEL of SETs with the
11230    original note, plus a synthetic register save.  */
11231
11232 static rtx
11233 spe_synthesize_frame_save (rtx real)
11234 {
11235   rtx synth, offset, reg, real2;
11236
11237   if (GET_CODE (real) != SET
11238       || GET_MODE (SET_SRC (real)) != V2SImode)
11239     return real;
11240
11241   /* For the SPE, registers saved in 64-bits, get a PARALLEL for their
11242      frame related note.  The parallel contains a set of the register
11243      being saved, and another set to a synthetic register (n+1200).
11244      This is so we can differentiate between 64-bit and 32-bit saves.
11245      Words cannot describe this nastiness.  */
11246
11247   if (GET_CODE (SET_DEST (real)) != MEM
11248       || GET_CODE (XEXP (SET_DEST (real), 0)) != PLUS
11249       || GET_CODE (SET_SRC (real)) != REG)
11250     abort ();
11251
11252   /* Transform:
11253        (set (mem (plus (reg x) (const y)))
11254             (reg z))
11255      into:
11256        (set (mem (plus (reg x) (const y+4)))
11257             (reg z+1200))
11258   */
11259
11260   real2 = copy_rtx (real);
11261   PUT_MODE (SET_DEST (real2), SImode);
11262   reg = SET_SRC (real2);
11263   real2 = replace_rtx (real2, reg, gen_rtx_REG (SImode, REGNO (reg)));
11264   synth = copy_rtx (real2);
11265
11266   if (BYTES_BIG_ENDIAN)
11267     {
11268       offset = XEXP (XEXP (SET_DEST (real2), 0), 1);
11269       real2 = replace_rtx (real2, offset, GEN_INT (INTVAL (offset) + 4));
11270     }
11271
11272   reg = SET_SRC (synth);
11273
11274   synth = replace_rtx (synth, reg,
11275                        gen_rtx_REG (SImode, REGNO (reg) + 1200));
11276
11277   offset = XEXP (XEXP (SET_DEST (synth), 0), 1);
11278   synth = replace_rtx (synth, offset,
11279                        GEN_INT (INTVAL (offset)
11280                                 + (BYTES_BIG_ENDIAN ? 0 : 4)));
11281
11282   RTX_FRAME_RELATED_P (synth) = 1;
11283   RTX_FRAME_RELATED_P (real2) = 1;
11284   if (BYTES_BIG_ENDIAN)
11285     real = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, synth, real2));
11286   else
11287     real = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, real2, synth));
11288
11289   return real;
11290 }
11291
11292 /* Returns an insn that has a vrsave set operation with the
11293    appropriate CLOBBERs.  */
11294
11295 static rtx
11296 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
11297 {
11298   int nclobs, i;
11299   rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
11300   rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
11301
11302   clobs[0]
11303     = gen_rtx_SET (VOIDmode,
11304                    vrsave,
11305                    gen_rtx_UNSPEC_VOLATILE (SImode,
11306                                             gen_rtvec (2, reg, vrsave),
11307                                             30));
11308
11309   nclobs = 1;
11310
11311   /* We need to clobber the registers in the mask so the scheduler
11312      does not move sets to VRSAVE before sets of AltiVec registers.
11313
11314      However, if the function receives nonlocal gotos, reload will set
11315      all call saved registers live.  We will end up with:
11316
11317         (set (reg 999) (mem))
11318         (parallel [ (set (reg vrsave) (unspec blah))
11319                     (clobber (reg 999))])
11320
11321      The clobber will cause the store into reg 999 to be dead, and
11322      flow will attempt to delete an epilogue insn.  In this case, we
11323      need an unspec use/set of the register.  */
11324
11325   for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
11326     if (info->vrsave_mask != 0 && ALTIVEC_REG_BIT (i) != 0)
11327       {
11328         if (!epiloguep || call_used_regs [i])
11329           clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
11330                                              gen_rtx_REG (V4SImode, i));
11331         else
11332           {
11333             rtx reg = gen_rtx_REG (V4SImode, i);
11334
11335             clobs[nclobs++]
11336               = gen_rtx_SET (VOIDmode,
11337                              reg,
11338                              gen_rtx_UNSPEC (V4SImode,
11339                                              gen_rtvec (1, reg), 27));
11340           }
11341       }
11342
11343   insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
11344
11345   for (i = 0; i < nclobs; ++i)
11346     XVECEXP (insn, 0, i) = clobs[i];
11347
11348   return insn;
11349 }
11350
11351 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
11352    Save REGNO into [FRAME_REG + OFFSET] in mode MODE.  */
11353
11354 static void
11355 emit_frame_save (rtx frame_reg, rtx frame_ptr, enum machine_mode mode, 
11356                  unsigned int regno, int offset, int total_size)
11357 {
11358   rtx reg, offset_rtx, insn, mem, addr, int_rtx;
11359   rtx replacea, replaceb;
11360
11361   int_rtx = GEN_INT (offset);
11362
11363   /* Some cases that need register indexed addressing.  */
11364   if ((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
11365       || (TARGET_SPE_ABI
11366           && SPE_VECTOR_MODE (mode)
11367           && !SPE_CONST_OFFSET_OK (offset)))
11368     {
11369       /* Whomever calls us must make sure r11 is available in the
11370          flow path of instructions in the prologue.  */
11371       offset_rtx = gen_rtx_REG (Pmode, 11);
11372       emit_move_insn (offset_rtx, int_rtx);
11373
11374       replacea = offset_rtx;
11375       replaceb = int_rtx;
11376     }
11377   else
11378     {
11379       offset_rtx = int_rtx;
11380       replacea = NULL_RTX;
11381       replaceb = NULL_RTX;
11382     }
11383
11384   reg = gen_rtx_REG (mode, regno);
11385   addr = gen_rtx_PLUS (Pmode, frame_reg, offset_rtx);
11386   mem = gen_rtx_MEM (mode, addr);
11387   set_mem_alias_set (mem, rs6000_sr_alias_set);
11388
11389   insn = emit_move_insn (mem, reg);
11390
11391   rs6000_frame_related (insn, frame_ptr, total_size, replacea, replaceb);
11392 }
11393
11394 /* Emit an offset memory reference suitable for a frame store, while
11395    converting to a valid addressing mode.  */
11396
11397 static rtx
11398 gen_frame_mem_offset (enum machine_mode mode, rtx reg, int offset)
11399 {
11400   rtx int_rtx, offset_rtx;
11401
11402   int_rtx = GEN_INT (offset);
11403
11404   if (TARGET_SPE_ABI && SPE_VECTOR_MODE (mode))
11405     {
11406       offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
11407       emit_move_insn (offset_rtx, int_rtx);
11408     }
11409   else
11410     offset_rtx = int_rtx;
11411
11412   return gen_rtx_MEM (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
11413 }
11414
11415 /* Emit function prologue as insns.  */
11416
11417 void
11418 rs6000_emit_prologue (void)
11419 {
11420   rs6000_stack_t *info = rs6000_stack_info ();
11421   enum machine_mode reg_mode = Pmode;
11422   int reg_size = UNITS_PER_WORD;
11423   rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
11424   rtx frame_ptr_rtx = gen_rtx_REG (Pmode, 12);
11425   rtx frame_reg_rtx = sp_reg_rtx;
11426   rtx cr_save_rtx = NULL_RTX;
11427   rtx insn;
11428   int saving_FPRs_inline;
11429   int using_store_multiple;
11430   HOST_WIDE_INT sp_offset = 0;
11431   
11432    if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
11433      {
11434        reg_mode = V2SImode;
11435        reg_size = 8;
11436      }
11437
11438   using_store_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
11439                           && (!TARGET_SPE_ABI
11440                               || info->spe_64bit_regs_used == 0)
11441                           && info->first_gp_reg_save < 31);
11442   saving_FPRs_inline = (info->first_fp_reg_save == 64
11443                         || FP_SAVE_INLINE (info->first_fp_reg_save)
11444                         || current_function_calls_eh_return
11445                         || cfun->machine->ra_need_lr);
11446
11447   /* For V.4, update stack before we do any saving and set back pointer.  */
11448   if (info->push_p
11449       && (DEFAULT_ABI == ABI_V4
11450           || current_function_calls_eh_return))
11451     {
11452       if (info->total_size < 32767)
11453         sp_offset = info->total_size;
11454       else
11455         frame_reg_rtx = frame_ptr_rtx;
11456       rs6000_emit_allocate_stack (info->total_size, 
11457                                   (frame_reg_rtx != sp_reg_rtx
11458                                    && (info->cr_save_p
11459                                        || info->lr_save_p
11460                                        || info->first_fp_reg_save < 64
11461                                        || info->first_gp_reg_save < 32
11462                                        )));
11463       if (frame_reg_rtx != sp_reg_rtx)
11464         rs6000_emit_stack_tie ();
11465     }
11466
11467   /* Save AltiVec registers if needed.  */
11468   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
11469     {
11470       int i;
11471
11472       /* There should be a non inline version of this, for when we
11473          are saving lots of vector registers.  */
11474       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
11475         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
11476           {
11477             rtx areg, savereg, mem;
11478             int offset;
11479
11480             offset = info->altivec_save_offset + sp_offset
11481               + 16 * (i - info->first_altivec_reg_save);
11482
11483             savereg = gen_rtx_REG (V4SImode, i);
11484
11485             areg = gen_rtx_REG (Pmode, 0);
11486             emit_move_insn (areg, GEN_INT (offset));
11487
11488             /* AltiVec addressing mode is [reg+reg].  */
11489             mem = gen_rtx_MEM (V4SImode,
11490                                gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
11491                                
11492             set_mem_alias_set (mem, rs6000_sr_alias_set);
11493
11494             insn = emit_move_insn (mem, savereg);
11495
11496             rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
11497                                   areg, GEN_INT (offset));
11498           }
11499     }
11500
11501   /* VRSAVE is a bit vector representing which AltiVec registers
11502      are used.  The OS uses this to determine which vector
11503      registers to save on a context switch.  We need to save
11504      VRSAVE on the stack frame, add whatever AltiVec registers we
11505      used in this function, and do the corresponding magic in the
11506      epilogue.  */
11507
11508   if (TARGET_ALTIVEC && info->vrsave_mask != 0)
11509     {
11510       rtx reg, mem, vrsave;
11511       int offset;
11512
11513       /* Get VRSAVE onto a GPR.  */
11514       reg = gen_rtx_REG (SImode, 12);
11515       vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
11516       if (TARGET_MACHO)
11517         emit_insn (gen_get_vrsave_internal (reg));
11518       else
11519         emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
11520
11521       /* Save VRSAVE.  */
11522       offset = info->vrsave_save_offset + sp_offset;
11523       mem
11524         = gen_rtx_MEM (SImode,
11525                        gen_rtx_PLUS (Pmode, frame_reg_rtx, GEN_INT (offset)));
11526       set_mem_alias_set (mem, rs6000_sr_alias_set);
11527       insn = emit_move_insn (mem, reg);
11528
11529       /* Include the registers in the mask.  */
11530       emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
11531
11532       insn = emit_insn (generate_set_vrsave (reg, info, 0));
11533     }
11534
11535   /* If we use the link register, get it into r0.  */
11536   if (info->lr_save_p)
11537     emit_move_insn (gen_rtx_REG (Pmode, 0),
11538                     gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
11539
11540   /* If we need to save CR, put it into r12.  */
11541   if (info->cr_save_p && frame_reg_rtx != frame_ptr_rtx)
11542     {
11543       cr_save_rtx = gen_rtx_REG (SImode, 12);
11544       emit_insn (gen_movesi_from_cr (cr_save_rtx));
11545     }
11546
11547   /* Do any required saving of fpr's.  If only one or two to save, do
11548      it ourselves.  Otherwise, call function.  */
11549   if (saving_FPRs_inline)
11550     {
11551       int i;
11552       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
11553         if ((regs_ever_live[info->first_fp_reg_save+i] 
11554              && ! call_used_regs[info->first_fp_reg_save+i]))
11555           emit_frame_save (frame_reg_rtx, frame_ptr_rtx, DFmode,
11556                            info->first_fp_reg_save + i,
11557                            info->fp_save_offset + sp_offset + 8 * i,
11558                            info->total_size);
11559     }
11560   else if (info->first_fp_reg_save != 64)
11561     {
11562       int i;
11563       char rname[30];
11564       const char *alloc_rname;
11565       rtvec p;
11566       p = rtvec_alloc (2 + 64 - info->first_fp_reg_save);
11567       
11568       RTVEC_ELT (p, 0) = gen_rtx_CLOBBER (VOIDmode, 
11569                                           gen_rtx_REG (Pmode, 
11570                                                        LINK_REGISTER_REGNUM));
11571       sprintf (rname, "%s%d%s", SAVE_FP_PREFIX,
11572                info->first_fp_reg_save - 32, SAVE_FP_SUFFIX);
11573       alloc_rname = ggc_strdup (rname);
11574       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode,
11575                                       gen_rtx_SYMBOL_REF (Pmode,
11576                                                           alloc_rname));
11577       for (i = 0; i < 64 - info->first_fp_reg_save; i++)
11578         {
11579           rtx addr, reg, mem;
11580           reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
11581           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
11582                                GEN_INT (info->fp_save_offset 
11583                                         + sp_offset + 8*i));
11584           mem = gen_rtx_MEM (DFmode, addr);
11585           set_mem_alias_set (mem, rs6000_sr_alias_set);
11586
11587           RTVEC_ELT (p, i + 2) = gen_rtx_SET (VOIDmode, mem, reg);
11588         }
11589       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
11590       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
11591                             NULL_RTX, NULL_RTX);
11592     }
11593
11594   /* Save GPRs.  This is done as a PARALLEL if we are using
11595      the store-multiple instructions.  */
11596   if (using_store_multiple)
11597     {
11598       rtvec p;
11599       int i;
11600       p = rtvec_alloc (32 - info->first_gp_reg_save);
11601       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
11602         {
11603           rtx addr, reg, mem;
11604           reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
11605           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
11606                                GEN_INT (info->gp_save_offset 
11607                                         + sp_offset 
11608                                         + reg_size * i));
11609           mem = gen_rtx_MEM (reg_mode, addr);
11610           set_mem_alias_set (mem, rs6000_sr_alias_set);
11611
11612           RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, reg);
11613         }
11614       insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
11615       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
11616                             NULL_RTX, NULL_RTX);
11617     }
11618   else
11619     {
11620       int i;
11621       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
11622         if ((regs_ever_live[info->first_gp_reg_save+i] 
11623              && ! call_used_regs[info->first_gp_reg_save+i])
11624             || (i+info->first_gp_reg_save == RS6000_PIC_OFFSET_TABLE_REGNUM
11625                 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
11626                     || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
11627           {
11628             rtx addr, reg, mem;
11629             reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
11630
11631             if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
11632               {
11633                 int offset = info->spe_gp_save_offset + sp_offset + 8 * i;
11634                 rtx b;
11635
11636                 if (!SPE_CONST_OFFSET_OK (offset))
11637                   {
11638                     b = gen_rtx_REG (Pmode, FIXED_SCRATCH);
11639                     emit_move_insn (b, GEN_INT (offset));
11640                   }
11641                 else
11642                   b = GEN_INT (offset);
11643
11644                 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, b);
11645                 mem = gen_rtx_MEM (V2SImode, addr);
11646                 set_mem_alias_set (mem, rs6000_sr_alias_set);
11647                 insn = emit_move_insn (mem, reg);
11648
11649                 if (GET_CODE (b) == CONST_INT)
11650                   rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
11651                                         NULL_RTX, NULL_RTX);
11652                 else
11653                   rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
11654                                         b, GEN_INT (offset));
11655               }
11656             else
11657               {
11658                 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
11659                                      GEN_INT (info->gp_save_offset 
11660                                               + sp_offset 
11661                                               + reg_size * i));
11662                 mem = gen_rtx_MEM (reg_mode, addr);
11663                 set_mem_alias_set (mem, rs6000_sr_alias_set);
11664
11665                 insn = emit_move_insn (mem, reg);
11666                 rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
11667                                       NULL_RTX, NULL_RTX);
11668               }
11669           }
11670     }
11671
11672   /* ??? There's no need to emit actual instructions here, but it's the
11673      easiest way to get the frame unwind information emitted.  */
11674   if (current_function_calls_eh_return)
11675     {
11676       unsigned int i, regno;
11677
11678       /* In AIX ABI we need to pretend we save r2 here.  */
11679       if (TARGET_AIX)
11680         {
11681           rtx addr, reg, mem;
11682
11683           reg = gen_rtx_REG (reg_mode, 2);
11684           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
11685                                GEN_INT (sp_offset + 5 * reg_size));
11686           mem = gen_rtx_MEM (reg_mode, addr);
11687           set_mem_alias_set (mem, rs6000_sr_alias_set);
11688
11689           insn = emit_move_insn (mem, reg);
11690           rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
11691                                 NULL_RTX, NULL_RTX);
11692           PATTERN (insn) = gen_blockage ();
11693         }
11694
11695       for (i = 0; ; ++i)
11696         {
11697           regno = EH_RETURN_DATA_REGNO (i);
11698           if (regno == INVALID_REGNUM)
11699             break;
11700
11701           emit_frame_save (frame_reg_rtx, frame_ptr_rtx, reg_mode, regno,
11702                            info->ehrd_offset + sp_offset
11703                            + reg_size * (int) i,
11704                            info->total_size);
11705         }
11706     }
11707
11708   /* Save lr if we used it.  */
11709   if (info->lr_save_p)
11710     {
11711       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
11712                                GEN_INT (info->lr_save_offset + sp_offset));
11713       rtx reg = gen_rtx_REG (Pmode, 0);
11714       rtx mem = gen_rtx_MEM (Pmode, addr);
11715       /* This should not be of rs6000_sr_alias_set, because of
11716          __builtin_return_address.  */
11717       
11718       insn = emit_move_insn (mem, reg);
11719       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
11720                             reg, gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
11721     }
11722
11723   /* Save CR if we use any that must be preserved.  */
11724   if (info->cr_save_p)
11725     {
11726       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
11727                                GEN_INT (info->cr_save_offset + sp_offset));
11728       rtx mem = gen_rtx_MEM (SImode, addr);
11729
11730       set_mem_alias_set (mem, rs6000_sr_alias_set);
11731
11732       /* If r12 was used to hold the original sp, copy cr into r0 now
11733          that it's free.  */
11734       if (REGNO (frame_reg_rtx) == 12)
11735         {
11736           cr_save_rtx = gen_rtx_REG (SImode, 0);
11737           emit_insn (gen_movesi_from_cr (cr_save_rtx));
11738         }
11739       insn = emit_move_insn (mem, cr_save_rtx);
11740
11741       /* Now, there's no way that dwarf2out_frame_debug_expr is going
11742          to understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)'.
11743          But that's OK.  All we have to do is specify that _one_ condition
11744          code register is saved in this stack slot.  The thrower's epilogue
11745          will then restore all the call-saved registers.
11746          We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux.  */
11747       rs6000_frame_related (insn, frame_ptr_rtx, info->total_size, 
11748                             cr_save_rtx, gen_rtx_REG (SImode, CR2_REGNO));
11749     }
11750
11751   /* Update stack and set back pointer unless this is V.4, 
11752      for which it was done previously.  */
11753   if (info->push_p
11754       && !(DEFAULT_ABI == ABI_V4 || current_function_calls_eh_return))
11755     rs6000_emit_allocate_stack (info->total_size, FALSE);
11756
11757   /* Set frame pointer, if needed.  */
11758   if (frame_pointer_needed)
11759     {
11760       insn = emit_move_insn (gen_rtx_REG (Pmode, FRAME_POINTER_REGNUM), 
11761                              sp_reg_rtx);
11762       RTX_FRAME_RELATED_P (insn) = 1;
11763     }
11764
11765   /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up.  */
11766   if ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
11767       || (DEFAULT_ABI == ABI_V4 && flag_pic == 1
11768           && regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM]))
11769   {
11770     /* If emit_load_toc_table will use the link register, we need to save
11771        it.  We use R12 for this purpose because emit_load_toc_table
11772        can use register 0.  This allows us to use a plain 'blr' to return
11773        from the procedure more often.  */
11774     int save_LR_around_toc_setup = (TARGET_ELF
11775                                     && DEFAULT_ABI != ABI_AIX
11776                                     && flag_pic
11777                                     && ! info->lr_save_p
11778                                     && EXIT_BLOCK_PTR->pred != NULL);
11779     if (save_LR_around_toc_setup)
11780       {
11781         rtx lr = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
11782         rs6000_maybe_dead (emit_move_insn (frame_ptr_rtx, lr));
11783         rs6000_emit_load_toc_table (TRUE);
11784         rs6000_maybe_dead (emit_move_insn (lr, frame_ptr_rtx));
11785       }
11786     else
11787       rs6000_emit_load_toc_table (TRUE);
11788   }
11789
11790 #if TARGET_MACHO
11791   if (DEFAULT_ABI == ABI_DARWIN
11792       && flag_pic && current_function_uses_pic_offset_table)
11793     {
11794       rtx dest = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
11795       const char *picbase = machopic_function_base_name ();
11796       rtx src = gen_rtx_SYMBOL_REF (Pmode, picbase);
11797
11798       rs6000_maybe_dead (emit_insn (gen_load_macho_picbase (dest, src)));
11799
11800       rs6000_maybe_dead (
11801         emit_move_insn (gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM),
11802                         gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)));
11803     }
11804 #endif
11805 }
11806
11807 /* Write function prologue.  */
11808
11809 static void
11810 rs6000_output_function_prologue (FILE *file, 
11811                                  HOST_WIDE_INT size ATTRIBUTE_UNUSED)
11812 {
11813   rs6000_stack_t *info = rs6000_stack_info ();
11814
11815   if (TARGET_DEBUG_STACK)
11816     debug_stack_info (info);
11817
11818   /* Write .extern for any function we will call to save and restore
11819      fp values.  */
11820   if (info->first_fp_reg_save < 64
11821       && !FP_SAVE_INLINE (info->first_fp_reg_save))
11822     fprintf (file, "\t.extern %s%d%s\n\t.extern %s%d%s\n",
11823              SAVE_FP_PREFIX, info->first_fp_reg_save - 32, SAVE_FP_SUFFIX,
11824              RESTORE_FP_PREFIX, info->first_fp_reg_save - 32,
11825              RESTORE_FP_SUFFIX);
11826
11827   /* Write .extern for AIX common mode routines, if needed.  */
11828   if (! TARGET_POWER && ! TARGET_POWERPC && ! common_mode_defined)
11829     {
11830       fputs ("\t.extern __mulh\n", file);
11831       fputs ("\t.extern __mull\n", file);
11832       fputs ("\t.extern __divss\n", file);
11833       fputs ("\t.extern __divus\n", file);
11834       fputs ("\t.extern __quoss\n", file);
11835       fputs ("\t.extern __quous\n", file);
11836       common_mode_defined = 1;
11837     }
11838
11839   if (! HAVE_prologue)
11840     {
11841       start_sequence ();
11842
11843       /* A NOTE_INSN_DELETED is supposed to be at the start and end of
11844          the "toplevel" insn chain.  */
11845       emit_note (NOTE_INSN_DELETED);
11846       rs6000_emit_prologue ();
11847       emit_note (NOTE_INSN_DELETED);
11848
11849       /* Expand INSN_ADDRESSES so final() doesn't crash. */
11850       {
11851         rtx insn;
11852         unsigned addr = 0;
11853         for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
11854           {
11855             INSN_ADDRESSES_NEW (insn, addr);
11856             addr += 4;
11857           }
11858       }
11859
11860       if (TARGET_DEBUG_STACK)
11861         debug_rtx_list (get_insns (), 100);
11862       final (get_insns (), file, FALSE, FALSE);
11863       end_sequence ();
11864     }
11865
11866   rs6000_pic_labelno++;
11867 }
11868   
11869 /* Emit function epilogue as insns.
11870
11871    At present, dwarf2out_frame_debug_expr doesn't understand
11872    register restores, so we don't bother setting RTX_FRAME_RELATED_P
11873    anywhere in the epilogue.  Most of the insns below would in any case
11874    need special notes to explain where r11 is in relation to the stack.  */
11875
11876 void
11877 rs6000_emit_epilogue (int sibcall)
11878 {
11879   rs6000_stack_t *info;
11880   int restoring_FPRs_inline;
11881   int using_load_multiple;
11882   int using_mfcr_multiple;
11883   int use_backchain_to_restore_sp;
11884   int sp_offset = 0;
11885   rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
11886   rtx frame_reg_rtx = sp_reg_rtx;
11887   enum machine_mode reg_mode = Pmode;
11888   int reg_size = UNITS_PER_WORD;
11889   int i;
11890
11891   info = rs6000_stack_info ();
11892
11893   if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
11894     {
11895       reg_mode = V2SImode;
11896       reg_size = 8;
11897     }
11898
11899   using_load_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
11900                          && (!TARGET_SPE_ABI
11901                              || info->spe_64bit_regs_used == 0)
11902                          && info->first_gp_reg_save < 31);
11903   restoring_FPRs_inline = (sibcall
11904                            || current_function_calls_eh_return
11905                            || info->first_fp_reg_save == 64
11906                            || FP_SAVE_INLINE (info->first_fp_reg_save));
11907   use_backchain_to_restore_sp = (frame_pointer_needed 
11908                                  || current_function_calls_alloca
11909                                  || info->total_size > 32767);
11910   using_mfcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
11911                          || rs6000_cpu == PROCESSOR_PPC603
11912                          || rs6000_cpu == PROCESSOR_PPC750
11913                          || optimize_size);
11914
11915   /* If we have a frame pointer, a call to alloca,  or a large stack
11916      frame, restore the old stack pointer using the backchain.  Otherwise,
11917      we know what size to update it with.  */
11918   if (use_backchain_to_restore_sp)
11919     {
11920       /* Under V.4, don't reset the stack pointer until after we're done
11921          loading the saved registers.  */
11922       if (DEFAULT_ABI == ABI_V4)
11923         frame_reg_rtx = gen_rtx_REG (Pmode, 11);
11924
11925       emit_move_insn (frame_reg_rtx,
11926                       gen_rtx_MEM (Pmode, sp_reg_rtx));
11927       
11928     }
11929   else if (info->push_p)
11930     {
11931       if (DEFAULT_ABI == ABI_V4
11932           || current_function_calls_eh_return)
11933         sp_offset = info->total_size;
11934       else
11935         {
11936           emit_insn (TARGET_32BIT
11937                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
11938                                    GEN_INT (info->total_size))
11939                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
11940                                    GEN_INT (info->total_size)));
11941         }
11942     }
11943   
11944   /* Restore AltiVec registers if needed.  */
11945   if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
11946     {
11947       int i;
11948
11949       for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
11950         if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
11951           {
11952             rtx addr, areg, mem;
11953
11954             areg = gen_rtx_REG (Pmode, 0);
11955             emit_move_insn
11956               (areg, GEN_INT (info->altivec_save_offset
11957                               + sp_offset
11958                               + 16 * (i - info->first_altivec_reg_save)));
11959
11960             /* AltiVec addressing mode is [reg+reg].  */
11961             addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
11962             mem = gen_rtx_MEM (V4SImode, addr);
11963             set_mem_alias_set (mem, rs6000_sr_alias_set);
11964
11965             emit_move_insn (gen_rtx_REG (V4SImode, i), mem);
11966           }
11967     }
11968
11969   /* Restore VRSAVE if needed.  */
11970   if (TARGET_ALTIVEC_ABI && info->vrsave_mask != 0)
11971     {
11972       rtx addr, mem, reg;
11973
11974       addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
11975                            GEN_INT (info->vrsave_save_offset + sp_offset));
11976       mem = gen_rtx_MEM (SImode, addr);
11977       set_mem_alias_set (mem, rs6000_sr_alias_set);
11978       reg = gen_rtx_REG (SImode, 12);
11979       emit_move_insn (reg, mem);
11980
11981       emit_insn (generate_set_vrsave (reg, info, 1));
11982     }
11983
11984   /* Get the old lr if we saved it.  */
11985   if (info->lr_save_p)
11986     {
11987       rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx,
11988                                       info->lr_save_offset + sp_offset);
11989
11990       set_mem_alias_set (mem, rs6000_sr_alias_set);
11991
11992       emit_move_insn (gen_rtx_REG (Pmode, 0), mem);
11993     }
11994   
11995   /* Get the old cr if we saved it.  */
11996   if (info->cr_save_p)
11997     {
11998       rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
11999                                GEN_INT (info->cr_save_offset + sp_offset));
12000       rtx mem = gen_rtx_MEM (SImode, addr);
12001
12002       set_mem_alias_set (mem, rs6000_sr_alias_set);
12003
12004       emit_move_insn (gen_rtx_REG (SImode, 12), mem);
12005     }
12006   
12007   /* Set LR here to try to overlap restores below.  */
12008   if (info->lr_save_p)
12009     emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM),
12010                     gen_rtx_REG (Pmode, 0));
12011   
12012   /* Load exception handler data registers, if needed.  */
12013   if (current_function_calls_eh_return)
12014     {
12015       unsigned int i, regno;
12016
12017       if (TARGET_AIX)
12018         {
12019           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12020                                    GEN_INT (sp_offset + 5 * reg_size));
12021           rtx mem = gen_rtx_MEM (reg_mode, addr);
12022
12023           set_mem_alias_set (mem, rs6000_sr_alias_set);
12024
12025           emit_move_insn (gen_rtx_REG (reg_mode, 2), mem);
12026         }
12027
12028       for (i = 0; ; ++i)
12029         {
12030           rtx mem;
12031
12032           regno = EH_RETURN_DATA_REGNO (i);
12033           if (regno == INVALID_REGNUM)
12034             break;
12035
12036           mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
12037                                       info->ehrd_offset + sp_offset
12038                                       + reg_size * (int) i);
12039           set_mem_alias_set (mem, rs6000_sr_alias_set);
12040
12041           emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
12042         }
12043     }
12044   
12045   /* Restore GPRs.  This is done as a PARALLEL if we are using
12046      the load-multiple instructions.  */
12047   if (using_load_multiple)
12048     {
12049       rtvec p;
12050       p = rtvec_alloc (32 - info->first_gp_reg_save);
12051       for (i = 0; i < 32 - info->first_gp_reg_save; i++)
12052         {
12053           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
12054                                    GEN_INT (info->gp_save_offset 
12055                                             + sp_offset 
12056                                             + reg_size * i));
12057           rtx mem = gen_rtx_MEM (reg_mode, addr);
12058
12059           set_mem_alias_set (mem, rs6000_sr_alias_set);
12060
12061           RTVEC_ELT (p, i) = 
12062             gen_rtx_SET (VOIDmode,
12063                          gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
12064                          mem);
12065         }
12066       emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
12067     }
12068   else
12069     for (i = 0; i < 32 - info->first_gp_reg_save; i++)
12070       if ((regs_ever_live[info->first_gp_reg_save+i] 
12071            && ! call_used_regs[info->first_gp_reg_save+i])
12072           || (i+info->first_gp_reg_save == RS6000_PIC_OFFSET_TABLE_REGNUM
12073               && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
12074                   || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
12075         {
12076           rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, 
12077                                    GEN_INT (info->gp_save_offset 
12078                                             + sp_offset 
12079                                             + reg_size * i));
12080           rtx mem = gen_rtx_MEM (reg_mode, addr);
12081
12082           /* Restore 64-bit quantities for SPE.  */
12083           if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
12084             {
12085               int offset = info->spe_gp_save_offset + sp_offset + 8 * i;
12086               rtx b;
12087
12088               if (!SPE_CONST_OFFSET_OK (offset))
12089                 {
12090                   b = gen_rtx_REG (Pmode, FIXED_SCRATCH);
12091                   emit_move_insn (b, GEN_INT (offset));
12092                 }
12093               else
12094                 b = GEN_INT (offset);
12095
12096               addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, b);
12097               mem = gen_rtx_MEM (V2SImode, addr);
12098             }
12099
12100           set_mem_alias_set (mem, rs6000_sr_alias_set);
12101
12102           emit_move_insn (gen_rtx_REG (reg_mode, 
12103                                        info->first_gp_reg_save + i), mem);
12104         }
12105
12106   /* Restore fpr's if we need to do it without calling a function.  */
12107   if (restoring_FPRs_inline)
12108     for (i = 0; i < 64 - info->first_fp_reg_save; i++)
12109       if ((regs_ever_live[info->first_fp_reg_save+i] 
12110            && ! call_used_regs[info->first_fp_reg_save+i]))
12111         {
12112           rtx addr, mem;
12113           addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12114                                GEN_INT (info->fp_save_offset 
12115                                         + sp_offset 
12116                                         + 8 * i));
12117           mem = gen_rtx_MEM (DFmode, addr);
12118           set_mem_alias_set (mem, rs6000_sr_alias_set);
12119
12120           emit_move_insn (gen_rtx_REG (DFmode, 
12121                                        info->first_fp_reg_save + i),
12122                           mem);
12123         }
12124
12125   /* If we saved cr, restore it here.  Just those that were used.  */
12126   if (info->cr_save_p)
12127     {
12128       rtx r12_rtx = gen_rtx_REG (SImode, 12);
12129       int count = 0;
12130       
12131       if (using_mfcr_multiple)
12132         {
12133           for (i = 0; i < 8; i++)
12134             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
12135               count++;
12136           if (count == 0)
12137             abort ();
12138         }
12139
12140       if (using_mfcr_multiple && count > 1)
12141         {
12142           rtvec p;
12143           int ndx;
12144           
12145           p = rtvec_alloc (count);
12146
12147           ndx = 0;
12148           for (i = 0; i < 8; i++)
12149             if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
12150               {
12151                 rtvec r = rtvec_alloc (2);
12152                 RTVEC_ELT (r, 0) = r12_rtx;
12153                 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
12154                 RTVEC_ELT (p, ndx) =
12155                   gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO+i), 
12156                                gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
12157                 ndx++;
12158               }
12159           emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
12160           if (ndx != count)
12161             abort ();
12162         }
12163       else
12164         for (i = 0; i < 8; i++)
12165           if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
12166             {
12167               emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode, 
12168                                                            CR0_REGNO+i),
12169                                               r12_rtx));
12170             }
12171     }
12172
12173   /* If this is V.4, unwind the stack pointer after all of the loads
12174      have been done.  We need to emit a block here so that sched
12175      doesn't decide to move the sp change before the register restores
12176      (which may not have any obvious dependency on the stack).  This
12177      doesn't hurt performance, because there is no scheduling that can
12178      be done after this point.  */
12179   if (DEFAULT_ABI == ABI_V4
12180       || current_function_calls_eh_return)
12181     {
12182       if (frame_reg_rtx != sp_reg_rtx)
12183           rs6000_emit_stack_tie ();
12184
12185       if (use_backchain_to_restore_sp)
12186         {
12187           emit_move_insn (sp_reg_rtx, frame_reg_rtx);
12188         }
12189       else if (sp_offset != 0)
12190         {
12191           emit_insn (TARGET_32BIT
12192                      ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
12193                                    GEN_INT (sp_offset))
12194                      : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
12195                                    GEN_INT (sp_offset)));
12196         }
12197     }
12198
12199   if (current_function_calls_eh_return)
12200     {
12201       rtx sa = EH_RETURN_STACKADJ_RTX;
12202       emit_insn (TARGET_32BIT
12203                  ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx, sa)
12204                  : gen_adddi3 (sp_reg_rtx, sp_reg_rtx, sa));
12205     }
12206
12207   if (!sibcall)
12208     {
12209       rtvec p;
12210       if (! restoring_FPRs_inline)
12211         p = rtvec_alloc (3 + 64 - info->first_fp_reg_save);
12212       else
12213         p = rtvec_alloc (2);
12214
12215       RTVEC_ELT (p, 0) = gen_rtx_RETURN (VOIDmode);
12216       RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode, 
12217                                       gen_rtx_REG (Pmode, 
12218                                                    LINK_REGISTER_REGNUM));
12219
12220       /* If we have to restore more than two FP registers, branch to the
12221          restore function.  It will return to our caller.  */
12222       if (! restoring_FPRs_inline)
12223         {
12224           int i;
12225           char rname[30];
12226           const char *alloc_rname;
12227
12228           sprintf (rname, "%s%d%s", RESTORE_FP_PREFIX, 
12229                    info->first_fp_reg_save - 32, RESTORE_FP_SUFFIX);
12230           alloc_rname = ggc_strdup (rname);
12231           RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode,
12232                                           gen_rtx_SYMBOL_REF (Pmode,
12233                                                               alloc_rname));
12234
12235           for (i = 0; i < 64 - info->first_fp_reg_save; i++)
12236             {
12237               rtx addr, mem;
12238               addr = gen_rtx_PLUS (Pmode, sp_reg_rtx,
12239                                    GEN_INT (info->fp_save_offset + 8*i));
12240               mem = gen_rtx_MEM (DFmode, addr);
12241               set_mem_alias_set (mem, rs6000_sr_alias_set);
12242
12243               RTVEC_ELT (p, i+3) = 
12244                 gen_rtx_SET (VOIDmode,
12245                              gen_rtx_REG (DFmode, info->first_fp_reg_save + i),
12246                              mem);
12247             }
12248         }
12249       
12250       emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
12251     }
12252 }
12253
12254 /* Write function epilogue.  */
12255
12256 static void
12257 rs6000_output_function_epilogue (FILE *file, 
12258                                  HOST_WIDE_INT size ATTRIBUTE_UNUSED)
12259 {
12260   rs6000_stack_t *info = rs6000_stack_info ();
12261
12262   if (! HAVE_epilogue)
12263     {
12264       rtx insn = get_last_insn ();
12265       /* If the last insn was a BARRIER, we don't have to write anything except
12266          the trace table.  */
12267       if (GET_CODE (insn) == NOTE)
12268         insn = prev_nonnote_insn (insn);
12269       if (insn == 0 ||  GET_CODE (insn) != BARRIER)
12270         {
12271           /* This is slightly ugly, but at least we don't have two
12272              copies of the epilogue-emitting code.  */
12273           start_sequence ();
12274
12275           /* A NOTE_INSN_DELETED is supposed to be at the start
12276              and end of the "toplevel" insn chain.  */
12277           emit_note (NOTE_INSN_DELETED);
12278           rs6000_emit_epilogue (FALSE);
12279           emit_note (NOTE_INSN_DELETED);
12280
12281           /* Expand INSN_ADDRESSES so final() doesn't crash. */
12282           {
12283             rtx insn;
12284             unsigned addr = 0;
12285             for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
12286               {
12287                 INSN_ADDRESSES_NEW (insn, addr);
12288                 addr += 4;
12289               }
12290           }
12291
12292           if (TARGET_DEBUG_STACK)
12293             debug_rtx_list (get_insns (), 100);
12294           final (get_insns (), file, FALSE, FALSE);
12295           end_sequence ();
12296         }
12297     }
12298
12299 #if TARGET_OBJECT_FORMAT == OBJECT_MACHO
12300   /* Mach-O doesn't support labels at the end of objects, so if
12301      it looks like we might want one, insert a NOP.  */
12302   {
12303     rtx insn = get_last_insn ();
12304     while (insn
12305            && NOTE_P (insn)
12306            && NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED_LABEL)
12307       insn = PREV_INSN (insn);
12308     if (insn 
12309         && (LABEL_P (insn) 
12310             || (NOTE_P (insn)
12311                 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
12312       fputs ("\tnop\n", file);
12313   }
12314 #endif
12315
12316   /* Output a traceback table here.  See /usr/include/sys/debug.h for info
12317      on its format.
12318
12319      We don't output a traceback table if -finhibit-size-directive was
12320      used.  The documentation for -finhibit-size-directive reads
12321      ``don't output a @code{.size} assembler directive, or anything
12322      else that would cause trouble if the function is split in the
12323      middle, and the two halves are placed at locations far apart in
12324      memory.''  The traceback table has this property, since it
12325      includes the offset from the start of the function to the
12326      traceback table itself.
12327
12328      System V.4 Powerpc's (and the embedded ABI derived from it) use a
12329      different traceback table.  */
12330   if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive
12331       && rs6000_traceback != traceback_none)
12332     {
12333       const char *fname = NULL;
12334       const char *language_string = lang_hooks.name;
12335       int fixed_parms = 0, float_parms = 0, parm_info = 0;
12336       int i;
12337       int optional_tbtab;
12338
12339       if (rs6000_traceback == traceback_full)
12340         optional_tbtab = 1;
12341       else if (rs6000_traceback == traceback_part)
12342         optional_tbtab = 0;
12343       else
12344         optional_tbtab = !optimize_size && !TARGET_ELF;
12345
12346       if (optional_tbtab)
12347         {
12348           fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
12349           while (*fname == '.') /* V.4 encodes . in the name */
12350             fname++;
12351
12352           /* Need label immediately before tbtab, so we can compute
12353              its offset from the function start.  */
12354           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
12355           ASM_OUTPUT_LABEL (file, fname);
12356         }
12357
12358       /* The .tbtab pseudo-op can only be used for the first eight
12359          expressions, since it can't handle the possibly variable
12360          length fields that follow.  However, if you omit the optional
12361          fields, the assembler outputs zeros for all optional fields
12362          anyways, giving each variable length field is minimum length
12363          (as defined in sys/debug.h).  Thus we can not use the .tbtab
12364          pseudo-op at all.  */
12365
12366       /* An all-zero word flags the start of the tbtab, for debuggers
12367          that have to find it by searching forward from the entry
12368          point or from the current pc.  */
12369       fputs ("\t.long 0\n", file);
12370
12371       /* Tbtab format type.  Use format type 0.  */
12372       fputs ("\t.byte 0,", file);
12373
12374       /* Language type.  Unfortunately, there doesn't seem to be any
12375          official way to get this info, so we use language_string.  C
12376          is 0.  C++ is 9.  No number defined for Obj-C, so use the
12377          value for C for now.  There is no official value for Java,
12378          although IBM appears to be using 13.  There is no official value
12379          for Chill, so we've chosen 44 pseudo-randomly.  */
12380       if (! strcmp (language_string, "GNU C")
12381           || ! strcmp (language_string, "GNU Objective-C"))
12382         i = 0;
12383       else if (! strcmp (language_string, "GNU F77"))
12384         i = 1;
12385       else if (! strcmp (language_string, "GNU Ada"))
12386         i = 3;
12387       else if (! strcmp (language_string, "GNU Pascal"))
12388         i = 2;
12389       else if (! strcmp (language_string, "GNU C++"))
12390         i = 9;
12391       else if (! strcmp (language_string, "GNU Java"))
12392         i = 13;
12393       else if (! strcmp (language_string, "GNU CHILL"))
12394         i = 44;
12395       else
12396         abort ();
12397       fprintf (file, "%d,", i);
12398
12399       /* 8 single bit fields: global linkage (not set for C extern linkage,
12400          apparently a PL/I convention?), out-of-line epilogue/prologue, offset
12401          from start of procedure stored in tbtab, internal function, function
12402          has controlled storage, function has no toc, function uses fp,
12403          function logs/aborts fp operations.  */
12404       /* Assume that fp operations are used if any fp reg must be saved.  */
12405       fprintf (file, "%d,",
12406                (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
12407
12408       /* 6 bitfields: function is interrupt handler, name present in
12409          proc table, function calls alloca, on condition directives
12410          (controls stack walks, 3 bits), saves condition reg, saves
12411          link reg.  */
12412       /* The `function calls alloca' bit seems to be set whenever reg 31 is
12413          set up as a frame pointer, even when there is no alloca call.  */
12414       fprintf (file, "%d,",
12415                ((optional_tbtab << 6)
12416                 | ((optional_tbtab & frame_pointer_needed) << 5)
12417                 | (info->cr_save_p << 1)
12418                 | (info->lr_save_p)));
12419
12420       /* 3 bitfields: saves backchain, fixup code, number of fpr saved
12421          (6 bits).  */
12422       fprintf (file, "%d,",
12423                (info->push_p << 7) | (64 - info->first_fp_reg_save));
12424
12425       /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits).  */
12426       fprintf (file, "%d,", (32 - first_reg_to_save ()));
12427
12428       if (optional_tbtab)
12429         {
12430           /* Compute the parameter info from the function decl argument
12431              list.  */
12432           tree decl;
12433           int next_parm_info_bit = 31;
12434
12435           for (decl = DECL_ARGUMENTS (current_function_decl);
12436                decl; decl = TREE_CHAIN (decl))
12437             {
12438               rtx parameter = DECL_INCOMING_RTL (decl);
12439               enum machine_mode mode = GET_MODE (parameter);
12440
12441               if (GET_CODE (parameter) == REG)
12442                 {
12443                   if (GET_MODE_CLASS (mode) == MODE_FLOAT)
12444                     {
12445                       int bits;
12446
12447                       float_parms++;
12448
12449                       if (mode == SFmode)
12450                         bits = 0x2;
12451                       else if (mode == DFmode || mode == TFmode)
12452                         bits = 0x3;
12453                       else
12454                         abort ();
12455
12456                       /* If only one bit will fit, don't or in this entry.  */
12457                       if (next_parm_info_bit > 0)
12458                         parm_info |= (bits << (next_parm_info_bit - 1));
12459                       next_parm_info_bit -= 2;
12460                     }
12461                   else
12462                     {
12463                       fixed_parms += ((GET_MODE_SIZE (mode)
12464                                        + (UNITS_PER_WORD - 1))
12465                                       / UNITS_PER_WORD);
12466                       next_parm_info_bit -= 1;
12467                     }
12468                 }
12469             }
12470         }
12471
12472       /* Number of fixed point parameters.  */
12473       /* This is actually the number of words of fixed point parameters; thus
12474          an 8 byte struct counts as 2; and thus the maximum value is 8.  */
12475       fprintf (file, "%d,", fixed_parms);
12476
12477       /* 2 bitfields: number of floating point parameters (7 bits), parameters
12478          all on stack.  */
12479       /* This is actually the number of fp registers that hold parameters;
12480          and thus the maximum value is 13.  */
12481       /* Set parameters on stack bit if parameters are not in their original
12482          registers, regardless of whether they are on the stack?  Xlc
12483          seems to set the bit when not optimizing.  */
12484       fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
12485
12486       if (! optional_tbtab)
12487         return;
12488
12489       /* Optional fields follow.  Some are variable length.  */
12490
12491       /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
12492          11 double float.  */
12493       /* There is an entry for each parameter in a register, in the order that
12494          they occur in the parameter list.  Any intervening arguments on the
12495          stack are ignored.  If the list overflows a long (max possible length
12496          34 bits) then completely leave off all elements that don't fit.  */
12497       /* Only emit this long if there was at least one parameter.  */
12498       if (fixed_parms || float_parms)
12499         fprintf (file, "\t.long %d\n", parm_info);
12500
12501       /* Offset from start of code to tb table.  */
12502       fputs ("\t.long ", file);
12503       ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
12504 #if TARGET_AIX
12505       RS6000_OUTPUT_BASENAME (file, fname);
12506 #else
12507       assemble_name (file, fname);
12508 #endif
12509       fputs ("-.", file);
12510 #if TARGET_AIX
12511       RS6000_OUTPUT_BASENAME (file, fname);
12512 #else
12513       assemble_name (file, fname);
12514 #endif
12515       putc ('\n', file);
12516
12517       /* Interrupt handler mask.  */
12518       /* Omit this long, since we never set the interrupt handler bit
12519          above.  */
12520
12521       /* Number of CTL (controlled storage) anchors.  */
12522       /* Omit this long, since the has_ctl bit is never set above.  */
12523
12524       /* Displacement into stack of each CTL anchor.  */
12525       /* Omit this list of longs, because there are no CTL anchors.  */
12526
12527       /* Length of function name.  */
12528       if (*fname == '*')
12529         ++fname;
12530       fprintf (file, "\t.short %d\n", (int) strlen (fname));
12531
12532       /* Function name.  */
12533       assemble_string (fname, strlen (fname));
12534
12535       /* Register for alloca automatic storage; this is always reg 31.
12536          Only emit this if the alloca bit was set above.  */
12537       if (frame_pointer_needed)
12538         fputs ("\t.byte 31\n", file);
12539
12540       fputs ("\t.align 2\n", file);
12541     }
12542 }
12543 \f
12544 /* A C compound statement that outputs the assembler code for a thunk
12545    function, used to implement C++ virtual function calls with
12546    multiple inheritance.  The thunk acts as a wrapper around a virtual
12547    function, adjusting the implicit object parameter before handing
12548    control off to the real function.
12549
12550    First, emit code to add the integer DELTA to the location that
12551    contains the incoming first argument.  Assume that this argument
12552    contains a pointer, and is the one used to pass the `this' pointer
12553    in C++.  This is the incoming argument *before* the function
12554    prologue, e.g. `%o0' on a sparc.  The addition must preserve the
12555    values of all other incoming arguments.
12556
12557    After the addition, emit code to jump to FUNCTION, which is a
12558    `FUNCTION_DECL'.  This is a direct pure jump, not a call, and does
12559    not touch the return address.  Hence returning from FUNCTION will
12560    return to whoever called the current `thunk'.
12561
12562    The effect must be as if FUNCTION had been called directly with the
12563    adjusted first argument.  This macro is responsible for emitting
12564    all of the code for a thunk function; output_function_prologue()
12565    and output_function_epilogue() are not invoked.
12566
12567    The THUNK_FNDECL is redundant.  (DELTA and FUNCTION have already
12568    been extracted from it.)  It might possibly be useful on some
12569    targets, but probably not.
12570
12571    If you do not define this macro, the target-independent code in the
12572    C++ frontend will generate a less efficient heavyweight thunk that
12573    calls FUNCTION instead of jumping to it.  The generic approach does
12574    not support varargs.  */
12575
12576 static void
12577 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED, 
12578                         HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset, 
12579                         tree function)
12580 {
12581   rtx this, insn, funexp;
12582
12583   reload_completed = 1;
12584   epilogue_completed = 1;
12585   no_new_pseudos = 1;
12586
12587   /* Mark the end of the (empty) prologue.  */
12588   emit_note (NOTE_INSN_PROLOGUE_END);
12589
12590   /* Find the "this" pointer.  If the function returns a structure,
12591      the structure return pointer is in r3.  */
12592   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
12593     this = gen_rtx_REG (Pmode, 4);
12594   else
12595     this = gen_rtx_REG (Pmode, 3);
12596
12597   /* Apply the constant offset, if required.  */
12598   if (delta)
12599     {
12600       rtx delta_rtx = GEN_INT (delta);
12601       emit_insn (TARGET_32BIT
12602                  ? gen_addsi3 (this, this, delta_rtx)
12603                  : gen_adddi3 (this, this, delta_rtx));
12604     }
12605
12606   /* Apply the offset from the vtable, if required.  */
12607   if (vcall_offset)
12608     {
12609       rtx vcall_offset_rtx = GEN_INT (vcall_offset);
12610       rtx tmp = gen_rtx_REG (Pmode, 12);
12611
12612       emit_move_insn (tmp, gen_rtx_MEM (Pmode, this));
12613       if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
12614         {
12615           emit_insn (TARGET_32BIT
12616                      ? gen_addsi3 (tmp, tmp, vcall_offset_rtx)
12617                      : gen_adddi3 (tmp, tmp, vcall_offset_rtx));
12618           emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
12619         }
12620       else
12621         {
12622           rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
12623
12624           emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
12625         }
12626       emit_insn (TARGET_32BIT
12627                  ? gen_addsi3 (this, this, tmp)
12628                  : gen_adddi3 (this, this, tmp));
12629     }
12630
12631   /* Generate a tail call to the target function.  */
12632   if (!TREE_USED (function))
12633     {
12634       assemble_external (function);
12635       TREE_USED (function) = 1;
12636     }
12637   funexp = XEXP (DECL_RTL (function), 0);
12638   funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
12639
12640 #if TARGET_MACHO
12641   if (MACHOPIC_INDIRECT)
12642     funexp = machopic_indirect_call_target (funexp);
12643 #endif
12644
12645   /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
12646      generate sibcall RTL explicitly to avoid constraint abort.  */
12647   insn = emit_call_insn (
12648            gen_rtx_PARALLEL (VOIDmode,
12649              gen_rtvec (4,
12650                         gen_rtx_CALL (VOIDmode,
12651                                       funexp, const0_rtx),
12652                         gen_rtx_USE (VOIDmode, const0_rtx),
12653                         gen_rtx_USE (VOIDmode,
12654                                      gen_rtx_REG (SImode,
12655                                                   LINK_REGISTER_REGNUM)),
12656                         gen_rtx_RETURN (VOIDmode))));
12657   SIBLING_CALL_P (insn) = 1;
12658   emit_barrier ();
12659
12660   /* Run just enough of rest_of_compilation to get the insns emitted.
12661      There's not really enough bulk here to make other passes such as
12662      instruction scheduling worth while.  Note that use_thunk calls
12663      assemble_start_function and assemble_end_function.  */
12664   insn = get_insns ();
12665   insn_locators_initialize ();
12666   shorten_branches (insn);
12667   final_start_function (insn, file, 1);
12668   final (insn, file, 1, 0);
12669   final_end_function ();
12670
12671   reload_completed = 0;
12672   epilogue_completed = 0;
12673   no_new_pseudos = 0;
12674 }
12675 \f
12676 /* A quick summary of the various types of 'constant-pool tables'
12677    under PowerPC:
12678
12679    Target       Flags           Name            One table per   
12680    AIX          (none)          AIX TOC         object file
12681    AIX          -mfull-toc      AIX TOC         object file
12682    AIX          -mminimal-toc   AIX minimal TOC translation unit
12683    SVR4/EABI    (none)          SVR4 SDATA      object file
12684    SVR4/EABI    -fpic           SVR4 pic        object file
12685    SVR4/EABI    -fPIC           SVR4 PIC        translation unit
12686    SVR4/EABI    -mrelocatable   EABI TOC        function
12687    SVR4/EABI    -maix           AIX TOC         object file
12688    SVR4/EABI    -maix -mminimal-toc 
12689                                 AIX minimal TOC translation unit
12690
12691    Name                 Reg.    Set by  entries       contains:
12692                                         made by  addrs? fp?     sum?
12693
12694    AIX TOC              2       crt0    as       Y      option  option
12695    AIX minimal TOC      30      prolog  gcc      Y      Y       option
12696    SVR4 SDATA           13      crt0    gcc      N      Y       N
12697    SVR4 pic             30      prolog  ld       Y      not yet N
12698    SVR4 PIC             30      prolog  gcc      Y      option  option
12699    EABI TOC             30      prolog  gcc      Y      option  option
12700
12701 */
12702
12703 /* Hash functions for the hash table.  */
12704
12705 static unsigned
12706 rs6000_hash_constant (rtx k)
12707 {
12708   enum rtx_code code = GET_CODE (k);
12709   enum machine_mode mode = GET_MODE (k);
12710   unsigned result = (code << 3) ^ mode;
12711   const char *format;
12712   int flen, fidx;
12713   
12714   format = GET_RTX_FORMAT (code);
12715   flen = strlen (format);
12716   fidx = 0;
12717
12718   switch (code)
12719     {
12720     case LABEL_REF:
12721       return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
12722
12723     case CONST_DOUBLE:
12724       if (mode != VOIDmode)
12725         return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
12726       flen = 2;
12727       break;
12728
12729     case CODE_LABEL:
12730       fidx = 3;
12731       break;
12732
12733     default:
12734       break;
12735     }
12736
12737   for (; fidx < flen; fidx++)
12738     switch (format[fidx])
12739       {
12740       case 's':
12741         {
12742           unsigned i, len;
12743           const char *str = XSTR (k, fidx);
12744           len = strlen (str);
12745           result = result * 613 + len;
12746           for (i = 0; i < len; i++)
12747             result = result * 613 + (unsigned) str[i];
12748           break;
12749         }
12750       case 'u':
12751       case 'e':
12752         result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
12753         break;
12754       case 'i':
12755       case 'n':
12756         result = result * 613 + (unsigned) XINT (k, fidx);
12757         break;
12758       case 'w':
12759         if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
12760           result = result * 613 + (unsigned) XWINT (k, fidx);
12761         else
12762           {
12763             size_t i;
12764             for (i = 0; i < sizeof(HOST_WIDE_INT)/sizeof(unsigned); i++)
12765               result = result * 613 + (unsigned) (XWINT (k, fidx)
12766                                                   >> CHAR_BIT * i);
12767           }
12768         break;
12769       case '0':
12770         break;
12771       default:
12772         abort ();
12773       }
12774
12775   return result;
12776 }
12777
12778 static unsigned
12779 toc_hash_function (const void *hash_entry)
12780 {
12781   const struct toc_hash_struct *thc = 
12782     (const struct toc_hash_struct *) hash_entry;
12783   return rs6000_hash_constant (thc->key) ^ thc->key_mode;
12784 }
12785
12786 /* Compare H1 and H2 for equivalence.  */
12787
12788 static int
12789 toc_hash_eq (const void *h1, const void *h2)
12790 {
12791   rtx r1 = ((const struct toc_hash_struct *) h1)->key;
12792   rtx r2 = ((const struct toc_hash_struct *) h2)->key;
12793
12794   if (((const struct toc_hash_struct *) h1)->key_mode
12795       != ((const struct toc_hash_struct *) h2)->key_mode)
12796     return 0;
12797
12798   return rtx_equal_p (r1, r2);
12799 }
12800
12801 /* These are the names given by the C++ front-end to vtables, and
12802    vtable-like objects.  Ideally, this logic should not be here;
12803    instead, there should be some programmatic way of inquiring as
12804    to whether or not an object is a vtable.  */
12805
12806 #define VTABLE_NAME_P(NAME)                             \
12807   (strncmp ("_vt.", name, strlen("_vt.")) == 0          \
12808   || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0       \
12809   || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0       \
12810   || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0) 
12811
12812 void
12813 rs6000_output_symbol_ref (FILE *file, rtx x)
12814 {
12815   /* Currently C++ toc references to vtables can be emitted before it
12816      is decided whether the vtable is public or private.  If this is
12817      the case, then the linker will eventually complain that there is
12818      a reference to an unknown section.  Thus, for vtables only, 
12819      we emit the TOC reference to reference the symbol and not the
12820      section.  */
12821   const char *name = XSTR (x, 0);
12822
12823   if (VTABLE_NAME_P (name)) 
12824     {
12825       RS6000_OUTPUT_BASENAME (file, name);
12826     }
12827   else
12828     assemble_name (file, name);
12829 }
12830
12831 /* Output a TOC entry.  We derive the entry name from what is being
12832    written.  */
12833
12834 void
12835 output_toc (FILE *file, rtx x, int labelno, enum machine_mode mode)
12836 {
12837   char buf[256];
12838   const char *name = buf;
12839   const char *real_name;
12840   rtx base = x;
12841   int offset = 0;
12842
12843   if (TARGET_NO_TOC)
12844     abort ();
12845
12846   /* When the linker won't eliminate them, don't output duplicate
12847      TOC entries (this happens on AIX if there is any kind of TOC,
12848      and on SVR4 under -fPIC or -mrelocatable).  Don't do this for
12849      CODE_LABELs.  */
12850   if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
12851     {
12852       struct toc_hash_struct *h;
12853       void * * found;
12854       
12855       /* Create toc_hash_table.  This can't be done at OVERRIDE_OPTIONS
12856          time because GGC is not initialized at that point.  */
12857       if (toc_hash_table == NULL)
12858         toc_hash_table = htab_create_ggc (1021, toc_hash_function, 
12859                                           toc_hash_eq, NULL);
12860
12861       h = ggc_alloc (sizeof (*h));
12862       h->key = x;
12863       h->key_mode = mode;
12864       h->labelno = labelno;
12865       
12866       found = htab_find_slot (toc_hash_table, h, 1);
12867       if (*found == NULL)
12868         *found = h;
12869       else  /* This is indeed a duplicate.  
12870                Set this label equal to that label.  */
12871         {
12872           fputs ("\t.set ", file);
12873           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
12874           fprintf (file, "%d,", labelno);
12875           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
12876           fprintf (file, "%d\n", ((*(const struct toc_hash_struct **) 
12877                                               found)->labelno));
12878           return;
12879         }
12880     }
12881
12882   /* If we're going to put a double constant in the TOC, make sure it's
12883      aligned properly when strict alignment is on.  */
12884   if (GET_CODE (x) == CONST_DOUBLE
12885       && STRICT_ALIGNMENT
12886       && GET_MODE_BITSIZE (mode) >= 64
12887       && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
12888     ASM_OUTPUT_ALIGN (file, 3);
12889   }
12890
12891   (*targetm.asm_out.internal_label) (file, "LC", labelno);
12892
12893   /* Handle FP constants specially.  Note that if we have a minimal
12894      TOC, things we put here aren't actually in the TOC, so we can allow
12895      FP constants.  */
12896   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == TFmode)
12897     {
12898       REAL_VALUE_TYPE rv;
12899       long k[4];
12900
12901       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
12902       REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
12903
12904       if (TARGET_64BIT)
12905         {
12906           if (TARGET_MINIMAL_TOC)
12907             fputs (DOUBLE_INT_ASM_OP, file);
12908           else
12909             fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
12910                      k[0] & 0xffffffff, k[1] & 0xffffffff,
12911                      k[2] & 0xffffffff, k[3] & 0xffffffff);
12912           fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
12913                    k[0] & 0xffffffff, k[1] & 0xffffffff,
12914                    k[2] & 0xffffffff, k[3] & 0xffffffff);
12915           return;
12916         }
12917       else
12918         {
12919           if (TARGET_MINIMAL_TOC)
12920             fputs ("\t.long ", file);
12921           else
12922             fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
12923                      k[0] & 0xffffffff, k[1] & 0xffffffff,
12924                      k[2] & 0xffffffff, k[3] & 0xffffffff);
12925           fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
12926                    k[0] & 0xffffffff, k[1] & 0xffffffff,
12927                    k[2] & 0xffffffff, k[3] & 0xffffffff);
12928           return;
12929         }
12930     }
12931   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
12932     {
12933       REAL_VALUE_TYPE rv;
12934       long k[2];
12935
12936       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
12937       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
12938
12939       if (TARGET_64BIT)
12940         {
12941           if (TARGET_MINIMAL_TOC)
12942             fputs (DOUBLE_INT_ASM_OP, file);
12943           else
12944             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
12945                      k[0] & 0xffffffff, k[1] & 0xffffffff);
12946           fprintf (file, "0x%lx%08lx\n",
12947                    k[0] & 0xffffffff, k[1] & 0xffffffff);
12948           return;
12949         }
12950       else
12951         {
12952           if (TARGET_MINIMAL_TOC)
12953             fputs ("\t.long ", file);
12954           else
12955             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
12956                      k[0] & 0xffffffff, k[1] & 0xffffffff);
12957           fprintf (file, "0x%lx,0x%lx\n",
12958                    k[0] & 0xffffffff, k[1] & 0xffffffff);
12959           return;
12960         }
12961     }
12962   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
12963     {
12964       REAL_VALUE_TYPE rv;
12965       long l;
12966
12967       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
12968       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
12969
12970       if (TARGET_64BIT)
12971         {
12972           if (TARGET_MINIMAL_TOC)
12973             fputs (DOUBLE_INT_ASM_OP, file);
12974           else
12975             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
12976           fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
12977           return;
12978         }
12979       else
12980         {
12981           if (TARGET_MINIMAL_TOC)
12982             fputs ("\t.long ", file);
12983           else
12984             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
12985           fprintf (file, "0x%lx\n", l & 0xffffffff);
12986           return;
12987         }
12988     }
12989   else if (GET_MODE (x) == VOIDmode
12990            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
12991     {
12992       unsigned HOST_WIDE_INT low;
12993       HOST_WIDE_INT high;
12994
12995       if (GET_CODE (x) == CONST_DOUBLE)
12996         {
12997           low = CONST_DOUBLE_LOW (x);
12998           high = CONST_DOUBLE_HIGH (x);
12999         }
13000       else
13001 #if HOST_BITS_PER_WIDE_INT == 32
13002         {
13003           low = INTVAL (x);
13004           high = (low & 0x80000000) ? ~0 : 0;
13005         }
13006 #else
13007         {
13008           low = INTVAL (x) & 0xffffffff;
13009           high = (HOST_WIDE_INT) INTVAL (x) >> 32;
13010         }
13011 #endif
13012
13013       /* TOC entries are always Pmode-sized, but since this
13014          is a bigendian machine then if we're putting smaller
13015          integer constants in the TOC we have to pad them.
13016          (This is still a win over putting the constants in
13017          a separate constant pool, because then we'd have
13018          to have both a TOC entry _and_ the actual constant.)
13019
13020          For a 32-bit target, CONST_INT values are loaded and shifted
13021          entirely within `low' and can be stored in one TOC entry.  */
13022
13023       if (TARGET_64BIT && POINTER_SIZE < GET_MODE_BITSIZE (mode))
13024         abort ();/* It would be easy to make this work, but it doesn't now.  */
13025
13026       if (POINTER_SIZE > GET_MODE_BITSIZE (mode))
13027         {
13028 #if HOST_BITS_PER_WIDE_INT == 32
13029           lshift_double (low, high, POINTER_SIZE - GET_MODE_BITSIZE (mode),
13030                          POINTER_SIZE, &low, &high, 0);
13031 #else
13032           low |= high << 32;
13033           low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
13034           high = (HOST_WIDE_INT) low >> 32;
13035           low &= 0xffffffff;
13036 #endif
13037         }
13038
13039       if (TARGET_64BIT)
13040         {
13041           if (TARGET_MINIMAL_TOC)
13042             fputs (DOUBLE_INT_ASM_OP, file);
13043           else
13044             fprintf (file, "\t.tc ID_%lx_%lx[TC],",
13045                      (long) high & 0xffffffff, (long) low & 0xffffffff);
13046           fprintf (file, "0x%lx%08lx\n",
13047                    (long) high & 0xffffffff, (long) low & 0xffffffff);
13048           return;
13049         }
13050       else
13051         {
13052           if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
13053             {
13054               if (TARGET_MINIMAL_TOC)
13055                 fputs ("\t.long ", file);
13056               else
13057                 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
13058                          (long) high & 0xffffffff, (long) low & 0xffffffff);
13059               fprintf (file, "0x%lx,0x%lx\n",
13060                        (long) high & 0xffffffff, (long) low & 0xffffffff);
13061             }
13062           else
13063             {
13064               if (TARGET_MINIMAL_TOC)
13065                 fputs ("\t.long ", file);
13066               else
13067                 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
13068               fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
13069             }
13070           return;
13071         }
13072     }
13073
13074   if (GET_CODE (x) == CONST)
13075     {
13076       if (GET_CODE (XEXP (x, 0)) != PLUS)
13077         abort ();
13078
13079       base = XEXP (XEXP (x, 0), 0);
13080       offset = INTVAL (XEXP (XEXP (x, 0), 1));
13081     }
13082   
13083   if (GET_CODE (base) == SYMBOL_REF)
13084     name = XSTR (base, 0);
13085   else if (GET_CODE (base) == LABEL_REF)
13086     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (XEXP (base, 0)));
13087   else if (GET_CODE (base) == CODE_LABEL)
13088     ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
13089   else
13090     abort ();
13091
13092   real_name = (*targetm.strip_name_encoding) (name);
13093   if (TARGET_MINIMAL_TOC)
13094     fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
13095   else
13096     {
13097       fprintf (file, "\t.tc %s", real_name);
13098
13099       if (offset < 0)
13100         fprintf (file, ".N%d", - offset);
13101       else if (offset)
13102         fprintf (file, ".P%d", offset);
13103
13104       fputs ("[TC],", file);
13105     }
13106
13107   /* Currently C++ toc references to vtables can be emitted before it
13108      is decided whether the vtable is public or private.  If this is
13109      the case, then the linker will eventually complain that there is
13110      a TOC reference to an unknown section.  Thus, for vtables only,
13111      we emit the TOC reference to reference the symbol and not the
13112      section.  */
13113   if (VTABLE_NAME_P (name))
13114     {
13115       RS6000_OUTPUT_BASENAME (file, name);
13116       if (offset < 0)
13117         fprintf (file, "%d", offset);
13118       else if (offset > 0)
13119         fprintf (file, "+%d", offset);
13120     }
13121   else
13122     output_addr_const (file, x);
13123   putc ('\n', file);
13124 }
13125 \f
13126 /* Output an assembler pseudo-op to write an ASCII string of N characters
13127    starting at P to FILE.
13128
13129    On the RS/6000, we have to do this using the .byte operation and
13130    write out special characters outside the quoted string.
13131    Also, the assembler is broken; very long strings are truncated,
13132    so we must artificially break them up early.  */
13133
13134 void
13135 output_ascii (FILE *file, const char *p, int n)
13136 {
13137   char c;
13138   int i, count_string;
13139   const char *for_string = "\t.byte \"";
13140   const char *for_decimal = "\t.byte ";
13141   const char *to_close = NULL;
13142
13143   count_string = 0;
13144   for (i = 0; i < n; i++)
13145     {
13146       c = *p++;
13147       if (c >= ' ' && c < 0177)
13148         {
13149           if (for_string)
13150             fputs (for_string, file);
13151           putc (c, file);
13152
13153           /* Write two quotes to get one.  */
13154           if (c == '"')
13155             {
13156               putc (c, file);
13157               ++count_string;
13158             }
13159
13160           for_string = NULL;
13161           for_decimal = "\"\n\t.byte ";
13162           to_close = "\"\n";
13163           ++count_string;
13164
13165           if (count_string >= 512)
13166             {
13167               fputs (to_close, file);
13168
13169               for_string = "\t.byte \"";
13170               for_decimal = "\t.byte ";
13171               to_close = NULL;
13172               count_string = 0;
13173             }
13174         }
13175       else
13176         {
13177           if (for_decimal)
13178             fputs (for_decimal, file);
13179           fprintf (file, "%d", c);
13180
13181           for_string = "\n\t.byte \"";
13182           for_decimal = ", ";
13183           to_close = "\n";
13184           count_string = 0;
13185         }
13186     }
13187
13188   /* Now close the string if we have written one.  Then end the line.  */
13189   if (to_close)
13190     fputs (to_close, file);
13191 }
13192 \f
13193 /* Generate a unique section name for FILENAME for a section type
13194    represented by SECTION_DESC.  Output goes into BUF.
13195
13196    SECTION_DESC can be any string, as long as it is different for each
13197    possible section type.
13198
13199    We name the section in the same manner as xlc.  The name begins with an
13200    underscore followed by the filename (after stripping any leading directory
13201    names) with the last period replaced by the string SECTION_DESC.  If
13202    FILENAME does not contain a period, SECTION_DESC is appended to the end of
13203    the name.  */
13204
13205 void
13206 rs6000_gen_section_name (char **buf, const char *filename, 
13207                          const char *section_desc)
13208 {
13209   const char *q, *after_last_slash, *last_period = 0;
13210   char *p;
13211   int len;
13212
13213   after_last_slash = filename;
13214   for (q = filename; *q; q++)
13215     {
13216       if (*q == '/')
13217         after_last_slash = q + 1;
13218       else if (*q == '.')
13219         last_period = q;
13220     }
13221
13222   len = strlen (after_last_slash) + strlen (section_desc) + 2;
13223   *buf = (char *) xmalloc (len);
13224
13225   p = *buf;
13226   *p++ = '_';
13227
13228   for (q = after_last_slash; *q; q++)
13229     {
13230       if (q == last_period)
13231         {
13232           strcpy (p, section_desc);
13233           p += strlen (section_desc);
13234           break;
13235         }
13236
13237       else if (ISALNUM (*q))
13238         *p++ = *q;
13239     }
13240
13241   if (last_period == 0)
13242     strcpy (p, section_desc);
13243   else
13244     *p = '\0';
13245 }
13246 \f
13247 /* Emit profile function.  */
13248
13249 void
13250 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
13251 {
13252   if (TARGET_PROFILE_KERNEL)
13253     return;
13254
13255   if (DEFAULT_ABI == ABI_AIX)
13256     {
13257 #ifndef NO_PROFILE_COUNTERS
13258 # define NO_PROFILE_COUNTERS 0
13259 #endif
13260       if (NO_PROFILE_COUNTERS)  
13261         emit_library_call (init_one_libfunc (RS6000_MCOUNT), 0, VOIDmode, 0);
13262       else
13263         {
13264           char buf[30];
13265           const char *label_name;
13266           rtx fun;
13267
13268           ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
13269           label_name = (*targetm.strip_name_encoding) (ggc_strdup (buf));
13270           fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
13271
13272           emit_library_call (init_one_libfunc (RS6000_MCOUNT), 0, VOIDmode, 1,
13273                              fun, Pmode);
13274         }
13275     }
13276   else if (DEFAULT_ABI == ABI_DARWIN)
13277     {
13278       const char *mcount_name = RS6000_MCOUNT;
13279       int caller_addr_regno = LINK_REGISTER_REGNUM;
13280
13281       /* Be conservative and always set this, at least for now.  */
13282       current_function_uses_pic_offset_table = 1;
13283
13284 #if TARGET_MACHO
13285       /* For PIC code, set up a stub and collect the caller's address
13286          from r0, which is where the prologue puts it.  */
13287       if (MACHOPIC_INDIRECT)
13288         {
13289           mcount_name = machopic_stub_name (mcount_name);
13290           if (current_function_uses_pic_offset_table)
13291             caller_addr_regno = 0;
13292         }
13293 #endif
13294       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
13295                          0, VOIDmode, 1,
13296                          gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
13297     }
13298 }
13299
13300 /* Write function profiler code.  */
13301
13302 void
13303 output_function_profiler (FILE *file, int labelno)
13304 {
13305   char buf[100];
13306   int save_lr = 8;
13307
13308   switch (DEFAULT_ABI)
13309     {
13310     default:
13311       abort ();
13312
13313     case ABI_V4:
13314       save_lr = 4;
13315       if (!TARGET_32BIT)
13316         {
13317           warning ("no profiling of 64-bit code for this ABI");
13318           return;
13319         }
13320       ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
13321       fprintf (file, "\tmflr %s\n", reg_names[0]);
13322       if (flag_pic == 1)
13323         {
13324           fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
13325           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
13326                        reg_names[0], save_lr, reg_names[1]);
13327           asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
13328           asm_fprintf (file, "\t{l|lwz} %s,", reg_names[0]);
13329           assemble_name (file, buf);
13330           asm_fprintf (file, "@got(%s)\n", reg_names[12]);
13331         }
13332       else if (flag_pic > 1)
13333         {
13334           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
13335                        reg_names[0], save_lr, reg_names[1]);
13336           /* Now, we need to get the address of the label.  */
13337           fputs ("\tbl 1f\n\t.long ", file);
13338           assemble_name (file, buf);
13339           fputs ("-.\n1:", file);
13340           asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
13341           asm_fprintf (file, "\t{l|lwz} %s,0(%s)\n", 
13342                        reg_names[0], reg_names[11]);
13343           asm_fprintf (file, "\t{cax|add} %s,%s,%s\n",
13344                        reg_names[0], reg_names[0], reg_names[11]);
13345         }
13346       else
13347         {
13348           asm_fprintf (file, "\t{liu|lis} %s,", reg_names[12]);
13349           assemble_name (file, buf);
13350           fputs ("@ha\n", file);
13351           asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
13352                        reg_names[0], save_lr, reg_names[1]);
13353           asm_fprintf (file, "\t{cal|la} %s,", reg_names[0]);
13354           assemble_name (file, buf);
13355           asm_fprintf (file, "@l(%s)\n", reg_names[12]);
13356         }
13357
13358       /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH.  */
13359       fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
13360       break;
13361
13362     case ABI_AIX:
13363     case ABI_DARWIN:
13364       if (!TARGET_PROFILE_KERNEL)
13365         {
13366           /* Don't do anything, done in output_profile_hook (). */
13367         }
13368       else
13369         {
13370           if (TARGET_32BIT)
13371             abort ();
13372
13373           asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
13374           asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
13375
13376           if (current_function_needs_context)
13377             {
13378               asm_fprintf (file, "\tstd %s,24(%s)\n",
13379                            reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
13380               fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
13381               asm_fprintf (file, "\tld %s,24(%s)\n",
13382                            reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
13383             }
13384           else
13385             fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
13386         }
13387       break;
13388     }
13389 }
13390
13391 \f
13392 static int
13393 rs6000_use_dfa_pipeline_interface (void)
13394 {
13395   return 1;
13396 }
13397
13398 /* Power4 load update and store update instructions are cracked into a
13399    load or store and an integer insn which are executed in the same cycle.
13400    Branches have their own dispatch slot which does not count against the
13401    GCC issue rate, but it changes the program flow so there are no other
13402    instructions to issue in this cycle.  */
13403
13404 static int
13405 rs6000_variable_issue (FILE *stream ATTRIBUTE_UNUSED, 
13406                        int verbose ATTRIBUTE_UNUSED, 
13407                        rtx insn, int more)
13408 {
13409   if (GET_CODE (PATTERN (insn)) == USE
13410       || GET_CODE (PATTERN (insn)) == CLOBBER)
13411     return more;
13412
13413   if (rs6000_cpu == PROCESSOR_POWER4)
13414     {
13415       if (is_microcoded_insn (insn))
13416         return 0;
13417       else if (is_cracked_insn (insn))
13418         return more > 2 ? more - 2 : 0;
13419     }
13420
13421   return more - 1;
13422 }
13423
13424 /* Adjust the cost of a scheduling dependency.  Return the new cost of
13425    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
13426
13427 static int
13428 rs6000_adjust_cost (rtx insn, rtx link, rtx dep_insn ATTRIBUTE_UNUSED, 
13429                     int cost)
13430 {
13431   if (! recog_memoized (insn))
13432     return 0;
13433
13434   if (REG_NOTE_KIND (link) != 0)
13435     return 0;
13436
13437   if (REG_NOTE_KIND (link) == 0)
13438     {
13439       /* Data dependency; DEP_INSN writes a register that INSN reads
13440          some cycles later.  */
13441       switch (get_attr_type (insn))
13442         {
13443         case TYPE_JMPREG:
13444           /* Tell the first scheduling pass about the latency between
13445              a mtctr and bctr (and mtlr and br/blr).  The first
13446              scheduling pass will not know about this latency since
13447              the mtctr instruction, which has the latency associated
13448              to it, will be generated by reload.  */
13449           return TARGET_POWER ? 5 : 4;
13450         case TYPE_BRANCH:
13451           /* Leave some extra cycles between a compare and its
13452              dependent branch, to inhibit expensive mispredicts.  */
13453           if ((rs6000_cpu_attr == CPU_PPC603
13454                || rs6000_cpu_attr == CPU_PPC604
13455                || rs6000_cpu_attr == CPU_PPC604E
13456                || rs6000_cpu_attr == CPU_PPC620
13457                || rs6000_cpu_attr == CPU_PPC630
13458                || rs6000_cpu_attr == CPU_PPC750
13459                || rs6000_cpu_attr == CPU_PPC7400
13460                || rs6000_cpu_attr == CPU_PPC7450
13461                || rs6000_cpu_attr == CPU_POWER4)
13462               && recog_memoized (dep_insn)
13463               && (INSN_CODE (dep_insn) >= 0)
13464               && (get_attr_type (dep_insn) == TYPE_CMP
13465                   || get_attr_type (dep_insn) == TYPE_COMPARE
13466                   || get_attr_type (dep_insn) == TYPE_DELAYED_COMPARE
13467                   || get_attr_type (dep_insn) == TYPE_IMUL_COMPARE
13468                   || get_attr_type (dep_insn) == TYPE_LMUL_COMPARE
13469                   || get_attr_type (dep_insn) == TYPE_FPCOMPARE
13470                   || get_attr_type (dep_insn) == TYPE_CR_LOGICAL
13471                   || get_attr_type (dep_insn) == TYPE_DELAYED_CR))
13472             return cost + 2;
13473         default:
13474           break;
13475         }
13476       /* Fall out to return default cost.  */
13477     }
13478
13479   return cost;
13480 }
13481
13482 /* The function returns a true if INSN is microcoded.
13483    Return false ptherwise.  */
13484
13485 static bool
13486 is_microcoded_insn (rtx insn)
13487 {
13488   if (!insn || !INSN_P (insn)
13489       || GET_CODE (PATTERN (insn)) == USE
13490       || GET_CODE (PATTERN (insn)) == CLOBBER)
13491     return false;
13492
13493   if (rs6000_cpu == PROCESSOR_POWER4)
13494     {
13495       enum attr_type type = get_attr_type (insn);
13496       if (type == TYPE_LOAD_EXT_U
13497           || type == TYPE_LOAD_EXT_UX
13498           || type == TYPE_LOAD_UX
13499           || type == TYPE_STORE_UX
13500           || type == TYPE_MFCR)
13501         return true;
13502     }
13503
13504   return false;
13505 }
13506
13507 /* The function returns a non-zero value if INSN can be scheduled only
13508    as the first insn in a dispatch group ("dispatch-slot restricted").
13509    In this case, the returned value indicates how many dispatch slots
13510    the insn occupies (at the beginning of the group).
13511    Return 0 otherwise.  */
13512
13513 static int
13514 is_dispatch_slot_restricted (rtx insn)
13515 {
13516   enum attr_type type;
13517
13518   if (rs6000_cpu != PROCESSOR_POWER4)
13519     return 0;
13520
13521   if (!insn
13522       || insn == NULL_RTX
13523       || GET_CODE (insn) == NOTE
13524       || GET_CODE (PATTERN (insn)) == USE
13525       || GET_CODE (PATTERN (insn)) == CLOBBER)
13526     return 0;
13527
13528   type = get_attr_type (insn);
13529
13530   switch (type){
13531   case TYPE_MFCR:
13532   case TYPE_MFCRF:
13533   case TYPE_MTCR:
13534   case TYPE_DELAYED_CR:
13535   case TYPE_CR_LOGICAL:
13536   case TYPE_MTJMPR:
13537   case TYPE_MFJMPR:
13538     return 1;
13539   case TYPE_IDIV:
13540   case TYPE_LDIV:
13541     return 2;
13542   default:
13543     return 0;
13544   }
13545 }
13546
13547 /* The function returns true if INSN is cracked into 2 instructions
13548    by the processor (and therefore occupies 2 issue slots).  */
13549
13550 static bool
13551 is_cracked_insn (rtx insn)
13552 {
13553   if (!insn || !INSN_P (insn)
13554       || GET_CODE (PATTERN (insn)) == USE
13555       || GET_CODE (PATTERN (insn)) == CLOBBER)
13556     return false;
13557
13558   if (rs6000_cpu == PROCESSOR_POWER4)
13559     {
13560       enum attr_type type = get_attr_type (insn);
13561       if (type == TYPE_LOAD_U || type == TYPE_STORE_U
13562                || type == TYPE_FPLOAD_U || type == TYPE_FPSTORE_U
13563                || type == TYPE_FPLOAD_UX || type == TYPE_FPSTORE_UX
13564                || type == TYPE_LOAD_EXT || type == TYPE_DELAYED_CR
13565                || type == TYPE_COMPARE || type == TYPE_DELAYED_COMPARE
13566                || type == TYPE_IMUL_COMPARE || type == TYPE_LMUL_COMPARE
13567                || type == TYPE_IDIV || type == TYPE_LDIV
13568                || type == TYPE_INSERT_WORD)
13569         return true;
13570     }
13571
13572   return false;
13573 }
13574
13575 /* The function returns true if INSN can be issued only from
13576    the branch slot. */
13577
13578 static bool
13579 is_branch_slot_insn (rtx insn)
13580 {
13581   if (!insn || !INSN_P (insn)
13582       || GET_CODE (PATTERN (insn)) == USE
13583       || GET_CODE (PATTERN (insn)) == CLOBBER)
13584     return false;
13585
13586   if (rs6000_cpu == PROCESSOR_POWER4)
13587     {
13588       enum attr_type type = get_attr_type (insn);
13589       if (type == TYPE_BRANCH || type == TYPE_JMPREG)
13590         return true;     
13591       return false;
13592     }
13593
13594   return false;
13595 }
13596
13597 /* A C statement (sans semicolon) to update the integer scheduling
13598    priority INSN_PRIORITY (INSN). Increase the priority to execute the
13599    INSN earlier, reduce the priority to execute INSN later.  Do not
13600    define this macro if you do not need to adjust the scheduling
13601    priorities of insns.  */
13602
13603 static int
13604 rs6000_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
13605 {
13606   /* On machines (like the 750) which have asymmetric integer units,
13607      where one integer unit can do multiply and divides and the other
13608      can't, reduce the priority of multiply/divide so it is scheduled
13609      before other integer operations.  */
13610
13611 #if 0
13612   if (! INSN_P (insn))
13613     return priority;
13614
13615   if (GET_CODE (PATTERN (insn)) == USE)
13616     return priority;
13617
13618   switch (rs6000_cpu_attr) {
13619   case CPU_PPC750:
13620     switch (get_attr_type (insn))
13621       {
13622       default:
13623         break;
13624
13625       case TYPE_IMUL:
13626       case TYPE_IDIV:
13627         fprintf (stderr, "priority was %#x (%d) before adjustment\n",
13628                  priority, priority);
13629         if (priority >= 0 && priority < 0x01000000)
13630           priority >>= 3;
13631         break;
13632       }
13633   }
13634 #endif
13635
13636   if (is_dispatch_slot_restricted (insn)
13637       && reload_completed
13638       && current_sched_info->sched_max_insns_priority 
13639       && rs6000_sched_restricted_insns_priority)
13640     {
13641
13642       /* Prioritize insns that can be dispatched only in the first dispatch slot.  */
13643       if (rs6000_sched_restricted_insns_priority == 1)
13644         /* Attach highest priority to insn. This means that in 
13645            haifa-sched.c:ready_sort(), dispatch-slot restriction considerations 
13646            precede 'priority' (critical path) considerations.  */
13647         return current_sched_info->sched_max_insns_priority; 
13648       else if (rs6000_sched_restricted_insns_priority == 2)
13649         /* Increase priority of insn by a minimal amount. This means that in 
13650            haifa-sched.c:ready_sort(), only 'priority' (critical path) considerations
13651            precede dispatch-slot restriction considerations.  */
13652         return (priority + 1); 
13653     } 
13654
13655   return priority;
13656 }
13657
13658 /* Return how many instructions the machine can issue per cycle.  */
13659
13660 static int
13661 rs6000_issue_rate (void)
13662 {
13663   /* Use issue rate of 1 for first scheduling pass to decrease degradation.  */
13664   if (!reload_completed)
13665     return 1;
13666
13667   switch (rs6000_cpu_attr) {
13668   case CPU_RIOS1:  /* ? */
13669   case CPU_RS64A:
13670   case CPU_PPC601: /* ? */
13671   case CPU_PPC7450:
13672     return 3;
13673   case CPU_PPC440:
13674   case CPU_PPC603:
13675   case CPU_PPC750:
13676   case CPU_PPC7400:
13677   case CPU_PPC8540:
13678     return 2; 
13679   case CPU_RIOS2:
13680   case CPU_PPC604:
13681   case CPU_PPC604E:
13682   case CPU_PPC620:
13683   case CPU_PPC630:
13684     return 4;
13685   case CPU_POWER4:
13686     return 5;
13687   default:
13688     return 1;
13689   }
13690 }
13691
13692 /* Return how many instructions to look ahead for better insn
13693    scheduling.  */
13694
13695 static int
13696 rs6000_use_sched_lookahead (void)
13697 {
13698   if (rs6000_cpu_attr == CPU_PPC8540)
13699     return 4;
13700   return 0;
13701 }
13702
13703 /* Determine is PAT refers to memory.  */
13704
13705 static bool
13706 is_mem_ref (rtx pat)
13707 {
13708   const char * fmt;
13709   int i, j;
13710   bool ret = false;
13711
13712   if (GET_CODE (pat) == MEM)
13713     return true;
13714
13715   /* Recursively process the pattern.  */
13716   fmt = GET_RTX_FORMAT (GET_CODE (pat));
13717
13718   for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0 && !ret; i--)
13719     {
13720       if (fmt[i] == 'e')
13721         ret |= is_mem_ref (XEXP (pat, i));
13722       else if (fmt[i] == 'E')
13723         for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
13724           ret |= is_mem_ref (XVECEXP (pat, i, j));
13725     }
13726
13727   return ret;
13728 }
13729
13730 /* Determine if PAT is a PATTERN of a load insn.  */
13731  
13732 static bool
13733 is_load_insn1 (rtx pat)
13734 {
13735   if (!pat || pat == NULL_RTX)
13736     return false;
13737
13738   if (GET_CODE (pat) == SET)
13739     return is_mem_ref (SET_SRC (pat));
13740
13741   if (GET_CODE (pat) == PARALLEL)
13742     {
13743       int i;
13744
13745       for (i = 0; i < XVECLEN (pat, 0); i++)
13746         if (is_load_insn1 (XVECEXP (pat, 0, i)))
13747           return true;
13748     }
13749
13750   return false;
13751 }
13752
13753 /* Determine if INSN loads from memory.  */
13754
13755 static bool
13756 is_load_insn (rtx insn)
13757 {
13758   if (!insn || !INSN_P (insn))
13759     return false;
13760
13761   if (GET_CODE (insn) == CALL_INSN)
13762     return false;
13763
13764   return is_load_insn1 (PATTERN (insn));
13765 }
13766
13767 /* Determine if PAT is a PATTERN of a store insn.  */
13768
13769 static bool
13770 is_store_insn1 (rtx pat)
13771 {
13772   if (!pat || pat == NULL_RTX)
13773     return false;
13774
13775   if (GET_CODE (pat) == SET)
13776     return is_mem_ref (SET_DEST (pat));
13777
13778   if (GET_CODE (pat) == PARALLEL)
13779     {
13780       int i;
13781
13782       for (i = 0; i < XVECLEN (pat, 0); i++)
13783         if (is_store_insn1 (XVECEXP (pat, 0, i)))
13784           return true;
13785     }
13786
13787   return false;
13788 }
13789
13790 /* Determine if INSN stores to memory.  */
13791
13792 static bool
13793 is_store_insn (rtx insn)
13794 {
13795   if (!insn || !INSN_P (insn))
13796     return false;
13797
13798   return is_store_insn1 (PATTERN (insn));
13799 }
13800
13801 /* Returns whether the dependence between INSN and NEXT is considered
13802    costly by the given target.  */
13803
13804 static bool
13805 rs6000_is_costly_dependence (rtx insn, rtx next, rtx link, int cost, int distance)
13806 {      
13807   /* If the flag is not enbled - no dependence is considered costly;
13808      allow all dependent insns in the same group. 
13809      This is the most aggressive option.  */
13810   if (rs6000_sched_costly_dep == no_dep_costly)
13811     return false;
13812
13813   /* If the flag is set to 1 - a dependence is always considered costly; 
13814      do not allow dependent instructions in the same group.
13815      This is the most conservative option.  */
13816   if (rs6000_sched_costly_dep == all_deps_costly)
13817     return true;       
13818
13819   if (rs6000_sched_costly_dep == store_to_load_dep_costly 
13820       && is_load_insn (next) 
13821       && is_store_insn (insn))
13822     /* Prevent load after store in the same group.  */
13823     return true;
13824
13825   if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
13826       && is_load_insn (next) 
13827       && is_store_insn (insn)
13828       && (!link || (int) REG_NOTE_KIND (link) == 0))
13829      /* Prevent load after store in the same group if it is a true dependence.  */
13830      return true;
13831     
13832   /* The flag is set to X; dependences with latency >= X are considered costly, 
13833      and will not be scheduled in the same group.  */
13834   if (rs6000_sched_costly_dep <= max_dep_latency
13835       && ((cost - distance) >= (int)rs6000_sched_costly_dep))
13836     return true;
13837
13838   return false;
13839 }
13840
13841 /* Return the next insn after INSN that is found before TAIL is reached, 
13842    skipping any "non-active" insns - insns that will not actually occupy
13843    an issue slot.  Return NULL_RTX if such an insn is not found.  */
13844
13845 static rtx
13846 get_next_active_insn (rtx insn, rtx tail)
13847 {
13848   rtx next_insn;
13849
13850   if (!insn || insn == tail)
13851     return NULL_RTX;
13852
13853   next_insn = NEXT_INSN (insn);
13854
13855   while (next_insn
13856          && next_insn != tail
13857          && (GET_CODE(next_insn) == NOTE
13858              || GET_CODE (PATTERN (next_insn)) == USE
13859              || GET_CODE (PATTERN (next_insn)) == CLOBBER))
13860     {
13861       next_insn = NEXT_INSN (next_insn);
13862     }
13863
13864   if (!next_insn || next_insn == tail)
13865     return NULL_RTX;
13866
13867   return next_insn;
13868 }
13869
13870 /* Return whether the presence of INSN causes a dispatch group terminatation
13871    of group WHICH_GROUP.
13872
13873    If WHICH_GROUP == current_group, this function will return true if INSN
13874    causes the termination of the current group (i.e, the dispatch group to
13875    which INSN belongs). This means that INSN will be the last insn in the
13876    group it belongs to.
13877
13878    If WHICH_GROUP == previous_group, this function will return true if INSN
13879    causes the termination of the previous group (i.e, the dispatch group that
13880    precedes the group to which INSN belongs).  This means that INSN will be
13881    the first insn in the group it belongs to).  */
13882
13883 static bool
13884 insn_terminates_group_p (rtx insn, enum group_termination which_group)
13885 {
13886   enum attr_type type;
13887
13888   if (! insn)
13889     return false;
13890
13891   type = get_attr_type (insn);
13892
13893   if (is_microcoded_insn (insn))
13894     return true;
13895
13896   if (which_group == current_group)
13897     {
13898       if (is_branch_slot_insn (insn))
13899         return true;
13900       return false;
13901     }
13902   else if (which_group == previous_group)
13903     {
13904       if (is_dispatch_slot_restricted (insn))
13905         return true;
13906       return false;
13907     }
13908
13909   return false;
13910 }
13911
13912 /* Return true if it is recommended to keep NEXT_INSN "far" (in a seperate
13913    dispatch group) from the insns in GROUP_INSNS.  Return false otherwise.  */
13914
13915 static bool
13916 is_costly_group (rtx *group_insns, rtx next_insn)
13917 {
13918   int i;
13919   rtx link;
13920   int cost;
13921   int issue_rate = rs6000_issue_rate ();
13922
13923   for (i = 0; i < issue_rate; i++)
13924     {
13925       rtx insn = group_insns[i];
13926       if (!insn)
13927         continue;
13928       for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
13929         {
13930           rtx next = XEXP (link, 0);
13931           if (next == next_insn)
13932             {
13933               cost = insn_cost (insn, link, next_insn);
13934               if (rs6000_is_costly_dependence (insn, next_insn, link, cost, 0))
13935                 return true;
13936             }
13937         }
13938     }
13939
13940   return false;
13941 }
13942
13943 /* Utility of the function redefine_groups. 
13944    Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
13945    in the same dispatch group.  If so, insert nops before NEXT_INSN, in order
13946    to keep it "far" (in a separate group) from GROUP_INSNS, following
13947    one of the following schemes, depending on the value of the flag
13948    -minsert_sched_nops = X:
13949    (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
13950        in order to force NEXT_INSN into a seperate group.
13951    (2) X < sched_finish_regroup_exact: insert exactly X nops.  
13952    GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop 
13953    insertion (has a group just ended, how many vacant issue slots remain in the
13954    last group, and how many dispatch groups were encountered so far).  */
13955
13956 static int 
13957 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns, rtx next_insn,
13958                  bool *group_end, int can_issue_more, int *group_count)
13959 {
13960   rtx nop;
13961   bool force;
13962   int issue_rate = rs6000_issue_rate ();
13963   bool end = *group_end;
13964   int i;
13965
13966   if (next_insn == NULL_RTX)
13967     return can_issue_more;
13968
13969   if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
13970     return can_issue_more;
13971
13972   force = is_costly_group (group_insns, next_insn);
13973   if (!force)
13974     return can_issue_more;
13975
13976   if (sched_verbose > 6)
13977     fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
13978                         *group_count ,can_issue_more);
13979
13980   if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
13981     {
13982       if (*group_end)
13983         can_issue_more = 0;
13984
13985       /* Since only a branch can be issued in the last issue_slot, it is
13986          sufficient to insert 'can_issue_more - 1' nops if next_insn is not
13987          a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
13988          in this case the last nop will start a new group and the branch will be
13989          forced to the new group.  */
13990       if (can_issue_more && !is_branch_slot_insn (next_insn))
13991         can_issue_more--;
13992
13993       while (can_issue_more > 0)
13994         {
13995           nop = gen_nop();
13996           emit_insn_before (nop, next_insn);
13997           can_issue_more--;
13998         }
13999
14000       *group_end = true;
14001       return 0;
14002     } 
14003
14004   if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
14005     {
14006       int n_nops = rs6000_sched_insert_nops;
14007
14008       /* Nops can't be issued from the branch slot, so the effective 
14009          issue_rate for nops is 'issue_rate - 1'.  */
14010       if (can_issue_more == 0)
14011         can_issue_more = issue_rate;
14012       can_issue_more--;
14013       if (can_issue_more == 0)
14014         {
14015           can_issue_more = issue_rate - 1;
14016           (*group_count)++;
14017           end = true;
14018           for (i = 0; i < issue_rate; i++)
14019             {
14020               group_insns[i] = 0;
14021             }
14022         }
14023
14024       while (n_nops > 0)
14025         {
14026           nop = gen_nop ();
14027           emit_insn_before (nop, next_insn);
14028           if (can_issue_more == issue_rate - 1) /* new group begins */
14029             end = false;
14030           can_issue_more--;
14031           if (can_issue_more == 0)
14032             {
14033               can_issue_more = issue_rate - 1;
14034               (*group_count)++;
14035               end = true;
14036               for (i = 0; i < issue_rate; i++)
14037                 {
14038                   group_insns[i] = 0;
14039                 } 
14040             }   
14041           n_nops--;
14042         }
14043
14044       /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1').  */
14045       can_issue_more++; 
14046
14047       *group_end = /* Is next_insn going to start a new group?  */
14048           (end 
14049            || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
14050            || (can_issue_more <= 2 && is_cracked_insn (next_insn))
14051            || (can_issue_more < issue_rate &&
14052               insn_terminates_group_p (next_insn, previous_group)));
14053       if (*group_end && end)
14054         (*group_count)--;
14055
14056       if (sched_verbose > 6)
14057         fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
14058                         *group_count, can_issue_more);
14059       return can_issue_more;    
14060     } 
14061
14062   return can_issue_more;
14063 }
14064
14065 /* This function tries to synch the dispatch groups that the compiler "sees"
14066    with the dispatch groups that the processor dispatcher is expected to 
14067    form in practice.  It tries to achieve this synchronization by forcing the
14068    estimated processor grouping on the compiler (as opposed to the function
14069    'pad_goups' which tries to force the scheduler's grouping on the processor).
14070
14071    The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
14072    examines the (estimated) dispatch groups that will be formed by the processor
14073    dispatcher.  It marks these group boundaries to reflect the estimated
14074    processor grouping, overriding the grouping that the scheduler had marked.
14075    Depending on the value of the flag '-minsert-sched-nops' this function can
14076    force certain insns into separate groups or force a certain distance between
14077    them by inserting nops, for example, if there exists a "costly dependence"
14078    between the insns.
14079
14080    The function estimates the group boundaries that the processor will form as
14081    folllows:  It keeps track of how many vacant issue slots are available after
14082    each insn.  A subsequent insn will start a new group if one of the following
14083    4 cases applies:
14084    - no more vacant issue slots remain in the current dispatch group.
14085    - only the last issue slot, which is the branch slot, is vacant, but the next
14086      insn is not a branch.
14087    - only the last 2 or less issue slots, including the branch slot, are vacant,
14088      which means that a cracked insn (which occupies two issue slots) can't be
14089      issued in this group.
14090    - less than 'issue_rate' slots are vacant, and the next insn always needs to 
14091      start a new group.  */
14092
14093 static int
14094 redefine_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
14095 {
14096   rtx insn, next_insn;
14097   int issue_rate;
14098   int can_issue_more;
14099   int slot, i;
14100   bool group_end;
14101   int group_count = 0;
14102   rtx *group_insns;
14103
14104   /* Initialize.  */
14105   issue_rate = rs6000_issue_rate ();
14106   group_insns = alloca (issue_rate * sizeof (rtx));
14107   for (i = 0; i < issue_rate; i++) 
14108     {
14109       group_insns[i] = 0;
14110     }
14111   can_issue_more = issue_rate;
14112   slot = 0;
14113   insn = get_next_active_insn (prev_head_insn, tail);
14114   group_end = false;
14115
14116   while (insn != NULL_RTX)
14117     {
14118       slot = (issue_rate - can_issue_more);
14119       group_insns[slot] = insn;
14120       can_issue_more =
14121         rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
14122       if (insn_terminates_group_p (insn, current_group))
14123         can_issue_more = 0;
14124
14125       next_insn = get_next_active_insn (insn, tail);
14126       if (next_insn == NULL_RTX)
14127         return group_count + 1;
14128
14129       group_end = /* Is next_insn going to start a new group?  */
14130         (can_issue_more == 0
14131          || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
14132          || (can_issue_more <= 2 && is_cracked_insn (next_insn))
14133          || (can_issue_more < issue_rate &&
14134              insn_terminates_group_p (next_insn, previous_group)));
14135
14136       can_issue_more = force_new_group (sched_verbose, dump, group_insns, 
14137                         next_insn, &group_end, can_issue_more, &group_count);
14138
14139       if (group_end)
14140         {
14141           group_count++;
14142           can_issue_more = 0;
14143           for (i = 0; i < issue_rate; i++)
14144             {
14145               group_insns[i] = 0;
14146             }
14147         }
14148
14149       if (GET_MODE (next_insn) == TImode && can_issue_more)
14150         PUT_MODE(next_insn, VOIDmode);
14151       else if (!can_issue_more && GET_MODE (next_insn) != TImode)
14152         PUT_MODE (next_insn, TImode);
14153
14154       insn = next_insn;
14155       if (can_issue_more == 0)
14156         can_issue_more = issue_rate;
14157    } /* while */
14158
14159   return group_count;
14160 }
14161
14162 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
14163    dispatch group boundaries that the scheduler had marked.  Pad with nops
14164    any dispatch groups which have vacant issue slots, in order to force the
14165    scheduler's grouping on the processor dispatcher.  The function
14166    returns the number of dispatch groups found.  */
14167
14168 static int
14169 pad_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
14170 {
14171   rtx insn, next_insn;
14172   rtx nop;
14173   int issue_rate;
14174   int can_issue_more;
14175   int group_end;
14176   int group_count = 0;
14177
14178   /* Initialize issue_rate.  */
14179   issue_rate = rs6000_issue_rate ();
14180   can_issue_more = issue_rate;
14181
14182   insn = get_next_active_insn (prev_head_insn, tail);
14183   next_insn = get_next_active_insn (insn, tail);
14184
14185   while (insn != NULL_RTX)
14186     {
14187       can_issue_more =
14188         rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
14189
14190       group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
14191
14192       if (next_insn == NULL_RTX)
14193         break;
14194
14195       if (group_end)
14196         {
14197           /* If the scheduler had marked group termination at this location
14198              (between insn and next_indn), and neither insn nor next_insn will
14199              force group termination, pad the group with nops to force group
14200              termination.  */
14201           if (can_issue_more
14202               && (rs6000_sched_insert_nops == sched_finish_pad_groups)
14203               && !insn_terminates_group_p (insn, current_group)
14204               && !insn_terminates_group_p (next_insn, previous_group))
14205             {
14206               if (!is_branch_slot_insn(next_insn))
14207                 can_issue_more--;
14208
14209               while (can_issue_more)
14210                 {
14211                   nop = gen_nop ();
14212                   emit_insn_before (nop, next_insn);
14213                   can_issue_more--;
14214                 }
14215             }
14216
14217           can_issue_more = issue_rate;
14218           group_count++;
14219         }
14220
14221       insn = next_insn;
14222       next_insn = get_next_active_insn (insn, tail);
14223     }
14224
14225   return group_count;
14226 }
14227
14228 /* The following function is called at the end of scheduling BB.
14229    After reload, it inserts nops at insn group bundling.  */
14230
14231 static void
14232 rs6000_sched_finish (FILE *dump, int sched_verbose)
14233 {
14234   int n_groups;
14235
14236   if (sched_verbose)
14237     fprintf (dump, "=== Finishing schedule.\n");
14238
14239   if (reload_completed && rs6000_cpu == PROCESSOR_POWER4)
14240     {
14241       if (rs6000_sched_insert_nops == sched_finish_none)
14242         return;
14243
14244       if (rs6000_sched_insert_nops == sched_finish_pad_groups)
14245         n_groups = pad_groups (dump, sched_verbose,
14246                                 current_sched_info->prev_head,
14247                                 current_sched_info->next_tail);
14248       else
14249         n_groups = redefine_groups (dump, sched_verbose,
14250                                 current_sched_info->prev_head,
14251                                 current_sched_info->next_tail);
14252
14253       if (sched_verbose >= 6)
14254         {
14255           fprintf (dump, "ngroups = %d\n", n_groups);
14256           print_rtl (dump, current_sched_info->prev_head);
14257           fprintf (dump, "Done finish_sched\n");
14258         }
14259     }
14260 }
14261 \f
14262 /* Length in units of the trampoline for entering a nested function.  */
14263
14264 int
14265 rs6000_trampoline_size (void)
14266 {
14267   int ret = 0;
14268
14269   switch (DEFAULT_ABI)
14270     {
14271     default:
14272       abort ();
14273
14274     case ABI_AIX:
14275       ret = (TARGET_32BIT) ? 12 : 24;
14276       break;
14277
14278     case ABI_DARWIN:
14279     case ABI_V4:
14280       ret = (TARGET_32BIT) ? 40 : 48;
14281       break;
14282     }
14283
14284   return ret;
14285 }
14286
14287 /* Emit RTL insns to initialize the variable parts of a trampoline.
14288    FNADDR is an RTX for the address of the function's pure code.
14289    CXT is an RTX for the static chain value for the function.  */
14290
14291 void
14292 rs6000_initialize_trampoline (rtx addr, rtx fnaddr, rtx cxt)
14293 {
14294   enum machine_mode pmode = Pmode;
14295   int regsize = (TARGET_32BIT) ? 4 : 8;
14296   rtx ctx_reg = force_reg (pmode, cxt);
14297
14298   switch (DEFAULT_ABI)
14299     {
14300     default:
14301       abort ();
14302
14303 /* Macros to shorten the code expansions below.  */
14304 #define MEM_DEREF(addr) gen_rtx_MEM (pmode, memory_address (pmode, addr))
14305 #define MEM_PLUS(addr,offset) \
14306   gen_rtx_MEM (pmode, memory_address (pmode, plus_constant (addr, offset)))
14307
14308     /* Under AIX, just build the 3 word function descriptor */
14309     case ABI_AIX:
14310       {
14311         rtx fn_reg = gen_reg_rtx (pmode);
14312         rtx toc_reg = gen_reg_rtx (pmode);
14313         emit_move_insn (fn_reg, MEM_DEREF (fnaddr));
14314         emit_move_insn (toc_reg, MEM_PLUS (fnaddr, regsize));
14315         emit_move_insn (MEM_DEREF (addr), fn_reg);
14316         emit_move_insn (MEM_PLUS (addr, regsize), toc_reg);
14317         emit_move_insn (MEM_PLUS (addr, 2*regsize), ctx_reg);
14318       }
14319       break;
14320
14321     /* Under V.4/eabi/darwin, __trampoline_setup does the real work.  */
14322     case ABI_DARWIN:
14323     case ABI_V4:
14324       emit_library_call (gen_rtx_SYMBOL_REF (SImode, "__trampoline_setup"),
14325                          FALSE, VOIDmode, 4,
14326                          addr, pmode,
14327                          GEN_INT (rs6000_trampoline_size ()), SImode,
14328                          fnaddr, pmode,
14329                          ctx_reg, pmode);
14330       break;
14331     }
14332
14333   return;
14334 }
14335
14336 \f
14337 /* Table of valid machine attributes.  */
14338
14339 const struct attribute_spec rs6000_attribute_table[] =
14340 {
14341   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
14342   { "longcall",  0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
14343   { "shortcall", 0, 0, false, true,  true,  rs6000_handle_longcall_attribute },
14344   { NULL,        0, 0, false, false, false, NULL }
14345 };
14346
14347 /* Handle a "longcall" or "shortcall" attribute; arguments as in
14348    struct attribute_spec.handler.  */
14349
14350 static tree
14351 rs6000_handle_longcall_attribute (tree *node, tree name, 
14352                                   tree args ATTRIBUTE_UNUSED, 
14353                                   int flags ATTRIBUTE_UNUSED, 
14354                                   bool *no_add_attrs)
14355 {
14356   if (TREE_CODE (*node) != FUNCTION_TYPE
14357       && TREE_CODE (*node) != FIELD_DECL
14358       && TREE_CODE (*node) != TYPE_DECL)
14359     {
14360       warning ("`%s' attribute only applies to functions",
14361                IDENTIFIER_POINTER (name));
14362       *no_add_attrs = true;
14363     }
14364
14365   return NULL_TREE;
14366 }
14367
14368 /* Set longcall attributes on all functions declared when
14369    rs6000_default_long_calls is true.  */
14370 static void
14371 rs6000_set_default_type_attributes (tree type)
14372 {
14373   if (rs6000_default_long_calls
14374       && (TREE_CODE (type) == FUNCTION_TYPE
14375           || TREE_CODE (type) == METHOD_TYPE))
14376     TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
14377                                         NULL_TREE,
14378                                         TYPE_ATTRIBUTES (type));
14379 }
14380
14381 /* Return a reference suitable for calling a function with the
14382    longcall attribute.  */
14383
14384 struct rtx_def *
14385 rs6000_longcall_ref (rtx call_ref)
14386 {
14387   const char *call_name;
14388   tree node;
14389
14390   if (GET_CODE (call_ref) != SYMBOL_REF)
14391     return call_ref;
14392
14393   /* System V adds '.' to the internal name, so skip them.  */
14394   call_name = XSTR (call_ref, 0);
14395   if (*call_name == '.')
14396     {
14397       while (*call_name == '.')
14398         call_name++;
14399
14400       node = get_identifier (call_name);
14401       call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
14402     }
14403
14404   return force_reg (Pmode, call_ref);
14405 }
14406 \f
14407 #ifdef USING_ELFOS_H
14408
14409 /* A C statement or statements to switch to the appropriate section
14410    for output of RTX in mode MODE.  You can assume that RTX is some
14411    kind of constant in RTL.  The argument MODE is redundant except in
14412    the case of a `const_int' rtx.  Select the section by calling
14413    `text_section' or one of the alternatives for other sections.
14414
14415    Do not define this macro if you put all constants in the read-only
14416    data section.  */
14417
14418 static void
14419 rs6000_elf_select_rtx_section (enum machine_mode mode, rtx x, 
14420                                unsigned HOST_WIDE_INT align)
14421 {
14422   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
14423     toc_section ();
14424   else
14425     default_elf_select_rtx_section (mode, x, align);
14426 }
14427
14428 /* A C statement or statements to switch to the appropriate
14429    section for output of DECL.  DECL is either a `VAR_DECL' node
14430    or a constant of some sort.  RELOC indicates whether forming
14431    the initial value of DECL requires link-time relocations.  */
14432
14433 static void
14434 rs6000_elf_select_section (tree decl, int reloc, 
14435                            unsigned HOST_WIDE_INT align)
14436 {
14437   /* Pretend that we're always building for a shared library when
14438      ABI_AIX, because otherwise we end up with dynamic relocations
14439      in read-only sections.  This happens for function pointers,
14440      references to vtables in typeinfo, and probably other cases.  */
14441   default_elf_select_section_1 (decl, reloc, align,
14442                                 flag_pic || DEFAULT_ABI == ABI_AIX);
14443 }
14444
14445 /* A C statement to build up a unique section name, expressed as a
14446    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
14447    RELOC indicates whether the initial value of EXP requires
14448    link-time relocations.  If you do not define this macro, GCC will use
14449    the symbol name prefixed by `.' as the section name.  Note - this
14450    macro can now be called for uninitialized data items as well as
14451    initialized data and functions.  */
14452
14453 static void
14454 rs6000_elf_unique_section (tree decl, int reloc)
14455 {
14456   /* As above, pretend that we're always building for a shared library
14457      when ABI_AIX, to avoid dynamic relocations in read-only sections.  */
14458   default_unique_section_1 (decl, reloc,
14459                             flag_pic || DEFAULT_ABI == ABI_AIX);
14460 }
14461 \f
14462 /* For a SYMBOL_REF, set generic flags and then perform some
14463    target-specific processing.
14464
14465    When the AIX ABI is requested on a non-AIX system, replace the
14466    function name with the real name (with a leading .) rather than the
14467    function descriptor name.  This saves a lot of overriding code to
14468    read the prefixes.  */
14469
14470 static void
14471 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
14472 {
14473   default_encode_section_info (decl, rtl, first);
14474
14475   if (first
14476       && TREE_CODE (decl) == FUNCTION_DECL
14477       && !TARGET_AIX
14478       && DEFAULT_ABI == ABI_AIX)
14479     {
14480       rtx sym_ref = XEXP (rtl, 0);
14481       size_t len = strlen (XSTR (sym_ref, 0));
14482       char *str = alloca (len + 2);
14483       str[0] = '.';
14484       memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
14485       XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
14486     }
14487 }
14488
14489 static bool
14490 rs6000_elf_in_small_data_p (tree decl)
14491 {
14492   if (rs6000_sdata == SDATA_NONE)
14493     return false;
14494
14495   if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
14496     {
14497       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
14498       if (strcmp (section, ".sdata") == 0
14499           || strcmp (section, ".sdata2") == 0
14500           || strcmp (section, ".sbss") == 0
14501           || strcmp (section, ".sbss2") == 0
14502           || strcmp (section, ".PPC.EMB.sdata0") == 0
14503           || strcmp (section, ".PPC.EMB.sbss0") == 0)
14504         return true;
14505     }
14506   else
14507     {
14508       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
14509
14510       if (size > 0
14511           && (unsigned HOST_WIDE_INT) size <= g_switch_value
14512           /* If it's not public, and we're not going to reference it there,
14513              there's no need to put it in the small data section.  */
14514           && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
14515         return true;
14516     }
14517
14518   return false;
14519 }
14520
14521 #endif /* USING_ELFOS_H */
14522
14523 \f
14524 /* Return a REG that occurs in ADDR with coefficient 1.
14525    ADDR can be effectively incremented by incrementing REG.
14526
14527    r0 is special and we must not select it as an address
14528    register by this routine since our caller will try to
14529    increment the returned register via an "la" instruction.  */
14530
14531 struct rtx_def *
14532 find_addr_reg (rtx addr)
14533 {
14534   while (GET_CODE (addr) == PLUS)
14535     {
14536       if (GET_CODE (XEXP (addr, 0)) == REG
14537           && REGNO (XEXP (addr, 0)) != 0)
14538         addr = XEXP (addr, 0);
14539       else if (GET_CODE (XEXP (addr, 1)) == REG
14540                && REGNO (XEXP (addr, 1)) != 0)
14541         addr = XEXP (addr, 1);
14542       else if (CONSTANT_P (XEXP (addr, 0)))
14543         addr = XEXP (addr, 1);
14544       else if (CONSTANT_P (XEXP (addr, 1)))
14545         addr = XEXP (addr, 0);
14546       else
14547         abort ();
14548     }
14549   if (GET_CODE (addr) == REG && REGNO (addr) != 0)
14550     return addr;
14551   abort ();
14552 }
14553
14554 void
14555 rs6000_fatal_bad_address (rtx op)
14556 {
14557   fatal_insn ("bad address", op);
14558 }
14559
14560 #if TARGET_MACHO
14561
14562 #if 0
14563 /* Returns 1 if OP is either a symbol reference or a sum of a symbol
14564    reference and a constant.  */
14565
14566 int
14567 symbolic_operand (rtx op)
14568 {
14569   switch (GET_CODE (op))
14570     {
14571     case SYMBOL_REF:
14572     case LABEL_REF:
14573       return 1;
14574     case CONST:
14575       op = XEXP (op, 0);
14576       return (GET_CODE (op) == SYMBOL_REF ||
14577               (GET_CODE (XEXP (op, 0)) == SYMBOL_REF
14578                || GET_CODE (XEXP (op, 0)) == LABEL_REF)
14579               && GET_CODE (XEXP (op, 1)) == CONST_INT);
14580     default:
14581       return 0;
14582     }
14583 }
14584 #endif
14585
14586 #ifdef RS6000_LONG_BRANCH
14587
14588 static tree stub_list = 0;
14589
14590 /* ADD_COMPILER_STUB adds the compiler generated stub for handling 
14591    procedure calls to the linked list.  */
14592
14593 void 
14594 add_compiler_stub (tree label_name, tree function_name, int line_number)
14595 {
14596   tree stub = build_tree_list (function_name, label_name);
14597   TREE_TYPE (stub) = build_int_2 (line_number, 0);
14598   TREE_CHAIN (stub) = stub_list;
14599   stub_list = stub;
14600 }
14601
14602 #define STUB_LABEL_NAME(STUB)     TREE_VALUE (STUB)
14603 #define STUB_FUNCTION_NAME(STUB)  TREE_PURPOSE (STUB)
14604 #define STUB_LINE_NUMBER(STUB)    TREE_INT_CST_LOW (TREE_TYPE (STUB))
14605
14606 /* OUTPUT_COMPILER_STUB outputs the compiler generated stub for
14607    handling procedure calls from the linked list and initializes the
14608    linked list.  */
14609
14610 void
14611 output_compiler_stub (void)
14612 {
14613   char tmp_buf[256];
14614   char label_buf[256];
14615   tree stub;
14616
14617   if (!flag_pic)
14618     for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
14619       {
14620         fprintf (asm_out_file,
14621                  "%s:\n", IDENTIFIER_POINTER(STUB_LABEL_NAME(stub)));
14622
14623 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
14624         if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
14625           fprintf (asm_out_file, "\t.stabd 68,0,%d\n", STUB_LINE_NUMBER(stub));
14626 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
14627
14628         if (IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub))[0] == '*')
14629           strcpy (label_buf,
14630                   IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub))+1);
14631         else
14632           {
14633             label_buf[0] = '_';
14634             strcpy (label_buf+1,
14635                     IDENTIFIER_POINTER (STUB_FUNCTION_NAME (stub)));
14636           }
14637
14638         strcpy (tmp_buf, "lis r12,hi16(");
14639         strcat (tmp_buf, label_buf);
14640         strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
14641         strcat (tmp_buf, label_buf);
14642         strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
14643         output_asm_insn (tmp_buf, 0);
14644
14645 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
14646         if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
14647           fprintf(asm_out_file, "\t.stabd 68,0,%d\n", STUB_LINE_NUMBER (stub));
14648 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
14649       }
14650
14651   stub_list = 0;
14652 }
14653
14654 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
14655    already there or not.  */
14656
14657 int
14658 no_previous_def (tree function_name)
14659 {
14660   tree stub;
14661   for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
14662     if (function_name == STUB_FUNCTION_NAME (stub))
14663       return 0;
14664   return 1;
14665 }
14666
14667 /* GET_PREV_LABEL gets the label name from the previous definition of
14668    the function.  */
14669
14670 tree
14671 get_prev_label (tree function_name)
14672 {
14673   tree stub;
14674   for (stub = stub_list; stub; stub = TREE_CHAIN (stub))
14675     if (function_name == STUB_FUNCTION_NAME (stub))
14676       return STUB_LABEL_NAME (stub);
14677   return 0;
14678 }
14679
14680 /* INSN is either a function call or a millicode call.  It may have an
14681    unconditional jump in its delay slot.  
14682
14683    CALL_DEST is the routine we are calling.  */
14684
14685 char *
14686 output_call (rtx insn, rtx call_dest, int operand_number)
14687 {
14688   static char buf[256];
14689   if (GET_CODE (call_dest) == SYMBOL_REF && TARGET_LONG_BRANCH && !flag_pic)
14690     {
14691       tree labelname;
14692       tree funname = get_identifier (XSTR (call_dest, 0));
14693       
14694       if (no_previous_def (funname))
14695         {
14696           int line_number = 0;
14697           rtx label_rtx = gen_label_rtx ();
14698           char *label_buf, temp_buf[256];
14699           ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
14700                                        CODE_LABEL_NUMBER (label_rtx));
14701           label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
14702           labelname = get_identifier (label_buf);
14703           for (; insn && GET_CODE (insn) != NOTE; insn = PREV_INSN (insn));
14704           if (insn)
14705             line_number = NOTE_LINE_NUMBER (insn);
14706           add_compiler_stub (labelname, funname, line_number);
14707         }
14708       else
14709         labelname = get_prev_label (funname);
14710
14711       sprintf (buf, "jbsr %%z%d,%.246s",
14712                operand_number, IDENTIFIER_POINTER (labelname));
14713       return buf;
14714     }
14715   else
14716     {
14717       sprintf (buf, "bl %%z%d", operand_number);
14718       return buf;
14719     }
14720 }
14721
14722 #endif /* RS6000_LONG_BRANCH */
14723
14724 /* Generate PIC and indirect symbol stubs.  */
14725
14726 void
14727 machopic_output_stub (FILE *file, const char *symb, const char *stub)
14728 {
14729   unsigned int length;
14730   char *symbol_name, *lazy_ptr_name;
14731   char *local_label_0;
14732   static int label = 0;
14733
14734   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
14735   symb = (*targetm.strip_name_encoding) (symb);
14736
14737
14738   length = strlen (symb);
14739   symbol_name = alloca (length + 32);
14740   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
14741
14742   lazy_ptr_name = alloca (length + 32);
14743   GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
14744
14745   if (flag_pic == 2)
14746     machopic_picsymbol_stub1_section ();
14747   else
14748     machopic_symbol_stub1_section ();
14749   fprintf (file, "\t.align 2\n");
14750
14751   fprintf (file, "%s:\n", stub);
14752   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
14753
14754   if (flag_pic == 2)
14755     {
14756       label++;
14757       local_label_0 = alloca (sizeof("\"L0000000000$spb\""));
14758       sprintf (local_label_0, "\"L%011d$spb\"", label);
14759     
14760       fprintf (file, "\tmflr r0\n");
14761       fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
14762       fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
14763       fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
14764                lazy_ptr_name, local_label_0);
14765       fprintf (file, "\tmtlr r0\n");
14766       fprintf (file, "\tlwzu r12,lo16(%s-%s)(r11)\n",
14767                lazy_ptr_name, local_label_0);
14768       fprintf (file, "\tmtctr r12\n");
14769       fprintf (file, "\tbctr\n");
14770     }
14771   else
14772    {
14773      fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
14774      fprintf (file, "\tlwzu r12,lo16(%s)(r11)\n", lazy_ptr_name);
14775      fprintf (file, "\tmtctr r12\n");
14776      fprintf (file, "\tbctr\n");
14777    }
14778   
14779   machopic_lazy_symbol_ptr_section ();
14780   fprintf (file, "%s:\n", lazy_ptr_name);
14781   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
14782   fprintf (file, "\t.long dyld_stub_binding_helper\n");
14783 }
14784
14785 /* Legitimize PIC addresses.  If the address is already
14786    position-independent, we return ORIG.  Newly generated
14787    position-independent addresses go into a reg.  This is REG if non
14788    zero, otherwise we allocate register(s) as necessary.  */
14789
14790 #define SMALL_INT(X) ((unsigned) (INTVAL(X) + 0x8000) < 0x10000)
14791
14792 rtx
14793 rs6000_machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, 
14794                                         rtx reg)
14795 {
14796   rtx base, offset;
14797
14798   if (reg == NULL && ! reload_in_progress && ! reload_completed)
14799     reg = gen_reg_rtx (Pmode);
14800
14801   if (GET_CODE (orig) == CONST)
14802     {
14803       if (GET_CODE (XEXP (orig, 0)) == PLUS
14804           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
14805         return orig;
14806
14807       if (GET_CODE (XEXP (orig, 0)) == PLUS)
14808         {
14809           /* Use a different reg for the intermediate value, as
14810              it will be marked UNCHANGING. */
14811           rtx reg_temp = no_new_pseudos ? reg : gen_reg_rtx (Pmode);
14812
14813           base =
14814             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
14815                                                     Pmode, reg_temp);
14816           offset =
14817             rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
14818                                                     Pmode, reg);
14819         }
14820       else
14821         abort ();
14822
14823       if (GET_CODE (offset) == CONST_INT)
14824         {
14825           if (SMALL_INT (offset))
14826             return plus_constant (base, INTVAL (offset));
14827           else if (! reload_in_progress && ! reload_completed)
14828             offset = force_reg (Pmode, offset);
14829           else
14830             {
14831               rtx mem = force_const_mem (Pmode, orig);
14832               return machopic_legitimize_pic_address (mem, Pmode, reg);
14833             }
14834         }
14835       return gen_rtx (PLUS, Pmode, base, offset);
14836     }
14837
14838   /* Fall back on generic machopic code.  */
14839   return machopic_legitimize_pic_address (orig, mode, reg);
14840 }
14841
14842 /* This is just a placeholder to make linking work without having to
14843    add this to the generic Darwin EXTRA_SECTIONS.  If -mcall-aix is
14844    ever needed for Darwin (not too likely!) this would have to get a
14845    real definition.  */
14846
14847 void
14848 toc_section (void)
14849 {
14850 }
14851
14852 #endif /* TARGET_MACHO */
14853
14854 #if TARGET_ELF
14855 static unsigned int
14856 rs6000_elf_section_type_flags (tree decl, const char *name, int reloc)
14857 {
14858   unsigned int flags
14859     = default_section_type_flags_1 (decl, name, reloc,
14860                                     flag_pic || DEFAULT_ABI == ABI_AIX);
14861
14862   if (TARGET_RELOCATABLE)
14863     flags |= SECTION_WRITE;
14864
14865   return flags;
14866 }
14867
14868 /* Record an element in the table of global constructors.  SYMBOL is
14869    a SYMBOL_REF of the function to be called; PRIORITY is a number
14870    between 0 and MAX_INIT_PRIORITY.
14871
14872    This differs from default_named_section_asm_out_constructor in
14873    that we have special handling for -mrelocatable.  */
14874
14875 static void
14876 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
14877 {
14878   const char *section = ".ctors";
14879   char buf[16];
14880
14881   if (priority != DEFAULT_INIT_PRIORITY)
14882     {
14883       sprintf (buf, ".ctors.%.5u",
14884                /* Invert the numbering so the linker puts us in the proper
14885                   order; constructors are run from right to left, and the
14886                   linker sorts in increasing order.  */
14887                MAX_INIT_PRIORITY - priority);
14888       section = buf;
14889     }
14890
14891   named_section_flags (section, SECTION_WRITE);
14892   assemble_align (POINTER_SIZE);
14893
14894   if (TARGET_RELOCATABLE)
14895     {
14896       fputs ("\t.long (", asm_out_file);
14897       output_addr_const (asm_out_file, symbol);
14898       fputs (")@fixup\n", asm_out_file);
14899     }
14900   else
14901     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
14902 }
14903
14904 static void
14905 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
14906 {
14907   const char *section = ".dtors";
14908   char buf[16];
14909
14910   if (priority != DEFAULT_INIT_PRIORITY)
14911     {
14912       sprintf (buf, ".dtors.%.5u",
14913                /* Invert the numbering so the linker puts us in the proper
14914                   order; constructors are run from right to left, and the
14915                   linker sorts in increasing order.  */
14916                MAX_INIT_PRIORITY - priority);
14917       section = buf;
14918     }
14919
14920   named_section_flags (section, SECTION_WRITE);
14921   assemble_align (POINTER_SIZE);
14922
14923   if (TARGET_RELOCATABLE)
14924     {
14925       fputs ("\t.long (", asm_out_file);
14926       output_addr_const (asm_out_file, symbol);
14927       fputs (")@fixup\n", asm_out_file);
14928     }
14929   else
14930     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
14931 }
14932
14933 void
14934 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
14935 {
14936   if (TARGET_64BIT)
14937     {
14938       fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
14939       ASM_OUTPUT_LABEL (file, name);
14940       fputs (DOUBLE_INT_ASM_OP, file);
14941       putc ('.', file);
14942       assemble_name (file, name);
14943       fputs (",.TOC.@tocbase,0\n\t.previous\n\t.size\t", file);
14944       assemble_name (file, name);
14945       fputs (",24\n\t.type\t.", file);
14946       assemble_name (file, name);
14947       fputs (",@function\n", file);
14948       if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
14949         {
14950           fputs ("\t.globl\t.", file);
14951           assemble_name (file, name);
14952           putc ('\n', file);
14953         }
14954       ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
14955       putc ('.', file);
14956       ASM_OUTPUT_LABEL (file, name);
14957       return;
14958     }
14959
14960   if (TARGET_RELOCATABLE
14961       && (get_pool_size () != 0 || current_function_profile)
14962       && uses_TOC())
14963     {
14964       char buf[256];
14965
14966       (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
14967
14968       ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
14969       fprintf (file, "\t.long ");
14970       assemble_name (file, buf);
14971       putc ('-', file);
14972       ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
14973       assemble_name (file, buf);
14974       putc ('\n', file);
14975     }
14976
14977   ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
14978   ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
14979
14980   if (DEFAULT_ABI == ABI_AIX)
14981     {
14982       const char *desc_name, *orig_name;
14983
14984       orig_name = (*targetm.strip_name_encoding) (name);
14985       desc_name = orig_name;
14986       while (*desc_name == '.')
14987         desc_name++;
14988
14989       if (TREE_PUBLIC (decl))
14990         fprintf (file, "\t.globl %s\n", desc_name);
14991
14992       fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
14993       fprintf (file, "%s:\n", desc_name);
14994       fprintf (file, "\t.long %s\n", orig_name);
14995       fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
14996       if (DEFAULT_ABI == ABI_AIX)
14997         fputs ("\t.long 0\n", file);
14998       fprintf (file, "\t.previous\n");
14999     }
15000   ASM_OUTPUT_LABEL (file, name);
15001 }
15002 #endif
15003
15004 #if TARGET_XCOFF
15005 static void
15006 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
15007 {
15008   fputs (GLOBAL_ASM_OP, stream);
15009   RS6000_OUTPUT_BASENAME (stream, name);
15010   putc ('\n', stream);
15011 }
15012
15013 static void
15014 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags)
15015 {
15016   int smclass;
15017   static const char * const suffix[3] = { "PR", "RO", "RW" };
15018
15019   if (flags & SECTION_CODE)
15020     smclass = 0;
15021   else if (flags & SECTION_WRITE)
15022     smclass = 2;
15023   else
15024     smclass = 1;
15025
15026   fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
15027            (flags & SECTION_CODE) ? "." : "",
15028            name, suffix[smclass], flags & SECTION_ENTSIZE);
15029 }
15030
15031 static void
15032 rs6000_xcoff_select_section (tree decl, int reloc, 
15033                             unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
15034 {
15035   if (decl_readonly_section_1 (decl, reloc, 1))
15036     {
15037       if (TREE_PUBLIC (decl))
15038         read_only_data_section ();
15039       else
15040         read_only_private_data_section ();
15041     }
15042   else
15043     {
15044       if (TREE_PUBLIC (decl))
15045         data_section ();
15046       else
15047         private_data_section ();
15048     }
15049 }
15050
15051 static void
15052 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
15053 {
15054   const char *name;
15055
15056   /* Use select_section for private and uninitialized data.  */
15057   if (!TREE_PUBLIC (decl)
15058       || DECL_COMMON (decl)
15059       || DECL_INITIAL (decl) == NULL_TREE
15060       || DECL_INITIAL (decl) == error_mark_node
15061       || (flag_zero_initialized_in_bss
15062           && initializer_zerop (DECL_INITIAL (decl))))
15063     return;
15064
15065   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15066   name = (*targetm.strip_name_encoding) (name);
15067   DECL_SECTION_NAME (decl) = build_string (strlen (name), name);
15068 }
15069
15070 /* Select section for constant in constant pool.
15071
15072    On RS/6000, all constants are in the private read-only data area.
15073    However, if this is being placed in the TOC it must be output as a
15074    toc entry.  */
15075
15076 static void
15077 rs6000_xcoff_select_rtx_section (enum machine_mode mode, rtx x, 
15078                                 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
15079 {
15080   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
15081     toc_section ();
15082   else
15083     read_only_private_data_section ();
15084 }
15085
15086 /* Remove any trailing [DS] or the like from the symbol name.  */
15087
15088 static const char *
15089 rs6000_xcoff_strip_name_encoding (const char *name)
15090 {
15091   size_t len;
15092   if (*name == '*')
15093     name++;
15094   len = strlen (name);
15095   if (name[len - 1] == ']')
15096     return ggc_alloc_string (name, len - 4);
15097   else
15098     return name;
15099 }
15100
15101 /* Section attributes.  AIX is always PIC.  */
15102
15103 static unsigned int
15104 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
15105 {
15106   unsigned int align;
15107   unsigned int flags = default_section_type_flags_1 (decl, name, reloc, 1);
15108
15109   /* Align to at least UNIT size.  */
15110   if (flags & SECTION_CODE)
15111     align = MIN_UNITS_PER_WORD;
15112   else
15113     /* Increase alignment of large objects if not already stricter.  */
15114     align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
15115                  int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
15116                  ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
15117
15118   return flags | (exact_log2 (align) & SECTION_ENTSIZE);
15119 }
15120
15121 /* Output at beginning of assembler file.
15122
15123    Initialize the section names for the RS/6000 at this point.
15124
15125    Specify filename, including full path, to assembler.
15126
15127    We want to go into the TOC section so at least one .toc will be emitted.
15128    Also, in order to output proper .bs/.es pairs, we need at least one static
15129    [RW] section emitted.
15130
15131    Finally, declare mcount when profiling to make the assembler happy.  */
15132
15133 static void
15134 rs6000_xcoff_file_start (void)
15135 {
15136   rs6000_gen_section_name (&xcoff_bss_section_name,
15137                            main_input_filename, ".bss_");
15138   rs6000_gen_section_name (&xcoff_private_data_section_name,
15139                            main_input_filename, ".rw_");
15140   rs6000_gen_section_name (&xcoff_read_only_section_name,
15141                            main_input_filename, ".ro_");
15142
15143   fputs ("\t.file\t", asm_out_file);
15144   output_quoted_string (asm_out_file, main_input_filename);
15145   fputc ('\n', asm_out_file);
15146   toc_section ();
15147   if (write_symbols != NO_DEBUG)
15148     private_data_section ();
15149   text_section ();
15150   if (profile_flag)
15151     fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
15152   rs6000_file_start ();
15153 }
15154
15155 /* Output at end of assembler file.
15156    On the RS/6000, referencing data should automatically pull in text.  */
15157
15158 static void
15159 rs6000_xcoff_file_end (void)
15160 {
15161   text_section ();
15162   fputs ("_section_.text:\n", asm_out_file);
15163   data_section ();
15164   fputs (TARGET_32BIT
15165          ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
15166          asm_out_file);
15167 }
15168 #endif /* TARGET_XCOFF */
15169
15170 #if TARGET_MACHO
15171 /* Cross-module name binding.  Darwin does not support overriding
15172    functions at dynamic-link time.  */
15173
15174 static bool
15175 rs6000_binds_local_p (tree decl)
15176 {
15177   return default_binds_local_p_1 (decl, 0);
15178 }
15179 #endif
15180
15181 /* Compute a (partial) cost for rtx X.  Return true if the complete
15182    cost has been computed, and false if subexpressions should be
15183    scanned.  In either case, *TOTAL contains the cost result.  */
15184
15185 static bool
15186 rs6000_rtx_costs (rtx x, int code, int outer_code ATTRIBUTE_UNUSED, 
15187                   int *total)
15188 {
15189   switch (code)
15190     {
15191       /* On the RS/6000, if it is valid in the insn, it is free.
15192          So this always returns 0.  */
15193     case CONST_INT:
15194     case CONST:
15195     case LABEL_REF:
15196     case SYMBOL_REF:
15197     case CONST_DOUBLE:
15198     case HIGH:
15199       *total = 0;
15200       return true;
15201
15202     case PLUS:
15203       *total = ((GET_CODE (XEXP (x, 1)) == CONST_INT
15204                  && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1))
15205                                                + 0x8000) >= 0x10000)
15206                  && ((INTVAL (XEXP (x, 1)) & 0xffff) != 0))
15207                 ? COSTS_N_INSNS (2)
15208                 : COSTS_N_INSNS (1));
15209       return true;
15210
15211     case AND:
15212     case IOR:
15213     case XOR:
15214       *total = ((GET_CODE (XEXP (x, 1)) == CONST_INT
15215                  && (INTVAL (XEXP (x, 1)) & (~ (HOST_WIDE_INT) 0xffff)) != 0
15216                  && ((INTVAL (XEXP (x, 1)) & 0xffff) != 0))
15217                 ? COSTS_N_INSNS (2)
15218                 : COSTS_N_INSNS (1));
15219       return true;
15220
15221     case MULT:
15222       if (optimize_size)
15223         {
15224           *total = COSTS_N_INSNS (2);
15225           return true;
15226         }
15227       switch (rs6000_cpu)
15228         {
15229         case PROCESSOR_RIOS1:
15230         case PROCESSOR_PPC405:
15231           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15232                     ? COSTS_N_INSNS (5)
15233                     : (INTVAL (XEXP (x, 1)) >= -256
15234                        && INTVAL (XEXP (x, 1)) <= 255)
15235                     ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4));
15236           return true;
15237
15238         case PROCESSOR_PPC440:
15239           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15240                     ? COSTS_N_INSNS (3)
15241                     : COSTS_N_INSNS (2));
15242           return true;
15243
15244         case PROCESSOR_RS64A:
15245           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15246                     ? GET_MODE (XEXP (x, 1)) != DImode
15247                     ? COSTS_N_INSNS (20) : COSTS_N_INSNS (34)
15248                     : (INTVAL (XEXP (x, 1)) >= -256
15249                        && INTVAL (XEXP (x, 1)) <= 255)
15250                     ? COSTS_N_INSNS (8) : COSTS_N_INSNS (12));
15251           return true;
15252
15253         case PROCESSOR_RIOS2:
15254         case PROCESSOR_MPCCORE:
15255         case PROCESSOR_PPC604e:
15256           *total = COSTS_N_INSNS (2);
15257           return true;
15258
15259         case PROCESSOR_PPC601:
15260           *total = COSTS_N_INSNS (5);
15261           return true;
15262
15263         case PROCESSOR_PPC603:
15264         case PROCESSOR_PPC7400:
15265         case PROCESSOR_PPC750:
15266           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15267                     ? COSTS_N_INSNS (5)
15268                     : (INTVAL (XEXP (x, 1)) >= -256
15269                        && INTVAL (XEXP (x, 1)) <= 255)
15270                     ? COSTS_N_INSNS (2) : COSTS_N_INSNS (3));
15271           return true;
15272
15273         case PROCESSOR_PPC7450:
15274           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15275                     ? COSTS_N_INSNS (4)
15276                     : COSTS_N_INSNS (3));
15277           return true;
15278
15279         case PROCESSOR_PPC403:
15280         case PROCESSOR_PPC604:
15281         case PROCESSOR_PPC8540:
15282           *total = COSTS_N_INSNS (4);
15283           return true;
15284
15285         case PROCESSOR_PPC620:
15286         case PROCESSOR_PPC630:
15287           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15288                     ? GET_MODE (XEXP (x, 1)) != DImode
15289                     ? COSTS_N_INSNS (5) : COSTS_N_INSNS (7)
15290                     : (INTVAL (XEXP (x, 1)) >= -256
15291                        && INTVAL (XEXP (x, 1)) <= 255)
15292                     ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4));
15293           return true;
15294
15295         case PROCESSOR_POWER4:
15296           *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15297                     ? GET_MODE (XEXP (x, 1)) != DImode
15298                     ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4)
15299                     : COSTS_N_INSNS (2));
15300           return true;
15301
15302         default:
15303           abort ();
15304         }
15305
15306     case DIV:
15307     case MOD:
15308       if (GET_CODE (XEXP (x, 1)) == CONST_INT
15309           && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
15310         {
15311           *total = COSTS_N_INSNS (2);
15312           return true;
15313         }
15314       /* FALLTHRU */
15315
15316     case UDIV:
15317     case UMOD:
15318       switch (rs6000_cpu)
15319         {
15320         case PROCESSOR_RIOS1:
15321           *total = COSTS_N_INSNS (19);
15322           return true;
15323
15324         case PROCESSOR_RIOS2:
15325           *total = COSTS_N_INSNS (13);
15326           return true;
15327
15328         case PROCESSOR_RS64A:
15329           *total = (GET_MODE (XEXP (x, 1)) != DImode
15330                     ? COSTS_N_INSNS (65)
15331                     : COSTS_N_INSNS (67));
15332           return true;
15333
15334         case PROCESSOR_MPCCORE:
15335           *total = COSTS_N_INSNS (6);
15336           return true;
15337
15338         case PROCESSOR_PPC403:
15339           *total = COSTS_N_INSNS (33);
15340           return true;
15341
15342         case PROCESSOR_PPC405:
15343           *total = COSTS_N_INSNS (35);
15344           return true;
15345
15346         case PROCESSOR_PPC440:
15347           *total = COSTS_N_INSNS (34);
15348           return true;
15349
15350         case PROCESSOR_PPC601:
15351           *total = COSTS_N_INSNS (36);
15352           return true;
15353
15354         case PROCESSOR_PPC603:
15355           *total = COSTS_N_INSNS (37);
15356           return true;
15357
15358         case PROCESSOR_PPC604:
15359         case PROCESSOR_PPC604e:
15360           *total = COSTS_N_INSNS (20);
15361           return true;
15362
15363         case PROCESSOR_PPC620:
15364         case PROCESSOR_PPC630:
15365           *total = (GET_MODE (XEXP (x, 1)) != DImode
15366                     ? COSTS_N_INSNS (21)
15367                     : COSTS_N_INSNS (37));
15368           return true;
15369
15370         case PROCESSOR_PPC750:
15371         case PROCESSOR_PPC8540:
15372         case PROCESSOR_PPC7400:
15373           *total = COSTS_N_INSNS (19);
15374           return true;
15375
15376         case PROCESSOR_PPC7450:
15377           *total = COSTS_N_INSNS (23);
15378           return true;
15379
15380         case PROCESSOR_POWER4:
15381           *total = (GET_MODE (XEXP (x, 1)) != DImode
15382                     ? COSTS_N_INSNS (18)
15383                     : COSTS_N_INSNS (34));
15384           return true;
15385
15386         default:
15387           abort ();
15388         }
15389
15390     case FFS:
15391       *total = COSTS_N_INSNS (4);
15392       return true;
15393
15394     case MEM:
15395       /* MEM should be slightly more expensive than (plus (reg) (const)) */
15396       *total = 5;
15397       return true;
15398
15399     default:
15400       return false;
15401     }
15402 }
15403
15404 /* A C expression returning the cost of moving data from a register of class
15405    CLASS1 to one of CLASS2.  */
15406
15407 int
15408 rs6000_register_move_cost (enum machine_mode mode, 
15409                            enum reg_class from, enum reg_class to)
15410 {
15411   /*  Moves from/to GENERAL_REGS.  */
15412   if (reg_classes_intersect_p (to, GENERAL_REGS)
15413       || reg_classes_intersect_p (from, GENERAL_REGS))
15414     {
15415       if (! reg_classes_intersect_p (to, GENERAL_REGS))
15416         from = to;
15417
15418       if (from == FLOAT_REGS || from == ALTIVEC_REGS)
15419         return (rs6000_memory_move_cost (mode, from, 0)
15420                 + rs6000_memory_move_cost (mode, GENERAL_REGS, 0));
15421
15422 /* It's more expensive to move CR_REGS than CR0_REGS because of the shift...*/
15423       else if (from == CR_REGS)
15424         return 4;
15425
15426       else
15427 /* A move will cost one instruction per GPR moved.  */
15428         return 2 * HARD_REGNO_NREGS (0, mode);
15429     }
15430
15431 /* Moving between two similar registers is just one instruction.  */
15432   else if (reg_classes_intersect_p (to, from))
15433     return mode == TFmode ? 4 : 2;
15434
15435 /* Everything else has to go through GENERAL_REGS.  */
15436   else
15437     return (rs6000_register_move_cost (mode, GENERAL_REGS, to) 
15438             + rs6000_register_move_cost (mode, from, GENERAL_REGS));
15439 }
15440
15441 /* A C expressions returning the cost of moving data of MODE from a register to
15442    or from memory.  */
15443
15444 int
15445 rs6000_memory_move_cost (enum machine_mode mode, enum reg_class class, 
15446                          int in ATTRIBUTE_UNUSED)
15447 {
15448   if (reg_classes_intersect_p (class, GENERAL_REGS))
15449     return 4 * HARD_REGNO_NREGS (0, mode);
15450   else if (reg_classes_intersect_p (class, FLOAT_REGS))
15451     return 4 * HARD_REGNO_NREGS (32, mode);
15452   else if (reg_classes_intersect_p (class, ALTIVEC_REGS))
15453     return 4 * HARD_REGNO_NREGS (FIRST_ALTIVEC_REGNO, mode);
15454   else
15455     return 4 + rs6000_register_move_cost (mode, class, GENERAL_REGS);
15456 }
15457
15458 /* Return an RTX representing where to find the function value of a
15459    function returning MODE.  */
15460 static rtx
15461 rs6000_complex_function_value (enum machine_mode mode)
15462 {
15463   unsigned int regno;
15464   rtx r1, r2;
15465   enum machine_mode inner = GET_MODE_INNER (mode);
15466
15467   if (FLOAT_MODE_P (mode))
15468     regno = FP_ARG_RETURN;
15469   else
15470     {
15471       regno = GP_ARG_RETURN;
15472
15473       /* 32-bit is OK since it'll go in r3/r4.  */
15474       if (TARGET_32BIT
15475           && GET_MODE_BITSIZE (inner) >= 32)
15476         return gen_rtx_REG (mode, regno);
15477     }
15478
15479   r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
15480                           const0_rtx);
15481   r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
15482                           GEN_INT (GET_MODE_UNIT_SIZE (inner)));
15483   return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
15484 }
15485
15486 /* Define how to find the value returned by a function.
15487    VALTYPE is the data type of the value (as a tree).
15488    If the precise function being called is known, FUNC is its FUNCTION_DECL;
15489    otherwise, FUNC is 0.
15490
15491    On the SPE, both FPs and vectors are returned in r3.
15492
15493    On RS/6000 an integer value is in r3 and a floating-point value is in
15494    fp1, unless -msoft-float.  */
15495
15496 rtx
15497 rs6000_function_value (tree valtype, tree func ATTRIBUTE_UNUSED)
15498 {
15499   enum machine_mode mode;
15500   unsigned int regno;
15501
15502   if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DImode)
15503     {
15504       /* Long long return value need be split in -mpowerpc64, 32bit ABI.  */
15505       return gen_rtx_PARALLEL (DImode,
15506         gen_rtvec (2,
15507                    gen_rtx_EXPR_LIST (VOIDmode,
15508                                       gen_rtx_REG (SImode, GP_ARG_RETURN),
15509                                       const0_rtx),
15510                    gen_rtx_EXPR_LIST (VOIDmode,
15511                                       gen_rtx_REG (SImode,
15512                                                    GP_ARG_RETURN + 1),
15513                                       GEN_INT (4))));
15514     }
15515
15516   if ((INTEGRAL_TYPE_P (valtype)
15517        && TYPE_PRECISION (valtype) < BITS_PER_WORD)
15518       || POINTER_TYPE_P (valtype))
15519     mode = TARGET_32BIT ? SImode : DImode;
15520   else
15521     mode = TYPE_MODE (valtype);
15522
15523   if (TREE_CODE (valtype) == REAL_TYPE && TARGET_HARD_FLOAT && TARGET_FPRS)
15524     regno = FP_ARG_RETURN;
15525   else if (TREE_CODE (valtype) == COMPLEX_TYPE
15526            && TARGET_HARD_FLOAT
15527            && SPLIT_COMPLEX_ARGS)
15528     return rs6000_complex_function_value (mode);
15529   else if (TREE_CODE (valtype) == VECTOR_TYPE && TARGET_ALTIVEC)
15530     regno = ALTIVEC_ARG_RETURN;
15531   else
15532     regno = GP_ARG_RETURN;
15533
15534   return gen_rtx_REG (mode, regno);
15535 }
15536
15537 /* Define how to find the value returned by a library function
15538    assuming the value has mode MODE.  */
15539 rtx
15540 rs6000_libcall_value (enum machine_mode mode)
15541 {
15542   unsigned int regno;
15543
15544   if (GET_MODE_CLASS (mode) == MODE_FLOAT
15545            && TARGET_HARD_FLOAT && TARGET_FPRS)
15546     regno = FP_ARG_RETURN;
15547   else if (ALTIVEC_VECTOR_MODE (mode))
15548     regno = ALTIVEC_ARG_RETURN;
15549   else if (COMPLEX_MODE_P (mode) && SPLIT_COMPLEX_ARGS)
15550     return rs6000_complex_function_value (mode);
15551   else
15552     regno = GP_ARG_RETURN;
15553
15554   return gen_rtx_REG (mode, regno);
15555 }
15556
15557 /* Return true if TYPE is of type __ev64_opaque__.  */
15558
15559 static bool
15560 is_ev64_opaque_type (tree type)
15561 {
15562   return (TARGET_SPE
15563           && (type == opaque_V2SI_type_node
15564               || type == opaque_V2SF_type_node
15565               || type == opaque_p_V2SI_type_node));
15566 }
15567
15568 static rtx
15569 rs6000_dwarf_register_span (rtx reg)
15570 {
15571   unsigned regno;
15572
15573   if (!TARGET_SPE || !SPE_VECTOR_MODE (GET_MODE (reg)))
15574     return NULL_RTX;
15575
15576   regno = REGNO (reg);
15577
15578   /* The duality of the SPE register size wreaks all kinds of havoc.
15579      This is a way of distinguishing r0 in 32-bits from r0 in
15580      64-bits.  */
15581   return
15582     gen_rtx_PARALLEL (VOIDmode,
15583                       BYTES_BIG_ENDIAN
15584                       ? gen_rtvec (2,
15585                                    gen_rtx_REG (SImode, regno + 1200),
15586                                    gen_rtx_REG (SImode, regno))
15587                       : gen_rtvec (2,
15588                                    gen_rtx_REG (SImode, regno),
15589                                    gen_rtx_REG (SImode, regno + 1200)));
15590 }
15591
15592 #include "gt-rs6000.h"